]> git.pld-linux.org Git - packages/kernel.git/blame - kernel-aufs4.patch
- 5.0.10
[packages/kernel.git] / kernel-aufs4.patch
CommitLineData
2121bcd9 1SPDX-License-Identifier: GPL-2.0
cd7a4cd9 2aufs4.x-rcN kbuild patch
7f207e10
AM
3
4diff --git a/fs/Kconfig b/fs/Kconfig
ba1aed25 5index ac474a61be37..284cee954591 100644
7f207e10
AM
6--- a/fs/Kconfig
7+++ b/fs/Kconfig
cd7a4cd9 8@@ -255,6 +255,7 @@ source "fs/pstore/Kconfig"
5527c038 9 source "fs/sysv/Kconfig"
7e9cd9fe 10 source "fs/ufs/Kconfig"
7f207e10
AM
11 source "fs/exofs/Kconfig"
12+source "fs/aufs/Kconfig"
13
14 endif # MISC_FILESYSTEMS
15
16diff --git a/fs/Makefile b/fs/Makefile
ba1aed25 17index 293733f61594..12d19d0de07a 100644
7f207e10
AM
18--- a/fs/Makefile
19+++ b/fs/Makefile
be118d29 20@@ -128,3 +128,4 @@ obj-y += exofs/ # Multiple modules
7f207e10 21 obj-$(CONFIG_CEPH_FS) += ceph/
bf0370f2 22 obj-$(CONFIG_PSTORE) += pstore/
c06a8ce3 23 obj-$(CONFIG_EFIVAR_FS) += efivarfs/
86dc4139 24+obj-$(CONFIG_AUFS_FS) += aufs/
2121bcd9 25SPDX-License-Identifier: GPL-2.0
cd7a4cd9 26aufs4.x-rcN base patch
7f207e10 27
c1595e42 28diff --git a/MAINTAINERS b/MAINTAINERS
ba1aed25 29index 8c68de3cfd80..c08e5dd95b5f 100644
c1595e42
JR
30--- a/MAINTAINERS
31+++ b/MAINTAINERS
ba1aed25 32@@ -2636,6 +2636,19 @@ F: include/linux/audit.h
c1595e42
JR
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: linux-unionfs@vger.kernel.org
39+L: aufs-users@lists.sourceforge.net (members only)
40+W: http://aufs.sourceforge.net
5527c038 41+T: git://github.com/sfjro/aufs4-linux.git
c1595e42
JR
42+S: Supported
43+F: Documentation/filesystems/aufs/
44+F: Documentation/ABI/testing/debugfs-aufs
45+F: Documentation/ABI/testing/sysfs-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>
cd7a4cd9 51 S: Maintained
392086de 52diff --git a/drivers/block/loop.c b/drivers/block/loop.c
ba1aed25 53index cf5538942834..a271e17e4b10 100644
392086de
AM
54--- a/drivers/block/loop.c
55+++ b/drivers/block/loop.c
9f237c51 56@@ -738,6 +738,24 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
cd7a4cd9 57 return error;
392086de
AM
58 }
59
60+/*
61+ * for AUFS
62+ * no get/put for file.
63+ */
64+struct file *loop_backing_file(struct super_block *sb)
65+{
66+ struct file *ret;
67+ struct loop_device *l;
68+
69+ ret = NULL;
70+ if (MAJOR(sb->s_dev) == LOOP_MAJOR) {
71+ l = sb->s_bdev->bd_disk->private_data;
72+ ret = l->lo_backing_file;
73+ }
74+ return ret;
75+}
febd17d6 76+EXPORT_SYMBOL_GPL(loop_backing_file);
392086de
AM
77+
78 /* loop sysfs attributes */
79
80 static ssize_t loop_attr_show(struct device *dev, char *page,
c1595e42 81diff --git a/fs/dcache.c b/fs/dcache.c
ba1aed25 82index aac41adf4743..fc18967c2522 100644
c1595e42
JR
83--- a/fs/dcache.c
84+++ b/fs/dcache.c
ba1aed25 85@@ -1247,7 +1247,7 @@ enum d_walk_ret {
c1595e42 86 *
cd7a4cd9 87 * The @enter() callbacks are called with d_lock held.
c1595e42
JR
88 */
89-static void d_walk(struct dentry *parent, void *data,
90+void d_walk(struct dentry *parent, void *data,
cd7a4cd9 91 enum d_walk_ret (*enter)(void *, struct dentry *))
c1595e42 92 {
cd7a4cd9 93 struct dentry *this_parent;
febd17d6 94diff --git a/fs/fcntl.c b/fs/fcntl.c
ba1aed25 95index 083185174c6d..78234ee16784 100644
febd17d6
JR
96--- a/fs/fcntl.c
97+++ b/fs/fcntl.c
2121bcd9 98@@ -32,7 +32,7 @@
febd17d6
JR
99
100 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
101
102-static int setfl(int fd, struct file * filp, unsigned long arg)
103+int setfl(int fd, struct file * filp, unsigned long arg)
104 {
105 struct inode * inode = file_inode(filp);
106 int error = 0;
2121bcd9 107@@ -63,6 +63,8 @@ static int setfl(int fd, struct file * filp, unsigned long arg)
febd17d6
JR
108
109 if (filp->f_op->check_flags)
110 error = filp->f_op->check_flags(arg);
111+ if (!error && filp->f_op->setfl)
112+ error = filp->f_op->setfl(filp, arg);
113 if (error)
114 return error;
115
5afbbe0d 116diff --git a/fs/inode.c b/fs/inode.c
ba1aed25 117index 0cd47fe0dbe5..117e6d16c3a1 100644
5afbbe0d
AM
118--- a/fs/inode.c
119+++ b/fs/inode.c
9f237c51 120@@ -1660,7 +1660,7 @@ EXPORT_SYMBOL(generic_update_time);
5afbbe0d
AM
121 * This does the actual work of updating an inodes time or version. Must have
122 * had called mnt_want_write() before calling this.
123 */
cd7a4cd9
AM
124-static int update_time(struct inode *inode, struct timespec64 *time, int flags)
125+int update_time(struct inode *inode, struct timespec64 *time, int flags)
5afbbe0d 126 {
cd7a4cd9 127 int (*update_time)(struct inode *, struct timespec64 *, int);
5afbbe0d 128
8b6a4947 129diff --git a/fs/namespace.c b/fs/namespace.c
ba1aed25 130index a677b59efd74..c3af16d31a75 100644
8b6a4947
AM
131--- a/fs/namespace.c
132+++ b/fs/namespace.c
ba1aed25 133@@ -768,6 +768,12 @@ static inline int check_mnt(struct mount *mnt)
8b6a4947
AM
134 return mnt->mnt_ns == current->nsproxy->mnt_ns;
135 }
136
137+/* for aufs, CONFIG_AUFS_BR_FUSE */
138+int is_current_mnt_ns(struct vfsmount *mnt)
139+{
140+ return check_mnt(real_mount(mnt));
141+}
142+
143 /*
144 * vfsmount lock must be held for write
145 */
5527c038 146diff --git a/fs/read_write.c b/fs/read_write.c
ba1aed25 147index ff3c5e6f87cf..5c18a9e51f37 100644
5527c038
JR
148--- a/fs/read_write.c
149+++ b/fs/read_write.c
b00004a5 150@@ -489,6 +489,28 @@ ssize_t __vfs_write(struct file *file, const char __user *p, size_t count,
8b6a4947 151 return -EINVAL;
5527c038 152 }
5527c038
JR
153
154+vfs_readf_t vfs_readf(struct file *file)
155+{
156+ const struct file_operations *fop = file->f_op;
157+
158+ if (fop->read)
159+ return fop->read;
160+ if (fop->read_iter)
161+ return new_sync_read;
162+ return ERR_PTR(-ENOSYS);
163+}
164+
165+vfs_writef_t vfs_writef(struct file *file)
166+{
167+ const struct file_operations *fop = file->f_op;
168+
169+ if (fop->write)
170+ return fop->write;
171+ if (fop->write_iter)
172+ return new_sync_write;
173+ return ERR_PTR(-ENOSYS);
174+}
175+
8b6a4947 176 ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
5527c038
JR
177 {
178 mm_segment_t old_fs;
7f207e10 179diff --git a/fs/splice.c b/fs/splice.c
ba1aed25 180index de2ede048473..5dcf77b8e1b2 100644
7f207e10
AM
181--- a/fs/splice.c
182+++ b/fs/splice.c
9f237c51 183@@ -837,8 +837,8 @@ EXPORT_SYMBOL(generic_splice_sendpage);
7f207e10
AM
184 /*
185 * Attempt to initiate a splice from pipe to file.
186 */
187-static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
188- loff_t *ppos, size_t len, unsigned int flags)
189+long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
190+ loff_t *ppos, size_t len, unsigned int flags)
191 {
192 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
193 loff_t *, size_t, unsigned int);
9f237c51 194@@ -854,9 +854,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
7f207e10
AM
195 /*
196 * Attempt to initiate a splice from a file to a pipe.
197 */
198-static long do_splice_to(struct file *in, loff_t *ppos,
199- struct pipe_inode_info *pipe, size_t len,
200- unsigned int flags)
201+long do_splice_to(struct file *in, loff_t *ppos,
202+ struct pipe_inode_info *pipe, size_t len,
203+ unsigned int flags)
204 {
205 ssize_t (*splice_read)(struct file *, loff_t *,
206 struct pipe_inode_info *, size_t, unsigned int);
a2654f78 207diff --git a/fs/sync.c b/fs/sync.c
ba1aed25 208index b54e0541ad89..28607828e96f 100644
a2654f78
AM
209--- a/fs/sync.c
210+++ b/fs/sync.c
2121bcd9 211@@ -28,7 +28,7 @@
a2654f78
AM
212 * wait == 1 case since in that case write_inode() functions do
213 * sync_dirty_buffer() and thus effectively write one block at a time.
214 */
215-static int __sync_filesystem(struct super_block *sb, int wait)
216+int __sync_filesystem(struct super_block *sb, int wait)
217 {
218 if (wait)
219 sync_inodes_sb(sb);
5527c038 220diff --git a/include/linux/fs.h b/include/linux/fs.h
ba1aed25 221index 29d8e2cfed0e..5db265e7d35a 100644
5527c038
JR
222--- a/include/linux/fs.h
223+++ b/include/linux/fs.h
ba1aed25 224@@ -1310,6 +1310,7 @@ extern void fasync_free(struct fasync_struct *);
febd17d6
JR
225 /* can be called from interrupts */
226 extern void kill_fasync(struct fasync_struct **, int, int);
227
228+extern int setfl(int fd, struct file * filp, unsigned long arg);
229 extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
3c1bdaff 230 extern int f_setown(struct file *filp, unsigned long arg, int force);
febd17d6 231 extern void f_delown(struct file *filp);
ba1aed25 232@@ -1803,6 +1804,7 @@ struct file_operations {
febd17d6
JR
233 ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
234 unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
235 int (*check_flags)(int);
236+ int (*setfl)(struct file *, unsigned long);
237 int (*flock) (struct file *, int, struct file_lock *);
238 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
239 ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
ba1aed25 240@@ -1873,6 +1875,12 @@ ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
5527c038
JR
241 struct iovec *fast_pointer,
242 struct iovec **ret_pointer);
243
244+typedef ssize_t (*vfs_readf_t)(struct file *, char __user *, size_t, loff_t *);
245+typedef ssize_t (*vfs_writef_t)(struct file *, const char __user *, size_t,
246+ loff_t *);
247+vfs_readf_t vfs_readf(struct file *file);
248+vfs_writef_t vfs_writef(struct file *file);
249+
250 extern ssize_t __vfs_read(struct file *, char __user *, size_t, loff_t *);
5527c038 251 extern ssize_t vfs_read(struct file *, char __user *, size_t, loff_t *);
8b6a4947 252 extern ssize_t vfs_write(struct file *, const char __user *, size_t, loff_t *);
ba1aed25 253@@ -2298,6 +2306,7 @@ extern int current_umask(void);
5afbbe0d
AM
254 extern void ihold(struct inode * inode);
255 extern void iput(struct inode *);
cd7a4cd9
AM
256 extern int generic_update_time(struct inode *, struct timespec64 *, int);
257+extern int update_time(struct inode *, struct timespec64 *, int);
5afbbe0d
AM
258
259 /* /sys/fs */
260 extern struct kobject *fs_kobj;
ba1aed25 261@@ -2585,6 +2594,7 @@ static inline bool sb_is_blkdev_sb(struct super_block *sb)
a2654f78
AM
262 return false;
263 }
264 #endif
265+extern int __sync_filesystem(struct super_block *, int);
266 extern int sync_filesystem(struct super_block *);
267 extern const struct file_operations def_blk_fops;
268 extern const struct file_operations def_chr_fops;
8b6a4947 269diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
ba1aed25 270index c5335df2372f..4e48a5059536 100644
8b6a4947
AM
271--- a/include/linux/lockdep.h
272+++ b/include/linux/lockdep.h
ba1aed25 273@@ -306,6 +306,8 @@ static inline int lockdep_match_key(struct lockdep_map *lock,
8b6a4947
AM
274 return lock->key == key;
275 }
276
277+struct lock_class *lockdep_hlock_class(struct held_lock *hlock);
278+
279 /*
280 * Acquire a lock.
281 *
ba1aed25 282@@ -432,6 +434,7 @@ struct lockdep_map { };
8b6a4947
AM
283
284 #define lockdep_depth(tsk) (0)
285
286+#define lockdep_is_held(lock) (1)
287 #define lockdep_is_held_type(l, r) (1)
288
289 #define lockdep_assert_held(l) do { (void)(l); } while (0)
290diff --git a/include/linux/mnt_namespace.h b/include/linux/mnt_namespace.h
ba1aed25 291index 35942084cd40..24f5fd1a789d 100644
8b6a4947
AM
292--- a/include/linux/mnt_namespace.h
293+++ b/include/linux/mnt_namespace.h
2121bcd9 294@@ -6,11 +6,14 @@
8b6a4947
AM
295 struct mnt_namespace;
296 struct fs_struct;
297 struct user_namespace;
298+struct vfsmount;
299
300 extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
301 struct user_namespace *, struct fs_struct *);
302 extern void put_mnt_ns(struct mnt_namespace *ns);
303
304+extern int is_current_mnt_ns(struct vfsmount *mnt);
305+
306 extern const struct file_operations proc_mounts_operations;
307 extern const struct file_operations proc_mountinfo_operations;
308 extern const struct file_operations proc_mountstats_operations;
1e00d052 309diff --git a/include/linux/splice.h b/include/linux/splice.h
ba1aed25 310index 74b4911ac16d..19789fbea567 100644
1e00d052
AM
311--- a/include/linux/splice.h
312+++ b/include/linux/splice.h
2121bcd9 313@@ -87,4 +87,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
4b3da204
AM
314
315 extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
106341ce 316 extern const struct pipe_buf_operations default_pipe_buf_ops;
1e00d052
AM
317+
318+extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
319+ loff_t *ppos, size_t len, unsigned int flags);
320+extern long do_splice_to(struct file *in, loff_t *ppos,
321+ struct pipe_inode_info *pipe, size_t len,
322+ unsigned int flags);
323 #endif
8b6a4947 324diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
ba1aed25 325index 95932333a48b..516ab7da26fb 100644
8b6a4947
AM
326--- a/kernel/locking/lockdep.c
327+++ b/kernel/locking/lockdep.c
ba1aed25
AM
328@@ -143,7 +143,7 @@ static
329 #endif
9f237c51 330 struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
8b6a4947
AM
331
332-static inline struct lock_class *hlock_class(struct held_lock *hlock)
333+inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
334 {
335 if (!hlock->class_idx) {
336 /*
ba1aed25 337@@ -154,6 +154,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
8b6a4947
AM
338 }
339 return lock_classes + hlock->class_idx - 1;
340 }
341+#define hlock_class(hlock) lockdep_hlock_class(hlock)
342
343 #ifdef CONFIG_LOCK_STAT
344 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
2121bcd9 345SPDX-License-Identifier: GPL-2.0
cd7a4cd9 346aufs4.x-rcN mmap patch
fb47a38f 347
c1595e42 348diff --git a/fs/proc/base.c b/fs/proc/base.c
ba1aed25 349index 633a63462573..cb290f06bd7e 100644
c1595e42
JR
350--- a/fs/proc/base.c
351+++ b/fs/proc/base.c
ba1aed25 352@@ -2018,7 +2018,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
c1595e42
JR
353 down_read(&mm->mmap_sem);
354 vma = find_exact_vma(mm, vm_start, vm_end);
355 if (vma && vma->vm_file) {
356- *path = vma->vm_file->f_path;
357+ *path = vma_pr_or_file(vma)->f_path;
358 path_get(path);
359 rc = 0;
360 }
fb47a38f 361diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
ba1aed25 362index 3b63be64e436..fb9913bf3d10 100644
fb47a38f
JR
363--- a/fs/proc/nommu.c
364+++ b/fs/proc/nommu.c
076b876e 365@@ -45,7 +45,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
fb47a38f
JR
366 file = region->vm_file;
367
368 if (file) {
369- struct inode *inode = file_inode(region->vm_file);
370+ struct inode *inode;
076b876e 371+
fb47a38f
JR
372+ file = vmr_pr_or_file(region);
373+ inode = file_inode(file);
374 dev = inode->i_sb->s_dev;
375 ino = inode->i_ino;
376 }
377diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
ba1aed25 378index f0ec9edab2f3..35e9e2a9cc15 100644
fb47a38f
JR
379--- a/fs/proc/task_mmu.c
380+++ b/fs/proc/task_mmu.c
acd2b654 381@@ -305,7 +305,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
fb47a38f
JR
382 const char *name = NULL;
383
384 if (file) {
385- struct inode *inode = file_inode(vma->vm_file);
386+ struct inode *inode;
076b876e 387+
fb47a38f
JR
388+ file = vma_pr_or_file(vma);
389+ inode = file_inode(file);
390 dev = inode->i_sb->s_dev;
391 ino = inode->i_ino;
392 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
ba1aed25 393@@ -1734,7 +1737,7 @@ static int show_numa_map(struct seq_file *m, void *v)
076b876e
AM
394 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
395 struct vm_area_struct *vma = v;
396 struct numa_maps *md = &numa_priv->md;
397- struct file *file = vma->vm_file;
398+ struct file *file = vma_pr_or_file(vma);
076b876e 399 struct mm_struct *mm = vma->vm_mm;
7e9cd9fe
AM
400 struct mm_walk walk = {
401 .hugetlb_entry = gather_hugetlb_stats,
fb47a38f 402diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
ba1aed25 403index 0b63d68dedb2..400d1c594ceb 100644
fb47a38f
JR
404--- a/fs/proc/task_nommu.c
405+++ b/fs/proc/task_nommu.c
acd2b654 406@@ -155,7 +155,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
fb47a38f
JR
407 file = vma->vm_file;
408
409 if (file) {
410- struct inode *inode = file_inode(vma->vm_file);
411+ struct inode *inode;
076b876e 412+
b912730e 413+ file = vma_pr_or_file(vma);
fb47a38f
JR
414+ inode = file_inode(file);
415 dev = inode->i_sb->s_dev;
416 ino = inode->i_ino;
417 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
418diff --git a/include/linux/mm.h b/include/linux/mm.h
ba1aed25 419index 80bb6408fe73..59a1ea611ba7 100644
fb47a38f
JR
420--- a/include/linux/mm.h
421+++ b/include/linux/mm.h
ba1aed25 422@@ -1518,6 +1518,28 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
a3232e84 423 unmap_mapping_range(mapping, holebegin, holelen, 0);
fb47a38f 424 }
fb47a38f 425
076b876e
AM
426+extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
427+extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
f2c43d5f 428+ int);
076b876e
AM
429+extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
430+extern void vma_do_fput(struct vm_area_struct *, const char[], int);
fb47a38f 431+
f2c43d5f
AM
432+#define vma_file_update_time(vma) vma_do_file_update_time(vma, __func__, \
433+ __LINE__)
434+#define vma_pr_or_file(vma) vma_do_pr_or_file(vma, __func__, \
435+ __LINE__)
436+#define vma_get_file(vma) vma_do_get_file(vma, __func__, __LINE__)
437+#define vma_fput(vma) vma_do_fput(vma, __func__, __LINE__)
b912730e
AM
438+
439+#ifndef CONFIG_MMU
076b876e
AM
440+extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
441+extern void vmr_do_fput(struct vm_region *, const char[], int);
442+
f2c43d5f
AM
443+#define vmr_pr_or_file(region) vmr_do_pr_or_file(region, __func__, \
444+ __LINE__)
445+#define vmr_fput(region) vmr_do_fput(region, __func__, __LINE__)
b912730e 446+#endif /* !CONFIG_MMU */
fb47a38f 447+
a3232e84
JR
448 extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
449 void *buf, int len, unsigned int gup_flags);
fb47a38f 450 extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
fb47a38f 451diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
ba1aed25 452index 2c471a2c43fa..c6e7aa6996ff 100644
fb47a38f
JR
453--- a/include/linux/mm_types.h
454+++ b/include/linux/mm_types.h
ba1aed25 455@@ -244,6 +244,7 @@ struct vm_region {
fb47a38f
JR
456 unsigned long vm_top; /* region allocated to here */
457 unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */
458 struct file *vm_file; /* the backing file or NULL */
459+ struct file *vm_prfile; /* the virtual backing file or NULL */
460
461 int vm_usage; /* region usage count (access under nommu_region_sem) */
462 bool vm_icache_flushed : 1; /* true if the icache has been flushed for
ba1aed25 463@@ -318,6 +319,7 @@ struct vm_area_struct {
fb47a38f 464 unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
5afbbe0d 465 units */
fb47a38f
JR
466 struct file * vm_file; /* File we map to (can be NULL). */
467+ struct file *vm_prfile; /* shadow of vm_file */
468 void * vm_private_data; /* was vm_pte (shared mem) */
469
8b6a4947 470 atomic_long_t swap_readahead_info;
fb47a38f 471diff --git a/kernel/fork.c b/kernel/fork.c
ba1aed25 472index b69248e6f0e0..099b89978dd3 100644
fb47a38f
JR
473--- a/kernel/fork.c
474+++ b/kernel/fork.c
ba1aed25 475@@ -547,7 +547,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
fb47a38f
JR
476 struct inode *inode = file_inode(file);
477 struct address_space *mapping = file->f_mapping;
478
479- get_file(file);
480+ vma_get_file(tmp);
481 if (tmp->vm_flags & VM_DENYWRITE)
482 atomic_dec(&inode->i_writecount);
2000de60 483 i_mmap_lock_write(mapping);
076b876e 484diff --git a/mm/Makefile b/mm/Makefile
ba1aed25 485index d210cc9d6f80..e77e80ce7298 100644
076b876e
AM
486--- a/mm/Makefile
487+++ b/mm/Makefile
acd2b654 488@@ -39,7 +39,7 @@ obj-y := filemap.o mempool.o oom_kill.o fadvise.o \
076b876e 489 mm_init.o mmu_context.o percpu.o slab_common.o \
b00004a5 490 compaction.o vmacache.o \
076b876e 491 interval_tree.o list_lru.o workingset.o \
7e9cd9fe
AM
492- debug.o $(mmu-y)
493+ prfile.o debug.o $(mmu-y)
076b876e
AM
494
495 obj-y += init-mm.o
9f237c51 496 obj-y += memblock.o
fb47a38f 497diff --git a/mm/filemap.c b/mm/filemap.c
ba1aed25 498index 9f5e323e883e..90db684a7cf4 100644
fb47a38f
JR
499--- a/mm/filemap.c
500+++ b/mm/filemap.c
ba1aed25 501@@ -2677,7 +2677,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
cd7a4cd9 502 vm_fault_t ret = VM_FAULT_LOCKED;
fb47a38f
JR
503
504 sb_start_pagefault(inode->i_sb);
521ced18
JR
505- file_update_time(vmf->vma->vm_file);
506+ vma_file_update_time(vmf->vma);
fb47a38f
JR
507 lock_page(page);
508 if (page->mapping != inode->i_mapping) {
509 unlock_page(page);
fb47a38f 510diff --git a/mm/mmap.c b/mm/mmap.c
ba1aed25 511index f901065c4c64..1089a8328a53 100644
fb47a38f
JR
512--- a/mm/mmap.c
513+++ b/mm/mmap.c
c4adf169 514@@ -180,7 +180,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
fb47a38f
JR
515 if (vma->vm_ops && vma->vm_ops->close)
516 vma->vm_ops->close(vma);
517 if (vma->vm_file)
518- fput(vma->vm_file);
519+ vma_fput(vma);
520 mpol_put(vma_policy(vma));
cd7a4cd9 521 vm_area_free(vma);
fb47a38f 522 return next;
9f237c51 523@@ -929,7 +929,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
fb47a38f
JR
524 if (remove_next) {
525 if (file) {
526 uprobe_munmap(next, next->vm_start, next->vm_end);
527- fput(file);
528+ vma_fput(vma);
529 }
530 if (next->anon_vma)
531 anon_vma_merge(vma, next);
9f237c51 532@@ -1845,8 +1845,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
35939ee7
JR
533 return addr;
534
fb47a38f 535 unmap_and_free_vma:
fb47a38f
JR
536+ vma_fput(vma);
537 vma->vm_file = NULL;
538- fput(file);
539
540 /* Undo any partial mapping done by a device driver. */
541 unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
ba1aed25 542@@ -2676,7 +2676,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
fb47a38f
JR
543 goto out_free_mpol;
544
545 if (new->vm_file)
546- get_file(new->vm_file);
547+ vma_get_file(new);
548
549 if (new->vm_ops && new->vm_ops->open)
550 new->vm_ops->open(new);
ba1aed25 551@@ -2695,7 +2695,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
fb47a38f
JR
552 if (new->vm_ops && new->vm_ops->close)
553 new->vm_ops->close(new);
554 if (new->vm_file)
555- fput(new->vm_file);
556+ vma_fput(new);
557 unlink_anon_vmas(new);
558 out_free_mpol:
559 mpol_put(vma_policy(new));
ba1aed25 560@@ -2885,7 +2885,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
7e9cd9fe
AM
561 struct vm_area_struct *vma;
562 unsigned long populate = 0;
563 unsigned long ret = -EINVAL;
564- struct file *file;
5afbbe0d 565+ struct file *file, *prfile;
7e9cd9fe 566
cd7a4cd9 567 pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.rst.\n",
5afbbe0d 568 current->comm, current->pid);
ba1aed25 569@@ -2960,10 +2960,27 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
febd17d6 570 }
7e9cd9fe
AM
571 }
572
573- file = get_file(vma->vm_file);
574+ vma_get_file(vma);
5afbbe0d
AM
575+ file = vma->vm_file;
576+ prfile = vma->vm_prfile;
7e9cd9fe 577 ret = do_mmap_pgoff(vma->vm_file, start, size,
521ced18 578 prot, flags, pgoff, &populate, NULL);
5afbbe0d
AM
579+ if (!IS_ERR_VALUE(ret) && file && prfile) {
580+ struct vm_area_struct *new_vma;
581+
582+ new_vma = find_vma(mm, ret);
583+ if (!new_vma->vm_prfile)
584+ new_vma->vm_prfile = prfile;
585+ if (new_vma != vma)
586+ get_file(prfile);
587+ }
588+ /*
589+ * two fput()s instead of vma_fput(vma),
590+ * coz vma may not be available anymore.
591+ */
592 fput(file);
593+ if (prfile)
594+ fput(prfile);
7e9cd9fe
AM
595 out:
596 up_write(&mm->mmap_sem);
597 if (populate)
ba1aed25 598@@ -3253,7 +3270,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
79b8bda9
AM
599 if (anon_vma_clone(new_vma, vma))
600 goto out_free_mempol;
601 if (new_vma->vm_file)
602- get_file(new_vma->vm_file);
603+ vma_get_file(new_vma);
604 if (new_vma->vm_ops && new_vma->vm_ops->open)
605 new_vma->vm_ops->open(new_vma);
606 vma_link(mm, new_vma, prev, rb_link, rb_parent);
fb47a38f 607diff --git a/mm/nommu.c b/mm/nommu.c
ba1aed25 608index 749276beb109..d56f8f2bbd24 100644
fb47a38f
JR
609--- a/mm/nommu.c
610+++ b/mm/nommu.c
acd2b654 611@@ -625,7 +625,7 @@ static void __put_nommu_region(struct vm_region *region)
fb47a38f
JR
612 up_write(&nommu_region_sem);
613
614 if (region->vm_file)
615- fput(region->vm_file);
616+ vmr_fput(region);
617
618 /* IO memory and memory shared directly out of the pagecache
619 * from ramfs/tmpfs mustn't be released here */
acd2b654 620@@ -763,7 +763,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
fb47a38f
JR
621 if (vma->vm_ops && vma->vm_ops->close)
622 vma->vm_ops->close(vma);
623 if (vma->vm_file)
624- fput(vma->vm_file);
625+ vma_fput(vma);
626 put_nommu_region(vma->vm_region);
cd7a4cd9 627 vm_area_free(vma);
fb47a38f 628 }
acd2b654 629@@ -1286,7 +1286,7 @@ unsigned long do_mmap(struct file *file,
fb47a38f
JR
630 goto error_just_free;
631 }
632 }
633- fput(region->vm_file);
634+ vmr_fput(region);
635 kmem_cache_free(vm_region_jar, region);
636 region = pregion;
637 result = start;
acd2b654 638@@ -1361,7 +1361,7 @@ unsigned long do_mmap(struct file *file,
fb47a38f
JR
639 up_write(&nommu_region_sem);
640 error:
641 if (region->vm_file)
642- fput(region->vm_file);
643+ vmr_fput(region);
644 kmem_cache_free(vm_region_jar, region);
645 if (vma->vm_file)
cd7a4cd9 646 fput(vma->vm_file);
076b876e
AM
647diff --git a/mm/prfile.c b/mm/prfile.c
648new file mode 100644
ba1aed25 649index 000000000000..024cdcfae1b1
076b876e
AM
650--- /dev/null
651+++ b/mm/prfile.c
2121bcd9 652@@ -0,0 +1,86 @@
cd7a4cd9 653+// SPDX-License-Identifier: GPL-2.0
076b876e 654+/*
1c60b727
AM
655+ * Mainly for aufs which mmap(2) different file and wants to print different
656+ * path in /proc/PID/maps.
076b876e
AM
657+ * Call these functions via macros defined in linux/mm.h.
658+ *
659+ * See Documentation/filesystems/aufs/design/06mmap.txt
660+ *
ba1aed25 661+ * Copyright (c) 2014-2019 Junjro R. Okajima
076b876e
AM
662+ * Copyright (c) 2014 Ian Campbell
663+ */
664+
665+#include <linux/mm.h>
666+#include <linux/file.h>
667+#include <linux/fs.h>
668+
669+/* #define PRFILE_TRACE */
670+static inline void prfile_trace(struct file *f, struct file *pr,
671+ const char func[], int line, const char func2[])
672+{
673+#ifdef PRFILE_TRACE
674+ if (pr)
1c60b727 675+ pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
076b876e
AM
676+#endif
677+}
678+
076b876e
AM
679+void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
680+ int line)
681+{
682+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
683+
684+ prfile_trace(f, pr, func, line, __func__);
685+ file_update_time(f);
686+ if (f && pr)
687+ file_update_time(pr);
688+}
689+
690+struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
691+ int line)
692+{
693+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
694+
695+ prfile_trace(f, pr, func, line, __func__);
696+ return (f && pr) ? pr : f;
697+}
698+
699+void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
700+{
701+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
702+
703+ prfile_trace(f, pr, func, line, __func__);
704+ get_file(f);
705+ if (f && pr)
706+ get_file(pr);
707+}
708+
709+void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
710+{
711+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
712+
713+ prfile_trace(f, pr, func, line, __func__);
714+ fput(f);
715+ if (f && pr)
716+ fput(pr);
717+}
b912730e
AM
718+
719+#ifndef CONFIG_MMU
076b876e
AM
720+struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
721+ int line)
722+{
723+ struct file *f = region->vm_file, *pr = region->vm_prfile;
724+
725+ prfile_trace(f, pr, func, line, __func__);
726+ return (f && pr) ? pr : f;
727+}
728+
729+void vmr_do_fput(struct vm_region *region, const char func[], int line)
730+{
731+ struct file *f = region->vm_file, *pr = region->vm_prfile;
732+
733+ prfile_trace(f, pr, func, line, __func__);
734+ fput(f);
735+ if (f && pr)
736+ fput(pr);
737+}
b912730e 738+#endif /* !CONFIG_MMU */
2121bcd9 739SPDX-License-Identifier: GPL-2.0
cd7a4cd9 740aufs4.x-rcN standalone patch
7f207e10 741
c1595e42 742diff --git a/fs/dcache.c b/fs/dcache.c
ba1aed25 743index fc18967c2522..50eb587c1253 100644
c1595e42
JR
744--- a/fs/dcache.c
745+++ b/fs/dcache.c
ba1aed25 746@@ -1352,6 +1352,7 @@ void d_walk(struct dentry *parent, void *data,
c1595e42
JR
747 seq = 1;
748 goto again;
749 }
febd17d6 750+EXPORT_SYMBOL_GPL(d_walk);
c1595e42 751
a2654f78
AM
752 struct check_mount {
753 struct vfsmount *mnt;
ba1aed25 754@@ -2845,6 +2846,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
f2c43d5f
AM
755
756 write_sequnlock(&rename_lock);
757 }
758+EXPORT_SYMBOL_GPL(d_exchange);
759
760 /**
761 * d_ancestor - search for an ancestor
79b8bda9 762diff --git a/fs/exec.c b/fs/exec.c
ba1aed25 763index fb72d36f7823..7ce68004cbf8 100644
79b8bda9
AM
764--- a/fs/exec.c
765+++ b/fs/exec.c
521ced18 766@@ -109,6 +109,7 @@ bool path_noexec(const struct path *path)
79b8bda9
AM
767 return (path->mnt->mnt_flags & MNT_NOEXEC) ||
768 (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
769 }
febd17d6 770+EXPORT_SYMBOL_GPL(path_noexec);
79b8bda9
AM
771
772 #ifdef CONFIG_USELIB
773 /*
febd17d6 774diff --git a/fs/fcntl.c b/fs/fcntl.c
ba1aed25 775index 78234ee16784..2072f690b121 100644
febd17d6
JR
776--- a/fs/fcntl.c
777+++ b/fs/fcntl.c
2121bcd9 778@@ -85,6 +85,7 @@ int setfl(int fd, struct file * filp, unsigned long arg)
febd17d6
JR
779 out:
780 return error;
781 }
782+EXPORT_SYMBOL_GPL(setfl);
783
784 static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
785 int force)
b912730e 786diff --git a/fs/file_table.c b/fs/file_table.c
ba1aed25 787index 5679e7fcb6b0..961eec3df1eb 100644
b912730e
AM
788--- a/fs/file_table.c
789+++ b/fs/file_table.c
acd2b654 790@@ -161,6 +161,7 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
b912730e
AM
791 }
792 return ERR_PTR(-ENFILE);
793 }
acd2b654 794+EXPORT_SYMBOL_GPL(alloc_empty_file);
b912730e 795
acd2b654
AM
796 /*
797 * Variant of alloc_empty_file() that doesn't check and modify nr_files.
798@@ -323,6 +324,7 @@ void flush_delayed_fput(void)
8cdd5066
JR
799 {
800 delayed_fput(NULL);
801 }
febd17d6 802+EXPORT_SYMBOL_GPL(flush_delayed_fput);
8cdd5066
JR
803
804 static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput);
805
acd2b654 806@@ -365,6 +367,7 @@ void __fput_sync(struct file *file)
8cdd5066
JR
807 }
808
809 EXPORT_SYMBOL(fput);
febd17d6 810+EXPORT_SYMBOL_GPL(__fput_sync);
8cdd5066 811
79b8bda9 812 void __init files_init(void)
8b6a4947 813 {
5afbbe0d 814diff --git a/fs/inode.c b/fs/inode.c
ba1aed25 815index 117e6d16c3a1..63a31af2f9d8 100644
5afbbe0d
AM
816--- a/fs/inode.c
817+++ b/fs/inode.c
9f237c51 818@@ -1669,6 +1669,7 @@ int update_time(struct inode *inode, struct timespec64 *time, int flags)
5afbbe0d
AM
819
820 return update_time(inode, time, flags);
821 }
822+EXPORT_SYMBOL_GPL(update_time);
823
824 /**
825 * touch_atime - update the access time
7f207e10 826diff --git a/fs/namespace.c b/fs/namespace.c
ba1aed25 827index c3af16d31a75..f9fc947a6600 100644
7f207e10
AM
828--- a/fs/namespace.c
829+++ b/fs/namespace.c
ba1aed25 830@@ -434,6 +434,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
c06a8ce3
AM
831 mnt_dec_writers(real_mount(mnt));
832 preempt_enable();
833 }
834+EXPORT_SYMBOL_GPL(__mnt_drop_write);
835
836 /**
837 * mnt_drop_write - give up write access to a mount
ba1aed25 838@@ -773,6 +774,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
8b6a4947
AM
839 {
840 return check_mnt(real_mount(mnt));
841 }
842+EXPORT_SYMBOL_GPL(is_current_mnt_ns);
843
844 /*
845 * vfsmount lock must be held for write
ba1aed25 846@@ -1842,6 +1844,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
7f207e10
AM
847 }
848 return 0;
849 }
febd17d6 850+EXPORT_SYMBOL_GPL(iterate_mounts);
7f207e10 851
7eafdf33 852 static void cleanup_group_ids(struct mount *mnt, struct mount *end)
7f207e10
AM
853 {
854diff --git a/fs/notify/group.c b/fs/notify/group.c
ba1aed25 855index c03b83662876..817f22c6e191 100644
7f207e10
AM
856--- a/fs/notify/group.c
857+++ b/fs/notify/group.c
acd2b654 858@@ -23,6 +23,7 @@
7f207e10
AM
859 #include <linux/rculist.h>
860 #include <linux/wait.h>
acd2b654 861 #include <linux/memcontrol.h>
7f207e10
AM
862+#include <linux/module.h>
863
864 #include <linux/fsnotify_backend.h>
865 #include "fsnotify.h"
acd2b654 866@@ -112,6 +113,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
1716fcea 867 {
2121bcd9 868 refcount_inc(&group->refcnt);
1716fcea 869 }
febd17d6 870+EXPORT_SYMBOL_GPL(fsnotify_get_group);
1716fcea
AM
871
872 /*
873 * Drop a reference to a group. Free it if it's through.
acd2b654 874@@ -121,6 +123,7 @@ void fsnotify_put_group(struct fsnotify_group *group)
2121bcd9 875 if (refcount_dec_and_test(&group->refcnt))
1716fcea 876 fsnotify_final_destroy_group(group);
7f207e10 877 }
febd17d6 878+EXPORT_SYMBOL_GPL(fsnotify_put_group);
7f207e10
AM
879
880 /*
881 * Create a new fsnotify_group and hold a reference for the group returned.
acd2b654 882@@ -150,6 +153,7 @@ struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops)
7f207e10
AM
883
884 return group;
885 }
febd17d6 886+EXPORT_SYMBOL_GPL(fsnotify_alloc_group);
1716fcea
AM
887
888 int fsnotify_fasync(int fd, struct file *file, int on)
889 {
7f207e10 890diff --git a/fs/notify/mark.c b/fs/notify/mark.c
ba1aed25 891index d2dd16cb5989..cf709b7d611a 100644
7f207e10
AM
892--- a/fs/notify/mark.c
893+++ b/fs/notify/mark.c
9f237c51 894@@ -289,6 +289,7 @@ void fsnotify_put_mark(struct fsnotify_mark *mark)
cd7a4cd9
AM
895 queue_delayed_work(system_unbound_wq, &reaper_work,
896 FSNOTIFY_REAPER_DELAY);
7f207e10 897 }
febd17d6 898+EXPORT_SYMBOL_GPL(fsnotify_put_mark);
7f207e10 899
cd7a4cd9
AM
900 /*
901 * Get mark reference when we found the mark via lockless traversal of object
9f237c51 902@@ -443,6 +444,7 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark,
1716fcea 903 mutex_unlock(&group->mark_mutex);
79b8bda9 904 fsnotify_free_mark(mark);
7f207e10 905 }
febd17d6 906+EXPORT_SYMBOL_GPL(fsnotify_destroy_mark);
7f207e10 907
ffa93bbd
AM
908 /*
909 * Sorting function for lists of fsnotify marks.
9f237c51 910@@ -658,6 +660,7 @@ int fsnotify_add_mark(struct fsnotify_mark *mark, fsnotify_connp_t *connp,
cd7a4cd9 911 mutex_unlock(&group->mark_mutex);
7f207e10
AM
912 return ret;
913 }
febd17d6 914+EXPORT_SYMBOL_GPL(fsnotify_add_mark);
7f207e10 915
cd7a4cd9
AM
916 /*
917 * Given a list of marks, find the mark associated with given group. If found
9f237c51 918@@ -781,6 +784,7 @@ void fsnotify_init_mark(struct fsnotify_mark *mark,
ffa93bbd
AM
919 fsnotify_get_group(group);
920 mark->group = group;
7f207e10 921 }
febd17d6 922+EXPORT_SYMBOL_GPL(fsnotify_init_mark);
7f207e10 923
5afbbe0d
AM
924 /*
925 * Destroy all marks in destroy_list, waits for SRCU period to finish before
7f207e10 926diff --git a/fs/open.c b/fs/open.c
ba1aed25 927index 0285ce7dbd51..cb81623a8b09 100644
7f207e10
AM
928--- a/fs/open.c
929+++ b/fs/open.c
c2c0f25c 930@@ -64,6 +64,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
febd17d6 931 inode_unlock(dentry->d_inode);
7f207e10
AM
932 return ret;
933 }
febd17d6 934+EXPORT_SYMBOL_GPL(do_truncate);
7f207e10 935
5afbbe0d 936 long vfs_truncate(const struct path *path, loff_t length)
7f207e10 937 {
5527c038 938diff --git a/fs/read_write.c b/fs/read_write.c
ba1aed25 939index 5c18a9e51f37..542e59cd8d27 100644
5527c038
JR
940--- a/fs/read_write.c
941+++ b/fs/read_write.c
b00004a5 942@@ -459,6 +459,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
8b6a4947
AM
943
944 return ret;
945 }
946+EXPORT_SYMBOL_GPL(vfs_read);
947
948 static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
949 {
b00004a5 950@@ -499,6 +500,7 @@ vfs_readf_t vfs_readf(struct file *file)
5527c038
JR
951 return new_sync_read;
952 return ERR_PTR(-ENOSYS);
953 }
febd17d6 954+EXPORT_SYMBOL_GPL(vfs_readf);
5527c038
JR
955
956 vfs_writef_t vfs_writef(struct file *file)
957 {
b00004a5 958@@ -510,6 +512,7 @@ vfs_writef_t vfs_writef(struct file *file)
5527c038
JR
959 return new_sync_write;
960 return ERR_PTR(-ENOSYS);
961 }
febd17d6 962+EXPORT_SYMBOL_GPL(vfs_writef);
5527c038 963
8b6a4947
AM
964 ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
965 {
b00004a5 966@@ -579,6 +582,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
8b6a4947
AM
967
968 return ret;
969 }
970+EXPORT_SYMBOL_GPL(vfs_write);
971
972 static inline loff_t file_pos_read(struct file *file)
5527c038 973 {
7f207e10 974diff --git a/fs/splice.c b/fs/splice.c
ba1aed25 975index 5dcf77b8e1b2..63fe2652c67d 100644
7f207e10
AM
976--- a/fs/splice.c
977+++ b/fs/splice.c
9f237c51 978@@ -850,6 +850,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
392086de
AM
979
980 return splice_write(pipe, out, ppos, len, flags);
7f207e10 981 }
febd17d6 982+EXPORT_SYMBOL_GPL(do_splice_from);
7f207e10
AM
983
984 /*
985 * Attempt to initiate a splice from a file to a pipe.
9f237c51 986@@ -879,6 +880,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
7f207e10
AM
987
988 return splice_read(in, ppos, pipe, len, flags);
989 }
febd17d6 990+EXPORT_SYMBOL_GPL(do_splice_to);
7f207e10
AM
991
992 /**
993 * splice_direct_to_actor - splices data directly between two non-pipes
a2654f78 994diff --git a/fs/sync.c b/fs/sync.c
ba1aed25 995index 28607828e96f..ffd7ea43831e 100644
a2654f78
AM
996--- a/fs/sync.c
997+++ b/fs/sync.c
2121bcd9 998@@ -39,6 +39,7 @@ int __sync_filesystem(struct super_block *sb, int wait)
a2654f78
AM
999 sb->s_op->sync_fs(sb, wait);
1000 return __sync_blockdev(sb->s_bdev, wait);
1001 }
1002+EXPORT_SYMBOL_GPL(__sync_filesystem);
1003
1004 /*
1005 * Write out and wait upon all dirty data associated with this
c1595e42 1006diff --git a/fs/xattr.c b/fs/xattr.c
ba1aed25 1007index 0d6a6a4af861..7ce4701b7289 100644
c1595e42
JR
1008--- a/fs/xattr.c
1009+++ b/fs/xattr.c
acd2b654 1010@@ -295,6 +295,7 @@ vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
c1595e42
JR
1011 *xattr_value = value;
1012 return error;
1013 }
febd17d6 1014+EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
c1595e42 1015
febd17d6 1016 ssize_t
f2c43d5f 1017 __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
8b6a4947 1018diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
ba1aed25 1019index 516ab7da26fb..c4f72e461d28 100644
8b6a4947
AM
1020--- a/kernel/locking/lockdep.c
1021+++ b/kernel/locking/lockdep.c
ba1aed25 1022@@ -154,6 +154,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
8b6a4947
AM
1023 }
1024 return lock_classes + hlock->class_idx - 1;
1025 }
1026+EXPORT_SYMBOL_GPL(lockdep_hlock_class);
1027 #define hlock_class(hlock) lockdep_hlock_class(hlock)
1028
1029 #ifdef CONFIG_LOCK_STAT
8cdd5066 1030diff --git a/kernel/task_work.c b/kernel/task_work.c
ba1aed25 1031index 0fef395662a6..83fb1ecfc33d 100644
8cdd5066
JR
1032--- a/kernel/task_work.c
1033+++ b/kernel/task_work.c
2121bcd9 1034@@ -116,3 +116,4 @@ void task_work_run(void)
8cdd5066
JR
1035 } while (work);
1036 }
1037 }
febd17d6 1038+EXPORT_SYMBOL_GPL(task_work_run);
7f207e10 1039diff --git a/security/commoncap.c b/security/commoncap.c
ba1aed25 1040index 232db019f051..a402a5b72bf4 100644
7f207e10
AM
1041--- a/security/commoncap.c
1042+++ b/security/commoncap.c
ba1aed25 1043@@ -1332,12 +1332,14 @@ int cap_mmap_addr(unsigned long addr)
94337f0d 1044 }
7f207e10
AM
1045 return ret;
1046 }
febd17d6 1047+EXPORT_SYMBOL_GPL(cap_mmap_addr);
0c3ec466
AM
1048
1049 int cap_mmap_file(struct file *file, unsigned long reqprot,
1050 unsigned long prot, unsigned long flags)
1051 {
1052 return 0;
1053 }
febd17d6 1054+EXPORT_SYMBOL_GPL(cap_mmap_file);
c2c0f25c
AM
1055
1056 #ifdef CONFIG_SECURITY
1057
7f207e10 1058diff --git a/security/device_cgroup.c b/security/device_cgroup.c
ba1aed25 1059index cd97929fac66..424fd230866d 100644
7f207e10
AM
1060--- a/security/device_cgroup.c
1061+++ b/security/device_cgroup.c
2121bcd9 1062@@ -8,6 +8,7 @@
f6c5ef8b
AM
1063 #include <linux/device_cgroup.h>
1064 #include <linux/cgroup.h>
1065 #include <linux/ctype.h>
1066+#include <linux/export.h>
1067 #include <linux/list.h>
1068 #include <linux/uaccess.h>
1069 #include <linux/seq_file.h>
2121bcd9 1070@@ -824,3 +825,4 @@ int __devcgroup_check_permission(short type, u32 major, u32 minor,
7f207e10 1071
2121bcd9
AM
1072 return 0;
1073 }
1074+EXPORT_SYMBOL_GPL(__devcgroup_check_permission);
7f207e10 1075diff --git a/security/security.c b/security/security.c
ba1aed25 1076index 55bc49027ba9..870eaa8ebedc 100644
7f207e10
AM
1077--- a/security/security.c
1078+++ b/security/security.c
ba1aed25 1079@@ -566,6 +566,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
7f207e10 1080 return 0;
c2c0f25c 1081 return call_int_hook(path_rmdir, 0, dir, dentry);
7f207e10 1082 }
febd17d6 1083+EXPORT_SYMBOL_GPL(security_path_rmdir);
7f207e10 1084
5afbbe0d 1085 int security_path_unlink(const struct path *dir, struct dentry *dentry)
7f207e10 1086 {
ba1aed25 1087@@ -582,6 +583,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
7f207e10 1088 return 0;
c2c0f25c 1089 return call_int_hook(path_symlink, 0, dir, dentry, old_name);
7f207e10 1090 }
febd17d6 1091+EXPORT_SYMBOL_GPL(security_path_symlink);
7f207e10 1092
5afbbe0d 1093 int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
7f207e10 1094 struct dentry *new_dentry)
ba1aed25 1095@@ -590,6 +592,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
7f207e10 1096 return 0;
c2c0f25c 1097 return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
7f207e10 1098 }
febd17d6 1099+EXPORT_SYMBOL_GPL(security_path_link);
7f207e10 1100
5afbbe0d
AM
1101 int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
1102 const struct path *new_dir, struct dentry *new_dentry,
ba1aed25 1103@@ -617,6 +620,7 @@ int security_path_truncate(const struct path *path)
7f207e10 1104 return 0;
c2c0f25c 1105 return call_int_hook(path_truncate, 0, path);
7f207e10 1106 }
febd17d6 1107+EXPORT_SYMBOL_GPL(security_path_truncate);
7f207e10 1108
5afbbe0d 1109 int security_path_chmod(const struct path *path, umode_t mode)
7eafdf33 1110 {
ba1aed25 1111@@ -624,6 +628,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
7f207e10 1112 return 0;
c2c0f25c 1113 return call_int_hook(path_chmod, 0, path, mode);
7f207e10 1114 }
febd17d6 1115+EXPORT_SYMBOL_GPL(security_path_chmod);
7f207e10 1116
5afbbe0d 1117 int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
7f207e10 1118 {
ba1aed25 1119@@ -631,6 +636,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
7f207e10 1120 return 0;
c2c0f25c 1121 return call_int_hook(path_chown, 0, path, uid, gid);
7f207e10 1122 }
febd17d6 1123+EXPORT_SYMBOL_GPL(security_path_chown);
7f207e10 1124
5afbbe0d 1125 int security_path_chroot(const struct path *path)
7f207e10 1126 {
ba1aed25 1127@@ -716,6 +722,7 @@ int security_inode_readlink(struct dentry *dentry)
7f207e10 1128 return 0;
c2c0f25c 1129 return call_int_hook(inode_readlink, 0, dentry);
7f207e10 1130 }
febd17d6 1131+EXPORT_SYMBOL_GPL(security_inode_readlink);
7f207e10 1132
c2c0f25c
AM
1133 int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
1134 bool rcu)
ba1aed25 1135@@ -731,6 +738,7 @@ int security_inode_permission(struct inode *inode, int mask)
7f207e10 1136 return 0;
c2c0f25c 1137 return call_int_hook(inode_permission, 0, inode, mask);
7f207e10 1138 }
febd17d6 1139+EXPORT_SYMBOL_GPL(security_inode_permission);
7f207e10 1140
1e00d052 1141 int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
7f207e10 1142 {
ba1aed25 1143@@ -902,6 +910,7 @@ int security_file_permission(struct file *file, int mask)
7f207e10
AM
1144
1145 return fsnotify_perm(file, mask);
1146 }
febd17d6 1147+EXPORT_SYMBOL_GPL(security_file_permission);
7f207e10
AM
1148
1149 int security_file_alloc(struct file *file)
1150 {
ba1aed25 1151@@ -961,6 +970,7 @@ int security_mmap_file(struct file *file, unsigned long prot,
7f207e10
AM
1152 return ret;
1153 return ima_file_mmap(file, prot);
1154 }
febd17d6 1155+EXPORT_SYMBOL_GPL(security_mmap_file);
7f207e10 1156
0c3ec466
AM
1157 int security_mmap_addr(unsigned long addr)
1158 {
7f207e10
AM
1159diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
1160--- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 1161+++ linux/Documentation/ABI/testing/debugfs-aufs 2018-08-12 23:43:05.450124426 +0200
062440b3 1162@@ -0,0 +1,55 @@
7f207e10
AM
1163+What: /debug/aufs/si_<id>/
1164+Date: March 2009
f6b6e03d 1165+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1166+Description:
1167+ Under /debug/aufs, a directory named si_<id> is created
1168+ per aufs mount, where <id> is a unique id generated
1169+ internally.
1facf9fc 1170+
86dc4139
AM
1171+What: /debug/aufs/si_<id>/plink
1172+Date: Apr 2013
f6b6e03d 1173+Contact: J. R. Okajima <hooanon05g@gmail.com>
86dc4139
AM
1174+Description:
1175+ It has three lines and shows the information about the
1176+ pseudo-link. The first line is a single number
1177+ representing a number of buckets. The second line is a
1178+ number of pseudo-links per buckets (separated by a
1179+ blank). The last line is a single number representing a
1180+ total number of psedo-links.
1181+ When the aufs mount option 'noplink' is specified, it
1182+ will show "1\n0\n0\n".
1183+
7f207e10
AM
1184+What: /debug/aufs/si_<id>/xib
1185+Date: March 2009
f6b6e03d 1186+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1187+Description:
1188+ It shows the consumed blocks by xib (External Inode Number
1189+ Bitmap), its block size and file size.
1190+ When the aufs mount option 'noxino' is specified, it
1191+ will be empty. About XINO files, see the aufs manual.
1192+
062440b3 1193+What: /debug/aufs/si_<id>/xi0, xi1 ... xiN and xiN-N
7f207e10 1194+Date: March 2009
f6b6e03d 1195+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1196+Description:
1197+ It shows the consumed blocks by xino (External Inode Number
1198+ Translation Table), its link count, block size and file
1199+ size.
062440b3
AM
1200+ Due to the file size limit, there may exist multiple
1201+ xino files per branch. In this case, "-N" is added to
1202+ the filename and it corresponds to the index of the
1203+ internal xino array. "-0" is omitted.
1204+ When the aufs mount option 'noxino' is specified, Those
1205+ entries won't exist. About XINO files, see the aufs
1206+ manual.
7f207e10
AM
1207+
1208+What: /debug/aufs/si_<id>/xigen
1209+Date: March 2009
f6b6e03d 1210+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1211+Description:
1212+ It shows the consumed blocks by xigen (External Inode
1213+ Generation Table), its block size and file size.
1214+ If CONFIG_AUFS_EXPORT is disabled, this entry will not
1215+ be created.
1216+ When the aufs mount option 'noxino' is specified, it
1217+ will be empty. About XINO files, see the aufs manual.
1218diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
1219--- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs 1970-01-01 01:00:00.000000000 +0100
b00004a5 1220+++ linux/Documentation/ABI/testing/sysfs-aufs 2017-07-29 12:14:25.893041746 +0200
392086de 1221@@ -0,0 +1,31 @@
7f207e10
AM
1222+What: /sys/fs/aufs/si_<id>/
1223+Date: March 2009
f6b6e03d 1224+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1225+Description:
1226+ Under /sys/fs/aufs, a directory named si_<id> is created
1227+ per aufs mount, where <id> is a unique id generated
1228+ internally.
1229+
1230+What: /sys/fs/aufs/si_<id>/br0, br1 ... brN
1231+Date: March 2009
f6b6e03d 1232+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1233+Description:
1234+ It shows the abolute path of a member directory (which
1235+ is called branch) in aufs, and its permission.
1236+
392086de
AM
1237+What: /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
1238+Date: July 2013
f6b6e03d 1239+Contact: J. R. Okajima <hooanon05g@gmail.com>
392086de
AM
1240+Description:
1241+ It shows the id of a member directory (which is called
1242+ branch) in aufs.
1243+
7f207e10
AM
1244+What: /sys/fs/aufs/si_<id>/xi_path
1245+Date: March 2009
f6b6e03d 1246+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1247+Description:
1248+ It shows the abolute path of XINO (External Inode Number
1249+ Bitmap, Translation Table and Generation Table) file
1250+ even if it is the default path.
1251+ When the aufs mount option 'noxino' is specified, it
1252+ will be empty. About XINO files, see the aufs manual.
53392da6
AM
1253diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1254--- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt 1970-01-01 01:00:00.000000000 +0100
ba1aed25 1255+++ linux/Documentation/filesystems/aufs/design/01intro.txt 2019-03-05 12:13:00.132557473 +0100
1c60b727 1256@@ -0,0 +1,171 @@
53392da6 1257+
ba1aed25 1258+# Copyright (C) 2005-2019 Junjiro R. Okajima
53392da6
AM
1259+#
1260+# This program is free software; you can redistribute it and/or modify
1261+# it under the terms of the GNU General Public License as published by
1262+# the Free Software Foundation; either version 2 of the License, or
1263+# (at your option) any later version.
1264+#
1265+# This program is distributed in the hope that it will be useful,
1266+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1267+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1268+# GNU General Public License for more details.
1269+#
1270+# You should have received a copy of the GNU General Public License
523b37e3 1271+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1272+
1273+Introduction
1274+----------------------------------------
1275+
3c1bdaff 1276+aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
53392da6
AM
1277+1. abbrev. for "advanced multi-layered unification filesystem".
1278+2. abbrev. for "another unionfs".
1279+3. abbrev. for "auf das" in German which means "on the" in English.
1280+ Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1281+ But "Filesystem aufs Filesystem" is hard to understand.
1c60b727 1282+4. abbrev. for "African Urban Fashion Show".
53392da6
AM
1283+
1284+AUFS is a filesystem with features:
1285+- multi layered stackable unification filesystem, the member directory
1286+ is called as a branch.
1287+- branch permission and attribute, 'readonly', 'real-readonly',
7e9cd9fe 1288+ 'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
53392da6
AM
1289+ combination.
1290+- internal "file copy-on-write".
1291+- logical deletion, whiteout.
1292+- dynamic branch manipulation, adding, deleting and changing permission.
1293+- allow bypassing aufs, user's direct branch access.
1294+- external inode number translation table and bitmap which maintains the
1295+ persistent aufs inode number.
1296+- seekable directory, including NFS readdir.
1297+- file mapping, mmap and sharing pages.
1298+- pseudo-link, hardlink over branches.
1299+- loopback mounted filesystem as a branch.
1300+- several policies to select one among multiple writable branches.
1301+- revert a single systemcall when an error occurs in aufs.
1302+- and more...
1303+
1304+
1305+Multi Layered Stackable Unification Filesystem
1306+----------------------------------------------------------------------
1307+Most people already knows what it is.
1308+It is a filesystem which unifies several directories and provides a
1309+merged single directory. When users access a file, the access will be
1310+passed/re-directed/converted (sorry, I am not sure which English word is
1311+correct) to the real file on the member filesystem. The member
1312+filesystem is called 'lower filesystem' or 'branch' and has a mode
1313+'readonly' and 'readwrite.' And the deletion for a file on the lower
1314+readonly branch is handled by creating 'whiteout' on the upper writable
1315+branch.
1316+
1317+On LKML, there have been discussions about UnionMount (Jan Blunck,
1318+Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1319+different approaches to implement the merged-view.
1320+The former tries putting it into VFS, and the latter implements as a
1321+separate filesystem.
1322+(If I misunderstand about these implementations, please let me know and
1323+I shall correct it. Because it is a long time ago when I read their
1324+source files last time).
1325+
1326+UnionMount's approach will be able to small, but may be hard to share
1327+branches between several UnionMount since the whiteout in it is
1328+implemented in the inode on branch filesystem and always
1329+shared. According to Bharata's post, readdir does not seems to be
1330+finished yet.
1331+There are several missing features known in this implementations such as
1332+- for users, the inode number may change silently. eg. copy-up.
1333+- link(2) may break by copy-up.
1334+- read(2) may get an obsoleted filedata (fstat(2) too).
1335+- fcntl(F_SETLK) may be broken by copy-up.
1336+- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1337+ open(O_RDWR).
1338+
7e9cd9fe
AM
1339+In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1340+merged into mainline. This is another implementation of UnionMount as a
1341+separated filesystem. All the limitations and known problems which
1342+UnionMount are equally inherited to "overlay" filesystem.
1343+
1344+Unionfs has a longer history. When I started implementing a stackable
1345+filesystem (Aug 2005), it already existed. It has virtual super_block,
1346+inode, dentry and file objects and they have an array pointing lower
1347+same kind objects. After contributing many patches for Unionfs, I
1348+re-started my project AUFS (Jun 2006).
53392da6
AM
1349+
1350+In AUFS, the structure of filesystem resembles to Unionfs, but I
1351+implemented my own ideas, approaches and enhancements and it became
1352+totally different one.
1353+
1354+Comparing DM snapshot and fs based implementation
1355+- the number of bytes to be copied between devices is much smaller.
1356+- the type of filesystem must be one and only.
1357+- the fs must be writable, no readonly fs, even for the lower original
1358+ device. so the compression fs will not be usable. but if we use
1359+ loopback mount, we may address this issue.
1360+ for instance,
1361+ mount /cdrom/squashfs.img /sq
1362+ losetup /sq/ext2.img
1363+ losetup /somewhere/cow
1364+ dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1365+- it will be difficult (or needs more operations) to extract the
1366+ difference between the original device and COW.
1367+- DM snapshot-merge may help a lot when users try merging. in the
1368+ fs-layer union, users will use rsync(1).
1369+
7e9cd9fe
AM
1370+You may want to read my old paper "Filesystems in LiveCD"
1371+(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
53392da6 1372+
7e9cd9fe
AM
1373+
1374+Several characters/aspects/persona of aufs
53392da6
AM
1375+----------------------------------------------------------------------
1376+
7e9cd9fe 1377+Aufs has several characters, aspects or persona.
53392da6
AM
1378+1. a filesystem, callee of VFS helper
1379+2. sub-VFS, caller of VFS helper for branches
1380+3. a virtual filesystem which maintains persistent inode number
1381+4. reader/writer of files on branches such like an application
1382+
1383+1. Callee of VFS Helper
1384+As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1385+unlink(2) from an application reaches sys_unlink() kernel function and
1386+then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1387+calls filesystem specific unlink operation. Actually aufs implements the
1388+unlink operation but it behaves like a redirector.
1389+
1390+2. Caller of VFS Helper for Branches
1391+aufs_unlink() passes the unlink request to the branch filesystem as if
1392+it were called from VFS. So the called unlink operation of the branch
1393+filesystem acts as usual. As a caller of VFS helper, aufs should handle
1394+every necessary pre/post operation for the branch filesystem.
1395+- acquire the lock for the parent dir on a branch
1396+- lookup in a branch
1397+- revalidate dentry on a branch
1398+- mnt_want_write() for a branch
1399+- vfs_unlink() for a branch
1400+- mnt_drop_write() for a branch
1401+- release the lock on a branch
1402+
1403+3. Persistent Inode Number
1404+One of the most important issue for a filesystem is to maintain inode
1405+numbers. This is particularly important to support exporting a
1406+filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1407+backend block device for its own. But some storage is necessary to
7e9cd9fe
AM
1408+keep and maintain the inode numbers. It may be a large space and may not
1409+suit to keep in memory. Aufs rents some space from its first writable
1410+branch filesystem (by default) and creates file(s) on it. These files
1411+are created by aufs internally and removed soon (currently) keeping
1412+opened.
53392da6
AM
1413+Note: Because these files are removed, they are totally gone after
1414+ unmounting aufs. It means the inode numbers are not persistent
1415+ across unmount or reboot. I have a plan to make them really
1416+ persistent which will be important for aufs on NFS server.
1417+
1418+4. Read/Write Files Internally (copy-on-write)
1419+Because a branch can be readonly, when you write a file on it, aufs will
1420+"copy-up" it to the upper writable branch internally. And then write the
1421+originally requested thing to the file. Generally kernel doesn't
1422+open/read/write file actively. In aufs, even a single write may cause a
1423+internal "file copy". This behaviour is very similar to cp(1) command.
1424+
1425+Some people may think it is better to pass such work to user space
1426+helper, instead of doing in kernel space. Actually I am still thinking
1427+about it. But currently I have implemented it in kernel space.
1428diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1429--- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
ba1aed25 1430+++ linux/Documentation/filesystems/aufs/design/02struct.txt 2019-03-05 12:13:00.132557473 +0100
7e9cd9fe 1431@@ -0,0 +1,258 @@
53392da6 1432+
ba1aed25 1433+# Copyright (C) 2005-2019 Junjiro R. Okajima
53392da6
AM
1434+#
1435+# This program is free software; you can redistribute it and/or modify
1436+# it under the terms of the GNU General Public License as published by
1437+# the Free Software Foundation; either version 2 of the License, or
1438+# (at your option) any later version.
1439+#
1440+# This program is distributed in the hope that it will be useful,
1441+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1442+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1443+# GNU General Public License for more details.
1444+#
1445+# You should have received a copy of the GNU General Public License
523b37e3 1446+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1447+
1448+Basic Aufs Internal Structure
1449+
1450+Superblock/Inode/Dentry/File Objects
1451+----------------------------------------------------------------------
1452+As like an ordinary filesystem, aufs has its own
1453+superblock/inode/dentry/file objects. All these objects have a
1454+dynamically allocated array and store the same kind of pointers to the
1455+lower filesystem, branch.
1456+For example, when you build a union with one readwrite branch and one
1457+readonly, mounted /au, /rw and /ro respectively.
1458+- /au = /rw + /ro
1459+- /ro/fileA exists but /rw/fileA
1460+
1461+Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1462+pointers are stored in a aufs dentry. The array in aufs dentry will be,
7e9cd9fe 1463+- [0] = NULL (because /rw/fileA doesn't exist)
53392da6
AM
1464+- [1] = /ro/fileA
1465+
1466+This style of an array is essentially same to the aufs
1467+superblock/inode/dentry/file objects.
1468+
1469+Because aufs supports manipulating branches, ie. add/delete/change
7e9cd9fe
AM
1470+branches dynamically, these objects has its own generation. When
1471+branches are changed, the generation in aufs superblock is
1472+incremented. And a generation in other object are compared when it is
1473+accessed. When a generation in other objects are obsoleted, aufs
1474+refreshes the internal array.
53392da6
AM
1475+
1476+
1477+Superblock
1478+----------------------------------------------------------------------
1479+Additionally aufs superblock has some data for policies to select one
1480+among multiple writable branches, XIB files, pseudo-links and kobject.
1481+See below in detail.
7e9cd9fe
AM
1482+About the policies which supports copy-down a directory, see
1483+wbr_policy.txt too.
53392da6
AM
1484+
1485+
1486+Branch and XINO(External Inode Number Translation Table)
1487+----------------------------------------------------------------------
1488+Every branch has its own xino (external inode number translation table)
1489+file. The xino file is created and unlinked by aufs internally. When two
1490+members of a union exist on the same filesystem, they share the single
1491+xino file.
1492+The struct of a xino file is simple, just a sequence of aufs inode
1493+numbers which is indexed by the lower inode number.
1494+In the above sample, assume the inode number of /ro/fileA is i111 and
1495+aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1496+4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1497+
1498+When the inode numbers are not contiguous, the xino file will be sparse
1499+which has a hole in it and doesn't consume as much disk space as it
1500+might appear. If your branch filesystem consumes disk space for such
1501+holes, then you should specify 'xino=' option at mounting aufs.
1502+
7e9cd9fe
AM
1503+Aufs has a mount option to free the disk blocks for such holes in XINO
1504+files on tmpfs or ramdisk. But it is not so effective actually. If you
1505+meet a problem of disk shortage due to XINO files, then you should try
1506+"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1507+The patch localizes the assignment inumbers per tmpfs-mount and avoid
1508+the holes in XINO files.
1509+
53392da6 1510+Also a writable branch has three kinds of "whiteout bases". All these
7e9cd9fe 1511+are existed when the branch is joined to aufs, and their names are
53392da6
AM
1512+whiteout-ed doubly, so that users will never see their names in aufs
1513+hierarchy.
7e9cd9fe 1514+1. a regular file which will be hardlinked to all whiteouts.
53392da6 1515+2. a directory to store a pseudo-link.
7e9cd9fe 1516+3. a directory to store an "orphan"-ed file temporary.
53392da6
AM
1517+
1518+1. Whiteout Base
1519+ When you remove a file on a readonly branch, aufs handles it as a
1520+ logical deletion and creates a whiteout on the upper writable branch
1521+ as a hardlink of this file in order not to consume inode on the
1522+ writable branch.
1523+2. Pseudo-link Dir
1524+ See below, Pseudo-link.
1525+3. Step-Parent Dir
1526+ When "fileC" exists on the lower readonly branch only and it is
1527+ opened and removed with its parent dir, and then user writes
1528+ something into it, then aufs copies-up fileC to this
1529+ directory. Because there is no other dir to store fileC. After
1530+ creating a file under this dir, the file is unlinked.
1531+
1532+Because aufs supports manipulating branches, ie. add/delete/change
7e9cd9fe
AM
1533+dynamically, a branch has its own id. When the branch order changes,
1534+aufs finds the new index by searching the branch id.
53392da6
AM
1535+
1536+
1537+Pseudo-link
1538+----------------------------------------------------------------------
1539+Assume "fileA" exists on the lower readonly branch only and it is
1540+hardlinked to "fileB" on the branch. When you write something to fileA,
1541+aufs copies-up it to the upper writable branch. Additionally aufs
1542+creates a hardlink under the Pseudo-link Directory of the writable
1543+branch. The inode of a pseudo-link is kept in aufs super_block as a
1544+simple list. If fileB is read after unlinking fileA, aufs returns
1545+filedata from the pseudo-link instead of the lower readonly
1546+branch. Because the pseudo-link is based upon the inode, to keep the
7e9cd9fe 1547+inode number by xino (see above) is essentially necessary.
53392da6
AM
1548+
1549+All the hardlinks under the Pseudo-link Directory of the writable branch
1550+should be restored in a proper location later. Aufs provides a utility
1551+to do this. The userspace helpers executed at remounting and unmounting
1552+aufs by default.
1553+During this utility is running, it puts aufs into the pseudo-link
1554+maintenance mode. In this mode, only the process which began the
1555+maintenance mode (and its child processes) is allowed to operate in
1556+aufs. Some other processes which are not related to the pseudo-link will
1557+be allowed to run too, but the rest have to return an error or wait
1558+until the maintenance mode ends. If a process already acquires an inode
1559+mutex (in VFS), it has to return an error.
1560+
1561+
1562+XIB(external inode number bitmap)
1563+----------------------------------------------------------------------
1564+Addition to the xino file per a branch, aufs has an external inode number
7e9cd9fe
AM
1565+bitmap in a superblock object. It is also an internal file such like a
1566+xino file.
53392da6
AM
1567+It is a simple bitmap to mark whether the aufs inode number is in-use or
1568+not.
1569+To reduce the file I/O, aufs prepares a single memory page to cache xib.
1570+
7e9cd9fe 1571+As well as XINO files, aufs has a feature to truncate/refresh XIB to
53392da6
AM
1572+reduce the number of consumed disk blocks for these files.
1573+
1574+
1575+Virtual or Vertical Dir, and Readdir in Userspace
1576+----------------------------------------------------------------------
1577+In order to support multiple layers (branches), aufs readdir operation
1578+constructs a virtual dir block on memory. For readdir, aufs calls
1579+vfs_readdir() internally for each dir on branches, merges their entries
1580+with eliminating the whiteout-ed ones, and sets it to file (dir)
1581+object. So the file object has its entry list until it is closed. The
1582+entry list will be updated when the file position is zero and becomes
7e9cd9fe 1583+obsoleted. This decision is made in aufs automatically.
53392da6
AM
1584+
1585+The dynamically allocated memory block for the name of entries has a
1586+unit of 512 bytes (by default) and stores the names contiguously (no
1587+padding). Another block for each entry is handled by kmem_cache too.
1588+During building dir blocks, aufs creates hash list and judging whether
1589+the entry is whiteouted by its upper branch or already listed.
1590+The merged result is cached in the corresponding inode object and
1591+maintained by a customizable life-time option.
1592+
1593+Some people may call it can be a security hole or invite DoS attack
1594+since the opened and once readdir-ed dir (file object) holds its entry
1595+list and becomes a pressure for system memory. But I'd say it is similar
1596+to files under /proc or /sys. The virtual files in them also holds a
1597+memory page (generally) while they are opened. When an idea to reduce
1598+memory for them is introduced, it will be applied to aufs too.
1599+For those who really hate this situation, I've developed readdir(3)
1600+library which operates this merging in userspace. You just need to set
1601+LD_PRELOAD environment variable, and aufs will not consume no memory in
1602+kernel space for readdir(3).
1603+
1604+
1605+Workqueue
1606+----------------------------------------------------------------------
1607+Aufs sometimes requires privilege access to a branch. For instance,
1608+in copy-up/down operation. When a user process is going to make changes
1609+to a file which exists in the lower readonly branch only, and the mode
1610+of one of ancestor directories may not be writable by a user
1611+process. Here aufs copy-up the file with its ancestors and they may
1612+require privilege to set its owner/group/mode/etc.
1613+This is a typical case of a application character of aufs (see
1614+Introduction).
1615+
1616+Aufs uses workqueue synchronously for this case. It creates its own
1617+workqueue. The workqueue is a kernel thread and has privilege. Aufs
1618+passes the request to call mkdir or write (for example), and wait for
1619+its completion. This approach solves a problem of a signal handler
1620+simply.
1621+If aufs didn't adopt the workqueue and changed the privilege of the
7e9cd9fe
AM
1622+process, then the process may receive the unexpected SIGXFSZ or other
1623+signals.
53392da6
AM
1624+
1625+Also aufs uses the system global workqueue ("events" kernel thread) too
1626+for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1627+whiteout base and etc. This is unrelated to a privilege.
1628+Most of aufs operation tries acquiring a rw_semaphore for aufs
1629+superblock at the beginning, at the same time waits for the completion
1630+of all queued asynchronous tasks.
1631+
1632+
1633+Whiteout
1634+----------------------------------------------------------------------
1635+The whiteout in aufs is very similar to Unionfs's. That is represented
1636+by its filename. UnionMount takes an approach of a file mode, but I am
1637+afraid several utilities (find(1) or something) will have to support it.
1638+
1639+Basically the whiteout represents "logical deletion" which stops aufs to
1640+lookup further, but also it represents "dir is opaque" which also stop
7e9cd9fe 1641+further lookup.
53392da6
AM
1642+
1643+In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1644+In order to make several functions in a single systemcall to be
1645+revertible, aufs adopts an approach to rename a directory to a temporary
1646+unique whiteouted name.
1647+For example, in rename(2) dir where the target dir already existed, aufs
1648+renames the target dir to a temporary unique whiteouted name before the
7e9cd9fe 1649+actual rename on a branch, and then handles other actions (make it opaque,
53392da6
AM
1650+update the attributes, etc). If an error happens in these actions, aufs
1651+simply renames the whiteouted name back and returns an error. If all are
1652+succeeded, aufs registers a function to remove the whiteouted unique
1653+temporary name completely and asynchronously to the system global
1654+workqueue.
1655+
1656+
1657+Copy-up
1658+----------------------------------------------------------------------
1659+It is a well-known feature or concept.
1660+When user modifies a file on a readonly branch, aufs operate "copy-up"
1661+internally and makes change to the new file on the upper writable branch.
1662+When the trigger systemcall does not update the timestamps of the parent
1663+dir, aufs reverts it after copy-up.
c2b27bf2
AM
1664+
1665+
1666+Move-down (aufs3.9 and later)
1667+----------------------------------------------------------------------
1668+"Copy-up" is one of the essential feature in aufs. It copies a file from
1669+the lower readonly branch to the upper writable branch when a user
1670+changes something about the file.
1671+"Move-down" is an opposite action of copy-up. Basically this action is
1672+ran manually instead of automatically and internally.
076b876e
AM
1673+For desgin and implementation, aufs has to consider these issues.
1674+- whiteout for the file may exist on the lower branch.
1675+- ancestor directories may not exist on the lower branch.
1676+- diropq for the ancestor directories may exist on the upper branch.
1677+- free space on the lower branch will reduce.
1678+- another access to the file may happen during moving-down, including
7e9cd9fe 1679+ UDBA (see "Revalidate Dentry and UDBA").
076b876e
AM
1680+- the file should not be hard-linked nor pseudo-linked. they should be
1681+ handled by auplink utility later.
c2b27bf2
AM
1682+
1683+Sometimes users want to move-down a file from the upper writable branch
1684+to the lower readonly or writable branch. For instance,
1685+- the free space of the upper writable branch is going to run out.
1686+- create a new intermediate branch between the upper and lower branch.
1687+- etc.
1688+
1689+For this purpose, use "aumvdown" command in aufs-util.git.
b912730e
AM
1690diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1691--- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt 1970-01-01 01:00:00.000000000 +0100
ba1aed25 1692+++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt 2019-03-05 12:13:00.132557473 +0100
b912730e
AM
1693@@ -0,0 +1,85 @@
1694+
ba1aed25 1695+# Copyright (C) 2015-2019 Junjiro R. Okajima
b912730e
AM
1696+#
1697+# This program is free software; you can redistribute it and/or modify
1698+# it under the terms of the GNU General Public License as published by
1699+# the Free Software Foundation; either version 2 of the License, or
1700+# (at your option) any later version.
1701+#
1702+# This program is distributed in the hope that it will be useful,
1703+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1704+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1705+# GNU General Public License for more details.
1706+#
1707+# You should have received a copy of the GNU General Public License
1708+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1709+
1710+Support for a branch who has its ->atomic_open()
1711+----------------------------------------------------------------------
1712+The filesystems who implement its ->atomic_open() are not majority. For
1713+example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1714+particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1715+->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1716+sure whether all filesystems who have ->atomic_open() behave like this,
1717+but NFSv4 surely returns the error.
1718+
1719+In order to support ->atomic_open() for aufs, there are a few
1720+approaches.
1721+
1722+A. Introduce aufs_atomic_open()
1723+ - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1724+ branch fs.
1725+B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1726+ an aufs user Pip Cet's approach
1727+ - calls aufs_create(), VFS finish_open() and notify_change().
1728+ - pass fake-mode to finish_open(), and then correct the mode by
1729+ notify_change().
1730+C. Extend aufs_open() to call branch fs's ->atomic_open()
1731+ - no aufs_atomic_open().
1732+ - aufs_lookup() registers the TID to an aufs internal object.
1733+ - aufs_create() does nothing when the matching TID is registered, but
1734+ registers the mode.
1735+ - aufs_open() calls branch fs's ->atomic_open() when the matching
1736+ TID is registered.
1737+D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1738+ credential
1739+ - no aufs_atomic_open().
1740+ - aufs_create() registers the TID to an internal object. this info
1741+ represents "this process created this file just now."
1742+ - when aufs gets EACCES from branch fs's ->open(), then confirm the
1743+ registered TID and re-try open() with superuser's credential.
1744+
1745+Pros and cons for each approach.
1746+
1747+A.
1748+ - straightforward but highly depends upon VFS internal.
1749+ - the atomic behavaiour is kept.
1750+ - some of parameters such as nameidata are hard to reproduce for
1751+ branch fs.
1752+ - large overhead.
1753+B.
1754+ - easy to implement.
1755+ - the atomic behavaiour is lost.
1756+C.
1757+ - the atomic behavaiour is kept.
1758+ - dirty and tricky.
1759+ - VFS checks whether the file is created correctly after calling
1760+ ->create(), which means this approach doesn't work.
1761+D.
1762+ - easy to implement.
1763+ - the atomic behavaiour is lost.
1764+ - to open a file with superuser's credential and give it to a user
1765+ process is a bad idea, since the file object keeps the credential
1766+ in it. It may affect LSM or something. This approach doesn't work
1767+ either.
1768+
1769+The approach A is ideal, but it hard to implement. So here is a
1770+variation of A, which is to be implemented.
1771+
1772+A-1. Introduce aufs_atomic_open()
1773+ - calls branch fs ->atomic_open() if exists. otherwise calls
1774+ vfs_create() and finish_open().
1775+ - the demerit is that the several checks after branch fs
1776+ ->atomic_open() are lost. in the ordinary case, the checks are
1777+ done by VFS:do_last(), lookup_open() and atomic_open(). some can
1778+ be implemented in aufs, but not all I am afraid.
53392da6
AM
1779diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1780--- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
ba1aed25 1781+++ linux/Documentation/filesystems/aufs/design/03lookup.txt 2019-03-05 12:13:00.132557473 +0100
7e9cd9fe 1782@@ -0,0 +1,113 @@
53392da6 1783+
ba1aed25 1784+# Copyright (C) 2005-2019 Junjiro R. Okajima
53392da6
AM
1785+#
1786+# This program is free software; you can redistribute it and/or modify
1787+# it under the terms of the GNU General Public License as published by
1788+# the Free Software Foundation; either version 2 of the License, or
1789+# (at your option) any later version.
1790+#
1791+# This program is distributed in the hope that it will be useful,
1792+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1793+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1794+# GNU General Public License for more details.
1795+#
1796+# You should have received a copy of the GNU General Public License
523b37e3 1797+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1798+
1799+Lookup in a Branch
1800+----------------------------------------------------------------------
1801+Since aufs has a character of sub-VFS (see Introduction), it operates
7e9cd9fe
AM
1802+lookup for branches as VFS does. It may be a heavy work. But almost all
1803+lookup operation in aufs is the simplest case, ie. lookup only an entry
1804+directly connected to its parent. Digging down the directory hierarchy
1805+is unnecessary. VFS has a function lookup_one_len() for that use, and
1806+aufs calls it.
1807+
1808+When a branch is a remote filesystem, aufs basically relies upon its
53392da6
AM
1809+->d_revalidate(), also aufs forces the hardest revalidate tests for
1810+them.
1811+For d_revalidate, aufs implements three levels of revalidate tests. See
1812+"Revalidate Dentry and UDBA" in detail.
1813+
1814+
076b876e
AM
1815+Test Only the Highest One for the Directory Permission (dirperm1 option)
1816+----------------------------------------------------------------------
1817+Let's try case study.
1818+- aufs has two branches, upper readwrite and lower readonly.
1819+ /au = /rw + /ro
1820+- "dirA" exists under /ro, but /rw. and its mode is 0700.
1821+- user invoked "chmod a+rx /au/dirA"
1822+- the internal copy-up is activated and "/rw/dirA" is created and its
7e9cd9fe 1823+ permission bits are set to world readable.
076b876e
AM
1824+- then "/au/dirA" becomes world readable?
1825+
1826+In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1827+or it may be a natively readonly filesystem. If aufs respects the lower
1828+branch, it should not respond readdir request from other users. But user
1829+allowed it by chmod. Should really aufs rejects showing the entries
1830+under /ro/dirA?
1831+
7e9cd9fe
AM
1832+To be honest, I don't have a good solution for this case. So aufs
1833+implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1834+users.
076b876e
AM
1835+When dirperm1 is specified, aufs checks only the highest one for the
1836+directory permission, and shows the entries. Otherwise, as usual, checks
1837+every dir existing on all branches and rejects the request.
1838+
1839+As a side effect, dirperm1 option improves the performance of aufs
1840+because the number of permission check is reduced when the number of
1841+branch is many.
1842+
1843+
53392da6
AM
1844+Revalidate Dentry and UDBA (User's Direct Branch Access)
1845+----------------------------------------------------------------------
1846+Generally VFS helpers re-validate a dentry as a part of lookup.
1847+0. digging down the directory hierarchy.
1848+1. lock the parent dir by its i_mutex.
1849+2. lookup the final (child) entry.
1850+3. revalidate it.
1851+4. call the actual operation (create, unlink, etc.)
1852+5. unlock the parent dir
1853+
1854+If the filesystem implements its ->d_revalidate() (step 3), then it is
1855+called. Actually aufs implements it and checks the dentry on a branch is
1856+still valid.
1857+But it is not enough. Because aufs has to release the lock for the
1858+parent dir on a branch at the end of ->lookup() (step 2) and
1859+->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1860+held by VFS.
1861+If the file on a branch is changed directly, eg. bypassing aufs, after
1862+aufs released the lock, then the subsequent operation may cause
1863+something unpleasant result.
1864+
1865+This situation is a result of VFS architecture, ->lookup() and
1866+->d_revalidate() is separated. But I never say it is wrong. It is a good
1867+design from VFS's point of view. It is just not suitable for sub-VFS
1868+character in aufs.
1869+
1870+Aufs supports such case by three level of revalidation which is
1871+selectable by user.
1872+1. Simple Revalidate
1873+ Addition to the native flow in VFS's, confirm the child-parent
1874+ relationship on the branch just after locking the parent dir on the
1875+ branch in the "actual operation" (step 4). When this validation
1876+ fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1877+ checks the validation of the dentry on branches.
1878+2. Monitor Changes Internally by Inotify/Fsnotify
1879+ Addition to above, in the "actual operation" (step 4) aufs re-lookup
1880+ the dentry on the branch, and returns EBUSY if it finds different
1881+ dentry.
1882+ Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1883+ during it is in cache. When the event is notified, aufs registers a
1884+ function to kernel 'events' thread by schedule_work(). And the
1885+ function sets some special status to the cached aufs dentry and inode
1886+ private data. If they are not cached, then aufs has nothing to
1887+ do. When the same file is accessed through aufs (step 0-3) later,
1888+ aufs will detect the status and refresh all necessary data.
1889+ In this mode, aufs has to ignore the event which is fired by aufs
1890+ itself.
1891+3. No Extra Validation
1892+ This is the simplest test and doesn't add any additional revalidation
7e9cd9fe 1893+ test, and skip the revalidation in step 4. It is useful and improves
53392da6
AM
1894+ aufs performance when system surely hide the aufs branches from user,
1895+ by over-mounting something (or another method).
1896diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1897--- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
ba1aed25 1898+++ linux/Documentation/filesystems/aufs/design/04branch.txt 2019-03-05 12:13:00.132557473 +0100
7e9cd9fe 1899@@ -0,0 +1,74 @@
53392da6 1900+
ba1aed25 1901+# Copyright (C) 2005-2019 Junjiro R. Okajima
53392da6
AM
1902+#
1903+# This program is free software; you can redistribute it and/or modify
1904+# it under the terms of the GNU General Public License as published by
1905+# the Free Software Foundation; either version 2 of the License, or
1906+# (at your option) any later version.
1907+#
1908+# This program is distributed in the hope that it will be useful,
1909+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1910+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1911+# GNU General Public License for more details.
1912+#
1913+# You should have received a copy of the GNU General Public License
523b37e3 1914+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1915+
1916+Branch Manipulation
1917+
1918+Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1919+and changing its permission/attribute, there are a lot of works to do.
1920+
1921+
1922+Add a Branch
1923+----------------------------------------------------------------------
1924+o Confirm the adding dir exists outside of aufs, including loopback
7e9cd9fe 1925+ mount, and its various attributes.
53392da6
AM
1926+o Initialize the xino file and whiteout bases if necessary.
1927+ See struct.txt.
1928+
1929+o Check the owner/group/mode of the directory
1930+ When the owner/group/mode of the adding directory differs from the
1931+ existing branch, aufs issues a warning because it may impose a
1932+ security risk.
1933+ For example, when a upper writable branch has a world writable empty
1934+ top directory, a malicious user can create any files on the writable
1935+ branch directly, like copy-up and modify manually. If something like
1936+ /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1937+ writable branch, and the writable branch is world-writable, then a
1938+ malicious guy may create /etc/passwd on the writable branch directly
1939+ and the infected file will be valid in aufs.
7e9cd9fe 1940+ I am afraid it can be a security issue, but aufs can do nothing except
53392da6
AM
1941+ producing a warning.
1942+
1943+
1944+Delete a Branch
1945+----------------------------------------------------------------------
1946+o Confirm the deleting branch is not busy
1947+ To be general, there is one merit to adopt "remount" interface to
1948+ manipulate branches. It is to discard caches. At deleting a branch,
1949+ aufs checks the still cached (and connected) dentries and inodes. If
1950+ there are any, then they are all in-use. An inode without its
1951+ corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1952+
1953+ For the cached one, aufs checks whether the same named entry exists on
1954+ other branches.
1955+ If the cached one is a directory, because aufs provides a merged view
1956+ to users, as long as one dir is left on any branch aufs can show the
1957+ dir to users. In this case, the branch can be removed from aufs.
1958+ Otherwise aufs rejects deleting the branch.
1959+
1960+ If any file on the deleting branch is opened by aufs, then aufs
1961+ rejects deleting.
1962+
1963+
1964+Modify the Permission of a Branch
1965+----------------------------------------------------------------------
1966+o Re-initialize or remove the xino file and whiteout bases if necessary.
1967+ See struct.txt.
1968+
1969+o rw --> ro: Confirm the modifying branch is not busy
1970+ Aufs rejects the request if any of these conditions are true.
1971+ - a file on the branch is mmap-ed.
1972+ - a regular file on the branch is opened for write and there is no
1973+ same named entry on the upper branch.
1974diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1975--- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt 1970-01-01 01:00:00.000000000 +0100
ba1aed25 1976+++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt 2019-03-05 12:13:00.132557473 +0100
523b37e3 1977@@ -0,0 +1,64 @@
53392da6 1978+
ba1aed25 1979+# Copyright (C) 2005-2019 Junjiro R. Okajima
53392da6
AM
1980+#
1981+# This program is free software; you can redistribute it and/or modify
1982+# it under the terms of the GNU General Public License as published by
1983+# the Free Software Foundation; either version 2 of the License, or
1984+# (at your option) any later version.
1985+#
1986+# This program is distributed in the hope that it will be useful,
1987+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1988+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1989+# GNU General Public License for more details.
1990+#
1991+# You should have received a copy of the GNU General Public License
523b37e3 1992+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1993+
1994+Policies to Select One among Multiple Writable Branches
1995+----------------------------------------------------------------------
1996+When the number of writable branch is more than one, aufs has to decide
1997+the target branch for file creation or copy-up. By default, the highest
1998+writable branch which has the parent (or ancestor) dir of the target
1999+file is chosen (top-down-parent policy).
2000+By user's request, aufs implements some other policies to select the
7e9cd9fe
AM
2001+writable branch, for file creation several policies, round-robin,
2002+most-free-space, and other policies. For copy-up, top-down-parent,
2003+bottom-up-parent, bottom-up and others.
53392da6
AM
2004+
2005+As expected, the round-robin policy selects the branch in circular. When
2006+you have two writable branches and creates 10 new files, 5 files will be
2007+created for each branch. mkdir(2) systemcall is an exception. When you
2008+create 10 new directories, all will be created on the same branch.
2009+And the most-free-space policy selects the one which has most free
2010+space among the writable branches. The amount of free space will be
2011+checked by aufs internally, and users can specify its time interval.
2012+
2013+The policies for copy-up is more simple,
2014+top-down-parent is equivalent to the same named on in create policy,
2015+bottom-up-parent selects the writable branch where the parent dir
2016+exists and the nearest upper one from the copyup-source,
2017+bottom-up selects the nearest upper writable branch from the
2018+copyup-source, regardless the existence of the parent dir.
2019+
2020+There are some rules or exceptions to apply these policies.
2021+- If there is a readonly branch above the policy-selected branch and
2022+ the parent dir is marked as opaque (a variation of whiteout), or the
2023+ target (creating) file is whiteout-ed on the upper readonly branch,
2024+ then the result of the policy is ignored and the target file will be
2025+ created on the nearest upper writable branch than the readonly branch.
2026+- If there is a writable branch above the policy-selected branch and
2027+ the parent dir is marked as opaque or the target file is whiteouted
2028+ on the branch, then the result of the policy is ignored and the target
2029+ file will be created on the highest one among the upper writable
2030+ branches who has diropq or whiteout. In case of whiteout, aufs removes
2031+ it as usual.
2032+- link(2) and rename(2) systemcalls are exceptions in every policy.
2033+ They try selecting the branch where the source exists as possible
2034+ since copyup a large file will take long time. If it can't be,
2035+ ie. the branch where the source exists is readonly, then they will
2036+ follow the copyup policy.
2037+- There is an exception for rename(2) when the target exists.
2038+ If the rename target exists, aufs compares the index of the branches
2039+ where the source and the target exists and selects the higher
2040+ one. If the selected branch is readonly, then aufs follows the
2041+ copyup policy.
8b6a4947
AM
2042diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
2043--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
b00004a5 2044+++ linux/Documentation/filesystems/aufs/design/06dirren.dot 2018-04-15 08:49:13.394483860 +0200
8b6a4947
AM
2045@@ -0,0 +1,31 @@
2046+
2047+// to view this graph, run dot(1) command in GRAPHVIZ.
2048+
2049+digraph G {
2050+node [shape=box];
2051+whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
2052+
2053+node [shape=oval];
2054+
2055+aufs_rename -> whinfo [label="store/remove"];
2056+
2057+node [shape=oval];
2058+inode_list [label="h_inum list in branch\ncache"];
2059+
2060+node [shape=box];
2061+whinode [label="h_inum list file"];
2062+
2063+node [shape=oval];
2064+brmgmt [label="br_add/del/mod/umount"];
2065+
2066+brmgmt -> inode_list [label="create/remove"];
2067+brmgmt -> whinode [label="load/store"];
2068+
2069+inode_list -> whinode [style=dashed,dir=both];
2070+
2071+aufs_rename -> inode_list [label="add/del"];
2072+
2073+aufs_lookup -> inode_list [label="search"];
2074+
2075+aufs_lookup -> whinfo [label="load/remove"];
2076+}
2077diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
2078--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
ba1aed25 2079+++ linux/Documentation/filesystems/aufs/design/06dirren.txt 2019-03-05 12:13:00.132557473 +0100
8b6a4947
AM
2080@@ -0,0 +1,102 @@
2081+
ba1aed25 2082+# Copyright (C) 2017-2019 Junjiro R. Okajima
8b6a4947
AM
2083+#
2084+# This program is free software; you can redistribute it and/or modify
2085+# it under the terms of the GNU General Public License as published by
2086+# the Free Software Foundation; either version 2 of the License, or
2087+# (at your option) any later version.
2088+#
2089+# This program is distributed in the hope that it will be useful,
2090+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2091+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2092+# GNU General Public License for more details.
2093+#
2094+# You should have received a copy of the GNU General Public License
2095+# along with this program. If not, see <http://www.gnu.org/licenses/>.
2096+
2097+Special handling for renaming a directory (DIRREN)
2098+----------------------------------------------------------------------
2099+First, let's assume we have a simple usecase.
2100+
2101+- /u = /rw + /ro
2102+- /rw/dirA exists
2103+- /ro/dirA and /ro/dirA/file exist too
2104+- there is no dirB on both branches
2105+- a user issues rename("dirA", "dirB")
2106+
2107+Now, what should aufs behave against this rename(2)?
2108+There are a few possible cases.
2109+
2110+A. returns EROFS.
2111+ since dirA exists on a readonly branch which cannot be renamed.
2112+B. returns EXDEV.
2113+ it is possible to copy-up dirA (only the dir itself), but the child
2114+ entries ("file" in this case) should not be. it must be a bad
2115+ approach to copy-up recursively.
2116+C. returns a success.
2117+ even the branch /ro is readonly, aufs tries renaming it. Obviously it
2118+ is a violation of aufs' policy.
2119+D. construct an extra information which indicates that /ro/dirA should
2120+ be handled as the name of dirB.
2121+ overlayfs has a similar feature called REDIRECT.
2122+
2123+Until now, aufs implements the case B only which returns EXDEV, and
2124+expects the userspace application behaves like mv(1) which tries
2125+issueing rename(2) recursively.
2126+
2127+A new aufs feature called DIRREN is introduced which implements the case
2128+D. There are several "extra information" added.
2129+
2130+1. detailed info per renamed directory
2131+ path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
2132+2. the inode-number list of directories on a branch
2133+ path: /rw/dirB/$AUFS_WH_DR_BRHINO
2134+
2135+The filename of "detailed info per directory" represents the lower
2136+branch, and its format is
2137+- a type of the branch id
2138+ one of these.
2139+ + uuid (not implemented yet)
2140+ + fsid
2141+ + dev
2142+- the inode-number of the branch root dir
2143+
2144+And it contains these info in a single regular file.
2145+- magic number
2146+- branch's inode-number of the logically renamed dir
2147+- the name of the before-renamed dir
2148+
2149+The "detailed info per directory" file is created in aufs rename(2), and
2150+loaded in any lookup.
2151+The info is considered in lookup for the matching case only. Here
2152+"matching" means that the root of branch (in the info filename) is same
2153+to the current looking-up branch. After looking-up the before-renamed
2154+name, the inode-number is compared. And the matched dentry is used.
2155+
2156+The "inode-number list of directories" is a regular file which contains
2157+simply the inode-numbers on the branch. The file is created or updated
2158+in removing the branch, and loaded in adding the branch. Its lifetime is
2159+equal to the branch.
2160+The list is refered in lookup, and when the current target inode is
2161+found in the list, the aufs tries loading the "detailed info per
2162+directory" and get the changed and valid name of the dir.
2163+
2164+Theoretically these "extra informaiton" may be able to be put into XATTR
2165+in the dir inode. But aufs doesn't choose this way because
2166+1. XATTR may not be supported by the branch (or its configuration)
2167+2. XATTR may have its size limit.
2168+3. XATTR may be less easy to convert than a regular file, when the
2169+ format of the info is changed in the future.
2170+At the same time, I agree that the regular file approach is much slower
2171+than XATTR approach. So, in the future, aufs may take the XATTR or other
2172+better approach.
2173+
2174+This DIRREN feature is enabled by aufs configuration, and is activated
2175+by a new mount option.
2176+
2177+For the more complicated case, there is a work with UDBA option, which
2178+is to dected the direct access to the branches (by-passing aufs) and to
2179+maintain the cashes in aufs. Since a single cached aufs dentry may
2180+contains two names, before- and after-rename, the name comparision in
2181+UDBA handler may not work correctly. In this case, the behaviour will be
2182+equivalen to udba=reval case.
076b876e
AM
2183diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
2184--- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt 1970-01-01 01:00:00.000000000 +0100
ba1aed25 2185+++ linux/Documentation/filesystems/aufs/design/06fhsm.txt 2019-03-05 12:13:00.132557473 +0100
076b876e
AM
2186@@ -0,0 +1,120 @@
2187+
ba1aed25 2188+# Copyright (C) 2011-2019 Junjiro R. Okajima
076b876e
AM
2189+#
2190+# This program is free software; you can redistribute it and/or modify
2191+# it under the terms of the GNU General Public License as published by
2192+# the Free Software Foundation; either version 2 of the License, or
2193+# (at your option) any later version.
2194+#
2195+# This program is distributed in the hope that it will be useful,
2196+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2197+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2198+# GNU General Public License for more details.
2199+#
2200+# You should have received a copy of the GNU General Public License
2201+# along with this program; if not, write to the Free Software
2202+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2203+
2204+
2205+File-based Hierarchical Storage Management (FHSM)
2206+----------------------------------------------------------------------
2207+Hierarchical Storage Management (or HSM) is a well-known feature in the
2208+storage world. Aufs provides this feature as file-based with multiple
7e9cd9fe 2209+writable branches, based upon the principle of "Colder, the Lower".
076b876e 2210+Here the word "colder" means that the less used files, and "lower" means
7e9cd9fe 2211+that the position in the order of the stacked branches vertically.
076b876e
AM
2212+These multiple writable branches are prioritized, ie. the topmost one
2213+should be the fastest drive and be used heavily.
2214+
2215+o Characters in aufs FHSM story
2216+- aufs itself and a new branch attribute.
2217+- a new ioctl interface to move-down and to establish a connection with
2218+ the daemon ("move-down" is a converse of "copy-up").
2219+- userspace tool and daemon.
2220+
2221+The userspace daemon establishes a connection with aufs and waits for
2222+the notification. The notified information is very similar to struct
2223+statfs containing the number of consumed blocks and inodes.
2224+When the consumed blocks/inodes of a branch exceeds the user-specified
2225+upper watermark, the daemon activates its move-down process until the
2226+consumed blocks/inodes reaches the user-specified lower watermark.
2227+
2228+The actual move-down is done by aufs based upon the request from
2229+user-space since we need to maintain the inode number and the internal
2230+pointer arrays in aufs.
2231+
2232+Currently aufs FHSM handles the regular files only. Additionally they
2233+must not be hard-linked nor pseudo-linked.
2234+
2235+
2236+o Cowork of aufs and the user-space daemon
2237+ During the userspace daemon established the connection, aufs sends a
2238+ small notification to it whenever aufs writes something into the
2239+ writable branch. But it may cost high since aufs issues statfs(2)
2240+ internally. So user can specify a new option to cache the
2241+ info. Actually the notification is controlled by these factors.
2242+ + the specified cache time.
2243+ + classified as "force" by aufs internally.
2244+ Until the specified time expires, aufs doesn't send the info
2245+ except the forced cases. When aufs decide forcing, the info is always
2246+ notified to userspace.
2247+ For example, the number of free inodes is generally large enough and
2248+ the shortage of it happens rarely. So aufs doesn't force the
2249+ notification when creating a new file, directory and others. This is
2250+ the typical case which aufs doesn't force.
2251+ When aufs writes the actual filedata and the files consumes any of new
2252+ blocks, the aufs forces notifying.
2253+
2254+
2255+o Interfaces in aufs
2256+- New branch attribute.
2257+ + fhsm
2258+ Specifies that the branch is managed by FHSM feature. In other word,
2259+ participant in the FHSM.
2260+ When nofhsm is set to the branch, it will not be the source/target
2261+ branch of the move-down operation. This attribute is set
2262+ independently from coo and moo attributes, and if you want full
2263+ FHSM, you should specify them as well.
2264+- New mount option.
2265+ + fhsm_sec
2266+ Specifies a second to suppress many less important info to be
2267+ notified.
2268+- New ioctl.
2269+ + AUFS_CTL_FHSM_FD
2270+ create a new file descriptor which userspace can read the notification
2271+ (a subset of struct statfs) from aufs.
2272+- Module parameter 'brs'
2273+ It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2274+ be set.
2275+- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2276+ When there are two or more branches with fhsm attributes,
2277+ /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2278+ terminates it. As a result of remounting and branch-manipulation, the
2279+ number of branches with fhsm attribute can be one. In this case,
2280+ /sbin/mount.aufs will terminate the user-space daemon.
2281+
2282+
2283+Finally the operation is done as these steps in kernel-space.
2284+- make sure that,
2285+ + no one else is using the file.
2286+ + the file is not hard-linked.
2287+ + the file is not pseudo-linked.
2288+ + the file is a regular file.
2289+ + the parent dir is not opaqued.
2290+- find the target writable branch.
2291+- make sure the file is not whiteout-ed by the upper (than the target)
2292+ branch.
2293+- make the parent dir on the target branch.
2294+- mutex lock the inode on the branch.
2295+- unlink the whiteout on the target branch (if exists).
2296+- lookup and create the whiteout-ed temporary name on the target branch.
2297+- copy the file as the whiteout-ed temporary name on the target branch.
2298+- rename the whiteout-ed temporary name to the original name.
2299+- unlink the file on the source branch.
2300+- maintain the internal pointer array and the external inode number
2301+ table (XINO).
2302+- maintain the timestamps and other attributes of the parent dir and the
2303+ file.
2304+
2305+And of course, in every step, an error may happen. So the operation
2306+should restore the original file state after an error happens.
53392da6
AM
2307diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2308--- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt 1970-01-01 01:00:00.000000000 +0100
ba1aed25 2309+++ linux/Documentation/filesystems/aufs/design/06mmap.txt 2019-03-05 12:13:00.132557473 +0100
b912730e 2310@@ -0,0 +1,72 @@
53392da6 2311+
ba1aed25 2312+# Copyright (C) 2005-2019 Junjiro R. Okajima
53392da6
AM
2313+#
2314+# This program is free software; you can redistribute it and/or modify
2315+# it under the terms of the GNU General Public License as published by
2316+# the Free Software Foundation; either version 2 of the License, or
2317+# (at your option) any later version.
2318+#
2319+# This program is distributed in the hope that it will be useful,
2320+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2321+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2322+# GNU General Public License for more details.
2323+#
2324+# You should have received a copy of the GNU General Public License
523b37e3 2325+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2326+
2327+mmap(2) -- File Memory Mapping
2328+----------------------------------------------------------------------
2329+In aufs, the file-mapped pages are handled by a branch fs directly, no
2330+interaction with aufs. It means aufs_mmap() calls the branch fs's
2331+->mmap().
2332+This approach is simple and good, but there is one problem.
7e9cd9fe 2333+Under /proc, several entries show the mmapped files by its path (with
53392da6
AM
2334+device and inode number), and the printed path will be the path on the
2335+branch fs's instead of virtual aufs's.
2336+This is not a problem in most cases, but some utilities lsof(1) (and its
2337+user) may expect the path on aufs.
2338+
2339+To address this issue, aufs adds a new member called vm_prfile in struct
2340+vm_area_struct (and struct vm_region). The original vm_file points to
2341+the file on the branch fs in order to handle everything correctly as
2342+usual. The new vm_prfile points to a virtual file in aufs, and the
2343+show-functions in procfs refers to vm_prfile if it is set.
2344+Also we need to maintain several other places where touching vm_file
2345+such like
2346+- fork()/clone() copies vma and the reference count of vm_file is
2347+ incremented.
2348+- merging vma maintains the ref count too.
2349+
7e9cd9fe 2350+This is not a good approach. It just fakes the printed path. But it
53392da6
AM
2351+leaves all behaviour around f_mapping unchanged. This is surely an
2352+advantage.
2353+Actually aufs had adopted another complicated approach which calls
2354+generic_file_mmap() and handles struct vm_operations_struct. In this
2355+approach, aufs met a hard problem and I could not solve it without
2356+switching the approach.
b912730e
AM
2357+
2358+There may be one more another approach which is
2359+- bind-mount the branch-root onto the aufs-root internally
2360+- grab the new vfsmount (ie. struct mount)
2361+- lazy-umount the branch-root internally
2362+- in open(2) the aufs-file, open the branch-file with the hidden
2363+ vfsmount (instead of the original branch's vfsmount)
2364+- ideally this "bind-mount and lazy-umount" should be done atomically,
2365+ but it may be possible from userspace by the mount helper.
2366+
2367+Adding the internal hidden vfsmount and using it in opening a file, the
2368+file path under /proc will be printed correctly. This approach looks
2369+smarter, but is not possible I am afraid.
2370+- aufs-root may be bind-mount later. when it happens, another hidden
2371+ vfsmount will be required.
2372+- it is hard to get the chance to bind-mount and lazy-umount
2373+ + in kernel-space, FS can have vfsmount in open(2) via
2374+ file->f_path, and aufs can know its vfsmount. But several locks are
2375+ already acquired, and if aufs tries to bind-mount and lazy-umount
2376+ here, then it may cause a deadlock.
2377+ + in user-space, bind-mount doesn't invoke the mount helper.
2378+- since /proc shows dev and ino, aufs has to give vma these info. it
2379+ means a new member vm_prinode will be necessary. this is essentially
2380+ equivalent to vm_prfile described above.
2381+
2382+I have to give up this "looks-smater" approach.
c1595e42
JR
2383diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2384--- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt 1970-01-01 01:00:00.000000000 +0100
ba1aed25 2385+++ linux/Documentation/filesystems/aufs/design/06xattr.txt 2019-03-05 12:13:00.132557473 +0100
c1595e42
JR
2386@@ -0,0 +1,96 @@
2387+
ba1aed25 2388+# Copyright (C) 2014-2019 Junjiro R. Okajima
c1595e42
JR
2389+#
2390+# This program is free software; you can redistribute it and/or modify
2391+# it under the terms of the GNU General Public License as published by
2392+# the Free Software Foundation; either version 2 of the License, or
2393+# (at your option) any later version.
2394+#
2395+# This program is distributed in the hope that it will be useful,
2396+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2397+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2398+# GNU General Public License for more details.
2399+#
2400+# You should have received a copy of the GNU General Public License
2401+# along with this program; if not, write to the Free Software
2402+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2403+
2404+
2405+Listing XATTR/EA and getting the value
2406+----------------------------------------------------------------------
2407+For the inode standard attributes (owner, group, timestamps, etc.), aufs
2408+shows the values from the topmost existing file. This behaviour is good
7e9cd9fe 2409+for the non-dir entries since the bahaviour exactly matches the shown
c1595e42
JR
2410+information. But for the directories, aufs considers all the same named
2411+entries on the lower branches. Which means, if one of the lower entry
2412+rejects readdir call, then aufs returns an error even if the topmost
2413+entry allows it. This behaviour is necessary to respect the branch fs's
2414+security, but can make users confused since the user-visible standard
2415+attributes don't match the behaviour.
2416+To address this issue, aufs has a mount option called dirperm1 which
2417+checks the permission for the topmost entry only, and ignores the lower
2418+entry's permission.
2419+
2420+A similar issue can happen around XATTR.
2421+getxattr(2) and listxattr(2) families behave as if dirperm1 option is
7e9cd9fe
AM
2422+always set. Otherwise these very unpleasant situation would happen.
2423+- listxattr(2) may return the duplicated entries.
c1595e42
JR
2424+- users may not be able to remove or reset the XATTR forever,
2425+
2426+
2427+XATTR/EA support in the internal (copy,move)-(up,down)
2428+----------------------------------------------------------------------
7e9cd9fe 2429+Generally the extended attributes of inode are categorized as these.
c1595e42
JR
2430+- "security" for LSM and capability.
2431+- "system" for posix ACL, 'acl' mount option is required for the branch
2432+ fs generally.
2433+- "trusted" for userspace, CAP_SYS_ADMIN is required.
2434+- "user" for userspace, 'user_xattr' mount option is required for the
2435+ branch fs generally.
2436+
2437+Moreover there are some other categories. Aufs handles these rather
2438+unpopular categories as the ordinary ones, ie. there is no special
2439+condition nor exception.
2440+
2441+In copy-up, the support for XATTR on the dst branch may differ from the
2442+src branch. In this case, the copy-up operation will get an error and
7e9cd9fe
AM
2443+the original user operation which triggered the copy-up will fail. It
2444+can happen that even all copy-up will fail.
c1595e42
JR
2445+When both of src and dst branches support XATTR and if an error occurs
2446+during copying XATTR, then the copy-up should fail obviously. That is a
2447+good reason and aufs should return an error to userspace. But when only
7e9cd9fe 2448+the src branch support that XATTR, aufs should not return an error.
c1595e42
JR
2449+For example, the src branch supports ACL but the dst branch doesn't
2450+because the dst branch may natively un-support it or temporary
2451+un-support it due to "noacl" mount option. Of course, the dst branch fs
2452+may NOT return an error even if the XATTR is not supported. It is
2453+totally up to the branch fs.
2454+
2455+Anyway when the aufs internal copy-up gets an error from the dst branch
2456+fs, then aufs tries removing the just copied entry and returns the error
2457+to the userspace. The worst case of this situation will be all copy-up
2458+will fail.
2459+
2460+For the copy-up operation, there two basic approaches.
2461+- copy the specified XATTR only (by category above), and return the
7e9cd9fe 2462+ error unconditionally if it happens.
c1595e42
JR
2463+- copy all XATTR, and ignore the error on the specified category only.
2464+
2465+In order to support XATTR and to implement the correct behaviour, aufs
7e9cd9fe
AM
2466+chooses the latter approach and introduces some new branch attributes,
2467+"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
c1595e42 2468+They correspond to the XATTR namespaces (see above). Additionally, to be
7e9cd9fe
AM
2469+convenient, "icex" is also provided which means all "icex*" attributes
2470+are set (here the word "icex" stands for "ignore copy-error on XATTR").
c1595e42
JR
2471+
2472+The meaning of these attributes is to ignore the error from setting
2473+XATTR on that branch.
2474+Note that aufs tries copying all XATTR unconditionally, and ignores the
2475+error from the dst branch according to the specified attributes.
2476+
2477+Some XATTR may have its default value. The default value may come from
2478+the parent dir or the environment. If the default value is set at the
2479+file creating-time, it will be overwritten by copy-up.
2480+Some contradiction may happen I am afraid.
2481+Do we need another attribute to stop copying XATTR? I am unsure. For
2482+now, aufs implements the branch attributes to ignore the error.
53392da6
AM
2483diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2484--- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
ba1aed25 2485+++ linux/Documentation/filesystems/aufs/design/07export.txt 2019-03-05 12:13:00.132557473 +0100
523b37e3 2486@@ -0,0 +1,58 @@
53392da6 2487+
ba1aed25 2488+# Copyright (C) 2005-2019 Junjiro R. Okajima
53392da6
AM
2489+#
2490+# This program is free software; you can redistribute it and/or modify
2491+# it under the terms of the GNU General Public License as published by
2492+# the Free Software Foundation; either version 2 of the License, or
2493+# (at your option) any later version.
2494+#
2495+# This program is distributed in the hope that it will be useful,
2496+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2497+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2498+# GNU General Public License for more details.
2499+#
2500+# You should have received a copy of the GNU General Public License
523b37e3 2501+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2502+
2503+Export Aufs via NFS
2504+----------------------------------------------------------------------
2505+Here is an approach.
2506+- like xino/xib, add a new file 'xigen' which stores aufs inode
2507+ generation.
2508+- iget_locked(): initialize aufs inode generation for a new inode, and
2509+ store it in xigen file.
2510+- destroy_inode(): increment aufs inode generation and store it in xigen
2511+ file. it is necessary even if it is not unlinked, because any data of
2512+ inode may be changed by UDBA.
2513+- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2514+ build file handle by
2515+ + branch id (4 bytes)
2516+ + superblock generation (4 bytes)
2517+ + inode number (4 or 8 bytes)
2518+ + parent dir inode number (4 or 8 bytes)
2519+ + inode generation (4 bytes))
2520+ + return value of exportfs_encode_fh() for the parent on a branch (4
2521+ bytes)
2522+ + file handle for a branch (by exportfs_encode_fh())
2523+- fh_to_dentry():
2524+ + find the index of a branch from its id in handle, and check it is
2525+ still exist in aufs.
2526+ + 1st level: get the inode number from handle and search it in cache.
7e9cd9fe
AM
2527+ + 2nd level: if not found in cache, get the parent inode number from
2528+ the handle and search it in cache. and then open the found parent
2529+ dir, find the matching inode number by vfs_readdir() and get its
2530+ name, and call lookup_one_len() for the target dentry.
53392da6
AM
2531+ + 3rd level: if the parent dir is not cached, call
2532+ exportfs_decode_fh() for a branch and get the parent on a branch,
2533+ build a pathname of it, convert it a pathname in aufs, call
2534+ path_lookup(). now aufs gets a parent dir dentry, then handle it as
2535+ the 2nd level.
2536+ + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2537+ for every branch, but not itself. to get this, (currently) aufs
2538+ searches in current->nsproxy->mnt_ns list. it may not be a good
2539+ idea, but I didn't get other approach.
2540+ + test the generation of the gotten inode.
2541+- every inode operation: they may get EBUSY due to UDBA. in this case,
2542+ convert it into ESTALE for NFSD.
2543+- readdir(): call lockdep_on/off() because filldir in NFSD calls
2544+ lookup_one_len(), vfs_getattr(), encode_fh() and others.
2545diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2546--- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt 1970-01-01 01:00:00.000000000 +0100
ba1aed25 2547+++ linux/Documentation/filesystems/aufs/design/08shwh.txt 2019-03-05 12:13:00.135890907 +0100
523b37e3 2548@@ -0,0 +1,52 @@
53392da6 2549+
ba1aed25 2550+# Copyright (C) 2005-2019 Junjiro R. Okajima
53392da6
AM
2551+#
2552+# This program is free software; you can redistribute it and/or modify
2553+# it under the terms of the GNU General Public License as published by
2554+# the Free Software Foundation; either version 2 of the License, or
2555+# (at your option) any later version.
2556+#
2557+# This program is distributed in the hope that it will be useful,
2558+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2559+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2560+# GNU General Public License for more details.
2561+#
2562+# You should have received a copy of the GNU General Public License
523b37e3 2563+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2564+
2565+Show Whiteout Mode (shwh)
2566+----------------------------------------------------------------------
2567+Generally aufs hides the name of whiteouts. But in some cases, to show
2568+them is very useful for users. For instance, creating a new middle layer
2569+(branch) by merging existing layers.
2570+
2571+(borrowing aufs1 HOW-TO from a user, Michael Towers)
2572+When you have three branches,
2573+- Bottom: 'system', squashfs (underlying base system), read-only
2574+- Middle: 'mods', squashfs, read-only
2575+- Top: 'overlay', ram (tmpfs), read-write
2576+
2577+The top layer is loaded at boot time and saved at shutdown, to preserve
2578+the changes made to the system during the session.
2579+When larger changes have been made, or smaller changes have accumulated,
2580+the size of the saved top layer data grows. At this point, it would be
2581+nice to be able to merge the two overlay branches ('mods' and 'overlay')
2582+and rewrite the 'mods' squashfs, clearing the top layer and thus
2583+restoring save and load speed.
2584+
2585+This merging is simplified by the use of another aufs mount, of just the
2586+two overlay branches using the 'shwh' option.
2587+# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2588+ aufs /livesys/merge_union
2589+
2590+A merged view of these two branches is then available at
2591+/livesys/merge_union, and the new feature is that the whiteouts are
2592+visible!
2593+Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2594+writing to all branches. Also the default mode for all branches is 'ro'.
2595+It is now possible to save the combined contents of the two overlay
2596+branches to a new squashfs, e.g.:
2597+# mksquashfs /livesys/merge_union /path/to/newmods.squash
2598+
2599+This new squashfs archive can be stored on the boot device and the
2600+initramfs will use it to replace the old one at the next boot.
2601diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2602--- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt 1970-01-01 01:00:00.000000000 +0100
ba1aed25 2603+++ linux/Documentation/filesystems/aufs/design/10dynop.txt 2019-03-05 12:13:00.135890907 +0100
7e9cd9fe 2604@@ -0,0 +1,47 @@
53392da6 2605+
ba1aed25 2606+# Copyright (C) 2010-2019 Junjiro R. Okajima
53392da6
AM
2607+#
2608+# This program is free software; you can redistribute it and/or modify
2609+# it under the terms of the GNU General Public License as published by
2610+# the Free Software Foundation; either version 2 of the License, or
2611+# (at your option) any later version.
2612+#
2613+# This program is distributed in the hope that it will be useful,
2614+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2615+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2616+# GNU General Public License for more details.
2617+#
2618+# You should have received a copy of the GNU General Public License
523b37e3 2619+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2620+
2621+Dynamically customizable FS operations
2622+----------------------------------------------------------------------
2623+Generally FS operations (struct inode_operations, struct
2624+address_space_operations, struct file_operations, etc.) are defined as
2625+"static const", but it never means that FS have only one set of
2626+operation. Some FS have multiple sets of them. For instance, ext2 has
2627+three sets, one for XIP, for NOBH, and for normal.
2628+Since aufs overrides and redirects these operations, sometimes aufs has
7e9cd9fe 2629+to change its behaviour according to the branch FS type. More importantly
53392da6
AM
2630+VFS acts differently if a function (member in the struct) is set or
2631+not. It means aufs should have several sets of operations and select one
2632+among them according to the branch FS definition.
2633+
7e9cd9fe 2634+In order to solve this problem and not to affect the behaviour of VFS,
53392da6 2635+aufs defines these operations dynamically. For instance, aufs defines
7e9cd9fe
AM
2636+dummy direct_IO function for struct address_space_operations, but it may
2637+not be set to the address_space_operations actually. When the branch FS
2638+doesn't have it, aufs doesn't set it to its address_space_operations
2639+while the function definition itself is still alive. So the behaviour
2640+itself will not change, and it will return an error when direct_IO is
2641+not set.
53392da6
AM
2642+
2643+The lifetime of these dynamically generated operation object is
2644+maintained by aufs branch object. When the branch is removed from aufs,
2645+the reference counter of the object is decremented. When it reaches
2646+zero, the dynamically generated operation object will be freed.
2647+
7e9cd9fe
AM
2648+This approach is designed to support AIO (io_submit), Direct I/O and
2649+XIP (DAX) mainly.
2650+Currently this approach is applied to address_space_operations for
2651+regular files only.
53392da6
AM
2652diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2653--- /usr/share/empty/Documentation/filesystems/aufs/README 1970-01-01 01:00:00.000000000 +0100
9f237c51
AM
2654+++ linux/Documentation/filesystems/aufs/README 2018-12-27 13:19:17.705082621 +0100
2655@@ -0,0 +1,395 @@
53392da6 2656+
5527c038 2657+Aufs4 -- advanced multi layered unification filesystem version 4.x
53392da6
AM
2658+http://aufs.sf.net
2659+Junjiro R. Okajima
2660+
2661+
2662+0. Introduction
2663+----------------------------------------
2664+In the early days, aufs was entirely re-designed and re-implemented
7e9cd9fe 2665+Unionfs Version 1.x series. Adding many original ideas, approaches,
53392da6
AM
2666+improvements and implementations, it becomes totally different from
2667+Unionfs while keeping the basic features.
2668+Recently, Unionfs Version 2.x series begin taking some of the same
2669+approaches to aufs1's.
2670+Unionfs is being developed by Professor Erez Zadok at Stony Brook
2671+University and his team.
2672+
5527c038 2673+Aufs4 supports linux-4.0 and later, and for linux-3.x series try aufs3.
53392da6
AM
2674+If you want older kernel version support, try aufs2-2.6.git or
2675+aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
2676+
2677+Note: it becomes clear that "Aufs was rejected. Let's give it up."
38d290e6
JR
2678+ According to Christoph Hellwig, linux rejects all union-type
2679+ filesystems but UnionMount.
53392da6
AM
2680+<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2681+
38d290e6
JR
2682+PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2683+ UnionMount, and he pointed out an issue around a directory mutex
2684+ lock and aufs addressed it. But it is still unsure whether aufs will
2685+ be merged (or any other union solution).
076b876e 2686+<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
38d290e6 2687+
53392da6
AM
2688+
2689+1. Features
2690+----------------------------------------
2691+- unite several directories into a single virtual filesystem. The member
2692+ directory is called as a branch.
2693+- you can specify the permission flags to the branch, which are 'readonly',
2694+ 'readwrite' and 'whiteout-able.'
2695+- by upper writable branch, internal copyup and whiteout, files/dirs on
2696+ readonly branch are modifiable logically.
2697+- dynamic branch manipulation, add, del.
2698+- etc...
2699+
7e9cd9fe
AM
2700+Also there are many enhancements in aufs, such as:
2701+- test only the highest one for the directory permission (dirperm1)
2702+- copyup on open (coo=)
2703+- 'move' policy for copy-up between two writable branches, after
2704+ checking free space.
2705+- xattr, acl
53392da6
AM
2706+- readdir(3) in userspace.
2707+- keep inode number by external inode number table
2708+- keep the timestamps of file/dir in internal copyup operation
2709+- seekable directory, supporting NFS readdir.
2710+- whiteout is hardlinked in order to reduce the consumption of inodes
2711+ on branch
2712+- do not copyup, nor create a whiteout when it is unnecessary
2713+- revert a single systemcall when an error occurs in aufs
2714+- remount interface instead of ioctl
2715+- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2716+- loopback mounted filesystem as a branch
2717+- kernel thread for removing the dir who has a plenty of whiteouts
2718+- support copyup sparse file (a file which has a 'hole' in it)
2719+- default permission flags for branches
2720+- selectable permission flags for ro branch, whether whiteout can
2721+ exist or not
2722+- export via NFS.
2723+- support <sysfs>/fs/aufs and <debugfs>/aufs.
2724+- support multiple writable branches, some policies to select one
2725+ among multiple writable branches.
2726+- a new semantics for link(2) and rename(2) to support multiple
2727+ writable branches.
2728+- no glibc changes are required.
2729+- pseudo hardlink (hardlink over branches)
2730+- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2731+ including NFS or remote filesystem branch.
2732+- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2733+- and more...
2734+
5527c038 2735+Currently these features are dropped temporary from aufs4.
53392da6 2736+See design/08plan.txt in detail.
53392da6
AM
2737+- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2738+ (robr)
2739+- statistics of aufs thread (/sys/fs/aufs/stat)
53392da6
AM
2740+
2741+Features or just an idea in the future (see also design/*.txt),
2742+- reorder the branch index without del/re-add.
2743+- permanent xino files for NFSD
2744+- an option for refreshing the opened files after add/del branches
53392da6
AM
2745+- light version, without branch manipulation. (unnecessary?)
2746+- copyup in userspace
2747+- inotify in userspace
2748+- readv/writev
53392da6
AM
2749+
2750+
2751+2. Download
2752+----------------------------------------
5527c038
JR
2753+There are three GIT trees for aufs4, aufs4-linux.git,
2754+aufs4-standalone.git, and aufs-util.git. Note that there is no "4" in
1e00d052 2755+"aufs-util.git."
5527c038
JR
2756+While the aufs-util is always necessary, you need either of aufs4-linux
2757+or aufs4-standalone.
1e00d052 2758+
5527c038 2759+The aufs4-linux tree includes the whole linux mainline GIT tree,
1e00d052
AM
2760+git://git.kernel.org/.../torvalds/linux.git.
2761+And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
5527c038 2762+build aufs4 as an external kernel module.
2000de60 2763+Several extra patches are not included in this tree. Only
be52b249 2764+aufs4-standalone tree contains them. They are described in the later
2000de60 2765+section "Configuration and Compilation."
1e00d052 2766+
5527c038 2767+On the other hand, the aufs4-standalone tree has only aufs source files
53392da6 2768+and necessary patches, and you can select CONFIG_AUFS_FS=m.
2000de60 2769+But you need to apply all aufs patches manually.
53392da6 2770+
5527c038
JR
2771+You will find GIT branches whose name is in form of "aufs4.x" where "x"
2772+represents the linux kernel version, "linux-4.x". For instance,
2773+"aufs4.0" is for linux-4.0. For latest "linux-4.x-rcN", use
2774+"aufs4.x-rcN" branch.
1e00d052 2775+
5527c038 2776+o aufs4-linux tree
1e00d052 2777+$ git clone --reference /your/linux/git/tree \
5527c038 2778+ git://github.com/sfjro/aufs4-linux.git aufs4-linux.git
1e00d052 2779+- if you don't have linux GIT tree, then remove "--reference ..."
5527c038
JR
2780+$ cd aufs4-linux.git
2781+$ git checkout origin/aufs4.0
53392da6 2782+
2000de60
JR
2783+Or You may want to directly git-pull aufs into your linux GIT tree, and
2784+leave the patch-work to GIT.
2785+$ cd /your/linux/git/tree
5527c038
JR
2786+$ git remote add aufs4 git://github.com/sfjro/aufs4-linux.git
2787+$ git fetch aufs4
2788+$ git checkout -b my4.0 v4.0
2789+$ (add your local change...)
2790+$ git pull aufs4 aufs4.0
2791+- now you have v4.0 + your_changes + aufs4.0 in you my4.0 branch.
2000de60 2792+- you may need to solve some conflicts between your_changes and
5527c038
JR
2793+ aufs4.0. in this case, git-rerere is recommended so that you can
2794+ solve the similar conflicts automatically when you upgrade to 4.1 or
2000de60
JR
2795+ later in the future.
2796+
5527c038
JR
2797+o aufs4-standalone tree
2798+$ git clone git://github.com/sfjro/aufs4-standalone.git aufs4-standalone.git
2799+$ cd aufs4-standalone.git
2800+$ git checkout origin/aufs4.0
53392da6
AM
2801+
2802+o aufs-util tree
5527c038
JR
2803+$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2804+- note that the public aufs-util.git is on SourceForge instead of
2805+ GitHUB.
53392da6 2806+$ cd aufs-util.git
5527c038 2807+$ git checkout origin/aufs4.0
53392da6 2808+
5527c038
JR
2809+Note: The 4.x-rcN branch is to be used with `rc' kernel versions ONLY.
2810+The minor version number, 'x' in '4.x', of aufs may not always
9dbd164d
AM
2811+follow the minor version number of the kernel.
2812+Because changes in the kernel that cause the use of a new
2813+minor version number do not always require changes to aufs-util.
2814+
2815+Since aufs-util has its own minor version number, you may not be
2816+able to find a GIT branch in aufs-util for your kernel's
2817+exact minor version number.
2818+In this case, you should git-checkout the branch for the
53392da6 2819+nearest lower number.
9dbd164d
AM
2820+
2821+For (an unreleased) example:
5527c038
JR
2822+If you are using "linux-4.10" and the "aufs4.10" branch
2823+does not exist in aufs-util repository, then "aufs4.9", "aufs4.8"
9dbd164d
AM
2824+or something numerically smaller is the branch for your kernel.
2825+
53392da6
AM
2826+Also you can view all branches by
2827+ $ git branch -a
2828+
2829+
2830+3. Configuration and Compilation
2831+----------------------------------------
2832+Make sure you have git-checkout'ed the correct branch.
2833+
5527c038 2834+For aufs4-linux tree,
c06a8ce3 2835+- enable CONFIG_AUFS_FS.
1e00d052
AM
2836+- set other aufs configurations if necessary.
2837+
5527c038 2838+For aufs4-standalone tree,
53392da6
AM
2839+There are several ways to build.
2840+
2841+1.
5527c038
JR
2842+- apply ./aufs4-kbuild.patch to your kernel source files.
2843+- apply ./aufs4-base.patch too.
2844+- apply ./aufs4-mmap.patch too.
2845+- apply ./aufs4-standalone.patch too, if you have a plan to set
2846+ CONFIG_AUFS_FS=m. otherwise you don't need ./aufs4-standalone.patch.
537831f9
AM
2847+- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2848+ kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
c06a8ce3 2849+- enable CONFIG_AUFS_FS, you can select either
53392da6
AM
2850+ =m or =y.
2851+- and build your kernel as usual.
2852+- install the built kernel.
c06a8ce3
AM
2853+ Note: Since linux-3.9, every filesystem module requires an alias
2854+ "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2855+ modules.aliases file if you set CONFIG_AUFS_FS=m.
7eafdf33
AM
2856+- install the header files too by "make headers_install" to the
2857+ directory where you specify. By default, it is $PWD/usr.
b4510431 2858+ "make help" shows a brief note for headers_install.
53392da6
AM
2859+- and reboot your system.
2860+
2861+2.
2862+- module only (CONFIG_AUFS_FS=m).
5527c038
JR
2863+- apply ./aufs4-base.patch to your kernel source files.
2864+- apply ./aufs4-mmap.patch too.
2865+- apply ./aufs4-standalone.patch too.
53392da6
AM
2866+- build your kernel, don't forget "make headers_install", and reboot.
2867+- edit ./config.mk and set other aufs configurations if necessary.
b4510431 2868+ Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
53392da6
AM
2869+ every aufs configurations.
2870+- build the module by simple "make".
c06a8ce3
AM
2871+ Note: Since linux-3.9, every filesystem module requires an alias
2872+ "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2873+ modules.aliases file.
53392da6
AM
2874+- you can specify ${KDIR} make variable which points to your kernel
2875+ source tree.
2876+- install the files
2877+ + run "make install" to install the aufs module, or copy the built
b4510431
AM
2878+ $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2879+ + run "make install_headers" (instead of headers_install) to install
2880+ the modified aufs header file (you can specify DESTDIR which is
2881+ available in aufs standalone version's Makefile only), or copy
2882+ $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2883+ you like manually. By default, the target directory is $PWD/usr.
5527c038 2884+- no need to apply aufs4-kbuild.patch, nor copying source files to your
53392da6
AM
2885+ kernel source tree.
2886+
b4510431 2887+Note: The header file aufs_type.h is necessary to build aufs-util
53392da6
AM
2888+ as well as "make headers_install" in the kernel source tree.
2889+ headers_install is subject to be forgotten, but it is essentially
2890+ necessary, not only for building aufs-util.
2891+ You may not meet problems without headers_install in some older
2892+ version though.
2893+
2894+And then,
2895+- read README in aufs-util, build and install it
9dbd164d
AM
2896+- note that your distribution may contain an obsoleted version of
2897+ aufs_type.h in /usr/include/linux or something. When you build aufs
2898+ utilities, make sure that your compiler refers the correct aufs header
2899+ file which is built by "make headers_install."
53392da6
AM
2900+- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2901+ then run "make install_ulib" too. And refer to the aufs manual in
2902+ detail.
2903+
5527c038 2904+There several other patches in aufs4-standalone.git. They are all
38d290e6 2905+optional. When you meet some problems, they will help you.
5527c038 2906+- aufs4-loopback.patch
38d290e6
JR
2907+ Supports a nested loopback mount in a branch-fs. This patch is
2908+ unnecessary until aufs produces a message like "you may want to try
2909+ another patch for loopback file".
2910+- vfs-ino.patch
2911+ Modifies a system global kernel internal function get_next_ino() in
2912+ order to stop assigning 0 for an inode-number. Not directly related to
2913+ aufs, but recommended generally.
2914+- tmpfs-idr.patch
2915+ Keeps the tmpfs inode number as the lowest value. Effective to reduce
2916+ the size of aufs XINO files for tmpfs branch. Also it prevents the
2917+ duplication of inode number, which is important for backup tools and
2918+ other utilities. When you find aufs XINO files for tmpfs branch
2919+ growing too much, try this patch.
be52b249
AM
2920+- lockdep-debug.patch
2921+ Because aufs is not only an ordinary filesystem (callee of VFS), but
2922+ also a caller of VFS functions for branch filesystems, subclassing of
2923+ the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2924+ feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2925+ need to apply this debug patch to expand several constant values.
2926+ If don't know what LOCKDEP, then you don't have apply this patch.
38d290e6 2927+
53392da6
AM
2928+
2929+4. Usage
2930+----------------------------------------
2931+At first, make sure aufs-util are installed, and please read the aufs
2932+manual, aufs.5 in aufs-util.git tree.
2933+$ man -l aufs.5
2934+
2935+And then,
2936+$ mkdir /tmp/rw /tmp/aufs
2937+# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2938+
2939+Here is another example. The result is equivalent.
2940+# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2941+ Or
2942+# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2943+# mount -o remount,append:${HOME} /tmp/aufs
2944+
2945+Then, you can see whole tree of your home dir through /tmp/aufs. If
2946+you modify a file under /tmp/aufs, the one on your home directory is
2947+not affected, instead the same named file will be newly created under
2948+/tmp/rw. And all of your modification to a file will be applied to
2949+the one under /tmp/rw. This is called the file based Copy on Write
2950+(COW) method.
2951+Aufs mount options are described in aufs.5.
2952+If you run chroot or something and make your aufs as a root directory,
2953+then you need to customize the shutdown script. See the aufs manual in
2954+detail.
2955+
2956+Additionally, there are some sample usages of aufs which are a
2957+diskless system with network booting, and LiveCD over NFS.
2958+See sample dir in CVS tree on SourceForge.
2959+
2960+
2961+5. Contact
2962+----------------------------------------
2963+When you have any problems or strange behaviour in aufs, please let me
2964+know with:
2965+- /proc/mounts (instead of the output of mount(8))
2966+- /sys/module/aufs/*
2967+- /sys/fs/aufs/* (if you have them)
2968+- /debug/aufs/* (if you have them)
2969+- linux kernel version
2970+ if your kernel is not plain, for example modified by distributor,
2971+ the url where i can download its source is necessary too.
2972+- aufs version which was printed at loading the module or booting the
2973+ system, instead of the date you downloaded.
2974+- configuration (define/undefine CONFIG_AUFS_xxx)
2975+- kernel configuration or /proc/config.gz (if you have it)
2976+- behaviour which you think to be incorrect
2977+- actual operation, reproducible one is better
2978+- mailto: aufs-users at lists.sourceforge.net
2979+
2980+Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2981+and Feature Requests) on SourceForge. Please join and write to
2982+aufs-users ML.
2983+
2984+
2985+6. Acknowledgements
2986+----------------------------------------
2987+Thanks to everyone who have tried and are using aufs, whoever
2988+have reported a bug or any feedback.
2989+
2990+Especially donators:
2991+Tomas Matejicek(slax.org) made a donation (much more than once).
2992+ Since Apr 2010, Tomas M (the author of Slax and Linux Live
2993+ scripts) is making "doubling" donations.
2994+ Unfortunately I cannot list all of the donators, but I really
b4510431 2995+ appreciate.
53392da6
AM
2996+ It ends Aug 2010, but the ordinary donation URL is still available.
2997+ <http://sourceforge.net/donate/index.php?group_id=167503>
2998+Dai Itasaka made a donation (2007/8).
2999+Chuck Smith made a donation (2008/4, 10 and 12).
3000+Henk Schoneveld made a donation (2008/9).
3001+Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
3002+Francois Dupoux made a donation (2008/11).
3003+Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
3004+ aufs2 GIT tree (2009/2).
3005+William Grant made a donation (2009/3).
3006+Patrick Lane made a donation (2009/4).
3007+The Mail Archive (mail-archive.com) made donations (2009/5).
3008+Nippy Networks (Ed Wildgoose) made a donation (2009/7).
3009+New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
3010+Pavel Pronskiy made a donation (2011/2).
3011+Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
3012+ Networks (Ed Wildgoose) made a donation for hardware (2011/3).
537831f9
AM
3013+Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
3014+11).
1e00d052 3015+Sam Liddicott made a donation (2011/9).
86dc4139
AM
3016+Era Scarecrow made a donation (2013/4).
3017+Bor Ratajc made a donation (2013/4).
3018+Alessandro Gorreta made a donation (2013/4).
3019+POIRETTE Marc made a donation (2013/4).
3020+Alessandro Gorreta made a donation (2013/4).
3021+lauri kasvandik made a donation (2013/5).
392086de 3022+"pemasu from Finland" made a donation (2013/7).
523b37e3
AM
3023+The Parted Magic Project made a donation (2013/9 and 11).
3024+Pavel Barta made a donation (2013/10).
38d290e6 3025+Nikolay Pertsev made a donation (2014/5).
c2c0f25c 3026+James B made a donation (2014/7 and 2015/7).
076b876e 3027+Stefano Di Biase made a donation (2014/8).
2000de60 3028+Daniel Epellei made a donation (2015/1).
c4adf169 3029+OmegaPhil made a donation (2016/1, 2018/4).
5afbbe0d 3030+Tomasz Szewczyk made a donation (2016/4).
f2c43d5f 3031+James Burry made a donation (2016/12).
acd2b654 3032+Carsten Rose made a donation (2018/9).
9f237c51 3033+Porteus Kiosk made a donation (2018/10).
53392da6
AM
3034+
3035+Thank you very much.
3036+Donations are always, including future donations, very important and
3037+helpful for me to keep on developing aufs.
3038+
3039+
3040+7.
3041+----------------------------------------
3042+If you are an experienced user, no explanation is needed. Aufs is
3043+just a linux filesystem.
3044+
3045+
3046+Enjoy!
3047+
3048+# Local variables: ;
3049+# mode: text;
3050+# End: ;
7f207e10
AM
3051diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
3052--- /usr/share/empty/fs/aufs/aufs.h 1970-01-01 01:00:00.000000000 +0100
ba1aed25 3053+++ linux/fs/aufs/aufs.h 2019-03-05 12:13:00.135890907 +0100
acd2b654 3054@@ -0,0 +1,62 @@
062440b3 3055+/* SPDX-License-Identifier: GPL-2.0 */
7f207e10 3056+/*
ba1aed25 3057+ * Copyright (C) 2005-2019 Junjiro R. Okajima
7f207e10
AM
3058+ *
3059+ * This program, aufs is free software; you can redistribute it and/or modify
3060+ * it under the terms of the GNU General Public License as published by
3061+ * the Free Software Foundation; either version 2 of the License, or
3062+ * (at your option) any later version.
3063+ *
3064+ * This program is distributed in the hope that it will be useful,
3065+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
3066+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3067+ * GNU General Public License for more details.
3068+ *
3069+ * You should have received a copy of the GNU General Public License
523b37e3 3070+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
3071+ */
3072+
3073+/*
3074+ * all header files
3075+ */
3076+
3077+#ifndef __AUFS_H__
3078+#define __AUFS_H__
3079+
3080+#ifdef __KERNEL__
3081+
3082+#define AuStub(type, name, body, ...) \
3083+ static inline type name(__VA_ARGS__) { body; }
3084+
3085+#define AuStubVoid(name, ...) \
3086+ AuStub(void, name, , __VA_ARGS__)
3087+#define AuStubInt0(name, ...) \
3088+ AuStub(int, name, return 0, __VA_ARGS__)
3089+
3090+#include "debug.h"
3091+
3092+#include "branch.h"
3093+#include "cpup.h"
3094+#include "dcsub.h"
3095+#include "dbgaufs.h"
3096+#include "dentry.h"
3097+#include "dir.h"
8b6a4947 3098+#include "dirren.h"
7f207e10
AM
3099+#include "dynop.h"
3100+#include "file.h"
3101+#include "fstype.h"
8b6a4947 3102+#include "hbl.h"
7f207e10 3103+#include "inode.h"
acd2b654 3104+#include "lcnt.h"
7f207e10
AM
3105+#include "loop.h"
3106+#include "module.h"
7f207e10
AM
3107+#include "opts.h"
3108+#include "rwsem.h"
7f207e10
AM
3109+#include "super.h"
3110+#include "sysaufs.h"
3111+#include "vfsub.h"
3112+#include "whout.h"
3113+#include "wkq.h"
3114+
3115+#endif /* __KERNEL__ */
3116+#endif /* __AUFS_H__ */
3117diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
3118--- /usr/share/empty/fs/aufs/branch.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25
AM
3119+++ linux/fs/aufs/branch.c 2019-03-05 12:13:00.135890907 +0100
3120@@ -0,0 +1,1423 @@
cd7a4cd9 3121+// SPDX-License-Identifier: GPL-2.0
7f207e10 3122+/*
ba1aed25 3123+ * Copyright (C) 2005-2019 Junjiro R. Okajima
7f207e10
AM
3124+ *
3125+ * This program, aufs is free software; you can redistribute it and/or modify
3126+ * it under the terms of the GNU General Public License as published by
3127+ * the Free Software Foundation; either version 2 of the License, or
3128+ * (at your option) any later version.
3129+ *
3130+ * This program is distributed in the hope that it will be useful,
3131+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
3132+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3133+ * GNU General Public License for more details.
3134+ *
3135+ * You should have received a copy of the GNU General Public License
523b37e3 3136+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
3137+ */
3138+
3139+/*
3140+ * branch management
3141+ */
3142+
027c5e7a 3143+#include <linux/compat.h>
7f207e10
AM
3144+#include <linux/statfs.h>
3145+#include "aufs.h"
3146+
3147+/*
3148+ * free a single branch
1facf9fc 3149+ */
3150+static void au_br_do_free(struct au_branch *br)
3151+{
3152+ int i;
3153+ struct au_wbr *wbr;
4a4d8108 3154+ struct au_dykey **key;
1facf9fc 3155+
027c5e7a 3156+ au_hnotify_fin_br(br);
8b6a4947
AM
3157+ /* always, regardless the mount option */
3158+ au_dr_hino_free(&br->br_dirren);
062440b3 3159+ au_xino_put(br);
1facf9fc 3160+
acd2b654
AM
3161+ AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
3162+ au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
3163+ AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
3164+ au_lcnt_fin(&br->br_count, /*do_sync*/0);
1facf9fc 3165+
3166+ wbr = br->br_wbr;
3167+ if (wbr) {
3168+ for (i = 0; i < AuBrWh_Last; i++)
3169+ dput(wbr->wbr_wh[i]);
3170+ AuDebugOn(atomic_read(&wbr->wbr_wh_running));
dece6358 3171+ AuRwDestroy(&wbr->wbr_wh_rwsem);
1facf9fc 3172+ }
3173+
076b876e
AM
3174+ if (br->br_fhsm) {
3175+ au_br_fhsm_fin(br->br_fhsm);
9f237c51 3176+ au_kfree_try_rcu(br->br_fhsm);
076b876e
AM
3177+ }
3178+
4a4d8108
AM
3179+ key = br->br_dykey;
3180+ for (i = 0; i < AuBrDynOp; i++, key++)
3181+ if (*key)
3182+ au_dy_put(*key);
3183+ else
3184+ break;
3185+
537831f9 3186+ /* recursive lock, s_umount of branch's */
acd2b654 3187+ /* synchronize_rcu(); */ /* why? */
537831f9 3188+ lockdep_off();
86dc4139 3189+ path_put(&br->br_path);
537831f9 3190+ lockdep_on();
9f237c51 3191+ au_kfree_rcu(wbr);
acd2b654
AM
3192+ au_lcnt_wait_for_fin(&br->br_nfiles);
3193+ au_lcnt_wait_for_fin(&br->br_count);
3194+ /* I don't know why, but percpu_refcount requires this */
3195+ /* synchronize_rcu(); */
9f237c51 3196+ au_kfree_rcu(br);
1facf9fc 3197+}
3198+
3199+/*
3200+ * frees all branches
3201+ */
3202+void au_br_free(struct au_sbinfo *sbinfo)
3203+{
3204+ aufs_bindex_t bmax;
3205+ struct au_branch **br;
3206+
dece6358
AM
3207+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3208+
5afbbe0d 3209+ bmax = sbinfo->si_bbot + 1;
1facf9fc 3210+ br = sbinfo->si_branch;
3211+ while (bmax--)
3212+ au_br_do_free(*br++);
3213+}
3214+
3215+/*
3216+ * find the index of a branch which is specified by @br_id.
3217+ */
3218+int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3219+{
5afbbe0d 3220+ aufs_bindex_t bindex, bbot;
1facf9fc 3221+
5afbbe0d
AM
3222+ bbot = au_sbbot(sb);
3223+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 3224+ if (au_sbr_id(sb, bindex) == br_id)
3225+ return bindex;
3226+ return -1;
3227+}
3228+
3229+/* ---------------------------------------------------------------------- */
3230+
3231+/*
3232+ * add a branch
3233+ */
3234+
b752ccd1
AM
3235+static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3236+ struct dentry *h_root)
1facf9fc 3237+{
b752ccd1
AM
3238+ if (unlikely(h_adding == h_root
3239+ || au_test_loopback_overlap(sb, h_adding)))
1facf9fc 3240+ return 1;
b752ccd1
AM
3241+ if (h_adding->d_sb != h_root->d_sb)
3242+ return 0;
3243+ return au_test_subdir(h_adding, h_root)
3244+ || au_test_subdir(h_root, h_adding);
1facf9fc 3245+}
3246+
3247+/*
3248+ * returns a newly allocated branch. @new_nbranch is a number of branches
3249+ * after adding a branch.
3250+ */
3251+static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3252+ int perm)
3253+{
3254+ struct au_branch *add_branch;
3255+ struct dentry *root;
5527c038 3256+ struct inode *inode;
4a4d8108 3257+ int err;
1facf9fc 3258+
4a4d8108 3259+ err = -ENOMEM;
be52b249 3260+ add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
1facf9fc 3261+ if (unlikely(!add_branch))
3262+ goto out;
acd2b654 3263+ add_branch->br_xino = au_xino_alloc(/*nfile*/1);
062440b3 3264+ if (unlikely(!add_branch->br_xino))
521ced18 3265+ goto out_br;
027c5e7a
AM
3266+ err = au_hnotify_init_br(add_branch, perm);
3267+ if (unlikely(err))
062440b3 3268+ goto out_xino;
027c5e7a 3269+
1facf9fc 3270+ if (au_br_writable(perm)) {
3271+ /* may be freed separately at changing the branch permission */
be52b249 3272+ add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
1facf9fc 3273+ GFP_NOFS);
3274+ if (unlikely(!add_branch->br_wbr))
027c5e7a 3275+ goto out_hnotify;
1facf9fc 3276+ }
3277+
076b876e
AM
3278+ if (au_br_fhsm(perm)) {
3279+ err = au_fhsm_br_alloc(add_branch);
3280+ if (unlikely(err))
3281+ goto out_wbr;
3282+ }
3283+
521ced18 3284+ root = sb->s_root;
e2f27e51 3285+ err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
4a4d8108 3286+ if (!err)
e2f27e51 3287+ err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
5527c038
JR
3288+ if (!err) {
3289+ inode = d_inode(root);
1c60b727
AM
3290+ err = au_hinode_realloc(au_ii(inode), new_nbranch,
3291+ /*may_shrink*/0);
5527c038 3292+ }
4a4d8108
AM
3293+ if (!err)
3294+ return add_branch; /* success */
1facf9fc 3295+
076b876e 3296+out_wbr:
9f237c51 3297+ au_kfree_rcu(add_branch->br_wbr);
027c5e7a
AM
3298+out_hnotify:
3299+ au_hnotify_fin_br(add_branch);
062440b3
AM
3300+out_xino:
3301+ au_xino_put(add_branch);
4f0767ce 3302+out_br:
9f237c51 3303+ au_kfree_rcu(add_branch);
4f0767ce 3304+out:
4a4d8108 3305+ return ERR_PTR(err);
1facf9fc 3306+}
3307+
3308+/*
3309+ * test if the branch permission is legal or not.
3310+ */
3311+static int test_br(struct inode *inode, int brperm, char *path)
3312+{
3313+ int err;
3314+
4a4d8108
AM
3315+ err = (au_br_writable(brperm) && IS_RDONLY(inode));
3316+ if (!err)
3317+ goto out;
1facf9fc 3318+
4a4d8108
AM
3319+ err = -EINVAL;
3320+ pr_err("write permission for readonly mount or inode, %s\n", path);
3321+
4f0767ce 3322+out:
1facf9fc 3323+ return err;
3324+}
3325+
3326+/*
3327+ * returns:
3328+ * 0: success, the caller will add it
3329+ * plus: success, it is already unified, the caller should ignore it
3330+ * minus: error
3331+ */
3332+static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3333+{
3334+ int err;
5afbbe0d 3335+ aufs_bindex_t bbot, bindex;
5527c038 3336+ struct dentry *root, *h_dentry;
1facf9fc 3337+ struct inode *inode, *h_inode;
3338+
3339+ root = sb->s_root;
5afbbe0d
AM
3340+ bbot = au_sbbot(sb);
3341+ if (unlikely(bbot >= 0
1facf9fc 3342+ && au_find_dbindex(root, add->path.dentry) >= 0)) {
3343+ err = 1;
3344+ if (!remount) {
3345+ err = -EINVAL;
4a4d8108 3346+ pr_err("%s duplicated\n", add->pathname);
1facf9fc 3347+ }
3348+ goto out;
3349+ }
3350+
3351+ err = -ENOSPC; /* -E2BIG; */
3352+ if (unlikely(AUFS_BRANCH_MAX <= add->bindex
5afbbe0d 3353+ || AUFS_BRANCH_MAX - 1 <= bbot)) {
4a4d8108 3354+ pr_err("number of branches exceeded %s\n", add->pathname);
1facf9fc 3355+ goto out;
3356+ }
3357+
3358+ err = -EDOM;
5afbbe0d 3359+ if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
4a4d8108 3360+ pr_err("bad index %d\n", add->bindex);
1facf9fc 3361+ goto out;
3362+ }
3363+
5527c038 3364+ inode = d_inode(add->path.dentry);
1facf9fc 3365+ err = -ENOENT;
3366+ if (unlikely(!inode->i_nlink)) {
4a4d8108 3367+ pr_err("no existence %s\n", add->pathname);
1facf9fc 3368+ goto out;
3369+ }
3370+
3371+ err = -EINVAL;
3372+ if (unlikely(inode->i_sb == sb)) {
4a4d8108 3373+ pr_err("%s must be outside\n", add->pathname);
1facf9fc 3374+ goto out;
3375+ }
3376+
3377+ if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
4a4d8108
AM
3378+ pr_err("unsupported filesystem, %s (%s)\n",
3379+ add->pathname, au_sbtype(inode->i_sb));
1facf9fc 3380+ goto out;
3381+ }
3382+
c1595e42
JR
3383+ if (unlikely(inode->i_sb->s_stack_depth)) {
3384+ pr_err("already stacked, %s (%s)\n",
3385+ add->pathname, au_sbtype(inode->i_sb));
3386+ goto out;
3387+ }
3388+
5527c038 3389+ err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
1facf9fc 3390+ if (unlikely(err))
3391+ goto out;
3392+
5afbbe0d 3393+ if (bbot < 0)
1facf9fc 3394+ return 0; /* success */
3395+
3396+ err = -EINVAL;
5afbbe0d 3397+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 3398+ if (unlikely(test_overlap(sb, add->path.dentry,
3399+ au_h_dptr(root, bindex)))) {
4a4d8108 3400+ pr_err("%s is overlapped\n", add->pathname);
1facf9fc 3401+ goto out;
3402+ }
3403+
3404+ err = 0;
3405+ if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
5527c038
JR
3406+ h_dentry = au_h_dptr(root, 0);
3407+ h_inode = d_inode(h_dentry);
1facf9fc 3408+ if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
0c3ec466
AM
3409+ || !uid_eq(h_inode->i_uid, inode->i_uid)
3410+ || !gid_eq(h_inode->i_gid, inode->i_gid))
3411+ pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3412+ add->pathname,
3413+ i_uid_read(inode), i_gid_read(inode),
3414+ (inode->i_mode & S_IALLUGO),
3415+ i_uid_read(h_inode), i_gid_read(h_inode),
3416+ (h_inode->i_mode & S_IALLUGO));
1facf9fc 3417+ }
3418+
4f0767ce 3419+out:
1facf9fc 3420+ return err;
3421+}
3422+
3423+/*
3424+ * initialize or clean the whiteouts for an adding branch
3425+ */
3426+static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
86dc4139 3427+ int new_perm)
1facf9fc 3428+{
3429+ int err, old_perm;
3430+ aufs_bindex_t bindex;
febd17d6 3431+ struct inode *h_inode;
1facf9fc 3432+ struct au_wbr *wbr;
3433+ struct au_hinode *hdir;
5527c038 3434+ struct dentry *h_dentry;
1facf9fc 3435+
86dc4139
AM
3436+ err = vfsub_mnt_want_write(au_br_mnt(br));
3437+ if (unlikely(err))
3438+ goto out;
3439+
1facf9fc 3440+ wbr = br->br_wbr;
3441+ old_perm = br->br_perm;
3442+ br->br_perm = new_perm;
3443+ hdir = NULL;
febd17d6 3444+ h_inode = NULL;
1facf9fc 3445+ bindex = au_br_index(sb, br->br_id);
3446+ if (0 <= bindex) {
5527c038 3447+ hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 3448+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 3449+ } else {
5527c038 3450+ h_dentry = au_br_dentry(br);
febd17d6
JR
3451+ h_inode = d_inode(h_dentry);
3452+ inode_lock_nested(h_inode, AuLsc_I_PARENT);
1facf9fc 3453+ }
3454+ if (!wbr)
86dc4139 3455+ err = au_wh_init(br, sb);
1facf9fc 3456+ else {
3457+ wbr_wh_write_lock(wbr);
86dc4139 3458+ err = au_wh_init(br, sb);
1facf9fc 3459+ wbr_wh_write_unlock(wbr);
3460+ }
3461+ if (hdir)
5afbbe0d 3462+ au_hn_inode_unlock(hdir);
1facf9fc 3463+ else
febd17d6 3464+ inode_unlock(h_inode);
86dc4139 3465+ vfsub_mnt_drop_write(au_br_mnt(br));
1facf9fc 3466+ br->br_perm = old_perm;
3467+
3468+ if (!err && wbr && !au_br_writable(new_perm)) {
9f237c51 3469+ au_kfree_rcu(wbr);
1facf9fc 3470+ br->br_wbr = NULL;
3471+ }
3472+
86dc4139 3473+out:
1facf9fc 3474+ return err;
3475+}
3476+
3477+static int au_wbr_init(struct au_branch *br, struct super_block *sb,
86dc4139 3478+ int perm)
1facf9fc 3479+{
3480+ int err;
4a4d8108 3481+ struct kstatfs kst;
1facf9fc 3482+ struct au_wbr *wbr;
3483+
3484+ wbr = br->br_wbr;
dece6358 3485+ au_rw_init(&wbr->wbr_wh_rwsem);
1facf9fc 3486+ atomic_set(&wbr->wbr_wh_running, 0);
1facf9fc 3487+
4a4d8108
AM
3488+ /*
3489+ * a limit for rmdir/rename a dir
523b37e3 3490+ * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
4a4d8108 3491+ */
86dc4139 3492+ err = vfs_statfs(&br->br_path, &kst);
4a4d8108
AM
3493+ if (unlikely(err))
3494+ goto out;
3495+ err = -EINVAL;
3496+ if (kst.f_namelen >= NAME_MAX)
86dc4139 3497+ err = au_br_init_wh(sb, br, perm);
4a4d8108 3498+ else
523b37e3
AM
3499+ pr_err("%pd(%s), unsupported namelen %ld\n",
3500+ au_br_dentry(br),
86dc4139 3501+ au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
1facf9fc 3502+
4f0767ce 3503+out:
1facf9fc 3504+ return err;
3505+}
3506+
c1595e42 3507+/* initialize a new branch */
1facf9fc 3508+static int au_br_init(struct au_branch *br, struct super_block *sb,
3509+ struct au_opt_add *add)
3510+{
3511+ int err;
062440b3
AM
3512+ struct au_branch *brbase;
3513+ struct file *xf;
5527c038 3514+ struct inode *h_inode;
1facf9fc 3515+
3516+ err = 0;
1facf9fc 3517+ br->br_perm = add->perm;
86dc4139 3518+ br->br_path = add->path; /* set first, path_get() later */
4a4d8108 3519+ spin_lock_init(&br->br_dykey_lock);
acd2b654
AM
3520+ au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3521+ au_lcnt_init(&br->br_count, /*release*/NULL);
1facf9fc 3522+ br->br_id = au_new_br_id(sb);
7f207e10 3523+ AuDebugOn(br->br_id < 0);
1facf9fc 3524+
8b6a4947
AM
3525+ /* always, regardless the given option */
3526+ err = au_dr_br_init(sb, br, &add->path);
3527+ if (unlikely(err))
3528+ goto out_err;
3529+
1facf9fc 3530+ if (au_br_writable(add->perm)) {
86dc4139 3531+ err = au_wbr_init(br, sb, add->perm);
1facf9fc 3532+ if (unlikely(err))
b752ccd1 3533+ goto out_err;
1facf9fc 3534+ }
3535+
3536+ if (au_opt_test(au_mntflags(sb), XINO)) {
062440b3 3537+ brbase = au_sbr(sb, 0);
acd2b654 3538+ xf = au_xino_file(brbase->br_xino, /*idx*/-1);
062440b3 3539+ AuDebugOn(!xf);
5527c038 3540+ h_inode = d_inode(add->path.dentry);
062440b3 3541+ err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
1facf9fc 3542+ if (unlikely(err)) {
acd2b654 3543+ AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
b752ccd1 3544+ goto out_err;
1facf9fc 3545+ }
3546+ }
3547+
3548+ sysaufs_br_init(br);
86dc4139 3549+ path_get(&br->br_path);
b752ccd1 3550+ goto out; /* success */
1facf9fc 3551+
4f0767ce 3552+out_err:
86dc4139 3553+ memset(&br->br_path, 0, sizeof(br->br_path));
4f0767ce 3554+out:
1facf9fc 3555+ return err;
3556+}
3557+
3558+static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
5afbbe0d 3559+ struct au_branch *br, aufs_bindex_t bbot,
1facf9fc 3560+ aufs_bindex_t amount)
3561+{
3562+ struct au_branch **brp;
3563+
dece6358
AM
3564+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3565+
1facf9fc 3566+ brp = sbinfo->si_branch + bindex;
3567+ memmove(brp + 1, brp, sizeof(*brp) * amount);
3568+ *brp = br;
5afbbe0d
AM
3569+ sbinfo->si_bbot++;
3570+ if (unlikely(bbot < 0))
3571+ sbinfo->si_bbot = 0;
1facf9fc 3572+}
3573+
3574+static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
5afbbe0d 3575+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3576+{
3577+ struct au_hdentry *hdp;
3578+
1308ab2a 3579+ AuRwMustWriteLock(&dinfo->di_rwsem);
3580+
5afbbe0d 3581+ hdp = au_hdentry(dinfo, bindex);
1facf9fc 3582+ memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3583+ au_h_dentry_init(hdp);
5afbbe0d
AM
3584+ dinfo->di_bbot++;
3585+ if (unlikely(bbot < 0))
3586+ dinfo->di_btop = 0;
1facf9fc 3587+}
3588+
3589+static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
5afbbe0d 3590+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3591+{
3592+ struct au_hinode *hip;
3593+
1308ab2a 3594+ AuRwMustWriteLock(&iinfo->ii_rwsem);
3595+
5afbbe0d 3596+ hip = au_hinode(iinfo, bindex);
1facf9fc 3597+ memmove(hip + 1, hip, sizeof(*hip) * amount);
5afbbe0d
AM
3598+ au_hinode_init(hip);
3599+ iinfo->ii_bbot++;
3600+ if (unlikely(bbot < 0))
3601+ iinfo->ii_btop = 0;
1facf9fc 3602+}
3603+
86dc4139
AM
3604+static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3605+ aufs_bindex_t bindex)
1facf9fc 3606+{
86dc4139 3607+ struct dentry *root, *h_dentry;
5527c038 3608+ struct inode *root_inode, *h_inode;
5afbbe0d 3609+ aufs_bindex_t bbot, amount;
1facf9fc 3610+
3611+ root = sb->s_root;
5527c038 3612+ root_inode = d_inode(root);
5afbbe0d
AM
3613+ bbot = au_sbbot(sb);
3614+ amount = bbot + 1 - bindex;
86dc4139 3615+ h_dentry = au_br_dentry(br);
53392da6 3616+ au_sbilist_lock();
5afbbe0d
AM
3617+ au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3618+ au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3619+ au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
1facf9fc 3620+ au_set_h_dptr(root, bindex, dget(h_dentry));
5527c038
JR
3621+ h_inode = d_inode(h_dentry);
3622+ au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
53392da6 3623+ au_sbilist_unlock();
1facf9fc 3624+}
3625+
3626+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3627+{
3628+ int err;
5afbbe0d 3629+ aufs_bindex_t bbot, add_bindex;
1facf9fc 3630+ struct dentry *root, *h_dentry;
3631+ struct inode *root_inode;
3632+ struct au_branch *add_branch;
3633+
3634+ root = sb->s_root;
5527c038 3635+ root_inode = d_inode(root);
1facf9fc 3636+ IMustLock(root_inode);
5afbbe0d 3637+ IiMustWriteLock(root_inode);
1facf9fc 3638+ err = test_add(sb, add, remount);
3639+ if (unlikely(err < 0))
3640+ goto out;
3641+ if (err) {
3642+ err = 0;
3643+ goto out; /* success */
3644+ }
3645+
5afbbe0d
AM
3646+ bbot = au_sbbot(sb);
3647+ add_branch = au_br_alloc(sb, bbot + 2, add->perm);
1facf9fc 3648+ err = PTR_ERR(add_branch);
3649+ if (IS_ERR(add_branch))
3650+ goto out;
3651+
3652+ err = au_br_init(add_branch, sb, add);
3653+ if (unlikely(err)) {
3654+ au_br_do_free(add_branch);
3655+ goto out;
3656+ }
3657+
3658+ add_bindex = add->bindex;
062440b3
AM
3659+ sysaufs_brs_del(sb, add_bindex); /* remove successors */
3660+ au_br_do_add(sb, add_branch, add_bindex);
3661+ sysaufs_brs_add(sb, add_bindex); /* append successors */
3662+ dbgaufs_brs_add(sb, add_bindex, /*topdown*/0); /* rename successors */
1facf9fc 3663+
86dc4139 3664+ h_dentry = add->path.dentry;
1308ab2a 3665+ if (!add_bindex) {
1facf9fc 3666+ au_cpup_attr_all(root_inode, /*force*/1);
1308ab2a 3667+ sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3668+ } else
5527c038 3669+ au_add_nlink(root_inode, d_inode(h_dentry));
1facf9fc 3670+
4f0767ce 3671+out:
1facf9fc 3672+ return err;
3673+}
3674+
3675+/* ---------------------------------------------------------------------- */
3676+
79b8bda9 3677+static unsigned long long au_farray_cb(struct super_block *sb, void *a,
076b876e
AM
3678+ unsigned long long max __maybe_unused,
3679+ void *arg)
3680+{
3681+ unsigned long long n;
3682+ struct file **p, *f;
8b6a4947
AM
3683+ struct hlist_bl_head *files;
3684+ struct hlist_bl_node *pos;
076b876e 3685+ struct au_finfo *finfo;
076b876e
AM
3686+
3687+ n = 0;
3688+ p = a;
3689+ files = &au_sbi(sb)->si_files;
8b6a4947
AM
3690+ hlist_bl_lock(files);
3691+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
076b876e
AM
3692+ f = finfo->fi_file;
3693+ if (file_count(f)
3694+ && !special_file(file_inode(f)->i_mode)) {
3695+ get_file(f);
3696+ *p++ = f;
3697+ n++;
3698+ AuDebugOn(n > max);
3699+ }
3700+ }
8b6a4947 3701+ hlist_bl_unlock(files);
076b876e
AM
3702+
3703+ return n;
3704+}
3705+
3706+static struct file **au_farray_alloc(struct super_block *sb,
3707+ unsigned long long *max)
3708+{
acd2b654
AM
3709+ struct au_sbinfo *sbi;
3710+
3711+ sbi = au_sbi(sb);
3712+ *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
79b8bda9 3713+ return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
076b876e
AM
3714+}
3715+
3716+static void au_farray_free(struct file **a, unsigned long long max)
3717+{
3718+ unsigned long long ull;
3719+
3720+ for (ull = 0; ull < max; ull++)
3721+ if (a[ull])
3722+ fput(a[ull]);
be52b249 3723+ kvfree(a);
076b876e
AM
3724+}
3725+
3726+/* ---------------------------------------------------------------------- */
3727+
1facf9fc 3728+/*
3729+ * delete a branch
3730+ */
3731+
3732+/* to show the line number, do not make it inlined function */
4a4d8108 3733+#define AuVerbose(do_info, fmt, ...) do { \
1facf9fc 3734+ if (do_info) \
4a4d8108 3735+ pr_info(fmt, ##__VA_ARGS__); \
1facf9fc 3736+} while (0)
3737+
5afbbe0d
AM
3738+static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3739+ aufs_bindex_t bbot)
027c5e7a 3740+{
5afbbe0d 3741+ return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
027c5e7a
AM
3742+}
3743+
5afbbe0d
AM
3744+static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3745+ aufs_bindex_t bbot)
027c5e7a 3746+{
5afbbe0d 3747+ return au_test_ibusy(d_inode(dentry), btop, bbot);
027c5e7a
AM
3748+}
3749+
1facf9fc 3750+/*
3751+ * test if the branch is deletable or not.
3752+ */
3753+static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
b752ccd1 3754+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3755+{
3756+ int err, i, j, ndentry;
5afbbe0d 3757+ aufs_bindex_t btop, bbot;
1facf9fc 3758+ struct au_dcsub_pages dpages;
3759+ struct au_dpage *dpage;
3760+ struct dentry *d;
1facf9fc 3761+
3762+ err = au_dpages_init(&dpages, GFP_NOFS);
3763+ if (unlikely(err))
3764+ goto out;
3765+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
3766+ if (unlikely(err))
3767+ goto out_dpages;
3768+
1facf9fc 3769+ for (i = 0; !err && i < dpages.ndpage; i++) {
3770+ dpage = dpages.dpages + i;
3771+ ndentry = dpage->ndentry;
3772+ for (j = 0; !err && j < ndentry; j++) {
3773+ d = dpage->dentries[j];
c1595e42 3774+ AuDebugOn(au_dcount(d) <= 0);
027c5e7a 3775+ if (!au_digen_test(d, sigen)) {
1facf9fc 3776+ di_read_lock_child(d, AuLock_IR);
027c5e7a
AM
3777+ if (unlikely(au_dbrange_test(d))) {
3778+ di_read_unlock(d, AuLock_IR);
3779+ continue;
3780+ }
3781+ } else {
1facf9fc 3782+ di_write_lock_child(d);
027c5e7a
AM
3783+ if (unlikely(au_dbrange_test(d))) {
3784+ di_write_unlock(d);
3785+ continue;
3786+ }
1facf9fc 3787+ err = au_reval_dpath(d, sigen);
3788+ if (!err)
3789+ di_downgrade_lock(d, AuLock_IR);
3790+ else {
3791+ di_write_unlock(d);
3792+ break;
3793+ }
3794+ }
3795+
027c5e7a 3796+ /* AuDbgDentry(d); */
5afbbe0d
AM
3797+ btop = au_dbtop(d);
3798+ bbot = au_dbbot(d);
3799+ if (btop <= bindex
3800+ && bindex <= bbot
1facf9fc 3801+ && au_h_dptr(d, bindex)
5afbbe0d 3802+ && au_test_dbusy(d, btop, bbot)) {
1facf9fc 3803+ err = -EBUSY;
523b37e3 3804+ AuVerbose(verbose, "busy %pd\n", d);
027c5e7a 3805+ AuDbgDentry(d);
1facf9fc 3806+ }
3807+ di_read_unlock(d, AuLock_IR);
3808+ }
3809+ }
3810+
4f0767ce 3811+out_dpages:
1facf9fc 3812+ au_dpages_free(&dpages);
4f0767ce 3813+out:
1facf9fc 3814+ return err;
3815+}
3816+
3817+static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
b752ccd1 3818+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3819+{
3820+ int err;
7f207e10
AM
3821+ unsigned long long max, ull;
3822+ struct inode *i, **array;
5afbbe0d 3823+ aufs_bindex_t btop, bbot;
1facf9fc 3824+
7f207e10
AM
3825+ array = au_iarray_alloc(sb, &max);
3826+ err = PTR_ERR(array);
3827+ if (IS_ERR(array))
3828+ goto out;
3829+
1facf9fc 3830+ err = 0;
7f207e10
AM
3831+ AuDbg("b%d\n", bindex);
3832+ for (ull = 0; !err && ull < max; ull++) {
3833+ i = array[ull];
076b876e
AM
3834+ if (unlikely(!i))
3835+ break;
7f207e10 3836+ if (i->i_ino == AUFS_ROOT_INO)
1facf9fc 3837+ continue;
3838+
7f207e10 3839+ /* AuDbgInode(i); */
537831f9 3840+ if (au_iigen(i, NULL) == sigen)
1facf9fc 3841+ ii_read_lock_child(i);
3842+ else {
3843+ ii_write_lock_child(i);
027c5e7a
AM
3844+ err = au_refresh_hinode_self(i);
3845+ au_iigen_dec(i);
1facf9fc 3846+ if (!err)
3847+ ii_downgrade_lock(i);
3848+ else {
3849+ ii_write_unlock(i);
3850+ break;
3851+ }
3852+ }
3853+
5afbbe0d
AM
3854+ btop = au_ibtop(i);
3855+ bbot = au_ibbot(i);
3856+ if (btop <= bindex
3857+ && bindex <= bbot
1facf9fc 3858+ && au_h_iptr(i, bindex)
5afbbe0d 3859+ && au_test_ibusy(i, btop, bbot)) {
1facf9fc 3860+ err = -EBUSY;
3861+ AuVerbose(verbose, "busy i%lu\n", i->i_ino);
7f207e10 3862+ AuDbgInode(i);
1facf9fc 3863+ }
3864+ ii_read_unlock(i);
3865+ }
7f207e10 3866+ au_iarray_free(array, max);
1facf9fc 3867+
7f207e10 3868+out:
1facf9fc 3869+ return err;
3870+}
3871+
b752ccd1
AM
3872+static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3873+ const unsigned int verbose)
1facf9fc 3874+{
3875+ int err;
3876+ unsigned int sigen;
3877+
3878+ sigen = au_sigen(root->d_sb);
3879+ DiMustNoWaiters(root);
5527c038 3880+ IiMustNoWaiters(d_inode(root));
1facf9fc 3881+ di_write_unlock(root);
b752ccd1 3882+ err = test_dentry_busy(root, bindex, sigen, verbose);
1facf9fc 3883+ if (!err)
b752ccd1 3884+ err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
1facf9fc 3885+ di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3886+
3887+ return err;
3888+}
3889+
076b876e
AM
3890+static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3891+ struct file **to_free, int *idx)
3892+{
3893+ int err;
c1595e42 3894+ unsigned char matched, root;
5afbbe0d 3895+ aufs_bindex_t bindex, bbot;
076b876e
AM
3896+ struct au_fidir *fidir;
3897+ struct au_hfile *hfile;
3898+
3899+ err = 0;
2000de60 3900+ root = IS_ROOT(file->f_path.dentry);
c1595e42
JR
3901+ if (root) {
3902+ get_file(file);
3903+ to_free[*idx] = file;
3904+ (*idx)++;
3905+ goto out;
3906+ }
3907+
076b876e 3908+ matched = 0;
076b876e
AM
3909+ fidir = au_fi(file)->fi_hdir;
3910+ AuDebugOn(!fidir);
5afbbe0d
AM
3911+ bbot = au_fbbot_dir(file);
3912+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
076b876e
AM
3913+ hfile = fidir->fd_hfile + bindex;
3914+ if (!hfile->hf_file)
3915+ continue;
3916+
c1595e42 3917+ if (hfile->hf_br->br_id == br_id) {
076b876e 3918+ matched = 1;
076b876e 3919+ break;
c1595e42 3920+ }
076b876e 3921+ }
c1595e42 3922+ if (matched)
076b876e
AM
3923+ err = -EBUSY;
3924+
3925+out:
3926+ return err;
3927+}
3928+
3929+static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3930+ struct file **to_free, int opened)
3931+{
3932+ int err, idx;
3933+ unsigned long long ull, max;
5afbbe0d 3934+ aufs_bindex_t btop;
076b876e 3935+ struct file *file, **array;
076b876e
AM
3936+ struct dentry *root;
3937+ struct au_hfile *hfile;
3938+
3939+ array = au_farray_alloc(sb, &max);
3940+ err = PTR_ERR(array);
3941+ if (IS_ERR(array))
3942+ goto out;
3943+
3944+ err = 0;
3945+ idx = 0;
3946+ root = sb->s_root;
3947+ di_write_unlock(root);
3948+ for (ull = 0; ull < max; ull++) {
3949+ file = array[ull];
3950+ if (unlikely(!file))
3951+ break;
3952+
3953+ /* AuDbg("%pD\n", file); */
3954+ fi_read_lock(file);
5afbbe0d 3955+ btop = au_fbtop(file);
2000de60 3956+ if (!d_is_dir(file->f_path.dentry)) {
076b876e
AM
3957+ hfile = &au_fi(file)->fi_htop;
3958+ if (hfile->hf_br->br_id == br_id)
3959+ err = -EBUSY;
3960+ } else
3961+ err = test_dir_busy(file, br_id, to_free, &idx);
3962+ fi_read_unlock(file);
3963+ if (unlikely(err))
3964+ break;
3965+ }
3966+ di_write_lock_child(root);
3967+ au_farray_free(array, max);
3968+ AuDebugOn(idx > opened);
3969+
3970+out:
3971+ return err;
3972+}
3973+
3974+static void br_del_file(struct file **to_free, unsigned long long opened,
062440b3 3975+ aufs_bindex_t br_id)
076b876e
AM
3976+{
3977+ unsigned long long ull;
5afbbe0d 3978+ aufs_bindex_t bindex, btop, bbot, bfound;
076b876e
AM
3979+ struct file *file;
3980+ struct au_fidir *fidir;
3981+ struct au_hfile *hfile;
3982+
3983+ for (ull = 0; ull < opened; ull++) {
3984+ file = to_free[ull];
3985+ if (unlikely(!file))
3986+ break;
3987+
3988+ /* AuDbg("%pD\n", file); */
2000de60 3989+ AuDebugOn(!d_is_dir(file->f_path.dentry));
076b876e
AM
3990+ bfound = -1;
3991+ fidir = au_fi(file)->fi_hdir;
3992+ AuDebugOn(!fidir);
3993+ fi_write_lock(file);
5afbbe0d
AM
3994+ btop = au_fbtop(file);
3995+ bbot = au_fbbot_dir(file);
3996+ for (bindex = btop; bindex <= bbot; bindex++) {
076b876e
AM
3997+ hfile = fidir->fd_hfile + bindex;
3998+ if (!hfile->hf_file)
3999+ continue;
4000+
4001+ if (hfile->hf_br->br_id == br_id) {
4002+ bfound = bindex;
4003+ break;
4004+ }
4005+ }
4006+ AuDebugOn(bfound < 0);
4007+ au_set_h_fptr(file, bfound, NULL);
5afbbe0d
AM
4008+ if (bfound == btop) {
4009+ for (btop++; btop <= bbot; btop++)
4010+ if (au_hf_dir(file, btop)) {
4011+ au_set_fbtop(file, btop);
076b876e
AM
4012+ break;
4013+ }
4014+ }
4015+ fi_write_unlock(file);
4016+ }
4017+}
4018+
1facf9fc 4019+static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
4020+ const aufs_bindex_t bindex,
5afbbe0d 4021+ const aufs_bindex_t bbot)
1facf9fc 4022+{
4023+ struct au_branch **brp, **p;
4024+
dece6358
AM
4025+ AuRwMustWriteLock(&sbinfo->si_rwsem);
4026+
1facf9fc 4027+ brp = sbinfo->si_branch + bindex;
5afbbe0d
AM
4028+ if (bindex < bbot)
4029+ memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
4030+ sbinfo->si_branch[0 + bbot] = NULL;
4031+ sbinfo->si_bbot--;
1facf9fc 4032+
e2f27e51
AM
4033+ p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
4034+ /*may_shrink*/1);
1facf9fc 4035+ if (p)
4036+ sbinfo->si_branch = p;
4a4d8108 4037+ /* harmless error */
1facf9fc 4038+}
4039+
4040+static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
5afbbe0d 4041+ const aufs_bindex_t bbot)
1facf9fc 4042+{
4043+ struct au_hdentry *hdp, *p;
4044+
1308ab2a 4045+ AuRwMustWriteLock(&dinfo->di_rwsem);
4046+
5afbbe0d
AM
4047+ hdp = au_hdentry(dinfo, bindex);
4048+ if (bindex < bbot)
4049+ memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
4050+ /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
4051+ dinfo->di_bbot--;
1facf9fc 4052+
e2f27e51
AM
4053+ p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
4054+ /*may_shrink*/1);
1facf9fc 4055+ if (p)
4056+ dinfo->di_hdentry = p;
4a4d8108 4057+ /* harmless error */
1facf9fc 4058+}
4059+
4060+static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
5afbbe0d 4061+ const aufs_bindex_t bbot)
1facf9fc 4062+{
4063+ struct au_hinode *hip, *p;
4064+
1308ab2a 4065+ AuRwMustWriteLock(&iinfo->ii_rwsem);
4066+
5afbbe0d
AM
4067+ hip = au_hinode(iinfo, bindex);
4068+ if (bindex < bbot)
4069+ memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
4070+ /* au_hinode_init(au_hinode(iinfo, bbot)); */
4071+ iinfo->ii_bbot--;
1facf9fc 4072+
e2f27e51
AM
4073+ p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
4074+ /*may_shrink*/1);
1facf9fc 4075+ if (p)
4076+ iinfo->ii_hinode = p;
4a4d8108 4077+ /* harmless error */
1facf9fc 4078+}
4079+
4080+static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
4081+ struct au_branch *br)
4082+{
5afbbe0d 4083+ aufs_bindex_t bbot;
1facf9fc 4084+ struct au_sbinfo *sbinfo;
53392da6
AM
4085+ struct dentry *root, *h_root;
4086+ struct inode *inode, *h_inode;
4087+ struct au_hinode *hinode;
1facf9fc 4088+
dece6358
AM
4089+ SiMustWriteLock(sb);
4090+
1facf9fc 4091+ root = sb->s_root;
5527c038 4092+ inode = d_inode(root);
1facf9fc 4093+ sbinfo = au_sbi(sb);
5afbbe0d 4094+ bbot = sbinfo->si_bbot;
1facf9fc 4095+
53392da6
AM
4096+ h_root = au_h_dptr(root, bindex);
4097+ hinode = au_hi(inode, bindex);
4098+ h_inode = au_igrab(hinode->hi_inode);
4099+ au_hiput(hinode);
1facf9fc 4100+
53392da6 4101+ au_sbilist_lock();
5afbbe0d
AM
4102+ au_br_do_del_brp(sbinfo, bindex, bbot);
4103+ au_br_do_del_hdp(au_di(root), bindex, bbot);
4104+ au_br_do_del_hip(au_ii(inode), bindex, bbot);
53392da6
AM
4105+ au_sbilist_unlock();
4106+
8b6a4947
AM
4107+ /* ignore an error */
4108+ au_dr_br_fin(sb, br); /* always, regardless the mount option */
4109+
53392da6
AM
4110+ dput(h_root);
4111+ iput(h_inode);
4112+ au_br_do_free(br);
1facf9fc 4113+}
4114+
79b8bda9
AM
4115+static unsigned long long empty_cb(struct super_block *sb, void *array,
4116+ unsigned long long max, void *arg)
076b876e
AM
4117+{
4118+ return max;
4119+}
4120+
1facf9fc 4121+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
4122+{
4123+ int err, rerr, i;
076b876e 4124+ unsigned long long opened;
1facf9fc 4125+ unsigned int mnt_flags;
5afbbe0d 4126+ aufs_bindex_t bindex, bbot, br_id;
1facf9fc 4127+ unsigned char do_wh, verbose;
4128+ struct au_branch *br;
4129+ struct au_wbr *wbr;
076b876e
AM
4130+ struct dentry *root;
4131+ struct file **to_free;
1facf9fc 4132+
4133+ err = 0;
076b876e
AM
4134+ opened = 0;
4135+ to_free = NULL;
4136+ root = sb->s_root;
4137+ bindex = au_find_dbindex(root, del->h_path.dentry);
1facf9fc 4138+ if (bindex < 0) {
4139+ if (remount)
4140+ goto out; /* success */
4141+ err = -ENOENT;
4a4d8108 4142+ pr_err("%s no such branch\n", del->pathname);
1facf9fc 4143+ goto out;
4144+ }
4145+ AuDbg("bindex b%d\n", bindex);
4146+
4147+ err = -EBUSY;
4148+ mnt_flags = au_mntflags(sb);
4149+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
5afbbe0d
AM
4150+ bbot = au_sbbot(sb);
4151+ if (unlikely(!bbot)) {
1facf9fc 4152+ AuVerbose(verbose, "no more branches left\n");
4153+ goto out;
4154+ }
acd2b654 4155+
1facf9fc 4156+ br = au_sbr(sb, bindex);
86dc4139 4157+ AuDebugOn(!path_equal(&br->br_path, &del->h_path));
acd2b654
AM
4158+ if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
4159+ AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
4160+ goto out;
4161+ }
076b876e
AM
4162+
4163+ br_id = br->br_id;
acd2b654 4164+ opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
076b876e 4165+ if (unlikely(opened)) {
79b8bda9 4166+ to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
076b876e
AM
4167+ err = PTR_ERR(to_free);
4168+ if (IS_ERR(to_free))
4169+ goto out;
4170+
4171+ err = test_file_busy(sb, br_id, to_free, opened);
4172+ if (unlikely(err)) {
4173+ AuVerbose(verbose, "%llu file(s) opened\n", opened);
4174+ goto out;
4175+ }
1facf9fc 4176+ }
4177+
4178+ wbr = br->br_wbr;
4179+ do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
4180+ if (do_wh) {
1308ab2a 4181+ /* instead of WbrWhMustWriteLock(wbr) */
4182+ SiMustWriteLock(sb);
1facf9fc 4183+ for (i = 0; i < AuBrWh_Last; i++) {
4184+ dput(wbr->wbr_wh[i]);
4185+ wbr->wbr_wh[i] = NULL;
4186+ }
4187+ }
4188+
076b876e 4189+ err = test_children_busy(root, bindex, verbose);
1facf9fc 4190+ if (unlikely(err)) {
4191+ if (do_wh)
4192+ goto out_wh;
4193+ goto out;
4194+ }
4195+
4196+ err = 0;
076b876e
AM
4197+ if (to_free) {
4198+ /*
4199+ * now we confirmed the branch is deletable.
4200+ * let's free the remaining opened dirs on the branch.
4201+ */
4202+ di_write_unlock(root);
4203+ br_del_file(to_free, opened, br_id);
4204+ di_write_lock_child(root);
4205+ }
4206+
062440b3
AM
4207+ sysaufs_brs_del(sb, bindex); /* remove successors */
4208+ dbgaufs_xino_del(br); /* remove one */
4209+ au_br_do_del(sb, bindex, br);
4210+ sysaufs_brs_add(sb, bindex); /* append successors */
4211+ dbgaufs_brs_add(sb, bindex, /*topdown*/1); /* rename successors */
1facf9fc 4212+
1308ab2a 4213+ if (!bindex) {
5527c038 4214+ au_cpup_attr_all(d_inode(root), /*force*/1);
1308ab2a 4215+ sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
4216+ } else
5527c038 4217+ au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
1facf9fc 4218+ if (au_opt_test(mnt_flags, PLINK))
4219+ au_plink_half_refresh(sb, br_id);
4220+
1facf9fc 4221+ goto out; /* success */
4222+
4f0767ce 4223+out_wh:
1facf9fc 4224+ /* revert */
86dc4139 4225+ rerr = au_br_init_wh(sb, br, br->br_perm);
1facf9fc 4226+ if (rerr)
0c3ec466
AM
4227+ pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4228+ del->pathname, rerr);
4f0767ce 4229+out:
076b876e
AM
4230+ if (to_free)
4231+ au_farray_free(to_free, opened);
1facf9fc 4232+ return err;
4233+}
4234+
4235+/* ---------------------------------------------------------------------- */
4236+
027c5e7a
AM
4237+static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4238+{
4239+ int err;
5afbbe0d 4240+ aufs_bindex_t btop, bbot;
027c5e7a
AM
4241+ struct aufs_ibusy ibusy;
4242+ struct inode *inode, *h_inode;
4243+
4244+ err = -EPERM;
4245+ if (unlikely(!capable(CAP_SYS_ADMIN)))
4246+ goto out;
4247+
4248+ err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4249+ if (!err)
ba1aed25
AM
4250+ /* VERIFY_WRITE */
4251+ err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
027c5e7a
AM
4252+ if (unlikely(err)) {
4253+ err = -EFAULT;
4254+ AuTraceErr(err);
4255+ goto out;
4256+ }
4257+
4258+ err = -EINVAL;
4259+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d 4260+ if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
027c5e7a
AM
4261+ goto out_unlock;
4262+
4263+ err = 0;
4264+ ibusy.h_ino = 0; /* invalid */
4265+ inode = ilookup(sb, ibusy.ino);
4266+ if (!inode
4267+ || inode->i_ino == AUFS_ROOT_INO
5afbbe0d 4268+ || au_is_bad_inode(inode))
027c5e7a
AM
4269+ goto out_unlock;
4270+
4271+ ii_read_lock_child(inode);
5afbbe0d
AM
4272+ btop = au_ibtop(inode);
4273+ bbot = au_ibbot(inode);
4274+ if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
027c5e7a 4275+ h_inode = au_h_iptr(inode, ibusy.bindex);
5afbbe0d 4276+ if (h_inode && au_test_ibusy(inode, btop, bbot))
027c5e7a
AM
4277+ ibusy.h_ino = h_inode->i_ino;
4278+ }
4279+ ii_read_unlock(inode);
4280+ iput(inode);
4281+
4282+out_unlock:
4283+ si_read_unlock(sb);
4284+ if (!err) {
4285+ err = __put_user(ibusy.h_ino, &arg->h_ino);
4286+ if (unlikely(err)) {
4287+ err = -EFAULT;
4288+ AuTraceErr(err);
4289+ }
4290+ }
4291+out:
4292+ return err;
4293+}
4294+
4295+long au_ibusy_ioctl(struct file *file, unsigned long arg)
4296+{
2000de60 4297+ return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
027c5e7a
AM
4298+}
4299+
4300+#ifdef CONFIG_COMPAT
4301+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4302+{
2000de60 4303+ return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
027c5e7a
AM
4304+}
4305+#endif
4306+
4307+/* ---------------------------------------------------------------------- */
4308+
1facf9fc 4309+/*
4310+ * change a branch permission
4311+ */
4312+
dece6358
AM
4313+static void au_warn_ima(void)
4314+{
4315+#ifdef CONFIG_IMA
1308ab2a 4316+ /* since it doesn't support mark_files_ro() */
027c5e7a 4317+ AuWarn1("RW -> RO makes IMA to produce wrong message\n");
dece6358
AM
4318+#endif
4319+}
4320+
1facf9fc 4321+static int do_need_sigen_inc(int a, int b)
4322+{
4323+ return au_br_whable(a) && !au_br_whable(b);
4324+}
4325+
4326+static int need_sigen_inc(int old, int new)
4327+{
4328+ return do_need_sigen_inc(old, new)
4329+ || do_need_sigen_inc(new, old);
4330+}
4331+
4332+static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4333+{
7f207e10 4334+ int err, do_warn;
027c5e7a 4335+ unsigned int mnt_flags;
7f207e10 4336+ unsigned long long ull, max;
e49829fe 4337+ aufs_bindex_t br_id;
38d290e6 4338+ unsigned char verbose, writer;
7f207e10 4339+ struct file *file, *hf, **array;
e49829fe 4340+ struct au_hfile *hfile;
1facf9fc 4341+
027c5e7a
AM
4342+ mnt_flags = au_mntflags(sb);
4343+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
4344+
7f207e10
AM
4345+ array = au_farray_alloc(sb, &max);
4346+ err = PTR_ERR(array);
4347+ if (IS_ERR(array))
1facf9fc 4348+ goto out;
4349+
7f207e10 4350+ do_warn = 0;
e49829fe 4351+ br_id = au_sbr_id(sb, bindex);
7f207e10
AM
4352+ for (ull = 0; ull < max; ull++) {
4353+ file = array[ull];
076b876e
AM
4354+ if (unlikely(!file))
4355+ break;
1facf9fc 4356+
523b37e3 4357+ /* AuDbg("%pD\n", file); */
1facf9fc 4358+ fi_read_lock(file);
4359+ if (unlikely(au_test_mmapped(file))) {
4360+ err = -EBUSY;
523b37e3 4361+ AuVerbose(verbose, "mmapped %pD\n", file);
7f207e10 4362+ AuDbgFile(file);
1facf9fc 4363+ FiMustNoWaiters(file);
4364+ fi_read_unlock(file);
7f207e10 4365+ goto out_array;
1facf9fc 4366+ }
4367+
e49829fe
JR
4368+ hfile = &au_fi(file)->fi_htop;
4369+ hf = hfile->hf_file;
7e9cd9fe 4370+ if (!d_is_reg(file->f_path.dentry)
1facf9fc 4371+ || !(file->f_mode & FMODE_WRITE)
e49829fe 4372+ || hfile->hf_br->br_id != br_id
7f207e10
AM
4373+ || !(hf->f_mode & FMODE_WRITE))
4374+ array[ull] = NULL;
4375+ else {
4376+ do_warn = 1;
4377+ get_file(file);
1facf9fc 4378+ }
4379+
1facf9fc 4380+ FiMustNoWaiters(file);
4381+ fi_read_unlock(file);
7f207e10
AM
4382+ fput(file);
4383+ }
1facf9fc 4384+
4385+ err = 0;
7f207e10 4386+ if (do_warn)
dece6358 4387+ au_warn_ima();
7f207e10
AM
4388+
4389+ for (ull = 0; ull < max; ull++) {
4390+ file = array[ull];
4391+ if (!file)
4392+ continue;
4393+
1facf9fc 4394+ /* todo: already flushed? */
523b37e3
AM
4395+ /*
4396+ * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4397+ * approach which resets f_mode and calls mnt_drop_write() and
4398+ * file_release_write() for each file, because the branch
4399+ * attribute in aufs world is totally different from the native
4400+ * fs rw/ro mode.
4401+ */
7f207e10
AM
4402+ /* fi_read_lock(file); */
4403+ hfile = &au_fi(file)->fi_htop;
4404+ hf = hfile->hf_file;
4405+ /* fi_read_unlock(file); */
027c5e7a 4406+ spin_lock(&hf->f_lock);
38d290e6
JR
4407+ writer = !!(hf->f_mode & FMODE_WRITER);
4408+ hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
027c5e7a 4409+ spin_unlock(&hf->f_lock);
38d290e6
JR
4410+ if (writer) {
4411+ put_write_access(file_inode(hf));
c06a8ce3 4412+ __mnt_drop_write(hf->f_path.mnt);
1facf9fc 4413+ }
4414+ }
4415+
7f207e10
AM
4416+out_array:
4417+ au_farray_free(array, max);
4f0767ce 4418+out:
7f207e10 4419+ AuTraceErr(err);
1facf9fc 4420+ return err;
4421+}
4422+
4423+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4424+ int *do_refresh)
1facf9fc 4425+{
4426+ int err, rerr;
4427+ aufs_bindex_t bindex;
4428+ struct dentry *root;
4429+ struct au_branch *br;
076b876e 4430+ struct au_br_fhsm *bf;
1facf9fc 4431+
4432+ root = sb->s_root;
1facf9fc 4433+ bindex = au_find_dbindex(root, mod->h_root);
4434+ if (bindex < 0) {
4435+ if (remount)
4436+ return 0; /* success */
4437+ err = -ENOENT;
4a4d8108 4438+ pr_err("%s no such branch\n", mod->path);
1facf9fc 4439+ goto out;
4440+ }
4441+ AuDbg("bindex b%d\n", bindex);
4442+
5527c038 4443+ err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
1facf9fc 4444+ if (unlikely(err))
4445+ goto out;
4446+
4447+ br = au_sbr(sb, bindex);
86dc4139 4448+ AuDebugOn(mod->h_root != au_br_dentry(br));
1facf9fc 4449+ if (br->br_perm == mod->perm)
4450+ return 0; /* success */
4451+
076b876e
AM
4452+ /* pre-allocate for non-fhsm --> fhsm */
4453+ bf = NULL;
4454+ if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4455+ err = au_fhsm_br_alloc(br);
4456+ if (unlikely(err))
4457+ goto out;
4458+ bf = br->br_fhsm;
4459+ br->br_fhsm = NULL;
4460+ }
4461+
1facf9fc 4462+ if (au_br_writable(br->br_perm)) {
4463+ /* remove whiteout base */
86dc4139 4464+ err = au_br_init_wh(sb, br, mod->perm);
1facf9fc 4465+ if (unlikely(err))
076b876e 4466+ goto out_bf;
1facf9fc 4467+
4468+ if (!au_br_writable(mod->perm)) {
4469+ /* rw --> ro, file might be mmapped */
4470+ DiMustNoWaiters(root);
5527c038 4471+ IiMustNoWaiters(d_inode(root));
1facf9fc 4472+ di_write_unlock(root);
4473+ err = au_br_mod_files_ro(sb, bindex);
4474+ /* aufs_write_lock() calls ..._child() */
4475+ di_write_lock_child(root);
4476+
4477+ if (unlikely(err)) {
4478+ rerr = -ENOMEM;
be52b249 4479+ br->br_wbr = kzalloc(sizeof(*br->br_wbr),
1facf9fc 4480+ GFP_NOFS);
86dc4139
AM
4481+ if (br->br_wbr)
4482+ rerr = au_wbr_init(br, sb, br->br_perm);
1facf9fc 4483+ if (unlikely(rerr)) {
4484+ AuIOErr("nested error %d (%d)\n",
4485+ rerr, err);
4486+ br->br_perm = mod->perm;
4487+ }
4488+ }
4489+ }
4490+ } else if (au_br_writable(mod->perm)) {
4491+ /* ro --> rw */
4492+ err = -ENOMEM;
be52b249 4493+ br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
1facf9fc 4494+ if (br->br_wbr) {
86dc4139 4495+ err = au_wbr_init(br, sb, mod->perm);
1facf9fc 4496+ if (unlikely(err)) {
9f237c51 4497+ au_kfree_rcu(br->br_wbr);
1facf9fc 4498+ br->br_wbr = NULL;
4499+ }
4500+ }
4501+ }
076b876e
AM
4502+ if (unlikely(err))
4503+ goto out_bf;
4504+
4505+ if (au_br_fhsm(br->br_perm)) {
4506+ if (!au_br_fhsm(mod->perm)) {
4507+ /* fhsm --> non-fhsm */
4508+ au_br_fhsm_fin(br->br_fhsm);
9f237c51 4509+ au_kfree_rcu(br->br_fhsm);
076b876e
AM
4510+ br->br_fhsm = NULL;
4511+ }
4512+ } else if (au_br_fhsm(mod->perm))
4513+ /* non-fhsm --> fhsm */
4514+ br->br_fhsm = bf;
4515+
076b876e
AM
4516+ *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4517+ br->br_perm = mod->perm;
4518+ goto out; /* success */
1facf9fc 4519+
076b876e 4520+out_bf:
9f237c51 4521+ au_kfree_try_rcu(bf);
076b876e
AM
4522+out:
4523+ AuTraceErr(err);
4524+ return err;
4525+}
4526+
4527+/* ---------------------------------------------------------------------- */
4528+
4529+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4530+{
4531+ int err;
4532+ struct kstatfs kstfs;
4533+
4534+ err = vfs_statfs(&br->br_path, &kstfs);
1facf9fc 4535+ if (!err) {
076b876e
AM
4536+ stfs->f_blocks = kstfs.f_blocks;
4537+ stfs->f_bavail = kstfs.f_bavail;
4538+ stfs->f_files = kstfs.f_files;
4539+ stfs->f_ffree = kstfs.f_ffree;
1facf9fc 4540+ }
4541+
1facf9fc 4542+ return err;
4543+}
7f207e10
AM
4544diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4545--- /usr/share/empty/fs/aufs/branch.h 1970-01-01 01:00:00.000000000 +0100
ba1aed25 4546+++ linux/fs/aufs/branch.h 2019-03-05 12:13:00.135890907 +0100
9f237c51 4547@@ -0,0 +1,365 @@
062440b3 4548+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 4549+/*
ba1aed25 4550+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 4551+ *
4552+ * This program, aufs is free software; you can redistribute it and/or modify
4553+ * it under the terms of the GNU General Public License as published by
4554+ * the Free Software Foundation; either version 2 of the License, or
4555+ * (at your option) any later version.
dece6358
AM
4556+ *
4557+ * This program is distributed in the hope that it will be useful,
4558+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4559+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4560+ * GNU General Public License for more details.
4561+ *
4562+ * You should have received a copy of the GNU General Public License
523b37e3 4563+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 4564+ */
4565+
4566+/*
4567+ * branch filesystems and xino for them
4568+ */
4569+
4570+#ifndef __AUFS_BRANCH_H__
4571+#define __AUFS_BRANCH_H__
4572+
4573+#ifdef __KERNEL__
4574+
1facf9fc 4575+#include <linux/mount.h>
8b6a4947 4576+#include "dirren.h"
4a4d8108 4577+#include "dynop.h"
acd2b654 4578+#include "lcnt.h"
1facf9fc 4579+#include "rwsem.h"
4580+#include "super.h"
4581+
4582+/* ---------------------------------------------------------------------- */
4583+
4584+/* a xino file */
062440b3 4585+struct au_xino {
acd2b654
AM
4586+ struct file **xi_file;
4587+ unsigned int xi_nfile;
4588+
521ced18
JR
4589+ struct {
4590+ spinlock_t spin;
4591+ ino_t *array;
4592+ int total;
4593+ /* reserved for future use */
4594+ /* unsigned long *bitmap; */
4595+ wait_queue_head_t wqh;
4596+ } xi_nondir;
1facf9fc 4597+
acd2b654 4598+ struct mutex xi_mtx; /* protects xi_file array */
9f237c51 4599+ struct hlist_bl_head xi_writing;
acd2b654 4600+
062440b3 4601+ atomic_t xi_truncating;
1facf9fc 4602+
062440b3 4603+ struct kref xi_kref;
1facf9fc 4604+};
4605+
076b876e
AM
4606+/* File-based Hierarchical Storage Management */
4607+struct au_br_fhsm {
4608+#ifdef CONFIG_AUFS_FHSM
4609+ struct mutex bf_lock;
4610+ unsigned long bf_jiffy;
4611+ struct aufs_stfs bf_stfs;
4612+ int bf_readable;
4613+#endif
4614+};
4615+
1facf9fc 4616+/* members for writable branch only */
4617+enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4618+struct au_wbr {
dece6358 4619+ struct au_rwsem wbr_wh_rwsem;
1facf9fc 4620+ struct dentry *wbr_wh[AuBrWh_Last];
4a4d8108 4621+ atomic_t wbr_wh_running;
1facf9fc 4622+#define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
4623+#define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
4624+#define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
4625+
4626+ /* mfs mode */
4627+ unsigned long long wbr_bytes;
4628+};
4629+
4a4d8108
AM
4630+/* ext2 has 3 types of operations at least, ext3 has 4 */
4631+#define AuBrDynOp (AuDyLast * 4)
4632+
1716fcea
AM
4633+#ifdef CONFIG_AUFS_HFSNOTIFY
4634+/* support for asynchronous destruction */
4635+struct au_br_hfsnotify {
4636+ struct fsnotify_group *hfsn_group;
4637+};
4638+#endif
4639+
392086de
AM
4640+/* sysfs entries */
4641+struct au_brsysfs {
4642+ char name[16];
4643+ struct attribute attr;
4644+};
4645+
4646+enum {
4647+ AuBrSysfs_BR,
4648+ AuBrSysfs_BRID,
4649+ AuBrSysfs_Last
4650+};
4651+
1facf9fc 4652+/* protected by superblock rwsem */
4653+struct au_branch {
062440b3 4654+ struct au_xino *br_xino;
1facf9fc 4655+
4656+ aufs_bindex_t br_id;
4657+
4658+ int br_perm;
86dc4139 4659+ struct path br_path;
4a4d8108
AM
4660+ spinlock_t br_dykey_lock;
4661+ struct au_dykey *br_dykey[AuBrDynOp];
acd2b654
AM
4662+ au_lcnt_t br_nfiles; /* opened files */
4663+ au_lcnt_t br_count; /* in-use for other */
1facf9fc 4664+
4665+ struct au_wbr *br_wbr;
076b876e 4666+ struct au_br_fhsm *br_fhsm;
1facf9fc 4667+
027c5e7a 4668+#ifdef CONFIG_AUFS_HFSNOTIFY
1716fcea 4669+ struct au_br_hfsnotify *br_hfsn;
027c5e7a
AM
4670+#endif
4671+
1facf9fc 4672+#ifdef CONFIG_SYSFS
392086de
AM
4673+ /* entries under sysfs per mount-point */
4674+ struct au_brsysfs br_sysfs[AuBrSysfs_Last];
1facf9fc 4675+#endif
8b6a4947 4676+
062440b3
AM
4677+#ifdef CONFIG_DEBUG_FS
4678+ struct dentry *br_dbgaufs; /* xino */
4679+#endif
4680+
8b6a4947 4681+ struct au_dr_br br_dirren;
1facf9fc 4682+};
4683+
4684+/* ---------------------------------------------------------------------- */
4685+
86dc4139
AM
4686+static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4687+{
4688+ return br->br_path.mnt;
4689+}
4690+
4691+static inline struct dentry *au_br_dentry(struct au_branch *br)
4692+{
4693+ return br->br_path.dentry;
4694+}
4695+
4696+static inline struct super_block *au_br_sb(struct au_branch *br)
4697+{
4698+ return au_br_mnt(br)->mnt_sb;
4699+}
4700+
1facf9fc 4701+static inline int au_br_rdonly(struct au_branch *br)
4702+{
8b6a4947 4703+ return (sb_rdonly(au_br_sb(br))
1facf9fc 4704+ || !au_br_writable(br->br_perm))
4705+ ? -EROFS : 0;
4706+}
4707+
4a4d8108 4708+static inline int au_br_hnotifyable(int brperm __maybe_unused)
1facf9fc 4709+{
4a4d8108 4710+#ifdef CONFIG_AUFS_HNOTIFY
1e00d052 4711+ return !(brperm & AuBrPerm_RR);
1facf9fc 4712+#else
4713+ return 0;
4714+#endif
4715+}
4716+
b912730e
AM
4717+static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4718+{
4719+ int err, exec_flag;
4720+
4721+ err = 0;
4722+ exec_flag = oflag & __FMODE_EXEC;
79b8bda9 4723+ if (unlikely(exec_flag && path_noexec(&br->br_path)))
b912730e
AM
4724+ err = -EACCES;
4725+
4726+ return err;
4727+}
4728+
062440b3
AM
4729+static inline void au_xino_get(struct au_branch *br)
4730+{
4731+ struct au_xino *xi;
4732+
4733+ xi = br->br_xino;
4734+ if (xi)
4735+ kref_get(&xi->xi_kref);
4736+}
4737+
4738+static inline int au_xino_count(struct au_branch *br)
4739+{
4740+ int v;
4741+ struct au_xino *xi;
4742+
4743+ v = 0;
4744+ xi = br->br_xino;
4745+ if (xi)
4746+ v = kref_read(&xi->xi_kref);
4747+
4748+ return v;
4749+}
4750+
1facf9fc 4751+/* ---------------------------------------------------------------------- */
4752+
4753+/* branch.c */
4754+struct au_sbinfo;
4755+void au_br_free(struct au_sbinfo *sinfo);
4756+int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4757+struct au_opt_add;
4758+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4759+struct au_opt_del;
4760+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
027c5e7a
AM
4761+long au_ibusy_ioctl(struct file *file, unsigned long arg);
4762+#ifdef CONFIG_COMPAT
4763+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4764+#endif
1facf9fc 4765+struct au_opt_mod;
4766+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4767+ int *do_refresh);
076b876e
AM
4768+struct aufs_stfs;
4769+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
1facf9fc 4770+
4771+/* xino.c */
4772+static const loff_t au_loff_max = LLONG_MAX;
4773+
acd2b654 4774+aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
062440b3
AM
4775+struct file *au_xino_create(struct super_block *sb, char *fpath, int silent);
4776+struct file *au_xino_create2(struct super_block *sb, struct path *base,
4777+ struct file *copy_src);
acd2b654
AM
4778+struct au_xi_new {
4779+ struct au_xino *xi; /* switch between xino and xigen */
4780+ int idx;
4781+ struct path *base;
4782+ struct file *copy_src;
4783+};
4784+struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
062440b3
AM
4785+
4786+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4787+ ino_t *ino);
4788+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4789+ ino_t ino);
5527c038 4790+ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
1facf9fc 4791+ loff_t *pos);
5527c038
JR
4792+ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
4793+ size_t size, loff_t *pos);
062440b3
AM
4794+
4795+int au_xib_trunc(struct super_block *sb);
acd2b654 4796+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
1facf9fc 4797+
acd2b654 4798+struct au_xino *au_xino_alloc(unsigned int nfile);
062440b3 4799+int au_xino_put(struct au_branch *br);
acd2b654 4800+struct file *au_xino_file1(struct au_xino *xi);
062440b3 4801+
1facf9fc 4802+struct au_opt_xino;
1facf9fc 4803+void au_xino_clr(struct super_block *sb);
062440b3 4804+int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
1facf9fc 4805+struct file *au_xino_def(struct super_block *sb);
062440b3
AM
4806+int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4807+ struct path *base);
4808+
4809+ino_t au_xino_new_ino(struct super_block *sb);
4810+void au_xino_delete_inode(struct inode *inode, const int unlinked);
1facf9fc 4811+
521ced18
JR
4812+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4813+ ino_t h_ino, int idx);
4814+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4815+ int *idx);
4816+
062440b3
AM
4817+int au_xino_path(struct seq_file *seq, struct file *file);
4818+
1facf9fc 4819+/* ---------------------------------------------------------------------- */
4820+
acd2b654
AM
4821+/* @idx is signed to accept -1 meaning the first file */
4822+static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4823+{
4824+ struct file *file;
4825+
4826+ file = NULL;
4827+ if (!xi)
4828+ goto out;
4829+
4830+ if (idx >= 0) {
4831+ if (idx < xi->xi_nfile)
4832+ file = xi->xi_file[idx];
4833+ } else
4834+ file = au_xino_file1(xi);
4835+
4836+out:
4837+ return file;
4838+}
4839+
4840+/* ---------------------------------------------------------------------- */
4841+
1facf9fc 4842+/* Superblock to branch */
4843+static inline
4844+aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4845+{
4846+ return au_sbr(sb, bindex)->br_id;
4847+}
4848+
4849+static inline
4850+struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4851+{
86dc4139 4852+ return au_br_mnt(au_sbr(sb, bindex));
1facf9fc 4853+}
4854+
4855+static inline
4856+struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4857+{
86dc4139 4858+ return au_br_sb(au_sbr(sb, bindex));
1facf9fc 4859+}
4860+
1facf9fc 4861+static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4862+{
4863+ return au_sbr(sb, bindex)->br_perm;
4864+}
4865+
4866+static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4867+{
4868+ return au_br_whable(au_sbr_perm(sb, bindex));
4869+}
4870+
4871+/* ---------------------------------------------------------------------- */
4872+
8b6a4947
AM
4873+#define wbr_wh_read_lock(wbr) au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4874+#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4875+#define wbr_wh_read_trylock(wbr) au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4876+#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
1facf9fc 4877+/*
8b6a4947
AM
4878+#define wbr_wh_read_trylock_nested(wbr) \
4879+ au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4880+#define wbr_wh_write_trylock_nested(wbr) \
4881+ au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4882+*/
1facf9fc 4883+
8b6a4947
AM
4884+#define wbr_wh_read_unlock(wbr) au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4885+#define wbr_wh_write_unlock(wbr) au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4886+#define wbr_wh_downgrade_lock(wbr) au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4887+
4888+#define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4889+#define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4890+#define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
dece6358 4891+
076b876e
AM
4892+/* ---------------------------------------------------------------------- */
4893+
4894+#ifdef CONFIG_AUFS_FHSM
4895+static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4896+{
4897+ mutex_init(&brfhsm->bf_lock);
4898+ brfhsm->bf_jiffy = 0;
4899+ brfhsm->bf_readable = 0;
4900+}
4901+
4902+static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4903+{
4904+ mutex_destroy(&brfhsm->bf_lock);
4905+}
4906+#else
4907+AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4908+AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4909+#endif
4910+
1facf9fc 4911+#endif /* __KERNEL__ */
4912+#endif /* __AUFS_BRANCH_H__ */
7f207e10
AM
4913diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4914--- /usr/share/empty/fs/aufs/conf.mk 1970-01-01 01:00:00.000000000 +0100
b00004a5 4915+++ linux/fs/aufs/conf.mk 2018-06-04 09:08:09.181412645 +0200
2121bcd9
AM
4916@@ -0,0 +1,40 @@
4917+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
4918+
4919+AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4920+
4921+define AuConf
4922+ifdef ${1}
4923+AuConfStr += ${1}=${${1}}
4924+endif
4925+endef
4926+
b752ccd1 4927+AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
e49829fe 4928+ SBILIST \
7f207e10 4929+ HNOTIFY HFSNOTIFY \
4a4d8108 4930+ EXPORT INO_T_64 \
c1595e42 4931+ XATTR \
076b876e 4932+ FHSM \
4a4d8108 4933+ RDU \
8b6a4947 4934+ DIRREN \
4a4d8108
AM
4935+ SHWH \
4936+ BR_RAMFS \
4937+ BR_FUSE POLL \
4938+ BR_HFSPLUS \
4939+ BDEV_LOOP \
b752ccd1
AM
4940+ DEBUG MAGIC_SYSRQ
4941+$(foreach i, ${AuConfAll}, \
4a4d8108
AM
4942+ $(eval $(call AuConf,CONFIG_AUFS_${i})))
4943+
4944+AuConfName = ${obj}/conf.str
4945+${AuConfName}.tmp: FORCE
4946+ @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4947+${AuConfName}: ${AuConfName}.tmp
4948+ @diff -q $< $@ > /dev/null 2>&1 || { \
4949+ echo ' GEN ' $@; \
4950+ cp -p $< $@; \
4951+ }
4952+FORCE:
4953+clean-files += ${AuConfName} ${AuConfName}.tmp
4954+${obj}/sysfs.o: ${AuConfName}
b752ccd1
AM
4955+
4956+-include ${srctree}/${src}/conf_priv.mk
7f207e10
AM
4957diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4958--- /usr/share/empty/fs/aufs/cpup.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 4959+++ linux/fs/aufs/cpup.c 2019-03-05 12:13:00.135890907 +0100
9f237c51 4960@@ -0,0 +1,1458 @@
cd7a4cd9 4961+// SPDX-License-Identifier: GPL-2.0
1facf9fc 4962+/*
ba1aed25 4963+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 4964+ *
4965+ * This program, aufs is free software; you can redistribute it and/or modify
4966+ * it under the terms of the GNU General Public License as published by
4967+ * the Free Software Foundation; either version 2 of the License, or
4968+ * (at your option) any later version.
dece6358
AM
4969+ *
4970+ * This program is distributed in the hope that it will be useful,
4971+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4972+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4973+ * GNU General Public License for more details.
4974+ *
4975+ * You should have received a copy of the GNU General Public License
523b37e3 4976+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 4977+ */
4978+
4979+/*
4980+ * copy-up functions, see wbr_policy.c for copy-down
4981+ */
4982+
4983+#include <linux/fs_stack.h>
dece6358 4984+#include <linux/mm.h>
8cdd5066 4985+#include <linux/task_work.h>
1facf9fc 4986+#include "aufs.h"
4987+
86dc4139 4988+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
1facf9fc 4989+{
4990+ const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
367653fa 4991+ | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
1facf9fc 4992+
86dc4139
AM
4993+ BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4994+
4995+ dst->i_flags |= iflags & ~mask;
1facf9fc 4996+ if (au_test_fs_notime(dst->i_sb))
4997+ dst->i_flags |= S_NOATIME | S_NOCMTIME;
4998+}
4999+
5000+void au_cpup_attr_timesizes(struct inode *inode)
5001+{
5002+ struct inode *h_inode;
5003+
5afbbe0d 5004+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 5005+ fsstack_copy_attr_times(inode, h_inode);
4a4d8108 5006+ fsstack_copy_inode_size(inode, h_inode);
1facf9fc 5007+}
5008+
5009+void au_cpup_attr_nlink(struct inode *inode, int force)
5010+{
5011+ struct inode *h_inode;
5012+ struct super_block *sb;
5afbbe0d 5013+ aufs_bindex_t bindex, bbot;
1facf9fc 5014+
5015+ sb = inode->i_sb;
5afbbe0d 5016+ bindex = au_ibtop(inode);
1facf9fc 5017+ h_inode = au_h_iptr(inode, bindex);
5018+ if (!force
5019+ && !S_ISDIR(h_inode->i_mode)
5020+ && au_opt_test(au_mntflags(sb), PLINK)
5021+ && au_plink_test(inode))
5022+ return;
5023+
7eafdf33
AM
5024+ /*
5025+ * 0 can happen in revalidating.
38d290e6
JR
5026+ * h_inode->i_mutex may not be held here, but it is harmless since once
5027+ * i_nlink reaches 0, it will never become positive except O_TMPFILE
5028+ * case.
5029+ * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
5030+ * the incorrect link count.
7eafdf33 5031+ */
92d182d2 5032+ set_nlink(inode, h_inode->i_nlink);
1facf9fc 5033+
5034+ /*
5035+ * fewer nlink makes find(1) noisy, but larger nlink doesn't.
5036+ * it may includes whplink directory.
5037+ */
5038+ if (S_ISDIR(h_inode->i_mode)) {
5afbbe0d
AM
5039+ bbot = au_ibbot(inode);
5040+ for (bindex++; bindex <= bbot; bindex++) {
1facf9fc 5041+ h_inode = au_h_iptr(inode, bindex);
5042+ if (h_inode)
5043+ au_add_nlink(inode, h_inode);
5044+ }
5045+ }
5046+}
5047+
5048+void au_cpup_attr_changeable(struct inode *inode)
5049+{
5050+ struct inode *h_inode;
5051+
5afbbe0d 5052+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 5053+ inode->i_mode = h_inode->i_mode;
5054+ inode->i_uid = h_inode->i_uid;
5055+ inode->i_gid = h_inode->i_gid;
5056+ au_cpup_attr_timesizes(inode);
86dc4139 5057+ au_cpup_attr_flags(inode, h_inode->i_flags);
1facf9fc 5058+}
5059+
5060+void au_cpup_igen(struct inode *inode, struct inode *h_inode)
5061+{
5062+ struct au_iinfo *iinfo = au_ii(inode);
5063+
1308ab2a 5064+ IiMustWriteLock(inode);
5065+
1facf9fc 5066+ iinfo->ii_higen = h_inode->i_generation;
5067+ iinfo->ii_hsb1 = h_inode->i_sb;
5068+}
5069+
5070+void au_cpup_attr_all(struct inode *inode, int force)
5071+{
5072+ struct inode *h_inode;
5073+
5afbbe0d 5074+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 5075+ au_cpup_attr_changeable(inode);
5076+ if (inode->i_nlink > 0)
5077+ au_cpup_attr_nlink(inode, force);
5078+ inode->i_rdev = h_inode->i_rdev;
5079+ inode->i_blkbits = h_inode->i_blkbits;
5080+ au_cpup_igen(inode, h_inode);
5081+}
5082+
5083+/* ---------------------------------------------------------------------- */
5084+
5085+/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
5086+
5087+/* keep the timestamps of the parent dir when cpup */
5088+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
5089+ struct path *h_path)
5090+{
5091+ struct inode *h_inode;
5092+
5093+ dt->dt_dentry = dentry;
5094+ dt->dt_h_path = *h_path;
5527c038 5095+ h_inode = d_inode(h_path->dentry);
1facf9fc 5096+ dt->dt_atime = h_inode->i_atime;
5097+ dt->dt_mtime = h_inode->i_mtime;
5098+ /* smp_mb(); */
5099+}
5100+
5101+void au_dtime_revert(struct au_dtime *dt)
5102+{
5103+ struct iattr attr;
5104+ int err;
5105+
5106+ attr.ia_atime = dt->dt_atime;
5107+ attr.ia_mtime = dt->dt_mtime;
5108+ attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
5109+ | ATTR_ATIME | ATTR_ATIME_SET;
5110+
523b37e3
AM
5111+ /* no delegation since this is a directory */
5112+ err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
1facf9fc 5113+ if (unlikely(err))
0c3ec466 5114+ pr_warn("restoring timestamps failed(%d). ignored\n", err);
1facf9fc 5115+}
5116+
5117+/* ---------------------------------------------------------------------- */
5118+
86dc4139
AM
5119+/* internal use only */
5120+struct au_cpup_reg_attr {
5121+ int valid;
5122+ struct kstat st;
5123+ unsigned int iflags; /* inode->i_flags */
5124+};
5125+
1facf9fc 5126+static noinline_for_stack
86dc4139
AM
5127+int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
5128+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5129+{
c1595e42 5130+ int err, sbits, icex;
7e9cd9fe
AM
5131+ unsigned int mnt_flags;
5132+ unsigned char verbose;
1facf9fc 5133+ struct iattr ia;
5134+ struct path h_path;
1308ab2a 5135+ struct inode *h_isrc, *h_idst;
86dc4139 5136+ struct kstat *h_st;
c1595e42 5137+ struct au_branch *br;
1facf9fc 5138+
5139+ h_path.dentry = au_h_dptr(dst, bindex);
5527c038 5140+ h_idst = d_inode(h_path.dentry);
c1595e42
JR
5141+ br = au_sbr(dst->d_sb, bindex);
5142+ h_path.mnt = au_br_mnt(br);
5527c038 5143+ h_isrc = d_inode(h_src);
1308ab2a 5144+ ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
1facf9fc 5145+ | ATTR_ATIME | ATTR_MTIME
5146+ | ATTR_ATIME_SET | ATTR_MTIME_SET;
86dc4139
AM
5147+ if (h_src_attr && h_src_attr->valid) {
5148+ h_st = &h_src_attr->st;
5149+ ia.ia_uid = h_st->uid;
5150+ ia.ia_gid = h_st->gid;
5151+ ia.ia_atime = h_st->atime;
5152+ ia.ia_mtime = h_st->mtime;
5153+ if (h_idst->i_mode != h_st->mode
5154+ && !S_ISLNK(h_idst->i_mode)) {
5155+ ia.ia_valid |= ATTR_MODE;
5156+ ia.ia_mode = h_st->mode;
5157+ }
5158+ sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
5159+ au_cpup_attr_flags(h_idst, h_src_attr->iflags);
5160+ } else {
5161+ ia.ia_uid = h_isrc->i_uid;
5162+ ia.ia_gid = h_isrc->i_gid;
5163+ ia.ia_atime = h_isrc->i_atime;
5164+ ia.ia_mtime = h_isrc->i_mtime;
5165+ if (h_idst->i_mode != h_isrc->i_mode
5166+ && !S_ISLNK(h_idst->i_mode)) {
5167+ ia.ia_valid |= ATTR_MODE;
5168+ ia.ia_mode = h_isrc->i_mode;
5169+ }
5170+ sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
5171+ au_cpup_attr_flags(h_idst, h_isrc->i_flags);
1308ab2a 5172+ }
523b37e3
AM
5173+ /* no delegation since it is just created */
5174+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 5175+
5176+ /* is this nfs only? */
5177+ if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
5178+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
5179+ ia.ia_mode = h_isrc->i_mode;
523b37e3 5180+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 5181+ }
5182+
c1595e42 5183+ icex = br->br_perm & AuBrAttr_ICEX;
7e9cd9fe
AM
5184+ if (!err) {
5185+ mnt_flags = au_mntflags(dst->d_sb);
5186+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
5187+ err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
5188+ }
c1595e42 5189+
1facf9fc 5190+ return err;
5191+}
5192+
5193+/* ---------------------------------------------------------------------- */
5194+
5195+static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
5196+ char *buf, unsigned long blksize)
5197+{
5198+ int err;
5199+ size_t sz, rbytes, wbytes;
5200+ unsigned char all_zero;
5201+ char *p, *zp;
febd17d6 5202+ struct inode *h_inode;
1facf9fc 5203+ /* reduce stack usage */
5204+ struct iattr *ia;
5205+
5206+ zp = page_address(ZERO_PAGE(0));
5207+ if (unlikely(!zp))
5208+ return -ENOMEM; /* possible? */
5209+
5210+ err = 0;
5211+ all_zero = 0;
5212+ while (len) {
5213+ AuDbg("len %lld\n", len);
5214+ sz = blksize;
5215+ if (len < blksize)
5216+ sz = len;
5217+
5218+ rbytes = 0;
5219+ /* todo: signal_pending? */
5220+ while (!rbytes || err == -EAGAIN || err == -EINTR) {
5221+ rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5222+ err = rbytes;
5223+ }
5224+ if (unlikely(err < 0))
5225+ break;
5226+
5227+ all_zero = 0;
5228+ if (len >= rbytes && rbytes == blksize)
5229+ all_zero = !memcmp(buf, zp, rbytes);
5230+ if (!all_zero) {
5231+ wbytes = rbytes;
5232+ p = buf;
5233+ while (wbytes) {
5234+ size_t b;
5235+
5236+ b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5237+ err = b;
5238+ /* todo: signal_pending? */
5239+ if (unlikely(err == -EAGAIN || err == -EINTR))
5240+ continue;
5241+ if (unlikely(err < 0))
5242+ break;
5243+ wbytes -= b;
5244+ p += b;
5245+ }
392086de
AM
5246+ if (unlikely(err < 0))
5247+ break;
1facf9fc 5248+ } else {
5249+ loff_t res;
5250+
5251+ AuLabel(hole);
5252+ res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5253+ err = res;
5254+ if (unlikely(res < 0))
5255+ break;
5256+ }
5257+ len -= rbytes;
5258+ err = 0;
5259+ }
5260+
5261+ /* the last block may be a hole */
5262+ if (!err && all_zero) {
5263+ AuLabel(last hole);
5264+
5265+ err = 1;
2000de60 5266+ if (au_test_nfs(dst->f_path.dentry->d_sb)) {
1facf9fc 5267+ /* nfs requires this step to make last hole */
5268+ /* is this only nfs? */
5269+ do {
5270+ /* todo: signal_pending? */
5271+ err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5272+ } while (err == -EAGAIN || err == -EINTR);
5273+ if (err == 1)
5274+ dst->f_pos--;
5275+ }
5276+
5277+ if (err == 1) {
5278+ ia = (void *)buf;
5279+ ia->ia_size = dst->f_pos;
5280+ ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5281+ ia->ia_file = dst;
febd17d6
JR
5282+ h_inode = file_inode(dst);
5283+ inode_lock_nested(h_inode, AuLsc_I_CHILD2);
523b37e3
AM
5284+ /* no delegation since it is just created */
5285+ err = vfsub_notify_change(&dst->f_path, ia,
5286+ /*delegated*/NULL);
febd17d6 5287+ inode_unlock(h_inode);
1facf9fc 5288+ }
5289+ }
5290+
5291+ return err;
5292+}
5293+
5294+int au_copy_file(struct file *dst, struct file *src, loff_t len)
5295+{
5296+ int err;
5297+ unsigned long blksize;
5298+ unsigned char do_kfree;
5299+ char *buf;
9f237c51 5300+ struct super_block *h_sb;
1facf9fc 5301+
5302+ err = -ENOMEM;
9f237c51
AM
5303+ h_sb = file_inode(dst)->i_sb;
5304+ blksize = h_sb->s_blocksize;
1facf9fc 5305+ if (!blksize || PAGE_SIZE < blksize)
5306+ blksize = PAGE_SIZE;
5307+ AuDbg("blksize %lu\n", blksize);
5308+ do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5309+ if (do_kfree)
5310+ buf = kmalloc(blksize, GFP_NOFS);
5311+ else
5312+ buf = (void *)__get_free_page(GFP_NOFS);
5313+ if (unlikely(!buf))
5314+ goto out;
5315+
5316+ if (len > (1 << 22))
5317+ AuDbg("copying a large file %lld\n", (long long)len);
5318+
5319+ src->f_pos = 0;
5320+ dst->f_pos = 0;
5321+ err = au_do_copy_file(dst, src, len, buf, blksize);
9f237c51
AM
5322+ if (do_kfree) {
5323+ AuDebugOn(!au_kfree_do_sz_test(blksize));
5324+ au_kfree_do_rcu(buf);
5325+ } else
1c60b727 5326+ free_page((unsigned long)buf);
1facf9fc 5327+
4f0767ce 5328+out:
1facf9fc 5329+ return err;
5330+}
5331+
1c60b727
AM
5332+static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5333+{
5334+ int err;
5335+ struct super_block *h_src_sb;
5336+ struct inode *h_src_inode;
5337+
5338+ h_src_inode = file_inode(src);
5339+ h_src_sb = h_src_inode->i_sb;
5340+
5341+ /* XFS acquires inode_lock */
5342+ if (!au_test_xfs(h_src_sb))
5343+ err = au_copy_file(dst, src, len);
5344+ else {
3c1bdaff 5345+ inode_unlock_shared(h_src_inode);
1c60b727 5346+ err = au_copy_file(dst, src, len);
be118d29 5347+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
1c60b727
AM
5348+ }
5349+
5350+ return err;
5351+}
5352+
5353+static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5354+{
5355+ int err;
9f237c51 5356+ loff_t lo;
1c60b727
AM
5357+ struct super_block *h_src_sb;
5358+ struct inode *h_src_inode;
5359+
5360+ h_src_inode = file_inode(src);
5361+ h_src_sb = h_src_inode->i_sb;
5362+ if (h_src_sb != file_inode(dst)->i_sb
9f237c51 5363+ || !dst->f_op->remap_file_range) {
1c60b727
AM
5364+ err = au_do_copy(dst, src, len);
5365+ goto out;
5366+ }
5367+
5368+ if (!au_test_nfs(h_src_sb)) {
3c1bdaff 5369+ inode_unlock_shared(h_src_inode);
9f237c51 5370+ lo = vfsub_clone_file_range(src, dst, len);
be118d29 5371+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
1c60b727 5372+ } else
9f237c51
AM
5373+ lo = vfsub_clone_file_range(src, dst, len);
5374+ if (lo == len) {
5375+ err = 0;
5376+ goto out; /* success */
5377+ } else if (lo >= 0)
5378+ /* todo: possible? */
5379+ /* paritially succeeded */
5380+ AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5381+ else if (lo != -EOPNOTSUPP) {
5382+ /* older XFS has a condition in cloning */
5383+ err = lo;
1c60b727 5384+ goto out;
9f237c51 5385+ }
1c60b727
AM
5386+
5387+ /* the backend fs on NFS may not support cloning */
5388+ err = au_do_copy(dst, src, len);
5389+
5390+out:
5391+ AuTraceErr(err);
5392+ return err;
5393+}
5394+
1facf9fc 5395+/*
5396+ * to support a sparse file which is opened with O_APPEND,
5397+ * we need to close the file.
5398+ */
c2b27bf2 5399+static int au_cp_regular(struct au_cp_generic *cpg)
1facf9fc 5400+{
5401+ int err, i;
5402+ enum { SRC, DST };
5403+ struct {
5404+ aufs_bindex_t bindex;
5405+ unsigned int flags;
5406+ struct dentry *dentry;
392086de 5407+ int force_wr;
1facf9fc 5408+ struct file *file;
1facf9fc 5409+ } *f, file[] = {
5410+ {
c2b27bf2 5411+ .bindex = cpg->bsrc,
1facf9fc 5412+ .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
1facf9fc 5413+ },
5414+ {
c2b27bf2 5415+ .bindex = cpg->bdst,
1facf9fc 5416+ .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
392086de 5417+ .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
1facf9fc 5418+ }
5419+ };
acd2b654 5420+ struct au_branch *br;
521ced18 5421+ struct super_block *sb, *h_src_sb;
e2f27e51 5422+ struct inode *h_src_inode;
8cdd5066 5423+ struct task_struct *tsk = current;
1facf9fc 5424+
5425+ /* bsrc branch can be ro/rw. */
c2b27bf2 5426+ sb = cpg->dentry->d_sb;
1facf9fc 5427+ f = file;
5428+ for (i = 0; i < 2; i++, f++) {
c2b27bf2
AM
5429+ f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5430+ f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
392086de 5431+ /*file*/NULL, f->force_wr);
9f237c51
AM
5432+ if (IS_ERR(f->file)) {
5433+ err = PTR_ERR(f->file);
5434+ if (i == SRC)
5435+ goto out;
5436+ else
5437+ goto out_src;
5438+ }
1facf9fc 5439+ }
5440+
5441+ /* try stopping to update while we copyup */
e2f27e51 5442+ h_src_inode = d_inode(file[SRC].dentry);
521ced18
JR
5443+ h_src_sb = h_src_inode->i_sb;
5444+ if (!au_test_nfs(h_src_sb))
e2f27e51 5445+ IMustLock(h_src_inode);
1c60b727 5446+ err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
1facf9fc 5447+
8cdd5066
JR
5448+ /* i wonder if we had O_NO_DELAY_FPUT flag */
5449+ if (tsk->flags & PF_KTHREAD)
5450+ __fput_sync(file[DST].file);
5451+ else {
062440b3 5452+ /* it happened actually */
8cdd5066
JR
5453+ fput(file[DST].file);
5454+ /*
5455+ * too bad.
5456+ * we have to call both since we don't know which place the file
5457+ * was added to.
5458+ */
5459+ task_work_run();
5460+ flush_delayed_fput();
5461+ }
acd2b654
AM
5462+ br = au_sbr(sb, file[DST].bindex);
5463+ au_lcnt_dec(&br->br_nfiles);
523b37e3 5464+
4f0767ce 5465+out_src:
1facf9fc 5466+ fput(file[SRC].file);
acd2b654
AM
5467+ br = au_sbr(sb, file[SRC].bindex);
5468+ au_lcnt_dec(&br->br_nfiles);
4f0767ce 5469+out:
1facf9fc 5470+ return err;
5471+}
5472+
c2b27bf2 5473+static int au_do_cpup_regular(struct au_cp_generic *cpg,
86dc4139 5474+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5475+{
5476+ int err, rerr;
5477+ loff_t l;
86dc4139 5478+ struct path h_path;
38d290e6 5479+ struct inode *h_src_inode, *h_dst_inode;
1facf9fc 5480+
5481+ err = 0;
5527c038 5482+ h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
86dc4139 5483+ l = i_size_read(h_src_inode);
c2b27bf2
AM
5484+ if (cpg->len == -1 || l < cpg->len)
5485+ cpg->len = l;
5486+ if (cpg->len) {
86dc4139 5487+ /* try stopping to update while we are referencing */
be118d29 5488+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
c2b27bf2 5489+ au_pin_hdir_unlock(cpg->pin);
1facf9fc 5490+
c2b27bf2
AM
5491+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5492+ h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
86dc4139 5493+ h_src_attr->iflags = h_src_inode->i_flags;
5527c038 5494+ if (!au_test_nfs(h_src_inode->i_sb))
521ced18 5495+ err = vfsub_getattr(&h_path, &h_src_attr->st);
5527c038 5496+ else {
3c1bdaff 5497+ inode_unlock_shared(h_src_inode);
521ced18 5498+ err = vfsub_getattr(&h_path, &h_src_attr->st);
be118d29 5499+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5527c038 5500+ }
86dc4139 5501+ if (unlikely(err)) {
3c1bdaff 5502+ inode_unlock_shared(h_src_inode);
86dc4139
AM
5503+ goto out;
5504+ }
5505+ h_src_attr->valid = 1;
e2f27e51
AM
5506+ if (!au_test_nfs(h_src_inode->i_sb)) {
5507+ err = au_cp_regular(cpg);
3c1bdaff 5508+ inode_unlock_shared(h_src_inode);
e2f27e51 5509+ } else {
3c1bdaff 5510+ inode_unlock_shared(h_src_inode);
e2f27e51
AM
5511+ err = au_cp_regular(cpg);
5512+ }
c2b27bf2 5513+ rerr = au_pin_hdir_relock(cpg->pin);
86dc4139
AM
5514+ if (!err && rerr)
5515+ err = rerr;
1facf9fc 5516+ }
38d290e6
JR
5517+ if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5518+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5527c038 5519+ h_dst_inode = d_inode(h_path.dentry);
38d290e6
JR
5520+ spin_lock(&h_dst_inode->i_lock);
5521+ h_dst_inode->i_state |= I_LINKABLE;
5522+ spin_unlock(&h_dst_inode->i_lock);
5523+ }
1facf9fc 5524+
4f0767ce 5525+out:
1facf9fc 5526+ return err;
5527+}
5528+
5529+static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5530+ struct inode *h_dir)
5531+{
5532+ int err, symlen;
5533+ mm_segment_t old_fs;
b752ccd1
AM
5534+ union {
5535+ char *k;
5536+ char __user *u;
5537+ } sym;
1facf9fc 5538+
5539+ err = -ENOMEM;
537831f9 5540+ sym.k = (void *)__get_free_page(GFP_NOFS);
b752ccd1 5541+ if (unlikely(!sym.k))
1facf9fc 5542+ goto out;
5543+
9dbd164d 5544+ /* unnecessary to support mmap_sem since symlink is not mmap-able */
1facf9fc 5545+ old_fs = get_fs();
5546+ set_fs(KERNEL_DS);
a2654f78 5547+ symlen = vfs_readlink(h_src, sym.u, PATH_MAX);
1facf9fc 5548+ err = symlen;
5549+ set_fs(old_fs);
5550+
5551+ if (symlen > 0) {
b752ccd1
AM
5552+ sym.k[symlen] = 0;
5553+ err = vfsub_symlink(h_dir, h_path, sym.k);
1facf9fc 5554+ }
1c60b727 5555+ free_page((unsigned long)sym.k);
1facf9fc 5556+
4f0767ce 5557+out:
1facf9fc 5558+ return err;
5559+}
5560+
8cdd5066
JR
5561+/*
5562+ * regardless 'acl' option, reset all ACL.
5563+ * All ACL will be copied up later from the original entry on the lower branch.
5564+ */
5565+static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5566+{
5567+ int err;
5568+ struct dentry *h_dentry;
5569+ struct inode *h_inode;
5570+
5571+ h_dentry = h_path->dentry;
5572+ h_inode = d_inode(h_dentry);
5573+ /* forget_all_cached_acls(h_inode)); */
5574+ err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5575+ AuTraceErr(err);
5576+ if (err == -EOPNOTSUPP)
5577+ err = 0;
5578+ if (!err)
5579+ err = vfsub_acl_chmod(h_inode, mode);
5580+
5581+ AuTraceErr(err);
5582+ return err;
5583+}
5584+
5585+static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5586+ struct inode *h_dir, struct path *h_path)
5587+{
5588+ int err;
5589+ struct inode *dir, *inode;
5590+
5591+ err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
5592+ AuTraceErr(err);
5593+ if (err == -EOPNOTSUPP)
5594+ err = 0;
5595+ if (unlikely(err))
5596+ goto out;
5597+
5598+ /*
5599+ * strange behaviour from the users view,
acd2b654 5600+ * particularly setattr case
8cdd5066
JR
5601+ */
5602+ dir = d_inode(dst_parent);
5afbbe0d 5603+ if (au_ibtop(dir) == cpg->bdst)
8cdd5066
JR
5604+ au_cpup_attr_nlink(dir, /*force*/1);
5605+ inode = d_inode(cpg->dentry);
5606+ au_cpup_attr_nlink(inode, /*force*/1);
5607+
5608+out:
5609+ return err;
5610+}
5611+
1facf9fc 5612+static noinline_for_stack
c2b27bf2 5613+int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
86dc4139 5614+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5615+{
5616+ int err;
5617+ umode_t mode;
5618+ unsigned int mnt_flags;
076b876e 5619+ unsigned char isdir, isreg, force;
c2b27bf2 5620+ const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5621+ struct au_dtime dt;
5622+ struct path h_path;
5623+ struct dentry *h_src, *h_dst, *h_parent;
8cdd5066 5624+ struct inode *h_inode, *h_dir;
1facf9fc 5625+ struct super_block *sb;
5626+
5627+ /* bsrc branch can be ro/rw. */
c2b27bf2 5628+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038
JR
5629+ h_inode = d_inode(h_src);
5630+ AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
1facf9fc 5631+
5632+ /* try stopping to be referenced while we are creating */
c2b27bf2
AM
5633+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5634+ if (au_ftest_cpup(cpg->flags, RENAME))
86dc4139
AM
5635+ AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5636+ AUFS_WH_PFX_LEN));
1facf9fc 5637+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5638+ h_dir = d_inode(h_parent);
1facf9fc 5639+ IMustLock(h_dir);
5640+ AuDebugOn(h_parent != h_dst->d_parent);
5641+
c2b27bf2
AM
5642+ sb = cpg->dentry->d_sb;
5643+ h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
1facf9fc 5644+ if (do_dt) {
5645+ h_path.dentry = h_parent;
5646+ au_dtime_store(&dt, dst_parent, &h_path);
5647+ }
5648+ h_path.dentry = h_dst;
5649+
076b876e 5650+ isreg = 0;
1facf9fc 5651+ isdir = 0;
5652+ mode = h_inode->i_mode;
5653+ switch (mode & S_IFMT) {
5654+ case S_IFREG:
076b876e 5655+ isreg = 1;
cd7a4cd9 5656+ err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
1facf9fc 5657+ if (!err)
c2b27bf2 5658+ err = au_do_cpup_regular(cpg, h_src_attr);
1facf9fc 5659+ break;
5660+ case S_IFDIR:
5661+ isdir = 1;
5662+ err = vfsub_mkdir(h_dir, &h_path, mode);
8cdd5066
JR
5663+ if (!err)
5664+ err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
1facf9fc 5665+ break;
5666+ case S_IFLNK:
5667+ err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5668+ break;
5669+ case S_IFCHR:
5670+ case S_IFBLK:
5671+ AuDebugOn(!capable(CAP_MKNOD));
5672+ /*FALLTHROUGH*/
5673+ case S_IFIFO:
5674+ case S_IFSOCK:
5675+ err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5676+ break;
5677+ default:
5678+ AuIOErr("Unknown inode type 0%o\n", mode);
5679+ err = -EIO;
5680+ }
8cdd5066
JR
5681+ if (!err)
5682+ err = au_reset_acl(h_dir, &h_path, mode);
1facf9fc 5683+
5684+ mnt_flags = au_mntflags(sb);
5685+ if (!au_opt_test(mnt_flags, UDBA_NONE)
5686+ && !isdir
5687+ && au_opt_test(mnt_flags, XINO)
38d290e6
JR
5688+ && (h_inode->i_nlink == 1
5689+ || (h_inode->i_state & I_LINKABLE))
1facf9fc 5690+ /* todo: unnecessary? */
5527c038 5691+ /* && d_inode(cpg->dentry)->i_nlink == 1 */
c2b27bf2
AM
5692+ && cpg->bdst < cpg->bsrc
5693+ && !au_ftest_cpup(cpg->flags, KEEPLINO))
5694+ au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
1facf9fc 5695+ /* ignore this error */
5696+
076b876e
AM
5697+ if (!err) {
5698+ force = 0;
5699+ if (isreg) {
5700+ force = !!cpg->len;
5701+ if (cpg->len == -1)
5702+ force = !!i_size_read(h_inode);
5703+ }
5704+ au_fhsm_wrote(sb, cpg->bdst, force);
5705+ }
5706+
1facf9fc 5707+ if (do_dt)
5708+ au_dtime_revert(&dt);
5709+ return err;
5710+}
5711+
392086de 5712+static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
86dc4139
AM
5713+{
5714+ int err;
392086de 5715+ struct dentry *dentry, *h_dentry, *h_parent, *parent;
86dc4139 5716+ struct inode *h_dir;
392086de 5717+ aufs_bindex_t bdst;
86dc4139 5718+
392086de
AM
5719+ dentry = cpg->dentry;
5720+ bdst = cpg->bdst;
5721+ h_dentry = au_h_dptr(dentry, bdst);
5722+ if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5723+ dget(h_dentry);
5724+ au_set_h_dptr(dentry, bdst, NULL);
5725+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
5726+ if (!err)
5727+ h_path->dentry = dget(au_h_dptr(dentry, bdst));
86dc4139 5728+ au_set_h_dptr(dentry, bdst, h_dentry);
392086de
AM
5729+ } else {
5730+ err = 0;
5731+ parent = dget_parent(dentry);
5732+ h_parent = au_h_dptr(parent, bdst);
5733+ dput(parent);
5734+ h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5735+ if (IS_ERR(h_path->dentry))
5736+ err = PTR_ERR(h_path->dentry);
86dc4139 5737+ }
392086de
AM
5738+ if (unlikely(err))
5739+ goto out;
86dc4139 5740+
86dc4139 5741+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 5742+ h_dir = d_inode(h_parent);
86dc4139 5743+ IMustLock(h_dir);
523b37e3
AM
5744+ AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5745+ /* no delegation since it is just created */
f2c43d5f
AM
5746+ err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5747+ /*flags*/0);
86dc4139
AM
5748+ dput(h_path->dentry);
5749+
5750+out:
5751+ return err;
5752+}
5753+
1facf9fc 5754+/*
5755+ * copyup the @dentry from @bsrc to @bdst.
5756+ * the caller must set the both of lower dentries.
5757+ * @len is for truncating when it is -1 copyup the entire file.
5758+ * in link/rename cases, @dst_parent may be different from the real one.
c2b27bf2 5759+ * basic->bsrc can be larger than basic->bdst.
f2c43d5f 5760+ * aufs doesn't touch the credential so
acd2b654 5761+ * security_inode_copy_up{,_xattr}() are unnecessary.
1facf9fc 5762+ */
c2b27bf2 5763+static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5764+{
5765+ int err, rerr;
5afbbe0d 5766+ aufs_bindex_t old_ibtop;
1facf9fc 5767+ unsigned char isdir, plink;
1facf9fc 5768+ struct dentry *h_src, *h_dst, *h_parent;
5527c038 5769+ struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
1facf9fc 5770+ struct super_block *sb;
86dc4139 5771+ struct au_branch *br;
acd2b654 5772+ /* to reduce stack size */
c2b27bf2
AM
5773+ struct {
5774+ struct au_dtime dt;
5775+ struct path h_path;
5776+ struct au_cpup_reg_attr h_src_attr;
5777+ } *a;
1facf9fc 5778+
c2b27bf2
AM
5779+ err = -ENOMEM;
5780+ a = kmalloc(sizeof(*a), GFP_NOFS);
5781+ if (unlikely(!a))
5782+ goto out;
5783+ a->h_src_attr.valid = 0;
1facf9fc 5784+
c2b27bf2
AM
5785+ sb = cpg->dentry->d_sb;
5786+ br = au_sbr(sb, cpg->bdst);
5787+ a->h_path.mnt = au_br_mnt(br);
5788+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
1facf9fc 5789+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5790+ h_dir = d_inode(h_parent);
1facf9fc 5791+ IMustLock(h_dir);
5792+
c2b27bf2 5793+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5794+ inode = d_inode(cpg->dentry);
1facf9fc 5795+
5796+ if (!dst_parent)
c2b27bf2 5797+ dst_parent = dget_parent(cpg->dentry);
1facf9fc 5798+ else
5799+ dget(dst_parent);
5800+
5801+ plink = !!au_opt_test(au_mntflags(sb), PLINK);
c2b27bf2 5802+ dst_inode = au_h_iptr(inode, cpg->bdst);
1facf9fc 5803+ if (dst_inode) {
5804+ if (unlikely(!plink)) {
5805+ err = -EIO;
027c5e7a
AM
5806+ AuIOErr("hi%lu(i%lu) exists on b%d "
5807+ "but plink is disabled\n",
c2b27bf2
AM
5808+ dst_inode->i_ino, inode->i_ino, cpg->bdst);
5809+ goto out_parent;
1facf9fc 5810+ }
5811+
5812+ if (dst_inode->i_nlink) {
c2b27bf2 5813+ const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5814+
c2b27bf2 5815+ h_src = au_plink_lkup(inode, cpg->bdst);
1facf9fc 5816+ err = PTR_ERR(h_src);
5817+ if (IS_ERR(h_src))
c2b27bf2 5818+ goto out_parent;
5527c038 5819+ if (unlikely(d_is_negative(h_src))) {
1facf9fc 5820+ err = -EIO;
79b8bda9 5821+ AuIOErr("i%lu exists on b%d "
027c5e7a 5822+ "but not pseudo-linked\n",
79b8bda9 5823+ inode->i_ino, cpg->bdst);
1facf9fc 5824+ dput(h_src);
c2b27bf2 5825+ goto out_parent;
1facf9fc 5826+ }
5827+
5828+ if (do_dt) {
c2b27bf2
AM
5829+ a->h_path.dentry = h_parent;
5830+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
1facf9fc 5831+ }
86dc4139 5832+
c2b27bf2 5833+ a->h_path.dentry = h_dst;
523b37e3
AM
5834+ delegated = NULL;
5835+ err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
c2b27bf2 5836+ if (!err && au_ftest_cpup(cpg->flags, RENAME))
392086de 5837+ err = au_do_ren_after_cpup(cpg, &a->h_path);
1facf9fc 5838+ if (do_dt)
c2b27bf2 5839+ au_dtime_revert(&a->dt);
523b37e3
AM
5840+ if (unlikely(err == -EWOULDBLOCK)) {
5841+ pr_warn("cannot retry for NFSv4 delegation"
5842+ " for an internal link\n");
5843+ iput(delegated);
5844+ }
1facf9fc 5845+ dput(h_src);
c2b27bf2 5846+ goto out_parent;
1facf9fc 5847+ } else
5848+ /* todo: cpup_wh_file? */
5849+ /* udba work */
4a4d8108 5850+ au_update_ibrange(inode, /*do_put_zero*/1);
1facf9fc 5851+ }
5852+
86dc4139 5853+ isdir = S_ISDIR(inode->i_mode);
5afbbe0d 5854+ old_ibtop = au_ibtop(inode);
c2b27bf2 5855+ err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
1facf9fc 5856+ if (unlikely(err))
86dc4139 5857+ goto out_rev;
5527c038 5858+ dst_inode = d_inode(h_dst);
febd17d6 5859+ inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
86dc4139 5860+ /* todo: necessary? */
c2b27bf2 5861+ /* au_pin_hdir_unlock(cpg->pin); */
1facf9fc 5862+
c2b27bf2 5863+ err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
86dc4139
AM
5864+ if (unlikely(err)) {
5865+ /* todo: necessary? */
c2b27bf2 5866+ /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
febd17d6 5867+ inode_unlock(dst_inode);
86dc4139
AM
5868+ goto out_rev;
5869+ }
5870+
5afbbe0d 5871+ if (cpg->bdst < old_ibtop) {
86dc4139 5872+ if (S_ISREG(inode->i_mode)) {
c2b27bf2 5873+ err = au_dy_iaop(inode, cpg->bdst, dst_inode);
86dc4139 5874+ if (unlikely(err)) {
c2b27bf2
AM
5875+ /* ignore an error */
5876+ /* au_pin_hdir_relock(cpg->pin); */
febd17d6 5877+ inode_unlock(dst_inode);
86dc4139 5878+ goto out_rev;
4a4d8108 5879+ }
4a4d8108 5880+ }
5afbbe0d 5881+ au_set_ibtop(inode, cpg->bdst);
c2b27bf2 5882+ } else
5afbbe0d 5883+ au_set_ibbot(inode, cpg->bdst);
c2b27bf2 5884+ au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
86dc4139
AM
5885+ au_hi_flags(inode, isdir));
5886+
5887+ /* todo: necessary? */
c2b27bf2 5888+ /* err = au_pin_hdir_relock(cpg->pin); */
febd17d6 5889+ inode_unlock(dst_inode);
86dc4139
AM
5890+ if (unlikely(err))
5891+ goto out_rev;
5892+
5527c038 5893+ src_inode = d_inode(h_src);
86dc4139 5894+ if (!isdir
5527c038
JR
5895+ && (src_inode->i_nlink > 1
5896+ || src_inode->i_state & I_LINKABLE)
86dc4139 5897+ && plink)
c2b27bf2 5898+ au_plink_append(inode, cpg->bdst, h_dst);
86dc4139 5899+
c2b27bf2
AM
5900+ if (au_ftest_cpup(cpg->flags, RENAME)) {
5901+ a->h_path.dentry = h_dst;
392086de 5902+ err = au_do_ren_after_cpup(cpg, &a->h_path);
86dc4139
AM
5903+ }
5904+ if (!err)
c2b27bf2 5905+ goto out_parent; /* success */
1facf9fc 5906+
5907+ /* revert */
4a4d8108 5908+out_rev:
c2b27bf2
AM
5909+ a->h_path.dentry = h_parent;
5910+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
5911+ a->h_path.dentry = h_dst;
86dc4139 5912+ rerr = 0;
5527c038 5913+ if (d_is_positive(h_dst)) {
523b37e3
AM
5914+ if (!isdir) {
5915+ /* no delegation since it is just created */
5916+ rerr = vfsub_unlink(h_dir, &a->h_path,
5917+ /*delegated*/NULL, /*force*/0);
5918+ } else
c2b27bf2 5919+ rerr = vfsub_rmdir(h_dir, &a->h_path);
86dc4139 5920+ }
c2b27bf2 5921+ au_dtime_revert(&a->dt);
1facf9fc 5922+ if (rerr) {
5923+ AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5924+ err = -EIO;
5925+ }
c2b27bf2 5926+out_parent:
1facf9fc 5927+ dput(dst_parent);
9f237c51 5928+ au_kfree_rcu(a);
c2b27bf2 5929+out:
1facf9fc 5930+ return err;
5931+}
5932+
7e9cd9fe 5933+#if 0 /* reserved */
1facf9fc 5934+struct au_cpup_single_args {
5935+ int *errp;
c2b27bf2 5936+ struct au_cp_generic *cpg;
1facf9fc 5937+ struct dentry *dst_parent;
5938+};
5939+
5940+static void au_call_cpup_single(void *args)
5941+{
5942+ struct au_cpup_single_args *a = args;
86dc4139 5943+
c2b27bf2
AM
5944+ au_pin_hdir_acquire_nest(a->cpg->pin);
5945+ *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5946+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 5947+}
c2b27bf2 5948+#endif
1facf9fc 5949+
53392da6
AM
5950+/*
5951+ * prevent SIGXFSZ in copy-up.
5952+ * testing CAP_MKNOD is for generic fs,
5953+ * but CAP_FSETID is for xfs only, currently.
5954+ */
86dc4139 5955+static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
53392da6
AM
5956+{
5957+ int do_sio;
86dc4139
AM
5958+ struct super_block *sb;
5959+ struct inode *h_dir;
53392da6
AM
5960+
5961+ do_sio = 0;
86dc4139 5962+ sb = au_pinned_parent(pin)->d_sb;
53392da6
AM
5963+ if (!au_wkq_test()
5964+ && (!au_sbi(sb)->si_plink_maint_pid
5965+ || au_plink_maint(sb, AuLock_NOPLM))) {
5966+ switch (mode & S_IFMT) {
5967+ case S_IFREG:
5968+ /* no condition about RLIMIT_FSIZE and the file size */
5969+ do_sio = 1;
5970+ break;
5971+ case S_IFCHR:
5972+ case S_IFBLK:
5973+ do_sio = !capable(CAP_MKNOD);
5974+ break;
5975+ }
5976+ if (!do_sio)
5977+ do_sio = ((mode & (S_ISUID | S_ISGID))
5978+ && !capable(CAP_FSETID));
86dc4139
AM
5979+ /* this workaround may be removed in the future */
5980+ if (!do_sio) {
5981+ h_dir = au_pinned_h_dir(pin);
5982+ do_sio = h_dir->i_mode & S_ISVTX;
5983+ }
53392da6
AM
5984+ }
5985+
5986+ return do_sio;
5987+}
5988+
7e9cd9fe 5989+#if 0 /* reserved */
c2b27bf2 5990+int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5991+{
5992+ int err, wkq_err;
1facf9fc 5993+ struct dentry *h_dentry;
5994+
c2b27bf2 5995+ h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5996+ if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
c2b27bf2 5997+ err = au_cpup_single(cpg, dst_parent);
1facf9fc 5998+ else {
5999+ struct au_cpup_single_args args = {
6000+ .errp = &err,
c2b27bf2
AM
6001+ .cpg = cpg,
6002+ .dst_parent = dst_parent
1facf9fc 6003+ };
6004+ wkq_err = au_wkq_wait(au_call_cpup_single, &args);
6005+ if (unlikely(wkq_err))
6006+ err = wkq_err;
6007+ }
6008+
6009+ return err;
6010+}
c2b27bf2 6011+#endif
1facf9fc 6012+
6013+/*
6014+ * copyup the @dentry from the first active lower branch to @bdst,
6015+ * using au_cpup_single().
6016+ */
c2b27bf2 6017+static int au_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 6018+{
6019+ int err;
c2b27bf2
AM
6020+ unsigned int flags_orig;
6021+ struct dentry *dentry;
6022+
6023+ AuDebugOn(cpg->bsrc < 0);
1facf9fc 6024+
c2b27bf2 6025+ dentry = cpg->dentry;
86dc4139 6026+ DiMustWriteLock(dentry);
1facf9fc 6027+
c2b27bf2 6028+ err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
1facf9fc 6029+ if (!err) {
c2b27bf2
AM
6030+ flags_orig = cpg->flags;
6031+ au_fset_cpup(cpg->flags, RENAME);
6032+ err = au_cpup_single(cpg, NULL);
6033+ cpg->flags = flags_orig;
1facf9fc 6034+ if (!err)
6035+ return 0; /* success */
6036+
6037+ /* revert */
c2b27bf2 6038+ au_set_h_dptr(dentry, cpg->bdst, NULL);
5afbbe0d 6039+ au_set_dbtop(dentry, cpg->bsrc);
1facf9fc 6040+ }
6041+
6042+ return err;
6043+}
6044+
6045+struct au_cpup_simple_args {
6046+ int *errp;
c2b27bf2 6047+ struct au_cp_generic *cpg;
1facf9fc 6048+};
6049+
6050+static void au_call_cpup_simple(void *args)
6051+{
6052+ struct au_cpup_simple_args *a = args;
86dc4139 6053+
c2b27bf2
AM
6054+ au_pin_hdir_acquire_nest(a->cpg->pin);
6055+ *a->errp = au_cpup_simple(a->cpg);
6056+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 6057+}
6058+
c2b27bf2 6059+static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 6060+{
6061+ int err, wkq_err;
c2b27bf2
AM
6062+ struct dentry *dentry, *parent;
6063+ struct file *h_file;
1facf9fc 6064+ struct inode *h_dir;
6065+
c2b27bf2
AM
6066+ dentry = cpg->dentry;
6067+ h_file = NULL;
6068+ if (au_ftest_cpup(cpg->flags, HOPEN)) {
6069+ AuDebugOn(cpg->bsrc < 0);
392086de 6070+ h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
c2b27bf2
AM
6071+ err = PTR_ERR(h_file);
6072+ if (IS_ERR(h_file))
6073+ goto out;
6074+ }
6075+
1facf9fc 6076+ parent = dget_parent(dentry);
5527c038 6077+ h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
53392da6 6078+ if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
5527c038 6079+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 6080+ err = au_cpup_simple(cpg);
1facf9fc 6081+ else {
6082+ struct au_cpup_simple_args args = {
6083+ .errp = &err,
c2b27bf2 6084+ .cpg = cpg
1facf9fc 6085+ };
6086+ wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
6087+ if (unlikely(wkq_err))
6088+ err = wkq_err;
6089+ }
6090+
6091+ dput(parent);
c2b27bf2
AM
6092+ if (h_file)
6093+ au_h_open_post(dentry, cpg->bsrc, h_file);
6094+
6095+out:
1facf9fc 6096+ return err;
6097+}
6098+
c2b27bf2 6099+int au_sio_cpup_simple(struct au_cp_generic *cpg)
367653fa 6100+{
5afbbe0d 6101+ aufs_bindex_t bsrc, bbot;
c2b27bf2 6102+ struct dentry *dentry, *h_dentry;
367653fa 6103+
c2b27bf2
AM
6104+ if (cpg->bsrc < 0) {
6105+ dentry = cpg->dentry;
5afbbe0d
AM
6106+ bbot = au_dbbot(dentry);
6107+ for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
c2b27bf2
AM
6108+ h_dentry = au_h_dptr(dentry, bsrc);
6109+ if (h_dentry) {
5527c038 6110+ AuDebugOn(d_is_negative(h_dentry));
c2b27bf2
AM
6111+ break;
6112+ }
6113+ }
5afbbe0d 6114+ AuDebugOn(bsrc > bbot);
c2b27bf2 6115+ cpg->bsrc = bsrc;
367653fa 6116+ }
c2b27bf2
AM
6117+ AuDebugOn(cpg->bsrc <= cpg->bdst);
6118+ return au_do_sio_cpup_simple(cpg);
6119+}
367653fa 6120+
c2b27bf2
AM
6121+int au_sio_cpdown_simple(struct au_cp_generic *cpg)
6122+{
6123+ AuDebugOn(cpg->bdst <= cpg->bsrc);
6124+ return au_do_sio_cpup_simple(cpg);
367653fa
AM
6125+}
6126+
1facf9fc 6127+/* ---------------------------------------------------------------------- */
6128+
6129+/*
6130+ * copyup the deleted file for writing.
6131+ */
c2b27bf2
AM
6132+static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
6133+ struct file *file)
1facf9fc 6134+{
6135+ int err;
c2b27bf2
AM
6136+ unsigned int flags_orig;
6137+ aufs_bindex_t bsrc_orig;
c2b27bf2 6138+ struct au_dinfo *dinfo;
5afbbe0d
AM
6139+ struct {
6140+ struct au_hdentry *hd;
6141+ struct dentry *h_dentry;
6142+ } hdst, hsrc;
1facf9fc 6143+
c2b27bf2 6144+ dinfo = au_di(cpg->dentry);
1308ab2a 6145+ AuRwMustWriteLock(&dinfo->di_rwsem);
6146+
c2b27bf2 6147+ bsrc_orig = cpg->bsrc;
5afbbe0d
AM
6148+ cpg->bsrc = dinfo->di_btop;
6149+ hdst.hd = au_hdentry(dinfo, cpg->bdst);
6150+ hdst.h_dentry = hdst.hd->hd_dentry;
6151+ hdst.hd->hd_dentry = wh_dentry;
6152+ dinfo->di_btop = cpg->bdst;
6153+
6154+ hsrc.h_dentry = NULL;
027c5e7a 6155+ if (file) {
5afbbe0d
AM
6156+ hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
6157+ hsrc.h_dentry = hsrc.hd->hd_dentry;
6158+ hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
027c5e7a 6159+ }
c2b27bf2
AM
6160+ flags_orig = cpg->flags;
6161+ cpg->flags = !AuCpup_DTIME;
6162+ err = au_cpup_single(cpg, /*h_parent*/NULL);
6163+ cpg->flags = flags_orig;
027c5e7a
AM
6164+ if (file) {
6165+ if (!err)
6166+ err = au_reopen_nondir(file);
5afbbe0d 6167+ hsrc.hd->hd_dentry = hsrc.h_dentry;
1facf9fc 6168+ }
5afbbe0d
AM
6169+ hdst.hd->hd_dentry = hdst.h_dentry;
6170+ dinfo->di_btop = cpg->bsrc;
c2b27bf2 6171+ cpg->bsrc = bsrc_orig;
1facf9fc 6172+
6173+ return err;
6174+}
6175+
c2b27bf2 6176+static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 6177+{
6178+ int err;
c2b27bf2 6179+ aufs_bindex_t bdst;
1facf9fc 6180+ struct au_dtime dt;
c2b27bf2 6181+ struct dentry *dentry, *parent, *h_parent, *wh_dentry;
1facf9fc 6182+ struct au_branch *br;
6183+ struct path h_path;
6184+
c2b27bf2
AM
6185+ dentry = cpg->dentry;
6186+ bdst = cpg->bdst;
1facf9fc 6187+ br = au_sbr(dentry->d_sb, bdst);
6188+ parent = dget_parent(dentry);
6189+ h_parent = au_h_dptr(parent, bdst);
6190+ wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
6191+ err = PTR_ERR(wh_dentry);
6192+ if (IS_ERR(wh_dentry))
6193+ goto out;
6194+
6195+ h_path.dentry = h_parent;
86dc4139 6196+ h_path.mnt = au_br_mnt(br);
1facf9fc 6197+ au_dtime_store(&dt, parent, &h_path);
c2b27bf2 6198+ err = au_do_cpup_wh(cpg, wh_dentry, file);
1facf9fc 6199+ if (unlikely(err))
6200+ goto out_wh;
6201+
6202+ dget(wh_dentry);
6203+ h_path.dentry = wh_dentry;
2000de60 6204+ if (!d_is_dir(wh_dentry)) {
523b37e3 6205+ /* no delegation since it is just created */
5527c038 6206+ err = vfsub_unlink(d_inode(h_parent), &h_path,
523b37e3
AM
6207+ /*delegated*/NULL, /*force*/0);
6208+ } else
5527c038 6209+ err = vfsub_rmdir(d_inode(h_parent), &h_path);
1facf9fc 6210+ if (unlikely(err)) {
523b37e3
AM
6211+ AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6212+ wh_dentry, err);
1facf9fc 6213+ err = -EIO;
6214+ }
6215+ au_dtime_revert(&dt);
5527c038 6216+ au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
1facf9fc 6217+
4f0767ce 6218+out_wh:
1facf9fc 6219+ dput(wh_dentry);
4f0767ce 6220+out:
1facf9fc 6221+ dput(parent);
6222+ return err;
6223+}
6224+
6225+struct au_cpup_wh_args {
6226+ int *errp;
c2b27bf2 6227+ struct au_cp_generic *cpg;
1facf9fc 6228+ struct file *file;
6229+};
6230+
6231+static void au_call_cpup_wh(void *args)
6232+{
6233+ struct au_cpup_wh_args *a = args;
86dc4139 6234+
c2b27bf2
AM
6235+ au_pin_hdir_acquire_nest(a->cpg->pin);
6236+ *a->errp = au_cpup_wh(a->cpg, a->file);
6237+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 6238+}
6239+
c2b27bf2 6240+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 6241+{
6242+ int err, wkq_err;
c2b27bf2 6243+ aufs_bindex_t bdst;
c1595e42 6244+ struct dentry *dentry, *parent, *h_orph, *h_parent;
86dc4139 6245+ struct inode *dir, *h_dir, *h_tmpdir;
1facf9fc 6246+ struct au_wbr *wbr;
c2b27bf2 6247+ struct au_pin wh_pin, *pin_orig;
1facf9fc 6248+
c2b27bf2
AM
6249+ dentry = cpg->dentry;
6250+ bdst = cpg->bdst;
1facf9fc 6251+ parent = dget_parent(dentry);
5527c038 6252+ dir = d_inode(parent);
1facf9fc 6253+ h_orph = NULL;
6254+ h_parent = NULL;
6255+ h_dir = au_igrab(au_h_iptr(dir, bdst));
6256+ h_tmpdir = h_dir;
c2b27bf2 6257+ pin_orig = NULL;
1facf9fc 6258+ if (!h_dir->i_nlink) {
6259+ wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6260+ h_orph = wbr->wbr_orph;
6261+
6262+ h_parent = dget(au_h_dptr(parent, bdst));
1facf9fc 6263+ au_set_h_dptr(parent, bdst, dget(h_orph));
5527c038 6264+ h_tmpdir = d_inode(h_orph);
1facf9fc 6265+ au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6266+
febd17d6 6267+ inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
4a4d8108 6268+ /* todo: au_h_open_pre()? */
86dc4139 6269+
c2b27bf2 6270+ pin_orig = cpg->pin;
86dc4139 6271+ au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
c2b27bf2
AM
6272+ AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6273+ cpg->pin = &wh_pin;
1facf9fc 6274+ }
6275+
53392da6 6276+ if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
5527c038 6277+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 6278+ err = au_cpup_wh(cpg, file);
1facf9fc 6279+ else {
6280+ struct au_cpup_wh_args args = {
6281+ .errp = &err,
c2b27bf2
AM
6282+ .cpg = cpg,
6283+ .file = file
1facf9fc 6284+ };
6285+ wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6286+ if (unlikely(wkq_err))
6287+ err = wkq_err;
6288+ }
6289+
6290+ if (h_orph) {
febd17d6 6291+ inode_unlock(h_tmpdir);
4a4d8108 6292+ /* todo: au_h_open_post()? */
1facf9fc 6293+ au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
1facf9fc 6294+ au_set_h_dptr(parent, bdst, h_parent);
c2b27bf2
AM
6295+ AuDebugOn(!pin_orig);
6296+ cpg->pin = pin_orig;
1facf9fc 6297+ }
6298+ iput(h_dir);
6299+ dput(parent);
6300+
6301+ return err;
6302+}
6303+
6304+/* ---------------------------------------------------------------------- */
6305+
6306+/*
6307+ * generic routine for both of copy-up and copy-down.
6308+ */
6309+/* cf. revalidate function in file.c */
6310+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6311+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6312+ struct au_pin *pin,
1facf9fc 6313+ struct dentry *h_parent, void *arg),
6314+ void *arg)
6315+{
6316+ int err;
6317+ struct au_pin pin;
5527c038 6318+ struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
1facf9fc 6319+
6320+ err = 0;
6321+ parent = dget_parent(dentry);
6322+ if (IS_ROOT(parent))
6323+ goto out;
6324+
6325+ au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6326+ au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6327+
6328+ /* do not use au_dpage */
6329+ real_parent = parent;
6330+ while (1) {
6331+ dput(parent);
6332+ parent = dget_parent(dentry);
6333+ h_parent = au_h_dptr(parent, bdst);
6334+ if (h_parent)
6335+ goto out; /* success */
6336+
6337+ /* find top dir which is necessary to cpup */
6338+ do {
6339+ d = parent;
6340+ dput(parent);
6341+ parent = dget_parent(d);
6342+ di_read_lock_parent3(parent, !AuLock_IR);
6343+ h_parent = au_h_dptr(parent, bdst);
6344+ di_read_unlock(parent, !AuLock_IR);
6345+ } while (!h_parent);
6346+
6347+ if (d != real_parent)
6348+ di_write_lock_child3(d);
6349+
6350+ /* somebody else might create while we were sleeping */
5527c038
JR
6351+ h_dentry = au_h_dptr(d, bdst);
6352+ if (!h_dentry || d_is_negative(h_dentry)) {
6353+ if (h_dentry)
5afbbe0d 6354+ au_update_dbtop(d);
1facf9fc 6355+
6356+ au_pin_set_dentry(&pin, d);
6357+ err = au_do_pin(&pin);
6358+ if (!err) {
86dc4139 6359+ err = cp(d, bdst, &pin, h_parent, arg);
1facf9fc 6360+ au_unpin(&pin);
6361+ }
6362+ }
6363+
6364+ if (d != real_parent)
6365+ di_write_unlock(d);
6366+ if (unlikely(err))
6367+ break;
6368+ }
6369+
4f0767ce 6370+out:
1facf9fc 6371+ dput(parent);
6372+ return err;
6373+}
6374+
6375+static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6376+ struct au_pin *pin,
2000de60 6377+ struct dentry *h_parent __maybe_unused,
1facf9fc 6378+ void *arg __maybe_unused)
6379+{
c2b27bf2
AM
6380+ struct au_cp_generic cpg = {
6381+ .dentry = dentry,
6382+ .bdst = bdst,
6383+ .bsrc = -1,
6384+ .len = 0,
6385+ .pin = pin,
6386+ .flags = AuCpup_DTIME
6387+ };
6388+ return au_sio_cpup_simple(&cpg);
1facf9fc 6389+}
6390+
6391+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6392+{
6393+ return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6394+}
6395+
6396+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6397+{
6398+ int err;
6399+ struct dentry *parent;
6400+ struct inode *dir;
6401+
6402+ parent = dget_parent(dentry);
5527c038 6403+ dir = d_inode(parent);
1facf9fc 6404+ err = 0;
6405+ if (au_h_iptr(dir, bdst))
6406+ goto out;
6407+
6408+ di_read_unlock(parent, AuLock_IR);
6409+ di_write_lock_parent(parent);
6410+ /* someone else might change our inode while we were sleeping */
6411+ if (!au_h_iptr(dir, bdst))
6412+ err = au_cpup_dirs(dentry, bdst);
6413+ di_downgrade_lock(parent, AuLock_IR);
6414+
4f0767ce 6415+out:
1facf9fc 6416+ dput(parent);
6417+ return err;
6418+}
7f207e10
AM
6419diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6420--- /usr/share/empty/fs/aufs/cpup.h 1970-01-01 01:00:00.000000000 +0100
ba1aed25 6421+++ linux/fs/aufs/cpup.h 2019-03-05 12:13:00.135890907 +0100
062440b3
AM
6422@@ -0,0 +1,100 @@
6423+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 6424+/*
ba1aed25 6425+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 6426+ *
6427+ * This program, aufs is free software; you can redistribute it and/or modify
6428+ * it under the terms of the GNU General Public License as published by
6429+ * the Free Software Foundation; either version 2 of the License, or
6430+ * (at your option) any later version.
dece6358
AM
6431+ *
6432+ * This program is distributed in the hope that it will be useful,
6433+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6434+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6435+ * GNU General Public License for more details.
6436+ *
6437+ * You should have received a copy of the GNU General Public License
523b37e3 6438+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6439+ */
6440+
6441+/*
6442+ * copy-up/down functions
6443+ */
6444+
6445+#ifndef __AUFS_CPUP_H__
6446+#define __AUFS_CPUP_H__
6447+
6448+#ifdef __KERNEL__
6449+
dece6358 6450+#include <linux/path.h>
1facf9fc 6451+
dece6358
AM
6452+struct inode;
6453+struct file;
86dc4139 6454+struct au_pin;
dece6358 6455+
86dc4139 6456+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
1facf9fc 6457+void au_cpup_attr_timesizes(struct inode *inode);
6458+void au_cpup_attr_nlink(struct inode *inode, int force);
6459+void au_cpup_attr_changeable(struct inode *inode);
6460+void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6461+void au_cpup_attr_all(struct inode *inode, int force);
6462+
6463+/* ---------------------------------------------------------------------- */
6464+
c2b27bf2
AM
6465+struct au_cp_generic {
6466+ struct dentry *dentry;
6467+ aufs_bindex_t bdst, bsrc;
6468+ loff_t len;
6469+ struct au_pin *pin;
6470+ unsigned int flags;
6471+};
6472+
1facf9fc 6473+/* cpup flags */
392086de
AM
6474+#define AuCpup_DTIME 1 /* do dtime_store/revert */
6475+#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
6476+ for link(2) */
6477+#define AuCpup_RENAME (1 << 2) /* rename after cpup */
6478+#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
6479+ cpup */
6480+#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
6481+ existing entry */
6482+#define AuCpup_RWDST (1 << 5) /* force write target even if
6483+ the branch is marked as RO */
c2b27bf2 6484+
8b6a4947
AM
6485+#ifndef CONFIG_AUFS_BR_HFSPLUS
6486+#undef AuCpup_HOPEN
6487+#define AuCpup_HOPEN 0
6488+#endif
6489+
1facf9fc 6490+#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
7f207e10
AM
6491+#define au_fset_cpup(flags, name) \
6492+ do { (flags) |= AuCpup_##name; } while (0)
6493+#define au_fclr_cpup(flags, name) \
6494+ do { (flags) &= ~AuCpup_##name; } while (0)
1facf9fc 6495+
6496+int au_copy_file(struct file *dst, struct file *src, loff_t len);
c2b27bf2
AM
6497+int au_sio_cpup_simple(struct au_cp_generic *cpg);
6498+int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6499+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
1facf9fc 6500+
6501+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6502+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6503+ struct au_pin *pin,
1facf9fc 6504+ struct dentry *h_parent, void *arg),
6505+ void *arg);
6506+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6507+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6508+
6509+/* ---------------------------------------------------------------------- */
6510+
6511+/* keep timestamps when copyup */
6512+struct au_dtime {
6513+ struct dentry *dt_dentry;
6514+ struct path dt_h_path;
cd7a4cd9 6515+ struct timespec64 dt_atime, dt_mtime;
1facf9fc 6516+};
6517+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6518+ struct path *h_path);
6519+void au_dtime_revert(struct au_dtime *dt);
6520+
6521+#endif /* __KERNEL__ */
6522+#endif /* __AUFS_CPUP_H__ */
7f207e10
AM
6523diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6524--- /usr/share/empty/fs/aufs/dbgaufs.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 6525+++ linux/fs/aufs/dbgaufs.c 2019-03-05 12:13:00.135890907 +0100
9f237c51 6526@@ -0,0 +1,526 @@
cd7a4cd9 6527+// SPDX-License-Identifier: GPL-2.0
1facf9fc 6528+/*
ba1aed25 6529+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 6530+ *
6531+ * This program, aufs is free software; you can redistribute it and/or modify
6532+ * it under the terms of the GNU General Public License as published by
6533+ * the Free Software Foundation; either version 2 of the License, or
6534+ * (at your option) any later version.
dece6358
AM
6535+ *
6536+ * This program is distributed in the hope that it will be useful,
6537+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6538+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6539+ * GNU General Public License for more details.
6540+ *
6541+ * You should have received a copy of the GNU General Public License
523b37e3 6542+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6543+ */
6544+
6545+/*
6546+ * debugfs interface
6547+ */
6548+
6549+#include <linux/debugfs.h>
6550+#include "aufs.h"
6551+
6552+#ifndef CONFIG_SYSFS
6553+#error DEBUG_FS depends upon SYSFS
6554+#endif
6555+
6556+static struct dentry *dbgaufs;
cd7a4cd9 6557+static const mode_t dbgaufs_mode = 0444;
1facf9fc 6558+
6559+/* 20 is max digits length of ulong 64 */
6560+struct dbgaufs_arg {
6561+ int n;
6562+ char a[20 * 4];
6563+};
6564+
6565+/*
6566+ * common function for all XINO files
6567+ */
6568+static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6569+ struct file *file)
6570+{
9f237c51
AM
6571+ void *p;
6572+
6573+ p = file->private_data;
6574+ if (p) {
6575+ /* this is struct dbgaufs_arg */
6576+ AuDebugOn(!au_kfree_sz_test(p));
6577+ au_kfree_do_rcu(p);
6578+ }
1facf9fc 6579+ return 0;
6580+}
6581+
062440b3
AM
6582+static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6583+ int cnt)
1facf9fc 6584+{
6585+ int err;
6586+ struct kstat st;
6587+ struct dbgaufs_arg *p;
6588+
6589+ err = -ENOMEM;
6590+ p = kmalloc(sizeof(*p), GFP_NOFS);
6591+ if (unlikely(!p))
6592+ goto out;
6593+
6594+ err = 0;
6595+ p->n = 0;
6596+ file->private_data = p;
6597+ if (!xf)
6598+ goto out;
6599+
521ced18 6600+ err = vfsub_getattr(&xf->f_path, &st);
1facf9fc 6601+ if (!err) {
6602+ if (do_fcnt)
6603+ p->n = snprintf
062440b3
AM
6604+ (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6605+ cnt, st.blocks, st.blksize,
1facf9fc 6606+ (long long)st.size);
6607+ else
521ced18 6608+ p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
1facf9fc 6609+ st.blocks, st.blksize,
6610+ (long long)st.size);
6611+ AuDebugOn(p->n >= sizeof(p->a));
6612+ } else {
6613+ p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6614+ err = 0;
6615+ }
6616+
4f0767ce 6617+out:
1facf9fc 6618+ return err;
1facf9fc 6619+}
6620+
6621+static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6622+ size_t count, loff_t *ppos)
6623+{
6624+ struct dbgaufs_arg *p;
6625+
6626+ p = file->private_data;
6627+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6628+}
6629+
6630+/* ---------------------------------------------------------------------- */
6631+
86dc4139
AM
6632+struct dbgaufs_plink_arg {
6633+ int n;
6634+ char a[];
6635+};
6636+
6637+static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6638+ struct file *file)
6639+{
1c60b727 6640+ free_page((unsigned long)file->private_data);
86dc4139
AM
6641+ return 0;
6642+}
6643+
6644+static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6645+{
6646+ int err, i, limit;
6647+ unsigned long n, sum;
6648+ struct dbgaufs_plink_arg *p;
6649+ struct au_sbinfo *sbinfo;
6650+ struct super_block *sb;
8b6a4947 6651+ struct hlist_bl_head *hbl;
86dc4139
AM
6652+
6653+ err = -ENOMEM;
6654+ p = (void *)get_zeroed_page(GFP_NOFS);
6655+ if (unlikely(!p))
6656+ goto out;
6657+
6658+ err = -EFBIG;
6659+ sbinfo = inode->i_private;
6660+ sb = sbinfo->si_sb;
6661+ si_noflush_read_lock(sb);
6662+ if (au_opt_test(au_mntflags(sb), PLINK)) {
6663+ limit = PAGE_SIZE - sizeof(p->n);
6664+
6665+ /* the number of buckets */
6666+ n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6667+ p->n += n;
6668+ limit -= n;
6669+
6670+ sum = 0;
8b6a4947
AM
6671+ for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6672+ i++, hbl++) {
6673+ n = au_hbl_count(hbl);
86dc4139
AM
6674+ sum += n;
6675+
6676+ n = snprintf(p->a + p->n, limit, "%lu ", n);
6677+ p->n += n;
6678+ limit -= n;
6679+ if (unlikely(limit <= 0))
6680+ goto out_free;
6681+ }
6682+ p->a[p->n - 1] = '\n';
6683+
6684+ /* the sum of plinks */
6685+ n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6686+ p->n += n;
6687+ limit -= n;
6688+ if (unlikely(limit <= 0))
6689+ goto out_free;
6690+ } else {
6691+#define str "1\n0\n0\n"
6692+ p->n = sizeof(str) - 1;
6693+ strcpy(p->a, str);
6694+#undef str
6695+ }
6696+ si_read_unlock(sb);
6697+
6698+ err = 0;
6699+ file->private_data = p;
6700+ goto out; /* success */
6701+
6702+out_free:
1c60b727 6703+ free_page((unsigned long)p);
86dc4139
AM
6704+out:
6705+ return err;
6706+}
6707+
6708+static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6709+ size_t count, loff_t *ppos)
6710+{
6711+ struct dbgaufs_plink_arg *p;
6712+
6713+ p = file->private_data;
6714+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6715+}
6716+
6717+static const struct file_operations dbgaufs_plink_fop = {
6718+ .owner = THIS_MODULE,
6719+ .open = dbgaufs_plink_open,
6720+ .release = dbgaufs_plink_release,
6721+ .read = dbgaufs_plink_read
6722+};
6723+
6724+/* ---------------------------------------------------------------------- */
6725+
1facf9fc 6726+static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6727+{
6728+ int err;
6729+ struct au_sbinfo *sbinfo;
6730+ struct super_block *sb;
6731+
6732+ sbinfo = inode->i_private;
6733+ sb = sbinfo->si_sb;
6734+ si_noflush_read_lock(sb);
062440b3 6735+ err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
1facf9fc 6736+ si_read_unlock(sb);
6737+ return err;
6738+}
6739+
6740+static const struct file_operations dbgaufs_xib_fop = {
4a4d8108 6741+ .owner = THIS_MODULE,
1facf9fc 6742+ .open = dbgaufs_xib_open,
6743+ .release = dbgaufs_xi_release,
6744+ .read = dbgaufs_xi_read
6745+};
6746+
6747+/* ---------------------------------------------------------------------- */
6748+
6749+#define DbgaufsXi_PREFIX "xi"
6750+
6751+static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6752+{
acd2b654 6753+ int err, idx;
1facf9fc 6754+ long l;
acd2b654
AM
6755+ aufs_bindex_t bindex;
6756+ char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
1facf9fc 6757+ struct au_sbinfo *sbinfo;
6758+ struct super_block *sb;
acd2b654 6759+ struct au_xino *xi;
1facf9fc 6760+ struct file *xf;
6761+ struct qstr *name;
062440b3 6762+ struct au_branch *br;
1facf9fc 6763+
6764+ err = -ENOENT;
2000de60 6765+ name = &file->f_path.dentry->d_name;
1facf9fc 6766+ if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6767+ || memcmp(name->name, DbgaufsXi_PREFIX,
6768+ sizeof(DbgaufsXi_PREFIX) - 1)))
6769+ goto out;
acd2b654
AM
6770+
6771+ AuDebugOn(name->len >= sizeof(a));
6772+ memcpy(a, name->name, name->len);
6773+ a[name->len] = '\0';
6774+ p = strchr(a, '-');
6775+ if (p)
6776+ *p = '\0';
6777+ err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
1facf9fc 6778+ if (unlikely(err))
6779+ goto out;
acd2b654
AM
6780+ bindex = l;
6781+ idx = 0;
6782+ if (p) {
6783+ err = kstrtol(p + 1, 10, &l);
6784+ if (unlikely(err))
6785+ goto out;
6786+ idx = l;
6787+ }
1facf9fc 6788+
acd2b654 6789+ err = -ENOENT;
1facf9fc 6790+ sbinfo = inode->i_private;
6791+ sb = sbinfo->si_sb;
6792+ si_noflush_read_lock(sb);
acd2b654
AM
6793+ if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6794+ goto out_si;
6795+ br = au_sbr(sb, bindex);
6796+ xi = br->br_xino;
6797+ if (unlikely(idx >= xi->xi_nfile))
6798+ goto out_si;
6799+ xf = au_xino_file(xi, idx);
6800+ if (xf)
062440b3
AM
6801+ err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6802+ au_xino_count(br));
1facf9fc 6803+
acd2b654
AM
6804+out_si:
6805+ si_read_unlock(sb);
4f0767ce 6806+out:
acd2b654 6807+ AuTraceErr(err);
1facf9fc 6808+ return err;
6809+}
6810+
6811+static const struct file_operations dbgaufs_xino_fop = {
4a4d8108 6812+ .owner = THIS_MODULE,
1facf9fc 6813+ .open = dbgaufs_xino_open,
6814+ .release = dbgaufs_xi_release,
6815+ .read = dbgaufs_xi_read
6816+};
6817+
062440b3
AM
6818+void dbgaufs_xino_del(struct au_branch *br)
6819+{
6820+ struct dentry *dbgaufs;
6821+
6822+ dbgaufs = br->br_dbgaufs;
6823+ if (!dbgaufs)
6824+ return;
6825+
6826+ br->br_dbgaufs = NULL;
6827+ /* debugfs acquires the parent i_mutex */
6828+ lockdep_off();
6829+ debugfs_remove(dbgaufs);
6830+ lockdep_on();
6831+}
6832+
1facf9fc 6833+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6834+{
5afbbe0d 6835+ aufs_bindex_t bbot;
1facf9fc 6836+ struct au_branch *br;
1facf9fc 6837+
6838+ if (!au_sbi(sb)->si_dbgaufs)
6839+ return;
6840+
5afbbe0d
AM
6841+ bbot = au_sbbot(sb);
6842+ for (; bindex <= bbot; bindex++) {
1facf9fc 6843+ br = au_sbr(sb, bindex);
062440b3
AM
6844+ dbgaufs_xino_del(br);
6845+ }
6846+}
6847+
acd2b654
AM
6848+static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6849+ unsigned int idx, struct dentry *parent,
6850+ struct au_sbinfo *sbinfo)
062440b3
AM
6851+{
6852+ struct au_branch *br;
6853+ struct dentry *d;
acd2b654
AM
6854+ /* "xi" bindex(5) "-" idx(2) NULL */
6855+ char name[sizeof(DbgaufsXi_PREFIX) + 8];
062440b3 6856+
acd2b654
AM
6857+ if (!idx)
6858+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6859+ else
6860+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6861+ bindex, idx);
062440b3
AM
6862+ br = au_sbr(sb, bindex);
6863+ if (br->br_dbgaufs) {
6864+ struct qstr qstr = QSTR_INIT(name, strlen(name));
6865+
6866+ if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6867+ /* debugfs acquires the parent i_mutex */
6868+ lockdep_off();
6869+ d = debugfs_rename(parent, br->br_dbgaufs, parent,
6870+ name);
6871+ lockdep_on();
6872+ if (unlikely(!d))
6873+ pr_warn("failed renaming %pd/%s, ignored.\n",
6874+ parent, name);
6875+ }
6876+ } else {
e2f27e51 6877+ lockdep_off();
062440b3
AM
6878+ br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6879+ sbinfo, &dbgaufs_xino_fop);
e2f27e51 6880+ lockdep_on();
062440b3 6881+ if (unlikely(!br->br_dbgaufs))
acd2b654 6882+ pr_warn("failed creating %pd/%s, ignored.\n",
062440b3 6883+ parent, name);
1facf9fc 6884+ }
6885+}
6886+
acd2b654
AM
6887+static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6888+ struct dentry *parent, struct au_sbinfo *sbinfo)
6889+{
6890+ struct au_branch *br;
6891+ struct au_xino *xi;
6892+ unsigned int u;
6893+
6894+ br = au_sbr(sb, bindex);
6895+ xi = br->br_xino;
6896+ for (u = 0; u < xi->xi_nfile; u++)
6897+ dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6898+}
6899+
062440b3 6900+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
1facf9fc 6901+{
6902+ struct au_sbinfo *sbinfo;
6903+ struct dentry *parent;
5afbbe0d 6904+ aufs_bindex_t bbot;
062440b3
AM
6905+
6906+ if (!au_opt_test(au_mntflags(sb), XINO))
6907+ return;
1facf9fc 6908+
6909+ sbinfo = au_sbi(sb);
6910+ parent = sbinfo->si_dbgaufs;
6911+ if (!parent)
6912+ return;
6913+
5afbbe0d 6914+ bbot = au_sbbot(sb);
062440b3
AM
6915+ if (topdown)
6916+ for (; bindex <= bbot; bindex++)
6917+ dbgaufs_br_add(sb, bindex, parent, sbinfo);
6918+ else
6919+ for (; bbot >= bindex; bbot--)
6920+ dbgaufs_br_add(sb, bbot, parent, sbinfo);
1facf9fc 6921+}
6922+
6923+/* ---------------------------------------------------------------------- */
6924+
6925+#ifdef CONFIG_AUFS_EXPORT
6926+static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6927+{
6928+ int err;
6929+ struct au_sbinfo *sbinfo;
6930+ struct super_block *sb;
6931+
6932+ sbinfo = inode->i_private;
6933+ sb = sbinfo->si_sb;
6934+ si_noflush_read_lock(sb);
062440b3 6935+ err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
1facf9fc 6936+ si_read_unlock(sb);
6937+ return err;
6938+}
6939+
6940+static const struct file_operations dbgaufs_xigen_fop = {
4a4d8108 6941+ .owner = THIS_MODULE,
1facf9fc 6942+ .open = dbgaufs_xigen_open,
6943+ .release = dbgaufs_xi_release,
6944+ .read = dbgaufs_xi_read
6945+};
6946+
6947+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6948+{
6949+ int err;
6950+
dece6358 6951+ /*
c1595e42 6952+ * This function is a dynamic '__init' function actually,
dece6358
AM
6953+ * so the tiny check for si_rwsem is unnecessary.
6954+ */
6955+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6956+
1facf9fc 6957+ err = -EIO;
6958+ sbinfo->si_dbgaufs_xigen = debugfs_create_file
6959+ ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6960+ &dbgaufs_xigen_fop);
6961+ if (sbinfo->si_dbgaufs_xigen)
6962+ err = 0;
6963+
6964+ return err;
6965+}
6966+#else
6967+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6968+{
6969+ return 0;
6970+}
6971+#endif /* CONFIG_AUFS_EXPORT */
6972+
6973+/* ---------------------------------------------------------------------- */
6974+
6975+void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6976+{
dece6358 6977+ /*
7e9cd9fe 6978+ * This function is a dynamic '__fin' function actually,
dece6358
AM
6979+ * so the tiny check for si_rwsem is unnecessary.
6980+ */
6981+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6982+
1facf9fc 6983+ debugfs_remove_recursive(sbinfo->si_dbgaufs);
6984+ sbinfo->si_dbgaufs = NULL;
1facf9fc 6985+}
6986+
6987+int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6988+{
6989+ int err;
6990+ char name[SysaufsSiNameLen];
6991+
dece6358 6992+ /*
c1595e42 6993+ * This function is a dynamic '__init' function actually,
dece6358
AM
6994+ * so the tiny check for si_rwsem is unnecessary.
6995+ */
6996+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6997+
1facf9fc 6998+ err = -ENOENT;
6999+ if (!dbgaufs) {
7000+ AuErr1("/debug/aufs is uninitialized\n");
7001+ goto out;
7002+ }
7003+
7004+ err = -EIO;
7005+ sysaufs_name(sbinfo, name);
7006+ sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
7007+ if (unlikely(!sbinfo->si_dbgaufs))
7008+ goto out;
1facf9fc 7009+
062440b3 7010+ /* regardless plink/noplink option */
86dc4139
AM
7011+ sbinfo->si_dbgaufs_plink = debugfs_create_file
7012+ ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
7013+ &dbgaufs_plink_fop);
7014+ if (unlikely(!sbinfo->si_dbgaufs_plink))
7015+ goto out_dir;
7016+
062440b3
AM
7017+ /* regardless xino/noxino option */
7018+ sbinfo->si_dbgaufs_xib = debugfs_create_file
7019+ ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
7020+ &dbgaufs_xib_fop);
7021+ if (unlikely(!sbinfo->si_dbgaufs_xib))
7022+ goto out_dir;
7023+
1facf9fc 7024+ err = dbgaufs_xigen_init(sbinfo);
7025+ if (!err)
7026+ goto out; /* success */
7027+
4f0767ce 7028+out_dir:
1facf9fc 7029+ dbgaufs_si_fin(sbinfo);
4f0767ce 7030+out:
062440b3
AM
7031+ if (unlikely(err))
7032+ pr_err("debugfs/aufs failed\n");
1facf9fc 7033+ return err;
7034+}
7035+
7036+/* ---------------------------------------------------------------------- */
7037+
7038+void dbgaufs_fin(void)
7039+{
7040+ debugfs_remove(dbgaufs);
7041+}
7042+
7043+int __init dbgaufs_init(void)
7044+{
7045+ int err;
7046+
7047+ err = -EIO;
7048+ dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
7049+ if (dbgaufs)
7050+ err = 0;
7051+ return err;
7052+}
7f207e10
AM
7053diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
7054--- /usr/share/empty/fs/aufs/dbgaufs.h 1970-01-01 01:00:00.000000000 +0100
ba1aed25 7055+++ linux/fs/aufs/dbgaufs.h 2019-03-05 12:13:00.135890907 +0100
062440b3
AM
7056@@ -0,0 +1,53 @@
7057+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7058+/*
ba1aed25 7059+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 7060+ *
7061+ * This program, aufs is free software; you can redistribute it and/or modify
7062+ * it under the terms of the GNU General Public License as published by
7063+ * the Free Software Foundation; either version 2 of the License, or
7064+ * (at your option) any later version.
dece6358
AM
7065+ *
7066+ * This program is distributed in the hope that it will be useful,
7067+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7068+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7069+ * GNU General Public License for more details.
7070+ *
7071+ * You should have received a copy of the GNU General Public License
523b37e3 7072+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7073+ */
7074+
7075+/*
7076+ * debugfs interface
7077+ */
7078+
7079+#ifndef __DBGAUFS_H__
7080+#define __DBGAUFS_H__
7081+
7082+#ifdef __KERNEL__
7083+
dece6358 7084+struct super_block;
1facf9fc 7085+struct au_sbinfo;
062440b3 7086+struct au_branch;
dece6358 7087+
1facf9fc 7088+#ifdef CONFIG_DEBUG_FS
7089+/* dbgaufs.c */
062440b3 7090+void dbgaufs_xino_del(struct au_branch *br);
1facf9fc 7091+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
062440b3 7092+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
1facf9fc 7093+void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
7094+int dbgaufs_si_init(struct au_sbinfo *sbinfo);
7095+void dbgaufs_fin(void);
7096+int __init dbgaufs_init(void);
1facf9fc 7097+#else
062440b3 7098+AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
4a4d8108 7099+AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
062440b3
AM
7100+AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
7101+ int topdown)
4a4d8108
AM
7102+AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
7103+AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
7104+AuStubVoid(dbgaufs_fin, void)
7105+AuStubInt0(__init dbgaufs_init, void)
1facf9fc 7106+#endif /* CONFIG_DEBUG_FS */
7107+
7108+#endif /* __KERNEL__ */
7109+#endif /* __DBGAUFS_H__ */
7f207e10
AM
7110diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
7111--- /usr/share/empty/fs/aufs/dcsub.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 7112+++ linux/fs/aufs/dcsub.c 2019-03-05 12:13:00.135890907 +0100
cd7a4cd9
AM
7113@@ -0,0 +1,225 @@
7114+// SPDX-License-Identifier: GPL-2.0
1facf9fc 7115+/*
ba1aed25 7116+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 7117+ *
7118+ * This program, aufs is free software; you can redistribute it and/or modify
7119+ * it under the terms of the GNU General Public License as published by
7120+ * the Free Software Foundation; either version 2 of the License, or
7121+ * (at your option) any later version.
dece6358
AM
7122+ *
7123+ * This program is distributed in the hope that it will be useful,
7124+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7125+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7126+ * GNU General Public License for more details.
7127+ *
7128+ * You should have received a copy of the GNU General Public License
523b37e3 7129+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7130+ */
7131+
7132+/*
7133+ * sub-routines for dentry cache
7134+ */
7135+
7136+#include "aufs.h"
7137+
7138+static void au_dpage_free(struct au_dpage *dpage)
7139+{
7140+ int i;
7141+ struct dentry **p;
7142+
7143+ p = dpage->dentries;
7144+ for (i = 0; i < dpage->ndentry; i++)
7145+ dput(*p++);
1c60b727 7146+ free_page((unsigned long)dpage->dentries);
1facf9fc 7147+}
7148+
7149+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
7150+{
7151+ int err;
7152+ void *p;
7153+
7154+ err = -ENOMEM;
7155+ dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
7156+ if (unlikely(!dpages->dpages))
7157+ goto out;
7158+
7159+ p = (void *)__get_free_page(gfp);
7160+ if (unlikely(!p))
7161+ goto out_dpages;
7162+
7163+ dpages->dpages[0].ndentry = 0;
7164+ dpages->dpages[0].dentries = p;
7165+ dpages->ndpage = 1;
7166+ return 0; /* success */
7167+
4f0767ce 7168+out_dpages:
9f237c51 7169+ au_kfree_try_rcu(dpages->dpages);
4f0767ce 7170+out:
1facf9fc 7171+ return err;
7172+}
7173+
7174+void au_dpages_free(struct au_dcsub_pages *dpages)
7175+{
7176+ int i;
7177+ struct au_dpage *p;
7178+
7179+ p = dpages->dpages;
7180+ for (i = 0; i < dpages->ndpage; i++)
7181+ au_dpage_free(p++);
9f237c51 7182+ au_kfree_try_rcu(dpages->dpages);
1facf9fc 7183+}
7184+
7185+static int au_dpages_append(struct au_dcsub_pages *dpages,
7186+ struct dentry *dentry, gfp_t gfp)
7187+{
7188+ int err, sz;
7189+ struct au_dpage *dpage;
7190+ void *p;
7191+
7192+ dpage = dpages->dpages + dpages->ndpage - 1;
7193+ sz = PAGE_SIZE / sizeof(dentry);
7194+ if (unlikely(dpage->ndentry >= sz)) {
7195+ AuLabel(new dpage);
7196+ err = -ENOMEM;
7197+ sz = dpages->ndpage * sizeof(*dpages->dpages);
7198+ p = au_kzrealloc(dpages->dpages, sz,
e2f27e51
AM
7199+ sz + sizeof(*dpages->dpages), gfp,
7200+ /*may_shrink*/0);
1facf9fc 7201+ if (unlikely(!p))
7202+ goto out;
7203+
7204+ dpages->dpages = p;
7205+ dpage = dpages->dpages + dpages->ndpage;
7206+ p = (void *)__get_free_page(gfp);
7207+ if (unlikely(!p))
7208+ goto out;
7209+
7210+ dpage->ndentry = 0;
7211+ dpage->dentries = p;
7212+ dpages->ndpage++;
7213+ }
7214+
c1595e42 7215+ AuDebugOn(au_dcount(dentry) <= 0);
027c5e7a 7216+ dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
1facf9fc 7217+ return 0; /* success */
7218+
4f0767ce 7219+out:
1facf9fc 7220+ return err;
7221+}
7222+
c1595e42
JR
7223+/* todo: BAD approach */
7224+/* copied from linux/fs/dcache.c */
7225+enum d_walk_ret {
7226+ D_WALK_CONTINUE,
7227+ D_WALK_QUIT,
7228+ D_WALK_NORETRY,
7229+ D_WALK_SKIP,
7230+};
7231+
7232+extern void d_walk(struct dentry *parent, void *data,
cd7a4cd9 7233+ enum d_walk_ret (*enter)(void *, struct dentry *));
c1595e42
JR
7234+
7235+struct ac_dpages_arg {
1facf9fc 7236+ int err;
c1595e42
JR
7237+ struct au_dcsub_pages *dpages;
7238+ struct super_block *sb;
7239+ au_dpages_test test;
7240+ void *arg;
7241+};
1facf9fc 7242+
c1595e42
JR
7243+static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7244+{
7245+ enum d_walk_ret ret;
7246+ struct ac_dpages_arg *arg = _arg;
1facf9fc 7247+
c1595e42
JR
7248+ ret = D_WALK_CONTINUE;
7249+ if (dentry->d_sb == arg->sb
7250+ && !IS_ROOT(dentry)
7251+ && au_dcount(dentry) > 0
7252+ && au_di(dentry)
7253+ && (!arg->test || arg->test(dentry, arg->arg))) {
7254+ arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7255+ if (unlikely(arg->err))
7256+ ret = D_WALK_QUIT;
1facf9fc 7257+ }
7258+
c1595e42
JR
7259+ return ret;
7260+}
027c5e7a 7261+
c1595e42
JR
7262+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7263+ au_dpages_test test, void *arg)
7264+{
7265+ struct ac_dpages_arg args = {
7266+ .err = 0,
7267+ .dpages = dpages,
7268+ .sb = root->d_sb,
7269+ .test = test,
7270+ .arg = arg
7271+ };
027c5e7a 7272+
cd7a4cd9 7273+ d_walk(root, &args, au_call_dpages_append);
c1595e42
JR
7274+
7275+ return args.err;
1facf9fc 7276+}
7277+
7278+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7279+ int do_include, au_dpages_test test, void *arg)
7280+{
7281+ int err;
7282+
7283+ err = 0;
027c5e7a
AM
7284+ write_seqlock(&rename_lock);
7285+ spin_lock(&dentry->d_lock);
7286+ if (do_include
c1595e42 7287+ && au_dcount(dentry) > 0
027c5e7a 7288+ && (!test || test(dentry, arg)))
1facf9fc 7289+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
7290+ spin_unlock(&dentry->d_lock);
7291+ if (unlikely(err))
7292+ goto out;
7293+
7294+ /*
523b37e3 7295+ * RCU for vfsmount is unnecessary since this is a traverse in a single
027c5e7a
AM
7296+ * mount
7297+ */
1facf9fc 7298+ while (!IS_ROOT(dentry)) {
027c5e7a
AM
7299+ dentry = dentry->d_parent; /* rename_lock is locked */
7300+ spin_lock(&dentry->d_lock);
c1595e42 7301+ if (au_dcount(dentry) > 0
027c5e7a 7302+ && (!test || test(dentry, arg)))
1facf9fc 7303+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
7304+ spin_unlock(&dentry->d_lock);
7305+ if (unlikely(err))
7306+ break;
1facf9fc 7307+ }
7308+
4f0767ce 7309+out:
027c5e7a 7310+ write_sequnlock(&rename_lock);
1facf9fc 7311+ return err;
7312+}
7313+
027c5e7a
AM
7314+static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7315+{
7316+ return au_di(dentry) && dentry->d_sb == arg;
7317+}
7318+
7319+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7320+ struct dentry *dentry, int do_include)
7321+{
7322+ return au_dcsub_pages_rev(dpages, dentry, do_include,
7323+ au_dcsub_dpages_aufs, dentry->d_sb);
7324+}
7325+
4a4d8108 7326+int au_test_subdir(struct dentry *d1, struct dentry *d2)
1facf9fc 7327+{
4a4d8108
AM
7328+ struct path path[2] = {
7329+ {
7330+ .dentry = d1
7331+ },
7332+ {
7333+ .dentry = d2
7334+ }
7335+ };
1facf9fc 7336+
4a4d8108 7337+ return path_is_under(path + 0, path + 1);
1facf9fc 7338+}
7f207e10
AM
7339diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7340--- /usr/share/empty/fs/aufs/dcsub.h 1970-01-01 01:00:00.000000000 +0100
ba1aed25 7341+++ linux/fs/aufs/dcsub.h 2019-03-05 12:13:00.135890907 +0100
062440b3
AM
7342@@ -0,0 +1,137 @@
7343+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7344+/*
ba1aed25 7345+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 7346+ *
7347+ * This program, aufs is free software; you can redistribute it and/or modify
7348+ * it under the terms of the GNU General Public License as published by
7349+ * the Free Software Foundation; either version 2 of the License, or
7350+ * (at your option) any later version.
dece6358
AM
7351+ *
7352+ * This program is distributed in the hope that it will be useful,
7353+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7354+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7355+ * GNU General Public License for more details.
7356+ *
7357+ * You should have received a copy of the GNU General Public License
523b37e3 7358+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7359+ */
7360+
7361+/*
7362+ * sub-routines for dentry cache
7363+ */
7364+
7365+#ifndef __AUFS_DCSUB_H__
7366+#define __AUFS_DCSUB_H__
7367+
7368+#ifdef __KERNEL__
7369+
7f207e10 7370+#include <linux/dcache.h>
027c5e7a 7371+#include <linux/fs.h>
dece6358 7372+
1facf9fc 7373+struct au_dpage {
7374+ int ndentry;
7375+ struct dentry **dentries;
7376+};
7377+
7378+struct au_dcsub_pages {
7379+ int ndpage;
7380+ struct au_dpage *dpages;
7381+};
7382+
7383+/* ---------------------------------------------------------------------- */
7384+
7f207e10 7385+/* dcsub.c */
1facf9fc 7386+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7387+void au_dpages_free(struct au_dcsub_pages *dpages);
7388+typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7389+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7390+ au_dpages_test test, void *arg);
7391+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7392+ int do_include, au_dpages_test test, void *arg);
027c5e7a
AM
7393+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7394+ struct dentry *dentry, int do_include);
4a4d8108 7395+int au_test_subdir(struct dentry *d1, struct dentry *d2);
1facf9fc 7396+
7f207e10
AM
7397+/* ---------------------------------------------------------------------- */
7398+
523b37e3
AM
7399+/*
7400+ * todo: in linux-3.13, several similar (but faster) helpers are added to
7401+ * include/linux/dcache.h. Try them (in the future).
7402+ */
7403+
027c5e7a
AM
7404+static inline int au_d_hashed_positive(struct dentry *d)
7405+{
7406+ int err;
5527c038 7407+ struct inode *inode = d_inode(d);
076b876e 7408+
027c5e7a 7409+ err = 0;
5527c038
JR
7410+ if (unlikely(d_unhashed(d)
7411+ || d_is_negative(d)
7412+ || !inode->i_nlink))
027c5e7a
AM
7413+ err = -ENOENT;
7414+ return err;
7415+}
7416+
38d290e6
JR
7417+static inline int au_d_linkable(struct dentry *d)
7418+{
7419+ int err;
5527c038 7420+ struct inode *inode = d_inode(d);
076b876e 7421+
38d290e6
JR
7422+ err = au_d_hashed_positive(d);
7423+ if (err
5527c038 7424+ && d_is_positive(d)
38d290e6
JR
7425+ && (inode->i_state & I_LINKABLE))
7426+ err = 0;
7427+ return err;
7428+}
7429+
027c5e7a
AM
7430+static inline int au_d_alive(struct dentry *d)
7431+{
7432+ int err;
7433+ struct inode *inode;
076b876e 7434+
027c5e7a
AM
7435+ err = 0;
7436+ if (!IS_ROOT(d))
7437+ err = au_d_hashed_positive(d);
7438+ else {
5527c038
JR
7439+ inode = d_inode(d);
7440+ if (unlikely(d_unlinked(d)
7441+ || d_is_negative(d)
7442+ || !inode->i_nlink))
027c5e7a
AM
7443+ err = -ENOENT;
7444+ }
7445+ return err;
7446+}
7447+
7448+static inline int au_alive_dir(struct dentry *d)
7f207e10 7449+{
027c5e7a 7450+ int err;
076b876e 7451+
027c5e7a 7452+ err = au_d_alive(d);
5527c038 7453+ if (unlikely(err || IS_DEADDIR(d_inode(d))))
027c5e7a
AM
7454+ err = -ENOENT;
7455+ return err;
7f207e10
AM
7456+}
7457+
38d290e6
JR
7458+static inline int au_qstreq(struct qstr *a, struct qstr *b)
7459+{
7460+ return a->len == b->len
7461+ && !memcmp(a->name, b->name, a->len);
7462+}
7463+
7e9cd9fe
AM
7464+/*
7465+ * by the commit
7466+ * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7467+ * taking d_lock
7468+ * the type of d_lockref.count became int, but the inlined function d_count()
7469+ * still returns unsigned int.
7470+ * I don't know why. Maybe it is for every d_count() users?
7471+ * Anyway au_dcount() lives on.
7472+ */
c1595e42
JR
7473+static inline int au_dcount(struct dentry *d)
7474+{
7475+ return (int)d_count(d);
7476+}
7477+
1facf9fc 7478+#endif /* __KERNEL__ */
7479+#endif /* __AUFS_DCSUB_H__ */
7f207e10
AM
7480diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7481--- /usr/share/empty/fs/aufs/debug.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 7482+++ linux/fs/aufs/debug.c 2019-03-05 12:13:00.135890907 +0100
acd2b654 7483@@ -0,0 +1,440 @@
cd7a4cd9 7484+// SPDX-License-Identifier: GPL-2.0
1facf9fc 7485+/*
ba1aed25 7486+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 7487+ *
7488+ * This program, aufs is free software; you can redistribute it and/or modify
7489+ * it under the terms of the GNU General Public License as published by
7490+ * the Free Software Foundation; either version 2 of the License, or
7491+ * (at your option) any later version.
dece6358
AM
7492+ *
7493+ * This program is distributed in the hope that it will be useful,
7494+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7495+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7496+ * GNU General Public License for more details.
7497+ *
7498+ * You should have received a copy of the GNU General Public License
523b37e3 7499+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7500+ */
7501+
7502+/*
7503+ * debug print functions
7504+ */
7505+
7506+#include "aufs.h"
7507+
392086de
AM
7508+/* Returns 0, or -errno. arg is in kp->arg. */
7509+static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7510+{
7511+ int err, n;
7512+
7513+ err = kstrtoint(val, 0, &n);
7514+ if (!err) {
7515+ if (n > 0)
7516+ au_debug_on();
7517+ else
7518+ au_debug_off();
7519+ }
7520+ return err;
7521+}
7522+
7523+/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
7524+static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7525+{
7526+ atomic_t *a;
7527+
7528+ a = kp->arg;
7529+ return sprintf(buffer, "%d", atomic_read(a));
7530+}
7531+
7532+static struct kernel_param_ops param_ops_atomic_t = {
7533+ .set = param_atomic_t_set,
7534+ .get = param_atomic_t_get
7535+ /* void (*free)(void *arg) */
7536+};
7537+
7538+atomic_t aufs_debug = ATOMIC_INIT(0);
1facf9fc 7539+MODULE_PARM_DESC(debug, "debug print");
cd7a4cd9 7540+module_param_named(debug, aufs_debug, atomic_t, 0664);
1facf9fc 7541+
c1595e42 7542+DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
1facf9fc 7543+char *au_plevel = KERN_DEBUG;
e49829fe
JR
7544+#define dpri(fmt, ...) do { \
7545+ if ((au_plevel \
7546+ && strcmp(au_plevel, KERN_DEBUG)) \
7547+ || au_debug_test()) \
7548+ printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
1facf9fc 7549+} while (0)
7550+
7551+/* ---------------------------------------------------------------------- */
7552+
7553+void au_dpri_whlist(struct au_nhash *whlist)
7554+{
7555+ unsigned long ul, n;
7556+ struct hlist_head *head;
c06a8ce3 7557+ struct au_vdir_wh *pos;
1facf9fc 7558+
7559+ n = whlist->nh_num;
7560+ head = whlist->nh_head;
7561+ for (ul = 0; ul < n; ul++) {
c06a8ce3 7562+ hlist_for_each_entry(pos, head, wh_hash)
1facf9fc 7563+ dpri("b%d, %.*s, %d\n",
c06a8ce3
AM
7564+ pos->wh_bindex,
7565+ pos->wh_str.len, pos->wh_str.name,
7566+ pos->wh_str.len);
1facf9fc 7567+ head++;
7568+ }
7569+}
7570+
7571+void au_dpri_vdir(struct au_vdir *vdir)
7572+{
7573+ unsigned long ul;
7574+ union au_vdir_deblk_p p;
7575+ unsigned char *o;
7576+
7577+ if (!vdir || IS_ERR(vdir)) {
7578+ dpri("err %ld\n", PTR_ERR(vdir));
7579+ return;
7580+ }
7581+
be118d29 7582+ dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
1facf9fc 7583+ vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7584+ vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7585+ for (ul = 0; ul < vdir->vd_nblk; ul++) {
7586+ p.deblk = vdir->vd_deblk[ul];
7587+ o = p.deblk;
7588+ dpri("[%lu]: %p\n", ul, o);
7589+ }
7590+}
7591+
53392da6 7592+static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
1facf9fc 7593+ struct dentry *wh)
7594+{
7595+ char *n = NULL;
7596+ int l = 0;
7597+
7598+ if (!inode || IS_ERR(inode)) {
7599+ dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7600+ return -1;
7601+ }
7602+
c2b27bf2 7603+ /* the type of i_blocks depends upon CONFIG_LBDAF */
1facf9fc 7604+ BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7605+ && sizeof(inode->i_blocks) != sizeof(u64));
7606+ if (wh) {
7607+ n = (void *)wh->d_name.name;
7608+ l = wh->d_name.len;
7609+ }
7610+
53392da6
AM
7611+ dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7612+ " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7613+ bindex, inode,
1facf9fc 7614+ inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7615+ atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7616+ i_size_read(inode), (unsigned long long)inode->i_blocks,
cd7a4cd9 7617+ hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
1facf9fc 7618+ inode->i_mapping ? inode->i_mapping->nrpages : 0,
be118d29 7619+ inode->i_state, inode->i_flags, inode_peek_iversion(inode),
b752ccd1 7620+ inode->i_generation,
1facf9fc 7621+ l ? ", wh " : "", l, n);
7622+ return 0;
7623+}
7624+
7625+void au_dpri_inode(struct inode *inode)
7626+{
7627+ struct au_iinfo *iinfo;
5afbbe0d 7628+ struct au_hinode *hi;
1facf9fc 7629+ aufs_bindex_t bindex;
53392da6 7630+ int err, hn;
1facf9fc 7631+
53392da6 7632+ err = do_pri_inode(-1, inode, -1, NULL);
5afbbe0d 7633+ if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
1facf9fc 7634+ return;
7635+
7636+ iinfo = au_ii(inode);
5afbbe0d
AM
7637+ dpri("i-1: btop %d, bbot %d, gen %d\n",
7638+ iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7639+ if (iinfo->ii_btop < 0)
1facf9fc 7640+ return;
53392da6 7641+ hn = 0;
5afbbe0d
AM
7642+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7643+ hi = au_hinode(iinfo, bindex);
7644+ hn = !!au_hn(hi);
7645+ do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
53392da6 7646+ }
1facf9fc 7647+}
7648+
2cbb1c4b
JR
7649+void au_dpri_dalias(struct inode *inode)
7650+{
7651+ struct dentry *d;
7652+
7653+ spin_lock(&inode->i_lock);
c1595e42 7654+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
2cbb1c4b
JR
7655+ au_dpri_dentry(d);
7656+ spin_unlock(&inode->i_lock);
7657+}
7658+
1facf9fc 7659+static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7660+{
7661+ struct dentry *wh = NULL;
53392da6 7662+ int hn;
5afbbe0d 7663+ struct inode *inode;
076b876e 7664+ struct au_iinfo *iinfo;
5afbbe0d 7665+ struct au_hinode *hi;
1facf9fc 7666+
7667+ if (!dentry || IS_ERR(dentry)) {
7668+ dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7669+ return -1;
7670+ }
7671+ /* do not call dget_parent() here */
027c5e7a 7672+ /* note: access d_xxx without d_lock */
523b37e3
AM
7673+ dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7674+ bindex, dentry, dentry,
1facf9fc 7675+ dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
c1595e42 7676+ au_dcount(dentry), dentry->d_flags,
523b37e3 7677+ d_unhashed(dentry) ? "un" : "");
53392da6 7678+ hn = -1;
5afbbe0d
AM
7679+ inode = NULL;
7680+ if (d_is_positive(dentry))
7681+ inode = d_inode(dentry);
7682+ if (inode
7683+ && au_test_aufs(dentry->d_sb)
7684+ && bindex >= 0
7685+ && !au_is_bad_inode(inode)) {
7686+ iinfo = au_ii(inode);
7687+ hi = au_hinode(iinfo, bindex);
7688+ hn = !!au_hn(hi);
7689+ wh = hi->hi_whdentry;
7690+ }
7691+ do_pri_inode(bindex, inode, hn, wh);
1facf9fc 7692+ return 0;
7693+}
7694+
7695+void au_dpri_dentry(struct dentry *dentry)
7696+{
7697+ struct au_dinfo *dinfo;
7698+ aufs_bindex_t bindex;
7699+ int err;
7700+
7701+ err = do_pri_dentry(-1, dentry);
7702+ if (err || !au_test_aufs(dentry->d_sb))
7703+ return;
7704+
7705+ dinfo = au_di(dentry);
7706+ if (!dinfo)
7707+ return;
5afbbe0d
AM
7708+ dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7709+ dinfo->di_btop, dinfo->di_bbot,
38d290e6
JR
7710+ dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7711+ dinfo->di_tmpfile);
5afbbe0d 7712+ if (dinfo->di_btop < 0)
1facf9fc 7713+ return;
5afbbe0d
AM
7714+ for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7715+ do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
1facf9fc 7716+}
7717+
7718+static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7719+{
7720+ char a[32];
7721+
7722+ if (!file || IS_ERR(file)) {
7723+ dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7724+ return -1;
7725+ }
7726+ a[0] = 0;
7727+ if (bindex < 0
b912730e 7728+ && !IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7729+ && au_test_aufs(file->f_path.dentry->d_sb)
1facf9fc 7730+ && au_fi(file))
e49829fe 7731+ snprintf(a, sizeof(a), ", gen %d, mmapped %d",
2cbb1c4b 7732+ au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
b752ccd1 7733+ dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
1facf9fc 7734+ bindex, file->f_mode, file->f_flags, (long)file_count(file),
b752ccd1 7735+ file->f_version, file->f_pos, a);
b912730e 7736+ if (!IS_ERR_OR_NULL(file->f_path.dentry))
2000de60 7737+ do_pri_dentry(bindex, file->f_path.dentry);
1facf9fc 7738+ return 0;
7739+}
7740+
7741+void au_dpri_file(struct file *file)
7742+{
7743+ struct au_finfo *finfo;
4a4d8108
AM
7744+ struct au_fidir *fidir;
7745+ struct au_hfile *hfile;
1facf9fc 7746+ aufs_bindex_t bindex;
7747+ int err;
7748+
7749+ err = do_pri_file(-1, file);
2000de60 7750+ if (err
b912730e 7751+ || IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7752+ || !au_test_aufs(file->f_path.dentry->d_sb))
1facf9fc 7753+ return;
7754+
7755+ finfo = au_fi(file);
7756+ if (!finfo)
7757+ return;
4a4d8108 7758+ if (finfo->fi_btop < 0)
1facf9fc 7759+ return;
4a4d8108
AM
7760+ fidir = finfo->fi_hdir;
7761+ if (!fidir)
7762+ do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7763+ else
e49829fe
JR
7764+ for (bindex = finfo->fi_btop;
7765+ bindex >= 0 && bindex <= fidir->fd_bbot;
4a4d8108
AM
7766+ bindex++) {
7767+ hfile = fidir->fd_hfile + bindex;
7768+ do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7769+ }
1facf9fc 7770+}
7771+
7772+static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7773+{
7774+ struct vfsmount *mnt;
7775+ struct super_block *sb;
7776+
7777+ if (!br || IS_ERR(br))
7778+ goto out;
86dc4139 7779+ mnt = au_br_mnt(br);
1facf9fc 7780+ if (!mnt || IS_ERR(mnt))
7781+ goto out;
7782+ sb = mnt->mnt_sb;
7783+ if (!sb || IS_ERR(sb))
7784+ goto out;
7785+
acd2b654 7786+ dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
b752ccd1 7787+ "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
1facf9fc 7788+ "xino %d\n",
acd2b654
AM
7789+ bindex, br->br_perm, br->br_id, br->br_wbr,
7790+ au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
b752ccd1 7791+ sb->s_flags, sb->s_count,
acd2b654
AM
7792+ atomic_read(&sb->s_active),
7793+ !!au_xino_file(br->br_xino, /*idx*/-1));
1facf9fc 7794+ return 0;
7795+
4f0767ce 7796+out:
1facf9fc 7797+ dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7798+ return -1;
7799+}
7800+
7801+void au_dpri_sb(struct super_block *sb)
7802+{
7803+ struct au_sbinfo *sbinfo;
7804+ aufs_bindex_t bindex;
7805+ int err;
acd2b654 7806+ /* to reduce stack size */
1facf9fc 7807+ struct {
7808+ struct vfsmount mnt;
7809+ struct au_branch fake;
7810+ } *a;
7811+
7812+ /* this function can be called from magic sysrq */
7813+ a = kzalloc(sizeof(*a), GFP_ATOMIC);
7814+ if (unlikely(!a)) {
7815+ dpri("no memory\n");
7816+ return;
7817+ }
7818+
7819+ a->mnt.mnt_sb = sb;
86dc4139 7820+ a->fake.br_path.mnt = &a->mnt;
1facf9fc 7821+ err = do_pri_br(-1, &a->fake);
9f237c51 7822+ au_kfree_rcu(a);
1facf9fc 7823+ dpri("dev 0x%x\n", sb->s_dev);
7824+ if (err || !au_test_aufs(sb))
7825+ return;
7826+
7827+ sbinfo = au_sbi(sb);
7828+ if (!sbinfo)
7829+ return;
f0c0a007
AM
7830+ dpri("nw %d, gen %u, kobj %d\n",
7831+ atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
521ced18 7832+ kref_read(&sbinfo->si_kobj.kref));
5afbbe0d 7833+ for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
1facf9fc 7834+ do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7835+}
7836+
7837+/* ---------------------------------------------------------------------- */
7838+
027c5e7a
AM
7839+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7840+{
5527c038 7841+ struct inode *h_inode, *inode = d_inode(dentry);
027c5e7a 7842+ struct dentry *h_dentry;
5afbbe0d 7843+ aufs_bindex_t bindex, bbot, bi;
027c5e7a
AM
7844+
7845+ if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7846+ return;
7847+
5afbbe0d
AM
7848+ bbot = au_dbbot(dentry);
7849+ bi = au_ibbot(inode);
7850+ if (bi < bbot)
7851+ bbot = bi;
7852+ bindex = au_dbtop(dentry);
7853+ bi = au_ibtop(inode);
027c5e7a
AM
7854+ if (bi > bindex)
7855+ bindex = bi;
7856+
5afbbe0d 7857+ for (; bindex <= bbot; bindex++) {
027c5e7a
AM
7858+ h_dentry = au_h_dptr(dentry, bindex);
7859+ if (!h_dentry)
7860+ continue;
7861+ h_inode = au_h_iptr(inode, bindex);
5527c038 7862+ if (unlikely(h_inode != d_inode(h_dentry))) {
392086de 7863+ au_debug_on();
027c5e7a
AM
7864+ AuDbg("b%d, %s:%d\n", bindex, func, line);
7865+ AuDbgDentry(dentry);
7866+ AuDbgInode(inode);
392086de 7867+ au_debug_off();
027c5e7a
AM
7868+ BUG();
7869+ }
7870+ }
7871+}
7872+
1facf9fc 7873+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7874+{
7875+ int err, i, j;
7876+ struct au_dcsub_pages dpages;
7877+ struct au_dpage *dpage;
7878+ struct dentry **dentries;
7879+
7880+ err = au_dpages_init(&dpages, GFP_NOFS);
7881+ AuDebugOn(err);
027c5e7a 7882+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
1facf9fc 7883+ AuDebugOn(err);
7884+ for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7885+ dpage = dpages.dpages + i;
7886+ dentries = dpage->dentries;
7887+ for (j = dpage->ndentry - 1; !err && j >= 0; j--)
027c5e7a 7888+ AuDebugOn(au_digen_test(dentries[j], sigen));
1facf9fc 7889+ }
7890+ au_dpages_free(&dpages);
7891+}
7892+
1facf9fc 7893+void au_dbg_verify_kthread(void)
7894+{
53392da6 7895+ if (au_wkq_test()) {
1facf9fc 7896+ au_dbg_blocked();
1e00d052
AM
7897+ /*
7898+ * It may be recursive, but udba=notify between two aufs mounts,
7899+ * where a single ro branch is shared, is not a problem.
7900+ */
7901+ /* WARN_ON(1); */
1facf9fc 7902+ }
7903+}
7904+
7905+/* ---------------------------------------------------------------------- */
7906+
1facf9fc 7907+int __init au_debug_init(void)
7908+{
7909+ aufs_bindex_t bindex;
7910+ struct au_vdir_destr destr;
7911+
7912+ bindex = -1;
7913+ AuDebugOn(bindex >= 0);
7914+
7915+ destr.len = -1;
7916+ AuDebugOn(destr.len < NAME_MAX);
7917+
7918+#ifdef CONFIG_4KSTACKS
0c3ec466 7919+ pr_warn("CONFIG_4KSTACKS is defined.\n");
1facf9fc 7920+#endif
7921+
1facf9fc 7922+ return 0;
7923+}
7f207e10
AM
7924diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7925--- /usr/share/empty/fs/aufs/debug.h 1970-01-01 01:00:00.000000000 +0100
ba1aed25 7926+++ linux/fs/aufs/debug.h 2019-03-05 12:13:00.135890907 +0100
062440b3
AM
7927@@ -0,0 +1,226 @@
7928+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7929+/*
ba1aed25 7930+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 7931+ *
7932+ * This program, aufs is free software; you can redistribute it and/or modify
7933+ * it under the terms of the GNU General Public License as published by
7934+ * the Free Software Foundation; either version 2 of the License, or
7935+ * (at your option) any later version.
dece6358
AM
7936+ *
7937+ * This program is distributed in the hope that it will be useful,
7938+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7939+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7940+ * GNU General Public License for more details.
7941+ *
7942+ * You should have received a copy of the GNU General Public License
523b37e3 7943+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7944+ */
7945+
7946+/*
7947+ * debug print functions
7948+ */
7949+
7950+#ifndef __AUFS_DEBUG_H__
7951+#define __AUFS_DEBUG_H__
7952+
7953+#ifdef __KERNEL__
7954+
392086de 7955+#include <linux/atomic.h>
4a4d8108
AM
7956+#include <linux/module.h>
7957+#include <linux/kallsyms.h>
1facf9fc 7958+#include <linux/sysrq.h>
4a4d8108 7959+
1facf9fc 7960+#ifdef CONFIG_AUFS_DEBUG
7961+#define AuDebugOn(a) BUG_ON(a)
7962+
7963+/* module parameter */
392086de
AM
7964+extern atomic_t aufs_debug;
7965+static inline void au_debug_on(void)
1facf9fc 7966+{
392086de
AM
7967+ atomic_inc(&aufs_debug);
7968+}
7969+static inline void au_debug_off(void)
7970+{
7971+ atomic_dec_if_positive(&aufs_debug);
1facf9fc 7972+}
7973+
7974+static inline int au_debug_test(void)
7975+{
392086de 7976+ return atomic_read(&aufs_debug) > 0;
1facf9fc 7977+}
7978+#else
7979+#define AuDebugOn(a) do {} while (0)
392086de
AM
7980+AuStubVoid(au_debug_on, void)
7981+AuStubVoid(au_debug_off, void)
4a4d8108 7982+AuStubInt0(au_debug_test, void)
1facf9fc 7983+#endif /* CONFIG_AUFS_DEBUG */
7984+
392086de
AM
7985+#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7986+
1facf9fc 7987+/* ---------------------------------------------------------------------- */
7988+
7989+/* debug print */
7990+
4a4d8108 7991+#define AuDbg(fmt, ...) do { \
1facf9fc 7992+ if (au_debug_test()) \
4a4d8108 7993+ pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
1facf9fc 7994+} while (0)
4a4d8108
AM
7995+#define AuLabel(l) AuDbg(#l "\n")
7996+#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7997+#define AuWarn1(fmt, ...) do { \
1facf9fc 7998+ static unsigned char _c; \
7999+ if (!_c++) \
0c3ec466 8000+ pr_warn(fmt, ##__VA_ARGS__); \
1facf9fc 8001+} while (0)
8002+
4a4d8108 8003+#define AuErr1(fmt, ...) do { \
1facf9fc 8004+ static unsigned char _c; \
8005+ if (!_c++) \
4a4d8108 8006+ pr_err(fmt, ##__VA_ARGS__); \
1facf9fc 8007+} while (0)
8008+
4a4d8108 8009+#define AuIOErr1(fmt, ...) do { \
1facf9fc 8010+ static unsigned char _c; \
8011+ if (!_c++) \
4a4d8108 8012+ AuIOErr(fmt, ##__VA_ARGS__); \
1facf9fc 8013+} while (0)
8014+
8015+#define AuUnsupportMsg "This operation is not supported." \
8016+ " Please report this application to aufs-users ML."
4a4d8108
AM
8017+#define AuUnsupport(fmt, ...) do { \
8018+ pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
1facf9fc 8019+ dump_stack(); \
8020+} while (0)
8021+
8022+#define AuTraceErr(e) do { \
8023+ if (unlikely((e) < 0)) \
8024+ AuDbg("err %d\n", (int)(e)); \
8025+} while (0)
8026+
8027+#define AuTraceErrPtr(p) do { \
8028+ if (IS_ERR(p)) \
8029+ AuDbg("err %ld\n", PTR_ERR(p)); \
8030+} while (0)
8031+
8032+/* dirty macros for debug print, use with "%.*s" and caution */
8033+#define AuLNPair(qstr) (qstr)->len, (qstr)->name
1facf9fc 8034+
8035+/* ---------------------------------------------------------------------- */
8036+
dece6358 8037+struct dentry;
1facf9fc 8038+#ifdef CONFIG_AUFS_DEBUG
c1595e42 8039+extern struct mutex au_dbg_mtx;
1facf9fc 8040+extern char *au_plevel;
8041+struct au_nhash;
8042+void au_dpri_whlist(struct au_nhash *whlist);
8043+struct au_vdir;
8044+void au_dpri_vdir(struct au_vdir *vdir);
dece6358 8045+struct inode;
1facf9fc 8046+void au_dpri_inode(struct inode *inode);
2cbb1c4b 8047+void au_dpri_dalias(struct inode *inode);
1facf9fc 8048+void au_dpri_dentry(struct dentry *dentry);
dece6358 8049+struct file;
1facf9fc 8050+void au_dpri_file(struct file *filp);
dece6358 8051+struct super_block;
1facf9fc 8052+void au_dpri_sb(struct super_block *sb);
8053+
027c5e7a
AM
8054+#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
8055+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
1facf9fc 8056+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
1facf9fc 8057+void au_dbg_verify_kthread(void);
8058+
8059+int __init au_debug_init(void);
7e9cd9fe 8060+
1facf9fc 8061+#define AuDbgWhlist(w) do { \
c1595e42 8062+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8063+ AuDbg(#w "\n"); \
8064+ au_dpri_whlist(w); \
c1595e42 8065+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8066+} while (0)
8067+
8068+#define AuDbgVdir(v) do { \
c1595e42 8069+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8070+ AuDbg(#v "\n"); \
8071+ au_dpri_vdir(v); \
c1595e42 8072+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8073+} while (0)
8074+
8075+#define AuDbgInode(i) do { \
c1595e42 8076+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8077+ AuDbg(#i "\n"); \
8078+ au_dpri_inode(i); \
c1595e42 8079+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8080+} while (0)
8081+
2cbb1c4b 8082+#define AuDbgDAlias(i) do { \
c1595e42 8083+ mutex_lock(&au_dbg_mtx); \
2cbb1c4b
JR
8084+ AuDbg(#i "\n"); \
8085+ au_dpri_dalias(i); \
c1595e42 8086+ mutex_unlock(&au_dbg_mtx); \
2cbb1c4b
JR
8087+} while (0)
8088+
1facf9fc 8089+#define AuDbgDentry(d) do { \
c1595e42 8090+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8091+ AuDbg(#d "\n"); \
8092+ au_dpri_dentry(d); \
c1595e42 8093+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8094+} while (0)
8095+
8096+#define AuDbgFile(f) do { \
c1595e42 8097+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8098+ AuDbg(#f "\n"); \
8099+ au_dpri_file(f); \
c1595e42 8100+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8101+} while (0)
8102+
8103+#define AuDbgSb(sb) do { \
c1595e42 8104+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8105+ AuDbg(#sb "\n"); \
8106+ au_dpri_sb(sb); \
c1595e42 8107+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8108+} while (0)
8109+
4a4d8108
AM
8110+#define AuDbgSym(addr) do { \
8111+ char sym[KSYM_SYMBOL_LEN]; \
8112+ sprint_symbol(sym, (unsigned long)addr); \
8113+ AuDbg("%s\n", sym); \
8114+} while (0)
1facf9fc 8115+#else
027c5e7a 8116+AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
4a4d8108
AM
8117+AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
8118+AuStubVoid(au_dbg_verify_kthread, void)
8119+AuStubInt0(__init au_debug_init, void)
1facf9fc 8120+
1facf9fc 8121+#define AuDbgWhlist(w) do {} while (0)
8122+#define AuDbgVdir(v) do {} while (0)
8123+#define AuDbgInode(i) do {} while (0)
2cbb1c4b 8124+#define AuDbgDAlias(i) do {} while (0)
1facf9fc 8125+#define AuDbgDentry(d) do {} while (0)
8126+#define AuDbgFile(f) do {} while (0)
8127+#define AuDbgSb(sb) do {} while (0)
4a4d8108 8128+#define AuDbgSym(addr) do {} while (0)
1facf9fc 8129+#endif /* CONFIG_AUFS_DEBUG */
8130+
8131+/* ---------------------------------------------------------------------- */
8132+
8133+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
8134+int __init au_sysrq_init(void);
8135+void au_sysrq_fin(void);
8136+
8137+#ifdef CONFIG_HW_CONSOLE
8138+#define au_dbg_blocked() do { \
8139+ WARN_ON(1); \
0c5527e5 8140+ handle_sysrq('w'); \
1facf9fc 8141+} while (0)
8142+#else
4a4d8108 8143+AuStubVoid(au_dbg_blocked, void)
1facf9fc 8144+#endif
8145+
8146+#else
4a4d8108
AM
8147+AuStubInt0(__init au_sysrq_init, void)
8148+AuStubVoid(au_sysrq_fin, void)
8149+AuStubVoid(au_dbg_blocked, void)
1facf9fc 8150+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
8151+
8152+#endif /* __KERNEL__ */
8153+#endif /* __AUFS_DEBUG_H__ */
7f207e10
AM
8154diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
8155--- /usr/share/empty/fs/aufs/dentry.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 8156+++ linux/fs/aufs/dentry.c 2019-03-05 12:13:00.135890907 +0100
062440b3 8157@@ -0,0 +1,1153 @@
cd7a4cd9 8158+// SPDX-License-Identifier: GPL-2.0
1facf9fc 8159+/*
ba1aed25 8160+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 8161+ *
8162+ * This program, aufs is free software; you can redistribute it and/or modify
8163+ * it under the terms of the GNU General Public License as published by
8164+ * the Free Software Foundation; either version 2 of the License, or
8165+ * (at your option) any later version.
dece6358
AM
8166+ *
8167+ * This program is distributed in the hope that it will be useful,
8168+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
8169+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8170+ * GNU General Public License for more details.
8171+ *
8172+ * You should have received a copy of the GNU General Public License
523b37e3 8173+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 8174+ */
8175+
8176+/*
8177+ * lookup and dentry operations
8178+ */
8179+
dece6358 8180+#include <linux/namei.h>
1facf9fc 8181+#include "aufs.h"
8182+
1facf9fc 8183+/*
8184+ * returns positive/negative dentry, NULL or an error.
8185+ * NULL means whiteout-ed or not-found.
8186+ */
8187+static struct dentry*
8188+au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
8b6a4947 8189+ aufs_bindex_t bindex, struct au_do_lookup_args *args)
1facf9fc 8190+{
8191+ struct dentry *h_dentry;
2000de60 8192+ struct inode *h_inode;
1facf9fc 8193+ struct au_branch *br;
8194+ int wh_found, opq;
8195+ unsigned char wh_able;
8196+ const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
076b876e
AM
8197+ const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8198+ IGNORE_PERM);
1facf9fc 8199+
1facf9fc 8200+ wh_found = 0;
8201+ br = au_sbr(dentry->d_sb, bindex);
8202+ wh_able = !!au_br_whable(br->br_perm);
8203+ if (wh_able)
8b6a4947 8204+ wh_found = au_wh_test(h_parent, &args->whname, ignore_perm);
1facf9fc 8205+ h_dentry = ERR_PTR(wh_found);
8206+ if (!wh_found)
8207+ goto real_lookup;
8208+ if (unlikely(wh_found < 0))
8209+ goto out;
8210+
8211+ /* We found a whiteout */
5afbbe0d 8212+ /* au_set_dbbot(dentry, bindex); */
1facf9fc 8213+ au_set_dbwh(dentry, bindex);
8214+ if (!allow_neg)
8215+ return NULL; /* success */
8216+
4f0767ce 8217+real_lookup:
076b876e 8218+ if (!ignore_perm)
8b6a4947 8219+ h_dentry = vfsub_lkup_one(args->name, h_parent);
076b876e 8220+ else
8b6a4947 8221+ h_dentry = au_sio_lkup_one(args->name, h_parent);
2000de60
JR
8222+ if (IS_ERR(h_dentry)) {
8223+ if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8224+ && !allow_neg)
8225+ h_dentry = NULL;
1facf9fc 8226+ goto out;
2000de60 8227+ }
1facf9fc 8228+
5527c038
JR
8229+ h_inode = d_inode(h_dentry);
8230+ if (d_is_negative(h_dentry)) {
1facf9fc 8231+ if (!allow_neg)
8232+ goto out_neg;
8233+ } else if (wh_found
8234+ || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8235+ goto out_neg;
8b6a4947
AM
8236+ else if (au_ftest_lkup(args->flags, DIRREN)
8237+ /* && h_inode */
8238+ && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8239+ AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8240+ (unsigned long long)h_inode->i_ino);
8241+ goto out_neg;
8242+ }
1facf9fc 8243+
5afbbe0d
AM
8244+ if (au_dbbot(dentry) <= bindex)
8245+ au_set_dbbot(dentry, bindex);
8246+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8247+ au_set_dbtop(dentry, bindex);
1facf9fc 8248+ au_set_h_dptr(dentry, bindex, h_dentry);
8249+
2000de60
JR
8250+ if (!d_is_dir(h_dentry)
8251+ || !wh_able
5527c038 8252+ || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
1facf9fc 8253+ goto out; /* success */
8254+
be118d29 8255+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
076b876e 8256+ opq = au_diropq_test(h_dentry);
3c1bdaff 8257+ inode_unlock_shared(h_inode);
1facf9fc 8258+ if (opq > 0)
8259+ au_set_dbdiropq(dentry, bindex);
8260+ else if (unlikely(opq < 0)) {
8261+ au_set_h_dptr(dentry, bindex, NULL);
8262+ h_dentry = ERR_PTR(opq);
8263+ }
8264+ goto out;
8265+
4f0767ce 8266+out_neg:
1facf9fc 8267+ dput(h_dentry);
8268+ h_dentry = NULL;
4f0767ce 8269+out:
1facf9fc 8270+ return h_dentry;
8271+}
8272+
dece6358
AM
8273+static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8274+{
8275+ if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8276+ && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8277+ return -EPERM;
8278+ return 0;
8279+}
8280+
1facf9fc 8281+/*
8282+ * returns the number of lower positive dentries,
8283+ * otherwise an error.
8284+ * can be called at unlinking with @type is zero.
8285+ */
5afbbe0d
AM
8286+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8287+ unsigned int flags)
1facf9fc 8288+{
8289+ int npositive, err;
8290+ aufs_bindex_t bindex, btail, bdiropq;
8b6a4947 8291+ unsigned char isdir, dirperm1, dirren;
1facf9fc 8292+ struct au_do_lookup_args args = {
8b6a4947
AM
8293+ .flags = flags,
8294+ .name = &dentry->d_name
1facf9fc 8295+ };
1facf9fc 8296+ struct dentry *parent;
076b876e 8297+ struct super_block *sb;
1facf9fc 8298+
076b876e 8299+ sb = dentry->d_sb;
8b6a4947 8300+ err = au_test_shwh(sb, args.name);
dece6358 8301+ if (unlikely(err))
1facf9fc 8302+ goto out;
8303+
8b6a4947 8304+ err = au_wh_name_alloc(&args.whname, args.name);
1facf9fc 8305+ if (unlikely(err))
8306+ goto out;
8307+
2000de60 8308+ isdir = !!d_is_dir(dentry);
076b876e 8309+ dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8b6a4947
AM
8310+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8311+ if (dirren)
8312+ au_fset_lkup(args.flags, DIRREN);
1facf9fc 8313+
8314+ npositive = 0;
4a4d8108 8315+ parent = dget_parent(dentry);
1facf9fc 8316+ btail = au_dbtaildir(parent);
5afbbe0d 8317+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 8318+ struct dentry *h_parent, *h_dentry;
8319+ struct inode *h_inode, *h_dir;
8b6a4947 8320+ struct au_branch *br;
1facf9fc 8321+
8322+ h_dentry = au_h_dptr(dentry, bindex);
8323+ if (h_dentry) {
5527c038 8324+ if (d_is_positive(h_dentry))
1facf9fc 8325+ npositive++;
5afbbe0d 8326+ break;
1facf9fc 8327+ }
8328+ h_parent = au_h_dptr(parent, bindex);
2000de60 8329+ if (!h_parent || !d_is_dir(h_parent))
1facf9fc 8330+ continue;
8331+
8b6a4947
AM
8332+ if (dirren) {
8333+ /* if the inum matches, then use the prepared name */
8334+ err = au_dr_lkup_name(&args, bindex);
8335+ if (unlikely(err))
8336+ goto out_parent;
8337+ }
8338+
5527c038 8339+ h_dir = d_inode(h_parent);
be118d29 8340+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8b6a4947 8341+ h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
3c1bdaff 8342+ inode_unlock_shared(h_dir);
1facf9fc 8343+ err = PTR_ERR(h_dentry);
8344+ if (IS_ERR(h_dentry))
4a4d8108 8345+ goto out_parent;
2000de60
JR
8346+ if (h_dentry)
8347+ au_fclr_lkup(args.flags, ALLOW_NEG);
076b876e
AM
8348+ if (dirperm1)
8349+ au_fset_lkup(args.flags, IGNORE_PERM);
1facf9fc 8350+
79b8bda9 8351+ if (au_dbwh(dentry) == bindex)
1facf9fc 8352+ break;
8353+ if (!h_dentry)
8354+ continue;
5527c038 8355+ if (d_is_negative(h_dentry))
1facf9fc 8356+ continue;
5527c038 8357+ h_inode = d_inode(h_dentry);
1facf9fc 8358+ npositive++;
8359+ if (!args.type)
8360+ args.type = h_inode->i_mode & S_IFMT;
8361+ if (args.type != S_IFDIR)
8362+ break;
8363+ else if (isdir) {
8364+ /* the type of lower may be different */
8365+ bdiropq = au_dbdiropq(dentry);
8366+ if (bdiropq >= 0 && bdiropq <= bindex)
8367+ break;
8368+ }
8b6a4947
AM
8369+ br = au_sbr(sb, bindex);
8370+ if (dirren
8371+ && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8372+ /*add_ent*/NULL)) {
8373+ /* prepare next name to lookup */
8374+ err = au_dr_lkup(&args, dentry, bindex);
8375+ if (unlikely(err))
8376+ goto out_parent;
8377+ }
1facf9fc 8378+ }
8379+
8380+ if (npositive) {
8381+ AuLabel(positive);
5afbbe0d 8382+ au_update_dbtop(dentry);
1facf9fc 8383+ }
8384+ err = npositive;
076b876e 8385+ if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
5afbbe0d 8386+ && au_dbtop(dentry) < 0)) {
1facf9fc 8387+ err = -EIO;
523b37e3
AM
8388+ AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8389+ dentry, err);
027c5e7a 8390+ }
1facf9fc 8391+
4f0767ce 8392+out_parent:
4a4d8108 8393+ dput(parent);
9f237c51 8394+ au_kfree_try_rcu(args.whname.name);
8b6a4947
AM
8395+ if (dirren)
8396+ au_dr_lkup_fin(&args);
4f0767ce 8397+out:
1facf9fc 8398+ return err;
8399+}
8400+
076b876e 8401+struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
1facf9fc 8402+{
8403+ struct dentry *dentry;
8404+ int wkq_err;
8405+
5527c038 8406+ if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
b4510431 8407+ dentry = vfsub_lkup_one(name, parent);
1facf9fc 8408+ else {
b4510431
AM
8409+ struct vfsub_lkup_one_args args = {
8410+ .errp = &dentry,
8411+ .name = name,
8412+ .parent = parent
1facf9fc 8413+ };
8414+
b4510431 8415+ wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
1facf9fc 8416+ if (unlikely(wkq_err))
8417+ dentry = ERR_PTR(wkq_err);
8418+ }
8419+
8420+ return dentry;
8421+}
8422+
8423+/*
8424+ * lookup @dentry on @bindex which should be negative.
8425+ */
86dc4139 8426+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
1facf9fc 8427+{
8428+ int err;
8429+ struct dentry *parent, *h_parent, *h_dentry;
86dc4139 8430+ struct au_branch *br;
1facf9fc 8431+
1facf9fc 8432+ parent = dget_parent(dentry);
8433+ h_parent = au_h_dptr(parent, bindex);
86dc4139
AM
8434+ br = au_sbr(dentry->d_sb, bindex);
8435+ if (wh)
8436+ h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
8437+ else
076b876e 8438+ h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
1facf9fc 8439+ err = PTR_ERR(h_dentry);
8440+ if (IS_ERR(h_dentry))
8441+ goto out;
5527c038 8442+ if (unlikely(d_is_positive(h_dentry))) {
1facf9fc 8443+ err = -EIO;
523b37e3 8444+ AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
1facf9fc 8445+ dput(h_dentry);
8446+ goto out;
8447+ }
8448+
4a4d8108 8449+ err = 0;
5afbbe0d
AM
8450+ if (bindex < au_dbtop(dentry))
8451+ au_set_dbtop(dentry, bindex);
8452+ if (au_dbbot(dentry) < bindex)
8453+ au_set_dbbot(dentry, bindex);
1facf9fc 8454+ au_set_h_dptr(dentry, bindex, h_dentry);
1facf9fc 8455+
4f0767ce 8456+out:
1facf9fc 8457+ dput(parent);
8458+ return err;
8459+}
8460+
8461+/* ---------------------------------------------------------------------- */
8462+
8463+/* subset of struct inode */
8464+struct au_iattr {
8465+ unsigned long i_ino;
8466+ /* unsigned int i_nlink; */
0c3ec466
AM
8467+ kuid_t i_uid;
8468+ kgid_t i_gid;
1facf9fc 8469+ u64 i_version;
8470+/*
8471+ loff_t i_size;
8472+ blkcnt_t i_blocks;
8473+*/
8474+ umode_t i_mode;
8475+};
8476+
8477+static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8478+{
8479+ ia->i_ino = h_inode->i_ino;
8480+ /* ia->i_nlink = h_inode->i_nlink; */
8481+ ia->i_uid = h_inode->i_uid;
8482+ ia->i_gid = h_inode->i_gid;
be118d29 8483+ ia->i_version = inode_query_iversion(h_inode);
1facf9fc 8484+/*
8485+ ia->i_size = h_inode->i_size;
8486+ ia->i_blocks = h_inode->i_blocks;
8487+*/
8488+ ia->i_mode = (h_inode->i_mode & S_IFMT);
8489+}
8490+
8491+static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8492+{
8493+ return ia->i_ino != h_inode->i_ino
8494+ /* || ia->i_nlink != h_inode->i_nlink */
0c3ec466 8495+ || !uid_eq(ia->i_uid, h_inode->i_uid)
2dfbb274 8496+ || !gid_eq(ia->i_gid, h_inode->i_gid)
be118d29 8497+ || !inode_eq_iversion(h_inode, ia->i_version)
1facf9fc 8498+/*
8499+ || ia->i_size != h_inode->i_size
8500+ || ia->i_blocks != h_inode->i_blocks
8501+*/
8502+ || ia->i_mode != (h_inode->i_mode & S_IFMT);
8503+}
8504+
8505+static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8506+ struct au_branch *br)
8507+{
8508+ int err;
8509+ struct au_iattr ia;
8510+ struct inode *h_inode;
8511+ struct dentry *h_d;
8512+ struct super_block *h_sb;
8513+
8514+ err = 0;
8515+ memset(&ia, -1, sizeof(ia));
8516+ h_sb = h_dentry->d_sb;
5527c038
JR
8517+ h_inode = NULL;
8518+ if (d_is_positive(h_dentry)) {
8519+ h_inode = d_inode(h_dentry);
1facf9fc 8520+ au_iattr_save(&ia, h_inode);
5527c038 8521+ } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
1facf9fc 8522+ /* nfs d_revalidate may return 0 for negative dentry */
8523+ /* fuse d_revalidate always return 0 for negative dentry */
8524+ goto out;
8525+
8526+ /* main purpose is namei.c:cached_lookup() and d_revalidate */
b4510431 8527+ h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
1facf9fc 8528+ err = PTR_ERR(h_d);
8529+ if (IS_ERR(h_d))
8530+ goto out;
8531+
8532+ err = 0;
8533+ if (unlikely(h_d != h_dentry
5527c038 8534+ || d_inode(h_d) != h_inode
1facf9fc 8535+ || (h_inode && au_iattr_test(&ia, h_inode))))
8536+ err = au_busy_or_stale();
8537+ dput(h_d);
8538+
4f0767ce 8539+out:
1facf9fc 8540+ AuTraceErr(err);
8541+ return err;
8542+}
8543+
8544+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8545+ struct dentry *h_parent, struct au_branch *br)
8546+{
8547+ int err;
8548+
8549+ err = 0;
027c5e7a
AM
8550+ if (udba == AuOpt_UDBA_REVAL
8551+ && !au_test_fs_remote(h_dentry->d_sb)) {
1facf9fc 8552+ IMustLock(h_dir);
5527c038 8553+ err = (d_inode(h_dentry->d_parent) != h_dir);
027c5e7a 8554+ } else if (udba != AuOpt_UDBA_NONE)
1facf9fc 8555+ err = au_h_verify_dentry(h_dentry, h_parent, br);
8556+
8557+ return err;
8558+}
8559+
8560+/* ---------------------------------------------------------------------- */
8561+
027c5e7a 8562+static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
1facf9fc 8563+{
027c5e7a 8564+ int err;
5afbbe0d 8565+ aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
027c5e7a
AM
8566+ struct au_hdentry tmp, *p, *q;
8567+ struct au_dinfo *dinfo;
8568+ struct super_block *sb;
1facf9fc 8569+
027c5e7a 8570+ DiMustWriteLock(dentry);
1308ab2a 8571+
027c5e7a
AM
8572+ sb = dentry->d_sb;
8573+ dinfo = au_di(dentry);
5afbbe0d 8574+ bbot = dinfo->di_bbot;
1facf9fc 8575+ bwh = dinfo->di_bwh;
8576+ bdiropq = dinfo->di_bdiropq;
5afbbe0d
AM
8577+ bindex = dinfo->di_btop;
8578+ p = au_hdentry(dinfo, bindex);
8579+ for (; bindex <= bbot; bindex++, p++) {
027c5e7a 8580+ if (!p->hd_dentry)
1facf9fc 8581+ continue;
8582+
027c5e7a
AM
8583+ new_bindex = au_br_index(sb, p->hd_id);
8584+ if (new_bindex == bindex)
1facf9fc 8585+ continue;
1facf9fc 8586+
1facf9fc 8587+ if (dinfo->di_bwh == bindex)
8588+ bwh = new_bindex;
8589+ if (dinfo->di_bdiropq == bindex)
8590+ bdiropq = new_bindex;
8591+ if (new_bindex < 0) {
8592+ au_hdput(p);
8593+ p->hd_dentry = NULL;
8594+ continue;
8595+ }
8596+
8597+ /* swap two lower dentries, and loop again */
5afbbe0d 8598+ q = au_hdentry(dinfo, new_bindex);
1facf9fc 8599+ tmp = *q;
8600+ *q = *p;
8601+ *p = tmp;
8602+ if (tmp.hd_dentry) {
8603+ bindex--;
8604+ p--;
8605+ }
8606+ }
8607+
1facf9fc 8608+ dinfo->di_bwh = -1;
5afbbe0d 8609+ if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
1facf9fc 8610+ dinfo->di_bwh = bwh;
8611+
8612+ dinfo->di_bdiropq = -1;
8613+ if (bdiropq >= 0
5afbbe0d 8614+ && bdiropq <= au_sbbot(sb)
1facf9fc 8615+ && au_sbr_whable(sb, bdiropq))
8616+ dinfo->di_bdiropq = bdiropq;
8617+
027c5e7a 8618+ err = -EIO;
5afbbe0d
AM
8619+ dinfo->di_btop = -1;
8620+ dinfo->di_bbot = -1;
8621+ bbot = au_dbbot(parent);
8622+ bindex = 0;
8623+ p = au_hdentry(dinfo, bindex);
8624+ for (; bindex <= bbot; bindex++, p++)
1facf9fc 8625+ if (p->hd_dentry) {
5afbbe0d 8626+ dinfo->di_btop = bindex;
1facf9fc 8627+ break;
8628+ }
8629+
5afbbe0d
AM
8630+ if (dinfo->di_btop >= 0) {
8631+ bindex = bbot;
8632+ p = au_hdentry(dinfo, bindex);
8633+ for (; bindex >= 0; bindex--, p--)
027c5e7a 8634+ if (p->hd_dentry) {
5afbbe0d 8635+ dinfo->di_bbot = bindex;
027c5e7a
AM
8636+ err = 0;
8637+ break;
8638+ }
8639+ }
8640+
8641+ return err;
1facf9fc 8642+}
8643+
027c5e7a 8644+static void au_do_hide(struct dentry *dentry)
1facf9fc 8645+{
027c5e7a 8646+ struct inode *inode;
1facf9fc 8647+
5527c038
JR
8648+ if (d_really_is_positive(dentry)) {
8649+ inode = d_inode(dentry);
8650+ if (!d_is_dir(dentry)) {
027c5e7a
AM
8651+ if (inode->i_nlink && !d_unhashed(dentry))
8652+ drop_nlink(inode);
8653+ } else {
8654+ clear_nlink(inode);
8655+ /* stop next lookup */
8656+ inode->i_flags |= S_DEAD;
8657+ }
8658+ smp_mb(); /* necessary? */
8659+ }
8660+ d_drop(dentry);
8661+}
1308ab2a 8662+
027c5e7a
AM
8663+static int au_hide_children(struct dentry *parent)
8664+{
8665+ int err, i, j, ndentry;
8666+ struct au_dcsub_pages dpages;
8667+ struct au_dpage *dpage;
8668+ struct dentry *dentry;
1facf9fc 8669+
027c5e7a 8670+ err = au_dpages_init(&dpages, GFP_NOFS);
1facf9fc 8671+ if (unlikely(err))
8672+ goto out;
027c5e7a
AM
8673+ err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8674+ if (unlikely(err))
8675+ goto out_dpages;
1facf9fc 8676+
027c5e7a
AM
8677+ /* in reverse order */
8678+ for (i = dpages.ndpage - 1; i >= 0; i--) {
8679+ dpage = dpages.dpages + i;
8680+ ndentry = dpage->ndentry;
8681+ for (j = ndentry - 1; j >= 0; j--) {
8682+ dentry = dpage->dentries[j];
8683+ if (dentry != parent)
8684+ au_do_hide(dentry);
8685+ }
8686+ }
1facf9fc 8687+
027c5e7a
AM
8688+out_dpages:
8689+ au_dpages_free(&dpages);
4f0767ce 8690+out:
027c5e7a 8691+ return err;
1facf9fc 8692+}
8693+
027c5e7a 8694+static void au_hide(struct dentry *dentry)
1facf9fc 8695+{
027c5e7a 8696+ int err;
1facf9fc 8697+
027c5e7a 8698+ AuDbgDentry(dentry);
2000de60 8699+ if (d_is_dir(dentry)) {
027c5e7a
AM
8700+ /* shrink_dcache_parent(dentry); */
8701+ err = au_hide_children(dentry);
8702+ if (unlikely(err))
523b37e3
AM
8703+ AuIOErr("%pd, failed hiding children, ignored %d\n",
8704+ dentry, err);
027c5e7a
AM
8705+ }
8706+ au_do_hide(dentry);
8707+}
1facf9fc 8708+
027c5e7a
AM
8709+/*
8710+ * By adding a dirty branch, a cached dentry may be affected in various ways.
8711+ *
8712+ * a dirty branch is added
8713+ * - on the top of layers
8714+ * - in the middle of layers
8715+ * - to the bottom of layers
8716+ *
8717+ * on the added branch there exists
8718+ * - a whiteout
8719+ * - a diropq
8720+ * - a same named entry
8721+ * + exist
8722+ * * negative --> positive
8723+ * * positive --> positive
8724+ * - type is unchanged
8725+ * - type is changed
8726+ * + doesn't exist
8727+ * * negative --> negative
8728+ * * positive --> negative (rejected by au_br_del() for non-dir case)
8729+ * - none
8730+ */
8731+static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8732+ struct au_dinfo *tmp)
8733+{
8734+ int err;
5afbbe0d 8735+ aufs_bindex_t bindex, bbot;
027c5e7a
AM
8736+ struct {
8737+ struct dentry *dentry;
8738+ struct inode *inode;
8739+ mode_t mode;
be52b249
AM
8740+ } orig_h, tmp_h = {
8741+ .dentry = NULL
8742+ };
027c5e7a
AM
8743+ struct au_hdentry *hd;
8744+ struct inode *inode, *h_inode;
8745+ struct dentry *h_dentry;
8746+
8747+ err = 0;
5afbbe0d 8748+ AuDebugOn(dinfo->di_btop < 0);
027c5e7a 8749+ orig_h.mode = 0;
5afbbe0d 8750+ orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
5527c038
JR
8751+ orig_h.inode = NULL;
8752+ if (d_is_positive(orig_h.dentry)) {
8753+ orig_h.inode = d_inode(orig_h.dentry);
027c5e7a 8754+ orig_h.mode = orig_h.inode->i_mode & S_IFMT;
5527c038 8755+ }
5afbbe0d
AM
8756+ if (tmp->di_btop >= 0) {
8757+ tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
5527c038
JR
8758+ if (d_is_positive(tmp_h.dentry)) {
8759+ tmp_h.inode = d_inode(tmp_h.dentry);
027c5e7a 8760+ tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
5527c038 8761+ }
027c5e7a
AM
8762+ }
8763+
5527c038
JR
8764+ inode = NULL;
8765+ if (d_really_is_positive(dentry))
8766+ inode = d_inode(dentry);
027c5e7a 8767+ if (!orig_h.inode) {
acd2b654 8768+ AuDbg("negative originally\n");
027c5e7a
AM
8769+ if (inode) {
8770+ au_hide(dentry);
8771+ goto out;
8772+ }
8773+ AuDebugOn(inode);
5afbbe0d 8774+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
027c5e7a
AM
8775+ AuDebugOn(dinfo->di_bdiropq != -1);
8776+
8777+ if (!tmp_h.inode) {
8778+ AuDbg("negative --> negative\n");
8779+ /* should have only one negative lower */
5afbbe0d
AM
8780+ if (tmp->di_btop >= 0
8781+ && tmp->di_btop < dinfo->di_btop) {
8782+ AuDebugOn(tmp->di_btop != tmp->di_bbot);
8783+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8784+ au_set_h_dptr(dentry, dinfo->di_btop, NULL);
027c5e7a 8785+ au_di_cp(dinfo, tmp);
5afbbe0d
AM
8786+ hd = au_hdentry(tmp, tmp->di_btop);
8787+ au_set_h_dptr(dentry, tmp->di_btop,
027c5e7a
AM
8788+ dget(hd->hd_dentry));
8789+ }
8790+ au_dbg_verify_dinode(dentry);
8791+ } else {
8792+ AuDbg("negative --> positive\n");
8793+ /*
8794+ * similar to the behaviour of creating with bypassing
8795+ * aufs.
8796+ * unhash it in order to force an error in the
8797+ * succeeding create operation.
8798+ * we should not set S_DEAD here.
8799+ */
8800+ d_drop(dentry);
8801+ /* au_di_swap(tmp, dinfo); */
8802+ au_dbg_verify_dinode(dentry);
8803+ }
8804+ } else {
8805+ AuDbg("positive originally\n");
8806+ /* inode may be NULL */
8807+ AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8808+ if (!tmp_h.inode) {
8809+ AuDbg("positive --> negative\n");
8810+ /* or bypassing aufs */
8811+ au_hide(dentry);
5afbbe0d 8812+ if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
027c5e7a
AM
8813+ dinfo->di_bwh = tmp->di_bwh;
8814+ if (inode)
8815+ err = au_refresh_hinode_self(inode);
8816+ au_dbg_verify_dinode(dentry);
8817+ } else if (orig_h.mode == tmp_h.mode) {
8818+ AuDbg("positive --> positive, same type\n");
8819+ if (!S_ISDIR(orig_h.mode)
5afbbe0d 8820+ && dinfo->di_btop > tmp->di_btop) {
027c5e7a
AM
8821+ /*
8822+ * similar to the behaviour of removing and
8823+ * creating.
8824+ */
8825+ au_hide(dentry);
8826+ if (inode)
8827+ err = au_refresh_hinode_self(inode);
8828+ au_dbg_verify_dinode(dentry);
8829+ } else {
8830+ /* fill empty slots */
5afbbe0d
AM
8831+ if (dinfo->di_btop > tmp->di_btop)
8832+ dinfo->di_btop = tmp->di_btop;
8833+ if (dinfo->di_bbot < tmp->di_bbot)
8834+ dinfo->di_bbot = tmp->di_bbot;
027c5e7a
AM
8835+ dinfo->di_bwh = tmp->di_bwh;
8836+ dinfo->di_bdiropq = tmp->di_bdiropq;
5afbbe0d
AM
8837+ bbot = dinfo->di_bbot;
8838+ bindex = tmp->di_btop;
8839+ hd = au_hdentry(tmp, bindex);
8840+ for (; bindex <= bbot; bindex++, hd++) {
027c5e7a
AM
8841+ if (au_h_dptr(dentry, bindex))
8842+ continue;
5afbbe0d 8843+ h_dentry = hd->hd_dentry;
027c5e7a
AM
8844+ if (!h_dentry)
8845+ continue;
5527c038
JR
8846+ AuDebugOn(d_is_negative(h_dentry));
8847+ h_inode = d_inode(h_dentry);
027c5e7a
AM
8848+ AuDebugOn(orig_h.mode
8849+ != (h_inode->i_mode
8850+ & S_IFMT));
8851+ au_set_h_dptr(dentry, bindex,
8852+ dget(h_dentry));
8853+ }
5afbbe0d
AM
8854+ if (inode)
8855+ err = au_refresh_hinode(inode, dentry);
027c5e7a
AM
8856+ au_dbg_verify_dinode(dentry);
8857+ }
8858+ } else {
8859+ AuDbg("positive --> positive, different type\n");
8860+ /* similar to the behaviour of removing and creating */
8861+ au_hide(dentry);
8862+ if (inode)
8863+ err = au_refresh_hinode_self(inode);
8864+ au_dbg_verify_dinode(dentry);
8865+ }
8866+ }
8867+
8868+out:
8869+ return err;
8870+}
8871+
79b8bda9
AM
8872+void au_refresh_dop(struct dentry *dentry, int force_reval)
8873+{
8874+ const struct dentry_operations *dop
8875+ = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8876+ static const unsigned int mask
8877+ = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8878+
8879+ BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8880+
8881+ if (dentry->d_op == dop)
8882+ return;
8883+
8884+ AuDbg("%pd\n", dentry);
8885+ spin_lock(&dentry->d_lock);
8886+ if (dop == &aufs_dop)
8887+ dentry->d_flags |= mask;
8888+ else
8889+ dentry->d_flags &= ~mask;
8890+ dentry->d_op = dop;
8891+ spin_unlock(&dentry->d_lock);
8892+}
8893+
027c5e7a
AM
8894+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8895+{
e2f27e51 8896+ int err, ebrange, nbr;
027c5e7a
AM
8897+ unsigned int sigen;
8898+ struct au_dinfo *dinfo, *tmp;
8899+ struct super_block *sb;
8900+ struct inode *inode;
8901+
8902+ DiMustWriteLock(dentry);
8903+ AuDebugOn(IS_ROOT(dentry));
5527c038 8904+ AuDebugOn(d_really_is_negative(parent));
027c5e7a
AM
8905+
8906+ sb = dentry->d_sb;
027c5e7a
AM
8907+ sigen = au_sigen(sb);
8908+ err = au_digen_test(parent, sigen);
8909+ if (unlikely(err))
8910+ goto out;
8911+
e2f27e51 8912+ nbr = au_sbbot(sb) + 1;
027c5e7a 8913+ dinfo = au_di(dentry);
e2f27e51 8914+ err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
027c5e7a
AM
8915+ if (unlikely(err))
8916+ goto out;
8917+ ebrange = au_dbrange_test(dentry);
8918+ if (!ebrange)
8919+ ebrange = au_do_refresh_hdentry(dentry, parent);
8920+
38d290e6 8921+ if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
5afbbe0d 8922+ AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
5527c038
JR
8923+ if (d_really_is_positive(dentry)) {
8924+ inode = d_inode(dentry);
027c5e7a 8925+ err = au_refresh_hinode_self(inode);
5527c038 8926+ }
027c5e7a
AM
8927+ au_dbg_verify_dinode(dentry);
8928+ if (!err)
8929+ goto out_dgen; /* success */
8930+ goto out;
8931+ }
8932+
8933+ /* temporary dinfo */
8934+ AuDbgDentry(dentry);
8935+ err = -ENOMEM;
8936+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8937+ if (unlikely(!tmp))
8938+ goto out;
8939+ au_di_swap(tmp, dinfo);
8940+ /* returns the number of positive dentries */
8941+ /*
8942+ * if current working dir is removed, it returns an error.
8943+ * but the dentry is legal.
8944+ */
5afbbe0d 8945+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
027c5e7a
AM
8946+ AuDbgDentry(dentry);
8947+ au_di_swap(tmp, dinfo);
8948+ if (err == -ENOENT)
8949+ err = 0;
8950+ if (err >= 0) {
8951+ /* compare/refresh by dinfo */
8952+ AuDbgDentry(dentry);
8953+ err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8954+ au_dbg_verify_dinode(dentry);
8955+ AuTraceErr(err);
8956+ }
e2f27e51 8957+ au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
027c5e7a
AM
8958+ au_rw_write_unlock(&tmp->di_rwsem);
8959+ au_di_free(tmp);
8960+ if (unlikely(err))
8961+ goto out;
8962+
8963+out_dgen:
8964+ au_update_digen(dentry);
8965+out:
8966+ if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
523b37e3 8967+ AuIOErr("failed refreshing %pd, %d\n", dentry, err);
027c5e7a
AM
8968+ AuDbgDentry(dentry);
8969+ }
8970+ AuTraceErr(err);
8971+ return err;
8972+}
8973+
b4510431
AM
8974+static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8975+ struct dentry *dentry, aufs_bindex_t bindex)
027c5e7a
AM
8976+{
8977+ int err, valid;
027c5e7a
AM
8978+
8979+ err = 0;
8980+ if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8981+ goto out;
027c5e7a
AM
8982+
8983+ AuDbg("b%d\n", bindex);
b4510431
AM
8984+ /*
8985+ * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8986+ * due to whiteout and branch permission.
8987+ */
8988+ flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8989+ | LOOKUP_FOLLOW | LOOKUP_EXCL);
8990+ /* it may return tri-state */
8991+ valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
1facf9fc 8992+
8993+ if (unlikely(valid < 0))
8994+ err = valid;
8995+ else if (!valid)
8996+ err = -EINVAL;
8997+
4f0767ce 8998+out:
1facf9fc 8999+ AuTraceErr(err);
9000+ return err;
9001+}
9002+
9003+/* todo: remove this */
9004+static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8b6a4947 9005+ unsigned int flags, int do_udba, int dirren)
1facf9fc 9006+{
9007+ int err;
9008+ umode_t mode, h_mode;
5afbbe0d 9009+ aufs_bindex_t bindex, btail, btop, ibs, ibe;
38d290e6 9010+ unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
4a4d8108 9011+ struct inode *h_inode, *h_cached_inode;
1facf9fc 9012+ struct dentry *h_dentry;
9013+ struct qstr *name, *h_name;
9014+
9015+ err = 0;
9016+ plus = 0;
9017+ mode = 0;
1facf9fc 9018+ ibs = -1;
9019+ ibe = -1;
9020+ unhashed = !!d_unhashed(dentry);
9021+ is_root = !!IS_ROOT(dentry);
9022+ name = &dentry->d_name;
38d290e6 9023+ tmpfile = au_di(dentry)->di_tmpfile;
1facf9fc 9024+
9025+ /*
7f207e10
AM
9026+ * Theoretically, REVAL test should be unnecessary in case of
9027+ * {FS,I}NOTIFY.
9028+ * But {fs,i}notify doesn't fire some necessary events,
1facf9fc 9029+ * IN_ATTRIB for atime/nlink/pageio
1facf9fc 9030+ * Let's do REVAL test too.
9031+ */
9032+ if (do_udba && inode) {
9033+ mode = (inode->i_mode & S_IFMT);
9034+ plus = (inode->i_nlink > 0);
5afbbe0d
AM
9035+ ibs = au_ibtop(inode);
9036+ ibe = au_ibbot(inode);
1facf9fc 9037+ }
9038+
5afbbe0d
AM
9039+ btop = au_dbtop(dentry);
9040+ btail = btop;
1facf9fc 9041+ if (inode && S_ISDIR(inode->i_mode))
9042+ btail = au_dbtaildir(dentry);
5afbbe0d 9043+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 9044+ h_dentry = au_h_dptr(dentry, bindex);
9045+ if (!h_dentry)
9046+ continue;
9047+
523b37e3
AM
9048+ AuDbg("b%d, %pd\n", bindex, h_dentry);
9049+ h_nfs = !!au_test_nfs(h_dentry->d_sb);
027c5e7a 9050+ spin_lock(&h_dentry->d_lock);
1facf9fc 9051+ h_name = &h_dentry->d_name;
9052+ if (unlikely(do_udba
9053+ && !is_root
523b37e3
AM
9054+ && ((!h_nfs
9055+ && (unhashed != !!d_unhashed(h_dentry)
8b6a4947 9056+ || (!tmpfile && !dirren
38d290e6
JR
9057+ && !au_qstreq(name, h_name))
9058+ ))
523b37e3
AM
9059+ || (h_nfs
9060+ && !(flags & LOOKUP_OPEN)
9061+ && (h_dentry->d_flags
9062+ & DCACHE_NFSFS_RENAMED)))
1facf9fc 9063+ )) {
38d290e6
JR
9064+ int h_unhashed;
9065+
9066+ h_unhashed = d_unhashed(h_dentry);
027c5e7a 9067+ spin_unlock(&h_dentry->d_lock);
38d290e6
JR
9068+ AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
9069+ unhashed, h_unhashed, dentry, h_dentry);
1facf9fc 9070+ goto err;
9071+ }
027c5e7a 9072+ spin_unlock(&h_dentry->d_lock);
1facf9fc 9073+
b4510431 9074+ err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
1facf9fc 9075+ if (unlikely(err))
9076+ /* do not goto err, to keep the errno */
9077+ break;
9078+
9079+ /* todo: plink too? */
9080+ if (!do_udba)
9081+ continue;
9082+
9083+ /* UDBA tests */
5527c038 9084+ if (unlikely(!!inode != d_is_positive(h_dentry)))
1facf9fc 9085+ goto err;
9086+
5527c038
JR
9087+ h_inode = NULL;
9088+ if (d_is_positive(h_dentry))
9089+ h_inode = d_inode(h_dentry);
1facf9fc 9090+ h_plus = plus;
9091+ h_mode = mode;
9092+ h_cached_inode = h_inode;
9093+ if (h_inode) {
9094+ h_mode = (h_inode->i_mode & S_IFMT);
9095+ h_plus = (h_inode->i_nlink > 0);
9096+ }
9097+ if (inode && ibs <= bindex && bindex <= ibe)
9098+ h_cached_inode = au_h_iptr(inode, bindex);
9099+
523b37e3 9100+ if (!h_nfs) {
38d290e6 9101+ if (unlikely(plus != h_plus && !tmpfile))
523b37e3
AM
9102+ goto err;
9103+ } else {
9104+ if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
9105+ && !is_root
9106+ && !IS_ROOT(h_dentry)
9107+ && unhashed != d_unhashed(h_dentry)))
9108+ goto err;
9109+ }
9110+ if (unlikely(mode != h_mode
1facf9fc 9111+ || h_cached_inode != h_inode))
9112+ goto err;
9113+ continue;
9114+
f6b6e03d 9115+err:
1facf9fc 9116+ err = -EINVAL;
9117+ break;
9118+ }
9119+
523b37e3 9120+ AuTraceErr(err);
1facf9fc 9121+ return err;
9122+}
9123+
027c5e7a 9124+/* todo: consolidate with do_refresh() and au_reval_for_attr() */
1facf9fc 9125+static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
9126+{
9127+ int err;
9128+ struct dentry *parent;
1facf9fc 9129+
027c5e7a 9130+ if (!au_digen_test(dentry, sigen))
1facf9fc 9131+ return 0;
9132+
9133+ parent = dget_parent(dentry);
9134+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 9135+ AuDebugOn(au_digen_test(parent, sigen));
1facf9fc 9136+ au_dbg_verify_gen(parent, sigen);
027c5e7a 9137+ err = au_refresh_dentry(dentry, parent);
1facf9fc 9138+ di_read_unlock(parent, AuLock_IR);
9139+ dput(parent);
027c5e7a 9140+ AuTraceErr(err);
1facf9fc 9141+ return err;
9142+}
9143+
9144+int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
9145+{
9146+ int err;
9147+ struct dentry *d, *parent;
1facf9fc 9148+
027c5e7a 9149+ if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
1facf9fc 9150+ return simple_reval_dpath(dentry, sigen);
9151+
9152+ /* slow loop, keep it simple and stupid */
9153+ /* cf: au_cpup_dirs() */
9154+ err = 0;
9155+ parent = NULL;
027c5e7a 9156+ while (au_digen_test(dentry, sigen)) {
1facf9fc 9157+ d = dentry;
9158+ while (1) {
9159+ dput(parent);
9160+ parent = dget_parent(d);
027c5e7a 9161+ if (!au_digen_test(parent, sigen))
1facf9fc 9162+ break;
9163+ d = parent;
9164+ }
9165+
1facf9fc 9166+ if (d != dentry)
027c5e7a 9167+ di_write_lock_child2(d);
1facf9fc 9168+
9169+ /* someone might update our dentry while we were sleeping */
027c5e7a
AM
9170+ if (au_digen_test(d, sigen)) {
9171+ /*
9172+ * todo: consolidate with simple_reval_dpath(),
9173+ * do_refresh() and au_reval_for_attr().
9174+ */
1facf9fc 9175+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 9176+ err = au_refresh_dentry(d, parent);
1facf9fc 9177+ di_read_unlock(parent, AuLock_IR);
9178+ }
9179+
9180+ if (d != dentry)
9181+ di_write_unlock(d);
9182+ dput(parent);
9183+ if (unlikely(err))
9184+ break;
9185+ }
9186+
9187+ return err;
9188+}
9189+
9190+/*
9191+ * if valid returns 1, otherwise 0.
9192+ */
b4510431 9193+static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
1facf9fc 9194+{
9195+ int valid, err;
9196+ unsigned int sigen;
8b6a4947 9197+ unsigned char do_udba, dirren;
1facf9fc 9198+ struct super_block *sb;
9199+ struct inode *inode;
9200+
027c5e7a 9201+ /* todo: support rcu-walk? */
b4510431 9202+ if (flags & LOOKUP_RCU)
027c5e7a
AM
9203+ return -ECHILD;
9204+
9205+ valid = 0;
9206+ if (unlikely(!au_di(dentry)))
9207+ goto out;
9208+
e49829fe 9209+ valid = 1;
1facf9fc 9210+ sb = dentry->d_sb;
e49829fe
JR
9211+ /*
9212+ * todo: very ugly
9213+ * i_mutex of parent dir may be held,
9214+ * but we should not return 'invalid' due to busy.
9215+ */
9216+ err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9217+ if (unlikely(err)) {
9218+ valid = err;
027c5e7a 9219+ AuTraceErr(err);
e49829fe
JR
9220+ goto out;
9221+ }
5527c038
JR
9222+ inode = NULL;
9223+ if (d_really_is_positive(dentry))
9224+ inode = d_inode(dentry);
5afbbe0d 9225+ if (unlikely(inode && au_is_bad_inode(inode))) {
c1595e42
JR
9226+ err = -EINVAL;
9227+ AuTraceErr(err);
9228+ goto out_dgrade;
9229+ }
027c5e7a
AM
9230+ if (unlikely(au_dbrange_test(dentry))) {
9231+ err = -EINVAL;
9232+ AuTraceErr(err);
9233+ goto out_dgrade;
1facf9fc 9234+ }
027c5e7a
AM
9235+
9236+ sigen = au_sigen(sb);
9237+ if (au_digen_test(dentry, sigen)) {
1facf9fc 9238+ AuDebugOn(IS_ROOT(dentry));
027c5e7a
AM
9239+ err = au_reval_dpath(dentry, sigen);
9240+ if (unlikely(err)) {
9241+ AuTraceErr(err);
1facf9fc 9242+ goto out_dgrade;
027c5e7a 9243+ }
1facf9fc 9244+ }
9245+ di_downgrade_lock(dentry, AuLock_IR);
9246+
1facf9fc 9247+ err = -EINVAL;
c1595e42 9248+ if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
523b37e3 9249+ && inode
38d290e6 9250+ && !(inode->i_state && I_LINKABLE)
79b8bda9
AM
9251+ && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9252+ AuTraceErr(err);
027c5e7a 9253+ goto out_inval;
79b8bda9 9254+ }
027c5e7a 9255+
1facf9fc 9256+ do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9257+ if (do_udba && inode) {
5afbbe0d 9258+ aufs_bindex_t btop = au_ibtop(inode);
027c5e7a 9259+ struct inode *h_inode;
1facf9fc 9260+
5afbbe0d
AM
9261+ if (btop >= 0) {
9262+ h_inode = au_h_iptr(inode, btop);
79b8bda9
AM
9263+ if (h_inode && au_test_higen(inode, h_inode)) {
9264+ AuTraceErr(err);
027c5e7a 9265+ goto out_inval;
79b8bda9 9266+ }
027c5e7a 9267+ }
1facf9fc 9268+ }
9269+
8b6a4947
AM
9270+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9271+ err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
5afbbe0d 9272+ if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
1facf9fc 9273+ err = -EIO;
523b37e3
AM
9274+ AuDbg("both of real entry and whiteout found, %p, err %d\n",
9275+ dentry, err);
027c5e7a 9276+ }
e49829fe 9277+ goto out_inval;
1facf9fc 9278+
4f0767ce 9279+out_dgrade:
1facf9fc 9280+ di_downgrade_lock(dentry, AuLock_IR);
e49829fe 9281+out_inval:
1facf9fc 9282+ aufs_read_unlock(dentry, AuLock_IR);
9283+ AuTraceErr(err);
9284+ valid = !err;
e49829fe 9285+out:
027c5e7a 9286+ if (!valid) {
523b37e3 9287+ AuDbg("%pd invalid, %d\n", dentry, valid);
027c5e7a
AM
9288+ d_drop(dentry);
9289+ }
1facf9fc 9290+ return valid;
9291+}
9292+
9293+static void aufs_d_release(struct dentry *dentry)
9294+{
027c5e7a 9295+ if (au_di(dentry)) {
4a4d8108
AM
9296+ au_di_fin(dentry);
9297+ au_hn_di_reinit(dentry);
1facf9fc 9298+ }
1facf9fc 9299+}
9300+
4a4d8108 9301+const struct dentry_operations aufs_dop = {
c06a8ce3
AM
9302+ .d_revalidate = aufs_d_revalidate,
9303+ .d_weak_revalidate = aufs_d_revalidate,
9304+ .d_release = aufs_d_release
1facf9fc 9305+};
79b8bda9
AM
9306+
9307+/* aufs_dop without d_revalidate */
9308+const struct dentry_operations aufs_dop_noreval = {
9309+ .d_release = aufs_d_release
9310+};
7f207e10
AM
9311diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9312--- /usr/share/empty/fs/aufs/dentry.h 1970-01-01 01:00:00.000000000 +0100
ba1aed25 9313+++ linux/fs/aufs/dentry.h 2019-03-05 12:13:00.135890907 +0100
9f237c51 9314@@ -0,0 +1,268 @@
062440b3 9315+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 9316+/*
ba1aed25 9317+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 9318+ *
9319+ * This program, aufs is free software; you can redistribute it and/or modify
9320+ * it under the terms of the GNU General Public License as published by
9321+ * the Free Software Foundation; either version 2 of the License, or
9322+ * (at your option) any later version.
dece6358
AM
9323+ *
9324+ * This program is distributed in the hope that it will be useful,
9325+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9326+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9327+ * GNU General Public License for more details.
9328+ *
9329+ * You should have received a copy of the GNU General Public License
523b37e3 9330+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9331+ */
9332+
9333+/*
9334+ * lookup and dentry operations
9335+ */
9336+
9337+#ifndef __AUFS_DENTRY_H__
9338+#define __AUFS_DENTRY_H__
9339+
9340+#ifdef __KERNEL__
9341+
dece6358 9342+#include <linux/dcache.h>
8b6a4947 9343+#include "dirren.h"
1facf9fc 9344+#include "rwsem.h"
9345+
1facf9fc 9346+struct au_hdentry {
9347+ struct dentry *hd_dentry;
027c5e7a 9348+ aufs_bindex_t hd_id;
1facf9fc 9349+};
9350+
9351+struct au_dinfo {
9352+ atomic_t di_generation;
9353+
dece6358 9354+ struct au_rwsem di_rwsem;
5afbbe0d 9355+ aufs_bindex_t di_btop, di_bbot, di_bwh, di_bdiropq;
38d290e6 9356+ unsigned char di_tmpfile; /* to allow the different name */
1c60b727 9357+ struct au_hdentry *di_hdentry;
9f237c51 9358+ struct rcu_head rcu;
4a4d8108 9359+} ____cacheline_aligned_in_smp;
1facf9fc 9360+
9361+/* ---------------------------------------------------------------------- */
9362+
5afbbe0d
AM
9363+/* flags for au_lkup_dentry() */
9364+#define AuLkup_ALLOW_NEG 1
9365+#define AuLkup_IGNORE_PERM (1 << 1)
8b6a4947 9366+#define AuLkup_DIRREN (1 << 2)
5afbbe0d
AM
9367+#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
9368+#define au_fset_lkup(flags, name) \
9369+ do { (flags) |= AuLkup_##name; } while (0)
9370+#define au_fclr_lkup(flags, name) \
9371+ do { (flags) &= ~AuLkup_##name; } while (0)
9372+
8b6a4947
AM
9373+#ifndef CONFIG_AUFS_DIRREN
9374+#undef AuLkup_DIRREN
9375+#define AuLkup_DIRREN 0
9376+#endif
9377+
9378+struct au_do_lookup_args {
9379+ unsigned int flags;
9380+ mode_t type;
9381+ struct qstr whname, *name;
9382+ struct au_dr_lookup dirren;
9383+};
9384+
5afbbe0d
AM
9385+/* ---------------------------------------------------------------------- */
9386+
1facf9fc 9387+/* dentry.c */
79b8bda9 9388+extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
1facf9fc 9389+struct au_branch;
076b876e 9390+struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
1facf9fc 9391+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9392+ struct dentry *h_parent, struct au_branch *br);
9393+
5afbbe0d
AM
9394+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9395+ unsigned int flags);
86dc4139 9396+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
027c5e7a 9397+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
1facf9fc 9398+int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
79b8bda9 9399+void au_refresh_dop(struct dentry *dentry, int force_reval);
1facf9fc 9400+
9401+/* dinfo.c */
4a4d8108 9402+void au_di_init_once(void *_di);
027c5e7a
AM
9403+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9404+void au_di_free(struct au_dinfo *dinfo);
9405+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9406+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
4a4d8108
AM
9407+int au_di_init(struct dentry *dentry);
9408+void au_di_fin(struct dentry *dentry);
e2f27e51 9409+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
1facf9fc 9410+
9411+void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9412+void di_read_unlock(struct dentry *d, int flags);
9413+void di_downgrade_lock(struct dentry *d, int flags);
9414+void di_write_lock(struct dentry *d, unsigned int lsc);
9415+void di_write_unlock(struct dentry *d);
9416+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9417+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9418+void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9419+
9420+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
2cbb1c4b 9421+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
1facf9fc 9422+aufs_bindex_t au_dbtail(struct dentry *dentry);
9423+aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9424+
9425+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9426+ struct dentry *h_dentry);
027c5e7a
AM
9427+int au_digen_test(struct dentry *dentry, unsigned int sigen);
9428+int au_dbrange_test(struct dentry *dentry);
1facf9fc 9429+void au_update_digen(struct dentry *dentry);
9430+void au_update_dbrange(struct dentry *dentry, int do_put_zero);
5afbbe0d
AM
9431+void au_update_dbtop(struct dentry *dentry);
9432+void au_update_dbbot(struct dentry *dentry);
1facf9fc 9433+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9434+
9435+/* ---------------------------------------------------------------------- */
9436+
9437+static inline struct au_dinfo *au_di(struct dentry *dentry)
9438+{
9439+ return dentry->d_fsdata;
9440+}
9441+
9442+/* ---------------------------------------------------------------------- */
9443+
9444+/* lock subclass for dinfo */
9445+enum {
9446+ AuLsc_DI_CHILD, /* child first */
4a4d8108 9447+ AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
1facf9fc 9448+ AuLsc_DI_CHILD3, /* copyup dirs */
9449+ AuLsc_DI_PARENT,
9450+ AuLsc_DI_PARENT2,
027c5e7a
AM
9451+ AuLsc_DI_PARENT3,
9452+ AuLsc_DI_TMP /* temp for replacing dinfo */
1facf9fc 9453+};
9454+
9455+/*
9456+ * di_read_lock_child, di_write_lock_child,
9457+ * di_read_lock_child2, di_write_lock_child2,
9458+ * di_read_lock_child3, di_write_lock_child3,
9459+ * di_read_lock_parent, di_write_lock_parent,
9460+ * di_read_lock_parent2, di_write_lock_parent2,
9461+ * di_read_lock_parent3, di_write_lock_parent3,
9462+ */
9463+#define AuReadLockFunc(name, lsc) \
9464+static inline void di_read_lock_##name(struct dentry *d, int flags) \
9465+{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9466+
9467+#define AuWriteLockFunc(name, lsc) \
9468+static inline void di_write_lock_##name(struct dentry *d) \
9469+{ di_write_lock(d, AuLsc_DI_##lsc); }
9470+
9471+#define AuRWLockFuncs(name, lsc) \
9472+ AuReadLockFunc(name, lsc) \
9473+ AuWriteLockFunc(name, lsc)
9474+
9475+AuRWLockFuncs(child, CHILD);
9476+AuRWLockFuncs(child2, CHILD2);
9477+AuRWLockFuncs(child3, CHILD3);
9478+AuRWLockFuncs(parent, PARENT);
9479+AuRWLockFuncs(parent2, PARENT2);
9480+AuRWLockFuncs(parent3, PARENT3);
9481+
9482+#undef AuReadLockFunc
9483+#undef AuWriteLockFunc
9484+#undef AuRWLockFuncs
9485+
9486+#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
dece6358
AM
9487+#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
9488+#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
1facf9fc 9489+
9490+/* ---------------------------------------------------------------------- */
9491+
9492+/* todo: memory barrier? */
9493+static inline unsigned int au_digen(struct dentry *d)
9494+{
9495+ return atomic_read(&au_di(d)->di_generation);
9496+}
9497+
9498+static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9499+{
9500+ hdentry->hd_dentry = NULL;
9501+}
9502+
5afbbe0d
AM
9503+static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9504+ aufs_bindex_t bindex)
9505+{
9506+ return di->di_hdentry + bindex;
9507+}
9508+
1facf9fc 9509+static inline void au_hdput(struct au_hdentry *hd)
9510+{
4a4d8108
AM
9511+ if (hd)
9512+ dput(hd->hd_dentry);
1facf9fc 9513+}
9514+
5afbbe0d 9515+static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
1facf9fc 9516+{
1308ab2a 9517+ DiMustAnyLock(dentry);
5afbbe0d 9518+ return au_di(dentry)->di_btop;
1facf9fc 9519+}
9520+
5afbbe0d 9521+static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
1facf9fc 9522+{
1308ab2a 9523+ DiMustAnyLock(dentry);
5afbbe0d 9524+ return au_di(dentry)->di_bbot;
1facf9fc 9525+}
9526+
9527+static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9528+{
1308ab2a 9529+ DiMustAnyLock(dentry);
1facf9fc 9530+ return au_di(dentry)->di_bwh;
9531+}
9532+
9533+static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9534+{
1308ab2a 9535+ DiMustAnyLock(dentry);
1facf9fc 9536+ return au_di(dentry)->di_bdiropq;
9537+}
9538+
9539+/* todo: hard/soft set? */
5afbbe0d 9540+static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9541+{
1308ab2a 9542+ DiMustWriteLock(dentry);
5afbbe0d 9543+ au_di(dentry)->di_btop = bindex;
1facf9fc 9544+}
9545+
5afbbe0d 9546+static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9547+{
1308ab2a 9548+ DiMustWriteLock(dentry);
5afbbe0d 9549+ au_di(dentry)->di_bbot = bindex;
1facf9fc 9550+}
9551+
9552+static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9553+{
1308ab2a 9554+ DiMustWriteLock(dentry);
5afbbe0d 9555+ /* dbwh can be outside of btop - bbot range */
1facf9fc 9556+ au_di(dentry)->di_bwh = bindex;
9557+}
9558+
9559+static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9560+{
1308ab2a 9561+ DiMustWriteLock(dentry);
1facf9fc 9562+ au_di(dentry)->di_bdiropq = bindex;
9563+}
9564+
9565+/* ---------------------------------------------------------------------- */
9566+
4a4d8108 9567+#ifdef CONFIG_AUFS_HNOTIFY
1facf9fc 9568+static inline void au_digen_dec(struct dentry *d)
9569+{
e49829fe 9570+ atomic_dec(&au_di(d)->di_generation);
1facf9fc 9571+}
9572+
4a4d8108 9573+static inline void au_hn_di_reinit(struct dentry *dentry)
1facf9fc 9574+{
9575+ dentry->d_fsdata = NULL;
9576+}
9577+#else
4a4d8108
AM
9578+AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9579+#endif /* CONFIG_AUFS_HNOTIFY */
1facf9fc 9580+
9581+#endif /* __KERNEL__ */
9582+#endif /* __AUFS_DENTRY_H__ */
7f207e10
AM
9583diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9584--- /usr/share/empty/fs/aufs/dinfo.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 9585+++ linux/fs/aufs/dinfo.c 2019-03-05 12:13:00.135890907 +0100
062440b3 9586@@ -0,0 +1,554 @@
cd7a4cd9 9587+// SPDX-License-Identifier: GPL-2.0
1facf9fc 9588+/*
ba1aed25 9589+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 9590+ *
9591+ * This program, aufs is free software; you can redistribute it and/or modify
9592+ * it under the terms of the GNU General Public License as published by
9593+ * the Free Software Foundation; either version 2 of the License, or
9594+ * (at your option) any later version.
dece6358
AM
9595+ *
9596+ * This program is distributed in the hope that it will be useful,
9597+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9598+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9599+ * GNU General Public License for more details.
9600+ *
9601+ * You should have received a copy of the GNU General Public License
523b37e3 9602+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9603+ */
9604+
9605+/*
9606+ * dentry private data
9607+ */
9608+
9609+#include "aufs.h"
9610+
e49829fe 9611+void au_di_init_once(void *_dinfo)
4a4d8108 9612+{
e49829fe 9613+ struct au_dinfo *dinfo = _dinfo;
4a4d8108 9614+
e49829fe 9615+ au_rw_init(&dinfo->di_rwsem);
4a4d8108
AM
9616+}
9617+
027c5e7a 9618+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
1facf9fc 9619+{
9620+ struct au_dinfo *dinfo;
027c5e7a 9621+ int nbr, i;
1facf9fc 9622+
9623+ dinfo = au_cache_alloc_dinfo();
9624+ if (unlikely(!dinfo))
9625+ goto out;
9626+
5afbbe0d 9627+ nbr = au_sbbot(sb) + 1;
1facf9fc 9628+ if (nbr <= 0)
9629+ nbr = 1;
9630+ dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
027c5e7a
AM
9631+ if (dinfo->di_hdentry) {
9632+ au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
5afbbe0d
AM
9633+ dinfo->di_btop = -1;
9634+ dinfo->di_bbot = -1;
027c5e7a
AM
9635+ dinfo->di_bwh = -1;
9636+ dinfo->di_bdiropq = -1;
38d290e6 9637+ dinfo->di_tmpfile = 0;
027c5e7a
AM
9638+ for (i = 0; i < nbr; i++)
9639+ dinfo->di_hdentry[i].hd_id = -1;
9640+ goto out;
9641+ }
1facf9fc 9642+
1c60b727 9643+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9644+ dinfo = NULL;
9645+
4f0767ce 9646+out:
027c5e7a 9647+ return dinfo;
1facf9fc 9648+}
9649+
027c5e7a 9650+void au_di_free(struct au_dinfo *dinfo)
4a4d8108 9651+{
4a4d8108 9652+ struct au_hdentry *p;
5afbbe0d 9653+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
9654+
9655+ /* dentry may not be revalidated */
5afbbe0d 9656+ bindex = dinfo->di_btop;
4a4d8108 9657+ if (bindex >= 0) {
5afbbe0d
AM
9658+ bbot = dinfo->di_bbot;
9659+ p = au_hdentry(dinfo, bindex);
9660+ while (bindex++ <= bbot)
4a4d8108
AM
9661+ au_hdput(p++);
9662+ }
9f237c51 9663+ au_kfree_try_rcu(dinfo->di_hdentry);
1c60b727 9664+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9665+}
9666+
9667+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9668+{
9669+ struct au_hdentry *p;
9670+ aufs_bindex_t bi;
9671+
9672+ AuRwMustWriteLock(&a->di_rwsem);
9673+ AuRwMustWriteLock(&b->di_rwsem);
9674+
9675+#define DiSwap(v, name) \
9676+ do { \
9677+ v = a->di_##name; \
9678+ a->di_##name = b->di_##name; \
9679+ b->di_##name = v; \
9680+ } while (0)
9681+
9682+ DiSwap(p, hdentry);
5afbbe0d
AM
9683+ DiSwap(bi, btop);
9684+ DiSwap(bi, bbot);
027c5e7a
AM
9685+ DiSwap(bi, bwh);
9686+ DiSwap(bi, bdiropq);
9687+ /* smp_mb(); */
9688+
9689+#undef DiSwap
9690+}
9691+
9692+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9693+{
9694+ AuRwMustWriteLock(&dst->di_rwsem);
9695+ AuRwMustWriteLock(&src->di_rwsem);
9696+
5afbbe0d
AM
9697+ dst->di_btop = src->di_btop;
9698+ dst->di_bbot = src->di_bbot;
027c5e7a
AM
9699+ dst->di_bwh = src->di_bwh;
9700+ dst->di_bdiropq = src->di_bdiropq;
9701+ /* smp_mb(); */
9702+}
9703+
9704+int au_di_init(struct dentry *dentry)
9705+{
9706+ int err;
9707+ struct super_block *sb;
9708+ struct au_dinfo *dinfo;
9709+
9710+ err = 0;
9711+ sb = dentry->d_sb;
9712+ dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9713+ if (dinfo) {
9714+ atomic_set(&dinfo->di_generation, au_sigen(sb));
9715+ /* smp_mb(); */ /* atomic_set */
9716+ dentry->d_fsdata = dinfo;
9717+ } else
9718+ err = -ENOMEM;
9719+
9720+ return err;
9721+}
9722+
9723+void au_di_fin(struct dentry *dentry)
9724+{
9725+ struct au_dinfo *dinfo;
9726+
9727+ dinfo = au_di(dentry);
9728+ AuRwDestroy(&dinfo->di_rwsem);
9729+ au_di_free(dinfo);
4a4d8108
AM
9730+}
9731+
e2f27e51 9732+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
1facf9fc 9733+{
9734+ int err, sz;
9735+ struct au_hdentry *hdp;
9736+
1308ab2a 9737+ AuRwMustWriteLock(&dinfo->di_rwsem);
9738+
1facf9fc 9739+ err = -ENOMEM;
5afbbe0d 9740+ sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
1facf9fc 9741+ if (!sz)
9742+ sz = sizeof(*hdp);
e2f27e51
AM
9743+ hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9744+ may_shrink);
1facf9fc 9745+ if (hdp) {
9746+ dinfo->di_hdentry = hdp;
9747+ err = 0;
9748+ }
9749+
9750+ return err;
9751+}
9752+
9753+/* ---------------------------------------------------------------------- */
9754+
9755+static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9756+{
9757+ switch (lsc) {
9758+ case AuLsc_DI_CHILD:
9759+ ii_write_lock_child(inode);
9760+ break;
9761+ case AuLsc_DI_CHILD2:
9762+ ii_write_lock_child2(inode);
9763+ break;
9764+ case AuLsc_DI_CHILD3:
9765+ ii_write_lock_child3(inode);
9766+ break;
9767+ case AuLsc_DI_PARENT:
9768+ ii_write_lock_parent(inode);
9769+ break;
9770+ case AuLsc_DI_PARENT2:
9771+ ii_write_lock_parent2(inode);
9772+ break;
9773+ case AuLsc_DI_PARENT3:
9774+ ii_write_lock_parent3(inode);
9775+ break;
9776+ default:
9777+ BUG();
9778+ }
9779+}
9780+
9781+static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9782+{
9783+ switch (lsc) {
9784+ case AuLsc_DI_CHILD:
9785+ ii_read_lock_child(inode);
9786+ break;
9787+ case AuLsc_DI_CHILD2:
9788+ ii_read_lock_child2(inode);
9789+ break;
9790+ case AuLsc_DI_CHILD3:
9791+ ii_read_lock_child3(inode);
9792+ break;
9793+ case AuLsc_DI_PARENT:
9794+ ii_read_lock_parent(inode);
9795+ break;
9796+ case AuLsc_DI_PARENT2:
9797+ ii_read_lock_parent2(inode);
9798+ break;
9799+ case AuLsc_DI_PARENT3:
9800+ ii_read_lock_parent3(inode);
9801+ break;
9802+ default:
9803+ BUG();
9804+ }
9805+}
9806+
9807+void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9808+{
5527c038
JR
9809+ struct inode *inode;
9810+
dece6358 9811+ au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9812+ if (d_really_is_positive(d)) {
9813+ inode = d_inode(d);
1facf9fc 9814+ if (au_ftest_lock(flags, IW))
5527c038 9815+ do_ii_write_lock(inode, lsc);
1facf9fc 9816+ else if (au_ftest_lock(flags, IR))
5527c038 9817+ do_ii_read_lock(inode, lsc);
1facf9fc 9818+ }
9819+}
9820+
9821+void di_read_unlock(struct dentry *d, int flags)
9822+{
5527c038
JR
9823+ struct inode *inode;
9824+
9825+ if (d_really_is_positive(d)) {
9826+ inode = d_inode(d);
027c5e7a
AM
9827+ if (au_ftest_lock(flags, IW)) {
9828+ au_dbg_verify_dinode(d);
5527c038 9829+ ii_write_unlock(inode);
027c5e7a
AM
9830+ } else if (au_ftest_lock(flags, IR)) {
9831+ au_dbg_verify_dinode(d);
5527c038 9832+ ii_read_unlock(inode);
027c5e7a 9833+ }
1facf9fc 9834+ }
dece6358 9835+ au_rw_read_unlock(&au_di(d)->di_rwsem);
1facf9fc 9836+}
9837+
9838+void di_downgrade_lock(struct dentry *d, int flags)
9839+{
5527c038
JR
9840+ if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9841+ ii_downgrade_lock(d_inode(d));
dece6358 9842+ au_rw_dgrade_lock(&au_di(d)->di_rwsem);
1facf9fc 9843+}
9844+
9845+void di_write_lock(struct dentry *d, unsigned int lsc)
9846+{
dece6358 9847+ au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9848+ if (d_really_is_positive(d))
9849+ do_ii_write_lock(d_inode(d), lsc);
1facf9fc 9850+}
9851+
9852+void di_write_unlock(struct dentry *d)
9853+{
027c5e7a 9854+ au_dbg_verify_dinode(d);
5527c038
JR
9855+ if (d_really_is_positive(d))
9856+ ii_write_unlock(d_inode(d));
dece6358 9857+ au_rw_write_unlock(&au_di(d)->di_rwsem);
1facf9fc 9858+}
9859+
9860+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9861+{
9862+ AuDebugOn(d1 == d2
5527c038 9863+ || d_inode(d1) == d_inode(d2)
1facf9fc 9864+ || d1->d_sb != d2->d_sb);
9865+
521ced18
JR
9866+ if ((isdir && au_test_subdir(d1, d2))
9867+ || d1 < d2) {
1facf9fc 9868+ di_write_lock_child(d1);
9869+ di_write_lock_child2(d2);
9870+ } else {
1facf9fc 9871+ di_write_lock_child(d2);
9872+ di_write_lock_child2(d1);
9873+ }
9874+}
9875+
9876+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9877+{
9878+ AuDebugOn(d1 == d2
5527c038 9879+ || d_inode(d1) == d_inode(d2)
1facf9fc 9880+ || d1->d_sb != d2->d_sb);
9881+
521ced18
JR
9882+ if ((isdir && au_test_subdir(d1, d2))
9883+ || d1 < d2) {
1facf9fc 9884+ di_write_lock_parent(d1);
9885+ di_write_lock_parent2(d2);
9886+ } else {
1facf9fc 9887+ di_write_lock_parent(d2);
9888+ di_write_lock_parent2(d1);
9889+ }
9890+}
9891+
9892+void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9893+{
9894+ di_write_unlock(d1);
5527c038 9895+ if (d_inode(d1) == d_inode(d2))
dece6358 9896+ au_rw_write_unlock(&au_di(d2)->di_rwsem);
1facf9fc 9897+ else
9898+ di_write_unlock(d2);
9899+}
9900+
9901+/* ---------------------------------------------------------------------- */
9902+
9903+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9904+{
9905+ struct dentry *d;
9906+
1308ab2a 9907+ DiMustAnyLock(dentry);
9908+
5afbbe0d 9909+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
1facf9fc 9910+ return NULL;
9911+ AuDebugOn(bindex < 0);
5afbbe0d 9912+ d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
c1595e42 9913+ AuDebugOn(d && au_dcount(d) <= 0);
1facf9fc 9914+ return d;
9915+}
9916+
2cbb1c4b
JR
9917+/*
9918+ * extended version of au_h_dptr().
38d290e6
JR
9919+ * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9920+ * error.
2cbb1c4b
JR
9921+ */
9922+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9923+{
9924+ struct dentry *h_dentry;
9925+ struct inode *inode, *h_inode;
9926+
5527c038 9927+ AuDebugOn(d_really_is_negative(dentry));
2cbb1c4b
JR
9928+
9929+ h_dentry = NULL;
5afbbe0d
AM
9930+ if (au_dbtop(dentry) <= bindex
9931+ && bindex <= au_dbbot(dentry))
2cbb1c4b 9932+ h_dentry = au_h_dptr(dentry, bindex);
38d290e6 9933+ if (h_dentry && !au_d_linkable(h_dentry)) {
2cbb1c4b
JR
9934+ dget(h_dentry);
9935+ goto out; /* success */
9936+ }
9937+
5527c038 9938+ inode = d_inode(dentry);
5afbbe0d
AM
9939+ AuDebugOn(bindex < au_ibtop(inode));
9940+ AuDebugOn(au_ibbot(inode) < bindex);
2cbb1c4b
JR
9941+ h_inode = au_h_iptr(inode, bindex);
9942+ h_dentry = d_find_alias(h_inode);
9943+ if (h_dentry) {
9944+ if (!IS_ERR(h_dentry)) {
38d290e6 9945+ if (!au_d_linkable(h_dentry))
2cbb1c4b
JR
9946+ goto out; /* success */
9947+ dput(h_dentry);
9948+ } else
9949+ goto out;
9950+ }
9951+
9952+ if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9953+ h_dentry = au_plink_lkup(inode, bindex);
9954+ AuDebugOn(!h_dentry);
9955+ if (!IS_ERR(h_dentry)) {
9956+ if (!au_d_hashed_positive(h_dentry))
9957+ goto out; /* success */
9958+ dput(h_dentry);
9959+ h_dentry = NULL;
9960+ }
9961+ }
9962+
9963+out:
9964+ AuDbgDentry(h_dentry);
9965+ return h_dentry;
9966+}
9967+
1facf9fc 9968+aufs_bindex_t au_dbtail(struct dentry *dentry)
9969+{
5afbbe0d 9970+ aufs_bindex_t bbot, bwh;
1facf9fc 9971+
5afbbe0d
AM
9972+ bbot = au_dbbot(dentry);
9973+ if (0 <= bbot) {
1facf9fc 9974+ bwh = au_dbwh(dentry);
9975+ if (!bwh)
9976+ return bwh;
5afbbe0d 9977+ if (0 < bwh && bwh < bbot)
1facf9fc 9978+ return bwh - 1;
9979+ }
5afbbe0d 9980+ return bbot;
1facf9fc 9981+}
9982+
9983+aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9984+{
5afbbe0d 9985+ aufs_bindex_t bbot, bopq;
1facf9fc 9986+
5afbbe0d
AM
9987+ bbot = au_dbtail(dentry);
9988+ if (0 <= bbot) {
1facf9fc 9989+ bopq = au_dbdiropq(dentry);
5afbbe0d
AM
9990+ if (0 <= bopq && bopq < bbot)
9991+ bbot = bopq;
1facf9fc 9992+ }
5afbbe0d 9993+ return bbot;
1facf9fc 9994+}
9995+
9996+/* ---------------------------------------------------------------------- */
9997+
9998+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9999+ struct dentry *h_dentry)
10000+{
5afbbe0d
AM
10001+ struct au_dinfo *dinfo;
10002+ struct au_hdentry *hd;
027c5e7a 10003+ struct au_branch *br;
1facf9fc 10004+
1308ab2a 10005+ DiMustWriteLock(dentry);
10006+
5afbbe0d
AM
10007+ dinfo = au_di(dentry);
10008+ hd = au_hdentry(dinfo, bindex);
4a4d8108 10009+ au_hdput(hd);
1facf9fc 10010+ hd->hd_dentry = h_dentry;
027c5e7a
AM
10011+ if (h_dentry) {
10012+ br = au_sbr(dentry->d_sb, bindex);
10013+ hd->hd_id = br->br_id;
10014+ }
10015+}
10016+
10017+int au_dbrange_test(struct dentry *dentry)
10018+{
10019+ int err;
5afbbe0d 10020+ aufs_bindex_t btop, bbot;
027c5e7a
AM
10021+
10022+ err = 0;
5afbbe0d
AM
10023+ btop = au_dbtop(dentry);
10024+ bbot = au_dbbot(dentry);
10025+ if (btop >= 0)
10026+ AuDebugOn(bbot < 0 && btop > bbot);
027c5e7a
AM
10027+ else {
10028+ err = -EIO;
5afbbe0d 10029+ AuDebugOn(bbot >= 0);
027c5e7a
AM
10030+ }
10031+
10032+ return err;
10033+}
10034+
10035+int au_digen_test(struct dentry *dentry, unsigned int sigen)
10036+{
10037+ int err;
10038+
10039+ err = 0;
10040+ if (unlikely(au_digen(dentry) != sigen
5527c038 10041+ || au_iigen_test(d_inode(dentry), sigen)))
027c5e7a
AM
10042+ err = -EIO;
10043+
10044+ return err;
1facf9fc 10045+}
10046+
10047+void au_update_digen(struct dentry *dentry)
10048+{
10049+ atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
10050+ /* smp_mb(); */ /* atomic_set */
10051+}
10052+
10053+void au_update_dbrange(struct dentry *dentry, int do_put_zero)
10054+{
10055+ struct au_dinfo *dinfo;
10056+ struct dentry *h_d;
4a4d8108 10057+ struct au_hdentry *hdp;
5afbbe0d 10058+ aufs_bindex_t bindex, bbot;
1facf9fc 10059+
1308ab2a 10060+ DiMustWriteLock(dentry);
10061+
1facf9fc 10062+ dinfo = au_di(dentry);
5afbbe0d 10063+ if (!dinfo || dinfo->di_btop < 0)
1facf9fc 10064+ return;
10065+
10066+ if (do_put_zero) {
5afbbe0d
AM
10067+ bbot = dinfo->di_bbot;
10068+ bindex = dinfo->di_btop;
10069+ hdp = au_hdentry(dinfo, bindex);
10070+ for (; bindex <= bbot; bindex++, hdp++) {
10071+ h_d = hdp->hd_dentry;
5527c038 10072+ if (h_d && d_is_negative(h_d))
1facf9fc 10073+ au_set_h_dptr(dentry, bindex, NULL);
10074+ }
10075+ }
10076+
5afbbe0d
AM
10077+ dinfo->di_btop = 0;
10078+ hdp = au_hdentry(dinfo, dinfo->di_btop);
10079+ for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
10080+ if (hdp->hd_dentry)
1facf9fc 10081+ break;
5afbbe0d
AM
10082+ if (dinfo->di_btop > dinfo->di_bbot) {
10083+ dinfo->di_btop = -1;
10084+ dinfo->di_bbot = -1;
1facf9fc 10085+ return;
10086+ }
10087+
5afbbe0d
AM
10088+ hdp = au_hdentry(dinfo, dinfo->di_bbot);
10089+ for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
10090+ if (hdp->hd_dentry)
1facf9fc 10091+ break;
5afbbe0d 10092+ AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
1facf9fc 10093+}
10094+
5afbbe0d 10095+void au_update_dbtop(struct dentry *dentry)
1facf9fc 10096+{
5afbbe0d 10097+ aufs_bindex_t bindex, bbot;
1facf9fc 10098+ struct dentry *h_dentry;
10099+
5afbbe0d
AM
10100+ bbot = au_dbbot(dentry);
10101+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
1facf9fc 10102+ h_dentry = au_h_dptr(dentry, bindex);
10103+ if (!h_dentry)
10104+ continue;
5527c038 10105+ if (d_is_positive(h_dentry)) {
5afbbe0d 10106+ au_set_dbtop(dentry, bindex);
1facf9fc 10107+ return;
10108+ }
10109+ au_set_h_dptr(dentry, bindex, NULL);
10110+ }
10111+}
10112+
5afbbe0d 10113+void au_update_dbbot(struct dentry *dentry)
1facf9fc 10114+{
5afbbe0d 10115+ aufs_bindex_t bindex, btop;
1facf9fc 10116+ struct dentry *h_dentry;
10117+
5afbbe0d
AM
10118+ btop = au_dbtop(dentry);
10119+ for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
1facf9fc 10120+ h_dentry = au_h_dptr(dentry, bindex);
10121+ if (!h_dentry)
10122+ continue;
5527c038 10123+ if (d_is_positive(h_dentry)) {
5afbbe0d 10124+ au_set_dbbot(dentry, bindex);
1facf9fc 10125+ return;
10126+ }
10127+ au_set_h_dptr(dentry, bindex, NULL);
10128+ }
10129+}
10130+
10131+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
10132+{
5afbbe0d 10133+ aufs_bindex_t bindex, bbot;
1facf9fc 10134+
5afbbe0d
AM
10135+ bbot = au_dbbot(dentry);
10136+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
1facf9fc 10137+ if (au_h_dptr(dentry, bindex) == h_dentry)
10138+ return bindex;
10139+ return -1;
10140+}
7f207e10
AM
10141diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
10142--- /usr/share/empty/fs/aufs/dir.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 10143+++ linux/fs/aufs/dir.c 2019-03-05 12:13:00.135890907 +0100
acd2b654 10144@@ -0,0 +1,762 @@
cd7a4cd9 10145+// SPDX-License-Identifier: GPL-2.0
1facf9fc 10146+/*
ba1aed25 10147+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 10148+ *
10149+ * This program, aufs is free software; you can redistribute it and/or modify
10150+ * it under the terms of the GNU General Public License as published by
10151+ * the Free Software Foundation; either version 2 of the License, or
10152+ * (at your option) any later version.
dece6358
AM
10153+ *
10154+ * This program is distributed in the hope that it will be useful,
10155+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10156+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10157+ * GNU General Public License for more details.
10158+ *
10159+ * You should have received a copy of the GNU General Public License
523b37e3 10160+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10161+ */
10162+
10163+/*
10164+ * directory operations
10165+ */
10166+
10167+#include <linux/fs_stack.h>
10168+#include "aufs.h"
10169+
10170+void au_add_nlink(struct inode *dir, struct inode *h_dir)
10171+{
9dbd164d
AM
10172+ unsigned int nlink;
10173+
1facf9fc 10174+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10175+
9dbd164d
AM
10176+ nlink = dir->i_nlink;
10177+ nlink += h_dir->i_nlink - 2;
1facf9fc 10178+ if (h_dir->i_nlink < 2)
9dbd164d 10179+ nlink += 2;
f6b6e03d 10180+ smp_mb(); /* for i_nlink */
7eafdf33 10181+ /* 0 can happen in revaliding */
92d182d2 10182+ set_nlink(dir, nlink);
1facf9fc 10183+}
10184+
10185+void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10186+{
9dbd164d
AM
10187+ unsigned int nlink;
10188+
1facf9fc 10189+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10190+
9dbd164d
AM
10191+ nlink = dir->i_nlink;
10192+ nlink -= h_dir->i_nlink - 2;
1facf9fc 10193+ if (h_dir->i_nlink < 2)
9dbd164d 10194+ nlink -= 2;
f6b6e03d 10195+ smp_mb(); /* for i_nlink */
92d182d2 10196+ /* nlink == 0 means the branch-fs is broken */
9dbd164d 10197+ set_nlink(dir, nlink);
1facf9fc 10198+}
10199+
1308ab2a 10200+loff_t au_dir_size(struct file *file, struct dentry *dentry)
10201+{
10202+ loff_t sz;
5afbbe0d 10203+ aufs_bindex_t bindex, bbot;
1308ab2a 10204+ struct file *h_file;
10205+ struct dentry *h_dentry;
10206+
10207+ sz = 0;
10208+ if (file) {
2000de60 10209+ AuDebugOn(!d_is_dir(file->f_path.dentry));
1308ab2a 10210+
5afbbe0d
AM
10211+ bbot = au_fbbot_dir(file);
10212+ for (bindex = au_fbtop(file);
10213+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 10214+ bindex++) {
4a4d8108 10215+ h_file = au_hf_dir(file, bindex);
c06a8ce3
AM
10216+ if (h_file && file_inode(h_file))
10217+ sz += vfsub_f_size_read(h_file);
1308ab2a 10218+ }
10219+ } else {
10220+ AuDebugOn(!dentry);
2000de60 10221+ AuDebugOn(!d_is_dir(dentry));
1308ab2a 10222+
5afbbe0d
AM
10223+ bbot = au_dbtaildir(dentry);
10224+ for (bindex = au_dbtop(dentry);
10225+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 10226+ bindex++) {
10227+ h_dentry = au_h_dptr(dentry, bindex);
5527c038
JR
10228+ if (h_dentry && d_is_positive(h_dentry))
10229+ sz += i_size_read(d_inode(h_dentry));
1308ab2a 10230+ }
10231+ }
10232+ if (sz < KMALLOC_MAX_SIZE)
10233+ sz = roundup_pow_of_two(sz);
10234+ if (sz > KMALLOC_MAX_SIZE)
10235+ sz = KMALLOC_MAX_SIZE;
10236+ else if (sz < NAME_MAX) {
10237+ BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10238+ sz = AUFS_RDBLK_DEF;
10239+ }
10240+ return sz;
10241+}
10242+
b912730e
AM
10243+struct au_dir_ts_arg {
10244+ struct dentry *dentry;
10245+ aufs_bindex_t brid;
10246+};
10247+
10248+static void au_do_dir_ts(void *arg)
10249+{
10250+ struct au_dir_ts_arg *a = arg;
10251+ struct au_dtime dt;
10252+ struct path h_path;
10253+ struct inode *dir, *h_dir;
10254+ struct super_block *sb;
10255+ struct au_branch *br;
10256+ struct au_hinode *hdir;
10257+ int err;
5afbbe0d 10258+ aufs_bindex_t btop, bindex;
b912730e
AM
10259+
10260+ sb = a->dentry->d_sb;
5527c038 10261+ if (d_really_is_negative(a->dentry))
b912730e 10262+ goto out;
5527c038 10263+ /* no dir->i_mutex lock */
b95c5147
AM
10264+ aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10265+
5527c038 10266+ dir = d_inode(a->dentry);
5afbbe0d 10267+ btop = au_ibtop(dir);
b912730e 10268+ bindex = au_br_index(sb, a->brid);
5afbbe0d 10269+ if (bindex < btop)
b912730e
AM
10270+ goto out_unlock;
10271+
10272+ br = au_sbr(sb, bindex);
10273+ h_path.dentry = au_h_dptr(a->dentry, bindex);
10274+ if (!h_path.dentry)
10275+ goto out_unlock;
10276+ h_path.mnt = au_br_mnt(br);
10277+ au_dtime_store(&dt, a->dentry, &h_path);
10278+
5afbbe0d 10279+ br = au_sbr(sb, btop);
b912730e
AM
10280+ if (!au_br_writable(br->br_perm))
10281+ goto out_unlock;
5afbbe0d 10282+ h_path.dentry = au_h_dptr(a->dentry, btop);
b912730e
AM
10283+ h_path.mnt = au_br_mnt(br);
10284+ err = vfsub_mnt_want_write(h_path.mnt);
10285+ if (err)
10286+ goto out_unlock;
5afbbe0d
AM
10287+ hdir = au_hi(dir, btop);
10288+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10289+ h_dir = au_h_iptr(dir, btop);
b912730e 10290+ if (h_dir->i_nlink
cd7a4cd9 10291+ && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
b912730e
AM
10292+ dt.dt_h_path = h_path;
10293+ au_dtime_revert(&dt);
10294+ }
5afbbe0d 10295+ au_hn_inode_unlock(hdir);
b912730e
AM
10296+ vfsub_mnt_drop_write(h_path.mnt);
10297+ au_cpup_attr_timesizes(dir);
10298+
10299+out_unlock:
10300+ aufs_read_unlock(a->dentry, AuLock_DW);
10301+out:
10302+ dput(a->dentry);
10303+ au_nwt_done(&au_sbi(sb)->si_nowait);
9f237c51 10304+ au_kfree_try_rcu(arg);
b912730e
AM
10305+}
10306+
10307+void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10308+{
10309+ int perm, wkq_err;
5afbbe0d 10310+ aufs_bindex_t btop;
b912730e
AM
10311+ struct au_dir_ts_arg *arg;
10312+ struct dentry *dentry;
10313+ struct super_block *sb;
10314+
10315+ IMustLock(dir);
10316+
10317+ dentry = d_find_any_alias(dir);
10318+ AuDebugOn(!dentry);
10319+ sb = dentry->d_sb;
5afbbe0d
AM
10320+ btop = au_ibtop(dir);
10321+ if (btop == bindex) {
b912730e
AM
10322+ au_cpup_attr_timesizes(dir);
10323+ goto out;
10324+ }
10325+
5afbbe0d 10326+ perm = au_sbr_perm(sb, btop);
b912730e
AM
10327+ if (!au_br_writable(perm))
10328+ goto out;
10329+
10330+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
10331+ if (!arg)
10332+ goto out;
10333+
10334+ arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10335+ arg->brid = au_sbr_id(sb, bindex);
10336+ wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10337+ if (unlikely(wkq_err)) {
10338+ pr_err("wkq %d\n", wkq_err);
10339+ dput(dentry);
9f237c51 10340+ au_kfree_try_rcu(arg);
b912730e
AM
10341+ }
10342+
10343+out:
10344+ dput(dentry);
10345+}
10346+
1facf9fc 10347+/* ---------------------------------------------------------------------- */
10348+
10349+static int reopen_dir(struct file *file)
10350+{
10351+ int err;
10352+ unsigned int flags;
5afbbe0d 10353+ aufs_bindex_t bindex, btail, btop;
1facf9fc 10354+ struct dentry *dentry, *h_dentry;
10355+ struct file *h_file;
10356+
10357+ /* open all lower dirs */
2000de60 10358+ dentry = file->f_path.dentry;
5afbbe0d
AM
10359+ btop = au_dbtop(dentry);
10360+ for (bindex = au_fbtop(file); bindex < btop; bindex++)
1facf9fc 10361+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 10362+ au_set_fbtop(file, btop);
1facf9fc 10363+
10364+ btail = au_dbtaildir(dentry);
5afbbe0d 10365+ for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
1facf9fc 10366+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 10367+ au_set_fbbot_dir(file, btail);
1facf9fc 10368+
4a4d8108 10369+ flags = vfsub_file_flags(file);
5afbbe0d 10370+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 10371+ h_dentry = au_h_dptr(dentry, bindex);
10372+ if (!h_dentry)
10373+ continue;
4a4d8108 10374+ h_file = au_hf_dir(file, bindex);
1facf9fc 10375+ if (h_file)
10376+ continue;
10377+
392086de 10378+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 10379+ err = PTR_ERR(h_file);
10380+ if (IS_ERR(h_file))
10381+ goto out; /* close all? */
10382+ au_set_h_fptr(file, bindex, h_file);
10383+ }
10384+ au_update_figen(file);
10385+ /* todo: necessary? */
10386+ /* file->f_ra = h_file->f_ra; */
10387+ err = 0;
10388+
4f0767ce 10389+out:
1facf9fc 10390+ return err;
10391+}
10392+
b912730e 10393+static int do_open_dir(struct file *file, int flags, struct file *h_file)
1facf9fc 10394+{
10395+ int err;
10396+ aufs_bindex_t bindex, btail;
10397+ struct dentry *dentry, *h_dentry;
8cdd5066 10398+ struct vfsmount *mnt;
1facf9fc 10399+
1308ab2a 10400+ FiMustWriteLock(file);
b912730e 10401+ AuDebugOn(h_file);
1308ab2a 10402+
523b37e3 10403+ err = 0;
8cdd5066 10404+ mnt = file->f_path.mnt;
2000de60 10405+ dentry = file->f_path.dentry;
be118d29 10406+ file->f_version = inode_query_iversion(d_inode(dentry));
5afbbe0d
AM
10407+ bindex = au_dbtop(dentry);
10408+ au_set_fbtop(file, bindex);
1facf9fc 10409+ btail = au_dbtaildir(dentry);
5afbbe0d 10410+ au_set_fbbot_dir(file, btail);
1facf9fc 10411+ for (; !err && bindex <= btail; bindex++) {
10412+ h_dentry = au_h_dptr(dentry, bindex);
10413+ if (!h_dentry)
10414+ continue;
10415+
8cdd5066
JR
10416+ err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10417+ if (unlikely(err))
10418+ break;
392086de 10419+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 10420+ if (IS_ERR(h_file)) {
10421+ err = PTR_ERR(h_file);
10422+ break;
10423+ }
10424+ au_set_h_fptr(file, bindex, h_file);
10425+ }
10426+ au_update_figen(file);
10427+ /* todo: necessary? */
10428+ /* file->f_ra = h_file->f_ra; */
10429+ if (!err)
10430+ return 0; /* success */
10431+
10432+ /* close all */
5afbbe0d 10433+ for (bindex = au_fbtop(file); bindex <= btail; bindex++)
1facf9fc 10434+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d
AM
10435+ au_set_fbtop(file, -1);
10436+ au_set_fbbot_dir(file, -1);
4a4d8108 10437+
1facf9fc 10438+ return err;
10439+}
10440+
10441+static int aufs_open_dir(struct inode *inode __maybe_unused,
10442+ struct file *file)
10443+{
4a4d8108
AM
10444+ int err;
10445+ struct super_block *sb;
10446+ struct au_fidir *fidir;
10447+
10448+ err = -ENOMEM;
2000de60 10449+ sb = file->f_path.dentry->d_sb;
4a4d8108 10450+ si_read_lock(sb, AuLock_FLUSH);
e49829fe 10451+ fidir = au_fidir_alloc(sb);
4a4d8108 10452+ if (fidir) {
b912730e
AM
10453+ struct au_do_open_args args = {
10454+ .open = do_open_dir,
10455+ .fidir = fidir
10456+ };
10457+ err = au_do_open(file, &args);
4a4d8108 10458+ if (unlikely(err))
9f237c51 10459+ au_kfree_rcu(fidir);
4a4d8108
AM
10460+ }
10461+ si_read_unlock(sb);
10462+ return err;
1facf9fc 10463+}
10464+
10465+static int aufs_release_dir(struct inode *inode __maybe_unused,
10466+ struct file *file)
10467+{
10468+ struct au_vdir *vdir_cache;
4a4d8108
AM
10469+ struct au_finfo *finfo;
10470+ struct au_fidir *fidir;
f0c0a007 10471+ struct au_hfile *hf;
5afbbe0d 10472+ aufs_bindex_t bindex, bbot;
1facf9fc 10473+
4a4d8108
AM
10474+ finfo = au_fi(file);
10475+ fidir = finfo->fi_hdir;
10476+ if (fidir) {
8b6a4947
AM
10477+ au_hbl_del(&finfo->fi_hlist,
10478+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108
AM
10479+ vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10480+ if (vdir_cache)
1c60b727 10481+ au_vdir_free(vdir_cache);
4a4d8108
AM
10482+
10483+ bindex = finfo->fi_btop;
10484+ if (bindex >= 0) {
f0c0a007 10485+ hf = fidir->fd_hfile + bindex;
4a4d8108
AM
10486+ /*
10487+ * calls fput() instead of filp_close(),
10488+ * since no dnotify or lock for the lower file.
10489+ */
5afbbe0d 10490+ bbot = fidir->fd_bbot;
f0c0a007
AM
10491+ for (; bindex <= bbot; bindex++, hf++)
10492+ if (hf->hf_file)
1c60b727 10493+ au_hfput(hf, /*execed*/0);
4a4d8108 10494+ }
9f237c51 10495+ au_kfree_rcu(fidir);
4a4d8108 10496+ finfo->fi_hdir = NULL;
1facf9fc 10497+ }
1c60b727 10498+ au_finfo_fin(file);
1facf9fc 10499+ return 0;
10500+}
10501+
10502+/* ---------------------------------------------------------------------- */
10503+
4a4d8108
AM
10504+static int au_do_flush_dir(struct file *file, fl_owner_t id)
10505+{
10506+ int err;
5afbbe0d 10507+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
10508+ struct file *h_file;
10509+
10510+ err = 0;
5afbbe0d
AM
10511+ bbot = au_fbbot_dir(file);
10512+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
10513+ h_file = au_hf_dir(file, bindex);
10514+ if (h_file)
10515+ err = vfsub_flush(h_file, id);
10516+ }
10517+ return err;
10518+}
10519+
10520+static int aufs_flush_dir(struct file *file, fl_owner_t id)
10521+{
10522+ return au_do_flush(file, id, au_do_flush_dir);
10523+}
10524+
10525+/* ---------------------------------------------------------------------- */
10526+
1facf9fc 10527+static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10528+{
10529+ int err;
5afbbe0d 10530+ aufs_bindex_t bbot, bindex;
1facf9fc 10531+ struct inode *inode;
10532+ struct super_block *sb;
10533+
10534+ err = 0;
10535+ sb = dentry->d_sb;
5527c038 10536+ inode = d_inode(dentry);
1facf9fc 10537+ IMustLock(inode);
5afbbe0d
AM
10538+ bbot = au_dbbot(dentry);
10539+ for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
1facf9fc 10540+ struct path h_path;
1facf9fc 10541+
10542+ if (au_test_ro(sb, bindex, inode))
10543+ continue;
10544+ h_path.dentry = au_h_dptr(dentry, bindex);
10545+ if (!h_path.dentry)
10546+ continue;
1facf9fc 10547+
1facf9fc 10548+ h_path.mnt = au_sbr_mnt(sb, bindex);
53392da6 10549+ err = vfsub_fsync(NULL, &h_path, datasync);
1facf9fc 10550+ }
10551+
10552+ return err;
10553+}
10554+
10555+static int au_do_fsync_dir(struct file *file, int datasync)
10556+{
10557+ int err;
5afbbe0d 10558+ aufs_bindex_t bbot, bindex;
1facf9fc 10559+ struct file *h_file;
10560+ struct super_block *sb;
10561+ struct inode *inode;
1facf9fc 10562+
521ced18 10563+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
1facf9fc 10564+ if (unlikely(err))
10565+ goto out;
10566+
c06a8ce3 10567+ inode = file_inode(file);
b912730e 10568+ sb = inode->i_sb;
5afbbe0d
AM
10569+ bbot = au_fbbot_dir(file);
10570+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108 10571+ h_file = au_hf_dir(file, bindex);
1facf9fc 10572+ if (!h_file || au_test_ro(sb, bindex, inode))
10573+ continue;
10574+
53392da6 10575+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
1facf9fc 10576+ }
10577+
4f0767ce 10578+out:
1facf9fc 10579+ return err;
10580+}
10581+
10582+/*
10583+ * @file may be NULL
10584+ */
1e00d052
AM
10585+static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10586+ int datasync)
1facf9fc 10587+{
10588+ int err;
b752ccd1 10589+ struct dentry *dentry;
5527c038 10590+ struct inode *inode;
1facf9fc 10591+ struct super_block *sb;
1facf9fc 10592+
10593+ err = 0;
2000de60 10594+ dentry = file->f_path.dentry;
5527c038 10595+ inode = d_inode(dentry);
febd17d6 10596+ inode_lock(inode);
1facf9fc 10597+ sb = dentry->d_sb;
10598+ si_noflush_read_lock(sb);
10599+ if (file)
10600+ err = au_do_fsync_dir(file, datasync);
10601+ else {
10602+ di_write_lock_child(dentry);
10603+ err = au_do_fsync_dir_no_file(dentry, datasync);
10604+ }
5527c038 10605+ au_cpup_attr_timesizes(inode);
1facf9fc 10606+ di_write_unlock(dentry);
10607+ if (file)
10608+ fi_write_unlock(file);
10609+
10610+ si_read_unlock(sb);
febd17d6 10611+ inode_unlock(inode);
1facf9fc 10612+ return err;
10613+}
10614+
10615+/* ---------------------------------------------------------------------- */
10616+
5afbbe0d 10617+static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
1facf9fc 10618+{
10619+ int err;
10620+ struct dentry *dentry;
9dbd164d 10621+ struct inode *inode, *h_inode;
1facf9fc 10622+ struct super_block *sb;
10623+
062440b3 10624+ AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
392086de 10625+
2000de60 10626+ dentry = file->f_path.dentry;
5527c038 10627+ inode = d_inode(dentry);
1facf9fc 10628+ IMustLock(inode);
10629+
10630+ sb = dentry->d_sb;
10631+ si_read_lock(sb, AuLock_FLUSH);
521ced18 10632+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
1facf9fc 10633+ if (unlikely(err))
10634+ goto out;
027c5e7a
AM
10635+ err = au_alive_dir(dentry);
10636+ if (!err)
10637+ err = au_vdir_init(file);
1facf9fc 10638+ di_downgrade_lock(dentry, AuLock_IR);
10639+ if (unlikely(err))
10640+ goto out_unlock;
10641+
5afbbe0d 10642+ h_inode = au_h_iptr(inode, au_ibtop(inode));
b752ccd1 10643+ if (!au_test_nfsd()) {
392086de 10644+ err = au_vdir_fill_de(file, ctx);
9dbd164d 10645+ fsstack_copy_attr_atime(inode, h_inode);
1facf9fc 10646+ } else {
10647+ /*
10648+ * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10649+ * encode_fh() and others.
10650+ */
9dbd164d 10651+ atomic_inc(&h_inode->i_count);
1facf9fc 10652+ di_read_unlock(dentry, AuLock_IR);
10653+ si_read_unlock(sb);
392086de 10654+ err = au_vdir_fill_de(file, ctx);
1facf9fc 10655+ fsstack_copy_attr_atime(inode, h_inode);
10656+ fi_write_unlock(file);
9dbd164d 10657+ iput(h_inode);
1facf9fc 10658+
10659+ AuTraceErr(err);
10660+ return err;
10661+ }
10662+
4f0767ce 10663+out_unlock:
1facf9fc 10664+ di_read_unlock(dentry, AuLock_IR);
10665+ fi_write_unlock(file);
4f0767ce 10666+out:
1facf9fc 10667+ si_read_unlock(sb);
10668+ return err;
10669+}
10670+
10671+/* ---------------------------------------------------------------------- */
10672+
10673+#define AuTestEmpty_WHONLY 1
dece6358
AM
10674+#define AuTestEmpty_CALLED (1 << 1)
10675+#define AuTestEmpty_SHWH (1 << 2)
1facf9fc 10676+#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
7f207e10
AM
10677+#define au_fset_testempty(flags, name) \
10678+ do { (flags) |= AuTestEmpty_##name; } while (0)
10679+#define au_fclr_testempty(flags, name) \
10680+ do { (flags) &= ~AuTestEmpty_##name; } while (0)
1facf9fc 10681+
dece6358
AM
10682+#ifndef CONFIG_AUFS_SHWH
10683+#undef AuTestEmpty_SHWH
10684+#define AuTestEmpty_SHWH 0
10685+#endif
10686+
1facf9fc 10687+struct test_empty_arg {
392086de 10688+ struct dir_context ctx;
1308ab2a 10689+ struct au_nhash *whlist;
1facf9fc 10690+ unsigned int flags;
10691+ int err;
10692+ aufs_bindex_t bindex;
10693+};
10694+
392086de
AM
10695+static int test_empty_cb(struct dir_context *ctx, const char *__name,
10696+ int namelen, loff_t offset __maybe_unused, u64 ino,
dece6358 10697+ unsigned int d_type)
1facf9fc 10698+{
392086de
AM
10699+ struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10700+ ctx);
1facf9fc 10701+ char *name = (void *)__name;
10702+
10703+ arg->err = 0;
10704+ au_fset_testempty(arg->flags, CALLED);
10705+ /* smp_mb(); */
10706+ if (name[0] == '.'
10707+ && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10708+ goto out; /* success */
10709+
10710+ if (namelen <= AUFS_WH_PFX_LEN
10711+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10712+ if (au_ftest_testempty(arg->flags, WHONLY)
1308ab2a 10713+ && !au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10714+ arg->err = -ENOTEMPTY;
10715+ goto out;
10716+ }
10717+
10718+ name += AUFS_WH_PFX_LEN;
10719+ namelen -= AUFS_WH_PFX_LEN;
1308ab2a 10720+ if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10721+ arg->err = au_nhash_append_wh
1308ab2a 10722+ (arg->whlist, name, namelen, ino, d_type, arg->bindex,
dece6358 10723+ au_ftest_testempty(arg->flags, SHWH));
1facf9fc 10724+
4f0767ce 10725+out:
1facf9fc 10726+ /* smp_mb(); */
10727+ AuTraceErr(arg->err);
10728+ return arg->err;
10729+}
10730+
10731+static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10732+{
10733+ int err;
10734+ struct file *h_file;
acd2b654 10735+ struct au_branch *br;
1facf9fc 10736+
10737+ h_file = au_h_open(dentry, arg->bindex,
10738+ O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
392086de 10739+ /*file*/NULL, /*force_wr*/0);
1facf9fc 10740+ err = PTR_ERR(h_file);
10741+ if (IS_ERR(h_file))
10742+ goto out;
10743+
10744+ err = 0;
10745+ if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
c06a8ce3 10746+ && !file_inode(h_file)->i_nlink)
1facf9fc 10747+ goto out_put;
10748+
10749+ do {
10750+ arg->err = 0;
10751+ au_fclr_testempty(arg->flags, CALLED);
10752+ /* smp_mb(); */
392086de 10753+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1facf9fc 10754+ if (err >= 0)
10755+ err = arg->err;
10756+ } while (!err && au_ftest_testempty(arg->flags, CALLED));
10757+
4f0767ce 10758+out_put:
1facf9fc 10759+ fput(h_file);
acd2b654
AM
10760+ br = au_sbr(dentry->d_sb, arg->bindex);
10761+ au_lcnt_dec(&br->br_nfiles);
4f0767ce 10762+out:
1facf9fc 10763+ return err;
10764+}
10765+
10766+struct do_test_empty_args {
10767+ int *errp;
10768+ struct dentry *dentry;
10769+ struct test_empty_arg *arg;
10770+};
10771+
10772+static void call_do_test_empty(void *args)
10773+{
10774+ struct do_test_empty_args *a = args;
10775+ *a->errp = do_test_empty(a->dentry, a->arg);
10776+}
10777+
10778+static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10779+{
10780+ int err, wkq_err;
10781+ struct dentry *h_dentry;
10782+ struct inode *h_inode;
10783+
10784+ h_dentry = au_h_dptr(dentry, arg->bindex);
5527c038 10785+ h_inode = d_inode(h_dentry);
53392da6 10786+ /* todo: i_mode changes anytime? */
be118d29 10787+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 10788+ err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
3c1bdaff 10789+ inode_unlock_shared(h_inode);
1facf9fc 10790+ if (!err)
10791+ err = do_test_empty(dentry, arg);
10792+ else {
10793+ struct do_test_empty_args args = {
10794+ .errp = &err,
10795+ .dentry = dentry,
10796+ .arg = arg
10797+ };
10798+ unsigned int flags = arg->flags;
10799+
10800+ wkq_err = au_wkq_wait(call_do_test_empty, &args);
10801+ if (unlikely(wkq_err))
10802+ err = wkq_err;
10803+ arg->flags = flags;
10804+ }
10805+
10806+ return err;
10807+}
10808+
10809+int au_test_empty_lower(struct dentry *dentry)
10810+{
10811+ int err;
1308ab2a 10812+ unsigned int rdhash;
5afbbe0d 10813+ aufs_bindex_t bindex, btop, btail;
1308ab2a 10814+ struct au_nhash whlist;
392086de
AM
10815+ struct test_empty_arg arg = {
10816+ .ctx = {
2000de60 10817+ .actor = test_empty_cb
392086de
AM
10818+ }
10819+ };
076b876e 10820+ int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
1facf9fc 10821+
dece6358
AM
10822+ SiMustAnyLock(dentry->d_sb);
10823+
1308ab2a 10824+ rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10825+ if (!rdhash)
10826+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10827+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
dece6358 10828+ if (unlikely(err))
1facf9fc 10829+ goto out;
10830+
1facf9fc 10831+ arg.flags = 0;
1308ab2a 10832+ arg.whlist = &whlist;
5afbbe0d 10833+ btop = au_dbtop(dentry);
dece6358
AM
10834+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10835+ au_fset_testempty(arg.flags, SHWH);
076b876e
AM
10836+ test_empty = do_test_empty;
10837+ if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10838+ test_empty = sio_test_empty;
5afbbe0d 10839+ arg.bindex = btop;
076b876e 10840+ err = test_empty(dentry, &arg);
1facf9fc 10841+ if (unlikely(err))
10842+ goto out_whlist;
10843+
10844+ au_fset_testempty(arg.flags, WHONLY);
10845+ btail = au_dbtaildir(dentry);
5afbbe0d 10846+ for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
1facf9fc 10847+ struct dentry *h_dentry;
10848+
10849+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10850+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10851+ arg.bindex = bindex;
076b876e 10852+ err = test_empty(dentry, &arg);
1facf9fc 10853+ }
10854+ }
10855+
4f0767ce 10856+out_whlist:
1308ab2a 10857+ au_nhash_wh_free(&whlist);
4f0767ce 10858+out:
1facf9fc 10859+ return err;
10860+}
10861+
10862+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10863+{
10864+ int err;
392086de
AM
10865+ struct test_empty_arg arg = {
10866+ .ctx = {
2000de60 10867+ .actor = test_empty_cb
392086de
AM
10868+ }
10869+ };
1facf9fc 10870+ aufs_bindex_t bindex, btail;
10871+
10872+ err = 0;
1308ab2a 10873+ arg.whlist = whlist;
1facf9fc 10874+ arg.flags = AuTestEmpty_WHONLY;
dece6358
AM
10875+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10876+ au_fset_testempty(arg.flags, SHWH);
1facf9fc 10877+ btail = au_dbtaildir(dentry);
5afbbe0d 10878+ for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
1facf9fc 10879+ struct dentry *h_dentry;
10880+
10881+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10882+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10883+ arg.bindex = bindex;
10884+ err = sio_test_empty(dentry, &arg);
10885+ }
10886+ }
10887+
10888+ return err;
10889+}
10890+
10891+/* ---------------------------------------------------------------------- */
10892+
10893+const struct file_operations aufs_dir_fop = {
4a4d8108 10894+ .owner = THIS_MODULE,
027c5e7a 10895+ .llseek = default_llseek,
1facf9fc 10896+ .read = generic_read_dir,
5afbbe0d 10897+ .iterate_shared = aufs_iterate_shared,
1facf9fc 10898+ .unlocked_ioctl = aufs_ioctl_dir,
b752ccd1
AM
10899+#ifdef CONFIG_COMPAT
10900+ .compat_ioctl = aufs_compat_ioctl_dir,
10901+#endif
1facf9fc 10902+ .open = aufs_open_dir,
10903+ .release = aufs_release_dir,
4a4d8108 10904+ .flush = aufs_flush_dir,
1facf9fc 10905+ .fsync = aufs_fsync_dir
10906+};
7f207e10
AM
10907diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10908--- /usr/share/empty/fs/aufs/dir.h 1970-01-01 01:00:00.000000000 +0100
ba1aed25 10909+++ linux/fs/aufs/dir.h 2019-03-05 12:13:00.139224339 +0100
9f237c51 10910@@ -0,0 +1,134 @@
062440b3 10911+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 10912+/*
ba1aed25 10913+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 10914+ *
10915+ * This program, aufs is free software; you can redistribute it and/or modify
10916+ * it under the terms of the GNU General Public License as published by
10917+ * the Free Software Foundation; either version 2 of the License, or
10918+ * (at your option) any later version.
dece6358
AM
10919+ *
10920+ * This program is distributed in the hope that it will be useful,
10921+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10922+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10923+ * GNU General Public License for more details.
10924+ *
10925+ * You should have received a copy of the GNU General Public License
523b37e3 10926+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10927+ */
10928+
10929+/*
10930+ * directory operations
10931+ */
10932+
10933+#ifndef __AUFS_DIR_H__
10934+#define __AUFS_DIR_H__
10935+
10936+#ifdef __KERNEL__
10937+
10938+#include <linux/fs.h>
1facf9fc 10939+
10940+/* ---------------------------------------------------------------------- */
10941+
10942+/* need to be faster and smaller */
10943+
10944+struct au_nhash {
dece6358
AM
10945+ unsigned int nh_num;
10946+ struct hlist_head *nh_head;
1facf9fc 10947+};
10948+
10949+struct au_vdir_destr {
10950+ unsigned char len;
10951+ unsigned char name[0];
10952+} __packed;
10953+
10954+struct au_vdir_dehstr {
10955+ struct hlist_node hash;
1c60b727 10956+ struct au_vdir_destr *str;
9f237c51 10957+ struct rcu_head rcu;
4a4d8108 10958+} ____cacheline_aligned_in_smp;
1facf9fc 10959+
10960+struct au_vdir_de {
10961+ ino_t de_ino;
10962+ unsigned char de_type;
10963+ /* caution: packed */
10964+ struct au_vdir_destr de_str;
10965+} __packed;
10966+
10967+struct au_vdir_wh {
10968+ struct hlist_node wh_hash;
dece6358
AM
10969+#ifdef CONFIG_AUFS_SHWH
10970+ ino_t wh_ino;
1facf9fc 10971+ aufs_bindex_t wh_bindex;
dece6358
AM
10972+ unsigned char wh_type;
10973+#else
10974+ aufs_bindex_t wh_bindex;
10975+#endif
10976+ /* caution: packed */
1facf9fc 10977+ struct au_vdir_destr wh_str;
10978+} __packed;
10979+
10980+union au_vdir_deblk_p {
10981+ unsigned char *deblk;
10982+ struct au_vdir_de *de;
10983+};
10984+
10985+struct au_vdir {
10986+ unsigned char **vd_deblk;
10987+ unsigned long vd_nblk;
1facf9fc 10988+ struct {
10989+ unsigned long ul;
10990+ union au_vdir_deblk_p p;
10991+ } vd_last;
10992+
be118d29 10993+ u64 vd_version;
dece6358 10994+ unsigned int vd_deblk_sz;
9f237c51
AM
10995+ unsigned long vd_jiffy;
10996+ struct rcu_head rcu;
4a4d8108 10997+} ____cacheline_aligned_in_smp;
1facf9fc 10998+
10999+/* ---------------------------------------------------------------------- */
11000+
11001+/* dir.c */
11002+extern const struct file_operations aufs_dir_fop;
11003+void au_add_nlink(struct inode *dir, struct inode *h_dir);
11004+void au_sub_nlink(struct inode *dir, struct inode *h_dir);
1308ab2a 11005+loff_t au_dir_size(struct file *file, struct dentry *dentry);
b912730e 11006+void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
1facf9fc 11007+int au_test_empty_lower(struct dentry *dentry);
11008+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
11009+
11010+/* vdir.c */
1308ab2a 11011+unsigned int au_rdhash_est(loff_t sz);
dece6358
AM
11012+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
11013+void au_nhash_wh_free(struct au_nhash *whlist);
1facf9fc 11014+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
11015+ int limit);
dece6358
AM
11016+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
11017+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
11018+ unsigned int d_type, aufs_bindex_t bindex,
11019+ unsigned char shwh);
1c60b727 11020+void au_vdir_free(struct au_vdir *vdir);
1facf9fc 11021+int au_vdir_init(struct file *file);
392086de 11022+int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
1facf9fc 11023+
11024+/* ioctl.c */
11025+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
11026+
1308ab2a 11027+#ifdef CONFIG_AUFS_RDU
11028+/* rdu.c */
11029+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
11030+#ifdef CONFIG_COMPAT
11031+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
11032+ unsigned long arg);
11033+#endif
1308ab2a 11034+#else
c1595e42
JR
11035+AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
11036+ unsigned int cmd, unsigned long arg)
b752ccd1 11037+#ifdef CONFIG_COMPAT
c1595e42
JR
11038+AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
11039+ unsigned int cmd, unsigned long arg)
b752ccd1 11040+#endif
1308ab2a 11041+#endif
11042+
1facf9fc 11043+#endif /* __KERNEL__ */
11044+#endif /* __AUFS_DIR_H__ */
8b6a4947
AM
11045diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
11046--- /usr/share/empty/fs/aufs/dirren.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 11047+++ linux/fs/aufs/dirren.c 2019-03-05 12:13:00.139224339 +0100
062440b3 11048@@ -0,0 +1,1316 @@
cd7a4cd9 11049+// SPDX-License-Identifier: GPL-2.0
8b6a4947 11050+/*
ba1aed25 11051+ * Copyright (C) 2017-2019 Junjiro R. Okajima
8b6a4947
AM
11052+ *
11053+ * This program, aufs is free software; you can redistribute it and/or modify
11054+ * it under the terms of the GNU General Public License as published by
11055+ * the Free Software Foundation; either version 2 of the License, or
11056+ * (at your option) any later version.
11057+ *
11058+ * This program is distributed in the hope that it will be useful,
11059+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11060+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11061+ * GNU General Public License for more details.
11062+ *
11063+ * You should have received a copy of the GNU General Public License
11064+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
11065+ */
11066+
11067+/*
acd2b654 11068+ * special handling in renaming a directory
8b6a4947
AM
11069+ * in order to support looking-up the before-renamed name on the lower readonly
11070+ * branches
11071+ */
11072+
11073+#include <linux/byteorder/generic.h>
11074+#include "aufs.h"
11075+
11076+static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
11077+{
11078+ int idx;
11079+
11080+ idx = au_dr_ihash(ent->dr_h_ino);
11081+ au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
11082+}
11083+
11084+static int au_dr_hino_test_empty(struct au_dr_br *dr)
11085+{
11086+ int ret, i;
11087+ struct hlist_bl_head *hbl;
11088+
11089+ ret = 1;
11090+ for (i = 0; ret && i < AuDirren_NHASH; i++) {
11091+ hbl = dr->dr_h_ino + i;
11092+ hlist_bl_lock(hbl);
11093+ ret &= hlist_bl_empty(hbl);
11094+ hlist_bl_unlock(hbl);
11095+ }
11096+
11097+ return ret;
11098+}
11099+
11100+static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
11101+{
11102+ struct au_dr_hino *found, *ent;
11103+ struct hlist_bl_head *hbl;
11104+ struct hlist_bl_node *pos;
11105+ int idx;
11106+
11107+ found = NULL;
11108+ idx = au_dr_ihash(ino);
11109+ hbl = dr->dr_h_ino + idx;
11110+ hlist_bl_lock(hbl);
11111+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11112+ if (ent->dr_h_ino == ino) {
11113+ found = ent;
11114+ break;
11115+ }
11116+ hlist_bl_unlock(hbl);
11117+
11118+ return found;
11119+}
11120+
11121+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
11122+ struct au_dr_hino *add_ent)
11123+{
11124+ int found, idx;
11125+ struct hlist_bl_head *hbl;
11126+ struct hlist_bl_node *pos;
11127+ struct au_dr_hino *ent;
11128+
11129+ found = 0;
11130+ idx = au_dr_ihash(ino);
11131+ hbl = dr->dr_h_ino + idx;
11132+#if 0
11133+ {
11134+ struct hlist_bl_node *tmp;
11135+
11136+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11137+ AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
11138+ }
11139+#endif
11140+ hlist_bl_lock(hbl);
11141+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11142+ if (ent->dr_h_ino == ino) {
11143+ found = 1;
11144+ break;
11145+ }
11146+ if (!found && add_ent)
11147+ hlist_bl_add_head(&add_ent->dr_hnode, hbl);
11148+ hlist_bl_unlock(hbl);
11149+
11150+ if (!found && add_ent)
11151+ AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
11152+
11153+ return found;
11154+}
11155+
11156+void au_dr_hino_free(struct au_dr_br *dr)
11157+{
11158+ int i;
11159+ struct hlist_bl_head *hbl;
11160+ struct hlist_bl_node *pos, *tmp;
11161+ struct au_dr_hino *ent;
11162+
11163+ /* SiMustWriteLock(sb); */
11164+
11165+ for (i = 0; i < AuDirren_NHASH; i++) {
11166+ hbl = dr->dr_h_ino + i;
11167+ /* no spinlock since sbinfo must be write-locked */
11168+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
9f237c51 11169+ au_kfree_rcu(ent);
8b6a4947
AM
11170+ INIT_HLIST_BL_HEAD(hbl);
11171+ }
11172+}
11173+
11174+/* returns the number of inodes or an error */
11175+static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
11176+ struct file *hinofile)
11177+{
11178+ int err, i;
11179+ ssize_t ssz;
11180+ loff_t pos, oldsize;
11181+ __be64 u64;
11182+ struct inode *hinoinode;
11183+ struct hlist_bl_head *hbl;
11184+ struct hlist_bl_node *n1, *n2;
11185+ struct au_dr_hino *ent;
11186+
11187+ SiMustWriteLock(sb);
11188+ AuDebugOn(!au_br_writable(br->br_perm));
11189+
11190+ hinoinode = file_inode(hinofile);
11191+ oldsize = i_size_read(hinoinode);
11192+
11193+ err = 0;
11194+ pos = 0;
11195+ hbl = br->br_dirren.dr_h_ino;
11196+ for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11197+ /* no bit-lock since sbinfo must be write-locked */
11198+ hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11199+ AuDbg("hi%llu, %pD2\n",
11200+ (unsigned long long)ent->dr_h_ino, hinofile);
11201+ u64 = cpu_to_be64(ent->dr_h_ino);
11202+ ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11203+ if (ssz == sizeof(u64))
11204+ continue;
11205+
11206+ /* write error */
11207+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11208+ err = -ENOSPC;
11209+ if (ssz < 0)
11210+ err = ssz;
11211+ break;
11212+ }
11213+ }
11214+ /* regardless the error */
11215+ if (pos < oldsize) {
11216+ err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11217+ AuTraceErr(err);
11218+ }
11219+
11220+ AuTraceErr(err);
11221+ return err;
11222+}
11223+
11224+static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11225+{
11226+ int err, hidx;
11227+ ssize_t ssz;
11228+ size_t sz, n;
11229+ loff_t pos;
11230+ uint64_t u64;
11231+ struct au_dr_hino *ent;
11232+ struct inode *hinoinode;
11233+ struct hlist_bl_head *hbl;
11234+
11235+ err = 0;
11236+ pos = 0;
11237+ hbl = dr->dr_h_ino;
11238+ hinoinode = file_inode(hinofile);
11239+ sz = i_size_read(hinoinode);
11240+ AuDebugOn(sz % sizeof(u64));
11241+ n = sz / sizeof(u64);
11242+ while (n--) {
11243+ ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11244+ if (unlikely(ssz != sizeof(u64))) {
11245+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11246+ err = -EINVAL;
11247+ if (ssz < 0)
11248+ err = ssz;
11249+ goto out_free;
11250+ }
11251+
11252+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11253+ if (!ent) {
11254+ err = -ENOMEM;
11255+ AuTraceErr(err);
11256+ goto out_free;
11257+ }
11258+ ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11259+ AuDbg("hi%llu, %pD2\n",
11260+ (unsigned long long)ent->dr_h_ino, hinofile);
11261+ hidx = au_dr_ihash(ent->dr_h_ino);
11262+ au_hbl_add(&ent->dr_hnode, hbl + hidx);
11263+ }
11264+ goto out; /* success */
11265+
11266+out_free:
11267+ au_dr_hino_free(dr);
11268+out:
11269+ AuTraceErr(err);
11270+ return err;
11271+}
11272+
11273+/*
11274+ * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11275+ * @path is a switch to distinguish load and store.
11276+ */
11277+static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11278+ struct au_branch *br, const struct path *path)
11279+{
11280+ int err, flags;
11281+ unsigned char load, suspend;
11282+ struct file *hinofile;
11283+ struct au_hinode *hdir;
11284+ struct inode *dir, *delegated;
11285+ struct path hinopath;
11286+ struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11287+ sizeof(AUFS_WH_DR_BRHINO) - 1);
11288+
11289+ AuDebugOn(bindex < 0 && !br);
11290+ AuDebugOn(bindex >= 0 && br);
11291+
11292+ err = -EINVAL;
11293+ suspend = !br;
11294+ if (suspend)
11295+ br = au_sbr(sb, bindex);
11296+ load = !!path;
11297+ if (!load) {
11298+ path = &br->br_path;
11299+ AuDebugOn(!au_br_writable(br->br_perm));
11300+ if (unlikely(!au_br_writable(br->br_perm)))
11301+ goto out;
11302+ }
11303+
11304+ hdir = NULL;
11305+ if (suspend) {
11306+ dir = d_inode(sb->s_root);
11307+ hdir = au_hinode(au_ii(dir), bindex);
11308+ dir = hdir->hi_inode;
11309+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11310+ } else {
11311+ dir = d_inode(path->dentry);
11312+ inode_lock_nested(dir, AuLsc_I_CHILD);
11313+ }
11314+ hinopath.dentry = vfsub_lkup_one(&hinoname, path->dentry);
11315+ err = PTR_ERR(hinopath.dentry);
11316+ if (IS_ERR(hinopath.dentry))
11317+ goto out_unlock;
11318+
11319+ err = 0;
11320+ flags = O_RDONLY;
11321+ if (load) {
11322+ if (d_is_negative(hinopath.dentry))
11323+ goto out_dput; /* success */
11324+ } else {
11325+ if (au_dr_hino_test_empty(&br->br_dirren)) {
11326+ if (d_is_positive(hinopath.dentry)) {
11327+ delegated = NULL;
11328+ err = vfsub_unlink(dir, &hinopath, &delegated,
11329+ /*force*/0);
11330+ AuTraceErr(err);
11331+ if (unlikely(err))
11332+ pr_err("ignored err %d, %pd2\n",
11333+ err, hinopath.dentry);
11334+ if (unlikely(err == -EWOULDBLOCK))
11335+ iput(delegated);
11336+ err = 0;
11337+ }
11338+ goto out_dput;
11339+ } else if (!d_is_positive(hinopath.dentry)) {
11340+ err = vfsub_create(dir, &hinopath, 0600,
11341+ /*want_excl*/false);
11342+ AuTraceErr(err);
11343+ if (unlikely(err))
11344+ goto out_dput;
11345+ }
11346+ flags = O_WRONLY;
11347+ }
11348+ hinopath.mnt = path->mnt;
11349+ hinofile = vfsub_dentry_open(&hinopath, flags);
11350+ if (suspend)
11351+ au_hn_inode_unlock(hdir);
11352+ else
11353+ inode_unlock(dir);
11354+ dput(hinopath.dentry);
11355+ AuTraceErrPtr(hinofile);
11356+ if (IS_ERR(hinofile)) {
11357+ err = PTR_ERR(hinofile);
11358+ goto out;
11359+ }
11360+
11361+ if (load)
11362+ err = au_dr_hino_load(&br->br_dirren, hinofile);
11363+ else
11364+ err = au_dr_hino_store(sb, br, hinofile);
11365+ fput(hinofile);
11366+ goto out;
11367+
11368+out_dput:
11369+ dput(hinopath.dentry);
11370+out_unlock:
11371+ if (suspend)
11372+ au_hn_inode_unlock(hdir);
11373+ else
11374+ inode_unlock(dir);
11375+out:
11376+ AuTraceErr(err);
11377+ return err;
11378+}
11379+
11380+/* ---------------------------------------------------------------------- */
11381+
11382+static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11383+{
11384+ int err;
11385+ struct kstatfs kstfs;
11386+ dev_t dev;
11387+ struct dentry *dentry;
11388+ struct super_block *sb;
11389+
11390+ err = vfs_statfs((void *)path, &kstfs);
11391+ AuTraceErr(err);
11392+ if (unlikely(err))
11393+ goto out;
11394+
11395+ /* todo: support for UUID */
11396+
11397+ if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11398+ brid->type = AuBrid_FSID;
11399+ brid->fsid = kstfs.f_fsid;
11400+ } else {
11401+ dentry = path->dentry;
11402+ sb = dentry->d_sb;
11403+ dev = sb->s_dev;
11404+ if (dev) {
11405+ brid->type = AuBrid_DEV;
11406+ brid->dev = dev;
11407+ }
11408+ }
11409+
11410+out:
11411+ return err;
11412+}
11413+
11414+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11415+ const struct path *path)
11416+{
11417+ int err, i;
11418+ struct au_dr_br *dr;
11419+ struct hlist_bl_head *hbl;
11420+
11421+ dr = &br->br_dirren;
11422+ hbl = dr->dr_h_ino;
11423+ for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11424+ INIT_HLIST_BL_HEAD(hbl);
11425+
11426+ err = au_dr_brid_init(&dr->dr_brid, path);
11427+ if (unlikely(err))
11428+ goto out;
11429+
11430+ if (au_opt_test(au_mntflags(sb), DIRREN))
11431+ err = au_dr_hino(sb, /*bindex*/-1, br, path);
11432+
11433+out:
11434+ AuTraceErr(err);
11435+ return err;
11436+}
11437+
11438+int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11439+{
11440+ int err;
11441+
11442+ err = 0;
11443+ if (au_br_writable(br->br_perm))
11444+ err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11445+ if (!err)
11446+ au_dr_hino_free(&br->br_dirren);
11447+
11448+ return err;
11449+}
11450+
11451+/* ---------------------------------------------------------------------- */
11452+
11453+static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11454+ char *buf, size_t sz)
11455+{
11456+ int err;
11457+ unsigned int major, minor;
11458+ char *p;
11459+
11460+ p = buf;
11461+ err = snprintf(p, sz, "%d_", brid->type);
11462+ AuDebugOn(err > sz);
11463+ p += err;
11464+ sz -= err;
11465+ switch (brid->type) {
11466+ case AuBrid_Unset:
11467+ return -EINVAL;
11468+ case AuBrid_UUID:
11469+ err = snprintf(p, sz, "%pU", brid->uuid.b);
11470+ break;
11471+ case AuBrid_FSID:
11472+ err = snprintf(p, sz, "%08x-%08x",
11473+ brid->fsid.val[0], brid->fsid.val[1]);
11474+ break;
11475+ case AuBrid_DEV:
11476+ major = MAJOR(brid->dev);
11477+ minor = MINOR(brid->dev);
11478+ if (major <= 0xff && minor <= 0xff)
11479+ err = snprintf(p, sz, "%02x%02x", major, minor);
11480+ else
11481+ err = snprintf(p, sz, "%03x:%05x", major, minor);
11482+ break;
11483+ }
11484+ AuDebugOn(err > sz);
11485+ p += err;
11486+ sz -= err;
11487+ err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11488+ AuDebugOn(err > sz);
11489+ p += err;
11490+ sz -= err;
11491+
11492+ return p - buf;
11493+}
11494+
11495+static int au_drinfo_name(struct au_branch *br, char *name, int len)
11496+{
11497+ int rlen;
11498+ struct dentry *br_dentry;
11499+ struct inode *br_inode;
11500+
11501+ br_dentry = au_br_dentry(br);
11502+ br_inode = d_inode(br_dentry);
11503+ rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11504+ AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11505+ AuDebugOn(rlen > len);
11506+
11507+ return rlen;
11508+}
11509+
11510+/* ---------------------------------------------------------------------- */
11511+
11512+/*
11513+ * from the given @h_dentry, construct drinfo at @*fdata.
11514+ * when the size of @*fdata is not enough, reallocate and return new @fdata and
11515+ * @allocated.
11516+ */
11517+static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11518+ struct dentry *h_dentry,
11519+ unsigned char *allocated)
11520+{
11521+ int err, v;
11522+ struct au_drinfo_fdata *f, *p;
11523+ struct au_drinfo *drinfo;
11524+ struct inode *h_inode;
11525+ struct qstr *qname;
11526+
11527+ err = 0;
11528+ f = *fdata;
11529+ h_inode = d_inode(h_dentry);
11530+ qname = &h_dentry->d_name;
11531+ drinfo = &f->drinfo;
11532+ drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11533+ drinfo->oldnamelen = qname->len;
11534+ if (*allocated < sizeof(*f) + qname->len) {
11535+ v = roundup_pow_of_two(*allocated + qname->len);
11536+ p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11537+ if (unlikely(!p)) {
11538+ err = -ENOMEM;
11539+ AuTraceErr(err);
11540+ goto out;
11541+ }
11542+ f = p;
11543+ *fdata = f;
11544+ *allocated = v;
11545+ drinfo = &f->drinfo;
11546+ }
11547+ memcpy(drinfo->oldname, qname->name, qname->len);
11548+ AuDbg("i%llu, %.*s\n",
11549+ be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11550+ drinfo->oldname);
11551+
11552+out:
11553+ AuTraceErr(err);
11554+ return err;
11555+}
11556+
11557+/* callers have to free the return value */
11558+static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11559+{
11560+ struct au_drinfo *ret, *drinfo;
11561+ struct au_drinfo_fdata fdata;
11562+ int len;
11563+ loff_t pos;
11564+ ssize_t ssz;
11565+
11566+ ret = ERR_PTR(-EIO);
11567+ pos = 0;
11568+ ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11569+ if (unlikely(ssz != sizeof(fdata))) {
11570+ AuIOErr("ssz %zd, %u, %pD2\n",
11571+ ssz, (unsigned int)sizeof(fdata), file);
11572+ goto out;
11573+ }
11574+
11575+ fdata.magic = ntohl((__force __be32)fdata.magic);
11576+ switch (fdata.magic) {
11577+ case AUFS_DRINFO_MAGIC_V1:
11578+ break;
11579+ default:
11580+ AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11581+ fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11582+ goto out;
11583+ }
11584+
11585+ drinfo = &fdata.drinfo;
11586+ len = drinfo->oldnamelen;
11587+ if (!len) {
11588+ AuIOErr("broken drinfo %pD2\n", file);
11589+ goto out;
11590+ }
11591+
11592+ ret = NULL;
11593+ drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11594+ if (unlikely(h_ino && drinfo->ino != h_ino)) {
11595+ AuDbg("ignored i%llu, i%llu, %pD2\n",
11596+ (unsigned long long)drinfo->ino,
11597+ (unsigned long long)h_ino, file);
11598+ goto out; /* success */
11599+ }
11600+
11601+ ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11602+ if (unlikely(!ret)) {
11603+ ret = ERR_PTR(-ENOMEM);
11604+ AuTraceErrPtr(ret);
11605+ goto out;
11606+ }
11607+
11608+ *ret = *drinfo;
11609+ ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11610+ if (unlikely(ssz != len)) {
9f237c51 11611+ au_kfree_rcu(ret);
8b6a4947
AM
11612+ ret = ERR_PTR(-EIO);
11613+ AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11614+ goto out;
11615+ }
11616+
11617+ AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11618+
11619+out:
11620+ return ret;
11621+}
11622+
11623+/* ---------------------------------------------------------------------- */
11624+
11625+/* in order to be revertible */
11626+struct au_drinfo_rev_elm {
11627+ int created;
11628+ struct dentry *info_dentry;
11629+ struct au_drinfo *info_last;
11630+};
11631+
11632+struct au_drinfo_rev {
11633+ unsigned char already;
11634+ aufs_bindex_t nelm;
11635+ struct au_drinfo_rev_elm elm[0];
11636+};
11637+
11638+/* todo: isn't it too large? */
11639+struct au_drinfo_store {
11640+ struct path h_ppath;
11641+ struct dentry *h_dentry;
11642+ struct au_drinfo_fdata *fdata;
11643+ char *infoname; /* inside of whname, just after PFX */
11644+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11645+ aufs_bindex_t btgt, btail;
11646+ unsigned char no_sio,
11647+ allocated, /* current size of *fdata */
11648+ infonamelen, /* room size for p */
acd2b654 11649+ whnamelen, /* length of the generated name */
8b6a4947
AM
11650+ renameback; /* renamed back */
11651+};
11652+
11653+/* on rename(2) error, the caller should revert it using @elm */
11654+static int au_drinfo_do_store(struct au_drinfo_store *w,
11655+ struct au_drinfo_rev_elm *elm)
11656+{
11657+ int err, len;
11658+ ssize_t ssz;
11659+ loff_t pos;
11660+ struct path infopath = {
11661+ .mnt = w->h_ppath.mnt
11662+ };
11663+ struct inode *h_dir, *h_inode, *delegated;
11664+ struct file *infofile;
11665+ struct qstr *qname;
11666+
11667+ AuDebugOn(elm
11668+ && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11669+
11670+ infopath.dentry = vfsub_lookup_one_len(w->whname, w->h_ppath.dentry,
11671+ w->whnamelen);
11672+ AuTraceErrPtr(infopath.dentry);
11673+ if (IS_ERR(infopath.dentry)) {
11674+ err = PTR_ERR(infopath.dentry);
11675+ goto out;
11676+ }
11677+
11678+ err = 0;
11679+ h_dir = d_inode(w->h_ppath.dentry);
11680+ if (elm && d_is_negative(infopath.dentry)) {
11681+ err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11682+ AuTraceErr(err);
11683+ if (unlikely(err))
11684+ goto out_dput;
11685+ elm->created = 1;
11686+ elm->info_dentry = dget(infopath.dentry);
11687+ }
11688+
11689+ infofile = vfsub_dentry_open(&infopath, O_RDWR);
11690+ AuTraceErrPtr(infofile);
11691+ if (IS_ERR(infofile)) {
11692+ err = PTR_ERR(infofile);
11693+ goto out_dput;
11694+ }
11695+
11696+ h_inode = d_inode(infopath.dentry);
11697+ if (elm && i_size_read(h_inode)) {
11698+ h_inode = d_inode(w->h_dentry);
11699+ elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11700+ AuTraceErrPtr(elm->info_last);
11701+ if (IS_ERR(elm->info_last)) {
11702+ err = PTR_ERR(elm->info_last);
11703+ elm->info_last = NULL;
11704+ AuDebugOn(elm->info_dentry);
11705+ goto out_fput;
11706+ }
11707+ }
11708+
11709+ if (elm && w->renameback) {
11710+ delegated = NULL;
11711+ err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11712+ AuTraceErr(err);
11713+ if (unlikely(err == -EWOULDBLOCK))
11714+ iput(delegated);
11715+ goto out_fput;
11716+ }
11717+
11718+ pos = 0;
11719+ qname = &w->h_dentry->d_name;
11720+ len = sizeof(*w->fdata) + qname->len;
11721+ if (!elm)
11722+ len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11723+ ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11724+ if (ssz == len) {
11725+ AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11726+ w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11727+ goto out_fput; /* success */
11728+ } else {
11729+ err = -EIO;
11730+ if (ssz < 0)
11731+ err = ssz;
11732+ /* the caller should revert it using @elm */
11733+ }
11734+
11735+out_fput:
11736+ fput(infofile);
11737+out_dput:
11738+ dput(infopath.dentry);
11739+out:
11740+ AuTraceErr(err);
11741+ return err;
11742+}
11743+
11744+struct au_call_drinfo_do_store_args {
11745+ int *errp;
11746+ struct au_drinfo_store *w;
11747+ struct au_drinfo_rev_elm *elm;
11748+};
11749+
11750+static void au_call_drinfo_do_store(void *args)
11751+{
11752+ struct au_call_drinfo_do_store_args *a = args;
11753+
11754+ *a->errp = au_drinfo_do_store(a->w, a->elm);
11755+}
11756+
11757+static int au_drinfo_store_sio(struct au_drinfo_store *w,
11758+ struct au_drinfo_rev_elm *elm)
11759+{
11760+ int err, wkq_err;
11761+
11762+ if (w->no_sio)
11763+ err = au_drinfo_do_store(w, elm);
11764+ else {
11765+ struct au_call_drinfo_do_store_args a = {
11766+ .errp = &err,
11767+ .w = w,
11768+ .elm = elm
11769+ };
11770+ wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11771+ if (unlikely(wkq_err))
11772+ err = wkq_err;
11773+ }
11774+ AuTraceErr(err);
11775+
11776+ return err;
11777+}
11778+
11779+static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11780+ aufs_bindex_t btgt)
11781+{
11782+ int err;
11783+
11784+ memset(w, 0, sizeof(*w));
11785+ w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11786+ strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11787+ w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11788+ w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11789+ w->btgt = btgt;
11790+ w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11791+
11792+ err = -ENOMEM;
11793+ w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11794+ if (unlikely(!w->fdata)) {
11795+ AuTraceErr(err);
11796+ goto out;
11797+ }
11798+ w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11799+ err = 0;
11800+
11801+out:
11802+ return err;
11803+}
11804+
11805+static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11806+{
9f237c51 11807+ au_kfree_rcu(w->fdata);
8b6a4947
AM
11808+}
11809+
11810+static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11811+ struct au_drinfo_store *w)
11812+{
11813+ struct au_drinfo_rev_elm *elm;
11814+ struct inode *h_dir, *delegated;
11815+ int err, nelm;
11816+ struct path infopath = {
11817+ .mnt = w->h_ppath.mnt
11818+ };
11819+
11820+ h_dir = d_inode(w->h_ppath.dentry);
11821+ IMustLock(h_dir);
11822+
11823+ err = 0;
11824+ elm = rev->elm;
11825+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11826+ AuDebugOn(elm->created && elm->info_last);
11827+ if (elm->created) {
11828+ AuDbg("here\n");
11829+ delegated = NULL;
11830+ infopath.dentry = elm->info_dentry;
11831+ err = vfsub_unlink(h_dir, &infopath, &delegated,
11832+ !w->no_sio);
11833+ AuTraceErr(err);
11834+ if (unlikely(err == -EWOULDBLOCK))
11835+ iput(delegated);
11836+ dput(elm->info_dentry);
11837+ } else if (elm->info_last) {
11838+ AuDbg("here\n");
11839+ w->fdata->drinfo = *elm->info_last;
11840+ memcpy(w->fdata->drinfo.oldname,
11841+ elm->info_last->oldname,
11842+ elm->info_last->oldnamelen);
11843+ err = au_drinfo_store_sio(w, /*elm*/NULL);
9f237c51 11844+ au_kfree_rcu(elm->info_last);
8b6a4947
AM
11845+ }
11846+ if (unlikely(err))
11847+ AuIOErr("%d, %s\n", err, w->whname);
11848+ /* go on even if err */
11849+ }
11850+}
11851+
11852+/* caller has to call au_dr_rename_fin() later */
11853+static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11854+ struct qstr *dst_name, void *_rev)
11855+{
11856+ int err, sz, nelm;
11857+ aufs_bindex_t bindex, btail;
11858+ struct au_drinfo_store work;
11859+ struct au_drinfo_rev *rev, **p;
11860+ struct au_drinfo_rev_elm *elm;
11861+ struct super_block *sb;
11862+ struct au_branch *br;
11863+ struct au_hinode *hdir;
11864+
11865+ err = au_drinfo_store_work_init(&work, btgt);
11866+ AuTraceErr(err);
11867+ if (unlikely(err))
11868+ goto out;
11869+
11870+ err = -ENOMEM;
11871+ btail = au_dbtaildir(dentry);
11872+ nelm = btail - btgt;
11873+ sz = sizeof(*rev) + sizeof(*elm) * nelm;
11874+ rev = kcalloc(1, sz, GFP_NOFS);
11875+ if (unlikely(!rev)) {
11876+ AuTraceErr(err);
11877+ goto out_args;
11878+ }
11879+ rev->nelm = nelm;
11880+ elm = rev->elm;
11881+ p = _rev;
11882+ *p = rev;
11883+
11884+ err = 0;
11885+ sb = dentry->d_sb;
11886+ work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11887+ work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11888+ hdir = au_hi(d_inode(dentry), btgt);
11889+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11890+ for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11891+ work.h_dentry = au_h_dptr(dentry, bindex);
11892+ if (!work.h_dentry)
11893+ continue;
11894+
11895+ err = au_drinfo_construct(&work.fdata, work.h_dentry,
11896+ &work.allocated);
11897+ AuTraceErr(err);
11898+ if (unlikely(err))
11899+ break;
11900+
11901+ work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11902+ br = au_sbr(sb, bindex);
11903+ work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11904+ work.whnamelen += au_drinfo_name(br, work.infoname,
11905+ work.infonamelen);
11906+ AuDbg("whname %.*s, i%llu, %.*s\n",
11907+ work.whnamelen, work.whname,
11908+ be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11909+ work.fdata->drinfo.oldnamelen,
11910+ work.fdata->drinfo.oldname);
11911+
11912+ err = au_drinfo_store_sio(&work, elm);
11913+ AuTraceErr(err);
11914+ if (unlikely(err))
11915+ break;
11916+ }
11917+ if (unlikely(err)) {
11918+ /* revert all drinfo */
11919+ au_drinfo_store_rev(rev, &work);
9f237c51 11920+ au_kfree_try_rcu(rev);
8b6a4947
AM
11921+ *p = NULL;
11922+ }
11923+ au_hn_inode_unlock(hdir);
11924+
11925+out_args:
11926+ au_drinfo_store_work_fin(&work);
11927+out:
11928+ return err;
11929+}
11930+
11931+/* ---------------------------------------------------------------------- */
11932+
11933+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11934+ struct qstr *dst_name, void *_rev)
11935+{
11936+ int err, already;
11937+ ino_t ino;
11938+ struct super_block *sb;
11939+ struct au_branch *br;
11940+ struct au_dr_br *dr;
11941+ struct dentry *h_dentry;
11942+ struct inode *h_inode;
11943+ struct au_dr_hino *ent;
11944+ struct au_drinfo_rev *rev, **p;
11945+
11946+ AuDbg("bindex %d\n", bindex);
11947+
11948+ err = -ENOMEM;
11949+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11950+ if (unlikely(!ent))
11951+ goto out;
11952+
11953+ sb = src->d_sb;
11954+ br = au_sbr(sb, bindex);
11955+ dr = &br->br_dirren;
11956+ h_dentry = au_h_dptr(src, bindex);
11957+ h_inode = d_inode(h_dentry);
11958+ ino = h_inode->i_ino;
11959+ ent->dr_h_ino = ino;
11960+ already = au_dr_hino_test_add(dr, ino, ent);
11961+ AuDbg("b%d, hi%llu, already %d\n",
11962+ bindex, (unsigned long long)ino, already);
11963+
11964+ err = au_drinfo_store(src, bindex, dst_name, _rev);
11965+ AuTraceErr(err);
11966+ if (!err) {
11967+ p = _rev;
11968+ rev = *p;
11969+ rev->already = already;
11970+ goto out; /* success */
11971+ }
11972+
11973+ /* revert */
11974+ if (!already)
11975+ au_dr_hino_del(dr, ent);
9f237c51 11976+ au_kfree_rcu(ent);
8b6a4947
AM
11977+
11978+out:
11979+ AuTraceErr(err);
11980+ return err;
11981+}
11982+
11983+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11984+{
11985+ struct au_drinfo_rev *rev;
11986+ struct au_drinfo_rev_elm *elm;
11987+ int nelm;
11988+
11989+ rev = _rev;
11990+ elm = rev->elm;
11991+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11992+ dput(elm->info_dentry);
9f237c51 11993+ au_kfree_rcu(elm->info_last);
8b6a4947 11994+ }
9f237c51 11995+ au_kfree_try_rcu(rev);
8b6a4947
AM
11996+}
11997+
11998+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11999+{
12000+ int err;
12001+ struct au_drinfo_store work;
12002+ struct au_drinfo_rev *rev = _rev;
12003+ struct super_block *sb;
12004+ struct au_branch *br;
12005+ struct inode *h_inode;
12006+ struct au_dr_br *dr;
12007+ struct au_dr_hino *ent;
12008+
12009+ err = au_drinfo_store_work_init(&work, btgt);
12010+ if (unlikely(err))
12011+ goto out;
12012+
12013+ sb = src->d_sb;
12014+ br = au_sbr(sb, btgt);
12015+ work.h_ppath.dentry = au_h_dptr(src, btgt);
12016+ work.h_ppath.mnt = au_br_mnt(br);
12017+ au_drinfo_store_rev(rev, &work);
12018+ au_drinfo_store_work_fin(&work);
12019+ if (rev->already)
12020+ goto out;
12021+
12022+ dr = &br->br_dirren;
12023+ h_inode = d_inode(work.h_ppath.dentry);
12024+ ent = au_dr_hino_find(dr, h_inode->i_ino);
12025+ BUG_ON(!ent);
12026+ au_dr_hino_del(dr, ent);
9f237c51 12027+ au_kfree_rcu(ent);
8b6a4947
AM
12028+
12029+out:
9f237c51 12030+ au_kfree_try_rcu(rev);
8b6a4947
AM
12031+ if (unlikely(err))
12032+ pr_err("failed to remove dirren info\n");
12033+}
12034+
12035+/* ---------------------------------------------------------------------- */
12036+
12037+static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
12038+ char *whname, int whnamelen,
12039+ struct dentry **info_dentry)
12040+{
12041+ struct au_drinfo *drinfo;
12042+ struct file *f;
12043+ struct inode *h_dir;
12044+ struct path infopath;
12045+ int unlocked;
12046+
12047+ AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
12048+
12049+ *info_dentry = NULL;
12050+ drinfo = NULL;
12051+ unlocked = 0;
12052+ h_dir = d_inode(h_ppath->dentry);
be118d29 12053+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8b6a4947
AM
12054+ infopath.dentry = vfsub_lookup_one_len(whname, h_ppath->dentry,
12055+ whnamelen);
12056+ if (IS_ERR(infopath.dentry)) {
12057+ drinfo = (void *)infopath.dentry;
12058+ goto out;
12059+ }
12060+
12061+ if (d_is_negative(infopath.dentry))
12062+ goto out_dput; /* success */
12063+
12064+ infopath.mnt = h_ppath->mnt;
12065+ f = vfsub_dentry_open(&infopath, O_RDONLY);
12066+ inode_unlock_shared(h_dir);
12067+ unlocked = 1;
12068+ if (IS_ERR(f)) {
12069+ drinfo = (void *)f;
12070+ goto out_dput;
12071+ }
12072+
12073+ drinfo = au_drinfo_read_k(f, /*h_ino*/0);
12074+ if (IS_ERR_OR_NULL(drinfo))
12075+ goto out_fput;
12076+
12077+ AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
12078+ *info_dentry = dget(infopath.dentry); /* keep it alive */
12079+
12080+out_fput:
12081+ fput(f);
12082+out_dput:
12083+ dput(infopath.dentry);
12084+out:
12085+ if (!unlocked)
12086+ inode_unlock_shared(h_dir);
12087+ AuTraceErrPtr(drinfo);
12088+ return drinfo;
12089+}
12090+
12091+struct au_drinfo_do_load_args {
12092+ struct au_drinfo **drinfop;
12093+ struct path *h_ppath;
12094+ char *whname;
12095+ int whnamelen;
12096+ struct dentry **info_dentry;
12097+};
12098+
12099+static void au_call_drinfo_do_load(void *args)
12100+{
12101+ struct au_drinfo_do_load_args *a = args;
12102+
12103+ *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
12104+ a->info_dentry);
12105+}
12106+
12107+struct au_drinfo_load {
12108+ struct path h_ppath;
12109+ struct qstr *qname;
12110+ unsigned char no_sio;
12111+
12112+ aufs_bindex_t ninfo;
12113+ struct au_drinfo **drinfo;
12114+};
12115+
12116+static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
12117+ struct au_branch *br)
12118+{
12119+ int err, wkq_err, whnamelen, e;
12120+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
12121+ = AUFS_WH_DR_INFO_PFX;
12122+ struct au_drinfo *drinfo;
12123+ struct qstr oldname;
12124+ struct inode *h_dir, *delegated;
12125+ struct dentry *info_dentry;
12126+ struct path infopath;
12127+
12128+ whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
12129+ whnamelen += au_drinfo_name(br, whname + whnamelen,
12130+ sizeof(whname) - whnamelen);
12131+ if (w->no_sio)
12132+ drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
12133+ &info_dentry);
12134+ else {
12135+ struct au_drinfo_do_load_args args = {
12136+ .drinfop = &drinfo,
12137+ .h_ppath = &w->h_ppath,
12138+ .whname = whname,
12139+ .whnamelen = whnamelen,
12140+ .info_dentry = &info_dentry
12141+ };
12142+ wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
12143+ if (unlikely(wkq_err))
12144+ drinfo = ERR_PTR(wkq_err);
12145+ }
12146+ err = PTR_ERR(drinfo);
12147+ if (IS_ERR_OR_NULL(drinfo))
12148+ goto out;
12149+
12150+ err = 0;
12151+ oldname.len = drinfo->oldnamelen;
12152+ oldname.name = drinfo->oldname;
12153+ if (au_qstreq(w->qname, &oldname)) {
12154+ /* the name is renamed back */
9f237c51 12155+ au_kfree_rcu(drinfo);
8b6a4947
AM
12156+ drinfo = NULL;
12157+
12158+ infopath.dentry = info_dentry;
12159+ infopath.mnt = w->h_ppath.mnt;
12160+ h_dir = d_inode(w->h_ppath.dentry);
12161+ delegated = NULL;
12162+ inode_lock_nested(h_dir, AuLsc_I_PARENT);
12163+ e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
12164+ inode_unlock(h_dir);
12165+ if (unlikely(e))
12166+ AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
12167+ if (unlikely(e == -EWOULDBLOCK))
12168+ iput(delegated);
12169+ }
9f237c51 12170+ au_kfree_rcu(w->drinfo[bindex]);
8b6a4947
AM
12171+ w->drinfo[bindex] = drinfo;
12172+ dput(info_dentry);
12173+
12174+out:
12175+ AuTraceErr(err);
12176+ return err;
12177+}
12178+
12179+/* ---------------------------------------------------------------------- */
12180+
12181+static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12182+{
12183+ struct au_drinfo **p = drinfo;
12184+
12185+ while (n-- > 0)
9f237c51
AM
12186+ au_kfree_rcu(*drinfo++);
12187+ au_kfree_try_rcu(p);
8b6a4947
AM
12188+}
12189+
12190+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12191+ aufs_bindex_t btgt)
12192+{
12193+ int err, ninfo;
12194+ struct au_drinfo_load w;
12195+ aufs_bindex_t bindex, bbot;
12196+ struct au_branch *br;
12197+ struct inode *h_dir;
12198+ struct au_dr_hino *ent;
12199+ struct super_block *sb;
12200+
12201+ AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12202+ AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12203+ AuLNPair(&lkup->whname), btgt);
12204+
12205+ sb = dentry->d_sb;
12206+ bbot = au_sbbot(sb);
12207+ w.ninfo = bbot + 1;
12208+ if (!lkup->dirren.drinfo) {
12209+ lkup->dirren.drinfo = kcalloc(w.ninfo,
12210+ sizeof(*lkup->dirren.drinfo),
12211+ GFP_NOFS);
12212+ if (unlikely(!lkup->dirren.drinfo)) {
12213+ err = -ENOMEM;
12214+ goto out;
12215+ }
12216+ lkup->dirren.ninfo = w.ninfo;
12217+ }
12218+ w.drinfo = lkup->dirren.drinfo;
12219+ w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12220+ w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12221+ AuDebugOn(!w.h_ppath.dentry);
12222+ w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12223+ w.qname = &dentry->d_name;
12224+
12225+ ninfo = 0;
12226+ for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12227+ br = au_sbr(sb, bindex);
12228+ err = au_drinfo_load(&w, bindex, br);
12229+ if (unlikely(err))
12230+ goto out_free;
12231+ if (w.drinfo[bindex])
12232+ ninfo++;
12233+ }
12234+ if (!ninfo) {
12235+ br = au_sbr(sb, btgt);
12236+ h_dir = d_inode(w.h_ppath.dentry);
12237+ ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12238+ AuDebugOn(!ent);
12239+ au_dr_hino_del(&br->br_dirren, ent);
9f237c51 12240+ au_kfree_rcu(ent);
8b6a4947
AM
12241+ }
12242+ goto out; /* success */
12243+
12244+out_free:
12245+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12246+ lkup->dirren.ninfo = 0;
12247+ lkup->dirren.drinfo = NULL;
12248+out:
12249+ AuTraceErr(err);
12250+ return err;
12251+}
12252+
12253+void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12254+{
12255+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12256+}
12257+
12258+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12259+{
12260+ int err;
12261+ struct au_drinfo *drinfo;
12262+
12263+ err = 0;
12264+ if (!lkup->dirren.drinfo)
12265+ goto out;
12266+ AuDebugOn(lkup->dirren.ninfo < btgt + 1);
12267+ drinfo = lkup->dirren.drinfo[btgt + 1];
12268+ if (!drinfo)
12269+ goto out;
12270+
9f237c51 12271+ au_kfree_try_rcu(lkup->whname.name);
8b6a4947
AM
12272+ lkup->whname.name = NULL;
12273+ lkup->dirren.dr_name.len = drinfo->oldnamelen;
12274+ lkup->dirren.dr_name.name = drinfo->oldname;
12275+ lkup->name = &lkup->dirren.dr_name;
12276+ err = au_wh_name_alloc(&lkup->whname, lkup->name);
12277+ if (!err)
12278+ AuDbg("name %.*s, whname %.*s, b%d\n",
12279+ AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12280+ btgt);
12281+
12282+out:
12283+ AuTraceErr(err);
12284+ return err;
12285+}
12286+
12287+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12288+ ino_t h_ino)
12289+{
12290+ int match;
12291+ struct au_drinfo *drinfo;
12292+
12293+ match = 1;
12294+ if (!lkup->dirren.drinfo)
12295+ goto out;
12296+ AuDebugOn(lkup->dirren.ninfo < bindex + 1);
12297+ drinfo = lkup->dirren.drinfo[bindex + 1];
12298+ if (!drinfo)
12299+ goto out;
12300+
12301+ match = (drinfo->ino == h_ino);
12302+ AuDbg("match %d\n", match);
12303+
12304+out:
12305+ return match;
12306+}
12307+
12308+/* ---------------------------------------------------------------------- */
12309+
12310+int au_dr_opt_set(struct super_block *sb)
12311+{
12312+ int err;
12313+ aufs_bindex_t bindex, bbot;
12314+ struct au_branch *br;
12315+
12316+ err = 0;
12317+ bbot = au_sbbot(sb);
12318+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12319+ br = au_sbr(sb, bindex);
12320+ err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12321+ }
12322+
12323+ return err;
12324+}
12325+
12326+int au_dr_opt_flush(struct super_block *sb)
12327+{
12328+ int err;
12329+ aufs_bindex_t bindex, bbot;
12330+ struct au_branch *br;
12331+
12332+ err = 0;
12333+ bbot = au_sbbot(sb);
12334+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12335+ br = au_sbr(sb, bindex);
12336+ if (au_br_writable(br->br_perm))
12337+ err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12338+ }
12339+
12340+ return err;
12341+}
12342+
12343+int au_dr_opt_clr(struct super_block *sb, int no_flush)
12344+{
12345+ int err;
12346+ aufs_bindex_t bindex, bbot;
12347+ struct au_branch *br;
12348+
12349+ err = 0;
12350+ if (!no_flush) {
12351+ err = au_dr_opt_flush(sb);
12352+ if (unlikely(err))
12353+ goto out;
12354+ }
12355+
12356+ bbot = au_sbbot(sb);
12357+ for (bindex = 0; bindex <= bbot; bindex++) {
12358+ br = au_sbr(sb, bindex);
12359+ au_dr_hino_free(&br->br_dirren);
12360+ }
12361+
12362+out:
12363+ return err;
12364+}
12365diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12366--- /usr/share/empty/fs/aufs/dirren.h 1970-01-01 01:00:00.000000000 +0100
ba1aed25 12367+++ linux/fs/aufs/dirren.h 2019-03-05 12:13:00.139224339 +0100
062440b3
AM
12368@@ -0,0 +1,140 @@
12369+/* SPDX-License-Identifier: GPL-2.0 */
8b6a4947 12370+/*
ba1aed25 12371+ * Copyright (C) 2017-2019 Junjiro R. Okajima
8b6a4947
AM
12372+ *
12373+ * This program, aufs is free software; you can redistribute it and/or modify
12374+ * it under the terms of the GNU General Public License as published by
12375+ * the Free Software Foundation; either version 2 of the License, or
12376+ * (at your option) any later version.
12377+ *
12378+ * This program is distributed in the hope that it will be useful,
12379+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12380+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12381+ * GNU General Public License for more details.
12382+ *
12383+ * You should have received a copy of the GNU General Public License
12384+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
12385+ */
12386+
12387+/*
12388+ * renamed dir info
12389+ */
12390+
12391+#ifndef __AUFS_DIRREN_H__
12392+#define __AUFS_DIRREN_H__
12393+
12394+#ifdef __KERNEL__
12395+
12396+#include <linux/dcache.h>
12397+#include <linux/statfs.h>
12398+#include <linux/uuid.h>
12399+#include "hbl.h"
12400+
12401+#define AuDirren_NHASH 100
12402+
12403+#ifdef CONFIG_AUFS_DIRREN
12404+enum au_brid_type {
12405+ AuBrid_Unset,
12406+ AuBrid_UUID,
12407+ AuBrid_FSID,
12408+ AuBrid_DEV
12409+};
12410+
12411+struct au_dr_brid {
12412+ enum au_brid_type type;
12413+ union {
12414+ uuid_t uuid; /* unimplemented yet */
12415+ fsid_t fsid;
12416+ dev_t dev;
12417+ };
12418+};
12419+
12420+/* 20 is the max digits length of ulong 64 */
12421+/* brid-type "_" uuid "_" inum */
12422+#define AUFS_DIRREN_FNAME_SZ (1 + 1 + UUID_STRING_LEN + 20)
12423+#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12424+
12425+struct au_dr_hino {
12426+ struct hlist_bl_node dr_hnode;
12427+ ino_t dr_h_ino;
12428+};
12429+
12430+struct au_dr_br {
12431+ struct hlist_bl_head dr_h_ino[AuDirren_NHASH];
12432+ struct au_dr_brid dr_brid;
12433+};
12434+
12435+struct au_dr_lookup {
12436+ /* dr_name is pointed by struct au_do_lookup_args.name */
12437+ struct qstr dr_name; /* subset of dr_info */
12438+ aufs_bindex_t ninfo;
12439+ struct au_drinfo **drinfo;
12440+};
12441+#else
12442+struct au_dr_hino;
12443+/* empty */
12444+struct au_dr_br { };
12445+struct au_dr_lookup { };
12446+#endif
12447+
12448+/* ---------------------------------------------------------------------- */
12449+
12450+struct au_branch;
12451+struct au_do_lookup_args;
12452+struct au_hinode;
12453+#ifdef CONFIG_AUFS_DIRREN
12454+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12455+ struct au_dr_hino *add_ent);
12456+void au_dr_hino_free(struct au_dr_br *dr);
12457+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12458+ const struct path *path);
12459+int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12460+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12461+ struct qstr *dst_name, void *_rev);
12462+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12463+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12464+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12465+ aufs_bindex_t bindex);
12466+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12467+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12468+ ino_t h_ino);
12469+void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12470+int au_dr_opt_set(struct super_block *sb);
12471+int au_dr_opt_flush(struct super_block *sb);
12472+int au_dr_opt_clr(struct super_block *sb, int no_flush);
12473+#else
12474+AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12475+ struct au_dr_hino *add_ent);
12476+AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12477+AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12478+ const struct path *path);
12479+AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12480+AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12481+ struct qstr *dst_name, void *_rev);
12482+AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12483+AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12484+ void *rev);
12485+AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12486+ aufs_bindex_t bindex);
12487+AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12488+AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12489+ aufs_bindex_t bindex, ino_t h_ino);
12490+AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12491+AuStubInt0(au_dr_opt_set, struct super_block *sb);
12492+AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12493+AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12494+#endif
12495+
12496+/* ---------------------------------------------------------------------- */
12497+
12498+#ifdef CONFIG_AUFS_DIRREN
12499+static inline int au_dr_ihash(ino_t h_ino)
12500+{
12501+ return h_ino % AuDirren_NHASH;
12502+}
12503+#else
12504+AuStubInt0(au_dr_ihash, ino_t h_ino);
12505+#endif
12506+
12507+#endif /* __KERNEL__ */
12508+#endif /* __AUFS_DIRREN_H__ */
7f207e10
AM
12509diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12510--- /usr/share/empty/fs/aufs/dynop.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 12511+++ linux/fs/aufs/dynop.c 2019-03-05 12:13:00.139224339 +0100
062440b3 12512@@ -0,0 +1,370 @@
cd7a4cd9 12513+// SPDX-License-Identifier: GPL-2.0
1facf9fc 12514+/*
ba1aed25 12515+ * Copyright (C) 2010-2019 Junjiro R. Okajima
1facf9fc 12516+ *
12517+ * This program, aufs is free software; you can redistribute it and/or modify
12518+ * it under the terms of the GNU General Public License as published by
12519+ * the Free Software Foundation; either version 2 of the License, or
12520+ * (at your option) any later version.
dece6358
AM
12521+ *
12522+ * This program is distributed in the hope that it will be useful,
12523+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12524+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12525+ * GNU General Public License for more details.
12526+ *
12527+ * You should have received a copy of the GNU General Public License
523b37e3 12528+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 12529+ */
12530+
12531+/*
4a4d8108 12532+ * dynamically customizable operations for regular files
1facf9fc 12533+ */
12534+
1facf9fc 12535+#include "aufs.h"
12536+
4a4d8108 12537+#define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
1facf9fc 12538+
4a4d8108
AM
12539+/*
12540+ * How large will these lists be?
12541+ * Usually just a few elements, 20-30 at most for each, I guess.
12542+ */
8b6a4947 12543+static struct hlist_bl_head dynop[AuDyLast];
4a4d8108 12544+
8b6a4947
AM
12545+static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12546+ const void *h_op)
1facf9fc 12547+{
4a4d8108 12548+ struct au_dykey *key, *tmp;
8b6a4947 12549+ struct hlist_bl_node *pos;
1facf9fc 12550+
4a4d8108 12551+ key = NULL;
8b6a4947
AM
12552+ hlist_bl_lock(hbl);
12553+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
4a4d8108
AM
12554+ if (tmp->dk_op.dy_hop == h_op) {
12555+ key = tmp;
12556+ kref_get(&key->dk_kref);
12557+ break;
12558+ }
8b6a4947 12559+ hlist_bl_unlock(hbl);
4a4d8108
AM
12560+
12561+ return key;
1facf9fc 12562+}
12563+
4a4d8108 12564+static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
1facf9fc 12565+{
4a4d8108
AM
12566+ struct au_dykey **k, *found;
12567+ const void *h_op = key->dk_op.dy_hop;
12568+ int i;
1facf9fc 12569+
4a4d8108
AM
12570+ found = NULL;
12571+ k = br->br_dykey;
12572+ for (i = 0; i < AuBrDynOp; i++)
12573+ if (k[i]) {
12574+ if (k[i]->dk_op.dy_hop == h_op) {
12575+ found = k[i];
12576+ break;
12577+ }
12578+ } else
12579+ break;
12580+ if (!found) {
12581+ spin_lock(&br->br_dykey_lock);
12582+ for (; i < AuBrDynOp; i++)
12583+ if (k[i]) {
12584+ if (k[i]->dk_op.dy_hop == h_op) {
12585+ found = k[i];
12586+ break;
12587+ }
12588+ } else {
12589+ k[i] = key;
12590+ break;
12591+ }
12592+ spin_unlock(&br->br_dykey_lock);
12593+ BUG_ON(i == AuBrDynOp); /* expand the array */
12594+ }
12595+
12596+ return found;
1facf9fc 12597+}
12598+
4a4d8108 12599+/* kref_get() if @key is already added */
8b6a4947 12600+static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
4a4d8108
AM
12601+{
12602+ struct au_dykey *tmp, *found;
8b6a4947 12603+ struct hlist_bl_node *pos;
4a4d8108 12604+ const void *h_op = key->dk_op.dy_hop;
1facf9fc 12605+
4a4d8108 12606+ found = NULL;
8b6a4947
AM
12607+ hlist_bl_lock(hbl);
12608+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
4a4d8108
AM
12609+ if (tmp->dk_op.dy_hop == h_op) {
12610+ kref_get(&tmp->dk_kref);
12611+ found = tmp;
12612+ break;
12613+ }
12614+ if (!found)
8b6a4947
AM
12615+ hlist_bl_add_head(&key->dk_hnode, hbl);
12616+ hlist_bl_unlock(hbl);
1facf9fc 12617+
4a4d8108
AM
12618+ if (!found)
12619+ DyPrSym(key);
12620+ return found;
12621+}
12622+
12623+static void dy_free_rcu(struct rcu_head *rcu)
1facf9fc 12624+{
4a4d8108
AM
12625+ struct au_dykey *key;
12626+
12627+ key = container_of(rcu, struct au_dykey, dk_rcu);
12628+ DyPrSym(key);
9f237c51 12629+ au_kfree_rcu(key);
1facf9fc 12630+}
12631+
4a4d8108
AM
12632+static void dy_free(struct kref *kref)
12633+{
12634+ struct au_dykey *key;
8b6a4947 12635+ struct hlist_bl_head *hbl;
1facf9fc 12636+
4a4d8108 12637+ key = container_of(kref, struct au_dykey, dk_kref);
8b6a4947
AM
12638+ hbl = dynop + key->dk_op.dy_type;
12639+ au_hbl_del(&key->dk_hnode, hbl);
4a4d8108
AM
12640+ call_rcu(&key->dk_rcu, dy_free_rcu);
12641+}
12642+
12643+void au_dy_put(struct au_dykey *key)
1facf9fc 12644+{
4a4d8108
AM
12645+ kref_put(&key->dk_kref, dy_free);
12646+}
1facf9fc 12647+
4a4d8108
AM
12648+/* ---------------------------------------------------------------------- */
12649+
12650+#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12651+
12652+#ifdef CONFIG_AUFS_DEBUG
12653+#define DyDbgDeclare(cnt) unsigned int cnt = 0
4f0767ce 12654+#define DyDbgInc(cnt) do { cnt++; } while (0)
4a4d8108
AM
12655+#else
12656+#define DyDbgDeclare(cnt) do {} while (0)
12657+#define DyDbgInc(cnt) do {} while (0)
12658+#endif
12659+
12660+#define DySet(func, dst, src, h_op, h_sb) do { \
12661+ DyDbgInc(cnt); \
12662+ if (h_op->func) { \
12663+ if (src.func) \
12664+ dst.func = src.func; \
12665+ else \
12666+ AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
12667+ } \
12668+} while (0)
12669+
12670+#define DySetForce(func, dst, src) do { \
12671+ AuDebugOn(!src.func); \
12672+ DyDbgInc(cnt); \
12673+ dst.func = src.func; \
12674+} while (0)
12675+
12676+#define DySetAop(func) \
12677+ DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12678+#define DySetAopForce(func) \
12679+ DySetForce(func, dyaop->da_op, aufs_aop)
12680+
12681+static void dy_aop(struct au_dykey *key, const void *h_op,
12682+ struct super_block *h_sb __maybe_unused)
12683+{
12684+ struct au_dyaop *dyaop = (void *)key;
12685+ const struct address_space_operations *h_aop = h_op;
12686+ DyDbgDeclare(cnt);
12687+
12688+ AuDbg("%s\n", au_sbtype(h_sb));
12689+
12690+ DySetAop(writepage);
12691+ DySetAopForce(readpage); /* force */
4a4d8108
AM
12692+ DySetAop(writepages);
12693+ DySetAop(set_page_dirty);
12694+ DySetAop(readpages);
12695+ DySetAop(write_begin);
12696+ DySetAop(write_end);
12697+ DySetAop(bmap);
12698+ DySetAop(invalidatepage);
12699+ DySetAop(releasepage);
027c5e7a 12700+ DySetAop(freepage);
7e9cd9fe 12701+ /* this one will be changed according to an aufs mount option */
4a4d8108 12702+ DySetAop(direct_IO);
4a4d8108 12703+ DySetAop(migratepage);
e2f27e51
AM
12704+ DySetAop(isolate_page);
12705+ DySetAop(putback_page);
4a4d8108
AM
12706+ DySetAop(launder_page);
12707+ DySetAop(is_partially_uptodate);
392086de 12708+ DySetAop(is_dirty_writeback);
4a4d8108 12709+ DySetAop(error_remove_page);
b4510431
AM
12710+ DySetAop(swap_activate);
12711+ DySetAop(swap_deactivate);
4a4d8108
AM
12712+
12713+ DyDbgSize(cnt, *h_aop);
4a4d8108
AM
12714+}
12715+
4a4d8108
AM
12716+/* ---------------------------------------------------------------------- */
12717+
12718+static void dy_bug(struct kref *kref)
12719+{
12720+ BUG();
12721+}
12722+
12723+static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12724+{
12725+ struct au_dykey *key, *old;
8b6a4947 12726+ struct hlist_bl_head *hbl;
b752ccd1 12727+ struct op {
4a4d8108 12728+ unsigned int sz;
b752ccd1
AM
12729+ void (*set)(struct au_dykey *key, const void *h_op,
12730+ struct super_block *h_sb __maybe_unused);
12731+ };
12732+ static const struct op a[] = {
4a4d8108
AM
12733+ [AuDy_AOP] = {
12734+ .sz = sizeof(struct au_dyaop),
b752ccd1 12735+ .set = dy_aop
4a4d8108 12736+ }
b752ccd1
AM
12737+ };
12738+ const struct op *p;
4a4d8108 12739+
8b6a4947
AM
12740+ hbl = dynop + op->dy_type;
12741+ key = dy_gfind_get(hbl, op->dy_hop);
4a4d8108
AM
12742+ if (key)
12743+ goto out_add; /* success */
12744+
12745+ p = a + op->dy_type;
12746+ key = kzalloc(p->sz, GFP_NOFS);
12747+ if (unlikely(!key)) {
12748+ key = ERR_PTR(-ENOMEM);
12749+ goto out;
12750+ }
12751+
12752+ key->dk_op.dy_hop = op->dy_hop;
12753+ kref_init(&key->dk_kref);
86dc4139 12754+ p->set(key, op->dy_hop, au_br_sb(br));
8b6a4947 12755+ old = dy_gadd(hbl, key);
4a4d8108 12756+ if (old) {
9f237c51 12757+ au_kfree_rcu(key);
4a4d8108
AM
12758+ key = old;
12759+ }
12760+
12761+out_add:
12762+ old = dy_bradd(br, key);
12763+ if (old)
12764+ /* its ref-count should never be zero here */
12765+ kref_put(&key->dk_kref, dy_bug);
12766+out:
12767+ return key;
12768+}
12769+
12770+/* ---------------------------------------------------------------------- */
12771+/*
acd2b654 12772+ * Aufs prohibits O_DIRECT by default even if the branch supports it.
c1595e42 12773+ * This behaviour is necessary to return an error from open(O_DIRECT) instead
4a4d8108
AM
12774+ * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12775+ * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12776+ * See the aufs manual in detail.
4a4d8108
AM
12777+ */
12778+static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12779+{
7e9cd9fe 12780+ if (!do_dx)
4a4d8108 12781+ dyaop->da_op.direct_IO = NULL;
7e9cd9fe 12782+ else
4a4d8108 12783+ dyaop->da_op.direct_IO = aufs_aop.direct_IO;
4a4d8108
AM
12784+}
12785+
12786+static struct au_dyaop *dy_aget(struct au_branch *br,
12787+ const struct address_space_operations *h_aop,
12788+ int do_dx)
12789+{
12790+ struct au_dyaop *dyaop;
12791+ struct au_dynop op;
12792+
12793+ op.dy_type = AuDy_AOP;
12794+ op.dy_haop = h_aop;
12795+ dyaop = (void *)dy_get(&op, br);
12796+ if (IS_ERR(dyaop))
12797+ goto out;
12798+ dy_adx(dyaop, do_dx);
12799+
12800+out:
12801+ return dyaop;
12802+}
12803+
12804+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12805+ struct inode *h_inode)
12806+{
12807+ int err, do_dx;
12808+ struct super_block *sb;
12809+ struct au_branch *br;
12810+ struct au_dyaop *dyaop;
12811+
12812+ AuDebugOn(!S_ISREG(h_inode->i_mode));
12813+ IiMustWriteLock(inode);
12814+
12815+ sb = inode->i_sb;
12816+ br = au_sbr(sb, bindex);
12817+ do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12818+ dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12819+ err = PTR_ERR(dyaop);
12820+ if (IS_ERR(dyaop))
12821+ /* unnecessary to call dy_fput() */
12822+ goto out;
12823+
12824+ err = 0;
12825+ inode->i_mapping->a_ops = &dyaop->da_op;
12826+
12827+out:
12828+ return err;
12829+}
12830+
b752ccd1
AM
12831+/*
12832+ * Is it safe to replace a_ops during the inode/file is in operation?
12833+ * Yes, I hope so.
12834+ */
12835+int au_dy_irefresh(struct inode *inode)
12836+{
12837+ int err;
5afbbe0d 12838+ aufs_bindex_t btop;
b752ccd1
AM
12839+ struct inode *h_inode;
12840+
12841+ err = 0;
12842+ if (S_ISREG(inode->i_mode)) {
5afbbe0d
AM
12843+ btop = au_ibtop(inode);
12844+ h_inode = au_h_iptr(inode, btop);
12845+ err = au_dy_iaop(inode, btop, h_inode);
b752ccd1
AM
12846+ }
12847+ return err;
12848+}
12849+
4a4d8108
AM
12850+void au_dy_arefresh(int do_dx)
12851+{
8b6a4947
AM
12852+ struct hlist_bl_head *hbl;
12853+ struct hlist_bl_node *pos;
4a4d8108
AM
12854+ struct au_dykey *key;
12855+
8b6a4947
AM
12856+ hbl = dynop + AuDy_AOP;
12857+ hlist_bl_lock(hbl);
12858+ hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
4a4d8108 12859+ dy_adx((void *)key, do_dx);
8b6a4947 12860+ hlist_bl_unlock(hbl);
4a4d8108
AM
12861+}
12862+
4a4d8108
AM
12863+/* ---------------------------------------------------------------------- */
12864+
12865+void __init au_dy_init(void)
12866+{
12867+ int i;
12868+
12869+ /* make sure that 'struct au_dykey *' can be any type */
12870+ BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
4a4d8108
AM
12871+
12872+ for (i = 0; i < AuDyLast; i++)
8b6a4947 12873+ INIT_HLIST_BL_HEAD(dynop + i);
4a4d8108
AM
12874+}
12875+
12876+void au_dy_fin(void)
12877+{
12878+ int i;
12879+
12880+ for (i = 0; i < AuDyLast; i++)
8b6a4947 12881+ WARN_ON(!hlist_bl_empty(dynop + i));
4a4d8108 12882+}
7f207e10
AM
12883diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12884--- /usr/share/empty/fs/aufs/dynop.h 1970-01-01 01:00:00.000000000 +0100
ba1aed25 12885+++ linux/fs/aufs/dynop.h 2019-03-05 12:13:00.139224339 +0100
062440b3
AM
12886@@ -0,0 +1,75 @@
12887+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 12888+/*
ba1aed25 12889+ * Copyright (C) 2010-2019 Junjiro R. Okajima
4a4d8108
AM
12890+ *
12891+ * This program, aufs is free software; you can redistribute it and/or modify
12892+ * it under the terms of the GNU General Public License as published by
12893+ * the Free Software Foundation; either version 2 of the License, or
12894+ * (at your option) any later version.
12895+ *
12896+ * This program is distributed in the hope that it will be useful,
12897+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12898+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12899+ * GNU General Public License for more details.
12900+ *
12901+ * You should have received a copy of the GNU General Public License
523b37e3 12902+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
12903+ */
12904+
12905+/*
12906+ * dynamically customizable operations (for regular files only)
12907+ */
12908+
12909+#ifndef __AUFS_DYNOP_H__
12910+#define __AUFS_DYNOP_H__
12911+
12912+#ifdef __KERNEL__
12913+
7e9cd9fe
AM
12914+#include <linux/fs.h>
12915+#include <linux/kref.h>
4a4d8108 12916+
2cbb1c4b 12917+enum {AuDy_AOP, AuDyLast};
4a4d8108
AM
12918+
12919+struct au_dynop {
12920+ int dy_type;
12921+ union {
12922+ const void *dy_hop;
12923+ const struct address_space_operations *dy_haop;
4a4d8108
AM
12924+ };
12925+};
12926+
12927+struct au_dykey {
12928+ union {
8b6a4947 12929+ struct hlist_bl_node dk_hnode;
4a4d8108
AM
12930+ struct rcu_head dk_rcu;
12931+ };
12932+ struct au_dynop dk_op;
12933+
12934+ /*
12935+ * during I am in the branch local array, kref is gotten. when the
12936+ * branch is removed, kref is put.
12937+ */
12938+ struct kref dk_kref;
12939+};
12940+
12941+/* stop unioning since their sizes are very different from each other */
12942+struct au_dyaop {
12943+ struct au_dykey da_key;
12944+ struct address_space_operations da_op; /* not const */
4a4d8108
AM
12945+};
12946+
4a4d8108
AM
12947+/* ---------------------------------------------------------------------- */
12948+
12949+/* dynop.c */
12950+struct au_branch;
12951+void au_dy_put(struct au_dykey *key);
12952+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12953+ struct inode *h_inode);
b752ccd1 12954+int au_dy_irefresh(struct inode *inode);
4a4d8108 12955+void au_dy_arefresh(int do_dio);
4a4d8108
AM
12956+
12957+void __init au_dy_init(void);
12958+void au_dy_fin(void);
12959+
4a4d8108
AM
12960+#endif /* __KERNEL__ */
12961+#endif /* __AUFS_DYNOP_H__ */
7f207e10
AM
12962diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12963--- /usr/share/empty/fs/aufs/export.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 12964+++ linux/fs/aufs/export.c 2019-03-05 12:13:00.139224339 +0100
062440b3 12965@@ -0,0 +1,838 @@
cd7a4cd9 12966+// SPDX-License-Identifier: GPL-2.0
4a4d8108 12967+/*
ba1aed25 12968+ * Copyright (C) 2005-2019 Junjiro R. Okajima
4a4d8108
AM
12969+ *
12970+ * This program, aufs is free software; you can redistribute it and/or modify
12971+ * it under the terms of the GNU General Public License as published by
12972+ * the Free Software Foundation; either version 2 of the License, or
12973+ * (at your option) any later version.
12974+ *
12975+ * This program is distributed in the hope that it will be useful,
12976+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12977+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12978+ * GNU General Public License for more details.
12979+ *
12980+ * You should have received a copy of the GNU General Public License
523b37e3 12981+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
12982+ */
12983+
12984+/*
12985+ * export via nfs
12986+ */
12987+
12988+#include <linux/exportfs.h>
7eafdf33 12989+#include <linux/fs_struct.h>
4a4d8108
AM
12990+#include <linux/namei.h>
12991+#include <linux/nsproxy.h>
12992+#include <linux/random.h>
12993+#include <linux/writeback.h>
12994+#include "aufs.h"
12995+
12996+union conv {
12997+#ifdef CONFIG_AUFS_INO_T_64
12998+ __u32 a[2];
12999+#else
13000+ __u32 a[1];
13001+#endif
13002+ ino_t ino;
13003+};
13004+
13005+static ino_t decode_ino(__u32 *a)
13006+{
13007+ union conv u;
13008+
13009+ BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
13010+ u.a[0] = a[0];
13011+#ifdef CONFIG_AUFS_INO_T_64
13012+ u.a[1] = a[1];
13013+#endif
13014+ return u.ino;
13015+}
13016+
13017+static void encode_ino(__u32 *a, ino_t ino)
13018+{
13019+ union conv u;
13020+
13021+ u.ino = ino;
13022+ a[0] = u.a[0];
13023+#ifdef CONFIG_AUFS_INO_T_64
13024+ a[1] = u.a[1];
13025+#endif
13026+}
13027+
13028+/* NFS file handle */
13029+enum {
13030+ Fh_br_id,
13031+ Fh_sigen,
13032+#ifdef CONFIG_AUFS_INO_T_64
13033+ /* support 64bit inode number */
13034+ Fh_ino1,
13035+ Fh_ino2,
13036+ Fh_dir_ino1,
13037+ Fh_dir_ino2,
13038+#else
13039+ Fh_ino1,
13040+ Fh_dir_ino1,
13041+#endif
13042+ Fh_igen,
13043+ Fh_h_type,
13044+ Fh_tail,
13045+
13046+ Fh_ino = Fh_ino1,
13047+ Fh_dir_ino = Fh_dir_ino1
13048+};
13049+
13050+static int au_test_anon(struct dentry *dentry)
13051+{
027c5e7a 13052+ /* note: read d_flags without d_lock */
4a4d8108
AM
13053+ return !!(dentry->d_flags & DCACHE_DISCONNECTED);
13054+}
13055+
a2a7ad62
AM
13056+int au_test_nfsd(void)
13057+{
13058+ int ret;
13059+ struct task_struct *tsk = current;
13060+ char comm[sizeof(tsk->comm)];
13061+
13062+ ret = 0;
13063+ if (tsk->flags & PF_KTHREAD) {
13064+ get_task_comm(comm, tsk);
13065+ ret = !strcmp(comm, "nfsd");
13066+ }
13067+
13068+ return ret;
13069+}
13070+
4a4d8108
AM
13071+/* ---------------------------------------------------------------------- */
13072+/* inode generation external table */
13073+
b752ccd1 13074+void au_xigen_inc(struct inode *inode)
4a4d8108 13075+{
4a4d8108
AM
13076+ loff_t pos;
13077+ ssize_t sz;
13078+ __u32 igen;
13079+ struct super_block *sb;
13080+ struct au_sbinfo *sbinfo;
13081+
4a4d8108 13082+ sb = inode->i_sb;
b752ccd1 13083+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
1facf9fc 13084+
b752ccd1 13085+ sbinfo = au_sbi(sb);
1facf9fc 13086+ pos = inode->i_ino;
13087+ pos *= sizeof(igen);
13088+ igen = inode->i_generation + 1;
1facf9fc 13089+ sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
13090+ sizeof(igen), &pos);
13091+ if (sz == sizeof(igen))
b752ccd1 13092+ return; /* success */
1facf9fc 13093+
b752ccd1 13094+ if (unlikely(sz >= 0))
1facf9fc 13095+ AuIOErr("xigen error (%zd)\n", sz);
1facf9fc 13096+}
13097+
13098+int au_xigen_new(struct inode *inode)
13099+{
13100+ int err;
13101+ loff_t pos;
13102+ ssize_t sz;
13103+ struct super_block *sb;
13104+ struct au_sbinfo *sbinfo;
13105+ struct file *file;
13106+
13107+ err = 0;
13108+ /* todo: dirty, at mount time */
13109+ if (inode->i_ino == AUFS_ROOT_INO)
13110+ goto out;
13111+ sb = inode->i_sb;
dece6358 13112+ SiMustAnyLock(sb);
1facf9fc 13113+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13114+ goto out;
13115+
13116+ err = -EFBIG;
13117+ pos = inode->i_ino;
13118+ if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
13119+ AuIOErr1("too large i%lld\n", pos);
13120+ goto out;
13121+ }
13122+ pos *= sizeof(inode->i_generation);
13123+
13124+ err = 0;
13125+ sbinfo = au_sbi(sb);
13126+ file = sbinfo->si_xigen;
13127+ BUG_ON(!file);
13128+
c06a8ce3 13129+ if (vfsub_f_size_read(file)
1facf9fc 13130+ < pos + sizeof(inode->i_generation)) {
13131+ inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
13132+ sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
13133+ sizeof(inode->i_generation), &pos);
13134+ } else
13135+ sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
13136+ sizeof(inode->i_generation), &pos);
13137+ if (sz == sizeof(inode->i_generation))
13138+ goto out; /* success */
13139+
13140+ err = sz;
13141+ if (unlikely(sz >= 0)) {
13142+ err = -EIO;
13143+ AuIOErr("xigen error (%zd)\n", sz);
13144+ }
13145+
4f0767ce 13146+out:
1facf9fc 13147+ return err;
13148+}
13149+
062440b3 13150+int au_xigen_set(struct super_block *sb, struct path *path)
1facf9fc 13151+{
13152+ int err;
13153+ struct au_sbinfo *sbinfo;
13154+ struct file *file;
13155+
dece6358
AM
13156+ SiMustWriteLock(sb);
13157+
1facf9fc 13158+ sbinfo = au_sbi(sb);
062440b3 13159+ file = au_xino_create2(sb, path, sbinfo->si_xigen);
1facf9fc 13160+ err = PTR_ERR(file);
13161+ if (IS_ERR(file))
13162+ goto out;
13163+ err = 0;
13164+ if (sbinfo->si_xigen)
13165+ fput(sbinfo->si_xigen);
13166+ sbinfo->si_xigen = file;
13167+
4f0767ce 13168+out:
062440b3 13169+ AuTraceErr(err);
1facf9fc 13170+ return err;
13171+}
13172+
13173+void au_xigen_clr(struct super_block *sb)
13174+{
13175+ struct au_sbinfo *sbinfo;
13176+
dece6358
AM
13177+ SiMustWriteLock(sb);
13178+
1facf9fc 13179+ sbinfo = au_sbi(sb);
13180+ if (sbinfo->si_xigen) {
13181+ fput(sbinfo->si_xigen);
13182+ sbinfo->si_xigen = NULL;
13183+ }
13184+}
13185+
13186+/* ---------------------------------------------------------------------- */
13187+
13188+static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13189+ ino_t dir_ino)
13190+{
13191+ struct dentry *dentry, *d;
13192+ struct inode *inode;
13193+ unsigned int sigen;
13194+
13195+ dentry = NULL;
13196+ inode = ilookup(sb, ino);
13197+ if (!inode)
13198+ goto out;
13199+
13200+ dentry = ERR_PTR(-ESTALE);
13201+ sigen = au_sigen(sb);
5afbbe0d 13202+ if (unlikely(au_is_bad_inode(inode)
1facf9fc 13203+ || IS_DEADDIR(inode)
537831f9 13204+ || sigen != au_iigen(inode, NULL)))
1facf9fc 13205+ goto out_iput;
13206+
13207+ dentry = NULL;
13208+ if (!dir_ino || S_ISDIR(inode->i_mode))
13209+ dentry = d_find_alias(inode);
13210+ else {
027c5e7a 13211+ spin_lock(&inode->i_lock);
c1595e42 13212+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 13213+ spin_lock(&d->d_lock);
1facf9fc 13214+ if (!au_test_anon(d)
5527c038 13215+ && d_inode(d->d_parent)->i_ino == dir_ino) {
027c5e7a
AM
13216+ dentry = dget_dlock(d);
13217+ spin_unlock(&d->d_lock);
1facf9fc 13218+ break;
13219+ }
027c5e7a
AM
13220+ spin_unlock(&d->d_lock);
13221+ }
13222+ spin_unlock(&inode->i_lock);
1facf9fc 13223+ }
027c5e7a 13224+ if (unlikely(dentry && au_digen_test(dentry, sigen))) {
2cbb1c4b 13225+ /* need to refresh */
1facf9fc 13226+ dput(dentry);
2cbb1c4b 13227+ dentry = NULL;
1facf9fc 13228+ }
13229+
4f0767ce 13230+out_iput:
1facf9fc 13231+ iput(inode);
4f0767ce 13232+out:
2cbb1c4b 13233+ AuTraceErrPtr(dentry);
1facf9fc 13234+ return dentry;
13235+}
13236+
13237+/* ---------------------------------------------------------------------- */
13238+
13239+/* todo: dirty? */
13240+/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
4a4d8108
AM
13241+
13242+struct au_compare_mnt_args {
13243+ /* input */
13244+ struct super_block *sb;
13245+
13246+ /* output */
13247+ struct vfsmount *mnt;
13248+};
13249+
13250+static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13251+{
13252+ struct au_compare_mnt_args *a = arg;
13253+
13254+ if (mnt->mnt_sb != a->sb)
13255+ return 0;
13256+ a->mnt = mntget(mnt);
13257+ return 1;
13258+}
13259+
1facf9fc 13260+static struct vfsmount *au_mnt_get(struct super_block *sb)
13261+{
4a4d8108 13262+ int err;
7eafdf33 13263+ struct path root;
4a4d8108
AM
13264+ struct au_compare_mnt_args args = {
13265+ .sb = sb
13266+ };
1facf9fc 13267+
7eafdf33 13268+ get_fs_root(current->fs, &root);
523b37e3 13269+ rcu_read_lock();
7eafdf33 13270+ err = iterate_mounts(au_compare_mnt, &args, root.mnt);
523b37e3 13271+ rcu_read_unlock();
7eafdf33 13272+ path_put(&root);
4a4d8108
AM
13273+ AuDebugOn(!err);
13274+ AuDebugOn(!args.mnt);
13275+ return args.mnt;
1facf9fc 13276+}
13277+
13278+struct au_nfsd_si_lock {
4a4d8108 13279+ unsigned int sigen;
027c5e7a 13280+ aufs_bindex_t bindex, br_id;
1facf9fc 13281+ unsigned char force_lock;
13282+};
13283+
027c5e7a
AM
13284+static int si_nfsd_read_lock(struct super_block *sb,
13285+ struct au_nfsd_si_lock *nsi_lock)
1facf9fc 13286+{
027c5e7a 13287+ int err;
1facf9fc 13288+ aufs_bindex_t bindex;
13289+
13290+ si_read_lock(sb, AuLock_FLUSH);
13291+
13292+ /* branch id may be wrapped around */
027c5e7a 13293+ err = 0;
1facf9fc 13294+ bindex = au_br_index(sb, nsi_lock->br_id);
13295+ if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13296+ goto out; /* success */
13297+
027c5e7a
AM
13298+ err = -ESTALE;
13299+ bindex = -1;
1facf9fc 13300+ if (!nsi_lock->force_lock)
13301+ si_read_unlock(sb);
1facf9fc 13302+
4f0767ce 13303+out:
027c5e7a
AM
13304+ nsi_lock->bindex = bindex;
13305+ return err;
1facf9fc 13306+}
13307+
13308+struct find_name_by_ino {
392086de 13309+ struct dir_context ctx;
1facf9fc 13310+ int called, found;
13311+ ino_t ino;
13312+ char *name;
13313+ int namelen;
13314+};
13315+
13316+static int
392086de
AM
13317+find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13318+ loff_t offset, u64 ino, unsigned int d_type)
1facf9fc 13319+{
392086de
AM
13320+ struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13321+ ctx);
1facf9fc 13322+
13323+ a->called++;
13324+ if (a->ino != ino)
13325+ return 0;
13326+
13327+ memcpy(a->name, name, namelen);
13328+ a->namelen = namelen;
13329+ a->found = 1;
13330+ return 1;
13331+}
13332+
13333+static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13334+ struct au_nfsd_si_lock *nsi_lock)
13335+{
13336+ struct dentry *dentry, *parent;
13337+ struct file *file;
13338+ struct inode *dir;
392086de
AM
13339+ struct find_name_by_ino arg = {
13340+ .ctx = {
2000de60 13341+ .actor = find_name_by_ino
392086de
AM
13342+ }
13343+ };
1facf9fc 13344+ int err;
13345+
13346+ parent = path->dentry;
13347+ if (nsi_lock)
13348+ si_read_unlock(parent->d_sb);
4a4d8108 13349+ file = vfsub_dentry_open(path, au_dir_roflags);
1facf9fc 13350+ dentry = (void *)file;
13351+ if (IS_ERR(file))
13352+ goto out;
13353+
13354+ dentry = ERR_PTR(-ENOMEM);
537831f9 13355+ arg.name = (void *)__get_free_page(GFP_NOFS);
1facf9fc 13356+ if (unlikely(!arg.name))
13357+ goto out_file;
13358+ arg.ino = ino;
13359+ arg.found = 0;
13360+ do {
13361+ arg.called = 0;
13362+ /* smp_mb(); */
392086de 13363+ err = vfsub_iterate_dir(file, &arg.ctx);
1facf9fc 13364+ } while (!err && !arg.found && arg.called);
13365+ dentry = ERR_PTR(err);
13366+ if (unlikely(err))
13367+ goto out_name;
1716fcea
AM
13368+ /* instead of ENOENT */
13369+ dentry = ERR_PTR(-ESTALE);
1facf9fc 13370+ if (!arg.found)
13371+ goto out_name;
13372+
b4510431 13373+ /* do not call vfsub_lkup_one() */
5527c038 13374+ dir = d_inode(parent);
febd17d6 13375+ dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
1facf9fc 13376+ AuTraceErrPtr(dentry);
13377+ if (IS_ERR(dentry))
13378+ goto out_name;
13379+ AuDebugOn(au_test_anon(dentry));
5527c038 13380+ if (unlikely(d_really_is_negative(dentry))) {
1facf9fc 13381+ dput(dentry);
13382+ dentry = ERR_PTR(-ENOENT);
13383+ }
13384+
4f0767ce 13385+out_name:
1c60b727 13386+ free_page((unsigned long)arg.name);
4f0767ce 13387+out_file:
1facf9fc 13388+ fput(file);
4f0767ce 13389+out:
1facf9fc 13390+ if (unlikely(nsi_lock
13391+ && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13392+ if (!IS_ERR(dentry)) {
13393+ dput(dentry);
13394+ dentry = ERR_PTR(-ESTALE);
13395+ }
13396+ AuTraceErrPtr(dentry);
13397+ return dentry;
13398+}
13399+
13400+static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13401+ ino_t dir_ino,
13402+ struct au_nfsd_si_lock *nsi_lock)
13403+{
13404+ struct dentry *dentry;
13405+ struct path path;
13406+
13407+ if (dir_ino != AUFS_ROOT_INO) {
13408+ path.dentry = decode_by_ino(sb, dir_ino, 0);
13409+ dentry = path.dentry;
13410+ if (!path.dentry || IS_ERR(path.dentry))
13411+ goto out;
13412+ AuDebugOn(au_test_anon(path.dentry));
13413+ } else
13414+ path.dentry = dget(sb->s_root);
13415+
13416+ path.mnt = au_mnt_get(sb);
13417+ dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13418+ path_put(&path);
13419+
4f0767ce 13420+out:
1facf9fc 13421+ AuTraceErrPtr(dentry);
13422+ return dentry;
13423+}
13424+
13425+/* ---------------------------------------------------------------------- */
13426+
13427+static int h_acceptable(void *expv, struct dentry *dentry)
13428+{
13429+ return 1;
13430+}
13431+
13432+static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13433+ char *buf, int len, struct super_block *sb)
13434+{
13435+ char *p;
13436+ int n;
13437+ struct path path;
13438+
13439+ p = d_path(h_rootpath, buf, len);
13440+ if (IS_ERR(p))
13441+ goto out;
13442+ n = strlen(p);
13443+
13444+ path.mnt = h_rootpath->mnt;
13445+ path.dentry = h_parent;
13446+ p = d_path(&path, buf, len);
13447+ if (IS_ERR(p))
13448+ goto out;
13449+ if (n != 1)
13450+ p += n;
13451+
13452+ path.mnt = au_mnt_get(sb);
13453+ path.dentry = sb->s_root;
13454+ p = d_path(&path, buf, len - strlen(p));
13455+ mntput(path.mnt);
13456+ if (IS_ERR(p))
13457+ goto out;
13458+ if (n != 1)
13459+ p[strlen(p)] = '/';
13460+
4f0767ce 13461+out:
1facf9fc 13462+ AuTraceErrPtr(p);
13463+ return p;
13464+}
13465+
13466+static
027c5e7a
AM
13467+struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13468+ int fh_len, struct au_nfsd_si_lock *nsi_lock)
1facf9fc 13469+{
13470+ struct dentry *dentry, *h_parent, *root;
13471+ struct super_block *h_sb;
13472+ char *pathname, *p;
13473+ struct vfsmount *h_mnt;
13474+ struct au_branch *br;
13475+ int err;
13476+ struct path path;
13477+
027c5e7a 13478+ br = au_sbr(sb, nsi_lock->bindex);
86dc4139 13479+ h_mnt = au_br_mnt(br);
1facf9fc 13480+ h_sb = h_mnt->mnt_sb;
13481+ /* todo: call lower fh_to_dentry()? fh_to_parent()? */
5afbbe0d 13482+ lockdep_off();
1facf9fc 13483+ h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13484+ fh_len - Fh_tail, fh[Fh_h_type],
13485+ h_acceptable, /*context*/NULL);
5afbbe0d 13486+ lockdep_on();
1facf9fc 13487+ dentry = h_parent;
13488+ if (unlikely(!h_parent || IS_ERR(h_parent))) {
13489+ AuWarn1("%s decode_fh failed, %ld\n",
13490+ au_sbtype(h_sb), PTR_ERR(h_parent));
13491+ goto out;
13492+ }
13493+ dentry = NULL;
13494+ if (unlikely(au_test_anon(h_parent))) {
13495+ AuWarn1("%s decode_fh returned a disconnected dentry\n",
13496+ au_sbtype(h_sb));
13497+ goto out_h_parent;
13498+ }
13499+
13500+ dentry = ERR_PTR(-ENOMEM);
13501+ pathname = (void *)__get_free_page(GFP_NOFS);
13502+ if (unlikely(!pathname))
13503+ goto out_h_parent;
13504+
13505+ root = sb->s_root;
13506+ path.mnt = h_mnt;
13507+ di_read_lock_parent(root, !AuLock_IR);
027c5e7a 13508+ path.dentry = au_h_dptr(root, nsi_lock->bindex);
1facf9fc 13509+ di_read_unlock(root, !AuLock_IR);
13510+ p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13511+ dentry = (void *)p;
13512+ if (IS_ERR(p))
13513+ goto out_pathname;
13514+
13515+ si_read_unlock(sb);
13516+ err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13517+ dentry = ERR_PTR(err);
13518+ if (unlikely(err))
13519+ goto out_relock;
13520+
13521+ dentry = ERR_PTR(-ENOENT);
13522+ AuDebugOn(au_test_anon(path.dentry));
5527c038 13523+ if (unlikely(d_really_is_negative(path.dentry)))
1facf9fc 13524+ goto out_path;
13525+
5527c038 13526+ if (ino != d_inode(path.dentry)->i_ino)
1facf9fc 13527+ dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13528+ else
13529+ dentry = dget(path.dentry);
13530+
4f0767ce 13531+out_path:
1facf9fc 13532+ path_put(&path);
4f0767ce 13533+out_relock:
1facf9fc 13534+ if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13535+ if (!IS_ERR(dentry)) {
13536+ dput(dentry);
13537+ dentry = ERR_PTR(-ESTALE);
13538+ }
4f0767ce 13539+out_pathname:
1c60b727 13540+ free_page((unsigned long)pathname);
4f0767ce 13541+out_h_parent:
1facf9fc 13542+ dput(h_parent);
4f0767ce 13543+out:
1facf9fc 13544+ AuTraceErrPtr(dentry);
13545+ return dentry;
13546+}
13547+
13548+/* ---------------------------------------------------------------------- */
13549+
13550+static struct dentry *
13551+aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13552+ int fh_type)
13553+{
13554+ struct dentry *dentry;
13555+ __u32 *fh = fid->raw;
027c5e7a 13556+ struct au_branch *br;
1facf9fc 13557+ ino_t ino, dir_ino;
1facf9fc 13558+ struct au_nfsd_si_lock nsi_lock = {
1facf9fc 13559+ .force_lock = 0
13560+ };
13561+
1facf9fc 13562+ dentry = ERR_PTR(-ESTALE);
4a4d8108
AM
13563+ /* it should never happen, but the file handle is unreliable */
13564+ if (unlikely(fh_len < Fh_tail))
13565+ goto out;
13566+ nsi_lock.sigen = fh[Fh_sigen];
13567+ nsi_lock.br_id = fh[Fh_br_id];
13568+
1facf9fc 13569+ /* branch id may be wrapped around */
027c5e7a
AM
13570+ br = NULL;
13571+ if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
1facf9fc 13572+ goto out;
13573+ nsi_lock.force_lock = 1;
13574+
13575+ /* is this inode still cached? */
13576+ ino = decode_ino(fh + Fh_ino);
4a4d8108
AM
13577+ /* it should never happen */
13578+ if (unlikely(ino == AUFS_ROOT_INO))
8cdd5066 13579+ goto out_unlock;
4a4d8108 13580+
1facf9fc 13581+ dir_ino = decode_ino(fh + Fh_dir_ino);
13582+ dentry = decode_by_ino(sb, ino, dir_ino);
13583+ if (IS_ERR(dentry))
13584+ goto out_unlock;
13585+ if (dentry)
13586+ goto accept;
13587+
13588+ /* is the parent dir cached? */
027c5e7a 13589+ br = au_sbr(sb, nsi_lock.bindex);
acd2b654 13590+ au_lcnt_inc(&br->br_nfiles);
1facf9fc 13591+ dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13592+ if (IS_ERR(dentry))
13593+ goto out_unlock;
13594+ if (dentry)
13595+ goto accept;
13596+
13597+ /* lookup path */
027c5e7a 13598+ dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
1facf9fc 13599+ if (IS_ERR(dentry))
13600+ goto out_unlock;
13601+ if (unlikely(!dentry))
13602+ /* todo?: make it ESTALE */
13603+ goto out_unlock;
13604+
4f0767ce 13605+accept:
027c5e7a 13606+ if (!au_digen_test(dentry, au_sigen(sb))
5527c038 13607+ && d_inode(dentry)->i_generation == fh[Fh_igen])
1facf9fc 13608+ goto out_unlock; /* success */
13609+
13610+ dput(dentry);
13611+ dentry = ERR_PTR(-ESTALE);
4f0767ce 13612+out_unlock:
027c5e7a 13613+ if (br)
acd2b654 13614+ au_lcnt_dec(&br->br_nfiles);
1facf9fc 13615+ si_read_unlock(sb);
4f0767ce 13616+out:
1facf9fc 13617+ AuTraceErrPtr(dentry);
13618+ return dentry;
13619+}
13620+
13621+#if 0 /* reserved for future use */
13622+/* support subtreecheck option */
13623+static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13624+ int fh_len, int fh_type)
13625+{
13626+ struct dentry *parent;
13627+ __u32 *fh = fid->raw;
13628+ ino_t dir_ino;
13629+
13630+ dir_ino = decode_ino(fh + Fh_dir_ino);
13631+ parent = decode_by_ino(sb, dir_ino, 0);
13632+ if (IS_ERR(parent))
13633+ goto out;
13634+ if (!parent)
13635+ parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13636+ dir_ino, fh, fh_len);
13637+
4f0767ce 13638+out:
1facf9fc 13639+ AuTraceErrPtr(parent);
13640+ return parent;
13641+}
13642+#endif
13643+
13644+/* ---------------------------------------------------------------------- */
13645+
0c3ec466
AM
13646+static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13647+ struct inode *dir)
1facf9fc 13648+{
13649+ int err;
0c3ec466 13650+ aufs_bindex_t bindex;
1facf9fc 13651+ struct super_block *sb, *h_sb;
0c3ec466
AM
13652+ struct dentry *dentry, *parent, *h_parent;
13653+ struct inode *h_dir;
1facf9fc 13654+ struct au_branch *br;
13655+
1facf9fc 13656+ err = -ENOSPC;
13657+ if (unlikely(*max_len <= Fh_tail)) {
13658+ AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13659+ goto out;
13660+ }
13661+
13662+ err = FILEID_ROOT;
0c3ec466
AM
13663+ if (inode->i_ino == AUFS_ROOT_INO) {
13664+ AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
1facf9fc 13665+ goto out;
13666+ }
13667+
1facf9fc 13668+ h_parent = NULL;
0c3ec466
AM
13669+ sb = inode->i_sb;
13670+ err = si_read_lock(sb, AuLock_FLUSH);
027c5e7a
AM
13671+ if (unlikely(err))
13672+ goto out;
13673+
1facf9fc 13674+#ifdef CONFIG_AUFS_DEBUG
13675+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13676+ AuWarn1("NFS-exporting requires xino\n");
13677+#endif
027c5e7a 13678+ err = -EIO;
0c3ec466
AM
13679+ parent = NULL;
13680+ ii_read_lock_child(inode);
5afbbe0d 13681+ bindex = au_ibtop(inode);
0c3ec466 13682+ if (!dir) {
c1595e42 13683+ dentry = d_find_any_alias(inode);
0c3ec466
AM
13684+ if (unlikely(!dentry))
13685+ goto out_unlock;
13686+ AuDebugOn(au_test_anon(dentry));
13687+ parent = dget_parent(dentry);
13688+ dput(dentry);
13689+ if (unlikely(!parent))
13690+ goto out_unlock;
5527c038
JR
13691+ if (d_really_is_positive(parent))
13692+ dir = d_inode(parent);
1facf9fc 13693+ }
0c3ec466
AM
13694+
13695+ ii_read_lock_parent(dir);
13696+ h_dir = au_h_iptr(dir, bindex);
13697+ ii_read_unlock(dir);
13698+ if (unlikely(!h_dir))
13699+ goto out_parent;
c1595e42 13700+ h_parent = d_find_any_alias(h_dir);
1facf9fc 13701+ if (unlikely(!h_parent))
0c3ec466 13702+ goto out_hparent;
1facf9fc 13703+
13704+ err = -EPERM;
13705+ br = au_sbr(sb, bindex);
86dc4139 13706+ h_sb = au_br_sb(br);
1facf9fc 13707+ if (unlikely(!h_sb->s_export_op)) {
13708+ AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
0c3ec466 13709+ goto out_hparent;
1facf9fc 13710+ }
13711+
13712+ fh[Fh_br_id] = br->br_id;
13713+ fh[Fh_sigen] = au_sigen(sb);
13714+ encode_ino(fh + Fh_ino, inode->i_ino);
0c3ec466 13715+ encode_ino(fh + Fh_dir_ino, dir->i_ino);
1facf9fc 13716+ fh[Fh_igen] = inode->i_generation;
13717+
13718+ *max_len -= Fh_tail;
13719+ fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13720+ max_len,
13721+ /*connectable or subtreecheck*/0);
13722+ err = fh[Fh_h_type];
13723+ *max_len += Fh_tail;
13724+ /* todo: macros? */
1716fcea 13725+ if (err != FILEID_INVALID)
1facf9fc 13726+ err = 99;
13727+ else
13728+ AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13729+
0c3ec466 13730+out_hparent:
1facf9fc 13731+ dput(h_parent);
0c3ec466 13732+out_parent:
1facf9fc 13733+ dput(parent);
0c3ec466
AM
13734+out_unlock:
13735+ ii_read_unlock(inode);
13736+ si_read_unlock(sb);
4f0767ce 13737+out:
1facf9fc 13738+ if (unlikely(err < 0))
1716fcea 13739+ err = FILEID_INVALID;
1facf9fc 13740+ return err;
13741+}
13742+
13743+/* ---------------------------------------------------------------------- */
13744+
4a4d8108
AM
13745+static int aufs_commit_metadata(struct inode *inode)
13746+{
13747+ int err;
13748+ aufs_bindex_t bindex;
13749+ struct super_block *sb;
13750+ struct inode *h_inode;
13751+ int (*f)(struct inode *inode);
13752+
13753+ sb = inode->i_sb;
e49829fe 13754+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 13755+ ii_write_lock_child(inode);
5afbbe0d 13756+ bindex = au_ibtop(inode);
4a4d8108
AM
13757+ AuDebugOn(bindex < 0);
13758+ h_inode = au_h_iptr(inode, bindex);
13759+
13760+ f = h_inode->i_sb->s_export_op->commit_metadata;
13761+ if (f)
13762+ err = f(h_inode);
13763+ else {
13764+ struct writeback_control wbc = {
13765+ .sync_mode = WB_SYNC_ALL,
13766+ .nr_to_write = 0 /* metadata only */
13767+ };
13768+
13769+ err = sync_inode(h_inode, &wbc);
13770+ }
13771+
13772+ au_cpup_attr_timesizes(inode);
13773+ ii_write_unlock(inode);
13774+ si_read_unlock(sb);
13775+ return err;
13776+}
13777+
13778+/* ---------------------------------------------------------------------- */
13779+
1facf9fc 13780+static struct export_operations aufs_export_op = {
4a4d8108 13781+ .fh_to_dentry = aufs_fh_to_dentry,
1facf9fc 13782+ /* .fh_to_parent = aufs_fh_to_parent, */
4a4d8108
AM
13783+ .encode_fh = aufs_encode_fh,
13784+ .commit_metadata = aufs_commit_metadata
1facf9fc 13785+};
13786+
13787+void au_export_init(struct super_block *sb)
13788+{
13789+ struct au_sbinfo *sbinfo;
13790+ __u32 u;
13791+
5afbbe0d
AM
13792+ BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13793+ && IS_MODULE(CONFIG_EXPORTFS),
13794+ AUFS_NAME ": unsupported configuration "
13795+ "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13796+
1facf9fc 13797+ sb->s_export_op = &aufs_export_op;
13798+ sbinfo = au_sbi(sb);
13799+ sbinfo->si_xigen = NULL;
13800+ get_random_bytes(&u, sizeof(u));
13801+ BUILD_BUG_ON(sizeof(u) != sizeof(int));
13802+ atomic_set(&sbinfo->si_xigen_next, u);
13803+}
076b876e
AM
13804diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13805--- /usr/share/empty/fs/aufs/fhsm.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 13806+++ linux/fs/aufs/fhsm.c 2019-03-05 12:13:00.139224339 +0100
062440b3 13807@@ -0,0 +1,427 @@
cd7a4cd9 13808+// SPDX-License-Identifier: GPL-2.0
076b876e 13809+/*
ba1aed25 13810+ * Copyright (C) 2011-2019 Junjiro R. Okajima
076b876e
AM
13811+ *
13812+ * This program, aufs is free software; you can redistribute it and/or modify
13813+ * it under the terms of the GNU General Public License as published by
13814+ * the Free Software Foundation; either version 2 of the License, or
13815+ * (at your option) any later version.
13816+ *
13817+ * This program is distributed in the hope that it will be useful,
13818+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13819+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13820+ * GNU General Public License for more details.
13821+ *
13822+ * You should have received a copy of the GNU General Public License
13823+ * along with this program; if not, write to the Free Software
13824+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
13825+ */
13826+
13827+/*
13828+ * File-based Hierarchy Storage Management
13829+ */
13830+
13831+#include <linux/anon_inodes.h>
13832+#include <linux/poll.h>
13833+#include <linux/seq_file.h>
13834+#include <linux/statfs.h>
13835+#include "aufs.h"
13836+
c1595e42
JR
13837+static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13838+{
13839+ struct au_sbinfo *sbinfo;
13840+ struct au_fhsm *fhsm;
13841+
13842+ SiMustAnyLock(sb);
13843+
13844+ sbinfo = au_sbi(sb);
13845+ fhsm = &sbinfo->si_fhsm;
13846+ AuDebugOn(!fhsm);
13847+ return fhsm->fhsm_bottom;
13848+}
13849+
13850+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13851+{
13852+ struct au_sbinfo *sbinfo;
13853+ struct au_fhsm *fhsm;
13854+
13855+ SiMustWriteLock(sb);
13856+
13857+ sbinfo = au_sbi(sb);
13858+ fhsm = &sbinfo->si_fhsm;
13859+ AuDebugOn(!fhsm);
13860+ fhsm->fhsm_bottom = bindex;
13861+}
13862+
13863+/* ---------------------------------------------------------------------- */
13864+
076b876e
AM
13865+static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13866+{
13867+ struct au_br_fhsm *bf;
13868+
13869+ bf = br->br_fhsm;
13870+ MtxMustLock(&bf->bf_lock);
13871+
13872+ return !bf->bf_readable
13873+ || time_after(jiffies,
13874+ bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13875+}
13876+
13877+/* ---------------------------------------------------------------------- */
13878+
13879+static void au_fhsm_notify(struct super_block *sb, int val)
13880+{
13881+ struct au_sbinfo *sbinfo;
13882+ struct au_fhsm *fhsm;
13883+
13884+ SiMustAnyLock(sb);
13885+
13886+ sbinfo = au_sbi(sb);
13887+ fhsm = &sbinfo->si_fhsm;
13888+ if (au_fhsm_pid(fhsm)
13889+ && atomic_read(&fhsm->fhsm_readable) != -1) {
13890+ atomic_set(&fhsm->fhsm_readable, val);
13891+ if (val)
13892+ wake_up(&fhsm->fhsm_wqh);
13893+ }
13894+}
13895+
13896+static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13897+ struct aufs_stfs *rstfs, int do_lock, int do_notify)
13898+{
13899+ int err;
13900+ struct au_branch *br;
13901+ struct au_br_fhsm *bf;
13902+
13903+ br = au_sbr(sb, bindex);
13904+ AuDebugOn(au_br_rdonly(br));
13905+ bf = br->br_fhsm;
13906+ AuDebugOn(!bf);
13907+
13908+ if (do_lock)
13909+ mutex_lock(&bf->bf_lock);
13910+ else
13911+ MtxMustLock(&bf->bf_lock);
13912+
13913+ /* sb->s_root for NFS is unreliable */
13914+ err = au_br_stfs(br, &bf->bf_stfs);
13915+ if (unlikely(err)) {
13916+ AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13917+ goto out;
13918+ }
13919+
13920+ bf->bf_jiffy = jiffies;
13921+ bf->bf_readable = 1;
13922+ if (do_notify)
13923+ au_fhsm_notify(sb, /*val*/1);
13924+ if (rstfs)
13925+ *rstfs = bf->bf_stfs;
13926+
13927+out:
13928+ if (do_lock)
13929+ mutex_unlock(&bf->bf_lock);
13930+ au_fhsm_notify(sb, /*val*/1);
13931+
13932+ return err;
13933+}
13934+
13935+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13936+{
13937+ int err;
076b876e
AM
13938+ struct au_sbinfo *sbinfo;
13939+ struct au_fhsm *fhsm;
13940+ struct au_branch *br;
13941+ struct au_br_fhsm *bf;
13942+
13943+ AuDbg("b%d, force %d\n", bindex, force);
13944+ SiMustAnyLock(sb);
13945+
13946+ sbinfo = au_sbi(sb);
13947+ fhsm = &sbinfo->si_fhsm;
c1595e42
JR
13948+ if (!au_ftest_si(sbinfo, FHSM)
13949+ || fhsm->fhsm_bottom == bindex)
076b876e
AM
13950+ return;
13951+
13952+ br = au_sbr(sb, bindex);
13953+ bf = br->br_fhsm;
13954+ AuDebugOn(!bf);
13955+ mutex_lock(&bf->bf_lock);
13956+ if (force
13957+ || au_fhsm_pid(fhsm)
13958+ || au_fhsm_test_jiffy(sbinfo, br))
13959+ err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13960+ /*do_notify*/1);
13961+ mutex_unlock(&bf->bf_lock);
13962+}
13963+
13964+void au_fhsm_wrote_all(struct super_block *sb, int force)
13965+{
5afbbe0d 13966+ aufs_bindex_t bindex, bbot;
076b876e
AM
13967+ struct au_branch *br;
13968+
13969+ /* exclude the bottom */
5afbbe0d
AM
13970+ bbot = au_fhsm_bottom(sb);
13971+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
13972+ br = au_sbr(sb, bindex);
13973+ if (au_br_fhsm(br->br_perm))
13974+ au_fhsm_wrote(sb, bindex, force);
13975+ }
13976+}
13977+
13978+/* ---------------------------------------------------------------------- */
13979+
be118d29 13980+static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
076b876e 13981+{
be118d29 13982+ __poll_t mask;
076b876e
AM
13983+ struct au_sbinfo *sbinfo;
13984+ struct au_fhsm *fhsm;
13985+
13986+ mask = 0;
13987+ sbinfo = file->private_data;
13988+ fhsm = &sbinfo->si_fhsm;
13989+ poll_wait(file, &fhsm->fhsm_wqh, wait);
13990+ if (atomic_read(&fhsm->fhsm_readable))
be118d29 13991+ mask = EPOLLIN /* | EPOLLRDNORM */;
076b876e 13992+
b00004a5
AM
13993+ if (!mask)
13994+ AuDbg("mask 0x%x\n", mask);
076b876e
AM
13995+ return mask;
13996+}
13997+
13998+static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13999+ struct aufs_stfs *stfs, __s16 brid)
14000+{
14001+ int err;
14002+
14003+ err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
14004+ if (!err)
14005+ err = __put_user(brid, &stbr->brid);
14006+ if (unlikely(err))
14007+ err = -EFAULT;
14008+
14009+ return err;
14010+}
14011+
14012+static ssize_t au_fhsm_do_read(struct super_block *sb,
14013+ struct aufs_stbr __user *stbr, size_t count)
14014+{
14015+ ssize_t err;
14016+ int nstbr;
5afbbe0d 14017+ aufs_bindex_t bindex, bbot;
076b876e
AM
14018+ struct au_branch *br;
14019+ struct au_br_fhsm *bf;
14020+
14021+ /* except the bottom branch */
14022+ err = 0;
14023+ nstbr = 0;
5afbbe0d
AM
14024+ bbot = au_fhsm_bottom(sb);
14025+ for (bindex = 0; !err && bindex < bbot; bindex++) {
076b876e
AM
14026+ br = au_sbr(sb, bindex);
14027+ if (!au_br_fhsm(br->br_perm))
14028+ continue;
14029+
14030+ bf = br->br_fhsm;
14031+ mutex_lock(&bf->bf_lock);
14032+ if (bf->bf_readable) {
14033+ err = -EFAULT;
14034+ if (count >= sizeof(*stbr))
14035+ err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
14036+ br->br_id);
14037+ if (!err) {
14038+ bf->bf_readable = 0;
14039+ count -= sizeof(*stbr);
14040+ nstbr++;
14041+ }
14042+ }
14043+ mutex_unlock(&bf->bf_lock);
14044+ }
14045+ if (!err)
14046+ err = sizeof(*stbr) * nstbr;
14047+
14048+ return err;
14049+}
14050+
14051+static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
14052+ loff_t *pos)
14053+{
14054+ ssize_t err;
14055+ int readable;
5afbbe0d 14056+ aufs_bindex_t nfhsm, bindex, bbot;
076b876e
AM
14057+ struct au_sbinfo *sbinfo;
14058+ struct au_fhsm *fhsm;
14059+ struct au_branch *br;
14060+ struct super_block *sb;
14061+
14062+ err = 0;
14063+ sbinfo = file->private_data;
14064+ fhsm = &sbinfo->si_fhsm;
14065+need_data:
14066+ spin_lock_irq(&fhsm->fhsm_wqh.lock);
14067+ if (!atomic_read(&fhsm->fhsm_readable)) {
14068+ if (vfsub_file_flags(file) & O_NONBLOCK)
14069+ err = -EAGAIN;
14070+ else
14071+ err = wait_event_interruptible_locked_irq
14072+ (fhsm->fhsm_wqh,
14073+ atomic_read(&fhsm->fhsm_readable));
14074+ }
14075+ spin_unlock_irq(&fhsm->fhsm_wqh.lock);
14076+ if (unlikely(err))
14077+ goto out;
14078+
14079+ /* sb may already be dead */
14080+ au_rw_read_lock(&sbinfo->si_rwsem);
14081+ readable = atomic_read(&fhsm->fhsm_readable);
14082+ if (readable > 0) {
14083+ sb = sbinfo->si_sb;
14084+ AuDebugOn(!sb);
14085+ /* exclude the bottom branch */
14086+ nfhsm = 0;
5afbbe0d
AM
14087+ bbot = au_fhsm_bottom(sb);
14088+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
14089+ br = au_sbr(sb, bindex);
14090+ if (au_br_fhsm(br->br_perm))
14091+ nfhsm++;
14092+ }
14093+ err = -EMSGSIZE;
14094+ if (nfhsm * sizeof(struct aufs_stbr) <= count) {
14095+ atomic_set(&fhsm->fhsm_readable, 0);
14096+ err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
14097+ count);
14098+ }
14099+ }
14100+ au_rw_read_unlock(&sbinfo->si_rwsem);
14101+ if (!readable)
14102+ goto need_data;
14103+
14104+out:
14105+ return err;
14106+}
14107+
14108+static int au_fhsm_release(struct inode *inode, struct file *file)
14109+{
14110+ struct au_sbinfo *sbinfo;
14111+ struct au_fhsm *fhsm;
14112+
14113+ /* sb may already be dead */
14114+ sbinfo = file->private_data;
14115+ fhsm = &sbinfo->si_fhsm;
14116+ spin_lock(&fhsm->fhsm_spin);
14117+ fhsm->fhsm_pid = 0;
14118+ spin_unlock(&fhsm->fhsm_spin);
14119+ kobject_put(&sbinfo->si_kobj);
14120+
14121+ return 0;
14122+}
14123+
14124+static const struct file_operations au_fhsm_fops = {
14125+ .owner = THIS_MODULE,
14126+ .llseek = noop_llseek,
14127+ .read = au_fhsm_read,
14128+ .poll = au_fhsm_poll,
14129+ .release = au_fhsm_release
14130+};
14131+
14132+int au_fhsm_fd(struct super_block *sb, int oflags)
14133+{
14134+ int err, fd;
14135+ struct au_sbinfo *sbinfo;
14136+ struct au_fhsm *fhsm;
14137+
14138+ err = -EPERM;
14139+ if (unlikely(!capable(CAP_SYS_ADMIN)))
14140+ goto out;
14141+
14142+ err = -EINVAL;
14143+ if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
14144+ goto out;
14145+
14146+ err = 0;
14147+ sbinfo = au_sbi(sb);
14148+ fhsm = &sbinfo->si_fhsm;
14149+ spin_lock(&fhsm->fhsm_spin);
14150+ if (!fhsm->fhsm_pid)
14151+ fhsm->fhsm_pid = current->pid;
14152+ else
14153+ err = -EBUSY;
14154+ spin_unlock(&fhsm->fhsm_spin);
14155+ if (unlikely(err))
14156+ goto out;
14157+
14158+ oflags |= O_RDONLY;
14159+ /* oflags |= FMODE_NONOTIFY; */
14160+ fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
14161+ err = fd;
14162+ if (unlikely(fd < 0))
14163+ goto out_pid;
14164+
acd2b654 14165+ /* succeed regardless 'fhsm' status */
076b876e
AM
14166+ kobject_get(&sbinfo->si_kobj);
14167+ si_noflush_read_lock(sb);
14168+ if (au_ftest_si(sbinfo, FHSM))
14169+ au_fhsm_wrote_all(sb, /*force*/0);
14170+ si_read_unlock(sb);
14171+ goto out; /* success */
14172+
14173+out_pid:
14174+ spin_lock(&fhsm->fhsm_spin);
14175+ fhsm->fhsm_pid = 0;
14176+ spin_unlock(&fhsm->fhsm_spin);
14177+out:
14178+ AuTraceErr(err);
14179+ return err;
14180+}
14181+
14182+/* ---------------------------------------------------------------------- */
14183+
14184+int au_fhsm_br_alloc(struct au_branch *br)
14185+{
14186+ int err;
14187+
14188+ err = 0;
14189+ br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14190+ if (br->br_fhsm)
14191+ au_br_fhsm_init(br->br_fhsm);
14192+ else
14193+ err = -ENOMEM;
14194+
14195+ return err;
14196+}
14197+
14198+/* ---------------------------------------------------------------------- */
14199+
14200+void au_fhsm_fin(struct super_block *sb)
14201+{
14202+ au_fhsm_notify(sb, /*val*/-1);
14203+}
14204+
14205+void au_fhsm_init(struct au_sbinfo *sbinfo)
14206+{
14207+ struct au_fhsm *fhsm;
14208+
14209+ fhsm = &sbinfo->si_fhsm;
14210+ spin_lock_init(&fhsm->fhsm_spin);
14211+ init_waitqueue_head(&fhsm->fhsm_wqh);
14212+ atomic_set(&fhsm->fhsm_readable, 0);
14213+ fhsm->fhsm_expire
14214+ = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
c1595e42 14215+ fhsm->fhsm_bottom = -1;
076b876e
AM
14216+}
14217+
14218+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14219+{
14220+ sbinfo->si_fhsm.fhsm_expire
14221+ = msecs_to_jiffies(sec * MSEC_PER_SEC);
14222+}
14223+
14224+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14225+{
14226+ unsigned int u;
14227+
14228+ if (!au_ftest_si(sbinfo, FHSM))
14229+ return;
14230+
14231+ u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14232+ if (u != AUFS_FHSM_CACHE_DEF_SEC)
14233+ seq_printf(seq, ",fhsm_sec=%u", u);
14234+}
7f207e10
AM
14235diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14236--- /usr/share/empty/fs/aufs/file.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 14237+++ linux/fs/aufs/file.c 2019-03-05 12:13:00.139224339 +0100
acd2b654 14238@@ -0,0 +1,863 @@
cd7a4cd9 14239+// SPDX-License-Identifier: GPL-2.0
1facf9fc 14240+/*
ba1aed25 14241+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 14242+ *
14243+ * This program, aufs is free software; you can redistribute it and/or modify
14244+ * it under the terms of the GNU General Public License as published by
14245+ * the Free Software Foundation; either version 2 of the License, or
14246+ * (at your option) any later version.
dece6358
AM
14247+ *
14248+ * This program is distributed in the hope that it will be useful,
14249+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14250+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14251+ * GNU General Public License for more details.
14252+ *
14253+ * You should have received a copy of the GNU General Public License
523b37e3 14254+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 14255+ */
14256+
14257+/*
4a4d8108 14258+ * handling file/dir, and address_space operation
1facf9fc 14259+ */
14260+
7eafdf33
AM
14261+#ifdef CONFIG_AUFS_DEBUG
14262+#include <linux/migrate.h>
14263+#endif
4a4d8108 14264+#include <linux/pagemap.h>
1facf9fc 14265+#include "aufs.h"
14266+
4a4d8108
AM
14267+/* drop flags for writing */
14268+unsigned int au_file_roflags(unsigned int flags)
14269+{
14270+ flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14271+ flags |= O_RDONLY | O_NOATIME;
14272+ return flags;
14273+}
14274+
14275+/* common functions to regular file and dir */
14276+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 14277+ struct file *file, int force_wr)
1facf9fc 14278+{
1308ab2a 14279+ struct file *h_file;
4a4d8108
AM
14280+ struct dentry *h_dentry;
14281+ struct inode *h_inode;
14282+ struct super_block *sb;
14283+ struct au_branch *br;
14284+ struct path h_path;
b912730e 14285+ int err;
1facf9fc 14286+
4a4d8108
AM
14287+ /* a race condition can happen between open and unlink/rmdir */
14288+ h_file = ERR_PTR(-ENOENT);
14289+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 14290+ if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
4a4d8108 14291+ goto out;
5527c038 14292+ h_inode = d_inode(h_dentry);
027c5e7a
AM
14293+ spin_lock(&h_dentry->d_lock);
14294+ err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
5527c038 14295+ /* || !d_inode(dentry)->i_nlink */
027c5e7a
AM
14296+ ;
14297+ spin_unlock(&h_dentry->d_lock);
14298+ if (unlikely(err))
4a4d8108 14299+ goto out;
1facf9fc 14300+
4a4d8108
AM
14301+ sb = dentry->d_sb;
14302+ br = au_sbr(sb, bindex);
b912730e
AM
14303+ err = au_br_test_oflag(flags, br);
14304+ h_file = ERR_PTR(err);
14305+ if (unlikely(err))
027c5e7a 14306+ goto out;
1facf9fc 14307+
4a4d8108 14308+ /* drop flags for writing */
5527c038 14309+ if (au_test_ro(sb, bindex, d_inode(dentry))) {
392086de
AM
14310+ if (force_wr && !(flags & O_WRONLY))
14311+ force_wr = 0;
4a4d8108 14312+ flags = au_file_roflags(flags);
392086de
AM
14313+ if (force_wr) {
14314+ h_file = ERR_PTR(-EROFS);
14315+ flags = au_file_roflags(flags);
14316+ if (unlikely(vfsub_native_ro(h_inode)
14317+ || IS_APPEND(h_inode)))
14318+ goto out;
14319+ flags &= ~O_ACCMODE;
14320+ flags |= O_WRONLY;
14321+ }
14322+ }
4a4d8108 14323+ flags &= ~O_CREAT;
acd2b654 14324+ au_lcnt_inc(&br->br_nfiles);
4a4d8108 14325+ h_path.dentry = h_dentry;
86dc4139 14326+ h_path.mnt = au_br_mnt(br);
38d290e6 14327+ h_file = vfsub_dentry_open(&h_path, flags);
4a4d8108
AM
14328+ if (IS_ERR(h_file))
14329+ goto out_br;
dece6358 14330+
b912730e 14331+ if (flags & __FMODE_EXEC) {
4a4d8108
AM
14332+ err = deny_write_access(h_file);
14333+ if (unlikely(err)) {
14334+ fput(h_file);
14335+ h_file = ERR_PTR(err);
14336+ goto out_br;
14337+ }
14338+ }
953406b4 14339+ fsnotify_open(h_file);
4a4d8108 14340+ goto out; /* success */
1facf9fc 14341+
4f0767ce 14342+out_br:
acd2b654 14343+ au_lcnt_dec(&br->br_nfiles);
4f0767ce 14344+out:
4a4d8108
AM
14345+ return h_file;
14346+}
1308ab2a 14347+
076b876e
AM
14348+static int au_cmoo(struct dentry *dentry)
14349+{
8b6a4947 14350+ int err, cmoo, matched;
076b876e
AM
14351+ unsigned int udba;
14352+ struct path h_path;
14353+ struct au_pin pin;
14354+ struct au_cp_generic cpg = {
14355+ .dentry = dentry,
14356+ .bdst = -1,
14357+ .bsrc = -1,
14358+ .len = -1,
14359+ .pin = &pin,
14360+ .flags = AuCpup_DTIME | AuCpup_HOPEN
14361+ };
7e9cd9fe 14362+ struct inode *delegated;
076b876e
AM
14363+ struct super_block *sb;
14364+ struct au_sbinfo *sbinfo;
14365+ struct au_fhsm *fhsm;
14366+ pid_t pid;
14367+ struct au_branch *br;
14368+ struct dentry *parent;
14369+ struct au_hinode *hdir;
14370+
14371+ DiMustWriteLock(dentry);
5527c038 14372+ IiMustWriteLock(d_inode(dentry));
076b876e
AM
14373+
14374+ err = 0;
14375+ if (IS_ROOT(dentry))
14376+ goto out;
5afbbe0d 14377+ cpg.bsrc = au_dbtop(dentry);
076b876e
AM
14378+ if (!cpg.bsrc)
14379+ goto out;
14380+
14381+ sb = dentry->d_sb;
14382+ sbinfo = au_sbi(sb);
14383+ fhsm = &sbinfo->si_fhsm;
14384+ pid = au_fhsm_pid(fhsm);
8b6a4947
AM
14385+ rcu_read_lock();
14386+ matched = (pid
14387+ && (current->pid == pid
14388+ || rcu_dereference(current->real_parent)->pid == pid));
14389+ rcu_read_unlock();
14390+ if (matched)
076b876e
AM
14391+ goto out;
14392+
14393+ br = au_sbr(sb, cpg.bsrc);
14394+ cmoo = au_br_cmoo(br->br_perm);
14395+ if (!cmoo)
14396+ goto out;
7e9cd9fe 14397+ if (!d_is_reg(dentry))
076b876e
AM
14398+ cmoo &= AuBrAttr_COO_ALL;
14399+ if (!cmoo)
14400+ goto out;
14401+
14402+ parent = dget_parent(dentry);
14403+ di_write_lock_parent(parent);
14404+ err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14405+ cpg.bdst = err;
14406+ if (unlikely(err < 0)) {
14407+ err = 0; /* there is no upper writable branch */
14408+ goto out_dgrade;
14409+ }
14410+ AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14411+
14412+ /* do not respect the coo attrib for the target branch */
14413+ err = au_cpup_dirs(dentry, cpg.bdst);
14414+ if (unlikely(err))
14415+ goto out_dgrade;
14416+
14417+ di_downgrade_lock(parent, AuLock_IR);
14418+ udba = au_opt_udba(sb);
14419+ err = au_pin(&pin, dentry, cpg.bdst, udba,
14420+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14421+ if (unlikely(err))
14422+ goto out_parent;
14423+
14424+ err = au_sio_cpup_simple(&cpg);
14425+ au_unpin(&pin);
14426+ if (unlikely(err))
14427+ goto out_parent;
14428+ if (!(cmoo & AuBrWAttr_MOO))
14429+ goto out_parent; /* success */
14430+
14431+ err = au_pin(&pin, dentry, cpg.bsrc, udba,
14432+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14433+ if (unlikely(err))
14434+ goto out_parent;
14435+
14436+ h_path.mnt = au_br_mnt(br);
14437+ h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
5527c038 14438+ hdir = au_hi(d_inode(parent), cpg.bsrc);
076b876e
AM
14439+ delegated = NULL;
14440+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14441+ au_unpin(&pin);
14442+ /* todo: keep h_dentry or not? */
14443+ if (unlikely(err == -EWOULDBLOCK)) {
14444+ pr_warn("cannot retry for NFSv4 delegation"
14445+ " for an internal unlink\n");
14446+ iput(delegated);
14447+ }
14448+ if (unlikely(err)) {
14449+ pr_err("unlink %pd after coo failed (%d), ignored\n",
14450+ dentry, err);
14451+ err = 0;
14452+ }
14453+ goto out_parent; /* success */
14454+
14455+out_dgrade:
14456+ di_downgrade_lock(parent, AuLock_IR);
14457+out_parent:
14458+ di_read_unlock(parent, AuLock_IR);
14459+ dput(parent);
14460+out:
14461+ AuTraceErr(err);
14462+ return err;
14463+}
14464+
b912730e 14465+int au_do_open(struct file *file, struct au_do_open_args *args)
1facf9fc 14466+{
8b6a4947 14467+ int err, aopen = args->aopen;
1facf9fc 14468+ struct dentry *dentry;
076b876e 14469+ struct au_finfo *finfo;
1308ab2a 14470+
8b6a4947 14471+ if (!aopen)
b912730e
AM
14472+ err = au_finfo_init(file, args->fidir);
14473+ else {
14474+ lockdep_off();
14475+ err = au_finfo_init(file, args->fidir);
14476+ lockdep_on();
14477+ }
4a4d8108
AM
14478+ if (unlikely(err))
14479+ goto out;
1facf9fc 14480+
2000de60 14481+ dentry = file->f_path.dentry;
b912730e 14482+ AuDebugOn(IS_ERR_OR_NULL(dentry));
8b6a4947
AM
14483+ di_write_lock_child(dentry);
14484+ err = au_cmoo(dentry);
14485+ di_downgrade_lock(dentry, AuLock_IR);
14486+ if (!err) {
14487+ if (!aopen)
b912730e 14488+ err = args->open(file, vfsub_file_flags(file), NULL);
8b6a4947
AM
14489+ else {
14490+ lockdep_off();
acd2b654
AM
14491+ err = args->open(file, vfsub_file_flags(file),
14492+ args->h_file);
8b6a4947
AM
14493+ lockdep_on();
14494+ }
b912730e 14495+ }
8b6a4947 14496+ di_read_unlock(dentry, AuLock_IR);
1facf9fc 14497+
076b876e
AM
14498+ finfo = au_fi(file);
14499+ if (!err) {
14500+ finfo->fi_file = file;
8b6a4947
AM
14501+ au_hbl_add(&finfo->fi_hlist,
14502+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
076b876e 14503+ }
8b6a4947 14504+ if (!aopen)
b912730e
AM
14505+ fi_write_unlock(file);
14506+ else {
14507+ lockdep_off();
14508+ fi_write_unlock(file);
14509+ lockdep_on();
14510+ }
4a4d8108 14511+ if (unlikely(err)) {
076b876e 14512+ finfo->fi_hdir = NULL;
1c60b727 14513+ au_finfo_fin(file);
1308ab2a 14514+ }
4a4d8108 14515+
4f0767ce 14516+out:
8b6a4947 14517+ AuTraceErr(err);
1308ab2a 14518+ return err;
14519+}
dece6358 14520+
4a4d8108 14521+int au_reopen_nondir(struct file *file)
1308ab2a 14522+{
4a4d8108 14523+ int err;
5afbbe0d 14524+ aufs_bindex_t btop;
4a4d8108 14525+ struct dentry *dentry;
acd2b654 14526+ struct au_branch *br;
4a4d8108 14527+ struct file *h_file, *h_file_tmp;
1308ab2a 14528+
2000de60 14529+ dentry = file->f_path.dentry;
5afbbe0d 14530+ btop = au_dbtop(dentry);
acd2b654 14531+ br = au_sbr(dentry->d_sb, btop);
4a4d8108 14532+ h_file_tmp = NULL;
5afbbe0d 14533+ if (au_fbtop(file) == btop) {
4a4d8108
AM
14534+ h_file = au_hf_top(file);
14535+ if (file->f_mode == h_file->f_mode)
14536+ return 0; /* success */
14537+ h_file_tmp = h_file;
14538+ get_file(h_file_tmp);
acd2b654 14539+ au_lcnt_inc(&br->br_nfiles);
5afbbe0d 14540+ au_set_h_fptr(file, btop, NULL);
4a4d8108
AM
14541+ }
14542+ AuDebugOn(au_fi(file)->fi_hdir);
86dc4139
AM
14543+ /*
14544+ * it can happen
14545+ * file exists on both of rw and ro
5afbbe0d 14546+ * open --> dbtop and fbtop are both 0
86dc4139
AM
14547+ * prepend a branch as rw, "rw" become ro
14548+ * remove rw/file
14549+ * delete the top branch, "rw" becomes rw again
5afbbe0d
AM
14550+ * --> dbtop is 1, fbtop is still 0
14551+ * write --> fbtop is 0 but dbtop is 1
86dc4139 14552+ */
5afbbe0d 14553+ /* AuDebugOn(au_fbtop(file) < btop); */
1308ab2a 14554+
5afbbe0d 14555+ h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
392086de 14556+ file, /*force_wr*/0);
4a4d8108 14557+ err = PTR_ERR(h_file);
86dc4139
AM
14558+ if (IS_ERR(h_file)) {
14559+ if (h_file_tmp) {
acd2b654 14560+ /* revert */
5afbbe0d 14561+ au_set_h_fptr(file, btop, h_file_tmp);
86dc4139
AM
14562+ h_file_tmp = NULL;
14563+ }
4a4d8108 14564+ goto out; /* todo: close all? */
86dc4139 14565+ }
4a4d8108
AM
14566+
14567+ err = 0;
5afbbe0d
AM
14568+ au_set_fbtop(file, btop);
14569+ au_set_h_fptr(file, btop, h_file);
4a4d8108
AM
14570+ au_update_figen(file);
14571+ /* todo: necessary? */
14572+ /* file->f_ra = h_file->f_ra; */
14573+
4f0767ce 14574+out:
acd2b654 14575+ if (h_file_tmp) {
4a4d8108 14576+ fput(h_file_tmp);
acd2b654
AM
14577+ au_lcnt_dec(&br->br_nfiles);
14578+ }
4a4d8108 14579+ return err;
1facf9fc 14580+}
14581+
1308ab2a 14582+/* ---------------------------------------------------------------------- */
14583+
4a4d8108
AM
14584+static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14585+ struct dentry *hi_wh)
1facf9fc 14586+{
4a4d8108 14587+ int err;
5afbbe0d 14588+ aufs_bindex_t btop;
4a4d8108
AM
14589+ struct au_dinfo *dinfo;
14590+ struct dentry *h_dentry;
14591+ struct au_hdentry *hdp;
1facf9fc 14592+
2000de60 14593+ dinfo = au_di(file->f_path.dentry);
4a4d8108 14594+ AuRwMustWriteLock(&dinfo->di_rwsem);
dece6358 14595+
5afbbe0d
AM
14596+ btop = dinfo->di_btop;
14597+ dinfo->di_btop = btgt;
14598+ hdp = au_hdentry(dinfo, btgt);
14599+ h_dentry = hdp->hd_dentry;
14600+ hdp->hd_dentry = hi_wh;
4a4d8108 14601+ err = au_reopen_nondir(file);
5afbbe0d
AM
14602+ hdp->hd_dentry = h_dentry;
14603+ dinfo->di_btop = btop;
1facf9fc 14604+
1facf9fc 14605+ return err;
14606+}
14607+
4a4d8108 14608+static int au_ready_to_write_wh(struct file *file, loff_t len,
86dc4139 14609+ aufs_bindex_t bcpup, struct au_pin *pin)
1facf9fc 14610+{
4a4d8108 14611+ int err;
027c5e7a 14612+ struct inode *inode, *h_inode;
c2b27bf2
AM
14613+ struct dentry *h_dentry, *hi_wh;
14614+ struct au_cp_generic cpg = {
2000de60 14615+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14616+ .bdst = bcpup,
14617+ .bsrc = -1,
14618+ .len = len,
14619+ .pin = pin
14620+ };
1facf9fc 14621+
5afbbe0d 14622+ au_update_dbtop(cpg.dentry);
5527c038 14623+ inode = d_inode(cpg.dentry);
027c5e7a 14624+ h_inode = NULL;
5afbbe0d
AM
14625+ if (au_dbtop(cpg.dentry) <= bcpup
14626+ && au_dbbot(cpg.dentry) >= bcpup) {
c2b27bf2 14627+ h_dentry = au_h_dptr(cpg.dentry, bcpup);
5527c038
JR
14628+ if (h_dentry && d_is_positive(h_dentry))
14629+ h_inode = d_inode(h_dentry);
027c5e7a 14630+ }
4a4d8108 14631+ hi_wh = au_hi_wh(inode, bcpup);
027c5e7a 14632+ if (!hi_wh && !h_inode)
c2b27bf2 14633+ err = au_sio_cpup_wh(&cpg, file);
4a4d8108
AM
14634+ else
14635+ /* already copied-up after unlink */
14636+ err = au_reopen_wh(file, bcpup, hi_wh);
1facf9fc 14637+
4a4d8108 14638+ if (!err
38d290e6
JR
14639+ && (inode->i_nlink > 1
14640+ || (inode->i_state & I_LINKABLE))
c2b27bf2
AM
14641+ && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14642+ au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
1308ab2a 14643+
dece6358 14644+ return err;
1facf9fc 14645+}
14646+
4a4d8108
AM
14647+/*
14648+ * prepare the @file for writing.
14649+ */
14650+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
1facf9fc 14651+{
4a4d8108 14652+ int err;
5afbbe0d 14653+ aufs_bindex_t dbtop;
c1595e42 14654+ struct dentry *parent;
86dc4139 14655+ struct inode *inode;
1facf9fc 14656+ struct super_block *sb;
4a4d8108 14657+ struct file *h_file;
c2b27bf2 14658+ struct au_cp_generic cpg = {
2000de60 14659+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14660+ .bdst = -1,
14661+ .bsrc = -1,
14662+ .len = len,
14663+ .pin = pin,
14664+ .flags = AuCpup_DTIME
14665+ };
1facf9fc 14666+
c2b27bf2 14667+ sb = cpg.dentry->d_sb;
5527c038 14668+ inode = d_inode(cpg.dentry);
5afbbe0d 14669+ cpg.bsrc = au_fbtop(file);
c2b27bf2 14670+ err = au_test_ro(sb, cpg.bsrc, inode);
4a4d8108 14671+ if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
c2b27bf2
AM
14672+ err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14673+ /*flags*/0);
1facf9fc 14674+ goto out;
4a4d8108 14675+ }
1facf9fc 14676+
027c5e7a 14677+ /* need to cpup or reopen */
c2b27bf2 14678+ parent = dget_parent(cpg.dentry);
4a4d8108 14679+ di_write_lock_parent(parent);
c2b27bf2
AM
14680+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14681+ cpg.bdst = err;
4a4d8108
AM
14682+ if (unlikely(err < 0))
14683+ goto out_dgrade;
14684+ err = 0;
14685+
c2b27bf2
AM
14686+ if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14687+ err = au_cpup_dirs(cpg.dentry, cpg.bdst);
1facf9fc 14688+ if (unlikely(err))
4a4d8108
AM
14689+ goto out_dgrade;
14690+ }
14691+
c2b27bf2 14692+ err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108
AM
14693+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14694+ if (unlikely(err))
14695+ goto out_dgrade;
14696+
5afbbe0d
AM
14697+ dbtop = au_dbtop(cpg.dentry);
14698+ if (dbtop <= cpg.bdst)
c2b27bf2 14699+ cpg.bsrc = cpg.bdst;
027c5e7a 14700+
5afbbe0d 14701+ if (dbtop <= cpg.bdst /* just reopen */
c2b27bf2 14702+ || !d_unhashed(cpg.dentry) /* copyup and reopen */
027c5e7a 14703+ ) {
392086de 14704+ h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
86dc4139 14705+ if (IS_ERR(h_file))
027c5e7a 14706+ err = PTR_ERR(h_file);
86dc4139 14707+ else {
027c5e7a 14708+ di_downgrade_lock(parent, AuLock_IR);
5afbbe0d 14709+ if (dbtop > cpg.bdst)
c2b27bf2 14710+ err = au_sio_cpup_simple(&cpg);
027c5e7a
AM
14711+ if (!err)
14712+ err = au_reopen_nondir(file);
c2b27bf2 14713+ au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
027c5e7a 14714+ }
027c5e7a
AM
14715+ } else { /* copyup as wh and reopen */
14716+ /*
14717+ * since writable hfsplus branch is not supported,
14718+ * h_open_pre/post() are unnecessary.
14719+ */
c2b27bf2 14720+ err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
4a4d8108 14721+ di_downgrade_lock(parent, AuLock_IR);
4a4d8108 14722+ }
4a4d8108
AM
14723+
14724+ if (!err) {
14725+ au_pin_set_parent_lflag(pin, /*lflag*/0);
14726+ goto out_dput; /* success */
14727+ }
14728+ au_unpin(pin);
14729+ goto out_unlock;
1facf9fc 14730+
4f0767ce 14731+out_dgrade:
4a4d8108 14732+ di_downgrade_lock(parent, AuLock_IR);
4f0767ce 14733+out_unlock:
4a4d8108 14734+ di_read_unlock(parent, AuLock_IR);
4f0767ce 14735+out_dput:
4a4d8108 14736+ dput(parent);
4f0767ce 14737+out:
1facf9fc 14738+ return err;
14739+}
14740+
4a4d8108
AM
14741+/* ---------------------------------------------------------------------- */
14742+
14743+int au_do_flush(struct file *file, fl_owner_t id,
14744+ int (*flush)(struct file *file, fl_owner_t id))
1facf9fc 14745+{
4a4d8108 14746+ int err;
1facf9fc 14747+ struct super_block *sb;
4a4d8108 14748+ struct inode *inode;
1facf9fc 14749+
c06a8ce3
AM
14750+ inode = file_inode(file);
14751+ sb = inode->i_sb;
4a4d8108
AM
14752+ si_noflush_read_lock(sb);
14753+ fi_read_lock(file);
b752ccd1 14754+ ii_read_lock_child(inode);
1facf9fc 14755+
4a4d8108
AM
14756+ err = flush(file, id);
14757+ au_cpup_attr_timesizes(inode);
1facf9fc 14758+
b752ccd1 14759+ ii_read_unlock(inode);
4a4d8108 14760+ fi_read_unlock(file);
1308ab2a 14761+ si_read_unlock(sb);
dece6358 14762+ return err;
1facf9fc 14763+}
14764+
4a4d8108
AM
14765+/* ---------------------------------------------------------------------- */
14766+
14767+static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
1facf9fc 14768+{
4a4d8108 14769+ int err;
4a4d8108
AM
14770+ struct au_pin pin;
14771+ struct au_finfo *finfo;
c2b27bf2 14772+ struct dentry *parent, *hi_wh;
4a4d8108 14773+ struct inode *inode;
1facf9fc 14774+ struct super_block *sb;
c2b27bf2 14775+ struct au_cp_generic cpg = {
2000de60 14776+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14777+ .bdst = -1,
14778+ .bsrc = -1,
14779+ .len = -1,
14780+ .pin = &pin,
14781+ .flags = AuCpup_DTIME
14782+ };
1facf9fc 14783+
4a4d8108
AM
14784+ FiMustWriteLock(file);
14785+
14786+ err = 0;
14787+ finfo = au_fi(file);
c2b27bf2 14788+ sb = cpg.dentry->d_sb;
5527c038 14789+ inode = d_inode(cpg.dentry);
5afbbe0d 14790+ cpg.bdst = au_ibtop(inode);
c2b27bf2 14791+ if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
1308ab2a 14792+ goto out;
dece6358 14793+
c2b27bf2
AM
14794+ parent = dget_parent(cpg.dentry);
14795+ if (au_test_ro(sb, cpg.bdst, inode)) {
4a4d8108 14796+ di_read_lock_parent(parent, !AuLock_IR);
c2b27bf2
AM
14797+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14798+ cpg.bdst = err;
4a4d8108
AM
14799+ di_read_unlock(parent, !AuLock_IR);
14800+ if (unlikely(err < 0))
14801+ goto out_parent;
14802+ err = 0;
1facf9fc 14803+ }
1facf9fc 14804+
4a4d8108 14805+ di_read_lock_parent(parent, AuLock_IR);
c2b27bf2 14806+ hi_wh = au_hi_wh(inode, cpg.bdst);
7f207e10
AM
14807+ if (!S_ISDIR(inode->i_mode)
14808+ && au_opt_test(au_mntflags(sb), PLINK)
4a4d8108 14809+ && au_plink_test(inode)
c2b27bf2 14810+ && !d_unhashed(cpg.dentry)
5afbbe0d 14811+ && cpg.bdst < au_dbtop(cpg.dentry)) {
c2b27bf2 14812+ err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
4a4d8108
AM
14813+ if (unlikely(err))
14814+ goto out_unlock;
14815+
14816+ /* always superio. */
c2b27bf2 14817+ err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108 14818+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 14819+ if (!err) {
c2b27bf2 14820+ err = au_sio_cpup_simple(&cpg);
367653fa
AM
14821+ au_unpin(&pin);
14822+ }
4a4d8108
AM
14823+ } else if (hi_wh) {
14824+ /* already copied-up after unlink */
c2b27bf2 14825+ err = au_reopen_wh(file, cpg.bdst, hi_wh);
4a4d8108
AM
14826+ *need_reopen = 0;
14827+ }
1facf9fc 14828+
4f0767ce 14829+out_unlock:
4a4d8108 14830+ di_read_unlock(parent, AuLock_IR);
4f0767ce 14831+out_parent:
4a4d8108 14832+ dput(parent);
4f0767ce 14833+out:
1308ab2a 14834+ return err;
dece6358 14835+}
1facf9fc 14836+
4a4d8108 14837+static void au_do_refresh_dir(struct file *file)
dece6358 14838+{
5afbbe0d 14839+ aufs_bindex_t bindex, bbot, new_bindex, brid;
4a4d8108
AM
14840+ struct au_hfile *p, tmp, *q;
14841+ struct au_finfo *finfo;
1308ab2a 14842+ struct super_block *sb;
4a4d8108 14843+ struct au_fidir *fidir;
1facf9fc 14844+
4a4d8108 14845+ FiMustWriteLock(file);
1facf9fc 14846+
2000de60 14847+ sb = file->f_path.dentry->d_sb;
4a4d8108
AM
14848+ finfo = au_fi(file);
14849+ fidir = finfo->fi_hdir;
14850+ AuDebugOn(!fidir);
14851+ p = fidir->fd_hfile + finfo->fi_btop;
14852+ brid = p->hf_br->br_id;
5afbbe0d
AM
14853+ bbot = fidir->fd_bbot;
14854+ for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
4a4d8108
AM
14855+ if (!p->hf_file)
14856+ continue;
1308ab2a 14857+
4a4d8108
AM
14858+ new_bindex = au_br_index(sb, p->hf_br->br_id);
14859+ if (new_bindex == bindex)
14860+ continue;
14861+ if (new_bindex < 0) {
14862+ au_set_h_fptr(file, bindex, NULL);
14863+ continue;
14864+ }
1308ab2a 14865+
4a4d8108
AM
14866+ /* swap two lower inode, and loop again */
14867+ q = fidir->fd_hfile + new_bindex;
14868+ tmp = *q;
14869+ *q = *p;
14870+ *p = tmp;
14871+ if (tmp.hf_file) {
14872+ bindex--;
14873+ p--;
14874+ }
14875+ }
1308ab2a 14876+
4a4d8108 14877+ p = fidir->fd_hfile;
2000de60 14878+ if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
5afbbe0d
AM
14879+ bbot = au_sbbot(sb);
14880+ for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
4a4d8108
AM
14881+ finfo->fi_btop++, p++)
14882+ if (p->hf_file) {
c06a8ce3 14883+ if (file_inode(p->hf_file))
4a4d8108 14884+ break;
1c60b727 14885+ au_hfput(p, /*execed*/0);
4a4d8108
AM
14886+ }
14887+ } else {
5afbbe0d
AM
14888+ bbot = au_br_index(sb, brid);
14889+ for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
4a4d8108
AM
14890+ finfo->fi_btop++, p++)
14891+ if (p->hf_file)
1c60b727 14892+ au_hfput(p, /*execed*/0);
5afbbe0d 14893+ bbot = au_sbbot(sb);
4a4d8108 14894+ }
1308ab2a 14895+
5afbbe0d
AM
14896+ p = fidir->fd_hfile + bbot;
14897+ for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
4a4d8108
AM
14898+ fidir->fd_bbot--, p--)
14899+ if (p->hf_file) {
c06a8ce3 14900+ if (file_inode(p->hf_file))
4a4d8108 14901+ break;
1c60b727 14902+ au_hfput(p, /*execed*/0);
4a4d8108
AM
14903+ }
14904+ AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
1308ab2a 14905+}
14906+
4a4d8108
AM
14907+/*
14908+ * after branch manipulating, refresh the file.
14909+ */
14910+static int refresh_file(struct file *file, int (*reopen)(struct file *file))
1facf9fc 14911+{
e2f27e51 14912+ int err, need_reopen, nbr;
5afbbe0d 14913+ aufs_bindex_t bbot, bindex;
4a4d8108 14914+ struct dentry *dentry;
e2f27e51 14915+ struct super_block *sb;
1308ab2a 14916+ struct au_finfo *finfo;
4a4d8108 14917+ struct au_hfile *hfile;
1facf9fc 14918+
2000de60 14919+ dentry = file->f_path.dentry;
e2f27e51
AM
14920+ sb = dentry->d_sb;
14921+ nbr = au_sbbot(sb) + 1;
1308ab2a 14922+ finfo = au_fi(file);
4a4d8108
AM
14923+ if (!finfo->fi_hdir) {
14924+ hfile = &finfo->fi_htop;
14925+ AuDebugOn(!hfile->hf_file);
e2f27e51 14926+ bindex = au_br_index(sb, hfile->hf_br->br_id);
4a4d8108
AM
14927+ AuDebugOn(bindex < 0);
14928+ if (bindex != finfo->fi_btop)
5afbbe0d 14929+ au_set_fbtop(file, bindex);
4a4d8108 14930+ } else {
e2f27e51 14931+ err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
4a4d8108
AM
14932+ if (unlikely(err))
14933+ goto out;
14934+ au_do_refresh_dir(file);
14935+ }
1facf9fc 14936+
4a4d8108
AM
14937+ err = 0;
14938+ need_reopen = 1;
14939+ if (!au_test_mmapped(file))
14940+ err = au_file_refresh_by_inode(file, &need_reopen);
e2f27e51
AM
14941+ if (finfo->fi_hdir)
14942+ /* harmless if err */
14943+ au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
027c5e7a 14944+ if (!err && need_reopen && !d_unlinked(dentry))
4a4d8108
AM
14945+ err = reopen(file);
14946+ if (!err) {
14947+ au_update_figen(file);
14948+ goto out; /* success */
14949+ }
14950+
14951+ /* error, close all lower files */
14952+ if (finfo->fi_hdir) {
5afbbe0d
AM
14953+ bbot = au_fbbot_dir(file);
14954+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
4a4d8108
AM
14955+ au_set_h_fptr(file, bindex, NULL);
14956+ }
1facf9fc 14957+
4f0767ce 14958+out:
1facf9fc 14959+ return err;
14960+}
14961+
4a4d8108
AM
14962+/* common function to regular file and dir */
14963+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
521ced18 14964+ int wlock, unsigned int fi_lsc)
dece6358 14965+{
1308ab2a 14966+ int err;
4a4d8108 14967+ unsigned int sigen, figen;
5afbbe0d 14968+ aufs_bindex_t btop;
4a4d8108
AM
14969+ unsigned char pseudo_link;
14970+ struct dentry *dentry;
14971+ struct inode *inode;
1facf9fc 14972+
4a4d8108 14973+ err = 0;
2000de60 14974+ dentry = file->f_path.dentry;
5527c038 14975+ inode = d_inode(dentry);
4a4d8108 14976+ sigen = au_sigen(dentry->d_sb);
521ced18 14977+ fi_write_lock_nested(file, fi_lsc);
4a4d8108 14978+ figen = au_figen(file);
521ced18
JR
14979+ if (!fi_lsc)
14980+ di_write_lock_child(dentry);
14981+ else
14982+ di_write_lock_child2(dentry);
5afbbe0d
AM
14983+ btop = au_dbtop(dentry);
14984+ pseudo_link = (btop != au_ibtop(inode));
14985+ if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
4a4d8108
AM
14986+ if (!wlock) {
14987+ di_downgrade_lock(dentry, AuLock_IR);
14988+ fi_downgrade_lock(file);
14989+ }
14990+ goto out; /* success */
14991+ }
dece6358 14992+
4a4d8108 14993+ AuDbg("sigen %d, figen %d\n", sigen, figen);
027c5e7a 14994+ if (au_digen_test(dentry, sigen)) {
4a4d8108 14995+ err = au_reval_dpath(dentry, sigen);
027c5e7a 14996+ AuDebugOn(!err && au_digen_test(dentry, sigen));
4a4d8108 14997+ }
dece6358 14998+
027c5e7a
AM
14999+ if (!err)
15000+ err = refresh_file(file, reopen);
4a4d8108
AM
15001+ if (!err) {
15002+ if (!wlock) {
15003+ di_downgrade_lock(dentry, AuLock_IR);
15004+ fi_downgrade_lock(file);
15005+ }
15006+ } else {
15007+ di_write_unlock(dentry);
15008+ fi_write_unlock(file);
15009+ }
1facf9fc 15010+
4f0767ce 15011+out:
1308ab2a 15012+ return err;
15013+}
1facf9fc 15014+
4a4d8108
AM
15015+/* ---------------------------------------------------------------------- */
15016+
15017+/* cf. aufs_nopage() */
15018+/* for madvise(2) */
15019+static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
1308ab2a 15020+{
4a4d8108
AM
15021+ unlock_page(page);
15022+ return 0;
15023+}
1facf9fc 15024+
4a4d8108 15025+/* it will never be called, but necessary to support O_DIRECT */
5afbbe0d 15026+static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
4a4d8108 15027+{ BUG(); return 0; }
1facf9fc 15028+
4a4d8108
AM
15029+/* they will never be called. */
15030+#ifdef CONFIG_AUFS_DEBUG
15031+static int aufs_write_begin(struct file *file, struct address_space *mapping,
15032+ loff_t pos, unsigned len, unsigned flags,
15033+ struct page **pagep, void **fsdata)
15034+{ AuUnsupport(); return 0; }
15035+static int aufs_write_end(struct file *file, struct address_space *mapping,
15036+ loff_t pos, unsigned len, unsigned copied,
15037+ struct page *page, void *fsdata)
15038+{ AuUnsupport(); return 0; }
15039+static int aufs_writepage(struct page *page, struct writeback_control *wbc)
15040+{ AuUnsupport(); return 0; }
1308ab2a 15041+
4a4d8108
AM
15042+static int aufs_set_page_dirty(struct page *page)
15043+{ AuUnsupport(); return 0; }
392086de
AM
15044+static void aufs_invalidatepage(struct page *page, unsigned int offset,
15045+ unsigned int length)
4a4d8108
AM
15046+{ AuUnsupport(); }
15047+static int aufs_releasepage(struct page *page, gfp_t gfp)
15048+{ AuUnsupport(); return 0; }
79b8bda9 15049+#if 0 /* called by memory compaction regardless file */
4a4d8108 15050+static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
7eafdf33 15051+ struct page *page, enum migrate_mode mode)
4a4d8108 15052+{ AuUnsupport(); return 0; }
79b8bda9 15053+#endif
e2f27e51
AM
15054+static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
15055+{ AuUnsupport(); return true; }
15056+static void aufs_putback_page(struct page *page)
15057+{ AuUnsupport(); }
4a4d8108
AM
15058+static int aufs_launder_page(struct page *page)
15059+{ AuUnsupport(); return 0; }
15060+static int aufs_is_partially_uptodate(struct page *page,
38d290e6
JR
15061+ unsigned long from,
15062+ unsigned long count)
4a4d8108 15063+{ AuUnsupport(); return 0; }
392086de
AM
15064+static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
15065+ bool *writeback)
15066+{ AuUnsupport(); }
4a4d8108
AM
15067+static int aufs_error_remove_page(struct address_space *mapping,
15068+ struct page *page)
15069+{ AuUnsupport(); return 0; }
b4510431
AM
15070+static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
15071+ sector_t *span)
15072+{ AuUnsupport(); return 0; }
15073+static void aufs_swap_deactivate(struct file *file)
15074+{ AuUnsupport(); }
4a4d8108
AM
15075+#endif /* CONFIG_AUFS_DEBUG */
15076+
15077+const struct address_space_operations aufs_aop = {
15078+ .readpage = aufs_readpage,
15079+ .direct_IO = aufs_direct_IO,
4a4d8108
AM
15080+#ifdef CONFIG_AUFS_DEBUG
15081+ .writepage = aufs_writepage,
4a4d8108
AM
15082+ /* no writepages, because of writepage */
15083+ .set_page_dirty = aufs_set_page_dirty,
15084+ /* no readpages, because of readpage */
15085+ .write_begin = aufs_write_begin,
15086+ .write_end = aufs_write_end,
15087+ /* no bmap, no block device */
15088+ .invalidatepage = aufs_invalidatepage,
15089+ .releasepage = aufs_releasepage,
79b8bda9
AM
15090+ /* is fallback_migrate_page ok? */
15091+ /* .migratepage = aufs_migratepage, */
e2f27e51
AM
15092+ .isolate_page = aufs_isolate_page,
15093+ .putback_page = aufs_putback_page,
4a4d8108
AM
15094+ .launder_page = aufs_launder_page,
15095+ .is_partially_uptodate = aufs_is_partially_uptodate,
392086de 15096+ .is_dirty_writeback = aufs_is_dirty_writeback,
b4510431
AM
15097+ .error_remove_page = aufs_error_remove_page,
15098+ .swap_activate = aufs_swap_activate,
15099+ .swap_deactivate = aufs_swap_deactivate
4a4d8108 15100+#endif /* CONFIG_AUFS_DEBUG */
dece6358 15101+};
7f207e10
AM
15102diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
15103--- /usr/share/empty/fs/aufs/file.h 1970-01-01 01:00:00.000000000 +0100
ba1aed25 15104+++ linux/fs/aufs/file.h 2019-03-05 12:13:00.139224339 +0100
9f237c51 15105@@ -0,0 +1,342 @@
062440b3 15106+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 15107+/*
ba1aed25 15108+ * Copyright (C) 2005-2019 Junjiro R. Okajima
4a4d8108
AM
15109+ *
15110+ * This program, aufs is free software; you can redistribute it and/or modify
15111+ * it under the terms of the GNU General Public License as published by
15112+ * the Free Software Foundation; either version 2 of the License, or
15113+ * (at your option) any later version.
15114+ *
15115+ * This program is distributed in the hope that it will be useful,
15116+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15117+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15118+ * GNU General Public License for more details.
15119+ *
15120+ * You should have received a copy of the GNU General Public License
523b37e3 15121+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 15122+ */
1facf9fc 15123+
4a4d8108
AM
15124+/*
15125+ * file operations
15126+ */
1facf9fc 15127+
4a4d8108
AM
15128+#ifndef __AUFS_FILE_H__
15129+#define __AUFS_FILE_H__
1facf9fc 15130+
4a4d8108 15131+#ifdef __KERNEL__
1facf9fc 15132+
2cbb1c4b 15133+#include <linux/file.h>
4a4d8108 15134+#include <linux/fs.h>
3c1bdaff 15135+#include <linux/mm_types.h>
4a4d8108 15136+#include <linux/poll.h>
4a4d8108 15137+#include "rwsem.h"
1facf9fc 15138+
4a4d8108
AM
15139+struct au_branch;
15140+struct au_hfile {
15141+ struct file *hf_file;
15142+ struct au_branch *hf_br;
15143+};
1facf9fc 15144+
4a4d8108
AM
15145+struct au_vdir;
15146+struct au_fidir {
15147+ aufs_bindex_t fd_bbot;
15148+ aufs_bindex_t fd_nent;
15149+ struct au_vdir *fd_vdir_cache;
15150+ struct au_hfile fd_hfile[];
15151+};
1facf9fc 15152+
4a4d8108 15153+static inline int au_fidir_sz(int nent)
dece6358 15154+{
4f0767ce
JR
15155+ AuDebugOn(nent < 0);
15156+ return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
4a4d8108 15157+}
1facf9fc 15158+
4a4d8108
AM
15159+struct au_finfo {
15160+ atomic_t fi_generation;
dece6358 15161+
4a4d8108
AM
15162+ struct au_rwsem fi_rwsem;
15163+ aufs_bindex_t fi_btop;
15164+
15165+ /* do not union them */
15166+ struct { /* for non-dir */
15167+ struct au_hfile fi_htop;
2cbb1c4b 15168+ atomic_t fi_mmapped;
4a4d8108
AM
15169+ };
15170+ struct au_fidir *fi_hdir; /* for dir only */
523b37e3 15171+
8b6a4947 15172+ struct hlist_bl_node fi_hlist;
1c60b727 15173+ struct file *fi_file; /* very ugly */
9f237c51 15174+ struct rcu_head rcu;
4a4d8108 15175+} ____cacheline_aligned_in_smp;
1facf9fc 15176+
4a4d8108 15177+/* ---------------------------------------------------------------------- */
1facf9fc 15178+
4a4d8108
AM
15179+/* file.c */
15180+extern const struct address_space_operations aufs_aop;
15181+unsigned int au_file_roflags(unsigned int flags);
15182+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 15183+ struct file *file, int force_wr);
b912730e 15184+struct au_do_open_args {
8b6a4947 15185+ int aopen;
b912730e
AM
15186+ int (*open)(struct file *file, int flags,
15187+ struct file *h_file);
15188+ struct au_fidir *fidir;
15189+ struct file *h_file;
15190+};
15191+int au_do_open(struct file *file, struct au_do_open_args *args);
4a4d8108
AM
15192+int au_reopen_nondir(struct file *file);
15193+struct au_pin;
15194+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15195+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
521ced18 15196+ int wlock, unsigned int fi_lsc);
4a4d8108
AM
15197+int au_do_flush(struct file *file, fl_owner_t id,
15198+ int (*flush)(struct file *file, fl_owner_t id));
1facf9fc 15199+
4a4d8108
AM
15200+/* poll.c */
15201+#ifdef CONFIG_AUFS_POLL
cd7a4cd9 15202+__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
4a4d8108 15203+#endif
1facf9fc 15204+
4a4d8108
AM
15205+#ifdef CONFIG_AUFS_BR_HFSPLUS
15206+/* hfsplus.c */
392086de
AM
15207+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15208+ int force_wr);
4a4d8108
AM
15209+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15210+ struct file *h_file);
15211+#else
c1595e42
JR
15212+AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15213+ aufs_bindex_t bindex, int force_wr)
4a4d8108
AM
15214+AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15215+ struct file *h_file);
15216+#endif
1facf9fc 15217+
4a4d8108
AM
15218+/* f_op.c */
15219+extern const struct file_operations aufs_file_fop;
b912730e 15220+int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
4a4d8108 15221+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
521ced18 15222+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
4a4d8108 15223+
4a4d8108 15224+/* finfo.c */
f0c0a007 15225+void au_hfput(struct au_hfile *hf, int execed);
4a4d8108
AM
15226+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15227+ struct file *h_file);
1facf9fc 15228+
4a4d8108 15229+void au_update_figen(struct file *file);
4a4d8108 15230+struct au_fidir *au_fidir_alloc(struct super_block *sb);
e2f27e51 15231+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
1facf9fc 15232+
4a4d8108 15233+void au_fi_init_once(void *_fi);
1c60b727 15234+void au_finfo_fin(struct file *file);
4a4d8108 15235+int au_finfo_init(struct file *file, struct au_fidir *fidir);
1facf9fc 15236+
4a4d8108
AM
15237+/* ioctl.c */
15238+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
15239+#ifdef CONFIG_COMPAT
15240+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15241+ unsigned long arg);
c2b27bf2
AM
15242+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15243+ unsigned long arg);
b752ccd1 15244+#endif
1facf9fc 15245+
4a4d8108 15246+/* ---------------------------------------------------------------------- */
1facf9fc 15247+
4a4d8108
AM
15248+static inline struct au_finfo *au_fi(struct file *file)
15249+{
38d290e6 15250+ return file->private_data;
4a4d8108 15251+}
1facf9fc 15252+
4a4d8108 15253+/* ---------------------------------------------------------------------- */
1facf9fc 15254+
8b6a4947
AM
15255+#define fi_read_lock(f) au_rw_read_lock(&au_fi(f)->fi_rwsem)
15256+#define fi_write_lock(f) au_rw_write_lock(&au_fi(f)->fi_rwsem)
15257+#define fi_read_trylock(f) au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15258+#define fi_write_trylock(f) au_rw_write_trylock(&au_fi(f)->fi_rwsem)
4a4d8108 15259+/*
8b6a4947
AM
15260+#define fi_read_trylock_nested(f) \
15261+ au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15262+#define fi_write_trylock_nested(f) \
15263+ au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15264+*/
15265+
15266+#define fi_read_unlock(f) au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15267+#define fi_write_unlock(f) au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15268+#define fi_downgrade_lock(f) au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
1308ab2a 15269+
521ced18
JR
15270+/* lock subclass for finfo */
15271+enum {
15272+ AuLsc_FI_1,
15273+ AuLsc_FI_2
15274+};
15275+
15276+static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15277+{
15278+ au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15279+}
15280+
15281+static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15282+{
15283+ au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15284+}
15285+
15286+/*
15287+ * fi_read_lock_1, fi_write_lock_1,
15288+ * fi_read_lock_2, fi_write_lock_2
15289+ */
15290+#define AuReadLockFunc(name) \
15291+static inline void fi_read_lock_##name(struct file *f) \
15292+{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15293+
15294+#define AuWriteLockFunc(name) \
15295+static inline void fi_write_lock_##name(struct file *f) \
15296+{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15297+
15298+#define AuRWLockFuncs(name) \
15299+ AuReadLockFunc(name) \
15300+ AuWriteLockFunc(name)
15301+
15302+AuRWLockFuncs(1);
15303+AuRWLockFuncs(2);
15304+
15305+#undef AuReadLockFunc
15306+#undef AuWriteLockFunc
15307+#undef AuRWLockFuncs
15308+
4a4d8108
AM
15309+#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15310+#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15311+#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
1facf9fc 15312+
1308ab2a 15313+/* ---------------------------------------------------------------------- */
15314+
4a4d8108 15315+/* todo: hard/soft set? */
5afbbe0d 15316+static inline aufs_bindex_t au_fbtop(struct file *file)
dece6358 15317+{
4a4d8108
AM
15318+ FiMustAnyLock(file);
15319+ return au_fi(file)->fi_btop;
15320+}
dece6358 15321+
5afbbe0d 15322+static inline aufs_bindex_t au_fbbot_dir(struct file *file)
4a4d8108
AM
15323+{
15324+ FiMustAnyLock(file);
15325+ AuDebugOn(!au_fi(file)->fi_hdir);
15326+ return au_fi(file)->fi_hdir->fd_bbot;
15327+}
1facf9fc 15328+
4a4d8108
AM
15329+static inline struct au_vdir *au_fvdir_cache(struct file *file)
15330+{
15331+ FiMustAnyLock(file);
15332+ AuDebugOn(!au_fi(file)->fi_hdir);
15333+ return au_fi(file)->fi_hdir->fd_vdir_cache;
15334+}
1facf9fc 15335+
5afbbe0d 15336+static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
15337+{
15338+ FiMustWriteLock(file);
15339+ au_fi(file)->fi_btop = bindex;
15340+}
1facf9fc 15341+
5afbbe0d 15342+static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
15343+{
15344+ FiMustWriteLock(file);
15345+ AuDebugOn(!au_fi(file)->fi_hdir);
15346+ au_fi(file)->fi_hdir->fd_bbot = bindex;
15347+}
1308ab2a 15348+
4a4d8108
AM
15349+static inline void au_set_fvdir_cache(struct file *file,
15350+ struct au_vdir *vdir_cache)
15351+{
15352+ FiMustWriteLock(file);
15353+ AuDebugOn(!au_fi(file)->fi_hdir);
15354+ au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15355+}
dece6358 15356+
4a4d8108
AM
15357+static inline struct file *au_hf_top(struct file *file)
15358+{
15359+ FiMustAnyLock(file);
15360+ AuDebugOn(au_fi(file)->fi_hdir);
15361+ return au_fi(file)->fi_htop.hf_file;
15362+}
1facf9fc 15363+
4a4d8108
AM
15364+static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15365+{
15366+ FiMustAnyLock(file);
15367+ AuDebugOn(!au_fi(file)->fi_hdir);
15368+ return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
dece6358
AM
15369+}
15370+
4a4d8108
AM
15371+/* todo: memory barrier? */
15372+static inline unsigned int au_figen(struct file *f)
dece6358 15373+{
4a4d8108
AM
15374+ return atomic_read(&au_fi(f)->fi_generation);
15375+}
dece6358 15376+
2cbb1c4b
JR
15377+static inline void au_set_mmapped(struct file *f)
15378+{
15379+ if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15380+ return;
0c3ec466 15381+ pr_warn("fi_mmapped wrapped around\n");
2cbb1c4b
JR
15382+ while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15383+ ;
15384+}
15385+
15386+static inline void au_unset_mmapped(struct file *f)
15387+{
15388+ atomic_dec(&au_fi(f)->fi_mmapped);
15389+}
15390+
4a4d8108
AM
15391+static inline int au_test_mmapped(struct file *f)
15392+{
2cbb1c4b
JR
15393+ return atomic_read(&au_fi(f)->fi_mmapped);
15394+}
15395+
15396+/* customize vma->vm_file */
15397+
15398+static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15399+ struct file *file)
15400+{
53392da6
AM
15401+ struct file *f;
15402+
15403+ f = vma->vm_file;
2cbb1c4b
JR
15404+ get_file(file);
15405+ vma->vm_file = file;
53392da6 15406+ fput(f);
2cbb1c4b
JR
15407+}
15408+
15409+#ifdef CONFIG_MMU
15410+#define AuDbgVmRegion(file, vma) do {} while (0)
15411+
15412+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15413+ struct file *file)
15414+{
15415+ au_do_vm_file_reset(vma, file);
15416+}
15417+#else
15418+#define AuDbgVmRegion(file, vma) \
15419+ AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15420+
15421+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15422+ struct file *file)
15423+{
53392da6
AM
15424+ struct file *f;
15425+
2cbb1c4b 15426+ au_do_vm_file_reset(vma, file);
53392da6 15427+ f = vma->vm_region->vm_file;
2cbb1c4b
JR
15428+ get_file(file);
15429+ vma->vm_region->vm_file = file;
53392da6 15430+ fput(f);
2cbb1c4b
JR
15431+}
15432+#endif /* CONFIG_MMU */
15433+
15434+/* handle vma->vm_prfile */
fb47a38f 15435+static inline void au_vm_prfile_set(struct vm_area_struct *vma,
2cbb1c4b
JR
15436+ struct file *file)
15437+{
2cbb1c4b
JR
15438+ get_file(file);
15439+ vma->vm_prfile = file;
15440+#ifndef CONFIG_MMU
15441+ get_file(file);
15442+ vma->vm_region->vm_prfile = file;
15443+#endif
fb47a38f 15444+}
1308ab2a 15445+
4a4d8108
AM
15446+#endif /* __KERNEL__ */
15447+#endif /* __AUFS_FILE_H__ */
7f207e10
AM
15448diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15449--- /usr/share/empty/fs/aufs/finfo.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 15450+++ linux/fs/aufs/finfo.c 2019-03-05 12:13:00.139224339 +0100
062440b3 15451@@ -0,0 +1,149 @@
cd7a4cd9 15452+// SPDX-License-Identifier: GPL-2.0
4a4d8108 15453+/*
ba1aed25 15454+ * Copyright (C) 2005-2019 Junjiro R. Okajima
4a4d8108
AM
15455+ *
15456+ * This program, aufs is free software; you can redistribute it and/or modify
15457+ * it under the terms of the GNU General Public License as published by
15458+ * the Free Software Foundation; either version 2 of the License, or
15459+ * (at your option) any later version.
15460+ *
15461+ * This program is distributed in the hope that it will be useful,
15462+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15463+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15464+ * GNU General Public License for more details.
15465+ *
15466+ * You should have received a copy of the GNU General Public License
523b37e3 15467+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 15468+ */
1308ab2a 15469+
4a4d8108
AM
15470+/*
15471+ * file private data
15472+ */
1facf9fc 15473+
4a4d8108 15474+#include "aufs.h"
1facf9fc 15475+
f0c0a007 15476+void au_hfput(struct au_hfile *hf, int execed)
4a4d8108 15477+{
f0c0a007 15478+ if (execed)
4a4d8108
AM
15479+ allow_write_access(hf->hf_file);
15480+ fput(hf->hf_file);
15481+ hf->hf_file = NULL;
acd2b654 15482+ au_lcnt_dec(&hf->hf_br->br_nfiles);
4a4d8108
AM
15483+ hf->hf_br = NULL;
15484+}
1facf9fc 15485+
4a4d8108
AM
15486+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15487+{
15488+ struct au_finfo *finfo = au_fi(file);
15489+ struct au_hfile *hf;
15490+ struct au_fidir *fidir;
15491+
15492+ fidir = finfo->fi_hdir;
15493+ if (!fidir) {
15494+ AuDebugOn(finfo->fi_btop != bindex);
15495+ hf = &finfo->fi_htop;
15496+ } else
15497+ hf = fidir->fd_hfile + bindex;
15498+
15499+ if (hf && hf->hf_file)
f0c0a007 15500+ au_hfput(hf, vfsub_file_execed(file));
4a4d8108
AM
15501+ if (val) {
15502+ FiMustWriteLock(file);
b912730e 15503+ AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
4a4d8108 15504+ hf->hf_file = val;
2000de60 15505+ hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
1308ab2a 15506+ }
4a4d8108 15507+}
1facf9fc 15508+
4a4d8108
AM
15509+void au_update_figen(struct file *file)
15510+{
2000de60 15511+ atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
4a4d8108 15512+ /* smp_mb(); */ /* atomic_set */
1facf9fc 15513+}
15514+
4a4d8108
AM
15515+/* ---------------------------------------------------------------------- */
15516+
4a4d8108
AM
15517+struct au_fidir *au_fidir_alloc(struct super_block *sb)
15518+{
15519+ struct au_fidir *fidir;
15520+ int nbr;
15521+
5afbbe0d 15522+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
15523+ if (nbr < 2)
15524+ nbr = 2; /* initial allocate for 2 branches */
15525+ fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15526+ if (fidir) {
15527+ fidir->fd_bbot = -1;
15528+ fidir->fd_nent = nbr;
4a4d8108
AM
15529+ }
15530+
15531+ return fidir;
15532+}
15533+
e2f27e51 15534+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
4a4d8108
AM
15535+{
15536+ int err;
15537+ struct au_fidir *fidir, *p;
15538+
15539+ AuRwMustWriteLock(&finfo->fi_rwsem);
15540+ fidir = finfo->fi_hdir;
15541+ AuDebugOn(!fidir);
15542+
15543+ err = -ENOMEM;
15544+ p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
e2f27e51 15545+ GFP_NOFS, may_shrink);
4a4d8108
AM
15546+ if (p) {
15547+ p->fd_nent = nbr;
15548+ finfo->fi_hdir = p;
15549+ err = 0;
15550+ }
1facf9fc 15551+
dece6358 15552+ return err;
1facf9fc 15553+}
1308ab2a 15554+
15555+/* ---------------------------------------------------------------------- */
15556+
1c60b727 15557+void au_finfo_fin(struct file *file)
1308ab2a 15558+{
4a4d8108
AM
15559+ struct au_finfo *finfo;
15560+
acd2b654 15561+ au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
7f207e10 15562+
4a4d8108
AM
15563+ finfo = au_fi(file);
15564+ AuDebugOn(finfo->fi_hdir);
15565+ AuRwDestroy(&finfo->fi_rwsem);
1c60b727 15566+ au_cache_free_finfo(finfo);
1308ab2a 15567+}
1308ab2a 15568+
e49829fe 15569+void au_fi_init_once(void *_finfo)
4a4d8108 15570+{
e49829fe 15571+ struct au_finfo *finfo = _finfo;
1308ab2a 15572+
e49829fe 15573+ au_rw_init(&finfo->fi_rwsem);
4a4d8108 15574+}
1308ab2a 15575+
4a4d8108
AM
15576+int au_finfo_init(struct file *file, struct au_fidir *fidir)
15577+{
1716fcea 15578+ int err;
4a4d8108
AM
15579+ struct au_finfo *finfo;
15580+ struct dentry *dentry;
15581+
15582+ err = -ENOMEM;
2000de60 15583+ dentry = file->f_path.dentry;
4a4d8108
AM
15584+ finfo = au_cache_alloc_finfo();
15585+ if (unlikely(!finfo))
15586+ goto out;
15587+
15588+ err = 0;
acd2b654 15589+ au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
4a4d8108
AM
15590+ au_rw_write_lock(&finfo->fi_rwsem);
15591+ finfo->fi_btop = -1;
15592+ finfo->fi_hdir = fidir;
15593+ atomic_set(&finfo->fi_generation, au_digen(dentry));
15594+ /* smp_mb(); */ /* atomic_set */
15595+
15596+ file->private_data = finfo;
15597+
15598+out:
15599+ return err;
15600+}
7f207e10
AM
15601diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15602--- /usr/share/empty/fs/aufs/f_op.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 15603+++ linux/fs/aufs/f_op.c 2019-03-05 12:13:00.139224339 +0100
acd2b654 15604@@ -0,0 +1,819 @@
cd7a4cd9 15605+// SPDX-License-Identifier: GPL-2.0
dece6358 15606+/*
ba1aed25 15607+ * Copyright (C) 2005-2019 Junjiro R. Okajima
dece6358
AM
15608+ *
15609+ * This program, aufs is free software; you can redistribute it and/or modify
15610+ * it under the terms of the GNU General Public License as published by
15611+ * the Free Software Foundation; either version 2 of the License, or
15612+ * (at your option) any later version.
15613+ *
15614+ * This program is distributed in the hope that it will be useful,
15615+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15616+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15617+ * GNU General Public License for more details.
15618+ *
15619+ * You should have received a copy of the GNU General Public License
523b37e3 15620+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 15621+ */
1facf9fc 15622+
15623+/*
4a4d8108 15624+ * file and vm operations
1facf9fc 15625+ */
dece6358 15626+
86dc4139 15627+#include <linux/aio.h>
4a4d8108
AM
15628+#include <linux/fs_stack.h>
15629+#include <linux/mman.h>
4a4d8108 15630+#include <linux/security.h>
dece6358
AM
15631+#include "aufs.h"
15632+
b912730e 15633+int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
1facf9fc 15634+{
4a4d8108
AM
15635+ int err;
15636+ aufs_bindex_t bindex;
8cdd5066 15637+ struct dentry *dentry, *h_dentry;
4a4d8108 15638+ struct au_finfo *finfo;
38d290e6 15639+ struct inode *h_inode;
4a4d8108
AM
15640+
15641+ FiMustWriteLock(file);
15642+
523b37e3 15643+ err = 0;
2000de60 15644+ dentry = file->f_path.dentry;
b912730e 15645+ AuDebugOn(IS_ERR_OR_NULL(dentry));
4a4d8108
AM
15646+ finfo = au_fi(file);
15647+ memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
2cbb1c4b 15648+ atomic_set(&finfo->fi_mmapped, 0);
5afbbe0d 15649+ bindex = au_dbtop(dentry);
8cdd5066
JR
15650+ if (!h_file) {
15651+ h_dentry = au_h_dptr(dentry, bindex);
15652+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15653+ if (unlikely(err))
15654+ goto out;
b912730e 15655+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
acd2b654
AM
15656+ if (IS_ERR(h_file)) {
15657+ err = PTR_ERR(h_file);
15658+ goto out;
15659+ }
8cdd5066
JR
15660+ } else {
15661+ h_dentry = h_file->f_path.dentry;
15662+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15663+ if (unlikely(err))
15664+ goto out;
acd2b654 15665+ /* br ref is already inc-ed */
8cdd5066 15666+ }
acd2b654
AM
15667+
15668+ if ((flags & __O_TMPFILE)
15669+ && !(flags & O_EXCL)) {
15670+ h_inode = file_inode(h_file);
15671+ spin_lock(&h_inode->i_lock);
15672+ h_inode->i_state |= I_LINKABLE;
15673+ spin_unlock(&h_inode->i_lock);
4a4d8108 15674+ }
acd2b654
AM
15675+ au_set_fbtop(file, bindex);
15676+ au_set_h_fptr(file, bindex, h_file);
15677+ au_update_figen(file);
15678+ /* todo: necessary? */
15679+ /* file->f_ra = h_file->f_ra; */
027c5e7a 15680+
8cdd5066 15681+out:
4a4d8108 15682+ return err;
1facf9fc 15683+}
15684+
4a4d8108
AM
15685+static int aufs_open_nondir(struct inode *inode __maybe_unused,
15686+ struct file *file)
1facf9fc 15687+{
4a4d8108 15688+ int err;
1308ab2a 15689+ struct super_block *sb;
b912730e
AM
15690+ struct au_do_open_args args = {
15691+ .open = au_do_open_nondir
15692+ };
1facf9fc 15693+
523b37e3
AM
15694+ AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15695+ file, vfsub_file_flags(file), file->f_mode);
1facf9fc 15696+
2000de60 15697+ sb = file->f_path.dentry->d_sb;
4a4d8108 15698+ si_read_lock(sb, AuLock_FLUSH);
b912730e 15699+ err = au_do_open(file, &args);
4a4d8108
AM
15700+ si_read_unlock(sb);
15701+ return err;
15702+}
1facf9fc 15703+
4a4d8108
AM
15704+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15705+{
15706+ struct au_finfo *finfo;
15707+ aufs_bindex_t bindex;
1facf9fc 15708+
4a4d8108 15709+ finfo = au_fi(file);
8b6a4947
AM
15710+ au_hbl_del(&finfo->fi_hlist,
15711+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108 15712+ bindex = finfo->fi_btop;
b4510431 15713+ if (bindex >= 0)
4a4d8108 15714+ au_set_h_fptr(file, bindex, NULL);
7f207e10 15715+
1c60b727 15716+ au_finfo_fin(file);
4a4d8108 15717+ return 0;
1facf9fc 15718+}
15719+
4a4d8108
AM
15720+/* ---------------------------------------------------------------------- */
15721+
15722+static int au_do_flush_nondir(struct file *file, fl_owner_t id)
dece6358 15723+{
1308ab2a 15724+ int err;
4a4d8108
AM
15725+ struct file *h_file;
15726+
15727+ err = 0;
15728+ h_file = au_hf_top(file);
15729+ if (h_file)
15730+ err = vfsub_flush(h_file, id);
15731+ return err;
15732+}
15733+
15734+static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15735+{
15736+ return au_do_flush(file, id, au_do_flush_nondir);
15737+}
15738+
15739+/* ---------------------------------------------------------------------- */
9dbd164d
AM
15740+/*
15741+ * read and write functions acquire [fdi]_rwsem once, but release before
15742+ * mmap_sem. This is because to stop a race condition between mmap(2).
acd2b654 15743+ * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
9dbd164d
AM
15744+ * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15745+ * read functions after [fdi]_rwsem are released, but it should be harmless.
15746+ */
4a4d8108 15747+
b912730e 15748+/* Callers should call au_read_post() or fput() in the end */
521ced18 15749+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
4a4d8108 15750+{
4a4d8108 15751+ struct file *h_file;
b912730e 15752+ int err;
1facf9fc 15753+
521ced18 15754+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
b912730e
AM
15755+ if (!err) {
15756+ di_read_unlock(file->f_path.dentry, AuLock_IR);
15757+ h_file = au_hf_top(file);
15758+ get_file(h_file);
15759+ if (!keep_fi)
15760+ fi_read_unlock(file);
15761+ } else
15762+ h_file = ERR_PTR(err);
15763+
15764+ return h_file;
15765+}
15766+
15767+static void au_read_post(struct inode *inode, struct file *h_file)
15768+{
15769+ /* update without lock, I don't think it a problem */
15770+ fsstack_copy_attr_atime(inode, file_inode(h_file));
15771+ fput(h_file);
15772+}
15773+
15774+struct au_write_pre {
521ced18
JR
15775+ /* input */
15776+ unsigned int lsc;
15777+
15778+ /* output */
b912730e 15779+ blkcnt_t blks;
5afbbe0d 15780+ aufs_bindex_t btop;
b912730e
AM
15781+};
15782+
15783+/*
15784+ * return with iinfo is write-locked
15785+ * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15786+ * end
15787+ */
15788+static struct file *au_write_pre(struct file *file, int do_ready,
15789+ struct au_write_pre *wpre)
15790+{
15791+ struct file *h_file;
15792+ struct dentry *dentry;
15793+ int err;
521ced18 15794+ unsigned int lsc;
b912730e
AM
15795+ struct au_pin pin;
15796+
521ced18
JR
15797+ lsc = 0;
15798+ if (wpre)
15799+ lsc = wpre->lsc;
15800+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
b912730e 15801+ h_file = ERR_PTR(err);
dece6358
AM
15802+ if (unlikely(err))
15803+ goto out;
1facf9fc 15804+
b912730e
AM
15805+ dentry = file->f_path.dentry;
15806+ if (do_ready) {
15807+ err = au_ready_to_write(file, -1, &pin);
15808+ if (unlikely(err)) {
15809+ h_file = ERR_PTR(err);
15810+ di_write_unlock(dentry);
15811+ goto out_fi;
15812+ }
15813+ }
15814+
15815+ di_downgrade_lock(dentry, /*flags*/0);
15816+ if (wpre)
5afbbe0d 15817+ wpre->btop = au_fbtop(file);
4a4d8108 15818+ h_file = au_hf_top(file);
9dbd164d 15819+ get_file(h_file);
b912730e
AM
15820+ if (wpre)
15821+ wpre->blks = file_inode(h_file)->i_blocks;
15822+ if (do_ready)
15823+ au_unpin(&pin);
15824+ di_read_unlock(dentry, /*flags*/0);
15825+
15826+out_fi:
15827+ fi_write_unlock(file);
15828+out:
15829+ return h_file;
15830+}
15831+
15832+static void au_write_post(struct inode *inode, struct file *h_file,
15833+ struct au_write_pre *wpre, ssize_t written)
15834+{
15835+ struct inode *h_inode;
15836+
15837+ au_cpup_attr_timesizes(inode);
5afbbe0d 15838+ AuDebugOn(au_ibtop(inode) != wpre->btop);
b912730e
AM
15839+ h_inode = file_inode(h_file);
15840+ inode->i_mode = h_inode->i_mode;
15841+ ii_write_unlock(inode);
b912730e
AM
15842+ /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15843+ if (written > 0)
5afbbe0d 15844+ au_fhsm_wrote(inode->i_sb, wpre->btop,
b912730e 15845+ /*force*/h_inode->i_blocks > wpre->blks);
1c60b727 15846+ fput(h_file);
b912730e
AM
15847+}
15848+
15849+static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
15850+ loff_t *ppos)
15851+{
15852+ ssize_t err;
15853+ struct inode *inode;
15854+ struct file *h_file;
15855+ struct super_block *sb;
15856+
15857+ inode = file_inode(file);
15858+ sb = inode->i_sb;
15859+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15860+
521ced18 15861+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
15862+ err = PTR_ERR(h_file);
15863+ if (IS_ERR(h_file))
15864+ goto out;
9dbd164d
AM
15865+
15866+ /* filedata may be obsoleted by concurrent copyup, but no problem */
4a4d8108
AM
15867+ err = vfsub_read_u(h_file, buf, count, ppos);
15868+ /* todo: necessary? */
15869+ /* file->f_ra = h_file->f_ra; */
b912730e 15870+ au_read_post(inode, h_file);
1308ab2a 15871+
4f0767ce 15872+out:
dece6358
AM
15873+ si_read_unlock(sb);
15874+ return err;
15875+}
1facf9fc 15876+
e49829fe
JR
15877+/*
15878+ * todo: very ugly
15879+ * it locks both of i_mutex and si_rwsem for read in safe.
15880+ * if the plink maintenance mode continues forever (that is the problem),
15881+ * may loop forever.
15882+ */
15883+static void au_mtx_and_read_lock(struct inode *inode)
15884+{
15885+ int err;
15886+ struct super_block *sb = inode->i_sb;
15887+
15888+ while (1) {
febd17d6 15889+ inode_lock(inode);
e49829fe
JR
15890+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15891+ if (!err)
15892+ break;
febd17d6 15893+ inode_unlock(inode);
e49829fe
JR
15894+ si_read_lock(sb, AuLock_NOPLMW);
15895+ si_read_unlock(sb);
15896+ }
15897+}
15898+
4a4d8108
AM
15899+static ssize_t aufs_write(struct file *file, const char __user *ubuf,
15900+ size_t count, loff_t *ppos)
dece6358 15901+{
4a4d8108 15902+ ssize_t err;
b912730e
AM
15903+ struct au_write_pre wpre;
15904+ struct inode *inode;
4a4d8108
AM
15905+ struct file *h_file;
15906+ char __user *buf = (char __user *)ubuf;
1facf9fc 15907+
b912730e 15908+ inode = file_inode(file);
e49829fe 15909+ au_mtx_and_read_lock(inode);
1facf9fc 15910+
521ced18 15911+ wpre.lsc = 0;
b912730e
AM
15912+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15913+ err = PTR_ERR(h_file);
15914+ if (IS_ERR(h_file))
9dbd164d 15915+ goto out;
9dbd164d 15916+
4a4d8108 15917+ err = vfsub_write_u(h_file, buf, count, ppos);
b912730e 15918+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15919+
4f0767ce 15920+out:
b912730e 15921+ si_read_unlock(inode->i_sb);
febd17d6 15922+ inode_unlock(inode);
dece6358
AM
15923+ return err;
15924+}
1facf9fc 15925+
076b876e
AM
15926+static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15927+ struct iov_iter *iov_iter)
dece6358 15928+{
4a4d8108
AM
15929+ ssize_t err;
15930+ struct file *file;
076b876e 15931+ ssize_t (*iter)(struct kiocb *, struct iov_iter *);
1facf9fc 15932+
4a4d8108
AM
15933+ err = security_file_permission(h_file, rw);
15934+ if (unlikely(err))
15935+ goto out;
1facf9fc 15936+
4a4d8108 15937+ err = -ENOSYS;
076b876e 15938+ iter = NULL;
5527c038 15939+ if (rw == MAY_READ)
076b876e 15940+ iter = h_file->f_op->read_iter;
5527c038 15941+ else if (rw == MAY_WRITE)
076b876e 15942+ iter = h_file->f_op->write_iter;
076b876e
AM
15943+
15944+ file = kio->ki_filp;
15945+ kio->ki_filp = h_file;
15946+ if (iter) {
2cbb1c4b 15947+ lockdep_off();
076b876e
AM
15948+ err = iter(kio, iov_iter);
15949+ lockdep_on();
4a4d8108
AM
15950+ } else
15951+ /* currently there is no such fs */
15952+ WARN_ON_ONCE(1);
076b876e 15953+ kio->ki_filp = file;
1facf9fc 15954+
4f0767ce 15955+out:
dece6358
AM
15956+ return err;
15957+}
1facf9fc 15958+
076b876e 15959+static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1facf9fc 15960+{
4a4d8108
AM
15961+ ssize_t err;
15962+ struct file *file, *h_file;
b912730e 15963+ struct inode *inode;
dece6358 15964+ struct super_block *sb;
1facf9fc 15965+
4a4d8108 15966+ file = kio->ki_filp;
b912730e
AM
15967+ inode = file_inode(file);
15968+ sb = inode->i_sb;
e49829fe 15969+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 15970+
521ced18 15971+ h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
b912730e
AM
15972+ err = PTR_ERR(h_file);
15973+ if (IS_ERR(h_file))
15974+ goto out;
9dbd164d 15975+
5afbbe0d
AM
15976+ if (au_test_loopback_kthread()) {
15977+ au_warn_loopback(h_file->f_path.dentry->d_sb);
15978+ if (file->f_mapping != h_file->f_mapping) {
15979+ file->f_mapping = h_file->f_mapping;
15980+ smp_mb(); /* unnecessary? */
15981+ }
15982+ }
15983+ fi_read_unlock(file);
15984+
076b876e 15985+ err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
4a4d8108
AM
15986+ /* todo: necessary? */
15987+ /* file->f_ra = h_file->f_ra; */
b912730e 15988+ au_read_post(inode, h_file);
1facf9fc 15989+
4f0767ce 15990+out:
4a4d8108 15991+ si_read_unlock(sb);
1308ab2a 15992+ return err;
15993+}
1facf9fc 15994+
076b876e 15995+static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1308ab2a 15996+{
4a4d8108 15997+ ssize_t err;
b912730e
AM
15998+ struct au_write_pre wpre;
15999+ struct inode *inode;
4a4d8108 16000+ struct file *file, *h_file;
1308ab2a 16001+
4a4d8108 16002+ file = kio->ki_filp;
b912730e 16003+ inode = file_inode(file);
e49829fe
JR
16004+ au_mtx_and_read_lock(inode);
16005+
521ced18 16006+ wpre.lsc = 0;
b912730e
AM
16007+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16008+ err = PTR_ERR(h_file);
16009+ if (IS_ERR(h_file))
9dbd164d 16010+ goto out;
9dbd164d 16011+
076b876e 16012+ err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
b912730e 16013+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 16014+
4f0767ce 16015+out:
b912730e 16016+ si_read_unlock(inode->i_sb);
febd17d6 16017+ inode_unlock(inode);
dece6358 16018+ return err;
1facf9fc 16019+}
16020+
4a4d8108
AM
16021+static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
16022+ struct pipe_inode_info *pipe, size_t len,
16023+ unsigned int flags)
1facf9fc 16024+{
4a4d8108
AM
16025+ ssize_t err;
16026+ struct file *h_file;
b912730e 16027+ struct inode *inode;
dece6358 16028+ struct super_block *sb;
1facf9fc 16029+
b912730e
AM
16030+ inode = file_inode(file);
16031+ sb = inode->i_sb;
e49829fe 16032+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 16033+
521ced18 16034+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
16035+ err = PTR_ERR(h_file);
16036+ if (IS_ERR(h_file))
dece6358 16037+ goto out;
1facf9fc 16038+
4a4d8108 16039+ err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
acd2b654 16040+ /* todo: necessary? */
4a4d8108 16041+ /* file->f_ra = h_file->f_ra; */
b912730e 16042+ au_read_post(inode, h_file);
1facf9fc 16043+
4f0767ce 16044+out:
4a4d8108 16045+ si_read_unlock(sb);
dece6358 16046+ return err;
1facf9fc 16047+}
16048+
4a4d8108
AM
16049+static ssize_t
16050+aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
16051+ size_t len, unsigned int flags)
1facf9fc 16052+{
4a4d8108 16053+ ssize_t err;
b912730e
AM
16054+ struct au_write_pre wpre;
16055+ struct inode *inode;
076b876e 16056+ struct file *h_file;
1facf9fc 16057+
b912730e 16058+ inode = file_inode(file);
e49829fe 16059+ au_mtx_and_read_lock(inode);
9dbd164d 16060+
521ced18 16061+ wpre.lsc = 0;
b912730e
AM
16062+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16063+ err = PTR_ERR(h_file);
16064+ if (IS_ERR(h_file))
9dbd164d 16065+ goto out;
9dbd164d 16066+
4a4d8108 16067+ err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
b912730e 16068+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 16069+
4f0767ce 16070+out:
b912730e 16071+ si_read_unlock(inode->i_sb);
febd17d6 16072+ inode_unlock(inode);
4a4d8108
AM
16073+ return err;
16074+}
1facf9fc 16075+
38d290e6
JR
16076+static long aufs_fallocate(struct file *file, int mode, loff_t offset,
16077+ loff_t len)
16078+{
16079+ long err;
b912730e 16080+ struct au_write_pre wpre;
38d290e6
JR
16081+ struct inode *inode;
16082+ struct file *h_file;
16083+
b912730e 16084+ inode = file_inode(file);
38d290e6
JR
16085+ au_mtx_and_read_lock(inode);
16086+
521ced18 16087+ wpre.lsc = 0;
b912730e
AM
16088+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16089+ err = PTR_ERR(h_file);
16090+ if (IS_ERR(h_file))
38d290e6 16091+ goto out;
38d290e6
JR
16092+
16093+ lockdep_off();
03673fb0 16094+ err = vfs_fallocate(h_file, mode, offset, len);
38d290e6 16095+ lockdep_on();
b912730e 16096+ au_write_post(inode, h_file, &wpre, /*written*/1);
38d290e6
JR
16097+
16098+out:
b912730e 16099+ si_read_unlock(inode->i_sb);
febd17d6 16100+ inode_unlock(inode);
38d290e6
JR
16101+ return err;
16102+}
16103+
521ced18
JR
16104+static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
16105+ struct file *dst, loff_t dst_pos,
16106+ size_t len, unsigned int flags)
16107+{
16108+ ssize_t err;
16109+ struct au_write_pre wpre;
16110+ enum { SRC, DST };
16111+ struct {
16112+ struct inode *inode;
16113+ struct file *h_file;
16114+ struct super_block *h_sb;
16115+ } a[2];
16116+#define a_src a[SRC]
16117+#define a_dst a[DST]
16118+
16119+ err = -EINVAL;
16120+ a_src.inode = file_inode(src);
16121+ if (unlikely(!S_ISREG(a_src.inode->i_mode)))
16122+ goto out;
16123+ a_dst.inode = file_inode(dst);
16124+ if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
16125+ goto out;
16126+
16127+ au_mtx_and_read_lock(a_dst.inode);
16128+ /*
16129+ * in order to match the order in di_write_lock2_{child,parent}(),
acd2b654 16130+ * use f_path.dentry for this comparison.
521ced18
JR
16131+ */
16132+ if (src->f_path.dentry < dst->f_path.dentry) {
16133+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
16134+ err = PTR_ERR(a_src.h_file);
16135+ if (IS_ERR(a_src.h_file))
16136+ goto out_si;
16137+
16138+ wpre.lsc = AuLsc_FI_2;
16139+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16140+ err = PTR_ERR(a_dst.h_file);
16141+ if (IS_ERR(a_dst.h_file)) {
16142+ au_read_post(a_src.inode, a_src.h_file);
16143+ goto out_si;
16144+ }
16145+ } else {
16146+ wpre.lsc = AuLsc_FI_1;
16147+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16148+ err = PTR_ERR(a_dst.h_file);
16149+ if (IS_ERR(a_dst.h_file))
16150+ goto out_si;
16151+
16152+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
16153+ err = PTR_ERR(a_src.h_file);
16154+ if (IS_ERR(a_src.h_file)) {
16155+ au_write_post(a_dst.inode, a_dst.h_file, &wpre,
16156+ /*written*/0);
16157+ goto out_si;
16158+ }
16159+ }
16160+
16161+ err = -EXDEV;
16162+ a_src.h_sb = file_inode(a_src.h_file)->i_sb;
16163+ a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
16164+ if (unlikely(a_src.h_sb != a_dst.h_sb)) {
16165+ AuDbgFile(src);
16166+ AuDbgFile(dst);
16167+ goto out_file;
16168+ }
16169+
16170+ err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
16171+ dst_pos, len, flags);
16172+
16173+out_file:
16174+ au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
16175+ fi_read_unlock(src);
16176+ au_read_post(a_src.inode, a_src.h_file);
16177+out_si:
16178+ si_read_unlock(a_dst.inode->i_sb);
16179+ inode_unlock(a_dst.inode);
16180+out:
16181+ return err;
16182+#undef a_src
16183+#undef a_dst
16184+}
16185+
4a4d8108
AM
16186+/* ---------------------------------------------------------------------- */
16187+
9dbd164d
AM
16188+/*
16189+ * The locking order around current->mmap_sem.
16190+ * - in most and regular cases
16191+ * file I/O syscall -- aufs_read() or something
16192+ * -- si_rwsem for read -- mmap_sem
16193+ * (Note that [fdi]i_rwsem are released before mmap_sem).
16194+ * - in mmap case
16195+ * mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
acd2b654
AM
16196+ * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
16197+ * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
16198+ * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
9dbd164d
AM
16199+ * It means that when aufs acquires si_rwsem for write, the process should never
16200+ * acquire mmap_sem.
16201+ *
392086de 16202+ * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
9dbd164d
AM
16203+ * problem either since any directory is not able to be mmap-ed.
16204+ * The similar scenario is applied to aufs_readlink() too.
16205+ */
16206+
38d290e6 16207+#if 0 /* stop calling security_file_mmap() */
2dfbb274
AM
16208+/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
16209+#define AuConv_VM_PROT(f, b) _calc_vm_trans(f, VM_##b, PROT_##b)
16210+
16211+static unsigned long au_arch_prot_conv(unsigned long flags)
16212+{
16213+ /* currently ppc64 only */
16214+#ifdef CONFIG_PPC64
16215+ /* cf. linux/arch/powerpc/include/asm/mman.h */
16216+ AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
16217+ return AuConv_VM_PROT(flags, SAO);
16218+#else
16219+ AuDebugOn(arch_calc_vm_prot_bits(-1));
16220+ return 0;
16221+#endif
16222+}
16223+
16224+static unsigned long au_prot_conv(unsigned long flags)
16225+{
16226+ return AuConv_VM_PROT(flags, READ)
16227+ | AuConv_VM_PROT(flags, WRITE)
16228+ | AuConv_VM_PROT(flags, EXEC)
16229+ | au_arch_prot_conv(flags);
16230+}
16231+
16232+/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16233+#define AuConv_VM_MAP(f, b) _calc_vm_trans(f, VM_##b, MAP_##b)
16234+
16235+static unsigned long au_flag_conv(unsigned long flags)
16236+{
16237+ return AuConv_VM_MAP(flags, GROWSDOWN)
16238+ | AuConv_VM_MAP(flags, DENYWRITE)
2dfbb274
AM
16239+ | AuConv_VM_MAP(flags, LOCKED);
16240+}
38d290e6 16241+#endif
2dfbb274 16242+
9dbd164d 16243+static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
dece6358 16244+{
4a4d8108 16245+ int err;
4a4d8108 16246+ const unsigned char wlock
9dbd164d 16247+ = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
4a4d8108 16248+ struct super_block *sb;
9dbd164d 16249+ struct file *h_file;
b912730e 16250+ struct inode *inode;
9dbd164d
AM
16251+
16252+ AuDbgVmRegion(file, vma);
1308ab2a 16253+
b912730e
AM
16254+ inode = file_inode(file);
16255+ sb = inode->i_sb;
9dbd164d 16256+ lockdep_off();
e49829fe 16257+ si_read_lock(sb, AuLock_NOPLMW);
4a4d8108 16258+
b912730e 16259+ h_file = au_write_pre(file, wlock, /*wpre*/NULL);
9dbd164d 16260+ lockdep_on();
b912730e
AM
16261+ err = PTR_ERR(h_file);
16262+ if (IS_ERR(h_file))
16263+ goto out;
1308ab2a 16264+
b912730e
AM
16265+ err = 0;
16266+ au_set_mmapped(file);
9dbd164d 16267+ au_vm_file_reset(vma, h_file);
38d290e6
JR
16268+ /*
16269+ * we cannot call security_mmap_file() here since it may acquire
16270+ * mmap_sem or i_mutex.
16271+ *
16272+ * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16273+ * au_flag_conv(vma->vm_flags));
16274+ */
9dbd164d 16275+ if (!err)
521ced18 16276+ err = call_mmap(h_file, vma);
b912730e
AM
16277+ if (!err) {
16278+ au_vm_prfile_set(vma, file);
16279+ fsstack_copy_attr_atime(inode, file_inode(h_file));
16280+ goto out_fput; /* success */
16281+ }
2cbb1c4b
JR
16282+ au_unset_mmapped(file);
16283+ au_vm_file_reset(vma, file);
b912730e 16284+
2cbb1c4b 16285+out_fput:
9dbd164d 16286+ lockdep_off();
b912730e
AM
16287+ ii_write_unlock(inode);
16288+ lockdep_on();
16289+ fput(h_file);
4f0767ce 16290+out:
b912730e 16291+ lockdep_off();
9dbd164d
AM
16292+ si_read_unlock(sb);
16293+ lockdep_on();
16294+ AuTraceErr(err);
4a4d8108
AM
16295+ return err;
16296+}
16297+
16298+/* ---------------------------------------------------------------------- */
16299+
1e00d052
AM
16300+static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16301+ int datasync)
4a4d8108
AM
16302+{
16303+ int err;
b912730e 16304+ struct au_write_pre wpre;
4a4d8108
AM
16305+ struct inode *inode;
16306+ struct file *h_file;
4a4d8108
AM
16307+
16308+ err = 0; /* -EBADF; */ /* posix? */
16309+ if (unlikely(!(file->f_mode & FMODE_WRITE)))
b912730e 16310+ goto out;
4a4d8108 16311+
b912730e
AM
16312+ inode = file_inode(file);
16313+ au_mtx_and_read_lock(inode);
16314+
521ced18 16315+ wpre.lsc = 0;
b912730e
AM
16316+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16317+ err = PTR_ERR(h_file);
16318+ if (IS_ERR(h_file))
4a4d8108 16319+ goto out_unlock;
4a4d8108 16320+
53392da6 16321+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
b912730e 16322+ au_write_post(inode, h_file, &wpre, /*written*/0);
4a4d8108 16323+
4f0767ce 16324+out_unlock:
b912730e 16325+ si_read_unlock(inode->i_sb);
febd17d6 16326+ inode_unlock(inode);
b912730e 16327+out:
4a4d8108 16328+ return err;
dece6358
AM
16329+}
16330+
4a4d8108 16331+static int aufs_fasync(int fd, struct file *file, int flag)
dece6358 16332+{
4a4d8108
AM
16333+ int err;
16334+ struct file *h_file;
4a4d8108 16335+ struct super_block *sb;
1308ab2a 16336+
b912730e 16337+ sb = file->f_path.dentry->d_sb;
e49829fe 16338+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 16339+
521ced18 16340+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
16341+ err = PTR_ERR(h_file);
16342+ if (IS_ERR(h_file))
4a4d8108
AM
16343+ goto out;
16344+
523b37e3 16345+ if (h_file->f_op->fasync)
4a4d8108 16346+ err = h_file->f_op->fasync(fd, h_file, flag);
b912730e 16347+ fput(h_file); /* instead of au_read_post() */
1308ab2a 16348+
4f0767ce 16349+out:
4a4d8108 16350+ si_read_unlock(sb);
1308ab2a 16351+ return err;
dece6358 16352+}
4a4d8108 16353+
febd17d6
JR
16354+static int aufs_setfl(struct file *file, unsigned long arg)
16355+{
16356+ int err;
16357+ struct file *h_file;
16358+ struct super_block *sb;
16359+
16360+ sb = file->f_path.dentry->d_sb;
16361+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16362+
521ced18 16363+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
febd17d6
JR
16364+ err = PTR_ERR(h_file);
16365+ if (IS_ERR(h_file))
16366+ goto out;
16367+
1c60b727
AM
16368+ /* stop calling h_file->fasync */
16369+ arg |= vfsub_file_flags(file) & FASYNC;
febd17d6
JR
16370+ err = setfl(/*unused fd*/-1, h_file, arg);
16371+ fput(h_file); /* instead of au_read_post() */
16372+
16373+out:
16374+ si_read_unlock(sb);
16375+ return err;
16376+}
16377+
4a4d8108
AM
16378+/* ---------------------------------------------------------------------- */
16379+
16380+/* no one supports this operation, currently */
16381+#if 0
16382+static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
2000de60 16383+ size_t len, loff_t *pos, int more)
4a4d8108
AM
16384+{
16385+}
16386+#endif
16387+
16388+/* ---------------------------------------------------------------------- */
16389+
16390+const struct file_operations aufs_file_fop = {
16391+ .owner = THIS_MODULE,
2cbb1c4b 16392+
027c5e7a 16393+ .llseek = default_llseek,
4a4d8108
AM
16394+
16395+ .read = aufs_read,
16396+ .write = aufs_write,
076b876e
AM
16397+ .read_iter = aufs_read_iter,
16398+ .write_iter = aufs_write_iter,
16399+
4a4d8108
AM
16400+#ifdef CONFIG_AUFS_POLL
16401+ .poll = aufs_poll,
16402+#endif
16403+ .unlocked_ioctl = aufs_ioctl_nondir,
b752ccd1 16404+#ifdef CONFIG_COMPAT
c2b27bf2 16405+ .compat_ioctl = aufs_compat_ioctl_nondir,
b752ccd1 16406+#endif
4a4d8108
AM
16407+ .mmap = aufs_mmap,
16408+ .open = aufs_open_nondir,
16409+ .flush = aufs_flush_nondir,
16410+ .release = aufs_release_nondir,
16411+ .fsync = aufs_fsync_nondir,
4a4d8108
AM
16412+ .fasync = aufs_fasync,
16413+ /* .sendpage = aufs_sendpage, */
febd17d6 16414+ .setfl = aufs_setfl,
4a4d8108
AM
16415+ .splice_write = aufs_splice_write,
16416+ .splice_read = aufs_splice_read,
16417+#if 0
16418+ .aio_splice_write = aufs_aio_splice_write,
38d290e6 16419+ .aio_splice_read = aufs_aio_splice_read,
4a4d8108 16420+#endif
521ced18
JR
16421+ .fallocate = aufs_fallocate,
16422+ .copy_file_range = aufs_copy_file_range
4a4d8108 16423+};
7f207e10
AM
16424diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
16425--- /usr/share/empty/fs/aufs/fstype.h 1970-01-01 01:00:00.000000000 +0100
ba1aed25 16426+++ linux/fs/aufs/fstype.h 2019-03-05 12:13:00.139224339 +0100
062440b3
AM
16427@@ -0,0 +1,401 @@
16428+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 16429+/*
ba1aed25 16430+ * Copyright (C) 2005-2019 Junjiro R. Okajima
4a4d8108
AM
16431+ *
16432+ * This program, aufs is free software; you can redistribute it and/or modify
16433+ * it under the terms of the GNU General Public License as published by
16434+ * the Free Software Foundation; either version 2 of the License, or
16435+ * (at your option) any later version.
16436+ *
16437+ * This program is distributed in the hope that it will be useful,
16438+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16439+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16440+ * GNU General Public License for more details.
16441+ *
16442+ * You should have received a copy of the GNU General Public License
523b37e3 16443+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
16444+ */
16445+
16446+/*
16447+ * judging filesystem type
16448+ */
16449+
16450+#ifndef __AUFS_FSTYPE_H__
16451+#define __AUFS_FSTYPE_H__
16452+
16453+#ifdef __KERNEL__
16454+
16455+#include <linux/fs.h>
16456+#include <linux/magic.h>
b912730e 16457+#include <linux/nfs_fs.h>
b95c5147 16458+#include <linux/romfs_fs.h>
4a4d8108
AM
16459+
16460+static inline int au_test_aufs(struct super_block *sb)
16461+{
16462+ return sb->s_magic == AUFS_SUPER_MAGIC;
16463+}
16464+
16465+static inline const char *au_sbtype(struct super_block *sb)
16466+{
16467+ return sb->s_type->name;
16468+}
1308ab2a 16469+
16470+static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
16471+{
f0c0a007 16472+#if IS_ENABLED(CONFIG_ISO9660_FS)
2000de60 16473+ return sb->s_magic == ISOFS_SUPER_MAGIC;
dece6358
AM
16474+#else
16475+ return 0;
16476+#endif
16477+}
16478+
1308ab2a 16479+static inline int au_test_romfs(struct super_block *sb __maybe_unused)
dece6358 16480+{
f0c0a007 16481+#if IS_ENABLED(CONFIG_ROMFS_FS)
2000de60 16482+ return sb->s_magic == ROMFS_MAGIC;
dece6358
AM
16483+#else
16484+ return 0;
16485+#endif
16486+}
16487+
1308ab2a 16488+static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
dece6358 16489+{
f0c0a007 16490+#if IS_ENABLED(CONFIG_CRAMFS)
1308ab2a 16491+ return sb->s_magic == CRAMFS_MAGIC;
16492+#endif
16493+ return 0;
16494+}
16495+
16496+static inline int au_test_nfs(struct super_block *sb __maybe_unused)
16497+{
f0c0a007 16498+#if IS_ENABLED(CONFIG_NFS_FS)
1308ab2a 16499+ return sb->s_magic == NFS_SUPER_MAGIC;
dece6358
AM
16500+#else
16501+ return 0;
16502+#endif
16503+}
16504+
1308ab2a 16505+static inline int au_test_fuse(struct super_block *sb __maybe_unused)
dece6358 16506+{
f0c0a007 16507+#if IS_ENABLED(CONFIG_FUSE_FS)
1308ab2a 16508+ return sb->s_magic == FUSE_SUPER_MAGIC;
dece6358
AM
16509+#else
16510+ return 0;
16511+#endif
16512+}
16513+
1308ab2a 16514+static inline int au_test_xfs(struct super_block *sb __maybe_unused)
dece6358 16515+{
f0c0a007 16516+#if IS_ENABLED(CONFIG_XFS_FS)
1308ab2a 16517+ return sb->s_magic == XFS_SB_MAGIC;
dece6358
AM
16518+#else
16519+ return 0;
16520+#endif
16521+}
16522+
1308ab2a 16523+static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
dece6358 16524+{
1308ab2a 16525+#ifdef CONFIG_TMPFS
16526+ return sb->s_magic == TMPFS_MAGIC;
16527+#else
16528+ return 0;
dece6358 16529+#endif
dece6358
AM
16530+}
16531+
1308ab2a 16532+static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
1facf9fc 16533+{
f0c0a007 16534+#if IS_ENABLED(CONFIG_ECRYPT_FS)
1308ab2a 16535+ return !strcmp(au_sbtype(sb), "ecryptfs");
16536+#else
16537+ return 0;
16538+#endif
1facf9fc 16539+}
16540+
1308ab2a 16541+static inline int au_test_ramfs(struct super_block *sb)
16542+{
16543+ return sb->s_magic == RAMFS_MAGIC;
16544+}
16545+
16546+static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
16547+{
f0c0a007 16548+#if IS_ENABLED(CONFIG_UBIFS_FS)
1308ab2a 16549+ return sb->s_magic == UBIFS_SUPER_MAGIC;
16550+#else
16551+ return 0;
16552+#endif
16553+}
16554+
16555+static inline int au_test_procfs(struct super_block *sb __maybe_unused)
16556+{
16557+#ifdef CONFIG_PROC_FS
16558+ return sb->s_magic == PROC_SUPER_MAGIC;
16559+#else
16560+ return 0;
16561+#endif
16562+}
16563+
16564+static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
16565+{
16566+#ifdef CONFIG_SYSFS
16567+ return sb->s_magic == SYSFS_MAGIC;
16568+#else
16569+ return 0;
16570+#endif
16571+}
16572+
16573+static inline int au_test_configfs(struct super_block *sb __maybe_unused)
16574+{
f0c0a007 16575+#if IS_ENABLED(CONFIG_CONFIGFS_FS)
1308ab2a 16576+ return sb->s_magic == CONFIGFS_MAGIC;
16577+#else
16578+ return 0;
16579+#endif
16580+}
16581+
16582+static inline int au_test_minix(struct super_block *sb __maybe_unused)
16583+{
f0c0a007 16584+#if IS_ENABLED(CONFIG_MINIX_FS)
1308ab2a 16585+ return sb->s_magic == MINIX3_SUPER_MAGIC
16586+ || sb->s_magic == MINIX2_SUPER_MAGIC
16587+ || sb->s_magic == MINIX2_SUPER_MAGIC2
16588+ || sb->s_magic == MINIX_SUPER_MAGIC
16589+ || sb->s_magic == MINIX_SUPER_MAGIC2;
16590+#else
16591+ return 0;
16592+#endif
16593+}
16594+
1308ab2a 16595+static inline int au_test_fat(struct super_block *sb __maybe_unused)
16596+{
f0c0a007 16597+#if IS_ENABLED(CONFIG_FAT_FS)
1308ab2a 16598+ return sb->s_magic == MSDOS_SUPER_MAGIC;
16599+#else
16600+ return 0;
16601+#endif
16602+}
16603+
16604+static inline int au_test_msdos(struct super_block *sb)
16605+{
16606+ return au_test_fat(sb);
16607+}
16608+
16609+static inline int au_test_vfat(struct super_block *sb)
16610+{
16611+ return au_test_fat(sb);
16612+}
16613+
16614+static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
16615+{
16616+#ifdef CONFIG_SECURITYFS
16617+ return sb->s_magic == SECURITYFS_MAGIC;
16618+#else
16619+ return 0;
16620+#endif
16621+}
16622+
16623+static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
16624+{
f0c0a007 16625+#if IS_ENABLED(CONFIG_SQUASHFS)
1308ab2a 16626+ return sb->s_magic == SQUASHFS_MAGIC;
16627+#else
16628+ return 0;
16629+#endif
16630+}
16631+
16632+static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
16633+{
f0c0a007 16634+#if IS_ENABLED(CONFIG_BTRFS_FS)
1308ab2a 16635+ return sb->s_magic == BTRFS_SUPER_MAGIC;
16636+#else
16637+ return 0;
16638+#endif
16639+}
16640+
16641+static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
16642+{
f0c0a007 16643+#if IS_ENABLED(CONFIG_XENFS)
1308ab2a 16644+ return sb->s_magic == XENFS_SUPER_MAGIC;
16645+#else
16646+ return 0;
16647+#endif
16648+}
16649+
16650+static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
16651+{
16652+#ifdef CONFIG_DEBUG_FS
16653+ return sb->s_magic == DEBUGFS_MAGIC;
16654+#else
16655+ return 0;
16656+#endif
16657+}
16658+
16659+static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
16660+{
f0c0a007 16661+#if IS_ENABLED(CONFIG_NILFS)
1308ab2a 16662+ return sb->s_magic == NILFS_SUPER_MAGIC;
16663+#else
16664+ return 0;
16665+#endif
16666+}
16667+
4a4d8108
AM
16668+static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
16669+{
f0c0a007 16670+#if IS_ENABLED(CONFIG_HFSPLUS_FS)
4a4d8108
AM
16671+ return sb->s_magic == HFSPLUS_SUPER_MAGIC;
16672+#else
16673+ return 0;
16674+#endif
16675+}
16676+
1308ab2a 16677+/* ---------------------------------------------------------------------- */
16678+/*
16679+ * they can't be an aufs branch.
16680+ */
16681+static inline int au_test_fs_unsuppoted(struct super_block *sb)
16682+{
16683+ return
16684+#ifndef CONFIG_AUFS_BR_RAMFS
16685+ au_test_ramfs(sb) ||
16686+#endif
16687+ au_test_procfs(sb)
16688+ || au_test_sysfs(sb)
16689+ || au_test_configfs(sb)
16690+ || au_test_debugfs(sb)
16691+ || au_test_securityfs(sb)
16692+ || au_test_xenfs(sb)
16693+ || au_test_ecryptfs(sb)
16694+ /* || !strcmp(au_sbtype(sb), "unionfs") */
16695+ || au_test_aufs(sb); /* will be supported in next version */
16696+}
16697+
1308ab2a 16698+static inline int au_test_fs_remote(struct super_block *sb)
16699+{
16700+ return !au_test_tmpfs(sb)
16701+#ifdef CONFIG_AUFS_BR_RAMFS
16702+ && !au_test_ramfs(sb)
16703+#endif
16704+ && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
16705+}
16706+
16707+/* ---------------------------------------------------------------------- */
16708+
16709+/*
16710+ * Note: these functions (below) are created after reading ->getattr() in all
16711+ * filesystems under linux/fs. it means we have to do so in every update...
16712+ */
16713+
16714+/*
16715+ * some filesystems require getattr to refresh the inode attributes before
16716+ * referencing.
16717+ * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
16718+ * and leave the work for d_revalidate()
16719+ */
16720+static inline int au_test_fs_refresh_iattr(struct super_block *sb)
16721+{
16722+ return au_test_nfs(sb)
16723+ || au_test_fuse(sb)
1308ab2a 16724+ /* || au_test_btrfs(sb) */ /* untested */
1308ab2a 16725+ ;
16726+}
16727+
16728+/*
16729+ * filesystems which don't maintain i_size or i_blocks.
16730+ */
16731+static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
16732+{
16733+ return au_test_xfs(sb)
4a4d8108
AM
16734+ || au_test_btrfs(sb)
16735+ || au_test_ubifs(sb)
16736+ || au_test_hfsplus(sb) /* maintained, but incorrect */
1308ab2a 16737+ /* || au_test_minix(sb) */ /* untested */
16738+ ;
16739+}
16740+
16741+/*
16742+ * filesystems which don't store the correct value in some of their inode
16743+ * attributes.
16744+ */
16745+static inline int au_test_fs_bad_iattr(struct super_block *sb)
16746+{
16747+ return au_test_fs_bad_iattr_size(sb)
1308ab2a 16748+ || au_test_fat(sb)
16749+ || au_test_msdos(sb)
16750+ || au_test_vfat(sb);
1facf9fc 16751+}
16752+
16753+/* they don't check i_nlink in link(2) */
16754+static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
16755+{
16756+ return au_test_tmpfs(sb)
16757+#ifdef CONFIG_AUFS_BR_RAMFS
16758+ || au_test_ramfs(sb)
16759+#endif
4a4d8108 16760+ || au_test_ubifs(sb)
4a4d8108 16761+ || au_test_hfsplus(sb);
1facf9fc 16762+}
16763+
16764+/*
16765+ * filesystems which sets S_NOATIME and S_NOCMTIME.
16766+ */
16767+static inline int au_test_fs_notime(struct super_block *sb)
16768+{
16769+ return au_test_nfs(sb)
16770+ || au_test_fuse(sb)
dece6358 16771+ || au_test_ubifs(sb)
1facf9fc 16772+ ;
16773+}
16774+
1facf9fc 16775+/* temporary support for i#1 in cramfs */
16776+static inline int au_test_fs_unique_ino(struct inode *inode)
16777+{
16778+ if (au_test_cramfs(inode->i_sb))
16779+ return inode->i_ino != 1;
16780+ return 1;
16781+}
16782+
16783+/* ---------------------------------------------------------------------- */
16784+
16785+/*
16786+ * the filesystem where the xino files placed must support i/o after unlink and
16787+ * maintain i_size and i_blocks.
16788+ */
16789+static inline int au_test_fs_bad_xino(struct super_block *sb)
16790+{
16791+ return au_test_fs_remote(sb)
16792+ || au_test_fs_bad_iattr_size(sb)
1facf9fc 16793+ /* don't want unnecessary work for xino */
16794+ || au_test_aufs(sb)
1308ab2a 16795+ || au_test_ecryptfs(sb)
16796+ || au_test_nilfs(sb);
1facf9fc 16797+}
16798+
16799+static inline int au_test_fs_trunc_xino(struct super_block *sb)
16800+{
16801+ return au_test_tmpfs(sb)
16802+ || au_test_ramfs(sb);
16803+}
16804+
16805+/*
16806+ * test if the @sb is real-readonly.
16807+ */
16808+static inline int au_test_fs_rr(struct super_block *sb)
16809+{
16810+ return au_test_squashfs(sb)
16811+ || au_test_iso9660(sb)
16812+ || au_test_cramfs(sb)
16813+ || au_test_romfs(sb);
16814+}
16815+
b912730e
AM
16816+/*
16817+ * test if the @inode is nfs with 'noacl' option
2121bcd9 16818+ * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
b912730e
AM
16819+ */
16820+static inline int au_test_nfs_noacl(struct inode *inode)
16821+{
16822+ return au_test_nfs(inode->i_sb)
16823+ /* && IS_POSIXACL(inode) */
16824+ && !nfs_server_capable(inode, NFS_CAP_ACLS);
16825+}
16826+
1facf9fc 16827+#endif /* __KERNEL__ */
16828+#endif /* __AUFS_FSTYPE_H__ */
8b6a4947
AM
16829diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
16830--- /usr/share/empty/fs/aufs/hbl.h 1970-01-01 01:00:00.000000000 +0100
ba1aed25 16831+++ linux/fs/aufs/hbl.h 2019-03-05 12:13:00.139224339 +0100
062440b3
AM
16832@@ -0,0 +1,65 @@
16833+/* SPDX-License-Identifier: GPL-2.0 */
8b6a4947 16834+/*
ba1aed25 16835+ * Copyright (C) 2017-2019 Junjiro R. Okajima
8b6a4947
AM
16836+ *
16837+ * This program, aufs is free software; you can redistribute it and/or modify
16838+ * it under the terms of the GNU General Public License as published by
16839+ * the Free Software Foundation; either version 2 of the License, or
16840+ * (at your option) any later version.
16841+ *
16842+ * This program is distributed in the hope that it will be useful,
16843+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16844+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16845+ * GNU General Public License for more details.
16846+ *
16847+ * You should have received a copy of the GNU General Public License
16848+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16849+ */
16850+
16851+/*
16852+ * helpers for hlist_bl.h
16853+ */
16854+
16855+#ifndef __AUFS_HBL_H__
16856+#define __AUFS_HBL_H__
16857+
16858+#ifdef __KERNEL__
16859+
16860+#include <linux/list_bl.h>
16861+
16862+static inline void au_hbl_add(struct hlist_bl_node *node,
16863+ struct hlist_bl_head *hbl)
16864+{
16865+ hlist_bl_lock(hbl);
16866+ hlist_bl_add_head(node, hbl);
16867+ hlist_bl_unlock(hbl);
16868+}
16869+
16870+static inline void au_hbl_del(struct hlist_bl_node *node,
16871+ struct hlist_bl_head *hbl)
16872+{
16873+ hlist_bl_lock(hbl);
16874+ hlist_bl_del(node);
16875+ hlist_bl_unlock(hbl);
16876+}
16877+
16878+#define au_hbl_for_each(pos, head) \
16879+ for (pos = hlist_bl_first(head); \
16880+ pos; \
16881+ pos = pos->next)
16882+
16883+static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
16884+{
16885+ unsigned long cnt;
16886+ struct hlist_bl_node *pos;
16887+
16888+ cnt = 0;
16889+ hlist_bl_lock(hbl);
16890+ au_hbl_for_each(pos, hbl)
16891+ cnt++;
16892+ hlist_bl_unlock(hbl);
16893+ return cnt;
16894+}
16895+
16896+#endif /* __KERNEL__ */
16897+#endif /* __AUFS_HBL_H__ */
7f207e10
AM
16898diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
16899--- /usr/share/empty/fs/aufs/hfsnotify.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 16900+++ linux/fs/aufs/hfsnotify.c 2019-03-05 12:13:00.139224339 +0100
acd2b654 16901@@ -0,0 +1,289 @@
cd7a4cd9 16902+// SPDX-License-Identifier: GPL-2.0
1facf9fc 16903+/*
ba1aed25 16904+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 16905+ *
16906+ * This program, aufs is free software; you can redistribute it and/or modify
16907+ * it under the terms of the GNU General Public License as published by
16908+ * the Free Software Foundation; either version 2 of the License, or
16909+ * (at your option) any later version.
dece6358
AM
16910+ *
16911+ * This program is distributed in the hope that it will be useful,
16912+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16913+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16914+ * GNU General Public License for more details.
16915+ *
16916+ * You should have received a copy of the GNU General Public License
523b37e3 16917+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 16918+ */
16919+
16920+/*
4a4d8108 16921+ * fsnotify for the lower directories
1facf9fc 16922+ */
16923+
16924+#include "aufs.h"
16925+
4a4d8108
AM
16926+/* FS_IN_IGNORED is unnecessary */
16927+static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
16928+ | FS_CREATE | FS_EVENT_ON_CHILD);
7f207e10 16929+static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
7eafdf33 16930+static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
1facf9fc 16931+
0c5527e5 16932+static void au_hfsn_free_mark(struct fsnotify_mark *mark)
1facf9fc 16933+{
0c5527e5
AM
16934+ struct au_hnotify *hn = container_of(mark, struct au_hnotify,
16935+ hn_mark);
5afbbe0d 16936+ /* AuDbg("here\n"); */
1c60b727 16937+ au_cache_free_hnotify(hn);
8b6a4947 16938+ smp_mb__before_atomic(); /* for atomic64_dec */
1716fcea
AM
16939+ if (atomic64_dec_and_test(&au_hfsn_ifree))
16940+ wake_up(&au_hfsn_wq);
4a4d8108 16941+}
1facf9fc 16942+
027c5e7a 16943+static int au_hfsn_alloc(struct au_hinode *hinode)
4a4d8108 16944+{
1716fcea 16945+ int err;
027c5e7a
AM
16946+ struct au_hnotify *hn;
16947+ struct super_block *sb;
16948+ struct au_branch *br;
0c5527e5 16949+ struct fsnotify_mark *mark;
027c5e7a 16950+ aufs_bindex_t bindex;
1facf9fc 16951+
027c5e7a
AM
16952+ hn = hinode->hi_notify;
16953+ sb = hn->hn_aufs_inode->i_sb;
16954+ bindex = au_br_index(sb, hinode->hi_id);
16955+ br = au_sbr(sb, bindex);
1716fcea
AM
16956+ AuDebugOn(!br->br_hfsn);
16957+
0c5527e5 16958+ mark = &hn->hn_mark;
ffa93bbd 16959+ fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
0c5527e5 16960+ mark->mask = AuHfsnMask;
7f207e10
AM
16961+ /*
16962+ * by udba rename or rmdir, aufs assign a new inode to the known
16963+ * h_inode, so specify 1 to allow dups.
16964+ */
c1595e42 16965+ lockdep_off();
acd2b654 16966+ err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
c1595e42 16967+ lockdep_on();
1716fcea
AM
16968+
16969+ return err;
1facf9fc 16970+}
16971+
7eafdf33 16972+static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
1facf9fc 16973+{
0c5527e5 16974+ struct fsnotify_mark *mark;
7eafdf33 16975+ unsigned long long ull;
1716fcea 16976+ struct fsnotify_group *group;
7eafdf33
AM
16977+
16978+ ull = atomic64_inc_return(&au_hfsn_ifree);
16979+ BUG_ON(!ull);
953406b4 16980+
0c5527e5 16981+ mark = &hn->hn_mark;
1716fcea
AM
16982+ spin_lock(&mark->lock);
16983+ group = mark->group;
16984+ fsnotify_get_group(group);
16985+ spin_unlock(&mark->lock);
c1595e42 16986+ lockdep_off();
1716fcea 16987+ fsnotify_destroy_mark(mark, group);
5afbbe0d 16988+ fsnotify_put_mark(mark);
1716fcea 16989+ fsnotify_put_group(group);
c1595e42 16990+ lockdep_on();
7f207e10 16991+
7eafdf33
AM
16992+ /* free hn by myself */
16993+ return 0;
1facf9fc 16994+}
16995+
16996+/* ---------------------------------------------------------------------- */
16997+
4a4d8108 16998+static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
1facf9fc 16999+{
0c5527e5 17000+ struct fsnotify_mark *mark;
1facf9fc 17001+
0c5527e5
AM
17002+ mark = &hinode->hi_notify->hn_mark;
17003+ spin_lock(&mark->lock);
1facf9fc 17004+ if (do_set) {
0c5527e5
AM
17005+ AuDebugOn(mark->mask & AuHfsnMask);
17006+ mark->mask |= AuHfsnMask;
1facf9fc 17007+ } else {
0c5527e5
AM
17008+ AuDebugOn(!(mark->mask & AuHfsnMask));
17009+ mark->mask &= ~AuHfsnMask;
1facf9fc 17010+ }
0c5527e5 17011+ spin_unlock(&mark->lock);
4a4d8108 17012+ /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
1facf9fc 17013+}
17014+
4a4d8108 17015+/* ---------------------------------------------------------------------- */
1facf9fc 17016+
4a4d8108
AM
17017+/* #define AuDbgHnotify */
17018+#ifdef AuDbgHnotify
17019+static char *au_hfsn_name(u32 mask)
17020+{
17021+#ifdef CONFIG_AUFS_DEBUG
c06a8ce3
AM
17022+#define test_ret(flag) \
17023+ do { \
17024+ if (mask & flag) \
17025+ return #flag; \
17026+ } while (0)
4a4d8108
AM
17027+ test_ret(FS_ACCESS);
17028+ test_ret(FS_MODIFY);
17029+ test_ret(FS_ATTRIB);
17030+ test_ret(FS_CLOSE_WRITE);
17031+ test_ret(FS_CLOSE_NOWRITE);
17032+ test_ret(FS_OPEN);
17033+ test_ret(FS_MOVED_FROM);
17034+ test_ret(FS_MOVED_TO);
17035+ test_ret(FS_CREATE);
17036+ test_ret(FS_DELETE);
17037+ test_ret(FS_DELETE_SELF);
17038+ test_ret(FS_MOVE_SELF);
17039+ test_ret(FS_UNMOUNT);
17040+ test_ret(FS_Q_OVERFLOW);
17041+ test_ret(FS_IN_IGNORED);
b912730e 17042+ test_ret(FS_ISDIR);
4a4d8108
AM
17043+ test_ret(FS_IN_ONESHOT);
17044+ test_ret(FS_EVENT_ON_CHILD);
17045+ return "";
17046+#undef test_ret
17047+#else
17048+ return "??";
17049+#endif
1facf9fc 17050+}
4a4d8108 17051+#endif
1facf9fc 17052+
17053+/* ---------------------------------------------------------------------- */
17054+
1716fcea
AM
17055+static void au_hfsn_free_group(struct fsnotify_group *group)
17056+{
17057+ struct au_br_hfsnotify *hfsn = group->private;
17058+
5afbbe0d 17059+ /* AuDbg("here\n"); */
9f237c51 17060+ au_kfree_try_rcu(hfsn);
1716fcea
AM
17061+}
17062+
4a4d8108 17063+static int au_hfsn_handle_event(struct fsnotify_group *group,
fb47a38f 17064+ struct inode *inode,
a2654f78 17065+ u32 mask, const void *data, int data_type,
ffa93bbd
AM
17066+ const unsigned char *file_name, u32 cookie,
17067+ struct fsnotify_iter_info *iter_info)
1facf9fc 17068+{
17069+ int err;
4a4d8108
AM
17070+ struct au_hnotify *hnotify;
17071+ struct inode *h_dir, *h_inode;
fb47a38f 17072+ struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
cd7a4cd9 17073+ struct fsnotify_mark *inode_mark;
4a4d8108 17074+
fb47a38f 17075+ AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
1facf9fc 17076+
17077+ err = 0;
0c5527e5 17078+ /* if FS_UNMOUNT happens, there must be another bug */
4a4d8108 17079+ AuDebugOn(mask & FS_UNMOUNT);
0c5527e5 17080+ if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
1facf9fc 17081+ goto out;
1facf9fc 17082+
fb47a38f
JR
17083+ h_dir = inode;
17084+ h_inode = NULL;
4a4d8108 17085+#ifdef AuDbgHnotify
392086de 17086+ au_debug_on();
4a4d8108
AM
17087+ if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
17088+ || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
17089+ AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
17090+ h_dir->i_ino, mask, au_hfsn_name(mask),
17091+ AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
17092+ /* WARN_ON(1); */
1facf9fc 17093+ }
392086de 17094+ au_debug_off();
1facf9fc 17095+#endif
4a4d8108 17096+
cd7a4cd9 17097+ inode_mark = fsnotify_iter_inode_mark(iter_info);
0c5527e5
AM
17098+ AuDebugOn(!inode_mark);
17099+ hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
17100+ err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
1facf9fc 17101+
4a4d8108
AM
17102+out:
17103+ return err;
17104+}
1facf9fc 17105+
4a4d8108 17106+static struct fsnotify_ops au_hfsn_ops = {
1716fcea 17107+ .handle_event = au_hfsn_handle_event,
ffa93bbd
AM
17108+ .free_group_priv = au_hfsn_free_group,
17109+ .free_mark = au_hfsn_free_mark
4a4d8108
AM
17110+};
17111+
17112+/* ---------------------------------------------------------------------- */
17113+
027c5e7a
AM
17114+static void au_hfsn_fin_br(struct au_branch *br)
17115+{
1716fcea 17116+ struct au_br_hfsnotify *hfsn;
027c5e7a 17117+
1716fcea 17118+ hfsn = br->br_hfsn;
c1595e42
JR
17119+ if (hfsn) {
17120+ lockdep_off();
1716fcea 17121+ fsnotify_put_group(hfsn->hfsn_group);
c1595e42
JR
17122+ lockdep_on();
17123+ }
027c5e7a
AM
17124+}
17125+
1716fcea 17126+static int au_hfsn_init_br(struct au_branch *br, int perm)
4a4d8108
AM
17127+{
17128+ int err;
1716fcea
AM
17129+ struct fsnotify_group *group;
17130+ struct au_br_hfsnotify *hfsn;
1facf9fc 17131+
4a4d8108 17132+ err = 0;
1716fcea
AM
17133+ br->br_hfsn = NULL;
17134+ if (!au_br_hnotifyable(perm))
027c5e7a 17135+ goto out;
027c5e7a 17136+
1716fcea
AM
17137+ err = -ENOMEM;
17138+ hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
17139+ if (unlikely(!hfsn))
027c5e7a
AM
17140+ goto out;
17141+
1716fcea
AM
17142+ err = 0;
17143+ group = fsnotify_alloc_group(&au_hfsn_ops);
17144+ if (IS_ERR(group)) {
17145+ err = PTR_ERR(group);
0c5527e5 17146+ pr_err("fsnotify_alloc_group() failed, %d\n", err);
1716fcea 17147+ goto out_hfsn;
4a4d8108 17148+ }
1facf9fc 17149+
1716fcea
AM
17150+ group->private = hfsn;
17151+ hfsn->hfsn_group = group;
17152+ br->br_hfsn = hfsn;
17153+ goto out; /* success */
17154+
17155+out_hfsn:
9f237c51 17156+ au_kfree_try_rcu(hfsn);
027c5e7a 17157+out:
1716fcea
AM
17158+ return err;
17159+}
17160+
17161+static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
17162+{
17163+ int err;
17164+
17165+ err = 0;
17166+ if (!br->br_hfsn)
17167+ err = au_hfsn_init_br(br, perm);
17168+
1facf9fc 17169+ return err;
17170+}
17171+
7eafdf33
AM
17172+/* ---------------------------------------------------------------------- */
17173+
17174+static void au_hfsn_fin(void)
17175+{
17176+ AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
17177+ wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
17178+}
17179+
4a4d8108
AM
17180+const struct au_hnotify_op au_hnotify_op = {
17181+ .ctl = au_hfsn_ctl,
17182+ .alloc = au_hfsn_alloc,
17183+ .free = au_hfsn_free,
1facf9fc 17184+
7eafdf33
AM
17185+ .fin = au_hfsn_fin,
17186+
027c5e7a
AM
17187+ .reset_br = au_hfsn_reset_br,
17188+ .fin_br = au_hfsn_fin_br,
17189+ .init_br = au_hfsn_init_br
4a4d8108 17190+};
7f207e10
AM
17191diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
17192--- /usr/share/empty/fs/aufs/hfsplus.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 17193+++ linux/fs/aufs/hfsplus.c 2019-03-05 12:13:00.139224339 +0100
acd2b654 17194@@ -0,0 +1,60 @@
cd7a4cd9 17195+// SPDX-License-Identifier: GPL-2.0
4a4d8108 17196+/*
ba1aed25 17197+ * Copyright (C) 2010-2019 Junjiro R. Okajima
4a4d8108
AM
17198+ *
17199+ * This program, aufs is free software; you can redistribute it and/or modify
17200+ * it under the terms of the GNU General Public License as published by
17201+ * the Free Software Foundation; either version 2 of the License, or
17202+ * (at your option) any later version.
17203+ *
17204+ * This program is distributed in the hope that it will be useful,
17205+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17206+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17207+ * GNU General Public License for more details.
17208+ *
17209+ * You should have received a copy of the GNU General Public License
523b37e3 17210+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 17211+ */
1facf9fc 17212+
4a4d8108 17213+/*
acd2b654 17214+ * special support for filesystems which acquires an inode mutex
4a4d8108
AM
17215+ * at final closing a file, eg, hfsplus.
17216+ *
17217+ * This trick is very simple and stupid, just to open the file before really
acd2b654 17218+ * necessary open to tell hfsplus that this is not the final closing.
4a4d8108
AM
17219+ * The caller should call au_h_open_pre() after acquiring the inode mutex,
17220+ * and au_h_open_post() after releasing it.
17221+ */
1facf9fc 17222+
4a4d8108 17223+#include "aufs.h"
1facf9fc 17224+
392086de
AM
17225+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
17226+ int force_wr)
4a4d8108
AM
17227+{
17228+ struct file *h_file;
17229+ struct dentry *h_dentry;
1facf9fc 17230+
4a4d8108
AM
17231+ h_dentry = au_h_dptr(dentry, bindex);
17232+ AuDebugOn(!h_dentry);
5527c038 17233+ AuDebugOn(d_is_negative(h_dentry));
4a4d8108
AM
17234+
17235+ h_file = NULL;
17236+ if (au_test_hfsplus(h_dentry->d_sb)
7e9cd9fe 17237+ && d_is_reg(h_dentry))
4a4d8108
AM
17238+ h_file = au_h_open(dentry, bindex,
17239+ O_RDONLY | O_NOATIME | O_LARGEFILE,
392086de 17240+ /*file*/NULL, force_wr);
4a4d8108 17241+ return h_file;
1facf9fc 17242+}
17243+
4a4d8108
AM
17244+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
17245+ struct file *h_file)
17246+{
acd2b654
AM
17247+ struct au_branch *br;
17248+
4a4d8108
AM
17249+ if (h_file) {
17250+ fput(h_file);
acd2b654
AM
17251+ br = au_sbr(dentry->d_sb, bindex);
17252+ au_lcnt_dec(&br->br_nfiles);
4a4d8108
AM
17253+ }
17254+}
7f207e10
AM
17255diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
17256--- /usr/share/empty/fs/aufs/hnotify.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 17257+++ linux/fs/aufs/hnotify.c 2019-03-05 12:13:00.139224339 +0100
062440b3 17258@@ -0,0 +1,720 @@
cd7a4cd9 17259+// SPDX-License-Identifier: GPL-2.0
e49829fe 17260+/*
ba1aed25 17261+ * Copyright (C) 2005-2019 Junjiro R. Okajima
e49829fe
JR
17262+ *
17263+ * This program, aufs is free software; you can redistribute it and/or modify
17264+ * it under the terms of the GNU General Public License as published by
17265+ * the Free Software Foundation; either version 2 of the License, or
17266+ * (at your option) any later version.
17267+ *
17268+ * This program is distributed in the hope that it will be useful,
17269+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17270+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17271+ * GNU General Public License for more details.
17272+ *
17273+ * You should have received a copy of the GNU General Public License
523b37e3 17274+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
17275+ */
17276+
17277+/*
7f207e10 17278+ * abstraction to notify the direct changes on lower directories
e49829fe
JR
17279+ */
17280+
17281+#include "aufs.h"
17282+
027c5e7a 17283+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
e49829fe
JR
17284+{
17285+ int err;
7f207e10 17286+ struct au_hnotify *hn;
1facf9fc 17287+
4a4d8108
AM
17288+ err = -ENOMEM;
17289+ hn = au_cache_alloc_hnotify();
17290+ if (hn) {
17291+ hn->hn_aufs_inode = inode;
027c5e7a
AM
17292+ hinode->hi_notify = hn;
17293+ err = au_hnotify_op.alloc(hinode);
17294+ AuTraceErr(err);
17295+ if (unlikely(err)) {
17296+ hinode->hi_notify = NULL;
1c60b727 17297+ au_cache_free_hnotify(hn);
4a4d8108
AM
17298+ /*
17299+ * The upper dir was removed by udba, but the same named
acd2b654 17300+ * dir left. In this case, aufs assigns a new inode
4a4d8108 17301+ * number and set the monitor again.
acd2b654 17302+ * For the lower dir, the old monitor is still left.
4a4d8108
AM
17303+ */
17304+ if (err == -EEXIST)
17305+ err = 0;
17306+ }
1308ab2a 17307+ }
1308ab2a 17308+
027c5e7a 17309+ AuTraceErr(err);
1308ab2a 17310+ return err;
dece6358 17311+}
1facf9fc 17312+
4a4d8108 17313+void au_hn_free(struct au_hinode *hinode)
dece6358 17314+{
4a4d8108 17315+ struct au_hnotify *hn;
1facf9fc 17316+
4a4d8108
AM
17317+ hn = hinode->hi_notify;
17318+ if (hn) {
4a4d8108 17319+ hinode->hi_notify = NULL;
7eafdf33 17320+ if (au_hnotify_op.free(hinode, hn))
1c60b727 17321+ au_cache_free_hnotify(hn);
4a4d8108
AM
17322+ }
17323+}
dece6358 17324+
4a4d8108 17325+/* ---------------------------------------------------------------------- */
dece6358 17326+
4a4d8108
AM
17327+void au_hn_ctl(struct au_hinode *hinode, int do_set)
17328+{
17329+ if (hinode->hi_notify)
17330+ au_hnotify_op.ctl(hinode, do_set);
17331+}
17332+
17333+void au_hn_reset(struct inode *inode, unsigned int flags)
17334+{
5afbbe0d 17335+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
17336+ struct inode *hi;
17337+ struct dentry *iwhdentry;
1facf9fc 17338+
5afbbe0d
AM
17339+ bbot = au_ibbot(inode);
17340+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108
AM
17341+ hi = au_h_iptr(inode, bindex);
17342+ if (!hi)
17343+ continue;
1308ab2a 17344+
febd17d6 17345+ /* inode_lock_nested(hi, AuLsc_I_CHILD); */
4a4d8108
AM
17346+ iwhdentry = au_hi_wh(inode, bindex);
17347+ if (iwhdentry)
17348+ dget(iwhdentry);
17349+ au_igrab(hi);
17350+ au_set_h_iptr(inode, bindex, NULL, 0);
17351+ au_set_h_iptr(inode, bindex, au_igrab(hi),
17352+ flags & ~AuHi_XINO);
17353+ iput(hi);
17354+ dput(iwhdentry);
febd17d6 17355+ /* inode_unlock(hi); */
1facf9fc 17356+ }
1facf9fc 17357+}
17358+
1308ab2a 17359+/* ---------------------------------------------------------------------- */
1facf9fc 17360+
4a4d8108 17361+static int hn_xino(struct inode *inode, struct inode *h_inode)
1facf9fc 17362+{
4a4d8108 17363+ int err;
5afbbe0d 17364+ aufs_bindex_t bindex, bbot, bfound, btop;
4a4d8108 17365+ struct inode *h_i;
1facf9fc 17366+
4a4d8108
AM
17367+ err = 0;
17368+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17369+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17370+ goto out;
17371+ }
1facf9fc 17372+
4a4d8108 17373+ bfound = -1;
5afbbe0d
AM
17374+ bbot = au_ibbot(inode);
17375+ btop = au_ibtop(inode);
4a4d8108 17376+#if 0 /* reserved for future use */
5afbbe0d 17377+ if (bindex == bbot) {
4a4d8108
AM
17378+ /* keep this ino in rename case */
17379+ goto out;
17380+ }
17381+#endif
5afbbe0d 17382+ for (bindex = btop; bindex <= bbot; bindex++)
4a4d8108
AM
17383+ if (au_h_iptr(inode, bindex) == h_inode) {
17384+ bfound = bindex;
17385+ break;
17386+ }
17387+ if (bfound < 0)
1308ab2a 17388+ goto out;
1facf9fc 17389+
5afbbe0d 17390+ for (bindex = btop; bindex <= bbot; bindex++) {
4a4d8108
AM
17391+ h_i = au_h_iptr(inode, bindex);
17392+ if (!h_i)
17393+ continue;
1facf9fc 17394+
4a4d8108
AM
17395+ err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
17396+ /* ignore this error */
17397+ /* bad action? */
1facf9fc 17398+ }
1facf9fc 17399+
4a4d8108 17400+ /* children inode number will be broken */
1facf9fc 17401+
4f0767ce 17402+out:
4a4d8108
AM
17403+ AuTraceErr(err);
17404+ return err;
1facf9fc 17405+}
17406+
4a4d8108 17407+static int hn_gen_tree(struct dentry *dentry)
1facf9fc 17408+{
4a4d8108
AM
17409+ int err, i, j, ndentry;
17410+ struct au_dcsub_pages dpages;
17411+ struct au_dpage *dpage;
17412+ struct dentry **dentries;
1facf9fc 17413+
4a4d8108
AM
17414+ err = au_dpages_init(&dpages, GFP_NOFS);
17415+ if (unlikely(err))
17416+ goto out;
17417+ err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
17418+ if (unlikely(err))
17419+ goto out_dpages;
1facf9fc 17420+
4a4d8108
AM
17421+ for (i = 0; i < dpages.ndpage; i++) {
17422+ dpage = dpages.dpages + i;
17423+ dentries = dpage->dentries;
17424+ ndentry = dpage->ndentry;
17425+ for (j = 0; j < ndentry; j++) {
17426+ struct dentry *d;
17427+
17428+ d = dentries[j];
17429+ if (IS_ROOT(d))
17430+ continue;
17431+
4a4d8108 17432+ au_digen_dec(d);
5527c038 17433+ if (d_really_is_positive(d))
4a4d8108
AM
17434+ /* todo: reset children xino?
17435+ cached children only? */
5527c038 17436+ au_iigen_dec(d_inode(d));
1308ab2a 17437+ }
dece6358 17438+ }
1facf9fc 17439+
4f0767ce 17440+out_dpages:
4a4d8108 17441+ au_dpages_free(&dpages);
dece6358 17442+
027c5e7a 17443+#if 0
4a4d8108
AM
17444+ /* discard children */
17445+ dentry_unhash(dentry);
17446+ dput(dentry);
027c5e7a 17447+#endif
4f0767ce 17448+out:
dece6358
AM
17449+ return err;
17450+}
17451+
1308ab2a 17452+/*
4a4d8108 17453+ * return 0 if processed.
1308ab2a 17454+ */
4a4d8108
AM
17455+static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
17456+ const unsigned int isdir)
dece6358 17457+{
1308ab2a 17458+ int err;
4a4d8108
AM
17459+ struct dentry *d;
17460+ struct qstr *dname;
1facf9fc 17461+
4a4d8108
AM
17462+ err = 1;
17463+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17464+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17465+ err = 0;
17466+ goto out;
17467+ }
dece6358 17468+
4a4d8108
AM
17469+ if (!isdir) {
17470+ AuDebugOn(!name);
17471+ au_iigen_dec(inode);
027c5e7a 17472+ spin_lock(&inode->i_lock);
c1595e42 17473+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 17474+ spin_lock(&d->d_lock);
4a4d8108
AM
17475+ dname = &d->d_name;
17476+ if (dname->len != nlen
027c5e7a
AM
17477+ && memcmp(dname->name, name, nlen)) {
17478+ spin_unlock(&d->d_lock);
4a4d8108 17479+ continue;
027c5e7a 17480+ }
4a4d8108 17481+ err = 0;
4a4d8108
AM
17482+ au_digen_dec(d);
17483+ spin_unlock(&d->d_lock);
17484+ break;
1facf9fc 17485+ }
027c5e7a 17486+ spin_unlock(&inode->i_lock);
1308ab2a 17487+ } else {
027c5e7a 17488+ au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
c1595e42 17489+ d = d_find_any_alias(inode);
4a4d8108
AM
17490+ if (!d) {
17491+ au_iigen_dec(inode);
17492+ goto out;
17493+ }
1facf9fc 17494+
027c5e7a 17495+ spin_lock(&d->d_lock);
4a4d8108 17496+ dname = &d->d_name;
027c5e7a
AM
17497+ if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
17498+ spin_unlock(&d->d_lock);
4a4d8108 17499+ err = hn_gen_tree(d);
027c5e7a
AM
17500+ spin_lock(&d->d_lock);
17501+ }
17502+ spin_unlock(&d->d_lock);
4a4d8108
AM
17503+ dput(d);
17504+ }
1facf9fc 17505+
4f0767ce 17506+out:
4a4d8108 17507+ AuTraceErr(err);
1308ab2a 17508+ return err;
17509+}
dece6358 17510+
4a4d8108 17511+static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
1facf9fc 17512+{
4a4d8108 17513+ int err;
1facf9fc 17514+
5527c038 17515+ if (IS_ROOT(dentry)) {
0c3ec466 17516+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17517+ return 0;
17518+ }
1308ab2a 17519+
4a4d8108
AM
17520+ err = 0;
17521+ if (!isdir) {
4a4d8108 17522+ au_digen_dec(dentry);
5527c038
JR
17523+ if (d_really_is_positive(dentry))
17524+ au_iigen_dec(d_inode(dentry));
4a4d8108 17525+ } else {
027c5e7a 17526+ au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
5527c038 17527+ if (d_really_is_positive(dentry))
4a4d8108
AM
17528+ err = hn_gen_tree(dentry);
17529+ }
17530+
17531+ AuTraceErr(err);
17532+ return err;
1facf9fc 17533+}
17534+
4a4d8108 17535+/* ---------------------------------------------------------------------- */
1facf9fc 17536+
4a4d8108
AM
17537+/* hnotify job flags */
17538+#define AuHnJob_XINO0 1
17539+#define AuHnJob_GEN (1 << 1)
17540+#define AuHnJob_DIRENT (1 << 2)
17541+#define AuHnJob_ISDIR (1 << 3)
17542+#define AuHnJob_TRYXINO0 (1 << 4)
17543+#define AuHnJob_MNTPNT (1 << 5)
17544+#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
7f207e10
AM
17545+#define au_fset_hnjob(flags, name) \
17546+ do { (flags) |= AuHnJob_##name; } while (0)
17547+#define au_fclr_hnjob(flags, name) \
17548+ do { (flags) &= ~AuHnJob_##name; } while (0)
1facf9fc 17549+
4a4d8108
AM
17550+enum {
17551+ AuHn_CHILD,
17552+ AuHn_PARENT,
17553+ AuHnLast
17554+};
1facf9fc 17555+
4a4d8108
AM
17556+struct au_hnotify_args {
17557+ struct inode *h_dir, *dir, *h_child_inode;
17558+ u32 mask;
17559+ unsigned int flags[AuHnLast];
17560+ unsigned int h_child_nlen;
17561+ char h_child_name[];
17562+};
1facf9fc 17563+
4a4d8108
AM
17564+struct hn_job_args {
17565+ unsigned int flags;
17566+ struct inode *inode, *h_inode, *dir, *h_dir;
17567+ struct dentry *dentry;
17568+ char *h_name;
17569+ int h_nlen;
17570+};
1308ab2a 17571+
4a4d8108
AM
17572+static int hn_job(struct hn_job_args *a)
17573+{
17574+ const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
076b876e 17575+ int e;
1308ab2a 17576+
4a4d8108
AM
17577+ /* reset xino */
17578+ if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
17579+ hn_xino(a->inode, a->h_inode); /* ignore this error */
1308ab2a 17580+
4a4d8108
AM
17581+ if (au_ftest_hnjob(a->flags, TRYXINO0)
17582+ && a->inode
17583+ && a->h_inode) {
be118d29 17584+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
38d290e6
JR
17585+ if (!a->h_inode->i_nlink
17586+ && !(a->h_inode->i_state & I_LINKABLE))
4a4d8108 17587+ hn_xino(a->inode, a->h_inode); /* ignore this error */
3c1bdaff 17588+ inode_unlock_shared(a->h_inode);
1308ab2a 17589+ }
1facf9fc 17590+
4a4d8108
AM
17591+ /* make the generation obsolete */
17592+ if (au_ftest_hnjob(a->flags, GEN)) {
076b876e 17593+ e = -1;
4a4d8108 17594+ if (a->inode)
076b876e 17595+ e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
4a4d8108 17596+ isdir);
076b876e 17597+ if (e && a->dentry)
4a4d8108
AM
17598+ hn_gen_by_name(a->dentry, isdir);
17599+ /* ignore this error */
1facf9fc 17600+ }
1facf9fc 17601+
4a4d8108
AM
17602+ /* make dir entries obsolete */
17603+ if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
17604+ struct au_vdir *vdir;
1facf9fc 17605+
4a4d8108
AM
17606+ vdir = au_ivdir(a->inode);
17607+ if (vdir)
17608+ vdir->vd_jiffy = 0;
17609+ /* IMustLock(a->inode); */
be118d29 17610+ /* inode_inc_iversion(a->inode); */
4a4d8108 17611+ }
1facf9fc 17612+
4a4d8108
AM
17613+ /* can do nothing but warn */
17614+ if (au_ftest_hnjob(a->flags, MNTPNT)
17615+ && a->dentry
17616+ && d_mountpoint(a->dentry))
523b37e3 17617+ pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
1facf9fc 17618+
4a4d8108 17619+ return 0;
1308ab2a 17620+}
1facf9fc 17621+
1308ab2a 17622+/* ---------------------------------------------------------------------- */
1facf9fc 17623+
4a4d8108
AM
17624+static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
17625+ struct inode *dir)
1308ab2a 17626+{
4a4d8108
AM
17627+ struct dentry *dentry, *d, *parent;
17628+ struct qstr *dname;
1308ab2a 17629+
c1595e42 17630+ parent = d_find_any_alias(dir);
4a4d8108
AM
17631+ if (!parent)
17632+ return NULL;
1308ab2a 17633+
4a4d8108 17634+ dentry = NULL;
027c5e7a 17635+ spin_lock(&parent->d_lock);
c1595e42 17636+ list_for_each_entry(d, &parent->d_subdirs, d_child) {
523b37e3 17637+ /* AuDbg("%pd\n", d); */
027c5e7a 17638+ spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
4a4d8108
AM
17639+ dname = &d->d_name;
17640+ if (dname->len != nlen || memcmp(dname->name, name, nlen))
027c5e7a
AM
17641+ goto cont_unlock;
17642+ if (au_di(d))
17643+ au_digen_dec(d);
17644+ else
17645+ goto cont_unlock;
c1595e42 17646+ if (au_dcount(d) > 0) {
027c5e7a 17647+ dentry = dget_dlock(d);
4a4d8108 17648+ spin_unlock(&d->d_lock);
027c5e7a 17649+ break;
dece6358 17650+ }
1facf9fc 17651+
f6b6e03d 17652+cont_unlock:
027c5e7a 17653+ spin_unlock(&d->d_lock);
1308ab2a 17654+ }
027c5e7a 17655+ spin_unlock(&parent->d_lock);
4a4d8108 17656+ dput(parent);
1facf9fc 17657+
4a4d8108
AM
17658+ if (dentry)
17659+ di_write_lock_child(dentry);
1308ab2a 17660+
4a4d8108
AM
17661+ return dentry;
17662+}
dece6358 17663+
4a4d8108
AM
17664+static struct inode *lookup_wlock_by_ino(struct super_block *sb,
17665+ aufs_bindex_t bindex, ino_t h_ino)
17666+{
17667+ struct inode *inode;
17668+ ino_t ino;
17669+ int err;
17670+
17671+ inode = NULL;
17672+ err = au_xino_read(sb, bindex, h_ino, &ino);
17673+ if (!err && ino)
17674+ inode = ilookup(sb, ino);
17675+ if (!inode)
17676+ goto out;
17677+
17678+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17679+ pr_warn("wrong root branch\n");
4a4d8108
AM
17680+ iput(inode);
17681+ inode = NULL;
17682+ goto out;
1308ab2a 17683+ }
17684+
4a4d8108 17685+ ii_write_lock_child(inode);
1308ab2a 17686+
4f0767ce 17687+out:
4a4d8108 17688+ return inode;
dece6358
AM
17689+}
17690+
4a4d8108 17691+static void au_hn_bh(void *_args)
1facf9fc 17692+{
4a4d8108
AM
17693+ struct au_hnotify_args *a = _args;
17694+ struct super_block *sb;
5afbbe0d 17695+ aufs_bindex_t bindex, bbot, bfound;
4a4d8108 17696+ unsigned char xino, try_iput;
1facf9fc 17697+ int err;
1308ab2a 17698+ struct inode *inode;
4a4d8108
AM
17699+ ino_t h_ino;
17700+ struct hn_job_args args;
17701+ struct dentry *dentry;
17702+ struct au_sbinfo *sbinfo;
1facf9fc 17703+
4a4d8108
AM
17704+ AuDebugOn(!_args);
17705+ AuDebugOn(!a->h_dir);
17706+ AuDebugOn(!a->dir);
17707+ AuDebugOn(!a->mask);
17708+ AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
17709+ a->mask, a->dir->i_ino, a->h_dir->i_ino,
17710+ a->h_child_inode ? a->h_child_inode->i_ino : 0);
1facf9fc 17711+
4a4d8108
AM
17712+ inode = NULL;
17713+ dentry = NULL;
17714+ /*
17715+ * do not lock a->dir->i_mutex here
17716+ * because of d_revalidate() may cause a deadlock.
17717+ */
17718+ sb = a->dir->i_sb;
17719+ AuDebugOn(!sb);
17720+ sbinfo = au_sbi(sb);
17721+ AuDebugOn(!sbinfo);
7f207e10 17722+ si_write_lock(sb, AuLock_NOPLMW);
1facf9fc 17723+
8b6a4947
AM
17724+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
17725+ switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
17726+ case FS_MOVED_FROM:
17727+ case FS_MOVED_TO:
17728+ AuWarn1("DIRREN with UDBA may not work correctly "
17729+ "for the direct rename(2)\n");
17730+ }
17731+
4a4d8108
AM
17732+ ii_read_lock_parent(a->dir);
17733+ bfound = -1;
5afbbe0d
AM
17734+ bbot = au_ibbot(a->dir);
17735+ for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
4a4d8108
AM
17736+ if (au_h_iptr(a->dir, bindex) == a->h_dir) {
17737+ bfound = bindex;
17738+ break;
17739+ }
17740+ ii_read_unlock(a->dir);
17741+ if (unlikely(bfound < 0))
17742+ goto out;
1facf9fc 17743+
4a4d8108
AM
17744+ xino = !!au_opt_test(au_mntflags(sb), XINO);
17745+ h_ino = 0;
17746+ if (a->h_child_inode)
17747+ h_ino = a->h_child_inode->i_ino;
1facf9fc 17748+
4a4d8108
AM
17749+ if (a->h_child_nlen
17750+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
17751+ || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
17752+ dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
17753+ a->dir);
17754+ try_iput = 0;
5527c038
JR
17755+ if (dentry && d_really_is_positive(dentry))
17756+ inode = d_inode(dentry);
4a4d8108
AM
17757+ if (xino && !inode && h_ino
17758+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
17759+ || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
17760+ || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
17761+ inode = lookup_wlock_by_ino(sb, bfound, h_ino);
17762+ try_iput = 1;
f0c0a007 17763+ }
1facf9fc 17764+
4a4d8108
AM
17765+ args.flags = a->flags[AuHn_CHILD];
17766+ args.dentry = dentry;
17767+ args.inode = inode;
17768+ args.h_inode = a->h_child_inode;
17769+ args.dir = a->dir;
17770+ args.h_dir = a->h_dir;
17771+ args.h_name = a->h_child_name;
17772+ args.h_nlen = a->h_child_nlen;
17773+ err = hn_job(&args);
17774+ if (dentry) {
027c5e7a 17775+ if (au_di(dentry))
4a4d8108
AM
17776+ di_write_unlock(dentry);
17777+ dput(dentry);
17778+ }
17779+ if (inode && try_iput) {
17780+ ii_write_unlock(inode);
17781+ iput(inode);
17782+ }
1facf9fc 17783+
4a4d8108
AM
17784+ ii_write_lock_parent(a->dir);
17785+ args.flags = a->flags[AuHn_PARENT];
17786+ args.dentry = NULL;
17787+ args.inode = a->dir;
17788+ args.h_inode = a->h_dir;
17789+ args.dir = NULL;
17790+ args.h_dir = NULL;
17791+ args.h_name = NULL;
17792+ args.h_nlen = 0;
17793+ err = hn_job(&args);
17794+ ii_write_unlock(a->dir);
1facf9fc 17795+
4f0767ce 17796+out:
4a4d8108
AM
17797+ iput(a->h_child_inode);
17798+ iput(a->h_dir);
17799+ iput(a->dir);
027c5e7a
AM
17800+ si_write_unlock(sb);
17801+ au_nwt_done(&sbinfo->si_nowait);
9f237c51 17802+ au_kfree_rcu(a);
dece6358 17803+}
1facf9fc 17804+
4a4d8108
AM
17805+/* ---------------------------------------------------------------------- */
17806+
17807+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17808+ struct qstr *h_child_qstr, struct inode *h_child_inode)
dece6358 17809+{
4a4d8108 17810+ int err, len;
53392da6 17811+ unsigned int flags[AuHnLast], f;
4a4d8108
AM
17812+ unsigned char isdir, isroot, wh;
17813+ struct inode *dir;
17814+ struct au_hnotify_args *args;
17815+ char *p, *h_child_name;
dece6358 17816+
1308ab2a 17817+ err = 0;
4a4d8108
AM
17818+ AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
17819+ dir = igrab(hnotify->hn_aufs_inode);
17820+ if (!dir)
17821+ goto out;
1facf9fc 17822+
4a4d8108
AM
17823+ isroot = (dir->i_ino == AUFS_ROOT_INO);
17824+ wh = 0;
17825+ h_child_name = (void *)h_child_qstr->name;
17826+ len = h_child_qstr->len;
17827+ if (h_child_name) {
17828+ if (len > AUFS_WH_PFX_LEN
17829+ && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
17830+ h_child_name += AUFS_WH_PFX_LEN;
17831+ len -= AUFS_WH_PFX_LEN;
17832+ wh = 1;
17833+ }
1facf9fc 17834+ }
dece6358 17835+
4a4d8108
AM
17836+ isdir = 0;
17837+ if (h_child_inode)
17838+ isdir = !!S_ISDIR(h_child_inode->i_mode);
17839+ flags[AuHn_PARENT] = AuHnJob_ISDIR;
17840+ flags[AuHn_CHILD] = 0;
17841+ if (isdir)
17842+ flags[AuHn_CHILD] = AuHnJob_ISDIR;
17843+ au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
17844+ au_fset_hnjob(flags[AuHn_CHILD], GEN);
17845+ switch (mask & FS_EVENTS_POSS_ON_CHILD) {
17846+ case FS_MOVED_FROM:
17847+ case FS_MOVED_TO:
17848+ au_fset_hnjob(flags[AuHn_CHILD], XINO0);
17849+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17850+ /*FALLTHROUGH*/
17851+ case FS_CREATE:
fb47a38f 17852+ AuDebugOn(!h_child_name);
4a4d8108 17853+ break;
1facf9fc 17854+
4a4d8108
AM
17855+ case FS_DELETE:
17856+ /*
17857+ * aufs never be able to get this child inode.
17858+ * revalidation should be in d_revalidate()
17859+ * by checking i_nlink, i_generation or d_unhashed().
17860+ */
17861+ AuDebugOn(!h_child_name);
17862+ au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
17863+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17864+ break;
dece6358 17865+
4a4d8108
AM
17866+ default:
17867+ AuDebugOn(1);
17868+ }
1308ab2a 17869+
4a4d8108
AM
17870+ if (wh)
17871+ h_child_inode = NULL;
1308ab2a 17872+
4a4d8108
AM
17873+ err = -ENOMEM;
17874+ /* iput() and kfree() will be called in au_hnotify() */
4a4d8108 17875+ args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
4a4d8108
AM
17876+ if (unlikely(!args)) {
17877+ AuErr1("no memory\n");
17878+ iput(dir);
17879+ goto out;
17880+ }
17881+ args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
17882+ args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
17883+ args->mask = mask;
17884+ args->dir = dir;
17885+ args->h_dir = igrab(h_dir);
17886+ if (h_child_inode)
17887+ h_child_inode = igrab(h_child_inode); /* can be NULL */
17888+ args->h_child_inode = h_child_inode;
17889+ args->h_child_nlen = len;
17890+ if (len) {
17891+ p = (void *)args;
17892+ p += sizeof(*args);
17893+ memcpy(p, h_child_name, len);
17894+ p[len] = 0;
1308ab2a 17895+ }
1308ab2a 17896+
38d290e6 17897+ /* NFS fires the event for silly-renamed one from kworker */
53392da6 17898+ f = 0;
38d290e6
JR
17899+ if (!dir->i_nlink
17900+ || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
53392da6
AM
17901+ f = AuWkq_NEST;
17902+ err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
4a4d8108
AM
17903+ if (unlikely(err)) {
17904+ pr_err("wkq %d\n", err);
17905+ iput(args->h_child_inode);
17906+ iput(args->h_dir);
17907+ iput(args->dir);
9f237c51 17908+ au_kfree_rcu(args);
1facf9fc 17909+ }
1facf9fc 17910+
4a4d8108 17911+out:
1facf9fc 17912+ return err;
17913+}
17914+
027c5e7a
AM
17915+/* ---------------------------------------------------------------------- */
17916+
17917+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
17918+{
17919+ int err;
17920+
17921+ AuDebugOn(!(udba & AuOptMask_UDBA));
17922+
17923+ err = 0;
17924+ if (au_hnotify_op.reset_br)
17925+ err = au_hnotify_op.reset_br(udba, br, perm);
17926+
17927+ return err;
17928+}
17929+
17930+int au_hnotify_init_br(struct au_branch *br, int perm)
17931+{
17932+ int err;
17933+
17934+ err = 0;
17935+ if (au_hnotify_op.init_br)
17936+ err = au_hnotify_op.init_br(br, perm);
17937+
17938+ return err;
17939+}
17940+
17941+void au_hnotify_fin_br(struct au_branch *br)
17942+{
17943+ if (au_hnotify_op.fin_br)
17944+ au_hnotify_op.fin_br(br);
17945+}
17946+
4a4d8108
AM
17947+static void au_hn_destroy_cache(void)
17948+{
1c60b727
AM
17949+ kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
17950+ au_cache[AuCache_HNOTIFY] = NULL;
4a4d8108 17951+}
1308ab2a 17952+
4a4d8108 17953+int __init au_hnotify_init(void)
1facf9fc 17954+{
1308ab2a 17955+ int err;
1308ab2a 17956+
4a4d8108 17957+ err = -ENOMEM;
1c60b727
AM
17958+ au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
17959+ if (au_cache[AuCache_HNOTIFY]) {
027c5e7a
AM
17960+ err = 0;
17961+ if (au_hnotify_op.init)
17962+ err = au_hnotify_op.init();
4a4d8108
AM
17963+ if (unlikely(err))
17964+ au_hn_destroy_cache();
1308ab2a 17965+ }
1308ab2a 17966+ AuTraceErr(err);
4a4d8108 17967+ return err;
1308ab2a 17968+}
17969+
4a4d8108 17970+void au_hnotify_fin(void)
1308ab2a 17971+{
027c5e7a
AM
17972+ if (au_hnotify_op.fin)
17973+ au_hnotify_op.fin();
f0c0a007 17974+
4a4d8108 17975+ /* cf. au_cache_fin() */
1c60b727 17976+ if (au_cache[AuCache_HNOTIFY])
4a4d8108 17977+ au_hn_destroy_cache();
dece6358 17978+}
7f207e10
AM
17979diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
17980--- /usr/share/empty/fs/aufs/iinfo.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 17981+++ linux/fs/aufs/iinfo.c 2019-03-05 12:13:00.139224339 +0100
062440b3 17982@@ -0,0 +1,286 @@
cd7a4cd9 17983+// SPDX-License-Identifier: GPL-2.0
dece6358 17984+/*
ba1aed25 17985+ * Copyright (C) 2005-2019 Junjiro R. Okajima
dece6358
AM
17986+ *
17987+ * This program, aufs is free software; you can redistribute it and/or modify
17988+ * it under the terms of the GNU General Public License as published by
17989+ * the Free Software Foundation; either version 2 of the License, or
17990+ * (at your option) any later version.
17991+ *
17992+ * This program is distributed in the hope that it will be useful,
17993+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17994+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17995+ * GNU General Public License for more details.
17996+ *
17997+ * You should have received a copy of the GNU General Public License
523b37e3 17998+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 17999+ */
1facf9fc 18000+
dece6358 18001+/*
4a4d8108 18002+ * inode private data
dece6358 18003+ */
1facf9fc 18004+
1308ab2a 18005+#include "aufs.h"
1facf9fc 18006+
4a4d8108 18007+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 18008+{
4a4d8108 18009+ struct inode *h_inode;
5afbbe0d 18010+ struct au_hinode *hinode;
1facf9fc 18011+
4a4d8108 18012+ IiMustAnyLock(inode);
1facf9fc 18013+
5afbbe0d
AM
18014+ hinode = au_hinode(au_ii(inode), bindex);
18015+ h_inode = hinode->hi_inode;
4a4d8108
AM
18016+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
18017+ return h_inode;
18018+}
1facf9fc 18019+
4a4d8108
AM
18020+/* todo: hard/soft set? */
18021+void au_hiput(struct au_hinode *hinode)
18022+{
18023+ au_hn_free(hinode);
18024+ dput(hinode->hi_whdentry);
18025+ iput(hinode->hi_inode);
18026+}
1facf9fc 18027+
4a4d8108
AM
18028+unsigned int au_hi_flags(struct inode *inode, int isdir)
18029+{
18030+ unsigned int flags;
18031+ const unsigned int mnt_flags = au_mntflags(inode->i_sb);
1facf9fc 18032+
4a4d8108
AM
18033+ flags = 0;
18034+ if (au_opt_test(mnt_flags, XINO))
18035+ au_fset_hi(flags, XINO);
18036+ if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
18037+ au_fset_hi(flags, HNOTIFY);
18038+ return flags;
1facf9fc 18039+}
18040+
4a4d8108
AM
18041+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
18042+ struct inode *h_inode, unsigned int flags)
1308ab2a 18043+{
4a4d8108
AM
18044+ struct au_hinode *hinode;
18045+ struct inode *hi;
18046+ struct au_iinfo *iinfo = au_ii(inode);
1facf9fc 18047+
4a4d8108 18048+ IiMustWriteLock(inode);
dece6358 18049+
5afbbe0d 18050+ hinode = au_hinode(iinfo, bindex);
4a4d8108
AM
18051+ hi = hinode->hi_inode;
18052+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
18053+
18054+ if (hi)
18055+ au_hiput(hinode);
18056+ hinode->hi_inode = h_inode;
18057+ if (h_inode) {
18058+ int err;
18059+ struct super_block *sb = inode->i_sb;
18060+ struct au_branch *br;
18061+
027c5e7a
AM
18062+ AuDebugOn(inode->i_mode
18063+ && (h_inode->i_mode & S_IFMT)
18064+ != (inode->i_mode & S_IFMT));
5afbbe0d 18065+ if (bindex == iinfo->ii_btop)
4a4d8108
AM
18066+ au_cpup_igen(inode, h_inode);
18067+ br = au_sbr(sb, bindex);
18068+ hinode->hi_id = br->br_id;
18069+ if (au_ftest_hi(flags, XINO)) {
18070+ err = au_xino_write(sb, bindex, h_inode->i_ino,
18071+ inode->i_ino);
18072+ if (unlikely(err))
18073+ AuIOErr1("failed au_xino_write() %d\n", err);
18074+ }
18075+
18076+ if (au_ftest_hi(flags, HNOTIFY)
18077+ && au_br_hnotifyable(br->br_perm)) {
027c5e7a 18078+ err = au_hn_alloc(hinode, inode);
4a4d8108
AM
18079+ if (unlikely(err))
18080+ AuIOErr1("au_hn_alloc() %d\n", err);
1308ab2a 18081+ }
18082+ }
4a4d8108 18083+}
dece6358 18084+
4a4d8108
AM
18085+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
18086+ struct dentry *h_wh)
18087+{
18088+ struct au_hinode *hinode;
dece6358 18089+
4a4d8108
AM
18090+ IiMustWriteLock(inode);
18091+
5afbbe0d 18092+ hinode = au_hinode(au_ii(inode), bindex);
4a4d8108
AM
18093+ AuDebugOn(hinode->hi_whdentry);
18094+ hinode->hi_whdentry = h_wh;
1facf9fc 18095+}
18096+
537831f9 18097+void au_update_iigen(struct inode *inode, int half)
1308ab2a 18098+{
537831f9
AM
18099+ struct au_iinfo *iinfo;
18100+ struct au_iigen *iigen;
18101+ unsigned int sigen;
18102+
18103+ sigen = au_sigen(inode->i_sb);
18104+ iinfo = au_ii(inode);
18105+ iigen = &iinfo->ii_generation;
be52b249 18106+ spin_lock(&iigen->ig_spin);
537831f9
AM
18107+ iigen->ig_generation = sigen;
18108+ if (half)
18109+ au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
18110+ else
18111+ au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
be52b249 18112+ spin_unlock(&iigen->ig_spin);
4a4d8108 18113+}
1facf9fc 18114+
4a4d8108
AM
18115+/* it may be called at remount time, too */
18116+void au_update_ibrange(struct inode *inode, int do_put_zero)
18117+{
18118+ struct au_iinfo *iinfo;
5afbbe0d 18119+ aufs_bindex_t bindex, bbot;
1facf9fc 18120+
5afbbe0d 18121+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 18122+ IiMustWriteLock(inode);
1facf9fc 18123+
5afbbe0d
AM
18124+ iinfo = au_ii(inode);
18125+ if (do_put_zero && iinfo->ii_btop >= 0) {
18126+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
18127+ bindex++) {
18128+ struct inode *h_i;
1facf9fc 18129+
5afbbe0d 18130+ h_i = au_hinode(iinfo, bindex)->hi_inode;
38d290e6
JR
18131+ if (h_i
18132+ && !h_i->i_nlink
18133+ && !(h_i->i_state & I_LINKABLE))
027c5e7a
AM
18134+ au_set_h_iptr(inode, bindex, NULL, 0);
18135+ }
4a4d8108
AM
18136+ }
18137+
5afbbe0d
AM
18138+ iinfo->ii_btop = -1;
18139+ iinfo->ii_bbot = -1;
18140+ bbot = au_sbbot(inode->i_sb);
18141+ for (bindex = 0; bindex <= bbot; bindex++)
18142+ if (au_hinode(iinfo, bindex)->hi_inode) {
18143+ iinfo->ii_btop = bindex;
4a4d8108 18144+ break;
027c5e7a 18145+ }
5afbbe0d
AM
18146+ if (iinfo->ii_btop >= 0)
18147+ for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
18148+ if (au_hinode(iinfo, bindex)->hi_inode) {
18149+ iinfo->ii_bbot = bindex;
027c5e7a
AM
18150+ break;
18151+ }
5afbbe0d 18152+ AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
1308ab2a 18153+}
1facf9fc 18154+
dece6358 18155+/* ---------------------------------------------------------------------- */
1facf9fc 18156+
4a4d8108 18157+void au_icntnr_init_once(void *_c)
dece6358 18158+{
4a4d8108
AM
18159+ struct au_icntnr *c = _c;
18160+ struct au_iinfo *iinfo = &c->iinfo;
1facf9fc 18161+
be52b249 18162+ spin_lock_init(&iinfo->ii_generation.ig_spin);
4a4d8108
AM
18163+ au_rw_init(&iinfo->ii_rwsem);
18164+ inode_init_once(&c->vfs_inode);
18165+}
1facf9fc 18166+
5afbbe0d
AM
18167+void au_hinode_init(struct au_hinode *hinode)
18168+{
18169+ hinode->hi_inode = NULL;
18170+ hinode->hi_id = -1;
18171+ au_hn_init(hinode);
18172+ hinode->hi_whdentry = NULL;
18173+}
18174+
4a4d8108
AM
18175+int au_iinfo_init(struct inode *inode)
18176+{
18177+ struct au_iinfo *iinfo;
18178+ struct super_block *sb;
5afbbe0d 18179+ struct au_hinode *hi;
4a4d8108 18180+ int nbr, i;
1facf9fc 18181+
4a4d8108
AM
18182+ sb = inode->i_sb;
18183+ iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
5afbbe0d 18184+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
18185+ if (unlikely(nbr <= 0))
18186+ nbr = 1;
5afbbe0d
AM
18187+ hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
18188+ if (hi) {
acd2b654 18189+ au_lcnt_inc(&au_sbi(sb)->si_ninodes);
5afbbe0d
AM
18190+
18191+ iinfo->ii_hinode = hi;
18192+ for (i = 0; i < nbr; i++, hi++)
18193+ au_hinode_init(hi);
1facf9fc 18194+
537831f9 18195+ iinfo->ii_generation.ig_generation = au_sigen(sb);
5afbbe0d
AM
18196+ iinfo->ii_btop = -1;
18197+ iinfo->ii_bbot = -1;
4a4d8108
AM
18198+ iinfo->ii_vdir = NULL;
18199+ return 0;
1308ab2a 18200+ }
4a4d8108
AM
18201+ return -ENOMEM;
18202+}
1facf9fc 18203+
e2f27e51 18204+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
4a4d8108 18205+{
5afbbe0d 18206+ int err, i;
4a4d8108 18207+ struct au_hinode *hip;
1facf9fc 18208+
4a4d8108
AM
18209+ AuRwMustWriteLock(&iinfo->ii_rwsem);
18210+
18211+ err = -ENOMEM;
e2f27e51
AM
18212+ hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
18213+ may_shrink);
4a4d8108
AM
18214+ if (hip) {
18215+ iinfo->ii_hinode = hip;
5afbbe0d
AM
18216+ i = iinfo->ii_bbot + 1;
18217+ hip += i;
18218+ for (; i < nbr; i++, hip++)
18219+ au_hinode_init(hip);
4a4d8108 18220+ err = 0;
1308ab2a 18221+ }
4a4d8108 18222+
1308ab2a 18223+ return err;
1facf9fc 18224+}
18225+
4a4d8108 18226+void au_iinfo_fin(struct inode *inode)
1facf9fc 18227+{
4a4d8108
AM
18228+ struct au_iinfo *iinfo;
18229+ struct au_hinode *hi;
18230+ struct super_block *sb;
5afbbe0d 18231+ aufs_bindex_t bindex, bbot;
b752ccd1 18232+ const unsigned char unlinked = !inode->i_nlink;
1308ab2a 18233+
5afbbe0d 18234+ AuDebugOn(au_is_bad_inode(inode));
1308ab2a 18235+
b752ccd1 18236+ sb = inode->i_sb;
acd2b654 18237+ au_lcnt_dec(&au_sbi(sb)->si_ninodes);
b752ccd1
AM
18238+ if (si_pid_test(sb))
18239+ au_xino_delete_inode(inode, unlinked);
18240+ else {
18241+ /*
18242+ * it is safe to hide the dependency between sbinfo and
18243+ * sb->s_umount.
18244+ */
18245+ lockdep_off();
18246+ si_noflush_read_lock(sb);
18247+ au_xino_delete_inode(inode, unlinked);
18248+ si_read_unlock(sb);
18249+ lockdep_on();
18250+ }
18251+
5afbbe0d 18252+ iinfo = au_ii(inode);
4a4d8108 18253+ if (iinfo->ii_vdir)
1c60b727 18254+ au_vdir_free(iinfo->ii_vdir);
1308ab2a 18255+
5afbbe0d 18256+ bindex = iinfo->ii_btop;
b752ccd1 18257+ if (bindex >= 0) {
5afbbe0d
AM
18258+ hi = au_hinode(iinfo, bindex);
18259+ bbot = iinfo->ii_bbot;
18260+ while (bindex++ <= bbot) {
b752ccd1 18261+ if (hi->hi_inode)
4a4d8108 18262+ au_hiput(hi);
4a4d8108
AM
18263+ hi++;
18264+ }
18265+ }
9f237c51 18266+ au_kfree_rcu(iinfo->ii_hinode);
4a4d8108 18267+ AuRwDestroy(&iinfo->ii_rwsem);
dece6358 18268+}
7f207e10
AM
18269diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
18270--- /usr/share/empty/fs/aufs/inode.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 18271+++ linux/fs/aufs/inode.c 2019-03-05 12:13:00.139224339 +0100
062440b3 18272@@ -0,0 +1,528 @@
cd7a4cd9 18273+// SPDX-License-Identifier: GPL-2.0
4a4d8108 18274+/*
ba1aed25 18275+ * Copyright (C) 2005-2019 Junjiro R. Okajima
4a4d8108
AM
18276+ *
18277+ * This program, aufs is free software; you can redistribute it and/or modify
18278+ * it under the terms of the GNU General Public License as published by
18279+ * the Free Software Foundation; either version 2 of the License, or
18280+ * (at your option) any later version.
18281+ *
18282+ * This program is distributed in the hope that it will be useful,
18283+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18284+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18285+ * GNU General Public License for more details.
18286+ *
18287+ * You should have received a copy of the GNU General Public License
523b37e3 18288+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 18289+ */
1facf9fc 18290+
4a4d8108
AM
18291+/*
18292+ * inode functions
18293+ */
1facf9fc 18294+
4a4d8108 18295+#include "aufs.h"
1308ab2a 18296+
4a4d8108
AM
18297+struct inode *au_igrab(struct inode *inode)
18298+{
18299+ if (inode) {
18300+ AuDebugOn(!atomic_read(&inode->i_count));
027c5e7a 18301+ ihold(inode);
1facf9fc 18302+ }
4a4d8108
AM
18303+ return inode;
18304+}
1facf9fc 18305+
4a4d8108
AM
18306+static void au_refresh_hinode_attr(struct inode *inode, int do_version)
18307+{
18308+ au_cpup_attr_all(inode, /*force*/0);
537831f9 18309+ au_update_iigen(inode, /*half*/1);
4a4d8108 18310+ if (do_version)
be118d29 18311+ inode_inc_iversion(inode);
dece6358 18312+}
1facf9fc 18313+
027c5e7a 18314+static int au_ii_refresh(struct inode *inode, int *update)
dece6358 18315+{
e2f27e51 18316+ int err, e, nbr;
027c5e7a 18317+ umode_t type;
4a4d8108 18318+ aufs_bindex_t bindex, new_bindex;
1308ab2a 18319+ struct super_block *sb;
4a4d8108 18320+ struct au_iinfo *iinfo;
027c5e7a 18321+ struct au_hinode *p, *q, tmp;
1facf9fc 18322+
5afbbe0d 18323+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 18324+ IiMustWriteLock(inode);
1facf9fc 18325+
027c5e7a 18326+ *update = 0;
4a4d8108 18327+ sb = inode->i_sb;
e2f27e51 18328+ nbr = au_sbbot(sb) + 1;
027c5e7a 18329+ type = inode->i_mode & S_IFMT;
4a4d8108 18330+ iinfo = au_ii(inode);
e2f27e51 18331+ err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
4a4d8108 18332+ if (unlikely(err))
1308ab2a 18333+ goto out;
1facf9fc 18334+
5afbbe0d
AM
18335+ AuDebugOn(iinfo->ii_btop < 0);
18336+ p = au_hinode(iinfo, iinfo->ii_btop);
18337+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
18338+ bindex++, p++) {
18339+ if (!p->hi_inode)
18340+ continue;
1facf9fc 18341+
027c5e7a 18342+ AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
4a4d8108
AM
18343+ new_bindex = au_br_index(sb, p->hi_id);
18344+ if (new_bindex == bindex)
18345+ continue;
1facf9fc 18346+
4a4d8108 18347+ if (new_bindex < 0) {
027c5e7a 18348+ *update = 1;
4a4d8108
AM
18349+ au_hiput(p);
18350+ p->hi_inode = NULL;
18351+ continue;
1308ab2a 18352+ }
4a4d8108 18353+
5afbbe0d
AM
18354+ if (new_bindex < iinfo->ii_btop)
18355+ iinfo->ii_btop = new_bindex;
18356+ if (iinfo->ii_bbot < new_bindex)
18357+ iinfo->ii_bbot = new_bindex;
4a4d8108 18358+ /* swap two lower inode, and loop again */
5afbbe0d 18359+ q = au_hinode(iinfo, new_bindex);
4a4d8108
AM
18360+ tmp = *q;
18361+ *q = *p;
18362+ *p = tmp;
18363+ if (tmp.hi_inode) {
18364+ bindex--;
18365+ p--;
1308ab2a 18366+ }
18367+ }
4a4d8108 18368+ au_update_ibrange(inode, /*do_put_zero*/0);
e2f27e51 18369+ au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
4a4d8108
AM
18370+ e = au_dy_irefresh(inode);
18371+ if (unlikely(e && !err))
18372+ err = e;
1facf9fc 18373+
4f0767ce 18374+out:
027c5e7a
AM
18375+ AuTraceErr(err);
18376+ return err;
18377+}
18378+
b95c5147
AM
18379+void au_refresh_iop(struct inode *inode, int force_getattr)
18380+{
18381+ int type;
18382+ struct au_sbinfo *sbi = au_sbi(inode->i_sb);
18383+ const struct inode_operations *iop
18384+ = force_getattr ? aufs_iop : sbi->si_iop_array;
18385+
18386+ if (inode->i_op == iop)
18387+ return;
18388+
18389+ switch (inode->i_mode & S_IFMT) {
18390+ case S_IFDIR:
18391+ type = AuIop_DIR;
18392+ break;
18393+ case S_IFLNK:
18394+ type = AuIop_SYMLINK;
18395+ break;
18396+ default:
18397+ type = AuIop_OTHER;
18398+ break;
18399+ }
18400+
18401+ inode->i_op = iop + type;
18402+ /* unnecessary smp_wmb() */
18403+}
18404+
027c5e7a
AM
18405+int au_refresh_hinode_self(struct inode *inode)
18406+{
18407+ int err, update;
18408+
18409+ err = au_ii_refresh(inode, &update);
18410+ if (!err)
18411+ au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
18412+
18413+ AuTraceErr(err);
4a4d8108
AM
18414+ return err;
18415+}
1facf9fc 18416+
4a4d8108
AM
18417+int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
18418+{
027c5e7a 18419+ int err, e, update;
4a4d8108 18420+ unsigned int flags;
027c5e7a 18421+ umode_t mode;
5afbbe0d 18422+ aufs_bindex_t bindex, bbot;
027c5e7a 18423+ unsigned char isdir;
4a4d8108
AM
18424+ struct au_hinode *p;
18425+ struct au_iinfo *iinfo;
1facf9fc 18426+
027c5e7a 18427+ err = au_ii_refresh(inode, &update);
4a4d8108
AM
18428+ if (unlikely(err))
18429+ goto out;
18430+
18431+ update = 0;
18432+ iinfo = au_ii(inode);
5afbbe0d 18433+ p = au_hinode(iinfo, iinfo->ii_btop);
027c5e7a
AM
18434+ mode = (inode->i_mode & S_IFMT);
18435+ isdir = S_ISDIR(mode);
4a4d8108 18436+ flags = au_hi_flags(inode, isdir);
5afbbe0d
AM
18437+ bbot = au_dbbot(dentry);
18438+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
5527c038 18439+ struct inode *h_i, *h_inode;
4a4d8108
AM
18440+ struct dentry *h_d;
18441+
18442+ h_d = au_h_dptr(dentry, bindex);
5527c038 18443+ if (!h_d || d_is_negative(h_d))
4a4d8108
AM
18444+ continue;
18445+
5527c038
JR
18446+ h_inode = d_inode(h_d);
18447+ AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
5afbbe0d 18448+ if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
4a4d8108
AM
18449+ h_i = au_h_iptr(inode, bindex);
18450+ if (h_i) {
5527c038 18451+ if (h_i == h_inode)
4a4d8108
AM
18452+ continue;
18453+ err = -EIO;
18454+ break;
18455+ }
18456+ }
5afbbe0d
AM
18457+ if (bindex < iinfo->ii_btop)
18458+ iinfo->ii_btop = bindex;
18459+ if (iinfo->ii_bbot < bindex)
18460+ iinfo->ii_bbot = bindex;
5527c038 18461+ au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
4a4d8108 18462+ update = 1;
1308ab2a 18463+ }
4a4d8108
AM
18464+ au_update_ibrange(inode, /*do_put_zero*/0);
18465+ e = au_dy_irefresh(inode);
18466+ if (unlikely(e && !err))
18467+ err = e;
027c5e7a
AM
18468+ if (!err)
18469+ au_refresh_hinode_attr(inode, update && isdir);
4a4d8108 18470+
4f0767ce 18471+out:
4a4d8108 18472+ AuTraceErr(err);
1308ab2a 18473+ return err;
dece6358
AM
18474+}
18475+
4a4d8108 18476+static int set_inode(struct inode *inode, struct dentry *dentry)
dece6358 18477+{
4a4d8108
AM
18478+ int err;
18479+ unsigned int flags;
18480+ umode_t mode;
5afbbe0d 18481+ aufs_bindex_t bindex, btop, btail;
4a4d8108
AM
18482+ unsigned char isdir;
18483+ struct dentry *h_dentry;
18484+ struct inode *h_inode;
18485+ struct au_iinfo *iinfo;
b95c5147 18486+ struct inode_operations *iop;
dece6358 18487+
4a4d8108 18488+ IiMustWriteLock(inode);
dece6358 18489+
4a4d8108
AM
18490+ err = 0;
18491+ isdir = 0;
b95c5147 18492+ iop = au_sbi(inode->i_sb)->si_iop_array;
5afbbe0d
AM
18493+ btop = au_dbtop(dentry);
18494+ h_dentry = au_h_dptr(dentry, btop);
5527c038 18495+ h_inode = d_inode(h_dentry);
4a4d8108
AM
18496+ mode = h_inode->i_mode;
18497+ switch (mode & S_IFMT) {
18498+ case S_IFREG:
18499+ btail = au_dbtail(dentry);
b95c5147 18500+ inode->i_op = iop + AuIop_OTHER;
4a4d8108 18501+ inode->i_fop = &aufs_file_fop;
5afbbe0d 18502+ err = au_dy_iaop(inode, btop, h_inode);
4a4d8108
AM
18503+ if (unlikely(err))
18504+ goto out;
18505+ break;
18506+ case S_IFDIR:
18507+ isdir = 1;
18508+ btail = au_dbtaildir(dentry);
b95c5147 18509+ inode->i_op = iop + AuIop_DIR;
4a4d8108
AM
18510+ inode->i_fop = &aufs_dir_fop;
18511+ break;
18512+ case S_IFLNK:
18513+ btail = au_dbtail(dentry);
b95c5147 18514+ inode->i_op = iop + AuIop_SYMLINK;
4a4d8108
AM
18515+ break;
18516+ case S_IFBLK:
18517+ case S_IFCHR:
18518+ case S_IFIFO:
18519+ case S_IFSOCK:
18520+ btail = au_dbtail(dentry);
b95c5147 18521+ inode->i_op = iop + AuIop_OTHER;
38d290e6 18522+ init_special_inode(inode, mode, h_inode->i_rdev);
4a4d8108
AM
18523+ break;
18524+ default:
18525+ AuIOErr("Unknown file type 0%o\n", mode);
18526+ err = -EIO;
1308ab2a 18527+ goto out;
4a4d8108 18528+ }
dece6358 18529+
4a4d8108
AM
18530+ /* do not set hnotify for whiteouted dirs (SHWH mode) */
18531+ flags = au_hi_flags(inode, isdir);
18532+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
18533+ && au_ftest_hi(flags, HNOTIFY)
18534+ && dentry->d_name.len > AUFS_WH_PFX_LEN
18535+ && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
18536+ au_fclr_hi(flags, HNOTIFY);
18537+ iinfo = au_ii(inode);
5afbbe0d
AM
18538+ iinfo->ii_btop = btop;
18539+ iinfo->ii_bbot = btail;
18540+ for (bindex = btop; bindex <= btail; bindex++) {
4a4d8108
AM
18541+ h_dentry = au_h_dptr(dentry, bindex);
18542+ if (h_dentry)
18543+ au_set_h_iptr(inode, bindex,
5527c038 18544+ au_igrab(d_inode(h_dentry)), flags);
4a4d8108
AM
18545+ }
18546+ au_cpup_attr_all(inode, /*force*/1);
c1595e42
JR
18547+ /*
18548+ * to force calling aufs_get_acl() every time,
18549+ * do not call cache_no_acl() for aufs inode.
18550+ */
dece6358 18551+
4f0767ce 18552+out:
4a4d8108
AM
18553+ return err;
18554+}
dece6358 18555+
027c5e7a
AM
18556+/*
18557+ * successful returns with iinfo write_locked
18558+ * minus: errno
18559+ * zero: success, matched
18560+ * plus: no error, but unmatched
18561+ */
18562+static int reval_inode(struct inode *inode, struct dentry *dentry)
4a4d8108
AM
18563+{
18564+ int err;
cfc41e69 18565+ unsigned int gen, igflags;
5afbbe0d 18566+ aufs_bindex_t bindex, bbot;
4a4d8108 18567+ struct inode *h_inode, *h_dinode;
5527c038 18568+ struct dentry *h_dentry;
dece6358 18569+
4a4d8108
AM
18570+ /*
18571+ * before this function, if aufs got any iinfo lock, it must be only
18572+ * one, the parent dir.
18573+ * it can happen by UDBA and the obsoleted inode number.
18574+ */
18575+ err = -EIO;
18576+ if (unlikely(inode->i_ino == parent_ino(dentry)))
18577+ goto out;
18578+
027c5e7a 18579+ err = 1;
4a4d8108 18580+ ii_write_lock_new_child(inode);
5afbbe0d 18581+ h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
5527c038 18582+ h_dinode = d_inode(h_dentry);
5afbbe0d
AM
18583+ bbot = au_ibbot(inode);
18584+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108 18585+ h_inode = au_h_iptr(inode, bindex);
537831f9
AM
18586+ if (!h_inode || h_inode != h_dinode)
18587+ continue;
18588+
18589+ err = 0;
cfc41e69 18590+ gen = au_iigen(inode, &igflags);
537831f9 18591+ if (gen == au_digen(dentry)
cfc41e69 18592+ && !au_ig_ftest(igflags, HALF_REFRESHED))
4a4d8108 18593+ break;
537831f9
AM
18594+
18595+ /* fully refresh inode using dentry */
18596+ err = au_refresh_hinode(inode, dentry);
18597+ if (!err)
18598+ au_update_iigen(inode, /*half*/0);
18599+ break;
1facf9fc 18600+ }
dece6358 18601+
4a4d8108
AM
18602+ if (unlikely(err))
18603+ ii_write_unlock(inode);
4f0767ce 18604+out:
1facf9fc 18605+ return err;
18606+}
1facf9fc 18607+
4a4d8108
AM
18608+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18609+ unsigned int d_type, ino_t *ino)
1facf9fc 18610+{
521ced18
JR
18611+ int err, idx;
18612+ const int isnondir = d_type != DT_DIR;
1facf9fc 18613+
b752ccd1 18614+ /* prevent hardlinked inode number from race condition */
521ced18
JR
18615+ if (isnondir) {
18616+ err = au_xinondir_enter(sb, bindex, h_ino, &idx);
18617+ if (unlikely(err))
18618+ goto out;
4a4d8108 18619+ }
521ced18 18620+
4a4d8108
AM
18621+ err = au_xino_read(sb, bindex, h_ino, ino);
18622+ if (unlikely(err))
521ced18 18623+ goto out_xinondir;
1308ab2a 18624+
4a4d8108
AM
18625+ if (!*ino) {
18626+ err = -EIO;
18627+ *ino = au_xino_new_ino(sb);
18628+ if (unlikely(!*ino))
521ced18 18629+ goto out_xinondir;
4a4d8108
AM
18630+ err = au_xino_write(sb, bindex, h_ino, *ino);
18631+ if (unlikely(err))
521ced18 18632+ goto out_xinondir;
1308ab2a 18633+ }
1facf9fc 18634+
521ced18
JR
18635+out_xinondir:
18636+ if (isnondir && idx >= 0)
18637+ au_xinondir_leave(sb, bindex, h_ino, idx);
4f0767ce 18638+out:
1facf9fc 18639+ return err;
18640+}
18641+
4a4d8108
AM
18642+/* successful returns with iinfo write_locked */
18643+/* todo: return with unlocked? */
18644+struct inode *au_new_inode(struct dentry *dentry, int must_new)
1facf9fc 18645+{
5527c038 18646+ struct inode *inode, *h_inode;
4a4d8108
AM
18647+ struct dentry *h_dentry;
18648+ struct super_block *sb;
18649+ ino_t h_ino, ino;
521ced18 18650+ int err, idx, hlinked;
5afbbe0d 18651+ aufs_bindex_t btop;
1facf9fc 18652+
4a4d8108 18653+ sb = dentry->d_sb;
5afbbe0d
AM
18654+ btop = au_dbtop(dentry);
18655+ h_dentry = au_h_dptr(dentry, btop);
5527c038
JR
18656+ h_inode = d_inode(h_dentry);
18657+ h_ino = h_inode->i_ino;
521ced18 18658+ hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
b752ccd1 18659+
521ced18 18660+new_ino:
b752ccd1
AM
18661+ /*
18662+ * stop 'race'-ing between hardlinks under different
18663+ * parents.
18664+ */
521ced18
JR
18665+ if (hlinked) {
18666+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18667+ inode = ERR_PTR(err);
18668+ if (unlikely(err))
18669+ goto out;
18670+ }
b752ccd1 18671+
5afbbe0d 18672+ err = au_xino_read(sb, btop, h_ino, &ino);
4a4d8108
AM
18673+ inode = ERR_PTR(err);
18674+ if (unlikely(err))
521ced18 18675+ goto out_xinondir;
b752ccd1 18676+
4a4d8108
AM
18677+ if (!ino) {
18678+ ino = au_xino_new_ino(sb);
18679+ if (unlikely(!ino)) {
18680+ inode = ERR_PTR(-EIO);
521ced18 18681+ goto out_xinondir;
dece6358
AM
18682+ }
18683+ }
1facf9fc 18684+
4a4d8108
AM
18685+ AuDbg("i%lu\n", (unsigned long)ino);
18686+ inode = au_iget_locked(sb, ino);
18687+ err = PTR_ERR(inode);
18688+ if (IS_ERR(inode))
521ced18 18689+ goto out_xinondir;
1facf9fc 18690+
4a4d8108
AM
18691+ AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
18692+ if (inode->i_state & I_NEW) {
18693+ ii_write_lock_new_child(inode);
18694+ err = set_inode(inode, dentry);
18695+ if (!err) {
18696+ unlock_new_inode(inode);
521ced18 18697+ goto out_xinondir; /* success */
4a4d8108 18698+ }
1308ab2a 18699+
027c5e7a
AM
18700+ /*
18701+ * iget_failed() calls iput(), but we need to call
18702+ * ii_write_unlock() after iget_failed(). so dirty hack for
18703+ * i_count.
18704+ */
18705+ atomic_inc(&inode->i_count);
4a4d8108 18706+ iget_failed(inode);
027c5e7a 18707+ ii_write_unlock(inode);
5afbbe0d 18708+ au_xino_write(sb, btop, h_ino, /*ino*/0);
027c5e7a
AM
18709+ /* ignore this error */
18710+ goto out_iput;
18711+ } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
b752ccd1
AM
18712+ /*
18713+ * horrible race condition between lookup, readdir and copyup
18714+ * (or something).
18715+ */
521ced18
JR
18716+ if (hlinked && idx >= 0)
18717+ au_xinondir_leave(sb, btop, h_ino, idx);
027c5e7a
AM
18718+ err = reval_inode(inode, dentry);
18719+ if (unlikely(err < 0)) {
521ced18 18720+ hlinked = 0;
027c5e7a
AM
18721+ goto out_iput;
18722+ }
521ced18 18723+ if (!err)
4a4d8108 18724+ goto out; /* success */
521ced18
JR
18725+ else if (hlinked && idx >= 0) {
18726+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18727+ if (unlikely(err)) {
18728+ iput(inode);
18729+ inode = ERR_PTR(err);
18730+ goto out;
18731+ }
18732+ }
4a4d8108
AM
18733+ }
18734+
5527c038 18735+ if (unlikely(au_test_fs_unique_ino(h_inode)))
4a4d8108 18736+ AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
523b37e3 18737+ " b%d, %s, %pd, hi%lu, i%lu.\n",
5afbbe0d 18738+ btop, au_sbtype(h_dentry->d_sb), dentry,
4a4d8108
AM
18739+ (unsigned long)h_ino, (unsigned long)ino);
18740+ ino = 0;
5afbbe0d 18741+ err = au_xino_write(sb, btop, h_ino, /*ino*/0);
4a4d8108
AM
18742+ if (!err) {
18743+ iput(inode);
521ced18
JR
18744+ if (hlinked && idx >= 0)
18745+ au_xinondir_leave(sb, btop, h_ino, idx);
4a4d8108
AM
18746+ goto new_ino;
18747+ }
1308ab2a 18748+
4f0767ce 18749+out_iput:
4a4d8108 18750+ iput(inode);
4a4d8108 18751+ inode = ERR_PTR(err);
521ced18
JR
18752+out_xinondir:
18753+ if (hlinked && idx >= 0)
18754+ au_xinondir_leave(sb, btop, h_ino, idx);
4f0767ce 18755+out:
4a4d8108 18756+ return inode;
1facf9fc 18757+}
18758+
4a4d8108 18759+/* ---------------------------------------------------------------------- */
1facf9fc 18760+
4a4d8108
AM
18761+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18762+ struct inode *inode)
18763+{
18764+ int err;
076b876e 18765+ struct inode *hi;
1facf9fc 18766+
4a4d8108 18767+ err = au_br_rdonly(au_sbr(sb, bindex));
1facf9fc 18768+
4a4d8108
AM
18769+ /* pseudo-link after flushed may happen out of bounds */
18770+ if (!err
18771+ && inode
5afbbe0d
AM
18772+ && au_ibtop(inode) <= bindex
18773+ && bindex <= au_ibbot(inode)) {
4a4d8108
AM
18774+ /*
18775+ * permission check is unnecessary since vfsub routine
18776+ * will be called later
18777+ */
076b876e 18778+ hi = au_h_iptr(inode, bindex);
4a4d8108
AM
18779+ if (hi)
18780+ err = IS_IMMUTABLE(hi) ? -EROFS : 0;
1facf9fc 18781+ }
18782+
4a4d8108
AM
18783+ return err;
18784+}
dece6358 18785+
4a4d8108
AM
18786+int au_test_h_perm(struct inode *h_inode, int mask)
18787+{
2dfbb274 18788+ if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
4a4d8108
AM
18789+ return 0;
18790+ return inode_permission(h_inode, mask);
18791+}
1facf9fc 18792+
4a4d8108
AM
18793+int au_test_h_perm_sio(struct inode *h_inode, int mask)
18794+{
18795+ if (au_test_nfs(h_inode->i_sb)
18796+ && (mask & MAY_WRITE)
18797+ && S_ISDIR(h_inode->i_mode))
18798+ mask |= MAY_READ; /* force permission check */
18799+ return au_test_h_perm(h_inode, mask);
1facf9fc 18800+}
7f207e10
AM
18801diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
18802--- /usr/share/empty/fs/aufs/inode.h 1970-01-01 01:00:00.000000000 +0100
ba1aed25 18803+++ linux/fs/aufs/inode.h 2019-03-05 12:13:00.142557771 +0100
9f237c51 18804@@ -0,0 +1,698 @@
062440b3 18805+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 18806+/*
ba1aed25 18807+ * Copyright (C) 2005-2019 Junjiro R. Okajima
4a4d8108
AM
18808+ *
18809+ * This program, aufs is free software; you can redistribute it and/or modify
18810+ * it under the terms of the GNU General Public License as published by
18811+ * the Free Software Foundation; either version 2 of the License, or
18812+ * (at your option) any later version.
18813+ *
18814+ * This program is distributed in the hope that it will be useful,
18815+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18816+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18817+ * GNU General Public License for more details.
18818+ *
18819+ * You should have received a copy of the GNU General Public License
523b37e3 18820+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 18821+ */
1facf9fc 18822+
1308ab2a 18823+/*
4a4d8108 18824+ * inode operations
1308ab2a 18825+ */
dece6358 18826+
4a4d8108
AM
18827+#ifndef __AUFS_INODE_H__
18828+#define __AUFS_INODE_H__
dece6358 18829+
4a4d8108 18830+#ifdef __KERNEL__
1308ab2a 18831+
4a4d8108 18832+#include <linux/fsnotify.h>
4a4d8108 18833+#include "rwsem.h"
1308ab2a 18834+
4a4d8108 18835+struct vfsmount;
1facf9fc 18836+
4a4d8108
AM
18837+struct au_hnotify {
18838+#ifdef CONFIG_AUFS_HNOTIFY
18839+#ifdef CONFIG_AUFS_HFSNOTIFY
7f207e10 18840+ /* never use fsnotify_add_vfsmount_mark() */
0c5527e5 18841+ struct fsnotify_mark hn_mark;
4a4d8108 18842+#endif
1c60b727 18843+ struct inode *hn_aufs_inode; /* no get/put */
9f237c51 18844+ struct rcu_head rcu;
4a4d8108
AM
18845+#endif
18846+} ____cacheline_aligned_in_smp;
1facf9fc 18847+
4a4d8108
AM
18848+struct au_hinode {
18849+ struct inode *hi_inode;
18850+ aufs_bindex_t hi_id;
18851+#ifdef CONFIG_AUFS_HNOTIFY
18852+ struct au_hnotify *hi_notify;
18853+#endif
dece6358 18854+
4a4d8108
AM
18855+ /* reference to the copied-up whiteout with get/put */
18856+ struct dentry *hi_whdentry;
18857+};
dece6358 18858+
537831f9
AM
18859+/* ig_flags */
18860+#define AuIG_HALF_REFRESHED 1
18861+#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
18862+#define au_ig_fset(flags, name) \
18863+ do { (flags) |= AuIG_##name; } while (0)
18864+#define au_ig_fclr(flags, name) \
18865+ do { (flags) &= ~AuIG_##name; } while (0)
18866+
18867+struct au_iigen {
be52b249 18868+ spinlock_t ig_spin;
537831f9
AM
18869+ __u32 ig_generation, ig_flags;
18870+};
18871+
4a4d8108
AM
18872+struct au_vdir;
18873+struct au_iinfo {
7a9e40b8 18874+ struct au_iigen ii_generation;
4a4d8108 18875+ struct super_block *ii_hsb1; /* no get/put */
1facf9fc 18876+
4a4d8108 18877+ struct au_rwsem ii_rwsem;
5afbbe0d 18878+ aufs_bindex_t ii_btop, ii_bbot;
4a4d8108
AM
18879+ __u32 ii_higen;
18880+ struct au_hinode *ii_hinode;
18881+ struct au_vdir *ii_vdir;
18882+};
1facf9fc 18883+
4a4d8108 18884+struct au_icntnr {
9f237c51
AM
18885+ struct au_iinfo iinfo;
18886+ struct inode vfs_inode;
18887+ struct hlist_bl_node plink;
18888+ struct rcu_head rcu;
4a4d8108 18889+} ____cacheline_aligned_in_smp;
1308ab2a 18890+
4a4d8108
AM
18891+/* au_pin flags */
18892+#define AuPin_DI_LOCKED 1
18893+#define AuPin_MNT_WRITE (1 << 1)
18894+#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
7f207e10
AM
18895+#define au_fset_pin(flags, name) \
18896+ do { (flags) |= AuPin_##name; } while (0)
18897+#define au_fclr_pin(flags, name) \
18898+ do { (flags) &= ~AuPin_##name; } while (0)
4a4d8108
AM
18899+
18900+struct au_pin {
18901+ /* input */
18902+ struct dentry *dentry;
18903+ unsigned int udba;
18904+ unsigned char lsc_di, lsc_hi, flags;
18905+ aufs_bindex_t bindex;
18906+
18907+ /* output */
18908+ struct dentry *parent;
18909+ struct au_hinode *hdir;
18910+ struct vfsmount *h_mnt;
86dc4139
AM
18911+
18912+ /* temporary unlock/relock for copyup */
18913+ struct dentry *h_dentry, *h_parent;
18914+ struct au_branch *br;
18915+ struct task_struct *task;
4a4d8108 18916+};
1facf9fc 18917+
86dc4139 18918+void au_pin_hdir_unlock(struct au_pin *p);
c1595e42 18919+int au_pin_hdir_lock(struct au_pin *p);
86dc4139 18920+int au_pin_hdir_relock(struct au_pin *p);
86dc4139
AM
18921+void au_pin_hdir_acquire_nest(struct au_pin *p);
18922+void au_pin_hdir_release(struct au_pin *p);
18923+
1308ab2a 18924+/* ---------------------------------------------------------------------- */
18925+
4a4d8108 18926+static inline struct au_iinfo *au_ii(struct inode *inode)
1facf9fc 18927+{
5afbbe0d
AM
18928+ BUG_ON(is_bad_inode(inode));
18929+ return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
4a4d8108 18930+}
1facf9fc 18931+
4a4d8108 18932+/* ---------------------------------------------------------------------- */
1facf9fc 18933+
4a4d8108
AM
18934+/* inode.c */
18935+struct inode *au_igrab(struct inode *inode);
b95c5147 18936+void au_refresh_iop(struct inode *inode, int force_getattr);
027c5e7a 18937+int au_refresh_hinode_self(struct inode *inode);
4a4d8108
AM
18938+int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
18939+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18940+ unsigned int d_type, ino_t *ino);
18941+struct inode *au_new_inode(struct dentry *dentry, int must_new);
18942+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18943+ struct inode *inode);
18944+int au_test_h_perm(struct inode *h_inode, int mask);
18945+int au_test_h_perm_sio(struct inode *h_inode, int mask);
1facf9fc 18946+
4a4d8108
AM
18947+static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
18948+ ino_t h_ino, unsigned int d_type, ino_t *ino)
18949+{
18950+#ifdef CONFIG_AUFS_SHWH
18951+ return au_ino(sb, bindex, h_ino, d_type, ino);
18952+#else
18953+ return 0;
18954+#endif
18955+}
1facf9fc 18956+
4a4d8108 18957+/* i_op.c */
b95c5147
AM
18958+enum {
18959+ AuIop_SYMLINK,
18960+ AuIop_DIR,
18961+ AuIop_OTHER,
18962+ AuIop_Last
18963+};
18964+extern struct inode_operations aufs_iop[AuIop_Last],
18965+ aufs_iop_nogetattr[AuIop_Last];
1308ab2a 18966+
4a4d8108
AM
18967+/* au_wr_dir flags */
18968+#define AuWrDir_ADD_ENTRY 1
7e9cd9fe
AM
18969+#define AuWrDir_ISDIR (1 << 1)
18970+#define AuWrDir_TMPFILE (1 << 2)
4a4d8108 18971+#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
7f207e10
AM
18972+#define au_fset_wrdir(flags, name) \
18973+ do { (flags) |= AuWrDir_##name; } while (0)
18974+#define au_fclr_wrdir(flags, name) \
18975+ do { (flags) &= ~AuWrDir_##name; } while (0)
1facf9fc 18976+
4a4d8108
AM
18977+struct au_wr_dir_args {
18978+ aufs_bindex_t force_btgt;
18979+ unsigned char flags;
18980+};
18981+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18982+ struct au_wr_dir_args *args);
dece6358 18983+
4a4d8108
AM
18984+struct dentry *au_pinned_h_parent(struct au_pin *pin);
18985+void au_pin_init(struct au_pin *pin, struct dentry *dentry,
18986+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18987+ unsigned int udba, unsigned char flags);
18988+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18989+ unsigned int udba, unsigned char flags) __must_check;
18990+int au_do_pin(struct au_pin *pin) __must_check;
18991+void au_unpin(struct au_pin *pin);
c1595e42
JR
18992+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
18993+
18994+#define AuIcpup_DID_CPUP 1
18995+#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
18996+#define au_fset_icpup(flags, name) \
18997+ do { (flags) |= AuIcpup_##name; } while (0)
18998+#define au_fclr_icpup(flags, name) \
18999+ do { (flags) &= ~AuIcpup_##name; } while (0)
19000+
19001+struct au_icpup_args {
19002+ unsigned char flags;
19003+ unsigned char pin_flags;
19004+ aufs_bindex_t btgt;
19005+ unsigned int udba;
19006+ struct au_pin pin;
19007+ struct path h_path;
19008+ struct inode *h_inode;
19009+};
19010+
19011+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
19012+ struct au_icpup_args *a);
19013+
a2654f78
AM
19014+int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
19015+ int locked);
1facf9fc 19016+
4a4d8108
AM
19017+/* i_op_add.c */
19018+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19019+ struct dentry *h_parent, int isdir);
7eafdf33
AM
19020+int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
19021+ dev_t dev);
4a4d8108 19022+int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
7eafdf33 19023+int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
b4510431 19024+ bool want_excl);
b912730e
AM
19025+struct vfsub_aopen_args;
19026+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
19027+ struct vfsub_aopen_args *args);
38d290e6 19028+int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
4a4d8108
AM
19029+int aufs_link(struct dentry *src_dentry, struct inode *dir,
19030+ struct dentry *dentry);
7eafdf33 19031+int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
1facf9fc 19032+
4a4d8108
AM
19033+/* i_op_del.c */
19034+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
19035+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
19036+ struct dentry *h_parent, int isdir);
19037+int aufs_unlink(struct inode *dir, struct dentry *dentry);
19038+int aufs_rmdir(struct inode *dir, struct dentry *dentry);
1308ab2a 19039+
4a4d8108
AM
19040+/* i_op_ren.c */
19041+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
19042+int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
f2c43d5f
AM
19043+ struct inode *dir, struct dentry *dentry,
19044+ unsigned int flags);
1facf9fc 19045+
4a4d8108
AM
19046+/* iinfo.c */
19047+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
19048+void au_hiput(struct au_hinode *hinode);
19049+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
19050+ struct dentry *h_wh);
19051+unsigned int au_hi_flags(struct inode *inode, int isdir);
1308ab2a 19052+
4a4d8108
AM
19053+/* hinode flags */
19054+#define AuHi_XINO 1
19055+#define AuHi_HNOTIFY (1 << 1)
19056+#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
7f207e10
AM
19057+#define au_fset_hi(flags, name) \
19058+ do { (flags) |= AuHi_##name; } while (0)
19059+#define au_fclr_hi(flags, name) \
19060+ do { (flags) &= ~AuHi_##name; } while (0)
1facf9fc 19061+
4a4d8108
AM
19062+#ifndef CONFIG_AUFS_HNOTIFY
19063+#undef AuHi_HNOTIFY
19064+#define AuHi_HNOTIFY 0
19065+#endif
1facf9fc 19066+
4a4d8108
AM
19067+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
19068+ struct inode *h_inode, unsigned int flags);
1facf9fc 19069+
537831f9 19070+void au_update_iigen(struct inode *inode, int half);
4a4d8108 19071+void au_update_ibrange(struct inode *inode, int do_put_zero);
1facf9fc 19072+
4a4d8108 19073+void au_icntnr_init_once(void *_c);
5afbbe0d 19074+void au_hinode_init(struct au_hinode *hinode);
4a4d8108
AM
19075+int au_iinfo_init(struct inode *inode);
19076+void au_iinfo_fin(struct inode *inode);
e2f27e51 19077+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
1308ab2a 19078+
e49829fe 19079+#ifdef CONFIG_PROC_FS
4a4d8108 19080+/* plink.c */
e49829fe 19081+int au_plink_maint(struct super_block *sb, int flags);
7e9cd9fe 19082+struct au_sbinfo;
e49829fe
JR
19083+void au_plink_maint_leave(struct au_sbinfo *sbinfo);
19084+int au_plink_maint_enter(struct super_block *sb);
4a4d8108
AM
19085+#ifdef CONFIG_AUFS_DEBUG
19086+void au_plink_list(struct super_block *sb);
19087+#else
19088+AuStubVoid(au_plink_list, struct super_block *sb)
19089+#endif
19090+int au_plink_test(struct inode *inode);
19091+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
19092+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
19093+ struct dentry *h_dentry);
e49829fe
JR
19094+void au_plink_put(struct super_block *sb, int verbose);
19095+void au_plink_clean(struct super_block *sb, int verbose);
4a4d8108 19096+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
e49829fe
JR
19097+#else
19098+AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
19099+AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
19100+AuStubInt0(au_plink_maint_enter, struct super_block *sb);
19101+AuStubVoid(au_plink_list, struct super_block *sb);
19102+AuStubInt0(au_plink_test, struct inode *inode);
19103+AuStub(struct dentry *, au_plink_lkup, return NULL,
19104+ struct inode *inode, aufs_bindex_t bindex);
19105+AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
19106+ struct dentry *h_dentry);
19107+AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
19108+AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
19109+AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
19110+#endif /* CONFIG_PROC_FS */
1facf9fc 19111+
c1595e42
JR
19112+#ifdef CONFIG_AUFS_XATTR
19113+/* xattr.c */
7e9cd9fe
AM
19114+int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
19115+ unsigned int verbose);
c1595e42 19116+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
f2c43d5f 19117+void au_xattr_init(struct super_block *sb);
c1595e42
JR
19118+#else
19119+AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
7e9cd9fe 19120+ int ignore_flags, unsigned int verbose);
f2c43d5f 19121+AuStubVoid(au_xattr_init, struct super_block *sb);
c1595e42
JR
19122+#endif
19123+
19124+#ifdef CONFIG_FS_POSIX_ACL
19125+struct posix_acl *aufs_get_acl(struct inode *inode, int type);
19126+int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
19127+#endif
19128+
19129+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
19130+enum {
19131+ AU_XATTR_SET,
c1595e42
JR
19132+ AU_ACL_SET
19133+};
19134+
f2c43d5f 19135+struct au_sxattr {
c1595e42
JR
19136+ int type;
19137+ union {
19138+ struct {
19139+ const char *name;
19140+ const void *value;
19141+ size_t size;
19142+ int flags;
19143+ } set;
19144+ struct {
c1595e42
JR
19145+ struct posix_acl *acl;
19146+ int type;
19147+ } acl_set;
19148+ } u;
19149+};
f2c43d5f
AM
19150+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
19151+ struct au_sxattr *arg);
c1595e42
JR
19152+#endif
19153+
4a4d8108 19154+/* ---------------------------------------------------------------------- */
1308ab2a 19155+
4a4d8108
AM
19156+/* lock subclass for iinfo */
19157+enum {
19158+ AuLsc_II_CHILD, /* child first */
19159+ AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
19160+ AuLsc_II_CHILD3, /* copyup dirs */
19161+ AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
19162+ AuLsc_II_PARENT2,
19163+ AuLsc_II_PARENT3, /* copyup dirs */
19164+ AuLsc_II_NEW_CHILD
19165+};
1308ab2a 19166+
1facf9fc 19167+/*
4a4d8108
AM
19168+ * ii_read_lock_child, ii_write_lock_child,
19169+ * ii_read_lock_child2, ii_write_lock_child2,
19170+ * ii_read_lock_child3, ii_write_lock_child3,
19171+ * ii_read_lock_parent, ii_write_lock_parent,
19172+ * ii_read_lock_parent2, ii_write_lock_parent2,
19173+ * ii_read_lock_parent3, ii_write_lock_parent3,
19174+ * ii_read_lock_new_child, ii_write_lock_new_child,
1facf9fc 19175+ */
4a4d8108
AM
19176+#define AuReadLockFunc(name, lsc) \
19177+static inline void ii_read_lock_##name(struct inode *i) \
19178+{ \
19179+ au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19180+}
19181+
19182+#define AuWriteLockFunc(name, lsc) \
19183+static inline void ii_write_lock_##name(struct inode *i) \
19184+{ \
19185+ au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19186+}
19187+
19188+#define AuRWLockFuncs(name, lsc) \
19189+ AuReadLockFunc(name, lsc) \
19190+ AuWriteLockFunc(name, lsc)
19191+
19192+AuRWLockFuncs(child, CHILD);
19193+AuRWLockFuncs(child2, CHILD2);
19194+AuRWLockFuncs(child3, CHILD3);
19195+AuRWLockFuncs(parent, PARENT);
19196+AuRWLockFuncs(parent2, PARENT2);
19197+AuRWLockFuncs(parent3, PARENT3);
19198+AuRWLockFuncs(new_child, NEW_CHILD);
19199+
19200+#undef AuReadLockFunc
19201+#undef AuWriteLockFunc
19202+#undef AuRWLockFuncs
1facf9fc 19203+
8b6a4947
AM
19204+#define ii_read_unlock(i) au_rw_read_unlock(&au_ii(i)->ii_rwsem)
19205+#define ii_write_unlock(i) au_rw_write_unlock(&au_ii(i)->ii_rwsem)
19206+#define ii_downgrade_lock(i) au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
1facf9fc 19207+
4a4d8108
AM
19208+#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
19209+#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
19210+#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
1facf9fc 19211+
4a4d8108 19212+/* ---------------------------------------------------------------------- */
1308ab2a 19213+
027c5e7a
AM
19214+static inline void au_icntnr_init(struct au_icntnr *c)
19215+{
19216+#ifdef CONFIG_AUFS_DEBUG
19217+ c->vfs_inode.i_mode = 0;
19218+#endif
19219+}
19220+
cfc41e69 19221+static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
4a4d8108 19222+{
537831f9
AM
19223+ unsigned int gen;
19224+ struct au_iinfo *iinfo;
be52b249 19225+ struct au_iigen *iigen;
537831f9
AM
19226+
19227+ iinfo = au_ii(inode);
be52b249
AM
19228+ iigen = &iinfo->ii_generation;
19229+ spin_lock(&iigen->ig_spin);
cfc41e69
AM
19230+ if (igflags)
19231+ *igflags = iigen->ig_flags;
be52b249
AM
19232+ gen = iigen->ig_generation;
19233+ spin_unlock(&iigen->ig_spin);
537831f9
AM
19234+
19235+ return gen;
4a4d8108 19236+}
1308ab2a 19237+
4a4d8108
AM
19238+/* tiny test for inode number */
19239+/* tmpfs generation is too rough */
19240+static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
19241+{
19242+ struct au_iinfo *iinfo;
1308ab2a 19243+
4a4d8108
AM
19244+ iinfo = au_ii(inode);
19245+ AuRwMustAnyLock(&iinfo->ii_rwsem);
19246+ return !(iinfo->ii_hsb1 == h_inode->i_sb
19247+ && iinfo->ii_higen == h_inode->i_generation);
19248+}
1308ab2a 19249+
4a4d8108
AM
19250+static inline void au_iigen_dec(struct inode *inode)
19251+{
537831f9 19252+ struct au_iinfo *iinfo;
be52b249 19253+ struct au_iigen *iigen;
537831f9
AM
19254+
19255+ iinfo = au_ii(inode);
be52b249
AM
19256+ iigen = &iinfo->ii_generation;
19257+ spin_lock(&iigen->ig_spin);
19258+ iigen->ig_generation--;
19259+ spin_unlock(&iigen->ig_spin);
027c5e7a
AM
19260+}
19261+
19262+static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
19263+{
19264+ int err;
19265+
19266+ err = 0;
537831f9 19267+ if (unlikely(inode && au_iigen(inode, NULL) != sigen))
027c5e7a
AM
19268+ err = -EIO;
19269+
19270+ return err;
4a4d8108 19271+}
1308ab2a 19272+
4a4d8108 19273+/* ---------------------------------------------------------------------- */
1308ab2a 19274+
5afbbe0d
AM
19275+static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
19276+ aufs_bindex_t bindex)
19277+{
19278+ return iinfo->ii_hinode + bindex;
19279+}
19280+
19281+static inline int au_is_bad_inode(struct inode *inode)
19282+{
19283+ return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
19284+}
19285+
4a4d8108
AM
19286+static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
19287+ aufs_bindex_t bindex)
19288+{
19289+ IiMustAnyLock(inode);
5afbbe0d 19290+ return au_hinode(au_ii(inode), bindex)->hi_id;
4a4d8108 19291+}
1308ab2a 19292+
5afbbe0d 19293+static inline aufs_bindex_t au_ibtop(struct inode *inode)
4a4d8108
AM
19294+{
19295+ IiMustAnyLock(inode);
5afbbe0d 19296+ return au_ii(inode)->ii_btop;
4a4d8108 19297+}
1308ab2a 19298+
5afbbe0d 19299+static inline aufs_bindex_t au_ibbot(struct inode *inode)
4a4d8108
AM
19300+{
19301+ IiMustAnyLock(inode);
5afbbe0d 19302+ return au_ii(inode)->ii_bbot;
4a4d8108 19303+}
1308ab2a 19304+
4a4d8108
AM
19305+static inline struct au_vdir *au_ivdir(struct inode *inode)
19306+{
19307+ IiMustAnyLock(inode);
19308+ return au_ii(inode)->ii_vdir;
19309+}
1308ab2a 19310+
4a4d8108
AM
19311+static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
19312+{
19313+ IiMustAnyLock(inode);
5afbbe0d 19314+ return au_hinode(au_ii(inode), bindex)->hi_whdentry;
4a4d8108 19315+}
1308ab2a 19316+
5afbbe0d 19317+static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 19318+{
4a4d8108 19319+ IiMustWriteLock(inode);
5afbbe0d 19320+ au_ii(inode)->ii_btop = bindex;
4a4d8108 19321+}
1308ab2a 19322+
5afbbe0d 19323+static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
4a4d8108
AM
19324+{
19325+ IiMustWriteLock(inode);
5afbbe0d 19326+ au_ii(inode)->ii_bbot = bindex;
1308ab2a 19327+}
19328+
4a4d8108
AM
19329+static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
19330+{
19331+ IiMustWriteLock(inode);
19332+ au_ii(inode)->ii_vdir = vdir;
19333+}
1facf9fc 19334+
4a4d8108 19335+static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 19336+{
4a4d8108 19337+ IiMustAnyLock(inode);
5afbbe0d 19338+ return au_hinode(au_ii(inode), bindex);
4a4d8108 19339+}
dece6358 19340+
4a4d8108 19341+/* ---------------------------------------------------------------------- */
1facf9fc 19342+
4a4d8108
AM
19343+static inline struct dentry *au_pinned_parent(struct au_pin *pin)
19344+{
19345+ if (pin)
19346+ return pin->parent;
19347+ return NULL;
1facf9fc 19348+}
19349+
4a4d8108 19350+static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
1facf9fc 19351+{
4a4d8108
AM
19352+ if (pin && pin->hdir)
19353+ return pin->hdir->hi_inode;
19354+ return NULL;
1308ab2a 19355+}
1facf9fc 19356+
4a4d8108
AM
19357+static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
19358+{
19359+ if (pin)
19360+ return pin->hdir;
19361+ return NULL;
19362+}
1facf9fc 19363+
4a4d8108 19364+static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
1308ab2a 19365+{
4a4d8108
AM
19366+ if (pin)
19367+ pin->dentry = dentry;
19368+}
1308ab2a 19369+
4a4d8108
AM
19370+static inline void au_pin_set_parent_lflag(struct au_pin *pin,
19371+ unsigned char lflag)
19372+{
19373+ if (pin) {
7f207e10 19374+ if (lflag)
4a4d8108 19375+ au_fset_pin(pin->flags, DI_LOCKED);
7f207e10 19376+ else
4a4d8108 19377+ au_fclr_pin(pin->flags, DI_LOCKED);
1308ab2a 19378+ }
4a4d8108
AM
19379+}
19380+
7e9cd9fe 19381+#if 0 /* reserved */
4a4d8108
AM
19382+static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
19383+{
19384+ if (pin) {
19385+ dput(pin->parent);
19386+ pin->parent = dget(parent);
1facf9fc 19387+ }
4a4d8108 19388+}
7e9cd9fe 19389+#endif
1facf9fc 19390+
4a4d8108
AM
19391+/* ---------------------------------------------------------------------- */
19392+
027c5e7a 19393+struct au_branch;
4a4d8108
AM
19394+#ifdef CONFIG_AUFS_HNOTIFY
19395+struct au_hnotify_op {
19396+ void (*ctl)(struct au_hinode *hinode, int do_set);
027c5e7a 19397+ int (*alloc)(struct au_hinode *hinode);
7eafdf33
AM
19398+
19399+ /*
19400+ * if it returns true, the the caller should free hinode->hi_notify,
19401+ * otherwise ->free() frees it.
19402+ */
19403+ int (*free)(struct au_hinode *hinode,
19404+ struct au_hnotify *hn) __must_check;
4a4d8108
AM
19405+
19406+ void (*fin)(void);
19407+ int (*init)(void);
027c5e7a
AM
19408+
19409+ int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
19410+ void (*fin_br)(struct au_branch *br);
19411+ int (*init_br)(struct au_branch *br, int perm);
4a4d8108
AM
19412+};
19413+
19414+/* hnotify.c */
027c5e7a 19415+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
4a4d8108
AM
19416+void au_hn_free(struct au_hinode *hinode);
19417+void au_hn_ctl(struct au_hinode *hinode, int do_set);
19418+void au_hn_reset(struct inode *inode, unsigned int flags);
19419+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
19420+ struct qstr *h_child_qstr, struct inode *h_child_inode);
027c5e7a
AM
19421+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
19422+int au_hnotify_init_br(struct au_branch *br, int perm);
19423+void au_hnotify_fin_br(struct au_branch *br);
4a4d8108
AM
19424+int __init au_hnotify_init(void);
19425+void au_hnotify_fin(void);
19426+
7f207e10 19427+/* hfsnotify.c */
4a4d8108
AM
19428+extern const struct au_hnotify_op au_hnotify_op;
19429+
19430+static inline
19431+void au_hn_init(struct au_hinode *hinode)
19432+{
19433+ hinode->hi_notify = NULL;
1308ab2a 19434+}
19435+
53392da6
AM
19436+static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
19437+{
19438+ return hinode->hi_notify;
19439+}
19440+
4a4d8108 19441+#else
c1595e42
JR
19442+AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
19443+ struct au_hinode *hinode __maybe_unused,
19444+ struct inode *inode __maybe_unused)
19445+AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
4a4d8108
AM
19446+AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
19447+AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
19448+ int do_set __maybe_unused)
19449+AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
19450+ unsigned int flags __maybe_unused)
027c5e7a
AM
19451+AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
19452+ struct au_branch *br __maybe_unused,
19453+ int perm __maybe_unused)
19454+AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
19455+ int perm __maybe_unused)
19456+AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
4a4d8108
AM
19457+AuStubInt0(__init au_hnotify_init, void)
19458+AuStubVoid(au_hnotify_fin, void)
19459+AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
19460+#endif /* CONFIG_AUFS_HNOTIFY */
19461+
19462+static inline void au_hn_suspend(struct au_hinode *hdir)
19463+{
19464+ au_hn_ctl(hdir, /*do_set*/0);
1308ab2a 19465+}
19466+
4a4d8108 19467+static inline void au_hn_resume(struct au_hinode *hdir)
1308ab2a 19468+{
4a4d8108
AM
19469+ au_hn_ctl(hdir, /*do_set*/1);
19470+}
1308ab2a 19471+
5afbbe0d 19472+static inline void au_hn_inode_lock(struct au_hinode *hdir)
4a4d8108 19473+{
febd17d6 19474+ inode_lock(hdir->hi_inode);
4a4d8108
AM
19475+ au_hn_suspend(hdir);
19476+}
dece6358 19477+
5afbbe0d 19478+static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
4a4d8108
AM
19479+ unsigned int sc __maybe_unused)
19480+{
febd17d6 19481+ inode_lock_nested(hdir->hi_inode, sc);
4a4d8108 19482+ au_hn_suspend(hdir);
1facf9fc 19483+}
1facf9fc 19484+
8b6a4947
AM
19485+#if 0 /* unused */
19486+#include "vfsub.h"
3c1bdaff
AM
19487+static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
19488+ unsigned int sc)
19489+{
be118d29 19490+ inode_lock_shared_nested(hdir->hi_inode, sc);
3c1bdaff
AM
19491+ au_hn_suspend(hdir);
19492+}
8b6a4947 19493+#endif
3c1bdaff 19494+
5afbbe0d 19495+static inline void au_hn_inode_unlock(struct au_hinode *hdir)
4a4d8108
AM
19496+{
19497+ au_hn_resume(hdir);
febd17d6 19498+ inode_unlock(hdir->hi_inode);
4a4d8108
AM
19499+}
19500+
19501+#endif /* __KERNEL__ */
19502+#endif /* __AUFS_INODE_H__ */
7f207e10
AM
19503diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
19504--- /usr/share/empty/fs/aufs/ioctl.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 19505+++ linux/fs/aufs/ioctl.c 2019-03-05 12:13:00.142557771 +0100
062440b3 19506@@ -0,0 +1,220 @@
cd7a4cd9 19507+// SPDX-License-Identifier: GPL-2.0
4a4d8108 19508+/*
ba1aed25 19509+ * Copyright (C) 2005-2019 Junjiro R. Okajima
4a4d8108
AM
19510+ *
19511+ * This program, aufs is free software; you can redistribute it and/or modify
19512+ * it under the terms of the GNU General Public License as published by
19513+ * the Free Software Foundation; either version 2 of the License, or
19514+ * (at your option) any later version.
19515+ *
19516+ * This program is distributed in the hope that it will be useful,
19517+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19518+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19519+ * GNU General Public License for more details.
19520+ *
19521+ * You should have received a copy of the GNU General Public License
523b37e3 19522+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
19523+ */
19524+
19525+/*
19526+ * ioctl
19527+ * plink-management and readdir in userspace.
19528+ * assist the pathconf(3) wrapper library.
c2b27bf2 19529+ * move-down
076b876e 19530+ * File-based Hierarchical Storage Management.
4a4d8108
AM
19531+ */
19532+
c2b27bf2
AM
19533+#include <linux/compat.h>
19534+#include <linux/file.h>
4a4d8108
AM
19535+#include "aufs.h"
19536+
1e00d052 19537+static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
4a4d8108
AM
19538+{
19539+ int err, fd;
5afbbe0d 19540+ aufs_bindex_t wbi, bindex, bbot;
4a4d8108
AM
19541+ struct file *h_file;
19542+ struct super_block *sb;
19543+ struct dentry *root;
1e00d052
AM
19544+ struct au_branch *br;
19545+ struct aufs_wbr_fd wbrfd = {
19546+ .oflags = au_dir_roflags,
19547+ .brid = -1
19548+ };
19549+ const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
19550+ | O_NOATIME | O_CLOEXEC;
4a4d8108 19551+
1e00d052
AM
19552+ AuDebugOn(wbrfd.oflags & ~valid);
19553+
19554+ if (arg) {
19555+ err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
19556+ if (unlikely(err)) {
19557+ err = -EFAULT;
19558+ goto out;
19559+ }
19560+
19561+ err = -EINVAL;
19562+ AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
19563+ wbrfd.oflags |= au_dir_roflags;
19564+ AuDbg("0%o\n", wbrfd.oflags);
19565+ if (unlikely(wbrfd.oflags & ~valid))
19566+ goto out;
19567+ }
19568+
2000de60 19569+ fd = get_unused_fd_flags(0);
1e00d052
AM
19570+ err = fd;
19571+ if (unlikely(fd < 0))
4a4d8108 19572+ goto out;
4a4d8108 19573+
1e00d052 19574+ h_file = ERR_PTR(-EINVAL);
4a4d8108 19575+ wbi = 0;
1e00d052 19576+ br = NULL;
4a4d8108
AM
19577+ sb = path->dentry->d_sb;
19578+ root = sb->s_root;
19579+ aufs_read_lock(root, AuLock_IR);
5afbbe0d 19580+ bbot = au_sbbot(sb);
1e00d052
AM
19581+ if (wbrfd.brid >= 0) {
19582+ wbi = au_br_index(sb, wbrfd.brid);
5afbbe0d 19583+ if (unlikely(wbi < 0 || wbi > bbot))
1e00d052
AM
19584+ goto out_unlock;
19585+ }
19586+
19587+ h_file = ERR_PTR(-ENOENT);
19588+ br = au_sbr(sb, wbi);
19589+ if (!au_br_writable(br->br_perm)) {
19590+ if (arg)
19591+ goto out_unlock;
19592+
19593+ bindex = wbi + 1;
19594+ wbi = -1;
5afbbe0d 19595+ for (; bindex <= bbot; bindex++) {
1e00d052
AM
19596+ br = au_sbr(sb, bindex);
19597+ if (au_br_writable(br->br_perm)) {
4a4d8108 19598+ wbi = bindex;
1e00d052 19599+ br = au_sbr(sb, wbi);
4a4d8108
AM
19600+ break;
19601+ }
19602+ }
4a4d8108
AM
19603+ }
19604+ AuDbg("wbi %d\n", wbi);
1e00d052 19605+ if (wbi >= 0)
392086de
AM
19606+ h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
19607+ /*force_wr*/0);
1e00d052
AM
19608+
19609+out_unlock:
4a4d8108
AM
19610+ aufs_read_unlock(root, AuLock_IR);
19611+ err = PTR_ERR(h_file);
19612+ if (IS_ERR(h_file))
19613+ goto out_fd;
19614+
acd2b654 19615+ au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
4a4d8108
AM
19616+ fd_install(fd, h_file);
19617+ err = fd;
19618+ goto out; /* success */
19619+
4f0767ce 19620+out_fd:
4a4d8108 19621+ put_unused_fd(fd);
4f0767ce 19622+out:
1e00d052 19623+ AuTraceErr(err);
4a4d8108
AM
19624+ return err;
19625+}
19626+
19627+/* ---------------------------------------------------------------------- */
19628+
19629+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
19630+{
19631+ long err;
c1595e42 19632+ struct dentry *dentry;
4a4d8108
AM
19633+
19634+ switch (cmd) {
4a4d8108
AM
19635+ case AUFS_CTL_RDU:
19636+ case AUFS_CTL_RDU_INO:
19637+ err = au_rdu_ioctl(file, cmd, arg);
19638+ break;
19639+
19640+ case AUFS_CTL_WBR_FD:
1e00d052 19641+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
19642+ break;
19643+
027c5e7a
AM
19644+ case AUFS_CTL_IBUSY:
19645+ err = au_ibusy_ioctl(file, arg);
19646+ break;
19647+
076b876e
AM
19648+ case AUFS_CTL_BRINFO:
19649+ err = au_brinfo_ioctl(file, arg);
19650+ break;
19651+
19652+ case AUFS_CTL_FHSM_FD:
2000de60 19653+ dentry = file->f_path.dentry;
c1595e42
JR
19654+ if (IS_ROOT(dentry))
19655+ err = au_fhsm_fd(dentry->d_sb, arg);
19656+ else
19657+ err = -ENOTTY;
076b876e
AM
19658+ break;
19659+
4a4d8108
AM
19660+ default:
19661+ /* do not call the lower */
19662+ AuDbg("0x%x\n", cmd);
19663+ err = -ENOTTY;
19664+ }
19665+
19666+ AuTraceErr(err);
19667+ return err;
19668+}
19669+
19670+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
19671+{
19672+ long err;
19673+
19674+ switch (cmd) {
c2b27bf2 19675+ case AUFS_CTL_MVDOWN:
2000de60 19676+ err = au_mvdown(file->f_path.dentry, (void __user *)arg);
c2b27bf2
AM
19677+ break;
19678+
4a4d8108 19679+ case AUFS_CTL_WBR_FD:
1e00d052 19680+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
19681+ break;
19682+
19683+ default:
19684+ /* do not call the lower */
19685+ AuDbg("0x%x\n", cmd);
19686+ err = -ENOTTY;
19687+ }
19688+
19689+ AuTraceErr(err);
19690+ return err;
19691+}
b752ccd1
AM
19692+
19693+#ifdef CONFIG_COMPAT
19694+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
19695+ unsigned long arg)
19696+{
19697+ long err;
19698+
19699+ switch (cmd) {
19700+ case AUFS_CTL_RDU:
19701+ case AUFS_CTL_RDU_INO:
19702+ err = au_rdu_compat_ioctl(file, cmd, arg);
19703+ break;
19704+
027c5e7a
AM
19705+ case AUFS_CTL_IBUSY:
19706+ err = au_ibusy_compat_ioctl(file, arg);
19707+ break;
19708+
076b876e
AM
19709+ case AUFS_CTL_BRINFO:
19710+ err = au_brinfo_compat_ioctl(file, arg);
19711+ break;
19712+
b752ccd1
AM
19713+ default:
19714+ err = aufs_ioctl_dir(file, cmd, arg);
19715+ }
19716+
19717+ AuTraceErr(err);
19718+ return err;
19719+}
19720+
b752ccd1
AM
19721+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
19722+ unsigned long arg)
19723+{
19724+ return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
19725+}
19726+#endif
7f207e10
AM
19727diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
19728--- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 19729+++ linux/fs/aufs/i_op_add.c 2019-03-05 12:13:00.139224339 +0100
acd2b654 19730@@ -0,0 +1,935 @@
cd7a4cd9 19731+// SPDX-License-Identifier: GPL-2.0
4a4d8108 19732+/*
ba1aed25 19733+ * Copyright (C) 2005-2019 Junjiro R. Okajima
4a4d8108
AM
19734+ *
19735+ * This program, aufs is free software; you can redistribute it and/or modify
19736+ * it under the terms of the GNU General Public License as published by
19737+ * the Free Software Foundation; either version 2 of the License, or
19738+ * (at your option) any later version.
19739+ *
19740+ * This program is distributed in the hope that it will be useful,
19741+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19742+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19743+ * GNU General Public License for more details.
19744+ *
19745+ * You should have received a copy of the GNU General Public License
523b37e3 19746+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
19747+ */
19748+
19749+/*
19750+ * inode operations (add entry)
19751+ */
19752+
19753+#include "aufs.h"
19754+
19755+/*
19756+ * final procedure of adding a new entry, except link(2).
19757+ * remove whiteout, instantiate, copyup the parent dir's times and size
19758+ * and update version.
19759+ * if it failed, re-create the removed whiteout.
19760+ */
19761+static int epilog(struct inode *dir, aufs_bindex_t bindex,
19762+ struct dentry *wh_dentry, struct dentry *dentry)
19763+{
19764+ int err, rerr;
19765+ aufs_bindex_t bwh;
19766+ struct path h_path;
076b876e 19767+ struct super_block *sb;
4a4d8108
AM
19768+ struct inode *inode, *h_dir;
19769+ struct dentry *wh;
19770+
19771+ bwh = -1;
076b876e 19772+ sb = dir->i_sb;
4a4d8108 19773+ if (wh_dentry) {
5527c038 19774+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
4a4d8108
AM
19775+ IMustLock(h_dir);
19776+ AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
19777+ bwh = au_dbwh(dentry);
19778+ h_path.dentry = wh_dentry;
076b876e 19779+ h_path.mnt = au_sbr_mnt(sb, bindex);
4a4d8108
AM
19780+ err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
19781+ dentry);
19782+ if (unlikely(err))
19783+ goto out;
19784+ }
19785+
19786+ inode = au_new_inode(dentry, /*must_new*/1);
19787+ if (!IS_ERR(inode)) {
19788+ d_instantiate(dentry, inode);
5527c038 19789+ dir = d_inode(dentry->d_parent); /* dir inode is locked */
4a4d8108 19790+ IMustLock(dir);
b912730e 19791+ au_dir_ts(dir, bindex);
be118d29 19792+ inode_inc_iversion(dir);
076b876e 19793+ au_fhsm_wrote(sb, bindex, /*force*/0);
4a4d8108
AM
19794+ return 0; /* success */
19795+ }
19796+
19797+ err = PTR_ERR(inode);
19798+ if (!wh_dentry)
19799+ goto out;
19800+
19801+ /* revert */
19802+ /* dir inode is locked */
19803+ wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
19804+ rerr = PTR_ERR(wh);
19805+ if (IS_ERR(wh)) {
523b37e3
AM
19806+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
19807+ dentry, err, rerr);
4a4d8108
AM
19808+ err = -EIO;
19809+ } else
19810+ dput(wh);
19811+
4f0767ce 19812+out:
4a4d8108
AM
19813+ return err;
19814+}
19815+
027c5e7a
AM
19816+static int au_d_may_add(struct dentry *dentry)
19817+{
19818+ int err;
19819+
19820+ err = 0;
19821+ if (unlikely(d_unhashed(dentry)))
19822+ err = -ENOENT;
5527c038 19823+ if (unlikely(d_really_is_positive(dentry)))
027c5e7a
AM
19824+ err = -EEXIST;
19825+ return err;
19826+}
19827+
4a4d8108
AM
19828+/*
19829+ * simple tests for the adding inode operations.
19830+ * following the checks in vfs, plus the parent-child relationship.
19831+ */
19832+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19833+ struct dentry *h_parent, int isdir)
19834+{
19835+ int err;
19836+ umode_t h_mode;
19837+ struct dentry *h_dentry;
19838+ struct inode *h_inode;
19839+
19840+ err = -ENAMETOOLONG;
19841+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19842+ goto out;
19843+
19844+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 19845+ if (d_really_is_negative(dentry)) {
4a4d8108 19846+ err = -EEXIST;
5527c038 19847+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
19848+ goto out;
19849+ } else {
19850+ /* rename(2) case */
19851+ err = -EIO;
5527c038
JR
19852+ if (unlikely(d_is_negative(h_dentry)))
19853+ goto out;
19854+ h_inode = d_inode(h_dentry);
19855+ if (unlikely(!h_inode->i_nlink))
4a4d8108
AM
19856+ goto out;
19857+
19858+ h_mode = h_inode->i_mode;
19859+ if (!isdir) {
19860+ err = -EISDIR;
19861+ if (unlikely(S_ISDIR(h_mode)))
19862+ goto out;
19863+ } else if (unlikely(!S_ISDIR(h_mode))) {
19864+ err = -ENOTDIR;
19865+ goto out;
19866+ }
19867+ }
19868+
19869+ err = 0;
19870+ /* expected parent dir is locked */
19871+ if (unlikely(h_parent != h_dentry->d_parent))
19872+ err = -EIO;
19873+
4f0767ce 19874+out:
4a4d8108
AM
19875+ AuTraceErr(err);
19876+ return err;
19877+}
19878+
19879+/*
19880+ * initial procedure of adding a new entry.
19881+ * prepare writable branch and the parent dir, lock it,
19882+ * and lookup whiteout for the new entry.
19883+ */
19884+static struct dentry*
19885+lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
19886+ struct dentry *src_dentry, struct au_pin *pin,
19887+ struct au_wr_dir_args *wr_dir_args)
19888+{
19889+ struct dentry *wh_dentry, *h_parent;
19890+ struct super_block *sb;
19891+ struct au_branch *br;
19892+ int err;
19893+ unsigned int udba;
19894+ aufs_bindex_t bcpup;
19895+
523b37e3 19896+ AuDbg("%pd\n", dentry);
4a4d8108
AM
19897+
19898+ err = au_wr_dir(dentry, src_dentry, wr_dir_args);
19899+ bcpup = err;
19900+ wh_dentry = ERR_PTR(err);
19901+ if (unlikely(err < 0))
19902+ goto out;
19903+
19904+ sb = dentry->d_sb;
19905+ udba = au_opt_udba(sb);
19906+ err = au_pin(pin, dentry, bcpup, udba,
19907+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19908+ wh_dentry = ERR_PTR(err);
19909+ if (unlikely(err))
19910+ goto out;
19911+
19912+ h_parent = au_pinned_h_parent(pin);
19913+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 19914+ && au_dbtop(dentry) == bcpup)
4a4d8108
AM
19915+ err = au_may_add(dentry, bcpup, h_parent,
19916+ au_ftest_wrdir(wr_dir_args->flags, ISDIR));
19917+ else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19918+ err = -ENAMETOOLONG;
19919+ wh_dentry = ERR_PTR(err);
19920+ if (unlikely(err))
19921+ goto out_unpin;
19922+
19923+ br = au_sbr(sb, bcpup);
19924+ if (dt) {
19925+ struct path tmp = {
19926+ .dentry = h_parent,
86dc4139 19927+ .mnt = au_br_mnt(br)
4a4d8108
AM
19928+ };
19929+ au_dtime_store(dt, au_pinned_parent(pin), &tmp);
19930+ }
19931+
19932+ wh_dentry = NULL;
19933+ if (bcpup != au_dbwh(dentry))
19934+ goto out; /* success */
19935+
2000de60
JR
19936+ /*
19937+ * ENAMETOOLONG here means that if we allowed create such name, then it
19938+ * would not be able to removed in the future. So we don't allow such
19939+ * name here and we don't handle ENAMETOOLONG differently here.
19940+ */
4a4d8108
AM
19941+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
19942+
4f0767ce 19943+out_unpin:
4a4d8108
AM
19944+ if (IS_ERR(wh_dentry))
19945+ au_unpin(pin);
4f0767ce 19946+out:
4a4d8108
AM
19947+ return wh_dentry;
19948+}
19949+
19950+/* ---------------------------------------------------------------------- */
19951+
19952+enum { Mknod, Symlink, Creat };
19953+struct simple_arg {
19954+ int type;
19955+ union {
19956+ struct {
b912730e
AM
19957+ umode_t mode;
19958+ bool want_excl;
19959+ bool try_aopen;
19960+ struct vfsub_aopen_args *aopen;
4a4d8108
AM
19961+ } c;
19962+ struct {
19963+ const char *symname;
19964+ } s;
19965+ struct {
7eafdf33 19966+ umode_t mode;
4a4d8108
AM
19967+ dev_t dev;
19968+ } m;
19969+ } u;
19970+};
19971+
19972+static int add_simple(struct inode *dir, struct dentry *dentry,
19973+ struct simple_arg *arg)
19974+{
076b876e 19975+ int err, rerr;
5afbbe0d 19976+ aufs_bindex_t btop;
4a4d8108 19977+ unsigned char created;
b912730e
AM
19978+ const unsigned char try_aopen
19979+ = (arg->type == Creat && arg->u.c.try_aopen);
acd2b654 19980+ struct vfsub_aopen_args *aopen = arg->u.c.aopen;
4a4d8108
AM
19981+ struct dentry *wh_dentry, *parent;
19982+ struct inode *h_dir;
b912730e
AM
19983+ struct super_block *sb;
19984+ struct au_branch *br;
acd2b654 19985+ /* to reduce stack size */
c2b27bf2
AM
19986+ struct {
19987+ struct au_dtime dt;
19988+ struct au_pin pin;
19989+ struct path h_path;
19990+ struct au_wr_dir_args wr_dir_args;
19991+ } *a;
4a4d8108 19992+
523b37e3 19993+ AuDbg("%pd\n", dentry);
4a4d8108
AM
19994+ IMustLock(dir);
19995+
c2b27bf2
AM
19996+ err = -ENOMEM;
19997+ a = kmalloc(sizeof(*a), GFP_NOFS);
19998+ if (unlikely(!a))
19999+ goto out;
20000+ a->wr_dir_args.force_btgt = -1;
20001+ a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
20002+
4a4d8108 20003+ parent = dentry->d_parent; /* dir inode is locked */
b912730e
AM
20004+ if (!try_aopen) {
20005+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20006+ if (unlikely(err))
20007+ goto out_free;
20008+ }
027c5e7a
AM
20009+ err = au_d_may_add(dentry);
20010+ if (unlikely(err))
20011+ goto out_unlock;
b912730e
AM
20012+ if (!try_aopen)
20013+ di_write_lock_parent(parent);
c2b27bf2
AM
20014+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20015+ &a->pin, &a->wr_dir_args);
4a4d8108
AM
20016+ err = PTR_ERR(wh_dentry);
20017+ if (IS_ERR(wh_dentry))
027c5e7a 20018+ goto out_parent;
4a4d8108 20019+
5afbbe0d 20020+ btop = au_dbtop(dentry);
b912730e 20021+ sb = dentry->d_sb;
5afbbe0d
AM
20022+ br = au_sbr(sb, btop);
20023+ a->h_path.dentry = au_h_dptr(dentry, btop);
b912730e 20024+ a->h_path.mnt = au_br_mnt(br);
c2b27bf2 20025+ h_dir = au_pinned_h_dir(&a->pin);
4a4d8108
AM
20026+ switch (arg->type) {
20027+ case Creat:
acd2b654 20028+ if (!try_aopen || !h_dir->i_op->atomic_open) {
b912730e
AM
20029+ err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
20030+ arg->u.c.want_excl);
acd2b654
AM
20031+ created = !err;
20032+ if (!err && try_aopen)
20033+ aopen->file->f_mode |= FMODE_CREATED;
20034+ } else {
20035+ aopen->br = br;
20036+ err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
20037+ AuDbg("err %d\n", err);
20038+ AuDbgFile(aopen->file);
20039+ created = err >= 0
20040+ && !!(aopen->file->f_mode & FMODE_CREATED);
20041+ }
4a4d8108
AM
20042+ break;
20043+ case Symlink:
c2b27bf2 20044+ err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
acd2b654 20045+ created = !err;
4a4d8108
AM
20046+ break;
20047+ case Mknod:
c2b27bf2
AM
20048+ err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
20049+ arg->u.m.dev);
acd2b654 20050+ created = !err;
4a4d8108
AM
20051+ break;
20052+ default:
20053+ BUG();
20054+ }
acd2b654
AM
20055+ if (unlikely(err < 0))
20056+ goto out_unpin;
20057+
20058+ err = epilog(dir, btop, wh_dentry, dentry);
4a4d8108 20059+ if (!err)
acd2b654 20060+ goto out_unpin; /* success */
4a4d8108
AM
20061+
20062+ /* revert */
acd2b654 20063+ if (created /* && d_is_positive(a->h_path.dentry) */) {
523b37e3
AM
20064+ /* no delegation since it is just created */
20065+ rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
20066+ /*force*/0);
4a4d8108 20067+ if (rerr) {
523b37e3
AM
20068+ AuIOErr("%pd revert failure(%d, %d)\n",
20069+ dentry, err, rerr);
4a4d8108
AM
20070+ err = -EIO;
20071+ }
c2b27bf2 20072+ au_dtime_revert(&a->dt);
4a4d8108 20073+ }
acd2b654
AM
20074+ if (try_aopen && h_dir->i_op->atomic_open
20075+ && (aopen->file->f_mode & FMODE_OPENED))
20076+ /* aopen->file is still opened */
20077+ au_lcnt_dec(&aopen->br->br_nfiles);
4a4d8108 20078+
acd2b654 20079+out_unpin:
c2b27bf2 20080+ au_unpin(&a->pin);
4a4d8108 20081+ dput(wh_dentry);
027c5e7a 20082+out_parent:
b912730e
AM
20083+ if (!try_aopen)
20084+ di_write_unlock(parent);
027c5e7a 20085+out_unlock:
4a4d8108 20086+ if (unlikely(err)) {
5afbbe0d 20087+ au_update_dbtop(dentry);
4a4d8108
AM
20088+ d_drop(dentry);
20089+ }
b912730e
AM
20090+ if (!try_aopen)
20091+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 20092+out_free:
9f237c51 20093+ au_kfree_rcu(a);
027c5e7a 20094+out:
4a4d8108
AM
20095+ return err;
20096+}
20097+
7eafdf33
AM
20098+int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
20099+ dev_t dev)
4a4d8108
AM
20100+{
20101+ struct simple_arg arg = {
20102+ .type = Mknod,
20103+ .u.m = {
20104+ .mode = mode,
20105+ .dev = dev
20106+ }
20107+ };
20108+ return add_simple(dir, dentry, &arg);
20109+}
20110+
20111+int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
20112+{
20113+ struct simple_arg arg = {
20114+ .type = Symlink,
20115+ .u.s.symname = symname
20116+ };
20117+ return add_simple(dir, dentry, &arg);
20118+}
20119+
7eafdf33 20120+int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
b4510431 20121+ bool want_excl)
4a4d8108
AM
20122+{
20123+ struct simple_arg arg = {
20124+ .type = Creat,
20125+ .u.c = {
b4510431
AM
20126+ .mode = mode,
20127+ .want_excl = want_excl
4a4d8108
AM
20128+ }
20129+ };
20130+ return add_simple(dir, dentry, &arg);
20131+}
20132+
b912730e
AM
20133+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20134+ struct vfsub_aopen_args *aopen_args)
20135+{
20136+ struct simple_arg arg = {
20137+ .type = Creat,
20138+ .u.c = {
20139+ .mode = aopen_args->create_mode,
20140+ .want_excl = aopen_args->open_flag & O_EXCL,
20141+ .try_aopen = true,
20142+ .aopen = aopen_args
20143+ }
20144+ };
20145+ return add_simple(dir, dentry, &arg);
20146+}
20147+
38d290e6
JR
20148+int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
20149+{
20150+ int err;
20151+ aufs_bindex_t bindex;
20152+ struct super_block *sb;
20153+ struct dentry *parent, *h_parent, *h_dentry;
20154+ struct inode *h_dir, *inode;
20155+ struct vfsmount *h_mnt;
20156+ struct au_wr_dir_args wr_dir_args = {
20157+ .force_btgt = -1,
20158+ .flags = AuWrDir_TMPFILE
20159+ };
20160+
20161+ /* copy-up may happen */
febd17d6 20162+ inode_lock(dir);
38d290e6
JR
20163+
20164+ sb = dir->i_sb;
20165+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20166+ if (unlikely(err))
20167+ goto out;
20168+
20169+ err = au_di_init(dentry);
20170+ if (unlikely(err))
20171+ goto out_si;
20172+
20173+ err = -EBUSY;
20174+ parent = d_find_any_alias(dir);
20175+ AuDebugOn(!parent);
20176+ di_write_lock_parent(parent);
5527c038 20177+ if (unlikely(d_inode(parent) != dir))
38d290e6
JR
20178+ goto out_parent;
20179+
20180+ err = au_digen_test(parent, au_sigen(sb));
20181+ if (unlikely(err))
20182+ goto out_parent;
20183+
5afbbe0d
AM
20184+ bindex = au_dbtop(parent);
20185+ au_set_dbtop(dentry, bindex);
20186+ au_set_dbbot(dentry, bindex);
38d290e6
JR
20187+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
20188+ bindex = err;
20189+ if (unlikely(err < 0))
20190+ goto out_parent;
20191+
20192+ err = -EOPNOTSUPP;
20193+ h_dir = au_h_iptr(dir, bindex);
20194+ if (unlikely(!h_dir->i_op->tmpfile))
20195+ goto out_parent;
20196+
20197+ h_mnt = au_sbr_mnt(sb, bindex);
20198+ err = vfsub_mnt_want_write(h_mnt);
20199+ if (unlikely(err))
20200+ goto out_parent;
20201+
20202+ h_parent = au_h_dptr(parent, bindex);
521ced18
JR
20203+ h_dentry = vfs_tmpfile(h_parent, mode, /*open_flag*/0);
20204+ if (IS_ERR(h_dentry)) {
20205+ err = PTR_ERR(h_dentry);
38d290e6 20206+ goto out_mnt;
521ced18 20207+ }
38d290e6 20208+
5afbbe0d
AM
20209+ au_set_dbtop(dentry, bindex);
20210+ au_set_dbbot(dentry, bindex);
38d290e6
JR
20211+ au_set_h_dptr(dentry, bindex, dget(h_dentry));
20212+ inode = au_new_inode(dentry, /*must_new*/1);
20213+ if (IS_ERR(inode)) {
20214+ err = PTR_ERR(inode);
20215+ au_set_h_dptr(dentry, bindex, NULL);
5afbbe0d
AM
20216+ au_set_dbtop(dentry, -1);
20217+ au_set_dbbot(dentry, -1);
38d290e6
JR
20218+ } else {
20219+ if (!inode->i_nlink)
20220+ set_nlink(inode, 1);
20221+ d_tmpfile(dentry, inode);
20222+ au_di(dentry)->di_tmpfile = 1;
20223+
20224+ /* update without i_mutex */
5afbbe0d 20225+ if (au_ibtop(dir) == au_dbtop(dentry))
38d290e6
JR
20226+ au_cpup_attr_timesizes(dir);
20227+ }
38d290e6 20228+ dput(h_dentry);
521ced18 20229+
38d290e6
JR
20230+out_mnt:
20231+ vfsub_mnt_drop_write(h_mnt);
20232+out_parent:
20233+ di_write_unlock(parent);
20234+ dput(parent);
20235+ di_write_unlock(dentry);
5afbbe0d 20236+ if (unlikely(err)) {
38d290e6
JR
20237+ au_di_fin(dentry);
20238+ dentry->d_fsdata = NULL;
20239+ }
20240+out_si:
20241+ si_read_unlock(sb);
20242+out:
febd17d6 20243+ inode_unlock(dir);
38d290e6
JR
20244+ return err;
20245+}
20246+
4a4d8108
AM
20247+/* ---------------------------------------------------------------------- */
20248+
20249+struct au_link_args {
20250+ aufs_bindex_t bdst, bsrc;
20251+ struct au_pin pin;
20252+ struct path h_path;
20253+ struct dentry *src_parent, *parent;
20254+};
20255+
20256+static int au_cpup_before_link(struct dentry *src_dentry,
20257+ struct au_link_args *a)
20258+{
20259+ int err;
20260+ struct dentry *h_src_dentry;
c2b27bf2
AM
20261+ struct au_cp_generic cpg = {
20262+ .dentry = src_dentry,
20263+ .bdst = a->bdst,
20264+ .bsrc = a->bsrc,
20265+ .len = -1,
20266+ .pin = &a->pin,
20267+ .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
20268+ };
4a4d8108
AM
20269+
20270+ di_read_lock_parent(a->src_parent, AuLock_IR);
20271+ err = au_test_and_cpup_dirs(src_dentry, a->bdst);
20272+ if (unlikely(err))
20273+ goto out;
20274+
20275+ h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
4a4d8108
AM
20276+ err = au_pin(&a->pin, src_dentry, a->bdst,
20277+ au_opt_udba(src_dentry->d_sb),
20278+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20279+ if (unlikely(err))
20280+ goto out;
367653fa 20281+
c2b27bf2 20282+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
20283+ au_unpin(&a->pin);
20284+
4f0767ce 20285+out:
4a4d8108
AM
20286+ di_read_unlock(a->src_parent, AuLock_IR);
20287+ return err;
20288+}
20289+
86dc4139
AM
20290+static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
20291+ struct au_link_args *a)
4a4d8108
AM
20292+{
20293+ int err;
20294+ unsigned char plink;
5afbbe0d 20295+ aufs_bindex_t bbot;
4a4d8108 20296+ struct dentry *h_src_dentry;
523b37e3 20297+ struct inode *h_inode, *inode, *delegated;
4a4d8108
AM
20298+ struct super_block *sb;
20299+ struct file *h_file;
20300+
20301+ plink = 0;
20302+ h_inode = NULL;
20303+ sb = src_dentry->d_sb;
5527c038 20304+ inode = d_inode(src_dentry);
5afbbe0d 20305+ if (au_ibtop(inode) <= a->bdst)
4a4d8108
AM
20306+ h_inode = au_h_iptr(inode, a->bdst);
20307+ if (!h_inode || !h_inode->i_nlink) {
20308+ /* copyup src_dentry as the name of dentry. */
5afbbe0d
AM
20309+ bbot = au_dbbot(dentry);
20310+ if (bbot < a->bsrc)
20311+ au_set_dbbot(dentry, a->bsrc);
86dc4139
AM
20312+ au_set_h_dptr(dentry, a->bsrc,
20313+ dget(au_h_dptr(src_dentry, a->bsrc)));
20314+ dget(a->h_path.dentry);
20315+ au_set_h_dptr(dentry, a->bdst, NULL);
c1595e42
JR
20316+ AuDbg("temporary d_inode...\n");
20317+ spin_lock(&dentry->d_lock);
5527c038 20318+ dentry->d_inode = d_inode(src_dentry); /* tmp */
c1595e42 20319+ spin_unlock(&dentry->d_lock);
392086de 20320+ h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
86dc4139 20321+ if (IS_ERR(h_file))
4a4d8108 20322+ err = PTR_ERR(h_file);
86dc4139 20323+ else {
c2b27bf2
AM
20324+ struct au_cp_generic cpg = {
20325+ .dentry = dentry,
20326+ .bdst = a->bdst,
20327+ .bsrc = -1,
20328+ .len = -1,
20329+ .pin = &a->pin,
20330+ .flags = AuCpup_KEEPLINO
20331+ };
20332+ err = au_sio_cpup_simple(&cpg);
86dc4139
AM
20333+ au_h_open_post(dentry, a->bsrc, h_file);
20334+ if (!err) {
20335+ dput(a->h_path.dentry);
20336+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20337+ } else
20338+ au_set_h_dptr(dentry, a->bdst,
20339+ a->h_path.dentry);
20340+ }
c1595e42 20341+ spin_lock(&dentry->d_lock);
86dc4139 20342+ dentry->d_inode = NULL; /* restore */
c1595e42
JR
20343+ spin_unlock(&dentry->d_lock);
20344+ AuDbg("temporary d_inode...done\n");
86dc4139 20345+ au_set_h_dptr(dentry, a->bsrc, NULL);
5afbbe0d 20346+ au_set_dbbot(dentry, bbot);
4a4d8108
AM
20347+ } else {
20348+ /* the inode of src_dentry already exists on a.bdst branch */
20349+ h_src_dentry = d_find_alias(h_inode);
20350+ if (!h_src_dentry && au_plink_test(inode)) {
20351+ plink = 1;
20352+ h_src_dentry = au_plink_lkup(inode, a->bdst);
20353+ err = PTR_ERR(h_src_dentry);
20354+ if (IS_ERR(h_src_dentry))
20355+ goto out;
20356+
5527c038 20357+ if (unlikely(d_is_negative(h_src_dentry))) {
4a4d8108
AM
20358+ dput(h_src_dentry);
20359+ h_src_dentry = NULL;
20360+ }
20361+
20362+ }
20363+ if (h_src_dentry) {
523b37e3 20364+ delegated = NULL;
4a4d8108 20365+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
20366+ &a->h_path, &delegated);
20367+ if (unlikely(err == -EWOULDBLOCK)) {
20368+ pr_warn("cannot retry for NFSv4 delegation"
20369+ " for an internal link\n");
20370+ iput(delegated);
20371+ }
4a4d8108
AM
20372+ dput(h_src_dentry);
20373+ } else {
20374+ AuIOErr("no dentry found for hi%lu on b%d\n",
20375+ h_inode->i_ino, a->bdst);
20376+ err = -EIO;
20377+ }
20378+ }
20379+
20380+ if (!err && !plink)
20381+ au_plink_append(inode, a->bdst, a->h_path.dentry);
20382+
20383+out:
2cbb1c4b 20384+ AuTraceErr(err);
4a4d8108
AM
20385+ return err;
20386+}
20387+
20388+int aufs_link(struct dentry *src_dentry, struct inode *dir,
20389+ struct dentry *dentry)
20390+{
20391+ int err, rerr;
20392+ struct au_dtime dt;
20393+ struct au_link_args *a;
20394+ struct dentry *wh_dentry, *h_src_dentry;
523b37e3 20395+ struct inode *inode, *delegated;
4a4d8108
AM
20396+ struct super_block *sb;
20397+ struct au_wr_dir_args wr_dir_args = {
20398+ /* .force_btgt = -1, */
20399+ .flags = AuWrDir_ADD_ENTRY
20400+ };
20401+
20402+ IMustLock(dir);
5527c038 20403+ inode = d_inode(src_dentry);
4a4d8108
AM
20404+ IMustLock(inode);
20405+
4a4d8108
AM
20406+ err = -ENOMEM;
20407+ a = kzalloc(sizeof(*a), GFP_NOFS);
20408+ if (unlikely(!a))
20409+ goto out;
20410+
20411+ a->parent = dentry->d_parent; /* dir inode is locked */
027c5e7a
AM
20412+ err = aufs_read_and_write_lock2(dentry, src_dentry,
20413+ AuLock_NOPLM | AuLock_GEN);
e49829fe
JR
20414+ if (unlikely(err))
20415+ goto out_kfree;
38d290e6 20416+ err = au_d_linkable(src_dentry);
027c5e7a
AM
20417+ if (unlikely(err))
20418+ goto out_unlock;
20419+ err = au_d_may_add(dentry);
20420+ if (unlikely(err))
20421+ goto out_unlock;
e49829fe 20422+
4a4d8108 20423+ a->src_parent = dget_parent(src_dentry);
5afbbe0d 20424+ wr_dir_args.force_btgt = au_ibtop(inode);
4a4d8108
AM
20425+
20426+ di_write_lock_parent(a->parent);
20427+ wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
20428+ wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
20429+ &wr_dir_args);
20430+ err = PTR_ERR(wh_dentry);
20431+ if (IS_ERR(wh_dentry))
027c5e7a 20432+ goto out_parent;
4a4d8108
AM
20433+
20434+ err = 0;
20435+ sb = dentry->d_sb;
5afbbe0d 20436+ a->bdst = au_dbtop(dentry);
4a4d8108
AM
20437+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20438+ a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
5afbbe0d 20439+ a->bsrc = au_ibtop(inode);
2cbb1c4b 20440+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
38d290e6
JR
20441+ if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
20442+ h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
2cbb1c4b 20443+ if (!h_src_dentry) {
5afbbe0d 20444+ a->bsrc = au_dbtop(src_dentry);
2cbb1c4b
JR
20445+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20446+ AuDebugOn(!h_src_dentry);
38d290e6
JR
20447+ } else if (IS_ERR(h_src_dentry)) {
20448+ err = PTR_ERR(h_src_dentry);
2cbb1c4b 20449+ goto out_parent;
38d290e6 20450+ }
2cbb1c4b 20451+
f2c43d5f
AM
20452+ /*
20453+ * aufs doesn't touch the credential so
acd2b654 20454+ * security_dentry_create_files_as() is unnecessary.
f2c43d5f 20455+ */
4a4d8108
AM
20456+ if (au_opt_test(au_mntflags(sb), PLINK)) {
20457+ if (a->bdst < a->bsrc
20458+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
86dc4139 20459+ err = au_cpup_or_link(src_dentry, dentry, a);
523b37e3
AM
20460+ else {
20461+ delegated = NULL;
4a4d8108 20462+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
20463+ &a->h_path, &delegated);
20464+ if (unlikely(err == -EWOULDBLOCK)) {
20465+ pr_warn("cannot retry for NFSv4 delegation"
20466+ " for an internal link\n");
20467+ iput(delegated);
20468+ }
20469+ }
2cbb1c4b 20470+ dput(h_src_dentry);
4a4d8108
AM
20471+ } else {
20472+ /*
20473+ * copyup src_dentry to the branch we process,
20474+ * and then link(2) to it.
20475+ */
2cbb1c4b 20476+ dput(h_src_dentry);
4a4d8108
AM
20477+ if (a->bdst < a->bsrc
20478+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
20479+ au_unpin(&a->pin);
20480+ di_write_unlock(a->parent);
20481+ err = au_cpup_before_link(src_dentry, a);
20482+ di_write_lock_parent(a->parent);
20483+ if (!err)
20484+ err = au_pin(&a->pin, dentry, a->bdst,
20485+ au_opt_udba(sb),
20486+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20487+ if (unlikely(err))
20488+ goto out_wh;
20489+ }
20490+ if (!err) {
20491+ h_src_dentry = au_h_dptr(src_dentry, a->bdst);
20492+ err = -ENOENT;
5527c038 20493+ if (h_src_dentry && d_is_positive(h_src_dentry)) {
523b37e3 20494+ delegated = NULL;
4a4d8108
AM
20495+ err = vfsub_link(h_src_dentry,
20496+ au_pinned_h_dir(&a->pin),
523b37e3
AM
20497+ &a->h_path, &delegated);
20498+ if (unlikely(err == -EWOULDBLOCK)) {
20499+ pr_warn("cannot retry"
20500+ " for NFSv4 delegation"
20501+ " for an internal link\n");
20502+ iput(delegated);
20503+ }
20504+ }
4a4d8108
AM
20505+ }
20506+ }
20507+ if (unlikely(err))
20508+ goto out_unpin;
20509+
20510+ if (wh_dentry) {
20511+ a->h_path.dentry = wh_dentry;
20512+ err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
20513+ dentry);
20514+ if (unlikely(err))
20515+ goto out_revert;
20516+ }
20517+
b912730e 20518+ au_dir_ts(dir, a->bdst);
be118d29 20519+ inode_inc_iversion(dir);
4a4d8108
AM
20520+ inc_nlink(inode);
20521+ inode->i_ctime = dir->i_ctime;
027c5e7a
AM
20522+ d_instantiate(dentry, au_igrab(inode));
20523+ if (d_unhashed(a->h_path.dentry))
4a4d8108
AM
20524+ /* some filesystem calls d_drop() */
20525+ d_drop(dentry);
076b876e
AM
20526+ /* some filesystems consume an inode even hardlink */
20527+ au_fhsm_wrote(sb, a->bdst, /*force*/0);
4a4d8108
AM
20528+ goto out_unpin; /* success */
20529+
4f0767ce 20530+out_revert:
523b37e3
AM
20531+ /* no delegation since it is just created */
20532+ rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
20533+ /*delegated*/NULL, /*force*/0);
027c5e7a 20534+ if (unlikely(rerr)) {
523b37e3 20535+ AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
027c5e7a
AM
20536+ err = -EIO;
20537+ }
4a4d8108 20538+ au_dtime_revert(&dt);
4f0767ce 20539+out_unpin:
4a4d8108 20540+ au_unpin(&a->pin);
4f0767ce 20541+out_wh:
4a4d8108 20542+ dput(wh_dentry);
027c5e7a
AM
20543+out_parent:
20544+ di_write_unlock(a->parent);
20545+ dput(a->src_parent);
4f0767ce 20546+out_unlock:
4a4d8108 20547+ if (unlikely(err)) {
5afbbe0d 20548+ au_update_dbtop(dentry);
4a4d8108
AM
20549+ d_drop(dentry);
20550+ }
4a4d8108 20551+ aufs_read_and_write_unlock2(dentry, src_dentry);
e49829fe 20552+out_kfree:
9f237c51 20553+ au_kfree_rcu(a);
4f0767ce 20554+out:
86dc4139 20555+ AuTraceErr(err);
4a4d8108
AM
20556+ return err;
20557+}
20558+
7eafdf33 20559+int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
4a4d8108
AM
20560+{
20561+ int err, rerr;
20562+ aufs_bindex_t bindex;
20563+ unsigned char diropq;
20564+ struct path h_path;
20565+ struct dentry *wh_dentry, *parent, *opq_dentry;
febd17d6 20566+ struct inode *h_inode;
4a4d8108
AM
20567+ struct super_block *sb;
20568+ struct {
20569+ struct au_pin pin;
20570+ struct au_dtime dt;
20571+ } *a; /* reduce the stack usage */
20572+ struct au_wr_dir_args wr_dir_args = {
20573+ .force_btgt = -1,
20574+ .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
20575+ };
20576+
20577+ IMustLock(dir);
20578+
20579+ err = -ENOMEM;
20580+ a = kmalloc(sizeof(*a), GFP_NOFS);
20581+ if (unlikely(!a))
20582+ goto out;
20583+
027c5e7a
AM
20584+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20585+ if (unlikely(err))
20586+ goto out_free;
20587+ err = au_d_may_add(dentry);
20588+ if (unlikely(err))
20589+ goto out_unlock;
20590+
4a4d8108
AM
20591+ parent = dentry->d_parent; /* dir inode is locked */
20592+ di_write_lock_parent(parent);
20593+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20594+ &a->pin, &wr_dir_args);
20595+ err = PTR_ERR(wh_dentry);
20596+ if (IS_ERR(wh_dentry))
027c5e7a 20597+ goto out_parent;
4a4d8108
AM
20598+
20599+ sb = dentry->d_sb;
5afbbe0d 20600+ bindex = au_dbtop(dentry);
4a4d8108
AM
20601+ h_path.dentry = au_h_dptr(dentry, bindex);
20602+ h_path.mnt = au_sbr_mnt(sb, bindex);
20603+ err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
20604+ if (unlikely(err))
027c5e7a 20605+ goto out_unpin;
4a4d8108
AM
20606+
20607+ /* make the dir opaque */
20608+ diropq = 0;
febd17d6 20609+ h_inode = d_inode(h_path.dentry);
4a4d8108
AM
20610+ if (wh_dentry
20611+ || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
febd17d6 20612+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 20613+ opq_dentry = au_diropq_create(dentry, bindex);
febd17d6 20614+ inode_unlock(h_inode);
4a4d8108
AM
20615+ err = PTR_ERR(opq_dentry);
20616+ if (IS_ERR(opq_dentry))
20617+ goto out_dir;
20618+ dput(opq_dentry);
20619+ diropq = 1;
20620+ }
20621+
20622+ err = epilog(dir, bindex, wh_dentry, dentry);
20623+ if (!err) {
20624+ inc_nlink(dir);
027c5e7a 20625+ goto out_unpin; /* success */
4a4d8108
AM
20626+ }
20627+
20628+ /* revert */
20629+ if (diropq) {
20630+ AuLabel(revert opq);
febd17d6 20631+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 20632+ rerr = au_diropq_remove(dentry, bindex);
febd17d6 20633+ inode_unlock(h_inode);
4a4d8108 20634+ if (rerr) {
523b37e3
AM
20635+ AuIOErr("%pd reverting diropq failed(%d, %d)\n",
20636+ dentry, err, rerr);
4a4d8108
AM
20637+ err = -EIO;
20638+ }
20639+ }
20640+
4f0767ce 20641+out_dir:
4a4d8108
AM
20642+ AuLabel(revert dir);
20643+ rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
20644+ if (rerr) {
523b37e3
AM
20645+ AuIOErr("%pd reverting dir failed(%d, %d)\n",
20646+ dentry, err, rerr);
4a4d8108
AM
20647+ err = -EIO;
20648+ }
4a4d8108 20649+ au_dtime_revert(&a->dt);
027c5e7a 20650+out_unpin:
4a4d8108
AM
20651+ au_unpin(&a->pin);
20652+ dput(wh_dentry);
027c5e7a
AM
20653+out_parent:
20654+ di_write_unlock(parent);
20655+out_unlock:
4a4d8108 20656+ if (unlikely(err)) {
5afbbe0d 20657+ au_update_dbtop(dentry);
4a4d8108
AM
20658+ d_drop(dentry);
20659+ }
4a4d8108 20660+ aufs_read_unlock(dentry, AuLock_DW);
027c5e7a 20661+out_free:
9f237c51 20662+ au_kfree_rcu(a);
4f0767ce 20663+out:
4a4d8108
AM
20664+ return err;
20665+}
7f207e10
AM
20666diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
20667--- /usr/share/empty/fs/aufs/i_op.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 20668+++ linux/fs/aufs/i_op.c 2019-03-05 12:13:00.139224339 +0100
acd2b654 20669@@ -0,0 +1,1506 @@
cd7a4cd9 20670+// SPDX-License-Identifier: GPL-2.0
4a4d8108 20671+/*
ba1aed25 20672+ * Copyright (C) 2005-2019 Junjiro R. Okajima
4a4d8108
AM
20673+ *
20674+ * This program, aufs is free software; you can redistribute it and/or modify
20675+ * it under the terms of the GNU General Public License as published by
20676+ * the Free Software Foundation; either version 2 of the License, or
20677+ * (at your option) any later version.
20678+ *
20679+ * This program is distributed in the hope that it will be useful,
20680+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20681+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20682+ * GNU General Public License for more details.
20683+ *
20684+ * You should have received a copy of the GNU General Public License
523b37e3 20685+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 20686+ */
1facf9fc 20687+
1308ab2a 20688+/*
4a4d8108 20689+ * inode operations (except add/del/rename)
1308ab2a 20690+ */
4a4d8108
AM
20691+
20692+#include <linux/device_cgroup.h>
20693+#include <linux/fs_stack.h>
4a4d8108
AM
20694+#include <linux/namei.h>
20695+#include <linux/security.h>
4a4d8108
AM
20696+#include "aufs.h"
20697+
1e00d052 20698+static int h_permission(struct inode *h_inode, int mask,
79b8bda9 20699+ struct path *h_path, int brperm)
1facf9fc 20700+{
1308ab2a 20701+ int err;
4a4d8108 20702+ const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
1facf9fc 20703+
e2f27e51
AM
20704+ err = -EPERM;
20705+ if (write_mask && IS_IMMUTABLE(h_inode))
20706+ goto out;
20707+
4a4d8108 20708+ err = -EACCES;
e2f27e51
AM
20709+ if (((mask & MAY_EXEC)
20710+ && S_ISREG(h_inode->i_mode)
20711+ && (path_noexec(h_path)
cd7a4cd9 20712+ || !(h_inode->i_mode & 0111))))
4a4d8108
AM
20713+ goto out;
20714+
20715+ /*
20716+ * - skip the lower fs test in the case of write to ro branch.
20717+ * - nfs dir permission write check is optimized, but a policy for
20718+ * link/rename requires a real check.
2121bcd9 20719+ * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
b912730e 20720+ * in this case, generic_permission() returns -EOPNOTSUPP.
4a4d8108
AM
20721+ */
20722+ if ((write_mask && !au_br_writable(brperm))
20723+ || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
20724+ && write_mask && !(mask & MAY_READ))
20725+ || !h_inode->i_op->permission) {
20726+ /* AuLabel(generic_permission); */
062440b3 20727+ /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
1e00d052 20728+ err = generic_permission(h_inode, mask);
b912730e
AM
20729+ if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
20730+ err = h_inode->i_op->permission(h_inode, mask);
20731+ AuTraceErr(err);
1308ab2a 20732+ } else {
4a4d8108 20733+ /* AuLabel(h_inode->permission); */
1e00d052 20734+ err = h_inode->i_op->permission(h_inode, mask);
4a4d8108
AM
20735+ AuTraceErr(err);
20736+ }
1facf9fc 20737+
4a4d8108
AM
20738+ if (!err)
20739+ err = devcgroup_inode_permission(h_inode, mask);
7f207e10 20740+ if (!err)
4a4d8108 20741+ err = security_inode_permission(h_inode, mask);
4a4d8108
AM
20742+
20743+#if 0
20744+ if (!err) {
20745+ /* todo: do we need to call ima_path_check()? */
20746+ struct path h_path = {
20747+ .dentry =
20748+ .mnt = h_mnt
20749+ };
20750+ err = ima_path_check(&h_path,
20751+ mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
20752+ IMA_COUNT_LEAVE);
1308ab2a 20753+ }
4a4d8108 20754+#endif
dece6358 20755+
4f0767ce 20756+out:
1308ab2a 20757+ return err;
20758+}
dece6358 20759+
1e00d052 20760+static int aufs_permission(struct inode *inode, int mask)
1308ab2a 20761+{
20762+ int err;
5afbbe0d 20763+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
20764+ const unsigned char isdir = !!S_ISDIR(inode->i_mode),
20765+ write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20766+ struct inode *h_inode;
20767+ struct super_block *sb;
20768+ struct au_branch *br;
1facf9fc 20769+
027c5e7a 20770+ /* todo: support rcu-walk? */
1e00d052 20771+ if (mask & MAY_NOT_BLOCK)
027c5e7a
AM
20772+ return -ECHILD;
20773+
4a4d8108
AM
20774+ sb = inode->i_sb;
20775+ si_read_lock(sb, AuLock_FLUSH);
20776+ ii_read_lock_child(inode);
027c5e7a
AM
20777+#if 0
20778+ err = au_iigen_test(inode, au_sigen(sb));
20779+ if (unlikely(err))
20780+ goto out;
20781+#endif
dece6358 20782+
076b876e
AM
20783+ if (!isdir
20784+ || write_mask
20785+ || au_opt_test(au_mntflags(sb), DIRPERM1)) {
4a4d8108 20786+ err = au_busy_or_stale();
5afbbe0d 20787+ h_inode = au_h_iptr(inode, au_ibtop(inode));
4a4d8108
AM
20788+ if (unlikely(!h_inode
20789+ || (h_inode->i_mode & S_IFMT)
20790+ != (inode->i_mode & S_IFMT)))
20791+ goto out;
1facf9fc 20792+
4a4d8108 20793+ err = 0;
5afbbe0d 20794+ bindex = au_ibtop(inode);
4a4d8108 20795+ br = au_sbr(sb, bindex);
79b8bda9 20796+ err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
4a4d8108
AM
20797+ if (write_mask
20798+ && !err
20799+ && !special_file(h_inode->i_mode)) {
20800+ /* test whether the upper writable branch exists */
20801+ err = -EROFS;
20802+ for (; bindex >= 0; bindex--)
20803+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
20804+ err = 0;
20805+ break;
20806+ }
20807+ }
20808+ goto out;
20809+ }
dece6358 20810+
4a4d8108 20811+ /* non-write to dir */
1308ab2a 20812+ err = 0;
5afbbe0d
AM
20813+ bbot = au_ibbot(inode);
20814+ for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
20815+ h_inode = au_h_iptr(inode, bindex);
20816+ if (h_inode) {
20817+ err = au_busy_or_stale();
20818+ if (unlikely(!S_ISDIR(h_inode->i_mode)))
20819+ break;
20820+
20821+ br = au_sbr(sb, bindex);
79b8bda9 20822+ err = h_permission(h_inode, mask, &br->br_path,
4a4d8108
AM
20823+ br->br_perm);
20824+ }
20825+ }
1308ab2a 20826+
4f0767ce 20827+out:
4a4d8108
AM
20828+ ii_read_unlock(inode);
20829+ si_read_unlock(sb);
1308ab2a 20830+ return err;
20831+}
20832+
4a4d8108 20833+/* ---------------------------------------------------------------------- */
1facf9fc 20834+
4a4d8108 20835+static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
b4510431 20836+ unsigned int flags)
4a4d8108
AM
20837+{
20838+ struct dentry *ret, *parent;
b752ccd1 20839+ struct inode *inode;
4a4d8108 20840+ struct super_block *sb;
1716fcea 20841+ int err, npositive;
dece6358 20842+
4a4d8108 20843+ IMustLock(dir);
1308ab2a 20844+
537831f9
AM
20845+ /* todo: support rcu-walk? */
20846+ ret = ERR_PTR(-ECHILD);
20847+ if (flags & LOOKUP_RCU)
20848+ goto out;
20849+
20850+ ret = ERR_PTR(-ENAMETOOLONG);
20851+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20852+ goto out;
20853+
4a4d8108 20854+ sb = dir->i_sb;
7f207e10
AM
20855+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20856+ ret = ERR_PTR(err);
20857+ if (unlikely(err))
20858+ goto out;
20859+
4a4d8108
AM
20860+ err = au_di_init(dentry);
20861+ ret = ERR_PTR(err);
20862+ if (unlikely(err))
7f207e10 20863+ goto out_si;
1308ab2a 20864+
9dbd164d 20865+ inode = NULL;
027c5e7a 20866+ npositive = 0; /* suppress a warning */
4a4d8108
AM
20867+ parent = dentry->d_parent; /* dir inode is locked */
20868+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
20869+ err = au_alive_dir(parent);
20870+ if (!err)
20871+ err = au_digen_test(parent, au_sigen(sb));
20872+ if (!err) {
5afbbe0d
AM
20873+ /* regardless LOOKUP_CREATE, always ALLOW_NEG */
20874+ npositive = au_lkup_dentry(dentry, au_dbtop(parent),
20875+ AuLkup_ALLOW_NEG);
027c5e7a
AM
20876+ err = npositive;
20877+ }
4a4d8108 20878+ di_read_unlock(parent, AuLock_IR);
4a4d8108
AM
20879+ ret = ERR_PTR(err);
20880+ if (unlikely(err < 0))
20881+ goto out_unlock;
1308ab2a 20882+
4a4d8108 20883+ if (npositive) {
b752ccd1 20884+ inode = au_new_inode(dentry, /*must_new*/0);
c1595e42
JR
20885+ if (IS_ERR(inode)) {
20886+ ret = (void *)inode;
20887+ inode = NULL;
20888+ goto out_unlock;
20889+ }
9dbd164d 20890+ }
4a4d8108 20891+
c1595e42
JR
20892+ if (inode)
20893+ atomic_inc(&inode->i_count);
4a4d8108 20894+ ret = d_splice_alias(inode, dentry);
537831f9
AM
20895+#if 0
20896+ if (unlikely(d_need_lookup(dentry))) {
20897+ spin_lock(&dentry->d_lock);
20898+ dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
20899+ spin_unlock(&dentry->d_lock);
20900+ } else
20901+#endif
c1595e42 20902+ if (inode) {
2000de60 20903+ if (!IS_ERR(ret)) {
c1595e42 20904+ iput(inode);
2000de60
JR
20905+ if (ret && ret != dentry)
20906+ ii_write_unlock(inode);
20907+ } else {
c1595e42
JR
20908+ ii_write_unlock(inode);
20909+ iput(inode);
20910+ inode = NULL;
20911+ }
7f207e10 20912+ }
1facf9fc 20913+
4f0767ce 20914+out_unlock:
4a4d8108 20915+ di_write_unlock(dentry);
7f207e10 20916+out_si:
4a4d8108 20917+ si_read_unlock(sb);
7f207e10 20918+out:
4a4d8108
AM
20919+ return ret;
20920+}
1facf9fc 20921+
4a4d8108 20922+/* ---------------------------------------------------------------------- */
1facf9fc 20923+
acd2b654
AM
20924+/*
20925+ * very dirty and complicated aufs ->atomic_open().
20926+ * aufs_atomic_open()
20927+ * + au_aopen_or_create()
20928+ * + add_simple()
20929+ * + vfsub_atomic_open()
20930+ * + branch fs ->atomic_open()
20931+ * may call the actual 'open' for h_file
20932+ * + inc br_nfiles only if opened
20933+ * + au_aopen_no_open() or au_aopen_do_open()
20934+ *
20935+ * au_aopen_do_open()
20936+ * + finish_open()
20937+ * + au_do_aopen()
20938+ * + au_do_open() the body of all 'open'
20939+ * + au_do_open_nondir()
20940+ * set the passed h_file
20941+ *
20942+ * au_aopen_no_open()
20943+ * + finish_no_open()
20944+ */
20945+
b912730e 20946+struct aopen_node {
8b6a4947 20947+ struct hlist_bl_node hblist;
b912730e
AM
20948+ struct file *file, *h_file;
20949+};
20950+
20951+static int au_do_aopen(struct inode *inode, struct file *file)
20952+{
8b6a4947
AM
20953+ struct hlist_bl_head *aopen;
20954+ struct hlist_bl_node *pos;
b912730e
AM
20955+ struct aopen_node *node;
20956+ struct au_do_open_args args = {
8b6a4947
AM
20957+ .aopen = 1,
20958+ .open = au_do_open_nondir
b912730e
AM
20959+ };
20960+
20961+ aopen = &au_sbi(inode->i_sb)->si_aopen;
8b6a4947
AM
20962+ hlist_bl_lock(aopen);
20963+ hlist_bl_for_each_entry(node, pos, aopen, hblist)
b912730e
AM
20964+ if (node->file == file) {
20965+ args.h_file = node->h_file;
20966+ break;
20967+ }
8b6a4947 20968+ hlist_bl_unlock(aopen);
b912730e
AM
20969+ /* AuDebugOn(!args.h_file); */
20970+
20971+ return au_do_open(file, &args);
20972+}
20973+
acd2b654
AM
20974+static int au_aopen_do_open(struct file *file, struct dentry *dentry,
20975+ struct aopen_node *aopen_node)
20976+{
20977+ int err;
20978+ struct hlist_bl_head *aopen;
20979+
20980+ AuLabel(here);
20981+ aopen = &au_sbi(dentry->d_sb)->si_aopen;
20982+ au_hbl_add(&aopen_node->hblist, aopen);
20983+ err = finish_open(file, dentry, au_do_aopen);
20984+ au_hbl_del(&aopen_node->hblist, aopen);
20985+ /* AuDbgFile(file); */
20986+ AuDbg("%pd%s%s\n", dentry,
20987+ (file->f_mode & FMODE_CREATED) ? " created" : "",
20988+ (file->f_mode & FMODE_OPENED) ? " opened" : "");
20989+
20990+ AuTraceErr(err);
20991+ return err;
20992+}
20993+
20994+static int au_aopen_no_open(struct file *file, struct dentry *dentry)
20995+{
20996+ int err;
20997+
20998+ AuLabel(here);
20999+ dget(dentry);
21000+ err = finish_no_open(file, dentry);
21001+
21002+ AuTraceErr(err);
21003+ return err;
21004+}
21005+
b912730e
AM
21006+static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
21007+ struct file *file, unsigned int open_flag,
acd2b654 21008+ umode_t create_mode)
b912730e 21009+{
acd2b654 21010+ int err, did_open;
5afbbe0d 21011+ unsigned int lkup_flags;
acd2b654
AM
21012+ aufs_bindex_t bindex;
21013+ struct super_block *sb;
f0c0a007 21014+ struct dentry *parent, *d;
b912730e
AM
21015+ struct vfsub_aopen_args args = {
21016+ .open_flag = open_flag,
acd2b654 21017+ .create_mode = create_mode
b912730e
AM
21018+ };
21019+ struct aopen_node aopen_node = {
21020+ .file = file
21021+ };
21022+
21023+ IMustLock(dir);
5afbbe0d 21024+ AuDbg("open_flag 0%o\n", open_flag);
b912730e
AM
21025+ AuDbgDentry(dentry);
21026+
21027+ err = 0;
21028+ if (!au_di(dentry)) {
5afbbe0d
AM
21029+ lkup_flags = LOOKUP_OPEN;
21030+ if (open_flag & O_CREAT)
21031+ lkup_flags |= LOOKUP_CREATE;
21032+ d = aufs_lookup(dir, dentry, lkup_flags);
b912730e
AM
21033+ if (IS_ERR(d)) {
21034+ err = PTR_ERR(d);
5afbbe0d 21035+ AuTraceErr(err);
b912730e
AM
21036+ goto out;
21037+ } else if (d) {
21038+ /*
21039+ * obsoleted dentry found.
21040+ * another error will be returned later.
21041+ */
21042+ d_drop(d);
b912730e 21043+ AuDbgDentry(d);
5afbbe0d 21044+ dput(d);
b912730e
AM
21045+ }
21046+ AuDbgDentry(dentry);
21047+ }
21048+
21049+ if (d_is_positive(dentry)
21050+ || d_unhashed(dentry)
21051+ || d_unlinked(dentry)
acd2b654
AM
21052+ || !(open_flag & O_CREAT)) {
21053+ err = au_aopen_no_open(file, dentry);
21054+ goto out; /* success */
21055+ }
b912730e
AM
21056+
21057+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
21058+ if (unlikely(err))
21059+ goto out;
21060+
acd2b654 21061+ sb = dentry->d_sb;
b912730e
AM
21062+ parent = dentry->d_parent; /* dir is locked */
21063+ di_write_lock_parent(parent);
5afbbe0d 21064+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
acd2b654
AM
21065+ if (unlikely(err < 0))
21066+ goto out_parent;
b912730e
AM
21067+
21068+ AuDbgDentry(dentry);
acd2b654
AM
21069+ if (d_is_positive(dentry)) {
21070+ err = au_aopen_no_open(file, dentry);
21071+ goto out_parent; /* success */
21072+ }
b912730e 21073+
acd2b654 21074+ args.file = alloc_empty_file(file->f_flags, current_cred());
b912730e
AM
21075+ err = PTR_ERR(args.file);
21076+ if (IS_ERR(args.file))
acd2b654 21077+ goto out_parent;
b912730e 21078+
acd2b654 21079+ bindex = au_dbtop(dentry);
b912730e
AM
21080+ err = au_aopen_or_create(dir, dentry, &args);
21081+ AuTraceErr(err);
21082+ AuDbgFile(args.file);
acd2b654
AM
21083+ file->f_mode = args.file->f_mode & ~FMODE_OPENED;
21084+ did_open = !!(args.file->f_mode & FMODE_OPENED);
21085+ if (!did_open) {
21086+ fput(args.file);
21087+ args.file = NULL;
b912730e 21088+ }
8b6a4947
AM
21089+ di_write_unlock(parent);
21090+ di_write_unlock(dentry);
acd2b654
AM
21091+ if (unlikely(err < 0)) {
21092+ if (args.file)
21093+ fput(args.file);
21094+ goto out_sb;
b912730e 21095+ }
b912730e 21096+
acd2b654
AM
21097+ if (!did_open)
21098+ err = au_aopen_no_open(file, dentry);
8b6a4947 21099+ else {
acd2b654
AM
21100+ aopen_node.h_file = args.file;
21101+ err = au_aopen_do_open(file, dentry, &aopen_node);
8b6a4947 21102+ }
acd2b654
AM
21103+ if (unlikely(err < 0)) {
21104+ if (args.file)
21105+ fput(args.file);
21106+ if (did_open)
21107+ au_lcnt_dec(&args.br->br_nfiles);
b912730e 21108+ }
acd2b654
AM
21109+ goto out_sb; /* success */
21110+
21111+out_parent:
21112+ di_write_unlock(parent);
21113+ di_write_unlock(dentry);
21114+out_sb:
21115+ si_read_unlock(sb);
b912730e 21116+out:
b912730e 21117+ AuTraceErr(err);
acd2b654 21118+ AuDbgFile(file);
b912730e
AM
21119+ return err;
21120+}
21121+
21122+
21123+/* ---------------------------------------------------------------------- */
21124+
4a4d8108
AM
21125+static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
21126+ const unsigned char add_entry, aufs_bindex_t bcpup,
5afbbe0d 21127+ aufs_bindex_t btop)
4a4d8108
AM
21128+{
21129+ int err;
21130+ struct dentry *h_parent;
21131+ struct inode *h_dir;
1facf9fc 21132+
027c5e7a 21133+ if (add_entry)
5527c038 21134+ IMustLock(d_inode(parent));
027c5e7a 21135+ else
4a4d8108
AM
21136+ di_write_lock_parent(parent);
21137+
21138+ err = 0;
21139+ if (!au_h_dptr(parent, bcpup)) {
5afbbe0d 21140+ if (btop > bcpup)
c2b27bf2 21141+ err = au_cpup_dirs(dentry, bcpup);
5afbbe0d 21142+ else if (btop < bcpup)
4a4d8108
AM
21143+ err = au_cpdown_dirs(dentry, bcpup);
21144+ else
c2b27bf2 21145+ BUG();
4a4d8108 21146+ }
38d290e6 21147+ if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
4a4d8108 21148+ h_parent = au_h_dptr(parent, bcpup);
5527c038 21149+ h_dir = d_inode(h_parent);
be118d29 21150+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
7e9cd9fe 21151+ err = au_lkup_neg(dentry, bcpup, /*wh*/0);
4a4d8108 21152+ /* todo: no unlock here */
3c1bdaff 21153+ inode_unlock_shared(h_dir);
027c5e7a
AM
21154+
21155+ AuDbg("bcpup %d\n", bcpup);
21156+ if (!err) {
5527c038 21157+ if (d_really_is_negative(dentry))
5afbbe0d 21158+ au_set_h_dptr(dentry, btop, NULL);
4a4d8108
AM
21159+ au_update_dbrange(dentry, /*do_put_zero*/0);
21160+ }
1308ab2a 21161+ }
1facf9fc 21162+
4a4d8108
AM
21163+ if (!add_entry)
21164+ di_write_unlock(parent);
21165+ if (!err)
21166+ err = bcpup; /* success */
1308ab2a 21167+
027c5e7a 21168+ AuTraceErr(err);
4a4d8108
AM
21169+ return err;
21170+}
1facf9fc 21171+
4a4d8108
AM
21172+/*
21173+ * decide the branch and the parent dir where we will create a new entry.
21174+ * returns new bindex or an error.
21175+ * copyup the parent dir if needed.
21176+ */
21177+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
21178+ struct au_wr_dir_args *args)
21179+{
21180+ int err;
392086de 21181+ unsigned int flags;
5afbbe0d 21182+ aufs_bindex_t bcpup, btop, src_btop;
86dc4139
AM
21183+ const unsigned char add_entry
21184+ = au_ftest_wrdir(args->flags, ADD_ENTRY)
38d290e6 21185+ | au_ftest_wrdir(args->flags, TMPFILE);
4a4d8108
AM
21186+ struct super_block *sb;
21187+ struct dentry *parent;
21188+ struct au_sbinfo *sbinfo;
1facf9fc 21189+
4a4d8108
AM
21190+ sb = dentry->d_sb;
21191+ sbinfo = au_sbi(sb);
21192+ parent = dget_parent(dentry);
5afbbe0d
AM
21193+ btop = au_dbtop(dentry);
21194+ bcpup = btop;
4a4d8108
AM
21195+ if (args->force_btgt < 0) {
21196+ if (src_dentry) {
5afbbe0d
AM
21197+ src_btop = au_dbtop(src_dentry);
21198+ if (src_btop < btop)
21199+ bcpup = src_btop;
4a4d8108 21200+ } else if (add_entry) {
392086de
AM
21201+ flags = 0;
21202+ if (au_ftest_wrdir(args->flags, ISDIR))
21203+ au_fset_wbr(flags, DIR);
21204+ err = AuWbrCreate(sbinfo, dentry, flags);
4a4d8108
AM
21205+ bcpup = err;
21206+ }
1facf9fc 21207+
5527c038 21208+ if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
4a4d8108
AM
21209+ if (add_entry)
21210+ err = AuWbrCopyup(sbinfo, dentry);
21211+ else {
21212+ if (!IS_ROOT(dentry)) {
21213+ di_read_lock_parent(parent, !AuLock_IR);
21214+ err = AuWbrCopyup(sbinfo, dentry);
21215+ di_read_unlock(parent, !AuLock_IR);
21216+ } else
21217+ err = AuWbrCopyup(sbinfo, dentry);
21218+ }
21219+ bcpup = err;
21220+ if (unlikely(err < 0))
21221+ goto out;
21222+ }
21223+ } else {
21224+ bcpup = args->force_btgt;
5527c038 21225+ AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
1308ab2a 21226+ }
027c5e7a 21227+
5afbbe0d 21228+ AuDbg("btop %d, bcpup %d\n", btop, bcpup);
4a4d8108 21229+ err = bcpup;
5afbbe0d 21230+ if (bcpup == btop)
4a4d8108 21231+ goto out; /* success */
4a4d8108
AM
21232+
21233+ /* copyup the new parent into the branch we process */
5afbbe0d 21234+ err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
027c5e7a 21235+ if (err >= 0) {
5527c038 21236+ if (d_really_is_negative(dentry)) {
5afbbe0d
AM
21237+ au_set_h_dptr(dentry, btop, NULL);
21238+ au_set_dbtop(dentry, bcpup);
21239+ au_set_dbbot(dentry, bcpup);
027c5e7a 21240+ }
38d290e6
JR
21241+ AuDebugOn(add_entry
21242+ && !au_ftest_wrdir(args->flags, TMPFILE)
21243+ && !au_h_dptr(dentry, bcpup));
027c5e7a 21244+ }
86dc4139
AM
21245+
21246+out:
21247+ dput(parent);
21248+ return err;
21249+}
21250+
21251+/* ---------------------------------------------------------------------- */
21252+
21253+void au_pin_hdir_unlock(struct au_pin *p)
21254+{
21255+ if (p->hdir)
5afbbe0d 21256+ au_hn_inode_unlock(p->hdir);
86dc4139
AM
21257+}
21258+
c1595e42 21259+int au_pin_hdir_lock(struct au_pin *p)
86dc4139
AM
21260+{
21261+ int err;
21262+
21263+ err = 0;
21264+ if (!p->hdir)
21265+ goto out;
21266+
21267+ /* even if an error happens later, keep this lock */
5afbbe0d 21268+ au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
86dc4139
AM
21269+
21270+ err = -EBUSY;
5527c038 21271+ if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
86dc4139
AM
21272+ goto out;
21273+
21274+ err = 0;
21275+ if (p->h_dentry)
21276+ err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
21277+ p->h_parent, p->br);
21278+
21279+out:
21280+ return err;
21281+}
21282+
21283+int au_pin_hdir_relock(struct au_pin *p)
21284+{
21285+ int err, i;
21286+ struct inode *h_i;
21287+ struct dentry *h_d[] = {
21288+ p->h_dentry,
21289+ p->h_parent
21290+ };
21291+
21292+ err = au_pin_hdir_lock(p);
21293+ if (unlikely(err))
21294+ goto out;
21295+
21296+ for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
21297+ if (!h_d[i])
21298+ continue;
5527c038
JR
21299+ if (d_is_positive(h_d[i])) {
21300+ h_i = d_inode(h_d[i]);
86dc4139 21301+ err = !h_i->i_nlink;
5527c038 21302+ }
86dc4139
AM
21303+ }
21304+
21305+out:
21306+ return err;
21307+}
21308+
5afbbe0d 21309+static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
86dc4139 21310+{
5afbbe0d
AM
21311+#if !defined(CONFIG_RWSEM_GENERIC_SPINLOCK) && defined(CONFIG_RWSEM_SPIN_ON_OWNER)
21312+ p->hdir->hi_inode->i_rwsem.owner = task;
86dc4139
AM
21313+#endif
21314+}
21315+
21316+void au_pin_hdir_acquire_nest(struct au_pin *p)
21317+{
21318+ if (p->hdir) {
5afbbe0d 21319+ rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
86dc4139
AM
21320+ p->lsc_hi, 0, NULL, _RET_IP_);
21321+ au_pin_hdir_set_owner(p, current);
21322+ }
dece6358 21323+}
1facf9fc 21324+
86dc4139
AM
21325+void au_pin_hdir_release(struct au_pin *p)
21326+{
21327+ if (p->hdir) {
21328+ au_pin_hdir_set_owner(p, p->task);
5afbbe0d 21329+ rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, 1, _RET_IP_);
86dc4139
AM
21330+ }
21331+}
1308ab2a 21332+
4a4d8108 21333+struct dentry *au_pinned_h_parent(struct au_pin *pin)
1308ab2a 21334+{
4a4d8108
AM
21335+ if (pin && pin->parent)
21336+ return au_h_dptr(pin->parent, pin->bindex);
21337+ return NULL;
dece6358 21338+}
1facf9fc 21339+
4a4d8108 21340+void au_unpin(struct au_pin *p)
dece6358 21341+{
86dc4139
AM
21342+ if (p->hdir)
21343+ au_pin_hdir_unlock(p);
e49829fe 21344+ if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
b4510431 21345+ vfsub_mnt_drop_write(p->h_mnt);
4a4d8108
AM
21346+ if (!p->hdir)
21347+ return;
1facf9fc 21348+
4a4d8108
AM
21349+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21350+ di_read_unlock(p->parent, AuLock_IR);
21351+ iput(p->hdir->hi_inode);
21352+ dput(p->parent);
21353+ p->parent = NULL;
21354+ p->hdir = NULL;
21355+ p->h_mnt = NULL;
86dc4139 21356+ /* do not clear p->task */
4a4d8108 21357+}
1308ab2a 21358+
4a4d8108
AM
21359+int au_do_pin(struct au_pin *p)
21360+{
21361+ int err;
21362+ struct super_block *sb;
4a4d8108
AM
21363+ struct inode *h_dir;
21364+
21365+ err = 0;
21366+ sb = p->dentry->d_sb;
86dc4139 21367+ p->br = au_sbr(sb, p->bindex);
4a4d8108
AM
21368+ if (IS_ROOT(p->dentry)) {
21369+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 21370+ p->h_mnt = au_br_mnt(p->br);
b4510431 21371+ err = vfsub_mnt_want_write(p->h_mnt);
4a4d8108
AM
21372+ if (unlikely(err)) {
21373+ au_fclr_pin(p->flags, MNT_WRITE);
21374+ goto out_err;
21375+ }
21376+ }
dece6358 21377+ goto out;
1facf9fc 21378+ }
21379+
86dc4139 21380+ p->h_dentry = NULL;
5afbbe0d 21381+ if (p->bindex <= au_dbbot(p->dentry))
86dc4139 21382+ p->h_dentry = au_h_dptr(p->dentry, p->bindex);
dece6358 21383+
4a4d8108
AM
21384+ p->parent = dget_parent(p->dentry);
21385+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21386+ di_read_lock(p->parent, AuLock_IR, p->lsc_di);
dece6358 21387+
4a4d8108 21388+ h_dir = NULL;
86dc4139 21389+ p->h_parent = au_h_dptr(p->parent, p->bindex);
5527c038 21390+ p->hdir = au_hi(d_inode(p->parent), p->bindex);
4a4d8108
AM
21391+ if (p->hdir)
21392+ h_dir = p->hdir->hi_inode;
dece6358 21393+
b752ccd1
AM
21394+ /*
21395+ * udba case, or
21396+ * if DI_LOCKED is not set, then p->parent may be different
21397+ * and h_parent can be NULL.
21398+ */
86dc4139 21399+ if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
e49829fe 21400+ err = -EBUSY;
4a4d8108
AM
21401+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21402+ di_read_unlock(p->parent, AuLock_IR);
21403+ dput(p->parent);
21404+ p->parent = NULL;
21405+ goto out_err;
21406+ }
1308ab2a 21407+
4a4d8108 21408+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 21409+ p->h_mnt = au_br_mnt(p->br);
b4510431 21410+ err = vfsub_mnt_want_write(p->h_mnt);
dece6358 21411+ if (unlikely(err)) {
4a4d8108 21412+ au_fclr_pin(p->flags, MNT_WRITE);
86dc4139
AM
21413+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21414+ di_read_unlock(p->parent, AuLock_IR);
21415+ dput(p->parent);
21416+ p->parent = NULL;
21417+ goto out_err;
dece6358
AM
21418+ }
21419+ }
4a4d8108 21420+
86dc4139
AM
21421+ au_igrab(h_dir);
21422+ err = au_pin_hdir_lock(p);
21423+ if (!err)
21424+ goto out; /* success */
21425+
076b876e
AM
21426+ au_unpin(p);
21427+
4f0767ce 21428+out_err:
4a4d8108
AM
21429+ pr_err("err %d\n", err);
21430+ err = au_busy_or_stale();
4f0767ce 21431+out:
1facf9fc 21432+ return err;
21433+}
21434+
4a4d8108
AM
21435+void au_pin_init(struct au_pin *p, struct dentry *dentry,
21436+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
21437+ unsigned int udba, unsigned char flags)
21438+{
21439+ p->dentry = dentry;
21440+ p->udba = udba;
21441+ p->lsc_di = lsc_di;
21442+ p->lsc_hi = lsc_hi;
21443+ p->flags = flags;
21444+ p->bindex = bindex;
21445+
21446+ p->parent = NULL;
21447+ p->hdir = NULL;
21448+ p->h_mnt = NULL;
86dc4139
AM
21449+
21450+ p->h_dentry = NULL;
21451+ p->h_parent = NULL;
21452+ p->br = NULL;
21453+ p->task = current;
4a4d8108
AM
21454+}
21455+
21456+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
21457+ unsigned int udba, unsigned char flags)
21458+{
21459+ au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
21460+ udba, flags);
21461+ return au_do_pin(pin);
21462+}
21463+
dece6358
AM
21464+/* ---------------------------------------------------------------------- */
21465+
1308ab2a 21466+/*
4a4d8108
AM
21467+ * ->setattr() and ->getattr() are called in various cases.
21468+ * chmod, stat: dentry is revalidated.
21469+ * fchmod, fstat: file and dentry are not revalidated, additionally they may be
21470+ * unhashed.
21471+ * for ->setattr(), ia->ia_file is passed from ftruncate only.
1308ab2a 21472+ */
027c5e7a 21473+/* todo: consolidate with do_refresh() and simple_reval_dpath() */
c1595e42 21474+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
1facf9fc 21475+{
4a4d8108 21476+ int err;
4a4d8108 21477+ struct dentry *parent;
1facf9fc 21478+
1308ab2a 21479+ err = 0;
027c5e7a 21480+ if (au_digen_test(dentry, sigen)) {
4a4d8108
AM
21481+ parent = dget_parent(dentry);
21482+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 21483+ err = au_refresh_dentry(dentry, parent);
4a4d8108
AM
21484+ di_read_unlock(parent, AuLock_IR);
21485+ dput(parent);
dece6358 21486+ }
1facf9fc 21487+
4a4d8108 21488+ AuTraceErr(err);
1308ab2a 21489+ return err;
21490+}
dece6358 21491+
c1595e42
JR
21492+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
21493+ struct au_icpup_args *a)
1308ab2a 21494+{
21495+ int err;
4a4d8108 21496+ loff_t sz;
5afbbe0d 21497+ aufs_bindex_t btop, ibtop;
4a4d8108
AM
21498+ struct dentry *hi_wh, *parent;
21499+ struct inode *inode;
4a4d8108
AM
21500+ struct au_wr_dir_args wr_dir_args = {
21501+ .force_btgt = -1,
21502+ .flags = 0
21503+ };
21504+
2000de60 21505+ if (d_is_dir(dentry))
4a4d8108
AM
21506+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
21507+ /* plink or hi_wh() case */
5afbbe0d 21508+ btop = au_dbtop(dentry);
5527c038 21509+ inode = d_inode(dentry);
5afbbe0d
AM
21510+ ibtop = au_ibtop(inode);
21511+ if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
21512+ wr_dir_args.force_btgt = ibtop;
4a4d8108
AM
21513+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21514+ if (unlikely(err < 0))
21515+ goto out;
21516+ a->btgt = err;
5afbbe0d 21517+ if (err != btop)
4a4d8108
AM
21518+ au_fset_icpup(a->flags, DID_CPUP);
21519+
21520+ err = 0;
21521+ a->pin_flags = AuPin_MNT_WRITE;
21522+ parent = NULL;
21523+ if (!IS_ROOT(dentry)) {
21524+ au_fset_pin(a->pin_flags, DI_LOCKED);
21525+ parent = dget_parent(dentry);
21526+ di_write_lock_parent(parent);
21527+ }
21528+
21529+ err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
21530+ if (unlikely(err))
21531+ goto out_parent;
21532+
4a4d8108 21533+ sz = -1;
5afbbe0d 21534+ a->h_path.dentry = au_h_dptr(dentry, btop);
5527c038 21535+ a->h_inode = d_inode(a->h_path.dentry);
c1595e42 21536+ if (ia && (ia->ia_valid & ATTR_SIZE)) {
be118d29 21537+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
c1595e42
JR
21538+ if (ia->ia_size < i_size_read(a->h_inode))
21539+ sz = ia->ia_size;
3c1bdaff 21540+ inode_unlock_shared(a->h_inode);
c1595e42 21541+ }
4a4d8108 21542+
4a4d8108 21543+ hi_wh = NULL;
027c5e7a 21544+ if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
4a4d8108
AM
21545+ hi_wh = au_hi_wh(inode, a->btgt);
21546+ if (!hi_wh) {
c2b27bf2
AM
21547+ struct au_cp_generic cpg = {
21548+ .dentry = dentry,
21549+ .bdst = a->btgt,
21550+ .bsrc = -1,
21551+ .len = sz,
21552+ .pin = &a->pin
21553+ };
21554+ err = au_sio_cpup_wh(&cpg, /*file*/NULL);
4a4d8108
AM
21555+ if (unlikely(err))
21556+ goto out_unlock;
21557+ hi_wh = au_hi_wh(inode, a->btgt);
21558+ /* todo: revalidate hi_wh? */
21559+ }
21560+ }
21561+
21562+ if (parent) {
21563+ au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
21564+ di_downgrade_lock(parent, AuLock_IR);
21565+ dput(parent);
21566+ parent = NULL;
21567+ }
21568+ if (!au_ftest_icpup(a->flags, DID_CPUP))
21569+ goto out; /* success */
21570+
21571+ if (!d_unhashed(dentry)) {
c2b27bf2
AM
21572+ struct au_cp_generic cpg = {
21573+ .dentry = dentry,
21574+ .bdst = a->btgt,
5afbbe0d 21575+ .bsrc = btop,
c2b27bf2
AM
21576+ .len = sz,
21577+ .pin = &a->pin,
21578+ .flags = AuCpup_DTIME | AuCpup_HOPEN
21579+ };
21580+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
21581+ if (!err)
21582+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21583+ } else if (!hi_wh)
21584+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21585+ else
21586+ a->h_path.dentry = hi_wh; /* do not dget here */
1308ab2a 21587+
4f0767ce 21588+out_unlock:
5527c038 21589+ a->h_inode = d_inode(a->h_path.dentry);
86dc4139 21590+ if (!err)
dece6358 21591+ goto out; /* success */
4a4d8108 21592+ au_unpin(&a->pin);
4f0767ce 21593+out_parent:
4a4d8108
AM
21594+ if (parent) {
21595+ di_write_unlock(parent);
21596+ dput(parent);
21597+ }
4f0767ce 21598+out:
86dc4139 21599+ if (!err)
febd17d6 21600+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
1facf9fc 21601+ return err;
21602+}
21603+
4a4d8108 21604+static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
1facf9fc 21605+{
4a4d8108 21606+ int err;
523b37e3 21607+ struct inode *inode, *delegated;
4a4d8108
AM
21608+ struct super_block *sb;
21609+ struct file *file;
21610+ struct au_icpup_args *a;
1facf9fc 21611+
5527c038 21612+ inode = d_inode(dentry);
4a4d8108 21613+ IMustLock(inode);
dece6358 21614+
f2c43d5f
AM
21615+ err = setattr_prepare(dentry, ia);
21616+ if (unlikely(err))
21617+ goto out;
21618+
4a4d8108
AM
21619+ err = -ENOMEM;
21620+ a = kzalloc(sizeof(*a), GFP_NOFS);
21621+ if (unlikely(!a))
21622+ goto out;
1facf9fc 21623+
4a4d8108
AM
21624+ if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
21625+ ia->ia_valid &= ~ATTR_MODE;
dece6358 21626+
4a4d8108
AM
21627+ file = NULL;
21628+ sb = dentry->d_sb;
e49829fe
JR
21629+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21630+ if (unlikely(err))
21631+ goto out_kfree;
21632+
4a4d8108
AM
21633+ if (ia->ia_valid & ATTR_FILE) {
21634+ /* currently ftruncate(2) only */
7e9cd9fe 21635+ AuDebugOn(!d_is_reg(dentry));
4a4d8108 21636+ file = ia->ia_file;
521ced18
JR
21637+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
21638+ /*fi_lsc*/0);
4a4d8108
AM
21639+ if (unlikely(err))
21640+ goto out_si;
21641+ ia->ia_file = au_hf_top(file);
21642+ a->udba = AuOpt_UDBA_NONE;
21643+ } else {
21644+ /* fchmod() doesn't pass ia_file */
21645+ a->udba = au_opt_udba(sb);
027c5e7a
AM
21646+ di_write_lock_child(dentry);
21647+ /* no d_unlinked(), to set UDBA_NONE for root */
4a4d8108
AM
21648+ if (d_unhashed(dentry))
21649+ a->udba = AuOpt_UDBA_NONE;
4a4d8108
AM
21650+ if (a->udba != AuOpt_UDBA_NONE) {
21651+ AuDebugOn(IS_ROOT(dentry));
21652+ err = au_reval_for_attr(dentry, au_sigen(sb));
21653+ if (unlikely(err))
21654+ goto out_dentry;
21655+ }
dece6358 21656+ }
dece6358 21657+
4a4d8108
AM
21658+ err = au_pin_and_icpup(dentry, ia, a);
21659+ if (unlikely(err < 0))
21660+ goto out_dentry;
21661+ if (au_ftest_icpup(a->flags, DID_CPUP)) {
21662+ ia->ia_file = NULL;
21663+ ia->ia_valid &= ~ATTR_FILE;
1308ab2a 21664+ }
dece6358 21665+
4a4d8108
AM
21666+ a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
21667+ if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
21668+ == (ATTR_MODE | ATTR_CTIME)) {
7eafdf33 21669+ err = security_path_chmod(&a->h_path, ia->ia_mode);
4a4d8108
AM
21670+ if (unlikely(err))
21671+ goto out_unlock;
21672+ } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
21673+ && (ia->ia_valid & ATTR_CTIME)) {
86dc4139 21674+ err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
4a4d8108
AM
21675+ if (unlikely(err))
21676+ goto out_unlock;
21677+ }
dece6358 21678+
4a4d8108
AM
21679+ if (ia->ia_valid & ATTR_SIZE) {
21680+ struct file *f;
1308ab2a 21681+
953406b4 21682+ if (ia->ia_size < i_size_read(inode))
4a4d8108 21683+ /* unmap only */
953406b4 21684+ truncate_setsize(inode, ia->ia_size);
1308ab2a 21685+
4a4d8108
AM
21686+ f = NULL;
21687+ if (ia->ia_valid & ATTR_FILE)
21688+ f = ia->ia_file;
febd17d6 21689+ inode_unlock(a->h_inode);
4a4d8108 21690+ err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
febd17d6 21691+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
523b37e3
AM
21692+ } else {
21693+ delegated = NULL;
21694+ while (1) {
21695+ err = vfsub_notify_change(&a->h_path, ia, &delegated);
21696+ if (delegated) {
21697+ err = break_deleg_wait(&delegated);
21698+ if (!err)
21699+ continue;
21700+ }
21701+ break;
21702+ }
21703+ }
8cdd5066
JR
21704+ /*
21705+ * regardless aufs 'acl' option setting.
21706+ * why don't all acl-aware fs call this func from their ->setattr()?
21707+ */
21708+ if (!err && (ia->ia_valid & ATTR_MODE))
21709+ err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
4a4d8108
AM
21710+ if (!err)
21711+ au_cpup_attr_changeable(inode);
1308ab2a 21712+
4f0767ce 21713+out_unlock:
febd17d6 21714+ inode_unlock(a->h_inode);
4a4d8108 21715+ au_unpin(&a->pin);
027c5e7a 21716+ if (unlikely(err))
5afbbe0d 21717+ au_update_dbtop(dentry);
4f0767ce 21718+out_dentry:
4a4d8108
AM
21719+ di_write_unlock(dentry);
21720+ if (file) {
21721+ fi_write_unlock(file);
21722+ ia->ia_file = file;
21723+ ia->ia_valid |= ATTR_FILE;
21724+ }
4f0767ce 21725+out_si:
4a4d8108 21726+ si_read_unlock(sb);
e49829fe 21727+out_kfree:
9f237c51 21728+ au_kfree_rcu(a);
4f0767ce 21729+out:
4a4d8108
AM
21730+ AuTraceErr(err);
21731+ return err;
1facf9fc 21732+}
21733+
c1595e42
JR
21734+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
21735+static int au_h_path_to_set_attr(struct dentry *dentry,
21736+ struct au_icpup_args *a, struct path *h_path)
21737+{
21738+ int err;
21739+ struct super_block *sb;
21740+
21741+ sb = dentry->d_sb;
21742+ a->udba = au_opt_udba(sb);
21743+ /* no d_unlinked(), to set UDBA_NONE for root */
21744+ if (d_unhashed(dentry))
21745+ a->udba = AuOpt_UDBA_NONE;
21746+ if (a->udba != AuOpt_UDBA_NONE) {
21747+ AuDebugOn(IS_ROOT(dentry));
21748+ err = au_reval_for_attr(dentry, au_sigen(sb));
21749+ if (unlikely(err))
21750+ goto out;
21751+ }
21752+ err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
21753+ if (unlikely(err < 0))
21754+ goto out;
21755+
21756+ h_path->dentry = a->h_path.dentry;
21757+ h_path->mnt = au_sbr_mnt(sb, a->btgt);
21758+
21759+out:
21760+ return err;
21761+}
21762+
f2c43d5f
AM
21763+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
21764+ struct au_sxattr *arg)
c1595e42
JR
21765+{
21766+ int err;
21767+ struct path h_path;
21768+ struct super_block *sb;
21769+ struct au_icpup_args *a;
5afbbe0d 21770+ struct inode *h_inode;
c1595e42 21771+
c1595e42
JR
21772+ IMustLock(inode);
21773+
21774+ err = -ENOMEM;
21775+ a = kzalloc(sizeof(*a), GFP_NOFS);
21776+ if (unlikely(!a))
21777+ goto out;
21778+
21779+ sb = dentry->d_sb;
21780+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21781+ if (unlikely(err))
21782+ goto out_kfree;
21783+
21784+ h_path.dentry = NULL; /* silence gcc */
21785+ di_write_lock_child(dentry);
21786+ err = au_h_path_to_set_attr(dentry, a, &h_path);
21787+ if (unlikely(err))
21788+ goto out_di;
21789+
febd17d6 21790+ inode_unlock(a->h_inode);
c1595e42
JR
21791+ switch (arg->type) {
21792+ case AU_XATTR_SET:
5afbbe0d 21793+ AuDebugOn(d_is_negative(h_path.dentry));
c1595e42
JR
21794+ err = vfsub_setxattr(h_path.dentry,
21795+ arg->u.set.name, arg->u.set.value,
21796+ arg->u.set.size, arg->u.set.flags);
21797+ break;
c1595e42
JR
21798+ case AU_ACL_SET:
21799+ err = -EOPNOTSUPP;
5527c038 21800+ h_inode = d_inode(h_path.dentry);
c1595e42 21801+ if (h_inode->i_op->set_acl)
f2c43d5f 21802+ /* this will call posix_acl_update_mode */
c1595e42
JR
21803+ err = h_inode->i_op->set_acl(h_inode,
21804+ arg->u.acl_set.acl,
21805+ arg->u.acl_set.type);
21806+ break;
21807+ }
21808+ if (!err)
21809+ au_cpup_attr_timesizes(inode);
21810+
21811+ au_unpin(&a->pin);
21812+ if (unlikely(err))
5afbbe0d 21813+ au_update_dbtop(dentry);
c1595e42
JR
21814+
21815+out_di:
21816+ di_write_unlock(dentry);
21817+ si_read_unlock(sb);
21818+out_kfree:
9f237c51 21819+ au_kfree_rcu(a);
c1595e42
JR
21820+out:
21821+ AuTraceErr(err);
21822+ return err;
21823+}
21824+#endif
21825+
4a4d8108
AM
21826+static void au_refresh_iattr(struct inode *inode, struct kstat *st,
21827+ unsigned int nlink)
1facf9fc 21828+{
9dbd164d
AM
21829+ unsigned int n;
21830+
4a4d8108 21831+ inode->i_mode = st->mode;
86dc4139
AM
21832+ /* don't i_[ug]id_write() here */
21833+ inode->i_uid = st->uid;
21834+ inode->i_gid = st->gid;
4a4d8108
AM
21835+ inode->i_atime = st->atime;
21836+ inode->i_mtime = st->mtime;
21837+ inode->i_ctime = st->ctime;
1facf9fc 21838+
4a4d8108
AM
21839+ au_cpup_attr_nlink(inode, /*force*/0);
21840+ if (S_ISDIR(inode->i_mode)) {
9dbd164d
AM
21841+ n = inode->i_nlink;
21842+ n -= nlink;
21843+ n += st->nlink;
f6b6e03d 21844+ smp_mb(); /* for i_nlink */
7eafdf33 21845+ /* 0 can happen */
92d182d2 21846+ set_nlink(inode, n);
4a4d8108 21847+ }
1facf9fc 21848+
4a4d8108
AM
21849+ spin_lock(&inode->i_lock);
21850+ inode->i_blocks = st->blocks;
21851+ i_size_write(inode, st->size);
21852+ spin_unlock(&inode->i_lock);
1facf9fc 21853+}
21854+
c1595e42 21855+/*
f2c43d5f 21856+ * common routine for aufs_getattr() and au_getxattr().
c1595e42
JR
21857+ * returns zero or negative (an error).
21858+ * @dentry will be read-locked in success.
21859+ */
a2654f78
AM
21860+int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
21861+ int locked)
1facf9fc 21862+{
4a4d8108 21863+ int err;
076b876e 21864+ unsigned int mnt_flags, sigen;
c1595e42 21865+ unsigned char udba_none;
4a4d8108 21866+ aufs_bindex_t bindex;
4a4d8108
AM
21867+ struct super_block *sb, *h_sb;
21868+ struct inode *inode;
1facf9fc 21869+
c1595e42
JR
21870+ h_path->mnt = NULL;
21871+ h_path->dentry = NULL;
21872+
21873+ err = 0;
4a4d8108 21874+ sb = dentry->d_sb;
4a4d8108
AM
21875+ mnt_flags = au_mntflags(sb);
21876+ udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
1facf9fc 21877+
a2654f78
AM
21878+ if (unlikely(locked))
21879+ goto body; /* skip locking dinfo */
21880+
4a4d8108 21881+ /* support fstat(2) */
027c5e7a 21882+ if (!d_unlinked(dentry) && !udba_none) {
076b876e 21883+ sigen = au_sigen(sb);
027c5e7a
AM
21884+ err = au_digen_test(dentry, sigen);
21885+ if (!err) {
4a4d8108 21886+ di_read_lock_child(dentry, AuLock_IR);
027c5e7a 21887+ err = au_dbrange_test(dentry);
c1595e42
JR
21888+ if (unlikely(err)) {
21889+ di_read_unlock(dentry, AuLock_IR);
21890+ goto out;
21891+ }
027c5e7a 21892+ } else {
4a4d8108
AM
21893+ AuDebugOn(IS_ROOT(dentry));
21894+ di_write_lock_child(dentry);
027c5e7a
AM
21895+ err = au_dbrange_test(dentry);
21896+ if (!err)
21897+ err = au_reval_for_attr(dentry, sigen);
c1595e42
JR
21898+ if (!err)
21899+ di_downgrade_lock(dentry, AuLock_IR);
21900+ else {
21901+ di_write_unlock(dentry);
21902+ goto out;
21903+ }
4a4d8108
AM
21904+ }
21905+ } else
21906+ di_read_lock_child(dentry, AuLock_IR);
1facf9fc 21907+
a2654f78 21908+body:
5527c038 21909+ inode = d_inode(dentry);
5afbbe0d 21910+ bindex = au_ibtop(inode);
c1595e42
JR
21911+ h_path->mnt = au_sbr_mnt(sb, bindex);
21912+ h_sb = h_path->mnt->mnt_sb;
21913+ if (!force
21914+ && !au_test_fs_bad_iattr(h_sb)
21915+ && udba_none)
21916+ goto out; /* success */
1facf9fc 21917+
5afbbe0d 21918+ if (au_dbtop(dentry) == bindex)
c1595e42 21919+ h_path->dentry = au_h_dptr(dentry, bindex);
4a4d8108 21920+ else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
c1595e42
JR
21921+ h_path->dentry = au_plink_lkup(inode, bindex);
21922+ if (IS_ERR(h_path->dentry))
21923+ /* pretending success */
21924+ h_path->dentry = NULL;
21925+ else
21926+ dput(h_path->dentry);
4a4d8108 21927+ }
c1595e42
JR
21928+
21929+out:
21930+ return err;
21931+}
21932+
521ced18
JR
21933+static int aufs_getattr(const struct path *path, struct kstat *st,
21934+ u32 request, unsigned int query)
c1595e42
JR
21935+{
21936+ int err;
21937+ unsigned char positive;
21938+ struct path h_path;
521ced18 21939+ struct dentry *dentry;
c1595e42
JR
21940+ struct inode *inode;
21941+ struct super_block *sb;
21942+
521ced18 21943+ dentry = path->dentry;
5527c038 21944+ inode = d_inode(dentry);
c1595e42
JR
21945+ sb = dentry->d_sb;
21946+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21947+ if (unlikely(err))
21948+ goto out;
a2654f78 21949+ err = au_h_path_getattr(dentry, /*force*/0, &h_path, /*locked*/0);
c1595e42
JR
21950+ if (unlikely(err))
21951+ goto out_si;
c06a8ce3 21952+ if (unlikely(!h_path.dentry))
c1595e42 21953+ /* illegally overlapped or something */
4a4d8108
AM
21954+ goto out_fill; /* pretending success */
21955+
5527c038 21956+ positive = d_is_positive(h_path.dentry);
4a4d8108 21957+ if (positive)
521ced18
JR
21958+ /* no vfsub version */
21959+ err = vfs_getattr(&h_path, st, request, query);
4a4d8108
AM
21960+ if (!err) {
21961+ if (positive)
c06a8ce3 21962+ au_refresh_iattr(inode, st,
5527c038 21963+ d_inode(h_path.dentry)->i_nlink);
4a4d8108 21964+ goto out_fill; /* success */
1facf9fc 21965+ }
7f207e10 21966+ AuTraceErr(err);
c1595e42 21967+ goto out_di;
4a4d8108 21968+
4f0767ce 21969+out_fill:
4a4d8108 21970+ generic_fillattr(inode, st);
c1595e42 21971+out_di:
4a4d8108 21972+ di_read_unlock(dentry, AuLock_IR);
c1595e42 21973+out_si:
4a4d8108 21974+ si_read_unlock(sb);
7f207e10
AM
21975+out:
21976+ AuTraceErr(err);
4a4d8108 21977+ return err;
1facf9fc 21978+}
21979+
21980+/* ---------------------------------------------------------------------- */
21981+
febd17d6
JR
21982+static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
21983+ struct delayed_call *done)
4a4d8108 21984+{
c2c0f25c 21985+ const char *ret;
c2c0f25c 21986+ struct dentry *h_dentry;
febd17d6 21987+ struct inode *h_inode;
4a4d8108 21988+ int err;
c2c0f25c 21989+ aufs_bindex_t bindex;
1facf9fc 21990+
79b8bda9 21991+ ret = NULL; /* suppress a warning */
febd17d6
JR
21992+ err = -ECHILD;
21993+ if (!dentry)
21994+ goto out;
21995+
027c5e7a
AM
21996+ err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
21997+ if (unlikely(err))
c2c0f25c 21998+ goto out;
027c5e7a
AM
21999+
22000+ err = au_d_hashed_positive(dentry);
c2c0f25c
AM
22001+ if (unlikely(err))
22002+ goto out_unlock;
22003+
22004+ err = -EINVAL;
22005+ inode = d_inode(dentry);
5afbbe0d 22006+ bindex = au_ibtop(inode);
c2c0f25c 22007+ h_inode = au_h_iptr(inode, bindex);
febd17d6 22008+ if (unlikely(!h_inode->i_op->get_link))
c2c0f25c
AM
22009+ goto out_unlock;
22010+
22011+ err = -EBUSY;
22012+ h_dentry = NULL;
5afbbe0d 22013+ if (au_dbtop(dentry) <= bindex) {
c2c0f25c
AM
22014+ h_dentry = au_h_dptr(dentry, bindex);
22015+ if (h_dentry)
22016+ dget(h_dentry);
027c5e7a 22017+ }
c2c0f25c
AM
22018+ if (!h_dentry) {
22019+ h_dentry = d_find_any_alias(h_inode);
22020+ if (IS_ERR(h_dentry)) {
22021+ err = PTR_ERR(h_dentry);
febd17d6 22022+ goto out_unlock;
c2c0f25c
AM
22023+ }
22024+ }
22025+ if (unlikely(!h_dentry))
febd17d6 22026+ goto out_unlock;
1facf9fc 22027+
c2c0f25c 22028+ err = 0;
062440b3 22029+ AuDbg("%ps\n", h_inode->i_op->get_link);
c2c0f25c 22030+ AuDbgDentry(h_dentry);
f2c43d5f 22031+ ret = vfs_get_link(h_dentry, done);
c2c0f25c 22032+ dput(h_dentry);
febd17d6
JR
22033+ if (IS_ERR(ret))
22034+ err = PTR_ERR(ret);
c2c0f25c 22035+
c2c0f25c
AM
22036+out_unlock:
22037+ aufs_read_unlock(dentry, AuLock_IR);
4f0767ce 22038+out:
c2c0f25c
AM
22039+ if (unlikely(err))
22040+ ret = ERR_PTR(err);
22041+ AuTraceErrPtr(ret);
22042+ return ret;
4a4d8108 22043+}
1facf9fc 22044+
4a4d8108 22045+/* ---------------------------------------------------------------------- */
1facf9fc 22046+
e2f27e51
AM
22047+static int au_is_special(struct inode *inode)
22048+{
22049+ return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
22050+}
22051+
acd2b654
AM
22052+static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
22053+ int flags)
4a4d8108 22054+{
0c3ec466 22055+ int err;
e2f27e51 22056+ aufs_bindex_t bindex;
0c3ec466
AM
22057+ struct super_block *sb;
22058+ struct inode *h_inode;
e2f27e51 22059+ struct vfsmount *h_mnt;
0c3ec466
AM
22060+
22061+ sb = inode->i_sb;
e2f27e51
AM
22062+ WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
22063+ "unexpected s_flags 0x%lx", sb->s_flags);
22064+
0c3ec466
AM
22065+ /* mmap_sem might be acquired already, cf. aufs_mmap() */
22066+ lockdep_off();
22067+ si_read_lock(sb, AuLock_FLUSH);
22068+ ii_write_lock_child(inode);
e2f27e51
AM
22069+
22070+ err = 0;
22071+ bindex = au_ibtop(inode);
22072+ h_inode = au_h_iptr(inode, bindex);
22073+ if (!au_test_ro(sb, bindex, inode)) {
22074+ h_mnt = au_sbr_mnt(sb, bindex);
22075+ err = vfsub_mnt_want_write(h_mnt);
22076+ if (!err) {
22077+ err = vfsub_update_time(h_inode, ts, flags);
22078+ vfsub_mnt_drop_write(h_mnt);
22079+ }
22080+ } else if (au_is_special(h_inode)) {
22081+ /*
22082+ * Never copy-up here.
22083+ * These special files may already be opened and used for
22084+ * communicating. If we copied it up, then the communication
22085+ * would be corrupted.
22086+ */
22087+ AuWarn1("timestamps for i%lu are ignored "
22088+ "since it is on readonly branch (hi%lu).\n",
22089+ inode->i_ino, h_inode->i_ino);
22090+ } else if (flags & ~S_ATIME) {
22091+ err = -EIO;
22092+ AuIOErr1("unexpected flags 0x%x\n", flags);
22093+ AuDebugOn(1);
22094+ }
22095+
38d290e6
JR
22096+ if (!err)
22097+ au_cpup_attr_timesizes(inode);
0c3ec466
AM
22098+ ii_write_unlock(inode);
22099+ si_read_unlock(sb);
22100+ lockdep_on();
38d290e6
JR
22101+
22102+ if (!err && (flags & S_VERSION))
22103+ inode_inc_iversion(inode);
22104+
0c3ec466 22105+ return err;
4a4d8108 22106+}
1facf9fc 22107+
4a4d8108 22108+/* ---------------------------------------------------------------------- */
1308ab2a 22109+
b95c5147
AM
22110+/* no getattr version will be set by module.c:aufs_init() */
22111+struct inode_operations aufs_iop_nogetattr[AuIop_Last],
22112+ aufs_iop[] = {
22113+ [AuIop_SYMLINK] = {
22114+ .permission = aufs_permission,
c1595e42 22115+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
22116+ .get_acl = aufs_get_acl,
22117+ .set_acl = aufs_set_acl, /* unsupport for symlink? */
c1595e42
JR
22118+#endif
22119+
b95c5147
AM
22120+ .setattr = aufs_setattr,
22121+ .getattr = aufs_getattr,
0c3ec466 22122+
c1595e42 22123+#ifdef CONFIG_AUFS_XATTR
b95c5147 22124+ .listxattr = aufs_listxattr,
c1595e42
JR
22125+#endif
22126+
febd17d6 22127+ .get_link = aufs_get_link,
0c3ec466 22128+
b95c5147
AM
22129+ /* .update_time = aufs_update_time */
22130+ },
22131+ [AuIop_DIR] = {
22132+ .create = aufs_create,
22133+ .lookup = aufs_lookup,
22134+ .link = aufs_link,
22135+ .unlink = aufs_unlink,
22136+ .symlink = aufs_symlink,
22137+ .mkdir = aufs_mkdir,
22138+ .rmdir = aufs_rmdir,
22139+ .mknod = aufs_mknod,
22140+ .rename = aufs_rename,
22141+
22142+ .permission = aufs_permission,
c1595e42 22143+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
22144+ .get_acl = aufs_get_acl,
22145+ .set_acl = aufs_set_acl,
c1595e42
JR
22146+#endif
22147+
b95c5147
AM
22148+ .setattr = aufs_setattr,
22149+ .getattr = aufs_getattr,
0c3ec466 22150+
c1595e42 22151+#ifdef CONFIG_AUFS_XATTR
b95c5147 22152+ .listxattr = aufs_listxattr,
c1595e42
JR
22153+#endif
22154+
b95c5147
AM
22155+ .update_time = aufs_update_time,
22156+ .atomic_open = aufs_atomic_open,
22157+ .tmpfile = aufs_tmpfile
22158+ },
22159+ [AuIop_OTHER] = {
22160+ .permission = aufs_permission,
c1595e42 22161+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
22162+ .get_acl = aufs_get_acl,
22163+ .set_acl = aufs_set_acl,
c1595e42
JR
22164+#endif
22165+
b95c5147
AM
22166+ .setattr = aufs_setattr,
22167+ .getattr = aufs_getattr,
0c3ec466 22168+
c1595e42 22169+#ifdef CONFIG_AUFS_XATTR
b95c5147 22170+ .listxattr = aufs_listxattr,
c1595e42
JR
22171+#endif
22172+
b95c5147
AM
22173+ .update_time = aufs_update_time
22174+ }
4a4d8108 22175+};
7f207e10
AM
22176diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
22177--- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 22178+++ linux/fs/aufs/i_op_del.c 2019-03-05 12:13:00.139224339 +0100
062440b3 22179@@ -0,0 +1,512 @@
cd7a4cd9 22180+// SPDX-License-Identifier: GPL-2.0
1facf9fc 22181+/*
ba1aed25 22182+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 22183+ *
22184+ * This program, aufs is free software; you can redistribute it and/or modify
22185+ * it under the terms of the GNU General Public License as published by
22186+ * the Free Software Foundation; either version 2 of the License, or
22187+ * (at your option) any later version.
dece6358
AM
22188+ *
22189+ * This program is distributed in the hope that it will be useful,
22190+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22191+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22192+ * GNU General Public License for more details.
22193+ *
22194+ * You should have received a copy of the GNU General Public License
523b37e3 22195+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 22196+ */
22197+
22198+/*
4a4d8108 22199+ * inode operations (del entry)
1308ab2a 22200+ */
dece6358 22201+
1308ab2a 22202+#include "aufs.h"
dece6358 22203+
4a4d8108
AM
22204+/*
22205+ * decide if a new whiteout for @dentry is necessary or not.
22206+ * when it is necessary, prepare the parent dir for the upper branch whose
22207+ * branch index is @bcpup for creation. the actual creation of the whiteout will
22208+ * be done by caller.
22209+ * return value:
22210+ * 0: wh is unnecessary
22211+ * plus: wh is necessary
22212+ * minus: error
22213+ */
22214+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
1308ab2a 22215+{
4a4d8108 22216+ int need_wh, err;
5afbbe0d 22217+ aufs_bindex_t btop;
4a4d8108 22218+ struct super_block *sb;
dece6358 22219+
4a4d8108 22220+ sb = dentry->d_sb;
5afbbe0d 22221+ btop = au_dbtop(dentry);
4a4d8108 22222+ if (*bcpup < 0) {
5afbbe0d
AM
22223+ *bcpup = btop;
22224+ if (au_test_ro(sb, btop, d_inode(dentry))) {
4a4d8108
AM
22225+ err = AuWbrCopyup(au_sbi(sb), dentry);
22226+ *bcpup = err;
22227+ if (unlikely(err < 0))
22228+ goto out;
22229+ }
22230+ } else
5afbbe0d 22231+ AuDebugOn(btop < *bcpup
5527c038 22232+ || au_test_ro(sb, *bcpup, d_inode(dentry)));
5afbbe0d 22233+ AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
1308ab2a 22234+
5afbbe0d 22235+ if (*bcpup != btop) {
4a4d8108
AM
22236+ err = au_cpup_dirs(dentry, *bcpup);
22237+ if (unlikely(err))
22238+ goto out;
22239+ need_wh = 1;
22240+ } else {
027c5e7a 22241+ struct au_dinfo *dinfo, *tmp;
4a4d8108 22242+
027c5e7a
AM
22243+ need_wh = -ENOMEM;
22244+ dinfo = au_di(dentry);
22245+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
22246+ if (tmp) {
22247+ au_di_cp(tmp, dinfo);
22248+ au_di_swap(tmp, dinfo);
22249+ /* returns the number of positive dentries */
5afbbe0d
AM
22250+ need_wh = au_lkup_dentry(dentry, btop + 1,
22251+ /* AuLkup_IGNORE_PERM */ 0);
027c5e7a
AM
22252+ au_di_swap(tmp, dinfo);
22253+ au_rw_write_unlock(&tmp->di_rwsem);
22254+ au_di_free(tmp);
4a4d8108
AM
22255+ }
22256+ }
22257+ AuDbg("need_wh %d\n", need_wh);
22258+ err = need_wh;
22259+
4f0767ce 22260+out:
4a4d8108 22261+ return err;
1facf9fc 22262+}
22263+
4a4d8108
AM
22264+/*
22265+ * simple tests for the del-entry operations.
22266+ * following the checks in vfs, plus the parent-child relationship.
22267+ */
22268+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
22269+ struct dentry *h_parent, int isdir)
1facf9fc 22270+{
4a4d8108
AM
22271+ int err;
22272+ umode_t h_mode;
22273+ struct dentry *h_dentry, *h_latest;
1308ab2a 22274+ struct inode *h_inode;
1facf9fc 22275+
4a4d8108 22276+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 22277+ if (d_really_is_positive(dentry)) {
4a4d8108 22278+ err = -ENOENT;
5527c038
JR
22279+ if (unlikely(d_is_negative(h_dentry)))
22280+ goto out;
22281+ h_inode = d_inode(h_dentry);
22282+ if (unlikely(!h_inode->i_nlink))
4a4d8108 22283+ goto out;
1facf9fc 22284+
4a4d8108
AM
22285+ h_mode = h_inode->i_mode;
22286+ if (!isdir) {
22287+ err = -EISDIR;
22288+ if (unlikely(S_ISDIR(h_mode)))
22289+ goto out;
22290+ } else if (unlikely(!S_ISDIR(h_mode))) {
22291+ err = -ENOTDIR;
22292+ goto out;
22293+ }
22294+ } else {
22295+ /* rename(2) case */
22296+ err = -EIO;
5527c038 22297+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
22298+ goto out;
22299+ }
1facf9fc 22300+
4a4d8108
AM
22301+ err = -ENOENT;
22302+ /* expected parent dir is locked */
22303+ if (unlikely(h_parent != h_dentry->d_parent))
22304+ goto out;
22305+ err = 0;
22306+
22307+ /*
22308+ * rmdir a dir may break the consistency on some filesystem.
22309+ * let's try heavy test.
22310+ */
22311+ err = -EACCES;
076b876e 22312+ if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
5527c038 22313+ && au_test_h_perm(d_inode(h_parent),
076b876e 22314+ MAY_EXEC | MAY_WRITE)))
4a4d8108
AM
22315+ goto out;
22316+
076b876e 22317+ h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
4a4d8108
AM
22318+ err = -EIO;
22319+ if (IS_ERR(h_latest))
22320+ goto out;
22321+ if (h_latest == h_dentry)
22322+ err = 0;
22323+ dput(h_latest);
22324+
4f0767ce 22325+out:
4a4d8108 22326+ return err;
1308ab2a 22327+}
1facf9fc 22328+
4a4d8108
AM
22329+/*
22330+ * decide the branch where we operate for @dentry. the branch index will be set
acd2b654 22331+ * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
4a4d8108
AM
22332+ * dir for reverting.
22333+ * when a new whiteout is necessary, create it.
22334+ */
22335+static struct dentry*
22336+lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
22337+ struct au_dtime *dt, struct au_pin *pin)
1308ab2a 22338+{
4a4d8108
AM
22339+ struct dentry *wh_dentry;
22340+ struct super_block *sb;
22341+ struct path h_path;
22342+ int err, need_wh;
22343+ unsigned int udba;
22344+ aufs_bindex_t bcpup;
dece6358 22345+
4a4d8108
AM
22346+ need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
22347+ wh_dentry = ERR_PTR(need_wh);
22348+ if (unlikely(need_wh < 0))
22349+ goto out;
22350+
22351+ sb = dentry->d_sb;
22352+ udba = au_opt_udba(sb);
22353+ bcpup = *rbcpup;
22354+ err = au_pin(pin, dentry, bcpup, udba,
22355+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22356+ wh_dentry = ERR_PTR(err);
22357+ if (unlikely(err))
22358+ goto out;
22359+
22360+ h_path.dentry = au_pinned_h_parent(pin);
22361+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 22362+ && au_dbtop(dentry) == bcpup) {
4a4d8108
AM
22363+ err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
22364+ wh_dentry = ERR_PTR(err);
22365+ if (unlikely(err))
22366+ goto out_unpin;
22367+ }
22368+
22369+ h_path.mnt = au_sbr_mnt(sb, bcpup);
22370+ au_dtime_store(dt, au_pinned_parent(pin), &h_path);
22371+ wh_dentry = NULL;
22372+ if (!need_wh)
22373+ goto out; /* success, no need to create whiteout */
22374+
22375+ wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
22376+ if (IS_ERR(wh_dentry))
22377+ goto out_unpin;
22378+
22379+ /* returns with the parent is locked and wh_dentry is dget-ed */
22380+ goto out; /* success */
22381+
4f0767ce 22382+out_unpin:
4a4d8108 22383+ au_unpin(pin);
4f0767ce 22384+out:
4a4d8108 22385+ return wh_dentry;
1facf9fc 22386+}
22387+
4a4d8108
AM
22388+/*
22389+ * when removing a dir, rename it to a unique temporary whiteout-ed name first
22390+ * in order to be revertible and save time for removing many child whiteouts
22391+ * under the dir.
22392+ * returns 1 when there are too many child whiteout and caller should remove
22393+ * them asynchronously. returns 0 when the number of children is enough small to
22394+ * remove now or the branch fs is a remote fs.
22395+ * otherwise return an error.
22396+ */
22397+static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
22398+ struct au_nhash *whlist, struct inode *dir)
1facf9fc 22399+{
4a4d8108
AM
22400+ int rmdir_later, err, dirwh;
22401+ struct dentry *h_dentry;
22402+ struct super_block *sb;
5527c038 22403+ struct inode *inode;
4a4d8108
AM
22404+
22405+ sb = dentry->d_sb;
22406+ SiMustAnyLock(sb);
22407+ h_dentry = au_h_dptr(dentry, bindex);
22408+ err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
22409+ if (unlikely(err))
22410+ goto out;
22411+
22412+ /* stop monitoring */
5527c038
JR
22413+ inode = d_inode(dentry);
22414+ au_hn_free(au_hi(inode, bindex));
4a4d8108
AM
22415+
22416+ if (!au_test_fs_remote(h_dentry->d_sb)) {
22417+ dirwh = au_sbi(sb)->si_dirwh;
22418+ rmdir_later = (dirwh <= 1);
22419+ if (!rmdir_later)
22420+ rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
22421+ dirwh);
22422+ if (rmdir_later)
22423+ return rmdir_later;
22424+ }
1facf9fc 22425+
4a4d8108
AM
22426+ err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
22427+ if (unlikely(err)) {
523b37e3
AM
22428+ AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
22429+ h_dentry, bindex, err);
4a4d8108
AM
22430+ err = 0;
22431+ }
dece6358 22432+
4f0767ce 22433+out:
4a4d8108
AM
22434+ AuTraceErr(err);
22435+ return err;
22436+}
1308ab2a 22437+
4a4d8108
AM
22438+/*
22439+ * final procedure for deleting a entry.
22440+ * maintain dentry and iattr.
22441+ */
22442+static void epilog(struct inode *dir, struct dentry *dentry,
22443+ aufs_bindex_t bindex)
22444+{
22445+ struct inode *inode;
1308ab2a 22446+
5527c038 22447+ inode = d_inode(dentry);
4a4d8108
AM
22448+ d_drop(dentry);
22449+ inode->i_ctime = dir->i_ctime;
1308ab2a 22450+
b912730e 22451+ au_dir_ts(dir, bindex);
be118d29 22452+ inode_inc_iversion(dir);
1facf9fc 22453+}
22454+
4a4d8108
AM
22455+/*
22456+ * when an error happened, remove the created whiteout and revert everything.
22457+ */
7f207e10
AM
22458+static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
22459+ aufs_bindex_t bwh, struct dentry *wh_dentry,
22460+ struct dentry *dentry, struct au_dtime *dt)
1facf9fc 22461+{
4a4d8108
AM
22462+ int rerr;
22463+ struct path h_path = {
22464+ .dentry = wh_dentry,
7f207e10 22465+ .mnt = au_sbr_mnt(dir->i_sb, bindex)
4a4d8108 22466+ };
dece6358 22467+
7f207e10 22468+ rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
4a4d8108
AM
22469+ if (!rerr) {
22470+ au_set_dbwh(dentry, bwh);
22471+ au_dtime_revert(dt);
22472+ return 0;
22473+ }
dece6358 22474+
523b37e3 22475+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
4a4d8108 22476+ return -EIO;
1facf9fc 22477+}
22478+
4a4d8108 22479+/* ---------------------------------------------------------------------- */
1facf9fc 22480+
4a4d8108 22481+int aufs_unlink(struct inode *dir, struct dentry *dentry)
1308ab2a 22482+{
4a4d8108 22483+ int err;
5afbbe0d 22484+ aufs_bindex_t bwh, bindex, btop;
523b37e3 22485+ struct inode *inode, *h_dir, *delegated;
4a4d8108 22486+ struct dentry *parent, *wh_dentry;
acd2b654 22487+ /* to reduce stack size */
c2b27bf2
AM
22488+ struct {
22489+ struct au_dtime dt;
22490+ struct au_pin pin;
22491+ struct path h_path;
22492+ } *a;
1facf9fc 22493+
4a4d8108 22494+ IMustLock(dir);
027c5e7a 22495+
c2b27bf2
AM
22496+ err = -ENOMEM;
22497+ a = kmalloc(sizeof(*a), GFP_NOFS);
22498+ if (unlikely(!a))
22499+ goto out;
22500+
027c5e7a
AM
22501+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
22502+ if (unlikely(err))
c2b27bf2 22503+ goto out_free;
027c5e7a
AM
22504+ err = au_d_hashed_positive(dentry);
22505+ if (unlikely(err))
22506+ goto out_unlock;
5527c038 22507+ inode = d_inode(dentry);
4a4d8108 22508+ IMustLock(inode);
027c5e7a 22509+ err = -EISDIR;
2000de60 22510+ if (unlikely(d_is_dir(dentry)))
027c5e7a 22511+ goto out_unlock; /* possible? */
1facf9fc 22512+
5afbbe0d 22513+ btop = au_dbtop(dentry);
4a4d8108
AM
22514+ bwh = au_dbwh(dentry);
22515+ bindex = -1;
027c5e7a
AM
22516+ parent = dentry->d_parent; /* dir inode is locked */
22517+ di_write_lock_parent(parent);
c2b27bf2
AM
22518+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
22519+ &a->pin);
4a4d8108
AM
22520+ err = PTR_ERR(wh_dentry);
22521+ if (IS_ERR(wh_dentry))
027c5e7a 22522+ goto out_parent;
1facf9fc 22523+
5afbbe0d
AM
22524+ a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
22525+ a->h_path.dentry = au_h_dptr(dentry, btop);
c2b27bf2 22526+ dget(a->h_path.dentry);
5afbbe0d 22527+ if (bindex == btop) {
c2b27bf2 22528+ h_dir = au_pinned_h_dir(&a->pin);
523b37e3
AM
22529+ delegated = NULL;
22530+ err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
22531+ if (unlikely(err == -EWOULDBLOCK)) {
22532+ pr_warn("cannot retry for NFSv4 delegation"
22533+ " for an internal unlink\n");
22534+ iput(delegated);
22535+ }
4a4d8108
AM
22536+ } else {
22537+ /* dir inode is locked */
5527c038 22538+ h_dir = d_inode(wh_dentry->d_parent);
4a4d8108
AM
22539+ IMustLock(h_dir);
22540+ err = 0;
22541+ }
dece6358 22542+
4a4d8108 22543+ if (!err) {
7f207e10 22544+ vfsub_drop_nlink(inode);
4a4d8108
AM
22545+ epilog(dir, dentry, bindex);
22546+
22547+ /* update target timestamps */
5afbbe0d 22548+ if (bindex == btop) {
c2b27bf2
AM
22549+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
22550+ /*ignore*/
5527c038 22551+ inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
4a4d8108
AM
22552+ } else
22553+ /* todo: this timestamp may be reverted later */
22554+ inode->i_ctime = h_dir->i_ctime;
027c5e7a 22555+ goto out_unpin; /* success */
1facf9fc 22556+ }
22557+
4a4d8108
AM
22558+ /* revert */
22559+ if (wh_dentry) {
22560+ int rerr;
22561+
c2b27bf2
AM
22562+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22563+ &a->dt);
4a4d8108
AM
22564+ if (rerr)
22565+ err = rerr;
dece6358 22566+ }
1facf9fc 22567+
027c5e7a 22568+out_unpin:
c2b27bf2 22569+ au_unpin(&a->pin);
4a4d8108 22570+ dput(wh_dentry);
c2b27bf2 22571+ dput(a->h_path.dentry);
027c5e7a 22572+out_parent:
4a4d8108 22573+ di_write_unlock(parent);
027c5e7a 22574+out_unlock:
4a4d8108 22575+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 22576+out_free:
9f237c51 22577+ au_kfree_rcu(a);
027c5e7a 22578+out:
4a4d8108 22579+ return err;
dece6358
AM
22580+}
22581+
4a4d8108 22582+int aufs_rmdir(struct inode *dir, struct dentry *dentry)
1308ab2a 22583+{
4a4d8108 22584+ int err, rmdir_later;
5afbbe0d 22585+ aufs_bindex_t bwh, bindex, btop;
4a4d8108
AM
22586+ struct inode *inode;
22587+ struct dentry *parent, *wh_dentry, *h_dentry;
22588+ struct au_whtmp_rmdir *args;
acd2b654 22589+ /* to reduce stack size */
c2b27bf2
AM
22590+ struct {
22591+ struct au_dtime dt;
22592+ struct au_pin pin;
22593+ } *a;
1facf9fc 22594+
4a4d8108 22595+ IMustLock(dir);
027c5e7a 22596+
c2b27bf2
AM
22597+ err = -ENOMEM;
22598+ a = kmalloc(sizeof(*a), GFP_NOFS);
22599+ if (unlikely(!a))
22600+ goto out;
22601+
027c5e7a
AM
22602+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22603+ if (unlikely(err))
c2b27bf2 22604+ goto out_free;
53392da6
AM
22605+ err = au_alive_dir(dentry);
22606+ if (unlikely(err))
027c5e7a 22607+ goto out_unlock;
5527c038 22608+ inode = d_inode(dentry);
4a4d8108 22609+ IMustLock(inode);
027c5e7a 22610+ err = -ENOTDIR;
2000de60 22611+ if (unlikely(!d_is_dir(dentry)))
027c5e7a 22612+ goto out_unlock; /* possible? */
dece6358 22613+
4a4d8108
AM
22614+ err = -ENOMEM;
22615+ args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
22616+ if (unlikely(!args))
22617+ goto out_unlock;
dece6358 22618+
4a4d8108
AM
22619+ parent = dentry->d_parent; /* dir inode is locked */
22620+ di_write_lock_parent(parent);
22621+ err = au_test_empty(dentry, &args->whlist);
22622+ if (unlikely(err))
027c5e7a 22623+ goto out_parent;
1facf9fc 22624+
5afbbe0d 22625+ btop = au_dbtop(dentry);
4a4d8108
AM
22626+ bwh = au_dbwh(dentry);
22627+ bindex = -1;
c2b27bf2
AM
22628+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
22629+ &a->pin);
4a4d8108
AM
22630+ err = PTR_ERR(wh_dentry);
22631+ if (IS_ERR(wh_dentry))
027c5e7a 22632+ goto out_parent;
1facf9fc 22633+
5afbbe0d 22634+ h_dentry = au_h_dptr(dentry, btop);
4a4d8108
AM
22635+ dget(h_dentry);
22636+ rmdir_later = 0;
5afbbe0d
AM
22637+ if (bindex == btop) {
22638+ err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
4a4d8108
AM
22639+ if (err > 0) {
22640+ rmdir_later = err;
22641+ err = 0;
22642+ }
22643+ } else {
22644+ /* stop monitoring */
5afbbe0d 22645+ au_hn_free(au_hi(inode, btop));
4a4d8108
AM
22646+
22647+ /* dir inode is locked */
5527c038 22648+ IMustLock(d_inode(wh_dentry->d_parent));
1facf9fc 22649+ err = 0;
22650+ }
22651+
4a4d8108 22652+ if (!err) {
027c5e7a 22653+ vfsub_dead_dir(inode);
4a4d8108
AM
22654+ au_set_dbdiropq(dentry, -1);
22655+ epilog(dir, dentry, bindex);
1308ab2a 22656+
4a4d8108 22657+ if (rmdir_later) {
5afbbe0d 22658+ au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
4a4d8108
AM
22659+ args = NULL;
22660+ }
1308ab2a 22661+
4a4d8108 22662+ goto out_unpin; /* success */
1facf9fc 22663+ }
22664+
4a4d8108
AM
22665+ /* revert */
22666+ AuLabel(revert);
22667+ if (wh_dentry) {
22668+ int rerr;
1308ab2a 22669+
c2b27bf2
AM
22670+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22671+ &a->dt);
4a4d8108
AM
22672+ if (rerr)
22673+ err = rerr;
1facf9fc 22674+ }
22675+
4f0767ce 22676+out_unpin:
c2b27bf2 22677+ au_unpin(&a->pin);
4a4d8108
AM
22678+ dput(wh_dentry);
22679+ dput(h_dentry);
027c5e7a 22680+out_parent:
4a4d8108
AM
22681+ di_write_unlock(parent);
22682+ if (args)
22683+ au_whtmp_rmdir_free(args);
4f0767ce 22684+out_unlock:
4a4d8108 22685+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 22686+out_free:
9f237c51 22687+ au_kfree_rcu(a);
4f0767ce 22688+out:
4a4d8108
AM
22689+ AuTraceErr(err);
22690+ return err;
dece6358 22691+}
7f207e10
AM
22692diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
22693--- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 22694+++ linux/fs/aufs/i_op_ren.c 2019-03-05 12:13:00.139224339 +0100
acd2b654 22695@@ -0,0 +1,1249 @@
cd7a4cd9 22696+// SPDX-License-Identifier: GPL-2.0
1facf9fc 22697+/*
ba1aed25 22698+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 22699+ *
22700+ * This program, aufs is free software; you can redistribute it and/or modify
22701+ * it under the terms of the GNU General Public License as published by
22702+ * the Free Software Foundation; either version 2 of the License, or
22703+ * (at your option) any later version.
dece6358
AM
22704+ *
22705+ * This program is distributed in the hope that it will be useful,
22706+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22707+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22708+ * GNU General Public License for more details.
22709+ *
22710+ * You should have received a copy of the GNU General Public License
523b37e3 22711+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 22712+ */
22713+
22714+/*
4a4d8108
AM
22715+ * inode operation (rename entry)
22716+ * todo: this is crazy monster
1facf9fc 22717+ */
22718+
22719+#include "aufs.h"
22720+
4a4d8108
AM
22721+enum { AuSRC, AuDST, AuSrcDst };
22722+enum { AuPARENT, AuCHILD, AuParentChild };
1facf9fc 22723+
f2c43d5f
AM
22724+#define AuRen_ISDIR_SRC 1
22725+#define AuRen_ISDIR_DST (1 << 1)
22726+#define AuRen_ISSAMEDIR (1 << 2)
22727+#define AuRen_WHSRC (1 << 3)
22728+#define AuRen_WHDST (1 << 4)
22729+#define AuRen_MNT_WRITE (1 << 5)
22730+#define AuRen_DT_DSTDIR (1 << 6)
22731+#define AuRen_DIROPQ_SRC (1 << 7)
22732+#define AuRen_DIROPQ_DST (1 << 8)
8b6a4947
AM
22733+#define AuRen_DIRREN (1 << 9)
22734+#define AuRen_DROPPED_SRC (1 << 10)
22735+#define AuRen_DROPPED_DST (1 << 11)
4a4d8108 22736+#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
7f207e10
AM
22737+#define au_fset_ren(flags, name) \
22738+ do { (flags) |= AuRen_##name; } while (0)
22739+#define au_fclr_ren(flags, name) \
22740+ do { (flags) &= ~AuRen_##name; } while (0)
1facf9fc 22741+
8b6a4947
AM
22742+#ifndef CONFIG_AUFS_DIRREN
22743+#undef AuRen_DIRREN
22744+#define AuRen_DIRREN 0
22745+#endif
22746+
4a4d8108
AM
22747+struct au_ren_args {
22748+ struct {
22749+ struct dentry *dentry, *h_dentry, *parent, *h_parent,
22750+ *wh_dentry;
22751+ struct inode *dir, *inode;
f2c43d5f 22752+ struct au_hinode *hdir, *hinode;
4a4d8108 22753+ struct au_dtime dt[AuParentChild];
f2c43d5f 22754+ aufs_bindex_t btop, bdiropq;
4a4d8108 22755+ } sd[AuSrcDst];
1facf9fc 22756+
4a4d8108
AM
22757+#define src_dentry sd[AuSRC].dentry
22758+#define src_dir sd[AuSRC].dir
22759+#define src_inode sd[AuSRC].inode
22760+#define src_h_dentry sd[AuSRC].h_dentry
22761+#define src_parent sd[AuSRC].parent
22762+#define src_h_parent sd[AuSRC].h_parent
22763+#define src_wh_dentry sd[AuSRC].wh_dentry
22764+#define src_hdir sd[AuSRC].hdir
f2c43d5f 22765+#define src_hinode sd[AuSRC].hinode
4a4d8108
AM
22766+#define src_h_dir sd[AuSRC].hdir->hi_inode
22767+#define src_dt sd[AuSRC].dt
5afbbe0d 22768+#define src_btop sd[AuSRC].btop
f2c43d5f 22769+#define src_bdiropq sd[AuSRC].bdiropq
1facf9fc 22770+
4a4d8108
AM
22771+#define dst_dentry sd[AuDST].dentry
22772+#define dst_dir sd[AuDST].dir
22773+#define dst_inode sd[AuDST].inode
22774+#define dst_h_dentry sd[AuDST].h_dentry
22775+#define dst_parent sd[AuDST].parent
22776+#define dst_h_parent sd[AuDST].h_parent
22777+#define dst_wh_dentry sd[AuDST].wh_dentry
22778+#define dst_hdir sd[AuDST].hdir
f2c43d5f 22779+#define dst_hinode sd[AuDST].hinode
4a4d8108
AM
22780+#define dst_h_dir sd[AuDST].hdir->hi_inode
22781+#define dst_dt sd[AuDST].dt
5afbbe0d 22782+#define dst_btop sd[AuDST].btop
f2c43d5f 22783+#define dst_bdiropq sd[AuDST].bdiropq
4a4d8108
AM
22784+
22785+ struct dentry *h_trap;
22786+ struct au_branch *br;
4a4d8108
AM
22787+ struct path h_path;
22788+ struct au_nhash whlist;
f2c43d5f 22789+ aufs_bindex_t btgt, src_bwh;
1facf9fc 22790+
f2c43d5f
AM
22791+ struct {
22792+ unsigned short auren_flags;
22793+ unsigned char flags; /* syscall parameter */
22794+ unsigned char exchange;
22795+ } __packed;
1facf9fc 22796+
4a4d8108
AM
22797+ struct au_whtmp_rmdir *thargs;
22798+ struct dentry *h_dst;
8b6a4947 22799+ struct au_hinode *h_root;
4a4d8108 22800+};
1308ab2a 22801+
4a4d8108 22802+/* ---------------------------------------------------------------------- */
1308ab2a 22803+
4a4d8108
AM
22804+/*
22805+ * functions for reverting.
22806+ * when an error happened in a single rename systemcall, we should revert
79b8bda9 22807+ * everything as if nothing happened.
4a4d8108
AM
22808+ * we don't need to revert the copied-up/down the parent dir since they are
22809+ * harmless.
22810+ */
1facf9fc 22811+
4a4d8108
AM
22812+#define RevertFailure(fmt, ...) do { \
22813+ AuIOErr("revert failure: " fmt " (%d, %d)\n", \
22814+ ##__VA_ARGS__, err, rerr); \
22815+ err = -EIO; \
22816+} while (0)
1facf9fc 22817+
f2c43d5f 22818+static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
1facf9fc 22819+{
4a4d8108 22820+ int rerr;
f2c43d5f
AM
22821+ struct dentry *d;
22822+#define src_or_dst(member) a->sd[idx].member
1facf9fc 22823+
f2c43d5f
AM
22824+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22825+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22826+ rerr = au_diropq_remove(d, a->btgt);
22827+ au_hn_inode_unlock(src_or_dst(hinode));
22828+ au_set_dbdiropq(d, src_or_dst(bdiropq));
4a4d8108 22829+ if (rerr)
f2c43d5f
AM
22830+ RevertFailure("remove diropq %pd", d);
22831+
22832+#undef src_or_dst_
22833+}
22834+
22835+static void au_ren_rev_diropq(int err, struct au_ren_args *a)
22836+{
22837+ if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
22838+ au_ren_do_rev_diropq(err, a, AuSRC);
22839+ if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
22840+ au_ren_do_rev_diropq(err, a, AuDST);
4a4d8108 22841+}
1facf9fc 22842+
4a4d8108
AM
22843+static void au_ren_rev_rename(int err, struct au_ren_args *a)
22844+{
22845+ int rerr;
523b37e3 22846+ struct inode *delegated;
1facf9fc 22847+
b4510431
AM
22848+ a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
22849+ a->src_h_parent);
4a4d8108
AM
22850+ rerr = PTR_ERR(a->h_path.dentry);
22851+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 22852+ RevertFailure("lkup one %pd", a->src_dentry);
4a4d8108 22853+ return;
1facf9fc 22854+ }
22855+
523b37e3 22856+ delegated = NULL;
4a4d8108
AM
22857+ rerr = vfsub_rename(a->dst_h_dir,
22858+ au_h_dptr(a->src_dentry, a->btgt),
f2c43d5f 22859+ a->src_h_dir, &a->h_path, &delegated, a->flags);
523b37e3
AM
22860+ if (unlikely(rerr == -EWOULDBLOCK)) {
22861+ pr_warn("cannot retry for NFSv4 delegation"
22862+ " for an internal rename\n");
22863+ iput(delegated);
22864+ }
4a4d8108
AM
22865+ d_drop(a->h_path.dentry);
22866+ dput(a->h_path.dentry);
22867+ /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
22868+ if (rerr)
523b37e3 22869+ RevertFailure("rename %pd", a->src_dentry);
1facf9fc 22870+}
22871+
4a4d8108 22872+static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
1facf9fc 22873+{
4a4d8108 22874+ int rerr;
523b37e3 22875+ struct inode *delegated;
dece6358 22876+
b4510431
AM
22877+ a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
22878+ a->dst_h_parent);
4a4d8108
AM
22879+ rerr = PTR_ERR(a->h_path.dentry);
22880+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 22881+ RevertFailure("lkup one %pd", a->dst_dentry);
4a4d8108
AM
22882+ return;
22883+ }
5527c038 22884+ if (d_is_positive(a->h_path.dentry)) {
4a4d8108
AM
22885+ d_drop(a->h_path.dentry);
22886+ dput(a->h_path.dentry);
22887+ return;
dece6358
AM
22888+ }
22889+
523b37e3
AM
22890+ delegated = NULL;
22891+ rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
f2c43d5f 22892+ &delegated, a->flags);
523b37e3
AM
22893+ if (unlikely(rerr == -EWOULDBLOCK)) {
22894+ pr_warn("cannot retry for NFSv4 delegation"
22895+ " for an internal rename\n");
22896+ iput(delegated);
22897+ }
4a4d8108
AM
22898+ d_drop(a->h_path.dentry);
22899+ dput(a->h_path.dentry);
22900+ if (!rerr)
22901+ au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
22902+ else
523b37e3 22903+ RevertFailure("rename %pd", a->h_dst);
4a4d8108 22904+}
1308ab2a 22905+
4a4d8108
AM
22906+static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
22907+{
22908+ int rerr;
1308ab2a 22909+
4a4d8108
AM
22910+ a->h_path.dentry = a->src_wh_dentry;
22911+ rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
027c5e7a 22912+ au_set_dbwh(a->src_dentry, a->src_bwh);
4a4d8108 22913+ if (rerr)
523b37e3 22914+ RevertFailure("unlink %pd", a->src_wh_dentry);
4a4d8108 22915+}
4a4d8108 22916+#undef RevertFailure
1facf9fc 22917+
1308ab2a 22918+/* ---------------------------------------------------------------------- */
22919+
4a4d8108
AM
22920+/*
22921+ * when we have to copyup the renaming entry, do it with the rename-target name
22922+ * in order to minimize the cost (the later actual rename is unnecessary).
22923+ * otherwise rename it on the target branch.
22924+ */
22925+static int au_ren_or_cpup(struct au_ren_args *a)
1facf9fc 22926+{
dece6358 22927+ int err;
4a4d8108 22928+ struct dentry *d;
523b37e3 22929+ struct inode *delegated;
1facf9fc 22930+
4a4d8108 22931+ d = a->src_dentry;
5afbbe0d 22932+ if (au_dbtop(d) == a->btgt) {
4a4d8108 22933+ a->h_path.dentry = a->dst_h_dentry;
5afbbe0d 22934+ AuDebugOn(au_dbtop(d) != a->btgt);
523b37e3 22935+ delegated = NULL;
4a4d8108 22936+ err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
f2c43d5f
AM
22937+ a->dst_h_dir, &a->h_path, &delegated,
22938+ a->flags);
523b37e3
AM
22939+ if (unlikely(err == -EWOULDBLOCK)) {
22940+ pr_warn("cannot retry for NFSv4 delegation"
22941+ " for an internal rename\n");
22942+ iput(delegated);
22943+ }
c2b27bf2 22944+ } else
86dc4139 22945+ BUG();
1308ab2a 22946+
027c5e7a
AM
22947+ if (!err && a->h_dst)
22948+ /* it will be set to dinfo later */
22949+ dget(a->h_dst);
1facf9fc 22950+
dece6358
AM
22951+ return err;
22952+}
1facf9fc 22953+
4a4d8108
AM
22954+/* cf. aufs_rmdir() */
22955+static int au_ren_del_whtmp(struct au_ren_args *a)
dece6358 22956+{
4a4d8108
AM
22957+ int err;
22958+ struct inode *dir;
1facf9fc 22959+
4a4d8108
AM
22960+ dir = a->dst_dir;
22961+ SiMustAnyLock(dir->i_sb);
22962+ if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
22963+ au_sbi(dir->i_sb)->si_dirwh)
22964+ || au_test_fs_remote(a->h_dst->d_sb)) {
22965+ err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
22966+ if (unlikely(err))
523b37e3
AM
22967+ pr_warn("failed removing whtmp dir %pd (%d), "
22968+ "ignored.\n", a->h_dst, err);
4a4d8108
AM
22969+ } else {
22970+ au_nhash_wh_free(&a->thargs->whlist);
22971+ a->thargs->whlist = a->whlist;
22972+ a->whlist.nh_num = 0;
22973+ au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
22974+ dput(a->h_dst);
22975+ a->thargs = NULL;
22976+ }
22977+
22978+ return 0;
1308ab2a 22979+}
1facf9fc 22980+
4a4d8108 22981+/* make it 'opaque' dir. */
f2c43d5f 22982+static int au_ren_do_diropq(struct au_ren_args *a, int idx)
4a4d8108
AM
22983+{
22984+ int err;
f2c43d5f
AM
22985+ struct dentry *d, *diropq;
22986+#define src_or_dst(member) a->sd[idx].member
1facf9fc 22987+
4a4d8108 22988+ err = 0;
f2c43d5f
AM
22989+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22990+ src_or_dst(bdiropq) = au_dbdiropq(d);
22991+ src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
22992+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22993+ diropq = au_diropq_create(d, a->btgt);
22994+ au_hn_inode_unlock(src_or_dst(hinode));
4a4d8108
AM
22995+ if (IS_ERR(diropq))
22996+ err = PTR_ERR(diropq);
076b876e
AM
22997+ else
22998+ dput(diropq);
1facf9fc 22999+
f2c43d5f 23000+#undef src_or_dst_
4a4d8108
AM
23001+ return err;
23002+}
1facf9fc 23003+
f2c43d5f 23004+static int au_ren_diropq(struct au_ren_args *a)
4a4d8108
AM
23005+{
23006+ int err;
f2c43d5f
AM
23007+ unsigned char always;
23008+ struct dentry *d;
1facf9fc 23009+
f2c43d5f
AM
23010+ err = 0;
23011+ d = a->dst_dentry; /* already renamed on the branch */
23012+ always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
23013+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
8b6a4947 23014+ && !au_ftest_ren(a->auren_flags, DIRREN)
f2c43d5f
AM
23015+ && a->btgt != au_dbdiropq(a->src_dentry)
23016+ && (a->dst_wh_dentry
23017+ || a->btgt <= au_dbdiropq(d)
23018+ /* hide the lower to keep xino */
23019+ /* the lowers may not be a dir, but we hide them anyway */
23020+ || a->btgt < au_dbbot(d)
23021+ || always)) {
23022+ AuDbg("here\n");
23023+ err = au_ren_do_diropq(a, AuSRC);
23024+ if (unlikely(err))
4a4d8108 23025+ goto out;
f2c43d5f 23026+ au_fset_ren(a->auren_flags, DIROPQ_SRC);
4a4d8108 23027+ }
f2c43d5f
AM
23028+ if (!a->exchange)
23029+ goto out; /* success */
1facf9fc 23030+
f2c43d5f
AM
23031+ d = a->src_dentry; /* already renamed on the branch */
23032+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
23033+ && a->btgt != au_dbdiropq(a->dst_dentry)
23034+ && (a->btgt < au_dbdiropq(d)
23035+ || a->btgt < au_dbbot(d)
23036+ || always)) {
23037+ AuDbgDentry(a->src_dentry);
23038+ AuDbgDentry(a->dst_dentry);
23039+ err = au_ren_do_diropq(a, AuDST);
4a4d8108 23040+ if (unlikely(err))
f2c43d5f
AM
23041+ goto out_rev_src;
23042+ au_fset_ren(a->auren_flags, DIROPQ_DST);
23043+ }
23044+ goto out; /* success */
dece6358 23045+
f2c43d5f
AM
23046+out_rev_src:
23047+ AuDbg("err %d, reverting src\n", err);
23048+ au_ren_rev_diropq(err, a);
23049+out:
23050+ return err;
23051+}
23052+
23053+static int do_rename(struct au_ren_args *a)
23054+{
23055+ int err;
23056+ struct dentry *d, *h_d;
23057+
23058+ if (!a->exchange) {
23059+ /* prepare workqueue args for asynchronous rmdir */
23060+ h_d = a->dst_h_dentry;
23061+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
8b6a4947 23062+ /* && !au_ftest_ren(a->auren_flags, DIRREN) */
f2c43d5f
AM
23063+ && d_is_positive(h_d)) {
23064+ err = -ENOMEM;
23065+ a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
23066+ GFP_NOFS);
23067+ if (unlikely(!a->thargs))
23068+ goto out;
23069+ a->h_dst = dget(h_d);
23070+ }
23071+
23072+ /* create whiteout for src_dentry */
23073+ if (au_ftest_ren(a->auren_flags, WHSRC)) {
23074+ a->src_bwh = au_dbwh(a->src_dentry);
23075+ AuDebugOn(a->src_bwh >= 0);
23076+ a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
23077+ a->src_h_parent);
23078+ err = PTR_ERR(a->src_wh_dentry);
23079+ if (IS_ERR(a->src_wh_dentry))
23080+ goto out_thargs;
23081+ }
23082+
23083+ /* lookup whiteout for dentry */
23084+ if (au_ftest_ren(a->auren_flags, WHDST)) {
23085+ h_d = au_wh_lkup(a->dst_h_parent,
23086+ &a->dst_dentry->d_name, a->br);
23087+ err = PTR_ERR(h_d);
23088+ if (IS_ERR(h_d))
23089+ goto out_whsrc;
23090+ if (d_is_negative(h_d))
23091+ dput(h_d);
23092+ else
23093+ a->dst_wh_dentry = h_d;
23094+ }
23095+
23096+ /* rename dentry to tmpwh */
23097+ if (a->thargs) {
23098+ err = au_whtmp_ren(a->dst_h_dentry, a->br);
23099+ if (unlikely(err))
23100+ goto out_whdst;
23101+
23102+ d = a->dst_dentry;
23103+ au_set_h_dptr(d, a->btgt, NULL);
23104+ err = au_lkup_neg(d, a->btgt, /*wh*/0);
23105+ if (unlikely(err))
23106+ goto out_whtmp;
23107+ a->dst_h_dentry = au_h_dptr(d, a->btgt);
23108+ }
4a4d8108 23109+ }
1facf9fc 23110+
5afbbe0d 23111+ BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
8b6a4947
AM
23112+#if 0
23113+ BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
23114+ && d_is_positive(a->dst_h_dentry)
23115+ && a->src_btop != a->btgt);
23116+#endif
1facf9fc 23117+
4a4d8108 23118+ /* rename by vfs_rename or cpup */
4a4d8108
AM
23119+ err = au_ren_or_cpup(a);
23120+ if (unlikely(err))
23121+ /* leave the copied-up one */
23122+ goto out_whtmp;
1308ab2a 23123+
4a4d8108 23124+ /* make dir opaque */
f2c43d5f
AM
23125+ err = au_ren_diropq(a);
23126+ if (unlikely(err))
23127+ goto out_rename;
1308ab2a 23128+
4a4d8108 23129+ /* update target timestamps */
f2c43d5f
AM
23130+ if (a->exchange) {
23131+ AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
23132+ a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
23133+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
23134+ a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
23135+ }
5afbbe0d 23136+ AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
4a4d8108
AM
23137+ a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
23138+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
5527c038 23139+ a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
1facf9fc 23140+
f2c43d5f
AM
23141+ if (!a->exchange) {
23142+ /* remove whiteout for dentry */
23143+ if (a->dst_wh_dentry) {
23144+ a->h_path.dentry = a->dst_wh_dentry;
23145+ err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
23146+ a->dst_dentry);
23147+ if (unlikely(err))
23148+ goto out_diropq;
23149+ }
1facf9fc 23150+
f2c43d5f
AM
23151+ /* remove whtmp */
23152+ if (a->thargs)
23153+ au_ren_del_whtmp(a); /* ignore this error */
1308ab2a 23154+
f2c43d5f
AM
23155+ au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
23156+ }
4a4d8108
AM
23157+ err = 0;
23158+ goto out_success;
23159+
4f0767ce 23160+out_diropq:
f2c43d5f 23161+ au_ren_rev_diropq(err, a);
4f0767ce 23162+out_rename:
7e9cd9fe 23163+ au_ren_rev_rename(err, a);
027c5e7a 23164+ dput(a->h_dst);
4f0767ce 23165+out_whtmp:
4a4d8108
AM
23166+ if (a->thargs)
23167+ au_ren_rev_whtmp(err, a);
4f0767ce 23168+out_whdst:
4a4d8108
AM
23169+ dput(a->dst_wh_dentry);
23170+ a->dst_wh_dentry = NULL;
4f0767ce 23171+out_whsrc:
4a4d8108
AM
23172+ if (a->src_wh_dentry)
23173+ au_ren_rev_whsrc(err, a);
4f0767ce 23174+out_success:
4a4d8108
AM
23175+ dput(a->src_wh_dentry);
23176+ dput(a->dst_wh_dentry);
4f0767ce 23177+out_thargs:
4a4d8108
AM
23178+ if (a->thargs) {
23179+ dput(a->h_dst);
23180+ au_whtmp_rmdir_free(a->thargs);
23181+ a->thargs = NULL;
23182+ }
4f0767ce 23183+out:
4a4d8108 23184+ return err;
dece6358 23185+}
1facf9fc 23186+
1308ab2a 23187+/* ---------------------------------------------------------------------- */
1facf9fc 23188+
4a4d8108
AM
23189+/*
23190+ * test if @dentry dir can be rename destination or not.
23191+ * success means, it is a logically empty dir.
23192+ */
23193+static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
1308ab2a 23194+{
4a4d8108 23195+ return au_test_empty(dentry, whlist);
1308ab2a 23196+}
1facf9fc 23197+
4a4d8108 23198+/*
8b6a4947
AM
23199+ * test if @a->src_dentry dir can be rename source or not.
23200+ * if it can, return 0.
4a4d8108
AM
23201+ * success means,
23202+ * - it is a logically empty dir.
23203+ * - or, it exists on writable branch and has no children including whiteouts
8b6a4947 23204+ * on the lower branch unless DIRREN is on.
4a4d8108 23205+ */
8b6a4947 23206+static int may_rename_srcdir(struct au_ren_args *a)
4a4d8108
AM
23207+{
23208+ int err;
23209+ unsigned int rdhash;
8b6a4947
AM
23210+ aufs_bindex_t btop, btgt;
23211+ struct dentry *dentry;
23212+ struct super_block *sb;
23213+ struct au_sbinfo *sbinfo;
1facf9fc 23214+
8b6a4947
AM
23215+ dentry = a->src_dentry;
23216+ sb = dentry->d_sb;
23217+ sbinfo = au_sbi(sb);
23218+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
23219+ au_fset_ren(a->auren_flags, DIRREN);
23220+
23221+ btgt = a->btgt;
5afbbe0d
AM
23222+ btop = au_dbtop(dentry);
23223+ if (btop != btgt) {
4a4d8108 23224+ struct au_nhash whlist;
dece6358 23225+
8b6a4947
AM
23226+ SiMustAnyLock(sb);
23227+ rdhash = sbinfo->si_rdhash;
4a4d8108
AM
23228+ if (!rdhash)
23229+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
23230+ dentry));
23231+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
23232+ if (unlikely(err))
23233+ goto out;
23234+ err = au_test_empty(dentry, &whlist);
23235+ au_nhash_wh_free(&whlist);
23236+ goto out;
23237+ }
dece6358 23238+
5afbbe0d 23239+ if (btop == au_dbtaildir(dentry))
4a4d8108 23240+ return 0; /* success */
dece6358 23241+
4a4d8108 23242+ err = au_test_empty_lower(dentry);
1facf9fc 23243+
4f0767ce 23244+out:
4a4d8108 23245+ if (err == -ENOTEMPTY) {
8b6a4947
AM
23246+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23247+ err = 0;
23248+ } else {
23249+ AuWarn1("renaming dir who has child(ren) on multiple "
23250+ "branches, is not supported\n");
23251+ err = -EXDEV;
23252+ }
4a4d8108
AM
23253+ }
23254+ return err;
23255+}
1308ab2a 23256+
4a4d8108
AM
23257+/* side effect: sets whlist and h_dentry */
23258+static int au_ren_may_dir(struct au_ren_args *a)
1308ab2a 23259+{
4a4d8108
AM
23260+ int err;
23261+ unsigned int rdhash;
23262+ struct dentry *d;
1facf9fc 23263+
4a4d8108
AM
23264+ d = a->dst_dentry;
23265+ SiMustAnyLock(d->d_sb);
1facf9fc 23266+
4a4d8108 23267+ err = 0;
f2c43d5f 23268+ if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
4a4d8108
AM
23269+ rdhash = au_sbi(d->d_sb)->si_rdhash;
23270+ if (!rdhash)
23271+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
23272+ err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
23273+ if (unlikely(err))
23274+ goto out;
1308ab2a 23275+
f2c43d5f
AM
23276+ if (!a->exchange) {
23277+ au_set_dbtop(d, a->dst_btop);
23278+ err = may_rename_dstdir(d, &a->whlist);
23279+ au_set_dbtop(d, a->btgt);
23280+ } else
8b6a4947 23281+ err = may_rename_srcdir(a);
4a4d8108 23282+ }
5afbbe0d 23283+ a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
4a4d8108
AM
23284+ if (unlikely(err))
23285+ goto out;
23286+
23287+ d = a->src_dentry;
5afbbe0d 23288+ a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
f2c43d5f 23289+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
8b6a4947 23290+ err = may_rename_srcdir(a);
4a4d8108
AM
23291+ if (unlikely(err)) {
23292+ au_nhash_wh_free(&a->whlist);
23293+ a->whlist.nh_num = 0;
23294+ }
23295+ }
4f0767ce 23296+out:
4a4d8108 23297+ return err;
1facf9fc 23298+}
23299+
4a4d8108 23300+/* ---------------------------------------------------------------------- */
1facf9fc 23301+
4a4d8108
AM
23302+/*
23303+ * simple tests for rename.
23304+ * following the checks in vfs, plus the parent-child relationship.
23305+ */
23306+static int au_may_ren(struct au_ren_args *a)
23307+{
23308+ int err, isdir;
23309+ struct inode *h_inode;
1facf9fc 23310+
5afbbe0d 23311+ if (a->src_btop == a->btgt) {
4a4d8108 23312+ err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
f2c43d5f 23313+ au_ftest_ren(a->auren_flags, ISDIR_SRC));
4a4d8108
AM
23314+ if (unlikely(err))
23315+ goto out;
23316+ err = -EINVAL;
23317+ if (unlikely(a->src_h_dentry == a->h_trap))
23318+ goto out;
23319+ }
1facf9fc 23320+
4a4d8108 23321+ err = 0;
5afbbe0d 23322+ if (a->dst_btop != a->btgt)
4a4d8108 23323+ goto out;
1facf9fc 23324+
027c5e7a
AM
23325+ err = -ENOTEMPTY;
23326+ if (unlikely(a->dst_h_dentry == a->h_trap))
23327+ goto out;
23328+
4a4d8108 23329+ err = -EIO;
f2c43d5f 23330+ isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
5527c038
JR
23331+ if (d_really_is_negative(a->dst_dentry)) {
23332+ if (d_is_negative(a->dst_h_dentry))
23333+ err = au_may_add(a->dst_dentry, a->btgt,
23334+ a->dst_h_parent, isdir);
4a4d8108 23335+ } else {
5527c038 23336+ if (unlikely(d_is_negative(a->dst_h_dentry)))
4a4d8108 23337+ goto out;
5527c038
JR
23338+ h_inode = d_inode(a->dst_h_dentry);
23339+ if (h_inode->i_nlink)
23340+ err = au_may_del(a->dst_dentry, a->btgt,
23341+ a->dst_h_parent, isdir);
4a4d8108 23342+ }
1facf9fc 23343+
4f0767ce 23344+out:
4a4d8108
AM
23345+ if (unlikely(err == -ENOENT || err == -EEXIST))
23346+ err = -EIO;
23347+ AuTraceErr(err);
23348+ return err;
23349+}
1facf9fc 23350+
1308ab2a 23351+/* ---------------------------------------------------------------------- */
1facf9fc 23352+
4a4d8108
AM
23353+/*
23354+ * locking order
23355+ * (VFS)
23356+ * - src_dir and dir by lock_rename()
acd2b654 23357+ * - inode if exists
4a4d8108
AM
23358+ * (aufs)
23359+ * - lock all
23360+ * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
23361+ * + si_read_lock
23362+ * + di_write_lock2_child()
23363+ * + di_write_lock_child()
23364+ * + ii_write_lock_child()
23365+ * + di_write_lock_child2()
23366+ * + ii_write_lock_child2()
23367+ * + src_parent and parent
23368+ * + di_write_lock_parent()
23369+ * + ii_write_lock_parent()
23370+ * + di_write_lock_parent2()
23371+ * + ii_write_lock_parent2()
23372+ * + lower src_dir and dir by vfsub_lock_rename()
23373+ * + verify the every relationships between child and parent. if any
23374+ * of them failed, unlock all and return -EBUSY.
23375+ */
23376+static void au_ren_unlock(struct au_ren_args *a)
1308ab2a 23377+{
4a4d8108
AM
23378+ vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
23379+ a->dst_h_parent, a->dst_hdir);
8b6a4947
AM
23380+ if (au_ftest_ren(a->auren_flags, DIRREN)
23381+ && a->h_root)
23382+ au_hn_inode_unlock(a->h_root);
f2c43d5f 23383+ if (au_ftest_ren(a->auren_flags, MNT_WRITE))
86dc4139 23384+ vfsub_mnt_drop_write(au_br_mnt(a->br));
1308ab2a 23385+}
23386+
4a4d8108 23387+static int au_ren_lock(struct au_ren_args *a)
1308ab2a 23388+{
4a4d8108
AM
23389+ int err;
23390+ unsigned int udba;
1308ab2a 23391+
4a4d8108
AM
23392+ err = 0;
23393+ a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
23394+ a->src_hdir = au_hi(a->src_dir, a->btgt);
23395+ a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
23396+ a->dst_hdir = au_hi(a->dst_dir, a->btgt);
86dc4139
AM
23397+
23398+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
23399+ if (unlikely(err))
23400+ goto out;
f2c43d5f 23401+ au_fset_ren(a->auren_flags, MNT_WRITE);
8b6a4947
AM
23402+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23403+ struct dentry *root;
23404+ struct inode *dir;
23405+
23406+ /*
23407+ * sbinfo is already locked, so this ii_read_lock is
23408+ * unnecessary. but our debugging feature checks it.
23409+ */
23410+ root = a->src_inode->i_sb->s_root;
23411+ if (root != a->src_parent && root != a->dst_parent) {
23412+ dir = d_inode(root);
23413+ ii_read_lock_parent3(dir);
23414+ a->h_root = au_hi(dir, a->btgt);
23415+ ii_read_unlock(dir);
23416+ au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
23417+ }
23418+ }
4a4d8108
AM
23419+ a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
23420+ a->dst_h_parent, a->dst_hdir);
23421+ udba = au_opt_udba(a->src_dentry->d_sb);
5527c038
JR
23422+ if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
23423+ || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
4a4d8108 23424+ err = au_busy_or_stale();
5afbbe0d 23425+ if (!err && au_dbtop(a->src_dentry) == a->btgt)
4a4d8108 23426+ err = au_h_verify(a->src_h_dentry, udba,
5527c038 23427+ d_inode(a->src_h_parent), a->src_h_parent,
4a4d8108 23428+ a->br);
5afbbe0d 23429+ if (!err && au_dbtop(a->dst_dentry) == a->btgt)
4a4d8108 23430+ err = au_h_verify(a->dst_h_dentry, udba,
5527c038 23431+ d_inode(a->dst_h_parent), a->dst_h_parent,
4a4d8108 23432+ a->br);
86dc4139 23433+ if (!err)
4a4d8108 23434+ goto out; /* success */
4a4d8108
AM
23435+
23436+ err = au_busy_or_stale();
4a4d8108 23437+ au_ren_unlock(a);
86dc4139 23438+
4f0767ce 23439+out:
4a4d8108 23440+ return err;
1facf9fc 23441+}
23442+
23443+/* ---------------------------------------------------------------------- */
23444+
4a4d8108 23445+static void au_ren_refresh_dir(struct au_ren_args *a)
1facf9fc 23446+{
4a4d8108 23447+ struct inode *dir;
dece6358 23448+
4a4d8108 23449+ dir = a->dst_dir;
be118d29 23450+ inode_inc_iversion(dir);
f2c43d5f 23451+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
4a4d8108
AM
23452+ /* is this updating defined in POSIX? */
23453+ au_cpup_attr_timesizes(a->src_inode);
23454+ au_cpup_attr_nlink(dir, /*force*/1);
4a4d8108 23455+ }
b912730e 23456+ au_dir_ts(dir, a->btgt);
dece6358 23457+
f2c43d5f
AM
23458+ if (a->exchange) {
23459+ dir = a->src_dir;
be118d29 23460+ inode_inc_iversion(dir);
f2c43d5f
AM
23461+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23462+ /* is this updating defined in POSIX? */
23463+ au_cpup_attr_timesizes(a->dst_inode);
23464+ au_cpup_attr_nlink(dir, /*force*/1);
23465+ }
23466+ au_dir_ts(dir, a->btgt);
23467+ }
23468+
23469+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108 23470+ return;
dece6358 23471+
4a4d8108 23472+ dir = a->src_dir;
be118d29 23473+ inode_inc_iversion(dir);
f2c43d5f 23474+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
4a4d8108 23475+ au_cpup_attr_nlink(dir, /*force*/1);
b912730e 23476+ au_dir_ts(dir, a->btgt);
1facf9fc 23477+}
23478+
4a4d8108 23479+static void au_ren_refresh(struct au_ren_args *a)
1facf9fc 23480+{
5afbbe0d 23481+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
23482+ struct dentry *d, *h_d;
23483+ struct inode *i, *h_i;
23484+ struct super_block *sb;
dece6358 23485+
027c5e7a
AM
23486+ d = a->dst_dentry;
23487+ d_drop(d);
23488+ if (a->h_dst)
23489+ /* already dget-ed by au_ren_or_cpup() */
23490+ au_set_h_dptr(d, a->btgt, a->h_dst);
23491+
23492+ i = a->dst_inode;
23493+ if (i) {
f2c43d5f
AM
23494+ if (!a->exchange) {
23495+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
23496+ vfsub_drop_nlink(i);
23497+ else {
23498+ vfsub_dead_dir(i);
23499+ au_cpup_attr_timesizes(i);
23500+ }
23501+ au_update_dbrange(d, /*do_put_zero*/1);
23502+ } else
23503+ au_cpup_attr_nlink(i, /*force*/1);
027c5e7a 23504+ } else {
5afbbe0d
AM
23505+ bbot = a->btgt;
23506+ for (bindex = au_dbtop(d); bindex < bbot; bindex++)
027c5e7a 23507+ au_set_h_dptr(d, bindex, NULL);
5afbbe0d
AM
23508+ bbot = au_dbbot(d);
23509+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
027c5e7a
AM
23510+ au_set_h_dptr(d, bindex, NULL);
23511+ au_update_dbrange(d, /*do_put_zero*/0);
23512+ }
23513+
8b6a4947
AM
23514+ if (a->exchange
23515+ || au_ftest_ren(a->auren_flags, DIRREN)) {
23516+ d_drop(a->src_dentry);
23517+ if (au_ftest_ren(a->auren_flags, DIRREN))
23518+ au_set_dbwh(a->src_dentry, -1);
23519+ return;
23520+ }
23521+
4a4d8108 23522+ d = a->src_dentry;
8b6a4947
AM
23523+ au_set_dbwh(d, -1);
23524+ bbot = au_dbbot(d);
23525+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23526+ h_d = au_h_dptr(d, bindex);
23527+ if (h_d)
23528+ au_set_h_dptr(d, bindex, NULL);
23529+ }
23530+ au_set_dbbot(d, a->btgt);
4a4d8108 23531+
8b6a4947
AM
23532+ sb = d->d_sb;
23533+ i = a->src_inode;
23534+ if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
23535+ return; /* success */
4a4d8108 23536+
8b6a4947
AM
23537+ bbot = au_ibbot(i);
23538+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23539+ h_i = au_h_iptr(i, bindex);
23540+ if (h_i) {
23541+ au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
23542+ /* ignore this error */
23543+ au_set_h_iptr(i, bindex, NULL, 0);
4a4d8108
AM
23544+ }
23545+ }
8b6a4947 23546+ au_set_ibbot(i, a->btgt);
1308ab2a 23547+}
dece6358 23548+
4a4d8108
AM
23549+/* ---------------------------------------------------------------------- */
23550+
23551+/* mainly for link(2) and rename(2) */
23552+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
1308ab2a 23553+{
4a4d8108
AM
23554+ aufs_bindex_t bdiropq, bwh;
23555+ struct dentry *parent;
23556+ struct au_branch *br;
23557+
23558+ parent = dentry->d_parent;
5527c038 23559+ IMustLock(d_inode(parent)); /* dir is locked */
4a4d8108
AM
23560+
23561+ bdiropq = au_dbdiropq(parent);
23562+ bwh = au_dbwh(dentry);
23563+ br = au_sbr(dentry->d_sb, btgt);
23564+ if (au_br_rdonly(br)
23565+ || (0 <= bdiropq && bdiropq < btgt)
23566+ || (0 <= bwh && bwh < btgt))
23567+ btgt = -1;
23568+
23569+ AuDbg("btgt %d\n", btgt);
23570+ return btgt;
1facf9fc 23571+}
23572+
5afbbe0d 23573+/* sets src_btop, dst_btop and btgt */
4a4d8108 23574+static int au_ren_wbr(struct au_ren_args *a)
1facf9fc 23575+{
4a4d8108
AM
23576+ int err;
23577+ struct au_wr_dir_args wr_dir_args = {
23578+ /* .force_btgt = -1, */
23579+ .flags = AuWrDir_ADD_ENTRY
23580+ };
dece6358 23581+
5afbbe0d
AM
23582+ a->src_btop = au_dbtop(a->src_dentry);
23583+ a->dst_btop = au_dbtop(a->dst_dentry);
f2c43d5f
AM
23584+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
23585+ || au_ftest_ren(a->auren_flags, ISDIR_DST))
4a4d8108 23586+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
5afbbe0d
AM
23587+ wr_dir_args.force_btgt = a->src_btop;
23588+ if (a->dst_inode && a->dst_btop < a->src_btop)
23589+ wr_dir_args.force_btgt = a->dst_btop;
4a4d8108
AM
23590+ wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
23591+ err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
23592+ a->btgt = err;
f2c43d5f
AM
23593+ if (a->exchange)
23594+ au_update_dbtop(a->dst_dentry);
dece6358 23595+
4a4d8108 23596+ return err;
1facf9fc 23597+}
23598+
4a4d8108 23599+static void au_ren_dt(struct au_ren_args *a)
1facf9fc 23600+{
4a4d8108
AM
23601+ a->h_path.dentry = a->src_h_parent;
23602+ au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
f2c43d5f 23603+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
4a4d8108
AM
23604+ a->h_path.dentry = a->dst_h_parent;
23605+ au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
23606+ }
1facf9fc 23607+
f2c43d5f
AM
23608+ au_fclr_ren(a->auren_flags, DT_DSTDIR);
23609+ if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
23610+ && !a->exchange)
4a4d8108 23611+ return;
dece6358 23612+
4a4d8108
AM
23613+ a->h_path.dentry = a->src_h_dentry;
23614+ au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
5527c038 23615+ if (d_is_positive(a->dst_h_dentry)) {
f2c43d5f 23616+ au_fset_ren(a->auren_flags, DT_DSTDIR);
4a4d8108
AM
23617+ a->h_path.dentry = a->dst_h_dentry;
23618+ au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
23619+ }
1308ab2a 23620+}
dece6358 23621+
4a4d8108 23622+static void au_ren_rev_dt(int err, struct au_ren_args *a)
1308ab2a 23623+{
4a4d8108 23624+ struct dentry *h_d;
febd17d6 23625+ struct inode *h_inode;
4a4d8108
AM
23626+
23627+ au_dtime_revert(a->src_dt + AuPARENT);
f2c43d5f 23628+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108
AM
23629+ au_dtime_revert(a->dst_dt + AuPARENT);
23630+
f2c43d5f 23631+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
4a4d8108 23632+ h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
23633+ h_inode = d_inode(h_d);
23634+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 23635+ au_dtime_revert(a->src_dt + AuCHILD);
febd17d6 23636+ inode_unlock(h_inode);
4a4d8108 23637+
f2c43d5f 23638+ if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
4a4d8108 23639+ h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
23640+ h_inode = d_inode(h_d);
23641+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 23642+ au_dtime_revert(a->dst_dt + AuCHILD);
febd17d6 23643+ inode_unlock(h_inode);
1facf9fc 23644+ }
23645+ }
23646+}
23647+
4a4d8108
AM
23648+/* ---------------------------------------------------------------------- */
23649+
23650+int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
f2c43d5f
AM
23651+ struct inode *_dst_dir, struct dentry *_dst_dentry,
23652+ unsigned int _flags)
1facf9fc 23653+{
f2c43d5f 23654+ int err, lock_flags;
8b6a4947 23655+ void *rev;
4a4d8108
AM
23656+ /* reduce stack space */
23657+ struct au_ren_args *a;
f2c43d5f 23658+ struct au_pin pin;
4a4d8108 23659+
f2c43d5f 23660+ AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
4a4d8108
AM
23661+ IMustLock(_src_dir);
23662+ IMustLock(_dst_dir);
23663+
f2c43d5f
AM
23664+ err = -EINVAL;
23665+ if (unlikely(_flags & RENAME_WHITEOUT))
23666+ goto out;
23667+
4a4d8108
AM
23668+ err = -ENOMEM;
23669+ BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
23670+ a = kzalloc(sizeof(*a), GFP_NOFS);
23671+ if (unlikely(!a))
23672+ goto out;
23673+
f2c43d5f 23674+ a->flags = _flags;
acd2b654
AM
23675+ BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
23676+ && RENAME_EXCHANGE > U8_MAX);
f2c43d5f 23677+ a->exchange = _flags & RENAME_EXCHANGE;
4a4d8108
AM
23678+ a->src_dir = _src_dir;
23679+ a->src_dentry = _src_dentry;
5527c038
JR
23680+ a->src_inode = NULL;
23681+ if (d_really_is_positive(a->src_dentry))
23682+ a->src_inode = d_inode(a->src_dentry);
4a4d8108
AM
23683+ a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
23684+ a->dst_dir = _dst_dir;
23685+ a->dst_dentry = _dst_dentry;
5527c038
JR
23686+ a->dst_inode = NULL;
23687+ if (d_really_is_positive(a->dst_dentry))
23688+ a->dst_inode = d_inode(a->dst_dentry);
4a4d8108
AM
23689+ a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
23690+ if (a->dst_inode) {
f2c43d5f
AM
23691+ /*
23692+ * if EXCHANGE && src is non-dir && dst is dir,
23693+ * dst is not locked.
23694+ */
23695+ /* IMustLock(a->dst_inode); */
4a4d8108 23696+ au_igrab(a->dst_inode);
1facf9fc 23697+ }
1facf9fc 23698+
4a4d8108 23699+ err = -ENOTDIR;
f2c43d5f 23700+ lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
2000de60 23701+ if (d_is_dir(a->src_dentry)) {
f2c43d5f
AM
23702+ au_fset_ren(a->auren_flags, ISDIR_SRC);
23703+ if (unlikely(!a->exchange
23704+ && d_really_is_positive(a->dst_dentry)
2000de60 23705+ && !d_is_dir(a->dst_dentry)))
4a4d8108 23706+ goto out_free;
f2c43d5f
AM
23707+ lock_flags |= AuLock_DIRS;
23708+ }
23709+ if (a->dst_inode && d_is_dir(a->dst_dentry)) {
23710+ au_fset_ren(a->auren_flags, ISDIR_DST);
23711+ if (unlikely(!a->exchange
23712+ && d_really_is_positive(a->src_dentry)
23713+ && !d_is_dir(a->src_dentry)))
23714+ goto out_free;
23715+ lock_flags |= AuLock_DIRS;
b95c5147 23716+ }
8b6a4947
AM
23717+ err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
23718+ lock_flags);
e49829fe
JR
23719+ if (unlikely(err))
23720+ goto out_free;
1facf9fc 23721+
027c5e7a
AM
23722+ err = au_d_hashed_positive(a->src_dentry);
23723+ if (unlikely(err))
23724+ goto out_unlock;
23725+ err = -ENOENT;
23726+ if (a->dst_inode) {
23727+ /*
f2c43d5f 23728+ * If it is a dir, VFS unhash it before this
027c5e7a
AM
23729+ * function. It means we cannot rely upon d_unhashed().
23730+ */
23731+ if (unlikely(!a->dst_inode->i_nlink))
23732+ goto out_unlock;
f2c43d5f 23733+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
027c5e7a 23734+ err = au_d_hashed_positive(a->dst_dentry);
f2c43d5f 23735+ if (unlikely(err && !a->exchange))
027c5e7a
AM
23736+ goto out_unlock;
23737+ } else if (unlikely(IS_DEADDIR(a->dst_inode)))
23738+ goto out_unlock;
23739+ } else if (unlikely(d_unhashed(a->dst_dentry)))
23740+ goto out_unlock;
23741+
7eafdf33
AM
23742+ /*
23743+ * is it possible?
79b8bda9 23744+ * yes, it happened (in linux-3.3-rcN) but I don't know why.
7eafdf33
AM
23745+ * there may exist a problem somewhere else.
23746+ */
23747+ err = -EINVAL;
5527c038 23748+ if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
7eafdf33
AM
23749+ goto out_unlock;
23750+
f2c43d5f 23751+ au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
4a4d8108 23752+ di_write_lock_parent(a->dst_parent);
1facf9fc 23753+
4a4d8108
AM
23754+ /* which branch we process */
23755+ err = au_ren_wbr(a);
23756+ if (unlikely(err < 0))
027c5e7a 23757+ goto out_parent;
4a4d8108 23758+ a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
86dc4139 23759+ a->h_path.mnt = au_br_mnt(a->br);
1facf9fc 23760+
4a4d8108
AM
23761+ /* are they available to be renamed */
23762+ err = au_ren_may_dir(a);
23763+ if (unlikely(err))
23764+ goto out_children;
1facf9fc 23765+
4a4d8108 23766+ /* prepare the writable parent dir on the same branch */
5afbbe0d 23767+ if (a->dst_btop == a->btgt) {
f2c43d5f 23768+ au_fset_ren(a->auren_flags, WHDST);
4a4d8108
AM
23769+ } else {
23770+ err = au_cpup_dirs(a->dst_dentry, a->btgt);
23771+ if (unlikely(err))
23772+ goto out_children;
23773+ }
1facf9fc 23774+
f2c43d5f
AM
23775+ err = 0;
23776+ if (!a->exchange) {
23777+ if (a->src_dir != a->dst_dir) {
23778+ /*
23779+ * this temporary unlock is safe,
23780+ * because both dir->i_mutex are locked.
23781+ */
23782+ di_write_unlock(a->dst_parent);
23783+ di_write_lock_parent(a->src_parent);
23784+ err = au_wr_dir_need_wh(a->src_dentry,
23785+ au_ftest_ren(a->auren_flags,
23786+ ISDIR_SRC),
23787+ &a->btgt);
23788+ di_write_unlock(a->src_parent);
23789+ di_write_lock2_parent(a->src_parent, a->dst_parent,
23790+ /*isdir*/1);
23791+ au_fclr_ren(a->auren_flags, ISSAMEDIR);
23792+ } else
23793+ err = au_wr_dir_need_wh(a->src_dentry,
23794+ au_ftest_ren(a->auren_flags,
23795+ ISDIR_SRC),
23796+ &a->btgt);
23797+ }
4a4d8108
AM
23798+ if (unlikely(err < 0))
23799+ goto out_children;
23800+ if (err)
f2c43d5f 23801+ au_fset_ren(a->auren_flags, WHSRC);
1facf9fc 23802+
86dc4139 23803+ /* cpup src */
5afbbe0d 23804+ if (a->src_btop != a->btgt) {
86dc4139
AM
23805+ err = au_pin(&pin, a->src_dentry, a->btgt,
23806+ au_opt_udba(a->src_dentry->d_sb),
23807+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 23808+ if (!err) {
c2b27bf2
AM
23809+ struct au_cp_generic cpg = {
23810+ .dentry = a->src_dentry,
23811+ .bdst = a->btgt,
5afbbe0d 23812+ .bsrc = a->src_btop,
c2b27bf2
AM
23813+ .len = -1,
23814+ .pin = &pin,
23815+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23816+ };
5afbbe0d 23817+ AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
c2b27bf2 23818+ err = au_sio_cpup_simple(&cpg);
367653fa 23819+ au_unpin(&pin);
86dc4139 23820+ }
86dc4139
AM
23821+ if (unlikely(err))
23822+ goto out_children;
5afbbe0d 23823+ a->src_btop = a->btgt;
86dc4139 23824+ a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
f2c43d5f
AM
23825+ if (!a->exchange)
23826+ au_fset_ren(a->auren_flags, WHSRC);
23827+ }
23828+
23829+ /* cpup dst */
23830+ if (a->exchange && a->dst_inode
23831+ && a->dst_btop != a->btgt) {
23832+ err = au_pin(&pin, a->dst_dentry, a->btgt,
23833+ au_opt_udba(a->dst_dentry->d_sb),
23834+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23835+ if (!err) {
23836+ struct au_cp_generic cpg = {
23837+ .dentry = a->dst_dentry,
23838+ .bdst = a->btgt,
23839+ .bsrc = a->dst_btop,
23840+ .len = -1,
23841+ .pin = &pin,
23842+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23843+ };
23844+ err = au_sio_cpup_simple(&cpg);
23845+ au_unpin(&pin);
23846+ }
23847+ if (unlikely(err))
23848+ goto out_children;
23849+ a->dst_btop = a->btgt;
23850+ a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
86dc4139
AM
23851+ }
23852+
4a4d8108
AM
23853+ /* lock them all */
23854+ err = au_ren_lock(a);
23855+ if (unlikely(err))
86dc4139 23856+ /* leave the copied-up one */
4a4d8108 23857+ goto out_children;
1facf9fc 23858+
f2c43d5f
AM
23859+ if (!a->exchange) {
23860+ if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
23861+ err = au_may_ren(a);
23862+ else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
23863+ err = -ENAMETOOLONG;
23864+ if (unlikely(err))
23865+ goto out_hdir;
23866+ }
1facf9fc 23867+
4a4d8108
AM
23868+ /* store timestamps to be revertible */
23869+ au_ren_dt(a);
1facf9fc 23870+
8b6a4947
AM
23871+ /* store dirren info */
23872+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23873+ err = au_dr_rename(a->src_dentry, a->btgt,
23874+ &a->dst_dentry->d_name, &rev);
23875+ AuTraceErr(err);
23876+ if (unlikely(err))
23877+ goto out_dt;
23878+ }
23879+
4a4d8108
AM
23880+ /* here we go */
23881+ err = do_rename(a);
23882+ if (unlikely(err))
8b6a4947
AM
23883+ goto out_dirren;
23884+
23885+ if (au_ftest_ren(a->auren_flags, DIRREN))
23886+ au_dr_rename_fin(a->src_dentry, a->btgt, rev);
4a4d8108
AM
23887+
23888+ /* update dir attributes */
23889+ au_ren_refresh_dir(a);
23890+
23891+ /* dput/iput all lower dentries */
23892+ au_ren_refresh(a);
23893+
23894+ goto out_hdir; /* success */
23895+
8b6a4947
AM
23896+out_dirren:
23897+ if (au_ftest_ren(a->auren_flags, DIRREN))
23898+ au_dr_rename_rev(a->src_dentry, a->btgt, rev);
4f0767ce 23899+out_dt:
4a4d8108 23900+ au_ren_rev_dt(err, a);
4f0767ce 23901+out_hdir:
4a4d8108 23902+ au_ren_unlock(a);
4f0767ce 23903+out_children:
4a4d8108 23904+ au_nhash_wh_free(&a->whlist);
5afbbe0d
AM
23905+ if (err && a->dst_inode && a->dst_btop != a->btgt) {
23906+ AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
027c5e7a 23907+ au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
5afbbe0d 23908+ au_set_dbtop(a->dst_dentry, a->dst_btop);
4a4d8108 23909+ }
027c5e7a 23910+out_parent:
f2c43d5f 23911+ if (!err) {
8b6a4947
AM
23912+ if (d_unhashed(a->src_dentry))
23913+ au_fset_ren(a->auren_flags, DROPPED_SRC);
23914+ if (d_unhashed(a->dst_dentry))
23915+ au_fset_ren(a->auren_flags, DROPPED_DST);
f2c43d5f
AM
23916+ if (!a->exchange)
23917+ d_move(a->src_dentry, a->dst_dentry);
8b6a4947 23918+ else {
f2c43d5f 23919+ d_exchange(a->src_dentry, a->dst_dentry);
8b6a4947
AM
23920+ if (au_ftest_ren(a->auren_flags, DROPPED_DST))
23921+ d_drop(a->dst_dentry);
23922+ }
23923+ if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
23924+ d_drop(a->src_dentry);
f2c43d5f 23925+ } else {
5afbbe0d 23926+ au_update_dbtop(a->dst_dentry);
027c5e7a
AM
23927+ if (!a->dst_inode)
23928+ d_drop(a->dst_dentry);
23929+ }
f2c43d5f 23930+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108
AM
23931+ di_write_unlock(a->dst_parent);
23932+ else
23933+ di_write_unlock2(a->src_parent, a->dst_parent);
027c5e7a 23934+out_unlock:
4a4d8108 23935+ aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
4f0767ce 23936+out_free:
4a4d8108
AM
23937+ iput(a->dst_inode);
23938+ if (a->thargs)
23939+ au_whtmp_rmdir_free(a->thargs);
9f237c51 23940+ au_kfree_rcu(a);
4f0767ce 23941+out:
4a4d8108
AM
23942+ AuTraceErr(err);
23943+ return err;
1308ab2a 23944+}
7f207e10
AM
23945diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
23946--- /usr/share/empty/fs/aufs/Kconfig 1970-01-01 01:00:00.000000000 +0100
b00004a5 23947+++ linux/fs/aufs/Kconfig 2018-06-04 09:08:09.181412645 +0200
2121bcd9
AM
23948@@ -0,0 +1,199 @@
23949+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
23950+config AUFS_FS
23951+ tristate "Aufs (Advanced multi layered unification filesystem) support"
4a4d8108
AM
23952+ help
23953+ Aufs is a stackable unification filesystem such as Unionfs,
23954+ which unifies several directories and provides a merged single
23955+ directory.
23956+ In the early days, aufs was entirely re-designed and
23957+ re-implemented Unionfs Version 1.x series. Introducing many
23958+ original ideas, approaches and improvements, it becomes totally
23959+ different from Unionfs while keeping the basic features.
1facf9fc 23960+
4a4d8108
AM
23961+if AUFS_FS
23962+choice
23963+ prompt "Maximum number of branches"
23964+ default AUFS_BRANCH_MAX_127
23965+ help
23966+ Specifies the maximum number of branches (or member directories)
23967+ in a single aufs. The larger value consumes more system
23968+ resources and has a minor impact to performance.
23969+config AUFS_BRANCH_MAX_127
23970+ bool "127"
23971+ help
23972+ Specifies the maximum number of branches (or member directories)
23973+ in a single aufs. The larger value consumes more system
23974+ resources and has a minor impact to performance.
23975+config AUFS_BRANCH_MAX_511
23976+ bool "511"
23977+ help
23978+ Specifies the maximum number of branches (or member directories)
23979+ in a single aufs. The larger value consumes more system
23980+ resources and has a minor impact to performance.
23981+config AUFS_BRANCH_MAX_1023
23982+ bool "1023"
23983+ help
23984+ Specifies the maximum number of branches (or member directories)
23985+ in a single aufs. The larger value consumes more system
23986+ resources and has a minor impact to performance.
23987+config AUFS_BRANCH_MAX_32767
23988+ bool "32767"
23989+ help
23990+ Specifies the maximum number of branches (or member directories)
23991+ in a single aufs. The larger value consumes more system
23992+ resources and has a minor impact to performance.
23993+endchoice
1facf9fc 23994+
e49829fe
JR
23995+config AUFS_SBILIST
23996+ bool
23997+ depends on AUFS_MAGIC_SYSRQ || PROC_FS
23998+ default y
23999+ help
24000+ Automatic configuration for internal use.
24001+ When aufs supports Magic SysRq or /proc, enabled automatically.
24002+
4a4d8108
AM
24003+config AUFS_HNOTIFY
24004+ bool "Detect direct branch access (bypassing aufs)"
24005+ help
24006+ If you want to modify files on branches directly, eg. bypassing aufs,
24007+ and want aufs to detect the changes of them fully, then enable this
24008+ option and use 'udba=notify' mount option.
7f207e10 24009+ Currently there is only one available configuration, "fsnotify".
4a4d8108
AM
24010+ It will have a negative impact to the performance.
24011+ See detail in aufs.5.
dece6358 24012+
4a4d8108
AM
24013+choice
24014+ prompt "method" if AUFS_HNOTIFY
24015+ default AUFS_HFSNOTIFY
24016+config AUFS_HFSNOTIFY
24017+ bool "fsnotify"
24018+ select FSNOTIFY
4a4d8108 24019+endchoice
1facf9fc 24020+
4a4d8108
AM
24021+config AUFS_EXPORT
24022+ bool "NFS-exportable aufs"
2cbb1c4b 24023+ depends on EXPORTFS
4a4d8108
AM
24024+ help
24025+ If you want to export your mounted aufs via NFS, then enable this
24026+ option. There are several requirements for this configuration.
24027+ See detail in aufs.5.
1facf9fc 24028+
4a4d8108
AM
24029+config AUFS_INO_T_64
24030+ bool
24031+ depends on AUFS_EXPORT
24032+ depends on 64BIT && !(ALPHA || S390)
24033+ default y
24034+ help
24035+ Automatic configuration for internal use.
24036+ /* typedef unsigned long/int __kernel_ino_t */
24037+ /* alpha and s390x are int */
1facf9fc 24038+
c1595e42
JR
24039+config AUFS_XATTR
24040+ bool "support for XATTR/EA (including Security Labels)"
24041+ help
24042+ If your branch fs supports XATTR/EA and you want to make them
24043+ available in aufs too, then enable this opsion and specify the
24044+ branch attributes for EA.
24045+ See detail in aufs.5.
24046+
076b876e
AM
24047+config AUFS_FHSM
24048+ bool "File-based Hierarchical Storage Management"
24049+ help
24050+ Hierarchical Storage Management (or HSM) is a well-known feature
24051+ in the storage world. Aufs provides this feature as file-based.
24052+ with multiple branches.
24053+ These multiple branches are prioritized, ie. the topmost one
24054+ should be the fastest drive and be used heavily.
24055+
4a4d8108
AM
24056+config AUFS_RDU
24057+ bool "Readdir in userspace"
24058+ help
24059+ Aufs has two methods to provide a merged view for a directory,
24060+ by a user-space library and by kernel-space natively. The latter
24061+ is always enabled but sometimes large and slow.
24062+ If you enable this option, install the library in aufs2-util
24063+ package, and set some environment variables for your readdir(3),
24064+ then the work will be handled in user-space which generally
24065+ shows better performance in most cases.
24066+ See detail in aufs.5.
1facf9fc 24067+
8b6a4947
AM
24068+config AUFS_DIRREN
24069+ bool "Workaround for rename(2)-ing a directory"
24070+ help
24071+ By default, aufs returns EXDEV error in renameing a dir who has
24072+ his child on the lower branch, since it is a bad idea to issue
24073+ rename(2) internally for every lower branch. But user may not
24074+ accept this behaviour. So here is a workaround to allow such
24075+ rename(2) and store some extra infromation on the writable
24076+ branch. Obviously this costs high (and I don't like it).
24077+ To use this feature, you need to enable this configuration AND
24078+ to specify the mount option `dirren.'
24079+ See details in aufs.5 and the design documents.
24080+
4a4d8108
AM
24081+config AUFS_SHWH
24082+ bool "Show whiteouts"
24083+ help
24084+ If you want to make the whiteouts in aufs visible, then enable
24085+ this option and specify 'shwh' mount option. Although it may
24086+ sounds like philosophy or something, but in technically it
24087+ simply shows the name of whiteout with keeping its behaviour.
1facf9fc 24088+
4a4d8108
AM
24089+config AUFS_BR_RAMFS
24090+ bool "Ramfs (initramfs/rootfs) as an aufs branch"
24091+ help
24092+ If you want to use ramfs as an aufs branch fs, then enable this
24093+ option. Generally tmpfs is recommended.
24094+ Aufs prohibited them to be a branch fs by default, because
24095+ initramfs becomes unusable after switch_root or something
24096+ generally. If you sets initramfs as an aufs branch and boot your
24097+ system by switch_root, you will meet a problem easily since the
24098+ files in initramfs may be inaccessible.
24099+ Unless you are going to use ramfs as an aufs branch fs without
24100+ switch_root or something, leave it N.
1facf9fc 24101+
4a4d8108
AM
24102+config AUFS_BR_FUSE
24103+ bool "Fuse fs as an aufs branch"
24104+ depends on FUSE_FS
24105+ select AUFS_POLL
24106+ help
24107+ If you want to use fuse-based userspace filesystem as an aufs
24108+ branch fs, then enable this option.
24109+ It implements the internal poll(2) operation which is
24110+ implemented by fuse only (curretnly).
1facf9fc 24111+
4a4d8108
AM
24112+config AUFS_POLL
24113+ bool
24114+ help
24115+ Automatic configuration for internal use.
1facf9fc 24116+
4a4d8108
AM
24117+config AUFS_BR_HFSPLUS
24118+ bool "Hfsplus as an aufs branch"
24119+ depends on HFSPLUS_FS
24120+ default y
24121+ help
24122+ If you want to use hfsplus fs as an aufs branch fs, then enable
24123+ this option. This option introduces a small overhead at
24124+ copying-up a file on hfsplus.
1facf9fc 24125+
4a4d8108
AM
24126+config AUFS_BDEV_LOOP
24127+ bool
24128+ depends on BLK_DEV_LOOP
24129+ default y
24130+ help
24131+ Automatic configuration for internal use.
24132+ Convert =[ym] into =y.
1308ab2a 24133+
4a4d8108
AM
24134+config AUFS_DEBUG
24135+ bool "Debug aufs"
24136+ help
24137+ Enable this to compile aufs internal debug code.
24138+ It will have a negative impact to the performance.
24139+
24140+config AUFS_MAGIC_SYSRQ
24141+ bool
24142+ depends on AUFS_DEBUG && MAGIC_SYSRQ
24143+ default y
24144+ help
24145+ Automatic configuration for internal use.
24146+ When aufs supports Magic SysRq, enabled automatically.
24147+endif
acd2b654
AM
24148diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
24149--- /usr/share/empty/fs/aufs/lcnt.h 1970-01-01 01:00:00.000000000 +0100
ba1aed25 24150+++ linux/fs/aufs/lcnt.h 2019-03-05 12:13:00.142557771 +0100
acd2b654
AM
24151@@ -0,0 +1,186 @@
24152+/* SPDX-License-Identifier: GPL-2.0 */
24153+/*
ba1aed25 24154+ * Copyright (C) 2018-2019 Junjiro R. Okajima
acd2b654
AM
24155+ *
24156+ * This program, aufs is free software; you can redistribute it and/or modify
24157+ * it under the terms of the GNU General Public License as published by
24158+ * the Free Software Foundation; either version 2 of the License, or
24159+ * (at your option) any later version.
24160+ *
24161+ * This program is distributed in the hope that it will be useful,
24162+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24163+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24164+ * GNU General Public License for more details.
24165+ *
24166+ * You should have received a copy of the GNU General Public License
24167+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
24168+ */
24169+
24170+/*
24171+ * simple long counter wrapper
24172+ */
24173+
24174+#ifndef __AUFS_LCNT_H__
24175+#define __AUFS_LCNT_H__
24176+
24177+#ifdef __KERNEL__
24178+
24179+#include "debug.h"
24180+
24181+#define AuLCntATOMIC 1
24182+#define AuLCntPCPUCNT 2
24183+/*
24184+ * why does percpu_refcount require extra synchronize_rcu()s in
24185+ * au_br_do_free()
24186+ */
24187+#define AuLCntPCPUREF 3
24188+
24189+/* #define AuLCntChosen AuLCntATOMIC */
24190+#define AuLCntChosen AuLCntPCPUCNT
24191+/* #define AuLCntChosen AuLCntPCPUREF */
24192+
24193+#if AuLCntChosen == AuLCntATOMIC
24194+#include <linux/atomic.h>
24195+
24196+typedef atomic_long_t au_lcnt_t;
24197+
24198+static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24199+{
24200+ atomic_long_set(cnt, 0);
24201+ return 0;
24202+}
24203+
24204+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24205+{
24206+ /* empty */
24207+}
24208+
24209+static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
24210+ int do_sync __maybe_unused)
24211+{
24212+ /* empty */
24213+}
24214+
24215+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24216+{
24217+ atomic_long_inc(cnt);
24218+}
24219+
24220+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24221+{
24222+ atomic_long_dec(cnt);
24223+}
24224+
24225+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24226+{
24227+ return atomic_long_read(cnt);
24228+}
24229+#endif
24230+
24231+#if AuLCntChosen == AuLCntPCPUCNT
24232+#include <linux/percpu_counter.h>
24233+
24234+typedef struct percpu_counter au_lcnt_t;
24235+
24236+static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24237+{
24238+ return percpu_counter_init(cnt, 0, GFP_NOFS);
24239+}
24240+
24241+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24242+{
24243+ /* empty */
24244+}
24245+
24246+static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
24247+{
24248+ percpu_counter_destroy(cnt);
24249+}
24250+
24251+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24252+{
24253+ percpu_counter_inc(cnt);
24254+}
24255+
24256+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24257+{
24258+ percpu_counter_dec(cnt);
24259+}
24260+
24261+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24262+{
24263+ s64 n;
24264+
24265+ n = percpu_counter_sum(cnt);
24266+ BUG_ON(n < 0);
24267+ if (LONG_MAX != LLONG_MAX
24268+ && n > LONG_MAX)
24269+ AuWarn1("%s\n", "wrap-around");
24270+
24271+ return n;
24272+}
24273+#endif
24274+
24275+#if AuLCntChosen == AuLCntPCPUREF
24276+#include <linux/percpu-refcount.h>
24277+
24278+typedef struct percpu_ref au_lcnt_t;
24279+
24280+static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
24281+{
24282+ if (!release)
24283+ release = percpu_ref_exit;
24284+ return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
24285+}
24286+
24287+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24288+{
24289+ synchronize_rcu();
24290+}
24291+
24292+static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
24293+{
24294+ percpu_ref_kill(cnt);
24295+ if (do_sync)
24296+ au_lcnt_wait_for_fin(cnt);
24297+}
24298+
24299+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24300+{
24301+ percpu_ref_get(cnt);
24302+}
24303+
24304+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24305+{
24306+ percpu_ref_put(cnt);
24307+}
24308+
24309+/*
24310+ * avoid calling this func as possible.
24311+ */
24312+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
24313+{
24314+ long l;
24315+
24316+ percpu_ref_switch_to_atomic_sync(cnt);
24317+ l = atomic_long_read(&cnt->count);
24318+ if (do_rev)
24319+ percpu_ref_switch_to_percpu(cnt);
24320+
24321+ /* percpu_ref is initialized by 1 instead of 0 */
24322+ return l - 1;
24323+}
24324+#endif
24325+
24326+#ifdef CONFIG_AUFS_DEBUG
24327+#define AuLCntZero(val) do { \
24328+ long l = val; \
24329+ if (l) \
24330+ AuDbg("%s = %ld\n", #val, l); \
24331+} while (0)
24332+#else
24333+#define AuLCntZero(val) do {} while (0)
24334+#endif
24335+
24336+#endif /* __KERNEL__ */
24337+#endif /* __AUFS_LCNT_H__ */
7f207e10
AM
24338diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
24339--- /usr/share/empty/fs/aufs/loop.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 24340+++ linux/fs/aufs/loop.c 2019-03-05 12:13:00.142557771 +0100
062440b3 24341@@ -0,0 +1,148 @@
cd7a4cd9 24342+// SPDX-License-Identifier: GPL-2.0
1facf9fc 24343+/*
ba1aed25 24344+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 24345+ *
24346+ * This program, aufs is free software; you can redistribute it and/or modify
24347+ * it under the terms of the GNU General Public License as published by
24348+ * the Free Software Foundation; either version 2 of the License, or
24349+ * (at your option) any later version.
dece6358
AM
24350+ *
24351+ * This program is distributed in the hope that it will be useful,
24352+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24353+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24354+ * GNU General Public License for more details.
24355+ *
24356+ * You should have received a copy of the GNU General Public License
523b37e3 24357+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24358+ */
24359+
24360+/*
24361+ * support for loopback block device as a branch
24362+ */
24363+
1facf9fc 24364+#include "aufs.h"
24365+
392086de
AM
24366+/* added into drivers/block/loop.c */
24367+static struct file *(*backing_file_func)(struct super_block *sb);
24368+
1facf9fc 24369+/*
24370+ * test if two lower dentries have overlapping branches.
24371+ */
b752ccd1 24372+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
1facf9fc 24373+{
b752ccd1 24374+ struct super_block *h_sb;
392086de
AM
24375+ struct file *backing_file;
24376+
24377+ if (unlikely(!backing_file_func)) {
24378+ /* don't load "loop" module here */
24379+ backing_file_func = symbol_get(loop_backing_file);
24380+ if (unlikely(!backing_file_func))
24381+ /* "loop" module is not loaded */
24382+ return 0;
24383+ }
1facf9fc 24384+
b752ccd1 24385+ h_sb = h_adding->d_sb;
392086de
AM
24386+ backing_file = backing_file_func(h_sb);
24387+ if (!backing_file)
1facf9fc 24388+ return 0;
24389+
2000de60 24390+ h_adding = backing_file->f_path.dentry;
b752ccd1
AM
24391+ /*
24392+ * h_adding can be local NFS.
24393+ * in this case aufs cannot detect the loop.
24394+ */
24395+ if (unlikely(h_adding->d_sb == sb))
1facf9fc 24396+ return 1;
b752ccd1 24397+ return !!au_test_subdir(h_adding, sb->s_root);
1facf9fc 24398+}
24399+
24400+/* true if a kernel thread named 'loop[0-9].*' accesses a file */
24401+int au_test_loopback_kthread(void)
24402+{
b752ccd1
AM
24403+ int ret;
24404+ struct task_struct *tsk = current;
a2a7ad62 24405+ char c, comm[sizeof(tsk->comm)];
b752ccd1
AM
24406+
24407+ ret = 0;
24408+ if (tsk->flags & PF_KTHREAD) {
a2a7ad62
AM
24409+ get_task_comm(comm, tsk);
24410+ c = comm[4];
b752ccd1 24411+ ret = ('0' <= c && c <= '9'
a2a7ad62 24412+ && !strncmp(comm, "loop", 4));
b752ccd1 24413+ }
1facf9fc 24414+
b752ccd1 24415+ return ret;
1facf9fc 24416+}
87a755f4
AM
24417+
24418+/* ---------------------------------------------------------------------- */
24419+
24420+#define au_warn_loopback_step 16
24421+static int au_warn_loopback_nelem = au_warn_loopback_step;
24422+static unsigned long *au_warn_loopback_array;
24423+
24424+void au_warn_loopback(struct super_block *h_sb)
24425+{
24426+ int i, new_nelem;
24427+ unsigned long *a, magic;
24428+ static DEFINE_SPINLOCK(spin);
24429+
24430+ magic = h_sb->s_magic;
24431+ spin_lock(&spin);
24432+ a = au_warn_loopback_array;
24433+ for (i = 0; i < au_warn_loopback_nelem && *a; i++)
24434+ if (a[i] == magic) {
24435+ spin_unlock(&spin);
24436+ return;
24437+ }
24438+
24439+ /* h_sb is new to us, print it */
24440+ if (i < au_warn_loopback_nelem) {
24441+ a[i] = magic;
24442+ goto pr;
24443+ }
24444+
24445+ /* expand the array */
24446+ new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
24447+ a = au_kzrealloc(au_warn_loopback_array,
24448+ au_warn_loopback_nelem * sizeof(unsigned long),
e2f27e51
AM
24449+ new_nelem * sizeof(unsigned long), GFP_ATOMIC,
24450+ /*may_shrink*/0);
87a755f4
AM
24451+ if (a) {
24452+ au_warn_loopback_nelem = new_nelem;
24453+ au_warn_loopback_array = a;
24454+ a[i] = magic;
24455+ goto pr;
24456+ }
24457+
24458+ spin_unlock(&spin);
24459+ AuWarn1("realloc failed, ignored\n");
24460+ return;
24461+
24462+pr:
24463+ spin_unlock(&spin);
0c3ec466
AM
24464+ pr_warn("you may want to try another patch for loopback file "
24465+ "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
87a755f4
AM
24466+}
24467+
24468+int au_loopback_init(void)
24469+{
24470+ int err;
24471+ struct super_block *sb __maybe_unused;
24472+
79b8bda9 24473+ BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(unsigned long));
87a755f4
AM
24474+
24475+ err = 0;
24476+ au_warn_loopback_array = kcalloc(au_warn_loopback_step,
24477+ sizeof(unsigned long), GFP_NOFS);
24478+ if (unlikely(!au_warn_loopback_array))
24479+ err = -ENOMEM;
24480+
24481+ return err;
24482+}
24483+
24484+void au_loopback_fin(void)
24485+{
79b8bda9
AM
24486+ if (backing_file_func)
24487+ symbol_put(loop_backing_file);
9f237c51 24488+ au_kfree_try_rcu(au_warn_loopback_array);
87a755f4 24489+}
7f207e10
AM
24490diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
24491--- /usr/share/empty/fs/aufs/loop.h 1970-01-01 01:00:00.000000000 +0100
ba1aed25 24492+++ linux/fs/aufs/loop.h 2019-03-05 12:13:00.142557771 +0100
062440b3
AM
24493@@ -0,0 +1,53 @@
24494+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 24495+/*
ba1aed25 24496+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 24497+ *
24498+ * This program, aufs is free software; you can redistribute it and/or modify
24499+ * it under the terms of the GNU General Public License as published by
24500+ * the Free Software Foundation; either version 2 of the License, or
24501+ * (at your option) any later version.
dece6358
AM
24502+ *
24503+ * This program is distributed in the hope that it will be useful,
24504+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24505+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24506+ * GNU General Public License for more details.
24507+ *
24508+ * You should have received a copy of the GNU General Public License
523b37e3 24509+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24510+ */
24511+
24512+/*
24513+ * support for loopback mount as a branch
24514+ */
24515+
24516+#ifndef __AUFS_LOOP_H__
24517+#define __AUFS_LOOP_H__
24518+
24519+#ifdef __KERNEL__
24520+
dece6358
AM
24521+struct dentry;
24522+struct super_block;
1facf9fc 24523+
24524+#ifdef CONFIG_AUFS_BDEV_LOOP
392086de
AM
24525+/* drivers/block/loop.c */
24526+struct file *loop_backing_file(struct super_block *sb);
24527+
1facf9fc 24528+/* loop.c */
b752ccd1 24529+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
1facf9fc 24530+int au_test_loopback_kthread(void);
87a755f4
AM
24531+void au_warn_loopback(struct super_block *h_sb);
24532+
24533+int au_loopback_init(void);
24534+void au_loopback_fin(void);
1facf9fc 24535+#else
4a4d8108 24536+AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
b752ccd1 24537+ struct dentry *h_adding)
4a4d8108 24538+AuStubInt0(au_test_loopback_kthread, void)
87a755f4
AM
24539+AuStubVoid(au_warn_loopback, struct super_block *h_sb)
24540+
24541+AuStubInt0(au_loopback_init, void)
24542+AuStubVoid(au_loopback_fin, void)
1facf9fc 24543+#endif /* BLK_DEV_LOOP */
24544+
24545+#endif /* __KERNEL__ */
24546+#endif /* __AUFS_LOOP_H__ */
7f207e10
AM
24547diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
24548--- /usr/share/empty/fs/aufs/magic.mk 1970-01-01 01:00:00.000000000 +0100
b00004a5 24549+++ linux/fs/aufs/magic.mk 2018-06-04 09:08:09.188079511 +0200
2121bcd9
AM
24550@@ -0,0 +1,31 @@
24551+# SPDX-License-Identifier: GPL-2.0
1facf9fc 24552+
24553+# defined in ${srctree}/fs/fuse/inode.c
24554+# tristate
24555+ifdef CONFIG_FUSE_FS
24556+ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
24557+endif
24558+
1facf9fc 24559+# defined in ${srctree}/fs/xfs/xfs_sb.h
24560+# tristate
24561+ifdef CONFIG_XFS_FS
24562+ccflags-y += -DXFS_SB_MAGIC=0x58465342
24563+endif
24564+
24565+# defined in ${srctree}/fs/configfs/mount.c
24566+# tristate
24567+ifdef CONFIG_CONFIGFS_FS
24568+ccflags-y += -DCONFIGFS_MAGIC=0x62656570
24569+endif
24570+
1facf9fc 24571+# defined in ${srctree}/fs/ubifs/ubifs.h
24572+# tristate
24573+ifdef CONFIG_UBIFS_FS
24574+ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
24575+endif
4a4d8108
AM
24576+
24577+# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
24578+# tristate
24579+ifdef CONFIG_HFSPLUS_FS
24580+ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
24581+endif
7f207e10
AM
24582diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
24583--- /usr/share/empty/fs/aufs/Makefile 1970-01-01 01:00:00.000000000 +0100
b00004a5 24584+++ linux/fs/aufs/Makefile 2018-06-04 09:08:09.181412645 +0200
2121bcd9
AM
24585@@ -0,0 +1,46 @@
24586+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
24587+
24588+include ${src}/magic.mk
24589+ifeq (${CONFIG_AUFS_FS},m)
24590+include ${src}/conf.mk
24591+endif
24592+-include ${src}/priv_def.mk
24593+
24594+# cf. include/linux/kernel.h
24595+# enable pr_debug
24596+ccflags-y += -DDEBUG
f6c5ef8b
AM
24597+# sparse requires the full pathname
24598+ifdef M
523b37e3 24599+ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
f6c5ef8b 24600+else
523b37e3 24601+ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
f6c5ef8b 24602+endif
4a4d8108
AM
24603+
24604+obj-$(CONFIG_AUFS_FS) += aufs.o
24605+aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
24606+ wkq.o vfsub.o dcsub.o \
e49829fe 24607+ cpup.o whout.o wbr_policy.o \
4a4d8108
AM
24608+ dinfo.o dentry.o \
24609+ dynop.o \
24610+ finfo.o file.o f_op.o \
24611+ dir.o vdir.o \
24612+ iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
c2b27bf2 24613+ mvdown.o ioctl.o
4a4d8108
AM
24614+
24615+# all are boolean
e49829fe 24616+aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
4a4d8108
AM
24617+aufs-$(CONFIG_SYSFS) += sysfs.o
24618+aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
24619+aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
24620+aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
24621+aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
4a4d8108 24622+aufs-$(CONFIG_AUFS_EXPORT) += export.o
c1595e42
JR
24623+aufs-$(CONFIG_AUFS_XATTR) += xattr.o
24624+aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
8b6a4947 24625+aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
076b876e 24626+aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
4a4d8108
AM
24627+aufs-$(CONFIG_AUFS_POLL) += poll.o
24628+aufs-$(CONFIG_AUFS_RDU) += rdu.o
4a4d8108
AM
24629+aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
24630+aufs-$(CONFIG_AUFS_DEBUG) += debug.o
24631+aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
7f207e10
AM
24632diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
24633--- /usr/share/empty/fs/aufs/module.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 24634+++ linux/fs/aufs/module.c 2019-03-05 12:13:00.142557771 +0100
062440b3 24635@@ -0,0 +1,273 @@
cd7a4cd9 24636+// SPDX-License-Identifier: GPL-2.0
1facf9fc 24637+/*
ba1aed25 24638+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 24639+ *
24640+ * This program, aufs is free software; you can redistribute it and/or modify
24641+ * it under the terms of the GNU General Public License as published by
24642+ * the Free Software Foundation; either version 2 of the License, or
24643+ * (at your option) any later version.
dece6358
AM
24644+ *
24645+ * This program is distributed in the hope that it will be useful,
24646+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24647+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24648+ * GNU General Public License for more details.
24649+ *
24650+ * You should have received a copy of the GNU General Public License
523b37e3 24651+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24652+ */
24653+
24654+/*
24655+ * module global variables and operations
24656+ */
24657+
24658+#include <linux/module.h>
24659+#include <linux/seq_file.h>
24660+#include "aufs.h"
24661+
e2f27e51
AM
24662+/* shrinkable realloc */
24663+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
1facf9fc 24664+{
e2f27e51
AM
24665+ size_t sz;
24666+ int diff;
1facf9fc 24667+
e2f27e51
AM
24668+ sz = 0;
24669+ diff = -1;
24670+ if (p) {
24671+#if 0 /* unused */
24672+ if (!new_sz) {
9f237c51 24673+ au_kfree_rcu(p);
e2f27e51
AM
24674+ p = NULL;
24675+ goto out;
24676+ }
24677+#else
24678+ AuDebugOn(!new_sz);
24679+#endif
24680+ sz = ksize(p);
24681+ diff = au_kmidx_sub(sz, new_sz);
24682+ }
24683+ if (sz && !diff)
24684+ goto out;
24685+
24686+ if (sz < new_sz)
24687+ /* expand or SLOB */
24688+ p = krealloc(p, new_sz, gfp);
24689+ else if (new_sz < sz && may_shrink) {
24690+ /* shrink */
24691+ void *q;
24692+
24693+ q = kmalloc(new_sz, gfp);
24694+ if (q) {
24695+ if (p) {
24696+ memcpy(q, p, new_sz);
9f237c51 24697+ au_kfree_try_rcu(p);
e2f27e51
AM
24698+ }
24699+ p = q;
24700+ } else
24701+ p = NULL;
24702+ }
24703+
24704+out:
24705+ return p;
24706+}
24707+
24708+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24709+ int may_shrink)
24710+{
24711+ p = au_krealloc(p, new_sz, gfp, may_shrink);
24712+ if (p && new_sz > nused)
1facf9fc 24713+ memset(p + nused, 0, new_sz - nused);
24714+ return p;
24715+}
24716+
24717+/* ---------------------------------------------------------------------- */
1facf9fc 24718+/*
24719+ * aufs caches
24720+ */
1c60b727 24721+struct kmem_cache *au_cache[AuCache_Last];
5afbbe0d
AM
24722+
24723+static void au_cache_fin(void)
24724+{
24725+ int i;
24726+
24727+ /*
24728+ * Make sure all delayed rcu free inodes are flushed before we
24729+ * destroy cache.
24730+ */
24731+ rcu_barrier();
24732+
24733+ /* excluding AuCache_HNOTIFY */
24734+ BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
24735+ for (i = 0; i < AuCache_HNOTIFY; i++) {
1c60b727
AM
24736+ kmem_cache_destroy(au_cache[i]);
24737+ au_cache[i] = NULL;
5afbbe0d
AM
24738+ }
24739+}
24740+
1facf9fc 24741+static int __init au_cache_init(void)
24742+{
1c60b727
AM
24743+ au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
24744+ if (au_cache[AuCache_DINFO])
027c5e7a 24745+ /* SLAB_DESTROY_BY_RCU */
1c60b727 24746+ au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
f0c0a007 24747+ au_icntnr_init_once);
1c60b727
AM
24748+ if (au_cache[AuCache_ICNTNR])
24749+ au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
f0c0a007 24750+ au_fi_init_once);
1c60b727
AM
24751+ if (au_cache[AuCache_FINFO])
24752+ au_cache[AuCache_VDIR] = AuCache(au_vdir);
24753+ if (au_cache[AuCache_VDIR])
24754+ au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
24755+ if (au_cache[AuCache_DEHSTR])
1facf9fc 24756+ return 0;
24757+
5afbbe0d 24758+ au_cache_fin();
1facf9fc 24759+ return -ENOMEM;
24760+}
24761+
1facf9fc 24762+/* ---------------------------------------------------------------------- */
24763+
24764+int au_dir_roflags;
24765+
e49829fe 24766+#ifdef CONFIG_AUFS_SBILIST
1e00d052
AM
24767+/*
24768+ * iterate_supers_type() doesn't protect us from
24769+ * remounting (branch management)
24770+ */
8b6a4947 24771+struct hlist_bl_head au_sbilist;
e49829fe
JR
24772+#endif
24773+
1facf9fc 24774+/*
24775+ * functions for module interface.
24776+ */
24777+MODULE_LICENSE("GPL");
24778+/* MODULE_LICENSE("GPL v2"); */
dece6358 24779+MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
1facf9fc 24780+MODULE_DESCRIPTION(AUFS_NAME
24781+ " -- Advanced multi layered unification filesystem");
24782+MODULE_VERSION(AUFS_VERSION);
c06a8ce3 24783+MODULE_ALIAS_FS(AUFS_NAME);
1facf9fc 24784+
1facf9fc 24785+/* this module parameter has no meaning when SYSFS is disabled */
24786+int sysaufs_brs = 1;
24787+MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
cd7a4cd9 24788+module_param_named(brs, sysaufs_brs, int, 0444);
1facf9fc 24789+
076b876e 24790+/* this module parameter has no meaning when USER_NS is disabled */
8cdd5066 24791+bool au_userns;
076b876e 24792+MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
cd7a4cd9 24793+module_param_named(allow_userns, au_userns, bool, 0444);
076b876e 24794+
1facf9fc 24795+/* ---------------------------------------------------------------------- */
24796+
24797+static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
24798+
24799+int au_seq_path(struct seq_file *seq, struct path *path)
24800+{
79b8bda9
AM
24801+ int err;
24802+
24803+ err = seq_path(seq, path, au_esc_chars);
1c60b727 24804+ if (err >= 0)
79b8bda9 24805+ err = 0;
1c60b727 24806+ else
79b8bda9
AM
24807+ err = -ENOMEM;
24808+
24809+ return err;
1facf9fc 24810+}
24811+
24812+/* ---------------------------------------------------------------------- */
24813+
24814+static int __init aufs_init(void)
24815+{
24816+ int err, i;
24817+ char *p;
24818+
24819+ p = au_esc_chars;
24820+ for (i = 1; i <= ' '; i++)
24821+ *p++ = i;
24822+ *p++ = '\\';
24823+ *p++ = '\x7f';
24824+ *p = 0;
24825+
24826+ au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
24827+
b95c5147
AM
24828+ memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
24829+ for (i = 0; i < AuIop_Last; i++)
24830+ aufs_iop_nogetattr[i].getattr = NULL;
24831+
1c60b727 24832+ memset(au_cache, 0, sizeof(au_cache)); /* including hnotify */
f0c0a007 24833+
e49829fe 24834+ au_sbilist_init();
1facf9fc 24835+ sysaufs_brs_init();
24836+ au_debug_init();
4a4d8108 24837+ au_dy_init();
1facf9fc 24838+ err = sysaufs_init();
24839+ if (unlikely(err))
24840+ goto out;
062440b3 24841+ err = dbgaufs_init();
4f0767ce 24842+ if (unlikely(err))
953406b4 24843+ goto out_sysaufs;
062440b3
AM
24844+ err = au_procfs_init();
24845+ if (unlikely(err))
24846+ goto out_dbgaufs;
e49829fe
JR
24847+ err = au_wkq_init();
24848+ if (unlikely(err))
24849+ goto out_procfs;
87a755f4 24850+ err = au_loopback_init();
1facf9fc 24851+ if (unlikely(err))
24852+ goto out_wkq;
87a755f4
AM
24853+ err = au_hnotify_init();
24854+ if (unlikely(err))
24855+ goto out_loopback;
1facf9fc 24856+ err = au_sysrq_init();
24857+ if (unlikely(err))
24858+ goto out_hin;
24859+ err = au_cache_init();
24860+ if (unlikely(err))
24861+ goto out_sysrq;
076b876e
AM
24862+
24863+ aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
1facf9fc 24864+ err = register_filesystem(&aufs_fs_type);
24865+ if (unlikely(err))
24866+ goto out_cache;
076b876e 24867+
4a4d8108
AM
24868+ /* since we define pr_fmt, call printk directly */
24869+ printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
1facf9fc 24870+ goto out; /* success */
24871+
4f0767ce 24872+out_cache:
1facf9fc 24873+ au_cache_fin();
4f0767ce 24874+out_sysrq:
1facf9fc 24875+ au_sysrq_fin();
4f0767ce 24876+out_hin:
4a4d8108 24877+ au_hnotify_fin();
87a755f4
AM
24878+out_loopback:
24879+ au_loopback_fin();
4f0767ce 24880+out_wkq:
1facf9fc 24881+ au_wkq_fin();
e49829fe
JR
24882+out_procfs:
24883+ au_procfs_fin();
062440b3
AM
24884+out_dbgaufs:
24885+ dbgaufs_fin();
4f0767ce 24886+out_sysaufs:
1facf9fc 24887+ sysaufs_fin();
4a4d8108 24888+ au_dy_fin();
4f0767ce 24889+out:
1facf9fc 24890+ return err;
24891+}
24892+
24893+static void __exit aufs_exit(void)
24894+{
24895+ unregister_filesystem(&aufs_fs_type);
24896+ au_cache_fin();
24897+ au_sysrq_fin();
4a4d8108 24898+ au_hnotify_fin();
87a755f4 24899+ au_loopback_fin();
1facf9fc 24900+ au_wkq_fin();
e49829fe 24901+ au_procfs_fin();
062440b3 24902+ dbgaufs_fin();
1facf9fc 24903+ sysaufs_fin();
4a4d8108 24904+ au_dy_fin();
1facf9fc 24905+}
24906+
24907+module_init(aufs_init);
24908+module_exit(aufs_exit);
7f207e10
AM
24909diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
24910--- /usr/share/empty/fs/aufs/module.h 1970-01-01 01:00:00.000000000 +0100
ba1aed25 24911+++ linux/fs/aufs/module.h 2019-03-05 12:13:00.142557771 +0100
9f237c51 24912@@ -0,0 +1,166 @@
062440b3 24913+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 24914+/*
ba1aed25 24915+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 24916+ *
24917+ * This program, aufs is free software; you can redistribute it and/or modify
24918+ * it under the terms of the GNU General Public License as published by
24919+ * the Free Software Foundation; either version 2 of the License, or
24920+ * (at your option) any later version.
dece6358
AM
24921+ *
24922+ * This program is distributed in the hope that it will be useful,
24923+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24924+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24925+ * GNU General Public License for more details.
24926+ *
24927+ * You should have received a copy of the GNU General Public License
523b37e3 24928+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24929+ */
24930+
24931+/*
24932+ * module initialization and module-global
24933+ */
24934+
24935+#ifndef __AUFS_MODULE_H__
24936+#define __AUFS_MODULE_H__
24937+
24938+#ifdef __KERNEL__
24939+
24940+#include <linux/slab.h>
9f237c51
AM
24941+#include "debug.h"
24942+#include "dentry.h"
24943+#include "dir.h"
24944+#include "file.h"
24945+#include "inode.h"
1facf9fc 24946+
dece6358
AM
24947+struct path;
24948+struct seq_file;
24949+
1facf9fc 24950+/* module parameters */
1facf9fc 24951+extern int sysaufs_brs;
8cdd5066 24952+extern bool au_userns;
1facf9fc 24953+
24954+/* ---------------------------------------------------------------------- */
24955+
24956+extern int au_dir_roflags;
24957+
e2f27e51
AM
24958+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
24959+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24960+ int may_shrink);
24961+
9f237c51
AM
24962+/*
24963+ * Comparing the size of the object with sizeof(struct rcu_head)
24964+ * case 1: object is always larger
24965+ * --> au_kfree_rcu() or au_kfree_do_rcu()
24966+ * case 2: object is always smaller
24967+ * --> au_kfree_small()
24968+ * case 3: object can be any size
24969+ * --> au_kfree_try_rcu()
24970+ */
24971+
24972+static inline void au_kfree_do_rcu(const void *p)
24973+{
24974+ struct {
24975+ struct rcu_head rcu;
24976+ } *a = (void *)p;
24977+
24978+ kfree_rcu(a, rcu);
24979+}
24980+
24981+#define au_kfree_rcu(_p) do { \
24982+ typeof(_p) p = (_p); \
24983+ BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head)); \
24984+ if (p) \
24985+ au_kfree_do_rcu(p); \
24986+ } while (0)
24987+
24988+#define au_kfree_do_sz_test(sz) (sz >= sizeof(struct rcu_head))
24989+#define au_kfree_sz_test(p) (p && au_kfree_do_sz_test(ksize(p)))
24990+
24991+static inline void au_kfree_try_rcu(const void *p)
24992+{
24993+ if (!p)
24994+ return;
24995+ if (au_kfree_sz_test(p))
24996+ au_kfree_do_rcu(p);
24997+ else
24998+ kfree(p);
24999+}
25000+
25001+static inline void au_kfree_small(const void *p)
25002+{
25003+ if (!p)
25004+ return;
25005+ AuDebugOn(au_kfree_sz_test(p));
25006+ kfree(p);
25007+}
25008+
e2f27e51
AM
25009+static inline int au_kmidx_sub(size_t sz, size_t new_sz)
25010+{
25011+#ifndef CONFIG_SLOB
25012+ return kmalloc_index(sz) - kmalloc_index(new_sz);
25013+#else
25014+ return -1; /* SLOB is untested */
25015+#endif
25016+}
25017+
1facf9fc 25018+int au_seq_path(struct seq_file *seq, struct path *path);
25019+
e49829fe
JR
25020+#ifdef CONFIG_PROC_FS
25021+/* procfs.c */
25022+int __init au_procfs_init(void);
25023+void au_procfs_fin(void);
25024+#else
25025+AuStubInt0(au_procfs_init, void);
25026+AuStubVoid(au_procfs_fin, void);
25027+#endif
25028+
4f0767ce
JR
25029+/* ---------------------------------------------------------------------- */
25030+
1c60b727 25031+/* kmem cache */
1facf9fc 25032+enum {
25033+ AuCache_DINFO,
25034+ AuCache_ICNTNR,
25035+ AuCache_FINFO,
25036+ AuCache_VDIR,
25037+ AuCache_DEHSTR,
7eafdf33 25038+ AuCache_HNOTIFY, /* must be last */
1facf9fc 25039+ AuCache_Last
25040+};
25041+
1c60b727 25042+extern struct kmem_cache *au_cache[AuCache_Last];
f0c0a007 25043+
4a4d8108
AM
25044+#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
25045+#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
25046+#define AuCacheCtor(type, ctor) \
25047+ kmem_cache_create(#type, sizeof(struct type), \
25048+ __alignof__(struct type), AuCacheFlags, ctor)
1facf9fc 25049+
9f237c51
AM
25050+#define AuCacheFuncs(name, index) \
25051+ static inline struct au_##name *au_cache_alloc_##name(void) \
25052+ { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
25053+ static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
25054+ { kmem_cache_free(au_cache[AuCache_##index], p); } \
25055+ \
25056+ static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
25057+ { void *p = rcu; \
25058+ p -= offsetof(struct au_##name, rcu); \
25059+ kmem_cache_free(au_cache[AuCache_##index], p); } \
25060+ static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
25061+ { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
25062+ call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); } \
25063+ \
25064+ static inline void au_cache_free_##name(struct au_##name *p) \
25065+ { /* au_cache_free_##name##_norcu(p); */ \
25066+ au_cache_free_##name##_rcu(p); }
1facf9fc 25067+
25068+AuCacheFuncs(dinfo, DINFO);
25069+AuCacheFuncs(icntnr, ICNTNR);
25070+AuCacheFuncs(finfo, FINFO);
25071+AuCacheFuncs(vdir, VDIR);
4a4d8108
AM
25072+AuCacheFuncs(vdir_dehstr, DEHSTR);
25073+#ifdef CONFIG_AUFS_HNOTIFY
25074+AuCacheFuncs(hnotify, HNOTIFY);
25075+#endif
1facf9fc 25076+
4a4d8108
AM
25077+#endif /* __KERNEL__ */
25078+#endif /* __AUFS_MODULE_H__ */
c2b27bf2
AM
25079diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
25080--- /usr/share/empty/fs/aufs/mvdown.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25
AM
25081+++ linux/fs/aufs/mvdown.c 2019-03-05 12:13:00.142557771 +0100
25082@@ -0,0 +1,706 @@
cd7a4cd9 25083+// SPDX-License-Identifier: GPL-2.0
c2b27bf2 25084+/*
ba1aed25 25085+ * Copyright (C) 2011-2019 Junjiro R. Okajima
c2b27bf2
AM
25086+ *
25087+ * This program, aufs is free software; you can redistribute it and/or modify
25088+ * it under the terms of the GNU General Public License as published by
25089+ * the Free Software Foundation; either version 2 of the License, or
25090+ * (at your option) any later version.
25091+ *
25092+ * This program is distributed in the hope that it will be useful,
25093+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25094+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25095+ * GNU General Public License for more details.
25096+ *
25097+ * You should have received a copy of the GNU General Public License
523b37e3
AM
25098+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
25099+ */
25100+
25101+/*
25102+ * move-down, opposite of copy-up
c2b27bf2
AM
25103+ */
25104+
25105+#include "aufs.h"
25106+
c2b27bf2
AM
25107+struct au_mvd_args {
25108+ struct {
c2b27bf2
AM
25109+ struct super_block *h_sb;
25110+ struct dentry *h_parent;
25111+ struct au_hinode *hdir;
392086de 25112+ struct inode *h_dir, *h_inode;
c1595e42 25113+ struct au_pin pin;
c2b27bf2
AM
25114+ } info[AUFS_MVDOWN_NARRAY];
25115+
25116+ struct aufs_mvdown mvdown;
25117+ struct dentry *dentry, *parent;
25118+ struct inode *inode, *dir;
25119+ struct super_block *sb;
25120+ aufs_bindex_t bopq, bwh, bfound;
25121+ unsigned char rename_lock;
c2b27bf2
AM
25122+};
25123+
392086de 25124+#define mvd_errno mvdown.au_errno
076b876e
AM
25125+#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
25126+#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
25127+#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
25128+#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
c2b27bf2 25129+
392086de
AM
25130+#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
25131+#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
25132+#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
25133+#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
25134+#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
c1595e42 25135+#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
392086de
AM
25136+
25137+#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
25138+#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
25139+#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
25140+#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
25141+#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
c1595e42 25142+#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
c2b27bf2
AM
25143+
25144+#define AU_MVD_PR(flag, ...) do { \
25145+ if (flag) \
25146+ pr_err(__VA_ARGS__); \
25147+ } while (0)
25148+
076b876e
AM
25149+static int find_lower_writable(struct au_mvd_args *a)
25150+{
25151+ struct super_block *sb;
5afbbe0d 25152+ aufs_bindex_t bindex, bbot;
076b876e
AM
25153+ struct au_branch *br;
25154+
25155+ sb = a->sb;
25156+ bindex = a->mvd_bsrc;
5afbbe0d 25157+ bbot = au_sbbot(sb);
076b876e 25158+ if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
5afbbe0d 25159+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
25160+ br = au_sbr(sb, bindex);
25161+ if (au_br_fhsm(br->br_perm)
8b6a4947 25162+ && !sb_rdonly(au_br_sb(br)))
076b876e
AM
25163+ return bindex;
25164+ }
25165+ else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
5afbbe0d 25166+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
25167+ br = au_sbr(sb, bindex);
25168+ if (!au_br_rdonly(br))
25169+ return bindex;
25170+ }
25171+ else
5afbbe0d 25172+ for (bindex++; bindex <= bbot; bindex++) {
076b876e 25173+ br = au_sbr(sb, bindex);
8b6a4947 25174+ if (!sb_rdonly(au_br_sb(br))) {
076b876e
AM
25175+ if (au_br_rdonly(br))
25176+ a->mvdown.flags
25177+ |= AUFS_MVDOWN_ROLOWER_R;
25178+ return bindex;
25179+ }
25180+ }
25181+
25182+ return -1;
25183+}
25184+
c2b27bf2 25185+/* make the parent dir on bdst */
392086de 25186+static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25187+{
25188+ int err;
25189+
25190+ err = 0;
25191+ a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
25192+ a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
25193+ a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
25194+ a->mvd_h_dst_parent = NULL;
5afbbe0d 25195+ if (au_dbbot(a->parent) >= a->mvd_bdst)
c2b27bf2
AM
25196+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25197+ if (!a->mvd_h_dst_parent) {
25198+ err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
25199+ if (unlikely(err)) {
392086de 25200+ AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
c2b27bf2
AM
25201+ goto out;
25202+ }
25203+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25204+ }
25205+
25206+out:
25207+ AuTraceErr(err);
25208+ return err;
25209+}
25210+
25211+/* lock them all */
392086de 25212+static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25213+{
25214+ int err;
25215+ struct dentry *h_trap;
25216+
25217+ a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
25218+ a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
c1595e42
JR
25219+ err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
25220+ au_opt_udba(a->sb),
25221+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25222+ AuTraceErr(err);
25223+ if (unlikely(err)) {
25224+ AU_MVD_PR(dmsg, "pin_dst failed\n");
25225+ goto out;
25226+ }
25227+
c2b27bf2
AM
25228+ if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
25229+ a->rename_lock = 0;
c1595e42
JR
25230+ au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25231+ AuLsc_DI_PARENT, AuLsc_I_PARENT3,
25232+ au_opt_udba(a->sb),
25233+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25234+ err = au_do_pin(&a->mvd_pin_src);
25235+ AuTraceErr(err);
5527c038 25236+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
25237+ if (unlikely(err)) {
25238+ AU_MVD_PR(dmsg, "pin_src failed\n");
25239+ goto out_dst;
25240+ }
25241+ goto out; /* success */
c2b27bf2
AM
25242+ }
25243+
c2b27bf2 25244+ a->rename_lock = 1;
c1595e42
JR
25245+ au_pin_hdir_unlock(&a->mvd_pin_dst);
25246+ err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25247+ au_opt_udba(a->sb),
25248+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25249+ AuTraceErr(err);
5527c038 25250+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
25251+ if (unlikely(err)) {
25252+ AU_MVD_PR(dmsg, "pin_src failed\n");
25253+ au_pin_hdir_lock(&a->mvd_pin_dst);
25254+ goto out_dst;
25255+ }
25256+ au_pin_hdir_unlock(&a->mvd_pin_src);
c2b27bf2
AM
25257+ h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25258+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
25259+ if (h_trap) {
25260+ err = (h_trap != a->mvd_h_src_parent);
25261+ if (err)
25262+ err = (h_trap != a->mvd_h_dst_parent);
25263+ }
25264+ BUG_ON(err); /* it should never happen */
c1595e42
JR
25265+ if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
25266+ err = -EBUSY;
25267+ AuTraceErr(err);
25268+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25269+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
25270+ au_pin_hdir_lock(&a->mvd_pin_src);
25271+ au_unpin(&a->mvd_pin_src);
25272+ au_pin_hdir_lock(&a->mvd_pin_dst);
25273+ goto out_dst;
25274+ }
25275+ goto out; /* success */
c2b27bf2 25276+
c1595e42
JR
25277+out_dst:
25278+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
25279+out:
25280+ AuTraceErr(err);
25281+ return err;
25282+}
25283+
392086de 25284+static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2 25285+{
c1595e42
JR
25286+ if (!a->rename_lock)
25287+ au_unpin(&a->mvd_pin_src);
25288+ else {
c2b27bf2
AM
25289+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25290+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
c1595e42
JR
25291+ au_pin_hdir_lock(&a->mvd_pin_src);
25292+ au_unpin(&a->mvd_pin_src);
25293+ au_pin_hdir_lock(&a->mvd_pin_dst);
25294+ }
25295+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
25296+}
25297+
25298+/* copy-down the file */
392086de 25299+static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25300+{
25301+ int err;
25302+ struct au_cp_generic cpg = {
25303+ .dentry = a->dentry,
25304+ .bdst = a->mvd_bdst,
25305+ .bsrc = a->mvd_bsrc,
25306+ .len = -1,
c1595e42 25307+ .pin = &a->mvd_pin_dst,
c2b27bf2
AM
25308+ .flags = AuCpup_DTIME | AuCpup_HOPEN
25309+ };
25310+
25311+ AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
392086de
AM
25312+ if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25313+ au_fset_cpup(cpg.flags, OVERWRITE);
25314+ if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
25315+ au_fset_cpup(cpg.flags, RWDST);
c2b27bf2
AM
25316+ err = au_sio_cpdown_simple(&cpg);
25317+ if (unlikely(err))
392086de 25318+ AU_MVD_PR(dmsg, "cpdown failed\n");
c2b27bf2
AM
25319+
25320+ AuTraceErr(err);
25321+ return err;
25322+}
25323+
25324+/*
25325+ * unlink the whiteout on bdst if exist which may be created by UDBA while we
25326+ * were sleeping
25327+ */
392086de 25328+static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25329+{
25330+ int err;
25331+ struct path h_path;
25332+ struct au_branch *br;
523b37e3 25333+ struct inode *delegated;
c2b27bf2
AM
25334+
25335+ br = au_sbr(a->sb, a->mvd_bdst);
25336+ h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
25337+ err = PTR_ERR(h_path.dentry);
25338+ if (IS_ERR(h_path.dentry)) {
392086de 25339+ AU_MVD_PR(dmsg, "wh_lkup failed\n");
c2b27bf2
AM
25340+ goto out;
25341+ }
25342+
25343+ err = 0;
5527c038 25344+ if (d_is_positive(h_path.dentry)) {
c2b27bf2 25345+ h_path.mnt = au_br_mnt(br);
523b37e3 25346+ delegated = NULL;
5527c038 25347+ err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
523b37e3
AM
25348+ &delegated, /*force*/0);
25349+ if (unlikely(err == -EWOULDBLOCK)) {
25350+ pr_warn("cannot retry for NFSv4 delegation"
25351+ " for an internal unlink\n");
25352+ iput(delegated);
25353+ }
c2b27bf2 25354+ if (unlikely(err))
392086de 25355+ AU_MVD_PR(dmsg, "wh_unlink failed\n");
c2b27bf2
AM
25356+ }
25357+ dput(h_path.dentry);
25358+
25359+out:
25360+ AuTraceErr(err);
25361+ return err;
25362+}
25363+
25364+/*
25365+ * unlink the topmost h_dentry
c2b27bf2 25366+ */
392086de 25367+static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25368+{
25369+ int err;
25370+ struct path h_path;
523b37e3 25371+ struct inode *delegated;
c2b27bf2
AM
25372+
25373+ h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
25374+ h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
523b37e3
AM
25375+ delegated = NULL;
25376+ err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
25377+ if (unlikely(err == -EWOULDBLOCK)) {
25378+ pr_warn("cannot retry for NFSv4 delegation"
25379+ " for an internal unlink\n");
25380+ iput(delegated);
25381+ }
c2b27bf2 25382+ if (unlikely(err))
392086de 25383+ AU_MVD_PR(dmsg, "unlink failed\n");
c2b27bf2
AM
25384+
25385+ AuTraceErr(err);
25386+ return err;
25387+}
25388+
076b876e
AM
25389+/* Since mvdown succeeded, we ignore an error of this function */
25390+static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
25391+{
25392+ int err;
25393+ struct au_branch *br;
25394+
25395+ a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
25396+ br = au_sbr(a->sb, a->mvd_bsrc);
25397+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
25398+ if (!err) {
25399+ br = au_sbr(a->sb, a->mvd_bdst);
25400+ a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
25401+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
25402+ }
25403+ if (!err)
25404+ a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
25405+ else
25406+ AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
25407+}
25408+
c2b27bf2
AM
25409+/*
25410+ * copy-down the file and unlink the bsrc file.
25411+ * - unlink the bdst whout if exist
25412+ * - copy-down the file (with whtmp name and rename)
25413+ * - unlink the bsrc file
25414+ */
392086de 25415+static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25416+{
25417+ int err;
25418+
392086de 25419+ err = au_do_mkdir(dmsg, a);
c2b27bf2 25420+ if (!err)
392086de 25421+ err = au_do_lock(dmsg, a);
c2b27bf2
AM
25422+ if (unlikely(err))
25423+ goto out;
25424+
25425+ /*
25426+ * do not revert the activities we made on bdst since they should be
25427+ * harmless in aufs.
25428+ */
25429+
392086de 25430+ err = au_do_cpdown(dmsg, a);
c2b27bf2 25431+ if (!err)
392086de
AM
25432+ err = au_do_unlink_wh(dmsg, a);
25433+ if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
25434+ err = au_do_unlink(dmsg, a);
c2b27bf2
AM
25435+ if (unlikely(err))
25436+ goto out_unlock;
25437+
c1595e42
JR
25438+ AuDbg("%pd2, 0x%x, %d --> %d\n",
25439+ a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
076b876e
AM
25440+ if (find_lower_writable(a) < 0)
25441+ a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
25442+
25443+ if (a->mvdown.flags & AUFS_MVDOWN_STFS)
25444+ au_do_stfs(dmsg, a);
25445+
c2b27bf2 25446+ /* maintain internal array */
392086de
AM
25447+ if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
25448+ au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
5afbbe0d 25449+ au_set_dbtop(a->dentry, a->mvd_bdst);
392086de 25450+ au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
5afbbe0d 25451+ au_set_ibtop(a->inode, a->mvd_bdst);
79b8bda9
AM
25452+ } else {
25453+ /* hide the lower */
25454+ au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
5afbbe0d 25455+ au_set_dbbot(a->dentry, a->mvd_bsrc);
79b8bda9 25456+ au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
5afbbe0d 25457+ au_set_ibbot(a->inode, a->mvd_bsrc);
392086de 25458+ }
5afbbe0d
AM
25459+ if (au_dbbot(a->dentry) < a->mvd_bdst)
25460+ au_set_dbbot(a->dentry, a->mvd_bdst);
25461+ if (au_ibbot(a->inode) < a->mvd_bdst)
25462+ au_set_ibbot(a->inode, a->mvd_bdst);
c2b27bf2
AM
25463+
25464+out_unlock:
392086de 25465+ au_do_unlock(dmsg, a);
c2b27bf2
AM
25466+out:
25467+ AuTraceErr(err);
25468+ return err;
25469+}
25470+
25471+/* ---------------------------------------------------------------------- */
25472+
c2b27bf2 25473+/* make sure the file is idle */
392086de 25474+static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25475+{
25476+ int err, plinked;
c2b27bf2
AM
25477+
25478+ err = 0;
c2b27bf2 25479+ plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
5afbbe0d 25480+ if (au_dbtop(a->dentry) == a->mvd_bsrc
c1595e42 25481+ && au_dcount(a->dentry) == 1
c2b27bf2 25482+ && atomic_read(&a->inode->i_count) == 1
392086de 25483+ /* && a->mvd_h_src_inode->i_nlink == 1 */
c2b27bf2
AM
25484+ && (!plinked || !au_plink_test(a->inode))
25485+ && a->inode->i_nlink == 1)
25486+ goto out;
25487+
25488+ err = -EBUSY;
392086de 25489+ AU_MVD_PR(dmsg,
c1595e42 25490+ "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
5afbbe0d 25491+ a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
c2b27bf2 25492+ atomic_read(&a->inode->i_count), a->inode->i_nlink,
392086de 25493+ a->mvd_h_src_inode->i_nlink,
c2b27bf2
AM
25494+ plinked, plinked ? au_plink_test(a->inode) : 0);
25495+
25496+out:
25497+ AuTraceErr(err);
25498+ return err;
25499+}
25500+
25501+/* make sure the parent dir is fine */
392086de 25502+static int au_mvd_args_parent(const unsigned char dmsg,
c2b27bf2
AM
25503+ struct au_mvd_args *a)
25504+{
25505+ int err;
25506+ aufs_bindex_t bindex;
25507+
25508+ err = 0;
25509+ if (unlikely(au_alive_dir(a->parent))) {
25510+ err = -ENOENT;
392086de 25511+ AU_MVD_PR(dmsg, "parent dir is dead\n");
c2b27bf2
AM
25512+ goto out;
25513+ }
25514+
25515+ a->bopq = au_dbdiropq(a->parent);
25516+ bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
25517+ AuDbg("b%d\n", bindex);
25518+ if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
25519+ || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
25520+ err = -EINVAL;
392086de
AM
25521+ a->mvd_errno = EAU_MVDOWN_OPAQUE;
25522+ AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
c2b27bf2
AM
25523+ a->bopq, a->mvd_bdst);
25524+ }
25525+
25526+out:
25527+ AuTraceErr(err);
25528+ return err;
25529+}
25530+
392086de 25531+static int au_mvd_args_intermediate(const unsigned char dmsg,
c2b27bf2
AM
25532+ struct au_mvd_args *a)
25533+{
25534+ int err;
25535+ struct au_dinfo *dinfo, *tmp;
25536+
25537+ /* lookup the next lower positive entry */
25538+ err = -ENOMEM;
25539+ tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
25540+ if (unlikely(!tmp))
25541+ goto out;
25542+
25543+ a->bfound = -1;
25544+ a->bwh = -1;
25545+ dinfo = au_di(a->dentry);
25546+ au_di_cp(tmp, dinfo);
25547+ au_di_swap(tmp, dinfo);
25548+
25549+ /* returns the number of positive dentries */
5afbbe0d
AM
25550+ err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
25551+ /* AuLkup_IGNORE_PERM */ 0);
c2b27bf2
AM
25552+ if (!err)
25553+ a->bwh = au_dbwh(a->dentry);
25554+ else if (err > 0)
5afbbe0d 25555+ a->bfound = au_dbtop(a->dentry);
c2b27bf2
AM
25556+
25557+ au_di_swap(tmp, dinfo);
25558+ au_rw_write_unlock(&tmp->di_rwsem);
25559+ au_di_free(tmp);
25560+ if (unlikely(err < 0))
392086de 25561+ AU_MVD_PR(dmsg, "failed look-up lower\n");
c2b27bf2
AM
25562+
25563+ /*
25564+ * here, we have these cases.
25565+ * bfound == -1
25566+ * no positive dentry under bsrc. there are more sub-cases.
25567+ * bwh < 0
25568+ * there no whiteout, we can safely move-down.
25569+ * bwh <= bsrc
25570+ * impossible
25571+ * bsrc < bwh && bwh < bdst
25572+ * there is a whiteout on RO branch. cannot proceed.
25573+ * bwh == bdst
25574+ * there is a whiteout on the RW target branch. it should
25575+ * be removed.
25576+ * bdst < bwh
25577+ * there is a whiteout somewhere unrelated branch.
25578+ * -1 < bfound && bfound <= bsrc
25579+ * impossible.
25580+ * bfound < bdst
25581+ * found, but it is on RO branch between bsrc and bdst. cannot
25582+ * proceed.
25583+ * bfound == bdst
25584+ * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
25585+ * error.
25586+ * bdst < bfound
25587+ * found, after we create the file on bdst, it will be hidden.
25588+ */
25589+
25590+ AuDebugOn(a->bfound == -1
25591+ && a->bwh != -1
25592+ && a->bwh <= a->mvd_bsrc);
25593+ AuDebugOn(-1 < a->bfound
25594+ && a->bfound <= a->mvd_bsrc);
25595+
25596+ err = -EINVAL;
25597+ if (a->bfound == -1
25598+ && a->mvd_bsrc < a->bwh
25599+ && a->bwh != -1
25600+ && a->bwh < a->mvd_bdst) {
392086de
AM
25601+ a->mvd_errno = EAU_MVDOWN_WHITEOUT;
25602+ AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
c2b27bf2
AM
25603+ a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
25604+ goto out;
25605+ } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
392086de
AM
25606+ a->mvd_errno = EAU_MVDOWN_UPPER;
25607+ AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
c2b27bf2
AM
25608+ a->mvd_bdst, a->bfound);
25609+ goto out;
25610+ }
25611+
25612+ err = 0; /* success */
25613+
25614+out:
25615+ AuTraceErr(err);
25616+ return err;
25617+}
25618+
392086de 25619+static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25620+{
25621+ int err;
25622+
392086de
AM
25623+ err = 0;
25624+ if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25625+ && a->bfound == a->mvd_bdst)
25626+ err = -EEXIST;
c2b27bf2
AM
25627+ AuTraceErr(err);
25628+ return err;
25629+}
25630+
392086de 25631+static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25632+{
25633+ int err;
25634+ struct au_branch *br;
25635+
25636+ err = -EISDIR;
25637+ if (unlikely(S_ISDIR(a->inode->i_mode)))
25638+ goto out;
25639+
25640+ err = -EINVAL;
392086de 25641+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
5afbbe0d 25642+ a->mvd_bsrc = au_ibtop(a->inode);
392086de
AM
25643+ else {
25644+ a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
25645+ if (unlikely(a->mvd_bsrc < 0
5afbbe0d
AM
25646+ || (a->mvd_bsrc < au_dbtop(a->dentry)
25647+ || au_dbbot(a->dentry) < a->mvd_bsrc
392086de 25648+ || !au_h_dptr(a->dentry, a->mvd_bsrc))
5afbbe0d
AM
25649+ || (a->mvd_bsrc < au_ibtop(a->inode)
25650+ || au_ibbot(a->inode) < a->mvd_bsrc
392086de
AM
25651+ || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
25652+ a->mvd_errno = EAU_MVDOWN_NOUPPER;
25653+ AU_MVD_PR(dmsg, "no upper\n");
25654+ goto out;
25655+ }
25656+ }
5afbbe0d 25657+ if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
392086de
AM
25658+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25659+ AU_MVD_PR(dmsg, "on the bottom\n");
c2b27bf2
AM
25660+ goto out;
25661+ }
392086de 25662+ a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
c2b27bf2
AM
25663+ br = au_sbr(a->sb, a->mvd_bsrc);
25664+ err = au_br_rdonly(br);
392086de
AM
25665+ if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
25666+ if (unlikely(err))
25667+ goto out;
25668+ } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
25669+ || IS_APPEND(a->mvd_h_src_inode))) {
25670+ if (err)
25671+ a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
25672+ /* go on */
25673+ } else
c2b27bf2
AM
25674+ goto out;
25675+
25676+ err = -EINVAL;
392086de
AM
25677+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
25678+ a->mvd_bdst = find_lower_writable(a);
25679+ if (unlikely(a->mvd_bdst < 0)) {
25680+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25681+ AU_MVD_PR(dmsg, "no writable lower branch\n");
25682+ goto out;
25683+ }
25684+ } else {
25685+ a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
25686+ if (unlikely(a->mvd_bdst < 0
5afbbe0d 25687+ || au_sbbot(a->sb) < a->mvd_bdst)) {
392086de
AM
25688+ a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
25689+ AU_MVD_PR(dmsg, "no lower brid\n");
25690+ goto out;
25691+ }
c2b27bf2
AM
25692+ }
25693+
392086de 25694+ err = au_mvd_args_busy(dmsg, a);
c2b27bf2 25695+ if (!err)
392086de 25696+ err = au_mvd_args_parent(dmsg, a);
c2b27bf2 25697+ if (!err)
392086de 25698+ err = au_mvd_args_intermediate(dmsg, a);
c2b27bf2 25699+ if (!err)
392086de 25700+ err = au_mvd_args_exist(dmsg, a);
c2b27bf2
AM
25701+ if (!err)
25702+ AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
25703+
25704+out:
25705+ AuTraceErr(err);
25706+ return err;
25707+}
25708+
25709+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
25710+{
392086de
AM
25711+ int err, e;
25712+ unsigned char dmsg;
25713+ struct au_mvd_args *args;
79b8bda9 25714+ struct inode *inode;
c2b27bf2 25715+
79b8bda9 25716+ inode = d_inode(dentry);
c2b27bf2
AM
25717+ err = -EPERM;
25718+ if (unlikely(!capable(CAP_SYS_ADMIN)))
25719+ goto out;
25720+
392086de
AM
25721+ err = -ENOMEM;
25722+ args = kmalloc(sizeof(*args), GFP_NOFS);
25723+ if (unlikely(!args))
25724+ goto out;
25725+
25726+ err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
25727+ if (!err)
ba1aed25
AM
25728+ /* VERIFY_WRITE */
25729+ err = !access_ok(uarg, sizeof(*uarg));
c2b27bf2
AM
25730+ if (unlikely(err)) {
25731+ err = -EFAULT;
392086de
AM
25732+ AuTraceErr(err);
25733+ goto out_free;
c2b27bf2 25734+ }
392086de
AM
25735+ AuDbg("flags 0x%x\n", args->mvdown.flags);
25736+ args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
25737+ args->mvdown.au_errno = 0;
25738+ args->dentry = dentry;
79b8bda9 25739+ args->inode = inode;
392086de 25740+ args->sb = dentry->d_sb;
c2b27bf2 25741+
392086de
AM
25742+ err = -ENOENT;
25743+ dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
25744+ args->parent = dget_parent(dentry);
5527c038 25745+ args->dir = d_inode(args->parent);
febd17d6 25746+ inode_lock_nested(args->dir, I_MUTEX_PARENT);
392086de
AM
25747+ dput(args->parent);
25748+ if (unlikely(args->parent != dentry->d_parent)) {
25749+ AU_MVD_PR(dmsg, "parent dir is moved\n");
c2b27bf2
AM
25750+ goto out_dir;
25751+ }
25752+
febd17d6 25753+ inode_lock_nested(inode, I_MUTEX_CHILD);
b95c5147 25754+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
c2b27bf2
AM
25755+ if (unlikely(err))
25756+ goto out_inode;
25757+
392086de
AM
25758+ di_write_lock_parent(args->parent);
25759+ err = au_mvd_args(dmsg, args);
c2b27bf2
AM
25760+ if (unlikely(err))
25761+ goto out_parent;
25762+
392086de 25763+ err = au_do_mvdown(dmsg, args);
c2b27bf2
AM
25764+ if (unlikely(err))
25765+ goto out_parent;
c2b27bf2 25766+
392086de 25767+ au_cpup_attr_timesizes(args->dir);
79b8bda9
AM
25768+ au_cpup_attr_timesizes(inode);
25769+ if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
25770+ au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
c2b27bf2
AM
25771+ /* au_digen_dec(dentry); */
25772+
25773+out_parent:
392086de 25774+ di_write_unlock(args->parent);
c2b27bf2
AM
25775+ aufs_read_unlock(dentry, AuLock_DW);
25776+out_inode:
febd17d6 25777+ inode_unlock(inode);
c2b27bf2 25778+out_dir:
febd17d6 25779+ inode_unlock(args->dir);
392086de
AM
25780+out_free:
25781+ e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
25782+ if (unlikely(e))
25783+ err = -EFAULT;
9f237c51 25784+ au_kfree_rcu(args);
c2b27bf2
AM
25785+out:
25786+ AuTraceErr(err);
25787+ return err;
25788+}
25789diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
25790--- /usr/share/empty/fs/aufs/opts.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 25791+++ linux/fs/aufs/opts.c 2019-03-05 12:13:00.142557771 +0100
acd2b654 25792@@ -0,0 +1,1877 @@
cd7a4cd9 25793+// SPDX-License-Identifier: GPL-2.0
1facf9fc 25794+/*
ba1aed25 25795+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 25796+ *
25797+ * This program, aufs is free software; you can redistribute it and/or modify
25798+ * it under the terms of the GNU General Public License as published by
25799+ * the Free Software Foundation; either version 2 of the License, or
25800+ * (at your option) any later version.
dece6358
AM
25801+ *
25802+ * This program is distributed in the hope that it will be useful,
25803+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25804+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25805+ * GNU General Public License for more details.
25806+ *
25807+ * You should have received a copy of the GNU General Public License
523b37e3 25808+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 25809+ */
25810+
25811+/*
25812+ * mount options/flags
25813+ */
25814+
dece6358 25815+#include <linux/namei.h>
1facf9fc 25816+#include <linux/types.h> /* a distribution requires */
25817+#include <linux/parser.h>
25818+#include "aufs.h"
25819+
25820+/* ---------------------------------------------------------------------- */
25821+
25822+enum {
25823+ Opt_br,
7e9cd9fe
AM
25824+ Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
25825+ Opt_idel, Opt_imod,
25826+ Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
dece6358 25827+ Opt_rdblk_def, Opt_rdhash_def,
7e9cd9fe 25828+ Opt_xino, Opt_noxino,
1facf9fc 25829+ Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
25830+ Opt_trunc_xino_path, Opt_itrunc_xino,
25831+ Opt_trunc_xib, Opt_notrunc_xib,
dece6358 25832+ Opt_shwh, Opt_noshwh,
1facf9fc 25833+ Opt_plink, Opt_noplink, Opt_list_plink,
25834+ Opt_udba,
4a4d8108 25835+ Opt_dio, Opt_nodio,
1facf9fc 25836+ Opt_diropq_a, Opt_diropq_w,
25837+ Opt_warn_perm, Opt_nowarn_perm,
25838+ Opt_wbr_copyup, Opt_wbr_create,
076b876e 25839+ Opt_fhsm_sec,
1facf9fc 25840+ Opt_verbose, Opt_noverbose,
25841+ Opt_sum, Opt_nosum, Opt_wsum,
076b876e 25842+ Opt_dirperm1, Opt_nodirperm1,
8b6a4947 25843+ Opt_dirren, Opt_nodirren,
c1595e42 25844+ Opt_acl, Opt_noacl,
1facf9fc 25845+ Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
25846+};
25847+
25848+static match_table_t options = {
25849+ {Opt_br, "br=%s"},
25850+ {Opt_br, "br:%s"},
25851+
25852+ {Opt_add, "add=%d:%s"},
25853+ {Opt_add, "add:%d:%s"},
25854+ {Opt_add, "ins=%d:%s"},
25855+ {Opt_add, "ins:%d:%s"},
25856+ {Opt_append, "append=%s"},
25857+ {Opt_append, "append:%s"},
25858+ {Opt_prepend, "prepend=%s"},
25859+ {Opt_prepend, "prepend:%s"},
25860+
25861+ {Opt_del, "del=%s"},
25862+ {Opt_del, "del:%s"},
25863+ /* {Opt_idel, "idel:%d"}, */
25864+ {Opt_mod, "mod=%s"},
25865+ {Opt_mod, "mod:%s"},
25866+ /* {Opt_imod, "imod:%d:%s"}, */
25867+
25868+ {Opt_dirwh, "dirwh=%d"},
25869+
25870+ {Opt_xino, "xino=%s"},
25871+ {Opt_noxino, "noxino"},
25872+ {Opt_trunc_xino, "trunc_xino"},
25873+ {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
25874+ {Opt_notrunc_xino, "notrunc_xino"},
25875+ {Opt_trunc_xino_path, "trunc_xino=%s"},
25876+ {Opt_itrunc_xino, "itrunc_xino=%d"},
25877+ /* {Opt_zxino, "zxino=%s"}, */
25878+ {Opt_trunc_xib, "trunc_xib"},
25879+ {Opt_notrunc_xib, "notrunc_xib"},
25880+
e49829fe 25881+#ifdef CONFIG_PROC_FS
1facf9fc 25882+ {Opt_plink, "plink"},
e49829fe
JR
25883+#else
25884+ {Opt_ignore_silent, "plink"},
25885+#endif
25886+
1facf9fc 25887+ {Opt_noplink, "noplink"},
e49829fe 25888+
1facf9fc 25889+#ifdef CONFIG_AUFS_DEBUG
25890+ {Opt_list_plink, "list_plink"},
25891+#endif
25892+
25893+ {Opt_udba, "udba=%s"},
25894+
4a4d8108
AM
25895+ {Opt_dio, "dio"},
25896+ {Opt_nodio, "nodio"},
25897+
8b6a4947
AM
25898+#ifdef CONFIG_AUFS_DIRREN
25899+ {Opt_dirren, "dirren"},
25900+ {Opt_nodirren, "nodirren"},
25901+#else
25902+ {Opt_ignore, "dirren"},
25903+ {Opt_ignore_silent, "nodirren"},
25904+#endif
25905+
076b876e
AM
25906+#ifdef CONFIG_AUFS_FHSM
25907+ {Opt_fhsm_sec, "fhsm_sec=%d"},
25908+#else
8b6a4947 25909+ {Opt_ignore, "fhsm_sec=%d"},
076b876e
AM
25910+#endif
25911+
1facf9fc 25912+ {Opt_diropq_a, "diropq=always"},
25913+ {Opt_diropq_a, "diropq=a"},
25914+ {Opt_diropq_w, "diropq=whiteouted"},
25915+ {Opt_diropq_w, "diropq=w"},
25916+
25917+ {Opt_warn_perm, "warn_perm"},
25918+ {Opt_nowarn_perm, "nowarn_perm"},
25919+
25920+ /* keep them temporary */
1facf9fc 25921+ {Opt_ignore_silent, "nodlgt"},
8b6a4947 25922+ {Opt_ignore, "clean_plink"},
1facf9fc 25923+
dece6358
AM
25924+#ifdef CONFIG_AUFS_SHWH
25925+ {Opt_shwh, "shwh"},
25926+#endif
25927+ {Opt_noshwh, "noshwh"},
25928+
076b876e
AM
25929+ {Opt_dirperm1, "dirperm1"},
25930+ {Opt_nodirperm1, "nodirperm1"},
25931+
1facf9fc 25932+ {Opt_verbose, "verbose"},
25933+ {Opt_verbose, "v"},
25934+ {Opt_noverbose, "noverbose"},
25935+ {Opt_noverbose, "quiet"},
25936+ {Opt_noverbose, "q"},
25937+ {Opt_noverbose, "silent"},
25938+
25939+ {Opt_sum, "sum"},
25940+ {Opt_nosum, "nosum"},
25941+ {Opt_wsum, "wsum"},
25942+
25943+ {Opt_rdcache, "rdcache=%d"},
25944+ {Opt_rdblk, "rdblk=%d"},
dece6358 25945+ {Opt_rdblk_def, "rdblk=def"},
1facf9fc 25946+ {Opt_rdhash, "rdhash=%d"},
dece6358 25947+ {Opt_rdhash_def, "rdhash=def"},
1facf9fc 25948+
25949+ {Opt_wbr_create, "create=%s"},
25950+ {Opt_wbr_create, "create_policy=%s"},
25951+ {Opt_wbr_copyup, "cpup=%s"},
25952+ {Opt_wbr_copyup, "copyup=%s"},
25953+ {Opt_wbr_copyup, "copyup_policy=%s"},
25954+
c1595e42
JR
25955+ /* generic VFS flag */
25956+#ifdef CONFIG_FS_POSIX_ACL
25957+ {Opt_acl, "acl"},
25958+ {Opt_noacl, "noacl"},
25959+#else
8b6a4947 25960+ {Opt_ignore, "acl"},
c1595e42
JR
25961+ {Opt_ignore_silent, "noacl"},
25962+#endif
25963+
1facf9fc 25964+ /* internal use for the scripts */
25965+ {Opt_ignore_silent, "si=%s"},
25966+
25967+ {Opt_br, "dirs=%s"},
25968+ {Opt_ignore, "debug=%d"},
25969+ {Opt_ignore, "delete=whiteout"},
25970+ {Opt_ignore, "delete=all"},
25971+ {Opt_ignore, "imap=%s"},
25972+
1308ab2a 25973+ /* temporary workaround, due to old mount(8)? */
25974+ {Opt_ignore_silent, "relatime"},
25975+
1facf9fc 25976+ {Opt_err, NULL}
25977+};
25978+
25979+/* ---------------------------------------------------------------------- */
25980+
076b876e 25981+static const char *au_parser_pattern(int val, match_table_t tbl)
1facf9fc 25982+{
076b876e
AM
25983+ struct match_token *p;
25984+
25985+ p = tbl;
25986+ while (p->pattern) {
25987+ if (p->token == val)
25988+ return p->pattern;
25989+ p++;
1facf9fc 25990+ }
25991+ BUG();
25992+ return "??";
25993+}
25994+
076b876e
AM
25995+static const char *au_optstr(int *val, match_table_t tbl)
25996+{
25997+ struct match_token *p;
25998+ int v;
25999+
26000+ v = *val;
2000de60
JR
26001+ if (!v)
26002+ goto out;
076b876e 26003+ p = tbl;
2000de60
JR
26004+ while (p->pattern) {
26005+ if (p->token
26006+ && (v & p->token) == p->token) {
076b876e
AM
26007+ *val &= ~p->token;
26008+ return p->pattern;
26009+ }
26010+ p++;
26011+ }
2000de60
JR
26012+
26013+out:
076b876e
AM
26014+ return NULL;
26015+}
26016+
1facf9fc 26017+/* ---------------------------------------------------------------------- */
26018+
1e00d052 26019+static match_table_t brperm = {
1facf9fc 26020+ {AuBrPerm_RO, AUFS_BRPERM_RO},
26021+ {AuBrPerm_RR, AUFS_BRPERM_RR},
26022+ {AuBrPerm_RW, AUFS_BRPERM_RW},
1e00d052
AM
26023+ {0, NULL}
26024+};
1facf9fc 26025+
86dc4139 26026+static match_table_t brattr = {
076b876e
AM
26027+ /* general */
26028+ {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
26029+ {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
c1595e42 26030+ /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
86dc4139 26031+ {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
2000de60 26032+#ifdef CONFIG_AUFS_FHSM
076b876e 26033+ {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
2000de60
JR
26034+#endif
26035+#ifdef CONFIG_AUFS_XATTR
c1595e42
JR
26036+ {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
26037+ {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
26038+ {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
26039+ {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
26040+ {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
26041+ {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
2000de60 26042+#endif
076b876e
AM
26043+
26044+ /* ro/rr branch */
1e00d052 26045+ {AuBrRAttr_WH, AUFS_BRRATTR_WH},
076b876e
AM
26046+
26047+ /* rw branch */
26048+ {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
1e00d052 26049+ {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
076b876e 26050+
1e00d052 26051+ {0, NULL}
1facf9fc 26052+};
26053+
1e00d052
AM
26054+static int br_attr_val(char *str, match_table_t table, substring_t args[])
26055+{
26056+ int attr, v;
26057+ char *p;
26058+
26059+ attr = 0;
26060+ do {
26061+ p = strchr(str, '+');
26062+ if (p)
26063+ *p = 0;
26064+ v = match_token(str, table, args);
076b876e
AM
26065+ if (v) {
26066+ if (v & AuBrAttr_CMOO_Mask)
26067+ attr &= ~AuBrAttr_CMOO_Mask;
1e00d052 26068+ attr |= v;
076b876e 26069+ } else {
1e00d052
AM
26070+ if (p)
26071+ *p = '+';
0c3ec466 26072+ pr_warn("ignored branch attribute %s\n", str);
1e00d052
AM
26073+ break;
26074+ }
26075+ if (p)
26076+ str = p + 1;
26077+ } while (p);
26078+
26079+ return attr;
26080+}
26081+
076b876e
AM
26082+static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
26083+{
26084+ int sz;
26085+ const char *p;
26086+ char *q;
26087+
076b876e
AM
26088+ q = str->a;
26089+ *q = 0;
26090+ p = au_optstr(&perm, brattr);
26091+ if (p) {
26092+ sz = strlen(p);
26093+ memcpy(q, p, sz + 1);
26094+ q += sz;
26095+ } else
26096+ goto out;
26097+
26098+ do {
26099+ p = au_optstr(&perm, brattr);
26100+ if (p) {
26101+ *q++ = '+';
26102+ sz = strlen(p);
26103+ memcpy(q, p, sz + 1);
26104+ q += sz;
26105+ }
26106+ } while (p);
26107+
26108+out:
c1595e42 26109+ return q - str->a;
076b876e
AM
26110+}
26111+
4a4d8108 26112+static int noinline_for_stack br_perm_val(char *perm)
1facf9fc 26113+{
076b876e
AM
26114+ int val, bad, sz;
26115+ char *p;
1facf9fc 26116+ substring_t args[MAX_OPT_ARGS];
076b876e 26117+ au_br_perm_str_t attr;
1facf9fc 26118+
1e00d052
AM
26119+ p = strchr(perm, '+');
26120+ if (p)
26121+ *p = 0;
26122+ val = match_token(perm, brperm, args);
26123+ if (!val) {
26124+ if (p)
26125+ *p = '+';
0c3ec466 26126+ pr_warn("ignored branch permission %s\n", perm);
1e00d052
AM
26127+ val = AuBrPerm_RO;
26128+ goto out;
26129+ }
26130+ if (!p)
26131+ goto out;
26132+
076b876e
AM
26133+ val |= br_attr_val(p + 1, brattr, args);
26134+
26135+ bad = 0;
86dc4139 26136+ switch (val & AuBrPerm_Mask) {
1e00d052
AM
26137+ case AuBrPerm_RO:
26138+ case AuBrPerm_RR:
076b876e
AM
26139+ bad = val & AuBrWAttr_Mask;
26140+ val &= ~AuBrWAttr_Mask;
1e00d052
AM
26141+ break;
26142+ case AuBrPerm_RW:
076b876e
AM
26143+ bad = val & AuBrRAttr_Mask;
26144+ val &= ~AuBrRAttr_Mask;
1e00d052
AM
26145+ break;
26146+ }
c1595e42
JR
26147+
26148+ /*
26149+ * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
26150+ * does not treat it as an error, just warning.
26151+ * this is a tiny guard for the user operation.
26152+ */
26153+ if (val & AuBrAttr_UNPIN) {
26154+ bad |= AuBrAttr_UNPIN;
26155+ val &= ~AuBrAttr_UNPIN;
26156+ }
26157+
076b876e
AM
26158+ if (unlikely(bad)) {
26159+ sz = au_do_optstr_br_attr(&attr, bad);
26160+ AuDebugOn(!sz);
26161+ pr_warn("ignored branch attribute %s\n", attr.a);
26162+ }
1e00d052
AM
26163+
26164+out:
1facf9fc 26165+ return val;
26166+}
26167+
076b876e 26168+void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
1facf9fc 26169+{
076b876e
AM
26170+ au_br_perm_str_t attr;
26171+ const char *p;
26172+ char *q;
1e00d052
AM
26173+ int sz;
26174+
076b876e
AM
26175+ q = str->a;
26176+ p = au_optstr(&perm, brperm);
26177+ AuDebugOn(!p || !*p);
26178+ sz = strlen(p);
26179+ memcpy(q, p, sz + 1);
26180+ q += sz;
1e00d052 26181+
076b876e
AM
26182+ sz = au_do_optstr_br_attr(&attr, perm);
26183+ if (sz) {
26184+ *q++ = '+';
26185+ memcpy(q, attr.a, sz + 1);
1e00d052
AM
26186+ }
26187+
076b876e 26188+ AuDebugOn(strlen(str->a) >= sizeof(str->a));
1facf9fc 26189+}
26190+
26191+/* ---------------------------------------------------------------------- */
26192+
26193+static match_table_t udbalevel = {
26194+ {AuOpt_UDBA_REVAL, "reval"},
26195+ {AuOpt_UDBA_NONE, "none"},
4a4d8108
AM
26196+#ifdef CONFIG_AUFS_HNOTIFY
26197+ {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
26198+#ifdef CONFIG_AUFS_HFSNOTIFY
26199+ {AuOpt_UDBA_HNOTIFY, "fsnotify"},
4a4d8108 26200+#endif
1facf9fc 26201+#endif
26202+ {-1, NULL}
26203+};
26204+
4a4d8108 26205+static int noinline_for_stack udba_val(char *str)
1facf9fc 26206+{
26207+ substring_t args[MAX_OPT_ARGS];
26208+
7f207e10 26209+ return match_token(str, udbalevel, args);
1facf9fc 26210+}
26211+
26212+const char *au_optstr_udba(int udba)
26213+{
076b876e 26214+ return au_parser_pattern(udba, udbalevel);
1facf9fc 26215+}
26216+
26217+/* ---------------------------------------------------------------------- */
26218+
26219+static match_table_t au_wbr_create_policy = {
26220+ {AuWbrCreate_TDP, "tdp"},
26221+ {AuWbrCreate_TDP, "top-down-parent"},
26222+ {AuWbrCreate_RR, "rr"},
26223+ {AuWbrCreate_RR, "round-robin"},
26224+ {AuWbrCreate_MFS, "mfs"},
26225+ {AuWbrCreate_MFS, "most-free-space"},
26226+ {AuWbrCreate_MFSV, "mfs:%d"},
26227+ {AuWbrCreate_MFSV, "most-free-space:%d"},
26228+
f2c43d5f
AM
26229+ /* top-down regardless the parent, and then mfs */
26230+ {AuWbrCreate_TDMFS, "tdmfs:%d"},
26231+ {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
26232+
1facf9fc 26233+ {AuWbrCreate_MFSRR, "mfsrr:%d"},
26234+ {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
26235+ {AuWbrCreate_PMFS, "pmfs"},
26236+ {AuWbrCreate_PMFSV, "pmfs:%d"},
392086de
AM
26237+ {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
26238+ {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
1facf9fc 26239+
26240+ {-1, NULL}
26241+};
26242+
1facf9fc 26243+static int au_wbr_mfs_wmark(substring_t *arg, char *str,
26244+ struct au_opt_wbr_create *create)
26245+{
26246+ int err;
26247+ unsigned long long ull;
26248+
26249+ err = 0;
a2654f78 26250+ if (!match_u64(arg, &ull))
1facf9fc 26251+ create->mfsrr_watermark = ull;
26252+ else {
4a4d8108 26253+ pr_err("bad integer in %s\n", str);
1facf9fc 26254+ err = -EINVAL;
26255+ }
26256+
26257+ return err;
26258+}
26259+
26260+static int au_wbr_mfs_sec(substring_t *arg, char *str,
26261+ struct au_opt_wbr_create *create)
26262+{
26263+ int n, err;
26264+
26265+ err = 0;
027c5e7a 26266+ if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
1facf9fc 26267+ create->mfs_second = n;
26268+ else {
4a4d8108 26269+ pr_err("bad integer in %s\n", str);
1facf9fc 26270+ err = -EINVAL;
26271+ }
26272+
26273+ return err;
26274+}
26275+
4a4d8108
AM
26276+static int noinline_for_stack
26277+au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
1facf9fc 26278+{
26279+ int err, e;
26280+ substring_t args[MAX_OPT_ARGS];
26281+
26282+ err = match_token(str, au_wbr_create_policy, args);
26283+ create->wbr_create = err;
26284+ switch (err) {
26285+ case AuWbrCreate_MFSRRV:
f2c43d5f 26286+ case AuWbrCreate_TDMFSV:
392086de 26287+ case AuWbrCreate_PMFSRRV:
1facf9fc 26288+ e = au_wbr_mfs_wmark(&args[0], str, create);
26289+ if (!e)
26290+ e = au_wbr_mfs_sec(&args[1], str, create);
26291+ if (unlikely(e))
26292+ err = e;
26293+ break;
26294+ case AuWbrCreate_MFSRR:
f2c43d5f 26295+ case AuWbrCreate_TDMFS:
392086de 26296+ case AuWbrCreate_PMFSRR:
1facf9fc 26297+ e = au_wbr_mfs_wmark(&args[0], str, create);
26298+ if (unlikely(e)) {
26299+ err = e;
26300+ break;
26301+ }
26302+ /*FALLTHROUGH*/
26303+ case AuWbrCreate_MFS:
26304+ case AuWbrCreate_PMFS:
027c5e7a 26305+ create->mfs_second = AUFS_MFS_DEF_SEC;
1facf9fc 26306+ break;
26307+ case AuWbrCreate_MFSV:
26308+ case AuWbrCreate_PMFSV:
26309+ e = au_wbr_mfs_sec(&args[0], str, create);
26310+ if (unlikely(e))
26311+ err = e;
26312+ break;
26313+ }
26314+
26315+ return err;
26316+}
26317+
26318+const char *au_optstr_wbr_create(int wbr_create)
26319+{
076b876e 26320+ return au_parser_pattern(wbr_create, au_wbr_create_policy);
1facf9fc 26321+}
26322+
26323+static match_table_t au_wbr_copyup_policy = {
26324+ {AuWbrCopyup_TDP, "tdp"},
26325+ {AuWbrCopyup_TDP, "top-down-parent"},
26326+ {AuWbrCopyup_BUP, "bup"},
26327+ {AuWbrCopyup_BUP, "bottom-up-parent"},
26328+ {AuWbrCopyup_BU, "bu"},
26329+ {AuWbrCopyup_BU, "bottom-up"},
26330+ {-1, NULL}
26331+};
26332+
4a4d8108 26333+static int noinline_for_stack au_wbr_copyup_val(char *str)
1facf9fc 26334+{
26335+ substring_t args[MAX_OPT_ARGS];
26336+
26337+ return match_token(str, au_wbr_copyup_policy, args);
26338+}
26339+
26340+const char *au_optstr_wbr_copyup(int wbr_copyup)
26341+{
076b876e 26342+ return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
1facf9fc 26343+}
26344+
26345+/* ---------------------------------------------------------------------- */
26346+
26347+static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
26348+
26349+static void dump_opts(struct au_opts *opts)
26350+{
26351+#ifdef CONFIG_AUFS_DEBUG
26352+ /* reduce stack space */
26353+ union {
26354+ struct au_opt_add *add;
26355+ struct au_opt_del *del;
26356+ struct au_opt_mod *mod;
26357+ struct au_opt_xino *xino;
26358+ struct au_opt_xino_itrunc *xino_itrunc;
26359+ struct au_opt_wbr_create *create;
26360+ } u;
26361+ struct au_opt *opt;
26362+
26363+ opt = opts->opt;
26364+ while (opt->type != Opt_tail) {
26365+ switch (opt->type) {
26366+ case Opt_add:
26367+ u.add = &opt->add;
26368+ AuDbg("add {b%d, %s, 0x%x, %p}\n",
26369+ u.add->bindex, u.add->pathname, u.add->perm,
26370+ u.add->path.dentry);
26371+ break;
26372+ case Opt_del:
26373+ case Opt_idel:
26374+ u.del = &opt->del;
26375+ AuDbg("del {%s, %p}\n",
26376+ u.del->pathname, u.del->h_path.dentry);
26377+ break;
26378+ case Opt_mod:
26379+ case Opt_imod:
26380+ u.mod = &opt->mod;
26381+ AuDbg("mod {%s, 0x%x, %p}\n",
26382+ u.mod->path, u.mod->perm, u.mod->h_root);
26383+ break;
26384+ case Opt_append:
26385+ u.add = &opt->add;
26386+ AuDbg("append {b%d, %s, 0x%x, %p}\n",
26387+ u.add->bindex, u.add->pathname, u.add->perm,
26388+ u.add->path.dentry);
26389+ break;
26390+ case Opt_prepend:
26391+ u.add = &opt->add;
26392+ AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
26393+ u.add->bindex, u.add->pathname, u.add->perm,
26394+ u.add->path.dentry);
26395+ break;
26396+ case Opt_dirwh:
26397+ AuDbg("dirwh %d\n", opt->dirwh);
26398+ break;
26399+ case Opt_rdcache:
26400+ AuDbg("rdcache %d\n", opt->rdcache);
26401+ break;
26402+ case Opt_rdblk:
26403+ AuDbg("rdblk %u\n", opt->rdblk);
26404+ break;
dece6358
AM
26405+ case Opt_rdblk_def:
26406+ AuDbg("rdblk_def\n");
26407+ break;
1facf9fc 26408+ case Opt_rdhash:
26409+ AuDbg("rdhash %u\n", opt->rdhash);
26410+ break;
dece6358
AM
26411+ case Opt_rdhash_def:
26412+ AuDbg("rdhash_def\n");
26413+ break;
1facf9fc 26414+ case Opt_xino:
26415+ u.xino = &opt->xino;
523b37e3 26416+ AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
1facf9fc 26417+ break;
26418+ case Opt_trunc_xino:
26419+ AuLabel(trunc_xino);
26420+ break;
26421+ case Opt_notrunc_xino:
26422+ AuLabel(notrunc_xino);
26423+ break;
26424+ case Opt_trunc_xino_path:
26425+ case Opt_itrunc_xino:
26426+ u.xino_itrunc = &opt->xino_itrunc;
26427+ AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
26428+ break;
1facf9fc 26429+ case Opt_noxino:
26430+ AuLabel(noxino);
26431+ break;
26432+ case Opt_trunc_xib:
26433+ AuLabel(trunc_xib);
26434+ break;
26435+ case Opt_notrunc_xib:
26436+ AuLabel(notrunc_xib);
26437+ break;
dece6358
AM
26438+ case Opt_shwh:
26439+ AuLabel(shwh);
26440+ break;
26441+ case Opt_noshwh:
26442+ AuLabel(noshwh);
26443+ break;
076b876e
AM
26444+ case Opt_dirperm1:
26445+ AuLabel(dirperm1);
26446+ break;
26447+ case Opt_nodirperm1:
26448+ AuLabel(nodirperm1);
26449+ break;
1facf9fc 26450+ case Opt_plink:
26451+ AuLabel(plink);
26452+ break;
26453+ case Opt_noplink:
26454+ AuLabel(noplink);
26455+ break;
26456+ case Opt_list_plink:
26457+ AuLabel(list_plink);
26458+ break;
26459+ case Opt_udba:
26460+ AuDbg("udba %d, %s\n",
26461+ opt->udba, au_optstr_udba(opt->udba));
26462+ break;
4a4d8108
AM
26463+ case Opt_dio:
26464+ AuLabel(dio);
26465+ break;
26466+ case Opt_nodio:
26467+ AuLabel(nodio);
26468+ break;
1facf9fc 26469+ case Opt_diropq_a:
26470+ AuLabel(diropq_a);
26471+ break;
26472+ case Opt_diropq_w:
26473+ AuLabel(diropq_w);
26474+ break;
26475+ case Opt_warn_perm:
26476+ AuLabel(warn_perm);
26477+ break;
26478+ case Opt_nowarn_perm:
26479+ AuLabel(nowarn_perm);
26480+ break;
1facf9fc 26481+ case Opt_verbose:
26482+ AuLabel(verbose);
26483+ break;
26484+ case Opt_noverbose:
26485+ AuLabel(noverbose);
26486+ break;
26487+ case Opt_sum:
26488+ AuLabel(sum);
26489+ break;
26490+ case Opt_nosum:
26491+ AuLabel(nosum);
26492+ break;
26493+ case Opt_wsum:
26494+ AuLabel(wsum);
26495+ break;
26496+ case Opt_wbr_create:
26497+ u.create = &opt->wbr_create;
26498+ AuDbg("create %d, %s\n", u.create->wbr_create,
26499+ au_optstr_wbr_create(u.create->wbr_create));
26500+ switch (u.create->wbr_create) {
26501+ case AuWbrCreate_MFSV:
26502+ case AuWbrCreate_PMFSV:
26503+ AuDbg("%d sec\n", u.create->mfs_second);
26504+ break;
26505+ case AuWbrCreate_MFSRR:
f2c43d5f 26506+ case AuWbrCreate_TDMFS:
1facf9fc 26507+ AuDbg("%llu watermark\n",
26508+ u.create->mfsrr_watermark);
26509+ break;
26510+ case AuWbrCreate_MFSRRV:
f2c43d5f 26511+ case AuWbrCreate_TDMFSV:
392086de 26512+ case AuWbrCreate_PMFSRRV:
1facf9fc 26513+ AuDbg("%llu watermark, %d sec\n",
26514+ u.create->mfsrr_watermark,
26515+ u.create->mfs_second);
26516+ break;
26517+ }
26518+ break;
26519+ case Opt_wbr_copyup:
26520+ AuDbg("copyup %d, %s\n", opt->wbr_copyup,
26521+ au_optstr_wbr_copyup(opt->wbr_copyup));
26522+ break;
076b876e
AM
26523+ case Opt_fhsm_sec:
26524+ AuDbg("fhsm_sec %u\n", opt->fhsm_second);
26525+ break;
8b6a4947
AM
26526+ case Opt_dirren:
26527+ AuLabel(dirren);
26528+ break;
26529+ case Opt_nodirren:
26530+ AuLabel(nodirren);
26531+ break;
c1595e42
JR
26532+ case Opt_acl:
26533+ AuLabel(acl);
26534+ break;
26535+ case Opt_noacl:
26536+ AuLabel(noacl);
26537+ break;
1facf9fc 26538+ default:
26539+ BUG();
26540+ }
26541+ opt++;
26542+ }
26543+#endif
26544+}
26545+
26546+void au_opts_free(struct au_opts *opts)
26547+{
26548+ struct au_opt *opt;
26549+
26550+ opt = opts->opt;
26551+ while (opt->type != Opt_tail) {
26552+ switch (opt->type) {
26553+ case Opt_add:
26554+ case Opt_append:
26555+ case Opt_prepend:
26556+ path_put(&opt->add.path);
26557+ break;
26558+ case Opt_del:
26559+ case Opt_idel:
26560+ path_put(&opt->del.h_path);
26561+ break;
26562+ case Opt_mod:
26563+ case Opt_imod:
26564+ dput(opt->mod.h_root);
26565+ break;
26566+ case Opt_xino:
26567+ fput(opt->xino.file);
26568+ break;
26569+ }
26570+ opt++;
26571+ }
26572+}
26573+
26574+static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
26575+ aufs_bindex_t bindex)
26576+{
26577+ int err;
26578+ struct au_opt_add *add = &opt->add;
26579+ char *p;
26580+
26581+ add->bindex = bindex;
1e00d052 26582+ add->perm = AuBrPerm_RO;
1facf9fc 26583+ add->pathname = opt_str;
26584+ p = strchr(opt_str, '=');
26585+ if (p) {
26586+ *p++ = 0;
26587+ if (*p)
26588+ add->perm = br_perm_val(p);
26589+ }
26590+
26591+ err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
26592+ if (!err) {
26593+ if (!p) {
26594+ add->perm = AuBrPerm_RO;
26595+ if (au_test_fs_rr(add->path.dentry->d_sb))
26596+ add->perm = AuBrPerm_RR;
2121bcd9 26597+ else if (!bindex && !(sb_flags & SB_RDONLY))
1facf9fc 26598+ add->perm = AuBrPerm_RW;
26599+ }
26600+ opt->type = Opt_add;
26601+ goto out;
26602+ }
4a4d8108 26603+ pr_err("lookup failed %s (%d)\n", add->pathname, err);
1facf9fc 26604+ err = -EINVAL;
26605+
4f0767ce 26606+out:
1facf9fc 26607+ return err;
26608+}
26609+
26610+static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
26611+{
26612+ int err;
26613+
26614+ del->pathname = args[0].from;
26615+ AuDbg("del path %s\n", del->pathname);
26616+
26617+ err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
26618+ if (unlikely(err))
4a4d8108 26619+ pr_err("lookup failed %s (%d)\n", del->pathname, err);
1facf9fc 26620+
26621+ return err;
26622+}
26623+
26624+#if 0 /* reserved for future use */
26625+static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
26626+ struct au_opt_del *del, substring_t args[])
26627+{
26628+ int err;
26629+ struct dentry *root;
26630+
26631+ err = -EINVAL;
26632+ root = sb->s_root;
26633+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26634+ if (bindex < 0 || au_sbbot(sb) < bindex) {
4a4d8108 26635+ pr_err("out of bounds, %d\n", bindex);
1facf9fc 26636+ goto out;
26637+ }
26638+
26639+ err = 0;
26640+ del->h_path.dentry = dget(au_h_dptr(root, bindex));
26641+ del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
26642+
4f0767ce 26643+out:
1facf9fc 26644+ aufs_read_unlock(root, !AuLock_IR);
26645+ return err;
26646+}
26647+#endif
26648+
4a4d8108
AM
26649+static int noinline_for_stack
26650+au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
1facf9fc 26651+{
26652+ int err;
26653+ struct path path;
26654+ char *p;
26655+
26656+ err = -EINVAL;
26657+ mod->path = args[0].from;
26658+ p = strchr(mod->path, '=');
26659+ if (unlikely(!p)) {
acd2b654 26660+ pr_err("no permission %s\n", args[0].from);
1facf9fc 26661+ goto out;
26662+ }
26663+
26664+ *p++ = 0;
26665+ err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
26666+ if (unlikely(err)) {
4a4d8108 26667+ pr_err("lookup failed %s (%d)\n", mod->path, err);
1facf9fc 26668+ goto out;
26669+ }
26670+
26671+ mod->perm = br_perm_val(p);
26672+ AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
26673+ mod->h_root = dget(path.dentry);
26674+ path_put(&path);
26675+
4f0767ce 26676+out:
1facf9fc 26677+ return err;
26678+}
26679+
26680+#if 0 /* reserved for future use */
26681+static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
26682+ struct au_opt_mod *mod, substring_t args[])
26683+{
26684+ int err;
26685+ struct dentry *root;
26686+
26687+ err = -EINVAL;
26688+ root = sb->s_root;
26689+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26690+ if (bindex < 0 || au_sbbot(sb) < bindex) {
4a4d8108 26691+ pr_err("out of bounds, %d\n", bindex);
1facf9fc 26692+ goto out;
26693+ }
26694+
26695+ err = 0;
26696+ mod->perm = br_perm_val(args[1].from);
26697+ AuDbg("mod path %s, perm 0x%x, %s\n",
26698+ mod->path, mod->perm, args[1].from);
26699+ mod->h_root = dget(au_h_dptr(root, bindex));
26700+
4f0767ce 26701+out:
1facf9fc 26702+ aufs_read_unlock(root, !AuLock_IR);
26703+ return err;
26704+}
26705+#endif
26706+
26707+static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
26708+ substring_t args[])
26709+{
26710+ int err;
26711+ struct file *file;
26712+
26713+ file = au_xino_create(sb, args[0].from, /*silent*/0);
26714+ err = PTR_ERR(file);
26715+ if (IS_ERR(file))
26716+ goto out;
26717+
26718+ err = -EINVAL;
2000de60 26719+ if (unlikely(file->f_path.dentry->d_sb == sb)) {
1facf9fc 26720+ fput(file);
4a4d8108 26721+ pr_err("%s must be outside\n", args[0].from);
1facf9fc 26722+ goto out;
26723+ }
26724+
26725+ err = 0;
26726+ xino->file = file;
26727+ xino->path = args[0].from;
26728+
4f0767ce 26729+out:
1facf9fc 26730+ return err;
26731+}
26732+
4a4d8108
AM
26733+static int noinline_for_stack
26734+au_opts_parse_xino_itrunc_path(struct super_block *sb,
26735+ struct au_opt_xino_itrunc *xino_itrunc,
26736+ substring_t args[])
1facf9fc 26737+{
26738+ int err;
5afbbe0d 26739+ aufs_bindex_t bbot, bindex;
1facf9fc 26740+ struct path path;
26741+ struct dentry *root;
26742+
26743+ err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
26744+ if (unlikely(err)) {
4a4d8108 26745+ pr_err("lookup failed %s (%d)\n", args[0].from, err);
1facf9fc 26746+ goto out;
26747+ }
26748+
26749+ xino_itrunc->bindex = -1;
26750+ root = sb->s_root;
26751+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d
AM
26752+ bbot = au_sbbot(sb);
26753+ for (bindex = 0; bindex <= bbot; bindex++) {
1facf9fc 26754+ if (au_h_dptr(root, bindex) == path.dentry) {
26755+ xino_itrunc->bindex = bindex;
26756+ break;
26757+ }
26758+ }
26759+ aufs_read_unlock(root, !AuLock_IR);
26760+ path_put(&path);
26761+
26762+ if (unlikely(xino_itrunc->bindex < 0)) {
4a4d8108 26763+ pr_err("no such branch %s\n", args[0].from);
1facf9fc 26764+ err = -EINVAL;
26765+ }
26766+
4f0767ce 26767+out:
1facf9fc 26768+ return err;
26769+}
26770+
26771+/* called without aufs lock */
26772+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
26773+{
26774+ int err, n, token;
26775+ aufs_bindex_t bindex;
26776+ unsigned char skipped;
26777+ struct dentry *root;
26778+ struct au_opt *opt, *opt_tail;
26779+ char *opt_str;
26780+ /* reduce the stack space */
26781+ union {
26782+ struct au_opt_xino_itrunc *xino_itrunc;
26783+ struct au_opt_wbr_create *create;
26784+ } u;
26785+ struct {
26786+ substring_t args[MAX_OPT_ARGS];
26787+ } *a;
26788+
26789+ err = -ENOMEM;
26790+ a = kmalloc(sizeof(*a), GFP_NOFS);
26791+ if (unlikely(!a))
26792+ goto out;
26793+
26794+ root = sb->s_root;
26795+ err = 0;
26796+ bindex = 0;
26797+ opt = opts->opt;
26798+ opt_tail = opt + opts->max_opt - 1;
26799+ opt->type = Opt_tail;
26800+ while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
26801+ err = -EINVAL;
26802+ skipped = 0;
26803+ token = match_token(opt_str, options, a->args);
26804+ switch (token) {
26805+ case Opt_br:
26806+ err = 0;
26807+ while (!err && (opt_str = strsep(&a->args[0].from, ":"))
26808+ && *opt_str) {
26809+ err = opt_add(opt, opt_str, opts->sb_flags,
26810+ bindex++);
26811+ if (unlikely(!err && ++opt > opt_tail)) {
26812+ err = -E2BIG;
26813+ break;
26814+ }
26815+ opt->type = Opt_tail;
26816+ skipped = 1;
26817+ }
26818+ break;
26819+ case Opt_add:
26820+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26821+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26822+ break;
26823+ }
26824+ bindex = n;
26825+ err = opt_add(opt, a->args[1].from, opts->sb_flags,
26826+ bindex);
26827+ if (!err)
26828+ opt->type = token;
26829+ break;
26830+ case Opt_append:
26831+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26832+ /*dummy bindex*/1);
26833+ if (!err)
26834+ opt->type = token;
26835+ break;
26836+ case Opt_prepend:
26837+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26838+ /*bindex*/0);
26839+ if (!err)
26840+ opt->type = token;
26841+ break;
26842+ case Opt_del:
26843+ err = au_opts_parse_del(&opt->del, a->args);
26844+ if (!err)
26845+ opt->type = token;
26846+ break;
26847+#if 0 /* reserved for future use */
26848+ case Opt_idel:
26849+ del->pathname = "(indexed)";
26850+ if (unlikely(match_int(&args[0], &n))) {
4a4d8108 26851+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26852+ break;
26853+ }
26854+ err = au_opts_parse_idel(sb, n, &opt->del, a->args);
26855+ if (!err)
26856+ opt->type = token;
26857+ break;
26858+#endif
26859+ case Opt_mod:
26860+ err = au_opts_parse_mod(&opt->mod, a->args);
26861+ if (!err)
26862+ opt->type = token;
26863+ break;
26864+#ifdef IMOD /* reserved for future use */
26865+ case Opt_imod:
26866+ u.mod->path = "(indexed)";
26867+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26868+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26869+ break;
26870+ }
26871+ err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
26872+ if (!err)
26873+ opt->type = token;
26874+ break;
26875+#endif
26876+ case Opt_xino:
26877+ err = au_opts_parse_xino(sb, &opt->xino, a->args);
26878+ if (!err)
26879+ opt->type = token;
26880+ break;
26881+
26882+ case Opt_trunc_xino_path:
26883+ err = au_opts_parse_xino_itrunc_path
26884+ (sb, &opt->xino_itrunc, a->args);
26885+ if (!err)
26886+ opt->type = token;
26887+ break;
26888+
26889+ case Opt_itrunc_xino:
26890+ u.xino_itrunc = &opt->xino_itrunc;
26891+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26892+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26893+ break;
26894+ }
26895+ u.xino_itrunc->bindex = n;
26896+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26897+ if (n < 0 || au_sbbot(sb) < n) {
4a4d8108 26898+ pr_err("out of bounds, %d\n", n);
1facf9fc 26899+ aufs_read_unlock(root, !AuLock_IR);
26900+ break;
26901+ }
26902+ aufs_read_unlock(root, !AuLock_IR);
26903+ err = 0;
26904+ opt->type = token;
26905+ break;
26906+
26907+ case Opt_dirwh:
26908+ if (unlikely(match_int(&a->args[0], &opt->dirwh)))
26909+ break;
26910+ err = 0;
26911+ opt->type = token;
26912+ break;
26913+
26914+ case Opt_rdcache:
027c5e7a
AM
26915+ if (unlikely(match_int(&a->args[0], &n))) {
26916+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26917+ break;
027c5e7a
AM
26918+ }
26919+ if (unlikely(n > AUFS_RDCACHE_MAX)) {
26920+ pr_err("rdcache must be smaller than %d\n",
26921+ AUFS_RDCACHE_MAX);
26922+ break;
26923+ }
26924+ opt->rdcache = n;
1facf9fc 26925+ err = 0;
26926+ opt->type = token;
26927+ break;
26928+ case Opt_rdblk:
26929+ if (unlikely(match_int(&a->args[0], &n)
1308ab2a 26930+ || n < 0
1facf9fc 26931+ || n > KMALLOC_MAX_SIZE)) {
4a4d8108 26932+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26933+ break;
26934+ }
1308ab2a 26935+ if (unlikely(n && n < NAME_MAX)) {
4a4d8108
AM
26936+ pr_err("rdblk must be larger than %d\n",
26937+ NAME_MAX);
1facf9fc 26938+ break;
26939+ }
26940+ opt->rdblk = n;
26941+ err = 0;
26942+ opt->type = token;
26943+ break;
26944+ case Opt_rdhash:
26945+ if (unlikely(match_int(&a->args[0], &n)
1308ab2a 26946+ || n < 0
1facf9fc 26947+ || n * sizeof(struct hlist_head)
26948+ > KMALLOC_MAX_SIZE)) {
4a4d8108 26949+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26950+ break;
26951+ }
26952+ opt->rdhash = n;
26953+ err = 0;
26954+ opt->type = token;
26955+ break;
26956+
26957+ case Opt_trunc_xino:
26958+ case Opt_notrunc_xino:
26959+ case Opt_noxino:
26960+ case Opt_trunc_xib:
26961+ case Opt_notrunc_xib:
dece6358
AM
26962+ case Opt_shwh:
26963+ case Opt_noshwh:
076b876e
AM
26964+ case Opt_dirperm1:
26965+ case Opt_nodirperm1:
1facf9fc 26966+ case Opt_plink:
26967+ case Opt_noplink:
26968+ case Opt_list_plink:
4a4d8108
AM
26969+ case Opt_dio:
26970+ case Opt_nodio:
1facf9fc 26971+ case Opt_diropq_a:
26972+ case Opt_diropq_w:
26973+ case Opt_warn_perm:
26974+ case Opt_nowarn_perm:
1facf9fc 26975+ case Opt_verbose:
26976+ case Opt_noverbose:
26977+ case Opt_sum:
26978+ case Opt_nosum:
26979+ case Opt_wsum:
dece6358
AM
26980+ case Opt_rdblk_def:
26981+ case Opt_rdhash_def:
8b6a4947
AM
26982+ case Opt_dirren:
26983+ case Opt_nodirren:
c1595e42
JR
26984+ case Opt_acl:
26985+ case Opt_noacl:
1facf9fc 26986+ err = 0;
26987+ opt->type = token;
26988+ break;
26989+
26990+ case Opt_udba:
26991+ opt->udba = udba_val(a->args[0].from);
26992+ if (opt->udba >= 0) {
26993+ err = 0;
26994+ opt->type = token;
26995+ } else
4a4d8108 26996+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26997+ break;
26998+
26999+ case Opt_wbr_create:
27000+ u.create = &opt->wbr_create;
27001+ u.create->wbr_create
27002+ = au_wbr_create_val(a->args[0].from, u.create);
27003+ if (u.create->wbr_create >= 0) {
27004+ err = 0;
27005+ opt->type = token;
27006+ } else
4a4d8108 27007+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 27008+ break;
27009+ case Opt_wbr_copyup:
27010+ opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
27011+ if (opt->wbr_copyup >= 0) {
27012+ err = 0;
27013+ opt->type = token;
27014+ } else
4a4d8108 27015+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 27016+ break;
27017+
076b876e
AM
27018+ case Opt_fhsm_sec:
27019+ if (unlikely(match_int(&a->args[0], &n)
27020+ || n < 0)) {
27021+ pr_err("bad integer in %s\n", opt_str);
27022+ break;
27023+ }
27024+ if (sysaufs_brs) {
27025+ opt->fhsm_second = n;
27026+ opt->type = token;
27027+ } else
27028+ pr_warn("ignored %s\n", opt_str);
27029+ err = 0;
27030+ break;
27031+
1facf9fc 27032+ case Opt_ignore:
0c3ec466 27033+ pr_warn("ignored %s\n", opt_str);
1facf9fc 27034+ /*FALLTHROUGH*/
27035+ case Opt_ignore_silent:
27036+ skipped = 1;
27037+ err = 0;
27038+ break;
27039+ case Opt_err:
4a4d8108 27040+ pr_err("unknown option %s\n", opt_str);
1facf9fc 27041+ break;
27042+ }
27043+
27044+ if (!err && !skipped) {
27045+ if (unlikely(++opt > opt_tail)) {
27046+ err = -E2BIG;
27047+ opt--;
27048+ opt->type = Opt_tail;
27049+ break;
27050+ }
27051+ opt->type = Opt_tail;
27052+ }
27053+ }
27054+
9f237c51 27055+ au_kfree_rcu(a);
1facf9fc 27056+ dump_opts(opts);
27057+ if (unlikely(err))
27058+ au_opts_free(opts);
27059+
4f0767ce 27060+out:
1facf9fc 27061+ return err;
27062+}
27063+
27064+static int au_opt_wbr_create(struct super_block *sb,
27065+ struct au_opt_wbr_create *create)
27066+{
27067+ int err;
27068+ struct au_sbinfo *sbinfo;
27069+
dece6358
AM
27070+ SiMustWriteLock(sb);
27071+
1facf9fc 27072+ err = 1; /* handled */
27073+ sbinfo = au_sbi(sb);
27074+ if (sbinfo->si_wbr_create_ops->fin) {
27075+ err = sbinfo->si_wbr_create_ops->fin(sb);
27076+ if (!err)
27077+ err = 1;
27078+ }
27079+
27080+ sbinfo->si_wbr_create = create->wbr_create;
27081+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
27082+ switch (create->wbr_create) {
27083+ case AuWbrCreate_MFSRRV:
27084+ case AuWbrCreate_MFSRR:
f2c43d5f
AM
27085+ case AuWbrCreate_TDMFS:
27086+ case AuWbrCreate_TDMFSV:
392086de
AM
27087+ case AuWbrCreate_PMFSRR:
27088+ case AuWbrCreate_PMFSRRV:
1facf9fc 27089+ sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
27090+ /*FALLTHROUGH*/
27091+ case AuWbrCreate_MFS:
27092+ case AuWbrCreate_MFSV:
27093+ case AuWbrCreate_PMFS:
27094+ case AuWbrCreate_PMFSV:
e49829fe
JR
27095+ sbinfo->si_wbr_mfs.mfs_expire
27096+ = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
1facf9fc 27097+ break;
27098+ }
27099+
27100+ if (sbinfo->si_wbr_create_ops->init)
27101+ sbinfo->si_wbr_create_ops->init(sb); /* ignore */
27102+
27103+ return err;
27104+}
27105+
27106+/*
27107+ * returns,
27108+ * plus: processed without an error
27109+ * zero: unprocessed
27110+ */
27111+static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
27112+ struct au_opts *opts)
27113+{
27114+ int err;
27115+ struct au_sbinfo *sbinfo;
27116+
dece6358
AM
27117+ SiMustWriteLock(sb);
27118+
1facf9fc 27119+ err = 1; /* handled */
27120+ sbinfo = au_sbi(sb);
27121+ switch (opt->type) {
27122+ case Opt_udba:
27123+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27124+ sbinfo->si_mntflags |= opt->udba;
27125+ opts->given_udba |= opt->udba;
27126+ break;
27127+
27128+ case Opt_plink:
27129+ au_opt_set(sbinfo->si_mntflags, PLINK);
27130+ break;
27131+ case Opt_noplink:
27132+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
e49829fe 27133+ au_plink_put(sb, /*verbose*/1);
1facf9fc 27134+ au_opt_clr(sbinfo->si_mntflags, PLINK);
27135+ break;
27136+ case Opt_list_plink:
27137+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
27138+ au_plink_list(sb);
27139+ break;
27140+
4a4d8108
AM
27141+ case Opt_dio:
27142+ au_opt_set(sbinfo->si_mntflags, DIO);
27143+ au_fset_opts(opts->flags, REFRESH_DYAOP);
27144+ break;
27145+ case Opt_nodio:
27146+ au_opt_clr(sbinfo->si_mntflags, DIO);
27147+ au_fset_opts(opts->flags, REFRESH_DYAOP);
27148+ break;
27149+
076b876e
AM
27150+ case Opt_fhsm_sec:
27151+ au_fhsm_set(sbinfo, opt->fhsm_second);
27152+ break;
27153+
1facf9fc 27154+ case Opt_diropq_a:
27155+ au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27156+ break;
27157+ case Opt_diropq_w:
27158+ au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27159+ break;
27160+
27161+ case Opt_warn_perm:
27162+ au_opt_set(sbinfo->si_mntflags, WARN_PERM);
27163+ break;
27164+ case Opt_nowarn_perm:
27165+ au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
27166+ break;
27167+
1facf9fc 27168+ case Opt_verbose:
27169+ au_opt_set(sbinfo->si_mntflags, VERBOSE);
27170+ break;
27171+ case Opt_noverbose:
27172+ au_opt_clr(sbinfo->si_mntflags, VERBOSE);
27173+ break;
27174+
27175+ case Opt_sum:
27176+ au_opt_set(sbinfo->si_mntflags, SUM);
27177+ break;
27178+ case Opt_wsum:
27179+ au_opt_clr(sbinfo->si_mntflags, SUM);
27180+ au_opt_set(sbinfo->si_mntflags, SUM_W);
27181+ case Opt_nosum:
27182+ au_opt_clr(sbinfo->si_mntflags, SUM);
27183+ au_opt_clr(sbinfo->si_mntflags, SUM_W);
27184+ break;
27185+
27186+ case Opt_wbr_create:
27187+ err = au_opt_wbr_create(sb, &opt->wbr_create);
27188+ break;
27189+ case Opt_wbr_copyup:
27190+ sbinfo->si_wbr_copyup = opt->wbr_copyup;
27191+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
27192+ break;
27193+
27194+ case Opt_dirwh:
27195+ sbinfo->si_dirwh = opt->dirwh;
27196+ break;
27197+
27198+ case Opt_rdcache:
e49829fe
JR
27199+ sbinfo->si_rdcache
27200+ = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
1facf9fc 27201+ break;
27202+ case Opt_rdblk:
27203+ sbinfo->si_rdblk = opt->rdblk;
27204+ break;
dece6358
AM
27205+ case Opt_rdblk_def:
27206+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
27207+ break;
1facf9fc 27208+ case Opt_rdhash:
27209+ sbinfo->si_rdhash = opt->rdhash;
27210+ break;
dece6358
AM
27211+ case Opt_rdhash_def:
27212+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
27213+ break;
27214+
27215+ case Opt_shwh:
27216+ au_opt_set(sbinfo->si_mntflags, SHWH);
27217+ break;
27218+ case Opt_noshwh:
27219+ au_opt_clr(sbinfo->si_mntflags, SHWH);
27220+ break;
1facf9fc 27221+
076b876e
AM
27222+ case Opt_dirperm1:
27223+ au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27224+ break;
27225+ case Opt_nodirperm1:
27226+ au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27227+ break;
27228+
1facf9fc 27229+ case Opt_trunc_xino:
27230+ au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27231+ break;
27232+ case Opt_notrunc_xino:
27233+ au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27234+ break;
27235+
27236+ case Opt_trunc_xino_path:
27237+ case Opt_itrunc_xino:
acd2b654
AM
27238+ err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27239+ /*idx_begin*/0);
1facf9fc 27240+ if (!err)
27241+ err = 1;
27242+ break;
27243+
27244+ case Opt_trunc_xib:
27245+ au_fset_opts(opts->flags, TRUNC_XIB);
27246+ break;
27247+ case Opt_notrunc_xib:
27248+ au_fclr_opts(opts->flags, TRUNC_XIB);
27249+ break;
27250+
8b6a4947
AM
27251+ case Opt_dirren:
27252+ err = 1;
27253+ if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27254+ err = au_dr_opt_set(sb);
27255+ if (!err)
27256+ err = 1;
27257+ }
27258+ if (err == 1)
27259+ au_opt_set(sbinfo->si_mntflags, DIRREN);
27260+ break;
27261+ case Opt_nodirren:
27262+ err = 1;
27263+ if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27264+ err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27265+ DR_FLUSHED));
27266+ if (!err)
27267+ err = 1;
27268+ }
27269+ if (err == 1)
27270+ au_opt_clr(sbinfo->si_mntflags, DIRREN);
27271+ break;
27272+
c1595e42 27273+ case Opt_acl:
2121bcd9 27274+ sb->s_flags |= SB_POSIXACL;
c1595e42
JR
27275+ break;
27276+ case Opt_noacl:
2121bcd9 27277+ sb->s_flags &= ~SB_POSIXACL;
c1595e42
JR
27278+ break;
27279+
1facf9fc 27280+ default:
27281+ err = 0;
27282+ break;
27283+ }
27284+
27285+ return err;
27286+}
27287+
27288+/*
27289+ * returns tri-state.
27290+ * plus: processed without an error
27291+ * zero: unprocessed
27292+ * minus: error
27293+ */
27294+static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27295+ struct au_opts *opts)
27296+{
27297+ int err, do_refresh;
27298+
27299+ err = 0;
27300+ switch (opt->type) {
27301+ case Opt_append:
5afbbe0d 27302+ opt->add.bindex = au_sbbot(sb) + 1;
1facf9fc 27303+ if (opt->add.bindex < 0)
27304+ opt->add.bindex = 0;
27305+ goto add;
27306+ case Opt_prepend:
27307+ opt->add.bindex = 0;
f6b6e03d 27308+ add: /* indented label */
1facf9fc 27309+ case Opt_add:
27310+ err = au_br_add(sb, &opt->add,
27311+ au_ftest_opts(opts->flags, REMOUNT));
27312+ if (!err) {
27313+ err = 1;
027c5e7a 27314+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27315+ }
27316+ break;
27317+
27318+ case Opt_del:
27319+ case Opt_idel:
27320+ err = au_br_del(sb, &opt->del,
27321+ au_ftest_opts(opts->flags, REMOUNT));
27322+ if (!err) {
27323+ err = 1;
27324+ au_fset_opts(opts->flags, TRUNC_XIB);
027c5e7a 27325+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27326+ }
27327+ break;
27328+
27329+ case Opt_mod:
27330+ case Opt_imod:
27331+ err = au_br_mod(sb, &opt->mod,
27332+ au_ftest_opts(opts->flags, REMOUNT),
27333+ &do_refresh);
27334+ if (!err) {
27335+ err = 1;
027c5e7a
AM
27336+ if (do_refresh)
27337+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27338+ }
27339+ break;
27340+ }
1facf9fc 27341+ return err;
27342+}
27343+
27344+static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27345+ struct au_opt_xino **opt_xino,
27346+ struct au_opts *opts)
27347+{
27348+ int err;
1facf9fc 27349+
27350+ err = 0;
27351+ switch (opt->type) {
27352+ case Opt_xino:
062440b3
AM
27353+ err = au_xino_set(sb, &opt->xino,
27354+ !!au_ftest_opts(opts->flags, REMOUNT));
27355+ if (unlikely(err))
27356+ break;
27357+
27358+ *opt_xino = &opt->xino;
1facf9fc 27359+ break;
27360+
27361+ case Opt_noxino:
27362+ au_xino_clr(sb);
1facf9fc 27363+ *opt_xino = (void *)-1;
27364+ break;
27365+ }
27366+
27367+ return err;
27368+}
27369+
27370+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27371+ unsigned int pending)
27372+{
076b876e 27373+ int err, fhsm;
5afbbe0d 27374+ aufs_bindex_t bindex, bbot;
79b8bda9 27375+ unsigned char do_plink, skip, do_free, can_no_dreval;
1facf9fc 27376+ struct au_branch *br;
27377+ struct au_wbr *wbr;
79b8bda9 27378+ struct dentry *root, *dentry;
1facf9fc 27379+ struct inode *dir, *h_dir;
27380+ struct au_sbinfo *sbinfo;
27381+ struct au_hinode *hdir;
27382+
dece6358
AM
27383+ SiMustAnyLock(sb);
27384+
1facf9fc 27385+ sbinfo = au_sbi(sb);
27386+ AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27387+
2121bcd9 27388+ if (!(sb_flags & SB_RDONLY)) {
dece6358 27389+ if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
0c3ec466 27390+ pr_warn("first branch should be rw\n");
dece6358 27391+ if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
febd17d6 27392+ pr_warn_once("shwh should be used with ro\n");
dece6358 27393+ }
1facf9fc 27394+
4a4d8108 27395+ if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
1facf9fc 27396+ && !au_opt_test(sbinfo->si_mntflags, XINO))
febd17d6 27397+ pr_warn_once("udba=*notify requires xino\n");
1facf9fc 27398+
076b876e 27399+ if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
febd17d6
JR
27400+ pr_warn_once("dirperm1 breaks the protection"
27401+ " by the permission bits on the lower branch\n");
076b876e 27402+
1facf9fc 27403+ err = 0;
076b876e 27404+ fhsm = 0;
1facf9fc 27405+ root = sb->s_root;
5527c038 27406+ dir = d_inode(root);
1facf9fc 27407+ do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
79b8bda9
AM
27408+ can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27409+ UDBA_NONE);
5afbbe0d
AM
27410+ bbot = au_sbbot(sb);
27411+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
1facf9fc 27412+ skip = 0;
27413+ h_dir = au_h_iptr(dir, bindex);
27414+ br = au_sbr(sb, bindex);
1facf9fc 27415+
c1595e42
JR
27416+ if ((br->br_perm & AuBrAttr_ICEX)
27417+ && !h_dir->i_op->listxattr)
27418+ br->br_perm &= ~AuBrAttr_ICEX;
27419+#if 0
27420+ if ((br->br_perm & AuBrAttr_ICEX_SEC)
2121bcd9 27421+ && (au_br_sb(br)->s_flags & SB_NOSEC))
c1595e42
JR
27422+ br->br_perm &= ~AuBrAttr_ICEX_SEC;
27423+#endif
27424+
27425+ do_free = 0;
1facf9fc 27426+ wbr = br->br_wbr;
27427+ if (wbr)
27428+ wbr_wh_read_lock(wbr);
27429+
1e00d052 27430+ if (!au_br_writable(br->br_perm)) {
1facf9fc 27431+ do_free = !!wbr;
27432+ skip = (!wbr
27433+ || (!wbr->wbr_whbase
27434+ && !wbr->wbr_plink
27435+ && !wbr->wbr_orph));
1e00d052 27436+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 27437+ /* skip = (!br->br_whbase && !br->br_orph); */
27438+ skip = (!wbr || !wbr->wbr_whbase);
27439+ if (skip && wbr) {
27440+ if (do_plink)
27441+ skip = !!wbr->wbr_plink;
27442+ else
27443+ skip = !wbr->wbr_plink;
27444+ }
1e00d052 27445+ } else {
1facf9fc 27446+ /* skip = (br->br_whbase && br->br_ohph); */
27447+ skip = (wbr && wbr->wbr_whbase);
27448+ if (skip) {
27449+ if (do_plink)
27450+ skip = !!wbr->wbr_plink;
27451+ else
27452+ skip = !wbr->wbr_plink;
27453+ }
1facf9fc 27454+ }
27455+ if (wbr)
27456+ wbr_wh_read_unlock(wbr);
27457+
79b8bda9
AM
27458+ if (can_no_dreval) {
27459+ dentry = br->br_path.dentry;
27460+ spin_lock(&dentry->d_lock);
27461+ if (dentry->d_flags &
27462+ (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27463+ can_no_dreval = 0;
27464+ spin_unlock(&dentry->d_lock);
27465+ }
27466+
076b876e
AM
27467+ if (au_br_fhsm(br->br_perm)) {
27468+ fhsm++;
27469+ AuDebugOn(!br->br_fhsm);
27470+ }
27471+
1facf9fc 27472+ if (skip)
27473+ continue;
27474+
27475+ hdir = au_hi(dir, bindex);
5afbbe0d 27476+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 27477+ if (wbr)
27478+ wbr_wh_write_lock(wbr);
86dc4139 27479+ err = au_wh_init(br, sb);
1facf9fc 27480+ if (wbr)
27481+ wbr_wh_write_unlock(wbr);
5afbbe0d 27482+ au_hn_inode_unlock(hdir);
1facf9fc 27483+
27484+ if (!err && do_free) {
9f237c51 27485+ au_kfree_rcu(wbr);
1facf9fc 27486+ br->br_wbr = NULL;
27487+ }
27488+ }
27489+
79b8bda9
AM
27490+ if (can_no_dreval)
27491+ au_fset_si(sbinfo, NO_DREVAL);
27492+ else
27493+ au_fclr_si(sbinfo, NO_DREVAL);
27494+
c1595e42 27495+ if (fhsm >= 2) {
076b876e 27496+ au_fset_si(sbinfo, FHSM);
5afbbe0d 27497+ for (bindex = bbot; bindex >= 0; bindex--) {
c1595e42
JR
27498+ br = au_sbr(sb, bindex);
27499+ if (au_br_fhsm(br->br_perm)) {
27500+ au_fhsm_set_bottom(sb, bindex);
27501+ break;
27502+ }
27503+ }
27504+ } else {
076b876e 27505+ au_fclr_si(sbinfo, FHSM);
c1595e42
JR
27506+ au_fhsm_set_bottom(sb, -1);
27507+ }
076b876e 27508+
1facf9fc 27509+ return err;
27510+}
27511+
27512+int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27513+{
27514+ int err;
27515+ unsigned int tmp;
5afbbe0d 27516+ aufs_bindex_t bindex, bbot;
1facf9fc 27517+ struct au_opt *opt;
27518+ struct au_opt_xino *opt_xino, xino;
27519+ struct au_sbinfo *sbinfo;
027c5e7a 27520+ struct au_branch *br;
076b876e 27521+ struct inode *dir;
1facf9fc 27522+
dece6358
AM
27523+ SiMustWriteLock(sb);
27524+
1facf9fc 27525+ err = 0;
27526+ opt_xino = NULL;
27527+ opt = opts->opt;
27528+ while (err >= 0 && opt->type != Opt_tail)
27529+ err = au_opt_simple(sb, opt++, opts);
27530+ if (err > 0)
27531+ err = 0;
27532+ else if (unlikely(err < 0))
27533+ goto out;
27534+
27535+ /* disable xino and udba temporary */
27536+ sbinfo = au_sbi(sb);
27537+ tmp = sbinfo->si_mntflags;
27538+ au_opt_clr(sbinfo->si_mntflags, XINO);
27539+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27540+
27541+ opt = opts->opt;
27542+ while (err >= 0 && opt->type != Opt_tail)
27543+ err = au_opt_br(sb, opt++, opts);
27544+ if (err > 0)
27545+ err = 0;
27546+ else if (unlikely(err < 0))
27547+ goto out;
27548+
5afbbe0d
AM
27549+ bbot = au_sbbot(sb);
27550+ if (unlikely(bbot < 0)) {
1facf9fc 27551+ err = -EINVAL;
4a4d8108 27552+ pr_err("no branches\n");
1facf9fc 27553+ goto out;
27554+ }
27555+
27556+ if (au_opt_test(tmp, XINO))
27557+ au_opt_set(sbinfo->si_mntflags, XINO);
27558+ opt = opts->opt;
27559+ while (!err && opt->type != Opt_tail)
27560+ err = au_opt_xino(sb, opt++, &opt_xino, opts);
27561+ if (unlikely(err))
27562+ goto out;
27563+
27564+ err = au_opts_verify(sb, sb->s_flags, tmp);
27565+ if (unlikely(err))
27566+ goto out;
27567+
27568+ /* restore xino */
27569+ if (au_opt_test(tmp, XINO) && !opt_xino) {
27570+ xino.file = au_xino_def(sb);
27571+ err = PTR_ERR(xino.file);
27572+ if (IS_ERR(xino.file))
27573+ goto out;
27574+
27575+ err = au_xino_set(sb, &xino, /*remount*/0);
27576+ fput(xino.file);
27577+ if (unlikely(err))
27578+ goto out;
27579+ }
27580+
27581+ /* restore udba */
027c5e7a 27582+ tmp &= AuOptMask_UDBA;
1facf9fc 27583+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
027c5e7a 27584+ sbinfo->si_mntflags |= tmp;
5afbbe0d
AM
27585+ bbot = au_sbbot(sb);
27586+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
27587+ br = au_sbr(sb, bindex);
27588+ err = au_hnotify_reset_br(tmp, br, br->br_perm);
27589+ if (unlikely(err))
27590+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
27591+ bindex, err);
27592+ /* go on even if err */
27593+ }
4a4d8108 27594+ if (au_opt_test(tmp, UDBA_HNOTIFY)) {
5527c038 27595+ dir = d_inode(sb->s_root);
4a4d8108 27596+ au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
1facf9fc 27597+ }
27598+
4f0767ce 27599+out:
1facf9fc 27600+ return err;
27601+}
27602+
27603+int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27604+{
27605+ int err, rerr;
79b8bda9 27606+ unsigned char no_dreval;
1facf9fc 27607+ struct inode *dir;
27608+ struct au_opt_xino *opt_xino;
27609+ struct au_opt *opt;
27610+ struct au_sbinfo *sbinfo;
27611+
dece6358
AM
27612+ SiMustWriteLock(sb);
27613+
8b6a4947
AM
27614+ err = au_dr_opt_flush(sb);
27615+ if (unlikely(err))
27616+ goto out;
27617+ au_fset_opts(opts->flags, DR_FLUSHED);
27618+
5527c038 27619+ dir = d_inode(sb->s_root);
1facf9fc 27620+ sbinfo = au_sbi(sb);
1facf9fc 27621+ opt_xino = NULL;
27622+ opt = opts->opt;
27623+ while (err >= 0 && opt->type != Opt_tail) {
27624+ err = au_opt_simple(sb, opt, opts);
27625+ if (!err)
27626+ err = au_opt_br(sb, opt, opts);
27627+ if (!err)
27628+ err = au_opt_xino(sb, opt, &opt_xino, opts);
27629+ opt++;
27630+ }
27631+ if (err > 0)
27632+ err = 0;
27633+ AuTraceErr(err);
27634+ /* go on even err */
27635+
79b8bda9 27636+ no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
1facf9fc 27637+ rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27638+ if (unlikely(rerr && !err))
27639+ err = rerr;
27640+
79b8bda9 27641+ if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
b95c5147 27642+ au_fset_opts(opts->flags, REFRESH_IDOP);
79b8bda9 27643+
1facf9fc 27644+ if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27645+ rerr = au_xib_trunc(sb);
27646+ if (unlikely(rerr && !err))
27647+ err = rerr;
27648+ }
27649+
27650+ /* will be handled by the caller */
027c5e7a 27651+ if (!au_ftest_opts(opts->flags, REFRESH)
79b8bda9
AM
27652+ && (opts->given_udba
27653+ || au_opt_test(sbinfo->si_mntflags, XINO)
b95c5147 27654+ || au_ftest_opts(opts->flags, REFRESH_IDOP)
79b8bda9 27655+ ))
027c5e7a 27656+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27657+
27658+ AuDbg("status 0x%x\n", opts->flags);
8b6a4947
AM
27659+
27660+out:
1facf9fc 27661+ return err;
27662+}
27663+
27664+/* ---------------------------------------------------------------------- */
27665+
27666+unsigned int au_opt_udba(struct super_block *sb)
27667+{
27668+ return au_mntflags(sb) & AuOptMask_UDBA;
27669+}
7f207e10
AM
27670diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27671--- /usr/share/empty/fs/aufs/opts.h 1970-01-01 01:00:00.000000000 +0100
ba1aed25 27672+++ linux/fs/aufs/opts.h 2019-03-05 12:13:00.142557771 +0100
062440b3
AM
27673@@ -0,0 +1,225 @@
27674+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 27675+/*
ba1aed25 27676+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 27677+ *
27678+ * This program, aufs is free software; you can redistribute it and/or modify
27679+ * it under the terms of the GNU General Public License as published by
27680+ * the Free Software Foundation; either version 2 of the License, or
27681+ * (at your option) any later version.
dece6358
AM
27682+ *
27683+ * This program is distributed in the hope that it will be useful,
27684+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27685+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27686+ * GNU General Public License for more details.
27687+ *
27688+ * You should have received a copy of the GNU General Public License
523b37e3 27689+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 27690+ */
27691+
27692+/*
27693+ * mount options/flags
27694+ */
27695+
27696+#ifndef __AUFS_OPTS_H__
27697+#define __AUFS_OPTS_H__
27698+
27699+#ifdef __KERNEL__
27700+
dece6358 27701+#include <linux/path.h>
1facf9fc 27702+
dece6358 27703+struct file;
dece6358 27704+
1facf9fc 27705+/* ---------------------------------------------------------------------- */
27706+
27707+/* mount flags */
27708+#define AuOpt_XINO 1 /* external inode number bitmap
27709+ and translation table */
27710+#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
27711+#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
27712+#define AuOpt_UDBA_REVAL (1 << 3)
4a4d8108 27713+#define AuOpt_UDBA_HNOTIFY (1 << 4)
dece6358
AM
27714+#define AuOpt_SHWH (1 << 5) /* show whiteout */
27715+#define AuOpt_PLINK (1 << 6) /* pseudo-link */
076b876e
AM
27716+#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
27717+ bits */
dece6358
AM
27718+#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
27719+#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
27720+#define AuOpt_SUM_W (1 << 11) /* unimplemented */
27721+#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
27722+#define AuOpt_VERBOSE (1 << 13) /* busy inode when del-branch */
4a4d8108 27723+#define AuOpt_DIO (1 << 14) /* direct io */
8b6a4947 27724+#define AuOpt_DIRREN (1 << 15) /* directory rename */
1facf9fc 27725+
4a4d8108
AM
27726+#ifndef CONFIG_AUFS_HNOTIFY
27727+#undef AuOpt_UDBA_HNOTIFY
27728+#define AuOpt_UDBA_HNOTIFY 0
1facf9fc 27729+#endif
8b6a4947
AM
27730+#ifndef CONFIG_AUFS_DIRREN
27731+#undef AuOpt_DIRREN
27732+#define AuOpt_DIRREN 0
27733+#endif
dece6358
AM
27734+#ifndef CONFIG_AUFS_SHWH
27735+#undef AuOpt_SHWH
27736+#define AuOpt_SHWH 0
27737+#endif
1facf9fc 27738+
27739+#define AuOpt_Def (AuOpt_XINO \
27740+ | AuOpt_UDBA_REVAL \
27741+ | AuOpt_PLINK \
27742+ /* | AuOpt_DIRPERM1 */ \
27743+ | AuOpt_WARN_PERM)
27744+#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27745+ | AuOpt_UDBA_REVAL \
4a4d8108 27746+ | AuOpt_UDBA_HNOTIFY)
1facf9fc 27747+
27748+#define au_opt_test(flags, name) (flags & AuOpt_##name)
27749+#define au_opt_set(flags, name) do { \
27750+ BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27751+ ((flags) |= AuOpt_##name); \
27752+} while (0)
27753+#define au_opt_set_udba(flags, name) do { \
27754+ (flags) &= ~AuOptMask_UDBA; \
27755+ ((flags) |= AuOpt_##name); \
27756+} while (0)
7f207e10
AM
27757+#define au_opt_clr(flags, name) do { \
27758+ ((flags) &= ~AuOpt_##name); \
27759+} while (0)
1facf9fc 27760+
e49829fe
JR
27761+static inline unsigned int au_opts_plink(unsigned int mntflags)
27762+{
27763+#ifdef CONFIG_PROC_FS
27764+ return mntflags;
27765+#else
27766+ return mntflags & ~AuOpt_PLINK;
27767+#endif
27768+}
27769+
1facf9fc 27770+/* ---------------------------------------------------------------------- */
27771+
27772+/* policies to select one among multiple writable branches */
27773+enum {
27774+ AuWbrCreate_TDP, /* top down parent */
27775+ AuWbrCreate_RR, /* round robin */
27776+ AuWbrCreate_MFS, /* most free space */
27777+ AuWbrCreate_MFSV, /* mfs with seconds */
27778+ AuWbrCreate_MFSRR, /* mfs then rr */
27779+ AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
f2c43d5f
AM
27780+ AuWbrCreate_TDMFS, /* top down regardless parent and mfs */
27781+ AuWbrCreate_TDMFSV, /* top down regardless parent and mfs */
1facf9fc 27782+ AuWbrCreate_PMFS, /* parent and mfs */
27783+ AuWbrCreate_PMFSV, /* parent and mfs with seconds */
392086de
AM
27784+ AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
27785+ AuWbrCreate_PMFSRRV, /* plus seconds */
1facf9fc 27786+
27787+ AuWbrCreate_Def = AuWbrCreate_TDP
27788+};
27789+
27790+enum {
27791+ AuWbrCopyup_TDP, /* top down parent */
27792+ AuWbrCopyup_BUP, /* bottom up parent */
27793+ AuWbrCopyup_BU, /* bottom up */
27794+
27795+ AuWbrCopyup_Def = AuWbrCopyup_TDP
27796+};
27797+
27798+/* ---------------------------------------------------------------------- */
27799+
27800+struct au_opt_add {
27801+ aufs_bindex_t bindex;
27802+ char *pathname;
27803+ int perm;
27804+ struct path path;
27805+};
27806+
27807+struct au_opt_del {
27808+ char *pathname;
27809+ struct path h_path;
27810+};
27811+
27812+struct au_opt_mod {
27813+ char *path;
27814+ int perm;
27815+ struct dentry *h_root;
27816+};
27817+
27818+struct au_opt_xino {
27819+ char *path;
27820+ struct file *file;
27821+};
27822+
27823+struct au_opt_xino_itrunc {
27824+ aufs_bindex_t bindex;
27825+};
27826+
27827+struct au_opt_wbr_create {
27828+ int wbr_create;
27829+ int mfs_second;
27830+ unsigned long long mfsrr_watermark;
27831+};
27832+
27833+struct au_opt {
27834+ int type;
27835+ union {
27836+ struct au_opt_xino xino;
27837+ struct au_opt_xino_itrunc xino_itrunc;
27838+ struct au_opt_add add;
27839+ struct au_opt_del del;
27840+ struct au_opt_mod mod;
27841+ int dirwh;
27842+ int rdcache;
27843+ unsigned int rdblk;
27844+ unsigned int rdhash;
27845+ int udba;
27846+ struct au_opt_wbr_create wbr_create;
27847+ int wbr_copyup;
076b876e 27848+ unsigned int fhsm_second;
1facf9fc 27849+ };
27850+};
27851+
27852+/* opts flags */
27853+#define AuOpts_REMOUNT 1
027c5e7a
AM
27854+#define AuOpts_REFRESH (1 << 1)
27855+#define AuOpts_TRUNC_XIB (1 << 2)
27856+#define AuOpts_REFRESH_DYAOP (1 << 3)
b95c5147 27857+#define AuOpts_REFRESH_IDOP (1 << 4)
8b6a4947 27858+#define AuOpts_DR_FLUSHED (1 << 5)
1facf9fc 27859+#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
7f207e10
AM
27860+#define au_fset_opts(flags, name) \
27861+ do { (flags) |= AuOpts_##name; } while (0)
27862+#define au_fclr_opts(flags, name) \
27863+ do { (flags) &= ~AuOpts_##name; } while (0)
1facf9fc 27864+
8b6a4947
AM
27865+#ifndef CONFIG_AUFS_DIRREN
27866+#undef AuOpts_DR_FLUSHED
27867+#define AuOpts_DR_FLUSHED 0
27868+#endif
27869+
1facf9fc 27870+struct au_opts {
27871+ struct au_opt *opt;
27872+ int max_opt;
27873+
27874+ unsigned int given_udba;
27875+ unsigned int flags;
27876+ unsigned long sb_flags;
27877+};
27878+
27879+/* ---------------------------------------------------------------------- */
27880+
7e9cd9fe 27881+/* opts.c */
076b876e 27882+void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
1facf9fc 27883+const char *au_optstr_udba(int udba);
27884+const char *au_optstr_wbr_copyup(int wbr_copyup);
27885+const char *au_optstr_wbr_create(int wbr_create);
27886+
27887+void au_opts_free(struct au_opts *opts);
3c1bdaff 27888+struct super_block;
1facf9fc 27889+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
27890+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27891+ unsigned int pending);
27892+int au_opts_mount(struct super_block *sb, struct au_opts *opts);
27893+int au_opts_remount(struct super_block *sb, struct au_opts *opts);
27894+
27895+unsigned int au_opt_udba(struct super_block *sb);
27896+
1facf9fc 27897+#endif /* __KERNEL__ */
27898+#endif /* __AUFS_OPTS_H__ */
7f207e10
AM
27899diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
27900--- /usr/share/empty/fs/aufs/plink.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 27901+++ linux/fs/aufs/plink.c 2019-03-05 12:13:00.142557771 +0100
062440b3 27902@@ -0,0 +1,516 @@
cd7a4cd9 27903+// SPDX-License-Identifier: GPL-2.0
1facf9fc 27904+/*
ba1aed25 27905+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 27906+ *
27907+ * This program, aufs is free software; you can redistribute it and/or modify
27908+ * it under the terms of the GNU General Public License as published by
27909+ * the Free Software Foundation; either version 2 of the License, or
27910+ * (at your option) any later version.
dece6358
AM
27911+ *
27912+ * This program is distributed in the hope that it will be useful,
27913+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27914+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27915+ * GNU General Public License for more details.
27916+ *
27917+ * You should have received a copy of the GNU General Public License
523b37e3 27918+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 27919+ */
27920+
27921+/*
27922+ * pseudo-link
27923+ */
27924+
27925+#include "aufs.h"
27926+
27927+/*
e49829fe 27928+ * the pseudo-link maintenance mode.
1facf9fc 27929+ * during a user process maintains the pseudo-links,
27930+ * prohibit adding a new plink and branch manipulation.
e49829fe
JR
27931+ *
27932+ * Flags
27933+ * NOPLM:
27934+ * For entry functions which will handle plink, and i_mutex is already held
27935+ * in VFS.
27936+ * They cannot wait and should return an error at once.
27937+ * Callers has to check the error.
27938+ * NOPLMW:
27939+ * For entry functions which will handle plink, but i_mutex is not held
27940+ * in VFS.
27941+ * They can wait the plink maintenance mode to finish.
27942+ *
27943+ * They behave like F_SETLK and F_SETLKW.
27944+ * If the caller never handle plink, then both flags are unnecessary.
1facf9fc 27945+ */
e49829fe
JR
27946+
27947+int au_plink_maint(struct super_block *sb, int flags)
1facf9fc 27948+{
e49829fe
JR
27949+ int err;
27950+ pid_t pid, ppid;
f0c0a007 27951+ struct task_struct *parent, *prev;
e49829fe 27952+ struct au_sbinfo *sbi;
dece6358
AM
27953+
27954+ SiMustAnyLock(sb);
27955+
e49829fe
JR
27956+ err = 0;
27957+ if (!au_opt_test(au_mntflags(sb), PLINK))
27958+ goto out;
27959+
27960+ sbi = au_sbi(sb);
27961+ pid = sbi->si_plink_maint_pid;
27962+ if (!pid || pid == current->pid)
27963+ goto out;
27964+
27965+ /* todo: it highly depends upon /sbin/mount.aufs */
f0c0a007
AM
27966+ prev = NULL;
27967+ parent = current;
27968+ ppid = 0;
e49829fe 27969+ rcu_read_lock();
f0c0a007
AM
27970+ while (1) {
27971+ parent = rcu_dereference(parent->real_parent);
27972+ if (parent == prev)
27973+ break;
27974+ ppid = task_pid_vnr(parent);
27975+ if (pid == ppid) {
27976+ rcu_read_unlock();
27977+ goto out;
27978+ }
27979+ prev = parent;
27980+ }
e49829fe 27981+ rcu_read_unlock();
e49829fe
JR
27982+
27983+ if (au_ftest_lock(flags, NOPLMW)) {
027c5e7a
AM
27984+ /* if there is no i_mutex lock in VFS, we don't need to wait */
27985+ /* AuDebugOn(!lockdep_depth(current)); */
e49829fe
JR
27986+ while (sbi->si_plink_maint_pid) {
27987+ si_read_unlock(sb);
27988+ /* gave up wake_up_bit() */
27989+ wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
27990+
27991+ if (au_ftest_lock(flags, FLUSH))
27992+ au_nwt_flush(&sbi->si_nowait);
27993+ si_noflush_read_lock(sb);
27994+ }
27995+ } else if (au_ftest_lock(flags, NOPLM)) {
27996+ AuDbg("ppid %d, pid %d\n", ppid, pid);
27997+ err = -EAGAIN;
27998+ }
27999+
28000+out:
28001+ return err;
4a4d8108
AM
28002+}
28003+
e49829fe 28004+void au_plink_maint_leave(struct au_sbinfo *sbinfo)
4a4d8108 28005+{
4a4d8108 28006+ spin_lock(&sbinfo->si_plink_maint_lock);
027c5e7a 28007+ sbinfo->si_plink_maint_pid = 0;
4a4d8108 28008+ spin_unlock(&sbinfo->si_plink_maint_lock);
027c5e7a 28009+ wake_up_all(&sbinfo->si_plink_wq);
4a4d8108
AM
28010+}
28011+
e49829fe 28012+int au_plink_maint_enter(struct super_block *sb)
4a4d8108
AM
28013+{
28014+ int err;
4a4d8108
AM
28015+ struct au_sbinfo *sbinfo;
28016+
28017+ err = 0;
4a4d8108
AM
28018+ sbinfo = au_sbi(sb);
28019+ /* make sure i am the only one in this fs */
e49829fe
JR
28020+ si_write_lock(sb, AuLock_FLUSH);
28021+ if (au_opt_test(au_mntflags(sb), PLINK)) {
28022+ spin_lock(&sbinfo->si_plink_maint_lock);
28023+ if (!sbinfo->si_plink_maint_pid)
28024+ sbinfo->si_plink_maint_pid = current->pid;
28025+ else
28026+ err = -EBUSY;
28027+ spin_unlock(&sbinfo->si_plink_maint_lock);
28028+ }
4a4d8108
AM
28029+ si_write_unlock(sb);
28030+
28031+ return err;
1facf9fc 28032+}
28033+
28034+/* ---------------------------------------------------------------------- */
28035+
1facf9fc 28036+#ifdef CONFIG_AUFS_DEBUG
28037+void au_plink_list(struct super_block *sb)
28038+{
86dc4139 28039+ int i;
1facf9fc 28040+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28041+ struct hlist_bl_head *hbl;
28042+ struct hlist_bl_node *pos;
5afbbe0d 28043+ struct au_icntnr *icntnr;
1facf9fc 28044+
dece6358
AM
28045+ SiMustAnyLock(sb);
28046+
1facf9fc 28047+ sbinfo = au_sbi(sb);
28048+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28049+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28050+
86dc4139 28051+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
28052+ hbl = sbinfo->si_plink + i;
28053+ hlist_bl_lock(hbl);
28054+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
5afbbe0d 28055+ AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
8b6a4947 28056+ hlist_bl_unlock(hbl);
86dc4139 28057+ }
1facf9fc 28058+}
28059+#endif
28060+
28061+/* is the inode pseudo-linked? */
28062+int au_plink_test(struct inode *inode)
28063+{
86dc4139 28064+ int found, i;
1facf9fc 28065+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28066+ struct hlist_bl_head *hbl;
28067+ struct hlist_bl_node *pos;
5afbbe0d 28068+ struct au_icntnr *icntnr;
1facf9fc 28069+
28070+ sbinfo = au_sbi(inode->i_sb);
dece6358 28071+ AuRwMustAnyLock(&sbinfo->si_rwsem);
1facf9fc 28072+ AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
e49829fe 28073+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
1facf9fc 28074+
28075+ found = 0;
86dc4139 28076+ i = au_plink_hash(inode->i_ino);
8b6a4947
AM
28077+ hbl = sbinfo->si_plink + i;
28078+ hlist_bl_lock(hbl);
28079+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
5afbbe0d 28080+ if (&icntnr->vfs_inode == inode) {
1facf9fc 28081+ found = 1;
28082+ break;
28083+ }
8b6a4947 28084+ hlist_bl_unlock(hbl);
1facf9fc 28085+ return found;
28086+}
28087+
28088+/* ---------------------------------------------------------------------- */
28089+
28090+/*
28091+ * generate a name for plink.
28092+ * the file will be stored under AUFS_WH_PLINKDIR.
28093+ */
28094+/* 20 is max digits length of ulong 64 */
28095+#define PLINK_NAME_LEN ((20 + 1) * 2)
28096+
28097+static int plink_name(char *name, int len, struct inode *inode,
28098+ aufs_bindex_t bindex)
28099+{
28100+ int rlen;
28101+ struct inode *h_inode;
28102+
28103+ h_inode = au_h_iptr(inode, bindex);
28104+ rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
28105+ return rlen;
28106+}
28107+
7f207e10
AM
28108+struct au_do_plink_lkup_args {
28109+ struct dentry **errp;
28110+ struct qstr *tgtname;
28111+ struct dentry *h_parent;
28112+ struct au_branch *br;
28113+};
28114+
28115+static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
28116+ struct dentry *h_parent,
28117+ struct au_branch *br)
28118+{
28119+ struct dentry *h_dentry;
febd17d6 28120+ struct inode *h_inode;
7f207e10 28121+
febd17d6 28122+ h_inode = d_inode(h_parent);
be118d29 28123+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
b4510431 28124+ h_dentry = vfsub_lkup_one(tgtname, h_parent);
3c1bdaff 28125+ inode_unlock_shared(h_inode);
7f207e10
AM
28126+ return h_dentry;
28127+}
28128+
28129+static void au_call_do_plink_lkup(void *args)
28130+{
28131+ struct au_do_plink_lkup_args *a = args;
28132+ *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
28133+}
28134+
1facf9fc 28135+/* lookup the plink-ed @inode under the branch at @bindex */
28136+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
28137+{
28138+ struct dentry *h_dentry, *h_parent;
28139+ struct au_branch *br;
7f207e10 28140+ int wkq_err;
1facf9fc 28141+ char a[PLINK_NAME_LEN];
0c3ec466 28142+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 28143+
e49829fe
JR
28144+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28145+
1facf9fc 28146+ br = au_sbr(inode->i_sb, bindex);
28147+ h_parent = br->br_wbr->wbr_plink;
1facf9fc 28148+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28149+
2dfbb274 28150+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
7f207e10
AM
28151+ struct au_do_plink_lkup_args args = {
28152+ .errp = &h_dentry,
28153+ .tgtname = &tgtname,
28154+ .h_parent = h_parent,
28155+ .br = br
28156+ };
28157+
28158+ wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
28159+ if (unlikely(wkq_err))
28160+ h_dentry = ERR_PTR(wkq_err);
28161+ } else
28162+ h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
28163+
1facf9fc 28164+ return h_dentry;
28165+}
28166+
28167+/* create a pseudo-link */
28168+static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
28169+ struct dentry *h_dentry, struct au_branch *br)
28170+{
28171+ int err;
28172+ struct path h_path = {
86dc4139 28173+ .mnt = au_br_mnt(br)
1facf9fc 28174+ };
523b37e3 28175+ struct inode *h_dir, *delegated;
1facf9fc 28176+
5527c038 28177+ h_dir = d_inode(h_parent);
febd17d6 28178+ inode_lock_nested(h_dir, AuLsc_I_CHILD2);
4f0767ce 28179+again:
b4510431 28180+ h_path.dentry = vfsub_lkup_one(tgt, h_parent);
1facf9fc 28181+ err = PTR_ERR(h_path.dentry);
28182+ if (IS_ERR(h_path.dentry))
28183+ goto out;
28184+
28185+ err = 0;
28186+ /* wh.plink dir is not monitored */
7f207e10 28187+ /* todo: is it really safe? */
5527c038
JR
28188+ if (d_is_positive(h_path.dentry)
28189+ && d_inode(h_path.dentry) != d_inode(h_dentry)) {
523b37e3
AM
28190+ delegated = NULL;
28191+ err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28192+ if (unlikely(err == -EWOULDBLOCK)) {
28193+ pr_warn("cannot retry for NFSv4 delegation"
28194+ " for an internal unlink\n");
28195+ iput(delegated);
28196+ }
1facf9fc 28197+ dput(h_path.dentry);
28198+ h_path.dentry = NULL;
28199+ if (!err)
28200+ goto again;
28201+ }
5527c038 28202+ if (!err && d_is_negative(h_path.dentry)) {
523b37e3
AM
28203+ delegated = NULL;
28204+ err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28205+ if (unlikely(err == -EWOULDBLOCK)) {
28206+ pr_warn("cannot retry for NFSv4 delegation"
28207+ " for an internal link\n");
28208+ iput(delegated);
28209+ }
28210+ }
1facf9fc 28211+ dput(h_path.dentry);
28212+
4f0767ce 28213+out:
febd17d6 28214+ inode_unlock(h_dir);
1facf9fc 28215+ return err;
28216+}
28217+
28218+struct do_whplink_args {
28219+ int *errp;
28220+ struct qstr *tgt;
28221+ struct dentry *h_parent;
28222+ struct dentry *h_dentry;
28223+ struct au_branch *br;
28224+};
28225+
28226+static void call_do_whplink(void *args)
28227+{
28228+ struct do_whplink_args *a = args;
28229+ *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
28230+}
28231+
28232+static int whplink(struct dentry *h_dentry, struct inode *inode,
28233+ aufs_bindex_t bindex, struct au_branch *br)
28234+{
28235+ int err, wkq_err;
28236+ struct au_wbr *wbr;
28237+ struct dentry *h_parent;
1facf9fc 28238+ char a[PLINK_NAME_LEN];
0c3ec466 28239+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 28240+
28241+ wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
28242+ h_parent = wbr->wbr_plink;
1facf9fc 28243+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28244+
28245+ /* always superio. */
2dfbb274 28246+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
1facf9fc 28247+ struct do_whplink_args args = {
28248+ .errp = &err,
28249+ .tgt = &tgtname,
28250+ .h_parent = h_parent,
28251+ .h_dentry = h_dentry,
28252+ .br = br
28253+ };
28254+ wkq_err = au_wkq_wait(call_do_whplink, &args);
28255+ if (unlikely(wkq_err))
28256+ err = wkq_err;
28257+ } else
28258+ err = do_whplink(&tgtname, h_parent, h_dentry, br);
1facf9fc 28259+
28260+ return err;
28261+}
28262+
1facf9fc 28263+/*
28264+ * create a new pseudo-link for @h_dentry on @bindex.
28265+ * the linked inode is held in aufs @inode.
28266+ */
28267+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28268+ struct dentry *h_dentry)
28269+{
28270+ struct super_block *sb;
28271+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28272+ struct hlist_bl_head *hbl;
28273+ struct hlist_bl_node *pos;
5afbbe0d 28274+ struct au_icntnr *icntnr;
86dc4139 28275+ int found, err, cnt, i;
1facf9fc 28276+
28277+ sb = inode->i_sb;
28278+ sbinfo = au_sbi(sb);
28279+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28280+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28281+
86dc4139 28282+ found = au_plink_test(inode);
4a4d8108 28283+ if (found)
1facf9fc 28284+ return;
4a4d8108 28285+
86dc4139 28286+ i = au_plink_hash(inode->i_ino);
8b6a4947 28287+ hbl = sbinfo->si_plink + i;
5afbbe0d 28288+ au_igrab(inode);
1facf9fc 28289+
8b6a4947
AM
28290+ hlist_bl_lock(hbl);
28291+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
5afbbe0d 28292+ if (&icntnr->vfs_inode == inode) {
4a4d8108
AM
28293+ found = 1;
28294+ break;
28295+ }
1facf9fc 28296+ }
5afbbe0d
AM
28297+ if (!found) {
28298+ icntnr = container_of(inode, struct au_icntnr, vfs_inode);
8b6a4947 28299+ hlist_bl_add_head(&icntnr->plink, hbl);
5afbbe0d 28300+ }
8b6a4947 28301+ hlist_bl_unlock(hbl);
4a4d8108 28302+ if (!found) {
8b6a4947 28303+ cnt = au_hbl_count(hbl);
acd2b654 28304+#define msg "unexpectedly unbalanced or too many pseudo-links"
86dc4139
AM
28305+ if (cnt > AUFS_PLINK_WARN)
28306+ AuWarn1(msg ", %d\n", cnt);
28307+#undef msg
1facf9fc 28308+ err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
5afbbe0d
AM
28309+ if (unlikely(err)) {
28310+ pr_warn("err %d, damaged pseudo link.\n", err);
8b6a4947 28311+ au_hbl_del(&icntnr->plink, hbl);
5afbbe0d 28312+ iput(&icntnr->vfs_inode);
4a4d8108 28313+ }
5afbbe0d
AM
28314+ } else
28315+ iput(&icntnr->vfs_inode);
1facf9fc 28316+}
28317+
28318+/* free all plinks */
e49829fe 28319+void au_plink_put(struct super_block *sb, int verbose)
1facf9fc 28320+{
86dc4139 28321+ int i, warned;
1facf9fc 28322+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28323+ struct hlist_bl_head *hbl;
28324+ struct hlist_bl_node *pos, *tmp;
5afbbe0d 28325+ struct au_icntnr *icntnr;
1facf9fc 28326+
dece6358
AM
28327+ SiMustWriteLock(sb);
28328+
1facf9fc 28329+ sbinfo = au_sbi(sb);
28330+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28331+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28332+
1facf9fc 28333+ /* no spin_lock since sbinfo is write-locked */
86dc4139
AM
28334+ warned = 0;
28335+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
28336+ hbl = sbinfo->si_plink + i;
28337+ if (!warned && verbose && !hlist_bl_empty(hbl)) {
86dc4139
AM
28338+ pr_warn("pseudo-link is not flushed");
28339+ warned = 1;
28340+ }
8b6a4947 28341+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
5afbbe0d 28342+ iput(&icntnr->vfs_inode);
8b6a4947 28343+ INIT_HLIST_BL_HEAD(hbl);
86dc4139 28344+ }
1facf9fc 28345+}
28346+
e49829fe
JR
28347+void au_plink_clean(struct super_block *sb, int verbose)
28348+{
28349+ struct dentry *root;
28350+
28351+ root = sb->s_root;
28352+ aufs_write_lock(root);
28353+ if (au_opt_test(au_mntflags(sb), PLINK))
28354+ au_plink_put(sb, verbose);
28355+ aufs_write_unlock(root);
28356+}
28357+
86dc4139
AM
28358+static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28359+{
28360+ int do_put;
5afbbe0d 28361+ aufs_bindex_t btop, bbot, bindex;
86dc4139
AM
28362+
28363+ do_put = 0;
5afbbe0d
AM
28364+ btop = au_ibtop(inode);
28365+ bbot = au_ibbot(inode);
28366+ if (btop >= 0) {
28367+ for (bindex = btop; bindex <= bbot; bindex++) {
86dc4139
AM
28368+ if (!au_h_iptr(inode, bindex)
28369+ || au_ii_br_id(inode, bindex) != br_id)
28370+ continue;
28371+ au_set_h_iptr(inode, bindex, NULL, 0);
28372+ do_put = 1;
28373+ break;
28374+ }
28375+ if (do_put)
5afbbe0d 28376+ for (bindex = btop; bindex <= bbot; bindex++)
86dc4139
AM
28377+ if (au_h_iptr(inode, bindex)) {
28378+ do_put = 0;
28379+ break;
28380+ }
28381+ } else
28382+ do_put = 1;
28383+
28384+ return do_put;
28385+}
28386+
1facf9fc 28387+/* free the plinks on a branch specified by @br_id */
28388+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28389+{
28390+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28391+ struct hlist_bl_head *hbl;
28392+ struct hlist_bl_node *pos, *tmp;
5afbbe0d 28393+ struct au_icntnr *icntnr;
1facf9fc 28394+ struct inode *inode;
86dc4139 28395+ int i, do_put;
1facf9fc 28396+
dece6358
AM
28397+ SiMustWriteLock(sb);
28398+
1facf9fc 28399+ sbinfo = au_sbi(sb);
28400+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28401+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28402+
8b6a4947 28403+ /* no bit_lock since sbinfo is write-locked */
86dc4139 28404+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
28405+ hbl = sbinfo->si_plink + i;
28406+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
5afbbe0d 28407+ inode = au_igrab(&icntnr->vfs_inode);
86dc4139
AM
28408+ ii_write_lock_child(inode);
28409+ do_put = au_plink_do_half_refresh(inode, br_id);
5afbbe0d 28410+ if (do_put) {
8b6a4947 28411+ hlist_bl_del(&icntnr->plink);
5afbbe0d
AM
28412+ iput(inode);
28413+ }
86dc4139
AM
28414+ ii_write_unlock(inode);
28415+ iput(inode);
dece6358 28416+ }
dece6358
AM
28417+ }
28418+}
7f207e10
AM
28419diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28420--- /usr/share/empty/fs/aufs/poll.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 28421+++ linux/fs/aufs/poll.c 2019-03-05 12:13:00.142557771 +0100
cd7a4cd9
AM
28422@@ -0,0 +1,51 @@
28423+// SPDX-License-Identifier: GPL-2.0
dece6358 28424+/*
ba1aed25 28425+ * Copyright (C) 2005-2019 Junjiro R. Okajima
dece6358
AM
28426+ *
28427+ * This program, aufs is free software; you can redistribute it and/or modify
28428+ * it under the terms of the GNU General Public License as published by
28429+ * the Free Software Foundation; either version 2 of the License, or
28430+ * (at your option) any later version.
28431+ *
28432+ * This program is distributed in the hope that it will be useful,
28433+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28434+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28435+ * GNU General Public License for more details.
28436+ *
28437+ * You should have received a copy of the GNU General Public License
523b37e3 28438+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358
AM
28439+ */
28440+
1308ab2a 28441+/*
28442+ * poll operation
28443+ * There is only one filesystem which implements ->poll operation, currently.
28444+ */
28445+
28446+#include "aufs.h"
28447+
cd7a4cd9 28448+__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
1308ab2a 28449+{
be118d29 28450+ __poll_t mask;
1308ab2a 28451+ struct file *h_file;
1308ab2a 28452+ struct super_block *sb;
28453+
28454+ /* We should pretend an error happened. */
be118d29 28455+ mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
b912730e 28456+ sb = file->f_path.dentry->d_sb;
e49829fe 28457+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 28458+
521ced18 28459+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
062440b3
AM
28460+ if (IS_ERR(h_file)) {
28461+ AuDbg("h_file %ld\n", PTR_ERR(h_file));
1308ab2a 28462+ goto out;
062440b3 28463+ }
1308ab2a 28464+
cd7a4cd9 28465+ mask = vfs_poll(h_file, pt);
b912730e 28466+ fput(h_file); /* instead of au_read_post() */
1308ab2a 28467+
4f0767ce 28468+out:
1308ab2a 28469+ si_read_unlock(sb);
062440b3 28470+ if (mask & EPOLLERR)
b00004a5 28471+ AuDbg("mask 0x%x\n", mask);
1308ab2a 28472+ return mask;
28473+}
c1595e42
JR
28474diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28475--- /usr/share/empty/fs/aufs/posix_acl.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 28476+++ linux/fs/aufs/posix_acl.c 2019-03-05 12:13:00.142557771 +0100
062440b3 28477@@ -0,0 +1,103 @@
cd7a4cd9 28478+// SPDX-License-Identifier: GPL-2.0
c1595e42 28479+/*
ba1aed25 28480+ * Copyright (C) 2014-2019 Junjiro R. Okajima
c1595e42
JR
28481+ *
28482+ * This program, aufs is free software; you can redistribute it and/or modify
28483+ * it under the terms of the GNU General Public License as published by
28484+ * the Free Software Foundation; either version 2 of the License, or
28485+ * (at your option) any later version.
28486+ *
28487+ * This program is distributed in the hope that it will be useful,
28488+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28489+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28490+ * GNU General Public License for more details.
28491+ *
28492+ * You should have received a copy of the GNU General Public License
28493+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
28494+ */
28495+
28496+/*
28497+ * posix acl operations
28498+ */
28499+
28500+#include <linux/fs.h>
c1595e42
JR
28501+#include "aufs.h"
28502+
28503+struct posix_acl *aufs_get_acl(struct inode *inode, int type)
28504+{
28505+ struct posix_acl *acl;
28506+ int err;
28507+ aufs_bindex_t bindex;
28508+ struct inode *h_inode;
28509+ struct super_block *sb;
28510+
28511+ acl = NULL;
28512+ sb = inode->i_sb;
28513+ si_read_lock(sb, AuLock_FLUSH);
28514+ ii_read_lock_child(inode);
2121bcd9 28515+ if (!(sb->s_flags & SB_POSIXACL))
c1595e42
JR
28516+ goto out;
28517+
5afbbe0d 28518+ bindex = au_ibtop(inode);
c1595e42
JR
28519+ h_inode = au_h_iptr(inode, bindex);
28520+ if (unlikely(!h_inode
28521+ || ((h_inode->i_mode & S_IFMT)
28522+ != (inode->i_mode & S_IFMT)))) {
28523+ err = au_busy_or_stale();
28524+ acl = ERR_PTR(err);
28525+ goto out;
28526+ }
28527+
28528+ /* always topmost only */
28529+ acl = get_acl(h_inode, type);
a2654f78
AM
28530+ if (!IS_ERR_OR_NULL(acl))
28531+ set_cached_acl(inode, type, acl);
c1595e42
JR
28532+
28533+out:
28534+ ii_read_unlock(inode);
28535+ si_read_unlock(sb);
28536+
28537+ AuTraceErrPtr(acl);
28538+ return acl;
28539+}
28540+
28541+int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
28542+{
28543+ int err;
28544+ ssize_t ssz;
28545+ struct dentry *dentry;
f2c43d5f 28546+ struct au_sxattr arg = {
c1595e42
JR
28547+ .type = AU_ACL_SET,
28548+ .u.acl_set = {
28549+ .acl = acl,
28550+ .type = type
28551+ },
28552+ };
28553+
5afbbe0d
AM
28554+ IMustLock(inode);
28555+
c1595e42
JR
28556+ if (inode->i_ino == AUFS_ROOT_INO)
28557+ dentry = dget(inode->i_sb->s_root);
28558+ else {
28559+ dentry = d_find_alias(inode);
28560+ if (!dentry)
28561+ dentry = d_find_any_alias(inode);
28562+ if (!dentry) {
28563+ pr_warn("cannot handle this inode, "
28564+ "please report to aufs-users ML\n");
28565+ err = -ENOENT;
28566+ goto out;
28567+ }
28568+ }
28569+
f2c43d5f 28570+ ssz = au_sxattr(dentry, inode, &arg);
c1595e42
JR
28571+ dput(dentry);
28572+ err = ssz;
a2654f78 28573+ if (ssz >= 0) {
c1595e42 28574+ err = 0;
a2654f78
AM
28575+ set_cached_acl(inode, type, acl);
28576+ }
c1595e42
JR
28577+
28578+out:
c1595e42
JR
28579+ return err;
28580+}
7f207e10
AM
28581diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28582--- /usr/share/empty/fs/aufs/procfs.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 28583+++ linux/fs/aufs/procfs.c 2019-03-05 12:13:00.142557771 +0100
062440b3 28584@@ -0,0 +1,171 @@
cd7a4cd9 28585+// SPDX-License-Identifier: GPL-2.0
e49829fe 28586+/*
ba1aed25 28587+ * Copyright (C) 2010-2019 Junjiro R. Okajima
e49829fe
JR
28588+ *
28589+ * This program, aufs is free software; you can redistribute it and/or modify
28590+ * it under the terms of the GNU General Public License as published by
28591+ * the Free Software Foundation; either version 2 of the License, or
28592+ * (at your option) any later version.
28593+ *
28594+ * This program is distributed in the hope that it will be useful,
28595+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28596+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28597+ * GNU General Public License for more details.
28598+ *
28599+ * You should have received a copy of the GNU General Public License
523b37e3 28600+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
28601+ */
28602+
28603+/*
28604+ * procfs interfaces
28605+ */
28606+
28607+#include <linux/proc_fs.h>
28608+#include "aufs.h"
28609+
28610+static int au_procfs_plm_release(struct inode *inode, struct file *file)
28611+{
28612+ struct au_sbinfo *sbinfo;
28613+
28614+ sbinfo = file->private_data;
28615+ if (sbinfo) {
28616+ au_plink_maint_leave(sbinfo);
28617+ kobject_put(&sbinfo->si_kobj);
28618+ }
28619+
28620+ return 0;
28621+}
28622+
28623+static void au_procfs_plm_write_clean(struct file *file)
28624+{
28625+ struct au_sbinfo *sbinfo;
28626+
28627+ sbinfo = file->private_data;
28628+ if (sbinfo)
28629+ au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28630+}
28631+
28632+static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28633+{
28634+ int err;
28635+ struct super_block *sb;
28636+ struct au_sbinfo *sbinfo;
8b6a4947 28637+ struct hlist_bl_node *pos;
e49829fe
JR
28638+
28639+ err = -EBUSY;
28640+ if (unlikely(file->private_data))
28641+ goto out;
28642+
28643+ sb = NULL;
53392da6 28644+ /* don't use au_sbilist_lock() here */
8b6a4947
AM
28645+ hlist_bl_lock(&au_sbilist);
28646+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
e49829fe
JR
28647+ if (id == sysaufs_si_id(sbinfo)) {
28648+ kobject_get(&sbinfo->si_kobj);
28649+ sb = sbinfo->si_sb;
28650+ break;
28651+ }
8b6a4947 28652+ hlist_bl_unlock(&au_sbilist);
e49829fe
JR
28653+
28654+ err = -EINVAL;
28655+ if (unlikely(!sb))
28656+ goto out;
28657+
28658+ err = au_plink_maint_enter(sb);
28659+ if (!err)
28660+ /* keep kobject_get() */
28661+ file->private_data = sbinfo;
28662+ else
28663+ kobject_put(&sbinfo->si_kobj);
28664+out:
28665+ return err;
28666+}
28667+
28668+/*
28669+ * Accept a valid "si=xxxx" only.
28670+ * Once it is accepted successfully, accept "clean" too.
28671+ */
28672+static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28673+ size_t count, loff_t *ppos)
28674+{
28675+ ssize_t err;
28676+ unsigned long id;
28677+ /* last newline is allowed */
28678+ char buf[3 + sizeof(unsigned long) * 2 + 1];
28679+
28680+ err = -EACCES;
28681+ if (unlikely(!capable(CAP_SYS_ADMIN)))
28682+ goto out;
28683+
28684+ err = -EINVAL;
28685+ if (unlikely(count > sizeof(buf)))
28686+ goto out;
28687+
28688+ err = copy_from_user(buf, ubuf, count);
28689+ if (unlikely(err)) {
28690+ err = -EFAULT;
28691+ goto out;
28692+ }
28693+ buf[count] = 0;
28694+
28695+ err = -EINVAL;
28696+ if (!strcmp("clean", buf)) {
28697+ au_procfs_plm_write_clean(file);
28698+ goto out_success;
28699+ } else if (unlikely(strncmp("si=", buf, 3)))
28700+ goto out;
28701+
9dbd164d 28702+ err = kstrtoul(buf + 3, 16, &id);
e49829fe
JR
28703+ if (unlikely(err))
28704+ goto out;
28705+
28706+ err = au_procfs_plm_write_si(file, id);
28707+ if (unlikely(err))
28708+ goto out;
28709+
28710+out_success:
28711+ err = count; /* success */
28712+out:
28713+ return err;
28714+}
28715+
28716+static const struct file_operations au_procfs_plm_fop = {
28717+ .write = au_procfs_plm_write,
28718+ .release = au_procfs_plm_release,
28719+ .owner = THIS_MODULE
28720+};
28721+
28722+/* ---------------------------------------------------------------------- */
28723+
28724+static struct proc_dir_entry *au_procfs_dir;
28725+
28726+void au_procfs_fin(void)
28727+{
28728+ remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28729+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28730+}
28731+
28732+int __init au_procfs_init(void)
28733+{
28734+ int err;
28735+ struct proc_dir_entry *entry;
28736+
28737+ err = -ENOMEM;
28738+ au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28739+ if (unlikely(!au_procfs_dir))
28740+ goto out;
28741+
cd7a4cd9 28742+ entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
e49829fe
JR
28743+ au_procfs_dir, &au_procfs_plm_fop);
28744+ if (unlikely(!entry))
28745+ goto out_dir;
28746+
28747+ err = 0;
28748+ goto out; /* success */
28749+
28750+
28751+out_dir:
28752+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28753+out:
28754+ return err;
28755+}
7f207e10
AM
28756diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
28757--- /usr/share/empty/fs/aufs/rdu.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25
AM
28758+++ linux/fs/aufs/rdu.c 2019-03-05 12:13:00.142557771 +0100
28759@@ -0,0 +1,384 @@
cd7a4cd9 28760+// SPDX-License-Identifier: GPL-2.0
1308ab2a 28761+/*
ba1aed25 28762+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1308ab2a 28763+ *
28764+ * This program, aufs is free software; you can redistribute it and/or modify
28765+ * it under the terms of the GNU General Public License as published by
28766+ * the Free Software Foundation; either version 2 of the License, or
28767+ * (at your option) any later version.
28768+ *
28769+ * This program is distributed in the hope that it will be useful,
28770+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28771+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28772+ * GNU General Public License for more details.
28773+ *
28774+ * You should have received a copy of the GNU General Public License
523b37e3 28775+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1308ab2a 28776+ */
28777+
28778+/*
28779+ * readdir in userspace.
28780+ */
28781+
b752ccd1 28782+#include <linux/compat.h>
4a4d8108 28783+#include <linux/fs_stack.h>
1308ab2a 28784+#include <linux/security.h>
1308ab2a 28785+#include "aufs.h"
28786+
28787+/* bits for struct aufs_rdu.flags */
28788+#define AuRdu_CALLED 1
28789+#define AuRdu_CONT (1 << 1)
28790+#define AuRdu_FULL (1 << 2)
28791+#define au_ftest_rdu(flags, name) ((flags) & AuRdu_##name)
7f207e10
AM
28792+#define au_fset_rdu(flags, name) \
28793+ do { (flags) |= AuRdu_##name; } while (0)
28794+#define au_fclr_rdu(flags, name) \
28795+ do { (flags) &= ~AuRdu_##name; } while (0)
1308ab2a 28796+
28797+struct au_rdu_arg {
392086de 28798+ struct dir_context ctx;
1308ab2a 28799+ struct aufs_rdu *rdu;
28800+ union au_rdu_ent_ul ent;
28801+ unsigned long end;
28802+
28803+ struct super_block *sb;
28804+ int err;
28805+};
28806+
392086de 28807+static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
1308ab2a 28808+ loff_t offset, u64 h_ino, unsigned int d_type)
28809+{
28810+ int err, len;
392086de 28811+ struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
1308ab2a 28812+ struct aufs_rdu *rdu = arg->rdu;
28813+ struct au_rdu_ent ent;
28814+
28815+ err = 0;
28816+ arg->err = 0;
28817+ au_fset_rdu(rdu->cookie.flags, CALLED);
28818+ len = au_rdu_len(nlen);
28819+ if (arg->ent.ul + len < arg->end) {
28820+ ent.ino = h_ino;
28821+ ent.bindex = rdu->cookie.bindex;
28822+ ent.type = d_type;
28823+ ent.nlen = nlen;
4a4d8108
AM
28824+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
28825+ ent.type = DT_UNKNOWN;
1308ab2a 28826+
9dbd164d 28827+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 28828+ err = -EFAULT;
28829+ if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
28830+ goto out;
28831+ if (copy_to_user(arg->ent.e->name, name, nlen))
28832+ goto out;
28833+ /* the terminating NULL */
28834+ if (__put_user(0, arg->ent.e->name + nlen))
28835+ goto out;
28836+ err = 0;
28837+ /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
28838+ arg->ent.ul += len;
28839+ rdu->rent++;
28840+ } else {
28841+ err = -EFAULT;
28842+ au_fset_rdu(rdu->cookie.flags, FULL);
28843+ rdu->full = 1;
28844+ rdu->tail = arg->ent;
28845+ }
28846+
4f0767ce 28847+out:
1308ab2a 28848+ /* AuTraceErr(err); */
28849+ return err;
28850+}
28851+
28852+static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
28853+{
28854+ int err;
28855+ loff_t offset;
28856+ struct au_rdu_cookie *cookie = &arg->rdu->cookie;
28857+
92d182d2 28858+ /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
1308ab2a 28859+ offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
28860+ err = offset;
28861+ if (unlikely(offset != cookie->h_pos))
28862+ goto out;
28863+
28864+ err = 0;
28865+ do {
28866+ arg->err = 0;
28867+ au_fclr_rdu(cookie->flags, CALLED);
28868+ /* smp_mb(); */
392086de 28869+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1308ab2a 28870+ if (err >= 0)
28871+ err = arg->err;
28872+ } while (!err
28873+ && au_ftest_rdu(cookie->flags, CALLED)
28874+ && !au_ftest_rdu(cookie->flags, FULL));
28875+ cookie->h_pos = h_file->f_pos;
28876+
4f0767ce 28877+out:
1308ab2a 28878+ AuTraceErr(err);
28879+ return err;
28880+}
28881+
28882+static int au_rdu(struct file *file, struct aufs_rdu *rdu)
28883+{
28884+ int err;
5afbbe0d 28885+ aufs_bindex_t bbot;
392086de
AM
28886+ struct au_rdu_arg arg = {
28887+ .ctx = {
2000de60 28888+ .actor = au_rdu_fill
392086de
AM
28889+ }
28890+ };
1308ab2a 28891+ struct dentry *dentry;
28892+ struct inode *inode;
28893+ struct file *h_file;
28894+ struct au_rdu_cookie *cookie = &rdu->cookie;
28895+
ba1aed25
AM
28896+ /* VERIFY_WRITE */
28897+ err = !access_ok(rdu->ent.e, rdu->sz);
1308ab2a 28898+ if (unlikely(err)) {
28899+ err = -EFAULT;
28900+ AuTraceErr(err);
28901+ goto out;
28902+ }
28903+ rdu->rent = 0;
28904+ rdu->tail = rdu->ent;
28905+ rdu->full = 0;
28906+ arg.rdu = rdu;
28907+ arg.ent = rdu->ent;
28908+ arg.end = arg.ent.ul;
28909+ arg.end += rdu->sz;
28910+
28911+ err = -ENOTDIR;
5afbbe0d 28912+ if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
1308ab2a 28913+ goto out;
28914+
28915+ err = security_file_permission(file, MAY_READ);
28916+ AuTraceErr(err);
28917+ if (unlikely(err))
28918+ goto out;
28919+
2000de60 28920+ dentry = file->f_path.dentry;
5527c038 28921+ inode = d_inode(dentry);
5afbbe0d 28922+ inode_lock_shared(inode);
1308ab2a 28923+
28924+ arg.sb = inode->i_sb;
e49829fe
JR
28925+ err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
28926+ if (unlikely(err))
28927+ goto out_mtx;
027c5e7a
AM
28928+ err = au_alive_dir(dentry);
28929+ if (unlikely(err))
28930+ goto out_si;
e49829fe 28931+ /* todo: reval? */
1308ab2a 28932+ fi_read_lock(file);
28933+
28934+ err = -EAGAIN;
28935+ if (unlikely(au_ftest_rdu(cookie->flags, CONT)
28936+ && cookie->generation != au_figen(file)))
28937+ goto out_unlock;
28938+
28939+ err = 0;
28940+ if (!rdu->blk) {
28941+ rdu->blk = au_sbi(arg.sb)->si_rdblk;
28942+ if (!rdu->blk)
28943+ rdu->blk = au_dir_size(file, /*dentry*/NULL);
28944+ }
5afbbe0d
AM
28945+ bbot = au_fbtop(file);
28946+ if (cookie->bindex < bbot)
28947+ cookie->bindex = bbot;
28948+ bbot = au_fbbot_dir(file);
28949+ /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
28950+ for (; !err && cookie->bindex <= bbot;
1308ab2a 28951+ cookie->bindex++, cookie->h_pos = 0) {
4a4d8108 28952+ h_file = au_hf_dir(file, cookie->bindex);
1308ab2a 28953+ if (!h_file)
28954+ continue;
28955+
28956+ au_fclr_rdu(cookie->flags, FULL);
28957+ err = au_rdu_do(h_file, &arg);
28958+ AuTraceErr(err);
28959+ if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
28960+ break;
28961+ }
28962+ AuDbg("rent %llu\n", rdu->rent);
28963+
28964+ if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
28965+ rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
28966+ au_fset_rdu(cookie->flags, CONT);
28967+ cookie->generation = au_figen(file);
28968+ }
28969+
28970+ ii_read_lock_child(inode);
5afbbe0d 28971+ fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
1308ab2a 28972+ ii_read_unlock(inode);
28973+
4f0767ce 28974+out_unlock:
1308ab2a 28975+ fi_read_unlock(file);
027c5e7a 28976+out_si:
1308ab2a 28977+ si_read_unlock(arg.sb);
4f0767ce 28978+out_mtx:
5afbbe0d 28979+ inode_unlock_shared(inode);
4f0767ce 28980+out:
1308ab2a 28981+ AuTraceErr(err);
28982+ return err;
28983+}
28984+
28985+static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
28986+{
28987+ int err;
28988+ ino_t ino;
28989+ unsigned long long nent;
28990+ union au_rdu_ent_ul *u;
28991+ struct au_rdu_ent ent;
28992+ struct super_block *sb;
28993+
28994+ err = 0;
28995+ nent = rdu->nent;
28996+ u = &rdu->ent;
2000de60 28997+ sb = file->f_path.dentry->d_sb;
1308ab2a 28998+ si_read_lock(sb, AuLock_FLUSH);
28999+ while (nent-- > 0) {
9dbd164d 29000+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 29001+ err = copy_from_user(&ent, u->e, sizeof(ent));
4a4d8108 29002+ if (!err)
ba1aed25
AM
29003+ /* VERIFY_WRITE */
29004+ err = !access_ok(&u->e->ino, sizeof(ino));
1308ab2a 29005+ if (unlikely(err)) {
29006+ err = -EFAULT;
29007+ AuTraceErr(err);
29008+ break;
29009+ }
29010+
29011+ /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
29012+ if (!ent.wh)
29013+ err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
29014+ else
29015+ err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
29016+ &ino);
29017+ if (unlikely(err)) {
29018+ AuTraceErr(err);
29019+ break;
29020+ }
29021+
29022+ err = __put_user(ino, &u->e->ino);
29023+ if (unlikely(err)) {
29024+ err = -EFAULT;
29025+ AuTraceErr(err);
29026+ break;
29027+ }
29028+ u->ul += au_rdu_len(ent.nlen);
29029+ }
29030+ si_read_unlock(sb);
29031+
29032+ return err;
29033+}
29034+
29035+/* ---------------------------------------------------------------------- */
29036+
29037+static int au_rdu_verify(struct aufs_rdu *rdu)
29038+{
b752ccd1 29039+ AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
1308ab2a 29040+ "%llu, b%d, 0x%x, g%u}\n",
b752ccd1 29041+ rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
1308ab2a 29042+ rdu->blk,
29043+ rdu->rent, rdu->shwh, rdu->full,
29044+ rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
29045+ rdu->cookie.generation);
dece6358 29046+
b752ccd1 29047+ if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
1308ab2a 29048+ return 0;
dece6358 29049+
b752ccd1
AM
29050+ AuDbg("%u:%u\n",
29051+ rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
1308ab2a 29052+ return -EINVAL;
29053+}
29054+
29055+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
dece6358 29056+{
1308ab2a 29057+ long err, e;
29058+ struct aufs_rdu rdu;
29059+ void __user *p = (void __user *)arg;
dece6358 29060+
1308ab2a 29061+ err = copy_from_user(&rdu, p, sizeof(rdu));
29062+ if (unlikely(err)) {
29063+ err = -EFAULT;
29064+ AuTraceErr(err);
29065+ goto out;
29066+ }
29067+ err = au_rdu_verify(&rdu);
dece6358
AM
29068+ if (unlikely(err))
29069+ goto out;
29070+
1308ab2a 29071+ switch (cmd) {
29072+ case AUFS_CTL_RDU:
29073+ err = au_rdu(file, &rdu);
29074+ if (unlikely(err))
29075+ break;
dece6358 29076+
1308ab2a 29077+ e = copy_to_user(p, &rdu, sizeof(rdu));
29078+ if (unlikely(e)) {
29079+ err = -EFAULT;
29080+ AuTraceErr(err);
29081+ }
29082+ break;
29083+ case AUFS_CTL_RDU_INO:
29084+ err = au_rdu_ino(file, &rdu);
29085+ break;
29086+
29087+ default:
4a4d8108 29088+ /* err = -ENOTTY; */
1308ab2a 29089+ err = -EINVAL;
29090+ }
dece6358 29091+
4f0767ce 29092+out:
1308ab2a 29093+ AuTraceErr(err);
29094+ return err;
1facf9fc 29095+}
b752ccd1
AM
29096+
29097+#ifdef CONFIG_COMPAT
29098+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29099+{
29100+ long err, e;
29101+ struct aufs_rdu rdu;
29102+ void __user *p = compat_ptr(arg);
29103+
29104+ /* todo: get_user()? */
29105+ err = copy_from_user(&rdu, p, sizeof(rdu));
29106+ if (unlikely(err)) {
29107+ err = -EFAULT;
29108+ AuTraceErr(err);
29109+ goto out;
29110+ }
29111+ rdu.ent.e = compat_ptr(rdu.ent.ul);
29112+ err = au_rdu_verify(&rdu);
29113+ if (unlikely(err))
29114+ goto out;
29115+
29116+ switch (cmd) {
29117+ case AUFS_CTL_RDU:
29118+ err = au_rdu(file, &rdu);
29119+ if (unlikely(err))
29120+ break;
29121+
29122+ rdu.ent.ul = ptr_to_compat(rdu.ent.e);
29123+ rdu.tail.ul = ptr_to_compat(rdu.tail.e);
29124+ e = copy_to_user(p, &rdu, sizeof(rdu));
29125+ if (unlikely(e)) {
29126+ err = -EFAULT;
29127+ AuTraceErr(err);
29128+ }
29129+ break;
29130+ case AUFS_CTL_RDU_INO:
29131+ err = au_rdu_ino(file, &rdu);
29132+ break;
29133+
29134+ default:
29135+ /* err = -ENOTTY; */
29136+ err = -EINVAL;
29137+ }
29138+
4f0767ce 29139+out:
b752ccd1
AM
29140+ AuTraceErr(err);
29141+ return err;
29142+}
29143+#endif
7f207e10
AM
29144diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
29145--- /usr/share/empty/fs/aufs/rwsem.h 1970-01-01 01:00:00.000000000 +0100
ba1aed25 29146+++ linux/fs/aufs/rwsem.h 2019-03-05 12:13:00.142557771 +0100
062440b3
AM
29147@@ -0,0 +1,73 @@
29148+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 29149+/*
ba1aed25 29150+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 29151+ *
29152+ * This program, aufs is free software; you can redistribute it and/or modify
29153+ * it under the terms of the GNU General Public License as published by
29154+ * the Free Software Foundation; either version 2 of the License, or
29155+ * (at your option) any later version.
dece6358
AM
29156+ *
29157+ * This program is distributed in the hope that it will be useful,
29158+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29159+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29160+ * GNU General Public License for more details.
29161+ *
29162+ * You should have received a copy of the GNU General Public License
523b37e3 29163+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29164+ */
29165+
29166+/*
29167+ * simple read-write semaphore wrappers
29168+ */
29169+
29170+#ifndef __AUFS_RWSEM_H__
29171+#define __AUFS_RWSEM_H__
29172+
29173+#ifdef __KERNEL__
29174+
4a4d8108 29175+#include "debug.h"
dece6358 29176+
acd2b654 29177+/* in the future, the name 'au_rwsem' will be totally gone */
8b6a4947 29178+#define au_rwsem rw_semaphore
dece6358
AM
29179+
29180+/* to debug easier, do not make them inlined functions */
8b6a4947 29181+#define AuRwMustNoWaiters(rw) AuDebugOn(rwsem_is_contended(rw))
dece6358 29182+/* rwsem_is_locked() is unusable */
8b6a4947
AM
29183+#define AuRwMustReadLock(rw) AuDebugOn(!lockdep_recursing(current) \
29184+ && debug_locks \
29185+ && !lockdep_is_held_type(rw, 1))
29186+#define AuRwMustWriteLock(rw) AuDebugOn(!lockdep_recursing(current) \
29187+ && debug_locks \
29188+ && !lockdep_is_held_type(rw, 0))
29189+#define AuRwMustAnyLock(rw) AuDebugOn(!lockdep_recursing(current) \
29190+ && debug_locks \
29191+ && !lockdep_is_held(rw))
29192+#define AuRwDestroy(rw) AuDebugOn(!lockdep_recursing(current) \
29193+ && debug_locks \
29194+ && lockdep_is_held(rw))
29195+
29196+#define au_rw_init(rw) init_rwsem(rw)
dece6358 29197+
5afbbe0d
AM
29198+#define au_rw_init_wlock(rw) do { \
29199+ au_rw_init(rw); \
8b6a4947 29200+ down_write(rw); \
5afbbe0d 29201+ } while (0)
dece6358 29202+
8b6a4947
AM
29203+#define au_rw_init_wlock_nested(rw, lsc) do { \
29204+ au_rw_init(rw); \
29205+ down_write_nested(rw, lsc); \
5afbbe0d 29206+ } while (0)
dece6358 29207+
8b6a4947
AM
29208+#define au_rw_read_lock(rw) down_read(rw)
29209+#define au_rw_read_lock_nested(rw, lsc) down_read_nested(rw, lsc)
29210+#define au_rw_read_unlock(rw) up_read(rw)
29211+#define au_rw_dgrade_lock(rw) downgrade_write(rw)
29212+#define au_rw_write_lock(rw) down_write(rw)
29213+#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29214+#define au_rw_write_unlock(rw) up_write(rw)
29215+/* why is not _nested version defined? */
29216+#define au_rw_read_trylock(rw) down_read_trylock(rw)
29217+#define au_rw_write_trylock(rw) down_write_trylock(rw)
1facf9fc 29218+
29219+#endif /* __KERNEL__ */
29220+#endif /* __AUFS_RWSEM_H__ */
7f207e10
AM
29221diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
29222--- /usr/share/empty/fs/aufs/sbinfo.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 29223+++ linux/fs/aufs/sbinfo.c 2019-03-05 12:13:00.142557771 +0100
acd2b654 29224@@ -0,0 +1,313 @@
cd7a4cd9 29225+// SPDX-License-Identifier: GPL-2.0
1facf9fc 29226+/*
ba1aed25 29227+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 29228+ *
29229+ * This program, aufs is free software; you can redistribute it and/or modify
29230+ * it under the terms of the GNU General Public License as published by
29231+ * the Free Software Foundation; either version 2 of the License, or
29232+ * (at your option) any later version.
dece6358
AM
29233+ *
29234+ * This program is distributed in the hope that it will be useful,
29235+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29236+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29237+ * GNU General Public License for more details.
29238+ *
29239+ * You should have received a copy of the GNU General Public License
523b37e3 29240+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29241+ */
29242+
29243+/*
29244+ * superblock private data
29245+ */
29246+
29247+#include "aufs.h"
29248+
29249+/*
29250+ * they are necessary regardless sysfs is disabled.
29251+ */
29252+void au_si_free(struct kobject *kobj)
29253+{
86dc4139 29254+ int i;
1facf9fc 29255+ struct au_sbinfo *sbinfo;
b752ccd1 29256+ char *locked __maybe_unused; /* debug only */
1facf9fc 29257+
29258+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
86dc4139 29259+ for (i = 0; i < AuPlink_NHASH; i++)
8b6a4947 29260+ AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
f0c0a007 29261+ AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
5afbbe0d 29262+
acd2b654
AM
29263+ AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29264+ au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29265+ AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29266+ au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
1facf9fc 29267+
062440b3 29268+ dbgaufs_si_fin(sbinfo);
e49829fe 29269+ au_rw_write_lock(&sbinfo->si_rwsem);
1facf9fc 29270+ au_br_free(sbinfo);
e49829fe 29271+ au_rw_write_unlock(&sbinfo->si_rwsem);
b752ccd1 29272+
9f237c51 29273+ au_kfree_try_rcu(sbinfo->si_branch);
1facf9fc 29274+ mutex_destroy(&sbinfo->si_xib_mtx);
dece6358 29275+ AuRwDestroy(&sbinfo->si_rwsem);
1facf9fc 29276+
acd2b654
AM
29277+ au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29278+ /* si_nfiles is waited too */
9f237c51 29279+ au_kfree_rcu(sbinfo);
1facf9fc 29280+}
29281+
29282+int au_si_alloc(struct super_block *sb)
29283+{
86dc4139 29284+ int err, i;
1facf9fc 29285+ struct au_sbinfo *sbinfo;
29286+
29287+ err = -ENOMEM;
4a4d8108 29288+ sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
1facf9fc 29289+ if (unlikely(!sbinfo))
29290+ goto out;
29291+
29292+ /* will be reallocated separately */
29293+ sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29294+ if (unlikely(!sbinfo->si_branch))
febd17d6 29295+ goto out_sbinfo;
1facf9fc 29296+
1facf9fc 29297+ err = sysaufs_si_init(sbinfo);
062440b3
AM
29298+ if (!err) {
29299+ dbgaufs_si_null(sbinfo);
29300+ err = dbgaufs_si_init(sbinfo);
29301+ if (unlikely(err))
29302+ kobject_put(&sbinfo->si_kobj);
29303+ }
1facf9fc 29304+ if (unlikely(err))
29305+ goto out_br;
29306+
29307+ au_nwt_init(&sbinfo->si_nowait);
dece6358 29308+ au_rw_init_wlock(&sbinfo->si_rwsem);
b752ccd1 29309+
acd2b654
AM
29310+ au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29311+ au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
7f207e10 29312+
5afbbe0d 29313+ sbinfo->si_bbot = -1;
392086de 29314+ sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
1facf9fc 29315+
29316+ sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29317+ sbinfo->si_wbr_create = AuWbrCreate_Def;
4a4d8108
AM
29318+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29319+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
1facf9fc 29320+
076b876e
AM
29321+ au_fhsm_init(sbinfo);
29322+
e49829fe 29323+ sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
1facf9fc 29324+
392086de
AM
29325+ sbinfo->si_xino_jiffy = jiffies;
29326+ sbinfo->si_xino_expire
29327+ = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
1facf9fc 29328+ mutex_init(&sbinfo->si_xib_mtx);
1facf9fc 29329+ /* leave si_xib_last_pindex and si_xib_next_bit */
29330+
8b6a4947 29331+ INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
b912730e 29332+
e49829fe 29333+ sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
1facf9fc 29334+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29335+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29336+ sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29337+
86dc4139 29338+ for (i = 0; i < AuPlink_NHASH; i++)
8b6a4947 29339+ INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
1facf9fc 29340+ init_waitqueue_head(&sbinfo->si_plink_wq);
4a4d8108 29341+ spin_lock_init(&sbinfo->si_plink_maint_lock);
1facf9fc 29342+
8b6a4947 29343+ INIT_HLIST_BL_HEAD(&sbinfo->si_files);
523b37e3 29344+
b95c5147
AM
29345+ /* with getattr by default */
29346+ sbinfo->si_iop_array = aufs_iop;
29347+
1facf9fc 29348+ /* leave other members for sysaufs and si_mnt. */
29349+ sbinfo->si_sb = sb;
29350+ sb->s_fs_info = sbinfo;
b752ccd1 29351+ si_pid_set(sb);
1facf9fc 29352+ return 0; /* success */
29353+
4f0767ce 29354+out_br:
9f237c51 29355+ au_kfree_try_rcu(sbinfo->si_branch);
4f0767ce 29356+out_sbinfo:
9f237c51 29357+ au_kfree_rcu(sbinfo);
4f0767ce 29358+out:
1facf9fc 29359+ return err;
29360+}
29361+
e2f27e51 29362+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
1facf9fc 29363+{
29364+ int err, sz;
29365+ struct au_branch **brp;
29366+
dece6358
AM
29367+ AuRwMustWriteLock(&sbinfo->si_rwsem);
29368+
1facf9fc 29369+ err = -ENOMEM;
5afbbe0d 29370+ sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
1facf9fc 29371+ if (unlikely(!sz))
29372+ sz = sizeof(*brp);
e2f27e51
AM
29373+ brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29374+ may_shrink);
1facf9fc 29375+ if (brp) {
29376+ sbinfo->si_branch = brp;
29377+ err = 0;
29378+ }
29379+
29380+ return err;
29381+}
29382+
29383+/* ---------------------------------------------------------------------- */
29384+
29385+unsigned int au_sigen_inc(struct super_block *sb)
29386+{
29387+ unsigned int gen;
5527c038 29388+ struct inode *inode;
1facf9fc 29389+
dece6358
AM
29390+ SiMustWriteLock(sb);
29391+
1facf9fc 29392+ gen = ++au_sbi(sb)->si_generation;
29393+ au_update_digen(sb->s_root);
5527c038
JR
29394+ inode = d_inode(sb->s_root);
29395+ au_update_iigen(inode, /*half*/0);
be118d29 29396+ inode_inc_iversion(inode);
1facf9fc 29397+ return gen;
29398+}
29399+
29400+aufs_bindex_t au_new_br_id(struct super_block *sb)
29401+{
29402+ aufs_bindex_t br_id;
29403+ int i;
29404+ struct au_sbinfo *sbinfo;
29405+
dece6358
AM
29406+ SiMustWriteLock(sb);
29407+
1facf9fc 29408+ sbinfo = au_sbi(sb);
29409+ for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29410+ br_id = ++sbinfo->si_last_br_id;
7f207e10 29411+ AuDebugOn(br_id < 0);
1facf9fc 29412+ if (br_id && au_br_index(sb, br_id) < 0)
29413+ return br_id;
29414+ }
29415+
29416+ return -1;
29417+}
29418+
29419+/* ---------------------------------------------------------------------- */
29420+
e49829fe
JR
29421+/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29422+int si_read_lock(struct super_block *sb, int flags)
29423+{
29424+ int err;
29425+
29426+ err = 0;
29427+ if (au_ftest_lock(flags, FLUSH))
29428+ au_nwt_flush(&au_sbi(sb)->si_nowait);
29429+
29430+ si_noflush_read_lock(sb);
29431+ err = au_plink_maint(sb, flags);
29432+ if (unlikely(err))
29433+ si_read_unlock(sb);
29434+
29435+ return err;
29436+}
29437+
29438+int si_write_lock(struct super_block *sb, int flags)
29439+{
29440+ int err;
29441+
29442+ if (au_ftest_lock(flags, FLUSH))
29443+ au_nwt_flush(&au_sbi(sb)->si_nowait);
29444+
29445+ si_noflush_write_lock(sb);
29446+ err = au_plink_maint(sb, flags);
29447+ if (unlikely(err))
29448+ si_write_unlock(sb);
29449+
29450+ return err;
29451+}
29452+
1facf9fc 29453+/* dentry and super_block lock. call at entry point */
e49829fe 29454+int aufs_read_lock(struct dentry *dentry, int flags)
1facf9fc 29455+{
e49829fe 29456+ int err;
027c5e7a 29457+ struct super_block *sb;
e49829fe 29458+
027c5e7a
AM
29459+ sb = dentry->d_sb;
29460+ err = si_read_lock(sb, flags);
29461+ if (unlikely(err))
29462+ goto out;
29463+
29464+ if (au_ftest_lock(flags, DW))
29465+ di_write_lock_child(dentry);
29466+ else
29467+ di_read_lock_child(dentry, flags);
29468+
29469+ if (au_ftest_lock(flags, GEN)) {
29470+ err = au_digen_test(dentry, au_sigen(sb));
79b8bda9
AM
29471+ if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29472+ AuDebugOn(!err && au_dbrange_test(dentry));
29473+ else if (!err)
29474+ err = au_dbrange_test(dentry);
027c5e7a
AM
29475+ if (unlikely(err))
29476+ aufs_read_unlock(dentry, flags);
e49829fe
JR
29477+ }
29478+
027c5e7a 29479+out:
e49829fe 29480+ return err;
1facf9fc 29481+}
29482+
29483+void aufs_read_unlock(struct dentry *dentry, int flags)
29484+{
29485+ if (au_ftest_lock(flags, DW))
29486+ di_write_unlock(dentry);
29487+ else
29488+ di_read_unlock(dentry, flags);
29489+ si_read_unlock(dentry->d_sb);
29490+}
29491+
29492+void aufs_write_lock(struct dentry *dentry)
29493+{
e49829fe 29494+ si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
1facf9fc 29495+ di_write_lock_child(dentry);
29496+}
29497+
29498+void aufs_write_unlock(struct dentry *dentry)
29499+{
29500+ di_write_unlock(dentry);
29501+ si_write_unlock(dentry->d_sb);
29502+}
29503+
e49829fe 29504+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
1facf9fc 29505+{
e49829fe 29506+ int err;
027c5e7a
AM
29507+ unsigned int sigen;
29508+ struct super_block *sb;
e49829fe 29509+
027c5e7a
AM
29510+ sb = d1->d_sb;
29511+ err = si_read_lock(sb, flags);
29512+ if (unlikely(err))
29513+ goto out;
29514+
b95c5147 29515+ di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
027c5e7a
AM
29516+
29517+ if (au_ftest_lock(flags, GEN)) {
29518+ sigen = au_sigen(sb);
29519+ err = au_digen_test(d1, sigen);
29520+ AuDebugOn(!err && au_dbrange_test(d1));
29521+ if (!err) {
29522+ err = au_digen_test(d2, sigen);
29523+ AuDebugOn(!err && au_dbrange_test(d2));
29524+ }
29525+ if (unlikely(err))
29526+ aufs_read_and_write_unlock2(d1, d2);
29527+ }
29528+
29529+out:
e49829fe 29530+ return err;
1facf9fc 29531+}
29532+
29533+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29534+{
29535+ di_write_unlock2(d1, d2);
29536+ si_read_unlock(d1->d_sb);
29537+}
7f207e10
AM
29538diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29539--- /usr/share/empty/fs/aufs/super.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 29540+++ linux/fs/aufs/super.c 2019-03-05 12:13:00.142557771 +0100
acd2b654 29541@@ -0,0 +1,1048 @@
cd7a4cd9 29542+// SPDX-License-Identifier: GPL-2.0
1facf9fc 29543+/*
ba1aed25 29544+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 29545+ *
29546+ * This program, aufs is free software; you can redistribute it and/or modify
29547+ * it under the terms of the GNU General Public License as published by
29548+ * the Free Software Foundation; either version 2 of the License, or
29549+ * (at your option) any later version.
dece6358
AM
29550+ *
29551+ * This program is distributed in the hope that it will be useful,
29552+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29553+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29554+ * GNU General Public License for more details.
29555+ *
29556+ * You should have received a copy of the GNU General Public License
523b37e3 29557+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29558+ */
29559+
29560+/*
29561+ * mount and super_block operations
29562+ */
29563+
f6c5ef8b 29564+#include <linux/mm.h>
1facf9fc 29565+#include <linux/seq_file.h>
29566+#include <linux/statfs.h>
7f207e10 29567+#include <linux/vmalloc.h>
1facf9fc 29568+#include "aufs.h"
29569+
29570+/*
29571+ * super_operations
29572+ */
29573+static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29574+{
29575+ struct au_icntnr *c;
29576+
29577+ c = au_cache_alloc_icntnr();
29578+ if (c) {
027c5e7a 29579+ au_icntnr_init(c);
be118d29 29580+ inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
1facf9fc 29581+ c->iinfo.ii_hinode = NULL;
29582+ return &c->vfs_inode;
29583+ }
29584+ return NULL;
29585+}
29586+
027c5e7a
AM
29587+static void aufs_destroy_inode_cb(struct rcu_head *head)
29588+{
29589+ struct inode *inode = container_of(head, struct inode, i_rcu);
29590+
1c60b727 29591+ au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
027c5e7a
AM
29592+}
29593+
1facf9fc 29594+static void aufs_destroy_inode(struct inode *inode)
29595+{
5afbbe0d
AM
29596+ if (!au_is_bad_inode(inode))
29597+ au_iinfo_fin(inode);
027c5e7a 29598+ call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
1facf9fc 29599+}
29600+
29601+struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29602+{
29603+ struct inode *inode;
29604+ int err;
29605+
29606+ inode = iget_locked(sb, ino);
29607+ if (unlikely(!inode)) {
29608+ inode = ERR_PTR(-ENOMEM);
29609+ goto out;
29610+ }
29611+ if (!(inode->i_state & I_NEW))
29612+ goto out;
29613+
29614+ err = au_xigen_new(inode);
29615+ if (!err)
29616+ err = au_iinfo_init(inode);
29617+ if (!err)
be118d29 29618+ inode_inc_iversion(inode);
1facf9fc 29619+ else {
29620+ iget_failed(inode);
29621+ inode = ERR_PTR(err);
29622+ }
29623+
4f0767ce 29624+out:
1facf9fc 29625+ /* never return NULL */
29626+ AuDebugOn(!inode);
29627+ AuTraceErrPtr(inode);
29628+ return inode;
29629+}
29630+
29631+/* lock free root dinfo */
29632+static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29633+{
29634+ int err;
5afbbe0d 29635+ aufs_bindex_t bindex, bbot;
1facf9fc 29636+ struct path path;
4a4d8108 29637+ struct au_hdentry *hdp;
1facf9fc 29638+ struct au_branch *br;
076b876e 29639+ au_br_perm_str_t perm;
1facf9fc 29640+
29641+ err = 0;
5afbbe0d
AM
29642+ bbot = au_sbbot(sb);
29643+ bindex = 0;
29644+ hdp = au_hdentry(au_di(sb->s_root), bindex);
29645+ for (; !err && bindex <= bbot; bindex++, hdp++) {
1facf9fc 29646+ br = au_sbr(sb, bindex);
86dc4139 29647+ path.mnt = au_br_mnt(br);
5afbbe0d 29648+ path.dentry = hdp->hd_dentry;
1facf9fc 29649+ err = au_seq_path(seq, &path);
79b8bda9 29650+ if (!err) {
076b876e 29651+ au_optstr_br_perm(&perm, br->br_perm);
79b8bda9 29652+ seq_printf(seq, "=%s", perm.a);
5afbbe0d 29653+ if (bindex != bbot)
79b8bda9 29654+ seq_putc(seq, ':');
1e00d052 29655+ }
1facf9fc 29656+ }
79b8bda9
AM
29657+ if (unlikely(err || seq_has_overflowed(seq)))
29658+ err = -E2BIG;
1facf9fc 29659+
29660+ return err;
29661+}
29662+
f2c43d5f
AM
29663+static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29664+ const char *append)
29665+{
29666+ char *p;
29667+
29668+ p = fmt;
29669+ while (*pat != ':')
29670+ *p++ = *pat++;
29671+ *p++ = *pat++;
29672+ strcpy(p, append);
29673+ AuDebugOn(strlen(fmt) >= len);
29674+}
29675+
1facf9fc 29676+static void au_show_wbr_create(struct seq_file *m, int v,
29677+ struct au_sbinfo *sbinfo)
29678+{
29679+ const char *pat;
f2c43d5f
AM
29680+ char fmt[32];
29681+ struct au_wbr_mfs *mfs;
1facf9fc 29682+
dece6358
AM
29683+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29684+
c2b27bf2 29685+ seq_puts(m, ",create=");
1facf9fc 29686+ pat = au_optstr_wbr_create(v);
f2c43d5f 29687+ mfs = &sbinfo->si_wbr_mfs;
1facf9fc 29688+ switch (v) {
29689+ case AuWbrCreate_TDP:
29690+ case AuWbrCreate_RR:
29691+ case AuWbrCreate_MFS:
29692+ case AuWbrCreate_PMFS:
c2b27bf2 29693+ seq_puts(m, pat);
1facf9fc 29694+ break;
f2c43d5f
AM
29695+ case AuWbrCreate_MFSRR:
29696+ case AuWbrCreate_TDMFS:
29697+ case AuWbrCreate_PMFSRR:
29698+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29699+ seq_printf(m, fmt, mfs->mfsrr_watermark);
1facf9fc 29700+ break;
f2c43d5f 29701+ case AuWbrCreate_MFSV:
1facf9fc 29702+ case AuWbrCreate_PMFSV:
f2c43d5f
AM
29703+ au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29704+ seq_printf(m, fmt,
29705+ jiffies_to_msecs(mfs->mfs_expire)
e49829fe 29706+ / MSEC_PER_SEC);
1facf9fc 29707+ break;
1facf9fc 29708+ case AuWbrCreate_MFSRRV:
f2c43d5f 29709+ case AuWbrCreate_TDMFSV:
392086de 29710+ case AuWbrCreate_PMFSRRV:
f2c43d5f
AM
29711+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29712+ seq_printf(m, fmt, mfs->mfsrr_watermark,
29713+ jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
392086de 29714+ break;
f2c43d5f
AM
29715+ default:
29716+ BUG();
1facf9fc 29717+ }
29718+}
29719+
7eafdf33 29720+static int au_show_xino(struct seq_file *seq, struct super_block *sb)
1facf9fc 29721+{
29722+#ifdef CONFIG_SYSFS
29723+ return 0;
29724+#else
29725+ int err;
29726+ const int len = sizeof(AUFS_XINO_FNAME) - 1;
29727+ aufs_bindex_t bindex, brid;
1facf9fc 29728+ struct qstr *name;
29729+ struct file *f;
29730+ struct dentry *d, *h_root;
acd2b654 29731+ struct au_branch *br;
1facf9fc 29732+
dece6358
AM
29733+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29734+
1facf9fc 29735+ err = 0;
1facf9fc 29736+ f = au_sbi(sb)->si_xib;
29737+ if (!f)
29738+ goto out;
29739+
29740+ /* stop printing the default xino path on the first writable branch */
29741+ h_root = NULL;
acd2b654
AM
29742+ bindex = au_xi_root(sb, f->f_path.dentry);
29743+ if (bindex >= 0) {
29744+ br = au_sbr_sb(sb, bindex);
29745+ h_root = au_br_dentry(br);
1facf9fc 29746+ }
acd2b654 29747+
2000de60 29748+ d = f->f_path.dentry;
1facf9fc 29749+ name = &d->d_name;
29750+ /* safe ->d_parent because the file is unlinked */
29751+ if (d->d_parent == h_root
29752+ && name->len == len
29753+ && !memcmp(name->name, AUFS_XINO_FNAME, len))
29754+ goto out;
29755+
29756+ seq_puts(seq, ",xino=");
29757+ err = au_xino_path(seq, f);
29758+
4f0767ce 29759+out:
1facf9fc 29760+ return err;
29761+#endif
29762+}
29763+
29764+/* seq_file will re-call me in case of too long string */
7eafdf33 29765+static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
1facf9fc 29766+{
027c5e7a 29767+ int err;
1facf9fc 29768+ unsigned int mnt_flags, v;
29769+ struct super_block *sb;
29770+ struct au_sbinfo *sbinfo;
29771+
29772+#define AuBool(name, str) do { \
29773+ v = au_opt_test(mnt_flags, name); \
29774+ if (v != au_opt_test(AuOpt_Def, name)) \
29775+ seq_printf(m, ",%s" #str, v ? "" : "no"); \
29776+} while (0)
29777+
29778+#define AuStr(name, str) do { \
29779+ v = mnt_flags & AuOptMask_##name; \
29780+ if (v != (AuOpt_Def & AuOptMask_##name)) \
29781+ seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
29782+} while (0)
29783+
29784+#define AuUInt(name, str, val) do { \
29785+ if (val != AUFS_##name##_DEF) \
29786+ seq_printf(m, "," #str "=%u", val); \
29787+} while (0)
29788+
7eafdf33 29789+ sb = dentry->d_sb;
2121bcd9 29790+ if (sb->s_flags & SB_POSIXACL)
c1595e42 29791+ seq_puts(m, ",acl");
be118d29
JR
29792+#if 0
29793+ if (sb->s_flags & SB_I_VERSION)
29794+ seq_puts(m, ",i_version");
29795+#endif
c1595e42
JR
29796+
29797+ /* lock free root dinfo */
1facf9fc 29798+ si_noflush_read_lock(sb);
29799+ sbinfo = au_sbi(sb);
29800+ seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
29801+
29802+ mnt_flags = au_mntflags(sb);
29803+ if (au_opt_test(mnt_flags, XINO)) {
7eafdf33 29804+ err = au_show_xino(m, sb);
1facf9fc 29805+ if (unlikely(err))
29806+ goto out;
29807+ } else
29808+ seq_puts(m, ",noxino");
29809+
29810+ AuBool(TRUNC_XINO, trunc_xino);
29811+ AuStr(UDBA, udba);
dece6358 29812+ AuBool(SHWH, shwh);
1facf9fc 29813+ AuBool(PLINK, plink);
4a4d8108 29814+ AuBool(DIO, dio);
076b876e 29815+ AuBool(DIRPERM1, dirperm1);
1facf9fc 29816+
29817+ v = sbinfo->si_wbr_create;
29818+ if (v != AuWbrCreate_Def)
29819+ au_show_wbr_create(m, v, sbinfo);
29820+
29821+ v = sbinfo->si_wbr_copyup;
29822+ if (v != AuWbrCopyup_Def)
29823+ seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
29824+
29825+ v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
29826+ if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
29827+ seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
29828+
29829+ AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
29830+
027c5e7a
AM
29831+ v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
29832+ AuUInt(RDCACHE, rdcache, v);
1facf9fc 29833+
29834+ AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
29835+ AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
29836+
076b876e
AM
29837+ au_fhsm_show(m, sbinfo);
29838+
8b6a4947 29839+ AuBool(DIRREN, dirren);
1facf9fc 29840+ AuBool(SUM, sum);
29841+ /* AuBool(SUM_W, wsum); */
29842+ AuBool(WARN_PERM, warn_perm);
29843+ AuBool(VERBOSE, verbose);
29844+
4f0767ce 29845+out:
1facf9fc 29846+ /* be sure to print "br:" last */
29847+ if (!sysaufs_brs) {
29848+ seq_puts(m, ",br:");
29849+ au_show_brs(m, sb);
29850+ }
29851+ si_read_unlock(sb);
29852+ return 0;
29853+
1facf9fc 29854+#undef AuBool
29855+#undef AuStr
4a4d8108 29856+#undef AuUInt
1facf9fc 29857+}
29858+
29859+/* ---------------------------------------------------------------------- */
29860+
29861+/* sum mode which returns the summation for statfs(2) */
29862+
29863+static u64 au_add_till_max(u64 a, u64 b)
29864+{
29865+ u64 old;
29866+
29867+ old = a;
29868+ a += b;
92d182d2
AM
29869+ if (old <= a)
29870+ return a;
29871+ return ULLONG_MAX;
29872+}
29873+
29874+static u64 au_mul_till_max(u64 a, long mul)
29875+{
29876+ u64 old;
29877+
29878+ old = a;
29879+ a *= mul;
29880+ if (old <= a)
1facf9fc 29881+ return a;
29882+ return ULLONG_MAX;
29883+}
29884+
29885+static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
29886+{
29887+ int err;
92d182d2 29888+ long bsize, factor;
1facf9fc 29889+ u64 blocks, bfree, bavail, files, ffree;
5afbbe0d 29890+ aufs_bindex_t bbot, bindex, i;
1facf9fc 29891+ unsigned char shared;
7f207e10 29892+ struct path h_path;
1facf9fc 29893+ struct super_block *h_sb;
29894+
92d182d2
AM
29895+ err = 0;
29896+ bsize = LONG_MAX;
29897+ files = 0;
29898+ ffree = 0;
1facf9fc 29899+ blocks = 0;
29900+ bfree = 0;
29901+ bavail = 0;
5afbbe0d
AM
29902+ bbot = au_sbbot(sb);
29903+ for (bindex = 0; bindex <= bbot; bindex++) {
7f207e10
AM
29904+ h_path.mnt = au_sbr_mnt(sb, bindex);
29905+ h_sb = h_path.mnt->mnt_sb;
1facf9fc 29906+ shared = 0;
92d182d2 29907+ for (i = 0; !shared && i < bindex; i++)
1facf9fc 29908+ shared = (au_sbr_sb(sb, i) == h_sb);
29909+ if (shared)
29910+ continue;
29911+
29912+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
29913+ h_path.dentry = h_path.mnt->mnt_root;
29914+ err = vfs_statfs(&h_path, buf);
1facf9fc 29915+ if (unlikely(err))
29916+ goto out;
29917+
92d182d2
AM
29918+ if (bsize > buf->f_bsize) {
29919+ /*
29920+ * we will reduce bsize, so we have to expand blocks
29921+ * etc. to match them again
29922+ */
29923+ factor = (bsize / buf->f_bsize);
29924+ blocks = au_mul_till_max(blocks, factor);
29925+ bfree = au_mul_till_max(bfree, factor);
29926+ bavail = au_mul_till_max(bavail, factor);
29927+ bsize = buf->f_bsize;
29928+ }
29929+
29930+ factor = (buf->f_bsize / bsize);
29931+ blocks = au_add_till_max(blocks,
29932+ au_mul_till_max(buf->f_blocks, factor));
29933+ bfree = au_add_till_max(bfree,
29934+ au_mul_till_max(buf->f_bfree, factor));
29935+ bavail = au_add_till_max(bavail,
29936+ au_mul_till_max(buf->f_bavail, factor));
1facf9fc 29937+ files = au_add_till_max(files, buf->f_files);
29938+ ffree = au_add_till_max(ffree, buf->f_ffree);
29939+ }
29940+
92d182d2 29941+ buf->f_bsize = bsize;
1facf9fc 29942+ buf->f_blocks = blocks;
29943+ buf->f_bfree = bfree;
29944+ buf->f_bavail = bavail;
29945+ buf->f_files = files;
29946+ buf->f_ffree = ffree;
92d182d2 29947+ buf->f_frsize = 0;
1facf9fc 29948+
4f0767ce 29949+out:
1facf9fc 29950+ return err;
29951+}
29952+
29953+static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
29954+{
29955+ int err;
7f207e10 29956+ struct path h_path;
1facf9fc 29957+ struct super_block *sb;
29958+
29959+ /* lock free root dinfo */
29960+ sb = dentry->d_sb;
29961+ si_noflush_read_lock(sb);
7f207e10 29962+ if (!au_opt_test(au_mntflags(sb), SUM)) {
1facf9fc 29963+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
29964+ h_path.mnt = au_sbr_mnt(sb, 0);
29965+ h_path.dentry = h_path.mnt->mnt_root;
29966+ err = vfs_statfs(&h_path, buf);
29967+ } else
1facf9fc 29968+ err = au_statfs_sum(sb, buf);
29969+ si_read_unlock(sb);
29970+
29971+ if (!err) {
29972+ buf->f_type = AUFS_SUPER_MAGIC;
4a4d8108 29973+ buf->f_namelen = AUFS_MAX_NAMELEN;
1facf9fc 29974+ memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
29975+ }
29976+ /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
29977+
29978+ return err;
29979+}
29980+
29981+/* ---------------------------------------------------------------------- */
29982+
537831f9
AM
29983+static int aufs_sync_fs(struct super_block *sb, int wait)
29984+{
29985+ int err, e;
5afbbe0d 29986+ aufs_bindex_t bbot, bindex;
537831f9
AM
29987+ struct au_branch *br;
29988+ struct super_block *h_sb;
29989+
29990+ err = 0;
29991+ si_noflush_read_lock(sb);
5afbbe0d
AM
29992+ bbot = au_sbbot(sb);
29993+ for (bindex = 0; bindex <= bbot; bindex++) {
537831f9
AM
29994+ br = au_sbr(sb, bindex);
29995+ if (!au_br_writable(br->br_perm))
29996+ continue;
29997+
29998+ h_sb = au_sbr_sb(sb, bindex);
a2654f78
AM
29999+ e = vfsub_sync_filesystem(h_sb, wait);
30000+ if (unlikely(e && !err))
30001+ err = e;
30002+ /* go on even if an error happens */
537831f9
AM
30003+ }
30004+ si_read_unlock(sb);
30005+
30006+ return err;
30007+}
30008+
30009+/* ---------------------------------------------------------------------- */
30010+
1facf9fc 30011+/* final actions when unmounting a file system */
30012+static void aufs_put_super(struct super_block *sb)
30013+{
30014+ struct au_sbinfo *sbinfo;
30015+
30016+ sbinfo = au_sbi(sb);
062440b3
AM
30017+ if (sbinfo)
30018+ kobject_put(&sbinfo->si_kobj);
1facf9fc 30019+}
30020+
30021+/* ---------------------------------------------------------------------- */
30022+
79b8bda9
AM
30023+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30024+ struct super_block *sb, void *arg)
7f207e10
AM
30025+{
30026+ void *array;
076b876e 30027+ unsigned long long n, sz;
7f207e10
AM
30028+
30029+ array = NULL;
30030+ n = 0;
30031+ if (!*hint)
30032+ goto out;
30033+
30034+ if (*hint > ULLONG_MAX / sizeof(array)) {
30035+ array = ERR_PTR(-EMFILE);
30036+ pr_err("hint %llu\n", *hint);
30037+ goto out;
30038+ }
30039+
076b876e
AM
30040+ sz = sizeof(array) * *hint;
30041+ array = kzalloc(sz, GFP_NOFS);
7f207e10 30042+ if (unlikely(!array))
076b876e 30043+ array = vzalloc(sz);
7f207e10
AM
30044+ if (unlikely(!array)) {
30045+ array = ERR_PTR(-ENOMEM);
30046+ goto out;
30047+ }
30048+
79b8bda9 30049+ n = cb(sb, array, *hint, arg);
7f207e10
AM
30050+ AuDebugOn(n > *hint);
30051+
30052+out:
30053+ *hint = n;
30054+ return array;
30055+}
30056+
79b8bda9 30057+static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
7f207e10
AM
30058+ unsigned long long max __maybe_unused,
30059+ void *arg)
30060+{
30061+ unsigned long long n;
30062+ struct inode **p, *inode;
30063+ struct list_head *head;
30064+
30065+ n = 0;
30066+ p = a;
30067+ head = arg;
79b8bda9 30068+ spin_lock(&sb->s_inode_list_lock);
7f207e10 30069+ list_for_each_entry(inode, head, i_sb_list) {
5afbbe0d
AM
30070+ if (!au_is_bad_inode(inode)
30071+ && au_ii(inode)->ii_btop >= 0) {
2cbb1c4b
JR
30072+ spin_lock(&inode->i_lock);
30073+ if (atomic_read(&inode->i_count)) {
30074+ au_igrab(inode);
30075+ *p++ = inode;
30076+ n++;
30077+ AuDebugOn(n > max);
30078+ }
30079+ spin_unlock(&inode->i_lock);
7f207e10
AM
30080+ }
30081+ }
79b8bda9 30082+ spin_unlock(&sb->s_inode_list_lock);
7f207e10
AM
30083+
30084+ return n;
30085+}
30086+
30087+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
30088+{
acd2b654
AM
30089+ struct au_sbinfo *sbi;
30090+
30091+ sbi = au_sbi(sb);
30092+ *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
79b8bda9 30093+ return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
7f207e10
AM
30094+}
30095+
30096+void au_iarray_free(struct inode **a, unsigned long long max)
30097+{
30098+ unsigned long long ull;
30099+
30100+ for (ull = 0; ull < max; ull++)
30101+ iput(a[ull]);
be52b249 30102+ kvfree(a);
7f207e10
AM
30103+}
30104+
30105+/* ---------------------------------------------------------------------- */
30106+
1facf9fc 30107+/*
30108+ * refresh dentry and inode at remount time.
30109+ */
027c5e7a
AM
30110+/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
30111+static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
30112+ struct dentry *parent)
1facf9fc 30113+{
30114+ int err;
1facf9fc 30115+
30116+ di_write_lock_child(dentry);
1facf9fc 30117+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
30118+ err = au_refresh_dentry(dentry, parent);
30119+ if (!err && dir_flags)
5527c038 30120+ au_hn_reset(d_inode(dentry), dir_flags);
1facf9fc 30121+ di_read_unlock(parent, AuLock_IR);
1facf9fc 30122+ di_write_unlock(dentry);
30123+
30124+ return err;
30125+}
30126+
027c5e7a
AM
30127+static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
30128+ struct au_sbinfo *sbinfo,
b95c5147 30129+ const unsigned int dir_flags, unsigned int do_idop)
1facf9fc 30130+{
027c5e7a
AM
30131+ int err;
30132+ struct dentry *parent;
027c5e7a
AM
30133+
30134+ err = 0;
30135+ parent = dget_parent(dentry);
30136+ if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
5527c038
JR
30137+ if (d_really_is_positive(dentry)) {
30138+ if (!d_is_dir(dentry))
027c5e7a
AM
30139+ err = au_do_refresh(dentry, /*dir_flags*/0,
30140+ parent);
30141+ else {
30142+ err = au_do_refresh(dentry, dir_flags, parent);
30143+ if (unlikely(err))
30144+ au_fset_si(sbinfo, FAILED_REFRESH_DIR);
30145+ }
30146+ } else
30147+ err = au_do_refresh(dentry, /*dir_flags*/0, parent);
30148+ AuDbgDentry(dentry);
30149+ }
30150+ dput(parent);
30151+
79b8bda9 30152+ if (!err) {
b95c5147 30153+ if (do_idop)
79b8bda9
AM
30154+ au_refresh_dop(dentry, /*force_reval*/0);
30155+ } else
30156+ au_refresh_dop(dentry, /*force_reval*/1);
30157+
027c5e7a
AM
30158+ AuTraceErr(err);
30159+ return err;
1facf9fc 30160+}
30161+
b95c5147 30162+static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
1facf9fc 30163+{
30164+ int err, i, j, ndentry, e;
027c5e7a 30165+ unsigned int sigen;
1facf9fc 30166+ struct au_dcsub_pages dpages;
30167+ struct au_dpage *dpage;
027c5e7a
AM
30168+ struct dentry **dentries, *d;
30169+ struct au_sbinfo *sbinfo;
30170+ struct dentry *root = sb->s_root;
5527c038 30171+ const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
1facf9fc 30172+
b95c5147 30173+ if (do_idop)
79b8bda9
AM
30174+ au_refresh_dop(root, /*force_reval*/0);
30175+
027c5e7a
AM
30176+ err = au_dpages_init(&dpages, GFP_NOFS);
30177+ if (unlikely(err))
1facf9fc 30178+ goto out;
027c5e7a
AM
30179+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
30180+ if (unlikely(err))
1facf9fc 30181+ goto out_dpages;
1facf9fc 30182+
027c5e7a
AM
30183+ sigen = au_sigen(sb);
30184+ sbinfo = au_sbi(sb);
30185+ for (i = 0; i < dpages.ndpage; i++) {
1facf9fc 30186+ dpage = dpages.dpages + i;
30187+ dentries = dpage->dentries;
30188+ ndentry = dpage->ndentry;
027c5e7a 30189+ for (j = 0; j < ndentry; j++) {
1facf9fc 30190+ d = dentries[j];
79b8bda9 30191+ e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
b95c5147 30192+ do_idop);
027c5e7a
AM
30193+ if (unlikely(e && !err))
30194+ err = e;
30195+ /* go on even err */
1facf9fc 30196+ }
30197+ }
30198+
4f0767ce 30199+out_dpages:
1facf9fc 30200+ au_dpages_free(&dpages);
4f0767ce 30201+out:
1facf9fc 30202+ return err;
30203+}
30204+
b95c5147 30205+static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
1facf9fc 30206+{
027c5e7a
AM
30207+ int err, e;
30208+ unsigned int sigen;
30209+ unsigned long long max, ull;
30210+ struct inode *inode, **array;
1facf9fc 30211+
027c5e7a
AM
30212+ array = au_iarray_alloc(sb, &max);
30213+ err = PTR_ERR(array);
30214+ if (IS_ERR(array))
30215+ goto out;
1facf9fc 30216+
30217+ err = 0;
027c5e7a
AM
30218+ sigen = au_sigen(sb);
30219+ for (ull = 0; ull < max; ull++) {
30220+ inode = array[ull];
076b876e
AM
30221+ if (unlikely(!inode))
30222+ break;
b95c5147
AM
30223+
30224+ e = 0;
30225+ ii_write_lock_child(inode);
537831f9 30226+ if (au_iigen(inode, NULL) != sigen) {
027c5e7a 30227+ e = au_refresh_hinode_self(inode);
1facf9fc 30228+ if (unlikely(e)) {
b95c5147 30229+ au_refresh_iop(inode, /*force_getattr*/1);
027c5e7a 30230+ pr_err("error %d, i%lu\n", e, inode->i_ino);
1facf9fc 30231+ if (!err)
30232+ err = e;
30233+ /* go on even if err */
30234+ }
30235+ }
b95c5147
AM
30236+ if (!e && do_idop)
30237+ au_refresh_iop(inode, /*force_getattr*/0);
30238+ ii_write_unlock(inode);
1facf9fc 30239+ }
30240+
027c5e7a 30241+ au_iarray_free(array, max);
1facf9fc 30242+
4f0767ce 30243+out:
1facf9fc 30244+ return err;
30245+}
30246+
b95c5147 30247+static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
1facf9fc 30248+{
027c5e7a
AM
30249+ int err, e;
30250+ unsigned int udba;
5afbbe0d 30251+ aufs_bindex_t bindex, bbot;
1facf9fc 30252+ struct dentry *root;
30253+ struct inode *inode;
027c5e7a 30254+ struct au_branch *br;
79b8bda9 30255+ struct au_sbinfo *sbi;
1facf9fc 30256+
30257+ au_sigen_inc(sb);
79b8bda9
AM
30258+ sbi = au_sbi(sb);
30259+ au_fclr_si(sbi, FAILED_REFRESH_DIR);
1facf9fc 30260+
30261+ root = sb->s_root;
30262+ DiMustNoWaiters(root);
5527c038 30263+ inode = d_inode(root);
1facf9fc 30264+ IiMustNoWaiters(inode);
1facf9fc 30265+
027c5e7a 30266+ udba = au_opt_udba(sb);
5afbbe0d
AM
30267+ bbot = au_sbbot(sb);
30268+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
30269+ br = au_sbr(sb, bindex);
30270+ err = au_hnotify_reset_br(udba, br, br->br_perm);
1facf9fc 30271+ if (unlikely(err))
027c5e7a
AM
30272+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
30273+ bindex, err);
30274+ /* go on even if err */
1facf9fc 30275+ }
027c5e7a 30276+ au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
1facf9fc 30277+
b95c5147 30278+ if (do_idop) {
79b8bda9
AM
30279+ if (au_ftest_si(sbi, NO_DREVAL)) {
30280+ AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30281+ sb->s_d_op = &aufs_dop_noreval;
b95c5147
AM
30282+ AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30283+ sbi->si_iop_array = aufs_iop_nogetattr;
79b8bda9
AM
30284+ } else {
30285+ AuDebugOn(sb->s_d_op == &aufs_dop);
30286+ sb->s_d_op = &aufs_dop;
b95c5147
AM
30287+ AuDebugOn(sbi->si_iop_array == aufs_iop);
30288+ sbi->si_iop_array = aufs_iop;
79b8bda9 30289+ }
062440b3 30290+ pr_info("reset to %ps and %ps\n",
b95c5147 30291+ sb->s_d_op, sbi->si_iop_array);
79b8bda9
AM
30292+ }
30293+
027c5e7a 30294+ di_write_unlock(root);
b95c5147
AM
30295+ err = au_refresh_d(sb, do_idop);
30296+ e = au_refresh_i(sb, do_idop);
027c5e7a
AM
30297+ if (unlikely(e && !err))
30298+ err = e;
1facf9fc 30299+ /* aufs_write_lock() calls ..._child() */
30300+ di_write_lock_child(root);
027c5e7a
AM
30301+
30302+ au_cpup_attr_all(inode, /*force*/1);
30303+
30304+ if (unlikely(err))
30305+ AuIOErr("refresh failed, ignored, %d\n", err);
1facf9fc 30306+}
30307+
30308+/* stop extra interpretation of errno in mount(8), and strange error messages */
30309+static int cvt_err(int err)
30310+{
30311+ AuTraceErr(err);
30312+
30313+ switch (err) {
30314+ case -ENOENT:
30315+ case -ENOTDIR:
30316+ case -EEXIST:
30317+ case -EIO:
30318+ err = -EINVAL;
30319+ }
30320+ return err;
30321+}
30322+
30323+static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
30324+{
4a4d8108
AM
30325+ int err, do_dx;
30326+ unsigned int mntflags;
be52b249
AM
30327+ struct au_opts opts = {
30328+ .opt = NULL
30329+ };
1facf9fc 30330+ struct dentry *root;
30331+ struct inode *inode;
30332+ struct au_sbinfo *sbinfo;
30333+
30334+ err = 0;
30335+ root = sb->s_root;
30336+ if (!data || !*data) {
e49829fe
JR
30337+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30338+ if (!err) {
30339+ di_write_lock_child(root);
30340+ err = au_opts_verify(sb, *flags, /*pending*/0);
30341+ aufs_write_unlock(root);
30342+ }
1facf9fc 30343+ goto out;
30344+ }
30345+
30346+ err = -ENOMEM;
1facf9fc 30347+ opts.opt = (void *)__get_free_page(GFP_NOFS);
30348+ if (unlikely(!opts.opt))
30349+ goto out;
30350+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30351+ opts.flags = AuOpts_REMOUNT;
30352+ opts.sb_flags = *flags;
30353+
30354+ /* parse it before aufs lock */
30355+ err = au_opts_parse(sb, data, &opts);
30356+ if (unlikely(err))
30357+ goto out_opts;
30358+
30359+ sbinfo = au_sbi(sb);
5527c038 30360+ inode = d_inode(root);
febd17d6 30361+ inode_lock(inode);
e49829fe
JR
30362+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30363+ if (unlikely(err))
30364+ goto out_mtx;
30365+ di_write_lock_child(root);
1facf9fc 30366+
30367+ /* au_opts_remount() may return an error */
30368+ err = au_opts_remount(sb, &opts);
30369+ au_opts_free(&opts);
30370+
027c5e7a 30371+ if (au_ftest_opts(opts.flags, REFRESH))
b95c5147 30372+ au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
1facf9fc 30373+
4a4d8108
AM
30374+ if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
30375+ mntflags = au_mntflags(sb);
30376+ do_dx = !!au_opt_test(mntflags, DIO);
30377+ au_dy_arefresh(do_dx);
30378+ }
30379+
076b876e 30380+ au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
1facf9fc 30381+ aufs_write_unlock(root);
953406b4 30382+
e49829fe 30383+out_mtx:
febd17d6 30384+ inode_unlock(inode);
4f0767ce 30385+out_opts:
1c60b727 30386+ free_page((unsigned long)opts.opt);
4f0767ce 30387+out:
1facf9fc 30388+ err = cvt_err(err);
30389+ AuTraceErr(err);
30390+ return err;
30391+}
30392+
4a4d8108 30393+static const struct super_operations aufs_sop = {
1facf9fc 30394+ .alloc_inode = aufs_alloc_inode,
30395+ .destroy_inode = aufs_destroy_inode,
b752ccd1 30396+ /* always deleting, no clearing */
1facf9fc 30397+ .drop_inode = generic_delete_inode,
30398+ .show_options = aufs_show_options,
30399+ .statfs = aufs_statfs,
30400+ .put_super = aufs_put_super,
537831f9 30401+ .sync_fs = aufs_sync_fs,
1facf9fc 30402+ .remount_fs = aufs_remount_fs
30403+};
30404+
30405+/* ---------------------------------------------------------------------- */
30406+
30407+static int alloc_root(struct super_block *sb)
30408+{
30409+ int err;
30410+ struct inode *inode;
30411+ struct dentry *root;
30412+
30413+ err = -ENOMEM;
30414+ inode = au_iget_locked(sb, AUFS_ROOT_INO);
30415+ err = PTR_ERR(inode);
30416+ if (IS_ERR(inode))
30417+ goto out;
30418+
b95c5147 30419+ inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
1facf9fc 30420+ inode->i_fop = &aufs_dir_fop;
30421+ inode->i_mode = S_IFDIR;
9dbd164d 30422+ set_nlink(inode, 2);
1facf9fc 30423+ unlock_new_inode(inode);
30424+
92d182d2 30425+ root = d_make_root(inode);
1facf9fc 30426+ if (unlikely(!root))
92d182d2 30427+ goto out;
1facf9fc 30428+ err = PTR_ERR(root);
30429+ if (IS_ERR(root))
92d182d2 30430+ goto out;
1facf9fc 30431+
4a4d8108 30432+ err = au_di_init(root);
1facf9fc 30433+ if (!err) {
30434+ sb->s_root = root;
30435+ return 0; /* success */
30436+ }
30437+ dput(root);
1facf9fc 30438+
4f0767ce 30439+out:
1facf9fc 30440+ return err;
1facf9fc 30441+}
30442+
30443+static int aufs_fill_super(struct super_block *sb, void *raw_data,
30444+ int silent __maybe_unused)
30445+{
30446+ int err;
be52b249
AM
30447+ struct au_opts opts = {
30448+ .opt = NULL
30449+ };
79b8bda9 30450+ struct au_sbinfo *sbinfo;
1facf9fc 30451+ struct dentry *root;
30452+ struct inode *inode;
30453+ char *arg = raw_data;
30454+
30455+ if (unlikely(!arg || !*arg)) {
30456+ err = -EINVAL;
4a4d8108 30457+ pr_err("no arg\n");
1facf9fc 30458+ goto out;
30459+ }
30460+
30461+ err = -ENOMEM;
1facf9fc 30462+ opts.opt = (void *)__get_free_page(GFP_NOFS);
30463+ if (unlikely(!opts.opt))
30464+ goto out;
30465+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30466+ opts.sb_flags = sb->s_flags;
30467+
30468+ err = au_si_alloc(sb);
30469+ if (unlikely(err))
30470+ goto out_opts;
79b8bda9 30471+ sbinfo = au_sbi(sb);
1facf9fc 30472+
30473+ /* all timestamps always follow the ones on the branch */
2121bcd9 30474+ sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
be118d29 30475+ sb->s_flags |= SB_I_VERSION; /* do we really need this? */
1facf9fc 30476+ sb->s_op = &aufs_sop;
027c5e7a 30477+ sb->s_d_op = &aufs_dop;
1facf9fc 30478+ sb->s_magic = AUFS_SUPER_MAGIC;
30479+ sb->s_maxbytes = 0;
c1595e42 30480+ sb->s_stack_depth = 1;
1facf9fc 30481+ au_export_init(sb);
f2c43d5f 30482+ au_xattr_init(sb);
1facf9fc 30483+
30484+ err = alloc_root(sb);
30485+ if (unlikely(err)) {
30486+ si_write_unlock(sb);
30487+ goto out_info;
30488+ }
30489+ root = sb->s_root;
5527c038 30490+ inode = d_inode(root);
1facf9fc 30491+
30492+ /*
30493+ * actually we can parse options regardless aufs lock here.
30494+ * but at remount time, parsing must be done before aufs lock.
30495+ * so we follow the same rule.
30496+ */
30497+ ii_write_lock_parent(inode);
30498+ aufs_write_unlock(root);
30499+ err = au_opts_parse(sb, arg, &opts);
30500+ if (unlikely(err))
30501+ goto out_root;
30502+
30503+ /* lock vfs_inode first, then aufs. */
febd17d6 30504+ inode_lock(inode);
1facf9fc 30505+ aufs_write_lock(root);
30506+ err = au_opts_mount(sb, &opts);
30507+ au_opts_free(&opts);
79b8bda9
AM
30508+ if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
30509+ sb->s_d_op = &aufs_dop_noreval;
062440b3 30510+ pr_info("%ps\n", sb->s_d_op);
79b8bda9 30511+ au_refresh_dop(root, /*force_reval*/0);
b95c5147
AM
30512+ sbinfo->si_iop_array = aufs_iop_nogetattr;
30513+ au_refresh_iop(inode, /*force_getattr*/0);
79b8bda9 30514+ }
1facf9fc 30515+ aufs_write_unlock(root);
febd17d6 30516+ inode_unlock(inode);
4a4d8108
AM
30517+ if (!err)
30518+ goto out_opts; /* success */
1facf9fc 30519+
4f0767ce 30520+out_root:
1facf9fc 30521+ dput(root);
30522+ sb->s_root = NULL;
4f0767ce 30523+out_info:
79b8bda9 30524+ kobject_put(&sbinfo->si_kobj);
1facf9fc 30525+ sb->s_fs_info = NULL;
4f0767ce 30526+out_opts:
1c60b727 30527+ free_page((unsigned long)opts.opt);
4f0767ce 30528+out:
1facf9fc 30529+ AuTraceErr(err);
30530+ err = cvt_err(err);
30531+ AuTraceErr(err);
30532+ return err;
30533+}
30534+
30535+/* ---------------------------------------------------------------------- */
30536+
027c5e7a
AM
30537+static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
30538+ const char *dev_name __maybe_unused,
30539+ void *raw_data)
1facf9fc 30540+{
027c5e7a 30541+ struct dentry *root;
1facf9fc 30542+
30543+ /* all timestamps always follow the ones on the branch */
30544+ /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
027c5e7a
AM
30545+ root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
30546+ if (IS_ERR(root))
30547+ goto out;
30548+
062440b3 30549+ au_sbilist_add(root->d_sb);
027c5e7a
AM
30550+
30551+out:
30552+ return root;
1facf9fc 30553+}
30554+
e49829fe
JR
30555+static void aufs_kill_sb(struct super_block *sb)
30556+{
30557+ struct au_sbinfo *sbinfo;
30558+
30559+ sbinfo = au_sbi(sb);
30560+ if (sbinfo) {
30561+ au_sbilist_del(sb);
30562+ aufs_write_lock(sb->s_root);
076b876e 30563+ au_fhsm_fin(sb);
e49829fe
JR
30564+ if (sbinfo->si_wbr_create_ops->fin)
30565+ sbinfo->si_wbr_create_ops->fin(sb);
30566+ if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30567+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
b95c5147 30568+ au_remount_refresh(sb, /*do_idop*/0);
e49829fe
JR
30569+ }
30570+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
30571+ au_plink_put(sb, /*verbose*/1);
30572+ au_xino_clr(sb);
8b6a4947 30573+ au_dr_opt_flush(sb);
1e00d052 30574+ sbinfo->si_sb = NULL;
e49829fe 30575+ aufs_write_unlock(sb->s_root);
e49829fe
JR
30576+ au_nwt_flush(&sbinfo->si_nowait);
30577+ }
98d9a5b1 30578+ kill_anon_super(sb);
e49829fe
JR
30579+}
30580+
1facf9fc 30581+struct file_system_type aufs_fs_type = {
30582+ .name = AUFS_FSTYPE,
c06a8ce3
AM
30583+ /* a race between rename and others */
30584+ .fs_flags = FS_RENAME_DOES_D_MOVE,
027c5e7a 30585+ .mount = aufs_mount,
e49829fe 30586+ .kill_sb = aufs_kill_sb,
1facf9fc 30587+ /* no need to __module_get() and module_put(). */
30588+ .owner = THIS_MODULE,
30589+};
7f207e10
AM
30590diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30591--- /usr/share/empty/fs/aufs/super.h 1970-01-01 01:00:00.000000000 +0100
ba1aed25 30592+++ linux/fs/aufs/super.h 2019-03-05 12:13:00.142557771 +0100
acd2b654 30593@@ -0,0 +1,589 @@
062440b3 30594+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 30595+/*
ba1aed25 30596+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 30597+ *
30598+ * This program, aufs is free software; you can redistribute it and/or modify
30599+ * it under the terms of the GNU General Public License as published by
30600+ * the Free Software Foundation; either version 2 of the License, or
30601+ * (at your option) any later version.
dece6358
AM
30602+ *
30603+ * This program is distributed in the hope that it will be useful,
30604+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30605+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30606+ * GNU General Public License for more details.
30607+ *
30608+ * You should have received a copy of the GNU General Public License
523b37e3 30609+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30610+ */
30611+
30612+/*
30613+ * super_block operations
30614+ */
30615+
30616+#ifndef __AUFS_SUPER_H__
30617+#define __AUFS_SUPER_H__
30618+
30619+#ifdef __KERNEL__
30620+
30621+#include <linux/fs.h>
5527c038 30622+#include <linux/kobject.h>
8b6a4947 30623+#include "hbl.h"
acd2b654 30624+#include "lcnt.h"
1facf9fc 30625+#include "rwsem.h"
1facf9fc 30626+#include "wkq.h"
30627+
1facf9fc 30628+/* policies to select one among multiple writable branches */
30629+struct au_wbr_copyup_operations {
30630+ int (*copyup)(struct dentry *dentry);
30631+};
30632+
392086de
AM
30633+#define AuWbr_DIR 1 /* target is a dir */
30634+#define AuWbr_PARENT (1 << 1) /* always require a parent */
30635+
30636+#define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
30637+#define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
30638+#define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
30639+
1facf9fc 30640+struct au_wbr_create_operations {
392086de 30641+ int (*create)(struct dentry *dentry, unsigned int flags);
1facf9fc 30642+ int (*init)(struct super_block *sb);
30643+ int (*fin)(struct super_block *sb);
30644+};
30645+
30646+struct au_wbr_mfs {
30647+ struct mutex mfs_lock; /* protect this structure */
30648+ unsigned long mfs_jiffy;
30649+ unsigned long mfs_expire;
30650+ aufs_bindex_t mfs_bindex;
30651+
30652+ unsigned long long mfsrr_bytes;
30653+ unsigned long long mfsrr_watermark;
30654+};
30655+
86dc4139
AM
30656+#define AuPlink_NHASH 100
30657+static inline int au_plink_hash(ino_t ino)
30658+{
30659+ return ino % AuPlink_NHASH;
30660+}
30661+
076b876e
AM
30662+/* File-based Hierarchical Storage Management */
30663+struct au_fhsm {
30664+#ifdef CONFIG_AUFS_FHSM
30665+ /* allow only one process who can receive the notification */
30666+ spinlock_t fhsm_spin;
30667+ pid_t fhsm_pid;
30668+ wait_queue_head_t fhsm_wqh;
30669+ atomic_t fhsm_readable;
30670+
c1595e42 30671+ /* these are protected by si_rwsem */
076b876e 30672+ unsigned long fhsm_expire;
c1595e42 30673+ aufs_bindex_t fhsm_bottom;
076b876e
AM
30674+#endif
30675+};
30676+
1facf9fc 30677+struct au_branch;
30678+struct au_sbinfo {
30679+ /* nowait tasks in the system-wide workqueue */
30680+ struct au_nowait_tasks si_nowait;
30681+
b752ccd1 30682+ /*
acd2b654 30683+ * tried sb->s_umount, but failed due to the dependency between i_mutex.
b752ccd1
AM
30684+ * rwsem for au_sbinfo is necessary.
30685+ */
dece6358 30686+ struct au_rwsem si_rwsem;
1facf9fc 30687+
7f207e10 30688+ /*
523b37e3
AM
30689+ * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30690+ * remount.
7f207e10 30691+ */
acd2b654 30692+ au_lcnt_t si_ninodes, si_nfiles;
7f207e10 30693+
1facf9fc 30694+ /* branch management */
30695+ unsigned int si_generation;
30696+
2000de60 30697+ /* see AuSi_ flags */
1facf9fc 30698+ unsigned char au_si_status;
30699+
5afbbe0d 30700+ aufs_bindex_t si_bbot;
7f207e10
AM
30701+
30702+ /* dirty trick to keep br_id plus */
30703+ unsigned int si_last_br_id :
30704+ sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
1facf9fc 30705+ struct au_branch **si_branch;
30706+
30707+ /* policy to select a writable branch */
30708+ unsigned char si_wbr_copyup;
30709+ unsigned char si_wbr_create;
30710+ struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30711+ struct au_wbr_create_operations *si_wbr_create_ops;
30712+
30713+ /* round robin */
30714+ atomic_t si_wbr_rr_next;
30715+
30716+ /* most free space */
30717+ struct au_wbr_mfs si_wbr_mfs;
30718+
076b876e
AM
30719+ /* File-based Hierarchical Storage Management */
30720+ struct au_fhsm si_fhsm;
30721+
1facf9fc 30722+ /* mount flags */
30723+ /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30724+ unsigned int si_mntflags;
30725+
30726+ /* external inode number (bitmap and translation table) */
5527c038
JR
30727+ vfs_readf_t si_xread;
30728+ vfs_writef_t si_xwrite;
acd2b654
AM
30729+ loff_t si_ximaxent; /* max entries in a xino */
30730+
1facf9fc 30731+ struct file *si_xib;
30732+ struct mutex si_xib_mtx; /* protect xib members */
30733+ unsigned long *si_xib_buf;
30734+ unsigned long si_xib_last_pindex;
30735+ int si_xib_next_bit;
acd2b654 30736+
392086de
AM
30737+ unsigned long si_xino_jiffy;
30738+ unsigned long si_xino_expire;
1facf9fc 30739+ /* reserved for future use */
30740+ /* unsigned long long si_xib_limit; */ /* Max xib file size */
30741+
30742+#ifdef CONFIG_AUFS_EXPORT
30743+ /* i_generation */
acd2b654 30744+ /* todo: make xigen file an array to support many inode numbers */
1facf9fc 30745+ struct file *si_xigen;
30746+ atomic_t si_xigen_next;
30747+#endif
30748+
acd2b654 30749+ /* dirty trick to support atomic_open */
8b6a4947 30750+ struct hlist_bl_head si_aopen;
b912730e 30751+
1facf9fc 30752+ /* vdir parameters */
e49829fe 30753+ unsigned long si_rdcache; /* max cache time in jiffies */
1facf9fc 30754+ unsigned int si_rdblk; /* deblk size */
30755+ unsigned int si_rdhash; /* hash size */
30756+
30757+ /*
30758+ * If the number of whiteouts are larger than si_dirwh, leave all of
30759+ * them after au_whtmp_ren to reduce the cost of rmdir(2).
30760+ * future fsck.aufs or kernel thread will remove them later.
30761+ * Otherwise, remove all whiteouts and the dir in rmdir(2).
30762+ */
30763+ unsigned int si_dirwh;
30764+
1facf9fc 30765+ /* pseudo_link list */
8b6a4947 30766+ struct hlist_bl_head si_plink[AuPlink_NHASH];
1facf9fc 30767+ wait_queue_head_t si_plink_wq;
4a4d8108 30768+ spinlock_t si_plink_maint_lock;
e49829fe 30769+ pid_t si_plink_maint_pid;
1facf9fc 30770+
523b37e3 30771+ /* file list */
8b6a4947 30772+ struct hlist_bl_head si_files;
523b37e3 30773+
b95c5147
AM
30774+ /* with/without getattr, brother of sb->s_d_op */
30775+ struct inode_operations *si_iop_array;
30776+
1facf9fc 30777+ /*
30778+ * sysfs and lifetime management.
30779+ * this is not a small structure and it may be a waste of memory in case
acd2b654 30780+ * of sysfs is disabled, particularly when many aufs-es are mounted.
1facf9fc 30781+ * but using sysfs is majority.
30782+ */
30783+ struct kobject si_kobj;
30784+#ifdef CONFIG_DEBUG_FS
86dc4139
AM
30785+ struct dentry *si_dbgaufs;
30786+ struct dentry *si_dbgaufs_plink;
30787+ struct dentry *si_dbgaufs_xib;
1facf9fc 30788+#ifdef CONFIG_AUFS_EXPORT
30789+ struct dentry *si_dbgaufs_xigen;
30790+#endif
30791+#endif
30792+
e49829fe 30793+#ifdef CONFIG_AUFS_SBILIST
8b6a4947 30794+ struct hlist_bl_node si_list;
e49829fe
JR
30795+#endif
30796+
1facf9fc 30797+ /* dirty, necessary for unmounting, sysfs and sysrq */
30798+ struct super_block *si_sb;
30799+};
30800+
dece6358
AM
30801+/* sbinfo status flags */
30802+/*
30803+ * set true when refresh_dirs() failed at remount time.
30804+ * then try refreshing dirs at access time again.
062440b3 30805+ * if it is false, refreshing dirs at access time is unnecessary
dece6358 30806+ */
027c5e7a 30807+#define AuSi_FAILED_REFRESH_DIR 1
076b876e 30808+#define AuSi_FHSM (1 << 1) /* fhsm is active now */
79b8bda9 30809+#define AuSi_NO_DREVAL (1 << 2) /* disable all d_revalidate */
076b876e
AM
30810+
30811+#ifndef CONFIG_AUFS_FHSM
30812+#undef AuSi_FHSM
30813+#define AuSi_FHSM 0
30814+#endif
30815+
dece6358
AM
30816+static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30817+ unsigned int flag)
30818+{
30819+ AuRwMustAnyLock(&sbi->si_rwsem);
30820+ return sbi->au_si_status & flag;
30821+}
30822+#define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
30823+#define au_fset_si(sbinfo, name) do { \
30824+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30825+ (sbinfo)->au_si_status |= AuSi_##name; \
30826+} while (0)
30827+#define au_fclr_si(sbinfo, name) do { \
30828+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30829+ (sbinfo)->au_si_status &= ~AuSi_##name; \
30830+} while (0)
30831+
1facf9fc 30832+/* ---------------------------------------------------------------------- */
30833+
30834+/* policy to select one among writable branches */
4a4d8108
AM
30835+#define AuWbrCopyup(sbinfo, ...) \
30836+ ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30837+#define AuWbrCreate(sbinfo, ...) \
30838+ ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
1facf9fc 30839+
30840+/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30841+#define AuLock_DW 1 /* write-lock dentry */
30842+#define AuLock_IR (1 << 1) /* read-lock inode */
30843+#define AuLock_IW (1 << 2) /* write-lock inode */
30844+#define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
b95c5147 30845+#define AuLock_DIRS (1 << 4) /* target is a pair of dirs */
f2c43d5f 30846+ /* except RENAME_EXCHANGE */
e49829fe
JR
30847+#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
30848+#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
027c5e7a 30849+#define AuLock_GEN (1 << 7) /* test digen/iigen */
1facf9fc 30850+#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
7f207e10
AM
30851+#define au_fset_lock(flags, name) \
30852+ do { (flags) |= AuLock_##name; } while (0)
30853+#define au_fclr_lock(flags, name) \
30854+ do { (flags) &= ~AuLock_##name; } while (0)
1facf9fc 30855+
30856+/* ---------------------------------------------------------------------- */
30857+
30858+/* super.c */
30859+extern struct file_system_type aufs_fs_type;
30860+struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
79b8bda9
AM
30861+typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30862+ unsigned long long max, void *arg);
79b8bda9
AM
30863+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30864+ struct super_block *sb, void *arg);
7f207e10
AM
30865+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30866+void au_iarray_free(struct inode **a, unsigned long long max);
1facf9fc 30867+
30868+/* sbinfo.c */
30869+void au_si_free(struct kobject *kobj);
30870+int au_si_alloc(struct super_block *sb);
e2f27e51 30871+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
1facf9fc 30872+
30873+unsigned int au_sigen_inc(struct super_block *sb);
30874+aufs_bindex_t au_new_br_id(struct super_block *sb);
30875+
e49829fe
JR
30876+int si_read_lock(struct super_block *sb, int flags);
30877+int si_write_lock(struct super_block *sb, int flags);
30878+int aufs_read_lock(struct dentry *dentry, int flags);
1facf9fc 30879+void aufs_read_unlock(struct dentry *dentry, int flags);
30880+void aufs_write_lock(struct dentry *dentry);
30881+void aufs_write_unlock(struct dentry *dentry);
e49829fe 30882+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
1facf9fc 30883+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30884+
30885+/* wbr_policy.c */
30886+extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30887+extern struct au_wbr_create_operations au_wbr_create_ops[];
30888+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
c2b27bf2 30889+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
5afbbe0d 30890+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
c2b27bf2
AM
30891+
30892+/* mvdown.c */
30893+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
1facf9fc 30894+
076b876e
AM
30895+#ifdef CONFIG_AUFS_FHSM
30896+/* fhsm.c */
30897+
30898+static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30899+{
30900+ pid_t pid;
30901+
30902+ spin_lock(&fhsm->fhsm_spin);
30903+ pid = fhsm->fhsm_pid;
30904+ spin_unlock(&fhsm->fhsm_spin);
30905+
30906+ return pid;
30907+}
30908+
30909+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
30910+void au_fhsm_wrote_all(struct super_block *sb, int force);
30911+int au_fhsm_fd(struct super_block *sb, int oflags);
30912+int au_fhsm_br_alloc(struct au_branch *br);
c1595e42 30913+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
076b876e
AM
30914+void au_fhsm_fin(struct super_block *sb);
30915+void au_fhsm_init(struct au_sbinfo *sbinfo);
30916+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
30917+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
30918+#else
30919+AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
30920+ int force)
30921+AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
30922+AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
c1595e42
JR
30923+AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
30924+AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
30925+AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
076b876e
AM
30926+AuStubVoid(au_fhsm_fin, struct super_block *sb)
30927+AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
30928+AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
30929+AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
30930+#endif
30931+
1facf9fc 30932+/* ---------------------------------------------------------------------- */
30933+
30934+static inline struct au_sbinfo *au_sbi(struct super_block *sb)
30935+{
30936+ return sb->s_fs_info;
30937+}
30938+
30939+/* ---------------------------------------------------------------------- */
30940+
30941+#ifdef CONFIG_AUFS_EXPORT
a2a7ad62 30942+int au_test_nfsd(void);
1facf9fc 30943+void au_export_init(struct super_block *sb);
b752ccd1 30944+void au_xigen_inc(struct inode *inode);
1facf9fc 30945+int au_xigen_new(struct inode *inode);
062440b3 30946+int au_xigen_set(struct super_block *sb, struct path *path);
1facf9fc 30947+void au_xigen_clr(struct super_block *sb);
30948+
30949+static inline int au_busy_or_stale(void)
30950+{
b752ccd1 30951+ if (!au_test_nfsd())
1facf9fc 30952+ return -EBUSY;
30953+ return -ESTALE;
30954+}
30955+#else
b752ccd1 30956+AuStubInt0(au_test_nfsd, void)
a2a7ad62 30957+AuStubVoid(au_export_init, struct super_block *sb)
b752ccd1 30958+AuStubVoid(au_xigen_inc, struct inode *inode)
4a4d8108 30959+AuStubInt0(au_xigen_new, struct inode *inode)
062440b3 30960+AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
4a4d8108 30961+AuStubVoid(au_xigen_clr, struct super_block *sb)
c1595e42 30962+AuStub(int, au_busy_or_stale, return -EBUSY, void)
1facf9fc 30963+#endif /* CONFIG_AUFS_EXPORT */
30964+
30965+/* ---------------------------------------------------------------------- */
30966+
e49829fe
JR
30967+#ifdef CONFIG_AUFS_SBILIST
30968+/* module.c */
8b6a4947 30969+extern struct hlist_bl_head au_sbilist;
e49829fe
JR
30970+
30971+static inline void au_sbilist_init(void)
30972+{
8b6a4947 30973+ INIT_HLIST_BL_HEAD(&au_sbilist);
e49829fe
JR
30974+}
30975+
30976+static inline void au_sbilist_add(struct super_block *sb)
30977+{
8b6a4947 30978+ au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe
JR
30979+}
30980+
30981+static inline void au_sbilist_del(struct super_block *sb)
30982+{
8b6a4947 30983+ au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe 30984+}
53392da6
AM
30985+
30986+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
30987+static inline void au_sbilist_lock(void)
30988+{
8b6a4947 30989+ hlist_bl_lock(&au_sbilist);
53392da6
AM
30990+}
30991+
30992+static inline void au_sbilist_unlock(void)
30993+{
8b6a4947 30994+ hlist_bl_unlock(&au_sbilist);
53392da6
AM
30995+}
30996+#define AuGFP_SBILIST GFP_ATOMIC
30997+#else
30998+AuStubVoid(au_sbilist_lock, void)
30999+AuStubVoid(au_sbilist_unlock, void)
31000+#define AuGFP_SBILIST GFP_NOFS
31001+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
e49829fe
JR
31002+#else
31003+AuStubVoid(au_sbilist_init, void)
c1595e42
JR
31004+AuStubVoid(au_sbilist_add, struct super_block *sb)
31005+AuStubVoid(au_sbilist_del, struct super_block *sb)
53392da6
AM
31006+AuStubVoid(au_sbilist_lock, void)
31007+AuStubVoid(au_sbilist_unlock, void)
31008+#define AuGFP_SBILIST GFP_NOFS
e49829fe
JR
31009+#endif
31010+
31011+/* ---------------------------------------------------------------------- */
31012+
1facf9fc 31013+static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
31014+{
dece6358 31015+ /*
c1595e42 31016+ * This function is a dynamic '__init' function actually,
dece6358
AM
31017+ * so the tiny check for si_rwsem is unnecessary.
31018+ */
31019+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
1facf9fc 31020+#ifdef CONFIG_DEBUG_FS
31021+ sbinfo->si_dbgaufs = NULL;
86dc4139 31022+ sbinfo->si_dbgaufs_plink = NULL;
1facf9fc 31023+ sbinfo->si_dbgaufs_xib = NULL;
31024+#ifdef CONFIG_AUFS_EXPORT
31025+ sbinfo->si_dbgaufs_xigen = NULL;
31026+#endif
31027+#endif
31028+}
31029+
31030+/* ---------------------------------------------------------------------- */
31031+
a2654f78
AM
31032+/* current->atomic_flags */
31033+/* this value should never corrupt the ones defined in linux/sched.h */
31034+#define PFA_AUFS 7
31035+
31036+TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
31037+TASK_PFA_SET(AUFS, aufs) /* task_set_aufs */
31038+TASK_PFA_CLEAR(AUFS, aufs) /* task_clear_aufs */
b752ccd1
AM
31039+
31040+static inline int si_pid_test(struct super_block *sb)
31041+{
a2654f78 31042+ return !!task_test_aufs(current);
b752ccd1
AM
31043+}
31044+
31045+static inline void si_pid_clr(struct super_block *sb)
31046+{
a2654f78
AM
31047+ AuDebugOn(!task_test_aufs(current));
31048+ task_clear_aufs(current);
b752ccd1
AM
31049+}
31050+
a2654f78
AM
31051+static inline void si_pid_set(struct super_block *sb)
31052+{
31053+ AuDebugOn(task_test_aufs(current));
31054+ task_set_aufs(current);
31055+}
febd17d6 31056+
b752ccd1
AM
31057+/* ---------------------------------------------------------------------- */
31058+
1facf9fc 31059+/* lock superblock. mainly for entry point functions */
8b6a4947
AM
31060+#define __si_read_lock(sb) au_rw_read_lock(&au_sbi(sb)->si_rwsem)
31061+#define __si_write_lock(sb) au_rw_write_lock(&au_sbi(sb)->si_rwsem)
31062+#define __si_read_trylock(sb) au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
31063+#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
31064+/*
31065+#define __si_read_trylock_nested(sb) \
31066+ au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
31067+#define __si_write_trylock_nested(sb) \
31068+ au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
31069+*/
31070+
31071+#define __si_read_unlock(sb) au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
31072+#define __si_write_unlock(sb) au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
31073+#define __si_downgrade_lock(sb) au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
1facf9fc 31074+
dece6358
AM
31075+#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
31076+#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
31077+#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
31078+
b752ccd1
AM
31079+static inline void si_noflush_read_lock(struct super_block *sb)
31080+{
31081+ __si_read_lock(sb);
31082+ si_pid_set(sb);
31083+}
31084+
31085+static inline int si_noflush_read_trylock(struct super_block *sb)
31086+{
076b876e
AM
31087+ int locked;
31088+
31089+ locked = __si_read_trylock(sb);
b752ccd1
AM
31090+ if (locked)
31091+ si_pid_set(sb);
31092+ return locked;
31093+}
31094+
31095+static inline void si_noflush_write_lock(struct super_block *sb)
31096+{
31097+ __si_write_lock(sb);
31098+ si_pid_set(sb);
31099+}
31100+
31101+static inline int si_noflush_write_trylock(struct super_block *sb)
31102+{
076b876e
AM
31103+ int locked;
31104+
31105+ locked = __si_write_trylock(sb);
b752ccd1
AM
31106+ if (locked)
31107+ si_pid_set(sb);
31108+ return locked;
31109+}
31110+
7e9cd9fe 31111+#if 0 /* reserved */
1facf9fc 31112+static inline int si_read_trylock(struct super_block *sb, int flags)
31113+{
31114+ if (au_ftest_lock(flags, FLUSH))
31115+ au_nwt_flush(&au_sbi(sb)->si_nowait);
31116+ return si_noflush_read_trylock(sb);
31117+}
e49829fe 31118+#endif
1facf9fc 31119+
b752ccd1
AM
31120+static inline void si_read_unlock(struct super_block *sb)
31121+{
31122+ si_pid_clr(sb);
31123+ __si_read_unlock(sb);
31124+}
31125+
7e9cd9fe 31126+#if 0 /* reserved */
1facf9fc 31127+static inline int si_write_trylock(struct super_block *sb, int flags)
31128+{
31129+ if (au_ftest_lock(flags, FLUSH))
31130+ au_nwt_flush(&au_sbi(sb)->si_nowait);
31131+ return si_noflush_write_trylock(sb);
31132+}
b752ccd1
AM
31133+#endif
31134+
31135+static inline void si_write_unlock(struct super_block *sb)
31136+{
31137+ si_pid_clr(sb);
31138+ __si_write_unlock(sb);
31139+}
31140+
7e9cd9fe 31141+#if 0 /* reserved */
b752ccd1
AM
31142+static inline void si_downgrade_lock(struct super_block *sb)
31143+{
31144+ __si_downgrade_lock(sb);
31145+}
31146+#endif
1facf9fc 31147+
31148+/* ---------------------------------------------------------------------- */
31149+
5afbbe0d 31150+static inline aufs_bindex_t au_sbbot(struct super_block *sb)
1facf9fc 31151+{
dece6358 31152+ SiMustAnyLock(sb);
5afbbe0d 31153+ return au_sbi(sb)->si_bbot;
1facf9fc 31154+}
31155+
31156+static inline unsigned int au_mntflags(struct super_block *sb)
31157+{
dece6358 31158+ SiMustAnyLock(sb);
1facf9fc 31159+ return au_sbi(sb)->si_mntflags;
31160+}
31161+
31162+static inline unsigned int au_sigen(struct super_block *sb)
31163+{
dece6358 31164+ SiMustAnyLock(sb);
1facf9fc 31165+ return au_sbi(sb)->si_generation;
31166+}
31167+
31168+static inline struct au_branch *au_sbr(struct super_block *sb,
31169+ aufs_bindex_t bindex)
31170+{
dece6358 31171+ SiMustAnyLock(sb);
1facf9fc 31172+ return au_sbi(sb)->si_branch[0 + bindex];
31173+}
31174+
acd2b654 31175+static inline loff_t au_xi_maxent(struct super_block *sb)
1facf9fc 31176+{
dece6358 31177+ SiMustAnyLock(sb);
acd2b654 31178+ return au_sbi(sb)->si_ximaxent;
1facf9fc 31179+}
31180+
31181+#endif /* __KERNEL__ */
31182+#endif /* __AUFS_SUPER_H__ */
7f207e10
AM
31183diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
31184--- /usr/share/empty/fs/aufs/sysaufs.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 31185+++ linux/fs/aufs/sysaufs.c 2019-03-05 12:13:00.142557771 +0100
062440b3 31186@@ -0,0 +1,93 @@
cd7a4cd9 31187+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31188+/*
ba1aed25 31189+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 31190+ *
31191+ * This program, aufs is free software; you can redistribute it and/or modify
31192+ * it under the terms of the GNU General Public License as published by
31193+ * the Free Software Foundation; either version 2 of the License, or
31194+ * (at your option) any later version.
dece6358
AM
31195+ *
31196+ * This program is distributed in the hope that it will be useful,
31197+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31198+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31199+ * GNU General Public License for more details.
31200+ *
31201+ * You should have received a copy of the GNU General Public License
523b37e3 31202+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31203+ */
31204+
31205+/*
31206+ * sysfs interface and lifetime management
31207+ * they are necessary regardless sysfs is disabled.
31208+ */
31209+
1facf9fc 31210+#include <linux/random.h>
1facf9fc 31211+#include "aufs.h"
31212+
31213+unsigned long sysaufs_si_mask;
e49829fe 31214+struct kset *sysaufs_kset;
1facf9fc 31215+
31216+#define AuSiAttr(_name) { \
31217+ .attr = { .name = __stringify(_name), .mode = 0444 }, \
31218+ .show = sysaufs_si_##_name, \
31219+}
31220+
31221+static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
31222+struct attribute *sysaufs_si_attrs[] = {
31223+ &sysaufs_si_attr_xi_path.attr,
31224+ NULL,
31225+};
31226+
4a4d8108 31227+static const struct sysfs_ops au_sbi_ops = {
1facf9fc 31228+ .show = sysaufs_si_show
31229+};
31230+
31231+static struct kobj_type au_sbi_ktype = {
31232+ .release = au_si_free,
31233+ .sysfs_ops = &au_sbi_ops,
31234+ .default_attrs = sysaufs_si_attrs
31235+};
31236+
31237+/* ---------------------------------------------------------------------- */
31238+
31239+int sysaufs_si_init(struct au_sbinfo *sbinfo)
31240+{
31241+ int err;
31242+
e49829fe 31243+ sbinfo->si_kobj.kset = sysaufs_kset;
1facf9fc 31244+ /* cf. sysaufs_name() */
31245+ err = kobject_init_and_add
e49829fe 31246+ (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
1facf9fc 31247+ SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31248+
1facf9fc 31249+ return err;
31250+}
31251+
31252+void sysaufs_fin(void)
31253+{
e49829fe
JR
31254+ sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31255+ kset_unregister(sysaufs_kset);
1facf9fc 31256+}
31257+
31258+int __init sysaufs_init(void)
31259+{
31260+ int err;
31261+
31262+ do {
31263+ get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31264+ } while (!sysaufs_si_mask);
31265+
4a4d8108 31266+ err = -EINVAL;
e49829fe
JR
31267+ sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31268+ if (unlikely(!sysaufs_kset))
4a4d8108 31269+ goto out;
e49829fe
JR
31270+ err = PTR_ERR(sysaufs_kset);
31271+ if (IS_ERR(sysaufs_kset))
1facf9fc 31272+ goto out;
e49829fe 31273+ err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
062440b3 31274+ if (unlikely(err))
e49829fe 31275+ kset_unregister(sysaufs_kset);
1facf9fc 31276+
4f0767ce 31277+out:
1facf9fc 31278+ return err;
31279+}
7f207e10
AM
31280diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
31281--- /usr/share/empty/fs/aufs/sysaufs.h 1970-01-01 01:00:00.000000000 +0100
ba1aed25 31282+++ linux/fs/aufs/sysaufs.h 2019-03-05 12:13:00.142557771 +0100
062440b3
AM
31283@@ -0,0 +1,102 @@
31284+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 31285+/*
ba1aed25 31286+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 31287+ *
31288+ * This program, aufs is free software; you can redistribute it and/or modify
31289+ * it under the terms of the GNU General Public License as published by
31290+ * the Free Software Foundation; either version 2 of the License, or
31291+ * (at your option) any later version.
dece6358
AM
31292+ *
31293+ * This program is distributed in the hope that it will be useful,
31294+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31295+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31296+ * GNU General Public License for more details.
31297+ *
31298+ * You should have received a copy of the GNU General Public License
523b37e3 31299+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31300+ */
31301+
31302+/*
31303+ * sysfs interface and mount lifetime management
31304+ */
31305+
31306+#ifndef __SYSAUFS_H__
31307+#define __SYSAUFS_H__
31308+
31309+#ifdef __KERNEL__
31310+
1facf9fc 31311+#include <linux/sysfs.h>
1facf9fc 31312+#include "module.h"
31313+
dece6358
AM
31314+struct super_block;
31315+struct au_sbinfo;
31316+
1facf9fc 31317+struct sysaufs_si_attr {
31318+ struct attribute attr;
31319+ int (*show)(struct seq_file *seq, struct super_block *sb);
31320+};
31321+
31322+/* ---------------------------------------------------------------------- */
31323+
31324+/* sysaufs.c */
31325+extern unsigned long sysaufs_si_mask;
e49829fe 31326+extern struct kset *sysaufs_kset;
1facf9fc 31327+extern struct attribute *sysaufs_si_attrs[];
31328+int sysaufs_si_init(struct au_sbinfo *sbinfo);
31329+int __init sysaufs_init(void);
31330+void sysaufs_fin(void);
31331+
31332+/* ---------------------------------------------------------------------- */
31333+
31334+/* some people doesn't like to show a pointer in kernel */
31335+static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31336+{
31337+ return sysaufs_si_mask ^ (unsigned long)sbinfo;
31338+}
31339+
31340+#define SysaufsSiNamePrefix "si_"
31341+#define SysaufsSiNameLen (sizeof(SysaufsSiNamePrefix) + 16)
31342+static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31343+{
31344+ snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31345+ sysaufs_si_id(sbinfo));
31346+}
31347+
31348+struct au_branch;
31349+#ifdef CONFIG_SYSFS
31350+/* sysfs.c */
31351+extern struct attribute_group *sysaufs_attr_group;
31352+
31353+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31354+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31355+ char *buf);
076b876e
AM
31356+long au_brinfo_ioctl(struct file *file, unsigned long arg);
31357+#ifdef CONFIG_COMPAT
31358+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31359+#endif
1facf9fc 31360+
31361+void sysaufs_br_init(struct au_branch *br);
31362+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31363+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31364+
31365+#define sysaufs_brs_init() do {} while (0)
31366+
31367+#else
31368+#define sysaufs_attr_group NULL
31369+
4a4d8108 31370+AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
c1595e42
JR
31371+AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31372+ struct attribute *attr, char *buf)
4a4d8108
AM
31373+AuStubVoid(sysaufs_br_init, struct au_branch *br)
31374+AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31375+AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
1facf9fc 31376+
31377+static inline void sysaufs_brs_init(void)
31378+{
31379+ sysaufs_brs = 0;
31380+}
31381+
31382+#endif /* CONFIG_SYSFS */
31383+
31384+#endif /* __KERNEL__ */
31385+#endif /* __SYSAUFS_H__ */
7f207e10
AM
31386diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31387--- /usr/share/empty/fs/aufs/sysfs.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25
AM
31388+++ linux/fs/aufs/sysfs.c 2019-03-05 12:13:00.142557771 +0100
31389@@ -0,0 +1,374 @@
cd7a4cd9 31390+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31391+/*
ba1aed25 31392+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 31393+ *
31394+ * This program, aufs is free software; you can redistribute it and/or modify
31395+ * it under the terms of the GNU General Public License as published by
31396+ * the Free Software Foundation; either version 2 of the License, or
31397+ * (at your option) any later version.
dece6358
AM
31398+ *
31399+ * This program is distributed in the hope that it will be useful,
31400+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31401+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31402+ * GNU General Public License for more details.
31403+ *
31404+ * You should have received a copy of the GNU General Public License
523b37e3 31405+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31406+ */
31407+
31408+/*
31409+ * sysfs interface
31410+ */
31411+
076b876e 31412+#include <linux/compat.h>
1facf9fc 31413+#include <linux/seq_file.h>
1facf9fc 31414+#include "aufs.h"
31415+
4a4d8108
AM
31416+#ifdef CONFIG_AUFS_FS_MODULE
31417+/* this entry violates the "one line per file" policy of sysfs */
31418+static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31419+ char *buf)
31420+{
31421+ ssize_t err;
31422+ static char *conf =
31423+/* this file is generated at compiling */
31424+#include "conf.str"
31425+ ;
31426+
31427+ err = snprintf(buf, PAGE_SIZE, conf);
31428+ if (unlikely(err >= PAGE_SIZE))
31429+ err = -EFBIG;
31430+ return err;
31431+}
31432+
31433+static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31434+#endif
31435+
1facf9fc 31436+static struct attribute *au_attr[] = {
4a4d8108
AM
31437+#ifdef CONFIG_AUFS_FS_MODULE
31438+ &au_config_attr.attr,
31439+#endif
1facf9fc 31440+ NULL, /* need to NULL terminate the list of attributes */
31441+};
31442+
31443+static struct attribute_group sysaufs_attr_group_body = {
31444+ .attrs = au_attr
31445+};
31446+
31447+struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31448+
31449+/* ---------------------------------------------------------------------- */
31450+
31451+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31452+{
31453+ int err;
31454+
dece6358
AM
31455+ SiMustAnyLock(sb);
31456+
1facf9fc 31457+ err = 0;
31458+ if (au_opt_test(au_mntflags(sb), XINO)) {
31459+ err = au_xino_path(seq, au_sbi(sb)->si_xib);
31460+ seq_putc(seq, '\n');
31461+ }
31462+ return err;
31463+}
31464+
31465+/*
31466+ * the lifetime of branch is independent from the entry under sysfs.
31467+ * sysfs handles the lifetime of the entry, and never call ->show() after it is
31468+ * unlinked.
31469+ */
31470+static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
392086de 31471+ aufs_bindex_t bindex, int idx)
1facf9fc 31472+{
1e00d052 31473+ int err;
1facf9fc 31474+ struct path path;
31475+ struct dentry *root;
31476+ struct au_branch *br;
076b876e 31477+ au_br_perm_str_t perm;
1facf9fc 31478+
31479+ AuDbg("b%d\n", bindex);
31480+
1e00d052 31481+ err = 0;
1facf9fc 31482+ root = sb->s_root;
31483+ di_read_lock_parent(root, !AuLock_IR);
31484+ br = au_sbr(sb, bindex);
392086de
AM
31485+
31486+ switch (idx) {
31487+ case AuBrSysfs_BR:
31488+ path.mnt = au_br_mnt(br);
31489+ path.dentry = au_h_dptr(root, bindex);
79b8bda9
AM
31490+ err = au_seq_path(seq, &path);
31491+ if (!err) {
31492+ au_optstr_br_perm(&perm, br->br_perm);
31493+ seq_printf(seq, "=%s\n", perm.a);
31494+ }
392086de
AM
31495+ break;
31496+ case AuBrSysfs_BRID:
79b8bda9 31497+ seq_printf(seq, "%d\n", br->br_id);
392086de
AM
31498+ break;
31499+ }
076b876e 31500+ di_read_unlock(root, !AuLock_IR);
79b8bda9 31501+ if (unlikely(err || seq_has_overflowed(seq)))
076b876e 31502+ err = -E2BIG;
392086de 31503+
1e00d052 31504+ return err;
1facf9fc 31505+}
31506+
31507+/* ---------------------------------------------------------------------- */
31508+
31509+static struct seq_file *au_seq(char *p, ssize_t len)
31510+{
31511+ struct seq_file *seq;
31512+
31513+ seq = kzalloc(sizeof(*seq), GFP_NOFS);
31514+ if (seq) {
31515+ /* mutex_init(&seq.lock); */
31516+ seq->buf = p;
31517+ seq->size = len;
31518+ return seq; /* success */
31519+ }
31520+
31521+ seq = ERR_PTR(-ENOMEM);
31522+ return seq;
31523+}
31524+
392086de
AM
31525+#define SysaufsBr_PREFIX "br"
31526+#define SysaufsBrid_PREFIX "brid"
1facf9fc 31527+
31528+/* todo: file size may exceed PAGE_SIZE */
31529+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
1308ab2a 31530+ char *buf)
1facf9fc 31531+{
31532+ ssize_t err;
392086de 31533+ int idx;
1facf9fc 31534+ long l;
5afbbe0d 31535+ aufs_bindex_t bbot;
1facf9fc 31536+ struct au_sbinfo *sbinfo;
31537+ struct super_block *sb;
31538+ struct seq_file *seq;
31539+ char *name;
31540+ struct attribute **cattr;
31541+
31542+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31543+ sb = sbinfo->si_sb;
1308ab2a 31544+
31545+ /*
31546+ * prevent a race condition between sysfs and aufs.
31547+ * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31548+ * prohibits maintaining the sysfs entries.
31549+ * hew we acquire read lock after sysfs_get_active_two().
31550+ * on the other hand, the remount process may maintain the sysfs/aufs
31551+ * entries after acquiring write lock.
31552+ * it can cause a deadlock.
31553+ * simply we gave up processing read here.
31554+ */
31555+ err = -EBUSY;
31556+ if (unlikely(!si_noflush_read_trylock(sb)))
31557+ goto out;
1facf9fc 31558+
31559+ seq = au_seq(buf, PAGE_SIZE);
31560+ err = PTR_ERR(seq);
31561+ if (IS_ERR(seq))
1308ab2a 31562+ goto out_unlock;
1facf9fc 31563+
31564+ name = (void *)attr->name;
31565+ cattr = sysaufs_si_attrs;
31566+ while (*cattr) {
31567+ if (!strcmp(name, (*cattr)->name)) {
31568+ err = container_of(*cattr, struct sysaufs_si_attr, attr)
31569+ ->show(seq, sb);
31570+ goto out_seq;
31571+ }
31572+ cattr++;
31573+ }
31574+
392086de
AM
31575+ if (!strncmp(name, SysaufsBrid_PREFIX,
31576+ sizeof(SysaufsBrid_PREFIX) - 1)) {
31577+ idx = AuBrSysfs_BRID;
31578+ name += sizeof(SysaufsBrid_PREFIX) - 1;
31579+ } else if (!strncmp(name, SysaufsBr_PREFIX,
31580+ sizeof(SysaufsBr_PREFIX) - 1)) {
31581+ idx = AuBrSysfs_BR;
1facf9fc 31582+ name += sizeof(SysaufsBr_PREFIX) - 1;
392086de
AM
31583+ } else
31584+ BUG();
31585+
31586+ err = kstrtol(name, 10, &l);
31587+ if (!err) {
5afbbe0d
AM
31588+ bbot = au_sbbot(sb);
31589+ if (l <= bbot)
392086de
AM
31590+ err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31591+ else
31592+ err = -ENOENT;
1facf9fc 31593+ }
1facf9fc 31594+
4f0767ce 31595+out_seq:
1facf9fc 31596+ if (!err) {
31597+ err = seq->count;
31598+ /* sysfs limit */
31599+ if (unlikely(err == PAGE_SIZE))
31600+ err = -EFBIG;
31601+ }
9f237c51 31602+ au_kfree_rcu(seq);
4f0767ce 31603+out_unlock:
1facf9fc 31604+ si_read_unlock(sb);
4f0767ce 31605+out:
1facf9fc 31606+ return err;
31607+}
31608+
31609+/* ---------------------------------------------------------------------- */
31610+
076b876e
AM
31611+static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31612+{
31613+ int err;
31614+ int16_t brid;
5afbbe0d 31615+ aufs_bindex_t bindex, bbot;
076b876e
AM
31616+ size_t sz;
31617+ char *buf;
31618+ struct seq_file *seq;
31619+ struct au_branch *br;
31620+
31621+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d
AM
31622+ bbot = au_sbbot(sb);
31623+ err = bbot + 1;
076b876e
AM
31624+ if (!arg)
31625+ goto out;
31626+
31627+ err = -ENOMEM;
31628+ buf = (void *)__get_free_page(GFP_NOFS);
31629+ if (unlikely(!buf))
31630+ goto out;
31631+
31632+ seq = au_seq(buf, PAGE_SIZE);
31633+ err = PTR_ERR(seq);
31634+ if (IS_ERR(seq))
31635+ goto out_buf;
31636+
31637+ sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
5afbbe0d 31638+ for (bindex = 0; bindex <= bbot; bindex++, arg++) {
ba1aed25
AM
31639+ /* VERIFY_WRITE */
31640+ err = !access_ok(arg, sizeof(*arg));
076b876e
AM
31641+ if (unlikely(err))
31642+ break;
31643+
31644+ br = au_sbr(sb, bindex);
31645+ brid = br->br_id;
31646+ BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31647+ err = __put_user(brid, &arg->id);
31648+ if (unlikely(err))
31649+ break;
31650+
31651+ BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31652+ err = __put_user(br->br_perm, &arg->perm);
31653+ if (unlikely(err))
31654+ break;
31655+
79b8bda9
AM
31656+ err = au_seq_path(seq, &br->br_path);
31657+ if (unlikely(err))
31658+ break;
31659+ seq_putc(seq, '\0');
31660+ if (!seq_has_overflowed(seq)) {
076b876e
AM
31661+ err = copy_to_user(arg->path, seq->buf, seq->count);
31662+ seq->count = 0;
31663+ if (unlikely(err))
31664+ break;
31665+ } else {
31666+ err = -E2BIG;
31667+ goto out_seq;
31668+ }
31669+ }
31670+ if (unlikely(err))
31671+ err = -EFAULT;
31672+
31673+out_seq:
9f237c51 31674+ au_kfree_rcu(seq);
076b876e 31675+out_buf:
1c60b727 31676+ free_page((unsigned long)buf);
076b876e
AM
31677+out:
31678+ si_read_unlock(sb);
31679+ return err;
31680+}
31681+
31682+long au_brinfo_ioctl(struct file *file, unsigned long arg)
31683+{
2000de60 31684+ return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
076b876e
AM
31685+}
31686+
31687+#ifdef CONFIG_COMPAT
31688+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31689+{
2000de60 31690+ return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
076b876e
AM
31691+}
31692+#endif
31693+
31694+/* ---------------------------------------------------------------------- */
31695+
1facf9fc 31696+void sysaufs_br_init(struct au_branch *br)
31697+{
392086de
AM
31698+ int i;
31699+ struct au_brsysfs *br_sysfs;
31700+ struct attribute *attr;
4a4d8108 31701+
392086de
AM
31702+ br_sysfs = br->br_sysfs;
31703+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31704+ attr = &br_sysfs->attr;
31705+ sysfs_attr_init(attr);
31706+ attr->name = br_sysfs->name;
cd7a4cd9 31707+ attr->mode = 0444;
392086de
AM
31708+ br_sysfs++;
31709+ }
1facf9fc 31710+}
31711+
31712+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31713+{
31714+ struct au_branch *br;
31715+ struct kobject *kobj;
392086de
AM
31716+ struct au_brsysfs *br_sysfs;
31717+ int i;
5afbbe0d 31718+ aufs_bindex_t bbot;
1facf9fc 31719+
1facf9fc 31720+ if (!sysaufs_brs)
31721+ return;
31722+
31723+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
31724+ bbot = au_sbbot(sb);
31725+ for (; bindex <= bbot; bindex++) {
1facf9fc 31726+ br = au_sbr(sb, bindex);
392086de
AM
31727+ br_sysfs = br->br_sysfs;
31728+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31729+ sysfs_remove_file(kobj, &br_sysfs->attr);
31730+ br_sysfs++;
31731+ }
1facf9fc 31732+ }
31733+}
31734+
31735+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31736+{
392086de 31737+ int err, i;
5afbbe0d 31738+ aufs_bindex_t bbot;
1facf9fc 31739+ struct kobject *kobj;
31740+ struct au_branch *br;
392086de 31741+ struct au_brsysfs *br_sysfs;
1facf9fc 31742+
1facf9fc 31743+ if (!sysaufs_brs)
31744+ return;
31745+
31746+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
31747+ bbot = au_sbbot(sb);
31748+ for (; bindex <= bbot; bindex++) {
1facf9fc 31749+ br = au_sbr(sb, bindex);
392086de
AM
31750+ br_sysfs = br->br_sysfs;
31751+ snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31752+ SysaufsBr_PREFIX "%d", bindex);
31753+ snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31754+ SysaufsBrid_PREFIX "%d", bindex);
31755+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31756+ err = sysfs_create_file(kobj, &br_sysfs->attr);
31757+ if (unlikely(err))
31758+ pr_warn("failed %s under sysfs(%d)\n",
31759+ br_sysfs->name, err);
31760+ br_sysfs++;
31761+ }
1facf9fc 31762+ }
31763+}
7f207e10
AM
31764diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31765--- /usr/share/empty/fs/aufs/sysrq.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 31766+++ linux/fs/aufs/sysrq.c 2019-03-05 12:13:00.142557771 +0100
062440b3 31767@@ -0,0 +1,160 @@
cd7a4cd9 31768+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31769+/*
ba1aed25 31770+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 31771+ *
31772+ * This program, aufs is free software; you can redistribute it and/or modify
31773+ * it under the terms of the GNU General Public License as published by
31774+ * the Free Software Foundation; either version 2 of the License, or
31775+ * (at your option) any later version.
dece6358
AM
31776+ *
31777+ * This program is distributed in the hope that it will be useful,
31778+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31779+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31780+ * GNU General Public License for more details.
31781+ *
31782+ * You should have received a copy of the GNU General Public License
523b37e3 31783+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31784+ */
31785+
31786+/*
acd2b654 31787+ * magic sysrq handler
1facf9fc 31788+ */
31789+
1facf9fc 31790+/* #include <linux/sysrq.h> */
027c5e7a 31791+#include <linux/writeback.h>
1facf9fc 31792+#include "aufs.h"
31793+
31794+/* ---------------------------------------------------------------------- */
31795+
31796+static void sysrq_sb(struct super_block *sb)
31797+{
31798+ char *plevel;
31799+ struct au_sbinfo *sbinfo;
31800+ struct file *file;
8b6a4947
AM
31801+ struct hlist_bl_head *files;
31802+ struct hlist_bl_node *pos;
523b37e3 31803+ struct au_finfo *finfo;
1facf9fc 31804+
31805+ plevel = au_plevel;
31806+ au_plevel = KERN_WARNING;
1facf9fc 31807+
4a4d8108 31808+ /* since we define pr_fmt, call printk directly */
c06a8ce3
AM
31809+#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31810+
31811+ sbinfo = au_sbi(sb);
4a4d8108 31812+ printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
c06a8ce3 31813+ pr("superblock\n");
1facf9fc 31814+ au_dpri_sb(sb);
027c5e7a
AM
31815+
31816+#if 0
c06a8ce3 31817+ pr("root dentry\n");
1facf9fc 31818+ au_dpri_dentry(sb->s_root);
c06a8ce3 31819+ pr("root inode\n");
5527c038 31820+ au_dpri_inode(d_inode(sb->s_root));
027c5e7a
AM
31821+#endif
31822+
1facf9fc 31823+#if 0
027c5e7a
AM
31824+ do {
31825+ int err, i, j, ndentry;
31826+ struct au_dcsub_pages dpages;
31827+ struct au_dpage *dpage;
31828+
31829+ err = au_dpages_init(&dpages, GFP_ATOMIC);
31830+ if (unlikely(err))
31831+ break;
31832+ err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31833+ if (!err)
31834+ for (i = 0; i < dpages.ndpage; i++) {
31835+ dpage = dpages.dpages + i;
31836+ ndentry = dpage->ndentry;
31837+ for (j = 0; j < ndentry; j++)
31838+ au_dpri_dentry(dpage->dentries[j]);
31839+ }
31840+ au_dpages_free(&dpages);
31841+ } while (0);
31842+#endif
31843+
31844+#if 1
31845+ {
31846+ struct inode *i;
076b876e 31847+
c06a8ce3 31848+ pr("isolated inode\n");
79b8bda9 31849+ spin_lock(&sb->s_inode_list_lock);
2cbb1c4b
JR
31850+ list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31851+ spin_lock(&i->i_lock);
b4510431 31852+ if (1 || hlist_empty(&i->i_dentry))
027c5e7a 31853+ au_dpri_inode(i);
2cbb1c4b
JR
31854+ spin_unlock(&i->i_lock);
31855+ }
79b8bda9 31856+ spin_unlock(&sb->s_inode_list_lock);
027c5e7a 31857+ }
1facf9fc 31858+#endif
c06a8ce3 31859+ pr("files\n");
523b37e3 31860+ files = &au_sbi(sb)->si_files;
8b6a4947
AM
31861+ hlist_bl_lock(files);
31862+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
4a4d8108 31863+ umode_t mode;
076b876e 31864+
523b37e3 31865+ file = finfo->fi_file;
c06a8ce3 31866+ mode = file_inode(file)->i_mode;
38d290e6 31867+ if (!special_file(mode))
1facf9fc 31868+ au_dpri_file(file);
523b37e3 31869+ }
8b6a4947 31870+ hlist_bl_unlock(files);
c06a8ce3 31871+ pr("done\n");
1facf9fc 31872+
c06a8ce3 31873+#undef pr
1facf9fc 31874+ au_plevel = plevel;
1facf9fc 31875+}
31876+
31877+/* ---------------------------------------------------------------------- */
31878+
31879+/* module parameter */
31880+static char *aufs_sysrq_key = "a";
cd7a4cd9 31881+module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
1facf9fc 31882+MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31883+
0c5527e5 31884+static void au_sysrq(int key __maybe_unused)
1facf9fc 31885+{
1facf9fc 31886+ struct au_sbinfo *sbinfo;
8b6a4947 31887+ struct hlist_bl_node *pos;
1facf9fc 31888+
027c5e7a 31889+ lockdep_off();
53392da6 31890+ au_sbilist_lock();
8b6a4947 31891+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
1facf9fc 31892+ sysrq_sb(sbinfo->si_sb);
53392da6 31893+ au_sbilist_unlock();
027c5e7a 31894+ lockdep_on();
1facf9fc 31895+}
31896+
31897+static struct sysrq_key_op au_sysrq_op = {
31898+ .handler = au_sysrq,
31899+ .help_msg = "Aufs",
31900+ .action_msg = "Aufs",
31901+ .enable_mask = SYSRQ_ENABLE_DUMP
31902+};
31903+
31904+/* ---------------------------------------------------------------------- */
31905+
31906+int __init au_sysrq_init(void)
31907+{
31908+ int err;
31909+ char key;
31910+
31911+ err = -1;
31912+ key = *aufs_sysrq_key;
31913+ if ('a' <= key && key <= 'z')
31914+ err = register_sysrq_key(key, &au_sysrq_op);
31915+ if (unlikely(err))
4a4d8108 31916+ pr_err("err %d, sysrq=%c\n", err, key);
1facf9fc 31917+ return err;
31918+}
31919+
31920+void au_sysrq_fin(void)
31921+{
31922+ int err;
076b876e 31923+
1facf9fc 31924+ err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31925+ if (unlikely(err))
4a4d8108 31926+ pr_err("err %d (ignored)\n", err);
1facf9fc 31927+}
7f207e10
AM
31928diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
31929--- /usr/share/empty/fs/aufs/vdir.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 31930+++ linux/fs/aufs/vdir.c 2019-03-05 12:13:00.145891204 +0100
acd2b654 31931@@ -0,0 +1,895 @@
cd7a4cd9 31932+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31933+/*
ba1aed25 31934+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 31935+ *
31936+ * This program, aufs is free software; you can redistribute it and/or modify
31937+ * it under the terms of the GNU General Public License as published by
31938+ * the Free Software Foundation; either version 2 of the License, or
31939+ * (at your option) any later version.
dece6358
AM
31940+ *
31941+ * This program is distributed in the hope that it will be useful,
31942+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31943+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31944+ * GNU General Public License for more details.
31945+ *
31946+ * You should have received a copy of the GNU General Public License
523b37e3 31947+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31948+ */
31949+
31950+/*
31951+ * virtual or vertical directory
31952+ */
31953+
31954+#include "aufs.h"
31955+
dece6358 31956+static unsigned int calc_size(int nlen)
1facf9fc 31957+{
dece6358 31958+ return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
1facf9fc 31959+}
31960+
31961+static int set_deblk_end(union au_vdir_deblk_p *p,
31962+ union au_vdir_deblk_p *deblk_end)
31963+{
31964+ if (calc_size(0) <= deblk_end->deblk - p->deblk) {
31965+ p->de->de_str.len = 0;
31966+ /* smp_mb(); */
31967+ return 0;
31968+ }
31969+ return -1; /* error */
31970+}
31971+
31972+/* returns true or false */
31973+static int is_deblk_end(union au_vdir_deblk_p *p,
31974+ union au_vdir_deblk_p *deblk_end)
31975+{
31976+ if (calc_size(0) <= deblk_end->deblk - p->deblk)
31977+ return !p->de->de_str.len;
31978+ return 1;
31979+}
31980+
31981+static unsigned char *last_deblk(struct au_vdir *vdir)
31982+{
31983+ return vdir->vd_deblk[vdir->vd_nblk - 1];
31984+}
31985+
31986+/* ---------------------------------------------------------------------- */
31987+
79b8bda9 31988+/* estimate the appropriate size for name hash table */
1308ab2a 31989+unsigned int au_rdhash_est(loff_t sz)
31990+{
31991+ unsigned int n;
31992+
31993+ n = UINT_MAX;
31994+ sz >>= 10;
31995+ if (sz < n)
31996+ n = sz;
31997+ if (sz < AUFS_RDHASH_DEF)
31998+ n = AUFS_RDHASH_DEF;
4a4d8108 31999+ /* pr_info("n %u\n", n); */
1308ab2a 32000+ return n;
32001+}
32002+
1facf9fc 32003+/*
32004+ * the allocated memory has to be freed by
dece6358 32005+ * au_nhash_wh_free() or au_nhash_de_free().
1facf9fc 32006+ */
dece6358 32007+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
1facf9fc 32008+{
1facf9fc 32009+ struct hlist_head *head;
dece6358 32010+ unsigned int u;
076b876e 32011+ size_t sz;
1facf9fc 32012+
076b876e
AM
32013+ sz = sizeof(*nhash->nh_head) * num_hash;
32014+ head = kmalloc(sz, gfp);
dece6358
AM
32015+ if (head) {
32016+ nhash->nh_num = num_hash;
32017+ nhash->nh_head = head;
32018+ for (u = 0; u < num_hash; u++)
1facf9fc 32019+ INIT_HLIST_HEAD(head++);
dece6358 32020+ return 0; /* success */
1facf9fc 32021+ }
1facf9fc 32022+
dece6358 32023+ return -ENOMEM;
1facf9fc 32024+}
32025+
dece6358
AM
32026+static void nhash_count(struct hlist_head *head)
32027+{
32028+#if 0
32029+ unsigned long n;
32030+ struct hlist_node *pos;
32031+
32032+ n = 0;
32033+ hlist_for_each(pos, head)
32034+ n++;
4a4d8108 32035+ pr_info("%lu\n", n);
dece6358
AM
32036+#endif
32037+}
32038+
32039+static void au_nhash_wh_do_free(struct hlist_head *head)
1facf9fc 32040+{
c06a8ce3
AM
32041+ struct au_vdir_wh *pos;
32042+ struct hlist_node *node;
1facf9fc 32043+
c06a8ce3 32044+ hlist_for_each_entry_safe(pos, node, head, wh_hash)
9f237c51 32045+ au_kfree_rcu(pos);
1facf9fc 32046+}
32047+
dece6358 32048+static void au_nhash_de_do_free(struct hlist_head *head)
1facf9fc 32049+{
c06a8ce3
AM
32050+ struct au_vdir_dehstr *pos;
32051+ struct hlist_node *node;
1facf9fc 32052+
c06a8ce3 32053+ hlist_for_each_entry_safe(pos, node, head, hash)
1c60b727 32054+ au_cache_free_vdir_dehstr(pos);
1facf9fc 32055+}
32056+
dece6358
AM
32057+static void au_nhash_do_free(struct au_nhash *nhash,
32058+ void (*free)(struct hlist_head *head))
1facf9fc 32059+{
1308ab2a 32060+ unsigned int n;
1facf9fc 32061+ struct hlist_head *head;
1facf9fc 32062+
dece6358 32063+ n = nhash->nh_num;
1308ab2a 32064+ if (!n)
32065+ return;
32066+
dece6358 32067+ head = nhash->nh_head;
1308ab2a 32068+ while (n-- > 0) {
dece6358
AM
32069+ nhash_count(head);
32070+ free(head++);
1facf9fc 32071+ }
9f237c51 32072+ au_kfree_try_rcu(nhash->nh_head);
1facf9fc 32073+}
32074+
dece6358 32075+void au_nhash_wh_free(struct au_nhash *whlist)
1facf9fc 32076+{
dece6358
AM
32077+ au_nhash_do_free(whlist, au_nhash_wh_do_free);
32078+}
1facf9fc 32079+
dece6358
AM
32080+static void au_nhash_de_free(struct au_nhash *delist)
32081+{
32082+ au_nhash_do_free(delist, au_nhash_de_do_free);
1facf9fc 32083+}
32084+
32085+/* ---------------------------------------------------------------------- */
32086+
32087+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
32088+ int limit)
32089+{
32090+ int num;
32091+ unsigned int u, n;
32092+ struct hlist_head *head;
c06a8ce3 32093+ struct au_vdir_wh *pos;
1facf9fc 32094+
32095+ num = 0;
32096+ n = whlist->nh_num;
32097+ head = whlist->nh_head;
1308ab2a 32098+ for (u = 0; u < n; u++, head++)
c06a8ce3
AM
32099+ hlist_for_each_entry(pos, head, wh_hash)
32100+ if (pos->wh_bindex == btgt && ++num > limit)
1facf9fc 32101+ return 1;
1facf9fc 32102+ return 0;
32103+}
32104+
32105+static struct hlist_head *au_name_hash(struct au_nhash *nhash,
dece6358 32106+ unsigned char *name,
1facf9fc 32107+ unsigned int len)
32108+{
dece6358
AM
32109+ unsigned int v;
32110+ /* const unsigned int magic_bit = 12; */
32111+
1308ab2a 32112+ AuDebugOn(!nhash->nh_num || !nhash->nh_head);
32113+
dece6358 32114+ v = 0;
f0c0a007
AM
32115+ if (len > 8)
32116+ len = 8;
dece6358
AM
32117+ while (len--)
32118+ v += *name++;
32119+ /* v = hash_long(v, magic_bit); */
32120+ v %= nhash->nh_num;
32121+ return nhash->nh_head + v;
32122+}
32123+
32124+static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
32125+ int nlen)
32126+{
32127+ return str->len == nlen && !memcmp(str->name, name, nlen);
1facf9fc 32128+}
32129+
32130+/* returns found or not */
dece6358 32131+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
1facf9fc 32132+{
32133+ struct hlist_head *head;
c06a8ce3 32134+ struct au_vdir_wh *pos;
1facf9fc 32135+ struct au_vdir_destr *str;
32136+
dece6358 32137+ head = au_name_hash(whlist, name, nlen);
c06a8ce3
AM
32138+ hlist_for_each_entry(pos, head, wh_hash) {
32139+ str = &pos->wh_str;
1facf9fc 32140+ AuDbg("%.*s\n", str->len, str->name);
dece6358
AM
32141+ if (au_nhash_test_name(str, name, nlen))
32142+ return 1;
32143+ }
32144+ return 0;
32145+}
32146+
32147+/* returns found(true) or not */
32148+static int test_known(struct au_nhash *delist, char *name, int nlen)
32149+{
32150+ struct hlist_head *head;
c06a8ce3 32151+ struct au_vdir_dehstr *pos;
dece6358
AM
32152+ struct au_vdir_destr *str;
32153+
32154+ head = au_name_hash(delist, name, nlen);
c06a8ce3
AM
32155+ hlist_for_each_entry(pos, head, hash) {
32156+ str = pos->str;
dece6358
AM
32157+ AuDbg("%.*s\n", str->len, str->name);
32158+ if (au_nhash_test_name(str, name, nlen))
1facf9fc 32159+ return 1;
32160+ }
32161+ return 0;
32162+}
32163+
dece6358
AM
32164+static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
32165+ unsigned char d_type)
32166+{
32167+#ifdef CONFIG_AUFS_SHWH
32168+ wh->wh_ino = ino;
32169+ wh->wh_type = d_type;
32170+#endif
32171+}
32172+
32173+/* ---------------------------------------------------------------------- */
32174+
32175+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
32176+ unsigned int d_type, aufs_bindex_t bindex,
32177+ unsigned char shwh)
1facf9fc 32178+{
32179+ int err;
32180+ struct au_vdir_destr *str;
32181+ struct au_vdir_wh *wh;
32182+
dece6358 32183+ AuDbg("%.*s\n", nlen, name);
1308ab2a 32184+ AuDebugOn(!whlist->nh_num || !whlist->nh_head);
32185+
1facf9fc 32186+ err = -ENOMEM;
dece6358 32187+ wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
1facf9fc 32188+ if (unlikely(!wh))
32189+ goto out;
32190+
32191+ err = 0;
32192+ wh->wh_bindex = bindex;
dece6358
AM
32193+ if (shwh)
32194+ au_shwh_init_wh(wh, ino, d_type);
1facf9fc 32195+ str = &wh->wh_str;
dece6358
AM
32196+ str->len = nlen;
32197+ memcpy(str->name, name, nlen);
32198+ hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
1facf9fc 32199+ /* smp_mb(); */
32200+
4f0767ce 32201+out:
1facf9fc 32202+ return err;
32203+}
32204+
1facf9fc 32205+static int append_deblk(struct au_vdir *vdir)
32206+{
32207+ int err;
dece6358 32208+ unsigned long ul;
1facf9fc 32209+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
32210+ union au_vdir_deblk_p p, deblk_end;
32211+ unsigned char **o;
32212+
32213+ err = -ENOMEM;
e2f27e51
AM
32214+ o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32215+ GFP_NOFS, /*may_shrink*/0);
1facf9fc 32216+ if (unlikely(!o))
32217+ goto out;
32218+
32219+ vdir->vd_deblk = o;
32220+ p.deblk = kmalloc(deblk_sz, GFP_NOFS);
32221+ if (p.deblk) {
32222+ ul = vdir->vd_nblk++;
32223+ vdir->vd_deblk[ul] = p.deblk;
32224+ vdir->vd_last.ul = ul;
32225+ vdir->vd_last.p.deblk = p.deblk;
32226+ deblk_end.deblk = p.deblk + deblk_sz;
32227+ err = set_deblk_end(&p, &deblk_end);
32228+ }
32229+
4f0767ce 32230+out:
1facf9fc 32231+ return err;
32232+}
32233+
dece6358
AM
32234+static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
32235+ unsigned int d_type, struct au_nhash *delist)
32236+{
32237+ int err;
32238+ unsigned int sz;
32239+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
32240+ union au_vdir_deblk_p p, *room, deblk_end;
32241+ struct au_vdir_dehstr *dehstr;
32242+
32243+ p.deblk = last_deblk(vdir);
32244+ deblk_end.deblk = p.deblk + deblk_sz;
32245+ room = &vdir->vd_last.p;
32246+ AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32247+ || !is_deblk_end(room, &deblk_end));
32248+
32249+ sz = calc_size(nlen);
32250+ if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32251+ err = append_deblk(vdir);
32252+ if (unlikely(err))
32253+ goto out;
32254+
32255+ p.deblk = last_deblk(vdir);
32256+ deblk_end.deblk = p.deblk + deblk_sz;
32257+ /* smp_mb(); */
32258+ AuDebugOn(room->deblk != p.deblk);
32259+ }
32260+
32261+ err = -ENOMEM;
4a4d8108 32262+ dehstr = au_cache_alloc_vdir_dehstr();
dece6358
AM
32263+ if (unlikely(!dehstr))
32264+ goto out;
32265+
32266+ dehstr->str = &room->de->de_str;
32267+ hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32268+ room->de->de_ino = ino;
32269+ room->de->de_type = d_type;
32270+ room->de->de_str.len = nlen;
32271+ memcpy(room->de->de_str.name, name, nlen);
32272+
32273+ err = 0;
32274+ room->deblk += sz;
32275+ if (unlikely(set_deblk_end(room, &deblk_end)))
32276+ err = append_deblk(vdir);
32277+ /* smp_mb(); */
32278+
4f0767ce 32279+out:
dece6358
AM
32280+ return err;
32281+}
32282+
32283+/* ---------------------------------------------------------------------- */
32284+
1c60b727 32285+void au_vdir_free(struct au_vdir *vdir)
dece6358
AM
32286+{
32287+ unsigned char **deblk;
32288+
32289+ deblk = vdir->vd_deblk;
1c60b727 32290+ while (vdir->vd_nblk--)
9f237c51
AM
32291+ au_kfree_try_rcu(*deblk++);
32292+ au_kfree_try_rcu(vdir->vd_deblk);
1c60b727 32293+ au_cache_free_vdir(vdir);
dece6358
AM
32294+}
32295+
1308ab2a 32296+static struct au_vdir *alloc_vdir(struct file *file)
1facf9fc 32297+{
32298+ struct au_vdir *vdir;
1308ab2a 32299+ struct super_block *sb;
1facf9fc 32300+ int err;
32301+
2000de60 32302+ sb = file->f_path.dentry->d_sb;
dece6358
AM
32303+ SiMustAnyLock(sb);
32304+
1facf9fc 32305+ err = -ENOMEM;
32306+ vdir = au_cache_alloc_vdir();
32307+ if (unlikely(!vdir))
32308+ goto out;
32309+
32310+ vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32311+ if (unlikely(!vdir->vd_deblk))
32312+ goto out_free;
32313+
32314+ vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
1308ab2a 32315+ if (!vdir->vd_deblk_sz) {
79b8bda9 32316+ /* estimate the appropriate size for deblk */
1308ab2a 32317+ vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
4a4d8108 32318+ /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
1308ab2a 32319+ }
1facf9fc 32320+ vdir->vd_nblk = 0;
32321+ vdir->vd_version = 0;
32322+ vdir->vd_jiffy = 0;
32323+ err = append_deblk(vdir);
32324+ if (!err)
32325+ return vdir; /* success */
32326+
9f237c51 32327+ au_kfree_try_rcu(vdir->vd_deblk);
1facf9fc 32328+
4f0767ce 32329+out_free:
1c60b727 32330+ au_cache_free_vdir(vdir);
4f0767ce 32331+out:
1facf9fc 32332+ vdir = ERR_PTR(err);
32333+ return vdir;
32334+}
32335+
32336+static int reinit_vdir(struct au_vdir *vdir)
32337+{
32338+ int err;
32339+ union au_vdir_deblk_p p, deblk_end;
32340+
32341+ while (vdir->vd_nblk > 1) {
9f237c51 32342+ au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
1facf9fc 32343+ /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32344+ vdir->vd_nblk--;
32345+ }
32346+ p.deblk = vdir->vd_deblk[0];
32347+ deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32348+ err = set_deblk_end(&p, &deblk_end);
32349+ /* keep vd_dblk_sz */
32350+ vdir->vd_last.ul = 0;
32351+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32352+ vdir->vd_version = 0;
32353+ vdir->vd_jiffy = 0;
32354+ /* smp_mb(); */
32355+ return err;
32356+}
32357+
32358+/* ---------------------------------------------------------------------- */
32359+
1facf9fc 32360+#define AuFillVdir_CALLED 1
32361+#define AuFillVdir_WHABLE (1 << 1)
dece6358 32362+#define AuFillVdir_SHWH (1 << 2)
1facf9fc 32363+#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
7f207e10
AM
32364+#define au_fset_fillvdir(flags, name) \
32365+ do { (flags) |= AuFillVdir_##name; } while (0)
32366+#define au_fclr_fillvdir(flags, name) \
32367+ do { (flags) &= ~AuFillVdir_##name; } while (0)
1facf9fc 32368+
dece6358
AM
32369+#ifndef CONFIG_AUFS_SHWH
32370+#undef AuFillVdir_SHWH
32371+#define AuFillVdir_SHWH 0
32372+#endif
32373+
1facf9fc 32374+struct fillvdir_arg {
392086de 32375+ struct dir_context ctx;
1facf9fc 32376+ struct file *file;
32377+ struct au_vdir *vdir;
dece6358
AM
32378+ struct au_nhash delist;
32379+ struct au_nhash whlist;
1facf9fc 32380+ aufs_bindex_t bindex;
32381+ unsigned int flags;
32382+ int err;
32383+};
32384+
392086de 32385+static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
1facf9fc 32386+ loff_t offset __maybe_unused, u64 h_ino,
32387+ unsigned int d_type)
32388+{
392086de 32389+ struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
1facf9fc 32390+ char *name = (void *)__name;
32391+ struct super_block *sb;
1facf9fc 32392+ ino_t ino;
dece6358 32393+ const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
1facf9fc 32394+
1facf9fc 32395+ arg->err = 0;
2000de60 32396+ sb = arg->file->f_path.dentry->d_sb;
1facf9fc 32397+ au_fset_fillvdir(arg->flags, CALLED);
32398+ /* smp_mb(); */
dece6358 32399+ if (nlen <= AUFS_WH_PFX_LEN
1facf9fc 32400+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
dece6358
AM
32401+ if (test_known(&arg->delist, name, nlen)
32402+ || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32403+ goto out; /* already exists or whiteouted */
1facf9fc 32404+
dece6358 32405+ arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
4a4d8108
AM
32406+ if (!arg->err) {
32407+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
32408+ d_type = DT_UNKNOWN;
dece6358
AM
32409+ arg->err = append_de(arg->vdir, name, nlen, ino,
32410+ d_type, &arg->delist);
4a4d8108 32411+ }
1facf9fc 32412+ } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32413+ name += AUFS_WH_PFX_LEN;
dece6358
AM
32414+ nlen -= AUFS_WH_PFX_LEN;
32415+ if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32416+ goto out; /* already whiteouted */
1facf9fc 32417+
acd2b654 32418+ ino = 0; /* just to suppress a warning */
dece6358
AM
32419+ if (shwh)
32420+ arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32421+ &ino);
4a4d8108
AM
32422+ if (!arg->err) {
32423+ if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32424+ d_type = DT_UNKNOWN;
1facf9fc 32425+ arg->err = au_nhash_append_wh
dece6358
AM
32426+ (&arg->whlist, name, nlen, ino, d_type,
32427+ arg->bindex, shwh);
4a4d8108 32428+ }
1facf9fc 32429+ }
32430+
4f0767ce 32431+out:
1facf9fc 32432+ if (!arg->err)
32433+ arg->vdir->vd_jiffy = jiffies;
32434+ /* smp_mb(); */
32435+ AuTraceErr(arg->err);
32436+ return arg->err;
32437+}
32438+
dece6358
AM
32439+static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32440+ struct au_nhash *whlist, struct au_nhash *delist)
32441+{
32442+#ifdef CONFIG_AUFS_SHWH
32443+ int err;
32444+ unsigned int nh, u;
32445+ struct hlist_head *head;
c06a8ce3
AM
32446+ struct au_vdir_wh *pos;
32447+ struct hlist_node *n;
dece6358
AM
32448+ char *p, *o;
32449+ struct au_vdir_destr *destr;
32450+
32451+ AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32452+
32453+ err = -ENOMEM;
537831f9 32454+ o = p = (void *)__get_free_page(GFP_NOFS);
dece6358
AM
32455+ if (unlikely(!p))
32456+ goto out;
32457+
32458+ err = 0;
32459+ nh = whlist->nh_num;
32460+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32461+ p += AUFS_WH_PFX_LEN;
32462+ for (u = 0; u < nh; u++) {
32463+ head = whlist->nh_head + u;
c06a8ce3
AM
32464+ hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32465+ destr = &pos->wh_str;
dece6358
AM
32466+ memcpy(p, destr->name, destr->len);
32467+ err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
c06a8ce3 32468+ pos->wh_ino, pos->wh_type, delist);
dece6358
AM
32469+ if (unlikely(err))
32470+ break;
32471+ }
32472+ }
32473+
1c60b727 32474+ free_page((unsigned long)o);
dece6358 32475+
4f0767ce 32476+out:
dece6358
AM
32477+ AuTraceErr(err);
32478+ return err;
32479+#else
32480+ return 0;
32481+#endif
32482+}
32483+
1facf9fc 32484+static int au_do_read_vdir(struct fillvdir_arg *arg)
32485+{
32486+ int err;
dece6358 32487+ unsigned int rdhash;
1facf9fc 32488+ loff_t offset;
5afbbe0d 32489+ aufs_bindex_t bbot, bindex, btop;
dece6358 32490+ unsigned char shwh;
1facf9fc 32491+ struct file *hf, *file;
32492+ struct super_block *sb;
32493+
1facf9fc 32494+ file = arg->file;
2000de60 32495+ sb = file->f_path.dentry->d_sb;
dece6358
AM
32496+ SiMustAnyLock(sb);
32497+
32498+ rdhash = au_sbi(sb)->si_rdhash;
1308ab2a 32499+ if (!rdhash)
32500+ rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
dece6358
AM
32501+ err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32502+ if (unlikely(err))
1facf9fc 32503+ goto out;
dece6358
AM
32504+ err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32505+ if (unlikely(err))
1facf9fc 32506+ goto out_delist;
32507+
32508+ err = 0;
32509+ arg->flags = 0;
dece6358
AM
32510+ shwh = 0;
32511+ if (au_opt_test(au_mntflags(sb), SHWH)) {
32512+ shwh = 1;
32513+ au_fset_fillvdir(arg->flags, SHWH);
32514+ }
5afbbe0d
AM
32515+ btop = au_fbtop(file);
32516+ bbot = au_fbbot_dir(file);
32517+ for (bindex = btop; !err && bindex <= bbot; bindex++) {
4a4d8108 32518+ hf = au_hf_dir(file, bindex);
1facf9fc 32519+ if (!hf)
32520+ continue;
32521+
32522+ offset = vfsub_llseek(hf, 0, SEEK_SET);
32523+ err = offset;
32524+ if (unlikely(offset))
32525+ break;
32526+
32527+ arg->bindex = bindex;
32528+ au_fclr_fillvdir(arg->flags, WHABLE);
dece6358 32529+ if (shwh
5afbbe0d 32530+ || (bindex != bbot
dece6358 32531+ && au_br_whable(au_sbr_perm(sb, bindex))))
1facf9fc 32532+ au_fset_fillvdir(arg->flags, WHABLE);
32533+ do {
32534+ arg->err = 0;
32535+ au_fclr_fillvdir(arg->flags, CALLED);
32536+ /* smp_mb(); */
392086de 32537+ err = vfsub_iterate_dir(hf, &arg->ctx);
1facf9fc 32538+ if (err >= 0)
32539+ err = arg->err;
32540+ } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
392086de
AM
32541+
32542+ /*
32543+ * dir_relax() may be good for concurrency, but aufs should not
32544+ * use it since it will cause a lockdep problem.
32545+ */
1facf9fc 32546+ }
dece6358
AM
32547+
32548+ if (!err && shwh)
32549+ err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32550+
32551+ au_nhash_wh_free(&arg->whlist);
1facf9fc 32552+
4f0767ce 32553+out_delist:
dece6358 32554+ au_nhash_de_free(&arg->delist);
4f0767ce 32555+out:
1facf9fc 32556+ return err;
32557+}
32558+
32559+static int read_vdir(struct file *file, int may_read)
32560+{
32561+ int err;
32562+ unsigned long expire;
32563+ unsigned char do_read;
392086de
AM
32564+ struct fillvdir_arg arg = {
32565+ .ctx = {
2000de60 32566+ .actor = fillvdir
392086de
AM
32567+ }
32568+ };
1facf9fc 32569+ struct inode *inode;
32570+ struct au_vdir *vdir, *allocated;
32571+
32572+ err = 0;
c06a8ce3 32573+ inode = file_inode(file);
1facf9fc 32574+ IMustLock(inode);
5afbbe0d 32575+ IiMustWriteLock(inode);
dece6358
AM
32576+ SiMustAnyLock(inode->i_sb);
32577+
1facf9fc 32578+ allocated = NULL;
32579+ do_read = 0;
32580+ expire = au_sbi(inode->i_sb)->si_rdcache;
32581+ vdir = au_ivdir(inode);
32582+ if (!vdir) {
32583+ do_read = 1;
1308ab2a 32584+ vdir = alloc_vdir(file);
1facf9fc 32585+ err = PTR_ERR(vdir);
32586+ if (IS_ERR(vdir))
32587+ goto out;
32588+ err = 0;
32589+ allocated = vdir;
32590+ } else if (may_read
be118d29 32591+ && (!inode_eq_iversion(inode, vdir->vd_version)
1facf9fc 32592+ || time_after(jiffies, vdir->vd_jiffy + expire))) {
32593+ do_read = 1;
32594+ err = reinit_vdir(vdir);
32595+ if (unlikely(err))
32596+ goto out;
32597+ }
32598+
32599+ if (!do_read)
32600+ return 0; /* success */
32601+
32602+ arg.file = file;
32603+ arg.vdir = vdir;
32604+ err = au_do_read_vdir(&arg);
32605+ if (!err) {
392086de 32606+ /* file->f_pos = 0; */ /* todo: ctx->pos? */
be118d29 32607+ vdir->vd_version = inode_query_iversion(inode);
1facf9fc 32608+ vdir->vd_last.ul = 0;
32609+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32610+ if (allocated)
32611+ au_set_ivdir(inode, allocated);
32612+ } else if (allocated)
1c60b727 32613+ au_vdir_free(allocated);
1facf9fc 32614+
4f0767ce 32615+out:
1facf9fc 32616+ return err;
32617+}
32618+
32619+static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32620+{
32621+ int err, rerr;
32622+ unsigned long ul, n;
32623+ const unsigned int deblk_sz = src->vd_deblk_sz;
32624+
32625+ AuDebugOn(tgt->vd_nblk != 1);
32626+
32627+ err = -ENOMEM;
32628+ if (tgt->vd_nblk < src->vd_nblk) {
32629+ unsigned char **p;
32630+
e2f27e51
AM
32631+ p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32632+ GFP_NOFS, /*may_shrink*/0);
1facf9fc 32633+ if (unlikely(!p))
32634+ goto out;
32635+ tgt->vd_deblk = p;
32636+ }
32637+
1308ab2a 32638+ if (tgt->vd_deblk_sz != deblk_sz) {
32639+ unsigned char *p;
32640+
32641+ tgt->vd_deblk_sz = deblk_sz;
e2f27e51
AM
32642+ p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32643+ /*may_shrink*/1);
1308ab2a 32644+ if (unlikely(!p))
32645+ goto out;
32646+ tgt->vd_deblk[0] = p;
32647+ }
1facf9fc 32648+ memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
1facf9fc 32649+ tgt->vd_version = src->vd_version;
32650+ tgt->vd_jiffy = src->vd_jiffy;
32651+
32652+ n = src->vd_nblk;
32653+ for (ul = 1; ul < n; ul++) {
dece6358
AM
32654+ tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32655+ GFP_NOFS);
32656+ if (unlikely(!tgt->vd_deblk[ul]))
1facf9fc 32657+ goto out;
1308ab2a 32658+ tgt->vd_nblk++;
1facf9fc 32659+ }
1308ab2a 32660+ tgt->vd_nblk = n;
32661+ tgt->vd_last.ul = tgt->vd_last.ul;
32662+ tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32663+ tgt->vd_last.p.deblk += src->vd_last.p.deblk
32664+ - src->vd_deblk[src->vd_last.ul];
1facf9fc 32665+ /* smp_mb(); */
32666+ return 0; /* success */
32667+
4f0767ce 32668+out:
1facf9fc 32669+ rerr = reinit_vdir(tgt);
32670+ BUG_ON(rerr);
32671+ return err;
32672+}
32673+
32674+int au_vdir_init(struct file *file)
32675+{
32676+ int err;
32677+ struct inode *inode;
32678+ struct au_vdir *vdir_cache, *allocated;
32679+
392086de 32680+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 32681+ err = read_vdir(file, !file->f_pos);
32682+ if (unlikely(err))
32683+ goto out;
32684+
32685+ allocated = NULL;
32686+ vdir_cache = au_fvdir_cache(file);
32687+ if (!vdir_cache) {
1308ab2a 32688+ vdir_cache = alloc_vdir(file);
1facf9fc 32689+ err = PTR_ERR(vdir_cache);
32690+ if (IS_ERR(vdir_cache))
32691+ goto out;
32692+ allocated = vdir_cache;
32693+ } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
392086de 32694+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 32695+ err = reinit_vdir(vdir_cache);
32696+ if (unlikely(err))
32697+ goto out;
32698+ } else
32699+ return 0; /* success */
32700+
c06a8ce3 32701+ inode = file_inode(file);
1facf9fc 32702+ err = copy_vdir(vdir_cache, au_ivdir(inode));
32703+ if (!err) {
be118d29 32704+ file->f_version = inode_query_iversion(inode);
1facf9fc 32705+ if (allocated)
32706+ au_set_fvdir_cache(file, allocated);
32707+ } else if (allocated)
1c60b727 32708+ au_vdir_free(allocated);
1facf9fc 32709+
4f0767ce 32710+out:
1facf9fc 32711+ return err;
32712+}
32713+
32714+static loff_t calc_offset(struct au_vdir *vdir)
32715+{
32716+ loff_t offset;
32717+ union au_vdir_deblk_p p;
32718+
32719+ p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32720+ offset = vdir->vd_last.p.deblk - p.deblk;
32721+ offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32722+ return offset;
32723+}
32724+
32725+/* returns true or false */
392086de 32726+static int seek_vdir(struct file *file, struct dir_context *ctx)
1facf9fc 32727+{
32728+ int valid;
32729+ unsigned int deblk_sz;
32730+ unsigned long ul, n;
32731+ loff_t offset;
32732+ union au_vdir_deblk_p p, deblk_end;
32733+ struct au_vdir *vdir_cache;
32734+
32735+ valid = 1;
32736+ vdir_cache = au_fvdir_cache(file);
32737+ offset = calc_offset(vdir_cache);
32738+ AuDbg("offset %lld\n", offset);
392086de 32739+ if (ctx->pos == offset)
1facf9fc 32740+ goto out;
32741+
32742+ vdir_cache->vd_last.ul = 0;
32743+ vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
392086de 32744+ if (!ctx->pos)
1facf9fc 32745+ goto out;
32746+
32747+ valid = 0;
32748+ deblk_sz = vdir_cache->vd_deblk_sz;
392086de 32749+ ul = div64_u64(ctx->pos, deblk_sz);
1facf9fc 32750+ AuDbg("ul %lu\n", ul);
32751+ if (ul >= vdir_cache->vd_nblk)
32752+ goto out;
32753+
32754+ n = vdir_cache->vd_nblk;
32755+ for (; ul < n; ul++) {
32756+ p.deblk = vdir_cache->vd_deblk[ul];
32757+ deblk_end.deblk = p.deblk + deblk_sz;
32758+ offset = ul;
32759+ offset *= deblk_sz;
392086de 32760+ while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
1facf9fc 32761+ unsigned int l;
32762+
32763+ l = calc_size(p.de->de_str.len);
32764+ offset += l;
32765+ p.deblk += l;
32766+ }
32767+ if (!is_deblk_end(&p, &deblk_end)) {
32768+ valid = 1;
32769+ vdir_cache->vd_last.ul = ul;
32770+ vdir_cache->vd_last.p = p;
32771+ break;
32772+ }
32773+ }
32774+
4f0767ce 32775+out:
1facf9fc 32776+ /* smp_mb(); */
b00004a5
AM
32777+ if (!valid)
32778+ AuDbg("valid %d\n", !valid);
1facf9fc 32779+ return valid;
32780+}
32781+
392086de 32782+int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
1facf9fc 32783+{
1facf9fc 32784+ unsigned int l, deblk_sz;
32785+ union au_vdir_deblk_p deblk_end;
32786+ struct au_vdir *vdir_cache;
32787+ struct au_vdir_de *de;
32788+
392086de 32789+ if (!seek_vdir(file, ctx))
1facf9fc 32790+ return 0;
32791+
acd2b654 32792+ vdir_cache = au_fvdir_cache(file);
1facf9fc 32793+ deblk_sz = vdir_cache->vd_deblk_sz;
32794+ while (1) {
32795+ deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32796+ deblk_end.deblk += deblk_sz;
32797+ while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32798+ de = vdir_cache->vd_last.p.de;
32799+ AuDbg("%.*s, off%lld, i%lu, dt%d\n",
392086de 32800+ de->de_str.len, de->de_str.name, ctx->pos,
1facf9fc 32801+ (unsigned long)de->de_ino, de->de_type);
392086de
AM
32802+ if (unlikely(!dir_emit(ctx, de->de_str.name,
32803+ de->de_str.len, de->de_ino,
32804+ de->de_type))) {
1facf9fc 32805+ /* todo: ignore the error caused by udba? */
32806+ /* return err; */
32807+ return 0;
32808+ }
32809+
32810+ l = calc_size(de->de_str.len);
32811+ vdir_cache->vd_last.p.deblk += l;
392086de 32812+ ctx->pos += l;
1facf9fc 32813+ }
32814+ if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32815+ vdir_cache->vd_last.ul++;
32816+ vdir_cache->vd_last.p.deblk
32817+ = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
392086de 32818+ ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
1facf9fc 32819+ continue;
32820+ }
32821+ break;
32822+ }
32823+
32824+ /* smp_mb(); */
32825+ return 0;
32826+}
7f207e10
AM
32827diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32828--- /usr/share/empty/fs/aufs/vfsub.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 32829+++ linux/fs/aufs/vfsub.c 2019-03-05 12:13:00.145891204 +0100
acd2b654 32830@@ -0,0 +1,902 @@
cd7a4cd9 32831+// SPDX-License-Identifier: GPL-2.0
1facf9fc 32832+/*
ba1aed25 32833+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 32834+ *
32835+ * This program, aufs is free software; you can redistribute it and/or modify
32836+ * it under the terms of the GNU General Public License as published by
32837+ * the Free Software Foundation; either version 2 of the License, or
32838+ * (at your option) any later version.
dece6358
AM
32839+ *
32840+ * This program is distributed in the hope that it will be useful,
32841+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
32842+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32843+ * GNU General Public License for more details.
32844+ *
32845+ * You should have received a copy of the GNU General Public License
523b37e3 32846+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 32847+ */
32848+
32849+/*
32850+ * sub-routines for VFS
32851+ */
32852+
8b6a4947 32853+#include <linux/mnt_namespace.h>
dece6358 32854+#include <linux/namei.h>
8cdd5066 32855+#include <linux/nsproxy.h>
dece6358
AM
32856+#include <linux/security.h>
32857+#include <linux/splice.h>
1facf9fc 32858+#include "aufs.h"
32859+
8cdd5066
JR
32860+#ifdef CONFIG_AUFS_BR_FUSE
32861+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32862+{
8cdd5066
JR
32863+ if (!au_test_fuse(h_sb) || !au_userns)
32864+ return 0;
32865+
8b6a4947 32866+ return is_current_mnt_ns(mnt) ? 0 : -EACCES;
8cdd5066
JR
32867+}
32868+#endif
32869+
a2654f78
AM
32870+int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
32871+{
32872+ int err;
32873+
32874+ lockdep_off();
32875+ down_read(&h_sb->s_umount);
32876+ err = __sync_filesystem(h_sb, wait);
32877+ up_read(&h_sb->s_umount);
32878+ lockdep_on();
32879+
32880+ return err;
32881+}
32882+
8cdd5066
JR
32883+/* ---------------------------------------------------------------------- */
32884+
1facf9fc 32885+int vfsub_update_h_iattr(struct path *h_path, int *did)
32886+{
32887+ int err;
32888+ struct kstat st;
32889+ struct super_block *h_sb;
32890+
32891+ /* for remote fs, leave work for its getattr or d_revalidate */
32892+ /* for bad i_attr fs, handle them in aufs_getattr() */
32893+ /* still some fs may acquire i_mutex. we need to skip them */
32894+ err = 0;
32895+ if (!did)
32896+ did = &err;
32897+ h_sb = h_path->dentry->d_sb;
32898+ *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32899+ if (*did)
521ced18 32900+ err = vfsub_getattr(h_path, &st);
1facf9fc 32901+
32902+ return err;
32903+}
32904+
32905+/* ---------------------------------------------------------------------- */
32906+
4a4d8108 32907+struct file *vfsub_dentry_open(struct path *path, int flags)
1308ab2a 32908+{
32909+ struct file *file;
32910+
b4510431 32911+ file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
7f207e10 32912+ current_cred());
2cbb1c4b
JR
32913+ if (!IS_ERR_OR_NULL(file)
32914+ && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
5527c038 32915+ i_readcount_inc(d_inode(path->dentry));
4a4d8108 32916+
1308ab2a 32917+ return file;
32918+}
32919+
1facf9fc 32920+struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32921+{
32922+ struct file *file;
32923+
2cbb1c4b 32924+ lockdep_off();
7f207e10 32925+ file = filp_open(path,
2cbb1c4b 32926+ oflags /* | __FMODE_NONOTIFY */,
7f207e10 32927+ mode);
2cbb1c4b 32928+ lockdep_on();
1facf9fc 32929+ if (IS_ERR(file))
32930+ goto out;
32931+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32932+
4f0767ce 32933+out:
1facf9fc 32934+ return file;
32935+}
32936+
b912730e
AM
32937+/*
32938+ * Ideally this function should call VFS:do_last() in order to keep all its
32939+ * checkings. But it is very hard for aufs to regenerate several VFS internal
32940+ * structure such as nameidata. This is a second (or third) best approach.
32941+ * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32942+ */
32943+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
acd2b654 32944+ struct vfsub_aopen_args *args)
b912730e
AM
32945+{
32946+ int err;
acd2b654 32947+ struct au_branch *br = args->br;
b912730e
AM
32948+ struct file *file = args->file;
32949+ /* copied from linux/fs/namei.c:atomic_open() */
32950+ struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
32951+
32952+ IMustLock(dir);
32953+ AuDebugOn(!dir->i_op->atomic_open);
32954+
32955+ err = au_br_test_oflag(args->open_flag, br);
32956+ if (unlikely(err))
32957+ goto out;
32958+
acd2b654
AM
32959+ au_lcnt_inc(&br->br_nfiles);
32960+ file->f_path.dentry = DENTRY_NOT_SET;
32961+ file->f_path.mnt = au_br_mnt(br);
32962+ AuDbg("%ps\n", dir->i_op->atomic_open);
b912730e 32963+ err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
acd2b654
AM
32964+ args->create_mode);
32965+ if (unlikely(err < 0)) {
32966+ au_lcnt_dec(&br->br_nfiles);
b912730e 32967+ goto out;
acd2b654 32968+ }
b912730e 32969+
acd2b654
AM
32970+ /* temporary workaround for nfsv4 branch */
32971+ if (au_test_nfs(dir->i_sb))
32972+ nfs_mark_for_revalidate(dir);
b912730e 32973+
acd2b654
AM
32974+ if (file->f_mode & FMODE_CREATED)
32975+ fsnotify_create(dir, dentry);
32976+ if (!(file->f_mode & FMODE_OPENED)) {
32977+ au_lcnt_dec(&br->br_nfiles);
32978+ goto out;
b912730e
AM
32979+ }
32980+
acd2b654
AM
32981+ /* todo: call VFS:may_open() here */
32982+ /* todo: ima_file_check() too? */
32983+ if (!err && (args->open_flag & __FMODE_EXEC))
32984+ err = deny_write_access(file);
32985+ if (!err)
32986+ fsnotify_open(file);
32987+ else
32988+ au_lcnt_dec(&br->br_nfiles);
32989+ /* note that the file is created and still opened */
b912730e
AM
32990+
32991+out:
32992+ return err;
32993+}
32994+
1facf9fc 32995+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
32996+{
32997+ int err;
32998+
1facf9fc 32999+ err = kern_path(name, flags, path);
5527c038 33000+ if (!err && d_is_positive(path->dentry))
1facf9fc 33001+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33002+ return err;
33003+}
33004+
febd17d6
JR
33005+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33006+ struct dentry *parent, int len)
33007+{
33008+ struct path path = {
33009+ .mnt = NULL
33010+ };
33011+
33012+ path.dentry = lookup_one_len_unlocked(name, parent, len);
33013+ if (IS_ERR(path.dentry))
33014+ goto out;
33015+ if (d_is_positive(path.dentry))
33016+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33017+
33018+out:
33019+ AuTraceErrPtr(path.dentry);
33020+ return path.dentry;
33021+}
33022+
1facf9fc 33023+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
33024+ int len)
33025+{
33026+ struct path path = {
33027+ .mnt = NULL
33028+ };
33029+
1308ab2a 33030+ /* VFS checks it too, but by WARN_ON_ONCE() */
5527c038 33031+ IMustLock(d_inode(parent));
1facf9fc 33032+
33033+ path.dentry = lookup_one_len(name, parent, len);
33034+ if (IS_ERR(path.dentry))
33035+ goto out;
5527c038 33036+ if (d_is_positive(path.dentry))
1facf9fc 33037+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33038+
4f0767ce 33039+out:
4a4d8108 33040+ AuTraceErrPtr(path.dentry);
1facf9fc 33041+ return path.dentry;
33042+}
33043+
b4510431 33044+void vfsub_call_lkup_one(void *args)
2cbb1c4b 33045+{
b4510431
AM
33046+ struct vfsub_lkup_one_args *a = args;
33047+ *a->errp = vfsub_lkup_one(a->name, a->parent);
2cbb1c4b
JR
33048+}
33049+
1facf9fc 33050+/* ---------------------------------------------------------------------- */
33051+
33052+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33053+ struct dentry *d2, struct au_hinode *hdir2)
33054+{
33055+ struct dentry *d;
33056+
2cbb1c4b 33057+ lockdep_off();
1facf9fc 33058+ d = lock_rename(d1, d2);
2cbb1c4b 33059+ lockdep_on();
4a4d8108 33060+ au_hn_suspend(hdir1);
1facf9fc 33061+ if (hdir1 != hdir2)
4a4d8108 33062+ au_hn_suspend(hdir2);
1facf9fc 33063+
33064+ return d;
33065+}
33066+
33067+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33068+ struct dentry *d2, struct au_hinode *hdir2)
33069+{
4a4d8108 33070+ au_hn_resume(hdir1);
1facf9fc 33071+ if (hdir1 != hdir2)
4a4d8108 33072+ au_hn_resume(hdir2);
2cbb1c4b 33073+ lockdep_off();
1facf9fc 33074+ unlock_rename(d1, d2);
2cbb1c4b 33075+ lockdep_on();
1facf9fc 33076+}
33077+
33078+/* ---------------------------------------------------------------------- */
33079+
b4510431 33080+int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
1facf9fc 33081+{
33082+ int err;
33083+ struct dentry *d;
33084+
33085+ IMustLock(dir);
33086+
33087+ d = path->dentry;
33088+ path->dentry = d->d_parent;
b752ccd1 33089+ err = security_path_mknod(path, d, mode, 0);
1facf9fc 33090+ path->dentry = d;
33091+ if (unlikely(err))
33092+ goto out;
33093+
c1595e42 33094+ lockdep_off();
b4510431 33095+ err = vfs_create(dir, path->dentry, mode, want_excl);
c1595e42 33096+ lockdep_on();
1facf9fc 33097+ if (!err) {
33098+ struct path tmp = *path;
33099+ int did;
33100+
33101+ vfsub_update_h_iattr(&tmp, &did);
33102+ if (did) {
33103+ tmp.dentry = path->dentry->d_parent;
33104+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33105+ }
33106+ /*ignore*/
33107+ }
33108+
4f0767ce 33109+out:
1facf9fc 33110+ return err;
33111+}
33112+
33113+int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
33114+{
33115+ int err;
33116+ struct dentry *d;
33117+
33118+ IMustLock(dir);
33119+
33120+ d = path->dentry;
33121+ path->dentry = d->d_parent;
b752ccd1 33122+ err = security_path_symlink(path, d, symname);
1facf9fc 33123+ path->dentry = d;
33124+ if (unlikely(err))
33125+ goto out;
33126+
c1595e42 33127+ lockdep_off();
1facf9fc 33128+ err = vfs_symlink(dir, path->dentry, symname);
c1595e42 33129+ lockdep_on();
1facf9fc 33130+ if (!err) {
33131+ struct path tmp = *path;
33132+ int did;
33133+
33134+ vfsub_update_h_iattr(&tmp, &did);
33135+ if (did) {
33136+ tmp.dentry = path->dentry->d_parent;
33137+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33138+ }
33139+ /*ignore*/
33140+ }
33141+
4f0767ce 33142+out:
1facf9fc 33143+ return err;
33144+}
33145+
33146+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
33147+{
33148+ int err;
33149+ struct dentry *d;
33150+
33151+ IMustLock(dir);
33152+
33153+ d = path->dentry;
33154+ path->dentry = d->d_parent;
027c5e7a 33155+ err = security_path_mknod(path, d, mode, new_encode_dev(dev));
1facf9fc 33156+ path->dentry = d;
33157+ if (unlikely(err))
33158+ goto out;
33159+
c1595e42 33160+ lockdep_off();
1facf9fc 33161+ err = vfs_mknod(dir, path->dentry, mode, dev);
c1595e42 33162+ lockdep_on();
1facf9fc 33163+ if (!err) {
33164+ struct path tmp = *path;
33165+ int did;
33166+
33167+ vfsub_update_h_iattr(&tmp, &did);
33168+ if (did) {
33169+ tmp.dentry = path->dentry->d_parent;
33170+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33171+ }
33172+ /*ignore*/
33173+ }
33174+
4f0767ce 33175+out:
1facf9fc 33176+ return err;
33177+}
33178+
33179+static int au_test_nlink(struct inode *inode)
33180+{
33181+ const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33182+
33183+ if (!au_test_fs_no_limit_nlink(inode->i_sb)
33184+ || inode->i_nlink < link_max)
33185+ return 0;
33186+ return -EMLINK;
33187+}
33188+
523b37e3
AM
33189+int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33190+ struct inode **delegated_inode)
1facf9fc 33191+{
33192+ int err;
33193+ struct dentry *d;
33194+
33195+ IMustLock(dir);
33196+
5527c038 33197+ err = au_test_nlink(d_inode(src_dentry));
1facf9fc 33198+ if (unlikely(err))
33199+ return err;
33200+
b4510431 33201+ /* we don't call may_linkat() */
1facf9fc 33202+ d = path->dentry;
33203+ path->dentry = d->d_parent;
b752ccd1 33204+ err = security_path_link(src_dentry, path, d);
1facf9fc 33205+ path->dentry = d;
33206+ if (unlikely(err))
33207+ goto out;
33208+
2cbb1c4b 33209+ lockdep_off();
523b37e3 33210+ err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
2cbb1c4b 33211+ lockdep_on();
1facf9fc 33212+ if (!err) {
33213+ struct path tmp = *path;
33214+ int did;
33215+
33216+ /* fuse has different memory inode for the same inumber */
33217+ vfsub_update_h_iattr(&tmp, &did);
33218+ if (did) {
33219+ tmp.dentry = path->dentry->d_parent;
33220+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33221+ tmp.dentry = src_dentry;
33222+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33223+ }
33224+ /*ignore*/
33225+ }
33226+
4f0767ce 33227+out:
1facf9fc 33228+ return err;
33229+}
33230+
33231+int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
523b37e3 33232+ struct inode *dir, struct path *path,
f2c43d5f 33233+ struct inode **delegated_inode, unsigned int flags)
1facf9fc 33234+{
33235+ int err;
33236+ struct path tmp = {
33237+ .mnt = path->mnt
33238+ };
33239+ struct dentry *d;
33240+
33241+ IMustLock(dir);
33242+ IMustLock(src_dir);
33243+
33244+ d = path->dentry;
33245+ path->dentry = d->d_parent;
33246+ tmp.dentry = src_dentry->d_parent;
38d290e6 33247+ err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
1facf9fc 33248+ path->dentry = d;
33249+ if (unlikely(err))
33250+ goto out;
33251+
2cbb1c4b 33252+ lockdep_off();
523b37e3 33253+ err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
f2c43d5f 33254+ delegated_inode, flags);
2cbb1c4b 33255+ lockdep_on();
1facf9fc 33256+ if (!err) {
33257+ int did;
33258+
33259+ tmp.dentry = d->d_parent;
33260+ vfsub_update_h_iattr(&tmp, &did);
33261+ if (did) {
33262+ tmp.dentry = src_dentry;
33263+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33264+ tmp.dentry = src_dentry->d_parent;
33265+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33266+ }
33267+ /*ignore*/
33268+ }
33269+
4f0767ce 33270+out:
1facf9fc 33271+ return err;
33272+}
33273+
33274+int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33275+{
33276+ int err;
33277+ struct dentry *d;
33278+
33279+ IMustLock(dir);
33280+
33281+ d = path->dentry;
33282+ path->dentry = d->d_parent;
b752ccd1 33283+ err = security_path_mkdir(path, d, mode);
1facf9fc 33284+ path->dentry = d;
33285+ if (unlikely(err))
33286+ goto out;
33287+
c1595e42 33288+ lockdep_off();
1facf9fc 33289+ err = vfs_mkdir(dir, path->dentry, mode);
c1595e42 33290+ lockdep_on();
1facf9fc 33291+ if (!err) {
33292+ struct path tmp = *path;
33293+ int did;
33294+
33295+ vfsub_update_h_iattr(&tmp, &did);
33296+ if (did) {
33297+ tmp.dentry = path->dentry->d_parent;
33298+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33299+ }
33300+ /*ignore*/
33301+ }
33302+
4f0767ce 33303+out:
1facf9fc 33304+ return err;
33305+}
33306+
33307+int vfsub_rmdir(struct inode *dir, struct path *path)
33308+{
33309+ int err;
33310+ struct dentry *d;
33311+
33312+ IMustLock(dir);
33313+
33314+ d = path->dentry;
33315+ path->dentry = d->d_parent;
b752ccd1 33316+ err = security_path_rmdir(path, d);
1facf9fc 33317+ path->dentry = d;
33318+ if (unlikely(err))
33319+ goto out;
33320+
2cbb1c4b 33321+ lockdep_off();
1facf9fc 33322+ err = vfs_rmdir(dir, path->dentry);
2cbb1c4b 33323+ lockdep_on();
1facf9fc 33324+ if (!err) {
33325+ struct path tmp = {
33326+ .dentry = path->dentry->d_parent,
33327+ .mnt = path->mnt
33328+ };
33329+
33330+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33331+ }
33332+
4f0767ce 33333+out:
1facf9fc 33334+ return err;
33335+}
33336+
33337+/* ---------------------------------------------------------------------- */
33338+
9dbd164d 33339+/* todo: support mmap_sem? */
1facf9fc 33340+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33341+ loff_t *ppos)
33342+{
33343+ ssize_t err;
33344+
2cbb1c4b 33345+ lockdep_off();
1facf9fc 33346+ err = vfs_read(file, ubuf, count, ppos);
2cbb1c4b 33347+ lockdep_on();
1facf9fc 33348+ if (err >= 0)
33349+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33350+ return err;
33351+}
33352+
33353+/* todo: kernel_read()? */
33354+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33355+ loff_t *ppos)
33356+{
33357+ ssize_t err;
33358+ mm_segment_t oldfs;
b752ccd1
AM
33359+ union {
33360+ void *k;
33361+ char __user *u;
33362+ } buf;
1facf9fc 33363+
b752ccd1 33364+ buf.k = kbuf;
1facf9fc 33365+ oldfs = get_fs();
33366+ set_fs(KERNEL_DS);
b752ccd1 33367+ err = vfsub_read_u(file, buf.u, count, ppos);
1facf9fc 33368+ set_fs(oldfs);
33369+ return err;
33370+}
33371+
33372+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33373+ loff_t *ppos)
33374+{
33375+ ssize_t err;
33376+
2cbb1c4b 33377+ lockdep_off();
1facf9fc 33378+ err = vfs_write(file, ubuf, count, ppos);
2cbb1c4b 33379+ lockdep_on();
1facf9fc 33380+ if (err >= 0)
33381+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33382+ return err;
33383+}
33384+
33385+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33386+{
33387+ ssize_t err;
33388+ mm_segment_t oldfs;
b752ccd1
AM
33389+ union {
33390+ void *k;
33391+ const char __user *u;
33392+ } buf;
1facf9fc 33393+
b752ccd1 33394+ buf.k = kbuf;
1facf9fc 33395+ oldfs = get_fs();
33396+ set_fs(KERNEL_DS);
b752ccd1 33397+ err = vfsub_write_u(file, buf.u, count, ppos);
1facf9fc 33398+ set_fs(oldfs);
33399+ return err;
33400+}
33401+
4a4d8108
AM
33402+int vfsub_flush(struct file *file, fl_owner_t id)
33403+{
33404+ int err;
33405+
33406+ err = 0;
523b37e3 33407+ if (file->f_op->flush) {
2000de60 33408+ if (!au_test_nfs(file->f_path.dentry->d_sb))
2cbb1c4b
JR
33409+ err = file->f_op->flush(file, id);
33410+ else {
33411+ lockdep_off();
33412+ err = file->f_op->flush(file, id);
33413+ lockdep_on();
33414+ }
4a4d8108
AM
33415+ if (!err)
33416+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33417+ /*ignore*/
33418+ }
33419+ return err;
33420+}
33421+
392086de 33422+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
1facf9fc 33423+{
33424+ int err;
33425+
062440b3 33426+ AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
392086de 33427+
2cbb1c4b 33428+ lockdep_off();
392086de 33429+ err = iterate_dir(file, ctx);
2cbb1c4b 33430+ lockdep_on();
1facf9fc 33431+ if (err >= 0)
33432+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
1c60b727 33433+
1facf9fc 33434+ return err;
33435+}
33436+
33437+long vfsub_splice_to(struct file *in, loff_t *ppos,
33438+ struct pipe_inode_info *pipe, size_t len,
33439+ unsigned int flags)
33440+{
33441+ long err;
33442+
2cbb1c4b 33443+ lockdep_off();
0fc653ad 33444+ err = do_splice_to(in, ppos, pipe, len, flags);
2cbb1c4b 33445+ lockdep_on();
4a4d8108 33446+ file_accessed(in);
1facf9fc 33447+ if (err >= 0)
33448+ vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33449+ return err;
33450+}
33451+
33452+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33453+ loff_t *ppos, size_t len, unsigned int flags)
33454+{
33455+ long err;
33456+
2cbb1c4b 33457+ lockdep_off();
0fc653ad 33458+ err = do_splice_from(pipe, out, ppos, len, flags);
2cbb1c4b 33459+ lockdep_on();
1facf9fc 33460+ if (err >= 0)
33461+ vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33462+ return err;
33463+}
33464+
53392da6
AM
33465+int vfsub_fsync(struct file *file, struct path *path, int datasync)
33466+{
33467+ int err;
33468+
33469+ /* file can be NULL */
33470+ lockdep_off();
33471+ err = vfs_fsync(file, datasync);
33472+ lockdep_on();
33473+ if (!err) {
33474+ if (!path) {
33475+ AuDebugOn(!file);
33476+ path = &file->f_path;
33477+ }
33478+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33479+ }
33480+ return err;
33481+}
33482+
1facf9fc 33483+/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33484+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33485+ struct file *h_file)
33486+{
33487+ int err;
33488+ struct inode *h_inode;
c06a8ce3 33489+ struct super_block *h_sb;
1facf9fc 33490+
1facf9fc 33491+ if (!h_file) {
c06a8ce3
AM
33492+ err = vfsub_truncate(h_path, length);
33493+ goto out;
1facf9fc 33494+ }
33495+
5527c038 33496+ h_inode = d_inode(h_path->dentry);
c06a8ce3
AM
33497+ h_sb = h_inode->i_sb;
33498+ lockdep_off();
33499+ sb_start_write(h_sb);
33500+ lockdep_on();
1facf9fc 33501+ err = locks_verify_truncate(h_inode, h_file, length);
33502+ if (!err)
953406b4 33503+ err = security_path_truncate(h_path);
2cbb1c4b
JR
33504+ if (!err) {
33505+ lockdep_off();
1facf9fc 33506+ err = do_truncate(h_path->dentry, length, attr, h_file);
2cbb1c4b
JR
33507+ lockdep_on();
33508+ }
c06a8ce3
AM
33509+ lockdep_off();
33510+ sb_end_write(h_sb);
33511+ lockdep_on();
1facf9fc 33512+
4f0767ce 33513+out:
1facf9fc 33514+ return err;
33515+}
33516+
33517+/* ---------------------------------------------------------------------- */
33518+
33519+struct au_vfsub_mkdir_args {
33520+ int *errp;
33521+ struct inode *dir;
33522+ struct path *path;
33523+ int mode;
33524+};
33525+
33526+static void au_call_vfsub_mkdir(void *args)
33527+{
33528+ struct au_vfsub_mkdir_args *a = args;
33529+ *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33530+}
33531+
33532+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33533+{
33534+ int err, do_sio, wkq_err;
33535+
33536+ do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
33537+ if (!do_sio) {
33538+ lockdep_off();
1facf9fc 33539+ err = vfsub_mkdir(dir, path, mode);
c1595e42
JR
33540+ lockdep_on();
33541+ } else {
1facf9fc 33542+ struct au_vfsub_mkdir_args args = {
33543+ .errp = &err,
33544+ .dir = dir,
33545+ .path = path,
33546+ .mode = mode
33547+ };
33548+ wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33549+ if (unlikely(wkq_err))
33550+ err = wkq_err;
33551+ }
33552+
33553+ return err;
33554+}
33555+
33556+struct au_vfsub_rmdir_args {
33557+ int *errp;
33558+ struct inode *dir;
33559+ struct path *path;
33560+};
33561+
33562+static void au_call_vfsub_rmdir(void *args)
33563+{
33564+ struct au_vfsub_rmdir_args *a = args;
33565+ *a->errp = vfsub_rmdir(a->dir, a->path);
33566+}
33567+
33568+int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33569+{
33570+ int err, do_sio, wkq_err;
33571+
33572+ do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
33573+ if (!do_sio) {
33574+ lockdep_off();
1facf9fc 33575+ err = vfsub_rmdir(dir, path);
c1595e42
JR
33576+ lockdep_on();
33577+ } else {
1facf9fc 33578+ struct au_vfsub_rmdir_args args = {
33579+ .errp = &err,
33580+ .dir = dir,
33581+ .path = path
33582+ };
33583+ wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33584+ if (unlikely(wkq_err))
33585+ err = wkq_err;
33586+ }
33587+
33588+ return err;
33589+}
33590+
33591+/* ---------------------------------------------------------------------- */
33592+
33593+struct notify_change_args {
33594+ int *errp;
33595+ struct path *path;
33596+ struct iattr *ia;
523b37e3 33597+ struct inode **delegated_inode;
1facf9fc 33598+};
33599+
33600+static void call_notify_change(void *args)
33601+{
33602+ struct notify_change_args *a = args;
33603+ struct inode *h_inode;
33604+
5527c038 33605+ h_inode = d_inode(a->path->dentry);
1facf9fc 33606+ IMustLock(h_inode);
33607+
33608+ *a->errp = -EPERM;
33609+ if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
c1595e42 33610+ lockdep_off();
523b37e3
AM
33611+ *a->errp = notify_change(a->path->dentry, a->ia,
33612+ a->delegated_inode);
c1595e42 33613+ lockdep_on();
1facf9fc 33614+ if (!*a->errp)
33615+ vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33616+ }
33617+ AuTraceErr(*a->errp);
33618+}
33619+
523b37e3
AM
33620+int vfsub_notify_change(struct path *path, struct iattr *ia,
33621+ struct inode **delegated_inode)
1facf9fc 33622+{
33623+ int err;
33624+ struct notify_change_args args = {
523b37e3
AM
33625+ .errp = &err,
33626+ .path = path,
33627+ .ia = ia,
33628+ .delegated_inode = delegated_inode
1facf9fc 33629+ };
33630+
33631+ call_notify_change(&args);
33632+
33633+ return err;
33634+}
33635+
523b37e3
AM
33636+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33637+ struct inode **delegated_inode)
1facf9fc 33638+{
33639+ int err, wkq_err;
33640+ struct notify_change_args args = {
523b37e3
AM
33641+ .errp = &err,
33642+ .path = path,
33643+ .ia = ia,
33644+ .delegated_inode = delegated_inode
1facf9fc 33645+ };
33646+
33647+ wkq_err = au_wkq_wait(call_notify_change, &args);
33648+ if (unlikely(wkq_err))
33649+ err = wkq_err;
33650+
33651+ return err;
33652+}
33653+
33654+/* ---------------------------------------------------------------------- */
33655+
33656+struct unlink_args {
33657+ int *errp;
33658+ struct inode *dir;
33659+ struct path *path;
523b37e3 33660+ struct inode **delegated_inode;
1facf9fc 33661+};
33662+
33663+static void call_unlink(void *args)
33664+{
33665+ struct unlink_args *a = args;
33666+ struct dentry *d = a->path->dentry;
33667+ struct inode *h_inode;
33668+ const int stop_sillyrename = (au_test_nfs(d->d_sb)
c1595e42 33669+ && au_dcount(d) == 1);
1facf9fc 33670+
33671+ IMustLock(a->dir);
33672+
33673+ a->path->dentry = d->d_parent;
33674+ *a->errp = security_path_unlink(a->path, d);
33675+ a->path->dentry = d;
33676+ if (unlikely(*a->errp))
33677+ return;
33678+
33679+ if (!stop_sillyrename)
33680+ dget(d);
5527c038
JR
33681+ h_inode = NULL;
33682+ if (d_is_positive(d)) {
33683+ h_inode = d_inode(d);
027c5e7a 33684+ ihold(h_inode);
5527c038 33685+ }
1facf9fc 33686+
2cbb1c4b 33687+ lockdep_off();
523b37e3 33688+ *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
2cbb1c4b 33689+ lockdep_on();
1facf9fc 33690+ if (!*a->errp) {
33691+ struct path tmp = {
33692+ .dentry = d->d_parent,
33693+ .mnt = a->path->mnt
33694+ };
33695+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33696+ }
33697+
33698+ if (!stop_sillyrename)
33699+ dput(d);
33700+ if (h_inode)
33701+ iput(h_inode);
33702+
33703+ AuTraceErr(*a->errp);
33704+}
33705+
33706+/*
33707+ * @dir: must be locked.
33708+ * @dentry: target dentry.
33709+ */
523b37e3
AM
33710+int vfsub_unlink(struct inode *dir, struct path *path,
33711+ struct inode **delegated_inode, int force)
1facf9fc 33712+{
33713+ int err;
33714+ struct unlink_args args = {
523b37e3
AM
33715+ .errp = &err,
33716+ .dir = dir,
33717+ .path = path,
33718+ .delegated_inode = delegated_inode
1facf9fc 33719+ };
33720+
33721+ if (!force)
33722+ call_unlink(&args);
33723+ else {
33724+ int wkq_err;
33725+
33726+ wkq_err = au_wkq_wait(call_unlink, &args);
33727+ if (unlikely(wkq_err))
33728+ err = wkq_err;
33729+ }
33730+
33731+ return err;
33732+}
7f207e10
AM
33733diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33734--- /usr/share/empty/fs/aufs/vfsub.h 1970-01-01 01:00:00.000000000 +0100
ba1aed25 33735+++ linux/fs/aufs/vfsub.h 2019-03-05 12:13:00.145891204 +0100
062440b3
AM
33736@@ -0,0 +1,355 @@
33737+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 33738+/*
ba1aed25 33739+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 33740+ *
33741+ * This program, aufs is free software; you can redistribute it and/or modify
33742+ * it under the terms of the GNU General Public License as published by
33743+ * the Free Software Foundation; either version 2 of the License, or
33744+ * (at your option) any later version.
dece6358
AM
33745+ *
33746+ * This program is distributed in the hope that it will be useful,
33747+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33748+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33749+ * GNU General Public License for more details.
33750+ *
33751+ * You should have received a copy of the GNU General Public License
523b37e3 33752+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 33753+ */
33754+
33755+/*
33756+ * sub-routines for VFS
33757+ */
33758+
33759+#ifndef __AUFS_VFSUB_H__
33760+#define __AUFS_VFSUB_H__
33761+
33762+#ifdef __KERNEL__
33763+
33764+#include <linux/fs.h>
be118d29 33765+#include <linux/iversion.h>
b4510431 33766+#include <linux/mount.h>
8cdd5066 33767+#include <linux/posix_acl.h>
c1595e42 33768+#include <linux/xattr.h>
7f207e10 33769+#include "debug.h"
1facf9fc 33770+
7f207e10 33771+/* copied from linux/fs/internal.h */
2cbb1c4b 33772+/* todo: BAD approach!! */
c06a8ce3 33773+extern void __mnt_drop_write(struct vfsmount *);
acd2b654 33774+extern struct file *alloc_empty_file(int, const struct cred *);
7f207e10
AM
33775+
33776+/* ---------------------------------------------------------------------- */
1facf9fc 33777+
33778+/* lock subclass for lower inode */
33779+/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33780+/* reduce? gave up. */
33781+enum {
c1595e42 33782+ AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
1facf9fc 33783+ AuLsc_I_PARENT, /* lower inode, parent first */
33784+ AuLsc_I_PARENT2, /* copyup dirs */
dece6358 33785+ AuLsc_I_PARENT3, /* copyup wh */
1facf9fc 33786+ AuLsc_I_CHILD,
33787+ AuLsc_I_CHILD2,
33788+ AuLsc_I_End
33789+};
33790+
33791+/* to debug easier, do not make them inlined functions */
33792+#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
febd17d6 33793+#define IMustLock(i) AuDebugOn(!inode_is_locked(i))
1facf9fc 33794+
33795+/* ---------------------------------------------------------------------- */
33796+
7f207e10
AM
33797+static inline void vfsub_drop_nlink(struct inode *inode)
33798+{
33799+ AuDebugOn(!inode->i_nlink);
33800+ drop_nlink(inode);
33801+}
33802+
027c5e7a
AM
33803+static inline void vfsub_dead_dir(struct inode *inode)
33804+{
33805+ AuDebugOn(!S_ISDIR(inode->i_mode));
33806+ inode->i_flags |= S_DEAD;
33807+ clear_nlink(inode);
33808+}
33809+
392086de
AM
33810+static inline int vfsub_native_ro(struct inode *inode)
33811+{
8b6a4947 33812+ return sb_rdonly(inode->i_sb)
392086de
AM
33813+ || IS_RDONLY(inode)
33814+ /* || IS_APPEND(inode) */
33815+ || IS_IMMUTABLE(inode);
33816+}
33817+
8cdd5066
JR
33818+#ifdef CONFIG_AUFS_BR_FUSE
33819+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33820+#else
33821+AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33822+#endif
33823+
a2654f78
AM
33824+int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
33825+
7f207e10
AM
33826+/* ---------------------------------------------------------------------- */
33827+
33828+int vfsub_update_h_iattr(struct path *h_path, int *did);
33829+struct file *vfsub_dentry_open(struct path *path, int flags);
33830+struct file *vfsub_filp_open(const char *path, int oflags, int mode);
acd2b654 33831+struct au_branch;
b912730e 33832+struct vfsub_aopen_args {
acd2b654
AM
33833+ struct file *file;
33834+ unsigned int open_flag;
33835+ umode_t create_mode;
33836+ struct au_branch *br;
b912730e 33837+};
b912730e 33838+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
acd2b654 33839+ struct vfsub_aopen_args *args);
1facf9fc 33840+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
b4510431 33841+
febd17d6
JR
33842+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33843+ struct dentry *parent, int len);
1facf9fc 33844+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
33845+ int len);
b4510431
AM
33846+
33847+struct vfsub_lkup_one_args {
33848+ struct dentry **errp;
33849+ struct qstr *name;
33850+ struct dentry *parent;
33851+};
33852+
33853+static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33854+ struct dentry *parent)
33855+{
33856+ return vfsub_lookup_one_len(name->name, parent, name->len);
33857+}
33858+
33859+void vfsub_call_lkup_one(void *args);
33860+
33861+/* ---------------------------------------------------------------------- */
33862+
33863+static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33864+{
33865+ int err;
076b876e 33866+
b4510431
AM
33867+ lockdep_off();
33868+ err = mnt_want_write(mnt);
33869+ lockdep_on();
33870+ return err;
33871+}
33872+
33873+static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33874+{
33875+ lockdep_off();
33876+ mnt_drop_write(mnt);
33877+ lockdep_on();
33878+}
1facf9fc 33879+
7e9cd9fe 33880+#if 0 /* reserved */
c06a8ce3
AM
33881+static inline void vfsub_mnt_drop_write_file(struct file *file)
33882+{
33883+ lockdep_off();
33884+ mnt_drop_write_file(file);
33885+ lockdep_on();
33886+}
7e9cd9fe 33887+#endif
c06a8ce3 33888+
1facf9fc 33889+/* ---------------------------------------------------------------------- */
33890+
33891+struct au_hinode;
33892+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33893+ struct dentry *d2, struct au_hinode *hdir2);
33894+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33895+ struct dentry *d2, struct au_hinode *hdir2);
33896+
537831f9
AM
33897+int vfsub_create(struct inode *dir, struct path *path, int mode,
33898+ bool want_excl);
1facf9fc 33899+int vfsub_symlink(struct inode *dir, struct path *path,
33900+ const char *symname);
33901+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33902+int vfsub_link(struct dentry *src_dentry, struct inode *dir,
523b37e3 33903+ struct path *path, struct inode **delegated_inode);
1facf9fc 33904+int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
523b37e3 33905+ struct inode *hdir, struct path *path,
f2c43d5f 33906+ struct inode **delegated_inode, unsigned int flags);
1facf9fc 33907+int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33908+int vfsub_rmdir(struct inode *dir, struct path *path);
33909+
33910+/* ---------------------------------------------------------------------- */
33911+
33912+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33913+ loff_t *ppos);
33914+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33915+ loff_t *ppos);
33916+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33917+ loff_t *ppos);
33918+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33919+ loff_t *ppos);
4a4d8108 33920+int vfsub_flush(struct file *file, fl_owner_t id);
392086de
AM
33921+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33922+
c06a8ce3
AM
33923+static inline loff_t vfsub_f_size_read(struct file *file)
33924+{
33925+ return i_size_read(file_inode(file));
33926+}
33927+
4a4d8108
AM
33928+static inline unsigned int vfsub_file_flags(struct file *file)
33929+{
33930+ unsigned int flags;
33931+
33932+ spin_lock(&file->f_lock);
33933+ flags = file->f_flags;
33934+ spin_unlock(&file->f_lock);
33935+
33936+ return flags;
33937+}
1308ab2a 33938+
f0c0a007
AM
33939+static inline int vfsub_file_execed(struct file *file)
33940+{
33941+ /* todo: direct access f_flags */
33942+ return !!(vfsub_file_flags(file) & __FMODE_EXEC);
33943+}
33944+
7e9cd9fe 33945+#if 0 /* reserved */
1facf9fc 33946+static inline void vfsub_file_accessed(struct file *h_file)
33947+{
33948+ file_accessed(h_file);
33949+ vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
33950+}
7e9cd9fe 33951+#endif
1facf9fc 33952+
79b8bda9 33953+#if 0 /* reserved */
1facf9fc 33954+static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
33955+ struct dentry *h_dentry)
33956+{
33957+ struct path h_path = {
33958+ .dentry = h_dentry,
33959+ .mnt = h_mnt
33960+ };
92d182d2 33961+ touch_atime(&h_path);
1facf9fc 33962+ vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
33963+}
79b8bda9 33964+#endif
1facf9fc 33965+
cd7a4cd9
AM
33966+static inline int vfsub_update_time(struct inode *h_inode,
33967+ struct timespec64 *ts, int flags)
0c3ec466 33968+{
5afbbe0d 33969+ return update_time(h_inode, ts, flags);
0c3ec466
AM
33970+ /* no vfsub_update_h_iattr() since we don't have struct path */
33971+}
33972+
8cdd5066
JR
33973+#ifdef CONFIG_FS_POSIX_ACL
33974+static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
33975+{
33976+ int err;
33977+
33978+ err = posix_acl_chmod(h_inode, h_mode);
33979+ if (err == -EOPNOTSUPP)
33980+ err = 0;
33981+ return err;
33982+}
33983+#else
33984+AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
33985+#endif
33986+
4a4d8108
AM
33987+long vfsub_splice_to(struct file *in, loff_t *ppos,
33988+ struct pipe_inode_info *pipe, size_t len,
33989+ unsigned int flags);
33990+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33991+ loff_t *ppos, size_t len, unsigned int flags);
c06a8ce3
AM
33992+
33993+static inline long vfsub_truncate(struct path *path, loff_t length)
33994+{
33995+ long err;
076b876e 33996+
c06a8ce3
AM
33997+ lockdep_off();
33998+ err = vfs_truncate(path, length);
33999+ lockdep_on();
34000+ return err;
34001+}
34002+
4a4d8108
AM
34003+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
34004+ struct file *h_file);
53392da6 34005+int vfsub_fsync(struct file *file, struct path *path, int datasync);
4a4d8108 34006+
521ced18
JR
34007+/*
34008+ * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
34009+ * ioctl.
34010+ */
9f237c51
AM
34011+static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
34012+ loff_t len)
521ced18 34013+{
9f237c51 34014+ loff_t err;
521ced18
JR
34015+
34016+ lockdep_off();
9f237c51 34017+ err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
521ced18
JR
34018+ lockdep_on();
34019+
34020+ return err;
34021+}
34022+
34023+/* copy_file_range(2) is a systemcall */
34024+static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
34025+ struct file *dst, loff_t dst_pos,
34026+ size_t len, unsigned int flags)
34027+{
34028+ ssize_t ssz;
34029+
34030+ lockdep_off();
34031+ ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
34032+ lockdep_on();
34033+
34034+ return ssz;
34035+}
34036+
1facf9fc 34037+/* ---------------------------------------------------------------------- */
34038+
34039+static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
34040+{
34041+ loff_t err;
34042+
2cbb1c4b 34043+ lockdep_off();
1facf9fc 34044+ err = vfs_llseek(file, offset, origin);
2cbb1c4b 34045+ lockdep_on();
1facf9fc 34046+ return err;
34047+}
34048+
34049+/* ---------------------------------------------------------------------- */
34050+
4a4d8108
AM
34051+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
34052+int vfsub_sio_rmdir(struct inode *dir, struct path *path);
523b37e3
AM
34053+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
34054+ struct inode **delegated_inode);
34055+int vfsub_notify_change(struct path *path, struct iattr *ia,
34056+ struct inode **delegated_inode);
34057+int vfsub_unlink(struct inode *dir, struct path *path,
34058+ struct inode **delegated_inode, int force);
4a4d8108 34059+
521ced18
JR
34060+static inline int vfsub_getattr(const struct path *path, struct kstat *st)
34061+{
34062+ return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
34063+}
34064+
c1595e42
JR
34065+/* ---------------------------------------------------------------------- */
34066+
34067+static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
34068+ const void *value, size_t size, int flags)
34069+{
34070+ int err;
34071+
34072+ lockdep_off();
34073+ err = vfs_setxattr(dentry, name, value, size, flags);
34074+ lockdep_on();
34075+
34076+ return err;
34077+}
34078+
34079+static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
34080+{
34081+ int err;
34082+
34083+ lockdep_off();
34084+ err = vfs_removexattr(dentry, name);
34085+ lockdep_on();
34086+
34087+ return err;
34088+}
34089+
1facf9fc 34090+#endif /* __KERNEL__ */
34091+#endif /* __AUFS_VFSUB_H__ */
7f207e10
AM
34092diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
34093--- /usr/share/empty/fs/aufs/wbr_policy.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 34094+++ linux/fs/aufs/wbr_policy.c 2019-03-05 12:13:00.145891204 +0100
cd7a4cd9
AM
34095@@ -0,0 +1,830 @@
34096+// SPDX-License-Identifier: GPL-2.0
1facf9fc 34097+/*
ba1aed25 34098+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 34099+ *
34100+ * This program, aufs is free software; you can redistribute it and/or modify
34101+ * it under the terms of the GNU General Public License as published by
34102+ * the Free Software Foundation; either version 2 of the License, or
34103+ * (at your option) any later version.
dece6358
AM
34104+ *
34105+ * This program is distributed in the hope that it will be useful,
34106+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
34107+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34108+ * GNU General Public License for more details.
34109+ *
34110+ * You should have received a copy of the GNU General Public License
523b37e3 34111+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 34112+ */
34113+
34114+/*
34115+ * policies for selecting one among multiple writable branches
34116+ */
34117+
34118+#include <linux/statfs.h>
34119+#include "aufs.h"
34120+
34121+/* subset of cpup_attr() */
34122+static noinline_for_stack
34123+int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
34124+{
34125+ int err, sbits;
34126+ struct iattr ia;
34127+ struct inode *h_isrc;
34128+
5527c038 34129+ h_isrc = d_inode(h_src);
1facf9fc 34130+ ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
34131+ ia.ia_mode = h_isrc->i_mode;
34132+ ia.ia_uid = h_isrc->i_uid;
34133+ ia.ia_gid = h_isrc->i_gid;
34134+ sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
5527c038 34135+ au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
523b37e3
AM
34136+ /* no delegation since it is just created */
34137+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 34138+
34139+ /* is this nfs only? */
34140+ if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
34141+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
34142+ ia.ia_mode = h_isrc->i_mode;
523b37e3 34143+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 34144+ }
34145+
34146+ return err;
34147+}
34148+
34149+#define AuCpdown_PARENT_OPQ 1
34150+#define AuCpdown_WHED (1 << 1)
34151+#define AuCpdown_MADE_DIR (1 << 2)
34152+#define AuCpdown_DIROPQ (1 << 3)
34153+#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
7f207e10
AM
34154+#define au_fset_cpdown(flags, name) \
34155+ do { (flags) |= AuCpdown_##name; } while (0)
34156+#define au_fclr_cpdown(flags, name) \
34157+ do { (flags) &= ~AuCpdown_##name; } while (0)
1facf9fc 34158+
1facf9fc 34159+static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
c2b27bf2 34160+ unsigned int *flags)
1facf9fc 34161+{
34162+ int err;
34163+ struct dentry *opq_dentry;
34164+
34165+ opq_dentry = au_diropq_create(dentry, bdst);
34166+ err = PTR_ERR(opq_dentry);
34167+ if (IS_ERR(opq_dentry))
34168+ goto out;
34169+ dput(opq_dentry);
c2b27bf2 34170+ au_fset_cpdown(*flags, DIROPQ);
1facf9fc 34171+
4f0767ce 34172+out:
1facf9fc 34173+ return err;
34174+}
34175+
34176+static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34177+ struct inode *dir, aufs_bindex_t bdst)
34178+{
34179+ int err;
34180+ struct path h_path;
34181+ struct au_branch *br;
34182+
34183+ br = au_sbr(dentry->d_sb, bdst);
34184+ h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34185+ err = PTR_ERR(h_path.dentry);
34186+ if (IS_ERR(h_path.dentry))
34187+ goto out;
34188+
34189+ err = 0;
5527c038 34190+ if (d_is_positive(h_path.dentry)) {
86dc4139 34191+ h_path.mnt = au_br_mnt(br);
1facf9fc 34192+ err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34193+ dentry);
34194+ }
34195+ dput(h_path.dentry);
34196+
4f0767ce 34197+out:
1facf9fc 34198+ return err;
34199+}
34200+
34201+static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 34202+ struct au_pin *pin,
1facf9fc 34203+ struct dentry *h_parent, void *arg)
34204+{
34205+ int err, rerr;
5afbbe0d 34206+ aufs_bindex_t bopq, btop;
1facf9fc 34207+ struct path h_path;
34208+ struct dentry *parent;
34209+ struct inode *h_dir, *h_inode, *inode, *dir;
c2b27bf2 34210+ unsigned int *flags = arg;
1facf9fc 34211+
5afbbe0d 34212+ btop = au_dbtop(dentry);
1facf9fc 34213+ /* dentry is di-locked */
34214+ parent = dget_parent(dentry);
5527c038
JR
34215+ dir = d_inode(parent);
34216+ h_dir = d_inode(h_parent);
1facf9fc 34217+ AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34218+ IMustLock(h_dir);
34219+
86dc4139 34220+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
1facf9fc 34221+ if (unlikely(err < 0))
34222+ goto out;
34223+ h_path.dentry = au_h_dptr(dentry, bdst);
34224+ h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
cd7a4cd9 34225+ err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
1facf9fc 34226+ if (unlikely(err))
34227+ goto out_put;
c2b27bf2 34228+ au_fset_cpdown(*flags, MADE_DIR);
1facf9fc 34229+
1facf9fc 34230+ bopq = au_dbdiropq(dentry);
c2b27bf2
AM
34231+ au_fclr_cpdown(*flags, WHED);
34232+ au_fclr_cpdown(*flags, DIROPQ);
1facf9fc 34233+ if (au_dbwh(dentry) == bdst)
c2b27bf2
AM
34234+ au_fset_cpdown(*flags, WHED);
34235+ if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34236+ au_fset_cpdown(*flags, PARENT_OPQ);
5527c038 34237+ h_inode = d_inode(h_path.dentry);
febd17d6 34238+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
c2b27bf2
AM
34239+ if (au_ftest_cpdown(*flags, WHED)) {
34240+ err = au_cpdown_dir_opq(dentry, bdst, flags);
1facf9fc 34241+ if (unlikely(err)) {
febd17d6 34242+ inode_unlock(h_inode);
1facf9fc 34243+ goto out_dir;
34244+ }
34245+ }
34246+
5afbbe0d 34247+ err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
febd17d6 34248+ inode_unlock(h_inode);
1facf9fc 34249+ if (unlikely(err))
34250+ goto out_opq;
34251+
c2b27bf2 34252+ if (au_ftest_cpdown(*flags, WHED)) {
1facf9fc 34253+ err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34254+ if (unlikely(err))
34255+ goto out_opq;
34256+ }
34257+
5527c038 34258+ inode = d_inode(dentry);
5afbbe0d
AM
34259+ if (au_ibbot(inode) < bdst)
34260+ au_set_ibbot(inode, bdst);
1facf9fc 34261+ au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34262+ au_hi_flags(inode, /*isdir*/1));
076b876e 34263+ au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
1facf9fc 34264+ goto out; /* success */
34265+
34266+ /* revert */
4f0767ce 34267+out_opq:
c2b27bf2 34268+ if (au_ftest_cpdown(*flags, DIROPQ)) {
febd17d6 34269+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 34270+ rerr = au_diropq_remove(dentry, bdst);
febd17d6 34271+ inode_unlock(h_inode);
1facf9fc 34272+ if (unlikely(rerr)) {
523b37e3
AM
34273+ AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34274+ dentry, bdst, rerr);
1facf9fc 34275+ err = -EIO;
34276+ goto out;
34277+ }
34278+ }
4f0767ce 34279+out_dir:
c2b27bf2 34280+ if (au_ftest_cpdown(*flags, MADE_DIR)) {
1facf9fc 34281+ rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34282+ if (unlikely(rerr)) {
523b37e3
AM
34283+ AuIOErr("failed removing %pd b%d (%d)\n",
34284+ dentry, bdst, rerr);
1facf9fc 34285+ err = -EIO;
34286+ }
34287+ }
4f0767ce 34288+out_put:
1facf9fc 34289+ au_set_h_dptr(dentry, bdst, NULL);
5afbbe0d
AM
34290+ if (au_dbbot(dentry) == bdst)
34291+ au_update_dbbot(dentry);
4f0767ce 34292+out:
1facf9fc 34293+ dput(parent);
34294+ return err;
34295+}
34296+
34297+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34298+{
34299+ int err;
c2b27bf2 34300+ unsigned int flags;
1facf9fc 34301+
c2b27bf2
AM
34302+ flags = 0;
34303+ err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
1facf9fc 34304+
34305+ return err;
34306+}
34307+
34308+/* ---------------------------------------------------------------------- */
34309+
34310+/* policies for create */
34311+
c2b27bf2 34312+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
4a4d8108
AM
34313+{
34314+ int err, i, j, ndentry;
34315+ aufs_bindex_t bopq;
34316+ struct au_dcsub_pages dpages;
34317+ struct au_dpage *dpage;
34318+ struct dentry **dentries, *parent, *d;
34319+
34320+ err = au_dpages_init(&dpages, GFP_NOFS);
34321+ if (unlikely(err))
34322+ goto out;
34323+ parent = dget_parent(dentry);
027c5e7a 34324+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
4a4d8108
AM
34325+ if (unlikely(err))
34326+ goto out_free;
34327+
34328+ err = bindex;
34329+ for (i = 0; i < dpages.ndpage; i++) {
34330+ dpage = dpages.dpages + i;
34331+ dentries = dpage->dentries;
34332+ ndentry = dpage->ndentry;
34333+ for (j = 0; j < ndentry; j++) {
34334+ d = dentries[j];
34335+ di_read_lock_parent2(d, !AuLock_IR);
34336+ bopq = au_dbdiropq(d);
34337+ di_read_unlock(d, !AuLock_IR);
34338+ if (bopq >= 0 && bopq < err)
34339+ err = bopq;
34340+ }
34341+ }
34342+
34343+out_free:
34344+ dput(parent);
34345+ au_dpages_free(&dpages);
34346+out:
34347+ return err;
34348+}
34349+
1facf9fc 34350+static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34351+{
34352+ for (; bindex >= 0; bindex--)
34353+ if (!au_br_rdonly(au_sbr(sb, bindex)))
34354+ return bindex;
34355+ return -EROFS;
34356+}
34357+
34358+/* top down parent */
392086de
AM
34359+static int au_wbr_create_tdp(struct dentry *dentry,
34360+ unsigned int flags __maybe_unused)
1facf9fc 34361+{
34362+ int err;
5afbbe0d 34363+ aufs_bindex_t btop, bindex;
1facf9fc 34364+ struct super_block *sb;
34365+ struct dentry *parent, *h_parent;
34366+
34367+ sb = dentry->d_sb;
5afbbe0d
AM
34368+ btop = au_dbtop(dentry);
34369+ err = btop;
34370+ if (!au_br_rdonly(au_sbr(sb, btop)))
1facf9fc 34371+ goto out;
34372+
34373+ err = -EROFS;
34374+ parent = dget_parent(dentry);
5afbbe0d 34375+ for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
1facf9fc 34376+ h_parent = au_h_dptr(parent, bindex);
5527c038 34377+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34378+ continue;
34379+
34380+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34381+ err = bindex;
34382+ break;
34383+ }
34384+ }
34385+ dput(parent);
34386+
34387+ /* bottom up here */
4a4d8108 34388+ if (unlikely(err < 0)) {
5afbbe0d 34389+ err = au_wbr_bu(sb, btop - 1);
4a4d8108
AM
34390+ if (err >= 0)
34391+ err = au_wbr_nonopq(dentry, err);
34392+ }
1facf9fc 34393+
4f0767ce 34394+out:
1facf9fc 34395+ AuDbg("b%d\n", err);
34396+ return err;
34397+}
34398+
34399+/* ---------------------------------------------------------------------- */
34400+
34401+/* an exception for the policy other than tdp */
34402+static int au_wbr_create_exp(struct dentry *dentry)
34403+{
34404+ int err;
34405+ aufs_bindex_t bwh, bdiropq;
34406+ struct dentry *parent;
34407+
34408+ err = -1;
34409+ bwh = au_dbwh(dentry);
34410+ parent = dget_parent(dentry);
34411+ bdiropq = au_dbdiropq(parent);
34412+ if (bwh >= 0) {
34413+ if (bdiropq >= 0)
34414+ err = min(bdiropq, bwh);
34415+ else
34416+ err = bwh;
34417+ AuDbg("%d\n", err);
34418+ } else if (bdiropq >= 0) {
34419+ err = bdiropq;
34420+ AuDbg("%d\n", err);
34421+ }
34422+ dput(parent);
34423+
4a4d8108
AM
34424+ if (err >= 0)
34425+ err = au_wbr_nonopq(dentry, err);
34426+
1facf9fc 34427+ if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34428+ err = -1;
34429+
34430+ AuDbg("%d\n", err);
34431+ return err;
34432+}
34433+
34434+/* ---------------------------------------------------------------------- */
34435+
34436+/* round robin */
34437+static int au_wbr_create_init_rr(struct super_block *sb)
34438+{
34439+ int err;
34440+
5afbbe0d 34441+ err = au_wbr_bu(sb, au_sbbot(sb));
1facf9fc 34442+ atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
dece6358 34443+ /* smp_mb(); */
1facf9fc 34444+
34445+ AuDbg("b%d\n", err);
34446+ return err;
34447+}
34448+
392086de 34449+static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
1facf9fc 34450+{
34451+ int err, nbr;
34452+ unsigned int u;
5afbbe0d 34453+ aufs_bindex_t bindex, bbot;
1facf9fc 34454+ struct super_block *sb;
34455+ atomic_t *next;
34456+
34457+ err = au_wbr_create_exp(dentry);
34458+ if (err >= 0)
34459+ goto out;
34460+
34461+ sb = dentry->d_sb;
34462+ next = &au_sbi(sb)->si_wbr_rr_next;
5afbbe0d
AM
34463+ bbot = au_sbbot(sb);
34464+ nbr = bbot + 1;
34465+ for (bindex = 0; bindex <= bbot; bindex++) {
392086de 34466+ if (!au_ftest_wbr(flags, DIR)) {
1facf9fc 34467+ err = atomic_dec_return(next) + 1;
34468+ /* modulo for 0 is meaningless */
34469+ if (unlikely(!err))
34470+ err = atomic_dec_return(next) + 1;
34471+ } else
34472+ err = atomic_read(next);
34473+ AuDbg("%d\n", err);
34474+ u = err;
34475+ err = u % nbr;
34476+ AuDbg("%d\n", err);
34477+ if (!au_br_rdonly(au_sbr(sb, err)))
34478+ break;
34479+ err = -EROFS;
34480+ }
34481+
4a4d8108
AM
34482+ if (err >= 0)
34483+ err = au_wbr_nonopq(dentry, err);
34484+
4f0767ce 34485+out:
1facf9fc 34486+ AuDbg("%d\n", err);
34487+ return err;
34488+}
34489+
34490+/* ---------------------------------------------------------------------- */
34491+
34492+/* most free space */
392086de 34493+static void au_mfs(struct dentry *dentry, struct dentry *parent)
1facf9fc 34494+{
34495+ struct super_block *sb;
34496+ struct au_branch *br;
34497+ struct au_wbr_mfs *mfs;
392086de 34498+ struct dentry *h_parent;
5afbbe0d 34499+ aufs_bindex_t bindex, bbot;
1facf9fc 34500+ int err;
34501+ unsigned long long b, bavail;
7f207e10 34502+ struct path h_path;
1facf9fc 34503+ /* reduce the stack usage */
34504+ struct kstatfs *st;
34505+
34506+ st = kmalloc(sizeof(*st), GFP_NOFS);
34507+ if (unlikely(!st)) {
34508+ AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34509+ return;
34510+ }
34511+
34512+ bavail = 0;
34513+ sb = dentry->d_sb;
34514+ mfs = &au_sbi(sb)->si_wbr_mfs;
dece6358 34515+ MtxMustLock(&mfs->mfs_lock);
1facf9fc 34516+ mfs->mfs_bindex = -EROFS;
34517+ mfs->mfsrr_bytes = 0;
392086de
AM
34518+ if (!parent) {
34519+ bindex = 0;
5afbbe0d 34520+ bbot = au_sbbot(sb);
392086de 34521+ } else {
5afbbe0d
AM
34522+ bindex = au_dbtop(parent);
34523+ bbot = au_dbtaildir(parent);
392086de
AM
34524+ }
34525+
5afbbe0d 34526+ for (; bindex <= bbot; bindex++) {
392086de
AM
34527+ if (parent) {
34528+ h_parent = au_h_dptr(parent, bindex);
5527c038 34529+ if (!h_parent || d_is_negative(h_parent))
392086de
AM
34530+ continue;
34531+ }
1facf9fc 34532+ br = au_sbr(sb, bindex);
34533+ if (au_br_rdonly(br))
34534+ continue;
34535+
34536+ /* sb->s_root for NFS is unreliable */
86dc4139 34537+ h_path.mnt = au_br_mnt(br);
7f207e10
AM
34538+ h_path.dentry = h_path.mnt->mnt_root;
34539+ err = vfs_statfs(&h_path, st);
1facf9fc 34540+ if (unlikely(err)) {
34541+ AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34542+ continue;
34543+ }
34544+
34545+ /* when the available size is equal, select the lower one */
34546+ BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34547+ || sizeof(b) < sizeof(st->f_bsize));
34548+ b = st->f_bavail * st->f_bsize;
34549+ br->br_wbr->wbr_bytes = b;
34550+ if (b >= bavail) {
34551+ bavail = b;
34552+ mfs->mfs_bindex = bindex;
34553+ mfs->mfs_jiffy = jiffies;
34554+ }
34555+ }
34556+
34557+ mfs->mfsrr_bytes = bavail;
34558+ AuDbg("b%d\n", mfs->mfs_bindex);
9f237c51 34559+ au_kfree_rcu(st);
1facf9fc 34560+}
34561+
392086de 34562+static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
1facf9fc 34563+{
34564+ int err;
392086de 34565+ struct dentry *parent;
1facf9fc 34566+ struct super_block *sb;
34567+ struct au_wbr_mfs *mfs;
34568+
34569+ err = au_wbr_create_exp(dentry);
34570+ if (err >= 0)
34571+ goto out;
34572+
34573+ sb = dentry->d_sb;
392086de
AM
34574+ parent = NULL;
34575+ if (au_ftest_wbr(flags, PARENT))
34576+ parent = dget_parent(dentry);
1facf9fc 34577+ mfs = &au_sbi(sb)->si_wbr_mfs;
34578+ mutex_lock(&mfs->mfs_lock);
34579+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34580+ || mfs->mfs_bindex < 0
34581+ || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
392086de 34582+ au_mfs(dentry, parent);
1facf9fc 34583+ mutex_unlock(&mfs->mfs_lock);
34584+ err = mfs->mfs_bindex;
392086de 34585+ dput(parent);
1facf9fc 34586+
4a4d8108
AM
34587+ if (err >= 0)
34588+ err = au_wbr_nonopq(dentry, err);
34589+
4f0767ce 34590+out:
1facf9fc 34591+ AuDbg("b%d\n", err);
34592+ return err;
34593+}
34594+
34595+static int au_wbr_create_init_mfs(struct super_block *sb)
34596+{
34597+ struct au_wbr_mfs *mfs;
34598+
34599+ mfs = &au_sbi(sb)->si_wbr_mfs;
34600+ mutex_init(&mfs->mfs_lock);
34601+ mfs->mfs_jiffy = 0;
34602+ mfs->mfs_bindex = -EROFS;
34603+
34604+ return 0;
34605+}
34606+
34607+static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34608+{
34609+ mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34610+ return 0;
34611+}
34612+
34613+/* ---------------------------------------------------------------------- */
34614+
f2c43d5f
AM
34615+/* top down regardless parent, and then mfs */
34616+static int au_wbr_create_tdmfs(struct dentry *dentry,
34617+ unsigned int flags __maybe_unused)
34618+{
34619+ int err;
34620+ aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34621+ unsigned long long watermark;
34622+ struct super_block *sb;
34623+ struct au_wbr_mfs *mfs;
34624+ struct au_branch *br;
34625+ struct dentry *parent;
34626+
34627+ sb = dentry->d_sb;
34628+ mfs = &au_sbi(sb)->si_wbr_mfs;
34629+ mutex_lock(&mfs->mfs_lock);
34630+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34631+ || mfs->mfs_bindex < 0)
34632+ au_mfs(dentry, /*parent*/NULL);
34633+ watermark = mfs->mfsrr_watermark;
34634+ bmfs = mfs->mfs_bindex;
34635+ mutex_unlock(&mfs->mfs_lock);
34636+
34637+ /* another style of au_wbr_create_exp() */
34638+ bwh = au_dbwh(dentry);
34639+ parent = dget_parent(dentry);
34640+ btail = au_dbtaildir(parent);
34641+ if (bwh >= 0 && bwh < btail)
34642+ btail = bwh;
34643+
34644+ err = au_wbr_nonopq(dentry, btail);
34645+ if (unlikely(err < 0))
34646+ goto out;
34647+ btail = err;
34648+ bfound = -1;
34649+ for (bindex = 0; bindex <= btail; bindex++) {
34650+ br = au_sbr(sb, bindex);
34651+ if (au_br_rdonly(br))
34652+ continue;
34653+ if (br->br_wbr->wbr_bytes > watermark) {
34654+ bfound = bindex;
34655+ break;
34656+ }
34657+ }
34658+ err = bfound;
34659+ if (err < 0)
34660+ err = bmfs;
34661+
34662+out:
34663+ dput(parent);
34664+ AuDbg("b%d\n", err);
34665+ return err;
34666+}
34667+
34668+/* ---------------------------------------------------------------------- */
34669+
1facf9fc 34670+/* most free space and then round robin */
392086de 34671+static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
1facf9fc 34672+{
34673+ int err;
34674+ struct au_wbr_mfs *mfs;
34675+
392086de 34676+ err = au_wbr_create_mfs(dentry, flags);
1facf9fc 34677+ if (err >= 0) {
34678+ mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
dece6358 34679+ mutex_lock(&mfs->mfs_lock);
1facf9fc 34680+ if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
392086de 34681+ err = au_wbr_create_rr(dentry, flags);
dece6358 34682+ mutex_unlock(&mfs->mfs_lock);
1facf9fc 34683+ }
34684+
34685+ AuDbg("b%d\n", err);
34686+ return err;
34687+}
34688+
34689+static int au_wbr_create_init_mfsrr(struct super_block *sb)
34690+{
34691+ int err;
34692+
34693+ au_wbr_create_init_mfs(sb); /* ignore */
34694+ err = au_wbr_create_init_rr(sb);
34695+
34696+ return err;
34697+}
34698+
34699+/* ---------------------------------------------------------------------- */
34700+
34701+/* top down parent and most free space */
392086de 34702+static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
1facf9fc 34703+{
34704+ int err, e2;
34705+ unsigned long long b;
5afbbe0d 34706+ aufs_bindex_t bindex, btop, bbot;
1facf9fc 34707+ struct super_block *sb;
34708+ struct dentry *parent, *h_parent;
34709+ struct au_branch *br;
34710+
392086de 34711+ err = au_wbr_create_tdp(dentry, flags);
1facf9fc 34712+ if (unlikely(err < 0))
34713+ goto out;
34714+ parent = dget_parent(dentry);
5afbbe0d
AM
34715+ btop = au_dbtop(parent);
34716+ bbot = au_dbtaildir(parent);
34717+ if (btop == bbot)
1facf9fc 34718+ goto out_parent; /* success */
34719+
392086de 34720+ e2 = au_wbr_create_mfs(dentry, flags);
1facf9fc 34721+ if (e2 < 0)
34722+ goto out_parent; /* success */
34723+
34724+ /* when the available size is equal, select upper one */
34725+ sb = dentry->d_sb;
34726+ br = au_sbr(sb, err);
34727+ b = br->br_wbr->wbr_bytes;
34728+ AuDbg("b%d, %llu\n", err, b);
34729+
5afbbe0d 34730+ for (bindex = btop; bindex <= bbot; bindex++) {
1facf9fc 34731+ h_parent = au_h_dptr(parent, bindex);
5527c038 34732+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34733+ continue;
34734+
34735+ br = au_sbr(sb, bindex);
34736+ if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34737+ b = br->br_wbr->wbr_bytes;
34738+ err = bindex;
34739+ AuDbg("b%d, %llu\n", err, b);
34740+ }
34741+ }
34742+
4a4d8108
AM
34743+ if (err >= 0)
34744+ err = au_wbr_nonopq(dentry, err);
34745+
4f0767ce 34746+out_parent:
1facf9fc 34747+ dput(parent);
4f0767ce 34748+out:
1facf9fc 34749+ AuDbg("b%d\n", err);
34750+ return err;
34751+}
34752+
34753+/* ---------------------------------------------------------------------- */
34754+
392086de
AM
34755+/*
34756+ * - top down parent
34757+ * - most free space with parent
34758+ * - most free space round-robin regardless parent
34759+ */
34760+static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34761+{
34762+ int err;
34763+ unsigned long long watermark;
34764+ struct super_block *sb;
34765+ struct au_branch *br;
34766+ struct au_wbr_mfs *mfs;
34767+
34768+ err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34769+ if (unlikely(err < 0))
34770+ goto out;
34771+
34772+ sb = dentry->d_sb;
34773+ br = au_sbr(sb, err);
34774+ mfs = &au_sbi(sb)->si_wbr_mfs;
34775+ mutex_lock(&mfs->mfs_lock);
34776+ watermark = mfs->mfsrr_watermark;
34777+ mutex_unlock(&mfs->mfs_lock);
34778+ if (br->br_wbr->wbr_bytes < watermark)
34779+ /* regardless the parent dir */
34780+ err = au_wbr_create_mfsrr(dentry, flags);
34781+
34782+out:
34783+ AuDbg("b%d\n", err);
34784+ return err;
34785+}
34786+
34787+/* ---------------------------------------------------------------------- */
34788+
1facf9fc 34789+/* policies for copyup */
34790+
34791+/* top down parent */
34792+static int au_wbr_copyup_tdp(struct dentry *dentry)
34793+{
392086de 34794+ return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
1facf9fc 34795+}
34796+
34797+/* bottom up parent */
34798+static int au_wbr_copyup_bup(struct dentry *dentry)
34799+{
34800+ int err;
5afbbe0d 34801+ aufs_bindex_t bindex, btop;
1facf9fc 34802+ struct dentry *parent, *h_parent;
34803+ struct super_block *sb;
34804+
34805+ err = -EROFS;
34806+ sb = dentry->d_sb;
34807+ parent = dget_parent(dentry);
5afbbe0d
AM
34808+ btop = au_dbtop(parent);
34809+ for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
1facf9fc 34810+ h_parent = au_h_dptr(parent, bindex);
5527c038 34811+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34812+ continue;
34813+
34814+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34815+ err = bindex;
34816+ break;
34817+ }
34818+ }
34819+ dput(parent);
34820+
34821+ /* bottom up here */
34822+ if (unlikely(err < 0))
5afbbe0d 34823+ err = au_wbr_bu(sb, btop - 1);
1facf9fc 34824+
34825+ AuDbg("b%d\n", err);
34826+ return err;
34827+}
34828+
34829+/* bottom up */
5afbbe0d 34830+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
1facf9fc 34831+{
34832+ int err;
34833+
5afbbe0d 34834+ err = au_wbr_bu(dentry->d_sb, btop);
4a4d8108 34835+ AuDbg("b%d\n", err);
5afbbe0d 34836+ if (err > btop)
4a4d8108 34837+ err = au_wbr_nonopq(dentry, err);
1facf9fc 34838+
34839+ AuDbg("b%d\n", err);
34840+ return err;
34841+}
34842+
076b876e
AM
34843+static int au_wbr_copyup_bu(struct dentry *dentry)
34844+{
34845+ int err;
5afbbe0d 34846+ aufs_bindex_t btop;
076b876e 34847+
5afbbe0d
AM
34848+ btop = au_dbtop(dentry);
34849+ err = au_wbr_do_copyup_bu(dentry, btop);
076b876e
AM
34850+ return err;
34851+}
34852+
1facf9fc 34853+/* ---------------------------------------------------------------------- */
34854+
34855+struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34856+ [AuWbrCopyup_TDP] = {
34857+ .copyup = au_wbr_copyup_tdp
34858+ },
34859+ [AuWbrCopyup_BUP] = {
34860+ .copyup = au_wbr_copyup_bup
34861+ },
34862+ [AuWbrCopyup_BU] = {
34863+ .copyup = au_wbr_copyup_bu
34864+ }
34865+};
34866+
34867+struct au_wbr_create_operations au_wbr_create_ops[] = {
34868+ [AuWbrCreate_TDP] = {
34869+ .create = au_wbr_create_tdp
34870+ },
34871+ [AuWbrCreate_RR] = {
34872+ .create = au_wbr_create_rr,
34873+ .init = au_wbr_create_init_rr
34874+ },
34875+ [AuWbrCreate_MFS] = {
34876+ .create = au_wbr_create_mfs,
34877+ .init = au_wbr_create_init_mfs,
34878+ .fin = au_wbr_create_fin_mfs
34879+ },
34880+ [AuWbrCreate_MFSV] = {
34881+ .create = au_wbr_create_mfs,
34882+ .init = au_wbr_create_init_mfs,
34883+ .fin = au_wbr_create_fin_mfs
34884+ },
34885+ [AuWbrCreate_MFSRR] = {
34886+ .create = au_wbr_create_mfsrr,
34887+ .init = au_wbr_create_init_mfsrr,
34888+ .fin = au_wbr_create_fin_mfs
34889+ },
34890+ [AuWbrCreate_MFSRRV] = {
34891+ .create = au_wbr_create_mfsrr,
34892+ .init = au_wbr_create_init_mfsrr,
34893+ .fin = au_wbr_create_fin_mfs
34894+ },
f2c43d5f
AM
34895+ [AuWbrCreate_TDMFS] = {
34896+ .create = au_wbr_create_tdmfs,
34897+ .init = au_wbr_create_init_mfs,
34898+ .fin = au_wbr_create_fin_mfs
34899+ },
34900+ [AuWbrCreate_TDMFSV] = {
34901+ .create = au_wbr_create_tdmfs,
34902+ .init = au_wbr_create_init_mfs,
34903+ .fin = au_wbr_create_fin_mfs
34904+ },
1facf9fc 34905+ [AuWbrCreate_PMFS] = {
34906+ .create = au_wbr_create_pmfs,
34907+ .init = au_wbr_create_init_mfs,
34908+ .fin = au_wbr_create_fin_mfs
34909+ },
34910+ [AuWbrCreate_PMFSV] = {
34911+ .create = au_wbr_create_pmfs,
34912+ .init = au_wbr_create_init_mfs,
34913+ .fin = au_wbr_create_fin_mfs
392086de
AM
34914+ },
34915+ [AuWbrCreate_PMFSRR] = {
34916+ .create = au_wbr_create_pmfsrr,
34917+ .init = au_wbr_create_init_mfsrr,
34918+ .fin = au_wbr_create_fin_mfs
34919+ },
34920+ [AuWbrCreate_PMFSRRV] = {
34921+ .create = au_wbr_create_pmfsrr,
34922+ .init = au_wbr_create_init_mfsrr,
34923+ .fin = au_wbr_create_fin_mfs
1facf9fc 34924+ }
34925+};
7f207e10
AM
34926diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
34927--- /usr/share/empty/fs/aufs/whout.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 34928+++ linux/fs/aufs/whout.c 2019-03-05 12:13:00.145891204 +0100
062440b3 34929@@ -0,0 +1,1062 @@
cd7a4cd9 34930+// SPDX-License-Identifier: GPL-2.0
1facf9fc 34931+/*
ba1aed25 34932+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 34933+ *
34934+ * This program, aufs is free software; you can redistribute it and/or modify
34935+ * it under the terms of the GNU General Public License as published by
34936+ * the Free Software Foundation; either version 2 of the License, or
34937+ * (at your option) any later version.
dece6358
AM
34938+ *
34939+ * This program is distributed in the hope that it will be useful,
34940+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
34941+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34942+ * GNU General Public License for more details.
34943+ *
34944+ * You should have received a copy of the GNU General Public License
523b37e3 34945+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 34946+ */
34947+
34948+/*
34949+ * whiteout for logical deletion and opaque directory
34950+ */
34951+
1facf9fc 34952+#include "aufs.h"
34953+
cd7a4cd9 34954+#define WH_MASK 0444
1facf9fc 34955+
34956+/*
34957+ * If a directory contains this file, then it is opaque. We start with the
34958+ * .wh. flag so that it is blocked by lookup.
34959+ */
0c3ec466
AM
34960+static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
34961+ sizeof(AUFS_WH_DIROPQ) - 1);
1facf9fc 34962+
34963+/*
34964+ * generate whiteout name, which is NOT terminated by NULL.
34965+ * @name: original d_name.name
34966+ * @len: original d_name.len
34967+ * @wh: whiteout qstr
34968+ * returns zero when succeeds, otherwise error.
34969+ * succeeded value as wh->name should be freed by kfree().
34970+ */
34971+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
34972+{
34973+ char *p;
34974+
34975+ if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
34976+ return -ENAMETOOLONG;
34977+
34978+ wh->len = name->len + AUFS_WH_PFX_LEN;
34979+ p = kmalloc(wh->len, GFP_NOFS);
34980+ wh->name = p;
34981+ if (p) {
34982+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
34983+ memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
34984+ /* smp_mb(); */
34985+ return 0;
34986+ }
34987+ return -ENOMEM;
34988+}
34989+
34990+/* ---------------------------------------------------------------------- */
34991+
34992+/*
34993+ * test if the @wh_name exists under @h_parent.
34994+ * @try_sio specifies the necessary of super-io.
34995+ */
076b876e 34996+int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
1facf9fc 34997+{
34998+ int err;
34999+ struct dentry *wh_dentry;
1facf9fc 35000+
1facf9fc 35001+ if (!try_sio)
b4510431 35002+ wh_dentry = vfsub_lkup_one(wh_name, h_parent);
1facf9fc 35003+ else
076b876e 35004+ wh_dentry = au_sio_lkup_one(wh_name, h_parent);
1facf9fc 35005+ err = PTR_ERR(wh_dentry);
2000de60
JR
35006+ if (IS_ERR(wh_dentry)) {
35007+ if (err == -ENAMETOOLONG)
35008+ err = 0;
1facf9fc 35009+ goto out;
2000de60 35010+ }
1facf9fc 35011+
35012+ err = 0;
5527c038 35013+ if (d_is_negative(wh_dentry))
1facf9fc 35014+ goto out_wh; /* success */
35015+
35016+ err = 1;
7e9cd9fe 35017+ if (d_is_reg(wh_dentry))
1facf9fc 35018+ goto out_wh; /* success */
35019+
35020+ err = -EIO;
523b37e3 35021+ AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
5527c038 35022+ wh_dentry, d_inode(wh_dentry)->i_mode);
1facf9fc 35023+
4f0767ce 35024+out_wh:
1facf9fc 35025+ dput(wh_dentry);
4f0767ce 35026+out:
1facf9fc 35027+ return err;
35028+}
35029+
35030+/*
35031+ * test if the @h_dentry sets opaque or not.
35032+ */
076b876e 35033+int au_diropq_test(struct dentry *h_dentry)
1facf9fc 35034+{
35035+ int err;
35036+ struct inode *h_dir;
35037+
5527c038 35038+ h_dir = d_inode(h_dentry);
076b876e 35039+ err = au_wh_test(h_dentry, &diropq_name,
1facf9fc 35040+ au_test_h_perm_sio(h_dir, MAY_EXEC));
35041+ return err;
35042+}
35043+
35044+/*
35045+ * returns a negative dentry whose name is unique and temporary.
35046+ */
35047+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35048+ struct qstr *prefix)
35049+{
1facf9fc 35050+ struct dentry *dentry;
35051+ int i;
027c5e7a 35052+ char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
4a4d8108 35053+ *name, *p;
027c5e7a 35054+ /* strict atomic_t is unnecessary here */
1facf9fc 35055+ static unsigned short cnt;
35056+ struct qstr qs;
35057+
4a4d8108
AM
35058+ BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
35059+
1facf9fc 35060+ name = defname;
027c5e7a
AM
35061+ qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
35062+ if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
1facf9fc 35063+ dentry = ERR_PTR(-ENAMETOOLONG);
4a4d8108 35064+ if (unlikely(qs.len > NAME_MAX))
1facf9fc 35065+ goto out;
35066+ dentry = ERR_PTR(-ENOMEM);
35067+ name = kmalloc(qs.len + 1, GFP_NOFS);
35068+ if (unlikely(!name))
35069+ goto out;
35070+ }
35071+
35072+ /* doubly whiteout-ed */
35073+ memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
35074+ p = name + AUFS_WH_PFX_LEN * 2;
35075+ memcpy(p, prefix->name, prefix->len);
35076+ p += prefix->len;
35077+ *p++ = '.';
4a4d8108 35078+ AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
1facf9fc 35079+
35080+ qs.name = name;
35081+ for (i = 0; i < 3; i++) {
b752ccd1 35082+ sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
076b876e 35083+ dentry = au_sio_lkup_one(&qs, h_parent);
5527c038 35084+ if (IS_ERR(dentry) || d_is_negative(dentry))
1facf9fc 35085+ goto out_name;
35086+ dput(dentry);
35087+ }
0c3ec466 35088+ /* pr_warn("could not get random name\n"); */
1facf9fc 35089+ dentry = ERR_PTR(-EEXIST);
35090+ AuDbg("%.*s\n", AuLNPair(&qs));
35091+ BUG();
35092+
4f0767ce 35093+out_name:
1facf9fc 35094+ if (name != defname)
9f237c51 35095+ au_kfree_try_rcu(name);
4f0767ce 35096+out:
4a4d8108 35097+ AuTraceErrPtr(dentry);
1facf9fc 35098+ return dentry;
1facf9fc 35099+}
35100+
35101+/*
35102+ * rename the @h_dentry on @br to the whiteouted temporary name.
35103+ */
35104+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
35105+{
35106+ int err;
35107+ struct path h_path = {
86dc4139 35108+ .mnt = au_br_mnt(br)
1facf9fc 35109+ };
523b37e3 35110+ struct inode *h_dir, *delegated;
1facf9fc 35111+ struct dentry *h_parent;
35112+
35113+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 35114+ h_dir = d_inode(h_parent);
1facf9fc 35115+ IMustLock(h_dir);
35116+
35117+ h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
35118+ err = PTR_ERR(h_path.dentry);
35119+ if (IS_ERR(h_path.dentry))
35120+ goto out;
35121+
35122+ /* under the same dir, no need to lock_rename() */
523b37e3 35123+ delegated = NULL;
f2c43d5f
AM
35124+ err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
35125+ /*flags*/0);
1facf9fc 35126+ AuTraceErr(err);
523b37e3
AM
35127+ if (unlikely(err == -EWOULDBLOCK)) {
35128+ pr_warn("cannot retry for NFSv4 delegation"
35129+ " for an internal rename\n");
35130+ iput(delegated);
35131+ }
1facf9fc 35132+ dput(h_path.dentry);
35133+
4f0767ce 35134+out:
4a4d8108 35135+ AuTraceErr(err);
1facf9fc 35136+ return err;
35137+}
35138+
35139+/* ---------------------------------------------------------------------- */
35140+/*
35141+ * functions for removing a whiteout
35142+ */
35143+
35144+static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
35145+{
523b37e3
AM
35146+ int err, force;
35147+ struct inode *delegated;
1facf9fc 35148+
35149+ /*
35150+ * forces superio when the dir has a sticky bit.
35151+ * this may be a violation of unix fs semantics.
35152+ */
35153+ force = (h_dir->i_mode & S_ISVTX)
5527c038 35154+ && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
523b37e3
AM
35155+ delegated = NULL;
35156+ err = vfsub_unlink(h_dir, h_path, &delegated, force);
35157+ if (unlikely(err == -EWOULDBLOCK)) {
35158+ pr_warn("cannot retry for NFSv4 delegation"
35159+ " for an internal unlink\n");
35160+ iput(delegated);
35161+ }
35162+ return err;
1facf9fc 35163+}
35164+
35165+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35166+ struct dentry *dentry)
35167+{
35168+ int err;
35169+
35170+ err = do_unlink_wh(h_dir, h_path);
35171+ if (!err && dentry)
35172+ au_set_dbwh(dentry, -1);
35173+
35174+ return err;
35175+}
35176+
35177+static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
35178+ struct au_branch *br)
35179+{
35180+ int err;
35181+ struct path h_path = {
86dc4139 35182+ .mnt = au_br_mnt(br)
1facf9fc 35183+ };
35184+
35185+ err = 0;
b4510431 35186+ h_path.dentry = vfsub_lkup_one(wh, h_parent);
1facf9fc 35187+ if (IS_ERR(h_path.dentry))
35188+ err = PTR_ERR(h_path.dentry);
35189+ else {
5527c038
JR
35190+ if (d_is_reg(h_path.dentry))
35191+ err = do_unlink_wh(d_inode(h_parent), &h_path);
1facf9fc 35192+ dput(h_path.dentry);
35193+ }
35194+
35195+ return err;
35196+}
35197+
35198+/* ---------------------------------------------------------------------- */
35199+/*
35200+ * initialize/clean whiteout for a branch
35201+ */
35202+
35203+static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35204+ const int isdir)
35205+{
35206+ int err;
523b37e3 35207+ struct inode *delegated;
1facf9fc 35208+
5527c038 35209+ if (d_is_negative(whpath->dentry))
1facf9fc 35210+ return;
35211+
86dc4139
AM
35212+ if (isdir)
35213+ err = vfsub_rmdir(h_dir, whpath);
523b37e3
AM
35214+ else {
35215+ delegated = NULL;
35216+ err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35217+ if (unlikely(err == -EWOULDBLOCK)) {
35218+ pr_warn("cannot retry for NFSv4 delegation"
35219+ " for an internal unlink\n");
35220+ iput(delegated);
35221+ }
35222+ }
1facf9fc 35223+ if (unlikely(err))
523b37e3
AM
35224+ pr_warn("failed removing %pd (%d), ignored.\n",
35225+ whpath->dentry, err);
1facf9fc 35226+}
35227+
35228+static int test_linkable(struct dentry *h_root)
35229+{
5527c038 35230+ struct inode *h_dir = d_inode(h_root);
1facf9fc 35231+
35232+ if (h_dir->i_op->link)
35233+ return 0;
35234+
523b37e3
AM
35235+ pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35236+ h_root, au_sbtype(h_root->d_sb));
1facf9fc 35237+ return -ENOSYS;
35238+}
35239+
35240+/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35241+static int au_whdir(struct inode *h_dir, struct path *path)
35242+{
35243+ int err;
35244+
35245+ err = -EEXIST;
5527c038 35246+ if (d_is_negative(path->dentry)) {
cd7a4cd9 35247+ int mode = 0700;
1facf9fc 35248+
35249+ if (au_test_nfs(path->dentry->d_sb))
cd7a4cd9 35250+ mode |= 0111;
86dc4139 35251+ err = vfsub_mkdir(h_dir, path, mode);
2000de60 35252+ } else if (d_is_dir(path->dentry))
1facf9fc 35253+ err = 0;
35254+ else
523b37e3 35255+ pr_err("unknown %pd exists\n", path->dentry);
1facf9fc 35256+
35257+ return err;
35258+}
35259+
35260+struct au_wh_base {
35261+ const struct qstr *name;
35262+ struct dentry *dentry;
35263+};
35264+
35265+static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35266+ struct path *h_path)
35267+{
35268+ h_path->dentry = base[AuBrWh_BASE].dentry;
35269+ au_wh_clean(h_dir, h_path, /*isdir*/0);
35270+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35271+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35272+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35273+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35274+}
35275+
35276+/*
35277+ * returns tri-state,
c1595e42 35278+ * minus: error, caller should print the message
acd2b654 35279+ * zero: success
c1595e42 35280+ * plus: error, caller should NOT print the message
1facf9fc 35281+ */
35282+static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35283+ int do_plink, struct au_wh_base base[],
35284+ struct path *h_path)
35285+{
35286+ int err;
35287+ struct inode *h_dir;
35288+
5527c038 35289+ h_dir = d_inode(h_root);
1facf9fc 35290+ h_path->dentry = base[AuBrWh_BASE].dentry;
35291+ au_wh_clean(h_dir, h_path, /*isdir*/0);
35292+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35293+ if (do_plink) {
35294+ err = test_linkable(h_root);
35295+ if (unlikely(err)) {
35296+ err = 1;
35297+ goto out;
35298+ }
35299+
35300+ err = au_whdir(h_dir, h_path);
35301+ if (unlikely(err))
35302+ goto out;
35303+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35304+ } else
35305+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35306+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35307+ err = au_whdir(h_dir, h_path);
35308+ if (unlikely(err))
35309+ goto out;
35310+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35311+
4f0767ce 35312+out:
1facf9fc 35313+ return err;
35314+}
35315+
35316+/*
35317+ * for the moment, aufs supports the branch filesystem which does not support
35318+ * link(2). testing on FAT which does not support i_op->setattr() fully either,
35319+ * copyup failed. finally, such filesystem will not be used as the writable
35320+ * branch.
35321+ *
35322+ * returns tri-state, see above.
35323+ */
35324+static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35325+ int do_plink, struct au_wh_base base[],
35326+ struct path *h_path)
35327+{
35328+ int err;
35329+ struct inode *h_dir;
35330+
1308ab2a 35331+ WbrWhMustWriteLock(wbr);
35332+
1facf9fc 35333+ err = test_linkable(h_root);
35334+ if (unlikely(err)) {
35335+ err = 1;
35336+ goto out;
35337+ }
35338+
35339+ /*
35340+ * todo: should this create be done in /sbin/mount.aufs helper?
35341+ */
35342+ err = -EEXIST;
5527c038
JR
35343+ h_dir = d_inode(h_root);
35344+ if (d_is_negative(base[AuBrWh_BASE].dentry)) {
86dc4139
AM
35345+ h_path->dentry = base[AuBrWh_BASE].dentry;
35346+ err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
7e9cd9fe 35347+ } else if (d_is_reg(base[AuBrWh_BASE].dentry))
1facf9fc 35348+ err = 0;
35349+ else
523b37e3 35350+ pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
1facf9fc 35351+ if (unlikely(err))
35352+ goto out;
35353+
35354+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35355+ if (do_plink) {
35356+ err = au_whdir(h_dir, h_path);
35357+ if (unlikely(err))
35358+ goto out;
35359+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35360+ } else
35361+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35362+ wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35363+
35364+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35365+ err = au_whdir(h_dir, h_path);
35366+ if (unlikely(err))
35367+ goto out;
35368+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35369+
4f0767ce 35370+out:
1facf9fc 35371+ return err;
35372+}
35373+
35374+/*
35375+ * initialize the whiteout base file/dir for @br.
35376+ */
86dc4139 35377+int au_wh_init(struct au_branch *br, struct super_block *sb)
1facf9fc 35378+{
35379+ int err, i;
35380+ const unsigned char do_plink
35381+ = !!au_opt_test(au_mntflags(sb), PLINK);
1facf9fc 35382+ struct inode *h_dir;
86dc4139
AM
35383+ struct path path = br->br_path;
35384+ struct dentry *h_root = path.dentry;
1facf9fc 35385+ struct au_wbr *wbr = br->br_wbr;
35386+ static const struct qstr base_name[] = {
0c3ec466
AM
35387+ [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35388+ sizeof(AUFS_BASE_NAME) - 1),
35389+ [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35390+ sizeof(AUFS_PLINKDIR_NAME) - 1),
35391+ [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35392+ sizeof(AUFS_ORPHDIR_NAME) - 1)
1facf9fc 35393+ };
35394+ struct au_wh_base base[] = {
35395+ [AuBrWh_BASE] = {
35396+ .name = base_name + AuBrWh_BASE,
35397+ .dentry = NULL
35398+ },
35399+ [AuBrWh_PLINK] = {
35400+ .name = base_name + AuBrWh_PLINK,
35401+ .dentry = NULL
35402+ },
35403+ [AuBrWh_ORPH] = {
35404+ .name = base_name + AuBrWh_ORPH,
35405+ .dentry = NULL
35406+ }
35407+ };
35408+
1308ab2a 35409+ if (wbr)
35410+ WbrWhMustWriteLock(wbr);
1facf9fc 35411+
1facf9fc 35412+ for (i = 0; i < AuBrWh_Last; i++) {
35413+ /* doubly whiteouted */
35414+ struct dentry *d;
35415+
35416+ d = au_wh_lkup(h_root, (void *)base[i].name, br);
35417+ err = PTR_ERR(d);
35418+ if (IS_ERR(d))
35419+ goto out;
35420+
35421+ base[i].dentry = d;
35422+ AuDebugOn(wbr
35423+ && wbr->wbr_wh[i]
35424+ && wbr->wbr_wh[i] != base[i].dentry);
35425+ }
35426+
35427+ if (wbr)
35428+ for (i = 0; i < AuBrWh_Last; i++) {
35429+ dput(wbr->wbr_wh[i]);
35430+ wbr->wbr_wh[i] = NULL;
35431+ }
35432+
35433+ err = 0;
1e00d052 35434+ if (!au_br_writable(br->br_perm)) {
5527c038 35435+ h_dir = d_inode(h_root);
1facf9fc 35436+ au_wh_init_ro(h_dir, base, &path);
1e00d052 35437+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 35438+ err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35439+ if (err > 0)
35440+ goto out;
35441+ else if (err)
35442+ goto out_err;
1e00d052 35443+ } else {
1facf9fc 35444+ err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35445+ if (err > 0)
35446+ goto out;
35447+ else if (err)
35448+ goto out_err;
1facf9fc 35449+ }
35450+ goto out; /* success */
35451+
4f0767ce 35452+out_err:
523b37e3
AM
35453+ pr_err("an error(%d) on the writable branch %pd(%s)\n",
35454+ err, h_root, au_sbtype(h_root->d_sb));
4f0767ce 35455+out:
1facf9fc 35456+ for (i = 0; i < AuBrWh_Last; i++)
35457+ dput(base[i].dentry);
35458+ return err;
35459+}
35460+
35461+/* ---------------------------------------------------------------------- */
35462+/*
35463+ * whiteouts are all hard-linked usually.
35464+ * when its link count reaches a ceiling, we create a new whiteout base
35465+ * asynchronously.
35466+ */
35467+
35468+struct reinit_br_wh {
35469+ struct super_block *sb;
35470+ struct au_branch *br;
35471+};
35472+
35473+static void reinit_br_wh(void *arg)
35474+{
35475+ int err;
35476+ aufs_bindex_t bindex;
35477+ struct path h_path;
35478+ struct reinit_br_wh *a = arg;
35479+ struct au_wbr *wbr;
523b37e3 35480+ struct inode *dir, *delegated;
1facf9fc 35481+ struct dentry *h_root;
35482+ struct au_hinode *hdir;
35483+
35484+ err = 0;
35485+ wbr = a->br->br_wbr;
35486+ /* big aufs lock */
35487+ si_noflush_write_lock(a->sb);
35488+ if (!au_br_writable(a->br->br_perm))
35489+ goto out;
35490+ bindex = au_br_index(a->sb, a->br->br_id);
35491+ if (unlikely(bindex < 0))
35492+ goto out;
35493+
1308ab2a 35494+ di_read_lock_parent(a->sb->s_root, AuLock_IR);
5527c038 35495+ dir = d_inode(a->sb->s_root);
1facf9fc 35496+ hdir = au_hi(dir, bindex);
35497+ h_root = au_h_dptr(a->sb->s_root, bindex);
86dc4139 35498+ AuDebugOn(h_root != au_br_dentry(a->br));
1facf9fc 35499+
5afbbe0d 35500+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 35501+ wbr_wh_write_lock(wbr);
35502+ err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35503+ h_root, a->br);
35504+ if (!err) {
86dc4139
AM
35505+ h_path.dentry = wbr->wbr_whbase;
35506+ h_path.mnt = au_br_mnt(a->br);
523b37e3
AM
35507+ delegated = NULL;
35508+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35509+ /*force*/0);
35510+ if (unlikely(err == -EWOULDBLOCK)) {
35511+ pr_warn("cannot retry for NFSv4 delegation"
35512+ " for an internal unlink\n");
35513+ iput(delegated);
35514+ }
1facf9fc 35515+ } else {
523b37e3 35516+ pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
1facf9fc 35517+ err = 0;
35518+ }
35519+ dput(wbr->wbr_whbase);
35520+ wbr->wbr_whbase = NULL;
35521+ if (!err)
86dc4139 35522+ err = au_wh_init(a->br, a->sb);
1facf9fc 35523+ wbr_wh_write_unlock(wbr);
5afbbe0d 35524+ au_hn_inode_unlock(hdir);
1308ab2a 35525+ di_read_unlock(a->sb->s_root, AuLock_IR);
076b876e
AM
35526+ if (!err)
35527+ au_fhsm_wrote(a->sb, bindex, /*force*/0);
1facf9fc 35528+
4f0767ce 35529+out:
1facf9fc 35530+ if (wbr)
35531+ atomic_dec(&wbr->wbr_wh_running);
acd2b654 35532+ au_lcnt_dec(&a->br->br_count);
1facf9fc 35533+ si_write_unlock(a->sb);
027c5e7a 35534+ au_nwt_done(&au_sbi(a->sb)->si_nowait);
9f237c51 35535+ au_kfree_rcu(a);
1facf9fc 35536+ if (unlikely(err))
35537+ AuIOErr("err %d\n", err);
35538+}
35539+
35540+static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35541+{
35542+ int do_dec, wkq_err;
35543+ struct reinit_br_wh *arg;
35544+
35545+ do_dec = 1;
35546+ if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35547+ goto out;
35548+
35549+ /* ignore ENOMEM */
35550+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
35551+ if (arg) {
35552+ /*
35553+ * dec(wh_running), kfree(arg) and dec(br_count)
35554+ * in reinit function
35555+ */
35556+ arg->sb = sb;
35557+ arg->br = br;
acd2b654 35558+ au_lcnt_inc(&br->br_count);
53392da6 35559+ wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
1facf9fc 35560+ if (unlikely(wkq_err)) {
35561+ atomic_dec(&br->br_wbr->wbr_wh_running);
acd2b654 35562+ au_lcnt_dec(&br->br_count);
9f237c51 35563+ au_kfree_rcu(arg);
1facf9fc 35564+ }
35565+ do_dec = 0;
35566+ }
35567+
4f0767ce 35568+out:
1facf9fc 35569+ if (do_dec)
35570+ atomic_dec(&br->br_wbr->wbr_wh_running);
35571+}
35572+
35573+/* ---------------------------------------------------------------------- */
35574+
35575+/*
35576+ * create the whiteout @wh.
35577+ */
35578+static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35579+ struct dentry *wh)
35580+{
35581+ int err;
35582+ struct path h_path = {
35583+ .dentry = wh
35584+ };
35585+ struct au_branch *br;
35586+ struct au_wbr *wbr;
35587+ struct dentry *h_parent;
523b37e3 35588+ struct inode *h_dir, *delegated;
1facf9fc 35589+
35590+ h_parent = wh->d_parent; /* dir inode is locked */
5527c038 35591+ h_dir = d_inode(h_parent);
1facf9fc 35592+ IMustLock(h_dir);
35593+
35594+ br = au_sbr(sb, bindex);
86dc4139 35595+ h_path.mnt = au_br_mnt(br);
1facf9fc 35596+ wbr = br->br_wbr;
35597+ wbr_wh_read_lock(wbr);
35598+ if (wbr->wbr_whbase) {
523b37e3
AM
35599+ delegated = NULL;
35600+ err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35601+ if (unlikely(err == -EWOULDBLOCK)) {
35602+ pr_warn("cannot retry for NFSv4 delegation"
35603+ " for an internal link\n");
35604+ iput(delegated);
35605+ }
1facf9fc 35606+ if (!err || err != -EMLINK)
35607+ goto out;
35608+
35609+ /* link count full. re-initialize br_whbase. */
35610+ kick_reinit_br_wh(sb, br);
35611+ }
35612+
35613+ /* return this error in this context */
b4510431 35614+ err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
076b876e
AM
35615+ if (!err)
35616+ au_fhsm_wrote(sb, bindex, /*force*/0);
1facf9fc 35617+
4f0767ce 35618+out:
1facf9fc 35619+ wbr_wh_read_unlock(wbr);
35620+ return err;
35621+}
35622+
35623+/* ---------------------------------------------------------------------- */
35624+
35625+/*
35626+ * create or remove the diropq.
35627+ */
35628+static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35629+ unsigned int flags)
35630+{
35631+ struct dentry *opq_dentry, *h_dentry;
35632+ struct super_block *sb;
35633+ struct au_branch *br;
35634+ int err;
35635+
35636+ sb = dentry->d_sb;
35637+ br = au_sbr(sb, bindex);
35638+ h_dentry = au_h_dptr(dentry, bindex);
b4510431 35639+ opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
1facf9fc 35640+ if (IS_ERR(opq_dentry))
35641+ goto out;
35642+
35643+ if (au_ftest_diropq(flags, CREATE)) {
35644+ err = link_or_create_wh(sb, bindex, opq_dentry);
35645+ if (!err) {
35646+ au_set_dbdiropq(dentry, bindex);
35647+ goto out; /* success */
35648+ }
35649+ } else {
35650+ struct path tmp = {
35651+ .dentry = opq_dentry,
86dc4139 35652+ .mnt = au_br_mnt(br)
1facf9fc 35653+ };
5527c038 35654+ err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
1facf9fc 35655+ if (!err)
35656+ au_set_dbdiropq(dentry, -1);
35657+ }
35658+ dput(opq_dentry);
35659+ opq_dentry = ERR_PTR(err);
35660+
4f0767ce 35661+out:
1facf9fc 35662+ return opq_dentry;
35663+}
35664+
35665+struct do_diropq_args {
35666+ struct dentry **errp;
35667+ struct dentry *dentry;
35668+ aufs_bindex_t bindex;
35669+ unsigned int flags;
35670+};
35671+
35672+static void call_do_diropq(void *args)
35673+{
35674+ struct do_diropq_args *a = args;
35675+ *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35676+}
35677+
35678+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35679+ unsigned int flags)
35680+{
35681+ struct dentry *diropq, *h_dentry;
35682+
35683+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 35684+ if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
1facf9fc 35685+ diropq = do_diropq(dentry, bindex, flags);
35686+ else {
35687+ int wkq_err;
35688+ struct do_diropq_args args = {
35689+ .errp = &diropq,
35690+ .dentry = dentry,
35691+ .bindex = bindex,
35692+ .flags = flags
35693+ };
35694+
35695+ wkq_err = au_wkq_wait(call_do_diropq, &args);
35696+ if (unlikely(wkq_err))
35697+ diropq = ERR_PTR(wkq_err);
35698+ }
35699+
35700+ return diropq;
35701+}
35702+
35703+/* ---------------------------------------------------------------------- */
35704+
35705+/*
35706+ * lookup whiteout dentry.
35707+ * @h_parent: lower parent dentry which must exist and be locked
35708+ * @base_name: name of dentry which will be whiteouted
35709+ * returns dentry for whiteout.
35710+ */
35711+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35712+ struct au_branch *br)
35713+{
35714+ int err;
35715+ struct qstr wh_name;
35716+ struct dentry *wh_dentry;
35717+
35718+ err = au_wh_name_alloc(&wh_name, base_name);
35719+ wh_dentry = ERR_PTR(err);
35720+ if (!err) {
b4510431 35721+ wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
9f237c51 35722+ au_kfree_try_rcu(wh_name.name);
1facf9fc 35723+ }
35724+ return wh_dentry;
35725+}
35726+
35727+/*
35728+ * link/create a whiteout for @dentry on @bindex.
35729+ */
35730+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35731+ struct dentry *h_parent)
35732+{
35733+ struct dentry *wh_dentry;
35734+ struct super_block *sb;
35735+ int err;
35736+
35737+ sb = dentry->d_sb;
35738+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
5527c038 35739+ if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
1facf9fc 35740+ err = link_or_create_wh(sb, bindex, wh_dentry);
076b876e 35741+ if (!err) {
1facf9fc 35742+ au_set_dbwh(dentry, bindex);
076b876e
AM
35743+ au_fhsm_wrote(sb, bindex, /*force*/0);
35744+ } else {
1facf9fc 35745+ dput(wh_dentry);
35746+ wh_dentry = ERR_PTR(err);
35747+ }
35748+ }
35749+
35750+ return wh_dentry;
35751+}
35752+
35753+/* ---------------------------------------------------------------------- */
35754+
35755+/* Delete all whiteouts in this directory on branch bindex. */
35756+static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
35757+ aufs_bindex_t bindex, struct au_branch *br)
35758+{
35759+ int err;
35760+ unsigned long ul, n;
35761+ struct qstr wh_name;
35762+ char *p;
35763+ struct hlist_head *head;
c06a8ce3 35764+ struct au_vdir_wh *pos;
1facf9fc 35765+ struct au_vdir_destr *str;
35766+
35767+ err = -ENOMEM;
537831f9 35768+ p = (void *)__get_free_page(GFP_NOFS);
1facf9fc 35769+ wh_name.name = p;
35770+ if (unlikely(!wh_name.name))
35771+ goto out;
35772+
35773+ err = 0;
35774+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35775+ p += AUFS_WH_PFX_LEN;
35776+ n = whlist->nh_num;
35777+ head = whlist->nh_head;
35778+ for (ul = 0; !err && ul < n; ul++, head++) {
c06a8ce3
AM
35779+ hlist_for_each_entry(pos, head, wh_hash) {
35780+ if (pos->wh_bindex != bindex)
1facf9fc 35781+ continue;
35782+
c06a8ce3 35783+ str = &pos->wh_str;
1facf9fc 35784+ if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35785+ memcpy(p, str->name, str->len);
35786+ wh_name.len = AUFS_WH_PFX_LEN + str->len;
35787+ err = unlink_wh_name(h_dentry, &wh_name, br);
35788+ if (!err)
35789+ continue;
35790+ break;
35791+ }
35792+ AuIOErr("whiteout name too long %.*s\n",
35793+ str->len, str->name);
35794+ err = -EIO;
35795+ break;
35796+ }
35797+ }
1c60b727 35798+ free_page((unsigned long)wh_name.name);
1facf9fc 35799+
4f0767ce 35800+out:
1facf9fc 35801+ return err;
35802+}
35803+
35804+struct del_wh_children_args {
35805+ int *errp;
35806+ struct dentry *h_dentry;
1308ab2a 35807+ struct au_nhash *whlist;
1facf9fc 35808+ aufs_bindex_t bindex;
35809+ struct au_branch *br;
35810+};
35811+
35812+static void call_del_wh_children(void *args)
35813+{
35814+ struct del_wh_children_args *a = args;
1308ab2a 35815+ *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
1facf9fc 35816+}
35817+
35818+/* ---------------------------------------------------------------------- */
35819+
35820+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35821+{
35822+ struct au_whtmp_rmdir *whtmp;
dece6358 35823+ int err;
1308ab2a 35824+ unsigned int rdhash;
dece6358
AM
35825+
35826+ SiMustAnyLock(sb);
1facf9fc 35827+
be52b249 35828+ whtmp = kzalloc(sizeof(*whtmp), gfp);
dece6358
AM
35829+ if (unlikely(!whtmp)) {
35830+ whtmp = ERR_PTR(-ENOMEM);
1facf9fc 35831+ goto out;
dece6358 35832+ }
1facf9fc 35833+
1308ab2a 35834+ /* no estimation for dir size */
35835+ rdhash = au_sbi(sb)->si_rdhash;
35836+ if (!rdhash)
35837+ rdhash = AUFS_RDHASH_DEF;
35838+ err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35839+ if (unlikely(err)) {
9f237c51 35840+ au_kfree_rcu(whtmp);
1308ab2a 35841+ whtmp = ERR_PTR(err);
35842+ }
dece6358 35843+
4f0767ce 35844+out:
dece6358 35845+ return whtmp;
1facf9fc 35846+}
35847+
35848+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35849+{
027c5e7a 35850+ if (whtmp->br)
acd2b654 35851+ au_lcnt_dec(&whtmp->br->br_count);
1facf9fc 35852+ dput(whtmp->wh_dentry);
35853+ iput(whtmp->dir);
dece6358 35854+ au_nhash_wh_free(&whtmp->whlist);
9f237c51 35855+ au_kfree_rcu(whtmp);
1facf9fc 35856+}
35857+
35858+/*
35859+ * rmdir the whiteouted temporary named dir @h_dentry.
35860+ * @whlist: whiteouted children.
35861+ */
35862+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35863+ struct dentry *wh_dentry, struct au_nhash *whlist)
35864+{
35865+ int err;
2000de60 35866+ unsigned int h_nlink;
1facf9fc 35867+ struct path h_tmp;
35868+ struct inode *wh_inode, *h_dir;
35869+ struct au_branch *br;
35870+
5527c038 35871+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
1facf9fc 35872+ IMustLock(h_dir);
35873+
35874+ br = au_sbr(dir->i_sb, bindex);
5527c038 35875+ wh_inode = d_inode(wh_dentry);
febd17d6 35876+ inode_lock_nested(wh_inode, AuLsc_I_CHILD);
1facf9fc 35877+
35878+ /*
35879+ * someone else might change some whiteouts while we were sleeping.
35880+ * it means this whlist may have an obsoleted entry.
35881+ */
35882+ if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
35883+ err = del_wh_children(wh_dentry, whlist, bindex, br);
35884+ else {
35885+ int wkq_err;
35886+ struct del_wh_children_args args = {
35887+ .errp = &err,
35888+ .h_dentry = wh_dentry,
1308ab2a 35889+ .whlist = whlist,
1facf9fc 35890+ .bindex = bindex,
35891+ .br = br
35892+ };
35893+
35894+ wkq_err = au_wkq_wait(call_del_wh_children, &args);
35895+ if (unlikely(wkq_err))
35896+ err = wkq_err;
35897+ }
febd17d6 35898+ inode_unlock(wh_inode);
1facf9fc 35899+
35900+ if (!err) {
35901+ h_tmp.dentry = wh_dentry;
86dc4139 35902+ h_tmp.mnt = au_br_mnt(br);
2000de60 35903+ h_nlink = h_dir->i_nlink;
1facf9fc 35904+ err = vfsub_rmdir(h_dir, &h_tmp);
2000de60
JR
35905+ /* some fs doesn't change the parent nlink in some cases */
35906+ h_nlink -= h_dir->i_nlink;
1facf9fc 35907+ }
35908+
35909+ if (!err) {
5afbbe0d 35910+ if (au_ibtop(dir) == bindex) {
7f207e10 35911+ /* todo: dir->i_mutex is necessary */
1facf9fc 35912+ au_cpup_attr_timesizes(dir);
2000de60
JR
35913+ if (h_nlink)
35914+ vfsub_drop_nlink(dir);
1facf9fc 35915+ }
35916+ return 0; /* success */
35917+ }
35918+
523b37e3 35919+ pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
1facf9fc 35920+ return err;
35921+}
35922+
35923+static void call_rmdir_whtmp(void *args)
35924+{
35925+ int err;
e49829fe 35926+ aufs_bindex_t bindex;
1facf9fc 35927+ struct au_whtmp_rmdir *a = args;
35928+ struct super_block *sb;
35929+ struct dentry *h_parent;
35930+ struct inode *h_dir;
1facf9fc 35931+ struct au_hinode *hdir;
35932+
35933+ /* rmdir by nfsd may cause deadlock with this i_mutex */
febd17d6 35934+ /* inode_lock(a->dir); */
e49829fe 35935+ err = -EROFS;
1facf9fc 35936+ sb = a->dir->i_sb;
e49829fe
JR
35937+ si_read_lock(sb, !AuLock_FLUSH);
35938+ if (!au_br_writable(a->br->br_perm))
35939+ goto out;
35940+ bindex = au_br_index(sb, a->br->br_id);
35941+ if (unlikely(bindex < 0))
1facf9fc 35942+ goto out;
35943+
35944+ err = -EIO;
1facf9fc 35945+ ii_write_lock_parent(a->dir);
35946+ h_parent = dget_parent(a->wh_dentry);
5527c038 35947+ h_dir = d_inode(h_parent);
e49829fe 35948+ hdir = au_hi(a->dir, bindex);
86dc4139
AM
35949+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
35950+ if (unlikely(err))
35951+ goto out_mnt;
5afbbe0d 35952+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
e49829fe
JR
35953+ err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
35954+ a->br);
86dc4139
AM
35955+ if (!err)
35956+ err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
5afbbe0d 35957+ au_hn_inode_unlock(hdir);
86dc4139
AM
35958+ vfsub_mnt_drop_write(au_br_mnt(a->br));
35959+
35960+out_mnt:
1facf9fc 35961+ dput(h_parent);
35962+ ii_write_unlock(a->dir);
4f0767ce 35963+out:
febd17d6 35964+ /* inode_unlock(a->dir); */
1facf9fc 35965+ au_whtmp_rmdir_free(a);
027c5e7a
AM
35966+ si_read_unlock(sb);
35967+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 35968+ if (unlikely(err))
35969+ AuIOErr("err %d\n", err);
35970+}
35971+
35972+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35973+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
35974+{
35975+ int wkq_err;
e49829fe 35976+ struct super_block *sb;
1facf9fc 35977+
35978+ IMustLock(dir);
35979+
35980+ /* all post-process will be done in do_rmdir_whtmp(). */
e49829fe 35981+ sb = dir->i_sb;
1facf9fc 35982+ args->dir = au_igrab(dir);
e49829fe 35983+ args->br = au_sbr(sb, bindex);
acd2b654 35984+ au_lcnt_inc(&args->br->br_count);
1facf9fc 35985+ args->wh_dentry = dget(wh_dentry);
53392da6 35986+ wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
1facf9fc 35987+ if (unlikely(wkq_err)) {
523b37e3 35988+ pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
1facf9fc 35989+ au_whtmp_rmdir_free(args);
35990+ }
35991+}
7f207e10
AM
35992diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
35993--- /usr/share/empty/fs/aufs/whout.h 1970-01-01 01:00:00.000000000 +0100
ba1aed25 35994+++ linux/fs/aufs/whout.h 2019-03-05 12:13:00.145891204 +0100
062440b3
AM
35995@@ -0,0 +1,86 @@
35996+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 35997+/*
ba1aed25 35998+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 35999+ *
36000+ * This program, aufs is free software; you can redistribute it and/or modify
36001+ * it under the terms of the GNU General Public License as published by
36002+ * the Free Software Foundation; either version 2 of the License, or
36003+ * (at your option) any later version.
dece6358
AM
36004+ *
36005+ * This program is distributed in the hope that it will be useful,
36006+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36007+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36008+ * GNU General Public License for more details.
36009+ *
36010+ * You should have received a copy of the GNU General Public License
523b37e3 36011+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36012+ */
36013+
36014+/*
36015+ * whiteout for logical deletion and opaque directory
36016+ */
36017+
36018+#ifndef __AUFS_WHOUT_H__
36019+#define __AUFS_WHOUT_H__
36020+
36021+#ifdef __KERNEL__
36022+
1facf9fc 36023+#include "dir.h"
36024+
36025+/* whout.c */
36026+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
076b876e
AM
36027+int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
36028+int au_diropq_test(struct dentry *h_dentry);
3c1bdaff 36029+struct au_branch;
1facf9fc 36030+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
36031+ struct qstr *prefix);
36032+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
36033+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
36034+ struct dentry *dentry);
86dc4139 36035+int au_wh_init(struct au_branch *br, struct super_block *sb);
1facf9fc 36036+
36037+/* diropq flags */
36038+#define AuDiropq_CREATE 1
36039+#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
7f207e10
AM
36040+#define au_fset_diropq(flags, name) \
36041+ do { (flags) |= AuDiropq_##name; } while (0)
36042+#define au_fclr_diropq(flags, name) \
36043+ do { (flags) &= ~AuDiropq_##name; } while (0)
1facf9fc 36044+
36045+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
36046+ unsigned int flags);
36047+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
36048+ struct au_branch *br);
36049+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
36050+ struct dentry *h_parent);
36051+
36052+/* real rmdir for the whiteout-ed dir */
36053+struct au_whtmp_rmdir {
36054+ struct inode *dir;
e49829fe 36055+ struct au_branch *br;
1facf9fc 36056+ struct dentry *wh_dentry;
dece6358 36057+ struct au_nhash whlist;
1facf9fc 36058+};
36059+
36060+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
36061+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
36062+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
36063+ struct dentry *wh_dentry, struct au_nhash *whlist);
36064+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36065+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
36066+
36067+/* ---------------------------------------------------------------------- */
36068+
36069+static inline struct dentry *au_diropq_create(struct dentry *dentry,
36070+ aufs_bindex_t bindex)
36071+{
36072+ return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
36073+}
36074+
36075+static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
36076+{
36077+ return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
36078+}
36079+
36080+#endif /* __KERNEL__ */
36081+#endif /* __AUFS_WHOUT_H__ */
7f207e10
AM
36082diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
36083--- /usr/share/empty/fs/aufs/wkq.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 36084+++ linux/fs/aufs/wkq.c 2019-03-05 12:13:00.145891204 +0100
acd2b654 36085@@ -0,0 +1,392 @@
cd7a4cd9 36086+// SPDX-License-Identifier: GPL-2.0
1facf9fc 36087+/*
ba1aed25 36088+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 36089+ *
36090+ * This program, aufs is free software; you can redistribute it and/or modify
36091+ * it under the terms of the GNU General Public License as published by
36092+ * the Free Software Foundation; either version 2 of the License, or
36093+ * (at your option) any later version.
dece6358
AM
36094+ *
36095+ * This program is distributed in the hope that it will be useful,
36096+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36097+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36098+ * GNU General Public License for more details.
36099+ *
36100+ * You should have received a copy of the GNU General Public License
523b37e3 36101+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36102+ */
36103+
36104+/*
36105+ * workqueue for asynchronous/super-io operations
acd2b654 36106+ * todo: try new credential scheme
1facf9fc 36107+ */
36108+
dece6358 36109+#include <linux/module.h>
1facf9fc 36110+#include "aufs.h"
36111+
9dbd164d 36112+/* internal workqueue named AUFS_WKQ_NAME */
b752ccd1 36113+
9dbd164d 36114+static struct workqueue_struct *au_wkq;
1facf9fc 36115+
36116+struct au_wkinfo {
36117+ struct work_struct wk;
7f207e10 36118+ struct kobject *kobj;
1facf9fc 36119+
36120+ unsigned int flags; /* see wkq.h */
36121+
36122+ au_wkq_func_t func;
36123+ void *args;
36124+
8b6a4947
AM
36125+#ifdef CONFIG_LOCKDEP
36126+ int dont_check;
36127+ struct held_lock **hlock;
36128+#endif
36129+
1facf9fc 36130+ struct completion *comp;
36131+};
36132+
36133+/* ---------------------------------------------------------------------- */
8b6a4947
AM
36134+/*
36135+ * Aufs passes some operations to the workqueue such as the internal copyup.
36136+ * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
36137+ * job run by workqueue depends upon the locks acquired in the other task.
36138+ * Delegating a small operation to the workqueue, aufs passes its lockdep
36139+ * information too. And the job in the workqueue restores the info in order to
36140+ * pretend as if it acquired those locks. This is just to make LOCKDEP work
36141+ * correctly and expectedly.
36142+ */
36143+
36144+#ifndef CONFIG_LOCKDEP
36145+AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
36146+AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
36147+AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
36148+AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
36149+AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
36150+#else
36151+static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
36152+{
36153+ wkinfo->hlock = NULL;
36154+ wkinfo->dont_check = 0;
36155+}
36156+
36157+/*
36158+ * 1: matched
36159+ * 0: unmatched
36160+ */
36161+static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
36162+{
36163+ static DEFINE_SPINLOCK(spin);
36164+ static struct {
36165+ char *name;
36166+ struct lock_class_key *key;
36167+ } a[] = {
36168+ { .name = "&sbinfo->si_rwsem" },
36169+ { .name = "&finfo->fi_rwsem" },
36170+ { .name = "&dinfo->di_rwsem" },
36171+ { .name = "&iinfo->ii_rwsem" }
36172+ };
36173+ static int set;
36174+ int i;
36175+
36176+ /* lockless read from 'set.' see below */
36177+ if (set == ARRAY_SIZE(a)) {
36178+ for (i = 0; i < ARRAY_SIZE(a); i++)
36179+ if (a[i].key == key)
36180+ goto match;
36181+ goto unmatch;
36182+ }
36183+
36184+ spin_lock(&spin);
36185+ if (set)
36186+ for (i = 0; i < ARRAY_SIZE(a); i++)
36187+ if (a[i].key == key) {
36188+ spin_unlock(&spin);
36189+ goto match;
36190+ }
36191+ for (i = 0; i < ARRAY_SIZE(a); i++) {
36192+ if (a[i].key) {
36193+ if (unlikely(a[i].key == key)) { /* rare but possible */
36194+ spin_unlock(&spin);
36195+ goto match;
36196+ } else
36197+ continue;
36198+ }
36199+ if (strstr(a[i].name, name)) {
36200+ /*
36201+ * the order of these three lines is important for the
36202+ * lockless read above.
36203+ */
36204+ a[i].key = key;
36205+ spin_unlock(&spin);
36206+ set++;
36207+ /* AuDbg("%d, %s\n", set, name); */
36208+ goto match;
36209+ }
36210+ }
36211+ spin_unlock(&spin);
36212+ goto unmatch;
36213+
36214+match:
36215+ return 1;
36216+unmatch:
36217+ return 0;
36218+}
36219+
36220+static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36221+{
36222+ int err, n;
36223+ struct task_struct *curr;
36224+ struct held_lock **hl, *held_locks, *p;
36225+
36226+ err = 0;
36227+ curr = current;
36228+ wkinfo->dont_check = lockdep_recursing(curr);
36229+ if (wkinfo->dont_check)
36230+ goto out;
36231+ n = curr->lockdep_depth;
36232+ if (!n)
36233+ goto out;
36234+
36235+ err = -ENOMEM;
36236+ wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36237+ if (unlikely(!wkinfo->hlock))
36238+ goto out;
36239+
36240+ err = 0;
36241+#if 0
36242+ if (0 && au_debug_test()) /* left for debugging */
36243+ lockdep_print_held_locks(curr);
36244+#endif
36245+ held_locks = curr->held_locks;
36246+ hl = wkinfo->hlock;
36247+ while (n--) {
36248+ p = held_locks++;
36249+ if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36250+ *hl++ = p;
36251+ }
36252+ *hl = NULL;
36253+
36254+out:
36255+ return err;
36256+}
36257+
36258+static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36259+{
9f237c51 36260+ au_kfree_try_rcu(wkinfo->hlock);
8b6a4947
AM
36261+}
36262+
36263+static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36264+{
36265+ struct held_lock *p, **hl = wkinfo->hlock;
36266+ int subclass;
36267+
36268+ if (wkinfo->dont_check)
36269+ lockdep_off();
36270+ if (!hl)
36271+ return;
36272+ while ((p = *hl++)) { /* assignment */
36273+ subclass = lockdep_hlock_class(p)->subclass;
36274+ /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36275+ if (p->read)
36276+ rwsem_acquire_read(p->instance, subclass, 0,
36277+ /*p->acquire_ip*/_RET_IP_);
36278+ else
36279+ rwsem_acquire(p->instance, subclass, 0,
36280+ /*p->acquire_ip*/_RET_IP_);
36281+ }
36282+}
36283+
36284+static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36285+{
36286+ struct held_lock *p, **hl = wkinfo->hlock;
36287+
36288+ if (wkinfo->dont_check)
36289+ lockdep_on();
36290+ if (!hl)
36291+ return;
36292+ while ((p = *hl++)) /* assignment */
36293+ rwsem_release(p->instance, 0, /*p->acquire_ip*/_RET_IP_);
36294+}
36295+#endif
1facf9fc 36296+
1facf9fc 36297+static void wkq_func(struct work_struct *wk)
36298+{
36299+ struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36300+
2dfbb274 36301+ AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
7f207e10
AM
36302+ AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36303+
8b6a4947 36304+ au_wkq_lockdep_pre(wkinfo);
1facf9fc 36305+ wkinfo->func(wkinfo->args);
8b6a4947 36306+ au_wkq_lockdep_post(wkinfo);
1facf9fc 36307+ if (au_ftest_wkq(wkinfo->flags, WAIT))
36308+ complete(wkinfo->comp);
36309+ else {
7f207e10 36310+ kobject_put(wkinfo->kobj);
9dbd164d 36311+ module_put(THIS_MODULE); /* todo: ?? */
9f237c51 36312+ au_kfree_rcu(wkinfo);
1facf9fc 36313+ }
36314+}
36315+
36316+/*
36317+ * Since struct completion is large, try allocating it dynamically.
36318+ */
c2b27bf2 36319+#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
1facf9fc 36320+#define AuWkqCompDeclare(name) struct completion *comp = NULL
36321+
36322+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36323+{
36324+ *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36325+ if (*comp) {
36326+ init_completion(*comp);
36327+ wkinfo->comp = *comp;
36328+ return 0;
36329+ }
36330+ return -ENOMEM;
36331+}
36332+
36333+static void au_wkq_comp_free(struct completion *comp)
36334+{
9f237c51 36335+ au_kfree_rcu(comp);
1facf9fc 36336+}
36337+
36338+#else
36339+
36340+/* no braces */
36341+#define AuWkqCompDeclare(name) \
36342+ DECLARE_COMPLETION_ONSTACK(_ ## name); \
36343+ struct completion *comp = &_ ## name
36344+
36345+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36346+{
36347+ wkinfo->comp = *comp;
36348+ return 0;
36349+}
36350+
36351+static void au_wkq_comp_free(struct completion *comp __maybe_unused)
36352+{
36353+ /* empty */
36354+}
36355+#endif /* 4KSTACKS */
36356+
53392da6 36357+static void au_wkq_run(struct au_wkinfo *wkinfo)
1facf9fc 36358+{
53392da6
AM
36359+ if (au_ftest_wkq(wkinfo->flags, NEST)) {
36360+ if (au_wkq_test()) {
38d290e6 36361+ AuWarn1("wkq from wkq, unless silly-rename on NFS,"
acd2b654
AM
36362+ " due to a dead dir by UDBA,"
36363+ " or async xino write?\n");
53392da6
AM
36364+ AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36365+ }
36366+ } else
36367+ au_dbg_verify_kthread();
36368+
36369+ if (au_ftest_wkq(wkinfo->flags, WAIT)) {
a1f66529 36370+ INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
9dbd164d 36371+ queue_work(au_wkq, &wkinfo->wk);
4a4d8108
AM
36372+ } else {
36373+ INIT_WORK(&wkinfo->wk, wkq_func);
36374+ schedule_work(&wkinfo->wk);
36375+ }
1facf9fc 36376+}
36377+
7f207e10
AM
36378+/*
36379+ * Be careful. It is easy to make deadlock happen.
36380+ * processA: lock, wkq and wait
36381+ * processB: wkq and wait, lock in wkq
36382+ * --> deadlock
36383+ */
b752ccd1 36384+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
1facf9fc 36385+{
36386+ int err;
36387+ AuWkqCompDeclare(comp);
36388+ struct au_wkinfo wkinfo = {
b752ccd1 36389+ .flags = flags,
1facf9fc 36390+ .func = func,
36391+ .args = args
36392+ };
36393+
36394+ err = au_wkq_comp_alloc(&wkinfo, &comp);
8b6a4947
AM
36395+ if (unlikely(err))
36396+ goto out;
36397+ err = au_wkq_lockdep_alloc(&wkinfo);
36398+ if (unlikely(err))
36399+ goto out_comp;
1facf9fc 36400+ if (!err) {
53392da6 36401+ au_wkq_run(&wkinfo);
1facf9fc 36402+ /* no timeout, no interrupt */
36403+ wait_for_completion(wkinfo.comp);
1facf9fc 36404+ }
8b6a4947 36405+ au_wkq_lockdep_free(&wkinfo);
1facf9fc 36406+
8b6a4947
AM
36407+out_comp:
36408+ au_wkq_comp_free(comp);
36409+out:
36410+ destroy_work_on_stack(&wkinfo.wk);
1facf9fc 36411+ return err;
1facf9fc 36412+}
36413+
027c5e7a
AM
36414+/*
36415+ * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36416+ * problem in a concurrent umounting.
36417+ */
53392da6
AM
36418+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36419+ unsigned int flags)
1facf9fc 36420+{
36421+ int err;
36422+ struct au_wkinfo *wkinfo;
36423+
f0c0a007 36424+ atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
1facf9fc 36425+
36426+ /*
36427+ * wkq_func() must free this wkinfo.
36428+ * it highly depends upon the implementation of workqueue.
36429+ */
36430+ err = 0;
36431+ wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36432+ if (wkinfo) {
7f207e10 36433+ wkinfo->kobj = &au_sbi(sb)->si_kobj;
53392da6 36434+ wkinfo->flags = flags & ~AuWkq_WAIT;
1facf9fc 36435+ wkinfo->func = func;
36436+ wkinfo->args = args;
36437+ wkinfo->comp = NULL;
8b6a4947 36438+ au_wkq_lockdep_init(wkinfo);
7f207e10 36439+ kobject_get(wkinfo->kobj);
9dbd164d 36440+ __module_get(THIS_MODULE); /* todo: ?? */
1facf9fc 36441+
53392da6 36442+ au_wkq_run(wkinfo);
1facf9fc 36443+ } else {
36444+ err = -ENOMEM;
e49829fe 36445+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 36446+ }
36447+
36448+ return err;
36449+}
36450+
36451+/* ---------------------------------------------------------------------- */
36452+
36453+void au_nwt_init(struct au_nowait_tasks *nwt)
36454+{
f0c0a007
AM
36455+ atomic_set(&nwt->nw_len, 0);
36456+ /* smp_mb(); */ /* atomic_set */
1facf9fc 36457+ init_waitqueue_head(&nwt->nw_wq);
36458+}
36459+
36460+void au_wkq_fin(void)
36461+{
9dbd164d 36462+ destroy_workqueue(au_wkq);
1facf9fc 36463+}
36464+
36465+int __init au_wkq_init(void)
36466+{
9dbd164d 36467+ int err;
b752ccd1
AM
36468+
36469+ err = 0;
86dc4139 36470+ au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
9dbd164d
AM
36471+ if (IS_ERR(au_wkq))
36472+ err = PTR_ERR(au_wkq);
36473+ else if (!au_wkq)
36474+ err = -ENOMEM;
b752ccd1
AM
36475+
36476+ return err;
1facf9fc 36477+}
7f207e10
AM
36478diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36479--- /usr/share/empty/fs/aufs/wkq.h 1970-01-01 01:00:00.000000000 +0100
ba1aed25 36480+++ linux/fs/aufs/wkq.h 2019-03-05 12:13:00.145891204 +0100
acd2b654 36481@@ -0,0 +1,89 @@
062440b3 36482+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 36483+/*
ba1aed25 36484+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 36485+ *
36486+ * This program, aufs is free software; you can redistribute it and/or modify
36487+ * it under the terms of the GNU General Public License as published by
36488+ * the Free Software Foundation; either version 2 of the License, or
36489+ * (at your option) any later version.
dece6358
AM
36490+ *
36491+ * This program is distributed in the hope that it will be useful,
36492+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36493+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36494+ * GNU General Public License for more details.
36495+ *
36496+ * You should have received a copy of the GNU General Public License
523b37e3 36497+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36498+ */
36499+
36500+/*
36501+ * workqueue for asynchronous/super-io operations
36502+ * todo: try new credentials management scheme
36503+ */
36504+
36505+#ifndef __AUFS_WKQ_H__
36506+#define __AUFS_WKQ_H__
36507+
36508+#ifdef __KERNEL__
36509+
8b6a4947 36510+#include <linux/wait.h>
5afbbe0d 36511+
dece6358
AM
36512+struct super_block;
36513+
1facf9fc 36514+/* ---------------------------------------------------------------------- */
36515+
36516+/*
36517+ * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36518+ */
36519+struct au_nowait_tasks {
f0c0a007 36520+ atomic_t nw_len;
1facf9fc 36521+ wait_queue_head_t nw_wq;
36522+};
36523+
36524+/* ---------------------------------------------------------------------- */
36525+
36526+typedef void (*au_wkq_func_t)(void *args);
36527+
36528+/* wkq flags */
36529+#define AuWkq_WAIT 1
9dbd164d 36530+#define AuWkq_NEST (1 << 1)
1facf9fc 36531+#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
7f207e10
AM
36532+#define au_fset_wkq(flags, name) \
36533+ do { (flags) |= AuWkq_##name; } while (0)
36534+#define au_fclr_wkq(flags, name) \
36535+ do { (flags) &= ~AuWkq_##name; } while (0)
1facf9fc 36536+
36537+/* wkq.c */
b752ccd1 36538+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
53392da6
AM
36539+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36540+ unsigned int flags);
1facf9fc 36541+void au_nwt_init(struct au_nowait_tasks *nwt);
36542+int __init au_wkq_init(void);
36543+void au_wkq_fin(void);
36544+
36545+/* ---------------------------------------------------------------------- */
36546+
53392da6
AM
36547+static inline int au_wkq_test(void)
36548+{
36549+ return current->flags & PF_WQ_WORKER;
36550+}
36551+
b752ccd1 36552+static inline int au_wkq_wait(au_wkq_func_t func, void *args)
1facf9fc 36553+{
b752ccd1 36554+ return au_wkq_do_wait(AuWkq_WAIT, func, args);
1facf9fc 36555+}
36556+
36557+static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36558+{
f0c0a007 36559+ if (atomic_dec_and_test(&nwt->nw_len))
1facf9fc 36560+ wake_up_all(&nwt->nw_wq);
36561+}
36562+
36563+static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36564+{
f0c0a007 36565+ wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
1facf9fc 36566+ return 0;
36567+}
36568+
36569+#endif /* __KERNEL__ */
36570+#endif /* __AUFS_WKQ_H__ */
c1595e42
JR
36571diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36572--- /usr/share/empty/fs/aufs/xattr.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 36573+++ linux/fs/aufs/xattr.c 2019-03-05 12:13:00.145891204 +0100
062440b3 36574@@ -0,0 +1,356 @@
cd7a4cd9 36575+// SPDX-License-Identifier: GPL-2.0
c1595e42 36576+/*
ba1aed25 36577+ * Copyright (C) 2014-2019 Junjiro R. Okajima
c1595e42
JR
36578+ *
36579+ * This program, aufs is free software; you can redistribute it and/or modify
36580+ * it under the terms of the GNU General Public License as published by
36581+ * the Free Software Foundation; either version 2 of the License, or
36582+ * (at your option) any later version.
36583+ *
36584+ * This program is distributed in the hope that it will be useful,
36585+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36586+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36587+ * GNU General Public License for more details.
36588+ *
36589+ * You should have received a copy of the GNU General Public License
36590+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
36591+ */
36592+
36593+/*
36594+ * handling xattr functions
36595+ */
36596+
a2654f78
AM
36597+#include <linux/fs.h>
36598+#include <linux/posix_acl_xattr.h>
c1595e42
JR
36599+#include <linux/xattr.h>
36600+#include "aufs.h"
36601+
36602+static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36603+{
36604+ if (!ignore_flags)
36605+ goto out;
36606+ switch (err) {
36607+ case -ENOMEM:
36608+ case -EDQUOT:
36609+ goto out;
36610+ }
36611+
36612+ if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36613+ err = 0;
36614+ goto out;
36615+ }
36616+
36617+#define cmp(brattr, prefix) do { \
36618+ if (!strncmp(name, XATTR_##prefix##_PREFIX, \
36619+ XATTR_##prefix##_PREFIX_LEN)) { \
36620+ if (ignore_flags & AuBrAttr_ICEX_##brattr) \
36621+ err = 0; \
36622+ goto out; \
36623+ } \
36624+ } while (0)
36625+
36626+ cmp(SEC, SECURITY);
36627+ cmp(SYS, SYSTEM);
36628+ cmp(TR, TRUSTED);
36629+ cmp(USR, USER);
36630+#undef cmp
36631+
36632+ if (ignore_flags & AuBrAttr_ICEX_OTH)
36633+ err = 0;
36634+
36635+out:
36636+ return err;
36637+}
36638+
36639+static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36640+
36641+static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
7e9cd9fe
AM
36642+ char *name, char **buf, unsigned int ignore_flags,
36643+ unsigned int verbose)
c1595e42
JR
36644+{
36645+ int err;
36646+ ssize_t ssz;
36647+ struct inode *h_idst;
36648+
36649+ ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
36650+ err = ssz;
36651+ if (unlikely(err <= 0)) {
c1595e42
JR
36652+ if (err == -ENODATA
36653+ || (err == -EOPNOTSUPP
b912730e 36654+ && ((ignore_flags & au_xattr_out_of_list)
5527c038 36655+ || (au_test_nfs_noacl(d_inode(h_src))
b912730e
AM
36656+ && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36657+ || !strcmp(name,
36658+ XATTR_NAME_POSIX_ACL_DEFAULT))))
36659+ ))
c1595e42 36660+ err = 0;
b912730e
AM
36661+ if (err && (verbose || au_debug_test()))
36662+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
36663+ goto out;
36664+ }
36665+
36666+ /* unlock it temporary */
5527c038 36667+ h_idst = d_inode(h_dst);
febd17d6 36668+ inode_unlock(h_idst);
c1595e42 36669+ err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
febd17d6 36670+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42 36671+ if (unlikely(err)) {
7e9cd9fe
AM
36672+ if (verbose || au_debug_test())
36673+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
36674+ err = au_xattr_ignore(err, name, ignore_flags);
36675+ }
36676+
36677+out:
36678+ return err;
36679+}
36680+
7e9cd9fe
AM
36681+int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
36682+ unsigned int verbose)
c1595e42
JR
36683+{
36684+ int err, unlocked, acl_access, acl_default;
36685+ ssize_t ssz;
36686+ struct inode *h_isrc, *h_idst;
36687+ char *value, *p, *o, *e;
36688+
36689+ /* try stopping to update the source inode while we are referencing */
7e9cd9fe 36690+ /* there should not be the parent-child relationship between them */
5527c038
JR
36691+ h_isrc = d_inode(h_src);
36692+ h_idst = d_inode(h_dst);
febd17d6 36693+ inode_unlock(h_idst);
be118d29 36694+ inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
febd17d6 36695+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42
JR
36696+ unlocked = 0;
36697+
36698+ /* some filesystems don't list POSIX ACL, for example tmpfs */
36699+ ssz = vfs_listxattr(h_src, NULL, 0);
36700+ err = ssz;
36701+ if (unlikely(err < 0)) {
36702+ AuTraceErr(err);
36703+ if (err == -ENODATA
36704+ || err == -EOPNOTSUPP)
36705+ err = 0; /* ignore */
36706+ goto out;
36707+ }
36708+
36709+ err = 0;
36710+ p = NULL;
36711+ o = NULL;
36712+ if (ssz) {
36713+ err = -ENOMEM;
36714+ p = kmalloc(ssz, GFP_NOFS);
36715+ o = p;
36716+ if (unlikely(!p))
36717+ goto out;
36718+ err = vfs_listxattr(h_src, p, ssz);
36719+ }
3c1bdaff 36720+ inode_unlock_shared(h_isrc);
c1595e42
JR
36721+ unlocked = 1;
36722+ AuDbg("err %d, ssz %zd\n", err, ssz);
36723+ if (unlikely(err < 0))
36724+ goto out_free;
36725+
36726+ err = 0;
36727+ e = p + ssz;
36728+ value = NULL;
36729+ acl_access = 0;
36730+ acl_default = 0;
36731+ while (!err && p < e) {
36732+ acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36733+ sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36734+ acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36735+ sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36736+ - 1);
7e9cd9fe
AM
36737+ err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36738+ verbose);
c1595e42
JR
36739+ p += strlen(p) + 1;
36740+ }
36741+ AuTraceErr(err);
36742+ ignore_flags |= au_xattr_out_of_list;
36743+ if (!err && !acl_access) {
36744+ err = au_do_cpup_xattr(h_dst, h_src,
36745+ XATTR_NAME_POSIX_ACL_ACCESS, &value,
7e9cd9fe 36746+ ignore_flags, verbose);
c1595e42
JR
36747+ AuTraceErr(err);
36748+ }
36749+ if (!err && !acl_default) {
36750+ err = au_do_cpup_xattr(h_dst, h_src,
36751+ XATTR_NAME_POSIX_ACL_DEFAULT, &value,
7e9cd9fe 36752+ ignore_flags, verbose);
c1595e42
JR
36753+ AuTraceErr(err);
36754+ }
36755+
9f237c51 36756+ au_kfree_try_rcu(value);
c1595e42
JR
36757+
36758+out_free:
9f237c51 36759+ au_kfree_try_rcu(o);
c1595e42
JR
36760+out:
36761+ if (!unlocked)
3c1bdaff 36762+ inode_unlock_shared(h_isrc);
c1595e42
JR
36763+ AuTraceErr(err);
36764+ return err;
36765+}
36766+
36767+/* ---------------------------------------------------------------------- */
36768+
a2654f78
AM
36769+static int au_smack_reentering(struct super_block *sb)
36770+{
36771+#if IS_ENABLED(CONFIG_SECURITY_SMACK)
36772+ /*
36773+ * as a part of lookup, smack_d_instantiate() is called, and it calls
36774+ * i_op->getxattr(). ouch.
36775+ */
36776+ return si_pid_test(sb);
36777+#else
36778+ return 0;
36779+#endif
36780+}
36781+
c1595e42
JR
36782+enum {
36783+ AU_XATTR_LIST,
36784+ AU_XATTR_GET
36785+};
36786+
36787+struct au_lgxattr {
36788+ int type;
36789+ union {
36790+ struct {
36791+ char *list;
36792+ size_t size;
36793+ } list;
36794+ struct {
36795+ const char *name;
36796+ void *value;
36797+ size_t size;
36798+ } get;
36799+ } u;
36800+};
36801+
36802+static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
36803+{
36804+ ssize_t err;
a2654f78 36805+ int reenter;
c1595e42
JR
36806+ struct path h_path;
36807+ struct super_block *sb;
36808+
36809+ sb = dentry->d_sb;
a2654f78
AM
36810+ reenter = au_smack_reentering(sb);
36811+ if (!reenter) {
36812+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36813+ if (unlikely(err))
36814+ goto out;
36815+ }
36816+ err = au_h_path_getattr(dentry, /*force*/1, &h_path, reenter);
c1595e42
JR
36817+ if (unlikely(err))
36818+ goto out_si;
36819+ if (unlikely(!h_path.dentry))
36820+ /* illegally overlapped or something */
36821+ goto out_di; /* pretending success */
36822+
36823+ /* always topmost entry only */
36824+ switch (arg->type) {
36825+ case AU_XATTR_LIST:
36826+ err = vfs_listxattr(h_path.dentry,
36827+ arg->u.list.list, arg->u.list.size);
36828+ break;
36829+ case AU_XATTR_GET:
5afbbe0d 36830+ AuDebugOn(d_is_negative(h_path.dentry));
c1595e42
JR
36831+ err = vfs_getxattr(h_path.dentry,
36832+ arg->u.get.name, arg->u.get.value,
36833+ arg->u.get.size);
36834+ break;
36835+ }
36836+
36837+out_di:
a2654f78
AM
36838+ if (!reenter)
36839+ di_read_unlock(dentry, AuLock_IR);
c1595e42 36840+out_si:
a2654f78
AM
36841+ if (!reenter)
36842+ si_read_unlock(sb);
c1595e42
JR
36843+out:
36844+ AuTraceErr(err);
36845+ return err;
36846+}
36847+
36848+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36849+{
36850+ struct au_lgxattr arg = {
36851+ .type = AU_XATTR_LIST,
36852+ .u.list = {
36853+ .list = list,
36854+ .size = size
36855+ },
36856+ };
36857+
36858+ return au_lgxattr(dentry, &arg);
36859+}
36860+
f2c43d5f
AM
36861+static ssize_t au_getxattr(struct dentry *dentry,
36862+ struct inode *inode __maybe_unused,
36863+ const char *name, void *value, size_t size)
c1595e42
JR
36864+{
36865+ struct au_lgxattr arg = {
36866+ .type = AU_XATTR_GET,
36867+ .u.get = {
36868+ .name = name,
36869+ .value = value,
36870+ .size = size
36871+ },
36872+ };
36873+
36874+ return au_lgxattr(dentry, &arg);
36875+}
36876+
f2c43d5f
AM
36877+static int au_setxattr(struct dentry *dentry, struct inode *inode,
36878+ const char *name, const void *value, size_t size,
36879+ int flags)
c1595e42 36880+{
f2c43d5f 36881+ struct au_sxattr arg = {
c1595e42
JR
36882+ .type = AU_XATTR_SET,
36883+ .u.set = {
36884+ .name = name,
36885+ .value = value,
36886+ .size = size,
36887+ .flags = flags
36888+ },
36889+ };
36890+
f2c43d5f 36891+ return au_sxattr(dentry, inode, &arg);
c1595e42
JR
36892+}
36893+
36894+/* ---------------------------------------------------------------------- */
36895+
f2c43d5f
AM
36896+static int au_xattr_get(const struct xattr_handler *handler,
36897+ struct dentry *dentry, struct inode *inode,
36898+ const char *name, void *buffer, size_t size)
c1595e42 36899+{
f2c43d5f 36900+ return au_getxattr(dentry, inode, name, buffer, size);
c1595e42
JR
36901+}
36902+
f2c43d5f
AM
36903+static int au_xattr_set(const struct xattr_handler *handler,
36904+ struct dentry *dentry, struct inode *inode,
36905+ const char *name, const void *value, size_t size,
36906+ int flags)
c1595e42 36907+{
f2c43d5f 36908+ return au_setxattr(dentry, inode, name, value, size, flags);
c1595e42
JR
36909+}
36910+
36911+static const struct xattr_handler au_xattr_handler = {
f2c43d5f
AM
36912+ .name = "",
36913+ .prefix = "",
c1595e42
JR
36914+ .get = au_xattr_get,
36915+ .set = au_xattr_set
c1595e42
JR
36916+};
36917+
36918+static const struct xattr_handler *au_xattr_handlers[] = {
a2654f78
AM
36919+#ifdef CONFIG_FS_POSIX_ACL
36920+ &posix_acl_access_xattr_handler,
36921+ &posix_acl_default_xattr_handler,
36922+#endif
36923+ &au_xattr_handler, /* must be last */
f2c43d5f 36924+ NULL
c1595e42
JR
36925+};
36926+
36927+void au_xattr_init(struct super_block *sb)
36928+{
f2c43d5f 36929+ sb->s_xattr = au_xattr_handlers;
c1595e42 36930+}
7f207e10
AM
36931diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
36932--- /usr/share/empty/fs/aufs/xino.c 1970-01-01 01:00:00.000000000 +0100
ba1aed25 36933+++ linux/fs/aufs/xino.c 2019-03-05 12:13:00.145891204 +0100
9f237c51 36934@@ -0,0 +1,1956 @@
cd7a4cd9 36935+// SPDX-License-Identifier: GPL-2.0
1facf9fc 36936+/*
ba1aed25 36937+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 36938+ *
36939+ * This program, aufs is free software; you can redistribute it and/or modify
36940+ * it under the terms of the GNU General Public License as published by
36941+ * the Free Software Foundation; either version 2 of the License, or
36942+ * (at your option) any later version.
dece6358
AM
36943+ *
36944+ * This program is distributed in the hope that it will be useful,
36945+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36946+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36947+ * GNU General Public License for more details.
36948+ *
36949+ * You should have received a copy of the GNU General Public License
523b37e3 36950+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36951+ */
36952+
36953+/*
36954+ * external inode number translation table and bitmap
062440b3
AM
36955+ *
36956+ * things to consider
36957+ * - the lifetime
36958+ * + au_xino object
36959+ * + XINO files (xino, xib, xigen)
36960+ * + dynamic debugfs entries (xiN)
36961+ * + static debugfs entries (xib, xigen)
36962+ * + static sysfs entry (xi_path)
36963+ * - several entry points to handle them.
36964+ * + mount(2) without xino option (default)
36965+ * + mount(2) with xino option
36966+ * + mount(2) with noxino option
36967+ * + umount(2)
36968+ * + remount with add/del branches
36969+ * + remount with xino/noxino options
1facf9fc 36970+ */
36971+
36972+#include <linux/seq_file.h>
392086de 36973+#include <linux/statfs.h>
1facf9fc 36974+#include "aufs.h"
36975+
062440b3
AM
36976+static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
36977+ aufs_bindex_t bbot,
36978+ struct super_block *h_sb)
1facf9fc 36979+{
062440b3
AM
36980+ /* todo: try binary-search if the branches are many */
36981+ for (; btop <= bbot; btop++)
36982+ if (h_sb == au_sbr_sb(sb, btop))
36983+ return btop;
36984+ return -1;
be52b249
AM
36985+}
36986+
062440b3
AM
36987+/*
36988+ * find another branch who is on the same filesystem of the specified
36989+ * branch{@btgt}. search until @bbot.
36990+ */
36991+static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
36992+ aufs_bindex_t bbot)
1facf9fc 36993+{
062440b3
AM
36994+ aufs_bindex_t bindex;
36995+ struct super_block *tgt_sb;
1facf9fc 36996+
062440b3
AM
36997+ tgt_sb = au_sbr_sb(sb, btgt);
36998+ bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
36999+ if (bindex < 0)
37000+ bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
1facf9fc 37001+
062440b3 37002+ return bindex;
1facf9fc 37003+}
37004+
37005+/* ---------------------------------------------------------------------- */
37006+
37007+/*
062440b3 37008+ * stop unnecessary notify events at creating xino files
1facf9fc 37009+ */
acd2b654
AM
37010+
37011+aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
37012+{
37013+ aufs_bindex_t bfound, bindex, bbot;
37014+ struct dentry *parent;
37015+ struct au_branch *br;
37016+
37017+ bfound = -1;
37018+ parent = dentry->d_parent; /* safe d_parent access */
37019+ bbot = au_sbbot(sb);
37020+ for (bindex = 0; bindex <= bbot; bindex++) {
37021+ br = au_sbr(sb, bindex);
37022+ if (au_br_dentry(br) == parent) {
37023+ bfound = bindex;
37024+ break;
37025+ }
37026+ }
37027+
37028+ AuDbg("bfound b%d\n", bfound);
37029+ return bfound;
37030+}
37031+
062440b3
AM
37032+struct au_xino_lock_dir {
37033+ struct au_hinode *hdir;
37034+ struct dentry *parent;
37035+ struct inode *dir;
37036+};
37037+
37038+static struct dentry *au_dget_parent_lock(struct dentry *dentry,
37039+ unsigned int lsc)
1facf9fc 37040+{
062440b3
AM
37041+ struct dentry *parent;
37042+ struct inode *dir;
1facf9fc 37043+
062440b3 37044+ parent = dget_parent(dentry);
5527c038 37045+ dir = d_inode(parent);
062440b3
AM
37046+ inode_lock_nested(dir, lsc);
37047+#if 0 /* it should not happen */
37048+ spin_lock(&dentry->d_lock);
37049+ if (unlikely(dentry->d_parent != parent)) {
37050+ spin_unlock(&dentry->d_lock);
37051+ inode_unlock(dir);
37052+ dput(parent);
37053+ parent = NULL;
1facf9fc 37054+ goto out;
37055+ }
062440b3 37056+ spin_unlock(&dentry->d_lock);
1facf9fc 37057+
4f0767ce 37058+out:
062440b3
AM
37059+#endif
37060+ return parent;
1facf9fc 37061+}
37062+
062440b3 37063+static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
1facf9fc 37064+ struct au_xino_lock_dir *ldir)
37065+{
acd2b654 37066+ aufs_bindex_t bindex;
1facf9fc 37067+
37068+ ldir->hdir = NULL;
acd2b654 37069+ bindex = au_xi_root(sb, xipath->dentry);
1facf9fc 37070+ if (bindex >= 0) {
062440b3 37071+ /* rw branch root */
5527c038 37072+ ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 37073+ au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
1facf9fc 37074+ } else {
062440b3
AM
37075+ /* other */
37076+ ldir->parent = au_dget_parent_lock(xipath->dentry,
37077+ AuLsc_I_PARENT);
febd17d6 37078+ ldir->dir = d_inode(ldir->parent);
1facf9fc 37079+ }
37080+}
37081+
37082+static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
37083+{
37084+ if (ldir->hdir)
5afbbe0d 37085+ au_hn_inode_unlock(ldir->hdir);
1facf9fc 37086+ else {
febd17d6 37087+ inode_unlock(ldir->dir);
1facf9fc 37088+ dput(ldir->parent);
37089+ }
37090+}
37091+
37092+/* ---------------------------------------------------------------------- */
37093+
062440b3
AM
37094+/*
37095+ * create and set a new xino file
37096+ */
37097+struct file *au_xino_create(struct super_block *sb, char *fpath, int silent)
37098+{
37099+ struct file *file;
37100+ struct dentry *h_parent, *d;
37101+ struct inode *h_dir, *inode;
37102+ int err;
37103+
37104+ /*
37105+ * at mount-time, and the xino file is the default path,
37106+ * hnotify is disabled so we have no notify events to ignore.
37107+ * when a user specified the xino, we cannot get au_hdir to be ignored.
37108+ */
37109+ file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37110+ /* | __FMODE_NONOTIFY */,
cd7a4cd9 37111+ 0666);
062440b3
AM
37112+ if (IS_ERR(file)) {
37113+ if (!silent)
37114+ pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
37115+ return file;
37116+ }
37117+
37118+ /* keep file count */
37119+ err = 0;
37120+ d = file->f_path.dentry;
37121+ h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
37122+ /* mnt_want_write() is unnecessary here */
37123+ h_dir = d_inode(h_parent);
37124+ inode = file_inode(file);
37125+ /* no delegation since it is just created */
37126+ if (inode->i_nlink)
37127+ err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
37128+ /*force*/0);
37129+ inode_unlock(h_dir);
37130+ dput(h_parent);
37131+ if (unlikely(err)) {
37132+ if (!silent)
37133+ pr_err("unlink %s(%d)\n", fpath, err);
37134+ goto out;
37135+ }
37136+
37137+ err = -EINVAL;
37138+ if (unlikely(sb == d->d_sb)) {
37139+ if (!silent)
37140+ pr_err("%s must be outside\n", fpath);
37141+ goto out;
37142+ }
37143+ if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
37144+ if (!silent)
37145+ pr_err("xino doesn't support %s(%s)\n",
37146+ fpath, au_sbtype(d->d_sb));
37147+ goto out;
37148+ }
37149+ return file; /* success */
37150+
37151+out:
37152+ fput(file);
37153+ file = ERR_PTR(err);
37154+ return file;
37155+}
37156+
37157+/*
37158+ * create a new xinofile at the same place/path as @base.
37159+ */
37160+struct file *au_xino_create2(struct super_block *sb, struct path *base,
37161+ struct file *copy_src)
37162+{
37163+ struct file *file;
37164+ struct dentry *dentry, *parent;
37165+ struct inode *dir, *delegated;
37166+ struct qstr *name;
37167+ struct path path;
37168+ int err, do_unlock;
37169+ struct au_xino_lock_dir ldir;
37170+
37171+ do_unlock = 1;
37172+ au_xino_lock_dir(sb, base, &ldir);
37173+ dentry = base->dentry;
37174+ parent = dentry->d_parent; /* dir inode is locked */
37175+ dir = d_inode(parent);
37176+ IMustLock(dir);
37177+
37178+ name = &dentry->d_name;
37179+ path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
37180+ if (IS_ERR(path.dentry)) {
37181+ file = (void *)path.dentry;
37182+ pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37183+ goto out;
37184+ }
37185+
37186+ /* no need to mnt_want_write() since we call dentry_open() later */
cd7a4cd9 37187+ err = vfs_create(dir, path.dentry, 0666, NULL);
062440b3
AM
37188+ if (unlikely(err)) {
37189+ file = ERR_PTR(err);
37190+ pr_err("%pd create err %d\n", dentry, err);
37191+ goto out_dput;
37192+ }
37193+
37194+ path.mnt = base->mnt;
37195+ file = vfsub_dentry_open(&path,
37196+ O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37197+ /* | __FMODE_NONOTIFY */);
37198+ if (IS_ERR(file)) {
37199+ pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37200+ goto out_dput;
37201+ }
37202+
37203+ delegated = NULL;
37204+ err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37205+ au_xino_unlock_dir(&ldir);
37206+ do_unlock = 0;
37207+ if (unlikely(err == -EWOULDBLOCK)) {
37208+ pr_warn("cannot retry for NFSv4 delegation"
37209+ " for an internal unlink\n");
37210+ iput(delegated);
37211+ }
37212+ if (unlikely(err)) {
37213+ pr_err("%pd unlink err %d\n", dentry, err);
37214+ goto out_fput;
37215+ }
37216+
37217+ if (copy_src) {
37218+ /* no one can touch copy_src xino */
37219+ err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37220+ if (unlikely(err)) {
37221+ pr_err("%pd copy err %d\n", dentry, err);
37222+ goto out_fput;
37223+ }
37224+ }
37225+ goto out_dput; /* success */
1facf9fc 37226+
062440b3
AM
37227+out_fput:
37228+ fput(file);
37229+ file = ERR_PTR(err);
37230+out_dput:
37231+ dput(path.dentry);
37232+out:
37233+ if (do_unlock)
37234+ au_xino_unlock_dir(&ldir);
37235+ return file;
37236+}
37237+
acd2b654
AM
37238+struct file *au_xino_file1(struct au_xino *xi)
37239+{
37240+ struct file *file;
37241+ unsigned int u, nfile;
37242+
37243+ file = NULL;
37244+ nfile = xi->xi_nfile;
37245+ for (u = 0; u < nfile; u++) {
37246+ file = xi->xi_file[u];
37247+ if (file)
37248+ break;
37249+ }
37250+
37251+ return file;
37252+}
37253+
37254+static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37255+{
37256+ int err;
37257+ struct file *f;
37258+ void *p;
37259+
37260+ if (file)
37261+ get_file(file);
37262+
37263+ err = 0;
37264+ f = NULL;
37265+ if (idx < xi->xi_nfile) {
37266+ f = xi->xi_file[idx];
37267+ if (f)
37268+ fput(f);
37269+ } else {
37270+ p = au_kzrealloc(xi->xi_file,
37271+ sizeof(*xi->xi_file) * xi->xi_nfile,
37272+ sizeof(*xi->xi_file) * (idx + 1),
37273+ GFP_NOFS, /*may_shrink*/0);
37274+ if (p) {
37275+ MtxMustLock(&xi->xi_mtx);
37276+ xi->xi_file = p;
37277+ xi->xi_nfile = idx + 1;
37278+ } else {
37279+ err = -ENOMEM;
37280+ if (file)
37281+ fput(file);
37282+ goto out;
37283+ }
37284+ }
37285+ xi->xi_file[idx] = file;
37286+
37287+out:
37288+ return err;
37289+}
37290+
37291+/*
37292+ * if @xinew->xi is not set, then create new xigen file.
37293+ */
37294+struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37295+{
37296+ struct file *file;
37297+ int err;
37298+
37299+ SiMustAnyLock(sb);
37300+
37301+ file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37302+ if (IS_ERR(file)) {
37303+ err = PTR_ERR(file);
37304+ pr_err("%s[%d], err %d\n",
37305+ xinew->xi ? "xino" : "xigen",
37306+ xinew->idx, err);
37307+ goto out;
37308+ }
37309+
37310+ if (xinew->xi)
37311+ err = au_xino_file_set(xinew->xi, xinew->idx, file);
37312+ else {
37313+ BUG();
37314+ /* todo: make xigen file an array */
37315+ /* err = au_xigen_file_set(sb, xinew->idx, file); */
37316+ }
37317+ fput(file);
37318+ if (unlikely(err))
37319+ file = ERR_PTR(err);
37320+
37321+out:
37322+ return file;
37323+}
37324+
062440b3
AM
37325+/* ---------------------------------------------------------------------- */
37326+
37327+/*
37328+ * truncate xino files
37329+ */
acd2b654
AM
37330+static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37331+ int idx, struct kstatfs *st)
1facf9fc 37332+{
37333+ int err;
392086de 37334+ blkcnt_t blocks;
acd2b654
AM
37335+ struct file *file, *new_xino;
37336+ struct au_xi_new xinew = {
37337+ .idx = idx
37338+ };
37339+
37340+ err = 0;
37341+ xinew.xi = au_sbr(sb, bindex)->br_xino;
37342+ file = au_xino_file(xinew.xi, idx);
37343+ if (!file)
37344+ goto out;
37345+
37346+ xinew.base = &file->f_path;
37347+ err = vfs_statfs(xinew.base, st);
37348+ if (unlikely(err)) {
37349+ AuErr1("statfs err %d, ignored\n", err);
37350+ err = 0;
37351+ goto out;
37352+ }
37353+
37354+ blocks = file_inode(file)->i_blocks;
37355+ pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37356+ bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37357+
37358+ xinew.copy_src = file;
37359+ new_xino = au_xi_new(sb, &xinew);
37360+ if (IS_ERR(new_xino)) {
37361+ err = PTR_ERR(new_xino);
37362+ pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37363+ goto out;
37364+ }
37365+
37366+ err = vfs_statfs(&new_xino->f_path, st);
37367+ if (!err)
37368+ pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37369+ bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37370+ st->f_bfree, st->f_blocks);
37371+ else {
37372+ AuErr1("statfs err %d, ignored\n", err);
37373+ err = 0;
37374+ }
37375+
37376+out:
37377+ return err;
37378+}
37379+
37380+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37381+{
37382+ int err, i;
37383+ unsigned long jiffy;
062440b3 37384+ aufs_bindex_t bbot;
392086de 37385+ struct kstatfs *st;
1facf9fc 37386+ struct au_branch *br;
acd2b654 37387+ struct au_xino *xi;
1facf9fc 37388+
392086de 37389+ err = -ENOMEM;
be52b249 37390+ st = kmalloc(sizeof(*st), GFP_NOFS);
392086de
AM
37391+ if (unlikely(!st))
37392+ goto out;
37393+
1facf9fc 37394+ err = -EINVAL;
5afbbe0d
AM
37395+ bbot = au_sbbot(sb);
37396+ if (unlikely(bindex < 0 || bbot < bindex))
392086de 37397+ goto out_st;
392086de 37398+
1facf9fc 37399+ err = 0;
acd2b654
AM
37400+ jiffy = jiffies;
37401+ br = au_sbr(sb, bindex);
37402+ xi = br->br_xino;
37403+ for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37404+ err = au_xino_do_trunc(sb, bindex, i, st);
37405+ if (!err)
37406+ au_sbi(sb)->si_xino_jiffy = jiffy;
392086de
AM
37407+
37408+out_st:
9f237c51 37409+ au_kfree_rcu(st);
4f0767ce 37410+out:
1facf9fc 37411+ return err;
37412+}
37413+
37414+struct xino_do_trunc_args {
37415+ struct super_block *sb;
37416+ struct au_branch *br;
acd2b654 37417+ int idx;
1facf9fc 37418+};
37419+
37420+static void xino_do_trunc(void *_args)
37421+{
37422+ struct xino_do_trunc_args *args = _args;
37423+ struct super_block *sb;
37424+ struct au_branch *br;
37425+ struct inode *dir;
acd2b654 37426+ int err, idx;
1facf9fc 37427+ aufs_bindex_t bindex;
37428+
37429+ err = 0;
37430+ sb = args->sb;
5527c038 37431+ dir = d_inode(sb->s_root);
1facf9fc 37432+ br = args->br;
acd2b654 37433+ idx = args->idx;
1facf9fc 37434+
37435+ si_noflush_write_lock(sb);
37436+ ii_read_lock_parent(dir);
37437+ bindex = au_br_index(sb, br->br_id);
acd2b654 37438+ err = au_xino_trunc(sb, bindex, idx);
1facf9fc 37439+ ii_read_unlock(dir);
37440+ if (unlikely(err))
392086de 37441+ pr_warn("err b%d, (%d)\n", bindex, err);
062440b3 37442+ atomic_dec(&br->br_xino->xi_truncating);
acd2b654 37443+ au_lcnt_dec(&br->br_count);
1facf9fc 37444+ si_write_unlock(sb);
027c5e7a 37445+ au_nwt_done(&au_sbi(sb)->si_nowait);
9f237c51 37446+ au_kfree_rcu(args);
1facf9fc 37447+}
37448+
acd2b654
AM
37449+/*
37450+ * returns the index in the xi_file array whose corresponding file is necessary
37451+ * to truncate, or -1 which means no need to truncate.
37452+ */
392086de
AM
37453+static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37454+{
37455+ int err;
acd2b654 37456+ unsigned int u;
392086de
AM
37457+ struct kstatfs st;
37458+ struct au_sbinfo *sbinfo;
acd2b654 37459+ struct au_xino *xi;
062440b3 37460+ struct file *file;
392086de
AM
37461+
37462+ /* todo: si_xino_expire and the ratio should be customizable */
37463+ sbinfo = au_sbi(sb);
37464+ if (time_before(jiffies,
37465+ sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
acd2b654 37466+ return -1;
392086de
AM
37467+
37468+ /* truncation border */
acd2b654
AM
37469+ xi = br->br_xino;
37470+ for (u = 0; u < xi->xi_nfile; u++) {
37471+ file = au_xino_file(xi, u);
37472+ if (!file)
37473+ continue;
37474+
37475+ err = vfs_statfs(&file->f_path, &st);
37476+ if (unlikely(err)) {
37477+ AuErr1("statfs err %d, ignored\n", err);
37478+ return -1;
37479+ }
37480+ if (div64_u64(st.f_bfree * 100, st.f_blocks)
37481+ >= AUFS_XINO_DEF_TRUNC)
37482+ return u;
392086de 37483+ }
392086de 37484+
acd2b654 37485+ return -1;
392086de
AM
37486+}
37487+
1facf9fc 37488+static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37489+{
acd2b654 37490+ int idx;
1facf9fc 37491+ struct xino_do_trunc_args *args;
37492+ int wkq_err;
37493+
acd2b654
AM
37494+ idx = xino_trunc_test(sb, br);
37495+ if (idx < 0)
1facf9fc 37496+ return;
37497+
062440b3 37498+ if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
1facf9fc 37499+ goto out;
37500+
37501+ /* lock and kfree() will be called in trunc_xino() */
37502+ args = kmalloc(sizeof(*args), GFP_NOFS);
37503+ if (unlikely(!args)) {
37504+ AuErr1("no memory\n");
f0c0a007 37505+ goto out;
1facf9fc 37506+ }
37507+
acd2b654 37508+ au_lcnt_inc(&br->br_count);
1facf9fc 37509+ args->sb = sb;
37510+ args->br = br;
acd2b654 37511+ args->idx = idx;
53392da6 37512+ wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
1facf9fc 37513+ if (!wkq_err)
37514+ return; /* success */
37515+
4a4d8108 37516+ pr_err("wkq %d\n", wkq_err);
acd2b654 37517+ au_lcnt_dec(&br->br_count);
9f237c51 37518+ au_kfree_rcu(args);
1facf9fc 37519+
4f0767ce 37520+out:
062440b3 37521+ atomic_dec(&br->br_xino->xi_truncating);
1facf9fc 37522+}
37523+
37524+/* ---------------------------------------------------------------------- */
37525+
acd2b654
AM
37526+struct au_xi_calc {
37527+ int idx;
37528+ loff_t pos;
37529+};
37530+
37531+static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37532+ struct au_xi_calc *calc)
37533+{
37534+ loff_t maxent;
37535+
37536+ maxent = au_xi_maxent(sb);
37537+ calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37538+ calc->pos *= sizeof(ino_t);
37539+}
37540+
37541+static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37542+ struct au_xi_calc *calc)
37543+{
37544+ int err;
37545+ struct file *file;
37546+ struct au_xino *xi = br->br_xino;
37547+ struct au_xi_new xinew = {
37548+ .xi = xi
37549+ };
37550+
37551+ SiMustAnyLock(sb);
37552+
37553+ err = 0;
37554+ if (!xi)
37555+ goto out;
37556+
37557+ mutex_lock(&xi->xi_mtx);
37558+ file = au_xino_file(xi, calc->idx);
37559+ if (file)
37560+ goto out_mtx;
37561+
37562+ file = au_xino_file(xi, /*idx*/-1);
37563+ AuDebugOn(!file);
37564+ xinew.idx = calc->idx;
37565+ xinew.base = &file->f_path;
37566+ /* xinew.copy_src = NULL; */
37567+ file = au_xi_new(sb, &xinew);
37568+ if (IS_ERR(file))
37569+ err = PTR_ERR(file);
37570+
37571+out_mtx:
37572+ mutex_unlock(&xi->xi_mtx);
37573+out:
37574+ return err;
37575+}
37576+
37577+struct au_xino_do_new_async_args {
37578+ struct super_block *sb;
37579+ struct au_branch *br;
37580+ struct au_xi_calc calc;
37581+ ino_t ino;
37582+};
37583+
9f237c51
AM
37584+struct au_xi_writing {
37585+ struct hlist_bl_node node;
37586+ ino_t h_ino, ino;
37587+};
37588+
acd2b654
AM
37589+static int au_xino_do_write(vfs_writef_t write, struct file *file,
37590+ struct au_xi_calc *calc, ino_t ino);
37591+
37592+static void au_xino_call_do_new_async(void *args)
37593+{
37594+ struct au_xino_do_new_async_args *a = args;
37595+ struct au_branch *br;
37596+ struct super_block *sb;
37597+ struct au_sbinfo *sbi;
37598+ struct inode *root;
37599+ struct file *file;
9f237c51
AM
37600+ struct au_xi_writing *del, *p;
37601+ struct hlist_bl_head *hbl;
37602+ struct hlist_bl_node *pos;
acd2b654
AM
37603+ int err;
37604+
37605+ br = a->br;
37606+ sb = a->sb;
37607+ sbi = au_sbi(sb);
37608+ si_noflush_read_lock(sb);
37609+ root = d_inode(sb->s_root);
37610+ ii_read_lock_child(root);
37611+ err = au_xino_do_new_async(sb, br, &a->calc);
9f237c51
AM
37612+ if (unlikely(err)) {
37613+ AuIOErr("err %d\n", err);
37614+ goto out;
37615+ }
37616+
37617+ file = au_xino_file(br->br_xino, a->calc.idx);
37618+ AuDebugOn(!file);
37619+ err = au_xino_do_write(sbi->si_xwrite, file, &a->calc, a->ino);
37620+ if (unlikely(err)) {
acd2b654 37621+ AuIOErr("err %d\n", err);
9f237c51
AM
37622+ goto out;
37623+ }
37624+
37625+ del = NULL;
37626+ hbl = &br->br_xino->xi_writing;
37627+ hlist_bl_lock(hbl);
37628+ au_hbl_for_each(pos, hbl) {
37629+ p = container_of(pos, struct au_xi_writing, node);
37630+ if (p->ino == a->ino) {
37631+ del = p;
37632+ hlist_bl_del(&p->node);
37633+ break;
37634+ }
37635+ }
37636+ hlist_bl_unlock(hbl);
37637+ au_kfree_rcu(del);
37638+
37639+out:
acd2b654
AM
37640+ au_lcnt_dec(&br->br_count);
37641+ ii_read_unlock(root);
37642+ si_read_unlock(sb);
37643+ au_nwt_done(&sbi->si_nowait);
9f237c51 37644+ au_kfree_rcu(a);
acd2b654
AM
37645+}
37646+
37647+/*
37648+ * create a new xino file asynchronously
37649+ */
37650+static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37651+ struct au_xi_calc *calc, ino_t ino)
37652+{
37653+ int err;
37654+ struct au_xino_do_new_async_args *arg;
37655+
37656+ err = -ENOMEM;
37657+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
37658+ if (unlikely(!arg))
37659+ goto out;
37660+
37661+ arg->sb = sb;
37662+ arg->br = br;
37663+ arg->calc = *calc;
37664+ arg->ino = ino;
37665+ au_lcnt_inc(&br->br_count);
37666+ err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37667+ if (unlikely(err)) {
37668+ pr_err("wkq %d\n", err);
37669+ au_lcnt_dec(&br->br_count);
9f237c51 37670+ au_kfree_rcu(arg);
acd2b654
AM
37671+ }
37672+
37673+out:
37674+ return err;
37675+}
37676+
062440b3
AM
37677+/*
37678+ * read @ino from xinofile for the specified branch{@sb, @bindex}
37679+ * at the position of @h_ino.
37680+ */
37681+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37682+ ino_t *ino)
37683+{
37684+ int err;
37685+ ssize_t sz;
acd2b654 37686+ struct au_xi_calc calc;
062440b3 37687+ struct au_sbinfo *sbinfo;
acd2b654 37688+ struct file *file;
9f237c51
AM
37689+ struct au_xino *xi;
37690+ struct hlist_bl_head *hbl;
37691+ struct hlist_bl_node *pos;
37692+ struct au_xi_writing *p;
062440b3
AM
37693+
37694+ *ino = 0;
37695+ if (!au_opt_test(au_mntflags(sb), XINO))
37696+ return 0; /* no xino */
37697+
37698+ err = 0;
acd2b654 37699+ au_xi_calc(sb, h_ino, &calc);
9f237c51
AM
37700+ xi = au_sbr(sb, bindex)->br_xino;
37701+ file = au_xino_file(xi, calc.idx);
37702+ if (!file) {
37703+ hbl = &xi->xi_writing;
37704+ hlist_bl_lock(hbl);
37705+ au_hbl_for_each(pos, hbl) {
37706+ p = container_of(pos, struct au_xi_writing, node);
37707+ if (p->h_ino == h_ino) {
37708+ AuDbg("hi%llu, i%llu, found\n",
37709+ (u64)p->h_ino, (u64)p->ino);
37710+ *ino = p->ino;
37711+ break;
37712+ }
37713+ }
37714+ hlist_bl_unlock(hbl);
37715+ return 0;
37716+ } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37717+ return 0; /* no xino */
062440b3 37718+
acd2b654
AM
37719+ sbinfo = au_sbi(sb);
37720+ sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &calc.pos);
062440b3
AM
37721+ if (sz == sizeof(*ino))
37722+ return 0; /* success */
37723+
37724+ err = sz;
37725+ if (unlikely(sz >= 0)) {
37726+ err = -EIO;
37727+ AuIOErr("xino read error (%zd)\n", sz);
37728+ }
062440b3
AM
37729+ return err;
37730+}
37731+
5527c038 37732+static int au_xino_do_write(vfs_writef_t write, struct file *file,
acd2b654 37733+ struct au_xi_calc *calc, ino_t ino)
1facf9fc 37734+{
1facf9fc 37735+ ssize_t sz;
37736+
acd2b654 37737+ sz = xino_fwrite(write, file, &ino, sizeof(ino), &calc->pos);
1facf9fc 37738+ if (sz == sizeof(ino))
37739+ return 0; /* success */
37740+
37741+ AuIOErr("write failed (%zd)\n", sz);
37742+ return -EIO;
37743+}
37744+
37745+/*
37746+ * write @ino to the xinofile for the specified branch{@sb, @bindex}
37747+ * at the position of @h_ino.
37748+ * even if @ino is zero, it is written to the xinofile and means no entry.
37749+ * if the size of the xino file on a specific filesystem exceeds the watermark,
37750+ * try truncating it.
37751+ */
37752+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37753+ ino_t ino)
37754+{
37755+ int err;
37756+ unsigned int mnt_flags;
acd2b654
AM
37757+ struct au_xi_calc calc;
37758+ struct file *file;
1facf9fc 37759+ struct au_branch *br;
acd2b654 37760+ struct au_xino *xi;
9f237c51 37761+ struct au_xi_writing *p;
1facf9fc 37762+
dece6358 37763+ SiMustAnyLock(sb);
1facf9fc 37764+
37765+ mnt_flags = au_mntflags(sb);
37766+ if (!au_opt_test(mnt_flags, XINO))
37767+ return 0;
37768+
acd2b654 37769+ au_xi_calc(sb, h_ino, &calc);
1facf9fc 37770+ br = au_sbr(sb, bindex);
acd2b654
AM
37771+ xi = br->br_xino;
37772+ file = au_xino_file(xi, calc.idx);
37773+ if (!file) {
9f237c51
AM
37774+ /* store the inum pair into the list */
37775+ p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37776+ p->h_ino = h_ino;
37777+ p->ino = ino;
37778+ au_hbl_add(&p->node, &xi->xi_writing);
37779+
acd2b654
AM
37780+ /* create and write a new xino file asynchronously */
37781+ err = au_xino_new_async(sb, br, &calc, ino);
37782+ if (!err)
37783+ return 0; /* success */
37784+ goto out;
37785+ }
37786+
37787+ err = au_xino_do_write(au_sbi(sb)->si_xwrite, file, &calc, ino);
1facf9fc 37788+ if (!err) {
acd2b654 37789+ br = au_sbr(sb, bindex);
1facf9fc 37790+ if (au_opt_test(mnt_flags, TRUNC_XINO)
86dc4139 37791+ && au_test_fs_trunc_xino(au_br_sb(br)))
1facf9fc 37792+ xino_try_trunc(sb, br);
37793+ return 0; /* success */
37794+ }
37795+
acd2b654 37796+out:
1facf9fc 37797+ AuIOErr("write failed (%d)\n", err);
37798+ return -EIO;
37799+}
37800+
062440b3
AM
37801+static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37802+ size_t size, loff_t *pos);
1facf9fc 37803+
062440b3
AM
37804+/* todo: unnecessary to support mmap_sem since kernel-space? */
37805+ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
37806+ loff_t *pos)
37807+{
37808+ ssize_t err;
37809+ mm_segment_t oldfs;
37810+ union {
37811+ void *k;
37812+ char __user *u;
37813+ } buf;
37814+ int i;
37815+ const int prevent_endless = 10;
1facf9fc 37816+
062440b3
AM
37817+ i = 0;
37818+ buf.k = kbuf;
37819+ oldfs = get_fs();
37820+ set_fs(KERNEL_DS);
37821+ do {
37822+ err = func(file, buf.u, size, pos);
37823+ if (err == -EINTR
37824+ && !au_wkq_test()
37825+ && fatal_signal_pending(current)) {
37826+ set_fs(oldfs);
37827+ err = xino_fread_wkq(func, file, kbuf, size, pos);
37828+ BUG_ON(err == -EINTR);
37829+ oldfs = get_fs();
37830+ set_fs(KERNEL_DS);
37831+ }
37832+ } while (i++ < prevent_endless
37833+ && (err == -EAGAIN || err == -EINTR));
37834+ set_fs(oldfs);
37835+
37836+#if 0 /* reserved for future use */
37837+ if (err > 0)
37838+ fsnotify_access(file->f_path.dentry);
37839+#endif
37840+
37841+ return err;
37842+}
37843+
37844+struct xino_fread_args {
37845+ ssize_t *errp;
37846+ vfs_readf_t func;
37847+ struct file *file;
37848+ void *buf;
37849+ size_t size;
37850+ loff_t *pos;
37851+};
37852+
37853+static void call_xino_fread(void *args)
37854+{
37855+ struct xino_fread_args *a = args;
37856+ *a->errp = xino_fread(a->func, a->file, a->buf, a->size, a->pos);
37857+}
37858+
37859+static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37860+ size_t size, loff_t *pos)
37861+{
37862+ ssize_t err;
37863+ int wkq_err;
37864+ struct xino_fread_args args = {
37865+ .errp = &err,
37866+ .func = func,
37867+ .file = file,
37868+ .buf = buf,
37869+ .size = size,
37870+ .pos = pos
37871+ };
37872+
37873+ wkq_err = au_wkq_wait(call_xino_fread, &args);
37874+ if (unlikely(wkq_err))
37875+ err = wkq_err;
37876+
37877+ return err;
37878+}
37879+
37880+static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37881+ size_t size, loff_t *pos);
37882+
37883+static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
37884+ size_t size, loff_t *pos)
37885+{
37886+ ssize_t err;
37887+ mm_segment_t oldfs;
37888+ union {
37889+ void *k;
37890+ const char __user *u;
37891+ } buf;
37892+ int i;
37893+ const int prevent_endless = 10;
37894+
37895+ i = 0;
37896+ buf.k = kbuf;
37897+ oldfs = get_fs();
37898+ set_fs(KERNEL_DS);
37899+ do {
37900+ err = func(file, buf.u, size, pos);
37901+ if (err == -EINTR
37902+ && !au_wkq_test()
37903+ && fatal_signal_pending(current)) {
37904+ set_fs(oldfs);
37905+ err = xino_fwrite_wkq(func, file, kbuf, size, pos);
37906+ BUG_ON(err == -EINTR);
37907+ oldfs = get_fs();
37908+ set_fs(KERNEL_DS);
37909+ }
37910+ } while (i++ < prevent_endless
37911+ && (err == -EAGAIN || err == -EINTR));
37912+ set_fs(oldfs);
37913+
37914+#if 0 /* reserved for future use */
37915+ if (err > 0)
37916+ fsnotify_modify(file->f_path.dentry);
37917+#endif
37918+
37919+ return err;
37920+}
37921+
37922+struct do_xino_fwrite_args {
37923+ ssize_t *errp;
37924+ vfs_writef_t func;
37925+ struct file *file;
37926+ void *buf;
37927+ size_t size;
37928+ loff_t *pos;
37929+};
37930+
37931+static void call_do_xino_fwrite(void *args)
37932+{
37933+ struct do_xino_fwrite_args *a = args;
37934+ *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
37935+}
37936+
37937+static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37938+ size_t size, loff_t *pos)
37939+{
37940+ ssize_t err;
37941+ int wkq_err;
37942+ struct do_xino_fwrite_args args = {
37943+ .errp = &err,
37944+ .func = func,
37945+ .file = file,
37946+ .buf = buf,
37947+ .size = size,
37948+ .pos = pos
37949+ };
37950+
37951+ /*
37952+ * it breaks RLIMIT_FSIZE and normal user's limit,
37953+ * users should care about quota and real 'filesystem full.'
37954+ */
37955+ wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
37956+ if (unlikely(wkq_err))
37957+ err = wkq_err;
37958+
37959+ return err;
37960+}
37961+
37962+ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
37963+ size_t size, loff_t *pos)
37964+{
37965+ ssize_t err;
37966+
37967+ if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
37968+ lockdep_off();
37969+ err = do_xino_fwrite(func, file, buf, size, pos);
37970+ lockdep_on();
37971+ } else {
37972+ lockdep_off();
37973+ err = xino_fwrite_wkq(func, file, buf, size, pos);
37974+ lockdep_on();
37975+ }
37976+
37977+ return err;
37978+}
37979+
37980+/* ---------------------------------------------------------------------- */
37981+
37982+/*
37983+ * inode number bitmap
37984+ */
1facf9fc 37985+static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
37986+static ino_t xib_calc_ino(unsigned long pindex, int bit)
37987+{
37988+ ino_t ino;
37989+
37990+ AuDebugOn(bit < 0 || page_bits <= bit);
37991+ ino = AUFS_FIRST_INO + pindex * page_bits + bit;
37992+ return ino;
37993+}
37994+
37995+static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
37996+{
37997+ AuDebugOn(ino < AUFS_FIRST_INO);
37998+ ino -= AUFS_FIRST_INO;
37999+ *pindex = ino / page_bits;
38000+ *bit = ino % page_bits;
38001+}
38002+
38003+static int xib_pindex(struct super_block *sb, unsigned long pindex)
38004+{
38005+ int err;
38006+ loff_t pos;
38007+ ssize_t sz;
38008+ struct au_sbinfo *sbinfo;
38009+ struct file *xib;
38010+ unsigned long *p;
38011+
38012+ sbinfo = au_sbi(sb);
38013+ MtxMustLock(&sbinfo->si_xib_mtx);
38014+ AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
38015+ || !au_opt_test(sbinfo->si_mntflags, XINO));
38016+
38017+ if (pindex == sbinfo->si_xib_last_pindex)
38018+ return 0;
38019+
38020+ xib = sbinfo->si_xib;
38021+ p = sbinfo->si_xib_buf;
38022+ pos = sbinfo->si_xib_last_pindex;
38023+ pos *= PAGE_SIZE;
38024+ sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
38025+ if (unlikely(sz != PAGE_SIZE))
38026+ goto out;
38027+
38028+ pos = pindex;
38029+ pos *= PAGE_SIZE;
c06a8ce3 38030+ if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
1facf9fc 38031+ sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
38032+ else {
38033+ memset(p, 0, PAGE_SIZE);
38034+ sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
38035+ }
38036+ if (sz == PAGE_SIZE) {
38037+ sbinfo->si_xib_last_pindex = pindex;
38038+ return 0; /* success */
38039+ }
38040+
4f0767ce 38041+out:
b752ccd1
AM
38042+ AuIOErr1("write failed (%zd)\n", sz);
38043+ err = sz;
38044+ if (sz >= 0)
38045+ err = -EIO;
38046+ return err;
38047+}
38048+
b752ccd1
AM
38049+static void au_xib_clear_bit(struct inode *inode)
38050+{
38051+ int err, bit;
38052+ unsigned long pindex;
38053+ struct super_block *sb;
38054+ struct au_sbinfo *sbinfo;
38055+
38056+ AuDebugOn(inode->i_nlink);
38057+
38058+ sb = inode->i_sb;
38059+ xib_calc_bit(inode->i_ino, &pindex, &bit);
38060+ AuDebugOn(page_bits <= bit);
38061+ sbinfo = au_sbi(sb);
38062+ mutex_lock(&sbinfo->si_xib_mtx);
38063+ err = xib_pindex(sb, pindex);
38064+ if (!err) {
38065+ clear_bit(bit, sbinfo->si_xib_buf);
38066+ sbinfo->si_xib_next_bit = bit;
38067+ }
38068+ mutex_unlock(&sbinfo->si_xib_mtx);
38069+}
38070+
1facf9fc 38071+/* ---------------------------------------------------------------------- */
38072+
1facf9fc 38073+/*
062440b3 38074+ * truncate a xino bitmap file
1facf9fc 38075+ */
1facf9fc 38076+
38077+/* todo: slow */
38078+static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
38079+{
38080+ int err, bit;
38081+ ssize_t sz;
38082+ unsigned long pindex;
38083+ loff_t pos, pend;
38084+ struct au_sbinfo *sbinfo;
5527c038 38085+ vfs_readf_t func;
1facf9fc 38086+ ino_t *ino;
38087+ unsigned long *p;
38088+
38089+ err = 0;
38090+ sbinfo = au_sbi(sb);
dece6358 38091+ MtxMustLock(&sbinfo->si_xib_mtx);
1facf9fc 38092+ p = sbinfo->si_xib_buf;
38093+ func = sbinfo->si_xread;
c06a8ce3 38094+ pend = vfsub_f_size_read(file);
1facf9fc 38095+ pos = 0;
38096+ while (pos < pend) {
38097+ sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
38098+ err = sz;
38099+ if (unlikely(sz <= 0))
38100+ goto out;
38101+
38102+ err = 0;
38103+ for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
38104+ if (unlikely(*ino < AUFS_FIRST_INO))
38105+ continue;
38106+
38107+ xib_calc_bit(*ino, &pindex, &bit);
38108+ AuDebugOn(page_bits <= bit);
38109+ err = xib_pindex(sb, pindex);
38110+ if (!err)
38111+ set_bit(bit, p);
38112+ else
38113+ goto out;
38114+ }
38115+ }
38116+
4f0767ce 38117+out:
1facf9fc 38118+ return err;
38119+}
38120+
38121+static int xib_restore(struct super_block *sb)
38122+{
acd2b654
AM
38123+ int err, i;
38124+ unsigned int nfile;
5afbbe0d 38125+ aufs_bindex_t bindex, bbot;
1facf9fc 38126+ void *page;
062440b3 38127+ struct au_branch *br;
acd2b654
AM
38128+ struct au_xino *xi;
38129+ struct file *file;
1facf9fc 38130+
38131+ err = -ENOMEM;
38132+ page = (void *)__get_free_page(GFP_NOFS);
38133+ if (unlikely(!page))
38134+ goto out;
38135+
38136+ err = 0;
5afbbe0d
AM
38137+ bbot = au_sbbot(sb);
38138+ for (bindex = 0; !err && bindex <= bbot; bindex++)
062440b3
AM
38139+ if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
38140+ br = au_sbr(sb, bindex);
acd2b654
AM
38141+ xi = br->br_xino;
38142+ nfile = xi->xi_nfile;
38143+ for (i = 0; i < nfile; i++) {
38144+ file = au_xino_file(xi, i);
38145+ if (file)
38146+ err = do_xib_restore(sb, file, page);
38147+ }
062440b3
AM
38148+ } else
38149+ AuDbg("skip shared b%d\n", bindex);
1c60b727 38150+ free_page((unsigned long)page);
1facf9fc 38151+
4f0767ce 38152+out:
1facf9fc 38153+ return err;
38154+}
38155+
38156+int au_xib_trunc(struct super_block *sb)
38157+{
38158+ int err;
38159+ ssize_t sz;
38160+ loff_t pos;
1facf9fc 38161+ struct au_sbinfo *sbinfo;
38162+ unsigned long *p;
38163+ struct file *file;
38164+
dece6358
AM
38165+ SiMustWriteLock(sb);
38166+
1facf9fc 38167+ err = 0;
38168+ sbinfo = au_sbi(sb);
38169+ if (!au_opt_test(sbinfo->si_mntflags, XINO))
38170+ goto out;
38171+
38172+ file = sbinfo->si_xib;
c06a8ce3 38173+ if (vfsub_f_size_read(file) <= PAGE_SIZE)
1facf9fc 38174+ goto out;
38175+
062440b3 38176+ file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
1facf9fc 38177+ err = PTR_ERR(file);
38178+ if (IS_ERR(file))
38179+ goto out;
38180+ fput(sbinfo->si_xib);
38181+ sbinfo->si_xib = file;
38182+
38183+ p = sbinfo->si_xib_buf;
38184+ memset(p, 0, PAGE_SIZE);
38185+ pos = 0;
38186+ sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
38187+ if (unlikely(sz != PAGE_SIZE)) {
38188+ err = sz;
38189+ AuIOErr("err %d\n", err);
38190+ if (sz >= 0)
38191+ err = -EIO;
38192+ goto out;
38193+ }
38194+
38195+ mutex_lock(&sbinfo->si_xib_mtx);
38196+ /* mnt_want_write() is unnecessary here */
38197+ err = xib_restore(sb);
38198+ mutex_unlock(&sbinfo->si_xib_mtx);
38199+
38200+out:
38201+ return err;
38202+}
38203+
38204+/* ---------------------------------------------------------------------- */
38205+
acd2b654 38206+struct au_xino *au_xino_alloc(unsigned int nfile)
062440b3
AM
38207+{
38208+ struct au_xino *xi;
38209+
38210+ xi = kzalloc(sizeof(*xi), GFP_NOFS);
38211+ if (unlikely(!xi))
38212+ goto out;
acd2b654
AM
38213+ xi->xi_nfile = nfile;
38214+ xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38215+ if (unlikely(!xi->xi_file))
38216+ goto out_free;
062440b3
AM
38217+
38218+ xi->xi_nondir.total = 8; /* initial size */
38219+ xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38220+ GFP_NOFS);
38221+ if (unlikely(!xi->xi_nondir.array))
acd2b654 38222+ goto out_file;
062440b3
AM
38223+
38224+ spin_lock_init(&xi->xi_nondir.spin);
38225+ init_waitqueue_head(&xi->xi_nondir.wqh);
acd2b654 38226+ mutex_init(&xi->xi_mtx);
9f237c51 38227+ INIT_HLIST_BL_HEAD(&xi->xi_writing);
062440b3
AM
38228+ atomic_set(&xi->xi_truncating, 0);
38229+ kref_init(&xi->xi_kref);
38230+ goto out; /* success */
38231+
acd2b654 38232+out_file:
9f237c51 38233+ au_kfree_try_rcu(xi->xi_file);
062440b3 38234+out_free:
9f237c51 38235+ au_kfree_rcu(xi);
062440b3
AM
38236+ xi = NULL;
38237+out:
38238+ return xi;
38239+}
38240+
acd2b654 38241+static int au_xino_init(struct au_branch *br, int idx, struct file *file)
062440b3
AM
38242+{
38243+ int err;
38244+ struct au_xino *xi;
38245+
38246+ err = 0;
acd2b654 38247+ xi = au_xino_alloc(idx + 1);
062440b3
AM
38248+ if (unlikely(!xi)) {
38249+ err = -ENOMEM;
38250+ goto out;
38251+ }
38252+
acd2b654
AM
38253+ if (file)
38254+ get_file(file);
38255+ xi->xi_file[idx] = file;
062440b3
AM
38256+ AuDebugOn(br->br_xino);
38257+ br->br_xino = xi;
38258+
38259+out:
38260+ return err;
38261+}
38262+
38263+static void au_xino_release(struct kref *kref)
38264+{
38265+ struct au_xino *xi;
38266+ int i;
9f237c51
AM
38267+ unsigned long ul;
38268+ struct hlist_bl_head *hbl;
38269+ struct hlist_bl_node *pos, *n;
38270+ struct au_xi_writing *p;
062440b3
AM
38271+
38272+ xi = container_of(kref, struct au_xino, xi_kref);
acd2b654
AM
38273+ for (i = 0; i < xi->xi_nfile; i++)
38274+ if (xi->xi_file[i])
38275+ fput(xi->xi_file[i]);
062440b3
AM
38276+ for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38277+ AuDebugOn(xi->xi_nondir.array[i]);
acd2b654 38278+ mutex_destroy(&xi->xi_mtx);
9f237c51
AM
38279+ hbl = &xi->xi_writing;
38280+ ul = au_hbl_count(hbl);
38281+ if (unlikely(ul)) {
38282+ pr_warn("xi_writing %lu\n", ul);
38283+ hlist_bl_lock(hbl);
38284+ hlist_bl_for_each_entry_safe (p, pos, n, hbl, node) {
38285+ hlist_bl_del(&p->node);
38286+ au_kfree_rcu(p);
38287+ }
38288+ hlist_bl_unlock(hbl);
38289+ }
38290+ au_kfree_try_rcu(xi->xi_file);
38291+ au_kfree_try_rcu(xi->xi_nondir.array);
38292+ au_kfree_rcu(xi);
062440b3
AM
38293+}
38294+
38295+int au_xino_put(struct au_branch *br)
38296+{
38297+ int ret;
38298+ struct au_xino *xi;
38299+
38300+ ret = 0;
38301+ xi = br->br_xino;
38302+ if (xi) {
38303+ br->br_xino = NULL;
38304+ ret = kref_put(&xi->xi_kref, au_xino_release);
38305+ }
38306+
38307+ return ret;
38308+}
38309+
062440b3
AM
38310+/* ---------------------------------------------------------------------- */
38311+
1facf9fc 38312+/*
38313+ * xino mount option handlers
38314+ */
1facf9fc 38315+
38316+/* xino bitmap */
38317+static void xino_clear_xib(struct super_block *sb)
38318+{
38319+ struct au_sbinfo *sbinfo;
38320+
dece6358
AM
38321+ SiMustWriteLock(sb);
38322+
1facf9fc 38323+ sbinfo = au_sbi(sb);
062440b3 38324+ /* unnecessary to clear sbinfo->si_xread and ->si_xwrite */
1facf9fc 38325+ if (sbinfo->si_xib)
38326+ fput(sbinfo->si_xib);
38327+ sbinfo->si_xib = NULL;
f0c0a007 38328+ if (sbinfo->si_xib_buf)
1c60b727 38329+ free_page((unsigned long)sbinfo->si_xib_buf);
1facf9fc 38330+ sbinfo->si_xib_buf = NULL;
38331+}
38332+
062440b3 38333+static int au_xino_set_xib(struct super_block *sb, struct path *path)
1facf9fc 38334+{
38335+ int err;
38336+ loff_t pos;
38337+ struct au_sbinfo *sbinfo;
38338+ struct file *file;
acd2b654 38339+ struct super_block *xi_sb;
1facf9fc 38340+
dece6358
AM
38341+ SiMustWriteLock(sb);
38342+
1facf9fc 38343+ sbinfo = au_sbi(sb);
062440b3 38344+ file = au_xino_create2(sb, path, sbinfo->si_xib);
1facf9fc 38345+ err = PTR_ERR(file);
38346+ if (IS_ERR(file))
38347+ goto out;
38348+ if (sbinfo->si_xib)
38349+ fput(sbinfo->si_xib);
38350+ sbinfo->si_xib = file;
5527c038
JR
38351+ sbinfo->si_xread = vfs_readf(file);
38352+ sbinfo->si_xwrite = vfs_writef(file);
acd2b654
AM
38353+ xi_sb = file_inode(file)->i_sb;
38354+ sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38355+ if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38356+ err = -EIO;
38357+ pr_err("s_maxbytes(%llu) on %s is too small\n",
38358+ (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38359+ goto out_unset;
38360+ }
38361+ sbinfo->si_ximaxent /= sizeof(ino_t);
1facf9fc 38362+
38363+ err = -ENOMEM;
38364+ if (!sbinfo->si_xib_buf)
38365+ sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38366+ if (unlikely(!sbinfo->si_xib_buf))
38367+ goto out_unset;
38368+
38369+ sbinfo->si_xib_last_pindex = 0;
38370+ sbinfo->si_xib_next_bit = 0;
c06a8ce3 38371+ if (vfsub_f_size_read(file) < PAGE_SIZE) {
1facf9fc 38372+ pos = 0;
38373+ err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
38374+ PAGE_SIZE, &pos);
38375+ if (unlikely(err != PAGE_SIZE))
38376+ goto out_free;
38377+ }
38378+ err = 0;
38379+ goto out; /* success */
38380+
4f0767ce 38381+out_free:
f0c0a007 38382+ if (sbinfo->si_xib_buf)
1c60b727 38383+ free_page((unsigned long)sbinfo->si_xib_buf);
b752ccd1
AM
38384+ sbinfo->si_xib_buf = NULL;
38385+ if (err >= 0)
38386+ err = -EIO;
4f0767ce 38387+out_unset:
b752ccd1
AM
38388+ fput(sbinfo->si_xib);
38389+ sbinfo->si_xib = NULL;
4f0767ce 38390+out:
062440b3 38391+ AuTraceErr(err);
b752ccd1 38392+ return err;
1facf9fc 38393+}
38394+
b752ccd1
AM
38395+/* xino for each branch */
38396+static void xino_clear_br(struct super_block *sb)
38397+{
5afbbe0d 38398+ aufs_bindex_t bindex, bbot;
b752ccd1 38399+ struct au_branch *br;
1facf9fc 38400+
5afbbe0d
AM
38401+ bbot = au_sbbot(sb);
38402+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1 38403+ br = au_sbr(sb, bindex);
062440b3
AM
38404+ AuDebugOn(!br);
38405+ au_xino_put(br);
38406+ }
38407+}
38408+
38409+static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38410+ aufs_bindex_t bshared)
38411+{
38412+ struct au_branch *brshared;
b752ccd1 38413+
062440b3
AM
38414+ brshared = au_sbr(sb, bshared);
38415+ AuDebugOn(!brshared->br_xino);
38416+ AuDebugOn(!brshared->br_xino->xi_file);
38417+ if (br->br_xino != brshared->br_xino) {
38418+ au_xino_get(brshared);
38419+ au_xino_put(br);
38420+ br->br_xino = brshared->br_xino;
b752ccd1
AM
38421+ }
38422+}
38423+
062440b3
AM
38424+struct au_xino_do_set_br {
38425+ vfs_writef_t writef;
38426+ struct au_branch *br;
38427+ ino_t h_ino;
38428+ aufs_bindex_t bshared;
38429+};
38430+
38431+static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38432+ struct au_xino_do_set_br *args)
1facf9fc 38433+{
38434+ int err;
acd2b654 38435+ struct au_xi_calc calc;
062440b3 38436+ struct file *file;
acd2b654
AM
38437+ struct au_branch *br;
38438+ struct au_xi_new xinew = {
38439+ .base = path
38440+ };
062440b3 38441+
acd2b654
AM
38442+ br = args->br;
38443+ xinew.xi = br->br_xino;
38444+ au_xi_calc(sb, args->h_ino, &calc);
38445+ xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38446+ if (args->bshared >= 0)
062440b3 38447+ /* shared xino */
acd2b654
AM
38448+ au_xino_set_br_shared(sb, br, args->bshared);
38449+ else if (!xinew.xi) {
38450+ /* new xino */
38451+ err = au_xino_init(br, calc.idx, xinew.copy_src);
38452+ if (unlikely(err))
38453+ goto out;
062440b3
AM
38454+ }
38455+
acd2b654
AM
38456+ /* force re-creating */
38457+ xinew.xi = br->br_xino;
38458+ xinew.idx = calc.idx;
38459+ mutex_lock(&xinew.xi->xi_mtx);
38460+ file = au_xi_new(sb, &xinew);
38461+ mutex_unlock(&xinew.xi->xi_mtx);
062440b3
AM
38462+ err = PTR_ERR(file);
38463+ if (IS_ERR(file))
38464+ goto out;
acd2b654
AM
38465+ AuDebugOn(!file);
38466+
38467+ err = au_xino_do_write(args->writef, file, &calc, AUFS_ROOT_INO);
38468+ if (unlikely(err))
38469+ au_xino_put(br);
062440b3 38470+
062440b3
AM
38471+out:
38472+ AuTraceErr(err);
38473+ return err;
38474+}
38475+
38476+static int au_xino_set_br(struct super_block *sb, struct path *path)
38477+{
38478+ int err;
38479+ aufs_bindex_t bindex, bbot;
38480+ struct au_xino_do_set_br args;
b752ccd1 38481+ struct inode *inode;
1facf9fc 38482+
b752ccd1
AM
38483+ SiMustWriteLock(sb);
38484+
5afbbe0d 38485+ bbot = au_sbbot(sb);
5527c038 38486+ inode = d_inode(sb->s_root);
062440b3
AM
38487+ args.writef = au_sbi(sb)->si_xwrite;
38488+ for (bindex = 0; bindex <= bbot; bindex++) {
38489+ args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38490+ args.br = au_sbr(sb, bindex);
38491+ args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38492+ err = au_xino_do_set_br(sb, path, &args);
b752ccd1 38493+ if (unlikely(err))
062440b3 38494+ break;
b752ccd1 38495+ }
1facf9fc 38496+
062440b3 38497+ AuTraceErr(err);
1facf9fc 38498+ return err;
38499+}
b752ccd1
AM
38500+
38501+void au_xino_clr(struct super_block *sb)
38502+{
38503+ struct au_sbinfo *sbinfo;
38504+
38505+ au_xigen_clr(sb);
38506+ xino_clear_xib(sb);
38507+ xino_clear_br(sb);
062440b3 38508+ dbgaufs_brs_del(sb, 0);
b752ccd1
AM
38509+ sbinfo = au_sbi(sb);
38510+ /* lvalue, do not call au_mntflags() */
38511+ au_opt_clr(sbinfo->si_mntflags, XINO);
38512+}
38513+
062440b3 38514+int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
b752ccd1
AM
38515+{
38516+ int err, skip;
062440b3 38517+ struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
b752ccd1
AM
38518+ struct qstr *dname, *cur_name;
38519+ struct file *cur_xino;
b752ccd1 38520+ struct au_sbinfo *sbinfo;
062440b3 38521+ struct path *path, *cur_path;
b752ccd1
AM
38522+
38523+ SiMustWriteLock(sb);
38524+
38525+ err = 0;
38526+ sbinfo = au_sbi(sb);
062440b3
AM
38527+ path = &xiopt->file->f_path;
38528+ dentry = path->dentry;
38529+ parent = dget_parent(dentry);
b752ccd1
AM
38530+ if (remount) {
38531+ skip = 0;
b752ccd1
AM
38532+ cur_xino = sbinfo->si_xib;
38533+ if (cur_xino) {
062440b3
AM
38534+ cur_path = &cur_xino->f_path;
38535+ cur_dentry = cur_path->dentry;
38536+ cur_parent = dget_parent(cur_dentry);
38537+ cur_name = &cur_dentry->d_name;
38538+ dname = &dentry->d_name;
b752ccd1 38539+ skip = (cur_parent == parent
38d290e6 38540+ && au_qstreq(dname, cur_name));
b752ccd1
AM
38541+ dput(cur_parent);
38542+ }
38543+ if (skip)
38544+ goto out;
38545+ }
38546+
38547+ au_opt_set(sbinfo->si_mntflags, XINO);
062440b3
AM
38548+ err = au_xino_set_xib(sb, path);
38549+ /* si_x{read,write} are set */
b752ccd1 38550+ if (!err)
062440b3 38551+ err = au_xigen_set(sb, path);
b752ccd1 38552+ if (!err)
062440b3
AM
38553+ err = au_xino_set_br(sb, path);
38554+ if (!err) {
38555+ dbgaufs_brs_add(sb, 0, /*topdown*/1);
b752ccd1 38556+ goto out; /* success */
062440b3 38557+ }
b752ccd1
AM
38558+
38559+ /* reset all */
062440b3
AM
38560+ AuIOErr("failed setting xino(%d).\n", err);
38561+ au_xino_clr(sb);
b752ccd1 38562+
4f0767ce 38563+out:
b752ccd1
AM
38564+ dput(parent);
38565+ return err;
38566+}
38567+
b752ccd1
AM
38568+/*
38569+ * create a xinofile at the default place/path.
38570+ */
38571+struct file *au_xino_def(struct super_block *sb)
38572+{
38573+ struct file *file;
38574+ char *page, *p;
38575+ struct au_branch *br;
38576+ struct super_block *h_sb;
38577+ struct path path;
5afbbe0d 38578+ aufs_bindex_t bbot, bindex, bwr;
b752ccd1
AM
38579+
38580+ br = NULL;
5afbbe0d 38581+ bbot = au_sbbot(sb);
b752ccd1 38582+ bwr = -1;
5afbbe0d 38583+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1
AM
38584+ br = au_sbr(sb, bindex);
38585+ if (au_br_writable(br->br_perm)
86dc4139 38586+ && !au_test_fs_bad_xino(au_br_sb(br))) {
b752ccd1
AM
38587+ bwr = bindex;
38588+ break;
38589+ }
38590+ }
38591+
7f207e10
AM
38592+ if (bwr >= 0) {
38593+ file = ERR_PTR(-ENOMEM);
537831f9 38594+ page = (void *)__get_free_page(GFP_NOFS);
7f207e10
AM
38595+ if (unlikely(!page))
38596+ goto out;
86dc4139 38597+ path.mnt = au_br_mnt(br);
7f207e10
AM
38598+ path.dentry = au_h_dptr(sb->s_root, bwr);
38599+ p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38600+ file = (void *)p;
38601+ if (!IS_ERR(p)) {
38602+ strcat(p, "/" AUFS_XINO_FNAME);
38603+ AuDbg("%s\n", p);
38604+ file = au_xino_create(sb, p, /*silent*/0);
7f207e10 38605+ }
1c60b727 38606+ free_page((unsigned long)page);
7f207e10
AM
38607+ } else {
38608+ file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
38609+ if (IS_ERR(file))
38610+ goto out;
2000de60 38611+ h_sb = file->f_path.dentry->d_sb;
7f207e10
AM
38612+ if (unlikely(au_test_fs_bad_xino(h_sb))) {
38613+ pr_err("xino doesn't support %s(%s)\n",
38614+ AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38615+ fput(file);
38616+ file = ERR_PTR(-EINVAL);
38617+ }
7f207e10 38618+ }
0c5527e5 38619+
7f207e10
AM
38620+out:
38621+ return file;
38622+}
38623+
38624+/* ---------------------------------------------------------------------- */
38625+
062440b3
AM
38626+/*
38627+ * initialize the xinofile for the specified branch @br
38628+ * at the place/path where @base_file indicates.
38629+ * test whether another branch is on the same filesystem or not,
38630+ * if found then share the xinofile with another branch.
38631+ */
38632+int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38633+ struct path *base)
7f207e10
AM
38634+{
38635+ int err;
062440b3
AM
38636+ struct au_xino_do_set_br args = {
38637+ .h_ino = h_ino,
38638+ .br = br
38639+ };
7f207e10 38640+
062440b3
AM
38641+ args.writef = au_sbi(sb)->si_xwrite;
38642+ args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38643+ au_br_sb(br));
38644+ err = au_xino_do_set_br(sb, base, &args);
79b8bda9 38645+ if (unlikely(err))
062440b3 38646+ au_xino_put(br);
7f207e10 38647+
7f207e10
AM
38648+ return err;
38649+}
521ced18
JR
38650+
38651+/* ---------------------------------------------------------------------- */
38652+
062440b3
AM
38653+/*
38654+ * get an unused inode number from bitmap
38655+ */
38656+ino_t au_xino_new_ino(struct super_block *sb)
38657+{
38658+ ino_t ino;
38659+ unsigned long *p, pindex, ul, pend;
38660+ struct au_sbinfo *sbinfo;
38661+ struct file *file;
38662+ int free_bit, err;
38663+
38664+ if (!au_opt_test(au_mntflags(sb), XINO))
38665+ return iunique(sb, AUFS_FIRST_INO);
38666+
38667+ sbinfo = au_sbi(sb);
38668+ mutex_lock(&sbinfo->si_xib_mtx);
38669+ p = sbinfo->si_xib_buf;
38670+ free_bit = sbinfo->si_xib_next_bit;
38671+ if (free_bit < page_bits && !test_bit(free_bit, p))
38672+ goto out; /* success */
38673+ free_bit = find_first_zero_bit(p, page_bits);
38674+ if (free_bit < page_bits)
38675+ goto out; /* success */
38676+
38677+ pindex = sbinfo->si_xib_last_pindex;
38678+ for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38679+ err = xib_pindex(sb, ul);
38680+ if (unlikely(err))
38681+ goto out_err;
38682+ free_bit = find_first_zero_bit(p, page_bits);
38683+ if (free_bit < page_bits)
38684+ goto out; /* success */
38685+ }
38686+
38687+ file = sbinfo->si_xib;
38688+ pend = vfsub_f_size_read(file) / PAGE_SIZE;
38689+ for (ul = pindex + 1; ul <= pend; ul++) {
38690+ err = xib_pindex(sb, ul);
38691+ if (unlikely(err))
38692+ goto out_err;
38693+ free_bit = find_first_zero_bit(p, page_bits);
38694+ if (free_bit < page_bits)
38695+ goto out; /* success */
38696+ }
38697+ BUG();
38698+
38699+out:
38700+ set_bit(free_bit, p);
38701+ sbinfo->si_xib_next_bit = free_bit + 1;
38702+ pindex = sbinfo->si_xib_last_pindex;
38703+ mutex_unlock(&sbinfo->si_xib_mtx);
38704+ ino = xib_calc_ino(pindex, free_bit);
38705+ AuDbg("i%lu\n", (unsigned long)ino);
38706+ return ino;
38707+out_err:
38708+ mutex_unlock(&sbinfo->si_xib_mtx);
38709+ AuDbg("i0\n");
38710+ return 0;
38711+}
38712+
38713+/* for s_op->delete_inode() */
38714+void au_xino_delete_inode(struct inode *inode, const int unlinked)
521ced18 38715+{
062440b3
AM
38716+ int err;
38717+ unsigned int mnt_flags;
38718+ aufs_bindex_t bindex, bbot, bi;
38719+ unsigned char try_trunc;
38720+ struct au_iinfo *iinfo;
38721+ struct super_block *sb;
38722+ struct au_hinode *hi;
38723+ struct inode *h_inode;
38724+ struct au_branch *br;
38725+ vfs_writef_t xwrite;
acd2b654
AM
38726+ struct au_xi_calc calc;
38727+ struct file *file;
521ced18 38728+
062440b3 38729+ AuDebugOn(au_is_bad_inode(inode));
521ced18 38730+
062440b3
AM
38731+ sb = inode->i_sb;
38732+ mnt_flags = au_mntflags(sb);
38733+ if (!au_opt_test(mnt_flags, XINO)
38734+ || inode->i_ino == AUFS_ROOT_INO)
38735+ return;
38736+
38737+ if (unlinked) {
38738+ au_xigen_inc(inode);
38739+ au_xib_clear_bit(inode);
38740+ }
38741+
38742+ iinfo = au_ii(inode);
38743+ bindex = iinfo->ii_btop;
38744+ if (bindex < 0)
38745+ return;
38746+
38747+ xwrite = au_sbi(sb)->si_xwrite;
38748+ try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38749+ hi = au_hinode(iinfo, bindex);
38750+ bbot = iinfo->ii_bbot;
38751+ for (; bindex <= bbot; bindex++, hi++) {
38752+ h_inode = hi->hi_inode;
38753+ if (!h_inode
38754+ || (!unlinked && h_inode->i_nlink))
38755+ continue;
38756+
38757+ /* inode may not be revalidated */
38758+ bi = au_br_index(sb, hi->hi_id);
38759+ if (bi < 0)
38760+ continue;
38761+
38762+ br = au_sbr(sb, bi);
acd2b654
AM
38763+ au_xi_calc(sb, h_inode->i_ino, &calc);
38764+ file = au_xino_file(br->br_xino, calc.idx);
38765+ if (IS_ERR_OR_NULL(file))
38766+ continue;
38767+
38768+ err = au_xino_do_write(xwrite, file, &calc, /*ino*/0);
062440b3
AM
38769+ if (!err && try_trunc
38770+ && au_test_fs_trunc_xino(au_br_sb(br)))
38771+ xino_try_trunc(sb, br);
38772+ }
521ced18
JR
38773+}
38774+
062440b3
AM
38775+/* ---------------------------------------------------------------------- */
38776+
38777+static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
521ced18
JR
38778+{
38779+ int found, total, i;
38780+
38781+ found = -1;
062440b3 38782+ total = xi->xi_nondir.total;
521ced18 38783+ for (i = 0; i < total; i++) {
062440b3 38784+ if (xi->xi_nondir.array[i] != h_ino)
521ced18
JR
38785+ continue;
38786+ found = i;
38787+ break;
38788+ }
38789+
38790+ return found;
38791+}
38792+
062440b3 38793+static int au_xinondir_expand(struct au_xino *xi)
521ced18
JR
38794+{
38795+ int err, sz;
38796+ ino_t *p;
38797+
38798+ BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38799+
38800+ err = -ENOMEM;
062440b3 38801+ sz = xi->xi_nondir.total * sizeof(ino_t);
521ced18
JR
38802+ if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38803+ goto out;
062440b3 38804+ p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
521ced18
JR
38805+ /*may_shrink*/0);
38806+ if (p) {
062440b3
AM
38807+ xi->xi_nondir.array = p;
38808+ xi->xi_nondir.total <<= 1;
38809+ AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
521ced18
JR
38810+ err = 0;
38811+ }
38812+
38813+out:
38814+ return err;
38815+}
38816+
062440b3
AM
38817+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38818+ ino_t h_ino, int idx)
38819+{
38820+ struct au_xino *xi;
38821+
38822+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38823+ xi = au_sbr(sb, bindex)->br_xino;
38824+ AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38825+
38826+ spin_lock(&xi->xi_nondir.spin);
38827+ AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38828+ xi->xi_nondir.array[idx] = 0;
38829+ spin_unlock(&xi->xi_nondir.spin);
38830+ wake_up_all(&xi->xi_nondir.wqh);
38831+}
38832+
521ced18
JR
38833+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38834+ int *idx)
38835+{
38836+ int err, found, empty;
062440b3 38837+ struct au_xino *xi;
521ced18
JR
38838+
38839+ err = 0;
38840+ *idx = -1;
38841+ if (!au_opt_test(au_mntflags(sb), XINO))
38842+ goto out; /* no xino */
38843+
062440b3 38844+ xi = au_sbr(sb, bindex)->br_xino;
521ced18
JR
38845+
38846+again:
062440b3
AM
38847+ spin_lock(&xi->xi_nondir.spin);
38848+ found = au_xinondir_find(xi, h_ino);
521ced18 38849+ if (found == -1) {
062440b3 38850+ empty = au_xinondir_find(xi, /*h_ino*/0);
521ced18 38851+ if (empty == -1) {
062440b3
AM
38852+ empty = xi->xi_nondir.total;
38853+ err = au_xinondir_expand(xi);
521ced18
JR
38854+ if (unlikely(err))
38855+ goto out_unlock;
38856+ }
062440b3 38857+ xi->xi_nondir.array[empty] = h_ino;
521ced18
JR
38858+ *idx = empty;
38859+ } else {
062440b3
AM
38860+ spin_unlock(&xi->xi_nondir.spin);
38861+ wait_event(xi->xi_nondir.wqh,
38862+ xi->xi_nondir.array[found] != h_ino);
521ced18
JR
38863+ goto again;
38864+ }
38865+
38866+out_unlock:
062440b3
AM
38867+ spin_unlock(&xi->xi_nondir.spin);
38868+out:
38869+ return err;
38870+}
38871+
38872+/* ---------------------------------------------------------------------- */
38873+
38874+int au_xino_path(struct seq_file *seq, struct file *file)
38875+{
38876+ int err;
38877+
38878+ err = au_seq_path(seq, &file->f_path);
38879+ if (unlikely(err))
38880+ goto out;
38881+
38882+#define Deleted "\\040(deleted)"
38883+ seq->count -= sizeof(Deleted) - 1;
38884+ AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38885+ sizeof(Deleted) - 1));
38886+#undef Deleted
38887+
521ced18
JR
38888+out:
38889+ return err;
38890+}
537831f9
AM
38891diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
38892--- /usr/share/empty/include/uapi/linux/aufs_type.h 1970-01-01 01:00:00.000000000 +0100
ba1aed25 38893+++ linux/include/uapi/linux/aufs_type.h 2019-03-05 12:13:00.155891502 +0100
062440b3
AM
38894@@ -0,0 +1,448 @@
38895+/* SPDX-License-Identifier: GPL-2.0 */
7f207e10 38896+/*
ba1aed25 38897+ * Copyright (C) 2005-2019 Junjiro R. Okajima
7f207e10
AM
38898+ *
38899+ * This program, aufs is free software; you can redistribute it and/or modify
38900+ * it under the terms of the GNU General Public License as published by
38901+ * the Free Software Foundation; either version 2 of the License, or
38902+ * (at your option) any later version.
38903+ *
38904+ * This program is distributed in the hope that it will be useful,
38905+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
38906+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38907+ * GNU General Public License for more details.
38908+ *
38909+ * You should have received a copy of the GNU General Public License
523b37e3 38910+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
38911+ */
38912+
38913+#ifndef __AUFS_TYPE_H__
38914+#define __AUFS_TYPE_H__
38915+
f6c5ef8b
AM
38916+#define AUFS_NAME "aufs"
38917+
9dbd164d 38918+#ifdef __KERNEL__
f6c5ef8b
AM
38919+/*
38920+ * define it before including all other headers.
38921+ * sched.h may use pr_* macros before defining "current", so define the
38922+ * no-current version first, and re-define later.
38923+ */
38924+#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
38925+#include <linux/sched.h>
38926+#undef pr_fmt
a2a7ad62
AM
38927+#define pr_fmt(fmt) \
38928+ AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
38929+ (int)sizeof(current->comm), current->comm, current->pid
9dbd164d
AM
38930+#else
38931+#include <stdint.h>
38932+#include <sys/types.h>
f6c5ef8b 38933+#endif /* __KERNEL__ */
7f207e10 38934+
f6c5ef8b
AM
38935+#include <linux/limits.h>
38936+
ba1aed25 38937+#define AUFS_VERSION "4.x-rcN-20190211"
7f207e10
AM
38938+
38939+/* todo? move this to linux-2.6.19/include/magic.h */
38940+#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
38941+
38942+/* ---------------------------------------------------------------------- */
38943+
38944+#ifdef CONFIG_AUFS_BRANCH_MAX_127
9dbd164d 38945+typedef int8_t aufs_bindex_t;
7f207e10
AM
38946+#define AUFS_BRANCH_MAX 127
38947+#else
9dbd164d 38948+typedef int16_t aufs_bindex_t;
7f207e10
AM
38949+#ifdef CONFIG_AUFS_BRANCH_MAX_511
38950+#define AUFS_BRANCH_MAX 511
38951+#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
38952+#define AUFS_BRANCH_MAX 1023
38953+#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
38954+#define AUFS_BRANCH_MAX 32767
38955+#endif
38956+#endif
38957+
38958+#ifdef __KERNEL__
38959+#ifndef AUFS_BRANCH_MAX
38960+#error unknown CONFIG_AUFS_BRANCH_MAX value
38961+#endif
38962+#endif /* __KERNEL__ */
38963+
38964+/* ---------------------------------------------------------------------- */
38965+
7f207e10
AM
38966+#define AUFS_FSTYPE AUFS_NAME
38967+
38968+#define AUFS_ROOT_INO 2
38969+#define AUFS_FIRST_INO 11
38970+
38971+#define AUFS_WH_PFX ".wh."
38972+#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
38973+#define AUFS_WH_TMP_LEN 4
86dc4139 38974+/* a limit for rmdir/rename a dir and copyup */
7f207e10
AM
38975+#define AUFS_MAX_NAMELEN (NAME_MAX \
38976+ - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
38977+ - 1 /* dot */\
38978+ - AUFS_WH_TMP_LEN) /* hex */
38979+#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
38980+#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
392086de
AM
38981+#define AUFS_XINO_DEF_SEC 30 /* seconds */
38982+#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
7f207e10
AM
38983+#define AUFS_DIRWH_DEF 3
38984+#define AUFS_RDCACHE_DEF 10 /* seconds */
027c5e7a 38985+#define AUFS_RDCACHE_MAX 3600 /* seconds */
7f207e10
AM
38986+#define AUFS_RDBLK_DEF 512 /* bytes */
38987+#define AUFS_RDHASH_DEF 32
38988+#define AUFS_WKQ_NAME AUFS_NAME "d"
027c5e7a
AM
38989+#define AUFS_MFS_DEF_SEC 30 /* seconds */
38990+#define AUFS_MFS_MAX_SEC 3600 /* seconds */
076b876e 38991+#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
86dc4139 38992+#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
7f207e10
AM
38993+
38994+/* pseudo-link maintenace under /proc */
38995+#define AUFS_PLINK_MAINT_NAME "plink_maint"
38996+#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
38997+#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
38998+
8b6a4947
AM
38999+/* dirren, renamed dir */
39000+#define AUFS_DR_INFO_PFX AUFS_WH_PFX ".dr."
39001+#define AUFS_DR_BRHINO_NAME AUFS_WH_PFX "hino"
39002+/* whiteouted doubly */
39003+#define AUFS_WH_DR_INFO_PFX AUFS_WH_PFX AUFS_DR_INFO_PFX
39004+#define AUFS_WH_DR_BRHINO AUFS_WH_PFX AUFS_DR_BRHINO_NAME
39005+
7f207e10
AM
39006+#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
39007+#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
39008+
39009+#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
39010+#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
39011+#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
39012+
39013+/* doubly whiteouted */
39014+#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
39015+#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
39016+#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
39017+
1e00d052 39018+/* branch permissions and attributes */
7f207e10
AM
39019+#define AUFS_BRPERM_RW "rw"
39020+#define AUFS_BRPERM_RO "ro"
39021+#define AUFS_BRPERM_RR "rr"
076b876e
AM
39022+#define AUFS_BRATTR_COO_REG "coo_reg"
39023+#define AUFS_BRATTR_COO_ALL "coo_all"
39024+#define AUFS_BRATTR_FHSM "fhsm"
39025+#define AUFS_BRATTR_UNPIN "unpin"
c1595e42
JR
39026+#define AUFS_BRATTR_ICEX "icex"
39027+#define AUFS_BRATTR_ICEX_SEC "icexsec"
39028+#define AUFS_BRATTR_ICEX_SYS "icexsys"
39029+#define AUFS_BRATTR_ICEX_TR "icextr"
39030+#define AUFS_BRATTR_ICEX_USR "icexusr"
39031+#define AUFS_BRATTR_ICEX_OTH "icexoth"
1e00d052
AM
39032+#define AUFS_BRRATTR_WH "wh"
39033+#define AUFS_BRWATTR_NLWH "nolwh"
076b876e
AM
39034+#define AUFS_BRWATTR_MOO "moo"
39035+
39036+#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
39037+#define AuBrPerm_RO (1 << 1) /* readonly */
39038+#define AuBrPerm_RR (1 << 2) /* natively readonly */
39039+#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
39040+
39041+#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
39042+#define AuBrAttr_COO_ALL (1 << 4)
39043+#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
39044+
39045+#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
39046+#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
c1595e42
JR
39047+ branch. meaningless since
39048+ linux-3.18-rc1 */
39049+
39050+/* ignore error in copying XATTR */
39051+#define AuBrAttr_ICEX_SEC (1 << 7)
39052+#define AuBrAttr_ICEX_SYS (1 << 8)
39053+#define AuBrAttr_ICEX_TR (1 << 9)
39054+#define AuBrAttr_ICEX_USR (1 << 10)
39055+#define AuBrAttr_ICEX_OTH (1 << 11)
39056+#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
39057+ | AuBrAttr_ICEX_SYS \
39058+ | AuBrAttr_ICEX_TR \
39059+ | AuBrAttr_ICEX_USR \
39060+ | AuBrAttr_ICEX_OTH)
39061+
39062+#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
076b876e
AM
39063+#define AuBrRAttr_Mask AuBrRAttr_WH
39064+
c1595e42
JR
39065+#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
39066+#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
076b876e
AM
39067+#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
39068+
39069+#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
39070+
c1595e42 39071+/* #warning test userspace */
076b876e
AM
39072+#ifdef __KERNEL__
39073+#ifndef CONFIG_AUFS_FHSM
39074+#undef AuBrAttr_FHSM
39075+#define AuBrAttr_FHSM 0
39076+#endif
c1595e42
JR
39077+#ifndef CONFIG_AUFS_XATTR
39078+#undef AuBrAttr_ICEX
39079+#define AuBrAttr_ICEX 0
39080+#undef AuBrAttr_ICEX_SEC
39081+#define AuBrAttr_ICEX_SEC 0
39082+#undef AuBrAttr_ICEX_SYS
39083+#define AuBrAttr_ICEX_SYS 0
39084+#undef AuBrAttr_ICEX_TR
39085+#define AuBrAttr_ICEX_TR 0
39086+#undef AuBrAttr_ICEX_USR
39087+#define AuBrAttr_ICEX_USR 0
39088+#undef AuBrAttr_ICEX_OTH
39089+#define AuBrAttr_ICEX_OTH 0
39090+#endif
076b876e
AM
39091+#endif
39092+
39093+/* the longest combination */
c1595e42
JR
39094+/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
39095+#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
39096+ "+" AUFS_BRATTR_COO_REG \
39097+ "+" AUFS_BRATTR_FHSM \
39098+ "+" AUFS_BRATTR_UNPIN \
7e9cd9fe
AM
39099+ "+" AUFS_BRATTR_ICEX_SEC \
39100+ "+" AUFS_BRATTR_ICEX_SYS \
39101+ "+" AUFS_BRATTR_ICEX_USR \
39102+ "+" AUFS_BRATTR_ICEX_OTH \
076b876e
AM
39103+ "+" AUFS_BRWATTR_NLWH)
39104+
39105+typedef struct {
39106+ char a[AuBrPermStrSz];
39107+} au_br_perm_str_t;
39108+
39109+static inline int au_br_writable(int brperm)
39110+{
39111+ return brperm & AuBrPerm_RW;
39112+}
39113+
39114+static inline int au_br_whable(int brperm)
39115+{
39116+ return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
39117+}
39118+
39119+static inline int au_br_wh_linkable(int brperm)
39120+{
39121+ return !(brperm & AuBrWAttr_NoLinkWH);
39122+}
39123+
39124+static inline int au_br_cmoo(int brperm)
39125+{
39126+ return brperm & AuBrAttr_CMOO_Mask;
39127+}
39128+
39129+static inline int au_br_fhsm(int brperm)
39130+{
39131+ return brperm & AuBrAttr_FHSM;
39132+}
7f207e10
AM
39133+
39134+/* ---------------------------------------------------------------------- */
39135+
39136+/* ioctl */
39137+enum {
39138+ /* readdir in userspace */
39139+ AuCtl_RDU,
39140+ AuCtl_RDU_INO,
39141+
076b876e
AM
39142+ AuCtl_WBR_FD, /* pathconf wrapper */
39143+ AuCtl_IBUSY, /* busy inode */
39144+ AuCtl_MVDOWN, /* move-down */
39145+ AuCtl_BR, /* info about branches */
39146+ AuCtl_FHSM_FD /* connection for fhsm */
7f207e10
AM
39147+};
39148+
39149+/* borrowed from linux/include/linux/kernel.h */
39150+#ifndef ALIGN
39151+#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
39152+#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
39153+#endif
39154+
39155+/* borrowed from linux/include/linux/compiler-gcc3.h */
39156+#ifndef __aligned
39157+#define __aligned(x) __attribute__((aligned(x)))
53392da6
AM
39158+#endif
39159+
39160+#ifdef __KERNEL__
39161+#ifndef __packed
7f207e10
AM
39162+#define __packed __attribute__((packed))
39163+#endif
53392da6 39164+#endif
7f207e10
AM
39165+
39166+struct au_rdu_cookie {
9dbd164d
AM
39167+ uint64_t h_pos;
39168+ int16_t bindex;
39169+ uint8_t flags;
39170+ uint8_t pad;
39171+ uint32_t generation;
7f207e10
AM
39172+} __aligned(8);
39173+
39174+struct au_rdu_ent {
9dbd164d
AM
39175+ uint64_t ino;
39176+ int16_t bindex;
39177+ uint8_t type;
39178+ uint8_t nlen;
39179+ uint8_t wh;
7f207e10
AM
39180+ char name[0];
39181+} __aligned(8);
39182+
39183+static inline int au_rdu_len(int nlen)
39184+{
39185+ /* include the terminating NULL */
39186+ return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
9dbd164d 39187+ sizeof(uint64_t));
7f207e10
AM
39188+}
39189+
39190+union au_rdu_ent_ul {
39191+ struct au_rdu_ent __user *e;
9dbd164d 39192+ uint64_t ul;
7f207e10
AM
39193+};
39194+
39195+enum {
39196+ AufsCtlRduV_SZ,
39197+ AufsCtlRduV_End
39198+};
39199+
39200+struct aufs_rdu {
39201+ /* input */
39202+ union {
9dbd164d
AM
39203+ uint64_t sz; /* AuCtl_RDU */
39204+ uint64_t nent; /* AuCtl_RDU_INO */
7f207e10
AM
39205+ };
39206+ union au_rdu_ent_ul ent;
9dbd164d 39207+ uint16_t verify[AufsCtlRduV_End];
7f207e10
AM
39208+
39209+ /* input/output */
9dbd164d 39210+ uint32_t blk;
7f207e10
AM
39211+
39212+ /* output */
39213+ union au_rdu_ent_ul tail;
39214+ /* number of entries which were added in a single call */
9dbd164d
AM
39215+ uint64_t rent;
39216+ uint8_t full;
39217+ uint8_t shwh;
7f207e10
AM
39218+
39219+ struct au_rdu_cookie cookie;
39220+} __aligned(8);
39221+
1e00d052
AM
39222+/* ---------------------------------------------------------------------- */
39223+
8b6a4947
AM
39224+/* dirren. the branch is identified by the filename who contains this */
39225+struct au_drinfo {
39226+ uint64_t ino;
39227+ union {
39228+ uint8_t oldnamelen;
39229+ uint64_t _padding;
39230+ };
39231+ uint8_t oldname[0];
39232+} __aligned(8);
39233+
39234+struct au_drinfo_fdata {
39235+ uint32_t magic;
39236+ struct au_drinfo drinfo;
39237+} __aligned(8);
39238+
39239+#define AUFS_DRINFO_MAGIC_V1 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39240+/* future */
39241+#define AUFS_DRINFO_MAGIC_V2 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39242+
39243+/* ---------------------------------------------------------------------- */
39244+
1e00d052 39245+struct aufs_wbr_fd {
9dbd164d
AM
39246+ uint32_t oflags;
39247+ int16_t brid;
1e00d052
AM
39248+} __aligned(8);
39249+
39250+/* ---------------------------------------------------------------------- */
39251+
027c5e7a 39252+struct aufs_ibusy {
9dbd164d
AM
39253+ uint64_t ino, h_ino;
39254+ int16_t bindex;
027c5e7a
AM
39255+} __aligned(8);
39256+
1e00d052
AM
39257+/* ---------------------------------------------------------------------- */
39258+
392086de
AM
39259+/* error code for move-down */
39260+/* the actual message strings are implemented in aufs-util.git */
39261+enum {
39262+ EAU_MVDOWN_OPAQUE = 1,
39263+ EAU_MVDOWN_WHITEOUT,
39264+ EAU_MVDOWN_UPPER,
39265+ EAU_MVDOWN_BOTTOM,
39266+ EAU_MVDOWN_NOUPPER,
39267+ EAU_MVDOWN_NOLOWERBR,
39268+ EAU_Last
39269+};
39270+
c2b27bf2 39271+/* flags for move-down */
392086de
AM
39272+#define AUFS_MVDOWN_DMSG 1
39273+#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
39274+#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
39275+#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
39276+#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
39277+#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
39278+#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
39279+#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
39280+#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
076b876e
AM
39281+#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
39282+#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
39283+#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
39284+#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
c2b27bf2 39285+
076b876e 39286+/* index for move-down */
392086de
AM
39287+enum {
39288+ AUFS_MVDOWN_UPPER,
39289+ AUFS_MVDOWN_LOWER,
39290+ AUFS_MVDOWN_NARRAY
39291+};
39292+
076b876e
AM
39293+/*
39294+ * additional info of move-down
39295+ * number of free blocks and inodes.
39296+ * subset of struct kstatfs, but smaller and always 64bit.
39297+ */
39298+struct aufs_stfs {
39299+ uint64_t f_blocks;
39300+ uint64_t f_bavail;
39301+ uint64_t f_files;
39302+ uint64_t f_ffree;
39303+};
39304+
39305+struct aufs_stbr {
39306+ int16_t brid; /* optional input */
39307+ int16_t bindex; /* output */
39308+ struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
39309+} __aligned(8);
39310+
c2b27bf2 39311+struct aufs_mvdown {
076b876e
AM
39312+ uint32_t flags; /* input/output */
39313+ struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39314+ int8_t au_errno; /* output */
39315+} __aligned(8);
39316+
39317+/* ---------------------------------------------------------------------- */
39318+
39319+union aufs_brinfo {
39320+ /* PATH_MAX may differ between kernel-space and user-space */
39321+ char _spacer[4096];
392086de 39322+ struct {
076b876e
AM
39323+ int16_t id;
39324+ int perm;
39325+ char path[0];
39326+ };
c2b27bf2
AM
39327+} __aligned(8);
39328+
39329+/* ---------------------------------------------------------------------- */
39330+
7f207e10
AM
39331+#define AuCtlType 'A'
39332+#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39333+#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
1e00d052
AM
39334+#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
39335+ struct aufs_wbr_fd)
027c5e7a 39336+#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
392086de
AM
39337+#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
39338+ struct aufs_mvdown)
076b876e
AM
39339+#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39340+#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
7f207e10
AM
39341+
39342+#endif /* __AUFS_TYPE_H__ */
2121bcd9 39343SPDX-License-Identifier: GPL-2.0
cd7a4cd9 39344aufs4.x-rcN loopback patch
5527c038
JR
39345
39346diff --git a/drivers/block/loop.c b/drivers/block/loop.c
ba1aed25 39347index a271e17e4b10..ee17c7577224 100644
5527c038
JR
39348--- a/drivers/block/loop.c
39349+++ b/drivers/block/loop.c
9f237c51 39350@@ -625,6 +625,15 @@ static inline void loop_update_dio(struct loop_device *lo)
8b6a4947 39351 lo->use_dio);
5527c038
JR
39352 }
39353
5527c038
JR
39354+static struct file *loop_real_file(struct file *file)
39355+{
39356+ struct file *f = NULL;
39357+
39358+ if (file->f_path.dentry->d_sb->s_op->real_loop)
39359+ f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39360+ return f;
8b6a4947
AM
39361+}
39362+
c2c0f25c 39363 static void loop_reread_partitions(struct loop_device *lo,
8b6a4947
AM
39364 struct block_device *bdev)
39365 {
ba1aed25 39366@@ -678,6 +687,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
5527c038
JR
39367 unsigned int arg)
39368 {
e1106418 39369 struct file *file = NULL, *old_file;
5527c038 39370+ struct file *f, *virt_file = NULL, *old_virt_file;
5527c038 39371 int error;
ba1aed25 39372 bool partscan;
5527c038 39373
ba1aed25 39374@@ -697,12 +707,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
cd7a4cd9
AM
39375 file = fget(arg);
39376 if (!file)
e1106418 39377 goto out_err;
5527c038
JR
39378+ f = loop_real_file(file);
39379+ if (f) {
39380+ virt_file = file;
39381+ file = f;
39382+ get_file(file);
39383+ }
39384
cd7a4cd9
AM
39385 error = loop_validate_file(file, bdev);
39386 if (error)
e1106418 39387 goto out_err;
cd7a4cd9 39388
5527c038
JR
39389 old_file = lo->lo_backing_file;
39390+ old_virt_file = lo->lo_backing_virt_file;
39391
39392 error = -EINVAL;
39393
ba1aed25 39394@@ -714,6 +731,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
8b6a4947
AM
39395 blk_mq_freeze_queue(lo->lo_queue);
39396 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39397 lo->lo_backing_file = file;
39398+ lo->lo_backing_virt_file = virt_file;
39399 lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39400 mapping_set_gfp_mask(file->f_mapping,
39401 lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
ba1aed25 39402@@ -727,6 +745,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
e1106418
JR
39403 * dependency.
39404 */
5527c038
JR
39405 fput(old_file);
39406+ if (old_virt_file)
39407+ fput(old_virt_file);
e1106418 39408 if (partscan)
c2c0f25c 39409 loop_reread_partitions(lo, bdev);
5527c038 39410 return 0;
ba1aed25 39411@@ -735,6 +755,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
e1106418
JR
39412 mutex_unlock(&loop_ctl_mutex);
39413 if (file)
39414 fput(file);
5527c038
JR
39415+ if (virt_file)
39416+ fput(virt_file);
5527c038
JR
39417 return error;
39418 }
e1106418 39419
9f237c51 39420@@ -921,7 +943,7 @@ static int loop_prepare_queue(struct loop_device *lo)
5527c038
JR
39421 static int loop_set_fd(struct loop_device *lo, fmode_t mode,
39422 struct block_device *bdev, unsigned int arg)
39423 {
062440b3 39424- struct file *file;
5527c038
JR
39425+ struct file *file, *f, *virt_file = NULL;
39426 struct inode *inode;
39427 struct address_space *mapping;
8b6a4947 39428 int lo_flags = 0;
ba1aed25 39429@@ -936,6 +958,12 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
39430 file = fget(arg);
39431 if (!file)
39432 goto out;
39433+ f = loop_real_file(file);
39434+ if (f) {
39435+ virt_file = file;
39436+ file = f;
39437+ get_file(file);
39438+ }
39439
ba1aed25
AM
39440 error = mutex_lock_killable(&loop_ctl_mutex);
39441 if (error)
39442@@ -972,6 +1000,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
39443 lo->lo_device = bdev;
39444 lo->lo_flags = lo_flags;
39445 lo->lo_backing_file = file;
39446+ lo->lo_backing_virt_file = virt_file;
39447 lo->transfer = NULL;
39448 lo->ioctl = NULL;
39449 lo->lo_sizelimit = 0;
ba1aed25
AM
39450@@ -1009,6 +1038,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
39451 mutex_unlock(&loop_ctl_mutex);
e1106418 39452 out_putf:
5527c038
JR
39453 fput(file);
39454+ if (virt_file)
39455+ fput(virt_file);
e1106418 39456 out:
5527c038
JR
39457 /* This is safe: open() is still holding a reference. */
39458 module_put(THIS_MODULE);
ba1aed25
AM
39459@@ -1055,6 +1086,7 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
39460 static int __loop_clr_fd(struct loop_device *lo, bool release)
5527c038 39461 {
e1106418 39462 struct file *filp = NULL;
5527c038
JR
39463+ struct file *virt_filp = lo->lo_backing_virt_file;
39464 gfp_t gfp = lo->old_gfp_mask;
39465 struct block_device *bdev = lo->lo_device;
ba1aed25
AM
39466 int err = 0;
39467@@ -1078,6 +1110,7 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
5527c038 39468
5527c038 39469 spin_lock_irq(&lo->lo_lock);
5527c038
JR
39470 lo->lo_backing_file = NULL;
39471+ lo->lo_backing_virt_file = NULL;
39472 spin_unlock_irq(&lo->lo_lock);
39473
39474 loop_release_xfer(lo);
ba1aed25 39475@@ -1146,6 +1179,8 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
5527c038 39476 */
e1106418
JR
39477 if (filp)
39478 fput(filp);
5527c038
JR
39479+ if (virt_filp)
39480+ fput(virt_filp);
e1106418 39481 return err;
5527c038
JR
39482 }
39483
39484diff --git a/drivers/block/loop.h b/drivers/block/loop.h
ba1aed25 39485index af75a5ee4094..1e6ee5a4f623 100644
5527c038
JR
39486--- a/drivers/block/loop.h
39487+++ b/drivers/block/loop.h
39488@@ -46,7 +46,7 @@ struct loop_device {
39489 int (*ioctl)(struct loop_device *, int cmd,
39490 unsigned long arg);
39491
39492- struct file * lo_backing_file;
39493+ struct file * lo_backing_file, *lo_backing_virt_file;
39494 struct block_device *lo_device;
5527c038 39495 void *key_data;
8b6a4947 39496
5527c038 39497diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
ba1aed25 39498index 0309f0d502ff..19feb4f3cb5f 100644
5527c038
JR
39499--- a/fs/aufs/f_op.c
39500+++ b/fs/aufs/f_op.c
acd2b654 39501@@ -359,7 +359,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
5527c038
JR
39502 if (IS_ERR(h_file))
39503 goto out;
39504
39505- if (au_test_loopback_kthread()) {
39506+ if (0 && au_test_loopback_kthread()) {
39507 au_warn_loopback(h_file->f_path.dentry->d_sb);
39508 if (file->f_mapping != h_file->f_mapping) {
39509 file->f_mapping = h_file->f_mapping;
39510diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
ba1aed25 39511index f05887d612fd..6f0505723cb4 100644
5527c038
JR
39512--- a/fs/aufs/loop.c
39513+++ b/fs/aufs/loop.c
2121bcd9 39514@@ -133,3 +133,19 @@ void au_loopback_fin(void)
79b8bda9 39515 symbol_put(loop_backing_file);
9f237c51 39516 au_kfree_try_rcu(au_warn_loopback_array);
5527c038
JR
39517 }
39518+
39519+/* ---------------------------------------------------------------------- */
39520+
39521+/* support the loopback block device insude aufs */
39522+
39523+struct file *aufs_real_loop(struct file *file)
39524+{
39525+ struct file *f;
39526+
39527+ BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39528+ fi_read_lock(file);
39529+ f = au_hf_top(file);
39530+ fi_read_unlock(file);
39531+ AuDebugOn(!f);
39532+ return f;
39533+}
39534diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
ba1aed25 39535index a570730f5482..255e2d3763e7 100644
5527c038
JR
39536--- a/fs/aufs/loop.h
39537+++ b/fs/aufs/loop.h
2121bcd9 39538@@ -26,7 +26,11 @@ void au_warn_loopback(struct super_block *h_sb);
5527c038
JR
39539
39540 int au_loopback_init(void);
39541 void au_loopback_fin(void);
39542+
39543+struct file *aufs_real_loop(struct file *file);
39544 #else
39545+AuStub(struct file *, loop_backing_file, return NULL)
39546+
39547 AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
39548 struct dentry *h_adding)
39549 AuStubInt0(au_test_loopback_kthread, void)
2121bcd9 39550@@ -34,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
5527c038
JR
39551
39552 AuStubInt0(au_loopback_init, void)
39553 AuStubVoid(au_loopback_fin, void)
39554+
39555+AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39556 #endif /* BLK_DEV_LOOP */
39557
39558 #endif /* __KERNEL__ */
39559diff --git a/fs/aufs/super.c b/fs/aufs/super.c
ba1aed25 39560index 7c270b14f8c9..61938c274f05 100644
5527c038
JR
39561--- a/fs/aufs/super.c
39562+++ b/fs/aufs/super.c
acd2b654 39563@@ -845,7 +845,10 @@ static const struct super_operations aufs_sop = {
5527c038
JR
39564 .statfs = aufs_statfs,
39565 .put_super = aufs_put_super,
39566 .sync_fs = aufs_sync_fs,
39567- .remount_fs = aufs_remount_fs
39568+ .remount_fs = aufs_remount_fs,
39569+#ifdef CONFIG_AUFS_BDEV_LOOP
39570+ .real_loop = aufs_real_loop
39571+#endif
39572 };
39573
39574 /* ---------------------------------------------------------------------- */
39575diff --git a/include/linux/fs.h b/include/linux/fs.h
ba1aed25 39576index 5db265e7d35a..8895cd2343e3 100644
5527c038
JR
39577--- a/include/linux/fs.h
39578+++ b/include/linux/fs.h
ba1aed25 39579@@ -1937,6 +1937,10 @@ struct super_operations {
5527c038
JR
39580 struct shrink_control *);
39581 long (*free_cached_objects)(struct super_block *,
39582 struct shrink_control *);
39583+#if defined(CONFIG_BLK_DEV_LOOP) || defined(CONFIG_BLK_DEV_LOOP_MODULE)
39584+ /* and aufs */
39585+ struct file *(*real_loop)(struct file *);
39586+#endif
39587 };
39588
39589 /*
This page took 7.199187 seconds and 4 git commands to generate.