]> git.pld-linux.org Git - packages/kernel.git/blame_incremental - kernel-aufs5.patch
- 5.10.62
[packages/kernel.git] / kernel-aufs5.patch
... / ...
CommitLineData
1SPDX-License-Identifier: GPL-2.0
2aufs5.10 kbuild patch
3
4diff --git a/fs/Kconfig b/fs/Kconfig
5index aa4c122823018..b29bad13b2491 100644
6--- a/fs/Kconfig
7+++ b/fs/Kconfig
8@@ -288,6 +288,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 999d1a23f036c..0cd76857ca764 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.10 base patch
27
28diff --git a/MAINTAINERS b/MAINTAINERS
29index 281de213ef478..ffc3c1c539ea2 100644
30--- a/MAINTAINERS
31+++ b/MAINTAINERS
32@@ -3009,6 +3009,19 @@ F: include/linux/audit.h
33 F: include/uapi/linux/audit.h
34 F: kernel/audit*
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 Sandonis <miguel.ojeda.sandonis@gmail.com>
51 S: Maintained
52diff --git a/drivers/block/loop.c b/drivers/block/loop.c
53index a58084c2ed7ce..7be7ca3f54547 100644
54--- a/drivers/block/loop.c
55+++ b/drivers/block/loop.c
56@@ -765,6 +765,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 ea0485861d937..ddca6240e0db4 100644
83--- a/fs/dcache.c
84+++ b/fs/dcache.c
85@@ -1285,7 +1285,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 19ac5baad50fd..073ca6af500f3 100644
96--- a/fs/fcntl.c
97+++ b/fs/fcntl.c
98@@ -32,7 +32,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@@ -63,6 +63,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 9d78c37b00b81..e7f0c614a58f3 100644
118--- a/fs/inode.c
119+++ b/fs/inode.c
120@@ -1770,7 +1770,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 cebaa3e817940..451dee24a546a 100644
131--- a/fs/namespace.c
132+++ b/fs/namespace.c
133@@ -792,6 +792,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 866d5c2367b23..55b5356262085 100644
148--- a/fs/splice.c
149+++ b/fs/splice.c
150@@ -756,8 +756,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@@ -767,9 +767,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 int ret;
173
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 8667d0cdc71e7..837dd6e727e45 100644
189--- a/include/linux/fs.h
190+++ b/include/linux/fs.h
191@@ -1332,6 +1332,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@@ -1843,6 +1844,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@@ -2328,6 +2330,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@@ -2564,6 +2567,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 f5594879175a6..93bb861981675 100644
225--- a/include/linux/lockdep.h
226+++ b/include/linux/lockdep.h
227@@ -241,6 +241,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 *
236@@ -375,6 +377,7 @@ static inline void lockdep_unregister_key(struct lock_class_key *key)
237
238 #define lockdep_depth(tsk) (0)
239
240+#define lockdep_is_held(lock) (1)
241 #define lockdep_is_held_type(l, r) (1)
242
243 #define lockdep_assert_held(l) do { (void)(l); } while (0)
244diff --git a/include/linux/mnt_namespace.h b/include/linux/mnt_namespace.h
245index 8f882f5881e87..6b9808f098435 100644
246--- a/include/linux/mnt_namespace.h
247+++ b/include/linux/mnt_namespace.h
248@@ -7,12 +7,15 @@ struct mnt_namespace;
249 struct fs_struct;
250 struct user_namespace;
251 struct ns_common;
252+struct vfsmount;
253
254 extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
255 struct user_namespace *, struct fs_struct *);
256 extern void put_mnt_ns(struct mnt_namespace *ns);
257 extern struct ns_common *from_mnt_ns(struct mnt_namespace *);
258
259+extern int is_current_mnt_ns(struct vfsmount *mnt);
260+
261 extern const struct file_operations proc_mounts_operations;
262 extern const struct file_operations proc_mountinfo_operations;
263 extern const struct file_operations proc_mountstats_operations;
264diff --git a/include/linux/splice.h b/include/linux/splice.h
265index a55179fd60fc3..8e21c53cf8831 100644
266--- a/include/linux/splice.h
267+++ b/include/linux/splice.h
268@@ -93,4 +93,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
269
270 extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
271 extern const struct pipe_buf_operations default_pipe_buf_ops;
272+
273+extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
274+ loff_t *ppos, size_t len, unsigned int flags);
275+extern long do_splice_to(struct file *in, loff_t *ppos,
276+ struct pipe_inode_info *pipe, size_t len,
277+ unsigned int flags);
278 #endif
279diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
280index c1418b47f625a..7f006a47790cf 100644
281--- a/kernel/locking/lockdep.c
282+++ b/kernel/locking/lockdep.c
283@@ -188,7 +188,7 @@ static
284 struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
285 static DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
286
287-static inline struct lock_class *hlock_class(struct held_lock *hlock)
288+inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
289 {
290 unsigned int class_idx = hlock->class_idx;
291
292@@ -209,6 +209,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
293 */
294 return lock_classes + class_idx;
295 }
296+#define hlock_class(hlock) lockdep_hlock_class(hlock)
297
298 #ifdef CONFIG_LOCK_STAT
299 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
300SPDX-License-Identifier: GPL-2.0
301aufs5.10 mmap patch
302
303diff --git a/fs/proc/base.c b/fs/proc/base.c
304index b362523a9829a..669448bb8a738 100644
305--- a/fs/proc/base.c
306+++ b/fs/proc/base.c
307@@ -2184,7 +2184,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
308 rc = -ENOENT;
309 vma = find_exact_vma(mm, vm_start, vm_end);
310 if (vma && vma->vm_file) {
311- *path = vma->vm_file->f_path;
312+ *path = vma_pr_or_file(vma)->f_path;
313 path_get(path);
314 rc = 0;
315 }
316diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
317index 13452b32e2bd5..38acccfef9d49 100644
318--- a/fs/proc/nommu.c
319+++ b/fs/proc/nommu.c
320@@ -40,7 +40,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
321 file = region->vm_file;
322
323 if (file) {
324- struct inode *inode = file_inode(region->vm_file);
325+ struct inode *inode;
326+
327+ file = vmr_pr_or_file(region);
328+ inode = file_inode(file);
329 dev = inode->i_sb->s_dev;
330 ino = inode->i_ino;
331 }
332diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
333index ee5a235b30562..80a46dcede7be 100644
334--- a/fs/proc/task_mmu.c
335+++ b/fs/proc/task_mmu.c
336@@ -280,7 +280,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
337 const char *name = NULL;
338
339 if (file) {
340- struct inode *inode = file_inode(vma->vm_file);
341+ struct inode *inode;
342+
343+ file = vma_pr_or_file(vma);
344+ inode = file_inode(file);
345 dev = inode->i_sb->s_dev;
346 ino = inode->i_ino;
347 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
348@@ -1855,7 +1858,7 @@ static int show_numa_map(struct seq_file *m, void *v)
349 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
350 struct vm_area_struct *vma = v;
351 struct numa_maps *md = &numa_priv->md;
352- struct file *file = vma->vm_file;
353+ struct file *file = vma_pr_or_file(vma);
354 struct mm_struct *mm = vma->vm_mm;
355 struct mempolicy *pol;
356 char buffer[64];
357diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
358index a6d21fc0033c6..02c2de31196e0 100644
359--- a/fs/proc/task_nommu.c
360+++ b/fs/proc/task_nommu.c
361@@ -155,7 +155,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
362 file = vma->vm_file;
363
364 if (file) {
365- struct inode *inode = file_inode(vma->vm_file);
366+ struct inode *inode;
367+
368+ file = vma_pr_or_file(vma);
369+ inode = file_inode(file);
370 dev = inode->i_sb->s_dev;
371 ino = inode->i_ino;
372 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
373diff --git a/include/linux/mm.h b/include/linux/mm.h
374index db6ae4d3fb4ed..1a632192d9d9f 100644
375--- a/include/linux/mm.h
376+++ b/include/linux/mm.h
377@@ -1712,6 +1712,28 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
378 unmap_mapping_range(mapping, holebegin, holelen, 0);
379 }
380
381+extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
382+extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
383+ int);
384+extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
385+extern void vma_do_fput(struct vm_area_struct *, const char[], int);
386+
387+#define vma_file_update_time(vma) vma_do_file_update_time(vma, __func__, \
388+ __LINE__)
389+#define vma_pr_or_file(vma) vma_do_pr_or_file(vma, __func__, \
390+ __LINE__)
391+#define vma_get_file(vma) vma_do_get_file(vma, __func__, __LINE__)
392+#define vma_fput(vma) vma_do_fput(vma, __func__, __LINE__)
393+
394+#ifndef CONFIG_MMU
395+extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
396+extern void vmr_do_fput(struct vm_region *, const char[], int);
397+
398+#define vmr_pr_or_file(region) vmr_do_pr_or_file(region, __func__, \
399+ __LINE__)
400+#define vmr_fput(region) vmr_do_fput(region, __func__, __LINE__)
401+#endif /* !CONFIG_MMU */
402+
403 extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
404 void *buf, int len, unsigned int gup_flags);
405 extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
406diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
407index 5a9238f6caad9..ad387c3cb14fc 100644
408--- a/include/linux/mm_types.h
409+++ b/include/linux/mm_types.h
410@@ -280,6 +280,7 @@ struct vm_region {
411 unsigned long vm_top; /* region allocated to here */
412 unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */
413 struct file *vm_file; /* the backing file or NULL */
414+ struct file *vm_prfile; /* the virtual backing file or NULL */
415
416 int vm_usage; /* region usage count (access under nommu_region_sem) */
417 bool vm_icache_flushed : 1; /* true if the icache has been flushed for
418@@ -359,6 +360,7 @@ struct vm_area_struct {
419 unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
420 units */
421 struct file * vm_file; /* File we map to (can be NULL). */
422+ struct file *vm_prfile; /* shadow of vm_file */
423 void * vm_private_data; /* was vm_pte (shared mem) */
424
425 #ifdef CONFIG_SWAP
426diff --git a/kernel/fork.c b/kernel/fork.c
427index 6d266388d3804..dd7f13c3bee7d 100644
428--- a/kernel/fork.c
429+++ b/kernel/fork.c
430@@ -554,7 +554,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
431 struct inode *inode = file_inode(file);
432 struct address_space *mapping = file->f_mapping;
433
434- get_file(file);
435+ vma_get_file(tmp);
436 if (tmp->vm_flags & VM_DENYWRITE)
437 put_write_access(inode);
438 i_mmap_lock_write(mapping);
439diff --git a/mm/Makefile b/mm/Makefile
440index d73aed0fc99c1..93076a66ad6a5 100644
441--- a/mm/Makefile
442+++ b/mm/Makefile
443@@ -52,7 +52,7 @@ obj-y := filemap.o mempool.o oom_kill.o fadvise.o \
444 mm_init.o percpu.o slab_common.o \
445 compaction.o vmacache.o \
446 interval_tree.o list_lru.o workingset.o \
447- debug.o gup.o $(mmu-y)
448+ prfile.o debug.o gup.o $(mmu-y)
449
450 # Give 'page_alloc' its own module-parameter namespace
451 page-alloc-y := page_alloc.o
452diff --git a/mm/filemap.c b/mm/filemap.c
453index 0b2067b3c3283..3334207431c79 100644
454--- a/mm/filemap.c
455+++ b/mm/filemap.c
456@@ -2909,7 +2909,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
457 vm_fault_t ret = VM_FAULT_LOCKED;
458
459 sb_start_pagefault(inode->i_sb);
460- file_update_time(vmf->vma->vm_file);
461+ vma_file_update_time(vmf->vma);
462 lock_page(page);
463 if (page->mapping != inode->i_mapping) {
464 unlock_page(page);
465diff --git a/mm/mmap.c b/mm/mmap.c
466index 5c8b4485860de..59f2f758d77e5 100644
467--- a/mm/mmap.c
468+++ b/mm/mmap.c
469@@ -179,7 +179,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
470 if (vma->vm_ops && vma->vm_ops->close)
471 vma->vm_ops->close(vma);
472 if (vma->vm_file)
473- fput(vma->vm_file);
474+ vma_fput(vma);
475 mpol_put(vma_policy(vma));
476 vm_area_free(vma);
477 return next;
478@@ -951,7 +951,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
479 if (remove_next) {
480 if (file) {
481 uprobe_munmap(next, next->vm_start, next->vm_end);
482- fput(file);
483+ vma_fput(vma);
484 }
485 if (next->anon_vma)
486 anon_vma_merge(vma, next);
487@@ -1897,8 +1897,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
488 return addr;
489
490 unmap_and_free_vma:
491+ vma_fput(vma);
492 vma->vm_file = NULL;
493- fput(file);
494
495 /* Undo any partial mapping done by a device driver. */
496 unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
497@@ -2757,7 +2757,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
498 goto out_free_mpol;
499
500 if (new->vm_file)
501- get_file(new->vm_file);
502+ vma_get_file(new);
503
504 if (new->vm_ops && new->vm_ops->open)
505 new->vm_ops->open(new);
506@@ -2776,7 +2776,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
507 if (new->vm_ops && new->vm_ops->close)
508 new->vm_ops->close(new);
509 if (new->vm_file)
510- fput(new->vm_file);
511+ vma_fput(new);
512 unlink_anon_vmas(new);
513 out_free_mpol:
514 mpol_put(vma_policy(new));
515@@ -2969,7 +2969,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
516 struct vm_area_struct *vma;
517 unsigned long populate = 0;
518 unsigned long ret = -EINVAL;
519- struct file *file;
520+ struct file *file, *prfile;
521
522 pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.rst.\n",
523 current->comm, current->pid);
524@@ -3044,10 +3044,27 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
525 }
526 }
527
528- file = get_file(vma->vm_file);
529+ vma_get_file(vma);
530+ file = vma->vm_file;
531+ prfile = vma->vm_prfile;
532 ret = do_mmap(vma->vm_file, start, size,
533 prot, flags, pgoff, &populate, NULL);
534+ if (!IS_ERR_VALUE(ret) && file && prfile) {
535+ struct vm_area_struct *new_vma;
536+
537+ new_vma = find_vma(mm, ret);
538+ if (!new_vma->vm_prfile)
539+ new_vma->vm_prfile = prfile;
540+ if (new_vma != vma)
541+ get_file(prfile);
542+ }
543+ /*
544+ * two fput()s instead of vma_fput(vma),
545+ * coz vma may not be available anymore.
546+ */
547 fput(file);
548+ if (prfile)
549+ fput(prfile);
550 out:
551 mmap_write_unlock(mm);
552 if (populate)
553@@ -3334,7 +3351,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
554 if (anon_vma_clone(new_vma, vma))
555 goto out_free_mempol;
556 if (new_vma->vm_file)
557- get_file(new_vma->vm_file);
558+ vma_get_file(new_vma);
559 if (new_vma->vm_ops && new_vma->vm_ops->open)
560 new_vma->vm_ops->open(new_vma);
561 vma_link(mm, new_vma, prev, rb_link, rb_parent);
562diff --git a/mm/nommu.c b/mm/nommu.c
563index 0faf39b32cdb9..78ecad7204c86 100644
564--- a/mm/nommu.c
565+++ b/mm/nommu.c
566@@ -533,7 +533,7 @@ static void __put_nommu_region(struct vm_region *region)
567 up_write(&nommu_region_sem);
568
569 if (region->vm_file)
570- fput(region->vm_file);
571+ vmr_fput(region);
572
573 /* IO memory and memory shared directly out of the pagecache
574 * from ramfs/tmpfs mustn't be released here */
575@@ -665,7 +665,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
576 if (vma->vm_ops && vma->vm_ops->close)
577 vma->vm_ops->close(vma);
578 if (vma->vm_file)
579- fput(vma->vm_file);
580+ vma_fput(vma);
581 put_nommu_region(vma->vm_region);
582 vm_area_free(vma);
583 }
584@@ -1188,7 +1188,7 @@ unsigned long do_mmap(struct file *file,
585 goto error_just_free;
586 }
587 }
588- fput(region->vm_file);
589+ vmr_fput(region);
590 kmem_cache_free(vm_region_jar, region);
591 region = pregion;
592 result = start;
593@@ -1265,10 +1265,10 @@ unsigned long do_mmap(struct file *file,
594 up_write(&nommu_region_sem);
595 error:
596 if (region->vm_file)
597- fput(region->vm_file);
598+ vmr_fput(region);
599 kmem_cache_free(vm_region_jar, region);
600 if (vma->vm_file)
601- fput(vma->vm_file);
602+ vma_fput(vma);
603 vm_area_free(vma);
604 return ret;
605
606diff --git a/mm/prfile.c b/mm/prfile.c
607new file mode 100644
608index 0000000000000..00d51187c3250
609--- /dev/null
610+++ b/mm/prfile.c
611@@ -0,0 +1,86 @@
612+// SPDX-License-Identifier: GPL-2.0
613+/*
614+ * Mainly for aufs which mmap(2) different file and wants to print different
615+ * path in /proc/PID/maps.
616+ * Call these functions via macros defined in linux/mm.h.
617+ *
618+ * See Documentation/filesystems/aufs/design/06mmap.txt
619+ *
620+ * Copyright (c) 2014-2020 Junjro R. Okajima
621+ * Copyright (c) 2014 Ian Campbell
622+ */
623+
624+#include <linux/mm.h>
625+#include <linux/file.h>
626+#include <linux/fs.h>
627+
628+/* #define PRFILE_TRACE */
629+static inline void prfile_trace(struct file *f, struct file *pr,
630+ const char func[], int line, const char func2[])
631+{
632+#ifdef PRFILE_TRACE
633+ if (pr)
634+ pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
635+#endif
636+}
637+
638+void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
639+ int line)
640+{
641+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
642+
643+ prfile_trace(f, pr, func, line, __func__);
644+ file_update_time(f);
645+ if (f && pr)
646+ file_update_time(pr);
647+}
648+
649+struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
650+ int line)
651+{
652+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
653+
654+ prfile_trace(f, pr, func, line, __func__);
655+ return (f && pr) ? pr : f;
656+}
657+
658+void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
659+{
660+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
661+
662+ prfile_trace(f, pr, func, line, __func__);
663+ get_file(f);
664+ if (f && pr)
665+ get_file(pr);
666+}
667+
668+void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
669+{
670+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
671+
672+ prfile_trace(f, pr, func, line, __func__);
673+ fput(f);
674+ if (f && pr)
675+ fput(pr);
676+}
677+
678+#ifndef CONFIG_MMU
679+struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
680+ int line)
681+{
682+ struct file *f = region->vm_file, *pr = region->vm_prfile;
683+
684+ prfile_trace(f, pr, func, line, __func__);
685+ return (f && pr) ? pr : f;
686+}
687+
688+void vmr_do_fput(struct vm_region *region, const char func[], int line)
689+{
690+ struct file *f = region->vm_file, *pr = region->vm_prfile;
691+
692+ prfile_trace(f, pr, func, line, __func__);
693+ fput(f);
694+ if (f && pr)
695+ fput(pr);
696+}
697+#endif /* !CONFIG_MMU */
698SPDX-License-Identifier: GPL-2.0
699aufs5.10 standalone patch
700
701diff --git a/fs/dcache.c b/fs/dcache.c
702index ddca6240e0db4..30dec552278dc 100644
703--- a/fs/dcache.c
704+++ b/fs/dcache.c
705@@ -1390,6 +1390,7 @@ void d_walk(struct dentry *parent, void *data,
706 seq = 1;
707 goto again;
708 }
709+EXPORT_SYMBOL_GPL(d_walk);
710
711 struct check_mount {
712 struct vfsmount *mnt;
713@@ -2935,6 +2936,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
714
715 write_sequnlock(&rename_lock);
716 }
717+EXPORT_SYMBOL_GPL(d_exchange);
718
719 /**
720 * d_ancestor - search for an ancestor
721diff --git a/fs/exec.c b/fs/exec.c
722index 547a2390baf54..18d51d0face68 100644
723--- a/fs/exec.c
724+++ b/fs/exec.c
725@@ -113,6 +113,7 @@ bool path_noexec(const struct path *path)
726 return (path->mnt->mnt_flags & MNT_NOEXEC) ||
727 (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
728 }
729+EXPORT_SYMBOL_GPL(path_noexec);
730
731 #ifdef CONFIG_USELIB
732 /*
733diff --git a/fs/fcntl.c b/fs/fcntl.c
734index 073ca6af500f3..b18afdf81e762 100644
735--- a/fs/fcntl.c
736+++ b/fs/fcntl.c
737@@ -85,6 +85,7 @@ int setfl(int fd, struct file *filp, unsigned long arg)
738 out:
739 return error;
740 }
741+EXPORT_SYMBOL_GPL(setfl);
742
743 static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
744 int force)
745diff --git a/fs/file_table.c b/fs/file_table.c
746index 709ada3151da5..27a3e3c9f2a82 100644
747--- a/fs/file_table.c
748+++ b/fs/file_table.c
749@@ -162,6 +162,7 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
750 }
751 return ERR_PTR(-ENFILE);
752 }
753+EXPORT_SYMBOL_GPL(alloc_empty_file);
754
755 /*
756 * Variant of alloc_empty_file() that doesn't check and modify nr_files.
757@@ -376,6 +377,7 @@ void __fput_sync(struct file *file)
758 }
759
760 EXPORT_SYMBOL(fput);
761+EXPORT_SYMBOL_GPL(__fput_sync);
762
763 void __init files_init(void)
764 {
765diff --git a/fs/inode.c b/fs/inode.c
766index e7f0c614a58f3..6fe0a92a4c93c 100644
767--- a/fs/inode.c
768+++ b/fs/inode.c
769@@ -1776,6 +1776,7 @@ int update_time(struct inode *inode, struct timespec64 *time, int flags)
770 return inode->i_op->update_time(inode, time, flags);
771 return generic_update_time(inode, time, flags);
772 }
773+EXPORT_SYMBOL_GPL(update_time);
774
775 /**
776 * touch_atime - update the access time
777diff --git a/fs/namespace.c b/fs/namespace.c
778index 451dee24a546a..38078cbede431 100644
779--- a/fs/namespace.c
780+++ b/fs/namespace.c
781@@ -431,6 +431,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
782 mnt_dec_writers(real_mount(mnt));
783 preempt_enable();
784 }
785+EXPORT_SYMBOL_GPL(__mnt_drop_write);
786
787 /**
788 * mnt_drop_write - give up write access to a mount
789@@ -797,6 +798,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
790 {
791 return check_mnt(real_mount(mnt));
792 }
793+EXPORT_SYMBOL_GPL(is_current_mnt_ns);
794
795 /*
796 * vfsmount lock must be held for write
797@@ -1961,6 +1963,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
798 }
799 return 0;
800 }
801+EXPORT_SYMBOL_GPL(iterate_mounts);
802
803 static void lock_mnt_tree(struct mount *mnt)
804 {
805diff --git a/fs/notify/group.c b/fs/notify/group.c
806index a4a4b1c64d32a..86dc2efb1850c 100644
807--- a/fs/notify/group.c
808+++ b/fs/notify/group.c
809@@ -100,6 +100,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
810 {
811 refcount_inc(&group->refcnt);
812 }
813+EXPORT_SYMBOL_GPL(fsnotify_get_group);
814
815 /*
816 * Drop a reference to a group. Free it if it's through.
817diff --git a/fs/open.c b/fs/open.c
818index 9af548fb841b0..2ff09b709f7bf 100644
819--- a/fs/open.c
820+++ b/fs/open.c
821@@ -65,6 +65,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
822 inode_unlock(dentry->d_inode);
823 return ret;
824 }
825+EXPORT_SYMBOL_GPL(do_truncate);
826
827 long vfs_truncate(const struct path *path, loff_t length)
828 {
829diff --git a/fs/read_write.c b/fs/read_write.c
830index 75f764b434184..7582bb3fb634b 100644
831--- a/fs/read_write.c
832+++ b/fs/read_write.c
833@@ -503,6 +503,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
834 inc_syscr(current);
835 return ret;
836 }
837+EXPORT_SYMBOL_GPL(vfs_read);
838
839 static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
840 {
841@@ -613,6 +614,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
842 file_end_write(file);
843 return ret;
844 }
845+EXPORT_SYMBOL_GPL(vfs_write);
846
847 /* file_ppos returns &file->f_pos or NULL if file is stream */
848 static inline loff_t *file_ppos(struct file *file)
849diff --git a/fs/splice.c b/fs/splice.c
850index 55b5356262085..c13ac0fbac318 100644
851--- a/fs/splice.c
852+++ b/fs/splice.c
853@@ -763,6 +763,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
854 return warn_unsupported(out, "write");
855 return out->f_op->splice_write(pipe, out, ppos, len, flags);
856 }
857+EXPORT_SYMBOL_GPL(do_splice_from);
858
859 /*
860 * Attempt to initiate a splice from a file to a pipe.
861@@ -787,6 +788,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
862 return warn_unsupported(in, "read");
863 return in->f_op->splice_read(in, ppos, pipe, len, flags);
864 }
865+EXPORT_SYMBOL_GPL(do_splice_to);
866
867 /**
868 * splice_direct_to_actor - splices data directly between two non-pipes
869diff --git a/fs/sync.c b/fs/sync.c
870index b7b5a0a0df6ff..fa5c7fba7f1ba 100644
871--- a/fs/sync.c
872+++ b/fs/sync.c
873@@ -39,6 +39,7 @@ int __sync_filesystem(struct super_block *sb, int wait)
874 sb->s_op->sync_fs(sb, wait);
875 return __sync_blockdev(sb->s_bdev, wait);
876 }
877+EXPORT_SYMBOL_GPL(__sync_filesystem);
878
879 /*
880 * Write out and wait upon all dirty data associated with this
881diff --git a/fs/xattr.c b/fs/xattr.c
882index cd7a563e8bcd4..7d989d57b0f0d 100644
883--- a/fs/xattr.c
884+++ b/fs/xattr.c
885@@ -360,6 +360,7 @@ vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
886 *xattr_value = value;
887 return error;
888 }
889+EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
890
891 ssize_t
892 __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
893diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
894index 7f006a47790cf..be002c3a30833 100644
895--- a/kernel/locking/lockdep.c
896+++ b/kernel/locking/lockdep.c
897@@ -209,6 +209,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
898 */
899 return lock_classes + class_idx;
900 }
901+EXPORT_SYMBOL_GPL(lockdep_hlock_class);
902 #define hlock_class(hlock) lockdep_hlock_class(hlock)
903
904 #ifdef CONFIG_LOCK_STAT
905diff --git a/kernel/task_work.c b/kernel/task_work.c
906index 8d6e1217c451c..0e73637adda86 100644
907--- a/kernel/task_work.c
908+++ b/kernel/task_work.c
909@@ -154,3 +154,4 @@ void task_work_run(void)
910 } while (work);
911 }
912 }
913+EXPORT_SYMBOL_GPL(task_work_run);
914diff --git a/security/security.c b/security/security.c
915index a28045dc9e7f6..310cf38efeec7 100644
916--- a/security/security.c
917+++ b/security/security.c
918@@ -1093,6 +1093,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
919 return 0;
920 return call_int_hook(path_rmdir, 0, dir, dentry);
921 }
922+EXPORT_SYMBOL_GPL(security_path_rmdir);
923
924 int security_path_unlink(const struct path *dir, struct dentry *dentry)
925 {
926@@ -1109,6 +1110,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
927 return 0;
928 return call_int_hook(path_symlink, 0, dir, dentry, old_name);
929 }
930+EXPORT_SYMBOL_GPL(security_path_symlink);
931
932 int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
933 struct dentry *new_dentry)
934@@ -1117,6 +1119,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
935 return 0;
936 return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
937 }
938+EXPORT_SYMBOL_GPL(security_path_link);
939
940 int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
941 const struct path *new_dir, struct dentry *new_dentry,
942@@ -1144,6 +1147,7 @@ int security_path_truncate(const struct path *path)
943 return 0;
944 return call_int_hook(path_truncate, 0, path);
945 }
946+EXPORT_SYMBOL_GPL(security_path_truncate);
947
948 int security_path_chmod(const struct path *path, umode_t mode)
949 {
950@@ -1151,6 +1155,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
951 return 0;
952 return call_int_hook(path_chmod, 0, path, mode);
953 }
954+EXPORT_SYMBOL_GPL(security_path_chmod);
955
956 int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
957 {
958@@ -1158,6 +1163,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
959 return 0;
960 return call_int_hook(path_chown, 0, path, uid, gid);
961 }
962+EXPORT_SYMBOL_GPL(security_path_chown);
963
964 int security_path_chroot(const struct path *path)
965 {
966@@ -1258,6 +1264,7 @@ int security_inode_permission(struct inode *inode, int mask)
967 return 0;
968 return call_int_hook(inode_permission, 0, inode, mask);
969 }
970+EXPORT_SYMBOL_GPL(security_inode_permission);
971
972 int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
973 {
974@@ -1450,6 +1457,7 @@ int security_file_permission(struct file *file, int mask)
975
976 return fsnotify_perm(file, mask);
977 }
978+EXPORT_SYMBOL_GPL(security_file_permission);
979
980 int security_file_alloc(struct file *file)
981 {
982diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
983--- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs 1970-01-01 01:00:00.000000000 +0100
984+++ linux/Documentation/ABI/testing/debugfs-aufs 2021-02-22 22:51:59.054745247 +0100
985@@ -0,0 +1,55 @@
986+What: /debug/aufs/si_<id>/
987+Date: March 2009
988+Contact: J. R. Okajima <hooanon05g@gmail.com>
989+Description:
990+ Under /debug/aufs, a directory named si_<id> is created
991+ per aufs mount, where <id> is a unique id generated
992+ internally.
993+
994+What: /debug/aufs/si_<id>/plink
995+Date: Apr 2013
996+Contact: J. R. Okajima <hooanon05g@gmail.com>
997+Description:
998+ It has three lines and shows the information about the
999+ pseudo-link. The first line is a single number
1000+ representing a number of buckets. The second line is a
1001+ number of pseudo-links per buckets (separated by a
1002+ blank). The last line is a single number representing a
1003+ total number of psedo-links.
1004+ When the aufs mount option 'noplink' is specified, it
1005+ will show "1\n0\n0\n".
1006+
1007+What: /debug/aufs/si_<id>/xib
1008+Date: March 2009
1009+Contact: J. R. Okajima <hooanon05g@gmail.com>
1010+Description:
1011+ It shows the consumed blocks by xib (External Inode Number
1012+ Bitmap), its block size and file size.
1013+ When the aufs mount option 'noxino' is specified, it
1014+ will be empty. About XINO files, see the aufs manual.
1015+
1016+What: /debug/aufs/si_<id>/xi0, xi1 ... xiN and xiN-N
1017+Date: March 2009
1018+Contact: J. R. Okajima <hooanon05g@gmail.com>
1019+Description:
1020+ It shows the consumed blocks by xino (External Inode Number
1021+ Translation Table), its link count, block size and file
1022+ size.
1023+ Due to the file size limit, there may exist multiple
1024+ xino files per branch. In this case, "-N" is added to
1025+ the filename and it corresponds to the index of the
1026+ internal xino array. "-0" is omitted.
1027+ When the aufs mount option 'noxino' is specified, Those
1028+ entries won't exist. About XINO files, see the aufs
1029+ manual.
1030+
1031+What: /debug/aufs/si_<id>/xigen
1032+Date: March 2009
1033+Contact: J. R. Okajima <hooanon05g@gmail.com>
1034+Description:
1035+ It shows the consumed blocks by xigen (External Inode
1036+ Generation Table), its block size and file size.
1037+ If CONFIG_AUFS_EXPORT is disabled, this entry will not
1038+ be created.
1039+ When the aufs mount option 'noxino' is specified, it
1040+ will be empty. About XINO files, see the aufs manual.
1041diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
1042--- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs 1970-01-01 01:00:00.000000000 +0100
1043+++ linux/Documentation/ABI/testing/sysfs-aufs 2021-02-22 22:51:59.054745247 +0100
1044@@ -0,0 +1,31 @@
1045+What: /sys/fs/aufs/si_<id>/
1046+Date: March 2009
1047+Contact: J. R. Okajima <hooanon05g@gmail.com>
1048+Description:
1049+ Under /sys/fs/aufs, a directory named si_<id> is created
1050+ per aufs mount, where <id> is a unique id generated
1051+ internally.
1052+
1053+What: /sys/fs/aufs/si_<id>/br0, br1 ... brN
1054+Date: March 2009
1055+Contact: J. R. Okajima <hooanon05g@gmail.com>
1056+Description:
1057+ It shows the abolute path of a member directory (which
1058+ is called branch) in aufs, and its permission.
1059+
1060+What: /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
1061+Date: July 2013
1062+Contact: J. R. Okajima <hooanon05g@gmail.com>
1063+Description:
1064+ It shows the id of a member directory (which is called
1065+ branch) in aufs.
1066+
1067+What: /sys/fs/aufs/si_<id>/xi_path
1068+Date: March 2009
1069+Contact: J. R. Okajima <hooanon05g@gmail.com>
1070+Description:
1071+ It shows the abolute path of XINO (External Inode Number
1072+ Bitmap, Translation Table and Generation Table) file
1073+ even if it is the default path.
1074+ When the aufs mount option 'noxino' is specified, it
1075+ will be empty. About XINO files, see the aufs manual.
1076diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1077--- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt 1970-01-01 01:00:00.000000000 +0100
1078+++ linux/Documentation/filesystems/aufs/design/01intro.txt 2021-02-22 22:51:59.054745247 +0100
1079@@ -0,0 +1,171 @@
1080+
1081+# Copyright (C) 2005-2020 Junjiro R. Okajima
1082+#
1083+# This program is free software; you can redistribute it and/or modify
1084+# it under the terms of the GNU General Public License as published by
1085+# the Free Software Foundation; either version 2 of the License, or
1086+# (at your option) any later version.
1087+#
1088+# This program is distributed in the hope that it will be useful,
1089+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1090+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1091+# GNU General Public License for more details.
1092+#
1093+# You should have received a copy of the GNU General Public License
1094+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1095+
1096+Introduction
1097+----------------------------------------
1098+
1099+aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
1100+1. abbrev. for "advanced multi-layered unification filesystem".
1101+2. abbrev. for "another unionfs".
1102+3. abbrev. for "auf das" in German which means "on the" in English.
1103+ Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1104+ But "Filesystem aufs Filesystem" is hard to understand.
1105+4. abbrev. for "African Urban Fashion Show".
1106+
1107+AUFS is a filesystem with features:
1108+- multi layered stackable unification filesystem, the member directory
1109+ is called as a branch.
1110+- branch permission and attribute, 'readonly', 'real-readonly',
1111+ 'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1112+ combination.
1113+- internal "file copy-on-write".
1114+- logical deletion, whiteout.
1115+- dynamic branch manipulation, adding, deleting and changing permission.
1116+- allow bypassing aufs, user's direct branch access.
1117+- external inode number translation table and bitmap which maintains the
1118+ persistent aufs inode number.
1119+- seekable directory, including NFS readdir.
1120+- file mapping, mmap and sharing pages.
1121+- pseudo-link, hardlink over branches.
1122+- loopback mounted filesystem as a branch.
1123+- several policies to select one among multiple writable branches.
1124+- revert a single systemcall when an error occurs in aufs.
1125+- and more...
1126+
1127+
1128+Multi Layered Stackable Unification Filesystem
1129+----------------------------------------------------------------------
1130+Most people already knows what it is.
1131+It is a filesystem which unifies several directories and provides a
1132+merged single directory. When users access a file, the access will be
1133+passed/re-directed/converted (sorry, I am not sure which English word is
1134+correct) to the real file on the member filesystem. The member
1135+filesystem is called 'lower filesystem' or 'branch' and has a mode
1136+'readonly' and 'readwrite.' And the deletion for a file on the lower
1137+readonly branch is handled by creating 'whiteout' on the upper writable
1138+branch.
1139+
1140+On LKML, there have been discussions about UnionMount (Jan Blunck,
1141+Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1142+different approaches to implement the merged-view.
1143+The former tries putting it into VFS, and the latter implements as a
1144+separate filesystem.
1145+(If I misunderstand about these implementations, please let me know and
1146+I shall correct it. Because it is a long time ago when I read their
1147+source files last time).
1148+
1149+UnionMount's approach will be able to small, but may be hard to share
1150+branches between several UnionMount since the whiteout in it is
1151+implemented in the inode on branch filesystem and always
1152+shared. According to Bharata's post, readdir does not seems to be
1153+finished yet.
1154+There are several missing features known in this implementations such as
1155+- for users, the inode number may change silently. eg. copy-up.
1156+- link(2) may break by copy-up.
1157+- read(2) may get an obsoleted filedata (fstat(2) too).
1158+- fcntl(F_SETLK) may be broken by copy-up.
1159+- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1160+ open(O_RDWR).
1161+
1162+In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1163+merged into mainline. This is another implementation of UnionMount as a
1164+separated filesystem. All the limitations and known problems which
1165+UnionMount are equally inherited to "overlay" filesystem.
1166+
1167+Unionfs has a longer history. When I started implementing a stackable
1168+filesystem (Aug 2005), it already existed. It has virtual super_block,
1169+inode, dentry and file objects and they have an array pointing lower
1170+same kind objects. After contributing many patches for Unionfs, I
1171+re-started my project AUFS (Jun 2006).
1172+
1173+In AUFS, the structure of filesystem resembles to Unionfs, but I
1174+implemented my own ideas, approaches and enhancements and it became
1175+totally different one.
1176+
1177+Comparing DM snapshot and fs based implementation
1178+- the number of bytes to be copied between devices is much smaller.
1179+- the type of filesystem must be one and only.
1180+- the fs must be writable, no readonly fs, even for the lower original
1181+ device. so the compression fs will not be usable. but if we use
1182+ loopback mount, we may address this issue.
1183+ for instance,
1184+ mount /cdrom/squashfs.img /sq
1185+ losetup /sq/ext2.img
1186+ losetup /somewhere/cow
1187+ dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1188+- it will be difficult (or needs more operations) to extract the
1189+ difference between the original device and COW.
1190+- DM snapshot-merge may help a lot when users try merging. in the
1191+ fs-layer union, users will use rsync(1).
1192+
1193+You may want to read my old paper "Filesystems in LiveCD"
1194+(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1195+
1196+
1197+Several characters/aspects/persona of aufs
1198+----------------------------------------------------------------------
1199+
1200+Aufs has several characters, aspects or persona.
1201+1. a filesystem, callee of VFS helper
1202+2. sub-VFS, caller of VFS helper for branches
1203+3. a virtual filesystem which maintains persistent inode number
1204+4. reader/writer of files on branches such like an application
1205+
1206+1. Callee of VFS Helper
1207+As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1208+unlink(2) from an application reaches sys_unlink() kernel function and
1209+then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1210+calls filesystem specific unlink operation. Actually aufs implements the
1211+unlink operation but it behaves like a redirector.
1212+
1213+2. Caller of VFS Helper for Branches
1214+aufs_unlink() passes the unlink request to the branch filesystem as if
1215+it were called from VFS. So the called unlink operation of the branch
1216+filesystem acts as usual. As a caller of VFS helper, aufs should handle
1217+every necessary pre/post operation for the branch filesystem.
1218+- acquire the lock for the parent dir on a branch
1219+- lookup in a branch
1220+- revalidate dentry on a branch
1221+- mnt_want_write() for a branch
1222+- vfs_unlink() for a branch
1223+- mnt_drop_write() for a branch
1224+- release the lock on a branch
1225+
1226+3. Persistent Inode Number
1227+One of the most important issue for a filesystem is to maintain inode
1228+numbers. This is particularly important to support exporting a
1229+filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1230+backend block device for its own. But some storage is necessary to
1231+keep and maintain the inode numbers. It may be a large space and may not
1232+suit to keep in memory. Aufs rents some space from its first writable
1233+branch filesystem (by default) and creates file(s) on it. These files
1234+are created by aufs internally and removed soon (currently) keeping
1235+opened.
1236+Note: Because these files are removed, they are totally gone after
1237+ unmounting aufs. It means the inode numbers are not persistent
1238+ across unmount or reboot. I have a plan to make them really
1239+ persistent which will be important for aufs on NFS server.
1240+
1241+4. Read/Write Files Internally (copy-on-write)
1242+Because a branch can be readonly, when you write a file on it, aufs will
1243+"copy-up" it to the upper writable branch internally. And then write the
1244+originally requested thing to the file. Generally kernel doesn't
1245+open/read/write file actively. In aufs, even a single write may cause a
1246+internal "file copy". This behaviour is very similar to cp(1) command.
1247+
1248+Some people may think it is better to pass such work to user space
1249+helper, instead of doing in kernel space. Actually I am still thinking
1250+about it. But currently I have implemented it in kernel space.
1251diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1252--- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1253+++ linux/Documentation/filesystems/aufs/design/02struct.txt 2021-02-22 22:51:59.058078584 +0100
1254@@ -0,0 +1,258 @@
1255+
1256+# Copyright (C) 2005-2020 Junjiro R. Okajima
1257+#
1258+# This program is free software; you can redistribute it and/or modify
1259+# it under the terms of the GNU General Public License as published by
1260+# the Free Software Foundation; either version 2 of the License, or
1261+# (at your option) any later version.
1262+#
1263+# This program is distributed in the hope that it will be useful,
1264+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1265+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1266+# GNU General Public License for more details.
1267+#
1268+# You should have received a copy of the GNU General Public License
1269+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1270+
1271+Basic Aufs Internal Structure
1272+
1273+Superblock/Inode/Dentry/File Objects
1274+----------------------------------------------------------------------
1275+As like an ordinary filesystem, aufs has its own
1276+superblock/inode/dentry/file objects. All these objects have a
1277+dynamically allocated array and store the same kind of pointers to the
1278+lower filesystem, branch.
1279+For example, when you build a union with one readwrite branch and one
1280+readonly, mounted /au, /rw and /ro respectively.
1281+- /au = /rw + /ro
1282+- /ro/fileA exists but /rw/fileA
1283+
1284+Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1285+pointers are stored in a aufs dentry. The array in aufs dentry will be,
1286+- [0] = NULL (because /rw/fileA doesn't exist)
1287+- [1] = /ro/fileA
1288+
1289+This style of an array is essentially same to the aufs
1290+superblock/inode/dentry/file objects.
1291+
1292+Because aufs supports manipulating branches, ie. add/delete/change
1293+branches dynamically, these objects has its own generation. When
1294+branches are changed, the generation in aufs superblock is
1295+incremented. And a generation in other object are compared when it is
1296+accessed. When a generation in other objects are obsoleted, aufs
1297+refreshes the internal array.
1298+
1299+
1300+Superblock
1301+----------------------------------------------------------------------
1302+Additionally aufs superblock has some data for policies to select one
1303+among multiple writable branches, XIB files, pseudo-links and kobject.
1304+See below in detail.
1305+About the policies which supports copy-down a directory, see
1306+wbr_policy.txt too.
1307+
1308+
1309+Branch and XINO(External Inode Number Translation Table)
1310+----------------------------------------------------------------------
1311+Every branch has its own xino (external inode number translation table)
1312+file. The xino file is created and unlinked by aufs internally. When two
1313+members of a union exist on the same filesystem, they share the single
1314+xino file.
1315+The struct of a xino file is simple, just a sequence of aufs inode
1316+numbers which is indexed by the lower inode number.
1317+In the above sample, assume the inode number of /ro/fileA is i111 and
1318+aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1319+4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1320+
1321+When the inode numbers are not contiguous, the xino file will be sparse
1322+which has a hole in it and doesn't consume as much disk space as it
1323+might appear. If your branch filesystem consumes disk space for such
1324+holes, then you should specify 'xino=' option at mounting aufs.
1325+
1326+Aufs has a mount option to free the disk blocks for such holes in XINO
1327+files on tmpfs or ramdisk. But it is not so effective actually. If you
1328+meet a problem of disk shortage due to XINO files, then you should try
1329+"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1330+The patch localizes the assignment inumbers per tmpfs-mount and avoid
1331+the holes in XINO files.
1332+
1333+Also a writable branch has three kinds of "whiteout bases". All these
1334+are existed when the branch is joined to aufs, and their names are
1335+whiteout-ed doubly, so that users will never see their names in aufs
1336+hierarchy.
1337+1. a regular file which will be hardlinked to all whiteouts.
1338+2. a directory to store a pseudo-link.
1339+3. a directory to store an "orphan"-ed file temporary.
1340+
1341+1. Whiteout Base
1342+ When you remove a file on a readonly branch, aufs handles it as a
1343+ logical deletion and creates a whiteout on the upper writable branch
1344+ as a hardlink of this file in order not to consume inode on the
1345+ writable branch.
1346+2. Pseudo-link Dir
1347+ See below, Pseudo-link.
1348+3. Step-Parent Dir
1349+ When "fileC" exists on the lower readonly branch only and it is
1350+ opened and removed with its parent dir, and then user writes
1351+ something into it, then aufs copies-up fileC to this
1352+ directory. Because there is no other dir to store fileC. After
1353+ creating a file under this dir, the file is unlinked.
1354+
1355+Because aufs supports manipulating branches, ie. add/delete/change
1356+dynamically, a branch has its own id. When the branch order changes,
1357+aufs finds the new index by searching the branch id.
1358+
1359+
1360+Pseudo-link
1361+----------------------------------------------------------------------
1362+Assume "fileA" exists on the lower readonly branch only and it is
1363+hardlinked to "fileB" on the branch. When you write something to fileA,
1364+aufs copies-up it to the upper writable branch. Additionally aufs
1365+creates a hardlink under the Pseudo-link Directory of the writable
1366+branch. The inode of a pseudo-link is kept in aufs super_block as a
1367+simple list. If fileB is read after unlinking fileA, aufs returns
1368+filedata from the pseudo-link instead of the lower readonly
1369+branch. Because the pseudo-link is based upon the inode, to keep the
1370+inode number by xino (see above) is essentially necessary.
1371+
1372+All the hardlinks under the Pseudo-link Directory of the writable branch
1373+should be restored in a proper location later. Aufs provides a utility
1374+to do this. The userspace helpers executed at remounting and unmounting
1375+aufs by default.
1376+During this utility is running, it puts aufs into the pseudo-link
1377+maintenance mode. In this mode, only the process which began the
1378+maintenance mode (and its child processes) is allowed to operate in
1379+aufs. Some other processes which are not related to the pseudo-link will
1380+be allowed to run too, but the rest have to return an error or wait
1381+until the maintenance mode ends. If a process already acquires an inode
1382+mutex (in VFS), it has to return an error.
1383+
1384+
1385+XIB(external inode number bitmap)
1386+----------------------------------------------------------------------
1387+Addition to the xino file per a branch, aufs has an external inode number
1388+bitmap in a superblock object. It is also an internal file such like a
1389+xino file.
1390+It is a simple bitmap to mark whether the aufs inode number is in-use or
1391+not.
1392+To reduce the file I/O, aufs prepares a single memory page to cache xib.
1393+
1394+As well as XINO files, aufs has a feature to truncate/refresh XIB to
1395+reduce the number of consumed disk blocks for these files.
1396+
1397+
1398+Virtual or Vertical Dir, and Readdir in Userspace
1399+----------------------------------------------------------------------
1400+In order to support multiple layers (branches), aufs readdir operation
1401+constructs a virtual dir block on memory. For readdir, aufs calls
1402+vfs_readdir() internally for each dir on branches, merges their entries
1403+with eliminating the whiteout-ed ones, and sets it to file (dir)
1404+object. So the file object has its entry list until it is closed. The
1405+entry list will be updated when the file position is zero and becomes
1406+obsoleted. This decision is made in aufs automatically.
1407+
1408+The dynamically allocated memory block for the name of entries has a
1409+unit of 512 bytes (by default) and stores the names contiguously (no
1410+padding). Another block for each entry is handled by kmem_cache too.
1411+During building dir blocks, aufs creates hash list and judging whether
1412+the entry is whiteouted by its upper branch or already listed.
1413+The merged result is cached in the corresponding inode object and
1414+maintained by a customizable life-time option.
1415+
1416+Some people may call it can be a security hole or invite DoS attack
1417+since the opened and once readdir-ed dir (file object) holds its entry
1418+list and becomes a pressure for system memory. But I'd say it is similar
1419+to files under /proc or /sys. The virtual files in them also holds a
1420+memory page (generally) while they are opened. When an idea to reduce
1421+memory for them is introduced, it will be applied to aufs too.
1422+For those who really hate this situation, I've developed readdir(3)
1423+library which operates this merging in userspace. You just need to set
1424+LD_PRELOAD environment variable, and aufs will not consume no memory in
1425+kernel space for readdir(3).
1426+
1427+
1428+Workqueue
1429+----------------------------------------------------------------------
1430+Aufs sometimes requires privilege access to a branch. For instance,
1431+in copy-up/down operation. When a user process is going to make changes
1432+to a file which exists in the lower readonly branch only, and the mode
1433+of one of ancestor directories may not be writable by a user
1434+process. Here aufs copy-up the file with its ancestors and they may
1435+require privilege to set its owner/group/mode/etc.
1436+This is a typical case of a application character of aufs (see
1437+Introduction).
1438+
1439+Aufs uses workqueue synchronously for this case. It creates its own
1440+workqueue. The workqueue is a kernel thread and has privilege. Aufs
1441+passes the request to call mkdir or write (for example), and wait for
1442+its completion. This approach solves a problem of a signal handler
1443+simply.
1444+If aufs didn't adopt the workqueue and changed the privilege of the
1445+process, then the process may receive the unexpected SIGXFSZ or other
1446+signals.
1447+
1448+Also aufs uses the system global workqueue ("events" kernel thread) too
1449+for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1450+whiteout base and etc. This is unrelated to a privilege.
1451+Most of aufs operation tries acquiring a rw_semaphore for aufs
1452+superblock at the beginning, at the same time waits for the completion
1453+of all queued asynchronous tasks.
1454+
1455+
1456+Whiteout
1457+----------------------------------------------------------------------
1458+The whiteout in aufs is very similar to Unionfs's. That is represented
1459+by its filename. UnionMount takes an approach of a file mode, but I am
1460+afraid several utilities (find(1) or something) will have to support it.
1461+
1462+Basically the whiteout represents "logical deletion" which stops aufs to
1463+lookup further, but also it represents "dir is opaque" which also stop
1464+further lookup.
1465+
1466+In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1467+In order to make several functions in a single systemcall to be
1468+revertible, aufs adopts an approach to rename a directory to a temporary
1469+unique whiteouted name.
1470+For example, in rename(2) dir where the target dir already existed, aufs
1471+renames the target dir to a temporary unique whiteouted name before the
1472+actual rename on a branch, and then handles other actions (make it opaque,
1473+update the attributes, etc). If an error happens in these actions, aufs
1474+simply renames the whiteouted name back and returns an error. If all are
1475+succeeded, aufs registers a function to remove the whiteouted unique
1476+temporary name completely and asynchronously to the system global
1477+workqueue.
1478+
1479+
1480+Copy-up
1481+----------------------------------------------------------------------
1482+It is a well-known feature or concept.
1483+When user modifies a file on a readonly branch, aufs operate "copy-up"
1484+internally and makes change to the new file on the upper writable branch.
1485+When the trigger systemcall does not update the timestamps of the parent
1486+dir, aufs reverts it after copy-up.
1487+
1488+
1489+Move-down (aufs3.9 and later)
1490+----------------------------------------------------------------------
1491+"Copy-up" is one of the essential feature in aufs. It copies a file from
1492+the lower readonly branch to the upper writable branch when a user
1493+changes something about the file.
1494+"Move-down" is an opposite action of copy-up. Basically this action is
1495+ran manually instead of automatically and internally.
1496+For desgin and implementation, aufs has to consider these issues.
1497+- whiteout for the file may exist on the lower branch.
1498+- ancestor directories may not exist on the lower branch.
1499+- diropq for the ancestor directories may exist on the upper branch.
1500+- free space on the lower branch will reduce.
1501+- another access to the file may happen during moving-down, including
1502+ UDBA (see "Revalidate Dentry and UDBA").
1503+- the file should not be hard-linked nor pseudo-linked. they should be
1504+ handled by auplink utility later.
1505+
1506+Sometimes users want to move-down a file from the upper writable branch
1507+to the lower readonly or writable branch. For instance,
1508+- the free space of the upper writable branch is going to run out.
1509+- create a new intermediate branch between the upper and lower branch.
1510+- etc.
1511+
1512+For this purpose, use "aumvdown" command in aufs-util.git.
1513diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1514--- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt 1970-01-01 01:00:00.000000000 +0100
1515+++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt 2021-02-22 22:51:59.058078584 +0100
1516@@ -0,0 +1,85 @@
1517+
1518+# Copyright (C) 2015-2020 Junjiro R. Okajima
1519+#
1520+# This program is free software; you can redistribute it and/or modify
1521+# it under the terms of the GNU General Public License as published by
1522+# the Free Software Foundation; either version 2 of the License, or
1523+# (at your option) any later version.
1524+#
1525+# This program is distributed in the hope that it will be useful,
1526+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1527+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1528+# GNU General Public License for more details.
1529+#
1530+# You should have received a copy of the GNU General Public License
1531+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1532+
1533+Support for a branch who has its ->atomic_open()
1534+----------------------------------------------------------------------
1535+The filesystems who implement its ->atomic_open() are not majority. For
1536+example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1537+particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1538+->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1539+sure whether all filesystems who have ->atomic_open() behave like this,
1540+but NFSv4 surely returns the error.
1541+
1542+In order to support ->atomic_open() for aufs, there are a few
1543+approaches.
1544+
1545+A. Introduce aufs_atomic_open()
1546+ - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1547+ branch fs.
1548+B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1549+ an aufs user Pip Cet's approach
1550+ - calls aufs_create(), VFS finish_open() and notify_change().
1551+ - pass fake-mode to finish_open(), and then correct the mode by
1552+ notify_change().
1553+C. Extend aufs_open() to call branch fs's ->atomic_open()
1554+ - no aufs_atomic_open().
1555+ - aufs_lookup() registers the TID to an aufs internal object.
1556+ - aufs_create() does nothing when the matching TID is registered, but
1557+ registers the mode.
1558+ - aufs_open() calls branch fs's ->atomic_open() when the matching
1559+ TID is registered.
1560+D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1561+ credential
1562+ - no aufs_atomic_open().
1563+ - aufs_create() registers the TID to an internal object. this info
1564+ represents "this process created this file just now."
1565+ - when aufs gets EACCES from branch fs's ->open(), then confirm the
1566+ registered TID and re-try open() with superuser's credential.
1567+
1568+Pros and cons for each approach.
1569+
1570+A.
1571+ - straightforward but highly depends upon VFS internal.
1572+ - the atomic behavaiour is kept.
1573+ - some of parameters such as nameidata are hard to reproduce for
1574+ branch fs.
1575+ - large overhead.
1576+B.
1577+ - easy to implement.
1578+ - the atomic behavaiour is lost.
1579+C.
1580+ - the atomic behavaiour is kept.
1581+ - dirty and tricky.
1582+ - VFS checks whether the file is created correctly after calling
1583+ ->create(), which means this approach doesn't work.
1584+D.
1585+ - easy to implement.
1586+ - the atomic behavaiour is lost.
1587+ - to open a file with superuser's credential and give it to a user
1588+ process is a bad idea, since the file object keeps the credential
1589+ in it. It may affect LSM or something. This approach doesn't work
1590+ either.
1591+
1592+The approach A is ideal, but it hard to implement. So here is a
1593+variation of A, which is to be implemented.
1594+
1595+A-1. Introduce aufs_atomic_open()
1596+ - calls branch fs ->atomic_open() if exists. otherwise calls
1597+ vfs_create() and finish_open().
1598+ - the demerit is that the several checks after branch fs
1599+ ->atomic_open() are lost. in the ordinary case, the checks are
1600+ done by VFS:do_last(), lookup_open() and atomic_open(). some can
1601+ be implemented in aufs, but not all I am afraid.
1602diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1603--- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1604+++ linux/Documentation/filesystems/aufs/design/03lookup.txt 2021-02-22 22:51:59.058078584 +0100
1605@@ -0,0 +1,113 @@
1606+
1607+# Copyright (C) 2005-2020 Junjiro R. Okajima
1608+#
1609+# This program is free software; you can redistribute it and/or modify
1610+# it under the terms of the GNU General Public License as published by
1611+# the Free Software Foundation; either version 2 of the License, or
1612+# (at your option) any later version.
1613+#
1614+# This program is distributed in the hope that it will be useful,
1615+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1616+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1617+# GNU General Public License for more details.
1618+#
1619+# You should have received a copy of the GNU General Public License
1620+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1621+
1622+Lookup in a Branch
1623+----------------------------------------------------------------------
1624+Since aufs has a character of sub-VFS (see Introduction), it operates
1625+lookup for branches as VFS does. It may be a heavy work. But almost all
1626+lookup operation in aufs is the simplest case, ie. lookup only an entry
1627+directly connected to its parent. Digging down the directory hierarchy
1628+is unnecessary. VFS has a function lookup_one_len() for that use, and
1629+aufs calls it.
1630+
1631+When a branch is a remote filesystem, aufs basically relies upon its
1632+->d_revalidate(), also aufs forces the hardest revalidate tests for
1633+them.
1634+For d_revalidate, aufs implements three levels of revalidate tests. See
1635+"Revalidate Dentry and UDBA" in detail.
1636+
1637+
1638+Test Only the Highest One for the Directory Permission (dirperm1 option)
1639+----------------------------------------------------------------------
1640+Let's try case study.
1641+- aufs has two branches, upper readwrite and lower readonly.
1642+ /au = /rw + /ro
1643+- "dirA" exists under /ro, but /rw. and its mode is 0700.
1644+- user invoked "chmod a+rx /au/dirA"
1645+- the internal copy-up is activated and "/rw/dirA" is created and its
1646+ permission bits are set to world readable.
1647+- then "/au/dirA" becomes world readable?
1648+
1649+In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1650+or it may be a natively readonly filesystem. If aufs respects the lower
1651+branch, it should not respond readdir request from other users. But user
1652+allowed it by chmod. Should really aufs rejects showing the entries
1653+under /ro/dirA?
1654+
1655+To be honest, I don't have a good solution for this case. So aufs
1656+implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1657+users.
1658+When dirperm1 is specified, aufs checks only the highest one for the
1659+directory permission, and shows the entries. Otherwise, as usual, checks
1660+every dir existing on all branches and rejects the request.
1661+
1662+As a side effect, dirperm1 option improves the performance of aufs
1663+because the number of permission check is reduced when the number of
1664+branch is many.
1665+
1666+
1667+Revalidate Dentry and UDBA (User's Direct Branch Access)
1668+----------------------------------------------------------------------
1669+Generally VFS helpers re-validate a dentry as a part of lookup.
1670+0. digging down the directory hierarchy.
1671+1. lock the parent dir by its i_mutex.
1672+2. lookup the final (child) entry.
1673+3. revalidate it.
1674+4. call the actual operation (create, unlink, etc.)
1675+5. unlock the parent dir
1676+
1677+If the filesystem implements its ->d_revalidate() (step 3), then it is
1678+called. Actually aufs implements it and checks the dentry on a branch is
1679+still valid.
1680+But it is not enough. Because aufs has to release the lock for the
1681+parent dir on a branch at the end of ->lookup() (step 2) and
1682+->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1683+held by VFS.
1684+If the file on a branch is changed directly, eg. bypassing aufs, after
1685+aufs released the lock, then the subsequent operation may cause
1686+something unpleasant result.
1687+
1688+This situation is a result of VFS architecture, ->lookup() and
1689+->d_revalidate() is separated. But I never say it is wrong. It is a good
1690+design from VFS's point of view. It is just not suitable for sub-VFS
1691+character in aufs.
1692+
1693+Aufs supports such case by three level of revalidation which is
1694+selectable by user.
1695+1. Simple Revalidate
1696+ Addition to the native flow in VFS's, confirm the child-parent
1697+ relationship on the branch just after locking the parent dir on the
1698+ branch in the "actual operation" (step 4). When this validation
1699+ fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1700+ checks the validation of the dentry on branches.
1701+2. Monitor Changes Internally by Inotify/Fsnotify
1702+ Addition to above, in the "actual operation" (step 4) aufs re-lookup
1703+ the dentry on the branch, and returns EBUSY if it finds different
1704+ dentry.
1705+ Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1706+ during it is in cache. When the event is notified, aufs registers a
1707+ function to kernel 'events' thread by schedule_work(). And the
1708+ function sets some special status to the cached aufs dentry and inode
1709+ private data. If they are not cached, then aufs has nothing to
1710+ do. When the same file is accessed through aufs (step 0-3) later,
1711+ aufs will detect the status and refresh all necessary data.
1712+ In this mode, aufs has to ignore the event which is fired by aufs
1713+ itself.
1714+3. No Extra Validation
1715+ This is the simplest test and doesn't add any additional revalidation
1716+ test, and skip the revalidation in step 4. It is useful and improves
1717+ aufs performance when system surely hide the aufs branches from user,
1718+ by over-mounting something (or another method).
1719diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1720--- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1721+++ linux/Documentation/filesystems/aufs/design/04branch.txt 2021-02-22 22:51:59.058078584 +0100
1722@@ -0,0 +1,74 @@
1723+
1724+# Copyright (C) 2005-2020 Junjiro R. Okajima
1725+#
1726+# This program is free software; you can redistribute it and/or modify
1727+# it under the terms of the GNU General Public License as published by
1728+# the Free Software Foundation; either version 2 of the License, or
1729+# (at your option) any later version.
1730+#
1731+# This program is distributed in the hope that it will be useful,
1732+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1733+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1734+# GNU General Public License for more details.
1735+#
1736+# You should have received a copy of the GNU General Public License
1737+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1738+
1739+Branch Manipulation
1740+
1741+Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1742+and changing its permission/attribute, there are a lot of works to do.
1743+
1744+
1745+Add a Branch
1746+----------------------------------------------------------------------
1747+o Confirm the adding dir exists outside of aufs, including loopback
1748+ mount, and its various attributes.
1749+o Initialize the xino file and whiteout bases if necessary.
1750+ See struct.txt.
1751+
1752+o Check the owner/group/mode of the directory
1753+ When the owner/group/mode of the adding directory differs from the
1754+ existing branch, aufs issues a warning because it may impose a
1755+ security risk.
1756+ For example, when a upper writable branch has a world writable empty
1757+ top directory, a malicious user can create any files on the writable
1758+ branch directly, like copy-up and modify manually. If something like
1759+ /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1760+ writable branch, and the writable branch is world-writable, then a
1761+ malicious guy may create /etc/passwd on the writable branch directly
1762+ and the infected file will be valid in aufs.
1763+ I am afraid it can be a security issue, but aufs can do nothing except
1764+ producing a warning.
1765+
1766+
1767+Delete a Branch
1768+----------------------------------------------------------------------
1769+o Confirm the deleting branch is not busy
1770+ To be general, there is one merit to adopt "remount" interface to
1771+ manipulate branches. It is to discard caches. At deleting a branch,
1772+ aufs checks the still cached (and connected) dentries and inodes. If
1773+ there are any, then they are all in-use. An inode without its
1774+ corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1775+
1776+ For the cached one, aufs checks whether the same named entry exists on
1777+ other branches.
1778+ If the cached one is a directory, because aufs provides a merged view
1779+ to users, as long as one dir is left on any branch aufs can show the
1780+ dir to users. In this case, the branch can be removed from aufs.
1781+ Otherwise aufs rejects deleting the branch.
1782+
1783+ If any file on the deleting branch is opened by aufs, then aufs
1784+ rejects deleting.
1785+
1786+
1787+Modify the Permission of a Branch
1788+----------------------------------------------------------------------
1789+o Re-initialize or remove the xino file and whiteout bases if necessary.
1790+ See struct.txt.
1791+
1792+o rw --> ro: Confirm the modifying branch is not busy
1793+ Aufs rejects the request if any of these conditions are true.
1794+ - a file on the branch is mmap-ed.
1795+ - a regular file on the branch is opened for write and there is no
1796+ same named entry on the upper branch.
1797diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1798--- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt 1970-01-01 01:00:00.000000000 +0100
1799+++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt 2021-02-22 22:51:59.058078584 +0100
1800@@ -0,0 +1,64 @@
1801+
1802+# Copyright (C) 2005-2020 Junjiro R. Okajima
1803+#
1804+# This program is free software; you can redistribute it and/or modify
1805+# it under the terms of the GNU General Public License as published by
1806+# the Free Software Foundation; either version 2 of the License, or
1807+# (at your option) any later version.
1808+#
1809+# This program is distributed in the hope that it will be useful,
1810+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1811+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1812+# GNU General Public License for more details.
1813+#
1814+# You should have received a copy of the GNU General Public License
1815+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1816+
1817+Policies to Select One among Multiple Writable Branches
1818+----------------------------------------------------------------------
1819+When the number of writable branch is more than one, aufs has to decide
1820+the target branch for file creation or copy-up. By default, the highest
1821+writable branch which has the parent (or ancestor) dir of the target
1822+file is chosen (top-down-parent policy).
1823+By user's request, aufs implements some other policies to select the
1824+writable branch, for file creation several policies, round-robin,
1825+most-free-space, and other policies. For copy-up, top-down-parent,
1826+bottom-up-parent, bottom-up and others.
1827+
1828+As expected, the round-robin policy selects the branch in circular. When
1829+you have two writable branches and creates 10 new files, 5 files will be
1830+created for each branch. mkdir(2) systemcall is an exception. When you
1831+create 10 new directories, all will be created on the same branch.
1832+And the most-free-space policy selects the one which has most free
1833+space among the writable branches. The amount of free space will be
1834+checked by aufs internally, and users can specify its time interval.
1835+
1836+The policies for copy-up is more simple,
1837+top-down-parent is equivalent to the same named on in create policy,
1838+bottom-up-parent selects the writable branch where the parent dir
1839+exists and the nearest upper one from the copyup-source,
1840+bottom-up selects the nearest upper writable branch from the
1841+copyup-source, regardless the existence of the parent dir.
1842+
1843+There are some rules or exceptions to apply these policies.
1844+- If there is a readonly branch above the policy-selected branch and
1845+ the parent dir is marked as opaque (a variation of whiteout), or the
1846+ target (creating) file is whiteout-ed on the upper readonly branch,
1847+ then the result of the policy is ignored and the target file will be
1848+ created on the nearest upper writable branch than the readonly branch.
1849+- If there is a writable branch above the policy-selected branch and
1850+ the parent dir is marked as opaque or the target file is whiteouted
1851+ on the branch, then the result of the policy is ignored and the target
1852+ file will be created on the highest one among the upper writable
1853+ branches who has diropq or whiteout. In case of whiteout, aufs removes
1854+ it as usual.
1855+- link(2) and rename(2) systemcalls are exceptions in every policy.
1856+ They try selecting the branch where the source exists as possible
1857+ since copyup a large file will take long time. If it can't be,
1858+ ie. the branch where the source exists is readonly, then they will
1859+ follow the copyup policy.
1860+- There is an exception for rename(2) when the target exists.
1861+ If the rename target exists, aufs compares the index of the branches
1862+ where the source and the target exists and selects the higher
1863+ one. If the selected branch is readonly, then aufs follows the
1864+ copyup policy.
1865diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
1866--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
1867+++ linux/Documentation/filesystems/aufs/design/06dirren.dot 2021-02-22 22:51:59.058078584 +0100
1868@@ -0,0 +1,31 @@
1869+
1870+// to view this graph, run dot(1) command in GRAPHVIZ.
1871+
1872+digraph G {
1873+node [shape=box];
1874+whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
1875+
1876+node [shape=oval];
1877+
1878+aufs_rename -> whinfo [label="store/remove"];
1879+
1880+node [shape=oval];
1881+inode_list [label="h_inum list in branch\ncache"];
1882+
1883+node [shape=box];
1884+whinode [label="h_inum list file"];
1885+
1886+node [shape=oval];
1887+brmgmt [label="br_add/del/mod/umount"];
1888+
1889+brmgmt -> inode_list [label="create/remove"];
1890+brmgmt -> whinode [label="load/store"];
1891+
1892+inode_list -> whinode [style=dashed,dir=both];
1893+
1894+aufs_rename -> inode_list [label="add/del"];
1895+
1896+aufs_lookup -> inode_list [label="search"];
1897+
1898+aufs_lookup -> whinfo [label="load/remove"];
1899+}
1900diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
1901--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
1902+++ linux/Documentation/filesystems/aufs/design/06dirren.txt 2021-02-22 22:51:59.058078584 +0100
1903@@ -0,0 +1,102 @@
1904+
1905+# Copyright (C) 2017-2020 Junjiro R. Okajima
1906+#
1907+# This program is free software; you can redistribute it and/or modify
1908+# it under the terms of the GNU General Public License as published by
1909+# the Free Software Foundation; either version 2 of the License, or
1910+# (at your option) any later version.
1911+#
1912+# This program is distributed in the hope that it will be useful,
1913+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1914+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1915+# GNU General Public License for more details.
1916+#
1917+# You should have received a copy of the GNU General Public License
1918+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1919+
1920+Special handling for renaming a directory (DIRREN)
1921+----------------------------------------------------------------------
1922+First, let's assume we have a simple usecase.
1923+
1924+- /u = /rw + /ro
1925+- /rw/dirA exists
1926+- /ro/dirA and /ro/dirA/file exist too
1927+- there is no dirB on both branches
1928+- a user issues rename("dirA", "dirB")
1929+
1930+Now, what should aufs behave against this rename(2)?
1931+There are a few possible cases.
1932+
1933+A. returns EROFS.
1934+ since dirA exists on a readonly branch which cannot be renamed.
1935+B. returns EXDEV.
1936+ it is possible to copy-up dirA (only the dir itself), but the child
1937+ entries ("file" in this case) should not be. it must be a bad
1938+ approach to copy-up recursively.
1939+C. returns a success.
1940+ even the branch /ro is readonly, aufs tries renaming it. Obviously it
1941+ is a violation of aufs' policy.
1942+D. construct an extra information which indicates that /ro/dirA should
1943+ be handled as the name of dirB.
1944+ overlayfs has a similar feature called REDIRECT.
1945+
1946+Until now, aufs implements the case B only which returns EXDEV, and
1947+expects the userspace application behaves like mv(1) which tries
1948+issueing rename(2) recursively.
1949+
1950+A new aufs feature called DIRREN is introduced which implements the case
1951+D. There are several "extra information" added.
1952+
1953+1. detailed info per renamed directory
1954+ path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
1955+2. the inode-number list of directories on a branch
1956+ path: /rw/dirB/$AUFS_WH_DR_BRHINO
1957+
1958+The filename of "detailed info per directory" represents the lower
1959+branch, and its format is
1960+- a type of the branch id
1961+ one of these.
1962+ + uuid (not implemented yet)
1963+ + fsid
1964+ + dev
1965+- the inode-number of the branch root dir
1966+
1967+And it contains these info in a single regular file.
1968+- magic number
1969+- branch's inode-number of the logically renamed dir
1970+- the name of the before-renamed dir
1971+
1972+The "detailed info per directory" file is created in aufs rename(2), and
1973+loaded in any lookup.
1974+The info is considered in lookup for the matching case only. Here
1975+"matching" means that the root of branch (in the info filename) is same
1976+to the current looking-up branch. After looking-up the before-renamed
1977+name, the inode-number is compared. And the matched dentry is used.
1978+
1979+The "inode-number list of directories" is a regular file which contains
1980+simply the inode-numbers on the branch. The file is created or updated
1981+in removing the branch, and loaded in adding the branch. Its lifetime is
1982+equal to the branch.
1983+The list is refered in lookup, and when the current target inode is
1984+found in the list, the aufs tries loading the "detailed info per
1985+directory" and get the changed and valid name of the dir.
1986+
1987+Theoretically these "extra informaiton" may be able to be put into XATTR
1988+in the dir inode. But aufs doesn't choose this way because
1989+1. XATTR may not be supported by the branch (or its configuration)
1990+2. XATTR may have its size limit.
1991+3. XATTR may be less easy to convert than a regular file, when the
1992+ format of the info is changed in the future.
1993+At the same time, I agree that the regular file approach is much slower
1994+than XATTR approach. So, in the future, aufs may take the XATTR or other
1995+better approach.
1996+
1997+This DIRREN feature is enabled by aufs configuration, and is activated
1998+by a new mount option.
1999+
2000+For the more complicated case, there is a work with UDBA option, which
2001+is to dected the direct access to the branches (by-passing aufs) and to
2002+maintain the cashes in aufs. Since a single cached aufs dentry may
2003+contains two names, before- and after-rename, the name comparision in
2004+UDBA handler may not work correctly. In this case, the behaviour will be
2005+equivalen to udba=reval case.
2006diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
2007--- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt 1970-01-01 01:00:00.000000000 +0100
2008+++ linux/Documentation/filesystems/aufs/design/06fhsm.txt 2021-02-22 22:51:59.058078584 +0100
2009@@ -0,0 +1,120 @@
2010+
2011+# Copyright (C) 2011-2020 Junjiro R. Okajima
2012+#
2013+# This program is free software; you can redistribute it and/or modify
2014+# it under the terms of the GNU General Public License as published by
2015+# the Free Software Foundation; either version 2 of the License, or
2016+# (at your option) any later version.
2017+#
2018+# This program is distributed in the hope that it will be useful,
2019+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2020+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2021+# GNU General Public License for more details.
2022+#
2023+# You should have received a copy of the GNU General Public License
2024+# along with this program; if not, write to the Free Software
2025+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2026+
2027+
2028+File-based Hierarchical Storage Management (FHSM)
2029+----------------------------------------------------------------------
2030+Hierarchical Storage Management (or HSM) is a well-known feature in the
2031+storage world. Aufs provides this feature as file-based with multiple
2032+writable branches, based upon the principle of "Colder, the Lower".
2033+Here the word "colder" means that the less used files, and "lower" means
2034+that the position in the order of the stacked branches vertically.
2035+These multiple writable branches are prioritized, ie. the topmost one
2036+should be the fastest drive and be used heavily.
2037+
2038+o Characters in aufs FHSM story
2039+- aufs itself and a new branch attribute.
2040+- a new ioctl interface to move-down and to establish a connection with
2041+ the daemon ("move-down" is a converse of "copy-up").
2042+- userspace tool and daemon.
2043+
2044+The userspace daemon establishes a connection with aufs and waits for
2045+the notification. The notified information is very similar to struct
2046+statfs containing the number of consumed blocks and inodes.
2047+When the consumed blocks/inodes of a branch exceeds the user-specified
2048+upper watermark, the daemon activates its move-down process until the
2049+consumed blocks/inodes reaches the user-specified lower watermark.
2050+
2051+The actual move-down is done by aufs based upon the request from
2052+user-space since we need to maintain the inode number and the internal
2053+pointer arrays in aufs.
2054+
2055+Currently aufs FHSM handles the regular files only. Additionally they
2056+must not be hard-linked nor pseudo-linked.
2057+
2058+
2059+o Cowork of aufs and the user-space daemon
2060+ During the userspace daemon established the connection, aufs sends a
2061+ small notification to it whenever aufs writes something into the
2062+ writable branch. But it may cost high since aufs issues statfs(2)
2063+ internally. So user can specify a new option to cache the
2064+ info. Actually the notification is controlled by these factors.
2065+ + the specified cache time.
2066+ + classified as "force" by aufs internally.
2067+ Until the specified time expires, aufs doesn't send the info
2068+ except the forced cases. When aufs decide forcing, the info is always
2069+ notified to userspace.
2070+ For example, the number of free inodes is generally large enough and
2071+ the shortage of it happens rarely. So aufs doesn't force the
2072+ notification when creating a new file, directory and others. This is
2073+ the typical case which aufs doesn't force.
2074+ When aufs writes the actual filedata and the files consumes any of new
2075+ blocks, the aufs forces notifying.
2076+
2077+
2078+o Interfaces in aufs
2079+- New branch attribute.
2080+ + fhsm
2081+ Specifies that the branch is managed by FHSM feature. In other word,
2082+ participant in the FHSM.
2083+ When nofhsm is set to the branch, it will not be the source/target
2084+ branch of the move-down operation. This attribute is set
2085+ independently from coo and moo attributes, and if you want full
2086+ FHSM, you should specify them as well.
2087+- New mount option.
2088+ + fhsm_sec
2089+ Specifies a second to suppress many less important info to be
2090+ notified.
2091+- New ioctl.
2092+ + AUFS_CTL_FHSM_FD
2093+ create a new file descriptor which userspace can read the notification
2094+ (a subset of struct statfs) from aufs.
2095+- Module parameter 'brs'
2096+ It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2097+ be set.
2098+- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2099+ When there are two or more branches with fhsm attributes,
2100+ /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2101+ terminates it. As a result of remounting and branch-manipulation, the
2102+ number of branches with fhsm attribute can be one. In this case,
2103+ /sbin/mount.aufs will terminate the user-space daemon.
2104+
2105+
2106+Finally the operation is done as these steps in kernel-space.
2107+- make sure that,
2108+ + no one else is using the file.
2109+ + the file is not hard-linked.
2110+ + the file is not pseudo-linked.
2111+ + the file is a regular file.
2112+ + the parent dir is not opaqued.
2113+- find the target writable branch.
2114+- make sure the file is not whiteout-ed by the upper (than the target)
2115+ branch.
2116+- make the parent dir on the target branch.
2117+- mutex lock the inode on the branch.
2118+- unlink the whiteout on the target branch (if exists).
2119+- lookup and create the whiteout-ed temporary name on the target branch.
2120+- copy the file as the whiteout-ed temporary name on the target branch.
2121+- rename the whiteout-ed temporary name to the original name.
2122+- unlink the file on the source branch.
2123+- maintain the internal pointer array and the external inode number
2124+ table (XINO).
2125+- maintain the timestamps and other attributes of the parent dir and the
2126+ file.
2127+
2128+And of course, in every step, an error may happen. So the operation
2129+should restore the original file state after an error happens.
2130diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2131--- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt 1970-01-01 01:00:00.000000000 +0100
2132+++ linux/Documentation/filesystems/aufs/design/06mmap.txt 2021-02-22 22:51:59.058078584 +0100
2133@@ -0,0 +1,72 @@
2134+
2135+# Copyright (C) 2005-2020 Junjiro R. Okajima
2136+#
2137+# This program is free software; you can redistribute it and/or modify
2138+# it under the terms of the GNU General Public License as published by
2139+# the Free Software Foundation; either version 2 of the License, or
2140+# (at your option) any later version.
2141+#
2142+# This program is distributed in the hope that it will be useful,
2143+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2144+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2145+# GNU General Public License for more details.
2146+#
2147+# You should have received a copy of the GNU General Public License
2148+# along with this program. If not, see <http://www.gnu.org/licenses/>.
2149+
2150+mmap(2) -- File Memory Mapping
2151+----------------------------------------------------------------------
2152+In aufs, the file-mapped pages are handled by a branch fs directly, no
2153+interaction with aufs. It means aufs_mmap() calls the branch fs's
2154+->mmap().
2155+This approach is simple and good, but there is one problem.
2156+Under /proc, several entries show the mmapped files by its path (with
2157+device and inode number), and the printed path will be the path on the
2158+branch fs's instead of virtual aufs's.
2159+This is not a problem in most cases, but some utilities lsof(1) (and its
2160+user) may expect the path on aufs.
2161+
2162+To address this issue, aufs adds a new member called vm_prfile in struct
2163+vm_area_struct (and struct vm_region). The original vm_file points to
2164+the file on the branch fs in order to handle everything correctly as
2165+usual. The new vm_prfile points to a virtual file in aufs, and the
2166+show-functions in procfs refers to vm_prfile if it is set.
2167+Also we need to maintain several other places where touching vm_file
2168+such like
2169+- fork()/clone() copies vma and the reference count of vm_file is
2170+ incremented.
2171+- merging vma maintains the ref count too.
2172+
2173+This is not a good approach. It just fakes the printed path. But it
2174+leaves all behaviour around f_mapping unchanged. This is surely an
2175+advantage.
2176+Actually aufs had adopted another complicated approach which calls
2177+generic_file_mmap() and handles struct vm_operations_struct. In this
2178+approach, aufs met a hard problem and I could not solve it without
2179+switching the approach.
2180+
2181+There may be one more another approach which is
2182+- bind-mount the branch-root onto the aufs-root internally
2183+- grab the new vfsmount (ie. struct mount)
2184+- lazy-umount the branch-root internally
2185+- in open(2) the aufs-file, open the branch-file with the hidden
2186+ vfsmount (instead of the original branch's vfsmount)
2187+- ideally this "bind-mount and lazy-umount" should be done atomically,
2188+ but it may be possible from userspace by the mount helper.
2189+
2190+Adding the internal hidden vfsmount and using it in opening a file, the
2191+file path under /proc will be printed correctly. This approach looks
2192+smarter, but is not possible I am afraid.
2193+- aufs-root may be bind-mount later. when it happens, another hidden
2194+ vfsmount will be required.
2195+- it is hard to get the chance to bind-mount and lazy-umount
2196+ + in kernel-space, FS can have vfsmount in open(2) via
2197+ file->f_path, and aufs can know its vfsmount. But several locks are
2198+ already acquired, and if aufs tries to bind-mount and lazy-umount
2199+ here, then it may cause a deadlock.
2200+ + in user-space, bind-mount doesn't invoke the mount helper.
2201+- since /proc shows dev and ino, aufs has to give vma these info. it
2202+ means a new member vm_prinode will be necessary. this is essentially
2203+ equivalent to vm_prfile described above.
2204+
2205+I have to give up this "looks-smater" approach.
2206diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2207--- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt 1970-01-01 01:00:00.000000000 +0100
2208+++ linux/Documentation/filesystems/aufs/design/06xattr.txt 2021-02-22 22:51:59.058078584 +0100
2209@@ -0,0 +1,96 @@
2210+
2211+# Copyright (C) 2014-2020 Junjiro R. Okajima
2212+#
2213+# This program is free software; you can redistribute it and/or modify
2214+# it under the terms of the GNU General Public License as published by
2215+# the Free Software Foundation; either version 2 of the License, or
2216+# (at your option) any later version.
2217+#
2218+# This program is distributed in the hope that it will be useful,
2219+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2220+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2221+# GNU General Public License for more details.
2222+#
2223+# You should have received a copy of the GNU General Public License
2224+# along with this program; if not, write to the Free Software
2225+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2226+
2227+
2228+Listing XATTR/EA and getting the value
2229+----------------------------------------------------------------------
2230+For the inode standard attributes (owner, group, timestamps, etc.), aufs
2231+shows the values from the topmost existing file. This behaviour is good
2232+for the non-dir entries since the bahaviour exactly matches the shown
2233+information. But for the directories, aufs considers all the same named
2234+entries on the lower branches. Which means, if one of the lower entry
2235+rejects readdir call, then aufs returns an error even if the topmost
2236+entry allows it. This behaviour is necessary to respect the branch fs's
2237+security, but can make users confused since the user-visible standard
2238+attributes don't match the behaviour.
2239+To address this issue, aufs has a mount option called dirperm1 which
2240+checks the permission for the topmost entry only, and ignores the lower
2241+entry's permission.
2242+
2243+A similar issue can happen around XATTR.
2244+getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2245+always set. Otherwise these very unpleasant situation would happen.
2246+- listxattr(2) may return the duplicated entries.
2247+- users may not be able to remove or reset the XATTR forever,
2248+
2249+
2250+XATTR/EA support in the internal (copy,move)-(up,down)
2251+----------------------------------------------------------------------
2252+Generally the extended attributes of inode are categorized as these.
2253+- "security" for LSM and capability.
2254+- "system" for posix ACL, 'acl' mount option is required for the branch
2255+ fs generally.
2256+- "trusted" for userspace, CAP_SYS_ADMIN is required.
2257+- "user" for userspace, 'user_xattr' mount option is required for the
2258+ branch fs generally.
2259+
2260+Moreover there are some other categories. Aufs handles these rather
2261+unpopular categories as the ordinary ones, ie. there is no special
2262+condition nor exception.
2263+
2264+In copy-up, the support for XATTR on the dst branch may differ from the
2265+src branch. In this case, the copy-up operation will get an error and
2266+the original user operation which triggered the copy-up will fail. It
2267+can happen that even all copy-up will fail.
2268+When both of src and dst branches support XATTR and if an error occurs
2269+during copying XATTR, then the copy-up should fail obviously. That is a
2270+good reason and aufs should return an error to userspace. But when only
2271+the src branch support that XATTR, aufs should not return an error.
2272+For example, the src branch supports ACL but the dst branch doesn't
2273+because the dst branch may natively un-support it or temporary
2274+un-support it due to "noacl" mount option. Of course, the dst branch fs
2275+may NOT return an error even if the XATTR is not supported. It is
2276+totally up to the branch fs.
2277+
2278+Anyway when the aufs internal copy-up gets an error from the dst branch
2279+fs, then aufs tries removing the just copied entry and returns the error
2280+to the userspace. The worst case of this situation will be all copy-up
2281+will fail.
2282+
2283+For the copy-up operation, there two basic approaches.
2284+- copy the specified XATTR only (by category above), and return the
2285+ error unconditionally if it happens.
2286+- copy all XATTR, and ignore the error on the specified category only.
2287+
2288+In order to support XATTR and to implement the correct behaviour, aufs
2289+chooses the latter approach and introduces some new branch attributes,
2290+"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2291+They correspond to the XATTR namespaces (see above). Additionally, to be
2292+convenient, "icex" is also provided which means all "icex*" attributes
2293+are set (here the word "icex" stands for "ignore copy-error on XATTR").
2294+
2295+The meaning of these attributes is to ignore the error from setting
2296+XATTR on that branch.
2297+Note that aufs tries copying all XATTR unconditionally, and ignores the
2298+error from the dst branch according to the specified attributes.
2299+
2300+Some XATTR may have its default value. The default value may come from
2301+the parent dir or the environment. If the default value is set at the
2302+file creating-time, it will be overwritten by copy-up.
2303+Some contradiction may happen I am afraid.
2304+Do we need another attribute to stop copying XATTR? I am unsure. For
2305+now, aufs implements the branch attributes to ignore the error.
2306diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2307--- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2308+++ linux/Documentation/filesystems/aufs/design/07export.txt 2021-02-22 22:51:59.058078584 +0100
2309@@ -0,0 +1,58 @@
2310+
2311+# Copyright (C) 2005-2020 Junjiro R. Okajima
2312+#
2313+# This program is free software; you can redistribute it and/or modify
2314+# it under the terms of the GNU General Public License as published by
2315+# the Free Software Foundation; either version 2 of the License, or
2316+# (at your option) any later version.
2317+#
2318+# This program is distributed in the hope that it will be useful,
2319+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2320+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2321+# GNU General Public License for more details.
2322+#
2323+# You should have received a copy of the GNU General Public License
2324+# along with this program. If not, see <http://www.gnu.org/licenses/>.
2325+
2326+Export Aufs via NFS
2327+----------------------------------------------------------------------
2328+Here is an approach.
2329+- like xino/xib, add a new file 'xigen' which stores aufs inode
2330+ generation.
2331+- iget_locked(): initialize aufs inode generation for a new inode, and
2332+ store it in xigen file.
2333+- destroy_inode(): increment aufs inode generation and store it in xigen
2334+ file. it is necessary even if it is not unlinked, because any data of
2335+ inode may be changed by UDBA.
2336+- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2337+ build file handle by
2338+ + branch id (4 bytes)
2339+ + superblock generation (4 bytes)
2340+ + inode number (4 or 8 bytes)
2341+ + parent dir inode number (4 or 8 bytes)
2342+ + inode generation (4 bytes))
2343+ + return value of exportfs_encode_fh() for the parent on a branch (4
2344+ bytes)
2345+ + file handle for a branch (by exportfs_encode_fh())
2346+- fh_to_dentry():
2347+ + find the index of a branch from its id in handle, and check it is
2348+ still exist in aufs.
2349+ + 1st level: get the inode number from handle and search it in cache.
2350+ + 2nd level: if not found in cache, get the parent inode number from
2351+ the handle and search it in cache. and then open the found parent
2352+ dir, find the matching inode number by vfs_readdir() and get its
2353+ name, and call lookup_one_len() for the target dentry.
2354+ + 3rd level: if the parent dir is not cached, call
2355+ exportfs_decode_fh() for a branch and get the parent on a branch,
2356+ build a pathname of it, convert it a pathname in aufs, call
2357+ path_lookup(). now aufs gets a parent dir dentry, then handle it as
2358+ the 2nd level.
2359+ + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2360+ for every branch, but not itself. to get this, (currently) aufs
2361+ searches in current->nsproxy->mnt_ns list. it may not be a good
2362+ idea, but I didn't get other approach.
2363+ + test the generation of the gotten inode.
2364+- every inode operation: they may get EBUSY due to UDBA. in this case,
2365+ convert it into ESTALE for NFSD.
2366+- readdir(): call lockdep_on/off() because filldir in NFSD calls
2367+ lookup_one_len(), vfs_getattr(), encode_fh() and others.
2368diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2369--- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt 1970-01-01 01:00:00.000000000 +0100
2370+++ linux/Documentation/filesystems/aufs/design/08shwh.txt 2021-02-22 22:51:59.058078584 +0100
2371@@ -0,0 +1,52 @@
2372+
2373+# Copyright (C) 2005-2020 Junjiro R. Okajima
2374+#
2375+# This program is free software; you can redistribute it and/or modify
2376+# it under the terms of the GNU General Public License as published by
2377+# the Free Software Foundation; either version 2 of the License, or
2378+# (at your option) any later version.
2379+#
2380+# This program is distributed in the hope that it will be useful,
2381+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2382+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2383+# GNU General Public License for more details.
2384+#
2385+# You should have received a copy of the GNU General Public License
2386+# along with this program. If not, see <http://www.gnu.org/licenses/>.
2387+
2388+Show Whiteout Mode (shwh)
2389+----------------------------------------------------------------------
2390+Generally aufs hides the name of whiteouts. But in some cases, to show
2391+them is very useful for users. For instance, creating a new middle layer
2392+(branch) by merging existing layers.
2393+
2394+(borrowing aufs1 HOW-TO from a user, Michael Towers)
2395+When you have three branches,
2396+- Bottom: 'system', squashfs (underlying base system), read-only
2397+- Middle: 'mods', squashfs, read-only
2398+- Top: 'overlay', ram (tmpfs), read-write
2399+
2400+The top layer is loaded at boot time and saved at shutdown, to preserve
2401+the changes made to the system during the session.
2402+When larger changes have been made, or smaller changes have accumulated,
2403+the size of the saved top layer data grows. At this point, it would be
2404+nice to be able to merge the two overlay branches ('mods' and 'overlay')
2405+and rewrite the 'mods' squashfs, clearing the top layer and thus
2406+restoring save and load speed.
2407+
2408+This merging is simplified by the use of another aufs mount, of just the
2409+two overlay branches using the 'shwh' option.
2410+# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2411+ aufs /livesys/merge_union
2412+
2413+A merged view of these two branches is then available at
2414+/livesys/merge_union, and the new feature is that the whiteouts are
2415+visible!
2416+Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2417+writing to all branches. Also the default mode for all branches is 'ro'.
2418+It is now possible to save the combined contents of the two overlay
2419+branches to a new squashfs, e.g.:
2420+# mksquashfs /livesys/merge_union /path/to/newmods.squash
2421+
2422+This new squashfs archive can be stored on the boot device and the
2423+initramfs will use it to replace the old one at the next boot.
2424diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2425--- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt 1970-01-01 01:00:00.000000000 +0100
2426+++ linux/Documentation/filesystems/aufs/design/10dynop.txt 2021-02-22 22:51:59.058078584 +0100
2427@@ -0,0 +1,47 @@
2428+
2429+# Copyright (C) 2010-2020 Junjiro R. Okajima
2430+#
2431+# This program is free software; you can redistribute it and/or modify
2432+# it under the terms of the GNU General Public License as published by
2433+# the Free Software Foundation; either version 2 of the License, or
2434+# (at your option) any later version.
2435+#
2436+# This program is distributed in the hope that it will be useful,
2437+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2438+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2439+# GNU General Public License for more details.
2440+#
2441+# You should have received a copy of the GNU General Public License
2442+# along with this program. If not, see <http://www.gnu.org/licenses/>.
2443+
2444+Dynamically customizable FS operations
2445+----------------------------------------------------------------------
2446+Generally FS operations (struct inode_operations, struct
2447+address_space_operations, struct file_operations, etc.) are defined as
2448+"static const", but it never means that FS have only one set of
2449+operation. Some FS have multiple sets of them. For instance, ext2 has
2450+three sets, one for XIP, for NOBH, and for normal.
2451+Since aufs overrides and redirects these operations, sometimes aufs has
2452+to change its behaviour according to the branch FS type. More importantly
2453+VFS acts differently if a function (member in the struct) is set or
2454+not. It means aufs should have several sets of operations and select one
2455+among them according to the branch FS definition.
2456+
2457+In order to solve this problem and not to affect the behaviour of VFS,
2458+aufs defines these operations dynamically. For instance, aufs defines
2459+dummy direct_IO function for struct address_space_operations, but it may
2460+not be set to the address_space_operations actually. When the branch FS
2461+doesn't have it, aufs doesn't set it to its address_space_operations
2462+while the function definition itself is still alive. So the behaviour
2463+itself will not change, and it will return an error when direct_IO is
2464+not set.
2465+
2466+The lifetime of these dynamically generated operation object is
2467+maintained by aufs branch object. When the branch is removed from aufs,
2468+the reference counter of the object is decremented. When it reaches
2469+zero, the dynamically generated operation object will be freed.
2470+
2471+This approach is designed to support AIO (io_submit), Direct I/O and
2472+XIP (DAX) mainly.
2473+Currently this approach is applied to address_space_operations for
2474+regular files only.
2475diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2476--- /usr/share/empty/Documentation/filesystems/aufs/README 1970-01-01 01:00:00.000000000 +0100
2477+++ linux/Documentation/filesystems/aufs/README 2021-02-22 22:51:59.054745247 +0100
2478@@ -0,0 +1,401 @@
2479+
2480+Aufs5 -- advanced multi layered unification filesystem version 5.x
2481+http://aufs.sf.net
2482+Junjiro R. Okajima
2483+
2484+
2485+0. Introduction
2486+----------------------------------------
2487+In the early days, aufs was entirely re-designed and re-implemented
2488+Unionfs Version 1.x series. Adding many original ideas, approaches,
2489+improvements and implementations, it became totally different from
2490+Unionfs while keeping the basic features.
2491+Later, Unionfs Version 2.x series began taking some of the same
2492+approaches to aufs1's.
2493+Unionfs was being developed by Professor Erez Zadok at Stony Brook
2494+University and his team.
2495+
2496+Aufs5 supports linux-v5.0 and later, If you want older kernel version
2497+support,
2498+- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
2499+- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
2500+- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
2501+ or aufs1 from CVS on SourceForge.
2502+
2503+Note: it becomes clear that "Aufs was rejected. Let's give it up."
2504+ According to Christoph Hellwig, linux rejects all union-type
2505+ filesystems but UnionMount.
2506+<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2507+
2508+PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2509+ UnionMount, and he pointed out an issue around a directory mutex
2510+ lock and aufs addressed it. But it is still unsure whether aufs will
2511+ be merged (or any other union solution).
2512+<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2513+
2514+
2515+1. Features
2516+----------------------------------------
2517+- unite several directories into a single virtual filesystem. The member
2518+ directory is called as a branch.
2519+- you can specify the permission flags to the branch, which are 'readonly',
2520+ 'readwrite' and 'whiteout-able.'
2521+- by upper writable branch, internal copyup and whiteout, files/dirs on
2522+ readonly branch are modifiable logically.
2523+- dynamic branch manipulation, add, del.
2524+- etc...
2525+
2526+Also there are many enhancements in aufs, such as:
2527+- test only the highest one for the directory permission (dirperm1)
2528+- copyup on open (coo=)
2529+- 'move' policy for copy-up between two writable branches, after
2530+ checking free space.
2531+- xattr, acl
2532+- readdir(3) in userspace.
2533+- keep inode number by external inode number table
2534+- keep the timestamps of file/dir in internal copyup operation
2535+- seekable directory, supporting NFS readdir.
2536+- whiteout is hardlinked in order to reduce the consumption of inodes
2537+ on branch
2538+- do not copyup, nor create a whiteout when it is unnecessary
2539+- revert a single systemcall when an error occurs in aufs
2540+- remount interface instead of ioctl
2541+- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2542+- loopback mounted filesystem as a branch
2543+- kernel thread for removing the dir who has a plenty of whiteouts
2544+- support copyup sparse file (a file which has a 'hole' in it)
2545+- default permission flags for branches
2546+- selectable permission flags for ro branch, whether whiteout can
2547+ exist or not
2548+- export via NFS.
2549+- support <sysfs>/fs/aufs and <debugfs>/aufs.
2550+- support multiple writable branches, some policies to select one
2551+ among multiple writable branches.
2552+- a new semantics for link(2) and rename(2) to support multiple
2553+ writable branches.
2554+- no glibc changes are required.
2555+- pseudo hardlink (hardlink over branches)
2556+- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2557+ including NFS or remote filesystem branch.
2558+- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2559+- and more...
2560+
2561+Currently these features are dropped temporary from aufs5.
2562+See design/08plan.txt in detail.
2563+- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2564+ (robr)
2565+- statistics of aufs thread (/sys/fs/aufs/stat)
2566+
2567+Features or just an idea in the future (see also design/*.txt),
2568+- reorder the branch index without del/re-add.
2569+- permanent xino files for NFSD
2570+- an option for refreshing the opened files after add/del branches
2571+- light version, without branch manipulation. (unnecessary?)
2572+- copyup in userspace
2573+- inotify in userspace
2574+- readv/writev
2575+
2576+
2577+2. Download
2578+----------------------------------------
2579+There are three GIT trees for aufs5, aufs5-linux.git,
2580+aufs5-standalone.git, and aufs-util.git. Note that there is no "5" in
2581+"aufs-util.git."
2582+While the aufs-util is always necessary, you need either of aufs5-linux
2583+or aufs5-standalone.
2584+
2585+The aufs5-linux tree includes the whole linux mainline GIT tree,
2586+git://git.kernel.org/.../torvalds/linux.git.
2587+And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2588+build aufs5 as an external kernel module.
2589+Several extra patches are not included in this tree. Only
2590+aufs5-standalone tree contains them. They are described in the later
2591+section "Configuration and Compilation."
2592+
2593+On the other hand, the aufs5-standalone tree has only aufs source files
2594+and necessary patches, and you can select CONFIG_AUFS_FS=m.
2595+But you need to apply all aufs patches manually.
2596+
2597+You will find GIT branches whose name is in form of "aufs5.x" where "x"
2598+represents the linux kernel version, "linux-5.x". For instance,
2599+"aufs5.0" is for linux-5.0. For latest "linux-5.x-rcN", use
2600+"aufs5.x-rcN" branch.
2601+
2602+o aufs5-linux tree
2603+$ git clone --reference /your/linux/git/tree \
2604+ git://github.com/sfjro/aufs5-linux.git aufs5-linux.git
2605+- if you don't have linux GIT tree, then remove "--reference ..."
2606+$ cd aufs5-linux.git
2607+$ git checkout origin/aufs5.0
2608+
2609+Or You may want to directly git-pull aufs into your linux GIT tree, and
2610+leave the patch-work to GIT.
2611+$ cd /your/linux/git/tree
2612+$ git remote add aufs5 git://github.com/sfjro/aufs5-linux.git
2613+$ git fetch aufs5
2614+$ git checkout -b my5.0 v5.0
2615+$ (add your local change...)
2616+$ git pull aufs5 aufs5.0
2617+- now you have v5.0 + your_changes + aufs5.0 in you my5.0 branch.
2618+- you may need to solve some conflicts between your_changes and
2619+ aufs5.0. in this case, git-rerere is recommended so that you can
2620+ solve the similar conflicts automatically when you upgrade to 5.1 or
2621+ later in the future.
2622+
2623+o aufs5-standalone tree
2624+$ git clone git://github.com/sfjro/aufs5-standalone.git aufs5-standalone.git
2625+$ cd aufs5-standalone.git
2626+$ git checkout origin/aufs5.0
2627+
2628+o aufs-util tree
2629+$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2630+- note that the public aufs-util.git is on SourceForge instead of
2631+ GitHUB.
2632+$ cd aufs-util.git
2633+$ git checkout origin/aufs5.0
2634+
2635+Note: The 5.x-rcN branch is to be used with `rc' kernel versions ONLY.
2636+The minor version number, 'x' in '5.x', of aufs may not always
2637+follow the minor version number of the kernel.
2638+Because changes in the kernel that cause the use of a new
2639+minor version number do not always require changes to aufs-util.
2640+
2641+Since aufs-util has its own minor version number, you may not be
2642+able to find a GIT branch in aufs-util for your kernel's
2643+exact minor version number.
2644+In this case, you should git-checkout the branch for the
2645+nearest lower number.
2646+
2647+For (an unreleased) example:
2648+If you are using "linux-5.10" and the "aufs5.10" branch
2649+does not exist in aufs-util repository, then "aufs5.9", "aufs5.8"
2650+or something numerically smaller is the branch for your kernel.
2651+
2652+Also you can view all branches by
2653+ $ git branch -a
2654+
2655+
2656+3. Configuration and Compilation
2657+----------------------------------------
2658+Make sure you have git-checkout'ed the correct branch.
2659+
2660+For aufs5-linux tree,
2661+- enable CONFIG_AUFS_FS.
2662+- set other aufs configurations if necessary.
2663+
2664+For aufs5-standalone tree,
2665+There are several ways to build.
2666+
2667+1.
2668+- apply ./aufs5-kbuild.patch to your kernel source files.
2669+- apply ./aufs5-base.patch too.
2670+- apply ./aufs5-mmap.patch too.
2671+- apply ./aufs5-standalone.patch too, if you have a plan to set
2672+ CONFIG_AUFS_FS=m. otherwise you don't need ./aufs5-standalone.patch.
2673+- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2674+ kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2675+- enable CONFIG_AUFS_FS, you can select either
2676+ =m or =y.
2677+- and build your kernel as usual.
2678+- install the built kernel.
2679+- install the header files too by "make headers_install" to the
2680+ directory where you specify. By default, it is $PWD/usr.
2681+ "make help" shows a brief note for headers_install.
2682+- and reboot your system.
2683+
2684+2.
2685+- module only (CONFIG_AUFS_FS=m).
2686+- apply ./aufs5-base.patch to your kernel source files.
2687+- apply ./aufs5-mmap.patch too.
2688+- apply ./aufs5-standalone.patch too.
2689+- build your kernel, don't forget "make headers_install", and reboot.
2690+- edit ./config.mk and set other aufs configurations if necessary.
2691+ Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2692+ every aufs configurations.
2693+- build the module by simple "make".
2694+- you can specify ${KDIR} make variable which points to your kernel
2695+ source tree.
2696+- install the files
2697+ + run "make install" to install the aufs module, or copy the built
2698+ $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2699+ + run "make install_headers" (instead of headers_install) to install
2700+ the modified aufs header file (you can specify DESTDIR which is
2701+ available in aufs standalone version's Makefile only), or copy
2702+ $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2703+ you like manually. By default, the target directory is $PWD/usr.
2704+- no need to apply aufs5-kbuild.patch, nor copying source files to your
2705+ kernel source tree.
2706+
2707+Note: The header file aufs_type.h is necessary to build aufs-util
2708+ as well as "make headers_install" in the kernel source tree.
2709+ headers_install is subject to be forgotten, but it is essentially
2710+ necessary, not only for building aufs-util.
2711+ You may not meet problems without headers_install in some older
2712+ version though.
2713+
2714+And then,
2715+- read README in aufs-util, build and install it
2716+- note that your distribution may contain an obsoleted version of
2717+ aufs_type.h in /usr/include/linux or something. When you build aufs
2718+ utilities, make sure that your compiler refers the correct aufs header
2719+ file which is built by "make headers_install."
2720+- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2721+ then run "make install_ulib" too. And refer to the aufs manual in
2722+ detail.
2723+
2724+There several other patches in aufs5-standalone.git. They are all
2725+optional. When you meet some problems, they will help you.
2726+- aufs5-loopback.patch
2727+ Supports a nested loopback mount in a branch-fs. This patch is
2728+ unnecessary until aufs produces a message like "you may want to try
2729+ another patch for loopback file".
2730+- proc_mounts.patch
2731+ When there are many mountpoints and many mount(2)/umount(2) are
2732+ running, then /proc/mounts may not show the all mountpoints. This
2733+ patch makes /proc/mounts always show the full mountpoints list.
2734+ If you don't want to apply this patch and meet such problem, then you
2735+ need to increase the value of 'ProcMounts_Times' make-variable in
2736+ aufs-util.git as a second best solution.
2737+- vfs-ino.patch
2738+ Modifies a system global kernel internal function get_next_ino() in
2739+ order to stop assigning 0 for an inode-number. Not directly related to
2740+ aufs, but recommended generally.
2741+- tmpfs-idr.patch
2742+ Keeps the tmpfs inode number as the lowest value. Effective to reduce
2743+ the size of aufs XINO files for tmpfs branch. Also it prevents the
2744+ duplication of inode number, which is important for backup tools and
2745+ other utilities. When you find aufs XINO files for tmpfs branch
2746+ growing too much, try this patch.
2747+- lockdep-debug.patch
2748+ Because aufs is not only an ordinary filesystem (callee of VFS), but
2749+ also a caller of VFS functions for branch filesystems, subclassing of
2750+ the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2751+ feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2752+ need to apply this debug patch to expand several constant values.
2753+ If you don't know what LOCKDEP is, then you don't have apply this
2754+ patch.
2755+
2756+
2757+4. Usage
2758+----------------------------------------
2759+At first, make sure aufs-util are installed, and please read the aufs
2760+manual, aufs.5 in aufs-util.git tree.
2761+$ man -l aufs.5
2762+
2763+And then,
2764+$ mkdir /tmp/rw /tmp/aufs
2765+# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2766+
2767+Here is another example. The result is equivalent.
2768+# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2769+ Or
2770+# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2771+# mount -o remount,append:${HOME} /tmp/aufs
2772+
2773+Then, you can see whole tree of your home dir through /tmp/aufs. If
2774+you modify a file under /tmp/aufs, the one on your home directory is
2775+not affected, instead the same named file will be newly created under
2776+/tmp/rw. And all of your modification to a file will be applied to
2777+the one under /tmp/rw. This is called the file based Copy on Write
2778+(COW) method.
2779+Aufs mount options are described in aufs.5.
2780+If you run chroot or something and make your aufs as a root directory,
2781+then you need to customize the shutdown script. See the aufs manual in
2782+detail.
2783+
2784+Additionally, there are some sample usages of aufs which are a
2785+diskless system with network booting, and LiveCD over NFS.
2786+See sample dir in CVS tree on SourceForge.
2787+
2788+
2789+5. Contact
2790+----------------------------------------
2791+When you have any problems or strange behaviour in aufs, please let me
2792+know with:
2793+- /proc/mounts (instead of the output of mount(8))
2794+- /sys/module/aufs/*
2795+- /sys/fs/aufs/* (if you have them)
2796+- /debug/aufs/* (if you have them)
2797+- linux kernel version
2798+ if your kernel is not plain, for example modified by distributor,
2799+ the url where i can download its source is necessary too.
2800+- aufs version which was printed at loading the module or booting the
2801+ system, instead of the date you downloaded.
2802+- configuration (define/undefine CONFIG_AUFS_xxx)
2803+- kernel configuration or /proc/config.gz (if you have it)
2804+- LSM (linux security module, if you are using)
2805+- behaviour which you think to be incorrect
2806+- actual operation, reproducible one is better
2807+- mailto: aufs-users at lists.sourceforge.net
2808+
2809+Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2810+and Feature Requests) on SourceForge. Please join and write to
2811+aufs-users ML.
2812+
2813+
2814+6. Acknowledgements
2815+----------------------------------------
2816+Thanks to everyone who have tried and are using aufs, whoever
2817+have reported a bug or any feedback.
2818+
2819+Especially donators:
2820+Tomas Matejicek(slax.org) made a donation (much more than once).
2821+ Since Apr 2010, Tomas M (the author of Slax and Linux Live
2822+ scripts) is making "doubling" donations.
2823+ Unfortunately I cannot list all of the donators, but I really
2824+ appreciate.
2825+ It ends Aug 2010, but the ordinary donation URL is still available.
2826+ <http://sourceforge.net/donate/index.php?group_id=167503>
2827+Dai Itasaka made a donation (2007/8).
2828+Chuck Smith made a donation (2008/4, 10 and 12).
2829+Henk Schoneveld made a donation (2008/9).
2830+Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2831+Francois Dupoux made a donation (2008/11).
2832+Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2833+ aufs2 GIT tree (2009/2).
2834+William Grant made a donation (2009/3).
2835+Patrick Lane made a donation (2009/4).
2836+The Mail Archive (mail-archive.com) made donations (2009/5).
2837+Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2838+New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2839+Pavel Pronskiy made a donation (2011/2).
2840+Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2841+ Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2842+Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2843+11).
2844+Sam Liddicott made a donation (2011/9).
2845+Era Scarecrow made a donation (2013/4).
2846+Bor Ratajc made a donation (2013/4).
2847+Alessandro Gorreta made a donation (2013/4).
2848+POIRETTE Marc made a donation (2013/4).
2849+Alessandro Gorreta made a donation (2013/4).
2850+lauri kasvandik made a donation (2013/5).
2851+"pemasu from Finland" made a donation (2013/7).
2852+The Parted Magic Project made a donation (2013/9 and 11).
2853+Pavel Barta made a donation (2013/10).
2854+Nikolay Pertsev made a donation (2014/5).
2855+James B made a donation (2014/7 and 2015/7).
2856+Stefano Di Biase made a donation (2014/8).
2857+Daniel Epellei made a donation (2015/1).
2858+OmegaPhil made a donation (2016/1, 2018/4).
2859+Tomasz Szewczyk made a donation (2016/4).
2860+James Burry made a donation (2016/12).
2861+Carsten Rose made a donation (2018/9).
2862+Porteus Kiosk made a donation (2018/10).
2863+
2864+Thank you very much.
2865+Donations are always, including future donations, very important and
2866+helpful for me to keep on developing aufs.
2867+
2868+
2869+7.
2870+----------------------------------------
2871+If you are an experienced user, no explanation is needed. Aufs is
2872+just a linux filesystem.
2873+
2874+
2875+Enjoy!
2876+
2877+# Local variables: ;
2878+# mode: text;
2879+# End: ;
2880diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2881--- /usr/share/empty/fs/aufs/aufs.h 1970-01-01 01:00:00.000000000 +0100
2882+++ linux/fs/aufs/aufs.h 2021-02-22 22:51:59.058078584 +0100
2883@@ -0,0 +1,62 @@
2884+/* SPDX-License-Identifier: GPL-2.0 */
2885+/*
2886+ * Copyright (C) 2005-2020 Junjiro R. Okajima
2887+ *
2888+ * This program, aufs is free software; you can redistribute it and/or modify
2889+ * it under the terms of the GNU General Public License as published by
2890+ * the Free Software Foundation; either version 2 of the License, or
2891+ * (at your option) any later version.
2892+ *
2893+ * This program is distributed in the hope that it will be useful,
2894+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
2895+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2896+ * GNU General Public License for more details.
2897+ *
2898+ * You should have received a copy of the GNU General Public License
2899+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
2900+ */
2901+
2902+/*
2903+ * all header files
2904+ */
2905+
2906+#ifndef __AUFS_H__
2907+#define __AUFS_H__
2908+
2909+#ifdef __KERNEL__
2910+
2911+#define AuStub(type, name, body, ...) \
2912+ static inline type name(__VA_ARGS__) { body; }
2913+
2914+#define AuStubVoid(name, ...) \
2915+ AuStub(void, name, , __VA_ARGS__)
2916+#define AuStubInt0(name, ...) \
2917+ AuStub(int, name, return 0, __VA_ARGS__)
2918+
2919+#include "debug.h"
2920+
2921+#include "branch.h"
2922+#include "cpup.h"
2923+#include "dcsub.h"
2924+#include "dbgaufs.h"
2925+#include "dentry.h"
2926+#include "dir.h"
2927+#include "dirren.h"
2928+#include "dynop.h"
2929+#include "file.h"
2930+#include "fstype.h"
2931+#include "hbl.h"
2932+#include "inode.h"
2933+#include "lcnt.h"
2934+#include "loop.h"
2935+#include "module.h"
2936+#include "opts.h"
2937+#include "rwsem.h"
2938+#include "super.h"
2939+#include "sysaufs.h"
2940+#include "vfsub.h"
2941+#include "whout.h"
2942+#include "wkq.h"
2943+
2944+#endif /* __KERNEL__ */
2945+#endif /* __AUFS_H__ */
2946diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
2947--- /usr/share/empty/fs/aufs/branch.c 1970-01-01 01:00:00.000000000 +0100
2948+++ linux/fs/aufs/branch.c 2021-02-22 22:51:59.058078584 +0100
2949@@ -0,0 +1,1427 @@
2950+// SPDX-License-Identifier: GPL-2.0
2951+/*
2952+ * Copyright (C) 2005-2020 Junjiro R. Okajima
2953+ *
2954+ * This program, aufs is free software; you can redistribute it and/or modify
2955+ * it under the terms of the GNU General Public License as published by
2956+ * the Free Software Foundation; either version 2 of the License, or
2957+ * (at your option) any later version.
2958+ *
2959+ * This program is distributed in the hope that it will be useful,
2960+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
2961+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2962+ * GNU General Public License for more details.
2963+ *
2964+ * You should have received a copy of the GNU General Public License
2965+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
2966+ */
2967+
2968+/*
2969+ * branch management
2970+ */
2971+
2972+#include <linux/compat.h>
2973+#include <linux/statfs.h>
2974+#include "aufs.h"
2975+
2976+/*
2977+ * free a single branch
2978+ */
2979+static void au_br_do_free(struct au_branch *br)
2980+{
2981+ int i;
2982+ struct au_wbr *wbr;
2983+ struct au_dykey **key;
2984+
2985+ au_hnotify_fin_br(br);
2986+ /* always, regardless the mount option */
2987+ au_dr_hino_free(&br->br_dirren);
2988+ au_xino_put(br);
2989+
2990+ AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
2991+ au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
2992+ AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
2993+ au_lcnt_fin(&br->br_count, /*do_sync*/0);
2994+
2995+ wbr = br->br_wbr;
2996+ if (wbr) {
2997+ for (i = 0; i < AuBrWh_Last; i++)
2998+ dput(wbr->wbr_wh[i]);
2999+ AuDebugOn(atomic_read(&wbr->wbr_wh_running));
3000+ AuRwDestroy(&wbr->wbr_wh_rwsem);
3001+ }
3002+
3003+ if (br->br_fhsm) {
3004+ au_br_fhsm_fin(br->br_fhsm);
3005+ au_kfree_try_rcu(br->br_fhsm);
3006+ }
3007+
3008+ key = br->br_dykey;
3009+ for (i = 0; i < AuBrDynOp; i++, key++)
3010+ if (*key)
3011+ au_dy_put(*key);
3012+ else
3013+ break;
3014+
3015+ /* recursive lock, s_umount of branch's */
3016+ /* synchronize_rcu(); */ /* why? */
3017+ lockdep_off();
3018+ path_put(&br->br_path);
3019+ lockdep_on();
3020+ au_kfree_rcu(wbr);
3021+ au_lcnt_wait_for_fin(&br->br_nfiles);
3022+ au_lcnt_wait_for_fin(&br->br_count);
3023+ /* I don't know why, but percpu_refcount requires this */
3024+ /* synchronize_rcu(); */
3025+ au_kfree_rcu(br);
3026+}
3027+
3028+/*
3029+ * frees all branches
3030+ */
3031+void au_br_free(struct au_sbinfo *sbinfo)
3032+{
3033+ aufs_bindex_t bmax;
3034+ struct au_branch **br;
3035+
3036+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3037+
3038+ bmax = sbinfo->si_bbot + 1;
3039+ br = sbinfo->si_branch;
3040+ while (bmax--)
3041+ au_br_do_free(*br++);
3042+}
3043+
3044+/*
3045+ * find the index of a branch which is specified by @br_id.
3046+ */
3047+int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3048+{
3049+ aufs_bindex_t bindex, bbot;
3050+
3051+ bbot = au_sbbot(sb);
3052+ for (bindex = 0; bindex <= bbot; bindex++)
3053+ if (au_sbr_id(sb, bindex) == br_id)
3054+ return bindex;
3055+ return -1;
3056+}
3057+
3058+/* ---------------------------------------------------------------------- */
3059+
3060+/*
3061+ * add a branch
3062+ */
3063+
3064+static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3065+ struct dentry *h_root)
3066+{
3067+ if (unlikely(h_adding == h_root
3068+ || au_test_loopback_overlap(sb, h_adding)))
3069+ return 1;
3070+ if (h_adding->d_sb != h_root->d_sb)
3071+ return 0;
3072+ return au_test_subdir(h_adding, h_root)
3073+ || au_test_subdir(h_root, h_adding);
3074+}
3075+
3076+/*
3077+ * returns a newly allocated branch. @new_nbranch is a number of branches
3078+ * after adding a branch.
3079+ */
3080+static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3081+ int perm)
3082+{
3083+ struct au_branch *add_branch;
3084+ struct dentry *root;
3085+ struct inode *inode;
3086+ int err;
3087+
3088+ err = -ENOMEM;
3089+ add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
3090+ if (unlikely(!add_branch))
3091+ goto out;
3092+ add_branch->br_xino = au_xino_alloc(/*nfile*/1);
3093+ if (unlikely(!add_branch->br_xino))
3094+ goto out_br;
3095+ err = au_hnotify_init_br(add_branch, perm);
3096+ if (unlikely(err))
3097+ goto out_xino;
3098+
3099+ if (au_br_writable(perm)) {
3100+ /* may be freed separately at changing the branch permission */
3101+ add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
3102+ GFP_NOFS);
3103+ if (unlikely(!add_branch->br_wbr))
3104+ goto out_hnotify;
3105+ }
3106+
3107+ if (au_br_fhsm(perm)) {
3108+ err = au_fhsm_br_alloc(add_branch);
3109+ if (unlikely(err))
3110+ goto out_wbr;
3111+ }
3112+
3113+ root = sb->s_root;
3114+ err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
3115+ if (!err)
3116+ err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
3117+ if (!err) {
3118+ inode = d_inode(root);
3119+ err = au_hinode_realloc(au_ii(inode), new_nbranch,
3120+ /*may_shrink*/0);
3121+ }
3122+ if (!err)
3123+ return add_branch; /* success */
3124+
3125+out_wbr:
3126+ au_kfree_rcu(add_branch->br_wbr);
3127+out_hnotify:
3128+ au_hnotify_fin_br(add_branch);
3129+out_xino:
3130+ au_xino_put(add_branch);
3131+out_br:
3132+ au_kfree_rcu(add_branch);
3133+out:
3134+ return ERR_PTR(err);
3135+}
3136+
3137+/*
3138+ * test if the branch permission is legal or not.
3139+ */
3140+static int test_br(struct inode *inode, int brperm, char *path)
3141+{
3142+ int err;
3143+
3144+ err = (au_br_writable(brperm) && IS_RDONLY(inode));
3145+ if (!err)
3146+ goto out;
3147+
3148+ err = -EINVAL;
3149+ pr_err("write permission for readonly mount or inode, %s\n", path);
3150+
3151+out:
3152+ return err;
3153+}
3154+
3155+/*
3156+ * returns:
3157+ * 0: success, the caller will add it
3158+ * plus: success, it is already unified, the caller should ignore it
3159+ * minus: error
3160+ */
3161+static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3162+{
3163+ int err;
3164+ aufs_bindex_t bbot, bindex;
3165+ struct dentry *root, *h_dentry;
3166+ struct inode *inode, *h_inode;
3167+
3168+ root = sb->s_root;
3169+ bbot = au_sbbot(sb);
3170+ if (unlikely(bbot >= 0
3171+ && au_find_dbindex(root, add->path.dentry) >= 0)) {
3172+ err = 1;
3173+ if (!remount) {
3174+ err = -EINVAL;
3175+ pr_err("%s duplicated\n", add->pathname);
3176+ }
3177+ goto out;
3178+ }
3179+
3180+ err = -ENOSPC; /* -E2BIG; */
3181+ if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3182+ || AUFS_BRANCH_MAX - 1 <= bbot)) {
3183+ pr_err("number of branches exceeded %s\n", add->pathname);
3184+ goto out;
3185+ }
3186+
3187+ err = -EDOM;
3188+ if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
3189+ pr_err("bad index %d\n", add->bindex);
3190+ goto out;
3191+ }
3192+
3193+ inode = d_inode(add->path.dentry);
3194+ err = -ENOENT;
3195+ if (unlikely(!inode->i_nlink)) {
3196+ pr_err("no existence %s\n", add->pathname);
3197+ goto out;
3198+ }
3199+
3200+ err = -EINVAL;
3201+ if (unlikely(inode->i_sb == sb)) {
3202+ pr_err("%s must be outside\n", add->pathname);
3203+ goto out;
3204+ }
3205+
3206+ if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3207+ pr_err("unsupported filesystem, %s (%s)\n",
3208+ add->pathname, au_sbtype(inode->i_sb));
3209+ goto out;
3210+ }
3211+
3212+ if (unlikely(inode->i_sb->s_stack_depth)) {
3213+ pr_err("already stacked, %s (%s)\n",
3214+ add->pathname, au_sbtype(inode->i_sb));
3215+ goto out;
3216+ }
3217+
3218+ err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3219+ if (unlikely(err))
3220+ goto out;
3221+
3222+ if (bbot < 0)
3223+ return 0; /* success */
3224+
3225+ err = -EINVAL;
3226+ for (bindex = 0; bindex <= bbot; bindex++)
3227+ if (unlikely(test_overlap(sb, add->path.dentry,
3228+ au_h_dptr(root, bindex)))) {
3229+ pr_err("%s is overlapped\n", add->pathname);
3230+ goto out;
3231+ }
3232+
3233+ err = 0;
3234+ if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3235+ h_dentry = au_h_dptr(root, 0);
3236+ h_inode = d_inode(h_dentry);
3237+ if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3238+ || !uid_eq(h_inode->i_uid, inode->i_uid)
3239+ || !gid_eq(h_inode->i_gid, inode->i_gid))
3240+ pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3241+ add->pathname,
3242+ i_uid_read(inode), i_gid_read(inode),
3243+ (inode->i_mode & S_IALLUGO),
3244+ i_uid_read(h_inode), i_gid_read(h_inode),
3245+ (h_inode->i_mode & S_IALLUGO));
3246+ }
3247+
3248+out:
3249+ return err;
3250+}
3251+
3252+/*
3253+ * initialize or clean the whiteouts for an adding branch
3254+ */
3255+static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3256+ int new_perm)
3257+{
3258+ int err, old_perm;
3259+ aufs_bindex_t bindex;
3260+ struct inode *h_inode;
3261+ struct au_wbr *wbr;
3262+ struct au_hinode *hdir;
3263+ struct dentry *h_dentry;
3264+
3265+ err = vfsub_mnt_want_write(au_br_mnt(br));
3266+ if (unlikely(err))
3267+ goto out;
3268+
3269+ wbr = br->br_wbr;
3270+ old_perm = br->br_perm;
3271+ br->br_perm = new_perm;
3272+ hdir = NULL;
3273+ h_inode = NULL;
3274+ bindex = au_br_index(sb, br->br_id);
3275+ if (0 <= bindex) {
3276+ hdir = au_hi(d_inode(sb->s_root), bindex);
3277+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
3278+ } else {
3279+ h_dentry = au_br_dentry(br);
3280+ h_inode = d_inode(h_dentry);
3281+ inode_lock_nested(h_inode, AuLsc_I_PARENT);
3282+ }
3283+ if (!wbr)
3284+ err = au_wh_init(br, sb);
3285+ else {
3286+ wbr_wh_write_lock(wbr);
3287+ err = au_wh_init(br, sb);
3288+ wbr_wh_write_unlock(wbr);
3289+ }
3290+ if (hdir)
3291+ au_hn_inode_unlock(hdir);
3292+ else
3293+ inode_unlock(h_inode);
3294+ vfsub_mnt_drop_write(au_br_mnt(br));
3295+ br->br_perm = old_perm;
3296+
3297+ if (!err && wbr && !au_br_writable(new_perm)) {
3298+ au_kfree_rcu(wbr);
3299+ br->br_wbr = NULL;
3300+ }
3301+
3302+out:
3303+ return err;
3304+}
3305+
3306+static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3307+ int perm)
3308+{
3309+ int err;
3310+ struct kstatfs kst;
3311+ struct au_wbr *wbr;
3312+
3313+ wbr = br->br_wbr;
3314+ au_rw_init(&wbr->wbr_wh_rwsem);
3315+ atomic_set(&wbr->wbr_wh_running, 0);
3316+
3317+ /*
3318+ * a limit for rmdir/rename a dir
3319+ * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3320+ */
3321+ err = vfs_statfs(&br->br_path, &kst);
3322+ if (unlikely(err))
3323+ goto out;
3324+ err = -EINVAL;
3325+ if (kst.f_namelen >= NAME_MAX)
3326+ err = au_br_init_wh(sb, br, perm);
3327+ else
3328+ pr_err("%pd(%s), unsupported namelen %ld\n",
3329+ au_br_dentry(br),
3330+ au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3331+
3332+out:
3333+ return err;
3334+}
3335+
3336+/* initialize a new branch */
3337+static int au_br_init(struct au_branch *br, struct super_block *sb,
3338+ struct au_opt_add *add)
3339+{
3340+ int err;
3341+ struct au_branch *brbase;
3342+ struct file *xf;
3343+ struct inode *h_inode;
3344+
3345+ err = 0;
3346+ br->br_perm = add->perm;
3347+ br->br_path = add->path; /* set first, path_get() later */
3348+ spin_lock_init(&br->br_dykey_lock);
3349+ au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3350+ au_lcnt_init(&br->br_count, /*release*/NULL);
3351+ br->br_id = au_new_br_id(sb);
3352+ AuDebugOn(br->br_id < 0);
3353+
3354+ /* always, regardless the given option */
3355+ err = au_dr_br_init(sb, br, &add->path);
3356+ if (unlikely(err))
3357+ goto out_err;
3358+
3359+ if (au_br_writable(add->perm)) {
3360+ err = au_wbr_init(br, sb, add->perm);
3361+ if (unlikely(err))
3362+ goto out_err;
3363+ }
3364+
3365+ if (au_opt_test(au_mntflags(sb), XINO)) {
3366+ brbase = au_sbr(sb, 0);
3367+ xf = au_xino_file(brbase->br_xino, /*idx*/-1);
3368+ AuDebugOn(!xf);
3369+ h_inode = d_inode(add->path.dentry);
3370+ err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
3371+ if (unlikely(err)) {
3372+ AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
3373+ goto out_err;
3374+ }
3375+ }
3376+
3377+ sysaufs_br_init(br);
3378+ path_get(&br->br_path);
3379+ goto out; /* success */
3380+
3381+out_err:
3382+ memset(&br->br_path, 0, sizeof(br->br_path));
3383+out:
3384+ return err;
3385+}
3386+
3387+static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3388+ struct au_branch *br, aufs_bindex_t bbot,
3389+ aufs_bindex_t amount)
3390+{
3391+ struct au_branch **brp;
3392+
3393+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3394+
3395+ brp = sbinfo->si_branch + bindex;
3396+ memmove(brp + 1, brp, sizeof(*brp) * amount);
3397+ *brp = br;
3398+ sbinfo->si_bbot++;
3399+ if (unlikely(bbot < 0))
3400+ sbinfo->si_bbot = 0;
3401+}
3402+
3403+static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3404+ aufs_bindex_t bbot, aufs_bindex_t amount)
3405+{
3406+ struct au_hdentry *hdp;
3407+
3408+ AuRwMustWriteLock(&dinfo->di_rwsem);
3409+
3410+ hdp = au_hdentry(dinfo, bindex);
3411+ memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3412+ au_h_dentry_init(hdp);
3413+ dinfo->di_bbot++;
3414+ if (unlikely(bbot < 0))
3415+ dinfo->di_btop = 0;
3416+}
3417+
3418+static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3419+ aufs_bindex_t bbot, aufs_bindex_t amount)
3420+{
3421+ struct au_hinode *hip;
3422+
3423+ AuRwMustWriteLock(&iinfo->ii_rwsem);
3424+
3425+ hip = au_hinode(iinfo, bindex);
3426+ memmove(hip + 1, hip, sizeof(*hip) * amount);
3427+ au_hinode_init(hip);
3428+ iinfo->ii_bbot++;
3429+ if (unlikely(bbot < 0))
3430+ iinfo->ii_btop = 0;
3431+}
3432+
3433+static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3434+ aufs_bindex_t bindex)
3435+{
3436+ struct dentry *root, *h_dentry;
3437+ struct inode *root_inode, *h_inode;
3438+ aufs_bindex_t bbot, amount;
3439+
3440+ root = sb->s_root;
3441+ root_inode = d_inode(root);
3442+ bbot = au_sbbot(sb);
3443+ amount = bbot + 1 - bindex;
3444+ h_dentry = au_br_dentry(br);
3445+ au_sbilist_lock();
3446+ au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3447+ au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3448+ au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3449+ au_set_h_dptr(root, bindex, dget(h_dentry));
3450+ h_inode = d_inode(h_dentry);
3451+ au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3452+ au_sbilist_unlock();
3453+}
3454+
3455+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3456+{
3457+ int err;
3458+ aufs_bindex_t bbot, add_bindex;
3459+ struct dentry *root, *h_dentry;
3460+ struct inode *root_inode;
3461+ struct au_branch *add_branch;
3462+
3463+ root = sb->s_root;
3464+ root_inode = d_inode(root);
3465+ IMustLock(root_inode);
3466+ IiMustWriteLock(root_inode);
3467+ err = test_add(sb, add, remount);
3468+ if (unlikely(err < 0))
3469+ goto out;
3470+ if (err) {
3471+ err = 0;
3472+ goto out; /* success */
3473+ }
3474+
3475+ bbot = au_sbbot(sb);
3476+ add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3477+ err = PTR_ERR(add_branch);
3478+ if (IS_ERR(add_branch))
3479+ goto out;
3480+
3481+ err = au_br_init(add_branch, sb, add);
3482+ if (unlikely(err)) {
3483+ au_br_do_free(add_branch);
3484+ goto out;
3485+ }
3486+
3487+ add_bindex = add->bindex;
3488+ sysaufs_brs_del(sb, add_bindex); /* remove successors */
3489+ au_br_do_add(sb, add_branch, add_bindex);
3490+ sysaufs_brs_add(sb, add_bindex); /* append successors */
3491+ dbgaufs_brs_add(sb, add_bindex, /*topdown*/0); /* rename successors */
3492+
3493+ h_dentry = add->path.dentry;
3494+ if (!add_bindex) {
3495+ au_cpup_attr_all(root_inode, /*force*/1);
3496+ sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3497+ } else
3498+ au_add_nlink(root_inode, d_inode(h_dentry));
3499+
3500+out:
3501+ return err;
3502+}
3503+
3504+/* ---------------------------------------------------------------------- */
3505+
3506+static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3507+ unsigned long long max __maybe_unused,
3508+ void *arg)
3509+{
3510+ unsigned long long n;
3511+ struct file **p, *f;
3512+ struct hlist_bl_head *files;
3513+ struct hlist_bl_node *pos;
3514+ struct au_finfo *finfo;
3515+
3516+ n = 0;
3517+ p = a;
3518+ files = &au_sbi(sb)->si_files;
3519+ hlist_bl_lock(files);
3520+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
3521+ f = finfo->fi_file;
3522+ if (file_count(f)
3523+ && !special_file(file_inode(f)->i_mode)) {
3524+ get_file(f);
3525+ *p++ = f;
3526+ n++;
3527+ AuDebugOn(n > max);
3528+ }
3529+ }
3530+ hlist_bl_unlock(files);
3531+
3532+ return n;
3533+}
3534+
3535+static struct file **au_farray_alloc(struct super_block *sb,
3536+ unsigned long long *max)
3537+{
3538+ struct au_sbinfo *sbi;
3539+
3540+ sbi = au_sbi(sb);
3541+ *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
3542+ return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3543+}
3544+
3545+static void au_farray_free(struct file **a, unsigned long long max)
3546+{
3547+ unsigned long long ull;
3548+
3549+ for (ull = 0; ull < max; ull++)
3550+ if (a[ull])
3551+ fput(a[ull]);
3552+ kvfree(a);
3553+}
3554+
3555+/* ---------------------------------------------------------------------- */
3556+
3557+/*
3558+ * delete a branch
3559+ */
3560+
3561+/* to show the line number, do not make it inlined function */
3562+#define AuVerbose(do_info, fmt, ...) do { \
3563+ if (do_info) \
3564+ pr_info(fmt, ##__VA_ARGS__); \
3565+} while (0)
3566+
3567+static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3568+ aufs_bindex_t bbot)
3569+{
3570+ return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3571+}
3572+
3573+static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3574+ aufs_bindex_t bbot)
3575+{
3576+ return au_test_ibusy(d_inode(dentry), btop, bbot);
3577+}
3578+
3579+/*
3580+ * test if the branch is deletable or not.
3581+ */
3582+static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3583+ unsigned int sigen, const unsigned int verbose)
3584+{
3585+ int err, i, j, ndentry;
3586+ aufs_bindex_t btop, bbot;
3587+ struct au_dcsub_pages dpages;
3588+ struct au_dpage *dpage;
3589+ struct dentry *d;
3590+
3591+ err = au_dpages_init(&dpages, GFP_NOFS);
3592+ if (unlikely(err))
3593+ goto out;
3594+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
3595+ if (unlikely(err))
3596+ goto out_dpages;
3597+
3598+ for (i = 0; !err && i < dpages.ndpage; i++) {
3599+ dpage = dpages.dpages + i;
3600+ ndentry = dpage->ndentry;
3601+ for (j = 0; !err && j < ndentry; j++) {
3602+ d = dpage->dentries[j];
3603+ AuDebugOn(au_dcount(d) <= 0);
3604+ if (!au_digen_test(d, sigen)) {
3605+ di_read_lock_child(d, AuLock_IR);
3606+ if (unlikely(au_dbrange_test(d))) {
3607+ di_read_unlock(d, AuLock_IR);
3608+ continue;
3609+ }
3610+ } else {
3611+ di_write_lock_child(d);
3612+ if (unlikely(au_dbrange_test(d))) {
3613+ di_write_unlock(d);
3614+ continue;
3615+ }
3616+ err = au_reval_dpath(d, sigen);
3617+ if (!err)
3618+ di_downgrade_lock(d, AuLock_IR);
3619+ else {
3620+ di_write_unlock(d);
3621+ break;
3622+ }
3623+ }
3624+
3625+ /* AuDbgDentry(d); */
3626+ btop = au_dbtop(d);
3627+ bbot = au_dbbot(d);
3628+ if (btop <= bindex
3629+ && bindex <= bbot
3630+ && au_h_dptr(d, bindex)
3631+ && au_test_dbusy(d, btop, bbot)) {
3632+ err = -EBUSY;
3633+ AuVerbose(verbose, "busy %pd\n", d);
3634+ AuDbgDentry(d);
3635+ }
3636+ di_read_unlock(d, AuLock_IR);
3637+ }
3638+ }
3639+
3640+out_dpages:
3641+ au_dpages_free(&dpages);
3642+out:
3643+ return err;
3644+}
3645+
3646+static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3647+ unsigned int sigen, const unsigned int verbose)
3648+{
3649+ int err;
3650+ unsigned long long max, ull;
3651+ struct inode *i, **array;
3652+ aufs_bindex_t btop, bbot;
3653+
3654+ array = au_iarray_alloc(sb, &max);
3655+ err = PTR_ERR(array);
3656+ if (IS_ERR(array))
3657+ goto out;
3658+
3659+ err = 0;
3660+ AuDbg("b%d\n", bindex);
3661+ for (ull = 0; !err && ull < max; ull++) {
3662+ i = array[ull];
3663+ if (unlikely(!i))
3664+ break;
3665+ if (i->i_ino == AUFS_ROOT_INO)
3666+ continue;
3667+
3668+ /* AuDbgInode(i); */
3669+ if (au_iigen(i, NULL) == sigen)
3670+ ii_read_lock_child(i);
3671+ else {
3672+ ii_write_lock_child(i);
3673+ err = au_refresh_hinode_self(i);
3674+ au_iigen_dec(i);
3675+ if (!err)
3676+ ii_downgrade_lock(i);
3677+ else {
3678+ ii_write_unlock(i);
3679+ break;
3680+ }
3681+ }
3682+
3683+ btop = au_ibtop(i);
3684+ bbot = au_ibbot(i);
3685+ if (btop <= bindex
3686+ && bindex <= bbot
3687+ && au_h_iptr(i, bindex)
3688+ && au_test_ibusy(i, btop, bbot)) {
3689+ err = -EBUSY;
3690+ AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3691+ AuDbgInode(i);
3692+ }
3693+ ii_read_unlock(i);
3694+ }
3695+ au_iarray_free(array, max);
3696+
3697+out:
3698+ return err;
3699+}
3700+
3701+static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3702+ const unsigned int verbose)
3703+{
3704+ int err;
3705+ unsigned int sigen;
3706+
3707+ sigen = au_sigen(root->d_sb);
3708+ DiMustNoWaiters(root);
3709+ IiMustNoWaiters(d_inode(root));
3710+ di_write_unlock(root);
3711+ err = test_dentry_busy(root, bindex, sigen, verbose);
3712+ if (!err)
3713+ err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3714+ di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3715+
3716+ return err;
3717+}
3718+
3719+static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3720+ struct file **to_free, int *idx)
3721+{
3722+ int err;
3723+ unsigned char matched, root;
3724+ aufs_bindex_t bindex, bbot;
3725+ struct au_fidir *fidir;
3726+ struct au_hfile *hfile;
3727+
3728+ err = 0;
3729+ root = IS_ROOT(file->f_path.dentry);
3730+ if (root) {
3731+ get_file(file);
3732+ to_free[*idx] = file;
3733+ (*idx)++;
3734+ goto out;
3735+ }
3736+
3737+ matched = 0;
3738+ fidir = au_fi(file)->fi_hdir;
3739+ AuDebugOn(!fidir);
3740+ bbot = au_fbbot_dir(file);
3741+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3742+ hfile = fidir->fd_hfile + bindex;
3743+ if (!hfile->hf_file)
3744+ continue;
3745+
3746+ if (hfile->hf_br->br_id == br_id) {
3747+ matched = 1;
3748+ break;
3749+ }
3750+ }
3751+ if (matched)
3752+ err = -EBUSY;
3753+
3754+out:
3755+ return err;
3756+}
3757+
3758+static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3759+ struct file **to_free, int opened)
3760+{
3761+ int err, idx;
3762+ unsigned long long ull, max;
3763+ aufs_bindex_t btop;
3764+ struct file *file, **array;
3765+ struct dentry *root;
3766+ struct au_hfile *hfile;
3767+
3768+ array = au_farray_alloc(sb, &max);
3769+ err = PTR_ERR(array);
3770+ if (IS_ERR(array))
3771+ goto out;
3772+
3773+ err = 0;
3774+ idx = 0;
3775+ root = sb->s_root;
3776+ di_write_unlock(root);
3777+ for (ull = 0; ull < max; ull++) {
3778+ file = array[ull];
3779+ if (unlikely(!file))
3780+ break;
3781+
3782+ /* AuDbg("%pD\n", file); */
3783+ fi_read_lock(file);
3784+ btop = au_fbtop(file);
3785+ if (!d_is_dir(file->f_path.dentry)) {
3786+ hfile = &au_fi(file)->fi_htop;
3787+ if (hfile->hf_br->br_id == br_id)
3788+ err = -EBUSY;
3789+ } else
3790+ err = test_dir_busy(file, br_id, to_free, &idx);
3791+ fi_read_unlock(file);
3792+ if (unlikely(err))
3793+ break;
3794+ }
3795+ di_write_lock_child(root);
3796+ au_farray_free(array, max);
3797+ AuDebugOn(idx > opened);
3798+
3799+out:
3800+ return err;
3801+}
3802+
3803+static void br_del_file(struct file **to_free, unsigned long long opened,
3804+ aufs_bindex_t br_id)
3805+{
3806+ unsigned long long ull;
3807+ aufs_bindex_t bindex, btop, bbot, bfound;
3808+ struct file *file;
3809+ struct au_fidir *fidir;
3810+ struct au_hfile *hfile;
3811+
3812+ for (ull = 0; ull < opened; ull++) {
3813+ file = to_free[ull];
3814+ if (unlikely(!file))
3815+ break;
3816+
3817+ /* AuDbg("%pD\n", file); */
3818+ AuDebugOn(!d_is_dir(file->f_path.dentry));
3819+ bfound = -1;
3820+ fidir = au_fi(file)->fi_hdir;
3821+ AuDebugOn(!fidir);
3822+ fi_write_lock(file);
3823+ btop = au_fbtop(file);
3824+ bbot = au_fbbot_dir(file);
3825+ for (bindex = btop; bindex <= bbot; bindex++) {
3826+ hfile = fidir->fd_hfile + bindex;
3827+ if (!hfile->hf_file)
3828+ continue;
3829+
3830+ if (hfile->hf_br->br_id == br_id) {
3831+ bfound = bindex;
3832+ break;
3833+ }
3834+ }
3835+ AuDebugOn(bfound < 0);
3836+ au_set_h_fptr(file, bfound, NULL);
3837+ if (bfound == btop) {
3838+ for (btop++; btop <= bbot; btop++)
3839+ if (au_hf_dir(file, btop)) {
3840+ au_set_fbtop(file, btop);
3841+ break;
3842+ }
3843+ }
3844+ fi_write_unlock(file);
3845+ }
3846+}
3847+
3848+static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3849+ const aufs_bindex_t bindex,
3850+ const aufs_bindex_t bbot)
3851+{
3852+ struct au_branch **brp, **p;
3853+
3854+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3855+
3856+ brp = sbinfo->si_branch + bindex;
3857+ if (bindex < bbot)
3858+ memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3859+ sbinfo->si_branch[0 + bbot] = NULL;
3860+ sbinfo->si_bbot--;
3861+
3862+ p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3863+ /*may_shrink*/1);
3864+ if (p)
3865+ sbinfo->si_branch = p;
3866+ /* harmless error */
3867+}
3868+
3869+static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3870+ const aufs_bindex_t bbot)
3871+{
3872+ struct au_hdentry *hdp, *p;
3873+
3874+ AuRwMustWriteLock(&dinfo->di_rwsem);
3875+
3876+ hdp = au_hdentry(dinfo, bindex);
3877+ if (bindex < bbot)
3878+ memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3879+ /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3880+ dinfo->di_bbot--;
3881+
3882+ p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3883+ /*may_shrink*/1);
3884+ if (p)
3885+ dinfo->di_hdentry = p;
3886+ /* harmless error */
3887+}
3888+
3889+static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3890+ const aufs_bindex_t bbot)
3891+{
3892+ struct au_hinode *hip, *p;
3893+
3894+ AuRwMustWriteLock(&iinfo->ii_rwsem);
3895+
3896+ hip = au_hinode(iinfo, bindex);
3897+ if (bindex < bbot)
3898+ memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3899+ /* au_hinode_init(au_hinode(iinfo, bbot)); */
3900+ iinfo->ii_bbot--;
3901+
3902+ p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3903+ /*may_shrink*/1);
3904+ if (p)
3905+ iinfo->ii_hinode = p;
3906+ /* harmless error */
3907+}
3908+
3909+static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3910+ struct au_branch *br)
3911+{
3912+ aufs_bindex_t bbot;
3913+ struct au_sbinfo *sbinfo;
3914+ struct dentry *root, *h_root;
3915+ struct inode *inode, *h_inode;
3916+ struct au_hinode *hinode;
3917+
3918+ SiMustWriteLock(sb);
3919+
3920+ root = sb->s_root;
3921+ inode = d_inode(root);
3922+ sbinfo = au_sbi(sb);
3923+ bbot = sbinfo->si_bbot;
3924+
3925+ h_root = au_h_dptr(root, bindex);
3926+ hinode = au_hi(inode, bindex);
3927+ h_inode = au_igrab(hinode->hi_inode);
3928+ au_hiput(hinode);
3929+
3930+ au_sbilist_lock();
3931+ au_br_do_del_brp(sbinfo, bindex, bbot);
3932+ au_br_do_del_hdp(au_di(root), bindex, bbot);
3933+ au_br_do_del_hip(au_ii(inode), bindex, bbot);
3934+ au_sbilist_unlock();
3935+
3936+ /* ignore an error */
3937+ au_dr_br_fin(sb, br); /* always, regardless the mount option */
3938+
3939+ dput(h_root);
3940+ iput(h_inode);
3941+ au_br_do_free(br);
3942+}
3943+
3944+static unsigned long long empty_cb(struct super_block *sb, void *array,
3945+ unsigned long long max, void *arg)
3946+{
3947+ return max;
3948+}
3949+
3950+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3951+{
3952+ int err, rerr, i;
3953+ unsigned long long opened;
3954+ unsigned int mnt_flags;
3955+ aufs_bindex_t bindex, bbot, br_id;
3956+ unsigned char do_wh, verbose;
3957+ struct au_branch *br;
3958+ struct au_wbr *wbr;
3959+ struct dentry *root;
3960+ struct file **to_free;
3961+
3962+ err = 0;
3963+ opened = 0;
3964+ to_free = NULL;
3965+ root = sb->s_root;
3966+ bindex = au_find_dbindex(root, del->h_path.dentry);
3967+ if (bindex < 0) {
3968+ if (remount)
3969+ goto out; /* success */
3970+ err = -ENOENT;
3971+ pr_err("%s no such branch\n", del->pathname);
3972+ goto out;
3973+ }
3974+ AuDbg("bindex b%d\n", bindex);
3975+
3976+ err = -EBUSY;
3977+ mnt_flags = au_mntflags(sb);
3978+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
3979+ bbot = au_sbbot(sb);
3980+ if (unlikely(!bbot)) {
3981+ AuVerbose(verbose, "no more branches left\n");
3982+ goto out;
3983+ }
3984+
3985+ br = au_sbr(sb, bindex);
3986+ AuDebugOn(!path_equal(&br->br_path, &del->h_path));
3987+ if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
3988+ AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
3989+ goto out;
3990+ }
3991+
3992+ br_id = br->br_id;
3993+ opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
3994+ if (unlikely(opened)) {
3995+ to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
3996+ err = PTR_ERR(to_free);
3997+ if (IS_ERR(to_free))
3998+ goto out;
3999+
4000+ err = test_file_busy(sb, br_id, to_free, opened);
4001+ if (unlikely(err)) {
4002+ AuVerbose(verbose, "%llu file(s) opened\n", opened);
4003+ goto out;
4004+ }
4005+ }
4006+
4007+ wbr = br->br_wbr;
4008+ do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
4009+ if (do_wh) {
4010+ /* instead of WbrWhMustWriteLock(wbr) */
4011+ SiMustWriteLock(sb);
4012+ for (i = 0; i < AuBrWh_Last; i++) {
4013+ dput(wbr->wbr_wh[i]);
4014+ wbr->wbr_wh[i] = NULL;
4015+ }
4016+ }
4017+
4018+ err = test_children_busy(root, bindex, verbose);
4019+ if (unlikely(err)) {
4020+ if (do_wh)
4021+ goto out_wh;
4022+ goto out;
4023+ }
4024+
4025+ err = 0;
4026+ if (to_free) {
4027+ /*
4028+ * now we confirmed the branch is deletable.
4029+ * let's free the remaining opened dirs on the branch.
4030+ */
4031+ di_write_unlock(root);
4032+ br_del_file(to_free, opened, br_id);
4033+ di_write_lock_child(root);
4034+ }
4035+
4036+ sysaufs_brs_del(sb, bindex); /* remove successors */
4037+ dbgaufs_xino_del(br); /* remove one */
4038+ au_br_do_del(sb, bindex, br);
4039+ sysaufs_brs_add(sb, bindex); /* append successors */
4040+ dbgaufs_brs_add(sb, bindex, /*topdown*/1); /* rename successors */
4041+
4042+ if (!bindex) {
4043+ au_cpup_attr_all(d_inode(root), /*force*/1);
4044+ sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
4045+ } else
4046+ au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
4047+ if (au_opt_test(mnt_flags, PLINK))
4048+ au_plink_half_refresh(sb, br_id);
4049+
4050+ goto out; /* success */
4051+
4052+out_wh:
4053+ /* revert */
4054+ rerr = au_br_init_wh(sb, br, br->br_perm);
4055+ if (rerr)
4056+ pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4057+ del->pathname, rerr);
4058+out:
4059+ if (to_free)
4060+ au_farray_free(to_free, opened);
4061+ return err;
4062+}
4063+
4064+/* ---------------------------------------------------------------------- */
4065+
4066+static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4067+{
4068+ int err;
4069+ aufs_bindex_t btop, bbot;
4070+ struct aufs_ibusy ibusy;
4071+ struct inode *inode, *h_inode;
4072+
4073+ err = -EPERM;
4074+ if (unlikely(!capable(CAP_SYS_ADMIN)))
4075+ goto out;
4076+
4077+ err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4078+ if (!err)
4079+ /* VERIFY_WRITE */
4080+ err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
4081+ if (unlikely(err)) {
4082+ err = -EFAULT;
4083+ AuTraceErr(err);
4084+ goto out;
4085+ }
4086+
4087+ err = -EINVAL;
4088+ si_read_lock(sb, AuLock_FLUSH);
4089+ if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
4090+ goto out_unlock;
4091+
4092+ err = 0;
4093+ ibusy.h_ino = 0; /* invalid */
4094+ inode = ilookup(sb, ibusy.ino);
4095+ if (!inode
4096+ || inode->i_ino == AUFS_ROOT_INO
4097+ || au_is_bad_inode(inode))
4098+ goto out_unlock;
4099+
4100+ ii_read_lock_child(inode);
4101+ btop = au_ibtop(inode);
4102+ bbot = au_ibbot(inode);
4103+ if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
4104+ h_inode = au_h_iptr(inode, ibusy.bindex);
4105+ if (h_inode && au_test_ibusy(inode, btop, bbot))
4106+ ibusy.h_ino = h_inode->i_ino;
4107+ }
4108+ ii_read_unlock(inode);
4109+ iput(inode);
4110+
4111+out_unlock:
4112+ si_read_unlock(sb);
4113+ if (!err) {
4114+ err = __put_user(ibusy.h_ino, &arg->h_ino);
4115+ if (unlikely(err)) {
4116+ err = -EFAULT;
4117+ AuTraceErr(err);
4118+ }
4119+ }
4120+out:
4121+ return err;
4122+}
4123+
4124+long au_ibusy_ioctl(struct file *file, unsigned long arg)
4125+{
4126+ return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
4127+}
4128+
4129+#ifdef CONFIG_COMPAT
4130+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4131+{
4132+ return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
4133+}
4134+#endif
4135+
4136+/* ---------------------------------------------------------------------- */
4137+
4138+/*
4139+ * change a branch permission
4140+ */
4141+
4142+static void au_warn_ima(void)
4143+{
4144+#ifdef CONFIG_IMA
4145+ /* since it doesn't support mark_files_ro() */
4146+ AuWarn1("RW -> RO makes IMA to produce wrong message\n");
4147+#endif
4148+}
4149+
4150+static int do_need_sigen_inc(int a, int b)
4151+{
4152+ return au_br_whable(a) && !au_br_whable(b);
4153+}
4154+
4155+static int need_sigen_inc(int old, int new)
4156+{
4157+ return do_need_sigen_inc(old, new)
4158+ || do_need_sigen_inc(new, old);
4159+}
4160+
4161+static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4162+{
4163+ int err, do_warn;
4164+ unsigned int mnt_flags;
4165+ unsigned long long ull, max;
4166+ aufs_bindex_t br_id;
4167+ unsigned char verbose, writer;
4168+ struct file *file, *hf, **array;
4169+ struct au_hfile *hfile;
4170+ struct inode *h_inode;
4171+
4172+ mnt_flags = au_mntflags(sb);
4173+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
4174+
4175+ array = au_farray_alloc(sb, &max);
4176+ err = PTR_ERR(array);
4177+ if (IS_ERR(array))
4178+ goto out;
4179+
4180+ do_warn = 0;
4181+ br_id = au_sbr_id(sb, bindex);
4182+ for (ull = 0; ull < max; ull++) {
4183+ file = array[ull];
4184+ if (unlikely(!file))
4185+ break;
4186+
4187+ /* AuDbg("%pD\n", file); */
4188+ fi_read_lock(file);
4189+ if (unlikely(au_test_mmapped(file))) {
4190+ err = -EBUSY;
4191+ AuVerbose(verbose, "mmapped %pD\n", file);
4192+ AuDbgFile(file);
4193+ FiMustNoWaiters(file);
4194+ fi_read_unlock(file);
4195+ goto out_array;
4196+ }
4197+
4198+ hfile = &au_fi(file)->fi_htop;
4199+ hf = hfile->hf_file;
4200+ if (!d_is_reg(file->f_path.dentry)
4201+ || !(file->f_mode & FMODE_WRITE)
4202+ || hfile->hf_br->br_id != br_id
4203+ || !(hf->f_mode & FMODE_WRITE))
4204+ array[ull] = NULL;
4205+ else {
4206+ do_warn = 1;
4207+ get_file(file);
4208+ }
4209+
4210+ FiMustNoWaiters(file);
4211+ fi_read_unlock(file);
4212+ fput(file);
4213+ }
4214+
4215+ err = 0;
4216+ if (do_warn)
4217+ au_warn_ima();
4218+
4219+ for (ull = 0; ull < max; ull++) {
4220+ file = array[ull];
4221+ if (!file)
4222+ continue;
4223+
4224+ /* todo: already flushed? */
4225+ /*
4226+ * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4227+ * approach which resets f_mode and calls mnt_drop_write() and
4228+ * file_release_write() for each file, because the branch
4229+ * attribute in aufs world is totally different from the native
4230+ * fs rw/ro mode.
4231+ */
4232+ /* fi_read_lock(file); */
4233+ hfile = &au_fi(file)->fi_htop;
4234+ hf = hfile->hf_file;
4235+ /* fi_read_unlock(file); */
4236+ spin_lock(&hf->f_lock);
4237+ writer = !!(hf->f_mode & FMODE_WRITER);
4238+ hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4239+ spin_unlock(&hf->f_lock);
4240+ if (writer) {
4241+ h_inode = file_inode(hf);
4242+ if (hf->f_mode & FMODE_READ)
4243+ i_readcount_inc(h_inode);
4244+ put_write_access(h_inode);
4245+ __mnt_drop_write(hf->f_path.mnt);
4246+ }
4247+ }
4248+
4249+out_array:
4250+ au_farray_free(array, max);
4251+out:
4252+ AuTraceErr(err);
4253+ return err;
4254+}
4255+
4256+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4257+ int *do_refresh)
4258+{
4259+ int err, rerr;
4260+ aufs_bindex_t bindex;
4261+ struct dentry *root;
4262+ struct au_branch *br;
4263+ struct au_br_fhsm *bf;
4264+
4265+ root = sb->s_root;
4266+ bindex = au_find_dbindex(root, mod->h_root);
4267+ if (bindex < 0) {
4268+ if (remount)
4269+ return 0; /* success */
4270+ err = -ENOENT;
4271+ pr_err("%s no such branch\n", mod->path);
4272+ goto out;
4273+ }
4274+ AuDbg("bindex b%d\n", bindex);
4275+
4276+ err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4277+ if (unlikely(err))
4278+ goto out;
4279+
4280+ br = au_sbr(sb, bindex);
4281+ AuDebugOn(mod->h_root != au_br_dentry(br));
4282+ if (br->br_perm == mod->perm)
4283+ return 0; /* success */
4284+
4285+ /* pre-allocate for non-fhsm --> fhsm */
4286+ bf = NULL;
4287+ if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4288+ err = au_fhsm_br_alloc(br);
4289+ if (unlikely(err))
4290+ goto out;
4291+ bf = br->br_fhsm;
4292+ br->br_fhsm = NULL;
4293+ }
4294+
4295+ if (au_br_writable(br->br_perm)) {
4296+ /* remove whiteout base */
4297+ err = au_br_init_wh(sb, br, mod->perm);
4298+ if (unlikely(err))
4299+ goto out_bf;
4300+
4301+ if (!au_br_writable(mod->perm)) {
4302+ /* rw --> ro, file might be mmapped */
4303+ DiMustNoWaiters(root);
4304+ IiMustNoWaiters(d_inode(root));
4305+ di_write_unlock(root);
4306+ err = au_br_mod_files_ro(sb, bindex);
4307+ /* aufs_write_lock() calls ..._child() */
4308+ di_write_lock_child(root);
4309+
4310+ if (unlikely(err)) {
4311+ rerr = -ENOMEM;
4312+ br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4313+ GFP_NOFS);
4314+ if (br->br_wbr)
4315+ rerr = au_wbr_init(br, sb, br->br_perm);
4316+ if (unlikely(rerr)) {
4317+ AuIOErr("nested error %d (%d)\n",
4318+ rerr, err);
4319+ br->br_perm = mod->perm;
4320+ }
4321+ }
4322+ }
4323+ } else if (au_br_writable(mod->perm)) {
4324+ /* ro --> rw */
4325+ err = -ENOMEM;
4326+ br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4327+ if (br->br_wbr) {
4328+ err = au_wbr_init(br, sb, mod->perm);
4329+ if (unlikely(err)) {
4330+ au_kfree_rcu(br->br_wbr);
4331+ br->br_wbr = NULL;
4332+ }
4333+ }
4334+ }
4335+ if (unlikely(err))
4336+ goto out_bf;
4337+
4338+ if (au_br_fhsm(br->br_perm)) {
4339+ if (!au_br_fhsm(mod->perm)) {
4340+ /* fhsm --> non-fhsm */
4341+ au_br_fhsm_fin(br->br_fhsm);
4342+ au_kfree_rcu(br->br_fhsm);
4343+ br->br_fhsm = NULL;
4344+ }
4345+ } else if (au_br_fhsm(mod->perm))
4346+ /* non-fhsm --> fhsm */
4347+ br->br_fhsm = bf;
4348+
4349+ *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4350+ br->br_perm = mod->perm;
4351+ goto out; /* success */
4352+
4353+out_bf:
4354+ au_kfree_try_rcu(bf);
4355+out:
4356+ AuTraceErr(err);
4357+ return err;
4358+}
4359+
4360+/* ---------------------------------------------------------------------- */
4361+
4362+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4363+{
4364+ int err;
4365+ struct kstatfs kstfs;
4366+
4367+ err = vfs_statfs(&br->br_path, &kstfs);
4368+ if (!err) {
4369+ stfs->f_blocks = kstfs.f_blocks;
4370+ stfs->f_bavail = kstfs.f_bavail;
4371+ stfs->f_files = kstfs.f_files;
4372+ stfs->f_ffree = kstfs.f_ffree;
4373+ }
4374+
4375+ return err;
4376+}
4377diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4378--- /usr/share/empty/fs/aufs/branch.h 1970-01-01 01:00:00.000000000 +0100
4379+++ linux/fs/aufs/branch.h 2021-02-22 22:51:59.058078584 +0100
4380@@ -0,0 +1,364 @@
4381+/* SPDX-License-Identifier: GPL-2.0 */
4382+/*
4383+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4384+ *
4385+ * This program, aufs is free software; you can redistribute it and/or modify
4386+ * it under the terms of the GNU General Public License as published by
4387+ * the Free Software Foundation; either version 2 of the License, or
4388+ * (at your option) any later version.
4389+ *
4390+ * This program is distributed in the hope that it will be useful,
4391+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4392+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4393+ * GNU General Public License for more details.
4394+ *
4395+ * You should have received a copy of the GNU General Public License
4396+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4397+ */
4398+
4399+/*
4400+ * branch filesystems and xino for them
4401+ */
4402+
4403+#ifndef __AUFS_BRANCH_H__
4404+#define __AUFS_BRANCH_H__
4405+
4406+#ifdef __KERNEL__
4407+
4408+#include <linux/mount.h>
4409+#include "dirren.h"
4410+#include "dynop.h"
4411+#include "lcnt.h"
4412+#include "rwsem.h"
4413+#include "super.h"
4414+
4415+/* ---------------------------------------------------------------------- */
4416+
4417+/* a xino file */
4418+struct au_xino {
4419+ struct file **xi_file;
4420+ unsigned int xi_nfile;
4421+
4422+ struct {
4423+ spinlock_t spin;
4424+ ino_t *array;
4425+ int total;
4426+ /* reserved for future use */
4427+ /* unsigned long *bitmap; */
4428+ wait_queue_head_t wqh;
4429+ } xi_nondir;
4430+
4431+ struct mutex xi_mtx; /* protects xi_file array */
4432+ struct hlist_bl_head xi_writing;
4433+
4434+ atomic_t xi_truncating;
4435+
4436+ struct kref xi_kref;
4437+};
4438+
4439+/* File-based Hierarchical Storage Management */
4440+struct au_br_fhsm {
4441+#ifdef CONFIG_AUFS_FHSM
4442+ struct mutex bf_lock;
4443+ unsigned long bf_jiffy;
4444+ struct aufs_stfs bf_stfs;
4445+ int bf_readable;
4446+#endif
4447+};
4448+
4449+/* members for writable branch only */
4450+enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4451+struct au_wbr {
4452+ struct au_rwsem wbr_wh_rwsem;
4453+ struct dentry *wbr_wh[AuBrWh_Last];
4454+ atomic_t wbr_wh_running;
4455+#define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
4456+#define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
4457+#define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
4458+
4459+ /* mfs mode */
4460+ unsigned long long wbr_bytes;
4461+};
4462+
4463+/* ext2 has 3 types of operations at least, ext3 has 4 */
4464+#define AuBrDynOp (AuDyLast * 4)
4465+
4466+#ifdef CONFIG_AUFS_HFSNOTIFY
4467+/* support for asynchronous destruction */
4468+struct au_br_hfsnotify {
4469+ struct fsnotify_group *hfsn_group;
4470+};
4471+#endif
4472+
4473+/* sysfs entries */
4474+struct au_brsysfs {
4475+ char name[16];
4476+ struct attribute attr;
4477+};
4478+
4479+enum {
4480+ AuBrSysfs_BR,
4481+ AuBrSysfs_BRID,
4482+ AuBrSysfs_Last
4483+};
4484+
4485+/* protected by superblock rwsem */
4486+struct au_branch {
4487+ struct au_xino *br_xino;
4488+
4489+ aufs_bindex_t br_id;
4490+
4491+ int br_perm;
4492+ struct path br_path;
4493+ spinlock_t br_dykey_lock;
4494+ struct au_dykey *br_dykey[AuBrDynOp];
4495+ au_lcnt_t br_nfiles; /* opened files */
4496+ au_lcnt_t br_count; /* in-use for other */
4497+
4498+ struct au_wbr *br_wbr;
4499+ struct au_br_fhsm *br_fhsm;
4500+
4501+#ifdef CONFIG_AUFS_HFSNOTIFY
4502+ struct au_br_hfsnotify *br_hfsn;
4503+#endif
4504+
4505+#ifdef CONFIG_SYSFS
4506+ /* entries under sysfs per mount-point */
4507+ struct au_brsysfs br_sysfs[AuBrSysfs_Last];
4508+#endif
4509+
4510+#ifdef CONFIG_DEBUG_FS
4511+ struct dentry *br_dbgaufs; /* xino */
4512+#endif
4513+
4514+ struct au_dr_br br_dirren;
4515+};
4516+
4517+/* ---------------------------------------------------------------------- */
4518+
4519+static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4520+{
4521+ return br->br_path.mnt;
4522+}
4523+
4524+static inline struct dentry *au_br_dentry(struct au_branch *br)
4525+{
4526+ return br->br_path.dentry;
4527+}
4528+
4529+static inline struct super_block *au_br_sb(struct au_branch *br)
4530+{
4531+ return au_br_mnt(br)->mnt_sb;
4532+}
4533+
4534+static inline int au_br_rdonly(struct au_branch *br)
4535+{
4536+ return (sb_rdonly(au_br_sb(br))
4537+ || !au_br_writable(br->br_perm))
4538+ ? -EROFS : 0;
4539+}
4540+
4541+static inline int au_br_hnotifyable(int brperm __maybe_unused)
4542+{
4543+#ifdef CONFIG_AUFS_HNOTIFY
4544+ return !(brperm & AuBrPerm_RR);
4545+#else
4546+ return 0;
4547+#endif
4548+}
4549+
4550+static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4551+{
4552+ int err, exec_flag;
4553+
4554+ err = 0;
4555+ exec_flag = oflag & __FMODE_EXEC;
4556+ if (unlikely(exec_flag && path_noexec(&br->br_path)))
4557+ err = -EACCES;
4558+
4559+ return err;
4560+}
4561+
4562+static inline void au_xino_get(struct au_branch *br)
4563+{
4564+ struct au_xino *xi;
4565+
4566+ xi = br->br_xino;
4567+ if (xi)
4568+ kref_get(&xi->xi_kref);
4569+}
4570+
4571+static inline int au_xino_count(struct au_branch *br)
4572+{
4573+ int v;
4574+ struct au_xino *xi;
4575+
4576+ v = 0;
4577+ xi = br->br_xino;
4578+ if (xi)
4579+ v = kref_read(&xi->xi_kref);
4580+
4581+ return v;
4582+}
4583+
4584+/* ---------------------------------------------------------------------- */
4585+
4586+/* branch.c */
4587+struct au_sbinfo;
4588+void au_br_free(struct au_sbinfo *sinfo);
4589+int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4590+struct au_opt_add;
4591+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4592+struct au_opt_del;
4593+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4594+long au_ibusy_ioctl(struct file *file, unsigned long arg);
4595+#ifdef CONFIG_COMPAT
4596+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4597+#endif
4598+struct au_opt_mod;
4599+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4600+ int *do_refresh);
4601+struct aufs_stfs;
4602+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4603+
4604+/* xino.c */
4605+static const loff_t au_loff_max = LLONG_MAX;
4606+
4607+aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
4608+struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4609+ int wbrtop);
4610+struct file *au_xino_create2(struct super_block *sb, struct path *base,
4611+ struct file *copy_src);
4612+struct au_xi_new {
4613+ struct au_xino *xi; /* switch between xino and xigen */
4614+ int idx;
4615+ struct path *base;
4616+ struct file *copy_src;
4617+};
4618+struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
4619+
4620+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4621+ ino_t *ino);
4622+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4623+ ino_t ino);
4624+ssize_t xino_fread(struct file *file, void *buf, size_t size, loff_t *pos);
4625+ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos);
4626+
4627+int au_xib_trunc(struct super_block *sb);
4628+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
4629+
4630+struct au_xino *au_xino_alloc(unsigned int nfile);
4631+int au_xino_put(struct au_branch *br);
4632+struct file *au_xino_file1(struct au_xino *xi);
4633+
4634+struct au_opt_xino;
4635+void au_xino_clr(struct super_block *sb);
4636+int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
4637+struct file *au_xino_def(struct super_block *sb);
4638+int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4639+ struct path *base);
4640+
4641+ino_t au_xino_new_ino(struct super_block *sb);
4642+void au_xino_delete_inode(struct inode *inode, const int unlinked);
4643+
4644+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4645+ ino_t h_ino, int idx);
4646+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4647+ int *idx);
4648+
4649+int au_xino_path(struct seq_file *seq, struct file *file);
4650+
4651+/* ---------------------------------------------------------------------- */
4652+
4653+/* @idx is signed to accept -1 meaning the first file */
4654+static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4655+{
4656+ struct file *file;
4657+
4658+ file = NULL;
4659+ if (!xi)
4660+ goto out;
4661+
4662+ if (idx >= 0) {
4663+ if (idx < xi->xi_nfile)
4664+ file = xi->xi_file[idx];
4665+ } else
4666+ file = au_xino_file1(xi);
4667+
4668+out:
4669+ return file;
4670+}
4671+
4672+/* ---------------------------------------------------------------------- */
4673+
4674+/* Superblock to branch */
4675+static inline
4676+aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4677+{
4678+ return au_sbr(sb, bindex)->br_id;
4679+}
4680+
4681+static inline
4682+struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4683+{
4684+ return au_br_mnt(au_sbr(sb, bindex));
4685+}
4686+
4687+static inline
4688+struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4689+{
4690+ return au_br_sb(au_sbr(sb, bindex));
4691+}
4692+
4693+static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4694+{
4695+ return au_sbr(sb, bindex)->br_perm;
4696+}
4697+
4698+static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4699+{
4700+ return au_br_whable(au_sbr_perm(sb, bindex));
4701+}
4702+
4703+/* ---------------------------------------------------------------------- */
4704+
4705+#define wbr_wh_read_lock(wbr) au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4706+#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4707+#define wbr_wh_read_trylock(wbr) au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4708+#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
4709+/*
4710+#define wbr_wh_read_trylock_nested(wbr) \
4711+ au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4712+#define wbr_wh_write_trylock_nested(wbr) \
4713+ au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4714+*/
4715+
4716+#define wbr_wh_read_unlock(wbr) au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4717+#define wbr_wh_write_unlock(wbr) au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4718+#define wbr_wh_downgrade_lock(wbr) au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4719+
4720+#define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4721+#define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4722+#define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
4723+
4724+/* ---------------------------------------------------------------------- */
4725+
4726+#ifdef CONFIG_AUFS_FHSM
4727+static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4728+{
4729+ mutex_init(&brfhsm->bf_lock);
4730+ brfhsm->bf_jiffy = 0;
4731+ brfhsm->bf_readable = 0;
4732+}
4733+
4734+static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4735+{
4736+ mutex_destroy(&brfhsm->bf_lock);
4737+}
4738+#else
4739+AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4740+AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4741+#endif
4742+
4743+#endif /* __KERNEL__ */
4744+#endif /* __AUFS_BRANCH_H__ */
4745diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4746--- /usr/share/empty/fs/aufs/conf.mk 1970-01-01 01:00:00.000000000 +0100
4747+++ linux/fs/aufs/conf.mk 2021-02-22 22:51:59.058078584 +0100
4748@@ -0,0 +1,40 @@
4749+# SPDX-License-Identifier: GPL-2.0
4750+
4751+AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4752+
4753+define AuConf
4754+ifdef ${1}
4755+AuConfStr += ${1}=${${1}}
4756+endif
4757+endef
4758+
4759+AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4760+ SBILIST \
4761+ HNOTIFY HFSNOTIFY \
4762+ EXPORT INO_T_64 \
4763+ XATTR \
4764+ FHSM \
4765+ RDU \
4766+ DIRREN \
4767+ SHWH \
4768+ BR_RAMFS \
4769+ BR_FUSE POLL \
4770+ BR_HFSPLUS \
4771+ BDEV_LOOP \
4772+ DEBUG MAGIC_SYSRQ
4773+$(foreach i, ${AuConfAll}, \
4774+ $(eval $(call AuConf,CONFIG_AUFS_${i})))
4775+
4776+AuConfName = ${obj}/conf.str
4777+${AuConfName}.tmp: FORCE
4778+ @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4779+${AuConfName}: ${AuConfName}.tmp
4780+ @diff -q $< $@ > /dev/null 2>&1 || { \
4781+ echo ' GEN ' $@; \
4782+ cp -p $< $@; \
4783+ }
4784+FORCE:
4785+clean-files += ${AuConfName} ${AuConfName}.tmp
4786+${obj}/sysfs.o: ${AuConfName}
4787+
4788+-include ${srctree}/${src}/conf_priv.mk
4789diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4790--- /usr/share/empty/fs/aufs/cpup.c 1970-01-01 01:00:00.000000000 +0100
4791+++ linux/fs/aufs/cpup.c 2021-02-22 22:51:59.058078584 +0100
4792@@ -0,0 +1,1445 @@
4793+// SPDX-License-Identifier: GPL-2.0
4794+/*
4795+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4796+ *
4797+ * This program, aufs is free software; you can redistribute it and/or modify
4798+ * it under the terms of the GNU General Public License as published by
4799+ * the Free Software Foundation; either version 2 of the License, or
4800+ * (at your option) any later version.
4801+ *
4802+ * This program is distributed in the hope that it will be useful,
4803+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4804+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4805+ * GNU General Public License for more details.
4806+ *
4807+ * You should have received a copy of the GNU General Public License
4808+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4809+ */
4810+
4811+/*
4812+ * copy-up functions, see wbr_policy.c for copy-down
4813+ */
4814+
4815+#include <linux/fs_stack.h>
4816+#include <linux/mm.h>
4817+#include <linux/task_work.h>
4818+#include "aufs.h"
4819+
4820+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4821+{
4822+ const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4823+ | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4824+
4825+ BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4826+
4827+ dst->i_flags |= iflags & ~mask;
4828+ if (au_test_fs_notime(dst->i_sb))
4829+ dst->i_flags |= S_NOATIME | S_NOCMTIME;
4830+}
4831+
4832+void au_cpup_attr_timesizes(struct inode *inode)
4833+{
4834+ struct inode *h_inode;
4835+
4836+ h_inode = au_h_iptr(inode, au_ibtop(inode));
4837+ fsstack_copy_attr_times(inode, h_inode);
4838+ fsstack_copy_inode_size(inode, h_inode);
4839+}
4840+
4841+void au_cpup_attr_nlink(struct inode *inode, int force)
4842+{
4843+ struct inode *h_inode;
4844+ struct super_block *sb;
4845+ aufs_bindex_t bindex, bbot;
4846+
4847+ sb = inode->i_sb;
4848+ bindex = au_ibtop(inode);
4849+ h_inode = au_h_iptr(inode, bindex);
4850+ if (!force
4851+ && !S_ISDIR(h_inode->i_mode)
4852+ && au_opt_test(au_mntflags(sb), PLINK)
4853+ && au_plink_test(inode))
4854+ return;
4855+
4856+ /*
4857+ * 0 can happen in revalidating.
4858+ * h_inode->i_mutex may not be held here, but it is harmless since once
4859+ * i_nlink reaches 0, it will never become positive except O_TMPFILE
4860+ * case.
4861+ * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4862+ * the incorrect link count.
4863+ */
4864+ set_nlink(inode, h_inode->i_nlink);
4865+
4866+ /*
4867+ * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4868+ * it may includes whplink directory.
4869+ */
4870+ if (S_ISDIR(h_inode->i_mode)) {
4871+ bbot = au_ibbot(inode);
4872+ for (bindex++; bindex <= bbot; bindex++) {
4873+ h_inode = au_h_iptr(inode, bindex);
4874+ if (h_inode)
4875+ au_add_nlink(inode, h_inode);
4876+ }
4877+ }
4878+}
4879+
4880+void au_cpup_attr_changeable(struct inode *inode)
4881+{
4882+ struct inode *h_inode;
4883+
4884+ h_inode = au_h_iptr(inode, au_ibtop(inode));
4885+ inode->i_mode = h_inode->i_mode;
4886+ inode->i_uid = h_inode->i_uid;
4887+ inode->i_gid = h_inode->i_gid;
4888+ au_cpup_attr_timesizes(inode);
4889+ au_cpup_attr_flags(inode, h_inode->i_flags);
4890+}
4891+
4892+void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4893+{
4894+ struct au_iinfo *iinfo = au_ii(inode);
4895+
4896+ IiMustWriteLock(inode);
4897+
4898+ iinfo->ii_higen = h_inode->i_generation;
4899+ iinfo->ii_hsb1 = h_inode->i_sb;
4900+}
4901+
4902+void au_cpup_attr_all(struct inode *inode, int force)
4903+{
4904+ struct inode *h_inode;
4905+
4906+ h_inode = au_h_iptr(inode, au_ibtop(inode));
4907+ au_cpup_attr_changeable(inode);
4908+ if (inode->i_nlink > 0)
4909+ au_cpup_attr_nlink(inode, force);
4910+ inode->i_rdev = h_inode->i_rdev;
4911+ inode->i_blkbits = h_inode->i_blkbits;
4912+ au_cpup_igen(inode, h_inode);
4913+}
4914+
4915+/* ---------------------------------------------------------------------- */
4916+
4917+/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4918+
4919+/* keep the timestamps of the parent dir when cpup */
4920+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4921+ struct path *h_path)
4922+{
4923+ struct inode *h_inode;
4924+
4925+ dt->dt_dentry = dentry;
4926+ dt->dt_h_path = *h_path;
4927+ h_inode = d_inode(h_path->dentry);
4928+ dt->dt_atime = h_inode->i_atime;
4929+ dt->dt_mtime = h_inode->i_mtime;
4930+ /* smp_mb(); */
4931+}
4932+
4933+void au_dtime_revert(struct au_dtime *dt)
4934+{
4935+ struct iattr attr;
4936+ int err;
4937+
4938+ attr.ia_atime = dt->dt_atime;
4939+ attr.ia_mtime = dt->dt_mtime;
4940+ attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4941+ | ATTR_ATIME | ATTR_ATIME_SET;
4942+
4943+ /* no delegation since this is a directory */
4944+ err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
4945+ if (unlikely(err))
4946+ pr_warn("restoring timestamps failed(%d). ignored\n", err);
4947+}
4948+
4949+/* ---------------------------------------------------------------------- */
4950+
4951+/* internal use only */
4952+struct au_cpup_reg_attr {
4953+ int valid;
4954+ struct kstat st;
4955+ unsigned int iflags; /* inode->i_flags */
4956+};
4957+
4958+static noinline_for_stack
4959+int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
4960+ struct au_cpup_reg_attr *h_src_attr)
4961+{
4962+ int err, sbits, icex;
4963+ unsigned int mnt_flags;
4964+ unsigned char verbose;
4965+ struct iattr ia;
4966+ struct path h_path;
4967+ struct inode *h_isrc, *h_idst;
4968+ struct kstat *h_st;
4969+ struct au_branch *br;
4970+
4971+ h_path.dentry = au_h_dptr(dst, bindex);
4972+ h_idst = d_inode(h_path.dentry);
4973+ br = au_sbr(dst->d_sb, bindex);
4974+ h_path.mnt = au_br_mnt(br);
4975+ h_isrc = d_inode(h_src);
4976+ ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
4977+ | ATTR_ATIME | ATTR_MTIME
4978+ | ATTR_ATIME_SET | ATTR_MTIME_SET;
4979+ if (h_src_attr && h_src_attr->valid) {
4980+ h_st = &h_src_attr->st;
4981+ ia.ia_uid = h_st->uid;
4982+ ia.ia_gid = h_st->gid;
4983+ ia.ia_atime = h_st->atime;
4984+ ia.ia_mtime = h_st->mtime;
4985+ if (h_idst->i_mode != h_st->mode
4986+ && !S_ISLNK(h_idst->i_mode)) {
4987+ ia.ia_valid |= ATTR_MODE;
4988+ ia.ia_mode = h_st->mode;
4989+ }
4990+ sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4991+ au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4992+ } else {
4993+ ia.ia_uid = h_isrc->i_uid;
4994+ ia.ia_gid = h_isrc->i_gid;
4995+ ia.ia_atime = h_isrc->i_atime;
4996+ ia.ia_mtime = h_isrc->i_mtime;
4997+ if (h_idst->i_mode != h_isrc->i_mode
4998+ && !S_ISLNK(h_idst->i_mode)) {
4999+ ia.ia_valid |= ATTR_MODE;
5000+ ia.ia_mode = h_isrc->i_mode;
5001+ }
5002+ sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
5003+ au_cpup_attr_flags(h_idst, h_isrc->i_flags);
5004+ }
5005+ /* no delegation since it is just created */
5006+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
5007+
5008+ /* is this nfs only? */
5009+ if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
5010+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
5011+ ia.ia_mode = h_isrc->i_mode;
5012+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
5013+ }
5014+
5015+ icex = br->br_perm & AuBrAttr_ICEX;
5016+ if (!err) {
5017+ mnt_flags = au_mntflags(dst->d_sb);
5018+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
5019+ err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
5020+ }
5021+
5022+ return err;
5023+}
5024+
5025+/* ---------------------------------------------------------------------- */
5026+
5027+static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
5028+ char *buf, unsigned long blksize)
5029+{
5030+ int err;
5031+ size_t sz, rbytes, wbytes;
5032+ unsigned char all_zero;
5033+ char *p, *zp;
5034+ struct inode *h_inode;
5035+ /* reduce stack usage */
5036+ struct iattr *ia;
5037+
5038+ zp = page_address(ZERO_PAGE(0));
5039+ if (unlikely(!zp))
5040+ return -ENOMEM; /* possible? */
5041+
5042+ err = 0;
5043+ all_zero = 0;
5044+ while (len) {
5045+ AuDbg("len %lld\n", len);
5046+ sz = blksize;
5047+ if (len < blksize)
5048+ sz = len;
5049+
5050+ rbytes = 0;
5051+ /* todo: signal_pending? */
5052+ while (!rbytes || err == -EAGAIN || err == -EINTR) {
5053+ rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5054+ err = rbytes;
5055+ }
5056+ if (unlikely(err < 0))
5057+ break;
5058+
5059+ all_zero = 0;
5060+ if (len >= rbytes && rbytes == blksize)
5061+ all_zero = !memcmp(buf, zp, rbytes);
5062+ if (!all_zero) {
5063+ wbytes = rbytes;
5064+ p = buf;
5065+ while (wbytes) {
5066+ size_t b;
5067+
5068+ b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5069+ err = b;
5070+ /* todo: signal_pending? */
5071+ if (unlikely(err == -EAGAIN || err == -EINTR))
5072+ continue;
5073+ if (unlikely(err < 0))
5074+ break;
5075+ wbytes -= b;
5076+ p += b;
5077+ }
5078+ if (unlikely(err < 0))
5079+ break;
5080+ } else {
5081+ loff_t res;
5082+
5083+ AuLabel(hole);
5084+ res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5085+ err = res;
5086+ if (unlikely(res < 0))
5087+ break;
5088+ }
5089+ len -= rbytes;
5090+ err = 0;
5091+ }
5092+
5093+ /* the last block may be a hole */
5094+ if (!err && all_zero) {
5095+ AuLabel(last hole);
5096+
5097+ err = 1;
5098+ if (au_test_nfs(dst->f_path.dentry->d_sb)) {
5099+ /* nfs requires this step to make last hole */
5100+ /* is this only nfs? */
5101+ do {
5102+ /* todo: signal_pending? */
5103+ err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5104+ } while (err == -EAGAIN || err == -EINTR);
5105+ if (err == 1)
5106+ dst->f_pos--;
5107+ }
5108+
5109+ if (err == 1) {
5110+ ia = (void *)buf;
5111+ ia->ia_size = dst->f_pos;
5112+ ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5113+ ia->ia_file = dst;
5114+ h_inode = file_inode(dst);
5115+ inode_lock_nested(h_inode, AuLsc_I_CHILD2);
5116+ /* no delegation since it is just created */
5117+ err = vfsub_notify_change(&dst->f_path, ia,
5118+ /*delegated*/NULL);
5119+ inode_unlock(h_inode);
5120+ }
5121+ }
5122+
5123+ return err;
5124+}
5125+
5126+int au_copy_file(struct file *dst, struct file *src, loff_t len)
5127+{
5128+ int err;
5129+ unsigned long blksize;
5130+ unsigned char do_kfree;
5131+ char *buf;
5132+ struct super_block *h_sb;
5133+
5134+ err = -ENOMEM;
5135+ h_sb = file_inode(dst)->i_sb;
5136+ blksize = h_sb->s_blocksize;
5137+ if (!blksize || PAGE_SIZE < blksize)
5138+ blksize = PAGE_SIZE;
5139+ AuDbg("blksize %lu\n", blksize);
5140+ do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5141+ if (do_kfree)
5142+ buf = kmalloc(blksize, GFP_NOFS);
5143+ else
5144+ buf = (void *)__get_free_page(GFP_NOFS);
5145+ if (unlikely(!buf))
5146+ goto out;
5147+
5148+ if (len > (1 << 22))
5149+ AuDbg("copying a large file %lld\n", (long long)len);
5150+
5151+ src->f_pos = 0;
5152+ dst->f_pos = 0;
5153+ err = au_do_copy_file(dst, src, len, buf, blksize);
5154+ if (do_kfree) {
5155+ AuDebugOn(!au_kfree_do_sz_test(blksize));
5156+ au_kfree_do_rcu(buf);
5157+ } else
5158+ free_page((unsigned long)buf);
5159+
5160+out:
5161+ return err;
5162+}
5163+
5164+static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5165+{
5166+ int err;
5167+ struct super_block *h_src_sb;
5168+ struct inode *h_src_inode;
5169+
5170+ h_src_inode = file_inode(src);
5171+ h_src_sb = h_src_inode->i_sb;
5172+
5173+ /* XFS acquires inode_lock */
5174+ if (!au_test_xfs(h_src_sb))
5175+ err = au_copy_file(dst, src, len);
5176+ else {
5177+ inode_unlock_shared(h_src_inode);
5178+ err = au_copy_file(dst, src, len);
5179+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5180+ }
5181+
5182+ return err;
5183+}
5184+
5185+static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5186+{
5187+ int err;
5188+ loff_t lo;
5189+ struct super_block *h_src_sb;
5190+ struct inode *h_src_inode;
5191+
5192+ h_src_inode = file_inode(src);
5193+ h_src_sb = h_src_inode->i_sb;
5194+ if (h_src_sb != file_inode(dst)->i_sb
5195+ || !dst->f_op->remap_file_range) {
5196+ err = au_do_copy(dst, src, len);
5197+ goto out;
5198+ }
5199+
5200+ if (!au_test_nfs(h_src_sb)) {
5201+ inode_unlock_shared(h_src_inode);
5202+ lo = vfsub_clone_file_range(src, dst, len);
5203+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5204+ } else
5205+ lo = vfsub_clone_file_range(src, dst, len);
5206+ if (lo == len) {
5207+ err = 0;
5208+ goto out; /* success */
5209+ } else if (lo >= 0)
5210+ /* todo: possible? */
5211+ /* paritially succeeded */
5212+ AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5213+ else if (lo != -EOPNOTSUPP) {
5214+ /* older XFS has a condition in cloning */
5215+ err = lo;
5216+ goto out;
5217+ }
5218+
5219+ /* the backend fs on NFS may not support cloning */
5220+ err = au_do_copy(dst, src, len);
5221+
5222+out:
5223+ AuTraceErr(err);
5224+ return err;
5225+}
5226+
5227+/*
5228+ * to support a sparse file which is opened with O_APPEND,
5229+ * we need to close the file.
5230+ */
5231+static int au_cp_regular(struct au_cp_generic *cpg)
5232+{
5233+ int err, i;
5234+ enum { SRC, DST };
5235+ struct {
5236+ aufs_bindex_t bindex;
5237+ unsigned int flags;
5238+ struct dentry *dentry;
5239+ int force_wr;
5240+ struct file *file;
5241+ } *f, file[] = {
5242+ {
5243+ .bindex = cpg->bsrc,
5244+ .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
5245+ },
5246+ {
5247+ .bindex = cpg->bdst,
5248+ .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
5249+ .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
5250+ }
5251+ };
5252+ struct au_branch *br;
5253+ struct super_block *sb, *h_src_sb;
5254+ struct inode *h_src_inode;
5255+ struct task_struct *tsk = current;
5256+
5257+ /* bsrc branch can be ro/rw. */
5258+ sb = cpg->dentry->d_sb;
5259+ f = file;
5260+ for (i = 0; i < 2; i++, f++) {
5261+ f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5262+ f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
5263+ /*file*/NULL, f->force_wr);
5264+ if (IS_ERR(f->file)) {
5265+ err = PTR_ERR(f->file);
5266+ if (i == SRC)
5267+ goto out;
5268+ else
5269+ goto out_src;
5270+ }
5271+ }
5272+
5273+ /* try stopping to update while we copyup */
5274+ h_src_inode = d_inode(file[SRC].dentry);
5275+ h_src_sb = h_src_inode->i_sb;
5276+ if (!au_test_nfs(h_src_sb))
5277+ IMustLock(h_src_inode);
5278+ err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
5279+
5280+ /* i wonder if we had O_NO_DELAY_FPUT flag */
5281+ if (tsk->flags & PF_KTHREAD)
5282+ __fput_sync(file[DST].file);
5283+ else {
5284+ /* it happened actually */
5285+ fput(file[DST].file);
5286+ /*
5287+ * too bad.
5288+ * we have to call both since we don't know which place the file
5289+ * was added to.
5290+ */
5291+ task_work_run();
5292+ flush_delayed_fput();
5293+ }
5294+ br = au_sbr(sb, file[DST].bindex);
5295+ au_lcnt_dec(&br->br_nfiles);
5296+
5297+out_src:
5298+ fput(file[SRC].file);
5299+ br = au_sbr(sb, file[SRC].bindex);
5300+ au_lcnt_dec(&br->br_nfiles);
5301+out:
5302+ return err;
5303+}
5304+
5305+static int au_do_cpup_regular(struct au_cp_generic *cpg,
5306+ struct au_cpup_reg_attr *h_src_attr)
5307+{
5308+ int err, rerr;
5309+ loff_t l;
5310+ struct path h_path;
5311+ struct inode *h_src_inode, *h_dst_inode;
5312+
5313+ err = 0;
5314+ h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5315+ l = i_size_read(h_src_inode);
5316+ if (cpg->len == -1 || l < cpg->len)
5317+ cpg->len = l;
5318+ if (cpg->len) {
5319+ /* try stopping to update while we are referencing */
5320+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5321+ au_pin_hdir_unlock(cpg->pin);
5322+
5323+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5324+ h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5325+ h_src_attr->iflags = h_src_inode->i_flags;
5326+ if (!au_test_nfs(h_src_inode->i_sb))
5327+ err = vfsub_getattr(&h_path, &h_src_attr->st);
5328+ else {
5329+ inode_unlock_shared(h_src_inode);
5330+ err = vfsub_getattr(&h_path, &h_src_attr->st);
5331+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5332+ }
5333+ if (unlikely(err)) {
5334+ inode_unlock_shared(h_src_inode);
5335+ goto out;
5336+ }
5337+ h_src_attr->valid = 1;
5338+ if (!au_test_nfs(h_src_inode->i_sb)) {
5339+ err = au_cp_regular(cpg);
5340+ inode_unlock_shared(h_src_inode);
5341+ } else {
5342+ inode_unlock_shared(h_src_inode);
5343+ err = au_cp_regular(cpg);
5344+ }
5345+ rerr = au_pin_hdir_relock(cpg->pin);
5346+ if (!err && rerr)
5347+ err = rerr;
5348+ }
5349+ if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5350+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5351+ h_dst_inode = d_inode(h_path.dentry);
5352+ spin_lock(&h_dst_inode->i_lock);
5353+ h_dst_inode->i_state |= I_LINKABLE;
5354+ spin_unlock(&h_dst_inode->i_lock);
5355+ }
5356+
5357+out:
5358+ return err;
5359+}
5360+
5361+static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5362+ struct inode *h_dir)
5363+{
5364+ int err;
5365+ DEFINE_DELAYED_CALL(done);
5366+ const char *sym;
5367+
5368+ sym = vfs_get_link(h_src, &done);
5369+ err = PTR_ERR(sym);
5370+ if (IS_ERR(sym))
5371+ goto out;
5372+
5373+ err = vfsub_symlink(h_dir, h_path, sym);
5374+
5375+out:
5376+ do_delayed_call(&done);
5377+ return err;
5378+}
5379+
5380+/*
5381+ * regardless 'acl' option, reset all ACL.
5382+ * All ACL will be copied up later from the original entry on the lower branch.
5383+ */
5384+static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5385+{
5386+ int err;
5387+ struct dentry *h_dentry;
5388+ struct inode *h_inode;
5389+
5390+ h_dentry = h_path->dentry;
5391+ h_inode = d_inode(h_dentry);
5392+ /* forget_all_cached_acls(h_inode)); */
5393+ err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5394+ AuTraceErr(err);
5395+ if (err == -EOPNOTSUPP)
5396+ err = 0;
5397+ if (!err)
5398+ err = vfsub_acl_chmod(h_inode, mode);
5399+
5400+ AuTraceErr(err);
5401+ return err;
5402+}
5403+
5404+static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5405+ struct inode *h_dir, struct path *h_path)
5406+{
5407+ int err;
5408+ struct inode *dir, *inode;
5409+
5410+ err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
5411+ AuTraceErr(err);
5412+ if (err == -EOPNOTSUPP)
5413+ err = 0;
5414+ if (unlikely(err))
5415+ goto out;
5416+
5417+ /*
5418+ * strange behaviour from the users view,
5419+ * particularly setattr case
5420+ */
5421+ dir = d_inode(dst_parent);
5422+ if (au_ibtop(dir) == cpg->bdst)
5423+ au_cpup_attr_nlink(dir, /*force*/1);
5424+ inode = d_inode(cpg->dentry);
5425+ au_cpup_attr_nlink(inode, /*force*/1);
5426+
5427+out:
5428+ return err;
5429+}
5430+
5431+static noinline_for_stack
5432+int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5433+ struct au_cpup_reg_attr *h_src_attr)
5434+{
5435+ int err;
5436+ umode_t mode;
5437+ unsigned int mnt_flags;
5438+ unsigned char isdir, isreg, force;
5439+ const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5440+ struct au_dtime dt;
5441+ struct path h_path;
5442+ struct dentry *h_src, *h_dst, *h_parent;
5443+ struct inode *h_inode, *h_dir;
5444+ struct super_block *sb;
5445+
5446+ /* bsrc branch can be ro/rw. */
5447+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5448+ h_inode = d_inode(h_src);
5449+ AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5450+
5451+ /* try stopping to be referenced while we are creating */
5452+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5453+ if (au_ftest_cpup(cpg->flags, RENAME))
5454+ AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5455+ AUFS_WH_PFX_LEN));
5456+ h_parent = h_dst->d_parent; /* dir inode is locked */
5457+ h_dir = d_inode(h_parent);
5458+ IMustLock(h_dir);
5459+ AuDebugOn(h_parent != h_dst->d_parent);
5460+
5461+ sb = cpg->dentry->d_sb;
5462+ h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5463+ if (do_dt) {
5464+ h_path.dentry = h_parent;
5465+ au_dtime_store(&dt, dst_parent, &h_path);
5466+ }
5467+ h_path.dentry = h_dst;
5468+
5469+ isreg = 0;
5470+ isdir = 0;
5471+ mode = h_inode->i_mode;
5472+ switch (mode & S_IFMT) {
5473+ case S_IFREG:
5474+ isreg = 1;
5475+ err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
5476+ if (!err)
5477+ err = au_do_cpup_regular(cpg, h_src_attr);
5478+ break;
5479+ case S_IFDIR:
5480+ isdir = 1;
5481+ err = vfsub_mkdir(h_dir, &h_path, mode);
5482+ if (!err)
5483+ err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5484+ break;
5485+ case S_IFLNK:
5486+ err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5487+ break;
5488+ case S_IFCHR:
5489+ case S_IFBLK:
5490+ AuDebugOn(!capable(CAP_MKNOD));
5491+ fallthrough;
5492+ case S_IFIFO:
5493+ case S_IFSOCK:
5494+ err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5495+ break;
5496+ default:
5497+ AuIOErr("Unknown inode type 0%o\n", mode);
5498+ err = -EIO;
5499+ }
5500+ if (!err)
5501+ err = au_reset_acl(h_dir, &h_path, mode);
5502+
5503+ mnt_flags = au_mntflags(sb);
5504+ if (!au_opt_test(mnt_flags, UDBA_NONE)
5505+ && !isdir
5506+ && au_opt_test(mnt_flags, XINO)
5507+ && (h_inode->i_nlink == 1
5508+ || (h_inode->i_state & I_LINKABLE))
5509+ /* todo: unnecessary? */
5510+ /* && d_inode(cpg->dentry)->i_nlink == 1 */
5511+ && cpg->bdst < cpg->bsrc
5512+ && !au_ftest_cpup(cpg->flags, KEEPLINO))
5513+ au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5514+ /* ignore this error */
5515+
5516+ if (!err) {
5517+ force = 0;
5518+ if (isreg) {
5519+ force = !!cpg->len;
5520+ if (cpg->len == -1)
5521+ force = !!i_size_read(h_inode);
5522+ }
5523+ au_fhsm_wrote(sb, cpg->bdst, force);
5524+ }
5525+
5526+ if (do_dt)
5527+ au_dtime_revert(&dt);
5528+ return err;
5529+}
5530+
5531+static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5532+{
5533+ int err;
5534+ struct dentry *dentry, *h_dentry, *h_parent, *parent;
5535+ struct inode *h_dir;
5536+ aufs_bindex_t bdst;
5537+
5538+ dentry = cpg->dentry;
5539+ bdst = cpg->bdst;
5540+ h_dentry = au_h_dptr(dentry, bdst);
5541+ if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5542+ dget(h_dentry);
5543+ au_set_h_dptr(dentry, bdst, NULL);
5544+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
5545+ if (!err)
5546+ h_path->dentry = dget(au_h_dptr(dentry, bdst));
5547+ au_set_h_dptr(dentry, bdst, h_dentry);
5548+ } else {
5549+ err = 0;
5550+ parent = dget_parent(dentry);
5551+ h_parent = au_h_dptr(parent, bdst);
5552+ dput(parent);
5553+ h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5554+ if (IS_ERR(h_path->dentry))
5555+ err = PTR_ERR(h_path->dentry);
5556+ }
5557+ if (unlikely(err))
5558+ goto out;
5559+
5560+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5561+ h_dir = d_inode(h_parent);
5562+ IMustLock(h_dir);
5563+ AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5564+ /* no delegation since it is just created */
5565+ err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5566+ /*flags*/0);
5567+ dput(h_path->dentry);
5568+
5569+out:
5570+ return err;
5571+}
5572+
5573+/*
5574+ * copyup the @dentry from @bsrc to @bdst.
5575+ * the caller must set the both of lower dentries.
5576+ * @len is for truncating when it is -1 copyup the entire file.
5577+ * in link/rename cases, @dst_parent may be different from the real one.
5578+ * basic->bsrc can be larger than basic->bdst.
5579+ * aufs doesn't touch the credential so
5580+ * security_inode_copy_up{,_xattr}() are unnecessary.
5581+ */
5582+static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5583+{
5584+ int err, rerr;
5585+ aufs_bindex_t old_ibtop;
5586+ unsigned char isdir, plink;
5587+ struct dentry *h_src, *h_dst, *h_parent;
5588+ struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5589+ struct super_block *sb;
5590+ struct au_branch *br;
5591+ /* to reduce stack size */
5592+ struct {
5593+ struct au_dtime dt;
5594+ struct path h_path;
5595+ struct au_cpup_reg_attr h_src_attr;
5596+ } *a;
5597+
5598+ err = -ENOMEM;
5599+ a = kmalloc(sizeof(*a), GFP_NOFS);
5600+ if (unlikely(!a))
5601+ goto out;
5602+ a->h_src_attr.valid = 0;
5603+
5604+ sb = cpg->dentry->d_sb;
5605+ br = au_sbr(sb, cpg->bdst);
5606+ a->h_path.mnt = au_br_mnt(br);
5607+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5608+ h_parent = h_dst->d_parent; /* dir inode is locked */
5609+ h_dir = d_inode(h_parent);
5610+ IMustLock(h_dir);
5611+
5612+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5613+ inode = d_inode(cpg->dentry);
5614+
5615+ if (!dst_parent)
5616+ dst_parent = dget_parent(cpg->dentry);
5617+ else
5618+ dget(dst_parent);
5619+
5620+ plink = !!au_opt_test(au_mntflags(sb), PLINK);
5621+ dst_inode = au_h_iptr(inode, cpg->bdst);
5622+ if (dst_inode) {
5623+ if (unlikely(!plink)) {
5624+ err = -EIO;
5625+ AuIOErr("hi%lu(i%lu) exists on b%d "
5626+ "but plink is disabled\n",
5627+ dst_inode->i_ino, inode->i_ino, cpg->bdst);
5628+ goto out_parent;
5629+ }
5630+
5631+ if (dst_inode->i_nlink) {
5632+ const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5633+
5634+ h_src = au_plink_lkup(inode, cpg->bdst);
5635+ err = PTR_ERR(h_src);
5636+ if (IS_ERR(h_src))
5637+ goto out_parent;
5638+ if (unlikely(d_is_negative(h_src))) {
5639+ err = -EIO;
5640+ AuIOErr("i%lu exists on b%d "
5641+ "but not pseudo-linked\n",
5642+ inode->i_ino, cpg->bdst);
5643+ dput(h_src);
5644+ goto out_parent;
5645+ }
5646+
5647+ if (do_dt) {
5648+ a->h_path.dentry = h_parent;
5649+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
5650+ }
5651+
5652+ a->h_path.dentry = h_dst;
5653+ delegated = NULL;
5654+ err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5655+ if (!err && au_ftest_cpup(cpg->flags, RENAME))
5656+ err = au_do_ren_after_cpup(cpg, &a->h_path);
5657+ if (do_dt)
5658+ au_dtime_revert(&a->dt);
5659+ if (unlikely(err == -EWOULDBLOCK)) {
5660+ pr_warn("cannot retry for NFSv4 delegation"
5661+ " for an internal link\n");
5662+ iput(delegated);
5663+ }
5664+ dput(h_src);
5665+ goto out_parent;
5666+ } else
5667+ /* todo: cpup_wh_file? */
5668+ /* udba work */
5669+ au_update_ibrange(inode, /*do_put_zero*/1);
5670+ }
5671+
5672+ isdir = S_ISDIR(inode->i_mode);
5673+ old_ibtop = au_ibtop(inode);
5674+ err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5675+ if (unlikely(err))
5676+ goto out_rev;
5677+ dst_inode = d_inode(h_dst);
5678+ inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5679+ /* todo: necessary? */
5680+ /* au_pin_hdir_unlock(cpg->pin); */
5681+
5682+ err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
5683+ if (unlikely(err)) {
5684+ /* todo: necessary? */
5685+ /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5686+ inode_unlock(dst_inode);
5687+ goto out_rev;
5688+ }
5689+
5690+ if (cpg->bdst < old_ibtop) {
5691+ if (S_ISREG(inode->i_mode)) {
5692+ err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5693+ if (unlikely(err)) {
5694+ /* ignore an error */
5695+ /* au_pin_hdir_relock(cpg->pin); */
5696+ inode_unlock(dst_inode);
5697+ goto out_rev;
5698+ }
5699+ }
5700+ au_set_ibtop(inode, cpg->bdst);
5701+ } else
5702+ au_set_ibbot(inode, cpg->bdst);
5703+ au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5704+ au_hi_flags(inode, isdir));
5705+
5706+ /* todo: necessary? */
5707+ /* err = au_pin_hdir_relock(cpg->pin); */
5708+ inode_unlock(dst_inode);
5709+ if (unlikely(err))
5710+ goto out_rev;
5711+
5712+ src_inode = d_inode(h_src);
5713+ if (!isdir
5714+ && (src_inode->i_nlink > 1
5715+ || src_inode->i_state & I_LINKABLE)
5716+ && plink)
5717+ au_plink_append(inode, cpg->bdst, h_dst);
5718+
5719+ if (au_ftest_cpup(cpg->flags, RENAME)) {
5720+ a->h_path.dentry = h_dst;
5721+ err = au_do_ren_after_cpup(cpg, &a->h_path);
5722+ }
5723+ if (!err)
5724+ goto out_parent; /* success */
5725+
5726+ /* revert */
5727+out_rev:
5728+ a->h_path.dentry = h_parent;
5729+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
5730+ a->h_path.dentry = h_dst;
5731+ rerr = 0;
5732+ if (d_is_positive(h_dst)) {
5733+ if (!isdir) {
5734+ /* no delegation since it is just created */
5735+ rerr = vfsub_unlink(h_dir, &a->h_path,
5736+ /*delegated*/NULL, /*force*/0);
5737+ } else
5738+ rerr = vfsub_rmdir(h_dir, &a->h_path);
5739+ }
5740+ au_dtime_revert(&a->dt);
5741+ if (rerr) {
5742+ AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5743+ err = -EIO;
5744+ }
5745+out_parent:
5746+ dput(dst_parent);
5747+ au_kfree_rcu(a);
5748+out:
5749+ return err;
5750+}
5751+
5752+#if 0 /* reserved */
5753+struct au_cpup_single_args {
5754+ int *errp;
5755+ struct au_cp_generic *cpg;
5756+ struct dentry *dst_parent;
5757+};
5758+
5759+static void au_call_cpup_single(void *args)
5760+{
5761+ struct au_cpup_single_args *a = args;
5762+
5763+ au_pin_hdir_acquire_nest(a->cpg->pin);
5764+ *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5765+ au_pin_hdir_release(a->cpg->pin);
5766+}
5767+#endif
5768+
5769+/*
5770+ * prevent SIGXFSZ in copy-up.
5771+ * testing CAP_MKNOD is for generic fs,
5772+ * but CAP_FSETID is for xfs only, currently.
5773+ */
5774+static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5775+{
5776+ int do_sio;
5777+ struct super_block *sb;
5778+ struct inode *h_dir;
5779+
5780+ do_sio = 0;
5781+ sb = au_pinned_parent(pin)->d_sb;
5782+ if (!au_wkq_test()
5783+ && (!au_sbi(sb)->si_plink_maint_pid
5784+ || au_plink_maint(sb, AuLock_NOPLM))) {
5785+ switch (mode & S_IFMT) {
5786+ case S_IFREG:
5787+ /* no condition about RLIMIT_FSIZE and the file size */
5788+ do_sio = 1;
5789+ break;
5790+ case S_IFCHR:
5791+ case S_IFBLK:
5792+ do_sio = !capable(CAP_MKNOD);
5793+ break;
5794+ }
5795+ if (!do_sio)
5796+ do_sio = ((mode & (S_ISUID | S_ISGID))
5797+ && !capable(CAP_FSETID));
5798+ /* this workaround may be removed in the future */
5799+ if (!do_sio) {
5800+ h_dir = au_pinned_h_dir(pin);
5801+ do_sio = h_dir->i_mode & S_ISVTX;
5802+ }
5803+ }
5804+
5805+ return do_sio;
5806+}
5807+
5808+#if 0 /* reserved */
5809+int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5810+{
5811+ int err, wkq_err;
5812+ struct dentry *h_dentry;
5813+
5814+ h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5815+ if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5816+ err = au_cpup_single(cpg, dst_parent);
5817+ else {
5818+ struct au_cpup_single_args args = {
5819+ .errp = &err,
5820+ .cpg = cpg,
5821+ .dst_parent = dst_parent
5822+ };
5823+ wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5824+ if (unlikely(wkq_err))
5825+ err = wkq_err;
5826+ }
5827+
5828+ return err;
5829+}
5830+#endif
5831+
5832+/*
5833+ * copyup the @dentry from the first active lower branch to @bdst,
5834+ * using au_cpup_single().
5835+ */
5836+static int au_cpup_simple(struct au_cp_generic *cpg)
5837+{
5838+ int err;
5839+ unsigned int flags_orig;
5840+ struct dentry *dentry;
5841+
5842+ AuDebugOn(cpg->bsrc < 0);
5843+
5844+ dentry = cpg->dentry;
5845+ DiMustWriteLock(dentry);
5846+
5847+ err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5848+ if (!err) {
5849+ flags_orig = cpg->flags;
5850+ au_fset_cpup(cpg->flags, RENAME);
5851+ err = au_cpup_single(cpg, NULL);
5852+ cpg->flags = flags_orig;
5853+ if (!err)
5854+ return 0; /* success */
5855+
5856+ /* revert */
5857+ au_set_h_dptr(dentry, cpg->bdst, NULL);
5858+ au_set_dbtop(dentry, cpg->bsrc);
5859+ }
5860+
5861+ return err;
5862+}
5863+
5864+struct au_cpup_simple_args {
5865+ int *errp;
5866+ struct au_cp_generic *cpg;
5867+};
5868+
5869+static void au_call_cpup_simple(void *args)
5870+{
5871+ struct au_cpup_simple_args *a = args;
5872+
5873+ au_pin_hdir_acquire_nest(a->cpg->pin);
5874+ *a->errp = au_cpup_simple(a->cpg);
5875+ au_pin_hdir_release(a->cpg->pin);
5876+}
5877+
5878+static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5879+{
5880+ int err, wkq_err;
5881+ struct dentry *dentry, *parent;
5882+ struct file *h_file;
5883+ struct inode *h_dir;
5884+
5885+ dentry = cpg->dentry;
5886+ h_file = NULL;
5887+ if (au_ftest_cpup(cpg->flags, HOPEN)) {
5888+ AuDebugOn(cpg->bsrc < 0);
5889+ h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5890+ err = PTR_ERR(h_file);
5891+ if (IS_ERR(h_file))
5892+ goto out;
5893+ }
5894+
5895+ parent = dget_parent(dentry);
5896+ h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5897+ if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
5898+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5899+ err = au_cpup_simple(cpg);
5900+ else {
5901+ struct au_cpup_simple_args args = {
5902+ .errp = &err,
5903+ .cpg = cpg
5904+ };
5905+ wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5906+ if (unlikely(wkq_err))
5907+ err = wkq_err;
5908+ }
5909+
5910+ dput(parent);
5911+ if (h_file)
5912+ au_h_open_post(dentry, cpg->bsrc, h_file);
5913+
5914+out:
5915+ return err;
5916+}
5917+
5918+int au_sio_cpup_simple(struct au_cp_generic *cpg)
5919+{
5920+ aufs_bindex_t bsrc, bbot;
5921+ struct dentry *dentry, *h_dentry;
5922+
5923+ if (cpg->bsrc < 0) {
5924+ dentry = cpg->dentry;
5925+ bbot = au_dbbot(dentry);
5926+ for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
5927+ h_dentry = au_h_dptr(dentry, bsrc);
5928+ if (h_dentry) {
5929+ AuDebugOn(d_is_negative(h_dentry));
5930+ break;
5931+ }
5932+ }
5933+ AuDebugOn(bsrc > bbot);
5934+ cpg->bsrc = bsrc;
5935+ }
5936+ AuDebugOn(cpg->bsrc <= cpg->bdst);
5937+ return au_do_sio_cpup_simple(cpg);
5938+}
5939+
5940+int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5941+{
5942+ AuDebugOn(cpg->bdst <= cpg->bsrc);
5943+ return au_do_sio_cpup_simple(cpg);
5944+}
5945+
5946+/* ---------------------------------------------------------------------- */
5947+
5948+/*
5949+ * copyup the deleted file for writing.
5950+ */
5951+static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5952+ struct file *file)
5953+{
5954+ int err;
5955+ unsigned int flags_orig;
5956+ aufs_bindex_t bsrc_orig;
5957+ struct au_dinfo *dinfo;
5958+ struct {
5959+ struct au_hdentry *hd;
5960+ struct dentry *h_dentry;
5961+ } hdst, hsrc;
5962+
5963+ dinfo = au_di(cpg->dentry);
5964+ AuRwMustWriteLock(&dinfo->di_rwsem);
5965+
5966+ bsrc_orig = cpg->bsrc;
5967+ cpg->bsrc = dinfo->di_btop;
5968+ hdst.hd = au_hdentry(dinfo, cpg->bdst);
5969+ hdst.h_dentry = hdst.hd->hd_dentry;
5970+ hdst.hd->hd_dentry = wh_dentry;
5971+ dinfo->di_btop = cpg->bdst;
5972+
5973+ hsrc.h_dentry = NULL;
5974+ if (file) {
5975+ hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
5976+ hsrc.h_dentry = hsrc.hd->hd_dentry;
5977+ hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
5978+ }
5979+ flags_orig = cpg->flags;
5980+ cpg->flags = !AuCpup_DTIME;
5981+ err = au_cpup_single(cpg, /*h_parent*/NULL);
5982+ cpg->flags = flags_orig;
5983+ if (file) {
5984+ if (!err)
5985+ err = au_reopen_nondir(file);
5986+ hsrc.hd->hd_dentry = hsrc.h_dentry;
5987+ }
5988+ hdst.hd->hd_dentry = hdst.h_dentry;
5989+ dinfo->di_btop = cpg->bsrc;
5990+ cpg->bsrc = bsrc_orig;
5991+
5992+ return err;
5993+}
5994+
5995+static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5996+{
5997+ int err;
5998+ aufs_bindex_t bdst;
5999+ struct au_dtime dt;
6000+ struct dentry *dentry, *parent, *h_parent, *wh_dentry;
6001+ struct au_branch *br;
6002+ struct path h_path;
6003+
6004+ dentry = cpg->dentry;
6005+ bdst = cpg->bdst;
6006+ br = au_sbr(dentry->d_sb, bdst);
6007+ parent = dget_parent(dentry);
6008+ h_parent = au_h_dptr(parent, bdst);
6009+ wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
6010+ err = PTR_ERR(wh_dentry);
6011+ if (IS_ERR(wh_dentry))
6012+ goto out;
6013+
6014+ h_path.dentry = h_parent;
6015+ h_path.mnt = au_br_mnt(br);
6016+ au_dtime_store(&dt, parent, &h_path);
6017+ err = au_do_cpup_wh(cpg, wh_dentry, file);
6018+ if (unlikely(err))
6019+ goto out_wh;
6020+
6021+ dget(wh_dentry);
6022+ h_path.dentry = wh_dentry;
6023+ if (!d_is_dir(wh_dentry)) {
6024+ /* no delegation since it is just created */
6025+ err = vfsub_unlink(d_inode(h_parent), &h_path,
6026+ /*delegated*/NULL, /*force*/0);
6027+ } else
6028+ err = vfsub_rmdir(d_inode(h_parent), &h_path);
6029+ if (unlikely(err)) {
6030+ AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6031+ wh_dentry, err);
6032+ err = -EIO;
6033+ }
6034+ au_dtime_revert(&dt);
6035+ au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
6036+
6037+out_wh:
6038+ dput(wh_dentry);
6039+out:
6040+ dput(parent);
6041+ return err;
6042+}
6043+
6044+struct au_cpup_wh_args {
6045+ int *errp;
6046+ struct au_cp_generic *cpg;
6047+ struct file *file;
6048+};
6049+
6050+static void au_call_cpup_wh(void *args)
6051+{
6052+ struct au_cpup_wh_args *a = args;
6053+
6054+ au_pin_hdir_acquire_nest(a->cpg->pin);
6055+ *a->errp = au_cpup_wh(a->cpg, a->file);
6056+ au_pin_hdir_release(a->cpg->pin);
6057+}
6058+
6059+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6060+{
6061+ int err, wkq_err;
6062+ aufs_bindex_t bdst;
6063+ struct dentry *dentry, *parent, *h_orph, *h_parent;
6064+ struct inode *dir, *h_dir, *h_tmpdir;
6065+ struct au_wbr *wbr;
6066+ struct au_pin wh_pin, *pin_orig;
6067+
6068+ dentry = cpg->dentry;
6069+ bdst = cpg->bdst;
6070+ parent = dget_parent(dentry);
6071+ dir = d_inode(parent);
6072+ h_orph = NULL;
6073+ h_parent = NULL;
6074+ h_dir = au_igrab(au_h_iptr(dir, bdst));
6075+ h_tmpdir = h_dir;
6076+ pin_orig = NULL;
6077+ if (!h_dir->i_nlink) {
6078+ wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6079+ h_orph = wbr->wbr_orph;
6080+
6081+ h_parent = dget(au_h_dptr(parent, bdst));
6082+ au_set_h_dptr(parent, bdst, dget(h_orph));
6083+ h_tmpdir = d_inode(h_orph);
6084+ au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6085+
6086+ inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
6087+ /* todo: au_h_open_pre()? */
6088+
6089+ pin_orig = cpg->pin;
6090+ au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
6091+ AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6092+ cpg->pin = &wh_pin;
6093+ }
6094+
6095+ if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
6096+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6097+ err = au_cpup_wh(cpg, file);
6098+ else {
6099+ struct au_cpup_wh_args args = {
6100+ .errp = &err,
6101+ .cpg = cpg,
6102+ .file = file
6103+ };
6104+ wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6105+ if (unlikely(wkq_err))
6106+ err = wkq_err;
6107+ }
6108+
6109+ if (h_orph) {
6110+ inode_unlock(h_tmpdir);
6111+ /* todo: au_h_open_post()? */
6112+ au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
6113+ au_set_h_dptr(parent, bdst, h_parent);
6114+ AuDebugOn(!pin_orig);
6115+ cpg->pin = pin_orig;
6116+ }
6117+ iput(h_dir);
6118+ dput(parent);
6119+
6120+ return err;
6121+}
6122+
6123+/* ---------------------------------------------------------------------- */
6124+
6125+/*
6126+ * generic routine for both of copy-up and copy-down.
6127+ */
6128+/* cf. revalidate function in file.c */
6129+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6130+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6131+ struct au_pin *pin,
6132+ struct dentry *h_parent, void *arg),
6133+ void *arg)
6134+{
6135+ int err;
6136+ struct au_pin pin;
6137+ struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
6138+
6139+ err = 0;
6140+ parent = dget_parent(dentry);
6141+ if (IS_ROOT(parent))
6142+ goto out;
6143+
6144+ au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6145+ au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6146+
6147+ /* do not use au_dpage */
6148+ real_parent = parent;
6149+ while (1) {
6150+ dput(parent);
6151+ parent = dget_parent(dentry);
6152+ h_parent = au_h_dptr(parent, bdst);
6153+ if (h_parent)
6154+ goto out; /* success */
6155+
6156+ /* find top dir which is necessary to cpup */
6157+ do {
6158+ d = parent;
6159+ dput(parent);
6160+ parent = dget_parent(d);
6161+ di_read_lock_parent3(parent, !AuLock_IR);
6162+ h_parent = au_h_dptr(parent, bdst);
6163+ di_read_unlock(parent, !AuLock_IR);
6164+ } while (!h_parent);
6165+
6166+ if (d != real_parent)
6167+ di_write_lock_child3(d);
6168+
6169+ /* somebody else might create while we were sleeping */
6170+ h_dentry = au_h_dptr(d, bdst);
6171+ if (!h_dentry || d_is_negative(h_dentry)) {
6172+ if (h_dentry)
6173+ au_update_dbtop(d);
6174+
6175+ au_pin_set_dentry(&pin, d);
6176+ err = au_do_pin(&pin);
6177+ if (!err) {
6178+ err = cp(d, bdst, &pin, h_parent, arg);
6179+ au_unpin(&pin);
6180+ }
6181+ }
6182+
6183+ if (d != real_parent)
6184+ di_write_unlock(d);
6185+ if (unlikely(err))
6186+ break;
6187+ }
6188+
6189+out:
6190+ dput(parent);
6191+ return err;
6192+}
6193+
6194+static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
6195+ struct au_pin *pin,
6196+ struct dentry *h_parent __maybe_unused,
6197+ void *arg __maybe_unused)
6198+{
6199+ struct au_cp_generic cpg = {
6200+ .dentry = dentry,
6201+ .bdst = bdst,
6202+ .bsrc = -1,
6203+ .len = 0,
6204+ .pin = pin,
6205+ .flags = AuCpup_DTIME
6206+ };
6207+ return au_sio_cpup_simple(&cpg);
6208+}
6209+
6210+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6211+{
6212+ return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6213+}
6214+
6215+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6216+{
6217+ int err;
6218+ struct dentry *parent;
6219+ struct inode *dir;
6220+
6221+ parent = dget_parent(dentry);
6222+ dir = d_inode(parent);
6223+ err = 0;
6224+ if (au_h_iptr(dir, bdst))
6225+ goto out;
6226+
6227+ di_read_unlock(parent, AuLock_IR);
6228+ di_write_lock_parent(parent);
6229+ /* someone else might change our inode while we were sleeping */
6230+ if (!au_h_iptr(dir, bdst))
6231+ err = au_cpup_dirs(dentry, bdst);
6232+ di_downgrade_lock(parent, AuLock_IR);
6233+
6234+out:
6235+ dput(parent);
6236+ return err;
6237+}
6238diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6239--- /usr/share/empty/fs/aufs/cpup.h 1970-01-01 01:00:00.000000000 +0100
6240+++ linux/fs/aufs/cpup.h 2021-02-22 22:51:59.058078584 +0100
6241@@ -0,0 +1,100 @@
6242+/* SPDX-License-Identifier: GPL-2.0 */
6243+/*
6244+ * Copyright (C) 2005-2020 Junjiro R. Okajima
6245+ *
6246+ * This program, aufs is free software; you can redistribute it and/or modify
6247+ * it under the terms of the GNU General Public License as published by
6248+ * the Free Software Foundation; either version 2 of the License, or
6249+ * (at your option) any later version.
6250+ *
6251+ * This program is distributed in the hope that it will be useful,
6252+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6253+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6254+ * GNU General Public License for more details.
6255+ *
6256+ * You should have received a copy of the GNU General Public License
6257+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
6258+ */
6259+
6260+/*
6261+ * copy-up/down functions
6262+ */
6263+
6264+#ifndef __AUFS_CPUP_H__
6265+#define __AUFS_CPUP_H__
6266+
6267+#ifdef __KERNEL__
6268+
6269+#include <linux/path.h>
6270+
6271+struct inode;
6272+struct file;
6273+struct au_pin;
6274+
6275+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
6276+void au_cpup_attr_timesizes(struct inode *inode);
6277+void au_cpup_attr_nlink(struct inode *inode, int force);
6278+void au_cpup_attr_changeable(struct inode *inode);
6279+void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6280+void au_cpup_attr_all(struct inode *inode, int force);
6281+
6282+/* ---------------------------------------------------------------------- */
6283+
6284+struct au_cp_generic {
6285+ struct dentry *dentry;
6286+ aufs_bindex_t bdst, bsrc;
6287+ loff_t len;
6288+ struct au_pin *pin;
6289+ unsigned int flags;
6290+};
6291+
6292+/* cpup flags */
6293+#define AuCpup_DTIME 1 /* do dtime_store/revert */
6294+#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
6295+ for link(2) */
6296+#define AuCpup_RENAME (1 << 2) /* rename after cpup */
6297+#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
6298+ cpup */
6299+#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
6300+ existing entry */
6301+#define AuCpup_RWDST (1 << 5) /* force write target even if
6302+ the branch is marked as RO */
6303+
6304+#ifndef CONFIG_AUFS_BR_HFSPLUS
6305+#undef AuCpup_HOPEN
6306+#define AuCpup_HOPEN 0
6307+#endif
6308+
6309+#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
6310+#define au_fset_cpup(flags, name) \
6311+ do { (flags) |= AuCpup_##name; } while (0)
6312+#define au_fclr_cpup(flags, name) \
6313+ do { (flags) &= ~AuCpup_##name; } while (0)
6314+
6315+int au_copy_file(struct file *dst, struct file *src, loff_t len);
6316+int au_sio_cpup_simple(struct au_cp_generic *cpg);
6317+int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6318+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6319+
6320+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6321+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6322+ struct au_pin *pin,
6323+ struct dentry *h_parent, void *arg),
6324+ void *arg);
6325+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6326+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6327+
6328+/* ---------------------------------------------------------------------- */
6329+
6330+/* keep timestamps when copyup */
6331+struct au_dtime {
6332+ struct dentry *dt_dentry;
6333+ struct path dt_h_path;
6334+ struct timespec64 dt_atime, dt_mtime;
6335+};
6336+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6337+ struct path *h_path);
6338+void au_dtime_revert(struct au_dtime *dt);
6339+
6340+#endif /* __KERNEL__ */
6341+#endif /* __AUFS_CPUP_H__ */
6342diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6343--- /usr/share/empty/fs/aufs/dbgaufs.c 1970-01-01 01:00:00.000000000 +0100
6344+++ linux/fs/aufs/dbgaufs.c 2021-02-22 22:51:59.058078584 +0100
6345@@ -0,0 +1,526 @@
6346+// SPDX-License-Identifier: GPL-2.0
6347+/*
6348+ * Copyright (C) 2005-2020 Junjiro R. Okajima
6349+ *
6350+ * This program, aufs is free software; you can redistribute it and/or modify
6351+ * it under the terms of the GNU General Public License as published by
6352+ * the Free Software Foundation; either version 2 of the License, or
6353+ * (at your option) any later version.
6354+ *
6355+ * This program is distributed in the hope that it will be useful,
6356+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6357+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6358+ * GNU General Public License for more details.
6359+ *
6360+ * You should have received a copy of the GNU General Public License
6361+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
6362+ */
6363+
6364+/*
6365+ * debugfs interface
6366+ */
6367+
6368+#include <linux/debugfs.h>
6369+#include "aufs.h"
6370+
6371+#ifndef CONFIG_SYSFS
6372+#error DEBUG_FS depends upon SYSFS
6373+#endif
6374+
6375+static struct dentry *dbgaufs;
6376+static const mode_t dbgaufs_mode = 0444;
6377+
6378+/* 20 is max digits length of ulong 64 */
6379+struct dbgaufs_arg {
6380+ int n;
6381+ char a[20 * 4];
6382+};
6383+
6384+/*
6385+ * common function for all XINO files
6386+ */
6387+static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6388+ struct file *file)
6389+{
6390+ void *p;
6391+
6392+ p = file->private_data;
6393+ if (p) {
6394+ /* this is struct dbgaufs_arg */
6395+ AuDebugOn(!au_kfree_sz_test(p));
6396+ au_kfree_do_rcu(p);
6397+ }
6398+ return 0;
6399+}
6400+
6401+static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6402+ int cnt)
6403+{
6404+ int err;
6405+ struct kstat st;
6406+ struct dbgaufs_arg *p;
6407+
6408+ err = -ENOMEM;
6409+ p = kmalloc(sizeof(*p), GFP_NOFS);
6410+ if (unlikely(!p))
6411+ goto out;
6412+
6413+ err = 0;
6414+ p->n = 0;
6415+ file->private_data = p;
6416+ if (!xf)
6417+ goto out;
6418+
6419+ err = vfsub_getattr(&xf->f_path, &st);
6420+ if (!err) {
6421+ if (do_fcnt)
6422+ p->n = snprintf
6423+ (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6424+ cnt, st.blocks, st.blksize,
6425+ (long long)st.size);
6426+ else
6427+ p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
6428+ st.blocks, st.blksize,
6429+ (long long)st.size);
6430+ AuDebugOn(p->n >= sizeof(p->a));
6431+ } else {
6432+ p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6433+ err = 0;
6434+ }
6435+
6436+out:
6437+ return err;
6438+}
6439+
6440+static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6441+ size_t count, loff_t *ppos)
6442+{
6443+ struct dbgaufs_arg *p;
6444+
6445+ p = file->private_data;
6446+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6447+}
6448+
6449+/* ---------------------------------------------------------------------- */
6450+
6451+struct dbgaufs_plink_arg {
6452+ int n;
6453+ char a[];
6454+};
6455+
6456+static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6457+ struct file *file)
6458+{
6459+ free_page((unsigned long)file->private_data);
6460+ return 0;
6461+}
6462+
6463+static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6464+{
6465+ int err, i, limit;
6466+ unsigned long n, sum;
6467+ struct dbgaufs_plink_arg *p;
6468+ struct au_sbinfo *sbinfo;
6469+ struct super_block *sb;
6470+ struct hlist_bl_head *hbl;
6471+
6472+ err = -ENOMEM;
6473+ p = (void *)get_zeroed_page(GFP_NOFS);
6474+ if (unlikely(!p))
6475+ goto out;
6476+
6477+ err = -EFBIG;
6478+ sbinfo = inode->i_private;
6479+ sb = sbinfo->si_sb;
6480+ si_noflush_read_lock(sb);
6481+ if (au_opt_test(au_mntflags(sb), PLINK)) {
6482+ limit = PAGE_SIZE - sizeof(p->n);
6483+
6484+ /* the number of buckets */
6485+ n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6486+ p->n += n;
6487+ limit -= n;
6488+
6489+ sum = 0;
6490+ for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6491+ i++, hbl++) {
6492+ n = au_hbl_count(hbl);
6493+ sum += n;
6494+
6495+ n = snprintf(p->a + p->n, limit, "%lu ", n);
6496+ p->n += n;
6497+ limit -= n;
6498+ if (unlikely(limit <= 0))
6499+ goto out_free;
6500+ }
6501+ p->a[p->n - 1] = '\n';
6502+
6503+ /* the sum of plinks */
6504+ n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6505+ p->n += n;
6506+ limit -= n;
6507+ if (unlikely(limit <= 0))
6508+ goto out_free;
6509+ } else {
6510+#define str "1\n0\n0\n"
6511+ p->n = sizeof(str) - 1;
6512+ strcpy(p->a, str);
6513+#undef str
6514+ }
6515+ si_read_unlock(sb);
6516+
6517+ err = 0;
6518+ file->private_data = p;
6519+ goto out; /* success */
6520+
6521+out_free:
6522+ free_page((unsigned long)p);
6523+out:
6524+ return err;
6525+}
6526+
6527+static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6528+ size_t count, loff_t *ppos)
6529+{
6530+ struct dbgaufs_plink_arg *p;
6531+
6532+ p = file->private_data;
6533+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6534+}
6535+
6536+static const struct file_operations dbgaufs_plink_fop = {
6537+ .owner = THIS_MODULE,
6538+ .open = dbgaufs_plink_open,
6539+ .release = dbgaufs_plink_release,
6540+ .read = dbgaufs_plink_read
6541+};
6542+
6543+/* ---------------------------------------------------------------------- */
6544+
6545+static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6546+{
6547+ int err;
6548+ struct au_sbinfo *sbinfo;
6549+ struct super_block *sb;
6550+
6551+ sbinfo = inode->i_private;
6552+ sb = sbinfo->si_sb;
6553+ si_noflush_read_lock(sb);
6554+ err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
6555+ si_read_unlock(sb);
6556+ return err;
6557+}
6558+
6559+static const struct file_operations dbgaufs_xib_fop = {
6560+ .owner = THIS_MODULE,
6561+ .open = dbgaufs_xib_open,
6562+ .release = dbgaufs_xi_release,
6563+ .read = dbgaufs_xi_read
6564+};
6565+
6566+/* ---------------------------------------------------------------------- */
6567+
6568+#define DbgaufsXi_PREFIX "xi"
6569+
6570+static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6571+{
6572+ int err, idx;
6573+ long l;
6574+ aufs_bindex_t bindex;
6575+ char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
6576+ struct au_sbinfo *sbinfo;
6577+ struct super_block *sb;
6578+ struct au_xino *xi;
6579+ struct file *xf;
6580+ struct qstr *name;
6581+ struct au_branch *br;
6582+
6583+ err = -ENOENT;
6584+ name = &file->f_path.dentry->d_name;
6585+ if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6586+ || memcmp(name->name, DbgaufsXi_PREFIX,
6587+ sizeof(DbgaufsXi_PREFIX) - 1)))
6588+ goto out;
6589+
6590+ AuDebugOn(name->len >= sizeof(a));
6591+ memcpy(a, name->name, name->len);
6592+ a[name->len] = '\0';
6593+ p = strchr(a, '-');
6594+ if (p)
6595+ *p = '\0';
6596+ err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6597+ if (unlikely(err))
6598+ goto out;
6599+ bindex = l;
6600+ idx = 0;
6601+ if (p) {
6602+ err = kstrtol(p + 1, 10, &l);
6603+ if (unlikely(err))
6604+ goto out;
6605+ idx = l;
6606+ }
6607+
6608+ err = -ENOENT;
6609+ sbinfo = inode->i_private;
6610+ sb = sbinfo->si_sb;
6611+ si_noflush_read_lock(sb);
6612+ if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6613+ goto out_si;
6614+ br = au_sbr(sb, bindex);
6615+ xi = br->br_xino;
6616+ if (unlikely(idx >= xi->xi_nfile))
6617+ goto out_si;
6618+ xf = au_xino_file(xi, idx);
6619+ if (xf)
6620+ err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6621+ au_xino_count(br));
6622+
6623+out_si:
6624+ si_read_unlock(sb);
6625+out:
6626+ AuTraceErr(err);
6627+ return err;
6628+}
6629+
6630+static const struct file_operations dbgaufs_xino_fop = {
6631+ .owner = THIS_MODULE,
6632+ .open = dbgaufs_xino_open,
6633+ .release = dbgaufs_xi_release,
6634+ .read = dbgaufs_xi_read
6635+};
6636+
6637+void dbgaufs_xino_del(struct au_branch *br)
6638+{
6639+ struct dentry *dbgaufs;
6640+
6641+ dbgaufs = br->br_dbgaufs;
6642+ if (!dbgaufs)
6643+ return;
6644+
6645+ br->br_dbgaufs = NULL;
6646+ /* debugfs acquires the parent i_mutex */
6647+ lockdep_off();
6648+ debugfs_remove(dbgaufs);
6649+ lockdep_on();
6650+}
6651+
6652+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6653+{
6654+ aufs_bindex_t bbot;
6655+ struct au_branch *br;
6656+
6657+ if (!au_sbi(sb)->si_dbgaufs)
6658+ return;
6659+
6660+ bbot = au_sbbot(sb);
6661+ for (; bindex <= bbot; bindex++) {
6662+ br = au_sbr(sb, bindex);
6663+ dbgaufs_xino_del(br);
6664+ }
6665+}
6666+
6667+static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6668+ unsigned int idx, struct dentry *parent,
6669+ struct au_sbinfo *sbinfo)
6670+{
6671+ struct au_branch *br;
6672+ struct dentry *d;
6673+ /* "xi" bindex(5) "-" idx(2) NULL */
6674+ char name[sizeof(DbgaufsXi_PREFIX) + 8];
6675+
6676+ if (!idx)
6677+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6678+ else
6679+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6680+ bindex, idx);
6681+ br = au_sbr(sb, bindex);
6682+ if (br->br_dbgaufs) {
6683+ struct qstr qstr = QSTR_INIT(name, strlen(name));
6684+
6685+ if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6686+ /* debugfs acquires the parent i_mutex */
6687+ lockdep_off();
6688+ d = debugfs_rename(parent, br->br_dbgaufs, parent,
6689+ name);
6690+ lockdep_on();
6691+ if (unlikely(!d))
6692+ pr_warn("failed renaming %pd/%s, ignored.\n",
6693+ parent, name);
6694+ }
6695+ } else {
6696+ lockdep_off();
6697+ br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6698+ sbinfo, &dbgaufs_xino_fop);
6699+ lockdep_on();
6700+ if (unlikely(!br->br_dbgaufs))
6701+ pr_warn("failed creating %pd/%s, ignored.\n",
6702+ parent, name);
6703+ }
6704+}
6705+
6706+static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6707+ struct dentry *parent, struct au_sbinfo *sbinfo)
6708+{
6709+ struct au_branch *br;
6710+ struct au_xino *xi;
6711+ unsigned int u;
6712+
6713+ br = au_sbr(sb, bindex);
6714+ xi = br->br_xino;
6715+ for (u = 0; u < xi->xi_nfile; u++)
6716+ dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6717+}
6718+
6719+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
6720+{
6721+ struct au_sbinfo *sbinfo;
6722+ struct dentry *parent;
6723+ aufs_bindex_t bbot;
6724+
6725+ if (!au_opt_test(au_mntflags(sb), XINO))
6726+ return;
6727+
6728+ sbinfo = au_sbi(sb);
6729+ parent = sbinfo->si_dbgaufs;
6730+ if (!parent)
6731+ return;
6732+
6733+ bbot = au_sbbot(sb);
6734+ if (topdown)
6735+ for (; bindex <= bbot; bindex++)
6736+ dbgaufs_br_add(sb, bindex, parent, sbinfo);
6737+ else
6738+ for (; bbot >= bindex; bbot--)
6739+ dbgaufs_br_add(sb, bbot, parent, sbinfo);
6740+}
6741+
6742+/* ---------------------------------------------------------------------- */
6743+
6744+#ifdef CONFIG_AUFS_EXPORT
6745+static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6746+{
6747+ int err;
6748+ struct au_sbinfo *sbinfo;
6749+ struct super_block *sb;
6750+
6751+ sbinfo = inode->i_private;
6752+ sb = sbinfo->si_sb;
6753+ si_noflush_read_lock(sb);
6754+ err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
6755+ si_read_unlock(sb);
6756+ return err;
6757+}
6758+
6759+static const struct file_operations dbgaufs_xigen_fop = {
6760+ .owner = THIS_MODULE,
6761+ .open = dbgaufs_xigen_open,
6762+ .release = dbgaufs_xi_release,
6763+ .read = dbgaufs_xi_read
6764+};
6765+
6766+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6767+{
6768+ int err;
6769+
6770+ /*
6771+ * This function is a dynamic '__init' function actually,
6772+ * so the tiny check for si_rwsem is unnecessary.
6773+ */
6774+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6775+
6776+ err = -EIO;
6777+ sbinfo->si_dbgaufs_xigen = debugfs_create_file
6778+ ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6779+ &dbgaufs_xigen_fop);
6780+ if (sbinfo->si_dbgaufs_xigen)
6781+ err = 0;
6782+
6783+ return err;
6784+}
6785+#else
6786+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6787+{
6788+ return 0;
6789+}
6790+#endif /* CONFIG_AUFS_EXPORT */
6791+
6792+/* ---------------------------------------------------------------------- */
6793+
6794+void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6795+{
6796+ /*
6797+ * This function is a dynamic '__fin' function actually,
6798+ * so the tiny check for si_rwsem is unnecessary.
6799+ */
6800+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6801+
6802+ debugfs_remove_recursive(sbinfo->si_dbgaufs);
6803+ sbinfo->si_dbgaufs = NULL;
6804+}
6805+
6806+int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6807+{
6808+ int err;
6809+ char name[SysaufsSiNameLen];
6810+
6811+ /*
6812+ * This function is a dynamic '__init' function actually,
6813+ * so the tiny check for si_rwsem is unnecessary.
6814+ */
6815+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6816+
6817+ err = -ENOENT;
6818+ if (!dbgaufs) {
6819+ AuErr1("/debug/aufs is uninitialized\n");
6820+ goto out;
6821+ }
6822+
6823+ err = -EIO;
6824+ sysaufs_name(sbinfo, name);
6825+ sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6826+ if (unlikely(!sbinfo->si_dbgaufs))
6827+ goto out;
6828+
6829+ /* regardless plink/noplink option */
6830+ sbinfo->si_dbgaufs_plink = debugfs_create_file
6831+ ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6832+ &dbgaufs_plink_fop);
6833+ if (unlikely(!sbinfo->si_dbgaufs_plink))
6834+ goto out_dir;
6835+
6836+ /* regardless xino/noxino option */
6837+ sbinfo->si_dbgaufs_xib = debugfs_create_file
6838+ ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6839+ &dbgaufs_xib_fop);
6840+ if (unlikely(!sbinfo->si_dbgaufs_xib))
6841+ goto out_dir;
6842+
6843+ err = dbgaufs_xigen_init(sbinfo);
6844+ if (!err)
6845+ goto out; /* success */
6846+
6847+out_dir:
6848+ dbgaufs_si_fin(sbinfo);
6849+out:
6850+ if (unlikely(err))
6851+ pr_err("debugfs/aufs failed\n");
6852+ return err;
6853+}
6854+
6855+/* ---------------------------------------------------------------------- */
6856+
6857+void dbgaufs_fin(void)
6858+{
6859+ debugfs_remove(dbgaufs);
6860+}
6861+
6862+int __init dbgaufs_init(void)
6863+{
6864+ int err;
6865+
6866+ err = -EIO;
6867+ dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6868+ if (dbgaufs)
6869+ err = 0;
6870+ return err;
6871+}
6872diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6873--- /usr/share/empty/fs/aufs/dbgaufs.h 1970-01-01 01:00:00.000000000 +0100
6874+++ linux/fs/aufs/dbgaufs.h 2021-02-22 22:51:59.058078584 +0100
6875@@ -0,0 +1,53 @@
6876+/* SPDX-License-Identifier: GPL-2.0 */
6877+/*
6878+ * Copyright (C) 2005-2020 Junjiro R. Okajima
6879+ *
6880+ * This program, aufs is free software; you can redistribute it and/or modify
6881+ * it under the terms of the GNU General Public License as published by
6882+ * the Free Software Foundation; either version 2 of the License, or
6883+ * (at your option) any later version.
6884+ *
6885+ * This program is distributed in the hope that it will be useful,
6886+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6887+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6888+ * GNU General Public License for more details.
6889+ *
6890+ * You should have received a copy of the GNU General Public License
6891+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
6892+ */
6893+
6894+/*
6895+ * debugfs interface
6896+ */
6897+
6898+#ifndef __DBGAUFS_H__
6899+#define __DBGAUFS_H__
6900+
6901+#ifdef __KERNEL__
6902+
6903+struct super_block;
6904+struct au_sbinfo;
6905+struct au_branch;
6906+
6907+#ifdef CONFIG_DEBUG_FS
6908+/* dbgaufs.c */
6909+void dbgaufs_xino_del(struct au_branch *br);
6910+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6911+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
6912+void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6913+int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6914+void dbgaufs_fin(void);
6915+int __init dbgaufs_init(void);
6916+#else
6917+AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
6918+AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6919+AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
6920+ int topdown)
6921+AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6922+AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6923+AuStubVoid(dbgaufs_fin, void)
6924+AuStubInt0(__init dbgaufs_init, void)
6925+#endif /* CONFIG_DEBUG_FS */
6926+
6927+#endif /* __KERNEL__ */
6928+#endif /* __DBGAUFS_H__ */
6929diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6930--- /usr/share/empty/fs/aufs/dcsub.c 1970-01-01 01:00:00.000000000 +0100
6931+++ linux/fs/aufs/dcsub.c 2021-02-22 22:51:59.058078584 +0100
6932@@ -0,0 +1,225 @@
6933+// SPDX-License-Identifier: GPL-2.0
6934+/*
6935+ * Copyright (C) 2005-2020 Junjiro R. Okajima
6936+ *
6937+ * This program, aufs is free software; you can redistribute it and/or modify
6938+ * it under the terms of the GNU General Public License as published by
6939+ * the Free Software Foundation; either version 2 of the License, or
6940+ * (at your option) any later version.
6941+ *
6942+ * This program is distributed in the hope that it will be useful,
6943+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6944+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6945+ * GNU General Public License for more details.
6946+ *
6947+ * You should have received a copy of the GNU General Public License
6948+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
6949+ */
6950+
6951+/*
6952+ * sub-routines for dentry cache
6953+ */
6954+
6955+#include "aufs.h"
6956+
6957+static void au_dpage_free(struct au_dpage *dpage)
6958+{
6959+ int i;
6960+ struct dentry **p;
6961+
6962+ p = dpage->dentries;
6963+ for (i = 0; i < dpage->ndentry; i++)
6964+ dput(*p++);
6965+ free_page((unsigned long)dpage->dentries);
6966+}
6967+
6968+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6969+{
6970+ int err;
6971+ void *p;
6972+
6973+ err = -ENOMEM;
6974+ dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6975+ if (unlikely(!dpages->dpages))
6976+ goto out;
6977+
6978+ p = (void *)__get_free_page(gfp);
6979+ if (unlikely(!p))
6980+ goto out_dpages;
6981+
6982+ dpages->dpages[0].ndentry = 0;
6983+ dpages->dpages[0].dentries = p;
6984+ dpages->ndpage = 1;
6985+ return 0; /* success */
6986+
6987+out_dpages:
6988+ au_kfree_try_rcu(dpages->dpages);
6989+out:
6990+ return err;
6991+}
6992+
6993+void au_dpages_free(struct au_dcsub_pages *dpages)
6994+{
6995+ int i;
6996+ struct au_dpage *p;
6997+
6998+ p = dpages->dpages;
6999+ for (i = 0; i < dpages->ndpage; i++)
7000+ au_dpage_free(p++);
7001+ au_kfree_try_rcu(dpages->dpages);
7002+}
7003+
7004+static int au_dpages_append(struct au_dcsub_pages *dpages,
7005+ struct dentry *dentry, gfp_t gfp)
7006+{
7007+ int err, sz;
7008+ struct au_dpage *dpage;
7009+ void *p;
7010+
7011+ dpage = dpages->dpages + dpages->ndpage - 1;
7012+ sz = PAGE_SIZE / sizeof(dentry);
7013+ if (unlikely(dpage->ndentry >= sz)) {
7014+ AuLabel(new dpage);
7015+ err = -ENOMEM;
7016+ sz = dpages->ndpage * sizeof(*dpages->dpages);
7017+ p = au_kzrealloc(dpages->dpages, sz,
7018+ sz + sizeof(*dpages->dpages), gfp,
7019+ /*may_shrink*/0);
7020+ if (unlikely(!p))
7021+ goto out;
7022+
7023+ dpages->dpages = p;
7024+ dpage = dpages->dpages + dpages->ndpage;
7025+ p = (void *)__get_free_page(gfp);
7026+ if (unlikely(!p))
7027+ goto out;
7028+
7029+ dpage->ndentry = 0;
7030+ dpage->dentries = p;
7031+ dpages->ndpage++;
7032+ }
7033+
7034+ AuDebugOn(au_dcount(dentry) <= 0);
7035+ dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
7036+ return 0; /* success */
7037+
7038+out:
7039+ return err;
7040+}
7041+
7042+/* todo: BAD approach */
7043+/* copied from linux/fs/dcache.c */
7044+enum d_walk_ret {
7045+ D_WALK_CONTINUE,
7046+ D_WALK_QUIT,
7047+ D_WALK_NORETRY,
7048+ D_WALK_SKIP,
7049+};
7050+
7051+extern void d_walk(struct dentry *parent, void *data,
7052+ enum d_walk_ret (*enter)(void *, struct dentry *));
7053+
7054+struct ac_dpages_arg {
7055+ int err;
7056+ struct au_dcsub_pages *dpages;
7057+ struct super_block *sb;
7058+ au_dpages_test test;
7059+ void *arg;
7060+};
7061+
7062+static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7063+{
7064+ enum d_walk_ret ret;
7065+ struct ac_dpages_arg *arg = _arg;
7066+
7067+ ret = D_WALK_CONTINUE;
7068+ if (dentry->d_sb == arg->sb
7069+ && !IS_ROOT(dentry)
7070+ && au_dcount(dentry) > 0
7071+ && au_di(dentry)
7072+ && (!arg->test || arg->test(dentry, arg->arg))) {
7073+ arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7074+ if (unlikely(arg->err))
7075+ ret = D_WALK_QUIT;
7076+ }
7077+
7078+ return ret;
7079+}
7080+
7081+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7082+ au_dpages_test test, void *arg)
7083+{
7084+ struct ac_dpages_arg args = {
7085+ .err = 0,
7086+ .dpages = dpages,
7087+ .sb = root->d_sb,
7088+ .test = test,
7089+ .arg = arg
7090+ };
7091+
7092+ d_walk(root, &args, au_call_dpages_append);
7093+
7094+ return args.err;
7095+}
7096+
7097+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7098+ int do_include, au_dpages_test test, void *arg)
7099+{
7100+ int err;
7101+
7102+ err = 0;
7103+ write_seqlock(&rename_lock);
7104+ spin_lock(&dentry->d_lock);
7105+ if (do_include
7106+ && au_dcount(dentry) > 0
7107+ && (!test || test(dentry, arg)))
7108+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7109+ spin_unlock(&dentry->d_lock);
7110+ if (unlikely(err))
7111+ goto out;
7112+
7113+ /*
7114+ * RCU for vfsmount is unnecessary since this is a traverse in a single
7115+ * mount
7116+ */
7117+ while (!IS_ROOT(dentry)) {
7118+ dentry = dentry->d_parent; /* rename_lock is locked */
7119+ spin_lock(&dentry->d_lock);
7120+ if (au_dcount(dentry) > 0
7121+ && (!test || test(dentry, arg)))
7122+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7123+ spin_unlock(&dentry->d_lock);
7124+ if (unlikely(err))
7125+ break;
7126+ }
7127+
7128+out:
7129+ write_sequnlock(&rename_lock);
7130+ return err;
7131+}
7132+
7133+static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7134+{
7135+ return au_di(dentry) && dentry->d_sb == arg;
7136+}
7137+
7138+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7139+ struct dentry *dentry, int do_include)
7140+{
7141+ return au_dcsub_pages_rev(dpages, dentry, do_include,
7142+ au_dcsub_dpages_aufs, dentry->d_sb);
7143+}
7144+
7145+int au_test_subdir(struct dentry *d1, struct dentry *d2)
7146+{
7147+ struct path path[2] = {
7148+ {
7149+ .dentry = d1
7150+ },
7151+ {
7152+ .dentry = d2
7153+ }
7154+ };
7155+
7156+ return path_is_under(path + 0, path + 1);
7157+}
7158diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7159--- /usr/share/empty/fs/aufs/dcsub.h 1970-01-01 01:00:00.000000000 +0100
7160+++ linux/fs/aufs/dcsub.h 2021-02-22 22:51:59.058078584 +0100
7161@@ -0,0 +1,137 @@
7162+/* SPDX-License-Identifier: GPL-2.0 */
7163+/*
7164+ * Copyright (C) 2005-2020 Junjiro R. Okajima
7165+ *
7166+ * This program, aufs is free software; you can redistribute it and/or modify
7167+ * it under the terms of the GNU General Public License as published by
7168+ * the Free Software Foundation; either version 2 of the License, or
7169+ * (at your option) any later version.
7170+ *
7171+ * This program is distributed in the hope that it will be useful,
7172+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7173+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7174+ * GNU General Public License for more details.
7175+ *
7176+ * You should have received a copy of the GNU General Public License
7177+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7178+ */
7179+
7180+/*
7181+ * sub-routines for dentry cache
7182+ */
7183+
7184+#ifndef __AUFS_DCSUB_H__
7185+#define __AUFS_DCSUB_H__
7186+
7187+#ifdef __KERNEL__
7188+
7189+#include <linux/dcache.h>
7190+#include <linux/fs.h>
7191+
7192+struct au_dpage {
7193+ int ndentry;
7194+ struct dentry **dentries;
7195+};
7196+
7197+struct au_dcsub_pages {
7198+ int ndpage;
7199+ struct au_dpage *dpages;
7200+};
7201+
7202+/* ---------------------------------------------------------------------- */
7203+
7204+/* dcsub.c */
7205+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7206+void au_dpages_free(struct au_dcsub_pages *dpages);
7207+typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7208+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7209+ au_dpages_test test, void *arg);
7210+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7211+ int do_include, au_dpages_test test, void *arg);
7212+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7213+ struct dentry *dentry, int do_include);
7214+int au_test_subdir(struct dentry *d1, struct dentry *d2);
7215+
7216+/* ---------------------------------------------------------------------- */
7217+
7218+/*
7219+ * todo: in linux-3.13, several similar (but faster) helpers are added to
7220+ * include/linux/dcache.h. Try them (in the future).
7221+ */
7222+
7223+static inline int au_d_hashed_positive(struct dentry *d)
7224+{
7225+ int err;
7226+ struct inode *inode = d_inode(d);
7227+
7228+ err = 0;
7229+ if (unlikely(d_unhashed(d)
7230+ || d_is_negative(d)
7231+ || !inode->i_nlink))
7232+ err = -ENOENT;
7233+ return err;
7234+}
7235+
7236+static inline int au_d_linkable(struct dentry *d)
7237+{
7238+ int err;
7239+ struct inode *inode = d_inode(d);
7240+
7241+ err = au_d_hashed_positive(d);
7242+ if (err
7243+ && d_is_positive(d)
7244+ && (inode->i_state & I_LINKABLE))
7245+ err = 0;
7246+ return err;
7247+}
7248+
7249+static inline int au_d_alive(struct dentry *d)
7250+{
7251+ int err;
7252+ struct inode *inode;
7253+
7254+ err = 0;
7255+ if (!IS_ROOT(d))
7256+ err = au_d_hashed_positive(d);
7257+ else {
7258+ inode = d_inode(d);
7259+ if (unlikely(d_unlinked(d)
7260+ || d_is_negative(d)
7261+ || !inode->i_nlink))
7262+ err = -ENOENT;
7263+ }
7264+ return err;
7265+}
7266+
7267+static inline int au_alive_dir(struct dentry *d)
7268+{
7269+ int err;
7270+
7271+ err = au_d_alive(d);
7272+ if (unlikely(err || IS_DEADDIR(d_inode(d))))
7273+ err = -ENOENT;
7274+ return err;
7275+}
7276+
7277+static inline int au_qstreq(struct qstr *a, struct qstr *b)
7278+{
7279+ return a->len == b->len
7280+ && !memcmp(a->name, b->name, a->len);
7281+}
7282+
7283+/*
7284+ * by the commit
7285+ * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7286+ * taking d_lock
7287+ * the type of d_lockref.count became int, but the inlined function d_count()
7288+ * still returns unsigned int.
7289+ * I don't know why. Maybe it is for every d_count() users?
7290+ * Anyway au_dcount() lives on.
7291+ */
7292+static inline int au_dcount(struct dentry *d)
7293+{
7294+ return (int)d_count(d);
7295+}
7296+
7297+#endif /* __KERNEL__ */
7298+#endif /* __AUFS_DCSUB_H__ */
7299diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7300--- /usr/share/empty/fs/aufs/debug.c 1970-01-01 01:00:00.000000000 +0100
7301+++ linux/fs/aufs/debug.c 2021-02-22 22:51:59.058078584 +0100
7302@@ -0,0 +1,441 @@
7303+// SPDX-License-Identifier: GPL-2.0
7304+/*
7305+ * Copyright (C) 2005-2020 Junjiro R. Okajima
7306+ *
7307+ * This program, aufs is free software; you can redistribute it and/or modify
7308+ * it under the terms of the GNU General Public License as published by
7309+ * the Free Software Foundation; either version 2 of the License, or
7310+ * (at your option) any later version.
7311+ *
7312+ * This program is distributed in the hope that it will be useful,
7313+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7314+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7315+ * GNU General Public License for more details.
7316+ *
7317+ * You should have received a copy of the GNU General Public License
7318+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7319+ */
7320+
7321+/*
7322+ * debug print functions
7323+ */
7324+
7325+#include <linux/iversion.h>
7326+#include "aufs.h"
7327+
7328+/* Returns 0, or -errno. arg is in kp->arg. */
7329+static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7330+{
7331+ int err, n;
7332+
7333+ err = kstrtoint(val, 0, &n);
7334+ if (!err) {
7335+ if (n > 0)
7336+ au_debug_on();
7337+ else
7338+ au_debug_off();
7339+ }
7340+ return err;
7341+}
7342+
7343+/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
7344+static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7345+{
7346+ atomic_t *a;
7347+
7348+ a = kp->arg;
7349+ return sprintf(buffer, "%d", atomic_read(a));
7350+}
7351+
7352+static struct kernel_param_ops param_ops_atomic_t = {
7353+ .set = param_atomic_t_set,
7354+ .get = param_atomic_t_get
7355+ /* void (*free)(void *arg) */
7356+};
7357+
7358+atomic_t aufs_debug = ATOMIC_INIT(0);
7359+MODULE_PARM_DESC(debug, "debug print");
7360+module_param_named(debug, aufs_debug, atomic_t, 0664);
7361+
7362+DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
7363+char *au_plevel = KERN_DEBUG;
7364+#define dpri(fmt, ...) do { \
7365+ if ((au_plevel \
7366+ && strcmp(au_plevel, KERN_DEBUG)) \
7367+ || au_debug_test()) \
7368+ printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
7369+} while (0)
7370+
7371+/* ---------------------------------------------------------------------- */
7372+
7373+void au_dpri_whlist(struct au_nhash *whlist)
7374+{
7375+ unsigned long ul, n;
7376+ struct hlist_head *head;
7377+ struct au_vdir_wh *pos;
7378+
7379+ n = whlist->nh_num;
7380+ head = whlist->nh_head;
7381+ for (ul = 0; ul < n; ul++) {
7382+ hlist_for_each_entry(pos, head, wh_hash)
7383+ dpri("b%d, %.*s, %d\n",
7384+ pos->wh_bindex,
7385+ pos->wh_str.len, pos->wh_str.name,
7386+ pos->wh_str.len);
7387+ head++;
7388+ }
7389+}
7390+
7391+void au_dpri_vdir(struct au_vdir *vdir)
7392+{
7393+ unsigned long ul;
7394+ union au_vdir_deblk_p p;
7395+ unsigned char *o;
7396+
7397+ if (!vdir || IS_ERR(vdir)) {
7398+ dpri("err %ld\n", PTR_ERR(vdir));
7399+ return;
7400+ }
7401+
7402+ dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
7403+ vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7404+ vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7405+ for (ul = 0; ul < vdir->vd_nblk; ul++) {
7406+ p.deblk = vdir->vd_deblk[ul];
7407+ o = p.deblk;
7408+ dpri("[%lu]: %p\n", ul, o);
7409+ }
7410+}
7411+
7412+static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7413+ struct dentry *wh)
7414+{
7415+ char *n = NULL;
7416+ int l = 0;
7417+
7418+ if (!inode || IS_ERR(inode)) {
7419+ dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7420+ return -1;
7421+ }
7422+
7423+ /* the type of i_blocks depends upon CONFIG_LBDAF */
7424+ BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7425+ && sizeof(inode->i_blocks) != sizeof(u64));
7426+ if (wh) {
7427+ n = (void *)wh->d_name.name;
7428+ l = wh->d_name.len;
7429+ }
7430+
7431+ dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7432+ " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7433+ bindex, inode,
7434+ inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7435+ atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7436+ i_size_read(inode), (unsigned long long)inode->i_blocks,
7437+ hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
7438+ inode->i_mapping ? inode->i_mapping->nrpages : 0,
7439+ inode->i_state, inode->i_flags, inode_peek_iversion(inode),
7440+ inode->i_generation,
7441+ l ? ", wh " : "", l, n);
7442+ return 0;
7443+}
7444+
7445+void au_dpri_inode(struct inode *inode)
7446+{
7447+ struct au_iinfo *iinfo;
7448+ struct au_hinode *hi;
7449+ aufs_bindex_t bindex;
7450+ int err, hn;
7451+
7452+ err = do_pri_inode(-1, inode, -1, NULL);
7453+ if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7454+ return;
7455+
7456+ iinfo = au_ii(inode);
7457+ dpri("i-1: btop %d, bbot %d, gen %d\n",
7458+ iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7459+ if (iinfo->ii_btop < 0)
7460+ return;
7461+ hn = 0;
7462+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7463+ hi = au_hinode(iinfo, bindex);
7464+ hn = !!au_hn(hi);
7465+ do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7466+ }
7467+}
7468+
7469+void au_dpri_dalias(struct inode *inode)
7470+{
7471+ struct dentry *d;
7472+
7473+ spin_lock(&inode->i_lock);
7474+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7475+ au_dpri_dentry(d);
7476+ spin_unlock(&inode->i_lock);
7477+}
7478+
7479+static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7480+{
7481+ struct dentry *wh = NULL;
7482+ int hn;
7483+ struct inode *inode;
7484+ struct au_iinfo *iinfo;
7485+ struct au_hinode *hi;
7486+
7487+ if (!dentry || IS_ERR(dentry)) {
7488+ dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7489+ return -1;
7490+ }
7491+ /* do not call dget_parent() here */
7492+ /* note: access d_xxx without d_lock */
7493+ dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7494+ bindex, dentry, dentry,
7495+ dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7496+ au_dcount(dentry), dentry->d_flags,
7497+ d_unhashed(dentry) ? "un" : "");
7498+ hn = -1;
7499+ inode = NULL;
7500+ if (d_is_positive(dentry))
7501+ inode = d_inode(dentry);
7502+ if (inode
7503+ && au_test_aufs(dentry->d_sb)
7504+ && bindex >= 0
7505+ && !au_is_bad_inode(inode)) {
7506+ iinfo = au_ii(inode);
7507+ hi = au_hinode(iinfo, bindex);
7508+ hn = !!au_hn(hi);
7509+ wh = hi->hi_whdentry;
7510+ }
7511+ do_pri_inode(bindex, inode, hn, wh);
7512+ return 0;
7513+}
7514+
7515+void au_dpri_dentry(struct dentry *dentry)
7516+{
7517+ struct au_dinfo *dinfo;
7518+ aufs_bindex_t bindex;
7519+ int err;
7520+
7521+ err = do_pri_dentry(-1, dentry);
7522+ if (err || !au_test_aufs(dentry->d_sb))
7523+ return;
7524+
7525+ dinfo = au_di(dentry);
7526+ if (!dinfo)
7527+ return;
7528+ dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7529+ dinfo->di_btop, dinfo->di_bbot,
7530+ dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7531+ dinfo->di_tmpfile);
7532+ if (dinfo->di_btop < 0)
7533+ return;
7534+ for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7535+ do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7536+}
7537+
7538+static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7539+{
7540+ char a[32];
7541+
7542+ if (!file || IS_ERR(file)) {
7543+ dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7544+ return -1;
7545+ }
7546+ a[0] = 0;
7547+ if (bindex < 0
7548+ && !IS_ERR_OR_NULL(file->f_path.dentry)
7549+ && au_test_aufs(file->f_path.dentry->d_sb)
7550+ && au_fi(file))
7551+ snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7552+ au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7553+ dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7554+ bindex, file->f_mode, file->f_flags, (long)file_count(file),
7555+ file->f_version, file->f_pos, a);
7556+ if (!IS_ERR_OR_NULL(file->f_path.dentry))
7557+ do_pri_dentry(bindex, file->f_path.dentry);
7558+ return 0;
7559+}
7560+
7561+void au_dpri_file(struct file *file)
7562+{
7563+ struct au_finfo *finfo;
7564+ struct au_fidir *fidir;
7565+ struct au_hfile *hfile;
7566+ aufs_bindex_t bindex;
7567+ int err;
7568+
7569+ err = do_pri_file(-1, file);
7570+ if (err
7571+ || IS_ERR_OR_NULL(file->f_path.dentry)
7572+ || !au_test_aufs(file->f_path.dentry->d_sb))
7573+ return;
7574+
7575+ finfo = au_fi(file);
7576+ if (!finfo)
7577+ return;
7578+ if (finfo->fi_btop < 0)
7579+ return;
7580+ fidir = finfo->fi_hdir;
7581+ if (!fidir)
7582+ do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7583+ else
7584+ for (bindex = finfo->fi_btop;
7585+ bindex >= 0 && bindex <= fidir->fd_bbot;
7586+ bindex++) {
7587+ hfile = fidir->fd_hfile + bindex;
7588+ do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7589+ }
7590+}
7591+
7592+static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7593+{
7594+ struct vfsmount *mnt;
7595+ struct super_block *sb;
7596+
7597+ if (!br || IS_ERR(br))
7598+ goto out;
7599+ mnt = au_br_mnt(br);
7600+ if (!mnt || IS_ERR(mnt))
7601+ goto out;
7602+ sb = mnt->mnt_sb;
7603+ if (!sb || IS_ERR(sb))
7604+ goto out;
7605+
7606+ dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
7607+ "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7608+ "xino %d\n",
7609+ bindex, br->br_perm, br->br_id, br->br_wbr,
7610+ au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7611+ sb->s_flags, sb->s_count,
7612+ atomic_read(&sb->s_active),
7613+ !!au_xino_file(br->br_xino, /*idx*/-1));
7614+ return 0;
7615+
7616+out:
7617+ dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7618+ return -1;
7619+}
7620+
7621+void au_dpri_sb(struct super_block *sb)
7622+{
7623+ struct au_sbinfo *sbinfo;
7624+ aufs_bindex_t bindex;
7625+ int err;
7626+ /* to reduce stack size */
7627+ struct {
7628+ struct vfsmount mnt;
7629+ struct au_branch fake;
7630+ } *a;
7631+
7632+ /* this function can be called from magic sysrq */
7633+ a = kzalloc(sizeof(*a), GFP_ATOMIC);
7634+ if (unlikely(!a)) {
7635+ dpri("no memory\n");
7636+ return;
7637+ }
7638+
7639+ a->mnt.mnt_sb = sb;
7640+ a->fake.br_path.mnt = &a->mnt;
7641+ err = do_pri_br(-1, &a->fake);
7642+ au_kfree_rcu(a);
7643+ dpri("dev 0x%x\n", sb->s_dev);
7644+ if (err || !au_test_aufs(sb))
7645+ return;
7646+
7647+ sbinfo = au_sbi(sb);
7648+ if (!sbinfo)
7649+ return;
7650+ dpri("nw %d, gen %u, kobj %d\n",
7651+ atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7652+ kref_read(&sbinfo->si_kobj.kref));
7653+ for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7654+ do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7655+}
7656+
7657+/* ---------------------------------------------------------------------- */
7658+
7659+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7660+{
7661+ struct inode *h_inode, *inode = d_inode(dentry);
7662+ struct dentry *h_dentry;
7663+ aufs_bindex_t bindex, bbot, bi;
7664+
7665+ if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7666+ return;
7667+
7668+ bbot = au_dbbot(dentry);
7669+ bi = au_ibbot(inode);
7670+ if (bi < bbot)
7671+ bbot = bi;
7672+ bindex = au_dbtop(dentry);
7673+ bi = au_ibtop(inode);
7674+ if (bi > bindex)
7675+ bindex = bi;
7676+
7677+ for (; bindex <= bbot; bindex++) {
7678+ h_dentry = au_h_dptr(dentry, bindex);
7679+ if (!h_dentry)
7680+ continue;
7681+ h_inode = au_h_iptr(inode, bindex);
7682+ if (unlikely(h_inode != d_inode(h_dentry))) {
7683+ au_debug_on();
7684+ AuDbg("b%d, %s:%d\n", bindex, func, line);
7685+ AuDbgDentry(dentry);
7686+ AuDbgInode(inode);
7687+ au_debug_off();
7688+ BUG();
7689+ }
7690+ }
7691+}
7692+
7693+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7694+{
7695+ int err, i, j;
7696+ struct au_dcsub_pages dpages;
7697+ struct au_dpage *dpage;
7698+ struct dentry **dentries;
7699+
7700+ err = au_dpages_init(&dpages, GFP_NOFS);
7701+ AuDebugOn(err);
7702+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7703+ AuDebugOn(err);
7704+ for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7705+ dpage = dpages.dpages + i;
7706+ dentries = dpage->dentries;
7707+ for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7708+ AuDebugOn(au_digen_test(dentries[j], sigen));
7709+ }
7710+ au_dpages_free(&dpages);
7711+}
7712+
7713+void au_dbg_verify_kthread(void)
7714+{
7715+ if (au_wkq_test()) {
7716+ au_dbg_blocked();
7717+ /*
7718+ * It may be recursive, but udba=notify between two aufs mounts,
7719+ * where a single ro branch is shared, is not a problem.
7720+ */
7721+ /* WARN_ON(1); */
7722+ }
7723+}
7724+
7725+/* ---------------------------------------------------------------------- */
7726+
7727+int __init au_debug_init(void)
7728+{
7729+ aufs_bindex_t bindex;
7730+ struct au_vdir_destr destr;
7731+
7732+ bindex = -1;
7733+ AuDebugOn(bindex >= 0);
7734+
7735+ destr.len = -1;
7736+ AuDebugOn(destr.len < NAME_MAX);
7737+
7738+#ifdef CONFIG_4KSTACKS
7739+ pr_warn("CONFIG_4KSTACKS is defined.\n");
7740+#endif
7741+
7742+ return 0;
7743+}
7744diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7745--- /usr/share/empty/fs/aufs/debug.h 1970-01-01 01:00:00.000000000 +0100
7746+++ linux/fs/aufs/debug.h 2021-02-22 22:51:59.058078584 +0100
7747@@ -0,0 +1,226 @@
7748+/* SPDX-License-Identifier: GPL-2.0 */
7749+/*
7750+ * Copyright (C) 2005-2020 Junjiro R. Okajima
7751+ *
7752+ * This program, aufs is free software; you can redistribute it and/or modify
7753+ * it under the terms of the GNU General Public License as published by
7754+ * the Free Software Foundation; either version 2 of the License, or
7755+ * (at your option) any later version.
7756+ *
7757+ * This program is distributed in the hope that it will be useful,
7758+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7759+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7760+ * GNU General Public License for more details.
7761+ *
7762+ * You should have received a copy of the GNU General Public License
7763+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7764+ */
7765+
7766+/*
7767+ * debug print functions
7768+ */
7769+
7770+#ifndef __AUFS_DEBUG_H__
7771+#define __AUFS_DEBUG_H__
7772+
7773+#ifdef __KERNEL__
7774+
7775+#include <linux/atomic.h>
7776+#include <linux/module.h>
7777+#include <linux/kallsyms.h>
7778+#include <linux/sysrq.h>
7779+
7780+#ifdef CONFIG_AUFS_DEBUG
7781+#define AuDebugOn(a) BUG_ON(a)
7782+
7783+/* module parameter */
7784+extern atomic_t aufs_debug;
7785+static inline void au_debug_on(void)
7786+{
7787+ atomic_inc(&aufs_debug);
7788+}
7789+static inline void au_debug_off(void)
7790+{
7791+ atomic_dec_if_positive(&aufs_debug);
7792+}
7793+
7794+static inline int au_debug_test(void)
7795+{
7796+ return atomic_read(&aufs_debug) > 0;
7797+}
7798+#else
7799+#define AuDebugOn(a) do {} while (0)
7800+AuStubVoid(au_debug_on, void)
7801+AuStubVoid(au_debug_off, void)
7802+AuStubInt0(au_debug_test, void)
7803+#endif /* CONFIG_AUFS_DEBUG */
7804+
7805+#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7806+
7807+/* ---------------------------------------------------------------------- */
7808+
7809+/* debug print */
7810+
7811+#define AuDbg(fmt, ...) do { \
7812+ if (au_debug_test()) \
7813+ pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7814+} while (0)
7815+#define AuLabel(l) AuDbg(#l "\n")
7816+#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7817+#define AuWarn1(fmt, ...) do { \
7818+ static unsigned char _c; \
7819+ if (!_c++) \
7820+ pr_warn(fmt, ##__VA_ARGS__); \
7821+} while (0)
7822+
7823+#define AuErr1(fmt, ...) do { \
7824+ static unsigned char _c; \
7825+ if (!_c++) \
7826+ pr_err(fmt, ##__VA_ARGS__); \
7827+} while (0)
7828+
7829+#define AuIOErr1(fmt, ...) do { \
7830+ static unsigned char _c; \
7831+ if (!_c++) \
7832+ AuIOErr(fmt, ##__VA_ARGS__); \
7833+} while (0)
7834+
7835+#define AuUnsupportMsg "This operation is not supported." \
7836+ " Please report this application to aufs-users ML."
7837+#define AuUnsupport(fmt, ...) do { \
7838+ pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7839+ dump_stack(); \
7840+} while (0)
7841+
7842+#define AuTraceErr(e) do { \
7843+ if (unlikely((e) < 0)) \
7844+ AuDbg("err %d\n", (int)(e)); \
7845+} while (0)
7846+
7847+#define AuTraceErrPtr(p) do { \
7848+ if (IS_ERR(p)) \
7849+ AuDbg("err %ld\n", PTR_ERR(p)); \
7850+} while (0)
7851+
7852+/* dirty macros for debug print, use with "%.*s" and caution */
7853+#define AuLNPair(qstr) (qstr)->len, (qstr)->name
7854+
7855+/* ---------------------------------------------------------------------- */
7856+
7857+struct dentry;
7858+#ifdef CONFIG_AUFS_DEBUG
7859+extern struct mutex au_dbg_mtx;
7860+extern char *au_plevel;
7861+struct au_nhash;
7862+void au_dpri_whlist(struct au_nhash *whlist);
7863+struct au_vdir;
7864+void au_dpri_vdir(struct au_vdir *vdir);
7865+struct inode;
7866+void au_dpri_inode(struct inode *inode);
7867+void au_dpri_dalias(struct inode *inode);
7868+void au_dpri_dentry(struct dentry *dentry);
7869+struct file;
7870+void au_dpri_file(struct file *filp);
7871+struct super_block;
7872+void au_dpri_sb(struct super_block *sb);
7873+
7874+#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7875+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7876+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7877+void au_dbg_verify_kthread(void);
7878+
7879+int __init au_debug_init(void);
7880+
7881+#define AuDbgWhlist(w) do { \
7882+ mutex_lock(&au_dbg_mtx); \
7883+ AuDbg(#w "\n"); \
7884+ au_dpri_whlist(w); \
7885+ mutex_unlock(&au_dbg_mtx); \
7886+} while (0)
7887+
7888+#define AuDbgVdir(v) do { \
7889+ mutex_lock(&au_dbg_mtx); \
7890+ AuDbg(#v "\n"); \
7891+ au_dpri_vdir(v); \
7892+ mutex_unlock(&au_dbg_mtx); \
7893+} while (0)
7894+
7895+#define AuDbgInode(i) do { \
7896+ mutex_lock(&au_dbg_mtx); \
7897+ AuDbg(#i "\n"); \
7898+ au_dpri_inode(i); \
7899+ mutex_unlock(&au_dbg_mtx); \
7900+} while (0)
7901+
7902+#define AuDbgDAlias(i) do { \
7903+ mutex_lock(&au_dbg_mtx); \
7904+ AuDbg(#i "\n"); \
7905+ au_dpri_dalias(i); \
7906+ mutex_unlock(&au_dbg_mtx); \
7907+} while (0)
7908+
7909+#define AuDbgDentry(d) do { \
7910+ mutex_lock(&au_dbg_mtx); \
7911+ AuDbg(#d "\n"); \
7912+ au_dpri_dentry(d); \
7913+ mutex_unlock(&au_dbg_mtx); \
7914+} while (0)
7915+
7916+#define AuDbgFile(f) do { \
7917+ mutex_lock(&au_dbg_mtx); \
7918+ AuDbg(#f "\n"); \
7919+ au_dpri_file(f); \
7920+ mutex_unlock(&au_dbg_mtx); \
7921+} while (0)
7922+
7923+#define AuDbgSb(sb) do { \
7924+ mutex_lock(&au_dbg_mtx); \
7925+ AuDbg(#sb "\n"); \
7926+ au_dpri_sb(sb); \
7927+ mutex_unlock(&au_dbg_mtx); \
7928+} while (0)
7929+
7930+#define AuDbgSym(addr) do { \
7931+ char sym[KSYM_SYMBOL_LEN]; \
7932+ sprint_symbol(sym, (unsigned long)addr); \
7933+ AuDbg("%s\n", sym); \
7934+} while (0)
7935+#else
7936+AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
7937+AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7938+AuStubVoid(au_dbg_verify_kthread, void)
7939+AuStubInt0(__init au_debug_init, void)
7940+
7941+#define AuDbgWhlist(w) do {} while (0)
7942+#define AuDbgVdir(v) do {} while (0)
7943+#define AuDbgInode(i) do {} while (0)
7944+#define AuDbgDAlias(i) do {} while (0)
7945+#define AuDbgDentry(d) do {} while (0)
7946+#define AuDbgFile(f) do {} while (0)
7947+#define AuDbgSb(sb) do {} while (0)
7948+#define AuDbgSym(addr) do {} while (0)
7949+#endif /* CONFIG_AUFS_DEBUG */
7950+
7951+/* ---------------------------------------------------------------------- */
7952+
7953+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7954+int __init au_sysrq_init(void);
7955+void au_sysrq_fin(void);
7956+
7957+#ifdef CONFIG_HW_CONSOLE
7958+#define au_dbg_blocked() do { \
7959+ WARN_ON(1); \
7960+ handle_sysrq('w'); \
7961+} while (0)
7962+#else
7963+AuStubVoid(au_dbg_blocked, void)
7964+#endif
7965+
7966+#else
7967+AuStubInt0(__init au_sysrq_init, void)
7968+AuStubVoid(au_sysrq_fin, void)
7969+AuStubVoid(au_dbg_blocked, void)
7970+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7971+
7972+#endif /* __KERNEL__ */
7973+#endif /* __AUFS_DEBUG_H__ */
7974diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
7975--- /usr/share/empty/fs/aufs/dentry.c 1970-01-01 01:00:00.000000000 +0100
7976+++ linux/fs/aufs/dentry.c 2021-02-22 22:51:59.058078584 +0100
7977@@ -0,0 +1,1154 @@
7978+// SPDX-License-Identifier: GPL-2.0
7979+/*
7980+ * Copyright (C) 2005-2020 Junjiro R. Okajima
7981+ *
7982+ * This program, aufs is free software; you can redistribute it and/or modify
7983+ * it under the terms of the GNU General Public License as published by
7984+ * the Free Software Foundation; either version 2 of the License, or
7985+ * (at your option) any later version.
7986+ *
7987+ * This program is distributed in the hope that it will be useful,
7988+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7989+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7990+ * GNU General Public License for more details.
7991+ *
7992+ * You should have received a copy of the GNU General Public License
7993+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7994+ */
7995+
7996+/*
7997+ * lookup and dentry operations
7998+ */
7999+
8000+#include <linux/iversion.h>
8001+#include <linux/namei.h>
8002+#include "aufs.h"
8003+
8004+/*
8005+ * returns positive/negative dentry, NULL or an error.
8006+ * NULL means whiteout-ed or not-found.
8007+ */
8008+static struct dentry*
8009+au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
8010+ aufs_bindex_t bindex, struct au_do_lookup_args *args)
8011+{
8012+ struct dentry *h_dentry;
8013+ struct inode *h_inode;
8014+ struct au_branch *br;
8015+ int wh_found, opq;
8016+ unsigned char wh_able;
8017+ const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
8018+ const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8019+ IGNORE_PERM);
8020+
8021+ wh_found = 0;
8022+ br = au_sbr(dentry->d_sb, bindex);
8023+ wh_able = !!au_br_whable(br->br_perm);
8024+ if (wh_able)
8025+ wh_found = au_wh_test(h_parent, &args->whname, ignore_perm);
8026+ h_dentry = ERR_PTR(wh_found);
8027+ if (!wh_found)
8028+ goto real_lookup;
8029+ if (unlikely(wh_found < 0))
8030+ goto out;
8031+
8032+ /* We found a whiteout */
8033+ /* au_set_dbbot(dentry, bindex); */
8034+ au_set_dbwh(dentry, bindex);
8035+ if (!allow_neg)
8036+ return NULL; /* success */
8037+
8038+real_lookup:
8039+ if (!ignore_perm)
8040+ h_dentry = vfsub_lkup_one(args->name, h_parent);
8041+ else
8042+ h_dentry = au_sio_lkup_one(args->name, h_parent);
8043+ if (IS_ERR(h_dentry)) {
8044+ if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8045+ && !allow_neg)
8046+ h_dentry = NULL;
8047+ goto out;
8048+ }
8049+
8050+ h_inode = d_inode(h_dentry);
8051+ if (d_is_negative(h_dentry)) {
8052+ if (!allow_neg)
8053+ goto out_neg;
8054+ } else if (wh_found
8055+ || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8056+ goto out_neg;
8057+ else if (au_ftest_lkup(args->flags, DIRREN)
8058+ /* && h_inode */
8059+ && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8060+ AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8061+ (unsigned long long)h_inode->i_ino);
8062+ goto out_neg;
8063+ }
8064+
8065+ if (au_dbbot(dentry) <= bindex)
8066+ au_set_dbbot(dentry, bindex);
8067+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8068+ au_set_dbtop(dentry, bindex);
8069+ au_set_h_dptr(dentry, bindex, h_dentry);
8070+
8071+ if (!d_is_dir(h_dentry)
8072+ || !wh_able
8073+ || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
8074+ goto out; /* success */
8075+
8076+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
8077+ opq = au_diropq_test(h_dentry);
8078+ inode_unlock_shared(h_inode);
8079+ if (opq > 0)
8080+ au_set_dbdiropq(dentry, bindex);
8081+ else if (unlikely(opq < 0)) {
8082+ au_set_h_dptr(dentry, bindex, NULL);
8083+ h_dentry = ERR_PTR(opq);
8084+ }
8085+ goto out;
8086+
8087+out_neg:
8088+ dput(h_dentry);
8089+ h_dentry = NULL;
8090+out:
8091+ return h_dentry;
8092+}
8093+
8094+static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8095+{
8096+ if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8097+ && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8098+ return -EPERM;
8099+ return 0;
8100+}
8101+
8102+/*
8103+ * returns the number of lower positive dentries,
8104+ * otherwise an error.
8105+ * can be called at unlinking with @type is zero.
8106+ */
8107+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8108+ unsigned int flags)
8109+{
8110+ int npositive, err;
8111+ aufs_bindex_t bindex, btail, bdiropq;
8112+ unsigned char isdir, dirperm1, dirren;
8113+ struct au_do_lookup_args args = {
8114+ .flags = flags,
8115+ .name = &dentry->d_name
8116+ };
8117+ struct dentry *parent;
8118+ struct super_block *sb;
8119+
8120+ sb = dentry->d_sb;
8121+ err = au_test_shwh(sb, args.name);
8122+ if (unlikely(err))
8123+ goto out;
8124+
8125+ err = au_wh_name_alloc(&args.whname, args.name);
8126+ if (unlikely(err))
8127+ goto out;
8128+
8129+ isdir = !!d_is_dir(dentry);
8130+ dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8131+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8132+ if (dirren)
8133+ au_fset_lkup(args.flags, DIRREN);
8134+
8135+ npositive = 0;
8136+ parent = dget_parent(dentry);
8137+ btail = au_dbtaildir(parent);
8138+ for (bindex = btop; bindex <= btail; bindex++) {
8139+ struct dentry *h_parent, *h_dentry;
8140+ struct inode *h_inode, *h_dir;
8141+ struct au_branch *br;
8142+
8143+ h_dentry = au_h_dptr(dentry, bindex);
8144+ if (h_dentry) {
8145+ if (d_is_positive(h_dentry))
8146+ npositive++;
8147+ break;
8148+ }
8149+ h_parent = au_h_dptr(parent, bindex);
8150+ if (!h_parent || !d_is_dir(h_parent))
8151+ continue;
8152+
8153+ if (dirren) {
8154+ /* if the inum matches, then use the prepared name */
8155+ err = au_dr_lkup_name(&args, bindex);
8156+ if (unlikely(err))
8157+ goto out_parent;
8158+ }
8159+
8160+ h_dir = d_inode(h_parent);
8161+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8162+ h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
8163+ inode_unlock_shared(h_dir);
8164+ err = PTR_ERR(h_dentry);
8165+ if (IS_ERR(h_dentry))
8166+ goto out_parent;
8167+ if (h_dentry)
8168+ au_fclr_lkup(args.flags, ALLOW_NEG);
8169+ if (dirperm1)
8170+ au_fset_lkup(args.flags, IGNORE_PERM);
8171+
8172+ if (au_dbwh(dentry) == bindex)
8173+ break;
8174+ if (!h_dentry)
8175+ continue;
8176+ if (d_is_negative(h_dentry))
8177+ continue;
8178+ h_inode = d_inode(h_dentry);
8179+ npositive++;
8180+ if (!args.type)
8181+ args.type = h_inode->i_mode & S_IFMT;
8182+ if (args.type != S_IFDIR)
8183+ break;
8184+ else if (isdir) {
8185+ /* the type of lower may be different */
8186+ bdiropq = au_dbdiropq(dentry);
8187+ if (bdiropq >= 0 && bdiropq <= bindex)
8188+ break;
8189+ }
8190+ br = au_sbr(sb, bindex);
8191+ if (dirren
8192+ && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8193+ /*add_ent*/NULL)) {
8194+ /* prepare next name to lookup */
8195+ err = au_dr_lkup(&args, dentry, bindex);
8196+ if (unlikely(err))
8197+ goto out_parent;
8198+ }
8199+ }
8200+
8201+ if (npositive) {
8202+ AuLabel(positive);
8203+ au_update_dbtop(dentry);
8204+ }
8205+ err = npositive;
8206+ if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
8207+ && au_dbtop(dentry) < 0)) {
8208+ err = -EIO;
8209+ AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8210+ dentry, err);
8211+ }
8212+
8213+out_parent:
8214+ dput(parent);
8215+ au_kfree_try_rcu(args.whname.name);
8216+ if (dirren)
8217+ au_dr_lkup_fin(&args);
8218+out:
8219+ return err;
8220+}
8221+
8222+struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
8223+{
8224+ struct dentry *dentry;
8225+ int wkq_err;
8226+
8227+ if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
8228+ dentry = vfsub_lkup_one(name, parent);
8229+ else {
8230+ struct vfsub_lkup_one_args args = {
8231+ .errp = &dentry,
8232+ .name = name,
8233+ .parent = parent
8234+ };
8235+
8236+ wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
8237+ if (unlikely(wkq_err))
8238+ dentry = ERR_PTR(wkq_err);
8239+ }
8240+
8241+ return dentry;
8242+}
8243+
8244+/*
8245+ * lookup @dentry on @bindex which should be negative.
8246+ */
8247+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
8248+{
8249+ int err;
8250+ struct dentry *parent, *h_parent, *h_dentry;
8251+ struct au_branch *br;
8252+
8253+ parent = dget_parent(dentry);
8254+ h_parent = au_h_dptr(parent, bindex);
8255+ br = au_sbr(dentry->d_sb, bindex);
8256+ if (wh)
8257+ h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
8258+ else
8259+ h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
8260+ err = PTR_ERR(h_dentry);
8261+ if (IS_ERR(h_dentry))
8262+ goto out;
8263+ if (unlikely(d_is_positive(h_dentry))) {
8264+ err = -EIO;
8265+ AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
8266+ dput(h_dentry);
8267+ goto out;
8268+ }
8269+
8270+ err = 0;
8271+ if (bindex < au_dbtop(dentry))
8272+ au_set_dbtop(dentry, bindex);
8273+ if (au_dbbot(dentry) < bindex)
8274+ au_set_dbbot(dentry, bindex);
8275+ au_set_h_dptr(dentry, bindex, h_dentry);
8276+
8277+out:
8278+ dput(parent);
8279+ return err;
8280+}
8281+
8282+/* ---------------------------------------------------------------------- */
8283+
8284+/* subset of struct inode */
8285+struct au_iattr {
8286+ unsigned long i_ino;
8287+ /* unsigned int i_nlink; */
8288+ kuid_t i_uid;
8289+ kgid_t i_gid;
8290+ u64 i_version;
8291+/*
8292+ loff_t i_size;
8293+ blkcnt_t i_blocks;
8294+*/
8295+ umode_t i_mode;
8296+};
8297+
8298+static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8299+{
8300+ ia->i_ino = h_inode->i_ino;
8301+ /* ia->i_nlink = h_inode->i_nlink; */
8302+ ia->i_uid = h_inode->i_uid;
8303+ ia->i_gid = h_inode->i_gid;
8304+ ia->i_version = inode_query_iversion(h_inode);
8305+/*
8306+ ia->i_size = h_inode->i_size;
8307+ ia->i_blocks = h_inode->i_blocks;
8308+*/
8309+ ia->i_mode = (h_inode->i_mode & S_IFMT);
8310+}
8311+
8312+static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8313+{
8314+ return ia->i_ino != h_inode->i_ino
8315+ /* || ia->i_nlink != h_inode->i_nlink */
8316+ || !uid_eq(ia->i_uid, h_inode->i_uid)
8317+ || !gid_eq(ia->i_gid, h_inode->i_gid)
8318+ || !inode_eq_iversion(h_inode, ia->i_version)
8319+/*
8320+ || ia->i_size != h_inode->i_size
8321+ || ia->i_blocks != h_inode->i_blocks
8322+*/
8323+ || ia->i_mode != (h_inode->i_mode & S_IFMT);
8324+}
8325+
8326+static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8327+ struct au_branch *br)
8328+{
8329+ int err;
8330+ struct au_iattr ia;
8331+ struct inode *h_inode;
8332+ struct dentry *h_d;
8333+ struct super_block *h_sb;
8334+
8335+ err = 0;
8336+ memset(&ia, -1, sizeof(ia));
8337+ h_sb = h_dentry->d_sb;
8338+ h_inode = NULL;
8339+ if (d_is_positive(h_dentry)) {
8340+ h_inode = d_inode(h_dentry);
8341+ au_iattr_save(&ia, h_inode);
8342+ } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8343+ /* nfs d_revalidate may return 0 for negative dentry */
8344+ /* fuse d_revalidate always return 0 for negative dentry */
8345+ goto out;
8346+
8347+ /* main purpose is namei.c:cached_lookup() and d_revalidate */
8348+ h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
8349+ err = PTR_ERR(h_d);
8350+ if (IS_ERR(h_d))
8351+ goto out;
8352+
8353+ err = 0;
8354+ if (unlikely(h_d != h_dentry
8355+ || d_inode(h_d) != h_inode
8356+ || (h_inode && au_iattr_test(&ia, h_inode))))
8357+ err = au_busy_or_stale();
8358+ dput(h_d);
8359+
8360+out:
8361+ AuTraceErr(err);
8362+ return err;
8363+}
8364+
8365+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8366+ struct dentry *h_parent, struct au_branch *br)
8367+{
8368+ int err;
8369+
8370+ err = 0;
8371+ if (udba == AuOpt_UDBA_REVAL
8372+ && !au_test_fs_remote(h_dentry->d_sb)) {
8373+ IMustLock(h_dir);
8374+ err = (d_inode(h_dentry->d_parent) != h_dir);
8375+ } else if (udba != AuOpt_UDBA_NONE)
8376+ err = au_h_verify_dentry(h_dentry, h_parent, br);
8377+
8378+ return err;
8379+}
8380+
8381+/* ---------------------------------------------------------------------- */
8382+
8383+static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8384+{
8385+ int err;
8386+ aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8387+ struct au_hdentry tmp, *p, *q;
8388+ struct au_dinfo *dinfo;
8389+ struct super_block *sb;
8390+
8391+ DiMustWriteLock(dentry);
8392+
8393+ sb = dentry->d_sb;
8394+ dinfo = au_di(dentry);
8395+ bbot = dinfo->di_bbot;
8396+ bwh = dinfo->di_bwh;
8397+ bdiropq = dinfo->di_bdiropq;
8398+ bindex = dinfo->di_btop;
8399+ p = au_hdentry(dinfo, bindex);
8400+ for (; bindex <= bbot; bindex++, p++) {
8401+ if (!p->hd_dentry)
8402+ continue;
8403+
8404+ new_bindex = au_br_index(sb, p->hd_id);
8405+ if (new_bindex == bindex)
8406+ continue;
8407+
8408+ if (dinfo->di_bwh == bindex)
8409+ bwh = new_bindex;
8410+ if (dinfo->di_bdiropq == bindex)
8411+ bdiropq = new_bindex;
8412+ if (new_bindex < 0) {
8413+ au_hdput(p);
8414+ p->hd_dentry = NULL;
8415+ continue;
8416+ }
8417+
8418+ /* swap two lower dentries, and loop again */
8419+ q = au_hdentry(dinfo, new_bindex);
8420+ tmp = *q;
8421+ *q = *p;
8422+ *p = tmp;
8423+ if (tmp.hd_dentry) {
8424+ bindex--;
8425+ p--;
8426+ }
8427+ }
8428+
8429+ dinfo->di_bwh = -1;
8430+ if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8431+ dinfo->di_bwh = bwh;
8432+
8433+ dinfo->di_bdiropq = -1;
8434+ if (bdiropq >= 0
8435+ && bdiropq <= au_sbbot(sb)
8436+ && au_sbr_whable(sb, bdiropq))
8437+ dinfo->di_bdiropq = bdiropq;
8438+
8439+ err = -EIO;
8440+ dinfo->di_btop = -1;
8441+ dinfo->di_bbot = -1;
8442+ bbot = au_dbbot(parent);
8443+ bindex = 0;
8444+ p = au_hdentry(dinfo, bindex);
8445+ for (; bindex <= bbot; bindex++, p++)
8446+ if (p->hd_dentry) {
8447+ dinfo->di_btop = bindex;
8448+ break;
8449+ }
8450+
8451+ if (dinfo->di_btop >= 0) {
8452+ bindex = bbot;
8453+ p = au_hdentry(dinfo, bindex);
8454+ for (; bindex >= 0; bindex--, p--)
8455+ if (p->hd_dentry) {
8456+ dinfo->di_bbot = bindex;
8457+ err = 0;
8458+ break;
8459+ }
8460+ }
8461+
8462+ return err;
8463+}
8464+
8465+static void au_do_hide(struct dentry *dentry)
8466+{
8467+ struct inode *inode;
8468+
8469+ if (d_really_is_positive(dentry)) {
8470+ inode = d_inode(dentry);
8471+ if (!d_is_dir(dentry)) {
8472+ if (inode->i_nlink && !d_unhashed(dentry))
8473+ drop_nlink(inode);
8474+ } else {
8475+ clear_nlink(inode);
8476+ /* stop next lookup */
8477+ inode->i_flags |= S_DEAD;
8478+ }
8479+ smp_mb(); /* necessary? */
8480+ }
8481+ d_drop(dentry);
8482+}
8483+
8484+static int au_hide_children(struct dentry *parent)
8485+{
8486+ int err, i, j, ndentry;
8487+ struct au_dcsub_pages dpages;
8488+ struct au_dpage *dpage;
8489+ struct dentry *dentry;
8490+
8491+ err = au_dpages_init(&dpages, GFP_NOFS);
8492+ if (unlikely(err))
8493+ goto out;
8494+ err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8495+ if (unlikely(err))
8496+ goto out_dpages;
8497+
8498+ /* in reverse order */
8499+ for (i = dpages.ndpage - 1; i >= 0; i--) {
8500+ dpage = dpages.dpages + i;
8501+ ndentry = dpage->ndentry;
8502+ for (j = ndentry - 1; j >= 0; j--) {
8503+ dentry = dpage->dentries[j];
8504+ if (dentry != parent)
8505+ au_do_hide(dentry);
8506+ }
8507+ }
8508+
8509+out_dpages:
8510+ au_dpages_free(&dpages);
8511+out:
8512+ return err;
8513+}
8514+
8515+static void au_hide(struct dentry *dentry)
8516+{
8517+ int err;
8518+
8519+ AuDbgDentry(dentry);
8520+ if (d_is_dir(dentry)) {
8521+ /* shrink_dcache_parent(dentry); */
8522+ err = au_hide_children(dentry);
8523+ if (unlikely(err))
8524+ AuIOErr("%pd, failed hiding children, ignored %d\n",
8525+ dentry, err);
8526+ }
8527+ au_do_hide(dentry);
8528+}
8529+
8530+/*
8531+ * By adding a dirty branch, a cached dentry may be affected in various ways.
8532+ *
8533+ * a dirty branch is added
8534+ * - on the top of layers
8535+ * - in the middle of layers
8536+ * - to the bottom of layers
8537+ *
8538+ * on the added branch there exists
8539+ * - a whiteout
8540+ * - a diropq
8541+ * - a same named entry
8542+ * + exist
8543+ * * negative --> positive
8544+ * * positive --> positive
8545+ * - type is unchanged
8546+ * - type is changed
8547+ * + doesn't exist
8548+ * * negative --> negative
8549+ * * positive --> negative (rejected by au_br_del() for non-dir case)
8550+ * - none
8551+ */
8552+static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8553+ struct au_dinfo *tmp)
8554+{
8555+ int err;
8556+ aufs_bindex_t bindex, bbot;
8557+ struct {
8558+ struct dentry *dentry;
8559+ struct inode *inode;
8560+ mode_t mode;
8561+ } orig_h, tmp_h = {
8562+ .dentry = NULL
8563+ };
8564+ struct au_hdentry *hd;
8565+ struct inode *inode, *h_inode;
8566+ struct dentry *h_dentry;
8567+
8568+ err = 0;
8569+ AuDebugOn(dinfo->di_btop < 0);
8570+ orig_h.mode = 0;
8571+ orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8572+ orig_h.inode = NULL;
8573+ if (d_is_positive(orig_h.dentry)) {
8574+ orig_h.inode = d_inode(orig_h.dentry);
8575+ orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8576+ }
8577+ if (tmp->di_btop >= 0) {
8578+ tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8579+ if (d_is_positive(tmp_h.dentry)) {
8580+ tmp_h.inode = d_inode(tmp_h.dentry);
8581+ tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8582+ }
8583+ }
8584+
8585+ inode = NULL;
8586+ if (d_really_is_positive(dentry))
8587+ inode = d_inode(dentry);
8588+ if (!orig_h.inode) {
8589+ AuDbg("negative originally\n");
8590+ if (inode) {
8591+ au_hide(dentry);
8592+ goto out;
8593+ }
8594+ AuDebugOn(inode);
8595+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8596+ AuDebugOn(dinfo->di_bdiropq != -1);
8597+
8598+ if (!tmp_h.inode) {
8599+ AuDbg("negative --> negative\n");
8600+ /* should have only one negative lower */
8601+ if (tmp->di_btop >= 0
8602+ && tmp->di_btop < dinfo->di_btop) {
8603+ AuDebugOn(tmp->di_btop != tmp->di_bbot);
8604+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8605+ au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8606+ au_di_cp(dinfo, tmp);
8607+ hd = au_hdentry(tmp, tmp->di_btop);
8608+ au_set_h_dptr(dentry, tmp->di_btop,
8609+ dget(hd->hd_dentry));
8610+ }
8611+ au_dbg_verify_dinode(dentry);
8612+ } else {
8613+ AuDbg("negative --> positive\n");
8614+ /*
8615+ * similar to the behaviour of creating with bypassing
8616+ * aufs.
8617+ * unhash it in order to force an error in the
8618+ * succeeding create operation.
8619+ * we should not set S_DEAD here.
8620+ */
8621+ d_drop(dentry);
8622+ /* au_di_swap(tmp, dinfo); */
8623+ au_dbg_verify_dinode(dentry);
8624+ }
8625+ } else {
8626+ AuDbg("positive originally\n");
8627+ /* inode may be NULL */
8628+ AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8629+ if (!tmp_h.inode) {
8630+ AuDbg("positive --> negative\n");
8631+ /* or bypassing aufs */
8632+ au_hide(dentry);
8633+ if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8634+ dinfo->di_bwh = tmp->di_bwh;
8635+ if (inode)
8636+ err = au_refresh_hinode_self(inode);
8637+ au_dbg_verify_dinode(dentry);
8638+ } else if (orig_h.mode == tmp_h.mode) {
8639+ AuDbg("positive --> positive, same type\n");
8640+ if (!S_ISDIR(orig_h.mode)
8641+ && dinfo->di_btop > tmp->di_btop) {
8642+ /*
8643+ * similar to the behaviour of removing and
8644+ * creating.
8645+ */
8646+ au_hide(dentry);
8647+ if (inode)
8648+ err = au_refresh_hinode_self(inode);
8649+ au_dbg_verify_dinode(dentry);
8650+ } else {
8651+ /* fill empty slots */
8652+ if (dinfo->di_btop > tmp->di_btop)
8653+ dinfo->di_btop = tmp->di_btop;
8654+ if (dinfo->di_bbot < tmp->di_bbot)
8655+ dinfo->di_bbot = tmp->di_bbot;
8656+ dinfo->di_bwh = tmp->di_bwh;
8657+ dinfo->di_bdiropq = tmp->di_bdiropq;
8658+ bbot = dinfo->di_bbot;
8659+ bindex = tmp->di_btop;
8660+ hd = au_hdentry(tmp, bindex);
8661+ for (; bindex <= bbot; bindex++, hd++) {
8662+ if (au_h_dptr(dentry, bindex))
8663+ continue;
8664+ h_dentry = hd->hd_dentry;
8665+ if (!h_dentry)
8666+ continue;
8667+ AuDebugOn(d_is_negative(h_dentry));
8668+ h_inode = d_inode(h_dentry);
8669+ AuDebugOn(orig_h.mode
8670+ != (h_inode->i_mode
8671+ & S_IFMT));
8672+ au_set_h_dptr(dentry, bindex,
8673+ dget(h_dentry));
8674+ }
8675+ if (inode)
8676+ err = au_refresh_hinode(inode, dentry);
8677+ au_dbg_verify_dinode(dentry);
8678+ }
8679+ } else {
8680+ AuDbg("positive --> positive, different type\n");
8681+ /* similar to the behaviour of removing and creating */
8682+ au_hide(dentry);
8683+ if (inode)
8684+ err = au_refresh_hinode_self(inode);
8685+ au_dbg_verify_dinode(dentry);
8686+ }
8687+ }
8688+
8689+out:
8690+ return err;
8691+}
8692+
8693+void au_refresh_dop(struct dentry *dentry, int force_reval)
8694+{
8695+ const struct dentry_operations *dop
8696+ = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8697+ static const unsigned int mask
8698+ = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8699+
8700+ BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8701+
8702+ if (dentry->d_op == dop)
8703+ return;
8704+
8705+ AuDbg("%pd\n", dentry);
8706+ spin_lock(&dentry->d_lock);
8707+ if (dop == &aufs_dop)
8708+ dentry->d_flags |= mask;
8709+ else
8710+ dentry->d_flags &= ~mask;
8711+ dentry->d_op = dop;
8712+ spin_unlock(&dentry->d_lock);
8713+}
8714+
8715+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8716+{
8717+ int err, ebrange, nbr;
8718+ unsigned int sigen;
8719+ struct au_dinfo *dinfo, *tmp;
8720+ struct super_block *sb;
8721+ struct inode *inode;
8722+
8723+ DiMustWriteLock(dentry);
8724+ AuDebugOn(IS_ROOT(dentry));
8725+ AuDebugOn(d_really_is_negative(parent));
8726+
8727+ sb = dentry->d_sb;
8728+ sigen = au_sigen(sb);
8729+ err = au_digen_test(parent, sigen);
8730+ if (unlikely(err))
8731+ goto out;
8732+
8733+ nbr = au_sbbot(sb) + 1;
8734+ dinfo = au_di(dentry);
8735+ err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8736+ if (unlikely(err))
8737+ goto out;
8738+ ebrange = au_dbrange_test(dentry);
8739+ if (!ebrange)
8740+ ebrange = au_do_refresh_hdentry(dentry, parent);
8741+
8742+ if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8743+ AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8744+ if (d_really_is_positive(dentry)) {
8745+ inode = d_inode(dentry);
8746+ err = au_refresh_hinode_self(inode);
8747+ }
8748+ au_dbg_verify_dinode(dentry);
8749+ if (!err)
8750+ goto out_dgen; /* success */
8751+ goto out;
8752+ }
8753+
8754+ /* temporary dinfo */
8755+ AuDbgDentry(dentry);
8756+ err = -ENOMEM;
8757+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8758+ if (unlikely(!tmp))
8759+ goto out;
8760+ au_di_swap(tmp, dinfo);
8761+ /* returns the number of positive dentries */
8762+ /*
8763+ * if current working dir is removed, it returns an error.
8764+ * but the dentry is legal.
8765+ */
8766+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8767+ AuDbgDentry(dentry);
8768+ au_di_swap(tmp, dinfo);
8769+ if (err == -ENOENT)
8770+ err = 0;
8771+ if (err >= 0) {
8772+ /* compare/refresh by dinfo */
8773+ AuDbgDentry(dentry);
8774+ err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8775+ au_dbg_verify_dinode(dentry);
8776+ AuTraceErr(err);
8777+ }
8778+ au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8779+ au_rw_write_unlock(&tmp->di_rwsem);
8780+ au_di_free(tmp);
8781+ if (unlikely(err))
8782+ goto out;
8783+
8784+out_dgen:
8785+ au_update_digen(dentry);
8786+out:
8787+ if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8788+ AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8789+ AuDbgDentry(dentry);
8790+ }
8791+ AuTraceErr(err);
8792+ return err;
8793+}
8794+
8795+static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8796+ struct dentry *dentry, aufs_bindex_t bindex)
8797+{
8798+ int err, valid;
8799+
8800+ err = 0;
8801+ if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8802+ goto out;
8803+
8804+ AuDbg("b%d\n", bindex);
8805+ /*
8806+ * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8807+ * due to whiteout and branch permission.
8808+ */
8809+ flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8810+ | LOOKUP_FOLLOW | LOOKUP_EXCL);
8811+ /* it may return tri-state */
8812+ valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8813+
8814+ if (unlikely(valid < 0))
8815+ err = valid;
8816+ else if (!valid)
8817+ err = -EINVAL;
8818+
8819+out:
8820+ AuTraceErr(err);
8821+ return err;
8822+}
8823+
8824+/* todo: remove this */
8825+static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8826+ unsigned int flags, int do_udba, int dirren)
8827+{
8828+ int err;
8829+ umode_t mode, h_mode;
8830+ aufs_bindex_t bindex, btail, btop, ibs, ibe;
8831+ unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8832+ struct inode *h_inode, *h_cached_inode;
8833+ struct dentry *h_dentry;
8834+ struct qstr *name, *h_name;
8835+
8836+ err = 0;
8837+ plus = 0;
8838+ mode = 0;
8839+ ibs = -1;
8840+ ibe = -1;
8841+ unhashed = !!d_unhashed(dentry);
8842+ is_root = !!IS_ROOT(dentry);
8843+ name = &dentry->d_name;
8844+ tmpfile = au_di(dentry)->di_tmpfile;
8845+
8846+ /*
8847+ * Theoretically, REVAL test should be unnecessary in case of
8848+ * {FS,I}NOTIFY.
8849+ * But {fs,i}notify doesn't fire some necessary events,
8850+ * IN_ATTRIB for atime/nlink/pageio
8851+ * Let's do REVAL test too.
8852+ */
8853+ if (do_udba && inode) {
8854+ mode = (inode->i_mode & S_IFMT);
8855+ plus = (inode->i_nlink > 0);
8856+ ibs = au_ibtop(inode);
8857+ ibe = au_ibbot(inode);
8858+ }
8859+
8860+ btop = au_dbtop(dentry);
8861+ btail = btop;
8862+ if (inode && S_ISDIR(inode->i_mode))
8863+ btail = au_dbtaildir(dentry);
8864+ for (bindex = btop; bindex <= btail; bindex++) {
8865+ h_dentry = au_h_dptr(dentry, bindex);
8866+ if (!h_dentry)
8867+ continue;
8868+
8869+ AuDbg("b%d, %pd\n", bindex, h_dentry);
8870+ h_nfs = !!au_test_nfs(h_dentry->d_sb);
8871+ spin_lock(&h_dentry->d_lock);
8872+ h_name = &h_dentry->d_name;
8873+ if (unlikely(do_udba
8874+ && !is_root
8875+ && ((!h_nfs
8876+ && (unhashed != !!d_unhashed(h_dentry)
8877+ || (!tmpfile && !dirren
8878+ && !au_qstreq(name, h_name))
8879+ ))
8880+ || (h_nfs
8881+ && !(flags & LOOKUP_OPEN)
8882+ && (h_dentry->d_flags
8883+ & DCACHE_NFSFS_RENAMED)))
8884+ )) {
8885+ int h_unhashed;
8886+
8887+ h_unhashed = d_unhashed(h_dentry);
8888+ spin_unlock(&h_dentry->d_lock);
8889+ AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8890+ unhashed, h_unhashed, dentry, h_dentry);
8891+ goto err;
8892+ }
8893+ spin_unlock(&h_dentry->d_lock);
8894+
8895+ err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8896+ if (unlikely(err))
8897+ /* do not goto err, to keep the errno */
8898+ break;
8899+
8900+ /* todo: plink too? */
8901+ if (!do_udba)
8902+ continue;
8903+
8904+ /* UDBA tests */
8905+ if (unlikely(!!inode != d_is_positive(h_dentry)))
8906+ goto err;
8907+
8908+ h_inode = NULL;
8909+ if (d_is_positive(h_dentry))
8910+ h_inode = d_inode(h_dentry);
8911+ h_plus = plus;
8912+ h_mode = mode;
8913+ h_cached_inode = h_inode;
8914+ if (h_inode) {
8915+ h_mode = (h_inode->i_mode & S_IFMT);
8916+ h_plus = (h_inode->i_nlink > 0);
8917+ }
8918+ if (inode && ibs <= bindex && bindex <= ibe)
8919+ h_cached_inode = au_h_iptr(inode, bindex);
8920+
8921+ if (!h_nfs) {
8922+ if (unlikely(plus != h_plus && !tmpfile))
8923+ goto err;
8924+ } else {
8925+ if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8926+ && !is_root
8927+ && !IS_ROOT(h_dentry)
8928+ && unhashed != d_unhashed(h_dentry)))
8929+ goto err;
8930+ }
8931+ if (unlikely(mode != h_mode
8932+ || h_cached_inode != h_inode))
8933+ goto err;
8934+ continue;
8935+
8936+err:
8937+ err = -EINVAL;
8938+ break;
8939+ }
8940+
8941+ AuTraceErr(err);
8942+ return err;
8943+}
8944+
8945+/* todo: consolidate with do_refresh() and au_reval_for_attr() */
8946+static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8947+{
8948+ int err;
8949+ struct dentry *parent;
8950+
8951+ if (!au_digen_test(dentry, sigen))
8952+ return 0;
8953+
8954+ parent = dget_parent(dentry);
8955+ di_read_lock_parent(parent, AuLock_IR);
8956+ AuDebugOn(au_digen_test(parent, sigen));
8957+ au_dbg_verify_gen(parent, sigen);
8958+ err = au_refresh_dentry(dentry, parent);
8959+ di_read_unlock(parent, AuLock_IR);
8960+ dput(parent);
8961+ AuTraceErr(err);
8962+ return err;
8963+}
8964+
8965+int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8966+{
8967+ int err;
8968+ struct dentry *d, *parent;
8969+
8970+ if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
8971+ return simple_reval_dpath(dentry, sigen);
8972+
8973+ /* slow loop, keep it simple and stupid */
8974+ /* cf: au_cpup_dirs() */
8975+ err = 0;
8976+ parent = NULL;
8977+ while (au_digen_test(dentry, sigen)) {
8978+ d = dentry;
8979+ while (1) {
8980+ dput(parent);
8981+ parent = dget_parent(d);
8982+ if (!au_digen_test(parent, sigen))
8983+ break;
8984+ d = parent;
8985+ }
8986+
8987+ if (d != dentry)
8988+ di_write_lock_child2(d);
8989+
8990+ /* someone might update our dentry while we were sleeping */
8991+ if (au_digen_test(d, sigen)) {
8992+ /*
8993+ * todo: consolidate with simple_reval_dpath(),
8994+ * do_refresh() and au_reval_for_attr().
8995+ */
8996+ di_read_lock_parent(parent, AuLock_IR);
8997+ err = au_refresh_dentry(d, parent);
8998+ di_read_unlock(parent, AuLock_IR);
8999+ }
9000+
9001+ if (d != dentry)
9002+ di_write_unlock(d);
9003+ dput(parent);
9004+ if (unlikely(err))
9005+ break;
9006+ }
9007+
9008+ return err;
9009+}
9010+
9011+/*
9012+ * if valid returns 1, otherwise 0.
9013+ */
9014+static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
9015+{
9016+ int valid, err;
9017+ unsigned int sigen;
9018+ unsigned char do_udba, dirren;
9019+ struct super_block *sb;
9020+ struct inode *inode;
9021+
9022+ /* todo: support rcu-walk? */
9023+ if (flags & LOOKUP_RCU)
9024+ return -ECHILD;
9025+
9026+ valid = 0;
9027+ if (unlikely(!au_di(dentry)))
9028+ goto out;
9029+
9030+ valid = 1;
9031+ sb = dentry->d_sb;
9032+ /*
9033+ * todo: very ugly
9034+ * i_mutex of parent dir may be held,
9035+ * but we should not return 'invalid' due to busy.
9036+ */
9037+ err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9038+ if (unlikely(err)) {
9039+ valid = err;
9040+ AuTraceErr(err);
9041+ goto out;
9042+ }
9043+ inode = NULL;
9044+ if (d_really_is_positive(dentry))
9045+ inode = d_inode(dentry);
9046+ if (unlikely(inode && au_is_bad_inode(inode))) {
9047+ err = -EINVAL;
9048+ AuTraceErr(err);
9049+ goto out_dgrade;
9050+ }
9051+ if (unlikely(au_dbrange_test(dentry))) {
9052+ err = -EINVAL;
9053+ AuTraceErr(err);
9054+ goto out_dgrade;
9055+ }
9056+
9057+ sigen = au_sigen(sb);
9058+ if (au_digen_test(dentry, sigen)) {
9059+ AuDebugOn(IS_ROOT(dentry));
9060+ err = au_reval_dpath(dentry, sigen);
9061+ if (unlikely(err)) {
9062+ AuTraceErr(err);
9063+ goto out_dgrade;
9064+ }
9065+ }
9066+ di_downgrade_lock(dentry, AuLock_IR);
9067+
9068+ err = -EINVAL;
9069+ if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
9070+ && inode
9071+ && !(inode->i_state && I_LINKABLE)
9072+ && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9073+ AuTraceErr(err);
9074+ goto out_inval;
9075+ }
9076+
9077+ do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9078+ if (do_udba && inode) {
9079+ aufs_bindex_t btop = au_ibtop(inode);
9080+ struct inode *h_inode;
9081+
9082+ if (btop >= 0) {
9083+ h_inode = au_h_iptr(inode, btop);
9084+ if (h_inode && au_test_higen(inode, h_inode)) {
9085+ AuTraceErr(err);
9086+ goto out_inval;
9087+ }
9088+ }
9089+ }
9090+
9091+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9092+ err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
9093+ if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
9094+ err = -EIO;
9095+ AuDbg("both of real entry and whiteout found, %p, err %d\n",
9096+ dentry, err);
9097+ }
9098+ goto out_inval;
9099+
9100+out_dgrade:
9101+ di_downgrade_lock(dentry, AuLock_IR);
9102+out_inval:
9103+ aufs_read_unlock(dentry, AuLock_IR);
9104+ AuTraceErr(err);
9105+ valid = !err;
9106+out:
9107+ if (!valid) {
9108+ AuDbg("%pd invalid, %d\n", dentry, valid);
9109+ d_drop(dentry);
9110+ }
9111+ return valid;
9112+}
9113+
9114+static void aufs_d_release(struct dentry *dentry)
9115+{
9116+ if (au_di(dentry)) {
9117+ au_di_fin(dentry);
9118+ au_hn_di_reinit(dentry);
9119+ }
9120+}
9121+
9122+const struct dentry_operations aufs_dop = {
9123+ .d_revalidate = aufs_d_revalidate,
9124+ .d_weak_revalidate = aufs_d_revalidate,
9125+ .d_release = aufs_d_release
9126+};
9127+
9128+/* aufs_dop without d_revalidate */
9129+const struct dentry_operations aufs_dop_noreval = {
9130+ .d_release = aufs_d_release
9131+};
9132diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9133--- /usr/share/empty/fs/aufs/dentry.h 1970-01-01 01:00:00.000000000 +0100
9134+++ linux/fs/aufs/dentry.h 2021-02-22 22:51:59.058078584 +0100
9135@@ -0,0 +1,268 @@
9136+/* SPDX-License-Identifier: GPL-2.0 */
9137+/*
9138+ * Copyright (C) 2005-2020 Junjiro R. Okajima
9139+ *
9140+ * This program, aufs is free software; you can redistribute it and/or modify
9141+ * it under the terms of the GNU General Public License as published by
9142+ * the Free Software Foundation; either version 2 of the License, or
9143+ * (at your option) any later version.
9144+ *
9145+ * This program is distributed in the hope that it will be useful,
9146+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9147+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9148+ * GNU General Public License for more details.
9149+ *
9150+ * You should have received a copy of the GNU General Public License
9151+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
9152+ */
9153+
9154+/*
9155+ * lookup and dentry operations
9156+ */
9157+
9158+#ifndef __AUFS_DENTRY_H__
9159+#define __AUFS_DENTRY_H__
9160+
9161+#ifdef __KERNEL__
9162+
9163+#include <linux/dcache.h>
9164+#include "dirren.h"
9165+#include "rwsem.h"
9166+
9167+struct au_hdentry {
9168+ struct dentry *hd_dentry;
9169+ aufs_bindex_t hd_id;
9170+};
9171+
9172+struct au_dinfo {
9173+ atomic_t di_generation;
9174+
9175+ struct au_rwsem di_rwsem;
9176+ aufs_bindex_t di_btop, di_bbot, di_bwh, di_bdiropq;
9177+ unsigned char di_tmpfile; /* to allow the different name */
9178+ struct au_hdentry *di_hdentry;
9179+ struct rcu_head rcu;
9180+} ____cacheline_aligned_in_smp;
9181+
9182+/* ---------------------------------------------------------------------- */
9183+
9184+/* flags for au_lkup_dentry() */
9185+#define AuLkup_ALLOW_NEG 1
9186+#define AuLkup_IGNORE_PERM (1 << 1)
9187+#define AuLkup_DIRREN (1 << 2)
9188+#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
9189+#define au_fset_lkup(flags, name) \
9190+ do { (flags) |= AuLkup_##name; } while (0)
9191+#define au_fclr_lkup(flags, name) \
9192+ do { (flags) &= ~AuLkup_##name; } while (0)
9193+
9194+#ifndef CONFIG_AUFS_DIRREN
9195+#undef AuLkup_DIRREN
9196+#define AuLkup_DIRREN 0
9197+#endif
9198+
9199+struct au_do_lookup_args {
9200+ unsigned int flags;
9201+ mode_t type;
9202+ struct qstr whname, *name;
9203+ struct au_dr_lookup dirren;
9204+};
9205+
9206+/* ---------------------------------------------------------------------- */
9207+
9208+/* dentry.c */
9209+extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
9210+struct au_branch;
9211+struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
9212+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9213+ struct dentry *h_parent, struct au_branch *br);
9214+
9215+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9216+ unsigned int flags);
9217+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
9218+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
9219+int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
9220+void au_refresh_dop(struct dentry *dentry, int force_reval);
9221+
9222+/* dinfo.c */
9223+void au_di_init_once(void *_di);
9224+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9225+void au_di_free(struct au_dinfo *dinfo);
9226+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9227+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
9228+int au_di_init(struct dentry *dentry);
9229+void au_di_fin(struct dentry *dentry);
9230+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
9231+
9232+void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9233+void di_read_unlock(struct dentry *d, int flags);
9234+void di_downgrade_lock(struct dentry *d, int flags);
9235+void di_write_lock(struct dentry *d, unsigned int lsc);
9236+void di_write_unlock(struct dentry *d);
9237+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9238+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9239+void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9240+
9241+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
9242+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
9243+aufs_bindex_t au_dbtail(struct dentry *dentry);
9244+aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9245+
9246+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9247+ struct dentry *h_dentry);
9248+int au_digen_test(struct dentry *dentry, unsigned int sigen);
9249+int au_dbrange_test(struct dentry *dentry);
9250+void au_update_digen(struct dentry *dentry);
9251+void au_update_dbrange(struct dentry *dentry, int do_put_zero);
9252+void au_update_dbtop(struct dentry *dentry);
9253+void au_update_dbbot(struct dentry *dentry);
9254+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9255+
9256+/* ---------------------------------------------------------------------- */
9257+
9258+static inline struct au_dinfo *au_di(struct dentry *dentry)
9259+{
9260+ return dentry->d_fsdata;
9261+}
9262+
9263+/* ---------------------------------------------------------------------- */
9264+
9265+/* lock subclass for dinfo */
9266+enum {
9267+ AuLsc_DI_CHILD, /* child first */
9268+ AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
9269+ AuLsc_DI_CHILD3, /* copyup dirs */
9270+ AuLsc_DI_PARENT,
9271+ AuLsc_DI_PARENT2,
9272+ AuLsc_DI_PARENT3,
9273+ AuLsc_DI_TMP /* temp for replacing dinfo */
9274+};
9275+
9276+/*
9277+ * di_read_lock_child, di_write_lock_child,
9278+ * di_read_lock_child2, di_write_lock_child2,
9279+ * di_read_lock_child3, di_write_lock_child3,
9280+ * di_read_lock_parent, di_write_lock_parent,
9281+ * di_read_lock_parent2, di_write_lock_parent2,
9282+ * di_read_lock_parent3, di_write_lock_parent3,
9283+ */
9284+#define AuReadLockFunc(name, lsc) \
9285+static inline void di_read_lock_##name(struct dentry *d, int flags) \
9286+{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9287+
9288+#define AuWriteLockFunc(name, lsc) \
9289+static inline void di_write_lock_##name(struct dentry *d) \
9290+{ di_write_lock(d, AuLsc_DI_##lsc); }
9291+
9292+#define AuRWLockFuncs(name, lsc) \
9293+ AuReadLockFunc(name, lsc) \
9294+ AuWriteLockFunc(name, lsc)
9295+
9296+AuRWLockFuncs(child, CHILD);
9297+AuRWLockFuncs(child2, CHILD2);
9298+AuRWLockFuncs(child3, CHILD3);
9299+AuRWLockFuncs(parent, PARENT);
9300+AuRWLockFuncs(parent2, PARENT2);
9301+AuRWLockFuncs(parent3, PARENT3);
9302+
9303+#undef AuReadLockFunc
9304+#undef AuWriteLockFunc
9305+#undef AuRWLockFuncs
9306+
9307+#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
9308+#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
9309+#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
9310+
9311+/* ---------------------------------------------------------------------- */
9312+
9313+/* todo: memory barrier? */
9314+static inline unsigned int au_digen(struct dentry *d)
9315+{
9316+ return atomic_read(&au_di(d)->di_generation);
9317+}
9318+
9319+static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9320+{
9321+ hdentry->hd_dentry = NULL;
9322+}
9323+
9324+static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9325+ aufs_bindex_t bindex)
9326+{
9327+ return di->di_hdentry + bindex;
9328+}
9329+
9330+static inline void au_hdput(struct au_hdentry *hd)
9331+{
9332+ if (hd)
9333+ dput(hd->hd_dentry);
9334+}
9335+
9336+static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9337+{
9338+ DiMustAnyLock(dentry);
9339+ return au_di(dentry)->di_btop;
9340+}
9341+
9342+static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9343+{
9344+ DiMustAnyLock(dentry);
9345+ return au_di(dentry)->di_bbot;
9346+}
9347+
9348+static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9349+{
9350+ DiMustAnyLock(dentry);
9351+ return au_di(dentry)->di_bwh;
9352+}
9353+
9354+static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9355+{
9356+ DiMustAnyLock(dentry);
9357+ return au_di(dentry)->di_bdiropq;
9358+}
9359+
9360+/* todo: hard/soft set? */
9361+static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9362+{
9363+ DiMustWriteLock(dentry);
9364+ au_di(dentry)->di_btop = bindex;
9365+}
9366+
9367+static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9368+{
9369+ DiMustWriteLock(dentry);
9370+ au_di(dentry)->di_bbot = bindex;
9371+}
9372+
9373+static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9374+{
9375+ DiMustWriteLock(dentry);
9376+ /* dbwh can be outside of btop - bbot range */
9377+ au_di(dentry)->di_bwh = bindex;
9378+}
9379+
9380+static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9381+{
9382+ DiMustWriteLock(dentry);
9383+ au_di(dentry)->di_bdiropq = bindex;
9384+}
9385+
9386+/* ---------------------------------------------------------------------- */
9387+
9388+#ifdef CONFIG_AUFS_HNOTIFY
9389+static inline void au_digen_dec(struct dentry *d)
9390+{
9391+ atomic_dec(&au_di(d)->di_generation);
9392+}
9393+
9394+static inline void au_hn_di_reinit(struct dentry *dentry)
9395+{
9396+ dentry->d_fsdata = NULL;
9397+}
9398+#else
9399+AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9400+#endif /* CONFIG_AUFS_HNOTIFY */
9401+
9402+#endif /* __KERNEL__ */
9403+#endif /* __AUFS_DENTRY_H__ */
9404diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9405--- /usr/share/empty/fs/aufs/dinfo.c 1970-01-01 01:00:00.000000000 +0100
9406+++ linux/fs/aufs/dinfo.c 2021-02-22 22:51:59.058078584 +0100
9407@@ -0,0 +1,554 @@
9408+// SPDX-License-Identifier: GPL-2.0
9409+/*
9410+ * Copyright (C) 2005-2020 Junjiro R. Okajima
9411+ *
9412+ * This program, aufs is free software; you can redistribute it and/or modify
9413+ * it under the terms of the GNU General Public License as published by
9414+ * the Free Software Foundation; either version 2 of the License, or
9415+ * (at your option) any later version.
9416+ *
9417+ * This program is distributed in the hope that it will be useful,
9418+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9419+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9420+ * GNU General Public License for more details.
9421+ *
9422+ * You should have received a copy of the GNU General Public License
9423+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
9424+ */
9425+
9426+/*
9427+ * dentry private data
9428+ */
9429+
9430+#include "aufs.h"
9431+
9432+void au_di_init_once(void *_dinfo)
9433+{
9434+ struct au_dinfo *dinfo = _dinfo;
9435+
9436+ au_rw_init(&dinfo->di_rwsem);
9437+}
9438+
9439+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9440+{
9441+ struct au_dinfo *dinfo;
9442+ int nbr, i;
9443+
9444+ dinfo = au_cache_alloc_dinfo();
9445+ if (unlikely(!dinfo))
9446+ goto out;
9447+
9448+ nbr = au_sbbot(sb) + 1;
9449+ if (nbr <= 0)
9450+ nbr = 1;
9451+ dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9452+ if (dinfo->di_hdentry) {
9453+ au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9454+ dinfo->di_btop = -1;
9455+ dinfo->di_bbot = -1;
9456+ dinfo->di_bwh = -1;
9457+ dinfo->di_bdiropq = -1;
9458+ dinfo->di_tmpfile = 0;
9459+ for (i = 0; i < nbr; i++)
9460+ dinfo->di_hdentry[i].hd_id = -1;
9461+ goto out;
9462+ }
9463+
9464+ au_cache_free_dinfo(dinfo);
9465+ dinfo = NULL;
9466+
9467+out:
9468+ return dinfo;
9469+}
9470+
9471+void au_di_free(struct au_dinfo *dinfo)
9472+{
9473+ struct au_hdentry *p;
9474+ aufs_bindex_t bbot, bindex;
9475+
9476+ /* dentry may not be revalidated */
9477+ bindex = dinfo->di_btop;
9478+ if (bindex >= 0) {
9479+ bbot = dinfo->di_bbot;
9480+ p = au_hdentry(dinfo, bindex);
9481+ while (bindex++ <= bbot)
9482+ au_hdput(p++);
9483+ }
9484+ au_kfree_try_rcu(dinfo->di_hdentry);
9485+ au_cache_free_dinfo(dinfo);
9486+}
9487+
9488+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9489+{
9490+ struct au_hdentry *p;
9491+ aufs_bindex_t bi;
9492+
9493+ AuRwMustWriteLock(&a->di_rwsem);
9494+ AuRwMustWriteLock(&b->di_rwsem);
9495+
9496+#define DiSwap(v, name) \
9497+ do { \
9498+ v = a->di_##name; \
9499+ a->di_##name = b->di_##name; \
9500+ b->di_##name = v; \
9501+ } while (0)
9502+
9503+ DiSwap(p, hdentry);
9504+ DiSwap(bi, btop);
9505+ DiSwap(bi, bbot);
9506+ DiSwap(bi, bwh);
9507+ DiSwap(bi, bdiropq);
9508+ /* smp_mb(); */
9509+
9510+#undef DiSwap
9511+}
9512+
9513+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9514+{
9515+ AuRwMustWriteLock(&dst->di_rwsem);
9516+ AuRwMustWriteLock(&src->di_rwsem);
9517+
9518+ dst->di_btop = src->di_btop;
9519+ dst->di_bbot = src->di_bbot;
9520+ dst->di_bwh = src->di_bwh;
9521+ dst->di_bdiropq = src->di_bdiropq;
9522+ /* smp_mb(); */
9523+}
9524+
9525+int au_di_init(struct dentry *dentry)
9526+{
9527+ int err;
9528+ struct super_block *sb;
9529+ struct au_dinfo *dinfo;
9530+
9531+ err = 0;
9532+ sb = dentry->d_sb;
9533+ dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9534+ if (dinfo) {
9535+ atomic_set(&dinfo->di_generation, au_sigen(sb));
9536+ /* smp_mb(); */ /* atomic_set */
9537+ dentry->d_fsdata = dinfo;
9538+ } else
9539+ err = -ENOMEM;
9540+
9541+ return err;
9542+}
9543+
9544+void au_di_fin(struct dentry *dentry)
9545+{
9546+ struct au_dinfo *dinfo;
9547+
9548+ dinfo = au_di(dentry);
9549+ AuRwDestroy(&dinfo->di_rwsem);
9550+ au_di_free(dinfo);
9551+}
9552+
9553+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9554+{
9555+ int err, sz;
9556+ struct au_hdentry *hdp;
9557+
9558+ AuRwMustWriteLock(&dinfo->di_rwsem);
9559+
9560+ err = -ENOMEM;
9561+ sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9562+ if (!sz)
9563+ sz = sizeof(*hdp);
9564+ hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9565+ may_shrink);
9566+ if (hdp) {
9567+ dinfo->di_hdentry = hdp;
9568+ err = 0;
9569+ }
9570+
9571+ return err;
9572+}
9573+
9574+/* ---------------------------------------------------------------------- */
9575+
9576+static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9577+{
9578+ switch (lsc) {
9579+ case AuLsc_DI_CHILD:
9580+ ii_write_lock_child(inode);
9581+ break;
9582+ case AuLsc_DI_CHILD2:
9583+ ii_write_lock_child2(inode);
9584+ break;
9585+ case AuLsc_DI_CHILD3:
9586+ ii_write_lock_child3(inode);
9587+ break;
9588+ case AuLsc_DI_PARENT:
9589+ ii_write_lock_parent(inode);
9590+ break;
9591+ case AuLsc_DI_PARENT2:
9592+ ii_write_lock_parent2(inode);
9593+ break;
9594+ case AuLsc_DI_PARENT3:
9595+ ii_write_lock_parent3(inode);
9596+ break;
9597+ default:
9598+ BUG();
9599+ }
9600+}
9601+
9602+static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9603+{
9604+ switch (lsc) {
9605+ case AuLsc_DI_CHILD:
9606+ ii_read_lock_child(inode);
9607+ break;
9608+ case AuLsc_DI_CHILD2:
9609+ ii_read_lock_child2(inode);
9610+ break;
9611+ case AuLsc_DI_CHILD3:
9612+ ii_read_lock_child3(inode);
9613+ break;
9614+ case AuLsc_DI_PARENT:
9615+ ii_read_lock_parent(inode);
9616+ break;
9617+ case AuLsc_DI_PARENT2:
9618+ ii_read_lock_parent2(inode);
9619+ break;
9620+ case AuLsc_DI_PARENT3:
9621+ ii_read_lock_parent3(inode);
9622+ break;
9623+ default:
9624+ BUG();
9625+ }
9626+}
9627+
9628+void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9629+{
9630+ struct inode *inode;
9631+
9632+ au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9633+ if (d_really_is_positive(d)) {
9634+ inode = d_inode(d);
9635+ if (au_ftest_lock(flags, IW))
9636+ do_ii_write_lock(inode, lsc);
9637+ else if (au_ftest_lock(flags, IR))
9638+ do_ii_read_lock(inode, lsc);
9639+ }
9640+}
9641+
9642+void di_read_unlock(struct dentry *d, int flags)
9643+{
9644+ struct inode *inode;
9645+
9646+ if (d_really_is_positive(d)) {
9647+ inode = d_inode(d);
9648+ if (au_ftest_lock(flags, IW)) {
9649+ au_dbg_verify_dinode(d);
9650+ ii_write_unlock(inode);
9651+ } else if (au_ftest_lock(flags, IR)) {
9652+ au_dbg_verify_dinode(d);
9653+ ii_read_unlock(inode);
9654+ }
9655+ }
9656+ au_rw_read_unlock(&au_di(d)->di_rwsem);
9657+}
9658+
9659+void di_downgrade_lock(struct dentry *d, int flags)
9660+{
9661+ if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9662+ ii_downgrade_lock(d_inode(d));
9663+ au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9664+}
9665+
9666+void di_write_lock(struct dentry *d, unsigned int lsc)
9667+{
9668+ au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9669+ if (d_really_is_positive(d))
9670+ do_ii_write_lock(d_inode(d), lsc);
9671+}
9672+
9673+void di_write_unlock(struct dentry *d)
9674+{
9675+ au_dbg_verify_dinode(d);
9676+ if (d_really_is_positive(d))
9677+ ii_write_unlock(d_inode(d));
9678+ au_rw_write_unlock(&au_di(d)->di_rwsem);
9679+}
9680+
9681+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9682+{
9683+ AuDebugOn(d1 == d2
9684+ || d_inode(d1) == d_inode(d2)
9685+ || d1->d_sb != d2->d_sb);
9686+
9687+ if ((isdir && au_test_subdir(d1, d2))
9688+ || d1 < d2) {
9689+ di_write_lock_child(d1);
9690+ di_write_lock_child2(d2);
9691+ } else {
9692+ di_write_lock_child(d2);
9693+ di_write_lock_child2(d1);
9694+ }
9695+}
9696+
9697+void di_write_lock2_parent(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_parent(d1);
9706+ di_write_lock_parent2(d2);
9707+ } else {
9708+ di_write_lock_parent(d2);
9709+ di_write_lock_parent2(d1);
9710+ }
9711+}
9712+
9713+void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9714+{
9715+ di_write_unlock(d1);
9716+ if (d_inode(d1) == d_inode(d2))
9717+ au_rw_write_unlock(&au_di(d2)->di_rwsem);
9718+ else
9719+ di_write_unlock(d2);
9720+}
9721+
9722+/* ---------------------------------------------------------------------- */
9723+
9724+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9725+{
9726+ struct dentry *d;
9727+
9728+ DiMustAnyLock(dentry);
9729+
9730+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9731+ return NULL;
9732+ AuDebugOn(bindex < 0);
9733+ d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9734+ AuDebugOn(d && au_dcount(d) <= 0);
9735+ return d;
9736+}
9737+
9738+/*
9739+ * extended version of au_h_dptr().
9740+ * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9741+ * error.
9742+ */
9743+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9744+{
9745+ struct dentry *h_dentry;
9746+ struct inode *inode, *h_inode;
9747+
9748+ AuDebugOn(d_really_is_negative(dentry));
9749+
9750+ h_dentry = NULL;
9751+ if (au_dbtop(dentry) <= bindex
9752+ && bindex <= au_dbbot(dentry))
9753+ h_dentry = au_h_dptr(dentry, bindex);
9754+ if (h_dentry && !au_d_linkable(h_dentry)) {
9755+ dget(h_dentry);
9756+ goto out; /* success */
9757+ }
9758+
9759+ inode = d_inode(dentry);
9760+ AuDebugOn(bindex < au_ibtop(inode));
9761+ AuDebugOn(au_ibbot(inode) < bindex);
9762+ h_inode = au_h_iptr(inode, bindex);
9763+ h_dentry = d_find_alias(h_inode);
9764+ if (h_dentry) {
9765+ if (!IS_ERR(h_dentry)) {
9766+ if (!au_d_linkable(h_dentry))
9767+ goto out; /* success */
9768+ dput(h_dentry);
9769+ } else
9770+ goto out;
9771+ }
9772+
9773+ if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9774+ h_dentry = au_plink_lkup(inode, bindex);
9775+ AuDebugOn(!h_dentry);
9776+ if (!IS_ERR(h_dentry)) {
9777+ if (!au_d_hashed_positive(h_dentry))
9778+ goto out; /* success */
9779+ dput(h_dentry);
9780+ h_dentry = NULL;
9781+ }
9782+ }
9783+
9784+out:
9785+ AuDbgDentry(h_dentry);
9786+ return h_dentry;
9787+}
9788+
9789+aufs_bindex_t au_dbtail(struct dentry *dentry)
9790+{
9791+ aufs_bindex_t bbot, bwh;
9792+
9793+ bbot = au_dbbot(dentry);
9794+ if (0 <= bbot) {
9795+ bwh = au_dbwh(dentry);
9796+ if (!bwh)
9797+ return bwh;
9798+ if (0 < bwh && bwh < bbot)
9799+ return bwh - 1;
9800+ }
9801+ return bbot;
9802+}
9803+
9804+aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9805+{
9806+ aufs_bindex_t bbot, bopq;
9807+
9808+ bbot = au_dbtail(dentry);
9809+ if (0 <= bbot) {
9810+ bopq = au_dbdiropq(dentry);
9811+ if (0 <= bopq && bopq < bbot)
9812+ bbot = bopq;
9813+ }
9814+ return bbot;
9815+}
9816+
9817+/* ---------------------------------------------------------------------- */
9818+
9819+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9820+ struct dentry *h_dentry)
9821+{
9822+ struct au_dinfo *dinfo;
9823+ struct au_hdentry *hd;
9824+ struct au_branch *br;
9825+
9826+ DiMustWriteLock(dentry);
9827+
9828+ dinfo = au_di(dentry);
9829+ hd = au_hdentry(dinfo, bindex);
9830+ au_hdput(hd);
9831+ hd->hd_dentry = h_dentry;
9832+ if (h_dentry) {
9833+ br = au_sbr(dentry->d_sb, bindex);
9834+ hd->hd_id = br->br_id;
9835+ }
9836+}
9837+
9838+int au_dbrange_test(struct dentry *dentry)
9839+{
9840+ int err;
9841+ aufs_bindex_t btop, bbot;
9842+
9843+ err = 0;
9844+ btop = au_dbtop(dentry);
9845+ bbot = au_dbbot(dentry);
9846+ if (btop >= 0)
9847+ AuDebugOn(bbot < 0 && btop > bbot);
9848+ else {
9849+ err = -EIO;
9850+ AuDebugOn(bbot >= 0);
9851+ }
9852+
9853+ return err;
9854+}
9855+
9856+int au_digen_test(struct dentry *dentry, unsigned int sigen)
9857+{
9858+ int err;
9859+
9860+ err = 0;
9861+ if (unlikely(au_digen(dentry) != sigen
9862+ || au_iigen_test(d_inode(dentry), sigen)))
9863+ err = -EIO;
9864+
9865+ return err;
9866+}
9867+
9868+void au_update_digen(struct dentry *dentry)
9869+{
9870+ atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9871+ /* smp_mb(); */ /* atomic_set */
9872+}
9873+
9874+void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9875+{
9876+ struct au_dinfo *dinfo;
9877+ struct dentry *h_d;
9878+ struct au_hdentry *hdp;
9879+ aufs_bindex_t bindex, bbot;
9880+
9881+ DiMustWriteLock(dentry);
9882+
9883+ dinfo = au_di(dentry);
9884+ if (!dinfo || dinfo->di_btop < 0)
9885+ return;
9886+
9887+ if (do_put_zero) {
9888+ bbot = dinfo->di_bbot;
9889+ bindex = dinfo->di_btop;
9890+ hdp = au_hdentry(dinfo, bindex);
9891+ for (; bindex <= bbot; bindex++, hdp++) {
9892+ h_d = hdp->hd_dentry;
9893+ if (h_d && d_is_negative(h_d))
9894+ au_set_h_dptr(dentry, bindex, NULL);
9895+ }
9896+ }
9897+
9898+ dinfo->di_btop = 0;
9899+ hdp = au_hdentry(dinfo, dinfo->di_btop);
9900+ for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9901+ if (hdp->hd_dentry)
9902+ break;
9903+ if (dinfo->di_btop > dinfo->di_bbot) {
9904+ dinfo->di_btop = -1;
9905+ dinfo->di_bbot = -1;
9906+ return;
9907+ }
9908+
9909+ hdp = au_hdentry(dinfo, dinfo->di_bbot);
9910+ for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9911+ if (hdp->hd_dentry)
9912+ break;
9913+ AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
9914+}
9915+
9916+void au_update_dbtop(struct dentry *dentry)
9917+{
9918+ aufs_bindex_t bindex, bbot;
9919+ struct dentry *h_dentry;
9920+
9921+ bbot = au_dbbot(dentry);
9922+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
9923+ h_dentry = au_h_dptr(dentry, bindex);
9924+ if (!h_dentry)
9925+ continue;
9926+ if (d_is_positive(h_dentry)) {
9927+ au_set_dbtop(dentry, bindex);
9928+ return;
9929+ }
9930+ au_set_h_dptr(dentry, bindex, NULL);
9931+ }
9932+}
9933+
9934+void au_update_dbbot(struct dentry *dentry)
9935+{
9936+ aufs_bindex_t bindex, btop;
9937+ struct dentry *h_dentry;
9938+
9939+ btop = au_dbtop(dentry);
9940+ for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
9941+ h_dentry = au_h_dptr(dentry, bindex);
9942+ if (!h_dentry)
9943+ continue;
9944+ if (d_is_positive(h_dentry)) {
9945+ au_set_dbbot(dentry, bindex);
9946+ return;
9947+ }
9948+ au_set_h_dptr(dentry, bindex, NULL);
9949+ }
9950+}
9951+
9952+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9953+{
9954+ aufs_bindex_t bindex, bbot;
9955+
9956+ bbot = au_dbbot(dentry);
9957+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
9958+ if (au_h_dptr(dentry, bindex) == h_dentry)
9959+ return bindex;
9960+ return -1;
9961+}
9962diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
9963--- /usr/share/empty/fs/aufs/dir.c 1970-01-01 01:00:00.000000000 +0100
9964+++ linux/fs/aufs/dir.c 2021-02-22 22:51:59.058078584 +0100
9965@@ -0,0 +1,763 @@
9966+// SPDX-License-Identifier: GPL-2.0
9967+/*
9968+ * Copyright (C) 2005-2020 Junjiro R. Okajima
9969+ *
9970+ * This program, aufs is free software; you can redistribute it and/or modify
9971+ * it under the terms of the GNU General Public License as published by
9972+ * the Free Software Foundation; either version 2 of the License, or
9973+ * (at your option) any later version.
9974+ *
9975+ * This program is distributed in the hope that it will be useful,
9976+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9977+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9978+ * GNU General Public License for more details.
9979+ *
9980+ * You should have received a copy of the GNU General Public License
9981+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
9982+ */
9983+
9984+/*
9985+ * directory operations
9986+ */
9987+
9988+#include <linux/fs_stack.h>
9989+#include <linux/iversion.h>
9990+#include "aufs.h"
9991+
9992+void au_add_nlink(struct inode *dir, struct inode *h_dir)
9993+{
9994+ unsigned int nlink;
9995+
9996+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9997+
9998+ nlink = dir->i_nlink;
9999+ nlink += h_dir->i_nlink - 2;
10000+ if (h_dir->i_nlink < 2)
10001+ nlink += 2;
10002+ smp_mb(); /* for i_nlink */
10003+ /* 0 can happen in revaliding */
10004+ set_nlink(dir, nlink);
10005+}
10006+
10007+void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10008+{
10009+ unsigned int nlink;
10010+
10011+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10012+
10013+ nlink = dir->i_nlink;
10014+ nlink -= h_dir->i_nlink - 2;
10015+ if (h_dir->i_nlink < 2)
10016+ nlink -= 2;
10017+ smp_mb(); /* for i_nlink */
10018+ /* nlink == 0 means the branch-fs is broken */
10019+ set_nlink(dir, nlink);
10020+}
10021+
10022+loff_t au_dir_size(struct file *file, struct dentry *dentry)
10023+{
10024+ loff_t sz;
10025+ aufs_bindex_t bindex, bbot;
10026+ struct file *h_file;
10027+ struct dentry *h_dentry;
10028+
10029+ sz = 0;
10030+ if (file) {
10031+ AuDebugOn(!d_is_dir(file->f_path.dentry));
10032+
10033+ bbot = au_fbbot_dir(file);
10034+ for (bindex = au_fbtop(file);
10035+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10036+ bindex++) {
10037+ h_file = au_hf_dir(file, bindex);
10038+ if (h_file && file_inode(h_file))
10039+ sz += vfsub_f_size_read(h_file);
10040+ }
10041+ } else {
10042+ AuDebugOn(!dentry);
10043+ AuDebugOn(!d_is_dir(dentry));
10044+
10045+ bbot = au_dbtaildir(dentry);
10046+ for (bindex = au_dbtop(dentry);
10047+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10048+ bindex++) {
10049+ h_dentry = au_h_dptr(dentry, bindex);
10050+ if (h_dentry && d_is_positive(h_dentry))
10051+ sz += i_size_read(d_inode(h_dentry));
10052+ }
10053+ }
10054+ if (sz < KMALLOC_MAX_SIZE)
10055+ sz = roundup_pow_of_two(sz);
10056+ if (sz > KMALLOC_MAX_SIZE)
10057+ sz = KMALLOC_MAX_SIZE;
10058+ else if (sz < NAME_MAX) {
10059+ BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10060+ sz = AUFS_RDBLK_DEF;
10061+ }
10062+ return sz;
10063+}
10064+
10065+struct au_dir_ts_arg {
10066+ struct dentry *dentry;
10067+ aufs_bindex_t brid;
10068+};
10069+
10070+static void au_do_dir_ts(void *arg)
10071+{
10072+ struct au_dir_ts_arg *a = arg;
10073+ struct au_dtime dt;
10074+ struct path h_path;
10075+ struct inode *dir, *h_dir;
10076+ struct super_block *sb;
10077+ struct au_branch *br;
10078+ struct au_hinode *hdir;
10079+ int err;
10080+ aufs_bindex_t btop, bindex;
10081+
10082+ sb = a->dentry->d_sb;
10083+ if (d_really_is_negative(a->dentry))
10084+ goto out;
10085+ /* no dir->i_mutex lock */
10086+ aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10087+
10088+ dir = d_inode(a->dentry);
10089+ btop = au_ibtop(dir);
10090+ bindex = au_br_index(sb, a->brid);
10091+ if (bindex < btop)
10092+ goto out_unlock;
10093+
10094+ br = au_sbr(sb, bindex);
10095+ h_path.dentry = au_h_dptr(a->dentry, bindex);
10096+ if (!h_path.dentry)
10097+ goto out_unlock;
10098+ h_path.mnt = au_br_mnt(br);
10099+ au_dtime_store(&dt, a->dentry, &h_path);
10100+
10101+ br = au_sbr(sb, btop);
10102+ if (!au_br_writable(br->br_perm))
10103+ goto out_unlock;
10104+ h_path.dentry = au_h_dptr(a->dentry, btop);
10105+ h_path.mnt = au_br_mnt(br);
10106+ err = vfsub_mnt_want_write(h_path.mnt);
10107+ if (err)
10108+ goto out_unlock;
10109+ hdir = au_hi(dir, btop);
10110+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10111+ h_dir = au_h_iptr(dir, btop);
10112+ if (h_dir->i_nlink
10113+ && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
10114+ dt.dt_h_path = h_path;
10115+ au_dtime_revert(&dt);
10116+ }
10117+ au_hn_inode_unlock(hdir);
10118+ vfsub_mnt_drop_write(h_path.mnt);
10119+ au_cpup_attr_timesizes(dir);
10120+
10121+out_unlock:
10122+ aufs_read_unlock(a->dentry, AuLock_DW);
10123+out:
10124+ dput(a->dentry);
10125+ au_nwt_done(&au_sbi(sb)->si_nowait);
10126+ au_kfree_try_rcu(arg);
10127+}
10128+
10129+void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10130+{
10131+ int perm, wkq_err;
10132+ aufs_bindex_t btop;
10133+ struct au_dir_ts_arg *arg;
10134+ struct dentry *dentry;
10135+ struct super_block *sb;
10136+
10137+ IMustLock(dir);
10138+
10139+ dentry = d_find_any_alias(dir);
10140+ AuDebugOn(!dentry);
10141+ sb = dentry->d_sb;
10142+ btop = au_ibtop(dir);
10143+ if (btop == bindex) {
10144+ au_cpup_attr_timesizes(dir);
10145+ goto out;
10146+ }
10147+
10148+ perm = au_sbr_perm(sb, btop);
10149+ if (!au_br_writable(perm))
10150+ goto out;
10151+
10152+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
10153+ if (!arg)
10154+ goto out;
10155+
10156+ arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10157+ arg->brid = au_sbr_id(sb, bindex);
10158+ wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10159+ if (unlikely(wkq_err)) {
10160+ pr_err("wkq %d\n", wkq_err);
10161+ dput(dentry);
10162+ au_kfree_try_rcu(arg);
10163+ }
10164+
10165+out:
10166+ dput(dentry);
10167+}
10168+
10169+/* ---------------------------------------------------------------------- */
10170+
10171+static int reopen_dir(struct file *file)
10172+{
10173+ int err;
10174+ unsigned int flags;
10175+ aufs_bindex_t bindex, btail, btop;
10176+ struct dentry *dentry, *h_dentry;
10177+ struct file *h_file;
10178+
10179+ /* open all lower dirs */
10180+ dentry = file->f_path.dentry;
10181+ btop = au_dbtop(dentry);
10182+ for (bindex = au_fbtop(file); bindex < btop; bindex++)
10183+ au_set_h_fptr(file, bindex, NULL);
10184+ au_set_fbtop(file, btop);
10185+
10186+ btail = au_dbtaildir(dentry);
10187+ for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
10188+ au_set_h_fptr(file, bindex, NULL);
10189+ au_set_fbbot_dir(file, btail);
10190+
10191+ flags = vfsub_file_flags(file);
10192+ for (bindex = btop; bindex <= btail; bindex++) {
10193+ h_dentry = au_h_dptr(dentry, bindex);
10194+ if (!h_dentry)
10195+ continue;
10196+ h_file = au_hf_dir(file, bindex);
10197+ if (h_file)
10198+ continue;
10199+
10200+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10201+ err = PTR_ERR(h_file);
10202+ if (IS_ERR(h_file))
10203+ goto out; /* close all? */
10204+ au_set_h_fptr(file, bindex, h_file);
10205+ }
10206+ au_update_figen(file);
10207+ /* todo: necessary? */
10208+ /* file->f_ra = h_file->f_ra; */
10209+ err = 0;
10210+
10211+out:
10212+ return err;
10213+}
10214+
10215+static int do_open_dir(struct file *file, int flags, struct file *h_file)
10216+{
10217+ int err;
10218+ aufs_bindex_t bindex, btail;
10219+ struct dentry *dentry, *h_dentry;
10220+ struct vfsmount *mnt;
10221+
10222+ FiMustWriteLock(file);
10223+ AuDebugOn(h_file);
10224+
10225+ err = 0;
10226+ mnt = file->f_path.mnt;
10227+ dentry = file->f_path.dentry;
10228+ file->f_version = inode_query_iversion(d_inode(dentry));
10229+ bindex = au_dbtop(dentry);
10230+ au_set_fbtop(file, bindex);
10231+ btail = au_dbtaildir(dentry);
10232+ au_set_fbbot_dir(file, btail);
10233+ for (; !err && bindex <= btail; bindex++) {
10234+ h_dentry = au_h_dptr(dentry, bindex);
10235+ if (!h_dentry)
10236+ continue;
10237+
10238+ err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10239+ if (unlikely(err))
10240+ break;
10241+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10242+ if (IS_ERR(h_file)) {
10243+ err = PTR_ERR(h_file);
10244+ break;
10245+ }
10246+ au_set_h_fptr(file, bindex, h_file);
10247+ }
10248+ au_update_figen(file);
10249+ /* todo: necessary? */
10250+ /* file->f_ra = h_file->f_ra; */
10251+ if (!err)
10252+ return 0; /* success */
10253+
10254+ /* close all */
10255+ for (bindex = au_fbtop(file); bindex <= btail; bindex++)
10256+ au_set_h_fptr(file, bindex, NULL);
10257+ au_set_fbtop(file, -1);
10258+ au_set_fbbot_dir(file, -1);
10259+
10260+ return err;
10261+}
10262+
10263+static int aufs_open_dir(struct inode *inode __maybe_unused,
10264+ struct file *file)
10265+{
10266+ int err;
10267+ struct super_block *sb;
10268+ struct au_fidir *fidir;
10269+
10270+ err = -ENOMEM;
10271+ sb = file->f_path.dentry->d_sb;
10272+ si_read_lock(sb, AuLock_FLUSH);
10273+ fidir = au_fidir_alloc(sb);
10274+ if (fidir) {
10275+ struct au_do_open_args args = {
10276+ .open = do_open_dir,
10277+ .fidir = fidir
10278+ };
10279+ err = au_do_open(file, &args);
10280+ if (unlikely(err))
10281+ au_kfree_rcu(fidir);
10282+ }
10283+ si_read_unlock(sb);
10284+ return err;
10285+}
10286+
10287+static int aufs_release_dir(struct inode *inode __maybe_unused,
10288+ struct file *file)
10289+{
10290+ struct au_vdir *vdir_cache;
10291+ struct au_finfo *finfo;
10292+ struct au_fidir *fidir;
10293+ struct au_hfile *hf;
10294+ aufs_bindex_t bindex, bbot;
10295+
10296+ finfo = au_fi(file);
10297+ fidir = finfo->fi_hdir;
10298+ if (fidir) {
10299+ au_hbl_del(&finfo->fi_hlist,
10300+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
10301+ vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10302+ if (vdir_cache)
10303+ au_vdir_free(vdir_cache);
10304+
10305+ bindex = finfo->fi_btop;
10306+ if (bindex >= 0) {
10307+ hf = fidir->fd_hfile + bindex;
10308+ /*
10309+ * calls fput() instead of filp_close(),
10310+ * since no dnotify or lock for the lower file.
10311+ */
10312+ bbot = fidir->fd_bbot;
10313+ for (; bindex <= bbot; bindex++, hf++)
10314+ if (hf->hf_file)
10315+ au_hfput(hf, /*execed*/0);
10316+ }
10317+ au_kfree_rcu(fidir);
10318+ finfo->fi_hdir = NULL;
10319+ }
10320+ au_finfo_fin(file);
10321+ return 0;
10322+}
10323+
10324+/* ---------------------------------------------------------------------- */
10325+
10326+static int au_do_flush_dir(struct file *file, fl_owner_t id)
10327+{
10328+ int err;
10329+ aufs_bindex_t bindex, bbot;
10330+ struct file *h_file;
10331+
10332+ err = 0;
10333+ bbot = au_fbbot_dir(file);
10334+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10335+ h_file = au_hf_dir(file, bindex);
10336+ if (h_file)
10337+ err = vfsub_flush(h_file, id);
10338+ }
10339+ return err;
10340+}
10341+
10342+static int aufs_flush_dir(struct file *file, fl_owner_t id)
10343+{
10344+ return au_do_flush(file, id, au_do_flush_dir);
10345+}
10346+
10347+/* ---------------------------------------------------------------------- */
10348+
10349+static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10350+{
10351+ int err;
10352+ aufs_bindex_t bbot, bindex;
10353+ struct inode *inode;
10354+ struct super_block *sb;
10355+
10356+ err = 0;
10357+ sb = dentry->d_sb;
10358+ inode = d_inode(dentry);
10359+ IMustLock(inode);
10360+ bbot = au_dbbot(dentry);
10361+ for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10362+ struct path h_path;
10363+
10364+ if (au_test_ro(sb, bindex, inode))
10365+ continue;
10366+ h_path.dentry = au_h_dptr(dentry, bindex);
10367+ if (!h_path.dentry)
10368+ continue;
10369+
10370+ h_path.mnt = au_sbr_mnt(sb, bindex);
10371+ err = vfsub_fsync(NULL, &h_path, datasync);
10372+ }
10373+
10374+ return err;
10375+}
10376+
10377+static int au_do_fsync_dir(struct file *file, int datasync)
10378+{
10379+ int err;
10380+ aufs_bindex_t bbot, bindex;
10381+ struct file *h_file;
10382+ struct super_block *sb;
10383+ struct inode *inode;
10384+
10385+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10386+ if (unlikely(err))
10387+ goto out;
10388+
10389+ inode = file_inode(file);
10390+ sb = inode->i_sb;
10391+ bbot = au_fbbot_dir(file);
10392+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10393+ h_file = au_hf_dir(file, bindex);
10394+ if (!h_file || au_test_ro(sb, bindex, inode))
10395+ continue;
10396+
10397+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10398+ }
10399+
10400+out:
10401+ return err;
10402+}
10403+
10404+/*
10405+ * @file may be NULL
10406+ */
10407+static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10408+ int datasync)
10409+{
10410+ int err;
10411+ struct dentry *dentry;
10412+ struct inode *inode;
10413+ struct super_block *sb;
10414+
10415+ err = 0;
10416+ dentry = file->f_path.dentry;
10417+ inode = d_inode(dentry);
10418+ inode_lock(inode);
10419+ sb = dentry->d_sb;
10420+ si_noflush_read_lock(sb);
10421+ if (file)
10422+ err = au_do_fsync_dir(file, datasync);
10423+ else {
10424+ di_write_lock_child(dentry);
10425+ err = au_do_fsync_dir_no_file(dentry, datasync);
10426+ }
10427+ au_cpup_attr_timesizes(inode);
10428+ di_write_unlock(dentry);
10429+ if (file)
10430+ fi_write_unlock(file);
10431+
10432+ si_read_unlock(sb);
10433+ inode_unlock(inode);
10434+ return err;
10435+}
10436+
10437+/* ---------------------------------------------------------------------- */
10438+
10439+static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10440+{
10441+ int err;
10442+ struct dentry *dentry;
10443+ struct inode *inode, *h_inode;
10444+ struct super_block *sb;
10445+
10446+ AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
10447+
10448+ dentry = file->f_path.dentry;
10449+ inode = d_inode(dentry);
10450+ IMustLock(inode);
10451+
10452+ sb = dentry->d_sb;
10453+ si_read_lock(sb, AuLock_FLUSH);
10454+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10455+ if (unlikely(err))
10456+ goto out;
10457+ err = au_alive_dir(dentry);
10458+ if (!err)
10459+ err = au_vdir_init(file);
10460+ di_downgrade_lock(dentry, AuLock_IR);
10461+ if (unlikely(err))
10462+ goto out_unlock;
10463+
10464+ h_inode = au_h_iptr(inode, au_ibtop(inode));
10465+ if (!au_test_nfsd()) {
10466+ err = au_vdir_fill_de(file, ctx);
10467+ fsstack_copy_attr_atime(inode, h_inode);
10468+ } else {
10469+ /*
10470+ * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10471+ * encode_fh() and others.
10472+ */
10473+ atomic_inc(&h_inode->i_count);
10474+ di_read_unlock(dentry, AuLock_IR);
10475+ si_read_unlock(sb);
10476+ err = au_vdir_fill_de(file, ctx);
10477+ fsstack_copy_attr_atime(inode, h_inode);
10478+ fi_write_unlock(file);
10479+ iput(h_inode);
10480+
10481+ AuTraceErr(err);
10482+ return err;
10483+ }
10484+
10485+out_unlock:
10486+ di_read_unlock(dentry, AuLock_IR);
10487+ fi_write_unlock(file);
10488+out:
10489+ si_read_unlock(sb);
10490+ return err;
10491+}
10492+
10493+/* ---------------------------------------------------------------------- */
10494+
10495+#define AuTestEmpty_WHONLY 1
10496+#define AuTestEmpty_CALLED (1 << 1)
10497+#define AuTestEmpty_SHWH (1 << 2)
10498+#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
10499+#define au_fset_testempty(flags, name) \
10500+ do { (flags) |= AuTestEmpty_##name; } while (0)
10501+#define au_fclr_testempty(flags, name) \
10502+ do { (flags) &= ~AuTestEmpty_##name; } while (0)
10503+
10504+#ifndef CONFIG_AUFS_SHWH
10505+#undef AuTestEmpty_SHWH
10506+#define AuTestEmpty_SHWH 0
10507+#endif
10508+
10509+struct test_empty_arg {
10510+ struct dir_context ctx;
10511+ struct au_nhash *whlist;
10512+ unsigned int flags;
10513+ int err;
10514+ aufs_bindex_t bindex;
10515+};
10516+
10517+static int test_empty_cb(struct dir_context *ctx, const char *__name,
10518+ int namelen, loff_t offset __maybe_unused, u64 ino,
10519+ unsigned int d_type)
10520+{
10521+ struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10522+ ctx);
10523+ char *name = (void *)__name;
10524+
10525+ arg->err = 0;
10526+ au_fset_testempty(arg->flags, CALLED);
10527+ /* smp_mb(); */
10528+ if (name[0] == '.'
10529+ && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10530+ goto out; /* success */
10531+
10532+ if (namelen <= AUFS_WH_PFX_LEN
10533+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10534+ if (au_ftest_testempty(arg->flags, WHONLY)
10535+ && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10536+ arg->err = -ENOTEMPTY;
10537+ goto out;
10538+ }
10539+
10540+ name += AUFS_WH_PFX_LEN;
10541+ namelen -= AUFS_WH_PFX_LEN;
10542+ if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10543+ arg->err = au_nhash_append_wh
10544+ (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10545+ au_ftest_testempty(arg->flags, SHWH));
10546+
10547+out:
10548+ /* smp_mb(); */
10549+ AuTraceErr(arg->err);
10550+ return arg->err;
10551+}
10552+
10553+static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10554+{
10555+ int err;
10556+ struct file *h_file;
10557+ struct au_branch *br;
10558+
10559+ h_file = au_h_open(dentry, arg->bindex,
10560+ O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10561+ /*file*/NULL, /*force_wr*/0);
10562+ err = PTR_ERR(h_file);
10563+ if (IS_ERR(h_file))
10564+ goto out;
10565+
10566+ err = 0;
10567+ if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10568+ && !file_inode(h_file)->i_nlink)
10569+ goto out_put;
10570+
10571+ do {
10572+ arg->err = 0;
10573+ au_fclr_testempty(arg->flags, CALLED);
10574+ /* smp_mb(); */
10575+ err = vfsub_iterate_dir(h_file, &arg->ctx);
10576+ if (err >= 0)
10577+ err = arg->err;
10578+ } while (!err && au_ftest_testempty(arg->flags, CALLED));
10579+
10580+out_put:
10581+ fput(h_file);
10582+ br = au_sbr(dentry->d_sb, arg->bindex);
10583+ au_lcnt_dec(&br->br_nfiles);
10584+out:
10585+ return err;
10586+}
10587+
10588+struct do_test_empty_args {
10589+ int *errp;
10590+ struct dentry *dentry;
10591+ struct test_empty_arg *arg;
10592+};
10593+
10594+static void call_do_test_empty(void *args)
10595+{
10596+ struct do_test_empty_args *a = args;
10597+ *a->errp = do_test_empty(a->dentry, a->arg);
10598+}
10599+
10600+static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10601+{
10602+ int err, wkq_err;
10603+ struct dentry *h_dentry;
10604+ struct inode *h_inode;
10605+
10606+ h_dentry = au_h_dptr(dentry, arg->bindex);
10607+ h_inode = d_inode(h_dentry);
10608+ /* todo: i_mode changes anytime? */
10609+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
10610+ err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
10611+ inode_unlock_shared(h_inode);
10612+ if (!err)
10613+ err = do_test_empty(dentry, arg);
10614+ else {
10615+ struct do_test_empty_args args = {
10616+ .errp = &err,
10617+ .dentry = dentry,
10618+ .arg = arg
10619+ };
10620+ unsigned int flags = arg->flags;
10621+
10622+ wkq_err = au_wkq_wait(call_do_test_empty, &args);
10623+ if (unlikely(wkq_err))
10624+ err = wkq_err;
10625+ arg->flags = flags;
10626+ }
10627+
10628+ return err;
10629+}
10630+
10631+int au_test_empty_lower(struct dentry *dentry)
10632+{
10633+ int err;
10634+ unsigned int rdhash;
10635+ aufs_bindex_t bindex, btop, btail;
10636+ struct au_nhash whlist;
10637+ struct test_empty_arg arg = {
10638+ .ctx = {
10639+ .actor = test_empty_cb
10640+ }
10641+ };
10642+ int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10643+
10644+ SiMustAnyLock(dentry->d_sb);
10645+
10646+ rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10647+ if (!rdhash)
10648+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10649+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10650+ if (unlikely(err))
10651+ goto out;
10652+
10653+ arg.flags = 0;
10654+ arg.whlist = &whlist;
10655+ btop = au_dbtop(dentry);
10656+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10657+ au_fset_testempty(arg.flags, SHWH);
10658+ test_empty = do_test_empty;
10659+ if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10660+ test_empty = sio_test_empty;
10661+ arg.bindex = btop;
10662+ err = test_empty(dentry, &arg);
10663+ if (unlikely(err))
10664+ goto out_whlist;
10665+
10666+ au_fset_testempty(arg.flags, WHONLY);
10667+ btail = au_dbtaildir(dentry);
10668+ for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10669+ struct dentry *h_dentry;
10670+
10671+ h_dentry = au_h_dptr(dentry, bindex);
10672+ if (h_dentry && d_is_positive(h_dentry)) {
10673+ arg.bindex = bindex;
10674+ err = test_empty(dentry, &arg);
10675+ }
10676+ }
10677+
10678+out_whlist:
10679+ au_nhash_wh_free(&whlist);
10680+out:
10681+ return err;
10682+}
10683+
10684+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10685+{
10686+ int err;
10687+ struct test_empty_arg arg = {
10688+ .ctx = {
10689+ .actor = test_empty_cb
10690+ }
10691+ };
10692+ aufs_bindex_t bindex, btail;
10693+
10694+ err = 0;
10695+ arg.whlist = whlist;
10696+ arg.flags = AuTestEmpty_WHONLY;
10697+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10698+ au_fset_testempty(arg.flags, SHWH);
10699+ btail = au_dbtaildir(dentry);
10700+ for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10701+ struct dentry *h_dentry;
10702+
10703+ h_dentry = au_h_dptr(dentry, bindex);
10704+ if (h_dentry && d_is_positive(h_dentry)) {
10705+ arg.bindex = bindex;
10706+ err = sio_test_empty(dentry, &arg);
10707+ }
10708+ }
10709+
10710+ return err;
10711+}
10712+
10713+/* ---------------------------------------------------------------------- */
10714+
10715+const struct file_operations aufs_dir_fop = {
10716+ .owner = THIS_MODULE,
10717+ .llseek = default_llseek,
10718+ .read = generic_read_dir,
10719+ .iterate_shared = aufs_iterate_shared,
10720+ .unlocked_ioctl = aufs_ioctl_dir,
10721+#ifdef CONFIG_COMPAT
10722+ .compat_ioctl = aufs_compat_ioctl_dir,
10723+#endif
10724+ .open = aufs_open_dir,
10725+ .release = aufs_release_dir,
10726+ .flush = aufs_flush_dir,
10727+ .fsync = aufs_fsync_dir
10728+};
10729diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10730--- /usr/share/empty/fs/aufs/dir.h 1970-01-01 01:00:00.000000000 +0100
10731+++ linux/fs/aufs/dir.h 2021-02-22 22:51:59.058078584 +0100
10732@@ -0,0 +1,134 @@
10733+/* SPDX-License-Identifier: GPL-2.0 */
10734+/*
10735+ * Copyright (C) 2005-2020 Junjiro R. Okajima
10736+ *
10737+ * This program, aufs is free software; you can redistribute it and/or modify
10738+ * it under the terms of the GNU General Public License as published by
10739+ * the Free Software Foundation; either version 2 of the License, or
10740+ * (at your option) any later version.
10741+ *
10742+ * This program is distributed in the hope that it will be useful,
10743+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10744+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10745+ * GNU General Public License for more details.
10746+ *
10747+ * You should have received a copy of the GNU General Public License
10748+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
10749+ */
10750+
10751+/*
10752+ * directory operations
10753+ */
10754+
10755+#ifndef __AUFS_DIR_H__
10756+#define __AUFS_DIR_H__
10757+
10758+#ifdef __KERNEL__
10759+
10760+#include <linux/fs.h>
10761+
10762+/* ---------------------------------------------------------------------- */
10763+
10764+/* need to be faster and smaller */
10765+
10766+struct au_nhash {
10767+ unsigned int nh_num;
10768+ struct hlist_head *nh_head;
10769+};
10770+
10771+struct au_vdir_destr {
10772+ unsigned char len;
10773+ unsigned char name[];
10774+} __packed;
10775+
10776+struct au_vdir_dehstr {
10777+ struct hlist_node hash;
10778+ struct au_vdir_destr *str;
10779+ struct rcu_head rcu;
10780+} ____cacheline_aligned_in_smp;
10781+
10782+struct au_vdir_de {
10783+ ino_t de_ino;
10784+ unsigned char de_type;
10785+ /* caution: packed */
10786+ struct au_vdir_destr de_str;
10787+} __packed;
10788+
10789+struct au_vdir_wh {
10790+ struct hlist_node wh_hash;
10791+#ifdef CONFIG_AUFS_SHWH
10792+ ino_t wh_ino;
10793+ aufs_bindex_t wh_bindex;
10794+ unsigned char wh_type;
10795+#else
10796+ aufs_bindex_t wh_bindex;
10797+#endif
10798+ /* caution: packed */
10799+ struct au_vdir_destr wh_str;
10800+} __packed;
10801+
10802+union au_vdir_deblk_p {
10803+ unsigned char *deblk;
10804+ struct au_vdir_de *de;
10805+};
10806+
10807+struct au_vdir {
10808+ unsigned char **vd_deblk;
10809+ unsigned long vd_nblk;
10810+ struct {
10811+ unsigned long ul;
10812+ union au_vdir_deblk_p p;
10813+ } vd_last;
10814+
10815+ u64 vd_version;
10816+ unsigned int vd_deblk_sz;
10817+ unsigned long vd_jiffy;
10818+ struct rcu_head rcu;
10819+} ____cacheline_aligned_in_smp;
10820+
10821+/* ---------------------------------------------------------------------- */
10822+
10823+/* dir.c */
10824+extern const struct file_operations aufs_dir_fop;
10825+void au_add_nlink(struct inode *dir, struct inode *h_dir);
10826+void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10827+loff_t au_dir_size(struct file *file, struct dentry *dentry);
10828+void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10829+int au_test_empty_lower(struct dentry *dentry);
10830+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10831+
10832+/* vdir.c */
10833+unsigned int au_rdhash_est(loff_t sz);
10834+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10835+void au_nhash_wh_free(struct au_nhash *whlist);
10836+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10837+ int limit);
10838+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10839+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10840+ unsigned int d_type, aufs_bindex_t bindex,
10841+ unsigned char shwh);
10842+void au_vdir_free(struct au_vdir *vdir);
10843+int au_vdir_init(struct file *file);
10844+int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10845+
10846+/* ioctl.c */
10847+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10848+
10849+#ifdef CONFIG_AUFS_RDU
10850+/* rdu.c */
10851+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10852+#ifdef CONFIG_COMPAT
10853+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10854+ unsigned long arg);
10855+#endif
10856+#else
10857+AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10858+ unsigned int cmd, unsigned long arg)
10859+#ifdef CONFIG_COMPAT
10860+AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10861+ unsigned int cmd, unsigned long arg)
10862+#endif
10863+#endif
10864+
10865+#endif /* __KERNEL__ */
10866+#endif /* __AUFS_DIR_H__ */
10867diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
10868--- /usr/share/empty/fs/aufs/dirren.c 1970-01-01 01:00:00.000000000 +0100
10869+++ linux/fs/aufs/dirren.c 2021-02-22 22:51:59.058078584 +0100
10870@@ -0,0 +1,1316 @@
10871+// SPDX-License-Identifier: GPL-2.0
10872+/*
10873+ * Copyright (C) 2017-2020 Junjiro R. Okajima
10874+ *
10875+ * This program, aufs is free software; you can redistribute it and/or modify
10876+ * it under the terms of the GNU General Public License as published by
10877+ * the Free Software Foundation; either version 2 of the License, or
10878+ * (at your option) any later version.
10879+ *
10880+ * This program is distributed in the hope that it will be useful,
10881+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10882+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10883+ * GNU General Public License for more details.
10884+ *
10885+ * You should have received a copy of the GNU General Public License
10886+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
10887+ */
10888+
10889+/*
10890+ * special handling in renaming a directory
10891+ * in order to support looking-up the before-renamed name on the lower readonly
10892+ * branches
10893+ */
10894+
10895+#include <linux/byteorder/generic.h>
10896+#include "aufs.h"
10897+
10898+static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10899+{
10900+ int idx;
10901+
10902+ idx = au_dr_ihash(ent->dr_h_ino);
10903+ au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10904+}
10905+
10906+static int au_dr_hino_test_empty(struct au_dr_br *dr)
10907+{
10908+ int ret, i;
10909+ struct hlist_bl_head *hbl;
10910+
10911+ ret = 1;
10912+ for (i = 0; ret && i < AuDirren_NHASH; i++) {
10913+ hbl = dr->dr_h_ino + i;
10914+ hlist_bl_lock(hbl);
10915+ ret &= hlist_bl_empty(hbl);
10916+ hlist_bl_unlock(hbl);
10917+ }
10918+
10919+ return ret;
10920+}
10921+
10922+static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
10923+{
10924+ struct au_dr_hino *found, *ent;
10925+ struct hlist_bl_head *hbl;
10926+ struct hlist_bl_node *pos;
10927+ int idx;
10928+
10929+ found = NULL;
10930+ idx = au_dr_ihash(ino);
10931+ hbl = dr->dr_h_ino + idx;
10932+ hlist_bl_lock(hbl);
10933+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10934+ if (ent->dr_h_ino == ino) {
10935+ found = ent;
10936+ break;
10937+ }
10938+ hlist_bl_unlock(hbl);
10939+
10940+ return found;
10941+}
10942+
10943+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
10944+ struct au_dr_hino *add_ent)
10945+{
10946+ int found, idx;
10947+ struct hlist_bl_head *hbl;
10948+ struct hlist_bl_node *pos;
10949+ struct au_dr_hino *ent;
10950+
10951+ found = 0;
10952+ idx = au_dr_ihash(ino);
10953+ hbl = dr->dr_h_ino + idx;
10954+#if 0 /* debug print */
10955+ {
10956+ struct hlist_bl_node *tmp;
10957+
10958+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10959+ AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
10960+ }
10961+#endif
10962+ hlist_bl_lock(hbl);
10963+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10964+ if (ent->dr_h_ino == ino) {
10965+ found = 1;
10966+ break;
10967+ }
10968+ if (!found && add_ent)
10969+ hlist_bl_add_head(&add_ent->dr_hnode, hbl);
10970+ hlist_bl_unlock(hbl);
10971+
10972+ if (!found && add_ent)
10973+ AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
10974+
10975+ return found;
10976+}
10977+
10978+void au_dr_hino_free(struct au_dr_br *dr)
10979+{
10980+ int i;
10981+ struct hlist_bl_head *hbl;
10982+ struct hlist_bl_node *pos, *tmp;
10983+ struct au_dr_hino *ent;
10984+
10985+ /* SiMustWriteLock(sb); */
10986+
10987+ for (i = 0; i < AuDirren_NHASH; i++) {
10988+ hbl = dr->dr_h_ino + i;
10989+ /* no spinlock since sbinfo must be write-locked */
10990+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10991+ au_kfree_rcu(ent);
10992+ INIT_HLIST_BL_HEAD(hbl);
10993+ }
10994+}
10995+
10996+/* returns the number of inodes or an error */
10997+static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
10998+ struct file *hinofile)
10999+{
11000+ int err, i;
11001+ ssize_t ssz;
11002+ loff_t pos, oldsize;
11003+ __be64 u64;
11004+ struct inode *hinoinode;
11005+ struct hlist_bl_head *hbl;
11006+ struct hlist_bl_node *n1, *n2;
11007+ struct au_dr_hino *ent;
11008+
11009+ SiMustWriteLock(sb);
11010+ AuDebugOn(!au_br_writable(br->br_perm));
11011+
11012+ hinoinode = file_inode(hinofile);
11013+ oldsize = i_size_read(hinoinode);
11014+
11015+ err = 0;
11016+ pos = 0;
11017+ hbl = br->br_dirren.dr_h_ino;
11018+ for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11019+ /* no bit-lock since sbinfo must be write-locked */
11020+ hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11021+ AuDbg("hi%llu, %pD2\n",
11022+ (unsigned long long)ent->dr_h_ino, hinofile);
11023+ u64 = cpu_to_be64(ent->dr_h_ino);
11024+ ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11025+ if (ssz == sizeof(u64))
11026+ continue;
11027+
11028+ /* write error */
11029+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11030+ err = -ENOSPC;
11031+ if (ssz < 0)
11032+ err = ssz;
11033+ break;
11034+ }
11035+ }
11036+ /* regardless the error */
11037+ if (pos < oldsize) {
11038+ err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11039+ AuTraceErr(err);
11040+ }
11041+
11042+ AuTraceErr(err);
11043+ return err;
11044+}
11045+
11046+static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11047+{
11048+ int err, hidx;
11049+ ssize_t ssz;
11050+ size_t sz, n;
11051+ loff_t pos;
11052+ uint64_t u64;
11053+ struct au_dr_hino *ent;
11054+ struct inode *hinoinode;
11055+ struct hlist_bl_head *hbl;
11056+
11057+ err = 0;
11058+ pos = 0;
11059+ hbl = dr->dr_h_ino;
11060+ hinoinode = file_inode(hinofile);
11061+ sz = i_size_read(hinoinode);
11062+ AuDebugOn(sz % sizeof(u64));
11063+ n = sz / sizeof(u64);
11064+ while (n--) {
11065+ ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11066+ if (unlikely(ssz != sizeof(u64))) {
11067+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11068+ err = -EINVAL;
11069+ if (ssz < 0)
11070+ err = ssz;
11071+ goto out_free;
11072+ }
11073+
11074+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11075+ if (!ent) {
11076+ err = -ENOMEM;
11077+ AuTraceErr(err);
11078+ goto out_free;
11079+ }
11080+ ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11081+ AuDbg("hi%llu, %pD2\n",
11082+ (unsigned long long)ent->dr_h_ino, hinofile);
11083+ hidx = au_dr_ihash(ent->dr_h_ino);
11084+ au_hbl_add(&ent->dr_hnode, hbl + hidx);
11085+ }
11086+ goto out; /* success */
11087+
11088+out_free:
11089+ au_dr_hino_free(dr);
11090+out:
11091+ AuTraceErr(err);
11092+ return err;
11093+}
11094+
11095+/*
11096+ * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11097+ * @path is a switch to distinguish load and store.
11098+ */
11099+static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11100+ struct au_branch *br, const struct path *path)
11101+{
11102+ int err, flags;
11103+ unsigned char load, suspend;
11104+ struct file *hinofile;
11105+ struct au_hinode *hdir;
11106+ struct inode *dir, *delegated;
11107+ struct path hinopath;
11108+ struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11109+ sizeof(AUFS_WH_DR_BRHINO) - 1);
11110+
11111+ AuDebugOn(bindex < 0 && !br);
11112+ AuDebugOn(bindex >= 0 && br);
11113+
11114+ err = -EINVAL;
11115+ suspend = !br;
11116+ if (suspend)
11117+ br = au_sbr(sb, bindex);
11118+ load = !!path;
11119+ if (!load) {
11120+ path = &br->br_path;
11121+ AuDebugOn(!au_br_writable(br->br_perm));
11122+ if (unlikely(!au_br_writable(br->br_perm)))
11123+ goto out;
11124+ }
11125+
11126+ hdir = NULL;
11127+ if (suspend) {
11128+ dir = d_inode(sb->s_root);
11129+ hdir = au_hinode(au_ii(dir), bindex);
11130+ dir = hdir->hi_inode;
11131+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11132+ } else {
11133+ dir = d_inode(path->dentry);
11134+ inode_lock_nested(dir, AuLsc_I_CHILD);
11135+ }
11136+ hinopath.dentry = vfsub_lkup_one(&hinoname, path->dentry);
11137+ err = PTR_ERR(hinopath.dentry);
11138+ if (IS_ERR(hinopath.dentry))
11139+ goto out_unlock;
11140+
11141+ err = 0;
11142+ flags = O_RDONLY;
11143+ if (load) {
11144+ if (d_is_negative(hinopath.dentry))
11145+ goto out_dput; /* success */
11146+ } else {
11147+ if (au_dr_hino_test_empty(&br->br_dirren)) {
11148+ if (d_is_positive(hinopath.dentry)) {
11149+ delegated = NULL;
11150+ err = vfsub_unlink(dir, &hinopath, &delegated,
11151+ /*force*/0);
11152+ AuTraceErr(err);
11153+ if (unlikely(err))
11154+ pr_err("ignored err %d, %pd2\n",
11155+ err, hinopath.dentry);
11156+ if (unlikely(err == -EWOULDBLOCK))
11157+ iput(delegated);
11158+ err = 0;
11159+ }
11160+ goto out_dput;
11161+ } else if (!d_is_positive(hinopath.dentry)) {
11162+ err = vfsub_create(dir, &hinopath, 0600,
11163+ /*want_excl*/false);
11164+ AuTraceErr(err);
11165+ if (unlikely(err))
11166+ goto out_dput;
11167+ }
11168+ flags = O_WRONLY;
11169+ }
11170+ hinopath.mnt = path->mnt;
11171+ hinofile = vfsub_dentry_open(&hinopath, flags);
11172+ if (suspend)
11173+ au_hn_inode_unlock(hdir);
11174+ else
11175+ inode_unlock(dir);
11176+ dput(hinopath.dentry);
11177+ AuTraceErrPtr(hinofile);
11178+ if (IS_ERR(hinofile)) {
11179+ err = PTR_ERR(hinofile);
11180+ goto out;
11181+ }
11182+
11183+ if (load)
11184+ err = au_dr_hino_load(&br->br_dirren, hinofile);
11185+ else
11186+ err = au_dr_hino_store(sb, br, hinofile);
11187+ fput(hinofile);
11188+ goto out;
11189+
11190+out_dput:
11191+ dput(hinopath.dentry);
11192+out_unlock:
11193+ if (suspend)
11194+ au_hn_inode_unlock(hdir);
11195+ else
11196+ inode_unlock(dir);
11197+out:
11198+ AuTraceErr(err);
11199+ return err;
11200+}
11201+
11202+/* ---------------------------------------------------------------------- */
11203+
11204+static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11205+{
11206+ int err;
11207+ struct kstatfs kstfs;
11208+ dev_t dev;
11209+ struct dentry *dentry;
11210+ struct super_block *sb;
11211+
11212+ err = vfs_statfs((void *)path, &kstfs);
11213+ AuTraceErr(err);
11214+ if (unlikely(err))
11215+ goto out;
11216+
11217+ /* todo: support for UUID */
11218+
11219+ if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11220+ brid->type = AuBrid_FSID;
11221+ brid->fsid = kstfs.f_fsid;
11222+ } else {
11223+ dentry = path->dentry;
11224+ sb = dentry->d_sb;
11225+ dev = sb->s_dev;
11226+ if (dev) {
11227+ brid->type = AuBrid_DEV;
11228+ brid->dev = dev;
11229+ }
11230+ }
11231+
11232+out:
11233+ return err;
11234+}
11235+
11236+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11237+ const struct path *path)
11238+{
11239+ int err, i;
11240+ struct au_dr_br *dr;
11241+ struct hlist_bl_head *hbl;
11242+
11243+ dr = &br->br_dirren;
11244+ hbl = dr->dr_h_ino;
11245+ for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11246+ INIT_HLIST_BL_HEAD(hbl);
11247+
11248+ err = au_dr_brid_init(&dr->dr_brid, path);
11249+ if (unlikely(err))
11250+ goto out;
11251+
11252+ if (au_opt_test(au_mntflags(sb), DIRREN))
11253+ err = au_dr_hino(sb, /*bindex*/-1, br, path);
11254+
11255+out:
11256+ AuTraceErr(err);
11257+ return err;
11258+}
11259+
11260+int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11261+{
11262+ int err;
11263+
11264+ err = 0;
11265+ if (au_br_writable(br->br_perm))
11266+ err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11267+ if (!err)
11268+ au_dr_hino_free(&br->br_dirren);
11269+
11270+ return err;
11271+}
11272+
11273+/* ---------------------------------------------------------------------- */
11274+
11275+static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11276+ char *buf, size_t sz)
11277+{
11278+ int err;
11279+ unsigned int major, minor;
11280+ char *p;
11281+
11282+ p = buf;
11283+ err = snprintf(p, sz, "%d_", brid->type);
11284+ AuDebugOn(err > sz);
11285+ p += err;
11286+ sz -= err;
11287+ switch (brid->type) {
11288+ case AuBrid_Unset:
11289+ return -EINVAL;
11290+ case AuBrid_UUID:
11291+ err = snprintf(p, sz, "%pU", brid->uuid.b);
11292+ break;
11293+ case AuBrid_FSID:
11294+ err = snprintf(p, sz, "%08x-%08x",
11295+ brid->fsid.val[0], brid->fsid.val[1]);
11296+ break;
11297+ case AuBrid_DEV:
11298+ major = MAJOR(brid->dev);
11299+ minor = MINOR(brid->dev);
11300+ if (major <= 0xff && minor <= 0xff)
11301+ err = snprintf(p, sz, "%02x%02x", major, minor);
11302+ else
11303+ err = snprintf(p, sz, "%03x:%05x", major, minor);
11304+ break;
11305+ }
11306+ AuDebugOn(err > sz);
11307+ p += err;
11308+ sz -= err;
11309+ err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11310+ AuDebugOn(err > sz);
11311+ p += err;
11312+ sz -= err;
11313+
11314+ return p - buf;
11315+}
11316+
11317+static int au_drinfo_name(struct au_branch *br, char *name, int len)
11318+{
11319+ int rlen;
11320+ struct dentry *br_dentry;
11321+ struct inode *br_inode;
11322+
11323+ br_dentry = au_br_dentry(br);
11324+ br_inode = d_inode(br_dentry);
11325+ rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11326+ AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11327+ AuDebugOn(rlen > len);
11328+
11329+ return rlen;
11330+}
11331+
11332+/* ---------------------------------------------------------------------- */
11333+
11334+/*
11335+ * from the given @h_dentry, construct drinfo at @*fdata.
11336+ * when the size of @*fdata is not enough, reallocate and return new @fdata and
11337+ * @allocated.
11338+ */
11339+static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11340+ struct dentry *h_dentry,
11341+ unsigned char *allocated)
11342+{
11343+ int err, v;
11344+ struct au_drinfo_fdata *f, *p;
11345+ struct au_drinfo *drinfo;
11346+ struct inode *h_inode;
11347+ struct qstr *qname;
11348+
11349+ err = 0;
11350+ f = *fdata;
11351+ h_inode = d_inode(h_dentry);
11352+ qname = &h_dentry->d_name;
11353+ drinfo = &f->drinfo;
11354+ drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11355+ drinfo->oldnamelen = qname->len;
11356+ if (*allocated < sizeof(*f) + qname->len) {
11357+ v = roundup_pow_of_two(*allocated + qname->len);
11358+ p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11359+ if (unlikely(!p)) {
11360+ err = -ENOMEM;
11361+ AuTraceErr(err);
11362+ goto out;
11363+ }
11364+ f = p;
11365+ *fdata = f;
11366+ *allocated = v;
11367+ drinfo = &f->drinfo;
11368+ }
11369+ memcpy(drinfo->oldname, qname->name, qname->len);
11370+ AuDbg("i%llu, %.*s\n",
11371+ be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11372+ drinfo->oldname);
11373+
11374+out:
11375+ AuTraceErr(err);
11376+ return err;
11377+}
11378+
11379+/* callers have to free the return value */
11380+static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11381+{
11382+ struct au_drinfo *ret, *drinfo;
11383+ struct au_drinfo_fdata fdata;
11384+ int len;
11385+ loff_t pos;
11386+ ssize_t ssz;
11387+
11388+ ret = ERR_PTR(-EIO);
11389+ pos = 0;
11390+ ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11391+ if (unlikely(ssz != sizeof(fdata))) {
11392+ AuIOErr("ssz %zd, %u, %pD2\n",
11393+ ssz, (unsigned int)sizeof(fdata), file);
11394+ goto out;
11395+ }
11396+
11397+ fdata.magic = ntohl((__force __be32)fdata.magic);
11398+ switch (fdata.magic) {
11399+ case AUFS_DRINFO_MAGIC_V1:
11400+ break;
11401+ default:
11402+ AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11403+ fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11404+ goto out;
11405+ }
11406+
11407+ drinfo = &fdata.drinfo;
11408+ len = drinfo->oldnamelen;
11409+ if (!len) {
11410+ AuIOErr("broken drinfo %pD2\n", file);
11411+ goto out;
11412+ }
11413+
11414+ ret = NULL;
11415+ drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11416+ if (unlikely(h_ino && drinfo->ino != h_ino)) {
11417+ AuDbg("ignored i%llu, i%llu, %pD2\n",
11418+ (unsigned long long)drinfo->ino,
11419+ (unsigned long long)h_ino, file);
11420+ goto out; /* success */
11421+ }
11422+
11423+ ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11424+ if (unlikely(!ret)) {
11425+ ret = ERR_PTR(-ENOMEM);
11426+ AuTraceErrPtr(ret);
11427+ goto out;
11428+ }
11429+
11430+ *ret = *drinfo;
11431+ ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11432+ if (unlikely(ssz != len)) {
11433+ au_kfree_rcu(ret);
11434+ ret = ERR_PTR(-EIO);
11435+ AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11436+ goto out;
11437+ }
11438+
11439+ AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11440+
11441+out:
11442+ return ret;
11443+}
11444+
11445+/* ---------------------------------------------------------------------- */
11446+
11447+/* in order to be revertible */
11448+struct au_drinfo_rev_elm {
11449+ int created;
11450+ struct dentry *info_dentry;
11451+ struct au_drinfo *info_last;
11452+};
11453+
11454+struct au_drinfo_rev {
11455+ unsigned char already;
11456+ aufs_bindex_t nelm;
11457+ struct au_drinfo_rev_elm elm[];
11458+};
11459+
11460+/* todo: isn't it too large? */
11461+struct au_drinfo_store {
11462+ struct path h_ppath;
11463+ struct dentry *h_dentry;
11464+ struct au_drinfo_fdata *fdata;
11465+ char *infoname; /* inside of whname, just after PFX */
11466+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11467+ aufs_bindex_t btgt, btail;
11468+ unsigned char no_sio,
11469+ allocated, /* current size of *fdata */
11470+ infonamelen, /* room size for p */
11471+ whnamelen, /* length of the generated name */
11472+ renameback; /* renamed back */
11473+};
11474+
11475+/* on rename(2) error, the caller should revert it using @elm */
11476+static int au_drinfo_do_store(struct au_drinfo_store *w,
11477+ struct au_drinfo_rev_elm *elm)
11478+{
11479+ int err, len;
11480+ ssize_t ssz;
11481+ loff_t pos;
11482+ struct path infopath = {
11483+ .mnt = w->h_ppath.mnt
11484+ };
11485+ struct inode *h_dir, *h_inode, *delegated;
11486+ struct file *infofile;
11487+ struct qstr *qname;
11488+
11489+ AuDebugOn(elm
11490+ && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11491+
11492+ infopath.dentry = vfsub_lookup_one_len(w->whname, w->h_ppath.dentry,
11493+ w->whnamelen);
11494+ AuTraceErrPtr(infopath.dentry);
11495+ if (IS_ERR(infopath.dentry)) {
11496+ err = PTR_ERR(infopath.dentry);
11497+ goto out;
11498+ }
11499+
11500+ err = 0;
11501+ h_dir = d_inode(w->h_ppath.dentry);
11502+ if (elm && d_is_negative(infopath.dentry)) {
11503+ err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11504+ AuTraceErr(err);
11505+ if (unlikely(err))
11506+ goto out_dput;
11507+ elm->created = 1;
11508+ elm->info_dentry = dget(infopath.dentry);
11509+ }
11510+
11511+ infofile = vfsub_dentry_open(&infopath, O_RDWR);
11512+ AuTraceErrPtr(infofile);
11513+ if (IS_ERR(infofile)) {
11514+ err = PTR_ERR(infofile);
11515+ goto out_dput;
11516+ }
11517+
11518+ h_inode = d_inode(infopath.dentry);
11519+ if (elm && i_size_read(h_inode)) {
11520+ h_inode = d_inode(w->h_dentry);
11521+ elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11522+ AuTraceErrPtr(elm->info_last);
11523+ if (IS_ERR(elm->info_last)) {
11524+ err = PTR_ERR(elm->info_last);
11525+ elm->info_last = NULL;
11526+ AuDebugOn(elm->info_dentry);
11527+ goto out_fput;
11528+ }
11529+ }
11530+
11531+ if (elm && w->renameback) {
11532+ delegated = NULL;
11533+ err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11534+ AuTraceErr(err);
11535+ if (unlikely(err == -EWOULDBLOCK))
11536+ iput(delegated);
11537+ goto out_fput;
11538+ }
11539+
11540+ pos = 0;
11541+ qname = &w->h_dentry->d_name;
11542+ len = sizeof(*w->fdata) + qname->len;
11543+ if (!elm)
11544+ len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11545+ ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11546+ if (ssz == len) {
11547+ AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11548+ w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11549+ goto out_fput; /* success */
11550+ } else {
11551+ err = -EIO;
11552+ if (ssz < 0)
11553+ err = ssz;
11554+ /* the caller should revert it using @elm */
11555+ }
11556+
11557+out_fput:
11558+ fput(infofile);
11559+out_dput:
11560+ dput(infopath.dentry);
11561+out:
11562+ AuTraceErr(err);
11563+ return err;
11564+}
11565+
11566+struct au_call_drinfo_do_store_args {
11567+ int *errp;
11568+ struct au_drinfo_store *w;
11569+ struct au_drinfo_rev_elm *elm;
11570+};
11571+
11572+static void au_call_drinfo_do_store(void *args)
11573+{
11574+ struct au_call_drinfo_do_store_args *a = args;
11575+
11576+ *a->errp = au_drinfo_do_store(a->w, a->elm);
11577+}
11578+
11579+static int au_drinfo_store_sio(struct au_drinfo_store *w,
11580+ struct au_drinfo_rev_elm *elm)
11581+{
11582+ int err, wkq_err;
11583+
11584+ if (w->no_sio)
11585+ err = au_drinfo_do_store(w, elm);
11586+ else {
11587+ struct au_call_drinfo_do_store_args a = {
11588+ .errp = &err,
11589+ .w = w,
11590+ .elm = elm
11591+ };
11592+ wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11593+ if (unlikely(wkq_err))
11594+ err = wkq_err;
11595+ }
11596+ AuTraceErr(err);
11597+
11598+ return err;
11599+}
11600+
11601+static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11602+ aufs_bindex_t btgt)
11603+{
11604+ int err;
11605+
11606+ memset(w, 0, sizeof(*w));
11607+ w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11608+ strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11609+ w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11610+ w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11611+ w->btgt = btgt;
11612+ w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11613+
11614+ err = -ENOMEM;
11615+ w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11616+ if (unlikely(!w->fdata)) {
11617+ AuTraceErr(err);
11618+ goto out;
11619+ }
11620+ w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11621+ err = 0;
11622+
11623+out:
11624+ return err;
11625+}
11626+
11627+static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11628+{
11629+ au_kfree_rcu(w->fdata);
11630+}
11631+
11632+static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11633+ struct au_drinfo_store *w)
11634+{
11635+ struct au_drinfo_rev_elm *elm;
11636+ struct inode *h_dir, *delegated;
11637+ int err, nelm;
11638+ struct path infopath = {
11639+ .mnt = w->h_ppath.mnt
11640+ };
11641+
11642+ h_dir = d_inode(w->h_ppath.dentry);
11643+ IMustLock(h_dir);
11644+
11645+ err = 0;
11646+ elm = rev->elm;
11647+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11648+ AuDebugOn(elm->created && elm->info_last);
11649+ if (elm->created) {
11650+ AuDbg("here\n");
11651+ delegated = NULL;
11652+ infopath.dentry = elm->info_dentry;
11653+ err = vfsub_unlink(h_dir, &infopath, &delegated,
11654+ !w->no_sio);
11655+ AuTraceErr(err);
11656+ if (unlikely(err == -EWOULDBLOCK))
11657+ iput(delegated);
11658+ dput(elm->info_dentry);
11659+ } else if (elm->info_last) {
11660+ AuDbg("here\n");
11661+ w->fdata->drinfo = *elm->info_last;
11662+ memcpy(w->fdata->drinfo.oldname,
11663+ elm->info_last->oldname,
11664+ elm->info_last->oldnamelen);
11665+ err = au_drinfo_store_sio(w, /*elm*/NULL);
11666+ au_kfree_rcu(elm->info_last);
11667+ }
11668+ if (unlikely(err))
11669+ AuIOErr("%d, %s\n", err, w->whname);
11670+ /* go on even if err */
11671+ }
11672+}
11673+
11674+/* caller has to call au_dr_rename_fin() later */
11675+static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11676+ struct qstr *dst_name, void *_rev)
11677+{
11678+ int err, sz, nelm;
11679+ aufs_bindex_t bindex, btail;
11680+ struct au_drinfo_store work;
11681+ struct au_drinfo_rev *rev, **p;
11682+ struct au_drinfo_rev_elm *elm;
11683+ struct super_block *sb;
11684+ struct au_branch *br;
11685+ struct au_hinode *hdir;
11686+
11687+ err = au_drinfo_store_work_init(&work, btgt);
11688+ AuTraceErr(err);
11689+ if (unlikely(err))
11690+ goto out;
11691+
11692+ err = -ENOMEM;
11693+ btail = au_dbtaildir(dentry);
11694+ nelm = btail - btgt;
11695+ sz = sizeof(*rev) + sizeof(*elm) * nelm;
11696+ rev = kcalloc(1, sz, GFP_NOFS);
11697+ if (unlikely(!rev)) {
11698+ AuTraceErr(err);
11699+ goto out_args;
11700+ }
11701+ rev->nelm = nelm;
11702+ elm = rev->elm;
11703+ p = _rev;
11704+ *p = rev;
11705+
11706+ err = 0;
11707+ sb = dentry->d_sb;
11708+ work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11709+ work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11710+ hdir = au_hi(d_inode(dentry), btgt);
11711+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11712+ for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11713+ work.h_dentry = au_h_dptr(dentry, bindex);
11714+ if (!work.h_dentry)
11715+ continue;
11716+
11717+ err = au_drinfo_construct(&work.fdata, work.h_dentry,
11718+ &work.allocated);
11719+ AuTraceErr(err);
11720+ if (unlikely(err))
11721+ break;
11722+
11723+ work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11724+ br = au_sbr(sb, bindex);
11725+ work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11726+ work.whnamelen += au_drinfo_name(br, work.infoname,
11727+ work.infonamelen);
11728+ AuDbg("whname %.*s, i%llu, %.*s\n",
11729+ work.whnamelen, work.whname,
11730+ be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11731+ work.fdata->drinfo.oldnamelen,
11732+ work.fdata->drinfo.oldname);
11733+
11734+ err = au_drinfo_store_sio(&work, elm);
11735+ AuTraceErr(err);
11736+ if (unlikely(err))
11737+ break;
11738+ }
11739+ if (unlikely(err)) {
11740+ /* revert all drinfo */
11741+ au_drinfo_store_rev(rev, &work);
11742+ au_kfree_try_rcu(rev);
11743+ *p = NULL;
11744+ }
11745+ au_hn_inode_unlock(hdir);
11746+
11747+out_args:
11748+ au_drinfo_store_work_fin(&work);
11749+out:
11750+ return err;
11751+}
11752+
11753+/* ---------------------------------------------------------------------- */
11754+
11755+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11756+ struct qstr *dst_name, void *_rev)
11757+{
11758+ int err, already;
11759+ ino_t ino;
11760+ struct super_block *sb;
11761+ struct au_branch *br;
11762+ struct au_dr_br *dr;
11763+ struct dentry *h_dentry;
11764+ struct inode *h_inode;
11765+ struct au_dr_hino *ent;
11766+ struct au_drinfo_rev *rev, **p;
11767+
11768+ AuDbg("bindex %d\n", bindex);
11769+
11770+ err = -ENOMEM;
11771+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11772+ if (unlikely(!ent))
11773+ goto out;
11774+
11775+ sb = src->d_sb;
11776+ br = au_sbr(sb, bindex);
11777+ dr = &br->br_dirren;
11778+ h_dentry = au_h_dptr(src, bindex);
11779+ h_inode = d_inode(h_dentry);
11780+ ino = h_inode->i_ino;
11781+ ent->dr_h_ino = ino;
11782+ already = au_dr_hino_test_add(dr, ino, ent);
11783+ AuDbg("b%d, hi%llu, already %d\n",
11784+ bindex, (unsigned long long)ino, already);
11785+
11786+ err = au_drinfo_store(src, bindex, dst_name, _rev);
11787+ AuTraceErr(err);
11788+ if (!err) {
11789+ p = _rev;
11790+ rev = *p;
11791+ rev->already = already;
11792+ goto out; /* success */
11793+ }
11794+
11795+ /* revert */
11796+ if (!already)
11797+ au_dr_hino_del(dr, ent);
11798+ au_kfree_rcu(ent);
11799+
11800+out:
11801+ AuTraceErr(err);
11802+ return err;
11803+}
11804+
11805+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11806+{
11807+ struct au_drinfo_rev *rev;
11808+ struct au_drinfo_rev_elm *elm;
11809+ int nelm;
11810+
11811+ rev = _rev;
11812+ elm = rev->elm;
11813+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11814+ dput(elm->info_dentry);
11815+ au_kfree_rcu(elm->info_last);
11816+ }
11817+ au_kfree_try_rcu(rev);
11818+}
11819+
11820+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11821+{
11822+ int err;
11823+ struct au_drinfo_store work;
11824+ struct au_drinfo_rev *rev = _rev;
11825+ struct super_block *sb;
11826+ struct au_branch *br;
11827+ struct inode *h_inode;
11828+ struct au_dr_br *dr;
11829+ struct au_dr_hino *ent;
11830+
11831+ err = au_drinfo_store_work_init(&work, btgt);
11832+ if (unlikely(err))
11833+ goto out;
11834+
11835+ sb = src->d_sb;
11836+ br = au_sbr(sb, btgt);
11837+ work.h_ppath.dentry = au_h_dptr(src, btgt);
11838+ work.h_ppath.mnt = au_br_mnt(br);
11839+ au_drinfo_store_rev(rev, &work);
11840+ au_drinfo_store_work_fin(&work);
11841+ if (rev->already)
11842+ goto out;
11843+
11844+ dr = &br->br_dirren;
11845+ h_inode = d_inode(work.h_ppath.dentry);
11846+ ent = au_dr_hino_find(dr, h_inode->i_ino);
11847+ BUG_ON(!ent);
11848+ au_dr_hino_del(dr, ent);
11849+ au_kfree_rcu(ent);
11850+
11851+out:
11852+ au_kfree_try_rcu(rev);
11853+ if (unlikely(err))
11854+ pr_err("failed to remove dirren info\n");
11855+}
11856+
11857+/* ---------------------------------------------------------------------- */
11858+
11859+static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11860+ char *whname, int whnamelen,
11861+ struct dentry **info_dentry)
11862+{
11863+ struct au_drinfo *drinfo;
11864+ struct file *f;
11865+ struct inode *h_dir;
11866+ struct path infopath;
11867+ int unlocked;
11868+
11869+ AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11870+
11871+ *info_dentry = NULL;
11872+ drinfo = NULL;
11873+ unlocked = 0;
11874+ h_dir = d_inode(h_ppath->dentry);
11875+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
11876+ infopath.dentry = vfsub_lookup_one_len(whname, h_ppath->dentry,
11877+ whnamelen);
11878+ if (IS_ERR(infopath.dentry)) {
11879+ drinfo = (void *)infopath.dentry;
11880+ goto out;
11881+ }
11882+
11883+ if (d_is_negative(infopath.dentry))
11884+ goto out_dput; /* success */
11885+
11886+ infopath.mnt = h_ppath->mnt;
11887+ f = vfsub_dentry_open(&infopath, O_RDONLY);
11888+ inode_unlock_shared(h_dir);
11889+ unlocked = 1;
11890+ if (IS_ERR(f)) {
11891+ drinfo = (void *)f;
11892+ goto out_dput;
11893+ }
11894+
11895+ drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11896+ if (IS_ERR_OR_NULL(drinfo))
11897+ goto out_fput;
11898+
11899+ AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11900+ *info_dentry = dget(infopath.dentry); /* keep it alive */
11901+
11902+out_fput:
11903+ fput(f);
11904+out_dput:
11905+ dput(infopath.dentry);
11906+out:
11907+ if (!unlocked)
11908+ inode_unlock_shared(h_dir);
11909+ AuTraceErrPtr(drinfo);
11910+ return drinfo;
11911+}
11912+
11913+struct au_drinfo_do_load_args {
11914+ struct au_drinfo **drinfop;
11915+ struct path *h_ppath;
11916+ char *whname;
11917+ int whnamelen;
11918+ struct dentry **info_dentry;
11919+};
11920+
11921+static void au_call_drinfo_do_load(void *args)
11922+{
11923+ struct au_drinfo_do_load_args *a = args;
11924+
11925+ *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
11926+ a->info_dentry);
11927+}
11928+
11929+struct au_drinfo_load {
11930+ struct path h_ppath;
11931+ struct qstr *qname;
11932+ unsigned char no_sio;
11933+
11934+ aufs_bindex_t ninfo;
11935+ struct au_drinfo **drinfo;
11936+};
11937+
11938+static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
11939+ struct au_branch *br)
11940+{
11941+ int err, wkq_err, whnamelen, e;
11942+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
11943+ = AUFS_WH_DR_INFO_PFX;
11944+ struct au_drinfo *drinfo;
11945+ struct qstr oldname;
11946+ struct inode *h_dir, *delegated;
11947+ struct dentry *info_dentry;
11948+ struct path infopath;
11949+
11950+ whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11951+ whnamelen += au_drinfo_name(br, whname + whnamelen,
11952+ sizeof(whname) - whnamelen);
11953+ if (w->no_sio)
11954+ drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
11955+ &info_dentry);
11956+ else {
11957+ struct au_drinfo_do_load_args args = {
11958+ .drinfop = &drinfo,
11959+ .h_ppath = &w->h_ppath,
11960+ .whname = whname,
11961+ .whnamelen = whnamelen,
11962+ .info_dentry = &info_dentry
11963+ };
11964+ wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
11965+ if (unlikely(wkq_err))
11966+ drinfo = ERR_PTR(wkq_err);
11967+ }
11968+ err = PTR_ERR(drinfo);
11969+ if (IS_ERR_OR_NULL(drinfo))
11970+ goto out;
11971+
11972+ err = 0;
11973+ oldname.len = drinfo->oldnamelen;
11974+ oldname.name = drinfo->oldname;
11975+ if (au_qstreq(w->qname, &oldname)) {
11976+ /* the name is renamed back */
11977+ au_kfree_rcu(drinfo);
11978+ drinfo = NULL;
11979+
11980+ infopath.dentry = info_dentry;
11981+ infopath.mnt = w->h_ppath.mnt;
11982+ h_dir = d_inode(w->h_ppath.dentry);
11983+ delegated = NULL;
11984+ inode_lock_nested(h_dir, AuLsc_I_PARENT);
11985+ e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
11986+ inode_unlock(h_dir);
11987+ if (unlikely(e))
11988+ AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
11989+ if (unlikely(e == -EWOULDBLOCK))
11990+ iput(delegated);
11991+ }
11992+ au_kfree_rcu(w->drinfo[bindex]);
11993+ w->drinfo[bindex] = drinfo;
11994+ dput(info_dentry);
11995+
11996+out:
11997+ AuTraceErr(err);
11998+ return err;
11999+}
12000+
12001+/* ---------------------------------------------------------------------- */
12002+
12003+static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12004+{
12005+ struct au_drinfo **p = drinfo;
12006+
12007+ while (n-- > 0)
12008+ au_kfree_rcu(*drinfo++);
12009+ au_kfree_try_rcu(p);
12010+}
12011+
12012+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12013+ aufs_bindex_t btgt)
12014+{
12015+ int err, ninfo;
12016+ struct au_drinfo_load w;
12017+ aufs_bindex_t bindex, bbot;
12018+ struct au_branch *br;
12019+ struct inode *h_dir;
12020+ struct au_dr_hino *ent;
12021+ struct super_block *sb;
12022+
12023+ AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12024+ AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12025+ AuLNPair(&lkup->whname), btgt);
12026+
12027+ sb = dentry->d_sb;
12028+ bbot = au_sbbot(sb);
12029+ w.ninfo = bbot + 1;
12030+ if (!lkup->dirren.drinfo) {
12031+ lkup->dirren.drinfo = kcalloc(w.ninfo,
12032+ sizeof(*lkup->dirren.drinfo),
12033+ GFP_NOFS);
12034+ if (unlikely(!lkup->dirren.drinfo)) {
12035+ err = -ENOMEM;
12036+ goto out;
12037+ }
12038+ lkup->dirren.ninfo = w.ninfo;
12039+ }
12040+ w.drinfo = lkup->dirren.drinfo;
12041+ w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12042+ w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12043+ AuDebugOn(!w.h_ppath.dentry);
12044+ w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12045+ w.qname = &dentry->d_name;
12046+
12047+ ninfo = 0;
12048+ for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12049+ br = au_sbr(sb, bindex);
12050+ err = au_drinfo_load(&w, bindex, br);
12051+ if (unlikely(err))
12052+ goto out_free;
12053+ if (w.drinfo[bindex])
12054+ ninfo++;
12055+ }
12056+ if (!ninfo) {
12057+ br = au_sbr(sb, btgt);
12058+ h_dir = d_inode(w.h_ppath.dentry);
12059+ ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12060+ AuDebugOn(!ent);
12061+ au_dr_hino_del(&br->br_dirren, ent);
12062+ au_kfree_rcu(ent);
12063+ }
12064+ goto out; /* success */
12065+
12066+out_free:
12067+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12068+ lkup->dirren.ninfo = 0;
12069+ lkup->dirren.drinfo = NULL;
12070+out:
12071+ AuTraceErr(err);
12072+ return err;
12073+}
12074+
12075+void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12076+{
12077+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12078+}
12079+
12080+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12081+{
12082+ int err;
12083+ struct au_drinfo *drinfo;
12084+
12085+ err = 0;
12086+ if (!lkup->dirren.drinfo)
12087+ goto out;
12088+ AuDebugOn(lkup->dirren.ninfo <= btgt);
12089+ drinfo = lkup->dirren.drinfo[btgt];
12090+ if (!drinfo)
12091+ goto out;
12092+
12093+ au_kfree_try_rcu(lkup->whname.name);
12094+ lkup->whname.name = NULL;
12095+ lkup->dirren.dr_name.len = drinfo->oldnamelen;
12096+ lkup->dirren.dr_name.name = drinfo->oldname;
12097+ lkup->name = &lkup->dirren.dr_name;
12098+ err = au_wh_name_alloc(&lkup->whname, lkup->name);
12099+ if (!err)
12100+ AuDbg("name %.*s, whname %.*s, b%d\n",
12101+ AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12102+ btgt);
12103+
12104+out:
12105+ AuTraceErr(err);
12106+ return err;
12107+}
12108+
12109+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12110+ ino_t h_ino)
12111+{
12112+ int match;
12113+ struct au_drinfo *drinfo;
12114+
12115+ match = 1;
12116+ if (!lkup->dirren.drinfo)
12117+ goto out;
12118+ AuDebugOn(lkup->dirren.ninfo <= bindex);
12119+ drinfo = lkup->dirren.drinfo[bindex];
12120+ if (!drinfo)
12121+ goto out;
12122+
12123+ match = (drinfo->ino == h_ino);
12124+ AuDbg("match %d\n", match);
12125+
12126+out:
12127+ return match;
12128+}
12129+
12130+/* ---------------------------------------------------------------------- */
12131+
12132+int au_dr_opt_set(struct super_block *sb)
12133+{
12134+ int err;
12135+ aufs_bindex_t bindex, bbot;
12136+ struct au_branch *br;
12137+
12138+ err = 0;
12139+ bbot = au_sbbot(sb);
12140+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12141+ br = au_sbr(sb, bindex);
12142+ err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12143+ }
12144+
12145+ return err;
12146+}
12147+
12148+int au_dr_opt_flush(struct super_block *sb)
12149+{
12150+ int err;
12151+ aufs_bindex_t bindex, bbot;
12152+ struct au_branch *br;
12153+
12154+ err = 0;
12155+ bbot = au_sbbot(sb);
12156+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12157+ br = au_sbr(sb, bindex);
12158+ if (au_br_writable(br->br_perm))
12159+ err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12160+ }
12161+
12162+ return err;
12163+}
12164+
12165+int au_dr_opt_clr(struct super_block *sb, int no_flush)
12166+{
12167+ int err;
12168+ aufs_bindex_t bindex, bbot;
12169+ struct au_branch *br;
12170+
12171+ err = 0;
12172+ if (!no_flush) {
12173+ err = au_dr_opt_flush(sb);
12174+ if (unlikely(err))
12175+ goto out;
12176+ }
12177+
12178+ bbot = au_sbbot(sb);
12179+ for (bindex = 0; bindex <= bbot; bindex++) {
12180+ br = au_sbr(sb, bindex);
12181+ au_dr_hino_free(&br->br_dirren);
12182+ }
12183+
12184+out:
12185+ return err;
12186+}
12187diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12188--- /usr/share/empty/fs/aufs/dirren.h 1970-01-01 01:00:00.000000000 +0100
12189+++ linux/fs/aufs/dirren.h 2021-02-22 22:51:59.058078584 +0100
12190@@ -0,0 +1,140 @@
12191+/* SPDX-License-Identifier: GPL-2.0 */
12192+/*
12193+ * Copyright (C) 2017-2020 Junjiro R. Okajima
12194+ *
12195+ * This program, aufs is free software; you can redistribute it and/or modify
12196+ * it under the terms of the GNU General Public License as published by
12197+ * the Free Software Foundation; either version 2 of the License, or
12198+ * (at your option) any later version.
12199+ *
12200+ * This program is distributed in the hope that it will be useful,
12201+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12202+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12203+ * GNU General Public License for more details.
12204+ *
12205+ * You should have received a copy of the GNU General Public License
12206+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
12207+ */
12208+
12209+/*
12210+ * renamed dir info
12211+ */
12212+
12213+#ifndef __AUFS_DIRREN_H__
12214+#define __AUFS_DIRREN_H__
12215+
12216+#ifdef __KERNEL__
12217+
12218+#include <linux/dcache.h>
12219+#include <linux/statfs.h>
12220+#include <linux/uuid.h>
12221+#include "hbl.h"
12222+
12223+#define AuDirren_NHASH 100
12224+
12225+#ifdef CONFIG_AUFS_DIRREN
12226+enum au_brid_type {
12227+ AuBrid_Unset,
12228+ AuBrid_UUID,
12229+ AuBrid_FSID,
12230+ AuBrid_DEV
12231+};
12232+
12233+struct au_dr_brid {
12234+ enum au_brid_type type;
12235+ union {
12236+ uuid_t uuid; /* unimplemented yet */
12237+ fsid_t fsid;
12238+ dev_t dev;
12239+ };
12240+};
12241+
12242+/* 20 is the max digits length of ulong 64 */
12243+/* brid-type "_" uuid "_" inum */
12244+#define AUFS_DIRREN_FNAME_SZ (1 + 1 + UUID_STRING_LEN + 20)
12245+#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12246+
12247+struct au_dr_hino {
12248+ struct hlist_bl_node dr_hnode;
12249+ ino_t dr_h_ino;
12250+};
12251+
12252+struct au_dr_br {
12253+ struct hlist_bl_head dr_h_ino[AuDirren_NHASH];
12254+ struct au_dr_brid dr_brid;
12255+};
12256+
12257+struct au_dr_lookup {
12258+ /* dr_name is pointed by struct au_do_lookup_args.name */
12259+ struct qstr dr_name; /* subset of dr_info */
12260+ aufs_bindex_t ninfo;
12261+ struct au_drinfo **drinfo;
12262+};
12263+#else
12264+struct au_dr_hino;
12265+/* empty */
12266+struct au_dr_br { };
12267+struct au_dr_lookup { };
12268+#endif
12269+
12270+/* ---------------------------------------------------------------------- */
12271+
12272+struct au_branch;
12273+struct au_do_lookup_args;
12274+struct au_hinode;
12275+#ifdef CONFIG_AUFS_DIRREN
12276+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12277+ struct au_dr_hino *add_ent);
12278+void au_dr_hino_free(struct au_dr_br *dr);
12279+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12280+ const struct path *path);
12281+int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12282+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12283+ struct qstr *dst_name, void *_rev);
12284+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12285+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12286+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12287+ aufs_bindex_t bindex);
12288+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12289+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12290+ ino_t h_ino);
12291+void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12292+int au_dr_opt_set(struct super_block *sb);
12293+int au_dr_opt_flush(struct super_block *sb);
12294+int au_dr_opt_clr(struct super_block *sb, int no_flush);
12295+#else
12296+AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12297+ struct au_dr_hino *add_ent);
12298+AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12299+AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12300+ const struct path *path);
12301+AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12302+AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12303+ struct qstr *dst_name, void *_rev);
12304+AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12305+AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12306+ void *rev);
12307+AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12308+ aufs_bindex_t bindex);
12309+AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12310+AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12311+ aufs_bindex_t bindex, ino_t h_ino);
12312+AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12313+AuStubInt0(au_dr_opt_set, struct super_block *sb);
12314+AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12315+AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12316+#endif
12317+
12318+/* ---------------------------------------------------------------------- */
12319+
12320+#ifdef CONFIG_AUFS_DIRREN
12321+static inline int au_dr_ihash(ino_t h_ino)
12322+{
12323+ return h_ino % AuDirren_NHASH;
12324+}
12325+#else
12326+AuStubInt0(au_dr_ihash, ino_t h_ino);
12327+#endif
12328+
12329+#endif /* __KERNEL__ */
12330+#endif /* __AUFS_DIRREN_H__ */
12331diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12332--- /usr/share/empty/fs/aufs/dynop.c 1970-01-01 01:00:00.000000000 +0100
12333+++ linux/fs/aufs/dynop.c 2021-02-22 22:51:59.058078584 +0100
12334@@ -0,0 +1,368 @@
12335+// SPDX-License-Identifier: GPL-2.0
12336+/*
12337+ * Copyright (C) 2010-2020 Junjiro R. Okajima
12338+ *
12339+ * This program, aufs is free software; you can redistribute it and/or modify
12340+ * it under the terms of the GNU General Public License as published by
12341+ * the Free Software Foundation; either version 2 of the License, or
12342+ * (at your option) any later version.
12343+ *
12344+ * This program is distributed in the hope that it will be useful,
12345+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12346+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12347+ * GNU General Public License for more details.
12348+ *
12349+ * You should have received a copy of the GNU General Public License
12350+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
12351+ */
12352+
12353+/*
12354+ * dynamically customizable operations for regular files
12355+ */
12356+
12357+#include "aufs.h"
12358+
12359+#define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
12360+
12361+/*
12362+ * How large will these lists be?
12363+ * Usually just a few elements, 20-30 at most for each, I guess.
12364+ */
12365+static struct hlist_bl_head dynop[AuDyLast];
12366+
12367+static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12368+ const void *h_op)
12369+{
12370+ struct au_dykey *key, *tmp;
12371+ struct hlist_bl_node *pos;
12372+
12373+ key = NULL;
12374+ hlist_bl_lock(hbl);
12375+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12376+ if (tmp->dk_op.dy_hop == h_op) {
12377+ if (kref_get_unless_zero(&tmp->dk_kref))
12378+ key = tmp;
12379+ break;
12380+ }
12381+ hlist_bl_unlock(hbl);
12382+
12383+ return key;
12384+}
12385+
12386+static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
12387+{
12388+ struct au_dykey **k, *found;
12389+ const void *h_op = key->dk_op.dy_hop;
12390+ int i;
12391+
12392+ found = NULL;
12393+ k = br->br_dykey;
12394+ for (i = 0; i < AuBrDynOp; i++)
12395+ if (k[i]) {
12396+ if (k[i]->dk_op.dy_hop == h_op) {
12397+ found = k[i];
12398+ break;
12399+ }
12400+ } else
12401+ break;
12402+ if (!found) {
12403+ spin_lock(&br->br_dykey_lock);
12404+ for (; i < AuBrDynOp; i++)
12405+ if (k[i]) {
12406+ if (k[i]->dk_op.dy_hop == h_op) {
12407+ found = k[i];
12408+ break;
12409+ }
12410+ } else {
12411+ k[i] = key;
12412+ break;
12413+ }
12414+ spin_unlock(&br->br_dykey_lock);
12415+ BUG_ON(i == AuBrDynOp); /* expand the array */
12416+ }
12417+
12418+ return found;
12419+}
12420+
12421+/* kref_get() if @key is already added */
12422+static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
12423+{
12424+ struct au_dykey *tmp, *found;
12425+ struct hlist_bl_node *pos;
12426+ const void *h_op = key->dk_op.dy_hop;
12427+
12428+ found = NULL;
12429+ hlist_bl_lock(hbl);
12430+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12431+ if (tmp->dk_op.dy_hop == h_op) {
12432+ if (kref_get_unless_zero(&tmp->dk_kref))
12433+ found = tmp;
12434+ break;
12435+ }
12436+ if (!found)
12437+ hlist_bl_add_head(&key->dk_hnode, hbl);
12438+ hlist_bl_unlock(hbl);
12439+
12440+ if (!found)
12441+ DyPrSym(key);
12442+ return found;
12443+}
12444+
12445+static void dy_free_rcu(struct rcu_head *rcu)
12446+{
12447+ struct au_dykey *key;
12448+
12449+ key = container_of(rcu, struct au_dykey, dk_rcu);
12450+ DyPrSym(key);
12451+ kfree(key);
12452+}
12453+
12454+static void dy_free(struct kref *kref)
12455+{
12456+ struct au_dykey *key;
12457+ struct hlist_bl_head *hbl;
12458+
12459+ key = container_of(kref, struct au_dykey, dk_kref);
12460+ hbl = dynop + key->dk_op.dy_type;
12461+ au_hbl_del(&key->dk_hnode, hbl);
12462+ call_rcu(&key->dk_rcu, dy_free_rcu);
12463+}
12464+
12465+void au_dy_put(struct au_dykey *key)
12466+{
12467+ kref_put(&key->dk_kref, dy_free);
12468+}
12469+
12470+/* ---------------------------------------------------------------------- */
12471+
12472+#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12473+
12474+#ifdef CONFIG_AUFS_DEBUG
12475+#define DyDbgDeclare(cnt) unsigned int cnt = 0
12476+#define DyDbgInc(cnt) do { cnt++; } while (0)
12477+#else
12478+#define DyDbgDeclare(cnt) do {} while (0)
12479+#define DyDbgInc(cnt) do {} while (0)
12480+#endif
12481+
12482+#define DySet(func, dst, src, h_op, h_sb) do { \
12483+ DyDbgInc(cnt); \
12484+ if (h_op->func) { \
12485+ if (src.func) \
12486+ dst.func = src.func; \
12487+ else \
12488+ AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
12489+ } \
12490+} while (0)
12491+
12492+#define DySetForce(func, dst, src) do { \
12493+ AuDebugOn(!src.func); \
12494+ DyDbgInc(cnt); \
12495+ dst.func = src.func; \
12496+} while (0)
12497+
12498+#define DySetAop(func) \
12499+ DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12500+#define DySetAopForce(func) \
12501+ DySetForce(func, dyaop->da_op, aufs_aop)
12502+
12503+static void dy_aop(struct au_dykey *key, const void *h_op,
12504+ struct super_block *h_sb __maybe_unused)
12505+{
12506+ struct au_dyaop *dyaop = (void *)key;
12507+ const struct address_space_operations *h_aop = h_op;
12508+ DyDbgDeclare(cnt);
12509+
12510+ AuDbg("%s\n", au_sbtype(h_sb));
12511+
12512+ DySetAop(writepage);
12513+ DySetAopForce(readpage); /* force */
12514+ DySetAop(writepages);
12515+ DySetAop(set_page_dirty);
12516+ DySetAop(readpages);
12517+ DySetAop(readahead);
12518+ DySetAop(write_begin);
12519+ DySetAop(write_end);
12520+ DySetAop(bmap);
12521+ DySetAop(invalidatepage);
12522+ DySetAop(releasepage);
12523+ DySetAop(freepage);
12524+ /* this one will be changed according to an aufs mount option */
12525+ DySetAop(direct_IO);
12526+ DySetAop(migratepage);
12527+ DySetAop(isolate_page);
12528+ DySetAop(putback_page);
12529+ DySetAop(launder_page);
12530+ DySetAop(is_partially_uptodate);
12531+ DySetAop(is_dirty_writeback);
12532+ DySetAop(error_remove_page);
12533+ DySetAop(swap_activate);
12534+ DySetAop(swap_deactivate);
12535+
12536+ DyDbgSize(cnt, *h_aop);
12537+}
12538+
12539+/* ---------------------------------------------------------------------- */
12540+
12541+static void dy_bug(struct kref *kref)
12542+{
12543+ BUG();
12544+}
12545+
12546+static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12547+{
12548+ struct au_dykey *key, *old;
12549+ struct hlist_bl_head *hbl;
12550+ struct op {
12551+ unsigned int sz;
12552+ void (*set)(struct au_dykey *key, const void *h_op,
12553+ struct super_block *h_sb __maybe_unused);
12554+ };
12555+ static const struct op a[] = {
12556+ [AuDy_AOP] = {
12557+ .sz = sizeof(struct au_dyaop),
12558+ .set = dy_aop
12559+ }
12560+ };
12561+ const struct op *p;
12562+
12563+ hbl = dynop + op->dy_type;
12564+ key = dy_gfind_get(hbl, op->dy_hop);
12565+ if (key)
12566+ goto out_add; /* success */
12567+
12568+ p = a + op->dy_type;
12569+ key = kzalloc(p->sz, GFP_NOFS);
12570+ if (unlikely(!key)) {
12571+ key = ERR_PTR(-ENOMEM);
12572+ goto out;
12573+ }
12574+
12575+ key->dk_op.dy_hop = op->dy_hop;
12576+ kref_init(&key->dk_kref);
12577+ p->set(key, op->dy_hop, au_br_sb(br));
12578+ old = dy_gadd(hbl, key);
12579+ if (old) {
12580+ au_kfree_rcu(key);
12581+ key = old;
12582+ }
12583+
12584+out_add:
12585+ old = dy_bradd(br, key);
12586+ if (old)
12587+ /* its ref-count should never be zero here */
12588+ kref_put(&key->dk_kref, dy_bug);
12589+out:
12590+ return key;
12591+}
12592+
12593+/* ---------------------------------------------------------------------- */
12594+/*
12595+ * Aufs prohibits O_DIRECT by default even if the branch supports it.
12596+ * This behaviour is necessary to return an error from open(O_DIRECT) instead
12597+ * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12598+ * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12599+ * See the aufs manual in detail.
12600+ */
12601+static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12602+{
12603+ if (!do_dx)
12604+ dyaop->da_op.direct_IO = NULL;
12605+ else
12606+ dyaop->da_op.direct_IO = aufs_aop.direct_IO;
12607+}
12608+
12609+static struct au_dyaop *dy_aget(struct au_branch *br,
12610+ const struct address_space_operations *h_aop,
12611+ int do_dx)
12612+{
12613+ struct au_dyaop *dyaop;
12614+ struct au_dynop op;
12615+
12616+ op.dy_type = AuDy_AOP;
12617+ op.dy_haop = h_aop;
12618+ dyaop = (void *)dy_get(&op, br);
12619+ if (IS_ERR(dyaop))
12620+ goto out;
12621+ dy_adx(dyaop, do_dx);
12622+
12623+out:
12624+ return dyaop;
12625+}
12626+
12627+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12628+ struct inode *h_inode)
12629+{
12630+ int err, do_dx;
12631+ struct super_block *sb;
12632+ struct au_branch *br;
12633+ struct au_dyaop *dyaop;
12634+
12635+ AuDebugOn(!S_ISREG(h_inode->i_mode));
12636+ IiMustWriteLock(inode);
12637+
12638+ sb = inode->i_sb;
12639+ br = au_sbr(sb, bindex);
12640+ do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12641+ dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12642+ err = PTR_ERR(dyaop);
12643+ if (IS_ERR(dyaop))
12644+ /* unnecessary to call dy_fput() */
12645+ goto out;
12646+
12647+ err = 0;
12648+ inode->i_mapping->a_ops = &dyaop->da_op;
12649+
12650+out:
12651+ return err;
12652+}
12653+
12654+/*
12655+ * Is it safe to replace a_ops during the inode/file is in operation?
12656+ * Yes, I hope so.
12657+ */
12658+int au_dy_irefresh(struct inode *inode)
12659+{
12660+ int err;
12661+ aufs_bindex_t btop;
12662+ struct inode *h_inode;
12663+
12664+ err = 0;
12665+ if (S_ISREG(inode->i_mode)) {
12666+ btop = au_ibtop(inode);
12667+ h_inode = au_h_iptr(inode, btop);
12668+ err = au_dy_iaop(inode, btop, h_inode);
12669+ }
12670+ return err;
12671+}
12672+
12673+void au_dy_arefresh(int do_dx)
12674+{
12675+ struct hlist_bl_head *hbl;
12676+ struct hlist_bl_node *pos;
12677+ struct au_dykey *key;
12678+
12679+ hbl = dynop + AuDy_AOP;
12680+ hlist_bl_lock(hbl);
12681+ hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
12682+ dy_adx((void *)key, do_dx);
12683+ hlist_bl_unlock(hbl);
12684+}
12685+
12686+/* ---------------------------------------------------------------------- */
12687+
12688+void __init au_dy_init(void)
12689+{
12690+ int i;
12691+
12692+ for (i = 0; i < AuDyLast; i++)
12693+ INIT_HLIST_BL_HEAD(dynop + i);
12694+}
12695+
12696+void au_dy_fin(void)
12697+{
12698+ int i;
12699+
12700+ for (i = 0; i < AuDyLast; i++)
12701+ WARN_ON(!hlist_bl_empty(dynop + i));
12702+}
12703diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12704--- /usr/share/empty/fs/aufs/dynop.h 1970-01-01 01:00:00.000000000 +0100
12705+++ linux/fs/aufs/dynop.h 2021-02-22 22:51:59.058078584 +0100
12706@@ -0,0 +1,77 @@
12707+/* SPDX-License-Identifier: GPL-2.0 */
12708+/*
12709+ * Copyright (C) 2010-2020 Junjiro R. Okajima
12710+ *
12711+ * This program, aufs is free software; you can redistribute it and/or modify
12712+ * it under the terms of the GNU General Public License as published by
12713+ * the Free Software Foundation; either version 2 of the License, or
12714+ * (at your option) any later version.
12715+ *
12716+ * This program is distributed in the hope that it will be useful,
12717+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12718+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12719+ * GNU General Public License for more details.
12720+ *
12721+ * You should have received a copy of the GNU General Public License
12722+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
12723+ */
12724+
12725+/*
12726+ * dynamically customizable operations (for regular files only)
12727+ */
12728+
12729+#ifndef __AUFS_DYNOP_H__
12730+#define __AUFS_DYNOP_H__
12731+
12732+#ifdef __KERNEL__
12733+
12734+#include <linux/fs.h>
12735+#include <linux/kref.h>
12736+
12737+enum {AuDy_AOP, AuDyLast};
12738+
12739+struct au_dynop {
12740+ int dy_type;
12741+ union {
12742+ const void *dy_hop;
12743+ const struct address_space_operations *dy_haop;
12744+ };
12745+};
12746+
12747+struct au_dykey {
12748+ union {
12749+ struct hlist_bl_node dk_hnode;
12750+ struct rcu_head dk_rcu;
12751+ };
12752+ struct au_dynop dk_op;
12753+
12754+ /*
12755+ * during I am in the branch local array, kref is gotten. when the
12756+ * branch is removed, kref is put.
12757+ */
12758+ struct kref dk_kref;
12759+};
12760+
12761+/* stop unioning since their sizes are very different from each other */
12762+struct au_dyaop {
12763+ struct au_dykey da_key;
12764+ struct address_space_operations da_op; /* not const */
12765+};
12766+/* make sure that 'struct au_dykey *' can be any type */
12767+static_assert(!offsetof(struct au_dyaop, da_key));
12768+
12769+/* ---------------------------------------------------------------------- */
12770+
12771+/* dynop.c */
12772+struct au_branch;
12773+void au_dy_put(struct au_dykey *key);
12774+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12775+ struct inode *h_inode);
12776+int au_dy_irefresh(struct inode *inode);
12777+void au_dy_arefresh(int do_dio);
12778+
12779+void __init au_dy_init(void);
12780+void au_dy_fin(void);
12781+
12782+#endif /* __KERNEL__ */
12783+#endif /* __AUFS_DYNOP_H__ */
12784diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12785--- /usr/share/empty/fs/aufs/export.c 1970-01-01 01:00:00.000000000 +0100
12786+++ linux/fs/aufs/export.c 2021-02-22 22:51:59.058078584 +0100
12787@@ -0,0 +1,837 @@
12788+// SPDX-License-Identifier: GPL-2.0
12789+/*
12790+ * Copyright (C) 2005-2020 Junjiro R. Okajima
12791+ *
12792+ * This program, aufs is free software; you can redistribute it and/or modify
12793+ * it under the terms of the GNU General Public License as published by
12794+ * the Free Software Foundation; either version 2 of the License, or
12795+ * (at your option) any later version.
12796+ *
12797+ * This program is distributed in the hope that it will be useful,
12798+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12799+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12800+ * GNU General Public License for more details.
12801+ *
12802+ * You should have received a copy of the GNU General Public License
12803+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
12804+ */
12805+
12806+/*
12807+ * export via nfs
12808+ */
12809+
12810+#include <linux/exportfs.h>
12811+#include <linux/fs_struct.h>
12812+#include <linux/namei.h>
12813+#include <linux/nsproxy.h>
12814+#include <linux/random.h>
12815+#include <linux/writeback.h>
12816+#include "aufs.h"
12817+
12818+union conv {
12819+#ifdef CONFIG_AUFS_INO_T_64
12820+ __u32 a[2];
12821+#else
12822+ __u32 a[1];
12823+#endif
12824+ ino_t ino;
12825+};
12826+
12827+static ino_t decode_ino(__u32 *a)
12828+{
12829+ union conv u;
12830+
12831+ BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12832+ u.a[0] = a[0];
12833+#ifdef CONFIG_AUFS_INO_T_64
12834+ u.a[1] = a[1];
12835+#endif
12836+ return u.ino;
12837+}
12838+
12839+static void encode_ino(__u32 *a, ino_t ino)
12840+{
12841+ union conv u;
12842+
12843+ u.ino = ino;
12844+ a[0] = u.a[0];
12845+#ifdef CONFIG_AUFS_INO_T_64
12846+ a[1] = u.a[1];
12847+#endif
12848+}
12849+
12850+/* NFS file handle */
12851+enum {
12852+ Fh_br_id,
12853+ Fh_sigen,
12854+#ifdef CONFIG_AUFS_INO_T_64
12855+ /* support 64bit inode number */
12856+ Fh_ino1,
12857+ Fh_ino2,
12858+ Fh_dir_ino1,
12859+ Fh_dir_ino2,
12860+#else
12861+ Fh_ino1,
12862+ Fh_dir_ino1,
12863+#endif
12864+ Fh_igen,
12865+ Fh_h_type,
12866+ Fh_tail,
12867+
12868+ Fh_ino = Fh_ino1,
12869+ Fh_dir_ino = Fh_dir_ino1
12870+};
12871+
12872+static int au_test_anon(struct dentry *dentry)
12873+{
12874+ /* note: read d_flags without d_lock */
12875+ return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12876+}
12877+
12878+int au_test_nfsd(void)
12879+{
12880+ int ret;
12881+ struct task_struct *tsk = current;
12882+ char comm[sizeof(tsk->comm)];
12883+
12884+ ret = 0;
12885+ if (tsk->flags & PF_KTHREAD) {
12886+ get_task_comm(comm, tsk);
12887+ ret = !strcmp(comm, "nfsd");
12888+ }
12889+
12890+ return ret;
12891+}
12892+
12893+/* ---------------------------------------------------------------------- */
12894+/* inode generation external table */
12895+
12896+void au_xigen_inc(struct inode *inode)
12897+{
12898+ loff_t pos;
12899+ ssize_t sz;
12900+ __u32 igen;
12901+ struct super_block *sb;
12902+ struct au_sbinfo *sbinfo;
12903+
12904+ sb = inode->i_sb;
12905+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
12906+
12907+ sbinfo = au_sbi(sb);
12908+ pos = inode->i_ino;
12909+ pos *= sizeof(igen);
12910+ igen = inode->i_generation + 1;
12911+ sz = xino_fwrite(sbinfo->si_xigen, &igen, sizeof(igen), &pos);
12912+ if (sz == sizeof(igen))
12913+ return; /* success */
12914+
12915+ if (unlikely(sz >= 0))
12916+ AuIOErr("xigen error (%zd)\n", sz);
12917+}
12918+
12919+int au_xigen_new(struct inode *inode)
12920+{
12921+ int err;
12922+ loff_t pos;
12923+ ssize_t sz;
12924+ struct super_block *sb;
12925+ struct au_sbinfo *sbinfo;
12926+ struct file *file;
12927+
12928+ err = 0;
12929+ /* todo: dirty, at mount time */
12930+ if (inode->i_ino == AUFS_ROOT_INO)
12931+ goto out;
12932+ sb = inode->i_sb;
12933+ SiMustAnyLock(sb);
12934+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
12935+ goto out;
12936+
12937+ err = -EFBIG;
12938+ pos = inode->i_ino;
12939+ if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
12940+ AuIOErr1("too large i%lld\n", pos);
12941+ goto out;
12942+ }
12943+ pos *= sizeof(inode->i_generation);
12944+
12945+ err = 0;
12946+ sbinfo = au_sbi(sb);
12947+ file = sbinfo->si_xigen;
12948+ BUG_ON(!file);
12949+
12950+ if (vfsub_f_size_read(file)
12951+ < pos + sizeof(inode->i_generation)) {
12952+ inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
12953+ sz = xino_fwrite(file, &inode->i_generation,
12954+ sizeof(inode->i_generation), &pos);
12955+ } else
12956+ sz = xino_fread(file, &inode->i_generation,
12957+ sizeof(inode->i_generation), &pos);
12958+ if (sz == sizeof(inode->i_generation))
12959+ goto out; /* success */
12960+
12961+ err = sz;
12962+ if (unlikely(sz >= 0)) {
12963+ err = -EIO;
12964+ AuIOErr("xigen error (%zd)\n", sz);
12965+ }
12966+
12967+out:
12968+ return err;
12969+}
12970+
12971+int au_xigen_set(struct super_block *sb, struct path *path)
12972+{
12973+ int err;
12974+ struct au_sbinfo *sbinfo;
12975+ struct file *file;
12976+
12977+ SiMustWriteLock(sb);
12978+
12979+ sbinfo = au_sbi(sb);
12980+ file = au_xino_create2(sb, path, sbinfo->si_xigen);
12981+ err = PTR_ERR(file);
12982+ if (IS_ERR(file))
12983+ goto out;
12984+ err = 0;
12985+ if (sbinfo->si_xigen)
12986+ fput(sbinfo->si_xigen);
12987+ sbinfo->si_xigen = file;
12988+
12989+out:
12990+ AuTraceErr(err);
12991+ return err;
12992+}
12993+
12994+void au_xigen_clr(struct super_block *sb)
12995+{
12996+ struct au_sbinfo *sbinfo;
12997+
12998+ SiMustWriteLock(sb);
12999+
13000+ sbinfo = au_sbi(sb);
13001+ if (sbinfo->si_xigen) {
13002+ fput(sbinfo->si_xigen);
13003+ sbinfo->si_xigen = NULL;
13004+ }
13005+}
13006+
13007+/* ---------------------------------------------------------------------- */
13008+
13009+static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13010+ ino_t dir_ino)
13011+{
13012+ struct dentry *dentry, *d;
13013+ struct inode *inode;
13014+ unsigned int sigen;
13015+
13016+ dentry = NULL;
13017+ inode = ilookup(sb, ino);
13018+ if (!inode)
13019+ goto out;
13020+
13021+ dentry = ERR_PTR(-ESTALE);
13022+ sigen = au_sigen(sb);
13023+ if (unlikely(au_is_bad_inode(inode)
13024+ || IS_DEADDIR(inode)
13025+ || sigen != au_iigen(inode, NULL)))
13026+ goto out_iput;
13027+
13028+ dentry = NULL;
13029+ if (!dir_ino || S_ISDIR(inode->i_mode))
13030+ dentry = d_find_alias(inode);
13031+ else {
13032+ spin_lock(&inode->i_lock);
13033+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
13034+ spin_lock(&d->d_lock);
13035+ if (!au_test_anon(d)
13036+ && d_inode(d->d_parent)->i_ino == dir_ino) {
13037+ dentry = dget_dlock(d);
13038+ spin_unlock(&d->d_lock);
13039+ break;
13040+ }
13041+ spin_unlock(&d->d_lock);
13042+ }
13043+ spin_unlock(&inode->i_lock);
13044+ }
13045+ if (unlikely(dentry && au_digen_test(dentry, sigen))) {
13046+ /* need to refresh */
13047+ dput(dentry);
13048+ dentry = NULL;
13049+ }
13050+
13051+out_iput:
13052+ iput(inode);
13053+out:
13054+ AuTraceErrPtr(dentry);
13055+ return dentry;
13056+}
13057+
13058+/* ---------------------------------------------------------------------- */
13059+
13060+/* todo: dirty? */
13061+/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
13062+
13063+struct au_compare_mnt_args {
13064+ /* input */
13065+ struct super_block *sb;
13066+
13067+ /* output */
13068+ struct vfsmount *mnt;
13069+};
13070+
13071+static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13072+{
13073+ struct au_compare_mnt_args *a = arg;
13074+
13075+ if (mnt->mnt_sb != a->sb)
13076+ return 0;
13077+ a->mnt = mntget(mnt);
13078+ return 1;
13079+}
13080+
13081+static struct vfsmount *au_mnt_get(struct super_block *sb)
13082+{
13083+ int err;
13084+ struct path root;
13085+ struct au_compare_mnt_args args = {
13086+ .sb = sb
13087+ };
13088+
13089+ get_fs_root(current->fs, &root);
13090+ rcu_read_lock();
13091+ err = iterate_mounts(au_compare_mnt, &args, root.mnt);
13092+ rcu_read_unlock();
13093+ path_put(&root);
13094+ AuDebugOn(!err);
13095+ AuDebugOn(!args.mnt);
13096+ return args.mnt;
13097+}
13098+
13099+struct au_nfsd_si_lock {
13100+ unsigned int sigen;
13101+ aufs_bindex_t bindex, br_id;
13102+ unsigned char force_lock;
13103+};
13104+
13105+static int si_nfsd_read_lock(struct super_block *sb,
13106+ struct au_nfsd_si_lock *nsi_lock)
13107+{
13108+ int err;
13109+ aufs_bindex_t bindex;
13110+
13111+ si_read_lock(sb, AuLock_FLUSH);
13112+
13113+ /* branch id may be wrapped around */
13114+ err = 0;
13115+ bindex = au_br_index(sb, nsi_lock->br_id);
13116+ if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13117+ goto out; /* success */
13118+
13119+ err = -ESTALE;
13120+ bindex = -1;
13121+ if (!nsi_lock->force_lock)
13122+ si_read_unlock(sb);
13123+
13124+out:
13125+ nsi_lock->bindex = bindex;
13126+ return err;
13127+}
13128+
13129+struct find_name_by_ino {
13130+ struct dir_context ctx;
13131+ int called, found;
13132+ ino_t ino;
13133+ char *name;
13134+ int namelen;
13135+};
13136+
13137+static int
13138+find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13139+ loff_t offset, u64 ino, unsigned int d_type)
13140+{
13141+ struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13142+ ctx);
13143+
13144+ a->called++;
13145+ if (a->ino != ino)
13146+ return 0;
13147+
13148+ memcpy(a->name, name, namelen);
13149+ a->namelen = namelen;
13150+ a->found = 1;
13151+ return 1;
13152+}
13153+
13154+static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13155+ struct au_nfsd_si_lock *nsi_lock)
13156+{
13157+ struct dentry *dentry, *parent;
13158+ struct file *file;
13159+ struct inode *dir;
13160+ struct find_name_by_ino arg = {
13161+ .ctx = {
13162+ .actor = find_name_by_ino
13163+ }
13164+ };
13165+ int err;
13166+
13167+ parent = path->dentry;
13168+ if (nsi_lock)
13169+ si_read_unlock(parent->d_sb);
13170+ file = vfsub_dentry_open(path, au_dir_roflags);
13171+ dentry = (void *)file;
13172+ if (IS_ERR(file))
13173+ goto out;
13174+
13175+ dentry = ERR_PTR(-ENOMEM);
13176+ arg.name = (void *)__get_free_page(GFP_NOFS);
13177+ if (unlikely(!arg.name))
13178+ goto out_file;
13179+ arg.ino = ino;
13180+ arg.found = 0;
13181+ do {
13182+ arg.called = 0;
13183+ /* smp_mb(); */
13184+ err = vfsub_iterate_dir(file, &arg.ctx);
13185+ } while (!err && !arg.found && arg.called);
13186+ dentry = ERR_PTR(err);
13187+ if (unlikely(err))
13188+ goto out_name;
13189+ /* instead of ENOENT */
13190+ dentry = ERR_PTR(-ESTALE);
13191+ if (!arg.found)
13192+ goto out_name;
13193+
13194+ /* do not call vfsub_lkup_one() */
13195+ dir = d_inode(parent);
13196+ dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
13197+ AuTraceErrPtr(dentry);
13198+ if (IS_ERR(dentry))
13199+ goto out_name;
13200+ AuDebugOn(au_test_anon(dentry));
13201+ if (unlikely(d_really_is_negative(dentry))) {
13202+ dput(dentry);
13203+ dentry = ERR_PTR(-ENOENT);
13204+ }
13205+
13206+out_name:
13207+ free_page((unsigned long)arg.name);
13208+out_file:
13209+ fput(file);
13210+out:
13211+ if (unlikely(nsi_lock
13212+ && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13213+ if (!IS_ERR(dentry)) {
13214+ dput(dentry);
13215+ dentry = ERR_PTR(-ESTALE);
13216+ }
13217+ AuTraceErrPtr(dentry);
13218+ return dentry;
13219+}
13220+
13221+static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13222+ ino_t dir_ino,
13223+ struct au_nfsd_si_lock *nsi_lock)
13224+{
13225+ struct dentry *dentry;
13226+ struct path path;
13227+
13228+ if (dir_ino != AUFS_ROOT_INO) {
13229+ path.dentry = decode_by_ino(sb, dir_ino, 0);
13230+ dentry = path.dentry;
13231+ if (!path.dentry || IS_ERR(path.dentry))
13232+ goto out;
13233+ AuDebugOn(au_test_anon(path.dentry));
13234+ } else
13235+ path.dentry = dget(sb->s_root);
13236+
13237+ path.mnt = au_mnt_get(sb);
13238+ dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13239+ path_put(&path);
13240+
13241+out:
13242+ AuTraceErrPtr(dentry);
13243+ return dentry;
13244+}
13245+
13246+/* ---------------------------------------------------------------------- */
13247+
13248+static int h_acceptable(void *expv, struct dentry *dentry)
13249+{
13250+ return 1;
13251+}
13252+
13253+static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13254+ char *buf, int len, struct super_block *sb)
13255+{
13256+ char *p;
13257+ int n;
13258+ struct path path;
13259+
13260+ p = d_path(h_rootpath, buf, len);
13261+ if (IS_ERR(p))
13262+ goto out;
13263+ n = strlen(p);
13264+
13265+ path.mnt = h_rootpath->mnt;
13266+ path.dentry = h_parent;
13267+ p = d_path(&path, buf, len);
13268+ if (IS_ERR(p))
13269+ goto out;
13270+ if (n != 1)
13271+ p += n;
13272+
13273+ path.mnt = au_mnt_get(sb);
13274+ path.dentry = sb->s_root;
13275+ p = d_path(&path, buf, len - strlen(p));
13276+ mntput(path.mnt);
13277+ if (IS_ERR(p))
13278+ goto out;
13279+ if (n != 1)
13280+ p[strlen(p)] = '/';
13281+
13282+out:
13283+ AuTraceErrPtr(p);
13284+ return p;
13285+}
13286+
13287+static
13288+struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13289+ int fh_len, struct au_nfsd_si_lock *nsi_lock)
13290+{
13291+ struct dentry *dentry, *h_parent, *root;
13292+ struct super_block *h_sb;
13293+ char *pathname, *p;
13294+ struct vfsmount *h_mnt;
13295+ struct au_branch *br;
13296+ int err;
13297+ struct path path;
13298+
13299+ br = au_sbr(sb, nsi_lock->bindex);
13300+ h_mnt = au_br_mnt(br);
13301+ h_sb = h_mnt->mnt_sb;
13302+ /* todo: call lower fh_to_dentry()? fh_to_parent()? */
13303+ lockdep_off();
13304+ h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13305+ fh_len - Fh_tail, fh[Fh_h_type],
13306+ h_acceptable, /*context*/NULL);
13307+ lockdep_on();
13308+ dentry = h_parent;
13309+ if (unlikely(!h_parent || IS_ERR(h_parent))) {
13310+ AuWarn1("%s decode_fh failed, %ld\n",
13311+ au_sbtype(h_sb), PTR_ERR(h_parent));
13312+ goto out;
13313+ }
13314+ dentry = NULL;
13315+ if (unlikely(au_test_anon(h_parent))) {
13316+ AuWarn1("%s decode_fh returned a disconnected dentry\n",
13317+ au_sbtype(h_sb));
13318+ goto out_h_parent;
13319+ }
13320+
13321+ dentry = ERR_PTR(-ENOMEM);
13322+ pathname = (void *)__get_free_page(GFP_NOFS);
13323+ if (unlikely(!pathname))
13324+ goto out_h_parent;
13325+
13326+ root = sb->s_root;
13327+ path.mnt = h_mnt;
13328+ di_read_lock_parent(root, !AuLock_IR);
13329+ path.dentry = au_h_dptr(root, nsi_lock->bindex);
13330+ di_read_unlock(root, !AuLock_IR);
13331+ p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13332+ dentry = (void *)p;
13333+ if (IS_ERR(p))
13334+ goto out_pathname;
13335+
13336+ si_read_unlock(sb);
13337+ err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13338+ dentry = ERR_PTR(err);
13339+ if (unlikely(err))
13340+ goto out_relock;
13341+
13342+ dentry = ERR_PTR(-ENOENT);
13343+ AuDebugOn(au_test_anon(path.dentry));
13344+ if (unlikely(d_really_is_negative(path.dentry)))
13345+ goto out_path;
13346+
13347+ if (ino != d_inode(path.dentry)->i_ino)
13348+ dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13349+ else
13350+ dentry = dget(path.dentry);
13351+
13352+out_path:
13353+ path_put(&path);
13354+out_relock:
13355+ if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13356+ if (!IS_ERR(dentry)) {
13357+ dput(dentry);
13358+ dentry = ERR_PTR(-ESTALE);
13359+ }
13360+out_pathname:
13361+ free_page((unsigned long)pathname);
13362+out_h_parent:
13363+ dput(h_parent);
13364+out:
13365+ AuTraceErrPtr(dentry);
13366+ return dentry;
13367+}
13368+
13369+/* ---------------------------------------------------------------------- */
13370+
13371+static struct dentry *
13372+aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13373+ int fh_type)
13374+{
13375+ struct dentry *dentry;
13376+ __u32 *fh = fid->raw;
13377+ struct au_branch *br;
13378+ ino_t ino, dir_ino;
13379+ struct au_nfsd_si_lock nsi_lock = {
13380+ .force_lock = 0
13381+ };
13382+
13383+ dentry = ERR_PTR(-ESTALE);
13384+ /* it should never happen, but the file handle is unreliable */
13385+ if (unlikely(fh_len < Fh_tail))
13386+ goto out;
13387+ nsi_lock.sigen = fh[Fh_sigen];
13388+ nsi_lock.br_id = fh[Fh_br_id];
13389+
13390+ /* branch id may be wrapped around */
13391+ br = NULL;
13392+ if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
13393+ goto out;
13394+ nsi_lock.force_lock = 1;
13395+
13396+ /* is this inode still cached? */
13397+ ino = decode_ino(fh + Fh_ino);
13398+ /* it should never happen */
13399+ if (unlikely(ino == AUFS_ROOT_INO))
13400+ goto out_unlock;
13401+
13402+ dir_ino = decode_ino(fh + Fh_dir_ino);
13403+ dentry = decode_by_ino(sb, ino, dir_ino);
13404+ if (IS_ERR(dentry))
13405+ goto out_unlock;
13406+ if (dentry)
13407+ goto accept;
13408+
13409+ /* is the parent dir cached? */
13410+ br = au_sbr(sb, nsi_lock.bindex);
13411+ au_lcnt_inc(&br->br_nfiles);
13412+ dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13413+ if (IS_ERR(dentry))
13414+ goto out_unlock;
13415+ if (dentry)
13416+ goto accept;
13417+
13418+ /* lookup path */
13419+ dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
13420+ if (IS_ERR(dentry))
13421+ goto out_unlock;
13422+ if (unlikely(!dentry))
13423+ /* todo?: make it ESTALE */
13424+ goto out_unlock;
13425+
13426+accept:
13427+ if (!au_digen_test(dentry, au_sigen(sb))
13428+ && d_inode(dentry)->i_generation == fh[Fh_igen])
13429+ goto out_unlock; /* success */
13430+
13431+ dput(dentry);
13432+ dentry = ERR_PTR(-ESTALE);
13433+out_unlock:
13434+ if (br)
13435+ au_lcnt_dec(&br->br_nfiles);
13436+ si_read_unlock(sb);
13437+out:
13438+ AuTraceErrPtr(dentry);
13439+ return dentry;
13440+}
13441+
13442+#if 0 /* reserved for future use */
13443+/* support subtreecheck option */
13444+static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13445+ int fh_len, int fh_type)
13446+{
13447+ struct dentry *parent;
13448+ __u32 *fh = fid->raw;
13449+ ino_t dir_ino;
13450+
13451+ dir_ino = decode_ino(fh + Fh_dir_ino);
13452+ parent = decode_by_ino(sb, dir_ino, 0);
13453+ if (IS_ERR(parent))
13454+ goto out;
13455+ if (!parent)
13456+ parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13457+ dir_ino, fh, fh_len);
13458+
13459+out:
13460+ AuTraceErrPtr(parent);
13461+ return parent;
13462+}
13463+#endif
13464+
13465+/* ---------------------------------------------------------------------- */
13466+
13467+static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13468+ struct inode *dir)
13469+{
13470+ int err;
13471+ aufs_bindex_t bindex;
13472+ struct super_block *sb, *h_sb;
13473+ struct dentry *dentry, *parent, *h_parent;
13474+ struct inode *h_dir;
13475+ struct au_branch *br;
13476+
13477+ err = -ENOSPC;
13478+ if (unlikely(*max_len <= Fh_tail)) {
13479+ AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13480+ goto out;
13481+ }
13482+
13483+ err = FILEID_ROOT;
13484+ if (inode->i_ino == AUFS_ROOT_INO) {
13485+ AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
13486+ goto out;
13487+ }
13488+
13489+ h_parent = NULL;
13490+ sb = inode->i_sb;
13491+ err = si_read_lock(sb, AuLock_FLUSH);
13492+ if (unlikely(err))
13493+ goto out;
13494+
13495+#ifdef CONFIG_AUFS_DEBUG
13496+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13497+ AuWarn1("NFS-exporting requires xino\n");
13498+#endif
13499+ err = -EIO;
13500+ parent = NULL;
13501+ ii_read_lock_child(inode);
13502+ bindex = au_ibtop(inode);
13503+ if (!dir) {
13504+ dentry = d_find_any_alias(inode);
13505+ if (unlikely(!dentry))
13506+ goto out_unlock;
13507+ AuDebugOn(au_test_anon(dentry));
13508+ parent = dget_parent(dentry);
13509+ dput(dentry);
13510+ if (unlikely(!parent))
13511+ goto out_unlock;
13512+ if (d_really_is_positive(parent))
13513+ dir = d_inode(parent);
13514+ }
13515+
13516+ ii_read_lock_parent(dir);
13517+ h_dir = au_h_iptr(dir, bindex);
13518+ ii_read_unlock(dir);
13519+ if (unlikely(!h_dir))
13520+ goto out_parent;
13521+ h_parent = d_find_any_alias(h_dir);
13522+ if (unlikely(!h_parent))
13523+ goto out_hparent;
13524+
13525+ err = -EPERM;
13526+ br = au_sbr(sb, bindex);
13527+ h_sb = au_br_sb(br);
13528+ if (unlikely(!h_sb->s_export_op)) {
13529+ AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
13530+ goto out_hparent;
13531+ }
13532+
13533+ fh[Fh_br_id] = br->br_id;
13534+ fh[Fh_sigen] = au_sigen(sb);
13535+ encode_ino(fh + Fh_ino, inode->i_ino);
13536+ encode_ino(fh + Fh_dir_ino, dir->i_ino);
13537+ fh[Fh_igen] = inode->i_generation;
13538+
13539+ *max_len -= Fh_tail;
13540+ fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13541+ max_len,
13542+ /*connectable or subtreecheck*/0);
13543+ err = fh[Fh_h_type];
13544+ *max_len += Fh_tail;
13545+ /* todo: macros? */
13546+ if (err != FILEID_INVALID)
13547+ err = 99;
13548+ else
13549+ AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13550+
13551+out_hparent:
13552+ dput(h_parent);
13553+out_parent:
13554+ dput(parent);
13555+out_unlock:
13556+ ii_read_unlock(inode);
13557+ si_read_unlock(sb);
13558+out:
13559+ if (unlikely(err < 0))
13560+ err = FILEID_INVALID;
13561+ return err;
13562+}
13563+
13564+/* ---------------------------------------------------------------------- */
13565+
13566+static int aufs_commit_metadata(struct inode *inode)
13567+{
13568+ int err;
13569+ aufs_bindex_t bindex;
13570+ struct super_block *sb;
13571+ struct inode *h_inode;
13572+ int (*f)(struct inode *inode);
13573+
13574+ sb = inode->i_sb;
13575+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13576+ ii_write_lock_child(inode);
13577+ bindex = au_ibtop(inode);
13578+ AuDebugOn(bindex < 0);
13579+ h_inode = au_h_iptr(inode, bindex);
13580+
13581+ f = h_inode->i_sb->s_export_op->commit_metadata;
13582+ if (f)
13583+ err = f(h_inode);
13584+ else {
13585+ struct writeback_control wbc = {
13586+ .sync_mode = WB_SYNC_ALL,
13587+ .nr_to_write = 0 /* metadata only */
13588+ };
13589+
13590+ err = sync_inode(h_inode, &wbc);
13591+ }
13592+
13593+ au_cpup_attr_timesizes(inode);
13594+ ii_write_unlock(inode);
13595+ si_read_unlock(sb);
13596+ return err;
13597+}
13598+
13599+/* ---------------------------------------------------------------------- */
13600+
13601+static struct export_operations aufs_export_op = {
13602+ .fh_to_dentry = aufs_fh_to_dentry,
13603+ /* .fh_to_parent = aufs_fh_to_parent, */
13604+ .encode_fh = aufs_encode_fh,
13605+ .commit_metadata = aufs_commit_metadata
13606+};
13607+
13608+void au_export_init(struct super_block *sb)
13609+{
13610+ struct au_sbinfo *sbinfo;
13611+ __u32 u;
13612+
13613+ BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13614+ && IS_MODULE(CONFIG_EXPORTFS),
13615+ AUFS_NAME ": unsupported configuration "
13616+ "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13617+
13618+ sb->s_export_op = &aufs_export_op;
13619+ sbinfo = au_sbi(sb);
13620+ sbinfo->si_xigen = NULL;
13621+ get_random_bytes(&u, sizeof(u));
13622+ BUILD_BUG_ON(sizeof(u) != sizeof(int));
13623+ atomic_set(&sbinfo->si_xigen_next, u);
13624+}
13625diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13626--- /usr/share/empty/fs/aufs/fhsm.c 1970-01-01 01:00:00.000000000 +0100
13627+++ linux/fs/aufs/fhsm.c 2021-02-22 22:51:59.058078584 +0100
13628@@ -0,0 +1,427 @@
13629+// SPDX-License-Identifier: GPL-2.0
13630+/*
13631+ * Copyright (C) 2011-2020 Junjiro R. Okajima
13632+ *
13633+ * This program, aufs is free software; you can redistribute it and/or modify
13634+ * it under the terms of the GNU General Public License as published by
13635+ * the Free Software Foundation; either version 2 of the License, or
13636+ * (at your option) any later version.
13637+ *
13638+ * This program is distributed in the hope that it will be useful,
13639+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13640+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13641+ * GNU General Public License for more details.
13642+ *
13643+ * You should have received a copy of the GNU General Public License
13644+ * along with this program; if not, write to the Free Software
13645+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
13646+ */
13647+
13648+/*
13649+ * File-based Hierarchy Storage Management
13650+ */
13651+
13652+#include <linux/anon_inodes.h>
13653+#include <linux/poll.h>
13654+#include <linux/seq_file.h>
13655+#include <linux/statfs.h>
13656+#include "aufs.h"
13657+
13658+static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13659+{
13660+ struct au_sbinfo *sbinfo;
13661+ struct au_fhsm *fhsm;
13662+
13663+ SiMustAnyLock(sb);
13664+
13665+ sbinfo = au_sbi(sb);
13666+ fhsm = &sbinfo->si_fhsm;
13667+ AuDebugOn(!fhsm);
13668+ return fhsm->fhsm_bottom;
13669+}
13670+
13671+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13672+{
13673+ struct au_sbinfo *sbinfo;
13674+ struct au_fhsm *fhsm;
13675+
13676+ SiMustWriteLock(sb);
13677+
13678+ sbinfo = au_sbi(sb);
13679+ fhsm = &sbinfo->si_fhsm;
13680+ AuDebugOn(!fhsm);
13681+ fhsm->fhsm_bottom = bindex;
13682+}
13683+
13684+/* ---------------------------------------------------------------------- */
13685+
13686+static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13687+{
13688+ struct au_br_fhsm *bf;
13689+
13690+ bf = br->br_fhsm;
13691+ MtxMustLock(&bf->bf_lock);
13692+
13693+ return !bf->bf_readable
13694+ || time_after(jiffies,
13695+ bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13696+}
13697+
13698+/* ---------------------------------------------------------------------- */
13699+
13700+static void au_fhsm_notify(struct super_block *sb, int val)
13701+{
13702+ struct au_sbinfo *sbinfo;
13703+ struct au_fhsm *fhsm;
13704+
13705+ SiMustAnyLock(sb);
13706+
13707+ sbinfo = au_sbi(sb);
13708+ fhsm = &sbinfo->si_fhsm;
13709+ if (au_fhsm_pid(fhsm)
13710+ && atomic_read(&fhsm->fhsm_readable) != -1) {
13711+ atomic_set(&fhsm->fhsm_readable, val);
13712+ if (val)
13713+ wake_up(&fhsm->fhsm_wqh);
13714+ }
13715+}
13716+
13717+static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13718+ struct aufs_stfs *rstfs, int do_lock, int do_notify)
13719+{
13720+ int err;
13721+ struct au_branch *br;
13722+ struct au_br_fhsm *bf;
13723+
13724+ br = au_sbr(sb, bindex);
13725+ AuDebugOn(au_br_rdonly(br));
13726+ bf = br->br_fhsm;
13727+ AuDebugOn(!bf);
13728+
13729+ if (do_lock)
13730+ mutex_lock(&bf->bf_lock);
13731+ else
13732+ MtxMustLock(&bf->bf_lock);
13733+
13734+ /* sb->s_root for NFS is unreliable */
13735+ err = au_br_stfs(br, &bf->bf_stfs);
13736+ if (unlikely(err)) {
13737+ AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13738+ goto out;
13739+ }
13740+
13741+ bf->bf_jiffy = jiffies;
13742+ bf->bf_readable = 1;
13743+ if (do_notify)
13744+ au_fhsm_notify(sb, /*val*/1);
13745+ if (rstfs)
13746+ *rstfs = bf->bf_stfs;
13747+
13748+out:
13749+ if (do_lock)
13750+ mutex_unlock(&bf->bf_lock);
13751+ au_fhsm_notify(sb, /*val*/1);
13752+
13753+ return err;
13754+}
13755+
13756+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13757+{
13758+ int err;
13759+ struct au_sbinfo *sbinfo;
13760+ struct au_fhsm *fhsm;
13761+ struct au_branch *br;
13762+ struct au_br_fhsm *bf;
13763+
13764+ AuDbg("b%d, force %d\n", bindex, force);
13765+ SiMustAnyLock(sb);
13766+
13767+ sbinfo = au_sbi(sb);
13768+ fhsm = &sbinfo->si_fhsm;
13769+ if (!au_ftest_si(sbinfo, FHSM)
13770+ || fhsm->fhsm_bottom == bindex)
13771+ return;
13772+
13773+ br = au_sbr(sb, bindex);
13774+ bf = br->br_fhsm;
13775+ AuDebugOn(!bf);
13776+ mutex_lock(&bf->bf_lock);
13777+ if (force
13778+ || au_fhsm_pid(fhsm)
13779+ || au_fhsm_test_jiffy(sbinfo, br))
13780+ err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13781+ /*do_notify*/1);
13782+ mutex_unlock(&bf->bf_lock);
13783+}
13784+
13785+void au_fhsm_wrote_all(struct super_block *sb, int force)
13786+{
13787+ aufs_bindex_t bindex, bbot;
13788+ struct au_branch *br;
13789+
13790+ /* exclude the bottom */
13791+ bbot = au_fhsm_bottom(sb);
13792+ for (bindex = 0; bindex < bbot; bindex++) {
13793+ br = au_sbr(sb, bindex);
13794+ if (au_br_fhsm(br->br_perm))
13795+ au_fhsm_wrote(sb, bindex, force);
13796+ }
13797+}
13798+
13799+/* ---------------------------------------------------------------------- */
13800+
13801+static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
13802+{
13803+ __poll_t mask;
13804+ struct au_sbinfo *sbinfo;
13805+ struct au_fhsm *fhsm;
13806+
13807+ mask = 0;
13808+ sbinfo = file->private_data;
13809+ fhsm = &sbinfo->si_fhsm;
13810+ poll_wait(file, &fhsm->fhsm_wqh, wait);
13811+ if (atomic_read(&fhsm->fhsm_readable))
13812+ mask = EPOLLIN /* | EPOLLRDNORM */;
13813+
13814+ if (!mask)
13815+ AuDbg("mask 0x%x\n", mask);
13816+ return mask;
13817+}
13818+
13819+static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13820+ struct aufs_stfs *stfs, __s16 brid)
13821+{
13822+ int err;
13823+
13824+ err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13825+ if (!err)
13826+ err = __put_user(brid, &stbr->brid);
13827+ if (unlikely(err))
13828+ err = -EFAULT;
13829+
13830+ return err;
13831+}
13832+
13833+static ssize_t au_fhsm_do_read(struct super_block *sb,
13834+ struct aufs_stbr __user *stbr, size_t count)
13835+{
13836+ ssize_t err;
13837+ int nstbr;
13838+ aufs_bindex_t bindex, bbot;
13839+ struct au_branch *br;
13840+ struct au_br_fhsm *bf;
13841+
13842+ /* except the bottom branch */
13843+ err = 0;
13844+ nstbr = 0;
13845+ bbot = au_fhsm_bottom(sb);
13846+ for (bindex = 0; !err && bindex < bbot; bindex++) {
13847+ br = au_sbr(sb, bindex);
13848+ if (!au_br_fhsm(br->br_perm))
13849+ continue;
13850+
13851+ bf = br->br_fhsm;
13852+ mutex_lock(&bf->bf_lock);
13853+ if (bf->bf_readable) {
13854+ err = -EFAULT;
13855+ if (count >= sizeof(*stbr))
13856+ err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13857+ br->br_id);
13858+ if (!err) {
13859+ bf->bf_readable = 0;
13860+ count -= sizeof(*stbr);
13861+ nstbr++;
13862+ }
13863+ }
13864+ mutex_unlock(&bf->bf_lock);
13865+ }
13866+ if (!err)
13867+ err = sizeof(*stbr) * nstbr;
13868+
13869+ return err;
13870+}
13871+
13872+static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13873+ loff_t *pos)
13874+{
13875+ ssize_t err;
13876+ int readable;
13877+ aufs_bindex_t nfhsm, bindex, bbot;
13878+ struct au_sbinfo *sbinfo;
13879+ struct au_fhsm *fhsm;
13880+ struct au_branch *br;
13881+ struct super_block *sb;
13882+
13883+ err = 0;
13884+ sbinfo = file->private_data;
13885+ fhsm = &sbinfo->si_fhsm;
13886+need_data:
13887+ spin_lock_irq(&fhsm->fhsm_wqh.lock);
13888+ if (!atomic_read(&fhsm->fhsm_readable)) {
13889+ if (vfsub_file_flags(file) & O_NONBLOCK)
13890+ err = -EAGAIN;
13891+ else
13892+ err = wait_event_interruptible_locked_irq
13893+ (fhsm->fhsm_wqh,
13894+ atomic_read(&fhsm->fhsm_readable));
13895+ }
13896+ spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13897+ if (unlikely(err))
13898+ goto out;
13899+
13900+ /* sb may already be dead */
13901+ au_rw_read_lock(&sbinfo->si_rwsem);
13902+ readable = atomic_read(&fhsm->fhsm_readable);
13903+ if (readable > 0) {
13904+ sb = sbinfo->si_sb;
13905+ AuDebugOn(!sb);
13906+ /* exclude the bottom branch */
13907+ nfhsm = 0;
13908+ bbot = au_fhsm_bottom(sb);
13909+ for (bindex = 0; bindex < bbot; bindex++) {
13910+ br = au_sbr(sb, bindex);
13911+ if (au_br_fhsm(br->br_perm))
13912+ nfhsm++;
13913+ }
13914+ err = -EMSGSIZE;
13915+ if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13916+ atomic_set(&fhsm->fhsm_readable, 0);
13917+ err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13918+ count);
13919+ }
13920+ }
13921+ au_rw_read_unlock(&sbinfo->si_rwsem);
13922+ if (!readable)
13923+ goto need_data;
13924+
13925+out:
13926+ return err;
13927+}
13928+
13929+static int au_fhsm_release(struct inode *inode, struct file *file)
13930+{
13931+ struct au_sbinfo *sbinfo;
13932+ struct au_fhsm *fhsm;
13933+
13934+ /* sb may already be dead */
13935+ sbinfo = file->private_data;
13936+ fhsm = &sbinfo->si_fhsm;
13937+ spin_lock(&fhsm->fhsm_spin);
13938+ fhsm->fhsm_pid = 0;
13939+ spin_unlock(&fhsm->fhsm_spin);
13940+ kobject_put(&sbinfo->si_kobj);
13941+
13942+ return 0;
13943+}
13944+
13945+static const struct file_operations au_fhsm_fops = {
13946+ .owner = THIS_MODULE,
13947+ .llseek = noop_llseek,
13948+ .read = au_fhsm_read,
13949+ .poll = au_fhsm_poll,
13950+ .release = au_fhsm_release
13951+};
13952+
13953+int au_fhsm_fd(struct super_block *sb, int oflags)
13954+{
13955+ int err, fd;
13956+ struct au_sbinfo *sbinfo;
13957+ struct au_fhsm *fhsm;
13958+
13959+ err = -EPERM;
13960+ if (unlikely(!capable(CAP_SYS_ADMIN)))
13961+ goto out;
13962+
13963+ err = -EINVAL;
13964+ if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
13965+ goto out;
13966+
13967+ err = 0;
13968+ sbinfo = au_sbi(sb);
13969+ fhsm = &sbinfo->si_fhsm;
13970+ spin_lock(&fhsm->fhsm_spin);
13971+ if (!fhsm->fhsm_pid)
13972+ fhsm->fhsm_pid = current->pid;
13973+ else
13974+ err = -EBUSY;
13975+ spin_unlock(&fhsm->fhsm_spin);
13976+ if (unlikely(err))
13977+ goto out;
13978+
13979+ oflags |= O_RDONLY;
13980+ /* oflags |= FMODE_NONOTIFY; */
13981+ fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
13982+ err = fd;
13983+ if (unlikely(fd < 0))
13984+ goto out_pid;
13985+
13986+ /* succeed regardless 'fhsm' status */
13987+ kobject_get(&sbinfo->si_kobj);
13988+ si_noflush_read_lock(sb);
13989+ if (au_ftest_si(sbinfo, FHSM))
13990+ au_fhsm_wrote_all(sb, /*force*/0);
13991+ si_read_unlock(sb);
13992+ goto out; /* success */
13993+
13994+out_pid:
13995+ spin_lock(&fhsm->fhsm_spin);
13996+ fhsm->fhsm_pid = 0;
13997+ spin_unlock(&fhsm->fhsm_spin);
13998+out:
13999+ AuTraceErr(err);
14000+ return err;
14001+}
14002+
14003+/* ---------------------------------------------------------------------- */
14004+
14005+int au_fhsm_br_alloc(struct au_branch *br)
14006+{
14007+ int err;
14008+
14009+ err = 0;
14010+ br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14011+ if (br->br_fhsm)
14012+ au_br_fhsm_init(br->br_fhsm);
14013+ else
14014+ err = -ENOMEM;
14015+
14016+ return err;
14017+}
14018+
14019+/* ---------------------------------------------------------------------- */
14020+
14021+void au_fhsm_fin(struct super_block *sb)
14022+{
14023+ au_fhsm_notify(sb, /*val*/-1);
14024+}
14025+
14026+void au_fhsm_init(struct au_sbinfo *sbinfo)
14027+{
14028+ struct au_fhsm *fhsm;
14029+
14030+ fhsm = &sbinfo->si_fhsm;
14031+ spin_lock_init(&fhsm->fhsm_spin);
14032+ init_waitqueue_head(&fhsm->fhsm_wqh);
14033+ atomic_set(&fhsm->fhsm_readable, 0);
14034+ fhsm->fhsm_expire
14035+ = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
14036+ fhsm->fhsm_bottom = -1;
14037+}
14038+
14039+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14040+{
14041+ sbinfo->si_fhsm.fhsm_expire
14042+ = msecs_to_jiffies(sec * MSEC_PER_SEC);
14043+}
14044+
14045+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14046+{
14047+ unsigned int u;
14048+
14049+ if (!au_ftest_si(sbinfo, FHSM))
14050+ return;
14051+
14052+ u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14053+ if (u != AUFS_FHSM_CACHE_DEF_SEC)
14054+ seq_printf(seq, ",fhsm_sec=%u", u);
14055+}
14056diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14057--- /usr/share/empty/fs/aufs/file.c 1970-01-01 01:00:00.000000000 +0100
14058+++ linux/fs/aufs/file.c 2021-02-22 22:51:59.058078584 +0100
14059@@ -0,0 +1,863 @@
14060+// SPDX-License-Identifier: GPL-2.0
14061+/*
14062+ * Copyright (C) 2005-2020 Junjiro R. Okajima
14063+ *
14064+ * This program, aufs is free software; you can redistribute it and/or modify
14065+ * it under the terms of the GNU General Public License as published by
14066+ * the Free Software Foundation; either version 2 of the License, or
14067+ * (at your option) any later version.
14068+ *
14069+ * This program is distributed in the hope that it will be useful,
14070+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14071+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14072+ * GNU General Public License for more details.
14073+ *
14074+ * You should have received a copy of the GNU General Public License
14075+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
14076+ */
14077+
14078+/*
14079+ * handling file/dir, and address_space operation
14080+ */
14081+
14082+#ifdef CONFIG_AUFS_DEBUG
14083+#include <linux/migrate.h>
14084+#endif
14085+#include <linux/pagemap.h>
14086+#include "aufs.h"
14087+
14088+/* drop flags for writing */
14089+unsigned int au_file_roflags(unsigned int flags)
14090+{
14091+ flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14092+ flags |= O_RDONLY | O_NOATIME;
14093+ return flags;
14094+}
14095+
14096+/* common functions to regular file and dir */
14097+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14098+ struct file *file, int force_wr)
14099+{
14100+ struct file *h_file;
14101+ struct dentry *h_dentry;
14102+ struct inode *h_inode;
14103+ struct super_block *sb;
14104+ struct au_branch *br;
14105+ struct path h_path;
14106+ int err;
14107+
14108+ /* a race condition can happen between open and unlink/rmdir */
14109+ h_file = ERR_PTR(-ENOENT);
14110+ h_dentry = au_h_dptr(dentry, bindex);
14111+ if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
14112+ goto out;
14113+ h_inode = d_inode(h_dentry);
14114+ spin_lock(&h_dentry->d_lock);
14115+ err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
14116+ /* || !d_inode(dentry)->i_nlink */
14117+ ;
14118+ spin_unlock(&h_dentry->d_lock);
14119+ if (unlikely(err))
14120+ goto out;
14121+
14122+ sb = dentry->d_sb;
14123+ br = au_sbr(sb, bindex);
14124+ err = au_br_test_oflag(flags, br);
14125+ h_file = ERR_PTR(err);
14126+ if (unlikely(err))
14127+ goto out;
14128+
14129+ /* drop flags for writing */
14130+ if (au_test_ro(sb, bindex, d_inode(dentry))) {
14131+ if (force_wr && !(flags & O_WRONLY))
14132+ force_wr = 0;
14133+ flags = au_file_roflags(flags);
14134+ if (force_wr) {
14135+ h_file = ERR_PTR(-EROFS);
14136+ flags = au_file_roflags(flags);
14137+ if (unlikely(vfsub_native_ro(h_inode)
14138+ || IS_APPEND(h_inode)))
14139+ goto out;
14140+ flags &= ~O_ACCMODE;
14141+ flags |= O_WRONLY;
14142+ }
14143+ }
14144+ flags &= ~O_CREAT;
14145+ au_lcnt_inc(&br->br_nfiles);
14146+ h_path.dentry = h_dentry;
14147+ h_path.mnt = au_br_mnt(br);
14148+ h_file = vfsub_dentry_open(&h_path, flags);
14149+ if (IS_ERR(h_file))
14150+ goto out_br;
14151+
14152+ if (flags & __FMODE_EXEC) {
14153+ err = deny_write_access(h_file);
14154+ if (unlikely(err)) {
14155+ fput(h_file);
14156+ h_file = ERR_PTR(err);
14157+ goto out_br;
14158+ }
14159+ }
14160+ fsnotify_open(h_file);
14161+ goto out; /* success */
14162+
14163+out_br:
14164+ au_lcnt_dec(&br->br_nfiles);
14165+out:
14166+ return h_file;
14167+}
14168+
14169+static int au_cmoo(struct dentry *dentry)
14170+{
14171+ int err, cmoo, matched;
14172+ unsigned int udba;
14173+ struct path h_path;
14174+ struct au_pin pin;
14175+ struct au_cp_generic cpg = {
14176+ .dentry = dentry,
14177+ .bdst = -1,
14178+ .bsrc = -1,
14179+ .len = -1,
14180+ .pin = &pin,
14181+ .flags = AuCpup_DTIME | AuCpup_HOPEN
14182+ };
14183+ struct inode *delegated;
14184+ struct super_block *sb;
14185+ struct au_sbinfo *sbinfo;
14186+ struct au_fhsm *fhsm;
14187+ pid_t pid;
14188+ struct au_branch *br;
14189+ struct dentry *parent;
14190+ struct au_hinode *hdir;
14191+
14192+ DiMustWriteLock(dentry);
14193+ IiMustWriteLock(d_inode(dentry));
14194+
14195+ err = 0;
14196+ if (IS_ROOT(dentry))
14197+ goto out;
14198+ cpg.bsrc = au_dbtop(dentry);
14199+ if (!cpg.bsrc)
14200+ goto out;
14201+
14202+ sb = dentry->d_sb;
14203+ sbinfo = au_sbi(sb);
14204+ fhsm = &sbinfo->si_fhsm;
14205+ pid = au_fhsm_pid(fhsm);
14206+ rcu_read_lock();
14207+ matched = (pid
14208+ && (current->pid == pid
14209+ || rcu_dereference(current->real_parent)->pid == pid));
14210+ rcu_read_unlock();
14211+ if (matched)
14212+ goto out;
14213+
14214+ br = au_sbr(sb, cpg.bsrc);
14215+ cmoo = au_br_cmoo(br->br_perm);
14216+ if (!cmoo)
14217+ goto out;
14218+ if (!d_is_reg(dentry))
14219+ cmoo &= AuBrAttr_COO_ALL;
14220+ if (!cmoo)
14221+ goto out;
14222+
14223+ parent = dget_parent(dentry);
14224+ di_write_lock_parent(parent);
14225+ err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14226+ cpg.bdst = err;
14227+ if (unlikely(err < 0)) {
14228+ err = 0; /* there is no upper writable branch */
14229+ goto out_dgrade;
14230+ }
14231+ AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14232+
14233+ /* do not respect the coo attrib for the target branch */
14234+ err = au_cpup_dirs(dentry, cpg.bdst);
14235+ if (unlikely(err))
14236+ goto out_dgrade;
14237+
14238+ di_downgrade_lock(parent, AuLock_IR);
14239+ udba = au_opt_udba(sb);
14240+ err = au_pin(&pin, dentry, cpg.bdst, udba,
14241+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14242+ if (unlikely(err))
14243+ goto out_parent;
14244+
14245+ err = au_sio_cpup_simple(&cpg);
14246+ au_unpin(&pin);
14247+ if (unlikely(err))
14248+ goto out_parent;
14249+ if (!(cmoo & AuBrWAttr_MOO))
14250+ goto out_parent; /* success */
14251+
14252+ err = au_pin(&pin, dentry, cpg.bsrc, udba,
14253+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14254+ if (unlikely(err))
14255+ goto out_parent;
14256+
14257+ h_path.mnt = au_br_mnt(br);
14258+ h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
14259+ hdir = au_hi(d_inode(parent), cpg.bsrc);
14260+ delegated = NULL;
14261+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14262+ au_unpin(&pin);
14263+ /* todo: keep h_dentry or not? */
14264+ if (unlikely(err == -EWOULDBLOCK)) {
14265+ pr_warn("cannot retry for NFSv4 delegation"
14266+ " for an internal unlink\n");
14267+ iput(delegated);
14268+ }
14269+ if (unlikely(err)) {
14270+ pr_err("unlink %pd after coo failed (%d), ignored\n",
14271+ dentry, err);
14272+ err = 0;
14273+ }
14274+ goto out_parent; /* success */
14275+
14276+out_dgrade:
14277+ di_downgrade_lock(parent, AuLock_IR);
14278+out_parent:
14279+ di_read_unlock(parent, AuLock_IR);
14280+ dput(parent);
14281+out:
14282+ AuTraceErr(err);
14283+ return err;
14284+}
14285+
14286+int au_do_open(struct file *file, struct au_do_open_args *args)
14287+{
14288+ int err, aopen = args->aopen;
14289+ struct dentry *dentry;
14290+ struct au_finfo *finfo;
14291+
14292+ if (!aopen)
14293+ err = au_finfo_init(file, args->fidir);
14294+ else {
14295+ lockdep_off();
14296+ err = au_finfo_init(file, args->fidir);
14297+ lockdep_on();
14298+ }
14299+ if (unlikely(err))
14300+ goto out;
14301+
14302+ dentry = file->f_path.dentry;
14303+ AuDebugOn(IS_ERR_OR_NULL(dentry));
14304+ di_write_lock_child(dentry);
14305+ err = au_cmoo(dentry);
14306+ di_downgrade_lock(dentry, AuLock_IR);
14307+ if (!err) {
14308+ if (!aopen)
14309+ err = args->open(file, vfsub_file_flags(file), NULL);
14310+ else {
14311+ lockdep_off();
14312+ err = args->open(file, vfsub_file_flags(file),
14313+ args->h_file);
14314+ lockdep_on();
14315+ }
14316+ }
14317+ di_read_unlock(dentry, AuLock_IR);
14318+
14319+ finfo = au_fi(file);
14320+ if (!err) {
14321+ finfo->fi_file = file;
14322+ au_hbl_add(&finfo->fi_hlist,
14323+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
14324+ }
14325+ if (!aopen)
14326+ fi_write_unlock(file);
14327+ else {
14328+ lockdep_off();
14329+ fi_write_unlock(file);
14330+ lockdep_on();
14331+ }
14332+ if (unlikely(err)) {
14333+ finfo->fi_hdir = NULL;
14334+ au_finfo_fin(file);
14335+ }
14336+
14337+out:
14338+ AuTraceErr(err);
14339+ return err;
14340+}
14341+
14342+int au_reopen_nondir(struct file *file)
14343+{
14344+ int err;
14345+ aufs_bindex_t btop;
14346+ struct dentry *dentry;
14347+ struct au_branch *br;
14348+ struct file *h_file, *h_file_tmp;
14349+
14350+ dentry = file->f_path.dentry;
14351+ btop = au_dbtop(dentry);
14352+ br = au_sbr(dentry->d_sb, btop);
14353+ h_file_tmp = NULL;
14354+ if (au_fbtop(file) == btop) {
14355+ h_file = au_hf_top(file);
14356+ if (file->f_mode == h_file->f_mode)
14357+ return 0; /* success */
14358+ h_file_tmp = h_file;
14359+ get_file(h_file_tmp);
14360+ au_lcnt_inc(&br->br_nfiles);
14361+ au_set_h_fptr(file, btop, NULL);
14362+ }
14363+ AuDebugOn(au_fi(file)->fi_hdir);
14364+ /*
14365+ * it can happen
14366+ * file exists on both of rw and ro
14367+ * open --> dbtop and fbtop are both 0
14368+ * prepend a branch as rw, "rw" become ro
14369+ * remove rw/file
14370+ * delete the top branch, "rw" becomes rw again
14371+ * --> dbtop is 1, fbtop is still 0
14372+ * write --> fbtop is 0 but dbtop is 1
14373+ */
14374+ /* AuDebugOn(au_fbtop(file) < btop); */
14375+
14376+ h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
14377+ file, /*force_wr*/0);
14378+ err = PTR_ERR(h_file);
14379+ if (IS_ERR(h_file)) {
14380+ if (h_file_tmp) {
14381+ /* revert */
14382+ au_set_h_fptr(file, btop, h_file_tmp);
14383+ h_file_tmp = NULL;
14384+ }
14385+ goto out; /* todo: close all? */
14386+ }
14387+
14388+ err = 0;
14389+ au_set_fbtop(file, btop);
14390+ au_set_h_fptr(file, btop, h_file);
14391+ au_update_figen(file);
14392+ /* todo: necessary? */
14393+ /* file->f_ra = h_file->f_ra; */
14394+
14395+out:
14396+ if (h_file_tmp) {
14397+ fput(h_file_tmp);
14398+ au_lcnt_dec(&br->br_nfiles);
14399+ }
14400+ return err;
14401+}
14402+
14403+/* ---------------------------------------------------------------------- */
14404+
14405+static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14406+ struct dentry *hi_wh)
14407+{
14408+ int err;
14409+ aufs_bindex_t btop;
14410+ struct au_dinfo *dinfo;
14411+ struct dentry *h_dentry;
14412+ struct au_hdentry *hdp;
14413+
14414+ dinfo = au_di(file->f_path.dentry);
14415+ AuRwMustWriteLock(&dinfo->di_rwsem);
14416+
14417+ btop = dinfo->di_btop;
14418+ dinfo->di_btop = btgt;
14419+ hdp = au_hdentry(dinfo, btgt);
14420+ h_dentry = hdp->hd_dentry;
14421+ hdp->hd_dentry = hi_wh;
14422+ err = au_reopen_nondir(file);
14423+ hdp->hd_dentry = h_dentry;
14424+ dinfo->di_btop = btop;
14425+
14426+ return err;
14427+}
14428+
14429+static int au_ready_to_write_wh(struct file *file, loff_t len,
14430+ aufs_bindex_t bcpup, struct au_pin *pin)
14431+{
14432+ int err;
14433+ struct inode *inode, *h_inode;
14434+ struct dentry *h_dentry, *hi_wh;
14435+ struct au_cp_generic cpg = {
14436+ .dentry = file->f_path.dentry,
14437+ .bdst = bcpup,
14438+ .bsrc = -1,
14439+ .len = len,
14440+ .pin = pin
14441+ };
14442+
14443+ au_update_dbtop(cpg.dentry);
14444+ inode = d_inode(cpg.dentry);
14445+ h_inode = NULL;
14446+ if (au_dbtop(cpg.dentry) <= bcpup
14447+ && au_dbbot(cpg.dentry) >= bcpup) {
14448+ h_dentry = au_h_dptr(cpg.dentry, bcpup);
14449+ if (h_dentry && d_is_positive(h_dentry))
14450+ h_inode = d_inode(h_dentry);
14451+ }
14452+ hi_wh = au_hi_wh(inode, bcpup);
14453+ if (!hi_wh && !h_inode)
14454+ err = au_sio_cpup_wh(&cpg, file);
14455+ else
14456+ /* already copied-up after unlink */
14457+ err = au_reopen_wh(file, bcpup, hi_wh);
14458+
14459+ if (!err
14460+ && (inode->i_nlink > 1
14461+ || (inode->i_state & I_LINKABLE))
14462+ && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14463+ au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
14464+
14465+ return err;
14466+}
14467+
14468+/*
14469+ * prepare the @file for writing.
14470+ */
14471+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
14472+{
14473+ int err;
14474+ aufs_bindex_t dbtop;
14475+ struct dentry *parent;
14476+ struct inode *inode;
14477+ struct super_block *sb;
14478+ struct file *h_file;
14479+ struct au_cp_generic cpg = {
14480+ .dentry = file->f_path.dentry,
14481+ .bdst = -1,
14482+ .bsrc = -1,
14483+ .len = len,
14484+ .pin = pin,
14485+ .flags = AuCpup_DTIME
14486+ };
14487+
14488+ sb = cpg.dentry->d_sb;
14489+ inode = d_inode(cpg.dentry);
14490+ cpg.bsrc = au_fbtop(file);
14491+ err = au_test_ro(sb, cpg.bsrc, inode);
14492+ if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
14493+ err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14494+ /*flags*/0);
14495+ goto out;
14496+ }
14497+
14498+ /* need to cpup or reopen */
14499+ parent = dget_parent(cpg.dentry);
14500+ di_write_lock_parent(parent);
14501+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14502+ cpg.bdst = err;
14503+ if (unlikely(err < 0))
14504+ goto out_dgrade;
14505+ err = 0;
14506+
14507+ if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14508+ err = au_cpup_dirs(cpg.dentry, cpg.bdst);
14509+ if (unlikely(err))
14510+ goto out_dgrade;
14511+ }
14512+
14513+ err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14514+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14515+ if (unlikely(err))
14516+ goto out_dgrade;
14517+
14518+ dbtop = au_dbtop(cpg.dentry);
14519+ if (dbtop <= cpg.bdst)
14520+ cpg.bsrc = cpg.bdst;
14521+
14522+ if (dbtop <= cpg.bdst /* just reopen */
14523+ || !d_unhashed(cpg.dentry) /* copyup and reopen */
14524+ ) {
14525+ h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
14526+ if (IS_ERR(h_file))
14527+ err = PTR_ERR(h_file);
14528+ else {
14529+ di_downgrade_lock(parent, AuLock_IR);
14530+ if (dbtop > cpg.bdst)
14531+ err = au_sio_cpup_simple(&cpg);
14532+ if (!err)
14533+ err = au_reopen_nondir(file);
14534+ au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
14535+ }
14536+ } else { /* copyup as wh and reopen */
14537+ /*
14538+ * since writable hfsplus branch is not supported,
14539+ * h_open_pre/post() are unnecessary.
14540+ */
14541+ err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
14542+ di_downgrade_lock(parent, AuLock_IR);
14543+ }
14544+
14545+ if (!err) {
14546+ au_pin_set_parent_lflag(pin, /*lflag*/0);
14547+ goto out_dput; /* success */
14548+ }
14549+ au_unpin(pin);
14550+ goto out_unlock;
14551+
14552+out_dgrade:
14553+ di_downgrade_lock(parent, AuLock_IR);
14554+out_unlock:
14555+ di_read_unlock(parent, AuLock_IR);
14556+out_dput:
14557+ dput(parent);
14558+out:
14559+ return err;
14560+}
14561+
14562+/* ---------------------------------------------------------------------- */
14563+
14564+int au_do_flush(struct file *file, fl_owner_t id,
14565+ int (*flush)(struct file *file, fl_owner_t id))
14566+{
14567+ int err;
14568+ struct super_block *sb;
14569+ struct inode *inode;
14570+
14571+ inode = file_inode(file);
14572+ sb = inode->i_sb;
14573+ si_noflush_read_lock(sb);
14574+ fi_read_lock(file);
14575+ ii_read_lock_child(inode);
14576+
14577+ err = flush(file, id);
14578+ au_cpup_attr_timesizes(inode);
14579+
14580+ ii_read_unlock(inode);
14581+ fi_read_unlock(file);
14582+ si_read_unlock(sb);
14583+ return err;
14584+}
14585+
14586+/* ---------------------------------------------------------------------- */
14587+
14588+static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
14589+{
14590+ int err;
14591+ struct au_pin pin;
14592+ struct au_finfo *finfo;
14593+ struct dentry *parent, *hi_wh;
14594+ struct inode *inode;
14595+ struct super_block *sb;
14596+ struct au_cp_generic cpg = {
14597+ .dentry = file->f_path.dentry,
14598+ .bdst = -1,
14599+ .bsrc = -1,
14600+ .len = -1,
14601+ .pin = &pin,
14602+ .flags = AuCpup_DTIME
14603+ };
14604+
14605+ FiMustWriteLock(file);
14606+
14607+ err = 0;
14608+ finfo = au_fi(file);
14609+ sb = cpg.dentry->d_sb;
14610+ inode = d_inode(cpg.dentry);
14611+ cpg.bdst = au_ibtop(inode);
14612+ if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
14613+ goto out;
14614+
14615+ parent = dget_parent(cpg.dentry);
14616+ if (au_test_ro(sb, cpg.bdst, inode)) {
14617+ di_read_lock_parent(parent, !AuLock_IR);
14618+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14619+ cpg.bdst = err;
14620+ di_read_unlock(parent, !AuLock_IR);
14621+ if (unlikely(err < 0))
14622+ goto out_parent;
14623+ err = 0;
14624+ }
14625+
14626+ di_read_lock_parent(parent, AuLock_IR);
14627+ hi_wh = au_hi_wh(inode, cpg.bdst);
14628+ if (!S_ISDIR(inode->i_mode)
14629+ && au_opt_test(au_mntflags(sb), PLINK)
14630+ && au_plink_test(inode)
14631+ && !d_unhashed(cpg.dentry)
14632+ && cpg.bdst < au_dbtop(cpg.dentry)) {
14633+ err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
14634+ if (unlikely(err))
14635+ goto out_unlock;
14636+
14637+ /* always superio. */
14638+ err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14639+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14640+ if (!err) {
14641+ err = au_sio_cpup_simple(&cpg);
14642+ au_unpin(&pin);
14643+ }
14644+ } else if (hi_wh) {
14645+ /* already copied-up after unlink */
14646+ err = au_reopen_wh(file, cpg.bdst, hi_wh);
14647+ *need_reopen = 0;
14648+ }
14649+
14650+out_unlock:
14651+ di_read_unlock(parent, AuLock_IR);
14652+out_parent:
14653+ dput(parent);
14654+out:
14655+ return err;
14656+}
14657+
14658+static void au_do_refresh_dir(struct file *file)
14659+{
14660+ aufs_bindex_t bindex, bbot, new_bindex, brid;
14661+ struct au_hfile *p, tmp, *q;
14662+ struct au_finfo *finfo;
14663+ struct super_block *sb;
14664+ struct au_fidir *fidir;
14665+
14666+ FiMustWriteLock(file);
14667+
14668+ sb = file->f_path.dentry->d_sb;
14669+ finfo = au_fi(file);
14670+ fidir = finfo->fi_hdir;
14671+ AuDebugOn(!fidir);
14672+ p = fidir->fd_hfile + finfo->fi_btop;
14673+ brid = p->hf_br->br_id;
14674+ bbot = fidir->fd_bbot;
14675+ for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
14676+ if (!p->hf_file)
14677+ continue;
14678+
14679+ new_bindex = au_br_index(sb, p->hf_br->br_id);
14680+ if (new_bindex == bindex)
14681+ continue;
14682+ if (new_bindex < 0) {
14683+ au_set_h_fptr(file, bindex, NULL);
14684+ continue;
14685+ }
14686+
14687+ /* swap two lower inode, and loop again */
14688+ q = fidir->fd_hfile + new_bindex;
14689+ tmp = *q;
14690+ *q = *p;
14691+ *p = tmp;
14692+ if (tmp.hf_file) {
14693+ bindex--;
14694+ p--;
14695+ }
14696+ }
14697+
14698+ p = fidir->fd_hfile;
14699+ if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
14700+ bbot = au_sbbot(sb);
14701+ for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
14702+ finfo->fi_btop++, p++)
14703+ if (p->hf_file) {
14704+ if (file_inode(p->hf_file))
14705+ break;
14706+ au_hfput(p, /*execed*/0);
14707+ }
14708+ } else {
14709+ bbot = au_br_index(sb, brid);
14710+ for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
14711+ finfo->fi_btop++, p++)
14712+ if (p->hf_file)
14713+ au_hfput(p, /*execed*/0);
14714+ bbot = au_sbbot(sb);
14715+ }
14716+
14717+ p = fidir->fd_hfile + bbot;
14718+ for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
14719+ fidir->fd_bbot--, p--)
14720+ if (p->hf_file) {
14721+ if (file_inode(p->hf_file))
14722+ break;
14723+ au_hfput(p, /*execed*/0);
14724+ }
14725+ AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
14726+}
14727+
14728+/*
14729+ * after branch manipulating, refresh the file.
14730+ */
14731+static int refresh_file(struct file *file, int (*reopen)(struct file *file))
14732+{
14733+ int err, need_reopen, nbr;
14734+ aufs_bindex_t bbot, bindex;
14735+ struct dentry *dentry;
14736+ struct super_block *sb;
14737+ struct au_finfo *finfo;
14738+ struct au_hfile *hfile;
14739+
14740+ dentry = file->f_path.dentry;
14741+ sb = dentry->d_sb;
14742+ nbr = au_sbbot(sb) + 1;
14743+ finfo = au_fi(file);
14744+ if (!finfo->fi_hdir) {
14745+ hfile = &finfo->fi_htop;
14746+ AuDebugOn(!hfile->hf_file);
14747+ bindex = au_br_index(sb, hfile->hf_br->br_id);
14748+ AuDebugOn(bindex < 0);
14749+ if (bindex != finfo->fi_btop)
14750+ au_set_fbtop(file, bindex);
14751+ } else {
14752+ err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
14753+ if (unlikely(err))
14754+ goto out;
14755+ au_do_refresh_dir(file);
14756+ }
14757+
14758+ err = 0;
14759+ need_reopen = 1;
14760+ if (!au_test_mmapped(file))
14761+ err = au_file_refresh_by_inode(file, &need_reopen);
14762+ if (finfo->fi_hdir)
14763+ /* harmless if err */
14764+ au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
14765+ if (!err && need_reopen && !d_unlinked(dentry))
14766+ err = reopen(file);
14767+ if (!err) {
14768+ au_update_figen(file);
14769+ goto out; /* success */
14770+ }
14771+
14772+ /* error, close all lower files */
14773+ if (finfo->fi_hdir) {
14774+ bbot = au_fbbot_dir(file);
14775+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
14776+ au_set_h_fptr(file, bindex, NULL);
14777+ }
14778+
14779+out:
14780+ return err;
14781+}
14782+
14783+/* common function to regular file and dir */
14784+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14785+ int wlock, unsigned int fi_lsc)
14786+{
14787+ int err;
14788+ unsigned int sigen, figen;
14789+ aufs_bindex_t btop;
14790+ unsigned char pseudo_link;
14791+ struct dentry *dentry;
14792+ struct inode *inode;
14793+
14794+ err = 0;
14795+ dentry = file->f_path.dentry;
14796+ inode = d_inode(dentry);
14797+ sigen = au_sigen(dentry->d_sb);
14798+ fi_write_lock_nested(file, fi_lsc);
14799+ figen = au_figen(file);
14800+ if (!fi_lsc)
14801+ di_write_lock_child(dentry);
14802+ else
14803+ di_write_lock_child2(dentry);
14804+ btop = au_dbtop(dentry);
14805+ pseudo_link = (btop != au_ibtop(inode));
14806+ if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
14807+ if (!wlock) {
14808+ di_downgrade_lock(dentry, AuLock_IR);
14809+ fi_downgrade_lock(file);
14810+ }
14811+ goto out; /* success */
14812+ }
14813+
14814+ AuDbg("sigen %d, figen %d\n", sigen, figen);
14815+ if (au_digen_test(dentry, sigen)) {
14816+ err = au_reval_dpath(dentry, sigen);
14817+ AuDebugOn(!err && au_digen_test(dentry, sigen));
14818+ }
14819+
14820+ if (!err)
14821+ err = refresh_file(file, reopen);
14822+ if (!err) {
14823+ if (!wlock) {
14824+ di_downgrade_lock(dentry, AuLock_IR);
14825+ fi_downgrade_lock(file);
14826+ }
14827+ } else {
14828+ di_write_unlock(dentry);
14829+ fi_write_unlock(file);
14830+ }
14831+
14832+out:
14833+ return err;
14834+}
14835+
14836+/* ---------------------------------------------------------------------- */
14837+
14838+/* cf. aufs_nopage() */
14839+/* for madvise(2) */
14840+static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
14841+{
14842+ unlock_page(page);
14843+ return 0;
14844+}
14845+
14846+/* it will never be called, but necessary to support O_DIRECT */
14847+static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
14848+{ BUG(); return 0; }
14849+
14850+/* they will never be called. */
14851+#ifdef CONFIG_AUFS_DEBUG
14852+static int aufs_write_begin(struct file *file, struct address_space *mapping,
14853+ loff_t pos, unsigned len, unsigned flags,
14854+ struct page **pagep, void **fsdata)
14855+{ AuUnsupport(); return 0; }
14856+static int aufs_write_end(struct file *file, struct address_space *mapping,
14857+ loff_t pos, unsigned len, unsigned copied,
14858+ struct page *page, void *fsdata)
14859+{ AuUnsupport(); return 0; }
14860+static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14861+{ AuUnsupport(); return 0; }
14862+
14863+static int aufs_set_page_dirty(struct page *page)
14864+{ AuUnsupport(); return 0; }
14865+static void aufs_invalidatepage(struct page *page, unsigned int offset,
14866+ unsigned int length)
14867+{ AuUnsupport(); }
14868+static int aufs_releasepage(struct page *page, gfp_t gfp)
14869+{ AuUnsupport(); return 0; }
14870+#if 0 /* called by memory compaction regardless file */
14871+static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
14872+ struct page *page, enum migrate_mode mode)
14873+{ AuUnsupport(); return 0; }
14874+#endif
14875+static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
14876+{ AuUnsupport(); return true; }
14877+static void aufs_putback_page(struct page *page)
14878+{ AuUnsupport(); }
14879+static int aufs_launder_page(struct page *page)
14880+{ AuUnsupport(); return 0; }
14881+static int aufs_is_partially_uptodate(struct page *page,
14882+ unsigned long from,
14883+ unsigned long count)
14884+{ AuUnsupport(); return 0; }
14885+static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
14886+ bool *writeback)
14887+{ AuUnsupport(); }
14888+static int aufs_error_remove_page(struct address_space *mapping,
14889+ struct page *page)
14890+{ AuUnsupport(); return 0; }
14891+static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14892+ sector_t *span)
14893+{ AuUnsupport(); return 0; }
14894+static void aufs_swap_deactivate(struct file *file)
14895+{ AuUnsupport(); }
14896+#endif /* CONFIG_AUFS_DEBUG */
14897+
14898+const struct address_space_operations aufs_aop = {
14899+ .readpage = aufs_readpage,
14900+ .direct_IO = aufs_direct_IO,
14901+#ifdef CONFIG_AUFS_DEBUG
14902+ .writepage = aufs_writepage,
14903+ /* no writepages, because of writepage */
14904+ .set_page_dirty = aufs_set_page_dirty,
14905+ /* no readpages, because of readpage */
14906+ .write_begin = aufs_write_begin,
14907+ .write_end = aufs_write_end,
14908+ /* no bmap, no block device */
14909+ .invalidatepage = aufs_invalidatepage,
14910+ .releasepage = aufs_releasepage,
14911+ /* is fallback_migrate_page ok? */
14912+ /* .migratepage = aufs_migratepage, */
14913+ .isolate_page = aufs_isolate_page,
14914+ .putback_page = aufs_putback_page,
14915+ .launder_page = aufs_launder_page,
14916+ .is_partially_uptodate = aufs_is_partially_uptodate,
14917+ .is_dirty_writeback = aufs_is_dirty_writeback,
14918+ .error_remove_page = aufs_error_remove_page,
14919+ .swap_activate = aufs_swap_activate,
14920+ .swap_deactivate = aufs_swap_deactivate
14921+#endif /* CONFIG_AUFS_DEBUG */
14922+};
14923diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
14924--- /usr/share/empty/fs/aufs/file.h 1970-01-01 01:00:00.000000000 +0100
14925+++ linux/fs/aufs/file.h 2021-02-22 22:51:59.058078584 +0100
14926@@ -0,0 +1,342 @@
14927+/* SPDX-License-Identifier: GPL-2.0 */
14928+/*
14929+ * Copyright (C) 2005-2020 Junjiro R. Okajima
14930+ *
14931+ * This program, aufs is free software; you can redistribute it and/or modify
14932+ * it under the terms of the GNU General Public License as published by
14933+ * the Free Software Foundation; either version 2 of the License, or
14934+ * (at your option) any later version.
14935+ *
14936+ * This program is distributed in the hope that it will be useful,
14937+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14938+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14939+ * GNU General Public License for more details.
14940+ *
14941+ * You should have received a copy of the GNU General Public License
14942+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
14943+ */
14944+
14945+/*
14946+ * file operations
14947+ */
14948+
14949+#ifndef __AUFS_FILE_H__
14950+#define __AUFS_FILE_H__
14951+
14952+#ifdef __KERNEL__
14953+
14954+#include <linux/file.h>
14955+#include <linux/fs.h>
14956+#include <linux/mm_types.h>
14957+#include <linux/poll.h>
14958+#include "rwsem.h"
14959+
14960+struct au_branch;
14961+struct au_hfile {
14962+ struct file *hf_file;
14963+ struct au_branch *hf_br;
14964+};
14965+
14966+struct au_vdir;
14967+struct au_fidir {
14968+ aufs_bindex_t fd_bbot;
14969+ aufs_bindex_t fd_nent;
14970+ struct au_vdir *fd_vdir_cache;
14971+ struct au_hfile fd_hfile[];
14972+};
14973+
14974+static inline int au_fidir_sz(int nent)
14975+{
14976+ AuDebugOn(nent < 0);
14977+ return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
14978+}
14979+
14980+struct au_finfo {
14981+ atomic_t fi_generation;
14982+
14983+ struct au_rwsem fi_rwsem;
14984+ aufs_bindex_t fi_btop;
14985+
14986+ /* do not union them */
14987+ struct { /* for non-dir */
14988+ struct au_hfile fi_htop;
14989+ atomic_t fi_mmapped;
14990+ };
14991+ struct au_fidir *fi_hdir; /* for dir only */
14992+
14993+ struct hlist_bl_node fi_hlist;
14994+ struct file *fi_file; /* very ugly */
14995+ struct rcu_head rcu;
14996+} ____cacheline_aligned_in_smp;
14997+
14998+/* ---------------------------------------------------------------------- */
14999+
15000+/* file.c */
15001+extern const struct address_space_operations aufs_aop;
15002+unsigned int au_file_roflags(unsigned int flags);
15003+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
15004+ struct file *file, int force_wr);
15005+struct au_do_open_args {
15006+ int aopen;
15007+ int (*open)(struct file *file, int flags,
15008+ struct file *h_file);
15009+ struct au_fidir *fidir;
15010+ struct file *h_file;
15011+};
15012+int au_do_open(struct file *file, struct au_do_open_args *args);
15013+int au_reopen_nondir(struct file *file);
15014+struct au_pin;
15015+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15016+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
15017+ int wlock, unsigned int fi_lsc);
15018+int au_do_flush(struct file *file, fl_owner_t id,
15019+ int (*flush)(struct file *file, fl_owner_t id));
15020+
15021+/* poll.c */
15022+#ifdef CONFIG_AUFS_POLL
15023+__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
15024+#endif
15025+
15026+#ifdef CONFIG_AUFS_BR_HFSPLUS
15027+/* hfsplus.c */
15028+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15029+ int force_wr);
15030+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15031+ struct file *h_file);
15032+#else
15033+AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15034+ aufs_bindex_t bindex, int force_wr)
15035+AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15036+ struct file *h_file);
15037+#endif
15038+
15039+/* f_op.c */
15040+extern const struct file_operations aufs_file_fop;
15041+int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
15042+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
15043+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
15044+
15045+/* finfo.c */
15046+void au_hfput(struct au_hfile *hf, int execed);
15047+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15048+ struct file *h_file);
15049+
15050+void au_update_figen(struct file *file);
15051+struct au_fidir *au_fidir_alloc(struct super_block *sb);
15052+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
15053+
15054+void au_fi_init_once(void *_fi);
15055+void au_finfo_fin(struct file *file);
15056+int au_finfo_init(struct file *file, struct au_fidir *fidir);
15057+
15058+/* ioctl.c */
15059+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
15060+#ifdef CONFIG_COMPAT
15061+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15062+ unsigned long arg);
15063+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15064+ unsigned long arg);
15065+#endif
15066+
15067+/* ---------------------------------------------------------------------- */
15068+
15069+static inline struct au_finfo *au_fi(struct file *file)
15070+{
15071+ return file->private_data;
15072+}
15073+
15074+/* ---------------------------------------------------------------------- */
15075+
15076+#define fi_read_lock(f) au_rw_read_lock(&au_fi(f)->fi_rwsem)
15077+#define fi_write_lock(f) au_rw_write_lock(&au_fi(f)->fi_rwsem)
15078+#define fi_read_trylock(f) au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15079+#define fi_write_trylock(f) au_rw_write_trylock(&au_fi(f)->fi_rwsem)
15080+/*
15081+#define fi_read_trylock_nested(f) \
15082+ au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15083+#define fi_write_trylock_nested(f) \
15084+ au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15085+*/
15086+
15087+#define fi_read_unlock(f) au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15088+#define fi_write_unlock(f) au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15089+#define fi_downgrade_lock(f) au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
15090+
15091+/* lock subclass for finfo */
15092+enum {
15093+ AuLsc_FI_1,
15094+ AuLsc_FI_2
15095+};
15096+
15097+static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15098+{
15099+ au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15100+}
15101+
15102+static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15103+{
15104+ au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15105+}
15106+
15107+/*
15108+ * fi_read_lock_1, fi_write_lock_1,
15109+ * fi_read_lock_2, fi_write_lock_2
15110+ */
15111+#define AuReadLockFunc(name) \
15112+static inline void fi_read_lock_##name(struct file *f) \
15113+{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15114+
15115+#define AuWriteLockFunc(name) \
15116+static inline void fi_write_lock_##name(struct file *f) \
15117+{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15118+
15119+#define AuRWLockFuncs(name) \
15120+ AuReadLockFunc(name) \
15121+ AuWriteLockFunc(name)
15122+
15123+AuRWLockFuncs(1);
15124+AuRWLockFuncs(2);
15125+
15126+#undef AuReadLockFunc
15127+#undef AuWriteLockFunc
15128+#undef AuRWLockFuncs
15129+
15130+#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15131+#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15132+#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
15133+
15134+/* ---------------------------------------------------------------------- */
15135+
15136+/* todo: hard/soft set? */
15137+static inline aufs_bindex_t au_fbtop(struct file *file)
15138+{
15139+ FiMustAnyLock(file);
15140+ return au_fi(file)->fi_btop;
15141+}
15142+
15143+static inline aufs_bindex_t au_fbbot_dir(struct file *file)
15144+{
15145+ FiMustAnyLock(file);
15146+ AuDebugOn(!au_fi(file)->fi_hdir);
15147+ return au_fi(file)->fi_hdir->fd_bbot;
15148+}
15149+
15150+static inline struct au_vdir *au_fvdir_cache(struct file *file)
15151+{
15152+ FiMustAnyLock(file);
15153+ AuDebugOn(!au_fi(file)->fi_hdir);
15154+ return au_fi(file)->fi_hdir->fd_vdir_cache;
15155+}
15156+
15157+static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
15158+{
15159+ FiMustWriteLock(file);
15160+ au_fi(file)->fi_btop = bindex;
15161+}
15162+
15163+static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
15164+{
15165+ FiMustWriteLock(file);
15166+ AuDebugOn(!au_fi(file)->fi_hdir);
15167+ au_fi(file)->fi_hdir->fd_bbot = bindex;
15168+}
15169+
15170+static inline void au_set_fvdir_cache(struct file *file,
15171+ struct au_vdir *vdir_cache)
15172+{
15173+ FiMustWriteLock(file);
15174+ AuDebugOn(!au_fi(file)->fi_hdir);
15175+ au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15176+}
15177+
15178+static inline struct file *au_hf_top(struct file *file)
15179+{
15180+ FiMustAnyLock(file);
15181+ AuDebugOn(au_fi(file)->fi_hdir);
15182+ return au_fi(file)->fi_htop.hf_file;
15183+}
15184+
15185+static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15186+{
15187+ FiMustAnyLock(file);
15188+ AuDebugOn(!au_fi(file)->fi_hdir);
15189+ return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
15190+}
15191+
15192+/* todo: memory barrier? */
15193+static inline unsigned int au_figen(struct file *f)
15194+{
15195+ return atomic_read(&au_fi(f)->fi_generation);
15196+}
15197+
15198+static inline void au_set_mmapped(struct file *f)
15199+{
15200+ if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15201+ return;
15202+ pr_warn("fi_mmapped wrapped around\n");
15203+ while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15204+ ;
15205+}
15206+
15207+static inline void au_unset_mmapped(struct file *f)
15208+{
15209+ atomic_dec(&au_fi(f)->fi_mmapped);
15210+}
15211+
15212+static inline int au_test_mmapped(struct file *f)
15213+{
15214+ return atomic_read(&au_fi(f)->fi_mmapped);
15215+}
15216+
15217+/* customize vma->vm_file */
15218+
15219+static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15220+ struct file *file)
15221+{
15222+ struct file *f;
15223+
15224+ f = vma->vm_file;
15225+ get_file(file);
15226+ vma->vm_file = file;
15227+ fput(f);
15228+}
15229+
15230+#ifdef CONFIG_MMU
15231+#define AuDbgVmRegion(file, vma) do {} while (0)
15232+
15233+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15234+ struct file *file)
15235+{
15236+ au_do_vm_file_reset(vma, file);
15237+}
15238+#else
15239+#define AuDbgVmRegion(file, vma) \
15240+ AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15241+
15242+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15243+ struct file *file)
15244+{
15245+ struct file *f;
15246+
15247+ au_do_vm_file_reset(vma, file);
15248+ f = vma->vm_region->vm_file;
15249+ get_file(file);
15250+ vma->vm_region->vm_file = file;
15251+ fput(f);
15252+}
15253+#endif /* CONFIG_MMU */
15254+
15255+/* handle vma->vm_prfile */
15256+static inline void au_vm_prfile_set(struct vm_area_struct *vma,
15257+ struct file *file)
15258+{
15259+ get_file(file);
15260+ vma->vm_prfile = file;
15261+#ifndef CONFIG_MMU
15262+ get_file(file);
15263+ vma->vm_region->vm_prfile = file;
15264+#endif
15265+}
15266+
15267+#endif /* __KERNEL__ */
15268+#endif /* __AUFS_FILE_H__ */
15269diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15270--- /usr/share/empty/fs/aufs/finfo.c 1970-01-01 01:00:00.000000000 +0100
15271+++ linux/fs/aufs/finfo.c 2021-02-22 22:51:59.058078584 +0100
15272@@ -0,0 +1,149 @@
15273+// SPDX-License-Identifier: GPL-2.0
15274+/*
15275+ * Copyright (C) 2005-2020 Junjiro R. Okajima
15276+ *
15277+ * This program, aufs is free software; you can redistribute it and/or modify
15278+ * it under the terms of the GNU General Public License as published by
15279+ * the Free Software Foundation; either version 2 of the License, or
15280+ * (at your option) any later version.
15281+ *
15282+ * This program is distributed in the hope that it will be useful,
15283+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15284+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15285+ * GNU General Public License for more details.
15286+ *
15287+ * You should have received a copy of the GNU General Public License
15288+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15289+ */
15290+
15291+/*
15292+ * file private data
15293+ */
15294+
15295+#include "aufs.h"
15296+
15297+void au_hfput(struct au_hfile *hf, int execed)
15298+{
15299+ if (execed)
15300+ allow_write_access(hf->hf_file);
15301+ fput(hf->hf_file);
15302+ hf->hf_file = NULL;
15303+ au_lcnt_dec(&hf->hf_br->br_nfiles);
15304+ hf->hf_br = NULL;
15305+}
15306+
15307+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15308+{
15309+ struct au_finfo *finfo = au_fi(file);
15310+ struct au_hfile *hf;
15311+ struct au_fidir *fidir;
15312+
15313+ fidir = finfo->fi_hdir;
15314+ if (!fidir) {
15315+ AuDebugOn(finfo->fi_btop != bindex);
15316+ hf = &finfo->fi_htop;
15317+ } else
15318+ hf = fidir->fd_hfile + bindex;
15319+
15320+ if (hf && hf->hf_file)
15321+ au_hfput(hf, vfsub_file_execed(file));
15322+ if (val) {
15323+ FiMustWriteLock(file);
15324+ AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
15325+ hf->hf_file = val;
15326+ hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
15327+ }
15328+}
15329+
15330+void au_update_figen(struct file *file)
15331+{
15332+ atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
15333+ /* smp_mb(); */ /* atomic_set */
15334+}
15335+
15336+/* ---------------------------------------------------------------------- */
15337+
15338+struct au_fidir *au_fidir_alloc(struct super_block *sb)
15339+{
15340+ struct au_fidir *fidir;
15341+ int nbr;
15342+
15343+ nbr = au_sbbot(sb) + 1;
15344+ if (nbr < 2)
15345+ nbr = 2; /* initial allocate for 2 branches */
15346+ fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15347+ if (fidir) {
15348+ fidir->fd_bbot = -1;
15349+ fidir->fd_nent = nbr;
15350+ }
15351+
15352+ return fidir;
15353+}
15354+
15355+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
15356+{
15357+ int err;
15358+ struct au_fidir *fidir, *p;
15359+
15360+ AuRwMustWriteLock(&finfo->fi_rwsem);
15361+ fidir = finfo->fi_hdir;
15362+ AuDebugOn(!fidir);
15363+
15364+ err = -ENOMEM;
15365+ p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
15366+ GFP_NOFS, may_shrink);
15367+ if (p) {
15368+ p->fd_nent = nbr;
15369+ finfo->fi_hdir = p;
15370+ err = 0;
15371+ }
15372+
15373+ return err;
15374+}
15375+
15376+/* ---------------------------------------------------------------------- */
15377+
15378+void au_finfo_fin(struct file *file)
15379+{
15380+ struct au_finfo *finfo;
15381+
15382+ au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
15383+
15384+ finfo = au_fi(file);
15385+ AuDebugOn(finfo->fi_hdir);
15386+ AuRwDestroy(&finfo->fi_rwsem);
15387+ au_cache_free_finfo(finfo);
15388+}
15389+
15390+void au_fi_init_once(void *_finfo)
15391+{
15392+ struct au_finfo *finfo = _finfo;
15393+
15394+ au_rw_init(&finfo->fi_rwsem);
15395+}
15396+
15397+int au_finfo_init(struct file *file, struct au_fidir *fidir)
15398+{
15399+ int err;
15400+ struct au_finfo *finfo;
15401+ struct dentry *dentry;
15402+
15403+ err = -ENOMEM;
15404+ dentry = file->f_path.dentry;
15405+ finfo = au_cache_alloc_finfo();
15406+ if (unlikely(!finfo))
15407+ goto out;
15408+
15409+ err = 0;
15410+ au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
15411+ au_rw_write_lock(&finfo->fi_rwsem);
15412+ finfo->fi_btop = -1;
15413+ finfo->fi_hdir = fidir;
15414+ atomic_set(&finfo->fi_generation, au_digen(dentry));
15415+ /* smp_mb(); */ /* atomic_set */
15416+
15417+ file->private_data = finfo;
15418+
15419+out:
15420+ return err;
15421+}
15422diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15423--- /usr/share/empty/fs/aufs/f_op.c 1970-01-01 01:00:00.000000000 +0100
15424+++ linux/fs/aufs/f_op.c 2021-02-22 22:51:59.058078584 +0100
15425@@ -0,0 +1,762 @@
15426+// SPDX-License-Identifier: GPL-2.0
15427+/*
15428+ * Copyright (C) 2005-2020 Junjiro R. Okajima
15429+ *
15430+ * This program, aufs is free software; you can redistribute it and/or modify
15431+ * it under the terms of the GNU General Public License as published by
15432+ * the Free Software Foundation; either version 2 of the License, or
15433+ * (at your option) any later version.
15434+ *
15435+ * This program is distributed in the hope that it will be useful,
15436+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15437+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15438+ * GNU General Public License for more details.
15439+ *
15440+ * You should have received a copy of the GNU General Public License
15441+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15442+ */
15443+
15444+/*
15445+ * file and vm operations
15446+ */
15447+
15448+#include <linux/aio.h>
15449+#include <linux/fs_stack.h>
15450+#include <linux/mman.h>
15451+#include <linux/security.h>
15452+#include "aufs.h"
15453+
15454+int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
15455+{
15456+ int err;
15457+ aufs_bindex_t bindex;
15458+ struct dentry *dentry, *h_dentry;
15459+ struct au_finfo *finfo;
15460+ struct inode *h_inode;
15461+
15462+ FiMustWriteLock(file);
15463+
15464+ err = 0;
15465+ dentry = file->f_path.dentry;
15466+ AuDebugOn(IS_ERR_OR_NULL(dentry));
15467+ finfo = au_fi(file);
15468+ memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
15469+ atomic_set(&finfo->fi_mmapped, 0);
15470+ bindex = au_dbtop(dentry);
15471+ if (!h_file) {
15472+ h_dentry = au_h_dptr(dentry, bindex);
15473+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15474+ if (unlikely(err))
15475+ goto out;
15476+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
15477+ if (IS_ERR(h_file)) {
15478+ err = PTR_ERR(h_file);
15479+ goto out;
15480+ }
15481+ } else {
15482+ h_dentry = h_file->f_path.dentry;
15483+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15484+ if (unlikely(err))
15485+ goto out;
15486+ /* br ref is already inc-ed */
15487+ }
15488+
15489+ if ((flags & __O_TMPFILE)
15490+ && !(flags & O_EXCL)) {
15491+ h_inode = file_inode(h_file);
15492+ spin_lock(&h_inode->i_lock);
15493+ h_inode->i_state |= I_LINKABLE;
15494+ spin_unlock(&h_inode->i_lock);
15495+ }
15496+ au_set_fbtop(file, bindex);
15497+ au_set_h_fptr(file, bindex, h_file);
15498+ au_update_figen(file);
15499+ /* todo: necessary? */
15500+ /* file->f_ra = h_file->f_ra; */
15501+
15502+out:
15503+ return err;
15504+}
15505+
15506+static int aufs_open_nondir(struct inode *inode __maybe_unused,
15507+ struct file *file)
15508+{
15509+ int err;
15510+ struct super_block *sb;
15511+ struct au_do_open_args args = {
15512+ .open = au_do_open_nondir
15513+ };
15514+
15515+ AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15516+ file, vfsub_file_flags(file), file->f_mode);
15517+
15518+ sb = file->f_path.dentry->d_sb;
15519+ si_read_lock(sb, AuLock_FLUSH);
15520+ err = au_do_open(file, &args);
15521+ si_read_unlock(sb);
15522+ return err;
15523+}
15524+
15525+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15526+{
15527+ struct au_finfo *finfo;
15528+ aufs_bindex_t bindex;
15529+
15530+ finfo = au_fi(file);
15531+ au_hbl_del(&finfo->fi_hlist,
15532+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
15533+ bindex = finfo->fi_btop;
15534+ if (bindex >= 0)
15535+ au_set_h_fptr(file, bindex, NULL);
15536+
15537+ au_finfo_fin(file);
15538+ return 0;
15539+}
15540+
15541+/* ---------------------------------------------------------------------- */
15542+
15543+static int au_do_flush_nondir(struct file *file, fl_owner_t id)
15544+{
15545+ int err;
15546+ struct file *h_file;
15547+
15548+ err = 0;
15549+ h_file = au_hf_top(file);
15550+ if (h_file)
15551+ err = vfsub_flush(h_file, id);
15552+ return err;
15553+}
15554+
15555+static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15556+{
15557+ return au_do_flush(file, id, au_do_flush_nondir);
15558+}
15559+
15560+/* ---------------------------------------------------------------------- */
15561+/*
15562+ * read and write functions acquire [fdi]_rwsem once, but release before
15563+ * mmap_sem. This is because to stop a race condition between mmap(2).
15564+ * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
15565+ * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15566+ * read functions after [fdi]_rwsem are released, but it should be harmless.
15567+ */
15568+
15569+/* Callers should call au_read_post() or fput() in the end */
15570+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
15571+{
15572+ struct file *h_file;
15573+ int err;
15574+
15575+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
15576+ if (!err) {
15577+ di_read_unlock(file->f_path.dentry, AuLock_IR);
15578+ h_file = au_hf_top(file);
15579+ get_file(h_file);
15580+ if (!keep_fi)
15581+ fi_read_unlock(file);
15582+ } else
15583+ h_file = ERR_PTR(err);
15584+
15585+ return h_file;
15586+}
15587+
15588+static void au_read_post(struct inode *inode, struct file *h_file)
15589+{
15590+ /* update without lock, I don't think it a problem */
15591+ fsstack_copy_attr_atime(inode, file_inode(h_file));
15592+ fput(h_file);
15593+}
15594+
15595+struct au_write_pre {
15596+ /* input */
15597+ unsigned int lsc;
15598+
15599+ /* output */
15600+ blkcnt_t blks;
15601+ aufs_bindex_t btop;
15602+};
15603+
15604+/*
15605+ * return with iinfo is write-locked
15606+ * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15607+ * end
15608+ */
15609+static struct file *au_write_pre(struct file *file, int do_ready,
15610+ struct au_write_pre *wpre)
15611+{
15612+ struct file *h_file;
15613+ struct dentry *dentry;
15614+ int err;
15615+ unsigned int lsc;
15616+ struct au_pin pin;
15617+
15618+ lsc = 0;
15619+ if (wpre)
15620+ lsc = wpre->lsc;
15621+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
15622+ h_file = ERR_PTR(err);
15623+ if (unlikely(err))
15624+ goto out;
15625+
15626+ dentry = file->f_path.dentry;
15627+ if (do_ready) {
15628+ err = au_ready_to_write(file, -1, &pin);
15629+ if (unlikely(err)) {
15630+ h_file = ERR_PTR(err);
15631+ di_write_unlock(dentry);
15632+ goto out_fi;
15633+ }
15634+ }
15635+
15636+ di_downgrade_lock(dentry, /*flags*/0);
15637+ if (wpre)
15638+ wpre->btop = au_fbtop(file);
15639+ h_file = au_hf_top(file);
15640+ get_file(h_file);
15641+ if (wpre)
15642+ wpre->blks = file_inode(h_file)->i_blocks;
15643+ if (do_ready)
15644+ au_unpin(&pin);
15645+ di_read_unlock(dentry, /*flags*/0);
15646+
15647+out_fi:
15648+ fi_write_unlock(file);
15649+out:
15650+ return h_file;
15651+}
15652+
15653+static void au_write_post(struct inode *inode, struct file *h_file,
15654+ struct au_write_pre *wpre, ssize_t written)
15655+{
15656+ struct inode *h_inode;
15657+
15658+ au_cpup_attr_timesizes(inode);
15659+ AuDebugOn(au_ibtop(inode) != wpre->btop);
15660+ h_inode = file_inode(h_file);
15661+ inode->i_mode = h_inode->i_mode;
15662+ ii_write_unlock(inode);
15663+ /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15664+ if (written > 0)
15665+ au_fhsm_wrote(inode->i_sb, wpre->btop,
15666+ /*force*/h_inode->i_blocks > wpre->blks);
15667+ fput(h_file);
15668+}
15669+
15670+/*
15671+ * todo: very ugly
15672+ * it locks both of i_mutex and si_rwsem for read in safe.
15673+ * if the plink maintenance mode continues forever (that is the problem),
15674+ * may loop forever.
15675+ */
15676+static void au_mtx_and_read_lock(struct inode *inode)
15677+{
15678+ int err;
15679+ struct super_block *sb = inode->i_sb;
15680+
15681+ while (1) {
15682+ inode_lock(inode);
15683+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15684+ if (!err)
15685+ break;
15686+ inode_unlock(inode);
15687+ si_read_lock(sb, AuLock_NOPLMW);
15688+ si_read_unlock(sb);
15689+ }
15690+}
15691+
15692+static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15693+ struct iov_iter *iov_iter)
15694+{
15695+ ssize_t err;
15696+ struct file *file;
15697+ ssize_t (*iter)(struct kiocb *, struct iov_iter *);
15698+
15699+ err = security_file_permission(h_file, rw);
15700+ if (unlikely(err))
15701+ goto out;
15702+
15703+ err = -ENOSYS; /* the branch doesn't have its ->(read|write)_iter() */
15704+ iter = NULL;
15705+ if (rw == MAY_READ)
15706+ iter = h_file->f_op->read_iter;
15707+ else if (rw == MAY_WRITE)
15708+ iter = h_file->f_op->write_iter;
15709+
15710+ file = kio->ki_filp;
15711+ kio->ki_filp = h_file;
15712+ if (iter) {
15713+ lockdep_off();
15714+ err = iter(kio, iov_iter);
15715+ lockdep_on();
15716+ } else
15717+ /* currently there is no such fs */
15718+ WARN_ON_ONCE(1);
15719+ kio->ki_filp = file;
15720+
15721+out:
15722+ return err;
15723+}
15724+
15725+static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15726+{
15727+ ssize_t err;
15728+ struct file *file, *h_file;
15729+ struct inode *inode;
15730+ struct super_block *sb;
15731+
15732+ file = kio->ki_filp;
15733+ inode = file_inode(file);
15734+ sb = inode->i_sb;
15735+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15736+
15737+ h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
15738+ err = PTR_ERR(h_file);
15739+ if (IS_ERR(h_file))
15740+ goto out;
15741+
15742+ if (au_test_loopback_kthread()) {
15743+ au_warn_loopback(h_file->f_path.dentry->d_sb);
15744+ if (file->f_mapping != h_file->f_mapping) {
15745+ file->f_mapping = h_file->f_mapping;
15746+ smp_mb(); /* unnecessary? */
15747+ }
15748+ }
15749+ fi_read_unlock(file);
15750+
15751+ err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
15752+ /* todo: necessary? */
15753+ /* file->f_ra = h_file->f_ra; */
15754+ au_read_post(inode, h_file);
15755+
15756+out:
15757+ si_read_unlock(sb);
15758+ return err;
15759+}
15760+
15761+static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15762+{
15763+ ssize_t err;
15764+ struct au_write_pre wpre;
15765+ struct inode *inode;
15766+ struct file *file, *h_file;
15767+
15768+ file = kio->ki_filp;
15769+ inode = file_inode(file);
15770+ au_mtx_and_read_lock(inode);
15771+
15772+ wpre.lsc = 0;
15773+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15774+ err = PTR_ERR(h_file);
15775+ if (IS_ERR(h_file))
15776+ goto out;
15777+
15778+ err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
15779+ au_write_post(inode, h_file, &wpre, err);
15780+
15781+out:
15782+ si_read_unlock(inode->i_sb);
15783+ inode_unlock(inode);
15784+ return err;
15785+}
15786+
15787+static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15788+ struct pipe_inode_info *pipe, size_t len,
15789+ unsigned int flags)
15790+{
15791+ ssize_t err;
15792+ struct file *h_file;
15793+ struct inode *inode;
15794+ struct super_block *sb;
15795+
15796+ inode = file_inode(file);
15797+ sb = inode->i_sb;
15798+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15799+
15800+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15801+ err = PTR_ERR(h_file);
15802+ if (IS_ERR(h_file))
15803+ goto out;
15804+
15805+ err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
15806+ /* todo: necessary? */
15807+ /* file->f_ra = h_file->f_ra; */
15808+ au_read_post(inode, h_file);
15809+
15810+out:
15811+ si_read_unlock(sb);
15812+ return err;
15813+}
15814+
15815+static ssize_t
15816+aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15817+ size_t len, unsigned int flags)
15818+{
15819+ ssize_t err;
15820+ struct au_write_pre wpre;
15821+ struct inode *inode;
15822+ struct file *h_file;
15823+
15824+ inode = file_inode(file);
15825+ au_mtx_and_read_lock(inode);
15826+
15827+ wpre.lsc = 0;
15828+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15829+ err = PTR_ERR(h_file);
15830+ if (IS_ERR(h_file))
15831+ goto out;
15832+
15833+ err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
15834+ au_write_post(inode, h_file, &wpre, err);
15835+
15836+out:
15837+ si_read_unlock(inode->i_sb);
15838+ inode_unlock(inode);
15839+ return err;
15840+}
15841+
15842+static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15843+ loff_t len)
15844+{
15845+ long err;
15846+ struct au_write_pre wpre;
15847+ struct inode *inode;
15848+ struct file *h_file;
15849+
15850+ inode = file_inode(file);
15851+ au_mtx_and_read_lock(inode);
15852+
15853+ wpre.lsc = 0;
15854+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15855+ err = PTR_ERR(h_file);
15856+ if (IS_ERR(h_file))
15857+ goto out;
15858+
15859+ lockdep_off();
15860+ err = vfs_fallocate(h_file, mode, offset, len);
15861+ lockdep_on();
15862+ au_write_post(inode, h_file, &wpre, /*written*/1);
15863+
15864+out:
15865+ si_read_unlock(inode->i_sb);
15866+ inode_unlock(inode);
15867+ return err;
15868+}
15869+
15870+static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
15871+ struct file *dst, loff_t dst_pos,
15872+ size_t len, unsigned int flags)
15873+{
15874+ ssize_t err;
15875+ struct au_write_pre wpre;
15876+ enum { SRC, DST };
15877+ struct {
15878+ struct inode *inode;
15879+ struct file *h_file;
15880+ struct super_block *h_sb;
15881+ } a[2];
15882+#define a_src a[SRC]
15883+#define a_dst a[DST]
15884+
15885+ err = -EINVAL;
15886+ a_src.inode = file_inode(src);
15887+ if (unlikely(!S_ISREG(a_src.inode->i_mode)))
15888+ goto out;
15889+ a_dst.inode = file_inode(dst);
15890+ if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
15891+ goto out;
15892+
15893+ au_mtx_and_read_lock(a_dst.inode);
15894+ /*
15895+ * in order to match the order in di_write_lock2_{child,parent}(),
15896+ * use f_path.dentry for this comparison.
15897+ */
15898+ if (src->f_path.dentry < dst->f_path.dentry) {
15899+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
15900+ err = PTR_ERR(a_src.h_file);
15901+ if (IS_ERR(a_src.h_file))
15902+ goto out_si;
15903+
15904+ wpre.lsc = AuLsc_FI_2;
15905+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15906+ err = PTR_ERR(a_dst.h_file);
15907+ if (IS_ERR(a_dst.h_file)) {
15908+ au_read_post(a_src.inode, a_src.h_file);
15909+ goto out_si;
15910+ }
15911+ } else {
15912+ wpre.lsc = AuLsc_FI_1;
15913+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15914+ err = PTR_ERR(a_dst.h_file);
15915+ if (IS_ERR(a_dst.h_file))
15916+ goto out_si;
15917+
15918+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
15919+ err = PTR_ERR(a_src.h_file);
15920+ if (IS_ERR(a_src.h_file)) {
15921+ au_write_post(a_dst.inode, a_dst.h_file, &wpre,
15922+ /*written*/0);
15923+ goto out_si;
15924+ }
15925+ }
15926+
15927+ err = -EXDEV;
15928+ a_src.h_sb = file_inode(a_src.h_file)->i_sb;
15929+ a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
15930+ if (unlikely(a_src.h_sb != a_dst.h_sb)) {
15931+ AuDbgFile(src);
15932+ AuDbgFile(dst);
15933+ goto out_file;
15934+ }
15935+
15936+ err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
15937+ dst_pos, len, flags);
15938+
15939+out_file:
15940+ au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
15941+ fi_read_unlock(src);
15942+ au_read_post(a_src.inode, a_src.h_file);
15943+out_si:
15944+ si_read_unlock(a_dst.inode->i_sb);
15945+ inode_unlock(a_dst.inode);
15946+out:
15947+ return err;
15948+#undef a_src
15949+#undef a_dst
15950+}
15951+
15952+/* ---------------------------------------------------------------------- */
15953+
15954+/*
15955+ * The locking order around current->mmap_sem.
15956+ * - in most and regular cases
15957+ * file I/O syscall -- aufs_read() or something
15958+ * -- si_rwsem for read -- mmap_sem
15959+ * (Note that [fdi]i_rwsem are released before mmap_sem).
15960+ * - in mmap case
15961+ * mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
15962+ * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
15963+ * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
15964+ * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
15965+ * It means that when aufs acquires si_rwsem for write, the process should never
15966+ * acquire mmap_sem.
15967+ *
15968+ * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
15969+ * problem either since any directory is not able to be mmap-ed.
15970+ * The similar scenario is applied to aufs_readlink() too.
15971+ */
15972+
15973+#if 0 /* stop calling security_file_mmap() */
15974+/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
15975+#define AuConv_VM_PROT(f, b) _calc_vm_trans(f, VM_##b, PROT_##b)
15976+
15977+static unsigned long au_arch_prot_conv(unsigned long flags)
15978+{
15979+ /* currently ppc64 only */
15980+#ifdef CONFIG_PPC64
15981+ /* cf. linux/arch/powerpc/include/asm/mman.h */
15982+ AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
15983+ return AuConv_VM_PROT(flags, SAO);
15984+#else
15985+ AuDebugOn(arch_calc_vm_prot_bits(-1));
15986+ return 0;
15987+#endif
15988+}
15989+
15990+static unsigned long au_prot_conv(unsigned long flags)
15991+{
15992+ return AuConv_VM_PROT(flags, READ)
15993+ | AuConv_VM_PROT(flags, WRITE)
15994+ | AuConv_VM_PROT(flags, EXEC)
15995+ | au_arch_prot_conv(flags);
15996+}
15997+
15998+/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
15999+#define AuConv_VM_MAP(f, b) _calc_vm_trans(f, VM_##b, MAP_##b)
16000+
16001+static unsigned long au_flag_conv(unsigned long flags)
16002+{
16003+ return AuConv_VM_MAP(flags, GROWSDOWN)
16004+ | AuConv_VM_MAP(flags, DENYWRITE)
16005+ | AuConv_VM_MAP(flags, LOCKED);
16006+}
16007+#endif
16008+
16009+static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
16010+{
16011+ int err;
16012+ const unsigned char wlock
16013+ = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
16014+ struct super_block *sb;
16015+ struct file *h_file;
16016+ struct inode *inode;
16017+
16018+ AuDbgVmRegion(file, vma);
16019+
16020+ inode = file_inode(file);
16021+ sb = inode->i_sb;
16022+ lockdep_off();
16023+ si_read_lock(sb, AuLock_NOPLMW);
16024+
16025+ h_file = au_write_pre(file, wlock, /*wpre*/NULL);
16026+ lockdep_on();
16027+ err = PTR_ERR(h_file);
16028+ if (IS_ERR(h_file))
16029+ goto out;
16030+
16031+ err = 0;
16032+ au_set_mmapped(file);
16033+ au_vm_file_reset(vma, h_file);
16034+ /*
16035+ * we cannot call security_mmap_file() here since it may acquire
16036+ * mmap_sem or i_mutex.
16037+ *
16038+ * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16039+ * au_flag_conv(vma->vm_flags));
16040+ */
16041+ if (!err)
16042+ err = call_mmap(h_file, vma);
16043+ if (!err) {
16044+ au_vm_prfile_set(vma, file);
16045+ fsstack_copy_attr_atime(inode, file_inode(h_file));
16046+ goto out_fput; /* success */
16047+ }
16048+ au_unset_mmapped(file);
16049+ au_vm_file_reset(vma, file);
16050+
16051+out_fput:
16052+ lockdep_off();
16053+ ii_write_unlock(inode);
16054+ lockdep_on();
16055+ fput(h_file);
16056+out:
16057+ lockdep_off();
16058+ si_read_unlock(sb);
16059+ lockdep_on();
16060+ AuTraceErr(err);
16061+ return err;
16062+}
16063+
16064+/* ---------------------------------------------------------------------- */
16065+
16066+static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16067+ int datasync)
16068+{
16069+ int err;
16070+ struct au_write_pre wpre;
16071+ struct inode *inode;
16072+ struct file *h_file;
16073+
16074+ err = 0; /* -EBADF; */ /* posix? */
16075+ if (unlikely(!(file->f_mode & FMODE_WRITE)))
16076+ goto out;
16077+
16078+ inode = file_inode(file);
16079+ au_mtx_and_read_lock(inode);
16080+
16081+ wpre.lsc = 0;
16082+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16083+ err = PTR_ERR(h_file);
16084+ if (IS_ERR(h_file))
16085+ goto out_unlock;
16086+
16087+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
16088+ au_write_post(inode, h_file, &wpre, /*written*/0);
16089+
16090+out_unlock:
16091+ si_read_unlock(inode->i_sb);
16092+ inode_unlock(inode);
16093+out:
16094+ return err;
16095+}
16096+
16097+static int aufs_fasync(int fd, struct file *file, int flag)
16098+{
16099+ int err;
16100+ struct file *h_file;
16101+ struct super_block *sb;
16102+
16103+ sb = file->f_path.dentry->d_sb;
16104+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16105+
16106+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16107+ err = PTR_ERR(h_file);
16108+ if (IS_ERR(h_file))
16109+ goto out;
16110+
16111+ if (h_file->f_op->fasync)
16112+ err = h_file->f_op->fasync(fd, h_file, flag);
16113+ fput(h_file); /* instead of au_read_post() */
16114+
16115+out:
16116+ si_read_unlock(sb);
16117+ return err;
16118+}
16119+
16120+static int aufs_setfl(struct file *file, unsigned long arg)
16121+{
16122+ int err;
16123+ struct file *h_file;
16124+ struct super_block *sb;
16125+
16126+ sb = file->f_path.dentry->d_sb;
16127+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16128+
16129+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16130+ err = PTR_ERR(h_file);
16131+ if (IS_ERR(h_file))
16132+ goto out;
16133+
16134+ /* stop calling h_file->fasync */
16135+ arg |= vfsub_file_flags(file) & FASYNC;
16136+ err = setfl(/*unused fd*/-1, h_file, arg);
16137+ fput(h_file); /* instead of au_read_post() */
16138+
16139+out:
16140+ si_read_unlock(sb);
16141+ return err;
16142+}
16143+
16144+/* ---------------------------------------------------------------------- */
16145+
16146+/* no one supports this operation, currently */
16147+#if 0 /* reserved for future use */
16148+static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
16149+ size_t len, loff_t *pos, int more)
16150+{
16151+}
16152+#endif
16153+
16154+/* ---------------------------------------------------------------------- */
16155+
16156+const struct file_operations aufs_file_fop = {
16157+ .owner = THIS_MODULE,
16158+
16159+ .llseek = default_llseek,
16160+
16161+ .read_iter = aufs_read_iter,
16162+ .write_iter = aufs_write_iter,
16163+
16164+#ifdef CONFIG_AUFS_POLL
16165+ .poll = aufs_poll,
16166+#endif
16167+ .unlocked_ioctl = aufs_ioctl_nondir,
16168+#ifdef CONFIG_COMPAT
16169+ .compat_ioctl = aufs_compat_ioctl_nondir,
16170+#endif
16171+ .mmap = aufs_mmap,
16172+ .open = aufs_open_nondir,
16173+ .flush = aufs_flush_nondir,
16174+ .release = aufs_release_nondir,
16175+ .fsync = aufs_fsync_nondir,
16176+ .fasync = aufs_fasync,
16177+ /* .sendpage = aufs_sendpage, */
16178+ .setfl = aufs_setfl,
16179+ .splice_write = aufs_splice_write,
16180+ .splice_read = aufs_splice_read,
16181+#if 0 /* reserved for future use */
16182+ .aio_splice_write = aufs_aio_splice_write,
16183+ .aio_splice_read = aufs_aio_splice_read,
16184+#endif
16185+ .fallocate = aufs_fallocate,
16186+ .copy_file_range = aufs_copy_file_range
16187+};
16188diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
16189--- /usr/share/empty/fs/aufs/fstype.h 1970-01-01 01:00:00.000000000 +0100
16190+++ linux/fs/aufs/fstype.h 2021-02-22 22:51:59.058078584 +0100
16191@@ -0,0 +1,401 @@
16192+/* SPDX-License-Identifier: GPL-2.0 */
16193+/*
16194+ * Copyright (C) 2005-2020 Junjiro R. Okajima
16195+ *
16196+ * This program, aufs is free software; you can redistribute it and/or modify
16197+ * it under the terms of the GNU General Public License as published by
16198+ * the Free Software Foundation; either version 2 of the License, or
16199+ * (at your option) any later version.
16200+ *
16201+ * This program is distributed in the hope that it will be useful,
16202+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16203+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16204+ * GNU General Public License for more details.
16205+ *
16206+ * You should have received a copy of the GNU General Public License
16207+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16208+ */
16209+
16210+/*
16211+ * judging filesystem type
16212+ */
16213+
16214+#ifndef __AUFS_FSTYPE_H__
16215+#define __AUFS_FSTYPE_H__
16216+
16217+#ifdef __KERNEL__
16218+
16219+#include <linux/fs.h>
16220+#include <linux/magic.h>
16221+#include <linux/nfs_fs.h>
16222+#include <linux/romfs_fs.h>
16223+
16224+static inline int au_test_aufs(struct super_block *sb)
16225+{
16226+ return sb->s_magic == AUFS_SUPER_MAGIC;
16227+}
16228+
16229+static inline const char *au_sbtype(struct super_block *sb)
16230+{
16231+ return sb->s_type->name;
16232+}
16233+
16234+static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
16235+{
16236+#if IS_ENABLED(CONFIG_ISO9660_FS)
16237+ return sb->s_magic == ISOFS_SUPER_MAGIC;
16238+#else
16239+ return 0;
16240+#endif
16241+}
16242+
16243+static inline int au_test_romfs(struct super_block *sb __maybe_unused)
16244+{
16245+#if IS_ENABLED(CONFIG_ROMFS_FS)
16246+ return sb->s_magic == ROMFS_MAGIC;
16247+#else
16248+ return 0;
16249+#endif
16250+}
16251+
16252+static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
16253+{
16254+#if IS_ENABLED(CONFIG_CRAMFS)
16255+ return sb->s_magic == CRAMFS_MAGIC;
16256+#endif
16257+ return 0;
16258+}
16259+
16260+static inline int au_test_nfs(struct super_block *sb __maybe_unused)
16261+{
16262+#if IS_ENABLED(CONFIG_NFS_FS)
16263+ return sb->s_magic == NFS_SUPER_MAGIC;
16264+#else
16265+ return 0;
16266+#endif
16267+}
16268+
16269+static inline int au_test_fuse(struct super_block *sb __maybe_unused)
16270+{
16271+#if IS_ENABLED(CONFIG_FUSE_FS)
16272+ return sb->s_magic == FUSE_SUPER_MAGIC;
16273+#else
16274+ return 0;
16275+#endif
16276+}
16277+
16278+static inline int au_test_xfs(struct super_block *sb __maybe_unused)
16279+{
16280+#if IS_ENABLED(CONFIG_XFS_FS)
16281+ return sb->s_magic == XFS_SB_MAGIC;
16282+#else
16283+ return 0;
16284+#endif
16285+}
16286+
16287+static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
16288+{
16289+#ifdef CONFIG_TMPFS
16290+ return sb->s_magic == TMPFS_MAGIC;
16291+#else
16292+ return 0;
16293+#endif
16294+}
16295+
16296+static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
16297+{
16298+#if IS_ENABLED(CONFIG_ECRYPT_FS)
16299+ return !strcmp(au_sbtype(sb), "ecryptfs");
16300+#else
16301+ return 0;
16302+#endif
16303+}
16304+
16305+static inline int au_test_ramfs(struct super_block *sb)
16306+{
16307+ return sb->s_magic == RAMFS_MAGIC;
16308+}
16309+
16310+static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
16311+{
16312+#if IS_ENABLED(CONFIG_UBIFS_FS)
16313+ return sb->s_magic == UBIFS_SUPER_MAGIC;
16314+#else
16315+ return 0;
16316+#endif
16317+}
16318+
16319+static inline int au_test_procfs(struct super_block *sb __maybe_unused)
16320+{
16321+#ifdef CONFIG_PROC_FS
16322+ return sb->s_magic == PROC_SUPER_MAGIC;
16323+#else
16324+ return 0;
16325+#endif
16326+}
16327+
16328+static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
16329+{
16330+#ifdef CONFIG_SYSFS
16331+ return sb->s_magic == SYSFS_MAGIC;
16332+#else
16333+ return 0;
16334+#endif
16335+}
16336+
16337+static inline int au_test_configfs(struct super_block *sb __maybe_unused)
16338+{
16339+#if IS_ENABLED(CONFIG_CONFIGFS_FS)
16340+ return sb->s_magic == CONFIGFS_MAGIC;
16341+#else
16342+ return 0;
16343+#endif
16344+}
16345+
16346+static inline int au_test_minix(struct super_block *sb __maybe_unused)
16347+{
16348+#if IS_ENABLED(CONFIG_MINIX_FS)
16349+ return sb->s_magic == MINIX3_SUPER_MAGIC
16350+ || sb->s_magic == MINIX2_SUPER_MAGIC
16351+ || sb->s_magic == MINIX2_SUPER_MAGIC2
16352+ || sb->s_magic == MINIX_SUPER_MAGIC
16353+ || sb->s_magic == MINIX_SUPER_MAGIC2;
16354+#else
16355+ return 0;
16356+#endif
16357+}
16358+
16359+static inline int au_test_fat(struct super_block *sb __maybe_unused)
16360+{
16361+#if IS_ENABLED(CONFIG_FAT_FS)
16362+ return sb->s_magic == MSDOS_SUPER_MAGIC;
16363+#else
16364+ return 0;
16365+#endif
16366+}
16367+
16368+static inline int au_test_msdos(struct super_block *sb)
16369+{
16370+ return au_test_fat(sb);
16371+}
16372+
16373+static inline int au_test_vfat(struct super_block *sb)
16374+{
16375+ return au_test_fat(sb);
16376+}
16377+
16378+static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
16379+{
16380+#ifdef CONFIG_SECURITYFS
16381+ return sb->s_magic == SECURITYFS_MAGIC;
16382+#else
16383+ return 0;
16384+#endif
16385+}
16386+
16387+static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
16388+{
16389+#if IS_ENABLED(CONFIG_SQUASHFS)
16390+ return sb->s_magic == SQUASHFS_MAGIC;
16391+#else
16392+ return 0;
16393+#endif
16394+}
16395+
16396+static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
16397+{
16398+#if IS_ENABLED(CONFIG_BTRFS_FS)
16399+ return sb->s_magic == BTRFS_SUPER_MAGIC;
16400+#else
16401+ return 0;
16402+#endif
16403+}
16404+
16405+static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
16406+{
16407+#if IS_ENABLED(CONFIG_XENFS)
16408+ return sb->s_magic == XENFS_SUPER_MAGIC;
16409+#else
16410+ return 0;
16411+#endif
16412+}
16413+
16414+static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
16415+{
16416+#ifdef CONFIG_DEBUG_FS
16417+ return sb->s_magic == DEBUGFS_MAGIC;
16418+#else
16419+ return 0;
16420+#endif
16421+}
16422+
16423+static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
16424+{
16425+#if IS_ENABLED(CONFIG_NILFS)
16426+ return sb->s_magic == NILFS_SUPER_MAGIC;
16427+#else
16428+ return 0;
16429+#endif
16430+}
16431+
16432+static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
16433+{
16434+#if IS_ENABLED(CONFIG_HFSPLUS_FS)
16435+ return sb->s_magic == HFSPLUS_SUPER_MAGIC;
16436+#else
16437+ return 0;
16438+#endif
16439+}
16440+
16441+/* ---------------------------------------------------------------------- */
16442+/*
16443+ * they can't be an aufs branch.
16444+ */
16445+static inline int au_test_fs_unsuppoted(struct super_block *sb)
16446+{
16447+ return
16448+#ifndef CONFIG_AUFS_BR_RAMFS
16449+ au_test_ramfs(sb) ||
16450+#endif
16451+ au_test_procfs(sb)
16452+ || au_test_sysfs(sb)
16453+ || au_test_configfs(sb)
16454+ || au_test_debugfs(sb)
16455+ || au_test_securityfs(sb)
16456+ || au_test_xenfs(sb)
16457+ || au_test_ecryptfs(sb)
16458+ /* || !strcmp(au_sbtype(sb), "unionfs") */
16459+ || au_test_aufs(sb); /* will be supported in next version */
16460+}
16461+
16462+static inline int au_test_fs_remote(struct super_block *sb)
16463+{
16464+ return !au_test_tmpfs(sb)
16465+#ifdef CONFIG_AUFS_BR_RAMFS
16466+ && !au_test_ramfs(sb)
16467+#endif
16468+ && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
16469+}
16470+
16471+/* ---------------------------------------------------------------------- */
16472+
16473+/*
16474+ * Note: these functions (below) are created after reading ->getattr() in all
16475+ * filesystems under linux/fs. it means we have to do so in every update...
16476+ */
16477+
16478+/*
16479+ * some filesystems require getattr to refresh the inode attributes before
16480+ * referencing.
16481+ * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
16482+ * and leave the work for d_revalidate()
16483+ */
16484+static inline int au_test_fs_refresh_iattr(struct super_block *sb)
16485+{
16486+ return au_test_nfs(sb)
16487+ || au_test_fuse(sb)
16488+ /* || au_test_btrfs(sb) */ /* untested */
16489+ ;
16490+}
16491+
16492+/*
16493+ * filesystems which don't maintain i_size or i_blocks.
16494+ */
16495+static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
16496+{
16497+ return au_test_xfs(sb)
16498+ || au_test_btrfs(sb)
16499+ || au_test_ubifs(sb)
16500+ || au_test_hfsplus(sb) /* maintained, but incorrect */
16501+ /* || au_test_minix(sb) */ /* untested */
16502+ ;
16503+}
16504+
16505+/*
16506+ * filesystems which don't store the correct value in some of their inode
16507+ * attributes.
16508+ */
16509+static inline int au_test_fs_bad_iattr(struct super_block *sb)
16510+{
16511+ return au_test_fs_bad_iattr_size(sb)
16512+ || au_test_fat(sb)
16513+ || au_test_msdos(sb)
16514+ || au_test_vfat(sb);
16515+}
16516+
16517+/* they don't check i_nlink in link(2) */
16518+static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
16519+{
16520+ return au_test_tmpfs(sb)
16521+#ifdef CONFIG_AUFS_BR_RAMFS
16522+ || au_test_ramfs(sb)
16523+#endif
16524+ || au_test_ubifs(sb)
16525+ || au_test_hfsplus(sb);
16526+}
16527+
16528+/*
16529+ * filesystems which sets S_NOATIME and S_NOCMTIME.
16530+ */
16531+static inline int au_test_fs_notime(struct super_block *sb)
16532+{
16533+ return au_test_nfs(sb)
16534+ || au_test_fuse(sb)
16535+ || au_test_ubifs(sb)
16536+ ;
16537+}
16538+
16539+/* temporary support for i#1 in cramfs */
16540+static inline int au_test_fs_unique_ino(struct inode *inode)
16541+{
16542+ if (au_test_cramfs(inode->i_sb))
16543+ return inode->i_ino != 1;
16544+ return 1;
16545+}
16546+
16547+/* ---------------------------------------------------------------------- */
16548+
16549+/*
16550+ * the filesystem where the xino files placed must support i/o after unlink and
16551+ * maintain i_size and i_blocks.
16552+ */
16553+static inline int au_test_fs_bad_xino(struct super_block *sb)
16554+{
16555+ return au_test_fs_remote(sb)
16556+ || au_test_fs_bad_iattr_size(sb)
16557+ /* don't want unnecessary work for xino */
16558+ || au_test_aufs(sb)
16559+ || au_test_ecryptfs(sb)
16560+ || au_test_nilfs(sb);
16561+}
16562+
16563+static inline int au_test_fs_trunc_xino(struct super_block *sb)
16564+{
16565+ return au_test_tmpfs(sb)
16566+ || au_test_ramfs(sb);
16567+}
16568+
16569+/*
16570+ * test if the @sb is real-readonly.
16571+ */
16572+static inline int au_test_fs_rr(struct super_block *sb)
16573+{
16574+ return au_test_squashfs(sb)
16575+ || au_test_iso9660(sb)
16576+ || au_test_cramfs(sb)
16577+ || au_test_romfs(sb);
16578+}
16579+
16580+/*
16581+ * test if the @inode is nfs with 'noacl' option
16582+ * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
16583+ */
16584+static inline int au_test_nfs_noacl(struct inode *inode)
16585+{
16586+ return au_test_nfs(inode->i_sb)
16587+ /* && IS_POSIXACL(inode) */
16588+ && !nfs_server_capable(inode, NFS_CAP_ACLS);
16589+}
16590+
16591+#endif /* __KERNEL__ */
16592+#endif /* __AUFS_FSTYPE_H__ */
16593diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
16594--- /usr/share/empty/fs/aufs/hbl.h 1970-01-01 01:00:00.000000000 +0100
16595+++ linux/fs/aufs/hbl.h 2021-02-22 22:51:59.058078584 +0100
16596@@ -0,0 +1,65 @@
16597+/* SPDX-License-Identifier: GPL-2.0 */
16598+/*
16599+ * Copyright (C) 2017-2020 Junjiro R. Okajima
16600+ *
16601+ * This program, aufs is free software; you can redistribute it and/or modify
16602+ * it under the terms of the GNU General Public License as published by
16603+ * the Free Software Foundation; either version 2 of the License, or
16604+ * (at your option) any later version.
16605+ *
16606+ * This program is distributed in the hope that it will be useful,
16607+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16608+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16609+ * GNU General Public License for more details.
16610+ *
16611+ * You should have received a copy of the GNU General Public License
16612+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16613+ */
16614+
16615+/*
16616+ * helpers for hlist_bl.h
16617+ */
16618+
16619+#ifndef __AUFS_HBL_H__
16620+#define __AUFS_HBL_H__
16621+
16622+#ifdef __KERNEL__
16623+
16624+#include <linux/list_bl.h>
16625+
16626+static inline void au_hbl_add(struct hlist_bl_node *node,
16627+ struct hlist_bl_head *hbl)
16628+{
16629+ hlist_bl_lock(hbl);
16630+ hlist_bl_add_head(node, hbl);
16631+ hlist_bl_unlock(hbl);
16632+}
16633+
16634+static inline void au_hbl_del(struct hlist_bl_node *node,
16635+ struct hlist_bl_head *hbl)
16636+{
16637+ hlist_bl_lock(hbl);
16638+ hlist_bl_del(node);
16639+ hlist_bl_unlock(hbl);
16640+}
16641+
16642+#define au_hbl_for_each(pos, head) \
16643+ for (pos = hlist_bl_first(head); \
16644+ pos; \
16645+ pos = pos->next)
16646+
16647+static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
16648+{
16649+ unsigned long cnt;
16650+ struct hlist_bl_node *pos;
16651+
16652+ cnt = 0;
16653+ hlist_bl_lock(hbl);
16654+ au_hbl_for_each(pos, hbl)
16655+ cnt++;
16656+ hlist_bl_unlock(hbl);
16657+ return cnt;
16658+}
16659+
16660+#endif /* __KERNEL__ */
16661+#endif /* __AUFS_HBL_H__ */
16662diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
16663--- /usr/share/empty/fs/aufs/hfsnotify.c 1970-01-01 01:00:00.000000000 +0100
16664+++ linux/fs/aufs/hfsnotify.c 2021-02-22 22:51:59.058078584 +0100
16665@@ -0,0 +1,288 @@
16666+// SPDX-License-Identifier: GPL-2.0
16667+/*
16668+ * Copyright (C) 2005-2020 Junjiro R. Okajima
16669+ *
16670+ * This program, aufs is free software; you can redistribute it and/or modify
16671+ * it under the terms of the GNU General Public License as published by
16672+ * the Free Software Foundation; either version 2 of the License, or
16673+ * (at your option) any later version.
16674+ *
16675+ * This program is distributed in the hope that it will be useful,
16676+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16677+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16678+ * GNU General Public License for more details.
16679+ *
16680+ * You should have received a copy of the GNU General Public License
16681+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16682+ */
16683+
16684+/*
16685+ * fsnotify for the lower directories
16686+ */
16687+
16688+#include "aufs.h"
16689+
16690+/* FS_IN_IGNORED is unnecessary */
16691+static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
16692+ | FS_CREATE | FS_EVENT_ON_CHILD);
16693+static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
16694+static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
16695+
16696+static void au_hfsn_free_mark(struct fsnotify_mark *mark)
16697+{
16698+ struct au_hnotify *hn = container_of(mark, struct au_hnotify,
16699+ hn_mark);
16700+ /* AuDbg("here\n"); */
16701+ au_cache_free_hnotify(hn);
16702+ smp_mb__before_atomic(); /* for atomic64_dec */
16703+ if (atomic64_dec_and_test(&au_hfsn_ifree))
16704+ wake_up(&au_hfsn_wq);
16705+}
16706+
16707+static int au_hfsn_alloc(struct au_hinode *hinode)
16708+{
16709+ int err;
16710+ struct au_hnotify *hn;
16711+ struct super_block *sb;
16712+ struct au_branch *br;
16713+ struct fsnotify_mark *mark;
16714+ aufs_bindex_t bindex;
16715+
16716+ hn = hinode->hi_notify;
16717+ sb = hn->hn_aufs_inode->i_sb;
16718+ bindex = au_br_index(sb, hinode->hi_id);
16719+ br = au_sbr(sb, bindex);
16720+ AuDebugOn(!br->br_hfsn);
16721+
16722+ mark = &hn->hn_mark;
16723+ fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
16724+ mark->mask = AuHfsnMask;
16725+ /*
16726+ * by udba rename or rmdir, aufs assign a new inode to the known
16727+ * h_inode, so specify 1 to allow dups.
16728+ */
16729+ lockdep_off();
16730+ err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
16731+ lockdep_on();
16732+
16733+ return err;
16734+}
16735+
16736+static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
16737+{
16738+ struct fsnotify_mark *mark;
16739+ unsigned long long ull;
16740+ struct fsnotify_group *group;
16741+
16742+ ull = atomic64_inc_return(&au_hfsn_ifree);
16743+ BUG_ON(!ull);
16744+
16745+ mark = &hn->hn_mark;
16746+ spin_lock(&mark->lock);
16747+ group = mark->group;
16748+ fsnotify_get_group(group);
16749+ spin_unlock(&mark->lock);
16750+ lockdep_off();
16751+ fsnotify_destroy_mark(mark, group);
16752+ fsnotify_put_mark(mark);
16753+ fsnotify_put_group(group);
16754+ lockdep_on();
16755+
16756+ /* free hn by myself */
16757+ return 0;
16758+}
16759+
16760+/* ---------------------------------------------------------------------- */
16761+
16762+static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
16763+{
16764+ struct fsnotify_mark *mark;
16765+
16766+ mark = &hinode->hi_notify->hn_mark;
16767+ spin_lock(&mark->lock);
16768+ if (do_set) {
16769+ AuDebugOn(mark->mask & AuHfsnMask);
16770+ mark->mask |= AuHfsnMask;
16771+ } else {
16772+ AuDebugOn(!(mark->mask & AuHfsnMask));
16773+ mark->mask &= ~AuHfsnMask;
16774+ }
16775+ spin_unlock(&mark->lock);
16776+ /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
16777+}
16778+
16779+/* ---------------------------------------------------------------------- */
16780+
16781+/* #define AuDbgHnotify */
16782+#ifdef AuDbgHnotify
16783+static char *au_hfsn_name(u32 mask)
16784+{
16785+#ifdef CONFIG_AUFS_DEBUG
16786+#define test_ret(flag) \
16787+ do { \
16788+ if (mask & flag) \
16789+ return #flag; \
16790+ } while (0)
16791+ test_ret(FS_ACCESS);
16792+ test_ret(FS_MODIFY);
16793+ test_ret(FS_ATTRIB);
16794+ test_ret(FS_CLOSE_WRITE);
16795+ test_ret(FS_CLOSE_NOWRITE);
16796+ test_ret(FS_OPEN);
16797+ test_ret(FS_MOVED_FROM);
16798+ test_ret(FS_MOVED_TO);
16799+ test_ret(FS_CREATE);
16800+ test_ret(FS_DELETE);
16801+ test_ret(FS_DELETE_SELF);
16802+ test_ret(FS_MOVE_SELF);
16803+ test_ret(FS_UNMOUNT);
16804+ test_ret(FS_Q_OVERFLOW);
16805+ test_ret(FS_IN_IGNORED);
16806+ test_ret(FS_ISDIR);
16807+ test_ret(FS_IN_ONESHOT);
16808+ test_ret(FS_EVENT_ON_CHILD);
16809+ return "";
16810+#undef test_ret
16811+#else
16812+ return "??";
16813+#endif
16814+}
16815+#endif
16816+
16817+/* ---------------------------------------------------------------------- */
16818+
16819+static void au_hfsn_free_group(struct fsnotify_group *group)
16820+{
16821+ struct au_br_hfsnotify *hfsn = group->private;
16822+
16823+ /* AuDbg("here\n"); */
16824+ au_kfree_try_rcu(hfsn);
16825+}
16826+
16827+static int au_hfsn_handle_event(struct fsnotify_group *group,
16828+ u32 mask, const void *data, int data_type,
16829+ struct inode *dir,
16830+ const struct qstr *file_name, u32 cookie,
16831+ struct fsnotify_iter_info *iter_info)
16832+{
16833+ int err;
16834+ struct au_hnotify *hnotify;
16835+ struct inode *h_dir, *h_inode;
16836+ struct fsnotify_mark *inode_mark;
16837+
16838+ AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
16839+
16840+ err = 0;
16841+ /* if FS_UNMOUNT happens, there must be another bug */
16842+ AuDebugOn(mask & FS_UNMOUNT);
16843+ if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
16844+ goto out;
16845+
16846+ h_dir = dir;
16847+ h_inode = NULL;
16848+#ifdef AuDbgHnotify
16849+ au_debug_on();
16850+ if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
16851+ || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
16852+ AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
16853+ h_dir->i_ino, mask, au_hfsn_name(mask),
16854+ AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
16855+ /* WARN_ON(1); */
16856+ }
16857+ au_debug_off();
16858+#endif
16859+
16860+ inode_mark = fsnotify_iter_inode_mark(iter_info);
16861+ AuDebugOn(!inode_mark);
16862+ hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
16863+ err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
16864+
16865+out:
16866+ return err;
16867+}
16868+
16869+static struct fsnotify_ops au_hfsn_ops = {
16870+ .handle_event = au_hfsn_handle_event,
16871+ .free_group_priv = au_hfsn_free_group,
16872+ .free_mark = au_hfsn_free_mark
16873+};
16874+
16875+/* ---------------------------------------------------------------------- */
16876+
16877+static void au_hfsn_fin_br(struct au_branch *br)
16878+{
16879+ struct au_br_hfsnotify *hfsn;
16880+
16881+ hfsn = br->br_hfsn;
16882+ if (hfsn) {
16883+ lockdep_off();
16884+ fsnotify_put_group(hfsn->hfsn_group);
16885+ lockdep_on();
16886+ }
16887+}
16888+
16889+static int au_hfsn_init_br(struct au_branch *br, int perm)
16890+{
16891+ int err;
16892+ struct fsnotify_group *group;
16893+ struct au_br_hfsnotify *hfsn;
16894+
16895+ err = 0;
16896+ br->br_hfsn = NULL;
16897+ if (!au_br_hnotifyable(perm))
16898+ goto out;
16899+
16900+ err = -ENOMEM;
16901+ hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
16902+ if (unlikely(!hfsn))
16903+ goto out;
16904+
16905+ err = 0;
16906+ group = fsnotify_alloc_group(&au_hfsn_ops);
16907+ if (IS_ERR(group)) {
16908+ err = PTR_ERR(group);
16909+ pr_err("fsnotify_alloc_group() failed, %d\n", err);
16910+ goto out_hfsn;
16911+ }
16912+
16913+ group->private = hfsn;
16914+ hfsn->hfsn_group = group;
16915+ br->br_hfsn = hfsn;
16916+ goto out; /* success */
16917+
16918+out_hfsn:
16919+ au_kfree_try_rcu(hfsn);
16920+out:
16921+ return err;
16922+}
16923+
16924+static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
16925+{
16926+ int err;
16927+
16928+ err = 0;
16929+ if (!br->br_hfsn)
16930+ err = au_hfsn_init_br(br, perm);
16931+
16932+ return err;
16933+}
16934+
16935+/* ---------------------------------------------------------------------- */
16936+
16937+static void au_hfsn_fin(void)
16938+{
16939+ AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
16940+ wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
16941+}
16942+
16943+const struct au_hnotify_op au_hnotify_op = {
16944+ .ctl = au_hfsn_ctl,
16945+ .alloc = au_hfsn_alloc,
16946+ .free = au_hfsn_free,
16947+
16948+ .fin = au_hfsn_fin,
16949+
16950+ .reset_br = au_hfsn_reset_br,
16951+ .fin_br = au_hfsn_fin_br,
16952+ .init_br = au_hfsn_init_br
16953+};
16954diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
16955--- /usr/share/empty/fs/aufs/hfsplus.c 1970-01-01 01:00:00.000000000 +0100
16956+++ linux/fs/aufs/hfsplus.c 2021-02-22 22:51:59.058078584 +0100
16957@@ -0,0 +1,60 @@
16958+// SPDX-License-Identifier: GPL-2.0
16959+/*
16960+ * Copyright (C) 2010-2020 Junjiro R. Okajima
16961+ *
16962+ * This program, aufs is free software; you can redistribute it and/or modify
16963+ * it under the terms of the GNU General Public License as published by
16964+ * the Free Software Foundation; either version 2 of the License, or
16965+ * (at your option) any later version.
16966+ *
16967+ * This program is distributed in the hope that it will be useful,
16968+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16969+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16970+ * GNU General Public License for more details.
16971+ *
16972+ * You should have received a copy of the GNU General Public License
16973+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16974+ */
16975+
16976+/*
16977+ * special support for filesystems which acquires an inode mutex
16978+ * at final closing a file, eg, hfsplus.
16979+ *
16980+ * This trick is very simple and stupid, just to open the file before really
16981+ * necessary open to tell hfsplus that this is not the final closing.
16982+ * The caller should call au_h_open_pre() after acquiring the inode mutex,
16983+ * and au_h_open_post() after releasing it.
16984+ */
16985+
16986+#include "aufs.h"
16987+
16988+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
16989+ int force_wr)
16990+{
16991+ struct file *h_file;
16992+ struct dentry *h_dentry;
16993+
16994+ h_dentry = au_h_dptr(dentry, bindex);
16995+ AuDebugOn(!h_dentry);
16996+ AuDebugOn(d_is_negative(h_dentry));
16997+
16998+ h_file = NULL;
16999+ if (au_test_hfsplus(h_dentry->d_sb)
17000+ && d_is_reg(h_dentry))
17001+ h_file = au_h_open(dentry, bindex,
17002+ O_RDONLY | O_NOATIME | O_LARGEFILE,
17003+ /*file*/NULL, force_wr);
17004+ return h_file;
17005+}
17006+
17007+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
17008+ struct file *h_file)
17009+{
17010+ struct au_branch *br;
17011+
17012+ if (h_file) {
17013+ fput(h_file);
17014+ br = au_sbr(dentry->d_sb, bindex);
17015+ au_lcnt_dec(&br->br_nfiles);
17016+ }
17017+}
17018diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
17019--- /usr/share/empty/fs/aufs/hnotify.c 1970-01-01 01:00:00.000000000 +0100
17020+++ linux/fs/aufs/hnotify.c 2021-02-22 22:51:59.058078584 +0100
17021@@ -0,0 +1,715 @@
17022+// SPDX-License-Identifier: GPL-2.0
17023+/*
17024+ * Copyright (C) 2005-2020 Junjiro R. Okajima
17025+ *
17026+ * This program, aufs is free software; you can redistribute it and/or modify
17027+ * it under the terms of the GNU General Public License as published by
17028+ * the Free Software Foundation; either version 2 of the License, or
17029+ * (at your option) any later version.
17030+ *
17031+ * This program is distributed in the hope that it will be useful,
17032+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17033+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17034+ * GNU General Public License for more details.
17035+ *
17036+ * You should have received a copy of the GNU General Public License
17037+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
17038+ */
17039+
17040+/*
17041+ * abstraction to notify the direct changes on lower directories
17042+ */
17043+
17044+/* #include <linux/iversion.h> */
17045+#include "aufs.h"
17046+
17047+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
17048+{
17049+ int err;
17050+ struct au_hnotify *hn;
17051+
17052+ err = -ENOMEM;
17053+ hn = au_cache_alloc_hnotify();
17054+ if (hn) {
17055+ hn->hn_aufs_inode = inode;
17056+ hinode->hi_notify = hn;
17057+ err = au_hnotify_op.alloc(hinode);
17058+ AuTraceErr(err);
17059+ if (unlikely(err)) {
17060+ hinode->hi_notify = NULL;
17061+ au_cache_free_hnotify(hn);
17062+ /*
17063+ * The upper dir was removed by udba, but the same named
17064+ * dir left. In this case, aufs assigns a new inode
17065+ * number and set the monitor again.
17066+ * For the lower dir, the old monitor is still left.
17067+ */
17068+ if (err == -EEXIST)
17069+ err = 0;
17070+ }
17071+ }
17072+
17073+ AuTraceErr(err);
17074+ return err;
17075+}
17076+
17077+void au_hn_free(struct au_hinode *hinode)
17078+{
17079+ struct au_hnotify *hn;
17080+
17081+ hn = hinode->hi_notify;
17082+ if (hn) {
17083+ hinode->hi_notify = NULL;
17084+ if (au_hnotify_op.free(hinode, hn))
17085+ au_cache_free_hnotify(hn);
17086+ }
17087+}
17088+
17089+/* ---------------------------------------------------------------------- */
17090+
17091+void au_hn_ctl(struct au_hinode *hinode, int do_set)
17092+{
17093+ if (hinode->hi_notify)
17094+ au_hnotify_op.ctl(hinode, do_set);
17095+}
17096+
17097+void au_hn_reset(struct inode *inode, unsigned int flags)
17098+{
17099+ aufs_bindex_t bindex, bbot;
17100+ struct inode *hi;
17101+ struct dentry *iwhdentry;
17102+
17103+ bbot = au_ibbot(inode);
17104+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
17105+ hi = au_h_iptr(inode, bindex);
17106+ if (!hi)
17107+ continue;
17108+
17109+ /* inode_lock_nested(hi, AuLsc_I_CHILD); */
17110+ iwhdentry = au_hi_wh(inode, bindex);
17111+ if (iwhdentry)
17112+ dget(iwhdentry);
17113+ au_igrab(hi);
17114+ au_set_h_iptr(inode, bindex, NULL, 0);
17115+ au_set_h_iptr(inode, bindex, au_igrab(hi),
17116+ flags & ~AuHi_XINO);
17117+ iput(hi);
17118+ dput(iwhdentry);
17119+ /* inode_unlock(hi); */
17120+ }
17121+}
17122+
17123+/* ---------------------------------------------------------------------- */
17124+
17125+static int hn_xino(struct inode *inode, struct inode *h_inode)
17126+{
17127+ int err;
17128+ aufs_bindex_t bindex, bbot, bfound, btop;
17129+ struct inode *h_i;
17130+
17131+ err = 0;
17132+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17133+ pr_warn("branch root dir was changed\n");
17134+ goto out;
17135+ }
17136+
17137+ bfound = -1;
17138+ bbot = au_ibbot(inode);
17139+ btop = au_ibtop(inode);
17140+#if 0 /* reserved for future use */
17141+ if (bindex == bbot) {
17142+ /* keep this ino in rename case */
17143+ goto out;
17144+ }
17145+#endif
17146+ for (bindex = btop; bindex <= bbot; bindex++)
17147+ if (au_h_iptr(inode, bindex) == h_inode) {
17148+ bfound = bindex;
17149+ break;
17150+ }
17151+ if (bfound < 0)
17152+ goto out;
17153+
17154+ for (bindex = btop; bindex <= bbot; bindex++) {
17155+ h_i = au_h_iptr(inode, bindex);
17156+ if (!h_i)
17157+ continue;
17158+
17159+ err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
17160+ /* ignore this error */
17161+ /* bad action? */
17162+ }
17163+
17164+ /* children inode number will be broken */
17165+
17166+out:
17167+ AuTraceErr(err);
17168+ return err;
17169+}
17170+
17171+static int hn_gen_tree(struct dentry *dentry)
17172+{
17173+ int err, i, j, ndentry;
17174+ struct au_dcsub_pages dpages;
17175+ struct au_dpage *dpage;
17176+ struct dentry **dentries;
17177+
17178+ err = au_dpages_init(&dpages, GFP_NOFS);
17179+ if (unlikely(err))
17180+ goto out;
17181+ err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
17182+ if (unlikely(err))
17183+ goto out_dpages;
17184+
17185+ for (i = 0; i < dpages.ndpage; i++) {
17186+ dpage = dpages.dpages + i;
17187+ dentries = dpage->dentries;
17188+ ndentry = dpage->ndentry;
17189+ for (j = 0; j < ndentry; j++) {
17190+ struct dentry *d;
17191+
17192+ d = dentries[j];
17193+ if (IS_ROOT(d))
17194+ continue;
17195+
17196+ au_digen_dec(d);
17197+ if (d_really_is_positive(d))
17198+ /* todo: reset children xino?
17199+ cached children only? */
17200+ au_iigen_dec(d_inode(d));
17201+ }
17202+ }
17203+
17204+out_dpages:
17205+ au_dpages_free(&dpages);
17206+out:
17207+ return err;
17208+}
17209+
17210+/*
17211+ * return 0 if processed.
17212+ */
17213+static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
17214+ const unsigned int isdir)
17215+{
17216+ int err;
17217+ struct dentry *d;
17218+ struct qstr *dname;
17219+
17220+ err = 1;
17221+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17222+ pr_warn("branch root dir was changed\n");
17223+ err = 0;
17224+ goto out;
17225+ }
17226+
17227+ if (!isdir) {
17228+ AuDebugOn(!name);
17229+ au_iigen_dec(inode);
17230+ spin_lock(&inode->i_lock);
17231+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
17232+ spin_lock(&d->d_lock);
17233+ dname = &d->d_name;
17234+ if (dname->len != nlen
17235+ && memcmp(dname->name, name, nlen)) {
17236+ spin_unlock(&d->d_lock);
17237+ continue;
17238+ }
17239+ err = 0;
17240+ au_digen_dec(d);
17241+ spin_unlock(&d->d_lock);
17242+ break;
17243+ }
17244+ spin_unlock(&inode->i_lock);
17245+ } else {
17246+ au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
17247+ d = d_find_any_alias(inode);
17248+ if (!d) {
17249+ au_iigen_dec(inode);
17250+ goto out;
17251+ }
17252+
17253+ spin_lock(&d->d_lock);
17254+ dname = &d->d_name;
17255+ if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
17256+ spin_unlock(&d->d_lock);
17257+ err = hn_gen_tree(d);
17258+ spin_lock(&d->d_lock);
17259+ }
17260+ spin_unlock(&d->d_lock);
17261+ dput(d);
17262+ }
17263+
17264+out:
17265+ AuTraceErr(err);
17266+ return err;
17267+}
17268+
17269+static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
17270+{
17271+ int err;
17272+
17273+ if (IS_ROOT(dentry)) {
17274+ pr_warn("branch root dir was changed\n");
17275+ return 0;
17276+ }
17277+
17278+ err = 0;
17279+ if (!isdir) {
17280+ au_digen_dec(dentry);
17281+ if (d_really_is_positive(dentry))
17282+ au_iigen_dec(d_inode(dentry));
17283+ } else {
17284+ au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
17285+ if (d_really_is_positive(dentry))
17286+ err = hn_gen_tree(dentry);
17287+ }
17288+
17289+ AuTraceErr(err);
17290+ return err;
17291+}
17292+
17293+/* ---------------------------------------------------------------------- */
17294+
17295+/* hnotify job flags */
17296+#define AuHnJob_XINO0 1
17297+#define AuHnJob_GEN (1 << 1)
17298+#define AuHnJob_DIRENT (1 << 2)
17299+#define AuHnJob_ISDIR (1 << 3)
17300+#define AuHnJob_TRYXINO0 (1 << 4)
17301+#define AuHnJob_MNTPNT (1 << 5)
17302+#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
17303+#define au_fset_hnjob(flags, name) \
17304+ do { (flags) |= AuHnJob_##name; } while (0)
17305+#define au_fclr_hnjob(flags, name) \
17306+ do { (flags) &= ~AuHnJob_##name; } while (0)
17307+
17308+enum {
17309+ AuHn_CHILD,
17310+ AuHn_PARENT,
17311+ AuHnLast
17312+};
17313+
17314+struct au_hnotify_args {
17315+ struct inode *h_dir, *dir, *h_child_inode;
17316+ u32 mask;
17317+ unsigned int flags[AuHnLast];
17318+ unsigned int h_child_nlen;
17319+ char h_child_name[];
17320+};
17321+
17322+struct hn_job_args {
17323+ unsigned int flags;
17324+ struct inode *inode, *h_inode, *dir, *h_dir;
17325+ struct dentry *dentry;
17326+ char *h_name;
17327+ int h_nlen;
17328+};
17329+
17330+static int hn_job(struct hn_job_args *a)
17331+{
17332+ const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
17333+ int e;
17334+
17335+ /* reset xino */
17336+ if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
17337+ hn_xino(a->inode, a->h_inode); /* ignore this error */
17338+
17339+ if (au_ftest_hnjob(a->flags, TRYXINO0)
17340+ && a->inode
17341+ && a->h_inode) {
17342+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
17343+ if (!a->h_inode->i_nlink
17344+ && !(a->h_inode->i_state & I_LINKABLE))
17345+ hn_xino(a->inode, a->h_inode); /* ignore this error */
17346+ inode_unlock_shared(a->h_inode);
17347+ }
17348+
17349+ /* make the generation obsolete */
17350+ if (au_ftest_hnjob(a->flags, GEN)) {
17351+ e = -1;
17352+ if (a->inode)
17353+ e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
17354+ isdir);
17355+ if (e && a->dentry)
17356+ hn_gen_by_name(a->dentry, isdir);
17357+ /* ignore this error */
17358+ }
17359+
17360+ /* make dir entries obsolete */
17361+ if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
17362+ struct au_vdir *vdir;
17363+
17364+ vdir = au_ivdir(a->inode);
17365+ if (vdir)
17366+ vdir->vd_jiffy = 0;
17367+ /* IMustLock(a->inode); */
17368+ /* inode_inc_iversion(a->inode); */
17369+ }
17370+
17371+ /* can do nothing but warn */
17372+ if (au_ftest_hnjob(a->flags, MNTPNT)
17373+ && a->dentry
17374+ && d_mountpoint(a->dentry))
17375+ pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
17376+
17377+ return 0;
17378+}
17379+
17380+/* ---------------------------------------------------------------------- */
17381+
17382+static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
17383+ struct inode *dir)
17384+{
17385+ struct dentry *dentry, *d, *parent;
17386+ struct qstr *dname;
17387+
17388+ parent = d_find_any_alias(dir);
17389+ if (!parent)
17390+ return NULL;
17391+
17392+ dentry = NULL;
17393+ spin_lock(&parent->d_lock);
17394+ list_for_each_entry(d, &parent->d_subdirs, d_child) {
17395+ /* AuDbg("%pd\n", d); */
17396+ spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
17397+ dname = &d->d_name;
17398+ if (dname->len != nlen || memcmp(dname->name, name, nlen))
17399+ goto cont_unlock;
17400+ if (au_di(d))
17401+ au_digen_dec(d);
17402+ else
17403+ goto cont_unlock;
17404+ if (au_dcount(d) > 0) {
17405+ dentry = dget_dlock(d);
17406+ spin_unlock(&d->d_lock);
17407+ break;
17408+ }
17409+
17410+cont_unlock:
17411+ spin_unlock(&d->d_lock);
17412+ }
17413+ spin_unlock(&parent->d_lock);
17414+ dput(parent);
17415+
17416+ if (dentry)
17417+ di_write_lock_child(dentry);
17418+
17419+ return dentry;
17420+}
17421+
17422+static struct inode *lookup_wlock_by_ino(struct super_block *sb,
17423+ aufs_bindex_t bindex, ino_t h_ino)
17424+{
17425+ struct inode *inode;
17426+ ino_t ino;
17427+ int err;
17428+
17429+ inode = NULL;
17430+ err = au_xino_read(sb, bindex, h_ino, &ino);
17431+ if (!err && ino)
17432+ inode = ilookup(sb, ino);
17433+ if (!inode)
17434+ goto out;
17435+
17436+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17437+ pr_warn("wrong root branch\n");
17438+ iput(inode);
17439+ inode = NULL;
17440+ goto out;
17441+ }
17442+
17443+ ii_write_lock_child(inode);
17444+
17445+out:
17446+ return inode;
17447+}
17448+
17449+static void au_hn_bh(void *_args)
17450+{
17451+ struct au_hnotify_args *a = _args;
17452+ struct super_block *sb;
17453+ aufs_bindex_t bindex, bbot, bfound;
17454+ unsigned char xino, try_iput;
17455+ int err;
17456+ struct inode *inode;
17457+ ino_t h_ino;
17458+ struct hn_job_args args;
17459+ struct dentry *dentry;
17460+ struct au_sbinfo *sbinfo;
17461+
17462+ AuDebugOn(!_args);
17463+ AuDebugOn(!a->h_dir);
17464+ AuDebugOn(!a->dir);
17465+ AuDebugOn(!a->mask);
17466+ AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
17467+ a->mask, a->dir->i_ino, a->h_dir->i_ino,
17468+ a->h_child_inode ? a->h_child_inode->i_ino : 0);
17469+
17470+ inode = NULL;
17471+ dentry = NULL;
17472+ /*
17473+ * do not lock a->dir->i_mutex here
17474+ * because of d_revalidate() may cause a deadlock.
17475+ */
17476+ sb = a->dir->i_sb;
17477+ AuDebugOn(!sb);
17478+ sbinfo = au_sbi(sb);
17479+ AuDebugOn(!sbinfo);
17480+ si_write_lock(sb, AuLock_NOPLMW);
17481+
17482+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
17483+ switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
17484+ case FS_MOVED_FROM:
17485+ case FS_MOVED_TO:
17486+ AuWarn1("DIRREN with UDBA may not work correctly "
17487+ "for the direct rename(2)\n");
17488+ }
17489+
17490+ ii_read_lock_parent(a->dir);
17491+ bfound = -1;
17492+ bbot = au_ibbot(a->dir);
17493+ for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
17494+ if (au_h_iptr(a->dir, bindex) == a->h_dir) {
17495+ bfound = bindex;
17496+ break;
17497+ }
17498+ ii_read_unlock(a->dir);
17499+ if (unlikely(bfound < 0))
17500+ goto out;
17501+
17502+ xino = !!au_opt_test(au_mntflags(sb), XINO);
17503+ h_ino = 0;
17504+ if (a->h_child_inode)
17505+ h_ino = a->h_child_inode->i_ino;
17506+
17507+ if (a->h_child_nlen
17508+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
17509+ || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
17510+ dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
17511+ a->dir);
17512+ try_iput = 0;
17513+ if (dentry && d_really_is_positive(dentry))
17514+ inode = d_inode(dentry);
17515+ if (xino && !inode && h_ino
17516+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
17517+ || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
17518+ || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
17519+ inode = lookup_wlock_by_ino(sb, bfound, h_ino);
17520+ try_iput = 1;
17521+ }
17522+
17523+ args.flags = a->flags[AuHn_CHILD];
17524+ args.dentry = dentry;
17525+ args.inode = inode;
17526+ args.h_inode = a->h_child_inode;
17527+ args.dir = a->dir;
17528+ args.h_dir = a->h_dir;
17529+ args.h_name = a->h_child_name;
17530+ args.h_nlen = a->h_child_nlen;
17531+ err = hn_job(&args);
17532+ if (dentry) {
17533+ if (au_di(dentry))
17534+ di_write_unlock(dentry);
17535+ dput(dentry);
17536+ }
17537+ if (inode && try_iput) {
17538+ ii_write_unlock(inode);
17539+ iput(inode);
17540+ }
17541+
17542+ ii_write_lock_parent(a->dir);
17543+ args.flags = a->flags[AuHn_PARENT];
17544+ args.dentry = NULL;
17545+ args.inode = a->dir;
17546+ args.h_inode = a->h_dir;
17547+ args.dir = NULL;
17548+ args.h_dir = NULL;
17549+ args.h_name = NULL;
17550+ args.h_nlen = 0;
17551+ err = hn_job(&args);
17552+ ii_write_unlock(a->dir);
17553+
17554+out:
17555+ iput(a->h_child_inode);
17556+ iput(a->h_dir);
17557+ iput(a->dir);
17558+ si_write_unlock(sb);
17559+ au_nwt_done(&sbinfo->si_nowait);
17560+ au_kfree_rcu(a);
17561+}
17562+
17563+/* ---------------------------------------------------------------------- */
17564+
17565+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17566+ const struct qstr *h_child_qstr, struct inode *h_child_inode)
17567+{
17568+ int err, len;
17569+ unsigned int flags[AuHnLast], f;
17570+ unsigned char isdir, isroot, wh;
17571+ struct inode *dir;
17572+ struct au_hnotify_args *args;
17573+ char *p, *h_child_name;
17574+
17575+ err = 0;
17576+ AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
17577+ dir = igrab(hnotify->hn_aufs_inode);
17578+ if (!dir)
17579+ goto out;
17580+
17581+ isroot = (dir->i_ino == AUFS_ROOT_INO);
17582+ wh = 0;
17583+ h_child_name = (void *)h_child_qstr->name;
17584+ len = h_child_qstr->len;
17585+ if (h_child_name) {
17586+ if (len > AUFS_WH_PFX_LEN
17587+ && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
17588+ h_child_name += AUFS_WH_PFX_LEN;
17589+ len -= AUFS_WH_PFX_LEN;
17590+ wh = 1;
17591+ }
17592+ }
17593+
17594+ isdir = 0;
17595+ if (h_child_inode)
17596+ isdir = !!S_ISDIR(h_child_inode->i_mode);
17597+ flags[AuHn_PARENT] = AuHnJob_ISDIR;
17598+ flags[AuHn_CHILD] = 0;
17599+ if (isdir)
17600+ flags[AuHn_CHILD] = AuHnJob_ISDIR;
17601+ au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
17602+ au_fset_hnjob(flags[AuHn_CHILD], GEN);
17603+ switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
17604+ case FS_MOVED_FROM:
17605+ case FS_MOVED_TO:
17606+ au_fset_hnjob(flags[AuHn_CHILD], XINO0);
17607+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17608+ fallthrough;
17609+ case FS_CREATE:
17610+ AuDebugOn(!h_child_name);
17611+ break;
17612+
17613+ case FS_DELETE:
17614+ /*
17615+ * aufs never be able to get this child inode.
17616+ * revalidation should be in d_revalidate()
17617+ * by checking i_nlink, i_generation or d_unhashed().
17618+ */
17619+ AuDebugOn(!h_child_name);
17620+ au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
17621+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17622+ break;
17623+
17624+ default:
17625+ AuDebugOn(1);
17626+ }
17627+
17628+ if (wh)
17629+ h_child_inode = NULL;
17630+
17631+ err = -ENOMEM;
17632+ /* iput() and kfree() will be called in au_hnotify() */
17633+ args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
17634+ if (unlikely(!args)) {
17635+ AuErr1("no memory\n");
17636+ iput(dir);
17637+ goto out;
17638+ }
17639+ args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
17640+ args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
17641+ args->mask = mask;
17642+ args->dir = dir;
17643+ args->h_dir = igrab(h_dir);
17644+ if (h_child_inode)
17645+ h_child_inode = igrab(h_child_inode); /* can be NULL */
17646+ args->h_child_inode = h_child_inode;
17647+ args->h_child_nlen = len;
17648+ if (len) {
17649+ p = (void *)args;
17650+ p += sizeof(*args);
17651+ memcpy(p, h_child_name, len);
17652+ p[len] = 0;
17653+ }
17654+
17655+ /* NFS fires the event for silly-renamed one from kworker */
17656+ f = 0;
17657+ if (!dir->i_nlink
17658+ || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
17659+ f = AuWkq_NEST;
17660+ err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
17661+ if (unlikely(err)) {
17662+ pr_err("wkq %d\n", err);
17663+ iput(args->h_child_inode);
17664+ iput(args->h_dir);
17665+ iput(args->dir);
17666+ au_kfree_rcu(args);
17667+ }
17668+
17669+out:
17670+ return err;
17671+}
17672+
17673+/* ---------------------------------------------------------------------- */
17674+
17675+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
17676+{
17677+ int err;
17678+
17679+ AuDebugOn(!(udba & AuOptMask_UDBA));
17680+
17681+ err = 0;
17682+ if (au_hnotify_op.reset_br)
17683+ err = au_hnotify_op.reset_br(udba, br, perm);
17684+
17685+ return err;
17686+}
17687+
17688+int au_hnotify_init_br(struct au_branch *br, int perm)
17689+{
17690+ int err;
17691+
17692+ err = 0;
17693+ if (au_hnotify_op.init_br)
17694+ err = au_hnotify_op.init_br(br, perm);
17695+
17696+ return err;
17697+}
17698+
17699+void au_hnotify_fin_br(struct au_branch *br)
17700+{
17701+ if (au_hnotify_op.fin_br)
17702+ au_hnotify_op.fin_br(br);
17703+}
17704+
17705+static void au_hn_destroy_cache(void)
17706+{
17707+ kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
17708+ au_cache[AuCache_HNOTIFY] = NULL;
17709+}
17710+
17711+int __init au_hnotify_init(void)
17712+{
17713+ int err;
17714+
17715+ err = -ENOMEM;
17716+ au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
17717+ if (au_cache[AuCache_HNOTIFY]) {
17718+ err = 0;
17719+ if (au_hnotify_op.init)
17720+ err = au_hnotify_op.init();
17721+ if (unlikely(err))
17722+ au_hn_destroy_cache();
17723+ }
17724+ AuTraceErr(err);
17725+ return err;
17726+}
17727+
17728+void au_hnotify_fin(void)
17729+{
17730+ if (au_hnotify_op.fin)
17731+ au_hnotify_op.fin();
17732+
17733+ /* cf. au_cache_fin() */
17734+ if (au_cache[AuCache_HNOTIFY])
17735+ au_hn_destroy_cache();
17736+}
17737diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
17738--- /usr/share/empty/fs/aufs/iinfo.c 1970-01-01 01:00:00.000000000 +0100
17739+++ linux/fs/aufs/iinfo.c 2021-02-22 22:51:59.061411920 +0100
17740@@ -0,0 +1,286 @@
17741+// SPDX-License-Identifier: GPL-2.0
17742+/*
17743+ * Copyright (C) 2005-2020 Junjiro R. Okajima
17744+ *
17745+ * This program, aufs is free software; you can redistribute it and/or modify
17746+ * it under the terms of the GNU General Public License as published by
17747+ * the Free Software Foundation; either version 2 of the License, or
17748+ * (at your option) any later version.
17749+ *
17750+ * This program is distributed in the hope that it will be useful,
17751+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17752+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17753+ * GNU General Public License for more details.
17754+ *
17755+ * You should have received a copy of the GNU General Public License
17756+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
17757+ */
17758+
17759+/*
17760+ * inode private data
17761+ */
17762+
17763+#include "aufs.h"
17764+
17765+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
17766+{
17767+ struct inode *h_inode;
17768+ struct au_hinode *hinode;
17769+
17770+ IiMustAnyLock(inode);
17771+
17772+ hinode = au_hinode(au_ii(inode), bindex);
17773+ h_inode = hinode->hi_inode;
17774+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17775+ return h_inode;
17776+}
17777+
17778+/* todo: hard/soft set? */
17779+void au_hiput(struct au_hinode *hinode)
17780+{
17781+ au_hn_free(hinode);
17782+ dput(hinode->hi_whdentry);
17783+ iput(hinode->hi_inode);
17784+}
17785+
17786+unsigned int au_hi_flags(struct inode *inode, int isdir)
17787+{
17788+ unsigned int flags;
17789+ const unsigned int mnt_flags = au_mntflags(inode->i_sb);
17790+
17791+ flags = 0;
17792+ if (au_opt_test(mnt_flags, XINO))
17793+ au_fset_hi(flags, XINO);
17794+ if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
17795+ au_fset_hi(flags, HNOTIFY);
17796+ return flags;
17797+}
17798+
17799+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
17800+ struct inode *h_inode, unsigned int flags)
17801+{
17802+ struct au_hinode *hinode;
17803+ struct inode *hi;
17804+ struct au_iinfo *iinfo = au_ii(inode);
17805+
17806+ IiMustWriteLock(inode);
17807+
17808+ hinode = au_hinode(iinfo, bindex);
17809+ hi = hinode->hi_inode;
17810+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17811+
17812+ if (hi)
17813+ au_hiput(hinode);
17814+ hinode->hi_inode = h_inode;
17815+ if (h_inode) {
17816+ int err;
17817+ struct super_block *sb = inode->i_sb;
17818+ struct au_branch *br;
17819+
17820+ AuDebugOn(inode->i_mode
17821+ && (h_inode->i_mode & S_IFMT)
17822+ != (inode->i_mode & S_IFMT));
17823+ if (bindex == iinfo->ii_btop)
17824+ au_cpup_igen(inode, h_inode);
17825+ br = au_sbr(sb, bindex);
17826+ hinode->hi_id = br->br_id;
17827+ if (au_ftest_hi(flags, XINO)) {
17828+ err = au_xino_write(sb, bindex, h_inode->i_ino,
17829+ inode->i_ino);
17830+ if (unlikely(err))
17831+ AuIOErr1("failed au_xino_write() %d\n", err);
17832+ }
17833+
17834+ if (au_ftest_hi(flags, HNOTIFY)
17835+ && au_br_hnotifyable(br->br_perm)) {
17836+ err = au_hn_alloc(hinode, inode);
17837+ if (unlikely(err))
17838+ AuIOErr1("au_hn_alloc() %d\n", err);
17839+ }
17840+ }
17841+}
17842+
17843+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
17844+ struct dentry *h_wh)
17845+{
17846+ struct au_hinode *hinode;
17847+
17848+ IiMustWriteLock(inode);
17849+
17850+ hinode = au_hinode(au_ii(inode), bindex);
17851+ AuDebugOn(hinode->hi_whdentry);
17852+ hinode->hi_whdentry = h_wh;
17853+}
17854+
17855+void au_update_iigen(struct inode *inode, int half)
17856+{
17857+ struct au_iinfo *iinfo;
17858+ struct au_iigen *iigen;
17859+ unsigned int sigen;
17860+
17861+ sigen = au_sigen(inode->i_sb);
17862+ iinfo = au_ii(inode);
17863+ iigen = &iinfo->ii_generation;
17864+ spin_lock(&iigen->ig_spin);
17865+ iigen->ig_generation = sigen;
17866+ if (half)
17867+ au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
17868+ else
17869+ au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
17870+ spin_unlock(&iigen->ig_spin);
17871+}
17872+
17873+/* it may be called at remount time, too */
17874+void au_update_ibrange(struct inode *inode, int do_put_zero)
17875+{
17876+ struct au_iinfo *iinfo;
17877+ aufs_bindex_t bindex, bbot;
17878+
17879+ AuDebugOn(au_is_bad_inode(inode));
17880+ IiMustWriteLock(inode);
17881+
17882+ iinfo = au_ii(inode);
17883+ if (do_put_zero && iinfo->ii_btop >= 0) {
17884+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
17885+ bindex++) {
17886+ struct inode *h_i;
17887+
17888+ h_i = au_hinode(iinfo, bindex)->hi_inode;
17889+ if (h_i
17890+ && !h_i->i_nlink
17891+ && !(h_i->i_state & I_LINKABLE))
17892+ au_set_h_iptr(inode, bindex, NULL, 0);
17893+ }
17894+ }
17895+
17896+ iinfo->ii_btop = -1;
17897+ iinfo->ii_bbot = -1;
17898+ bbot = au_sbbot(inode->i_sb);
17899+ for (bindex = 0; bindex <= bbot; bindex++)
17900+ if (au_hinode(iinfo, bindex)->hi_inode) {
17901+ iinfo->ii_btop = bindex;
17902+ break;
17903+ }
17904+ if (iinfo->ii_btop >= 0)
17905+ for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
17906+ if (au_hinode(iinfo, bindex)->hi_inode) {
17907+ iinfo->ii_bbot = bindex;
17908+ break;
17909+ }
17910+ AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
17911+}
17912+
17913+/* ---------------------------------------------------------------------- */
17914+
17915+void au_icntnr_init_once(void *_c)
17916+{
17917+ struct au_icntnr *c = _c;
17918+ struct au_iinfo *iinfo = &c->iinfo;
17919+
17920+ spin_lock_init(&iinfo->ii_generation.ig_spin);
17921+ au_rw_init(&iinfo->ii_rwsem);
17922+ inode_init_once(&c->vfs_inode);
17923+}
17924+
17925+void au_hinode_init(struct au_hinode *hinode)
17926+{
17927+ hinode->hi_inode = NULL;
17928+ hinode->hi_id = -1;
17929+ au_hn_init(hinode);
17930+ hinode->hi_whdentry = NULL;
17931+}
17932+
17933+int au_iinfo_init(struct inode *inode)
17934+{
17935+ struct au_iinfo *iinfo;
17936+ struct super_block *sb;
17937+ struct au_hinode *hi;
17938+ int nbr, i;
17939+
17940+ sb = inode->i_sb;
17941+ iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
17942+ nbr = au_sbbot(sb) + 1;
17943+ if (unlikely(nbr <= 0))
17944+ nbr = 1;
17945+ hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
17946+ if (hi) {
17947+ au_lcnt_inc(&au_sbi(sb)->si_ninodes);
17948+
17949+ iinfo->ii_hinode = hi;
17950+ for (i = 0; i < nbr; i++, hi++)
17951+ au_hinode_init(hi);
17952+
17953+ iinfo->ii_generation.ig_generation = au_sigen(sb);
17954+ iinfo->ii_btop = -1;
17955+ iinfo->ii_bbot = -1;
17956+ iinfo->ii_vdir = NULL;
17957+ return 0;
17958+ }
17959+ return -ENOMEM;
17960+}
17961+
17962+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
17963+{
17964+ int err, i;
17965+ struct au_hinode *hip;
17966+
17967+ AuRwMustWriteLock(&iinfo->ii_rwsem);
17968+
17969+ err = -ENOMEM;
17970+ hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
17971+ may_shrink);
17972+ if (hip) {
17973+ iinfo->ii_hinode = hip;
17974+ i = iinfo->ii_bbot + 1;
17975+ hip += i;
17976+ for (; i < nbr; i++, hip++)
17977+ au_hinode_init(hip);
17978+ err = 0;
17979+ }
17980+
17981+ return err;
17982+}
17983+
17984+void au_iinfo_fin(struct inode *inode)
17985+{
17986+ struct au_iinfo *iinfo;
17987+ struct au_hinode *hi;
17988+ struct super_block *sb;
17989+ aufs_bindex_t bindex, bbot;
17990+ const unsigned char unlinked = !inode->i_nlink;
17991+
17992+ AuDebugOn(au_is_bad_inode(inode));
17993+
17994+ sb = inode->i_sb;
17995+ au_lcnt_dec(&au_sbi(sb)->si_ninodes);
17996+ if (si_pid_test(sb))
17997+ au_xino_delete_inode(inode, unlinked);
17998+ else {
17999+ /*
18000+ * it is safe to hide the dependency between sbinfo and
18001+ * sb->s_umount.
18002+ */
18003+ lockdep_off();
18004+ si_noflush_read_lock(sb);
18005+ au_xino_delete_inode(inode, unlinked);
18006+ si_read_unlock(sb);
18007+ lockdep_on();
18008+ }
18009+
18010+ iinfo = au_ii(inode);
18011+ if (iinfo->ii_vdir)
18012+ au_vdir_free(iinfo->ii_vdir);
18013+
18014+ bindex = iinfo->ii_btop;
18015+ if (bindex >= 0) {
18016+ hi = au_hinode(iinfo, bindex);
18017+ bbot = iinfo->ii_bbot;
18018+ while (bindex++ <= bbot) {
18019+ if (hi->hi_inode)
18020+ au_hiput(hi);
18021+ hi++;
18022+ }
18023+ }
18024+ au_kfree_rcu(iinfo->ii_hinode);
18025+ AuRwDestroy(&iinfo->ii_rwsem);
18026+}
18027diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
18028--- /usr/share/empty/fs/aufs/inode.c 1970-01-01 01:00:00.000000000 +0100
18029+++ linux/fs/aufs/inode.c 2021-02-22 22:51:59.061411920 +0100
18030@@ -0,0 +1,529 @@
18031+// SPDX-License-Identifier: GPL-2.0
18032+/*
18033+ * Copyright (C) 2005-2020 Junjiro R. Okajima
18034+ *
18035+ * This program, aufs is free software; you can redistribute it and/or modify
18036+ * it under the terms of the GNU General Public License as published by
18037+ * the Free Software Foundation; either version 2 of the License, or
18038+ * (at your option) any later version.
18039+ *
18040+ * This program is distributed in the hope that it will be useful,
18041+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18042+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18043+ * GNU General Public License for more details.
18044+ *
18045+ * You should have received a copy of the GNU General Public License
18046+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
18047+ */
18048+
18049+/*
18050+ * inode functions
18051+ */
18052+
18053+#include <linux/iversion.h>
18054+#include "aufs.h"
18055+
18056+struct inode *au_igrab(struct inode *inode)
18057+{
18058+ if (inode) {
18059+ AuDebugOn(!atomic_read(&inode->i_count));
18060+ ihold(inode);
18061+ }
18062+ return inode;
18063+}
18064+
18065+static void au_refresh_hinode_attr(struct inode *inode, int do_version)
18066+{
18067+ au_cpup_attr_all(inode, /*force*/0);
18068+ au_update_iigen(inode, /*half*/1);
18069+ if (do_version)
18070+ inode_inc_iversion(inode);
18071+}
18072+
18073+static int au_ii_refresh(struct inode *inode, int *update)
18074+{
18075+ int err, e, nbr;
18076+ umode_t type;
18077+ aufs_bindex_t bindex, new_bindex;
18078+ struct super_block *sb;
18079+ struct au_iinfo *iinfo;
18080+ struct au_hinode *p, *q, tmp;
18081+
18082+ AuDebugOn(au_is_bad_inode(inode));
18083+ IiMustWriteLock(inode);
18084+
18085+ *update = 0;
18086+ sb = inode->i_sb;
18087+ nbr = au_sbbot(sb) + 1;
18088+ type = inode->i_mode & S_IFMT;
18089+ iinfo = au_ii(inode);
18090+ err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
18091+ if (unlikely(err))
18092+ goto out;
18093+
18094+ AuDebugOn(iinfo->ii_btop < 0);
18095+ p = au_hinode(iinfo, iinfo->ii_btop);
18096+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
18097+ bindex++, p++) {
18098+ if (!p->hi_inode)
18099+ continue;
18100+
18101+ AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
18102+ new_bindex = au_br_index(sb, p->hi_id);
18103+ if (new_bindex == bindex)
18104+ continue;
18105+
18106+ if (new_bindex < 0) {
18107+ *update = 1;
18108+ au_hiput(p);
18109+ p->hi_inode = NULL;
18110+ continue;
18111+ }
18112+
18113+ if (new_bindex < iinfo->ii_btop)
18114+ iinfo->ii_btop = new_bindex;
18115+ if (iinfo->ii_bbot < new_bindex)
18116+ iinfo->ii_bbot = new_bindex;
18117+ /* swap two lower inode, and loop again */
18118+ q = au_hinode(iinfo, new_bindex);
18119+ tmp = *q;
18120+ *q = *p;
18121+ *p = tmp;
18122+ if (tmp.hi_inode) {
18123+ bindex--;
18124+ p--;
18125+ }
18126+ }
18127+ au_update_ibrange(inode, /*do_put_zero*/0);
18128+ au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
18129+ e = au_dy_irefresh(inode);
18130+ if (unlikely(e && !err))
18131+ err = e;
18132+
18133+out:
18134+ AuTraceErr(err);
18135+ return err;
18136+}
18137+
18138+void au_refresh_iop(struct inode *inode, int force_getattr)
18139+{
18140+ int type;
18141+ struct au_sbinfo *sbi = au_sbi(inode->i_sb);
18142+ const struct inode_operations *iop
18143+ = force_getattr ? aufs_iop : sbi->si_iop_array;
18144+
18145+ if (inode->i_op == iop)
18146+ return;
18147+
18148+ switch (inode->i_mode & S_IFMT) {
18149+ case S_IFDIR:
18150+ type = AuIop_DIR;
18151+ break;
18152+ case S_IFLNK:
18153+ type = AuIop_SYMLINK;
18154+ break;
18155+ default:
18156+ type = AuIop_OTHER;
18157+ break;
18158+ }
18159+
18160+ inode->i_op = iop + type;
18161+ /* unnecessary smp_wmb() */
18162+}
18163+
18164+int au_refresh_hinode_self(struct inode *inode)
18165+{
18166+ int err, update;
18167+
18168+ err = au_ii_refresh(inode, &update);
18169+ if (!err)
18170+ au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
18171+
18172+ AuTraceErr(err);
18173+ return err;
18174+}
18175+
18176+int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
18177+{
18178+ int err, e, update;
18179+ unsigned int flags;
18180+ umode_t mode;
18181+ aufs_bindex_t bindex, bbot;
18182+ unsigned char isdir;
18183+ struct au_hinode *p;
18184+ struct au_iinfo *iinfo;
18185+
18186+ err = au_ii_refresh(inode, &update);
18187+ if (unlikely(err))
18188+ goto out;
18189+
18190+ update = 0;
18191+ iinfo = au_ii(inode);
18192+ p = au_hinode(iinfo, iinfo->ii_btop);
18193+ mode = (inode->i_mode & S_IFMT);
18194+ isdir = S_ISDIR(mode);
18195+ flags = au_hi_flags(inode, isdir);
18196+ bbot = au_dbbot(dentry);
18197+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
18198+ struct inode *h_i, *h_inode;
18199+ struct dentry *h_d;
18200+
18201+ h_d = au_h_dptr(dentry, bindex);
18202+ if (!h_d || d_is_negative(h_d))
18203+ continue;
18204+
18205+ h_inode = d_inode(h_d);
18206+ AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
18207+ if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
18208+ h_i = au_h_iptr(inode, bindex);
18209+ if (h_i) {
18210+ if (h_i == h_inode)
18211+ continue;
18212+ err = -EIO;
18213+ break;
18214+ }
18215+ }
18216+ if (bindex < iinfo->ii_btop)
18217+ iinfo->ii_btop = bindex;
18218+ if (iinfo->ii_bbot < bindex)
18219+ iinfo->ii_bbot = bindex;
18220+ au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
18221+ update = 1;
18222+ }
18223+ au_update_ibrange(inode, /*do_put_zero*/0);
18224+ e = au_dy_irefresh(inode);
18225+ if (unlikely(e && !err))
18226+ err = e;
18227+ if (!err)
18228+ au_refresh_hinode_attr(inode, update && isdir);
18229+
18230+out:
18231+ AuTraceErr(err);
18232+ return err;
18233+}
18234+
18235+static int set_inode(struct inode *inode, struct dentry *dentry)
18236+{
18237+ int err;
18238+ unsigned int flags;
18239+ umode_t mode;
18240+ aufs_bindex_t bindex, btop, btail;
18241+ unsigned char isdir;
18242+ struct dentry *h_dentry;
18243+ struct inode *h_inode;
18244+ struct au_iinfo *iinfo;
18245+ const struct inode_operations *iop;
18246+
18247+ IiMustWriteLock(inode);
18248+
18249+ err = 0;
18250+ isdir = 0;
18251+ iop = au_sbi(inode->i_sb)->si_iop_array;
18252+ btop = au_dbtop(dentry);
18253+ h_dentry = au_h_dptr(dentry, btop);
18254+ h_inode = d_inode(h_dentry);
18255+ mode = h_inode->i_mode;
18256+ switch (mode & S_IFMT) {
18257+ case S_IFREG:
18258+ btail = au_dbtail(dentry);
18259+ inode->i_op = iop + AuIop_OTHER;
18260+ inode->i_fop = &aufs_file_fop;
18261+ err = au_dy_iaop(inode, btop, h_inode);
18262+ if (unlikely(err))
18263+ goto out;
18264+ break;
18265+ case S_IFDIR:
18266+ isdir = 1;
18267+ btail = au_dbtaildir(dentry);
18268+ inode->i_op = iop + AuIop_DIR;
18269+ inode->i_fop = &aufs_dir_fop;
18270+ break;
18271+ case S_IFLNK:
18272+ btail = au_dbtail(dentry);
18273+ inode->i_op = iop + AuIop_SYMLINK;
18274+ break;
18275+ case S_IFBLK:
18276+ case S_IFCHR:
18277+ case S_IFIFO:
18278+ case S_IFSOCK:
18279+ btail = au_dbtail(dentry);
18280+ inode->i_op = iop + AuIop_OTHER;
18281+ init_special_inode(inode, mode, h_inode->i_rdev);
18282+ break;
18283+ default:
18284+ AuIOErr("Unknown file type 0%o\n", mode);
18285+ err = -EIO;
18286+ goto out;
18287+ }
18288+
18289+ /* do not set hnotify for whiteouted dirs (SHWH mode) */
18290+ flags = au_hi_flags(inode, isdir);
18291+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
18292+ && au_ftest_hi(flags, HNOTIFY)
18293+ && dentry->d_name.len > AUFS_WH_PFX_LEN
18294+ && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
18295+ au_fclr_hi(flags, HNOTIFY);
18296+ iinfo = au_ii(inode);
18297+ iinfo->ii_btop = btop;
18298+ iinfo->ii_bbot = btail;
18299+ for (bindex = btop; bindex <= btail; bindex++) {
18300+ h_dentry = au_h_dptr(dentry, bindex);
18301+ if (h_dentry)
18302+ au_set_h_iptr(inode, bindex,
18303+ au_igrab(d_inode(h_dentry)), flags);
18304+ }
18305+ au_cpup_attr_all(inode, /*force*/1);
18306+ /*
18307+ * to force calling aufs_get_acl() every time,
18308+ * do not call cache_no_acl() for aufs inode.
18309+ */
18310+
18311+out:
18312+ return err;
18313+}
18314+
18315+/*
18316+ * successful returns with iinfo write_locked
18317+ * minus: errno
18318+ * zero: success, matched
18319+ * plus: no error, but unmatched
18320+ */
18321+static int reval_inode(struct inode *inode, struct dentry *dentry)
18322+{
18323+ int err;
18324+ unsigned int gen, igflags;
18325+ aufs_bindex_t bindex, bbot;
18326+ struct inode *h_inode, *h_dinode;
18327+ struct dentry *h_dentry;
18328+
18329+ /*
18330+ * before this function, if aufs got any iinfo lock, it must be only
18331+ * one, the parent dir.
18332+ * it can happen by UDBA and the obsoleted inode number.
18333+ */
18334+ err = -EIO;
18335+ if (unlikely(inode->i_ino == parent_ino(dentry)))
18336+ goto out;
18337+
18338+ err = 1;
18339+ ii_write_lock_new_child(inode);
18340+ h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
18341+ h_dinode = d_inode(h_dentry);
18342+ bbot = au_ibbot(inode);
18343+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
18344+ h_inode = au_h_iptr(inode, bindex);
18345+ if (!h_inode || h_inode != h_dinode)
18346+ continue;
18347+
18348+ err = 0;
18349+ gen = au_iigen(inode, &igflags);
18350+ if (gen == au_digen(dentry)
18351+ && !au_ig_ftest(igflags, HALF_REFRESHED))
18352+ break;
18353+
18354+ /* fully refresh inode using dentry */
18355+ err = au_refresh_hinode(inode, dentry);
18356+ if (!err)
18357+ au_update_iigen(inode, /*half*/0);
18358+ break;
18359+ }
18360+
18361+ if (unlikely(err))
18362+ ii_write_unlock(inode);
18363+out:
18364+ return err;
18365+}
18366+
18367+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18368+ unsigned int d_type, ino_t *ino)
18369+{
18370+ int err, idx;
18371+ const int isnondir = d_type != DT_DIR;
18372+
18373+ /* prevent hardlinked inode number from race condition */
18374+ if (isnondir) {
18375+ err = au_xinondir_enter(sb, bindex, h_ino, &idx);
18376+ if (unlikely(err))
18377+ goto out;
18378+ }
18379+
18380+ err = au_xino_read(sb, bindex, h_ino, ino);
18381+ if (unlikely(err))
18382+ goto out_xinondir;
18383+
18384+ if (!*ino) {
18385+ err = -EIO;
18386+ *ino = au_xino_new_ino(sb);
18387+ if (unlikely(!*ino))
18388+ goto out_xinondir;
18389+ err = au_xino_write(sb, bindex, h_ino, *ino);
18390+ if (unlikely(err))
18391+ goto out_xinondir;
18392+ }
18393+
18394+out_xinondir:
18395+ if (isnondir && idx >= 0)
18396+ au_xinondir_leave(sb, bindex, h_ino, idx);
18397+out:
18398+ return err;
18399+}
18400+
18401+/* successful returns with iinfo write_locked */
18402+/* todo: return with unlocked? */
18403+struct inode *au_new_inode(struct dentry *dentry, int must_new)
18404+{
18405+ struct inode *inode, *h_inode;
18406+ struct dentry *h_dentry;
18407+ struct super_block *sb;
18408+ ino_t h_ino, ino;
18409+ int err, idx, hlinked;
18410+ aufs_bindex_t btop;
18411+
18412+ sb = dentry->d_sb;
18413+ btop = au_dbtop(dentry);
18414+ h_dentry = au_h_dptr(dentry, btop);
18415+ h_inode = d_inode(h_dentry);
18416+ h_ino = h_inode->i_ino;
18417+ hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
18418+
18419+new_ino:
18420+ /*
18421+ * stop 'race'-ing between hardlinks under different
18422+ * parents.
18423+ */
18424+ if (hlinked) {
18425+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18426+ inode = ERR_PTR(err);
18427+ if (unlikely(err))
18428+ goto out;
18429+ }
18430+
18431+ err = au_xino_read(sb, btop, h_ino, &ino);
18432+ inode = ERR_PTR(err);
18433+ if (unlikely(err))
18434+ goto out_xinondir;
18435+
18436+ if (!ino) {
18437+ ino = au_xino_new_ino(sb);
18438+ if (unlikely(!ino)) {
18439+ inode = ERR_PTR(-EIO);
18440+ goto out_xinondir;
18441+ }
18442+ }
18443+
18444+ AuDbg("i%lu\n", (unsigned long)ino);
18445+ inode = au_iget_locked(sb, ino);
18446+ err = PTR_ERR(inode);
18447+ if (IS_ERR(inode))
18448+ goto out_xinondir;
18449+
18450+ AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
18451+ if (inode->i_state & I_NEW) {
18452+ ii_write_lock_new_child(inode);
18453+ err = set_inode(inode, dentry);
18454+ if (!err) {
18455+ unlock_new_inode(inode);
18456+ goto out_xinondir; /* success */
18457+ }
18458+
18459+ /*
18460+ * iget_failed() calls iput(), but we need to call
18461+ * ii_write_unlock() after iget_failed(). so dirty hack for
18462+ * i_count.
18463+ */
18464+ atomic_inc(&inode->i_count);
18465+ iget_failed(inode);
18466+ ii_write_unlock(inode);
18467+ au_xino_write(sb, btop, h_ino, /*ino*/0);
18468+ /* ignore this error */
18469+ goto out_iput;
18470+ } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
18471+ /*
18472+ * horrible race condition between lookup, readdir and copyup
18473+ * (or something).
18474+ */
18475+ if (hlinked && idx >= 0)
18476+ au_xinondir_leave(sb, btop, h_ino, idx);
18477+ err = reval_inode(inode, dentry);
18478+ if (unlikely(err < 0)) {
18479+ hlinked = 0;
18480+ goto out_iput;
18481+ }
18482+ if (!err)
18483+ goto out; /* success */
18484+ else if (hlinked && idx >= 0) {
18485+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18486+ if (unlikely(err)) {
18487+ iput(inode);
18488+ inode = ERR_PTR(err);
18489+ goto out;
18490+ }
18491+ }
18492+ }
18493+
18494+ if (unlikely(au_test_fs_unique_ino(h_inode)))
18495+ AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
18496+ " b%d, %s, %pd, hi%lu, i%lu.\n",
18497+ btop, au_sbtype(h_dentry->d_sb), dentry,
18498+ (unsigned long)h_ino, (unsigned long)ino);
18499+ ino = 0;
18500+ err = au_xino_write(sb, btop, h_ino, /*ino*/0);
18501+ if (!err) {
18502+ iput(inode);
18503+ if (hlinked && idx >= 0)
18504+ au_xinondir_leave(sb, btop, h_ino, idx);
18505+ goto new_ino;
18506+ }
18507+
18508+out_iput:
18509+ iput(inode);
18510+ inode = ERR_PTR(err);
18511+out_xinondir:
18512+ if (hlinked && idx >= 0)
18513+ au_xinondir_leave(sb, btop, h_ino, idx);
18514+out:
18515+ return inode;
18516+}
18517+
18518+/* ---------------------------------------------------------------------- */
18519+
18520+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18521+ struct inode *inode)
18522+{
18523+ int err;
18524+ struct inode *hi;
18525+
18526+ err = au_br_rdonly(au_sbr(sb, bindex));
18527+
18528+ /* pseudo-link after flushed may happen out of bounds */
18529+ if (!err
18530+ && inode
18531+ && au_ibtop(inode) <= bindex
18532+ && bindex <= au_ibbot(inode)) {
18533+ /*
18534+ * permission check is unnecessary since vfsub routine
18535+ * will be called later
18536+ */
18537+ hi = au_h_iptr(inode, bindex);
18538+ if (hi)
18539+ err = IS_IMMUTABLE(hi) ? -EROFS : 0;
18540+ }
18541+
18542+ return err;
18543+}
18544+
18545+int au_test_h_perm(struct inode *h_inode, int mask)
18546+{
18547+ if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
18548+ return 0;
18549+ return inode_permission(h_inode, mask);
18550+}
18551+
18552+int au_test_h_perm_sio(struct inode *h_inode, int mask)
18553+{
18554+ if (au_test_nfs(h_inode->i_sb)
18555+ && (mask & MAY_WRITE)
18556+ && S_ISDIR(h_inode->i_mode))
18557+ mask |= MAY_READ; /* force permission check */
18558+ return au_test_h_perm(h_inode, mask);
18559+}
18560diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
18561--- /usr/share/empty/fs/aufs/inode.h 1970-01-01 01:00:00.000000000 +0100
18562+++ linux/fs/aufs/inode.h 2021-02-22 22:51:59.061411920 +0100
18563@@ -0,0 +1,698 @@
18564+/* SPDX-License-Identifier: GPL-2.0 */
18565+/*
18566+ * Copyright (C) 2005-2020 Junjiro R. Okajima
18567+ *
18568+ * This program, aufs is free software; you can redistribute it and/or modify
18569+ * it under the terms of the GNU General Public License as published by
18570+ * the Free Software Foundation; either version 2 of the License, or
18571+ * (at your option) any later version.
18572+ *
18573+ * This program is distributed in the hope that it will be useful,
18574+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18575+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18576+ * GNU General Public License for more details.
18577+ *
18578+ * You should have received a copy of the GNU General Public License
18579+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
18580+ */
18581+
18582+/*
18583+ * inode operations
18584+ */
18585+
18586+#ifndef __AUFS_INODE_H__
18587+#define __AUFS_INODE_H__
18588+
18589+#ifdef __KERNEL__
18590+
18591+#include <linux/fsnotify.h>
18592+#include "rwsem.h"
18593+
18594+struct vfsmount;
18595+
18596+struct au_hnotify {
18597+#ifdef CONFIG_AUFS_HNOTIFY
18598+#ifdef CONFIG_AUFS_HFSNOTIFY
18599+ /* never use fsnotify_add_vfsmount_mark() */
18600+ struct fsnotify_mark hn_mark;
18601+#endif
18602+ struct inode *hn_aufs_inode; /* no get/put */
18603+ struct rcu_head rcu;
18604+#endif
18605+} ____cacheline_aligned_in_smp;
18606+
18607+struct au_hinode {
18608+ struct inode *hi_inode;
18609+ aufs_bindex_t hi_id;
18610+#ifdef CONFIG_AUFS_HNOTIFY
18611+ struct au_hnotify *hi_notify;
18612+#endif
18613+
18614+ /* reference to the copied-up whiteout with get/put */
18615+ struct dentry *hi_whdentry;
18616+};
18617+
18618+/* ig_flags */
18619+#define AuIG_HALF_REFRESHED 1
18620+#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
18621+#define au_ig_fset(flags, name) \
18622+ do { (flags) |= AuIG_##name; } while (0)
18623+#define au_ig_fclr(flags, name) \
18624+ do { (flags) &= ~AuIG_##name; } while (0)
18625+
18626+struct au_iigen {
18627+ spinlock_t ig_spin;
18628+ __u32 ig_generation, ig_flags;
18629+};
18630+
18631+struct au_vdir;
18632+struct au_iinfo {
18633+ struct au_iigen ii_generation;
18634+ struct super_block *ii_hsb1; /* no get/put */
18635+
18636+ struct au_rwsem ii_rwsem;
18637+ aufs_bindex_t ii_btop, ii_bbot;
18638+ __u32 ii_higen;
18639+ struct au_hinode *ii_hinode;
18640+ struct au_vdir *ii_vdir;
18641+};
18642+
18643+struct au_icntnr {
18644+ struct au_iinfo iinfo;
18645+ struct inode vfs_inode;
18646+ struct hlist_bl_node plink;
18647+ struct rcu_head rcu;
18648+} ____cacheline_aligned_in_smp;
18649+
18650+/* au_pin flags */
18651+#define AuPin_DI_LOCKED 1
18652+#define AuPin_MNT_WRITE (1 << 1)
18653+#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
18654+#define au_fset_pin(flags, name) \
18655+ do { (flags) |= AuPin_##name; } while (0)
18656+#define au_fclr_pin(flags, name) \
18657+ do { (flags) &= ~AuPin_##name; } while (0)
18658+
18659+struct au_pin {
18660+ /* input */
18661+ struct dentry *dentry;
18662+ unsigned int udba;
18663+ unsigned char lsc_di, lsc_hi, flags;
18664+ aufs_bindex_t bindex;
18665+
18666+ /* output */
18667+ struct dentry *parent;
18668+ struct au_hinode *hdir;
18669+ struct vfsmount *h_mnt;
18670+
18671+ /* temporary unlock/relock for copyup */
18672+ struct dentry *h_dentry, *h_parent;
18673+ struct au_branch *br;
18674+ struct task_struct *task;
18675+};
18676+
18677+void au_pin_hdir_unlock(struct au_pin *p);
18678+int au_pin_hdir_lock(struct au_pin *p);
18679+int au_pin_hdir_relock(struct au_pin *p);
18680+void au_pin_hdir_acquire_nest(struct au_pin *p);
18681+void au_pin_hdir_release(struct au_pin *p);
18682+
18683+/* ---------------------------------------------------------------------- */
18684+
18685+static inline struct au_iinfo *au_ii(struct inode *inode)
18686+{
18687+ BUG_ON(is_bad_inode(inode));
18688+ return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
18689+}
18690+
18691+/* ---------------------------------------------------------------------- */
18692+
18693+/* inode.c */
18694+struct inode *au_igrab(struct inode *inode);
18695+void au_refresh_iop(struct inode *inode, int force_getattr);
18696+int au_refresh_hinode_self(struct inode *inode);
18697+int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
18698+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18699+ unsigned int d_type, ino_t *ino);
18700+struct inode *au_new_inode(struct dentry *dentry, int must_new);
18701+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18702+ struct inode *inode);
18703+int au_test_h_perm(struct inode *h_inode, int mask);
18704+int au_test_h_perm_sio(struct inode *h_inode, int mask);
18705+
18706+static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
18707+ ino_t h_ino, unsigned int d_type, ino_t *ino)
18708+{
18709+#ifdef CONFIG_AUFS_SHWH
18710+ return au_ino(sb, bindex, h_ino, d_type, ino);
18711+#else
18712+ return 0;
18713+#endif
18714+}
18715+
18716+/* i_op.c */
18717+enum {
18718+ AuIop_SYMLINK,
18719+ AuIop_DIR,
18720+ AuIop_OTHER,
18721+ AuIop_Last
18722+};
18723+extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
18724+ aufs_iop_nogetattr[AuIop_Last];
18725+
18726+/* au_wr_dir flags */
18727+#define AuWrDir_ADD_ENTRY 1
18728+#define AuWrDir_ISDIR (1 << 1)
18729+#define AuWrDir_TMPFILE (1 << 2)
18730+#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
18731+#define au_fset_wrdir(flags, name) \
18732+ do { (flags) |= AuWrDir_##name; } while (0)
18733+#define au_fclr_wrdir(flags, name) \
18734+ do { (flags) &= ~AuWrDir_##name; } while (0)
18735+
18736+struct au_wr_dir_args {
18737+ aufs_bindex_t force_btgt;
18738+ unsigned char flags;
18739+};
18740+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18741+ struct au_wr_dir_args *args);
18742+
18743+struct dentry *au_pinned_h_parent(struct au_pin *pin);
18744+void au_pin_init(struct au_pin *pin, struct dentry *dentry,
18745+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18746+ unsigned int udba, unsigned char flags);
18747+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18748+ unsigned int udba, unsigned char flags) __must_check;
18749+int au_do_pin(struct au_pin *pin) __must_check;
18750+void au_unpin(struct au_pin *pin);
18751+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
18752+
18753+#define AuIcpup_DID_CPUP 1
18754+#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
18755+#define au_fset_icpup(flags, name) \
18756+ do { (flags) |= AuIcpup_##name; } while (0)
18757+#define au_fclr_icpup(flags, name) \
18758+ do { (flags) &= ~AuIcpup_##name; } while (0)
18759+
18760+struct au_icpup_args {
18761+ unsigned char flags;
18762+ unsigned char pin_flags;
18763+ aufs_bindex_t btgt;
18764+ unsigned int udba;
18765+ struct au_pin pin;
18766+ struct path h_path;
18767+ struct inode *h_inode;
18768+};
18769+
18770+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
18771+ struct au_icpup_args *a);
18772+
18773+int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
18774+ struct path *h_path, int locked);
18775+
18776+/* i_op_add.c */
18777+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
18778+ struct dentry *h_parent, int isdir);
18779+int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
18780+ dev_t dev);
18781+int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
18782+int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
18783+ bool want_excl);
18784+struct vfsub_aopen_args;
18785+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
18786+ struct vfsub_aopen_args *args);
18787+int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
18788+int aufs_link(struct dentry *src_dentry, struct inode *dir,
18789+ struct dentry *dentry);
18790+int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
18791+
18792+/* i_op_del.c */
18793+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
18794+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
18795+ struct dentry *h_parent, int isdir);
18796+int aufs_unlink(struct inode *dir, struct dentry *dentry);
18797+int aufs_rmdir(struct inode *dir, struct dentry *dentry);
18798+
18799+/* i_op_ren.c */
18800+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
18801+int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
18802+ struct inode *dir, struct dentry *dentry,
18803+ unsigned int flags);
18804+
18805+/* iinfo.c */
18806+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
18807+void au_hiput(struct au_hinode *hinode);
18808+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
18809+ struct dentry *h_wh);
18810+unsigned int au_hi_flags(struct inode *inode, int isdir);
18811+
18812+/* hinode flags */
18813+#define AuHi_XINO 1
18814+#define AuHi_HNOTIFY (1 << 1)
18815+#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
18816+#define au_fset_hi(flags, name) \
18817+ do { (flags) |= AuHi_##name; } while (0)
18818+#define au_fclr_hi(flags, name) \
18819+ do { (flags) &= ~AuHi_##name; } while (0)
18820+
18821+#ifndef CONFIG_AUFS_HNOTIFY
18822+#undef AuHi_HNOTIFY
18823+#define AuHi_HNOTIFY 0
18824+#endif
18825+
18826+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
18827+ struct inode *h_inode, unsigned int flags);
18828+
18829+void au_update_iigen(struct inode *inode, int half);
18830+void au_update_ibrange(struct inode *inode, int do_put_zero);
18831+
18832+void au_icntnr_init_once(void *_c);
18833+void au_hinode_init(struct au_hinode *hinode);
18834+int au_iinfo_init(struct inode *inode);
18835+void au_iinfo_fin(struct inode *inode);
18836+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
18837+
18838+#ifdef CONFIG_PROC_FS
18839+/* plink.c */
18840+int au_plink_maint(struct super_block *sb, int flags);
18841+struct au_sbinfo;
18842+void au_plink_maint_leave(struct au_sbinfo *sbinfo);
18843+int au_plink_maint_enter(struct super_block *sb);
18844+#ifdef CONFIG_AUFS_DEBUG
18845+void au_plink_list(struct super_block *sb);
18846+#else
18847+AuStubVoid(au_plink_list, struct super_block *sb)
18848+#endif
18849+int au_plink_test(struct inode *inode);
18850+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
18851+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
18852+ struct dentry *h_dentry);
18853+void au_plink_put(struct super_block *sb, int verbose);
18854+void au_plink_clean(struct super_block *sb, int verbose);
18855+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
18856+#else
18857+AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
18858+AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
18859+AuStubInt0(au_plink_maint_enter, struct super_block *sb);
18860+AuStubVoid(au_plink_list, struct super_block *sb);
18861+AuStubInt0(au_plink_test, struct inode *inode);
18862+AuStub(struct dentry *, au_plink_lkup, return NULL,
18863+ struct inode *inode, aufs_bindex_t bindex);
18864+AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
18865+ struct dentry *h_dentry);
18866+AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
18867+AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
18868+AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
18869+#endif /* CONFIG_PROC_FS */
18870+
18871+#ifdef CONFIG_AUFS_XATTR
18872+/* xattr.c */
18873+int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
18874+ unsigned int verbose);
18875+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
18876+void au_xattr_init(struct super_block *sb);
18877+#else
18878+AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
18879+ int ignore_flags, unsigned int verbose);
18880+AuStubVoid(au_xattr_init, struct super_block *sb);
18881+#endif
18882+
18883+#ifdef CONFIG_FS_POSIX_ACL
18884+struct posix_acl *aufs_get_acl(struct inode *inode, int type);
18885+int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
18886+#endif
18887+
18888+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
18889+enum {
18890+ AU_XATTR_SET,
18891+ AU_ACL_SET
18892+};
18893+
18894+struct au_sxattr {
18895+ int type;
18896+ union {
18897+ struct {
18898+ const char *name;
18899+ const void *value;
18900+ size_t size;
18901+ int flags;
18902+ } set;
18903+ struct {
18904+ struct posix_acl *acl;
18905+ int type;
18906+ } acl_set;
18907+ } u;
18908+};
18909+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
18910+ struct au_sxattr *arg);
18911+#endif
18912+
18913+/* ---------------------------------------------------------------------- */
18914+
18915+/* lock subclass for iinfo */
18916+enum {
18917+ AuLsc_II_CHILD, /* child first */
18918+ AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
18919+ AuLsc_II_CHILD3, /* copyup dirs */
18920+ AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
18921+ AuLsc_II_PARENT2,
18922+ AuLsc_II_PARENT3, /* copyup dirs */
18923+ AuLsc_II_NEW_CHILD
18924+};
18925+
18926+/*
18927+ * ii_read_lock_child, ii_write_lock_child,
18928+ * ii_read_lock_child2, ii_write_lock_child2,
18929+ * ii_read_lock_child3, ii_write_lock_child3,
18930+ * ii_read_lock_parent, ii_write_lock_parent,
18931+ * ii_read_lock_parent2, ii_write_lock_parent2,
18932+ * ii_read_lock_parent3, ii_write_lock_parent3,
18933+ * ii_read_lock_new_child, ii_write_lock_new_child,
18934+ */
18935+#define AuReadLockFunc(name, lsc) \
18936+static inline void ii_read_lock_##name(struct inode *i) \
18937+{ \
18938+ au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
18939+}
18940+
18941+#define AuWriteLockFunc(name, lsc) \
18942+static inline void ii_write_lock_##name(struct inode *i) \
18943+{ \
18944+ au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
18945+}
18946+
18947+#define AuRWLockFuncs(name, lsc) \
18948+ AuReadLockFunc(name, lsc) \
18949+ AuWriteLockFunc(name, lsc)
18950+
18951+AuRWLockFuncs(child, CHILD);
18952+AuRWLockFuncs(child2, CHILD2);
18953+AuRWLockFuncs(child3, CHILD3);
18954+AuRWLockFuncs(parent, PARENT);
18955+AuRWLockFuncs(parent2, PARENT2);
18956+AuRWLockFuncs(parent3, PARENT3);
18957+AuRWLockFuncs(new_child, NEW_CHILD);
18958+
18959+#undef AuReadLockFunc
18960+#undef AuWriteLockFunc
18961+#undef AuRWLockFuncs
18962+
18963+#define ii_read_unlock(i) au_rw_read_unlock(&au_ii(i)->ii_rwsem)
18964+#define ii_write_unlock(i) au_rw_write_unlock(&au_ii(i)->ii_rwsem)
18965+#define ii_downgrade_lock(i) au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
18966+
18967+#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
18968+#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
18969+#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
18970+
18971+/* ---------------------------------------------------------------------- */
18972+
18973+static inline void au_icntnr_init(struct au_icntnr *c)
18974+{
18975+#ifdef CONFIG_AUFS_DEBUG
18976+ c->vfs_inode.i_mode = 0;
18977+#endif
18978+}
18979+
18980+static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
18981+{
18982+ unsigned int gen;
18983+ struct au_iinfo *iinfo;
18984+ struct au_iigen *iigen;
18985+
18986+ iinfo = au_ii(inode);
18987+ iigen = &iinfo->ii_generation;
18988+ spin_lock(&iigen->ig_spin);
18989+ if (igflags)
18990+ *igflags = iigen->ig_flags;
18991+ gen = iigen->ig_generation;
18992+ spin_unlock(&iigen->ig_spin);
18993+
18994+ return gen;
18995+}
18996+
18997+/* tiny test for inode number */
18998+/* tmpfs generation is too rough */
18999+static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
19000+{
19001+ struct au_iinfo *iinfo;
19002+
19003+ iinfo = au_ii(inode);
19004+ AuRwMustAnyLock(&iinfo->ii_rwsem);
19005+ return !(iinfo->ii_hsb1 == h_inode->i_sb
19006+ && iinfo->ii_higen == h_inode->i_generation);
19007+}
19008+
19009+static inline void au_iigen_dec(struct inode *inode)
19010+{
19011+ struct au_iinfo *iinfo;
19012+ struct au_iigen *iigen;
19013+
19014+ iinfo = au_ii(inode);
19015+ iigen = &iinfo->ii_generation;
19016+ spin_lock(&iigen->ig_spin);
19017+ iigen->ig_generation--;
19018+ spin_unlock(&iigen->ig_spin);
19019+}
19020+
19021+static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
19022+{
19023+ int err;
19024+
19025+ err = 0;
19026+ if (unlikely(inode && au_iigen(inode, NULL) != sigen))
19027+ err = -EIO;
19028+
19029+ return err;
19030+}
19031+
19032+/* ---------------------------------------------------------------------- */
19033+
19034+static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
19035+ aufs_bindex_t bindex)
19036+{
19037+ return iinfo->ii_hinode + bindex;
19038+}
19039+
19040+static inline int au_is_bad_inode(struct inode *inode)
19041+{
19042+ return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
19043+}
19044+
19045+static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
19046+ aufs_bindex_t bindex)
19047+{
19048+ IiMustAnyLock(inode);
19049+ return au_hinode(au_ii(inode), bindex)->hi_id;
19050+}
19051+
19052+static inline aufs_bindex_t au_ibtop(struct inode *inode)
19053+{
19054+ IiMustAnyLock(inode);
19055+ return au_ii(inode)->ii_btop;
19056+}
19057+
19058+static inline aufs_bindex_t au_ibbot(struct inode *inode)
19059+{
19060+ IiMustAnyLock(inode);
19061+ return au_ii(inode)->ii_bbot;
19062+}
19063+
19064+static inline struct au_vdir *au_ivdir(struct inode *inode)
19065+{
19066+ IiMustAnyLock(inode);
19067+ return au_ii(inode)->ii_vdir;
19068+}
19069+
19070+static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
19071+{
19072+ IiMustAnyLock(inode);
19073+ return au_hinode(au_ii(inode), bindex)->hi_whdentry;
19074+}
19075+
19076+static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
19077+{
19078+ IiMustWriteLock(inode);
19079+ au_ii(inode)->ii_btop = bindex;
19080+}
19081+
19082+static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
19083+{
19084+ IiMustWriteLock(inode);
19085+ au_ii(inode)->ii_bbot = bindex;
19086+}
19087+
19088+static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
19089+{
19090+ IiMustWriteLock(inode);
19091+ au_ii(inode)->ii_vdir = vdir;
19092+}
19093+
19094+static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
19095+{
19096+ IiMustAnyLock(inode);
19097+ return au_hinode(au_ii(inode), bindex);
19098+}
19099+
19100+/* ---------------------------------------------------------------------- */
19101+
19102+static inline struct dentry *au_pinned_parent(struct au_pin *pin)
19103+{
19104+ if (pin)
19105+ return pin->parent;
19106+ return NULL;
19107+}
19108+
19109+static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
19110+{
19111+ if (pin && pin->hdir)
19112+ return pin->hdir->hi_inode;
19113+ return NULL;
19114+}
19115+
19116+static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
19117+{
19118+ if (pin)
19119+ return pin->hdir;
19120+ return NULL;
19121+}
19122+
19123+static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
19124+{
19125+ if (pin)
19126+ pin->dentry = dentry;
19127+}
19128+
19129+static inline void au_pin_set_parent_lflag(struct au_pin *pin,
19130+ unsigned char lflag)
19131+{
19132+ if (pin) {
19133+ if (lflag)
19134+ au_fset_pin(pin->flags, DI_LOCKED);
19135+ else
19136+ au_fclr_pin(pin->flags, DI_LOCKED);
19137+ }
19138+}
19139+
19140+#if 0 /* reserved */
19141+static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
19142+{
19143+ if (pin) {
19144+ dput(pin->parent);
19145+ pin->parent = dget(parent);
19146+ }
19147+}
19148+#endif
19149+
19150+/* ---------------------------------------------------------------------- */
19151+
19152+struct au_branch;
19153+#ifdef CONFIG_AUFS_HNOTIFY
19154+struct au_hnotify_op {
19155+ void (*ctl)(struct au_hinode *hinode, int do_set);
19156+ int (*alloc)(struct au_hinode *hinode);
19157+
19158+ /*
19159+ * if it returns true, the caller should free hinode->hi_notify,
19160+ * otherwise ->free() frees it.
19161+ */
19162+ int (*free)(struct au_hinode *hinode,
19163+ struct au_hnotify *hn) __must_check;
19164+
19165+ void (*fin)(void);
19166+ int (*init)(void);
19167+
19168+ int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
19169+ void (*fin_br)(struct au_branch *br);
19170+ int (*init_br)(struct au_branch *br, int perm);
19171+};
19172+
19173+/* hnotify.c */
19174+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
19175+void au_hn_free(struct au_hinode *hinode);
19176+void au_hn_ctl(struct au_hinode *hinode, int do_set);
19177+void au_hn_reset(struct inode *inode, unsigned int flags);
19178+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
19179+ const struct qstr *h_child_qstr, struct inode *h_child_inode);
19180+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
19181+int au_hnotify_init_br(struct au_branch *br, int perm);
19182+void au_hnotify_fin_br(struct au_branch *br);
19183+int __init au_hnotify_init(void);
19184+void au_hnotify_fin(void);
19185+
19186+/* hfsnotify.c */
19187+extern const struct au_hnotify_op au_hnotify_op;
19188+
19189+static inline
19190+void au_hn_init(struct au_hinode *hinode)
19191+{
19192+ hinode->hi_notify = NULL;
19193+}
19194+
19195+static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
19196+{
19197+ return hinode->hi_notify;
19198+}
19199+
19200+#else
19201+AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
19202+ struct au_hinode *hinode __maybe_unused,
19203+ struct inode *inode __maybe_unused)
19204+AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
19205+AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
19206+AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
19207+ int do_set __maybe_unused)
19208+AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
19209+ unsigned int flags __maybe_unused)
19210+AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
19211+ struct au_branch *br __maybe_unused,
19212+ int perm __maybe_unused)
19213+AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
19214+ int perm __maybe_unused)
19215+AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
19216+AuStubInt0(__init au_hnotify_init, void)
19217+AuStubVoid(au_hnotify_fin, void)
19218+AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
19219+#endif /* CONFIG_AUFS_HNOTIFY */
19220+
19221+static inline void au_hn_suspend(struct au_hinode *hdir)
19222+{
19223+ au_hn_ctl(hdir, /*do_set*/0);
19224+}
19225+
19226+static inline void au_hn_resume(struct au_hinode *hdir)
19227+{
19228+ au_hn_ctl(hdir, /*do_set*/1);
19229+}
19230+
19231+static inline void au_hn_inode_lock(struct au_hinode *hdir)
19232+{
19233+ inode_lock(hdir->hi_inode);
19234+ au_hn_suspend(hdir);
19235+}
19236+
19237+static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
19238+ unsigned int sc __maybe_unused)
19239+{
19240+ inode_lock_nested(hdir->hi_inode, sc);
19241+ au_hn_suspend(hdir);
19242+}
19243+
19244+#if 0 /* unused */
19245+#include "vfsub.h"
19246+static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
19247+ unsigned int sc)
19248+{
19249+ inode_lock_shared_nested(hdir->hi_inode, sc);
19250+ au_hn_suspend(hdir);
19251+}
19252+#endif
19253+
19254+static inline void au_hn_inode_unlock(struct au_hinode *hdir)
19255+{
19256+ au_hn_resume(hdir);
19257+ inode_unlock(hdir->hi_inode);
19258+}
19259+
19260+#endif /* __KERNEL__ */
19261+#endif /* __AUFS_INODE_H__ */
19262diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
19263--- /usr/share/empty/fs/aufs/ioctl.c 1970-01-01 01:00:00.000000000 +0100
19264+++ linux/fs/aufs/ioctl.c 2021-02-22 22:51:59.061411920 +0100
19265@@ -0,0 +1,220 @@
19266+// SPDX-License-Identifier: GPL-2.0
19267+/*
19268+ * Copyright (C) 2005-2020 Junjiro R. Okajima
19269+ *
19270+ * This program, aufs is free software; you can redistribute it and/or modify
19271+ * it under the terms of the GNU General Public License as published by
19272+ * the Free Software Foundation; either version 2 of the License, or
19273+ * (at your option) any later version.
19274+ *
19275+ * This program is distributed in the hope that it will be useful,
19276+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19277+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19278+ * GNU General Public License for more details.
19279+ *
19280+ * You should have received a copy of the GNU General Public License
19281+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
19282+ */
19283+
19284+/*
19285+ * ioctl
19286+ * plink-management and readdir in userspace.
19287+ * assist the pathconf(3) wrapper library.
19288+ * move-down
19289+ * File-based Hierarchical Storage Management.
19290+ */
19291+
19292+#include <linux/compat.h>
19293+#include <linux/file.h>
19294+#include "aufs.h"
19295+
19296+static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
19297+{
19298+ int err, fd;
19299+ aufs_bindex_t wbi, bindex, bbot;
19300+ struct file *h_file;
19301+ struct super_block *sb;
19302+ struct dentry *root;
19303+ struct au_branch *br;
19304+ struct aufs_wbr_fd wbrfd = {
19305+ .oflags = au_dir_roflags,
19306+ .brid = -1
19307+ };
19308+ const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
19309+ | O_NOATIME | O_CLOEXEC;
19310+
19311+ AuDebugOn(wbrfd.oflags & ~valid);
19312+
19313+ if (arg) {
19314+ err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
19315+ if (unlikely(err)) {
19316+ err = -EFAULT;
19317+ goto out;
19318+ }
19319+
19320+ err = -EINVAL;
19321+ AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
19322+ wbrfd.oflags |= au_dir_roflags;
19323+ AuDbg("0%o\n", wbrfd.oflags);
19324+ if (unlikely(wbrfd.oflags & ~valid))
19325+ goto out;
19326+ }
19327+
19328+ fd = get_unused_fd_flags(0);
19329+ err = fd;
19330+ if (unlikely(fd < 0))
19331+ goto out;
19332+
19333+ h_file = ERR_PTR(-EINVAL);
19334+ wbi = 0;
19335+ br = NULL;
19336+ sb = path->dentry->d_sb;
19337+ root = sb->s_root;
19338+ aufs_read_lock(root, AuLock_IR);
19339+ bbot = au_sbbot(sb);
19340+ if (wbrfd.brid >= 0) {
19341+ wbi = au_br_index(sb, wbrfd.brid);
19342+ if (unlikely(wbi < 0 || wbi > bbot))
19343+ goto out_unlock;
19344+ }
19345+
19346+ h_file = ERR_PTR(-ENOENT);
19347+ br = au_sbr(sb, wbi);
19348+ if (!au_br_writable(br->br_perm)) {
19349+ if (arg)
19350+ goto out_unlock;
19351+
19352+ bindex = wbi + 1;
19353+ wbi = -1;
19354+ for (; bindex <= bbot; bindex++) {
19355+ br = au_sbr(sb, bindex);
19356+ if (au_br_writable(br->br_perm)) {
19357+ wbi = bindex;
19358+ br = au_sbr(sb, wbi);
19359+ break;
19360+ }
19361+ }
19362+ }
19363+ AuDbg("wbi %d\n", wbi);
19364+ if (wbi >= 0)
19365+ h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
19366+ /*force_wr*/0);
19367+
19368+out_unlock:
19369+ aufs_read_unlock(root, AuLock_IR);
19370+ err = PTR_ERR(h_file);
19371+ if (IS_ERR(h_file))
19372+ goto out_fd;
19373+
19374+ au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
19375+ fd_install(fd, h_file);
19376+ err = fd;
19377+ goto out; /* success */
19378+
19379+out_fd:
19380+ put_unused_fd(fd);
19381+out:
19382+ AuTraceErr(err);
19383+ return err;
19384+}
19385+
19386+/* ---------------------------------------------------------------------- */
19387+
19388+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
19389+{
19390+ long err;
19391+ struct dentry *dentry;
19392+
19393+ switch (cmd) {
19394+ case AUFS_CTL_RDU:
19395+ case AUFS_CTL_RDU_INO:
19396+ err = au_rdu_ioctl(file, cmd, arg);
19397+ break;
19398+
19399+ case AUFS_CTL_WBR_FD:
19400+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
19401+ break;
19402+
19403+ case AUFS_CTL_IBUSY:
19404+ err = au_ibusy_ioctl(file, arg);
19405+ break;
19406+
19407+ case AUFS_CTL_BRINFO:
19408+ err = au_brinfo_ioctl(file, arg);
19409+ break;
19410+
19411+ case AUFS_CTL_FHSM_FD:
19412+ dentry = file->f_path.dentry;
19413+ if (IS_ROOT(dentry))
19414+ err = au_fhsm_fd(dentry->d_sb, arg);
19415+ else
19416+ err = -ENOTTY;
19417+ break;
19418+
19419+ default:
19420+ /* do not call the lower */
19421+ AuDbg("0x%x\n", cmd);
19422+ err = -ENOTTY;
19423+ }
19424+
19425+ AuTraceErr(err);
19426+ return err;
19427+}
19428+
19429+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
19430+{
19431+ long err;
19432+
19433+ switch (cmd) {
19434+ case AUFS_CTL_MVDOWN:
19435+ err = au_mvdown(file->f_path.dentry, (void __user *)arg);
19436+ break;
19437+
19438+ case AUFS_CTL_WBR_FD:
19439+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
19440+ break;
19441+
19442+ default:
19443+ /* do not call the lower */
19444+ AuDbg("0x%x\n", cmd);
19445+ err = -ENOTTY;
19446+ }
19447+
19448+ AuTraceErr(err);
19449+ return err;
19450+}
19451+
19452+#ifdef CONFIG_COMPAT
19453+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
19454+ unsigned long arg)
19455+{
19456+ long err;
19457+
19458+ switch (cmd) {
19459+ case AUFS_CTL_RDU:
19460+ case AUFS_CTL_RDU_INO:
19461+ err = au_rdu_compat_ioctl(file, cmd, arg);
19462+ break;
19463+
19464+ case AUFS_CTL_IBUSY:
19465+ err = au_ibusy_compat_ioctl(file, arg);
19466+ break;
19467+
19468+ case AUFS_CTL_BRINFO:
19469+ err = au_brinfo_compat_ioctl(file, arg);
19470+ break;
19471+
19472+ default:
19473+ err = aufs_ioctl_dir(file, cmd, arg);
19474+ }
19475+
19476+ AuTraceErr(err);
19477+ return err;
19478+}
19479+
19480+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
19481+ unsigned long arg)
19482+{
19483+ return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
19484+}
19485+#endif
19486diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
19487--- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
19488+++ linux/fs/aufs/i_op_add.c 2021-02-22 22:51:59.058078584 +0100
19489@@ -0,0 +1,936 @@
19490+// SPDX-License-Identifier: GPL-2.0
19491+/*
19492+ * Copyright (C) 2005-2020 Junjiro R. Okajima
19493+ *
19494+ * This program, aufs is free software; you can redistribute it and/or modify
19495+ * it under the terms of the GNU General Public License as published by
19496+ * the Free Software Foundation; either version 2 of the License, or
19497+ * (at your option) any later version.
19498+ *
19499+ * This program is distributed in the hope that it will be useful,
19500+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19501+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19502+ * GNU General Public License for more details.
19503+ *
19504+ * You should have received a copy of the GNU General Public License
19505+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
19506+ */
19507+
19508+/*
19509+ * inode operations (add entry)
19510+ */
19511+
19512+#include <linux/iversion.h>
19513+#include "aufs.h"
19514+
19515+/*
19516+ * final procedure of adding a new entry, except link(2).
19517+ * remove whiteout, instantiate, copyup the parent dir's times and size
19518+ * and update version.
19519+ * if it failed, re-create the removed whiteout.
19520+ */
19521+static int epilog(struct inode *dir, aufs_bindex_t bindex,
19522+ struct dentry *wh_dentry, struct dentry *dentry)
19523+{
19524+ int err, rerr;
19525+ aufs_bindex_t bwh;
19526+ struct path h_path;
19527+ struct super_block *sb;
19528+ struct inode *inode, *h_dir;
19529+ struct dentry *wh;
19530+
19531+ bwh = -1;
19532+ sb = dir->i_sb;
19533+ if (wh_dentry) {
19534+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
19535+ IMustLock(h_dir);
19536+ AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
19537+ bwh = au_dbwh(dentry);
19538+ h_path.dentry = wh_dentry;
19539+ h_path.mnt = au_sbr_mnt(sb, bindex);
19540+ err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
19541+ dentry);
19542+ if (unlikely(err))
19543+ goto out;
19544+ }
19545+
19546+ inode = au_new_inode(dentry, /*must_new*/1);
19547+ if (!IS_ERR(inode)) {
19548+ d_instantiate(dentry, inode);
19549+ dir = d_inode(dentry->d_parent); /* dir inode is locked */
19550+ IMustLock(dir);
19551+ au_dir_ts(dir, bindex);
19552+ inode_inc_iversion(dir);
19553+ au_fhsm_wrote(sb, bindex, /*force*/0);
19554+ return 0; /* success */
19555+ }
19556+
19557+ err = PTR_ERR(inode);
19558+ if (!wh_dentry)
19559+ goto out;
19560+
19561+ /* revert */
19562+ /* dir inode is locked */
19563+ wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
19564+ rerr = PTR_ERR(wh);
19565+ if (IS_ERR(wh)) {
19566+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
19567+ dentry, err, rerr);
19568+ err = -EIO;
19569+ } else
19570+ dput(wh);
19571+
19572+out:
19573+ return err;
19574+}
19575+
19576+static int au_d_may_add(struct dentry *dentry)
19577+{
19578+ int err;
19579+
19580+ err = 0;
19581+ if (unlikely(d_unhashed(dentry)))
19582+ err = -ENOENT;
19583+ if (unlikely(d_really_is_positive(dentry)))
19584+ err = -EEXIST;
19585+ return err;
19586+}
19587+
19588+/*
19589+ * simple tests for the adding inode operations.
19590+ * following the checks in vfs, plus the parent-child relationship.
19591+ */
19592+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19593+ struct dentry *h_parent, int isdir)
19594+{
19595+ int err;
19596+ umode_t h_mode;
19597+ struct dentry *h_dentry;
19598+ struct inode *h_inode;
19599+
19600+ err = -ENAMETOOLONG;
19601+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19602+ goto out;
19603+
19604+ h_dentry = au_h_dptr(dentry, bindex);
19605+ if (d_really_is_negative(dentry)) {
19606+ err = -EEXIST;
19607+ if (unlikely(d_is_positive(h_dentry)))
19608+ goto out;
19609+ } else {
19610+ /* rename(2) case */
19611+ err = -EIO;
19612+ if (unlikely(d_is_negative(h_dentry)))
19613+ goto out;
19614+ h_inode = d_inode(h_dentry);
19615+ if (unlikely(!h_inode->i_nlink))
19616+ goto out;
19617+
19618+ h_mode = h_inode->i_mode;
19619+ if (!isdir) {
19620+ err = -EISDIR;
19621+ if (unlikely(S_ISDIR(h_mode)))
19622+ goto out;
19623+ } else if (unlikely(!S_ISDIR(h_mode))) {
19624+ err = -ENOTDIR;
19625+ goto out;
19626+ }
19627+ }
19628+
19629+ err = 0;
19630+ /* expected parent dir is locked */
19631+ if (unlikely(h_parent != h_dentry->d_parent))
19632+ err = -EIO;
19633+
19634+out:
19635+ AuTraceErr(err);
19636+ return err;
19637+}
19638+
19639+/*
19640+ * initial procedure of adding a new entry.
19641+ * prepare writable branch and the parent dir, lock it,
19642+ * and lookup whiteout for the new entry.
19643+ */
19644+static struct dentry*
19645+lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
19646+ struct dentry *src_dentry, struct au_pin *pin,
19647+ struct au_wr_dir_args *wr_dir_args)
19648+{
19649+ struct dentry *wh_dentry, *h_parent;
19650+ struct super_block *sb;
19651+ struct au_branch *br;
19652+ int err;
19653+ unsigned int udba;
19654+ aufs_bindex_t bcpup;
19655+
19656+ AuDbg("%pd\n", dentry);
19657+
19658+ err = au_wr_dir(dentry, src_dentry, wr_dir_args);
19659+ bcpup = err;
19660+ wh_dentry = ERR_PTR(err);
19661+ if (unlikely(err < 0))
19662+ goto out;
19663+
19664+ sb = dentry->d_sb;
19665+ udba = au_opt_udba(sb);
19666+ err = au_pin(pin, dentry, bcpup, udba,
19667+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19668+ wh_dentry = ERR_PTR(err);
19669+ if (unlikely(err))
19670+ goto out;
19671+
19672+ h_parent = au_pinned_h_parent(pin);
19673+ if (udba != AuOpt_UDBA_NONE
19674+ && au_dbtop(dentry) == bcpup)
19675+ err = au_may_add(dentry, bcpup, h_parent,
19676+ au_ftest_wrdir(wr_dir_args->flags, ISDIR));
19677+ else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19678+ err = -ENAMETOOLONG;
19679+ wh_dentry = ERR_PTR(err);
19680+ if (unlikely(err))
19681+ goto out_unpin;
19682+
19683+ br = au_sbr(sb, bcpup);
19684+ if (dt) {
19685+ struct path tmp = {
19686+ .dentry = h_parent,
19687+ .mnt = au_br_mnt(br)
19688+ };
19689+ au_dtime_store(dt, au_pinned_parent(pin), &tmp);
19690+ }
19691+
19692+ wh_dentry = NULL;
19693+ if (bcpup != au_dbwh(dentry))
19694+ goto out; /* success */
19695+
19696+ /*
19697+ * ENAMETOOLONG here means that if we allowed create such name, then it
19698+ * would not be able to removed in the future. So we don't allow such
19699+ * name here and we don't handle ENAMETOOLONG differently here.
19700+ */
19701+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
19702+
19703+out_unpin:
19704+ if (IS_ERR(wh_dentry))
19705+ au_unpin(pin);
19706+out:
19707+ return wh_dentry;
19708+}
19709+
19710+/* ---------------------------------------------------------------------- */
19711+
19712+enum { Mknod, Symlink, Creat };
19713+struct simple_arg {
19714+ int type;
19715+ union {
19716+ struct {
19717+ umode_t mode;
19718+ bool want_excl;
19719+ bool try_aopen;
19720+ struct vfsub_aopen_args *aopen;
19721+ } c;
19722+ struct {
19723+ const char *symname;
19724+ } s;
19725+ struct {
19726+ umode_t mode;
19727+ dev_t dev;
19728+ } m;
19729+ } u;
19730+};
19731+
19732+static int add_simple(struct inode *dir, struct dentry *dentry,
19733+ struct simple_arg *arg)
19734+{
19735+ int err, rerr;
19736+ aufs_bindex_t btop;
19737+ unsigned char created;
19738+ const unsigned char try_aopen
19739+ = (arg->type == Creat && arg->u.c.try_aopen);
19740+ struct vfsub_aopen_args *aopen = arg->u.c.aopen;
19741+ struct dentry *wh_dentry, *parent;
19742+ struct inode *h_dir;
19743+ struct super_block *sb;
19744+ struct au_branch *br;
19745+ /* to reduce stack size */
19746+ struct {
19747+ struct au_dtime dt;
19748+ struct au_pin pin;
19749+ struct path h_path;
19750+ struct au_wr_dir_args wr_dir_args;
19751+ } *a;
19752+
19753+ AuDbg("%pd\n", dentry);
19754+ IMustLock(dir);
19755+
19756+ err = -ENOMEM;
19757+ a = kmalloc(sizeof(*a), GFP_NOFS);
19758+ if (unlikely(!a))
19759+ goto out;
19760+ a->wr_dir_args.force_btgt = -1;
19761+ a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
19762+
19763+ parent = dentry->d_parent; /* dir inode is locked */
19764+ if (!try_aopen) {
19765+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
19766+ if (unlikely(err))
19767+ goto out_free;
19768+ }
19769+ err = au_d_may_add(dentry);
19770+ if (unlikely(err))
19771+ goto out_unlock;
19772+ if (!try_aopen)
19773+ di_write_lock_parent(parent);
19774+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
19775+ &a->pin, &a->wr_dir_args);
19776+ err = PTR_ERR(wh_dentry);
19777+ if (IS_ERR(wh_dentry))
19778+ goto out_parent;
19779+
19780+ btop = au_dbtop(dentry);
19781+ sb = dentry->d_sb;
19782+ br = au_sbr(sb, btop);
19783+ a->h_path.dentry = au_h_dptr(dentry, btop);
19784+ a->h_path.mnt = au_br_mnt(br);
19785+ h_dir = au_pinned_h_dir(&a->pin);
19786+ switch (arg->type) {
19787+ case Creat:
19788+ if (!try_aopen || !h_dir->i_op->atomic_open) {
19789+ err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
19790+ arg->u.c.want_excl);
19791+ created = !err;
19792+ if (!err && try_aopen)
19793+ aopen->file->f_mode |= FMODE_CREATED;
19794+ } else {
19795+ aopen->br = br;
19796+ err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
19797+ AuDbg("err %d\n", err);
19798+ AuDbgFile(aopen->file);
19799+ created = err >= 0
19800+ && !!(aopen->file->f_mode & FMODE_CREATED);
19801+ }
19802+ break;
19803+ case Symlink:
19804+ err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
19805+ created = !err;
19806+ break;
19807+ case Mknod:
19808+ err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
19809+ arg->u.m.dev);
19810+ created = !err;
19811+ break;
19812+ default:
19813+ BUG();
19814+ }
19815+ if (unlikely(err < 0))
19816+ goto out_unpin;
19817+
19818+ err = epilog(dir, btop, wh_dentry, dentry);
19819+ if (!err)
19820+ goto out_unpin; /* success */
19821+
19822+ /* revert */
19823+ if (created /* && d_is_positive(a->h_path.dentry) */) {
19824+ /* no delegation since it is just created */
19825+ rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
19826+ /*force*/0);
19827+ if (rerr) {
19828+ AuIOErr("%pd revert failure(%d, %d)\n",
19829+ dentry, err, rerr);
19830+ err = -EIO;
19831+ }
19832+ au_dtime_revert(&a->dt);
19833+ }
19834+ if (try_aopen && h_dir->i_op->atomic_open
19835+ && (aopen->file->f_mode & FMODE_OPENED))
19836+ /* aopen->file is still opened */
19837+ au_lcnt_dec(&aopen->br->br_nfiles);
19838+
19839+out_unpin:
19840+ au_unpin(&a->pin);
19841+ dput(wh_dentry);
19842+out_parent:
19843+ if (!try_aopen)
19844+ di_write_unlock(parent);
19845+out_unlock:
19846+ if (unlikely(err)) {
19847+ au_update_dbtop(dentry);
19848+ d_drop(dentry);
19849+ }
19850+ if (!try_aopen)
19851+ aufs_read_unlock(dentry, AuLock_DW);
19852+out_free:
19853+ au_kfree_rcu(a);
19854+out:
19855+ return err;
19856+}
19857+
19858+int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
19859+ dev_t dev)
19860+{
19861+ struct simple_arg arg = {
19862+ .type = Mknod,
19863+ .u.m = {
19864+ .mode = mode,
19865+ .dev = dev
19866+ }
19867+ };
19868+ return add_simple(dir, dentry, &arg);
19869+}
19870+
19871+int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
19872+{
19873+ struct simple_arg arg = {
19874+ .type = Symlink,
19875+ .u.s.symname = symname
19876+ };
19877+ return add_simple(dir, dentry, &arg);
19878+}
19879+
19880+int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
19881+ bool want_excl)
19882+{
19883+ struct simple_arg arg = {
19884+ .type = Creat,
19885+ .u.c = {
19886+ .mode = mode,
19887+ .want_excl = want_excl
19888+ }
19889+ };
19890+ return add_simple(dir, dentry, &arg);
19891+}
19892+
19893+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
19894+ struct vfsub_aopen_args *aopen_args)
19895+{
19896+ struct simple_arg arg = {
19897+ .type = Creat,
19898+ .u.c = {
19899+ .mode = aopen_args->create_mode,
19900+ .want_excl = aopen_args->open_flag & O_EXCL,
19901+ .try_aopen = true,
19902+ .aopen = aopen_args
19903+ }
19904+ };
19905+ return add_simple(dir, dentry, &arg);
19906+}
19907+
19908+int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
19909+{
19910+ int err;
19911+ aufs_bindex_t bindex;
19912+ struct super_block *sb;
19913+ struct dentry *parent, *h_parent, *h_dentry;
19914+ struct inode *h_dir, *inode;
19915+ struct vfsmount *h_mnt;
19916+ struct au_wr_dir_args wr_dir_args = {
19917+ .force_btgt = -1,
19918+ .flags = AuWrDir_TMPFILE
19919+ };
19920+
19921+ /* copy-up may happen */
19922+ inode_lock(dir);
19923+
19924+ sb = dir->i_sb;
19925+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19926+ if (unlikely(err))
19927+ goto out;
19928+
19929+ err = au_di_init(dentry);
19930+ if (unlikely(err))
19931+ goto out_si;
19932+
19933+ err = -EBUSY;
19934+ parent = d_find_any_alias(dir);
19935+ AuDebugOn(!parent);
19936+ di_write_lock_parent(parent);
19937+ if (unlikely(d_inode(parent) != dir))
19938+ goto out_parent;
19939+
19940+ err = au_digen_test(parent, au_sigen(sb));
19941+ if (unlikely(err))
19942+ goto out_parent;
19943+
19944+ bindex = au_dbtop(parent);
19945+ au_set_dbtop(dentry, bindex);
19946+ au_set_dbbot(dentry, bindex);
19947+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
19948+ bindex = err;
19949+ if (unlikely(err < 0))
19950+ goto out_parent;
19951+
19952+ err = -EOPNOTSUPP;
19953+ h_dir = au_h_iptr(dir, bindex);
19954+ if (unlikely(!h_dir->i_op->tmpfile))
19955+ goto out_parent;
19956+
19957+ h_mnt = au_sbr_mnt(sb, bindex);
19958+ err = vfsub_mnt_want_write(h_mnt);
19959+ if (unlikely(err))
19960+ goto out_parent;
19961+
19962+ h_parent = au_h_dptr(parent, bindex);
19963+ h_dentry = vfs_tmpfile(h_parent, mode, /*open_flag*/0);
19964+ if (IS_ERR(h_dentry)) {
19965+ err = PTR_ERR(h_dentry);
19966+ goto out_mnt;
19967+ }
19968+
19969+ au_set_dbtop(dentry, bindex);
19970+ au_set_dbbot(dentry, bindex);
19971+ au_set_h_dptr(dentry, bindex, dget(h_dentry));
19972+ inode = au_new_inode(dentry, /*must_new*/1);
19973+ if (IS_ERR(inode)) {
19974+ err = PTR_ERR(inode);
19975+ au_set_h_dptr(dentry, bindex, NULL);
19976+ au_set_dbtop(dentry, -1);
19977+ au_set_dbbot(dentry, -1);
19978+ } else {
19979+ if (!inode->i_nlink)
19980+ set_nlink(inode, 1);
19981+ d_tmpfile(dentry, inode);
19982+ au_di(dentry)->di_tmpfile = 1;
19983+
19984+ /* update without i_mutex */
19985+ if (au_ibtop(dir) == au_dbtop(dentry))
19986+ au_cpup_attr_timesizes(dir);
19987+ }
19988+ dput(h_dentry);
19989+
19990+out_mnt:
19991+ vfsub_mnt_drop_write(h_mnt);
19992+out_parent:
19993+ di_write_unlock(parent);
19994+ dput(parent);
19995+ di_write_unlock(dentry);
19996+ if (unlikely(err)) {
19997+ au_di_fin(dentry);
19998+ dentry->d_fsdata = NULL;
19999+ }
20000+out_si:
20001+ si_read_unlock(sb);
20002+out:
20003+ inode_unlock(dir);
20004+ return err;
20005+}
20006+
20007+/* ---------------------------------------------------------------------- */
20008+
20009+struct au_link_args {
20010+ aufs_bindex_t bdst, bsrc;
20011+ struct au_pin pin;
20012+ struct path h_path;
20013+ struct dentry *src_parent, *parent;
20014+};
20015+
20016+static int au_cpup_before_link(struct dentry *src_dentry,
20017+ struct au_link_args *a)
20018+{
20019+ int err;
20020+ struct dentry *h_src_dentry;
20021+ struct au_cp_generic cpg = {
20022+ .dentry = src_dentry,
20023+ .bdst = a->bdst,
20024+ .bsrc = a->bsrc,
20025+ .len = -1,
20026+ .pin = &a->pin,
20027+ .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
20028+ };
20029+
20030+ di_read_lock_parent(a->src_parent, AuLock_IR);
20031+ err = au_test_and_cpup_dirs(src_dentry, a->bdst);
20032+ if (unlikely(err))
20033+ goto out;
20034+
20035+ h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
20036+ err = au_pin(&a->pin, src_dentry, a->bdst,
20037+ au_opt_udba(src_dentry->d_sb),
20038+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20039+ if (unlikely(err))
20040+ goto out;
20041+
20042+ err = au_sio_cpup_simple(&cpg);
20043+ au_unpin(&a->pin);
20044+
20045+out:
20046+ di_read_unlock(a->src_parent, AuLock_IR);
20047+ return err;
20048+}
20049+
20050+static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
20051+ struct au_link_args *a)
20052+{
20053+ int err;
20054+ unsigned char plink;
20055+ aufs_bindex_t bbot;
20056+ struct dentry *h_src_dentry;
20057+ struct inode *h_inode, *inode, *delegated;
20058+ struct super_block *sb;
20059+ struct file *h_file;
20060+
20061+ plink = 0;
20062+ h_inode = NULL;
20063+ sb = src_dentry->d_sb;
20064+ inode = d_inode(src_dentry);
20065+ if (au_ibtop(inode) <= a->bdst)
20066+ h_inode = au_h_iptr(inode, a->bdst);
20067+ if (!h_inode || !h_inode->i_nlink) {
20068+ /* copyup src_dentry as the name of dentry. */
20069+ bbot = au_dbbot(dentry);
20070+ if (bbot < a->bsrc)
20071+ au_set_dbbot(dentry, a->bsrc);
20072+ au_set_h_dptr(dentry, a->bsrc,
20073+ dget(au_h_dptr(src_dentry, a->bsrc)));
20074+ dget(a->h_path.dentry);
20075+ au_set_h_dptr(dentry, a->bdst, NULL);
20076+ AuDbg("temporary d_inode...\n");
20077+ spin_lock(&dentry->d_lock);
20078+ dentry->d_inode = d_inode(src_dentry); /* tmp */
20079+ spin_unlock(&dentry->d_lock);
20080+ h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
20081+ if (IS_ERR(h_file))
20082+ err = PTR_ERR(h_file);
20083+ else {
20084+ struct au_cp_generic cpg = {
20085+ .dentry = dentry,
20086+ .bdst = a->bdst,
20087+ .bsrc = -1,
20088+ .len = -1,
20089+ .pin = &a->pin,
20090+ .flags = AuCpup_KEEPLINO
20091+ };
20092+ err = au_sio_cpup_simple(&cpg);
20093+ au_h_open_post(dentry, a->bsrc, h_file);
20094+ if (!err) {
20095+ dput(a->h_path.dentry);
20096+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20097+ } else
20098+ au_set_h_dptr(dentry, a->bdst,
20099+ a->h_path.dentry);
20100+ }
20101+ spin_lock(&dentry->d_lock);
20102+ dentry->d_inode = NULL; /* restore */
20103+ spin_unlock(&dentry->d_lock);
20104+ AuDbg("temporary d_inode...done\n");
20105+ au_set_h_dptr(dentry, a->bsrc, NULL);
20106+ au_set_dbbot(dentry, bbot);
20107+ } else {
20108+ /* the inode of src_dentry already exists on a.bdst branch */
20109+ h_src_dentry = d_find_alias(h_inode);
20110+ if (!h_src_dentry && au_plink_test(inode)) {
20111+ plink = 1;
20112+ h_src_dentry = au_plink_lkup(inode, a->bdst);
20113+ err = PTR_ERR(h_src_dentry);
20114+ if (IS_ERR(h_src_dentry))
20115+ goto out;
20116+
20117+ if (unlikely(d_is_negative(h_src_dentry))) {
20118+ dput(h_src_dentry);
20119+ h_src_dentry = NULL;
20120+ }
20121+
20122+ }
20123+ if (h_src_dentry) {
20124+ delegated = NULL;
20125+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20126+ &a->h_path, &delegated);
20127+ if (unlikely(err == -EWOULDBLOCK)) {
20128+ pr_warn("cannot retry for NFSv4 delegation"
20129+ " for an internal link\n");
20130+ iput(delegated);
20131+ }
20132+ dput(h_src_dentry);
20133+ } else {
20134+ AuIOErr("no dentry found for hi%lu on b%d\n",
20135+ h_inode->i_ino, a->bdst);
20136+ err = -EIO;
20137+ }
20138+ }
20139+
20140+ if (!err && !plink)
20141+ au_plink_append(inode, a->bdst, a->h_path.dentry);
20142+
20143+out:
20144+ AuTraceErr(err);
20145+ return err;
20146+}
20147+
20148+int aufs_link(struct dentry *src_dentry, struct inode *dir,
20149+ struct dentry *dentry)
20150+{
20151+ int err, rerr;
20152+ struct au_dtime dt;
20153+ struct au_link_args *a;
20154+ struct dentry *wh_dentry, *h_src_dentry;
20155+ struct inode *inode, *delegated;
20156+ struct super_block *sb;
20157+ struct au_wr_dir_args wr_dir_args = {
20158+ /* .force_btgt = -1, */
20159+ .flags = AuWrDir_ADD_ENTRY
20160+ };
20161+
20162+ IMustLock(dir);
20163+ inode = d_inode(src_dentry);
20164+ IMustLock(inode);
20165+
20166+ err = -ENOMEM;
20167+ a = kzalloc(sizeof(*a), GFP_NOFS);
20168+ if (unlikely(!a))
20169+ goto out;
20170+
20171+ a->parent = dentry->d_parent; /* dir inode is locked */
20172+ err = aufs_read_and_write_lock2(dentry, src_dentry,
20173+ AuLock_NOPLM | AuLock_GEN);
20174+ if (unlikely(err))
20175+ goto out_kfree;
20176+ err = au_d_linkable(src_dentry);
20177+ if (unlikely(err))
20178+ goto out_unlock;
20179+ err = au_d_may_add(dentry);
20180+ if (unlikely(err))
20181+ goto out_unlock;
20182+
20183+ a->src_parent = dget_parent(src_dentry);
20184+ wr_dir_args.force_btgt = au_ibtop(inode);
20185+
20186+ di_write_lock_parent(a->parent);
20187+ wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
20188+ wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
20189+ &wr_dir_args);
20190+ err = PTR_ERR(wh_dentry);
20191+ if (IS_ERR(wh_dentry))
20192+ goto out_parent;
20193+
20194+ err = 0;
20195+ sb = dentry->d_sb;
20196+ a->bdst = au_dbtop(dentry);
20197+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20198+ a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
20199+ a->bsrc = au_ibtop(inode);
20200+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20201+ if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
20202+ h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
20203+ if (!h_src_dentry) {
20204+ a->bsrc = au_dbtop(src_dentry);
20205+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20206+ AuDebugOn(!h_src_dentry);
20207+ } else if (IS_ERR(h_src_dentry)) {
20208+ err = PTR_ERR(h_src_dentry);
20209+ goto out_parent;
20210+ }
20211+
20212+ /*
20213+ * aufs doesn't touch the credential so
20214+ * security_dentry_create_files_as() is unnecessary.
20215+ */
20216+ if (au_opt_test(au_mntflags(sb), PLINK)) {
20217+ if (a->bdst < a->bsrc
20218+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
20219+ err = au_cpup_or_link(src_dentry, dentry, a);
20220+ else {
20221+ delegated = NULL;
20222+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20223+ &a->h_path, &delegated);
20224+ if (unlikely(err == -EWOULDBLOCK)) {
20225+ pr_warn("cannot retry for NFSv4 delegation"
20226+ " for an internal link\n");
20227+ iput(delegated);
20228+ }
20229+ }
20230+ dput(h_src_dentry);
20231+ } else {
20232+ /*
20233+ * copyup src_dentry to the branch we process,
20234+ * and then link(2) to it.
20235+ */
20236+ dput(h_src_dentry);
20237+ if (a->bdst < a->bsrc
20238+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
20239+ au_unpin(&a->pin);
20240+ di_write_unlock(a->parent);
20241+ err = au_cpup_before_link(src_dentry, a);
20242+ di_write_lock_parent(a->parent);
20243+ if (!err)
20244+ err = au_pin(&a->pin, dentry, a->bdst,
20245+ au_opt_udba(sb),
20246+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20247+ if (unlikely(err))
20248+ goto out_wh;
20249+ }
20250+ if (!err) {
20251+ h_src_dentry = au_h_dptr(src_dentry, a->bdst);
20252+ err = -ENOENT;
20253+ if (h_src_dentry && d_is_positive(h_src_dentry)) {
20254+ delegated = NULL;
20255+ err = vfsub_link(h_src_dentry,
20256+ au_pinned_h_dir(&a->pin),
20257+ &a->h_path, &delegated);
20258+ if (unlikely(err == -EWOULDBLOCK)) {
20259+ pr_warn("cannot retry"
20260+ " for NFSv4 delegation"
20261+ " for an internal link\n");
20262+ iput(delegated);
20263+ }
20264+ }
20265+ }
20266+ }
20267+ if (unlikely(err))
20268+ goto out_unpin;
20269+
20270+ if (wh_dentry) {
20271+ a->h_path.dentry = wh_dentry;
20272+ err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
20273+ dentry);
20274+ if (unlikely(err))
20275+ goto out_revert;
20276+ }
20277+
20278+ au_dir_ts(dir, a->bdst);
20279+ inode_inc_iversion(dir);
20280+ inc_nlink(inode);
20281+ inode->i_ctime = dir->i_ctime;
20282+ d_instantiate(dentry, au_igrab(inode));
20283+ if (d_unhashed(a->h_path.dentry))
20284+ /* some filesystem calls d_drop() */
20285+ d_drop(dentry);
20286+ /* some filesystems consume an inode even hardlink */
20287+ au_fhsm_wrote(sb, a->bdst, /*force*/0);
20288+ goto out_unpin; /* success */
20289+
20290+out_revert:
20291+ /* no delegation since it is just created */
20292+ rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
20293+ /*delegated*/NULL, /*force*/0);
20294+ if (unlikely(rerr)) {
20295+ AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
20296+ err = -EIO;
20297+ }
20298+ au_dtime_revert(&dt);
20299+out_unpin:
20300+ au_unpin(&a->pin);
20301+out_wh:
20302+ dput(wh_dentry);
20303+out_parent:
20304+ di_write_unlock(a->parent);
20305+ dput(a->src_parent);
20306+out_unlock:
20307+ if (unlikely(err)) {
20308+ au_update_dbtop(dentry);
20309+ d_drop(dentry);
20310+ }
20311+ aufs_read_and_write_unlock2(dentry, src_dentry);
20312+out_kfree:
20313+ au_kfree_rcu(a);
20314+out:
20315+ AuTraceErr(err);
20316+ return err;
20317+}
20318+
20319+int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
20320+{
20321+ int err, rerr;
20322+ aufs_bindex_t bindex;
20323+ unsigned char diropq;
20324+ struct path h_path;
20325+ struct dentry *wh_dentry, *parent, *opq_dentry;
20326+ struct inode *h_inode;
20327+ struct super_block *sb;
20328+ struct {
20329+ struct au_pin pin;
20330+ struct au_dtime dt;
20331+ } *a; /* reduce the stack usage */
20332+ struct au_wr_dir_args wr_dir_args = {
20333+ .force_btgt = -1,
20334+ .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
20335+ };
20336+
20337+ IMustLock(dir);
20338+
20339+ err = -ENOMEM;
20340+ a = kmalloc(sizeof(*a), GFP_NOFS);
20341+ if (unlikely(!a))
20342+ goto out;
20343+
20344+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20345+ if (unlikely(err))
20346+ goto out_free;
20347+ err = au_d_may_add(dentry);
20348+ if (unlikely(err))
20349+ goto out_unlock;
20350+
20351+ parent = dentry->d_parent; /* dir inode is locked */
20352+ di_write_lock_parent(parent);
20353+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20354+ &a->pin, &wr_dir_args);
20355+ err = PTR_ERR(wh_dentry);
20356+ if (IS_ERR(wh_dentry))
20357+ goto out_parent;
20358+
20359+ sb = dentry->d_sb;
20360+ bindex = au_dbtop(dentry);
20361+ h_path.dentry = au_h_dptr(dentry, bindex);
20362+ h_path.mnt = au_sbr_mnt(sb, bindex);
20363+ err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
20364+ if (unlikely(err))
20365+ goto out_unpin;
20366+
20367+ /* make the dir opaque */
20368+ diropq = 0;
20369+ h_inode = d_inode(h_path.dentry);
20370+ if (wh_dentry
20371+ || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
20372+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
20373+ opq_dentry = au_diropq_create(dentry, bindex);
20374+ inode_unlock(h_inode);
20375+ err = PTR_ERR(opq_dentry);
20376+ if (IS_ERR(opq_dentry))
20377+ goto out_dir;
20378+ dput(opq_dentry);
20379+ diropq = 1;
20380+ }
20381+
20382+ err = epilog(dir, bindex, wh_dentry, dentry);
20383+ if (!err) {
20384+ inc_nlink(dir);
20385+ goto out_unpin; /* success */
20386+ }
20387+
20388+ /* revert */
20389+ if (diropq) {
20390+ AuLabel(revert opq);
20391+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
20392+ rerr = au_diropq_remove(dentry, bindex);
20393+ inode_unlock(h_inode);
20394+ if (rerr) {
20395+ AuIOErr("%pd reverting diropq failed(%d, %d)\n",
20396+ dentry, err, rerr);
20397+ err = -EIO;
20398+ }
20399+ }
20400+
20401+out_dir:
20402+ AuLabel(revert dir);
20403+ rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
20404+ if (rerr) {
20405+ AuIOErr("%pd reverting dir failed(%d, %d)\n",
20406+ dentry, err, rerr);
20407+ err = -EIO;
20408+ }
20409+ au_dtime_revert(&a->dt);
20410+out_unpin:
20411+ au_unpin(&a->pin);
20412+ dput(wh_dentry);
20413+out_parent:
20414+ di_write_unlock(parent);
20415+out_unlock:
20416+ if (unlikely(err)) {
20417+ au_update_dbtop(dentry);
20418+ d_drop(dentry);
20419+ }
20420+ aufs_read_unlock(dentry, AuLock_DW);
20421+out_free:
20422+ au_kfree_rcu(a);
20423+out:
20424+ return err;
20425+}
20426diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
20427--- /usr/share/empty/fs/aufs/i_op.c 1970-01-01 01:00:00.000000000 +0100
20428+++ linux/fs/aufs/i_op.c 2021-02-22 22:51:59.058078584 +0100
20429@@ -0,0 +1,1502 @@
20430+// SPDX-License-Identifier: GPL-2.0
20431+/*
20432+ * Copyright (C) 2005-2020 Junjiro R. Okajima
20433+ *
20434+ * This program, aufs is free software; you can redistribute it and/or modify
20435+ * it under the terms of the GNU General Public License as published by
20436+ * the Free Software Foundation; either version 2 of the License, or
20437+ * (at your option) any later version.
20438+ *
20439+ * This program is distributed in the hope that it will be useful,
20440+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20441+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20442+ * GNU General Public License for more details.
20443+ *
20444+ * You should have received a copy of the GNU General Public License
20445+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
20446+ */
20447+
20448+/*
20449+ * inode operations (except add/del/rename)
20450+ */
20451+
20452+#include <linux/device_cgroup.h>
20453+#include <linux/fs_stack.h>
20454+#include <linux/iversion.h>
20455+#include <linux/namei.h>
20456+#include <linux/security.h>
20457+#include "aufs.h"
20458+
20459+static int h_permission(struct inode *h_inode, int mask,
20460+ struct path *h_path, int brperm)
20461+{
20462+ int err;
20463+ const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20464+
20465+ err = -EPERM;
20466+ if (write_mask && IS_IMMUTABLE(h_inode))
20467+ goto out;
20468+
20469+ err = -EACCES;
20470+ if (((mask & MAY_EXEC)
20471+ && S_ISREG(h_inode->i_mode)
20472+ && (path_noexec(h_path)
20473+ || !(h_inode->i_mode & 0111))))
20474+ goto out;
20475+
20476+ /*
20477+ * - skip the lower fs test in the case of write to ro branch.
20478+ * - nfs dir permission write check is optimized, but a policy for
20479+ * link/rename requires a real check.
20480+ * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
20481+ * in this case, generic_permission() returns -EOPNOTSUPP.
20482+ */
20483+ if ((write_mask && !au_br_writable(brperm))
20484+ || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
20485+ && write_mask && !(mask & MAY_READ))
20486+ || !h_inode->i_op->permission) {
20487+ /* AuLabel(generic_permission); */
20488+ /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
20489+ err = generic_permission(h_inode, mask);
20490+ if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
20491+ err = h_inode->i_op->permission(h_inode, mask);
20492+ AuTraceErr(err);
20493+ } else {
20494+ /* AuLabel(h_inode->permission); */
20495+ err = h_inode->i_op->permission(h_inode, mask);
20496+ AuTraceErr(err);
20497+ }
20498+
20499+ if (!err)
20500+ err = devcgroup_inode_permission(h_inode, mask);
20501+ if (!err)
20502+ err = security_inode_permission(h_inode, mask);
20503+
20504+out:
20505+ return err;
20506+}
20507+
20508+static int aufs_permission(struct inode *inode, int mask)
20509+{
20510+ int err;
20511+ aufs_bindex_t bindex, bbot;
20512+ const unsigned char isdir = !!S_ISDIR(inode->i_mode),
20513+ write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20514+ struct inode *h_inode;
20515+ struct super_block *sb;
20516+ struct au_branch *br;
20517+
20518+ /* todo: support rcu-walk? */
20519+ if (mask & MAY_NOT_BLOCK)
20520+ return -ECHILD;
20521+
20522+ sb = inode->i_sb;
20523+ si_read_lock(sb, AuLock_FLUSH);
20524+ ii_read_lock_child(inode);
20525+#if 0 /* reserved for future use */
20526+ /*
20527+ * This test may be rather 'too much' since the test is essentially done
20528+ * in the aufs_lookup(). Theoretically it is possible that the inode
20529+ * generation doesn't match to the superblock's here. But it isn't a
20530+ * big deal I suppose.
20531+ */
20532+ err = au_iigen_test(inode, au_sigen(sb));
20533+ if (unlikely(err))
20534+ goto out;
20535+#endif
20536+
20537+ if (!isdir
20538+ || write_mask
20539+ || au_opt_test(au_mntflags(sb), DIRPERM1)) {
20540+ err = au_busy_or_stale();
20541+ h_inode = au_h_iptr(inode, au_ibtop(inode));
20542+ if (unlikely(!h_inode
20543+ || (h_inode->i_mode & S_IFMT)
20544+ != (inode->i_mode & S_IFMT)))
20545+ goto out;
20546+
20547+ err = 0;
20548+ bindex = au_ibtop(inode);
20549+ br = au_sbr(sb, bindex);
20550+ err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
20551+ if (write_mask
20552+ && !err
20553+ && !special_file(h_inode->i_mode)) {
20554+ /* test whether the upper writable branch exists */
20555+ err = -EROFS;
20556+ for (; bindex >= 0; bindex--)
20557+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
20558+ err = 0;
20559+ break;
20560+ }
20561+ }
20562+ goto out;
20563+ }
20564+
20565+ /* non-write to dir */
20566+ err = 0;
20567+ bbot = au_ibbot(inode);
20568+ for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
20569+ h_inode = au_h_iptr(inode, bindex);
20570+ if (h_inode) {
20571+ err = au_busy_or_stale();
20572+ if (unlikely(!S_ISDIR(h_inode->i_mode)))
20573+ break;
20574+
20575+ br = au_sbr(sb, bindex);
20576+ err = h_permission(h_inode, mask, &br->br_path,
20577+ br->br_perm);
20578+ }
20579+ }
20580+
20581+out:
20582+ ii_read_unlock(inode);
20583+ si_read_unlock(sb);
20584+ return err;
20585+}
20586+
20587+/* ---------------------------------------------------------------------- */
20588+
20589+static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
20590+ unsigned int flags)
20591+{
20592+ struct dentry *ret, *parent;
20593+ struct inode *inode;
20594+ struct super_block *sb;
20595+ int err, npositive;
20596+
20597+ IMustLock(dir);
20598+
20599+ /* todo: support rcu-walk? */
20600+ ret = ERR_PTR(-ECHILD);
20601+ if (flags & LOOKUP_RCU)
20602+ goto out;
20603+
20604+ ret = ERR_PTR(-ENAMETOOLONG);
20605+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20606+ goto out;
20607+
20608+ sb = dir->i_sb;
20609+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20610+ ret = ERR_PTR(err);
20611+ if (unlikely(err))
20612+ goto out;
20613+
20614+ err = au_di_init(dentry);
20615+ ret = ERR_PTR(err);
20616+ if (unlikely(err))
20617+ goto out_si;
20618+
20619+ inode = NULL;
20620+ npositive = 0; /* suppress a warning */
20621+ parent = dentry->d_parent; /* dir inode is locked */
20622+ di_read_lock_parent(parent, AuLock_IR);
20623+ err = au_alive_dir(parent);
20624+ if (!err)
20625+ err = au_digen_test(parent, au_sigen(sb));
20626+ if (!err) {
20627+ /* regardless LOOKUP_CREATE, always ALLOW_NEG */
20628+ npositive = au_lkup_dentry(dentry, au_dbtop(parent),
20629+ AuLkup_ALLOW_NEG);
20630+ err = npositive;
20631+ }
20632+ di_read_unlock(parent, AuLock_IR);
20633+ ret = ERR_PTR(err);
20634+ if (unlikely(err < 0))
20635+ goto out_unlock;
20636+
20637+ if (npositive) {
20638+ inode = au_new_inode(dentry, /*must_new*/0);
20639+ if (IS_ERR(inode)) {
20640+ ret = (void *)inode;
20641+ inode = NULL;
20642+ goto out_unlock;
20643+ }
20644+ }
20645+
20646+ if (inode)
20647+ atomic_inc(&inode->i_count);
20648+ ret = d_splice_alias(inode, dentry);
20649+#if 0 /* reserved for future use */
20650+ if (unlikely(d_need_lookup(dentry))) {
20651+ spin_lock(&dentry->d_lock);
20652+ dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
20653+ spin_unlock(&dentry->d_lock);
20654+ } else
20655+#endif
20656+ if (inode) {
20657+ if (!IS_ERR(ret)) {
20658+ iput(inode);
20659+ if (ret && ret != dentry)
20660+ ii_write_unlock(inode);
20661+ } else {
20662+ ii_write_unlock(inode);
20663+ iput(inode);
20664+ inode = NULL;
20665+ }
20666+ }
20667+
20668+out_unlock:
20669+ di_write_unlock(dentry);
20670+out_si:
20671+ si_read_unlock(sb);
20672+out:
20673+ return ret;
20674+}
20675+
20676+/* ---------------------------------------------------------------------- */
20677+
20678+/*
20679+ * very dirty and complicated aufs ->atomic_open().
20680+ * aufs_atomic_open()
20681+ * + au_aopen_or_create()
20682+ * + add_simple()
20683+ * + vfsub_atomic_open()
20684+ * + branch fs ->atomic_open()
20685+ * may call the actual 'open' for h_file
20686+ * + inc br_nfiles only if opened
20687+ * + au_aopen_no_open() or au_aopen_do_open()
20688+ *
20689+ * au_aopen_do_open()
20690+ * + finish_open()
20691+ * + au_do_aopen()
20692+ * + au_do_open() the body of all 'open'
20693+ * + au_do_open_nondir()
20694+ * set the passed h_file
20695+ *
20696+ * au_aopen_no_open()
20697+ * + finish_no_open()
20698+ */
20699+
20700+struct aopen_node {
20701+ struct hlist_bl_node hblist;
20702+ struct file *file, *h_file;
20703+};
20704+
20705+static int au_do_aopen(struct inode *inode, struct file *file)
20706+{
20707+ struct hlist_bl_head *aopen;
20708+ struct hlist_bl_node *pos;
20709+ struct aopen_node *node;
20710+ struct au_do_open_args args = {
20711+ .aopen = 1,
20712+ .open = au_do_open_nondir
20713+ };
20714+
20715+ aopen = &au_sbi(inode->i_sb)->si_aopen;
20716+ hlist_bl_lock(aopen);
20717+ hlist_bl_for_each_entry(node, pos, aopen, hblist)
20718+ if (node->file == file) {
20719+ args.h_file = node->h_file;
20720+ break;
20721+ }
20722+ hlist_bl_unlock(aopen);
20723+ /* AuDebugOn(!args.h_file); */
20724+
20725+ return au_do_open(file, &args);
20726+}
20727+
20728+static int au_aopen_do_open(struct file *file, struct dentry *dentry,
20729+ struct aopen_node *aopen_node)
20730+{
20731+ int err;
20732+ struct hlist_bl_head *aopen;
20733+
20734+ AuLabel(here);
20735+ aopen = &au_sbi(dentry->d_sb)->si_aopen;
20736+ au_hbl_add(&aopen_node->hblist, aopen);
20737+ err = finish_open(file, dentry, au_do_aopen);
20738+ au_hbl_del(&aopen_node->hblist, aopen);
20739+ /* AuDbgFile(file); */
20740+ AuDbg("%pd%s%s\n", dentry,
20741+ (file->f_mode & FMODE_CREATED) ? " created" : "",
20742+ (file->f_mode & FMODE_OPENED) ? " opened" : "");
20743+
20744+ AuTraceErr(err);
20745+ return err;
20746+}
20747+
20748+static int au_aopen_no_open(struct file *file, struct dentry *dentry)
20749+{
20750+ int err;
20751+
20752+ AuLabel(here);
20753+ dget(dentry);
20754+ err = finish_no_open(file, dentry);
20755+
20756+ AuTraceErr(err);
20757+ return err;
20758+}
20759+
20760+static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
20761+ struct file *file, unsigned int open_flag,
20762+ umode_t create_mode)
20763+{
20764+ int err, did_open;
20765+ unsigned int lkup_flags;
20766+ aufs_bindex_t bindex;
20767+ struct super_block *sb;
20768+ struct dentry *parent, *d;
20769+ struct vfsub_aopen_args args = {
20770+ .open_flag = open_flag,
20771+ .create_mode = create_mode
20772+ };
20773+ struct aopen_node aopen_node = {
20774+ .file = file
20775+ };
20776+
20777+ IMustLock(dir);
20778+ AuDbg("open_flag 0%o\n", open_flag);
20779+ AuDbgDentry(dentry);
20780+
20781+ err = 0;
20782+ if (!au_di(dentry)) {
20783+ lkup_flags = LOOKUP_OPEN;
20784+ if (open_flag & O_CREAT)
20785+ lkup_flags |= LOOKUP_CREATE;
20786+ d = aufs_lookup(dir, dentry, lkup_flags);
20787+ if (IS_ERR(d)) {
20788+ err = PTR_ERR(d);
20789+ AuTraceErr(err);
20790+ goto out;
20791+ } else if (d) {
20792+ /*
20793+ * obsoleted dentry found.
20794+ * another error will be returned later.
20795+ */
20796+ d_drop(d);
20797+ AuDbgDentry(d);
20798+ dput(d);
20799+ }
20800+ AuDbgDentry(dentry);
20801+ }
20802+
20803+ if (d_is_positive(dentry)
20804+ || d_unhashed(dentry)
20805+ || d_unlinked(dentry)
20806+ || !(open_flag & O_CREAT)) {
20807+ err = au_aopen_no_open(file, dentry);
20808+ goto out; /* success */
20809+ }
20810+
20811+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
20812+ if (unlikely(err))
20813+ goto out;
20814+
20815+ sb = dentry->d_sb;
20816+ parent = dentry->d_parent; /* dir is locked */
20817+ di_write_lock_parent(parent);
20818+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
20819+ if (unlikely(err < 0))
20820+ goto out_parent;
20821+
20822+ AuDbgDentry(dentry);
20823+ if (d_is_positive(dentry)) {
20824+ err = au_aopen_no_open(file, dentry);
20825+ goto out_parent; /* success */
20826+ }
20827+
20828+ args.file = alloc_empty_file(file->f_flags, current_cred());
20829+ err = PTR_ERR(args.file);
20830+ if (IS_ERR(args.file))
20831+ goto out_parent;
20832+
20833+ bindex = au_dbtop(dentry);
20834+ err = au_aopen_or_create(dir, dentry, &args);
20835+ AuTraceErr(err);
20836+ AuDbgFile(args.file);
20837+ file->f_mode = args.file->f_mode & ~FMODE_OPENED;
20838+ did_open = !!(args.file->f_mode & FMODE_OPENED);
20839+ if (!did_open) {
20840+ fput(args.file);
20841+ args.file = NULL;
20842+ }
20843+ di_write_unlock(parent);
20844+ di_write_unlock(dentry);
20845+ if (unlikely(err < 0)) {
20846+ if (args.file)
20847+ fput(args.file);
20848+ goto out_sb;
20849+ }
20850+
20851+ if (!did_open)
20852+ err = au_aopen_no_open(file, dentry);
20853+ else {
20854+ aopen_node.h_file = args.file;
20855+ err = au_aopen_do_open(file, dentry, &aopen_node);
20856+ }
20857+ if (unlikely(err < 0)) {
20858+ if (args.file)
20859+ fput(args.file);
20860+ if (did_open)
20861+ au_lcnt_dec(&args.br->br_nfiles);
20862+ }
20863+ goto out_sb; /* success */
20864+
20865+out_parent:
20866+ di_write_unlock(parent);
20867+ di_write_unlock(dentry);
20868+out_sb:
20869+ si_read_unlock(sb);
20870+out:
20871+ AuTraceErr(err);
20872+ AuDbgFile(file);
20873+ return err;
20874+}
20875+
20876+
20877+/* ---------------------------------------------------------------------- */
20878+
20879+static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
20880+ const unsigned char add_entry, aufs_bindex_t bcpup,
20881+ aufs_bindex_t btop)
20882+{
20883+ int err;
20884+ struct dentry *h_parent;
20885+ struct inode *h_dir;
20886+
20887+ if (add_entry)
20888+ IMustLock(d_inode(parent));
20889+ else
20890+ di_write_lock_parent(parent);
20891+
20892+ err = 0;
20893+ if (!au_h_dptr(parent, bcpup)) {
20894+ if (btop > bcpup)
20895+ err = au_cpup_dirs(dentry, bcpup);
20896+ else if (btop < bcpup)
20897+ err = au_cpdown_dirs(dentry, bcpup);
20898+ else
20899+ BUG();
20900+ }
20901+ if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
20902+ h_parent = au_h_dptr(parent, bcpup);
20903+ h_dir = d_inode(h_parent);
20904+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
20905+ err = au_lkup_neg(dentry, bcpup, /*wh*/0);
20906+ /* todo: no unlock here */
20907+ inode_unlock_shared(h_dir);
20908+
20909+ AuDbg("bcpup %d\n", bcpup);
20910+ if (!err) {
20911+ if (d_really_is_negative(dentry))
20912+ au_set_h_dptr(dentry, btop, NULL);
20913+ au_update_dbrange(dentry, /*do_put_zero*/0);
20914+ }
20915+ }
20916+
20917+ if (!add_entry)
20918+ di_write_unlock(parent);
20919+ if (!err)
20920+ err = bcpup; /* success */
20921+
20922+ AuTraceErr(err);
20923+ return err;
20924+}
20925+
20926+/*
20927+ * decide the branch and the parent dir where we will create a new entry.
20928+ * returns new bindex or an error.
20929+ * copyup the parent dir if needed.
20930+ */
20931+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
20932+ struct au_wr_dir_args *args)
20933+{
20934+ int err;
20935+ unsigned int flags;
20936+ aufs_bindex_t bcpup, btop, src_btop;
20937+ const unsigned char add_entry
20938+ = au_ftest_wrdir(args->flags, ADD_ENTRY)
20939+ | au_ftest_wrdir(args->flags, TMPFILE);
20940+ struct super_block *sb;
20941+ struct dentry *parent;
20942+ struct au_sbinfo *sbinfo;
20943+
20944+ sb = dentry->d_sb;
20945+ sbinfo = au_sbi(sb);
20946+ parent = dget_parent(dentry);
20947+ btop = au_dbtop(dentry);
20948+ bcpup = btop;
20949+ if (args->force_btgt < 0) {
20950+ if (src_dentry) {
20951+ src_btop = au_dbtop(src_dentry);
20952+ if (src_btop < btop)
20953+ bcpup = src_btop;
20954+ } else if (add_entry) {
20955+ flags = 0;
20956+ if (au_ftest_wrdir(args->flags, ISDIR))
20957+ au_fset_wbr(flags, DIR);
20958+ err = AuWbrCreate(sbinfo, dentry, flags);
20959+ bcpup = err;
20960+ }
20961+
20962+ if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
20963+ if (add_entry)
20964+ err = AuWbrCopyup(sbinfo, dentry);
20965+ else {
20966+ if (!IS_ROOT(dentry)) {
20967+ di_read_lock_parent(parent, !AuLock_IR);
20968+ err = AuWbrCopyup(sbinfo, dentry);
20969+ di_read_unlock(parent, !AuLock_IR);
20970+ } else
20971+ err = AuWbrCopyup(sbinfo, dentry);
20972+ }
20973+ bcpup = err;
20974+ if (unlikely(err < 0))
20975+ goto out;
20976+ }
20977+ } else {
20978+ bcpup = args->force_btgt;
20979+ AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
20980+ }
20981+
20982+ AuDbg("btop %d, bcpup %d\n", btop, bcpup);
20983+ err = bcpup;
20984+ if (bcpup == btop)
20985+ goto out; /* success */
20986+
20987+ /* copyup the new parent into the branch we process */
20988+ err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
20989+ if (err >= 0) {
20990+ if (d_really_is_negative(dentry)) {
20991+ au_set_h_dptr(dentry, btop, NULL);
20992+ au_set_dbtop(dentry, bcpup);
20993+ au_set_dbbot(dentry, bcpup);
20994+ }
20995+ AuDebugOn(add_entry
20996+ && !au_ftest_wrdir(args->flags, TMPFILE)
20997+ && !au_h_dptr(dentry, bcpup));
20998+ }
20999+
21000+out:
21001+ dput(parent);
21002+ return err;
21003+}
21004+
21005+/* ---------------------------------------------------------------------- */
21006+
21007+void au_pin_hdir_unlock(struct au_pin *p)
21008+{
21009+ if (p->hdir)
21010+ au_hn_inode_unlock(p->hdir);
21011+}
21012+
21013+int au_pin_hdir_lock(struct au_pin *p)
21014+{
21015+ int err;
21016+
21017+ err = 0;
21018+ if (!p->hdir)
21019+ goto out;
21020+
21021+ /* even if an error happens later, keep this lock */
21022+ au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
21023+
21024+ err = -EBUSY;
21025+ if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
21026+ goto out;
21027+
21028+ err = 0;
21029+ if (p->h_dentry)
21030+ err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
21031+ p->h_parent, p->br);
21032+
21033+out:
21034+ return err;
21035+}
21036+
21037+int au_pin_hdir_relock(struct au_pin *p)
21038+{
21039+ int err, i;
21040+ struct inode *h_i;
21041+ struct dentry *h_d[] = {
21042+ p->h_dentry,
21043+ p->h_parent
21044+ };
21045+
21046+ err = au_pin_hdir_lock(p);
21047+ if (unlikely(err))
21048+ goto out;
21049+
21050+ for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
21051+ if (!h_d[i])
21052+ continue;
21053+ if (d_is_positive(h_d[i])) {
21054+ h_i = d_inode(h_d[i]);
21055+ err = !h_i->i_nlink;
21056+ }
21057+ }
21058+
21059+out:
21060+ return err;
21061+}
21062+
21063+static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
21064+{
21065+ atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
21066+}
21067+
21068+void au_pin_hdir_acquire_nest(struct au_pin *p)
21069+{
21070+ if (p->hdir) {
21071+ rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
21072+ p->lsc_hi, 0, NULL, _RET_IP_);
21073+ au_pin_hdir_set_owner(p, current);
21074+ }
21075+}
21076+
21077+void au_pin_hdir_release(struct au_pin *p)
21078+{
21079+ if (p->hdir) {
21080+ au_pin_hdir_set_owner(p, p->task);
21081+ rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, _RET_IP_);
21082+ }
21083+}
21084+
21085+struct dentry *au_pinned_h_parent(struct au_pin *pin)
21086+{
21087+ if (pin && pin->parent)
21088+ return au_h_dptr(pin->parent, pin->bindex);
21089+ return NULL;
21090+}
21091+
21092+void au_unpin(struct au_pin *p)
21093+{
21094+ if (p->hdir)
21095+ au_pin_hdir_unlock(p);
21096+ if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
21097+ vfsub_mnt_drop_write(p->h_mnt);
21098+ if (!p->hdir)
21099+ return;
21100+
21101+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21102+ di_read_unlock(p->parent, AuLock_IR);
21103+ iput(p->hdir->hi_inode);
21104+ dput(p->parent);
21105+ p->parent = NULL;
21106+ p->hdir = NULL;
21107+ p->h_mnt = NULL;
21108+ /* do not clear p->task */
21109+}
21110+
21111+int au_do_pin(struct au_pin *p)
21112+{
21113+ int err;
21114+ struct super_block *sb;
21115+ struct inode *h_dir;
21116+
21117+ err = 0;
21118+ sb = p->dentry->d_sb;
21119+ p->br = au_sbr(sb, p->bindex);
21120+ if (IS_ROOT(p->dentry)) {
21121+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
21122+ p->h_mnt = au_br_mnt(p->br);
21123+ err = vfsub_mnt_want_write(p->h_mnt);
21124+ if (unlikely(err)) {
21125+ au_fclr_pin(p->flags, MNT_WRITE);
21126+ goto out_err;
21127+ }
21128+ }
21129+ goto out;
21130+ }
21131+
21132+ p->h_dentry = NULL;
21133+ if (p->bindex <= au_dbbot(p->dentry))
21134+ p->h_dentry = au_h_dptr(p->dentry, p->bindex);
21135+
21136+ p->parent = dget_parent(p->dentry);
21137+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21138+ di_read_lock(p->parent, AuLock_IR, p->lsc_di);
21139+
21140+ h_dir = NULL;
21141+ p->h_parent = au_h_dptr(p->parent, p->bindex);
21142+ p->hdir = au_hi(d_inode(p->parent), p->bindex);
21143+ if (p->hdir)
21144+ h_dir = p->hdir->hi_inode;
21145+
21146+ /*
21147+ * udba case, or
21148+ * if DI_LOCKED is not set, then p->parent may be different
21149+ * and h_parent can be NULL.
21150+ */
21151+ if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
21152+ err = -EBUSY;
21153+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21154+ di_read_unlock(p->parent, AuLock_IR);
21155+ dput(p->parent);
21156+ p->parent = NULL;
21157+ goto out_err;
21158+ }
21159+
21160+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
21161+ p->h_mnt = au_br_mnt(p->br);
21162+ err = vfsub_mnt_want_write(p->h_mnt);
21163+ if (unlikely(err)) {
21164+ au_fclr_pin(p->flags, MNT_WRITE);
21165+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21166+ di_read_unlock(p->parent, AuLock_IR);
21167+ dput(p->parent);
21168+ p->parent = NULL;
21169+ goto out_err;
21170+ }
21171+ }
21172+
21173+ au_igrab(h_dir);
21174+ err = au_pin_hdir_lock(p);
21175+ if (!err)
21176+ goto out; /* success */
21177+
21178+ au_unpin(p);
21179+
21180+out_err:
21181+ pr_err("err %d\n", err);
21182+ err = au_busy_or_stale();
21183+out:
21184+ return err;
21185+}
21186+
21187+void au_pin_init(struct au_pin *p, struct dentry *dentry,
21188+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
21189+ unsigned int udba, unsigned char flags)
21190+{
21191+ p->dentry = dentry;
21192+ p->udba = udba;
21193+ p->lsc_di = lsc_di;
21194+ p->lsc_hi = lsc_hi;
21195+ p->flags = flags;
21196+ p->bindex = bindex;
21197+
21198+ p->parent = NULL;
21199+ p->hdir = NULL;
21200+ p->h_mnt = NULL;
21201+
21202+ p->h_dentry = NULL;
21203+ p->h_parent = NULL;
21204+ p->br = NULL;
21205+ p->task = current;
21206+}
21207+
21208+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
21209+ unsigned int udba, unsigned char flags)
21210+{
21211+ au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
21212+ udba, flags);
21213+ return au_do_pin(pin);
21214+}
21215+
21216+/* ---------------------------------------------------------------------- */
21217+
21218+/*
21219+ * ->setattr() and ->getattr() are called in various cases.
21220+ * chmod, stat: dentry is revalidated.
21221+ * fchmod, fstat: file and dentry are not revalidated, additionally they may be
21222+ * unhashed.
21223+ * for ->setattr(), ia->ia_file is passed from ftruncate only.
21224+ */
21225+/* todo: consolidate with do_refresh() and simple_reval_dpath() */
21226+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
21227+{
21228+ int err;
21229+ struct dentry *parent;
21230+
21231+ err = 0;
21232+ if (au_digen_test(dentry, sigen)) {
21233+ parent = dget_parent(dentry);
21234+ di_read_lock_parent(parent, AuLock_IR);
21235+ err = au_refresh_dentry(dentry, parent);
21236+ di_read_unlock(parent, AuLock_IR);
21237+ dput(parent);
21238+ }
21239+
21240+ AuTraceErr(err);
21241+ return err;
21242+}
21243+
21244+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
21245+ struct au_icpup_args *a)
21246+{
21247+ int err;
21248+ loff_t sz;
21249+ aufs_bindex_t btop, ibtop;
21250+ struct dentry *hi_wh, *parent;
21251+ struct inode *inode;
21252+ struct au_wr_dir_args wr_dir_args = {
21253+ .force_btgt = -1,
21254+ .flags = 0
21255+ };
21256+
21257+ if (d_is_dir(dentry))
21258+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
21259+ /* plink or hi_wh() case */
21260+ btop = au_dbtop(dentry);
21261+ inode = d_inode(dentry);
21262+ ibtop = au_ibtop(inode);
21263+ if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
21264+ wr_dir_args.force_btgt = ibtop;
21265+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21266+ if (unlikely(err < 0))
21267+ goto out;
21268+ a->btgt = err;
21269+ if (err != btop)
21270+ au_fset_icpup(a->flags, DID_CPUP);
21271+
21272+ err = 0;
21273+ a->pin_flags = AuPin_MNT_WRITE;
21274+ parent = NULL;
21275+ if (!IS_ROOT(dentry)) {
21276+ au_fset_pin(a->pin_flags, DI_LOCKED);
21277+ parent = dget_parent(dentry);
21278+ di_write_lock_parent(parent);
21279+ }
21280+
21281+ err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
21282+ if (unlikely(err))
21283+ goto out_parent;
21284+
21285+ sz = -1;
21286+ a->h_path.dentry = au_h_dptr(dentry, btop);
21287+ a->h_inode = d_inode(a->h_path.dentry);
21288+ if (ia && (ia->ia_valid & ATTR_SIZE)) {
21289+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
21290+ if (ia->ia_size < i_size_read(a->h_inode))
21291+ sz = ia->ia_size;
21292+ inode_unlock_shared(a->h_inode);
21293+ }
21294+
21295+ hi_wh = NULL;
21296+ if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
21297+ hi_wh = au_hi_wh(inode, a->btgt);
21298+ if (!hi_wh) {
21299+ struct au_cp_generic cpg = {
21300+ .dentry = dentry,
21301+ .bdst = a->btgt,
21302+ .bsrc = -1,
21303+ .len = sz,
21304+ .pin = &a->pin
21305+ };
21306+ err = au_sio_cpup_wh(&cpg, /*file*/NULL);
21307+ if (unlikely(err))
21308+ goto out_unlock;
21309+ hi_wh = au_hi_wh(inode, a->btgt);
21310+ /* todo: revalidate hi_wh? */
21311+ }
21312+ }
21313+
21314+ if (parent) {
21315+ au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
21316+ di_downgrade_lock(parent, AuLock_IR);
21317+ dput(parent);
21318+ parent = NULL;
21319+ }
21320+ if (!au_ftest_icpup(a->flags, DID_CPUP))
21321+ goto out; /* success */
21322+
21323+ if (!d_unhashed(dentry)) {
21324+ struct au_cp_generic cpg = {
21325+ .dentry = dentry,
21326+ .bdst = a->btgt,
21327+ .bsrc = btop,
21328+ .len = sz,
21329+ .pin = &a->pin,
21330+ .flags = AuCpup_DTIME | AuCpup_HOPEN
21331+ };
21332+ err = au_sio_cpup_simple(&cpg);
21333+ if (!err)
21334+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21335+ } else if (!hi_wh)
21336+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21337+ else
21338+ a->h_path.dentry = hi_wh; /* do not dget here */
21339+
21340+out_unlock:
21341+ a->h_inode = d_inode(a->h_path.dentry);
21342+ if (!err)
21343+ goto out; /* success */
21344+ au_unpin(&a->pin);
21345+out_parent:
21346+ if (parent) {
21347+ di_write_unlock(parent);
21348+ dput(parent);
21349+ }
21350+out:
21351+ if (!err)
21352+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21353+ return err;
21354+}
21355+
21356+static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
21357+{
21358+ int err;
21359+ struct inode *inode, *delegated;
21360+ struct super_block *sb;
21361+ struct file *file;
21362+ struct au_icpup_args *a;
21363+
21364+ inode = d_inode(dentry);
21365+ IMustLock(inode);
21366+
21367+ err = setattr_prepare(dentry, ia);
21368+ if (unlikely(err))
21369+ goto out;
21370+
21371+ err = -ENOMEM;
21372+ a = kzalloc(sizeof(*a), GFP_NOFS);
21373+ if (unlikely(!a))
21374+ goto out;
21375+
21376+ if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
21377+ ia->ia_valid &= ~ATTR_MODE;
21378+
21379+ file = NULL;
21380+ sb = dentry->d_sb;
21381+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21382+ if (unlikely(err))
21383+ goto out_kfree;
21384+
21385+ if (ia->ia_valid & ATTR_FILE) {
21386+ /* currently ftruncate(2) only */
21387+ AuDebugOn(!d_is_reg(dentry));
21388+ file = ia->ia_file;
21389+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
21390+ /*fi_lsc*/0);
21391+ if (unlikely(err))
21392+ goto out_si;
21393+ ia->ia_file = au_hf_top(file);
21394+ a->udba = AuOpt_UDBA_NONE;
21395+ } else {
21396+ /* fchmod() doesn't pass ia_file */
21397+ a->udba = au_opt_udba(sb);
21398+ di_write_lock_child(dentry);
21399+ /* no d_unlinked(), to set UDBA_NONE for root */
21400+ if (d_unhashed(dentry))
21401+ a->udba = AuOpt_UDBA_NONE;
21402+ if (a->udba != AuOpt_UDBA_NONE) {
21403+ AuDebugOn(IS_ROOT(dentry));
21404+ err = au_reval_for_attr(dentry, au_sigen(sb));
21405+ if (unlikely(err))
21406+ goto out_dentry;
21407+ }
21408+ }
21409+
21410+ err = au_pin_and_icpup(dentry, ia, a);
21411+ if (unlikely(err < 0))
21412+ goto out_dentry;
21413+ if (au_ftest_icpup(a->flags, DID_CPUP)) {
21414+ ia->ia_file = NULL;
21415+ ia->ia_valid &= ~ATTR_FILE;
21416+ }
21417+
21418+ a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
21419+ if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
21420+ == (ATTR_MODE | ATTR_CTIME)) {
21421+ err = security_path_chmod(&a->h_path, ia->ia_mode);
21422+ if (unlikely(err))
21423+ goto out_unlock;
21424+ } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
21425+ && (ia->ia_valid & ATTR_CTIME)) {
21426+ err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
21427+ if (unlikely(err))
21428+ goto out_unlock;
21429+ }
21430+
21431+ if (ia->ia_valid & ATTR_SIZE) {
21432+ struct file *f;
21433+
21434+ if (ia->ia_size < i_size_read(inode))
21435+ /* unmap only */
21436+ truncate_setsize(inode, ia->ia_size);
21437+
21438+ f = NULL;
21439+ if (ia->ia_valid & ATTR_FILE)
21440+ f = ia->ia_file;
21441+ inode_unlock(a->h_inode);
21442+ err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
21443+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21444+ } else {
21445+ delegated = NULL;
21446+ while (1) {
21447+ err = vfsub_notify_change(&a->h_path, ia, &delegated);
21448+ if (delegated) {
21449+ err = break_deleg_wait(&delegated);
21450+ if (!err)
21451+ continue;
21452+ }
21453+ break;
21454+ }
21455+ }
21456+ /*
21457+ * regardless aufs 'acl' option setting.
21458+ * why don't all acl-aware fs call this func from their ->setattr()?
21459+ */
21460+ if (!err && (ia->ia_valid & ATTR_MODE))
21461+ err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
21462+ if (!err)
21463+ au_cpup_attr_changeable(inode);
21464+
21465+out_unlock:
21466+ inode_unlock(a->h_inode);
21467+ au_unpin(&a->pin);
21468+ if (unlikely(err))
21469+ au_update_dbtop(dentry);
21470+out_dentry:
21471+ di_write_unlock(dentry);
21472+ if (file) {
21473+ fi_write_unlock(file);
21474+ ia->ia_file = file;
21475+ ia->ia_valid |= ATTR_FILE;
21476+ }
21477+out_si:
21478+ si_read_unlock(sb);
21479+out_kfree:
21480+ au_kfree_rcu(a);
21481+out:
21482+ AuTraceErr(err);
21483+ return err;
21484+}
21485+
21486+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
21487+static int au_h_path_to_set_attr(struct dentry *dentry,
21488+ struct au_icpup_args *a, struct path *h_path)
21489+{
21490+ int err;
21491+ struct super_block *sb;
21492+
21493+ sb = dentry->d_sb;
21494+ a->udba = au_opt_udba(sb);
21495+ /* no d_unlinked(), to set UDBA_NONE for root */
21496+ if (d_unhashed(dentry))
21497+ a->udba = AuOpt_UDBA_NONE;
21498+ if (a->udba != AuOpt_UDBA_NONE) {
21499+ AuDebugOn(IS_ROOT(dentry));
21500+ err = au_reval_for_attr(dentry, au_sigen(sb));
21501+ if (unlikely(err))
21502+ goto out;
21503+ }
21504+ err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
21505+ if (unlikely(err < 0))
21506+ goto out;
21507+
21508+ h_path->dentry = a->h_path.dentry;
21509+ h_path->mnt = au_sbr_mnt(sb, a->btgt);
21510+
21511+out:
21512+ return err;
21513+}
21514+
21515+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
21516+ struct au_sxattr *arg)
21517+{
21518+ int err;
21519+ struct path h_path;
21520+ struct super_block *sb;
21521+ struct au_icpup_args *a;
21522+ struct inode *h_inode;
21523+
21524+ IMustLock(inode);
21525+
21526+ err = -ENOMEM;
21527+ a = kzalloc(sizeof(*a), GFP_NOFS);
21528+ if (unlikely(!a))
21529+ goto out;
21530+
21531+ sb = dentry->d_sb;
21532+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21533+ if (unlikely(err))
21534+ goto out_kfree;
21535+
21536+ h_path.dentry = NULL; /* silence gcc */
21537+ di_write_lock_child(dentry);
21538+ err = au_h_path_to_set_attr(dentry, a, &h_path);
21539+ if (unlikely(err))
21540+ goto out_di;
21541+
21542+ inode_unlock(a->h_inode);
21543+ switch (arg->type) {
21544+ case AU_XATTR_SET:
21545+ AuDebugOn(d_is_negative(h_path.dentry));
21546+ err = vfsub_setxattr(h_path.dentry,
21547+ arg->u.set.name, arg->u.set.value,
21548+ arg->u.set.size, arg->u.set.flags);
21549+ break;
21550+ case AU_ACL_SET:
21551+ err = -EOPNOTSUPP;
21552+ h_inode = d_inode(h_path.dentry);
21553+ if (h_inode->i_op->set_acl)
21554+ /* this will call posix_acl_update_mode */
21555+ err = h_inode->i_op->set_acl(h_inode,
21556+ arg->u.acl_set.acl,
21557+ arg->u.acl_set.type);
21558+ break;
21559+ }
21560+ if (!err)
21561+ au_cpup_attr_timesizes(inode);
21562+
21563+ au_unpin(&a->pin);
21564+ if (unlikely(err))
21565+ au_update_dbtop(dentry);
21566+
21567+out_di:
21568+ di_write_unlock(dentry);
21569+ si_read_unlock(sb);
21570+out_kfree:
21571+ au_kfree_rcu(a);
21572+out:
21573+ AuTraceErr(err);
21574+ return err;
21575+}
21576+#endif
21577+
21578+static void au_refresh_iattr(struct inode *inode, struct kstat *st,
21579+ unsigned int nlink)
21580+{
21581+ unsigned int n;
21582+
21583+ inode->i_mode = st->mode;
21584+ /* don't i_[ug]id_write() here */
21585+ inode->i_uid = st->uid;
21586+ inode->i_gid = st->gid;
21587+ inode->i_atime = st->atime;
21588+ inode->i_mtime = st->mtime;
21589+ inode->i_ctime = st->ctime;
21590+
21591+ au_cpup_attr_nlink(inode, /*force*/0);
21592+ if (S_ISDIR(inode->i_mode)) {
21593+ n = inode->i_nlink;
21594+ n -= nlink;
21595+ n += st->nlink;
21596+ smp_mb(); /* for i_nlink */
21597+ /* 0 can happen */
21598+ set_nlink(inode, n);
21599+ }
21600+
21601+ spin_lock(&inode->i_lock);
21602+ inode->i_blocks = st->blocks;
21603+ i_size_write(inode, st->size);
21604+ spin_unlock(&inode->i_lock);
21605+}
21606+
21607+/*
21608+ * common routine for aufs_getattr() and au_getxattr().
21609+ * returns zero or negative (an error).
21610+ * @dentry will be read-locked in success.
21611+ */
21612+int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
21613+ struct path *h_path, int locked)
21614+{
21615+ int err;
21616+ unsigned int mnt_flags, sigen;
21617+ unsigned char udba_none;
21618+ aufs_bindex_t bindex;
21619+ struct super_block *sb, *h_sb;
21620+
21621+ h_path->mnt = NULL;
21622+ h_path->dentry = NULL;
21623+
21624+ err = 0;
21625+ sb = dentry->d_sb;
21626+ mnt_flags = au_mntflags(sb);
21627+ udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
21628+
21629+ if (unlikely(locked))
21630+ goto body; /* skip locking dinfo */
21631+
21632+ /* support fstat(2) */
21633+ if (!d_unlinked(dentry) && !udba_none) {
21634+ sigen = au_sigen(sb);
21635+ err = au_digen_test(dentry, sigen);
21636+ if (!err) {
21637+ di_read_lock_child(dentry, AuLock_IR);
21638+ err = au_dbrange_test(dentry);
21639+ if (unlikely(err)) {
21640+ di_read_unlock(dentry, AuLock_IR);
21641+ goto out;
21642+ }
21643+ } else {
21644+ AuDebugOn(IS_ROOT(dentry));
21645+ di_write_lock_child(dentry);
21646+ err = au_dbrange_test(dentry);
21647+ if (!err)
21648+ err = au_reval_for_attr(dentry, sigen);
21649+ if (!err)
21650+ di_downgrade_lock(dentry, AuLock_IR);
21651+ else {
21652+ di_write_unlock(dentry);
21653+ goto out;
21654+ }
21655+ }
21656+ } else
21657+ di_read_lock_child(dentry, AuLock_IR);
21658+
21659+body:
21660+ if (!inode) {
21661+ inode = d_inode(dentry);
21662+ if (unlikely(!inode))
21663+ goto out;
21664+ }
21665+ bindex = au_ibtop(inode);
21666+ h_path->mnt = au_sbr_mnt(sb, bindex);
21667+ h_sb = h_path->mnt->mnt_sb;
21668+ if (!force
21669+ && !au_test_fs_bad_iattr(h_sb)
21670+ && udba_none)
21671+ goto out; /* success */
21672+
21673+ if (au_dbtop(dentry) == bindex)
21674+ h_path->dentry = au_h_dptr(dentry, bindex);
21675+ else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
21676+ h_path->dentry = au_plink_lkup(inode, bindex);
21677+ if (IS_ERR(h_path->dentry))
21678+ /* pretending success */
21679+ h_path->dentry = NULL;
21680+ else
21681+ dput(h_path->dentry);
21682+ }
21683+
21684+out:
21685+ return err;
21686+}
21687+
21688+static int aufs_getattr(const struct path *path, struct kstat *st,
21689+ u32 request, unsigned int query)
21690+{
21691+ int err;
21692+ unsigned char positive;
21693+ struct path h_path;
21694+ struct dentry *dentry;
21695+ struct inode *inode;
21696+ struct super_block *sb;
21697+
21698+ dentry = path->dentry;
21699+ inode = d_inode(dentry);
21700+ sb = dentry->d_sb;
21701+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21702+ if (unlikely(err))
21703+ goto out;
21704+ err = au_h_path_getattr(dentry, /*inode*/NULL, /*force*/0, &h_path,
21705+ /*locked*/0);
21706+ if (unlikely(err))
21707+ goto out_si;
21708+ if (unlikely(!h_path.dentry))
21709+ /* illegally overlapped or something */
21710+ goto out_fill; /* pretending success */
21711+
21712+ positive = d_is_positive(h_path.dentry);
21713+ if (positive)
21714+ /* no vfsub version */
21715+ err = vfs_getattr(&h_path, st, request, query);
21716+ if (!err) {
21717+ if (positive)
21718+ au_refresh_iattr(inode, st,
21719+ d_inode(h_path.dentry)->i_nlink);
21720+ goto out_fill; /* success */
21721+ }
21722+ AuTraceErr(err);
21723+ goto out_di;
21724+
21725+out_fill:
21726+ generic_fillattr(inode, st);
21727+out_di:
21728+ di_read_unlock(dentry, AuLock_IR);
21729+out_si:
21730+ si_read_unlock(sb);
21731+out:
21732+ AuTraceErr(err);
21733+ return err;
21734+}
21735+
21736+/* ---------------------------------------------------------------------- */
21737+
21738+static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
21739+ struct delayed_call *done)
21740+{
21741+ const char *ret;
21742+ struct dentry *h_dentry;
21743+ struct inode *h_inode;
21744+ int err;
21745+ aufs_bindex_t bindex;
21746+
21747+ ret = NULL; /* suppress a warning */
21748+ err = -ECHILD;
21749+ if (!dentry)
21750+ goto out;
21751+
21752+ err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
21753+ if (unlikely(err))
21754+ goto out;
21755+
21756+ err = au_d_hashed_positive(dentry);
21757+ if (unlikely(err))
21758+ goto out_unlock;
21759+
21760+ err = -EINVAL;
21761+ inode = d_inode(dentry);
21762+ bindex = au_ibtop(inode);
21763+ h_inode = au_h_iptr(inode, bindex);
21764+ if (unlikely(!h_inode->i_op->get_link))
21765+ goto out_unlock;
21766+
21767+ err = -EBUSY;
21768+ h_dentry = NULL;
21769+ if (au_dbtop(dentry) <= bindex) {
21770+ h_dentry = au_h_dptr(dentry, bindex);
21771+ if (h_dentry)
21772+ dget(h_dentry);
21773+ }
21774+ if (!h_dentry) {
21775+ h_dentry = d_find_any_alias(h_inode);
21776+ if (IS_ERR(h_dentry)) {
21777+ err = PTR_ERR(h_dentry);
21778+ goto out_unlock;
21779+ }
21780+ }
21781+ if (unlikely(!h_dentry))
21782+ goto out_unlock;
21783+
21784+ err = 0;
21785+ AuDbg("%ps\n", h_inode->i_op->get_link);
21786+ AuDbgDentry(h_dentry);
21787+ ret = vfs_get_link(h_dentry, done);
21788+ dput(h_dentry);
21789+ if (IS_ERR(ret))
21790+ err = PTR_ERR(ret);
21791+
21792+out_unlock:
21793+ aufs_read_unlock(dentry, AuLock_IR);
21794+out:
21795+ if (unlikely(err))
21796+ ret = ERR_PTR(err);
21797+ AuTraceErrPtr(ret);
21798+ return ret;
21799+}
21800+
21801+/* ---------------------------------------------------------------------- */
21802+
21803+static int au_is_special(struct inode *inode)
21804+{
21805+ return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
21806+}
21807+
21808+static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
21809+ int flags)
21810+{
21811+ int err;
21812+ aufs_bindex_t bindex;
21813+ struct super_block *sb;
21814+ struct inode *h_inode;
21815+ struct vfsmount *h_mnt;
21816+
21817+ sb = inode->i_sb;
21818+ WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
21819+ "unexpected s_flags 0x%lx", sb->s_flags);
21820+
21821+ /* mmap_sem might be acquired already, cf. aufs_mmap() */
21822+ lockdep_off();
21823+ si_read_lock(sb, AuLock_FLUSH);
21824+ ii_write_lock_child(inode);
21825+
21826+ err = 0;
21827+ bindex = au_ibtop(inode);
21828+ h_inode = au_h_iptr(inode, bindex);
21829+ if (!au_test_ro(sb, bindex, inode)) {
21830+ h_mnt = au_sbr_mnt(sb, bindex);
21831+ err = vfsub_mnt_want_write(h_mnt);
21832+ if (!err) {
21833+ err = vfsub_update_time(h_inode, ts, flags);
21834+ vfsub_mnt_drop_write(h_mnt);
21835+ }
21836+ } else if (au_is_special(h_inode)) {
21837+ /*
21838+ * Never copy-up here.
21839+ * These special files may already be opened and used for
21840+ * communicating. If we copied it up, then the communication
21841+ * would be corrupted.
21842+ */
21843+ AuWarn1("timestamps for i%lu are ignored "
21844+ "since it is on readonly branch (hi%lu).\n",
21845+ inode->i_ino, h_inode->i_ino);
21846+ } else if (flags & ~S_ATIME) {
21847+ err = -EIO;
21848+ AuIOErr1("unexpected flags 0x%x\n", flags);
21849+ AuDebugOn(1);
21850+ }
21851+
21852+ if (!err)
21853+ au_cpup_attr_timesizes(inode);
21854+ ii_write_unlock(inode);
21855+ si_read_unlock(sb);
21856+ lockdep_on();
21857+
21858+ if (!err && (flags & S_VERSION))
21859+ inode_inc_iversion(inode);
21860+
21861+ return err;
21862+}
21863+
21864+/* ---------------------------------------------------------------------- */
21865+
21866+/* no getattr version will be set by module.c:aufs_init() */
21867+struct inode_operations aufs_iop_nogetattr[AuIop_Last],
21868+ aufs_iop[] = {
21869+ [AuIop_SYMLINK] = {
21870+ .permission = aufs_permission,
21871+#ifdef CONFIG_FS_POSIX_ACL
21872+ .get_acl = aufs_get_acl,
21873+ .set_acl = aufs_set_acl, /* unsupport for symlink? */
21874+#endif
21875+
21876+ .setattr = aufs_setattr,
21877+ .getattr = aufs_getattr,
21878+
21879+#ifdef CONFIG_AUFS_XATTR
21880+ .listxattr = aufs_listxattr,
21881+#endif
21882+
21883+ .get_link = aufs_get_link,
21884+
21885+ /* .update_time = aufs_update_time */
21886+ },
21887+ [AuIop_DIR] = {
21888+ .create = aufs_create,
21889+ .lookup = aufs_lookup,
21890+ .link = aufs_link,
21891+ .unlink = aufs_unlink,
21892+ .symlink = aufs_symlink,
21893+ .mkdir = aufs_mkdir,
21894+ .rmdir = aufs_rmdir,
21895+ .mknod = aufs_mknod,
21896+ .rename = aufs_rename,
21897+
21898+ .permission = aufs_permission,
21899+#ifdef CONFIG_FS_POSIX_ACL
21900+ .get_acl = aufs_get_acl,
21901+ .set_acl = aufs_set_acl,
21902+#endif
21903+
21904+ .setattr = aufs_setattr,
21905+ .getattr = aufs_getattr,
21906+
21907+#ifdef CONFIG_AUFS_XATTR
21908+ .listxattr = aufs_listxattr,
21909+#endif
21910+
21911+ .update_time = aufs_update_time,
21912+ .atomic_open = aufs_atomic_open,
21913+ .tmpfile = aufs_tmpfile
21914+ },
21915+ [AuIop_OTHER] = {
21916+ .permission = aufs_permission,
21917+#ifdef CONFIG_FS_POSIX_ACL
21918+ .get_acl = aufs_get_acl,
21919+ .set_acl = aufs_set_acl,
21920+#endif
21921+
21922+ .setattr = aufs_setattr,
21923+ .getattr = aufs_getattr,
21924+
21925+#ifdef CONFIG_AUFS_XATTR
21926+ .listxattr = aufs_listxattr,
21927+#endif
21928+
21929+ .update_time = aufs_update_time
21930+ }
21931+};
21932diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
21933--- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
21934+++ linux/fs/aufs/i_op_del.c 2021-02-22 22:51:59.058078584 +0100
21935@@ -0,0 +1,513 @@
21936+// SPDX-License-Identifier: GPL-2.0
21937+/*
21938+ * Copyright (C) 2005-2020 Junjiro R. Okajima
21939+ *
21940+ * This program, aufs is free software; you can redistribute it and/or modify
21941+ * it under the terms of the GNU General Public License as published by
21942+ * the Free Software Foundation; either version 2 of the License, or
21943+ * (at your option) any later version.
21944+ *
21945+ * This program is distributed in the hope that it will be useful,
21946+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21947+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21948+ * GNU General Public License for more details.
21949+ *
21950+ * You should have received a copy of the GNU General Public License
21951+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
21952+ */
21953+
21954+/*
21955+ * inode operations (del entry)
21956+ */
21957+
21958+#include <linux/iversion.h>
21959+#include "aufs.h"
21960+
21961+/*
21962+ * decide if a new whiteout for @dentry is necessary or not.
21963+ * when it is necessary, prepare the parent dir for the upper branch whose
21964+ * branch index is @bcpup for creation. the actual creation of the whiteout will
21965+ * be done by caller.
21966+ * return value:
21967+ * 0: wh is unnecessary
21968+ * plus: wh is necessary
21969+ * minus: error
21970+ */
21971+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
21972+{
21973+ int need_wh, err;
21974+ aufs_bindex_t btop;
21975+ struct super_block *sb;
21976+
21977+ sb = dentry->d_sb;
21978+ btop = au_dbtop(dentry);
21979+ if (*bcpup < 0) {
21980+ *bcpup = btop;
21981+ if (au_test_ro(sb, btop, d_inode(dentry))) {
21982+ err = AuWbrCopyup(au_sbi(sb), dentry);
21983+ *bcpup = err;
21984+ if (unlikely(err < 0))
21985+ goto out;
21986+ }
21987+ } else
21988+ AuDebugOn(btop < *bcpup
21989+ || au_test_ro(sb, *bcpup, d_inode(dentry)));
21990+ AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
21991+
21992+ if (*bcpup != btop) {
21993+ err = au_cpup_dirs(dentry, *bcpup);
21994+ if (unlikely(err))
21995+ goto out;
21996+ need_wh = 1;
21997+ } else {
21998+ struct au_dinfo *dinfo, *tmp;
21999+
22000+ need_wh = -ENOMEM;
22001+ dinfo = au_di(dentry);
22002+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
22003+ if (tmp) {
22004+ au_di_cp(tmp, dinfo);
22005+ au_di_swap(tmp, dinfo);
22006+ /* returns the number of positive dentries */
22007+ need_wh = au_lkup_dentry(dentry, btop + 1,
22008+ /* AuLkup_IGNORE_PERM */ 0);
22009+ au_di_swap(tmp, dinfo);
22010+ au_rw_write_unlock(&tmp->di_rwsem);
22011+ au_di_free(tmp);
22012+ }
22013+ }
22014+ AuDbg("need_wh %d\n", need_wh);
22015+ err = need_wh;
22016+
22017+out:
22018+ return err;
22019+}
22020+
22021+/*
22022+ * simple tests for the del-entry operations.
22023+ * following the checks in vfs, plus the parent-child relationship.
22024+ */
22025+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
22026+ struct dentry *h_parent, int isdir)
22027+{
22028+ int err;
22029+ umode_t h_mode;
22030+ struct dentry *h_dentry, *h_latest;
22031+ struct inode *h_inode;
22032+
22033+ h_dentry = au_h_dptr(dentry, bindex);
22034+ if (d_really_is_positive(dentry)) {
22035+ err = -ENOENT;
22036+ if (unlikely(d_is_negative(h_dentry)))
22037+ goto out;
22038+ h_inode = d_inode(h_dentry);
22039+ if (unlikely(!h_inode->i_nlink))
22040+ goto out;
22041+
22042+ h_mode = h_inode->i_mode;
22043+ if (!isdir) {
22044+ err = -EISDIR;
22045+ if (unlikely(S_ISDIR(h_mode)))
22046+ goto out;
22047+ } else if (unlikely(!S_ISDIR(h_mode))) {
22048+ err = -ENOTDIR;
22049+ goto out;
22050+ }
22051+ } else {
22052+ /* rename(2) case */
22053+ err = -EIO;
22054+ if (unlikely(d_is_positive(h_dentry)))
22055+ goto out;
22056+ }
22057+
22058+ err = -ENOENT;
22059+ /* expected parent dir is locked */
22060+ if (unlikely(h_parent != h_dentry->d_parent))
22061+ goto out;
22062+ err = 0;
22063+
22064+ /*
22065+ * rmdir a dir may break the consistency on some filesystem.
22066+ * let's try heavy test.
22067+ */
22068+ err = -EACCES;
22069+ if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
22070+ && au_test_h_perm(d_inode(h_parent),
22071+ MAY_EXEC | MAY_WRITE)))
22072+ goto out;
22073+
22074+ h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
22075+ err = -EIO;
22076+ if (IS_ERR(h_latest))
22077+ goto out;
22078+ if (h_latest == h_dentry)
22079+ err = 0;
22080+ dput(h_latest);
22081+
22082+out:
22083+ return err;
22084+}
22085+
22086+/*
22087+ * decide the branch where we operate for @dentry. the branch index will be set
22088+ * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
22089+ * dir for reverting.
22090+ * when a new whiteout is necessary, create it.
22091+ */
22092+static struct dentry*
22093+lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
22094+ struct au_dtime *dt, struct au_pin *pin)
22095+{
22096+ struct dentry *wh_dentry;
22097+ struct super_block *sb;
22098+ struct path h_path;
22099+ int err, need_wh;
22100+ unsigned int udba;
22101+ aufs_bindex_t bcpup;
22102+
22103+ need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
22104+ wh_dentry = ERR_PTR(need_wh);
22105+ if (unlikely(need_wh < 0))
22106+ goto out;
22107+
22108+ sb = dentry->d_sb;
22109+ udba = au_opt_udba(sb);
22110+ bcpup = *rbcpup;
22111+ err = au_pin(pin, dentry, bcpup, udba,
22112+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22113+ wh_dentry = ERR_PTR(err);
22114+ if (unlikely(err))
22115+ goto out;
22116+
22117+ h_path.dentry = au_pinned_h_parent(pin);
22118+ if (udba != AuOpt_UDBA_NONE
22119+ && au_dbtop(dentry) == bcpup) {
22120+ err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
22121+ wh_dentry = ERR_PTR(err);
22122+ if (unlikely(err))
22123+ goto out_unpin;
22124+ }
22125+
22126+ h_path.mnt = au_sbr_mnt(sb, bcpup);
22127+ au_dtime_store(dt, au_pinned_parent(pin), &h_path);
22128+ wh_dentry = NULL;
22129+ if (!need_wh)
22130+ goto out; /* success, no need to create whiteout */
22131+
22132+ wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
22133+ if (IS_ERR(wh_dentry))
22134+ goto out_unpin;
22135+
22136+ /* returns with the parent is locked and wh_dentry is dget-ed */
22137+ goto out; /* success */
22138+
22139+out_unpin:
22140+ au_unpin(pin);
22141+out:
22142+ return wh_dentry;
22143+}
22144+
22145+/*
22146+ * when removing a dir, rename it to a unique temporary whiteout-ed name first
22147+ * in order to be revertible and save time for removing many child whiteouts
22148+ * under the dir.
22149+ * returns 1 when there are too many child whiteout and caller should remove
22150+ * them asynchronously. returns 0 when the number of children is enough small to
22151+ * remove now or the branch fs is a remote fs.
22152+ * otherwise return an error.
22153+ */
22154+static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
22155+ struct au_nhash *whlist, struct inode *dir)
22156+{
22157+ int rmdir_later, err, dirwh;
22158+ struct dentry *h_dentry;
22159+ struct super_block *sb;
22160+ struct inode *inode;
22161+
22162+ sb = dentry->d_sb;
22163+ SiMustAnyLock(sb);
22164+ h_dentry = au_h_dptr(dentry, bindex);
22165+ err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
22166+ if (unlikely(err))
22167+ goto out;
22168+
22169+ /* stop monitoring */
22170+ inode = d_inode(dentry);
22171+ au_hn_free(au_hi(inode, bindex));
22172+
22173+ if (!au_test_fs_remote(h_dentry->d_sb)) {
22174+ dirwh = au_sbi(sb)->si_dirwh;
22175+ rmdir_later = (dirwh <= 1);
22176+ if (!rmdir_later)
22177+ rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
22178+ dirwh);
22179+ if (rmdir_later)
22180+ return rmdir_later;
22181+ }
22182+
22183+ err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
22184+ if (unlikely(err)) {
22185+ AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
22186+ h_dentry, bindex, err);
22187+ err = 0;
22188+ }
22189+
22190+out:
22191+ AuTraceErr(err);
22192+ return err;
22193+}
22194+
22195+/*
22196+ * final procedure for deleting a entry.
22197+ * maintain dentry and iattr.
22198+ */
22199+static void epilog(struct inode *dir, struct dentry *dentry,
22200+ aufs_bindex_t bindex)
22201+{
22202+ struct inode *inode;
22203+
22204+ inode = d_inode(dentry);
22205+ d_drop(dentry);
22206+ inode->i_ctime = dir->i_ctime;
22207+
22208+ au_dir_ts(dir, bindex);
22209+ inode_inc_iversion(dir);
22210+}
22211+
22212+/*
22213+ * when an error happened, remove the created whiteout and revert everything.
22214+ */
22215+static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
22216+ aufs_bindex_t bwh, struct dentry *wh_dentry,
22217+ struct dentry *dentry, struct au_dtime *dt)
22218+{
22219+ int rerr;
22220+ struct path h_path = {
22221+ .dentry = wh_dentry,
22222+ .mnt = au_sbr_mnt(dir->i_sb, bindex)
22223+ };
22224+
22225+ rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
22226+ if (!rerr) {
22227+ au_set_dbwh(dentry, bwh);
22228+ au_dtime_revert(dt);
22229+ return 0;
22230+ }
22231+
22232+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
22233+ return -EIO;
22234+}
22235+
22236+/* ---------------------------------------------------------------------- */
22237+
22238+int aufs_unlink(struct inode *dir, struct dentry *dentry)
22239+{
22240+ int err;
22241+ aufs_bindex_t bwh, bindex, btop;
22242+ struct inode *inode, *h_dir, *delegated;
22243+ struct dentry *parent, *wh_dentry;
22244+ /* to reduce stack size */
22245+ struct {
22246+ struct au_dtime dt;
22247+ struct au_pin pin;
22248+ struct path h_path;
22249+ } *a;
22250+
22251+ IMustLock(dir);
22252+
22253+ err = -ENOMEM;
22254+ a = kmalloc(sizeof(*a), GFP_NOFS);
22255+ if (unlikely(!a))
22256+ goto out;
22257+
22258+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
22259+ if (unlikely(err))
22260+ goto out_free;
22261+ err = au_d_hashed_positive(dentry);
22262+ if (unlikely(err))
22263+ goto out_unlock;
22264+ inode = d_inode(dentry);
22265+ IMustLock(inode);
22266+ err = -EISDIR;
22267+ if (unlikely(d_is_dir(dentry)))
22268+ goto out_unlock; /* possible? */
22269+
22270+ btop = au_dbtop(dentry);
22271+ bwh = au_dbwh(dentry);
22272+ bindex = -1;
22273+ parent = dentry->d_parent; /* dir inode is locked */
22274+ di_write_lock_parent(parent);
22275+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
22276+ &a->pin);
22277+ err = PTR_ERR(wh_dentry);
22278+ if (IS_ERR(wh_dentry))
22279+ goto out_parent;
22280+
22281+ a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
22282+ a->h_path.dentry = au_h_dptr(dentry, btop);
22283+ dget(a->h_path.dentry);
22284+ if (bindex == btop) {
22285+ h_dir = au_pinned_h_dir(&a->pin);
22286+ delegated = NULL;
22287+ err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
22288+ if (unlikely(err == -EWOULDBLOCK)) {
22289+ pr_warn("cannot retry for NFSv4 delegation"
22290+ " for an internal unlink\n");
22291+ iput(delegated);
22292+ }
22293+ } else {
22294+ /* dir inode is locked */
22295+ h_dir = d_inode(wh_dentry->d_parent);
22296+ IMustLock(h_dir);
22297+ err = 0;
22298+ }
22299+
22300+ if (!err) {
22301+ vfsub_drop_nlink(inode);
22302+ epilog(dir, dentry, bindex);
22303+
22304+ /* update target timestamps */
22305+ if (bindex == btop) {
22306+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
22307+ /*ignore*/
22308+ inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22309+ } else
22310+ /* todo: this timestamp may be reverted later */
22311+ inode->i_ctime = h_dir->i_ctime;
22312+ goto out_unpin; /* success */
22313+ }
22314+
22315+ /* revert */
22316+ if (wh_dentry) {
22317+ int rerr;
22318+
22319+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22320+ &a->dt);
22321+ if (rerr)
22322+ err = rerr;
22323+ }
22324+
22325+out_unpin:
22326+ au_unpin(&a->pin);
22327+ dput(wh_dentry);
22328+ dput(a->h_path.dentry);
22329+out_parent:
22330+ di_write_unlock(parent);
22331+out_unlock:
22332+ aufs_read_unlock(dentry, AuLock_DW);
22333+out_free:
22334+ au_kfree_rcu(a);
22335+out:
22336+ return err;
22337+}
22338+
22339+int aufs_rmdir(struct inode *dir, struct dentry *dentry)
22340+{
22341+ int err, rmdir_later;
22342+ aufs_bindex_t bwh, bindex, btop;
22343+ struct inode *inode;
22344+ struct dentry *parent, *wh_dentry, *h_dentry;
22345+ struct au_whtmp_rmdir *args;
22346+ /* to reduce stack size */
22347+ struct {
22348+ struct au_dtime dt;
22349+ struct au_pin pin;
22350+ } *a;
22351+
22352+ IMustLock(dir);
22353+
22354+ err = -ENOMEM;
22355+ a = kmalloc(sizeof(*a), GFP_NOFS);
22356+ if (unlikely(!a))
22357+ goto out;
22358+
22359+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22360+ if (unlikely(err))
22361+ goto out_free;
22362+ err = au_alive_dir(dentry);
22363+ if (unlikely(err))
22364+ goto out_unlock;
22365+ inode = d_inode(dentry);
22366+ IMustLock(inode);
22367+ err = -ENOTDIR;
22368+ if (unlikely(!d_is_dir(dentry)))
22369+ goto out_unlock; /* possible? */
22370+
22371+ err = -ENOMEM;
22372+ args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
22373+ if (unlikely(!args))
22374+ goto out_unlock;
22375+
22376+ parent = dentry->d_parent; /* dir inode is locked */
22377+ di_write_lock_parent(parent);
22378+ err = au_test_empty(dentry, &args->whlist);
22379+ if (unlikely(err))
22380+ goto out_parent;
22381+
22382+ btop = au_dbtop(dentry);
22383+ bwh = au_dbwh(dentry);
22384+ bindex = -1;
22385+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
22386+ &a->pin);
22387+ err = PTR_ERR(wh_dentry);
22388+ if (IS_ERR(wh_dentry))
22389+ goto out_parent;
22390+
22391+ h_dentry = au_h_dptr(dentry, btop);
22392+ dget(h_dentry);
22393+ rmdir_later = 0;
22394+ if (bindex == btop) {
22395+ err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
22396+ if (err > 0) {
22397+ rmdir_later = err;
22398+ err = 0;
22399+ }
22400+ } else {
22401+ /* stop monitoring */
22402+ au_hn_free(au_hi(inode, btop));
22403+
22404+ /* dir inode is locked */
22405+ IMustLock(d_inode(wh_dentry->d_parent));
22406+ err = 0;
22407+ }
22408+
22409+ if (!err) {
22410+ vfsub_dead_dir(inode);
22411+ au_set_dbdiropq(dentry, -1);
22412+ epilog(dir, dentry, bindex);
22413+
22414+ if (rmdir_later) {
22415+ au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
22416+ args = NULL;
22417+ }
22418+
22419+ goto out_unpin; /* success */
22420+ }
22421+
22422+ /* revert */
22423+ AuLabel(revert);
22424+ if (wh_dentry) {
22425+ int rerr;
22426+
22427+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22428+ &a->dt);
22429+ if (rerr)
22430+ err = rerr;
22431+ }
22432+
22433+out_unpin:
22434+ au_unpin(&a->pin);
22435+ dput(wh_dentry);
22436+ dput(h_dentry);
22437+out_parent:
22438+ di_write_unlock(parent);
22439+ if (args)
22440+ au_whtmp_rmdir_free(args);
22441+out_unlock:
22442+ aufs_read_unlock(dentry, AuLock_DW);
22443+out_free:
22444+ au_kfree_rcu(a);
22445+out:
22446+ AuTraceErr(err);
22447+ return err;
22448+}
22449diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
22450--- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
22451+++ linux/fs/aufs/i_op_ren.c 2021-02-22 22:51:59.061411920 +0100
22452@@ -0,0 +1,1250 @@
22453+// SPDX-License-Identifier: GPL-2.0
22454+/*
22455+ * Copyright (C) 2005-2020 Junjiro R. Okajima
22456+ *
22457+ * This program, aufs is free software; you can redistribute it and/or modify
22458+ * it under the terms of the GNU General Public License as published by
22459+ * the Free Software Foundation; either version 2 of the License, or
22460+ * (at your option) any later version.
22461+ *
22462+ * This program is distributed in the hope that it will be useful,
22463+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22464+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22465+ * GNU General Public License for more details.
22466+ *
22467+ * You should have received a copy of the GNU General Public License
22468+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
22469+ */
22470+
22471+/*
22472+ * inode operation (rename entry)
22473+ * todo: this is crazy monster
22474+ */
22475+
22476+#include <linux/iversion.h>
22477+#include "aufs.h"
22478+
22479+enum { AuSRC, AuDST, AuSrcDst };
22480+enum { AuPARENT, AuCHILD, AuParentChild };
22481+
22482+#define AuRen_ISDIR_SRC 1
22483+#define AuRen_ISDIR_DST (1 << 1)
22484+#define AuRen_ISSAMEDIR (1 << 2)
22485+#define AuRen_WHSRC (1 << 3)
22486+#define AuRen_WHDST (1 << 4)
22487+#define AuRen_MNT_WRITE (1 << 5)
22488+#define AuRen_DT_DSTDIR (1 << 6)
22489+#define AuRen_DIROPQ_SRC (1 << 7)
22490+#define AuRen_DIROPQ_DST (1 << 8)
22491+#define AuRen_DIRREN (1 << 9)
22492+#define AuRen_DROPPED_SRC (1 << 10)
22493+#define AuRen_DROPPED_DST (1 << 11)
22494+#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
22495+#define au_fset_ren(flags, name) \
22496+ do { (flags) |= AuRen_##name; } while (0)
22497+#define au_fclr_ren(flags, name) \
22498+ do { (flags) &= ~AuRen_##name; } while (0)
22499+
22500+#ifndef CONFIG_AUFS_DIRREN
22501+#undef AuRen_DIRREN
22502+#define AuRen_DIRREN 0
22503+#endif
22504+
22505+struct au_ren_args {
22506+ struct {
22507+ struct dentry *dentry, *h_dentry, *parent, *h_parent,
22508+ *wh_dentry;
22509+ struct inode *dir, *inode;
22510+ struct au_hinode *hdir, *hinode;
22511+ struct au_dtime dt[AuParentChild];
22512+ aufs_bindex_t btop, bdiropq;
22513+ } sd[AuSrcDst];
22514+
22515+#define src_dentry sd[AuSRC].dentry
22516+#define src_dir sd[AuSRC].dir
22517+#define src_inode sd[AuSRC].inode
22518+#define src_h_dentry sd[AuSRC].h_dentry
22519+#define src_parent sd[AuSRC].parent
22520+#define src_h_parent sd[AuSRC].h_parent
22521+#define src_wh_dentry sd[AuSRC].wh_dentry
22522+#define src_hdir sd[AuSRC].hdir
22523+#define src_hinode sd[AuSRC].hinode
22524+#define src_h_dir sd[AuSRC].hdir->hi_inode
22525+#define src_dt sd[AuSRC].dt
22526+#define src_btop sd[AuSRC].btop
22527+#define src_bdiropq sd[AuSRC].bdiropq
22528+
22529+#define dst_dentry sd[AuDST].dentry
22530+#define dst_dir sd[AuDST].dir
22531+#define dst_inode sd[AuDST].inode
22532+#define dst_h_dentry sd[AuDST].h_dentry
22533+#define dst_parent sd[AuDST].parent
22534+#define dst_h_parent sd[AuDST].h_parent
22535+#define dst_wh_dentry sd[AuDST].wh_dentry
22536+#define dst_hdir sd[AuDST].hdir
22537+#define dst_hinode sd[AuDST].hinode
22538+#define dst_h_dir sd[AuDST].hdir->hi_inode
22539+#define dst_dt sd[AuDST].dt
22540+#define dst_btop sd[AuDST].btop
22541+#define dst_bdiropq sd[AuDST].bdiropq
22542+
22543+ struct dentry *h_trap;
22544+ struct au_branch *br;
22545+ struct path h_path;
22546+ struct au_nhash whlist;
22547+ aufs_bindex_t btgt, src_bwh;
22548+
22549+ struct {
22550+ unsigned short auren_flags;
22551+ unsigned char flags; /* syscall parameter */
22552+ unsigned char exchange;
22553+ } __packed;
22554+
22555+ struct au_whtmp_rmdir *thargs;
22556+ struct dentry *h_dst;
22557+ struct au_hinode *h_root;
22558+};
22559+
22560+/* ---------------------------------------------------------------------- */
22561+
22562+/*
22563+ * functions for reverting.
22564+ * when an error happened in a single rename systemcall, we should revert
22565+ * everything as if nothing happened.
22566+ * we don't need to revert the copied-up/down the parent dir since they are
22567+ * harmless.
22568+ */
22569+
22570+#define RevertFailure(fmt, ...) do { \
22571+ AuIOErr("revert failure: " fmt " (%d, %d)\n", \
22572+ ##__VA_ARGS__, err, rerr); \
22573+ err = -EIO; \
22574+} while (0)
22575+
22576+static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
22577+{
22578+ int rerr;
22579+ struct dentry *d;
22580+#define src_or_dst(member) a->sd[idx].member
22581+
22582+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22583+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22584+ rerr = au_diropq_remove(d, a->btgt);
22585+ au_hn_inode_unlock(src_or_dst(hinode));
22586+ au_set_dbdiropq(d, src_or_dst(bdiropq));
22587+ if (rerr)
22588+ RevertFailure("remove diropq %pd", d);
22589+
22590+#undef src_or_dst_
22591+}
22592+
22593+static void au_ren_rev_diropq(int err, struct au_ren_args *a)
22594+{
22595+ if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
22596+ au_ren_do_rev_diropq(err, a, AuSRC);
22597+ if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
22598+ au_ren_do_rev_diropq(err, a, AuDST);
22599+}
22600+
22601+static void au_ren_rev_rename(int err, struct au_ren_args *a)
22602+{
22603+ int rerr;
22604+ struct inode *delegated;
22605+
22606+ a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
22607+ a->src_h_parent);
22608+ rerr = PTR_ERR(a->h_path.dentry);
22609+ if (IS_ERR(a->h_path.dentry)) {
22610+ RevertFailure("lkup one %pd", a->src_dentry);
22611+ return;
22612+ }
22613+
22614+ delegated = NULL;
22615+ rerr = vfsub_rename(a->dst_h_dir,
22616+ au_h_dptr(a->src_dentry, a->btgt),
22617+ a->src_h_dir, &a->h_path, &delegated, a->flags);
22618+ if (unlikely(rerr == -EWOULDBLOCK)) {
22619+ pr_warn("cannot retry for NFSv4 delegation"
22620+ " for an internal rename\n");
22621+ iput(delegated);
22622+ }
22623+ d_drop(a->h_path.dentry);
22624+ dput(a->h_path.dentry);
22625+ /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
22626+ if (rerr)
22627+ RevertFailure("rename %pd", a->src_dentry);
22628+}
22629+
22630+static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
22631+{
22632+ int rerr;
22633+ struct inode *delegated;
22634+
22635+ a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
22636+ a->dst_h_parent);
22637+ rerr = PTR_ERR(a->h_path.dentry);
22638+ if (IS_ERR(a->h_path.dentry)) {
22639+ RevertFailure("lkup one %pd", a->dst_dentry);
22640+ return;
22641+ }
22642+ if (d_is_positive(a->h_path.dentry)) {
22643+ d_drop(a->h_path.dentry);
22644+ dput(a->h_path.dentry);
22645+ return;
22646+ }
22647+
22648+ delegated = NULL;
22649+ rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
22650+ &delegated, a->flags);
22651+ if (unlikely(rerr == -EWOULDBLOCK)) {
22652+ pr_warn("cannot retry for NFSv4 delegation"
22653+ " for an internal rename\n");
22654+ iput(delegated);
22655+ }
22656+ d_drop(a->h_path.dentry);
22657+ dput(a->h_path.dentry);
22658+ if (!rerr)
22659+ au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
22660+ else
22661+ RevertFailure("rename %pd", a->h_dst);
22662+}
22663+
22664+static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
22665+{
22666+ int rerr;
22667+
22668+ a->h_path.dentry = a->src_wh_dentry;
22669+ rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
22670+ au_set_dbwh(a->src_dentry, a->src_bwh);
22671+ if (rerr)
22672+ RevertFailure("unlink %pd", a->src_wh_dentry);
22673+}
22674+#undef RevertFailure
22675+
22676+/* ---------------------------------------------------------------------- */
22677+
22678+/*
22679+ * when we have to copyup the renaming entry, do it with the rename-target name
22680+ * in order to minimize the cost (the later actual rename is unnecessary).
22681+ * otherwise rename it on the target branch.
22682+ */
22683+static int au_ren_or_cpup(struct au_ren_args *a)
22684+{
22685+ int err;
22686+ struct dentry *d;
22687+ struct inode *delegated;
22688+
22689+ d = a->src_dentry;
22690+ if (au_dbtop(d) == a->btgt) {
22691+ a->h_path.dentry = a->dst_h_dentry;
22692+ AuDebugOn(au_dbtop(d) != a->btgt);
22693+ delegated = NULL;
22694+ err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
22695+ a->dst_h_dir, &a->h_path, &delegated,
22696+ a->flags);
22697+ if (unlikely(err == -EWOULDBLOCK)) {
22698+ pr_warn("cannot retry for NFSv4 delegation"
22699+ " for an internal rename\n");
22700+ iput(delegated);
22701+ }
22702+ } else
22703+ BUG();
22704+
22705+ if (!err && a->h_dst)
22706+ /* it will be set to dinfo later */
22707+ dget(a->h_dst);
22708+
22709+ return err;
22710+}
22711+
22712+/* cf. aufs_rmdir() */
22713+static int au_ren_del_whtmp(struct au_ren_args *a)
22714+{
22715+ int err;
22716+ struct inode *dir;
22717+
22718+ dir = a->dst_dir;
22719+ SiMustAnyLock(dir->i_sb);
22720+ if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
22721+ au_sbi(dir->i_sb)->si_dirwh)
22722+ || au_test_fs_remote(a->h_dst->d_sb)) {
22723+ err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
22724+ if (unlikely(err))
22725+ pr_warn("failed removing whtmp dir %pd (%d), "
22726+ "ignored.\n", a->h_dst, err);
22727+ } else {
22728+ au_nhash_wh_free(&a->thargs->whlist);
22729+ a->thargs->whlist = a->whlist;
22730+ a->whlist.nh_num = 0;
22731+ au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
22732+ dput(a->h_dst);
22733+ a->thargs = NULL;
22734+ }
22735+
22736+ return 0;
22737+}
22738+
22739+/* make it 'opaque' dir. */
22740+static int au_ren_do_diropq(struct au_ren_args *a, int idx)
22741+{
22742+ int err;
22743+ struct dentry *d, *diropq;
22744+#define src_or_dst(member) a->sd[idx].member
22745+
22746+ err = 0;
22747+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22748+ src_or_dst(bdiropq) = au_dbdiropq(d);
22749+ src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
22750+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22751+ diropq = au_diropq_create(d, a->btgt);
22752+ au_hn_inode_unlock(src_or_dst(hinode));
22753+ if (IS_ERR(diropq))
22754+ err = PTR_ERR(diropq);
22755+ else
22756+ dput(diropq);
22757+
22758+#undef src_or_dst_
22759+ return err;
22760+}
22761+
22762+static int au_ren_diropq(struct au_ren_args *a)
22763+{
22764+ int err;
22765+ unsigned char always;
22766+ struct dentry *d;
22767+
22768+ err = 0;
22769+ d = a->dst_dentry; /* already renamed on the branch */
22770+ always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
22771+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
22772+ && !au_ftest_ren(a->auren_flags, DIRREN)
22773+ && a->btgt != au_dbdiropq(a->src_dentry)
22774+ && (a->dst_wh_dentry
22775+ || a->btgt <= au_dbdiropq(d)
22776+ /* hide the lower to keep xino */
22777+ /* the lowers may not be a dir, but we hide them anyway */
22778+ || a->btgt < au_dbbot(d)
22779+ || always)) {
22780+ AuDbg("here\n");
22781+ err = au_ren_do_diropq(a, AuSRC);
22782+ if (unlikely(err))
22783+ goto out;
22784+ au_fset_ren(a->auren_flags, DIROPQ_SRC);
22785+ }
22786+ if (!a->exchange)
22787+ goto out; /* success */
22788+
22789+ d = a->src_dentry; /* already renamed on the branch */
22790+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22791+ && a->btgt != au_dbdiropq(a->dst_dentry)
22792+ && (a->btgt < au_dbdiropq(d)
22793+ || a->btgt < au_dbbot(d)
22794+ || always)) {
22795+ AuDbgDentry(a->src_dentry);
22796+ AuDbgDentry(a->dst_dentry);
22797+ err = au_ren_do_diropq(a, AuDST);
22798+ if (unlikely(err))
22799+ goto out_rev_src;
22800+ au_fset_ren(a->auren_flags, DIROPQ_DST);
22801+ }
22802+ goto out; /* success */
22803+
22804+out_rev_src:
22805+ AuDbg("err %d, reverting src\n", err);
22806+ au_ren_rev_diropq(err, a);
22807+out:
22808+ return err;
22809+}
22810+
22811+static int do_rename(struct au_ren_args *a)
22812+{
22813+ int err;
22814+ struct dentry *d, *h_d;
22815+
22816+ if (!a->exchange) {
22817+ /* prepare workqueue args for asynchronous rmdir */
22818+ h_d = a->dst_h_dentry;
22819+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22820+ /* && !au_ftest_ren(a->auren_flags, DIRREN) */
22821+ && d_is_positive(h_d)) {
22822+ err = -ENOMEM;
22823+ a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
22824+ GFP_NOFS);
22825+ if (unlikely(!a->thargs))
22826+ goto out;
22827+ a->h_dst = dget(h_d);
22828+ }
22829+
22830+ /* create whiteout for src_dentry */
22831+ if (au_ftest_ren(a->auren_flags, WHSRC)) {
22832+ a->src_bwh = au_dbwh(a->src_dentry);
22833+ AuDebugOn(a->src_bwh >= 0);
22834+ a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
22835+ a->src_h_parent);
22836+ err = PTR_ERR(a->src_wh_dentry);
22837+ if (IS_ERR(a->src_wh_dentry))
22838+ goto out_thargs;
22839+ }
22840+
22841+ /* lookup whiteout for dentry */
22842+ if (au_ftest_ren(a->auren_flags, WHDST)) {
22843+ h_d = au_wh_lkup(a->dst_h_parent,
22844+ &a->dst_dentry->d_name, a->br);
22845+ err = PTR_ERR(h_d);
22846+ if (IS_ERR(h_d))
22847+ goto out_whsrc;
22848+ if (d_is_negative(h_d))
22849+ dput(h_d);
22850+ else
22851+ a->dst_wh_dentry = h_d;
22852+ }
22853+
22854+ /* rename dentry to tmpwh */
22855+ if (a->thargs) {
22856+ err = au_whtmp_ren(a->dst_h_dentry, a->br);
22857+ if (unlikely(err))
22858+ goto out_whdst;
22859+
22860+ d = a->dst_dentry;
22861+ au_set_h_dptr(d, a->btgt, NULL);
22862+ err = au_lkup_neg(d, a->btgt, /*wh*/0);
22863+ if (unlikely(err))
22864+ goto out_whtmp;
22865+ a->dst_h_dentry = au_h_dptr(d, a->btgt);
22866+ }
22867+ }
22868+
22869+ BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
22870+#if 0 /* debugging */
22871+ BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
22872+ && d_is_positive(a->dst_h_dentry)
22873+ && a->src_btop != a->btgt);
22874+#endif
22875+
22876+ /* rename by vfs_rename or cpup */
22877+ err = au_ren_or_cpup(a);
22878+ if (unlikely(err))
22879+ /* leave the copied-up one */
22880+ goto out_whtmp;
22881+
22882+ /* make dir opaque */
22883+ err = au_ren_diropq(a);
22884+ if (unlikely(err))
22885+ goto out_rename;
22886+
22887+ /* update target timestamps */
22888+ if (a->exchange) {
22889+ AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
22890+ a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
22891+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
22892+ a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22893+ }
22894+ AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
22895+ a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
22896+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
22897+ a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22898+
22899+ if (!a->exchange) {
22900+ /* remove whiteout for dentry */
22901+ if (a->dst_wh_dentry) {
22902+ a->h_path.dentry = a->dst_wh_dentry;
22903+ err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
22904+ a->dst_dentry);
22905+ if (unlikely(err))
22906+ goto out_diropq;
22907+ }
22908+
22909+ /* remove whtmp */
22910+ if (a->thargs)
22911+ au_ren_del_whtmp(a); /* ignore this error */
22912+
22913+ au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
22914+ }
22915+ err = 0;
22916+ goto out_success;
22917+
22918+out_diropq:
22919+ au_ren_rev_diropq(err, a);
22920+out_rename:
22921+ au_ren_rev_rename(err, a);
22922+ dput(a->h_dst);
22923+out_whtmp:
22924+ if (a->thargs)
22925+ au_ren_rev_whtmp(err, a);
22926+out_whdst:
22927+ dput(a->dst_wh_dentry);
22928+ a->dst_wh_dentry = NULL;
22929+out_whsrc:
22930+ if (a->src_wh_dentry)
22931+ au_ren_rev_whsrc(err, a);
22932+out_success:
22933+ dput(a->src_wh_dentry);
22934+ dput(a->dst_wh_dentry);
22935+out_thargs:
22936+ if (a->thargs) {
22937+ dput(a->h_dst);
22938+ au_whtmp_rmdir_free(a->thargs);
22939+ a->thargs = NULL;
22940+ }
22941+out:
22942+ return err;
22943+}
22944+
22945+/* ---------------------------------------------------------------------- */
22946+
22947+/*
22948+ * test if @dentry dir can be rename destination or not.
22949+ * success means, it is a logically empty dir.
22950+ */
22951+static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
22952+{
22953+ return au_test_empty(dentry, whlist);
22954+}
22955+
22956+/*
22957+ * test if @a->src_dentry dir can be rename source or not.
22958+ * if it can, return 0.
22959+ * success means,
22960+ * - it is a logically empty dir.
22961+ * - or, it exists on writable branch and has no children including whiteouts
22962+ * on the lower branch unless DIRREN is on.
22963+ */
22964+static int may_rename_srcdir(struct au_ren_args *a)
22965+{
22966+ int err;
22967+ unsigned int rdhash;
22968+ aufs_bindex_t btop, btgt;
22969+ struct dentry *dentry;
22970+ struct super_block *sb;
22971+ struct au_sbinfo *sbinfo;
22972+
22973+ dentry = a->src_dentry;
22974+ sb = dentry->d_sb;
22975+ sbinfo = au_sbi(sb);
22976+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
22977+ au_fset_ren(a->auren_flags, DIRREN);
22978+
22979+ btgt = a->btgt;
22980+ btop = au_dbtop(dentry);
22981+ if (btop != btgt) {
22982+ struct au_nhash whlist;
22983+
22984+ SiMustAnyLock(sb);
22985+ rdhash = sbinfo->si_rdhash;
22986+ if (!rdhash)
22987+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
22988+ dentry));
22989+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
22990+ if (unlikely(err))
22991+ goto out;
22992+ err = au_test_empty(dentry, &whlist);
22993+ au_nhash_wh_free(&whlist);
22994+ goto out;
22995+ }
22996+
22997+ if (btop == au_dbtaildir(dentry))
22998+ return 0; /* success */
22999+
23000+ err = au_test_empty_lower(dentry);
23001+
23002+out:
23003+ if (err == -ENOTEMPTY) {
23004+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23005+ err = 0;
23006+ } else {
23007+ AuWarn1("renaming dir who has child(ren) on multiple "
23008+ "branches, is not supported\n");
23009+ err = -EXDEV;
23010+ }
23011+ }
23012+ return err;
23013+}
23014+
23015+/* side effect: sets whlist and h_dentry */
23016+static int au_ren_may_dir(struct au_ren_args *a)
23017+{
23018+ int err;
23019+ unsigned int rdhash;
23020+ struct dentry *d;
23021+
23022+ d = a->dst_dentry;
23023+ SiMustAnyLock(d->d_sb);
23024+
23025+ err = 0;
23026+ if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
23027+ rdhash = au_sbi(d->d_sb)->si_rdhash;
23028+ if (!rdhash)
23029+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
23030+ err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
23031+ if (unlikely(err))
23032+ goto out;
23033+
23034+ if (!a->exchange) {
23035+ au_set_dbtop(d, a->dst_btop);
23036+ err = may_rename_dstdir(d, &a->whlist);
23037+ au_set_dbtop(d, a->btgt);
23038+ } else
23039+ err = may_rename_srcdir(a);
23040+ }
23041+ a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
23042+ if (unlikely(err))
23043+ goto out;
23044+
23045+ d = a->src_dentry;
23046+ a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
23047+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
23048+ err = may_rename_srcdir(a);
23049+ if (unlikely(err)) {
23050+ au_nhash_wh_free(&a->whlist);
23051+ a->whlist.nh_num = 0;
23052+ }
23053+ }
23054+out:
23055+ return err;
23056+}
23057+
23058+/* ---------------------------------------------------------------------- */
23059+
23060+/*
23061+ * simple tests for rename.
23062+ * following the checks in vfs, plus the parent-child relationship.
23063+ */
23064+static int au_may_ren(struct au_ren_args *a)
23065+{
23066+ int err, isdir;
23067+ struct inode *h_inode;
23068+
23069+ if (a->src_btop == a->btgt) {
23070+ err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
23071+ au_ftest_ren(a->auren_flags, ISDIR_SRC));
23072+ if (unlikely(err))
23073+ goto out;
23074+ err = -EINVAL;
23075+ if (unlikely(a->src_h_dentry == a->h_trap))
23076+ goto out;
23077+ }
23078+
23079+ err = 0;
23080+ if (a->dst_btop != a->btgt)
23081+ goto out;
23082+
23083+ err = -ENOTEMPTY;
23084+ if (unlikely(a->dst_h_dentry == a->h_trap))
23085+ goto out;
23086+
23087+ err = -EIO;
23088+ isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
23089+ if (d_really_is_negative(a->dst_dentry)) {
23090+ if (d_is_negative(a->dst_h_dentry))
23091+ err = au_may_add(a->dst_dentry, a->btgt,
23092+ a->dst_h_parent, isdir);
23093+ } else {
23094+ if (unlikely(d_is_negative(a->dst_h_dentry)))
23095+ goto out;
23096+ h_inode = d_inode(a->dst_h_dentry);
23097+ if (h_inode->i_nlink)
23098+ err = au_may_del(a->dst_dentry, a->btgt,
23099+ a->dst_h_parent, isdir);
23100+ }
23101+
23102+out:
23103+ if (unlikely(err == -ENOENT || err == -EEXIST))
23104+ err = -EIO;
23105+ AuTraceErr(err);
23106+ return err;
23107+}
23108+
23109+/* ---------------------------------------------------------------------- */
23110+
23111+/*
23112+ * locking order
23113+ * (VFS)
23114+ * - src_dir and dir by lock_rename()
23115+ * - inode if exists
23116+ * (aufs)
23117+ * - lock all
23118+ * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
23119+ * + si_read_lock
23120+ * + di_write_lock2_child()
23121+ * + di_write_lock_child()
23122+ * + ii_write_lock_child()
23123+ * + di_write_lock_child2()
23124+ * + ii_write_lock_child2()
23125+ * + src_parent and parent
23126+ * + di_write_lock_parent()
23127+ * + ii_write_lock_parent()
23128+ * + di_write_lock_parent2()
23129+ * + ii_write_lock_parent2()
23130+ * + lower src_dir and dir by vfsub_lock_rename()
23131+ * + verify the every relationships between child and parent. if any
23132+ * of them failed, unlock all and return -EBUSY.
23133+ */
23134+static void au_ren_unlock(struct au_ren_args *a)
23135+{
23136+ vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
23137+ a->dst_h_parent, a->dst_hdir);
23138+ if (au_ftest_ren(a->auren_flags, DIRREN)
23139+ && a->h_root)
23140+ au_hn_inode_unlock(a->h_root);
23141+ if (au_ftest_ren(a->auren_flags, MNT_WRITE))
23142+ vfsub_mnt_drop_write(au_br_mnt(a->br));
23143+}
23144+
23145+static int au_ren_lock(struct au_ren_args *a)
23146+{
23147+ int err;
23148+ unsigned int udba;
23149+
23150+ err = 0;
23151+ a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
23152+ a->src_hdir = au_hi(a->src_dir, a->btgt);
23153+ a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
23154+ a->dst_hdir = au_hi(a->dst_dir, a->btgt);
23155+
23156+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
23157+ if (unlikely(err))
23158+ goto out;
23159+ au_fset_ren(a->auren_flags, MNT_WRITE);
23160+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23161+ struct dentry *root;
23162+ struct inode *dir;
23163+
23164+ /*
23165+ * sbinfo is already locked, so this ii_read_lock is
23166+ * unnecessary. but our debugging feature checks it.
23167+ */
23168+ root = a->src_inode->i_sb->s_root;
23169+ if (root != a->src_parent && root != a->dst_parent) {
23170+ dir = d_inode(root);
23171+ ii_read_lock_parent3(dir);
23172+ a->h_root = au_hi(dir, a->btgt);
23173+ ii_read_unlock(dir);
23174+ au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
23175+ }
23176+ }
23177+ a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
23178+ a->dst_h_parent, a->dst_hdir);
23179+ udba = au_opt_udba(a->src_dentry->d_sb);
23180+ if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
23181+ || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
23182+ err = au_busy_or_stale();
23183+ if (!err && au_dbtop(a->src_dentry) == a->btgt)
23184+ err = au_h_verify(a->src_h_dentry, udba,
23185+ d_inode(a->src_h_parent), a->src_h_parent,
23186+ a->br);
23187+ if (!err && au_dbtop(a->dst_dentry) == a->btgt)
23188+ err = au_h_verify(a->dst_h_dentry, udba,
23189+ d_inode(a->dst_h_parent), a->dst_h_parent,
23190+ a->br);
23191+ if (!err)
23192+ goto out; /* success */
23193+
23194+ err = au_busy_or_stale();
23195+ au_ren_unlock(a);
23196+
23197+out:
23198+ return err;
23199+}
23200+
23201+/* ---------------------------------------------------------------------- */
23202+
23203+static void au_ren_refresh_dir(struct au_ren_args *a)
23204+{
23205+ struct inode *dir;
23206+
23207+ dir = a->dst_dir;
23208+ inode_inc_iversion(dir);
23209+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
23210+ /* is this updating defined in POSIX? */
23211+ au_cpup_attr_timesizes(a->src_inode);
23212+ au_cpup_attr_nlink(dir, /*force*/1);
23213+ }
23214+ au_dir_ts(dir, a->btgt);
23215+
23216+ if (a->exchange) {
23217+ dir = a->src_dir;
23218+ inode_inc_iversion(dir);
23219+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23220+ /* is this updating defined in POSIX? */
23221+ au_cpup_attr_timesizes(a->dst_inode);
23222+ au_cpup_attr_nlink(dir, /*force*/1);
23223+ }
23224+ au_dir_ts(dir, a->btgt);
23225+ }
23226+
23227+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23228+ return;
23229+
23230+ dir = a->src_dir;
23231+ inode_inc_iversion(dir);
23232+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
23233+ au_cpup_attr_nlink(dir, /*force*/1);
23234+ au_dir_ts(dir, a->btgt);
23235+}
23236+
23237+static void au_ren_refresh(struct au_ren_args *a)
23238+{
23239+ aufs_bindex_t bbot, bindex;
23240+ struct dentry *d, *h_d;
23241+ struct inode *i, *h_i;
23242+ struct super_block *sb;
23243+
23244+ d = a->dst_dentry;
23245+ d_drop(d);
23246+ if (a->h_dst)
23247+ /* already dget-ed by au_ren_or_cpup() */
23248+ au_set_h_dptr(d, a->btgt, a->h_dst);
23249+
23250+ i = a->dst_inode;
23251+ if (i) {
23252+ if (!a->exchange) {
23253+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
23254+ vfsub_drop_nlink(i);
23255+ else {
23256+ vfsub_dead_dir(i);
23257+ au_cpup_attr_timesizes(i);
23258+ }
23259+ au_update_dbrange(d, /*do_put_zero*/1);
23260+ } else
23261+ au_cpup_attr_nlink(i, /*force*/1);
23262+ } else {
23263+ bbot = a->btgt;
23264+ for (bindex = au_dbtop(d); bindex < bbot; bindex++)
23265+ au_set_h_dptr(d, bindex, NULL);
23266+ bbot = au_dbbot(d);
23267+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
23268+ au_set_h_dptr(d, bindex, NULL);
23269+ au_update_dbrange(d, /*do_put_zero*/0);
23270+ }
23271+
23272+ if (a->exchange
23273+ || au_ftest_ren(a->auren_flags, DIRREN)) {
23274+ d_drop(a->src_dentry);
23275+ if (au_ftest_ren(a->auren_flags, DIRREN))
23276+ au_set_dbwh(a->src_dentry, -1);
23277+ return;
23278+ }
23279+
23280+ d = a->src_dentry;
23281+ au_set_dbwh(d, -1);
23282+ bbot = au_dbbot(d);
23283+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23284+ h_d = au_h_dptr(d, bindex);
23285+ if (h_d)
23286+ au_set_h_dptr(d, bindex, NULL);
23287+ }
23288+ au_set_dbbot(d, a->btgt);
23289+
23290+ sb = d->d_sb;
23291+ i = a->src_inode;
23292+ if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
23293+ return; /* success */
23294+
23295+ bbot = au_ibbot(i);
23296+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23297+ h_i = au_h_iptr(i, bindex);
23298+ if (h_i) {
23299+ au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
23300+ /* ignore this error */
23301+ au_set_h_iptr(i, bindex, NULL, 0);
23302+ }
23303+ }
23304+ au_set_ibbot(i, a->btgt);
23305+}
23306+
23307+/* ---------------------------------------------------------------------- */
23308+
23309+/* mainly for link(2) and rename(2) */
23310+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
23311+{
23312+ aufs_bindex_t bdiropq, bwh;
23313+ struct dentry *parent;
23314+ struct au_branch *br;
23315+
23316+ parent = dentry->d_parent;
23317+ IMustLock(d_inode(parent)); /* dir is locked */
23318+
23319+ bdiropq = au_dbdiropq(parent);
23320+ bwh = au_dbwh(dentry);
23321+ br = au_sbr(dentry->d_sb, btgt);
23322+ if (au_br_rdonly(br)
23323+ || (0 <= bdiropq && bdiropq < btgt)
23324+ || (0 <= bwh && bwh < btgt))
23325+ btgt = -1;
23326+
23327+ AuDbg("btgt %d\n", btgt);
23328+ return btgt;
23329+}
23330+
23331+/* sets src_btop, dst_btop and btgt */
23332+static int au_ren_wbr(struct au_ren_args *a)
23333+{
23334+ int err;
23335+ struct au_wr_dir_args wr_dir_args = {
23336+ /* .force_btgt = -1, */
23337+ .flags = AuWrDir_ADD_ENTRY
23338+ };
23339+
23340+ a->src_btop = au_dbtop(a->src_dentry);
23341+ a->dst_btop = au_dbtop(a->dst_dentry);
23342+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
23343+ || au_ftest_ren(a->auren_flags, ISDIR_DST))
23344+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
23345+ wr_dir_args.force_btgt = a->src_btop;
23346+ if (a->dst_inode && a->dst_btop < a->src_btop)
23347+ wr_dir_args.force_btgt = a->dst_btop;
23348+ wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
23349+ err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
23350+ a->btgt = err;
23351+ if (a->exchange)
23352+ au_update_dbtop(a->dst_dentry);
23353+
23354+ return err;
23355+}
23356+
23357+static void au_ren_dt(struct au_ren_args *a)
23358+{
23359+ a->h_path.dentry = a->src_h_parent;
23360+ au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
23361+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
23362+ a->h_path.dentry = a->dst_h_parent;
23363+ au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
23364+ }
23365+
23366+ au_fclr_ren(a->auren_flags, DT_DSTDIR);
23367+ if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
23368+ && !a->exchange)
23369+ return;
23370+
23371+ a->h_path.dentry = a->src_h_dentry;
23372+ au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
23373+ if (d_is_positive(a->dst_h_dentry)) {
23374+ au_fset_ren(a->auren_flags, DT_DSTDIR);
23375+ a->h_path.dentry = a->dst_h_dentry;
23376+ au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
23377+ }
23378+}
23379+
23380+static void au_ren_rev_dt(int err, struct au_ren_args *a)
23381+{
23382+ struct dentry *h_d;
23383+ struct inode *h_inode;
23384+
23385+ au_dtime_revert(a->src_dt + AuPARENT);
23386+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
23387+ au_dtime_revert(a->dst_dt + AuPARENT);
23388+
23389+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
23390+ h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
23391+ h_inode = d_inode(h_d);
23392+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
23393+ au_dtime_revert(a->src_dt + AuCHILD);
23394+ inode_unlock(h_inode);
23395+
23396+ if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
23397+ h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
23398+ h_inode = d_inode(h_d);
23399+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
23400+ au_dtime_revert(a->dst_dt + AuCHILD);
23401+ inode_unlock(h_inode);
23402+ }
23403+ }
23404+}
23405+
23406+/* ---------------------------------------------------------------------- */
23407+
23408+int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
23409+ struct inode *_dst_dir, struct dentry *_dst_dentry,
23410+ unsigned int _flags)
23411+{
23412+ int err, lock_flags;
23413+ void *rev;
23414+ /* reduce stack space */
23415+ struct au_ren_args *a;
23416+ struct au_pin pin;
23417+
23418+ AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
23419+ IMustLock(_src_dir);
23420+ IMustLock(_dst_dir);
23421+
23422+ err = -EINVAL;
23423+ if (unlikely(_flags & RENAME_WHITEOUT))
23424+ goto out;
23425+
23426+ err = -ENOMEM;
23427+ BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
23428+ a = kzalloc(sizeof(*a), GFP_NOFS);
23429+ if (unlikely(!a))
23430+ goto out;
23431+
23432+ a->flags = _flags;
23433+ BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
23434+ && RENAME_EXCHANGE > U8_MAX);
23435+ a->exchange = _flags & RENAME_EXCHANGE;
23436+ a->src_dir = _src_dir;
23437+ a->src_dentry = _src_dentry;
23438+ a->src_inode = NULL;
23439+ if (d_really_is_positive(a->src_dentry))
23440+ a->src_inode = d_inode(a->src_dentry);
23441+ a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
23442+ a->dst_dir = _dst_dir;
23443+ a->dst_dentry = _dst_dentry;
23444+ a->dst_inode = NULL;
23445+ if (d_really_is_positive(a->dst_dentry))
23446+ a->dst_inode = d_inode(a->dst_dentry);
23447+ a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
23448+ if (a->dst_inode) {
23449+ /*
23450+ * if EXCHANGE && src is non-dir && dst is dir,
23451+ * dst is not locked.
23452+ */
23453+ /* IMustLock(a->dst_inode); */
23454+ au_igrab(a->dst_inode);
23455+ }
23456+
23457+ err = -ENOTDIR;
23458+ lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
23459+ if (d_is_dir(a->src_dentry)) {
23460+ au_fset_ren(a->auren_flags, ISDIR_SRC);
23461+ if (unlikely(!a->exchange
23462+ && d_really_is_positive(a->dst_dentry)
23463+ && !d_is_dir(a->dst_dentry)))
23464+ goto out_free;
23465+ lock_flags |= AuLock_DIRS;
23466+ }
23467+ if (a->dst_inode && d_is_dir(a->dst_dentry)) {
23468+ au_fset_ren(a->auren_flags, ISDIR_DST);
23469+ if (unlikely(!a->exchange
23470+ && d_really_is_positive(a->src_dentry)
23471+ && !d_is_dir(a->src_dentry)))
23472+ goto out_free;
23473+ lock_flags |= AuLock_DIRS;
23474+ }
23475+ err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
23476+ lock_flags);
23477+ if (unlikely(err))
23478+ goto out_free;
23479+
23480+ err = au_d_hashed_positive(a->src_dentry);
23481+ if (unlikely(err))
23482+ goto out_unlock;
23483+ err = -ENOENT;
23484+ if (a->dst_inode) {
23485+ /*
23486+ * If it is a dir, VFS unhash it before this
23487+ * function. It means we cannot rely upon d_unhashed().
23488+ */
23489+ if (unlikely(!a->dst_inode->i_nlink))
23490+ goto out_unlock;
23491+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23492+ err = au_d_hashed_positive(a->dst_dentry);
23493+ if (unlikely(err && !a->exchange))
23494+ goto out_unlock;
23495+ } else if (unlikely(IS_DEADDIR(a->dst_inode)))
23496+ goto out_unlock;
23497+ } else if (unlikely(d_unhashed(a->dst_dentry)))
23498+ goto out_unlock;
23499+
23500+ /*
23501+ * is it possible?
23502+ * yes, it happened (in linux-3.3-rcN) but I don't know why.
23503+ * there may exist a problem somewhere else.
23504+ */
23505+ err = -EINVAL;
23506+ if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
23507+ goto out_unlock;
23508+
23509+ au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
23510+ di_write_lock_parent(a->dst_parent);
23511+
23512+ /* which branch we process */
23513+ err = au_ren_wbr(a);
23514+ if (unlikely(err < 0))
23515+ goto out_parent;
23516+ a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
23517+ a->h_path.mnt = au_br_mnt(a->br);
23518+
23519+ /* are they available to be renamed */
23520+ err = au_ren_may_dir(a);
23521+ if (unlikely(err))
23522+ goto out_children;
23523+
23524+ /* prepare the writable parent dir on the same branch */
23525+ if (a->dst_btop == a->btgt) {
23526+ au_fset_ren(a->auren_flags, WHDST);
23527+ } else {
23528+ err = au_cpup_dirs(a->dst_dentry, a->btgt);
23529+ if (unlikely(err))
23530+ goto out_children;
23531+ }
23532+
23533+ err = 0;
23534+ if (!a->exchange) {
23535+ if (a->src_dir != a->dst_dir) {
23536+ /*
23537+ * this temporary unlock is safe,
23538+ * because both dir->i_mutex are locked.
23539+ */
23540+ di_write_unlock(a->dst_parent);
23541+ di_write_lock_parent(a->src_parent);
23542+ err = au_wr_dir_need_wh(a->src_dentry,
23543+ au_ftest_ren(a->auren_flags,
23544+ ISDIR_SRC),
23545+ &a->btgt);
23546+ di_write_unlock(a->src_parent);
23547+ di_write_lock2_parent(a->src_parent, a->dst_parent,
23548+ /*isdir*/1);
23549+ au_fclr_ren(a->auren_flags, ISSAMEDIR);
23550+ } else
23551+ err = au_wr_dir_need_wh(a->src_dentry,
23552+ au_ftest_ren(a->auren_flags,
23553+ ISDIR_SRC),
23554+ &a->btgt);
23555+ }
23556+ if (unlikely(err < 0))
23557+ goto out_children;
23558+ if (err)
23559+ au_fset_ren(a->auren_flags, WHSRC);
23560+
23561+ /* cpup src */
23562+ if (a->src_btop != a->btgt) {
23563+ err = au_pin(&pin, a->src_dentry, a->btgt,
23564+ au_opt_udba(a->src_dentry->d_sb),
23565+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23566+ if (!err) {
23567+ struct au_cp_generic cpg = {
23568+ .dentry = a->src_dentry,
23569+ .bdst = a->btgt,
23570+ .bsrc = a->src_btop,
23571+ .len = -1,
23572+ .pin = &pin,
23573+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23574+ };
23575+ AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
23576+ err = au_sio_cpup_simple(&cpg);
23577+ au_unpin(&pin);
23578+ }
23579+ if (unlikely(err))
23580+ goto out_children;
23581+ a->src_btop = a->btgt;
23582+ a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
23583+ if (!a->exchange)
23584+ au_fset_ren(a->auren_flags, WHSRC);
23585+ }
23586+
23587+ /* cpup dst */
23588+ if (a->exchange && a->dst_inode
23589+ && a->dst_btop != a->btgt) {
23590+ err = au_pin(&pin, a->dst_dentry, a->btgt,
23591+ au_opt_udba(a->dst_dentry->d_sb),
23592+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23593+ if (!err) {
23594+ struct au_cp_generic cpg = {
23595+ .dentry = a->dst_dentry,
23596+ .bdst = a->btgt,
23597+ .bsrc = a->dst_btop,
23598+ .len = -1,
23599+ .pin = &pin,
23600+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23601+ };
23602+ err = au_sio_cpup_simple(&cpg);
23603+ au_unpin(&pin);
23604+ }
23605+ if (unlikely(err))
23606+ goto out_children;
23607+ a->dst_btop = a->btgt;
23608+ a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
23609+ }
23610+
23611+ /* lock them all */
23612+ err = au_ren_lock(a);
23613+ if (unlikely(err))
23614+ /* leave the copied-up one */
23615+ goto out_children;
23616+
23617+ if (!a->exchange) {
23618+ if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
23619+ err = au_may_ren(a);
23620+ else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
23621+ err = -ENAMETOOLONG;
23622+ if (unlikely(err))
23623+ goto out_hdir;
23624+ }
23625+
23626+ /* store timestamps to be revertible */
23627+ au_ren_dt(a);
23628+
23629+ /* store dirren info */
23630+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23631+ err = au_dr_rename(a->src_dentry, a->btgt,
23632+ &a->dst_dentry->d_name, &rev);
23633+ AuTraceErr(err);
23634+ if (unlikely(err))
23635+ goto out_dt;
23636+ }
23637+
23638+ /* here we go */
23639+ err = do_rename(a);
23640+ if (unlikely(err))
23641+ goto out_dirren;
23642+
23643+ if (au_ftest_ren(a->auren_flags, DIRREN))
23644+ au_dr_rename_fin(a->src_dentry, a->btgt, rev);
23645+
23646+ /* update dir attributes */
23647+ au_ren_refresh_dir(a);
23648+
23649+ /* dput/iput all lower dentries */
23650+ au_ren_refresh(a);
23651+
23652+ goto out_hdir; /* success */
23653+
23654+out_dirren:
23655+ if (au_ftest_ren(a->auren_flags, DIRREN))
23656+ au_dr_rename_rev(a->src_dentry, a->btgt, rev);
23657+out_dt:
23658+ au_ren_rev_dt(err, a);
23659+out_hdir:
23660+ au_ren_unlock(a);
23661+out_children:
23662+ au_nhash_wh_free(&a->whlist);
23663+ if (err && a->dst_inode && a->dst_btop != a->btgt) {
23664+ AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
23665+ au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
23666+ au_set_dbtop(a->dst_dentry, a->dst_btop);
23667+ }
23668+out_parent:
23669+ if (!err) {
23670+ if (d_unhashed(a->src_dentry))
23671+ au_fset_ren(a->auren_flags, DROPPED_SRC);
23672+ if (d_unhashed(a->dst_dentry))
23673+ au_fset_ren(a->auren_flags, DROPPED_DST);
23674+ if (!a->exchange)
23675+ d_move(a->src_dentry, a->dst_dentry);
23676+ else {
23677+ d_exchange(a->src_dentry, a->dst_dentry);
23678+ if (au_ftest_ren(a->auren_flags, DROPPED_DST))
23679+ d_drop(a->dst_dentry);
23680+ }
23681+ if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
23682+ d_drop(a->src_dentry);
23683+ } else {
23684+ au_update_dbtop(a->dst_dentry);
23685+ if (!a->dst_inode)
23686+ d_drop(a->dst_dentry);
23687+ }
23688+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23689+ di_write_unlock(a->dst_parent);
23690+ else
23691+ di_write_unlock2(a->src_parent, a->dst_parent);
23692+out_unlock:
23693+ aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
23694+out_free:
23695+ iput(a->dst_inode);
23696+ if (a->thargs)
23697+ au_whtmp_rmdir_free(a->thargs);
23698+ au_kfree_rcu(a);
23699+out:
23700+ AuTraceErr(err);
23701+ return err;
23702+}
23703diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
23704--- /usr/share/empty/fs/aufs/Kconfig 1970-01-01 01:00:00.000000000 +0100
23705+++ linux/fs/aufs/Kconfig 2021-02-22 22:51:59.058078584 +0100
23706@@ -0,0 +1,199 @@
23707+# SPDX-License-Identifier: GPL-2.0
23708+config AUFS_FS
23709+ tristate "Aufs (Advanced multi layered unification filesystem) support"
23710+ help
23711+ Aufs is a stackable unification filesystem such as Unionfs,
23712+ which unifies several directories and provides a merged single
23713+ directory.
23714+ In the early days, aufs was entirely re-designed and
23715+ re-implemented Unionfs Version 1.x series. Introducing many
23716+ original ideas, approaches and improvements, it becomes totally
23717+ different from Unionfs while keeping the basic features.
23718+
23719+if AUFS_FS
23720+choice
23721+ prompt "Maximum number of branches"
23722+ default AUFS_BRANCH_MAX_127
23723+ help
23724+ Specifies the maximum number of branches (or member directories)
23725+ in a single aufs. The larger value consumes more system
23726+ resources and has a minor impact to performance.
23727+config AUFS_BRANCH_MAX_127
23728+ bool "127"
23729+ help
23730+ Specifies the maximum number of branches (or member directories)
23731+ in a single aufs. The larger value consumes more system
23732+ resources and has a minor impact to performance.
23733+config AUFS_BRANCH_MAX_511
23734+ bool "511"
23735+ help
23736+ Specifies the maximum number of branches (or member directories)
23737+ in a single aufs. The larger value consumes more system
23738+ resources and has a minor impact to performance.
23739+config AUFS_BRANCH_MAX_1023
23740+ bool "1023"
23741+ help
23742+ Specifies the maximum number of branches (or member directories)
23743+ in a single aufs. The larger value consumes more system
23744+ resources and has a minor impact to performance.
23745+config AUFS_BRANCH_MAX_32767
23746+ bool "32767"
23747+ help
23748+ Specifies the maximum number of branches (or member directories)
23749+ in a single aufs. The larger value consumes more system
23750+ resources and has a minor impact to performance.
23751+endchoice
23752+
23753+config AUFS_SBILIST
23754+ bool
23755+ depends on AUFS_MAGIC_SYSRQ || PROC_FS
23756+ default y
23757+ help
23758+ Automatic configuration for internal use.
23759+ When aufs supports Magic SysRq or /proc, enabled automatically.
23760+
23761+config AUFS_HNOTIFY
23762+ bool "Detect direct branch access (bypassing aufs)"
23763+ help
23764+ If you want to modify files on branches directly, eg. bypassing aufs,
23765+ and want aufs to detect the changes of them fully, then enable this
23766+ option and use 'udba=notify' mount option.
23767+ Currently there is only one available configuration, "fsnotify".
23768+ It will have a negative impact to the performance.
23769+ See detail in aufs.5.
23770+
23771+choice
23772+ prompt "method" if AUFS_HNOTIFY
23773+ default AUFS_HFSNOTIFY
23774+config AUFS_HFSNOTIFY
23775+ bool "fsnotify"
23776+ select FSNOTIFY
23777+endchoice
23778+
23779+config AUFS_EXPORT
23780+ bool "NFS-exportable aufs"
23781+ depends on EXPORTFS
23782+ help
23783+ If you want to export your mounted aufs via NFS, then enable this
23784+ option. There are several requirements for this configuration.
23785+ See detail in aufs.5.
23786+
23787+config AUFS_INO_T_64
23788+ bool
23789+ depends on AUFS_EXPORT
23790+ depends on 64BIT && !(ALPHA || S390)
23791+ default y
23792+ help
23793+ Automatic configuration for internal use.
23794+ /* typedef unsigned long/int __kernel_ino_t */
23795+ /* alpha and s390x are int */
23796+
23797+config AUFS_XATTR
23798+ bool "support for XATTR/EA (including Security Labels)"
23799+ help
23800+ If your branch fs supports XATTR/EA and you want to make them
23801+ available in aufs too, then enable this opsion and specify the
23802+ branch attributes for EA.
23803+ See detail in aufs.5.
23804+
23805+config AUFS_FHSM
23806+ bool "File-based Hierarchical Storage Management"
23807+ help
23808+ Hierarchical Storage Management (or HSM) is a well-known feature
23809+ in the storage world. Aufs provides this feature as file-based.
23810+ with multiple branches.
23811+ These multiple branches are prioritized, ie. the topmost one
23812+ should be the fastest drive and be used heavily.
23813+
23814+config AUFS_RDU
23815+ bool "Readdir in userspace"
23816+ help
23817+ Aufs has two methods to provide a merged view for a directory,
23818+ by a user-space library and by kernel-space natively. The latter
23819+ is always enabled but sometimes large and slow.
23820+ If you enable this option, install the library in aufs2-util
23821+ package, and set some environment variables for your readdir(3),
23822+ then the work will be handled in user-space which generally
23823+ shows better performance in most cases.
23824+ See detail in aufs.5.
23825+
23826+config AUFS_DIRREN
23827+ bool "Workaround for rename(2)-ing a directory"
23828+ help
23829+ By default, aufs returns EXDEV error in renameing a dir who has
23830+ his child on the lower branch, since it is a bad idea to issue
23831+ rename(2) internally for every lower branch. But user may not
23832+ accept this behaviour. So here is a workaround to allow such
23833+ rename(2) and store some extra infromation on the writable
23834+ branch. Obviously this costs high (and I don't like it).
23835+ To use this feature, you need to enable this configuration AND
23836+ to specify the mount option `dirren.'
23837+ See details in aufs.5 and the design documents.
23838+
23839+config AUFS_SHWH
23840+ bool "Show whiteouts"
23841+ help
23842+ If you want to make the whiteouts in aufs visible, then enable
23843+ this option and specify 'shwh' mount option. Although it may
23844+ sounds like philosophy or something, but in technically it
23845+ simply shows the name of whiteout with keeping its behaviour.
23846+
23847+config AUFS_BR_RAMFS
23848+ bool "Ramfs (initramfs/rootfs) as an aufs branch"
23849+ help
23850+ If you want to use ramfs as an aufs branch fs, then enable this
23851+ option. Generally tmpfs is recommended.
23852+ Aufs prohibited them to be a branch fs by default, because
23853+ initramfs becomes unusable after switch_root or something
23854+ generally. If you sets initramfs as an aufs branch and boot your
23855+ system by switch_root, you will meet a problem easily since the
23856+ files in initramfs may be inaccessible.
23857+ Unless you are going to use ramfs as an aufs branch fs without
23858+ switch_root or something, leave it N.
23859+
23860+config AUFS_BR_FUSE
23861+ bool "Fuse fs as an aufs branch"
23862+ depends on FUSE_FS
23863+ select AUFS_POLL
23864+ help
23865+ If you want to use fuse-based userspace filesystem as an aufs
23866+ branch fs, then enable this option.
23867+ It implements the internal poll(2) operation which is
23868+ implemented by fuse only (curretnly).
23869+
23870+config AUFS_POLL
23871+ bool
23872+ help
23873+ Automatic configuration for internal use.
23874+
23875+config AUFS_BR_HFSPLUS
23876+ bool "Hfsplus as an aufs branch"
23877+ depends on HFSPLUS_FS
23878+ default y
23879+ help
23880+ If you want to use hfsplus fs as an aufs branch fs, then enable
23881+ this option. This option introduces a small overhead at
23882+ copying-up a file on hfsplus.
23883+
23884+config AUFS_BDEV_LOOP
23885+ bool
23886+ depends on BLK_DEV_LOOP
23887+ default y
23888+ help
23889+ Automatic configuration for internal use.
23890+ Convert =[ym] into =y.
23891+
23892+config AUFS_DEBUG
23893+ bool "Debug aufs"
23894+ help
23895+ Enable this to compile aufs internal debug code.
23896+ It will have a negative impact to the performance.
23897+
23898+config AUFS_MAGIC_SYSRQ
23899+ bool
23900+ depends on AUFS_DEBUG && MAGIC_SYSRQ
23901+ default y
23902+ help
23903+ Automatic configuration for internal use.
23904+ When aufs supports Magic SysRq, enabled automatically.
23905+endif
23906diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
23907--- /usr/share/empty/fs/aufs/lcnt.h 1970-01-01 01:00:00.000000000 +0100
23908+++ linux/fs/aufs/lcnt.h 2021-02-22 22:51:59.061411920 +0100
23909@@ -0,0 +1,186 @@
23910+/* SPDX-License-Identifier: GPL-2.0 */
23911+/*
23912+ * Copyright (C) 2018-2020 Junjiro R. Okajima
23913+ *
23914+ * This program, aufs is free software; you can redistribute it and/or modify
23915+ * it under the terms of the GNU General Public License as published by
23916+ * the Free Software Foundation; either version 2 of the License, or
23917+ * (at your option) any later version.
23918+ *
23919+ * This program is distributed in the hope that it will be useful,
23920+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
23921+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23922+ * GNU General Public License for more details.
23923+ *
23924+ * You should have received a copy of the GNU General Public License
23925+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
23926+ */
23927+
23928+/*
23929+ * simple long counter wrapper
23930+ */
23931+
23932+#ifndef __AUFS_LCNT_H__
23933+#define __AUFS_LCNT_H__
23934+
23935+#ifdef __KERNEL__
23936+
23937+#include "debug.h"
23938+
23939+#define AuLCntATOMIC 1
23940+#define AuLCntPCPUCNT 2
23941+/*
23942+ * why does percpu_refcount require extra synchronize_rcu()s in
23943+ * au_br_do_free()
23944+ */
23945+#define AuLCntPCPUREF 3
23946+
23947+/* #define AuLCntChosen AuLCntATOMIC */
23948+#define AuLCntChosen AuLCntPCPUCNT
23949+/* #define AuLCntChosen AuLCntPCPUREF */
23950+
23951+#if AuLCntChosen == AuLCntATOMIC
23952+#include <linux/atomic.h>
23953+
23954+typedef atomic_long_t au_lcnt_t;
23955+
23956+static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
23957+{
23958+ atomic_long_set(cnt, 0);
23959+ return 0;
23960+}
23961+
23962+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
23963+{
23964+ /* empty */
23965+}
23966+
23967+static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
23968+ int do_sync __maybe_unused)
23969+{
23970+ /* empty */
23971+}
23972+
23973+static inline void au_lcnt_inc(au_lcnt_t *cnt)
23974+{
23975+ atomic_long_inc(cnt);
23976+}
23977+
23978+static inline void au_lcnt_dec(au_lcnt_t *cnt)
23979+{
23980+ atomic_long_dec(cnt);
23981+}
23982+
23983+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
23984+{
23985+ return atomic_long_read(cnt);
23986+}
23987+#endif
23988+
23989+#if AuLCntChosen == AuLCntPCPUCNT
23990+#include <linux/percpu_counter.h>
23991+
23992+typedef struct percpu_counter au_lcnt_t;
23993+
23994+static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
23995+{
23996+ return percpu_counter_init(cnt, 0, GFP_NOFS);
23997+}
23998+
23999+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24000+{
24001+ /* empty */
24002+}
24003+
24004+static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
24005+{
24006+ percpu_counter_destroy(cnt);
24007+}
24008+
24009+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24010+{
24011+ percpu_counter_inc(cnt);
24012+}
24013+
24014+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24015+{
24016+ percpu_counter_dec(cnt);
24017+}
24018+
24019+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24020+{
24021+ s64 n;
24022+
24023+ n = percpu_counter_sum(cnt);
24024+ BUG_ON(n < 0);
24025+ if (LONG_MAX != LLONG_MAX
24026+ && n > LONG_MAX)
24027+ AuWarn1("%s\n", "wrap-around");
24028+
24029+ return n;
24030+}
24031+#endif
24032+
24033+#if AuLCntChosen == AuLCntPCPUREF
24034+#include <linux/percpu-refcount.h>
24035+
24036+typedef struct percpu_ref au_lcnt_t;
24037+
24038+static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
24039+{
24040+ if (!release)
24041+ release = percpu_ref_exit;
24042+ return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
24043+}
24044+
24045+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24046+{
24047+ synchronize_rcu();
24048+}
24049+
24050+static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
24051+{
24052+ percpu_ref_kill(cnt);
24053+ if (do_sync)
24054+ au_lcnt_wait_for_fin(cnt);
24055+}
24056+
24057+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24058+{
24059+ percpu_ref_get(cnt);
24060+}
24061+
24062+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24063+{
24064+ percpu_ref_put(cnt);
24065+}
24066+
24067+/*
24068+ * avoid calling this func as possible.
24069+ */
24070+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
24071+{
24072+ long l;
24073+
24074+ percpu_ref_switch_to_atomic_sync(cnt);
24075+ l = atomic_long_read(&cnt->count);
24076+ if (do_rev)
24077+ percpu_ref_switch_to_percpu(cnt);
24078+
24079+ /* percpu_ref is initialized by 1 instead of 0 */
24080+ return l - 1;
24081+}
24082+#endif
24083+
24084+#ifdef CONFIG_AUFS_DEBUG
24085+#define AuLCntZero(val) do { \
24086+ long l = val; \
24087+ if (l) \
24088+ AuDbg("%s = %ld\n", #val, l); \
24089+} while (0)
24090+#else
24091+#define AuLCntZero(val) do {} while (0)
24092+#endif
24093+
24094+#endif /* __KERNEL__ */
24095+#endif /* __AUFS_LCNT_H__ */
24096diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
24097--- /usr/share/empty/fs/aufs/loop.c 1970-01-01 01:00:00.000000000 +0100
24098+++ linux/fs/aufs/loop.c 2021-02-22 22:51:59.061411920 +0100
24099@@ -0,0 +1,148 @@
24100+// SPDX-License-Identifier: GPL-2.0
24101+/*
24102+ * Copyright (C) 2005-2020 Junjiro R. Okajima
24103+ *
24104+ * This program, aufs is free software; you can redistribute it and/or modify
24105+ * it under the terms of the GNU General Public License as published by
24106+ * the Free Software Foundation; either version 2 of the License, or
24107+ * (at your option) any later version.
24108+ *
24109+ * This program is distributed in the hope that it will be useful,
24110+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24111+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24112+ * GNU General Public License for more details.
24113+ *
24114+ * You should have received a copy of the GNU General Public License
24115+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
24116+ */
24117+
24118+/*
24119+ * support for loopback block device as a branch
24120+ */
24121+
24122+#include "aufs.h"
24123+
24124+/* added into drivers/block/loop.c */
24125+static struct file *(*backing_file_func)(struct super_block *sb);
24126+
24127+/*
24128+ * test if two lower dentries have overlapping branches.
24129+ */
24130+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
24131+{
24132+ struct super_block *h_sb;
24133+ struct file *backing_file;
24134+
24135+ if (unlikely(!backing_file_func)) {
24136+ /* don't load "loop" module here */
24137+ backing_file_func = symbol_get(loop_backing_file);
24138+ if (unlikely(!backing_file_func))
24139+ /* "loop" module is not loaded */
24140+ return 0;
24141+ }
24142+
24143+ h_sb = h_adding->d_sb;
24144+ backing_file = backing_file_func(h_sb);
24145+ if (!backing_file)
24146+ return 0;
24147+
24148+ h_adding = backing_file->f_path.dentry;
24149+ /*
24150+ * h_adding can be local NFS.
24151+ * in this case aufs cannot detect the loop.
24152+ */
24153+ if (unlikely(h_adding->d_sb == sb))
24154+ return 1;
24155+ return !!au_test_subdir(h_adding, sb->s_root);
24156+}
24157+
24158+/* true if a kernel thread named 'loop[0-9].*' accesses a file */
24159+int au_test_loopback_kthread(void)
24160+{
24161+ int ret;
24162+ struct task_struct *tsk = current;
24163+ char c, comm[sizeof(tsk->comm)];
24164+
24165+ ret = 0;
24166+ if (tsk->flags & PF_KTHREAD) {
24167+ get_task_comm(comm, tsk);
24168+ c = comm[4];
24169+ ret = ('0' <= c && c <= '9'
24170+ && !strncmp(comm, "loop", 4));
24171+ }
24172+
24173+ return ret;
24174+}
24175+
24176+/* ---------------------------------------------------------------------- */
24177+
24178+#define au_warn_loopback_step 16
24179+static int au_warn_loopback_nelem = au_warn_loopback_step;
24180+static unsigned long *au_warn_loopback_array;
24181+
24182+void au_warn_loopback(struct super_block *h_sb)
24183+{
24184+ int i, new_nelem;
24185+ unsigned long *a, magic;
24186+ static DEFINE_SPINLOCK(spin);
24187+
24188+ magic = h_sb->s_magic;
24189+ spin_lock(&spin);
24190+ a = au_warn_loopback_array;
24191+ for (i = 0; i < au_warn_loopback_nelem && *a; i++)
24192+ if (a[i] == magic) {
24193+ spin_unlock(&spin);
24194+ return;
24195+ }
24196+
24197+ /* h_sb is new to us, print it */
24198+ if (i < au_warn_loopback_nelem) {
24199+ a[i] = magic;
24200+ goto pr;
24201+ }
24202+
24203+ /* expand the array */
24204+ new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
24205+ a = au_kzrealloc(au_warn_loopback_array,
24206+ au_warn_loopback_nelem * sizeof(unsigned long),
24207+ new_nelem * sizeof(unsigned long), GFP_ATOMIC,
24208+ /*may_shrink*/0);
24209+ if (a) {
24210+ au_warn_loopback_nelem = new_nelem;
24211+ au_warn_loopback_array = a;
24212+ a[i] = magic;
24213+ goto pr;
24214+ }
24215+
24216+ spin_unlock(&spin);
24217+ AuWarn1("realloc failed, ignored\n");
24218+ return;
24219+
24220+pr:
24221+ spin_unlock(&spin);
24222+ pr_warn("you may want to try another patch for loopback file "
24223+ "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
24224+}
24225+
24226+int au_loopback_init(void)
24227+{
24228+ int err;
24229+ struct super_block *sb __maybe_unused;
24230+
24231+ BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
24232+
24233+ err = 0;
24234+ au_warn_loopback_array = kcalloc(au_warn_loopback_step,
24235+ sizeof(unsigned long), GFP_NOFS);
24236+ if (unlikely(!au_warn_loopback_array))
24237+ err = -ENOMEM;
24238+
24239+ return err;
24240+}
24241+
24242+void au_loopback_fin(void)
24243+{
24244+ if (backing_file_func)
24245+ symbol_put(loop_backing_file);
24246+ au_kfree_try_rcu(au_warn_loopback_array);
24247+}
24248diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
24249--- /usr/share/empty/fs/aufs/loop.h 1970-01-01 01:00:00.000000000 +0100
24250+++ linux/fs/aufs/loop.h 2021-02-22 22:51:59.061411920 +0100
24251@@ -0,0 +1,55 @@
24252+/* SPDX-License-Identifier: GPL-2.0 */
24253+/*
24254+ * Copyright (C) 2005-2020 Junjiro R. Okajima
24255+ *
24256+ * This program, aufs is free software; you can redistribute it and/or modify
24257+ * it under the terms of the GNU General Public License as published by
24258+ * the Free Software Foundation; either version 2 of the License, or
24259+ * (at your option) any later version.
24260+ *
24261+ * This program is distributed in the hope that it will be useful,
24262+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24263+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24264+ * GNU General Public License for more details.
24265+ *
24266+ * You should have received a copy of the GNU General Public License
24267+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
24268+ */
24269+
24270+/*
24271+ * support for loopback mount as a branch
24272+ */
24273+
24274+#ifndef __AUFS_LOOP_H__
24275+#define __AUFS_LOOP_H__
24276+
24277+#ifdef __KERNEL__
24278+
24279+struct dentry;
24280+struct super_block;
24281+
24282+#ifdef CONFIG_AUFS_BDEV_LOOP
24283+/* drivers/block/loop.c */
24284+struct file *loop_backing_file(struct super_block *sb);
24285+
24286+/* loop.c */
24287+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
24288+int au_test_loopback_kthread(void);
24289+void au_warn_loopback(struct super_block *h_sb);
24290+
24291+int au_loopback_init(void);
24292+void au_loopback_fin(void);
24293+#else
24294+AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
24295+
24296+AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
24297+ struct dentry *h_adding)
24298+AuStubInt0(au_test_loopback_kthread, void)
24299+AuStubVoid(au_warn_loopback, struct super_block *h_sb)
24300+
24301+AuStubInt0(au_loopback_init, void)
24302+AuStubVoid(au_loopback_fin, void)
24303+#endif /* BLK_DEV_LOOP */
24304+
24305+#endif /* __KERNEL__ */
24306+#endif /* __AUFS_LOOP_H__ */
24307diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
24308--- /usr/share/empty/fs/aufs/magic.mk 1970-01-01 01:00:00.000000000 +0100
24309+++ linux/fs/aufs/magic.mk 2021-02-22 22:51:59.061411920 +0100
24310@@ -0,0 +1,31 @@
24311+# SPDX-License-Identifier: GPL-2.0
24312+
24313+# defined in ${srctree}/fs/fuse/inode.c
24314+# tristate
24315+ifdef CONFIG_FUSE_FS
24316+ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
24317+endif
24318+
24319+# defined in ${srctree}/fs/xfs/xfs_sb.h
24320+# tristate
24321+ifdef CONFIG_XFS_FS
24322+ccflags-y += -DXFS_SB_MAGIC=0x58465342
24323+endif
24324+
24325+# defined in ${srctree}/fs/configfs/mount.c
24326+# tristate
24327+ifdef CONFIG_CONFIGFS_FS
24328+ccflags-y += -DCONFIGFS_MAGIC=0x62656570
24329+endif
24330+
24331+# defined in ${srctree}/fs/ubifs/ubifs.h
24332+# tristate
24333+ifdef CONFIG_UBIFS_FS
24334+ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
24335+endif
24336+
24337+# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
24338+# tristate
24339+ifdef CONFIG_HFSPLUS_FS
24340+ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
24341+endif
24342diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
24343--- /usr/share/empty/fs/aufs/Makefile 1970-01-01 01:00:00.000000000 +0100
24344+++ linux/fs/aufs/Makefile 2021-02-22 22:51:59.058078584 +0100
24345@@ -0,0 +1,46 @@
24346+# SPDX-License-Identifier: GPL-2.0
24347+
24348+include ${src}/magic.mk
24349+ifeq (${CONFIG_AUFS_FS},m)
24350+include ${src}/conf.mk
24351+endif
24352+-include ${src}/priv_def.mk
24353+
24354+# cf. include/linux/kernel.h
24355+# enable pr_debug
24356+ccflags-y += -DDEBUG
24357+# sparse requires the full pathname
24358+ifdef M
24359+ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
24360+else
24361+ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
24362+endif
24363+
24364+obj-$(CONFIG_AUFS_FS) += aufs.o
24365+aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
24366+ wkq.o vfsub.o dcsub.o \
24367+ cpup.o whout.o wbr_policy.o \
24368+ dinfo.o dentry.o \
24369+ dynop.o \
24370+ finfo.o file.o f_op.o \
24371+ dir.o vdir.o \
24372+ iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
24373+ mvdown.o ioctl.o
24374+
24375+# all are boolean
24376+aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
24377+aufs-$(CONFIG_SYSFS) += sysfs.o
24378+aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
24379+aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
24380+aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
24381+aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
24382+aufs-$(CONFIG_AUFS_EXPORT) += export.o
24383+aufs-$(CONFIG_AUFS_XATTR) += xattr.o
24384+aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
24385+aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
24386+aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
24387+aufs-$(CONFIG_AUFS_POLL) += poll.o
24388+aufs-$(CONFIG_AUFS_RDU) += rdu.o
24389+aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
24390+aufs-$(CONFIG_AUFS_DEBUG) += debug.o
24391+aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
24392diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
24393--- /usr/share/empty/fs/aufs/module.c 1970-01-01 01:00:00.000000000 +0100
24394+++ linux/fs/aufs/module.c 2021-02-22 22:51:59.061411920 +0100
24395@@ -0,0 +1,273 @@
24396+// SPDX-License-Identifier: GPL-2.0
24397+/*
24398+ * Copyright (C) 2005-2020 Junjiro R. Okajima
24399+ *
24400+ * This program, aufs is free software; you can redistribute it and/or modify
24401+ * it under the terms of the GNU General Public License as published by
24402+ * the Free Software Foundation; either version 2 of the License, or
24403+ * (at your option) any later version.
24404+ *
24405+ * This program is distributed in the hope that it will be useful,
24406+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24407+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24408+ * GNU General Public License for more details.
24409+ *
24410+ * You should have received a copy of the GNU General Public License
24411+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
24412+ */
24413+
24414+/*
24415+ * module global variables and operations
24416+ */
24417+
24418+#include <linux/module.h>
24419+#include <linux/seq_file.h>
24420+#include "aufs.h"
24421+
24422+/* shrinkable realloc */
24423+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
24424+{
24425+ size_t sz;
24426+ int diff;
24427+
24428+ sz = 0;
24429+ diff = -1;
24430+ if (p) {
24431+#if 0 /* unused */
24432+ if (!new_sz) {
24433+ au_kfree_rcu(p);
24434+ p = NULL;
24435+ goto out;
24436+ }
24437+#else
24438+ AuDebugOn(!new_sz);
24439+#endif
24440+ sz = ksize(p);
24441+ diff = au_kmidx_sub(sz, new_sz);
24442+ }
24443+ if (sz && !diff)
24444+ goto out;
24445+
24446+ if (sz < new_sz)
24447+ /* expand or SLOB */
24448+ p = krealloc(p, new_sz, gfp);
24449+ else if (new_sz < sz && may_shrink) {
24450+ /* shrink */
24451+ void *q;
24452+
24453+ q = kmalloc(new_sz, gfp);
24454+ if (q) {
24455+ if (p) {
24456+ memcpy(q, p, new_sz);
24457+ au_kfree_try_rcu(p);
24458+ }
24459+ p = q;
24460+ } else
24461+ p = NULL;
24462+ }
24463+
24464+out:
24465+ return p;
24466+}
24467+
24468+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24469+ int may_shrink)
24470+{
24471+ p = au_krealloc(p, new_sz, gfp, may_shrink);
24472+ if (p && new_sz > nused)
24473+ memset(p + nused, 0, new_sz - nused);
24474+ return p;
24475+}
24476+
24477+/* ---------------------------------------------------------------------- */
24478+/*
24479+ * aufs caches
24480+ */
24481+struct kmem_cache *au_cache[AuCache_Last];
24482+
24483+static void au_cache_fin(void)
24484+{
24485+ int i;
24486+
24487+ /*
24488+ * Make sure all delayed rcu free inodes are flushed before we
24489+ * destroy cache.
24490+ */
24491+ rcu_barrier();
24492+
24493+ /* excluding AuCache_HNOTIFY */
24494+ BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
24495+ for (i = 0; i < AuCache_HNOTIFY; i++) {
24496+ kmem_cache_destroy(au_cache[i]);
24497+ au_cache[i] = NULL;
24498+ }
24499+}
24500+
24501+static int __init au_cache_init(void)
24502+{
24503+ au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
24504+ if (au_cache[AuCache_DINFO])
24505+ /* SLAB_DESTROY_BY_RCU */
24506+ au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
24507+ au_icntnr_init_once);
24508+ if (au_cache[AuCache_ICNTNR])
24509+ au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
24510+ au_fi_init_once);
24511+ if (au_cache[AuCache_FINFO])
24512+ au_cache[AuCache_VDIR] = AuCache(au_vdir);
24513+ if (au_cache[AuCache_VDIR])
24514+ au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
24515+ if (au_cache[AuCache_DEHSTR])
24516+ return 0;
24517+
24518+ au_cache_fin();
24519+ return -ENOMEM;
24520+}
24521+
24522+/* ---------------------------------------------------------------------- */
24523+
24524+int au_dir_roflags;
24525+
24526+#ifdef CONFIG_AUFS_SBILIST
24527+/*
24528+ * iterate_supers_type() doesn't protect us from
24529+ * remounting (branch management)
24530+ */
24531+struct hlist_bl_head au_sbilist;
24532+#endif
24533+
24534+/*
24535+ * functions for module interface.
24536+ */
24537+MODULE_LICENSE("GPL");
24538+/* MODULE_LICENSE("GPL v2"); */
24539+MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
24540+MODULE_DESCRIPTION(AUFS_NAME
24541+ " -- Advanced multi layered unification filesystem");
24542+MODULE_VERSION(AUFS_VERSION);
24543+MODULE_ALIAS_FS(AUFS_NAME);
24544+
24545+/* this module parameter has no meaning when SYSFS is disabled */
24546+int sysaufs_brs = 1;
24547+MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
24548+module_param_named(brs, sysaufs_brs, int, 0444);
24549+
24550+/* this module parameter has no meaning when USER_NS is disabled */
24551+bool au_userns;
24552+MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
24553+module_param_named(allow_userns, au_userns, bool, 0444);
24554+
24555+/* ---------------------------------------------------------------------- */
24556+
24557+static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
24558+
24559+int au_seq_path(struct seq_file *seq, struct path *path)
24560+{
24561+ int err;
24562+
24563+ err = seq_path(seq, path, au_esc_chars);
24564+ if (err >= 0)
24565+ err = 0;
24566+ else
24567+ err = -ENOMEM;
24568+
24569+ return err;
24570+}
24571+
24572+/* ---------------------------------------------------------------------- */
24573+
24574+static int __init aufs_init(void)
24575+{
24576+ int err, i;
24577+ char *p;
24578+
24579+ p = au_esc_chars;
24580+ for (i = 1; i <= ' '; i++)
24581+ *p++ = i;
24582+ *p++ = '\\';
24583+ *p++ = '\x7f';
24584+ *p = 0;
24585+
24586+ au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
24587+
24588+ memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
24589+ for (i = 0; i < AuIop_Last; i++)
24590+ aufs_iop_nogetattr[i].getattr = NULL;
24591+
24592+ memset(au_cache, 0, sizeof(au_cache)); /* including hnotify */
24593+
24594+ au_sbilist_init();
24595+ sysaufs_brs_init();
24596+ au_debug_init();
24597+ au_dy_init();
24598+ err = sysaufs_init();
24599+ if (unlikely(err))
24600+ goto out;
24601+ err = dbgaufs_init();
24602+ if (unlikely(err))
24603+ goto out_sysaufs;
24604+ err = au_procfs_init();
24605+ if (unlikely(err))
24606+ goto out_dbgaufs;
24607+ err = au_wkq_init();
24608+ if (unlikely(err))
24609+ goto out_procfs;
24610+ err = au_loopback_init();
24611+ if (unlikely(err))
24612+ goto out_wkq;
24613+ err = au_hnotify_init();
24614+ if (unlikely(err))
24615+ goto out_loopback;
24616+ err = au_sysrq_init();
24617+ if (unlikely(err))
24618+ goto out_hin;
24619+ err = au_cache_init();
24620+ if (unlikely(err))
24621+ goto out_sysrq;
24622+
24623+ aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
24624+ err = register_filesystem(&aufs_fs_type);
24625+ if (unlikely(err))
24626+ goto out_cache;
24627+
24628+ /* since we define pr_fmt, call printk directly */
24629+ printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
24630+ goto out; /* success */
24631+
24632+out_cache:
24633+ au_cache_fin();
24634+out_sysrq:
24635+ au_sysrq_fin();
24636+out_hin:
24637+ au_hnotify_fin();
24638+out_loopback:
24639+ au_loopback_fin();
24640+out_wkq:
24641+ au_wkq_fin();
24642+out_procfs:
24643+ au_procfs_fin();
24644+out_dbgaufs:
24645+ dbgaufs_fin();
24646+out_sysaufs:
24647+ sysaufs_fin();
24648+ au_dy_fin();
24649+out:
24650+ return err;
24651+}
24652+
24653+static void __exit aufs_exit(void)
24654+{
24655+ unregister_filesystem(&aufs_fs_type);
24656+ au_cache_fin();
24657+ au_sysrq_fin();
24658+ au_hnotify_fin();
24659+ au_loopback_fin();
24660+ au_wkq_fin();
24661+ au_procfs_fin();
24662+ dbgaufs_fin();
24663+ sysaufs_fin();
24664+ au_dy_fin();
24665+}
24666+
24667+module_init(aufs_init);
24668+module_exit(aufs_exit);
24669diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
24670--- /usr/share/empty/fs/aufs/module.h 1970-01-01 01:00:00.000000000 +0100
24671+++ linux/fs/aufs/module.h 2021-02-22 22:51:59.061411920 +0100
24672@@ -0,0 +1,166 @@
24673+/* SPDX-License-Identifier: GPL-2.0 */
24674+/*
24675+ * Copyright (C) 2005-2020 Junjiro R. Okajima
24676+ *
24677+ * This program, aufs is free software; you can redistribute it and/or modify
24678+ * it under the terms of the GNU General Public License as published by
24679+ * the Free Software Foundation; either version 2 of the License, or
24680+ * (at your option) any later version.
24681+ *
24682+ * This program is distributed in the hope that it will be useful,
24683+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24684+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24685+ * GNU General Public License for more details.
24686+ *
24687+ * You should have received a copy of the GNU General Public License
24688+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
24689+ */
24690+
24691+/*
24692+ * module initialization and module-global
24693+ */
24694+
24695+#ifndef __AUFS_MODULE_H__
24696+#define __AUFS_MODULE_H__
24697+
24698+#ifdef __KERNEL__
24699+
24700+#include <linux/slab.h>
24701+#include "debug.h"
24702+#include "dentry.h"
24703+#include "dir.h"
24704+#include "file.h"
24705+#include "inode.h"
24706+
24707+struct path;
24708+struct seq_file;
24709+
24710+/* module parameters */
24711+extern int sysaufs_brs;
24712+extern bool au_userns;
24713+
24714+/* ---------------------------------------------------------------------- */
24715+
24716+extern int au_dir_roflags;
24717+
24718+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
24719+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24720+ int may_shrink);
24721+
24722+/*
24723+ * Comparing the size of the object with sizeof(struct rcu_head)
24724+ * case 1: object is always larger
24725+ * --> au_kfree_rcu() or au_kfree_do_rcu()
24726+ * case 2: object is always smaller
24727+ * --> au_kfree_small()
24728+ * case 3: object can be any size
24729+ * --> au_kfree_try_rcu()
24730+ */
24731+
24732+static inline void au_kfree_do_rcu(const void *p)
24733+{
24734+ struct {
24735+ struct rcu_head rcu;
24736+ } *a = (void *)p;
24737+
24738+ kfree_rcu(a, rcu);
24739+}
24740+
24741+#define au_kfree_rcu(_p) do { \
24742+ typeof(_p) p = (_p); \
24743+ BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head)); \
24744+ if (p) \
24745+ au_kfree_do_rcu(p); \
24746+ } while (0)
24747+
24748+#define au_kfree_do_sz_test(sz) (sz >= sizeof(struct rcu_head))
24749+#define au_kfree_sz_test(p) (p && au_kfree_do_sz_test(ksize(p)))
24750+
24751+static inline void au_kfree_try_rcu(const void *p)
24752+{
24753+ if (!p)
24754+ return;
24755+ if (au_kfree_sz_test(p))
24756+ au_kfree_do_rcu(p);
24757+ else
24758+ kfree(p);
24759+}
24760+
24761+static inline void au_kfree_small(const void *p)
24762+{
24763+ if (!p)
24764+ return;
24765+ AuDebugOn(au_kfree_sz_test(p));
24766+ kfree(p);
24767+}
24768+
24769+static inline int au_kmidx_sub(size_t sz, size_t new_sz)
24770+{
24771+#ifndef CONFIG_SLOB
24772+ return kmalloc_index(sz) - kmalloc_index(new_sz);
24773+#else
24774+ return -1; /* SLOB is untested */
24775+#endif
24776+}
24777+
24778+int au_seq_path(struct seq_file *seq, struct path *path);
24779+
24780+#ifdef CONFIG_PROC_FS
24781+/* procfs.c */
24782+int __init au_procfs_init(void);
24783+void au_procfs_fin(void);
24784+#else
24785+AuStubInt0(au_procfs_init, void);
24786+AuStubVoid(au_procfs_fin, void);
24787+#endif
24788+
24789+/* ---------------------------------------------------------------------- */
24790+
24791+/* kmem cache */
24792+enum {
24793+ AuCache_DINFO,
24794+ AuCache_ICNTNR,
24795+ AuCache_FINFO,
24796+ AuCache_VDIR,
24797+ AuCache_DEHSTR,
24798+ AuCache_HNOTIFY, /* must be last */
24799+ AuCache_Last
24800+};
24801+
24802+extern struct kmem_cache *au_cache[AuCache_Last];
24803+
24804+#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
24805+#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
24806+#define AuCacheCtor(type, ctor) \
24807+ kmem_cache_create(#type, sizeof(struct type), \
24808+ __alignof__(struct type), AuCacheFlags, ctor)
24809+
24810+#define AuCacheFuncs(name, index) \
24811+ static inline struct au_##name *au_cache_alloc_##name(void) \
24812+ { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
24813+ static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
24814+ { kmem_cache_free(au_cache[AuCache_##index], p); } \
24815+ \
24816+ static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
24817+ { void *p = rcu; \
24818+ p -= offsetof(struct au_##name, rcu); \
24819+ kmem_cache_free(au_cache[AuCache_##index], p); } \
24820+ static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
24821+ { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
24822+ call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); } \
24823+ \
24824+ static inline void au_cache_free_##name(struct au_##name *p) \
24825+ { /* au_cache_free_##name##_norcu(p); */ \
24826+ au_cache_free_##name##_rcu(p); }
24827+
24828+AuCacheFuncs(dinfo, DINFO);
24829+AuCacheFuncs(icntnr, ICNTNR);
24830+AuCacheFuncs(finfo, FINFO);
24831+AuCacheFuncs(vdir, VDIR);
24832+AuCacheFuncs(vdir_dehstr, DEHSTR);
24833+#ifdef CONFIG_AUFS_HNOTIFY
24834+AuCacheFuncs(hnotify, HNOTIFY);
24835+#endif
24836+
24837+#endif /* __KERNEL__ */
24838+#endif /* __AUFS_MODULE_H__ */
24839diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
24840--- /usr/share/empty/fs/aufs/mvdown.c 1970-01-01 01:00:00.000000000 +0100
24841+++ linux/fs/aufs/mvdown.c 2021-02-22 22:51:59.061411920 +0100
24842@@ -0,0 +1,706 @@
24843+// SPDX-License-Identifier: GPL-2.0
24844+/*
24845+ * Copyright (C) 2011-2020 Junjiro R. Okajima
24846+ *
24847+ * This program, aufs is free software; you can redistribute it and/or modify
24848+ * it under the terms of the GNU General Public License as published by
24849+ * the Free Software Foundation; either version 2 of the License, or
24850+ * (at your option) any later version.
24851+ *
24852+ * This program is distributed in the hope that it will be useful,
24853+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24854+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24855+ * GNU General Public License for more details.
24856+ *
24857+ * You should have received a copy of the GNU General Public License
24858+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
24859+ */
24860+
24861+/*
24862+ * move-down, opposite of copy-up
24863+ */
24864+
24865+#include "aufs.h"
24866+
24867+struct au_mvd_args {
24868+ struct {
24869+ struct super_block *h_sb;
24870+ struct dentry *h_parent;
24871+ struct au_hinode *hdir;
24872+ struct inode *h_dir, *h_inode;
24873+ struct au_pin pin;
24874+ } info[AUFS_MVDOWN_NARRAY];
24875+
24876+ struct aufs_mvdown mvdown;
24877+ struct dentry *dentry, *parent;
24878+ struct inode *inode, *dir;
24879+ struct super_block *sb;
24880+ aufs_bindex_t bopq, bwh, bfound;
24881+ unsigned char rename_lock;
24882+};
24883+
24884+#define mvd_errno mvdown.au_errno
24885+#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
24886+#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
24887+#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
24888+#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
24889+
24890+#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
24891+#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
24892+#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
24893+#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
24894+#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
24895+#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
24896+
24897+#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
24898+#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
24899+#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
24900+#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
24901+#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
24902+#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
24903+
24904+#define AU_MVD_PR(flag, ...) do { \
24905+ if (flag) \
24906+ pr_err(__VA_ARGS__); \
24907+ } while (0)
24908+
24909+static int find_lower_writable(struct au_mvd_args *a)
24910+{
24911+ struct super_block *sb;
24912+ aufs_bindex_t bindex, bbot;
24913+ struct au_branch *br;
24914+
24915+ sb = a->sb;
24916+ bindex = a->mvd_bsrc;
24917+ bbot = au_sbbot(sb);
24918+ if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
24919+ for (bindex++; bindex <= bbot; bindex++) {
24920+ br = au_sbr(sb, bindex);
24921+ if (au_br_fhsm(br->br_perm)
24922+ && !sb_rdonly(au_br_sb(br)))
24923+ return bindex;
24924+ }
24925+ else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
24926+ for (bindex++; bindex <= bbot; bindex++) {
24927+ br = au_sbr(sb, bindex);
24928+ if (!au_br_rdonly(br))
24929+ return bindex;
24930+ }
24931+ else
24932+ for (bindex++; bindex <= bbot; bindex++) {
24933+ br = au_sbr(sb, bindex);
24934+ if (!sb_rdonly(au_br_sb(br))) {
24935+ if (au_br_rdonly(br))
24936+ a->mvdown.flags
24937+ |= AUFS_MVDOWN_ROLOWER_R;
24938+ return bindex;
24939+ }
24940+ }
24941+
24942+ return -1;
24943+}
24944+
24945+/* make the parent dir on bdst */
24946+static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
24947+{
24948+ int err;
24949+
24950+ err = 0;
24951+ a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
24952+ a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
24953+ a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
24954+ a->mvd_h_dst_parent = NULL;
24955+ if (au_dbbot(a->parent) >= a->mvd_bdst)
24956+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
24957+ if (!a->mvd_h_dst_parent) {
24958+ err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
24959+ if (unlikely(err)) {
24960+ AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
24961+ goto out;
24962+ }
24963+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
24964+ }
24965+
24966+out:
24967+ AuTraceErr(err);
24968+ return err;
24969+}
24970+
24971+/* lock them all */
24972+static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
24973+{
24974+ int err;
24975+ struct dentry *h_trap;
24976+
24977+ a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
24978+ a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
24979+ err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
24980+ au_opt_udba(a->sb),
24981+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
24982+ AuTraceErr(err);
24983+ if (unlikely(err)) {
24984+ AU_MVD_PR(dmsg, "pin_dst failed\n");
24985+ goto out;
24986+ }
24987+
24988+ if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
24989+ a->rename_lock = 0;
24990+ au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
24991+ AuLsc_DI_PARENT, AuLsc_I_PARENT3,
24992+ au_opt_udba(a->sb),
24993+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
24994+ err = au_do_pin(&a->mvd_pin_src);
24995+ AuTraceErr(err);
24996+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
24997+ if (unlikely(err)) {
24998+ AU_MVD_PR(dmsg, "pin_src failed\n");
24999+ goto out_dst;
25000+ }
25001+ goto out; /* success */
25002+ }
25003+
25004+ a->rename_lock = 1;
25005+ au_pin_hdir_unlock(&a->mvd_pin_dst);
25006+ err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25007+ au_opt_udba(a->sb),
25008+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25009+ AuTraceErr(err);
25010+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
25011+ if (unlikely(err)) {
25012+ AU_MVD_PR(dmsg, "pin_src failed\n");
25013+ au_pin_hdir_lock(&a->mvd_pin_dst);
25014+ goto out_dst;
25015+ }
25016+ au_pin_hdir_unlock(&a->mvd_pin_src);
25017+ h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25018+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
25019+ if (h_trap) {
25020+ err = (h_trap != a->mvd_h_src_parent);
25021+ if (err)
25022+ err = (h_trap != a->mvd_h_dst_parent);
25023+ }
25024+ BUG_ON(err); /* it should never happen */
25025+ if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
25026+ err = -EBUSY;
25027+ AuTraceErr(err);
25028+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25029+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
25030+ au_pin_hdir_lock(&a->mvd_pin_src);
25031+ au_unpin(&a->mvd_pin_src);
25032+ au_pin_hdir_lock(&a->mvd_pin_dst);
25033+ goto out_dst;
25034+ }
25035+ goto out; /* success */
25036+
25037+out_dst:
25038+ au_unpin(&a->mvd_pin_dst);
25039+out:
25040+ AuTraceErr(err);
25041+ return err;
25042+}
25043+
25044+static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
25045+{
25046+ if (!a->rename_lock)
25047+ au_unpin(&a->mvd_pin_src);
25048+ else {
25049+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25050+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
25051+ au_pin_hdir_lock(&a->mvd_pin_src);
25052+ au_unpin(&a->mvd_pin_src);
25053+ au_pin_hdir_lock(&a->mvd_pin_dst);
25054+ }
25055+ au_unpin(&a->mvd_pin_dst);
25056+}
25057+
25058+/* copy-down the file */
25059+static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
25060+{
25061+ int err;
25062+ struct au_cp_generic cpg = {
25063+ .dentry = a->dentry,
25064+ .bdst = a->mvd_bdst,
25065+ .bsrc = a->mvd_bsrc,
25066+ .len = -1,
25067+ .pin = &a->mvd_pin_dst,
25068+ .flags = AuCpup_DTIME | AuCpup_HOPEN
25069+ };
25070+
25071+ AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
25072+ if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25073+ au_fset_cpup(cpg.flags, OVERWRITE);
25074+ if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
25075+ au_fset_cpup(cpg.flags, RWDST);
25076+ err = au_sio_cpdown_simple(&cpg);
25077+ if (unlikely(err))
25078+ AU_MVD_PR(dmsg, "cpdown failed\n");
25079+
25080+ AuTraceErr(err);
25081+ return err;
25082+}
25083+
25084+/*
25085+ * unlink the whiteout on bdst if exist which may be created by UDBA while we
25086+ * were sleeping
25087+ */
25088+static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
25089+{
25090+ int err;
25091+ struct path h_path;
25092+ struct au_branch *br;
25093+ struct inode *delegated;
25094+
25095+ br = au_sbr(a->sb, a->mvd_bdst);
25096+ h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
25097+ err = PTR_ERR(h_path.dentry);
25098+ if (IS_ERR(h_path.dentry)) {
25099+ AU_MVD_PR(dmsg, "wh_lkup failed\n");
25100+ goto out;
25101+ }
25102+
25103+ err = 0;
25104+ if (d_is_positive(h_path.dentry)) {
25105+ h_path.mnt = au_br_mnt(br);
25106+ delegated = NULL;
25107+ err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
25108+ &delegated, /*force*/0);
25109+ if (unlikely(err == -EWOULDBLOCK)) {
25110+ pr_warn("cannot retry for NFSv4 delegation"
25111+ " for an internal unlink\n");
25112+ iput(delegated);
25113+ }
25114+ if (unlikely(err))
25115+ AU_MVD_PR(dmsg, "wh_unlink failed\n");
25116+ }
25117+ dput(h_path.dentry);
25118+
25119+out:
25120+ AuTraceErr(err);
25121+ return err;
25122+}
25123+
25124+/*
25125+ * unlink the topmost h_dentry
25126+ */
25127+static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
25128+{
25129+ int err;
25130+ struct path h_path;
25131+ struct inode *delegated;
25132+
25133+ h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
25134+ h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
25135+ delegated = NULL;
25136+ err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
25137+ if (unlikely(err == -EWOULDBLOCK)) {
25138+ pr_warn("cannot retry for NFSv4 delegation"
25139+ " for an internal unlink\n");
25140+ iput(delegated);
25141+ }
25142+ if (unlikely(err))
25143+ AU_MVD_PR(dmsg, "unlink failed\n");
25144+
25145+ AuTraceErr(err);
25146+ return err;
25147+}
25148+
25149+/* Since mvdown succeeded, we ignore an error of this function */
25150+static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
25151+{
25152+ int err;
25153+ struct au_branch *br;
25154+
25155+ a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
25156+ br = au_sbr(a->sb, a->mvd_bsrc);
25157+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
25158+ if (!err) {
25159+ br = au_sbr(a->sb, a->mvd_bdst);
25160+ a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
25161+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
25162+ }
25163+ if (!err)
25164+ a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
25165+ else
25166+ AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
25167+}
25168+
25169+/*
25170+ * copy-down the file and unlink the bsrc file.
25171+ * - unlink the bdst whout if exist
25172+ * - copy-down the file (with whtmp name and rename)
25173+ * - unlink the bsrc file
25174+ */
25175+static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
25176+{
25177+ int err;
25178+
25179+ err = au_do_mkdir(dmsg, a);
25180+ if (!err)
25181+ err = au_do_lock(dmsg, a);
25182+ if (unlikely(err))
25183+ goto out;
25184+
25185+ /*
25186+ * do not revert the activities we made on bdst since they should be
25187+ * harmless in aufs.
25188+ */
25189+
25190+ err = au_do_cpdown(dmsg, a);
25191+ if (!err)
25192+ err = au_do_unlink_wh(dmsg, a);
25193+ if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
25194+ err = au_do_unlink(dmsg, a);
25195+ if (unlikely(err))
25196+ goto out_unlock;
25197+
25198+ AuDbg("%pd2, 0x%x, %d --> %d\n",
25199+ a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
25200+ if (find_lower_writable(a) < 0)
25201+ a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
25202+
25203+ if (a->mvdown.flags & AUFS_MVDOWN_STFS)
25204+ au_do_stfs(dmsg, a);
25205+
25206+ /* maintain internal array */
25207+ if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
25208+ au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
25209+ au_set_dbtop(a->dentry, a->mvd_bdst);
25210+ au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
25211+ au_set_ibtop(a->inode, a->mvd_bdst);
25212+ } else {
25213+ /* hide the lower */
25214+ au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
25215+ au_set_dbbot(a->dentry, a->mvd_bsrc);
25216+ au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
25217+ au_set_ibbot(a->inode, a->mvd_bsrc);
25218+ }
25219+ if (au_dbbot(a->dentry) < a->mvd_bdst)
25220+ au_set_dbbot(a->dentry, a->mvd_bdst);
25221+ if (au_ibbot(a->inode) < a->mvd_bdst)
25222+ au_set_ibbot(a->inode, a->mvd_bdst);
25223+
25224+out_unlock:
25225+ au_do_unlock(dmsg, a);
25226+out:
25227+ AuTraceErr(err);
25228+ return err;
25229+}
25230+
25231+/* ---------------------------------------------------------------------- */
25232+
25233+/* make sure the file is idle */
25234+static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
25235+{
25236+ int err, plinked;
25237+
25238+ err = 0;
25239+ plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
25240+ if (au_dbtop(a->dentry) == a->mvd_bsrc
25241+ && au_dcount(a->dentry) == 1
25242+ && atomic_read(&a->inode->i_count) == 1
25243+ /* && a->mvd_h_src_inode->i_nlink == 1 */
25244+ && (!plinked || !au_plink_test(a->inode))
25245+ && a->inode->i_nlink == 1)
25246+ goto out;
25247+
25248+ err = -EBUSY;
25249+ AU_MVD_PR(dmsg,
25250+ "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
25251+ a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
25252+ atomic_read(&a->inode->i_count), a->inode->i_nlink,
25253+ a->mvd_h_src_inode->i_nlink,
25254+ plinked, plinked ? au_plink_test(a->inode) : 0);
25255+
25256+out:
25257+ AuTraceErr(err);
25258+ return err;
25259+}
25260+
25261+/* make sure the parent dir is fine */
25262+static int au_mvd_args_parent(const unsigned char dmsg,
25263+ struct au_mvd_args *a)
25264+{
25265+ int err;
25266+ aufs_bindex_t bindex;
25267+
25268+ err = 0;
25269+ if (unlikely(au_alive_dir(a->parent))) {
25270+ err = -ENOENT;
25271+ AU_MVD_PR(dmsg, "parent dir is dead\n");
25272+ goto out;
25273+ }
25274+
25275+ a->bopq = au_dbdiropq(a->parent);
25276+ bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
25277+ AuDbg("b%d\n", bindex);
25278+ if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
25279+ || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
25280+ err = -EINVAL;
25281+ a->mvd_errno = EAU_MVDOWN_OPAQUE;
25282+ AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
25283+ a->bopq, a->mvd_bdst);
25284+ }
25285+
25286+out:
25287+ AuTraceErr(err);
25288+ return err;
25289+}
25290+
25291+static int au_mvd_args_intermediate(const unsigned char dmsg,
25292+ struct au_mvd_args *a)
25293+{
25294+ int err;
25295+ struct au_dinfo *dinfo, *tmp;
25296+
25297+ /* lookup the next lower positive entry */
25298+ err = -ENOMEM;
25299+ tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
25300+ if (unlikely(!tmp))
25301+ goto out;
25302+
25303+ a->bfound = -1;
25304+ a->bwh = -1;
25305+ dinfo = au_di(a->dentry);
25306+ au_di_cp(tmp, dinfo);
25307+ au_di_swap(tmp, dinfo);
25308+
25309+ /* returns the number of positive dentries */
25310+ err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
25311+ /* AuLkup_IGNORE_PERM */ 0);
25312+ if (!err)
25313+ a->bwh = au_dbwh(a->dentry);
25314+ else if (err > 0)
25315+ a->bfound = au_dbtop(a->dentry);
25316+
25317+ au_di_swap(tmp, dinfo);
25318+ au_rw_write_unlock(&tmp->di_rwsem);
25319+ au_di_free(tmp);
25320+ if (unlikely(err < 0))
25321+ AU_MVD_PR(dmsg, "failed look-up lower\n");
25322+
25323+ /*
25324+ * here, we have these cases.
25325+ * bfound == -1
25326+ * no positive dentry under bsrc. there are more sub-cases.
25327+ * bwh < 0
25328+ * there no whiteout, we can safely move-down.
25329+ * bwh <= bsrc
25330+ * impossible
25331+ * bsrc < bwh && bwh < bdst
25332+ * there is a whiteout on RO branch. cannot proceed.
25333+ * bwh == bdst
25334+ * there is a whiteout on the RW target branch. it should
25335+ * be removed.
25336+ * bdst < bwh
25337+ * there is a whiteout somewhere unrelated branch.
25338+ * -1 < bfound && bfound <= bsrc
25339+ * impossible.
25340+ * bfound < bdst
25341+ * found, but it is on RO branch between bsrc and bdst. cannot
25342+ * proceed.
25343+ * bfound == bdst
25344+ * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
25345+ * error.
25346+ * bdst < bfound
25347+ * found, after we create the file on bdst, it will be hidden.
25348+ */
25349+
25350+ AuDebugOn(a->bfound == -1
25351+ && a->bwh != -1
25352+ && a->bwh <= a->mvd_bsrc);
25353+ AuDebugOn(-1 < a->bfound
25354+ && a->bfound <= a->mvd_bsrc);
25355+
25356+ err = -EINVAL;
25357+ if (a->bfound == -1
25358+ && a->mvd_bsrc < a->bwh
25359+ && a->bwh != -1
25360+ && a->bwh < a->mvd_bdst) {
25361+ a->mvd_errno = EAU_MVDOWN_WHITEOUT;
25362+ AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
25363+ a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
25364+ goto out;
25365+ } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
25366+ a->mvd_errno = EAU_MVDOWN_UPPER;
25367+ AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
25368+ a->mvd_bdst, a->bfound);
25369+ goto out;
25370+ }
25371+
25372+ err = 0; /* success */
25373+
25374+out:
25375+ AuTraceErr(err);
25376+ return err;
25377+}
25378+
25379+static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
25380+{
25381+ int err;
25382+
25383+ err = 0;
25384+ if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25385+ && a->bfound == a->mvd_bdst)
25386+ err = -EEXIST;
25387+ AuTraceErr(err);
25388+ return err;
25389+}
25390+
25391+static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
25392+{
25393+ int err;
25394+ struct au_branch *br;
25395+
25396+ err = -EISDIR;
25397+ if (unlikely(S_ISDIR(a->inode->i_mode)))
25398+ goto out;
25399+
25400+ err = -EINVAL;
25401+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
25402+ a->mvd_bsrc = au_ibtop(a->inode);
25403+ else {
25404+ a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
25405+ if (unlikely(a->mvd_bsrc < 0
25406+ || (a->mvd_bsrc < au_dbtop(a->dentry)
25407+ || au_dbbot(a->dentry) < a->mvd_bsrc
25408+ || !au_h_dptr(a->dentry, a->mvd_bsrc))
25409+ || (a->mvd_bsrc < au_ibtop(a->inode)
25410+ || au_ibbot(a->inode) < a->mvd_bsrc
25411+ || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
25412+ a->mvd_errno = EAU_MVDOWN_NOUPPER;
25413+ AU_MVD_PR(dmsg, "no upper\n");
25414+ goto out;
25415+ }
25416+ }
25417+ if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
25418+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25419+ AU_MVD_PR(dmsg, "on the bottom\n");
25420+ goto out;
25421+ }
25422+ a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
25423+ br = au_sbr(a->sb, a->mvd_bsrc);
25424+ err = au_br_rdonly(br);
25425+ if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
25426+ if (unlikely(err))
25427+ goto out;
25428+ } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
25429+ || IS_APPEND(a->mvd_h_src_inode))) {
25430+ if (err)
25431+ a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
25432+ /* go on */
25433+ } else
25434+ goto out;
25435+
25436+ err = -EINVAL;
25437+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
25438+ a->mvd_bdst = find_lower_writable(a);
25439+ if (unlikely(a->mvd_bdst < 0)) {
25440+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25441+ AU_MVD_PR(dmsg, "no writable lower branch\n");
25442+ goto out;
25443+ }
25444+ } else {
25445+ a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
25446+ if (unlikely(a->mvd_bdst < 0
25447+ || au_sbbot(a->sb) < a->mvd_bdst)) {
25448+ a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
25449+ AU_MVD_PR(dmsg, "no lower brid\n");
25450+ goto out;
25451+ }
25452+ }
25453+
25454+ err = au_mvd_args_busy(dmsg, a);
25455+ if (!err)
25456+ err = au_mvd_args_parent(dmsg, a);
25457+ if (!err)
25458+ err = au_mvd_args_intermediate(dmsg, a);
25459+ if (!err)
25460+ err = au_mvd_args_exist(dmsg, a);
25461+ if (!err)
25462+ AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
25463+
25464+out:
25465+ AuTraceErr(err);
25466+ return err;
25467+}
25468+
25469+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
25470+{
25471+ int err, e;
25472+ unsigned char dmsg;
25473+ struct au_mvd_args *args;
25474+ struct inode *inode;
25475+
25476+ inode = d_inode(dentry);
25477+ err = -EPERM;
25478+ if (unlikely(!capable(CAP_SYS_ADMIN)))
25479+ goto out;
25480+
25481+ err = -ENOMEM;
25482+ args = kmalloc(sizeof(*args), GFP_NOFS);
25483+ if (unlikely(!args))
25484+ goto out;
25485+
25486+ err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
25487+ if (!err)
25488+ /* VERIFY_WRITE */
25489+ err = !access_ok(uarg, sizeof(*uarg));
25490+ if (unlikely(err)) {
25491+ err = -EFAULT;
25492+ AuTraceErr(err);
25493+ goto out_free;
25494+ }
25495+ AuDbg("flags 0x%x\n", args->mvdown.flags);
25496+ args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
25497+ args->mvdown.au_errno = 0;
25498+ args->dentry = dentry;
25499+ args->inode = inode;
25500+ args->sb = dentry->d_sb;
25501+
25502+ err = -ENOENT;
25503+ dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
25504+ args->parent = dget_parent(dentry);
25505+ args->dir = d_inode(args->parent);
25506+ inode_lock_nested(args->dir, I_MUTEX_PARENT);
25507+ dput(args->parent);
25508+ if (unlikely(args->parent != dentry->d_parent)) {
25509+ AU_MVD_PR(dmsg, "parent dir is moved\n");
25510+ goto out_dir;
25511+ }
25512+
25513+ inode_lock_nested(inode, I_MUTEX_CHILD);
25514+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
25515+ if (unlikely(err))
25516+ goto out_inode;
25517+
25518+ di_write_lock_parent(args->parent);
25519+ err = au_mvd_args(dmsg, args);
25520+ if (unlikely(err))
25521+ goto out_parent;
25522+
25523+ err = au_do_mvdown(dmsg, args);
25524+ if (unlikely(err))
25525+ goto out_parent;
25526+
25527+ au_cpup_attr_timesizes(args->dir);
25528+ au_cpup_attr_timesizes(inode);
25529+ if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
25530+ au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
25531+ /* au_digen_dec(dentry); */
25532+
25533+out_parent:
25534+ di_write_unlock(args->parent);
25535+ aufs_read_unlock(dentry, AuLock_DW);
25536+out_inode:
25537+ inode_unlock(inode);
25538+out_dir:
25539+ inode_unlock(args->dir);
25540+out_free:
25541+ e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
25542+ if (unlikely(e))
25543+ err = -EFAULT;
25544+ au_kfree_rcu(args);
25545+out:
25546+ AuTraceErr(err);
25547+ return err;
25548+}
25549diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
25550--- /usr/share/empty/fs/aufs/opts.c 1970-01-01 01:00:00.000000000 +0100
25551+++ linux/fs/aufs/opts.c 2021-02-22 22:51:59.061411920 +0100
25552@@ -0,0 +1,1880 @@
25553+// SPDX-License-Identifier: GPL-2.0
25554+/*
25555+ * Copyright (C) 2005-2020 Junjiro R. Okajima
25556+ *
25557+ * This program, aufs is free software; you can redistribute it and/or modify
25558+ * it under the terms of the GNU General Public License as published by
25559+ * the Free Software Foundation; either version 2 of the License, or
25560+ * (at your option) any later version.
25561+ *
25562+ * This program is distributed in the hope that it will be useful,
25563+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25564+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25565+ * GNU General Public License for more details.
25566+ *
25567+ * You should have received a copy of the GNU General Public License
25568+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
25569+ */
25570+
25571+/*
25572+ * mount options/flags
25573+ */
25574+
25575+#include <linux/namei.h>
25576+#include <linux/types.h> /* a distribution requires */
25577+#include <linux/parser.h>
25578+#include "aufs.h"
25579+
25580+/* ---------------------------------------------------------------------- */
25581+
25582+enum {
25583+ Opt_br,
25584+ Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
25585+ Opt_idel, Opt_imod,
25586+ Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
25587+ Opt_rdblk_def, Opt_rdhash_def,
25588+ Opt_xino, Opt_noxino,
25589+ Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
25590+ Opt_trunc_xino_path, Opt_itrunc_xino,
25591+ Opt_trunc_xib, Opt_notrunc_xib,
25592+ Opt_shwh, Opt_noshwh,
25593+ Opt_plink, Opt_noplink, Opt_list_plink,
25594+ Opt_udba,
25595+ Opt_dio, Opt_nodio,
25596+ Opt_diropq_a, Opt_diropq_w,
25597+ Opt_warn_perm, Opt_nowarn_perm,
25598+ Opt_wbr_copyup, Opt_wbr_create,
25599+ Opt_fhsm_sec,
25600+ Opt_verbose, Opt_noverbose,
25601+ Opt_sum, Opt_nosum, Opt_wsum,
25602+ Opt_dirperm1, Opt_nodirperm1,
25603+ Opt_dirren, Opt_nodirren,
25604+ Opt_acl, Opt_noacl,
25605+ Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
25606+};
25607+
25608+static match_table_t options = {
25609+ {Opt_br, "br=%s"},
25610+ {Opt_br, "br:%s"},
25611+
25612+ {Opt_add, "add=%d:%s"},
25613+ {Opt_add, "add:%d:%s"},
25614+ {Opt_add, "ins=%d:%s"},
25615+ {Opt_add, "ins:%d:%s"},
25616+ {Opt_append, "append=%s"},
25617+ {Opt_append, "append:%s"},
25618+ {Opt_prepend, "prepend=%s"},
25619+ {Opt_prepend, "prepend:%s"},
25620+
25621+ {Opt_del, "del=%s"},
25622+ {Opt_del, "del:%s"},
25623+ /* {Opt_idel, "idel:%d"}, */
25624+ {Opt_mod, "mod=%s"},
25625+ {Opt_mod, "mod:%s"},
25626+ /* {Opt_imod, "imod:%d:%s"}, */
25627+
25628+ {Opt_dirwh, "dirwh=%d"},
25629+
25630+ {Opt_xino, "xino=%s"},
25631+ {Opt_noxino, "noxino"},
25632+ {Opt_trunc_xino, "trunc_xino"},
25633+ {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
25634+ {Opt_notrunc_xino, "notrunc_xino"},
25635+ {Opt_trunc_xino_path, "trunc_xino=%s"},
25636+ {Opt_itrunc_xino, "itrunc_xino=%d"},
25637+ /* {Opt_zxino, "zxino=%s"}, */
25638+ {Opt_trunc_xib, "trunc_xib"},
25639+ {Opt_notrunc_xib, "notrunc_xib"},
25640+
25641+#ifdef CONFIG_PROC_FS
25642+ {Opt_plink, "plink"},
25643+#else
25644+ {Opt_ignore_silent, "plink"},
25645+#endif
25646+
25647+ {Opt_noplink, "noplink"},
25648+
25649+#ifdef CONFIG_AUFS_DEBUG
25650+ {Opt_list_plink, "list_plink"},
25651+#endif
25652+
25653+ {Opt_udba, "udba=%s"},
25654+
25655+ {Opt_dio, "dio"},
25656+ {Opt_nodio, "nodio"},
25657+
25658+#ifdef CONFIG_AUFS_DIRREN
25659+ {Opt_dirren, "dirren"},
25660+ {Opt_nodirren, "nodirren"},
25661+#else
25662+ {Opt_ignore, "dirren"},
25663+ {Opt_ignore_silent, "nodirren"},
25664+#endif
25665+
25666+#ifdef CONFIG_AUFS_FHSM
25667+ {Opt_fhsm_sec, "fhsm_sec=%d"},
25668+#else
25669+ {Opt_ignore, "fhsm_sec=%d"},
25670+#endif
25671+
25672+ {Opt_diropq_a, "diropq=always"},
25673+ {Opt_diropq_a, "diropq=a"},
25674+ {Opt_diropq_w, "diropq=whiteouted"},
25675+ {Opt_diropq_w, "diropq=w"},
25676+
25677+ {Opt_warn_perm, "warn_perm"},
25678+ {Opt_nowarn_perm, "nowarn_perm"},
25679+
25680+ /* keep them temporary */
25681+ {Opt_ignore_silent, "nodlgt"},
25682+ {Opt_ignore, "clean_plink"},
25683+
25684+#ifdef CONFIG_AUFS_SHWH
25685+ {Opt_shwh, "shwh"},
25686+#endif
25687+ {Opt_noshwh, "noshwh"},
25688+
25689+ {Opt_dirperm1, "dirperm1"},
25690+ {Opt_nodirperm1, "nodirperm1"},
25691+
25692+ {Opt_verbose, "verbose"},
25693+ {Opt_verbose, "v"},
25694+ {Opt_noverbose, "noverbose"},
25695+ {Opt_noverbose, "quiet"},
25696+ {Opt_noverbose, "q"},
25697+ {Opt_noverbose, "silent"},
25698+
25699+ {Opt_sum, "sum"},
25700+ {Opt_nosum, "nosum"},
25701+ {Opt_wsum, "wsum"},
25702+
25703+ {Opt_rdcache, "rdcache=%d"},
25704+ {Opt_rdblk, "rdblk=%d"},
25705+ {Opt_rdblk_def, "rdblk=def"},
25706+ {Opt_rdhash, "rdhash=%d"},
25707+ {Opt_rdhash_def, "rdhash=def"},
25708+
25709+ {Opt_wbr_create, "create=%s"},
25710+ {Opt_wbr_create, "create_policy=%s"},
25711+ {Opt_wbr_copyup, "cpup=%s"},
25712+ {Opt_wbr_copyup, "copyup=%s"},
25713+ {Opt_wbr_copyup, "copyup_policy=%s"},
25714+
25715+ /* generic VFS flag */
25716+#ifdef CONFIG_FS_POSIX_ACL
25717+ {Opt_acl, "acl"},
25718+ {Opt_noacl, "noacl"},
25719+#else
25720+ {Opt_ignore, "acl"},
25721+ {Opt_ignore_silent, "noacl"},
25722+#endif
25723+
25724+ /* internal use for the scripts */
25725+ {Opt_ignore_silent, "si=%s"},
25726+
25727+ {Opt_br, "dirs=%s"},
25728+ {Opt_ignore, "debug=%d"},
25729+ {Opt_ignore, "delete=whiteout"},
25730+ {Opt_ignore, "delete=all"},
25731+ {Opt_ignore, "imap=%s"},
25732+
25733+ /* temporary workaround, due to old mount(8)? */
25734+ {Opt_ignore_silent, "relatime"},
25735+
25736+ {Opt_err, NULL}
25737+};
25738+
25739+/* ---------------------------------------------------------------------- */
25740+
25741+static const char *au_parser_pattern(int val, match_table_t tbl)
25742+{
25743+ struct match_token *p;
25744+
25745+ p = tbl;
25746+ while (p->pattern) {
25747+ if (p->token == val)
25748+ return p->pattern;
25749+ p++;
25750+ }
25751+ BUG();
25752+ return "??";
25753+}
25754+
25755+static const char *au_optstr(int *val, match_table_t tbl)
25756+{
25757+ struct match_token *p;
25758+ int v;
25759+
25760+ v = *val;
25761+ if (!v)
25762+ goto out;
25763+ p = tbl;
25764+ while (p->pattern) {
25765+ if (p->token
25766+ && (v & p->token) == p->token) {
25767+ *val &= ~p->token;
25768+ return p->pattern;
25769+ }
25770+ p++;
25771+ }
25772+
25773+out:
25774+ return NULL;
25775+}
25776+
25777+/* ---------------------------------------------------------------------- */
25778+
25779+static match_table_t brperm = {
25780+ {AuBrPerm_RO, AUFS_BRPERM_RO},
25781+ {AuBrPerm_RR, AUFS_BRPERM_RR},
25782+ {AuBrPerm_RW, AUFS_BRPERM_RW},
25783+ {0, NULL}
25784+};
25785+
25786+static match_table_t brattr = {
25787+ /* general */
25788+ {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
25789+ {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
25790+ /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
25791+ {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
25792+#ifdef CONFIG_AUFS_FHSM
25793+ {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
25794+#endif
25795+#ifdef CONFIG_AUFS_XATTR
25796+ {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
25797+ {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
25798+ {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
25799+ {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
25800+ {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
25801+ {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
25802+#endif
25803+
25804+ /* ro/rr branch */
25805+ {AuBrRAttr_WH, AUFS_BRRATTR_WH},
25806+
25807+ /* rw branch */
25808+ {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
25809+ {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
25810+
25811+ {0, NULL}
25812+};
25813+
25814+static int br_attr_val(char *str, match_table_t table, substring_t args[])
25815+{
25816+ int attr, v;
25817+ char *p;
25818+
25819+ attr = 0;
25820+ do {
25821+ p = strchr(str, '+');
25822+ if (p)
25823+ *p = 0;
25824+ v = match_token(str, table, args);
25825+ if (v) {
25826+ if (v & AuBrAttr_CMOO_Mask)
25827+ attr &= ~AuBrAttr_CMOO_Mask;
25828+ attr |= v;
25829+ } else {
25830+ if (p)
25831+ *p = '+';
25832+ pr_warn("ignored branch attribute %s\n", str);
25833+ break;
25834+ }
25835+ if (p)
25836+ str = p + 1;
25837+ } while (p);
25838+
25839+ return attr;
25840+}
25841+
25842+static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
25843+{
25844+ int sz;
25845+ const char *p;
25846+ char *q;
25847+
25848+ q = str->a;
25849+ *q = 0;
25850+ p = au_optstr(&perm, brattr);
25851+ if (p) {
25852+ sz = strlen(p);
25853+ memcpy(q, p, sz + 1);
25854+ q += sz;
25855+ } else
25856+ goto out;
25857+
25858+ do {
25859+ p = au_optstr(&perm, brattr);
25860+ if (p) {
25861+ *q++ = '+';
25862+ sz = strlen(p);
25863+ memcpy(q, p, sz + 1);
25864+ q += sz;
25865+ }
25866+ } while (p);
25867+
25868+out:
25869+ return q - str->a;
25870+}
25871+
25872+static int noinline_for_stack br_perm_val(char *perm)
25873+{
25874+ int val, bad, sz;
25875+ char *p;
25876+ substring_t args[MAX_OPT_ARGS];
25877+ au_br_perm_str_t attr;
25878+
25879+ p = strchr(perm, '+');
25880+ if (p)
25881+ *p = 0;
25882+ val = match_token(perm, brperm, args);
25883+ if (!val) {
25884+ if (p)
25885+ *p = '+';
25886+ pr_warn("ignored branch permission %s\n", perm);
25887+ val = AuBrPerm_RO;
25888+ goto out;
25889+ }
25890+ if (!p)
25891+ goto out;
25892+
25893+ val |= br_attr_val(p + 1, brattr, args);
25894+
25895+ bad = 0;
25896+ switch (val & AuBrPerm_Mask) {
25897+ case AuBrPerm_RO:
25898+ case AuBrPerm_RR:
25899+ bad = val & AuBrWAttr_Mask;
25900+ val &= ~AuBrWAttr_Mask;
25901+ break;
25902+ case AuBrPerm_RW:
25903+ bad = val & AuBrRAttr_Mask;
25904+ val &= ~AuBrRAttr_Mask;
25905+ break;
25906+ }
25907+
25908+ /*
25909+ * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
25910+ * does not treat it as an error, just warning.
25911+ * this is a tiny guard for the user operation.
25912+ */
25913+ if (val & AuBrAttr_UNPIN) {
25914+ bad |= AuBrAttr_UNPIN;
25915+ val &= ~AuBrAttr_UNPIN;
25916+ }
25917+
25918+ if (unlikely(bad)) {
25919+ sz = au_do_optstr_br_attr(&attr, bad);
25920+ AuDebugOn(!sz);
25921+ pr_warn("ignored branch attribute %s\n", attr.a);
25922+ }
25923+
25924+out:
25925+ return val;
25926+}
25927+
25928+void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
25929+{
25930+ au_br_perm_str_t attr;
25931+ const char *p;
25932+ char *q;
25933+ int sz;
25934+
25935+ q = str->a;
25936+ p = au_optstr(&perm, brperm);
25937+ AuDebugOn(!p || !*p);
25938+ sz = strlen(p);
25939+ memcpy(q, p, sz + 1);
25940+ q += sz;
25941+
25942+ sz = au_do_optstr_br_attr(&attr, perm);
25943+ if (sz) {
25944+ *q++ = '+';
25945+ memcpy(q, attr.a, sz + 1);
25946+ }
25947+
25948+ AuDebugOn(strlen(str->a) >= sizeof(str->a));
25949+}
25950+
25951+/* ---------------------------------------------------------------------- */
25952+
25953+static match_table_t udbalevel = {
25954+ {AuOpt_UDBA_REVAL, "reval"},
25955+ {AuOpt_UDBA_NONE, "none"},
25956+#ifdef CONFIG_AUFS_HNOTIFY
25957+ {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
25958+#ifdef CONFIG_AUFS_HFSNOTIFY
25959+ {AuOpt_UDBA_HNOTIFY, "fsnotify"},
25960+#endif
25961+#endif
25962+ {-1, NULL}
25963+};
25964+
25965+static int noinline_for_stack udba_val(char *str)
25966+{
25967+ substring_t args[MAX_OPT_ARGS];
25968+
25969+ return match_token(str, udbalevel, args);
25970+}
25971+
25972+const char *au_optstr_udba(int udba)
25973+{
25974+ return au_parser_pattern(udba, udbalevel);
25975+}
25976+
25977+/* ---------------------------------------------------------------------- */
25978+
25979+static match_table_t au_wbr_create_policy = {
25980+ {AuWbrCreate_TDP, "tdp"},
25981+ {AuWbrCreate_TDP, "top-down-parent"},
25982+ {AuWbrCreate_RR, "rr"},
25983+ {AuWbrCreate_RR, "round-robin"},
25984+ {AuWbrCreate_MFS, "mfs"},
25985+ {AuWbrCreate_MFS, "most-free-space"},
25986+ {AuWbrCreate_MFSV, "mfs:%d"},
25987+ {AuWbrCreate_MFSV, "most-free-space:%d"},
25988+
25989+ /* top-down regardless the parent, and then mfs */
25990+ {AuWbrCreate_TDMFS, "tdmfs:%d"},
25991+ {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
25992+
25993+ {AuWbrCreate_MFSRR, "mfsrr:%d"},
25994+ {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
25995+ {AuWbrCreate_PMFS, "pmfs"},
25996+ {AuWbrCreate_PMFSV, "pmfs:%d"},
25997+ {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
25998+ {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
25999+
26000+ {-1, NULL}
26001+};
26002+
26003+static int au_wbr_mfs_wmark(substring_t *arg, char *str,
26004+ struct au_opt_wbr_create *create)
26005+{
26006+ int err;
26007+ unsigned long long ull;
26008+
26009+ err = 0;
26010+ if (!match_u64(arg, &ull))
26011+ create->mfsrr_watermark = ull;
26012+ else {
26013+ pr_err("bad integer in %s\n", str);
26014+ err = -EINVAL;
26015+ }
26016+
26017+ return err;
26018+}
26019+
26020+static int au_wbr_mfs_sec(substring_t *arg, char *str,
26021+ struct au_opt_wbr_create *create)
26022+{
26023+ int n, err;
26024+
26025+ err = 0;
26026+ if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
26027+ create->mfs_second = n;
26028+ else {
26029+ pr_err("bad integer in %s\n", str);
26030+ err = -EINVAL;
26031+ }
26032+
26033+ return err;
26034+}
26035+
26036+static int noinline_for_stack
26037+au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
26038+{
26039+ int err, e;
26040+ substring_t args[MAX_OPT_ARGS];
26041+
26042+ err = match_token(str, au_wbr_create_policy, args);
26043+ create->wbr_create = err;
26044+ switch (err) {
26045+ case AuWbrCreate_MFSRRV:
26046+ case AuWbrCreate_TDMFSV:
26047+ case AuWbrCreate_PMFSRRV:
26048+ e = au_wbr_mfs_wmark(&args[0], str, create);
26049+ if (!e)
26050+ e = au_wbr_mfs_sec(&args[1], str, create);
26051+ if (unlikely(e))
26052+ err = e;
26053+ break;
26054+ case AuWbrCreate_MFSRR:
26055+ case AuWbrCreate_TDMFS:
26056+ case AuWbrCreate_PMFSRR:
26057+ e = au_wbr_mfs_wmark(&args[0], str, create);
26058+ if (unlikely(e)) {
26059+ err = e;
26060+ break;
26061+ }
26062+ fallthrough;
26063+ case AuWbrCreate_MFS:
26064+ case AuWbrCreate_PMFS:
26065+ create->mfs_second = AUFS_MFS_DEF_SEC;
26066+ break;
26067+ case AuWbrCreate_MFSV:
26068+ case AuWbrCreate_PMFSV:
26069+ e = au_wbr_mfs_sec(&args[0], str, create);
26070+ if (unlikely(e))
26071+ err = e;
26072+ break;
26073+ }
26074+
26075+ return err;
26076+}
26077+
26078+const char *au_optstr_wbr_create(int wbr_create)
26079+{
26080+ return au_parser_pattern(wbr_create, au_wbr_create_policy);
26081+}
26082+
26083+static match_table_t au_wbr_copyup_policy = {
26084+ {AuWbrCopyup_TDP, "tdp"},
26085+ {AuWbrCopyup_TDP, "top-down-parent"},
26086+ {AuWbrCopyup_BUP, "bup"},
26087+ {AuWbrCopyup_BUP, "bottom-up-parent"},
26088+ {AuWbrCopyup_BU, "bu"},
26089+ {AuWbrCopyup_BU, "bottom-up"},
26090+ {-1, NULL}
26091+};
26092+
26093+static int noinline_for_stack au_wbr_copyup_val(char *str)
26094+{
26095+ substring_t args[MAX_OPT_ARGS];
26096+
26097+ return match_token(str, au_wbr_copyup_policy, args);
26098+}
26099+
26100+const char *au_optstr_wbr_copyup(int wbr_copyup)
26101+{
26102+ return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
26103+}
26104+
26105+/* ---------------------------------------------------------------------- */
26106+
26107+static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
26108+
26109+static void dump_opts(struct au_opts *opts)
26110+{
26111+#ifdef CONFIG_AUFS_DEBUG
26112+ /* reduce stack space */
26113+ union {
26114+ struct au_opt_add *add;
26115+ struct au_opt_del *del;
26116+ struct au_opt_mod *mod;
26117+ struct au_opt_xino *xino;
26118+ struct au_opt_xino_itrunc *xino_itrunc;
26119+ struct au_opt_wbr_create *create;
26120+ } u;
26121+ struct au_opt *opt;
26122+
26123+ opt = opts->opt;
26124+ while (opt->type != Opt_tail) {
26125+ switch (opt->type) {
26126+ case Opt_add:
26127+ u.add = &opt->add;
26128+ AuDbg("add {b%d, %s, 0x%x, %p}\n",
26129+ u.add->bindex, u.add->pathname, u.add->perm,
26130+ u.add->path.dentry);
26131+ break;
26132+ case Opt_del:
26133+ case Opt_idel:
26134+ u.del = &opt->del;
26135+ AuDbg("del {%s, %p}\n",
26136+ u.del->pathname, u.del->h_path.dentry);
26137+ break;
26138+ case Opt_mod:
26139+ case Opt_imod:
26140+ u.mod = &opt->mod;
26141+ AuDbg("mod {%s, 0x%x, %p}\n",
26142+ u.mod->path, u.mod->perm, u.mod->h_root);
26143+ break;
26144+ case Opt_append:
26145+ u.add = &opt->add;
26146+ AuDbg("append {b%d, %s, 0x%x, %p}\n",
26147+ u.add->bindex, u.add->pathname, u.add->perm,
26148+ u.add->path.dentry);
26149+ break;
26150+ case Opt_prepend:
26151+ u.add = &opt->add;
26152+ AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
26153+ u.add->bindex, u.add->pathname, u.add->perm,
26154+ u.add->path.dentry);
26155+ break;
26156+ case Opt_dirwh:
26157+ AuDbg("dirwh %d\n", opt->dirwh);
26158+ break;
26159+ case Opt_rdcache:
26160+ AuDbg("rdcache %d\n", opt->rdcache);
26161+ break;
26162+ case Opt_rdblk:
26163+ AuDbg("rdblk %u\n", opt->rdblk);
26164+ break;
26165+ case Opt_rdblk_def:
26166+ AuDbg("rdblk_def\n");
26167+ break;
26168+ case Opt_rdhash:
26169+ AuDbg("rdhash %u\n", opt->rdhash);
26170+ break;
26171+ case Opt_rdhash_def:
26172+ AuDbg("rdhash_def\n");
26173+ break;
26174+ case Opt_xino:
26175+ u.xino = &opt->xino;
26176+ AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
26177+ break;
26178+ case Opt_trunc_xino:
26179+ AuLabel(trunc_xino);
26180+ break;
26181+ case Opt_notrunc_xino:
26182+ AuLabel(notrunc_xino);
26183+ break;
26184+ case Opt_trunc_xino_path:
26185+ case Opt_itrunc_xino:
26186+ u.xino_itrunc = &opt->xino_itrunc;
26187+ AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
26188+ break;
26189+ case Opt_noxino:
26190+ AuLabel(noxino);
26191+ break;
26192+ case Opt_trunc_xib:
26193+ AuLabel(trunc_xib);
26194+ break;
26195+ case Opt_notrunc_xib:
26196+ AuLabel(notrunc_xib);
26197+ break;
26198+ case Opt_shwh:
26199+ AuLabel(shwh);
26200+ break;
26201+ case Opt_noshwh:
26202+ AuLabel(noshwh);
26203+ break;
26204+ case Opt_dirperm1:
26205+ AuLabel(dirperm1);
26206+ break;
26207+ case Opt_nodirperm1:
26208+ AuLabel(nodirperm1);
26209+ break;
26210+ case Opt_plink:
26211+ AuLabel(plink);
26212+ break;
26213+ case Opt_noplink:
26214+ AuLabel(noplink);
26215+ break;
26216+ case Opt_list_plink:
26217+ AuLabel(list_plink);
26218+ break;
26219+ case Opt_udba:
26220+ AuDbg("udba %d, %s\n",
26221+ opt->udba, au_optstr_udba(opt->udba));
26222+ break;
26223+ case Opt_dio:
26224+ AuLabel(dio);
26225+ break;
26226+ case Opt_nodio:
26227+ AuLabel(nodio);
26228+ break;
26229+ case Opt_diropq_a:
26230+ AuLabel(diropq_a);
26231+ break;
26232+ case Opt_diropq_w:
26233+ AuLabel(diropq_w);
26234+ break;
26235+ case Opt_warn_perm:
26236+ AuLabel(warn_perm);
26237+ break;
26238+ case Opt_nowarn_perm:
26239+ AuLabel(nowarn_perm);
26240+ break;
26241+ case Opt_verbose:
26242+ AuLabel(verbose);
26243+ break;
26244+ case Opt_noverbose:
26245+ AuLabel(noverbose);
26246+ break;
26247+ case Opt_sum:
26248+ AuLabel(sum);
26249+ break;
26250+ case Opt_nosum:
26251+ AuLabel(nosum);
26252+ break;
26253+ case Opt_wsum:
26254+ AuLabel(wsum);
26255+ break;
26256+ case Opt_wbr_create:
26257+ u.create = &opt->wbr_create;
26258+ AuDbg("create %d, %s\n", u.create->wbr_create,
26259+ au_optstr_wbr_create(u.create->wbr_create));
26260+ switch (u.create->wbr_create) {
26261+ case AuWbrCreate_MFSV:
26262+ case AuWbrCreate_PMFSV:
26263+ AuDbg("%d sec\n", u.create->mfs_second);
26264+ break;
26265+ case AuWbrCreate_MFSRR:
26266+ case AuWbrCreate_TDMFS:
26267+ AuDbg("%llu watermark\n",
26268+ u.create->mfsrr_watermark);
26269+ break;
26270+ case AuWbrCreate_MFSRRV:
26271+ case AuWbrCreate_TDMFSV:
26272+ case AuWbrCreate_PMFSRRV:
26273+ AuDbg("%llu watermark, %d sec\n",
26274+ u.create->mfsrr_watermark,
26275+ u.create->mfs_second);
26276+ break;
26277+ }
26278+ break;
26279+ case Opt_wbr_copyup:
26280+ AuDbg("copyup %d, %s\n", opt->wbr_copyup,
26281+ au_optstr_wbr_copyup(opt->wbr_copyup));
26282+ break;
26283+ case Opt_fhsm_sec:
26284+ AuDbg("fhsm_sec %u\n", opt->fhsm_second);
26285+ break;
26286+ case Opt_dirren:
26287+ AuLabel(dirren);
26288+ break;
26289+ case Opt_nodirren:
26290+ AuLabel(nodirren);
26291+ break;
26292+ case Opt_acl:
26293+ AuLabel(acl);
26294+ break;
26295+ case Opt_noacl:
26296+ AuLabel(noacl);
26297+ break;
26298+ default:
26299+ BUG();
26300+ }
26301+ opt++;
26302+ }
26303+#endif
26304+}
26305+
26306+void au_opts_free(struct au_opts *opts)
26307+{
26308+ struct au_opt *opt;
26309+
26310+ opt = opts->opt;
26311+ while (opt->type != Opt_tail) {
26312+ switch (opt->type) {
26313+ case Opt_add:
26314+ case Opt_append:
26315+ case Opt_prepend:
26316+ path_put(&opt->add.path);
26317+ break;
26318+ case Opt_del:
26319+ case Opt_idel:
26320+ path_put(&opt->del.h_path);
26321+ break;
26322+ case Opt_mod:
26323+ case Opt_imod:
26324+ dput(opt->mod.h_root);
26325+ break;
26326+ case Opt_xino:
26327+ fput(opt->xino.file);
26328+ break;
26329+ }
26330+ opt++;
26331+ }
26332+}
26333+
26334+static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
26335+ aufs_bindex_t bindex)
26336+{
26337+ int err;
26338+ struct au_opt_add *add = &opt->add;
26339+ char *p;
26340+
26341+ add->bindex = bindex;
26342+ add->perm = AuBrPerm_RO;
26343+ add->pathname = opt_str;
26344+ p = strchr(opt_str, '=');
26345+ if (p) {
26346+ *p++ = 0;
26347+ if (*p)
26348+ add->perm = br_perm_val(p);
26349+ }
26350+
26351+ err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
26352+ if (!err) {
26353+ if (!p) {
26354+ add->perm = AuBrPerm_RO;
26355+ if (au_test_fs_rr(add->path.dentry->d_sb))
26356+ add->perm = AuBrPerm_RR;
26357+ else if (!bindex && !(sb_flags & SB_RDONLY))
26358+ add->perm = AuBrPerm_RW;
26359+ }
26360+ opt->type = Opt_add;
26361+ goto out;
26362+ }
26363+ pr_err("lookup failed %s (%d)\n", add->pathname, err);
26364+ err = -EINVAL;
26365+
26366+out:
26367+ return err;
26368+}
26369+
26370+static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
26371+{
26372+ int err;
26373+
26374+ del->pathname = args[0].from;
26375+ AuDbg("del path %s\n", del->pathname);
26376+
26377+ err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
26378+ if (unlikely(err))
26379+ pr_err("lookup failed %s (%d)\n", del->pathname, err);
26380+
26381+ return err;
26382+}
26383+
26384+#if 0 /* reserved for future use */
26385+static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
26386+ struct au_opt_del *del, substring_t args[])
26387+{
26388+ int err;
26389+ struct dentry *root;
26390+
26391+ err = -EINVAL;
26392+ root = sb->s_root;
26393+ aufs_read_lock(root, AuLock_FLUSH);
26394+ if (bindex < 0 || au_sbbot(sb) < bindex) {
26395+ pr_err("out of bounds, %d\n", bindex);
26396+ goto out;
26397+ }
26398+
26399+ err = 0;
26400+ del->h_path.dentry = dget(au_h_dptr(root, bindex));
26401+ del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
26402+
26403+out:
26404+ aufs_read_unlock(root, !AuLock_IR);
26405+ return err;
26406+}
26407+#endif
26408+
26409+static int noinline_for_stack
26410+au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
26411+{
26412+ int err;
26413+ struct path path;
26414+ char *p;
26415+
26416+ err = -EINVAL;
26417+ mod->path = args[0].from;
26418+ p = strchr(mod->path, '=');
26419+ if (unlikely(!p)) {
26420+ pr_err("no permission %s\n", args[0].from);
26421+ goto out;
26422+ }
26423+
26424+ *p++ = 0;
26425+ err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
26426+ if (unlikely(err)) {
26427+ pr_err("lookup failed %s (%d)\n", mod->path, err);
26428+ goto out;
26429+ }
26430+
26431+ mod->perm = br_perm_val(p);
26432+ AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
26433+ mod->h_root = dget(path.dentry);
26434+ path_put(&path);
26435+
26436+out:
26437+ return err;
26438+}
26439+
26440+#if 0 /* reserved for future use */
26441+static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
26442+ struct au_opt_mod *mod, substring_t args[])
26443+{
26444+ int err;
26445+ struct dentry *root;
26446+
26447+ err = -EINVAL;
26448+ root = sb->s_root;
26449+ aufs_read_lock(root, AuLock_FLUSH);
26450+ if (bindex < 0 || au_sbbot(sb) < bindex) {
26451+ pr_err("out of bounds, %d\n", bindex);
26452+ goto out;
26453+ }
26454+
26455+ err = 0;
26456+ mod->perm = br_perm_val(args[1].from);
26457+ AuDbg("mod path %s, perm 0x%x, %s\n",
26458+ mod->path, mod->perm, args[1].from);
26459+ mod->h_root = dget(au_h_dptr(root, bindex));
26460+
26461+out:
26462+ aufs_read_unlock(root, !AuLock_IR);
26463+ return err;
26464+}
26465+#endif
26466+
26467+static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
26468+ substring_t args[])
26469+{
26470+ int err;
26471+ struct file *file;
26472+
26473+ file = au_xino_create(sb, args[0].from, /*silent*/0, /*wbrtop*/0);
26474+ err = PTR_ERR(file);
26475+ if (IS_ERR(file))
26476+ goto out;
26477+
26478+ err = -EINVAL;
26479+ if (unlikely(file->f_path.dentry->d_sb == sb)) {
26480+ fput(file);
26481+ pr_err("%s must be outside\n", args[0].from);
26482+ goto out;
26483+ }
26484+
26485+ err = 0;
26486+ xino->file = file;
26487+ xino->path = args[0].from;
26488+
26489+out:
26490+ return err;
26491+}
26492+
26493+static int noinline_for_stack
26494+au_opts_parse_xino_itrunc_path(struct super_block *sb,
26495+ struct au_opt_xino_itrunc *xino_itrunc,
26496+ substring_t args[])
26497+{
26498+ int err;
26499+ aufs_bindex_t bbot, bindex;
26500+ struct path path;
26501+ struct dentry *root;
26502+
26503+ err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
26504+ if (unlikely(err)) {
26505+ pr_err("lookup failed %s (%d)\n", args[0].from, err);
26506+ goto out;
26507+ }
26508+
26509+ xino_itrunc->bindex = -1;
26510+ root = sb->s_root;
26511+ aufs_read_lock(root, AuLock_FLUSH);
26512+ bbot = au_sbbot(sb);
26513+ for (bindex = 0; bindex <= bbot; bindex++) {
26514+ if (au_h_dptr(root, bindex) == path.dentry) {
26515+ xino_itrunc->bindex = bindex;
26516+ break;
26517+ }
26518+ }
26519+ aufs_read_unlock(root, !AuLock_IR);
26520+ path_put(&path);
26521+
26522+ if (unlikely(xino_itrunc->bindex < 0)) {
26523+ pr_err("no such branch %s\n", args[0].from);
26524+ err = -EINVAL;
26525+ }
26526+
26527+out:
26528+ return err;
26529+}
26530+
26531+/* called without aufs lock */
26532+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
26533+{
26534+ int err, n, token;
26535+ aufs_bindex_t bindex;
26536+ unsigned char skipped;
26537+ struct dentry *root;
26538+ struct au_opt *opt, *opt_tail;
26539+ char *opt_str;
26540+ /* reduce the stack space */
26541+ union {
26542+ struct au_opt_xino_itrunc *xino_itrunc;
26543+ struct au_opt_wbr_create *create;
26544+ } u;
26545+ struct {
26546+ substring_t args[MAX_OPT_ARGS];
26547+ } *a;
26548+
26549+ err = -ENOMEM;
26550+ a = kmalloc(sizeof(*a), GFP_NOFS);
26551+ if (unlikely(!a))
26552+ goto out;
26553+
26554+ root = sb->s_root;
26555+ err = 0;
26556+ bindex = 0;
26557+ opt = opts->opt;
26558+ opt_tail = opt + opts->max_opt - 1;
26559+ opt->type = Opt_tail;
26560+ while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
26561+ err = -EINVAL;
26562+ skipped = 0;
26563+ token = match_token(opt_str, options, a->args);
26564+ switch (token) {
26565+ case Opt_br:
26566+ err = 0;
26567+ while (!err && (opt_str = strsep(&a->args[0].from, ":"))
26568+ && *opt_str) {
26569+ err = opt_add(opt, opt_str, opts->sb_flags,
26570+ bindex++);
26571+ if (unlikely(!err && ++opt > opt_tail)) {
26572+ err = -E2BIG;
26573+ break;
26574+ }
26575+ opt->type = Opt_tail;
26576+ skipped = 1;
26577+ }
26578+ break;
26579+ case Opt_add:
26580+ if (unlikely(match_int(&a->args[0], &n))) {
26581+ pr_err("bad integer in %s\n", opt_str);
26582+ break;
26583+ }
26584+ bindex = n;
26585+ err = opt_add(opt, a->args[1].from, opts->sb_flags,
26586+ bindex);
26587+ if (!err)
26588+ opt->type = token;
26589+ break;
26590+ case Opt_append:
26591+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26592+ /*dummy bindex*/1);
26593+ if (!err)
26594+ opt->type = token;
26595+ break;
26596+ case Opt_prepend:
26597+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26598+ /*bindex*/0);
26599+ if (!err)
26600+ opt->type = token;
26601+ break;
26602+ case Opt_del:
26603+ err = au_opts_parse_del(&opt->del, a->args);
26604+ if (!err)
26605+ opt->type = token;
26606+ break;
26607+#if 0 /* reserved for future use */
26608+ case Opt_idel:
26609+ del->pathname = "(indexed)";
26610+ if (unlikely(match_int(&args[0], &n))) {
26611+ pr_err("bad integer in %s\n", opt_str);
26612+ break;
26613+ }
26614+ err = au_opts_parse_idel(sb, n, &opt->del, a->args);
26615+ if (!err)
26616+ opt->type = token;
26617+ break;
26618+#endif
26619+ case Opt_mod:
26620+ err = au_opts_parse_mod(&opt->mod, a->args);
26621+ if (!err)
26622+ opt->type = token;
26623+ break;
26624+#ifdef IMOD /* reserved for future use */
26625+ case Opt_imod:
26626+ u.mod->path = "(indexed)";
26627+ if (unlikely(match_int(&a->args[0], &n))) {
26628+ pr_err("bad integer in %s\n", opt_str);
26629+ break;
26630+ }
26631+ err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
26632+ if (!err)
26633+ opt->type = token;
26634+ break;
26635+#endif
26636+ case Opt_xino:
26637+ err = au_opts_parse_xino(sb, &opt->xino, a->args);
26638+ if (!err)
26639+ opt->type = token;
26640+ break;
26641+
26642+ case Opt_trunc_xino_path:
26643+ err = au_opts_parse_xino_itrunc_path
26644+ (sb, &opt->xino_itrunc, a->args);
26645+ if (!err)
26646+ opt->type = token;
26647+ break;
26648+
26649+ case Opt_itrunc_xino:
26650+ u.xino_itrunc = &opt->xino_itrunc;
26651+ if (unlikely(match_int(&a->args[0], &n))) {
26652+ pr_err("bad integer in %s\n", opt_str);
26653+ break;
26654+ }
26655+ u.xino_itrunc->bindex = n;
26656+ aufs_read_lock(root, AuLock_FLUSH);
26657+ if (n < 0 || au_sbbot(sb) < n) {
26658+ pr_err("out of bounds, %d\n", n);
26659+ aufs_read_unlock(root, !AuLock_IR);
26660+ break;
26661+ }
26662+ aufs_read_unlock(root, !AuLock_IR);
26663+ err = 0;
26664+ opt->type = token;
26665+ break;
26666+
26667+ case Opt_dirwh:
26668+ if (unlikely(match_int(&a->args[0], &opt->dirwh)))
26669+ break;
26670+ err = 0;
26671+ opt->type = token;
26672+ break;
26673+
26674+ case Opt_rdcache:
26675+ if (unlikely(match_int(&a->args[0], &n))) {
26676+ pr_err("bad integer in %s\n", opt_str);
26677+ break;
26678+ }
26679+ if (unlikely(n > AUFS_RDCACHE_MAX)) {
26680+ pr_err("rdcache must be smaller than %d\n",
26681+ AUFS_RDCACHE_MAX);
26682+ break;
26683+ }
26684+ opt->rdcache = n;
26685+ err = 0;
26686+ opt->type = token;
26687+ break;
26688+ case Opt_rdblk:
26689+ if (unlikely(match_int(&a->args[0], &n)
26690+ || n < 0
26691+ || n > KMALLOC_MAX_SIZE)) {
26692+ pr_err("bad integer in %s\n", opt_str);
26693+ break;
26694+ }
26695+ if (unlikely(n && n < NAME_MAX)) {
26696+ pr_err("rdblk must be larger than %d\n",
26697+ NAME_MAX);
26698+ break;
26699+ }
26700+ opt->rdblk = n;
26701+ err = 0;
26702+ opt->type = token;
26703+ break;
26704+ case Opt_rdhash:
26705+ if (unlikely(match_int(&a->args[0], &n)
26706+ || n < 0
26707+ || n * sizeof(struct hlist_head)
26708+ > KMALLOC_MAX_SIZE)) {
26709+ pr_err("bad integer in %s\n", opt_str);
26710+ break;
26711+ }
26712+ opt->rdhash = n;
26713+ err = 0;
26714+ opt->type = token;
26715+ break;
26716+
26717+ case Opt_trunc_xino:
26718+ case Opt_notrunc_xino:
26719+ case Opt_noxino:
26720+ case Opt_trunc_xib:
26721+ case Opt_notrunc_xib:
26722+ case Opt_shwh:
26723+ case Opt_noshwh:
26724+ case Opt_dirperm1:
26725+ case Opt_nodirperm1:
26726+ case Opt_plink:
26727+ case Opt_noplink:
26728+ case Opt_list_plink:
26729+ case Opt_dio:
26730+ case Opt_nodio:
26731+ case Opt_diropq_a:
26732+ case Opt_diropq_w:
26733+ case Opt_warn_perm:
26734+ case Opt_nowarn_perm:
26735+ case Opt_verbose:
26736+ case Opt_noverbose:
26737+ case Opt_sum:
26738+ case Opt_nosum:
26739+ case Opt_wsum:
26740+ case Opt_rdblk_def:
26741+ case Opt_rdhash_def:
26742+ case Opt_dirren:
26743+ case Opt_nodirren:
26744+ case Opt_acl:
26745+ case Opt_noacl:
26746+ err = 0;
26747+ opt->type = token;
26748+ break;
26749+
26750+ case Opt_udba:
26751+ opt->udba = udba_val(a->args[0].from);
26752+ if (opt->udba >= 0) {
26753+ err = 0;
26754+ opt->type = token;
26755+ } else
26756+ pr_err("wrong value, %s\n", opt_str);
26757+ break;
26758+
26759+ case Opt_wbr_create:
26760+ u.create = &opt->wbr_create;
26761+ u.create->wbr_create
26762+ = au_wbr_create_val(a->args[0].from, u.create);
26763+ if (u.create->wbr_create >= 0) {
26764+ err = 0;
26765+ opt->type = token;
26766+ } else
26767+ pr_err("wrong value, %s\n", opt_str);
26768+ break;
26769+ case Opt_wbr_copyup:
26770+ opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
26771+ if (opt->wbr_copyup >= 0) {
26772+ err = 0;
26773+ opt->type = token;
26774+ } else
26775+ pr_err("wrong value, %s\n", opt_str);
26776+ break;
26777+
26778+ case Opt_fhsm_sec:
26779+ if (unlikely(match_int(&a->args[0], &n)
26780+ || n < 0)) {
26781+ pr_err("bad integer in %s\n", opt_str);
26782+ break;
26783+ }
26784+ if (sysaufs_brs) {
26785+ opt->fhsm_second = n;
26786+ opt->type = token;
26787+ } else
26788+ pr_warn("ignored %s\n", opt_str);
26789+ err = 0;
26790+ break;
26791+
26792+ case Opt_ignore:
26793+ pr_warn("ignored %s\n", opt_str);
26794+ fallthrough;
26795+ case Opt_ignore_silent:
26796+ skipped = 1;
26797+ err = 0;
26798+ break;
26799+ case Opt_err:
26800+ pr_err("unknown option %s\n", opt_str);
26801+ break;
26802+ }
26803+
26804+ if (!err && !skipped) {
26805+ if (unlikely(++opt > opt_tail)) {
26806+ err = -E2BIG;
26807+ opt--;
26808+ opt->type = Opt_tail;
26809+ break;
26810+ }
26811+ opt->type = Opt_tail;
26812+ }
26813+ }
26814+
26815+ au_kfree_rcu(a);
26816+ dump_opts(opts);
26817+ if (unlikely(err))
26818+ au_opts_free(opts);
26819+
26820+out:
26821+ return err;
26822+}
26823+
26824+static int au_opt_wbr_create(struct super_block *sb,
26825+ struct au_opt_wbr_create *create)
26826+{
26827+ int err;
26828+ struct au_sbinfo *sbinfo;
26829+
26830+ SiMustWriteLock(sb);
26831+
26832+ err = 1; /* handled */
26833+ sbinfo = au_sbi(sb);
26834+ if (sbinfo->si_wbr_create_ops->fin) {
26835+ err = sbinfo->si_wbr_create_ops->fin(sb);
26836+ if (!err)
26837+ err = 1;
26838+ }
26839+
26840+ sbinfo->si_wbr_create = create->wbr_create;
26841+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
26842+ switch (create->wbr_create) {
26843+ case AuWbrCreate_MFSRRV:
26844+ case AuWbrCreate_MFSRR:
26845+ case AuWbrCreate_TDMFS:
26846+ case AuWbrCreate_TDMFSV:
26847+ case AuWbrCreate_PMFSRR:
26848+ case AuWbrCreate_PMFSRRV:
26849+ sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
26850+ fallthrough;
26851+ case AuWbrCreate_MFS:
26852+ case AuWbrCreate_MFSV:
26853+ case AuWbrCreate_PMFS:
26854+ case AuWbrCreate_PMFSV:
26855+ sbinfo->si_wbr_mfs.mfs_expire
26856+ = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
26857+ break;
26858+ }
26859+
26860+ if (sbinfo->si_wbr_create_ops->init)
26861+ sbinfo->si_wbr_create_ops->init(sb); /* ignore */
26862+
26863+ return err;
26864+}
26865+
26866+/*
26867+ * returns,
26868+ * plus: processed without an error
26869+ * zero: unprocessed
26870+ */
26871+static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
26872+ struct au_opts *opts)
26873+{
26874+ int err;
26875+ struct au_sbinfo *sbinfo;
26876+
26877+ SiMustWriteLock(sb);
26878+
26879+ err = 1; /* handled */
26880+ sbinfo = au_sbi(sb);
26881+ switch (opt->type) {
26882+ case Opt_udba:
26883+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
26884+ sbinfo->si_mntflags |= opt->udba;
26885+ opts->given_udba |= opt->udba;
26886+ break;
26887+
26888+ case Opt_plink:
26889+ au_opt_set(sbinfo->si_mntflags, PLINK);
26890+ break;
26891+ case Opt_noplink:
26892+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
26893+ au_plink_put(sb, /*verbose*/1);
26894+ au_opt_clr(sbinfo->si_mntflags, PLINK);
26895+ break;
26896+ case Opt_list_plink:
26897+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
26898+ au_plink_list(sb);
26899+ break;
26900+
26901+ case Opt_dio:
26902+ au_opt_set(sbinfo->si_mntflags, DIO);
26903+ au_fset_opts(opts->flags, REFRESH_DYAOP);
26904+ break;
26905+ case Opt_nodio:
26906+ au_opt_clr(sbinfo->si_mntflags, DIO);
26907+ au_fset_opts(opts->flags, REFRESH_DYAOP);
26908+ break;
26909+
26910+ case Opt_fhsm_sec:
26911+ au_fhsm_set(sbinfo, opt->fhsm_second);
26912+ break;
26913+
26914+ case Opt_diropq_a:
26915+ au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26916+ break;
26917+ case Opt_diropq_w:
26918+ au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26919+ break;
26920+
26921+ case Opt_warn_perm:
26922+ au_opt_set(sbinfo->si_mntflags, WARN_PERM);
26923+ break;
26924+ case Opt_nowarn_perm:
26925+ au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
26926+ break;
26927+
26928+ case Opt_verbose:
26929+ au_opt_set(sbinfo->si_mntflags, VERBOSE);
26930+ break;
26931+ case Opt_noverbose:
26932+ au_opt_clr(sbinfo->si_mntflags, VERBOSE);
26933+ break;
26934+
26935+ case Opt_sum:
26936+ au_opt_set(sbinfo->si_mntflags, SUM);
26937+ break;
26938+ case Opt_wsum:
26939+ au_opt_clr(sbinfo->si_mntflags, SUM);
26940+ au_opt_set(sbinfo->si_mntflags, SUM_W);
26941+ break;
26942+ case Opt_nosum:
26943+ au_opt_clr(sbinfo->si_mntflags, SUM);
26944+ au_opt_clr(sbinfo->si_mntflags, SUM_W);
26945+ break;
26946+
26947+ case Opt_wbr_create:
26948+ err = au_opt_wbr_create(sb, &opt->wbr_create);
26949+ break;
26950+ case Opt_wbr_copyup:
26951+ sbinfo->si_wbr_copyup = opt->wbr_copyup;
26952+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
26953+ break;
26954+
26955+ case Opt_dirwh:
26956+ sbinfo->si_dirwh = opt->dirwh;
26957+ break;
26958+
26959+ case Opt_rdcache:
26960+ sbinfo->si_rdcache
26961+ = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
26962+ break;
26963+ case Opt_rdblk:
26964+ sbinfo->si_rdblk = opt->rdblk;
26965+ break;
26966+ case Opt_rdblk_def:
26967+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
26968+ break;
26969+ case Opt_rdhash:
26970+ sbinfo->si_rdhash = opt->rdhash;
26971+ break;
26972+ case Opt_rdhash_def:
26973+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
26974+ break;
26975+
26976+ case Opt_shwh:
26977+ au_opt_set(sbinfo->si_mntflags, SHWH);
26978+ break;
26979+ case Opt_noshwh:
26980+ au_opt_clr(sbinfo->si_mntflags, SHWH);
26981+ break;
26982+
26983+ case Opt_dirperm1:
26984+ au_opt_set(sbinfo->si_mntflags, DIRPERM1);
26985+ break;
26986+ case Opt_nodirperm1:
26987+ au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
26988+ break;
26989+
26990+ case Opt_trunc_xino:
26991+ au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
26992+ break;
26993+ case Opt_notrunc_xino:
26994+ au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
26995+ break;
26996+
26997+ case Opt_trunc_xino_path:
26998+ case Opt_itrunc_xino:
26999+ err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27000+ /*idx_begin*/0);
27001+ if (!err)
27002+ err = 1;
27003+ break;
27004+
27005+ case Opt_trunc_xib:
27006+ au_fset_opts(opts->flags, TRUNC_XIB);
27007+ break;
27008+ case Opt_notrunc_xib:
27009+ au_fclr_opts(opts->flags, TRUNC_XIB);
27010+ break;
27011+
27012+ case Opt_dirren:
27013+ err = 1;
27014+ if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27015+ err = au_dr_opt_set(sb);
27016+ if (!err)
27017+ err = 1;
27018+ }
27019+ if (err == 1)
27020+ au_opt_set(sbinfo->si_mntflags, DIRREN);
27021+ break;
27022+ case Opt_nodirren:
27023+ err = 1;
27024+ if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27025+ err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27026+ DR_FLUSHED));
27027+ if (!err)
27028+ err = 1;
27029+ }
27030+ if (err == 1)
27031+ au_opt_clr(sbinfo->si_mntflags, DIRREN);
27032+ break;
27033+
27034+ case Opt_acl:
27035+ sb->s_flags |= SB_POSIXACL;
27036+ break;
27037+ case Opt_noacl:
27038+ sb->s_flags &= ~SB_POSIXACL;
27039+ break;
27040+
27041+ default:
27042+ err = 0;
27043+ break;
27044+ }
27045+
27046+ return err;
27047+}
27048+
27049+/*
27050+ * returns tri-state.
27051+ * plus: processed without an error
27052+ * zero: unprocessed
27053+ * minus: error
27054+ */
27055+static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27056+ struct au_opts *opts)
27057+{
27058+ int err, do_refresh;
27059+
27060+ err = 0;
27061+ switch (opt->type) {
27062+ case Opt_append:
27063+ opt->add.bindex = au_sbbot(sb) + 1;
27064+ if (opt->add.bindex < 0)
27065+ opt->add.bindex = 0;
27066+ goto add;
27067+ /* Always goto add, not fallthrough */
27068+ case Opt_prepend:
27069+ opt->add.bindex = 0;
27070+ fallthrough;
27071+ add: /* indented label */
27072+ case Opt_add:
27073+ err = au_br_add(sb, &opt->add,
27074+ au_ftest_opts(opts->flags, REMOUNT));
27075+ if (!err) {
27076+ err = 1;
27077+ au_fset_opts(opts->flags, REFRESH);
27078+ }
27079+ break;
27080+
27081+ case Opt_del:
27082+ case Opt_idel:
27083+ err = au_br_del(sb, &opt->del,
27084+ au_ftest_opts(opts->flags, REMOUNT));
27085+ if (!err) {
27086+ err = 1;
27087+ au_fset_opts(opts->flags, TRUNC_XIB);
27088+ au_fset_opts(opts->flags, REFRESH);
27089+ }
27090+ break;
27091+
27092+ case Opt_mod:
27093+ case Opt_imod:
27094+ err = au_br_mod(sb, &opt->mod,
27095+ au_ftest_opts(opts->flags, REMOUNT),
27096+ &do_refresh);
27097+ if (!err) {
27098+ err = 1;
27099+ if (do_refresh)
27100+ au_fset_opts(opts->flags, REFRESH);
27101+ }
27102+ break;
27103+ }
27104+ return err;
27105+}
27106+
27107+static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27108+ struct au_opt_xino **opt_xino,
27109+ struct au_opts *opts)
27110+{
27111+ int err;
27112+
27113+ err = 0;
27114+ switch (opt->type) {
27115+ case Opt_xino:
27116+ err = au_xino_set(sb, &opt->xino,
27117+ !!au_ftest_opts(opts->flags, REMOUNT));
27118+ if (unlikely(err))
27119+ break;
27120+
27121+ *opt_xino = &opt->xino;
27122+ break;
27123+
27124+ case Opt_noxino:
27125+ au_xino_clr(sb);
27126+ *opt_xino = (void *)-1;
27127+ break;
27128+ }
27129+
27130+ return err;
27131+}
27132+
27133+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27134+ unsigned int pending)
27135+{
27136+ int err, fhsm;
27137+ aufs_bindex_t bindex, bbot;
27138+ unsigned char do_plink, skip, do_free, can_no_dreval;
27139+ struct au_branch *br;
27140+ struct au_wbr *wbr;
27141+ struct dentry *root, *dentry;
27142+ struct inode *dir, *h_dir;
27143+ struct au_sbinfo *sbinfo;
27144+ struct au_hinode *hdir;
27145+
27146+ SiMustAnyLock(sb);
27147+
27148+ sbinfo = au_sbi(sb);
27149+ AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27150+
27151+ if (!(sb_flags & SB_RDONLY)) {
27152+ if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
27153+ pr_warn("first branch should be rw\n");
27154+ if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
27155+ pr_warn_once("shwh should be used with ro\n");
27156+ }
27157+
27158+ if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
27159+ && !au_opt_test(sbinfo->si_mntflags, XINO))
27160+ pr_warn_once("udba=*notify requires xino\n");
27161+
27162+ if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
27163+ pr_warn_once("dirperm1 breaks the protection"
27164+ " by the permission bits on the lower branch\n");
27165+
27166+ err = 0;
27167+ fhsm = 0;
27168+ root = sb->s_root;
27169+ dir = d_inode(root);
27170+ do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
27171+ can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27172+ UDBA_NONE);
27173+ bbot = au_sbbot(sb);
27174+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
27175+ skip = 0;
27176+ h_dir = au_h_iptr(dir, bindex);
27177+ br = au_sbr(sb, bindex);
27178+
27179+ if ((br->br_perm & AuBrAttr_ICEX)
27180+ && !h_dir->i_op->listxattr)
27181+ br->br_perm &= ~AuBrAttr_ICEX;
27182+#if 0 /* untested */
27183+ if ((br->br_perm & AuBrAttr_ICEX_SEC)
27184+ && (au_br_sb(br)->s_flags & SB_NOSEC))
27185+ br->br_perm &= ~AuBrAttr_ICEX_SEC;
27186+#endif
27187+
27188+ do_free = 0;
27189+ wbr = br->br_wbr;
27190+ if (wbr)
27191+ wbr_wh_read_lock(wbr);
27192+
27193+ if (!au_br_writable(br->br_perm)) {
27194+ do_free = !!wbr;
27195+ skip = (!wbr
27196+ || (!wbr->wbr_whbase
27197+ && !wbr->wbr_plink
27198+ && !wbr->wbr_orph));
27199+ } else if (!au_br_wh_linkable(br->br_perm)) {
27200+ /* skip = (!br->br_whbase && !br->br_orph); */
27201+ skip = (!wbr || !wbr->wbr_whbase);
27202+ if (skip && wbr) {
27203+ if (do_plink)
27204+ skip = !!wbr->wbr_plink;
27205+ else
27206+ skip = !wbr->wbr_plink;
27207+ }
27208+ } else {
27209+ /* skip = (br->br_whbase && br->br_ohph); */
27210+ skip = (wbr && wbr->wbr_whbase);
27211+ if (skip) {
27212+ if (do_plink)
27213+ skip = !!wbr->wbr_plink;
27214+ else
27215+ skip = !wbr->wbr_plink;
27216+ }
27217+ }
27218+ if (wbr)
27219+ wbr_wh_read_unlock(wbr);
27220+
27221+ if (can_no_dreval) {
27222+ dentry = br->br_path.dentry;
27223+ spin_lock(&dentry->d_lock);
27224+ if (dentry->d_flags &
27225+ (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27226+ can_no_dreval = 0;
27227+ spin_unlock(&dentry->d_lock);
27228+ }
27229+
27230+ if (au_br_fhsm(br->br_perm)) {
27231+ fhsm++;
27232+ AuDebugOn(!br->br_fhsm);
27233+ }
27234+
27235+ if (skip)
27236+ continue;
27237+
27238+ hdir = au_hi(dir, bindex);
27239+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
27240+ if (wbr)
27241+ wbr_wh_write_lock(wbr);
27242+ err = au_wh_init(br, sb);
27243+ if (wbr)
27244+ wbr_wh_write_unlock(wbr);
27245+ au_hn_inode_unlock(hdir);
27246+
27247+ if (!err && do_free) {
27248+ au_kfree_rcu(wbr);
27249+ br->br_wbr = NULL;
27250+ }
27251+ }
27252+
27253+ if (can_no_dreval)
27254+ au_fset_si(sbinfo, NO_DREVAL);
27255+ else
27256+ au_fclr_si(sbinfo, NO_DREVAL);
27257+
27258+ if (fhsm >= 2) {
27259+ au_fset_si(sbinfo, FHSM);
27260+ for (bindex = bbot; bindex >= 0; bindex--) {
27261+ br = au_sbr(sb, bindex);
27262+ if (au_br_fhsm(br->br_perm)) {
27263+ au_fhsm_set_bottom(sb, bindex);
27264+ break;
27265+ }
27266+ }
27267+ } else {
27268+ au_fclr_si(sbinfo, FHSM);
27269+ au_fhsm_set_bottom(sb, -1);
27270+ }
27271+
27272+ return err;
27273+}
27274+
27275+int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27276+{
27277+ int err;
27278+ unsigned int tmp;
27279+ aufs_bindex_t bindex, bbot;
27280+ struct au_opt *opt;
27281+ struct au_opt_xino *opt_xino, xino;
27282+ struct au_sbinfo *sbinfo;
27283+ struct au_branch *br;
27284+ struct inode *dir;
27285+
27286+ SiMustWriteLock(sb);
27287+
27288+ err = 0;
27289+ opt_xino = NULL;
27290+ opt = opts->opt;
27291+ while (err >= 0 && opt->type != Opt_tail)
27292+ err = au_opt_simple(sb, opt++, opts);
27293+ if (err > 0)
27294+ err = 0;
27295+ else if (unlikely(err < 0))
27296+ goto out;
27297+
27298+ /* disable xino and udba temporary */
27299+ sbinfo = au_sbi(sb);
27300+ tmp = sbinfo->si_mntflags;
27301+ au_opt_clr(sbinfo->si_mntflags, XINO);
27302+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27303+
27304+ opt = opts->opt;
27305+ while (err >= 0 && opt->type != Opt_tail)
27306+ err = au_opt_br(sb, opt++, opts);
27307+ if (err > 0)
27308+ err = 0;
27309+ else if (unlikely(err < 0))
27310+ goto out;
27311+
27312+ bbot = au_sbbot(sb);
27313+ if (unlikely(bbot < 0)) {
27314+ err = -EINVAL;
27315+ pr_err("no branches\n");
27316+ goto out;
27317+ }
27318+
27319+ if (au_opt_test(tmp, XINO))
27320+ au_opt_set(sbinfo->si_mntflags, XINO);
27321+ opt = opts->opt;
27322+ while (!err && opt->type != Opt_tail)
27323+ err = au_opt_xino(sb, opt++, &opt_xino, opts);
27324+ if (unlikely(err))
27325+ goto out;
27326+
27327+ err = au_opts_verify(sb, sb->s_flags, tmp);
27328+ if (unlikely(err))
27329+ goto out;
27330+
27331+ /* restore xino */
27332+ if (au_opt_test(tmp, XINO) && !opt_xino) {
27333+ xino.file = au_xino_def(sb);
27334+ err = PTR_ERR(xino.file);
27335+ if (IS_ERR(xino.file))
27336+ goto out;
27337+
27338+ err = au_xino_set(sb, &xino, /*remount*/0);
27339+ fput(xino.file);
27340+ if (unlikely(err))
27341+ goto out;
27342+ }
27343+
27344+ /* restore udba */
27345+ tmp &= AuOptMask_UDBA;
27346+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27347+ sbinfo->si_mntflags |= tmp;
27348+ bbot = au_sbbot(sb);
27349+ for (bindex = 0; bindex <= bbot; bindex++) {
27350+ br = au_sbr(sb, bindex);
27351+ err = au_hnotify_reset_br(tmp, br, br->br_perm);
27352+ if (unlikely(err))
27353+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
27354+ bindex, err);
27355+ /* go on even if err */
27356+ }
27357+ if (au_opt_test(tmp, UDBA_HNOTIFY)) {
27358+ dir = d_inode(sb->s_root);
27359+ au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
27360+ }
27361+
27362+out:
27363+ return err;
27364+}
27365+
27366+int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27367+{
27368+ int err, rerr;
27369+ unsigned char no_dreval;
27370+ struct inode *dir;
27371+ struct au_opt_xino *opt_xino;
27372+ struct au_opt *opt;
27373+ struct au_sbinfo *sbinfo;
27374+
27375+ SiMustWriteLock(sb);
27376+
27377+ err = au_dr_opt_flush(sb);
27378+ if (unlikely(err))
27379+ goto out;
27380+ au_fset_opts(opts->flags, DR_FLUSHED);
27381+
27382+ dir = d_inode(sb->s_root);
27383+ sbinfo = au_sbi(sb);
27384+ opt_xino = NULL;
27385+ opt = opts->opt;
27386+ while (err >= 0 && opt->type != Opt_tail) {
27387+ err = au_opt_simple(sb, opt, opts);
27388+ if (!err)
27389+ err = au_opt_br(sb, opt, opts);
27390+ if (!err)
27391+ err = au_opt_xino(sb, opt, &opt_xino, opts);
27392+ opt++;
27393+ }
27394+ if (err > 0)
27395+ err = 0;
27396+ AuTraceErr(err);
27397+ /* go on even err */
27398+
27399+ no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
27400+ rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27401+ if (unlikely(rerr && !err))
27402+ err = rerr;
27403+
27404+ if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
27405+ au_fset_opts(opts->flags, REFRESH_IDOP);
27406+
27407+ if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27408+ rerr = au_xib_trunc(sb);
27409+ if (unlikely(rerr && !err))
27410+ err = rerr;
27411+ }
27412+
27413+ /* will be handled by the caller */
27414+ if (!au_ftest_opts(opts->flags, REFRESH)
27415+ && (opts->given_udba
27416+ || au_opt_test(sbinfo->si_mntflags, XINO)
27417+ || au_ftest_opts(opts->flags, REFRESH_IDOP)
27418+ ))
27419+ au_fset_opts(opts->flags, REFRESH);
27420+
27421+ AuDbg("status 0x%x\n", opts->flags);
27422+
27423+out:
27424+ return err;
27425+}
27426+
27427+/* ---------------------------------------------------------------------- */
27428+
27429+unsigned int au_opt_udba(struct super_block *sb)
27430+{
27431+ return au_mntflags(sb) & AuOptMask_UDBA;
27432+}
27433diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27434--- /usr/share/empty/fs/aufs/opts.h 1970-01-01 01:00:00.000000000 +0100
27435+++ linux/fs/aufs/opts.h 2021-02-22 22:51:59.061411920 +0100
27436@@ -0,0 +1,225 @@
27437+/* SPDX-License-Identifier: GPL-2.0 */
27438+/*
27439+ * Copyright (C) 2005-2020 Junjiro R. Okajima
27440+ *
27441+ * This program, aufs is free software; you can redistribute it and/or modify
27442+ * it under the terms of the GNU General Public License as published by
27443+ * the Free Software Foundation; either version 2 of the License, or
27444+ * (at your option) any later version.
27445+ *
27446+ * This program is distributed in the hope that it will be useful,
27447+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27448+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27449+ * GNU General Public License for more details.
27450+ *
27451+ * You should have received a copy of the GNU General Public License
27452+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
27453+ */
27454+
27455+/*
27456+ * mount options/flags
27457+ */
27458+
27459+#ifndef __AUFS_OPTS_H__
27460+#define __AUFS_OPTS_H__
27461+
27462+#ifdef __KERNEL__
27463+
27464+#include <linux/path.h>
27465+
27466+struct file;
27467+
27468+/* ---------------------------------------------------------------------- */
27469+
27470+/* mount flags */
27471+#define AuOpt_XINO 1 /* external inode number bitmap
27472+ and translation table */
27473+#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
27474+#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
27475+#define AuOpt_UDBA_REVAL (1 << 3)
27476+#define AuOpt_UDBA_HNOTIFY (1 << 4)
27477+#define AuOpt_SHWH (1 << 5) /* show whiteout */
27478+#define AuOpt_PLINK (1 << 6) /* pseudo-link */
27479+#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
27480+ bits */
27481+#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
27482+#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
27483+#define AuOpt_SUM_W (1 << 11) /* unimplemented */
27484+#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
27485+#define AuOpt_VERBOSE (1 << 13) /* print the cause of error */
27486+#define AuOpt_DIO (1 << 14) /* direct io */
27487+#define AuOpt_DIRREN (1 << 15) /* directory rename */
27488+
27489+#ifndef CONFIG_AUFS_HNOTIFY
27490+#undef AuOpt_UDBA_HNOTIFY
27491+#define AuOpt_UDBA_HNOTIFY 0
27492+#endif
27493+#ifndef CONFIG_AUFS_DIRREN
27494+#undef AuOpt_DIRREN
27495+#define AuOpt_DIRREN 0
27496+#endif
27497+#ifndef CONFIG_AUFS_SHWH
27498+#undef AuOpt_SHWH
27499+#define AuOpt_SHWH 0
27500+#endif
27501+
27502+#define AuOpt_Def (AuOpt_XINO \
27503+ | AuOpt_UDBA_REVAL \
27504+ | AuOpt_PLINK \
27505+ /* | AuOpt_DIRPERM1 */ \
27506+ | AuOpt_WARN_PERM)
27507+#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27508+ | AuOpt_UDBA_REVAL \
27509+ | AuOpt_UDBA_HNOTIFY)
27510+
27511+#define au_opt_test(flags, name) (flags & AuOpt_##name)
27512+#define au_opt_set(flags, name) do { \
27513+ BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27514+ ((flags) |= AuOpt_##name); \
27515+} while (0)
27516+#define au_opt_set_udba(flags, name) do { \
27517+ (flags) &= ~AuOptMask_UDBA; \
27518+ ((flags) |= AuOpt_##name); \
27519+} while (0)
27520+#define au_opt_clr(flags, name) do { \
27521+ ((flags) &= ~AuOpt_##name); \
27522+} while (0)
27523+
27524+static inline unsigned int au_opts_plink(unsigned int mntflags)
27525+{
27526+#ifdef CONFIG_PROC_FS
27527+ return mntflags;
27528+#else
27529+ return mntflags & ~AuOpt_PLINK;
27530+#endif
27531+}
27532+
27533+/* ---------------------------------------------------------------------- */
27534+
27535+/* policies to select one among multiple writable branches */
27536+enum {
27537+ AuWbrCreate_TDP, /* top down parent */
27538+ AuWbrCreate_RR, /* round robin */
27539+ AuWbrCreate_MFS, /* most free space */
27540+ AuWbrCreate_MFSV, /* mfs with seconds */
27541+ AuWbrCreate_MFSRR, /* mfs then rr */
27542+ AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
27543+ AuWbrCreate_TDMFS, /* top down regardless parent and mfs */
27544+ AuWbrCreate_TDMFSV, /* top down regardless parent and mfs */
27545+ AuWbrCreate_PMFS, /* parent and mfs */
27546+ AuWbrCreate_PMFSV, /* parent and mfs with seconds */
27547+ AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
27548+ AuWbrCreate_PMFSRRV, /* plus seconds */
27549+
27550+ AuWbrCreate_Def = AuWbrCreate_TDP
27551+};
27552+
27553+enum {
27554+ AuWbrCopyup_TDP, /* top down parent */
27555+ AuWbrCopyup_BUP, /* bottom up parent */
27556+ AuWbrCopyup_BU, /* bottom up */
27557+
27558+ AuWbrCopyup_Def = AuWbrCopyup_TDP
27559+};
27560+
27561+/* ---------------------------------------------------------------------- */
27562+
27563+struct au_opt_add {
27564+ aufs_bindex_t bindex;
27565+ char *pathname;
27566+ int perm;
27567+ struct path path;
27568+};
27569+
27570+struct au_opt_del {
27571+ char *pathname;
27572+ struct path h_path;
27573+};
27574+
27575+struct au_opt_mod {
27576+ char *path;
27577+ int perm;
27578+ struct dentry *h_root;
27579+};
27580+
27581+struct au_opt_xino {
27582+ char *path;
27583+ struct file *file;
27584+};
27585+
27586+struct au_opt_xino_itrunc {
27587+ aufs_bindex_t bindex;
27588+};
27589+
27590+struct au_opt_wbr_create {
27591+ int wbr_create;
27592+ int mfs_second;
27593+ unsigned long long mfsrr_watermark;
27594+};
27595+
27596+struct au_opt {
27597+ int type;
27598+ union {
27599+ struct au_opt_xino xino;
27600+ struct au_opt_xino_itrunc xino_itrunc;
27601+ struct au_opt_add add;
27602+ struct au_opt_del del;
27603+ struct au_opt_mod mod;
27604+ int dirwh;
27605+ int rdcache;
27606+ unsigned int rdblk;
27607+ unsigned int rdhash;
27608+ int udba;
27609+ struct au_opt_wbr_create wbr_create;
27610+ int wbr_copyup;
27611+ unsigned int fhsm_second;
27612+ };
27613+};
27614+
27615+/* opts flags */
27616+#define AuOpts_REMOUNT 1
27617+#define AuOpts_REFRESH (1 << 1)
27618+#define AuOpts_TRUNC_XIB (1 << 2)
27619+#define AuOpts_REFRESH_DYAOP (1 << 3)
27620+#define AuOpts_REFRESH_IDOP (1 << 4)
27621+#define AuOpts_DR_FLUSHED (1 << 5)
27622+#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
27623+#define au_fset_opts(flags, name) \
27624+ do { (flags) |= AuOpts_##name; } while (0)
27625+#define au_fclr_opts(flags, name) \
27626+ do { (flags) &= ~AuOpts_##name; } while (0)
27627+
27628+#ifndef CONFIG_AUFS_DIRREN
27629+#undef AuOpts_DR_FLUSHED
27630+#define AuOpts_DR_FLUSHED 0
27631+#endif
27632+
27633+struct au_opts {
27634+ struct au_opt *opt;
27635+ int max_opt;
27636+
27637+ unsigned int given_udba;
27638+ unsigned int flags;
27639+ unsigned long sb_flags;
27640+};
27641+
27642+/* ---------------------------------------------------------------------- */
27643+
27644+/* opts.c */
27645+void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
27646+const char *au_optstr_udba(int udba);
27647+const char *au_optstr_wbr_copyup(int wbr_copyup);
27648+const char *au_optstr_wbr_create(int wbr_create);
27649+
27650+void au_opts_free(struct au_opts *opts);
27651+struct super_block;
27652+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
27653+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27654+ unsigned int pending);
27655+int au_opts_mount(struct super_block *sb, struct au_opts *opts);
27656+int au_opts_remount(struct super_block *sb, struct au_opts *opts);
27657+
27658+unsigned int au_opt_udba(struct super_block *sb);
27659+
27660+#endif /* __KERNEL__ */
27661+#endif /* __AUFS_OPTS_H__ */
27662diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
27663--- /usr/share/empty/fs/aufs/plink.c 1970-01-01 01:00:00.000000000 +0100
27664+++ linux/fs/aufs/plink.c 2021-02-22 22:51:59.061411920 +0100
27665@@ -0,0 +1,516 @@
27666+// SPDX-License-Identifier: GPL-2.0
27667+/*
27668+ * Copyright (C) 2005-2020 Junjiro R. Okajima
27669+ *
27670+ * This program, aufs is free software; you can redistribute it and/or modify
27671+ * it under the terms of the GNU General Public License as published by
27672+ * the Free Software Foundation; either version 2 of the License, or
27673+ * (at your option) any later version.
27674+ *
27675+ * This program is distributed in the hope that it will be useful,
27676+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27677+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27678+ * GNU General Public License for more details.
27679+ *
27680+ * You should have received a copy of the GNU General Public License
27681+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
27682+ */
27683+
27684+/*
27685+ * pseudo-link
27686+ */
27687+
27688+#include "aufs.h"
27689+
27690+/*
27691+ * the pseudo-link maintenance mode.
27692+ * during a user process maintains the pseudo-links,
27693+ * prohibit adding a new plink and branch manipulation.
27694+ *
27695+ * Flags
27696+ * NOPLM:
27697+ * For entry functions which will handle plink, and i_mutex is already held
27698+ * in VFS.
27699+ * They cannot wait and should return an error at once.
27700+ * Callers has to check the error.
27701+ * NOPLMW:
27702+ * For entry functions which will handle plink, but i_mutex is not held
27703+ * in VFS.
27704+ * They can wait the plink maintenance mode to finish.
27705+ *
27706+ * They behave like F_SETLK and F_SETLKW.
27707+ * If the caller never handle plink, then both flags are unnecessary.
27708+ */
27709+
27710+int au_plink_maint(struct super_block *sb, int flags)
27711+{
27712+ int err;
27713+ pid_t pid, ppid;
27714+ struct task_struct *parent, *prev;
27715+ struct au_sbinfo *sbi;
27716+
27717+ SiMustAnyLock(sb);
27718+
27719+ err = 0;
27720+ if (!au_opt_test(au_mntflags(sb), PLINK))
27721+ goto out;
27722+
27723+ sbi = au_sbi(sb);
27724+ pid = sbi->si_plink_maint_pid;
27725+ if (!pid || pid == current->pid)
27726+ goto out;
27727+
27728+ /* todo: it highly depends upon /sbin/mount.aufs */
27729+ prev = NULL;
27730+ parent = current;
27731+ ppid = 0;
27732+ rcu_read_lock();
27733+ while (1) {
27734+ parent = rcu_dereference(parent->real_parent);
27735+ if (parent == prev)
27736+ break;
27737+ ppid = task_pid_vnr(parent);
27738+ if (pid == ppid) {
27739+ rcu_read_unlock();
27740+ goto out;
27741+ }
27742+ prev = parent;
27743+ }
27744+ rcu_read_unlock();
27745+
27746+ if (au_ftest_lock(flags, NOPLMW)) {
27747+ /* if there is no i_mutex lock in VFS, we don't need to wait */
27748+ /* AuDebugOn(!lockdep_depth(current)); */
27749+ while (sbi->si_plink_maint_pid) {
27750+ si_read_unlock(sb);
27751+ /* gave up wake_up_bit() */
27752+ wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
27753+
27754+ if (au_ftest_lock(flags, FLUSH))
27755+ au_nwt_flush(&sbi->si_nowait);
27756+ si_noflush_read_lock(sb);
27757+ }
27758+ } else if (au_ftest_lock(flags, NOPLM)) {
27759+ AuDbg("ppid %d, pid %d\n", ppid, pid);
27760+ err = -EAGAIN;
27761+ }
27762+
27763+out:
27764+ return err;
27765+}
27766+
27767+void au_plink_maint_leave(struct au_sbinfo *sbinfo)
27768+{
27769+ spin_lock(&sbinfo->si_plink_maint_lock);
27770+ sbinfo->si_plink_maint_pid = 0;
27771+ spin_unlock(&sbinfo->si_plink_maint_lock);
27772+ wake_up_all(&sbinfo->si_plink_wq);
27773+}
27774+
27775+int au_plink_maint_enter(struct super_block *sb)
27776+{
27777+ int err;
27778+ struct au_sbinfo *sbinfo;
27779+
27780+ err = 0;
27781+ sbinfo = au_sbi(sb);
27782+ /* make sure i am the only one in this fs */
27783+ si_write_lock(sb, AuLock_FLUSH);
27784+ if (au_opt_test(au_mntflags(sb), PLINK)) {
27785+ spin_lock(&sbinfo->si_plink_maint_lock);
27786+ if (!sbinfo->si_plink_maint_pid)
27787+ sbinfo->si_plink_maint_pid = current->pid;
27788+ else
27789+ err = -EBUSY;
27790+ spin_unlock(&sbinfo->si_plink_maint_lock);
27791+ }
27792+ si_write_unlock(sb);
27793+
27794+ return err;
27795+}
27796+
27797+/* ---------------------------------------------------------------------- */
27798+
27799+#ifdef CONFIG_AUFS_DEBUG
27800+void au_plink_list(struct super_block *sb)
27801+{
27802+ int i;
27803+ struct au_sbinfo *sbinfo;
27804+ struct hlist_bl_head *hbl;
27805+ struct hlist_bl_node *pos;
27806+ struct au_icntnr *icntnr;
27807+
27808+ SiMustAnyLock(sb);
27809+
27810+ sbinfo = au_sbi(sb);
27811+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
27812+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
27813+
27814+ for (i = 0; i < AuPlink_NHASH; i++) {
27815+ hbl = sbinfo->si_plink + i;
27816+ hlist_bl_lock(hbl);
27817+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27818+ AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
27819+ hlist_bl_unlock(hbl);
27820+ }
27821+}
27822+#endif
27823+
27824+/* is the inode pseudo-linked? */
27825+int au_plink_test(struct inode *inode)
27826+{
27827+ int found, i;
27828+ struct au_sbinfo *sbinfo;
27829+ struct hlist_bl_head *hbl;
27830+ struct hlist_bl_node *pos;
27831+ struct au_icntnr *icntnr;
27832+
27833+ sbinfo = au_sbi(inode->i_sb);
27834+ AuRwMustAnyLock(&sbinfo->si_rwsem);
27835+ AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
27836+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27837+
27838+ found = 0;
27839+ i = au_plink_hash(inode->i_ino);
27840+ hbl = sbinfo->si_plink + i;
27841+ hlist_bl_lock(hbl);
27842+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27843+ if (&icntnr->vfs_inode == inode) {
27844+ found = 1;
27845+ break;
27846+ }
27847+ hlist_bl_unlock(hbl);
27848+ return found;
27849+}
27850+
27851+/* ---------------------------------------------------------------------- */
27852+
27853+/*
27854+ * generate a name for plink.
27855+ * the file will be stored under AUFS_WH_PLINKDIR.
27856+ */
27857+/* 20 is max digits length of ulong 64 */
27858+#define PLINK_NAME_LEN ((20 + 1) * 2)
27859+
27860+static int plink_name(char *name, int len, struct inode *inode,
27861+ aufs_bindex_t bindex)
27862+{
27863+ int rlen;
27864+ struct inode *h_inode;
27865+
27866+ h_inode = au_h_iptr(inode, bindex);
27867+ rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
27868+ return rlen;
27869+}
27870+
27871+struct au_do_plink_lkup_args {
27872+ struct dentry **errp;
27873+ struct qstr *tgtname;
27874+ struct dentry *h_parent;
27875+ struct au_branch *br;
27876+};
27877+
27878+static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
27879+ struct dentry *h_parent,
27880+ struct au_branch *br)
27881+{
27882+ struct dentry *h_dentry;
27883+ struct inode *h_inode;
27884+
27885+ h_inode = d_inode(h_parent);
27886+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
27887+ h_dentry = vfsub_lkup_one(tgtname, h_parent);
27888+ inode_unlock_shared(h_inode);
27889+ return h_dentry;
27890+}
27891+
27892+static void au_call_do_plink_lkup(void *args)
27893+{
27894+ struct au_do_plink_lkup_args *a = args;
27895+ *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
27896+}
27897+
27898+/* lookup the plink-ed @inode under the branch at @bindex */
27899+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
27900+{
27901+ struct dentry *h_dentry, *h_parent;
27902+ struct au_branch *br;
27903+ int wkq_err;
27904+ char a[PLINK_NAME_LEN];
27905+ struct qstr tgtname = QSTR_INIT(a, 0);
27906+
27907+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27908+
27909+ br = au_sbr(inode->i_sb, bindex);
27910+ h_parent = br->br_wbr->wbr_plink;
27911+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
27912+
27913+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
27914+ struct au_do_plink_lkup_args args = {
27915+ .errp = &h_dentry,
27916+ .tgtname = &tgtname,
27917+ .h_parent = h_parent,
27918+ .br = br
27919+ };
27920+
27921+ wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
27922+ if (unlikely(wkq_err))
27923+ h_dentry = ERR_PTR(wkq_err);
27924+ } else
27925+ h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
27926+
27927+ return h_dentry;
27928+}
27929+
27930+/* create a pseudo-link */
27931+static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
27932+ struct dentry *h_dentry, struct au_branch *br)
27933+{
27934+ int err;
27935+ struct path h_path = {
27936+ .mnt = au_br_mnt(br)
27937+ };
27938+ struct inode *h_dir, *delegated;
27939+
27940+ h_dir = d_inode(h_parent);
27941+ inode_lock_nested(h_dir, AuLsc_I_CHILD2);
27942+again:
27943+ h_path.dentry = vfsub_lkup_one(tgt, h_parent);
27944+ err = PTR_ERR(h_path.dentry);
27945+ if (IS_ERR(h_path.dentry))
27946+ goto out;
27947+
27948+ err = 0;
27949+ /* wh.plink dir is not monitored */
27950+ /* todo: is it really safe? */
27951+ if (d_is_positive(h_path.dentry)
27952+ && d_inode(h_path.dentry) != d_inode(h_dentry)) {
27953+ delegated = NULL;
27954+ err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
27955+ if (unlikely(err == -EWOULDBLOCK)) {
27956+ pr_warn("cannot retry for NFSv4 delegation"
27957+ " for an internal unlink\n");
27958+ iput(delegated);
27959+ }
27960+ dput(h_path.dentry);
27961+ h_path.dentry = NULL;
27962+ if (!err)
27963+ goto again;
27964+ }
27965+ if (!err && d_is_negative(h_path.dentry)) {
27966+ delegated = NULL;
27967+ err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
27968+ if (unlikely(err == -EWOULDBLOCK)) {
27969+ pr_warn("cannot retry for NFSv4 delegation"
27970+ " for an internal link\n");
27971+ iput(delegated);
27972+ }
27973+ }
27974+ dput(h_path.dentry);
27975+
27976+out:
27977+ inode_unlock(h_dir);
27978+ return err;
27979+}
27980+
27981+struct do_whplink_args {
27982+ int *errp;
27983+ struct qstr *tgt;
27984+ struct dentry *h_parent;
27985+ struct dentry *h_dentry;
27986+ struct au_branch *br;
27987+};
27988+
27989+static void call_do_whplink(void *args)
27990+{
27991+ struct do_whplink_args *a = args;
27992+ *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
27993+}
27994+
27995+static int whplink(struct dentry *h_dentry, struct inode *inode,
27996+ aufs_bindex_t bindex, struct au_branch *br)
27997+{
27998+ int err, wkq_err;
27999+ struct au_wbr *wbr;
28000+ struct dentry *h_parent;
28001+ char a[PLINK_NAME_LEN];
28002+ struct qstr tgtname = QSTR_INIT(a, 0);
28003+
28004+ wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
28005+ h_parent = wbr->wbr_plink;
28006+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28007+
28008+ /* always superio. */
28009+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28010+ struct do_whplink_args args = {
28011+ .errp = &err,
28012+ .tgt = &tgtname,
28013+ .h_parent = h_parent,
28014+ .h_dentry = h_dentry,
28015+ .br = br
28016+ };
28017+ wkq_err = au_wkq_wait(call_do_whplink, &args);
28018+ if (unlikely(wkq_err))
28019+ err = wkq_err;
28020+ } else
28021+ err = do_whplink(&tgtname, h_parent, h_dentry, br);
28022+
28023+ return err;
28024+}
28025+
28026+/*
28027+ * create a new pseudo-link for @h_dentry on @bindex.
28028+ * the linked inode is held in aufs @inode.
28029+ */
28030+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28031+ struct dentry *h_dentry)
28032+{
28033+ struct super_block *sb;
28034+ struct au_sbinfo *sbinfo;
28035+ struct hlist_bl_head *hbl;
28036+ struct hlist_bl_node *pos;
28037+ struct au_icntnr *icntnr;
28038+ int found, err, cnt, i;
28039+
28040+ sb = inode->i_sb;
28041+ sbinfo = au_sbi(sb);
28042+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28043+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28044+
28045+ found = au_plink_test(inode);
28046+ if (found)
28047+ return;
28048+
28049+ i = au_plink_hash(inode->i_ino);
28050+ hbl = sbinfo->si_plink + i;
28051+ au_igrab(inode);
28052+
28053+ hlist_bl_lock(hbl);
28054+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
28055+ if (&icntnr->vfs_inode == inode) {
28056+ found = 1;
28057+ break;
28058+ }
28059+ }
28060+ if (!found) {
28061+ icntnr = container_of(inode, struct au_icntnr, vfs_inode);
28062+ hlist_bl_add_head(&icntnr->plink, hbl);
28063+ }
28064+ hlist_bl_unlock(hbl);
28065+ if (!found) {
28066+ cnt = au_hbl_count(hbl);
28067+#define msg "unexpectedly unbalanced or too many pseudo-links"
28068+ if (cnt > AUFS_PLINK_WARN)
28069+ AuWarn1(msg ", %d\n", cnt);
28070+#undef msg
28071+ err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
28072+ if (unlikely(err)) {
28073+ pr_warn("err %d, damaged pseudo link.\n", err);
28074+ au_hbl_del(&icntnr->plink, hbl);
28075+ iput(&icntnr->vfs_inode);
28076+ }
28077+ } else
28078+ iput(&icntnr->vfs_inode);
28079+}
28080+
28081+/* free all plinks */
28082+void au_plink_put(struct super_block *sb, int verbose)
28083+{
28084+ int i, warned;
28085+ struct au_sbinfo *sbinfo;
28086+ struct hlist_bl_head *hbl;
28087+ struct hlist_bl_node *pos, *tmp;
28088+ struct au_icntnr *icntnr;
28089+
28090+ SiMustWriteLock(sb);
28091+
28092+ sbinfo = au_sbi(sb);
28093+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28094+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28095+
28096+ /* no spin_lock since sbinfo is write-locked */
28097+ warned = 0;
28098+ for (i = 0; i < AuPlink_NHASH; i++) {
28099+ hbl = sbinfo->si_plink + i;
28100+ if (!warned && verbose && !hlist_bl_empty(hbl)) {
28101+ pr_warn("pseudo-link is not flushed");
28102+ warned = 1;
28103+ }
28104+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
28105+ iput(&icntnr->vfs_inode);
28106+ INIT_HLIST_BL_HEAD(hbl);
28107+ }
28108+}
28109+
28110+void au_plink_clean(struct super_block *sb, int verbose)
28111+{
28112+ struct dentry *root;
28113+
28114+ root = sb->s_root;
28115+ aufs_write_lock(root);
28116+ if (au_opt_test(au_mntflags(sb), PLINK))
28117+ au_plink_put(sb, verbose);
28118+ aufs_write_unlock(root);
28119+}
28120+
28121+static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28122+{
28123+ int do_put;
28124+ aufs_bindex_t btop, bbot, bindex;
28125+
28126+ do_put = 0;
28127+ btop = au_ibtop(inode);
28128+ bbot = au_ibbot(inode);
28129+ if (btop >= 0) {
28130+ for (bindex = btop; bindex <= bbot; bindex++) {
28131+ if (!au_h_iptr(inode, bindex)
28132+ || au_ii_br_id(inode, bindex) != br_id)
28133+ continue;
28134+ au_set_h_iptr(inode, bindex, NULL, 0);
28135+ do_put = 1;
28136+ break;
28137+ }
28138+ if (do_put)
28139+ for (bindex = btop; bindex <= bbot; bindex++)
28140+ if (au_h_iptr(inode, bindex)) {
28141+ do_put = 0;
28142+ break;
28143+ }
28144+ } else
28145+ do_put = 1;
28146+
28147+ return do_put;
28148+}
28149+
28150+/* free the plinks on a branch specified by @br_id */
28151+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28152+{
28153+ struct au_sbinfo *sbinfo;
28154+ struct hlist_bl_head *hbl;
28155+ struct hlist_bl_node *pos, *tmp;
28156+ struct au_icntnr *icntnr;
28157+ struct inode *inode;
28158+ int i, do_put;
28159+
28160+ SiMustWriteLock(sb);
28161+
28162+ sbinfo = au_sbi(sb);
28163+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28164+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28165+
28166+ /* no bit_lock since sbinfo is write-locked */
28167+ for (i = 0; i < AuPlink_NHASH; i++) {
28168+ hbl = sbinfo->si_plink + i;
28169+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
28170+ inode = au_igrab(&icntnr->vfs_inode);
28171+ ii_write_lock_child(inode);
28172+ do_put = au_plink_do_half_refresh(inode, br_id);
28173+ if (do_put) {
28174+ hlist_bl_del(&icntnr->plink);
28175+ iput(inode);
28176+ }
28177+ ii_write_unlock(inode);
28178+ iput(inode);
28179+ }
28180+ }
28181+}
28182diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28183--- /usr/share/empty/fs/aufs/poll.c 1970-01-01 01:00:00.000000000 +0100
28184+++ linux/fs/aufs/poll.c 2021-02-22 22:51:59.061411920 +0100
28185@@ -0,0 +1,51 @@
28186+// SPDX-License-Identifier: GPL-2.0
28187+/*
28188+ * Copyright (C) 2005-2020 Junjiro R. Okajima
28189+ *
28190+ * This program, aufs is free software; you can redistribute it and/or modify
28191+ * it under the terms of the GNU General Public License as published by
28192+ * the Free Software Foundation; either version 2 of the License, or
28193+ * (at your option) any later version.
28194+ *
28195+ * This program is distributed in the hope that it will be useful,
28196+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28197+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28198+ * GNU General Public License for more details.
28199+ *
28200+ * You should have received a copy of the GNU General Public License
28201+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
28202+ */
28203+
28204+/*
28205+ * poll operation
28206+ * There is only one filesystem which implements ->poll operation, currently.
28207+ */
28208+
28209+#include "aufs.h"
28210+
28211+__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
28212+{
28213+ __poll_t mask;
28214+ struct file *h_file;
28215+ struct super_block *sb;
28216+
28217+ /* We should pretend an error happened. */
28218+ mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
28219+ sb = file->f_path.dentry->d_sb;
28220+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
28221+
28222+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
28223+ if (IS_ERR(h_file)) {
28224+ AuDbg("h_file %ld\n", PTR_ERR(h_file));
28225+ goto out;
28226+ }
28227+
28228+ mask = vfs_poll(h_file, pt);
28229+ fput(h_file); /* instead of au_read_post() */
28230+
28231+out:
28232+ si_read_unlock(sb);
28233+ if (mask & EPOLLERR)
28234+ AuDbg("mask 0x%x\n", mask);
28235+ return mask;
28236+}
28237diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28238--- /usr/share/empty/fs/aufs/posix_acl.c 1970-01-01 01:00:00.000000000 +0100
28239+++ linux/fs/aufs/posix_acl.c 2021-02-22 22:51:59.061411920 +0100
28240@@ -0,0 +1,105 @@
28241+// SPDX-License-Identifier: GPL-2.0
28242+/*
28243+ * Copyright (C) 2014-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+ * posix acl operations
28261+ */
28262+
28263+#include <linux/fs.h>
28264+#include "aufs.h"
28265+
28266+struct posix_acl *aufs_get_acl(struct inode *inode, int type)
28267+{
28268+ struct posix_acl *acl;
28269+ int err;
28270+ aufs_bindex_t bindex;
28271+ struct inode *h_inode;
28272+ struct super_block *sb;
28273+
28274+ acl = NULL;
28275+ sb = inode->i_sb;
28276+ si_read_lock(sb, AuLock_FLUSH);
28277+ ii_read_lock_child(inode);
28278+ if (!(sb->s_flags & SB_POSIXACL))
28279+ goto out;
28280+
28281+ bindex = au_ibtop(inode);
28282+ h_inode = au_h_iptr(inode, bindex);
28283+ if (unlikely(!h_inode
28284+ || ((h_inode->i_mode & S_IFMT)
28285+ != (inode->i_mode & S_IFMT)))) {
28286+ err = au_busy_or_stale();
28287+ acl = ERR_PTR(err);
28288+ goto out;
28289+ }
28290+
28291+ /* always topmost only */
28292+ acl = get_acl(h_inode, type);
28293+ if (IS_ERR(acl))
28294+ forget_cached_acl(inode, type);
28295+ else
28296+ set_cached_acl(inode, type, acl);
28297+
28298+out:
28299+ ii_read_unlock(inode);
28300+ si_read_unlock(sb);
28301+
28302+ AuTraceErrPtr(acl);
28303+ return acl;
28304+}
28305+
28306+int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
28307+{
28308+ int err;
28309+ ssize_t ssz;
28310+ struct dentry *dentry;
28311+ struct au_sxattr arg = {
28312+ .type = AU_ACL_SET,
28313+ .u.acl_set = {
28314+ .acl = acl,
28315+ .type = type
28316+ },
28317+ };
28318+
28319+ IMustLock(inode);
28320+
28321+ if (inode->i_ino == AUFS_ROOT_INO)
28322+ dentry = dget(inode->i_sb->s_root);
28323+ else {
28324+ dentry = d_find_alias(inode);
28325+ if (!dentry)
28326+ dentry = d_find_any_alias(inode);
28327+ if (!dentry) {
28328+ pr_warn("cannot handle this inode, "
28329+ "please report to aufs-users ML\n");
28330+ err = -ENOENT;
28331+ goto out;
28332+ }
28333+ }
28334+
28335+ ssz = au_sxattr(dentry, inode, &arg);
28336+ /* forget even it if succeeds since the branch might set differently */
28337+ forget_cached_acl(inode, type);
28338+ dput(dentry);
28339+ err = ssz;
28340+ if (ssz >= 0)
28341+ err = 0;
28342+
28343+out:
28344+ return err;
28345+}
28346diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28347--- /usr/share/empty/fs/aufs/procfs.c 1970-01-01 01:00:00.000000000 +0100
28348+++ linux/fs/aufs/procfs.c 2021-02-22 22:51:59.061411920 +0100
28349@@ -0,0 +1,170 @@
28350+// SPDX-License-Identifier: GPL-2.0
28351+/*
28352+ * Copyright (C) 2010-2020 Junjiro R. Okajima
28353+ *
28354+ * This program, aufs is free software; you can redistribute it and/or modify
28355+ * it under the terms of the GNU General Public License as published by
28356+ * the Free Software Foundation; either version 2 of the License, or
28357+ * (at your option) any later version.
28358+ *
28359+ * This program is distributed in the hope that it will be useful,
28360+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28361+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28362+ * GNU General Public License for more details.
28363+ *
28364+ * You should have received a copy of the GNU General Public License
28365+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
28366+ */
28367+
28368+/*
28369+ * procfs interfaces
28370+ */
28371+
28372+#include <linux/proc_fs.h>
28373+#include "aufs.h"
28374+
28375+static int au_procfs_plm_release(struct inode *inode, struct file *file)
28376+{
28377+ struct au_sbinfo *sbinfo;
28378+
28379+ sbinfo = file->private_data;
28380+ if (sbinfo) {
28381+ au_plink_maint_leave(sbinfo);
28382+ kobject_put(&sbinfo->si_kobj);
28383+ }
28384+
28385+ return 0;
28386+}
28387+
28388+static void au_procfs_plm_write_clean(struct file *file)
28389+{
28390+ struct au_sbinfo *sbinfo;
28391+
28392+ sbinfo = file->private_data;
28393+ if (sbinfo)
28394+ au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28395+}
28396+
28397+static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28398+{
28399+ int err;
28400+ struct super_block *sb;
28401+ struct au_sbinfo *sbinfo;
28402+ struct hlist_bl_node *pos;
28403+
28404+ err = -EBUSY;
28405+ if (unlikely(file->private_data))
28406+ goto out;
28407+
28408+ sb = NULL;
28409+ /* don't use au_sbilist_lock() here */
28410+ hlist_bl_lock(&au_sbilist);
28411+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
28412+ if (id == sysaufs_si_id(sbinfo)) {
28413+ if (kobject_get_unless_zero(&sbinfo->si_kobj))
28414+ sb = sbinfo->si_sb;
28415+ break;
28416+ }
28417+ hlist_bl_unlock(&au_sbilist);
28418+
28419+ err = -EINVAL;
28420+ if (unlikely(!sb))
28421+ goto out;
28422+
28423+ err = au_plink_maint_enter(sb);
28424+ if (!err)
28425+ /* keep kobject_get() */
28426+ file->private_data = sbinfo;
28427+ else
28428+ kobject_put(&sbinfo->si_kobj);
28429+out:
28430+ return err;
28431+}
28432+
28433+/*
28434+ * Accept a valid "si=xxxx" only.
28435+ * Once it is accepted successfully, accept "clean" too.
28436+ */
28437+static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28438+ size_t count, loff_t *ppos)
28439+{
28440+ ssize_t err;
28441+ unsigned long id;
28442+ /* last newline is allowed */
28443+ char buf[3 + sizeof(unsigned long) * 2 + 1];
28444+
28445+ err = -EACCES;
28446+ if (unlikely(!capable(CAP_SYS_ADMIN)))
28447+ goto out;
28448+
28449+ err = -EINVAL;
28450+ if (unlikely(count > sizeof(buf)))
28451+ goto out;
28452+
28453+ err = copy_from_user(buf, ubuf, count);
28454+ if (unlikely(err)) {
28455+ err = -EFAULT;
28456+ goto out;
28457+ }
28458+ buf[count] = 0;
28459+
28460+ err = -EINVAL;
28461+ if (!strcmp("clean", buf)) {
28462+ au_procfs_plm_write_clean(file);
28463+ goto out_success;
28464+ } else if (unlikely(strncmp("si=", buf, 3)))
28465+ goto out;
28466+
28467+ err = kstrtoul(buf + 3, 16, &id);
28468+ if (unlikely(err))
28469+ goto out;
28470+
28471+ err = au_procfs_plm_write_si(file, id);
28472+ if (unlikely(err))
28473+ goto out;
28474+
28475+out_success:
28476+ err = count; /* success */
28477+out:
28478+ return err;
28479+}
28480+
28481+static const struct proc_ops au_procfs_plm_op = {
28482+ .proc_write = au_procfs_plm_write,
28483+ .proc_release = au_procfs_plm_release
28484+};
28485+
28486+/* ---------------------------------------------------------------------- */
28487+
28488+static struct proc_dir_entry *au_procfs_dir;
28489+
28490+void au_procfs_fin(void)
28491+{
28492+ remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28493+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28494+}
28495+
28496+int __init au_procfs_init(void)
28497+{
28498+ int err;
28499+ struct proc_dir_entry *entry;
28500+
28501+ err = -ENOMEM;
28502+ au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28503+ if (unlikely(!au_procfs_dir))
28504+ goto out;
28505+
28506+ entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
28507+ au_procfs_dir, &au_procfs_plm_op);
28508+ if (unlikely(!entry))
28509+ goto out_dir;
28510+
28511+ err = 0;
28512+ goto out; /* success */
28513+
28514+
28515+out_dir:
28516+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28517+out:
28518+ return err;
28519+}
28520diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
28521--- /usr/share/empty/fs/aufs/rdu.c 1970-01-01 01:00:00.000000000 +0100
28522+++ linux/fs/aufs/rdu.c 2021-02-22 22:51:59.061411920 +0100
28523@@ -0,0 +1,384 @@
28524+// SPDX-License-Identifier: GPL-2.0
28525+/*
28526+ * Copyright (C) 2005-2020 Junjiro R. Okajima
28527+ *
28528+ * This program, aufs is free software; you can redistribute it and/or modify
28529+ * it under the terms of the GNU General Public License as published by
28530+ * the Free Software Foundation; either version 2 of the License, or
28531+ * (at your option) any later version.
28532+ *
28533+ * This program is distributed in the hope that it will be useful,
28534+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28535+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28536+ * GNU General Public License for more details.
28537+ *
28538+ * You should have received a copy of the GNU General Public License
28539+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
28540+ */
28541+
28542+/*
28543+ * readdir in userspace.
28544+ */
28545+
28546+#include <linux/compat.h>
28547+#include <linux/fs_stack.h>
28548+#include <linux/security.h>
28549+#include "aufs.h"
28550+
28551+/* bits for struct aufs_rdu.flags */
28552+#define AuRdu_CALLED 1
28553+#define AuRdu_CONT (1 << 1)
28554+#define AuRdu_FULL (1 << 2)
28555+#define au_ftest_rdu(flags, name) ((flags) & AuRdu_##name)
28556+#define au_fset_rdu(flags, name) \
28557+ do { (flags) |= AuRdu_##name; } while (0)
28558+#define au_fclr_rdu(flags, name) \
28559+ do { (flags) &= ~AuRdu_##name; } while (0)
28560+
28561+struct au_rdu_arg {
28562+ struct dir_context ctx;
28563+ struct aufs_rdu *rdu;
28564+ union au_rdu_ent_ul ent;
28565+ unsigned long end;
28566+
28567+ struct super_block *sb;
28568+ int err;
28569+};
28570+
28571+static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
28572+ loff_t offset, u64 h_ino, unsigned int d_type)
28573+{
28574+ int err, len;
28575+ struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
28576+ struct aufs_rdu *rdu = arg->rdu;
28577+ struct au_rdu_ent ent;
28578+
28579+ err = 0;
28580+ arg->err = 0;
28581+ au_fset_rdu(rdu->cookie.flags, CALLED);
28582+ len = au_rdu_len(nlen);
28583+ if (arg->ent.ul + len < arg->end) {
28584+ ent.ino = h_ino;
28585+ ent.bindex = rdu->cookie.bindex;
28586+ ent.type = d_type;
28587+ ent.nlen = nlen;
28588+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
28589+ ent.type = DT_UNKNOWN;
28590+
28591+ /* unnecessary to support mmap_sem since this is a dir */
28592+ err = -EFAULT;
28593+ if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
28594+ goto out;
28595+ if (copy_to_user(arg->ent.e->name, name, nlen))
28596+ goto out;
28597+ /* the terminating NULL */
28598+ if (__put_user(0, arg->ent.e->name + nlen))
28599+ goto out;
28600+ err = 0;
28601+ /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
28602+ arg->ent.ul += len;
28603+ rdu->rent++;
28604+ } else {
28605+ err = -EFAULT;
28606+ au_fset_rdu(rdu->cookie.flags, FULL);
28607+ rdu->full = 1;
28608+ rdu->tail = arg->ent;
28609+ }
28610+
28611+out:
28612+ /* AuTraceErr(err); */
28613+ return err;
28614+}
28615+
28616+static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
28617+{
28618+ int err;
28619+ loff_t offset;
28620+ struct au_rdu_cookie *cookie = &arg->rdu->cookie;
28621+
28622+ /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
28623+ offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
28624+ err = offset;
28625+ if (unlikely(offset != cookie->h_pos))
28626+ goto out;
28627+
28628+ err = 0;
28629+ do {
28630+ arg->err = 0;
28631+ au_fclr_rdu(cookie->flags, CALLED);
28632+ /* smp_mb(); */
28633+ err = vfsub_iterate_dir(h_file, &arg->ctx);
28634+ if (err >= 0)
28635+ err = arg->err;
28636+ } while (!err
28637+ && au_ftest_rdu(cookie->flags, CALLED)
28638+ && !au_ftest_rdu(cookie->flags, FULL));
28639+ cookie->h_pos = h_file->f_pos;
28640+
28641+out:
28642+ AuTraceErr(err);
28643+ return err;
28644+}
28645+
28646+static int au_rdu(struct file *file, struct aufs_rdu *rdu)
28647+{
28648+ int err;
28649+ aufs_bindex_t bbot;
28650+ struct au_rdu_arg arg = {
28651+ .ctx = {
28652+ .actor = au_rdu_fill
28653+ }
28654+ };
28655+ struct dentry *dentry;
28656+ struct inode *inode;
28657+ struct file *h_file;
28658+ struct au_rdu_cookie *cookie = &rdu->cookie;
28659+
28660+ /* VERIFY_WRITE */
28661+ err = !access_ok(rdu->ent.e, rdu->sz);
28662+ if (unlikely(err)) {
28663+ err = -EFAULT;
28664+ AuTraceErr(err);
28665+ goto out;
28666+ }
28667+ rdu->rent = 0;
28668+ rdu->tail = rdu->ent;
28669+ rdu->full = 0;
28670+ arg.rdu = rdu;
28671+ arg.ent = rdu->ent;
28672+ arg.end = arg.ent.ul;
28673+ arg.end += rdu->sz;
28674+
28675+ err = -ENOTDIR;
28676+ if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
28677+ goto out;
28678+
28679+ err = security_file_permission(file, MAY_READ);
28680+ AuTraceErr(err);
28681+ if (unlikely(err))
28682+ goto out;
28683+
28684+ dentry = file->f_path.dentry;
28685+ inode = d_inode(dentry);
28686+ inode_lock_shared(inode);
28687+
28688+ arg.sb = inode->i_sb;
28689+ err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
28690+ if (unlikely(err))
28691+ goto out_mtx;
28692+ err = au_alive_dir(dentry);
28693+ if (unlikely(err))
28694+ goto out_si;
28695+ /* todo: reval? */
28696+ fi_read_lock(file);
28697+
28698+ err = -EAGAIN;
28699+ if (unlikely(au_ftest_rdu(cookie->flags, CONT)
28700+ && cookie->generation != au_figen(file)))
28701+ goto out_unlock;
28702+
28703+ err = 0;
28704+ if (!rdu->blk) {
28705+ rdu->blk = au_sbi(arg.sb)->si_rdblk;
28706+ if (!rdu->blk)
28707+ rdu->blk = au_dir_size(file, /*dentry*/NULL);
28708+ }
28709+ bbot = au_fbtop(file);
28710+ if (cookie->bindex < bbot)
28711+ cookie->bindex = bbot;
28712+ bbot = au_fbbot_dir(file);
28713+ /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
28714+ for (; !err && cookie->bindex <= bbot;
28715+ cookie->bindex++, cookie->h_pos = 0) {
28716+ h_file = au_hf_dir(file, cookie->bindex);
28717+ if (!h_file)
28718+ continue;
28719+
28720+ au_fclr_rdu(cookie->flags, FULL);
28721+ err = au_rdu_do(h_file, &arg);
28722+ AuTraceErr(err);
28723+ if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
28724+ break;
28725+ }
28726+ AuDbg("rent %llu\n", rdu->rent);
28727+
28728+ if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
28729+ rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
28730+ au_fset_rdu(cookie->flags, CONT);
28731+ cookie->generation = au_figen(file);
28732+ }
28733+
28734+ ii_read_lock_child(inode);
28735+ fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
28736+ ii_read_unlock(inode);
28737+
28738+out_unlock:
28739+ fi_read_unlock(file);
28740+out_si:
28741+ si_read_unlock(arg.sb);
28742+out_mtx:
28743+ inode_unlock_shared(inode);
28744+out:
28745+ AuTraceErr(err);
28746+ return err;
28747+}
28748+
28749+static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
28750+{
28751+ int err;
28752+ ino_t ino;
28753+ unsigned long long nent;
28754+ union au_rdu_ent_ul *u;
28755+ struct au_rdu_ent ent;
28756+ struct super_block *sb;
28757+
28758+ err = 0;
28759+ nent = rdu->nent;
28760+ u = &rdu->ent;
28761+ sb = file->f_path.dentry->d_sb;
28762+ si_read_lock(sb, AuLock_FLUSH);
28763+ while (nent-- > 0) {
28764+ /* unnecessary to support mmap_sem since this is a dir */
28765+ err = copy_from_user(&ent, u->e, sizeof(ent));
28766+ if (!err)
28767+ /* VERIFY_WRITE */
28768+ err = !access_ok(&u->e->ino, sizeof(ino));
28769+ if (unlikely(err)) {
28770+ err = -EFAULT;
28771+ AuTraceErr(err);
28772+ break;
28773+ }
28774+
28775+ /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
28776+ if (!ent.wh)
28777+ err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
28778+ else
28779+ err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
28780+ &ino);
28781+ if (unlikely(err)) {
28782+ AuTraceErr(err);
28783+ break;
28784+ }
28785+
28786+ err = __put_user(ino, &u->e->ino);
28787+ if (unlikely(err)) {
28788+ err = -EFAULT;
28789+ AuTraceErr(err);
28790+ break;
28791+ }
28792+ u->ul += au_rdu_len(ent.nlen);
28793+ }
28794+ si_read_unlock(sb);
28795+
28796+ return err;
28797+}
28798+
28799+/* ---------------------------------------------------------------------- */
28800+
28801+static int au_rdu_verify(struct aufs_rdu *rdu)
28802+{
28803+ AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
28804+ "%llu, b%d, 0x%x, g%u}\n",
28805+ rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
28806+ rdu->blk,
28807+ rdu->rent, rdu->shwh, rdu->full,
28808+ rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
28809+ rdu->cookie.generation);
28810+
28811+ if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
28812+ return 0;
28813+
28814+ AuDbg("%u:%u\n",
28815+ rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
28816+ return -EINVAL;
28817+}
28818+
28819+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28820+{
28821+ long err, e;
28822+ struct aufs_rdu rdu;
28823+ void __user *p = (void __user *)arg;
28824+
28825+ err = copy_from_user(&rdu, p, sizeof(rdu));
28826+ if (unlikely(err)) {
28827+ err = -EFAULT;
28828+ AuTraceErr(err);
28829+ goto out;
28830+ }
28831+ err = au_rdu_verify(&rdu);
28832+ if (unlikely(err))
28833+ goto out;
28834+
28835+ switch (cmd) {
28836+ case AUFS_CTL_RDU:
28837+ err = au_rdu(file, &rdu);
28838+ if (unlikely(err))
28839+ break;
28840+
28841+ e = copy_to_user(p, &rdu, sizeof(rdu));
28842+ if (unlikely(e)) {
28843+ err = -EFAULT;
28844+ AuTraceErr(err);
28845+ }
28846+ break;
28847+ case AUFS_CTL_RDU_INO:
28848+ err = au_rdu_ino(file, &rdu);
28849+ break;
28850+
28851+ default:
28852+ /* err = -ENOTTY; */
28853+ err = -EINVAL;
28854+ }
28855+
28856+out:
28857+ AuTraceErr(err);
28858+ return err;
28859+}
28860+
28861+#ifdef CONFIG_COMPAT
28862+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28863+{
28864+ long err, e;
28865+ struct aufs_rdu rdu;
28866+ void __user *p = compat_ptr(arg);
28867+
28868+ /* todo: get_user()? */
28869+ err = copy_from_user(&rdu, p, sizeof(rdu));
28870+ if (unlikely(err)) {
28871+ err = -EFAULT;
28872+ AuTraceErr(err);
28873+ goto out;
28874+ }
28875+ rdu.ent.e = compat_ptr(rdu.ent.ul);
28876+ err = au_rdu_verify(&rdu);
28877+ if (unlikely(err))
28878+ goto out;
28879+
28880+ switch (cmd) {
28881+ case AUFS_CTL_RDU:
28882+ err = au_rdu(file, &rdu);
28883+ if (unlikely(err))
28884+ break;
28885+
28886+ rdu.ent.ul = ptr_to_compat(rdu.ent.e);
28887+ rdu.tail.ul = ptr_to_compat(rdu.tail.e);
28888+ e = copy_to_user(p, &rdu, sizeof(rdu));
28889+ if (unlikely(e)) {
28890+ err = -EFAULT;
28891+ AuTraceErr(err);
28892+ }
28893+ break;
28894+ case AUFS_CTL_RDU_INO:
28895+ err = au_rdu_ino(file, &rdu);
28896+ break;
28897+
28898+ default:
28899+ /* err = -ENOTTY; */
28900+ err = -EINVAL;
28901+ }
28902+
28903+out:
28904+ AuTraceErr(err);
28905+ return err;
28906+}
28907+#endif
28908diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
28909--- /usr/share/empty/fs/aufs/rwsem.h 1970-01-01 01:00:00.000000000 +0100
28910+++ linux/fs/aufs/rwsem.h 2021-02-22 22:51:59.061411920 +0100
28911@@ -0,0 +1,77 @@
28912+/* SPDX-License-Identifier: GPL-2.0 */
28913+/*
28914+ * Copyright (C) 2005-2020 Junjiro R. Okajima
28915+ *
28916+ * This program, aufs is free software; you can redistribute it and/or modify
28917+ * it under the terms of the GNU General Public License as published by
28918+ * the Free Software Foundation; either version 2 of the License, or
28919+ * (at your option) any later version.
28920+ *
28921+ * This program is distributed in the hope that it will be useful,
28922+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28923+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28924+ * GNU General Public License for more details.
28925+ *
28926+ * You should have received a copy of the GNU General Public License
28927+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
28928+ */
28929+
28930+/*
28931+ * simple read-write semaphore wrappers
28932+ */
28933+
28934+#ifndef __AUFS_RWSEM_H__
28935+#define __AUFS_RWSEM_H__
28936+
28937+#ifdef __KERNEL__
28938+
28939+#include "debug.h"
28940+
28941+/* in the future, the name 'au_rwsem' will be totally gone */
28942+#define au_rwsem rw_semaphore
28943+
28944+/* to debug easier, do not make them inlined functions */
28945+#define AuRwMustNoWaiters(rw) AuDebugOn(rwsem_is_contended(rw))
28946+/* rwsem_is_locked() is unusable */
28947+#define AuRwMustReadLock(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
28948+ && !lockdep_recursing(current) \
28949+ && debug_locks \
28950+ && !lockdep_is_held_type(rw, 1))
28951+#define AuRwMustWriteLock(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
28952+ && !lockdep_recursing(current) \
28953+ && debug_locks \
28954+ && !lockdep_is_held_type(rw, 0))
28955+#define AuRwMustAnyLock(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
28956+ && !lockdep_recursing(current) \
28957+ && debug_locks \
28958+ && !lockdep_is_held(rw))
28959+#define AuRwDestroy(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
28960+ && !lockdep_recursing(current) \
28961+ && debug_locks \
28962+ && lockdep_is_held(rw))
28963+
28964+#define au_rw_init(rw) init_rwsem(rw)
28965+
28966+#define au_rw_init_wlock(rw) do { \
28967+ au_rw_init(rw); \
28968+ down_write(rw); \
28969+ } while (0)
28970+
28971+#define au_rw_init_wlock_nested(rw, lsc) do { \
28972+ au_rw_init(rw); \
28973+ down_write_nested(rw, lsc); \
28974+ } while (0)
28975+
28976+#define au_rw_read_lock(rw) down_read(rw)
28977+#define au_rw_read_lock_nested(rw, lsc) down_read_nested(rw, lsc)
28978+#define au_rw_read_unlock(rw) up_read(rw)
28979+#define au_rw_dgrade_lock(rw) downgrade_write(rw)
28980+#define au_rw_write_lock(rw) down_write(rw)
28981+#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
28982+#define au_rw_write_unlock(rw) up_write(rw)
28983+/* why is not _nested version defined? */
28984+#define au_rw_read_trylock(rw) down_read_trylock(rw)
28985+#define au_rw_write_trylock(rw) down_write_trylock(rw)
28986+
28987+#endif /* __KERNEL__ */
28988+#endif /* __AUFS_RWSEM_H__ */
28989diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
28990--- /usr/share/empty/fs/aufs/sbinfo.c 1970-01-01 01:00:00.000000000 +0100
28991+++ linux/fs/aufs/sbinfo.c 2021-02-22 22:51:59.061411920 +0100
28992@@ -0,0 +1,314 @@
28993+// SPDX-License-Identifier: GPL-2.0
28994+/*
28995+ * Copyright (C) 2005-2020 Junjiro R. Okajima
28996+ *
28997+ * This program, aufs is free software; you can redistribute it and/or modify
28998+ * it under the terms of the GNU General Public License as published by
28999+ * the Free Software Foundation; either version 2 of the License, or
29000+ * (at your option) any later version.
29001+ *
29002+ * This program is distributed in the hope that it will be useful,
29003+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29004+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29005+ * GNU General Public License for more details.
29006+ *
29007+ * You should have received a copy of the GNU General Public License
29008+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
29009+ */
29010+
29011+/*
29012+ * superblock private data
29013+ */
29014+
29015+#include <linux/iversion.h>
29016+#include "aufs.h"
29017+
29018+/*
29019+ * they are necessary regardless sysfs is disabled.
29020+ */
29021+void au_si_free(struct kobject *kobj)
29022+{
29023+ int i;
29024+ struct au_sbinfo *sbinfo;
29025+ char *locked __maybe_unused; /* debug only */
29026+
29027+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
29028+ for (i = 0; i < AuPlink_NHASH; i++)
29029+ AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
29030+ AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
29031+
29032+ AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29033+ au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29034+ AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29035+ au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
29036+
29037+ dbgaufs_si_fin(sbinfo);
29038+ au_rw_write_lock(&sbinfo->si_rwsem);
29039+ au_br_free(sbinfo);
29040+ au_rw_write_unlock(&sbinfo->si_rwsem);
29041+
29042+ au_kfree_try_rcu(sbinfo->si_branch);
29043+ mutex_destroy(&sbinfo->si_xib_mtx);
29044+ AuRwDestroy(&sbinfo->si_rwsem);
29045+
29046+ au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29047+ /* si_nfiles is waited too */
29048+ au_kfree_rcu(sbinfo);
29049+}
29050+
29051+int au_si_alloc(struct super_block *sb)
29052+{
29053+ int err, i;
29054+ struct au_sbinfo *sbinfo;
29055+
29056+ err = -ENOMEM;
29057+ sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
29058+ if (unlikely(!sbinfo))
29059+ goto out;
29060+
29061+ /* will be reallocated separately */
29062+ sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29063+ if (unlikely(!sbinfo->si_branch))
29064+ goto out_sbinfo;
29065+
29066+ err = sysaufs_si_init(sbinfo);
29067+ if (!err) {
29068+ dbgaufs_si_null(sbinfo);
29069+ err = dbgaufs_si_init(sbinfo);
29070+ if (unlikely(err))
29071+ kobject_put(&sbinfo->si_kobj);
29072+ }
29073+ if (unlikely(err))
29074+ goto out_br;
29075+
29076+ au_nwt_init(&sbinfo->si_nowait);
29077+ au_rw_init_wlock(&sbinfo->si_rwsem);
29078+
29079+ au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29080+ au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
29081+
29082+ sbinfo->si_bbot = -1;
29083+ sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
29084+
29085+ sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29086+ sbinfo->si_wbr_create = AuWbrCreate_Def;
29087+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29088+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
29089+
29090+ au_fhsm_init(sbinfo);
29091+
29092+ sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
29093+
29094+ sbinfo->si_xino_jiffy = jiffies;
29095+ sbinfo->si_xino_expire
29096+ = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
29097+ mutex_init(&sbinfo->si_xib_mtx);
29098+ /* leave si_xib_last_pindex and si_xib_next_bit */
29099+
29100+ INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
29101+
29102+ sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
29103+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29104+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29105+ sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29106+
29107+ for (i = 0; i < AuPlink_NHASH; i++)
29108+ INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
29109+ init_waitqueue_head(&sbinfo->si_plink_wq);
29110+ spin_lock_init(&sbinfo->si_plink_maint_lock);
29111+
29112+ INIT_HLIST_BL_HEAD(&sbinfo->si_files);
29113+
29114+ /* with getattr by default */
29115+ sbinfo->si_iop_array = aufs_iop;
29116+
29117+ /* leave other members for sysaufs and si_mnt. */
29118+ sbinfo->si_sb = sb;
29119+ sb->s_fs_info = sbinfo;
29120+ si_pid_set(sb);
29121+ return 0; /* success */
29122+
29123+out_br:
29124+ au_kfree_try_rcu(sbinfo->si_branch);
29125+out_sbinfo:
29126+ au_kfree_rcu(sbinfo);
29127+out:
29128+ return err;
29129+}
29130+
29131+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
29132+{
29133+ int err, sz;
29134+ struct au_branch **brp;
29135+
29136+ AuRwMustWriteLock(&sbinfo->si_rwsem);
29137+
29138+ err = -ENOMEM;
29139+ sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
29140+ if (unlikely(!sz))
29141+ sz = sizeof(*brp);
29142+ brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29143+ may_shrink);
29144+ if (brp) {
29145+ sbinfo->si_branch = brp;
29146+ err = 0;
29147+ }
29148+
29149+ return err;
29150+}
29151+
29152+/* ---------------------------------------------------------------------- */
29153+
29154+unsigned int au_sigen_inc(struct super_block *sb)
29155+{
29156+ unsigned int gen;
29157+ struct inode *inode;
29158+
29159+ SiMustWriteLock(sb);
29160+
29161+ gen = ++au_sbi(sb)->si_generation;
29162+ au_update_digen(sb->s_root);
29163+ inode = d_inode(sb->s_root);
29164+ au_update_iigen(inode, /*half*/0);
29165+ inode_inc_iversion(inode);
29166+ return gen;
29167+}
29168+
29169+aufs_bindex_t au_new_br_id(struct super_block *sb)
29170+{
29171+ aufs_bindex_t br_id;
29172+ int i;
29173+ struct au_sbinfo *sbinfo;
29174+
29175+ SiMustWriteLock(sb);
29176+
29177+ sbinfo = au_sbi(sb);
29178+ for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29179+ br_id = ++sbinfo->si_last_br_id;
29180+ AuDebugOn(br_id < 0);
29181+ if (br_id && au_br_index(sb, br_id) < 0)
29182+ return br_id;
29183+ }
29184+
29185+ return -1;
29186+}
29187+
29188+/* ---------------------------------------------------------------------- */
29189+
29190+/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29191+int si_read_lock(struct super_block *sb, int flags)
29192+{
29193+ int err;
29194+
29195+ err = 0;
29196+ if (au_ftest_lock(flags, FLUSH))
29197+ au_nwt_flush(&au_sbi(sb)->si_nowait);
29198+
29199+ si_noflush_read_lock(sb);
29200+ err = au_plink_maint(sb, flags);
29201+ if (unlikely(err))
29202+ si_read_unlock(sb);
29203+
29204+ return err;
29205+}
29206+
29207+int si_write_lock(struct super_block *sb, int flags)
29208+{
29209+ int err;
29210+
29211+ if (au_ftest_lock(flags, FLUSH))
29212+ au_nwt_flush(&au_sbi(sb)->si_nowait);
29213+
29214+ si_noflush_write_lock(sb);
29215+ err = au_plink_maint(sb, flags);
29216+ if (unlikely(err))
29217+ si_write_unlock(sb);
29218+
29219+ return err;
29220+}
29221+
29222+/* dentry and super_block lock. call at entry point */
29223+int aufs_read_lock(struct dentry *dentry, int flags)
29224+{
29225+ int err;
29226+ struct super_block *sb;
29227+
29228+ sb = dentry->d_sb;
29229+ err = si_read_lock(sb, flags);
29230+ if (unlikely(err))
29231+ goto out;
29232+
29233+ if (au_ftest_lock(flags, DW))
29234+ di_write_lock_child(dentry);
29235+ else
29236+ di_read_lock_child(dentry, flags);
29237+
29238+ if (au_ftest_lock(flags, GEN)) {
29239+ err = au_digen_test(dentry, au_sigen(sb));
29240+ if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29241+ AuDebugOn(!err && au_dbrange_test(dentry));
29242+ else if (!err)
29243+ err = au_dbrange_test(dentry);
29244+ if (unlikely(err))
29245+ aufs_read_unlock(dentry, flags);
29246+ }
29247+
29248+out:
29249+ return err;
29250+}
29251+
29252+void aufs_read_unlock(struct dentry *dentry, int flags)
29253+{
29254+ if (au_ftest_lock(flags, DW))
29255+ di_write_unlock(dentry);
29256+ else
29257+ di_read_unlock(dentry, flags);
29258+ si_read_unlock(dentry->d_sb);
29259+}
29260+
29261+void aufs_write_lock(struct dentry *dentry)
29262+{
29263+ si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
29264+ di_write_lock_child(dentry);
29265+}
29266+
29267+void aufs_write_unlock(struct dentry *dentry)
29268+{
29269+ di_write_unlock(dentry);
29270+ si_write_unlock(dentry->d_sb);
29271+}
29272+
29273+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
29274+{
29275+ int err;
29276+ unsigned int sigen;
29277+ struct super_block *sb;
29278+
29279+ sb = d1->d_sb;
29280+ err = si_read_lock(sb, flags);
29281+ if (unlikely(err))
29282+ goto out;
29283+
29284+ di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
29285+
29286+ if (au_ftest_lock(flags, GEN)) {
29287+ sigen = au_sigen(sb);
29288+ err = au_digen_test(d1, sigen);
29289+ AuDebugOn(!err && au_dbrange_test(d1));
29290+ if (!err) {
29291+ err = au_digen_test(d2, sigen);
29292+ AuDebugOn(!err && au_dbrange_test(d2));
29293+ }
29294+ if (unlikely(err))
29295+ aufs_read_and_write_unlock2(d1, d2);
29296+ }
29297+
29298+out:
29299+ return err;
29300+}
29301+
29302+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29303+{
29304+ di_write_unlock2(d1, d2);
29305+ si_read_unlock(d1->d_sb);
29306+}
29307diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29308--- /usr/share/empty/fs/aufs/super.c 1970-01-01 01:00:00.000000000 +0100
29309+++ linux/fs/aufs/super.c 2021-02-22 22:51:59.061411920 +0100
29310@@ -0,0 +1,1047 @@
29311+// SPDX-License-Identifier: GPL-2.0
29312+/*
29313+ * Copyright (C) 2005-2020 Junjiro R. Okajima
29314+ *
29315+ * This program, aufs is free software; you can redistribute it and/or modify
29316+ * it under the terms of the GNU General Public License as published by
29317+ * the Free Software Foundation; either version 2 of the License, or
29318+ * (at your option) any later version.
29319+ *
29320+ * This program is distributed in the hope that it will be useful,
29321+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29322+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29323+ * GNU General Public License for more details.
29324+ *
29325+ * You should have received a copy of the GNU General Public License
29326+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
29327+ */
29328+
29329+/*
29330+ * mount and super_block operations
29331+ */
29332+
29333+#include <linux/iversion.h>
29334+#include <linux/mm.h>
29335+#include <linux/seq_file.h>
29336+#include <linux/statfs.h>
29337+#include <linux/vmalloc.h>
29338+#include "aufs.h"
29339+
29340+/*
29341+ * super_operations
29342+ */
29343+static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29344+{
29345+ struct au_icntnr *c;
29346+
29347+ c = au_cache_alloc_icntnr();
29348+ if (c) {
29349+ au_icntnr_init(c);
29350+ inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
29351+ c->iinfo.ii_hinode = NULL;
29352+ return &c->vfs_inode;
29353+ }
29354+ return NULL;
29355+}
29356+
29357+static void aufs_destroy_inode(struct inode *inode)
29358+{
29359+ if (!au_is_bad_inode(inode))
29360+ au_iinfo_fin(inode);
29361+}
29362+
29363+static void aufs_free_inode(struct inode *inode)
29364+{
29365+ au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
29366+}
29367+
29368+struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29369+{
29370+ struct inode *inode;
29371+ int err;
29372+
29373+ inode = iget_locked(sb, ino);
29374+ if (unlikely(!inode)) {
29375+ inode = ERR_PTR(-ENOMEM);
29376+ goto out;
29377+ }
29378+ if (!(inode->i_state & I_NEW))
29379+ goto out;
29380+
29381+ err = au_xigen_new(inode);
29382+ if (!err)
29383+ err = au_iinfo_init(inode);
29384+ if (!err)
29385+ inode_inc_iversion(inode);
29386+ else {
29387+ iget_failed(inode);
29388+ inode = ERR_PTR(err);
29389+ }
29390+
29391+out:
29392+ /* never return NULL */
29393+ AuDebugOn(!inode);
29394+ AuTraceErrPtr(inode);
29395+ return inode;
29396+}
29397+
29398+/* lock free root dinfo */
29399+static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29400+{
29401+ int err;
29402+ aufs_bindex_t bindex, bbot;
29403+ struct path path;
29404+ struct au_hdentry *hdp;
29405+ struct au_branch *br;
29406+ au_br_perm_str_t perm;
29407+
29408+ err = 0;
29409+ bbot = au_sbbot(sb);
29410+ bindex = 0;
29411+ hdp = au_hdentry(au_di(sb->s_root), bindex);
29412+ for (; !err && bindex <= bbot; bindex++, hdp++) {
29413+ br = au_sbr(sb, bindex);
29414+ path.mnt = au_br_mnt(br);
29415+ path.dentry = hdp->hd_dentry;
29416+ err = au_seq_path(seq, &path);
29417+ if (!err) {
29418+ au_optstr_br_perm(&perm, br->br_perm);
29419+ seq_printf(seq, "=%s", perm.a);
29420+ if (bindex != bbot)
29421+ seq_putc(seq, ':');
29422+ }
29423+ }
29424+ if (unlikely(err || seq_has_overflowed(seq)))
29425+ err = -E2BIG;
29426+
29427+ return err;
29428+}
29429+
29430+static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29431+ const char *append)
29432+{
29433+ char *p;
29434+
29435+ p = fmt;
29436+ while (*pat != ':')
29437+ *p++ = *pat++;
29438+ *p++ = *pat++;
29439+ strcpy(p, append);
29440+ AuDebugOn(strlen(fmt) >= len);
29441+}
29442+
29443+static void au_show_wbr_create(struct seq_file *m, int v,
29444+ struct au_sbinfo *sbinfo)
29445+{
29446+ const char *pat;
29447+ char fmt[32];
29448+ struct au_wbr_mfs *mfs;
29449+
29450+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29451+
29452+ seq_puts(m, ",create=");
29453+ pat = au_optstr_wbr_create(v);
29454+ mfs = &sbinfo->si_wbr_mfs;
29455+ switch (v) {
29456+ case AuWbrCreate_TDP:
29457+ case AuWbrCreate_RR:
29458+ case AuWbrCreate_MFS:
29459+ case AuWbrCreate_PMFS:
29460+ seq_puts(m, pat);
29461+ break;
29462+ case AuWbrCreate_MFSRR:
29463+ case AuWbrCreate_TDMFS:
29464+ case AuWbrCreate_PMFSRR:
29465+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29466+ seq_printf(m, fmt, mfs->mfsrr_watermark);
29467+ break;
29468+ case AuWbrCreate_MFSV:
29469+ case AuWbrCreate_PMFSV:
29470+ au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29471+ seq_printf(m, fmt,
29472+ jiffies_to_msecs(mfs->mfs_expire)
29473+ / MSEC_PER_SEC);
29474+ break;
29475+ case AuWbrCreate_MFSRRV:
29476+ case AuWbrCreate_TDMFSV:
29477+ case AuWbrCreate_PMFSRRV:
29478+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29479+ seq_printf(m, fmt, mfs->mfsrr_watermark,
29480+ jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
29481+ break;
29482+ default:
29483+ BUG();
29484+ }
29485+}
29486+
29487+static int au_show_xino(struct seq_file *seq, struct super_block *sb)
29488+{
29489+#ifdef CONFIG_SYSFS
29490+ return 0;
29491+#else
29492+ int err;
29493+ const int len = sizeof(AUFS_XINO_FNAME) - 1;
29494+ aufs_bindex_t bindex, brid;
29495+ struct qstr *name;
29496+ struct file *f;
29497+ struct dentry *d, *h_root;
29498+ struct au_branch *br;
29499+
29500+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29501+
29502+ err = 0;
29503+ f = au_sbi(sb)->si_xib;
29504+ if (!f)
29505+ goto out;
29506+
29507+ /* stop printing the default xino path on the first writable branch */
29508+ h_root = NULL;
29509+ bindex = au_xi_root(sb, f->f_path.dentry);
29510+ if (bindex >= 0) {
29511+ br = au_sbr_sb(sb, bindex);
29512+ h_root = au_br_dentry(br);
29513+ }
29514+
29515+ d = f->f_path.dentry;
29516+ name = &d->d_name;
29517+ /* safe ->d_parent because the file is unlinked */
29518+ if (d->d_parent == h_root
29519+ && name->len == len
29520+ && !memcmp(name->name, AUFS_XINO_FNAME, len))
29521+ goto out;
29522+
29523+ seq_puts(seq, ",xino=");
29524+ err = au_xino_path(seq, f);
29525+
29526+out:
29527+ return err;
29528+#endif
29529+}
29530+
29531+/* seq_file will re-call me in case of too long string */
29532+static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
29533+{
29534+ int err;
29535+ unsigned int mnt_flags, v;
29536+ struct super_block *sb;
29537+ struct au_sbinfo *sbinfo;
29538+
29539+#define AuBool(name, str) do { \
29540+ v = au_opt_test(mnt_flags, name); \
29541+ if (v != au_opt_test(AuOpt_Def, name)) \
29542+ seq_printf(m, ",%s" #str, v ? "" : "no"); \
29543+} while (0)
29544+
29545+#define AuStr(name, str) do { \
29546+ v = mnt_flags & AuOptMask_##name; \
29547+ if (v != (AuOpt_Def & AuOptMask_##name)) \
29548+ seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
29549+} while (0)
29550+
29551+#define AuUInt(name, str, val) do { \
29552+ if (val != AUFS_##name##_DEF) \
29553+ seq_printf(m, "," #str "=%u", val); \
29554+} while (0)
29555+
29556+ sb = dentry->d_sb;
29557+ if (sb->s_flags & SB_POSIXACL)
29558+ seq_puts(m, ",acl");
29559+#if 0 /* reserved for future use */
29560+ if (sb->s_flags & SB_I_VERSION)
29561+ seq_puts(m, ",i_version");
29562+#endif
29563+
29564+ /* lock free root dinfo */
29565+ si_noflush_read_lock(sb);
29566+ sbinfo = au_sbi(sb);
29567+ seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
29568+
29569+ mnt_flags = au_mntflags(sb);
29570+ if (au_opt_test(mnt_flags, XINO)) {
29571+ err = au_show_xino(m, sb);
29572+ if (unlikely(err))
29573+ goto out;
29574+ } else
29575+ seq_puts(m, ",noxino");
29576+
29577+ AuBool(TRUNC_XINO, trunc_xino);
29578+ AuStr(UDBA, udba);
29579+ AuBool(SHWH, shwh);
29580+ AuBool(PLINK, plink);
29581+ AuBool(DIO, dio);
29582+ AuBool(DIRPERM1, dirperm1);
29583+
29584+ v = sbinfo->si_wbr_create;
29585+ if (v != AuWbrCreate_Def)
29586+ au_show_wbr_create(m, v, sbinfo);
29587+
29588+ v = sbinfo->si_wbr_copyup;
29589+ if (v != AuWbrCopyup_Def)
29590+ seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
29591+
29592+ v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
29593+ if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
29594+ seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
29595+
29596+ AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
29597+
29598+ v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
29599+ AuUInt(RDCACHE, rdcache, v);
29600+
29601+ AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
29602+ AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
29603+
29604+ au_fhsm_show(m, sbinfo);
29605+
29606+ AuBool(DIRREN, dirren);
29607+ AuBool(SUM, sum);
29608+ /* AuBool(SUM_W, wsum); */
29609+ AuBool(WARN_PERM, warn_perm);
29610+ AuBool(VERBOSE, verbose);
29611+
29612+out:
29613+ /* be sure to print "br:" last */
29614+ if (!sysaufs_brs) {
29615+ seq_puts(m, ",br:");
29616+ au_show_brs(m, sb);
29617+ }
29618+ si_read_unlock(sb);
29619+ return 0;
29620+
29621+#undef AuBool
29622+#undef AuStr
29623+#undef AuUInt
29624+}
29625+
29626+/* ---------------------------------------------------------------------- */
29627+
29628+/* sum mode which returns the summation for statfs(2) */
29629+
29630+static u64 au_add_till_max(u64 a, u64 b)
29631+{
29632+ u64 old;
29633+
29634+ old = a;
29635+ a += b;
29636+ if (old <= a)
29637+ return a;
29638+ return ULLONG_MAX;
29639+}
29640+
29641+static u64 au_mul_till_max(u64 a, long mul)
29642+{
29643+ u64 old;
29644+
29645+ old = a;
29646+ a *= mul;
29647+ if (old <= a)
29648+ return a;
29649+ return ULLONG_MAX;
29650+}
29651+
29652+static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
29653+{
29654+ int err;
29655+ long bsize, factor;
29656+ u64 blocks, bfree, bavail, files, ffree;
29657+ aufs_bindex_t bbot, bindex, i;
29658+ unsigned char shared;
29659+ struct path h_path;
29660+ struct super_block *h_sb;
29661+
29662+ err = 0;
29663+ bsize = LONG_MAX;
29664+ files = 0;
29665+ ffree = 0;
29666+ blocks = 0;
29667+ bfree = 0;
29668+ bavail = 0;
29669+ bbot = au_sbbot(sb);
29670+ for (bindex = 0; bindex <= bbot; bindex++) {
29671+ h_path.mnt = au_sbr_mnt(sb, bindex);
29672+ h_sb = h_path.mnt->mnt_sb;
29673+ shared = 0;
29674+ for (i = 0; !shared && i < bindex; i++)
29675+ shared = (au_sbr_sb(sb, i) == h_sb);
29676+ if (shared)
29677+ continue;
29678+
29679+ /* sb->s_root for NFS is unreliable */
29680+ h_path.dentry = h_path.mnt->mnt_root;
29681+ err = vfs_statfs(&h_path, buf);
29682+ if (unlikely(err))
29683+ goto out;
29684+
29685+ if (bsize > buf->f_bsize) {
29686+ /*
29687+ * we will reduce bsize, so we have to expand blocks
29688+ * etc. to match them again
29689+ */
29690+ factor = (bsize / buf->f_bsize);
29691+ blocks = au_mul_till_max(blocks, factor);
29692+ bfree = au_mul_till_max(bfree, factor);
29693+ bavail = au_mul_till_max(bavail, factor);
29694+ bsize = buf->f_bsize;
29695+ }
29696+
29697+ factor = (buf->f_bsize / bsize);
29698+ blocks = au_add_till_max(blocks,
29699+ au_mul_till_max(buf->f_blocks, factor));
29700+ bfree = au_add_till_max(bfree,
29701+ au_mul_till_max(buf->f_bfree, factor));
29702+ bavail = au_add_till_max(bavail,
29703+ au_mul_till_max(buf->f_bavail, factor));
29704+ files = au_add_till_max(files, buf->f_files);
29705+ ffree = au_add_till_max(ffree, buf->f_ffree);
29706+ }
29707+
29708+ buf->f_bsize = bsize;
29709+ buf->f_blocks = blocks;
29710+ buf->f_bfree = bfree;
29711+ buf->f_bavail = bavail;
29712+ buf->f_files = files;
29713+ buf->f_ffree = ffree;
29714+ buf->f_frsize = 0;
29715+
29716+out:
29717+ return err;
29718+}
29719+
29720+static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
29721+{
29722+ int err;
29723+ struct path h_path;
29724+ struct super_block *sb;
29725+
29726+ /* lock free root dinfo */
29727+ sb = dentry->d_sb;
29728+ si_noflush_read_lock(sb);
29729+ if (!au_opt_test(au_mntflags(sb), SUM)) {
29730+ /* sb->s_root for NFS is unreliable */
29731+ h_path.mnt = au_sbr_mnt(sb, 0);
29732+ h_path.dentry = h_path.mnt->mnt_root;
29733+ err = vfs_statfs(&h_path, buf);
29734+ } else
29735+ err = au_statfs_sum(sb, buf);
29736+ si_read_unlock(sb);
29737+
29738+ if (!err) {
29739+ buf->f_type = AUFS_SUPER_MAGIC;
29740+ buf->f_namelen = AUFS_MAX_NAMELEN;
29741+ memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
29742+ }
29743+ /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
29744+
29745+ return err;
29746+}
29747+
29748+/* ---------------------------------------------------------------------- */
29749+
29750+static int aufs_sync_fs(struct super_block *sb, int wait)
29751+{
29752+ int err, e;
29753+ aufs_bindex_t bbot, bindex;
29754+ struct au_branch *br;
29755+ struct super_block *h_sb;
29756+
29757+ err = 0;
29758+ si_noflush_read_lock(sb);
29759+ bbot = au_sbbot(sb);
29760+ for (bindex = 0; bindex <= bbot; bindex++) {
29761+ br = au_sbr(sb, bindex);
29762+ if (!au_br_writable(br->br_perm))
29763+ continue;
29764+
29765+ h_sb = au_sbr_sb(sb, bindex);
29766+ e = vfsub_sync_filesystem(h_sb, wait);
29767+ if (unlikely(e && !err))
29768+ err = e;
29769+ /* go on even if an error happens */
29770+ }
29771+ si_read_unlock(sb);
29772+
29773+ return err;
29774+}
29775+
29776+/* ---------------------------------------------------------------------- */
29777+
29778+/* final actions when unmounting a file system */
29779+static void aufs_put_super(struct super_block *sb)
29780+{
29781+ struct au_sbinfo *sbinfo;
29782+
29783+ sbinfo = au_sbi(sb);
29784+ if (sbinfo)
29785+ kobject_put(&sbinfo->si_kobj);
29786+}
29787+
29788+/* ---------------------------------------------------------------------- */
29789+
29790+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
29791+ struct super_block *sb, void *arg)
29792+{
29793+ void *array;
29794+ unsigned long long n, sz;
29795+
29796+ array = NULL;
29797+ n = 0;
29798+ if (!*hint)
29799+ goto out;
29800+
29801+ if (*hint > ULLONG_MAX / sizeof(array)) {
29802+ array = ERR_PTR(-EMFILE);
29803+ pr_err("hint %llu\n", *hint);
29804+ goto out;
29805+ }
29806+
29807+ sz = sizeof(array) * *hint;
29808+ array = kzalloc(sz, GFP_NOFS);
29809+ if (unlikely(!array))
29810+ array = vzalloc(sz);
29811+ if (unlikely(!array)) {
29812+ array = ERR_PTR(-ENOMEM);
29813+ goto out;
29814+ }
29815+
29816+ n = cb(sb, array, *hint, arg);
29817+ AuDebugOn(n > *hint);
29818+
29819+out:
29820+ *hint = n;
29821+ return array;
29822+}
29823+
29824+static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
29825+ unsigned long long max __maybe_unused,
29826+ void *arg)
29827+{
29828+ unsigned long long n;
29829+ struct inode **p, *inode;
29830+ struct list_head *head;
29831+
29832+ n = 0;
29833+ p = a;
29834+ head = arg;
29835+ spin_lock(&sb->s_inode_list_lock);
29836+ list_for_each_entry(inode, head, i_sb_list) {
29837+ if (!au_is_bad_inode(inode)
29838+ && au_ii(inode)->ii_btop >= 0) {
29839+ spin_lock(&inode->i_lock);
29840+ if (atomic_read(&inode->i_count)) {
29841+ au_igrab(inode);
29842+ *p++ = inode;
29843+ n++;
29844+ AuDebugOn(n > max);
29845+ }
29846+ spin_unlock(&inode->i_lock);
29847+ }
29848+ }
29849+ spin_unlock(&sb->s_inode_list_lock);
29850+
29851+ return n;
29852+}
29853+
29854+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
29855+{
29856+ struct au_sbinfo *sbi;
29857+
29858+ sbi = au_sbi(sb);
29859+ *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
29860+ return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
29861+}
29862+
29863+void au_iarray_free(struct inode **a, unsigned long long max)
29864+{
29865+ unsigned long long ull;
29866+
29867+ for (ull = 0; ull < max; ull++)
29868+ iput(a[ull]);
29869+ kvfree(a);
29870+}
29871+
29872+/* ---------------------------------------------------------------------- */
29873+
29874+/*
29875+ * refresh dentry and inode at remount time.
29876+ */
29877+/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
29878+static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
29879+ struct dentry *parent)
29880+{
29881+ int err;
29882+
29883+ di_write_lock_child(dentry);
29884+ di_read_lock_parent(parent, AuLock_IR);
29885+ err = au_refresh_dentry(dentry, parent);
29886+ if (!err && dir_flags)
29887+ au_hn_reset(d_inode(dentry), dir_flags);
29888+ di_read_unlock(parent, AuLock_IR);
29889+ di_write_unlock(dentry);
29890+
29891+ return err;
29892+}
29893+
29894+static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
29895+ struct au_sbinfo *sbinfo,
29896+ const unsigned int dir_flags, unsigned int do_idop)
29897+{
29898+ int err;
29899+ struct dentry *parent;
29900+
29901+ err = 0;
29902+ parent = dget_parent(dentry);
29903+ if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
29904+ if (d_really_is_positive(dentry)) {
29905+ if (!d_is_dir(dentry))
29906+ err = au_do_refresh(dentry, /*dir_flags*/0,
29907+ parent);
29908+ else {
29909+ err = au_do_refresh(dentry, dir_flags, parent);
29910+ if (unlikely(err))
29911+ au_fset_si(sbinfo, FAILED_REFRESH_DIR);
29912+ }
29913+ } else
29914+ err = au_do_refresh(dentry, /*dir_flags*/0, parent);
29915+ AuDbgDentry(dentry);
29916+ }
29917+ dput(parent);
29918+
29919+ if (!err) {
29920+ if (do_idop)
29921+ au_refresh_dop(dentry, /*force_reval*/0);
29922+ } else
29923+ au_refresh_dop(dentry, /*force_reval*/1);
29924+
29925+ AuTraceErr(err);
29926+ return err;
29927+}
29928+
29929+static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
29930+{
29931+ int err, i, j, ndentry, e;
29932+ unsigned int sigen;
29933+ struct au_dcsub_pages dpages;
29934+ struct au_dpage *dpage;
29935+ struct dentry **dentries, *d;
29936+ struct au_sbinfo *sbinfo;
29937+ struct dentry *root = sb->s_root;
29938+ const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
29939+
29940+ if (do_idop)
29941+ au_refresh_dop(root, /*force_reval*/0);
29942+
29943+ err = au_dpages_init(&dpages, GFP_NOFS);
29944+ if (unlikely(err))
29945+ goto out;
29946+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
29947+ if (unlikely(err))
29948+ goto out_dpages;
29949+
29950+ sigen = au_sigen(sb);
29951+ sbinfo = au_sbi(sb);
29952+ for (i = 0; i < dpages.ndpage; i++) {
29953+ dpage = dpages.dpages + i;
29954+ dentries = dpage->dentries;
29955+ ndentry = dpage->ndentry;
29956+ for (j = 0; j < ndentry; j++) {
29957+ d = dentries[j];
29958+ e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
29959+ do_idop);
29960+ if (unlikely(e && !err))
29961+ err = e;
29962+ /* go on even err */
29963+ }
29964+ }
29965+
29966+out_dpages:
29967+ au_dpages_free(&dpages);
29968+out:
29969+ return err;
29970+}
29971+
29972+static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
29973+{
29974+ int err, e;
29975+ unsigned int sigen;
29976+ unsigned long long max, ull;
29977+ struct inode *inode, **array;
29978+
29979+ array = au_iarray_alloc(sb, &max);
29980+ err = PTR_ERR(array);
29981+ if (IS_ERR(array))
29982+ goto out;
29983+
29984+ err = 0;
29985+ sigen = au_sigen(sb);
29986+ for (ull = 0; ull < max; ull++) {
29987+ inode = array[ull];
29988+ if (unlikely(!inode))
29989+ break;
29990+
29991+ e = 0;
29992+ ii_write_lock_child(inode);
29993+ if (au_iigen(inode, NULL) != sigen) {
29994+ e = au_refresh_hinode_self(inode);
29995+ if (unlikely(e)) {
29996+ au_refresh_iop(inode, /*force_getattr*/1);
29997+ pr_err("error %d, i%lu\n", e, inode->i_ino);
29998+ if (!err)
29999+ err = e;
30000+ /* go on even if err */
30001+ }
30002+ }
30003+ if (!e && do_idop)
30004+ au_refresh_iop(inode, /*force_getattr*/0);
30005+ ii_write_unlock(inode);
30006+ }
30007+
30008+ au_iarray_free(array, max);
30009+
30010+out:
30011+ return err;
30012+}
30013+
30014+static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
30015+{
30016+ int err, e;
30017+ unsigned int udba;
30018+ aufs_bindex_t bindex, bbot;
30019+ struct dentry *root;
30020+ struct inode *inode;
30021+ struct au_branch *br;
30022+ struct au_sbinfo *sbi;
30023+
30024+ au_sigen_inc(sb);
30025+ sbi = au_sbi(sb);
30026+ au_fclr_si(sbi, FAILED_REFRESH_DIR);
30027+
30028+ root = sb->s_root;
30029+ DiMustNoWaiters(root);
30030+ inode = d_inode(root);
30031+ IiMustNoWaiters(inode);
30032+
30033+ udba = au_opt_udba(sb);
30034+ bbot = au_sbbot(sb);
30035+ for (bindex = 0; bindex <= bbot; bindex++) {
30036+ br = au_sbr(sb, bindex);
30037+ err = au_hnotify_reset_br(udba, br, br->br_perm);
30038+ if (unlikely(err))
30039+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
30040+ bindex, err);
30041+ /* go on even if err */
30042+ }
30043+ au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
30044+
30045+ if (do_idop) {
30046+ if (au_ftest_si(sbi, NO_DREVAL)) {
30047+ AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30048+ sb->s_d_op = &aufs_dop_noreval;
30049+ AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30050+ sbi->si_iop_array = aufs_iop_nogetattr;
30051+ } else {
30052+ AuDebugOn(sb->s_d_op == &aufs_dop);
30053+ sb->s_d_op = &aufs_dop;
30054+ AuDebugOn(sbi->si_iop_array == aufs_iop);
30055+ sbi->si_iop_array = aufs_iop;
30056+ }
30057+ pr_info("reset to %ps and %ps\n",
30058+ sb->s_d_op, sbi->si_iop_array);
30059+ }
30060+
30061+ di_write_unlock(root);
30062+ err = au_refresh_d(sb, do_idop);
30063+ e = au_refresh_i(sb, do_idop);
30064+ if (unlikely(e && !err))
30065+ err = e;
30066+ /* aufs_write_lock() calls ..._child() */
30067+ di_write_lock_child(root);
30068+
30069+ au_cpup_attr_all(inode, /*force*/1);
30070+
30071+ if (unlikely(err))
30072+ AuIOErr("refresh failed, ignored, %d\n", err);
30073+}
30074+
30075+/* stop extra interpretation of errno in mount(8), and strange error messages */
30076+static int cvt_err(int err)
30077+{
30078+ AuTraceErr(err);
30079+
30080+ switch (err) {
30081+ case -ENOENT:
30082+ case -ENOTDIR:
30083+ case -EEXIST:
30084+ case -EIO:
30085+ err = -EINVAL;
30086+ }
30087+ return err;
30088+}
30089+
30090+static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
30091+{
30092+ int err, do_dx;
30093+ unsigned int mntflags;
30094+ struct au_opts opts = {
30095+ .opt = NULL
30096+ };
30097+ struct dentry *root;
30098+ struct inode *inode;
30099+ struct au_sbinfo *sbinfo;
30100+
30101+ err = 0;
30102+ root = sb->s_root;
30103+ if (!data || !*data) {
30104+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30105+ if (!err) {
30106+ di_write_lock_child(root);
30107+ err = au_opts_verify(sb, *flags, /*pending*/0);
30108+ aufs_write_unlock(root);
30109+ }
30110+ goto out;
30111+ }
30112+
30113+ err = -ENOMEM;
30114+ opts.opt = (void *)__get_free_page(GFP_NOFS);
30115+ if (unlikely(!opts.opt))
30116+ goto out;
30117+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30118+ opts.flags = AuOpts_REMOUNT;
30119+ opts.sb_flags = *flags;
30120+
30121+ /* parse it before aufs lock */
30122+ err = au_opts_parse(sb, data, &opts);
30123+ if (unlikely(err))
30124+ goto out_opts;
30125+
30126+ sbinfo = au_sbi(sb);
30127+ inode = d_inode(root);
30128+ inode_lock(inode);
30129+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30130+ if (unlikely(err))
30131+ goto out_mtx;
30132+ di_write_lock_child(root);
30133+
30134+ /* au_opts_remount() may return an error */
30135+ err = au_opts_remount(sb, &opts);
30136+ au_opts_free(&opts);
30137+
30138+ if (au_ftest_opts(opts.flags, REFRESH))
30139+ au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
30140+
30141+ if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
30142+ mntflags = au_mntflags(sb);
30143+ do_dx = !!au_opt_test(mntflags, DIO);
30144+ au_dy_arefresh(do_dx);
30145+ }
30146+
30147+ au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
30148+ aufs_write_unlock(root);
30149+
30150+out_mtx:
30151+ inode_unlock(inode);
30152+out_opts:
30153+ free_page((unsigned long)opts.opt);
30154+out:
30155+ err = cvt_err(err);
30156+ AuTraceErr(err);
30157+ return err;
30158+}
30159+
30160+static const struct super_operations aufs_sop = {
30161+ .alloc_inode = aufs_alloc_inode,
30162+ .destroy_inode = aufs_destroy_inode,
30163+ .free_inode = aufs_free_inode,
30164+ /* always deleting, no clearing */
30165+ .drop_inode = generic_delete_inode,
30166+ .show_options = aufs_show_options,
30167+ .statfs = aufs_statfs,
30168+ .put_super = aufs_put_super,
30169+ .sync_fs = aufs_sync_fs,
30170+ .remount_fs = aufs_remount_fs
30171+};
30172+
30173+/* ---------------------------------------------------------------------- */
30174+
30175+static int alloc_root(struct super_block *sb)
30176+{
30177+ int err;
30178+ struct inode *inode;
30179+ struct dentry *root;
30180+
30181+ err = -ENOMEM;
30182+ inode = au_iget_locked(sb, AUFS_ROOT_INO);
30183+ err = PTR_ERR(inode);
30184+ if (IS_ERR(inode))
30185+ goto out;
30186+
30187+ inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
30188+ inode->i_fop = &aufs_dir_fop;
30189+ inode->i_mode = S_IFDIR;
30190+ set_nlink(inode, 2);
30191+ unlock_new_inode(inode);
30192+
30193+ root = d_make_root(inode);
30194+ if (unlikely(!root))
30195+ goto out;
30196+ err = PTR_ERR(root);
30197+ if (IS_ERR(root))
30198+ goto out;
30199+
30200+ err = au_di_init(root);
30201+ if (!err) {
30202+ sb->s_root = root;
30203+ return 0; /* success */
30204+ }
30205+ dput(root);
30206+
30207+out:
30208+ return err;
30209+}
30210+
30211+static int aufs_fill_super(struct super_block *sb, void *raw_data,
30212+ int silent __maybe_unused)
30213+{
30214+ int err;
30215+ struct au_opts opts = {
30216+ .opt = NULL
30217+ };
30218+ struct au_sbinfo *sbinfo;
30219+ struct dentry *root;
30220+ struct inode *inode;
30221+ char *arg = raw_data;
30222+
30223+ if (unlikely(!arg || !*arg)) {
30224+ err = -EINVAL;
30225+ pr_err("no arg\n");
30226+ goto out;
30227+ }
30228+
30229+ err = -ENOMEM;
30230+ opts.opt = (void *)__get_free_page(GFP_NOFS);
30231+ if (unlikely(!opts.opt))
30232+ goto out;
30233+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30234+ opts.sb_flags = sb->s_flags;
30235+
30236+ err = au_si_alloc(sb);
30237+ if (unlikely(err))
30238+ goto out_opts;
30239+ sbinfo = au_sbi(sb);
30240+
30241+ /* all timestamps always follow the ones on the branch */
30242+ sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
30243+ sb->s_flags |= SB_I_VERSION; /* do we really need this? */
30244+ sb->s_op = &aufs_sop;
30245+ sb->s_d_op = &aufs_dop;
30246+ sb->s_magic = AUFS_SUPER_MAGIC;
30247+ sb->s_maxbytes = 0;
30248+ sb->s_stack_depth = 1;
30249+ au_export_init(sb);
30250+ au_xattr_init(sb);
30251+
30252+ err = alloc_root(sb);
30253+ if (unlikely(err)) {
30254+ si_write_unlock(sb);
30255+ goto out_info;
30256+ }
30257+ root = sb->s_root;
30258+ inode = d_inode(root);
30259+
30260+ /*
30261+ * actually we can parse options regardless aufs lock here.
30262+ * but at remount time, parsing must be done before aufs lock.
30263+ * so we follow the same rule.
30264+ */
30265+ ii_write_lock_parent(inode);
30266+ aufs_write_unlock(root);
30267+ err = au_opts_parse(sb, arg, &opts);
30268+ if (unlikely(err))
30269+ goto out_root;
30270+
30271+ /* lock vfs_inode first, then aufs. */
30272+ inode_lock(inode);
30273+ aufs_write_lock(root);
30274+ err = au_opts_mount(sb, &opts);
30275+ au_opts_free(&opts);
30276+ if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
30277+ sb->s_d_op = &aufs_dop_noreval;
30278+ pr_info("%ps\n", sb->s_d_op);
30279+ au_refresh_dop(root, /*force_reval*/0);
30280+ sbinfo->si_iop_array = aufs_iop_nogetattr;
30281+ au_refresh_iop(inode, /*force_getattr*/0);
30282+ }
30283+ aufs_write_unlock(root);
30284+ inode_unlock(inode);
30285+ if (!err)
30286+ goto out_opts; /* success */
30287+
30288+out_root:
30289+ dput(root);
30290+ sb->s_root = NULL;
30291+out_info:
30292+ kobject_put(&sbinfo->si_kobj);
30293+ sb->s_fs_info = NULL;
30294+out_opts:
30295+ free_page((unsigned long)opts.opt);
30296+out:
30297+ AuTraceErr(err);
30298+ err = cvt_err(err);
30299+ AuTraceErr(err);
30300+ return err;
30301+}
30302+
30303+/* ---------------------------------------------------------------------- */
30304+
30305+static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
30306+ const char *dev_name __maybe_unused,
30307+ void *raw_data)
30308+{
30309+ struct dentry *root;
30310+
30311+ /* all timestamps always follow the ones on the branch */
30312+ /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
30313+ root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
30314+ if (IS_ERR(root))
30315+ goto out;
30316+
30317+ au_sbilist_add(root->d_sb);
30318+
30319+out:
30320+ return root;
30321+}
30322+
30323+static void aufs_kill_sb(struct super_block *sb)
30324+{
30325+ struct au_sbinfo *sbinfo;
30326+
30327+ sbinfo = au_sbi(sb);
30328+ if (sbinfo) {
30329+ au_sbilist_del(sb);
30330+ aufs_write_lock(sb->s_root);
30331+ au_fhsm_fin(sb);
30332+ if (sbinfo->si_wbr_create_ops->fin)
30333+ sbinfo->si_wbr_create_ops->fin(sb);
30334+ if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30335+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
30336+ au_remount_refresh(sb, /*do_idop*/0);
30337+ }
30338+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
30339+ au_plink_put(sb, /*verbose*/1);
30340+ au_xino_clr(sb);
30341+ au_dr_opt_flush(sb);
30342+ sbinfo->si_sb = NULL;
30343+ aufs_write_unlock(sb->s_root);
30344+ au_nwt_flush(&sbinfo->si_nowait);
30345+ }
30346+ kill_anon_super(sb);
30347+}
30348+
30349+struct file_system_type aufs_fs_type = {
30350+ .name = AUFS_FSTYPE,
30351+ /* a race between rename and others */
30352+ .fs_flags = FS_RENAME_DOES_D_MOVE,
30353+ .mount = aufs_mount,
30354+ .kill_sb = aufs_kill_sb,
30355+ /* no need to __module_get() and module_put(). */
30356+ .owner = THIS_MODULE,
30357+};
30358diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30359--- /usr/share/empty/fs/aufs/super.h 1970-01-01 01:00:00.000000000 +0100
30360+++ linux/fs/aufs/super.h 2021-02-22 22:51:59.061411920 +0100
30361@@ -0,0 +1,587 @@
30362+/* SPDX-License-Identifier: GPL-2.0 */
30363+/*
30364+ * Copyright (C) 2005-2020 Junjiro R. Okajima
30365+ *
30366+ * This program, aufs is free software; you can redistribute it and/or modify
30367+ * it under the terms of the GNU General Public License as published by
30368+ * the Free Software Foundation; either version 2 of the License, or
30369+ * (at your option) any later version.
30370+ *
30371+ * This program is distributed in the hope that it will be useful,
30372+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30373+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30374+ * GNU General Public License for more details.
30375+ *
30376+ * You should have received a copy of the GNU General Public License
30377+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
30378+ */
30379+
30380+/*
30381+ * super_block operations
30382+ */
30383+
30384+#ifndef __AUFS_SUPER_H__
30385+#define __AUFS_SUPER_H__
30386+
30387+#ifdef __KERNEL__
30388+
30389+#include <linux/fs.h>
30390+#include <linux/kobject.h>
30391+#include "hbl.h"
30392+#include "lcnt.h"
30393+#include "rwsem.h"
30394+#include "wkq.h"
30395+
30396+/* policies to select one among multiple writable branches */
30397+struct au_wbr_copyup_operations {
30398+ int (*copyup)(struct dentry *dentry);
30399+};
30400+
30401+#define AuWbr_DIR 1 /* target is a dir */
30402+#define AuWbr_PARENT (1 << 1) /* always require a parent */
30403+
30404+#define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
30405+#define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
30406+#define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
30407+
30408+struct au_wbr_create_operations {
30409+ int (*create)(struct dentry *dentry, unsigned int flags);
30410+ int (*init)(struct super_block *sb);
30411+ int (*fin)(struct super_block *sb);
30412+};
30413+
30414+struct au_wbr_mfs {
30415+ struct mutex mfs_lock; /* protect this structure */
30416+ unsigned long mfs_jiffy;
30417+ unsigned long mfs_expire;
30418+ aufs_bindex_t mfs_bindex;
30419+
30420+ unsigned long long mfsrr_bytes;
30421+ unsigned long long mfsrr_watermark;
30422+};
30423+
30424+#define AuPlink_NHASH 100
30425+static inline int au_plink_hash(ino_t ino)
30426+{
30427+ return ino % AuPlink_NHASH;
30428+}
30429+
30430+/* File-based Hierarchical Storage Management */
30431+struct au_fhsm {
30432+#ifdef CONFIG_AUFS_FHSM
30433+ /* allow only one process who can receive the notification */
30434+ spinlock_t fhsm_spin;
30435+ pid_t fhsm_pid;
30436+ wait_queue_head_t fhsm_wqh;
30437+ atomic_t fhsm_readable;
30438+
30439+ /* these are protected by si_rwsem */
30440+ unsigned long fhsm_expire;
30441+ aufs_bindex_t fhsm_bottom;
30442+#endif
30443+};
30444+
30445+struct au_branch;
30446+struct au_sbinfo {
30447+ /* nowait tasks in the system-wide workqueue */
30448+ struct au_nowait_tasks si_nowait;
30449+
30450+ /*
30451+ * tried sb->s_umount, but failed due to the dependency between i_mutex.
30452+ * rwsem for au_sbinfo is necessary.
30453+ */
30454+ struct au_rwsem si_rwsem;
30455+
30456+ /*
30457+ * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30458+ * remount.
30459+ */
30460+ au_lcnt_t si_ninodes, si_nfiles;
30461+
30462+ /* branch management */
30463+ unsigned int si_generation;
30464+
30465+ /* see AuSi_ flags */
30466+ unsigned char au_si_status;
30467+
30468+ aufs_bindex_t si_bbot;
30469+
30470+ /* dirty trick to keep br_id plus */
30471+ unsigned int si_last_br_id :
30472+ sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
30473+ struct au_branch **si_branch;
30474+
30475+ /* policy to select a writable branch */
30476+ unsigned char si_wbr_copyup;
30477+ unsigned char si_wbr_create;
30478+ struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30479+ struct au_wbr_create_operations *si_wbr_create_ops;
30480+
30481+ /* round robin */
30482+ atomic_t si_wbr_rr_next;
30483+
30484+ /* most free space */
30485+ struct au_wbr_mfs si_wbr_mfs;
30486+
30487+ /* File-based Hierarchical Storage Management */
30488+ struct au_fhsm si_fhsm;
30489+
30490+ /* mount flags */
30491+ /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30492+ unsigned int si_mntflags;
30493+
30494+ /* external inode number (bitmap and translation table) */
30495+ loff_t si_ximaxent; /* max entries in a xino */
30496+
30497+ struct file *si_xib;
30498+ struct mutex si_xib_mtx; /* protect xib members */
30499+ unsigned long *si_xib_buf;
30500+ unsigned long si_xib_last_pindex;
30501+ int si_xib_next_bit;
30502+
30503+ unsigned long si_xino_jiffy;
30504+ unsigned long si_xino_expire;
30505+ /* reserved for future use */
30506+ /* unsigned long long si_xib_limit; */ /* Max xib file size */
30507+
30508+#ifdef CONFIG_AUFS_EXPORT
30509+ /* i_generation */
30510+ /* todo: make xigen file an array to support many inode numbers */
30511+ struct file *si_xigen;
30512+ atomic_t si_xigen_next;
30513+#endif
30514+
30515+ /* dirty trick to support atomic_open */
30516+ struct hlist_bl_head si_aopen;
30517+
30518+ /* vdir parameters */
30519+ unsigned long si_rdcache; /* max cache time in jiffies */
30520+ unsigned int si_rdblk; /* deblk size */
30521+ unsigned int si_rdhash; /* hash size */
30522+
30523+ /*
30524+ * If the number of whiteouts are larger than si_dirwh, leave all of
30525+ * them after au_whtmp_ren to reduce the cost of rmdir(2).
30526+ * future fsck.aufs or kernel thread will remove them later.
30527+ * Otherwise, remove all whiteouts and the dir in rmdir(2).
30528+ */
30529+ unsigned int si_dirwh;
30530+
30531+ /* pseudo_link list */
30532+ struct hlist_bl_head si_plink[AuPlink_NHASH];
30533+ wait_queue_head_t si_plink_wq;
30534+ spinlock_t si_plink_maint_lock;
30535+ pid_t si_plink_maint_pid;
30536+
30537+ /* file list */
30538+ struct hlist_bl_head si_files;
30539+
30540+ /* with/without getattr, brother of sb->s_d_op */
30541+ const struct inode_operations *si_iop_array;
30542+
30543+ /*
30544+ * sysfs and lifetime management.
30545+ * this is not a small structure and it may be a waste of memory in case
30546+ * of sysfs is disabled, particularly when many aufs-es are mounted.
30547+ * but using sysfs is majority.
30548+ */
30549+ struct kobject si_kobj;
30550+#ifdef CONFIG_DEBUG_FS
30551+ struct dentry *si_dbgaufs;
30552+ struct dentry *si_dbgaufs_plink;
30553+ struct dentry *si_dbgaufs_xib;
30554+#ifdef CONFIG_AUFS_EXPORT
30555+ struct dentry *si_dbgaufs_xigen;
30556+#endif
30557+#endif
30558+
30559+#ifdef CONFIG_AUFS_SBILIST
30560+ struct hlist_bl_node si_list;
30561+#endif
30562+
30563+ /* dirty, necessary for unmounting, sysfs and sysrq */
30564+ struct super_block *si_sb;
30565+};
30566+
30567+/* sbinfo status flags */
30568+/*
30569+ * set true when refresh_dirs() failed at remount time.
30570+ * then try refreshing dirs at access time again.
30571+ * if it is false, refreshing dirs at access time is unnecessary
30572+ */
30573+#define AuSi_FAILED_REFRESH_DIR 1
30574+#define AuSi_FHSM (1 << 1) /* fhsm is active now */
30575+#define AuSi_NO_DREVAL (1 << 2) /* disable all d_revalidate */
30576+
30577+#ifndef CONFIG_AUFS_FHSM
30578+#undef AuSi_FHSM
30579+#define AuSi_FHSM 0
30580+#endif
30581+
30582+static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30583+ unsigned int flag)
30584+{
30585+ AuRwMustAnyLock(&sbi->si_rwsem);
30586+ return sbi->au_si_status & flag;
30587+}
30588+#define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
30589+#define au_fset_si(sbinfo, name) do { \
30590+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30591+ (sbinfo)->au_si_status |= AuSi_##name; \
30592+} while (0)
30593+#define au_fclr_si(sbinfo, name) do { \
30594+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30595+ (sbinfo)->au_si_status &= ~AuSi_##name; \
30596+} while (0)
30597+
30598+/* ---------------------------------------------------------------------- */
30599+
30600+/* policy to select one among writable branches */
30601+#define AuWbrCopyup(sbinfo, ...) \
30602+ ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30603+#define AuWbrCreate(sbinfo, ...) \
30604+ ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
30605+
30606+/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30607+#define AuLock_DW 1 /* write-lock dentry */
30608+#define AuLock_IR (1 << 1) /* read-lock inode */
30609+#define AuLock_IW (1 << 2) /* write-lock inode */
30610+#define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
30611+#define AuLock_DIRS (1 << 4) /* target is a pair of dirs */
30612+ /* except RENAME_EXCHANGE */
30613+#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
30614+#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
30615+#define AuLock_GEN (1 << 7) /* test digen/iigen */
30616+#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
30617+#define au_fset_lock(flags, name) \
30618+ do { (flags) |= AuLock_##name; } while (0)
30619+#define au_fclr_lock(flags, name) \
30620+ do { (flags) &= ~AuLock_##name; } while (0)
30621+
30622+/* ---------------------------------------------------------------------- */
30623+
30624+/* super.c */
30625+extern struct file_system_type aufs_fs_type;
30626+struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
30627+typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30628+ unsigned long long max, void *arg);
30629+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30630+ struct super_block *sb, void *arg);
30631+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30632+void au_iarray_free(struct inode **a, unsigned long long max);
30633+
30634+/* sbinfo.c */
30635+void au_si_free(struct kobject *kobj);
30636+int au_si_alloc(struct super_block *sb);
30637+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
30638+
30639+unsigned int au_sigen_inc(struct super_block *sb);
30640+aufs_bindex_t au_new_br_id(struct super_block *sb);
30641+
30642+int si_read_lock(struct super_block *sb, int flags);
30643+int si_write_lock(struct super_block *sb, int flags);
30644+int aufs_read_lock(struct dentry *dentry, int flags);
30645+void aufs_read_unlock(struct dentry *dentry, int flags);
30646+void aufs_write_lock(struct dentry *dentry);
30647+void aufs_write_unlock(struct dentry *dentry);
30648+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
30649+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30650+
30651+/* wbr_policy.c */
30652+extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30653+extern struct au_wbr_create_operations au_wbr_create_ops[];
30654+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
30655+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
30656+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
30657+
30658+/* mvdown.c */
30659+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
30660+
30661+#ifdef CONFIG_AUFS_FHSM
30662+/* fhsm.c */
30663+
30664+static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30665+{
30666+ pid_t pid;
30667+
30668+ spin_lock(&fhsm->fhsm_spin);
30669+ pid = fhsm->fhsm_pid;
30670+ spin_unlock(&fhsm->fhsm_spin);
30671+
30672+ return pid;
30673+}
30674+
30675+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
30676+void au_fhsm_wrote_all(struct super_block *sb, int force);
30677+int au_fhsm_fd(struct super_block *sb, int oflags);
30678+int au_fhsm_br_alloc(struct au_branch *br);
30679+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
30680+void au_fhsm_fin(struct super_block *sb);
30681+void au_fhsm_init(struct au_sbinfo *sbinfo);
30682+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
30683+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
30684+#else
30685+AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
30686+ int force)
30687+AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
30688+AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
30689+AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
30690+AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
30691+AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
30692+AuStubVoid(au_fhsm_fin, struct super_block *sb)
30693+AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
30694+AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
30695+AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
30696+#endif
30697+
30698+/* ---------------------------------------------------------------------- */
30699+
30700+static inline struct au_sbinfo *au_sbi(struct super_block *sb)
30701+{
30702+ return sb->s_fs_info;
30703+}
30704+
30705+/* ---------------------------------------------------------------------- */
30706+
30707+#ifdef CONFIG_AUFS_EXPORT
30708+int au_test_nfsd(void);
30709+void au_export_init(struct super_block *sb);
30710+void au_xigen_inc(struct inode *inode);
30711+int au_xigen_new(struct inode *inode);
30712+int au_xigen_set(struct super_block *sb, struct path *path);
30713+void au_xigen_clr(struct super_block *sb);
30714+
30715+static inline int au_busy_or_stale(void)
30716+{
30717+ if (!au_test_nfsd())
30718+ return -EBUSY;
30719+ return -ESTALE;
30720+}
30721+#else
30722+AuStubInt0(au_test_nfsd, void)
30723+AuStubVoid(au_export_init, struct super_block *sb)
30724+AuStubVoid(au_xigen_inc, struct inode *inode)
30725+AuStubInt0(au_xigen_new, struct inode *inode)
30726+AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
30727+AuStubVoid(au_xigen_clr, struct super_block *sb)
30728+AuStub(int, au_busy_or_stale, return -EBUSY, void)
30729+#endif /* CONFIG_AUFS_EXPORT */
30730+
30731+/* ---------------------------------------------------------------------- */
30732+
30733+#ifdef CONFIG_AUFS_SBILIST
30734+/* module.c */
30735+extern struct hlist_bl_head au_sbilist;
30736+
30737+static inline void au_sbilist_init(void)
30738+{
30739+ INIT_HLIST_BL_HEAD(&au_sbilist);
30740+}
30741+
30742+static inline void au_sbilist_add(struct super_block *sb)
30743+{
30744+ au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
30745+}
30746+
30747+static inline void au_sbilist_del(struct super_block *sb)
30748+{
30749+ au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
30750+}
30751+
30752+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
30753+static inline void au_sbilist_lock(void)
30754+{
30755+ hlist_bl_lock(&au_sbilist);
30756+}
30757+
30758+static inline void au_sbilist_unlock(void)
30759+{
30760+ hlist_bl_unlock(&au_sbilist);
30761+}
30762+#define AuGFP_SBILIST GFP_ATOMIC
30763+#else
30764+AuStubVoid(au_sbilist_lock, void)
30765+AuStubVoid(au_sbilist_unlock, void)
30766+#define AuGFP_SBILIST GFP_NOFS
30767+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
30768+#else
30769+AuStubVoid(au_sbilist_init, void)
30770+AuStubVoid(au_sbilist_add, struct super_block *sb)
30771+AuStubVoid(au_sbilist_del, struct super_block *sb)
30772+AuStubVoid(au_sbilist_lock, void)
30773+AuStubVoid(au_sbilist_unlock, void)
30774+#define AuGFP_SBILIST GFP_NOFS
30775+#endif
30776+
30777+/* ---------------------------------------------------------------------- */
30778+
30779+static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
30780+{
30781+ /*
30782+ * This function is a dynamic '__init' function actually,
30783+ * so the tiny check for si_rwsem is unnecessary.
30784+ */
30785+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
30786+#ifdef CONFIG_DEBUG_FS
30787+ sbinfo->si_dbgaufs = NULL;
30788+ sbinfo->si_dbgaufs_plink = NULL;
30789+ sbinfo->si_dbgaufs_xib = NULL;
30790+#ifdef CONFIG_AUFS_EXPORT
30791+ sbinfo->si_dbgaufs_xigen = NULL;
30792+#endif
30793+#endif
30794+}
30795+
30796+/* ---------------------------------------------------------------------- */
30797+
30798+/* current->atomic_flags */
30799+/* this value should never corrupt the ones defined in linux/sched.h */
30800+#define PFA_AUFS 0x10
30801+
30802+TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
30803+TASK_PFA_SET(AUFS, aufs) /* task_set_aufs */
30804+TASK_PFA_CLEAR(AUFS, aufs) /* task_clear_aufs */
30805+
30806+static inline int si_pid_test(struct super_block *sb)
30807+{
30808+ return !!task_test_aufs(current);
30809+}
30810+
30811+static inline void si_pid_clr(struct super_block *sb)
30812+{
30813+ AuDebugOn(!task_test_aufs(current));
30814+ task_clear_aufs(current);
30815+}
30816+
30817+static inline void si_pid_set(struct super_block *sb)
30818+{
30819+ AuDebugOn(task_test_aufs(current));
30820+ task_set_aufs(current);
30821+}
30822+
30823+/* ---------------------------------------------------------------------- */
30824+
30825+/* lock superblock. mainly for entry point functions */
30826+#define __si_read_lock(sb) au_rw_read_lock(&au_sbi(sb)->si_rwsem)
30827+#define __si_write_lock(sb) au_rw_write_lock(&au_sbi(sb)->si_rwsem)
30828+#define __si_read_trylock(sb) au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
30829+#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
30830+/*
30831+#define __si_read_trylock_nested(sb) \
30832+ au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
30833+#define __si_write_trylock_nested(sb) \
30834+ au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
30835+*/
30836+
30837+#define __si_read_unlock(sb) au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
30838+#define __si_write_unlock(sb) au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
30839+#define __si_downgrade_lock(sb) au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
30840+
30841+#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
30842+#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
30843+#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
30844+
30845+static inline void si_noflush_read_lock(struct super_block *sb)
30846+{
30847+ __si_read_lock(sb);
30848+ si_pid_set(sb);
30849+}
30850+
30851+static inline int si_noflush_read_trylock(struct super_block *sb)
30852+{
30853+ int locked;
30854+
30855+ locked = __si_read_trylock(sb);
30856+ if (locked)
30857+ si_pid_set(sb);
30858+ return locked;
30859+}
30860+
30861+static inline void si_noflush_write_lock(struct super_block *sb)
30862+{
30863+ __si_write_lock(sb);
30864+ si_pid_set(sb);
30865+}
30866+
30867+static inline int si_noflush_write_trylock(struct super_block *sb)
30868+{
30869+ int locked;
30870+
30871+ locked = __si_write_trylock(sb);
30872+ if (locked)
30873+ si_pid_set(sb);
30874+ return locked;
30875+}
30876+
30877+#if 0 /* reserved */
30878+static inline int si_read_trylock(struct super_block *sb, int flags)
30879+{
30880+ if (au_ftest_lock(flags, FLUSH))
30881+ au_nwt_flush(&au_sbi(sb)->si_nowait);
30882+ return si_noflush_read_trylock(sb);
30883+}
30884+#endif
30885+
30886+static inline void si_read_unlock(struct super_block *sb)
30887+{
30888+ si_pid_clr(sb);
30889+ __si_read_unlock(sb);
30890+}
30891+
30892+#if 0 /* reserved */
30893+static inline int si_write_trylock(struct super_block *sb, int flags)
30894+{
30895+ if (au_ftest_lock(flags, FLUSH))
30896+ au_nwt_flush(&au_sbi(sb)->si_nowait);
30897+ return si_noflush_write_trylock(sb);
30898+}
30899+#endif
30900+
30901+static inline void si_write_unlock(struct super_block *sb)
30902+{
30903+ si_pid_clr(sb);
30904+ __si_write_unlock(sb);
30905+}
30906+
30907+#if 0 /* reserved */
30908+static inline void si_downgrade_lock(struct super_block *sb)
30909+{
30910+ __si_downgrade_lock(sb);
30911+}
30912+#endif
30913+
30914+/* ---------------------------------------------------------------------- */
30915+
30916+static inline aufs_bindex_t au_sbbot(struct super_block *sb)
30917+{
30918+ SiMustAnyLock(sb);
30919+ return au_sbi(sb)->si_bbot;
30920+}
30921+
30922+static inline unsigned int au_mntflags(struct super_block *sb)
30923+{
30924+ SiMustAnyLock(sb);
30925+ return au_sbi(sb)->si_mntflags;
30926+}
30927+
30928+static inline unsigned int au_sigen(struct super_block *sb)
30929+{
30930+ SiMustAnyLock(sb);
30931+ return au_sbi(sb)->si_generation;
30932+}
30933+
30934+static inline struct au_branch *au_sbr(struct super_block *sb,
30935+ aufs_bindex_t bindex)
30936+{
30937+ SiMustAnyLock(sb);
30938+ return au_sbi(sb)->si_branch[0 + bindex];
30939+}
30940+
30941+static inline loff_t au_xi_maxent(struct super_block *sb)
30942+{
30943+ SiMustAnyLock(sb);
30944+ return au_sbi(sb)->si_ximaxent;
30945+}
30946+
30947+#endif /* __KERNEL__ */
30948+#endif /* __AUFS_SUPER_H__ */
30949diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
30950--- /usr/share/empty/fs/aufs/sysaufs.c 1970-01-01 01:00:00.000000000 +0100
30951+++ linux/fs/aufs/sysaufs.c 2021-02-22 22:51:59.061411920 +0100
30952@@ -0,0 +1,93 @@
30953+// SPDX-License-Identifier: GPL-2.0
30954+/*
30955+ * Copyright (C) 2005-2020 Junjiro R. Okajima
30956+ *
30957+ * This program, aufs is free software; you can redistribute it and/or modify
30958+ * it under the terms of the GNU General Public License as published by
30959+ * the Free Software Foundation; either version 2 of the License, or
30960+ * (at your option) any later version.
30961+ *
30962+ * This program is distributed in the hope that it will be useful,
30963+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30964+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30965+ * GNU General Public License for more details.
30966+ *
30967+ * You should have received a copy of the GNU General Public License
30968+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
30969+ */
30970+
30971+/*
30972+ * sysfs interface and lifetime management
30973+ * they are necessary regardless sysfs is disabled.
30974+ */
30975+
30976+#include <linux/random.h>
30977+#include "aufs.h"
30978+
30979+unsigned long sysaufs_si_mask;
30980+struct kset *sysaufs_kset;
30981+
30982+#define AuSiAttr(_name) { \
30983+ .attr = { .name = __stringify(_name), .mode = 0444 }, \
30984+ .show = sysaufs_si_##_name, \
30985+}
30986+
30987+static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
30988+struct attribute *sysaufs_si_attrs[] = {
30989+ &sysaufs_si_attr_xi_path.attr,
30990+ NULL,
30991+};
30992+
30993+static const struct sysfs_ops au_sbi_ops = {
30994+ .show = sysaufs_si_show
30995+};
30996+
30997+static struct kobj_type au_sbi_ktype = {
30998+ .release = au_si_free,
30999+ .sysfs_ops = &au_sbi_ops,
31000+ .default_attrs = sysaufs_si_attrs
31001+};
31002+
31003+/* ---------------------------------------------------------------------- */
31004+
31005+int sysaufs_si_init(struct au_sbinfo *sbinfo)
31006+{
31007+ int err;
31008+
31009+ sbinfo->si_kobj.kset = sysaufs_kset;
31010+ /* cf. sysaufs_name() */
31011+ err = kobject_init_and_add
31012+ (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
31013+ SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31014+
31015+ return err;
31016+}
31017+
31018+void sysaufs_fin(void)
31019+{
31020+ sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31021+ kset_unregister(sysaufs_kset);
31022+}
31023+
31024+int __init sysaufs_init(void)
31025+{
31026+ int err;
31027+
31028+ do {
31029+ get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31030+ } while (!sysaufs_si_mask);
31031+
31032+ err = -EINVAL;
31033+ sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31034+ if (unlikely(!sysaufs_kset))
31035+ goto out;
31036+ err = PTR_ERR(sysaufs_kset);
31037+ if (IS_ERR(sysaufs_kset))
31038+ goto out;
31039+ err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31040+ if (unlikely(err))
31041+ kset_unregister(sysaufs_kset);
31042+
31043+out:
31044+ return err;
31045+}
31046diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
31047--- /usr/share/empty/fs/aufs/sysaufs.h 1970-01-01 01:00:00.000000000 +0100
31048+++ linux/fs/aufs/sysaufs.h 2021-02-22 22:51:59.061411920 +0100
31049@@ -0,0 +1,102 @@
31050+/* SPDX-License-Identifier: GPL-2.0 */
31051+/*
31052+ * Copyright (C) 2005-2020 Junjiro R. Okajima
31053+ *
31054+ * This program, aufs is free software; you can redistribute it and/or modify
31055+ * it under the terms of the GNU General Public License as published by
31056+ * the Free Software Foundation; either version 2 of the License, or
31057+ * (at your option) any later version.
31058+ *
31059+ * This program is distributed in the hope that it will be useful,
31060+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31061+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31062+ * GNU General Public License for more details.
31063+ *
31064+ * You should have received a copy of the GNU General Public License
31065+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
31066+ */
31067+
31068+/*
31069+ * sysfs interface and mount lifetime management
31070+ */
31071+
31072+#ifndef __SYSAUFS_H__
31073+#define __SYSAUFS_H__
31074+
31075+#ifdef __KERNEL__
31076+
31077+#include <linux/sysfs.h>
31078+#include "module.h"
31079+
31080+struct super_block;
31081+struct au_sbinfo;
31082+
31083+struct sysaufs_si_attr {
31084+ struct attribute attr;
31085+ int (*show)(struct seq_file *seq, struct super_block *sb);
31086+};
31087+
31088+/* ---------------------------------------------------------------------- */
31089+
31090+/* sysaufs.c */
31091+extern unsigned long sysaufs_si_mask;
31092+extern struct kset *sysaufs_kset;
31093+extern struct attribute *sysaufs_si_attrs[];
31094+int sysaufs_si_init(struct au_sbinfo *sbinfo);
31095+int __init sysaufs_init(void);
31096+void sysaufs_fin(void);
31097+
31098+/* ---------------------------------------------------------------------- */
31099+
31100+/* some people doesn't like to show a pointer in kernel */
31101+static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31102+{
31103+ return sysaufs_si_mask ^ (unsigned long)sbinfo;
31104+}
31105+
31106+#define SysaufsSiNamePrefix "si_"
31107+#define SysaufsSiNameLen (sizeof(SysaufsSiNamePrefix) + 16)
31108+static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31109+{
31110+ snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31111+ sysaufs_si_id(sbinfo));
31112+}
31113+
31114+struct au_branch;
31115+#ifdef CONFIG_SYSFS
31116+/* sysfs.c */
31117+extern struct attribute_group *sysaufs_attr_group;
31118+
31119+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31120+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31121+ char *buf);
31122+long au_brinfo_ioctl(struct file *file, unsigned long arg);
31123+#ifdef CONFIG_COMPAT
31124+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31125+#endif
31126+
31127+void sysaufs_br_init(struct au_branch *br);
31128+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31129+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31130+
31131+#define sysaufs_brs_init() do {} while (0)
31132+
31133+#else
31134+#define sysaufs_attr_group NULL
31135+
31136+AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
31137+AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31138+ struct attribute *attr, char *buf)
31139+AuStubVoid(sysaufs_br_init, struct au_branch *br)
31140+AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31141+AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
31142+
31143+static inline void sysaufs_brs_init(void)
31144+{
31145+ sysaufs_brs = 0;
31146+}
31147+
31148+#endif /* CONFIG_SYSFS */
31149+
31150+#endif /* __KERNEL__ */
31151+#endif /* __SYSAUFS_H__ */
31152diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31153--- /usr/share/empty/fs/aufs/sysfs.c 1970-01-01 01:00:00.000000000 +0100
31154+++ linux/fs/aufs/sysfs.c 2021-02-22 22:51:59.061411920 +0100
31155@@ -0,0 +1,374 @@
31156+// SPDX-License-Identifier: GPL-2.0
31157+/*
31158+ * Copyright (C) 2005-2020 Junjiro R. Okajima
31159+ *
31160+ * This program, aufs is free software; you can redistribute it and/or modify
31161+ * it under the terms of the GNU General Public License as published by
31162+ * the Free Software Foundation; either version 2 of the License, or
31163+ * (at your option) any later version.
31164+ *
31165+ * This program is distributed in the hope that it will be useful,
31166+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31167+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31168+ * GNU General Public License for more details.
31169+ *
31170+ * You should have received a copy of the GNU General Public License
31171+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
31172+ */
31173+
31174+/*
31175+ * sysfs interface
31176+ */
31177+
31178+#include <linux/compat.h>
31179+#include <linux/seq_file.h>
31180+#include "aufs.h"
31181+
31182+#ifdef CONFIG_AUFS_FS_MODULE
31183+/* this entry violates the "one line per file" policy of sysfs */
31184+static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31185+ char *buf)
31186+{
31187+ ssize_t err;
31188+ static char *conf =
31189+/* this file is generated at compiling */
31190+#include "conf.str"
31191+ ;
31192+
31193+ err = snprintf(buf, PAGE_SIZE, conf);
31194+ if (unlikely(err >= PAGE_SIZE))
31195+ err = -EFBIG;
31196+ return err;
31197+}
31198+
31199+static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31200+#endif
31201+
31202+static struct attribute *au_attr[] = {
31203+#ifdef CONFIG_AUFS_FS_MODULE
31204+ &au_config_attr.attr,
31205+#endif
31206+ NULL, /* need to NULL terminate the list of attributes */
31207+};
31208+
31209+static struct attribute_group sysaufs_attr_group_body = {
31210+ .attrs = au_attr
31211+};
31212+
31213+struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31214+
31215+/* ---------------------------------------------------------------------- */
31216+
31217+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31218+{
31219+ int err;
31220+
31221+ SiMustAnyLock(sb);
31222+
31223+ err = 0;
31224+ if (au_opt_test(au_mntflags(sb), XINO)) {
31225+ err = au_xino_path(seq, au_sbi(sb)->si_xib);
31226+ seq_putc(seq, '\n');
31227+ }
31228+ return err;
31229+}
31230+
31231+/*
31232+ * the lifetime of branch is independent from the entry under sysfs.
31233+ * sysfs handles the lifetime of the entry, and never call ->show() after it is
31234+ * unlinked.
31235+ */
31236+static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
31237+ aufs_bindex_t bindex, int idx)
31238+{
31239+ int err;
31240+ struct path path;
31241+ struct dentry *root;
31242+ struct au_branch *br;
31243+ au_br_perm_str_t perm;
31244+
31245+ AuDbg("b%d\n", bindex);
31246+
31247+ err = 0;
31248+ root = sb->s_root;
31249+ di_read_lock_parent(root, !AuLock_IR);
31250+ br = au_sbr(sb, bindex);
31251+
31252+ switch (idx) {
31253+ case AuBrSysfs_BR:
31254+ path.mnt = au_br_mnt(br);
31255+ path.dentry = au_h_dptr(root, bindex);
31256+ err = au_seq_path(seq, &path);
31257+ if (!err) {
31258+ au_optstr_br_perm(&perm, br->br_perm);
31259+ seq_printf(seq, "=%s\n", perm.a);
31260+ }
31261+ break;
31262+ case AuBrSysfs_BRID:
31263+ seq_printf(seq, "%d\n", br->br_id);
31264+ break;
31265+ }
31266+ di_read_unlock(root, !AuLock_IR);
31267+ if (unlikely(err || seq_has_overflowed(seq)))
31268+ err = -E2BIG;
31269+
31270+ return err;
31271+}
31272+
31273+/* ---------------------------------------------------------------------- */
31274+
31275+static struct seq_file *au_seq(char *p, ssize_t len)
31276+{
31277+ struct seq_file *seq;
31278+
31279+ seq = kzalloc(sizeof(*seq), GFP_NOFS);
31280+ if (seq) {
31281+ /* mutex_init(&seq.lock); */
31282+ seq->buf = p;
31283+ seq->size = len;
31284+ return seq; /* success */
31285+ }
31286+
31287+ seq = ERR_PTR(-ENOMEM);
31288+ return seq;
31289+}
31290+
31291+#define SysaufsBr_PREFIX "br"
31292+#define SysaufsBrid_PREFIX "brid"
31293+
31294+/* todo: file size may exceed PAGE_SIZE */
31295+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31296+ char *buf)
31297+{
31298+ ssize_t err;
31299+ int idx;
31300+ long l;
31301+ aufs_bindex_t bbot;
31302+ struct au_sbinfo *sbinfo;
31303+ struct super_block *sb;
31304+ struct seq_file *seq;
31305+ char *name;
31306+ struct attribute **cattr;
31307+
31308+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31309+ sb = sbinfo->si_sb;
31310+
31311+ /*
31312+ * prevent a race condition between sysfs and aufs.
31313+ * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31314+ * prohibits maintaining the sysfs entries.
31315+ * hew we acquire read lock after sysfs_get_active_two().
31316+ * on the other hand, the remount process may maintain the sysfs/aufs
31317+ * entries after acquiring write lock.
31318+ * it can cause a deadlock.
31319+ * simply we gave up processing read here.
31320+ */
31321+ err = -EBUSY;
31322+ if (unlikely(!si_noflush_read_trylock(sb)))
31323+ goto out;
31324+
31325+ seq = au_seq(buf, PAGE_SIZE);
31326+ err = PTR_ERR(seq);
31327+ if (IS_ERR(seq))
31328+ goto out_unlock;
31329+
31330+ name = (void *)attr->name;
31331+ cattr = sysaufs_si_attrs;
31332+ while (*cattr) {
31333+ if (!strcmp(name, (*cattr)->name)) {
31334+ err = container_of(*cattr, struct sysaufs_si_attr, attr)
31335+ ->show(seq, sb);
31336+ goto out_seq;
31337+ }
31338+ cattr++;
31339+ }
31340+
31341+ if (!strncmp(name, SysaufsBrid_PREFIX,
31342+ sizeof(SysaufsBrid_PREFIX) - 1)) {
31343+ idx = AuBrSysfs_BRID;
31344+ name += sizeof(SysaufsBrid_PREFIX) - 1;
31345+ } else if (!strncmp(name, SysaufsBr_PREFIX,
31346+ sizeof(SysaufsBr_PREFIX) - 1)) {
31347+ idx = AuBrSysfs_BR;
31348+ name += sizeof(SysaufsBr_PREFIX) - 1;
31349+ } else
31350+ BUG();
31351+
31352+ err = kstrtol(name, 10, &l);
31353+ if (!err) {
31354+ bbot = au_sbbot(sb);
31355+ if (l <= bbot)
31356+ err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31357+ else
31358+ err = -ENOENT;
31359+ }
31360+
31361+out_seq:
31362+ if (!err) {
31363+ err = seq->count;
31364+ /* sysfs limit */
31365+ if (unlikely(err == PAGE_SIZE))
31366+ err = -EFBIG;
31367+ }
31368+ au_kfree_rcu(seq);
31369+out_unlock:
31370+ si_read_unlock(sb);
31371+out:
31372+ return err;
31373+}
31374+
31375+/* ---------------------------------------------------------------------- */
31376+
31377+static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31378+{
31379+ int err;
31380+ int16_t brid;
31381+ aufs_bindex_t bindex, bbot;
31382+ size_t sz;
31383+ char *buf;
31384+ struct seq_file *seq;
31385+ struct au_branch *br;
31386+
31387+ si_read_lock(sb, AuLock_FLUSH);
31388+ bbot = au_sbbot(sb);
31389+ err = bbot + 1;
31390+ if (!arg)
31391+ goto out;
31392+
31393+ err = -ENOMEM;
31394+ buf = (void *)__get_free_page(GFP_NOFS);
31395+ if (unlikely(!buf))
31396+ goto out;
31397+
31398+ seq = au_seq(buf, PAGE_SIZE);
31399+ err = PTR_ERR(seq);
31400+ if (IS_ERR(seq))
31401+ goto out_buf;
31402+
31403+ sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
31404+ for (bindex = 0; bindex <= bbot; bindex++, arg++) {
31405+ /* VERIFY_WRITE */
31406+ err = !access_ok(arg, sizeof(*arg));
31407+ if (unlikely(err))
31408+ break;
31409+
31410+ br = au_sbr(sb, bindex);
31411+ brid = br->br_id;
31412+ BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31413+ err = __put_user(brid, &arg->id);
31414+ if (unlikely(err))
31415+ break;
31416+
31417+ BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31418+ err = __put_user(br->br_perm, &arg->perm);
31419+ if (unlikely(err))
31420+ break;
31421+
31422+ err = au_seq_path(seq, &br->br_path);
31423+ if (unlikely(err))
31424+ break;
31425+ seq_putc(seq, '\0');
31426+ if (!seq_has_overflowed(seq)) {
31427+ err = copy_to_user(arg->path, seq->buf, seq->count);
31428+ seq->count = 0;
31429+ if (unlikely(err))
31430+ break;
31431+ } else {
31432+ err = -E2BIG;
31433+ goto out_seq;
31434+ }
31435+ }
31436+ if (unlikely(err))
31437+ err = -EFAULT;
31438+
31439+out_seq:
31440+ au_kfree_rcu(seq);
31441+out_buf:
31442+ free_page((unsigned long)buf);
31443+out:
31444+ si_read_unlock(sb);
31445+ return err;
31446+}
31447+
31448+long au_brinfo_ioctl(struct file *file, unsigned long arg)
31449+{
31450+ return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
31451+}
31452+
31453+#ifdef CONFIG_COMPAT
31454+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31455+{
31456+ return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
31457+}
31458+#endif
31459+
31460+/* ---------------------------------------------------------------------- */
31461+
31462+void sysaufs_br_init(struct au_branch *br)
31463+{
31464+ int i;
31465+ struct au_brsysfs *br_sysfs;
31466+ struct attribute *attr;
31467+
31468+ br_sysfs = br->br_sysfs;
31469+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31470+ attr = &br_sysfs->attr;
31471+ sysfs_attr_init(attr);
31472+ attr->name = br_sysfs->name;
31473+ attr->mode = 0444;
31474+ br_sysfs++;
31475+ }
31476+}
31477+
31478+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31479+{
31480+ struct au_branch *br;
31481+ struct kobject *kobj;
31482+ struct au_brsysfs *br_sysfs;
31483+ int i;
31484+ aufs_bindex_t bbot;
31485+
31486+ if (!sysaufs_brs)
31487+ return;
31488+
31489+ kobj = &au_sbi(sb)->si_kobj;
31490+ bbot = au_sbbot(sb);
31491+ for (; bindex <= bbot; bindex++) {
31492+ br = au_sbr(sb, bindex);
31493+ br_sysfs = br->br_sysfs;
31494+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31495+ sysfs_remove_file(kobj, &br_sysfs->attr);
31496+ br_sysfs++;
31497+ }
31498+ }
31499+}
31500+
31501+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31502+{
31503+ int err, i;
31504+ aufs_bindex_t bbot;
31505+ struct kobject *kobj;
31506+ struct au_branch *br;
31507+ struct au_brsysfs *br_sysfs;
31508+
31509+ if (!sysaufs_brs)
31510+ return;
31511+
31512+ kobj = &au_sbi(sb)->si_kobj;
31513+ bbot = au_sbbot(sb);
31514+ for (; bindex <= bbot; bindex++) {
31515+ br = au_sbr(sb, bindex);
31516+ br_sysfs = br->br_sysfs;
31517+ snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31518+ SysaufsBr_PREFIX "%d", bindex);
31519+ snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31520+ SysaufsBrid_PREFIX "%d", bindex);
31521+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31522+ err = sysfs_create_file(kobj, &br_sysfs->attr);
31523+ if (unlikely(err))
31524+ pr_warn("failed %s under sysfs(%d)\n",
31525+ br_sysfs->name, err);
31526+ br_sysfs++;
31527+ }
31528+ }
31529+}
31530diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31531--- /usr/share/empty/fs/aufs/sysrq.c 1970-01-01 01:00:00.000000000 +0100
31532+++ linux/fs/aufs/sysrq.c 2021-02-22 22:51:59.061411920 +0100
31533@@ -0,0 +1,149 @@
31534+// SPDX-License-Identifier: GPL-2.0
31535+/*
31536+ * Copyright (C) 2005-2020 Junjiro R. Okajima
31537+ *
31538+ * This program, aufs is free software; you can redistribute it and/or modify
31539+ * it under the terms of the GNU General Public License as published by
31540+ * the Free Software Foundation; either version 2 of the License, or
31541+ * (at your option) any later version.
31542+ *
31543+ * This program is distributed in the hope that it will be useful,
31544+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31545+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31546+ * GNU General Public License for more details.
31547+ *
31548+ * You should have received a copy of the GNU General Public License
31549+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
31550+ */
31551+
31552+/*
31553+ * magic sysrq handler
31554+ */
31555+
31556+/* #include <linux/sysrq.h> */
31557+#include <linux/writeback.h>
31558+#include "aufs.h"
31559+
31560+/* ---------------------------------------------------------------------- */
31561+
31562+static void sysrq_sb(struct super_block *sb)
31563+{
31564+ char *plevel;
31565+ struct au_sbinfo *sbinfo;
31566+ struct file *file;
31567+ struct hlist_bl_head *files;
31568+ struct hlist_bl_node *pos;
31569+ struct au_finfo *finfo;
31570+ struct inode *i;
31571+
31572+ plevel = au_plevel;
31573+ au_plevel = KERN_WARNING;
31574+
31575+ /* since we define pr_fmt, call printk directly */
31576+#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31577+
31578+ sbinfo = au_sbi(sb);
31579+ printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
31580+ pr("superblock\n");
31581+ au_dpri_sb(sb);
31582+
31583+#if 0 /* reserved */
31584+ do {
31585+ int err, i, j, ndentry;
31586+ struct au_dcsub_pages dpages;
31587+ struct au_dpage *dpage;
31588+
31589+ err = au_dpages_init(&dpages, GFP_ATOMIC);
31590+ if (unlikely(err))
31591+ break;
31592+ err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31593+ if (!err)
31594+ for (i = 0; i < dpages.ndpage; i++) {
31595+ dpage = dpages.dpages + i;
31596+ ndentry = dpage->ndentry;
31597+ for (j = 0; j < ndentry; j++)
31598+ au_dpri_dentry(dpage->dentries[j]);
31599+ }
31600+ au_dpages_free(&dpages);
31601+ } while (0);
31602+#endif
31603+
31604+ pr("isolated inode\n");
31605+ spin_lock(&sb->s_inode_list_lock);
31606+ list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31607+ spin_lock(&i->i_lock);
31608+ if (hlist_empty(&i->i_dentry))
31609+ au_dpri_inode(i);
31610+ spin_unlock(&i->i_lock);
31611+ }
31612+ spin_unlock(&sb->s_inode_list_lock);
31613+
31614+ pr("files\n");
31615+ files = &au_sbi(sb)->si_files;
31616+ hlist_bl_lock(files);
31617+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
31618+ umode_t mode;
31619+
31620+ file = finfo->fi_file;
31621+ mode = file_inode(file)->i_mode;
31622+ if (!special_file(mode))
31623+ au_dpri_file(file);
31624+ }
31625+ hlist_bl_unlock(files);
31626+ pr("done\n");
31627+
31628+#undef pr
31629+ au_plevel = plevel;
31630+}
31631+
31632+/* ---------------------------------------------------------------------- */
31633+
31634+/* module parameter */
31635+static char *aufs_sysrq_key = "a";
31636+module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
31637+MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31638+
31639+static void au_sysrq(int key __maybe_unused)
31640+{
31641+ struct au_sbinfo *sbinfo;
31642+ struct hlist_bl_node *pos;
31643+
31644+ lockdep_off();
31645+ au_sbilist_lock();
31646+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
31647+ sysrq_sb(sbinfo->si_sb);
31648+ au_sbilist_unlock();
31649+ lockdep_on();
31650+}
31651+
31652+static struct sysrq_key_op au_sysrq_op = {
31653+ .handler = au_sysrq,
31654+ .help_msg = "Aufs",
31655+ .action_msg = "Aufs",
31656+ .enable_mask = SYSRQ_ENABLE_DUMP
31657+};
31658+
31659+/* ---------------------------------------------------------------------- */
31660+
31661+int __init au_sysrq_init(void)
31662+{
31663+ int err;
31664+ char key;
31665+
31666+ err = -1;
31667+ key = *aufs_sysrq_key;
31668+ if ('a' <= key && key <= 'z')
31669+ err = register_sysrq_key(key, &au_sysrq_op);
31670+ if (unlikely(err))
31671+ pr_err("err %d, sysrq=%c\n", err, key);
31672+ return err;
31673+}
31674+
31675+void au_sysrq_fin(void)
31676+{
31677+ int err;
31678+
31679+ err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31680+ if (unlikely(err))
31681+ pr_err("err %d (ignored)\n", err);
31682+}
31683diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
31684--- /usr/share/empty/fs/aufs/vdir.c 1970-01-01 01:00:00.000000000 +0100
31685+++ linux/fs/aufs/vdir.c 2021-02-22 22:51:59.061411920 +0100
31686@@ -0,0 +1,896 @@
31687+// SPDX-License-Identifier: GPL-2.0
31688+/*
31689+ * Copyright (C) 2005-2020 Junjiro R. Okajima
31690+ *
31691+ * This program, aufs is free software; you can redistribute it and/or modify
31692+ * it under the terms of the GNU General Public License as published by
31693+ * the Free Software Foundation; either version 2 of the License, or
31694+ * (at your option) any later version.
31695+ *
31696+ * This program is distributed in the hope that it will be useful,
31697+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31698+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31699+ * GNU General Public License for more details.
31700+ *
31701+ * You should have received a copy of the GNU General Public License
31702+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
31703+ */
31704+
31705+/*
31706+ * virtual or vertical directory
31707+ */
31708+
31709+#include <linux/iversion.h>
31710+#include "aufs.h"
31711+
31712+static unsigned int calc_size(int nlen)
31713+{
31714+ return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
31715+}
31716+
31717+static int set_deblk_end(union au_vdir_deblk_p *p,
31718+ union au_vdir_deblk_p *deblk_end)
31719+{
31720+ if (calc_size(0) <= deblk_end->deblk - p->deblk) {
31721+ p->de->de_str.len = 0;
31722+ /* smp_mb(); */
31723+ return 0;
31724+ }
31725+ return -1; /* error */
31726+}
31727+
31728+/* returns true or false */
31729+static int is_deblk_end(union au_vdir_deblk_p *p,
31730+ union au_vdir_deblk_p *deblk_end)
31731+{
31732+ if (calc_size(0) <= deblk_end->deblk - p->deblk)
31733+ return !p->de->de_str.len;
31734+ return 1;
31735+}
31736+
31737+static unsigned char *last_deblk(struct au_vdir *vdir)
31738+{
31739+ return vdir->vd_deblk[vdir->vd_nblk - 1];
31740+}
31741+
31742+/* ---------------------------------------------------------------------- */
31743+
31744+/* estimate the appropriate size for name hash table */
31745+unsigned int au_rdhash_est(loff_t sz)
31746+{
31747+ unsigned int n;
31748+
31749+ n = UINT_MAX;
31750+ sz >>= 10;
31751+ if (sz < n)
31752+ n = sz;
31753+ if (sz < AUFS_RDHASH_DEF)
31754+ n = AUFS_RDHASH_DEF;
31755+ /* pr_info("n %u\n", n); */
31756+ return n;
31757+}
31758+
31759+/*
31760+ * the allocated memory has to be freed by
31761+ * au_nhash_wh_free() or au_nhash_de_free().
31762+ */
31763+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
31764+{
31765+ struct hlist_head *head;
31766+ unsigned int u;
31767+ size_t sz;
31768+
31769+ sz = sizeof(*nhash->nh_head) * num_hash;
31770+ head = kmalloc(sz, gfp);
31771+ if (head) {
31772+ nhash->nh_num = num_hash;
31773+ nhash->nh_head = head;
31774+ for (u = 0; u < num_hash; u++)
31775+ INIT_HLIST_HEAD(head++);
31776+ return 0; /* success */
31777+ }
31778+
31779+ return -ENOMEM;
31780+}
31781+
31782+static void nhash_count(struct hlist_head *head)
31783+{
31784+#if 0 /* debugging */
31785+ unsigned long n;
31786+ struct hlist_node *pos;
31787+
31788+ n = 0;
31789+ hlist_for_each(pos, head)
31790+ n++;
31791+ pr_info("%lu\n", n);
31792+#endif
31793+}
31794+
31795+static void au_nhash_wh_do_free(struct hlist_head *head)
31796+{
31797+ struct au_vdir_wh *pos;
31798+ struct hlist_node *node;
31799+
31800+ hlist_for_each_entry_safe(pos, node, head, wh_hash)
31801+ au_kfree_rcu(pos);
31802+}
31803+
31804+static void au_nhash_de_do_free(struct hlist_head *head)
31805+{
31806+ struct au_vdir_dehstr *pos;
31807+ struct hlist_node *node;
31808+
31809+ hlist_for_each_entry_safe(pos, node, head, hash)
31810+ au_cache_free_vdir_dehstr(pos);
31811+}
31812+
31813+static void au_nhash_do_free(struct au_nhash *nhash,
31814+ void (*free)(struct hlist_head *head))
31815+{
31816+ unsigned int n;
31817+ struct hlist_head *head;
31818+
31819+ n = nhash->nh_num;
31820+ if (!n)
31821+ return;
31822+
31823+ head = nhash->nh_head;
31824+ while (n-- > 0) {
31825+ nhash_count(head);
31826+ free(head++);
31827+ }
31828+ au_kfree_try_rcu(nhash->nh_head);
31829+}
31830+
31831+void au_nhash_wh_free(struct au_nhash *whlist)
31832+{
31833+ au_nhash_do_free(whlist, au_nhash_wh_do_free);
31834+}
31835+
31836+static void au_nhash_de_free(struct au_nhash *delist)
31837+{
31838+ au_nhash_do_free(delist, au_nhash_de_do_free);
31839+}
31840+
31841+/* ---------------------------------------------------------------------- */
31842+
31843+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
31844+ int limit)
31845+{
31846+ int num;
31847+ unsigned int u, n;
31848+ struct hlist_head *head;
31849+ struct au_vdir_wh *pos;
31850+
31851+ num = 0;
31852+ n = whlist->nh_num;
31853+ head = whlist->nh_head;
31854+ for (u = 0; u < n; u++, head++)
31855+ hlist_for_each_entry(pos, head, wh_hash)
31856+ if (pos->wh_bindex == btgt && ++num > limit)
31857+ return 1;
31858+ return 0;
31859+}
31860+
31861+static struct hlist_head *au_name_hash(struct au_nhash *nhash,
31862+ unsigned char *name,
31863+ unsigned int len)
31864+{
31865+ unsigned int v;
31866+ /* const unsigned int magic_bit = 12; */
31867+
31868+ AuDebugOn(!nhash->nh_num || !nhash->nh_head);
31869+
31870+ v = 0;
31871+ if (len > 8)
31872+ len = 8;
31873+ while (len--)
31874+ v += *name++;
31875+ /* v = hash_long(v, magic_bit); */
31876+ v %= nhash->nh_num;
31877+ return nhash->nh_head + v;
31878+}
31879+
31880+static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
31881+ int nlen)
31882+{
31883+ return str->len == nlen && !memcmp(str->name, name, nlen);
31884+}
31885+
31886+/* returns found or not */
31887+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
31888+{
31889+ struct hlist_head *head;
31890+ struct au_vdir_wh *pos;
31891+ struct au_vdir_destr *str;
31892+
31893+ head = au_name_hash(whlist, name, nlen);
31894+ hlist_for_each_entry(pos, head, wh_hash) {
31895+ str = &pos->wh_str;
31896+ AuDbg("%.*s\n", str->len, str->name);
31897+ if (au_nhash_test_name(str, name, nlen))
31898+ return 1;
31899+ }
31900+ return 0;
31901+}
31902+
31903+/* returns found(true) or not */
31904+static int test_known(struct au_nhash *delist, char *name, int nlen)
31905+{
31906+ struct hlist_head *head;
31907+ struct au_vdir_dehstr *pos;
31908+ struct au_vdir_destr *str;
31909+
31910+ head = au_name_hash(delist, name, nlen);
31911+ hlist_for_each_entry(pos, head, hash) {
31912+ str = pos->str;
31913+ AuDbg("%.*s\n", str->len, str->name);
31914+ if (au_nhash_test_name(str, name, nlen))
31915+ return 1;
31916+ }
31917+ return 0;
31918+}
31919+
31920+static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
31921+ unsigned char d_type)
31922+{
31923+#ifdef CONFIG_AUFS_SHWH
31924+ wh->wh_ino = ino;
31925+ wh->wh_type = d_type;
31926+#endif
31927+}
31928+
31929+/* ---------------------------------------------------------------------- */
31930+
31931+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
31932+ unsigned int d_type, aufs_bindex_t bindex,
31933+ unsigned char shwh)
31934+{
31935+ int err;
31936+ struct au_vdir_destr *str;
31937+ struct au_vdir_wh *wh;
31938+
31939+ AuDbg("%.*s\n", nlen, name);
31940+ AuDebugOn(!whlist->nh_num || !whlist->nh_head);
31941+
31942+ err = -ENOMEM;
31943+ wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
31944+ if (unlikely(!wh))
31945+ goto out;
31946+
31947+ err = 0;
31948+ wh->wh_bindex = bindex;
31949+ if (shwh)
31950+ au_shwh_init_wh(wh, ino, d_type);
31951+ str = &wh->wh_str;
31952+ str->len = nlen;
31953+ memcpy(str->name, name, nlen);
31954+ hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
31955+ /* smp_mb(); */
31956+
31957+out:
31958+ return err;
31959+}
31960+
31961+static int append_deblk(struct au_vdir *vdir)
31962+{
31963+ int err;
31964+ unsigned long ul;
31965+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
31966+ union au_vdir_deblk_p p, deblk_end;
31967+ unsigned char **o;
31968+
31969+ err = -ENOMEM;
31970+ o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
31971+ GFP_NOFS, /*may_shrink*/0);
31972+ if (unlikely(!o))
31973+ goto out;
31974+
31975+ vdir->vd_deblk = o;
31976+ p.deblk = kmalloc(deblk_sz, GFP_NOFS);
31977+ if (p.deblk) {
31978+ ul = vdir->vd_nblk++;
31979+ vdir->vd_deblk[ul] = p.deblk;
31980+ vdir->vd_last.ul = ul;
31981+ vdir->vd_last.p.deblk = p.deblk;
31982+ deblk_end.deblk = p.deblk + deblk_sz;
31983+ err = set_deblk_end(&p, &deblk_end);
31984+ }
31985+
31986+out:
31987+ return err;
31988+}
31989+
31990+static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
31991+ unsigned int d_type, struct au_nhash *delist)
31992+{
31993+ int err;
31994+ unsigned int sz;
31995+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
31996+ union au_vdir_deblk_p p, *room, deblk_end;
31997+ struct au_vdir_dehstr *dehstr;
31998+
31999+ p.deblk = last_deblk(vdir);
32000+ deblk_end.deblk = p.deblk + deblk_sz;
32001+ room = &vdir->vd_last.p;
32002+ AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32003+ || !is_deblk_end(room, &deblk_end));
32004+
32005+ sz = calc_size(nlen);
32006+ if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32007+ err = append_deblk(vdir);
32008+ if (unlikely(err))
32009+ goto out;
32010+
32011+ p.deblk = last_deblk(vdir);
32012+ deblk_end.deblk = p.deblk + deblk_sz;
32013+ /* smp_mb(); */
32014+ AuDebugOn(room->deblk != p.deblk);
32015+ }
32016+
32017+ err = -ENOMEM;
32018+ dehstr = au_cache_alloc_vdir_dehstr();
32019+ if (unlikely(!dehstr))
32020+ goto out;
32021+
32022+ dehstr->str = &room->de->de_str;
32023+ hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32024+ room->de->de_ino = ino;
32025+ room->de->de_type = d_type;
32026+ room->de->de_str.len = nlen;
32027+ memcpy(room->de->de_str.name, name, nlen);
32028+
32029+ err = 0;
32030+ room->deblk += sz;
32031+ if (unlikely(set_deblk_end(room, &deblk_end)))
32032+ err = append_deblk(vdir);
32033+ /* smp_mb(); */
32034+
32035+out:
32036+ return err;
32037+}
32038+
32039+/* ---------------------------------------------------------------------- */
32040+
32041+void au_vdir_free(struct au_vdir *vdir)
32042+{
32043+ unsigned char **deblk;
32044+
32045+ deblk = vdir->vd_deblk;
32046+ while (vdir->vd_nblk--)
32047+ au_kfree_try_rcu(*deblk++);
32048+ au_kfree_try_rcu(vdir->vd_deblk);
32049+ au_cache_free_vdir(vdir);
32050+}
32051+
32052+static struct au_vdir *alloc_vdir(struct file *file)
32053+{
32054+ struct au_vdir *vdir;
32055+ struct super_block *sb;
32056+ int err;
32057+
32058+ sb = file->f_path.dentry->d_sb;
32059+ SiMustAnyLock(sb);
32060+
32061+ err = -ENOMEM;
32062+ vdir = au_cache_alloc_vdir();
32063+ if (unlikely(!vdir))
32064+ goto out;
32065+
32066+ vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32067+ if (unlikely(!vdir->vd_deblk))
32068+ goto out_free;
32069+
32070+ vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
32071+ if (!vdir->vd_deblk_sz) {
32072+ /* estimate the appropriate size for deblk */
32073+ vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
32074+ /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
32075+ }
32076+ vdir->vd_nblk = 0;
32077+ vdir->vd_version = 0;
32078+ vdir->vd_jiffy = 0;
32079+ err = append_deblk(vdir);
32080+ if (!err)
32081+ return vdir; /* success */
32082+
32083+ au_kfree_try_rcu(vdir->vd_deblk);
32084+
32085+out_free:
32086+ au_cache_free_vdir(vdir);
32087+out:
32088+ vdir = ERR_PTR(err);
32089+ return vdir;
32090+}
32091+
32092+static int reinit_vdir(struct au_vdir *vdir)
32093+{
32094+ int err;
32095+ union au_vdir_deblk_p p, deblk_end;
32096+
32097+ while (vdir->vd_nblk > 1) {
32098+ au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
32099+ /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32100+ vdir->vd_nblk--;
32101+ }
32102+ p.deblk = vdir->vd_deblk[0];
32103+ deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32104+ err = set_deblk_end(&p, &deblk_end);
32105+ /* keep vd_dblk_sz */
32106+ vdir->vd_last.ul = 0;
32107+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32108+ vdir->vd_version = 0;
32109+ vdir->vd_jiffy = 0;
32110+ /* smp_mb(); */
32111+ return err;
32112+}
32113+
32114+/* ---------------------------------------------------------------------- */
32115+
32116+#define AuFillVdir_CALLED 1
32117+#define AuFillVdir_WHABLE (1 << 1)
32118+#define AuFillVdir_SHWH (1 << 2)
32119+#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
32120+#define au_fset_fillvdir(flags, name) \
32121+ do { (flags) |= AuFillVdir_##name; } while (0)
32122+#define au_fclr_fillvdir(flags, name) \
32123+ do { (flags) &= ~AuFillVdir_##name; } while (0)
32124+
32125+#ifndef CONFIG_AUFS_SHWH
32126+#undef AuFillVdir_SHWH
32127+#define AuFillVdir_SHWH 0
32128+#endif
32129+
32130+struct fillvdir_arg {
32131+ struct dir_context ctx;
32132+ struct file *file;
32133+ struct au_vdir *vdir;
32134+ struct au_nhash delist;
32135+ struct au_nhash whlist;
32136+ aufs_bindex_t bindex;
32137+ unsigned int flags;
32138+ int err;
32139+};
32140+
32141+static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
32142+ loff_t offset __maybe_unused, u64 h_ino,
32143+ unsigned int d_type)
32144+{
32145+ struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
32146+ char *name = (void *)__name;
32147+ struct super_block *sb;
32148+ ino_t ino;
32149+ const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
32150+
32151+ arg->err = 0;
32152+ sb = arg->file->f_path.dentry->d_sb;
32153+ au_fset_fillvdir(arg->flags, CALLED);
32154+ /* smp_mb(); */
32155+ if (nlen <= AUFS_WH_PFX_LEN
32156+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
32157+ if (test_known(&arg->delist, name, nlen)
32158+ || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32159+ goto out; /* already exists or whiteouted */
32160+
32161+ arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
32162+ if (!arg->err) {
32163+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
32164+ d_type = DT_UNKNOWN;
32165+ arg->err = append_de(arg->vdir, name, nlen, ino,
32166+ d_type, &arg->delist);
32167+ }
32168+ } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32169+ name += AUFS_WH_PFX_LEN;
32170+ nlen -= AUFS_WH_PFX_LEN;
32171+ if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32172+ goto out; /* already whiteouted */
32173+
32174+ ino = 0; /* just to suppress a warning */
32175+ if (shwh)
32176+ arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32177+ &ino);
32178+ if (!arg->err) {
32179+ if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32180+ d_type = DT_UNKNOWN;
32181+ arg->err = au_nhash_append_wh
32182+ (&arg->whlist, name, nlen, ino, d_type,
32183+ arg->bindex, shwh);
32184+ }
32185+ }
32186+
32187+out:
32188+ if (!arg->err)
32189+ arg->vdir->vd_jiffy = jiffies;
32190+ /* smp_mb(); */
32191+ AuTraceErr(arg->err);
32192+ return arg->err;
32193+}
32194+
32195+static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32196+ struct au_nhash *whlist, struct au_nhash *delist)
32197+{
32198+#ifdef CONFIG_AUFS_SHWH
32199+ int err;
32200+ unsigned int nh, u;
32201+ struct hlist_head *head;
32202+ struct au_vdir_wh *pos;
32203+ struct hlist_node *n;
32204+ char *p, *o;
32205+ struct au_vdir_destr *destr;
32206+
32207+ AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32208+
32209+ err = -ENOMEM;
32210+ o = p = (void *)__get_free_page(GFP_NOFS);
32211+ if (unlikely(!p))
32212+ goto out;
32213+
32214+ err = 0;
32215+ nh = whlist->nh_num;
32216+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32217+ p += AUFS_WH_PFX_LEN;
32218+ for (u = 0; u < nh; u++) {
32219+ head = whlist->nh_head + u;
32220+ hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32221+ destr = &pos->wh_str;
32222+ memcpy(p, destr->name, destr->len);
32223+ err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
32224+ pos->wh_ino, pos->wh_type, delist);
32225+ if (unlikely(err))
32226+ break;
32227+ }
32228+ }
32229+
32230+ free_page((unsigned long)o);
32231+
32232+out:
32233+ AuTraceErr(err);
32234+ return err;
32235+#else
32236+ return 0;
32237+#endif
32238+}
32239+
32240+static int au_do_read_vdir(struct fillvdir_arg *arg)
32241+{
32242+ int err;
32243+ unsigned int rdhash;
32244+ loff_t offset;
32245+ aufs_bindex_t bbot, bindex, btop;
32246+ unsigned char shwh;
32247+ struct file *hf, *file;
32248+ struct super_block *sb;
32249+
32250+ file = arg->file;
32251+ sb = file->f_path.dentry->d_sb;
32252+ SiMustAnyLock(sb);
32253+
32254+ rdhash = au_sbi(sb)->si_rdhash;
32255+ if (!rdhash)
32256+ rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
32257+ err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32258+ if (unlikely(err))
32259+ goto out;
32260+ err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32261+ if (unlikely(err))
32262+ goto out_delist;
32263+
32264+ err = 0;
32265+ arg->flags = 0;
32266+ shwh = 0;
32267+ if (au_opt_test(au_mntflags(sb), SHWH)) {
32268+ shwh = 1;
32269+ au_fset_fillvdir(arg->flags, SHWH);
32270+ }
32271+ btop = au_fbtop(file);
32272+ bbot = au_fbbot_dir(file);
32273+ for (bindex = btop; !err && bindex <= bbot; bindex++) {
32274+ hf = au_hf_dir(file, bindex);
32275+ if (!hf)
32276+ continue;
32277+
32278+ offset = vfsub_llseek(hf, 0, SEEK_SET);
32279+ err = offset;
32280+ if (unlikely(offset))
32281+ break;
32282+
32283+ arg->bindex = bindex;
32284+ au_fclr_fillvdir(arg->flags, WHABLE);
32285+ if (shwh
32286+ || (bindex != bbot
32287+ && au_br_whable(au_sbr_perm(sb, bindex))))
32288+ au_fset_fillvdir(arg->flags, WHABLE);
32289+ do {
32290+ arg->err = 0;
32291+ au_fclr_fillvdir(arg->flags, CALLED);
32292+ /* smp_mb(); */
32293+ err = vfsub_iterate_dir(hf, &arg->ctx);
32294+ if (err >= 0)
32295+ err = arg->err;
32296+ } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
32297+
32298+ /*
32299+ * dir_relax() may be good for concurrency, but aufs should not
32300+ * use it since it will cause a lockdep problem.
32301+ */
32302+ }
32303+
32304+ if (!err && shwh)
32305+ err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32306+
32307+ au_nhash_wh_free(&arg->whlist);
32308+
32309+out_delist:
32310+ au_nhash_de_free(&arg->delist);
32311+out:
32312+ return err;
32313+}
32314+
32315+static int read_vdir(struct file *file, int may_read)
32316+{
32317+ int err;
32318+ unsigned long expire;
32319+ unsigned char do_read;
32320+ struct fillvdir_arg arg = {
32321+ .ctx = {
32322+ .actor = fillvdir
32323+ }
32324+ };
32325+ struct inode *inode;
32326+ struct au_vdir *vdir, *allocated;
32327+
32328+ err = 0;
32329+ inode = file_inode(file);
32330+ IMustLock(inode);
32331+ IiMustWriteLock(inode);
32332+ SiMustAnyLock(inode->i_sb);
32333+
32334+ allocated = NULL;
32335+ do_read = 0;
32336+ expire = au_sbi(inode->i_sb)->si_rdcache;
32337+ vdir = au_ivdir(inode);
32338+ if (!vdir) {
32339+ do_read = 1;
32340+ vdir = alloc_vdir(file);
32341+ err = PTR_ERR(vdir);
32342+ if (IS_ERR(vdir))
32343+ goto out;
32344+ err = 0;
32345+ allocated = vdir;
32346+ } else if (may_read
32347+ && (!inode_eq_iversion(inode, vdir->vd_version)
32348+ || time_after(jiffies, vdir->vd_jiffy + expire))) {
32349+ do_read = 1;
32350+ err = reinit_vdir(vdir);
32351+ if (unlikely(err))
32352+ goto out;
32353+ }
32354+
32355+ if (!do_read)
32356+ return 0; /* success */
32357+
32358+ arg.file = file;
32359+ arg.vdir = vdir;
32360+ err = au_do_read_vdir(&arg);
32361+ if (!err) {
32362+ /* file->f_pos = 0; */ /* todo: ctx->pos? */
32363+ vdir->vd_version = inode_query_iversion(inode);
32364+ vdir->vd_last.ul = 0;
32365+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32366+ if (allocated)
32367+ au_set_ivdir(inode, allocated);
32368+ } else if (allocated)
32369+ au_vdir_free(allocated);
32370+
32371+out:
32372+ return err;
32373+}
32374+
32375+static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32376+{
32377+ int err, rerr;
32378+ unsigned long ul, n;
32379+ const unsigned int deblk_sz = src->vd_deblk_sz;
32380+
32381+ AuDebugOn(tgt->vd_nblk != 1);
32382+
32383+ err = -ENOMEM;
32384+ if (tgt->vd_nblk < src->vd_nblk) {
32385+ unsigned char **p;
32386+
32387+ p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32388+ GFP_NOFS, /*may_shrink*/0);
32389+ if (unlikely(!p))
32390+ goto out;
32391+ tgt->vd_deblk = p;
32392+ }
32393+
32394+ if (tgt->vd_deblk_sz != deblk_sz) {
32395+ unsigned char *p;
32396+
32397+ tgt->vd_deblk_sz = deblk_sz;
32398+ p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32399+ /*may_shrink*/1);
32400+ if (unlikely(!p))
32401+ goto out;
32402+ tgt->vd_deblk[0] = p;
32403+ }
32404+ memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
32405+ tgt->vd_version = src->vd_version;
32406+ tgt->vd_jiffy = src->vd_jiffy;
32407+
32408+ n = src->vd_nblk;
32409+ for (ul = 1; ul < n; ul++) {
32410+ tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32411+ GFP_NOFS);
32412+ if (unlikely(!tgt->vd_deblk[ul]))
32413+ goto out;
32414+ tgt->vd_nblk++;
32415+ }
32416+ tgt->vd_nblk = n;
32417+ tgt->vd_last.ul = tgt->vd_last.ul;
32418+ tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32419+ tgt->vd_last.p.deblk += src->vd_last.p.deblk
32420+ - src->vd_deblk[src->vd_last.ul];
32421+ /* smp_mb(); */
32422+ return 0; /* success */
32423+
32424+out:
32425+ rerr = reinit_vdir(tgt);
32426+ BUG_ON(rerr);
32427+ return err;
32428+}
32429+
32430+int au_vdir_init(struct file *file)
32431+{
32432+ int err;
32433+ struct inode *inode;
32434+ struct au_vdir *vdir_cache, *allocated;
32435+
32436+ /* test file->f_pos here instead of ctx->pos */
32437+ err = read_vdir(file, !file->f_pos);
32438+ if (unlikely(err))
32439+ goto out;
32440+
32441+ allocated = NULL;
32442+ vdir_cache = au_fvdir_cache(file);
32443+ if (!vdir_cache) {
32444+ vdir_cache = alloc_vdir(file);
32445+ err = PTR_ERR(vdir_cache);
32446+ if (IS_ERR(vdir_cache))
32447+ goto out;
32448+ allocated = vdir_cache;
32449+ } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
32450+ /* test file->f_pos here instead of ctx->pos */
32451+ err = reinit_vdir(vdir_cache);
32452+ if (unlikely(err))
32453+ goto out;
32454+ } else
32455+ return 0; /* success */
32456+
32457+ inode = file_inode(file);
32458+ err = copy_vdir(vdir_cache, au_ivdir(inode));
32459+ if (!err) {
32460+ file->f_version = inode_query_iversion(inode);
32461+ if (allocated)
32462+ au_set_fvdir_cache(file, allocated);
32463+ } else if (allocated)
32464+ au_vdir_free(allocated);
32465+
32466+out:
32467+ return err;
32468+}
32469+
32470+static loff_t calc_offset(struct au_vdir *vdir)
32471+{
32472+ loff_t offset;
32473+ union au_vdir_deblk_p p;
32474+
32475+ p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32476+ offset = vdir->vd_last.p.deblk - p.deblk;
32477+ offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32478+ return offset;
32479+}
32480+
32481+/* returns true or false */
32482+static int seek_vdir(struct file *file, struct dir_context *ctx)
32483+{
32484+ int valid;
32485+ unsigned int deblk_sz;
32486+ unsigned long ul, n;
32487+ loff_t offset;
32488+ union au_vdir_deblk_p p, deblk_end;
32489+ struct au_vdir *vdir_cache;
32490+
32491+ valid = 1;
32492+ vdir_cache = au_fvdir_cache(file);
32493+ offset = calc_offset(vdir_cache);
32494+ AuDbg("offset %lld\n", offset);
32495+ if (ctx->pos == offset)
32496+ goto out;
32497+
32498+ vdir_cache->vd_last.ul = 0;
32499+ vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
32500+ if (!ctx->pos)
32501+ goto out;
32502+
32503+ valid = 0;
32504+ deblk_sz = vdir_cache->vd_deblk_sz;
32505+ ul = div64_u64(ctx->pos, deblk_sz);
32506+ AuDbg("ul %lu\n", ul);
32507+ if (ul >= vdir_cache->vd_nblk)
32508+ goto out;
32509+
32510+ n = vdir_cache->vd_nblk;
32511+ for (; ul < n; ul++) {
32512+ p.deblk = vdir_cache->vd_deblk[ul];
32513+ deblk_end.deblk = p.deblk + deblk_sz;
32514+ offset = ul;
32515+ offset *= deblk_sz;
32516+ while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
32517+ unsigned int l;
32518+
32519+ l = calc_size(p.de->de_str.len);
32520+ offset += l;
32521+ p.deblk += l;
32522+ }
32523+ if (!is_deblk_end(&p, &deblk_end)) {
32524+ valid = 1;
32525+ vdir_cache->vd_last.ul = ul;
32526+ vdir_cache->vd_last.p = p;
32527+ break;
32528+ }
32529+ }
32530+
32531+out:
32532+ /* smp_mb(); */
32533+ if (!valid)
32534+ AuDbg("valid %d\n", !valid);
32535+ return valid;
32536+}
32537+
32538+int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
32539+{
32540+ unsigned int l, deblk_sz;
32541+ union au_vdir_deblk_p deblk_end;
32542+ struct au_vdir *vdir_cache;
32543+ struct au_vdir_de *de;
32544+
32545+ if (!seek_vdir(file, ctx))
32546+ return 0;
32547+
32548+ vdir_cache = au_fvdir_cache(file);
32549+ deblk_sz = vdir_cache->vd_deblk_sz;
32550+ while (1) {
32551+ deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32552+ deblk_end.deblk += deblk_sz;
32553+ while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32554+ de = vdir_cache->vd_last.p.de;
32555+ AuDbg("%.*s, off%lld, i%lu, dt%d\n",
32556+ de->de_str.len, de->de_str.name, ctx->pos,
32557+ (unsigned long)de->de_ino, de->de_type);
32558+ if (unlikely(!dir_emit(ctx, de->de_str.name,
32559+ de->de_str.len, de->de_ino,
32560+ de->de_type))) {
32561+ /* todo: ignore the error caused by udba? */
32562+ /* return err; */
32563+ return 0;
32564+ }
32565+
32566+ l = calc_size(de->de_str.len);
32567+ vdir_cache->vd_last.p.deblk += l;
32568+ ctx->pos += l;
32569+ }
32570+ if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32571+ vdir_cache->vd_last.ul++;
32572+ vdir_cache->vd_last.p.deblk
32573+ = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32574+ ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
32575+ continue;
32576+ }
32577+ break;
32578+ }
32579+
32580+ /* smp_mb(); */
32581+ return 0;
32582+}
32583diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32584--- /usr/share/empty/fs/aufs/vfsub.c 1970-01-01 01:00:00.000000000 +0100
32585+++ linux/fs/aufs/vfsub.c 2021-02-22 22:51:59.061411920 +0100
32586@@ -0,0 +1,885 @@
32587+// SPDX-License-Identifier: GPL-2.0
32588+/*
32589+ * Copyright (C) 2005-2020 Junjiro R. Okajima
32590+ *
32591+ * This program, aufs is free software; you can redistribute it and/or modify
32592+ * it under the terms of the GNU General Public License as published by
32593+ * the Free Software Foundation; either version 2 of the License, or
32594+ * (at your option) any later version.
32595+ *
32596+ * This program is distributed in the hope that it will be useful,
32597+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
32598+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32599+ * GNU General Public License for more details.
32600+ *
32601+ * You should have received a copy of the GNU General Public License
32602+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
32603+ */
32604+
32605+/*
32606+ * sub-routines for VFS
32607+ */
32608+
32609+#include <linux/mnt_namespace.h>
32610+#include <linux/namei.h>
32611+#include <linux/nsproxy.h>
32612+#include <linux/security.h>
32613+#include <linux/splice.h>
32614+#include "aufs.h"
32615+
32616+#ifdef CONFIG_AUFS_BR_FUSE
32617+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32618+{
32619+ if (!au_test_fuse(h_sb) || !au_userns)
32620+ return 0;
32621+
32622+ return is_current_mnt_ns(mnt) ? 0 : -EACCES;
32623+}
32624+#endif
32625+
32626+int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
32627+{
32628+ int err;
32629+
32630+ lockdep_off();
32631+ down_read(&h_sb->s_umount);
32632+ err = __sync_filesystem(h_sb, wait);
32633+ up_read(&h_sb->s_umount);
32634+ lockdep_on();
32635+
32636+ return err;
32637+}
32638+
32639+/* ---------------------------------------------------------------------- */
32640+
32641+int vfsub_update_h_iattr(struct path *h_path, int *did)
32642+{
32643+ int err;
32644+ struct kstat st;
32645+ struct super_block *h_sb;
32646+
32647+ /* for remote fs, leave work for its getattr or d_revalidate */
32648+ /* for bad i_attr fs, handle them in aufs_getattr() */
32649+ /* still some fs may acquire i_mutex. we need to skip them */
32650+ err = 0;
32651+ if (!did)
32652+ did = &err;
32653+ h_sb = h_path->dentry->d_sb;
32654+ *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32655+ if (*did)
32656+ err = vfsub_getattr(h_path, &st);
32657+
32658+ return err;
32659+}
32660+
32661+/* ---------------------------------------------------------------------- */
32662+
32663+struct file *vfsub_dentry_open(struct path *path, int flags)
32664+{
32665+ return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
32666+ current_cred());
32667+}
32668+
32669+struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32670+{
32671+ struct file *file;
32672+
32673+ lockdep_off();
32674+ file = filp_open(path,
32675+ oflags /* | __FMODE_NONOTIFY */,
32676+ mode);
32677+ lockdep_on();
32678+ if (IS_ERR(file))
32679+ goto out;
32680+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32681+
32682+out:
32683+ return file;
32684+}
32685+
32686+/*
32687+ * Ideally this function should call VFS:do_last() in order to keep all its
32688+ * checkings. But it is very hard for aufs to regenerate several VFS internal
32689+ * structure such as nameidata. This is a second (or third) best approach.
32690+ * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32691+ */
32692+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
32693+ struct vfsub_aopen_args *args)
32694+{
32695+ int err;
32696+ struct au_branch *br = args->br;
32697+ struct file *file = args->file;
32698+ /* copied from linux/fs/namei.c:atomic_open() */
32699+ struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
32700+
32701+ IMustLock(dir);
32702+ AuDebugOn(!dir->i_op->atomic_open);
32703+
32704+ err = au_br_test_oflag(args->open_flag, br);
32705+ if (unlikely(err))
32706+ goto out;
32707+
32708+ au_lcnt_inc(&br->br_nfiles);
32709+ file->f_path.dentry = DENTRY_NOT_SET;
32710+ file->f_path.mnt = au_br_mnt(br);
32711+ AuDbg("%ps\n", dir->i_op->atomic_open);
32712+ err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
32713+ args->create_mode);
32714+ if (unlikely(err < 0)) {
32715+ au_lcnt_dec(&br->br_nfiles);
32716+ goto out;
32717+ }
32718+
32719+ /* temporary workaround for nfsv4 branch */
32720+ if (au_test_nfs(dir->i_sb))
32721+ nfs_mark_for_revalidate(dir);
32722+
32723+ if (file->f_mode & FMODE_CREATED)
32724+ fsnotify_create(dir, dentry);
32725+ if (!(file->f_mode & FMODE_OPENED)) {
32726+ au_lcnt_dec(&br->br_nfiles);
32727+ goto out;
32728+ }
32729+
32730+ /* todo: call VFS:may_open() here */
32731+ /* todo: ima_file_check() too? */
32732+ if (!err && (args->open_flag & __FMODE_EXEC))
32733+ err = deny_write_access(file);
32734+ if (!err)
32735+ fsnotify_open(file);
32736+ else
32737+ au_lcnt_dec(&br->br_nfiles);
32738+ /* note that the file is created and still opened */
32739+
32740+out:
32741+ return err;
32742+}
32743+
32744+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
32745+{
32746+ int err;
32747+
32748+ err = kern_path(name, flags, path);
32749+ if (!err && d_is_positive(path->dentry))
32750+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
32751+ return err;
32752+}
32753+
32754+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
32755+ struct dentry *parent, int len)
32756+{
32757+ struct path path = {
32758+ .mnt = NULL
32759+ };
32760+
32761+ path.dentry = lookup_one_len_unlocked(name, parent, len);
32762+ if (IS_ERR(path.dentry))
32763+ goto out;
32764+ if (d_is_positive(path.dentry))
32765+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32766+
32767+out:
32768+ AuTraceErrPtr(path.dentry);
32769+ return path.dentry;
32770+}
32771+
32772+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
32773+ int len)
32774+{
32775+ struct path path = {
32776+ .mnt = NULL
32777+ };
32778+
32779+ /* VFS checks it too, but by WARN_ON_ONCE() */
32780+ IMustLock(d_inode(parent));
32781+
32782+ path.dentry = lookup_one_len(name, parent, len);
32783+ if (IS_ERR(path.dentry))
32784+ goto out;
32785+ if (d_is_positive(path.dentry))
32786+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32787+
32788+out:
32789+ AuTraceErrPtr(path.dentry);
32790+ return path.dentry;
32791+}
32792+
32793+void vfsub_call_lkup_one(void *args)
32794+{
32795+ struct vfsub_lkup_one_args *a = args;
32796+ *a->errp = vfsub_lkup_one(a->name, a->parent);
32797+}
32798+
32799+/* ---------------------------------------------------------------------- */
32800+
32801+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
32802+ struct dentry *d2, struct au_hinode *hdir2)
32803+{
32804+ struct dentry *d;
32805+
32806+ lockdep_off();
32807+ d = lock_rename(d1, d2);
32808+ lockdep_on();
32809+ au_hn_suspend(hdir1);
32810+ if (hdir1 != hdir2)
32811+ au_hn_suspend(hdir2);
32812+
32813+ return d;
32814+}
32815+
32816+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
32817+ struct dentry *d2, struct au_hinode *hdir2)
32818+{
32819+ au_hn_resume(hdir1);
32820+ if (hdir1 != hdir2)
32821+ au_hn_resume(hdir2);
32822+ lockdep_off();
32823+ unlock_rename(d1, d2);
32824+ lockdep_on();
32825+}
32826+
32827+/* ---------------------------------------------------------------------- */
32828+
32829+int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
32830+{
32831+ int err;
32832+ struct dentry *d;
32833+
32834+ IMustLock(dir);
32835+
32836+ d = path->dentry;
32837+ path->dentry = d->d_parent;
32838+ err = security_path_mknod(path, d, mode, 0);
32839+ path->dentry = d;
32840+ if (unlikely(err))
32841+ goto out;
32842+
32843+ lockdep_off();
32844+ err = vfs_create(dir, path->dentry, mode, want_excl);
32845+ lockdep_on();
32846+ if (!err) {
32847+ struct path tmp = *path;
32848+ int did;
32849+
32850+ vfsub_update_h_iattr(&tmp, &did);
32851+ if (did) {
32852+ tmp.dentry = path->dentry->d_parent;
32853+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32854+ }
32855+ /*ignore*/
32856+ }
32857+
32858+out:
32859+ return err;
32860+}
32861+
32862+int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
32863+{
32864+ int err;
32865+ struct dentry *d;
32866+
32867+ IMustLock(dir);
32868+
32869+ d = path->dentry;
32870+ path->dentry = d->d_parent;
32871+ err = security_path_symlink(path, d, symname);
32872+ path->dentry = d;
32873+ if (unlikely(err))
32874+ goto out;
32875+
32876+ lockdep_off();
32877+ err = vfs_symlink(dir, path->dentry, symname);
32878+ lockdep_on();
32879+ if (!err) {
32880+ struct path tmp = *path;
32881+ int did;
32882+
32883+ vfsub_update_h_iattr(&tmp, &did);
32884+ if (did) {
32885+ tmp.dentry = path->dentry->d_parent;
32886+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32887+ }
32888+ /*ignore*/
32889+ }
32890+
32891+out:
32892+ return err;
32893+}
32894+
32895+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
32896+{
32897+ int err;
32898+ struct dentry *d;
32899+
32900+ IMustLock(dir);
32901+
32902+ d = path->dentry;
32903+ path->dentry = d->d_parent;
32904+ err = security_path_mknod(path, d, mode, new_encode_dev(dev));
32905+ path->dentry = d;
32906+ if (unlikely(err))
32907+ goto out;
32908+
32909+ lockdep_off();
32910+ err = vfs_mknod(dir, path->dentry, mode, dev);
32911+ lockdep_on();
32912+ if (!err) {
32913+ struct path tmp = *path;
32914+ int did;
32915+
32916+ vfsub_update_h_iattr(&tmp, &did);
32917+ if (did) {
32918+ tmp.dentry = path->dentry->d_parent;
32919+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32920+ }
32921+ /*ignore*/
32922+ }
32923+
32924+out:
32925+ return err;
32926+}
32927+
32928+static int au_test_nlink(struct inode *inode)
32929+{
32930+ const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
32931+
32932+ if (!au_test_fs_no_limit_nlink(inode->i_sb)
32933+ || inode->i_nlink < link_max)
32934+ return 0;
32935+ return -EMLINK;
32936+}
32937+
32938+int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
32939+ struct inode **delegated_inode)
32940+{
32941+ int err;
32942+ struct dentry *d;
32943+
32944+ IMustLock(dir);
32945+
32946+ err = au_test_nlink(d_inode(src_dentry));
32947+ if (unlikely(err))
32948+ return err;
32949+
32950+ /* we don't call may_linkat() */
32951+ d = path->dentry;
32952+ path->dentry = d->d_parent;
32953+ err = security_path_link(src_dentry, path, d);
32954+ path->dentry = d;
32955+ if (unlikely(err))
32956+ goto out;
32957+
32958+ lockdep_off();
32959+ err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
32960+ lockdep_on();
32961+ if (!err) {
32962+ struct path tmp = *path;
32963+ int did;
32964+
32965+ /* fuse has different memory inode for the same inumber */
32966+ vfsub_update_h_iattr(&tmp, &did);
32967+ if (did) {
32968+ tmp.dentry = path->dentry->d_parent;
32969+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32970+ tmp.dentry = src_dentry;
32971+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32972+ }
32973+ /*ignore*/
32974+ }
32975+
32976+out:
32977+ return err;
32978+}
32979+
32980+int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
32981+ struct inode *dir, struct path *path,
32982+ struct inode **delegated_inode, unsigned int flags)
32983+{
32984+ int err;
32985+ struct path tmp = {
32986+ .mnt = path->mnt
32987+ };
32988+ struct dentry *d;
32989+
32990+ IMustLock(dir);
32991+ IMustLock(src_dir);
32992+
32993+ d = path->dentry;
32994+ path->dentry = d->d_parent;
32995+ tmp.dentry = src_dentry->d_parent;
32996+ err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
32997+ path->dentry = d;
32998+ if (unlikely(err))
32999+ goto out;
33000+
33001+ lockdep_off();
33002+ err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
33003+ delegated_inode, flags);
33004+ lockdep_on();
33005+ if (!err) {
33006+ int did;
33007+
33008+ tmp.dentry = d->d_parent;
33009+ vfsub_update_h_iattr(&tmp, &did);
33010+ if (did) {
33011+ tmp.dentry = src_dentry;
33012+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33013+ tmp.dentry = src_dentry->d_parent;
33014+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33015+ }
33016+ /*ignore*/
33017+ }
33018+
33019+out:
33020+ return err;
33021+}
33022+
33023+int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33024+{
33025+ int err;
33026+ struct dentry *d;
33027+
33028+ IMustLock(dir);
33029+
33030+ d = path->dentry;
33031+ path->dentry = d->d_parent;
33032+ err = security_path_mkdir(path, d, mode);
33033+ path->dentry = d;
33034+ if (unlikely(err))
33035+ goto out;
33036+
33037+ lockdep_off();
33038+ err = vfs_mkdir(dir, path->dentry, mode);
33039+ lockdep_on();
33040+ if (!err) {
33041+ struct path tmp = *path;
33042+ int did;
33043+
33044+ vfsub_update_h_iattr(&tmp, &did);
33045+ if (did) {
33046+ tmp.dentry = path->dentry->d_parent;
33047+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33048+ }
33049+ /*ignore*/
33050+ }
33051+
33052+out:
33053+ return err;
33054+}
33055+
33056+int vfsub_rmdir(struct inode *dir, struct path *path)
33057+{
33058+ int err;
33059+ struct dentry *d;
33060+
33061+ IMustLock(dir);
33062+
33063+ d = path->dentry;
33064+ path->dentry = d->d_parent;
33065+ err = security_path_rmdir(path, d);
33066+ path->dentry = d;
33067+ if (unlikely(err))
33068+ goto out;
33069+
33070+ lockdep_off();
33071+ err = vfs_rmdir(dir, path->dentry);
33072+ lockdep_on();
33073+ if (!err) {
33074+ struct path tmp = {
33075+ .dentry = path->dentry->d_parent,
33076+ .mnt = path->mnt
33077+ };
33078+
33079+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33080+ }
33081+
33082+out:
33083+ return err;
33084+}
33085+
33086+/* ---------------------------------------------------------------------- */
33087+
33088+/* todo: support mmap_sem? */
33089+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33090+ loff_t *ppos)
33091+{
33092+ ssize_t err;
33093+
33094+ lockdep_off();
33095+ err = vfs_read(file, ubuf, count, ppos);
33096+ lockdep_on();
33097+ if (err >= 0)
33098+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33099+ return err;
33100+}
33101+
33102+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33103+ loff_t *ppos)
33104+{
33105+ ssize_t err;
33106+
33107+ lockdep_off();
33108+ err = kernel_read(file, kbuf, count, ppos);
33109+ lockdep_on();
33110+ AuTraceErr(err);
33111+ if (err >= 0)
33112+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33113+ return err;
33114+}
33115+
33116+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33117+ loff_t *ppos)
33118+{
33119+ ssize_t err;
33120+
33121+ lockdep_off();
33122+ err = vfs_write(file, ubuf, count, ppos);
33123+ lockdep_on();
33124+ if (err >= 0)
33125+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33126+ return err;
33127+}
33128+
33129+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33130+{
33131+ ssize_t err;
33132+
33133+ lockdep_off();
33134+ err = kernel_write(file, kbuf, count, ppos);
33135+ lockdep_on();
33136+ if (err >= 0)
33137+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33138+ return err;
33139+}
33140+
33141+int vfsub_flush(struct file *file, fl_owner_t id)
33142+{
33143+ int err;
33144+
33145+ err = 0;
33146+ if (file->f_op->flush) {
33147+ if (!au_test_nfs(file->f_path.dentry->d_sb))
33148+ err = file->f_op->flush(file, id);
33149+ else {
33150+ lockdep_off();
33151+ err = file->f_op->flush(file, id);
33152+ lockdep_on();
33153+ }
33154+ if (!err)
33155+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33156+ /*ignore*/
33157+ }
33158+ return err;
33159+}
33160+
33161+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
33162+{
33163+ int err;
33164+
33165+ AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
33166+
33167+ lockdep_off();
33168+ err = iterate_dir(file, ctx);
33169+ lockdep_on();
33170+ if (err >= 0)
33171+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33172+
33173+ return err;
33174+}
33175+
33176+long vfsub_splice_to(struct file *in, loff_t *ppos,
33177+ struct pipe_inode_info *pipe, size_t len,
33178+ unsigned int flags)
33179+{
33180+ long err;
33181+
33182+ lockdep_off();
33183+ err = do_splice_to(in, ppos, pipe, len, flags);
33184+ lockdep_on();
33185+ file_accessed(in);
33186+ if (err >= 0)
33187+ vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33188+ return err;
33189+}
33190+
33191+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33192+ loff_t *ppos, size_t len, unsigned int flags)
33193+{
33194+ long err;
33195+
33196+ lockdep_off();
33197+ err = do_splice_from(pipe, out, ppos, len, flags);
33198+ lockdep_on();
33199+ if (err >= 0)
33200+ vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33201+ return err;
33202+}
33203+
33204+int vfsub_fsync(struct file *file, struct path *path, int datasync)
33205+{
33206+ int err;
33207+
33208+ /* file can be NULL */
33209+ lockdep_off();
33210+ err = vfs_fsync(file, datasync);
33211+ lockdep_on();
33212+ if (!err) {
33213+ if (!path) {
33214+ AuDebugOn(!file);
33215+ path = &file->f_path;
33216+ }
33217+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33218+ }
33219+ return err;
33220+}
33221+
33222+/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33223+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33224+ struct file *h_file)
33225+{
33226+ int err;
33227+ struct inode *h_inode;
33228+ struct super_block *h_sb;
33229+
33230+ if (!h_file) {
33231+ err = vfsub_truncate(h_path, length);
33232+ goto out;
33233+ }
33234+
33235+ h_inode = d_inode(h_path->dentry);
33236+ h_sb = h_inode->i_sb;
33237+ lockdep_off();
33238+ sb_start_write(h_sb);
33239+ lockdep_on();
33240+ err = locks_verify_truncate(h_inode, h_file, length);
33241+ if (!err)
33242+ err = security_path_truncate(h_path);
33243+ if (!err) {
33244+ lockdep_off();
33245+ err = do_truncate(h_path->dentry, length, attr, h_file);
33246+ lockdep_on();
33247+ }
33248+ lockdep_off();
33249+ sb_end_write(h_sb);
33250+ lockdep_on();
33251+
33252+out:
33253+ return err;
33254+}
33255+
33256+/* ---------------------------------------------------------------------- */
33257+
33258+struct au_vfsub_mkdir_args {
33259+ int *errp;
33260+ struct inode *dir;
33261+ struct path *path;
33262+ int mode;
33263+};
33264+
33265+static void au_call_vfsub_mkdir(void *args)
33266+{
33267+ struct au_vfsub_mkdir_args *a = args;
33268+ *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33269+}
33270+
33271+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33272+{
33273+ int err, do_sio, wkq_err;
33274+
33275+ do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
33276+ if (!do_sio) {
33277+ lockdep_off();
33278+ err = vfsub_mkdir(dir, path, mode);
33279+ lockdep_on();
33280+ } else {
33281+ struct au_vfsub_mkdir_args args = {
33282+ .errp = &err,
33283+ .dir = dir,
33284+ .path = path,
33285+ .mode = mode
33286+ };
33287+ wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33288+ if (unlikely(wkq_err))
33289+ err = wkq_err;
33290+ }
33291+
33292+ return err;
33293+}
33294+
33295+struct au_vfsub_rmdir_args {
33296+ int *errp;
33297+ struct inode *dir;
33298+ struct path *path;
33299+};
33300+
33301+static void au_call_vfsub_rmdir(void *args)
33302+{
33303+ struct au_vfsub_rmdir_args *a = args;
33304+ *a->errp = vfsub_rmdir(a->dir, a->path);
33305+}
33306+
33307+int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33308+{
33309+ int err, do_sio, wkq_err;
33310+
33311+ do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
33312+ if (!do_sio) {
33313+ lockdep_off();
33314+ err = vfsub_rmdir(dir, path);
33315+ lockdep_on();
33316+ } else {
33317+ struct au_vfsub_rmdir_args args = {
33318+ .errp = &err,
33319+ .dir = dir,
33320+ .path = path
33321+ };
33322+ wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33323+ if (unlikely(wkq_err))
33324+ err = wkq_err;
33325+ }
33326+
33327+ return err;
33328+}
33329+
33330+/* ---------------------------------------------------------------------- */
33331+
33332+struct notify_change_args {
33333+ int *errp;
33334+ struct path *path;
33335+ struct iattr *ia;
33336+ struct inode **delegated_inode;
33337+};
33338+
33339+static void call_notify_change(void *args)
33340+{
33341+ struct notify_change_args *a = args;
33342+ struct inode *h_inode;
33343+
33344+ h_inode = d_inode(a->path->dentry);
33345+ IMustLock(h_inode);
33346+
33347+ *a->errp = -EPERM;
33348+ if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
33349+ lockdep_off();
33350+ *a->errp = notify_change(a->path->dentry, a->ia,
33351+ a->delegated_inode);
33352+ lockdep_on();
33353+ if (!*a->errp)
33354+ vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33355+ }
33356+ AuTraceErr(*a->errp);
33357+}
33358+
33359+int vfsub_notify_change(struct path *path, struct iattr *ia,
33360+ struct inode **delegated_inode)
33361+{
33362+ int err;
33363+ struct notify_change_args args = {
33364+ .errp = &err,
33365+ .path = path,
33366+ .ia = ia,
33367+ .delegated_inode = delegated_inode
33368+ };
33369+
33370+ call_notify_change(&args);
33371+
33372+ return err;
33373+}
33374+
33375+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33376+ struct inode **delegated_inode)
33377+{
33378+ int err, wkq_err;
33379+ struct notify_change_args args = {
33380+ .errp = &err,
33381+ .path = path,
33382+ .ia = ia,
33383+ .delegated_inode = delegated_inode
33384+ };
33385+
33386+ wkq_err = au_wkq_wait(call_notify_change, &args);
33387+ if (unlikely(wkq_err))
33388+ err = wkq_err;
33389+
33390+ return err;
33391+}
33392+
33393+/* ---------------------------------------------------------------------- */
33394+
33395+struct unlink_args {
33396+ int *errp;
33397+ struct inode *dir;
33398+ struct path *path;
33399+ struct inode **delegated_inode;
33400+};
33401+
33402+static void call_unlink(void *args)
33403+{
33404+ struct unlink_args *a = args;
33405+ struct dentry *d = a->path->dentry;
33406+ struct inode *h_inode;
33407+ const int stop_sillyrename = (au_test_nfs(d->d_sb)
33408+ && au_dcount(d) == 1);
33409+
33410+ IMustLock(a->dir);
33411+
33412+ a->path->dentry = d->d_parent;
33413+ *a->errp = security_path_unlink(a->path, d);
33414+ a->path->dentry = d;
33415+ if (unlikely(*a->errp))
33416+ return;
33417+
33418+ if (!stop_sillyrename)
33419+ dget(d);
33420+ h_inode = NULL;
33421+ if (d_is_positive(d)) {
33422+ h_inode = d_inode(d);
33423+ ihold(h_inode);
33424+ }
33425+
33426+ lockdep_off();
33427+ *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
33428+ lockdep_on();
33429+ if (!*a->errp) {
33430+ struct path tmp = {
33431+ .dentry = d->d_parent,
33432+ .mnt = a->path->mnt
33433+ };
33434+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33435+ }
33436+
33437+ if (!stop_sillyrename)
33438+ dput(d);
33439+ if (h_inode)
33440+ iput(h_inode);
33441+
33442+ AuTraceErr(*a->errp);
33443+}
33444+
33445+/*
33446+ * @dir: must be locked.
33447+ * @dentry: target dentry.
33448+ */
33449+int vfsub_unlink(struct inode *dir, struct path *path,
33450+ struct inode **delegated_inode, int force)
33451+{
33452+ int err;
33453+ struct unlink_args args = {
33454+ .errp = &err,
33455+ .dir = dir,
33456+ .path = path,
33457+ .delegated_inode = delegated_inode
33458+ };
33459+
33460+ if (!force)
33461+ call_unlink(&args);
33462+ else {
33463+ int wkq_err;
33464+
33465+ wkq_err = au_wkq_wait(call_unlink, &args);
33466+ if (unlikely(wkq_err))
33467+ err = wkq_err;
33468+ }
33469+
33470+ return err;
33471+}
33472diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33473--- /usr/share/empty/fs/aufs/vfsub.h 1970-01-01 01:00:00.000000000 +0100
33474+++ linux/fs/aufs/vfsub.h 2021-02-22 22:51:59.061411920 +0100
33475@@ -0,0 +1,354 @@
33476+/* SPDX-License-Identifier: GPL-2.0 */
33477+/*
33478+ * Copyright (C) 2005-2020 Junjiro R. Okajima
33479+ *
33480+ * This program, aufs is free software; you can redistribute it and/or modify
33481+ * it under the terms of the GNU General Public License as published by
33482+ * the Free Software Foundation; either version 2 of the License, or
33483+ * (at your option) any later version.
33484+ *
33485+ * This program is distributed in the hope that it will be useful,
33486+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33487+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33488+ * GNU General Public License for more details.
33489+ *
33490+ * You should have received a copy of the GNU General Public License
33491+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
33492+ */
33493+
33494+/*
33495+ * sub-routines for VFS
33496+ */
33497+
33498+#ifndef __AUFS_VFSUB_H__
33499+#define __AUFS_VFSUB_H__
33500+
33501+#ifdef __KERNEL__
33502+
33503+#include <linux/fs.h>
33504+#include <linux/mount.h>
33505+#include <linux/posix_acl.h>
33506+#include <linux/xattr.h>
33507+#include "debug.h"
33508+
33509+/* copied from linux/fs/internal.h */
33510+/* todo: BAD approach!! */
33511+extern void __mnt_drop_write(struct vfsmount *);
33512+extern struct file *alloc_empty_file(int, const struct cred *);
33513+
33514+/* ---------------------------------------------------------------------- */
33515+
33516+/* lock subclass for lower inode */
33517+/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33518+/* reduce? gave up. */
33519+enum {
33520+ AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
33521+ AuLsc_I_PARENT, /* lower inode, parent first */
33522+ AuLsc_I_PARENT2, /* copyup dirs */
33523+ AuLsc_I_PARENT3, /* copyup wh */
33524+ AuLsc_I_CHILD,
33525+ AuLsc_I_CHILD2,
33526+ AuLsc_I_End
33527+};
33528+
33529+/* to debug easier, do not make them inlined functions */
33530+#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
33531+#define IMustLock(i) AuDebugOn(!inode_is_locked(i))
33532+
33533+/* ---------------------------------------------------------------------- */
33534+
33535+static inline void vfsub_drop_nlink(struct inode *inode)
33536+{
33537+ AuDebugOn(!inode->i_nlink);
33538+ drop_nlink(inode);
33539+}
33540+
33541+static inline void vfsub_dead_dir(struct inode *inode)
33542+{
33543+ AuDebugOn(!S_ISDIR(inode->i_mode));
33544+ inode->i_flags |= S_DEAD;
33545+ clear_nlink(inode);
33546+}
33547+
33548+static inline int vfsub_native_ro(struct inode *inode)
33549+{
33550+ return sb_rdonly(inode->i_sb)
33551+ || IS_RDONLY(inode)
33552+ /* || IS_APPEND(inode) */
33553+ || IS_IMMUTABLE(inode);
33554+}
33555+
33556+#ifdef CONFIG_AUFS_BR_FUSE
33557+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33558+#else
33559+AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33560+#endif
33561+
33562+int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
33563+
33564+/* ---------------------------------------------------------------------- */
33565+
33566+int vfsub_update_h_iattr(struct path *h_path, int *did);
33567+struct file *vfsub_dentry_open(struct path *path, int flags);
33568+struct file *vfsub_filp_open(const char *path, int oflags, int mode);
33569+struct au_branch;
33570+struct vfsub_aopen_args {
33571+ struct file *file;
33572+ unsigned int open_flag;
33573+ umode_t create_mode;
33574+ struct au_branch *br;
33575+};
33576+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33577+ struct vfsub_aopen_args *args);
33578+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
33579+
33580+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33581+ struct dentry *parent, int len);
33582+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
33583+ int len);
33584+
33585+struct vfsub_lkup_one_args {
33586+ struct dentry **errp;
33587+ struct qstr *name;
33588+ struct dentry *parent;
33589+};
33590+
33591+static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33592+ struct dentry *parent)
33593+{
33594+ return vfsub_lookup_one_len(name->name, parent, name->len);
33595+}
33596+
33597+void vfsub_call_lkup_one(void *args);
33598+
33599+/* ---------------------------------------------------------------------- */
33600+
33601+static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33602+{
33603+ int err;
33604+
33605+ lockdep_off();
33606+ err = mnt_want_write(mnt);
33607+ lockdep_on();
33608+ return err;
33609+}
33610+
33611+static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33612+{
33613+ lockdep_off();
33614+ mnt_drop_write(mnt);
33615+ lockdep_on();
33616+}
33617+
33618+#if 0 /* reserved */
33619+static inline void vfsub_mnt_drop_write_file(struct file *file)
33620+{
33621+ lockdep_off();
33622+ mnt_drop_write_file(file);
33623+ lockdep_on();
33624+}
33625+#endif
33626+
33627+/* ---------------------------------------------------------------------- */
33628+
33629+struct au_hinode;
33630+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33631+ struct dentry *d2, struct au_hinode *hdir2);
33632+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33633+ struct dentry *d2, struct au_hinode *hdir2);
33634+
33635+int vfsub_create(struct inode *dir, struct path *path, int mode,
33636+ bool want_excl);
33637+int vfsub_symlink(struct inode *dir, struct path *path,
33638+ const char *symname);
33639+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33640+int vfsub_link(struct dentry *src_dentry, struct inode *dir,
33641+ struct path *path, struct inode **delegated_inode);
33642+int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
33643+ struct inode *hdir, struct path *path,
33644+ struct inode **delegated_inode, unsigned int flags);
33645+int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33646+int vfsub_rmdir(struct inode *dir, struct path *path);
33647+
33648+/* ---------------------------------------------------------------------- */
33649+
33650+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33651+ loff_t *ppos);
33652+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33653+ loff_t *ppos);
33654+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33655+ loff_t *ppos);
33656+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33657+ loff_t *ppos);
33658+int vfsub_flush(struct file *file, fl_owner_t id);
33659+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33660+
33661+static inline loff_t vfsub_f_size_read(struct file *file)
33662+{
33663+ return i_size_read(file_inode(file));
33664+}
33665+
33666+static inline unsigned int vfsub_file_flags(struct file *file)
33667+{
33668+ unsigned int flags;
33669+
33670+ spin_lock(&file->f_lock);
33671+ flags = file->f_flags;
33672+ spin_unlock(&file->f_lock);
33673+
33674+ return flags;
33675+}
33676+
33677+static inline int vfsub_file_execed(struct file *file)
33678+{
33679+ /* todo: direct access f_flags */
33680+ return !!(vfsub_file_flags(file) & __FMODE_EXEC);
33681+}
33682+
33683+#if 0 /* reserved */
33684+static inline void vfsub_file_accessed(struct file *h_file)
33685+{
33686+ file_accessed(h_file);
33687+ vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
33688+}
33689+#endif
33690+
33691+#if 0 /* reserved */
33692+static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
33693+ struct dentry *h_dentry)
33694+{
33695+ struct path h_path = {
33696+ .dentry = h_dentry,
33697+ .mnt = h_mnt
33698+ };
33699+ touch_atime(&h_path);
33700+ vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
33701+}
33702+#endif
33703+
33704+static inline int vfsub_update_time(struct inode *h_inode,
33705+ struct timespec64 *ts, int flags)
33706+{
33707+ return update_time(h_inode, ts, flags);
33708+ /* no vfsub_update_h_iattr() since we don't have struct path */
33709+}
33710+
33711+#ifdef CONFIG_FS_POSIX_ACL
33712+static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
33713+{
33714+ int err;
33715+
33716+ err = posix_acl_chmod(h_inode, h_mode);
33717+ if (err == -EOPNOTSUPP)
33718+ err = 0;
33719+ return err;
33720+}
33721+#else
33722+AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
33723+#endif
33724+
33725+long vfsub_splice_to(struct file *in, loff_t *ppos,
33726+ struct pipe_inode_info *pipe, size_t len,
33727+ unsigned int flags);
33728+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33729+ loff_t *ppos, size_t len, unsigned int flags);
33730+
33731+static inline long vfsub_truncate(struct path *path, loff_t length)
33732+{
33733+ long err;
33734+
33735+ lockdep_off();
33736+ err = vfs_truncate(path, length);
33737+ lockdep_on();
33738+ return err;
33739+}
33740+
33741+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33742+ struct file *h_file);
33743+int vfsub_fsync(struct file *file, struct path *path, int datasync);
33744+
33745+/*
33746+ * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
33747+ * ioctl.
33748+ */
33749+static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
33750+ loff_t len)
33751+{
33752+ loff_t err;
33753+
33754+ lockdep_off();
33755+ err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
33756+ lockdep_on();
33757+
33758+ return err;
33759+}
33760+
33761+/* copy_file_range(2) is a systemcall */
33762+static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
33763+ struct file *dst, loff_t dst_pos,
33764+ size_t len, unsigned int flags)
33765+{
33766+ ssize_t ssz;
33767+
33768+ lockdep_off();
33769+ ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
33770+ lockdep_on();
33771+
33772+ return ssz;
33773+}
33774+
33775+/* ---------------------------------------------------------------------- */
33776+
33777+static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
33778+{
33779+ loff_t err;
33780+
33781+ lockdep_off();
33782+ err = vfs_llseek(file, offset, origin);
33783+ lockdep_on();
33784+ return err;
33785+}
33786+
33787+/* ---------------------------------------------------------------------- */
33788+
33789+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
33790+int vfsub_sio_rmdir(struct inode *dir, struct path *path);
33791+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33792+ struct inode **delegated_inode);
33793+int vfsub_notify_change(struct path *path, struct iattr *ia,
33794+ struct inode **delegated_inode);
33795+int vfsub_unlink(struct inode *dir, struct path *path,
33796+ struct inode **delegated_inode, int force);
33797+
33798+static inline int vfsub_getattr(const struct path *path, struct kstat *st)
33799+{
33800+ return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
33801+}
33802+
33803+/* ---------------------------------------------------------------------- */
33804+
33805+static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
33806+ const void *value, size_t size, int flags)
33807+{
33808+ int err;
33809+
33810+ lockdep_off();
33811+ err = vfs_setxattr(dentry, name, value, size, flags);
33812+ lockdep_on();
33813+
33814+ return err;
33815+}
33816+
33817+static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
33818+{
33819+ int err;
33820+
33821+ lockdep_off();
33822+ err = vfs_removexattr(dentry, name);
33823+ lockdep_on();
33824+
33825+ return err;
33826+}
33827+
33828+#endif /* __KERNEL__ */
33829+#endif /* __AUFS_VFSUB_H__ */
33830diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
33831--- /usr/share/empty/fs/aufs/wbr_policy.c 1970-01-01 01:00:00.000000000 +0100
33832+++ linux/fs/aufs/wbr_policy.c 2021-02-22 22:51:59.061411920 +0100
33833@@ -0,0 +1,830 @@
33834+// SPDX-License-Identifier: GPL-2.0
33835+/*
33836+ * Copyright (C) 2005-2020 Junjiro R. Okajima
33837+ *
33838+ * This program, aufs is free software; you can redistribute it and/or modify
33839+ * it under the terms of the GNU General Public License as published by
33840+ * the Free Software Foundation; either version 2 of the License, or
33841+ * (at your option) any later version.
33842+ *
33843+ * This program is distributed in the hope that it will be useful,
33844+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33845+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33846+ * GNU General Public License for more details.
33847+ *
33848+ * You should have received a copy of the GNU General Public License
33849+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
33850+ */
33851+
33852+/*
33853+ * policies for selecting one among multiple writable branches
33854+ */
33855+
33856+#include <linux/statfs.h>
33857+#include "aufs.h"
33858+
33859+/* subset of cpup_attr() */
33860+static noinline_for_stack
33861+int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
33862+{
33863+ int err, sbits;
33864+ struct iattr ia;
33865+ struct inode *h_isrc;
33866+
33867+ h_isrc = d_inode(h_src);
33868+ ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
33869+ ia.ia_mode = h_isrc->i_mode;
33870+ ia.ia_uid = h_isrc->i_uid;
33871+ ia.ia_gid = h_isrc->i_gid;
33872+ sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
33873+ au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
33874+ /* no delegation since it is just created */
33875+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
33876+
33877+ /* is this nfs only? */
33878+ if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
33879+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
33880+ ia.ia_mode = h_isrc->i_mode;
33881+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
33882+ }
33883+
33884+ return err;
33885+}
33886+
33887+#define AuCpdown_PARENT_OPQ 1
33888+#define AuCpdown_WHED (1 << 1)
33889+#define AuCpdown_MADE_DIR (1 << 2)
33890+#define AuCpdown_DIROPQ (1 << 3)
33891+#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
33892+#define au_fset_cpdown(flags, name) \
33893+ do { (flags) |= AuCpdown_##name; } while (0)
33894+#define au_fclr_cpdown(flags, name) \
33895+ do { (flags) &= ~AuCpdown_##name; } while (0)
33896+
33897+static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
33898+ unsigned int *flags)
33899+{
33900+ int err;
33901+ struct dentry *opq_dentry;
33902+
33903+ opq_dentry = au_diropq_create(dentry, bdst);
33904+ err = PTR_ERR(opq_dentry);
33905+ if (IS_ERR(opq_dentry))
33906+ goto out;
33907+ dput(opq_dentry);
33908+ au_fset_cpdown(*flags, DIROPQ);
33909+
33910+out:
33911+ return err;
33912+}
33913+
33914+static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
33915+ struct inode *dir, aufs_bindex_t bdst)
33916+{
33917+ int err;
33918+ struct path h_path;
33919+ struct au_branch *br;
33920+
33921+ br = au_sbr(dentry->d_sb, bdst);
33922+ h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
33923+ err = PTR_ERR(h_path.dentry);
33924+ if (IS_ERR(h_path.dentry))
33925+ goto out;
33926+
33927+ err = 0;
33928+ if (d_is_positive(h_path.dentry)) {
33929+ h_path.mnt = au_br_mnt(br);
33930+ err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
33931+ dentry);
33932+ }
33933+ dput(h_path.dentry);
33934+
33935+out:
33936+ return err;
33937+}
33938+
33939+static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
33940+ struct au_pin *pin,
33941+ struct dentry *h_parent, void *arg)
33942+{
33943+ int err, rerr;
33944+ aufs_bindex_t bopq, btop;
33945+ struct path h_path;
33946+ struct dentry *parent;
33947+ struct inode *h_dir, *h_inode, *inode, *dir;
33948+ unsigned int *flags = arg;
33949+
33950+ btop = au_dbtop(dentry);
33951+ /* dentry is di-locked */
33952+ parent = dget_parent(dentry);
33953+ dir = d_inode(parent);
33954+ h_dir = d_inode(h_parent);
33955+ AuDebugOn(h_dir != au_h_iptr(dir, bdst));
33956+ IMustLock(h_dir);
33957+
33958+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
33959+ if (unlikely(err < 0))
33960+ goto out;
33961+ h_path.dentry = au_h_dptr(dentry, bdst);
33962+ h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
33963+ err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
33964+ if (unlikely(err))
33965+ goto out_put;
33966+ au_fset_cpdown(*flags, MADE_DIR);
33967+
33968+ bopq = au_dbdiropq(dentry);
33969+ au_fclr_cpdown(*flags, WHED);
33970+ au_fclr_cpdown(*flags, DIROPQ);
33971+ if (au_dbwh(dentry) == bdst)
33972+ au_fset_cpdown(*flags, WHED);
33973+ if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
33974+ au_fset_cpdown(*flags, PARENT_OPQ);
33975+ h_inode = d_inode(h_path.dentry);
33976+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
33977+ if (au_ftest_cpdown(*flags, WHED)) {
33978+ err = au_cpdown_dir_opq(dentry, bdst, flags);
33979+ if (unlikely(err)) {
33980+ inode_unlock(h_inode);
33981+ goto out_dir;
33982+ }
33983+ }
33984+
33985+ err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
33986+ inode_unlock(h_inode);
33987+ if (unlikely(err))
33988+ goto out_opq;
33989+
33990+ if (au_ftest_cpdown(*flags, WHED)) {
33991+ err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
33992+ if (unlikely(err))
33993+ goto out_opq;
33994+ }
33995+
33996+ inode = d_inode(dentry);
33997+ if (au_ibbot(inode) < bdst)
33998+ au_set_ibbot(inode, bdst);
33999+ au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34000+ au_hi_flags(inode, /*isdir*/1));
34001+ au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
34002+ goto out; /* success */
34003+
34004+ /* revert */
34005+out_opq:
34006+ if (au_ftest_cpdown(*flags, DIROPQ)) {
34007+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
34008+ rerr = au_diropq_remove(dentry, bdst);
34009+ inode_unlock(h_inode);
34010+ if (unlikely(rerr)) {
34011+ AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34012+ dentry, bdst, rerr);
34013+ err = -EIO;
34014+ goto out;
34015+ }
34016+ }
34017+out_dir:
34018+ if (au_ftest_cpdown(*flags, MADE_DIR)) {
34019+ rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34020+ if (unlikely(rerr)) {
34021+ AuIOErr("failed removing %pd b%d (%d)\n",
34022+ dentry, bdst, rerr);
34023+ err = -EIO;
34024+ }
34025+ }
34026+out_put:
34027+ au_set_h_dptr(dentry, bdst, NULL);
34028+ if (au_dbbot(dentry) == bdst)
34029+ au_update_dbbot(dentry);
34030+out:
34031+ dput(parent);
34032+ return err;
34033+}
34034+
34035+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34036+{
34037+ int err;
34038+ unsigned int flags;
34039+
34040+ flags = 0;
34041+ err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
34042+
34043+ return err;
34044+}
34045+
34046+/* ---------------------------------------------------------------------- */
34047+
34048+/* policies for create */
34049+
34050+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
34051+{
34052+ int err, i, j, ndentry;
34053+ aufs_bindex_t bopq;
34054+ struct au_dcsub_pages dpages;
34055+ struct au_dpage *dpage;
34056+ struct dentry **dentries, *parent, *d;
34057+
34058+ err = au_dpages_init(&dpages, GFP_NOFS);
34059+ if (unlikely(err))
34060+ goto out;
34061+ parent = dget_parent(dentry);
34062+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
34063+ if (unlikely(err))
34064+ goto out_free;
34065+
34066+ err = bindex;
34067+ for (i = 0; i < dpages.ndpage; i++) {
34068+ dpage = dpages.dpages + i;
34069+ dentries = dpage->dentries;
34070+ ndentry = dpage->ndentry;
34071+ for (j = 0; j < ndentry; j++) {
34072+ d = dentries[j];
34073+ di_read_lock_parent2(d, !AuLock_IR);
34074+ bopq = au_dbdiropq(d);
34075+ di_read_unlock(d, !AuLock_IR);
34076+ if (bopq >= 0 && bopq < err)
34077+ err = bopq;
34078+ }
34079+ }
34080+
34081+out_free:
34082+ dput(parent);
34083+ au_dpages_free(&dpages);
34084+out:
34085+ return err;
34086+}
34087+
34088+static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34089+{
34090+ for (; bindex >= 0; bindex--)
34091+ if (!au_br_rdonly(au_sbr(sb, bindex)))
34092+ return bindex;
34093+ return -EROFS;
34094+}
34095+
34096+/* top down parent */
34097+static int au_wbr_create_tdp(struct dentry *dentry,
34098+ unsigned int flags __maybe_unused)
34099+{
34100+ int err;
34101+ aufs_bindex_t btop, bindex;
34102+ struct super_block *sb;
34103+ struct dentry *parent, *h_parent;
34104+
34105+ sb = dentry->d_sb;
34106+ btop = au_dbtop(dentry);
34107+ err = btop;
34108+ if (!au_br_rdonly(au_sbr(sb, btop)))
34109+ goto out;
34110+
34111+ err = -EROFS;
34112+ parent = dget_parent(dentry);
34113+ for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
34114+ h_parent = au_h_dptr(parent, bindex);
34115+ if (!h_parent || d_is_negative(h_parent))
34116+ continue;
34117+
34118+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34119+ err = bindex;
34120+ break;
34121+ }
34122+ }
34123+ dput(parent);
34124+
34125+ /* bottom up here */
34126+ if (unlikely(err < 0)) {
34127+ err = au_wbr_bu(sb, btop - 1);
34128+ if (err >= 0)
34129+ err = au_wbr_nonopq(dentry, err);
34130+ }
34131+
34132+out:
34133+ AuDbg("b%d\n", err);
34134+ return err;
34135+}
34136+
34137+/* ---------------------------------------------------------------------- */
34138+
34139+/* an exception for the policy other than tdp */
34140+static int au_wbr_create_exp(struct dentry *dentry)
34141+{
34142+ int err;
34143+ aufs_bindex_t bwh, bdiropq;
34144+ struct dentry *parent;
34145+
34146+ err = -1;
34147+ bwh = au_dbwh(dentry);
34148+ parent = dget_parent(dentry);
34149+ bdiropq = au_dbdiropq(parent);
34150+ if (bwh >= 0) {
34151+ if (bdiropq >= 0)
34152+ err = min(bdiropq, bwh);
34153+ else
34154+ err = bwh;
34155+ AuDbg("%d\n", err);
34156+ } else if (bdiropq >= 0) {
34157+ err = bdiropq;
34158+ AuDbg("%d\n", err);
34159+ }
34160+ dput(parent);
34161+
34162+ if (err >= 0)
34163+ err = au_wbr_nonopq(dentry, err);
34164+
34165+ if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34166+ err = -1;
34167+
34168+ AuDbg("%d\n", err);
34169+ return err;
34170+}
34171+
34172+/* ---------------------------------------------------------------------- */
34173+
34174+/* round robin */
34175+static int au_wbr_create_init_rr(struct super_block *sb)
34176+{
34177+ int err;
34178+
34179+ err = au_wbr_bu(sb, au_sbbot(sb));
34180+ atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
34181+ /* smp_mb(); */
34182+
34183+ AuDbg("b%d\n", err);
34184+ return err;
34185+}
34186+
34187+static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
34188+{
34189+ int err, nbr;
34190+ unsigned int u;
34191+ aufs_bindex_t bindex, bbot;
34192+ struct super_block *sb;
34193+ atomic_t *next;
34194+
34195+ err = au_wbr_create_exp(dentry);
34196+ if (err >= 0)
34197+ goto out;
34198+
34199+ sb = dentry->d_sb;
34200+ next = &au_sbi(sb)->si_wbr_rr_next;
34201+ bbot = au_sbbot(sb);
34202+ nbr = bbot + 1;
34203+ for (bindex = 0; bindex <= bbot; bindex++) {
34204+ if (!au_ftest_wbr(flags, DIR)) {
34205+ err = atomic_dec_return(next) + 1;
34206+ /* modulo for 0 is meaningless */
34207+ if (unlikely(!err))
34208+ err = atomic_dec_return(next) + 1;
34209+ } else
34210+ err = atomic_read(next);
34211+ AuDbg("%d\n", err);
34212+ u = err;
34213+ err = u % nbr;
34214+ AuDbg("%d\n", err);
34215+ if (!au_br_rdonly(au_sbr(sb, err)))
34216+ break;
34217+ err = -EROFS;
34218+ }
34219+
34220+ if (err >= 0)
34221+ err = au_wbr_nonopq(dentry, err);
34222+
34223+out:
34224+ AuDbg("%d\n", err);
34225+ return err;
34226+}
34227+
34228+/* ---------------------------------------------------------------------- */
34229+
34230+/* most free space */
34231+static void au_mfs(struct dentry *dentry, struct dentry *parent)
34232+{
34233+ struct super_block *sb;
34234+ struct au_branch *br;
34235+ struct au_wbr_mfs *mfs;
34236+ struct dentry *h_parent;
34237+ aufs_bindex_t bindex, bbot;
34238+ int err;
34239+ unsigned long long b, bavail;
34240+ struct path h_path;
34241+ /* reduce the stack usage */
34242+ struct kstatfs *st;
34243+
34244+ st = kmalloc(sizeof(*st), GFP_NOFS);
34245+ if (unlikely(!st)) {
34246+ AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34247+ return;
34248+ }
34249+
34250+ bavail = 0;
34251+ sb = dentry->d_sb;
34252+ mfs = &au_sbi(sb)->si_wbr_mfs;
34253+ MtxMustLock(&mfs->mfs_lock);
34254+ mfs->mfs_bindex = -EROFS;
34255+ mfs->mfsrr_bytes = 0;
34256+ if (!parent) {
34257+ bindex = 0;
34258+ bbot = au_sbbot(sb);
34259+ } else {
34260+ bindex = au_dbtop(parent);
34261+ bbot = au_dbtaildir(parent);
34262+ }
34263+
34264+ for (; bindex <= bbot; bindex++) {
34265+ if (parent) {
34266+ h_parent = au_h_dptr(parent, bindex);
34267+ if (!h_parent || d_is_negative(h_parent))
34268+ continue;
34269+ }
34270+ br = au_sbr(sb, bindex);
34271+ if (au_br_rdonly(br))
34272+ continue;
34273+
34274+ /* sb->s_root for NFS is unreliable */
34275+ h_path.mnt = au_br_mnt(br);
34276+ h_path.dentry = h_path.mnt->mnt_root;
34277+ err = vfs_statfs(&h_path, st);
34278+ if (unlikely(err)) {
34279+ AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34280+ continue;
34281+ }
34282+
34283+ /* when the available size is equal, select the lower one */
34284+ BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34285+ || sizeof(b) < sizeof(st->f_bsize));
34286+ b = st->f_bavail * st->f_bsize;
34287+ br->br_wbr->wbr_bytes = b;
34288+ if (b >= bavail) {
34289+ bavail = b;
34290+ mfs->mfs_bindex = bindex;
34291+ mfs->mfs_jiffy = jiffies;
34292+ }
34293+ }
34294+
34295+ mfs->mfsrr_bytes = bavail;
34296+ AuDbg("b%d\n", mfs->mfs_bindex);
34297+ au_kfree_rcu(st);
34298+}
34299+
34300+static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
34301+{
34302+ int err;
34303+ struct dentry *parent;
34304+ struct super_block *sb;
34305+ struct au_wbr_mfs *mfs;
34306+
34307+ err = au_wbr_create_exp(dentry);
34308+ if (err >= 0)
34309+ goto out;
34310+
34311+ sb = dentry->d_sb;
34312+ parent = NULL;
34313+ if (au_ftest_wbr(flags, PARENT))
34314+ parent = dget_parent(dentry);
34315+ mfs = &au_sbi(sb)->si_wbr_mfs;
34316+ mutex_lock(&mfs->mfs_lock);
34317+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34318+ || mfs->mfs_bindex < 0
34319+ || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
34320+ au_mfs(dentry, parent);
34321+ mutex_unlock(&mfs->mfs_lock);
34322+ err = mfs->mfs_bindex;
34323+ dput(parent);
34324+
34325+ if (err >= 0)
34326+ err = au_wbr_nonopq(dentry, err);
34327+
34328+out:
34329+ AuDbg("b%d\n", err);
34330+ return err;
34331+}
34332+
34333+static int au_wbr_create_init_mfs(struct super_block *sb)
34334+{
34335+ struct au_wbr_mfs *mfs;
34336+
34337+ mfs = &au_sbi(sb)->si_wbr_mfs;
34338+ mutex_init(&mfs->mfs_lock);
34339+ mfs->mfs_jiffy = 0;
34340+ mfs->mfs_bindex = -EROFS;
34341+
34342+ return 0;
34343+}
34344+
34345+static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34346+{
34347+ mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34348+ return 0;
34349+}
34350+
34351+/* ---------------------------------------------------------------------- */
34352+
34353+/* top down regardless parent, and then mfs */
34354+static int au_wbr_create_tdmfs(struct dentry *dentry,
34355+ unsigned int flags __maybe_unused)
34356+{
34357+ int err;
34358+ aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34359+ unsigned long long watermark;
34360+ struct super_block *sb;
34361+ struct au_wbr_mfs *mfs;
34362+ struct au_branch *br;
34363+ struct dentry *parent;
34364+
34365+ sb = dentry->d_sb;
34366+ mfs = &au_sbi(sb)->si_wbr_mfs;
34367+ mutex_lock(&mfs->mfs_lock);
34368+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34369+ || mfs->mfs_bindex < 0)
34370+ au_mfs(dentry, /*parent*/NULL);
34371+ watermark = mfs->mfsrr_watermark;
34372+ bmfs = mfs->mfs_bindex;
34373+ mutex_unlock(&mfs->mfs_lock);
34374+
34375+ /* another style of au_wbr_create_exp() */
34376+ bwh = au_dbwh(dentry);
34377+ parent = dget_parent(dentry);
34378+ btail = au_dbtaildir(parent);
34379+ if (bwh >= 0 && bwh < btail)
34380+ btail = bwh;
34381+
34382+ err = au_wbr_nonopq(dentry, btail);
34383+ if (unlikely(err < 0))
34384+ goto out;
34385+ btail = err;
34386+ bfound = -1;
34387+ for (bindex = 0; bindex <= btail; bindex++) {
34388+ br = au_sbr(sb, bindex);
34389+ if (au_br_rdonly(br))
34390+ continue;
34391+ if (br->br_wbr->wbr_bytes > watermark) {
34392+ bfound = bindex;
34393+ break;
34394+ }
34395+ }
34396+ err = bfound;
34397+ if (err < 0)
34398+ err = bmfs;
34399+
34400+out:
34401+ dput(parent);
34402+ AuDbg("b%d\n", err);
34403+ return err;
34404+}
34405+
34406+/* ---------------------------------------------------------------------- */
34407+
34408+/* most free space and then round robin */
34409+static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
34410+{
34411+ int err;
34412+ struct au_wbr_mfs *mfs;
34413+
34414+ err = au_wbr_create_mfs(dentry, flags);
34415+ if (err >= 0) {
34416+ mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
34417+ mutex_lock(&mfs->mfs_lock);
34418+ if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
34419+ err = au_wbr_create_rr(dentry, flags);
34420+ mutex_unlock(&mfs->mfs_lock);
34421+ }
34422+
34423+ AuDbg("b%d\n", err);
34424+ return err;
34425+}
34426+
34427+static int au_wbr_create_init_mfsrr(struct super_block *sb)
34428+{
34429+ int err;
34430+
34431+ au_wbr_create_init_mfs(sb); /* ignore */
34432+ err = au_wbr_create_init_rr(sb);
34433+
34434+ return err;
34435+}
34436+
34437+/* ---------------------------------------------------------------------- */
34438+
34439+/* top down parent and most free space */
34440+static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
34441+{
34442+ int err, e2;
34443+ unsigned long long b;
34444+ aufs_bindex_t bindex, btop, bbot;
34445+ struct super_block *sb;
34446+ struct dentry *parent, *h_parent;
34447+ struct au_branch *br;
34448+
34449+ err = au_wbr_create_tdp(dentry, flags);
34450+ if (unlikely(err < 0))
34451+ goto out;
34452+ parent = dget_parent(dentry);
34453+ btop = au_dbtop(parent);
34454+ bbot = au_dbtaildir(parent);
34455+ if (btop == bbot)
34456+ goto out_parent; /* success */
34457+
34458+ e2 = au_wbr_create_mfs(dentry, flags);
34459+ if (e2 < 0)
34460+ goto out_parent; /* success */
34461+
34462+ /* when the available size is equal, select upper one */
34463+ sb = dentry->d_sb;
34464+ br = au_sbr(sb, err);
34465+ b = br->br_wbr->wbr_bytes;
34466+ AuDbg("b%d, %llu\n", err, b);
34467+
34468+ for (bindex = btop; bindex <= bbot; bindex++) {
34469+ h_parent = au_h_dptr(parent, bindex);
34470+ if (!h_parent || d_is_negative(h_parent))
34471+ continue;
34472+
34473+ br = au_sbr(sb, bindex);
34474+ if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34475+ b = br->br_wbr->wbr_bytes;
34476+ err = bindex;
34477+ AuDbg("b%d, %llu\n", err, b);
34478+ }
34479+ }
34480+
34481+ if (err >= 0)
34482+ err = au_wbr_nonopq(dentry, err);
34483+
34484+out_parent:
34485+ dput(parent);
34486+out:
34487+ AuDbg("b%d\n", err);
34488+ return err;
34489+}
34490+
34491+/* ---------------------------------------------------------------------- */
34492+
34493+/*
34494+ * - top down parent
34495+ * - most free space with parent
34496+ * - most free space round-robin regardless parent
34497+ */
34498+static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34499+{
34500+ int err;
34501+ unsigned long long watermark;
34502+ struct super_block *sb;
34503+ struct au_branch *br;
34504+ struct au_wbr_mfs *mfs;
34505+
34506+ err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34507+ if (unlikely(err < 0))
34508+ goto out;
34509+
34510+ sb = dentry->d_sb;
34511+ br = au_sbr(sb, err);
34512+ mfs = &au_sbi(sb)->si_wbr_mfs;
34513+ mutex_lock(&mfs->mfs_lock);
34514+ watermark = mfs->mfsrr_watermark;
34515+ mutex_unlock(&mfs->mfs_lock);
34516+ if (br->br_wbr->wbr_bytes < watermark)
34517+ /* regardless the parent dir */
34518+ err = au_wbr_create_mfsrr(dentry, flags);
34519+
34520+out:
34521+ AuDbg("b%d\n", err);
34522+ return err;
34523+}
34524+
34525+/* ---------------------------------------------------------------------- */
34526+
34527+/* policies for copyup */
34528+
34529+/* top down parent */
34530+static int au_wbr_copyup_tdp(struct dentry *dentry)
34531+{
34532+ return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
34533+}
34534+
34535+/* bottom up parent */
34536+static int au_wbr_copyup_bup(struct dentry *dentry)
34537+{
34538+ int err;
34539+ aufs_bindex_t bindex, btop;
34540+ struct dentry *parent, *h_parent;
34541+ struct super_block *sb;
34542+
34543+ err = -EROFS;
34544+ sb = dentry->d_sb;
34545+ parent = dget_parent(dentry);
34546+ btop = au_dbtop(parent);
34547+ for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
34548+ h_parent = au_h_dptr(parent, bindex);
34549+ if (!h_parent || d_is_negative(h_parent))
34550+ continue;
34551+
34552+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34553+ err = bindex;
34554+ break;
34555+ }
34556+ }
34557+ dput(parent);
34558+
34559+ /* bottom up here */
34560+ if (unlikely(err < 0))
34561+ err = au_wbr_bu(sb, btop - 1);
34562+
34563+ AuDbg("b%d\n", err);
34564+ return err;
34565+}
34566+
34567+/* bottom up */
34568+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
34569+{
34570+ int err;
34571+
34572+ err = au_wbr_bu(dentry->d_sb, btop);
34573+ AuDbg("b%d\n", err);
34574+ if (err > btop)
34575+ err = au_wbr_nonopq(dentry, err);
34576+
34577+ AuDbg("b%d\n", err);
34578+ return err;
34579+}
34580+
34581+static int au_wbr_copyup_bu(struct dentry *dentry)
34582+{
34583+ int err;
34584+ aufs_bindex_t btop;
34585+
34586+ btop = au_dbtop(dentry);
34587+ err = au_wbr_do_copyup_bu(dentry, btop);
34588+ return err;
34589+}
34590+
34591+/* ---------------------------------------------------------------------- */
34592+
34593+struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34594+ [AuWbrCopyup_TDP] = {
34595+ .copyup = au_wbr_copyup_tdp
34596+ },
34597+ [AuWbrCopyup_BUP] = {
34598+ .copyup = au_wbr_copyup_bup
34599+ },
34600+ [AuWbrCopyup_BU] = {
34601+ .copyup = au_wbr_copyup_bu
34602+ }
34603+};
34604+
34605+struct au_wbr_create_operations au_wbr_create_ops[] = {
34606+ [AuWbrCreate_TDP] = {
34607+ .create = au_wbr_create_tdp
34608+ },
34609+ [AuWbrCreate_RR] = {
34610+ .create = au_wbr_create_rr,
34611+ .init = au_wbr_create_init_rr
34612+ },
34613+ [AuWbrCreate_MFS] = {
34614+ .create = au_wbr_create_mfs,
34615+ .init = au_wbr_create_init_mfs,
34616+ .fin = au_wbr_create_fin_mfs
34617+ },
34618+ [AuWbrCreate_MFSV] = {
34619+ .create = au_wbr_create_mfs,
34620+ .init = au_wbr_create_init_mfs,
34621+ .fin = au_wbr_create_fin_mfs
34622+ },
34623+ [AuWbrCreate_MFSRR] = {
34624+ .create = au_wbr_create_mfsrr,
34625+ .init = au_wbr_create_init_mfsrr,
34626+ .fin = au_wbr_create_fin_mfs
34627+ },
34628+ [AuWbrCreate_MFSRRV] = {
34629+ .create = au_wbr_create_mfsrr,
34630+ .init = au_wbr_create_init_mfsrr,
34631+ .fin = au_wbr_create_fin_mfs
34632+ },
34633+ [AuWbrCreate_TDMFS] = {
34634+ .create = au_wbr_create_tdmfs,
34635+ .init = au_wbr_create_init_mfs,
34636+ .fin = au_wbr_create_fin_mfs
34637+ },
34638+ [AuWbrCreate_TDMFSV] = {
34639+ .create = au_wbr_create_tdmfs,
34640+ .init = au_wbr_create_init_mfs,
34641+ .fin = au_wbr_create_fin_mfs
34642+ },
34643+ [AuWbrCreate_PMFS] = {
34644+ .create = au_wbr_create_pmfs,
34645+ .init = au_wbr_create_init_mfs,
34646+ .fin = au_wbr_create_fin_mfs
34647+ },
34648+ [AuWbrCreate_PMFSV] = {
34649+ .create = au_wbr_create_pmfs,
34650+ .init = au_wbr_create_init_mfs,
34651+ .fin = au_wbr_create_fin_mfs
34652+ },
34653+ [AuWbrCreate_PMFSRR] = {
34654+ .create = au_wbr_create_pmfsrr,
34655+ .init = au_wbr_create_init_mfsrr,
34656+ .fin = au_wbr_create_fin_mfs
34657+ },
34658+ [AuWbrCreate_PMFSRRV] = {
34659+ .create = au_wbr_create_pmfsrr,
34660+ .init = au_wbr_create_init_mfsrr,
34661+ .fin = au_wbr_create_fin_mfs
34662+ }
34663+};
34664diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
34665--- /usr/share/empty/fs/aufs/whout.c 1970-01-01 01:00:00.000000000 +0100
34666+++ linux/fs/aufs/whout.c 2021-02-22 22:51:59.061411920 +0100
34667@@ -0,0 +1,1062 @@
34668+// SPDX-License-Identifier: GPL-2.0
34669+/*
34670+ * Copyright (C) 2005-2020 Junjiro R. Okajima
34671+ *
34672+ * This program, aufs is free software; you can redistribute it and/or modify
34673+ * it under the terms of the GNU General Public License as published by
34674+ * the Free Software Foundation; either version 2 of the License, or
34675+ * (at your option) any later version.
34676+ *
34677+ * This program is distributed in the hope that it will be useful,
34678+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
34679+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34680+ * GNU General Public License for more details.
34681+ *
34682+ * You should have received a copy of the GNU General Public License
34683+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
34684+ */
34685+
34686+/*
34687+ * whiteout for logical deletion and opaque directory
34688+ */
34689+
34690+#include "aufs.h"
34691+
34692+#define WH_MASK 0444
34693+
34694+/*
34695+ * If a directory contains this file, then it is opaque. We start with the
34696+ * .wh. flag so that it is blocked by lookup.
34697+ */
34698+static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
34699+ sizeof(AUFS_WH_DIROPQ) - 1);
34700+
34701+/*
34702+ * generate whiteout name, which is NOT terminated by NULL.
34703+ * @name: original d_name.name
34704+ * @len: original d_name.len
34705+ * @wh: whiteout qstr
34706+ * returns zero when succeeds, otherwise error.
34707+ * succeeded value as wh->name should be freed by kfree().
34708+ */
34709+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
34710+{
34711+ char *p;
34712+
34713+ if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
34714+ return -ENAMETOOLONG;
34715+
34716+ wh->len = name->len + AUFS_WH_PFX_LEN;
34717+ p = kmalloc(wh->len, GFP_NOFS);
34718+ wh->name = p;
34719+ if (p) {
34720+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
34721+ memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
34722+ /* smp_mb(); */
34723+ return 0;
34724+ }
34725+ return -ENOMEM;
34726+}
34727+
34728+/* ---------------------------------------------------------------------- */
34729+
34730+/*
34731+ * test if the @wh_name exists under @h_parent.
34732+ * @try_sio specifies the necessary of super-io.
34733+ */
34734+int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
34735+{
34736+ int err;
34737+ struct dentry *wh_dentry;
34738+
34739+ if (!try_sio)
34740+ wh_dentry = vfsub_lkup_one(wh_name, h_parent);
34741+ else
34742+ wh_dentry = au_sio_lkup_one(wh_name, h_parent);
34743+ err = PTR_ERR(wh_dentry);
34744+ if (IS_ERR(wh_dentry)) {
34745+ if (err == -ENAMETOOLONG)
34746+ err = 0;
34747+ goto out;
34748+ }
34749+
34750+ err = 0;
34751+ if (d_is_negative(wh_dentry))
34752+ goto out_wh; /* success */
34753+
34754+ err = 1;
34755+ if (d_is_reg(wh_dentry))
34756+ goto out_wh; /* success */
34757+
34758+ err = -EIO;
34759+ AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
34760+ wh_dentry, d_inode(wh_dentry)->i_mode);
34761+
34762+out_wh:
34763+ dput(wh_dentry);
34764+out:
34765+ return err;
34766+}
34767+
34768+/*
34769+ * test if the @h_dentry sets opaque or not.
34770+ */
34771+int au_diropq_test(struct dentry *h_dentry)
34772+{
34773+ int err;
34774+ struct inode *h_dir;
34775+
34776+ h_dir = d_inode(h_dentry);
34777+ err = au_wh_test(h_dentry, &diropq_name,
34778+ au_test_h_perm_sio(h_dir, MAY_EXEC));
34779+ return err;
34780+}
34781+
34782+/*
34783+ * returns a negative dentry whose name is unique and temporary.
34784+ */
34785+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
34786+ struct qstr *prefix)
34787+{
34788+ struct dentry *dentry;
34789+ int i;
34790+ char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
34791+ *name, *p;
34792+ /* strict atomic_t is unnecessary here */
34793+ static unsigned short cnt;
34794+ struct qstr qs;
34795+
34796+ BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
34797+
34798+ name = defname;
34799+ qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
34800+ if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
34801+ dentry = ERR_PTR(-ENAMETOOLONG);
34802+ if (unlikely(qs.len > NAME_MAX))
34803+ goto out;
34804+ dentry = ERR_PTR(-ENOMEM);
34805+ name = kmalloc(qs.len + 1, GFP_NOFS);
34806+ if (unlikely(!name))
34807+ goto out;
34808+ }
34809+
34810+ /* doubly whiteout-ed */
34811+ memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
34812+ p = name + AUFS_WH_PFX_LEN * 2;
34813+ memcpy(p, prefix->name, prefix->len);
34814+ p += prefix->len;
34815+ *p++ = '.';
34816+ AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
34817+
34818+ qs.name = name;
34819+ for (i = 0; i < 3; i++) {
34820+ sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
34821+ dentry = au_sio_lkup_one(&qs, h_parent);
34822+ if (IS_ERR(dentry) || d_is_negative(dentry))
34823+ goto out_name;
34824+ dput(dentry);
34825+ }
34826+ /* pr_warn("could not get random name\n"); */
34827+ dentry = ERR_PTR(-EEXIST);
34828+ AuDbg("%.*s\n", AuLNPair(&qs));
34829+ BUG();
34830+
34831+out_name:
34832+ if (name != defname)
34833+ au_kfree_try_rcu(name);
34834+out:
34835+ AuTraceErrPtr(dentry);
34836+ return dentry;
34837+}
34838+
34839+/*
34840+ * rename the @h_dentry on @br to the whiteouted temporary name.
34841+ */
34842+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
34843+{
34844+ int err;
34845+ struct path h_path = {
34846+ .mnt = au_br_mnt(br)
34847+ };
34848+ struct inode *h_dir, *delegated;
34849+ struct dentry *h_parent;
34850+
34851+ h_parent = h_dentry->d_parent; /* dir inode is locked */
34852+ h_dir = d_inode(h_parent);
34853+ IMustLock(h_dir);
34854+
34855+ h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
34856+ err = PTR_ERR(h_path.dentry);
34857+ if (IS_ERR(h_path.dentry))
34858+ goto out;
34859+
34860+ /* under the same dir, no need to lock_rename() */
34861+ delegated = NULL;
34862+ err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
34863+ /*flags*/0);
34864+ AuTraceErr(err);
34865+ if (unlikely(err == -EWOULDBLOCK)) {
34866+ pr_warn("cannot retry for NFSv4 delegation"
34867+ " for an internal rename\n");
34868+ iput(delegated);
34869+ }
34870+ dput(h_path.dentry);
34871+
34872+out:
34873+ AuTraceErr(err);
34874+ return err;
34875+}
34876+
34877+/* ---------------------------------------------------------------------- */
34878+/*
34879+ * functions for removing a whiteout
34880+ */
34881+
34882+static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
34883+{
34884+ int err, force;
34885+ struct inode *delegated;
34886+
34887+ /*
34888+ * forces superio when the dir has a sticky bit.
34889+ * this may be a violation of unix fs semantics.
34890+ */
34891+ force = (h_dir->i_mode & S_ISVTX)
34892+ && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
34893+ delegated = NULL;
34894+ err = vfsub_unlink(h_dir, h_path, &delegated, force);
34895+ if (unlikely(err == -EWOULDBLOCK)) {
34896+ pr_warn("cannot retry for NFSv4 delegation"
34897+ " for an internal unlink\n");
34898+ iput(delegated);
34899+ }
34900+ return err;
34901+}
34902+
34903+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
34904+ struct dentry *dentry)
34905+{
34906+ int err;
34907+
34908+ err = do_unlink_wh(h_dir, h_path);
34909+ if (!err && dentry)
34910+ au_set_dbwh(dentry, -1);
34911+
34912+ return err;
34913+}
34914+
34915+static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
34916+ struct au_branch *br)
34917+{
34918+ int err;
34919+ struct path h_path = {
34920+ .mnt = au_br_mnt(br)
34921+ };
34922+
34923+ err = 0;
34924+ h_path.dentry = vfsub_lkup_one(wh, h_parent);
34925+ if (IS_ERR(h_path.dentry))
34926+ err = PTR_ERR(h_path.dentry);
34927+ else {
34928+ if (d_is_reg(h_path.dentry))
34929+ err = do_unlink_wh(d_inode(h_parent), &h_path);
34930+ dput(h_path.dentry);
34931+ }
34932+
34933+ return err;
34934+}
34935+
34936+/* ---------------------------------------------------------------------- */
34937+/*
34938+ * initialize/clean whiteout for a branch
34939+ */
34940+
34941+static void au_wh_clean(struct inode *h_dir, struct path *whpath,
34942+ const int isdir)
34943+{
34944+ int err;
34945+ struct inode *delegated;
34946+
34947+ if (d_is_negative(whpath->dentry))
34948+ return;
34949+
34950+ if (isdir)
34951+ err = vfsub_rmdir(h_dir, whpath);
34952+ else {
34953+ delegated = NULL;
34954+ err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
34955+ if (unlikely(err == -EWOULDBLOCK)) {
34956+ pr_warn("cannot retry for NFSv4 delegation"
34957+ " for an internal unlink\n");
34958+ iput(delegated);
34959+ }
34960+ }
34961+ if (unlikely(err))
34962+ pr_warn("failed removing %pd (%d), ignored.\n",
34963+ whpath->dentry, err);
34964+}
34965+
34966+static int test_linkable(struct dentry *h_root)
34967+{
34968+ struct inode *h_dir = d_inode(h_root);
34969+
34970+ if (h_dir->i_op->link)
34971+ return 0;
34972+
34973+ pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
34974+ h_root, au_sbtype(h_root->d_sb));
34975+ return -ENOSYS; /* the branch doesn't have its ->link() */
34976+}
34977+
34978+/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
34979+static int au_whdir(struct inode *h_dir, struct path *path)
34980+{
34981+ int err;
34982+
34983+ err = -EEXIST;
34984+ if (d_is_negative(path->dentry)) {
34985+ int mode = 0700;
34986+
34987+ if (au_test_nfs(path->dentry->d_sb))
34988+ mode |= 0111;
34989+ err = vfsub_mkdir(h_dir, path, mode);
34990+ } else if (d_is_dir(path->dentry))
34991+ err = 0;
34992+ else
34993+ pr_err("unknown %pd exists\n", path->dentry);
34994+
34995+ return err;
34996+}
34997+
34998+struct au_wh_base {
34999+ const struct qstr *name;
35000+ struct dentry *dentry;
35001+};
35002+
35003+static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35004+ struct path *h_path)
35005+{
35006+ h_path->dentry = base[AuBrWh_BASE].dentry;
35007+ au_wh_clean(h_dir, h_path, /*isdir*/0);
35008+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35009+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35010+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35011+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35012+}
35013+
35014+/*
35015+ * returns tri-state,
35016+ * minus: error, caller should print the message
35017+ * zero: success
35018+ * plus: error, caller should NOT print the message
35019+ */
35020+static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35021+ int do_plink, struct au_wh_base base[],
35022+ struct path *h_path)
35023+{
35024+ int err;
35025+ struct inode *h_dir;
35026+
35027+ h_dir = d_inode(h_root);
35028+ h_path->dentry = base[AuBrWh_BASE].dentry;
35029+ au_wh_clean(h_dir, h_path, /*isdir*/0);
35030+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35031+ if (do_plink) {
35032+ err = test_linkable(h_root);
35033+ if (unlikely(err)) {
35034+ err = 1;
35035+ goto out;
35036+ }
35037+
35038+ err = au_whdir(h_dir, h_path);
35039+ if (unlikely(err))
35040+ goto out;
35041+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35042+ } else
35043+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35044+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35045+ err = au_whdir(h_dir, h_path);
35046+ if (unlikely(err))
35047+ goto out;
35048+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35049+
35050+out:
35051+ return err;
35052+}
35053+
35054+/*
35055+ * for the moment, aufs supports the branch filesystem which does not support
35056+ * link(2). testing on FAT which does not support i_op->setattr() fully either,
35057+ * copyup failed. finally, such filesystem will not be used as the writable
35058+ * branch.
35059+ *
35060+ * returns tri-state, see above.
35061+ */
35062+static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35063+ int do_plink, struct au_wh_base base[],
35064+ struct path *h_path)
35065+{
35066+ int err;
35067+ struct inode *h_dir;
35068+
35069+ WbrWhMustWriteLock(wbr);
35070+
35071+ err = test_linkable(h_root);
35072+ if (unlikely(err)) {
35073+ err = 1;
35074+ goto out;
35075+ }
35076+
35077+ /*
35078+ * todo: should this create be done in /sbin/mount.aufs helper?
35079+ */
35080+ err = -EEXIST;
35081+ h_dir = d_inode(h_root);
35082+ if (d_is_negative(base[AuBrWh_BASE].dentry)) {
35083+ h_path->dentry = base[AuBrWh_BASE].dentry;
35084+ err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
35085+ } else if (d_is_reg(base[AuBrWh_BASE].dentry))
35086+ err = 0;
35087+ else
35088+ pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
35089+ if (unlikely(err))
35090+ goto out;
35091+
35092+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35093+ if (do_plink) {
35094+ err = au_whdir(h_dir, h_path);
35095+ if (unlikely(err))
35096+ goto out;
35097+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35098+ } else
35099+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35100+ wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35101+
35102+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35103+ err = au_whdir(h_dir, h_path);
35104+ if (unlikely(err))
35105+ goto out;
35106+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35107+
35108+out:
35109+ return err;
35110+}
35111+
35112+/*
35113+ * initialize the whiteout base file/dir for @br.
35114+ */
35115+int au_wh_init(struct au_branch *br, struct super_block *sb)
35116+{
35117+ int err, i;
35118+ const unsigned char do_plink
35119+ = !!au_opt_test(au_mntflags(sb), PLINK);
35120+ struct inode *h_dir;
35121+ struct path path = br->br_path;
35122+ struct dentry *h_root = path.dentry;
35123+ struct au_wbr *wbr = br->br_wbr;
35124+ static const struct qstr base_name[] = {
35125+ [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35126+ sizeof(AUFS_BASE_NAME) - 1),
35127+ [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35128+ sizeof(AUFS_PLINKDIR_NAME) - 1),
35129+ [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35130+ sizeof(AUFS_ORPHDIR_NAME) - 1)
35131+ };
35132+ struct au_wh_base base[] = {
35133+ [AuBrWh_BASE] = {
35134+ .name = base_name + AuBrWh_BASE,
35135+ .dentry = NULL
35136+ },
35137+ [AuBrWh_PLINK] = {
35138+ .name = base_name + AuBrWh_PLINK,
35139+ .dentry = NULL
35140+ },
35141+ [AuBrWh_ORPH] = {
35142+ .name = base_name + AuBrWh_ORPH,
35143+ .dentry = NULL
35144+ }
35145+ };
35146+
35147+ if (wbr)
35148+ WbrWhMustWriteLock(wbr);
35149+
35150+ for (i = 0; i < AuBrWh_Last; i++) {
35151+ /* doubly whiteouted */
35152+ struct dentry *d;
35153+
35154+ d = au_wh_lkup(h_root, (void *)base[i].name, br);
35155+ err = PTR_ERR(d);
35156+ if (IS_ERR(d))
35157+ goto out;
35158+
35159+ base[i].dentry = d;
35160+ AuDebugOn(wbr
35161+ && wbr->wbr_wh[i]
35162+ && wbr->wbr_wh[i] != base[i].dentry);
35163+ }
35164+
35165+ if (wbr)
35166+ for (i = 0; i < AuBrWh_Last; i++) {
35167+ dput(wbr->wbr_wh[i]);
35168+ wbr->wbr_wh[i] = NULL;
35169+ }
35170+
35171+ err = 0;
35172+ if (!au_br_writable(br->br_perm)) {
35173+ h_dir = d_inode(h_root);
35174+ au_wh_init_ro(h_dir, base, &path);
35175+ } else if (!au_br_wh_linkable(br->br_perm)) {
35176+ err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35177+ if (err > 0)
35178+ goto out;
35179+ else if (err)
35180+ goto out_err;
35181+ } else {
35182+ err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35183+ if (err > 0)
35184+ goto out;
35185+ else if (err)
35186+ goto out_err;
35187+ }
35188+ goto out; /* success */
35189+
35190+out_err:
35191+ pr_err("an error(%d) on the writable branch %pd(%s)\n",
35192+ err, h_root, au_sbtype(h_root->d_sb));
35193+out:
35194+ for (i = 0; i < AuBrWh_Last; i++)
35195+ dput(base[i].dentry);
35196+ return err;
35197+}
35198+
35199+/* ---------------------------------------------------------------------- */
35200+/*
35201+ * whiteouts are all hard-linked usually.
35202+ * when its link count reaches a ceiling, we create a new whiteout base
35203+ * asynchronously.
35204+ */
35205+
35206+struct reinit_br_wh {
35207+ struct super_block *sb;
35208+ struct au_branch *br;
35209+};
35210+
35211+static void reinit_br_wh(void *arg)
35212+{
35213+ int err;
35214+ aufs_bindex_t bindex;
35215+ struct path h_path;
35216+ struct reinit_br_wh *a = arg;
35217+ struct au_wbr *wbr;
35218+ struct inode *dir, *delegated;
35219+ struct dentry *h_root;
35220+ struct au_hinode *hdir;
35221+
35222+ err = 0;
35223+ wbr = a->br->br_wbr;
35224+ /* big aufs lock */
35225+ si_noflush_write_lock(a->sb);
35226+ if (!au_br_writable(a->br->br_perm))
35227+ goto out;
35228+ bindex = au_br_index(a->sb, a->br->br_id);
35229+ if (unlikely(bindex < 0))
35230+ goto out;
35231+
35232+ di_read_lock_parent(a->sb->s_root, AuLock_IR);
35233+ dir = d_inode(a->sb->s_root);
35234+ hdir = au_hi(dir, bindex);
35235+ h_root = au_h_dptr(a->sb->s_root, bindex);
35236+ AuDebugOn(h_root != au_br_dentry(a->br));
35237+
35238+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35239+ wbr_wh_write_lock(wbr);
35240+ err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35241+ h_root, a->br);
35242+ if (!err) {
35243+ h_path.dentry = wbr->wbr_whbase;
35244+ h_path.mnt = au_br_mnt(a->br);
35245+ delegated = NULL;
35246+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35247+ /*force*/0);
35248+ if (unlikely(err == -EWOULDBLOCK)) {
35249+ pr_warn("cannot retry for NFSv4 delegation"
35250+ " for an internal unlink\n");
35251+ iput(delegated);
35252+ }
35253+ } else {
35254+ pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
35255+ err = 0;
35256+ }
35257+ dput(wbr->wbr_whbase);
35258+ wbr->wbr_whbase = NULL;
35259+ if (!err)
35260+ err = au_wh_init(a->br, a->sb);
35261+ wbr_wh_write_unlock(wbr);
35262+ au_hn_inode_unlock(hdir);
35263+ di_read_unlock(a->sb->s_root, AuLock_IR);
35264+ if (!err)
35265+ au_fhsm_wrote(a->sb, bindex, /*force*/0);
35266+
35267+out:
35268+ if (wbr)
35269+ atomic_dec(&wbr->wbr_wh_running);
35270+ au_lcnt_dec(&a->br->br_count);
35271+ si_write_unlock(a->sb);
35272+ au_nwt_done(&au_sbi(a->sb)->si_nowait);
35273+ au_kfree_rcu(a);
35274+ if (unlikely(err))
35275+ AuIOErr("err %d\n", err);
35276+}
35277+
35278+static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35279+{
35280+ int do_dec, wkq_err;
35281+ struct reinit_br_wh *arg;
35282+
35283+ do_dec = 1;
35284+ if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35285+ goto out;
35286+
35287+ /* ignore ENOMEM */
35288+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
35289+ if (arg) {
35290+ /*
35291+ * dec(wh_running), kfree(arg) and dec(br_count)
35292+ * in reinit function
35293+ */
35294+ arg->sb = sb;
35295+ arg->br = br;
35296+ au_lcnt_inc(&br->br_count);
35297+ wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
35298+ if (unlikely(wkq_err)) {
35299+ atomic_dec(&br->br_wbr->wbr_wh_running);
35300+ au_lcnt_dec(&br->br_count);
35301+ au_kfree_rcu(arg);
35302+ }
35303+ do_dec = 0;
35304+ }
35305+
35306+out:
35307+ if (do_dec)
35308+ atomic_dec(&br->br_wbr->wbr_wh_running);
35309+}
35310+
35311+/* ---------------------------------------------------------------------- */
35312+
35313+/*
35314+ * create the whiteout @wh.
35315+ */
35316+static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35317+ struct dentry *wh)
35318+{
35319+ int err;
35320+ struct path h_path = {
35321+ .dentry = wh
35322+ };
35323+ struct au_branch *br;
35324+ struct au_wbr *wbr;
35325+ struct dentry *h_parent;
35326+ struct inode *h_dir, *delegated;
35327+
35328+ h_parent = wh->d_parent; /* dir inode is locked */
35329+ h_dir = d_inode(h_parent);
35330+ IMustLock(h_dir);
35331+
35332+ br = au_sbr(sb, bindex);
35333+ h_path.mnt = au_br_mnt(br);
35334+ wbr = br->br_wbr;
35335+ wbr_wh_read_lock(wbr);
35336+ if (wbr->wbr_whbase) {
35337+ delegated = NULL;
35338+ err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35339+ if (unlikely(err == -EWOULDBLOCK)) {
35340+ pr_warn("cannot retry for NFSv4 delegation"
35341+ " for an internal link\n");
35342+ iput(delegated);
35343+ }
35344+ if (!err || err != -EMLINK)
35345+ goto out;
35346+
35347+ /* link count full. re-initialize br_whbase. */
35348+ kick_reinit_br_wh(sb, br);
35349+ }
35350+
35351+ /* return this error in this context */
35352+ err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
35353+ if (!err)
35354+ au_fhsm_wrote(sb, bindex, /*force*/0);
35355+
35356+out:
35357+ wbr_wh_read_unlock(wbr);
35358+ return err;
35359+}
35360+
35361+/* ---------------------------------------------------------------------- */
35362+
35363+/*
35364+ * create or remove the diropq.
35365+ */
35366+static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35367+ unsigned int flags)
35368+{
35369+ struct dentry *opq_dentry, *h_dentry;
35370+ struct super_block *sb;
35371+ struct au_branch *br;
35372+ int err;
35373+
35374+ sb = dentry->d_sb;
35375+ br = au_sbr(sb, bindex);
35376+ h_dentry = au_h_dptr(dentry, bindex);
35377+ opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
35378+ if (IS_ERR(opq_dentry))
35379+ goto out;
35380+
35381+ if (au_ftest_diropq(flags, CREATE)) {
35382+ err = link_or_create_wh(sb, bindex, opq_dentry);
35383+ if (!err) {
35384+ au_set_dbdiropq(dentry, bindex);
35385+ goto out; /* success */
35386+ }
35387+ } else {
35388+ struct path tmp = {
35389+ .dentry = opq_dentry,
35390+ .mnt = au_br_mnt(br)
35391+ };
35392+ err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
35393+ if (!err)
35394+ au_set_dbdiropq(dentry, -1);
35395+ }
35396+ dput(opq_dentry);
35397+ opq_dentry = ERR_PTR(err);
35398+
35399+out:
35400+ return opq_dentry;
35401+}
35402+
35403+struct do_diropq_args {
35404+ struct dentry **errp;
35405+ struct dentry *dentry;
35406+ aufs_bindex_t bindex;
35407+ unsigned int flags;
35408+};
35409+
35410+static void call_do_diropq(void *args)
35411+{
35412+ struct do_diropq_args *a = args;
35413+ *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35414+}
35415+
35416+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35417+ unsigned int flags)
35418+{
35419+ struct dentry *diropq, *h_dentry;
35420+
35421+ h_dentry = au_h_dptr(dentry, bindex);
35422+ if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
35423+ diropq = do_diropq(dentry, bindex, flags);
35424+ else {
35425+ int wkq_err;
35426+ struct do_diropq_args args = {
35427+ .errp = &diropq,
35428+ .dentry = dentry,
35429+ .bindex = bindex,
35430+ .flags = flags
35431+ };
35432+
35433+ wkq_err = au_wkq_wait(call_do_diropq, &args);
35434+ if (unlikely(wkq_err))
35435+ diropq = ERR_PTR(wkq_err);
35436+ }
35437+
35438+ return diropq;
35439+}
35440+
35441+/* ---------------------------------------------------------------------- */
35442+
35443+/*
35444+ * lookup whiteout dentry.
35445+ * @h_parent: lower parent dentry which must exist and be locked
35446+ * @base_name: name of dentry which will be whiteouted
35447+ * returns dentry for whiteout.
35448+ */
35449+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35450+ struct au_branch *br)
35451+{
35452+ int err;
35453+ struct qstr wh_name;
35454+ struct dentry *wh_dentry;
35455+
35456+ err = au_wh_name_alloc(&wh_name, base_name);
35457+ wh_dentry = ERR_PTR(err);
35458+ if (!err) {
35459+ wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
35460+ au_kfree_try_rcu(wh_name.name);
35461+ }
35462+ return wh_dentry;
35463+}
35464+
35465+/*
35466+ * link/create a whiteout for @dentry on @bindex.
35467+ */
35468+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35469+ struct dentry *h_parent)
35470+{
35471+ struct dentry *wh_dentry;
35472+ struct super_block *sb;
35473+ int err;
35474+
35475+ sb = dentry->d_sb;
35476+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
35477+ if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
35478+ err = link_or_create_wh(sb, bindex, wh_dentry);
35479+ if (!err) {
35480+ au_set_dbwh(dentry, bindex);
35481+ au_fhsm_wrote(sb, bindex, /*force*/0);
35482+ } else {
35483+ dput(wh_dentry);
35484+ wh_dentry = ERR_PTR(err);
35485+ }
35486+ }
35487+
35488+ return wh_dentry;
35489+}
35490+
35491+/* ---------------------------------------------------------------------- */
35492+
35493+/* Delete all whiteouts in this directory on branch bindex. */
35494+static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
35495+ aufs_bindex_t bindex, struct au_branch *br)
35496+{
35497+ int err;
35498+ unsigned long ul, n;
35499+ struct qstr wh_name;
35500+ char *p;
35501+ struct hlist_head *head;
35502+ struct au_vdir_wh *pos;
35503+ struct au_vdir_destr *str;
35504+
35505+ err = -ENOMEM;
35506+ p = (void *)__get_free_page(GFP_NOFS);
35507+ wh_name.name = p;
35508+ if (unlikely(!wh_name.name))
35509+ goto out;
35510+
35511+ err = 0;
35512+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35513+ p += AUFS_WH_PFX_LEN;
35514+ n = whlist->nh_num;
35515+ head = whlist->nh_head;
35516+ for (ul = 0; !err && ul < n; ul++, head++) {
35517+ hlist_for_each_entry(pos, head, wh_hash) {
35518+ if (pos->wh_bindex != bindex)
35519+ continue;
35520+
35521+ str = &pos->wh_str;
35522+ if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35523+ memcpy(p, str->name, str->len);
35524+ wh_name.len = AUFS_WH_PFX_LEN + str->len;
35525+ err = unlink_wh_name(h_dentry, &wh_name, br);
35526+ if (!err)
35527+ continue;
35528+ break;
35529+ }
35530+ AuIOErr("whiteout name too long %.*s\n",
35531+ str->len, str->name);
35532+ err = -EIO;
35533+ break;
35534+ }
35535+ }
35536+ free_page((unsigned long)wh_name.name);
35537+
35538+out:
35539+ return err;
35540+}
35541+
35542+struct del_wh_children_args {
35543+ int *errp;
35544+ struct dentry *h_dentry;
35545+ struct au_nhash *whlist;
35546+ aufs_bindex_t bindex;
35547+ struct au_branch *br;
35548+};
35549+
35550+static void call_del_wh_children(void *args)
35551+{
35552+ struct del_wh_children_args *a = args;
35553+ *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
35554+}
35555+
35556+/* ---------------------------------------------------------------------- */
35557+
35558+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35559+{
35560+ struct au_whtmp_rmdir *whtmp;
35561+ int err;
35562+ unsigned int rdhash;
35563+
35564+ SiMustAnyLock(sb);
35565+
35566+ whtmp = kzalloc(sizeof(*whtmp), gfp);
35567+ if (unlikely(!whtmp)) {
35568+ whtmp = ERR_PTR(-ENOMEM);
35569+ goto out;
35570+ }
35571+
35572+ /* no estimation for dir size */
35573+ rdhash = au_sbi(sb)->si_rdhash;
35574+ if (!rdhash)
35575+ rdhash = AUFS_RDHASH_DEF;
35576+ err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35577+ if (unlikely(err)) {
35578+ au_kfree_rcu(whtmp);
35579+ whtmp = ERR_PTR(err);
35580+ }
35581+
35582+out:
35583+ return whtmp;
35584+}
35585+
35586+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35587+{
35588+ if (whtmp->br)
35589+ au_lcnt_dec(&whtmp->br->br_count);
35590+ dput(whtmp->wh_dentry);
35591+ iput(whtmp->dir);
35592+ au_nhash_wh_free(&whtmp->whlist);
35593+ au_kfree_rcu(whtmp);
35594+}
35595+
35596+/*
35597+ * rmdir the whiteouted temporary named dir @h_dentry.
35598+ * @whlist: whiteouted children.
35599+ */
35600+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35601+ struct dentry *wh_dentry, struct au_nhash *whlist)
35602+{
35603+ int err;
35604+ unsigned int h_nlink;
35605+ struct path h_tmp;
35606+ struct inode *wh_inode, *h_dir;
35607+ struct au_branch *br;
35608+
35609+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
35610+ IMustLock(h_dir);
35611+
35612+ br = au_sbr(dir->i_sb, bindex);
35613+ wh_inode = d_inode(wh_dentry);
35614+ inode_lock_nested(wh_inode, AuLsc_I_CHILD);
35615+
35616+ /*
35617+ * someone else might change some whiteouts while we were sleeping.
35618+ * it means this whlist may have an obsoleted entry.
35619+ */
35620+ if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
35621+ err = del_wh_children(wh_dentry, whlist, bindex, br);
35622+ else {
35623+ int wkq_err;
35624+ struct del_wh_children_args args = {
35625+ .errp = &err,
35626+ .h_dentry = wh_dentry,
35627+ .whlist = whlist,
35628+ .bindex = bindex,
35629+ .br = br
35630+ };
35631+
35632+ wkq_err = au_wkq_wait(call_del_wh_children, &args);
35633+ if (unlikely(wkq_err))
35634+ err = wkq_err;
35635+ }
35636+ inode_unlock(wh_inode);
35637+
35638+ if (!err) {
35639+ h_tmp.dentry = wh_dentry;
35640+ h_tmp.mnt = au_br_mnt(br);
35641+ h_nlink = h_dir->i_nlink;
35642+ err = vfsub_rmdir(h_dir, &h_tmp);
35643+ /* some fs doesn't change the parent nlink in some cases */
35644+ h_nlink -= h_dir->i_nlink;
35645+ }
35646+
35647+ if (!err) {
35648+ if (au_ibtop(dir) == bindex) {
35649+ /* todo: dir->i_mutex is necessary */
35650+ au_cpup_attr_timesizes(dir);
35651+ if (h_nlink)
35652+ vfsub_drop_nlink(dir);
35653+ }
35654+ return 0; /* success */
35655+ }
35656+
35657+ pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
35658+ return err;
35659+}
35660+
35661+static void call_rmdir_whtmp(void *args)
35662+{
35663+ int err;
35664+ aufs_bindex_t bindex;
35665+ struct au_whtmp_rmdir *a = args;
35666+ struct super_block *sb;
35667+ struct dentry *h_parent;
35668+ struct inode *h_dir;
35669+ struct au_hinode *hdir;
35670+
35671+ /* rmdir by nfsd may cause deadlock with this i_mutex */
35672+ /* inode_lock(a->dir); */
35673+ err = -EROFS;
35674+ sb = a->dir->i_sb;
35675+ si_read_lock(sb, !AuLock_FLUSH);
35676+ if (!au_br_writable(a->br->br_perm))
35677+ goto out;
35678+ bindex = au_br_index(sb, a->br->br_id);
35679+ if (unlikely(bindex < 0))
35680+ goto out;
35681+
35682+ err = -EIO;
35683+ ii_write_lock_parent(a->dir);
35684+ h_parent = dget_parent(a->wh_dentry);
35685+ h_dir = d_inode(h_parent);
35686+ hdir = au_hi(a->dir, bindex);
35687+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
35688+ if (unlikely(err))
35689+ goto out_mnt;
35690+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35691+ err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
35692+ a->br);
35693+ if (!err)
35694+ err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
35695+ au_hn_inode_unlock(hdir);
35696+ vfsub_mnt_drop_write(au_br_mnt(a->br));
35697+
35698+out_mnt:
35699+ dput(h_parent);
35700+ ii_write_unlock(a->dir);
35701+out:
35702+ /* inode_unlock(a->dir); */
35703+ au_whtmp_rmdir_free(a);
35704+ si_read_unlock(sb);
35705+ au_nwt_done(&au_sbi(sb)->si_nowait);
35706+ if (unlikely(err))
35707+ AuIOErr("err %d\n", err);
35708+}
35709+
35710+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35711+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
35712+{
35713+ int wkq_err;
35714+ struct super_block *sb;
35715+
35716+ IMustLock(dir);
35717+
35718+ /* all post-process will be done in do_rmdir_whtmp(). */
35719+ sb = dir->i_sb;
35720+ args->dir = au_igrab(dir);
35721+ args->br = au_sbr(sb, bindex);
35722+ au_lcnt_inc(&args->br->br_count);
35723+ args->wh_dentry = dget(wh_dentry);
35724+ wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
35725+ if (unlikely(wkq_err)) {
35726+ pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
35727+ au_whtmp_rmdir_free(args);
35728+ }
35729+}
35730diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
35731--- /usr/share/empty/fs/aufs/whout.h 1970-01-01 01:00:00.000000000 +0100
35732+++ linux/fs/aufs/whout.h 2021-02-22 22:51:59.061411920 +0100
35733@@ -0,0 +1,86 @@
35734+/* SPDX-License-Identifier: GPL-2.0 */
35735+/*
35736+ * Copyright (C) 2005-2020 Junjiro R. Okajima
35737+ *
35738+ * This program, aufs is free software; you can redistribute it and/or modify
35739+ * it under the terms of the GNU General Public License as published by
35740+ * the Free Software Foundation; either version 2 of the License, or
35741+ * (at your option) any later version.
35742+ *
35743+ * This program is distributed in the hope that it will be useful,
35744+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
35745+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35746+ * GNU General Public License for more details.
35747+ *
35748+ * You should have received a copy of the GNU General Public License
35749+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
35750+ */
35751+
35752+/*
35753+ * whiteout for logical deletion and opaque directory
35754+ */
35755+
35756+#ifndef __AUFS_WHOUT_H__
35757+#define __AUFS_WHOUT_H__
35758+
35759+#ifdef __KERNEL__
35760+
35761+#include "dir.h"
35762+
35763+/* whout.c */
35764+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
35765+int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
35766+int au_diropq_test(struct dentry *h_dentry);
35767+struct au_branch;
35768+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35769+ struct qstr *prefix);
35770+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
35771+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35772+ struct dentry *dentry);
35773+int au_wh_init(struct au_branch *br, struct super_block *sb);
35774+
35775+/* diropq flags */
35776+#define AuDiropq_CREATE 1
35777+#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
35778+#define au_fset_diropq(flags, name) \
35779+ do { (flags) |= AuDiropq_##name; } while (0)
35780+#define au_fclr_diropq(flags, name) \
35781+ do { (flags) &= ~AuDiropq_##name; } while (0)
35782+
35783+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35784+ unsigned int flags);
35785+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35786+ struct au_branch *br);
35787+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35788+ struct dentry *h_parent);
35789+
35790+/* real rmdir for the whiteout-ed dir */
35791+struct au_whtmp_rmdir {
35792+ struct inode *dir;
35793+ struct au_branch *br;
35794+ struct dentry *wh_dentry;
35795+ struct au_nhash whlist;
35796+};
35797+
35798+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
35799+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
35800+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35801+ struct dentry *wh_dentry, struct au_nhash *whlist);
35802+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35803+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
35804+
35805+/* ---------------------------------------------------------------------- */
35806+
35807+static inline struct dentry *au_diropq_create(struct dentry *dentry,
35808+ aufs_bindex_t bindex)
35809+{
35810+ return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
35811+}
35812+
35813+static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
35814+{
35815+ return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
35816+}
35817+
35818+#endif /* __KERNEL__ */
35819+#endif /* __AUFS_WHOUT_H__ */
35820diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
35821--- /usr/share/empty/fs/aufs/wkq.c 1970-01-01 01:00:00.000000000 +0100
35822+++ linux/fs/aufs/wkq.c 2021-02-22 22:51:59.061411920 +0100
35823@@ -0,0 +1,372 @@
35824+// SPDX-License-Identifier: GPL-2.0
35825+/*
35826+ * Copyright (C) 2005-2020 Junjiro R. Okajima
35827+ *
35828+ * This program, aufs is free software; you can redistribute it and/or modify
35829+ * it under the terms of the GNU General Public License as published by
35830+ * the Free Software Foundation; either version 2 of the License, or
35831+ * (at your option) any later version.
35832+ *
35833+ * This program is distributed in the hope that it will be useful,
35834+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
35835+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35836+ * GNU General Public License for more details.
35837+ *
35838+ * You should have received a copy of the GNU General Public License
35839+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
35840+ */
35841+
35842+/*
35843+ * workqueue for asynchronous/super-io operations
35844+ * todo: try new credential scheme
35845+ */
35846+
35847+#include <linux/module.h>
35848+#include "aufs.h"
35849+
35850+/* internal workqueue named AUFS_WKQ_NAME */
35851+
35852+static struct workqueue_struct *au_wkq;
35853+
35854+struct au_wkinfo {
35855+ struct work_struct wk;
35856+ struct kobject *kobj;
35857+
35858+ unsigned int flags; /* see wkq.h */
35859+
35860+ au_wkq_func_t func;
35861+ void *args;
35862+
35863+#ifdef CONFIG_LOCKDEP
35864+ int dont_check;
35865+ struct held_lock **hlock;
35866+#endif
35867+
35868+ struct completion *comp;
35869+};
35870+
35871+/* ---------------------------------------------------------------------- */
35872+/*
35873+ * Aufs passes some operations to the workqueue such as the internal copyup.
35874+ * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
35875+ * job run by workqueue depends upon the locks acquired in the other task.
35876+ * Delegating a small operation to the workqueue, aufs passes its lockdep
35877+ * information too. And the job in the workqueue restores the info in order to
35878+ * pretend as if it acquired those locks. This is just to make LOCKDEP work
35879+ * correctly and expectedly.
35880+ */
35881+
35882+#ifndef CONFIG_LOCKDEP
35883+AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
35884+AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
35885+AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
35886+AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
35887+AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
35888+#else
35889+static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
35890+{
35891+ wkinfo->hlock = NULL;
35892+ wkinfo->dont_check = 0;
35893+}
35894+
35895+/*
35896+ * 1: matched
35897+ * 0: unmatched
35898+ */
35899+static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
35900+{
35901+ static DEFINE_SPINLOCK(spin);
35902+ static struct {
35903+ char *name;
35904+ struct lock_class_key *key;
35905+ } a[] = {
35906+ { .name = "&sbinfo->si_rwsem" },
35907+ { .name = "&finfo->fi_rwsem" },
35908+ { .name = "&dinfo->di_rwsem" },
35909+ { .name = "&iinfo->ii_rwsem" }
35910+ };
35911+ static int set;
35912+ int i;
35913+
35914+ /* lockless read from 'set.' see below */
35915+ if (set == ARRAY_SIZE(a)) {
35916+ for (i = 0; i < ARRAY_SIZE(a); i++)
35917+ if (a[i].key == key)
35918+ goto match;
35919+ goto unmatch;
35920+ }
35921+
35922+ spin_lock(&spin);
35923+ if (set)
35924+ for (i = 0; i < ARRAY_SIZE(a); i++)
35925+ if (a[i].key == key) {
35926+ spin_unlock(&spin);
35927+ goto match;
35928+ }
35929+ for (i = 0; i < ARRAY_SIZE(a); i++) {
35930+ if (a[i].key) {
35931+ if (unlikely(a[i].key == key)) { /* rare but possible */
35932+ spin_unlock(&spin);
35933+ goto match;
35934+ } else
35935+ continue;
35936+ }
35937+ if (strstr(a[i].name, name)) {
35938+ /*
35939+ * the order of these three lines is important for the
35940+ * lockless read above.
35941+ */
35942+ a[i].key = key;
35943+ spin_unlock(&spin);
35944+ set++;
35945+ /* AuDbg("%d, %s\n", set, name); */
35946+ goto match;
35947+ }
35948+ }
35949+ spin_unlock(&spin);
35950+ goto unmatch;
35951+
35952+match:
35953+ return 1;
35954+unmatch:
35955+ return 0;
35956+}
35957+
35958+static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
35959+{
35960+ int err, n;
35961+ struct task_struct *curr;
35962+ struct held_lock **hl, *held_locks, *p;
35963+
35964+ err = 0;
35965+ curr = current;
35966+ wkinfo->dont_check = lockdep_recursing(curr);
35967+ if (wkinfo->dont_check)
35968+ goto out;
35969+ n = curr->lockdep_depth;
35970+ if (!n)
35971+ goto out;
35972+
35973+ err = -ENOMEM;
35974+ wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
35975+ if (unlikely(!wkinfo->hlock))
35976+ goto out;
35977+
35978+ err = 0;
35979+#if 0 /* left for debugging */
35980+ if (0 && au_debug_test())
35981+ lockdep_print_held_locks(curr);
35982+#endif
35983+ held_locks = curr->held_locks;
35984+ hl = wkinfo->hlock;
35985+ while (n--) {
35986+ p = held_locks++;
35987+ if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
35988+ *hl++ = p;
35989+ }
35990+ *hl = NULL;
35991+
35992+out:
35993+ return err;
35994+}
35995+
35996+static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
35997+{
35998+ au_kfree_try_rcu(wkinfo->hlock);
35999+}
36000+
36001+static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36002+{
36003+ struct held_lock *p, **hl = wkinfo->hlock;
36004+ int subclass;
36005+
36006+ if (wkinfo->dont_check)
36007+ lockdep_off();
36008+ if (!hl)
36009+ return;
36010+ while ((p = *hl++)) { /* assignment */
36011+ subclass = lockdep_hlock_class(p)->subclass;
36012+ /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36013+ if (p->read)
36014+ rwsem_acquire_read(p->instance, subclass, 0,
36015+ /*p->acquire_ip*/_RET_IP_);
36016+ else
36017+ rwsem_acquire(p->instance, subclass, 0,
36018+ /*p->acquire_ip*/_RET_IP_);
36019+ }
36020+}
36021+
36022+static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36023+{
36024+ struct held_lock *p, **hl = wkinfo->hlock;
36025+
36026+ if (wkinfo->dont_check)
36027+ lockdep_on();
36028+ if (!hl)
36029+ return;
36030+ while ((p = *hl++)) /* assignment */
36031+ rwsem_release(p->instance, /*p->acquire_ip*/_RET_IP_);
36032+}
36033+#endif
36034+
36035+static void wkq_func(struct work_struct *wk)
36036+{
36037+ struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36038+
36039+ AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
36040+ AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36041+
36042+ au_wkq_lockdep_pre(wkinfo);
36043+ wkinfo->func(wkinfo->args);
36044+ au_wkq_lockdep_post(wkinfo);
36045+ if (au_ftest_wkq(wkinfo->flags, WAIT))
36046+ complete(wkinfo->comp);
36047+ else {
36048+ kobject_put(wkinfo->kobj);
36049+ module_put(THIS_MODULE); /* todo: ?? */
36050+ au_kfree_rcu(wkinfo);
36051+ }
36052+}
36053+
36054+/*
36055+ * Since struct completion is large, try allocating it dynamically.
36056+ */
36057+#define AuWkqCompDeclare(name) struct completion *comp = NULL
36058+
36059+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36060+{
36061+ *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36062+ if (*comp) {
36063+ init_completion(*comp);
36064+ wkinfo->comp = *comp;
36065+ return 0;
36066+ }
36067+ return -ENOMEM;
36068+}
36069+
36070+static void au_wkq_comp_free(struct completion *comp)
36071+{
36072+ au_kfree_rcu(comp);
36073+}
36074+
36075+static void au_wkq_run(struct au_wkinfo *wkinfo)
36076+{
36077+ if (au_ftest_wkq(wkinfo->flags, NEST)) {
36078+ if (au_wkq_test()) {
36079+ AuWarn1("wkq from wkq, unless silly-rename on NFS,"
36080+ " due to a dead dir by UDBA,"
36081+ " or async xino write?\n");
36082+ AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36083+ }
36084+ } else
36085+ au_dbg_verify_kthread();
36086+
36087+ if (au_ftest_wkq(wkinfo->flags, WAIT)) {
36088+ INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
36089+ queue_work(au_wkq, &wkinfo->wk);
36090+ } else {
36091+ INIT_WORK(&wkinfo->wk, wkq_func);
36092+ schedule_work(&wkinfo->wk);
36093+ }
36094+}
36095+
36096+/*
36097+ * Be careful. It is easy to make deadlock happen.
36098+ * processA: lock, wkq and wait
36099+ * processB: wkq and wait, lock in wkq
36100+ * --> deadlock
36101+ */
36102+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
36103+{
36104+ int err;
36105+ AuWkqCompDeclare(comp);
36106+ struct au_wkinfo wkinfo = {
36107+ .flags = flags,
36108+ .func = func,
36109+ .args = args
36110+ };
36111+
36112+ err = au_wkq_comp_alloc(&wkinfo, &comp);
36113+ if (unlikely(err))
36114+ goto out;
36115+ err = au_wkq_lockdep_alloc(&wkinfo);
36116+ if (unlikely(err))
36117+ goto out_comp;
36118+ if (!err) {
36119+ au_wkq_run(&wkinfo);
36120+ /* no timeout, no interrupt */
36121+ wait_for_completion(wkinfo.comp);
36122+ }
36123+ au_wkq_lockdep_free(&wkinfo);
36124+
36125+out_comp:
36126+ au_wkq_comp_free(comp);
36127+out:
36128+ destroy_work_on_stack(&wkinfo.wk);
36129+ return err;
36130+}
36131+
36132+/*
36133+ * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36134+ * problem in a concurrent umounting.
36135+ */
36136+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36137+ unsigned int flags)
36138+{
36139+ int err;
36140+ struct au_wkinfo *wkinfo;
36141+
36142+ atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
36143+
36144+ /*
36145+ * wkq_func() must free this wkinfo.
36146+ * it highly depends upon the implementation of workqueue.
36147+ */
36148+ err = 0;
36149+ wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36150+ if (wkinfo) {
36151+ wkinfo->kobj = &au_sbi(sb)->si_kobj;
36152+ wkinfo->flags = flags & ~AuWkq_WAIT;
36153+ wkinfo->func = func;
36154+ wkinfo->args = args;
36155+ wkinfo->comp = NULL;
36156+ au_wkq_lockdep_init(wkinfo);
36157+ kobject_get(wkinfo->kobj);
36158+ __module_get(THIS_MODULE); /* todo: ?? */
36159+
36160+ au_wkq_run(wkinfo);
36161+ } else {
36162+ err = -ENOMEM;
36163+ au_nwt_done(&au_sbi(sb)->si_nowait);
36164+ }
36165+
36166+ return err;
36167+}
36168+
36169+/* ---------------------------------------------------------------------- */
36170+
36171+void au_nwt_init(struct au_nowait_tasks *nwt)
36172+{
36173+ atomic_set(&nwt->nw_len, 0);
36174+ /* smp_mb(); */ /* atomic_set */
36175+ init_waitqueue_head(&nwt->nw_wq);
36176+}
36177+
36178+void au_wkq_fin(void)
36179+{
36180+ destroy_workqueue(au_wkq);
36181+}
36182+
36183+int __init au_wkq_init(void)
36184+{
36185+ int err;
36186+
36187+ err = 0;
36188+ au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
36189+ if (IS_ERR(au_wkq))
36190+ err = PTR_ERR(au_wkq);
36191+ else if (!au_wkq)
36192+ err = -ENOMEM;
36193+
36194+ return err;
36195+}
36196diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36197--- /usr/share/empty/fs/aufs/wkq.h 1970-01-01 01:00:00.000000000 +0100
36198+++ linux/fs/aufs/wkq.h 2021-02-22 22:51:59.061411920 +0100
36199@@ -0,0 +1,89 @@
36200+/* SPDX-License-Identifier: GPL-2.0 */
36201+/*
36202+ * Copyright (C) 2005-2020 Junjiro R. Okajima
36203+ *
36204+ * This program, aufs is free software; you can redistribute it and/or modify
36205+ * it under the terms of the GNU General Public License as published by
36206+ * the Free Software Foundation; either version 2 of the License, or
36207+ * (at your option) any later version.
36208+ *
36209+ * This program is distributed in the hope that it will be useful,
36210+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36211+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36212+ * GNU General Public License for more details.
36213+ *
36214+ * You should have received a copy of the GNU General Public License
36215+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
36216+ */
36217+
36218+/*
36219+ * workqueue for asynchronous/super-io operations
36220+ * todo: try new credentials management scheme
36221+ */
36222+
36223+#ifndef __AUFS_WKQ_H__
36224+#define __AUFS_WKQ_H__
36225+
36226+#ifdef __KERNEL__
36227+
36228+#include <linux/wait.h>
36229+
36230+struct super_block;
36231+
36232+/* ---------------------------------------------------------------------- */
36233+
36234+/*
36235+ * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36236+ */
36237+struct au_nowait_tasks {
36238+ atomic_t nw_len;
36239+ wait_queue_head_t nw_wq;
36240+};
36241+
36242+/* ---------------------------------------------------------------------- */
36243+
36244+typedef void (*au_wkq_func_t)(void *args);
36245+
36246+/* wkq flags */
36247+#define AuWkq_WAIT 1
36248+#define AuWkq_NEST (1 << 1)
36249+#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
36250+#define au_fset_wkq(flags, name) \
36251+ do { (flags) |= AuWkq_##name; } while (0)
36252+#define au_fclr_wkq(flags, name) \
36253+ do { (flags) &= ~AuWkq_##name; } while (0)
36254+
36255+/* wkq.c */
36256+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
36257+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36258+ unsigned int flags);
36259+void au_nwt_init(struct au_nowait_tasks *nwt);
36260+int __init au_wkq_init(void);
36261+void au_wkq_fin(void);
36262+
36263+/* ---------------------------------------------------------------------- */
36264+
36265+static inline int au_wkq_test(void)
36266+{
36267+ return current->flags & PF_WQ_WORKER;
36268+}
36269+
36270+static inline int au_wkq_wait(au_wkq_func_t func, void *args)
36271+{
36272+ return au_wkq_do_wait(AuWkq_WAIT, func, args);
36273+}
36274+
36275+static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36276+{
36277+ if (atomic_dec_and_test(&nwt->nw_len))
36278+ wake_up_all(&nwt->nw_wq);
36279+}
36280+
36281+static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36282+{
36283+ wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
36284+ return 0;
36285+}
36286+
36287+#endif /* __KERNEL__ */
36288+#endif /* __AUFS_WKQ_H__ */
36289diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36290--- /usr/share/empty/fs/aufs/xattr.c 1970-01-01 01:00:00.000000000 +0100
36291+++ linux/fs/aufs/xattr.c 2021-02-22 22:51:59.061411920 +0100
36292@@ -0,0 +1,356 @@
36293+// SPDX-License-Identifier: GPL-2.0
36294+/*
36295+ * Copyright (C) 2014-2020 Junjiro R. Okajima
36296+ *
36297+ * This program, aufs is free software; you can redistribute it and/or modify
36298+ * it under the terms of the GNU General Public License as published by
36299+ * the Free Software Foundation; either version 2 of the License, or
36300+ * (at your option) any later version.
36301+ *
36302+ * This program is distributed in the hope that it will be useful,
36303+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36304+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36305+ * GNU General Public License for more details.
36306+ *
36307+ * You should have received a copy of the GNU General Public License
36308+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
36309+ */
36310+
36311+/*
36312+ * handling xattr functions
36313+ */
36314+
36315+#include <linux/fs.h>
36316+#include <linux/posix_acl_xattr.h>
36317+#include <linux/xattr.h>
36318+#include "aufs.h"
36319+
36320+static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36321+{
36322+ if (!ignore_flags)
36323+ goto out;
36324+ switch (err) {
36325+ case -ENOMEM:
36326+ case -EDQUOT:
36327+ goto out;
36328+ }
36329+
36330+ if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36331+ err = 0;
36332+ goto out;
36333+ }
36334+
36335+#define cmp(brattr, prefix) do { \
36336+ if (!strncmp(name, XATTR_##prefix##_PREFIX, \
36337+ XATTR_##prefix##_PREFIX_LEN)) { \
36338+ if (ignore_flags & AuBrAttr_ICEX_##brattr) \
36339+ err = 0; \
36340+ goto out; \
36341+ } \
36342+ } while (0)
36343+
36344+ cmp(SEC, SECURITY);
36345+ cmp(SYS, SYSTEM);
36346+ cmp(TR, TRUSTED);
36347+ cmp(USR, USER);
36348+#undef cmp
36349+
36350+ if (ignore_flags & AuBrAttr_ICEX_OTH)
36351+ err = 0;
36352+
36353+out:
36354+ return err;
36355+}
36356+
36357+static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36358+
36359+static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
36360+ char *name, char **buf, unsigned int ignore_flags,
36361+ unsigned int verbose)
36362+{
36363+ int err;
36364+ ssize_t ssz;
36365+ struct inode *h_idst;
36366+
36367+ ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
36368+ err = ssz;
36369+ if (unlikely(err <= 0)) {
36370+ if (err == -ENODATA
36371+ || (err == -EOPNOTSUPP
36372+ && ((ignore_flags & au_xattr_out_of_list)
36373+ || (au_test_nfs_noacl(d_inode(h_src))
36374+ && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36375+ || !strcmp(name,
36376+ XATTR_NAME_POSIX_ACL_DEFAULT))))
36377+ ))
36378+ err = 0;
36379+ if (err && (verbose || au_debug_test()))
36380+ pr_err("%s, err %d\n", name, err);
36381+ goto out;
36382+ }
36383+
36384+ /* unlock it temporary */
36385+ h_idst = d_inode(h_dst);
36386+ inode_unlock(h_idst);
36387+ err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
36388+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36389+ if (unlikely(err)) {
36390+ if (verbose || au_debug_test())
36391+ pr_err("%s, err %d\n", name, err);
36392+ err = au_xattr_ignore(err, name, ignore_flags);
36393+ }
36394+
36395+out:
36396+ return err;
36397+}
36398+
36399+int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
36400+ unsigned int verbose)
36401+{
36402+ int err, unlocked, acl_access, acl_default;
36403+ ssize_t ssz;
36404+ struct inode *h_isrc, *h_idst;
36405+ char *value, *p, *o, *e;
36406+
36407+ /* try stopping to update the source inode while we are referencing */
36408+ /* there should not be the parent-child relationship between them */
36409+ h_isrc = d_inode(h_src);
36410+ h_idst = d_inode(h_dst);
36411+ inode_unlock(h_idst);
36412+ inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
36413+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36414+ unlocked = 0;
36415+
36416+ /* some filesystems don't list POSIX ACL, for example tmpfs */
36417+ ssz = vfs_listxattr(h_src, NULL, 0);
36418+ err = ssz;
36419+ if (unlikely(err < 0)) {
36420+ AuTraceErr(err);
36421+ if (err == -ENODATA
36422+ || err == -EOPNOTSUPP)
36423+ err = 0; /* ignore */
36424+ goto out;
36425+ }
36426+
36427+ err = 0;
36428+ p = NULL;
36429+ o = NULL;
36430+ if (ssz) {
36431+ err = -ENOMEM;
36432+ p = kmalloc(ssz, GFP_NOFS);
36433+ o = p;
36434+ if (unlikely(!p))
36435+ goto out;
36436+ err = vfs_listxattr(h_src, p, ssz);
36437+ }
36438+ inode_unlock_shared(h_isrc);
36439+ unlocked = 1;
36440+ AuDbg("err %d, ssz %zd\n", err, ssz);
36441+ if (unlikely(err < 0))
36442+ goto out_free;
36443+
36444+ err = 0;
36445+ e = p + ssz;
36446+ value = NULL;
36447+ acl_access = 0;
36448+ acl_default = 0;
36449+ while (!err && p < e) {
36450+ acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36451+ sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36452+ acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36453+ sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36454+ - 1);
36455+ err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36456+ verbose);
36457+ p += strlen(p) + 1;
36458+ }
36459+ AuTraceErr(err);
36460+ ignore_flags |= au_xattr_out_of_list;
36461+ if (!err && !acl_access) {
36462+ err = au_do_cpup_xattr(h_dst, h_src,
36463+ XATTR_NAME_POSIX_ACL_ACCESS, &value,
36464+ ignore_flags, verbose);
36465+ AuTraceErr(err);
36466+ }
36467+ if (!err && !acl_default) {
36468+ err = au_do_cpup_xattr(h_dst, h_src,
36469+ XATTR_NAME_POSIX_ACL_DEFAULT, &value,
36470+ ignore_flags, verbose);
36471+ AuTraceErr(err);
36472+ }
36473+
36474+ au_kfree_try_rcu(value);
36475+
36476+out_free:
36477+ au_kfree_try_rcu(o);
36478+out:
36479+ if (!unlocked)
36480+ inode_unlock_shared(h_isrc);
36481+ AuTraceErr(err);
36482+ return err;
36483+}
36484+
36485+/* ---------------------------------------------------------------------- */
36486+
36487+static int au_smack_reentering(struct super_block *sb)
36488+{
36489+#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
36490+ /*
36491+ * as a part of lookup, smack_d_instantiate() is called, and it calls
36492+ * i_op->getxattr(). ouch.
36493+ */
36494+ return si_pid_test(sb);
36495+#else
36496+ return 0;
36497+#endif
36498+}
36499+
36500+enum {
36501+ AU_XATTR_LIST,
36502+ AU_XATTR_GET
36503+};
36504+
36505+struct au_lgxattr {
36506+ int type;
36507+ union {
36508+ struct {
36509+ char *list;
36510+ size_t size;
36511+ } list;
36512+ struct {
36513+ const char *name;
36514+ void *value;
36515+ size_t size;
36516+ } get;
36517+ } u;
36518+};
36519+
36520+static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
36521+ struct au_lgxattr *arg)
36522+{
36523+ ssize_t err;
36524+ int reenter;
36525+ struct path h_path;
36526+ struct super_block *sb;
36527+
36528+ sb = dentry->d_sb;
36529+ reenter = au_smack_reentering(sb);
36530+ if (!reenter) {
36531+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36532+ if (unlikely(err))
36533+ goto out;
36534+ }
36535+ err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
36536+ if (unlikely(err))
36537+ goto out_si;
36538+ if (unlikely(!h_path.dentry))
36539+ /* illegally overlapped or something */
36540+ goto out_di; /* pretending success */
36541+
36542+ /* always topmost entry only */
36543+ switch (arg->type) {
36544+ case AU_XATTR_LIST:
36545+ err = vfs_listxattr(h_path.dentry,
36546+ arg->u.list.list, arg->u.list.size);
36547+ break;
36548+ case AU_XATTR_GET:
36549+ AuDebugOn(d_is_negative(h_path.dentry));
36550+ err = vfs_getxattr(h_path.dentry,
36551+ arg->u.get.name, arg->u.get.value,
36552+ arg->u.get.size);
36553+ break;
36554+ }
36555+
36556+out_di:
36557+ if (!reenter)
36558+ di_read_unlock(dentry, AuLock_IR);
36559+out_si:
36560+ if (!reenter)
36561+ si_read_unlock(sb);
36562+out:
36563+ AuTraceErr(err);
36564+ return err;
36565+}
36566+
36567+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36568+{
36569+ struct au_lgxattr arg = {
36570+ .type = AU_XATTR_LIST,
36571+ .u.list = {
36572+ .list = list,
36573+ .size = size
36574+ },
36575+ };
36576+
36577+ return au_lgxattr(dentry, /*inode*/NULL, &arg);
36578+}
36579+
36580+static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
36581+ const char *name, void *value, size_t size)
36582+{
36583+ struct au_lgxattr arg = {
36584+ .type = AU_XATTR_GET,
36585+ .u.get = {
36586+ .name = name,
36587+ .value = value,
36588+ .size = size
36589+ },
36590+ };
36591+
36592+ return au_lgxattr(dentry, inode, &arg);
36593+}
36594+
36595+static int au_setxattr(struct dentry *dentry, struct inode *inode,
36596+ const char *name, const void *value, size_t size,
36597+ int flags)
36598+{
36599+ struct au_sxattr arg = {
36600+ .type = AU_XATTR_SET,
36601+ .u.set = {
36602+ .name = name,
36603+ .value = value,
36604+ .size = size,
36605+ .flags = flags
36606+ },
36607+ };
36608+
36609+ return au_sxattr(dentry, inode, &arg);
36610+}
36611+
36612+/* ---------------------------------------------------------------------- */
36613+
36614+static int au_xattr_get(const struct xattr_handler *handler,
36615+ struct dentry *dentry, struct inode *inode,
36616+ const char *name, void *buffer, size_t size)
36617+{
36618+ return au_getxattr(dentry, inode, name, buffer, size);
36619+}
36620+
36621+static int au_xattr_set(const struct xattr_handler *handler,
36622+ struct dentry *dentry, struct inode *inode,
36623+ const char *name, const void *value, size_t size,
36624+ int flags)
36625+{
36626+ return au_setxattr(dentry, inode, name, value, size, flags);
36627+}
36628+
36629+static const struct xattr_handler au_xattr_handler = {
36630+ .name = "",
36631+ .prefix = "",
36632+ .get = au_xattr_get,
36633+ .set = au_xattr_set
36634+};
36635+
36636+static const struct xattr_handler *au_xattr_handlers[] = {
36637+#ifdef CONFIG_FS_POSIX_ACL
36638+ &posix_acl_access_xattr_handler,
36639+ &posix_acl_default_xattr_handler,
36640+#endif
36641+ &au_xattr_handler, /* must be last */
36642+ NULL
36643+};
36644+
36645+void au_xattr_init(struct super_block *sb)
36646+{
36647+ sb->s_xattr = au_xattr_handlers;
36648+}
36649diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
36650--- /usr/share/empty/fs/aufs/xino.c 1970-01-01 01:00:00.000000000 +0100
36651+++ linux/fs/aufs/xino.c 2021-02-22 22:51:59.061411920 +0100
36652@@ -0,0 +1,1925 @@
36653+// SPDX-License-Identifier: GPL-2.0
36654+/*
36655+ * Copyright (C) 2005-2020 Junjiro R. Okajima
36656+ *
36657+ * This program, aufs is free software; you can redistribute it and/or modify
36658+ * it under the terms of the GNU General Public License as published by
36659+ * the Free Software Foundation; either version 2 of the License, or
36660+ * (at your option) any later version.
36661+ *
36662+ * This program is distributed in the hope that it will be useful,
36663+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36664+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36665+ * GNU General Public License for more details.
36666+ *
36667+ * You should have received a copy of the GNU General Public License
36668+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
36669+ */
36670+
36671+/*
36672+ * external inode number translation table and bitmap
36673+ *
36674+ * things to consider
36675+ * - the lifetime
36676+ * + au_xino object
36677+ * + XINO files (xino, xib, xigen)
36678+ * + dynamic debugfs entries (xiN)
36679+ * + static debugfs entries (xib, xigen)
36680+ * + static sysfs entry (xi_path)
36681+ * - several entry points to handle them.
36682+ * + mount(2) without xino option (default)
36683+ * + mount(2) with xino option
36684+ * + mount(2) with noxino option
36685+ * + umount(2)
36686+ * + remount with add/del branches
36687+ * + remount with xino/noxino options
36688+ */
36689+
36690+#include <linux/seq_file.h>
36691+#include <linux/statfs.h>
36692+#include "aufs.h"
36693+
36694+static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
36695+ aufs_bindex_t bbot,
36696+ struct super_block *h_sb)
36697+{
36698+ /* todo: try binary-search if the branches are many */
36699+ for (; btop <= bbot; btop++)
36700+ if (h_sb == au_sbr_sb(sb, btop))
36701+ return btop;
36702+ return -1;
36703+}
36704+
36705+/*
36706+ * find another branch who is on the same filesystem of the specified
36707+ * branch{@btgt}. search until @bbot.
36708+ */
36709+static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
36710+ aufs_bindex_t bbot)
36711+{
36712+ aufs_bindex_t bindex;
36713+ struct super_block *tgt_sb;
36714+
36715+ tgt_sb = au_sbr_sb(sb, btgt);
36716+ bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
36717+ if (bindex < 0)
36718+ bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
36719+
36720+ return bindex;
36721+}
36722+
36723+/* ---------------------------------------------------------------------- */
36724+
36725+/*
36726+ * stop unnecessary notify events at creating xino files
36727+ */
36728+
36729+aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
36730+{
36731+ aufs_bindex_t bfound, bindex, bbot;
36732+ struct dentry *parent;
36733+ struct au_branch *br;
36734+
36735+ bfound = -1;
36736+ parent = dentry->d_parent; /* safe d_parent access */
36737+ bbot = au_sbbot(sb);
36738+ for (bindex = 0; bindex <= bbot; bindex++) {
36739+ br = au_sbr(sb, bindex);
36740+ if (au_br_dentry(br) == parent) {
36741+ bfound = bindex;
36742+ break;
36743+ }
36744+ }
36745+
36746+ AuDbg("bfound b%d\n", bfound);
36747+ return bfound;
36748+}
36749+
36750+struct au_xino_lock_dir {
36751+ struct au_hinode *hdir;
36752+ struct dentry *parent;
36753+ struct inode *dir;
36754+};
36755+
36756+static struct dentry *au_dget_parent_lock(struct dentry *dentry,
36757+ unsigned int lsc)
36758+{
36759+ struct dentry *parent;
36760+ struct inode *dir;
36761+
36762+ parent = dget_parent(dentry);
36763+ dir = d_inode(parent);
36764+ inode_lock_nested(dir, lsc);
36765+#if 0 /* it should not happen */
36766+ spin_lock(&dentry->d_lock);
36767+ if (unlikely(dentry->d_parent != parent)) {
36768+ spin_unlock(&dentry->d_lock);
36769+ inode_unlock(dir);
36770+ dput(parent);
36771+ parent = NULL;
36772+ goto out;
36773+ }
36774+ spin_unlock(&dentry->d_lock);
36775+
36776+out:
36777+#endif
36778+ return parent;
36779+}
36780+
36781+static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
36782+ struct au_xino_lock_dir *ldir)
36783+{
36784+ aufs_bindex_t bindex;
36785+
36786+ ldir->hdir = NULL;
36787+ bindex = au_xi_root(sb, xipath->dentry);
36788+ if (bindex >= 0) {
36789+ /* rw branch root */
36790+ ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
36791+ au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
36792+ } else {
36793+ /* other */
36794+ ldir->parent = au_dget_parent_lock(xipath->dentry,
36795+ AuLsc_I_PARENT);
36796+ ldir->dir = d_inode(ldir->parent);
36797+ }
36798+}
36799+
36800+static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
36801+{
36802+ if (ldir->hdir)
36803+ au_hn_inode_unlock(ldir->hdir);
36804+ else {
36805+ inode_unlock(ldir->dir);
36806+ dput(ldir->parent);
36807+ }
36808+}
36809+
36810+/* ---------------------------------------------------------------------- */
36811+
36812+/*
36813+ * create and set a new xino file
36814+ */
36815+struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
36816+ int wbrtop)
36817+{
36818+ struct file *file;
36819+ struct dentry *h_parent, *d;
36820+ struct inode *h_dir, *inode;
36821+ int err;
36822+ static DEFINE_MUTEX(mtx);
36823+
36824+ /*
36825+ * at mount-time, and the xino file is the default path,
36826+ * hnotify is disabled so we have no notify events to ignore.
36827+ * when a user specified the xino, we cannot get au_hdir to be ignored.
36828+ */
36829+ if (!wbrtop)
36830+ mutex_lock(&mtx);
36831+ file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
36832+ /* | __FMODE_NONOTIFY */,
36833+ 0666);
36834+ if (IS_ERR(file)) {
36835+ if (!wbrtop)
36836+ mutex_unlock(&mtx);
36837+ if (!silent)
36838+ pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
36839+ return file;
36840+ }
36841+
36842+ /* keep file count */
36843+ err = 0;
36844+ d = file->f_path.dentry;
36845+ h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
36846+ if (!wbrtop)
36847+ mutex_unlock(&mtx);
36848+ /* mnt_want_write() is unnecessary here */
36849+ h_dir = d_inode(h_parent);
36850+ inode = file_inode(file);
36851+ /* no delegation since it is just created */
36852+ if (inode->i_nlink)
36853+ err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
36854+ /*force*/0);
36855+ inode_unlock(h_dir);
36856+ dput(h_parent);
36857+ if (unlikely(err)) {
36858+ if (!silent)
36859+ pr_err("unlink %s(%d)\n", fpath, err);
36860+ goto out;
36861+ }
36862+
36863+ err = -EINVAL;
36864+ if (unlikely(sb == d->d_sb)) {
36865+ if (!silent)
36866+ pr_err("%s must be outside\n", fpath);
36867+ goto out;
36868+ }
36869+ if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
36870+ if (!silent)
36871+ pr_err("xino doesn't support %s(%s)\n",
36872+ fpath, au_sbtype(d->d_sb));
36873+ goto out;
36874+ }
36875+ return file; /* success */
36876+
36877+out:
36878+ fput(file);
36879+ file = ERR_PTR(err);
36880+ return file;
36881+}
36882+
36883+/*
36884+ * create a new xinofile at the same place/path as @base.
36885+ */
36886+struct file *au_xino_create2(struct super_block *sb, struct path *base,
36887+ struct file *copy_src)
36888+{
36889+ struct file *file;
36890+ struct dentry *dentry, *parent;
36891+ struct inode *dir, *delegated;
36892+ struct qstr *name;
36893+ struct path path;
36894+ int err, do_unlock;
36895+ struct au_xino_lock_dir ldir;
36896+
36897+ do_unlock = 1;
36898+ au_xino_lock_dir(sb, base, &ldir);
36899+ dentry = base->dentry;
36900+ parent = dentry->d_parent; /* dir inode is locked */
36901+ dir = d_inode(parent);
36902+ IMustLock(dir);
36903+
36904+ name = &dentry->d_name;
36905+ path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
36906+ if (IS_ERR(path.dentry)) {
36907+ file = (void *)path.dentry;
36908+ pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
36909+ goto out;
36910+ }
36911+
36912+ /* no need to mnt_want_write() since we call dentry_open() later */
36913+ err = vfs_create(dir, path.dentry, 0666, NULL);
36914+ if (unlikely(err)) {
36915+ file = ERR_PTR(err);
36916+ pr_err("%pd create err %d\n", dentry, err);
36917+ goto out_dput;
36918+ }
36919+
36920+ path.mnt = base->mnt;
36921+ file = vfsub_dentry_open(&path,
36922+ O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
36923+ /* | __FMODE_NONOTIFY */);
36924+ if (IS_ERR(file)) {
36925+ pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
36926+ goto out_dput;
36927+ }
36928+
36929+ delegated = NULL;
36930+ err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
36931+ au_xino_unlock_dir(&ldir);
36932+ do_unlock = 0;
36933+ if (unlikely(err == -EWOULDBLOCK)) {
36934+ pr_warn("cannot retry for NFSv4 delegation"
36935+ " for an internal unlink\n");
36936+ iput(delegated);
36937+ }
36938+ if (unlikely(err)) {
36939+ pr_err("%pd unlink err %d\n", dentry, err);
36940+ goto out_fput;
36941+ }
36942+
36943+ if (copy_src) {
36944+ /* no one can touch copy_src xino */
36945+ err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
36946+ if (unlikely(err)) {
36947+ pr_err("%pd copy err %d\n", dentry, err);
36948+ goto out_fput;
36949+ }
36950+ }
36951+ goto out_dput; /* success */
36952+
36953+out_fput:
36954+ fput(file);
36955+ file = ERR_PTR(err);
36956+out_dput:
36957+ dput(path.dentry);
36958+out:
36959+ if (do_unlock)
36960+ au_xino_unlock_dir(&ldir);
36961+ return file;
36962+}
36963+
36964+struct file *au_xino_file1(struct au_xino *xi)
36965+{
36966+ struct file *file;
36967+ unsigned int u, nfile;
36968+
36969+ file = NULL;
36970+ nfile = xi->xi_nfile;
36971+ for (u = 0; u < nfile; u++) {
36972+ file = xi->xi_file[u];
36973+ if (file)
36974+ break;
36975+ }
36976+
36977+ return file;
36978+}
36979+
36980+static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
36981+{
36982+ int err;
36983+ struct file *f;
36984+ void *p;
36985+
36986+ if (file)
36987+ get_file(file);
36988+
36989+ err = 0;
36990+ f = NULL;
36991+ if (idx < xi->xi_nfile) {
36992+ f = xi->xi_file[idx];
36993+ if (f)
36994+ fput(f);
36995+ } else {
36996+ p = au_kzrealloc(xi->xi_file,
36997+ sizeof(*xi->xi_file) * xi->xi_nfile,
36998+ sizeof(*xi->xi_file) * (idx + 1),
36999+ GFP_NOFS, /*may_shrink*/0);
37000+ if (p) {
37001+ MtxMustLock(&xi->xi_mtx);
37002+ xi->xi_file = p;
37003+ xi->xi_nfile = idx + 1;
37004+ } else {
37005+ err = -ENOMEM;
37006+ if (file)
37007+ fput(file);
37008+ goto out;
37009+ }
37010+ }
37011+ xi->xi_file[idx] = file;
37012+
37013+out:
37014+ return err;
37015+}
37016+
37017+/*
37018+ * if @xinew->xi is not set, then create new xigen file.
37019+ */
37020+struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37021+{
37022+ struct file *file;
37023+ int err;
37024+
37025+ SiMustAnyLock(sb);
37026+
37027+ file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37028+ if (IS_ERR(file)) {
37029+ err = PTR_ERR(file);
37030+ pr_err("%s[%d], err %d\n",
37031+ xinew->xi ? "xino" : "xigen",
37032+ xinew->idx, err);
37033+ goto out;
37034+ }
37035+
37036+ if (xinew->xi)
37037+ err = au_xino_file_set(xinew->xi, xinew->idx, file);
37038+ else {
37039+ BUG();
37040+ /* todo: make xigen file an array */
37041+ /* err = au_xigen_file_set(sb, xinew->idx, file); */
37042+ }
37043+ fput(file);
37044+ if (unlikely(err))
37045+ file = ERR_PTR(err);
37046+
37047+out:
37048+ return file;
37049+}
37050+
37051+/* ---------------------------------------------------------------------- */
37052+
37053+/*
37054+ * truncate xino files
37055+ */
37056+static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37057+ int idx, struct kstatfs *st)
37058+{
37059+ int err;
37060+ blkcnt_t blocks;
37061+ struct file *file, *new_xino;
37062+ struct au_xi_new xinew = {
37063+ .idx = idx
37064+ };
37065+
37066+ err = 0;
37067+ xinew.xi = au_sbr(sb, bindex)->br_xino;
37068+ file = au_xino_file(xinew.xi, idx);
37069+ if (!file)
37070+ goto out;
37071+
37072+ xinew.base = &file->f_path;
37073+ err = vfs_statfs(xinew.base, st);
37074+ if (unlikely(err)) {
37075+ AuErr1("statfs err %d, ignored\n", err);
37076+ err = 0;
37077+ goto out;
37078+ }
37079+
37080+ blocks = file_inode(file)->i_blocks;
37081+ pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37082+ bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37083+
37084+ xinew.copy_src = file;
37085+ new_xino = au_xi_new(sb, &xinew);
37086+ if (IS_ERR(new_xino)) {
37087+ err = PTR_ERR(new_xino);
37088+ pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37089+ goto out;
37090+ }
37091+
37092+ err = vfs_statfs(&new_xino->f_path, st);
37093+ if (!err)
37094+ pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37095+ bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37096+ st->f_bfree, st->f_blocks);
37097+ else {
37098+ AuErr1("statfs err %d, ignored\n", err);
37099+ err = 0;
37100+ }
37101+
37102+out:
37103+ return err;
37104+}
37105+
37106+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37107+{
37108+ int err, i;
37109+ unsigned long jiffy;
37110+ aufs_bindex_t bbot;
37111+ struct kstatfs *st;
37112+ struct au_branch *br;
37113+ struct au_xino *xi;
37114+
37115+ err = -ENOMEM;
37116+ st = kmalloc(sizeof(*st), GFP_NOFS);
37117+ if (unlikely(!st))
37118+ goto out;
37119+
37120+ err = -EINVAL;
37121+ bbot = au_sbbot(sb);
37122+ if (unlikely(bindex < 0 || bbot < bindex))
37123+ goto out_st;
37124+
37125+ err = 0;
37126+ jiffy = jiffies;
37127+ br = au_sbr(sb, bindex);
37128+ xi = br->br_xino;
37129+ for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37130+ err = au_xino_do_trunc(sb, bindex, i, st);
37131+ if (!err)
37132+ au_sbi(sb)->si_xino_jiffy = jiffy;
37133+
37134+out_st:
37135+ au_kfree_rcu(st);
37136+out:
37137+ return err;
37138+}
37139+
37140+struct xino_do_trunc_args {
37141+ struct super_block *sb;
37142+ struct au_branch *br;
37143+ int idx;
37144+};
37145+
37146+static void xino_do_trunc(void *_args)
37147+{
37148+ struct xino_do_trunc_args *args = _args;
37149+ struct super_block *sb;
37150+ struct au_branch *br;
37151+ struct inode *dir;
37152+ int err, idx;
37153+ aufs_bindex_t bindex;
37154+
37155+ err = 0;
37156+ sb = args->sb;
37157+ dir = d_inode(sb->s_root);
37158+ br = args->br;
37159+ idx = args->idx;
37160+
37161+ si_noflush_write_lock(sb);
37162+ ii_read_lock_parent(dir);
37163+ bindex = au_br_index(sb, br->br_id);
37164+ err = au_xino_trunc(sb, bindex, idx);
37165+ ii_read_unlock(dir);
37166+ if (unlikely(err))
37167+ pr_warn("err b%d, (%d)\n", bindex, err);
37168+ atomic_dec(&br->br_xino->xi_truncating);
37169+ au_lcnt_dec(&br->br_count);
37170+ si_write_unlock(sb);
37171+ au_nwt_done(&au_sbi(sb)->si_nowait);
37172+ au_kfree_rcu(args);
37173+}
37174+
37175+/*
37176+ * returns the index in the xi_file array whose corresponding file is necessary
37177+ * to truncate, or -1 which means no need to truncate.
37178+ */
37179+static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37180+{
37181+ int err;
37182+ unsigned int u;
37183+ struct kstatfs st;
37184+ struct au_sbinfo *sbinfo;
37185+ struct au_xino *xi;
37186+ struct file *file;
37187+
37188+ /* todo: si_xino_expire and the ratio should be customizable */
37189+ sbinfo = au_sbi(sb);
37190+ if (time_before(jiffies,
37191+ sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37192+ return -1;
37193+
37194+ /* truncation border */
37195+ xi = br->br_xino;
37196+ for (u = 0; u < xi->xi_nfile; u++) {
37197+ file = au_xino_file(xi, u);
37198+ if (!file)
37199+ continue;
37200+
37201+ err = vfs_statfs(&file->f_path, &st);
37202+ if (unlikely(err)) {
37203+ AuErr1("statfs err %d, ignored\n", err);
37204+ return -1;
37205+ }
37206+ if (div64_u64(st.f_bfree * 100, st.f_blocks)
37207+ >= AUFS_XINO_DEF_TRUNC)
37208+ return u;
37209+ }
37210+
37211+ return -1;
37212+}
37213+
37214+static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37215+{
37216+ int idx;
37217+ struct xino_do_trunc_args *args;
37218+ int wkq_err;
37219+
37220+ idx = xino_trunc_test(sb, br);
37221+ if (idx < 0)
37222+ return;
37223+
37224+ if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
37225+ goto out;
37226+
37227+ /* lock and kfree() will be called in trunc_xino() */
37228+ args = kmalloc(sizeof(*args), GFP_NOFS);
37229+ if (unlikely(!args)) {
37230+ AuErr1("no memory\n");
37231+ goto out;
37232+ }
37233+
37234+ au_lcnt_inc(&br->br_count);
37235+ args->sb = sb;
37236+ args->br = br;
37237+ args->idx = idx;
37238+ wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
37239+ if (!wkq_err)
37240+ return; /* success */
37241+
37242+ pr_err("wkq %d\n", wkq_err);
37243+ au_lcnt_dec(&br->br_count);
37244+ au_kfree_rcu(args);
37245+
37246+out:
37247+ atomic_dec(&br->br_xino->xi_truncating);
37248+}
37249+
37250+/* ---------------------------------------------------------------------- */
37251+
37252+struct au_xi_calc {
37253+ int idx;
37254+ loff_t pos;
37255+};
37256+
37257+static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37258+ struct au_xi_calc *calc)
37259+{
37260+ loff_t maxent;
37261+
37262+ maxent = au_xi_maxent(sb);
37263+ calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37264+ calc->pos *= sizeof(ino_t);
37265+}
37266+
37267+static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37268+ struct au_xi_calc *calc)
37269+{
37270+ int err;
37271+ struct file *file;
37272+ struct au_xino *xi = br->br_xino;
37273+ struct au_xi_new xinew = {
37274+ .xi = xi
37275+ };
37276+
37277+ SiMustAnyLock(sb);
37278+
37279+ err = 0;
37280+ if (!xi)
37281+ goto out;
37282+
37283+ mutex_lock(&xi->xi_mtx);
37284+ file = au_xino_file(xi, calc->idx);
37285+ if (file)
37286+ goto out_mtx;
37287+
37288+ file = au_xino_file(xi, /*idx*/-1);
37289+ AuDebugOn(!file);
37290+ xinew.idx = calc->idx;
37291+ xinew.base = &file->f_path;
37292+ /* xinew.copy_src = NULL; */
37293+ file = au_xi_new(sb, &xinew);
37294+ if (IS_ERR(file))
37295+ err = PTR_ERR(file);
37296+
37297+out_mtx:
37298+ mutex_unlock(&xi->xi_mtx);
37299+out:
37300+ return err;
37301+}
37302+
37303+struct au_xino_do_new_async_args {
37304+ struct super_block *sb;
37305+ struct au_branch *br;
37306+ struct au_xi_calc calc;
37307+ ino_t ino;
37308+};
37309+
37310+struct au_xi_writing {
37311+ struct hlist_bl_node node;
37312+ ino_t h_ino, ino;
37313+};
37314+
37315+static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37316+ ino_t ino);
37317+
37318+static void au_xino_call_do_new_async(void *args)
37319+{
37320+ struct au_xino_do_new_async_args *a = args;
37321+ struct au_branch *br;
37322+ struct super_block *sb;
37323+ struct au_sbinfo *sbi;
37324+ struct inode *root;
37325+ struct file *file;
37326+ struct au_xi_writing *del, *p;
37327+ struct hlist_bl_head *hbl;
37328+ struct hlist_bl_node *pos;
37329+ int err;
37330+
37331+ br = a->br;
37332+ sb = a->sb;
37333+ sbi = au_sbi(sb);
37334+ si_noflush_read_lock(sb);
37335+ root = d_inode(sb->s_root);
37336+ ii_read_lock_child(root);
37337+ err = au_xino_do_new_async(sb, br, &a->calc);
37338+ if (unlikely(err)) {
37339+ AuIOErr("err %d\n", err);
37340+ goto out;
37341+ }
37342+
37343+ file = au_xino_file(br->br_xino, a->calc.idx);
37344+ AuDebugOn(!file);
37345+ err = au_xino_do_write(file, &a->calc, a->ino);
37346+ if (unlikely(err)) {
37347+ AuIOErr("err %d\n", err);
37348+ goto out;
37349+ }
37350+
37351+ del = NULL;
37352+ hbl = &br->br_xino->xi_writing;
37353+ hlist_bl_lock(hbl);
37354+ au_hbl_for_each(pos, hbl) {
37355+ p = container_of(pos, struct au_xi_writing, node);
37356+ if (p->ino == a->ino) {
37357+ del = p;
37358+ hlist_bl_del(&p->node);
37359+ break;
37360+ }
37361+ }
37362+ hlist_bl_unlock(hbl);
37363+ au_kfree_rcu(del);
37364+
37365+out:
37366+ au_lcnt_dec(&br->br_count);
37367+ ii_read_unlock(root);
37368+ si_read_unlock(sb);
37369+ au_nwt_done(&sbi->si_nowait);
37370+ au_kfree_rcu(a);
37371+}
37372+
37373+/*
37374+ * create a new xino file asynchronously
37375+ */
37376+static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37377+ struct au_xi_calc *calc, ino_t ino)
37378+{
37379+ int err;
37380+ struct au_xino_do_new_async_args *arg;
37381+
37382+ err = -ENOMEM;
37383+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
37384+ if (unlikely(!arg))
37385+ goto out;
37386+
37387+ arg->sb = sb;
37388+ arg->br = br;
37389+ arg->calc = *calc;
37390+ arg->ino = ino;
37391+ au_lcnt_inc(&br->br_count);
37392+ err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37393+ if (unlikely(err)) {
37394+ pr_err("wkq %d\n", err);
37395+ au_lcnt_dec(&br->br_count);
37396+ au_kfree_rcu(arg);
37397+ }
37398+
37399+out:
37400+ return err;
37401+}
37402+
37403+/*
37404+ * read @ino from xinofile for the specified branch{@sb, @bindex}
37405+ * at the position of @h_ino.
37406+ */
37407+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37408+ ino_t *ino)
37409+{
37410+ int err;
37411+ ssize_t sz;
37412+ struct au_xi_calc calc;
37413+ struct au_sbinfo *sbinfo;
37414+ struct file *file;
37415+ struct au_xino *xi;
37416+ struct hlist_bl_head *hbl;
37417+ struct hlist_bl_node *pos;
37418+ struct au_xi_writing *p;
37419+
37420+ *ino = 0;
37421+ if (!au_opt_test(au_mntflags(sb), XINO))
37422+ return 0; /* no xino */
37423+
37424+ err = 0;
37425+ au_xi_calc(sb, h_ino, &calc);
37426+ xi = au_sbr(sb, bindex)->br_xino;
37427+ file = au_xino_file(xi, calc.idx);
37428+ if (!file) {
37429+ hbl = &xi->xi_writing;
37430+ hlist_bl_lock(hbl);
37431+ au_hbl_for_each(pos, hbl) {
37432+ p = container_of(pos, struct au_xi_writing, node);
37433+ if (p->h_ino == h_ino) {
37434+ AuDbg("hi%llu, i%llu, found\n",
37435+ (u64)p->h_ino, (u64)p->ino);
37436+ *ino = p->ino;
37437+ break;
37438+ }
37439+ }
37440+ hlist_bl_unlock(hbl);
37441+ return 0;
37442+ } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37443+ return 0; /* no xino */
37444+
37445+ sbinfo = au_sbi(sb);
37446+ sz = xino_fread(file, ino, sizeof(*ino), &calc.pos);
37447+ if (sz == sizeof(*ino))
37448+ return 0; /* success */
37449+
37450+ err = sz;
37451+ if (unlikely(sz >= 0)) {
37452+ err = -EIO;
37453+ AuIOErr("xino read error (%zd)\n", sz);
37454+ }
37455+ return err;
37456+}
37457+
37458+static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37459+ ino_t ino)
37460+{
37461+ ssize_t sz;
37462+
37463+ sz = xino_fwrite(file, &ino, sizeof(ino), &calc->pos);
37464+ if (sz == sizeof(ino))
37465+ return 0; /* success */
37466+
37467+ AuIOErr("write failed (%zd)\n", sz);
37468+ return -EIO;
37469+}
37470+
37471+/*
37472+ * write @ino to the xinofile for the specified branch{@sb, @bindex}
37473+ * at the position of @h_ino.
37474+ * even if @ino is zero, it is written to the xinofile and means no entry.
37475+ * if the size of the xino file on a specific filesystem exceeds the watermark,
37476+ * try truncating it.
37477+ */
37478+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37479+ ino_t ino)
37480+{
37481+ int err;
37482+ unsigned int mnt_flags;
37483+ struct au_xi_calc calc;
37484+ struct file *file;
37485+ struct au_branch *br;
37486+ struct au_xino *xi;
37487+ struct au_xi_writing *p;
37488+
37489+ SiMustAnyLock(sb);
37490+
37491+ mnt_flags = au_mntflags(sb);
37492+ if (!au_opt_test(mnt_flags, XINO))
37493+ return 0;
37494+
37495+ au_xi_calc(sb, h_ino, &calc);
37496+ br = au_sbr(sb, bindex);
37497+ xi = br->br_xino;
37498+ file = au_xino_file(xi, calc.idx);
37499+ if (!file) {
37500+ /* store the inum pair into the list */
37501+ p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37502+ p->h_ino = h_ino;
37503+ p->ino = ino;
37504+ au_hbl_add(&p->node, &xi->xi_writing);
37505+
37506+ /* create and write a new xino file asynchronously */
37507+ err = au_xino_new_async(sb, br, &calc, ino);
37508+ if (!err)
37509+ return 0; /* success */
37510+ goto out;
37511+ }
37512+
37513+ err = au_xino_do_write(file, &calc, ino);
37514+ if (!err) {
37515+ br = au_sbr(sb, bindex);
37516+ if (au_opt_test(mnt_flags, TRUNC_XINO)
37517+ && au_test_fs_trunc_xino(au_br_sb(br)))
37518+ xino_try_trunc(sb, br);
37519+ return 0; /* success */
37520+ }
37521+
37522+out:
37523+ AuIOErr("write failed (%d)\n", err);
37524+ return -EIO;
37525+}
37526+
37527+static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37528+ loff_t *pos);
37529+
37530+/* todo: unnecessary to support mmap_sem since kernel-space? */
37531+ssize_t xino_fread(struct file *file, void *kbuf, size_t size, loff_t *pos)
37532+{
37533+ ssize_t err;
37534+ int i;
37535+ const int prevent_endless = 10;
37536+
37537+ i = 0;
37538+ do {
37539+ err = vfsub_read_k(file, kbuf, size, pos);
37540+ if (err == -EINTR
37541+ && !au_wkq_test()
37542+ && fatal_signal_pending(current)) {
37543+ err = xino_fread_wkq(file, kbuf, size, pos);
37544+ BUG_ON(err == -EINTR);
37545+ }
37546+ } while (i++ < prevent_endless
37547+ && (err == -EAGAIN || err == -EINTR));
37548+
37549+#if 0 /* reserved for future use */
37550+ if (err > 0)
37551+ fsnotify_access(file->f_path.dentry);
37552+#endif
37553+
37554+ return err;
37555+}
37556+
37557+struct xino_fread_args {
37558+ ssize_t *errp;
37559+ struct file *file;
37560+ void *buf;
37561+ size_t size;
37562+ loff_t *pos;
37563+};
37564+
37565+static void call_xino_fread(void *args)
37566+{
37567+ struct xino_fread_args *a = args;
37568+ *a->errp = xino_fread(a->file, a->buf, a->size, a->pos);
37569+}
37570+
37571+static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37572+ loff_t *pos)
37573+{
37574+ ssize_t err;
37575+ int wkq_err;
37576+ struct xino_fread_args args = {
37577+ .errp = &err,
37578+ .file = file,
37579+ .buf = buf,
37580+ .size = size,
37581+ .pos = pos
37582+ };
37583+
37584+ wkq_err = au_wkq_wait(call_xino_fread, &args);
37585+ if (unlikely(wkq_err))
37586+ err = wkq_err;
37587+
37588+ return err;
37589+}
37590+
37591+static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
37592+ loff_t *pos);
37593+
37594+static ssize_t do_xino_fwrite(struct file *file, void *kbuf, size_t size,
37595+ loff_t *pos)
37596+{
37597+ ssize_t err;
37598+ int i;
37599+ const int prevent_endless = 10;
37600+
37601+ i = 0;
37602+ do {
37603+ err = vfsub_write_k(file, kbuf, size, pos);
37604+ if (err == -EINTR
37605+ && !au_wkq_test()
37606+ && fatal_signal_pending(current)) {
37607+ err = xino_fwrite_wkq(file, kbuf, size, pos);
37608+ BUG_ON(err == -EINTR);
37609+ }
37610+ } while (i++ < prevent_endless
37611+ && (err == -EAGAIN || err == -EINTR));
37612+
37613+#if 0 /* reserved for future use */
37614+ if (err > 0)
37615+ fsnotify_modify(file->f_path.dentry);
37616+#endif
37617+
37618+ return err;
37619+}
37620+
37621+struct do_xino_fwrite_args {
37622+ ssize_t *errp;
37623+ struct file *file;
37624+ void *buf;
37625+ size_t size;
37626+ loff_t *pos;
37627+};
37628+
37629+static void call_do_xino_fwrite(void *args)
37630+{
37631+ struct do_xino_fwrite_args *a = args;
37632+ *a->errp = do_xino_fwrite(a->file, a->buf, a->size, a->pos);
37633+}
37634+
37635+static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
37636+ loff_t *pos)
37637+{
37638+ ssize_t err;
37639+ int wkq_err;
37640+ struct do_xino_fwrite_args args = {
37641+ .errp = &err,
37642+ .file = file,
37643+ .buf = buf,
37644+ .size = size,
37645+ .pos = pos
37646+ };
37647+
37648+ /*
37649+ * it breaks RLIMIT_FSIZE and normal user's limit,
37650+ * users should care about quota and real 'filesystem full.'
37651+ */
37652+ wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
37653+ if (unlikely(wkq_err))
37654+ err = wkq_err;
37655+
37656+ return err;
37657+}
37658+
37659+ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos)
37660+{
37661+ ssize_t err;
37662+
37663+ if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
37664+ lockdep_off();
37665+ err = do_xino_fwrite(file, buf, size, pos);
37666+ lockdep_on();
37667+ } else {
37668+ lockdep_off();
37669+ err = xino_fwrite_wkq(file, buf, size, pos);
37670+ lockdep_on();
37671+ }
37672+
37673+ return err;
37674+}
37675+
37676+/* ---------------------------------------------------------------------- */
37677+
37678+/*
37679+ * inode number bitmap
37680+ */
37681+static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
37682+static ino_t xib_calc_ino(unsigned long pindex, int bit)
37683+{
37684+ ino_t ino;
37685+
37686+ AuDebugOn(bit < 0 || page_bits <= bit);
37687+ ino = AUFS_FIRST_INO + pindex * page_bits + bit;
37688+ return ino;
37689+}
37690+
37691+static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
37692+{
37693+ AuDebugOn(ino < AUFS_FIRST_INO);
37694+ ino -= AUFS_FIRST_INO;
37695+ *pindex = ino / page_bits;
37696+ *bit = ino % page_bits;
37697+}
37698+
37699+static int xib_pindex(struct super_block *sb, unsigned long pindex)
37700+{
37701+ int err;
37702+ loff_t pos;
37703+ ssize_t sz;
37704+ struct au_sbinfo *sbinfo;
37705+ struct file *xib;
37706+ unsigned long *p;
37707+
37708+ sbinfo = au_sbi(sb);
37709+ MtxMustLock(&sbinfo->si_xib_mtx);
37710+ AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
37711+ || !au_opt_test(sbinfo->si_mntflags, XINO));
37712+
37713+ if (pindex == sbinfo->si_xib_last_pindex)
37714+ return 0;
37715+
37716+ xib = sbinfo->si_xib;
37717+ p = sbinfo->si_xib_buf;
37718+ pos = sbinfo->si_xib_last_pindex;
37719+ pos *= PAGE_SIZE;
37720+ sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
37721+ if (unlikely(sz != PAGE_SIZE))
37722+ goto out;
37723+
37724+ pos = pindex;
37725+ pos *= PAGE_SIZE;
37726+ if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
37727+ sz = xino_fread(xib, p, PAGE_SIZE, &pos);
37728+ else {
37729+ memset(p, 0, PAGE_SIZE);
37730+ sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
37731+ }
37732+ if (sz == PAGE_SIZE) {
37733+ sbinfo->si_xib_last_pindex = pindex;
37734+ return 0; /* success */
37735+ }
37736+
37737+out:
37738+ AuIOErr1("write failed (%zd)\n", sz);
37739+ err = sz;
37740+ if (sz >= 0)
37741+ err = -EIO;
37742+ return err;
37743+}
37744+
37745+static void au_xib_clear_bit(struct inode *inode)
37746+{
37747+ int err, bit;
37748+ unsigned long pindex;
37749+ struct super_block *sb;
37750+ struct au_sbinfo *sbinfo;
37751+
37752+ AuDebugOn(inode->i_nlink);
37753+
37754+ sb = inode->i_sb;
37755+ xib_calc_bit(inode->i_ino, &pindex, &bit);
37756+ AuDebugOn(page_bits <= bit);
37757+ sbinfo = au_sbi(sb);
37758+ mutex_lock(&sbinfo->si_xib_mtx);
37759+ err = xib_pindex(sb, pindex);
37760+ if (!err) {
37761+ clear_bit(bit, sbinfo->si_xib_buf);
37762+ sbinfo->si_xib_next_bit = bit;
37763+ }
37764+ mutex_unlock(&sbinfo->si_xib_mtx);
37765+}
37766+
37767+/* ---------------------------------------------------------------------- */
37768+
37769+/*
37770+ * truncate a xino bitmap file
37771+ */
37772+
37773+/* todo: slow */
37774+static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
37775+{
37776+ int err, bit;
37777+ ssize_t sz;
37778+ unsigned long pindex;
37779+ loff_t pos, pend;
37780+ struct au_sbinfo *sbinfo;
37781+ ino_t *ino;
37782+ unsigned long *p;
37783+
37784+ err = 0;
37785+ sbinfo = au_sbi(sb);
37786+ MtxMustLock(&sbinfo->si_xib_mtx);
37787+ p = sbinfo->si_xib_buf;
37788+ pend = vfsub_f_size_read(file);
37789+ pos = 0;
37790+ while (pos < pend) {
37791+ sz = xino_fread(file, page, PAGE_SIZE, &pos);
37792+ err = sz;
37793+ if (unlikely(sz <= 0))
37794+ goto out;
37795+
37796+ err = 0;
37797+ for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
37798+ if (unlikely(*ino < AUFS_FIRST_INO))
37799+ continue;
37800+
37801+ xib_calc_bit(*ino, &pindex, &bit);
37802+ AuDebugOn(page_bits <= bit);
37803+ err = xib_pindex(sb, pindex);
37804+ if (!err)
37805+ set_bit(bit, p);
37806+ else
37807+ goto out;
37808+ }
37809+ }
37810+
37811+out:
37812+ return err;
37813+}
37814+
37815+static int xib_restore(struct super_block *sb)
37816+{
37817+ int err, i;
37818+ unsigned int nfile;
37819+ aufs_bindex_t bindex, bbot;
37820+ void *page;
37821+ struct au_branch *br;
37822+ struct au_xino *xi;
37823+ struct file *file;
37824+
37825+ err = -ENOMEM;
37826+ page = (void *)__get_free_page(GFP_NOFS);
37827+ if (unlikely(!page))
37828+ goto out;
37829+
37830+ err = 0;
37831+ bbot = au_sbbot(sb);
37832+ for (bindex = 0; !err && bindex <= bbot; bindex++)
37833+ if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
37834+ br = au_sbr(sb, bindex);
37835+ xi = br->br_xino;
37836+ nfile = xi->xi_nfile;
37837+ for (i = 0; i < nfile; i++) {
37838+ file = au_xino_file(xi, i);
37839+ if (file)
37840+ err = do_xib_restore(sb, file, page);
37841+ }
37842+ } else
37843+ AuDbg("skip shared b%d\n", bindex);
37844+ free_page((unsigned long)page);
37845+
37846+out:
37847+ return err;
37848+}
37849+
37850+int au_xib_trunc(struct super_block *sb)
37851+{
37852+ int err;
37853+ ssize_t sz;
37854+ loff_t pos;
37855+ struct au_sbinfo *sbinfo;
37856+ unsigned long *p;
37857+ struct file *file;
37858+
37859+ SiMustWriteLock(sb);
37860+
37861+ err = 0;
37862+ sbinfo = au_sbi(sb);
37863+ if (!au_opt_test(sbinfo->si_mntflags, XINO))
37864+ goto out;
37865+
37866+ file = sbinfo->si_xib;
37867+ if (vfsub_f_size_read(file) <= PAGE_SIZE)
37868+ goto out;
37869+
37870+ file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
37871+ err = PTR_ERR(file);
37872+ if (IS_ERR(file))
37873+ goto out;
37874+ fput(sbinfo->si_xib);
37875+ sbinfo->si_xib = file;
37876+
37877+ p = sbinfo->si_xib_buf;
37878+ memset(p, 0, PAGE_SIZE);
37879+ pos = 0;
37880+ sz = xino_fwrite(sbinfo->si_xib, p, PAGE_SIZE, &pos);
37881+ if (unlikely(sz != PAGE_SIZE)) {
37882+ err = sz;
37883+ AuIOErr("err %d\n", err);
37884+ if (sz >= 0)
37885+ err = -EIO;
37886+ goto out;
37887+ }
37888+
37889+ mutex_lock(&sbinfo->si_xib_mtx);
37890+ /* mnt_want_write() is unnecessary here */
37891+ err = xib_restore(sb);
37892+ mutex_unlock(&sbinfo->si_xib_mtx);
37893+
37894+out:
37895+ return err;
37896+}
37897+
37898+/* ---------------------------------------------------------------------- */
37899+
37900+struct au_xino *au_xino_alloc(unsigned int nfile)
37901+{
37902+ struct au_xino *xi;
37903+
37904+ xi = kzalloc(sizeof(*xi), GFP_NOFS);
37905+ if (unlikely(!xi))
37906+ goto out;
37907+ xi->xi_nfile = nfile;
37908+ xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
37909+ if (unlikely(!xi->xi_file))
37910+ goto out_free;
37911+
37912+ xi->xi_nondir.total = 8; /* initial size */
37913+ xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
37914+ GFP_NOFS);
37915+ if (unlikely(!xi->xi_nondir.array))
37916+ goto out_file;
37917+
37918+ spin_lock_init(&xi->xi_nondir.spin);
37919+ init_waitqueue_head(&xi->xi_nondir.wqh);
37920+ mutex_init(&xi->xi_mtx);
37921+ INIT_HLIST_BL_HEAD(&xi->xi_writing);
37922+ atomic_set(&xi->xi_truncating, 0);
37923+ kref_init(&xi->xi_kref);
37924+ goto out; /* success */
37925+
37926+out_file:
37927+ au_kfree_try_rcu(xi->xi_file);
37928+out_free:
37929+ au_kfree_rcu(xi);
37930+ xi = NULL;
37931+out:
37932+ return xi;
37933+}
37934+
37935+static int au_xino_init(struct au_branch *br, int idx, struct file *file)
37936+{
37937+ int err;
37938+ struct au_xino *xi;
37939+
37940+ err = 0;
37941+ xi = au_xino_alloc(idx + 1);
37942+ if (unlikely(!xi)) {
37943+ err = -ENOMEM;
37944+ goto out;
37945+ }
37946+
37947+ if (file)
37948+ get_file(file);
37949+ xi->xi_file[idx] = file;
37950+ AuDebugOn(br->br_xino);
37951+ br->br_xino = xi;
37952+
37953+out:
37954+ return err;
37955+}
37956+
37957+static void au_xino_release(struct kref *kref)
37958+{
37959+ struct au_xino *xi;
37960+ int i;
37961+ unsigned long ul;
37962+ struct hlist_bl_head *hbl;
37963+ struct hlist_bl_node *pos, *n;
37964+ struct au_xi_writing *p;
37965+
37966+ xi = container_of(kref, struct au_xino, xi_kref);
37967+ for (i = 0; i < xi->xi_nfile; i++)
37968+ if (xi->xi_file[i])
37969+ fput(xi->xi_file[i]);
37970+ for (i = xi->xi_nondir.total - 1; i >= 0; i--)
37971+ AuDebugOn(xi->xi_nondir.array[i]);
37972+ mutex_destroy(&xi->xi_mtx);
37973+ hbl = &xi->xi_writing;
37974+ ul = au_hbl_count(hbl);
37975+ if (unlikely(ul)) {
37976+ pr_warn("xi_writing %lu\n", ul);
37977+ hlist_bl_lock(hbl);
37978+ hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
37979+ hlist_bl_del(&p->node);
37980+ /* kmemleak reported au_kfree_rcu() doesn't free it */
37981+ kfree(p);
37982+ }
37983+ hlist_bl_unlock(hbl);
37984+ }
37985+ au_kfree_try_rcu(xi->xi_file);
37986+ au_kfree_try_rcu(xi->xi_nondir.array);
37987+ au_kfree_rcu(xi);
37988+}
37989+
37990+int au_xino_put(struct au_branch *br)
37991+{
37992+ int ret;
37993+ struct au_xino *xi;
37994+
37995+ ret = 0;
37996+ xi = br->br_xino;
37997+ if (xi) {
37998+ br->br_xino = NULL;
37999+ ret = kref_put(&xi->xi_kref, au_xino_release);
38000+ }
38001+
38002+ return ret;
38003+}
38004+
38005+/* ---------------------------------------------------------------------- */
38006+
38007+/*
38008+ * xino mount option handlers
38009+ */
38010+
38011+/* xino bitmap */
38012+static void xino_clear_xib(struct super_block *sb)
38013+{
38014+ struct au_sbinfo *sbinfo;
38015+
38016+ SiMustWriteLock(sb);
38017+
38018+ sbinfo = au_sbi(sb);
38019+ if (sbinfo->si_xib)
38020+ fput(sbinfo->si_xib);
38021+ sbinfo->si_xib = NULL;
38022+ if (sbinfo->si_xib_buf)
38023+ free_page((unsigned long)sbinfo->si_xib_buf);
38024+ sbinfo->si_xib_buf = NULL;
38025+}
38026+
38027+static int au_xino_set_xib(struct super_block *sb, struct path *path)
38028+{
38029+ int err;
38030+ loff_t pos;
38031+ struct au_sbinfo *sbinfo;
38032+ struct file *file;
38033+ struct super_block *xi_sb;
38034+
38035+ SiMustWriteLock(sb);
38036+
38037+ sbinfo = au_sbi(sb);
38038+ file = au_xino_create2(sb, path, sbinfo->si_xib);
38039+ err = PTR_ERR(file);
38040+ if (IS_ERR(file))
38041+ goto out;
38042+ if (sbinfo->si_xib)
38043+ fput(sbinfo->si_xib);
38044+ sbinfo->si_xib = file;
38045+ xi_sb = file_inode(file)->i_sb;
38046+ sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38047+ if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38048+ err = -EIO;
38049+ pr_err("s_maxbytes(%llu) on %s is too small\n",
38050+ (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38051+ goto out_unset;
38052+ }
38053+ sbinfo->si_ximaxent /= sizeof(ino_t);
38054+
38055+ err = -ENOMEM;
38056+ if (!sbinfo->si_xib_buf)
38057+ sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38058+ if (unlikely(!sbinfo->si_xib_buf))
38059+ goto out_unset;
38060+
38061+ sbinfo->si_xib_last_pindex = 0;
38062+ sbinfo->si_xib_next_bit = 0;
38063+ if (vfsub_f_size_read(file) < PAGE_SIZE) {
38064+ pos = 0;
38065+ err = xino_fwrite(file, sbinfo->si_xib_buf, PAGE_SIZE, &pos);
38066+ if (unlikely(err != PAGE_SIZE))
38067+ goto out_free;
38068+ }
38069+ err = 0;
38070+ goto out; /* success */
38071+
38072+out_free:
38073+ if (sbinfo->si_xib_buf)
38074+ free_page((unsigned long)sbinfo->si_xib_buf);
38075+ sbinfo->si_xib_buf = NULL;
38076+ if (err >= 0)
38077+ err = -EIO;
38078+out_unset:
38079+ fput(sbinfo->si_xib);
38080+ sbinfo->si_xib = NULL;
38081+out:
38082+ AuTraceErr(err);
38083+ return err;
38084+}
38085+
38086+/* xino for each branch */
38087+static void xino_clear_br(struct super_block *sb)
38088+{
38089+ aufs_bindex_t bindex, bbot;
38090+ struct au_branch *br;
38091+
38092+ bbot = au_sbbot(sb);
38093+ for (bindex = 0; bindex <= bbot; bindex++) {
38094+ br = au_sbr(sb, bindex);
38095+ AuDebugOn(!br);
38096+ au_xino_put(br);
38097+ }
38098+}
38099+
38100+static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38101+ aufs_bindex_t bshared)
38102+{
38103+ struct au_branch *brshared;
38104+
38105+ brshared = au_sbr(sb, bshared);
38106+ AuDebugOn(!brshared->br_xino);
38107+ AuDebugOn(!brshared->br_xino->xi_file);
38108+ if (br->br_xino != brshared->br_xino) {
38109+ au_xino_get(brshared);
38110+ au_xino_put(br);
38111+ br->br_xino = brshared->br_xino;
38112+ }
38113+}
38114+
38115+struct au_xino_do_set_br {
38116+ struct au_branch *br;
38117+ ino_t h_ino;
38118+ aufs_bindex_t bshared;
38119+};
38120+
38121+static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38122+ struct au_xino_do_set_br *args)
38123+{
38124+ int err;
38125+ struct au_xi_calc calc;
38126+ struct file *file;
38127+ struct au_branch *br;
38128+ struct au_xi_new xinew = {
38129+ .base = path
38130+ };
38131+
38132+ br = args->br;
38133+ xinew.xi = br->br_xino;
38134+ au_xi_calc(sb, args->h_ino, &calc);
38135+ xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38136+ if (args->bshared >= 0)
38137+ /* shared xino */
38138+ au_xino_set_br_shared(sb, br, args->bshared);
38139+ else if (!xinew.xi) {
38140+ /* new xino */
38141+ err = au_xino_init(br, calc.idx, xinew.copy_src);
38142+ if (unlikely(err))
38143+ goto out;
38144+ }
38145+
38146+ /* force re-creating */
38147+ xinew.xi = br->br_xino;
38148+ xinew.idx = calc.idx;
38149+ mutex_lock(&xinew.xi->xi_mtx);
38150+ file = au_xi_new(sb, &xinew);
38151+ mutex_unlock(&xinew.xi->xi_mtx);
38152+ err = PTR_ERR(file);
38153+ if (IS_ERR(file))
38154+ goto out;
38155+ AuDebugOn(!file);
38156+
38157+ err = au_xino_do_write(file, &calc, AUFS_ROOT_INO);
38158+ if (unlikely(err))
38159+ au_xino_put(br);
38160+
38161+out:
38162+ AuTraceErr(err);
38163+ return err;
38164+}
38165+
38166+static int au_xino_set_br(struct super_block *sb, struct path *path)
38167+{
38168+ int err;
38169+ aufs_bindex_t bindex, bbot;
38170+ struct au_xino_do_set_br args;
38171+ struct inode *inode;
38172+
38173+ SiMustWriteLock(sb);
38174+
38175+ bbot = au_sbbot(sb);
38176+ inode = d_inode(sb->s_root);
38177+ for (bindex = 0; bindex <= bbot; bindex++) {
38178+ args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38179+ args.br = au_sbr(sb, bindex);
38180+ args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38181+ err = au_xino_do_set_br(sb, path, &args);
38182+ if (unlikely(err))
38183+ break;
38184+ }
38185+
38186+ AuTraceErr(err);
38187+ return err;
38188+}
38189+
38190+void au_xino_clr(struct super_block *sb)
38191+{
38192+ struct au_sbinfo *sbinfo;
38193+
38194+ au_xigen_clr(sb);
38195+ xino_clear_xib(sb);
38196+ xino_clear_br(sb);
38197+ dbgaufs_brs_del(sb, 0);
38198+ sbinfo = au_sbi(sb);
38199+ /* lvalue, do not call au_mntflags() */
38200+ au_opt_clr(sbinfo->si_mntflags, XINO);
38201+}
38202+
38203+int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
38204+{
38205+ int err, skip;
38206+ struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
38207+ struct qstr *dname, *cur_name;
38208+ struct file *cur_xino;
38209+ struct au_sbinfo *sbinfo;
38210+ struct path *path, *cur_path;
38211+
38212+ SiMustWriteLock(sb);
38213+
38214+ err = 0;
38215+ sbinfo = au_sbi(sb);
38216+ path = &xiopt->file->f_path;
38217+ dentry = path->dentry;
38218+ parent = dget_parent(dentry);
38219+ if (remount) {
38220+ skip = 0;
38221+ cur_xino = sbinfo->si_xib;
38222+ if (cur_xino) {
38223+ cur_path = &cur_xino->f_path;
38224+ cur_dentry = cur_path->dentry;
38225+ cur_parent = dget_parent(cur_dentry);
38226+ cur_name = &cur_dentry->d_name;
38227+ dname = &dentry->d_name;
38228+ skip = (cur_parent == parent
38229+ && au_qstreq(dname, cur_name));
38230+ dput(cur_parent);
38231+ }
38232+ if (skip)
38233+ goto out;
38234+ }
38235+
38236+ au_opt_set(sbinfo->si_mntflags, XINO);
38237+ err = au_xino_set_xib(sb, path);
38238+ /* si_x{read,write} are set */
38239+ if (!err)
38240+ err = au_xigen_set(sb, path);
38241+ if (!err)
38242+ err = au_xino_set_br(sb, path);
38243+ if (!err) {
38244+ dbgaufs_brs_add(sb, 0, /*topdown*/1);
38245+ goto out; /* success */
38246+ }
38247+
38248+ /* reset all */
38249+ AuIOErr("failed setting xino(%d).\n", err);
38250+ au_xino_clr(sb);
38251+
38252+out:
38253+ dput(parent);
38254+ return err;
38255+}
38256+
38257+/*
38258+ * create a xinofile at the default place/path.
38259+ */
38260+struct file *au_xino_def(struct super_block *sb)
38261+{
38262+ struct file *file;
38263+ char *page, *p;
38264+ struct au_branch *br;
38265+ struct super_block *h_sb;
38266+ struct path path;
38267+ aufs_bindex_t bbot, bindex, bwr;
38268+
38269+ br = NULL;
38270+ bbot = au_sbbot(sb);
38271+ bwr = -1;
38272+ for (bindex = 0; bindex <= bbot; bindex++) {
38273+ br = au_sbr(sb, bindex);
38274+ if (au_br_writable(br->br_perm)
38275+ && !au_test_fs_bad_xino(au_br_sb(br))) {
38276+ bwr = bindex;
38277+ break;
38278+ }
38279+ }
38280+
38281+ if (bwr >= 0) {
38282+ file = ERR_PTR(-ENOMEM);
38283+ page = (void *)__get_free_page(GFP_NOFS);
38284+ if (unlikely(!page))
38285+ goto out;
38286+ path.mnt = au_br_mnt(br);
38287+ path.dentry = au_h_dptr(sb->s_root, bwr);
38288+ p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38289+ file = (void *)p;
38290+ if (!IS_ERR(p)) {
38291+ strcat(p, "/" AUFS_XINO_FNAME);
38292+ AuDbg("%s\n", p);
38293+ file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
38294+ }
38295+ free_page((unsigned long)page);
38296+ } else {
38297+ file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38298+ /*wbrtop*/0);
38299+ if (IS_ERR(file))
38300+ goto out;
38301+ h_sb = file->f_path.dentry->d_sb;
38302+ if (unlikely(au_test_fs_bad_xino(h_sb))) {
38303+ pr_err("xino doesn't support %s(%s)\n",
38304+ AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38305+ fput(file);
38306+ file = ERR_PTR(-EINVAL);
38307+ }
38308+ }
38309+
38310+out:
38311+ return file;
38312+}
38313+
38314+/* ---------------------------------------------------------------------- */
38315+
38316+/*
38317+ * initialize the xinofile for the specified branch @br
38318+ * at the place/path where @base_file indicates.
38319+ * test whether another branch is on the same filesystem or not,
38320+ * if found then share the xinofile with another branch.
38321+ */
38322+int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38323+ struct path *base)
38324+{
38325+ int err;
38326+ struct au_xino_do_set_br args = {
38327+ .h_ino = h_ino,
38328+ .br = br
38329+ };
38330+
38331+ args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38332+ au_br_sb(br));
38333+ err = au_xino_do_set_br(sb, base, &args);
38334+ if (unlikely(err))
38335+ au_xino_put(br);
38336+
38337+ return err;
38338+}
38339+
38340+/* ---------------------------------------------------------------------- */
38341+
38342+/*
38343+ * get an unused inode number from bitmap
38344+ */
38345+ino_t au_xino_new_ino(struct super_block *sb)
38346+{
38347+ ino_t ino;
38348+ unsigned long *p, pindex, ul, pend;
38349+ struct au_sbinfo *sbinfo;
38350+ struct file *file;
38351+ int free_bit, err;
38352+
38353+ if (!au_opt_test(au_mntflags(sb), XINO))
38354+ return iunique(sb, AUFS_FIRST_INO);
38355+
38356+ sbinfo = au_sbi(sb);
38357+ mutex_lock(&sbinfo->si_xib_mtx);
38358+ p = sbinfo->si_xib_buf;
38359+ free_bit = sbinfo->si_xib_next_bit;
38360+ if (free_bit < page_bits && !test_bit(free_bit, p))
38361+ goto out; /* success */
38362+ free_bit = find_first_zero_bit(p, page_bits);
38363+ if (free_bit < page_bits)
38364+ goto out; /* success */
38365+
38366+ pindex = sbinfo->si_xib_last_pindex;
38367+ for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38368+ err = xib_pindex(sb, ul);
38369+ if (unlikely(err))
38370+ goto out_err;
38371+ free_bit = find_first_zero_bit(p, page_bits);
38372+ if (free_bit < page_bits)
38373+ goto out; /* success */
38374+ }
38375+
38376+ file = sbinfo->si_xib;
38377+ pend = vfsub_f_size_read(file) / PAGE_SIZE;
38378+ for (ul = pindex + 1; ul <= pend; ul++) {
38379+ err = xib_pindex(sb, ul);
38380+ if (unlikely(err))
38381+ goto out_err;
38382+ free_bit = find_first_zero_bit(p, page_bits);
38383+ if (free_bit < page_bits)
38384+ goto out; /* success */
38385+ }
38386+ BUG();
38387+
38388+out:
38389+ set_bit(free_bit, p);
38390+ sbinfo->si_xib_next_bit = free_bit + 1;
38391+ pindex = sbinfo->si_xib_last_pindex;
38392+ mutex_unlock(&sbinfo->si_xib_mtx);
38393+ ino = xib_calc_ino(pindex, free_bit);
38394+ AuDbg("i%lu\n", (unsigned long)ino);
38395+ return ino;
38396+out_err:
38397+ mutex_unlock(&sbinfo->si_xib_mtx);
38398+ AuDbg("i0\n");
38399+ return 0;
38400+}
38401+
38402+/* for s_op->delete_inode() */
38403+void au_xino_delete_inode(struct inode *inode, const int unlinked)
38404+{
38405+ int err;
38406+ unsigned int mnt_flags;
38407+ aufs_bindex_t bindex, bbot, bi;
38408+ unsigned char try_trunc;
38409+ struct au_iinfo *iinfo;
38410+ struct super_block *sb;
38411+ struct au_hinode *hi;
38412+ struct inode *h_inode;
38413+ struct au_branch *br;
38414+ struct au_xi_calc calc;
38415+ struct file *file;
38416+
38417+ AuDebugOn(au_is_bad_inode(inode));
38418+
38419+ sb = inode->i_sb;
38420+ mnt_flags = au_mntflags(sb);
38421+ if (!au_opt_test(mnt_flags, XINO)
38422+ || inode->i_ino == AUFS_ROOT_INO)
38423+ return;
38424+
38425+ if (unlinked) {
38426+ au_xigen_inc(inode);
38427+ au_xib_clear_bit(inode);
38428+ }
38429+
38430+ iinfo = au_ii(inode);
38431+ bindex = iinfo->ii_btop;
38432+ if (bindex < 0)
38433+ return;
38434+
38435+ try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38436+ hi = au_hinode(iinfo, bindex);
38437+ bbot = iinfo->ii_bbot;
38438+ for (; bindex <= bbot; bindex++, hi++) {
38439+ h_inode = hi->hi_inode;
38440+ if (!h_inode
38441+ || (!unlinked && h_inode->i_nlink))
38442+ continue;
38443+
38444+ /* inode may not be revalidated */
38445+ bi = au_br_index(sb, hi->hi_id);
38446+ if (bi < 0)
38447+ continue;
38448+
38449+ br = au_sbr(sb, bi);
38450+ au_xi_calc(sb, h_inode->i_ino, &calc);
38451+ file = au_xino_file(br->br_xino, calc.idx);
38452+ if (IS_ERR_OR_NULL(file))
38453+ continue;
38454+
38455+ err = au_xino_do_write(file, &calc, /*ino*/0);
38456+ if (!err && try_trunc
38457+ && au_test_fs_trunc_xino(au_br_sb(br)))
38458+ xino_try_trunc(sb, br);
38459+ }
38460+}
38461+
38462+/* ---------------------------------------------------------------------- */
38463+
38464+static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
38465+{
38466+ int found, total, i;
38467+
38468+ found = -1;
38469+ total = xi->xi_nondir.total;
38470+ for (i = 0; i < total; i++) {
38471+ if (xi->xi_nondir.array[i] != h_ino)
38472+ continue;
38473+ found = i;
38474+ break;
38475+ }
38476+
38477+ return found;
38478+}
38479+
38480+static int au_xinondir_expand(struct au_xino *xi)
38481+{
38482+ int err, sz;
38483+ ino_t *p;
38484+
38485+ BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38486+
38487+ err = -ENOMEM;
38488+ sz = xi->xi_nondir.total * sizeof(ino_t);
38489+ if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38490+ goto out;
38491+ p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
38492+ /*may_shrink*/0);
38493+ if (p) {
38494+ xi->xi_nondir.array = p;
38495+ xi->xi_nondir.total <<= 1;
38496+ AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
38497+ err = 0;
38498+ }
38499+
38500+out:
38501+ return err;
38502+}
38503+
38504+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38505+ ino_t h_ino, int idx)
38506+{
38507+ struct au_xino *xi;
38508+
38509+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38510+ xi = au_sbr(sb, bindex)->br_xino;
38511+ AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38512+
38513+ spin_lock(&xi->xi_nondir.spin);
38514+ AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38515+ xi->xi_nondir.array[idx] = 0;
38516+ spin_unlock(&xi->xi_nondir.spin);
38517+ wake_up_all(&xi->xi_nondir.wqh);
38518+}
38519+
38520+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38521+ int *idx)
38522+{
38523+ int err, found, empty;
38524+ struct au_xino *xi;
38525+
38526+ err = 0;
38527+ *idx = -1;
38528+ if (!au_opt_test(au_mntflags(sb), XINO))
38529+ goto out; /* no xino */
38530+
38531+ xi = au_sbr(sb, bindex)->br_xino;
38532+
38533+again:
38534+ spin_lock(&xi->xi_nondir.spin);
38535+ found = au_xinondir_find(xi, h_ino);
38536+ if (found == -1) {
38537+ empty = au_xinondir_find(xi, /*h_ino*/0);
38538+ if (empty == -1) {
38539+ empty = xi->xi_nondir.total;
38540+ err = au_xinondir_expand(xi);
38541+ if (unlikely(err))
38542+ goto out_unlock;
38543+ }
38544+ xi->xi_nondir.array[empty] = h_ino;
38545+ *idx = empty;
38546+ } else {
38547+ spin_unlock(&xi->xi_nondir.spin);
38548+ wait_event(xi->xi_nondir.wqh,
38549+ xi->xi_nondir.array[found] != h_ino);
38550+ goto again;
38551+ }
38552+
38553+out_unlock:
38554+ spin_unlock(&xi->xi_nondir.spin);
38555+out:
38556+ return err;
38557+}
38558+
38559+/* ---------------------------------------------------------------------- */
38560+
38561+int au_xino_path(struct seq_file *seq, struct file *file)
38562+{
38563+ int err;
38564+
38565+ err = au_seq_path(seq, &file->f_path);
38566+ if (unlikely(err))
38567+ goto out;
38568+
38569+#define Deleted "\\040(deleted)"
38570+ seq->count -= sizeof(Deleted) - 1;
38571+ AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38572+ sizeof(Deleted) - 1));
38573+#undef Deleted
38574+
38575+out:
38576+ return err;
38577+}
38578diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
38579--- /usr/share/empty/include/uapi/linux/aufs_type.h 1970-01-01 01:00:00.000000000 +0100
38580+++ linux/include/uapi/linux/aufs_type.h 2021-02-22 22:51:59.061411920 +0100
38581@@ -0,0 +1,452 @@
38582+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
38583+/*
38584+ * Copyright (C) 2005-2020 Junjiro R. Okajima
38585+ *
38586+ * This program, aufs is free software; you can redistribute it and/or modify
38587+ * it under the terms of the GNU General Public License as published by
38588+ * the Free Software Foundation; either version 2 of the License, or
38589+ * (at your option) any later version.
38590+ *
38591+ * This program is distributed in the hope that it will be useful,
38592+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
38593+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38594+ * GNU General Public License for more details.
38595+ *
38596+ * You should have received a copy of the GNU General Public License
38597+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
38598+ */
38599+
38600+#ifndef __AUFS_TYPE_H__
38601+#define __AUFS_TYPE_H__
38602+
38603+#define AUFS_NAME "aufs"
38604+
38605+#ifdef __KERNEL__
38606+/*
38607+ * define it before including all other headers.
38608+ * sched.h may use pr_* macros before defining "current", so define the
38609+ * no-current version first, and re-define later.
38610+ */
38611+#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
38612+#include <linux/sched.h>
38613+#undef pr_fmt
38614+#define pr_fmt(fmt) \
38615+ AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
38616+ (int)sizeof(current->comm), current->comm, current->pid
38617+#include <linux/limits.h>
38618+#else
38619+#include <stdint.h>
38620+#include <sys/types.h>
38621+#include <limits.h>
38622+#endif /* __KERNEL__ */
38623+
38624+#define AUFS_VERSION "5.10-20210215"
38625+
38626+/* todo? move this to linux-2.6.19/include/magic.h */
38627+#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
38628+
38629+/* ---------------------------------------------------------------------- */
38630+
38631+#ifdef __KERNEL__
38632+#ifdef CONFIG_AUFS_BRANCH_MAX_127
38633+typedef int8_t aufs_bindex_t;
38634+#define AUFS_BRANCH_MAX 127
38635+#else
38636+typedef int16_t aufs_bindex_t;
38637+#ifdef CONFIG_AUFS_BRANCH_MAX_511
38638+#define AUFS_BRANCH_MAX 511
38639+#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
38640+#define AUFS_BRANCH_MAX 1023
38641+#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
38642+#define AUFS_BRANCH_MAX 32767
38643+#endif
38644+#endif
38645+
38646+#ifndef AUFS_BRANCH_MAX
38647+#error unknown CONFIG_AUFS_BRANCH_MAX value
38648+#endif
38649+#endif /* __KERNEL__ */
38650+
38651+/* ---------------------------------------------------------------------- */
38652+
38653+#define AUFS_FSTYPE AUFS_NAME
38654+
38655+#define AUFS_ROOT_INO 2
38656+#define AUFS_FIRST_INO 11
38657+
38658+#define AUFS_WH_PFX ".wh."
38659+#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
38660+#define AUFS_WH_TMP_LEN 4
38661+/* a limit for rmdir/rename a dir and copyup */
38662+#define AUFS_MAX_NAMELEN (NAME_MAX \
38663+ - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
38664+ - 1 /* dot */\
38665+ - AUFS_WH_TMP_LEN) /* hex */
38666+#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
38667+#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
38668+#define AUFS_XINO_DEF_SEC 30 /* seconds */
38669+#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
38670+#define AUFS_DIRWH_DEF 3
38671+#define AUFS_RDCACHE_DEF 10 /* seconds */
38672+#define AUFS_RDCACHE_MAX 3600 /* seconds */
38673+#define AUFS_RDBLK_DEF 512 /* bytes */
38674+#define AUFS_RDHASH_DEF 32
38675+#define AUFS_WKQ_NAME AUFS_NAME "d"
38676+#define AUFS_MFS_DEF_SEC 30 /* seconds */
38677+#define AUFS_MFS_MAX_SEC 3600 /* seconds */
38678+#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
38679+#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
38680+
38681+/* pseudo-link maintenace under /proc */
38682+#define AUFS_PLINK_MAINT_NAME "plink_maint"
38683+#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
38684+#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
38685+
38686+/* dirren, renamed dir */
38687+#define AUFS_DR_INFO_PFX AUFS_WH_PFX ".dr."
38688+#define AUFS_DR_BRHINO_NAME AUFS_WH_PFX "hino"
38689+/* whiteouted doubly */
38690+#define AUFS_WH_DR_INFO_PFX AUFS_WH_PFX AUFS_DR_INFO_PFX
38691+#define AUFS_WH_DR_BRHINO AUFS_WH_PFX AUFS_DR_BRHINO_NAME
38692+
38693+#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
38694+#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
38695+
38696+#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
38697+#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
38698+#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
38699+
38700+/* doubly whiteouted */
38701+#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
38702+#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
38703+#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
38704+
38705+/* branch permissions and attributes */
38706+#define AUFS_BRPERM_RW "rw"
38707+#define AUFS_BRPERM_RO "ro"
38708+#define AUFS_BRPERM_RR "rr"
38709+#define AUFS_BRATTR_COO_REG "coo_reg"
38710+#define AUFS_BRATTR_COO_ALL "coo_all"
38711+#define AUFS_BRATTR_FHSM "fhsm"
38712+#define AUFS_BRATTR_UNPIN "unpin"
38713+#define AUFS_BRATTR_ICEX "icex"
38714+#define AUFS_BRATTR_ICEX_SEC "icexsec"
38715+#define AUFS_BRATTR_ICEX_SYS "icexsys"
38716+#define AUFS_BRATTR_ICEX_TR "icextr"
38717+#define AUFS_BRATTR_ICEX_USR "icexusr"
38718+#define AUFS_BRATTR_ICEX_OTH "icexoth"
38719+#define AUFS_BRRATTR_WH "wh"
38720+#define AUFS_BRWATTR_NLWH "nolwh"
38721+#define AUFS_BRWATTR_MOO "moo"
38722+
38723+#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
38724+#define AuBrPerm_RO (1 << 1) /* readonly */
38725+#define AuBrPerm_RR (1 << 2) /* natively readonly */
38726+#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
38727+
38728+#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
38729+#define AuBrAttr_COO_ALL (1 << 4)
38730+#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
38731+
38732+#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
38733+#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
38734+ branch. meaningless since
38735+ linux-3.18-rc1 */
38736+
38737+/* ignore error in copying XATTR */
38738+#define AuBrAttr_ICEX_SEC (1 << 7)
38739+#define AuBrAttr_ICEX_SYS (1 << 8)
38740+#define AuBrAttr_ICEX_TR (1 << 9)
38741+#define AuBrAttr_ICEX_USR (1 << 10)
38742+#define AuBrAttr_ICEX_OTH (1 << 11)
38743+#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
38744+ | AuBrAttr_ICEX_SYS \
38745+ | AuBrAttr_ICEX_TR \
38746+ | AuBrAttr_ICEX_USR \
38747+ | AuBrAttr_ICEX_OTH)
38748+
38749+#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
38750+#define AuBrRAttr_Mask AuBrRAttr_WH
38751+
38752+#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
38753+#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
38754+#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
38755+
38756+#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
38757+
38758+/* #warning test userspace */
38759+#ifdef __KERNEL__
38760+#ifndef CONFIG_AUFS_FHSM
38761+#undef AuBrAttr_FHSM
38762+#define AuBrAttr_FHSM 0
38763+#endif
38764+#ifndef CONFIG_AUFS_XATTR
38765+#undef AuBrAttr_ICEX
38766+#define AuBrAttr_ICEX 0
38767+#undef AuBrAttr_ICEX_SEC
38768+#define AuBrAttr_ICEX_SEC 0
38769+#undef AuBrAttr_ICEX_SYS
38770+#define AuBrAttr_ICEX_SYS 0
38771+#undef AuBrAttr_ICEX_TR
38772+#define AuBrAttr_ICEX_TR 0
38773+#undef AuBrAttr_ICEX_USR
38774+#define AuBrAttr_ICEX_USR 0
38775+#undef AuBrAttr_ICEX_OTH
38776+#define AuBrAttr_ICEX_OTH 0
38777+#endif
38778+#endif
38779+
38780+/* the longest combination */
38781+/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
38782+#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
38783+ "+" AUFS_BRATTR_COO_REG \
38784+ "+" AUFS_BRATTR_FHSM \
38785+ "+" AUFS_BRATTR_UNPIN \
38786+ "+" AUFS_BRATTR_ICEX_SEC \
38787+ "+" AUFS_BRATTR_ICEX_SYS \
38788+ "+" AUFS_BRATTR_ICEX_USR \
38789+ "+" AUFS_BRATTR_ICEX_OTH \
38790+ "+" AUFS_BRWATTR_NLWH)
38791+
38792+typedef struct {
38793+ char a[AuBrPermStrSz];
38794+} au_br_perm_str_t;
38795+
38796+static inline int au_br_writable(int brperm)
38797+{
38798+ return brperm & AuBrPerm_RW;
38799+}
38800+
38801+static inline int au_br_whable(int brperm)
38802+{
38803+ return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
38804+}
38805+
38806+static inline int au_br_wh_linkable(int brperm)
38807+{
38808+ return !(brperm & AuBrWAttr_NoLinkWH);
38809+}
38810+
38811+static inline int au_br_cmoo(int brperm)
38812+{
38813+ return brperm & AuBrAttr_CMOO_Mask;
38814+}
38815+
38816+static inline int au_br_fhsm(int brperm)
38817+{
38818+ return brperm & AuBrAttr_FHSM;
38819+}
38820+
38821+/* ---------------------------------------------------------------------- */
38822+
38823+/* ioctl */
38824+enum {
38825+ /* readdir in userspace */
38826+ AuCtl_RDU,
38827+ AuCtl_RDU_INO,
38828+
38829+ AuCtl_WBR_FD, /* pathconf wrapper */
38830+ AuCtl_IBUSY, /* busy inode */
38831+ AuCtl_MVDOWN, /* move-down */
38832+ AuCtl_BR, /* info about branches */
38833+ AuCtl_FHSM_FD /* connection for fhsm */
38834+};
38835+
38836+/* borrowed from linux/include/linux/kernel.h */
38837+#ifndef ALIGN
38838+#ifdef _GNU_SOURCE
38839+#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
38840+#else
38841+#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
38842+#endif
38843+#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
38844+#endif
38845+
38846+/* borrowed from linux/include/linux/compiler-gcc3.h */
38847+#ifndef __aligned
38848+#define __aligned(x) __attribute__((aligned(x)))
38849+#endif
38850+
38851+#ifdef __KERNEL__
38852+#ifndef __packed
38853+#define __packed __attribute__((packed))
38854+#endif
38855+#endif
38856+
38857+struct au_rdu_cookie {
38858+ uint64_t h_pos;
38859+ int16_t bindex;
38860+ uint8_t flags;
38861+ uint8_t pad;
38862+ uint32_t generation;
38863+} __aligned(8);
38864+
38865+struct au_rdu_ent {
38866+ uint64_t ino;
38867+ int16_t bindex;
38868+ uint8_t type;
38869+ uint8_t nlen;
38870+ uint8_t wh;
38871+ char name[];
38872+} __aligned(8);
38873+
38874+static inline int au_rdu_len(int nlen)
38875+{
38876+ /* include the terminating NULL */
38877+ return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
38878+ sizeof(uint64_t));
38879+}
38880+
38881+union au_rdu_ent_ul {
38882+ struct au_rdu_ent __user *e;
38883+ uint64_t ul;
38884+};
38885+
38886+enum {
38887+ AufsCtlRduV_SZ,
38888+ AufsCtlRduV_End
38889+};
38890+
38891+struct aufs_rdu {
38892+ /* input */
38893+ union {
38894+ uint64_t sz; /* AuCtl_RDU */
38895+ uint64_t nent; /* AuCtl_RDU_INO */
38896+ };
38897+ union au_rdu_ent_ul ent;
38898+ uint16_t verify[AufsCtlRduV_End];
38899+
38900+ /* input/output */
38901+ uint32_t blk;
38902+
38903+ /* output */
38904+ union au_rdu_ent_ul tail;
38905+ /* number of entries which were added in a single call */
38906+ uint64_t rent;
38907+ uint8_t full;
38908+ uint8_t shwh;
38909+
38910+ struct au_rdu_cookie cookie;
38911+} __aligned(8);
38912+
38913+/* ---------------------------------------------------------------------- */
38914+
38915+/* dirren. the branch is identified by the filename who contains this */
38916+struct au_drinfo {
38917+ uint64_t ino;
38918+ union {
38919+ uint8_t oldnamelen;
38920+ uint64_t _padding;
38921+ };
38922+ uint8_t oldname[];
38923+} __aligned(8);
38924+
38925+struct au_drinfo_fdata {
38926+ uint32_t magic;
38927+ struct au_drinfo drinfo;
38928+} __aligned(8);
38929+
38930+#define AUFS_DRINFO_MAGIC_V1 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
38931+/* future */
38932+#define AUFS_DRINFO_MAGIC_V2 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
38933+
38934+/* ---------------------------------------------------------------------- */
38935+
38936+struct aufs_wbr_fd {
38937+ uint32_t oflags;
38938+ int16_t brid;
38939+} __aligned(8);
38940+
38941+/* ---------------------------------------------------------------------- */
38942+
38943+struct aufs_ibusy {
38944+ uint64_t ino, h_ino;
38945+ int16_t bindex;
38946+} __aligned(8);
38947+
38948+/* ---------------------------------------------------------------------- */
38949+
38950+/* error code for move-down */
38951+/* the actual message strings are implemented in aufs-util.git */
38952+enum {
38953+ EAU_MVDOWN_OPAQUE = 1,
38954+ EAU_MVDOWN_WHITEOUT,
38955+ EAU_MVDOWN_UPPER,
38956+ EAU_MVDOWN_BOTTOM,
38957+ EAU_MVDOWN_NOUPPER,
38958+ EAU_MVDOWN_NOLOWERBR,
38959+ EAU_Last
38960+};
38961+
38962+/* flags for move-down */
38963+#define AUFS_MVDOWN_DMSG 1
38964+#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
38965+#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
38966+#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
38967+#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
38968+#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
38969+#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
38970+#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
38971+#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
38972+#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
38973+#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
38974+#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
38975+#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
38976+
38977+/* index for move-down */
38978+enum {
38979+ AUFS_MVDOWN_UPPER,
38980+ AUFS_MVDOWN_LOWER,
38981+ AUFS_MVDOWN_NARRAY
38982+};
38983+
38984+/*
38985+ * additional info of move-down
38986+ * number of free blocks and inodes.
38987+ * subset of struct kstatfs, but smaller and always 64bit.
38988+ */
38989+struct aufs_stfs {
38990+ uint64_t f_blocks;
38991+ uint64_t f_bavail;
38992+ uint64_t f_files;
38993+ uint64_t f_ffree;
38994+};
38995+
38996+struct aufs_stbr {
38997+ int16_t brid; /* optional input */
38998+ int16_t bindex; /* output */
38999+ struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
39000+} __aligned(8);
39001+
39002+struct aufs_mvdown {
39003+ uint32_t flags; /* input/output */
39004+ struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39005+ int8_t au_errno; /* output */
39006+} __aligned(8);
39007+
39008+/* ---------------------------------------------------------------------- */
39009+
39010+union aufs_brinfo {
39011+ /* PATH_MAX may differ between kernel-space and user-space */
39012+ char _spacer[4096];
39013+ struct {
39014+ int16_t id;
39015+ int perm;
39016+ char path[];
39017+ };
39018+} __aligned(8);
39019+
39020+/* ---------------------------------------------------------------------- */
39021+
39022+#define AuCtlType 'A'
39023+#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39024+#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
39025+#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
39026+ struct aufs_wbr_fd)
39027+#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
39028+#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
39029+ struct aufs_mvdown)
39030+#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39031+#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
39032+
39033+#endif /* __AUFS_TYPE_H__ */
39034SPDX-License-Identifier: GPL-2.0
39035aufs5.10 loopback patch
39036
39037diff --git a/drivers/block/loop.c b/drivers/block/loop.c
39038index 7be7ca3f54547..a163aeade6148 100644
39039--- a/drivers/block/loop.c
39040+++ b/drivers/block/loop.c
39041@@ -650,6 +650,15 @@ static inline void loop_update_dio(struct loop_device *lo)
39042 lo->use_dio);
39043 }
39044
39045+static struct file *loop_real_file(struct file *file)
39046+{
39047+ struct file *f = NULL;
39048+
39049+ if (file->f_path.dentry->d_sb->s_op->real_loop)
39050+ f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39051+ return f;
39052+}
39053+
39054 static void loop_reread_partitions(struct loop_device *lo,
39055 struct block_device *bdev)
39056 {
39057@@ -705,6 +714,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39058 unsigned int arg)
39059 {
39060 struct file *file = NULL, *old_file;
39061+ struct file *f, *virt_file = NULL, *old_virt_file;
39062 int error;
39063 bool partscan;
39064
39065@@ -724,12 +734,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39066 file = fget(arg);
39067 if (!file)
39068 goto out_err;
39069+ f = loop_real_file(file);
39070+ if (f) {
39071+ virt_file = file;
39072+ file = f;
39073+ get_file(file);
39074+ }
39075
39076 error = loop_validate_file(file, bdev);
39077 if (error)
39078 goto out_err;
39079
39080 old_file = lo->lo_backing_file;
39081+ old_virt_file = lo->lo_backing_virt_file;
39082
39083 error = -EINVAL;
39084
39085@@ -741,6 +758,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39086 blk_mq_freeze_queue(lo->lo_queue);
39087 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39088 lo->lo_backing_file = file;
39089+ lo->lo_backing_virt_file = virt_file;
39090 lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39091 mapping_set_gfp_mask(file->f_mapping,
39092 lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39093@@ -754,6 +772,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39094 * dependency.
39095 */
39096 fput(old_file);
39097+ if (old_virt_file)
39098+ fput(old_virt_file);
39099 if (partscan)
39100 loop_reread_partitions(lo, bdev);
39101 return 0;
39102@@ -762,6 +782,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39103 mutex_unlock(&loop_ctl_mutex);
39104 if (file)
39105 fput(file);
39106+ if (virt_file)
39107+ fput(virt_file);
39108 return error;
39109 }
39110
39111@@ -1090,7 +1112,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39112 struct block_device *bdev,
39113 const struct loop_config *config)
39114 {
39115- struct file *file;
39116+ struct file *file, *f, *virt_file = NULL;
39117 struct inode *inode;
39118 struct address_space *mapping;
39119 struct block_device *claimed_bdev = NULL;
39120@@ -1106,6 +1128,12 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39121 file = fget(config->fd);
39122 if (!file)
39123 goto out;
39124+ f = loop_real_file(file);
39125+ if (f) {
39126+ virt_file = file;
39127+ file = f;
39128+ get_file(file);
39129+ }
39130
39131 /*
39132 * If we don't hold exclusive handle for the device, upgrade to it
39133@@ -1161,6 +1189,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39134 lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
39135 lo->lo_device = bdev;
39136 lo->lo_backing_file = file;
39137+ lo->lo_backing_virt_file = virt_file;
39138 lo->old_gfp_mask = mapping_gfp_mask(mapping);
39139 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39140
39141@@ -1214,6 +1243,8 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39142 bd_abort_claiming(bdev, claimed_bdev, loop_configure);
39143 out_putf:
39144 fput(file);
39145+ if (virt_file)
39146+ fput(virt_file);
39147 out:
39148 /* This is safe: open() is still holding a reference. */
39149 module_put(THIS_MODULE);
39150@@ -1223,6 +1254,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39151 static int __loop_clr_fd(struct loop_device *lo, bool release)
39152 {
39153 struct file *filp = NULL;
39154+ struct file *virt_filp = lo->lo_backing_virt_file;
39155 gfp_t gfp = lo->old_gfp_mask;
39156 struct block_device *bdev = lo->lo_device;
39157 int err = 0;
39158@@ -1246,6 +1278,7 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
39159
39160 spin_lock_irq(&lo->lo_lock);
39161 lo->lo_backing_file = NULL;
39162+ lo->lo_backing_virt_file = NULL;
39163 spin_unlock_irq(&lo->lo_lock);
39164
39165 loop_release_xfer(lo);
39166@@ -1329,6 +1362,8 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
39167 */
39168 if (filp)
39169 fput(filp);
39170+ if (virt_filp)
39171+ fput(virt_filp);
39172 return err;
39173 }
39174
39175diff --git a/drivers/block/loop.h b/drivers/block/loop.h
39176index af75a5ee40944..1d847cb194ff6 100644
39177--- a/drivers/block/loop.h
39178+++ b/drivers/block/loop.h
39179@@ -46,7 +46,7 @@ struct loop_device {
39180 int (*ioctl)(struct loop_device *, int cmd,
39181 unsigned long arg);
39182
39183- struct file * lo_backing_file;
39184+ struct file *lo_backing_file, *lo_backing_virt_file;
39185 struct block_device *lo_device;
39186 void *key_data;
39187
39188diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
39189index 5ef279a59bb14..6447c37f98756 100644
39190--- a/fs/aufs/f_op.c
39191+++ b/fs/aufs/f_op.c
39192@@ -304,7 +304,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
39193 if (IS_ERR(h_file))
39194 goto out;
39195
39196- if (au_test_loopback_kthread()) {
39197+ if (0 && au_test_loopback_kthread()) {
39198 au_warn_loopback(h_file->f_path.dentry->d_sb);
39199 if (file->f_mapping != h_file->f_mapping) {
39200 file->f_mapping = h_file->f_mapping;
39201diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
39202index a8b63acc62045..9d97c3af5686a 100644
39203--- a/fs/aufs/loop.c
39204+++ b/fs/aufs/loop.c
39205@@ -133,3 +133,19 @@ void au_loopback_fin(void)
39206 symbol_put(loop_backing_file);
39207 au_kfree_try_rcu(au_warn_loopback_array);
39208 }
39209+
39210+/* ---------------------------------------------------------------------- */
39211+
39212+/* support the loopback block device insude aufs */
39213+
39214+struct file *aufs_real_loop(struct file *file)
39215+{
39216+ struct file *f;
39217+
39218+ BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39219+ fi_read_lock(file);
39220+ f = au_hf_top(file);
39221+ fi_read_unlock(file);
39222+ AuDebugOn(!f);
39223+ return f;
39224+}
39225diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
39226index 94f4f80ae33bf..ca1194354aff4 100644
39227--- a/fs/aufs/loop.h
39228+++ b/fs/aufs/loop.h
39229@@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
39230
39231 int au_loopback_init(void);
39232 void au_loopback_fin(void);
39233+
39234+struct file *aufs_real_loop(struct file *file);
39235 #else
39236 AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
39237
39238@@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
39239
39240 AuStubInt0(au_loopback_init, void)
39241 AuStubVoid(au_loopback_fin, void)
39242+
39243+AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39244 #endif /* BLK_DEV_LOOP */
39245
39246 #endif /* __KERNEL__ */
39247diff --git a/fs/aufs/super.c b/fs/aufs/super.c
39248index 589dd01220201..801e0a7faec59 100644
39249--- a/fs/aufs/super.c
39250+++ b/fs/aufs/super.c
39251@@ -844,7 +844,10 @@ static const struct super_operations aufs_sop = {
39252 .statfs = aufs_statfs,
39253 .put_super = aufs_put_super,
39254 .sync_fs = aufs_sync_fs,
39255- .remount_fs = aufs_remount_fs
39256+ .remount_fs = aufs_remount_fs,
39257+#ifdef CONFIG_AUFS_BDEV_LOOP
39258+ .real_loop = aufs_real_loop
39259+#endif
39260 };
39261
39262 /* ---------------------------------------------------------------------- */
39263diff --git a/include/linux/fs.h b/include/linux/fs.h
39264index 837dd6e727e45..0a05534f8582f 100644
39265--- a/include/linux/fs.h
39266+++ b/include/linux/fs.h
39267@@ -1967,6 +1967,10 @@ struct super_operations {
39268 struct shrink_control *);
39269 long (*free_cached_objects)(struct super_block *,
39270 struct shrink_control *);
39271+#if IS_ENABLED(CONFIG_BLK_DEV_LOOP) || IS_ENABLED(CONFIG_BLK_DEV_LOOP_MODULE)
39272+ /* and aufs */
39273+ struct file *(*real_loop)(struct file *);
39274+#endif
39275 };
39276
39277 /*
This page took 0.685423 seconds and 4 git commands to generate.