]> git.pld-linux.org Git - packages/kernel.git/blame - kernel-aufs4.patch
- up to 4.19.43; workarounds zombieload intel cpu vulnerability: https://www.kernel...
[packages/kernel.git] / kernel-aufs4.patch
CommitLineData
2121bcd9 1SPDX-License-Identifier: GPL-2.0
e49925d1 2aufs4.19 kbuild patch
7f207e10
AM
3
4diff --git a/fs/Kconfig b/fs/Kconfig
cd7a4cd9 5index ac474a6..284cee9 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
cd7a4cd9 17index 293733f..12d19d0 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
e49925d1 26aufs4.19 base patch
7f207e10 27
c1595e42 28diff --git a/MAINTAINERS b/MAINTAINERS
e49925d1 29index b2f710e..d211619 100644
c1595e42
JR
30--- a/MAINTAINERS
31+++ b/MAINTAINERS
acd2b654 32@@ -2605,6 +2605,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
acd2b654 53index ea9debf..9e534a3 100644
392086de
AM
54--- a/drivers/block/loop.c
55+++ b/drivers/block/loop.c
acd2b654 56@@ -739,6 +739,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
acd2b654 82index 2e7e8d8..9f57bd8 100644
c1595e42
JR
83--- a/fs/dcache.c
84+++ b/fs/dcache.c
acd2b654 85@@ -1238,7 +1238,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
acd2b654 95index 4137d96..c91b3e3 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
acd2b654 117index 42f6d25..fa6ae6a 100644
5afbbe0d
AM
118--- a/fs/inode.c
119+++ b/fs/inode.c
acd2b654 120@@ -1657,7 +1657,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
acd2b654 130index 9918655..72c93f3 100644
8b6a4947
AM
131--- a/fs/namespace.c
132+++ b/fs/namespace.c
acd2b654 133@@ -770,6 +770,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
acd2b654 147index 8a2737f..42f64cc 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
cd7a4cd9 180index b3daa97..1dd7f96 100644
7f207e10
AM
181--- a/fs/splice.c
182+++ b/fs/splice.c
cd7a4cd9 183@@ -838,8 +838,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);
cd7a4cd9 194@@ -855,9 +855,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
b00004a5 208index b54e054..2860782 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
acd2b654 221index 897eae8..7fb92a9 100644
5527c038
JR
222--- a/include/linux/fs.h
223+++ b/include/linux/fs.h
acd2b654 224@@ -1286,6 +1286,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);
acd2b654 232@@ -1747,6 +1748,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);
acd2b654 240@@ -1818,6 +1820,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 *);
acd2b654 253@@ -2243,6 +2251,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;
acd2b654 261@@ -2530,6 +2539,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
acd2b654 270index b0d0b51..f73ffaa 100644
8b6a4947
AM
271--- a/include/linux/lockdep.h
272+++ b/include/linux/lockdep.h
be118d29 273@@ -313,6 +313,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 *
be118d29 282@@ -439,6 +441,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
2121bcd9 291index 3594208..24f5fd1 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
2121bcd9 310index 74b4911..19789fb 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
acd2b654 325index dd13f86..fa6f559 100644
8b6a4947
AM
326--- a/kernel/locking/lockdep.c
327+++ b/kernel/locking/lockdep.c
be118d29 328@@ -140,7 +140,7 @@ static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
8b6a4947
AM
329 unsigned long nr_lock_classes;
330 static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
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 /*
be118d29 337@@ -151,6 +151,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
e49925d1 346aufs4.19 mmap patch
fb47a38f 347
c1595e42 348diff --git a/fs/proc/base.c b/fs/proc/base.c
acd2b654 349index 7e9f07bf..3ab5901 100644
c1595e42
JR
350--- a/fs/proc/base.c
351+++ b/fs/proc/base.c
acd2b654 352@@ -2016,7 +2016,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
cd7a4cd9 362index 3b63be6..fb9913b 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
acd2b654 378index 5ea1d64..7865a470 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;
acd2b654 393@@ -1727,7 +1730,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
acd2b654 403index 0b63d68..400d1c5 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
acd2b654 419index 0416a72..4a298a9 100644
fb47a38f
JR
420--- a/include/linux/mm.h
421+++ b/include/linux/mm.h
acd2b654 422@@ -1440,6 +1440,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
acd2b654 452index 5ed8f62..0122975 100644
fb47a38f
JR
453--- a/include/linux/mm_types.h
454+++ b/include/linux/mm_types.h
acd2b654 455@@ -239,6 +239,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
acd2b654 463@@ -313,6 +314,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
acd2b654 472index f0b5847..fa562c3 100644
fb47a38f
JR
473--- a/kernel/fork.c
474+++ b/kernel/fork.c
acd2b654 475@@ -505,7 +505,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
acd2b654 485index 26ef77a..b2869af 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
496
fb47a38f 497diff --git a/mm/filemap.c b/mm/filemap.c
cd7a4cd9 498index 52517f2..250f675 100644
fb47a38f
JR
499--- a/mm/filemap.c
500+++ b/mm/filemap.c
cd7a4cd9
AM
501@@ -2700,7 +2700,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
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
acd2b654 511index f7cd9cb..515e88a 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;
c4adf169 523@@ -905,7 +905,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);
acd2b654 532@@ -1821,8 +1821,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);
acd2b654 542@@ -2641,7 +2641,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);
acd2b654 551@@ -2660,7 +2660,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));
acd2b654 560@@ -2822,7 +2822,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);
acd2b654 569@@ -2897,10 +2897,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)
acd2b654 598@@ -3206,7 +3223,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
acd2b654 608index e4aac33..b27b200 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
cd7a4cd9 649index 0000000..a27ac36
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+ *
b00004a5 661+ * Copyright (c) 2014-2018 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
e49925d1 740aufs4.19 standalone patch
7f207e10 741
c1595e42 742diff --git a/fs/dcache.c b/fs/dcache.c
acd2b654 743index 9f57bd8..328a136 100644
c1595e42
JR
744--- a/fs/dcache.c
745+++ b/fs/dcache.c
acd2b654 746@@ -1343,6 +1343,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;
acd2b654 754@@ -2837,6 +2838,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
acd2b654 763index 1ebf6e5..a72c294 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
acd2b654 775index c91b3e3..7751309 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
acd2b654 787index e49af4c..569020f 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
acd2b654 815index fa6ae6a..69d4a6c 100644
5afbbe0d
AM
816--- a/fs/inode.c
817+++ b/fs/inode.c
acd2b654 818@@ -1666,6 +1666,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
acd2b654 827index 72c93f3..c49803c 100644
7f207e10
AM
828--- a/fs/namespace.c
829+++ b/fs/namespace.c
acd2b654 830@@ -437,6 +437,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
acd2b654 838@@ -775,6 +776,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
acd2b654 846@@ -1832,6 +1834,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
acd2b654 855index c03b836..817f22c 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
acd2b654 891index 59cdb27..ce365c7 100644
7f207e10
AM
892--- a/fs/notify/mark.c
893+++ b/fs/notify/mark.c
acd2b654 894@@ -263,6 +263,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
acd2b654 902@@ -417,6 +418,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.
acd2b654 910@@ -632,6 +634,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
acd2b654 918@@ -754,6 +757,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
acd2b654 927index 0285ce7..cb81623 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
acd2b654 939index 42f64cc..d9cb969 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
cd7a4cd9 975index 1dd7f96..a5e3bcb 100644
7f207e10
AM
976--- a/fs/splice.c
977+++ b/fs/splice.c
cd7a4cd9 978@@ -851,6 +851,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.
cd7a4cd9 986@@ -880,6 +881,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
b00004a5 995index 2860782..ffd7ea4 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
acd2b654 1007index 0d6a6a4..7ce4701 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
acd2b654 1019index fa6f559..7ac19ef 100644
8b6a4947
AM
1020--- a/kernel/locking/lockdep.c
1021+++ b/kernel/locking/lockdep.c
be118d29 1022@@ -151,6 +151,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
2121bcd9 1031index 0fef395..83fb1ec 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
acd2b654 1040index 2e489d6..1e146da 100644
7f207e10
AM
1041--- a/security/commoncap.c
1042+++ b/security/commoncap.c
cd7a4cd9 1043@@ -1336,12 +1336,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
cd7a4cd9 1059index cd97929..424fd23 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
acd2b654 1076index 736e78d..b314539 100644
7f207e10
AM
1077--- a/security/security.c
1078+++ b/security/security.c
acd2b654 1079@@ -542,6 +542,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 {
acd2b654 1087@@ -558,6 +559,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)
acd2b654 1095@@ -566,6 +568,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,
acd2b654 1103@@ -593,6 +596,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 {
acd2b654 1111@@ -600,6 +604,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 {
acd2b654 1119@@ -607,6 +612,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 {
acd2b654 1127@@ -692,6 +698,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)
acd2b654 1135@@ -707,6 +714,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 {
acd2b654 1143@@ -878,6 +886,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 {
acd2b654 1151@@ -937,6 +946,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
e49925d1 1161+++ linux/Documentation/ABI/testing/debugfs-aufs 2019-01-28 14:36:12.215751270 +0100
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
e49925d1 1220+++ linux/Documentation/ABI/testing/sysfs-aufs 2019-01-28 14:36:12.215751270 +0100
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
e49925d1 1255+++ linux/Documentation/filesystems/aufs/design/01intro.txt 2019-01-28 14:36:12.215751270 +0100
1c60b727 1256@@ -0,0 +1,171 @@
53392da6 1257+
b00004a5 1258+# Copyright (C) 2005-2018 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
e49925d1 1430+++ linux/Documentation/filesystems/aufs/design/02struct.txt 2019-01-28 14:36:12.215751270 +0100
7e9cd9fe 1431@@ -0,0 +1,258 @@
53392da6 1432+
b00004a5 1433+# Copyright (C) 2005-2018 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
e49925d1 1692+++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt 2019-01-28 14:36:12.215751270 +0100
b912730e
AM
1693@@ -0,0 +1,85 @@
1694+
b00004a5 1695+# Copyright (C) 2015-2018 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
e49925d1 1781+++ linux/Documentation/filesystems/aufs/design/03lookup.txt 2019-01-28 14:36:12.215751270 +0100
7e9cd9fe 1782@@ -0,0 +1,113 @@
53392da6 1783+
b00004a5 1784+# Copyright (C) 2005-2018 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
e49925d1 1898+++ linux/Documentation/filesystems/aufs/design/04branch.txt 2019-01-28 14:36:12.215751270 +0100
7e9cd9fe 1899@@ -0,0 +1,74 @@
53392da6 1900+
b00004a5 1901+# Copyright (C) 2005-2018 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
e49925d1 1976+++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt 2019-01-28 14:36:12.215751270 +0100
523b37e3 1977@@ -0,0 +1,64 @@
53392da6 1978+
b00004a5 1979+# Copyright (C) 2005-2018 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
e49925d1 2044+++ linux/Documentation/filesystems/aufs/design/06dirren.dot 2019-01-28 14:36:12.215751270 +0100
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
e49925d1 2079+++ linux/Documentation/filesystems/aufs/design/06dirren.txt 2019-01-28 14:36:12.215751270 +0100
8b6a4947
AM
2080@@ -0,0 +1,102 @@
2081+
b00004a5 2082+# Copyright (C) 2017-2018 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
e49925d1 2185+++ linux/Documentation/filesystems/aufs/design/06fhsm.txt 2019-01-28 14:36:12.215751270 +0100
076b876e
AM
2186@@ -0,0 +1,120 @@
2187+
b00004a5 2188+# Copyright (C) 2011-2018 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
e49925d1 2309+++ linux/Documentation/filesystems/aufs/design/06mmap.txt 2019-01-28 14:36:12.215751270 +0100
b912730e 2310@@ -0,0 +1,72 @@
53392da6 2311+
b00004a5 2312+# Copyright (C) 2005-2018 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
e49925d1 2385+++ linux/Documentation/filesystems/aufs/design/06xattr.txt 2019-01-28 14:36:12.215751270 +0100
c1595e42
JR
2386@@ -0,0 +1,96 @@
2387+
b00004a5 2388+# Copyright (C) 2014-2018 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
e49925d1 2485+++ linux/Documentation/filesystems/aufs/design/07export.txt 2019-01-28 14:36:12.215751270 +0100
523b37e3 2486@@ -0,0 +1,58 @@
53392da6 2487+
b00004a5 2488+# Copyright (C) 2005-2018 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
e49925d1 2547+++ linux/Documentation/filesystems/aufs/design/08shwh.txt 2019-01-28 14:36:12.215751270 +0100
523b37e3 2548@@ -0,0 +1,52 @@
53392da6 2549+
b00004a5 2550+# Copyright (C) 2005-2018 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
e49925d1 2603+++ linux/Documentation/filesystems/aufs/design/10dynop.txt 2019-01-28 14:36:12.215751270 +0100
7e9cd9fe 2604@@ -0,0 +1,47 @@
53392da6 2605+
b00004a5 2606+# Copyright (C) 2010-2018 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
e49925d1
JR
2654+++ linux/Documentation/filesystems/aufs/README 2019-01-28 14:36:12.215751270 +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).
e49925d1 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
e49925d1 3053+++ linux/fs/aufs/aufs.h 2019-01-28 14:36:12.219084659 +0100
acd2b654 3054@@ -0,0 +1,62 @@
062440b3 3055+/* SPDX-License-Identifier: GPL-2.0 */
7f207e10 3056+/*
b00004a5 3057+ * Copyright (C) 2005-2018 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
e49925d1 3119+++ linux/fs/aufs/branch.c 2019-01-28 14:36:12.219084659 +0100
062440b3 3120@@ -0,0 +1,1422 @@
cd7a4cd9 3121+// SPDX-License-Identifier: GPL-2.0
7f207e10 3122+/*
b00004a5 3123+ * Copyright (C) 2005-2018 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);
e49925d1 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();
e49925d1 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(); */
e49925d1 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:
e49925d1 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:
e49925d1 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)) {
e49925d1 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)
4250+ err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
4251+ if (unlikely(err)) {
4252+ err = -EFAULT;
4253+ AuTraceErr(err);
4254+ goto out;
4255+ }
4256+
4257+ err = -EINVAL;
4258+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d 4259+ if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
027c5e7a
AM
4260+ goto out_unlock;
4261+
4262+ err = 0;
4263+ ibusy.h_ino = 0; /* invalid */
4264+ inode = ilookup(sb, ibusy.ino);
4265+ if (!inode
4266+ || inode->i_ino == AUFS_ROOT_INO
5afbbe0d 4267+ || au_is_bad_inode(inode))
027c5e7a
AM
4268+ goto out_unlock;
4269+
4270+ ii_read_lock_child(inode);
5afbbe0d
AM
4271+ btop = au_ibtop(inode);
4272+ bbot = au_ibbot(inode);
4273+ if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
027c5e7a 4274+ h_inode = au_h_iptr(inode, ibusy.bindex);
5afbbe0d 4275+ if (h_inode && au_test_ibusy(inode, btop, bbot))
027c5e7a
AM
4276+ ibusy.h_ino = h_inode->i_ino;
4277+ }
4278+ ii_read_unlock(inode);
4279+ iput(inode);
4280+
4281+out_unlock:
4282+ si_read_unlock(sb);
4283+ if (!err) {
4284+ err = __put_user(ibusy.h_ino, &arg->h_ino);
4285+ if (unlikely(err)) {
4286+ err = -EFAULT;
4287+ AuTraceErr(err);
4288+ }
4289+ }
4290+out:
4291+ return err;
4292+}
4293+
4294+long au_ibusy_ioctl(struct file *file, unsigned long arg)
4295+{
2000de60 4296+ return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
027c5e7a
AM
4297+}
4298+
4299+#ifdef CONFIG_COMPAT
4300+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4301+{
2000de60 4302+ return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
027c5e7a
AM
4303+}
4304+#endif
4305+
4306+/* ---------------------------------------------------------------------- */
4307+
1facf9fc 4308+/*
4309+ * change a branch permission
4310+ */
4311+
dece6358
AM
4312+static void au_warn_ima(void)
4313+{
4314+#ifdef CONFIG_IMA
1308ab2a 4315+ /* since it doesn't support mark_files_ro() */
027c5e7a 4316+ AuWarn1("RW -> RO makes IMA to produce wrong message\n");
dece6358
AM
4317+#endif
4318+}
4319+
1facf9fc 4320+static int do_need_sigen_inc(int a, int b)
4321+{
4322+ return au_br_whable(a) && !au_br_whable(b);
4323+}
4324+
4325+static int need_sigen_inc(int old, int new)
4326+{
4327+ return do_need_sigen_inc(old, new)
4328+ || do_need_sigen_inc(new, old);
4329+}
4330+
4331+static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4332+{
7f207e10 4333+ int err, do_warn;
027c5e7a 4334+ unsigned int mnt_flags;
7f207e10 4335+ unsigned long long ull, max;
e49829fe 4336+ aufs_bindex_t br_id;
38d290e6 4337+ unsigned char verbose, writer;
7f207e10 4338+ struct file *file, *hf, **array;
e49829fe 4339+ struct au_hfile *hfile;
1facf9fc 4340+
027c5e7a
AM
4341+ mnt_flags = au_mntflags(sb);
4342+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
4343+
7f207e10
AM
4344+ array = au_farray_alloc(sb, &max);
4345+ err = PTR_ERR(array);
4346+ if (IS_ERR(array))
1facf9fc 4347+ goto out;
4348+
7f207e10 4349+ do_warn = 0;
e49829fe 4350+ br_id = au_sbr_id(sb, bindex);
7f207e10
AM
4351+ for (ull = 0; ull < max; ull++) {
4352+ file = array[ull];
076b876e
AM
4353+ if (unlikely(!file))
4354+ break;
1facf9fc 4355+
523b37e3 4356+ /* AuDbg("%pD\n", file); */
1facf9fc 4357+ fi_read_lock(file);
4358+ if (unlikely(au_test_mmapped(file))) {
4359+ err = -EBUSY;
523b37e3 4360+ AuVerbose(verbose, "mmapped %pD\n", file);
7f207e10 4361+ AuDbgFile(file);
1facf9fc 4362+ FiMustNoWaiters(file);
4363+ fi_read_unlock(file);
7f207e10 4364+ goto out_array;
1facf9fc 4365+ }
4366+
e49829fe
JR
4367+ hfile = &au_fi(file)->fi_htop;
4368+ hf = hfile->hf_file;
7e9cd9fe 4369+ if (!d_is_reg(file->f_path.dentry)
1facf9fc 4370+ || !(file->f_mode & FMODE_WRITE)
e49829fe 4371+ || hfile->hf_br->br_id != br_id
7f207e10
AM
4372+ || !(hf->f_mode & FMODE_WRITE))
4373+ array[ull] = NULL;
4374+ else {
4375+ do_warn = 1;
4376+ get_file(file);
1facf9fc 4377+ }
4378+
1facf9fc 4379+ FiMustNoWaiters(file);
4380+ fi_read_unlock(file);
7f207e10
AM
4381+ fput(file);
4382+ }
1facf9fc 4383+
4384+ err = 0;
7f207e10 4385+ if (do_warn)
dece6358 4386+ au_warn_ima();
7f207e10
AM
4387+
4388+ for (ull = 0; ull < max; ull++) {
4389+ file = array[ull];
4390+ if (!file)
4391+ continue;
4392+
1facf9fc 4393+ /* todo: already flushed? */
523b37e3
AM
4394+ /*
4395+ * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4396+ * approach which resets f_mode and calls mnt_drop_write() and
4397+ * file_release_write() for each file, because the branch
4398+ * attribute in aufs world is totally different from the native
4399+ * fs rw/ro mode.
4400+ */
7f207e10
AM
4401+ /* fi_read_lock(file); */
4402+ hfile = &au_fi(file)->fi_htop;
4403+ hf = hfile->hf_file;
4404+ /* fi_read_unlock(file); */
027c5e7a 4405+ spin_lock(&hf->f_lock);
38d290e6
JR
4406+ writer = !!(hf->f_mode & FMODE_WRITER);
4407+ hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
027c5e7a 4408+ spin_unlock(&hf->f_lock);
38d290e6
JR
4409+ if (writer) {
4410+ put_write_access(file_inode(hf));
c06a8ce3 4411+ __mnt_drop_write(hf->f_path.mnt);
1facf9fc 4412+ }
4413+ }
4414+
7f207e10
AM
4415+out_array:
4416+ au_farray_free(array, max);
4f0767ce 4417+out:
7f207e10 4418+ AuTraceErr(err);
1facf9fc 4419+ return err;
4420+}
4421+
4422+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4423+ int *do_refresh)
1facf9fc 4424+{
4425+ int err, rerr;
4426+ aufs_bindex_t bindex;
4427+ struct dentry *root;
4428+ struct au_branch *br;
076b876e 4429+ struct au_br_fhsm *bf;
1facf9fc 4430+
4431+ root = sb->s_root;
1facf9fc 4432+ bindex = au_find_dbindex(root, mod->h_root);
4433+ if (bindex < 0) {
4434+ if (remount)
4435+ return 0; /* success */
4436+ err = -ENOENT;
4a4d8108 4437+ pr_err("%s no such branch\n", mod->path);
1facf9fc 4438+ goto out;
4439+ }
4440+ AuDbg("bindex b%d\n", bindex);
4441+
5527c038 4442+ err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
1facf9fc 4443+ if (unlikely(err))
4444+ goto out;
4445+
4446+ br = au_sbr(sb, bindex);
86dc4139 4447+ AuDebugOn(mod->h_root != au_br_dentry(br));
1facf9fc 4448+ if (br->br_perm == mod->perm)
4449+ return 0; /* success */
4450+
076b876e
AM
4451+ /* pre-allocate for non-fhsm --> fhsm */
4452+ bf = NULL;
4453+ if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4454+ err = au_fhsm_br_alloc(br);
4455+ if (unlikely(err))
4456+ goto out;
4457+ bf = br->br_fhsm;
4458+ br->br_fhsm = NULL;
4459+ }
4460+
1facf9fc 4461+ if (au_br_writable(br->br_perm)) {
4462+ /* remove whiteout base */
86dc4139 4463+ err = au_br_init_wh(sb, br, mod->perm);
1facf9fc 4464+ if (unlikely(err))
076b876e 4465+ goto out_bf;
1facf9fc 4466+
4467+ if (!au_br_writable(mod->perm)) {
4468+ /* rw --> ro, file might be mmapped */
4469+ DiMustNoWaiters(root);
5527c038 4470+ IiMustNoWaiters(d_inode(root));
1facf9fc 4471+ di_write_unlock(root);
4472+ err = au_br_mod_files_ro(sb, bindex);
4473+ /* aufs_write_lock() calls ..._child() */
4474+ di_write_lock_child(root);
4475+
4476+ if (unlikely(err)) {
4477+ rerr = -ENOMEM;
be52b249 4478+ br->br_wbr = kzalloc(sizeof(*br->br_wbr),
1facf9fc 4479+ GFP_NOFS);
86dc4139
AM
4480+ if (br->br_wbr)
4481+ rerr = au_wbr_init(br, sb, br->br_perm);
1facf9fc 4482+ if (unlikely(rerr)) {
4483+ AuIOErr("nested error %d (%d)\n",
4484+ rerr, err);
4485+ br->br_perm = mod->perm;
4486+ }
4487+ }
4488+ }
4489+ } else if (au_br_writable(mod->perm)) {
4490+ /* ro --> rw */
4491+ err = -ENOMEM;
be52b249 4492+ br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
1facf9fc 4493+ if (br->br_wbr) {
86dc4139 4494+ err = au_wbr_init(br, sb, mod->perm);
1facf9fc 4495+ if (unlikely(err)) {
e49925d1 4496+ au_kfree_rcu(br->br_wbr);
1facf9fc 4497+ br->br_wbr = NULL;
4498+ }
4499+ }
4500+ }
076b876e
AM
4501+ if (unlikely(err))
4502+ goto out_bf;
4503+
4504+ if (au_br_fhsm(br->br_perm)) {
4505+ if (!au_br_fhsm(mod->perm)) {
4506+ /* fhsm --> non-fhsm */
4507+ au_br_fhsm_fin(br->br_fhsm);
e49925d1 4508+ au_kfree_rcu(br->br_fhsm);
076b876e
AM
4509+ br->br_fhsm = NULL;
4510+ }
4511+ } else if (au_br_fhsm(mod->perm))
4512+ /* non-fhsm --> fhsm */
4513+ br->br_fhsm = bf;
4514+
076b876e
AM
4515+ *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4516+ br->br_perm = mod->perm;
4517+ goto out; /* success */
1facf9fc 4518+
076b876e 4519+out_bf:
e49925d1 4520+ au_kfree_try_rcu(bf);
076b876e
AM
4521+out:
4522+ AuTraceErr(err);
4523+ return err;
4524+}
4525+
4526+/* ---------------------------------------------------------------------- */
4527+
4528+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4529+{
4530+ int err;
4531+ struct kstatfs kstfs;
4532+
4533+ err = vfs_statfs(&br->br_path, &kstfs);
1facf9fc 4534+ if (!err) {
076b876e
AM
4535+ stfs->f_blocks = kstfs.f_blocks;
4536+ stfs->f_bavail = kstfs.f_bavail;
4537+ stfs->f_files = kstfs.f_files;
4538+ stfs->f_ffree = kstfs.f_ffree;
1facf9fc 4539+ }
4540+
1facf9fc 4541+ return err;
4542+}
7f207e10
AM
4543diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4544--- /usr/share/empty/fs/aufs/branch.h 1970-01-01 01:00:00.000000000 +0100
e49925d1
JR
4545+++ linux/fs/aufs/branch.h 2019-01-28 14:36:12.219084659 +0100
4546@@ -0,0 +1,365 @@
062440b3 4547+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 4548+/*
b00004a5 4549+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 4550+ *
4551+ * This program, aufs is free software; you can redistribute it and/or modify
4552+ * it under the terms of the GNU General Public License as published by
4553+ * the Free Software Foundation; either version 2 of the License, or
4554+ * (at your option) any later version.
dece6358
AM
4555+ *
4556+ * This program is distributed in the hope that it will be useful,
4557+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4558+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4559+ * GNU General Public License for more details.
4560+ *
4561+ * You should have received a copy of the GNU General Public License
523b37e3 4562+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 4563+ */
4564+
4565+/*
4566+ * branch filesystems and xino for them
4567+ */
4568+
4569+#ifndef __AUFS_BRANCH_H__
4570+#define __AUFS_BRANCH_H__
4571+
4572+#ifdef __KERNEL__
4573+
1facf9fc 4574+#include <linux/mount.h>
8b6a4947 4575+#include "dirren.h"
4a4d8108 4576+#include "dynop.h"
acd2b654 4577+#include "lcnt.h"
1facf9fc 4578+#include "rwsem.h"
4579+#include "super.h"
4580+
4581+/* ---------------------------------------------------------------------- */
4582+
4583+/* a xino file */
062440b3 4584+struct au_xino {
acd2b654
AM
4585+ struct file **xi_file;
4586+ unsigned int xi_nfile;
4587+
521ced18
JR
4588+ struct {
4589+ spinlock_t spin;
4590+ ino_t *array;
4591+ int total;
4592+ /* reserved for future use */
4593+ /* unsigned long *bitmap; */
4594+ wait_queue_head_t wqh;
4595+ } xi_nondir;
1facf9fc 4596+
acd2b654 4597+ struct mutex xi_mtx; /* protects xi_file array */
e49925d1 4598+ struct hlist_bl_head xi_writing;
acd2b654 4599+
062440b3 4600+ atomic_t xi_truncating;
1facf9fc 4601+
062440b3 4602+ struct kref xi_kref;
1facf9fc 4603+};
4604+
076b876e
AM
4605+/* File-based Hierarchical Storage Management */
4606+struct au_br_fhsm {
4607+#ifdef CONFIG_AUFS_FHSM
4608+ struct mutex bf_lock;
4609+ unsigned long bf_jiffy;
4610+ struct aufs_stfs bf_stfs;
4611+ int bf_readable;
4612+#endif
4613+};
4614+
1facf9fc 4615+/* members for writable branch only */
4616+enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4617+struct au_wbr {
dece6358 4618+ struct au_rwsem wbr_wh_rwsem;
1facf9fc 4619+ struct dentry *wbr_wh[AuBrWh_Last];
4a4d8108 4620+ atomic_t wbr_wh_running;
1facf9fc 4621+#define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
4622+#define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
4623+#define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
4624+
4625+ /* mfs mode */
4626+ unsigned long long wbr_bytes;
4627+};
4628+
4a4d8108
AM
4629+/* ext2 has 3 types of operations at least, ext3 has 4 */
4630+#define AuBrDynOp (AuDyLast * 4)
4631+
1716fcea
AM
4632+#ifdef CONFIG_AUFS_HFSNOTIFY
4633+/* support for asynchronous destruction */
4634+struct au_br_hfsnotify {
4635+ struct fsnotify_group *hfsn_group;
4636+};
4637+#endif
4638+
392086de
AM
4639+/* sysfs entries */
4640+struct au_brsysfs {
4641+ char name[16];
4642+ struct attribute attr;
4643+};
4644+
4645+enum {
4646+ AuBrSysfs_BR,
4647+ AuBrSysfs_BRID,
4648+ AuBrSysfs_Last
4649+};
4650+
1facf9fc 4651+/* protected by superblock rwsem */
4652+struct au_branch {
062440b3 4653+ struct au_xino *br_xino;
1facf9fc 4654+
4655+ aufs_bindex_t br_id;
4656+
4657+ int br_perm;
86dc4139 4658+ struct path br_path;
4a4d8108
AM
4659+ spinlock_t br_dykey_lock;
4660+ struct au_dykey *br_dykey[AuBrDynOp];
acd2b654
AM
4661+ au_lcnt_t br_nfiles; /* opened files */
4662+ au_lcnt_t br_count; /* in-use for other */
1facf9fc 4663+
4664+ struct au_wbr *br_wbr;
076b876e 4665+ struct au_br_fhsm *br_fhsm;
1facf9fc 4666+
027c5e7a 4667+#ifdef CONFIG_AUFS_HFSNOTIFY
1716fcea 4668+ struct au_br_hfsnotify *br_hfsn;
027c5e7a
AM
4669+#endif
4670+
1facf9fc 4671+#ifdef CONFIG_SYSFS
392086de
AM
4672+ /* entries under sysfs per mount-point */
4673+ struct au_brsysfs br_sysfs[AuBrSysfs_Last];
1facf9fc 4674+#endif
8b6a4947 4675+
062440b3
AM
4676+#ifdef CONFIG_DEBUG_FS
4677+ struct dentry *br_dbgaufs; /* xino */
4678+#endif
4679+
8b6a4947 4680+ struct au_dr_br br_dirren;
1facf9fc 4681+};
4682+
4683+/* ---------------------------------------------------------------------- */
4684+
86dc4139
AM
4685+static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4686+{
4687+ return br->br_path.mnt;
4688+}
4689+
4690+static inline struct dentry *au_br_dentry(struct au_branch *br)
4691+{
4692+ return br->br_path.dentry;
4693+}
4694+
4695+static inline struct super_block *au_br_sb(struct au_branch *br)
4696+{
4697+ return au_br_mnt(br)->mnt_sb;
4698+}
4699+
1facf9fc 4700+static inline int au_br_rdonly(struct au_branch *br)
4701+{
8b6a4947 4702+ return (sb_rdonly(au_br_sb(br))
1facf9fc 4703+ || !au_br_writable(br->br_perm))
4704+ ? -EROFS : 0;
4705+}
4706+
4a4d8108 4707+static inline int au_br_hnotifyable(int brperm __maybe_unused)
1facf9fc 4708+{
4a4d8108 4709+#ifdef CONFIG_AUFS_HNOTIFY
1e00d052 4710+ return !(brperm & AuBrPerm_RR);
1facf9fc 4711+#else
4712+ return 0;
4713+#endif
4714+}
4715+
b912730e
AM
4716+static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4717+{
4718+ int err, exec_flag;
4719+
4720+ err = 0;
4721+ exec_flag = oflag & __FMODE_EXEC;
79b8bda9 4722+ if (unlikely(exec_flag && path_noexec(&br->br_path)))
b912730e
AM
4723+ err = -EACCES;
4724+
4725+ return err;
4726+}
4727+
062440b3
AM
4728+static inline void au_xino_get(struct au_branch *br)
4729+{
4730+ struct au_xino *xi;
4731+
4732+ xi = br->br_xino;
4733+ if (xi)
4734+ kref_get(&xi->xi_kref);
4735+}
4736+
4737+static inline int au_xino_count(struct au_branch *br)
4738+{
4739+ int v;
4740+ struct au_xino *xi;
4741+
4742+ v = 0;
4743+ xi = br->br_xino;
4744+ if (xi)
4745+ v = kref_read(&xi->xi_kref);
4746+
4747+ return v;
4748+}
4749+
1facf9fc 4750+/* ---------------------------------------------------------------------- */
4751+
4752+/* branch.c */
4753+struct au_sbinfo;
4754+void au_br_free(struct au_sbinfo *sinfo);
4755+int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4756+struct au_opt_add;
4757+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4758+struct au_opt_del;
4759+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
027c5e7a
AM
4760+long au_ibusy_ioctl(struct file *file, unsigned long arg);
4761+#ifdef CONFIG_COMPAT
4762+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4763+#endif
1facf9fc 4764+struct au_opt_mod;
4765+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4766+ int *do_refresh);
076b876e
AM
4767+struct aufs_stfs;
4768+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
1facf9fc 4769+
4770+/* xino.c */
4771+static const loff_t au_loff_max = LLONG_MAX;
4772+
acd2b654 4773+aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
062440b3
AM
4774+struct file *au_xino_create(struct super_block *sb, char *fpath, int silent);
4775+struct file *au_xino_create2(struct super_block *sb, struct path *base,
4776+ struct file *copy_src);
acd2b654
AM
4777+struct au_xi_new {
4778+ struct au_xino *xi; /* switch between xino and xigen */
4779+ int idx;
4780+ struct path *base;
4781+ struct file *copy_src;
4782+};
4783+struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
062440b3
AM
4784+
4785+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4786+ ino_t *ino);
4787+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4788+ ino_t ino);
5527c038 4789+ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
1facf9fc 4790+ loff_t *pos);
5527c038
JR
4791+ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
4792+ size_t size, loff_t *pos);
062440b3
AM
4793+
4794+int au_xib_trunc(struct super_block *sb);
acd2b654 4795+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
1facf9fc 4796+
acd2b654 4797+struct au_xino *au_xino_alloc(unsigned int nfile);
062440b3 4798+int au_xino_put(struct au_branch *br);
acd2b654 4799+struct file *au_xino_file1(struct au_xino *xi);
062440b3 4800+
1facf9fc 4801+struct au_opt_xino;
1facf9fc 4802+void au_xino_clr(struct super_block *sb);
062440b3 4803+int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
1facf9fc 4804+struct file *au_xino_def(struct super_block *sb);
062440b3
AM
4805+int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4806+ struct path *base);
4807+
4808+ino_t au_xino_new_ino(struct super_block *sb);
4809+void au_xino_delete_inode(struct inode *inode, const int unlinked);
1facf9fc 4810+
521ced18
JR
4811+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4812+ ino_t h_ino, int idx);
4813+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4814+ int *idx);
4815+
062440b3
AM
4816+int au_xino_path(struct seq_file *seq, struct file *file);
4817+
1facf9fc 4818+/* ---------------------------------------------------------------------- */
4819+
acd2b654
AM
4820+/* @idx is signed to accept -1 meaning the first file */
4821+static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4822+{
4823+ struct file *file;
4824+
4825+ file = NULL;
4826+ if (!xi)
4827+ goto out;
4828+
4829+ if (idx >= 0) {
4830+ if (idx < xi->xi_nfile)
4831+ file = xi->xi_file[idx];
4832+ } else
4833+ file = au_xino_file1(xi);
4834+
4835+out:
4836+ return file;
4837+}
4838+
4839+/* ---------------------------------------------------------------------- */
4840+
1facf9fc 4841+/* Superblock to branch */
4842+static inline
4843+aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4844+{
4845+ return au_sbr(sb, bindex)->br_id;
4846+}
4847+
4848+static inline
4849+struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4850+{
86dc4139 4851+ return au_br_mnt(au_sbr(sb, bindex));
1facf9fc 4852+}
4853+
4854+static inline
4855+struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4856+{
86dc4139 4857+ return au_br_sb(au_sbr(sb, bindex));
1facf9fc 4858+}
4859+
1facf9fc 4860+static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4861+{
4862+ return au_sbr(sb, bindex)->br_perm;
4863+}
4864+
4865+static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4866+{
4867+ return au_br_whable(au_sbr_perm(sb, bindex));
4868+}
4869+
4870+/* ---------------------------------------------------------------------- */
4871+
8b6a4947
AM
4872+#define wbr_wh_read_lock(wbr) au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4873+#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4874+#define wbr_wh_read_trylock(wbr) au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4875+#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
1facf9fc 4876+/*
8b6a4947
AM
4877+#define wbr_wh_read_trylock_nested(wbr) \
4878+ au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4879+#define wbr_wh_write_trylock_nested(wbr) \
4880+ au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4881+*/
1facf9fc 4882+
8b6a4947
AM
4883+#define wbr_wh_read_unlock(wbr) au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4884+#define wbr_wh_write_unlock(wbr) au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4885+#define wbr_wh_downgrade_lock(wbr) au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4886+
4887+#define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4888+#define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4889+#define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
dece6358 4890+
076b876e
AM
4891+/* ---------------------------------------------------------------------- */
4892+
4893+#ifdef CONFIG_AUFS_FHSM
4894+static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4895+{
4896+ mutex_init(&brfhsm->bf_lock);
4897+ brfhsm->bf_jiffy = 0;
4898+ brfhsm->bf_readable = 0;
4899+}
4900+
4901+static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4902+{
4903+ mutex_destroy(&brfhsm->bf_lock);
4904+}
4905+#else
4906+AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4907+AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4908+#endif
4909+
1facf9fc 4910+#endif /* __KERNEL__ */
4911+#endif /* __AUFS_BRANCH_H__ */
7f207e10
AM
4912diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4913--- /usr/share/empty/fs/aufs/conf.mk 1970-01-01 01:00:00.000000000 +0100
e49925d1 4914+++ linux/fs/aufs/conf.mk 2019-01-28 14:36:12.219084659 +0100
2121bcd9
AM
4915@@ -0,0 +1,40 @@
4916+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
4917+
4918+AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4919+
4920+define AuConf
4921+ifdef ${1}
4922+AuConfStr += ${1}=${${1}}
4923+endif
4924+endef
4925+
b752ccd1 4926+AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
e49829fe 4927+ SBILIST \
7f207e10 4928+ HNOTIFY HFSNOTIFY \
4a4d8108 4929+ EXPORT INO_T_64 \
c1595e42 4930+ XATTR \
076b876e 4931+ FHSM \
4a4d8108 4932+ RDU \
8b6a4947 4933+ DIRREN \
4a4d8108
AM
4934+ SHWH \
4935+ BR_RAMFS \
4936+ BR_FUSE POLL \
4937+ BR_HFSPLUS \
4938+ BDEV_LOOP \
b752ccd1
AM
4939+ DEBUG MAGIC_SYSRQ
4940+$(foreach i, ${AuConfAll}, \
4a4d8108
AM
4941+ $(eval $(call AuConf,CONFIG_AUFS_${i})))
4942+
4943+AuConfName = ${obj}/conf.str
4944+${AuConfName}.tmp: FORCE
4945+ @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4946+${AuConfName}: ${AuConfName}.tmp
4947+ @diff -q $< $@ > /dev/null 2>&1 || { \
4948+ echo ' GEN ' $@; \
4949+ cp -p $< $@; \
4950+ }
4951+FORCE:
4952+clean-files += ${AuConfName} ${AuConfName}.tmp
4953+${obj}/sysfs.o: ${AuConfName}
b752ccd1
AM
4954+
4955+-include ${srctree}/${src}/conf_priv.mk
7f207e10
AM
4956diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4957--- /usr/share/empty/fs/aufs/cpup.c 1970-01-01 01:00:00.000000000 +0100
e49925d1
JR
4958+++ linux/fs/aufs/cpup.c 2019-01-28 14:36:12.219084659 +0100
4959@@ -0,0 +1,1448 @@
cd7a4cd9 4960+// SPDX-License-Identifier: GPL-2.0
1facf9fc 4961+/*
b00004a5 4962+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 4963+ *
4964+ * This program, aufs is free software; you can redistribute it and/or modify
4965+ * it under the terms of the GNU General Public License as published by
4966+ * the Free Software Foundation; either version 2 of the License, or
4967+ * (at your option) any later version.
dece6358
AM
4968+ *
4969+ * This program is distributed in the hope that it will be useful,
4970+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4971+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4972+ * GNU General Public License for more details.
4973+ *
4974+ * You should have received a copy of the GNU General Public License
523b37e3 4975+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 4976+ */
4977+
4978+/*
4979+ * copy-up functions, see wbr_policy.c for copy-down
4980+ */
4981+
4982+#include <linux/fs_stack.h>
dece6358 4983+#include <linux/mm.h>
8cdd5066 4984+#include <linux/task_work.h>
1facf9fc 4985+#include "aufs.h"
4986+
86dc4139 4987+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
1facf9fc 4988+{
4989+ const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
367653fa 4990+ | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
1facf9fc 4991+
86dc4139
AM
4992+ BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4993+
4994+ dst->i_flags |= iflags & ~mask;
1facf9fc 4995+ if (au_test_fs_notime(dst->i_sb))
4996+ dst->i_flags |= S_NOATIME | S_NOCMTIME;
4997+}
4998+
4999+void au_cpup_attr_timesizes(struct inode *inode)
5000+{
5001+ struct inode *h_inode;
5002+
5afbbe0d 5003+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 5004+ fsstack_copy_attr_times(inode, h_inode);
4a4d8108 5005+ fsstack_copy_inode_size(inode, h_inode);
1facf9fc 5006+}
5007+
5008+void au_cpup_attr_nlink(struct inode *inode, int force)
5009+{
5010+ struct inode *h_inode;
5011+ struct super_block *sb;
5afbbe0d 5012+ aufs_bindex_t bindex, bbot;
1facf9fc 5013+
5014+ sb = inode->i_sb;
5afbbe0d 5015+ bindex = au_ibtop(inode);
1facf9fc 5016+ h_inode = au_h_iptr(inode, bindex);
5017+ if (!force
5018+ && !S_ISDIR(h_inode->i_mode)
5019+ && au_opt_test(au_mntflags(sb), PLINK)
5020+ && au_plink_test(inode))
5021+ return;
5022+
7eafdf33
AM
5023+ /*
5024+ * 0 can happen in revalidating.
38d290e6
JR
5025+ * h_inode->i_mutex may not be held here, but it is harmless since once
5026+ * i_nlink reaches 0, it will never become positive except O_TMPFILE
5027+ * case.
5028+ * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
5029+ * the incorrect link count.
7eafdf33 5030+ */
92d182d2 5031+ set_nlink(inode, h_inode->i_nlink);
1facf9fc 5032+
5033+ /*
5034+ * fewer nlink makes find(1) noisy, but larger nlink doesn't.
5035+ * it may includes whplink directory.
5036+ */
5037+ if (S_ISDIR(h_inode->i_mode)) {
5afbbe0d
AM
5038+ bbot = au_ibbot(inode);
5039+ for (bindex++; bindex <= bbot; bindex++) {
1facf9fc 5040+ h_inode = au_h_iptr(inode, bindex);
5041+ if (h_inode)
5042+ au_add_nlink(inode, h_inode);
5043+ }
5044+ }
5045+}
5046+
5047+void au_cpup_attr_changeable(struct inode *inode)
5048+{
5049+ struct inode *h_inode;
5050+
5afbbe0d 5051+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 5052+ inode->i_mode = h_inode->i_mode;
5053+ inode->i_uid = h_inode->i_uid;
5054+ inode->i_gid = h_inode->i_gid;
5055+ au_cpup_attr_timesizes(inode);
86dc4139 5056+ au_cpup_attr_flags(inode, h_inode->i_flags);
1facf9fc 5057+}
5058+
5059+void au_cpup_igen(struct inode *inode, struct inode *h_inode)
5060+{
5061+ struct au_iinfo *iinfo = au_ii(inode);
5062+
1308ab2a 5063+ IiMustWriteLock(inode);
5064+
1facf9fc 5065+ iinfo->ii_higen = h_inode->i_generation;
5066+ iinfo->ii_hsb1 = h_inode->i_sb;
5067+}
5068+
5069+void au_cpup_attr_all(struct inode *inode, int force)
5070+{
5071+ struct inode *h_inode;
5072+
5afbbe0d 5073+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 5074+ au_cpup_attr_changeable(inode);
5075+ if (inode->i_nlink > 0)
5076+ au_cpup_attr_nlink(inode, force);
5077+ inode->i_rdev = h_inode->i_rdev;
5078+ inode->i_blkbits = h_inode->i_blkbits;
5079+ au_cpup_igen(inode, h_inode);
5080+}
5081+
5082+/* ---------------------------------------------------------------------- */
5083+
5084+/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
5085+
5086+/* keep the timestamps of the parent dir when cpup */
5087+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
5088+ struct path *h_path)
5089+{
5090+ struct inode *h_inode;
5091+
5092+ dt->dt_dentry = dentry;
5093+ dt->dt_h_path = *h_path;
5527c038 5094+ h_inode = d_inode(h_path->dentry);
1facf9fc 5095+ dt->dt_atime = h_inode->i_atime;
5096+ dt->dt_mtime = h_inode->i_mtime;
5097+ /* smp_mb(); */
5098+}
5099+
5100+void au_dtime_revert(struct au_dtime *dt)
5101+{
5102+ struct iattr attr;
5103+ int err;
5104+
5105+ attr.ia_atime = dt->dt_atime;
5106+ attr.ia_mtime = dt->dt_mtime;
5107+ attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
5108+ | ATTR_ATIME | ATTR_ATIME_SET;
5109+
523b37e3
AM
5110+ /* no delegation since this is a directory */
5111+ err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
1facf9fc 5112+ if (unlikely(err))
0c3ec466 5113+ pr_warn("restoring timestamps failed(%d). ignored\n", err);
1facf9fc 5114+}
5115+
5116+/* ---------------------------------------------------------------------- */
5117+
86dc4139
AM
5118+/* internal use only */
5119+struct au_cpup_reg_attr {
5120+ int valid;
5121+ struct kstat st;
5122+ unsigned int iflags; /* inode->i_flags */
5123+};
5124+
1facf9fc 5125+static noinline_for_stack
86dc4139
AM
5126+int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
5127+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5128+{
c1595e42 5129+ int err, sbits, icex;
7e9cd9fe
AM
5130+ unsigned int mnt_flags;
5131+ unsigned char verbose;
1facf9fc 5132+ struct iattr ia;
5133+ struct path h_path;
1308ab2a 5134+ struct inode *h_isrc, *h_idst;
86dc4139 5135+ struct kstat *h_st;
c1595e42 5136+ struct au_branch *br;
1facf9fc 5137+
5138+ h_path.dentry = au_h_dptr(dst, bindex);
5527c038 5139+ h_idst = d_inode(h_path.dentry);
c1595e42
JR
5140+ br = au_sbr(dst->d_sb, bindex);
5141+ h_path.mnt = au_br_mnt(br);
5527c038 5142+ h_isrc = d_inode(h_src);
1308ab2a 5143+ ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
1facf9fc 5144+ | ATTR_ATIME | ATTR_MTIME
5145+ | ATTR_ATIME_SET | ATTR_MTIME_SET;
86dc4139
AM
5146+ if (h_src_attr && h_src_attr->valid) {
5147+ h_st = &h_src_attr->st;
5148+ ia.ia_uid = h_st->uid;
5149+ ia.ia_gid = h_st->gid;
5150+ ia.ia_atime = h_st->atime;
5151+ ia.ia_mtime = h_st->mtime;
5152+ if (h_idst->i_mode != h_st->mode
5153+ && !S_ISLNK(h_idst->i_mode)) {
5154+ ia.ia_valid |= ATTR_MODE;
5155+ ia.ia_mode = h_st->mode;
5156+ }
5157+ sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
5158+ au_cpup_attr_flags(h_idst, h_src_attr->iflags);
5159+ } else {
5160+ ia.ia_uid = h_isrc->i_uid;
5161+ ia.ia_gid = h_isrc->i_gid;
5162+ ia.ia_atime = h_isrc->i_atime;
5163+ ia.ia_mtime = h_isrc->i_mtime;
5164+ if (h_idst->i_mode != h_isrc->i_mode
5165+ && !S_ISLNK(h_idst->i_mode)) {
5166+ ia.ia_valid |= ATTR_MODE;
5167+ ia.ia_mode = h_isrc->i_mode;
5168+ }
5169+ sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
5170+ au_cpup_attr_flags(h_idst, h_isrc->i_flags);
1308ab2a 5171+ }
523b37e3
AM
5172+ /* no delegation since it is just created */
5173+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 5174+
5175+ /* is this nfs only? */
5176+ if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
5177+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
5178+ ia.ia_mode = h_isrc->i_mode;
523b37e3 5179+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 5180+ }
5181+
c1595e42 5182+ icex = br->br_perm & AuBrAttr_ICEX;
7e9cd9fe
AM
5183+ if (!err) {
5184+ mnt_flags = au_mntflags(dst->d_sb);
5185+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
5186+ err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
5187+ }
c1595e42 5188+
1facf9fc 5189+ return err;
5190+}
5191+
5192+/* ---------------------------------------------------------------------- */
5193+
5194+static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
5195+ char *buf, unsigned long blksize)
5196+{
5197+ int err;
5198+ size_t sz, rbytes, wbytes;
5199+ unsigned char all_zero;
5200+ char *p, *zp;
febd17d6 5201+ struct inode *h_inode;
1facf9fc 5202+ /* reduce stack usage */
5203+ struct iattr *ia;
5204+
5205+ zp = page_address(ZERO_PAGE(0));
5206+ if (unlikely(!zp))
5207+ return -ENOMEM; /* possible? */
5208+
5209+ err = 0;
5210+ all_zero = 0;
5211+ while (len) {
5212+ AuDbg("len %lld\n", len);
5213+ sz = blksize;
5214+ if (len < blksize)
5215+ sz = len;
5216+
5217+ rbytes = 0;
5218+ /* todo: signal_pending? */
5219+ while (!rbytes || err == -EAGAIN || err == -EINTR) {
5220+ rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5221+ err = rbytes;
5222+ }
5223+ if (unlikely(err < 0))
5224+ break;
5225+
5226+ all_zero = 0;
5227+ if (len >= rbytes && rbytes == blksize)
5228+ all_zero = !memcmp(buf, zp, rbytes);
5229+ if (!all_zero) {
5230+ wbytes = rbytes;
5231+ p = buf;
5232+ while (wbytes) {
5233+ size_t b;
5234+
5235+ b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5236+ err = b;
5237+ /* todo: signal_pending? */
5238+ if (unlikely(err == -EAGAIN || err == -EINTR))
5239+ continue;
5240+ if (unlikely(err < 0))
5241+ break;
5242+ wbytes -= b;
5243+ p += b;
5244+ }
392086de
AM
5245+ if (unlikely(err < 0))
5246+ break;
1facf9fc 5247+ } else {
5248+ loff_t res;
5249+
5250+ AuLabel(hole);
5251+ res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5252+ err = res;
5253+ if (unlikely(res < 0))
5254+ break;
5255+ }
5256+ len -= rbytes;
5257+ err = 0;
5258+ }
5259+
5260+ /* the last block may be a hole */
5261+ if (!err && all_zero) {
5262+ AuLabel(last hole);
5263+
5264+ err = 1;
2000de60 5265+ if (au_test_nfs(dst->f_path.dentry->d_sb)) {
1facf9fc 5266+ /* nfs requires this step to make last hole */
5267+ /* is this only nfs? */
5268+ do {
5269+ /* todo: signal_pending? */
5270+ err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5271+ } while (err == -EAGAIN || err == -EINTR);
5272+ if (err == 1)
5273+ dst->f_pos--;
5274+ }
5275+
5276+ if (err == 1) {
5277+ ia = (void *)buf;
5278+ ia->ia_size = dst->f_pos;
5279+ ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5280+ ia->ia_file = dst;
febd17d6
JR
5281+ h_inode = file_inode(dst);
5282+ inode_lock_nested(h_inode, AuLsc_I_CHILD2);
523b37e3
AM
5283+ /* no delegation since it is just created */
5284+ err = vfsub_notify_change(&dst->f_path, ia,
5285+ /*delegated*/NULL);
febd17d6 5286+ inode_unlock(h_inode);
1facf9fc 5287+ }
5288+ }
5289+
5290+ return err;
5291+}
5292+
5293+int au_copy_file(struct file *dst, struct file *src, loff_t len)
5294+{
5295+ int err;
5296+ unsigned long blksize;
5297+ unsigned char do_kfree;
5298+ char *buf;
e49925d1 5299+ struct super_block *h_sb;
1facf9fc 5300+
5301+ err = -ENOMEM;
e49925d1
JR
5302+ h_sb = file_inode(dst)->i_sb;
5303+ blksize = h_sb->s_blocksize;
1facf9fc 5304+ if (!blksize || PAGE_SIZE < blksize)
5305+ blksize = PAGE_SIZE;
5306+ AuDbg("blksize %lu\n", blksize);
5307+ do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5308+ if (do_kfree)
5309+ buf = kmalloc(blksize, GFP_NOFS);
5310+ else
5311+ buf = (void *)__get_free_page(GFP_NOFS);
5312+ if (unlikely(!buf))
5313+ goto out;
5314+
5315+ if (len > (1 << 22))
5316+ AuDbg("copying a large file %lld\n", (long long)len);
5317+
5318+ src->f_pos = 0;
5319+ dst->f_pos = 0;
5320+ err = au_do_copy_file(dst, src, len, buf, blksize);
e49925d1
JR
5321+ if (do_kfree) {
5322+ AuDebugOn(!au_kfree_do_sz_test(blksize));
5323+ au_kfree_do_rcu(buf);
5324+ } else
1c60b727 5325+ free_page((unsigned long)buf);
1facf9fc 5326+
4f0767ce 5327+out:
1facf9fc 5328+ return err;
5329+}
5330+
1c60b727
AM
5331+static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5332+{
5333+ int err;
5334+ struct super_block *h_src_sb;
5335+ struct inode *h_src_inode;
5336+
5337+ h_src_inode = file_inode(src);
5338+ h_src_sb = h_src_inode->i_sb;
5339+
5340+ /* XFS acquires inode_lock */
5341+ if (!au_test_xfs(h_src_sb))
5342+ err = au_copy_file(dst, src, len);
5343+ else {
3c1bdaff 5344+ inode_unlock_shared(h_src_inode);
1c60b727 5345+ err = au_copy_file(dst, src, len);
be118d29 5346+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
1c60b727
AM
5347+ }
5348+
5349+ return err;
5350+}
5351+
5352+static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5353+{
5354+ int err;
5355+ struct super_block *h_src_sb;
5356+ struct inode *h_src_inode;
5357+
5358+ h_src_inode = file_inode(src);
5359+ h_src_sb = h_src_inode->i_sb;
5360+ if (h_src_sb != file_inode(dst)->i_sb
5361+ || !dst->f_op->clone_file_range) {
5362+ err = au_do_copy(dst, src, len);
5363+ goto out;
5364+ }
5365+
5366+ if (!au_test_nfs(h_src_sb)) {
3c1bdaff 5367+ inode_unlock_shared(h_src_inode);
1c60b727 5368+ err = vfsub_clone_file_range(src, dst, len);
be118d29 5369+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
1c60b727
AM
5370+ } else
5371+ err = vfsub_clone_file_range(src, dst, len);
5372+ /* older XFS has a condition in cloning */
5373+ if (unlikely(err != -EOPNOTSUPP))
5374+ goto out;
5375+
5376+ /* the backend fs on NFS may not support cloning */
5377+ err = au_do_copy(dst, src, len);
5378+
5379+out:
5380+ AuTraceErr(err);
5381+ return err;
5382+}
5383+
1facf9fc 5384+/*
5385+ * to support a sparse file which is opened with O_APPEND,
5386+ * we need to close the file.
5387+ */
c2b27bf2 5388+static int au_cp_regular(struct au_cp_generic *cpg)
1facf9fc 5389+{
5390+ int err, i;
5391+ enum { SRC, DST };
5392+ struct {
5393+ aufs_bindex_t bindex;
5394+ unsigned int flags;
5395+ struct dentry *dentry;
392086de 5396+ int force_wr;
1facf9fc 5397+ struct file *file;
1facf9fc 5398+ } *f, file[] = {
5399+ {
c2b27bf2 5400+ .bindex = cpg->bsrc,
1facf9fc 5401+ .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
1facf9fc 5402+ },
5403+ {
c2b27bf2 5404+ .bindex = cpg->bdst,
1facf9fc 5405+ .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
392086de 5406+ .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
1facf9fc 5407+ }
5408+ };
acd2b654 5409+ struct au_branch *br;
521ced18 5410+ struct super_block *sb, *h_src_sb;
e2f27e51 5411+ struct inode *h_src_inode;
8cdd5066 5412+ struct task_struct *tsk = current;
1facf9fc 5413+
5414+ /* bsrc branch can be ro/rw. */
c2b27bf2 5415+ sb = cpg->dentry->d_sb;
1facf9fc 5416+ f = file;
5417+ for (i = 0; i < 2; i++, f++) {
c2b27bf2
AM
5418+ f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5419+ f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
392086de 5420+ /*file*/NULL, f->force_wr);
e49925d1
JR
5421+ if (IS_ERR(f->file)) {
5422+ err = PTR_ERR(f->file);
5423+ if (i == SRC)
5424+ goto out;
5425+ else
5426+ goto out_src;
5427+ }
1facf9fc 5428+ }
5429+
5430+ /* try stopping to update while we copyup */
e2f27e51 5431+ h_src_inode = d_inode(file[SRC].dentry);
521ced18
JR
5432+ h_src_sb = h_src_inode->i_sb;
5433+ if (!au_test_nfs(h_src_sb))
e2f27e51 5434+ IMustLock(h_src_inode);
1c60b727 5435+ err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
1facf9fc 5436+
8cdd5066
JR
5437+ /* i wonder if we had O_NO_DELAY_FPUT flag */
5438+ if (tsk->flags & PF_KTHREAD)
5439+ __fput_sync(file[DST].file);
5440+ else {
062440b3 5441+ /* it happened actually */
8cdd5066
JR
5442+ fput(file[DST].file);
5443+ /*
5444+ * too bad.
5445+ * we have to call both since we don't know which place the file
5446+ * was added to.
5447+ */
5448+ task_work_run();
5449+ flush_delayed_fput();
5450+ }
acd2b654
AM
5451+ br = au_sbr(sb, file[DST].bindex);
5452+ au_lcnt_dec(&br->br_nfiles);
523b37e3 5453+
4f0767ce 5454+out_src:
1facf9fc 5455+ fput(file[SRC].file);
acd2b654
AM
5456+ br = au_sbr(sb, file[SRC].bindex);
5457+ au_lcnt_dec(&br->br_nfiles);
4f0767ce 5458+out:
1facf9fc 5459+ return err;
5460+}
5461+
c2b27bf2 5462+static int au_do_cpup_regular(struct au_cp_generic *cpg,
86dc4139 5463+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5464+{
5465+ int err, rerr;
5466+ loff_t l;
86dc4139 5467+ struct path h_path;
38d290e6 5468+ struct inode *h_src_inode, *h_dst_inode;
1facf9fc 5469+
5470+ err = 0;
5527c038 5471+ h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
86dc4139 5472+ l = i_size_read(h_src_inode);
c2b27bf2
AM
5473+ if (cpg->len == -1 || l < cpg->len)
5474+ cpg->len = l;
5475+ if (cpg->len) {
86dc4139 5476+ /* try stopping to update while we are referencing */
be118d29 5477+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
c2b27bf2 5478+ au_pin_hdir_unlock(cpg->pin);
1facf9fc 5479+
c2b27bf2
AM
5480+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5481+ h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
86dc4139 5482+ h_src_attr->iflags = h_src_inode->i_flags;
5527c038 5483+ if (!au_test_nfs(h_src_inode->i_sb))
521ced18 5484+ err = vfsub_getattr(&h_path, &h_src_attr->st);
5527c038 5485+ else {
3c1bdaff 5486+ inode_unlock_shared(h_src_inode);
521ced18 5487+ err = vfsub_getattr(&h_path, &h_src_attr->st);
be118d29 5488+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5527c038 5489+ }
86dc4139 5490+ if (unlikely(err)) {
3c1bdaff 5491+ inode_unlock_shared(h_src_inode);
86dc4139
AM
5492+ goto out;
5493+ }
5494+ h_src_attr->valid = 1;
e2f27e51
AM
5495+ if (!au_test_nfs(h_src_inode->i_sb)) {
5496+ err = au_cp_regular(cpg);
3c1bdaff 5497+ inode_unlock_shared(h_src_inode);
e2f27e51 5498+ } else {
3c1bdaff 5499+ inode_unlock_shared(h_src_inode);
e2f27e51
AM
5500+ err = au_cp_regular(cpg);
5501+ }
c2b27bf2 5502+ rerr = au_pin_hdir_relock(cpg->pin);
86dc4139
AM
5503+ if (!err && rerr)
5504+ err = rerr;
1facf9fc 5505+ }
38d290e6
JR
5506+ if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5507+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5527c038 5508+ h_dst_inode = d_inode(h_path.dentry);
38d290e6
JR
5509+ spin_lock(&h_dst_inode->i_lock);
5510+ h_dst_inode->i_state |= I_LINKABLE;
5511+ spin_unlock(&h_dst_inode->i_lock);
5512+ }
1facf9fc 5513+
4f0767ce 5514+out:
1facf9fc 5515+ return err;
5516+}
5517+
5518+static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5519+ struct inode *h_dir)
5520+{
5521+ int err, symlen;
5522+ mm_segment_t old_fs;
b752ccd1
AM
5523+ union {
5524+ char *k;
5525+ char __user *u;
5526+ } sym;
1facf9fc 5527+
5528+ err = -ENOMEM;
537831f9 5529+ sym.k = (void *)__get_free_page(GFP_NOFS);
b752ccd1 5530+ if (unlikely(!sym.k))
1facf9fc 5531+ goto out;
5532+
9dbd164d 5533+ /* unnecessary to support mmap_sem since symlink is not mmap-able */
1facf9fc 5534+ old_fs = get_fs();
5535+ set_fs(KERNEL_DS);
a2654f78 5536+ symlen = vfs_readlink(h_src, sym.u, PATH_MAX);
1facf9fc 5537+ err = symlen;
5538+ set_fs(old_fs);
5539+
5540+ if (symlen > 0) {
b752ccd1
AM
5541+ sym.k[symlen] = 0;
5542+ err = vfsub_symlink(h_dir, h_path, sym.k);
1facf9fc 5543+ }
1c60b727 5544+ free_page((unsigned long)sym.k);
1facf9fc 5545+
4f0767ce 5546+out:
1facf9fc 5547+ return err;
5548+}
5549+
8cdd5066
JR
5550+/*
5551+ * regardless 'acl' option, reset all ACL.
5552+ * All ACL will be copied up later from the original entry on the lower branch.
5553+ */
5554+static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5555+{
5556+ int err;
5557+ struct dentry *h_dentry;
5558+ struct inode *h_inode;
5559+
5560+ h_dentry = h_path->dentry;
5561+ h_inode = d_inode(h_dentry);
5562+ /* forget_all_cached_acls(h_inode)); */
5563+ err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5564+ AuTraceErr(err);
5565+ if (err == -EOPNOTSUPP)
5566+ err = 0;
5567+ if (!err)
5568+ err = vfsub_acl_chmod(h_inode, mode);
5569+
5570+ AuTraceErr(err);
5571+ return err;
5572+}
5573+
5574+static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5575+ struct inode *h_dir, struct path *h_path)
5576+{
5577+ int err;
5578+ struct inode *dir, *inode;
5579+
5580+ err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
5581+ AuTraceErr(err);
5582+ if (err == -EOPNOTSUPP)
5583+ err = 0;
5584+ if (unlikely(err))
5585+ goto out;
5586+
5587+ /*
5588+ * strange behaviour from the users view,
acd2b654 5589+ * particularly setattr case
8cdd5066
JR
5590+ */
5591+ dir = d_inode(dst_parent);
5afbbe0d 5592+ if (au_ibtop(dir) == cpg->bdst)
8cdd5066
JR
5593+ au_cpup_attr_nlink(dir, /*force*/1);
5594+ inode = d_inode(cpg->dentry);
5595+ au_cpup_attr_nlink(inode, /*force*/1);
5596+
5597+out:
5598+ return err;
5599+}
5600+
1facf9fc 5601+static noinline_for_stack
c2b27bf2 5602+int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
86dc4139 5603+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5604+{
5605+ int err;
5606+ umode_t mode;
5607+ unsigned int mnt_flags;
076b876e 5608+ unsigned char isdir, isreg, force;
c2b27bf2 5609+ const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5610+ struct au_dtime dt;
5611+ struct path h_path;
5612+ struct dentry *h_src, *h_dst, *h_parent;
8cdd5066 5613+ struct inode *h_inode, *h_dir;
1facf9fc 5614+ struct super_block *sb;
5615+
5616+ /* bsrc branch can be ro/rw. */
c2b27bf2 5617+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038
JR
5618+ h_inode = d_inode(h_src);
5619+ AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
1facf9fc 5620+
5621+ /* try stopping to be referenced while we are creating */
c2b27bf2
AM
5622+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5623+ if (au_ftest_cpup(cpg->flags, RENAME))
86dc4139
AM
5624+ AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5625+ AUFS_WH_PFX_LEN));
1facf9fc 5626+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5627+ h_dir = d_inode(h_parent);
1facf9fc 5628+ IMustLock(h_dir);
5629+ AuDebugOn(h_parent != h_dst->d_parent);
5630+
c2b27bf2
AM
5631+ sb = cpg->dentry->d_sb;
5632+ h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
1facf9fc 5633+ if (do_dt) {
5634+ h_path.dentry = h_parent;
5635+ au_dtime_store(&dt, dst_parent, &h_path);
5636+ }
5637+ h_path.dentry = h_dst;
5638+
076b876e 5639+ isreg = 0;
1facf9fc 5640+ isdir = 0;
5641+ mode = h_inode->i_mode;
5642+ switch (mode & S_IFMT) {
5643+ case S_IFREG:
076b876e 5644+ isreg = 1;
cd7a4cd9 5645+ err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
1facf9fc 5646+ if (!err)
c2b27bf2 5647+ err = au_do_cpup_regular(cpg, h_src_attr);
1facf9fc 5648+ break;
5649+ case S_IFDIR:
5650+ isdir = 1;
5651+ err = vfsub_mkdir(h_dir, &h_path, mode);
8cdd5066
JR
5652+ if (!err)
5653+ err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
1facf9fc 5654+ break;
5655+ case S_IFLNK:
5656+ err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5657+ break;
5658+ case S_IFCHR:
5659+ case S_IFBLK:
5660+ AuDebugOn(!capable(CAP_MKNOD));
5661+ /*FALLTHROUGH*/
5662+ case S_IFIFO:
5663+ case S_IFSOCK:
5664+ err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5665+ break;
5666+ default:
5667+ AuIOErr("Unknown inode type 0%o\n", mode);
5668+ err = -EIO;
5669+ }
8cdd5066
JR
5670+ if (!err)
5671+ err = au_reset_acl(h_dir, &h_path, mode);
1facf9fc 5672+
5673+ mnt_flags = au_mntflags(sb);
5674+ if (!au_opt_test(mnt_flags, UDBA_NONE)
5675+ && !isdir
5676+ && au_opt_test(mnt_flags, XINO)
38d290e6
JR
5677+ && (h_inode->i_nlink == 1
5678+ || (h_inode->i_state & I_LINKABLE))
1facf9fc 5679+ /* todo: unnecessary? */
5527c038 5680+ /* && d_inode(cpg->dentry)->i_nlink == 1 */
c2b27bf2
AM
5681+ && cpg->bdst < cpg->bsrc
5682+ && !au_ftest_cpup(cpg->flags, KEEPLINO))
5683+ au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
1facf9fc 5684+ /* ignore this error */
5685+
076b876e
AM
5686+ if (!err) {
5687+ force = 0;
5688+ if (isreg) {
5689+ force = !!cpg->len;
5690+ if (cpg->len == -1)
5691+ force = !!i_size_read(h_inode);
5692+ }
5693+ au_fhsm_wrote(sb, cpg->bdst, force);
5694+ }
5695+
1facf9fc 5696+ if (do_dt)
5697+ au_dtime_revert(&dt);
5698+ return err;
5699+}
5700+
392086de 5701+static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
86dc4139
AM
5702+{
5703+ int err;
392086de 5704+ struct dentry *dentry, *h_dentry, *h_parent, *parent;
86dc4139 5705+ struct inode *h_dir;
392086de 5706+ aufs_bindex_t bdst;
86dc4139 5707+
392086de
AM
5708+ dentry = cpg->dentry;
5709+ bdst = cpg->bdst;
5710+ h_dentry = au_h_dptr(dentry, bdst);
5711+ if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5712+ dget(h_dentry);
5713+ au_set_h_dptr(dentry, bdst, NULL);
5714+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
5715+ if (!err)
5716+ h_path->dentry = dget(au_h_dptr(dentry, bdst));
86dc4139 5717+ au_set_h_dptr(dentry, bdst, h_dentry);
392086de
AM
5718+ } else {
5719+ err = 0;
5720+ parent = dget_parent(dentry);
5721+ h_parent = au_h_dptr(parent, bdst);
5722+ dput(parent);
5723+ h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5724+ if (IS_ERR(h_path->dentry))
5725+ err = PTR_ERR(h_path->dentry);
86dc4139 5726+ }
392086de
AM
5727+ if (unlikely(err))
5728+ goto out;
86dc4139 5729+
86dc4139 5730+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 5731+ h_dir = d_inode(h_parent);
86dc4139 5732+ IMustLock(h_dir);
523b37e3
AM
5733+ AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5734+ /* no delegation since it is just created */
f2c43d5f
AM
5735+ err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5736+ /*flags*/0);
86dc4139
AM
5737+ dput(h_path->dentry);
5738+
5739+out:
5740+ return err;
5741+}
5742+
1facf9fc 5743+/*
5744+ * copyup the @dentry from @bsrc to @bdst.
5745+ * the caller must set the both of lower dentries.
5746+ * @len is for truncating when it is -1 copyup the entire file.
5747+ * in link/rename cases, @dst_parent may be different from the real one.
c2b27bf2 5748+ * basic->bsrc can be larger than basic->bdst.
f2c43d5f 5749+ * aufs doesn't touch the credential so
acd2b654 5750+ * security_inode_copy_up{,_xattr}() are unnecessary.
1facf9fc 5751+ */
c2b27bf2 5752+static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5753+{
5754+ int err, rerr;
5afbbe0d 5755+ aufs_bindex_t old_ibtop;
1facf9fc 5756+ unsigned char isdir, plink;
1facf9fc 5757+ struct dentry *h_src, *h_dst, *h_parent;
5527c038 5758+ struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
1facf9fc 5759+ struct super_block *sb;
86dc4139 5760+ struct au_branch *br;
acd2b654 5761+ /* to reduce stack size */
c2b27bf2
AM
5762+ struct {
5763+ struct au_dtime dt;
5764+ struct path h_path;
5765+ struct au_cpup_reg_attr h_src_attr;
5766+ } *a;
1facf9fc 5767+
c2b27bf2
AM
5768+ err = -ENOMEM;
5769+ a = kmalloc(sizeof(*a), GFP_NOFS);
5770+ if (unlikely(!a))
5771+ goto out;
5772+ a->h_src_attr.valid = 0;
1facf9fc 5773+
c2b27bf2
AM
5774+ sb = cpg->dentry->d_sb;
5775+ br = au_sbr(sb, cpg->bdst);
5776+ a->h_path.mnt = au_br_mnt(br);
5777+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
1facf9fc 5778+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5779+ h_dir = d_inode(h_parent);
1facf9fc 5780+ IMustLock(h_dir);
5781+
c2b27bf2 5782+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5783+ inode = d_inode(cpg->dentry);
1facf9fc 5784+
5785+ if (!dst_parent)
c2b27bf2 5786+ dst_parent = dget_parent(cpg->dentry);
1facf9fc 5787+ else
5788+ dget(dst_parent);
5789+
5790+ plink = !!au_opt_test(au_mntflags(sb), PLINK);
c2b27bf2 5791+ dst_inode = au_h_iptr(inode, cpg->bdst);
1facf9fc 5792+ if (dst_inode) {
5793+ if (unlikely(!plink)) {
5794+ err = -EIO;
027c5e7a
AM
5795+ AuIOErr("hi%lu(i%lu) exists on b%d "
5796+ "but plink is disabled\n",
c2b27bf2
AM
5797+ dst_inode->i_ino, inode->i_ino, cpg->bdst);
5798+ goto out_parent;
1facf9fc 5799+ }
5800+
5801+ if (dst_inode->i_nlink) {
c2b27bf2 5802+ const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5803+
c2b27bf2 5804+ h_src = au_plink_lkup(inode, cpg->bdst);
1facf9fc 5805+ err = PTR_ERR(h_src);
5806+ if (IS_ERR(h_src))
c2b27bf2 5807+ goto out_parent;
5527c038 5808+ if (unlikely(d_is_negative(h_src))) {
1facf9fc 5809+ err = -EIO;
79b8bda9 5810+ AuIOErr("i%lu exists on b%d "
027c5e7a 5811+ "but not pseudo-linked\n",
79b8bda9 5812+ inode->i_ino, cpg->bdst);
1facf9fc 5813+ dput(h_src);
c2b27bf2 5814+ goto out_parent;
1facf9fc 5815+ }
5816+
5817+ if (do_dt) {
c2b27bf2
AM
5818+ a->h_path.dentry = h_parent;
5819+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
1facf9fc 5820+ }
86dc4139 5821+
c2b27bf2 5822+ a->h_path.dentry = h_dst;
523b37e3
AM
5823+ delegated = NULL;
5824+ err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
c2b27bf2 5825+ if (!err && au_ftest_cpup(cpg->flags, RENAME))
392086de 5826+ err = au_do_ren_after_cpup(cpg, &a->h_path);
1facf9fc 5827+ if (do_dt)
c2b27bf2 5828+ au_dtime_revert(&a->dt);
523b37e3
AM
5829+ if (unlikely(err == -EWOULDBLOCK)) {
5830+ pr_warn("cannot retry for NFSv4 delegation"
5831+ " for an internal link\n");
5832+ iput(delegated);
5833+ }
1facf9fc 5834+ dput(h_src);
c2b27bf2 5835+ goto out_parent;
1facf9fc 5836+ } else
5837+ /* todo: cpup_wh_file? */
5838+ /* udba work */
4a4d8108 5839+ au_update_ibrange(inode, /*do_put_zero*/1);
1facf9fc 5840+ }
5841+
86dc4139 5842+ isdir = S_ISDIR(inode->i_mode);
5afbbe0d 5843+ old_ibtop = au_ibtop(inode);
c2b27bf2 5844+ err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
1facf9fc 5845+ if (unlikely(err))
86dc4139 5846+ goto out_rev;
5527c038 5847+ dst_inode = d_inode(h_dst);
febd17d6 5848+ inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
86dc4139 5849+ /* todo: necessary? */
c2b27bf2 5850+ /* au_pin_hdir_unlock(cpg->pin); */
1facf9fc 5851+
c2b27bf2 5852+ err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
86dc4139
AM
5853+ if (unlikely(err)) {
5854+ /* todo: necessary? */
c2b27bf2 5855+ /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
febd17d6 5856+ inode_unlock(dst_inode);
86dc4139
AM
5857+ goto out_rev;
5858+ }
5859+
5afbbe0d 5860+ if (cpg->bdst < old_ibtop) {
86dc4139 5861+ if (S_ISREG(inode->i_mode)) {
c2b27bf2 5862+ err = au_dy_iaop(inode, cpg->bdst, dst_inode);
86dc4139 5863+ if (unlikely(err)) {
c2b27bf2
AM
5864+ /* ignore an error */
5865+ /* au_pin_hdir_relock(cpg->pin); */
febd17d6 5866+ inode_unlock(dst_inode);
86dc4139 5867+ goto out_rev;
4a4d8108 5868+ }
4a4d8108 5869+ }
5afbbe0d 5870+ au_set_ibtop(inode, cpg->bdst);
c2b27bf2 5871+ } else
5afbbe0d 5872+ au_set_ibbot(inode, cpg->bdst);
c2b27bf2 5873+ au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
86dc4139
AM
5874+ au_hi_flags(inode, isdir));
5875+
5876+ /* todo: necessary? */
c2b27bf2 5877+ /* err = au_pin_hdir_relock(cpg->pin); */
febd17d6 5878+ inode_unlock(dst_inode);
86dc4139
AM
5879+ if (unlikely(err))
5880+ goto out_rev;
5881+
5527c038 5882+ src_inode = d_inode(h_src);
86dc4139 5883+ if (!isdir
5527c038
JR
5884+ && (src_inode->i_nlink > 1
5885+ || src_inode->i_state & I_LINKABLE)
86dc4139 5886+ && plink)
c2b27bf2 5887+ au_plink_append(inode, cpg->bdst, h_dst);
86dc4139 5888+
c2b27bf2
AM
5889+ if (au_ftest_cpup(cpg->flags, RENAME)) {
5890+ a->h_path.dentry = h_dst;
392086de 5891+ err = au_do_ren_after_cpup(cpg, &a->h_path);
86dc4139
AM
5892+ }
5893+ if (!err)
c2b27bf2 5894+ goto out_parent; /* success */
1facf9fc 5895+
5896+ /* revert */
4a4d8108 5897+out_rev:
c2b27bf2
AM
5898+ a->h_path.dentry = h_parent;
5899+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
5900+ a->h_path.dentry = h_dst;
86dc4139 5901+ rerr = 0;
5527c038 5902+ if (d_is_positive(h_dst)) {
523b37e3
AM
5903+ if (!isdir) {
5904+ /* no delegation since it is just created */
5905+ rerr = vfsub_unlink(h_dir, &a->h_path,
5906+ /*delegated*/NULL, /*force*/0);
5907+ } else
c2b27bf2 5908+ rerr = vfsub_rmdir(h_dir, &a->h_path);
86dc4139 5909+ }
c2b27bf2 5910+ au_dtime_revert(&a->dt);
1facf9fc 5911+ if (rerr) {
5912+ AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5913+ err = -EIO;
5914+ }
c2b27bf2 5915+out_parent:
1facf9fc 5916+ dput(dst_parent);
e49925d1 5917+ au_kfree_rcu(a);
c2b27bf2 5918+out:
1facf9fc 5919+ return err;
5920+}
5921+
7e9cd9fe 5922+#if 0 /* reserved */
1facf9fc 5923+struct au_cpup_single_args {
5924+ int *errp;
c2b27bf2 5925+ struct au_cp_generic *cpg;
1facf9fc 5926+ struct dentry *dst_parent;
5927+};
5928+
5929+static void au_call_cpup_single(void *args)
5930+{
5931+ struct au_cpup_single_args *a = args;
86dc4139 5932+
c2b27bf2
AM
5933+ au_pin_hdir_acquire_nest(a->cpg->pin);
5934+ *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5935+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 5936+}
c2b27bf2 5937+#endif
1facf9fc 5938+
53392da6
AM
5939+/*
5940+ * prevent SIGXFSZ in copy-up.
5941+ * testing CAP_MKNOD is for generic fs,
5942+ * but CAP_FSETID is for xfs only, currently.
5943+ */
86dc4139 5944+static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
53392da6
AM
5945+{
5946+ int do_sio;
86dc4139
AM
5947+ struct super_block *sb;
5948+ struct inode *h_dir;
53392da6
AM
5949+
5950+ do_sio = 0;
86dc4139 5951+ sb = au_pinned_parent(pin)->d_sb;
53392da6
AM
5952+ if (!au_wkq_test()
5953+ && (!au_sbi(sb)->si_plink_maint_pid
5954+ || au_plink_maint(sb, AuLock_NOPLM))) {
5955+ switch (mode & S_IFMT) {
5956+ case S_IFREG:
5957+ /* no condition about RLIMIT_FSIZE and the file size */
5958+ do_sio = 1;
5959+ break;
5960+ case S_IFCHR:
5961+ case S_IFBLK:
5962+ do_sio = !capable(CAP_MKNOD);
5963+ break;
5964+ }
5965+ if (!do_sio)
5966+ do_sio = ((mode & (S_ISUID | S_ISGID))
5967+ && !capable(CAP_FSETID));
86dc4139
AM
5968+ /* this workaround may be removed in the future */
5969+ if (!do_sio) {
5970+ h_dir = au_pinned_h_dir(pin);
5971+ do_sio = h_dir->i_mode & S_ISVTX;
5972+ }
53392da6
AM
5973+ }
5974+
5975+ return do_sio;
5976+}
5977+
7e9cd9fe 5978+#if 0 /* reserved */
c2b27bf2 5979+int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5980+{
5981+ int err, wkq_err;
1facf9fc 5982+ struct dentry *h_dentry;
5983+
c2b27bf2 5984+ h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5985+ if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
c2b27bf2 5986+ err = au_cpup_single(cpg, dst_parent);
1facf9fc 5987+ else {
5988+ struct au_cpup_single_args args = {
5989+ .errp = &err,
c2b27bf2
AM
5990+ .cpg = cpg,
5991+ .dst_parent = dst_parent
1facf9fc 5992+ };
5993+ wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5994+ if (unlikely(wkq_err))
5995+ err = wkq_err;
5996+ }
5997+
5998+ return err;
5999+}
c2b27bf2 6000+#endif
1facf9fc 6001+
6002+/*
6003+ * copyup the @dentry from the first active lower branch to @bdst,
6004+ * using au_cpup_single().
6005+ */
c2b27bf2 6006+static int au_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 6007+{
6008+ int err;
c2b27bf2
AM
6009+ unsigned int flags_orig;
6010+ struct dentry *dentry;
6011+
6012+ AuDebugOn(cpg->bsrc < 0);
1facf9fc 6013+
c2b27bf2 6014+ dentry = cpg->dentry;
86dc4139 6015+ DiMustWriteLock(dentry);
1facf9fc 6016+
c2b27bf2 6017+ err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
1facf9fc 6018+ if (!err) {
c2b27bf2
AM
6019+ flags_orig = cpg->flags;
6020+ au_fset_cpup(cpg->flags, RENAME);
6021+ err = au_cpup_single(cpg, NULL);
6022+ cpg->flags = flags_orig;
1facf9fc 6023+ if (!err)
6024+ return 0; /* success */
6025+
6026+ /* revert */
c2b27bf2 6027+ au_set_h_dptr(dentry, cpg->bdst, NULL);
5afbbe0d 6028+ au_set_dbtop(dentry, cpg->bsrc);
1facf9fc 6029+ }
6030+
6031+ return err;
6032+}
6033+
6034+struct au_cpup_simple_args {
6035+ int *errp;
c2b27bf2 6036+ struct au_cp_generic *cpg;
1facf9fc 6037+};
6038+
6039+static void au_call_cpup_simple(void *args)
6040+{
6041+ struct au_cpup_simple_args *a = args;
86dc4139 6042+
c2b27bf2
AM
6043+ au_pin_hdir_acquire_nest(a->cpg->pin);
6044+ *a->errp = au_cpup_simple(a->cpg);
6045+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 6046+}
6047+
c2b27bf2 6048+static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 6049+{
6050+ int err, wkq_err;
c2b27bf2
AM
6051+ struct dentry *dentry, *parent;
6052+ struct file *h_file;
1facf9fc 6053+ struct inode *h_dir;
6054+
c2b27bf2
AM
6055+ dentry = cpg->dentry;
6056+ h_file = NULL;
6057+ if (au_ftest_cpup(cpg->flags, HOPEN)) {
6058+ AuDebugOn(cpg->bsrc < 0);
392086de 6059+ h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
c2b27bf2
AM
6060+ err = PTR_ERR(h_file);
6061+ if (IS_ERR(h_file))
6062+ goto out;
6063+ }
6064+
1facf9fc 6065+ parent = dget_parent(dentry);
5527c038 6066+ h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
53392da6 6067+ if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
5527c038 6068+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 6069+ err = au_cpup_simple(cpg);
1facf9fc 6070+ else {
6071+ struct au_cpup_simple_args args = {
6072+ .errp = &err,
c2b27bf2 6073+ .cpg = cpg
1facf9fc 6074+ };
6075+ wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
6076+ if (unlikely(wkq_err))
6077+ err = wkq_err;
6078+ }
6079+
6080+ dput(parent);
c2b27bf2
AM
6081+ if (h_file)
6082+ au_h_open_post(dentry, cpg->bsrc, h_file);
6083+
6084+out:
1facf9fc 6085+ return err;
6086+}
6087+
c2b27bf2 6088+int au_sio_cpup_simple(struct au_cp_generic *cpg)
367653fa 6089+{
5afbbe0d 6090+ aufs_bindex_t bsrc, bbot;
c2b27bf2 6091+ struct dentry *dentry, *h_dentry;
367653fa 6092+
c2b27bf2
AM
6093+ if (cpg->bsrc < 0) {
6094+ dentry = cpg->dentry;
5afbbe0d
AM
6095+ bbot = au_dbbot(dentry);
6096+ for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
c2b27bf2
AM
6097+ h_dentry = au_h_dptr(dentry, bsrc);
6098+ if (h_dentry) {
5527c038 6099+ AuDebugOn(d_is_negative(h_dentry));
c2b27bf2
AM
6100+ break;
6101+ }
6102+ }
5afbbe0d 6103+ AuDebugOn(bsrc > bbot);
c2b27bf2 6104+ cpg->bsrc = bsrc;
367653fa 6105+ }
c2b27bf2
AM
6106+ AuDebugOn(cpg->bsrc <= cpg->bdst);
6107+ return au_do_sio_cpup_simple(cpg);
6108+}
367653fa 6109+
c2b27bf2
AM
6110+int au_sio_cpdown_simple(struct au_cp_generic *cpg)
6111+{
6112+ AuDebugOn(cpg->bdst <= cpg->bsrc);
6113+ return au_do_sio_cpup_simple(cpg);
367653fa
AM
6114+}
6115+
1facf9fc 6116+/* ---------------------------------------------------------------------- */
6117+
6118+/*
6119+ * copyup the deleted file for writing.
6120+ */
c2b27bf2
AM
6121+static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
6122+ struct file *file)
1facf9fc 6123+{
6124+ int err;
c2b27bf2
AM
6125+ unsigned int flags_orig;
6126+ aufs_bindex_t bsrc_orig;
c2b27bf2 6127+ struct au_dinfo *dinfo;
5afbbe0d
AM
6128+ struct {
6129+ struct au_hdentry *hd;
6130+ struct dentry *h_dentry;
6131+ } hdst, hsrc;
1facf9fc 6132+
c2b27bf2 6133+ dinfo = au_di(cpg->dentry);
1308ab2a 6134+ AuRwMustWriteLock(&dinfo->di_rwsem);
6135+
c2b27bf2 6136+ bsrc_orig = cpg->bsrc;
5afbbe0d
AM
6137+ cpg->bsrc = dinfo->di_btop;
6138+ hdst.hd = au_hdentry(dinfo, cpg->bdst);
6139+ hdst.h_dentry = hdst.hd->hd_dentry;
6140+ hdst.hd->hd_dentry = wh_dentry;
6141+ dinfo->di_btop = cpg->bdst;
6142+
6143+ hsrc.h_dentry = NULL;
027c5e7a 6144+ if (file) {
5afbbe0d
AM
6145+ hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
6146+ hsrc.h_dentry = hsrc.hd->hd_dentry;
6147+ hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
027c5e7a 6148+ }
c2b27bf2
AM
6149+ flags_orig = cpg->flags;
6150+ cpg->flags = !AuCpup_DTIME;
6151+ err = au_cpup_single(cpg, /*h_parent*/NULL);
6152+ cpg->flags = flags_orig;
027c5e7a
AM
6153+ if (file) {
6154+ if (!err)
6155+ err = au_reopen_nondir(file);
5afbbe0d 6156+ hsrc.hd->hd_dentry = hsrc.h_dentry;
1facf9fc 6157+ }
5afbbe0d
AM
6158+ hdst.hd->hd_dentry = hdst.h_dentry;
6159+ dinfo->di_btop = cpg->bsrc;
c2b27bf2 6160+ cpg->bsrc = bsrc_orig;
1facf9fc 6161+
6162+ return err;
6163+}
6164+
c2b27bf2 6165+static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 6166+{
6167+ int err;
c2b27bf2 6168+ aufs_bindex_t bdst;
1facf9fc 6169+ struct au_dtime dt;
c2b27bf2 6170+ struct dentry *dentry, *parent, *h_parent, *wh_dentry;
1facf9fc 6171+ struct au_branch *br;
6172+ struct path h_path;
6173+
c2b27bf2
AM
6174+ dentry = cpg->dentry;
6175+ bdst = cpg->bdst;
1facf9fc 6176+ br = au_sbr(dentry->d_sb, bdst);
6177+ parent = dget_parent(dentry);
6178+ h_parent = au_h_dptr(parent, bdst);
6179+ wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
6180+ err = PTR_ERR(wh_dentry);
6181+ if (IS_ERR(wh_dentry))
6182+ goto out;
6183+
6184+ h_path.dentry = h_parent;
86dc4139 6185+ h_path.mnt = au_br_mnt(br);
1facf9fc 6186+ au_dtime_store(&dt, parent, &h_path);
c2b27bf2 6187+ err = au_do_cpup_wh(cpg, wh_dentry, file);
1facf9fc 6188+ if (unlikely(err))
6189+ goto out_wh;
6190+
6191+ dget(wh_dentry);
6192+ h_path.dentry = wh_dentry;
2000de60 6193+ if (!d_is_dir(wh_dentry)) {
523b37e3 6194+ /* no delegation since it is just created */
5527c038 6195+ err = vfsub_unlink(d_inode(h_parent), &h_path,
523b37e3
AM
6196+ /*delegated*/NULL, /*force*/0);
6197+ } else
5527c038 6198+ err = vfsub_rmdir(d_inode(h_parent), &h_path);
1facf9fc 6199+ if (unlikely(err)) {
523b37e3
AM
6200+ AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6201+ wh_dentry, err);
1facf9fc 6202+ err = -EIO;
6203+ }
6204+ au_dtime_revert(&dt);
5527c038 6205+ au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
1facf9fc 6206+
4f0767ce 6207+out_wh:
1facf9fc 6208+ dput(wh_dentry);
4f0767ce 6209+out:
1facf9fc 6210+ dput(parent);
6211+ return err;
6212+}
6213+
6214+struct au_cpup_wh_args {
6215+ int *errp;
c2b27bf2 6216+ struct au_cp_generic *cpg;
1facf9fc 6217+ struct file *file;
6218+};
6219+
6220+static void au_call_cpup_wh(void *args)
6221+{
6222+ struct au_cpup_wh_args *a = args;
86dc4139 6223+
c2b27bf2
AM
6224+ au_pin_hdir_acquire_nest(a->cpg->pin);
6225+ *a->errp = au_cpup_wh(a->cpg, a->file);
6226+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 6227+}
6228+
c2b27bf2 6229+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 6230+{
6231+ int err, wkq_err;
c2b27bf2 6232+ aufs_bindex_t bdst;
c1595e42 6233+ struct dentry *dentry, *parent, *h_orph, *h_parent;
86dc4139 6234+ struct inode *dir, *h_dir, *h_tmpdir;
1facf9fc 6235+ struct au_wbr *wbr;
c2b27bf2 6236+ struct au_pin wh_pin, *pin_orig;
1facf9fc 6237+
c2b27bf2
AM
6238+ dentry = cpg->dentry;
6239+ bdst = cpg->bdst;
1facf9fc 6240+ parent = dget_parent(dentry);
5527c038 6241+ dir = d_inode(parent);
1facf9fc 6242+ h_orph = NULL;
6243+ h_parent = NULL;
6244+ h_dir = au_igrab(au_h_iptr(dir, bdst));
6245+ h_tmpdir = h_dir;
c2b27bf2 6246+ pin_orig = NULL;
1facf9fc 6247+ if (!h_dir->i_nlink) {
6248+ wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6249+ h_orph = wbr->wbr_orph;
6250+
6251+ h_parent = dget(au_h_dptr(parent, bdst));
1facf9fc 6252+ au_set_h_dptr(parent, bdst, dget(h_orph));
5527c038 6253+ h_tmpdir = d_inode(h_orph);
1facf9fc 6254+ au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6255+
febd17d6 6256+ inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
4a4d8108 6257+ /* todo: au_h_open_pre()? */
86dc4139 6258+
c2b27bf2 6259+ pin_orig = cpg->pin;
86dc4139 6260+ au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
c2b27bf2
AM
6261+ AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6262+ cpg->pin = &wh_pin;
1facf9fc 6263+ }
6264+
53392da6 6265+ if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
5527c038 6266+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 6267+ err = au_cpup_wh(cpg, file);
1facf9fc 6268+ else {
6269+ struct au_cpup_wh_args args = {
6270+ .errp = &err,
c2b27bf2
AM
6271+ .cpg = cpg,
6272+ .file = file
1facf9fc 6273+ };
6274+ wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6275+ if (unlikely(wkq_err))
6276+ err = wkq_err;
6277+ }
6278+
6279+ if (h_orph) {
febd17d6 6280+ inode_unlock(h_tmpdir);
4a4d8108 6281+ /* todo: au_h_open_post()? */
1facf9fc 6282+ au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
1facf9fc 6283+ au_set_h_dptr(parent, bdst, h_parent);
c2b27bf2
AM
6284+ AuDebugOn(!pin_orig);
6285+ cpg->pin = pin_orig;
1facf9fc 6286+ }
6287+ iput(h_dir);
6288+ dput(parent);
6289+
6290+ return err;
6291+}
6292+
6293+/* ---------------------------------------------------------------------- */
6294+
6295+/*
6296+ * generic routine for both of copy-up and copy-down.
6297+ */
6298+/* cf. revalidate function in file.c */
6299+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6300+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6301+ struct au_pin *pin,
1facf9fc 6302+ struct dentry *h_parent, void *arg),
6303+ void *arg)
6304+{
6305+ int err;
6306+ struct au_pin pin;
5527c038 6307+ struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
1facf9fc 6308+
6309+ err = 0;
6310+ parent = dget_parent(dentry);
6311+ if (IS_ROOT(parent))
6312+ goto out;
6313+
6314+ au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6315+ au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6316+
6317+ /* do not use au_dpage */
6318+ real_parent = parent;
6319+ while (1) {
6320+ dput(parent);
6321+ parent = dget_parent(dentry);
6322+ h_parent = au_h_dptr(parent, bdst);
6323+ if (h_parent)
6324+ goto out; /* success */
6325+
6326+ /* find top dir which is necessary to cpup */
6327+ do {
6328+ d = parent;
6329+ dput(parent);
6330+ parent = dget_parent(d);
6331+ di_read_lock_parent3(parent, !AuLock_IR);
6332+ h_parent = au_h_dptr(parent, bdst);
6333+ di_read_unlock(parent, !AuLock_IR);
6334+ } while (!h_parent);
6335+
6336+ if (d != real_parent)
6337+ di_write_lock_child3(d);
6338+
6339+ /* somebody else might create while we were sleeping */
5527c038
JR
6340+ h_dentry = au_h_dptr(d, bdst);
6341+ if (!h_dentry || d_is_negative(h_dentry)) {
6342+ if (h_dentry)
5afbbe0d 6343+ au_update_dbtop(d);
1facf9fc 6344+
6345+ au_pin_set_dentry(&pin, d);
6346+ err = au_do_pin(&pin);
6347+ if (!err) {
86dc4139 6348+ err = cp(d, bdst, &pin, h_parent, arg);
1facf9fc 6349+ au_unpin(&pin);
6350+ }
6351+ }
6352+
6353+ if (d != real_parent)
6354+ di_write_unlock(d);
6355+ if (unlikely(err))
6356+ break;
6357+ }
6358+
4f0767ce 6359+out:
1facf9fc 6360+ dput(parent);
6361+ return err;
6362+}
6363+
6364+static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6365+ struct au_pin *pin,
2000de60 6366+ struct dentry *h_parent __maybe_unused,
1facf9fc 6367+ void *arg __maybe_unused)
6368+{
c2b27bf2
AM
6369+ struct au_cp_generic cpg = {
6370+ .dentry = dentry,
6371+ .bdst = bdst,
6372+ .bsrc = -1,
6373+ .len = 0,
6374+ .pin = pin,
6375+ .flags = AuCpup_DTIME
6376+ };
6377+ return au_sio_cpup_simple(&cpg);
1facf9fc 6378+}
6379+
6380+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6381+{
6382+ return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6383+}
6384+
6385+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6386+{
6387+ int err;
6388+ struct dentry *parent;
6389+ struct inode *dir;
6390+
6391+ parent = dget_parent(dentry);
5527c038 6392+ dir = d_inode(parent);
1facf9fc 6393+ err = 0;
6394+ if (au_h_iptr(dir, bdst))
6395+ goto out;
6396+
6397+ di_read_unlock(parent, AuLock_IR);
6398+ di_write_lock_parent(parent);
6399+ /* someone else might change our inode while we were sleeping */
6400+ if (!au_h_iptr(dir, bdst))
6401+ err = au_cpup_dirs(dentry, bdst);
6402+ di_downgrade_lock(parent, AuLock_IR);
6403+
4f0767ce 6404+out:
1facf9fc 6405+ dput(parent);
6406+ return err;
6407+}
7f207e10
AM
6408diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6409--- /usr/share/empty/fs/aufs/cpup.h 1970-01-01 01:00:00.000000000 +0100
e49925d1 6410+++ linux/fs/aufs/cpup.h 2019-01-28 14:36:12.219084659 +0100
062440b3
AM
6411@@ -0,0 +1,100 @@
6412+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 6413+/*
b00004a5 6414+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 6415+ *
6416+ * This program, aufs is free software; you can redistribute it and/or modify
6417+ * it under the terms of the GNU General Public License as published by
6418+ * the Free Software Foundation; either version 2 of the License, or
6419+ * (at your option) any later version.
dece6358
AM
6420+ *
6421+ * This program is distributed in the hope that it will be useful,
6422+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6423+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6424+ * GNU General Public License for more details.
6425+ *
6426+ * You should have received a copy of the GNU General Public License
523b37e3 6427+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6428+ */
6429+
6430+/*
6431+ * copy-up/down functions
6432+ */
6433+
6434+#ifndef __AUFS_CPUP_H__
6435+#define __AUFS_CPUP_H__
6436+
6437+#ifdef __KERNEL__
6438+
dece6358 6439+#include <linux/path.h>
1facf9fc 6440+
dece6358
AM
6441+struct inode;
6442+struct file;
86dc4139 6443+struct au_pin;
dece6358 6444+
86dc4139 6445+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
1facf9fc 6446+void au_cpup_attr_timesizes(struct inode *inode);
6447+void au_cpup_attr_nlink(struct inode *inode, int force);
6448+void au_cpup_attr_changeable(struct inode *inode);
6449+void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6450+void au_cpup_attr_all(struct inode *inode, int force);
6451+
6452+/* ---------------------------------------------------------------------- */
6453+
c2b27bf2
AM
6454+struct au_cp_generic {
6455+ struct dentry *dentry;
6456+ aufs_bindex_t bdst, bsrc;
6457+ loff_t len;
6458+ struct au_pin *pin;
6459+ unsigned int flags;
6460+};
6461+
1facf9fc 6462+/* cpup flags */
392086de
AM
6463+#define AuCpup_DTIME 1 /* do dtime_store/revert */
6464+#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
6465+ for link(2) */
6466+#define AuCpup_RENAME (1 << 2) /* rename after cpup */
6467+#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
6468+ cpup */
6469+#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
6470+ existing entry */
6471+#define AuCpup_RWDST (1 << 5) /* force write target even if
6472+ the branch is marked as RO */
c2b27bf2 6473+
8b6a4947
AM
6474+#ifndef CONFIG_AUFS_BR_HFSPLUS
6475+#undef AuCpup_HOPEN
6476+#define AuCpup_HOPEN 0
6477+#endif
6478+
1facf9fc 6479+#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
7f207e10
AM
6480+#define au_fset_cpup(flags, name) \
6481+ do { (flags) |= AuCpup_##name; } while (0)
6482+#define au_fclr_cpup(flags, name) \
6483+ do { (flags) &= ~AuCpup_##name; } while (0)
1facf9fc 6484+
6485+int au_copy_file(struct file *dst, struct file *src, loff_t len);
c2b27bf2
AM
6486+int au_sio_cpup_simple(struct au_cp_generic *cpg);
6487+int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6488+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
1facf9fc 6489+
6490+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6491+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6492+ struct au_pin *pin,
1facf9fc 6493+ struct dentry *h_parent, void *arg),
6494+ void *arg);
6495+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6496+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6497+
6498+/* ---------------------------------------------------------------------- */
6499+
6500+/* keep timestamps when copyup */
6501+struct au_dtime {
6502+ struct dentry *dt_dentry;
6503+ struct path dt_h_path;
cd7a4cd9 6504+ struct timespec64 dt_atime, dt_mtime;
1facf9fc 6505+};
6506+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6507+ struct path *h_path);
6508+void au_dtime_revert(struct au_dtime *dt);
6509+
6510+#endif /* __KERNEL__ */
6511+#endif /* __AUFS_CPUP_H__ */
7f207e10
AM
6512diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6513--- /usr/share/empty/fs/aufs/dbgaufs.c 1970-01-01 01:00:00.000000000 +0100
e49925d1
JR
6514+++ linux/fs/aufs/dbgaufs.c 2019-01-28 14:36:12.219084659 +0100
6515@@ -0,0 +1,526 @@
cd7a4cd9 6516+// SPDX-License-Identifier: GPL-2.0
1facf9fc 6517+/*
b00004a5 6518+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 6519+ *
6520+ * This program, aufs is free software; you can redistribute it and/or modify
6521+ * it under the terms of the GNU General Public License as published by
6522+ * the Free Software Foundation; either version 2 of the License, or
6523+ * (at your option) any later version.
dece6358
AM
6524+ *
6525+ * This program is distributed in the hope that it will be useful,
6526+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6527+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6528+ * GNU General Public License for more details.
6529+ *
6530+ * You should have received a copy of the GNU General Public License
523b37e3 6531+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6532+ */
6533+
6534+/*
6535+ * debugfs interface
6536+ */
6537+
6538+#include <linux/debugfs.h>
6539+#include "aufs.h"
6540+
6541+#ifndef CONFIG_SYSFS
6542+#error DEBUG_FS depends upon SYSFS
6543+#endif
6544+
6545+static struct dentry *dbgaufs;
cd7a4cd9 6546+static const mode_t dbgaufs_mode = 0444;
1facf9fc 6547+
6548+/* 20 is max digits length of ulong 64 */
6549+struct dbgaufs_arg {
6550+ int n;
6551+ char a[20 * 4];
6552+};
6553+
6554+/*
6555+ * common function for all XINO files
6556+ */
6557+static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6558+ struct file *file)
6559+{
e49925d1
JR
6560+ void *p;
6561+
6562+ p = file->private_data;
6563+ if (p) {
6564+ /* this is struct dbgaufs_arg */
6565+ AuDebugOn(!au_kfree_sz_test(p));
6566+ au_kfree_do_rcu(p);
6567+ }
1facf9fc 6568+ return 0;
6569+}
6570+
062440b3
AM
6571+static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6572+ int cnt)
1facf9fc 6573+{
6574+ int err;
6575+ struct kstat st;
6576+ struct dbgaufs_arg *p;
6577+
6578+ err = -ENOMEM;
6579+ p = kmalloc(sizeof(*p), GFP_NOFS);
6580+ if (unlikely(!p))
6581+ goto out;
6582+
6583+ err = 0;
6584+ p->n = 0;
6585+ file->private_data = p;
6586+ if (!xf)
6587+ goto out;
6588+
521ced18 6589+ err = vfsub_getattr(&xf->f_path, &st);
1facf9fc 6590+ if (!err) {
6591+ if (do_fcnt)
6592+ p->n = snprintf
062440b3
AM
6593+ (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6594+ cnt, st.blocks, st.blksize,
1facf9fc 6595+ (long long)st.size);
6596+ else
521ced18 6597+ p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
1facf9fc 6598+ st.blocks, st.blksize,
6599+ (long long)st.size);
6600+ AuDebugOn(p->n >= sizeof(p->a));
6601+ } else {
6602+ p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6603+ err = 0;
6604+ }
6605+
4f0767ce 6606+out:
1facf9fc 6607+ return err;
1facf9fc 6608+}
6609+
6610+static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6611+ size_t count, loff_t *ppos)
6612+{
6613+ struct dbgaufs_arg *p;
6614+
6615+ p = file->private_data;
6616+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6617+}
6618+
6619+/* ---------------------------------------------------------------------- */
6620+
86dc4139
AM
6621+struct dbgaufs_plink_arg {
6622+ int n;
6623+ char a[];
6624+};
6625+
6626+static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6627+ struct file *file)
6628+{
1c60b727 6629+ free_page((unsigned long)file->private_data);
86dc4139
AM
6630+ return 0;
6631+}
6632+
6633+static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6634+{
6635+ int err, i, limit;
6636+ unsigned long n, sum;
6637+ struct dbgaufs_plink_arg *p;
6638+ struct au_sbinfo *sbinfo;
6639+ struct super_block *sb;
8b6a4947 6640+ struct hlist_bl_head *hbl;
86dc4139
AM
6641+
6642+ err = -ENOMEM;
6643+ p = (void *)get_zeroed_page(GFP_NOFS);
6644+ if (unlikely(!p))
6645+ goto out;
6646+
6647+ err = -EFBIG;
6648+ sbinfo = inode->i_private;
6649+ sb = sbinfo->si_sb;
6650+ si_noflush_read_lock(sb);
6651+ if (au_opt_test(au_mntflags(sb), PLINK)) {
6652+ limit = PAGE_SIZE - sizeof(p->n);
6653+
6654+ /* the number of buckets */
6655+ n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6656+ p->n += n;
6657+ limit -= n;
6658+
6659+ sum = 0;
8b6a4947
AM
6660+ for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6661+ i++, hbl++) {
6662+ n = au_hbl_count(hbl);
86dc4139
AM
6663+ sum += n;
6664+
6665+ n = snprintf(p->a + p->n, limit, "%lu ", n);
6666+ p->n += n;
6667+ limit -= n;
6668+ if (unlikely(limit <= 0))
6669+ goto out_free;
6670+ }
6671+ p->a[p->n - 1] = '\n';
6672+
6673+ /* the sum of plinks */
6674+ n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6675+ p->n += n;
6676+ limit -= n;
6677+ if (unlikely(limit <= 0))
6678+ goto out_free;
6679+ } else {
6680+#define str "1\n0\n0\n"
6681+ p->n = sizeof(str) - 1;
6682+ strcpy(p->a, str);
6683+#undef str
6684+ }
6685+ si_read_unlock(sb);
6686+
6687+ err = 0;
6688+ file->private_data = p;
6689+ goto out; /* success */
6690+
6691+out_free:
1c60b727 6692+ free_page((unsigned long)p);
86dc4139
AM
6693+out:
6694+ return err;
6695+}
6696+
6697+static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6698+ size_t count, loff_t *ppos)
6699+{
6700+ struct dbgaufs_plink_arg *p;
6701+
6702+ p = file->private_data;
6703+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6704+}
6705+
6706+static const struct file_operations dbgaufs_plink_fop = {
6707+ .owner = THIS_MODULE,
6708+ .open = dbgaufs_plink_open,
6709+ .release = dbgaufs_plink_release,
6710+ .read = dbgaufs_plink_read
6711+};
6712+
6713+/* ---------------------------------------------------------------------- */
6714+
1facf9fc 6715+static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6716+{
6717+ int err;
6718+ struct au_sbinfo *sbinfo;
6719+ struct super_block *sb;
6720+
6721+ sbinfo = inode->i_private;
6722+ sb = sbinfo->si_sb;
6723+ si_noflush_read_lock(sb);
062440b3 6724+ err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
1facf9fc 6725+ si_read_unlock(sb);
6726+ return err;
6727+}
6728+
6729+static const struct file_operations dbgaufs_xib_fop = {
4a4d8108 6730+ .owner = THIS_MODULE,
1facf9fc 6731+ .open = dbgaufs_xib_open,
6732+ .release = dbgaufs_xi_release,
6733+ .read = dbgaufs_xi_read
6734+};
6735+
6736+/* ---------------------------------------------------------------------- */
6737+
6738+#define DbgaufsXi_PREFIX "xi"
6739+
6740+static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6741+{
acd2b654 6742+ int err, idx;
1facf9fc 6743+ long l;
acd2b654
AM
6744+ aufs_bindex_t bindex;
6745+ char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
1facf9fc 6746+ struct au_sbinfo *sbinfo;
6747+ struct super_block *sb;
acd2b654 6748+ struct au_xino *xi;
1facf9fc 6749+ struct file *xf;
6750+ struct qstr *name;
062440b3 6751+ struct au_branch *br;
1facf9fc 6752+
6753+ err = -ENOENT;
2000de60 6754+ name = &file->f_path.dentry->d_name;
1facf9fc 6755+ if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6756+ || memcmp(name->name, DbgaufsXi_PREFIX,
6757+ sizeof(DbgaufsXi_PREFIX) - 1)))
6758+ goto out;
acd2b654
AM
6759+
6760+ AuDebugOn(name->len >= sizeof(a));
6761+ memcpy(a, name->name, name->len);
6762+ a[name->len] = '\0';
6763+ p = strchr(a, '-');
6764+ if (p)
6765+ *p = '\0';
6766+ err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
1facf9fc 6767+ if (unlikely(err))
6768+ goto out;
acd2b654
AM
6769+ bindex = l;
6770+ idx = 0;
6771+ if (p) {
6772+ err = kstrtol(p + 1, 10, &l);
6773+ if (unlikely(err))
6774+ goto out;
6775+ idx = l;
6776+ }
1facf9fc 6777+
acd2b654 6778+ err = -ENOENT;
1facf9fc 6779+ sbinfo = inode->i_private;
6780+ sb = sbinfo->si_sb;
6781+ si_noflush_read_lock(sb);
acd2b654
AM
6782+ if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6783+ goto out_si;
6784+ br = au_sbr(sb, bindex);
6785+ xi = br->br_xino;
6786+ if (unlikely(idx >= xi->xi_nfile))
6787+ goto out_si;
6788+ xf = au_xino_file(xi, idx);
6789+ if (xf)
062440b3
AM
6790+ err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6791+ au_xino_count(br));
1facf9fc 6792+
acd2b654
AM
6793+out_si:
6794+ si_read_unlock(sb);
4f0767ce 6795+out:
acd2b654 6796+ AuTraceErr(err);
1facf9fc 6797+ return err;
6798+}
6799+
6800+static const struct file_operations dbgaufs_xino_fop = {
4a4d8108 6801+ .owner = THIS_MODULE,
1facf9fc 6802+ .open = dbgaufs_xino_open,
6803+ .release = dbgaufs_xi_release,
6804+ .read = dbgaufs_xi_read
6805+};
6806+
062440b3
AM
6807+void dbgaufs_xino_del(struct au_branch *br)
6808+{
6809+ struct dentry *dbgaufs;
6810+
6811+ dbgaufs = br->br_dbgaufs;
6812+ if (!dbgaufs)
6813+ return;
6814+
6815+ br->br_dbgaufs = NULL;
6816+ /* debugfs acquires the parent i_mutex */
6817+ lockdep_off();
6818+ debugfs_remove(dbgaufs);
6819+ lockdep_on();
6820+}
6821+
1facf9fc 6822+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6823+{
5afbbe0d 6824+ aufs_bindex_t bbot;
1facf9fc 6825+ struct au_branch *br;
1facf9fc 6826+
6827+ if (!au_sbi(sb)->si_dbgaufs)
6828+ return;
6829+
5afbbe0d
AM
6830+ bbot = au_sbbot(sb);
6831+ for (; bindex <= bbot; bindex++) {
1facf9fc 6832+ br = au_sbr(sb, bindex);
062440b3
AM
6833+ dbgaufs_xino_del(br);
6834+ }
6835+}
6836+
acd2b654
AM
6837+static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6838+ unsigned int idx, struct dentry *parent,
6839+ struct au_sbinfo *sbinfo)
062440b3
AM
6840+{
6841+ struct au_branch *br;
6842+ struct dentry *d;
acd2b654
AM
6843+ /* "xi" bindex(5) "-" idx(2) NULL */
6844+ char name[sizeof(DbgaufsXi_PREFIX) + 8];
062440b3 6845+
acd2b654
AM
6846+ if (!idx)
6847+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6848+ else
6849+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6850+ bindex, idx);
062440b3
AM
6851+ br = au_sbr(sb, bindex);
6852+ if (br->br_dbgaufs) {
6853+ struct qstr qstr = QSTR_INIT(name, strlen(name));
6854+
6855+ if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6856+ /* debugfs acquires the parent i_mutex */
6857+ lockdep_off();
6858+ d = debugfs_rename(parent, br->br_dbgaufs, parent,
6859+ name);
6860+ lockdep_on();
6861+ if (unlikely(!d))
6862+ pr_warn("failed renaming %pd/%s, ignored.\n",
6863+ parent, name);
6864+ }
6865+ } else {
e2f27e51 6866+ lockdep_off();
062440b3
AM
6867+ br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6868+ sbinfo, &dbgaufs_xino_fop);
e2f27e51 6869+ lockdep_on();
062440b3 6870+ if (unlikely(!br->br_dbgaufs))
acd2b654 6871+ pr_warn("failed creating %pd/%s, ignored.\n",
062440b3 6872+ parent, name);
1facf9fc 6873+ }
6874+}
6875+
acd2b654
AM
6876+static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6877+ struct dentry *parent, struct au_sbinfo *sbinfo)
6878+{
6879+ struct au_branch *br;
6880+ struct au_xino *xi;
6881+ unsigned int u;
6882+
6883+ br = au_sbr(sb, bindex);
6884+ xi = br->br_xino;
6885+ for (u = 0; u < xi->xi_nfile; u++)
6886+ dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6887+}
6888+
062440b3 6889+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
1facf9fc 6890+{
6891+ struct au_sbinfo *sbinfo;
6892+ struct dentry *parent;
5afbbe0d 6893+ aufs_bindex_t bbot;
062440b3
AM
6894+
6895+ if (!au_opt_test(au_mntflags(sb), XINO))
6896+ return;
1facf9fc 6897+
6898+ sbinfo = au_sbi(sb);
6899+ parent = sbinfo->si_dbgaufs;
6900+ if (!parent)
6901+ return;
6902+
5afbbe0d 6903+ bbot = au_sbbot(sb);
062440b3
AM
6904+ if (topdown)
6905+ for (; bindex <= bbot; bindex++)
6906+ dbgaufs_br_add(sb, bindex, parent, sbinfo);
6907+ else
6908+ for (; bbot >= bindex; bbot--)
6909+ dbgaufs_br_add(sb, bbot, parent, sbinfo);
1facf9fc 6910+}
6911+
6912+/* ---------------------------------------------------------------------- */
6913+
6914+#ifdef CONFIG_AUFS_EXPORT
6915+static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6916+{
6917+ int err;
6918+ struct au_sbinfo *sbinfo;
6919+ struct super_block *sb;
6920+
6921+ sbinfo = inode->i_private;
6922+ sb = sbinfo->si_sb;
6923+ si_noflush_read_lock(sb);
062440b3 6924+ err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
1facf9fc 6925+ si_read_unlock(sb);
6926+ return err;
6927+}
6928+
6929+static const struct file_operations dbgaufs_xigen_fop = {
4a4d8108 6930+ .owner = THIS_MODULE,
1facf9fc 6931+ .open = dbgaufs_xigen_open,
6932+ .release = dbgaufs_xi_release,
6933+ .read = dbgaufs_xi_read
6934+};
6935+
6936+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6937+{
6938+ int err;
6939+
dece6358 6940+ /*
c1595e42 6941+ * This function is a dynamic '__init' function actually,
dece6358
AM
6942+ * so the tiny check for si_rwsem is unnecessary.
6943+ */
6944+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6945+
1facf9fc 6946+ err = -EIO;
6947+ sbinfo->si_dbgaufs_xigen = debugfs_create_file
6948+ ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6949+ &dbgaufs_xigen_fop);
6950+ if (sbinfo->si_dbgaufs_xigen)
6951+ err = 0;
6952+
6953+ return err;
6954+}
6955+#else
6956+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6957+{
6958+ return 0;
6959+}
6960+#endif /* CONFIG_AUFS_EXPORT */
6961+
6962+/* ---------------------------------------------------------------------- */
6963+
6964+void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6965+{
dece6358 6966+ /*
7e9cd9fe 6967+ * This function is a dynamic '__fin' function actually,
dece6358
AM
6968+ * so the tiny check for si_rwsem is unnecessary.
6969+ */
6970+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6971+
1facf9fc 6972+ debugfs_remove_recursive(sbinfo->si_dbgaufs);
6973+ sbinfo->si_dbgaufs = NULL;
1facf9fc 6974+}
6975+
6976+int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6977+{
6978+ int err;
6979+ char name[SysaufsSiNameLen];
6980+
dece6358 6981+ /*
c1595e42 6982+ * This function is a dynamic '__init' function actually,
dece6358
AM
6983+ * so the tiny check for si_rwsem is unnecessary.
6984+ */
6985+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6986+
1facf9fc 6987+ err = -ENOENT;
6988+ if (!dbgaufs) {
6989+ AuErr1("/debug/aufs is uninitialized\n");
6990+ goto out;
6991+ }
6992+
6993+ err = -EIO;
6994+ sysaufs_name(sbinfo, name);
6995+ sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6996+ if (unlikely(!sbinfo->si_dbgaufs))
6997+ goto out;
1facf9fc 6998+
062440b3 6999+ /* regardless plink/noplink option */
86dc4139
AM
7000+ sbinfo->si_dbgaufs_plink = debugfs_create_file
7001+ ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
7002+ &dbgaufs_plink_fop);
7003+ if (unlikely(!sbinfo->si_dbgaufs_plink))
7004+ goto out_dir;
7005+
062440b3
AM
7006+ /* regardless xino/noxino option */
7007+ sbinfo->si_dbgaufs_xib = debugfs_create_file
7008+ ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
7009+ &dbgaufs_xib_fop);
7010+ if (unlikely(!sbinfo->si_dbgaufs_xib))
7011+ goto out_dir;
7012+
1facf9fc 7013+ err = dbgaufs_xigen_init(sbinfo);
7014+ if (!err)
7015+ goto out; /* success */
7016+
4f0767ce 7017+out_dir:
1facf9fc 7018+ dbgaufs_si_fin(sbinfo);
4f0767ce 7019+out:
062440b3
AM
7020+ if (unlikely(err))
7021+ pr_err("debugfs/aufs failed\n");
1facf9fc 7022+ return err;
7023+}
7024+
7025+/* ---------------------------------------------------------------------- */
7026+
7027+void dbgaufs_fin(void)
7028+{
7029+ debugfs_remove(dbgaufs);
7030+}
7031+
7032+int __init dbgaufs_init(void)
7033+{
7034+ int err;
7035+
7036+ err = -EIO;
7037+ dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
7038+ if (dbgaufs)
7039+ err = 0;
7040+ return err;
7041+}
7f207e10
AM
7042diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
7043--- /usr/share/empty/fs/aufs/dbgaufs.h 1970-01-01 01:00:00.000000000 +0100
e49925d1 7044+++ linux/fs/aufs/dbgaufs.h 2019-01-28 14:36:12.219084659 +0100
062440b3
AM
7045@@ -0,0 +1,53 @@
7046+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7047+/*
b00004a5 7048+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 7049+ *
7050+ * This program, aufs is free software; you can redistribute it and/or modify
7051+ * it under the terms of the GNU General Public License as published by
7052+ * the Free Software Foundation; either version 2 of the License, or
7053+ * (at your option) any later version.
dece6358
AM
7054+ *
7055+ * This program is distributed in the hope that it will be useful,
7056+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7057+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7058+ * GNU General Public License for more details.
7059+ *
7060+ * You should have received a copy of the GNU General Public License
523b37e3 7061+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7062+ */
7063+
7064+/*
7065+ * debugfs interface
7066+ */
7067+
7068+#ifndef __DBGAUFS_H__
7069+#define __DBGAUFS_H__
7070+
7071+#ifdef __KERNEL__
7072+
dece6358 7073+struct super_block;
1facf9fc 7074+struct au_sbinfo;
062440b3 7075+struct au_branch;
dece6358 7076+
1facf9fc 7077+#ifdef CONFIG_DEBUG_FS
7078+/* dbgaufs.c */
062440b3 7079+void dbgaufs_xino_del(struct au_branch *br);
1facf9fc 7080+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
062440b3 7081+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
1facf9fc 7082+void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
7083+int dbgaufs_si_init(struct au_sbinfo *sbinfo);
7084+void dbgaufs_fin(void);
7085+int __init dbgaufs_init(void);
1facf9fc 7086+#else
062440b3 7087+AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
4a4d8108 7088+AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
062440b3
AM
7089+AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
7090+ int topdown)
4a4d8108
AM
7091+AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
7092+AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
7093+AuStubVoid(dbgaufs_fin, void)
7094+AuStubInt0(__init dbgaufs_init, void)
1facf9fc 7095+#endif /* CONFIG_DEBUG_FS */
7096+
7097+#endif /* __KERNEL__ */
7098+#endif /* __DBGAUFS_H__ */
7f207e10
AM
7099diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
7100--- /usr/share/empty/fs/aufs/dcsub.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 7101+++ linux/fs/aufs/dcsub.c 2019-01-28 14:36:12.219084659 +0100
cd7a4cd9
AM
7102@@ -0,0 +1,225 @@
7103+// SPDX-License-Identifier: GPL-2.0
1facf9fc 7104+/*
b00004a5 7105+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 7106+ *
7107+ * This program, aufs is free software; you can redistribute it and/or modify
7108+ * it under the terms of the GNU General Public License as published by
7109+ * the Free Software Foundation; either version 2 of the License, or
7110+ * (at your option) any later version.
dece6358
AM
7111+ *
7112+ * This program is distributed in the hope that it will be useful,
7113+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7114+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7115+ * GNU General Public License for more details.
7116+ *
7117+ * You should have received a copy of the GNU General Public License
523b37e3 7118+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7119+ */
7120+
7121+/*
7122+ * sub-routines for dentry cache
7123+ */
7124+
7125+#include "aufs.h"
7126+
7127+static void au_dpage_free(struct au_dpage *dpage)
7128+{
7129+ int i;
7130+ struct dentry **p;
7131+
7132+ p = dpage->dentries;
7133+ for (i = 0; i < dpage->ndentry; i++)
7134+ dput(*p++);
1c60b727 7135+ free_page((unsigned long)dpage->dentries);
1facf9fc 7136+}
7137+
7138+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
7139+{
7140+ int err;
7141+ void *p;
7142+
7143+ err = -ENOMEM;
7144+ dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
7145+ if (unlikely(!dpages->dpages))
7146+ goto out;
7147+
7148+ p = (void *)__get_free_page(gfp);
7149+ if (unlikely(!p))
7150+ goto out_dpages;
7151+
7152+ dpages->dpages[0].ndentry = 0;
7153+ dpages->dpages[0].dentries = p;
7154+ dpages->ndpage = 1;
7155+ return 0; /* success */
7156+
4f0767ce 7157+out_dpages:
e49925d1 7158+ au_kfree_try_rcu(dpages->dpages);
4f0767ce 7159+out:
1facf9fc 7160+ return err;
7161+}
7162+
7163+void au_dpages_free(struct au_dcsub_pages *dpages)
7164+{
7165+ int i;
7166+ struct au_dpage *p;
7167+
7168+ p = dpages->dpages;
7169+ for (i = 0; i < dpages->ndpage; i++)
7170+ au_dpage_free(p++);
e49925d1 7171+ au_kfree_try_rcu(dpages->dpages);
1facf9fc 7172+}
7173+
7174+static int au_dpages_append(struct au_dcsub_pages *dpages,
7175+ struct dentry *dentry, gfp_t gfp)
7176+{
7177+ int err, sz;
7178+ struct au_dpage *dpage;
7179+ void *p;
7180+
7181+ dpage = dpages->dpages + dpages->ndpage - 1;
7182+ sz = PAGE_SIZE / sizeof(dentry);
7183+ if (unlikely(dpage->ndentry >= sz)) {
7184+ AuLabel(new dpage);
7185+ err = -ENOMEM;
7186+ sz = dpages->ndpage * sizeof(*dpages->dpages);
7187+ p = au_kzrealloc(dpages->dpages, sz,
e2f27e51
AM
7188+ sz + sizeof(*dpages->dpages), gfp,
7189+ /*may_shrink*/0);
1facf9fc 7190+ if (unlikely(!p))
7191+ goto out;
7192+
7193+ dpages->dpages = p;
7194+ dpage = dpages->dpages + dpages->ndpage;
7195+ p = (void *)__get_free_page(gfp);
7196+ if (unlikely(!p))
7197+ goto out;
7198+
7199+ dpage->ndentry = 0;
7200+ dpage->dentries = p;
7201+ dpages->ndpage++;
7202+ }
7203+
c1595e42 7204+ AuDebugOn(au_dcount(dentry) <= 0);
027c5e7a 7205+ dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
1facf9fc 7206+ return 0; /* success */
7207+
4f0767ce 7208+out:
1facf9fc 7209+ return err;
7210+}
7211+
c1595e42
JR
7212+/* todo: BAD approach */
7213+/* copied from linux/fs/dcache.c */
7214+enum d_walk_ret {
7215+ D_WALK_CONTINUE,
7216+ D_WALK_QUIT,
7217+ D_WALK_NORETRY,
7218+ D_WALK_SKIP,
7219+};
7220+
7221+extern void d_walk(struct dentry *parent, void *data,
cd7a4cd9 7222+ enum d_walk_ret (*enter)(void *, struct dentry *));
c1595e42
JR
7223+
7224+struct ac_dpages_arg {
1facf9fc 7225+ int err;
c1595e42
JR
7226+ struct au_dcsub_pages *dpages;
7227+ struct super_block *sb;
7228+ au_dpages_test test;
7229+ void *arg;
7230+};
1facf9fc 7231+
c1595e42
JR
7232+static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7233+{
7234+ enum d_walk_ret ret;
7235+ struct ac_dpages_arg *arg = _arg;
1facf9fc 7236+
c1595e42
JR
7237+ ret = D_WALK_CONTINUE;
7238+ if (dentry->d_sb == arg->sb
7239+ && !IS_ROOT(dentry)
7240+ && au_dcount(dentry) > 0
7241+ && au_di(dentry)
7242+ && (!arg->test || arg->test(dentry, arg->arg))) {
7243+ arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7244+ if (unlikely(arg->err))
7245+ ret = D_WALK_QUIT;
1facf9fc 7246+ }
7247+
c1595e42
JR
7248+ return ret;
7249+}
027c5e7a 7250+
c1595e42
JR
7251+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7252+ au_dpages_test test, void *arg)
7253+{
7254+ struct ac_dpages_arg args = {
7255+ .err = 0,
7256+ .dpages = dpages,
7257+ .sb = root->d_sb,
7258+ .test = test,
7259+ .arg = arg
7260+ };
027c5e7a 7261+
cd7a4cd9 7262+ d_walk(root, &args, au_call_dpages_append);
c1595e42
JR
7263+
7264+ return args.err;
1facf9fc 7265+}
7266+
7267+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7268+ int do_include, au_dpages_test test, void *arg)
7269+{
7270+ int err;
7271+
7272+ err = 0;
027c5e7a
AM
7273+ write_seqlock(&rename_lock);
7274+ spin_lock(&dentry->d_lock);
7275+ if (do_include
c1595e42 7276+ && au_dcount(dentry) > 0
027c5e7a 7277+ && (!test || test(dentry, arg)))
1facf9fc 7278+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
7279+ spin_unlock(&dentry->d_lock);
7280+ if (unlikely(err))
7281+ goto out;
7282+
7283+ /*
523b37e3 7284+ * RCU for vfsmount is unnecessary since this is a traverse in a single
027c5e7a
AM
7285+ * mount
7286+ */
1facf9fc 7287+ while (!IS_ROOT(dentry)) {
027c5e7a
AM
7288+ dentry = dentry->d_parent; /* rename_lock is locked */
7289+ spin_lock(&dentry->d_lock);
c1595e42 7290+ if (au_dcount(dentry) > 0
027c5e7a 7291+ && (!test || test(dentry, arg)))
1facf9fc 7292+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
7293+ spin_unlock(&dentry->d_lock);
7294+ if (unlikely(err))
7295+ break;
1facf9fc 7296+ }
7297+
4f0767ce 7298+out:
027c5e7a 7299+ write_sequnlock(&rename_lock);
1facf9fc 7300+ return err;
7301+}
7302+
027c5e7a
AM
7303+static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7304+{
7305+ return au_di(dentry) && dentry->d_sb == arg;
7306+}
7307+
7308+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7309+ struct dentry *dentry, int do_include)
7310+{
7311+ return au_dcsub_pages_rev(dpages, dentry, do_include,
7312+ au_dcsub_dpages_aufs, dentry->d_sb);
7313+}
7314+
4a4d8108 7315+int au_test_subdir(struct dentry *d1, struct dentry *d2)
1facf9fc 7316+{
4a4d8108
AM
7317+ struct path path[2] = {
7318+ {
7319+ .dentry = d1
7320+ },
7321+ {
7322+ .dentry = d2
7323+ }
7324+ };
1facf9fc 7325+
4a4d8108 7326+ return path_is_under(path + 0, path + 1);
1facf9fc 7327+}
7f207e10
AM
7328diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7329--- /usr/share/empty/fs/aufs/dcsub.h 1970-01-01 01:00:00.000000000 +0100
e49925d1 7330+++ linux/fs/aufs/dcsub.h 2019-01-28 14:36:12.219084659 +0100
062440b3
AM
7331@@ -0,0 +1,137 @@
7332+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7333+/*
b00004a5 7334+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 7335+ *
7336+ * This program, aufs is free software; you can redistribute it and/or modify
7337+ * it under the terms of the GNU General Public License as published by
7338+ * the Free Software Foundation; either version 2 of the License, or
7339+ * (at your option) any later version.
dece6358
AM
7340+ *
7341+ * This program is distributed in the hope that it will be useful,
7342+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7343+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7344+ * GNU General Public License for more details.
7345+ *
7346+ * You should have received a copy of the GNU General Public License
523b37e3 7347+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7348+ */
7349+
7350+/*
7351+ * sub-routines for dentry cache
7352+ */
7353+
7354+#ifndef __AUFS_DCSUB_H__
7355+#define __AUFS_DCSUB_H__
7356+
7357+#ifdef __KERNEL__
7358+
7f207e10 7359+#include <linux/dcache.h>
027c5e7a 7360+#include <linux/fs.h>
dece6358 7361+
1facf9fc 7362+struct au_dpage {
7363+ int ndentry;
7364+ struct dentry **dentries;
7365+};
7366+
7367+struct au_dcsub_pages {
7368+ int ndpage;
7369+ struct au_dpage *dpages;
7370+};
7371+
7372+/* ---------------------------------------------------------------------- */
7373+
7f207e10 7374+/* dcsub.c */
1facf9fc 7375+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7376+void au_dpages_free(struct au_dcsub_pages *dpages);
7377+typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7378+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7379+ au_dpages_test test, void *arg);
7380+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7381+ int do_include, au_dpages_test test, void *arg);
027c5e7a
AM
7382+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7383+ struct dentry *dentry, int do_include);
4a4d8108 7384+int au_test_subdir(struct dentry *d1, struct dentry *d2);
1facf9fc 7385+
7f207e10
AM
7386+/* ---------------------------------------------------------------------- */
7387+
523b37e3
AM
7388+/*
7389+ * todo: in linux-3.13, several similar (but faster) helpers are added to
7390+ * include/linux/dcache.h. Try them (in the future).
7391+ */
7392+
027c5e7a
AM
7393+static inline int au_d_hashed_positive(struct dentry *d)
7394+{
7395+ int err;
5527c038 7396+ struct inode *inode = d_inode(d);
076b876e 7397+
027c5e7a 7398+ err = 0;
5527c038
JR
7399+ if (unlikely(d_unhashed(d)
7400+ || d_is_negative(d)
7401+ || !inode->i_nlink))
027c5e7a
AM
7402+ err = -ENOENT;
7403+ return err;
7404+}
7405+
38d290e6
JR
7406+static inline int au_d_linkable(struct dentry *d)
7407+{
7408+ int err;
5527c038 7409+ struct inode *inode = d_inode(d);
076b876e 7410+
38d290e6
JR
7411+ err = au_d_hashed_positive(d);
7412+ if (err
5527c038 7413+ && d_is_positive(d)
38d290e6
JR
7414+ && (inode->i_state & I_LINKABLE))
7415+ err = 0;
7416+ return err;
7417+}
7418+
027c5e7a
AM
7419+static inline int au_d_alive(struct dentry *d)
7420+{
7421+ int err;
7422+ struct inode *inode;
076b876e 7423+
027c5e7a
AM
7424+ err = 0;
7425+ if (!IS_ROOT(d))
7426+ err = au_d_hashed_positive(d);
7427+ else {
5527c038
JR
7428+ inode = d_inode(d);
7429+ if (unlikely(d_unlinked(d)
7430+ || d_is_negative(d)
7431+ || !inode->i_nlink))
027c5e7a
AM
7432+ err = -ENOENT;
7433+ }
7434+ return err;
7435+}
7436+
7437+static inline int au_alive_dir(struct dentry *d)
7f207e10 7438+{
027c5e7a 7439+ int err;
076b876e 7440+
027c5e7a 7441+ err = au_d_alive(d);
5527c038 7442+ if (unlikely(err || IS_DEADDIR(d_inode(d))))
027c5e7a
AM
7443+ err = -ENOENT;
7444+ return err;
7f207e10
AM
7445+}
7446+
38d290e6
JR
7447+static inline int au_qstreq(struct qstr *a, struct qstr *b)
7448+{
7449+ return a->len == b->len
7450+ && !memcmp(a->name, b->name, a->len);
7451+}
7452+
7e9cd9fe
AM
7453+/*
7454+ * by the commit
7455+ * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7456+ * taking d_lock
7457+ * the type of d_lockref.count became int, but the inlined function d_count()
7458+ * still returns unsigned int.
7459+ * I don't know why. Maybe it is for every d_count() users?
7460+ * Anyway au_dcount() lives on.
7461+ */
c1595e42
JR
7462+static inline int au_dcount(struct dentry *d)
7463+{
7464+ return (int)d_count(d);
7465+}
7466+
1facf9fc 7467+#endif /* __KERNEL__ */
7468+#endif /* __AUFS_DCSUB_H__ */
7f207e10
AM
7469diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7470--- /usr/share/empty/fs/aufs/debug.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 7471+++ linux/fs/aufs/debug.c 2019-01-28 14:36:12.219084659 +0100
acd2b654 7472@@ -0,0 +1,440 @@
cd7a4cd9 7473+// SPDX-License-Identifier: GPL-2.0
1facf9fc 7474+/*
b00004a5 7475+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 7476+ *
7477+ * This program, aufs is free software; you can redistribute it and/or modify
7478+ * it under the terms of the GNU General Public License as published by
7479+ * the Free Software Foundation; either version 2 of the License, or
7480+ * (at your option) any later version.
dece6358
AM
7481+ *
7482+ * This program is distributed in the hope that it will be useful,
7483+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7484+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7485+ * GNU General Public License for more details.
7486+ *
7487+ * You should have received a copy of the GNU General Public License
523b37e3 7488+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7489+ */
7490+
7491+/*
7492+ * debug print functions
7493+ */
7494+
7495+#include "aufs.h"
7496+
392086de
AM
7497+/* Returns 0, or -errno. arg is in kp->arg. */
7498+static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7499+{
7500+ int err, n;
7501+
7502+ err = kstrtoint(val, 0, &n);
7503+ if (!err) {
7504+ if (n > 0)
7505+ au_debug_on();
7506+ else
7507+ au_debug_off();
7508+ }
7509+ return err;
7510+}
7511+
7512+/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
7513+static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7514+{
7515+ atomic_t *a;
7516+
7517+ a = kp->arg;
7518+ return sprintf(buffer, "%d", atomic_read(a));
7519+}
7520+
7521+static struct kernel_param_ops param_ops_atomic_t = {
7522+ .set = param_atomic_t_set,
7523+ .get = param_atomic_t_get
7524+ /* void (*free)(void *arg) */
7525+};
7526+
7527+atomic_t aufs_debug = ATOMIC_INIT(0);
1facf9fc 7528+MODULE_PARM_DESC(debug, "debug print");
cd7a4cd9 7529+module_param_named(debug, aufs_debug, atomic_t, 0664);
1facf9fc 7530+
c1595e42 7531+DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
1facf9fc 7532+char *au_plevel = KERN_DEBUG;
e49829fe
JR
7533+#define dpri(fmt, ...) do { \
7534+ if ((au_plevel \
7535+ && strcmp(au_plevel, KERN_DEBUG)) \
7536+ || au_debug_test()) \
7537+ printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
1facf9fc 7538+} while (0)
7539+
7540+/* ---------------------------------------------------------------------- */
7541+
7542+void au_dpri_whlist(struct au_nhash *whlist)
7543+{
7544+ unsigned long ul, n;
7545+ struct hlist_head *head;
c06a8ce3 7546+ struct au_vdir_wh *pos;
1facf9fc 7547+
7548+ n = whlist->nh_num;
7549+ head = whlist->nh_head;
7550+ for (ul = 0; ul < n; ul++) {
c06a8ce3 7551+ hlist_for_each_entry(pos, head, wh_hash)
1facf9fc 7552+ dpri("b%d, %.*s, %d\n",
c06a8ce3
AM
7553+ pos->wh_bindex,
7554+ pos->wh_str.len, pos->wh_str.name,
7555+ pos->wh_str.len);
1facf9fc 7556+ head++;
7557+ }
7558+}
7559+
7560+void au_dpri_vdir(struct au_vdir *vdir)
7561+{
7562+ unsigned long ul;
7563+ union au_vdir_deblk_p p;
7564+ unsigned char *o;
7565+
7566+ if (!vdir || IS_ERR(vdir)) {
7567+ dpri("err %ld\n", PTR_ERR(vdir));
7568+ return;
7569+ }
7570+
be118d29 7571+ dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
1facf9fc 7572+ vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7573+ vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7574+ for (ul = 0; ul < vdir->vd_nblk; ul++) {
7575+ p.deblk = vdir->vd_deblk[ul];
7576+ o = p.deblk;
7577+ dpri("[%lu]: %p\n", ul, o);
7578+ }
7579+}
7580+
53392da6 7581+static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
1facf9fc 7582+ struct dentry *wh)
7583+{
7584+ char *n = NULL;
7585+ int l = 0;
7586+
7587+ if (!inode || IS_ERR(inode)) {
7588+ dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7589+ return -1;
7590+ }
7591+
c2b27bf2 7592+ /* the type of i_blocks depends upon CONFIG_LBDAF */
1facf9fc 7593+ BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7594+ && sizeof(inode->i_blocks) != sizeof(u64));
7595+ if (wh) {
7596+ n = (void *)wh->d_name.name;
7597+ l = wh->d_name.len;
7598+ }
7599+
53392da6
AM
7600+ dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7601+ " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7602+ bindex, inode,
1facf9fc 7603+ inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7604+ atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7605+ i_size_read(inode), (unsigned long long)inode->i_blocks,
cd7a4cd9 7606+ hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
1facf9fc 7607+ inode->i_mapping ? inode->i_mapping->nrpages : 0,
be118d29 7608+ inode->i_state, inode->i_flags, inode_peek_iversion(inode),
b752ccd1 7609+ inode->i_generation,
1facf9fc 7610+ l ? ", wh " : "", l, n);
7611+ return 0;
7612+}
7613+
7614+void au_dpri_inode(struct inode *inode)
7615+{
7616+ struct au_iinfo *iinfo;
5afbbe0d 7617+ struct au_hinode *hi;
1facf9fc 7618+ aufs_bindex_t bindex;
53392da6 7619+ int err, hn;
1facf9fc 7620+
53392da6 7621+ err = do_pri_inode(-1, inode, -1, NULL);
5afbbe0d 7622+ if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
1facf9fc 7623+ return;
7624+
7625+ iinfo = au_ii(inode);
5afbbe0d
AM
7626+ dpri("i-1: btop %d, bbot %d, gen %d\n",
7627+ iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7628+ if (iinfo->ii_btop < 0)
1facf9fc 7629+ return;
53392da6 7630+ hn = 0;
5afbbe0d
AM
7631+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7632+ hi = au_hinode(iinfo, bindex);
7633+ hn = !!au_hn(hi);
7634+ do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
53392da6 7635+ }
1facf9fc 7636+}
7637+
2cbb1c4b
JR
7638+void au_dpri_dalias(struct inode *inode)
7639+{
7640+ struct dentry *d;
7641+
7642+ spin_lock(&inode->i_lock);
c1595e42 7643+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
2cbb1c4b
JR
7644+ au_dpri_dentry(d);
7645+ spin_unlock(&inode->i_lock);
7646+}
7647+
1facf9fc 7648+static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7649+{
7650+ struct dentry *wh = NULL;
53392da6 7651+ int hn;
5afbbe0d 7652+ struct inode *inode;
076b876e 7653+ struct au_iinfo *iinfo;
5afbbe0d 7654+ struct au_hinode *hi;
1facf9fc 7655+
7656+ if (!dentry || IS_ERR(dentry)) {
7657+ dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7658+ return -1;
7659+ }
7660+ /* do not call dget_parent() here */
027c5e7a 7661+ /* note: access d_xxx without d_lock */
523b37e3
AM
7662+ dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7663+ bindex, dentry, dentry,
1facf9fc 7664+ dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
c1595e42 7665+ au_dcount(dentry), dentry->d_flags,
523b37e3 7666+ d_unhashed(dentry) ? "un" : "");
53392da6 7667+ hn = -1;
5afbbe0d
AM
7668+ inode = NULL;
7669+ if (d_is_positive(dentry))
7670+ inode = d_inode(dentry);
7671+ if (inode
7672+ && au_test_aufs(dentry->d_sb)
7673+ && bindex >= 0
7674+ && !au_is_bad_inode(inode)) {
7675+ iinfo = au_ii(inode);
7676+ hi = au_hinode(iinfo, bindex);
7677+ hn = !!au_hn(hi);
7678+ wh = hi->hi_whdentry;
7679+ }
7680+ do_pri_inode(bindex, inode, hn, wh);
1facf9fc 7681+ return 0;
7682+}
7683+
7684+void au_dpri_dentry(struct dentry *dentry)
7685+{
7686+ struct au_dinfo *dinfo;
7687+ aufs_bindex_t bindex;
7688+ int err;
7689+
7690+ err = do_pri_dentry(-1, dentry);
7691+ if (err || !au_test_aufs(dentry->d_sb))
7692+ return;
7693+
7694+ dinfo = au_di(dentry);
7695+ if (!dinfo)
7696+ return;
5afbbe0d
AM
7697+ dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7698+ dinfo->di_btop, dinfo->di_bbot,
38d290e6
JR
7699+ dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7700+ dinfo->di_tmpfile);
5afbbe0d 7701+ if (dinfo->di_btop < 0)
1facf9fc 7702+ return;
5afbbe0d
AM
7703+ for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7704+ do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
1facf9fc 7705+}
7706+
7707+static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7708+{
7709+ char a[32];
7710+
7711+ if (!file || IS_ERR(file)) {
7712+ dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7713+ return -1;
7714+ }
7715+ a[0] = 0;
7716+ if (bindex < 0
b912730e 7717+ && !IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7718+ && au_test_aufs(file->f_path.dentry->d_sb)
1facf9fc 7719+ && au_fi(file))
e49829fe 7720+ snprintf(a, sizeof(a), ", gen %d, mmapped %d",
2cbb1c4b 7721+ au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
b752ccd1 7722+ dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
1facf9fc 7723+ bindex, file->f_mode, file->f_flags, (long)file_count(file),
b752ccd1 7724+ file->f_version, file->f_pos, a);
b912730e 7725+ if (!IS_ERR_OR_NULL(file->f_path.dentry))
2000de60 7726+ do_pri_dentry(bindex, file->f_path.dentry);
1facf9fc 7727+ return 0;
7728+}
7729+
7730+void au_dpri_file(struct file *file)
7731+{
7732+ struct au_finfo *finfo;
4a4d8108
AM
7733+ struct au_fidir *fidir;
7734+ struct au_hfile *hfile;
1facf9fc 7735+ aufs_bindex_t bindex;
7736+ int err;
7737+
7738+ err = do_pri_file(-1, file);
2000de60 7739+ if (err
b912730e 7740+ || IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7741+ || !au_test_aufs(file->f_path.dentry->d_sb))
1facf9fc 7742+ return;
7743+
7744+ finfo = au_fi(file);
7745+ if (!finfo)
7746+ return;
4a4d8108 7747+ if (finfo->fi_btop < 0)
1facf9fc 7748+ return;
4a4d8108
AM
7749+ fidir = finfo->fi_hdir;
7750+ if (!fidir)
7751+ do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7752+ else
e49829fe
JR
7753+ for (bindex = finfo->fi_btop;
7754+ bindex >= 0 && bindex <= fidir->fd_bbot;
4a4d8108
AM
7755+ bindex++) {
7756+ hfile = fidir->fd_hfile + bindex;
7757+ do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7758+ }
1facf9fc 7759+}
7760+
7761+static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7762+{
7763+ struct vfsmount *mnt;
7764+ struct super_block *sb;
7765+
7766+ if (!br || IS_ERR(br))
7767+ goto out;
86dc4139 7768+ mnt = au_br_mnt(br);
1facf9fc 7769+ if (!mnt || IS_ERR(mnt))
7770+ goto out;
7771+ sb = mnt->mnt_sb;
7772+ if (!sb || IS_ERR(sb))
7773+ goto out;
7774+
acd2b654 7775+ dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
b752ccd1 7776+ "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
1facf9fc 7777+ "xino %d\n",
acd2b654
AM
7778+ bindex, br->br_perm, br->br_id, br->br_wbr,
7779+ au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
b752ccd1 7780+ sb->s_flags, sb->s_count,
acd2b654
AM
7781+ atomic_read(&sb->s_active),
7782+ !!au_xino_file(br->br_xino, /*idx*/-1));
1facf9fc 7783+ return 0;
7784+
4f0767ce 7785+out:
1facf9fc 7786+ dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7787+ return -1;
7788+}
7789+
7790+void au_dpri_sb(struct super_block *sb)
7791+{
7792+ struct au_sbinfo *sbinfo;
7793+ aufs_bindex_t bindex;
7794+ int err;
acd2b654 7795+ /* to reduce stack size */
1facf9fc 7796+ struct {
7797+ struct vfsmount mnt;
7798+ struct au_branch fake;
7799+ } *a;
7800+
7801+ /* this function can be called from magic sysrq */
7802+ a = kzalloc(sizeof(*a), GFP_ATOMIC);
7803+ if (unlikely(!a)) {
7804+ dpri("no memory\n");
7805+ return;
7806+ }
7807+
7808+ a->mnt.mnt_sb = sb;
86dc4139 7809+ a->fake.br_path.mnt = &a->mnt;
1facf9fc 7810+ err = do_pri_br(-1, &a->fake);
e49925d1 7811+ au_kfree_rcu(a);
1facf9fc 7812+ dpri("dev 0x%x\n", sb->s_dev);
7813+ if (err || !au_test_aufs(sb))
7814+ return;
7815+
7816+ sbinfo = au_sbi(sb);
7817+ if (!sbinfo)
7818+ return;
f0c0a007
AM
7819+ dpri("nw %d, gen %u, kobj %d\n",
7820+ atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
521ced18 7821+ kref_read(&sbinfo->si_kobj.kref));
5afbbe0d 7822+ for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
1facf9fc 7823+ do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7824+}
7825+
7826+/* ---------------------------------------------------------------------- */
7827+
027c5e7a
AM
7828+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7829+{
5527c038 7830+ struct inode *h_inode, *inode = d_inode(dentry);
027c5e7a 7831+ struct dentry *h_dentry;
5afbbe0d 7832+ aufs_bindex_t bindex, bbot, bi;
027c5e7a
AM
7833+
7834+ if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7835+ return;
7836+
5afbbe0d
AM
7837+ bbot = au_dbbot(dentry);
7838+ bi = au_ibbot(inode);
7839+ if (bi < bbot)
7840+ bbot = bi;
7841+ bindex = au_dbtop(dentry);
7842+ bi = au_ibtop(inode);
027c5e7a
AM
7843+ if (bi > bindex)
7844+ bindex = bi;
7845+
5afbbe0d 7846+ for (; bindex <= bbot; bindex++) {
027c5e7a
AM
7847+ h_dentry = au_h_dptr(dentry, bindex);
7848+ if (!h_dentry)
7849+ continue;
7850+ h_inode = au_h_iptr(inode, bindex);
5527c038 7851+ if (unlikely(h_inode != d_inode(h_dentry))) {
392086de 7852+ au_debug_on();
027c5e7a
AM
7853+ AuDbg("b%d, %s:%d\n", bindex, func, line);
7854+ AuDbgDentry(dentry);
7855+ AuDbgInode(inode);
392086de 7856+ au_debug_off();
027c5e7a
AM
7857+ BUG();
7858+ }
7859+ }
7860+}
7861+
1facf9fc 7862+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7863+{
7864+ int err, i, j;
7865+ struct au_dcsub_pages dpages;
7866+ struct au_dpage *dpage;
7867+ struct dentry **dentries;
7868+
7869+ err = au_dpages_init(&dpages, GFP_NOFS);
7870+ AuDebugOn(err);
027c5e7a 7871+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
1facf9fc 7872+ AuDebugOn(err);
7873+ for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7874+ dpage = dpages.dpages + i;
7875+ dentries = dpage->dentries;
7876+ for (j = dpage->ndentry - 1; !err && j >= 0; j--)
027c5e7a 7877+ AuDebugOn(au_digen_test(dentries[j], sigen));
1facf9fc 7878+ }
7879+ au_dpages_free(&dpages);
7880+}
7881+
1facf9fc 7882+void au_dbg_verify_kthread(void)
7883+{
53392da6 7884+ if (au_wkq_test()) {
1facf9fc 7885+ au_dbg_blocked();
1e00d052
AM
7886+ /*
7887+ * It may be recursive, but udba=notify between two aufs mounts,
7888+ * where a single ro branch is shared, is not a problem.
7889+ */
7890+ /* WARN_ON(1); */
1facf9fc 7891+ }
7892+}
7893+
7894+/* ---------------------------------------------------------------------- */
7895+
1facf9fc 7896+int __init au_debug_init(void)
7897+{
7898+ aufs_bindex_t bindex;
7899+ struct au_vdir_destr destr;
7900+
7901+ bindex = -1;
7902+ AuDebugOn(bindex >= 0);
7903+
7904+ destr.len = -1;
7905+ AuDebugOn(destr.len < NAME_MAX);
7906+
7907+#ifdef CONFIG_4KSTACKS
0c3ec466 7908+ pr_warn("CONFIG_4KSTACKS is defined.\n");
1facf9fc 7909+#endif
7910+
1facf9fc 7911+ return 0;
7912+}
7f207e10
AM
7913diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7914--- /usr/share/empty/fs/aufs/debug.h 1970-01-01 01:00:00.000000000 +0100
e49925d1 7915+++ linux/fs/aufs/debug.h 2019-01-28 14:36:12.219084659 +0100
062440b3
AM
7916@@ -0,0 +1,226 @@
7917+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7918+/*
b00004a5 7919+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 7920+ *
7921+ * This program, aufs is free software; you can redistribute it and/or modify
7922+ * it under the terms of the GNU General Public License as published by
7923+ * the Free Software Foundation; either version 2 of the License, or
7924+ * (at your option) any later version.
dece6358
AM
7925+ *
7926+ * This program is distributed in the hope that it will be useful,
7927+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7928+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7929+ * GNU General Public License for more details.
7930+ *
7931+ * You should have received a copy of the GNU General Public License
523b37e3 7932+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7933+ */
7934+
7935+/*
7936+ * debug print functions
7937+ */
7938+
7939+#ifndef __AUFS_DEBUG_H__
7940+#define __AUFS_DEBUG_H__
7941+
7942+#ifdef __KERNEL__
7943+
392086de 7944+#include <linux/atomic.h>
4a4d8108
AM
7945+#include <linux/module.h>
7946+#include <linux/kallsyms.h>
1facf9fc 7947+#include <linux/sysrq.h>
4a4d8108 7948+
1facf9fc 7949+#ifdef CONFIG_AUFS_DEBUG
7950+#define AuDebugOn(a) BUG_ON(a)
7951+
7952+/* module parameter */
392086de
AM
7953+extern atomic_t aufs_debug;
7954+static inline void au_debug_on(void)
1facf9fc 7955+{
392086de
AM
7956+ atomic_inc(&aufs_debug);
7957+}
7958+static inline void au_debug_off(void)
7959+{
7960+ atomic_dec_if_positive(&aufs_debug);
1facf9fc 7961+}
7962+
7963+static inline int au_debug_test(void)
7964+{
392086de 7965+ return atomic_read(&aufs_debug) > 0;
1facf9fc 7966+}
7967+#else
7968+#define AuDebugOn(a) do {} while (0)
392086de
AM
7969+AuStubVoid(au_debug_on, void)
7970+AuStubVoid(au_debug_off, void)
4a4d8108 7971+AuStubInt0(au_debug_test, void)
1facf9fc 7972+#endif /* CONFIG_AUFS_DEBUG */
7973+
392086de
AM
7974+#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7975+
1facf9fc 7976+/* ---------------------------------------------------------------------- */
7977+
7978+/* debug print */
7979+
4a4d8108 7980+#define AuDbg(fmt, ...) do { \
1facf9fc 7981+ if (au_debug_test()) \
4a4d8108 7982+ pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
1facf9fc 7983+} while (0)
4a4d8108
AM
7984+#define AuLabel(l) AuDbg(#l "\n")
7985+#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7986+#define AuWarn1(fmt, ...) do { \
1facf9fc 7987+ static unsigned char _c; \
7988+ if (!_c++) \
0c3ec466 7989+ pr_warn(fmt, ##__VA_ARGS__); \
1facf9fc 7990+} while (0)
7991+
4a4d8108 7992+#define AuErr1(fmt, ...) do { \
1facf9fc 7993+ static unsigned char _c; \
7994+ if (!_c++) \
4a4d8108 7995+ pr_err(fmt, ##__VA_ARGS__); \
1facf9fc 7996+} while (0)
7997+
4a4d8108 7998+#define AuIOErr1(fmt, ...) do { \
1facf9fc 7999+ static unsigned char _c; \
8000+ if (!_c++) \
4a4d8108 8001+ AuIOErr(fmt, ##__VA_ARGS__); \
1facf9fc 8002+} while (0)
8003+
8004+#define AuUnsupportMsg "This operation is not supported." \
8005+ " Please report this application to aufs-users ML."
4a4d8108
AM
8006+#define AuUnsupport(fmt, ...) do { \
8007+ pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
1facf9fc 8008+ dump_stack(); \
8009+} while (0)
8010+
8011+#define AuTraceErr(e) do { \
8012+ if (unlikely((e) < 0)) \
8013+ AuDbg("err %d\n", (int)(e)); \
8014+} while (0)
8015+
8016+#define AuTraceErrPtr(p) do { \
8017+ if (IS_ERR(p)) \
8018+ AuDbg("err %ld\n", PTR_ERR(p)); \
8019+} while (0)
8020+
8021+/* dirty macros for debug print, use with "%.*s" and caution */
8022+#define AuLNPair(qstr) (qstr)->len, (qstr)->name
1facf9fc 8023+
8024+/* ---------------------------------------------------------------------- */
8025+
dece6358 8026+struct dentry;
1facf9fc 8027+#ifdef CONFIG_AUFS_DEBUG
c1595e42 8028+extern struct mutex au_dbg_mtx;
1facf9fc 8029+extern char *au_plevel;
8030+struct au_nhash;
8031+void au_dpri_whlist(struct au_nhash *whlist);
8032+struct au_vdir;
8033+void au_dpri_vdir(struct au_vdir *vdir);
dece6358 8034+struct inode;
1facf9fc 8035+void au_dpri_inode(struct inode *inode);
2cbb1c4b 8036+void au_dpri_dalias(struct inode *inode);
1facf9fc 8037+void au_dpri_dentry(struct dentry *dentry);
dece6358 8038+struct file;
1facf9fc 8039+void au_dpri_file(struct file *filp);
dece6358 8040+struct super_block;
1facf9fc 8041+void au_dpri_sb(struct super_block *sb);
8042+
027c5e7a
AM
8043+#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
8044+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
1facf9fc 8045+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
1facf9fc 8046+void au_dbg_verify_kthread(void);
8047+
8048+int __init au_debug_init(void);
7e9cd9fe 8049+
1facf9fc 8050+#define AuDbgWhlist(w) do { \
c1595e42 8051+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8052+ AuDbg(#w "\n"); \
8053+ au_dpri_whlist(w); \
c1595e42 8054+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8055+} while (0)
8056+
8057+#define AuDbgVdir(v) do { \
c1595e42 8058+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8059+ AuDbg(#v "\n"); \
8060+ au_dpri_vdir(v); \
c1595e42 8061+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8062+} while (0)
8063+
8064+#define AuDbgInode(i) do { \
c1595e42 8065+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8066+ AuDbg(#i "\n"); \
8067+ au_dpri_inode(i); \
c1595e42 8068+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8069+} while (0)
8070+
2cbb1c4b 8071+#define AuDbgDAlias(i) do { \
c1595e42 8072+ mutex_lock(&au_dbg_mtx); \
2cbb1c4b
JR
8073+ AuDbg(#i "\n"); \
8074+ au_dpri_dalias(i); \
c1595e42 8075+ mutex_unlock(&au_dbg_mtx); \
2cbb1c4b
JR
8076+} while (0)
8077+
1facf9fc 8078+#define AuDbgDentry(d) do { \
c1595e42 8079+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8080+ AuDbg(#d "\n"); \
8081+ au_dpri_dentry(d); \
c1595e42 8082+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8083+} while (0)
8084+
8085+#define AuDbgFile(f) do { \
c1595e42 8086+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8087+ AuDbg(#f "\n"); \
8088+ au_dpri_file(f); \
c1595e42 8089+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8090+} while (0)
8091+
8092+#define AuDbgSb(sb) do { \
c1595e42 8093+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8094+ AuDbg(#sb "\n"); \
8095+ au_dpri_sb(sb); \
c1595e42 8096+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8097+} while (0)
8098+
4a4d8108
AM
8099+#define AuDbgSym(addr) do { \
8100+ char sym[KSYM_SYMBOL_LEN]; \
8101+ sprint_symbol(sym, (unsigned long)addr); \
8102+ AuDbg("%s\n", sym); \
8103+} while (0)
1facf9fc 8104+#else
027c5e7a 8105+AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
4a4d8108
AM
8106+AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
8107+AuStubVoid(au_dbg_verify_kthread, void)
8108+AuStubInt0(__init au_debug_init, void)
1facf9fc 8109+
1facf9fc 8110+#define AuDbgWhlist(w) do {} while (0)
8111+#define AuDbgVdir(v) do {} while (0)
8112+#define AuDbgInode(i) do {} while (0)
2cbb1c4b 8113+#define AuDbgDAlias(i) do {} while (0)
1facf9fc 8114+#define AuDbgDentry(d) do {} while (0)
8115+#define AuDbgFile(f) do {} while (0)
8116+#define AuDbgSb(sb) do {} while (0)
4a4d8108 8117+#define AuDbgSym(addr) do {} while (0)
1facf9fc 8118+#endif /* CONFIG_AUFS_DEBUG */
8119+
8120+/* ---------------------------------------------------------------------- */
8121+
8122+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
8123+int __init au_sysrq_init(void);
8124+void au_sysrq_fin(void);
8125+
8126+#ifdef CONFIG_HW_CONSOLE
8127+#define au_dbg_blocked() do { \
8128+ WARN_ON(1); \
0c5527e5 8129+ handle_sysrq('w'); \
1facf9fc 8130+} while (0)
8131+#else
4a4d8108 8132+AuStubVoid(au_dbg_blocked, void)
1facf9fc 8133+#endif
8134+
8135+#else
4a4d8108
AM
8136+AuStubInt0(__init au_sysrq_init, void)
8137+AuStubVoid(au_sysrq_fin, void)
8138+AuStubVoid(au_dbg_blocked, void)
1facf9fc 8139+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
8140+
8141+#endif /* __KERNEL__ */
8142+#endif /* __AUFS_DEBUG_H__ */
7f207e10
AM
8143diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
8144--- /usr/share/empty/fs/aufs/dentry.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 8145+++ linux/fs/aufs/dentry.c 2019-01-28 14:36:12.219084659 +0100
062440b3 8146@@ -0,0 +1,1153 @@
cd7a4cd9 8147+// SPDX-License-Identifier: GPL-2.0
1facf9fc 8148+/*
b00004a5 8149+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 8150+ *
8151+ * This program, aufs is free software; you can redistribute it and/or modify
8152+ * it under the terms of the GNU General Public License as published by
8153+ * the Free Software Foundation; either version 2 of the License, or
8154+ * (at your option) any later version.
dece6358
AM
8155+ *
8156+ * This program is distributed in the hope that it will be useful,
8157+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
8158+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8159+ * GNU General Public License for more details.
8160+ *
8161+ * You should have received a copy of the GNU General Public License
523b37e3 8162+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 8163+ */
8164+
8165+/*
8166+ * lookup and dentry operations
8167+ */
8168+
dece6358 8169+#include <linux/namei.h>
1facf9fc 8170+#include "aufs.h"
8171+
1facf9fc 8172+/*
8173+ * returns positive/negative dentry, NULL or an error.
8174+ * NULL means whiteout-ed or not-found.
8175+ */
8176+static struct dentry*
8177+au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
8b6a4947 8178+ aufs_bindex_t bindex, struct au_do_lookup_args *args)
1facf9fc 8179+{
8180+ struct dentry *h_dentry;
2000de60 8181+ struct inode *h_inode;
1facf9fc 8182+ struct au_branch *br;
8183+ int wh_found, opq;
8184+ unsigned char wh_able;
8185+ const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
076b876e
AM
8186+ const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8187+ IGNORE_PERM);
1facf9fc 8188+
1facf9fc 8189+ wh_found = 0;
8190+ br = au_sbr(dentry->d_sb, bindex);
8191+ wh_able = !!au_br_whable(br->br_perm);
8192+ if (wh_able)
8b6a4947 8193+ wh_found = au_wh_test(h_parent, &args->whname, ignore_perm);
1facf9fc 8194+ h_dentry = ERR_PTR(wh_found);
8195+ if (!wh_found)
8196+ goto real_lookup;
8197+ if (unlikely(wh_found < 0))
8198+ goto out;
8199+
8200+ /* We found a whiteout */
5afbbe0d 8201+ /* au_set_dbbot(dentry, bindex); */
1facf9fc 8202+ au_set_dbwh(dentry, bindex);
8203+ if (!allow_neg)
8204+ return NULL; /* success */
8205+
4f0767ce 8206+real_lookup:
076b876e 8207+ if (!ignore_perm)
8b6a4947 8208+ h_dentry = vfsub_lkup_one(args->name, h_parent);
076b876e 8209+ else
8b6a4947 8210+ h_dentry = au_sio_lkup_one(args->name, h_parent);
2000de60
JR
8211+ if (IS_ERR(h_dentry)) {
8212+ if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8213+ && !allow_neg)
8214+ h_dentry = NULL;
1facf9fc 8215+ goto out;
2000de60 8216+ }
1facf9fc 8217+
5527c038
JR
8218+ h_inode = d_inode(h_dentry);
8219+ if (d_is_negative(h_dentry)) {
1facf9fc 8220+ if (!allow_neg)
8221+ goto out_neg;
8222+ } else if (wh_found
8223+ || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8224+ goto out_neg;
8b6a4947
AM
8225+ else if (au_ftest_lkup(args->flags, DIRREN)
8226+ /* && h_inode */
8227+ && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8228+ AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8229+ (unsigned long long)h_inode->i_ino);
8230+ goto out_neg;
8231+ }
1facf9fc 8232+
5afbbe0d
AM
8233+ if (au_dbbot(dentry) <= bindex)
8234+ au_set_dbbot(dentry, bindex);
8235+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8236+ au_set_dbtop(dentry, bindex);
1facf9fc 8237+ au_set_h_dptr(dentry, bindex, h_dentry);
8238+
2000de60
JR
8239+ if (!d_is_dir(h_dentry)
8240+ || !wh_able
5527c038 8241+ || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
1facf9fc 8242+ goto out; /* success */
8243+
be118d29 8244+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
076b876e 8245+ opq = au_diropq_test(h_dentry);
3c1bdaff 8246+ inode_unlock_shared(h_inode);
1facf9fc 8247+ if (opq > 0)
8248+ au_set_dbdiropq(dentry, bindex);
8249+ else if (unlikely(opq < 0)) {
8250+ au_set_h_dptr(dentry, bindex, NULL);
8251+ h_dentry = ERR_PTR(opq);
8252+ }
8253+ goto out;
8254+
4f0767ce 8255+out_neg:
1facf9fc 8256+ dput(h_dentry);
8257+ h_dentry = NULL;
4f0767ce 8258+out:
1facf9fc 8259+ return h_dentry;
8260+}
8261+
dece6358
AM
8262+static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8263+{
8264+ if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8265+ && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8266+ return -EPERM;
8267+ return 0;
8268+}
8269+
1facf9fc 8270+/*
8271+ * returns the number of lower positive dentries,
8272+ * otherwise an error.
8273+ * can be called at unlinking with @type is zero.
8274+ */
5afbbe0d
AM
8275+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8276+ unsigned int flags)
1facf9fc 8277+{
8278+ int npositive, err;
8279+ aufs_bindex_t bindex, btail, bdiropq;
8b6a4947 8280+ unsigned char isdir, dirperm1, dirren;
1facf9fc 8281+ struct au_do_lookup_args args = {
8b6a4947
AM
8282+ .flags = flags,
8283+ .name = &dentry->d_name
1facf9fc 8284+ };
1facf9fc 8285+ struct dentry *parent;
076b876e 8286+ struct super_block *sb;
1facf9fc 8287+
076b876e 8288+ sb = dentry->d_sb;
8b6a4947 8289+ err = au_test_shwh(sb, args.name);
dece6358 8290+ if (unlikely(err))
1facf9fc 8291+ goto out;
8292+
8b6a4947 8293+ err = au_wh_name_alloc(&args.whname, args.name);
1facf9fc 8294+ if (unlikely(err))
8295+ goto out;
8296+
2000de60 8297+ isdir = !!d_is_dir(dentry);
076b876e 8298+ dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8b6a4947
AM
8299+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8300+ if (dirren)
8301+ au_fset_lkup(args.flags, DIRREN);
1facf9fc 8302+
8303+ npositive = 0;
4a4d8108 8304+ parent = dget_parent(dentry);
1facf9fc 8305+ btail = au_dbtaildir(parent);
5afbbe0d 8306+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 8307+ struct dentry *h_parent, *h_dentry;
8308+ struct inode *h_inode, *h_dir;
8b6a4947 8309+ struct au_branch *br;
1facf9fc 8310+
8311+ h_dentry = au_h_dptr(dentry, bindex);
8312+ if (h_dentry) {
5527c038 8313+ if (d_is_positive(h_dentry))
1facf9fc 8314+ npositive++;
5afbbe0d 8315+ break;
1facf9fc 8316+ }
8317+ h_parent = au_h_dptr(parent, bindex);
2000de60 8318+ if (!h_parent || !d_is_dir(h_parent))
1facf9fc 8319+ continue;
8320+
8b6a4947
AM
8321+ if (dirren) {
8322+ /* if the inum matches, then use the prepared name */
8323+ err = au_dr_lkup_name(&args, bindex);
8324+ if (unlikely(err))
8325+ goto out_parent;
8326+ }
8327+
5527c038 8328+ h_dir = d_inode(h_parent);
be118d29 8329+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8b6a4947 8330+ h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
3c1bdaff 8331+ inode_unlock_shared(h_dir);
1facf9fc 8332+ err = PTR_ERR(h_dentry);
8333+ if (IS_ERR(h_dentry))
4a4d8108 8334+ goto out_parent;
2000de60
JR
8335+ if (h_dentry)
8336+ au_fclr_lkup(args.flags, ALLOW_NEG);
076b876e
AM
8337+ if (dirperm1)
8338+ au_fset_lkup(args.flags, IGNORE_PERM);
1facf9fc 8339+
79b8bda9 8340+ if (au_dbwh(dentry) == bindex)
1facf9fc 8341+ break;
8342+ if (!h_dentry)
8343+ continue;
5527c038 8344+ if (d_is_negative(h_dentry))
1facf9fc 8345+ continue;
5527c038 8346+ h_inode = d_inode(h_dentry);
1facf9fc 8347+ npositive++;
8348+ if (!args.type)
8349+ args.type = h_inode->i_mode & S_IFMT;
8350+ if (args.type != S_IFDIR)
8351+ break;
8352+ else if (isdir) {
8353+ /* the type of lower may be different */
8354+ bdiropq = au_dbdiropq(dentry);
8355+ if (bdiropq >= 0 && bdiropq <= bindex)
8356+ break;
8357+ }
8b6a4947
AM
8358+ br = au_sbr(sb, bindex);
8359+ if (dirren
8360+ && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8361+ /*add_ent*/NULL)) {
8362+ /* prepare next name to lookup */
8363+ err = au_dr_lkup(&args, dentry, bindex);
8364+ if (unlikely(err))
8365+ goto out_parent;
8366+ }
1facf9fc 8367+ }
8368+
8369+ if (npositive) {
8370+ AuLabel(positive);
5afbbe0d 8371+ au_update_dbtop(dentry);
1facf9fc 8372+ }
8373+ err = npositive;
076b876e 8374+ if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
5afbbe0d 8375+ && au_dbtop(dentry) < 0)) {
1facf9fc 8376+ err = -EIO;
523b37e3
AM
8377+ AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8378+ dentry, err);
027c5e7a 8379+ }
1facf9fc 8380+
4f0767ce 8381+out_parent:
4a4d8108 8382+ dput(parent);
e49925d1 8383+ au_kfree_try_rcu(args.whname.name);
8b6a4947
AM
8384+ if (dirren)
8385+ au_dr_lkup_fin(&args);
4f0767ce 8386+out:
1facf9fc 8387+ return err;
8388+}
8389+
076b876e 8390+struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
1facf9fc 8391+{
8392+ struct dentry *dentry;
8393+ int wkq_err;
8394+
5527c038 8395+ if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
b4510431 8396+ dentry = vfsub_lkup_one(name, parent);
1facf9fc 8397+ else {
b4510431
AM
8398+ struct vfsub_lkup_one_args args = {
8399+ .errp = &dentry,
8400+ .name = name,
8401+ .parent = parent
1facf9fc 8402+ };
8403+
b4510431 8404+ wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
1facf9fc 8405+ if (unlikely(wkq_err))
8406+ dentry = ERR_PTR(wkq_err);
8407+ }
8408+
8409+ return dentry;
8410+}
8411+
8412+/*
8413+ * lookup @dentry on @bindex which should be negative.
8414+ */
86dc4139 8415+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
1facf9fc 8416+{
8417+ int err;
8418+ struct dentry *parent, *h_parent, *h_dentry;
86dc4139 8419+ struct au_branch *br;
1facf9fc 8420+
1facf9fc 8421+ parent = dget_parent(dentry);
8422+ h_parent = au_h_dptr(parent, bindex);
86dc4139
AM
8423+ br = au_sbr(dentry->d_sb, bindex);
8424+ if (wh)
8425+ h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
8426+ else
076b876e 8427+ h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
1facf9fc 8428+ err = PTR_ERR(h_dentry);
8429+ if (IS_ERR(h_dentry))
8430+ goto out;
5527c038 8431+ if (unlikely(d_is_positive(h_dentry))) {
1facf9fc 8432+ err = -EIO;
523b37e3 8433+ AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
1facf9fc 8434+ dput(h_dentry);
8435+ goto out;
8436+ }
8437+
4a4d8108 8438+ err = 0;
5afbbe0d
AM
8439+ if (bindex < au_dbtop(dentry))
8440+ au_set_dbtop(dentry, bindex);
8441+ if (au_dbbot(dentry) < bindex)
8442+ au_set_dbbot(dentry, bindex);
1facf9fc 8443+ au_set_h_dptr(dentry, bindex, h_dentry);
1facf9fc 8444+
4f0767ce 8445+out:
1facf9fc 8446+ dput(parent);
8447+ return err;
8448+}
8449+
8450+/* ---------------------------------------------------------------------- */
8451+
8452+/* subset of struct inode */
8453+struct au_iattr {
8454+ unsigned long i_ino;
8455+ /* unsigned int i_nlink; */
0c3ec466
AM
8456+ kuid_t i_uid;
8457+ kgid_t i_gid;
1facf9fc 8458+ u64 i_version;
8459+/*
8460+ loff_t i_size;
8461+ blkcnt_t i_blocks;
8462+*/
8463+ umode_t i_mode;
8464+};
8465+
8466+static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8467+{
8468+ ia->i_ino = h_inode->i_ino;
8469+ /* ia->i_nlink = h_inode->i_nlink; */
8470+ ia->i_uid = h_inode->i_uid;
8471+ ia->i_gid = h_inode->i_gid;
be118d29 8472+ ia->i_version = inode_query_iversion(h_inode);
1facf9fc 8473+/*
8474+ ia->i_size = h_inode->i_size;
8475+ ia->i_blocks = h_inode->i_blocks;
8476+*/
8477+ ia->i_mode = (h_inode->i_mode & S_IFMT);
8478+}
8479+
8480+static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8481+{
8482+ return ia->i_ino != h_inode->i_ino
8483+ /* || ia->i_nlink != h_inode->i_nlink */
0c3ec466 8484+ || !uid_eq(ia->i_uid, h_inode->i_uid)
2dfbb274 8485+ || !gid_eq(ia->i_gid, h_inode->i_gid)
be118d29 8486+ || !inode_eq_iversion(h_inode, ia->i_version)
1facf9fc 8487+/*
8488+ || ia->i_size != h_inode->i_size
8489+ || ia->i_blocks != h_inode->i_blocks
8490+*/
8491+ || ia->i_mode != (h_inode->i_mode & S_IFMT);
8492+}
8493+
8494+static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8495+ struct au_branch *br)
8496+{
8497+ int err;
8498+ struct au_iattr ia;
8499+ struct inode *h_inode;
8500+ struct dentry *h_d;
8501+ struct super_block *h_sb;
8502+
8503+ err = 0;
8504+ memset(&ia, -1, sizeof(ia));
8505+ h_sb = h_dentry->d_sb;
5527c038
JR
8506+ h_inode = NULL;
8507+ if (d_is_positive(h_dentry)) {
8508+ h_inode = d_inode(h_dentry);
1facf9fc 8509+ au_iattr_save(&ia, h_inode);
5527c038 8510+ } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
1facf9fc 8511+ /* nfs d_revalidate may return 0 for negative dentry */
8512+ /* fuse d_revalidate always return 0 for negative dentry */
8513+ goto out;
8514+
8515+ /* main purpose is namei.c:cached_lookup() and d_revalidate */
b4510431 8516+ h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
1facf9fc 8517+ err = PTR_ERR(h_d);
8518+ if (IS_ERR(h_d))
8519+ goto out;
8520+
8521+ err = 0;
8522+ if (unlikely(h_d != h_dentry
5527c038 8523+ || d_inode(h_d) != h_inode
1facf9fc 8524+ || (h_inode && au_iattr_test(&ia, h_inode))))
8525+ err = au_busy_or_stale();
8526+ dput(h_d);
8527+
4f0767ce 8528+out:
1facf9fc 8529+ AuTraceErr(err);
8530+ return err;
8531+}
8532+
8533+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8534+ struct dentry *h_parent, struct au_branch *br)
8535+{
8536+ int err;
8537+
8538+ err = 0;
027c5e7a
AM
8539+ if (udba == AuOpt_UDBA_REVAL
8540+ && !au_test_fs_remote(h_dentry->d_sb)) {
1facf9fc 8541+ IMustLock(h_dir);
5527c038 8542+ err = (d_inode(h_dentry->d_parent) != h_dir);
027c5e7a 8543+ } else if (udba != AuOpt_UDBA_NONE)
1facf9fc 8544+ err = au_h_verify_dentry(h_dentry, h_parent, br);
8545+
8546+ return err;
8547+}
8548+
8549+/* ---------------------------------------------------------------------- */
8550+
027c5e7a 8551+static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
1facf9fc 8552+{
027c5e7a 8553+ int err;
5afbbe0d 8554+ aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
027c5e7a
AM
8555+ struct au_hdentry tmp, *p, *q;
8556+ struct au_dinfo *dinfo;
8557+ struct super_block *sb;
1facf9fc 8558+
027c5e7a 8559+ DiMustWriteLock(dentry);
1308ab2a 8560+
027c5e7a
AM
8561+ sb = dentry->d_sb;
8562+ dinfo = au_di(dentry);
5afbbe0d 8563+ bbot = dinfo->di_bbot;
1facf9fc 8564+ bwh = dinfo->di_bwh;
8565+ bdiropq = dinfo->di_bdiropq;
5afbbe0d
AM
8566+ bindex = dinfo->di_btop;
8567+ p = au_hdentry(dinfo, bindex);
8568+ for (; bindex <= bbot; bindex++, p++) {
027c5e7a 8569+ if (!p->hd_dentry)
1facf9fc 8570+ continue;
8571+
027c5e7a
AM
8572+ new_bindex = au_br_index(sb, p->hd_id);
8573+ if (new_bindex == bindex)
1facf9fc 8574+ continue;
1facf9fc 8575+
1facf9fc 8576+ if (dinfo->di_bwh == bindex)
8577+ bwh = new_bindex;
8578+ if (dinfo->di_bdiropq == bindex)
8579+ bdiropq = new_bindex;
8580+ if (new_bindex < 0) {
8581+ au_hdput(p);
8582+ p->hd_dentry = NULL;
8583+ continue;
8584+ }
8585+
8586+ /* swap two lower dentries, and loop again */
5afbbe0d 8587+ q = au_hdentry(dinfo, new_bindex);
1facf9fc 8588+ tmp = *q;
8589+ *q = *p;
8590+ *p = tmp;
8591+ if (tmp.hd_dentry) {
8592+ bindex--;
8593+ p--;
8594+ }
8595+ }
8596+
1facf9fc 8597+ dinfo->di_bwh = -1;
5afbbe0d 8598+ if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
1facf9fc 8599+ dinfo->di_bwh = bwh;
8600+
8601+ dinfo->di_bdiropq = -1;
8602+ if (bdiropq >= 0
5afbbe0d 8603+ && bdiropq <= au_sbbot(sb)
1facf9fc 8604+ && au_sbr_whable(sb, bdiropq))
8605+ dinfo->di_bdiropq = bdiropq;
8606+
027c5e7a 8607+ err = -EIO;
5afbbe0d
AM
8608+ dinfo->di_btop = -1;
8609+ dinfo->di_bbot = -1;
8610+ bbot = au_dbbot(parent);
8611+ bindex = 0;
8612+ p = au_hdentry(dinfo, bindex);
8613+ for (; bindex <= bbot; bindex++, p++)
1facf9fc 8614+ if (p->hd_dentry) {
5afbbe0d 8615+ dinfo->di_btop = bindex;
1facf9fc 8616+ break;
8617+ }
8618+
5afbbe0d
AM
8619+ if (dinfo->di_btop >= 0) {
8620+ bindex = bbot;
8621+ p = au_hdentry(dinfo, bindex);
8622+ for (; bindex >= 0; bindex--, p--)
027c5e7a 8623+ if (p->hd_dentry) {
5afbbe0d 8624+ dinfo->di_bbot = bindex;
027c5e7a
AM
8625+ err = 0;
8626+ break;
8627+ }
8628+ }
8629+
8630+ return err;
1facf9fc 8631+}
8632+
027c5e7a 8633+static void au_do_hide(struct dentry *dentry)
1facf9fc 8634+{
027c5e7a 8635+ struct inode *inode;
1facf9fc 8636+
5527c038
JR
8637+ if (d_really_is_positive(dentry)) {
8638+ inode = d_inode(dentry);
8639+ if (!d_is_dir(dentry)) {
027c5e7a
AM
8640+ if (inode->i_nlink && !d_unhashed(dentry))
8641+ drop_nlink(inode);
8642+ } else {
8643+ clear_nlink(inode);
8644+ /* stop next lookup */
8645+ inode->i_flags |= S_DEAD;
8646+ }
8647+ smp_mb(); /* necessary? */
8648+ }
8649+ d_drop(dentry);
8650+}
1308ab2a 8651+
027c5e7a
AM
8652+static int au_hide_children(struct dentry *parent)
8653+{
8654+ int err, i, j, ndentry;
8655+ struct au_dcsub_pages dpages;
8656+ struct au_dpage *dpage;
8657+ struct dentry *dentry;
1facf9fc 8658+
027c5e7a 8659+ err = au_dpages_init(&dpages, GFP_NOFS);
1facf9fc 8660+ if (unlikely(err))
8661+ goto out;
027c5e7a
AM
8662+ err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8663+ if (unlikely(err))
8664+ goto out_dpages;
1facf9fc 8665+
027c5e7a
AM
8666+ /* in reverse order */
8667+ for (i = dpages.ndpage - 1; i >= 0; i--) {
8668+ dpage = dpages.dpages + i;
8669+ ndentry = dpage->ndentry;
8670+ for (j = ndentry - 1; j >= 0; j--) {
8671+ dentry = dpage->dentries[j];
8672+ if (dentry != parent)
8673+ au_do_hide(dentry);
8674+ }
8675+ }
1facf9fc 8676+
027c5e7a
AM
8677+out_dpages:
8678+ au_dpages_free(&dpages);
4f0767ce 8679+out:
027c5e7a 8680+ return err;
1facf9fc 8681+}
8682+
027c5e7a 8683+static void au_hide(struct dentry *dentry)
1facf9fc 8684+{
027c5e7a 8685+ int err;
1facf9fc 8686+
027c5e7a 8687+ AuDbgDentry(dentry);
2000de60 8688+ if (d_is_dir(dentry)) {
027c5e7a
AM
8689+ /* shrink_dcache_parent(dentry); */
8690+ err = au_hide_children(dentry);
8691+ if (unlikely(err))
523b37e3
AM
8692+ AuIOErr("%pd, failed hiding children, ignored %d\n",
8693+ dentry, err);
027c5e7a
AM
8694+ }
8695+ au_do_hide(dentry);
8696+}
1facf9fc 8697+
027c5e7a
AM
8698+/*
8699+ * By adding a dirty branch, a cached dentry may be affected in various ways.
8700+ *
8701+ * a dirty branch is added
8702+ * - on the top of layers
8703+ * - in the middle of layers
8704+ * - to the bottom of layers
8705+ *
8706+ * on the added branch there exists
8707+ * - a whiteout
8708+ * - a diropq
8709+ * - a same named entry
8710+ * + exist
8711+ * * negative --> positive
8712+ * * positive --> positive
8713+ * - type is unchanged
8714+ * - type is changed
8715+ * + doesn't exist
8716+ * * negative --> negative
8717+ * * positive --> negative (rejected by au_br_del() for non-dir case)
8718+ * - none
8719+ */
8720+static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8721+ struct au_dinfo *tmp)
8722+{
8723+ int err;
5afbbe0d 8724+ aufs_bindex_t bindex, bbot;
027c5e7a
AM
8725+ struct {
8726+ struct dentry *dentry;
8727+ struct inode *inode;
8728+ mode_t mode;
be52b249
AM
8729+ } orig_h, tmp_h = {
8730+ .dentry = NULL
8731+ };
027c5e7a
AM
8732+ struct au_hdentry *hd;
8733+ struct inode *inode, *h_inode;
8734+ struct dentry *h_dentry;
8735+
8736+ err = 0;
5afbbe0d 8737+ AuDebugOn(dinfo->di_btop < 0);
027c5e7a 8738+ orig_h.mode = 0;
5afbbe0d 8739+ orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
5527c038
JR
8740+ orig_h.inode = NULL;
8741+ if (d_is_positive(orig_h.dentry)) {
8742+ orig_h.inode = d_inode(orig_h.dentry);
027c5e7a 8743+ orig_h.mode = orig_h.inode->i_mode & S_IFMT;
5527c038 8744+ }
5afbbe0d
AM
8745+ if (tmp->di_btop >= 0) {
8746+ tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
5527c038
JR
8747+ if (d_is_positive(tmp_h.dentry)) {
8748+ tmp_h.inode = d_inode(tmp_h.dentry);
027c5e7a 8749+ tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
5527c038 8750+ }
027c5e7a
AM
8751+ }
8752+
5527c038
JR
8753+ inode = NULL;
8754+ if (d_really_is_positive(dentry))
8755+ inode = d_inode(dentry);
027c5e7a 8756+ if (!orig_h.inode) {
acd2b654 8757+ AuDbg("negative originally\n");
027c5e7a
AM
8758+ if (inode) {
8759+ au_hide(dentry);
8760+ goto out;
8761+ }
8762+ AuDebugOn(inode);
5afbbe0d 8763+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
027c5e7a
AM
8764+ AuDebugOn(dinfo->di_bdiropq != -1);
8765+
8766+ if (!tmp_h.inode) {
8767+ AuDbg("negative --> negative\n");
8768+ /* should have only one negative lower */
5afbbe0d
AM
8769+ if (tmp->di_btop >= 0
8770+ && tmp->di_btop < dinfo->di_btop) {
8771+ AuDebugOn(tmp->di_btop != tmp->di_bbot);
8772+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8773+ au_set_h_dptr(dentry, dinfo->di_btop, NULL);
027c5e7a 8774+ au_di_cp(dinfo, tmp);
5afbbe0d
AM
8775+ hd = au_hdentry(tmp, tmp->di_btop);
8776+ au_set_h_dptr(dentry, tmp->di_btop,
027c5e7a
AM
8777+ dget(hd->hd_dentry));
8778+ }
8779+ au_dbg_verify_dinode(dentry);
8780+ } else {
8781+ AuDbg("negative --> positive\n");
8782+ /*
8783+ * similar to the behaviour of creating with bypassing
8784+ * aufs.
8785+ * unhash it in order to force an error in the
8786+ * succeeding create operation.
8787+ * we should not set S_DEAD here.
8788+ */
8789+ d_drop(dentry);
8790+ /* au_di_swap(tmp, dinfo); */
8791+ au_dbg_verify_dinode(dentry);
8792+ }
8793+ } else {
8794+ AuDbg("positive originally\n");
8795+ /* inode may be NULL */
8796+ AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8797+ if (!tmp_h.inode) {
8798+ AuDbg("positive --> negative\n");
8799+ /* or bypassing aufs */
8800+ au_hide(dentry);
5afbbe0d 8801+ if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
027c5e7a
AM
8802+ dinfo->di_bwh = tmp->di_bwh;
8803+ if (inode)
8804+ err = au_refresh_hinode_self(inode);
8805+ au_dbg_verify_dinode(dentry);
8806+ } else if (orig_h.mode == tmp_h.mode) {
8807+ AuDbg("positive --> positive, same type\n");
8808+ if (!S_ISDIR(orig_h.mode)
5afbbe0d 8809+ && dinfo->di_btop > tmp->di_btop) {
027c5e7a
AM
8810+ /*
8811+ * similar to the behaviour of removing and
8812+ * creating.
8813+ */
8814+ au_hide(dentry);
8815+ if (inode)
8816+ err = au_refresh_hinode_self(inode);
8817+ au_dbg_verify_dinode(dentry);
8818+ } else {
8819+ /* fill empty slots */
5afbbe0d
AM
8820+ if (dinfo->di_btop > tmp->di_btop)
8821+ dinfo->di_btop = tmp->di_btop;
8822+ if (dinfo->di_bbot < tmp->di_bbot)
8823+ dinfo->di_bbot = tmp->di_bbot;
027c5e7a
AM
8824+ dinfo->di_bwh = tmp->di_bwh;
8825+ dinfo->di_bdiropq = tmp->di_bdiropq;
5afbbe0d
AM
8826+ bbot = dinfo->di_bbot;
8827+ bindex = tmp->di_btop;
8828+ hd = au_hdentry(tmp, bindex);
8829+ for (; bindex <= bbot; bindex++, hd++) {
027c5e7a
AM
8830+ if (au_h_dptr(dentry, bindex))
8831+ continue;
5afbbe0d 8832+ h_dentry = hd->hd_dentry;
027c5e7a
AM
8833+ if (!h_dentry)
8834+ continue;
5527c038
JR
8835+ AuDebugOn(d_is_negative(h_dentry));
8836+ h_inode = d_inode(h_dentry);
027c5e7a
AM
8837+ AuDebugOn(orig_h.mode
8838+ != (h_inode->i_mode
8839+ & S_IFMT));
8840+ au_set_h_dptr(dentry, bindex,
8841+ dget(h_dentry));
8842+ }
5afbbe0d
AM
8843+ if (inode)
8844+ err = au_refresh_hinode(inode, dentry);
027c5e7a
AM
8845+ au_dbg_verify_dinode(dentry);
8846+ }
8847+ } else {
8848+ AuDbg("positive --> positive, different type\n");
8849+ /* similar to the behaviour of removing and creating */
8850+ au_hide(dentry);
8851+ if (inode)
8852+ err = au_refresh_hinode_self(inode);
8853+ au_dbg_verify_dinode(dentry);
8854+ }
8855+ }
8856+
8857+out:
8858+ return err;
8859+}
8860+
79b8bda9
AM
8861+void au_refresh_dop(struct dentry *dentry, int force_reval)
8862+{
8863+ const struct dentry_operations *dop
8864+ = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8865+ static const unsigned int mask
8866+ = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8867+
8868+ BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8869+
8870+ if (dentry->d_op == dop)
8871+ return;
8872+
8873+ AuDbg("%pd\n", dentry);
8874+ spin_lock(&dentry->d_lock);
8875+ if (dop == &aufs_dop)
8876+ dentry->d_flags |= mask;
8877+ else
8878+ dentry->d_flags &= ~mask;
8879+ dentry->d_op = dop;
8880+ spin_unlock(&dentry->d_lock);
8881+}
8882+
027c5e7a
AM
8883+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8884+{
e2f27e51 8885+ int err, ebrange, nbr;
027c5e7a
AM
8886+ unsigned int sigen;
8887+ struct au_dinfo *dinfo, *tmp;
8888+ struct super_block *sb;
8889+ struct inode *inode;
8890+
8891+ DiMustWriteLock(dentry);
8892+ AuDebugOn(IS_ROOT(dentry));
5527c038 8893+ AuDebugOn(d_really_is_negative(parent));
027c5e7a
AM
8894+
8895+ sb = dentry->d_sb;
027c5e7a
AM
8896+ sigen = au_sigen(sb);
8897+ err = au_digen_test(parent, sigen);
8898+ if (unlikely(err))
8899+ goto out;
8900+
e2f27e51 8901+ nbr = au_sbbot(sb) + 1;
027c5e7a 8902+ dinfo = au_di(dentry);
e2f27e51 8903+ err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
027c5e7a
AM
8904+ if (unlikely(err))
8905+ goto out;
8906+ ebrange = au_dbrange_test(dentry);
8907+ if (!ebrange)
8908+ ebrange = au_do_refresh_hdentry(dentry, parent);
8909+
38d290e6 8910+ if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
5afbbe0d 8911+ AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
5527c038
JR
8912+ if (d_really_is_positive(dentry)) {
8913+ inode = d_inode(dentry);
027c5e7a 8914+ err = au_refresh_hinode_self(inode);
5527c038 8915+ }
027c5e7a
AM
8916+ au_dbg_verify_dinode(dentry);
8917+ if (!err)
8918+ goto out_dgen; /* success */
8919+ goto out;
8920+ }
8921+
8922+ /* temporary dinfo */
8923+ AuDbgDentry(dentry);
8924+ err = -ENOMEM;
8925+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8926+ if (unlikely(!tmp))
8927+ goto out;
8928+ au_di_swap(tmp, dinfo);
8929+ /* returns the number of positive dentries */
8930+ /*
8931+ * if current working dir is removed, it returns an error.
8932+ * but the dentry is legal.
8933+ */
5afbbe0d 8934+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
027c5e7a
AM
8935+ AuDbgDentry(dentry);
8936+ au_di_swap(tmp, dinfo);
8937+ if (err == -ENOENT)
8938+ err = 0;
8939+ if (err >= 0) {
8940+ /* compare/refresh by dinfo */
8941+ AuDbgDentry(dentry);
8942+ err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8943+ au_dbg_verify_dinode(dentry);
8944+ AuTraceErr(err);
8945+ }
e2f27e51 8946+ au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
027c5e7a
AM
8947+ au_rw_write_unlock(&tmp->di_rwsem);
8948+ au_di_free(tmp);
8949+ if (unlikely(err))
8950+ goto out;
8951+
8952+out_dgen:
8953+ au_update_digen(dentry);
8954+out:
8955+ if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
523b37e3 8956+ AuIOErr("failed refreshing %pd, %d\n", dentry, err);
027c5e7a
AM
8957+ AuDbgDentry(dentry);
8958+ }
8959+ AuTraceErr(err);
8960+ return err;
8961+}
8962+
b4510431
AM
8963+static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8964+ struct dentry *dentry, aufs_bindex_t bindex)
027c5e7a
AM
8965+{
8966+ int err, valid;
027c5e7a
AM
8967+
8968+ err = 0;
8969+ if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8970+ goto out;
027c5e7a
AM
8971+
8972+ AuDbg("b%d\n", bindex);
b4510431
AM
8973+ /*
8974+ * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8975+ * due to whiteout and branch permission.
8976+ */
8977+ flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8978+ | LOOKUP_FOLLOW | LOOKUP_EXCL);
8979+ /* it may return tri-state */
8980+ valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
1facf9fc 8981+
8982+ if (unlikely(valid < 0))
8983+ err = valid;
8984+ else if (!valid)
8985+ err = -EINVAL;
8986+
4f0767ce 8987+out:
1facf9fc 8988+ AuTraceErr(err);
8989+ return err;
8990+}
8991+
8992+/* todo: remove this */
8993+static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8b6a4947 8994+ unsigned int flags, int do_udba, int dirren)
1facf9fc 8995+{
8996+ int err;
8997+ umode_t mode, h_mode;
5afbbe0d 8998+ aufs_bindex_t bindex, btail, btop, ibs, ibe;
38d290e6 8999+ unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
4a4d8108 9000+ struct inode *h_inode, *h_cached_inode;
1facf9fc 9001+ struct dentry *h_dentry;
9002+ struct qstr *name, *h_name;
9003+
9004+ err = 0;
9005+ plus = 0;
9006+ mode = 0;
1facf9fc 9007+ ibs = -1;
9008+ ibe = -1;
9009+ unhashed = !!d_unhashed(dentry);
9010+ is_root = !!IS_ROOT(dentry);
9011+ name = &dentry->d_name;
38d290e6 9012+ tmpfile = au_di(dentry)->di_tmpfile;
1facf9fc 9013+
9014+ /*
7f207e10
AM
9015+ * Theoretically, REVAL test should be unnecessary in case of
9016+ * {FS,I}NOTIFY.
9017+ * But {fs,i}notify doesn't fire some necessary events,
1facf9fc 9018+ * IN_ATTRIB for atime/nlink/pageio
1facf9fc 9019+ * Let's do REVAL test too.
9020+ */
9021+ if (do_udba && inode) {
9022+ mode = (inode->i_mode & S_IFMT);
9023+ plus = (inode->i_nlink > 0);
5afbbe0d
AM
9024+ ibs = au_ibtop(inode);
9025+ ibe = au_ibbot(inode);
1facf9fc 9026+ }
9027+
5afbbe0d
AM
9028+ btop = au_dbtop(dentry);
9029+ btail = btop;
1facf9fc 9030+ if (inode && S_ISDIR(inode->i_mode))
9031+ btail = au_dbtaildir(dentry);
5afbbe0d 9032+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 9033+ h_dentry = au_h_dptr(dentry, bindex);
9034+ if (!h_dentry)
9035+ continue;
9036+
523b37e3
AM
9037+ AuDbg("b%d, %pd\n", bindex, h_dentry);
9038+ h_nfs = !!au_test_nfs(h_dentry->d_sb);
027c5e7a 9039+ spin_lock(&h_dentry->d_lock);
1facf9fc 9040+ h_name = &h_dentry->d_name;
9041+ if (unlikely(do_udba
9042+ && !is_root
523b37e3
AM
9043+ && ((!h_nfs
9044+ && (unhashed != !!d_unhashed(h_dentry)
8b6a4947 9045+ || (!tmpfile && !dirren
38d290e6
JR
9046+ && !au_qstreq(name, h_name))
9047+ ))
523b37e3
AM
9048+ || (h_nfs
9049+ && !(flags & LOOKUP_OPEN)
9050+ && (h_dentry->d_flags
9051+ & DCACHE_NFSFS_RENAMED)))
1facf9fc 9052+ )) {
38d290e6
JR
9053+ int h_unhashed;
9054+
9055+ h_unhashed = d_unhashed(h_dentry);
027c5e7a 9056+ spin_unlock(&h_dentry->d_lock);
38d290e6
JR
9057+ AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
9058+ unhashed, h_unhashed, dentry, h_dentry);
1facf9fc 9059+ goto err;
9060+ }
027c5e7a 9061+ spin_unlock(&h_dentry->d_lock);
1facf9fc 9062+
b4510431 9063+ err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
1facf9fc 9064+ if (unlikely(err))
9065+ /* do not goto err, to keep the errno */
9066+ break;
9067+
9068+ /* todo: plink too? */
9069+ if (!do_udba)
9070+ continue;
9071+
9072+ /* UDBA tests */
5527c038 9073+ if (unlikely(!!inode != d_is_positive(h_dentry)))
1facf9fc 9074+ goto err;
9075+
5527c038
JR
9076+ h_inode = NULL;
9077+ if (d_is_positive(h_dentry))
9078+ h_inode = d_inode(h_dentry);
1facf9fc 9079+ h_plus = plus;
9080+ h_mode = mode;
9081+ h_cached_inode = h_inode;
9082+ if (h_inode) {
9083+ h_mode = (h_inode->i_mode & S_IFMT);
9084+ h_plus = (h_inode->i_nlink > 0);
9085+ }
9086+ if (inode && ibs <= bindex && bindex <= ibe)
9087+ h_cached_inode = au_h_iptr(inode, bindex);
9088+
523b37e3 9089+ if (!h_nfs) {
38d290e6 9090+ if (unlikely(plus != h_plus && !tmpfile))
523b37e3
AM
9091+ goto err;
9092+ } else {
9093+ if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
9094+ && !is_root
9095+ && !IS_ROOT(h_dentry)
9096+ && unhashed != d_unhashed(h_dentry)))
9097+ goto err;
9098+ }
9099+ if (unlikely(mode != h_mode
1facf9fc 9100+ || h_cached_inode != h_inode))
9101+ goto err;
9102+ continue;
9103+
f6b6e03d 9104+err:
1facf9fc 9105+ err = -EINVAL;
9106+ break;
9107+ }
9108+
523b37e3 9109+ AuTraceErr(err);
1facf9fc 9110+ return err;
9111+}
9112+
027c5e7a 9113+/* todo: consolidate with do_refresh() and au_reval_for_attr() */
1facf9fc 9114+static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
9115+{
9116+ int err;
9117+ struct dentry *parent;
1facf9fc 9118+
027c5e7a 9119+ if (!au_digen_test(dentry, sigen))
1facf9fc 9120+ return 0;
9121+
9122+ parent = dget_parent(dentry);
9123+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 9124+ AuDebugOn(au_digen_test(parent, sigen));
1facf9fc 9125+ au_dbg_verify_gen(parent, sigen);
027c5e7a 9126+ err = au_refresh_dentry(dentry, parent);
1facf9fc 9127+ di_read_unlock(parent, AuLock_IR);
9128+ dput(parent);
027c5e7a 9129+ AuTraceErr(err);
1facf9fc 9130+ return err;
9131+}
9132+
9133+int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
9134+{
9135+ int err;
9136+ struct dentry *d, *parent;
1facf9fc 9137+
027c5e7a 9138+ if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
1facf9fc 9139+ return simple_reval_dpath(dentry, sigen);
9140+
9141+ /* slow loop, keep it simple and stupid */
9142+ /* cf: au_cpup_dirs() */
9143+ err = 0;
9144+ parent = NULL;
027c5e7a 9145+ while (au_digen_test(dentry, sigen)) {
1facf9fc 9146+ d = dentry;
9147+ while (1) {
9148+ dput(parent);
9149+ parent = dget_parent(d);
027c5e7a 9150+ if (!au_digen_test(parent, sigen))
1facf9fc 9151+ break;
9152+ d = parent;
9153+ }
9154+
1facf9fc 9155+ if (d != dentry)
027c5e7a 9156+ di_write_lock_child2(d);
1facf9fc 9157+
9158+ /* someone might update our dentry while we were sleeping */
027c5e7a
AM
9159+ if (au_digen_test(d, sigen)) {
9160+ /*
9161+ * todo: consolidate with simple_reval_dpath(),
9162+ * do_refresh() and au_reval_for_attr().
9163+ */
1facf9fc 9164+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 9165+ err = au_refresh_dentry(d, parent);
1facf9fc 9166+ di_read_unlock(parent, AuLock_IR);
9167+ }
9168+
9169+ if (d != dentry)
9170+ di_write_unlock(d);
9171+ dput(parent);
9172+ if (unlikely(err))
9173+ break;
9174+ }
9175+
9176+ return err;
9177+}
9178+
9179+/*
9180+ * if valid returns 1, otherwise 0.
9181+ */
b4510431 9182+static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
1facf9fc 9183+{
9184+ int valid, err;
9185+ unsigned int sigen;
8b6a4947 9186+ unsigned char do_udba, dirren;
1facf9fc 9187+ struct super_block *sb;
9188+ struct inode *inode;
9189+
027c5e7a 9190+ /* todo: support rcu-walk? */
b4510431 9191+ if (flags & LOOKUP_RCU)
027c5e7a
AM
9192+ return -ECHILD;
9193+
9194+ valid = 0;
9195+ if (unlikely(!au_di(dentry)))
9196+ goto out;
9197+
e49829fe 9198+ valid = 1;
1facf9fc 9199+ sb = dentry->d_sb;
e49829fe
JR
9200+ /*
9201+ * todo: very ugly
9202+ * i_mutex of parent dir may be held,
9203+ * but we should not return 'invalid' due to busy.
9204+ */
9205+ err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9206+ if (unlikely(err)) {
9207+ valid = err;
027c5e7a 9208+ AuTraceErr(err);
e49829fe
JR
9209+ goto out;
9210+ }
5527c038
JR
9211+ inode = NULL;
9212+ if (d_really_is_positive(dentry))
9213+ inode = d_inode(dentry);
5afbbe0d 9214+ if (unlikely(inode && au_is_bad_inode(inode))) {
c1595e42
JR
9215+ err = -EINVAL;
9216+ AuTraceErr(err);
9217+ goto out_dgrade;
9218+ }
027c5e7a
AM
9219+ if (unlikely(au_dbrange_test(dentry))) {
9220+ err = -EINVAL;
9221+ AuTraceErr(err);
9222+ goto out_dgrade;
1facf9fc 9223+ }
027c5e7a
AM
9224+
9225+ sigen = au_sigen(sb);
9226+ if (au_digen_test(dentry, sigen)) {
1facf9fc 9227+ AuDebugOn(IS_ROOT(dentry));
027c5e7a
AM
9228+ err = au_reval_dpath(dentry, sigen);
9229+ if (unlikely(err)) {
9230+ AuTraceErr(err);
1facf9fc 9231+ goto out_dgrade;
027c5e7a 9232+ }
1facf9fc 9233+ }
9234+ di_downgrade_lock(dentry, AuLock_IR);
9235+
1facf9fc 9236+ err = -EINVAL;
c1595e42 9237+ if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
523b37e3 9238+ && inode
38d290e6 9239+ && !(inode->i_state && I_LINKABLE)
79b8bda9
AM
9240+ && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9241+ AuTraceErr(err);
027c5e7a 9242+ goto out_inval;
79b8bda9 9243+ }
027c5e7a 9244+
1facf9fc 9245+ do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9246+ if (do_udba && inode) {
5afbbe0d 9247+ aufs_bindex_t btop = au_ibtop(inode);
027c5e7a 9248+ struct inode *h_inode;
1facf9fc 9249+
5afbbe0d
AM
9250+ if (btop >= 0) {
9251+ h_inode = au_h_iptr(inode, btop);
79b8bda9
AM
9252+ if (h_inode && au_test_higen(inode, h_inode)) {
9253+ AuTraceErr(err);
027c5e7a 9254+ goto out_inval;
79b8bda9 9255+ }
027c5e7a 9256+ }
1facf9fc 9257+ }
9258+
8b6a4947
AM
9259+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9260+ err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
5afbbe0d 9261+ if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
1facf9fc 9262+ err = -EIO;
523b37e3
AM
9263+ AuDbg("both of real entry and whiteout found, %p, err %d\n",
9264+ dentry, err);
027c5e7a 9265+ }
e49829fe 9266+ goto out_inval;
1facf9fc 9267+
4f0767ce 9268+out_dgrade:
1facf9fc 9269+ di_downgrade_lock(dentry, AuLock_IR);
e49829fe 9270+out_inval:
1facf9fc 9271+ aufs_read_unlock(dentry, AuLock_IR);
9272+ AuTraceErr(err);
9273+ valid = !err;
e49829fe 9274+out:
027c5e7a 9275+ if (!valid) {
523b37e3 9276+ AuDbg("%pd invalid, %d\n", dentry, valid);
027c5e7a
AM
9277+ d_drop(dentry);
9278+ }
1facf9fc 9279+ return valid;
9280+}
9281+
9282+static void aufs_d_release(struct dentry *dentry)
9283+{
027c5e7a 9284+ if (au_di(dentry)) {
4a4d8108
AM
9285+ au_di_fin(dentry);
9286+ au_hn_di_reinit(dentry);
1facf9fc 9287+ }
1facf9fc 9288+}
9289+
4a4d8108 9290+const struct dentry_operations aufs_dop = {
c06a8ce3
AM
9291+ .d_revalidate = aufs_d_revalidate,
9292+ .d_weak_revalidate = aufs_d_revalidate,
9293+ .d_release = aufs_d_release
1facf9fc 9294+};
79b8bda9
AM
9295+
9296+/* aufs_dop without d_revalidate */
9297+const struct dentry_operations aufs_dop_noreval = {
9298+ .d_release = aufs_d_release
9299+};
7f207e10
AM
9300diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9301--- /usr/share/empty/fs/aufs/dentry.h 1970-01-01 01:00:00.000000000 +0100
e49925d1
JR
9302+++ linux/fs/aufs/dentry.h 2019-01-28 14:36:12.219084659 +0100
9303@@ -0,0 +1,268 @@
062440b3 9304+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 9305+/*
b00004a5 9306+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 9307+ *
9308+ * This program, aufs is free software; you can redistribute it and/or modify
9309+ * it under the terms of the GNU General Public License as published by
9310+ * the Free Software Foundation; either version 2 of the License, or
9311+ * (at your option) any later version.
dece6358
AM
9312+ *
9313+ * This program is distributed in the hope that it will be useful,
9314+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9315+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9316+ * GNU General Public License for more details.
9317+ *
9318+ * You should have received a copy of the GNU General Public License
523b37e3 9319+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9320+ */
9321+
9322+/*
9323+ * lookup and dentry operations
9324+ */
9325+
9326+#ifndef __AUFS_DENTRY_H__
9327+#define __AUFS_DENTRY_H__
9328+
9329+#ifdef __KERNEL__
9330+
dece6358 9331+#include <linux/dcache.h>
8b6a4947 9332+#include "dirren.h"
1facf9fc 9333+#include "rwsem.h"
9334+
1facf9fc 9335+struct au_hdentry {
9336+ struct dentry *hd_dentry;
027c5e7a 9337+ aufs_bindex_t hd_id;
1facf9fc 9338+};
9339+
9340+struct au_dinfo {
9341+ atomic_t di_generation;
9342+
dece6358 9343+ struct au_rwsem di_rwsem;
5afbbe0d 9344+ aufs_bindex_t di_btop, di_bbot, di_bwh, di_bdiropq;
38d290e6 9345+ unsigned char di_tmpfile; /* to allow the different name */
1c60b727 9346+ struct au_hdentry *di_hdentry;
e49925d1 9347+ struct rcu_head rcu;
4a4d8108 9348+} ____cacheline_aligned_in_smp;
1facf9fc 9349+
9350+/* ---------------------------------------------------------------------- */
9351+
5afbbe0d
AM
9352+/* flags for au_lkup_dentry() */
9353+#define AuLkup_ALLOW_NEG 1
9354+#define AuLkup_IGNORE_PERM (1 << 1)
8b6a4947 9355+#define AuLkup_DIRREN (1 << 2)
5afbbe0d
AM
9356+#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
9357+#define au_fset_lkup(flags, name) \
9358+ do { (flags) |= AuLkup_##name; } while (0)
9359+#define au_fclr_lkup(flags, name) \
9360+ do { (flags) &= ~AuLkup_##name; } while (0)
9361+
8b6a4947
AM
9362+#ifndef CONFIG_AUFS_DIRREN
9363+#undef AuLkup_DIRREN
9364+#define AuLkup_DIRREN 0
9365+#endif
9366+
9367+struct au_do_lookup_args {
9368+ unsigned int flags;
9369+ mode_t type;
9370+ struct qstr whname, *name;
9371+ struct au_dr_lookup dirren;
9372+};
9373+
5afbbe0d
AM
9374+/* ---------------------------------------------------------------------- */
9375+
1facf9fc 9376+/* dentry.c */
79b8bda9 9377+extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
1facf9fc 9378+struct au_branch;
076b876e 9379+struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
1facf9fc 9380+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9381+ struct dentry *h_parent, struct au_branch *br);
9382+
5afbbe0d
AM
9383+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9384+ unsigned int flags);
86dc4139 9385+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
027c5e7a 9386+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
1facf9fc 9387+int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
79b8bda9 9388+void au_refresh_dop(struct dentry *dentry, int force_reval);
1facf9fc 9389+
9390+/* dinfo.c */
4a4d8108 9391+void au_di_init_once(void *_di);
027c5e7a
AM
9392+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9393+void au_di_free(struct au_dinfo *dinfo);
9394+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9395+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
4a4d8108
AM
9396+int au_di_init(struct dentry *dentry);
9397+void au_di_fin(struct dentry *dentry);
e2f27e51 9398+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
1facf9fc 9399+
9400+void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9401+void di_read_unlock(struct dentry *d, int flags);
9402+void di_downgrade_lock(struct dentry *d, int flags);
9403+void di_write_lock(struct dentry *d, unsigned int lsc);
9404+void di_write_unlock(struct dentry *d);
9405+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9406+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9407+void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9408+
9409+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
2cbb1c4b 9410+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
1facf9fc 9411+aufs_bindex_t au_dbtail(struct dentry *dentry);
9412+aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9413+
9414+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9415+ struct dentry *h_dentry);
027c5e7a
AM
9416+int au_digen_test(struct dentry *dentry, unsigned int sigen);
9417+int au_dbrange_test(struct dentry *dentry);
1facf9fc 9418+void au_update_digen(struct dentry *dentry);
9419+void au_update_dbrange(struct dentry *dentry, int do_put_zero);
5afbbe0d
AM
9420+void au_update_dbtop(struct dentry *dentry);
9421+void au_update_dbbot(struct dentry *dentry);
1facf9fc 9422+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9423+
9424+/* ---------------------------------------------------------------------- */
9425+
9426+static inline struct au_dinfo *au_di(struct dentry *dentry)
9427+{
9428+ return dentry->d_fsdata;
9429+}
9430+
9431+/* ---------------------------------------------------------------------- */
9432+
9433+/* lock subclass for dinfo */
9434+enum {
9435+ AuLsc_DI_CHILD, /* child first */
4a4d8108 9436+ AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
1facf9fc 9437+ AuLsc_DI_CHILD3, /* copyup dirs */
9438+ AuLsc_DI_PARENT,
9439+ AuLsc_DI_PARENT2,
027c5e7a
AM
9440+ AuLsc_DI_PARENT3,
9441+ AuLsc_DI_TMP /* temp for replacing dinfo */
1facf9fc 9442+};
9443+
9444+/*
9445+ * di_read_lock_child, di_write_lock_child,
9446+ * di_read_lock_child2, di_write_lock_child2,
9447+ * di_read_lock_child3, di_write_lock_child3,
9448+ * di_read_lock_parent, di_write_lock_parent,
9449+ * di_read_lock_parent2, di_write_lock_parent2,
9450+ * di_read_lock_parent3, di_write_lock_parent3,
9451+ */
9452+#define AuReadLockFunc(name, lsc) \
9453+static inline void di_read_lock_##name(struct dentry *d, int flags) \
9454+{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9455+
9456+#define AuWriteLockFunc(name, lsc) \
9457+static inline void di_write_lock_##name(struct dentry *d) \
9458+{ di_write_lock(d, AuLsc_DI_##lsc); }
9459+
9460+#define AuRWLockFuncs(name, lsc) \
9461+ AuReadLockFunc(name, lsc) \
9462+ AuWriteLockFunc(name, lsc)
9463+
9464+AuRWLockFuncs(child, CHILD);
9465+AuRWLockFuncs(child2, CHILD2);
9466+AuRWLockFuncs(child3, CHILD3);
9467+AuRWLockFuncs(parent, PARENT);
9468+AuRWLockFuncs(parent2, PARENT2);
9469+AuRWLockFuncs(parent3, PARENT3);
9470+
9471+#undef AuReadLockFunc
9472+#undef AuWriteLockFunc
9473+#undef AuRWLockFuncs
9474+
9475+#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
dece6358
AM
9476+#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
9477+#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
1facf9fc 9478+
9479+/* ---------------------------------------------------------------------- */
9480+
9481+/* todo: memory barrier? */
9482+static inline unsigned int au_digen(struct dentry *d)
9483+{
9484+ return atomic_read(&au_di(d)->di_generation);
9485+}
9486+
9487+static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9488+{
9489+ hdentry->hd_dentry = NULL;
9490+}
9491+
5afbbe0d
AM
9492+static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9493+ aufs_bindex_t bindex)
9494+{
9495+ return di->di_hdentry + bindex;
9496+}
9497+
1facf9fc 9498+static inline void au_hdput(struct au_hdentry *hd)
9499+{
4a4d8108
AM
9500+ if (hd)
9501+ dput(hd->hd_dentry);
1facf9fc 9502+}
9503+
5afbbe0d 9504+static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
1facf9fc 9505+{
1308ab2a 9506+ DiMustAnyLock(dentry);
5afbbe0d 9507+ return au_di(dentry)->di_btop;
1facf9fc 9508+}
9509+
5afbbe0d 9510+static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
1facf9fc 9511+{
1308ab2a 9512+ DiMustAnyLock(dentry);
5afbbe0d 9513+ return au_di(dentry)->di_bbot;
1facf9fc 9514+}
9515+
9516+static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9517+{
1308ab2a 9518+ DiMustAnyLock(dentry);
1facf9fc 9519+ return au_di(dentry)->di_bwh;
9520+}
9521+
9522+static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9523+{
1308ab2a 9524+ DiMustAnyLock(dentry);
1facf9fc 9525+ return au_di(dentry)->di_bdiropq;
9526+}
9527+
9528+/* todo: hard/soft set? */
5afbbe0d 9529+static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9530+{
1308ab2a 9531+ DiMustWriteLock(dentry);
5afbbe0d 9532+ au_di(dentry)->di_btop = bindex;
1facf9fc 9533+}
9534+
5afbbe0d 9535+static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9536+{
1308ab2a 9537+ DiMustWriteLock(dentry);
5afbbe0d 9538+ au_di(dentry)->di_bbot = bindex;
1facf9fc 9539+}
9540+
9541+static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9542+{
1308ab2a 9543+ DiMustWriteLock(dentry);
5afbbe0d 9544+ /* dbwh can be outside of btop - bbot range */
1facf9fc 9545+ au_di(dentry)->di_bwh = bindex;
9546+}
9547+
9548+static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9549+{
1308ab2a 9550+ DiMustWriteLock(dentry);
1facf9fc 9551+ au_di(dentry)->di_bdiropq = bindex;
9552+}
9553+
9554+/* ---------------------------------------------------------------------- */
9555+
4a4d8108 9556+#ifdef CONFIG_AUFS_HNOTIFY
1facf9fc 9557+static inline void au_digen_dec(struct dentry *d)
9558+{
e49829fe 9559+ atomic_dec(&au_di(d)->di_generation);
1facf9fc 9560+}
9561+
4a4d8108 9562+static inline void au_hn_di_reinit(struct dentry *dentry)
1facf9fc 9563+{
9564+ dentry->d_fsdata = NULL;
9565+}
9566+#else
4a4d8108
AM
9567+AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9568+#endif /* CONFIG_AUFS_HNOTIFY */
1facf9fc 9569+
9570+#endif /* __KERNEL__ */
9571+#endif /* __AUFS_DENTRY_H__ */
7f207e10
AM
9572diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9573--- /usr/share/empty/fs/aufs/dinfo.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 9574+++ linux/fs/aufs/dinfo.c 2019-01-28 14:36:12.219084659 +0100
062440b3 9575@@ -0,0 +1,554 @@
cd7a4cd9 9576+// SPDX-License-Identifier: GPL-2.0
1facf9fc 9577+/*
b00004a5 9578+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 9579+ *
9580+ * This program, aufs is free software; you can redistribute it and/or modify
9581+ * it under the terms of the GNU General Public License as published by
9582+ * the Free Software Foundation; either version 2 of the License, or
9583+ * (at your option) any later version.
dece6358
AM
9584+ *
9585+ * This program is distributed in the hope that it will be useful,
9586+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9587+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9588+ * GNU General Public License for more details.
9589+ *
9590+ * You should have received a copy of the GNU General Public License
523b37e3 9591+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9592+ */
9593+
9594+/*
9595+ * dentry private data
9596+ */
9597+
9598+#include "aufs.h"
9599+
e49829fe 9600+void au_di_init_once(void *_dinfo)
4a4d8108 9601+{
e49829fe 9602+ struct au_dinfo *dinfo = _dinfo;
4a4d8108 9603+
e49829fe 9604+ au_rw_init(&dinfo->di_rwsem);
4a4d8108
AM
9605+}
9606+
027c5e7a 9607+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
1facf9fc 9608+{
9609+ struct au_dinfo *dinfo;
027c5e7a 9610+ int nbr, i;
1facf9fc 9611+
9612+ dinfo = au_cache_alloc_dinfo();
9613+ if (unlikely(!dinfo))
9614+ goto out;
9615+
5afbbe0d 9616+ nbr = au_sbbot(sb) + 1;
1facf9fc 9617+ if (nbr <= 0)
9618+ nbr = 1;
9619+ dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
027c5e7a
AM
9620+ if (dinfo->di_hdentry) {
9621+ au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
5afbbe0d
AM
9622+ dinfo->di_btop = -1;
9623+ dinfo->di_bbot = -1;
027c5e7a
AM
9624+ dinfo->di_bwh = -1;
9625+ dinfo->di_bdiropq = -1;
38d290e6 9626+ dinfo->di_tmpfile = 0;
027c5e7a
AM
9627+ for (i = 0; i < nbr; i++)
9628+ dinfo->di_hdentry[i].hd_id = -1;
9629+ goto out;
9630+ }
1facf9fc 9631+
1c60b727 9632+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9633+ dinfo = NULL;
9634+
4f0767ce 9635+out:
027c5e7a 9636+ return dinfo;
1facf9fc 9637+}
9638+
027c5e7a 9639+void au_di_free(struct au_dinfo *dinfo)
4a4d8108 9640+{
4a4d8108 9641+ struct au_hdentry *p;
5afbbe0d 9642+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
9643+
9644+ /* dentry may not be revalidated */
5afbbe0d 9645+ bindex = dinfo->di_btop;
4a4d8108 9646+ if (bindex >= 0) {
5afbbe0d
AM
9647+ bbot = dinfo->di_bbot;
9648+ p = au_hdentry(dinfo, bindex);
9649+ while (bindex++ <= bbot)
4a4d8108
AM
9650+ au_hdput(p++);
9651+ }
e49925d1 9652+ au_kfree_try_rcu(dinfo->di_hdentry);
1c60b727 9653+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9654+}
9655+
9656+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9657+{
9658+ struct au_hdentry *p;
9659+ aufs_bindex_t bi;
9660+
9661+ AuRwMustWriteLock(&a->di_rwsem);
9662+ AuRwMustWriteLock(&b->di_rwsem);
9663+
9664+#define DiSwap(v, name) \
9665+ do { \
9666+ v = a->di_##name; \
9667+ a->di_##name = b->di_##name; \
9668+ b->di_##name = v; \
9669+ } while (0)
9670+
9671+ DiSwap(p, hdentry);
5afbbe0d
AM
9672+ DiSwap(bi, btop);
9673+ DiSwap(bi, bbot);
027c5e7a
AM
9674+ DiSwap(bi, bwh);
9675+ DiSwap(bi, bdiropq);
9676+ /* smp_mb(); */
9677+
9678+#undef DiSwap
9679+}
9680+
9681+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9682+{
9683+ AuRwMustWriteLock(&dst->di_rwsem);
9684+ AuRwMustWriteLock(&src->di_rwsem);
9685+
5afbbe0d
AM
9686+ dst->di_btop = src->di_btop;
9687+ dst->di_bbot = src->di_bbot;
027c5e7a
AM
9688+ dst->di_bwh = src->di_bwh;
9689+ dst->di_bdiropq = src->di_bdiropq;
9690+ /* smp_mb(); */
9691+}
9692+
9693+int au_di_init(struct dentry *dentry)
9694+{
9695+ int err;
9696+ struct super_block *sb;
9697+ struct au_dinfo *dinfo;
9698+
9699+ err = 0;
9700+ sb = dentry->d_sb;
9701+ dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9702+ if (dinfo) {
9703+ atomic_set(&dinfo->di_generation, au_sigen(sb));
9704+ /* smp_mb(); */ /* atomic_set */
9705+ dentry->d_fsdata = dinfo;
9706+ } else
9707+ err = -ENOMEM;
9708+
9709+ return err;
9710+}
9711+
9712+void au_di_fin(struct dentry *dentry)
9713+{
9714+ struct au_dinfo *dinfo;
9715+
9716+ dinfo = au_di(dentry);
9717+ AuRwDestroy(&dinfo->di_rwsem);
9718+ au_di_free(dinfo);
4a4d8108
AM
9719+}
9720+
e2f27e51 9721+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
1facf9fc 9722+{
9723+ int err, sz;
9724+ struct au_hdentry *hdp;
9725+
1308ab2a 9726+ AuRwMustWriteLock(&dinfo->di_rwsem);
9727+
1facf9fc 9728+ err = -ENOMEM;
5afbbe0d 9729+ sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
1facf9fc 9730+ if (!sz)
9731+ sz = sizeof(*hdp);
e2f27e51
AM
9732+ hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9733+ may_shrink);
1facf9fc 9734+ if (hdp) {
9735+ dinfo->di_hdentry = hdp;
9736+ err = 0;
9737+ }
9738+
9739+ return err;
9740+}
9741+
9742+/* ---------------------------------------------------------------------- */
9743+
9744+static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9745+{
9746+ switch (lsc) {
9747+ case AuLsc_DI_CHILD:
9748+ ii_write_lock_child(inode);
9749+ break;
9750+ case AuLsc_DI_CHILD2:
9751+ ii_write_lock_child2(inode);
9752+ break;
9753+ case AuLsc_DI_CHILD3:
9754+ ii_write_lock_child3(inode);
9755+ break;
9756+ case AuLsc_DI_PARENT:
9757+ ii_write_lock_parent(inode);
9758+ break;
9759+ case AuLsc_DI_PARENT2:
9760+ ii_write_lock_parent2(inode);
9761+ break;
9762+ case AuLsc_DI_PARENT3:
9763+ ii_write_lock_parent3(inode);
9764+ break;
9765+ default:
9766+ BUG();
9767+ }
9768+}
9769+
9770+static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9771+{
9772+ switch (lsc) {
9773+ case AuLsc_DI_CHILD:
9774+ ii_read_lock_child(inode);
9775+ break;
9776+ case AuLsc_DI_CHILD2:
9777+ ii_read_lock_child2(inode);
9778+ break;
9779+ case AuLsc_DI_CHILD3:
9780+ ii_read_lock_child3(inode);
9781+ break;
9782+ case AuLsc_DI_PARENT:
9783+ ii_read_lock_parent(inode);
9784+ break;
9785+ case AuLsc_DI_PARENT2:
9786+ ii_read_lock_parent2(inode);
9787+ break;
9788+ case AuLsc_DI_PARENT3:
9789+ ii_read_lock_parent3(inode);
9790+ break;
9791+ default:
9792+ BUG();
9793+ }
9794+}
9795+
9796+void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9797+{
5527c038
JR
9798+ struct inode *inode;
9799+
dece6358 9800+ au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9801+ if (d_really_is_positive(d)) {
9802+ inode = d_inode(d);
1facf9fc 9803+ if (au_ftest_lock(flags, IW))
5527c038 9804+ do_ii_write_lock(inode, lsc);
1facf9fc 9805+ else if (au_ftest_lock(flags, IR))
5527c038 9806+ do_ii_read_lock(inode, lsc);
1facf9fc 9807+ }
9808+}
9809+
9810+void di_read_unlock(struct dentry *d, int flags)
9811+{
5527c038
JR
9812+ struct inode *inode;
9813+
9814+ if (d_really_is_positive(d)) {
9815+ inode = d_inode(d);
027c5e7a
AM
9816+ if (au_ftest_lock(flags, IW)) {
9817+ au_dbg_verify_dinode(d);
5527c038 9818+ ii_write_unlock(inode);
027c5e7a
AM
9819+ } else if (au_ftest_lock(flags, IR)) {
9820+ au_dbg_verify_dinode(d);
5527c038 9821+ ii_read_unlock(inode);
027c5e7a 9822+ }
1facf9fc 9823+ }
dece6358 9824+ au_rw_read_unlock(&au_di(d)->di_rwsem);
1facf9fc 9825+}
9826+
9827+void di_downgrade_lock(struct dentry *d, int flags)
9828+{
5527c038
JR
9829+ if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9830+ ii_downgrade_lock(d_inode(d));
dece6358 9831+ au_rw_dgrade_lock(&au_di(d)->di_rwsem);
1facf9fc 9832+}
9833+
9834+void di_write_lock(struct dentry *d, unsigned int lsc)
9835+{
dece6358 9836+ au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9837+ if (d_really_is_positive(d))
9838+ do_ii_write_lock(d_inode(d), lsc);
1facf9fc 9839+}
9840+
9841+void di_write_unlock(struct dentry *d)
9842+{
027c5e7a 9843+ au_dbg_verify_dinode(d);
5527c038
JR
9844+ if (d_really_is_positive(d))
9845+ ii_write_unlock(d_inode(d));
dece6358 9846+ au_rw_write_unlock(&au_di(d)->di_rwsem);
1facf9fc 9847+}
9848+
9849+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9850+{
9851+ AuDebugOn(d1 == d2
5527c038 9852+ || d_inode(d1) == d_inode(d2)
1facf9fc 9853+ || d1->d_sb != d2->d_sb);
9854+
521ced18
JR
9855+ if ((isdir && au_test_subdir(d1, d2))
9856+ || d1 < d2) {
1facf9fc 9857+ di_write_lock_child(d1);
9858+ di_write_lock_child2(d2);
9859+ } else {
1facf9fc 9860+ di_write_lock_child(d2);
9861+ di_write_lock_child2(d1);
9862+ }
9863+}
9864+
9865+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9866+{
9867+ AuDebugOn(d1 == d2
5527c038 9868+ || d_inode(d1) == d_inode(d2)
1facf9fc 9869+ || d1->d_sb != d2->d_sb);
9870+
521ced18
JR
9871+ if ((isdir && au_test_subdir(d1, d2))
9872+ || d1 < d2) {
1facf9fc 9873+ di_write_lock_parent(d1);
9874+ di_write_lock_parent2(d2);
9875+ } else {
1facf9fc 9876+ di_write_lock_parent(d2);
9877+ di_write_lock_parent2(d1);
9878+ }
9879+}
9880+
9881+void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9882+{
9883+ di_write_unlock(d1);
5527c038 9884+ if (d_inode(d1) == d_inode(d2))
dece6358 9885+ au_rw_write_unlock(&au_di(d2)->di_rwsem);
1facf9fc 9886+ else
9887+ di_write_unlock(d2);
9888+}
9889+
9890+/* ---------------------------------------------------------------------- */
9891+
9892+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9893+{
9894+ struct dentry *d;
9895+
1308ab2a 9896+ DiMustAnyLock(dentry);
9897+
5afbbe0d 9898+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
1facf9fc 9899+ return NULL;
9900+ AuDebugOn(bindex < 0);
5afbbe0d 9901+ d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
c1595e42 9902+ AuDebugOn(d && au_dcount(d) <= 0);
1facf9fc 9903+ return d;
9904+}
9905+
2cbb1c4b
JR
9906+/*
9907+ * extended version of au_h_dptr().
38d290e6
JR
9908+ * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9909+ * error.
2cbb1c4b
JR
9910+ */
9911+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9912+{
9913+ struct dentry *h_dentry;
9914+ struct inode *inode, *h_inode;
9915+
5527c038 9916+ AuDebugOn(d_really_is_negative(dentry));
2cbb1c4b
JR
9917+
9918+ h_dentry = NULL;
5afbbe0d
AM
9919+ if (au_dbtop(dentry) <= bindex
9920+ && bindex <= au_dbbot(dentry))
2cbb1c4b 9921+ h_dentry = au_h_dptr(dentry, bindex);
38d290e6 9922+ if (h_dentry && !au_d_linkable(h_dentry)) {
2cbb1c4b
JR
9923+ dget(h_dentry);
9924+ goto out; /* success */
9925+ }
9926+
5527c038 9927+ inode = d_inode(dentry);
5afbbe0d
AM
9928+ AuDebugOn(bindex < au_ibtop(inode));
9929+ AuDebugOn(au_ibbot(inode) < bindex);
2cbb1c4b
JR
9930+ h_inode = au_h_iptr(inode, bindex);
9931+ h_dentry = d_find_alias(h_inode);
9932+ if (h_dentry) {
9933+ if (!IS_ERR(h_dentry)) {
38d290e6 9934+ if (!au_d_linkable(h_dentry))
2cbb1c4b
JR
9935+ goto out; /* success */
9936+ dput(h_dentry);
9937+ } else
9938+ goto out;
9939+ }
9940+
9941+ if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9942+ h_dentry = au_plink_lkup(inode, bindex);
9943+ AuDebugOn(!h_dentry);
9944+ if (!IS_ERR(h_dentry)) {
9945+ if (!au_d_hashed_positive(h_dentry))
9946+ goto out; /* success */
9947+ dput(h_dentry);
9948+ h_dentry = NULL;
9949+ }
9950+ }
9951+
9952+out:
9953+ AuDbgDentry(h_dentry);
9954+ return h_dentry;
9955+}
9956+
1facf9fc 9957+aufs_bindex_t au_dbtail(struct dentry *dentry)
9958+{
5afbbe0d 9959+ aufs_bindex_t bbot, bwh;
1facf9fc 9960+
5afbbe0d
AM
9961+ bbot = au_dbbot(dentry);
9962+ if (0 <= bbot) {
1facf9fc 9963+ bwh = au_dbwh(dentry);
9964+ if (!bwh)
9965+ return bwh;
5afbbe0d 9966+ if (0 < bwh && bwh < bbot)
1facf9fc 9967+ return bwh - 1;
9968+ }
5afbbe0d 9969+ return bbot;
1facf9fc 9970+}
9971+
9972+aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9973+{
5afbbe0d 9974+ aufs_bindex_t bbot, bopq;
1facf9fc 9975+
5afbbe0d
AM
9976+ bbot = au_dbtail(dentry);
9977+ if (0 <= bbot) {
1facf9fc 9978+ bopq = au_dbdiropq(dentry);
5afbbe0d
AM
9979+ if (0 <= bopq && bopq < bbot)
9980+ bbot = bopq;
1facf9fc 9981+ }
5afbbe0d 9982+ return bbot;
1facf9fc 9983+}
9984+
9985+/* ---------------------------------------------------------------------- */
9986+
9987+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9988+ struct dentry *h_dentry)
9989+{
5afbbe0d
AM
9990+ struct au_dinfo *dinfo;
9991+ struct au_hdentry *hd;
027c5e7a 9992+ struct au_branch *br;
1facf9fc 9993+
1308ab2a 9994+ DiMustWriteLock(dentry);
9995+
5afbbe0d
AM
9996+ dinfo = au_di(dentry);
9997+ hd = au_hdentry(dinfo, bindex);
4a4d8108 9998+ au_hdput(hd);
1facf9fc 9999+ hd->hd_dentry = h_dentry;
027c5e7a
AM
10000+ if (h_dentry) {
10001+ br = au_sbr(dentry->d_sb, bindex);
10002+ hd->hd_id = br->br_id;
10003+ }
10004+}
10005+
10006+int au_dbrange_test(struct dentry *dentry)
10007+{
10008+ int err;
5afbbe0d 10009+ aufs_bindex_t btop, bbot;
027c5e7a
AM
10010+
10011+ err = 0;
5afbbe0d
AM
10012+ btop = au_dbtop(dentry);
10013+ bbot = au_dbbot(dentry);
10014+ if (btop >= 0)
10015+ AuDebugOn(bbot < 0 && btop > bbot);
027c5e7a
AM
10016+ else {
10017+ err = -EIO;
5afbbe0d 10018+ AuDebugOn(bbot >= 0);
027c5e7a
AM
10019+ }
10020+
10021+ return err;
10022+}
10023+
10024+int au_digen_test(struct dentry *dentry, unsigned int sigen)
10025+{
10026+ int err;
10027+
10028+ err = 0;
10029+ if (unlikely(au_digen(dentry) != sigen
5527c038 10030+ || au_iigen_test(d_inode(dentry), sigen)))
027c5e7a
AM
10031+ err = -EIO;
10032+
10033+ return err;
1facf9fc 10034+}
10035+
10036+void au_update_digen(struct dentry *dentry)
10037+{
10038+ atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
10039+ /* smp_mb(); */ /* atomic_set */
10040+}
10041+
10042+void au_update_dbrange(struct dentry *dentry, int do_put_zero)
10043+{
10044+ struct au_dinfo *dinfo;
10045+ struct dentry *h_d;
4a4d8108 10046+ struct au_hdentry *hdp;
5afbbe0d 10047+ aufs_bindex_t bindex, bbot;
1facf9fc 10048+
1308ab2a 10049+ DiMustWriteLock(dentry);
10050+
1facf9fc 10051+ dinfo = au_di(dentry);
5afbbe0d 10052+ if (!dinfo || dinfo->di_btop < 0)
1facf9fc 10053+ return;
10054+
10055+ if (do_put_zero) {
5afbbe0d
AM
10056+ bbot = dinfo->di_bbot;
10057+ bindex = dinfo->di_btop;
10058+ hdp = au_hdentry(dinfo, bindex);
10059+ for (; bindex <= bbot; bindex++, hdp++) {
10060+ h_d = hdp->hd_dentry;
5527c038 10061+ if (h_d && d_is_negative(h_d))
1facf9fc 10062+ au_set_h_dptr(dentry, bindex, NULL);
10063+ }
10064+ }
10065+
5afbbe0d
AM
10066+ dinfo->di_btop = 0;
10067+ hdp = au_hdentry(dinfo, dinfo->di_btop);
10068+ for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
10069+ if (hdp->hd_dentry)
1facf9fc 10070+ break;
5afbbe0d
AM
10071+ if (dinfo->di_btop > dinfo->di_bbot) {
10072+ dinfo->di_btop = -1;
10073+ dinfo->di_bbot = -1;
1facf9fc 10074+ return;
10075+ }
10076+
5afbbe0d
AM
10077+ hdp = au_hdentry(dinfo, dinfo->di_bbot);
10078+ for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
10079+ if (hdp->hd_dentry)
1facf9fc 10080+ break;
5afbbe0d 10081+ AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
1facf9fc 10082+}
10083+
5afbbe0d 10084+void au_update_dbtop(struct dentry *dentry)
1facf9fc 10085+{
5afbbe0d 10086+ aufs_bindex_t bindex, bbot;
1facf9fc 10087+ struct dentry *h_dentry;
10088+
5afbbe0d
AM
10089+ bbot = au_dbbot(dentry);
10090+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
1facf9fc 10091+ h_dentry = au_h_dptr(dentry, bindex);
10092+ if (!h_dentry)
10093+ continue;
5527c038 10094+ if (d_is_positive(h_dentry)) {
5afbbe0d 10095+ au_set_dbtop(dentry, bindex);
1facf9fc 10096+ return;
10097+ }
10098+ au_set_h_dptr(dentry, bindex, NULL);
10099+ }
10100+}
10101+
5afbbe0d 10102+void au_update_dbbot(struct dentry *dentry)
1facf9fc 10103+{
5afbbe0d 10104+ aufs_bindex_t bindex, btop;
1facf9fc 10105+ struct dentry *h_dentry;
10106+
5afbbe0d
AM
10107+ btop = au_dbtop(dentry);
10108+ for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
1facf9fc 10109+ h_dentry = au_h_dptr(dentry, bindex);
10110+ if (!h_dentry)
10111+ continue;
5527c038 10112+ if (d_is_positive(h_dentry)) {
5afbbe0d 10113+ au_set_dbbot(dentry, bindex);
1facf9fc 10114+ return;
10115+ }
10116+ au_set_h_dptr(dentry, bindex, NULL);
10117+ }
10118+}
10119+
10120+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
10121+{
5afbbe0d 10122+ aufs_bindex_t bindex, bbot;
1facf9fc 10123+
5afbbe0d
AM
10124+ bbot = au_dbbot(dentry);
10125+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
1facf9fc 10126+ if (au_h_dptr(dentry, bindex) == h_dentry)
10127+ return bindex;
10128+ return -1;
10129+}
7f207e10
AM
10130diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
10131--- /usr/share/empty/fs/aufs/dir.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 10132+++ linux/fs/aufs/dir.c 2019-01-28 14:36:12.219084659 +0100
acd2b654 10133@@ -0,0 +1,762 @@
cd7a4cd9 10134+// SPDX-License-Identifier: GPL-2.0
1facf9fc 10135+/*
b00004a5 10136+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 10137+ *
10138+ * This program, aufs is free software; you can redistribute it and/or modify
10139+ * it under the terms of the GNU General Public License as published by
10140+ * the Free Software Foundation; either version 2 of the License, or
10141+ * (at your option) any later version.
dece6358
AM
10142+ *
10143+ * This program is distributed in the hope that it will be useful,
10144+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10145+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10146+ * GNU General Public License for more details.
10147+ *
10148+ * You should have received a copy of the GNU General Public License
523b37e3 10149+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10150+ */
10151+
10152+/*
10153+ * directory operations
10154+ */
10155+
10156+#include <linux/fs_stack.h>
10157+#include "aufs.h"
10158+
10159+void au_add_nlink(struct inode *dir, struct inode *h_dir)
10160+{
9dbd164d
AM
10161+ unsigned int nlink;
10162+
1facf9fc 10163+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10164+
9dbd164d
AM
10165+ nlink = dir->i_nlink;
10166+ nlink += h_dir->i_nlink - 2;
1facf9fc 10167+ if (h_dir->i_nlink < 2)
9dbd164d 10168+ nlink += 2;
f6b6e03d 10169+ smp_mb(); /* for i_nlink */
7eafdf33 10170+ /* 0 can happen in revaliding */
92d182d2 10171+ set_nlink(dir, nlink);
1facf9fc 10172+}
10173+
10174+void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10175+{
9dbd164d
AM
10176+ unsigned int nlink;
10177+
1facf9fc 10178+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10179+
9dbd164d
AM
10180+ nlink = dir->i_nlink;
10181+ nlink -= h_dir->i_nlink - 2;
1facf9fc 10182+ if (h_dir->i_nlink < 2)
9dbd164d 10183+ nlink -= 2;
f6b6e03d 10184+ smp_mb(); /* for i_nlink */
92d182d2 10185+ /* nlink == 0 means the branch-fs is broken */
9dbd164d 10186+ set_nlink(dir, nlink);
1facf9fc 10187+}
10188+
1308ab2a 10189+loff_t au_dir_size(struct file *file, struct dentry *dentry)
10190+{
10191+ loff_t sz;
5afbbe0d 10192+ aufs_bindex_t bindex, bbot;
1308ab2a 10193+ struct file *h_file;
10194+ struct dentry *h_dentry;
10195+
10196+ sz = 0;
10197+ if (file) {
2000de60 10198+ AuDebugOn(!d_is_dir(file->f_path.dentry));
1308ab2a 10199+
5afbbe0d
AM
10200+ bbot = au_fbbot_dir(file);
10201+ for (bindex = au_fbtop(file);
10202+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 10203+ bindex++) {
4a4d8108 10204+ h_file = au_hf_dir(file, bindex);
c06a8ce3
AM
10205+ if (h_file && file_inode(h_file))
10206+ sz += vfsub_f_size_read(h_file);
1308ab2a 10207+ }
10208+ } else {
10209+ AuDebugOn(!dentry);
2000de60 10210+ AuDebugOn(!d_is_dir(dentry));
1308ab2a 10211+
5afbbe0d
AM
10212+ bbot = au_dbtaildir(dentry);
10213+ for (bindex = au_dbtop(dentry);
10214+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 10215+ bindex++) {
10216+ h_dentry = au_h_dptr(dentry, bindex);
5527c038
JR
10217+ if (h_dentry && d_is_positive(h_dentry))
10218+ sz += i_size_read(d_inode(h_dentry));
1308ab2a 10219+ }
10220+ }
10221+ if (sz < KMALLOC_MAX_SIZE)
10222+ sz = roundup_pow_of_two(sz);
10223+ if (sz > KMALLOC_MAX_SIZE)
10224+ sz = KMALLOC_MAX_SIZE;
10225+ else if (sz < NAME_MAX) {
10226+ BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10227+ sz = AUFS_RDBLK_DEF;
10228+ }
10229+ return sz;
10230+}
10231+
b912730e
AM
10232+struct au_dir_ts_arg {
10233+ struct dentry *dentry;
10234+ aufs_bindex_t brid;
10235+};
10236+
10237+static void au_do_dir_ts(void *arg)
10238+{
10239+ struct au_dir_ts_arg *a = arg;
10240+ struct au_dtime dt;
10241+ struct path h_path;
10242+ struct inode *dir, *h_dir;
10243+ struct super_block *sb;
10244+ struct au_branch *br;
10245+ struct au_hinode *hdir;
10246+ int err;
5afbbe0d 10247+ aufs_bindex_t btop, bindex;
b912730e
AM
10248+
10249+ sb = a->dentry->d_sb;
5527c038 10250+ if (d_really_is_negative(a->dentry))
b912730e 10251+ goto out;
5527c038 10252+ /* no dir->i_mutex lock */
b95c5147
AM
10253+ aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10254+
5527c038 10255+ dir = d_inode(a->dentry);
5afbbe0d 10256+ btop = au_ibtop(dir);
b912730e 10257+ bindex = au_br_index(sb, a->brid);
5afbbe0d 10258+ if (bindex < btop)
b912730e
AM
10259+ goto out_unlock;
10260+
10261+ br = au_sbr(sb, bindex);
10262+ h_path.dentry = au_h_dptr(a->dentry, bindex);
10263+ if (!h_path.dentry)
10264+ goto out_unlock;
10265+ h_path.mnt = au_br_mnt(br);
10266+ au_dtime_store(&dt, a->dentry, &h_path);
10267+
5afbbe0d 10268+ br = au_sbr(sb, btop);
b912730e
AM
10269+ if (!au_br_writable(br->br_perm))
10270+ goto out_unlock;
5afbbe0d 10271+ h_path.dentry = au_h_dptr(a->dentry, btop);
b912730e
AM
10272+ h_path.mnt = au_br_mnt(br);
10273+ err = vfsub_mnt_want_write(h_path.mnt);
10274+ if (err)
10275+ goto out_unlock;
5afbbe0d
AM
10276+ hdir = au_hi(dir, btop);
10277+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10278+ h_dir = au_h_iptr(dir, btop);
b912730e 10279+ if (h_dir->i_nlink
cd7a4cd9 10280+ && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
b912730e
AM
10281+ dt.dt_h_path = h_path;
10282+ au_dtime_revert(&dt);
10283+ }
5afbbe0d 10284+ au_hn_inode_unlock(hdir);
b912730e
AM
10285+ vfsub_mnt_drop_write(h_path.mnt);
10286+ au_cpup_attr_timesizes(dir);
10287+
10288+out_unlock:
10289+ aufs_read_unlock(a->dentry, AuLock_DW);
10290+out:
10291+ dput(a->dentry);
10292+ au_nwt_done(&au_sbi(sb)->si_nowait);
e49925d1 10293+ au_kfree_try_rcu(arg);
b912730e
AM
10294+}
10295+
10296+void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10297+{
10298+ int perm, wkq_err;
5afbbe0d 10299+ aufs_bindex_t btop;
b912730e
AM
10300+ struct au_dir_ts_arg *arg;
10301+ struct dentry *dentry;
10302+ struct super_block *sb;
10303+
10304+ IMustLock(dir);
10305+
10306+ dentry = d_find_any_alias(dir);
10307+ AuDebugOn(!dentry);
10308+ sb = dentry->d_sb;
5afbbe0d
AM
10309+ btop = au_ibtop(dir);
10310+ if (btop == bindex) {
b912730e
AM
10311+ au_cpup_attr_timesizes(dir);
10312+ goto out;
10313+ }
10314+
5afbbe0d 10315+ perm = au_sbr_perm(sb, btop);
b912730e
AM
10316+ if (!au_br_writable(perm))
10317+ goto out;
10318+
10319+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
10320+ if (!arg)
10321+ goto out;
10322+
10323+ arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10324+ arg->brid = au_sbr_id(sb, bindex);
10325+ wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10326+ if (unlikely(wkq_err)) {
10327+ pr_err("wkq %d\n", wkq_err);
10328+ dput(dentry);
e49925d1 10329+ au_kfree_try_rcu(arg);
b912730e
AM
10330+ }
10331+
10332+out:
10333+ dput(dentry);
10334+}
10335+
1facf9fc 10336+/* ---------------------------------------------------------------------- */
10337+
10338+static int reopen_dir(struct file *file)
10339+{
10340+ int err;
10341+ unsigned int flags;
5afbbe0d 10342+ aufs_bindex_t bindex, btail, btop;
1facf9fc 10343+ struct dentry *dentry, *h_dentry;
10344+ struct file *h_file;
10345+
10346+ /* open all lower dirs */
2000de60 10347+ dentry = file->f_path.dentry;
5afbbe0d
AM
10348+ btop = au_dbtop(dentry);
10349+ for (bindex = au_fbtop(file); bindex < btop; bindex++)
1facf9fc 10350+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 10351+ au_set_fbtop(file, btop);
1facf9fc 10352+
10353+ btail = au_dbtaildir(dentry);
5afbbe0d 10354+ for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
1facf9fc 10355+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 10356+ au_set_fbbot_dir(file, btail);
1facf9fc 10357+
4a4d8108 10358+ flags = vfsub_file_flags(file);
5afbbe0d 10359+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 10360+ h_dentry = au_h_dptr(dentry, bindex);
10361+ if (!h_dentry)
10362+ continue;
4a4d8108 10363+ h_file = au_hf_dir(file, bindex);
1facf9fc 10364+ if (h_file)
10365+ continue;
10366+
392086de 10367+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 10368+ err = PTR_ERR(h_file);
10369+ if (IS_ERR(h_file))
10370+ goto out; /* close all? */
10371+ au_set_h_fptr(file, bindex, h_file);
10372+ }
10373+ au_update_figen(file);
10374+ /* todo: necessary? */
10375+ /* file->f_ra = h_file->f_ra; */
10376+ err = 0;
10377+
4f0767ce 10378+out:
1facf9fc 10379+ return err;
10380+}
10381+
b912730e 10382+static int do_open_dir(struct file *file, int flags, struct file *h_file)
1facf9fc 10383+{
10384+ int err;
10385+ aufs_bindex_t bindex, btail;
10386+ struct dentry *dentry, *h_dentry;
8cdd5066 10387+ struct vfsmount *mnt;
1facf9fc 10388+
1308ab2a 10389+ FiMustWriteLock(file);
b912730e 10390+ AuDebugOn(h_file);
1308ab2a 10391+
523b37e3 10392+ err = 0;
8cdd5066 10393+ mnt = file->f_path.mnt;
2000de60 10394+ dentry = file->f_path.dentry;
be118d29 10395+ file->f_version = inode_query_iversion(d_inode(dentry));
5afbbe0d
AM
10396+ bindex = au_dbtop(dentry);
10397+ au_set_fbtop(file, bindex);
1facf9fc 10398+ btail = au_dbtaildir(dentry);
5afbbe0d 10399+ au_set_fbbot_dir(file, btail);
1facf9fc 10400+ for (; !err && bindex <= btail; bindex++) {
10401+ h_dentry = au_h_dptr(dentry, bindex);
10402+ if (!h_dentry)
10403+ continue;
10404+
8cdd5066
JR
10405+ err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10406+ if (unlikely(err))
10407+ break;
392086de 10408+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 10409+ if (IS_ERR(h_file)) {
10410+ err = PTR_ERR(h_file);
10411+ break;
10412+ }
10413+ au_set_h_fptr(file, bindex, h_file);
10414+ }
10415+ au_update_figen(file);
10416+ /* todo: necessary? */
10417+ /* file->f_ra = h_file->f_ra; */
10418+ if (!err)
10419+ return 0; /* success */
10420+
10421+ /* close all */
5afbbe0d 10422+ for (bindex = au_fbtop(file); bindex <= btail; bindex++)
1facf9fc 10423+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d
AM
10424+ au_set_fbtop(file, -1);
10425+ au_set_fbbot_dir(file, -1);
4a4d8108 10426+
1facf9fc 10427+ return err;
10428+}
10429+
10430+static int aufs_open_dir(struct inode *inode __maybe_unused,
10431+ struct file *file)
10432+{
4a4d8108
AM
10433+ int err;
10434+ struct super_block *sb;
10435+ struct au_fidir *fidir;
10436+
10437+ err = -ENOMEM;
2000de60 10438+ sb = file->f_path.dentry->d_sb;
4a4d8108 10439+ si_read_lock(sb, AuLock_FLUSH);
e49829fe 10440+ fidir = au_fidir_alloc(sb);
4a4d8108 10441+ if (fidir) {
b912730e
AM
10442+ struct au_do_open_args args = {
10443+ .open = do_open_dir,
10444+ .fidir = fidir
10445+ };
10446+ err = au_do_open(file, &args);
4a4d8108 10447+ if (unlikely(err))
e49925d1 10448+ au_kfree_rcu(fidir);
4a4d8108
AM
10449+ }
10450+ si_read_unlock(sb);
10451+ return err;
1facf9fc 10452+}
10453+
10454+static int aufs_release_dir(struct inode *inode __maybe_unused,
10455+ struct file *file)
10456+{
10457+ struct au_vdir *vdir_cache;
4a4d8108
AM
10458+ struct au_finfo *finfo;
10459+ struct au_fidir *fidir;
f0c0a007 10460+ struct au_hfile *hf;
5afbbe0d 10461+ aufs_bindex_t bindex, bbot;
1facf9fc 10462+
4a4d8108
AM
10463+ finfo = au_fi(file);
10464+ fidir = finfo->fi_hdir;
10465+ if (fidir) {
8b6a4947
AM
10466+ au_hbl_del(&finfo->fi_hlist,
10467+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108
AM
10468+ vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10469+ if (vdir_cache)
1c60b727 10470+ au_vdir_free(vdir_cache);
4a4d8108
AM
10471+
10472+ bindex = finfo->fi_btop;
10473+ if (bindex >= 0) {
f0c0a007 10474+ hf = fidir->fd_hfile + bindex;
4a4d8108
AM
10475+ /*
10476+ * calls fput() instead of filp_close(),
10477+ * since no dnotify or lock for the lower file.
10478+ */
5afbbe0d 10479+ bbot = fidir->fd_bbot;
f0c0a007
AM
10480+ for (; bindex <= bbot; bindex++, hf++)
10481+ if (hf->hf_file)
1c60b727 10482+ au_hfput(hf, /*execed*/0);
4a4d8108 10483+ }
e49925d1 10484+ au_kfree_rcu(fidir);
4a4d8108 10485+ finfo->fi_hdir = NULL;
1facf9fc 10486+ }
1c60b727 10487+ au_finfo_fin(file);
1facf9fc 10488+ return 0;
10489+}
10490+
10491+/* ---------------------------------------------------------------------- */
10492+
4a4d8108
AM
10493+static int au_do_flush_dir(struct file *file, fl_owner_t id)
10494+{
10495+ int err;
5afbbe0d 10496+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
10497+ struct file *h_file;
10498+
10499+ err = 0;
5afbbe0d
AM
10500+ bbot = au_fbbot_dir(file);
10501+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
10502+ h_file = au_hf_dir(file, bindex);
10503+ if (h_file)
10504+ err = vfsub_flush(h_file, id);
10505+ }
10506+ return err;
10507+}
10508+
10509+static int aufs_flush_dir(struct file *file, fl_owner_t id)
10510+{
10511+ return au_do_flush(file, id, au_do_flush_dir);
10512+}
10513+
10514+/* ---------------------------------------------------------------------- */
10515+
1facf9fc 10516+static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10517+{
10518+ int err;
5afbbe0d 10519+ aufs_bindex_t bbot, bindex;
1facf9fc 10520+ struct inode *inode;
10521+ struct super_block *sb;
10522+
10523+ err = 0;
10524+ sb = dentry->d_sb;
5527c038 10525+ inode = d_inode(dentry);
1facf9fc 10526+ IMustLock(inode);
5afbbe0d
AM
10527+ bbot = au_dbbot(dentry);
10528+ for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
1facf9fc 10529+ struct path h_path;
1facf9fc 10530+
10531+ if (au_test_ro(sb, bindex, inode))
10532+ continue;
10533+ h_path.dentry = au_h_dptr(dentry, bindex);
10534+ if (!h_path.dentry)
10535+ continue;
1facf9fc 10536+
1facf9fc 10537+ h_path.mnt = au_sbr_mnt(sb, bindex);
53392da6 10538+ err = vfsub_fsync(NULL, &h_path, datasync);
1facf9fc 10539+ }
10540+
10541+ return err;
10542+}
10543+
10544+static int au_do_fsync_dir(struct file *file, int datasync)
10545+{
10546+ int err;
5afbbe0d 10547+ aufs_bindex_t bbot, bindex;
1facf9fc 10548+ struct file *h_file;
10549+ struct super_block *sb;
10550+ struct inode *inode;
1facf9fc 10551+
521ced18 10552+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
1facf9fc 10553+ if (unlikely(err))
10554+ goto out;
10555+
c06a8ce3 10556+ inode = file_inode(file);
b912730e 10557+ sb = inode->i_sb;
5afbbe0d
AM
10558+ bbot = au_fbbot_dir(file);
10559+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108 10560+ h_file = au_hf_dir(file, bindex);
1facf9fc 10561+ if (!h_file || au_test_ro(sb, bindex, inode))
10562+ continue;
10563+
53392da6 10564+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
1facf9fc 10565+ }
10566+
4f0767ce 10567+out:
1facf9fc 10568+ return err;
10569+}
10570+
10571+/*
10572+ * @file may be NULL
10573+ */
1e00d052
AM
10574+static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10575+ int datasync)
1facf9fc 10576+{
10577+ int err;
b752ccd1 10578+ struct dentry *dentry;
5527c038 10579+ struct inode *inode;
1facf9fc 10580+ struct super_block *sb;
1facf9fc 10581+
10582+ err = 0;
2000de60 10583+ dentry = file->f_path.dentry;
5527c038 10584+ inode = d_inode(dentry);
febd17d6 10585+ inode_lock(inode);
1facf9fc 10586+ sb = dentry->d_sb;
10587+ si_noflush_read_lock(sb);
10588+ if (file)
10589+ err = au_do_fsync_dir(file, datasync);
10590+ else {
10591+ di_write_lock_child(dentry);
10592+ err = au_do_fsync_dir_no_file(dentry, datasync);
10593+ }
5527c038 10594+ au_cpup_attr_timesizes(inode);
1facf9fc 10595+ di_write_unlock(dentry);
10596+ if (file)
10597+ fi_write_unlock(file);
10598+
10599+ si_read_unlock(sb);
febd17d6 10600+ inode_unlock(inode);
1facf9fc 10601+ return err;
10602+}
10603+
10604+/* ---------------------------------------------------------------------- */
10605+
5afbbe0d 10606+static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
1facf9fc 10607+{
10608+ int err;
10609+ struct dentry *dentry;
9dbd164d 10610+ struct inode *inode, *h_inode;
1facf9fc 10611+ struct super_block *sb;
10612+
062440b3 10613+ AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
392086de 10614+
2000de60 10615+ dentry = file->f_path.dentry;
5527c038 10616+ inode = d_inode(dentry);
1facf9fc 10617+ IMustLock(inode);
10618+
10619+ sb = dentry->d_sb;
10620+ si_read_lock(sb, AuLock_FLUSH);
521ced18 10621+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
1facf9fc 10622+ if (unlikely(err))
10623+ goto out;
027c5e7a
AM
10624+ err = au_alive_dir(dentry);
10625+ if (!err)
10626+ err = au_vdir_init(file);
1facf9fc 10627+ di_downgrade_lock(dentry, AuLock_IR);
10628+ if (unlikely(err))
10629+ goto out_unlock;
10630+
5afbbe0d 10631+ h_inode = au_h_iptr(inode, au_ibtop(inode));
b752ccd1 10632+ if (!au_test_nfsd()) {
392086de 10633+ err = au_vdir_fill_de(file, ctx);
9dbd164d 10634+ fsstack_copy_attr_atime(inode, h_inode);
1facf9fc 10635+ } else {
10636+ /*
10637+ * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10638+ * encode_fh() and others.
10639+ */
9dbd164d 10640+ atomic_inc(&h_inode->i_count);
1facf9fc 10641+ di_read_unlock(dentry, AuLock_IR);
10642+ si_read_unlock(sb);
392086de 10643+ err = au_vdir_fill_de(file, ctx);
1facf9fc 10644+ fsstack_copy_attr_atime(inode, h_inode);
10645+ fi_write_unlock(file);
9dbd164d 10646+ iput(h_inode);
1facf9fc 10647+
10648+ AuTraceErr(err);
10649+ return err;
10650+ }
10651+
4f0767ce 10652+out_unlock:
1facf9fc 10653+ di_read_unlock(dentry, AuLock_IR);
10654+ fi_write_unlock(file);
4f0767ce 10655+out:
1facf9fc 10656+ si_read_unlock(sb);
10657+ return err;
10658+}
10659+
10660+/* ---------------------------------------------------------------------- */
10661+
10662+#define AuTestEmpty_WHONLY 1
dece6358
AM
10663+#define AuTestEmpty_CALLED (1 << 1)
10664+#define AuTestEmpty_SHWH (1 << 2)
1facf9fc 10665+#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
7f207e10
AM
10666+#define au_fset_testempty(flags, name) \
10667+ do { (flags) |= AuTestEmpty_##name; } while (0)
10668+#define au_fclr_testempty(flags, name) \
10669+ do { (flags) &= ~AuTestEmpty_##name; } while (0)
1facf9fc 10670+
dece6358
AM
10671+#ifndef CONFIG_AUFS_SHWH
10672+#undef AuTestEmpty_SHWH
10673+#define AuTestEmpty_SHWH 0
10674+#endif
10675+
1facf9fc 10676+struct test_empty_arg {
392086de 10677+ struct dir_context ctx;
1308ab2a 10678+ struct au_nhash *whlist;
1facf9fc 10679+ unsigned int flags;
10680+ int err;
10681+ aufs_bindex_t bindex;
10682+};
10683+
392086de
AM
10684+static int test_empty_cb(struct dir_context *ctx, const char *__name,
10685+ int namelen, loff_t offset __maybe_unused, u64 ino,
dece6358 10686+ unsigned int d_type)
1facf9fc 10687+{
392086de
AM
10688+ struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10689+ ctx);
1facf9fc 10690+ char *name = (void *)__name;
10691+
10692+ arg->err = 0;
10693+ au_fset_testempty(arg->flags, CALLED);
10694+ /* smp_mb(); */
10695+ if (name[0] == '.'
10696+ && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10697+ goto out; /* success */
10698+
10699+ if (namelen <= AUFS_WH_PFX_LEN
10700+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10701+ if (au_ftest_testempty(arg->flags, WHONLY)
1308ab2a 10702+ && !au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10703+ arg->err = -ENOTEMPTY;
10704+ goto out;
10705+ }
10706+
10707+ name += AUFS_WH_PFX_LEN;
10708+ namelen -= AUFS_WH_PFX_LEN;
1308ab2a 10709+ if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10710+ arg->err = au_nhash_append_wh
1308ab2a 10711+ (arg->whlist, name, namelen, ino, d_type, arg->bindex,
dece6358 10712+ au_ftest_testempty(arg->flags, SHWH));
1facf9fc 10713+
4f0767ce 10714+out:
1facf9fc 10715+ /* smp_mb(); */
10716+ AuTraceErr(arg->err);
10717+ return arg->err;
10718+}
10719+
10720+static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10721+{
10722+ int err;
10723+ struct file *h_file;
acd2b654 10724+ struct au_branch *br;
1facf9fc 10725+
10726+ h_file = au_h_open(dentry, arg->bindex,
10727+ O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
392086de 10728+ /*file*/NULL, /*force_wr*/0);
1facf9fc 10729+ err = PTR_ERR(h_file);
10730+ if (IS_ERR(h_file))
10731+ goto out;
10732+
10733+ err = 0;
10734+ if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
c06a8ce3 10735+ && !file_inode(h_file)->i_nlink)
1facf9fc 10736+ goto out_put;
10737+
10738+ do {
10739+ arg->err = 0;
10740+ au_fclr_testempty(arg->flags, CALLED);
10741+ /* smp_mb(); */
392086de 10742+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1facf9fc 10743+ if (err >= 0)
10744+ err = arg->err;
10745+ } while (!err && au_ftest_testempty(arg->flags, CALLED));
10746+
4f0767ce 10747+out_put:
1facf9fc 10748+ fput(h_file);
acd2b654
AM
10749+ br = au_sbr(dentry->d_sb, arg->bindex);
10750+ au_lcnt_dec(&br->br_nfiles);
4f0767ce 10751+out:
1facf9fc 10752+ return err;
10753+}
10754+
10755+struct do_test_empty_args {
10756+ int *errp;
10757+ struct dentry *dentry;
10758+ struct test_empty_arg *arg;
10759+};
10760+
10761+static void call_do_test_empty(void *args)
10762+{
10763+ struct do_test_empty_args *a = args;
10764+ *a->errp = do_test_empty(a->dentry, a->arg);
10765+}
10766+
10767+static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10768+{
10769+ int err, wkq_err;
10770+ struct dentry *h_dentry;
10771+ struct inode *h_inode;
10772+
10773+ h_dentry = au_h_dptr(dentry, arg->bindex);
5527c038 10774+ h_inode = d_inode(h_dentry);
53392da6 10775+ /* todo: i_mode changes anytime? */
be118d29 10776+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 10777+ err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
3c1bdaff 10778+ inode_unlock_shared(h_inode);
1facf9fc 10779+ if (!err)
10780+ err = do_test_empty(dentry, arg);
10781+ else {
10782+ struct do_test_empty_args args = {
10783+ .errp = &err,
10784+ .dentry = dentry,
10785+ .arg = arg
10786+ };
10787+ unsigned int flags = arg->flags;
10788+
10789+ wkq_err = au_wkq_wait(call_do_test_empty, &args);
10790+ if (unlikely(wkq_err))
10791+ err = wkq_err;
10792+ arg->flags = flags;
10793+ }
10794+
10795+ return err;
10796+}
10797+
10798+int au_test_empty_lower(struct dentry *dentry)
10799+{
10800+ int err;
1308ab2a 10801+ unsigned int rdhash;
5afbbe0d 10802+ aufs_bindex_t bindex, btop, btail;
1308ab2a 10803+ struct au_nhash whlist;
392086de
AM
10804+ struct test_empty_arg arg = {
10805+ .ctx = {
2000de60 10806+ .actor = test_empty_cb
392086de
AM
10807+ }
10808+ };
076b876e 10809+ int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
1facf9fc 10810+
dece6358
AM
10811+ SiMustAnyLock(dentry->d_sb);
10812+
1308ab2a 10813+ rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10814+ if (!rdhash)
10815+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10816+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
dece6358 10817+ if (unlikely(err))
1facf9fc 10818+ goto out;
10819+
1facf9fc 10820+ arg.flags = 0;
1308ab2a 10821+ arg.whlist = &whlist;
5afbbe0d 10822+ btop = au_dbtop(dentry);
dece6358
AM
10823+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10824+ au_fset_testempty(arg.flags, SHWH);
076b876e
AM
10825+ test_empty = do_test_empty;
10826+ if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10827+ test_empty = sio_test_empty;
5afbbe0d 10828+ arg.bindex = btop;
076b876e 10829+ err = test_empty(dentry, &arg);
1facf9fc 10830+ if (unlikely(err))
10831+ goto out_whlist;
10832+
10833+ au_fset_testempty(arg.flags, WHONLY);
10834+ btail = au_dbtaildir(dentry);
5afbbe0d 10835+ for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
1facf9fc 10836+ struct dentry *h_dentry;
10837+
10838+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10839+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10840+ arg.bindex = bindex;
076b876e 10841+ err = test_empty(dentry, &arg);
1facf9fc 10842+ }
10843+ }
10844+
4f0767ce 10845+out_whlist:
1308ab2a 10846+ au_nhash_wh_free(&whlist);
4f0767ce 10847+out:
1facf9fc 10848+ return err;
10849+}
10850+
10851+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10852+{
10853+ int err;
392086de
AM
10854+ struct test_empty_arg arg = {
10855+ .ctx = {
2000de60 10856+ .actor = test_empty_cb
392086de
AM
10857+ }
10858+ };
1facf9fc 10859+ aufs_bindex_t bindex, btail;
10860+
10861+ err = 0;
1308ab2a 10862+ arg.whlist = whlist;
1facf9fc 10863+ arg.flags = AuTestEmpty_WHONLY;
dece6358
AM
10864+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10865+ au_fset_testempty(arg.flags, SHWH);
1facf9fc 10866+ btail = au_dbtaildir(dentry);
5afbbe0d 10867+ for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
1facf9fc 10868+ struct dentry *h_dentry;
10869+
10870+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10871+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10872+ arg.bindex = bindex;
10873+ err = sio_test_empty(dentry, &arg);
10874+ }
10875+ }
10876+
10877+ return err;
10878+}
10879+
10880+/* ---------------------------------------------------------------------- */
10881+
10882+const struct file_operations aufs_dir_fop = {
4a4d8108 10883+ .owner = THIS_MODULE,
027c5e7a 10884+ .llseek = default_llseek,
1facf9fc 10885+ .read = generic_read_dir,
5afbbe0d 10886+ .iterate_shared = aufs_iterate_shared,
1facf9fc 10887+ .unlocked_ioctl = aufs_ioctl_dir,
b752ccd1
AM
10888+#ifdef CONFIG_COMPAT
10889+ .compat_ioctl = aufs_compat_ioctl_dir,
10890+#endif
1facf9fc 10891+ .open = aufs_open_dir,
10892+ .release = aufs_release_dir,
4a4d8108 10893+ .flush = aufs_flush_dir,
1facf9fc 10894+ .fsync = aufs_fsync_dir
10895+};
7f207e10
AM
10896diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10897--- /usr/share/empty/fs/aufs/dir.h 1970-01-01 01:00:00.000000000 +0100
e49925d1
JR
10898+++ linux/fs/aufs/dir.h 2019-01-28 14:36:12.219084659 +0100
10899@@ -0,0 +1,134 @@
062440b3 10900+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 10901+/*
b00004a5 10902+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 10903+ *
10904+ * This program, aufs is free software; you can redistribute it and/or modify
10905+ * it under the terms of the GNU General Public License as published by
10906+ * the Free Software Foundation; either version 2 of the License, or
10907+ * (at your option) any later version.
dece6358
AM
10908+ *
10909+ * This program is distributed in the hope that it will be useful,
10910+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10911+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10912+ * GNU General Public License for more details.
10913+ *
10914+ * You should have received a copy of the GNU General Public License
523b37e3 10915+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10916+ */
10917+
10918+/*
10919+ * directory operations
10920+ */
10921+
10922+#ifndef __AUFS_DIR_H__
10923+#define __AUFS_DIR_H__
10924+
10925+#ifdef __KERNEL__
10926+
10927+#include <linux/fs.h>
1facf9fc 10928+
10929+/* ---------------------------------------------------------------------- */
10930+
10931+/* need to be faster and smaller */
10932+
10933+struct au_nhash {
dece6358
AM
10934+ unsigned int nh_num;
10935+ struct hlist_head *nh_head;
1facf9fc 10936+};
10937+
10938+struct au_vdir_destr {
10939+ unsigned char len;
10940+ unsigned char name[0];
10941+} __packed;
10942+
10943+struct au_vdir_dehstr {
10944+ struct hlist_node hash;
1c60b727 10945+ struct au_vdir_destr *str;
e49925d1 10946+ struct rcu_head rcu;
4a4d8108 10947+} ____cacheline_aligned_in_smp;
1facf9fc 10948+
10949+struct au_vdir_de {
10950+ ino_t de_ino;
10951+ unsigned char de_type;
10952+ /* caution: packed */
10953+ struct au_vdir_destr de_str;
10954+} __packed;
10955+
10956+struct au_vdir_wh {
10957+ struct hlist_node wh_hash;
dece6358
AM
10958+#ifdef CONFIG_AUFS_SHWH
10959+ ino_t wh_ino;
1facf9fc 10960+ aufs_bindex_t wh_bindex;
dece6358
AM
10961+ unsigned char wh_type;
10962+#else
10963+ aufs_bindex_t wh_bindex;
10964+#endif
10965+ /* caution: packed */
1facf9fc 10966+ struct au_vdir_destr wh_str;
10967+} __packed;
10968+
10969+union au_vdir_deblk_p {
10970+ unsigned char *deblk;
10971+ struct au_vdir_de *de;
10972+};
10973+
10974+struct au_vdir {
10975+ unsigned char **vd_deblk;
10976+ unsigned long vd_nblk;
1facf9fc 10977+ struct {
10978+ unsigned long ul;
10979+ union au_vdir_deblk_p p;
10980+ } vd_last;
10981+
be118d29 10982+ u64 vd_version;
dece6358 10983+ unsigned int vd_deblk_sz;
e49925d1
JR
10984+ unsigned long vd_jiffy;
10985+ struct rcu_head rcu;
4a4d8108 10986+} ____cacheline_aligned_in_smp;
1facf9fc 10987+
10988+/* ---------------------------------------------------------------------- */
10989+
10990+/* dir.c */
10991+extern const struct file_operations aufs_dir_fop;
10992+void au_add_nlink(struct inode *dir, struct inode *h_dir);
10993+void au_sub_nlink(struct inode *dir, struct inode *h_dir);
1308ab2a 10994+loff_t au_dir_size(struct file *file, struct dentry *dentry);
b912730e 10995+void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
1facf9fc 10996+int au_test_empty_lower(struct dentry *dentry);
10997+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10998+
10999+/* vdir.c */
1308ab2a 11000+unsigned int au_rdhash_est(loff_t sz);
dece6358
AM
11001+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
11002+void au_nhash_wh_free(struct au_nhash *whlist);
1facf9fc 11003+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
11004+ int limit);
dece6358
AM
11005+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
11006+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
11007+ unsigned int d_type, aufs_bindex_t bindex,
11008+ unsigned char shwh);
1c60b727 11009+void au_vdir_free(struct au_vdir *vdir);
1facf9fc 11010+int au_vdir_init(struct file *file);
392086de 11011+int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
1facf9fc 11012+
11013+/* ioctl.c */
11014+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
11015+
1308ab2a 11016+#ifdef CONFIG_AUFS_RDU
11017+/* rdu.c */
11018+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
11019+#ifdef CONFIG_COMPAT
11020+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
11021+ unsigned long arg);
11022+#endif
1308ab2a 11023+#else
c1595e42
JR
11024+AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
11025+ unsigned int cmd, unsigned long arg)
b752ccd1 11026+#ifdef CONFIG_COMPAT
c1595e42
JR
11027+AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
11028+ unsigned int cmd, unsigned long arg)
b752ccd1 11029+#endif
1308ab2a 11030+#endif
11031+
1facf9fc 11032+#endif /* __KERNEL__ */
11033+#endif /* __AUFS_DIR_H__ */
8b6a4947
AM
11034diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
11035--- /usr/share/empty/fs/aufs/dirren.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 11036+++ linux/fs/aufs/dirren.c 2019-01-28 14:36:12.219084659 +0100
062440b3 11037@@ -0,0 +1,1316 @@
cd7a4cd9 11038+// SPDX-License-Identifier: GPL-2.0
8b6a4947 11039+/*
b00004a5 11040+ * Copyright (C) 2017-2018 Junjiro R. Okajima
8b6a4947
AM
11041+ *
11042+ * This program, aufs is free software; you can redistribute it and/or modify
11043+ * it under the terms of the GNU General Public License as published by
11044+ * the Free Software Foundation; either version 2 of the License, or
11045+ * (at your option) any later version.
11046+ *
11047+ * This program is distributed in the hope that it will be useful,
11048+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11049+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11050+ * GNU General Public License for more details.
11051+ *
11052+ * You should have received a copy of the GNU General Public License
11053+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
11054+ */
11055+
11056+/*
acd2b654 11057+ * special handling in renaming a directory
8b6a4947
AM
11058+ * in order to support looking-up the before-renamed name on the lower readonly
11059+ * branches
11060+ */
11061+
11062+#include <linux/byteorder/generic.h>
11063+#include "aufs.h"
11064+
11065+static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
11066+{
11067+ int idx;
11068+
11069+ idx = au_dr_ihash(ent->dr_h_ino);
11070+ au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
11071+}
11072+
11073+static int au_dr_hino_test_empty(struct au_dr_br *dr)
11074+{
11075+ int ret, i;
11076+ struct hlist_bl_head *hbl;
11077+
11078+ ret = 1;
11079+ for (i = 0; ret && i < AuDirren_NHASH; i++) {
11080+ hbl = dr->dr_h_ino + i;
11081+ hlist_bl_lock(hbl);
11082+ ret &= hlist_bl_empty(hbl);
11083+ hlist_bl_unlock(hbl);
11084+ }
11085+
11086+ return ret;
11087+}
11088+
11089+static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
11090+{
11091+ struct au_dr_hino *found, *ent;
11092+ struct hlist_bl_head *hbl;
11093+ struct hlist_bl_node *pos;
11094+ int idx;
11095+
11096+ found = NULL;
11097+ idx = au_dr_ihash(ino);
11098+ hbl = dr->dr_h_ino + idx;
11099+ hlist_bl_lock(hbl);
11100+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11101+ if (ent->dr_h_ino == ino) {
11102+ found = ent;
11103+ break;
11104+ }
11105+ hlist_bl_unlock(hbl);
11106+
11107+ return found;
11108+}
11109+
11110+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
11111+ struct au_dr_hino *add_ent)
11112+{
11113+ int found, idx;
11114+ struct hlist_bl_head *hbl;
11115+ struct hlist_bl_node *pos;
11116+ struct au_dr_hino *ent;
11117+
11118+ found = 0;
11119+ idx = au_dr_ihash(ino);
11120+ hbl = dr->dr_h_ino + idx;
11121+#if 0
11122+ {
11123+ struct hlist_bl_node *tmp;
11124+
11125+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11126+ AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
11127+ }
11128+#endif
11129+ hlist_bl_lock(hbl);
11130+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11131+ if (ent->dr_h_ino == ino) {
11132+ found = 1;
11133+ break;
11134+ }
11135+ if (!found && add_ent)
11136+ hlist_bl_add_head(&add_ent->dr_hnode, hbl);
11137+ hlist_bl_unlock(hbl);
11138+
11139+ if (!found && add_ent)
11140+ AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
11141+
11142+ return found;
11143+}
11144+
11145+void au_dr_hino_free(struct au_dr_br *dr)
11146+{
11147+ int i;
11148+ struct hlist_bl_head *hbl;
11149+ struct hlist_bl_node *pos, *tmp;
11150+ struct au_dr_hino *ent;
11151+
11152+ /* SiMustWriteLock(sb); */
11153+
11154+ for (i = 0; i < AuDirren_NHASH; i++) {
11155+ hbl = dr->dr_h_ino + i;
11156+ /* no spinlock since sbinfo must be write-locked */
11157+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
e49925d1 11158+ au_kfree_rcu(ent);
8b6a4947
AM
11159+ INIT_HLIST_BL_HEAD(hbl);
11160+ }
11161+}
11162+
11163+/* returns the number of inodes or an error */
11164+static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
11165+ struct file *hinofile)
11166+{
11167+ int err, i;
11168+ ssize_t ssz;
11169+ loff_t pos, oldsize;
11170+ __be64 u64;
11171+ struct inode *hinoinode;
11172+ struct hlist_bl_head *hbl;
11173+ struct hlist_bl_node *n1, *n2;
11174+ struct au_dr_hino *ent;
11175+
11176+ SiMustWriteLock(sb);
11177+ AuDebugOn(!au_br_writable(br->br_perm));
11178+
11179+ hinoinode = file_inode(hinofile);
11180+ oldsize = i_size_read(hinoinode);
11181+
11182+ err = 0;
11183+ pos = 0;
11184+ hbl = br->br_dirren.dr_h_ino;
11185+ for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11186+ /* no bit-lock since sbinfo must be write-locked */
11187+ hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11188+ AuDbg("hi%llu, %pD2\n",
11189+ (unsigned long long)ent->dr_h_ino, hinofile);
11190+ u64 = cpu_to_be64(ent->dr_h_ino);
11191+ ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11192+ if (ssz == sizeof(u64))
11193+ continue;
11194+
11195+ /* write error */
11196+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11197+ err = -ENOSPC;
11198+ if (ssz < 0)
11199+ err = ssz;
11200+ break;
11201+ }
11202+ }
11203+ /* regardless the error */
11204+ if (pos < oldsize) {
11205+ err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11206+ AuTraceErr(err);
11207+ }
11208+
11209+ AuTraceErr(err);
11210+ return err;
11211+}
11212+
11213+static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11214+{
11215+ int err, hidx;
11216+ ssize_t ssz;
11217+ size_t sz, n;
11218+ loff_t pos;
11219+ uint64_t u64;
11220+ struct au_dr_hino *ent;
11221+ struct inode *hinoinode;
11222+ struct hlist_bl_head *hbl;
11223+
11224+ err = 0;
11225+ pos = 0;
11226+ hbl = dr->dr_h_ino;
11227+ hinoinode = file_inode(hinofile);
11228+ sz = i_size_read(hinoinode);
11229+ AuDebugOn(sz % sizeof(u64));
11230+ n = sz / sizeof(u64);
11231+ while (n--) {
11232+ ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11233+ if (unlikely(ssz != sizeof(u64))) {
11234+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11235+ err = -EINVAL;
11236+ if (ssz < 0)
11237+ err = ssz;
11238+ goto out_free;
11239+ }
11240+
11241+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11242+ if (!ent) {
11243+ err = -ENOMEM;
11244+ AuTraceErr(err);
11245+ goto out_free;
11246+ }
11247+ ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11248+ AuDbg("hi%llu, %pD2\n",
11249+ (unsigned long long)ent->dr_h_ino, hinofile);
11250+ hidx = au_dr_ihash(ent->dr_h_ino);
11251+ au_hbl_add(&ent->dr_hnode, hbl + hidx);
11252+ }
11253+ goto out; /* success */
11254+
11255+out_free:
11256+ au_dr_hino_free(dr);
11257+out:
11258+ AuTraceErr(err);
11259+ return err;
11260+}
11261+
11262+/*
11263+ * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11264+ * @path is a switch to distinguish load and store.
11265+ */
11266+static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11267+ struct au_branch *br, const struct path *path)
11268+{
11269+ int err, flags;
11270+ unsigned char load, suspend;
11271+ struct file *hinofile;
11272+ struct au_hinode *hdir;
11273+ struct inode *dir, *delegated;
11274+ struct path hinopath;
11275+ struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11276+ sizeof(AUFS_WH_DR_BRHINO) - 1);
11277+
11278+ AuDebugOn(bindex < 0 && !br);
11279+ AuDebugOn(bindex >= 0 && br);
11280+
11281+ err = -EINVAL;
11282+ suspend = !br;
11283+ if (suspend)
11284+ br = au_sbr(sb, bindex);
11285+ load = !!path;
11286+ if (!load) {
11287+ path = &br->br_path;
11288+ AuDebugOn(!au_br_writable(br->br_perm));
11289+ if (unlikely(!au_br_writable(br->br_perm)))
11290+ goto out;
11291+ }
11292+
11293+ hdir = NULL;
11294+ if (suspend) {
11295+ dir = d_inode(sb->s_root);
11296+ hdir = au_hinode(au_ii(dir), bindex);
11297+ dir = hdir->hi_inode;
11298+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11299+ } else {
11300+ dir = d_inode(path->dentry);
11301+ inode_lock_nested(dir, AuLsc_I_CHILD);
11302+ }
11303+ hinopath.dentry = vfsub_lkup_one(&hinoname, path->dentry);
11304+ err = PTR_ERR(hinopath.dentry);
11305+ if (IS_ERR(hinopath.dentry))
11306+ goto out_unlock;
11307+
11308+ err = 0;
11309+ flags = O_RDONLY;
11310+ if (load) {
11311+ if (d_is_negative(hinopath.dentry))
11312+ goto out_dput; /* success */
11313+ } else {
11314+ if (au_dr_hino_test_empty(&br->br_dirren)) {
11315+ if (d_is_positive(hinopath.dentry)) {
11316+ delegated = NULL;
11317+ err = vfsub_unlink(dir, &hinopath, &delegated,
11318+ /*force*/0);
11319+ AuTraceErr(err);
11320+ if (unlikely(err))
11321+ pr_err("ignored err %d, %pd2\n",
11322+ err, hinopath.dentry);
11323+ if (unlikely(err == -EWOULDBLOCK))
11324+ iput(delegated);
11325+ err = 0;
11326+ }
11327+ goto out_dput;
11328+ } else if (!d_is_positive(hinopath.dentry)) {
11329+ err = vfsub_create(dir, &hinopath, 0600,
11330+ /*want_excl*/false);
11331+ AuTraceErr(err);
11332+ if (unlikely(err))
11333+ goto out_dput;
11334+ }
11335+ flags = O_WRONLY;
11336+ }
11337+ hinopath.mnt = path->mnt;
11338+ hinofile = vfsub_dentry_open(&hinopath, flags);
11339+ if (suspend)
11340+ au_hn_inode_unlock(hdir);
11341+ else
11342+ inode_unlock(dir);
11343+ dput(hinopath.dentry);
11344+ AuTraceErrPtr(hinofile);
11345+ if (IS_ERR(hinofile)) {
11346+ err = PTR_ERR(hinofile);
11347+ goto out;
11348+ }
11349+
11350+ if (load)
11351+ err = au_dr_hino_load(&br->br_dirren, hinofile);
11352+ else
11353+ err = au_dr_hino_store(sb, br, hinofile);
11354+ fput(hinofile);
11355+ goto out;
11356+
11357+out_dput:
11358+ dput(hinopath.dentry);
11359+out_unlock:
11360+ if (suspend)
11361+ au_hn_inode_unlock(hdir);
11362+ else
11363+ inode_unlock(dir);
11364+out:
11365+ AuTraceErr(err);
11366+ return err;
11367+}
11368+
11369+/* ---------------------------------------------------------------------- */
11370+
11371+static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11372+{
11373+ int err;
11374+ struct kstatfs kstfs;
11375+ dev_t dev;
11376+ struct dentry *dentry;
11377+ struct super_block *sb;
11378+
11379+ err = vfs_statfs((void *)path, &kstfs);
11380+ AuTraceErr(err);
11381+ if (unlikely(err))
11382+ goto out;
11383+
11384+ /* todo: support for UUID */
11385+
11386+ if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11387+ brid->type = AuBrid_FSID;
11388+ brid->fsid = kstfs.f_fsid;
11389+ } else {
11390+ dentry = path->dentry;
11391+ sb = dentry->d_sb;
11392+ dev = sb->s_dev;
11393+ if (dev) {
11394+ brid->type = AuBrid_DEV;
11395+ brid->dev = dev;
11396+ }
11397+ }
11398+
11399+out:
11400+ return err;
11401+}
11402+
11403+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11404+ const struct path *path)
11405+{
11406+ int err, i;
11407+ struct au_dr_br *dr;
11408+ struct hlist_bl_head *hbl;
11409+
11410+ dr = &br->br_dirren;
11411+ hbl = dr->dr_h_ino;
11412+ for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11413+ INIT_HLIST_BL_HEAD(hbl);
11414+
11415+ err = au_dr_brid_init(&dr->dr_brid, path);
11416+ if (unlikely(err))
11417+ goto out;
11418+
11419+ if (au_opt_test(au_mntflags(sb), DIRREN))
11420+ err = au_dr_hino(sb, /*bindex*/-1, br, path);
11421+
11422+out:
11423+ AuTraceErr(err);
11424+ return err;
11425+}
11426+
11427+int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11428+{
11429+ int err;
11430+
11431+ err = 0;
11432+ if (au_br_writable(br->br_perm))
11433+ err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11434+ if (!err)
11435+ au_dr_hino_free(&br->br_dirren);
11436+
11437+ return err;
11438+}
11439+
11440+/* ---------------------------------------------------------------------- */
11441+
11442+static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11443+ char *buf, size_t sz)
11444+{
11445+ int err;
11446+ unsigned int major, minor;
11447+ char *p;
11448+
11449+ p = buf;
11450+ err = snprintf(p, sz, "%d_", brid->type);
11451+ AuDebugOn(err > sz);
11452+ p += err;
11453+ sz -= err;
11454+ switch (brid->type) {
11455+ case AuBrid_Unset:
11456+ return -EINVAL;
11457+ case AuBrid_UUID:
11458+ err = snprintf(p, sz, "%pU", brid->uuid.b);
11459+ break;
11460+ case AuBrid_FSID:
11461+ err = snprintf(p, sz, "%08x-%08x",
11462+ brid->fsid.val[0], brid->fsid.val[1]);
11463+ break;
11464+ case AuBrid_DEV:
11465+ major = MAJOR(brid->dev);
11466+ minor = MINOR(brid->dev);
11467+ if (major <= 0xff && minor <= 0xff)
11468+ err = snprintf(p, sz, "%02x%02x", major, minor);
11469+ else
11470+ err = snprintf(p, sz, "%03x:%05x", major, minor);
11471+ break;
11472+ }
11473+ AuDebugOn(err > sz);
11474+ p += err;
11475+ sz -= err;
11476+ err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11477+ AuDebugOn(err > sz);
11478+ p += err;
11479+ sz -= err;
11480+
11481+ return p - buf;
11482+}
11483+
11484+static int au_drinfo_name(struct au_branch *br, char *name, int len)
11485+{
11486+ int rlen;
11487+ struct dentry *br_dentry;
11488+ struct inode *br_inode;
11489+
11490+ br_dentry = au_br_dentry(br);
11491+ br_inode = d_inode(br_dentry);
11492+ rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11493+ AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11494+ AuDebugOn(rlen > len);
11495+
11496+ return rlen;
11497+}
11498+
11499+/* ---------------------------------------------------------------------- */
11500+
11501+/*
11502+ * from the given @h_dentry, construct drinfo at @*fdata.
11503+ * when the size of @*fdata is not enough, reallocate and return new @fdata and
11504+ * @allocated.
11505+ */
11506+static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11507+ struct dentry *h_dentry,
11508+ unsigned char *allocated)
11509+{
11510+ int err, v;
11511+ struct au_drinfo_fdata *f, *p;
11512+ struct au_drinfo *drinfo;
11513+ struct inode *h_inode;
11514+ struct qstr *qname;
11515+
11516+ err = 0;
11517+ f = *fdata;
11518+ h_inode = d_inode(h_dentry);
11519+ qname = &h_dentry->d_name;
11520+ drinfo = &f->drinfo;
11521+ drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11522+ drinfo->oldnamelen = qname->len;
11523+ if (*allocated < sizeof(*f) + qname->len) {
11524+ v = roundup_pow_of_two(*allocated + qname->len);
11525+ p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11526+ if (unlikely(!p)) {
11527+ err = -ENOMEM;
11528+ AuTraceErr(err);
11529+ goto out;
11530+ }
11531+ f = p;
11532+ *fdata = f;
11533+ *allocated = v;
11534+ drinfo = &f->drinfo;
11535+ }
11536+ memcpy(drinfo->oldname, qname->name, qname->len);
11537+ AuDbg("i%llu, %.*s\n",
11538+ be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11539+ drinfo->oldname);
11540+
11541+out:
11542+ AuTraceErr(err);
11543+ return err;
11544+}
11545+
11546+/* callers have to free the return value */
11547+static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11548+{
11549+ struct au_drinfo *ret, *drinfo;
11550+ struct au_drinfo_fdata fdata;
11551+ int len;
11552+ loff_t pos;
11553+ ssize_t ssz;
11554+
11555+ ret = ERR_PTR(-EIO);
11556+ pos = 0;
11557+ ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11558+ if (unlikely(ssz != sizeof(fdata))) {
11559+ AuIOErr("ssz %zd, %u, %pD2\n",
11560+ ssz, (unsigned int)sizeof(fdata), file);
11561+ goto out;
11562+ }
11563+
11564+ fdata.magic = ntohl((__force __be32)fdata.magic);
11565+ switch (fdata.magic) {
11566+ case AUFS_DRINFO_MAGIC_V1:
11567+ break;
11568+ default:
11569+ AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11570+ fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11571+ goto out;
11572+ }
11573+
11574+ drinfo = &fdata.drinfo;
11575+ len = drinfo->oldnamelen;
11576+ if (!len) {
11577+ AuIOErr("broken drinfo %pD2\n", file);
11578+ goto out;
11579+ }
11580+
11581+ ret = NULL;
11582+ drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11583+ if (unlikely(h_ino && drinfo->ino != h_ino)) {
11584+ AuDbg("ignored i%llu, i%llu, %pD2\n",
11585+ (unsigned long long)drinfo->ino,
11586+ (unsigned long long)h_ino, file);
11587+ goto out; /* success */
11588+ }
11589+
11590+ ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11591+ if (unlikely(!ret)) {
11592+ ret = ERR_PTR(-ENOMEM);
11593+ AuTraceErrPtr(ret);
11594+ goto out;
11595+ }
11596+
11597+ *ret = *drinfo;
11598+ ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11599+ if (unlikely(ssz != len)) {
e49925d1 11600+ au_kfree_rcu(ret);
8b6a4947
AM
11601+ ret = ERR_PTR(-EIO);
11602+ AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11603+ goto out;
11604+ }
11605+
11606+ AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11607+
11608+out:
11609+ return ret;
11610+}
11611+
11612+/* ---------------------------------------------------------------------- */
11613+
11614+/* in order to be revertible */
11615+struct au_drinfo_rev_elm {
11616+ int created;
11617+ struct dentry *info_dentry;
11618+ struct au_drinfo *info_last;
11619+};
11620+
11621+struct au_drinfo_rev {
11622+ unsigned char already;
11623+ aufs_bindex_t nelm;
11624+ struct au_drinfo_rev_elm elm[0];
11625+};
11626+
11627+/* todo: isn't it too large? */
11628+struct au_drinfo_store {
11629+ struct path h_ppath;
11630+ struct dentry *h_dentry;
11631+ struct au_drinfo_fdata *fdata;
11632+ char *infoname; /* inside of whname, just after PFX */
11633+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11634+ aufs_bindex_t btgt, btail;
11635+ unsigned char no_sio,
11636+ allocated, /* current size of *fdata */
11637+ infonamelen, /* room size for p */
acd2b654 11638+ whnamelen, /* length of the generated name */
8b6a4947
AM
11639+ renameback; /* renamed back */
11640+};
11641+
11642+/* on rename(2) error, the caller should revert it using @elm */
11643+static int au_drinfo_do_store(struct au_drinfo_store *w,
11644+ struct au_drinfo_rev_elm *elm)
11645+{
11646+ int err, len;
11647+ ssize_t ssz;
11648+ loff_t pos;
11649+ struct path infopath = {
11650+ .mnt = w->h_ppath.mnt
11651+ };
11652+ struct inode *h_dir, *h_inode, *delegated;
11653+ struct file *infofile;
11654+ struct qstr *qname;
11655+
11656+ AuDebugOn(elm
11657+ && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11658+
11659+ infopath.dentry = vfsub_lookup_one_len(w->whname, w->h_ppath.dentry,
11660+ w->whnamelen);
11661+ AuTraceErrPtr(infopath.dentry);
11662+ if (IS_ERR(infopath.dentry)) {
11663+ err = PTR_ERR(infopath.dentry);
11664+ goto out;
11665+ }
11666+
11667+ err = 0;
11668+ h_dir = d_inode(w->h_ppath.dentry);
11669+ if (elm && d_is_negative(infopath.dentry)) {
11670+ err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11671+ AuTraceErr(err);
11672+ if (unlikely(err))
11673+ goto out_dput;
11674+ elm->created = 1;
11675+ elm->info_dentry = dget(infopath.dentry);
11676+ }
11677+
11678+ infofile = vfsub_dentry_open(&infopath, O_RDWR);
11679+ AuTraceErrPtr(infofile);
11680+ if (IS_ERR(infofile)) {
11681+ err = PTR_ERR(infofile);
11682+ goto out_dput;
11683+ }
11684+
11685+ h_inode = d_inode(infopath.dentry);
11686+ if (elm && i_size_read(h_inode)) {
11687+ h_inode = d_inode(w->h_dentry);
11688+ elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11689+ AuTraceErrPtr(elm->info_last);
11690+ if (IS_ERR(elm->info_last)) {
11691+ err = PTR_ERR(elm->info_last);
11692+ elm->info_last = NULL;
11693+ AuDebugOn(elm->info_dentry);
11694+ goto out_fput;
11695+ }
11696+ }
11697+
11698+ if (elm && w->renameback) {
11699+ delegated = NULL;
11700+ err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11701+ AuTraceErr(err);
11702+ if (unlikely(err == -EWOULDBLOCK))
11703+ iput(delegated);
11704+ goto out_fput;
11705+ }
11706+
11707+ pos = 0;
11708+ qname = &w->h_dentry->d_name;
11709+ len = sizeof(*w->fdata) + qname->len;
11710+ if (!elm)
11711+ len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11712+ ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11713+ if (ssz == len) {
11714+ AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11715+ w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11716+ goto out_fput; /* success */
11717+ } else {
11718+ err = -EIO;
11719+ if (ssz < 0)
11720+ err = ssz;
11721+ /* the caller should revert it using @elm */
11722+ }
11723+
11724+out_fput:
11725+ fput(infofile);
11726+out_dput:
11727+ dput(infopath.dentry);
11728+out:
11729+ AuTraceErr(err);
11730+ return err;
11731+}
11732+
11733+struct au_call_drinfo_do_store_args {
11734+ int *errp;
11735+ struct au_drinfo_store *w;
11736+ struct au_drinfo_rev_elm *elm;
11737+};
11738+
11739+static void au_call_drinfo_do_store(void *args)
11740+{
11741+ struct au_call_drinfo_do_store_args *a = args;
11742+
11743+ *a->errp = au_drinfo_do_store(a->w, a->elm);
11744+}
11745+
11746+static int au_drinfo_store_sio(struct au_drinfo_store *w,
11747+ struct au_drinfo_rev_elm *elm)
11748+{
11749+ int err, wkq_err;
11750+
11751+ if (w->no_sio)
11752+ err = au_drinfo_do_store(w, elm);
11753+ else {
11754+ struct au_call_drinfo_do_store_args a = {
11755+ .errp = &err,
11756+ .w = w,
11757+ .elm = elm
11758+ };
11759+ wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11760+ if (unlikely(wkq_err))
11761+ err = wkq_err;
11762+ }
11763+ AuTraceErr(err);
11764+
11765+ return err;
11766+}
11767+
11768+static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11769+ aufs_bindex_t btgt)
11770+{
11771+ int err;
11772+
11773+ memset(w, 0, sizeof(*w));
11774+ w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11775+ strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11776+ w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11777+ w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11778+ w->btgt = btgt;
11779+ w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11780+
11781+ err = -ENOMEM;
11782+ w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11783+ if (unlikely(!w->fdata)) {
11784+ AuTraceErr(err);
11785+ goto out;
11786+ }
11787+ w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11788+ err = 0;
11789+
11790+out:
11791+ return err;
11792+}
11793+
11794+static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11795+{
e49925d1 11796+ au_kfree_rcu(w->fdata);
8b6a4947
AM
11797+}
11798+
11799+static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11800+ struct au_drinfo_store *w)
11801+{
11802+ struct au_drinfo_rev_elm *elm;
11803+ struct inode *h_dir, *delegated;
11804+ int err, nelm;
11805+ struct path infopath = {
11806+ .mnt = w->h_ppath.mnt
11807+ };
11808+
11809+ h_dir = d_inode(w->h_ppath.dentry);
11810+ IMustLock(h_dir);
11811+
11812+ err = 0;
11813+ elm = rev->elm;
11814+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11815+ AuDebugOn(elm->created && elm->info_last);
11816+ if (elm->created) {
11817+ AuDbg("here\n");
11818+ delegated = NULL;
11819+ infopath.dentry = elm->info_dentry;
11820+ err = vfsub_unlink(h_dir, &infopath, &delegated,
11821+ !w->no_sio);
11822+ AuTraceErr(err);
11823+ if (unlikely(err == -EWOULDBLOCK))
11824+ iput(delegated);
11825+ dput(elm->info_dentry);
11826+ } else if (elm->info_last) {
11827+ AuDbg("here\n");
11828+ w->fdata->drinfo = *elm->info_last;
11829+ memcpy(w->fdata->drinfo.oldname,
11830+ elm->info_last->oldname,
11831+ elm->info_last->oldnamelen);
11832+ err = au_drinfo_store_sio(w, /*elm*/NULL);
e49925d1 11833+ au_kfree_rcu(elm->info_last);
8b6a4947
AM
11834+ }
11835+ if (unlikely(err))
11836+ AuIOErr("%d, %s\n", err, w->whname);
11837+ /* go on even if err */
11838+ }
11839+}
11840+
11841+/* caller has to call au_dr_rename_fin() later */
11842+static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11843+ struct qstr *dst_name, void *_rev)
11844+{
11845+ int err, sz, nelm;
11846+ aufs_bindex_t bindex, btail;
11847+ struct au_drinfo_store work;
11848+ struct au_drinfo_rev *rev, **p;
11849+ struct au_drinfo_rev_elm *elm;
11850+ struct super_block *sb;
11851+ struct au_branch *br;
11852+ struct au_hinode *hdir;
11853+
11854+ err = au_drinfo_store_work_init(&work, btgt);
11855+ AuTraceErr(err);
11856+ if (unlikely(err))
11857+ goto out;
11858+
11859+ err = -ENOMEM;
11860+ btail = au_dbtaildir(dentry);
11861+ nelm = btail - btgt;
11862+ sz = sizeof(*rev) + sizeof(*elm) * nelm;
11863+ rev = kcalloc(1, sz, GFP_NOFS);
11864+ if (unlikely(!rev)) {
11865+ AuTraceErr(err);
11866+ goto out_args;
11867+ }
11868+ rev->nelm = nelm;
11869+ elm = rev->elm;
11870+ p = _rev;
11871+ *p = rev;
11872+
11873+ err = 0;
11874+ sb = dentry->d_sb;
11875+ work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11876+ work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11877+ hdir = au_hi(d_inode(dentry), btgt);
11878+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11879+ for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11880+ work.h_dentry = au_h_dptr(dentry, bindex);
11881+ if (!work.h_dentry)
11882+ continue;
11883+
11884+ err = au_drinfo_construct(&work.fdata, work.h_dentry,
11885+ &work.allocated);
11886+ AuTraceErr(err);
11887+ if (unlikely(err))
11888+ break;
11889+
11890+ work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11891+ br = au_sbr(sb, bindex);
11892+ work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11893+ work.whnamelen += au_drinfo_name(br, work.infoname,
11894+ work.infonamelen);
11895+ AuDbg("whname %.*s, i%llu, %.*s\n",
11896+ work.whnamelen, work.whname,
11897+ be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11898+ work.fdata->drinfo.oldnamelen,
11899+ work.fdata->drinfo.oldname);
11900+
11901+ err = au_drinfo_store_sio(&work, elm);
11902+ AuTraceErr(err);
11903+ if (unlikely(err))
11904+ break;
11905+ }
11906+ if (unlikely(err)) {
11907+ /* revert all drinfo */
11908+ au_drinfo_store_rev(rev, &work);
e49925d1 11909+ au_kfree_try_rcu(rev);
8b6a4947
AM
11910+ *p = NULL;
11911+ }
11912+ au_hn_inode_unlock(hdir);
11913+
11914+out_args:
11915+ au_drinfo_store_work_fin(&work);
11916+out:
11917+ return err;
11918+}
11919+
11920+/* ---------------------------------------------------------------------- */
11921+
11922+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11923+ struct qstr *dst_name, void *_rev)
11924+{
11925+ int err, already;
11926+ ino_t ino;
11927+ struct super_block *sb;
11928+ struct au_branch *br;
11929+ struct au_dr_br *dr;
11930+ struct dentry *h_dentry;
11931+ struct inode *h_inode;
11932+ struct au_dr_hino *ent;
11933+ struct au_drinfo_rev *rev, **p;
11934+
11935+ AuDbg("bindex %d\n", bindex);
11936+
11937+ err = -ENOMEM;
11938+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11939+ if (unlikely(!ent))
11940+ goto out;
11941+
11942+ sb = src->d_sb;
11943+ br = au_sbr(sb, bindex);
11944+ dr = &br->br_dirren;
11945+ h_dentry = au_h_dptr(src, bindex);
11946+ h_inode = d_inode(h_dentry);
11947+ ino = h_inode->i_ino;
11948+ ent->dr_h_ino = ino;
11949+ already = au_dr_hino_test_add(dr, ino, ent);
11950+ AuDbg("b%d, hi%llu, already %d\n",
11951+ bindex, (unsigned long long)ino, already);
11952+
11953+ err = au_drinfo_store(src, bindex, dst_name, _rev);
11954+ AuTraceErr(err);
11955+ if (!err) {
11956+ p = _rev;
11957+ rev = *p;
11958+ rev->already = already;
11959+ goto out; /* success */
11960+ }
11961+
11962+ /* revert */
11963+ if (!already)
11964+ au_dr_hino_del(dr, ent);
e49925d1 11965+ au_kfree_rcu(ent);
8b6a4947
AM
11966+
11967+out:
11968+ AuTraceErr(err);
11969+ return err;
11970+}
11971+
11972+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11973+{
11974+ struct au_drinfo_rev *rev;
11975+ struct au_drinfo_rev_elm *elm;
11976+ int nelm;
11977+
11978+ rev = _rev;
11979+ elm = rev->elm;
11980+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11981+ dput(elm->info_dentry);
e49925d1 11982+ au_kfree_rcu(elm->info_last);
8b6a4947 11983+ }
e49925d1 11984+ au_kfree_try_rcu(rev);
8b6a4947
AM
11985+}
11986+
11987+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11988+{
11989+ int err;
11990+ struct au_drinfo_store work;
11991+ struct au_drinfo_rev *rev = _rev;
11992+ struct super_block *sb;
11993+ struct au_branch *br;
11994+ struct inode *h_inode;
11995+ struct au_dr_br *dr;
11996+ struct au_dr_hino *ent;
11997+
11998+ err = au_drinfo_store_work_init(&work, btgt);
11999+ if (unlikely(err))
12000+ goto out;
12001+
12002+ sb = src->d_sb;
12003+ br = au_sbr(sb, btgt);
12004+ work.h_ppath.dentry = au_h_dptr(src, btgt);
12005+ work.h_ppath.mnt = au_br_mnt(br);
12006+ au_drinfo_store_rev(rev, &work);
12007+ au_drinfo_store_work_fin(&work);
12008+ if (rev->already)
12009+ goto out;
12010+
12011+ dr = &br->br_dirren;
12012+ h_inode = d_inode(work.h_ppath.dentry);
12013+ ent = au_dr_hino_find(dr, h_inode->i_ino);
12014+ BUG_ON(!ent);
12015+ au_dr_hino_del(dr, ent);
e49925d1 12016+ au_kfree_rcu(ent);
8b6a4947
AM
12017+
12018+out:
e49925d1 12019+ au_kfree_try_rcu(rev);
8b6a4947
AM
12020+ if (unlikely(err))
12021+ pr_err("failed to remove dirren info\n");
12022+}
12023+
12024+/* ---------------------------------------------------------------------- */
12025+
12026+static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
12027+ char *whname, int whnamelen,
12028+ struct dentry **info_dentry)
12029+{
12030+ struct au_drinfo *drinfo;
12031+ struct file *f;
12032+ struct inode *h_dir;
12033+ struct path infopath;
12034+ int unlocked;
12035+
12036+ AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
12037+
12038+ *info_dentry = NULL;
12039+ drinfo = NULL;
12040+ unlocked = 0;
12041+ h_dir = d_inode(h_ppath->dentry);
be118d29 12042+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8b6a4947
AM
12043+ infopath.dentry = vfsub_lookup_one_len(whname, h_ppath->dentry,
12044+ whnamelen);
12045+ if (IS_ERR(infopath.dentry)) {
12046+ drinfo = (void *)infopath.dentry;
12047+ goto out;
12048+ }
12049+
12050+ if (d_is_negative(infopath.dentry))
12051+ goto out_dput; /* success */
12052+
12053+ infopath.mnt = h_ppath->mnt;
12054+ f = vfsub_dentry_open(&infopath, O_RDONLY);
12055+ inode_unlock_shared(h_dir);
12056+ unlocked = 1;
12057+ if (IS_ERR(f)) {
12058+ drinfo = (void *)f;
12059+ goto out_dput;
12060+ }
12061+
12062+ drinfo = au_drinfo_read_k(f, /*h_ino*/0);
12063+ if (IS_ERR_OR_NULL(drinfo))
12064+ goto out_fput;
12065+
12066+ AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
12067+ *info_dentry = dget(infopath.dentry); /* keep it alive */
12068+
12069+out_fput:
12070+ fput(f);
12071+out_dput:
12072+ dput(infopath.dentry);
12073+out:
12074+ if (!unlocked)
12075+ inode_unlock_shared(h_dir);
12076+ AuTraceErrPtr(drinfo);
12077+ return drinfo;
12078+}
12079+
12080+struct au_drinfo_do_load_args {
12081+ struct au_drinfo **drinfop;
12082+ struct path *h_ppath;
12083+ char *whname;
12084+ int whnamelen;
12085+ struct dentry **info_dentry;
12086+};
12087+
12088+static void au_call_drinfo_do_load(void *args)
12089+{
12090+ struct au_drinfo_do_load_args *a = args;
12091+
12092+ *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
12093+ a->info_dentry);
12094+}
12095+
12096+struct au_drinfo_load {
12097+ struct path h_ppath;
12098+ struct qstr *qname;
12099+ unsigned char no_sio;
12100+
12101+ aufs_bindex_t ninfo;
12102+ struct au_drinfo **drinfo;
12103+};
12104+
12105+static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
12106+ struct au_branch *br)
12107+{
12108+ int err, wkq_err, whnamelen, e;
12109+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
12110+ = AUFS_WH_DR_INFO_PFX;
12111+ struct au_drinfo *drinfo;
12112+ struct qstr oldname;
12113+ struct inode *h_dir, *delegated;
12114+ struct dentry *info_dentry;
12115+ struct path infopath;
12116+
12117+ whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
12118+ whnamelen += au_drinfo_name(br, whname + whnamelen,
12119+ sizeof(whname) - whnamelen);
12120+ if (w->no_sio)
12121+ drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
12122+ &info_dentry);
12123+ else {
12124+ struct au_drinfo_do_load_args args = {
12125+ .drinfop = &drinfo,
12126+ .h_ppath = &w->h_ppath,
12127+ .whname = whname,
12128+ .whnamelen = whnamelen,
12129+ .info_dentry = &info_dentry
12130+ };
12131+ wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
12132+ if (unlikely(wkq_err))
12133+ drinfo = ERR_PTR(wkq_err);
12134+ }
12135+ err = PTR_ERR(drinfo);
12136+ if (IS_ERR_OR_NULL(drinfo))
12137+ goto out;
12138+
12139+ err = 0;
12140+ oldname.len = drinfo->oldnamelen;
12141+ oldname.name = drinfo->oldname;
12142+ if (au_qstreq(w->qname, &oldname)) {
12143+ /* the name is renamed back */
e49925d1 12144+ au_kfree_rcu(drinfo);
8b6a4947
AM
12145+ drinfo = NULL;
12146+
12147+ infopath.dentry = info_dentry;
12148+ infopath.mnt = w->h_ppath.mnt;
12149+ h_dir = d_inode(w->h_ppath.dentry);
12150+ delegated = NULL;
12151+ inode_lock_nested(h_dir, AuLsc_I_PARENT);
12152+ e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
12153+ inode_unlock(h_dir);
12154+ if (unlikely(e))
12155+ AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
12156+ if (unlikely(e == -EWOULDBLOCK))
12157+ iput(delegated);
12158+ }
e49925d1 12159+ au_kfree_rcu(w->drinfo[bindex]);
8b6a4947
AM
12160+ w->drinfo[bindex] = drinfo;
12161+ dput(info_dentry);
12162+
12163+out:
12164+ AuTraceErr(err);
12165+ return err;
12166+}
12167+
12168+/* ---------------------------------------------------------------------- */
12169+
12170+static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12171+{
12172+ struct au_drinfo **p = drinfo;
12173+
12174+ while (n-- > 0)
e49925d1
JR
12175+ au_kfree_rcu(*drinfo++);
12176+ au_kfree_try_rcu(p);
8b6a4947
AM
12177+}
12178+
12179+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12180+ aufs_bindex_t btgt)
12181+{
12182+ int err, ninfo;
12183+ struct au_drinfo_load w;
12184+ aufs_bindex_t bindex, bbot;
12185+ struct au_branch *br;
12186+ struct inode *h_dir;
12187+ struct au_dr_hino *ent;
12188+ struct super_block *sb;
12189+
12190+ AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12191+ AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12192+ AuLNPair(&lkup->whname), btgt);
12193+
12194+ sb = dentry->d_sb;
12195+ bbot = au_sbbot(sb);
12196+ w.ninfo = bbot + 1;
12197+ if (!lkup->dirren.drinfo) {
12198+ lkup->dirren.drinfo = kcalloc(w.ninfo,
12199+ sizeof(*lkup->dirren.drinfo),
12200+ GFP_NOFS);
12201+ if (unlikely(!lkup->dirren.drinfo)) {
12202+ err = -ENOMEM;
12203+ goto out;
12204+ }
12205+ lkup->dirren.ninfo = w.ninfo;
12206+ }
12207+ w.drinfo = lkup->dirren.drinfo;
12208+ w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12209+ w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12210+ AuDebugOn(!w.h_ppath.dentry);
12211+ w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12212+ w.qname = &dentry->d_name;
12213+
12214+ ninfo = 0;
12215+ for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12216+ br = au_sbr(sb, bindex);
12217+ err = au_drinfo_load(&w, bindex, br);
12218+ if (unlikely(err))
12219+ goto out_free;
12220+ if (w.drinfo[bindex])
12221+ ninfo++;
12222+ }
12223+ if (!ninfo) {
12224+ br = au_sbr(sb, btgt);
12225+ h_dir = d_inode(w.h_ppath.dentry);
12226+ ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12227+ AuDebugOn(!ent);
12228+ au_dr_hino_del(&br->br_dirren, ent);
e49925d1 12229+ au_kfree_rcu(ent);
8b6a4947
AM
12230+ }
12231+ goto out; /* success */
12232+
12233+out_free:
12234+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12235+ lkup->dirren.ninfo = 0;
12236+ lkup->dirren.drinfo = NULL;
12237+out:
12238+ AuTraceErr(err);
12239+ return err;
12240+}
12241+
12242+void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12243+{
12244+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12245+}
12246+
12247+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12248+{
12249+ int err;
12250+ struct au_drinfo *drinfo;
12251+
12252+ err = 0;
12253+ if (!lkup->dirren.drinfo)
12254+ goto out;
12255+ AuDebugOn(lkup->dirren.ninfo < btgt + 1);
12256+ drinfo = lkup->dirren.drinfo[btgt + 1];
12257+ if (!drinfo)
12258+ goto out;
12259+
e49925d1 12260+ au_kfree_try_rcu(lkup->whname.name);
8b6a4947
AM
12261+ lkup->whname.name = NULL;
12262+ lkup->dirren.dr_name.len = drinfo->oldnamelen;
12263+ lkup->dirren.dr_name.name = drinfo->oldname;
12264+ lkup->name = &lkup->dirren.dr_name;
12265+ err = au_wh_name_alloc(&lkup->whname, lkup->name);
12266+ if (!err)
12267+ AuDbg("name %.*s, whname %.*s, b%d\n",
12268+ AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12269+ btgt);
12270+
12271+out:
12272+ AuTraceErr(err);
12273+ return err;
12274+}
12275+
12276+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12277+ ino_t h_ino)
12278+{
12279+ int match;
12280+ struct au_drinfo *drinfo;
12281+
12282+ match = 1;
12283+ if (!lkup->dirren.drinfo)
12284+ goto out;
12285+ AuDebugOn(lkup->dirren.ninfo < bindex + 1);
12286+ drinfo = lkup->dirren.drinfo[bindex + 1];
12287+ if (!drinfo)
12288+ goto out;
12289+
12290+ match = (drinfo->ino == h_ino);
12291+ AuDbg("match %d\n", match);
12292+
12293+out:
12294+ return match;
12295+}
12296+
12297+/* ---------------------------------------------------------------------- */
12298+
12299+int au_dr_opt_set(struct super_block *sb)
12300+{
12301+ int err;
12302+ aufs_bindex_t bindex, bbot;
12303+ struct au_branch *br;
12304+
12305+ err = 0;
12306+ bbot = au_sbbot(sb);
12307+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12308+ br = au_sbr(sb, bindex);
12309+ err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12310+ }
12311+
12312+ return err;
12313+}
12314+
12315+int au_dr_opt_flush(struct super_block *sb)
12316+{
12317+ int err;
12318+ aufs_bindex_t bindex, bbot;
12319+ struct au_branch *br;
12320+
12321+ err = 0;
12322+ bbot = au_sbbot(sb);
12323+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12324+ br = au_sbr(sb, bindex);
12325+ if (au_br_writable(br->br_perm))
12326+ err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12327+ }
12328+
12329+ return err;
12330+}
12331+
12332+int au_dr_opt_clr(struct super_block *sb, int no_flush)
12333+{
12334+ int err;
12335+ aufs_bindex_t bindex, bbot;
12336+ struct au_branch *br;
12337+
12338+ err = 0;
12339+ if (!no_flush) {
12340+ err = au_dr_opt_flush(sb);
12341+ if (unlikely(err))
12342+ goto out;
12343+ }
12344+
12345+ bbot = au_sbbot(sb);
12346+ for (bindex = 0; bindex <= bbot; bindex++) {
12347+ br = au_sbr(sb, bindex);
12348+ au_dr_hino_free(&br->br_dirren);
12349+ }
12350+
12351+out:
12352+ return err;
12353+}
12354diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12355--- /usr/share/empty/fs/aufs/dirren.h 1970-01-01 01:00:00.000000000 +0100
e49925d1 12356+++ linux/fs/aufs/dirren.h 2019-01-28 14:36:12.219084659 +0100
062440b3
AM
12357@@ -0,0 +1,140 @@
12358+/* SPDX-License-Identifier: GPL-2.0 */
8b6a4947 12359+/*
b00004a5 12360+ * Copyright (C) 2017-2018 Junjiro R. Okajima
8b6a4947
AM
12361+ *
12362+ * This program, aufs is free software; you can redistribute it and/or modify
12363+ * it under the terms of the GNU General Public License as published by
12364+ * the Free Software Foundation; either version 2 of the License, or
12365+ * (at your option) any later version.
12366+ *
12367+ * This program is distributed in the hope that it will be useful,
12368+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12369+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12370+ * GNU General Public License for more details.
12371+ *
12372+ * You should have received a copy of the GNU General Public License
12373+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
12374+ */
12375+
12376+/*
12377+ * renamed dir info
12378+ */
12379+
12380+#ifndef __AUFS_DIRREN_H__
12381+#define __AUFS_DIRREN_H__
12382+
12383+#ifdef __KERNEL__
12384+
12385+#include <linux/dcache.h>
12386+#include <linux/statfs.h>
12387+#include <linux/uuid.h>
12388+#include "hbl.h"
12389+
12390+#define AuDirren_NHASH 100
12391+
12392+#ifdef CONFIG_AUFS_DIRREN
12393+enum au_brid_type {
12394+ AuBrid_Unset,
12395+ AuBrid_UUID,
12396+ AuBrid_FSID,
12397+ AuBrid_DEV
12398+};
12399+
12400+struct au_dr_brid {
12401+ enum au_brid_type type;
12402+ union {
12403+ uuid_t uuid; /* unimplemented yet */
12404+ fsid_t fsid;
12405+ dev_t dev;
12406+ };
12407+};
12408+
12409+/* 20 is the max digits length of ulong 64 */
12410+/* brid-type "_" uuid "_" inum */
12411+#define AUFS_DIRREN_FNAME_SZ (1 + 1 + UUID_STRING_LEN + 20)
12412+#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12413+
12414+struct au_dr_hino {
12415+ struct hlist_bl_node dr_hnode;
12416+ ino_t dr_h_ino;
12417+};
12418+
12419+struct au_dr_br {
12420+ struct hlist_bl_head dr_h_ino[AuDirren_NHASH];
12421+ struct au_dr_brid dr_brid;
12422+};
12423+
12424+struct au_dr_lookup {
12425+ /* dr_name is pointed by struct au_do_lookup_args.name */
12426+ struct qstr dr_name; /* subset of dr_info */
12427+ aufs_bindex_t ninfo;
12428+ struct au_drinfo **drinfo;
12429+};
12430+#else
12431+struct au_dr_hino;
12432+/* empty */
12433+struct au_dr_br { };
12434+struct au_dr_lookup { };
12435+#endif
12436+
12437+/* ---------------------------------------------------------------------- */
12438+
12439+struct au_branch;
12440+struct au_do_lookup_args;
12441+struct au_hinode;
12442+#ifdef CONFIG_AUFS_DIRREN
12443+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12444+ struct au_dr_hino *add_ent);
12445+void au_dr_hino_free(struct au_dr_br *dr);
12446+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12447+ const struct path *path);
12448+int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12449+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12450+ struct qstr *dst_name, void *_rev);
12451+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12452+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12453+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12454+ aufs_bindex_t bindex);
12455+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12456+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12457+ ino_t h_ino);
12458+void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12459+int au_dr_opt_set(struct super_block *sb);
12460+int au_dr_opt_flush(struct super_block *sb);
12461+int au_dr_opt_clr(struct super_block *sb, int no_flush);
12462+#else
12463+AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12464+ struct au_dr_hino *add_ent);
12465+AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12466+AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12467+ const struct path *path);
12468+AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12469+AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12470+ struct qstr *dst_name, void *_rev);
12471+AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12472+AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12473+ void *rev);
12474+AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12475+ aufs_bindex_t bindex);
12476+AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12477+AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12478+ aufs_bindex_t bindex, ino_t h_ino);
12479+AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12480+AuStubInt0(au_dr_opt_set, struct super_block *sb);
12481+AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12482+AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12483+#endif
12484+
12485+/* ---------------------------------------------------------------------- */
12486+
12487+#ifdef CONFIG_AUFS_DIRREN
12488+static inline int au_dr_ihash(ino_t h_ino)
12489+{
12490+ return h_ino % AuDirren_NHASH;
12491+}
12492+#else
12493+AuStubInt0(au_dr_ihash, ino_t h_ino);
12494+#endif
12495+
12496+#endif /* __KERNEL__ */
12497+#endif /* __AUFS_DIRREN_H__ */
7f207e10
AM
12498diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12499--- /usr/share/empty/fs/aufs/dynop.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 12500+++ linux/fs/aufs/dynop.c 2019-01-28 14:36:12.219084659 +0100
062440b3 12501@@ -0,0 +1,370 @@
cd7a4cd9 12502+// SPDX-License-Identifier: GPL-2.0
1facf9fc 12503+/*
b00004a5 12504+ * Copyright (C) 2010-2018 Junjiro R. Okajima
1facf9fc 12505+ *
12506+ * This program, aufs is free software; you can redistribute it and/or modify
12507+ * it under the terms of the GNU General Public License as published by
12508+ * the Free Software Foundation; either version 2 of the License, or
12509+ * (at your option) any later version.
dece6358
AM
12510+ *
12511+ * This program is distributed in the hope that it will be useful,
12512+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12513+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12514+ * GNU General Public License for more details.
12515+ *
12516+ * You should have received a copy of the GNU General Public License
523b37e3 12517+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 12518+ */
12519+
12520+/*
4a4d8108 12521+ * dynamically customizable operations for regular files
1facf9fc 12522+ */
12523+
1facf9fc 12524+#include "aufs.h"
12525+
4a4d8108 12526+#define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
1facf9fc 12527+
4a4d8108
AM
12528+/*
12529+ * How large will these lists be?
12530+ * Usually just a few elements, 20-30 at most for each, I guess.
12531+ */
8b6a4947 12532+static struct hlist_bl_head dynop[AuDyLast];
4a4d8108 12533+
8b6a4947
AM
12534+static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12535+ const void *h_op)
1facf9fc 12536+{
4a4d8108 12537+ struct au_dykey *key, *tmp;
8b6a4947 12538+ struct hlist_bl_node *pos;
1facf9fc 12539+
4a4d8108 12540+ key = NULL;
8b6a4947
AM
12541+ hlist_bl_lock(hbl);
12542+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
4a4d8108
AM
12543+ if (tmp->dk_op.dy_hop == h_op) {
12544+ key = tmp;
12545+ kref_get(&key->dk_kref);
12546+ break;
12547+ }
8b6a4947 12548+ hlist_bl_unlock(hbl);
4a4d8108
AM
12549+
12550+ return key;
1facf9fc 12551+}
12552+
4a4d8108 12553+static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
1facf9fc 12554+{
4a4d8108
AM
12555+ struct au_dykey **k, *found;
12556+ const void *h_op = key->dk_op.dy_hop;
12557+ int i;
1facf9fc 12558+
4a4d8108
AM
12559+ found = NULL;
12560+ k = br->br_dykey;
12561+ for (i = 0; i < AuBrDynOp; i++)
12562+ if (k[i]) {
12563+ if (k[i]->dk_op.dy_hop == h_op) {
12564+ found = k[i];
12565+ break;
12566+ }
12567+ } else
12568+ break;
12569+ if (!found) {
12570+ spin_lock(&br->br_dykey_lock);
12571+ for (; i < AuBrDynOp; i++)
12572+ if (k[i]) {
12573+ if (k[i]->dk_op.dy_hop == h_op) {
12574+ found = k[i];
12575+ break;
12576+ }
12577+ } else {
12578+ k[i] = key;
12579+ break;
12580+ }
12581+ spin_unlock(&br->br_dykey_lock);
12582+ BUG_ON(i == AuBrDynOp); /* expand the array */
12583+ }
12584+
12585+ return found;
1facf9fc 12586+}
12587+
4a4d8108 12588+/* kref_get() if @key is already added */
8b6a4947 12589+static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
4a4d8108
AM
12590+{
12591+ struct au_dykey *tmp, *found;
8b6a4947 12592+ struct hlist_bl_node *pos;
4a4d8108 12593+ const void *h_op = key->dk_op.dy_hop;
1facf9fc 12594+
4a4d8108 12595+ found = NULL;
8b6a4947
AM
12596+ hlist_bl_lock(hbl);
12597+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
4a4d8108
AM
12598+ if (tmp->dk_op.dy_hop == h_op) {
12599+ kref_get(&tmp->dk_kref);
12600+ found = tmp;
12601+ break;
12602+ }
12603+ if (!found)
8b6a4947
AM
12604+ hlist_bl_add_head(&key->dk_hnode, hbl);
12605+ hlist_bl_unlock(hbl);
1facf9fc 12606+
4a4d8108
AM
12607+ if (!found)
12608+ DyPrSym(key);
12609+ return found;
12610+}
12611+
12612+static void dy_free_rcu(struct rcu_head *rcu)
1facf9fc 12613+{
4a4d8108
AM
12614+ struct au_dykey *key;
12615+
12616+ key = container_of(rcu, struct au_dykey, dk_rcu);
12617+ DyPrSym(key);
e49925d1 12618+ au_kfree_rcu(key);
1facf9fc 12619+}
12620+
4a4d8108
AM
12621+static void dy_free(struct kref *kref)
12622+{
12623+ struct au_dykey *key;
8b6a4947 12624+ struct hlist_bl_head *hbl;
1facf9fc 12625+
4a4d8108 12626+ key = container_of(kref, struct au_dykey, dk_kref);
8b6a4947
AM
12627+ hbl = dynop + key->dk_op.dy_type;
12628+ au_hbl_del(&key->dk_hnode, hbl);
4a4d8108
AM
12629+ call_rcu(&key->dk_rcu, dy_free_rcu);
12630+}
12631+
12632+void au_dy_put(struct au_dykey *key)
1facf9fc 12633+{
4a4d8108
AM
12634+ kref_put(&key->dk_kref, dy_free);
12635+}
1facf9fc 12636+
4a4d8108
AM
12637+/* ---------------------------------------------------------------------- */
12638+
12639+#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12640+
12641+#ifdef CONFIG_AUFS_DEBUG
12642+#define DyDbgDeclare(cnt) unsigned int cnt = 0
4f0767ce 12643+#define DyDbgInc(cnt) do { cnt++; } while (0)
4a4d8108
AM
12644+#else
12645+#define DyDbgDeclare(cnt) do {} while (0)
12646+#define DyDbgInc(cnt) do {} while (0)
12647+#endif
12648+
12649+#define DySet(func, dst, src, h_op, h_sb) do { \
12650+ DyDbgInc(cnt); \
12651+ if (h_op->func) { \
12652+ if (src.func) \
12653+ dst.func = src.func; \
12654+ else \
12655+ AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
12656+ } \
12657+} while (0)
12658+
12659+#define DySetForce(func, dst, src) do { \
12660+ AuDebugOn(!src.func); \
12661+ DyDbgInc(cnt); \
12662+ dst.func = src.func; \
12663+} while (0)
12664+
12665+#define DySetAop(func) \
12666+ DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12667+#define DySetAopForce(func) \
12668+ DySetForce(func, dyaop->da_op, aufs_aop)
12669+
12670+static void dy_aop(struct au_dykey *key, const void *h_op,
12671+ struct super_block *h_sb __maybe_unused)
12672+{
12673+ struct au_dyaop *dyaop = (void *)key;
12674+ const struct address_space_operations *h_aop = h_op;
12675+ DyDbgDeclare(cnt);
12676+
12677+ AuDbg("%s\n", au_sbtype(h_sb));
12678+
12679+ DySetAop(writepage);
12680+ DySetAopForce(readpage); /* force */
4a4d8108
AM
12681+ DySetAop(writepages);
12682+ DySetAop(set_page_dirty);
12683+ DySetAop(readpages);
12684+ DySetAop(write_begin);
12685+ DySetAop(write_end);
12686+ DySetAop(bmap);
12687+ DySetAop(invalidatepage);
12688+ DySetAop(releasepage);
027c5e7a 12689+ DySetAop(freepage);
7e9cd9fe 12690+ /* this one will be changed according to an aufs mount option */
4a4d8108 12691+ DySetAop(direct_IO);
4a4d8108 12692+ DySetAop(migratepage);
e2f27e51
AM
12693+ DySetAop(isolate_page);
12694+ DySetAop(putback_page);
4a4d8108
AM
12695+ DySetAop(launder_page);
12696+ DySetAop(is_partially_uptodate);
392086de 12697+ DySetAop(is_dirty_writeback);
4a4d8108 12698+ DySetAop(error_remove_page);
b4510431
AM
12699+ DySetAop(swap_activate);
12700+ DySetAop(swap_deactivate);
4a4d8108
AM
12701+
12702+ DyDbgSize(cnt, *h_aop);
4a4d8108
AM
12703+}
12704+
4a4d8108
AM
12705+/* ---------------------------------------------------------------------- */
12706+
12707+static void dy_bug(struct kref *kref)
12708+{
12709+ BUG();
12710+}
12711+
12712+static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12713+{
12714+ struct au_dykey *key, *old;
8b6a4947 12715+ struct hlist_bl_head *hbl;
b752ccd1 12716+ struct op {
4a4d8108 12717+ unsigned int sz;
b752ccd1
AM
12718+ void (*set)(struct au_dykey *key, const void *h_op,
12719+ struct super_block *h_sb __maybe_unused);
12720+ };
12721+ static const struct op a[] = {
4a4d8108
AM
12722+ [AuDy_AOP] = {
12723+ .sz = sizeof(struct au_dyaop),
b752ccd1 12724+ .set = dy_aop
4a4d8108 12725+ }
b752ccd1
AM
12726+ };
12727+ const struct op *p;
4a4d8108 12728+
8b6a4947
AM
12729+ hbl = dynop + op->dy_type;
12730+ key = dy_gfind_get(hbl, op->dy_hop);
4a4d8108
AM
12731+ if (key)
12732+ goto out_add; /* success */
12733+
12734+ p = a + op->dy_type;
12735+ key = kzalloc(p->sz, GFP_NOFS);
12736+ if (unlikely(!key)) {
12737+ key = ERR_PTR(-ENOMEM);
12738+ goto out;
12739+ }
12740+
12741+ key->dk_op.dy_hop = op->dy_hop;
12742+ kref_init(&key->dk_kref);
86dc4139 12743+ p->set(key, op->dy_hop, au_br_sb(br));
8b6a4947 12744+ old = dy_gadd(hbl, key);
4a4d8108 12745+ if (old) {
e49925d1 12746+ au_kfree_rcu(key);
4a4d8108
AM
12747+ key = old;
12748+ }
12749+
12750+out_add:
12751+ old = dy_bradd(br, key);
12752+ if (old)
12753+ /* its ref-count should never be zero here */
12754+ kref_put(&key->dk_kref, dy_bug);
12755+out:
12756+ return key;
12757+}
12758+
12759+/* ---------------------------------------------------------------------- */
12760+/*
acd2b654 12761+ * Aufs prohibits O_DIRECT by default even if the branch supports it.
c1595e42 12762+ * This behaviour is necessary to return an error from open(O_DIRECT) instead
4a4d8108
AM
12763+ * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12764+ * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12765+ * See the aufs manual in detail.
4a4d8108
AM
12766+ */
12767+static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12768+{
7e9cd9fe 12769+ if (!do_dx)
4a4d8108 12770+ dyaop->da_op.direct_IO = NULL;
7e9cd9fe 12771+ else
4a4d8108 12772+ dyaop->da_op.direct_IO = aufs_aop.direct_IO;
4a4d8108
AM
12773+}
12774+
12775+static struct au_dyaop *dy_aget(struct au_branch *br,
12776+ const struct address_space_operations *h_aop,
12777+ int do_dx)
12778+{
12779+ struct au_dyaop *dyaop;
12780+ struct au_dynop op;
12781+
12782+ op.dy_type = AuDy_AOP;
12783+ op.dy_haop = h_aop;
12784+ dyaop = (void *)dy_get(&op, br);
12785+ if (IS_ERR(dyaop))
12786+ goto out;
12787+ dy_adx(dyaop, do_dx);
12788+
12789+out:
12790+ return dyaop;
12791+}
12792+
12793+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12794+ struct inode *h_inode)
12795+{
12796+ int err, do_dx;
12797+ struct super_block *sb;
12798+ struct au_branch *br;
12799+ struct au_dyaop *dyaop;
12800+
12801+ AuDebugOn(!S_ISREG(h_inode->i_mode));
12802+ IiMustWriteLock(inode);
12803+
12804+ sb = inode->i_sb;
12805+ br = au_sbr(sb, bindex);
12806+ do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12807+ dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12808+ err = PTR_ERR(dyaop);
12809+ if (IS_ERR(dyaop))
12810+ /* unnecessary to call dy_fput() */
12811+ goto out;
12812+
12813+ err = 0;
12814+ inode->i_mapping->a_ops = &dyaop->da_op;
12815+
12816+out:
12817+ return err;
12818+}
12819+
b752ccd1
AM
12820+/*
12821+ * Is it safe to replace a_ops during the inode/file is in operation?
12822+ * Yes, I hope so.
12823+ */
12824+int au_dy_irefresh(struct inode *inode)
12825+{
12826+ int err;
5afbbe0d 12827+ aufs_bindex_t btop;
b752ccd1
AM
12828+ struct inode *h_inode;
12829+
12830+ err = 0;
12831+ if (S_ISREG(inode->i_mode)) {
5afbbe0d
AM
12832+ btop = au_ibtop(inode);
12833+ h_inode = au_h_iptr(inode, btop);
12834+ err = au_dy_iaop(inode, btop, h_inode);
b752ccd1
AM
12835+ }
12836+ return err;
12837+}
12838+
4a4d8108
AM
12839+void au_dy_arefresh(int do_dx)
12840+{
8b6a4947
AM
12841+ struct hlist_bl_head *hbl;
12842+ struct hlist_bl_node *pos;
4a4d8108
AM
12843+ struct au_dykey *key;
12844+
8b6a4947
AM
12845+ hbl = dynop + AuDy_AOP;
12846+ hlist_bl_lock(hbl);
12847+ hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
4a4d8108 12848+ dy_adx((void *)key, do_dx);
8b6a4947 12849+ hlist_bl_unlock(hbl);
4a4d8108
AM
12850+}
12851+
4a4d8108
AM
12852+/* ---------------------------------------------------------------------- */
12853+
12854+void __init au_dy_init(void)
12855+{
12856+ int i;
12857+
12858+ /* make sure that 'struct au_dykey *' can be any type */
12859+ BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
4a4d8108
AM
12860+
12861+ for (i = 0; i < AuDyLast; i++)
8b6a4947 12862+ INIT_HLIST_BL_HEAD(dynop + i);
4a4d8108
AM
12863+}
12864+
12865+void au_dy_fin(void)
12866+{
12867+ int i;
12868+
12869+ for (i = 0; i < AuDyLast; i++)
8b6a4947 12870+ WARN_ON(!hlist_bl_empty(dynop + i));
4a4d8108 12871+}
7f207e10
AM
12872diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12873--- /usr/share/empty/fs/aufs/dynop.h 1970-01-01 01:00:00.000000000 +0100
e49925d1 12874+++ linux/fs/aufs/dynop.h 2019-01-28 14:36:12.219084659 +0100
062440b3
AM
12875@@ -0,0 +1,75 @@
12876+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 12877+/*
b00004a5 12878+ * Copyright (C) 2010-2018 Junjiro R. Okajima
4a4d8108
AM
12879+ *
12880+ * This program, aufs is free software; you can redistribute it and/or modify
12881+ * it under the terms of the GNU General Public License as published by
12882+ * the Free Software Foundation; either version 2 of the License, or
12883+ * (at your option) any later version.
12884+ *
12885+ * This program is distributed in the hope that it will be useful,
12886+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12887+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12888+ * GNU General Public License for more details.
12889+ *
12890+ * You should have received a copy of the GNU General Public License
523b37e3 12891+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
12892+ */
12893+
12894+/*
12895+ * dynamically customizable operations (for regular files only)
12896+ */
12897+
12898+#ifndef __AUFS_DYNOP_H__
12899+#define __AUFS_DYNOP_H__
12900+
12901+#ifdef __KERNEL__
12902+
7e9cd9fe
AM
12903+#include <linux/fs.h>
12904+#include <linux/kref.h>
4a4d8108 12905+
2cbb1c4b 12906+enum {AuDy_AOP, AuDyLast};
4a4d8108
AM
12907+
12908+struct au_dynop {
12909+ int dy_type;
12910+ union {
12911+ const void *dy_hop;
12912+ const struct address_space_operations *dy_haop;
4a4d8108
AM
12913+ };
12914+};
12915+
12916+struct au_dykey {
12917+ union {
8b6a4947 12918+ struct hlist_bl_node dk_hnode;
4a4d8108
AM
12919+ struct rcu_head dk_rcu;
12920+ };
12921+ struct au_dynop dk_op;
12922+
12923+ /*
12924+ * during I am in the branch local array, kref is gotten. when the
12925+ * branch is removed, kref is put.
12926+ */
12927+ struct kref dk_kref;
12928+};
12929+
12930+/* stop unioning since their sizes are very different from each other */
12931+struct au_dyaop {
12932+ struct au_dykey da_key;
12933+ struct address_space_operations da_op; /* not const */
4a4d8108
AM
12934+};
12935+
4a4d8108
AM
12936+/* ---------------------------------------------------------------------- */
12937+
12938+/* dynop.c */
12939+struct au_branch;
12940+void au_dy_put(struct au_dykey *key);
12941+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12942+ struct inode *h_inode);
b752ccd1 12943+int au_dy_irefresh(struct inode *inode);
4a4d8108 12944+void au_dy_arefresh(int do_dio);
4a4d8108
AM
12945+
12946+void __init au_dy_init(void);
12947+void au_dy_fin(void);
12948+
4a4d8108
AM
12949+#endif /* __KERNEL__ */
12950+#endif /* __AUFS_DYNOP_H__ */
7f207e10
AM
12951diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12952--- /usr/share/empty/fs/aufs/export.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 12953+++ linux/fs/aufs/export.c 2019-01-28 14:36:12.219084659 +0100
062440b3 12954@@ -0,0 +1,838 @@
cd7a4cd9 12955+// SPDX-License-Identifier: GPL-2.0
4a4d8108 12956+/*
b00004a5 12957+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
12958+ *
12959+ * This program, aufs is free software; you can redistribute it and/or modify
12960+ * it under the terms of the GNU General Public License as published by
12961+ * the Free Software Foundation; either version 2 of the License, or
12962+ * (at your option) any later version.
12963+ *
12964+ * This program is distributed in the hope that it will be useful,
12965+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12966+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12967+ * GNU General Public License for more details.
12968+ *
12969+ * You should have received a copy of the GNU General Public License
523b37e3 12970+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
12971+ */
12972+
12973+/*
12974+ * export via nfs
12975+ */
12976+
12977+#include <linux/exportfs.h>
7eafdf33 12978+#include <linux/fs_struct.h>
4a4d8108
AM
12979+#include <linux/namei.h>
12980+#include <linux/nsproxy.h>
12981+#include <linux/random.h>
12982+#include <linux/writeback.h>
12983+#include "aufs.h"
12984+
12985+union conv {
12986+#ifdef CONFIG_AUFS_INO_T_64
12987+ __u32 a[2];
12988+#else
12989+ __u32 a[1];
12990+#endif
12991+ ino_t ino;
12992+};
12993+
12994+static ino_t decode_ino(__u32 *a)
12995+{
12996+ union conv u;
12997+
12998+ BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12999+ u.a[0] = a[0];
13000+#ifdef CONFIG_AUFS_INO_T_64
13001+ u.a[1] = a[1];
13002+#endif
13003+ return u.ino;
13004+}
13005+
13006+static void encode_ino(__u32 *a, ino_t ino)
13007+{
13008+ union conv u;
13009+
13010+ u.ino = ino;
13011+ a[0] = u.a[0];
13012+#ifdef CONFIG_AUFS_INO_T_64
13013+ a[1] = u.a[1];
13014+#endif
13015+}
13016+
13017+/* NFS file handle */
13018+enum {
13019+ Fh_br_id,
13020+ Fh_sigen,
13021+#ifdef CONFIG_AUFS_INO_T_64
13022+ /* support 64bit inode number */
13023+ Fh_ino1,
13024+ Fh_ino2,
13025+ Fh_dir_ino1,
13026+ Fh_dir_ino2,
13027+#else
13028+ Fh_ino1,
13029+ Fh_dir_ino1,
13030+#endif
13031+ Fh_igen,
13032+ Fh_h_type,
13033+ Fh_tail,
13034+
13035+ Fh_ino = Fh_ino1,
13036+ Fh_dir_ino = Fh_dir_ino1
13037+};
13038+
13039+static int au_test_anon(struct dentry *dentry)
13040+{
027c5e7a 13041+ /* note: read d_flags without d_lock */
4a4d8108
AM
13042+ return !!(dentry->d_flags & DCACHE_DISCONNECTED);
13043+}
13044+
a2a7ad62
AM
13045+int au_test_nfsd(void)
13046+{
13047+ int ret;
13048+ struct task_struct *tsk = current;
13049+ char comm[sizeof(tsk->comm)];
13050+
13051+ ret = 0;
13052+ if (tsk->flags & PF_KTHREAD) {
13053+ get_task_comm(comm, tsk);
13054+ ret = !strcmp(comm, "nfsd");
13055+ }
13056+
13057+ return ret;
13058+}
13059+
4a4d8108
AM
13060+/* ---------------------------------------------------------------------- */
13061+/* inode generation external table */
13062+
b752ccd1 13063+void au_xigen_inc(struct inode *inode)
4a4d8108 13064+{
4a4d8108
AM
13065+ loff_t pos;
13066+ ssize_t sz;
13067+ __u32 igen;
13068+ struct super_block *sb;
13069+ struct au_sbinfo *sbinfo;
13070+
4a4d8108 13071+ sb = inode->i_sb;
b752ccd1 13072+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
1facf9fc 13073+
b752ccd1 13074+ sbinfo = au_sbi(sb);
1facf9fc 13075+ pos = inode->i_ino;
13076+ pos *= sizeof(igen);
13077+ igen = inode->i_generation + 1;
1facf9fc 13078+ sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
13079+ sizeof(igen), &pos);
13080+ if (sz == sizeof(igen))
b752ccd1 13081+ return; /* success */
1facf9fc 13082+
b752ccd1 13083+ if (unlikely(sz >= 0))
1facf9fc 13084+ AuIOErr("xigen error (%zd)\n", sz);
1facf9fc 13085+}
13086+
13087+int au_xigen_new(struct inode *inode)
13088+{
13089+ int err;
13090+ loff_t pos;
13091+ ssize_t sz;
13092+ struct super_block *sb;
13093+ struct au_sbinfo *sbinfo;
13094+ struct file *file;
13095+
13096+ err = 0;
13097+ /* todo: dirty, at mount time */
13098+ if (inode->i_ino == AUFS_ROOT_INO)
13099+ goto out;
13100+ sb = inode->i_sb;
dece6358 13101+ SiMustAnyLock(sb);
1facf9fc 13102+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13103+ goto out;
13104+
13105+ err = -EFBIG;
13106+ pos = inode->i_ino;
13107+ if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
13108+ AuIOErr1("too large i%lld\n", pos);
13109+ goto out;
13110+ }
13111+ pos *= sizeof(inode->i_generation);
13112+
13113+ err = 0;
13114+ sbinfo = au_sbi(sb);
13115+ file = sbinfo->si_xigen;
13116+ BUG_ON(!file);
13117+
c06a8ce3 13118+ if (vfsub_f_size_read(file)
1facf9fc 13119+ < pos + sizeof(inode->i_generation)) {
13120+ inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
13121+ sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
13122+ sizeof(inode->i_generation), &pos);
13123+ } else
13124+ sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
13125+ sizeof(inode->i_generation), &pos);
13126+ if (sz == sizeof(inode->i_generation))
13127+ goto out; /* success */
13128+
13129+ err = sz;
13130+ if (unlikely(sz >= 0)) {
13131+ err = -EIO;
13132+ AuIOErr("xigen error (%zd)\n", sz);
13133+ }
13134+
4f0767ce 13135+out:
1facf9fc 13136+ return err;
13137+}
13138+
062440b3 13139+int au_xigen_set(struct super_block *sb, struct path *path)
1facf9fc 13140+{
13141+ int err;
13142+ struct au_sbinfo *sbinfo;
13143+ struct file *file;
13144+
dece6358
AM
13145+ SiMustWriteLock(sb);
13146+
1facf9fc 13147+ sbinfo = au_sbi(sb);
062440b3 13148+ file = au_xino_create2(sb, path, sbinfo->si_xigen);
1facf9fc 13149+ err = PTR_ERR(file);
13150+ if (IS_ERR(file))
13151+ goto out;
13152+ err = 0;
13153+ if (sbinfo->si_xigen)
13154+ fput(sbinfo->si_xigen);
13155+ sbinfo->si_xigen = file;
13156+
4f0767ce 13157+out:
062440b3 13158+ AuTraceErr(err);
1facf9fc 13159+ return err;
13160+}
13161+
13162+void au_xigen_clr(struct super_block *sb)
13163+{
13164+ struct au_sbinfo *sbinfo;
13165+
dece6358
AM
13166+ SiMustWriteLock(sb);
13167+
1facf9fc 13168+ sbinfo = au_sbi(sb);
13169+ if (sbinfo->si_xigen) {
13170+ fput(sbinfo->si_xigen);
13171+ sbinfo->si_xigen = NULL;
13172+ }
13173+}
13174+
13175+/* ---------------------------------------------------------------------- */
13176+
13177+static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13178+ ino_t dir_ino)
13179+{
13180+ struct dentry *dentry, *d;
13181+ struct inode *inode;
13182+ unsigned int sigen;
13183+
13184+ dentry = NULL;
13185+ inode = ilookup(sb, ino);
13186+ if (!inode)
13187+ goto out;
13188+
13189+ dentry = ERR_PTR(-ESTALE);
13190+ sigen = au_sigen(sb);
5afbbe0d 13191+ if (unlikely(au_is_bad_inode(inode)
1facf9fc 13192+ || IS_DEADDIR(inode)
537831f9 13193+ || sigen != au_iigen(inode, NULL)))
1facf9fc 13194+ goto out_iput;
13195+
13196+ dentry = NULL;
13197+ if (!dir_ino || S_ISDIR(inode->i_mode))
13198+ dentry = d_find_alias(inode);
13199+ else {
027c5e7a 13200+ spin_lock(&inode->i_lock);
c1595e42 13201+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 13202+ spin_lock(&d->d_lock);
1facf9fc 13203+ if (!au_test_anon(d)
5527c038 13204+ && d_inode(d->d_parent)->i_ino == dir_ino) {
027c5e7a
AM
13205+ dentry = dget_dlock(d);
13206+ spin_unlock(&d->d_lock);
1facf9fc 13207+ break;
13208+ }
027c5e7a
AM
13209+ spin_unlock(&d->d_lock);
13210+ }
13211+ spin_unlock(&inode->i_lock);
1facf9fc 13212+ }
027c5e7a 13213+ if (unlikely(dentry && au_digen_test(dentry, sigen))) {
2cbb1c4b 13214+ /* need to refresh */
1facf9fc 13215+ dput(dentry);
2cbb1c4b 13216+ dentry = NULL;
1facf9fc 13217+ }
13218+
4f0767ce 13219+out_iput:
1facf9fc 13220+ iput(inode);
4f0767ce 13221+out:
2cbb1c4b 13222+ AuTraceErrPtr(dentry);
1facf9fc 13223+ return dentry;
13224+}
13225+
13226+/* ---------------------------------------------------------------------- */
13227+
13228+/* todo: dirty? */
13229+/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
4a4d8108
AM
13230+
13231+struct au_compare_mnt_args {
13232+ /* input */
13233+ struct super_block *sb;
13234+
13235+ /* output */
13236+ struct vfsmount *mnt;
13237+};
13238+
13239+static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13240+{
13241+ struct au_compare_mnt_args *a = arg;
13242+
13243+ if (mnt->mnt_sb != a->sb)
13244+ return 0;
13245+ a->mnt = mntget(mnt);
13246+ return 1;
13247+}
13248+
1facf9fc 13249+static struct vfsmount *au_mnt_get(struct super_block *sb)
13250+{
4a4d8108 13251+ int err;
7eafdf33 13252+ struct path root;
4a4d8108
AM
13253+ struct au_compare_mnt_args args = {
13254+ .sb = sb
13255+ };
1facf9fc 13256+
7eafdf33 13257+ get_fs_root(current->fs, &root);
523b37e3 13258+ rcu_read_lock();
7eafdf33 13259+ err = iterate_mounts(au_compare_mnt, &args, root.mnt);
523b37e3 13260+ rcu_read_unlock();
7eafdf33 13261+ path_put(&root);
4a4d8108
AM
13262+ AuDebugOn(!err);
13263+ AuDebugOn(!args.mnt);
13264+ return args.mnt;
1facf9fc 13265+}
13266+
13267+struct au_nfsd_si_lock {
4a4d8108 13268+ unsigned int sigen;
027c5e7a 13269+ aufs_bindex_t bindex, br_id;
1facf9fc 13270+ unsigned char force_lock;
13271+};
13272+
027c5e7a
AM
13273+static int si_nfsd_read_lock(struct super_block *sb,
13274+ struct au_nfsd_si_lock *nsi_lock)
1facf9fc 13275+{
027c5e7a 13276+ int err;
1facf9fc 13277+ aufs_bindex_t bindex;
13278+
13279+ si_read_lock(sb, AuLock_FLUSH);
13280+
13281+ /* branch id may be wrapped around */
027c5e7a 13282+ err = 0;
1facf9fc 13283+ bindex = au_br_index(sb, nsi_lock->br_id);
13284+ if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13285+ goto out; /* success */
13286+
027c5e7a
AM
13287+ err = -ESTALE;
13288+ bindex = -1;
1facf9fc 13289+ if (!nsi_lock->force_lock)
13290+ si_read_unlock(sb);
1facf9fc 13291+
4f0767ce 13292+out:
027c5e7a
AM
13293+ nsi_lock->bindex = bindex;
13294+ return err;
1facf9fc 13295+}
13296+
13297+struct find_name_by_ino {
392086de 13298+ struct dir_context ctx;
1facf9fc 13299+ int called, found;
13300+ ino_t ino;
13301+ char *name;
13302+ int namelen;
13303+};
13304+
13305+static int
392086de
AM
13306+find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13307+ loff_t offset, u64 ino, unsigned int d_type)
1facf9fc 13308+{
392086de
AM
13309+ struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13310+ ctx);
1facf9fc 13311+
13312+ a->called++;
13313+ if (a->ino != ino)
13314+ return 0;
13315+
13316+ memcpy(a->name, name, namelen);
13317+ a->namelen = namelen;
13318+ a->found = 1;
13319+ return 1;
13320+}
13321+
13322+static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13323+ struct au_nfsd_si_lock *nsi_lock)
13324+{
13325+ struct dentry *dentry, *parent;
13326+ struct file *file;
13327+ struct inode *dir;
392086de
AM
13328+ struct find_name_by_ino arg = {
13329+ .ctx = {
2000de60 13330+ .actor = find_name_by_ino
392086de
AM
13331+ }
13332+ };
1facf9fc 13333+ int err;
13334+
13335+ parent = path->dentry;
13336+ if (nsi_lock)
13337+ si_read_unlock(parent->d_sb);
4a4d8108 13338+ file = vfsub_dentry_open(path, au_dir_roflags);
1facf9fc 13339+ dentry = (void *)file;
13340+ if (IS_ERR(file))
13341+ goto out;
13342+
13343+ dentry = ERR_PTR(-ENOMEM);
537831f9 13344+ arg.name = (void *)__get_free_page(GFP_NOFS);
1facf9fc 13345+ if (unlikely(!arg.name))
13346+ goto out_file;
13347+ arg.ino = ino;
13348+ arg.found = 0;
13349+ do {
13350+ arg.called = 0;
13351+ /* smp_mb(); */
392086de 13352+ err = vfsub_iterate_dir(file, &arg.ctx);
1facf9fc 13353+ } while (!err && !arg.found && arg.called);
13354+ dentry = ERR_PTR(err);
13355+ if (unlikely(err))
13356+ goto out_name;
1716fcea
AM
13357+ /* instead of ENOENT */
13358+ dentry = ERR_PTR(-ESTALE);
1facf9fc 13359+ if (!arg.found)
13360+ goto out_name;
13361+
b4510431 13362+ /* do not call vfsub_lkup_one() */
5527c038 13363+ dir = d_inode(parent);
febd17d6 13364+ dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
1facf9fc 13365+ AuTraceErrPtr(dentry);
13366+ if (IS_ERR(dentry))
13367+ goto out_name;
13368+ AuDebugOn(au_test_anon(dentry));
5527c038 13369+ if (unlikely(d_really_is_negative(dentry))) {
1facf9fc 13370+ dput(dentry);
13371+ dentry = ERR_PTR(-ENOENT);
13372+ }
13373+
4f0767ce 13374+out_name:
1c60b727 13375+ free_page((unsigned long)arg.name);
4f0767ce 13376+out_file:
1facf9fc 13377+ fput(file);
4f0767ce 13378+out:
1facf9fc 13379+ if (unlikely(nsi_lock
13380+ && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13381+ if (!IS_ERR(dentry)) {
13382+ dput(dentry);
13383+ dentry = ERR_PTR(-ESTALE);
13384+ }
13385+ AuTraceErrPtr(dentry);
13386+ return dentry;
13387+}
13388+
13389+static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13390+ ino_t dir_ino,
13391+ struct au_nfsd_si_lock *nsi_lock)
13392+{
13393+ struct dentry *dentry;
13394+ struct path path;
13395+
13396+ if (dir_ino != AUFS_ROOT_INO) {
13397+ path.dentry = decode_by_ino(sb, dir_ino, 0);
13398+ dentry = path.dentry;
13399+ if (!path.dentry || IS_ERR(path.dentry))
13400+ goto out;
13401+ AuDebugOn(au_test_anon(path.dentry));
13402+ } else
13403+ path.dentry = dget(sb->s_root);
13404+
13405+ path.mnt = au_mnt_get(sb);
13406+ dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13407+ path_put(&path);
13408+
4f0767ce 13409+out:
1facf9fc 13410+ AuTraceErrPtr(dentry);
13411+ return dentry;
13412+}
13413+
13414+/* ---------------------------------------------------------------------- */
13415+
13416+static int h_acceptable(void *expv, struct dentry *dentry)
13417+{
13418+ return 1;
13419+}
13420+
13421+static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13422+ char *buf, int len, struct super_block *sb)
13423+{
13424+ char *p;
13425+ int n;
13426+ struct path path;
13427+
13428+ p = d_path(h_rootpath, buf, len);
13429+ if (IS_ERR(p))
13430+ goto out;
13431+ n = strlen(p);
13432+
13433+ path.mnt = h_rootpath->mnt;
13434+ path.dentry = h_parent;
13435+ p = d_path(&path, buf, len);
13436+ if (IS_ERR(p))
13437+ goto out;
13438+ if (n != 1)
13439+ p += n;
13440+
13441+ path.mnt = au_mnt_get(sb);
13442+ path.dentry = sb->s_root;
13443+ p = d_path(&path, buf, len - strlen(p));
13444+ mntput(path.mnt);
13445+ if (IS_ERR(p))
13446+ goto out;
13447+ if (n != 1)
13448+ p[strlen(p)] = '/';
13449+
4f0767ce 13450+out:
1facf9fc 13451+ AuTraceErrPtr(p);
13452+ return p;
13453+}
13454+
13455+static
027c5e7a
AM
13456+struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13457+ int fh_len, struct au_nfsd_si_lock *nsi_lock)
1facf9fc 13458+{
13459+ struct dentry *dentry, *h_parent, *root;
13460+ struct super_block *h_sb;
13461+ char *pathname, *p;
13462+ struct vfsmount *h_mnt;
13463+ struct au_branch *br;
13464+ int err;
13465+ struct path path;
13466+
027c5e7a 13467+ br = au_sbr(sb, nsi_lock->bindex);
86dc4139 13468+ h_mnt = au_br_mnt(br);
1facf9fc 13469+ h_sb = h_mnt->mnt_sb;
13470+ /* todo: call lower fh_to_dentry()? fh_to_parent()? */
5afbbe0d 13471+ lockdep_off();
1facf9fc 13472+ h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13473+ fh_len - Fh_tail, fh[Fh_h_type],
13474+ h_acceptable, /*context*/NULL);
5afbbe0d 13475+ lockdep_on();
1facf9fc 13476+ dentry = h_parent;
13477+ if (unlikely(!h_parent || IS_ERR(h_parent))) {
13478+ AuWarn1("%s decode_fh failed, %ld\n",
13479+ au_sbtype(h_sb), PTR_ERR(h_parent));
13480+ goto out;
13481+ }
13482+ dentry = NULL;
13483+ if (unlikely(au_test_anon(h_parent))) {
13484+ AuWarn1("%s decode_fh returned a disconnected dentry\n",
13485+ au_sbtype(h_sb));
13486+ goto out_h_parent;
13487+ }
13488+
13489+ dentry = ERR_PTR(-ENOMEM);
13490+ pathname = (void *)__get_free_page(GFP_NOFS);
13491+ if (unlikely(!pathname))
13492+ goto out_h_parent;
13493+
13494+ root = sb->s_root;
13495+ path.mnt = h_mnt;
13496+ di_read_lock_parent(root, !AuLock_IR);
027c5e7a 13497+ path.dentry = au_h_dptr(root, nsi_lock->bindex);
1facf9fc 13498+ di_read_unlock(root, !AuLock_IR);
13499+ p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13500+ dentry = (void *)p;
13501+ if (IS_ERR(p))
13502+ goto out_pathname;
13503+
13504+ si_read_unlock(sb);
13505+ err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13506+ dentry = ERR_PTR(err);
13507+ if (unlikely(err))
13508+ goto out_relock;
13509+
13510+ dentry = ERR_PTR(-ENOENT);
13511+ AuDebugOn(au_test_anon(path.dentry));
5527c038 13512+ if (unlikely(d_really_is_negative(path.dentry)))
1facf9fc 13513+ goto out_path;
13514+
5527c038 13515+ if (ino != d_inode(path.dentry)->i_ino)
1facf9fc 13516+ dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13517+ else
13518+ dentry = dget(path.dentry);
13519+
4f0767ce 13520+out_path:
1facf9fc 13521+ path_put(&path);
4f0767ce 13522+out_relock:
1facf9fc 13523+ if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13524+ if (!IS_ERR(dentry)) {
13525+ dput(dentry);
13526+ dentry = ERR_PTR(-ESTALE);
13527+ }
4f0767ce 13528+out_pathname:
1c60b727 13529+ free_page((unsigned long)pathname);
4f0767ce 13530+out_h_parent:
1facf9fc 13531+ dput(h_parent);
4f0767ce 13532+out:
1facf9fc 13533+ AuTraceErrPtr(dentry);
13534+ return dentry;
13535+}
13536+
13537+/* ---------------------------------------------------------------------- */
13538+
13539+static struct dentry *
13540+aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13541+ int fh_type)
13542+{
13543+ struct dentry *dentry;
13544+ __u32 *fh = fid->raw;
027c5e7a 13545+ struct au_branch *br;
1facf9fc 13546+ ino_t ino, dir_ino;
1facf9fc 13547+ struct au_nfsd_si_lock nsi_lock = {
1facf9fc 13548+ .force_lock = 0
13549+ };
13550+
1facf9fc 13551+ dentry = ERR_PTR(-ESTALE);
4a4d8108
AM
13552+ /* it should never happen, but the file handle is unreliable */
13553+ if (unlikely(fh_len < Fh_tail))
13554+ goto out;
13555+ nsi_lock.sigen = fh[Fh_sigen];
13556+ nsi_lock.br_id = fh[Fh_br_id];
13557+
1facf9fc 13558+ /* branch id may be wrapped around */
027c5e7a
AM
13559+ br = NULL;
13560+ if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
1facf9fc 13561+ goto out;
13562+ nsi_lock.force_lock = 1;
13563+
13564+ /* is this inode still cached? */
13565+ ino = decode_ino(fh + Fh_ino);
4a4d8108
AM
13566+ /* it should never happen */
13567+ if (unlikely(ino == AUFS_ROOT_INO))
8cdd5066 13568+ goto out_unlock;
4a4d8108 13569+
1facf9fc 13570+ dir_ino = decode_ino(fh + Fh_dir_ino);
13571+ dentry = decode_by_ino(sb, ino, dir_ino);
13572+ if (IS_ERR(dentry))
13573+ goto out_unlock;
13574+ if (dentry)
13575+ goto accept;
13576+
13577+ /* is the parent dir cached? */
027c5e7a 13578+ br = au_sbr(sb, nsi_lock.bindex);
acd2b654 13579+ au_lcnt_inc(&br->br_nfiles);
1facf9fc 13580+ dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13581+ if (IS_ERR(dentry))
13582+ goto out_unlock;
13583+ if (dentry)
13584+ goto accept;
13585+
13586+ /* lookup path */
027c5e7a 13587+ dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
1facf9fc 13588+ if (IS_ERR(dentry))
13589+ goto out_unlock;
13590+ if (unlikely(!dentry))
13591+ /* todo?: make it ESTALE */
13592+ goto out_unlock;
13593+
4f0767ce 13594+accept:
027c5e7a 13595+ if (!au_digen_test(dentry, au_sigen(sb))
5527c038 13596+ && d_inode(dentry)->i_generation == fh[Fh_igen])
1facf9fc 13597+ goto out_unlock; /* success */
13598+
13599+ dput(dentry);
13600+ dentry = ERR_PTR(-ESTALE);
4f0767ce 13601+out_unlock:
027c5e7a 13602+ if (br)
acd2b654 13603+ au_lcnt_dec(&br->br_nfiles);
1facf9fc 13604+ si_read_unlock(sb);
4f0767ce 13605+out:
1facf9fc 13606+ AuTraceErrPtr(dentry);
13607+ return dentry;
13608+}
13609+
13610+#if 0 /* reserved for future use */
13611+/* support subtreecheck option */
13612+static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13613+ int fh_len, int fh_type)
13614+{
13615+ struct dentry *parent;
13616+ __u32 *fh = fid->raw;
13617+ ino_t dir_ino;
13618+
13619+ dir_ino = decode_ino(fh + Fh_dir_ino);
13620+ parent = decode_by_ino(sb, dir_ino, 0);
13621+ if (IS_ERR(parent))
13622+ goto out;
13623+ if (!parent)
13624+ parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13625+ dir_ino, fh, fh_len);
13626+
4f0767ce 13627+out:
1facf9fc 13628+ AuTraceErrPtr(parent);
13629+ return parent;
13630+}
13631+#endif
13632+
13633+/* ---------------------------------------------------------------------- */
13634+
0c3ec466
AM
13635+static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13636+ struct inode *dir)
1facf9fc 13637+{
13638+ int err;
0c3ec466 13639+ aufs_bindex_t bindex;
1facf9fc 13640+ struct super_block *sb, *h_sb;
0c3ec466
AM
13641+ struct dentry *dentry, *parent, *h_parent;
13642+ struct inode *h_dir;
1facf9fc 13643+ struct au_branch *br;
13644+
1facf9fc 13645+ err = -ENOSPC;
13646+ if (unlikely(*max_len <= Fh_tail)) {
13647+ AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13648+ goto out;
13649+ }
13650+
13651+ err = FILEID_ROOT;
0c3ec466
AM
13652+ if (inode->i_ino == AUFS_ROOT_INO) {
13653+ AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
1facf9fc 13654+ goto out;
13655+ }
13656+
1facf9fc 13657+ h_parent = NULL;
0c3ec466
AM
13658+ sb = inode->i_sb;
13659+ err = si_read_lock(sb, AuLock_FLUSH);
027c5e7a
AM
13660+ if (unlikely(err))
13661+ goto out;
13662+
1facf9fc 13663+#ifdef CONFIG_AUFS_DEBUG
13664+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13665+ AuWarn1("NFS-exporting requires xino\n");
13666+#endif
027c5e7a 13667+ err = -EIO;
0c3ec466
AM
13668+ parent = NULL;
13669+ ii_read_lock_child(inode);
5afbbe0d 13670+ bindex = au_ibtop(inode);
0c3ec466 13671+ if (!dir) {
c1595e42 13672+ dentry = d_find_any_alias(inode);
0c3ec466
AM
13673+ if (unlikely(!dentry))
13674+ goto out_unlock;
13675+ AuDebugOn(au_test_anon(dentry));
13676+ parent = dget_parent(dentry);
13677+ dput(dentry);
13678+ if (unlikely(!parent))
13679+ goto out_unlock;
5527c038
JR
13680+ if (d_really_is_positive(parent))
13681+ dir = d_inode(parent);
1facf9fc 13682+ }
0c3ec466
AM
13683+
13684+ ii_read_lock_parent(dir);
13685+ h_dir = au_h_iptr(dir, bindex);
13686+ ii_read_unlock(dir);
13687+ if (unlikely(!h_dir))
13688+ goto out_parent;
c1595e42 13689+ h_parent = d_find_any_alias(h_dir);
1facf9fc 13690+ if (unlikely(!h_parent))
0c3ec466 13691+ goto out_hparent;
1facf9fc 13692+
13693+ err = -EPERM;
13694+ br = au_sbr(sb, bindex);
86dc4139 13695+ h_sb = au_br_sb(br);
1facf9fc 13696+ if (unlikely(!h_sb->s_export_op)) {
13697+ AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
0c3ec466 13698+ goto out_hparent;
1facf9fc 13699+ }
13700+
13701+ fh[Fh_br_id] = br->br_id;
13702+ fh[Fh_sigen] = au_sigen(sb);
13703+ encode_ino(fh + Fh_ino, inode->i_ino);
0c3ec466 13704+ encode_ino(fh + Fh_dir_ino, dir->i_ino);
1facf9fc 13705+ fh[Fh_igen] = inode->i_generation;
13706+
13707+ *max_len -= Fh_tail;
13708+ fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13709+ max_len,
13710+ /*connectable or subtreecheck*/0);
13711+ err = fh[Fh_h_type];
13712+ *max_len += Fh_tail;
13713+ /* todo: macros? */
1716fcea 13714+ if (err != FILEID_INVALID)
1facf9fc 13715+ err = 99;
13716+ else
13717+ AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13718+
0c3ec466 13719+out_hparent:
1facf9fc 13720+ dput(h_parent);
0c3ec466 13721+out_parent:
1facf9fc 13722+ dput(parent);
0c3ec466
AM
13723+out_unlock:
13724+ ii_read_unlock(inode);
13725+ si_read_unlock(sb);
4f0767ce 13726+out:
1facf9fc 13727+ if (unlikely(err < 0))
1716fcea 13728+ err = FILEID_INVALID;
1facf9fc 13729+ return err;
13730+}
13731+
13732+/* ---------------------------------------------------------------------- */
13733+
4a4d8108
AM
13734+static int aufs_commit_metadata(struct inode *inode)
13735+{
13736+ int err;
13737+ aufs_bindex_t bindex;
13738+ struct super_block *sb;
13739+ struct inode *h_inode;
13740+ int (*f)(struct inode *inode);
13741+
13742+ sb = inode->i_sb;
e49829fe 13743+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 13744+ ii_write_lock_child(inode);
5afbbe0d 13745+ bindex = au_ibtop(inode);
4a4d8108
AM
13746+ AuDebugOn(bindex < 0);
13747+ h_inode = au_h_iptr(inode, bindex);
13748+
13749+ f = h_inode->i_sb->s_export_op->commit_metadata;
13750+ if (f)
13751+ err = f(h_inode);
13752+ else {
13753+ struct writeback_control wbc = {
13754+ .sync_mode = WB_SYNC_ALL,
13755+ .nr_to_write = 0 /* metadata only */
13756+ };
13757+
13758+ err = sync_inode(h_inode, &wbc);
13759+ }
13760+
13761+ au_cpup_attr_timesizes(inode);
13762+ ii_write_unlock(inode);
13763+ si_read_unlock(sb);
13764+ return err;
13765+}
13766+
13767+/* ---------------------------------------------------------------------- */
13768+
1facf9fc 13769+static struct export_operations aufs_export_op = {
4a4d8108 13770+ .fh_to_dentry = aufs_fh_to_dentry,
1facf9fc 13771+ /* .fh_to_parent = aufs_fh_to_parent, */
4a4d8108
AM
13772+ .encode_fh = aufs_encode_fh,
13773+ .commit_metadata = aufs_commit_metadata
1facf9fc 13774+};
13775+
13776+void au_export_init(struct super_block *sb)
13777+{
13778+ struct au_sbinfo *sbinfo;
13779+ __u32 u;
13780+
5afbbe0d
AM
13781+ BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13782+ && IS_MODULE(CONFIG_EXPORTFS),
13783+ AUFS_NAME ": unsupported configuration "
13784+ "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13785+
1facf9fc 13786+ sb->s_export_op = &aufs_export_op;
13787+ sbinfo = au_sbi(sb);
13788+ sbinfo->si_xigen = NULL;
13789+ get_random_bytes(&u, sizeof(u));
13790+ BUILD_BUG_ON(sizeof(u) != sizeof(int));
13791+ atomic_set(&sbinfo->si_xigen_next, u);
13792+}
076b876e
AM
13793diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13794--- /usr/share/empty/fs/aufs/fhsm.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 13795+++ linux/fs/aufs/fhsm.c 2019-01-28 14:36:12.219084659 +0100
062440b3 13796@@ -0,0 +1,427 @@
cd7a4cd9 13797+// SPDX-License-Identifier: GPL-2.0
076b876e 13798+/*
b00004a5 13799+ * Copyright (C) 2011-2018 Junjiro R. Okajima
076b876e
AM
13800+ *
13801+ * This program, aufs is free software; you can redistribute it and/or modify
13802+ * it under the terms of the GNU General Public License as published by
13803+ * the Free Software Foundation; either version 2 of the License, or
13804+ * (at your option) any later version.
13805+ *
13806+ * This program is distributed in the hope that it will be useful,
13807+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13808+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13809+ * GNU General Public License for more details.
13810+ *
13811+ * You should have received a copy of the GNU General Public License
13812+ * along with this program; if not, write to the Free Software
13813+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
13814+ */
13815+
13816+/*
13817+ * File-based Hierarchy Storage Management
13818+ */
13819+
13820+#include <linux/anon_inodes.h>
13821+#include <linux/poll.h>
13822+#include <linux/seq_file.h>
13823+#include <linux/statfs.h>
13824+#include "aufs.h"
13825+
c1595e42
JR
13826+static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13827+{
13828+ struct au_sbinfo *sbinfo;
13829+ struct au_fhsm *fhsm;
13830+
13831+ SiMustAnyLock(sb);
13832+
13833+ sbinfo = au_sbi(sb);
13834+ fhsm = &sbinfo->si_fhsm;
13835+ AuDebugOn(!fhsm);
13836+ return fhsm->fhsm_bottom;
13837+}
13838+
13839+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13840+{
13841+ struct au_sbinfo *sbinfo;
13842+ struct au_fhsm *fhsm;
13843+
13844+ SiMustWriteLock(sb);
13845+
13846+ sbinfo = au_sbi(sb);
13847+ fhsm = &sbinfo->si_fhsm;
13848+ AuDebugOn(!fhsm);
13849+ fhsm->fhsm_bottom = bindex;
13850+}
13851+
13852+/* ---------------------------------------------------------------------- */
13853+
076b876e
AM
13854+static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13855+{
13856+ struct au_br_fhsm *bf;
13857+
13858+ bf = br->br_fhsm;
13859+ MtxMustLock(&bf->bf_lock);
13860+
13861+ return !bf->bf_readable
13862+ || time_after(jiffies,
13863+ bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13864+}
13865+
13866+/* ---------------------------------------------------------------------- */
13867+
13868+static void au_fhsm_notify(struct super_block *sb, int val)
13869+{
13870+ struct au_sbinfo *sbinfo;
13871+ struct au_fhsm *fhsm;
13872+
13873+ SiMustAnyLock(sb);
13874+
13875+ sbinfo = au_sbi(sb);
13876+ fhsm = &sbinfo->si_fhsm;
13877+ if (au_fhsm_pid(fhsm)
13878+ && atomic_read(&fhsm->fhsm_readable) != -1) {
13879+ atomic_set(&fhsm->fhsm_readable, val);
13880+ if (val)
13881+ wake_up(&fhsm->fhsm_wqh);
13882+ }
13883+}
13884+
13885+static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13886+ struct aufs_stfs *rstfs, int do_lock, int do_notify)
13887+{
13888+ int err;
13889+ struct au_branch *br;
13890+ struct au_br_fhsm *bf;
13891+
13892+ br = au_sbr(sb, bindex);
13893+ AuDebugOn(au_br_rdonly(br));
13894+ bf = br->br_fhsm;
13895+ AuDebugOn(!bf);
13896+
13897+ if (do_lock)
13898+ mutex_lock(&bf->bf_lock);
13899+ else
13900+ MtxMustLock(&bf->bf_lock);
13901+
13902+ /* sb->s_root for NFS is unreliable */
13903+ err = au_br_stfs(br, &bf->bf_stfs);
13904+ if (unlikely(err)) {
13905+ AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13906+ goto out;
13907+ }
13908+
13909+ bf->bf_jiffy = jiffies;
13910+ bf->bf_readable = 1;
13911+ if (do_notify)
13912+ au_fhsm_notify(sb, /*val*/1);
13913+ if (rstfs)
13914+ *rstfs = bf->bf_stfs;
13915+
13916+out:
13917+ if (do_lock)
13918+ mutex_unlock(&bf->bf_lock);
13919+ au_fhsm_notify(sb, /*val*/1);
13920+
13921+ return err;
13922+}
13923+
13924+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13925+{
13926+ int err;
076b876e
AM
13927+ struct au_sbinfo *sbinfo;
13928+ struct au_fhsm *fhsm;
13929+ struct au_branch *br;
13930+ struct au_br_fhsm *bf;
13931+
13932+ AuDbg("b%d, force %d\n", bindex, force);
13933+ SiMustAnyLock(sb);
13934+
13935+ sbinfo = au_sbi(sb);
13936+ fhsm = &sbinfo->si_fhsm;
c1595e42
JR
13937+ if (!au_ftest_si(sbinfo, FHSM)
13938+ || fhsm->fhsm_bottom == bindex)
076b876e
AM
13939+ return;
13940+
13941+ br = au_sbr(sb, bindex);
13942+ bf = br->br_fhsm;
13943+ AuDebugOn(!bf);
13944+ mutex_lock(&bf->bf_lock);
13945+ if (force
13946+ || au_fhsm_pid(fhsm)
13947+ || au_fhsm_test_jiffy(sbinfo, br))
13948+ err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13949+ /*do_notify*/1);
13950+ mutex_unlock(&bf->bf_lock);
13951+}
13952+
13953+void au_fhsm_wrote_all(struct super_block *sb, int force)
13954+{
5afbbe0d 13955+ aufs_bindex_t bindex, bbot;
076b876e
AM
13956+ struct au_branch *br;
13957+
13958+ /* exclude the bottom */
5afbbe0d
AM
13959+ bbot = au_fhsm_bottom(sb);
13960+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
13961+ br = au_sbr(sb, bindex);
13962+ if (au_br_fhsm(br->br_perm))
13963+ au_fhsm_wrote(sb, bindex, force);
13964+ }
13965+}
13966+
13967+/* ---------------------------------------------------------------------- */
13968+
be118d29 13969+static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
076b876e 13970+{
be118d29 13971+ __poll_t mask;
076b876e
AM
13972+ struct au_sbinfo *sbinfo;
13973+ struct au_fhsm *fhsm;
13974+
13975+ mask = 0;
13976+ sbinfo = file->private_data;
13977+ fhsm = &sbinfo->si_fhsm;
13978+ poll_wait(file, &fhsm->fhsm_wqh, wait);
13979+ if (atomic_read(&fhsm->fhsm_readable))
be118d29 13980+ mask = EPOLLIN /* | EPOLLRDNORM */;
076b876e 13981+
b00004a5
AM
13982+ if (!mask)
13983+ AuDbg("mask 0x%x\n", mask);
076b876e
AM
13984+ return mask;
13985+}
13986+
13987+static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13988+ struct aufs_stfs *stfs, __s16 brid)
13989+{
13990+ int err;
13991+
13992+ err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13993+ if (!err)
13994+ err = __put_user(brid, &stbr->brid);
13995+ if (unlikely(err))
13996+ err = -EFAULT;
13997+
13998+ return err;
13999+}
14000+
14001+static ssize_t au_fhsm_do_read(struct super_block *sb,
14002+ struct aufs_stbr __user *stbr, size_t count)
14003+{
14004+ ssize_t err;
14005+ int nstbr;
5afbbe0d 14006+ aufs_bindex_t bindex, bbot;
076b876e
AM
14007+ struct au_branch *br;
14008+ struct au_br_fhsm *bf;
14009+
14010+ /* except the bottom branch */
14011+ err = 0;
14012+ nstbr = 0;
5afbbe0d
AM
14013+ bbot = au_fhsm_bottom(sb);
14014+ for (bindex = 0; !err && bindex < bbot; bindex++) {
076b876e
AM
14015+ br = au_sbr(sb, bindex);
14016+ if (!au_br_fhsm(br->br_perm))
14017+ continue;
14018+
14019+ bf = br->br_fhsm;
14020+ mutex_lock(&bf->bf_lock);
14021+ if (bf->bf_readable) {
14022+ err = -EFAULT;
14023+ if (count >= sizeof(*stbr))
14024+ err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
14025+ br->br_id);
14026+ if (!err) {
14027+ bf->bf_readable = 0;
14028+ count -= sizeof(*stbr);
14029+ nstbr++;
14030+ }
14031+ }
14032+ mutex_unlock(&bf->bf_lock);
14033+ }
14034+ if (!err)
14035+ err = sizeof(*stbr) * nstbr;
14036+
14037+ return err;
14038+}
14039+
14040+static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
14041+ loff_t *pos)
14042+{
14043+ ssize_t err;
14044+ int readable;
5afbbe0d 14045+ aufs_bindex_t nfhsm, bindex, bbot;
076b876e
AM
14046+ struct au_sbinfo *sbinfo;
14047+ struct au_fhsm *fhsm;
14048+ struct au_branch *br;
14049+ struct super_block *sb;
14050+
14051+ err = 0;
14052+ sbinfo = file->private_data;
14053+ fhsm = &sbinfo->si_fhsm;
14054+need_data:
14055+ spin_lock_irq(&fhsm->fhsm_wqh.lock);
14056+ if (!atomic_read(&fhsm->fhsm_readable)) {
14057+ if (vfsub_file_flags(file) & O_NONBLOCK)
14058+ err = -EAGAIN;
14059+ else
14060+ err = wait_event_interruptible_locked_irq
14061+ (fhsm->fhsm_wqh,
14062+ atomic_read(&fhsm->fhsm_readable));
14063+ }
14064+ spin_unlock_irq(&fhsm->fhsm_wqh.lock);
14065+ if (unlikely(err))
14066+ goto out;
14067+
14068+ /* sb may already be dead */
14069+ au_rw_read_lock(&sbinfo->si_rwsem);
14070+ readable = atomic_read(&fhsm->fhsm_readable);
14071+ if (readable > 0) {
14072+ sb = sbinfo->si_sb;
14073+ AuDebugOn(!sb);
14074+ /* exclude the bottom branch */
14075+ nfhsm = 0;
5afbbe0d
AM
14076+ bbot = au_fhsm_bottom(sb);
14077+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
14078+ br = au_sbr(sb, bindex);
14079+ if (au_br_fhsm(br->br_perm))
14080+ nfhsm++;
14081+ }
14082+ err = -EMSGSIZE;
14083+ if (nfhsm * sizeof(struct aufs_stbr) <= count) {
14084+ atomic_set(&fhsm->fhsm_readable, 0);
14085+ err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
14086+ count);
14087+ }
14088+ }
14089+ au_rw_read_unlock(&sbinfo->si_rwsem);
14090+ if (!readable)
14091+ goto need_data;
14092+
14093+out:
14094+ return err;
14095+}
14096+
14097+static int au_fhsm_release(struct inode *inode, struct file *file)
14098+{
14099+ struct au_sbinfo *sbinfo;
14100+ struct au_fhsm *fhsm;
14101+
14102+ /* sb may already be dead */
14103+ sbinfo = file->private_data;
14104+ fhsm = &sbinfo->si_fhsm;
14105+ spin_lock(&fhsm->fhsm_spin);
14106+ fhsm->fhsm_pid = 0;
14107+ spin_unlock(&fhsm->fhsm_spin);
14108+ kobject_put(&sbinfo->si_kobj);
14109+
14110+ return 0;
14111+}
14112+
14113+static const struct file_operations au_fhsm_fops = {
14114+ .owner = THIS_MODULE,
14115+ .llseek = noop_llseek,
14116+ .read = au_fhsm_read,
14117+ .poll = au_fhsm_poll,
14118+ .release = au_fhsm_release
14119+};
14120+
14121+int au_fhsm_fd(struct super_block *sb, int oflags)
14122+{
14123+ int err, fd;
14124+ struct au_sbinfo *sbinfo;
14125+ struct au_fhsm *fhsm;
14126+
14127+ err = -EPERM;
14128+ if (unlikely(!capable(CAP_SYS_ADMIN)))
14129+ goto out;
14130+
14131+ err = -EINVAL;
14132+ if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
14133+ goto out;
14134+
14135+ err = 0;
14136+ sbinfo = au_sbi(sb);
14137+ fhsm = &sbinfo->si_fhsm;
14138+ spin_lock(&fhsm->fhsm_spin);
14139+ if (!fhsm->fhsm_pid)
14140+ fhsm->fhsm_pid = current->pid;
14141+ else
14142+ err = -EBUSY;
14143+ spin_unlock(&fhsm->fhsm_spin);
14144+ if (unlikely(err))
14145+ goto out;
14146+
14147+ oflags |= O_RDONLY;
14148+ /* oflags |= FMODE_NONOTIFY; */
14149+ fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
14150+ err = fd;
14151+ if (unlikely(fd < 0))
14152+ goto out_pid;
14153+
acd2b654 14154+ /* succeed regardless 'fhsm' status */
076b876e
AM
14155+ kobject_get(&sbinfo->si_kobj);
14156+ si_noflush_read_lock(sb);
14157+ if (au_ftest_si(sbinfo, FHSM))
14158+ au_fhsm_wrote_all(sb, /*force*/0);
14159+ si_read_unlock(sb);
14160+ goto out; /* success */
14161+
14162+out_pid:
14163+ spin_lock(&fhsm->fhsm_spin);
14164+ fhsm->fhsm_pid = 0;
14165+ spin_unlock(&fhsm->fhsm_spin);
14166+out:
14167+ AuTraceErr(err);
14168+ return err;
14169+}
14170+
14171+/* ---------------------------------------------------------------------- */
14172+
14173+int au_fhsm_br_alloc(struct au_branch *br)
14174+{
14175+ int err;
14176+
14177+ err = 0;
14178+ br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14179+ if (br->br_fhsm)
14180+ au_br_fhsm_init(br->br_fhsm);
14181+ else
14182+ err = -ENOMEM;
14183+
14184+ return err;
14185+}
14186+
14187+/* ---------------------------------------------------------------------- */
14188+
14189+void au_fhsm_fin(struct super_block *sb)
14190+{
14191+ au_fhsm_notify(sb, /*val*/-1);
14192+}
14193+
14194+void au_fhsm_init(struct au_sbinfo *sbinfo)
14195+{
14196+ struct au_fhsm *fhsm;
14197+
14198+ fhsm = &sbinfo->si_fhsm;
14199+ spin_lock_init(&fhsm->fhsm_spin);
14200+ init_waitqueue_head(&fhsm->fhsm_wqh);
14201+ atomic_set(&fhsm->fhsm_readable, 0);
14202+ fhsm->fhsm_expire
14203+ = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
c1595e42 14204+ fhsm->fhsm_bottom = -1;
076b876e
AM
14205+}
14206+
14207+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14208+{
14209+ sbinfo->si_fhsm.fhsm_expire
14210+ = msecs_to_jiffies(sec * MSEC_PER_SEC);
14211+}
14212+
14213+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14214+{
14215+ unsigned int u;
14216+
14217+ if (!au_ftest_si(sbinfo, FHSM))
14218+ return;
14219+
14220+ u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14221+ if (u != AUFS_FHSM_CACHE_DEF_SEC)
14222+ seq_printf(seq, ",fhsm_sec=%u", u);
14223+}
7f207e10
AM
14224diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14225--- /usr/share/empty/fs/aufs/file.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 14226+++ linux/fs/aufs/file.c 2019-01-28 14:36:12.219084659 +0100
acd2b654 14227@@ -0,0 +1,863 @@
cd7a4cd9 14228+// SPDX-License-Identifier: GPL-2.0
1facf9fc 14229+/*
b00004a5 14230+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 14231+ *
14232+ * This program, aufs is free software; you can redistribute it and/or modify
14233+ * it under the terms of the GNU General Public License as published by
14234+ * the Free Software Foundation; either version 2 of the License, or
14235+ * (at your option) any later version.
dece6358
AM
14236+ *
14237+ * This program is distributed in the hope that it will be useful,
14238+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14239+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14240+ * GNU General Public License for more details.
14241+ *
14242+ * You should have received a copy of the GNU General Public License
523b37e3 14243+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 14244+ */
14245+
14246+/*
4a4d8108 14247+ * handling file/dir, and address_space operation
1facf9fc 14248+ */
14249+
7eafdf33
AM
14250+#ifdef CONFIG_AUFS_DEBUG
14251+#include <linux/migrate.h>
14252+#endif
4a4d8108 14253+#include <linux/pagemap.h>
1facf9fc 14254+#include "aufs.h"
14255+
4a4d8108
AM
14256+/* drop flags for writing */
14257+unsigned int au_file_roflags(unsigned int flags)
14258+{
14259+ flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14260+ flags |= O_RDONLY | O_NOATIME;
14261+ return flags;
14262+}
14263+
14264+/* common functions to regular file and dir */
14265+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 14266+ struct file *file, int force_wr)
1facf9fc 14267+{
1308ab2a 14268+ struct file *h_file;
4a4d8108
AM
14269+ struct dentry *h_dentry;
14270+ struct inode *h_inode;
14271+ struct super_block *sb;
14272+ struct au_branch *br;
14273+ struct path h_path;
b912730e 14274+ int err;
1facf9fc 14275+
4a4d8108
AM
14276+ /* a race condition can happen between open and unlink/rmdir */
14277+ h_file = ERR_PTR(-ENOENT);
14278+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 14279+ if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
4a4d8108 14280+ goto out;
5527c038 14281+ h_inode = d_inode(h_dentry);
027c5e7a
AM
14282+ spin_lock(&h_dentry->d_lock);
14283+ err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
5527c038 14284+ /* || !d_inode(dentry)->i_nlink */
027c5e7a
AM
14285+ ;
14286+ spin_unlock(&h_dentry->d_lock);
14287+ if (unlikely(err))
4a4d8108 14288+ goto out;
1facf9fc 14289+
4a4d8108
AM
14290+ sb = dentry->d_sb;
14291+ br = au_sbr(sb, bindex);
b912730e
AM
14292+ err = au_br_test_oflag(flags, br);
14293+ h_file = ERR_PTR(err);
14294+ if (unlikely(err))
027c5e7a 14295+ goto out;
1facf9fc 14296+
4a4d8108 14297+ /* drop flags for writing */
5527c038 14298+ if (au_test_ro(sb, bindex, d_inode(dentry))) {
392086de
AM
14299+ if (force_wr && !(flags & O_WRONLY))
14300+ force_wr = 0;
4a4d8108 14301+ flags = au_file_roflags(flags);
392086de
AM
14302+ if (force_wr) {
14303+ h_file = ERR_PTR(-EROFS);
14304+ flags = au_file_roflags(flags);
14305+ if (unlikely(vfsub_native_ro(h_inode)
14306+ || IS_APPEND(h_inode)))
14307+ goto out;
14308+ flags &= ~O_ACCMODE;
14309+ flags |= O_WRONLY;
14310+ }
14311+ }
4a4d8108 14312+ flags &= ~O_CREAT;
acd2b654 14313+ au_lcnt_inc(&br->br_nfiles);
4a4d8108 14314+ h_path.dentry = h_dentry;
86dc4139 14315+ h_path.mnt = au_br_mnt(br);
38d290e6 14316+ h_file = vfsub_dentry_open(&h_path, flags);
4a4d8108
AM
14317+ if (IS_ERR(h_file))
14318+ goto out_br;
dece6358 14319+
b912730e 14320+ if (flags & __FMODE_EXEC) {
4a4d8108
AM
14321+ err = deny_write_access(h_file);
14322+ if (unlikely(err)) {
14323+ fput(h_file);
14324+ h_file = ERR_PTR(err);
14325+ goto out_br;
14326+ }
14327+ }
953406b4 14328+ fsnotify_open(h_file);
4a4d8108 14329+ goto out; /* success */
1facf9fc 14330+
4f0767ce 14331+out_br:
acd2b654 14332+ au_lcnt_dec(&br->br_nfiles);
4f0767ce 14333+out:
4a4d8108
AM
14334+ return h_file;
14335+}
1308ab2a 14336+
076b876e
AM
14337+static int au_cmoo(struct dentry *dentry)
14338+{
8b6a4947 14339+ int err, cmoo, matched;
076b876e
AM
14340+ unsigned int udba;
14341+ struct path h_path;
14342+ struct au_pin pin;
14343+ struct au_cp_generic cpg = {
14344+ .dentry = dentry,
14345+ .bdst = -1,
14346+ .bsrc = -1,
14347+ .len = -1,
14348+ .pin = &pin,
14349+ .flags = AuCpup_DTIME | AuCpup_HOPEN
14350+ };
7e9cd9fe 14351+ struct inode *delegated;
076b876e
AM
14352+ struct super_block *sb;
14353+ struct au_sbinfo *sbinfo;
14354+ struct au_fhsm *fhsm;
14355+ pid_t pid;
14356+ struct au_branch *br;
14357+ struct dentry *parent;
14358+ struct au_hinode *hdir;
14359+
14360+ DiMustWriteLock(dentry);
5527c038 14361+ IiMustWriteLock(d_inode(dentry));
076b876e
AM
14362+
14363+ err = 0;
14364+ if (IS_ROOT(dentry))
14365+ goto out;
5afbbe0d 14366+ cpg.bsrc = au_dbtop(dentry);
076b876e
AM
14367+ if (!cpg.bsrc)
14368+ goto out;
14369+
14370+ sb = dentry->d_sb;
14371+ sbinfo = au_sbi(sb);
14372+ fhsm = &sbinfo->si_fhsm;
14373+ pid = au_fhsm_pid(fhsm);
8b6a4947
AM
14374+ rcu_read_lock();
14375+ matched = (pid
14376+ && (current->pid == pid
14377+ || rcu_dereference(current->real_parent)->pid == pid));
14378+ rcu_read_unlock();
14379+ if (matched)
076b876e
AM
14380+ goto out;
14381+
14382+ br = au_sbr(sb, cpg.bsrc);
14383+ cmoo = au_br_cmoo(br->br_perm);
14384+ if (!cmoo)
14385+ goto out;
7e9cd9fe 14386+ if (!d_is_reg(dentry))
076b876e
AM
14387+ cmoo &= AuBrAttr_COO_ALL;
14388+ if (!cmoo)
14389+ goto out;
14390+
14391+ parent = dget_parent(dentry);
14392+ di_write_lock_parent(parent);
14393+ err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14394+ cpg.bdst = err;
14395+ if (unlikely(err < 0)) {
14396+ err = 0; /* there is no upper writable branch */
14397+ goto out_dgrade;
14398+ }
14399+ AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14400+
14401+ /* do not respect the coo attrib for the target branch */
14402+ err = au_cpup_dirs(dentry, cpg.bdst);
14403+ if (unlikely(err))
14404+ goto out_dgrade;
14405+
14406+ di_downgrade_lock(parent, AuLock_IR);
14407+ udba = au_opt_udba(sb);
14408+ err = au_pin(&pin, dentry, cpg.bdst, udba,
14409+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14410+ if (unlikely(err))
14411+ goto out_parent;
14412+
14413+ err = au_sio_cpup_simple(&cpg);
14414+ au_unpin(&pin);
14415+ if (unlikely(err))
14416+ goto out_parent;
14417+ if (!(cmoo & AuBrWAttr_MOO))
14418+ goto out_parent; /* success */
14419+
14420+ err = au_pin(&pin, dentry, cpg.bsrc, udba,
14421+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14422+ if (unlikely(err))
14423+ goto out_parent;
14424+
14425+ h_path.mnt = au_br_mnt(br);
14426+ h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
5527c038 14427+ hdir = au_hi(d_inode(parent), cpg.bsrc);
076b876e
AM
14428+ delegated = NULL;
14429+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14430+ au_unpin(&pin);
14431+ /* todo: keep h_dentry or not? */
14432+ if (unlikely(err == -EWOULDBLOCK)) {
14433+ pr_warn("cannot retry for NFSv4 delegation"
14434+ " for an internal unlink\n");
14435+ iput(delegated);
14436+ }
14437+ if (unlikely(err)) {
14438+ pr_err("unlink %pd after coo failed (%d), ignored\n",
14439+ dentry, err);
14440+ err = 0;
14441+ }
14442+ goto out_parent; /* success */
14443+
14444+out_dgrade:
14445+ di_downgrade_lock(parent, AuLock_IR);
14446+out_parent:
14447+ di_read_unlock(parent, AuLock_IR);
14448+ dput(parent);
14449+out:
14450+ AuTraceErr(err);
14451+ return err;
14452+}
14453+
b912730e 14454+int au_do_open(struct file *file, struct au_do_open_args *args)
1facf9fc 14455+{
8b6a4947 14456+ int err, aopen = args->aopen;
1facf9fc 14457+ struct dentry *dentry;
076b876e 14458+ struct au_finfo *finfo;
1308ab2a 14459+
8b6a4947 14460+ if (!aopen)
b912730e
AM
14461+ err = au_finfo_init(file, args->fidir);
14462+ else {
14463+ lockdep_off();
14464+ err = au_finfo_init(file, args->fidir);
14465+ lockdep_on();
14466+ }
4a4d8108
AM
14467+ if (unlikely(err))
14468+ goto out;
1facf9fc 14469+
2000de60 14470+ dentry = file->f_path.dentry;
b912730e 14471+ AuDebugOn(IS_ERR_OR_NULL(dentry));
8b6a4947
AM
14472+ di_write_lock_child(dentry);
14473+ err = au_cmoo(dentry);
14474+ di_downgrade_lock(dentry, AuLock_IR);
14475+ if (!err) {
14476+ if (!aopen)
b912730e 14477+ err = args->open(file, vfsub_file_flags(file), NULL);
8b6a4947
AM
14478+ else {
14479+ lockdep_off();
acd2b654
AM
14480+ err = args->open(file, vfsub_file_flags(file),
14481+ args->h_file);
8b6a4947
AM
14482+ lockdep_on();
14483+ }
b912730e 14484+ }
8b6a4947 14485+ di_read_unlock(dentry, AuLock_IR);
1facf9fc 14486+
076b876e
AM
14487+ finfo = au_fi(file);
14488+ if (!err) {
14489+ finfo->fi_file = file;
8b6a4947
AM
14490+ au_hbl_add(&finfo->fi_hlist,
14491+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
076b876e 14492+ }
8b6a4947 14493+ if (!aopen)
b912730e
AM
14494+ fi_write_unlock(file);
14495+ else {
14496+ lockdep_off();
14497+ fi_write_unlock(file);
14498+ lockdep_on();
14499+ }
4a4d8108 14500+ if (unlikely(err)) {
076b876e 14501+ finfo->fi_hdir = NULL;
1c60b727 14502+ au_finfo_fin(file);
1308ab2a 14503+ }
4a4d8108 14504+
4f0767ce 14505+out:
8b6a4947 14506+ AuTraceErr(err);
1308ab2a 14507+ return err;
14508+}
dece6358 14509+
4a4d8108 14510+int au_reopen_nondir(struct file *file)
1308ab2a 14511+{
4a4d8108 14512+ int err;
5afbbe0d 14513+ aufs_bindex_t btop;
4a4d8108 14514+ struct dentry *dentry;
acd2b654 14515+ struct au_branch *br;
4a4d8108 14516+ struct file *h_file, *h_file_tmp;
1308ab2a 14517+
2000de60 14518+ dentry = file->f_path.dentry;
5afbbe0d 14519+ btop = au_dbtop(dentry);
acd2b654 14520+ br = au_sbr(dentry->d_sb, btop);
4a4d8108 14521+ h_file_tmp = NULL;
5afbbe0d 14522+ if (au_fbtop(file) == btop) {
4a4d8108
AM
14523+ h_file = au_hf_top(file);
14524+ if (file->f_mode == h_file->f_mode)
14525+ return 0; /* success */
14526+ h_file_tmp = h_file;
14527+ get_file(h_file_tmp);
acd2b654 14528+ au_lcnt_inc(&br->br_nfiles);
5afbbe0d 14529+ au_set_h_fptr(file, btop, NULL);
4a4d8108
AM
14530+ }
14531+ AuDebugOn(au_fi(file)->fi_hdir);
86dc4139
AM
14532+ /*
14533+ * it can happen
14534+ * file exists on both of rw and ro
5afbbe0d 14535+ * open --> dbtop and fbtop are both 0
86dc4139
AM
14536+ * prepend a branch as rw, "rw" become ro
14537+ * remove rw/file
14538+ * delete the top branch, "rw" becomes rw again
5afbbe0d
AM
14539+ * --> dbtop is 1, fbtop is still 0
14540+ * write --> fbtop is 0 but dbtop is 1
86dc4139 14541+ */
5afbbe0d 14542+ /* AuDebugOn(au_fbtop(file) < btop); */
1308ab2a 14543+
5afbbe0d 14544+ h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
392086de 14545+ file, /*force_wr*/0);
4a4d8108 14546+ err = PTR_ERR(h_file);
86dc4139
AM
14547+ if (IS_ERR(h_file)) {
14548+ if (h_file_tmp) {
acd2b654 14549+ /* revert */
5afbbe0d 14550+ au_set_h_fptr(file, btop, h_file_tmp);
86dc4139
AM
14551+ h_file_tmp = NULL;
14552+ }
4a4d8108 14553+ goto out; /* todo: close all? */
86dc4139 14554+ }
4a4d8108
AM
14555+
14556+ err = 0;
5afbbe0d
AM
14557+ au_set_fbtop(file, btop);
14558+ au_set_h_fptr(file, btop, h_file);
4a4d8108
AM
14559+ au_update_figen(file);
14560+ /* todo: necessary? */
14561+ /* file->f_ra = h_file->f_ra; */
14562+
4f0767ce 14563+out:
acd2b654 14564+ if (h_file_tmp) {
4a4d8108 14565+ fput(h_file_tmp);
acd2b654
AM
14566+ au_lcnt_dec(&br->br_nfiles);
14567+ }
4a4d8108 14568+ return err;
1facf9fc 14569+}
14570+
1308ab2a 14571+/* ---------------------------------------------------------------------- */
14572+
4a4d8108
AM
14573+static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14574+ struct dentry *hi_wh)
1facf9fc 14575+{
4a4d8108 14576+ int err;
5afbbe0d 14577+ aufs_bindex_t btop;
4a4d8108
AM
14578+ struct au_dinfo *dinfo;
14579+ struct dentry *h_dentry;
14580+ struct au_hdentry *hdp;
1facf9fc 14581+
2000de60 14582+ dinfo = au_di(file->f_path.dentry);
4a4d8108 14583+ AuRwMustWriteLock(&dinfo->di_rwsem);
dece6358 14584+
5afbbe0d
AM
14585+ btop = dinfo->di_btop;
14586+ dinfo->di_btop = btgt;
14587+ hdp = au_hdentry(dinfo, btgt);
14588+ h_dentry = hdp->hd_dentry;
14589+ hdp->hd_dentry = hi_wh;
4a4d8108 14590+ err = au_reopen_nondir(file);
5afbbe0d
AM
14591+ hdp->hd_dentry = h_dentry;
14592+ dinfo->di_btop = btop;
1facf9fc 14593+
1facf9fc 14594+ return err;
14595+}
14596+
4a4d8108 14597+static int au_ready_to_write_wh(struct file *file, loff_t len,
86dc4139 14598+ aufs_bindex_t bcpup, struct au_pin *pin)
1facf9fc 14599+{
4a4d8108 14600+ int err;
027c5e7a 14601+ struct inode *inode, *h_inode;
c2b27bf2
AM
14602+ struct dentry *h_dentry, *hi_wh;
14603+ struct au_cp_generic cpg = {
2000de60 14604+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14605+ .bdst = bcpup,
14606+ .bsrc = -1,
14607+ .len = len,
14608+ .pin = pin
14609+ };
1facf9fc 14610+
5afbbe0d 14611+ au_update_dbtop(cpg.dentry);
5527c038 14612+ inode = d_inode(cpg.dentry);
027c5e7a 14613+ h_inode = NULL;
5afbbe0d
AM
14614+ if (au_dbtop(cpg.dentry) <= bcpup
14615+ && au_dbbot(cpg.dentry) >= bcpup) {
c2b27bf2 14616+ h_dentry = au_h_dptr(cpg.dentry, bcpup);
5527c038
JR
14617+ if (h_dentry && d_is_positive(h_dentry))
14618+ h_inode = d_inode(h_dentry);
027c5e7a 14619+ }
4a4d8108 14620+ hi_wh = au_hi_wh(inode, bcpup);
027c5e7a 14621+ if (!hi_wh && !h_inode)
c2b27bf2 14622+ err = au_sio_cpup_wh(&cpg, file);
4a4d8108
AM
14623+ else
14624+ /* already copied-up after unlink */
14625+ err = au_reopen_wh(file, bcpup, hi_wh);
1facf9fc 14626+
4a4d8108 14627+ if (!err
38d290e6
JR
14628+ && (inode->i_nlink > 1
14629+ || (inode->i_state & I_LINKABLE))
c2b27bf2
AM
14630+ && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14631+ au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
1308ab2a 14632+
dece6358 14633+ return err;
1facf9fc 14634+}
14635+
4a4d8108
AM
14636+/*
14637+ * prepare the @file for writing.
14638+ */
14639+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
1facf9fc 14640+{
4a4d8108 14641+ int err;
5afbbe0d 14642+ aufs_bindex_t dbtop;
c1595e42 14643+ struct dentry *parent;
86dc4139 14644+ struct inode *inode;
1facf9fc 14645+ struct super_block *sb;
4a4d8108 14646+ struct file *h_file;
c2b27bf2 14647+ struct au_cp_generic cpg = {
2000de60 14648+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14649+ .bdst = -1,
14650+ .bsrc = -1,
14651+ .len = len,
14652+ .pin = pin,
14653+ .flags = AuCpup_DTIME
14654+ };
1facf9fc 14655+
c2b27bf2 14656+ sb = cpg.dentry->d_sb;
5527c038 14657+ inode = d_inode(cpg.dentry);
5afbbe0d 14658+ cpg.bsrc = au_fbtop(file);
c2b27bf2 14659+ err = au_test_ro(sb, cpg.bsrc, inode);
4a4d8108 14660+ if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
c2b27bf2
AM
14661+ err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14662+ /*flags*/0);
1facf9fc 14663+ goto out;
4a4d8108 14664+ }
1facf9fc 14665+
027c5e7a 14666+ /* need to cpup or reopen */
c2b27bf2 14667+ parent = dget_parent(cpg.dentry);
4a4d8108 14668+ di_write_lock_parent(parent);
c2b27bf2
AM
14669+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14670+ cpg.bdst = err;
4a4d8108
AM
14671+ if (unlikely(err < 0))
14672+ goto out_dgrade;
14673+ err = 0;
14674+
c2b27bf2
AM
14675+ if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14676+ err = au_cpup_dirs(cpg.dentry, cpg.bdst);
1facf9fc 14677+ if (unlikely(err))
4a4d8108
AM
14678+ goto out_dgrade;
14679+ }
14680+
c2b27bf2 14681+ err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108
AM
14682+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14683+ if (unlikely(err))
14684+ goto out_dgrade;
14685+
5afbbe0d
AM
14686+ dbtop = au_dbtop(cpg.dentry);
14687+ if (dbtop <= cpg.bdst)
c2b27bf2 14688+ cpg.bsrc = cpg.bdst;
027c5e7a 14689+
5afbbe0d 14690+ if (dbtop <= cpg.bdst /* just reopen */
c2b27bf2 14691+ || !d_unhashed(cpg.dentry) /* copyup and reopen */
027c5e7a 14692+ ) {
392086de 14693+ h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
86dc4139 14694+ if (IS_ERR(h_file))
027c5e7a 14695+ err = PTR_ERR(h_file);
86dc4139 14696+ else {
027c5e7a 14697+ di_downgrade_lock(parent, AuLock_IR);
5afbbe0d 14698+ if (dbtop > cpg.bdst)
c2b27bf2 14699+ err = au_sio_cpup_simple(&cpg);
027c5e7a
AM
14700+ if (!err)
14701+ err = au_reopen_nondir(file);
c2b27bf2 14702+ au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
027c5e7a 14703+ }
027c5e7a
AM
14704+ } else { /* copyup as wh and reopen */
14705+ /*
14706+ * since writable hfsplus branch is not supported,
14707+ * h_open_pre/post() are unnecessary.
14708+ */
c2b27bf2 14709+ err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
4a4d8108 14710+ di_downgrade_lock(parent, AuLock_IR);
4a4d8108 14711+ }
4a4d8108
AM
14712+
14713+ if (!err) {
14714+ au_pin_set_parent_lflag(pin, /*lflag*/0);
14715+ goto out_dput; /* success */
14716+ }
14717+ au_unpin(pin);
14718+ goto out_unlock;
1facf9fc 14719+
4f0767ce 14720+out_dgrade:
4a4d8108 14721+ di_downgrade_lock(parent, AuLock_IR);
4f0767ce 14722+out_unlock:
4a4d8108 14723+ di_read_unlock(parent, AuLock_IR);
4f0767ce 14724+out_dput:
4a4d8108 14725+ dput(parent);
4f0767ce 14726+out:
1facf9fc 14727+ return err;
14728+}
14729+
4a4d8108
AM
14730+/* ---------------------------------------------------------------------- */
14731+
14732+int au_do_flush(struct file *file, fl_owner_t id,
14733+ int (*flush)(struct file *file, fl_owner_t id))
1facf9fc 14734+{
4a4d8108 14735+ int err;
1facf9fc 14736+ struct super_block *sb;
4a4d8108 14737+ struct inode *inode;
1facf9fc 14738+
c06a8ce3
AM
14739+ inode = file_inode(file);
14740+ sb = inode->i_sb;
4a4d8108
AM
14741+ si_noflush_read_lock(sb);
14742+ fi_read_lock(file);
b752ccd1 14743+ ii_read_lock_child(inode);
1facf9fc 14744+
4a4d8108
AM
14745+ err = flush(file, id);
14746+ au_cpup_attr_timesizes(inode);
1facf9fc 14747+
b752ccd1 14748+ ii_read_unlock(inode);
4a4d8108 14749+ fi_read_unlock(file);
1308ab2a 14750+ si_read_unlock(sb);
dece6358 14751+ return err;
1facf9fc 14752+}
14753+
4a4d8108
AM
14754+/* ---------------------------------------------------------------------- */
14755+
14756+static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
1facf9fc 14757+{
4a4d8108 14758+ int err;
4a4d8108
AM
14759+ struct au_pin pin;
14760+ struct au_finfo *finfo;
c2b27bf2 14761+ struct dentry *parent, *hi_wh;
4a4d8108 14762+ struct inode *inode;
1facf9fc 14763+ struct super_block *sb;
c2b27bf2 14764+ struct au_cp_generic cpg = {
2000de60 14765+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14766+ .bdst = -1,
14767+ .bsrc = -1,
14768+ .len = -1,
14769+ .pin = &pin,
14770+ .flags = AuCpup_DTIME
14771+ };
1facf9fc 14772+
4a4d8108
AM
14773+ FiMustWriteLock(file);
14774+
14775+ err = 0;
14776+ finfo = au_fi(file);
c2b27bf2 14777+ sb = cpg.dentry->d_sb;
5527c038 14778+ inode = d_inode(cpg.dentry);
5afbbe0d 14779+ cpg.bdst = au_ibtop(inode);
c2b27bf2 14780+ if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
1308ab2a 14781+ goto out;
dece6358 14782+
c2b27bf2
AM
14783+ parent = dget_parent(cpg.dentry);
14784+ if (au_test_ro(sb, cpg.bdst, inode)) {
4a4d8108 14785+ di_read_lock_parent(parent, !AuLock_IR);
c2b27bf2
AM
14786+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14787+ cpg.bdst = err;
4a4d8108
AM
14788+ di_read_unlock(parent, !AuLock_IR);
14789+ if (unlikely(err < 0))
14790+ goto out_parent;
14791+ err = 0;
1facf9fc 14792+ }
1facf9fc 14793+
4a4d8108 14794+ di_read_lock_parent(parent, AuLock_IR);
c2b27bf2 14795+ hi_wh = au_hi_wh(inode, cpg.bdst);
7f207e10
AM
14796+ if (!S_ISDIR(inode->i_mode)
14797+ && au_opt_test(au_mntflags(sb), PLINK)
4a4d8108 14798+ && au_plink_test(inode)
c2b27bf2 14799+ && !d_unhashed(cpg.dentry)
5afbbe0d 14800+ && cpg.bdst < au_dbtop(cpg.dentry)) {
c2b27bf2 14801+ err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
4a4d8108
AM
14802+ if (unlikely(err))
14803+ goto out_unlock;
14804+
14805+ /* always superio. */
c2b27bf2 14806+ err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108 14807+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 14808+ if (!err) {
c2b27bf2 14809+ err = au_sio_cpup_simple(&cpg);
367653fa
AM
14810+ au_unpin(&pin);
14811+ }
4a4d8108
AM
14812+ } else if (hi_wh) {
14813+ /* already copied-up after unlink */
c2b27bf2 14814+ err = au_reopen_wh(file, cpg.bdst, hi_wh);
4a4d8108
AM
14815+ *need_reopen = 0;
14816+ }
1facf9fc 14817+
4f0767ce 14818+out_unlock:
4a4d8108 14819+ di_read_unlock(parent, AuLock_IR);
4f0767ce 14820+out_parent:
4a4d8108 14821+ dput(parent);
4f0767ce 14822+out:
1308ab2a 14823+ return err;
dece6358 14824+}
1facf9fc 14825+
4a4d8108 14826+static void au_do_refresh_dir(struct file *file)
dece6358 14827+{
5afbbe0d 14828+ aufs_bindex_t bindex, bbot, new_bindex, brid;
4a4d8108
AM
14829+ struct au_hfile *p, tmp, *q;
14830+ struct au_finfo *finfo;
1308ab2a 14831+ struct super_block *sb;
4a4d8108 14832+ struct au_fidir *fidir;
1facf9fc 14833+
4a4d8108 14834+ FiMustWriteLock(file);
1facf9fc 14835+
2000de60 14836+ sb = file->f_path.dentry->d_sb;
4a4d8108
AM
14837+ finfo = au_fi(file);
14838+ fidir = finfo->fi_hdir;
14839+ AuDebugOn(!fidir);
14840+ p = fidir->fd_hfile + finfo->fi_btop;
14841+ brid = p->hf_br->br_id;
5afbbe0d
AM
14842+ bbot = fidir->fd_bbot;
14843+ for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
4a4d8108
AM
14844+ if (!p->hf_file)
14845+ continue;
1308ab2a 14846+
4a4d8108
AM
14847+ new_bindex = au_br_index(sb, p->hf_br->br_id);
14848+ if (new_bindex == bindex)
14849+ continue;
14850+ if (new_bindex < 0) {
14851+ au_set_h_fptr(file, bindex, NULL);
14852+ continue;
14853+ }
1308ab2a 14854+
4a4d8108
AM
14855+ /* swap two lower inode, and loop again */
14856+ q = fidir->fd_hfile + new_bindex;
14857+ tmp = *q;
14858+ *q = *p;
14859+ *p = tmp;
14860+ if (tmp.hf_file) {
14861+ bindex--;
14862+ p--;
14863+ }
14864+ }
1308ab2a 14865+
4a4d8108 14866+ p = fidir->fd_hfile;
2000de60 14867+ if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
5afbbe0d
AM
14868+ bbot = au_sbbot(sb);
14869+ for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
4a4d8108
AM
14870+ finfo->fi_btop++, p++)
14871+ if (p->hf_file) {
c06a8ce3 14872+ if (file_inode(p->hf_file))
4a4d8108 14873+ break;
1c60b727 14874+ au_hfput(p, /*execed*/0);
4a4d8108
AM
14875+ }
14876+ } else {
5afbbe0d
AM
14877+ bbot = au_br_index(sb, brid);
14878+ for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
4a4d8108
AM
14879+ finfo->fi_btop++, p++)
14880+ if (p->hf_file)
1c60b727 14881+ au_hfput(p, /*execed*/0);
5afbbe0d 14882+ bbot = au_sbbot(sb);
4a4d8108 14883+ }
1308ab2a 14884+
5afbbe0d
AM
14885+ p = fidir->fd_hfile + bbot;
14886+ for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
4a4d8108
AM
14887+ fidir->fd_bbot--, p--)
14888+ if (p->hf_file) {
c06a8ce3 14889+ if (file_inode(p->hf_file))
4a4d8108 14890+ break;
1c60b727 14891+ au_hfput(p, /*execed*/0);
4a4d8108
AM
14892+ }
14893+ AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
1308ab2a 14894+}
14895+
4a4d8108
AM
14896+/*
14897+ * after branch manipulating, refresh the file.
14898+ */
14899+static int refresh_file(struct file *file, int (*reopen)(struct file *file))
1facf9fc 14900+{
e2f27e51 14901+ int err, need_reopen, nbr;
5afbbe0d 14902+ aufs_bindex_t bbot, bindex;
4a4d8108 14903+ struct dentry *dentry;
e2f27e51 14904+ struct super_block *sb;
1308ab2a 14905+ struct au_finfo *finfo;
4a4d8108 14906+ struct au_hfile *hfile;
1facf9fc 14907+
2000de60 14908+ dentry = file->f_path.dentry;
e2f27e51
AM
14909+ sb = dentry->d_sb;
14910+ nbr = au_sbbot(sb) + 1;
1308ab2a 14911+ finfo = au_fi(file);
4a4d8108
AM
14912+ if (!finfo->fi_hdir) {
14913+ hfile = &finfo->fi_htop;
14914+ AuDebugOn(!hfile->hf_file);
e2f27e51 14915+ bindex = au_br_index(sb, hfile->hf_br->br_id);
4a4d8108
AM
14916+ AuDebugOn(bindex < 0);
14917+ if (bindex != finfo->fi_btop)
5afbbe0d 14918+ au_set_fbtop(file, bindex);
4a4d8108 14919+ } else {
e2f27e51 14920+ err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
4a4d8108
AM
14921+ if (unlikely(err))
14922+ goto out;
14923+ au_do_refresh_dir(file);
14924+ }
1facf9fc 14925+
4a4d8108
AM
14926+ err = 0;
14927+ need_reopen = 1;
14928+ if (!au_test_mmapped(file))
14929+ err = au_file_refresh_by_inode(file, &need_reopen);
e2f27e51
AM
14930+ if (finfo->fi_hdir)
14931+ /* harmless if err */
14932+ au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
027c5e7a 14933+ if (!err && need_reopen && !d_unlinked(dentry))
4a4d8108
AM
14934+ err = reopen(file);
14935+ if (!err) {
14936+ au_update_figen(file);
14937+ goto out; /* success */
14938+ }
14939+
14940+ /* error, close all lower files */
14941+ if (finfo->fi_hdir) {
5afbbe0d
AM
14942+ bbot = au_fbbot_dir(file);
14943+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
4a4d8108
AM
14944+ au_set_h_fptr(file, bindex, NULL);
14945+ }
1facf9fc 14946+
4f0767ce 14947+out:
1facf9fc 14948+ return err;
14949+}
14950+
4a4d8108
AM
14951+/* common function to regular file and dir */
14952+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
521ced18 14953+ int wlock, unsigned int fi_lsc)
dece6358 14954+{
1308ab2a 14955+ int err;
4a4d8108 14956+ unsigned int sigen, figen;
5afbbe0d 14957+ aufs_bindex_t btop;
4a4d8108
AM
14958+ unsigned char pseudo_link;
14959+ struct dentry *dentry;
14960+ struct inode *inode;
1facf9fc 14961+
4a4d8108 14962+ err = 0;
2000de60 14963+ dentry = file->f_path.dentry;
5527c038 14964+ inode = d_inode(dentry);
4a4d8108 14965+ sigen = au_sigen(dentry->d_sb);
521ced18 14966+ fi_write_lock_nested(file, fi_lsc);
4a4d8108 14967+ figen = au_figen(file);
521ced18
JR
14968+ if (!fi_lsc)
14969+ di_write_lock_child(dentry);
14970+ else
14971+ di_write_lock_child2(dentry);
5afbbe0d
AM
14972+ btop = au_dbtop(dentry);
14973+ pseudo_link = (btop != au_ibtop(inode));
14974+ if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
4a4d8108
AM
14975+ if (!wlock) {
14976+ di_downgrade_lock(dentry, AuLock_IR);
14977+ fi_downgrade_lock(file);
14978+ }
14979+ goto out; /* success */
14980+ }
dece6358 14981+
4a4d8108 14982+ AuDbg("sigen %d, figen %d\n", sigen, figen);
027c5e7a 14983+ if (au_digen_test(dentry, sigen)) {
4a4d8108 14984+ err = au_reval_dpath(dentry, sigen);
027c5e7a 14985+ AuDebugOn(!err && au_digen_test(dentry, sigen));
4a4d8108 14986+ }
dece6358 14987+
027c5e7a
AM
14988+ if (!err)
14989+ err = refresh_file(file, reopen);
4a4d8108
AM
14990+ if (!err) {
14991+ if (!wlock) {
14992+ di_downgrade_lock(dentry, AuLock_IR);
14993+ fi_downgrade_lock(file);
14994+ }
14995+ } else {
14996+ di_write_unlock(dentry);
14997+ fi_write_unlock(file);
14998+ }
1facf9fc 14999+
4f0767ce 15000+out:
1308ab2a 15001+ return err;
15002+}
1facf9fc 15003+
4a4d8108
AM
15004+/* ---------------------------------------------------------------------- */
15005+
15006+/* cf. aufs_nopage() */
15007+/* for madvise(2) */
15008+static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
1308ab2a 15009+{
4a4d8108
AM
15010+ unlock_page(page);
15011+ return 0;
15012+}
1facf9fc 15013+
4a4d8108 15014+/* it will never be called, but necessary to support O_DIRECT */
5afbbe0d 15015+static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
4a4d8108 15016+{ BUG(); return 0; }
1facf9fc 15017+
4a4d8108
AM
15018+/* they will never be called. */
15019+#ifdef CONFIG_AUFS_DEBUG
15020+static int aufs_write_begin(struct file *file, struct address_space *mapping,
15021+ loff_t pos, unsigned len, unsigned flags,
15022+ struct page **pagep, void **fsdata)
15023+{ AuUnsupport(); return 0; }
15024+static int aufs_write_end(struct file *file, struct address_space *mapping,
15025+ loff_t pos, unsigned len, unsigned copied,
15026+ struct page *page, void *fsdata)
15027+{ AuUnsupport(); return 0; }
15028+static int aufs_writepage(struct page *page, struct writeback_control *wbc)
15029+{ AuUnsupport(); return 0; }
1308ab2a 15030+
4a4d8108
AM
15031+static int aufs_set_page_dirty(struct page *page)
15032+{ AuUnsupport(); return 0; }
392086de
AM
15033+static void aufs_invalidatepage(struct page *page, unsigned int offset,
15034+ unsigned int length)
4a4d8108
AM
15035+{ AuUnsupport(); }
15036+static int aufs_releasepage(struct page *page, gfp_t gfp)
15037+{ AuUnsupport(); return 0; }
79b8bda9 15038+#if 0 /* called by memory compaction regardless file */
4a4d8108 15039+static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
7eafdf33 15040+ struct page *page, enum migrate_mode mode)
4a4d8108 15041+{ AuUnsupport(); return 0; }
79b8bda9 15042+#endif
e2f27e51
AM
15043+static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
15044+{ AuUnsupport(); return true; }
15045+static void aufs_putback_page(struct page *page)
15046+{ AuUnsupport(); }
4a4d8108
AM
15047+static int aufs_launder_page(struct page *page)
15048+{ AuUnsupport(); return 0; }
15049+static int aufs_is_partially_uptodate(struct page *page,
38d290e6
JR
15050+ unsigned long from,
15051+ unsigned long count)
4a4d8108 15052+{ AuUnsupport(); return 0; }
392086de
AM
15053+static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
15054+ bool *writeback)
15055+{ AuUnsupport(); }
4a4d8108
AM
15056+static int aufs_error_remove_page(struct address_space *mapping,
15057+ struct page *page)
15058+{ AuUnsupport(); return 0; }
b4510431
AM
15059+static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
15060+ sector_t *span)
15061+{ AuUnsupport(); return 0; }
15062+static void aufs_swap_deactivate(struct file *file)
15063+{ AuUnsupport(); }
4a4d8108
AM
15064+#endif /* CONFIG_AUFS_DEBUG */
15065+
15066+const struct address_space_operations aufs_aop = {
15067+ .readpage = aufs_readpage,
15068+ .direct_IO = aufs_direct_IO,
4a4d8108
AM
15069+#ifdef CONFIG_AUFS_DEBUG
15070+ .writepage = aufs_writepage,
4a4d8108
AM
15071+ /* no writepages, because of writepage */
15072+ .set_page_dirty = aufs_set_page_dirty,
15073+ /* no readpages, because of readpage */
15074+ .write_begin = aufs_write_begin,
15075+ .write_end = aufs_write_end,
15076+ /* no bmap, no block device */
15077+ .invalidatepage = aufs_invalidatepage,
15078+ .releasepage = aufs_releasepage,
79b8bda9
AM
15079+ /* is fallback_migrate_page ok? */
15080+ /* .migratepage = aufs_migratepage, */
e2f27e51
AM
15081+ .isolate_page = aufs_isolate_page,
15082+ .putback_page = aufs_putback_page,
4a4d8108
AM
15083+ .launder_page = aufs_launder_page,
15084+ .is_partially_uptodate = aufs_is_partially_uptodate,
392086de 15085+ .is_dirty_writeback = aufs_is_dirty_writeback,
b4510431
AM
15086+ .error_remove_page = aufs_error_remove_page,
15087+ .swap_activate = aufs_swap_activate,
15088+ .swap_deactivate = aufs_swap_deactivate
4a4d8108 15089+#endif /* CONFIG_AUFS_DEBUG */
dece6358 15090+};
7f207e10
AM
15091diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
15092--- /usr/share/empty/fs/aufs/file.h 1970-01-01 01:00:00.000000000 +0100
e49925d1
JR
15093+++ linux/fs/aufs/file.h 2019-01-28 14:36:12.219084659 +0100
15094@@ -0,0 +1,342 @@
062440b3 15095+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 15096+/*
b00004a5 15097+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
15098+ *
15099+ * This program, aufs is free software; you can redistribute it and/or modify
15100+ * it under the terms of the GNU General Public License as published by
15101+ * the Free Software Foundation; either version 2 of the License, or
15102+ * (at your option) any later version.
15103+ *
15104+ * This program is distributed in the hope that it will be useful,
15105+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15106+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15107+ * GNU General Public License for more details.
15108+ *
15109+ * You should have received a copy of the GNU General Public License
523b37e3 15110+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 15111+ */
1facf9fc 15112+
4a4d8108
AM
15113+/*
15114+ * file operations
15115+ */
1facf9fc 15116+
4a4d8108
AM
15117+#ifndef __AUFS_FILE_H__
15118+#define __AUFS_FILE_H__
1facf9fc 15119+
4a4d8108 15120+#ifdef __KERNEL__
1facf9fc 15121+
2cbb1c4b 15122+#include <linux/file.h>
4a4d8108 15123+#include <linux/fs.h>
3c1bdaff 15124+#include <linux/mm_types.h>
4a4d8108 15125+#include <linux/poll.h>
4a4d8108 15126+#include "rwsem.h"
1facf9fc 15127+
4a4d8108
AM
15128+struct au_branch;
15129+struct au_hfile {
15130+ struct file *hf_file;
15131+ struct au_branch *hf_br;
15132+};
1facf9fc 15133+
4a4d8108
AM
15134+struct au_vdir;
15135+struct au_fidir {
15136+ aufs_bindex_t fd_bbot;
15137+ aufs_bindex_t fd_nent;
15138+ struct au_vdir *fd_vdir_cache;
15139+ struct au_hfile fd_hfile[];
15140+};
1facf9fc 15141+
4a4d8108 15142+static inline int au_fidir_sz(int nent)
dece6358 15143+{
4f0767ce
JR
15144+ AuDebugOn(nent < 0);
15145+ return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
4a4d8108 15146+}
1facf9fc 15147+
4a4d8108
AM
15148+struct au_finfo {
15149+ atomic_t fi_generation;
dece6358 15150+
4a4d8108
AM
15151+ struct au_rwsem fi_rwsem;
15152+ aufs_bindex_t fi_btop;
15153+
15154+ /* do not union them */
15155+ struct { /* for non-dir */
15156+ struct au_hfile fi_htop;
2cbb1c4b 15157+ atomic_t fi_mmapped;
4a4d8108
AM
15158+ };
15159+ struct au_fidir *fi_hdir; /* for dir only */
523b37e3 15160+
8b6a4947 15161+ struct hlist_bl_node fi_hlist;
1c60b727 15162+ struct file *fi_file; /* very ugly */
e49925d1 15163+ struct rcu_head rcu;
4a4d8108 15164+} ____cacheline_aligned_in_smp;
1facf9fc 15165+
4a4d8108 15166+/* ---------------------------------------------------------------------- */
1facf9fc 15167+
4a4d8108
AM
15168+/* file.c */
15169+extern const struct address_space_operations aufs_aop;
15170+unsigned int au_file_roflags(unsigned int flags);
15171+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 15172+ struct file *file, int force_wr);
b912730e 15173+struct au_do_open_args {
8b6a4947 15174+ int aopen;
b912730e
AM
15175+ int (*open)(struct file *file, int flags,
15176+ struct file *h_file);
15177+ struct au_fidir *fidir;
15178+ struct file *h_file;
15179+};
15180+int au_do_open(struct file *file, struct au_do_open_args *args);
4a4d8108
AM
15181+int au_reopen_nondir(struct file *file);
15182+struct au_pin;
15183+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15184+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
521ced18 15185+ int wlock, unsigned int fi_lsc);
4a4d8108
AM
15186+int au_do_flush(struct file *file, fl_owner_t id,
15187+ int (*flush)(struct file *file, fl_owner_t id));
1facf9fc 15188+
4a4d8108
AM
15189+/* poll.c */
15190+#ifdef CONFIG_AUFS_POLL
cd7a4cd9 15191+__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
4a4d8108 15192+#endif
1facf9fc 15193+
4a4d8108
AM
15194+#ifdef CONFIG_AUFS_BR_HFSPLUS
15195+/* hfsplus.c */
392086de
AM
15196+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15197+ int force_wr);
4a4d8108
AM
15198+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15199+ struct file *h_file);
15200+#else
c1595e42
JR
15201+AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15202+ aufs_bindex_t bindex, int force_wr)
4a4d8108
AM
15203+AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15204+ struct file *h_file);
15205+#endif
1facf9fc 15206+
4a4d8108
AM
15207+/* f_op.c */
15208+extern const struct file_operations aufs_file_fop;
b912730e 15209+int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
4a4d8108 15210+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
521ced18 15211+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
4a4d8108 15212+
4a4d8108 15213+/* finfo.c */
f0c0a007 15214+void au_hfput(struct au_hfile *hf, int execed);
4a4d8108
AM
15215+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15216+ struct file *h_file);
1facf9fc 15217+
4a4d8108 15218+void au_update_figen(struct file *file);
4a4d8108 15219+struct au_fidir *au_fidir_alloc(struct super_block *sb);
e2f27e51 15220+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
1facf9fc 15221+
4a4d8108 15222+void au_fi_init_once(void *_fi);
1c60b727 15223+void au_finfo_fin(struct file *file);
4a4d8108 15224+int au_finfo_init(struct file *file, struct au_fidir *fidir);
1facf9fc 15225+
4a4d8108
AM
15226+/* ioctl.c */
15227+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
15228+#ifdef CONFIG_COMPAT
15229+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15230+ unsigned long arg);
c2b27bf2
AM
15231+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15232+ unsigned long arg);
b752ccd1 15233+#endif
1facf9fc 15234+
4a4d8108 15235+/* ---------------------------------------------------------------------- */
1facf9fc 15236+
4a4d8108
AM
15237+static inline struct au_finfo *au_fi(struct file *file)
15238+{
38d290e6 15239+ return file->private_data;
4a4d8108 15240+}
1facf9fc 15241+
4a4d8108 15242+/* ---------------------------------------------------------------------- */
1facf9fc 15243+
8b6a4947
AM
15244+#define fi_read_lock(f) au_rw_read_lock(&au_fi(f)->fi_rwsem)
15245+#define fi_write_lock(f) au_rw_write_lock(&au_fi(f)->fi_rwsem)
15246+#define fi_read_trylock(f) au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15247+#define fi_write_trylock(f) au_rw_write_trylock(&au_fi(f)->fi_rwsem)
4a4d8108 15248+/*
8b6a4947
AM
15249+#define fi_read_trylock_nested(f) \
15250+ au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15251+#define fi_write_trylock_nested(f) \
15252+ au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15253+*/
15254+
15255+#define fi_read_unlock(f) au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15256+#define fi_write_unlock(f) au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15257+#define fi_downgrade_lock(f) au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
1308ab2a 15258+
521ced18
JR
15259+/* lock subclass for finfo */
15260+enum {
15261+ AuLsc_FI_1,
15262+ AuLsc_FI_2
15263+};
15264+
15265+static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15266+{
15267+ au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15268+}
15269+
15270+static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15271+{
15272+ au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15273+}
15274+
15275+/*
15276+ * fi_read_lock_1, fi_write_lock_1,
15277+ * fi_read_lock_2, fi_write_lock_2
15278+ */
15279+#define AuReadLockFunc(name) \
15280+static inline void fi_read_lock_##name(struct file *f) \
15281+{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15282+
15283+#define AuWriteLockFunc(name) \
15284+static inline void fi_write_lock_##name(struct file *f) \
15285+{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15286+
15287+#define AuRWLockFuncs(name) \
15288+ AuReadLockFunc(name) \
15289+ AuWriteLockFunc(name)
15290+
15291+AuRWLockFuncs(1);
15292+AuRWLockFuncs(2);
15293+
15294+#undef AuReadLockFunc
15295+#undef AuWriteLockFunc
15296+#undef AuRWLockFuncs
15297+
4a4d8108
AM
15298+#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15299+#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15300+#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
1facf9fc 15301+
1308ab2a 15302+/* ---------------------------------------------------------------------- */
15303+
4a4d8108 15304+/* todo: hard/soft set? */
5afbbe0d 15305+static inline aufs_bindex_t au_fbtop(struct file *file)
dece6358 15306+{
4a4d8108
AM
15307+ FiMustAnyLock(file);
15308+ return au_fi(file)->fi_btop;
15309+}
dece6358 15310+
5afbbe0d 15311+static inline aufs_bindex_t au_fbbot_dir(struct file *file)
4a4d8108
AM
15312+{
15313+ FiMustAnyLock(file);
15314+ AuDebugOn(!au_fi(file)->fi_hdir);
15315+ return au_fi(file)->fi_hdir->fd_bbot;
15316+}
1facf9fc 15317+
4a4d8108
AM
15318+static inline struct au_vdir *au_fvdir_cache(struct file *file)
15319+{
15320+ FiMustAnyLock(file);
15321+ AuDebugOn(!au_fi(file)->fi_hdir);
15322+ return au_fi(file)->fi_hdir->fd_vdir_cache;
15323+}
1facf9fc 15324+
5afbbe0d 15325+static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
15326+{
15327+ FiMustWriteLock(file);
15328+ au_fi(file)->fi_btop = bindex;
15329+}
1facf9fc 15330+
5afbbe0d 15331+static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
15332+{
15333+ FiMustWriteLock(file);
15334+ AuDebugOn(!au_fi(file)->fi_hdir);
15335+ au_fi(file)->fi_hdir->fd_bbot = bindex;
15336+}
1308ab2a 15337+
4a4d8108
AM
15338+static inline void au_set_fvdir_cache(struct file *file,
15339+ struct au_vdir *vdir_cache)
15340+{
15341+ FiMustWriteLock(file);
15342+ AuDebugOn(!au_fi(file)->fi_hdir);
15343+ au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15344+}
dece6358 15345+
4a4d8108
AM
15346+static inline struct file *au_hf_top(struct file *file)
15347+{
15348+ FiMustAnyLock(file);
15349+ AuDebugOn(au_fi(file)->fi_hdir);
15350+ return au_fi(file)->fi_htop.hf_file;
15351+}
1facf9fc 15352+
4a4d8108
AM
15353+static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15354+{
15355+ FiMustAnyLock(file);
15356+ AuDebugOn(!au_fi(file)->fi_hdir);
15357+ return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
dece6358
AM
15358+}
15359+
4a4d8108
AM
15360+/* todo: memory barrier? */
15361+static inline unsigned int au_figen(struct file *f)
dece6358 15362+{
4a4d8108
AM
15363+ return atomic_read(&au_fi(f)->fi_generation);
15364+}
dece6358 15365+
2cbb1c4b
JR
15366+static inline void au_set_mmapped(struct file *f)
15367+{
15368+ if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15369+ return;
0c3ec466 15370+ pr_warn("fi_mmapped wrapped around\n");
2cbb1c4b
JR
15371+ while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15372+ ;
15373+}
15374+
15375+static inline void au_unset_mmapped(struct file *f)
15376+{
15377+ atomic_dec(&au_fi(f)->fi_mmapped);
15378+}
15379+
4a4d8108
AM
15380+static inline int au_test_mmapped(struct file *f)
15381+{
2cbb1c4b
JR
15382+ return atomic_read(&au_fi(f)->fi_mmapped);
15383+}
15384+
15385+/* customize vma->vm_file */
15386+
15387+static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15388+ struct file *file)
15389+{
53392da6
AM
15390+ struct file *f;
15391+
15392+ f = vma->vm_file;
2cbb1c4b
JR
15393+ get_file(file);
15394+ vma->vm_file = file;
53392da6 15395+ fput(f);
2cbb1c4b
JR
15396+}
15397+
15398+#ifdef CONFIG_MMU
15399+#define AuDbgVmRegion(file, vma) do {} while (0)
15400+
15401+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15402+ struct file *file)
15403+{
15404+ au_do_vm_file_reset(vma, file);
15405+}
15406+#else
15407+#define AuDbgVmRegion(file, vma) \
15408+ AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15409+
15410+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15411+ struct file *file)
15412+{
53392da6
AM
15413+ struct file *f;
15414+
2cbb1c4b 15415+ au_do_vm_file_reset(vma, file);
53392da6 15416+ f = vma->vm_region->vm_file;
2cbb1c4b
JR
15417+ get_file(file);
15418+ vma->vm_region->vm_file = file;
53392da6 15419+ fput(f);
2cbb1c4b
JR
15420+}
15421+#endif /* CONFIG_MMU */
15422+
15423+/* handle vma->vm_prfile */
fb47a38f 15424+static inline void au_vm_prfile_set(struct vm_area_struct *vma,
2cbb1c4b
JR
15425+ struct file *file)
15426+{
2cbb1c4b
JR
15427+ get_file(file);
15428+ vma->vm_prfile = file;
15429+#ifndef CONFIG_MMU
15430+ get_file(file);
15431+ vma->vm_region->vm_prfile = file;
15432+#endif
fb47a38f 15433+}
1308ab2a 15434+
4a4d8108
AM
15435+#endif /* __KERNEL__ */
15436+#endif /* __AUFS_FILE_H__ */
7f207e10
AM
15437diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15438--- /usr/share/empty/fs/aufs/finfo.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 15439+++ linux/fs/aufs/finfo.c 2019-01-28 14:36:12.219084659 +0100
062440b3 15440@@ -0,0 +1,149 @@
cd7a4cd9 15441+// SPDX-License-Identifier: GPL-2.0
4a4d8108 15442+/*
b00004a5 15443+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
15444+ *
15445+ * This program, aufs is free software; you can redistribute it and/or modify
15446+ * it under the terms of the GNU General Public License as published by
15447+ * the Free Software Foundation; either version 2 of the License, or
15448+ * (at your option) any later version.
15449+ *
15450+ * This program is distributed in the hope that it will be useful,
15451+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15452+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15453+ * GNU General Public License for more details.
15454+ *
15455+ * You should have received a copy of the GNU General Public License
523b37e3 15456+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 15457+ */
1308ab2a 15458+
4a4d8108
AM
15459+/*
15460+ * file private data
15461+ */
1facf9fc 15462+
4a4d8108 15463+#include "aufs.h"
1facf9fc 15464+
f0c0a007 15465+void au_hfput(struct au_hfile *hf, int execed)
4a4d8108 15466+{
f0c0a007 15467+ if (execed)
4a4d8108
AM
15468+ allow_write_access(hf->hf_file);
15469+ fput(hf->hf_file);
15470+ hf->hf_file = NULL;
acd2b654 15471+ au_lcnt_dec(&hf->hf_br->br_nfiles);
4a4d8108
AM
15472+ hf->hf_br = NULL;
15473+}
1facf9fc 15474+
4a4d8108
AM
15475+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15476+{
15477+ struct au_finfo *finfo = au_fi(file);
15478+ struct au_hfile *hf;
15479+ struct au_fidir *fidir;
15480+
15481+ fidir = finfo->fi_hdir;
15482+ if (!fidir) {
15483+ AuDebugOn(finfo->fi_btop != bindex);
15484+ hf = &finfo->fi_htop;
15485+ } else
15486+ hf = fidir->fd_hfile + bindex;
15487+
15488+ if (hf && hf->hf_file)
f0c0a007 15489+ au_hfput(hf, vfsub_file_execed(file));
4a4d8108
AM
15490+ if (val) {
15491+ FiMustWriteLock(file);
b912730e 15492+ AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
4a4d8108 15493+ hf->hf_file = val;
2000de60 15494+ hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
1308ab2a 15495+ }
4a4d8108 15496+}
1facf9fc 15497+
4a4d8108
AM
15498+void au_update_figen(struct file *file)
15499+{
2000de60 15500+ atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
4a4d8108 15501+ /* smp_mb(); */ /* atomic_set */
1facf9fc 15502+}
15503+
4a4d8108
AM
15504+/* ---------------------------------------------------------------------- */
15505+
4a4d8108
AM
15506+struct au_fidir *au_fidir_alloc(struct super_block *sb)
15507+{
15508+ struct au_fidir *fidir;
15509+ int nbr;
15510+
5afbbe0d 15511+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
15512+ if (nbr < 2)
15513+ nbr = 2; /* initial allocate for 2 branches */
15514+ fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15515+ if (fidir) {
15516+ fidir->fd_bbot = -1;
15517+ fidir->fd_nent = nbr;
4a4d8108
AM
15518+ }
15519+
15520+ return fidir;
15521+}
15522+
e2f27e51 15523+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
4a4d8108
AM
15524+{
15525+ int err;
15526+ struct au_fidir *fidir, *p;
15527+
15528+ AuRwMustWriteLock(&finfo->fi_rwsem);
15529+ fidir = finfo->fi_hdir;
15530+ AuDebugOn(!fidir);
15531+
15532+ err = -ENOMEM;
15533+ p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
e2f27e51 15534+ GFP_NOFS, may_shrink);
4a4d8108
AM
15535+ if (p) {
15536+ p->fd_nent = nbr;
15537+ finfo->fi_hdir = p;
15538+ err = 0;
15539+ }
1facf9fc 15540+
dece6358 15541+ return err;
1facf9fc 15542+}
1308ab2a 15543+
15544+/* ---------------------------------------------------------------------- */
15545+
1c60b727 15546+void au_finfo_fin(struct file *file)
1308ab2a 15547+{
4a4d8108
AM
15548+ struct au_finfo *finfo;
15549+
acd2b654 15550+ au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
7f207e10 15551+
4a4d8108
AM
15552+ finfo = au_fi(file);
15553+ AuDebugOn(finfo->fi_hdir);
15554+ AuRwDestroy(&finfo->fi_rwsem);
1c60b727 15555+ au_cache_free_finfo(finfo);
1308ab2a 15556+}
1308ab2a 15557+
e49829fe 15558+void au_fi_init_once(void *_finfo)
4a4d8108 15559+{
e49829fe 15560+ struct au_finfo *finfo = _finfo;
1308ab2a 15561+
e49829fe 15562+ au_rw_init(&finfo->fi_rwsem);
4a4d8108 15563+}
1308ab2a 15564+
4a4d8108
AM
15565+int au_finfo_init(struct file *file, struct au_fidir *fidir)
15566+{
1716fcea 15567+ int err;
4a4d8108
AM
15568+ struct au_finfo *finfo;
15569+ struct dentry *dentry;
15570+
15571+ err = -ENOMEM;
2000de60 15572+ dentry = file->f_path.dentry;
4a4d8108
AM
15573+ finfo = au_cache_alloc_finfo();
15574+ if (unlikely(!finfo))
15575+ goto out;
15576+
15577+ err = 0;
acd2b654 15578+ au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
4a4d8108
AM
15579+ au_rw_write_lock(&finfo->fi_rwsem);
15580+ finfo->fi_btop = -1;
15581+ finfo->fi_hdir = fidir;
15582+ atomic_set(&finfo->fi_generation, au_digen(dentry));
15583+ /* smp_mb(); */ /* atomic_set */
15584+
15585+ file->private_data = finfo;
15586+
15587+out:
15588+ return err;
15589+}
7f207e10
AM
15590diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15591--- /usr/share/empty/fs/aufs/f_op.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 15592+++ linux/fs/aufs/f_op.c 2019-01-28 14:36:12.219084659 +0100
acd2b654 15593@@ -0,0 +1,819 @@
cd7a4cd9 15594+// SPDX-License-Identifier: GPL-2.0
dece6358 15595+/*
b00004a5 15596+ * Copyright (C) 2005-2018 Junjiro R. Okajima
dece6358
AM
15597+ *
15598+ * This program, aufs is free software; you can redistribute it and/or modify
15599+ * it under the terms of the GNU General Public License as published by
15600+ * the Free Software Foundation; either version 2 of the License, or
15601+ * (at your option) any later version.
15602+ *
15603+ * This program is distributed in the hope that it will be useful,
15604+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15605+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15606+ * GNU General Public License for more details.
15607+ *
15608+ * You should have received a copy of the GNU General Public License
523b37e3 15609+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 15610+ */
1facf9fc 15611+
15612+/*
4a4d8108 15613+ * file and vm operations
1facf9fc 15614+ */
dece6358 15615+
86dc4139 15616+#include <linux/aio.h>
4a4d8108
AM
15617+#include <linux/fs_stack.h>
15618+#include <linux/mman.h>
4a4d8108 15619+#include <linux/security.h>
dece6358
AM
15620+#include "aufs.h"
15621+
b912730e 15622+int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
1facf9fc 15623+{
4a4d8108
AM
15624+ int err;
15625+ aufs_bindex_t bindex;
8cdd5066 15626+ struct dentry *dentry, *h_dentry;
4a4d8108 15627+ struct au_finfo *finfo;
38d290e6 15628+ struct inode *h_inode;
4a4d8108
AM
15629+
15630+ FiMustWriteLock(file);
15631+
523b37e3 15632+ err = 0;
2000de60 15633+ dentry = file->f_path.dentry;
b912730e 15634+ AuDebugOn(IS_ERR_OR_NULL(dentry));
4a4d8108
AM
15635+ finfo = au_fi(file);
15636+ memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
2cbb1c4b 15637+ atomic_set(&finfo->fi_mmapped, 0);
5afbbe0d 15638+ bindex = au_dbtop(dentry);
8cdd5066
JR
15639+ if (!h_file) {
15640+ h_dentry = au_h_dptr(dentry, bindex);
15641+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15642+ if (unlikely(err))
15643+ goto out;
b912730e 15644+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
acd2b654
AM
15645+ if (IS_ERR(h_file)) {
15646+ err = PTR_ERR(h_file);
15647+ goto out;
15648+ }
8cdd5066
JR
15649+ } else {
15650+ h_dentry = h_file->f_path.dentry;
15651+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15652+ if (unlikely(err))
15653+ goto out;
acd2b654 15654+ /* br ref is already inc-ed */
8cdd5066 15655+ }
acd2b654
AM
15656+
15657+ if ((flags & __O_TMPFILE)
15658+ && !(flags & O_EXCL)) {
15659+ h_inode = file_inode(h_file);
15660+ spin_lock(&h_inode->i_lock);
15661+ h_inode->i_state |= I_LINKABLE;
15662+ spin_unlock(&h_inode->i_lock);
4a4d8108 15663+ }
acd2b654
AM
15664+ au_set_fbtop(file, bindex);
15665+ au_set_h_fptr(file, bindex, h_file);
15666+ au_update_figen(file);
15667+ /* todo: necessary? */
15668+ /* file->f_ra = h_file->f_ra; */
027c5e7a 15669+
8cdd5066 15670+out:
4a4d8108 15671+ return err;
1facf9fc 15672+}
15673+
4a4d8108
AM
15674+static int aufs_open_nondir(struct inode *inode __maybe_unused,
15675+ struct file *file)
1facf9fc 15676+{
4a4d8108 15677+ int err;
1308ab2a 15678+ struct super_block *sb;
b912730e
AM
15679+ struct au_do_open_args args = {
15680+ .open = au_do_open_nondir
15681+ };
1facf9fc 15682+
523b37e3
AM
15683+ AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15684+ file, vfsub_file_flags(file), file->f_mode);
1facf9fc 15685+
2000de60 15686+ sb = file->f_path.dentry->d_sb;
4a4d8108 15687+ si_read_lock(sb, AuLock_FLUSH);
b912730e 15688+ err = au_do_open(file, &args);
4a4d8108
AM
15689+ si_read_unlock(sb);
15690+ return err;
15691+}
1facf9fc 15692+
4a4d8108
AM
15693+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15694+{
15695+ struct au_finfo *finfo;
15696+ aufs_bindex_t bindex;
1facf9fc 15697+
4a4d8108 15698+ finfo = au_fi(file);
8b6a4947
AM
15699+ au_hbl_del(&finfo->fi_hlist,
15700+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108 15701+ bindex = finfo->fi_btop;
b4510431 15702+ if (bindex >= 0)
4a4d8108 15703+ au_set_h_fptr(file, bindex, NULL);
7f207e10 15704+
1c60b727 15705+ au_finfo_fin(file);
4a4d8108 15706+ return 0;
1facf9fc 15707+}
15708+
4a4d8108
AM
15709+/* ---------------------------------------------------------------------- */
15710+
15711+static int au_do_flush_nondir(struct file *file, fl_owner_t id)
dece6358 15712+{
1308ab2a 15713+ int err;
4a4d8108
AM
15714+ struct file *h_file;
15715+
15716+ err = 0;
15717+ h_file = au_hf_top(file);
15718+ if (h_file)
15719+ err = vfsub_flush(h_file, id);
15720+ return err;
15721+}
15722+
15723+static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15724+{
15725+ return au_do_flush(file, id, au_do_flush_nondir);
15726+}
15727+
15728+/* ---------------------------------------------------------------------- */
9dbd164d
AM
15729+/*
15730+ * read and write functions acquire [fdi]_rwsem once, but release before
15731+ * mmap_sem. This is because to stop a race condition between mmap(2).
acd2b654 15732+ * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
9dbd164d
AM
15733+ * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15734+ * read functions after [fdi]_rwsem are released, but it should be harmless.
15735+ */
4a4d8108 15736+
b912730e 15737+/* Callers should call au_read_post() or fput() in the end */
521ced18 15738+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
4a4d8108 15739+{
4a4d8108 15740+ struct file *h_file;
b912730e 15741+ int err;
1facf9fc 15742+
521ced18 15743+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
b912730e
AM
15744+ if (!err) {
15745+ di_read_unlock(file->f_path.dentry, AuLock_IR);
15746+ h_file = au_hf_top(file);
15747+ get_file(h_file);
15748+ if (!keep_fi)
15749+ fi_read_unlock(file);
15750+ } else
15751+ h_file = ERR_PTR(err);
15752+
15753+ return h_file;
15754+}
15755+
15756+static void au_read_post(struct inode *inode, struct file *h_file)
15757+{
15758+ /* update without lock, I don't think it a problem */
15759+ fsstack_copy_attr_atime(inode, file_inode(h_file));
15760+ fput(h_file);
15761+}
15762+
15763+struct au_write_pre {
521ced18
JR
15764+ /* input */
15765+ unsigned int lsc;
15766+
15767+ /* output */
b912730e 15768+ blkcnt_t blks;
5afbbe0d 15769+ aufs_bindex_t btop;
b912730e
AM
15770+};
15771+
15772+/*
15773+ * return with iinfo is write-locked
15774+ * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15775+ * end
15776+ */
15777+static struct file *au_write_pre(struct file *file, int do_ready,
15778+ struct au_write_pre *wpre)
15779+{
15780+ struct file *h_file;
15781+ struct dentry *dentry;
15782+ int err;
521ced18 15783+ unsigned int lsc;
b912730e
AM
15784+ struct au_pin pin;
15785+
521ced18
JR
15786+ lsc = 0;
15787+ if (wpre)
15788+ lsc = wpre->lsc;
15789+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
b912730e 15790+ h_file = ERR_PTR(err);
dece6358
AM
15791+ if (unlikely(err))
15792+ goto out;
1facf9fc 15793+
b912730e
AM
15794+ dentry = file->f_path.dentry;
15795+ if (do_ready) {
15796+ err = au_ready_to_write(file, -1, &pin);
15797+ if (unlikely(err)) {
15798+ h_file = ERR_PTR(err);
15799+ di_write_unlock(dentry);
15800+ goto out_fi;
15801+ }
15802+ }
15803+
15804+ di_downgrade_lock(dentry, /*flags*/0);
15805+ if (wpre)
5afbbe0d 15806+ wpre->btop = au_fbtop(file);
4a4d8108 15807+ h_file = au_hf_top(file);
9dbd164d 15808+ get_file(h_file);
b912730e
AM
15809+ if (wpre)
15810+ wpre->blks = file_inode(h_file)->i_blocks;
15811+ if (do_ready)
15812+ au_unpin(&pin);
15813+ di_read_unlock(dentry, /*flags*/0);
15814+
15815+out_fi:
15816+ fi_write_unlock(file);
15817+out:
15818+ return h_file;
15819+}
15820+
15821+static void au_write_post(struct inode *inode, struct file *h_file,
15822+ struct au_write_pre *wpre, ssize_t written)
15823+{
15824+ struct inode *h_inode;
15825+
15826+ au_cpup_attr_timesizes(inode);
5afbbe0d 15827+ AuDebugOn(au_ibtop(inode) != wpre->btop);
b912730e
AM
15828+ h_inode = file_inode(h_file);
15829+ inode->i_mode = h_inode->i_mode;
15830+ ii_write_unlock(inode);
b912730e
AM
15831+ /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15832+ if (written > 0)
5afbbe0d 15833+ au_fhsm_wrote(inode->i_sb, wpre->btop,
b912730e 15834+ /*force*/h_inode->i_blocks > wpre->blks);
1c60b727 15835+ fput(h_file);
b912730e
AM
15836+}
15837+
15838+static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
15839+ loff_t *ppos)
15840+{
15841+ ssize_t err;
15842+ struct inode *inode;
15843+ struct file *h_file;
15844+ struct super_block *sb;
15845+
15846+ inode = file_inode(file);
15847+ sb = inode->i_sb;
15848+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15849+
521ced18 15850+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
15851+ err = PTR_ERR(h_file);
15852+ if (IS_ERR(h_file))
15853+ goto out;
9dbd164d
AM
15854+
15855+ /* filedata may be obsoleted by concurrent copyup, but no problem */
4a4d8108
AM
15856+ err = vfsub_read_u(h_file, buf, count, ppos);
15857+ /* todo: necessary? */
15858+ /* file->f_ra = h_file->f_ra; */
b912730e 15859+ au_read_post(inode, h_file);
1308ab2a 15860+
4f0767ce 15861+out:
dece6358
AM
15862+ si_read_unlock(sb);
15863+ return err;
15864+}
1facf9fc 15865+
e49829fe
JR
15866+/*
15867+ * todo: very ugly
15868+ * it locks both of i_mutex and si_rwsem for read in safe.
15869+ * if the plink maintenance mode continues forever (that is the problem),
15870+ * may loop forever.
15871+ */
15872+static void au_mtx_and_read_lock(struct inode *inode)
15873+{
15874+ int err;
15875+ struct super_block *sb = inode->i_sb;
15876+
15877+ while (1) {
febd17d6 15878+ inode_lock(inode);
e49829fe
JR
15879+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15880+ if (!err)
15881+ break;
febd17d6 15882+ inode_unlock(inode);
e49829fe
JR
15883+ si_read_lock(sb, AuLock_NOPLMW);
15884+ si_read_unlock(sb);
15885+ }
15886+}
15887+
4a4d8108
AM
15888+static ssize_t aufs_write(struct file *file, const char __user *ubuf,
15889+ size_t count, loff_t *ppos)
dece6358 15890+{
4a4d8108 15891+ ssize_t err;
b912730e
AM
15892+ struct au_write_pre wpre;
15893+ struct inode *inode;
4a4d8108
AM
15894+ struct file *h_file;
15895+ char __user *buf = (char __user *)ubuf;
1facf9fc 15896+
b912730e 15897+ inode = file_inode(file);
e49829fe 15898+ au_mtx_and_read_lock(inode);
1facf9fc 15899+
521ced18 15900+ wpre.lsc = 0;
b912730e
AM
15901+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15902+ err = PTR_ERR(h_file);
15903+ if (IS_ERR(h_file))
9dbd164d 15904+ goto out;
9dbd164d 15905+
4a4d8108 15906+ err = vfsub_write_u(h_file, buf, count, ppos);
b912730e 15907+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15908+
4f0767ce 15909+out:
b912730e 15910+ si_read_unlock(inode->i_sb);
febd17d6 15911+ inode_unlock(inode);
dece6358
AM
15912+ return err;
15913+}
1facf9fc 15914+
076b876e
AM
15915+static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15916+ struct iov_iter *iov_iter)
dece6358 15917+{
4a4d8108
AM
15918+ ssize_t err;
15919+ struct file *file;
076b876e 15920+ ssize_t (*iter)(struct kiocb *, struct iov_iter *);
1facf9fc 15921+
4a4d8108
AM
15922+ err = security_file_permission(h_file, rw);
15923+ if (unlikely(err))
15924+ goto out;
1facf9fc 15925+
4a4d8108 15926+ err = -ENOSYS;
076b876e 15927+ iter = NULL;
5527c038 15928+ if (rw == MAY_READ)
076b876e 15929+ iter = h_file->f_op->read_iter;
5527c038 15930+ else if (rw == MAY_WRITE)
076b876e 15931+ iter = h_file->f_op->write_iter;
076b876e
AM
15932+
15933+ file = kio->ki_filp;
15934+ kio->ki_filp = h_file;
15935+ if (iter) {
2cbb1c4b 15936+ lockdep_off();
076b876e
AM
15937+ err = iter(kio, iov_iter);
15938+ lockdep_on();
4a4d8108
AM
15939+ } else
15940+ /* currently there is no such fs */
15941+ WARN_ON_ONCE(1);
076b876e 15942+ kio->ki_filp = file;
1facf9fc 15943+
4f0767ce 15944+out:
dece6358
AM
15945+ return err;
15946+}
1facf9fc 15947+
076b876e 15948+static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1facf9fc 15949+{
4a4d8108
AM
15950+ ssize_t err;
15951+ struct file *file, *h_file;
b912730e 15952+ struct inode *inode;
dece6358 15953+ struct super_block *sb;
1facf9fc 15954+
4a4d8108 15955+ file = kio->ki_filp;
b912730e
AM
15956+ inode = file_inode(file);
15957+ sb = inode->i_sb;
e49829fe 15958+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 15959+
521ced18 15960+ h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
b912730e
AM
15961+ err = PTR_ERR(h_file);
15962+ if (IS_ERR(h_file))
15963+ goto out;
9dbd164d 15964+
5afbbe0d
AM
15965+ if (au_test_loopback_kthread()) {
15966+ au_warn_loopback(h_file->f_path.dentry->d_sb);
15967+ if (file->f_mapping != h_file->f_mapping) {
15968+ file->f_mapping = h_file->f_mapping;
15969+ smp_mb(); /* unnecessary? */
15970+ }
15971+ }
15972+ fi_read_unlock(file);
15973+
076b876e 15974+ err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
4a4d8108
AM
15975+ /* todo: necessary? */
15976+ /* file->f_ra = h_file->f_ra; */
b912730e 15977+ au_read_post(inode, h_file);
1facf9fc 15978+
4f0767ce 15979+out:
4a4d8108 15980+ si_read_unlock(sb);
1308ab2a 15981+ return err;
15982+}
1facf9fc 15983+
076b876e 15984+static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1308ab2a 15985+{
4a4d8108 15986+ ssize_t err;
b912730e
AM
15987+ struct au_write_pre wpre;
15988+ struct inode *inode;
4a4d8108 15989+ struct file *file, *h_file;
1308ab2a 15990+
4a4d8108 15991+ file = kio->ki_filp;
b912730e 15992+ inode = file_inode(file);
e49829fe
JR
15993+ au_mtx_and_read_lock(inode);
15994+
521ced18 15995+ wpre.lsc = 0;
b912730e
AM
15996+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15997+ err = PTR_ERR(h_file);
15998+ if (IS_ERR(h_file))
9dbd164d 15999+ goto out;
9dbd164d 16000+
076b876e 16001+ err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
b912730e 16002+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 16003+
4f0767ce 16004+out:
b912730e 16005+ si_read_unlock(inode->i_sb);
febd17d6 16006+ inode_unlock(inode);
dece6358 16007+ return err;
1facf9fc 16008+}
16009+
4a4d8108
AM
16010+static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
16011+ struct pipe_inode_info *pipe, size_t len,
16012+ unsigned int flags)
1facf9fc 16013+{
4a4d8108
AM
16014+ ssize_t err;
16015+ struct file *h_file;
b912730e 16016+ struct inode *inode;
dece6358 16017+ struct super_block *sb;
1facf9fc 16018+
b912730e
AM
16019+ inode = file_inode(file);
16020+ sb = inode->i_sb;
e49829fe 16021+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 16022+
521ced18 16023+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
16024+ err = PTR_ERR(h_file);
16025+ if (IS_ERR(h_file))
dece6358 16026+ goto out;
1facf9fc 16027+
4a4d8108 16028+ err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
acd2b654 16029+ /* todo: necessary? */
4a4d8108 16030+ /* file->f_ra = h_file->f_ra; */
b912730e 16031+ au_read_post(inode, h_file);
1facf9fc 16032+
4f0767ce 16033+out:
4a4d8108 16034+ si_read_unlock(sb);
dece6358 16035+ return err;
1facf9fc 16036+}
16037+
4a4d8108
AM
16038+static ssize_t
16039+aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
16040+ size_t len, unsigned int flags)
1facf9fc 16041+{
4a4d8108 16042+ ssize_t err;
b912730e
AM
16043+ struct au_write_pre wpre;
16044+ struct inode *inode;
076b876e 16045+ struct file *h_file;
1facf9fc 16046+
b912730e 16047+ inode = file_inode(file);
e49829fe 16048+ au_mtx_and_read_lock(inode);
9dbd164d 16049+
521ced18 16050+ wpre.lsc = 0;
b912730e
AM
16051+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16052+ err = PTR_ERR(h_file);
16053+ if (IS_ERR(h_file))
9dbd164d 16054+ goto out;
9dbd164d 16055+
4a4d8108 16056+ err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
b912730e 16057+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 16058+
4f0767ce 16059+out:
b912730e 16060+ si_read_unlock(inode->i_sb);
febd17d6 16061+ inode_unlock(inode);
4a4d8108
AM
16062+ return err;
16063+}
1facf9fc 16064+
38d290e6
JR
16065+static long aufs_fallocate(struct file *file, int mode, loff_t offset,
16066+ loff_t len)
16067+{
16068+ long err;
b912730e 16069+ struct au_write_pre wpre;
38d290e6
JR
16070+ struct inode *inode;
16071+ struct file *h_file;
16072+
b912730e 16073+ inode = file_inode(file);
38d290e6
JR
16074+ au_mtx_and_read_lock(inode);
16075+
521ced18 16076+ wpre.lsc = 0;
b912730e
AM
16077+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16078+ err = PTR_ERR(h_file);
16079+ if (IS_ERR(h_file))
38d290e6 16080+ goto out;
38d290e6
JR
16081+
16082+ lockdep_off();
03673fb0 16083+ err = vfs_fallocate(h_file, mode, offset, len);
38d290e6 16084+ lockdep_on();
b912730e 16085+ au_write_post(inode, h_file, &wpre, /*written*/1);
38d290e6
JR
16086+
16087+out:
b912730e 16088+ si_read_unlock(inode->i_sb);
febd17d6 16089+ inode_unlock(inode);
38d290e6
JR
16090+ return err;
16091+}
16092+
521ced18
JR
16093+static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
16094+ struct file *dst, loff_t dst_pos,
16095+ size_t len, unsigned int flags)
16096+{
16097+ ssize_t err;
16098+ struct au_write_pre wpre;
16099+ enum { SRC, DST };
16100+ struct {
16101+ struct inode *inode;
16102+ struct file *h_file;
16103+ struct super_block *h_sb;
16104+ } a[2];
16105+#define a_src a[SRC]
16106+#define a_dst a[DST]
16107+
16108+ err = -EINVAL;
16109+ a_src.inode = file_inode(src);
16110+ if (unlikely(!S_ISREG(a_src.inode->i_mode)))
16111+ goto out;
16112+ a_dst.inode = file_inode(dst);
16113+ if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
16114+ goto out;
16115+
16116+ au_mtx_and_read_lock(a_dst.inode);
16117+ /*
16118+ * in order to match the order in di_write_lock2_{child,parent}(),
acd2b654 16119+ * use f_path.dentry for this comparison.
521ced18
JR
16120+ */
16121+ if (src->f_path.dentry < dst->f_path.dentry) {
16122+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
16123+ err = PTR_ERR(a_src.h_file);
16124+ if (IS_ERR(a_src.h_file))
16125+ goto out_si;
16126+
16127+ wpre.lsc = AuLsc_FI_2;
16128+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16129+ err = PTR_ERR(a_dst.h_file);
16130+ if (IS_ERR(a_dst.h_file)) {
16131+ au_read_post(a_src.inode, a_src.h_file);
16132+ goto out_si;
16133+ }
16134+ } else {
16135+ wpre.lsc = AuLsc_FI_1;
16136+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16137+ err = PTR_ERR(a_dst.h_file);
16138+ if (IS_ERR(a_dst.h_file))
16139+ goto out_si;
16140+
16141+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
16142+ err = PTR_ERR(a_src.h_file);
16143+ if (IS_ERR(a_src.h_file)) {
16144+ au_write_post(a_dst.inode, a_dst.h_file, &wpre,
16145+ /*written*/0);
16146+ goto out_si;
16147+ }
16148+ }
16149+
16150+ err = -EXDEV;
16151+ a_src.h_sb = file_inode(a_src.h_file)->i_sb;
16152+ a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
16153+ if (unlikely(a_src.h_sb != a_dst.h_sb)) {
16154+ AuDbgFile(src);
16155+ AuDbgFile(dst);
16156+ goto out_file;
16157+ }
16158+
16159+ err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
16160+ dst_pos, len, flags);
16161+
16162+out_file:
16163+ au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
16164+ fi_read_unlock(src);
16165+ au_read_post(a_src.inode, a_src.h_file);
16166+out_si:
16167+ si_read_unlock(a_dst.inode->i_sb);
16168+ inode_unlock(a_dst.inode);
16169+out:
16170+ return err;
16171+#undef a_src
16172+#undef a_dst
16173+}
16174+
4a4d8108
AM
16175+/* ---------------------------------------------------------------------- */
16176+
9dbd164d
AM
16177+/*
16178+ * The locking order around current->mmap_sem.
16179+ * - in most and regular cases
16180+ * file I/O syscall -- aufs_read() or something
16181+ * -- si_rwsem for read -- mmap_sem
16182+ * (Note that [fdi]i_rwsem are released before mmap_sem).
16183+ * - in mmap case
16184+ * mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
acd2b654
AM
16185+ * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
16186+ * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
16187+ * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
9dbd164d
AM
16188+ * It means that when aufs acquires si_rwsem for write, the process should never
16189+ * acquire mmap_sem.
16190+ *
392086de 16191+ * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
9dbd164d
AM
16192+ * problem either since any directory is not able to be mmap-ed.
16193+ * The similar scenario is applied to aufs_readlink() too.
16194+ */
16195+
38d290e6 16196+#if 0 /* stop calling security_file_mmap() */
2dfbb274
AM
16197+/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
16198+#define AuConv_VM_PROT(f, b) _calc_vm_trans(f, VM_##b, PROT_##b)
16199+
16200+static unsigned long au_arch_prot_conv(unsigned long flags)
16201+{
16202+ /* currently ppc64 only */
16203+#ifdef CONFIG_PPC64
16204+ /* cf. linux/arch/powerpc/include/asm/mman.h */
16205+ AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
16206+ return AuConv_VM_PROT(flags, SAO);
16207+#else
16208+ AuDebugOn(arch_calc_vm_prot_bits(-1));
16209+ return 0;
16210+#endif
16211+}
16212+
16213+static unsigned long au_prot_conv(unsigned long flags)
16214+{
16215+ return AuConv_VM_PROT(flags, READ)
16216+ | AuConv_VM_PROT(flags, WRITE)
16217+ | AuConv_VM_PROT(flags, EXEC)
16218+ | au_arch_prot_conv(flags);
16219+}
16220+
16221+/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16222+#define AuConv_VM_MAP(f, b) _calc_vm_trans(f, VM_##b, MAP_##b)
16223+
16224+static unsigned long au_flag_conv(unsigned long flags)
16225+{
16226+ return AuConv_VM_MAP(flags, GROWSDOWN)
16227+ | AuConv_VM_MAP(flags, DENYWRITE)
2dfbb274
AM
16228+ | AuConv_VM_MAP(flags, LOCKED);
16229+}
38d290e6 16230+#endif
2dfbb274 16231+
9dbd164d 16232+static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
dece6358 16233+{
4a4d8108 16234+ int err;
4a4d8108 16235+ const unsigned char wlock
9dbd164d 16236+ = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
4a4d8108 16237+ struct super_block *sb;
9dbd164d 16238+ struct file *h_file;
b912730e 16239+ struct inode *inode;
9dbd164d
AM
16240+
16241+ AuDbgVmRegion(file, vma);
1308ab2a 16242+
b912730e
AM
16243+ inode = file_inode(file);
16244+ sb = inode->i_sb;
9dbd164d 16245+ lockdep_off();
e49829fe 16246+ si_read_lock(sb, AuLock_NOPLMW);
4a4d8108 16247+
b912730e 16248+ h_file = au_write_pre(file, wlock, /*wpre*/NULL);
9dbd164d 16249+ lockdep_on();
b912730e
AM
16250+ err = PTR_ERR(h_file);
16251+ if (IS_ERR(h_file))
16252+ goto out;
1308ab2a 16253+
b912730e
AM
16254+ err = 0;
16255+ au_set_mmapped(file);
9dbd164d 16256+ au_vm_file_reset(vma, h_file);
38d290e6
JR
16257+ /*
16258+ * we cannot call security_mmap_file() here since it may acquire
16259+ * mmap_sem or i_mutex.
16260+ *
16261+ * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16262+ * au_flag_conv(vma->vm_flags));
16263+ */
9dbd164d 16264+ if (!err)
521ced18 16265+ err = call_mmap(h_file, vma);
b912730e
AM
16266+ if (!err) {
16267+ au_vm_prfile_set(vma, file);
16268+ fsstack_copy_attr_atime(inode, file_inode(h_file));
16269+ goto out_fput; /* success */
16270+ }
2cbb1c4b
JR
16271+ au_unset_mmapped(file);
16272+ au_vm_file_reset(vma, file);
b912730e 16273+
2cbb1c4b 16274+out_fput:
9dbd164d 16275+ lockdep_off();
b912730e
AM
16276+ ii_write_unlock(inode);
16277+ lockdep_on();
16278+ fput(h_file);
4f0767ce 16279+out:
b912730e 16280+ lockdep_off();
9dbd164d
AM
16281+ si_read_unlock(sb);
16282+ lockdep_on();
16283+ AuTraceErr(err);
4a4d8108
AM
16284+ return err;
16285+}
16286+
16287+/* ---------------------------------------------------------------------- */
16288+
1e00d052
AM
16289+static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16290+ int datasync)
4a4d8108
AM
16291+{
16292+ int err;
b912730e 16293+ struct au_write_pre wpre;
4a4d8108
AM
16294+ struct inode *inode;
16295+ struct file *h_file;
4a4d8108
AM
16296+
16297+ err = 0; /* -EBADF; */ /* posix? */
16298+ if (unlikely(!(file->f_mode & FMODE_WRITE)))
b912730e 16299+ goto out;
4a4d8108 16300+
b912730e
AM
16301+ inode = file_inode(file);
16302+ au_mtx_and_read_lock(inode);
16303+
521ced18 16304+ wpre.lsc = 0;
b912730e
AM
16305+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16306+ err = PTR_ERR(h_file);
16307+ if (IS_ERR(h_file))
4a4d8108 16308+ goto out_unlock;
4a4d8108 16309+
53392da6 16310+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
b912730e 16311+ au_write_post(inode, h_file, &wpre, /*written*/0);
4a4d8108 16312+
4f0767ce 16313+out_unlock:
b912730e 16314+ si_read_unlock(inode->i_sb);
febd17d6 16315+ inode_unlock(inode);
b912730e 16316+out:
4a4d8108 16317+ return err;
dece6358
AM
16318+}
16319+
4a4d8108 16320+static int aufs_fasync(int fd, struct file *file, int flag)
dece6358 16321+{
4a4d8108
AM
16322+ int err;
16323+ struct file *h_file;
4a4d8108 16324+ struct super_block *sb;
1308ab2a 16325+
b912730e 16326+ sb = file->f_path.dentry->d_sb;
e49829fe 16327+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 16328+
521ced18 16329+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
16330+ err = PTR_ERR(h_file);
16331+ if (IS_ERR(h_file))
4a4d8108
AM
16332+ goto out;
16333+
523b37e3 16334+ if (h_file->f_op->fasync)
4a4d8108 16335+ err = h_file->f_op->fasync(fd, h_file, flag);
b912730e 16336+ fput(h_file); /* instead of au_read_post() */
1308ab2a 16337+
4f0767ce 16338+out:
4a4d8108 16339+ si_read_unlock(sb);
1308ab2a 16340+ return err;
dece6358 16341+}
4a4d8108 16342+
febd17d6
JR
16343+static int aufs_setfl(struct file *file, unsigned long arg)
16344+{
16345+ int err;
16346+ struct file *h_file;
16347+ struct super_block *sb;
16348+
16349+ sb = file->f_path.dentry->d_sb;
16350+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16351+
521ced18 16352+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
febd17d6
JR
16353+ err = PTR_ERR(h_file);
16354+ if (IS_ERR(h_file))
16355+ goto out;
16356+
1c60b727
AM
16357+ /* stop calling h_file->fasync */
16358+ arg |= vfsub_file_flags(file) & FASYNC;
febd17d6
JR
16359+ err = setfl(/*unused fd*/-1, h_file, arg);
16360+ fput(h_file); /* instead of au_read_post() */
16361+
16362+out:
16363+ si_read_unlock(sb);
16364+ return err;
16365+}
16366+
4a4d8108
AM
16367+/* ---------------------------------------------------------------------- */
16368+
16369+/* no one supports this operation, currently */
16370+#if 0
16371+static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
2000de60 16372+ size_t len, loff_t *pos, int more)
4a4d8108
AM
16373+{
16374+}
16375+#endif
16376+
16377+/* ---------------------------------------------------------------------- */
16378+
16379+const struct file_operations aufs_file_fop = {
16380+ .owner = THIS_MODULE,
2cbb1c4b 16381+
027c5e7a 16382+ .llseek = default_llseek,
4a4d8108
AM
16383+
16384+ .read = aufs_read,
16385+ .write = aufs_write,
076b876e
AM
16386+ .read_iter = aufs_read_iter,
16387+ .write_iter = aufs_write_iter,
16388+
4a4d8108
AM
16389+#ifdef CONFIG_AUFS_POLL
16390+ .poll = aufs_poll,
16391+#endif
16392+ .unlocked_ioctl = aufs_ioctl_nondir,
b752ccd1 16393+#ifdef CONFIG_COMPAT
c2b27bf2 16394+ .compat_ioctl = aufs_compat_ioctl_nondir,
b752ccd1 16395+#endif
4a4d8108
AM
16396+ .mmap = aufs_mmap,
16397+ .open = aufs_open_nondir,
16398+ .flush = aufs_flush_nondir,
16399+ .release = aufs_release_nondir,
16400+ .fsync = aufs_fsync_nondir,
4a4d8108
AM
16401+ .fasync = aufs_fasync,
16402+ /* .sendpage = aufs_sendpage, */
febd17d6 16403+ .setfl = aufs_setfl,
4a4d8108
AM
16404+ .splice_write = aufs_splice_write,
16405+ .splice_read = aufs_splice_read,
16406+#if 0
16407+ .aio_splice_write = aufs_aio_splice_write,
38d290e6 16408+ .aio_splice_read = aufs_aio_splice_read,
4a4d8108 16409+#endif
521ced18
JR
16410+ .fallocate = aufs_fallocate,
16411+ .copy_file_range = aufs_copy_file_range
4a4d8108 16412+};
7f207e10
AM
16413diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
16414--- /usr/share/empty/fs/aufs/fstype.h 1970-01-01 01:00:00.000000000 +0100
e49925d1 16415+++ linux/fs/aufs/fstype.h 2019-01-28 14:36:12.219084659 +0100
062440b3
AM
16416@@ -0,0 +1,401 @@
16417+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 16418+/*
b00004a5 16419+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
16420+ *
16421+ * This program, aufs is free software; you can redistribute it and/or modify
16422+ * it under the terms of the GNU General Public License as published by
16423+ * the Free Software Foundation; either version 2 of the License, or
16424+ * (at your option) any later version.
16425+ *
16426+ * This program is distributed in the hope that it will be useful,
16427+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16428+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16429+ * GNU General Public License for more details.
16430+ *
16431+ * You should have received a copy of the GNU General Public License
523b37e3 16432+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
16433+ */
16434+
16435+/*
16436+ * judging filesystem type
16437+ */
16438+
16439+#ifndef __AUFS_FSTYPE_H__
16440+#define __AUFS_FSTYPE_H__
16441+
16442+#ifdef __KERNEL__
16443+
16444+#include <linux/fs.h>
16445+#include <linux/magic.h>
b912730e 16446+#include <linux/nfs_fs.h>
b95c5147 16447+#include <linux/romfs_fs.h>
4a4d8108
AM
16448+
16449+static inline int au_test_aufs(struct super_block *sb)
16450+{
16451+ return sb->s_magic == AUFS_SUPER_MAGIC;
16452+}
16453+
16454+static inline const char *au_sbtype(struct super_block *sb)
16455+{
16456+ return sb->s_type->name;
16457+}
1308ab2a 16458+
16459+static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
16460+{
f0c0a007 16461+#if IS_ENABLED(CONFIG_ISO9660_FS)
2000de60 16462+ return sb->s_magic == ISOFS_SUPER_MAGIC;
dece6358
AM
16463+#else
16464+ return 0;
16465+#endif
16466+}
16467+
1308ab2a 16468+static inline int au_test_romfs(struct super_block *sb __maybe_unused)
dece6358 16469+{
f0c0a007 16470+#if IS_ENABLED(CONFIG_ROMFS_FS)
2000de60 16471+ return sb->s_magic == ROMFS_MAGIC;
dece6358
AM
16472+#else
16473+ return 0;
16474+#endif
16475+}
16476+
1308ab2a 16477+static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
dece6358 16478+{
f0c0a007 16479+#if IS_ENABLED(CONFIG_CRAMFS)
1308ab2a 16480+ return sb->s_magic == CRAMFS_MAGIC;
16481+#endif
16482+ return 0;
16483+}
16484+
16485+static inline int au_test_nfs(struct super_block *sb __maybe_unused)
16486+{
f0c0a007 16487+#if IS_ENABLED(CONFIG_NFS_FS)
1308ab2a 16488+ return sb->s_magic == NFS_SUPER_MAGIC;
dece6358
AM
16489+#else
16490+ return 0;
16491+#endif
16492+}
16493+
1308ab2a 16494+static inline int au_test_fuse(struct super_block *sb __maybe_unused)
dece6358 16495+{
f0c0a007 16496+#if IS_ENABLED(CONFIG_FUSE_FS)
1308ab2a 16497+ return sb->s_magic == FUSE_SUPER_MAGIC;
dece6358
AM
16498+#else
16499+ return 0;
16500+#endif
16501+}
16502+
1308ab2a 16503+static inline int au_test_xfs(struct super_block *sb __maybe_unused)
dece6358 16504+{
f0c0a007 16505+#if IS_ENABLED(CONFIG_XFS_FS)
1308ab2a 16506+ return sb->s_magic == XFS_SB_MAGIC;
dece6358
AM
16507+#else
16508+ return 0;
16509+#endif
16510+}
16511+
1308ab2a 16512+static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
dece6358 16513+{
1308ab2a 16514+#ifdef CONFIG_TMPFS
16515+ return sb->s_magic == TMPFS_MAGIC;
16516+#else
16517+ return 0;
dece6358 16518+#endif
dece6358
AM
16519+}
16520+
1308ab2a 16521+static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
1facf9fc 16522+{
f0c0a007 16523+#if IS_ENABLED(CONFIG_ECRYPT_FS)
1308ab2a 16524+ return !strcmp(au_sbtype(sb), "ecryptfs");
16525+#else
16526+ return 0;
16527+#endif
1facf9fc 16528+}
16529+
1308ab2a 16530+static inline int au_test_ramfs(struct super_block *sb)
16531+{
16532+ return sb->s_magic == RAMFS_MAGIC;
16533+}
16534+
16535+static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
16536+{
f0c0a007 16537+#if IS_ENABLED(CONFIG_UBIFS_FS)
1308ab2a 16538+ return sb->s_magic == UBIFS_SUPER_MAGIC;
16539+#else
16540+ return 0;
16541+#endif
16542+}
16543+
16544+static inline int au_test_procfs(struct super_block *sb __maybe_unused)
16545+{
16546+#ifdef CONFIG_PROC_FS
16547+ return sb->s_magic == PROC_SUPER_MAGIC;
16548+#else
16549+ return 0;
16550+#endif
16551+}
16552+
16553+static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
16554+{
16555+#ifdef CONFIG_SYSFS
16556+ return sb->s_magic == SYSFS_MAGIC;
16557+#else
16558+ return 0;
16559+#endif
16560+}
16561+
16562+static inline int au_test_configfs(struct super_block *sb __maybe_unused)
16563+{
f0c0a007 16564+#if IS_ENABLED(CONFIG_CONFIGFS_FS)
1308ab2a 16565+ return sb->s_magic == CONFIGFS_MAGIC;
16566+#else
16567+ return 0;
16568+#endif
16569+}
16570+
16571+static inline int au_test_minix(struct super_block *sb __maybe_unused)
16572+{
f0c0a007 16573+#if IS_ENABLED(CONFIG_MINIX_FS)
1308ab2a 16574+ return sb->s_magic == MINIX3_SUPER_MAGIC
16575+ || sb->s_magic == MINIX2_SUPER_MAGIC
16576+ || sb->s_magic == MINIX2_SUPER_MAGIC2
16577+ || sb->s_magic == MINIX_SUPER_MAGIC
16578+ || sb->s_magic == MINIX_SUPER_MAGIC2;
16579+#else
16580+ return 0;
16581+#endif
16582+}
16583+
1308ab2a 16584+static inline int au_test_fat(struct super_block *sb __maybe_unused)
16585+{
f0c0a007 16586+#if IS_ENABLED(CONFIG_FAT_FS)
1308ab2a 16587+ return sb->s_magic == MSDOS_SUPER_MAGIC;
16588+#else
16589+ return 0;
16590+#endif
16591+}
16592+
16593+static inline int au_test_msdos(struct super_block *sb)
16594+{
16595+ return au_test_fat(sb);
16596+}
16597+
16598+static inline int au_test_vfat(struct super_block *sb)
16599+{
16600+ return au_test_fat(sb);
16601+}
16602+
16603+static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
16604+{
16605+#ifdef CONFIG_SECURITYFS
16606+ return sb->s_magic == SECURITYFS_MAGIC;
16607+#else
16608+ return 0;
16609+#endif
16610+}
16611+
16612+static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
16613+{
f0c0a007 16614+#if IS_ENABLED(CONFIG_SQUASHFS)
1308ab2a 16615+ return sb->s_magic == SQUASHFS_MAGIC;
16616+#else
16617+ return 0;
16618+#endif
16619+}
16620+
16621+static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
16622+{
f0c0a007 16623+#if IS_ENABLED(CONFIG_BTRFS_FS)
1308ab2a 16624+ return sb->s_magic == BTRFS_SUPER_MAGIC;
16625+#else
16626+ return 0;
16627+#endif
16628+}
16629+
16630+static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
16631+{
f0c0a007 16632+#if IS_ENABLED(CONFIG_XENFS)
1308ab2a 16633+ return sb->s_magic == XENFS_SUPER_MAGIC;
16634+#else
16635+ return 0;
16636+#endif
16637+}
16638+
16639+static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
16640+{
16641+#ifdef CONFIG_DEBUG_FS
16642+ return sb->s_magic == DEBUGFS_MAGIC;
16643+#else
16644+ return 0;
16645+#endif
16646+}
16647+
16648+static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
16649+{
f0c0a007 16650+#if IS_ENABLED(CONFIG_NILFS)
1308ab2a 16651+ return sb->s_magic == NILFS_SUPER_MAGIC;
16652+#else
16653+ return 0;
16654+#endif
16655+}
16656+
4a4d8108
AM
16657+static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
16658+{
f0c0a007 16659+#if IS_ENABLED(CONFIG_HFSPLUS_FS)
4a4d8108
AM
16660+ return sb->s_magic == HFSPLUS_SUPER_MAGIC;
16661+#else
16662+ return 0;
16663+#endif
16664+}
16665+
1308ab2a 16666+/* ---------------------------------------------------------------------- */
16667+/*
16668+ * they can't be an aufs branch.
16669+ */
16670+static inline int au_test_fs_unsuppoted(struct super_block *sb)
16671+{
16672+ return
16673+#ifndef CONFIG_AUFS_BR_RAMFS
16674+ au_test_ramfs(sb) ||
16675+#endif
16676+ au_test_procfs(sb)
16677+ || au_test_sysfs(sb)
16678+ || au_test_configfs(sb)
16679+ || au_test_debugfs(sb)
16680+ || au_test_securityfs(sb)
16681+ || au_test_xenfs(sb)
16682+ || au_test_ecryptfs(sb)
16683+ /* || !strcmp(au_sbtype(sb), "unionfs") */
16684+ || au_test_aufs(sb); /* will be supported in next version */
16685+}
16686+
1308ab2a 16687+static inline int au_test_fs_remote(struct super_block *sb)
16688+{
16689+ return !au_test_tmpfs(sb)
16690+#ifdef CONFIG_AUFS_BR_RAMFS
16691+ && !au_test_ramfs(sb)
16692+#endif
16693+ && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
16694+}
16695+
16696+/* ---------------------------------------------------------------------- */
16697+
16698+/*
16699+ * Note: these functions (below) are created after reading ->getattr() in all
16700+ * filesystems under linux/fs. it means we have to do so in every update...
16701+ */
16702+
16703+/*
16704+ * some filesystems require getattr to refresh the inode attributes before
16705+ * referencing.
16706+ * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
16707+ * and leave the work for d_revalidate()
16708+ */
16709+static inline int au_test_fs_refresh_iattr(struct super_block *sb)
16710+{
16711+ return au_test_nfs(sb)
16712+ || au_test_fuse(sb)
1308ab2a 16713+ /* || au_test_btrfs(sb) */ /* untested */
1308ab2a 16714+ ;
16715+}
16716+
16717+/*
16718+ * filesystems which don't maintain i_size or i_blocks.
16719+ */
16720+static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
16721+{
16722+ return au_test_xfs(sb)
4a4d8108
AM
16723+ || au_test_btrfs(sb)
16724+ || au_test_ubifs(sb)
16725+ || au_test_hfsplus(sb) /* maintained, but incorrect */
1308ab2a 16726+ /* || au_test_minix(sb) */ /* untested */
16727+ ;
16728+}
16729+
16730+/*
16731+ * filesystems which don't store the correct value in some of their inode
16732+ * attributes.
16733+ */
16734+static inline int au_test_fs_bad_iattr(struct super_block *sb)
16735+{
16736+ return au_test_fs_bad_iattr_size(sb)
1308ab2a 16737+ || au_test_fat(sb)
16738+ || au_test_msdos(sb)
16739+ || au_test_vfat(sb);
1facf9fc 16740+}
16741+
16742+/* they don't check i_nlink in link(2) */
16743+static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
16744+{
16745+ return au_test_tmpfs(sb)
16746+#ifdef CONFIG_AUFS_BR_RAMFS
16747+ || au_test_ramfs(sb)
16748+#endif
4a4d8108 16749+ || au_test_ubifs(sb)
4a4d8108 16750+ || au_test_hfsplus(sb);
1facf9fc 16751+}
16752+
16753+/*
16754+ * filesystems which sets S_NOATIME and S_NOCMTIME.
16755+ */
16756+static inline int au_test_fs_notime(struct super_block *sb)
16757+{
16758+ return au_test_nfs(sb)
16759+ || au_test_fuse(sb)
dece6358 16760+ || au_test_ubifs(sb)
1facf9fc 16761+ ;
16762+}
16763+
1facf9fc 16764+/* temporary support for i#1 in cramfs */
16765+static inline int au_test_fs_unique_ino(struct inode *inode)
16766+{
16767+ if (au_test_cramfs(inode->i_sb))
16768+ return inode->i_ino != 1;
16769+ return 1;
16770+}
16771+
16772+/* ---------------------------------------------------------------------- */
16773+
16774+/*
16775+ * the filesystem where the xino files placed must support i/o after unlink and
16776+ * maintain i_size and i_blocks.
16777+ */
16778+static inline int au_test_fs_bad_xino(struct super_block *sb)
16779+{
16780+ return au_test_fs_remote(sb)
16781+ || au_test_fs_bad_iattr_size(sb)
1facf9fc 16782+ /* don't want unnecessary work for xino */
16783+ || au_test_aufs(sb)
1308ab2a 16784+ || au_test_ecryptfs(sb)
16785+ || au_test_nilfs(sb);
1facf9fc 16786+}
16787+
16788+static inline int au_test_fs_trunc_xino(struct super_block *sb)
16789+{
16790+ return au_test_tmpfs(sb)
16791+ || au_test_ramfs(sb);
16792+}
16793+
16794+/*
16795+ * test if the @sb is real-readonly.
16796+ */
16797+static inline int au_test_fs_rr(struct super_block *sb)
16798+{
16799+ return au_test_squashfs(sb)
16800+ || au_test_iso9660(sb)
16801+ || au_test_cramfs(sb)
16802+ || au_test_romfs(sb);
16803+}
16804+
b912730e
AM
16805+/*
16806+ * test if the @inode is nfs with 'noacl' option
2121bcd9 16807+ * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
b912730e
AM
16808+ */
16809+static inline int au_test_nfs_noacl(struct inode *inode)
16810+{
16811+ return au_test_nfs(inode->i_sb)
16812+ /* && IS_POSIXACL(inode) */
16813+ && !nfs_server_capable(inode, NFS_CAP_ACLS);
16814+}
16815+
1facf9fc 16816+#endif /* __KERNEL__ */
16817+#endif /* __AUFS_FSTYPE_H__ */
8b6a4947
AM
16818diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
16819--- /usr/share/empty/fs/aufs/hbl.h 1970-01-01 01:00:00.000000000 +0100
e49925d1 16820+++ linux/fs/aufs/hbl.h 2019-01-28 14:36:12.219084659 +0100
062440b3
AM
16821@@ -0,0 +1,65 @@
16822+/* SPDX-License-Identifier: GPL-2.0 */
8b6a4947 16823+/*
b00004a5 16824+ * Copyright (C) 2017-2018 Junjiro R. Okajima
8b6a4947
AM
16825+ *
16826+ * This program, aufs is free software; you can redistribute it and/or modify
16827+ * it under the terms of the GNU General Public License as published by
16828+ * the Free Software Foundation; either version 2 of the License, or
16829+ * (at your option) any later version.
16830+ *
16831+ * This program is distributed in the hope that it will be useful,
16832+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16833+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16834+ * GNU General Public License for more details.
16835+ *
16836+ * You should have received a copy of the GNU General Public License
16837+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16838+ */
16839+
16840+/*
16841+ * helpers for hlist_bl.h
16842+ */
16843+
16844+#ifndef __AUFS_HBL_H__
16845+#define __AUFS_HBL_H__
16846+
16847+#ifdef __KERNEL__
16848+
16849+#include <linux/list_bl.h>
16850+
16851+static inline void au_hbl_add(struct hlist_bl_node *node,
16852+ struct hlist_bl_head *hbl)
16853+{
16854+ hlist_bl_lock(hbl);
16855+ hlist_bl_add_head(node, hbl);
16856+ hlist_bl_unlock(hbl);
16857+}
16858+
16859+static inline void au_hbl_del(struct hlist_bl_node *node,
16860+ struct hlist_bl_head *hbl)
16861+{
16862+ hlist_bl_lock(hbl);
16863+ hlist_bl_del(node);
16864+ hlist_bl_unlock(hbl);
16865+}
16866+
16867+#define au_hbl_for_each(pos, head) \
16868+ for (pos = hlist_bl_first(head); \
16869+ pos; \
16870+ pos = pos->next)
16871+
16872+static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
16873+{
16874+ unsigned long cnt;
16875+ struct hlist_bl_node *pos;
16876+
16877+ cnt = 0;
16878+ hlist_bl_lock(hbl);
16879+ au_hbl_for_each(pos, hbl)
16880+ cnt++;
16881+ hlist_bl_unlock(hbl);
16882+ return cnt;
16883+}
16884+
16885+#endif /* __KERNEL__ */
16886+#endif /* __AUFS_HBL_H__ */
7f207e10
AM
16887diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
16888--- /usr/share/empty/fs/aufs/hfsnotify.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 16889+++ linux/fs/aufs/hfsnotify.c 2019-01-28 14:36:12.219084659 +0100
acd2b654 16890@@ -0,0 +1,289 @@
cd7a4cd9 16891+// SPDX-License-Identifier: GPL-2.0
1facf9fc 16892+/*
b00004a5 16893+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 16894+ *
16895+ * This program, aufs is free software; you can redistribute it and/or modify
16896+ * it under the terms of the GNU General Public License as published by
16897+ * the Free Software Foundation; either version 2 of the License, or
16898+ * (at your option) any later version.
dece6358
AM
16899+ *
16900+ * This program is distributed in the hope that it will be useful,
16901+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16902+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16903+ * GNU General Public License for more details.
16904+ *
16905+ * You should have received a copy of the GNU General Public License
523b37e3 16906+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 16907+ */
16908+
16909+/*
4a4d8108 16910+ * fsnotify for the lower directories
1facf9fc 16911+ */
16912+
16913+#include "aufs.h"
16914+
4a4d8108
AM
16915+/* FS_IN_IGNORED is unnecessary */
16916+static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
16917+ | FS_CREATE | FS_EVENT_ON_CHILD);
7f207e10 16918+static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
7eafdf33 16919+static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
1facf9fc 16920+
0c5527e5 16921+static void au_hfsn_free_mark(struct fsnotify_mark *mark)
1facf9fc 16922+{
0c5527e5
AM
16923+ struct au_hnotify *hn = container_of(mark, struct au_hnotify,
16924+ hn_mark);
5afbbe0d 16925+ /* AuDbg("here\n"); */
1c60b727 16926+ au_cache_free_hnotify(hn);
8b6a4947 16927+ smp_mb__before_atomic(); /* for atomic64_dec */
1716fcea
AM
16928+ if (atomic64_dec_and_test(&au_hfsn_ifree))
16929+ wake_up(&au_hfsn_wq);
4a4d8108 16930+}
1facf9fc 16931+
027c5e7a 16932+static int au_hfsn_alloc(struct au_hinode *hinode)
4a4d8108 16933+{
1716fcea 16934+ int err;
027c5e7a
AM
16935+ struct au_hnotify *hn;
16936+ struct super_block *sb;
16937+ struct au_branch *br;
0c5527e5 16938+ struct fsnotify_mark *mark;
027c5e7a 16939+ aufs_bindex_t bindex;
1facf9fc 16940+
027c5e7a
AM
16941+ hn = hinode->hi_notify;
16942+ sb = hn->hn_aufs_inode->i_sb;
16943+ bindex = au_br_index(sb, hinode->hi_id);
16944+ br = au_sbr(sb, bindex);
1716fcea
AM
16945+ AuDebugOn(!br->br_hfsn);
16946+
0c5527e5 16947+ mark = &hn->hn_mark;
ffa93bbd 16948+ fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
0c5527e5 16949+ mark->mask = AuHfsnMask;
7f207e10
AM
16950+ /*
16951+ * by udba rename or rmdir, aufs assign a new inode to the known
16952+ * h_inode, so specify 1 to allow dups.
16953+ */
c1595e42 16954+ lockdep_off();
acd2b654 16955+ err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
c1595e42 16956+ lockdep_on();
1716fcea
AM
16957+
16958+ return err;
1facf9fc 16959+}
16960+
7eafdf33 16961+static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
1facf9fc 16962+{
0c5527e5 16963+ struct fsnotify_mark *mark;
7eafdf33 16964+ unsigned long long ull;
1716fcea 16965+ struct fsnotify_group *group;
7eafdf33
AM
16966+
16967+ ull = atomic64_inc_return(&au_hfsn_ifree);
16968+ BUG_ON(!ull);
953406b4 16969+
0c5527e5 16970+ mark = &hn->hn_mark;
1716fcea
AM
16971+ spin_lock(&mark->lock);
16972+ group = mark->group;
16973+ fsnotify_get_group(group);
16974+ spin_unlock(&mark->lock);
c1595e42 16975+ lockdep_off();
1716fcea 16976+ fsnotify_destroy_mark(mark, group);
5afbbe0d 16977+ fsnotify_put_mark(mark);
1716fcea 16978+ fsnotify_put_group(group);
c1595e42 16979+ lockdep_on();
7f207e10 16980+
7eafdf33
AM
16981+ /* free hn by myself */
16982+ return 0;
1facf9fc 16983+}
16984+
16985+/* ---------------------------------------------------------------------- */
16986+
4a4d8108 16987+static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
1facf9fc 16988+{
0c5527e5 16989+ struct fsnotify_mark *mark;
1facf9fc 16990+
0c5527e5
AM
16991+ mark = &hinode->hi_notify->hn_mark;
16992+ spin_lock(&mark->lock);
1facf9fc 16993+ if (do_set) {
0c5527e5
AM
16994+ AuDebugOn(mark->mask & AuHfsnMask);
16995+ mark->mask |= AuHfsnMask;
1facf9fc 16996+ } else {
0c5527e5
AM
16997+ AuDebugOn(!(mark->mask & AuHfsnMask));
16998+ mark->mask &= ~AuHfsnMask;
1facf9fc 16999+ }
0c5527e5 17000+ spin_unlock(&mark->lock);
4a4d8108 17001+ /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
1facf9fc 17002+}
17003+
4a4d8108 17004+/* ---------------------------------------------------------------------- */
1facf9fc 17005+
4a4d8108
AM
17006+/* #define AuDbgHnotify */
17007+#ifdef AuDbgHnotify
17008+static char *au_hfsn_name(u32 mask)
17009+{
17010+#ifdef CONFIG_AUFS_DEBUG
c06a8ce3
AM
17011+#define test_ret(flag) \
17012+ do { \
17013+ if (mask & flag) \
17014+ return #flag; \
17015+ } while (0)
4a4d8108
AM
17016+ test_ret(FS_ACCESS);
17017+ test_ret(FS_MODIFY);
17018+ test_ret(FS_ATTRIB);
17019+ test_ret(FS_CLOSE_WRITE);
17020+ test_ret(FS_CLOSE_NOWRITE);
17021+ test_ret(FS_OPEN);
17022+ test_ret(FS_MOVED_FROM);
17023+ test_ret(FS_MOVED_TO);
17024+ test_ret(FS_CREATE);
17025+ test_ret(FS_DELETE);
17026+ test_ret(FS_DELETE_SELF);
17027+ test_ret(FS_MOVE_SELF);
17028+ test_ret(FS_UNMOUNT);
17029+ test_ret(FS_Q_OVERFLOW);
17030+ test_ret(FS_IN_IGNORED);
b912730e 17031+ test_ret(FS_ISDIR);
4a4d8108
AM
17032+ test_ret(FS_IN_ONESHOT);
17033+ test_ret(FS_EVENT_ON_CHILD);
17034+ return "";
17035+#undef test_ret
17036+#else
17037+ return "??";
17038+#endif
1facf9fc 17039+}
4a4d8108 17040+#endif
1facf9fc 17041+
17042+/* ---------------------------------------------------------------------- */
17043+
1716fcea
AM
17044+static void au_hfsn_free_group(struct fsnotify_group *group)
17045+{
17046+ struct au_br_hfsnotify *hfsn = group->private;
17047+
5afbbe0d 17048+ /* AuDbg("here\n"); */
e49925d1 17049+ au_kfree_try_rcu(hfsn);
1716fcea
AM
17050+}
17051+
4a4d8108 17052+static int au_hfsn_handle_event(struct fsnotify_group *group,
fb47a38f 17053+ struct inode *inode,
a2654f78 17054+ u32 mask, const void *data, int data_type,
ffa93bbd
AM
17055+ const unsigned char *file_name, u32 cookie,
17056+ struct fsnotify_iter_info *iter_info)
1facf9fc 17057+{
17058+ int err;
4a4d8108
AM
17059+ struct au_hnotify *hnotify;
17060+ struct inode *h_dir, *h_inode;
fb47a38f 17061+ struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
cd7a4cd9 17062+ struct fsnotify_mark *inode_mark;
4a4d8108 17063+
fb47a38f 17064+ AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
1facf9fc 17065+
17066+ err = 0;
0c5527e5 17067+ /* if FS_UNMOUNT happens, there must be another bug */
4a4d8108 17068+ AuDebugOn(mask & FS_UNMOUNT);
0c5527e5 17069+ if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
1facf9fc 17070+ goto out;
1facf9fc 17071+
fb47a38f
JR
17072+ h_dir = inode;
17073+ h_inode = NULL;
4a4d8108 17074+#ifdef AuDbgHnotify
392086de 17075+ au_debug_on();
4a4d8108
AM
17076+ if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
17077+ || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
17078+ AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
17079+ h_dir->i_ino, mask, au_hfsn_name(mask),
17080+ AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
17081+ /* WARN_ON(1); */
1facf9fc 17082+ }
392086de 17083+ au_debug_off();
1facf9fc 17084+#endif
4a4d8108 17085+
cd7a4cd9 17086+ inode_mark = fsnotify_iter_inode_mark(iter_info);
0c5527e5
AM
17087+ AuDebugOn(!inode_mark);
17088+ hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
17089+ err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
1facf9fc 17090+
4a4d8108
AM
17091+out:
17092+ return err;
17093+}
1facf9fc 17094+
4a4d8108 17095+static struct fsnotify_ops au_hfsn_ops = {
1716fcea 17096+ .handle_event = au_hfsn_handle_event,
ffa93bbd
AM
17097+ .free_group_priv = au_hfsn_free_group,
17098+ .free_mark = au_hfsn_free_mark
4a4d8108
AM
17099+};
17100+
17101+/* ---------------------------------------------------------------------- */
17102+
027c5e7a
AM
17103+static void au_hfsn_fin_br(struct au_branch *br)
17104+{
1716fcea 17105+ struct au_br_hfsnotify *hfsn;
027c5e7a 17106+
1716fcea 17107+ hfsn = br->br_hfsn;
c1595e42
JR
17108+ if (hfsn) {
17109+ lockdep_off();
1716fcea 17110+ fsnotify_put_group(hfsn->hfsn_group);
c1595e42
JR
17111+ lockdep_on();
17112+ }
027c5e7a
AM
17113+}
17114+
1716fcea 17115+static int au_hfsn_init_br(struct au_branch *br, int perm)
4a4d8108
AM
17116+{
17117+ int err;
1716fcea
AM
17118+ struct fsnotify_group *group;
17119+ struct au_br_hfsnotify *hfsn;
1facf9fc 17120+
4a4d8108 17121+ err = 0;
1716fcea
AM
17122+ br->br_hfsn = NULL;
17123+ if (!au_br_hnotifyable(perm))
027c5e7a 17124+ goto out;
027c5e7a 17125+
1716fcea
AM
17126+ err = -ENOMEM;
17127+ hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
17128+ if (unlikely(!hfsn))
027c5e7a
AM
17129+ goto out;
17130+
1716fcea
AM
17131+ err = 0;
17132+ group = fsnotify_alloc_group(&au_hfsn_ops);
17133+ if (IS_ERR(group)) {
17134+ err = PTR_ERR(group);
0c5527e5 17135+ pr_err("fsnotify_alloc_group() failed, %d\n", err);
1716fcea 17136+ goto out_hfsn;
4a4d8108 17137+ }
1facf9fc 17138+
1716fcea
AM
17139+ group->private = hfsn;
17140+ hfsn->hfsn_group = group;
17141+ br->br_hfsn = hfsn;
17142+ goto out; /* success */
17143+
17144+out_hfsn:
e49925d1 17145+ au_kfree_try_rcu(hfsn);
027c5e7a 17146+out:
1716fcea
AM
17147+ return err;
17148+}
17149+
17150+static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
17151+{
17152+ int err;
17153+
17154+ err = 0;
17155+ if (!br->br_hfsn)
17156+ err = au_hfsn_init_br(br, perm);
17157+
1facf9fc 17158+ return err;
17159+}
17160+
7eafdf33
AM
17161+/* ---------------------------------------------------------------------- */
17162+
17163+static void au_hfsn_fin(void)
17164+{
17165+ AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
17166+ wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
17167+}
17168+
4a4d8108
AM
17169+const struct au_hnotify_op au_hnotify_op = {
17170+ .ctl = au_hfsn_ctl,
17171+ .alloc = au_hfsn_alloc,
17172+ .free = au_hfsn_free,
1facf9fc 17173+
7eafdf33
AM
17174+ .fin = au_hfsn_fin,
17175+
027c5e7a
AM
17176+ .reset_br = au_hfsn_reset_br,
17177+ .fin_br = au_hfsn_fin_br,
17178+ .init_br = au_hfsn_init_br
4a4d8108 17179+};
7f207e10
AM
17180diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
17181--- /usr/share/empty/fs/aufs/hfsplus.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 17182+++ linux/fs/aufs/hfsplus.c 2019-01-28 14:36:12.219084659 +0100
acd2b654 17183@@ -0,0 +1,60 @@
cd7a4cd9 17184+// SPDX-License-Identifier: GPL-2.0
4a4d8108 17185+/*
b00004a5 17186+ * Copyright (C) 2010-2018 Junjiro R. Okajima
4a4d8108
AM
17187+ *
17188+ * This program, aufs is free software; you can redistribute it and/or modify
17189+ * it under the terms of the GNU General Public License as published by
17190+ * the Free Software Foundation; either version 2 of the License, or
17191+ * (at your option) any later version.
17192+ *
17193+ * This program is distributed in the hope that it will be useful,
17194+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17195+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17196+ * GNU General Public License for more details.
17197+ *
17198+ * You should have received a copy of the GNU General Public License
523b37e3 17199+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 17200+ */
1facf9fc 17201+
4a4d8108 17202+/*
acd2b654 17203+ * special support for filesystems which acquires an inode mutex
4a4d8108
AM
17204+ * at final closing a file, eg, hfsplus.
17205+ *
17206+ * This trick is very simple and stupid, just to open the file before really
acd2b654 17207+ * necessary open to tell hfsplus that this is not the final closing.
4a4d8108
AM
17208+ * The caller should call au_h_open_pre() after acquiring the inode mutex,
17209+ * and au_h_open_post() after releasing it.
17210+ */
1facf9fc 17211+
4a4d8108 17212+#include "aufs.h"
1facf9fc 17213+
392086de
AM
17214+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
17215+ int force_wr)
4a4d8108
AM
17216+{
17217+ struct file *h_file;
17218+ struct dentry *h_dentry;
1facf9fc 17219+
4a4d8108
AM
17220+ h_dentry = au_h_dptr(dentry, bindex);
17221+ AuDebugOn(!h_dentry);
5527c038 17222+ AuDebugOn(d_is_negative(h_dentry));
4a4d8108
AM
17223+
17224+ h_file = NULL;
17225+ if (au_test_hfsplus(h_dentry->d_sb)
7e9cd9fe 17226+ && d_is_reg(h_dentry))
4a4d8108
AM
17227+ h_file = au_h_open(dentry, bindex,
17228+ O_RDONLY | O_NOATIME | O_LARGEFILE,
392086de 17229+ /*file*/NULL, force_wr);
4a4d8108 17230+ return h_file;
1facf9fc 17231+}
17232+
4a4d8108
AM
17233+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
17234+ struct file *h_file)
17235+{
acd2b654
AM
17236+ struct au_branch *br;
17237+
4a4d8108
AM
17238+ if (h_file) {
17239+ fput(h_file);
acd2b654
AM
17240+ br = au_sbr(dentry->d_sb, bindex);
17241+ au_lcnt_dec(&br->br_nfiles);
4a4d8108
AM
17242+ }
17243+}
7f207e10
AM
17244diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
17245--- /usr/share/empty/fs/aufs/hnotify.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 17246+++ linux/fs/aufs/hnotify.c 2019-01-28 14:36:12.219084659 +0100
062440b3 17247@@ -0,0 +1,720 @@
cd7a4cd9 17248+// SPDX-License-Identifier: GPL-2.0
e49829fe 17249+/*
b00004a5 17250+ * Copyright (C) 2005-2018 Junjiro R. Okajima
e49829fe
JR
17251+ *
17252+ * This program, aufs is free software; you can redistribute it and/or modify
17253+ * it under the terms of the GNU General Public License as published by
17254+ * the Free Software Foundation; either version 2 of the License, or
17255+ * (at your option) any later version.
17256+ *
17257+ * This program is distributed in the hope that it will be useful,
17258+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17259+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17260+ * GNU General Public License for more details.
17261+ *
17262+ * You should have received a copy of the GNU General Public License
523b37e3 17263+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
17264+ */
17265+
17266+/*
7f207e10 17267+ * abstraction to notify the direct changes on lower directories
e49829fe
JR
17268+ */
17269+
17270+#include "aufs.h"
17271+
027c5e7a 17272+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
e49829fe
JR
17273+{
17274+ int err;
7f207e10 17275+ struct au_hnotify *hn;
1facf9fc 17276+
4a4d8108
AM
17277+ err = -ENOMEM;
17278+ hn = au_cache_alloc_hnotify();
17279+ if (hn) {
17280+ hn->hn_aufs_inode = inode;
027c5e7a
AM
17281+ hinode->hi_notify = hn;
17282+ err = au_hnotify_op.alloc(hinode);
17283+ AuTraceErr(err);
17284+ if (unlikely(err)) {
17285+ hinode->hi_notify = NULL;
1c60b727 17286+ au_cache_free_hnotify(hn);
4a4d8108
AM
17287+ /*
17288+ * The upper dir was removed by udba, but the same named
acd2b654 17289+ * dir left. In this case, aufs assigns a new inode
4a4d8108 17290+ * number and set the monitor again.
acd2b654 17291+ * For the lower dir, the old monitor is still left.
4a4d8108
AM
17292+ */
17293+ if (err == -EEXIST)
17294+ err = 0;
17295+ }
1308ab2a 17296+ }
1308ab2a 17297+
027c5e7a 17298+ AuTraceErr(err);
1308ab2a 17299+ return err;
dece6358 17300+}
1facf9fc 17301+
4a4d8108 17302+void au_hn_free(struct au_hinode *hinode)
dece6358 17303+{
4a4d8108 17304+ struct au_hnotify *hn;
1facf9fc 17305+
4a4d8108
AM
17306+ hn = hinode->hi_notify;
17307+ if (hn) {
4a4d8108 17308+ hinode->hi_notify = NULL;
7eafdf33 17309+ if (au_hnotify_op.free(hinode, hn))
1c60b727 17310+ au_cache_free_hnotify(hn);
4a4d8108
AM
17311+ }
17312+}
dece6358 17313+
4a4d8108 17314+/* ---------------------------------------------------------------------- */
dece6358 17315+
4a4d8108
AM
17316+void au_hn_ctl(struct au_hinode *hinode, int do_set)
17317+{
17318+ if (hinode->hi_notify)
17319+ au_hnotify_op.ctl(hinode, do_set);
17320+}
17321+
17322+void au_hn_reset(struct inode *inode, unsigned int flags)
17323+{
5afbbe0d 17324+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
17325+ struct inode *hi;
17326+ struct dentry *iwhdentry;
1facf9fc 17327+
5afbbe0d
AM
17328+ bbot = au_ibbot(inode);
17329+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108
AM
17330+ hi = au_h_iptr(inode, bindex);
17331+ if (!hi)
17332+ continue;
1308ab2a 17333+
febd17d6 17334+ /* inode_lock_nested(hi, AuLsc_I_CHILD); */
4a4d8108
AM
17335+ iwhdentry = au_hi_wh(inode, bindex);
17336+ if (iwhdentry)
17337+ dget(iwhdentry);
17338+ au_igrab(hi);
17339+ au_set_h_iptr(inode, bindex, NULL, 0);
17340+ au_set_h_iptr(inode, bindex, au_igrab(hi),
17341+ flags & ~AuHi_XINO);
17342+ iput(hi);
17343+ dput(iwhdentry);
febd17d6 17344+ /* inode_unlock(hi); */
1facf9fc 17345+ }
1facf9fc 17346+}
17347+
1308ab2a 17348+/* ---------------------------------------------------------------------- */
1facf9fc 17349+
4a4d8108 17350+static int hn_xino(struct inode *inode, struct inode *h_inode)
1facf9fc 17351+{
4a4d8108 17352+ int err;
5afbbe0d 17353+ aufs_bindex_t bindex, bbot, bfound, btop;
4a4d8108 17354+ struct inode *h_i;
1facf9fc 17355+
4a4d8108
AM
17356+ err = 0;
17357+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17358+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17359+ goto out;
17360+ }
1facf9fc 17361+
4a4d8108 17362+ bfound = -1;
5afbbe0d
AM
17363+ bbot = au_ibbot(inode);
17364+ btop = au_ibtop(inode);
4a4d8108 17365+#if 0 /* reserved for future use */
5afbbe0d 17366+ if (bindex == bbot) {
4a4d8108
AM
17367+ /* keep this ino in rename case */
17368+ goto out;
17369+ }
17370+#endif
5afbbe0d 17371+ for (bindex = btop; bindex <= bbot; bindex++)
4a4d8108
AM
17372+ if (au_h_iptr(inode, bindex) == h_inode) {
17373+ bfound = bindex;
17374+ break;
17375+ }
17376+ if (bfound < 0)
1308ab2a 17377+ goto out;
1facf9fc 17378+
5afbbe0d 17379+ for (bindex = btop; bindex <= bbot; bindex++) {
4a4d8108
AM
17380+ h_i = au_h_iptr(inode, bindex);
17381+ if (!h_i)
17382+ continue;
1facf9fc 17383+
4a4d8108
AM
17384+ err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
17385+ /* ignore this error */
17386+ /* bad action? */
1facf9fc 17387+ }
1facf9fc 17388+
4a4d8108 17389+ /* children inode number will be broken */
1facf9fc 17390+
4f0767ce 17391+out:
4a4d8108
AM
17392+ AuTraceErr(err);
17393+ return err;
1facf9fc 17394+}
17395+
4a4d8108 17396+static int hn_gen_tree(struct dentry *dentry)
1facf9fc 17397+{
4a4d8108
AM
17398+ int err, i, j, ndentry;
17399+ struct au_dcsub_pages dpages;
17400+ struct au_dpage *dpage;
17401+ struct dentry **dentries;
1facf9fc 17402+
4a4d8108
AM
17403+ err = au_dpages_init(&dpages, GFP_NOFS);
17404+ if (unlikely(err))
17405+ goto out;
17406+ err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
17407+ if (unlikely(err))
17408+ goto out_dpages;
1facf9fc 17409+
4a4d8108
AM
17410+ for (i = 0; i < dpages.ndpage; i++) {
17411+ dpage = dpages.dpages + i;
17412+ dentries = dpage->dentries;
17413+ ndentry = dpage->ndentry;
17414+ for (j = 0; j < ndentry; j++) {
17415+ struct dentry *d;
17416+
17417+ d = dentries[j];
17418+ if (IS_ROOT(d))
17419+ continue;
17420+
4a4d8108 17421+ au_digen_dec(d);
5527c038 17422+ if (d_really_is_positive(d))
4a4d8108
AM
17423+ /* todo: reset children xino?
17424+ cached children only? */
5527c038 17425+ au_iigen_dec(d_inode(d));
1308ab2a 17426+ }
dece6358 17427+ }
1facf9fc 17428+
4f0767ce 17429+out_dpages:
4a4d8108 17430+ au_dpages_free(&dpages);
dece6358 17431+
027c5e7a 17432+#if 0
4a4d8108
AM
17433+ /* discard children */
17434+ dentry_unhash(dentry);
17435+ dput(dentry);
027c5e7a 17436+#endif
4f0767ce 17437+out:
dece6358
AM
17438+ return err;
17439+}
17440+
1308ab2a 17441+/*
4a4d8108 17442+ * return 0 if processed.
1308ab2a 17443+ */
4a4d8108
AM
17444+static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
17445+ const unsigned int isdir)
dece6358 17446+{
1308ab2a 17447+ int err;
4a4d8108
AM
17448+ struct dentry *d;
17449+ struct qstr *dname;
1facf9fc 17450+
4a4d8108
AM
17451+ err = 1;
17452+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17453+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17454+ err = 0;
17455+ goto out;
17456+ }
dece6358 17457+
4a4d8108
AM
17458+ if (!isdir) {
17459+ AuDebugOn(!name);
17460+ au_iigen_dec(inode);
027c5e7a 17461+ spin_lock(&inode->i_lock);
c1595e42 17462+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 17463+ spin_lock(&d->d_lock);
4a4d8108
AM
17464+ dname = &d->d_name;
17465+ if (dname->len != nlen
027c5e7a
AM
17466+ && memcmp(dname->name, name, nlen)) {
17467+ spin_unlock(&d->d_lock);
4a4d8108 17468+ continue;
027c5e7a 17469+ }
4a4d8108 17470+ err = 0;
4a4d8108
AM
17471+ au_digen_dec(d);
17472+ spin_unlock(&d->d_lock);
17473+ break;
1facf9fc 17474+ }
027c5e7a 17475+ spin_unlock(&inode->i_lock);
1308ab2a 17476+ } else {
027c5e7a 17477+ au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
c1595e42 17478+ d = d_find_any_alias(inode);
4a4d8108
AM
17479+ if (!d) {
17480+ au_iigen_dec(inode);
17481+ goto out;
17482+ }
1facf9fc 17483+
027c5e7a 17484+ spin_lock(&d->d_lock);
4a4d8108 17485+ dname = &d->d_name;
027c5e7a
AM
17486+ if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
17487+ spin_unlock(&d->d_lock);
4a4d8108 17488+ err = hn_gen_tree(d);
027c5e7a
AM
17489+ spin_lock(&d->d_lock);
17490+ }
17491+ spin_unlock(&d->d_lock);
4a4d8108
AM
17492+ dput(d);
17493+ }
1facf9fc 17494+
4f0767ce 17495+out:
4a4d8108 17496+ AuTraceErr(err);
1308ab2a 17497+ return err;
17498+}
dece6358 17499+
4a4d8108 17500+static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
1facf9fc 17501+{
4a4d8108 17502+ int err;
1facf9fc 17503+
5527c038 17504+ if (IS_ROOT(dentry)) {
0c3ec466 17505+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17506+ return 0;
17507+ }
1308ab2a 17508+
4a4d8108
AM
17509+ err = 0;
17510+ if (!isdir) {
4a4d8108 17511+ au_digen_dec(dentry);
5527c038
JR
17512+ if (d_really_is_positive(dentry))
17513+ au_iigen_dec(d_inode(dentry));
4a4d8108 17514+ } else {
027c5e7a 17515+ au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
5527c038 17516+ if (d_really_is_positive(dentry))
4a4d8108
AM
17517+ err = hn_gen_tree(dentry);
17518+ }
17519+
17520+ AuTraceErr(err);
17521+ return err;
1facf9fc 17522+}
17523+
4a4d8108 17524+/* ---------------------------------------------------------------------- */
1facf9fc 17525+
4a4d8108
AM
17526+/* hnotify job flags */
17527+#define AuHnJob_XINO0 1
17528+#define AuHnJob_GEN (1 << 1)
17529+#define AuHnJob_DIRENT (1 << 2)
17530+#define AuHnJob_ISDIR (1 << 3)
17531+#define AuHnJob_TRYXINO0 (1 << 4)
17532+#define AuHnJob_MNTPNT (1 << 5)
17533+#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
7f207e10
AM
17534+#define au_fset_hnjob(flags, name) \
17535+ do { (flags) |= AuHnJob_##name; } while (0)
17536+#define au_fclr_hnjob(flags, name) \
17537+ do { (flags) &= ~AuHnJob_##name; } while (0)
1facf9fc 17538+
4a4d8108
AM
17539+enum {
17540+ AuHn_CHILD,
17541+ AuHn_PARENT,
17542+ AuHnLast
17543+};
1facf9fc 17544+
4a4d8108
AM
17545+struct au_hnotify_args {
17546+ struct inode *h_dir, *dir, *h_child_inode;
17547+ u32 mask;
17548+ unsigned int flags[AuHnLast];
17549+ unsigned int h_child_nlen;
17550+ char h_child_name[];
17551+};
1facf9fc 17552+
4a4d8108
AM
17553+struct hn_job_args {
17554+ unsigned int flags;
17555+ struct inode *inode, *h_inode, *dir, *h_dir;
17556+ struct dentry *dentry;
17557+ char *h_name;
17558+ int h_nlen;
17559+};
1308ab2a 17560+
4a4d8108
AM
17561+static int hn_job(struct hn_job_args *a)
17562+{
17563+ const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
076b876e 17564+ int e;
1308ab2a 17565+
4a4d8108
AM
17566+ /* reset xino */
17567+ if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
17568+ hn_xino(a->inode, a->h_inode); /* ignore this error */
1308ab2a 17569+
4a4d8108
AM
17570+ if (au_ftest_hnjob(a->flags, TRYXINO0)
17571+ && a->inode
17572+ && a->h_inode) {
be118d29 17573+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
38d290e6
JR
17574+ if (!a->h_inode->i_nlink
17575+ && !(a->h_inode->i_state & I_LINKABLE))
4a4d8108 17576+ hn_xino(a->inode, a->h_inode); /* ignore this error */
3c1bdaff 17577+ inode_unlock_shared(a->h_inode);
1308ab2a 17578+ }
1facf9fc 17579+
4a4d8108
AM
17580+ /* make the generation obsolete */
17581+ if (au_ftest_hnjob(a->flags, GEN)) {
076b876e 17582+ e = -1;
4a4d8108 17583+ if (a->inode)
076b876e 17584+ e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
4a4d8108 17585+ isdir);
076b876e 17586+ if (e && a->dentry)
4a4d8108
AM
17587+ hn_gen_by_name(a->dentry, isdir);
17588+ /* ignore this error */
1facf9fc 17589+ }
1facf9fc 17590+
4a4d8108
AM
17591+ /* make dir entries obsolete */
17592+ if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
17593+ struct au_vdir *vdir;
1facf9fc 17594+
4a4d8108
AM
17595+ vdir = au_ivdir(a->inode);
17596+ if (vdir)
17597+ vdir->vd_jiffy = 0;
17598+ /* IMustLock(a->inode); */
be118d29 17599+ /* inode_inc_iversion(a->inode); */
4a4d8108 17600+ }
1facf9fc 17601+
4a4d8108
AM
17602+ /* can do nothing but warn */
17603+ if (au_ftest_hnjob(a->flags, MNTPNT)
17604+ && a->dentry
17605+ && d_mountpoint(a->dentry))
523b37e3 17606+ pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
1facf9fc 17607+
4a4d8108 17608+ return 0;
1308ab2a 17609+}
1facf9fc 17610+
1308ab2a 17611+/* ---------------------------------------------------------------------- */
1facf9fc 17612+
4a4d8108
AM
17613+static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
17614+ struct inode *dir)
1308ab2a 17615+{
4a4d8108
AM
17616+ struct dentry *dentry, *d, *parent;
17617+ struct qstr *dname;
1308ab2a 17618+
c1595e42 17619+ parent = d_find_any_alias(dir);
4a4d8108
AM
17620+ if (!parent)
17621+ return NULL;
1308ab2a 17622+
4a4d8108 17623+ dentry = NULL;
027c5e7a 17624+ spin_lock(&parent->d_lock);
c1595e42 17625+ list_for_each_entry(d, &parent->d_subdirs, d_child) {
523b37e3 17626+ /* AuDbg("%pd\n", d); */
027c5e7a 17627+ spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
4a4d8108
AM
17628+ dname = &d->d_name;
17629+ if (dname->len != nlen || memcmp(dname->name, name, nlen))
027c5e7a
AM
17630+ goto cont_unlock;
17631+ if (au_di(d))
17632+ au_digen_dec(d);
17633+ else
17634+ goto cont_unlock;
c1595e42 17635+ if (au_dcount(d) > 0) {
027c5e7a 17636+ dentry = dget_dlock(d);
4a4d8108 17637+ spin_unlock(&d->d_lock);
027c5e7a 17638+ break;
dece6358 17639+ }
1facf9fc 17640+
f6b6e03d 17641+cont_unlock:
027c5e7a 17642+ spin_unlock(&d->d_lock);
1308ab2a 17643+ }
027c5e7a 17644+ spin_unlock(&parent->d_lock);
4a4d8108 17645+ dput(parent);
1facf9fc 17646+
4a4d8108
AM
17647+ if (dentry)
17648+ di_write_lock_child(dentry);
1308ab2a 17649+
4a4d8108
AM
17650+ return dentry;
17651+}
dece6358 17652+
4a4d8108
AM
17653+static struct inode *lookup_wlock_by_ino(struct super_block *sb,
17654+ aufs_bindex_t bindex, ino_t h_ino)
17655+{
17656+ struct inode *inode;
17657+ ino_t ino;
17658+ int err;
17659+
17660+ inode = NULL;
17661+ err = au_xino_read(sb, bindex, h_ino, &ino);
17662+ if (!err && ino)
17663+ inode = ilookup(sb, ino);
17664+ if (!inode)
17665+ goto out;
17666+
17667+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17668+ pr_warn("wrong root branch\n");
4a4d8108
AM
17669+ iput(inode);
17670+ inode = NULL;
17671+ goto out;
1308ab2a 17672+ }
17673+
4a4d8108 17674+ ii_write_lock_child(inode);
1308ab2a 17675+
4f0767ce 17676+out:
4a4d8108 17677+ return inode;
dece6358
AM
17678+}
17679+
4a4d8108 17680+static void au_hn_bh(void *_args)
1facf9fc 17681+{
4a4d8108
AM
17682+ struct au_hnotify_args *a = _args;
17683+ struct super_block *sb;
5afbbe0d 17684+ aufs_bindex_t bindex, bbot, bfound;
4a4d8108 17685+ unsigned char xino, try_iput;
1facf9fc 17686+ int err;
1308ab2a 17687+ struct inode *inode;
4a4d8108
AM
17688+ ino_t h_ino;
17689+ struct hn_job_args args;
17690+ struct dentry *dentry;
17691+ struct au_sbinfo *sbinfo;
1facf9fc 17692+
4a4d8108
AM
17693+ AuDebugOn(!_args);
17694+ AuDebugOn(!a->h_dir);
17695+ AuDebugOn(!a->dir);
17696+ AuDebugOn(!a->mask);
17697+ AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
17698+ a->mask, a->dir->i_ino, a->h_dir->i_ino,
17699+ a->h_child_inode ? a->h_child_inode->i_ino : 0);
1facf9fc 17700+
4a4d8108
AM
17701+ inode = NULL;
17702+ dentry = NULL;
17703+ /*
17704+ * do not lock a->dir->i_mutex here
17705+ * because of d_revalidate() may cause a deadlock.
17706+ */
17707+ sb = a->dir->i_sb;
17708+ AuDebugOn(!sb);
17709+ sbinfo = au_sbi(sb);
17710+ AuDebugOn(!sbinfo);
7f207e10 17711+ si_write_lock(sb, AuLock_NOPLMW);
1facf9fc 17712+
8b6a4947
AM
17713+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
17714+ switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
17715+ case FS_MOVED_FROM:
17716+ case FS_MOVED_TO:
17717+ AuWarn1("DIRREN with UDBA may not work correctly "
17718+ "for the direct rename(2)\n");
17719+ }
17720+
4a4d8108
AM
17721+ ii_read_lock_parent(a->dir);
17722+ bfound = -1;
5afbbe0d
AM
17723+ bbot = au_ibbot(a->dir);
17724+ for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
4a4d8108
AM
17725+ if (au_h_iptr(a->dir, bindex) == a->h_dir) {
17726+ bfound = bindex;
17727+ break;
17728+ }
17729+ ii_read_unlock(a->dir);
17730+ if (unlikely(bfound < 0))
17731+ goto out;
1facf9fc 17732+
4a4d8108
AM
17733+ xino = !!au_opt_test(au_mntflags(sb), XINO);
17734+ h_ino = 0;
17735+ if (a->h_child_inode)
17736+ h_ino = a->h_child_inode->i_ino;
1facf9fc 17737+
4a4d8108
AM
17738+ if (a->h_child_nlen
17739+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
17740+ || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
17741+ dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
17742+ a->dir);
17743+ try_iput = 0;
5527c038
JR
17744+ if (dentry && d_really_is_positive(dentry))
17745+ inode = d_inode(dentry);
4a4d8108
AM
17746+ if (xino && !inode && h_ino
17747+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
17748+ || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
17749+ || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
17750+ inode = lookup_wlock_by_ino(sb, bfound, h_ino);
17751+ try_iput = 1;
f0c0a007 17752+ }
1facf9fc 17753+
4a4d8108
AM
17754+ args.flags = a->flags[AuHn_CHILD];
17755+ args.dentry = dentry;
17756+ args.inode = inode;
17757+ args.h_inode = a->h_child_inode;
17758+ args.dir = a->dir;
17759+ args.h_dir = a->h_dir;
17760+ args.h_name = a->h_child_name;
17761+ args.h_nlen = a->h_child_nlen;
17762+ err = hn_job(&args);
17763+ if (dentry) {
027c5e7a 17764+ if (au_di(dentry))
4a4d8108
AM
17765+ di_write_unlock(dentry);
17766+ dput(dentry);
17767+ }
17768+ if (inode && try_iput) {
17769+ ii_write_unlock(inode);
17770+ iput(inode);
17771+ }
1facf9fc 17772+
4a4d8108
AM
17773+ ii_write_lock_parent(a->dir);
17774+ args.flags = a->flags[AuHn_PARENT];
17775+ args.dentry = NULL;
17776+ args.inode = a->dir;
17777+ args.h_inode = a->h_dir;
17778+ args.dir = NULL;
17779+ args.h_dir = NULL;
17780+ args.h_name = NULL;
17781+ args.h_nlen = 0;
17782+ err = hn_job(&args);
17783+ ii_write_unlock(a->dir);
1facf9fc 17784+
4f0767ce 17785+out:
4a4d8108
AM
17786+ iput(a->h_child_inode);
17787+ iput(a->h_dir);
17788+ iput(a->dir);
027c5e7a
AM
17789+ si_write_unlock(sb);
17790+ au_nwt_done(&sbinfo->si_nowait);
e49925d1 17791+ au_kfree_rcu(a);
dece6358 17792+}
1facf9fc 17793+
4a4d8108
AM
17794+/* ---------------------------------------------------------------------- */
17795+
17796+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17797+ struct qstr *h_child_qstr, struct inode *h_child_inode)
dece6358 17798+{
4a4d8108 17799+ int err, len;
53392da6 17800+ unsigned int flags[AuHnLast], f;
4a4d8108
AM
17801+ unsigned char isdir, isroot, wh;
17802+ struct inode *dir;
17803+ struct au_hnotify_args *args;
17804+ char *p, *h_child_name;
dece6358 17805+
1308ab2a 17806+ err = 0;
4a4d8108
AM
17807+ AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
17808+ dir = igrab(hnotify->hn_aufs_inode);
17809+ if (!dir)
17810+ goto out;
1facf9fc 17811+
4a4d8108
AM
17812+ isroot = (dir->i_ino == AUFS_ROOT_INO);
17813+ wh = 0;
17814+ h_child_name = (void *)h_child_qstr->name;
17815+ len = h_child_qstr->len;
17816+ if (h_child_name) {
17817+ if (len > AUFS_WH_PFX_LEN
17818+ && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
17819+ h_child_name += AUFS_WH_PFX_LEN;
17820+ len -= AUFS_WH_PFX_LEN;
17821+ wh = 1;
17822+ }
1facf9fc 17823+ }
dece6358 17824+
4a4d8108
AM
17825+ isdir = 0;
17826+ if (h_child_inode)
17827+ isdir = !!S_ISDIR(h_child_inode->i_mode);
17828+ flags[AuHn_PARENT] = AuHnJob_ISDIR;
17829+ flags[AuHn_CHILD] = 0;
17830+ if (isdir)
17831+ flags[AuHn_CHILD] = AuHnJob_ISDIR;
17832+ au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
17833+ au_fset_hnjob(flags[AuHn_CHILD], GEN);
17834+ switch (mask & FS_EVENTS_POSS_ON_CHILD) {
17835+ case FS_MOVED_FROM:
17836+ case FS_MOVED_TO:
17837+ au_fset_hnjob(flags[AuHn_CHILD], XINO0);
17838+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17839+ /*FALLTHROUGH*/
17840+ case FS_CREATE:
fb47a38f 17841+ AuDebugOn(!h_child_name);
4a4d8108 17842+ break;
1facf9fc 17843+
4a4d8108
AM
17844+ case FS_DELETE:
17845+ /*
17846+ * aufs never be able to get this child inode.
17847+ * revalidation should be in d_revalidate()
17848+ * by checking i_nlink, i_generation or d_unhashed().
17849+ */
17850+ AuDebugOn(!h_child_name);
17851+ au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
17852+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17853+ break;
dece6358 17854+
4a4d8108
AM
17855+ default:
17856+ AuDebugOn(1);
17857+ }
1308ab2a 17858+
4a4d8108
AM
17859+ if (wh)
17860+ h_child_inode = NULL;
1308ab2a 17861+
4a4d8108
AM
17862+ err = -ENOMEM;
17863+ /* iput() and kfree() will be called in au_hnotify() */
4a4d8108 17864+ args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
4a4d8108
AM
17865+ if (unlikely(!args)) {
17866+ AuErr1("no memory\n");
17867+ iput(dir);
17868+ goto out;
17869+ }
17870+ args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
17871+ args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
17872+ args->mask = mask;
17873+ args->dir = dir;
17874+ args->h_dir = igrab(h_dir);
17875+ if (h_child_inode)
17876+ h_child_inode = igrab(h_child_inode); /* can be NULL */
17877+ args->h_child_inode = h_child_inode;
17878+ args->h_child_nlen = len;
17879+ if (len) {
17880+ p = (void *)args;
17881+ p += sizeof(*args);
17882+ memcpy(p, h_child_name, len);
17883+ p[len] = 0;
1308ab2a 17884+ }
1308ab2a 17885+
38d290e6 17886+ /* NFS fires the event for silly-renamed one from kworker */
53392da6 17887+ f = 0;
38d290e6
JR
17888+ if (!dir->i_nlink
17889+ || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
53392da6
AM
17890+ f = AuWkq_NEST;
17891+ err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
4a4d8108
AM
17892+ if (unlikely(err)) {
17893+ pr_err("wkq %d\n", err);
17894+ iput(args->h_child_inode);
17895+ iput(args->h_dir);
17896+ iput(args->dir);
e49925d1 17897+ au_kfree_rcu(args);
1facf9fc 17898+ }
1facf9fc 17899+
4a4d8108 17900+out:
1facf9fc 17901+ return err;
17902+}
17903+
027c5e7a
AM
17904+/* ---------------------------------------------------------------------- */
17905+
17906+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
17907+{
17908+ int err;
17909+
17910+ AuDebugOn(!(udba & AuOptMask_UDBA));
17911+
17912+ err = 0;
17913+ if (au_hnotify_op.reset_br)
17914+ err = au_hnotify_op.reset_br(udba, br, perm);
17915+
17916+ return err;
17917+}
17918+
17919+int au_hnotify_init_br(struct au_branch *br, int perm)
17920+{
17921+ int err;
17922+
17923+ err = 0;
17924+ if (au_hnotify_op.init_br)
17925+ err = au_hnotify_op.init_br(br, perm);
17926+
17927+ return err;
17928+}
17929+
17930+void au_hnotify_fin_br(struct au_branch *br)
17931+{
17932+ if (au_hnotify_op.fin_br)
17933+ au_hnotify_op.fin_br(br);
17934+}
17935+
4a4d8108
AM
17936+static void au_hn_destroy_cache(void)
17937+{
1c60b727
AM
17938+ kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
17939+ au_cache[AuCache_HNOTIFY] = NULL;
4a4d8108 17940+}
1308ab2a 17941+
4a4d8108 17942+int __init au_hnotify_init(void)
1facf9fc 17943+{
1308ab2a 17944+ int err;
1308ab2a 17945+
4a4d8108 17946+ err = -ENOMEM;
1c60b727
AM
17947+ au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
17948+ if (au_cache[AuCache_HNOTIFY]) {
027c5e7a
AM
17949+ err = 0;
17950+ if (au_hnotify_op.init)
17951+ err = au_hnotify_op.init();
4a4d8108
AM
17952+ if (unlikely(err))
17953+ au_hn_destroy_cache();
1308ab2a 17954+ }
1308ab2a 17955+ AuTraceErr(err);
4a4d8108 17956+ return err;
1308ab2a 17957+}
17958+
4a4d8108 17959+void au_hnotify_fin(void)
1308ab2a 17960+{
027c5e7a
AM
17961+ if (au_hnotify_op.fin)
17962+ au_hnotify_op.fin();
f0c0a007 17963+
4a4d8108 17964+ /* cf. au_cache_fin() */
1c60b727 17965+ if (au_cache[AuCache_HNOTIFY])
4a4d8108 17966+ au_hn_destroy_cache();
dece6358 17967+}
7f207e10
AM
17968diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
17969--- /usr/share/empty/fs/aufs/iinfo.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 17970+++ linux/fs/aufs/iinfo.c 2019-01-28 14:36:12.222418047 +0100
062440b3 17971@@ -0,0 +1,286 @@
cd7a4cd9 17972+// SPDX-License-Identifier: GPL-2.0
dece6358 17973+/*
b00004a5 17974+ * Copyright (C) 2005-2018 Junjiro R. Okajima
dece6358
AM
17975+ *
17976+ * This program, aufs is free software; you can redistribute it and/or modify
17977+ * it under the terms of the GNU General Public License as published by
17978+ * the Free Software Foundation; either version 2 of the License, or
17979+ * (at your option) any later version.
17980+ *
17981+ * This program is distributed in the hope that it will be useful,
17982+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17983+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17984+ * GNU General Public License for more details.
17985+ *
17986+ * You should have received a copy of the GNU General Public License
523b37e3 17987+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 17988+ */
1facf9fc 17989+
dece6358 17990+/*
4a4d8108 17991+ * inode private data
dece6358 17992+ */
1facf9fc 17993+
1308ab2a 17994+#include "aufs.h"
1facf9fc 17995+
4a4d8108 17996+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 17997+{
4a4d8108 17998+ struct inode *h_inode;
5afbbe0d 17999+ struct au_hinode *hinode;
1facf9fc 18000+
4a4d8108 18001+ IiMustAnyLock(inode);
1facf9fc 18002+
5afbbe0d
AM
18003+ hinode = au_hinode(au_ii(inode), bindex);
18004+ h_inode = hinode->hi_inode;
4a4d8108
AM
18005+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
18006+ return h_inode;
18007+}
1facf9fc 18008+
4a4d8108
AM
18009+/* todo: hard/soft set? */
18010+void au_hiput(struct au_hinode *hinode)
18011+{
18012+ au_hn_free(hinode);
18013+ dput(hinode->hi_whdentry);
18014+ iput(hinode->hi_inode);
18015+}
1facf9fc 18016+
4a4d8108
AM
18017+unsigned int au_hi_flags(struct inode *inode, int isdir)
18018+{
18019+ unsigned int flags;
18020+ const unsigned int mnt_flags = au_mntflags(inode->i_sb);
1facf9fc 18021+
4a4d8108
AM
18022+ flags = 0;
18023+ if (au_opt_test(mnt_flags, XINO))
18024+ au_fset_hi(flags, XINO);
18025+ if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
18026+ au_fset_hi(flags, HNOTIFY);
18027+ return flags;
1facf9fc 18028+}
18029+
4a4d8108
AM
18030+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
18031+ struct inode *h_inode, unsigned int flags)
1308ab2a 18032+{
4a4d8108
AM
18033+ struct au_hinode *hinode;
18034+ struct inode *hi;
18035+ struct au_iinfo *iinfo = au_ii(inode);
1facf9fc 18036+
4a4d8108 18037+ IiMustWriteLock(inode);
dece6358 18038+
5afbbe0d 18039+ hinode = au_hinode(iinfo, bindex);
4a4d8108
AM
18040+ hi = hinode->hi_inode;
18041+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
18042+
18043+ if (hi)
18044+ au_hiput(hinode);
18045+ hinode->hi_inode = h_inode;
18046+ if (h_inode) {
18047+ int err;
18048+ struct super_block *sb = inode->i_sb;
18049+ struct au_branch *br;
18050+
027c5e7a
AM
18051+ AuDebugOn(inode->i_mode
18052+ && (h_inode->i_mode & S_IFMT)
18053+ != (inode->i_mode & S_IFMT));
5afbbe0d 18054+ if (bindex == iinfo->ii_btop)
4a4d8108
AM
18055+ au_cpup_igen(inode, h_inode);
18056+ br = au_sbr(sb, bindex);
18057+ hinode->hi_id = br->br_id;
18058+ if (au_ftest_hi(flags, XINO)) {
18059+ err = au_xino_write(sb, bindex, h_inode->i_ino,
18060+ inode->i_ino);
18061+ if (unlikely(err))
18062+ AuIOErr1("failed au_xino_write() %d\n", err);
18063+ }
18064+
18065+ if (au_ftest_hi(flags, HNOTIFY)
18066+ && au_br_hnotifyable(br->br_perm)) {
027c5e7a 18067+ err = au_hn_alloc(hinode, inode);
4a4d8108
AM
18068+ if (unlikely(err))
18069+ AuIOErr1("au_hn_alloc() %d\n", err);
1308ab2a 18070+ }
18071+ }
4a4d8108 18072+}
dece6358 18073+
4a4d8108
AM
18074+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
18075+ struct dentry *h_wh)
18076+{
18077+ struct au_hinode *hinode;
dece6358 18078+
4a4d8108
AM
18079+ IiMustWriteLock(inode);
18080+
5afbbe0d 18081+ hinode = au_hinode(au_ii(inode), bindex);
4a4d8108
AM
18082+ AuDebugOn(hinode->hi_whdentry);
18083+ hinode->hi_whdentry = h_wh;
1facf9fc 18084+}
18085+
537831f9 18086+void au_update_iigen(struct inode *inode, int half)
1308ab2a 18087+{
537831f9
AM
18088+ struct au_iinfo *iinfo;
18089+ struct au_iigen *iigen;
18090+ unsigned int sigen;
18091+
18092+ sigen = au_sigen(inode->i_sb);
18093+ iinfo = au_ii(inode);
18094+ iigen = &iinfo->ii_generation;
be52b249 18095+ spin_lock(&iigen->ig_spin);
537831f9
AM
18096+ iigen->ig_generation = sigen;
18097+ if (half)
18098+ au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
18099+ else
18100+ au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
be52b249 18101+ spin_unlock(&iigen->ig_spin);
4a4d8108 18102+}
1facf9fc 18103+
4a4d8108
AM
18104+/* it may be called at remount time, too */
18105+void au_update_ibrange(struct inode *inode, int do_put_zero)
18106+{
18107+ struct au_iinfo *iinfo;
5afbbe0d 18108+ aufs_bindex_t bindex, bbot;
1facf9fc 18109+
5afbbe0d 18110+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 18111+ IiMustWriteLock(inode);
1facf9fc 18112+
5afbbe0d
AM
18113+ iinfo = au_ii(inode);
18114+ if (do_put_zero && iinfo->ii_btop >= 0) {
18115+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
18116+ bindex++) {
18117+ struct inode *h_i;
1facf9fc 18118+
5afbbe0d 18119+ h_i = au_hinode(iinfo, bindex)->hi_inode;
38d290e6
JR
18120+ if (h_i
18121+ && !h_i->i_nlink
18122+ && !(h_i->i_state & I_LINKABLE))
027c5e7a
AM
18123+ au_set_h_iptr(inode, bindex, NULL, 0);
18124+ }
4a4d8108
AM
18125+ }
18126+
5afbbe0d
AM
18127+ iinfo->ii_btop = -1;
18128+ iinfo->ii_bbot = -1;
18129+ bbot = au_sbbot(inode->i_sb);
18130+ for (bindex = 0; bindex <= bbot; bindex++)
18131+ if (au_hinode(iinfo, bindex)->hi_inode) {
18132+ iinfo->ii_btop = bindex;
4a4d8108 18133+ break;
027c5e7a 18134+ }
5afbbe0d
AM
18135+ if (iinfo->ii_btop >= 0)
18136+ for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
18137+ if (au_hinode(iinfo, bindex)->hi_inode) {
18138+ iinfo->ii_bbot = bindex;
027c5e7a
AM
18139+ break;
18140+ }
5afbbe0d 18141+ AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
1308ab2a 18142+}
1facf9fc 18143+
dece6358 18144+/* ---------------------------------------------------------------------- */
1facf9fc 18145+
4a4d8108 18146+void au_icntnr_init_once(void *_c)
dece6358 18147+{
4a4d8108
AM
18148+ struct au_icntnr *c = _c;
18149+ struct au_iinfo *iinfo = &c->iinfo;
1facf9fc 18150+
be52b249 18151+ spin_lock_init(&iinfo->ii_generation.ig_spin);
4a4d8108
AM
18152+ au_rw_init(&iinfo->ii_rwsem);
18153+ inode_init_once(&c->vfs_inode);
18154+}
1facf9fc 18155+
5afbbe0d
AM
18156+void au_hinode_init(struct au_hinode *hinode)
18157+{
18158+ hinode->hi_inode = NULL;
18159+ hinode->hi_id = -1;
18160+ au_hn_init(hinode);
18161+ hinode->hi_whdentry = NULL;
18162+}
18163+
4a4d8108
AM
18164+int au_iinfo_init(struct inode *inode)
18165+{
18166+ struct au_iinfo *iinfo;
18167+ struct super_block *sb;
5afbbe0d 18168+ struct au_hinode *hi;
4a4d8108 18169+ int nbr, i;
1facf9fc 18170+
4a4d8108
AM
18171+ sb = inode->i_sb;
18172+ iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
5afbbe0d 18173+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
18174+ if (unlikely(nbr <= 0))
18175+ nbr = 1;
5afbbe0d
AM
18176+ hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
18177+ if (hi) {
acd2b654 18178+ au_lcnt_inc(&au_sbi(sb)->si_ninodes);
5afbbe0d
AM
18179+
18180+ iinfo->ii_hinode = hi;
18181+ for (i = 0; i < nbr; i++, hi++)
18182+ au_hinode_init(hi);
1facf9fc 18183+
537831f9 18184+ iinfo->ii_generation.ig_generation = au_sigen(sb);
5afbbe0d
AM
18185+ iinfo->ii_btop = -1;
18186+ iinfo->ii_bbot = -1;
4a4d8108
AM
18187+ iinfo->ii_vdir = NULL;
18188+ return 0;
1308ab2a 18189+ }
4a4d8108
AM
18190+ return -ENOMEM;
18191+}
1facf9fc 18192+
e2f27e51 18193+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
4a4d8108 18194+{
5afbbe0d 18195+ int err, i;
4a4d8108 18196+ struct au_hinode *hip;
1facf9fc 18197+
4a4d8108
AM
18198+ AuRwMustWriteLock(&iinfo->ii_rwsem);
18199+
18200+ err = -ENOMEM;
e2f27e51
AM
18201+ hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
18202+ may_shrink);
4a4d8108
AM
18203+ if (hip) {
18204+ iinfo->ii_hinode = hip;
5afbbe0d
AM
18205+ i = iinfo->ii_bbot + 1;
18206+ hip += i;
18207+ for (; i < nbr; i++, hip++)
18208+ au_hinode_init(hip);
4a4d8108 18209+ err = 0;
1308ab2a 18210+ }
4a4d8108 18211+
1308ab2a 18212+ return err;
1facf9fc 18213+}
18214+
4a4d8108 18215+void au_iinfo_fin(struct inode *inode)
1facf9fc 18216+{
4a4d8108
AM
18217+ struct au_iinfo *iinfo;
18218+ struct au_hinode *hi;
18219+ struct super_block *sb;
5afbbe0d 18220+ aufs_bindex_t bindex, bbot;
b752ccd1 18221+ const unsigned char unlinked = !inode->i_nlink;
1308ab2a 18222+
5afbbe0d 18223+ AuDebugOn(au_is_bad_inode(inode));
1308ab2a 18224+
b752ccd1 18225+ sb = inode->i_sb;
acd2b654 18226+ au_lcnt_dec(&au_sbi(sb)->si_ninodes);
b752ccd1
AM
18227+ if (si_pid_test(sb))
18228+ au_xino_delete_inode(inode, unlinked);
18229+ else {
18230+ /*
18231+ * it is safe to hide the dependency between sbinfo and
18232+ * sb->s_umount.
18233+ */
18234+ lockdep_off();
18235+ si_noflush_read_lock(sb);
18236+ au_xino_delete_inode(inode, unlinked);
18237+ si_read_unlock(sb);
18238+ lockdep_on();
18239+ }
18240+
5afbbe0d 18241+ iinfo = au_ii(inode);
4a4d8108 18242+ if (iinfo->ii_vdir)
1c60b727 18243+ au_vdir_free(iinfo->ii_vdir);
1308ab2a 18244+
5afbbe0d 18245+ bindex = iinfo->ii_btop;
b752ccd1 18246+ if (bindex >= 0) {
5afbbe0d
AM
18247+ hi = au_hinode(iinfo, bindex);
18248+ bbot = iinfo->ii_bbot;
18249+ while (bindex++ <= bbot) {
b752ccd1 18250+ if (hi->hi_inode)
4a4d8108 18251+ au_hiput(hi);
4a4d8108
AM
18252+ hi++;
18253+ }
18254+ }
e49925d1 18255+ au_kfree_rcu(iinfo->ii_hinode);
4a4d8108 18256+ AuRwDestroy(&iinfo->ii_rwsem);
dece6358 18257+}
7f207e10
AM
18258diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
18259--- /usr/share/empty/fs/aufs/inode.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 18260+++ linux/fs/aufs/inode.c 2019-01-28 14:36:12.222418047 +0100
062440b3 18261@@ -0,0 +1,528 @@
cd7a4cd9 18262+// SPDX-License-Identifier: GPL-2.0
4a4d8108 18263+/*
b00004a5 18264+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
18265+ *
18266+ * This program, aufs is free software; you can redistribute it and/or modify
18267+ * it under the terms of the GNU General Public License as published by
18268+ * the Free Software Foundation; either version 2 of the License, or
18269+ * (at your option) any later version.
18270+ *
18271+ * This program is distributed in the hope that it will be useful,
18272+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18273+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18274+ * GNU General Public License for more details.
18275+ *
18276+ * You should have received a copy of the GNU General Public License
523b37e3 18277+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 18278+ */
1facf9fc 18279+
4a4d8108
AM
18280+/*
18281+ * inode functions
18282+ */
1facf9fc 18283+
4a4d8108 18284+#include "aufs.h"
1308ab2a 18285+
4a4d8108
AM
18286+struct inode *au_igrab(struct inode *inode)
18287+{
18288+ if (inode) {
18289+ AuDebugOn(!atomic_read(&inode->i_count));
027c5e7a 18290+ ihold(inode);
1facf9fc 18291+ }
4a4d8108
AM
18292+ return inode;
18293+}
1facf9fc 18294+
4a4d8108
AM
18295+static void au_refresh_hinode_attr(struct inode *inode, int do_version)
18296+{
18297+ au_cpup_attr_all(inode, /*force*/0);
537831f9 18298+ au_update_iigen(inode, /*half*/1);
4a4d8108 18299+ if (do_version)
be118d29 18300+ inode_inc_iversion(inode);
dece6358 18301+}
1facf9fc 18302+
027c5e7a 18303+static int au_ii_refresh(struct inode *inode, int *update)
dece6358 18304+{
e2f27e51 18305+ int err, e, nbr;
027c5e7a 18306+ umode_t type;
4a4d8108 18307+ aufs_bindex_t bindex, new_bindex;
1308ab2a 18308+ struct super_block *sb;
4a4d8108 18309+ struct au_iinfo *iinfo;
027c5e7a 18310+ struct au_hinode *p, *q, tmp;
1facf9fc 18311+
5afbbe0d 18312+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 18313+ IiMustWriteLock(inode);
1facf9fc 18314+
027c5e7a 18315+ *update = 0;
4a4d8108 18316+ sb = inode->i_sb;
e2f27e51 18317+ nbr = au_sbbot(sb) + 1;
027c5e7a 18318+ type = inode->i_mode & S_IFMT;
4a4d8108 18319+ iinfo = au_ii(inode);
e2f27e51 18320+ err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
4a4d8108 18321+ if (unlikely(err))
1308ab2a 18322+ goto out;
1facf9fc 18323+
5afbbe0d
AM
18324+ AuDebugOn(iinfo->ii_btop < 0);
18325+ p = au_hinode(iinfo, iinfo->ii_btop);
18326+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
18327+ bindex++, p++) {
18328+ if (!p->hi_inode)
18329+ continue;
1facf9fc 18330+
027c5e7a 18331+ AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
4a4d8108
AM
18332+ new_bindex = au_br_index(sb, p->hi_id);
18333+ if (new_bindex == bindex)
18334+ continue;
1facf9fc 18335+
4a4d8108 18336+ if (new_bindex < 0) {
027c5e7a 18337+ *update = 1;
4a4d8108
AM
18338+ au_hiput(p);
18339+ p->hi_inode = NULL;
18340+ continue;
1308ab2a 18341+ }
4a4d8108 18342+
5afbbe0d
AM
18343+ if (new_bindex < iinfo->ii_btop)
18344+ iinfo->ii_btop = new_bindex;
18345+ if (iinfo->ii_bbot < new_bindex)
18346+ iinfo->ii_bbot = new_bindex;
4a4d8108 18347+ /* swap two lower inode, and loop again */
5afbbe0d 18348+ q = au_hinode(iinfo, new_bindex);
4a4d8108
AM
18349+ tmp = *q;
18350+ *q = *p;
18351+ *p = tmp;
18352+ if (tmp.hi_inode) {
18353+ bindex--;
18354+ p--;
1308ab2a 18355+ }
18356+ }
4a4d8108 18357+ au_update_ibrange(inode, /*do_put_zero*/0);
e2f27e51 18358+ au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
4a4d8108
AM
18359+ e = au_dy_irefresh(inode);
18360+ if (unlikely(e && !err))
18361+ err = e;
1facf9fc 18362+
4f0767ce 18363+out:
027c5e7a
AM
18364+ AuTraceErr(err);
18365+ return err;
18366+}
18367+
b95c5147
AM
18368+void au_refresh_iop(struct inode *inode, int force_getattr)
18369+{
18370+ int type;
18371+ struct au_sbinfo *sbi = au_sbi(inode->i_sb);
18372+ const struct inode_operations *iop
18373+ = force_getattr ? aufs_iop : sbi->si_iop_array;
18374+
18375+ if (inode->i_op == iop)
18376+ return;
18377+
18378+ switch (inode->i_mode & S_IFMT) {
18379+ case S_IFDIR:
18380+ type = AuIop_DIR;
18381+ break;
18382+ case S_IFLNK:
18383+ type = AuIop_SYMLINK;
18384+ break;
18385+ default:
18386+ type = AuIop_OTHER;
18387+ break;
18388+ }
18389+
18390+ inode->i_op = iop + type;
18391+ /* unnecessary smp_wmb() */
18392+}
18393+
027c5e7a
AM
18394+int au_refresh_hinode_self(struct inode *inode)
18395+{
18396+ int err, update;
18397+
18398+ err = au_ii_refresh(inode, &update);
18399+ if (!err)
18400+ au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
18401+
18402+ AuTraceErr(err);
4a4d8108
AM
18403+ return err;
18404+}
1facf9fc 18405+
4a4d8108
AM
18406+int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
18407+{
027c5e7a 18408+ int err, e, update;
4a4d8108 18409+ unsigned int flags;
027c5e7a 18410+ umode_t mode;
5afbbe0d 18411+ aufs_bindex_t bindex, bbot;
027c5e7a 18412+ unsigned char isdir;
4a4d8108
AM
18413+ struct au_hinode *p;
18414+ struct au_iinfo *iinfo;
1facf9fc 18415+
027c5e7a 18416+ err = au_ii_refresh(inode, &update);
4a4d8108
AM
18417+ if (unlikely(err))
18418+ goto out;
18419+
18420+ update = 0;
18421+ iinfo = au_ii(inode);
5afbbe0d 18422+ p = au_hinode(iinfo, iinfo->ii_btop);
027c5e7a
AM
18423+ mode = (inode->i_mode & S_IFMT);
18424+ isdir = S_ISDIR(mode);
4a4d8108 18425+ flags = au_hi_flags(inode, isdir);
5afbbe0d
AM
18426+ bbot = au_dbbot(dentry);
18427+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
5527c038 18428+ struct inode *h_i, *h_inode;
4a4d8108
AM
18429+ struct dentry *h_d;
18430+
18431+ h_d = au_h_dptr(dentry, bindex);
5527c038 18432+ if (!h_d || d_is_negative(h_d))
4a4d8108
AM
18433+ continue;
18434+
5527c038
JR
18435+ h_inode = d_inode(h_d);
18436+ AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
5afbbe0d 18437+ if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
4a4d8108
AM
18438+ h_i = au_h_iptr(inode, bindex);
18439+ if (h_i) {
5527c038 18440+ if (h_i == h_inode)
4a4d8108
AM
18441+ continue;
18442+ err = -EIO;
18443+ break;
18444+ }
18445+ }
5afbbe0d
AM
18446+ if (bindex < iinfo->ii_btop)
18447+ iinfo->ii_btop = bindex;
18448+ if (iinfo->ii_bbot < bindex)
18449+ iinfo->ii_bbot = bindex;
5527c038 18450+ au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
4a4d8108 18451+ update = 1;
1308ab2a 18452+ }
4a4d8108
AM
18453+ au_update_ibrange(inode, /*do_put_zero*/0);
18454+ e = au_dy_irefresh(inode);
18455+ if (unlikely(e && !err))
18456+ err = e;
027c5e7a
AM
18457+ if (!err)
18458+ au_refresh_hinode_attr(inode, update && isdir);
4a4d8108 18459+
4f0767ce 18460+out:
4a4d8108 18461+ AuTraceErr(err);
1308ab2a 18462+ return err;
dece6358
AM
18463+}
18464+
4a4d8108 18465+static int set_inode(struct inode *inode, struct dentry *dentry)
dece6358 18466+{
4a4d8108
AM
18467+ int err;
18468+ unsigned int flags;
18469+ umode_t mode;
5afbbe0d 18470+ aufs_bindex_t bindex, btop, btail;
4a4d8108
AM
18471+ unsigned char isdir;
18472+ struct dentry *h_dentry;
18473+ struct inode *h_inode;
18474+ struct au_iinfo *iinfo;
b95c5147 18475+ struct inode_operations *iop;
dece6358 18476+
4a4d8108 18477+ IiMustWriteLock(inode);
dece6358 18478+
4a4d8108
AM
18479+ err = 0;
18480+ isdir = 0;
b95c5147 18481+ iop = au_sbi(inode->i_sb)->si_iop_array;
5afbbe0d
AM
18482+ btop = au_dbtop(dentry);
18483+ h_dentry = au_h_dptr(dentry, btop);
5527c038 18484+ h_inode = d_inode(h_dentry);
4a4d8108
AM
18485+ mode = h_inode->i_mode;
18486+ switch (mode & S_IFMT) {
18487+ case S_IFREG:
18488+ btail = au_dbtail(dentry);
b95c5147 18489+ inode->i_op = iop + AuIop_OTHER;
4a4d8108 18490+ inode->i_fop = &aufs_file_fop;
5afbbe0d 18491+ err = au_dy_iaop(inode, btop, h_inode);
4a4d8108
AM
18492+ if (unlikely(err))
18493+ goto out;
18494+ break;
18495+ case S_IFDIR:
18496+ isdir = 1;
18497+ btail = au_dbtaildir(dentry);
b95c5147 18498+ inode->i_op = iop + AuIop_DIR;
4a4d8108
AM
18499+ inode->i_fop = &aufs_dir_fop;
18500+ break;
18501+ case S_IFLNK:
18502+ btail = au_dbtail(dentry);
b95c5147 18503+ inode->i_op = iop + AuIop_SYMLINK;
4a4d8108
AM
18504+ break;
18505+ case S_IFBLK:
18506+ case S_IFCHR:
18507+ case S_IFIFO:
18508+ case S_IFSOCK:
18509+ btail = au_dbtail(dentry);
b95c5147 18510+ inode->i_op = iop + AuIop_OTHER;
38d290e6 18511+ init_special_inode(inode, mode, h_inode->i_rdev);
4a4d8108
AM
18512+ break;
18513+ default:
18514+ AuIOErr("Unknown file type 0%o\n", mode);
18515+ err = -EIO;
1308ab2a 18516+ goto out;
4a4d8108 18517+ }
dece6358 18518+
4a4d8108
AM
18519+ /* do not set hnotify for whiteouted dirs (SHWH mode) */
18520+ flags = au_hi_flags(inode, isdir);
18521+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
18522+ && au_ftest_hi(flags, HNOTIFY)
18523+ && dentry->d_name.len > AUFS_WH_PFX_LEN
18524+ && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
18525+ au_fclr_hi(flags, HNOTIFY);
18526+ iinfo = au_ii(inode);
5afbbe0d
AM
18527+ iinfo->ii_btop = btop;
18528+ iinfo->ii_bbot = btail;
18529+ for (bindex = btop; bindex <= btail; bindex++) {
4a4d8108
AM
18530+ h_dentry = au_h_dptr(dentry, bindex);
18531+ if (h_dentry)
18532+ au_set_h_iptr(inode, bindex,
5527c038 18533+ au_igrab(d_inode(h_dentry)), flags);
4a4d8108
AM
18534+ }
18535+ au_cpup_attr_all(inode, /*force*/1);
c1595e42
JR
18536+ /*
18537+ * to force calling aufs_get_acl() every time,
18538+ * do not call cache_no_acl() for aufs inode.
18539+ */
dece6358 18540+
4f0767ce 18541+out:
4a4d8108
AM
18542+ return err;
18543+}
dece6358 18544+
027c5e7a
AM
18545+/*
18546+ * successful returns with iinfo write_locked
18547+ * minus: errno
18548+ * zero: success, matched
18549+ * plus: no error, but unmatched
18550+ */
18551+static int reval_inode(struct inode *inode, struct dentry *dentry)
4a4d8108
AM
18552+{
18553+ int err;
cfc41e69 18554+ unsigned int gen, igflags;
5afbbe0d 18555+ aufs_bindex_t bindex, bbot;
4a4d8108 18556+ struct inode *h_inode, *h_dinode;
5527c038 18557+ struct dentry *h_dentry;
dece6358 18558+
4a4d8108
AM
18559+ /*
18560+ * before this function, if aufs got any iinfo lock, it must be only
18561+ * one, the parent dir.
18562+ * it can happen by UDBA and the obsoleted inode number.
18563+ */
18564+ err = -EIO;
18565+ if (unlikely(inode->i_ino == parent_ino(dentry)))
18566+ goto out;
18567+
027c5e7a 18568+ err = 1;
4a4d8108 18569+ ii_write_lock_new_child(inode);
5afbbe0d 18570+ h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
5527c038 18571+ h_dinode = d_inode(h_dentry);
5afbbe0d
AM
18572+ bbot = au_ibbot(inode);
18573+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108 18574+ h_inode = au_h_iptr(inode, bindex);
537831f9
AM
18575+ if (!h_inode || h_inode != h_dinode)
18576+ continue;
18577+
18578+ err = 0;
cfc41e69 18579+ gen = au_iigen(inode, &igflags);
537831f9 18580+ if (gen == au_digen(dentry)
cfc41e69 18581+ && !au_ig_ftest(igflags, HALF_REFRESHED))
4a4d8108 18582+ break;
537831f9
AM
18583+
18584+ /* fully refresh inode using dentry */
18585+ err = au_refresh_hinode(inode, dentry);
18586+ if (!err)
18587+ au_update_iigen(inode, /*half*/0);
18588+ break;
1facf9fc 18589+ }
dece6358 18590+
4a4d8108
AM
18591+ if (unlikely(err))
18592+ ii_write_unlock(inode);
4f0767ce 18593+out:
1facf9fc 18594+ return err;
18595+}
1facf9fc 18596+
4a4d8108
AM
18597+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18598+ unsigned int d_type, ino_t *ino)
1facf9fc 18599+{
521ced18
JR
18600+ int err, idx;
18601+ const int isnondir = d_type != DT_DIR;
1facf9fc 18602+
b752ccd1 18603+ /* prevent hardlinked inode number from race condition */
521ced18
JR
18604+ if (isnondir) {
18605+ err = au_xinondir_enter(sb, bindex, h_ino, &idx);
18606+ if (unlikely(err))
18607+ goto out;
4a4d8108 18608+ }
521ced18 18609+
4a4d8108
AM
18610+ err = au_xino_read(sb, bindex, h_ino, ino);
18611+ if (unlikely(err))
521ced18 18612+ goto out_xinondir;
1308ab2a 18613+
4a4d8108
AM
18614+ if (!*ino) {
18615+ err = -EIO;
18616+ *ino = au_xino_new_ino(sb);
18617+ if (unlikely(!*ino))
521ced18 18618+ goto out_xinondir;
4a4d8108
AM
18619+ err = au_xino_write(sb, bindex, h_ino, *ino);
18620+ if (unlikely(err))
521ced18 18621+ goto out_xinondir;
1308ab2a 18622+ }
1facf9fc 18623+
521ced18
JR
18624+out_xinondir:
18625+ if (isnondir && idx >= 0)
18626+ au_xinondir_leave(sb, bindex, h_ino, idx);
4f0767ce 18627+out:
1facf9fc 18628+ return err;
18629+}
18630+
4a4d8108
AM
18631+/* successful returns with iinfo write_locked */
18632+/* todo: return with unlocked? */
18633+struct inode *au_new_inode(struct dentry *dentry, int must_new)
1facf9fc 18634+{
5527c038 18635+ struct inode *inode, *h_inode;
4a4d8108
AM
18636+ struct dentry *h_dentry;
18637+ struct super_block *sb;
18638+ ino_t h_ino, ino;
521ced18 18639+ int err, idx, hlinked;
5afbbe0d 18640+ aufs_bindex_t btop;
1facf9fc 18641+
4a4d8108 18642+ sb = dentry->d_sb;
5afbbe0d
AM
18643+ btop = au_dbtop(dentry);
18644+ h_dentry = au_h_dptr(dentry, btop);
5527c038
JR
18645+ h_inode = d_inode(h_dentry);
18646+ h_ino = h_inode->i_ino;
521ced18 18647+ hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
b752ccd1 18648+
521ced18 18649+new_ino:
b752ccd1
AM
18650+ /*
18651+ * stop 'race'-ing between hardlinks under different
18652+ * parents.
18653+ */
521ced18
JR
18654+ if (hlinked) {
18655+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18656+ inode = ERR_PTR(err);
18657+ if (unlikely(err))
18658+ goto out;
18659+ }
b752ccd1 18660+
5afbbe0d 18661+ err = au_xino_read(sb, btop, h_ino, &ino);
4a4d8108
AM
18662+ inode = ERR_PTR(err);
18663+ if (unlikely(err))
521ced18 18664+ goto out_xinondir;
b752ccd1 18665+
4a4d8108
AM
18666+ if (!ino) {
18667+ ino = au_xino_new_ino(sb);
18668+ if (unlikely(!ino)) {
18669+ inode = ERR_PTR(-EIO);
521ced18 18670+ goto out_xinondir;
dece6358
AM
18671+ }
18672+ }
1facf9fc 18673+
4a4d8108
AM
18674+ AuDbg("i%lu\n", (unsigned long)ino);
18675+ inode = au_iget_locked(sb, ino);
18676+ err = PTR_ERR(inode);
18677+ if (IS_ERR(inode))
521ced18 18678+ goto out_xinondir;
1facf9fc 18679+
4a4d8108
AM
18680+ AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
18681+ if (inode->i_state & I_NEW) {
18682+ ii_write_lock_new_child(inode);
18683+ err = set_inode(inode, dentry);
18684+ if (!err) {
18685+ unlock_new_inode(inode);
521ced18 18686+ goto out_xinondir; /* success */
4a4d8108 18687+ }
1308ab2a 18688+
027c5e7a
AM
18689+ /*
18690+ * iget_failed() calls iput(), but we need to call
18691+ * ii_write_unlock() after iget_failed(). so dirty hack for
18692+ * i_count.
18693+ */
18694+ atomic_inc(&inode->i_count);
4a4d8108 18695+ iget_failed(inode);
027c5e7a 18696+ ii_write_unlock(inode);
5afbbe0d 18697+ au_xino_write(sb, btop, h_ino, /*ino*/0);
027c5e7a
AM
18698+ /* ignore this error */
18699+ goto out_iput;
18700+ } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
b752ccd1
AM
18701+ /*
18702+ * horrible race condition between lookup, readdir and copyup
18703+ * (or something).
18704+ */
521ced18
JR
18705+ if (hlinked && idx >= 0)
18706+ au_xinondir_leave(sb, btop, h_ino, idx);
027c5e7a
AM
18707+ err = reval_inode(inode, dentry);
18708+ if (unlikely(err < 0)) {
521ced18 18709+ hlinked = 0;
027c5e7a
AM
18710+ goto out_iput;
18711+ }
521ced18 18712+ if (!err)
4a4d8108 18713+ goto out; /* success */
521ced18
JR
18714+ else if (hlinked && idx >= 0) {
18715+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18716+ if (unlikely(err)) {
18717+ iput(inode);
18718+ inode = ERR_PTR(err);
18719+ goto out;
18720+ }
18721+ }
4a4d8108
AM
18722+ }
18723+
5527c038 18724+ if (unlikely(au_test_fs_unique_ino(h_inode)))
4a4d8108 18725+ AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
523b37e3 18726+ " b%d, %s, %pd, hi%lu, i%lu.\n",
5afbbe0d 18727+ btop, au_sbtype(h_dentry->d_sb), dentry,
4a4d8108
AM
18728+ (unsigned long)h_ino, (unsigned long)ino);
18729+ ino = 0;
5afbbe0d 18730+ err = au_xino_write(sb, btop, h_ino, /*ino*/0);
4a4d8108
AM
18731+ if (!err) {
18732+ iput(inode);
521ced18
JR
18733+ if (hlinked && idx >= 0)
18734+ au_xinondir_leave(sb, btop, h_ino, idx);
4a4d8108
AM
18735+ goto new_ino;
18736+ }
1308ab2a 18737+
4f0767ce 18738+out_iput:
4a4d8108 18739+ iput(inode);
4a4d8108 18740+ inode = ERR_PTR(err);
521ced18
JR
18741+out_xinondir:
18742+ if (hlinked && idx >= 0)
18743+ au_xinondir_leave(sb, btop, h_ino, idx);
4f0767ce 18744+out:
4a4d8108 18745+ return inode;
1facf9fc 18746+}
18747+
4a4d8108 18748+/* ---------------------------------------------------------------------- */
1facf9fc 18749+
4a4d8108
AM
18750+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18751+ struct inode *inode)
18752+{
18753+ int err;
076b876e 18754+ struct inode *hi;
1facf9fc 18755+
4a4d8108 18756+ err = au_br_rdonly(au_sbr(sb, bindex));
1facf9fc 18757+
4a4d8108
AM
18758+ /* pseudo-link after flushed may happen out of bounds */
18759+ if (!err
18760+ && inode
5afbbe0d
AM
18761+ && au_ibtop(inode) <= bindex
18762+ && bindex <= au_ibbot(inode)) {
4a4d8108
AM
18763+ /*
18764+ * permission check is unnecessary since vfsub routine
18765+ * will be called later
18766+ */
076b876e 18767+ hi = au_h_iptr(inode, bindex);
4a4d8108
AM
18768+ if (hi)
18769+ err = IS_IMMUTABLE(hi) ? -EROFS : 0;
1facf9fc 18770+ }
18771+
4a4d8108
AM
18772+ return err;
18773+}
dece6358 18774+
4a4d8108
AM
18775+int au_test_h_perm(struct inode *h_inode, int mask)
18776+{
2dfbb274 18777+ if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
4a4d8108
AM
18778+ return 0;
18779+ return inode_permission(h_inode, mask);
18780+}
1facf9fc 18781+
4a4d8108
AM
18782+int au_test_h_perm_sio(struct inode *h_inode, int mask)
18783+{
18784+ if (au_test_nfs(h_inode->i_sb)
18785+ && (mask & MAY_WRITE)
18786+ && S_ISDIR(h_inode->i_mode))
18787+ mask |= MAY_READ; /* force permission check */
18788+ return au_test_h_perm(h_inode, mask);
1facf9fc 18789+}
7f207e10
AM
18790diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
18791--- /usr/share/empty/fs/aufs/inode.h 1970-01-01 01:00:00.000000000 +0100
e49925d1
JR
18792+++ linux/fs/aufs/inode.h 2019-01-28 14:36:12.222418047 +0100
18793@@ -0,0 +1,698 @@
062440b3 18794+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 18795+/*
b00004a5 18796+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
18797+ *
18798+ * This program, aufs is free software; you can redistribute it and/or modify
18799+ * it under the terms of the GNU General Public License as published by
18800+ * the Free Software Foundation; either version 2 of the License, or
18801+ * (at your option) any later version.
18802+ *
18803+ * This program is distributed in the hope that it will be useful,
18804+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18805+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18806+ * GNU General Public License for more details.
18807+ *
18808+ * You should have received a copy of the GNU General Public License
523b37e3 18809+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 18810+ */
1facf9fc 18811+
1308ab2a 18812+/*
4a4d8108 18813+ * inode operations
1308ab2a 18814+ */
dece6358 18815+
4a4d8108
AM
18816+#ifndef __AUFS_INODE_H__
18817+#define __AUFS_INODE_H__
dece6358 18818+
4a4d8108 18819+#ifdef __KERNEL__
1308ab2a 18820+
4a4d8108 18821+#include <linux/fsnotify.h>
4a4d8108 18822+#include "rwsem.h"
1308ab2a 18823+
4a4d8108 18824+struct vfsmount;
1facf9fc 18825+
4a4d8108
AM
18826+struct au_hnotify {
18827+#ifdef CONFIG_AUFS_HNOTIFY
18828+#ifdef CONFIG_AUFS_HFSNOTIFY
7f207e10 18829+ /* never use fsnotify_add_vfsmount_mark() */
0c5527e5 18830+ struct fsnotify_mark hn_mark;
4a4d8108 18831+#endif
1c60b727 18832+ struct inode *hn_aufs_inode; /* no get/put */
e49925d1 18833+ struct rcu_head rcu;
4a4d8108
AM
18834+#endif
18835+} ____cacheline_aligned_in_smp;
1facf9fc 18836+
4a4d8108
AM
18837+struct au_hinode {
18838+ struct inode *hi_inode;
18839+ aufs_bindex_t hi_id;
18840+#ifdef CONFIG_AUFS_HNOTIFY
18841+ struct au_hnotify *hi_notify;
18842+#endif
dece6358 18843+
4a4d8108
AM
18844+ /* reference to the copied-up whiteout with get/put */
18845+ struct dentry *hi_whdentry;
18846+};
dece6358 18847+
537831f9
AM
18848+/* ig_flags */
18849+#define AuIG_HALF_REFRESHED 1
18850+#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
18851+#define au_ig_fset(flags, name) \
18852+ do { (flags) |= AuIG_##name; } while (0)
18853+#define au_ig_fclr(flags, name) \
18854+ do { (flags) &= ~AuIG_##name; } while (0)
18855+
18856+struct au_iigen {
be52b249 18857+ spinlock_t ig_spin;
537831f9
AM
18858+ __u32 ig_generation, ig_flags;
18859+};
18860+
4a4d8108
AM
18861+struct au_vdir;
18862+struct au_iinfo {
7a9e40b8 18863+ struct au_iigen ii_generation;
4a4d8108 18864+ struct super_block *ii_hsb1; /* no get/put */
1facf9fc 18865+
4a4d8108 18866+ struct au_rwsem ii_rwsem;
5afbbe0d 18867+ aufs_bindex_t ii_btop, ii_bbot;
4a4d8108
AM
18868+ __u32 ii_higen;
18869+ struct au_hinode *ii_hinode;
18870+ struct au_vdir *ii_vdir;
18871+};
1facf9fc 18872+
4a4d8108 18873+struct au_icntnr {
e49925d1
JR
18874+ struct au_iinfo iinfo;
18875+ struct inode vfs_inode;
18876+ struct hlist_bl_node plink;
18877+ struct rcu_head rcu;
4a4d8108 18878+} ____cacheline_aligned_in_smp;
1308ab2a 18879+
4a4d8108
AM
18880+/* au_pin flags */
18881+#define AuPin_DI_LOCKED 1
18882+#define AuPin_MNT_WRITE (1 << 1)
18883+#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
7f207e10
AM
18884+#define au_fset_pin(flags, name) \
18885+ do { (flags) |= AuPin_##name; } while (0)
18886+#define au_fclr_pin(flags, name) \
18887+ do { (flags) &= ~AuPin_##name; } while (0)
4a4d8108
AM
18888+
18889+struct au_pin {
18890+ /* input */
18891+ struct dentry *dentry;
18892+ unsigned int udba;
18893+ unsigned char lsc_di, lsc_hi, flags;
18894+ aufs_bindex_t bindex;
18895+
18896+ /* output */
18897+ struct dentry *parent;
18898+ struct au_hinode *hdir;
18899+ struct vfsmount *h_mnt;
86dc4139
AM
18900+
18901+ /* temporary unlock/relock for copyup */
18902+ struct dentry *h_dentry, *h_parent;
18903+ struct au_branch *br;
18904+ struct task_struct *task;
4a4d8108 18905+};
1facf9fc 18906+
86dc4139 18907+void au_pin_hdir_unlock(struct au_pin *p);
c1595e42 18908+int au_pin_hdir_lock(struct au_pin *p);
86dc4139 18909+int au_pin_hdir_relock(struct au_pin *p);
86dc4139
AM
18910+void au_pin_hdir_acquire_nest(struct au_pin *p);
18911+void au_pin_hdir_release(struct au_pin *p);
18912+
1308ab2a 18913+/* ---------------------------------------------------------------------- */
18914+
4a4d8108 18915+static inline struct au_iinfo *au_ii(struct inode *inode)
1facf9fc 18916+{
5afbbe0d
AM
18917+ BUG_ON(is_bad_inode(inode));
18918+ return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
4a4d8108 18919+}
1facf9fc 18920+
4a4d8108 18921+/* ---------------------------------------------------------------------- */
1facf9fc 18922+
4a4d8108
AM
18923+/* inode.c */
18924+struct inode *au_igrab(struct inode *inode);
b95c5147 18925+void au_refresh_iop(struct inode *inode, int force_getattr);
027c5e7a 18926+int au_refresh_hinode_self(struct inode *inode);
4a4d8108
AM
18927+int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
18928+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18929+ unsigned int d_type, ino_t *ino);
18930+struct inode *au_new_inode(struct dentry *dentry, int must_new);
18931+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18932+ struct inode *inode);
18933+int au_test_h_perm(struct inode *h_inode, int mask);
18934+int au_test_h_perm_sio(struct inode *h_inode, int mask);
1facf9fc 18935+
4a4d8108
AM
18936+static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
18937+ ino_t h_ino, unsigned int d_type, ino_t *ino)
18938+{
18939+#ifdef CONFIG_AUFS_SHWH
18940+ return au_ino(sb, bindex, h_ino, d_type, ino);
18941+#else
18942+ return 0;
18943+#endif
18944+}
1facf9fc 18945+
4a4d8108 18946+/* i_op.c */
b95c5147
AM
18947+enum {
18948+ AuIop_SYMLINK,
18949+ AuIop_DIR,
18950+ AuIop_OTHER,
18951+ AuIop_Last
18952+};
18953+extern struct inode_operations aufs_iop[AuIop_Last],
18954+ aufs_iop_nogetattr[AuIop_Last];
1308ab2a 18955+
4a4d8108
AM
18956+/* au_wr_dir flags */
18957+#define AuWrDir_ADD_ENTRY 1
7e9cd9fe
AM
18958+#define AuWrDir_ISDIR (1 << 1)
18959+#define AuWrDir_TMPFILE (1 << 2)
4a4d8108 18960+#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
7f207e10
AM
18961+#define au_fset_wrdir(flags, name) \
18962+ do { (flags) |= AuWrDir_##name; } while (0)
18963+#define au_fclr_wrdir(flags, name) \
18964+ do { (flags) &= ~AuWrDir_##name; } while (0)
1facf9fc 18965+
4a4d8108
AM
18966+struct au_wr_dir_args {
18967+ aufs_bindex_t force_btgt;
18968+ unsigned char flags;
18969+};
18970+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18971+ struct au_wr_dir_args *args);
dece6358 18972+
4a4d8108
AM
18973+struct dentry *au_pinned_h_parent(struct au_pin *pin);
18974+void au_pin_init(struct au_pin *pin, struct dentry *dentry,
18975+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18976+ unsigned int udba, unsigned char flags);
18977+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18978+ unsigned int udba, unsigned char flags) __must_check;
18979+int au_do_pin(struct au_pin *pin) __must_check;
18980+void au_unpin(struct au_pin *pin);
c1595e42
JR
18981+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
18982+
18983+#define AuIcpup_DID_CPUP 1
18984+#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
18985+#define au_fset_icpup(flags, name) \
18986+ do { (flags) |= AuIcpup_##name; } while (0)
18987+#define au_fclr_icpup(flags, name) \
18988+ do { (flags) &= ~AuIcpup_##name; } while (0)
18989+
18990+struct au_icpup_args {
18991+ unsigned char flags;
18992+ unsigned char pin_flags;
18993+ aufs_bindex_t btgt;
18994+ unsigned int udba;
18995+ struct au_pin pin;
18996+ struct path h_path;
18997+ struct inode *h_inode;
18998+};
18999+
19000+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
19001+ struct au_icpup_args *a);
19002+
a2654f78
AM
19003+int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
19004+ int locked);
1facf9fc 19005+
4a4d8108
AM
19006+/* i_op_add.c */
19007+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19008+ struct dentry *h_parent, int isdir);
7eafdf33
AM
19009+int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
19010+ dev_t dev);
4a4d8108 19011+int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
7eafdf33 19012+int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
b4510431 19013+ bool want_excl);
b912730e
AM
19014+struct vfsub_aopen_args;
19015+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
19016+ struct vfsub_aopen_args *args);
38d290e6 19017+int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
4a4d8108
AM
19018+int aufs_link(struct dentry *src_dentry, struct inode *dir,
19019+ struct dentry *dentry);
7eafdf33 19020+int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
1facf9fc 19021+
4a4d8108
AM
19022+/* i_op_del.c */
19023+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
19024+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
19025+ struct dentry *h_parent, int isdir);
19026+int aufs_unlink(struct inode *dir, struct dentry *dentry);
19027+int aufs_rmdir(struct inode *dir, struct dentry *dentry);
1308ab2a 19028+
4a4d8108
AM
19029+/* i_op_ren.c */
19030+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
19031+int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
f2c43d5f
AM
19032+ struct inode *dir, struct dentry *dentry,
19033+ unsigned int flags);
1facf9fc 19034+
4a4d8108
AM
19035+/* iinfo.c */
19036+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
19037+void au_hiput(struct au_hinode *hinode);
19038+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
19039+ struct dentry *h_wh);
19040+unsigned int au_hi_flags(struct inode *inode, int isdir);
1308ab2a 19041+
4a4d8108
AM
19042+/* hinode flags */
19043+#define AuHi_XINO 1
19044+#define AuHi_HNOTIFY (1 << 1)
19045+#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
7f207e10
AM
19046+#define au_fset_hi(flags, name) \
19047+ do { (flags) |= AuHi_##name; } while (0)
19048+#define au_fclr_hi(flags, name) \
19049+ do { (flags) &= ~AuHi_##name; } while (0)
1facf9fc 19050+
4a4d8108
AM
19051+#ifndef CONFIG_AUFS_HNOTIFY
19052+#undef AuHi_HNOTIFY
19053+#define AuHi_HNOTIFY 0
19054+#endif
1facf9fc 19055+
4a4d8108
AM
19056+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
19057+ struct inode *h_inode, unsigned int flags);
1facf9fc 19058+
537831f9 19059+void au_update_iigen(struct inode *inode, int half);
4a4d8108 19060+void au_update_ibrange(struct inode *inode, int do_put_zero);
1facf9fc 19061+
4a4d8108 19062+void au_icntnr_init_once(void *_c);
5afbbe0d 19063+void au_hinode_init(struct au_hinode *hinode);
4a4d8108
AM
19064+int au_iinfo_init(struct inode *inode);
19065+void au_iinfo_fin(struct inode *inode);
e2f27e51 19066+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
1308ab2a 19067+
e49829fe 19068+#ifdef CONFIG_PROC_FS
4a4d8108 19069+/* plink.c */
e49829fe 19070+int au_plink_maint(struct super_block *sb, int flags);
7e9cd9fe 19071+struct au_sbinfo;
e49829fe
JR
19072+void au_plink_maint_leave(struct au_sbinfo *sbinfo);
19073+int au_plink_maint_enter(struct super_block *sb);
4a4d8108
AM
19074+#ifdef CONFIG_AUFS_DEBUG
19075+void au_plink_list(struct super_block *sb);
19076+#else
19077+AuStubVoid(au_plink_list, struct super_block *sb)
19078+#endif
19079+int au_plink_test(struct inode *inode);
19080+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
19081+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
19082+ struct dentry *h_dentry);
e49829fe
JR
19083+void au_plink_put(struct super_block *sb, int verbose);
19084+void au_plink_clean(struct super_block *sb, int verbose);
4a4d8108 19085+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
e49829fe
JR
19086+#else
19087+AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
19088+AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
19089+AuStubInt0(au_plink_maint_enter, struct super_block *sb);
19090+AuStubVoid(au_plink_list, struct super_block *sb);
19091+AuStubInt0(au_plink_test, struct inode *inode);
19092+AuStub(struct dentry *, au_plink_lkup, return NULL,
19093+ struct inode *inode, aufs_bindex_t bindex);
19094+AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
19095+ struct dentry *h_dentry);
19096+AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
19097+AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
19098+AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
19099+#endif /* CONFIG_PROC_FS */
1facf9fc 19100+
c1595e42
JR
19101+#ifdef CONFIG_AUFS_XATTR
19102+/* xattr.c */
7e9cd9fe
AM
19103+int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
19104+ unsigned int verbose);
c1595e42 19105+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
f2c43d5f 19106+void au_xattr_init(struct super_block *sb);
c1595e42
JR
19107+#else
19108+AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
7e9cd9fe 19109+ int ignore_flags, unsigned int verbose);
f2c43d5f 19110+AuStubVoid(au_xattr_init, struct super_block *sb);
c1595e42
JR
19111+#endif
19112+
19113+#ifdef CONFIG_FS_POSIX_ACL
19114+struct posix_acl *aufs_get_acl(struct inode *inode, int type);
19115+int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
19116+#endif
19117+
19118+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
19119+enum {
19120+ AU_XATTR_SET,
c1595e42
JR
19121+ AU_ACL_SET
19122+};
19123+
f2c43d5f 19124+struct au_sxattr {
c1595e42
JR
19125+ int type;
19126+ union {
19127+ struct {
19128+ const char *name;
19129+ const void *value;
19130+ size_t size;
19131+ int flags;
19132+ } set;
19133+ struct {
c1595e42
JR
19134+ struct posix_acl *acl;
19135+ int type;
19136+ } acl_set;
19137+ } u;
19138+};
f2c43d5f
AM
19139+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
19140+ struct au_sxattr *arg);
c1595e42
JR
19141+#endif
19142+
4a4d8108 19143+/* ---------------------------------------------------------------------- */
1308ab2a 19144+
4a4d8108
AM
19145+/* lock subclass for iinfo */
19146+enum {
19147+ AuLsc_II_CHILD, /* child first */
19148+ AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
19149+ AuLsc_II_CHILD3, /* copyup dirs */
19150+ AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
19151+ AuLsc_II_PARENT2,
19152+ AuLsc_II_PARENT3, /* copyup dirs */
19153+ AuLsc_II_NEW_CHILD
19154+};
1308ab2a 19155+
1facf9fc 19156+/*
4a4d8108
AM
19157+ * ii_read_lock_child, ii_write_lock_child,
19158+ * ii_read_lock_child2, ii_write_lock_child2,
19159+ * ii_read_lock_child3, ii_write_lock_child3,
19160+ * ii_read_lock_parent, ii_write_lock_parent,
19161+ * ii_read_lock_parent2, ii_write_lock_parent2,
19162+ * ii_read_lock_parent3, ii_write_lock_parent3,
19163+ * ii_read_lock_new_child, ii_write_lock_new_child,
1facf9fc 19164+ */
4a4d8108
AM
19165+#define AuReadLockFunc(name, lsc) \
19166+static inline void ii_read_lock_##name(struct inode *i) \
19167+{ \
19168+ au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19169+}
19170+
19171+#define AuWriteLockFunc(name, lsc) \
19172+static inline void ii_write_lock_##name(struct inode *i) \
19173+{ \
19174+ au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19175+}
19176+
19177+#define AuRWLockFuncs(name, lsc) \
19178+ AuReadLockFunc(name, lsc) \
19179+ AuWriteLockFunc(name, lsc)
19180+
19181+AuRWLockFuncs(child, CHILD);
19182+AuRWLockFuncs(child2, CHILD2);
19183+AuRWLockFuncs(child3, CHILD3);
19184+AuRWLockFuncs(parent, PARENT);
19185+AuRWLockFuncs(parent2, PARENT2);
19186+AuRWLockFuncs(parent3, PARENT3);
19187+AuRWLockFuncs(new_child, NEW_CHILD);
19188+
19189+#undef AuReadLockFunc
19190+#undef AuWriteLockFunc
19191+#undef AuRWLockFuncs
1facf9fc 19192+
8b6a4947
AM
19193+#define ii_read_unlock(i) au_rw_read_unlock(&au_ii(i)->ii_rwsem)
19194+#define ii_write_unlock(i) au_rw_write_unlock(&au_ii(i)->ii_rwsem)
19195+#define ii_downgrade_lock(i) au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
1facf9fc 19196+
4a4d8108
AM
19197+#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
19198+#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
19199+#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
1facf9fc 19200+
4a4d8108 19201+/* ---------------------------------------------------------------------- */
1308ab2a 19202+
027c5e7a
AM
19203+static inline void au_icntnr_init(struct au_icntnr *c)
19204+{
19205+#ifdef CONFIG_AUFS_DEBUG
19206+ c->vfs_inode.i_mode = 0;
19207+#endif
19208+}
19209+
cfc41e69 19210+static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
4a4d8108 19211+{
537831f9
AM
19212+ unsigned int gen;
19213+ struct au_iinfo *iinfo;
be52b249 19214+ struct au_iigen *iigen;
537831f9
AM
19215+
19216+ iinfo = au_ii(inode);
be52b249
AM
19217+ iigen = &iinfo->ii_generation;
19218+ spin_lock(&iigen->ig_spin);
cfc41e69
AM
19219+ if (igflags)
19220+ *igflags = iigen->ig_flags;
be52b249
AM
19221+ gen = iigen->ig_generation;
19222+ spin_unlock(&iigen->ig_spin);
537831f9
AM
19223+
19224+ return gen;
4a4d8108 19225+}
1308ab2a 19226+
4a4d8108
AM
19227+/* tiny test for inode number */
19228+/* tmpfs generation is too rough */
19229+static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
19230+{
19231+ struct au_iinfo *iinfo;
1308ab2a 19232+
4a4d8108
AM
19233+ iinfo = au_ii(inode);
19234+ AuRwMustAnyLock(&iinfo->ii_rwsem);
19235+ return !(iinfo->ii_hsb1 == h_inode->i_sb
19236+ && iinfo->ii_higen == h_inode->i_generation);
19237+}
1308ab2a 19238+
4a4d8108
AM
19239+static inline void au_iigen_dec(struct inode *inode)
19240+{
537831f9 19241+ struct au_iinfo *iinfo;
be52b249 19242+ struct au_iigen *iigen;
537831f9
AM
19243+
19244+ iinfo = au_ii(inode);
be52b249
AM
19245+ iigen = &iinfo->ii_generation;
19246+ spin_lock(&iigen->ig_spin);
19247+ iigen->ig_generation--;
19248+ spin_unlock(&iigen->ig_spin);
027c5e7a
AM
19249+}
19250+
19251+static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
19252+{
19253+ int err;
19254+
19255+ err = 0;
537831f9 19256+ if (unlikely(inode && au_iigen(inode, NULL) != sigen))
027c5e7a
AM
19257+ err = -EIO;
19258+
19259+ return err;
4a4d8108 19260+}
1308ab2a 19261+
4a4d8108 19262+/* ---------------------------------------------------------------------- */
1308ab2a 19263+
5afbbe0d
AM
19264+static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
19265+ aufs_bindex_t bindex)
19266+{
19267+ return iinfo->ii_hinode + bindex;
19268+}
19269+
19270+static inline int au_is_bad_inode(struct inode *inode)
19271+{
19272+ return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
19273+}
19274+
4a4d8108
AM
19275+static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
19276+ aufs_bindex_t bindex)
19277+{
19278+ IiMustAnyLock(inode);
5afbbe0d 19279+ return au_hinode(au_ii(inode), bindex)->hi_id;
4a4d8108 19280+}
1308ab2a 19281+
5afbbe0d 19282+static inline aufs_bindex_t au_ibtop(struct inode *inode)
4a4d8108
AM
19283+{
19284+ IiMustAnyLock(inode);
5afbbe0d 19285+ return au_ii(inode)->ii_btop;
4a4d8108 19286+}
1308ab2a 19287+
5afbbe0d 19288+static inline aufs_bindex_t au_ibbot(struct inode *inode)
4a4d8108
AM
19289+{
19290+ IiMustAnyLock(inode);
5afbbe0d 19291+ return au_ii(inode)->ii_bbot;
4a4d8108 19292+}
1308ab2a 19293+
4a4d8108
AM
19294+static inline struct au_vdir *au_ivdir(struct inode *inode)
19295+{
19296+ IiMustAnyLock(inode);
19297+ return au_ii(inode)->ii_vdir;
19298+}
1308ab2a 19299+
4a4d8108
AM
19300+static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
19301+{
19302+ IiMustAnyLock(inode);
5afbbe0d 19303+ return au_hinode(au_ii(inode), bindex)->hi_whdentry;
4a4d8108 19304+}
1308ab2a 19305+
5afbbe0d 19306+static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 19307+{
4a4d8108 19308+ IiMustWriteLock(inode);
5afbbe0d 19309+ au_ii(inode)->ii_btop = bindex;
4a4d8108 19310+}
1308ab2a 19311+
5afbbe0d 19312+static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
4a4d8108
AM
19313+{
19314+ IiMustWriteLock(inode);
5afbbe0d 19315+ au_ii(inode)->ii_bbot = bindex;
1308ab2a 19316+}
19317+
4a4d8108
AM
19318+static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
19319+{
19320+ IiMustWriteLock(inode);
19321+ au_ii(inode)->ii_vdir = vdir;
19322+}
1facf9fc 19323+
4a4d8108 19324+static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 19325+{
4a4d8108 19326+ IiMustAnyLock(inode);
5afbbe0d 19327+ return au_hinode(au_ii(inode), bindex);
4a4d8108 19328+}
dece6358 19329+
4a4d8108 19330+/* ---------------------------------------------------------------------- */
1facf9fc 19331+
4a4d8108
AM
19332+static inline struct dentry *au_pinned_parent(struct au_pin *pin)
19333+{
19334+ if (pin)
19335+ return pin->parent;
19336+ return NULL;
1facf9fc 19337+}
19338+
4a4d8108 19339+static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
1facf9fc 19340+{
4a4d8108
AM
19341+ if (pin && pin->hdir)
19342+ return pin->hdir->hi_inode;
19343+ return NULL;
1308ab2a 19344+}
1facf9fc 19345+
4a4d8108
AM
19346+static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
19347+{
19348+ if (pin)
19349+ return pin->hdir;
19350+ return NULL;
19351+}
1facf9fc 19352+
4a4d8108 19353+static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
1308ab2a 19354+{
4a4d8108
AM
19355+ if (pin)
19356+ pin->dentry = dentry;
19357+}
1308ab2a 19358+
4a4d8108
AM
19359+static inline void au_pin_set_parent_lflag(struct au_pin *pin,
19360+ unsigned char lflag)
19361+{
19362+ if (pin) {
7f207e10 19363+ if (lflag)
4a4d8108 19364+ au_fset_pin(pin->flags, DI_LOCKED);
7f207e10 19365+ else
4a4d8108 19366+ au_fclr_pin(pin->flags, DI_LOCKED);
1308ab2a 19367+ }
4a4d8108
AM
19368+}
19369+
7e9cd9fe 19370+#if 0 /* reserved */
4a4d8108
AM
19371+static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
19372+{
19373+ if (pin) {
19374+ dput(pin->parent);
19375+ pin->parent = dget(parent);
1facf9fc 19376+ }
4a4d8108 19377+}
7e9cd9fe 19378+#endif
1facf9fc 19379+
4a4d8108
AM
19380+/* ---------------------------------------------------------------------- */
19381+
027c5e7a 19382+struct au_branch;
4a4d8108
AM
19383+#ifdef CONFIG_AUFS_HNOTIFY
19384+struct au_hnotify_op {
19385+ void (*ctl)(struct au_hinode *hinode, int do_set);
027c5e7a 19386+ int (*alloc)(struct au_hinode *hinode);
7eafdf33
AM
19387+
19388+ /*
19389+ * if it returns true, the the caller should free hinode->hi_notify,
19390+ * otherwise ->free() frees it.
19391+ */
19392+ int (*free)(struct au_hinode *hinode,
19393+ struct au_hnotify *hn) __must_check;
4a4d8108
AM
19394+
19395+ void (*fin)(void);
19396+ int (*init)(void);
027c5e7a
AM
19397+
19398+ int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
19399+ void (*fin_br)(struct au_branch *br);
19400+ int (*init_br)(struct au_branch *br, int perm);
4a4d8108
AM
19401+};
19402+
19403+/* hnotify.c */
027c5e7a 19404+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
4a4d8108
AM
19405+void au_hn_free(struct au_hinode *hinode);
19406+void au_hn_ctl(struct au_hinode *hinode, int do_set);
19407+void au_hn_reset(struct inode *inode, unsigned int flags);
19408+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
19409+ struct qstr *h_child_qstr, struct inode *h_child_inode);
027c5e7a
AM
19410+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
19411+int au_hnotify_init_br(struct au_branch *br, int perm);
19412+void au_hnotify_fin_br(struct au_branch *br);
4a4d8108
AM
19413+int __init au_hnotify_init(void);
19414+void au_hnotify_fin(void);
19415+
7f207e10 19416+/* hfsnotify.c */
4a4d8108
AM
19417+extern const struct au_hnotify_op au_hnotify_op;
19418+
19419+static inline
19420+void au_hn_init(struct au_hinode *hinode)
19421+{
19422+ hinode->hi_notify = NULL;
1308ab2a 19423+}
19424+
53392da6
AM
19425+static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
19426+{
19427+ return hinode->hi_notify;
19428+}
19429+
4a4d8108 19430+#else
c1595e42
JR
19431+AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
19432+ struct au_hinode *hinode __maybe_unused,
19433+ struct inode *inode __maybe_unused)
19434+AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
4a4d8108
AM
19435+AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
19436+AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
19437+ int do_set __maybe_unused)
19438+AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
19439+ unsigned int flags __maybe_unused)
027c5e7a
AM
19440+AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
19441+ struct au_branch *br __maybe_unused,
19442+ int perm __maybe_unused)
19443+AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
19444+ int perm __maybe_unused)
19445+AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
4a4d8108
AM
19446+AuStubInt0(__init au_hnotify_init, void)
19447+AuStubVoid(au_hnotify_fin, void)
19448+AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
19449+#endif /* CONFIG_AUFS_HNOTIFY */
19450+
19451+static inline void au_hn_suspend(struct au_hinode *hdir)
19452+{
19453+ au_hn_ctl(hdir, /*do_set*/0);
1308ab2a 19454+}
19455+
4a4d8108 19456+static inline void au_hn_resume(struct au_hinode *hdir)
1308ab2a 19457+{
4a4d8108
AM
19458+ au_hn_ctl(hdir, /*do_set*/1);
19459+}
1308ab2a 19460+
5afbbe0d 19461+static inline void au_hn_inode_lock(struct au_hinode *hdir)
4a4d8108 19462+{
febd17d6 19463+ inode_lock(hdir->hi_inode);
4a4d8108
AM
19464+ au_hn_suspend(hdir);
19465+}
dece6358 19466+
5afbbe0d 19467+static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
4a4d8108
AM
19468+ unsigned int sc __maybe_unused)
19469+{
febd17d6 19470+ inode_lock_nested(hdir->hi_inode, sc);
4a4d8108 19471+ au_hn_suspend(hdir);
1facf9fc 19472+}
1facf9fc 19473+
8b6a4947
AM
19474+#if 0 /* unused */
19475+#include "vfsub.h"
3c1bdaff
AM
19476+static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
19477+ unsigned int sc)
19478+{
be118d29 19479+ inode_lock_shared_nested(hdir->hi_inode, sc);
3c1bdaff
AM
19480+ au_hn_suspend(hdir);
19481+}
8b6a4947 19482+#endif
3c1bdaff 19483+
5afbbe0d 19484+static inline void au_hn_inode_unlock(struct au_hinode *hdir)
4a4d8108
AM
19485+{
19486+ au_hn_resume(hdir);
febd17d6 19487+ inode_unlock(hdir->hi_inode);
4a4d8108
AM
19488+}
19489+
19490+#endif /* __KERNEL__ */
19491+#endif /* __AUFS_INODE_H__ */
7f207e10
AM
19492diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
19493--- /usr/share/empty/fs/aufs/ioctl.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 19494+++ linux/fs/aufs/ioctl.c 2019-01-28 14:36:12.222418047 +0100
062440b3 19495@@ -0,0 +1,220 @@
cd7a4cd9 19496+// SPDX-License-Identifier: GPL-2.0
4a4d8108 19497+/*
b00004a5 19498+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
19499+ *
19500+ * This program, aufs is free software; you can redistribute it and/or modify
19501+ * it under the terms of the GNU General Public License as published by
19502+ * the Free Software Foundation; either version 2 of the License, or
19503+ * (at your option) any later version.
19504+ *
19505+ * This program is distributed in the hope that it will be useful,
19506+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19507+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19508+ * GNU General Public License for more details.
19509+ *
19510+ * You should have received a copy of the GNU General Public License
523b37e3 19511+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
19512+ */
19513+
19514+/*
19515+ * ioctl
19516+ * plink-management and readdir in userspace.
19517+ * assist the pathconf(3) wrapper library.
c2b27bf2 19518+ * move-down
076b876e 19519+ * File-based Hierarchical Storage Management.
4a4d8108
AM
19520+ */
19521+
c2b27bf2
AM
19522+#include <linux/compat.h>
19523+#include <linux/file.h>
4a4d8108
AM
19524+#include "aufs.h"
19525+
1e00d052 19526+static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
4a4d8108
AM
19527+{
19528+ int err, fd;
5afbbe0d 19529+ aufs_bindex_t wbi, bindex, bbot;
4a4d8108
AM
19530+ struct file *h_file;
19531+ struct super_block *sb;
19532+ struct dentry *root;
1e00d052
AM
19533+ struct au_branch *br;
19534+ struct aufs_wbr_fd wbrfd = {
19535+ .oflags = au_dir_roflags,
19536+ .brid = -1
19537+ };
19538+ const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
19539+ | O_NOATIME | O_CLOEXEC;
4a4d8108 19540+
1e00d052
AM
19541+ AuDebugOn(wbrfd.oflags & ~valid);
19542+
19543+ if (arg) {
19544+ err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
19545+ if (unlikely(err)) {
19546+ err = -EFAULT;
19547+ goto out;
19548+ }
19549+
19550+ err = -EINVAL;
19551+ AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
19552+ wbrfd.oflags |= au_dir_roflags;
19553+ AuDbg("0%o\n", wbrfd.oflags);
19554+ if (unlikely(wbrfd.oflags & ~valid))
19555+ goto out;
19556+ }
19557+
2000de60 19558+ fd = get_unused_fd_flags(0);
1e00d052
AM
19559+ err = fd;
19560+ if (unlikely(fd < 0))
4a4d8108 19561+ goto out;
4a4d8108 19562+
1e00d052 19563+ h_file = ERR_PTR(-EINVAL);
4a4d8108 19564+ wbi = 0;
1e00d052 19565+ br = NULL;
4a4d8108
AM
19566+ sb = path->dentry->d_sb;
19567+ root = sb->s_root;
19568+ aufs_read_lock(root, AuLock_IR);
5afbbe0d 19569+ bbot = au_sbbot(sb);
1e00d052
AM
19570+ if (wbrfd.brid >= 0) {
19571+ wbi = au_br_index(sb, wbrfd.brid);
5afbbe0d 19572+ if (unlikely(wbi < 0 || wbi > bbot))
1e00d052
AM
19573+ goto out_unlock;
19574+ }
19575+
19576+ h_file = ERR_PTR(-ENOENT);
19577+ br = au_sbr(sb, wbi);
19578+ if (!au_br_writable(br->br_perm)) {
19579+ if (arg)
19580+ goto out_unlock;
19581+
19582+ bindex = wbi + 1;
19583+ wbi = -1;
5afbbe0d 19584+ for (; bindex <= bbot; bindex++) {
1e00d052
AM
19585+ br = au_sbr(sb, bindex);
19586+ if (au_br_writable(br->br_perm)) {
4a4d8108 19587+ wbi = bindex;
1e00d052 19588+ br = au_sbr(sb, wbi);
4a4d8108
AM
19589+ break;
19590+ }
19591+ }
4a4d8108
AM
19592+ }
19593+ AuDbg("wbi %d\n", wbi);
1e00d052 19594+ if (wbi >= 0)
392086de
AM
19595+ h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
19596+ /*force_wr*/0);
1e00d052
AM
19597+
19598+out_unlock:
4a4d8108
AM
19599+ aufs_read_unlock(root, AuLock_IR);
19600+ err = PTR_ERR(h_file);
19601+ if (IS_ERR(h_file))
19602+ goto out_fd;
19603+
acd2b654 19604+ au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
4a4d8108
AM
19605+ fd_install(fd, h_file);
19606+ err = fd;
19607+ goto out; /* success */
19608+
4f0767ce 19609+out_fd:
4a4d8108 19610+ put_unused_fd(fd);
4f0767ce 19611+out:
1e00d052 19612+ AuTraceErr(err);
4a4d8108
AM
19613+ return err;
19614+}
19615+
19616+/* ---------------------------------------------------------------------- */
19617+
19618+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
19619+{
19620+ long err;
c1595e42 19621+ struct dentry *dentry;
4a4d8108
AM
19622+
19623+ switch (cmd) {
4a4d8108
AM
19624+ case AUFS_CTL_RDU:
19625+ case AUFS_CTL_RDU_INO:
19626+ err = au_rdu_ioctl(file, cmd, arg);
19627+ break;
19628+
19629+ case AUFS_CTL_WBR_FD:
1e00d052 19630+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
19631+ break;
19632+
027c5e7a
AM
19633+ case AUFS_CTL_IBUSY:
19634+ err = au_ibusy_ioctl(file, arg);
19635+ break;
19636+
076b876e
AM
19637+ case AUFS_CTL_BRINFO:
19638+ err = au_brinfo_ioctl(file, arg);
19639+ break;
19640+
19641+ case AUFS_CTL_FHSM_FD:
2000de60 19642+ dentry = file->f_path.dentry;
c1595e42
JR
19643+ if (IS_ROOT(dentry))
19644+ err = au_fhsm_fd(dentry->d_sb, arg);
19645+ else
19646+ err = -ENOTTY;
076b876e
AM
19647+ break;
19648+
4a4d8108
AM
19649+ default:
19650+ /* do not call the lower */
19651+ AuDbg("0x%x\n", cmd);
19652+ err = -ENOTTY;
19653+ }
19654+
19655+ AuTraceErr(err);
19656+ return err;
19657+}
19658+
19659+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
19660+{
19661+ long err;
19662+
19663+ switch (cmd) {
c2b27bf2 19664+ case AUFS_CTL_MVDOWN:
2000de60 19665+ err = au_mvdown(file->f_path.dentry, (void __user *)arg);
c2b27bf2
AM
19666+ break;
19667+
4a4d8108 19668+ case AUFS_CTL_WBR_FD:
1e00d052 19669+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
19670+ break;
19671+
19672+ default:
19673+ /* do not call the lower */
19674+ AuDbg("0x%x\n", cmd);
19675+ err = -ENOTTY;
19676+ }
19677+
19678+ AuTraceErr(err);
19679+ return err;
19680+}
b752ccd1
AM
19681+
19682+#ifdef CONFIG_COMPAT
19683+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
19684+ unsigned long arg)
19685+{
19686+ long err;
19687+
19688+ switch (cmd) {
19689+ case AUFS_CTL_RDU:
19690+ case AUFS_CTL_RDU_INO:
19691+ err = au_rdu_compat_ioctl(file, cmd, arg);
19692+ break;
19693+
027c5e7a
AM
19694+ case AUFS_CTL_IBUSY:
19695+ err = au_ibusy_compat_ioctl(file, arg);
19696+ break;
19697+
076b876e
AM
19698+ case AUFS_CTL_BRINFO:
19699+ err = au_brinfo_compat_ioctl(file, arg);
19700+ break;
19701+
b752ccd1
AM
19702+ default:
19703+ err = aufs_ioctl_dir(file, cmd, arg);
19704+ }
19705+
19706+ AuTraceErr(err);
19707+ return err;
19708+}
19709+
b752ccd1
AM
19710+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
19711+ unsigned long arg)
19712+{
19713+ return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
19714+}
19715+#endif
7f207e10
AM
19716diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
19717--- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 19718+++ linux/fs/aufs/i_op_add.c 2019-01-28 14:36:12.222418047 +0100
acd2b654 19719@@ -0,0 +1,935 @@
cd7a4cd9 19720+// SPDX-License-Identifier: GPL-2.0
4a4d8108 19721+/*
b00004a5 19722+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
19723+ *
19724+ * This program, aufs is free software; you can redistribute it and/or modify
19725+ * it under the terms of the GNU General Public License as published by
19726+ * the Free Software Foundation; either version 2 of the License, or
19727+ * (at your option) any later version.
19728+ *
19729+ * This program is distributed in the hope that it will be useful,
19730+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19731+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19732+ * GNU General Public License for more details.
19733+ *
19734+ * You should have received a copy of the GNU General Public License
523b37e3 19735+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
19736+ */
19737+
19738+/*
19739+ * inode operations (add entry)
19740+ */
19741+
19742+#include "aufs.h"
19743+
19744+/*
19745+ * final procedure of adding a new entry, except link(2).
19746+ * remove whiteout, instantiate, copyup the parent dir's times and size
19747+ * and update version.
19748+ * if it failed, re-create the removed whiteout.
19749+ */
19750+static int epilog(struct inode *dir, aufs_bindex_t bindex,
19751+ struct dentry *wh_dentry, struct dentry *dentry)
19752+{
19753+ int err, rerr;
19754+ aufs_bindex_t bwh;
19755+ struct path h_path;
076b876e 19756+ struct super_block *sb;
4a4d8108
AM
19757+ struct inode *inode, *h_dir;
19758+ struct dentry *wh;
19759+
19760+ bwh = -1;
076b876e 19761+ sb = dir->i_sb;
4a4d8108 19762+ if (wh_dentry) {
5527c038 19763+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
4a4d8108
AM
19764+ IMustLock(h_dir);
19765+ AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
19766+ bwh = au_dbwh(dentry);
19767+ h_path.dentry = wh_dentry;
076b876e 19768+ h_path.mnt = au_sbr_mnt(sb, bindex);
4a4d8108
AM
19769+ err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
19770+ dentry);
19771+ if (unlikely(err))
19772+ goto out;
19773+ }
19774+
19775+ inode = au_new_inode(dentry, /*must_new*/1);
19776+ if (!IS_ERR(inode)) {
19777+ d_instantiate(dentry, inode);
5527c038 19778+ dir = d_inode(dentry->d_parent); /* dir inode is locked */
4a4d8108 19779+ IMustLock(dir);
b912730e 19780+ au_dir_ts(dir, bindex);
be118d29 19781+ inode_inc_iversion(dir);
076b876e 19782+ au_fhsm_wrote(sb, bindex, /*force*/0);
4a4d8108
AM
19783+ return 0; /* success */
19784+ }
19785+
19786+ err = PTR_ERR(inode);
19787+ if (!wh_dentry)
19788+ goto out;
19789+
19790+ /* revert */
19791+ /* dir inode is locked */
19792+ wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
19793+ rerr = PTR_ERR(wh);
19794+ if (IS_ERR(wh)) {
523b37e3
AM
19795+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
19796+ dentry, err, rerr);
4a4d8108
AM
19797+ err = -EIO;
19798+ } else
19799+ dput(wh);
19800+
4f0767ce 19801+out:
4a4d8108
AM
19802+ return err;
19803+}
19804+
027c5e7a
AM
19805+static int au_d_may_add(struct dentry *dentry)
19806+{
19807+ int err;
19808+
19809+ err = 0;
19810+ if (unlikely(d_unhashed(dentry)))
19811+ err = -ENOENT;
5527c038 19812+ if (unlikely(d_really_is_positive(dentry)))
027c5e7a
AM
19813+ err = -EEXIST;
19814+ return err;
19815+}
19816+
4a4d8108
AM
19817+/*
19818+ * simple tests for the adding inode operations.
19819+ * following the checks in vfs, plus the parent-child relationship.
19820+ */
19821+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19822+ struct dentry *h_parent, int isdir)
19823+{
19824+ int err;
19825+ umode_t h_mode;
19826+ struct dentry *h_dentry;
19827+ struct inode *h_inode;
19828+
19829+ err = -ENAMETOOLONG;
19830+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19831+ goto out;
19832+
19833+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 19834+ if (d_really_is_negative(dentry)) {
4a4d8108 19835+ err = -EEXIST;
5527c038 19836+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
19837+ goto out;
19838+ } else {
19839+ /* rename(2) case */
19840+ err = -EIO;
5527c038
JR
19841+ if (unlikely(d_is_negative(h_dentry)))
19842+ goto out;
19843+ h_inode = d_inode(h_dentry);
19844+ if (unlikely(!h_inode->i_nlink))
4a4d8108
AM
19845+ goto out;
19846+
19847+ h_mode = h_inode->i_mode;
19848+ if (!isdir) {
19849+ err = -EISDIR;
19850+ if (unlikely(S_ISDIR(h_mode)))
19851+ goto out;
19852+ } else if (unlikely(!S_ISDIR(h_mode))) {
19853+ err = -ENOTDIR;
19854+ goto out;
19855+ }
19856+ }
19857+
19858+ err = 0;
19859+ /* expected parent dir is locked */
19860+ if (unlikely(h_parent != h_dentry->d_parent))
19861+ err = -EIO;
19862+
4f0767ce 19863+out:
4a4d8108
AM
19864+ AuTraceErr(err);
19865+ return err;
19866+}
19867+
19868+/*
19869+ * initial procedure of adding a new entry.
19870+ * prepare writable branch and the parent dir, lock it,
19871+ * and lookup whiteout for the new entry.
19872+ */
19873+static struct dentry*
19874+lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
19875+ struct dentry *src_dentry, struct au_pin *pin,
19876+ struct au_wr_dir_args *wr_dir_args)
19877+{
19878+ struct dentry *wh_dentry, *h_parent;
19879+ struct super_block *sb;
19880+ struct au_branch *br;
19881+ int err;
19882+ unsigned int udba;
19883+ aufs_bindex_t bcpup;
19884+
523b37e3 19885+ AuDbg("%pd\n", dentry);
4a4d8108
AM
19886+
19887+ err = au_wr_dir(dentry, src_dentry, wr_dir_args);
19888+ bcpup = err;
19889+ wh_dentry = ERR_PTR(err);
19890+ if (unlikely(err < 0))
19891+ goto out;
19892+
19893+ sb = dentry->d_sb;
19894+ udba = au_opt_udba(sb);
19895+ err = au_pin(pin, dentry, bcpup, udba,
19896+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19897+ wh_dentry = ERR_PTR(err);
19898+ if (unlikely(err))
19899+ goto out;
19900+
19901+ h_parent = au_pinned_h_parent(pin);
19902+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 19903+ && au_dbtop(dentry) == bcpup)
4a4d8108
AM
19904+ err = au_may_add(dentry, bcpup, h_parent,
19905+ au_ftest_wrdir(wr_dir_args->flags, ISDIR));
19906+ else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19907+ err = -ENAMETOOLONG;
19908+ wh_dentry = ERR_PTR(err);
19909+ if (unlikely(err))
19910+ goto out_unpin;
19911+
19912+ br = au_sbr(sb, bcpup);
19913+ if (dt) {
19914+ struct path tmp = {
19915+ .dentry = h_parent,
86dc4139 19916+ .mnt = au_br_mnt(br)
4a4d8108
AM
19917+ };
19918+ au_dtime_store(dt, au_pinned_parent(pin), &tmp);
19919+ }
19920+
19921+ wh_dentry = NULL;
19922+ if (bcpup != au_dbwh(dentry))
19923+ goto out; /* success */
19924+
2000de60
JR
19925+ /*
19926+ * ENAMETOOLONG here means that if we allowed create such name, then it
19927+ * would not be able to removed in the future. So we don't allow such
19928+ * name here and we don't handle ENAMETOOLONG differently here.
19929+ */
4a4d8108
AM
19930+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
19931+
4f0767ce 19932+out_unpin:
4a4d8108
AM
19933+ if (IS_ERR(wh_dentry))
19934+ au_unpin(pin);
4f0767ce 19935+out:
4a4d8108
AM
19936+ return wh_dentry;
19937+}
19938+
19939+/* ---------------------------------------------------------------------- */
19940+
19941+enum { Mknod, Symlink, Creat };
19942+struct simple_arg {
19943+ int type;
19944+ union {
19945+ struct {
b912730e
AM
19946+ umode_t mode;
19947+ bool want_excl;
19948+ bool try_aopen;
19949+ struct vfsub_aopen_args *aopen;
4a4d8108
AM
19950+ } c;
19951+ struct {
19952+ const char *symname;
19953+ } s;
19954+ struct {
7eafdf33 19955+ umode_t mode;
4a4d8108
AM
19956+ dev_t dev;
19957+ } m;
19958+ } u;
19959+};
19960+
19961+static int add_simple(struct inode *dir, struct dentry *dentry,
19962+ struct simple_arg *arg)
19963+{
076b876e 19964+ int err, rerr;
5afbbe0d 19965+ aufs_bindex_t btop;
4a4d8108 19966+ unsigned char created;
b912730e
AM
19967+ const unsigned char try_aopen
19968+ = (arg->type == Creat && arg->u.c.try_aopen);
acd2b654 19969+ struct vfsub_aopen_args *aopen = arg->u.c.aopen;
4a4d8108
AM
19970+ struct dentry *wh_dentry, *parent;
19971+ struct inode *h_dir;
b912730e
AM
19972+ struct super_block *sb;
19973+ struct au_branch *br;
acd2b654 19974+ /* to reduce stack size */
c2b27bf2
AM
19975+ struct {
19976+ struct au_dtime dt;
19977+ struct au_pin pin;
19978+ struct path h_path;
19979+ struct au_wr_dir_args wr_dir_args;
19980+ } *a;
4a4d8108 19981+
523b37e3 19982+ AuDbg("%pd\n", dentry);
4a4d8108
AM
19983+ IMustLock(dir);
19984+
c2b27bf2
AM
19985+ err = -ENOMEM;
19986+ a = kmalloc(sizeof(*a), GFP_NOFS);
19987+ if (unlikely(!a))
19988+ goto out;
19989+ a->wr_dir_args.force_btgt = -1;
19990+ a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
19991+
4a4d8108 19992+ parent = dentry->d_parent; /* dir inode is locked */
b912730e
AM
19993+ if (!try_aopen) {
19994+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
19995+ if (unlikely(err))
19996+ goto out_free;
19997+ }
027c5e7a
AM
19998+ err = au_d_may_add(dentry);
19999+ if (unlikely(err))
20000+ goto out_unlock;
b912730e
AM
20001+ if (!try_aopen)
20002+ di_write_lock_parent(parent);
c2b27bf2
AM
20003+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20004+ &a->pin, &a->wr_dir_args);
4a4d8108
AM
20005+ err = PTR_ERR(wh_dentry);
20006+ if (IS_ERR(wh_dentry))
027c5e7a 20007+ goto out_parent;
4a4d8108 20008+
5afbbe0d 20009+ btop = au_dbtop(dentry);
b912730e 20010+ sb = dentry->d_sb;
5afbbe0d
AM
20011+ br = au_sbr(sb, btop);
20012+ a->h_path.dentry = au_h_dptr(dentry, btop);
b912730e 20013+ a->h_path.mnt = au_br_mnt(br);
c2b27bf2 20014+ h_dir = au_pinned_h_dir(&a->pin);
4a4d8108
AM
20015+ switch (arg->type) {
20016+ case Creat:
acd2b654 20017+ if (!try_aopen || !h_dir->i_op->atomic_open) {
b912730e
AM
20018+ err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
20019+ arg->u.c.want_excl);
acd2b654
AM
20020+ created = !err;
20021+ if (!err && try_aopen)
20022+ aopen->file->f_mode |= FMODE_CREATED;
20023+ } else {
20024+ aopen->br = br;
20025+ err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
20026+ AuDbg("err %d\n", err);
20027+ AuDbgFile(aopen->file);
20028+ created = err >= 0
20029+ && !!(aopen->file->f_mode & FMODE_CREATED);
20030+ }
4a4d8108
AM
20031+ break;
20032+ case Symlink:
c2b27bf2 20033+ err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
acd2b654 20034+ created = !err;
4a4d8108
AM
20035+ break;
20036+ case Mknod:
c2b27bf2
AM
20037+ err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
20038+ arg->u.m.dev);
acd2b654 20039+ created = !err;
4a4d8108
AM
20040+ break;
20041+ default:
20042+ BUG();
20043+ }
acd2b654
AM
20044+ if (unlikely(err < 0))
20045+ goto out_unpin;
20046+
20047+ err = epilog(dir, btop, wh_dentry, dentry);
4a4d8108 20048+ if (!err)
acd2b654 20049+ goto out_unpin; /* success */
4a4d8108
AM
20050+
20051+ /* revert */
acd2b654 20052+ if (created /* && d_is_positive(a->h_path.dentry) */) {
523b37e3
AM
20053+ /* no delegation since it is just created */
20054+ rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
20055+ /*force*/0);
4a4d8108 20056+ if (rerr) {
523b37e3
AM
20057+ AuIOErr("%pd revert failure(%d, %d)\n",
20058+ dentry, err, rerr);
4a4d8108
AM
20059+ err = -EIO;
20060+ }
c2b27bf2 20061+ au_dtime_revert(&a->dt);
4a4d8108 20062+ }
acd2b654
AM
20063+ if (try_aopen && h_dir->i_op->atomic_open
20064+ && (aopen->file->f_mode & FMODE_OPENED))
20065+ /* aopen->file is still opened */
20066+ au_lcnt_dec(&aopen->br->br_nfiles);
4a4d8108 20067+
acd2b654 20068+out_unpin:
c2b27bf2 20069+ au_unpin(&a->pin);
4a4d8108 20070+ dput(wh_dentry);
027c5e7a 20071+out_parent:
b912730e
AM
20072+ if (!try_aopen)
20073+ di_write_unlock(parent);
027c5e7a 20074+out_unlock:
4a4d8108 20075+ if (unlikely(err)) {
5afbbe0d 20076+ au_update_dbtop(dentry);
4a4d8108
AM
20077+ d_drop(dentry);
20078+ }
b912730e
AM
20079+ if (!try_aopen)
20080+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 20081+out_free:
e49925d1 20082+ au_kfree_rcu(a);
027c5e7a 20083+out:
4a4d8108
AM
20084+ return err;
20085+}
20086+
7eafdf33
AM
20087+int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
20088+ dev_t dev)
4a4d8108
AM
20089+{
20090+ struct simple_arg arg = {
20091+ .type = Mknod,
20092+ .u.m = {
20093+ .mode = mode,
20094+ .dev = dev
20095+ }
20096+ };
20097+ return add_simple(dir, dentry, &arg);
20098+}
20099+
20100+int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
20101+{
20102+ struct simple_arg arg = {
20103+ .type = Symlink,
20104+ .u.s.symname = symname
20105+ };
20106+ return add_simple(dir, dentry, &arg);
20107+}
20108+
7eafdf33 20109+int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
b4510431 20110+ bool want_excl)
4a4d8108
AM
20111+{
20112+ struct simple_arg arg = {
20113+ .type = Creat,
20114+ .u.c = {
b4510431
AM
20115+ .mode = mode,
20116+ .want_excl = want_excl
4a4d8108
AM
20117+ }
20118+ };
20119+ return add_simple(dir, dentry, &arg);
20120+}
20121+
b912730e
AM
20122+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20123+ struct vfsub_aopen_args *aopen_args)
20124+{
20125+ struct simple_arg arg = {
20126+ .type = Creat,
20127+ .u.c = {
20128+ .mode = aopen_args->create_mode,
20129+ .want_excl = aopen_args->open_flag & O_EXCL,
20130+ .try_aopen = true,
20131+ .aopen = aopen_args
20132+ }
20133+ };
20134+ return add_simple(dir, dentry, &arg);
20135+}
20136+
38d290e6
JR
20137+int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
20138+{
20139+ int err;
20140+ aufs_bindex_t bindex;
20141+ struct super_block *sb;
20142+ struct dentry *parent, *h_parent, *h_dentry;
20143+ struct inode *h_dir, *inode;
20144+ struct vfsmount *h_mnt;
20145+ struct au_wr_dir_args wr_dir_args = {
20146+ .force_btgt = -1,
20147+ .flags = AuWrDir_TMPFILE
20148+ };
20149+
20150+ /* copy-up may happen */
febd17d6 20151+ inode_lock(dir);
38d290e6
JR
20152+
20153+ sb = dir->i_sb;
20154+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20155+ if (unlikely(err))
20156+ goto out;
20157+
20158+ err = au_di_init(dentry);
20159+ if (unlikely(err))
20160+ goto out_si;
20161+
20162+ err = -EBUSY;
20163+ parent = d_find_any_alias(dir);
20164+ AuDebugOn(!parent);
20165+ di_write_lock_parent(parent);
5527c038 20166+ if (unlikely(d_inode(parent) != dir))
38d290e6
JR
20167+ goto out_parent;
20168+
20169+ err = au_digen_test(parent, au_sigen(sb));
20170+ if (unlikely(err))
20171+ goto out_parent;
20172+
5afbbe0d
AM
20173+ bindex = au_dbtop(parent);
20174+ au_set_dbtop(dentry, bindex);
20175+ au_set_dbbot(dentry, bindex);
38d290e6
JR
20176+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
20177+ bindex = err;
20178+ if (unlikely(err < 0))
20179+ goto out_parent;
20180+
20181+ err = -EOPNOTSUPP;
20182+ h_dir = au_h_iptr(dir, bindex);
20183+ if (unlikely(!h_dir->i_op->tmpfile))
20184+ goto out_parent;
20185+
20186+ h_mnt = au_sbr_mnt(sb, bindex);
20187+ err = vfsub_mnt_want_write(h_mnt);
20188+ if (unlikely(err))
20189+ goto out_parent;
20190+
20191+ h_parent = au_h_dptr(parent, bindex);
521ced18
JR
20192+ h_dentry = vfs_tmpfile(h_parent, mode, /*open_flag*/0);
20193+ if (IS_ERR(h_dentry)) {
20194+ err = PTR_ERR(h_dentry);
38d290e6 20195+ goto out_mnt;
521ced18 20196+ }
38d290e6 20197+
5afbbe0d
AM
20198+ au_set_dbtop(dentry, bindex);
20199+ au_set_dbbot(dentry, bindex);
38d290e6
JR
20200+ au_set_h_dptr(dentry, bindex, dget(h_dentry));
20201+ inode = au_new_inode(dentry, /*must_new*/1);
20202+ if (IS_ERR(inode)) {
20203+ err = PTR_ERR(inode);
20204+ au_set_h_dptr(dentry, bindex, NULL);
5afbbe0d
AM
20205+ au_set_dbtop(dentry, -1);
20206+ au_set_dbbot(dentry, -1);
38d290e6
JR
20207+ } else {
20208+ if (!inode->i_nlink)
20209+ set_nlink(inode, 1);
20210+ d_tmpfile(dentry, inode);
20211+ au_di(dentry)->di_tmpfile = 1;
20212+
20213+ /* update without i_mutex */
5afbbe0d 20214+ if (au_ibtop(dir) == au_dbtop(dentry))
38d290e6
JR
20215+ au_cpup_attr_timesizes(dir);
20216+ }
38d290e6 20217+ dput(h_dentry);
521ced18 20218+
38d290e6
JR
20219+out_mnt:
20220+ vfsub_mnt_drop_write(h_mnt);
20221+out_parent:
20222+ di_write_unlock(parent);
20223+ dput(parent);
20224+ di_write_unlock(dentry);
5afbbe0d 20225+ if (unlikely(err)) {
38d290e6
JR
20226+ au_di_fin(dentry);
20227+ dentry->d_fsdata = NULL;
20228+ }
20229+out_si:
20230+ si_read_unlock(sb);
20231+out:
febd17d6 20232+ inode_unlock(dir);
38d290e6
JR
20233+ return err;
20234+}
20235+
4a4d8108
AM
20236+/* ---------------------------------------------------------------------- */
20237+
20238+struct au_link_args {
20239+ aufs_bindex_t bdst, bsrc;
20240+ struct au_pin pin;
20241+ struct path h_path;
20242+ struct dentry *src_parent, *parent;
20243+};
20244+
20245+static int au_cpup_before_link(struct dentry *src_dentry,
20246+ struct au_link_args *a)
20247+{
20248+ int err;
20249+ struct dentry *h_src_dentry;
c2b27bf2
AM
20250+ struct au_cp_generic cpg = {
20251+ .dentry = src_dentry,
20252+ .bdst = a->bdst,
20253+ .bsrc = a->bsrc,
20254+ .len = -1,
20255+ .pin = &a->pin,
20256+ .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
20257+ };
4a4d8108
AM
20258+
20259+ di_read_lock_parent(a->src_parent, AuLock_IR);
20260+ err = au_test_and_cpup_dirs(src_dentry, a->bdst);
20261+ if (unlikely(err))
20262+ goto out;
20263+
20264+ h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
4a4d8108
AM
20265+ err = au_pin(&a->pin, src_dentry, a->bdst,
20266+ au_opt_udba(src_dentry->d_sb),
20267+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20268+ if (unlikely(err))
20269+ goto out;
367653fa 20270+
c2b27bf2 20271+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
20272+ au_unpin(&a->pin);
20273+
4f0767ce 20274+out:
4a4d8108
AM
20275+ di_read_unlock(a->src_parent, AuLock_IR);
20276+ return err;
20277+}
20278+
86dc4139
AM
20279+static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
20280+ struct au_link_args *a)
4a4d8108
AM
20281+{
20282+ int err;
20283+ unsigned char plink;
5afbbe0d 20284+ aufs_bindex_t bbot;
4a4d8108 20285+ struct dentry *h_src_dentry;
523b37e3 20286+ struct inode *h_inode, *inode, *delegated;
4a4d8108
AM
20287+ struct super_block *sb;
20288+ struct file *h_file;
20289+
20290+ plink = 0;
20291+ h_inode = NULL;
20292+ sb = src_dentry->d_sb;
5527c038 20293+ inode = d_inode(src_dentry);
5afbbe0d 20294+ if (au_ibtop(inode) <= a->bdst)
4a4d8108
AM
20295+ h_inode = au_h_iptr(inode, a->bdst);
20296+ if (!h_inode || !h_inode->i_nlink) {
20297+ /* copyup src_dentry as the name of dentry. */
5afbbe0d
AM
20298+ bbot = au_dbbot(dentry);
20299+ if (bbot < a->bsrc)
20300+ au_set_dbbot(dentry, a->bsrc);
86dc4139
AM
20301+ au_set_h_dptr(dentry, a->bsrc,
20302+ dget(au_h_dptr(src_dentry, a->bsrc)));
20303+ dget(a->h_path.dentry);
20304+ au_set_h_dptr(dentry, a->bdst, NULL);
c1595e42
JR
20305+ AuDbg("temporary d_inode...\n");
20306+ spin_lock(&dentry->d_lock);
5527c038 20307+ dentry->d_inode = d_inode(src_dentry); /* tmp */
c1595e42 20308+ spin_unlock(&dentry->d_lock);
392086de 20309+ h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
86dc4139 20310+ if (IS_ERR(h_file))
4a4d8108 20311+ err = PTR_ERR(h_file);
86dc4139 20312+ else {
c2b27bf2
AM
20313+ struct au_cp_generic cpg = {
20314+ .dentry = dentry,
20315+ .bdst = a->bdst,
20316+ .bsrc = -1,
20317+ .len = -1,
20318+ .pin = &a->pin,
20319+ .flags = AuCpup_KEEPLINO
20320+ };
20321+ err = au_sio_cpup_simple(&cpg);
86dc4139
AM
20322+ au_h_open_post(dentry, a->bsrc, h_file);
20323+ if (!err) {
20324+ dput(a->h_path.dentry);
20325+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20326+ } else
20327+ au_set_h_dptr(dentry, a->bdst,
20328+ a->h_path.dentry);
20329+ }
c1595e42 20330+ spin_lock(&dentry->d_lock);
86dc4139 20331+ dentry->d_inode = NULL; /* restore */
c1595e42
JR
20332+ spin_unlock(&dentry->d_lock);
20333+ AuDbg("temporary d_inode...done\n");
86dc4139 20334+ au_set_h_dptr(dentry, a->bsrc, NULL);
5afbbe0d 20335+ au_set_dbbot(dentry, bbot);
4a4d8108
AM
20336+ } else {
20337+ /* the inode of src_dentry already exists on a.bdst branch */
20338+ h_src_dentry = d_find_alias(h_inode);
20339+ if (!h_src_dentry && au_plink_test(inode)) {
20340+ plink = 1;
20341+ h_src_dentry = au_plink_lkup(inode, a->bdst);
20342+ err = PTR_ERR(h_src_dentry);
20343+ if (IS_ERR(h_src_dentry))
20344+ goto out;
20345+
5527c038 20346+ if (unlikely(d_is_negative(h_src_dentry))) {
4a4d8108
AM
20347+ dput(h_src_dentry);
20348+ h_src_dentry = NULL;
20349+ }
20350+
20351+ }
20352+ if (h_src_dentry) {
523b37e3 20353+ delegated = NULL;
4a4d8108 20354+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
20355+ &a->h_path, &delegated);
20356+ if (unlikely(err == -EWOULDBLOCK)) {
20357+ pr_warn("cannot retry for NFSv4 delegation"
20358+ " for an internal link\n");
20359+ iput(delegated);
20360+ }
4a4d8108
AM
20361+ dput(h_src_dentry);
20362+ } else {
20363+ AuIOErr("no dentry found for hi%lu on b%d\n",
20364+ h_inode->i_ino, a->bdst);
20365+ err = -EIO;
20366+ }
20367+ }
20368+
20369+ if (!err && !plink)
20370+ au_plink_append(inode, a->bdst, a->h_path.dentry);
20371+
20372+out:
2cbb1c4b 20373+ AuTraceErr(err);
4a4d8108
AM
20374+ return err;
20375+}
20376+
20377+int aufs_link(struct dentry *src_dentry, struct inode *dir,
20378+ struct dentry *dentry)
20379+{
20380+ int err, rerr;
20381+ struct au_dtime dt;
20382+ struct au_link_args *a;
20383+ struct dentry *wh_dentry, *h_src_dentry;
523b37e3 20384+ struct inode *inode, *delegated;
4a4d8108
AM
20385+ struct super_block *sb;
20386+ struct au_wr_dir_args wr_dir_args = {
20387+ /* .force_btgt = -1, */
20388+ .flags = AuWrDir_ADD_ENTRY
20389+ };
20390+
20391+ IMustLock(dir);
5527c038 20392+ inode = d_inode(src_dentry);
4a4d8108
AM
20393+ IMustLock(inode);
20394+
4a4d8108
AM
20395+ err = -ENOMEM;
20396+ a = kzalloc(sizeof(*a), GFP_NOFS);
20397+ if (unlikely(!a))
20398+ goto out;
20399+
20400+ a->parent = dentry->d_parent; /* dir inode is locked */
027c5e7a
AM
20401+ err = aufs_read_and_write_lock2(dentry, src_dentry,
20402+ AuLock_NOPLM | AuLock_GEN);
e49829fe
JR
20403+ if (unlikely(err))
20404+ goto out_kfree;
38d290e6 20405+ err = au_d_linkable(src_dentry);
027c5e7a
AM
20406+ if (unlikely(err))
20407+ goto out_unlock;
20408+ err = au_d_may_add(dentry);
20409+ if (unlikely(err))
20410+ goto out_unlock;
e49829fe 20411+
4a4d8108 20412+ a->src_parent = dget_parent(src_dentry);
5afbbe0d 20413+ wr_dir_args.force_btgt = au_ibtop(inode);
4a4d8108
AM
20414+
20415+ di_write_lock_parent(a->parent);
20416+ wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
20417+ wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
20418+ &wr_dir_args);
20419+ err = PTR_ERR(wh_dentry);
20420+ if (IS_ERR(wh_dentry))
027c5e7a 20421+ goto out_parent;
4a4d8108
AM
20422+
20423+ err = 0;
20424+ sb = dentry->d_sb;
5afbbe0d 20425+ a->bdst = au_dbtop(dentry);
4a4d8108
AM
20426+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20427+ a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
5afbbe0d 20428+ a->bsrc = au_ibtop(inode);
2cbb1c4b 20429+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
38d290e6
JR
20430+ if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
20431+ h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
2cbb1c4b 20432+ if (!h_src_dentry) {
5afbbe0d 20433+ a->bsrc = au_dbtop(src_dentry);
2cbb1c4b
JR
20434+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20435+ AuDebugOn(!h_src_dentry);
38d290e6
JR
20436+ } else if (IS_ERR(h_src_dentry)) {
20437+ err = PTR_ERR(h_src_dentry);
2cbb1c4b 20438+ goto out_parent;
38d290e6 20439+ }
2cbb1c4b 20440+
f2c43d5f
AM
20441+ /*
20442+ * aufs doesn't touch the credential so
acd2b654 20443+ * security_dentry_create_files_as() is unnecessary.
f2c43d5f 20444+ */
4a4d8108
AM
20445+ if (au_opt_test(au_mntflags(sb), PLINK)) {
20446+ if (a->bdst < a->bsrc
20447+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
86dc4139 20448+ err = au_cpup_or_link(src_dentry, dentry, a);
523b37e3
AM
20449+ else {
20450+ delegated = NULL;
4a4d8108 20451+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
20452+ &a->h_path, &delegated);
20453+ if (unlikely(err == -EWOULDBLOCK)) {
20454+ pr_warn("cannot retry for NFSv4 delegation"
20455+ " for an internal link\n");
20456+ iput(delegated);
20457+ }
20458+ }
2cbb1c4b 20459+ dput(h_src_dentry);
4a4d8108
AM
20460+ } else {
20461+ /*
20462+ * copyup src_dentry to the branch we process,
20463+ * and then link(2) to it.
20464+ */
2cbb1c4b 20465+ dput(h_src_dentry);
4a4d8108
AM
20466+ if (a->bdst < a->bsrc
20467+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
20468+ au_unpin(&a->pin);
20469+ di_write_unlock(a->parent);
20470+ err = au_cpup_before_link(src_dentry, a);
20471+ di_write_lock_parent(a->parent);
20472+ if (!err)
20473+ err = au_pin(&a->pin, dentry, a->bdst,
20474+ au_opt_udba(sb),
20475+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20476+ if (unlikely(err))
20477+ goto out_wh;
20478+ }
20479+ if (!err) {
20480+ h_src_dentry = au_h_dptr(src_dentry, a->bdst);
20481+ err = -ENOENT;
5527c038 20482+ if (h_src_dentry && d_is_positive(h_src_dentry)) {
523b37e3 20483+ delegated = NULL;
4a4d8108
AM
20484+ err = vfsub_link(h_src_dentry,
20485+ au_pinned_h_dir(&a->pin),
523b37e3
AM
20486+ &a->h_path, &delegated);
20487+ if (unlikely(err == -EWOULDBLOCK)) {
20488+ pr_warn("cannot retry"
20489+ " for NFSv4 delegation"
20490+ " for an internal link\n");
20491+ iput(delegated);
20492+ }
20493+ }
4a4d8108
AM
20494+ }
20495+ }
20496+ if (unlikely(err))
20497+ goto out_unpin;
20498+
20499+ if (wh_dentry) {
20500+ a->h_path.dentry = wh_dentry;
20501+ err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
20502+ dentry);
20503+ if (unlikely(err))
20504+ goto out_revert;
20505+ }
20506+
b912730e 20507+ au_dir_ts(dir, a->bdst);
be118d29 20508+ inode_inc_iversion(dir);
4a4d8108
AM
20509+ inc_nlink(inode);
20510+ inode->i_ctime = dir->i_ctime;
027c5e7a
AM
20511+ d_instantiate(dentry, au_igrab(inode));
20512+ if (d_unhashed(a->h_path.dentry))
4a4d8108
AM
20513+ /* some filesystem calls d_drop() */
20514+ d_drop(dentry);
076b876e
AM
20515+ /* some filesystems consume an inode even hardlink */
20516+ au_fhsm_wrote(sb, a->bdst, /*force*/0);
4a4d8108
AM
20517+ goto out_unpin; /* success */
20518+
4f0767ce 20519+out_revert:
523b37e3
AM
20520+ /* no delegation since it is just created */
20521+ rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
20522+ /*delegated*/NULL, /*force*/0);
027c5e7a 20523+ if (unlikely(rerr)) {
523b37e3 20524+ AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
027c5e7a
AM
20525+ err = -EIO;
20526+ }
4a4d8108 20527+ au_dtime_revert(&dt);
4f0767ce 20528+out_unpin:
4a4d8108 20529+ au_unpin(&a->pin);
4f0767ce 20530+out_wh:
4a4d8108 20531+ dput(wh_dentry);
027c5e7a
AM
20532+out_parent:
20533+ di_write_unlock(a->parent);
20534+ dput(a->src_parent);
4f0767ce 20535+out_unlock:
4a4d8108 20536+ if (unlikely(err)) {
5afbbe0d 20537+ au_update_dbtop(dentry);
4a4d8108
AM
20538+ d_drop(dentry);
20539+ }
4a4d8108 20540+ aufs_read_and_write_unlock2(dentry, src_dentry);
e49829fe 20541+out_kfree:
e49925d1 20542+ au_kfree_rcu(a);
4f0767ce 20543+out:
86dc4139 20544+ AuTraceErr(err);
4a4d8108
AM
20545+ return err;
20546+}
20547+
7eafdf33 20548+int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
4a4d8108
AM
20549+{
20550+ int err, rerr;
20551+ aufs_bindex_t bindex;
20552+ unsigned char diropq;
20553+ struct path h_path;
20554+ struct dentry *wh_dentry, *parent, *opq_dentry;
febd17d6 20555+ struct inode *h_inode;
4a4d8108
AM
20556+ struct super_block *sb;
20557+ struct {
20558+ struct au_pin pin;
20559+ struct au_dtime dt;
20560+ } *a; /* reduce the stack usage */
20561+ struct au_wr_dir_args wr_dir_args = {
20562+ .force_btgt = -1,
20563+ .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
20564+ };
20565+
20566+ IMustLock(dir);
20567+
20568+ err = -ENOMEM;
20569+ a = kmalloc(sizeof(*a), GFP_NOFS);
20570+ if (unlikely(!a))
20571+ goto out;
20572+
027c5e7a
AM
20573+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20574+ if (unlikely(err))
20575+ goto out_free;
20576+ err = au_d_may_add(dentry);
20577+ if (unlikely(err))
20578+ goto out_unlock;
20579+
4a4d8108
AM
20580+ parent = dentry->d_parent; /* dir inode is locked */
20581+ di_write_lock_parent(parent);
20582+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20583+ &a->pin, &wr_dir_args);
20584+ err = PTR_ERR(wh_dentry);
20585+ if (IS_ERR(wh_dentry))
027c5e7a 20586+ goto out_parent;
4a4d8108
AM
20587+
20588+ sb = dentry->d_sb;
5afbbe0d 20589+ bindex = au_dbtop(dentry);
4a4d8108
AM
20590+ h_path.dentry = au_h_dptr(dentry, bindex);
20591+ h_path.mnt = au_sbr_mnt(sb, bindex);
20592+ err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
20593+ if (unlikely(err))
027c5e7a 20594+ goto out_unpin;
4a4d8108
AM
20595+
20596+ /* make the dir opaque */
20597+ diropq = 0;
febd17d6 20598+ h_inode = d_inode(h_path.dentry);
4a4d8108
AM
20599+ if (wh_dentry
20600+ || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
febd17d6 20601+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 20602+ opq_dentry = au_diropq_create(dentry, bindex);
febd17d6 20603+ inode_unlock(h_inode);
4a4d8108
AM
20604+ err = PTR_ERR(opq_dentry);
20605+ if (IS_ERR(opq_dentry))
20606+ goto out_dir;
20607+ dput(opq_dentry);
20608+ diropq = 1;
20609+ }
20610+
20611+ err = epilog(dir, bindex, wh_dentry, dentry);
20612+ if (!err) {
20613+ inc_nlink(dir);
027c5e7a 20614+ goto out_unpin; /* success */
4a4d8108
AM
20615+ }
20616+
20617+ /* revert */
20618+ if (diropq) {
20619+ AuLabel(revert opq);
febd17d6 20620+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 20621+ rerr = au_diropq_remove(dentry, bindex);
febd17d6 20622+ inode_unlock(h_inode);
4a4d8108 20623+ if (rerr) {
523b37e3
AM
20624+ AuIOErr("%pd reverting diropq failed(%d, %d)\n",
20625+ dentry, err, rerr);
4a4d8108
AM
20626+ err = -EIO;
20627+ }
20628+ }
20629+
4f0767ce 20630+out_dir:
4a4d8108
AM
20631+ AuLabel(revert dir);
20632+ rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
20633+ if (rerr) {
523b37e3
AM
20634+ AuIOErr("%pd reverting dir failed(%d, %d)\n",
20635+ dentry, err, rerr);
4a4d8108
AM
20636+ err = -EIO;
20637+ }
4a4d8108 20638+ au_dtime_revert(&a->dt);
027c5e7a 20639+out_unpin:
4a4d8108
AM
20640+ au_unpin(&a->pin);
20641+ dput(wh_dentry);
027c5e7a
AM
20642+out_parent:
20643+ di_write_unlock(parent);
20644+out_unlock:
4a4d8108 20645+ if (unlikely(err)) {
5afbbe0d 20646+ au_update_dbtop(dentry);
4a4d8108
AM
20647+ d_drop(dentry);
20648+ }
4a4d8108 20649+ aufs_read_unlock(dentry, AuLock_DW);
027c5e7a 20650+out_free:
e49925d1 20651+ au_kfree_rcu(a);
4f0767ce 20652+out:
4a4d8108
AM
20653+ return err;
20654+}
7f207e10
AM
20655diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
20656--- /usr/share/empty/fs/aufs/i_op.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 20657+++ linux/fs/aufs/i_op.c 2019-01-28 14:36:12.222418047 +0100
acd2b654 20658@@ -0,0 +1,1506 @@
cd7a4cd9 20659+// SPDX-License-Identifier: GPL-2.0
4a4d8108 20660+/*
b00004a5 20661+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
20662+ *
20663+ * This program, aufs is free software; you can redistribute it and/or modify
20664+ * it under the terms of the GNU General Public License as published by
20665+ * the Free Software Foundation; either version 2 of the License, or
20666+ * (at your option) any later version.
20667+ *
20668+ * This program is distributed in the hope that it will be useful,
20669+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20670+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20671+ * GNU General Public License for more details.
20672+ *
20673+ * You should have received a copy of the GNU General Public License
523b37e3 20674+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 20675+ */
1facf9fc 20676+
1308ab2a 20677+/*
4a4d8108 20678+ * inode operations (except add/del/rename)
1308ab2a 20679+ */
4a4d8108
AM
20680+
20681+#include <linux/device_cgroup.h>
20682+#include <linux/fs_stack.h>
4a4d8108
AM
20683+#include <linux/namei.h>
20684+#include <linux/security.h>
4a4d8108
AM
20685+#include "aufs.h"
20686+
1e00d052 20687+static int h_permission(struct inode *h_inode, int mask,
79b8bda9 20688+ struct path *h_path, int brperm)
1facf9fc 20689+{
1308ab2a 20690+ int err;
4a4d8108 20691+ const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
1facf9fc 20692+
e2f27e51
AM
20693+ err = -EPERM;
20694+ if (write_mask && IS_IMMUTABLE(h_inode))
20695+ goto out;
20696+
4a4d8108 20697+ err = -EACCES;
e2f27e51
AM
20698+ if (((mask & MAY_EXEC)
20699+ && S_ISREG(h_inode->i_mode)
20700+ && (path_noexec(h_path)
cd7a4cd9 20701+ || !(h_inode->i_mode & 0111))))
4a4d8108
AM
20702+ goto out;
20703+
20704+ /*
20705+ * - skip the lower fs test in the case of write to ro branch.
20706+ * - nfs dir permission write check is optimized, but a policy for
20707+ * link/rename requires a real check.
2121bcd9 20708+ * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
b912730e 20709+ * in this case, generic_permission() returns -EOPNOTSUPP.
4a4d8108
AM
20710+ */
20711+ if ((write_mask && !au_br_writable(brperm))
20712+ || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
20713+ && write_mask && !(mask & MAY_READ))
20714+ || !h_inode->i_op->permission) {
20715+ /* AuLabel(generic_permission); */
062440b3 20716+ /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
1e00d052 20717+ err = generic_permission(h_inode, mask);
b912730e
AM
20718+ if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
20719+ err = h_inode->i_op->permission(h_inode, mask);
20720+ AuTraceErr(err);
1308ab2a 20721+ } else {
4a4d8108 20722+ /* AuLabel(h_inode->permission); */
1e00d052 20723+ err = h_inode->i_op->permission(h_inode, mask);
4a4d8108
AM
20724+ AuTraceErr(err);
20725+ }
1facf9fc 20726+
4a4d8108
AM
20727+ if (!err)
20728+ err = devcgroup_inode_permission(h_inode, mask);
7f207e10 20729+ if (!err)
4a4d8108 20730+ err = security_inode_permission(h_inode, mask);
4a4d8108
AM
20731+
20732+#if 0
20733+ if (!err) {
20734+ /* todo: do we need to call ima_path_check()? */
20735+ struct path h_path = {
20736+ .dentry =
20737+ .mnt = h_mnt
20738+ };
20739+ err = ima_path_check(&h_path,
20740+ mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
20741+ IMA_COUNT_LEAVE);
1308ab2a 20742+ }
4a4d8108 20743+#endif
dece6358 20744+
4f0767ce 20745+out:
1308ab2a 20746+ return err;
20747+}
dece6358 20748+
1e00d052 20749+static int aufs_permission(struct inode *inode, int mask)
1308ab2a 20750+{
20751+ int err;
5afbbe0d 20752+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
20753+ const unsigned char isdir = !!S_ISDIR(inode->i_mode),
20754+ write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20755+ struct inode *h_inode;
20756+ struct super_block *sb;
20757+ struct au_branch *br;
1facf9fc 20758+
027c5e7a 20759+ /* todo: support rcu-walk? */
1e00d052 20760+ if (mask & MAY_NOT_BLOCK)
027c5e7a
AM
20761+ return -ECHILD;
20762+
4a4d8108
AM
20763+ sb = inode->i_sb;
20764+ si_read_lock(sb, AuLock_FLUSH);
20765+ ii_read_lock_child(inode);
027c5e7a
AM
20766+#if 0
20767+ err = au_iigen_test(inode, au_sigen(sb));
20768+ if (unlikely(err))
20769+ goto out;
20770+#endif
dece6358 20771+
076b876e
AM
20772+ if (!isdir
20773+ || write_mask
20774+ || au_opt_test(au_mntflags(sb), DIRPERM1)) {
4a4d8108 20775+ err = au_busy_or_stale();
5afbbe0d 20776+ h_inode = au_h_iptr(inode, au_ibtop(inode));
4a4d8108
AM
20777+ if (unlikely(!h_inode
20778+ || (h_inode->i_mode & S_IFMT)
20779+ != (inode->i_mode & S_IFMT)))
20780+ goto out;
1facf9fc 20781+
4a4d8108 20782+ err = 0;
5afbbe0d 20783+ bindex = au_ibtop(inode);
4a4d8108 20784+ br = au_sbr(sb, bindex);
79b8bda9 20785+ err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
4a4d8108
AM
20786+ if (write_mask
20787+ && !err
20788+ && !special_file(h_inode->i_mode)) {
20789+ /* test whether the upper writable branch exists */
20790+ err = -EROFS;
20791+ for (; bindex >= 0; bindex--)
20792+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
20793+ err = 0;
20794+ break;
20795+ }
20796+ }
20797+ goto out;
20798+ }
dece6358 20799+
4a4d8108 20800+ /* non-write to dir */
1308ab2a 20801+ err = 0;
5afbbe0d
AM
20802+ bbot = au_ibbot(inode);
20803+ for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
20804+ h_inode = au_h_iptr(inode, bindex);
20805+ if (h_inode) {
20806+ err = au_busy_or_stale();
20807+ if (unlikely(!S_ISDIR(h_inode->i_mode)))
20808+ break;
20809+
20810+ br = au_sbr(sb, bindex);
79b8bda9 20811+ err = h_permission(h_inode, mask, &br->br_path,
4a4d8108
AM
20812+ br->br_perm);
20813+ }
20814+ }
1308ab2a 20815+
4f0767ce 20816+out:
4a4d8108
AM
20817+ ii_read_unlock(inode);
20818+ si_read_unlock(sb);
1308ab2a 20819+ return err;
20820+}
20821+
4a4d8108 20822+/* ---------------------------------------------------------------------- */
1facf9fc 20823+
4a4d8108 20824+static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
b4510431 20825+ unsigned int flags)
4a4d8108
AM
20826+{
20827+ struct dentry *ret, *parent;
b752ccd1 20828+ struct inode *inode;
4a4d8108 20829+ struct super_block *sb;
1716fcea 20830+ int err, npositive;
dece6358 20831+
4a4d8108 20832+ IMustLock(dir);
1308ab2a 20833+
537831f9
AM
20834+ /* todo: support rcu-walk? */
20835+ ret = ERR_PTR(-ECHILD);
20836+ if (flags & LOOKUP_RCU)
20837+ goto out;
20838+
20839+ ret = ERR_PTR(-ENAMETOOLONG);
20840+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20841+ goto out;
20842+
4a4d8108 20843+ sb = dir->i_sb;
7f207e10
AM
20844+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20845+ ret = ERR_PTR(err);
20846+ if (unlikely(err))
20847+ goto out;
20848+
4a4d8108
AM
20849+ err = au_di_init(dentry);
20850+ ret = ERR_PTR(err);
20851+ if (unlikely(err))
7f207e10 20852+ goto out_si;
1308ab2a 20853+
9dbd164d 20854+ inode = NULL;
027c5e7a 20855+ npositive = 0; /* suppress a warning */
4a4d8108
AM
20856+ parent = dentry->d_parent; /* dir inode is locked */
20857+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
20858+ err = au_alive_dir(parent);
20859+ if (!err)
20860+ err = au_digen_test(parent, au_sigen(sb));
20861+ if (!err) {
5afbbe0d
AM
20862+ /* regardless LOOKUP_CREATE, always ALLOW_NEG */
20863+ npositive = au_lkup_dentry(dentry, au_dbtop(parent),
20864+ AuLkup_ALLOW_NEG);
027c5e7a
AM
20865+ err = npositive;
20866+ }
4a4d8108 20867+ di_read_unlock(parent, AuLock_IR);
4a4d8108
AM
20868+ ret = ERR_PTR(err);
20869+ if (unlikely(err < 0))
20870+ goto out_unlock;
1308ab2a 20871+
4a4d8108 20872+ if (npositive) {
b752ccd1 20873+ inode = au_new_inode(dentry, /*must_new*/0);
c1595e42
JR
20874+ if (IS_ERR(inode)) {
20875+ ret = (void *)inode;
20876+ inode = NULL;
20877+ goto out_unlock;
20878+ }
9dbd164d 20879+ }
4a4d8108 20880+
c1595e42
JR
20881+ if (inode)
20882+ atomic_inc(&inode->i_count);
4a4d8108 20883+ ret = d_splice_alias(inode, dentry);
537831f9
AM
20884+#if 0
20885+ if (unlikely(d_need_lookup(dentry))) {
20886+ spin_lock(&dentry->d_lock);
20887+ dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
20888+ spin_unlock(&dentry->d_lock);
20889+ } else
20890+#endif
c1595e42 20891+ if (inode) {
2000de60 20892+ if (!IS_ERR(ret)) {
c1595e42 20893+ iput(inode);
2000de60
JR
20894+ if (ret && ret != dentry)
20895+ ii_write_unlock(inode);
20896+ } else {
c1595e42
JR
20897+ ii_write_unlock(inode);
20898+ iput(inode);
20899+ inode = NULL;
20900+ }
7f207e10 20901+ }
1facf9fc 20902+
4f0767ce 20903+out_unlock:
4a4d8108 20904+ di_write_unlock(dentry);
7f207e10 20905+out_si:
4a4d8108 20906+ si_read_unlock(sb);
7f207e10 20907+out:
4a4d8108
AM
20908+ return ret;
20909+}
1facf9fc 20910+
4a4d8108 20911+/* ---------------------------------------------------------------------- */
1facf9fc 20912+
acd2b654
AM
20913+/*
20914+ * very dirty and complicated aufs ->atomic_open().
20915+ * aufs_atomic_open()
20916+ * + au_aopen_or_create()
20917+ * + add_simple()
20918+ * + vfsub_atomic_open()
20919+ * + branch fs ->atomic_open()
20920+ * may call the actual 'open' for h_file
20921+ * + inc br_nfiles only if opened
20922+ * + au_aopen_no_open() or au_aopen_do_open()
20923+ *
20924+ * au_aopen_do_open()
20925+ * + finish_open()
20926+ * + au_do_aopen()
20927+ * + au_do_open() the body of all 'open'
20928+ * + au_do_open_nondir()
20929+ * set the passed h_file
20930+ *
20931+ * au_aopen_no_open()
20932+ * + finish_no_open()
20933+ */
20934+
b912730e 20935+struct aopen_node {
8b6a4947 20936+ struct hlist_bl_node hblist;
b912730e
AM
20937+ struct file *file, *h_file;
20938+};
20939+
20940+static int au_do_aopen(struct inode *inode, struct file *file)
20941+{
8b6a4947
AM
20942+ struct hlist_bl_head *aopen;
20943+ struct hlist_bl_node *pos;
b912730e
AM
20944+ struct aopen_node *node;
20945+ struct au_do_open_args args = {
8b6a4947
AM
20946+ .aopen = 1,
20947+ .open = au_do_open_nondir
b912730e
AM
20948+ };
20949+
20950+ aopen = &au_sbi(inode->i_sb)->si_aopen;
8b6a4947
AM
20951+ hlist_bl_lock(aopen);
20952+ hlist_bl_for_each_entry(node, pos, aopen, hblist)
b912730e
AM
20953+ if (node->file == file) {
20954+ args.h_file = node->h_file;
20955+ break;
20956+ }
8b6a4947 20957+ hlist_bl_unlock(aopen);
b912730e
AM
20958+ /* AuDebugOn(!args.h_file); */
20959+
20960+ return au_do_open(file, &args);
20961+}
20962+
acd2b654
AM
20963+static int au_aopen_do_open(struct file *file, struct dentry *dentry,
20964+ struct aopen_node *aopen_node)
20965+{
20966+ int err;
20967+ struct hlist_bl_head *aopen;
20968+
20969+ AuLabel(here);
20970+ aopen = &au_sbi(dentry->d_sb)->si_aopen;
20971+ au_hbl_add(&aopen_node->hblist, aopen);
20972+ err = finish_open(file, dentry, au_do_aopen);
20973+ au_hbl_del(&aopen_node->hblist, aopen);
20974+ /* AuDbgFile(file); */
20975+ AuDbg("%pd%s%s\n", dentry,
20976+ (file->f_mode & FMODE_CREATED) ? " created" : "",
20977+ (file->f_mode & FMODE_OPENED) ? " opened" : "");
20978+
20979+ AuTraceErr(err);
20980+ return err;
20981+}
20982+
20983+static int au_aopen_no_open(struct file *file, struct dentry *dentry)
20984+{
20985+ int err;
20986+
20987+ AuLabel(here);
20988+ dget(dentry);
20989+ err = finish_no_open(file, dentry);
20990+
20991+ AuTraceErr(err);
20992+ return err;
20993+}
20994+
b912730e
AM
20995+static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
20996+ struct file *file, unsigned int open_flag,
acd2b654 20997+ umode_t create_mode)
b912730e 20998+{
acd2b654 20999+ int err, did_open;
5afbbe0d 21000+ unsigned int lkup_flags;
acd2b654
AM
21001+ aufs_bindex_t bindex;
21002+ struct super_block *sb;
f0c0a007 21003+ struct dentry *parent, *d;
b912730e
AM
21004+ struct vfsub_aopen_args args = {
21005+ .open_flag = open_flag,
acd2b654 21006+ .create_mode = create_mode
b912730e
AM
21007+ };
21008+ struct aopen_node aopen_node = {
21009+ .file = file
21010+ };
21011+
21012+ IMustLock(dir);
5afbbe0d 21013+ AuDbg("open_flag 0%o\n", open_flag);
b912730e
AM
21014+ AuDbgDentry(dentry);
21015+
21016+ err = 0;
21017+ if (!au_di(dentry)) {
5afbbe0d
AM
21018+ lkup_flags = LOOKUP_OPEN;
21019+ if (open_flag & O_CREAT)
21020+ lkup_flags |= LOOKUP_CREATE;
21021+ d = aufs_lookup(dir, dentry, lkup_flags);
b912730e
AM
21022+ if (IS_ERR(d)) {
21023+ err = PTR_ERR(d);
5afbbe0d 21024+ AuTraceErr(err);
b912730e
AM
21025+ goto out;
21026+ } else if (d) {
21027+ /*
21028+ * obsoleted dentry found.
21029+ * another error will be returned later.
21030+ */
21031+ d_drop(d);
b912730e 21032+ AuDbgDentry(d);
5afbbe0d 21033+ dput(d);
b912730e
AM
21034+ }
21035+ AuDbgDentry(dentry);
21036+ }
21037+
21038+ if (d_is_positive(dentry)
21039+ || d_unhashed(dentry)
21040+ || d_unlinked(dentry)
acd2b654
AM
21041+ || !(open_flag & O_CREAT)) {
21042+ err = au_aopen_no_open(file, dentry);
21043+ goto out; /* success */
21044+ }
b912730e
AM
21045+
21046+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
21047+ if (unlikely(err))
21048+ goto out;
21049+
acd2b654 21050+ sb = dentry->d_sb;
b912730e
AM
21051+ parent = dentry->d_parent; /* dir is locked */
21052+ di_write_lock_parent(parent);
5afbbe0d 21053+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
acd2b654
AM
21054+ if (unlikely(err < 0))
21055+ goto out_parent;
b912730e
AM
21056+
21057+ AuDbgDentry(dentry);
acd2b654
AM
21058+ if (d_is_positive(dentry)) {
21059+ err = au_aopen_no_open(file, dentry);
21060+ goto out_parent; /* success */
21061+ }
b912730e 21062+
acd2b654 21063+ args.file = alloc_empty_file(file->f_flags, current_cred());
b912730e
AM
21064+ err = PTR_ERR(args.file);
21065+ if (IS_ERR(args.file))
acd2b654 21066+ goto out_parent;
b912730e 21067+
acd2b654 21068+ bindex = au_dbtop(dentry);
b912730e
AM
21069+ err = au_aopen_or_create(dir, dentry, &args);
21070+ AuTraceErr(err);
21071+ AuDbgFile(args.file);
acd2b654
AM
21072+ file->f_mode = args.file->f_mode & ~FMODE_OPENED;
21073+ did_open = !!(args.file->f_mode & FMODE_OPENED);
21074+ if (!did_open) {
21075+ fput(args.file);
21076+ args.file = NULL;
b912730e 21077+ }
8b6a4947
AM
21078+ di_write_unlock(parent);
21079+ di_write_unlock(dentry);
acd2b654
AM
21080+ if (unlikely(err < 0)) {
21081+ if (args.file)
21082+ fput(args.file);
21083+ goto out_sb;
b912730e 21084+ }
b912730e 21085+
acd2b654
AM
21086+ if (!did_open)
21087+ err = au_aopen_no_open(file, dentry);
8b6a4947 21088+ else {
acd2b654
AM
21089+ aopen_node.h_file = args.file;
21090+ err = au_aopen_do_open(file, dentry, &aopen_node);
8b6a4947 21091+ }
acd2b654
AM
21092+ if (unlikely(err < 0)) {
21093+ if (args.file)
21094+ fput(args.file);
21095+ if (did_open)
21096+ au_lcnt_dec(&args.br->br_nfiles);
b912730e 21097+ }
acd2b654
AM
21098+ goto out_sb; /* success */
21099+
21100+out_parent:
21101+ di_write_unlock(parent);
21102+ di_write_unlock(dentry);
21103+out_sb:
21104+ si_read_unlock(sb);
b912730e 21105+out:
b912730e 21106+ AuTraceErr(err);
acd2b654 21107+ AuDbgFile(file);
b912730e
AM
21108+ return err;
21109+}
21110+
21111+
21112+/* ---------------------------------------------------------------------- */
21113+
4a4d8108
AM
21114+static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
21115+ const unsigned char add_entry, aufs_bindex_t bcpup,
5afbbe0d 21116+ aufs_bindex_t btop)
4a4d8108
AM
21117+{
21118+ int err;
21119+ struct dentry *h_parent;
21120+ struct inode *h_dir;
1facf9fc 21121+
027c5e7a 21122+ if (add_entry)
5527c038 21123+ IMustLock(d_inode(parent));
027c5e7a 21124+ else
4a4d8108
AM
21125+ di_write_lock_parent(parent);
21126+
21127+ err = 0;
21128+ if (!au_h_dptr(parent, bcpup)) {
5afbbe0d 21129+ if (btop > bcpup)
c2b27bf2 21130+ err = au_cpup_dirs(dentry, bcpup);
5afbbe0d 21131+ else if (btop < bcpup)
4a4d8108
AM
21132+ err = au_cpdown_dirs(dentry, bcpup);
21133+ else
c2b27bf2 21134+ BUG();
4a4d8108 21135+ }
38d290e6 21136+ if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
4a4d8108 21137+ h_parent = au_h_dptr(parent, bcpup);
5527c038 21138+ h_dir = d_inode(h_parent);
be118d29 21139+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
7e9cd9fe 21140+ err = au_lkup_neg(dentry, bcpup, /*wh*/0);
4a4d8108 21141+ /* todo: no unlock here */
3c1bdaff 21142+ inode_unlock_shared(h_dir);
027c5e7a
AM
21143+
21144+ AuDbg("bcpup %d\n", bcpup);
21145+ if (!err) {
5527c038 21146+ if (d_really_is_negative(dentry))
5afbbe0d 21147+ au_set_h_dptr(dentry, btop, NULL);
4a4d8108
AM
21148+ au_update_dbrange(dentry, /*do_put_zero*/0);
21149+ }
1308ab2a 21150+ }
1facf9fc 21151+
4a4d8108
AM
21152+ if (!add_entry)
21153+ di_write_unlock(parent);
21154+ if (!err)
21155+ err = bcpup; /* success */
1308ab2a 21156+
027c5e7a 21157+ AuTraceErr(err);
4a4d8108
AM
21158+ return err;
21159+}
1facf9fc 21160+
4a4d8108
AM
21161+/*
21162+ * decide the branch and the parent dir where we will create a new entry.
21163+ * returns new bindex or an error.
21164+ * copyup the parent dir if needed.
21165+ */
21166+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
21167+ struct au_wr_dir_args *args)
21168+{
21169+ int err;
392086de 21170+ unsigned int flags;
5afbbe0d 21171+ aufs_bindex_t bcpup, btop, src_btop;
86dc4139
AM
21172+ const unsigned char add_entry
21173+ = au_ftest_wrdir(args->flags, ADD_ENTRY)
38d290e6 21174+ | au_ftest_wrdir(args->flags, TMPFILE);
4a4d8108
AM
21175+ struct super_block *sb;
21176+ struct dentry *parent;
21177+ struct au_sbinfo *sbinfo;
1facf9fc 21178+
4a4d8108
AM
21179+ sb = dentry->d_sb;
21180+ sbinfo = au_sbi(sb);
21181+ parent = dget_parent(dentry);
5afbbe0d
AM
21182+ btop = au_dbtop(dentry);
21183+ bcpup = btop;
4a4d8108
AM
21184+ if (args->force_btgt < 0) {
21185+ if (src_dentry) {
5afbbe0d
AM
21186+ src_btop = au_dbtop(src_dentry);
21187+ if (src_btop < btop)
21188+ bcpup = src_btop;
4a4d8108 21189+ } else if (add_entry) {
392086de
AM
21190+ flags = 0;
21191+ if (au_ftest_wrdir(args->flags, ISDIR))
21192+ au_fset_wbr(flags, DIR);
21193+ err = AuWbrCreate(sbinfo, dentry, flags);
4a4d8108
AM
21194+ bcpup = err;
21195+ }
1facf9fc 21196+
5527c038 21197+ if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
4a4d8108
AM
21198+ if (add_entry)
21199+ err = AuWbrCopyup(sbinfo, dentry);
21200+ else {
21201+ if (!IS_ROOT(dentry)) {
21202+ di_read_lock_parent(parent, !AuLock_IR);
21203+ err = AuWbrCopyup(sbinfo, dentry);
21204+ di_read_unlock(parent, !AuLock_IR);
21205+ } else
21206+ err = AuWbrCopyup(sbinfo, dentry);
21207+ }
21208+ bcpup = err;
21209+ if (unlikely(err < 0))
21210+ goto out;
21211+ }
21212+ } else {
21213+ bcpup = args->force_btgt;
5527c038 21214+ AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
1308ab2a 21215+ }
027c5e7a 21216+
5afbbe0d 21217+ AuDbg("btop %d, bcpup %d\n", btop, bcpup);
4a4d8108 21218+ err = bcpup;
5afbbe0d 21219+ if (bcpup == btop)
4a4d8108 21220+ goto out; /* success */
4a4d8108
AM
21221+
21222+ /* copyup the new parent into the branch we process */
5afbbe0d 21223+ err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
027c5e7a 21224+ if (err >= 0) {
5527c038 21225+ if (d_really_is_negative(dentry)) {
5afbbe0d
AM
21226+ au_set_h_dptr(dentry, btop, NULL);
21227+ au_set_dbtop(dentry, bcpup);
21228+ au_set_dbbot(dentry, bcpup);
027c5e7a 21229+ }
38d290e6
JR
21230+ AuDebugOn(add_entry
21231+ && !au_ftest_wrdir(args->flags, TMPFILE)
21232+ && !au_h_dptr(dentry, bcpup));
027c5e7a 21233+ }
86dc4139
AM
21234+
21235+out:
21236+ dput(parent);
21237+ return err;
21238+}
21239+
21240+/* ---------------------------------------------------------------------- */
21241+
21242+void au_pin_hdir_unlock(struct au_pin *p)
21243+{
21244+ if (p->hdir)
5afbbe0d 21245+ au_hn_inode_unlock(p->hdir);
86dc4139
AM
21246+}
21247+
c1595e42 21248+int au_pin_hdir_lock(struct au_pin *p)
86dc4139
AM
21249+{
21250+ int err;
21251+
21252+ err = 0;
21253+ if (!p->hdir)
21254+ goto out;
21255+
21256+ /* even if an error happens later, keep this lock */
5afbbe0d 21257+ au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
86dc4139
AM
21258+
21259+ err = -EBUSY;
5527c038 21260+ if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
86dc4139
AM
21261+ goto out;
21262+
21263+ err = 0;
21264+ if (p->h_dentry)
21265+ err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
21266+ p->h_parent, p->br);
21267+
21268+out:
21269+ return err;
21270+}
21271+
21272+int au_pin_hdir_relock(struct au_pin *p)
21273+{
21274+ int err, i;
21275+ struct inode *h_i;
21276+ struct dentry *h_d[] = {
21277+ p->h_dentry,
21278+ p->h_parent
21279+ };
21280+
21281+ err = au_pin_hdir_lock(p);
21282+ if (unlikely(err))
21283+ goto out;
21284+
21285+ for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
21286+ if (!h_d[i])
21287+ continue;
5527c038
JR
21288+ if (d_is_positive(h_d[i])) {
21289+ h_i = d_inode(h_d[i]);
86dc4139 21290+ err = !h_i->i_nlink;
5527c038 21291+ }
86dc4139
AM
21292+ }
21293+
21294+out:
21295+ return err;
21296+}
21297+
5afbbe0d 21298+static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
86dc4139 21299+{
5afbbe0d
AM
21300+#if !defined(CONFIG_RWSEM_GENERIC_SPINLOCK) && defined(CONFIG_RWSEM_SPIN_ON_OWNER)
21301+ p->hdir->hi_inode->i_rwsem.owner = task;
86dc4139
AM
21302+#endif
21303+}
21304+
21305+void au_pin_hdir_acquire_nest(struct au_pin *p)
21306+{
21307+ if (p->hdir) {
5afbbe0d 21308+ rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
86dc4139
AM
21309+ p->lsc_hi, 0, NULL, _RET_IP_);
21310+ au_pin_hdir_set_owner(p, current);
21311+ }
dece6358 21312+}
1facf9fc 21313+
86dc4139
AM
21314+void au_pin_hdir_release(struct au_pin *p)
21315+{
21316+ if (p->hdir) {
21317+ au_pin_hdir_set_owner(p, p->task);
5afbbe0d 21318+ rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, 1, _RET_IP_);
86dc4139
AM
21319+ }
21320+}
1308ab2a 21321+
4a4d8108 21322+struct dentry *au_pinned_h_parent(struct au_pin *pin)
1308ab2a 21323+{
4a4d8108
AM
21324+ if (pin && pin->parent)
21325+ return au_h_dptr(pin->parent, pin->bindex);
21326+ return NULL;
dece6358 21327+}
1facf9fc 21328+
4a4d8108 21329+void au_unpin(struct au_pin *p)
dece6358 21330+{
86dc4139
AM
21331+ if (p->hdir)
21332+ au_pin_hdir_unlock(p);
e49829fe 21333+ if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
b4510431 21334+ vfsub_mnt_drop_write(p->h_mnt);
4a4d8108
AM
21335+ if (!p->hdir)
21336+ return;
1facf9fc 21337+
4a4d8108
AM
21338+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21339+ di_read_unlock(p->parent, AuLock_IR);
21340+ iput(p->hdir->hi_inode);
21341+ dput(p->parent);
21342+ p->parent = NULL;
21343+ p->hdir = NULL;
21344+ p->h_mnt = NULL;
86dc4139 21345+ /* do not clear p->task */
4a4d8108 21346+}
1308ab2a 21347+
4a4d8108
AM
21348+int au_do_pin(struct au_pin *p)
21349+{
21350+ int err;
21351+ struct super_block *sb;
4a4d8108
AM
21352+ struct inode *h_dir;
21353+
21354+ err = 0;
21355+ sb = p->dentry->d_sb;
86dc4139 21356+ p->br = au_sbr(sb, p->bindex);
4a4d8108
AM
21357+ if (IS_ROOT(p->dentry)) {
21358+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 21359+ p->h_mnt = au_br_mnt(p->br);
b4510431 21360+ err = vfsub_mnt_want_write(p->h_mnt);
4a4d8108
AM
21361+ if (unlikely(err)) {
21362+ au_fclr_pin(p->flags, MNT_WRITE);
21363+ goto out_err;
21364+ }
21365+ }
dece6358 21366+ goto out;
1facf9fc 21367+ }
21368+
86dc4139 21369+ p->h_dentry = NULL;
5afbbe0d 21370+ if (p->bindex <= au_dbbot(p->dentry))
86dc4139 21371+ p->h_dentry = au_h_dptr(p->dentry, p->bindex);
dece6358 21372+
4a4d8108
AM
21373+ p->parent = dget_parent(p->dentry);
21374+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21375+ di_read_lock(p->parent, AuLock_IR, p->lsc_di);
dece6358 21376+
4a4d8108 21377+ h_dir = NULL;
86dc4139 21378+ p->h_parent = au_h_dptr(p->parent, p->bindex);
5527c038 21379+ p->hdir = au_hi(d_inode(p->parent), p->bindex);
4a4d8108
AM
21380+ if (p->hdir)
21381+ h_dir = p->hdir->hi_inode;
dece6358 21382+
b752ccd1
AM
21383+ /*
21384+ * udba case, or
21385+ * if DI_LOCKED is not set, then p->parent may be different
21386+ * and h_parent can be NULL.
21387+ */
86dc4139 21388+ if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
e49829fe 21389+ err = -EBUSY;
4a4d8108
AM
21390+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21391+ di_read_unlock(p->parent, AuLock_IR);
21392+ dput(p->parent);
21393+ p->parent = NULL;
21394+ goto out_err;
21395+ }
1308ab2a 21396+
4a4d8108 21397+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 21398+ p->h_mnt = au_br_mnt(p->br);
b4510431 21399+ err = vfsub_mnt_want_write(p->h_mnt);
dece6358 21400+ if (unlikely(err)) {
4a4d8108 21401+ au_fclr_pin(p->flags, MNT_WRITE);
86dc4139
AM
21402+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21403+ di_read_unlock(p->parent, AuLock_IR);
21404+ dput(p->parent);
21405+ p->parent = NULL;
21406+ goto out_err;
dece6358
AM
21407+ }
21408+ }
4a4d8108 21409+
86dc4139
AM
21410+ au_igrab(h_dir);
21411+ err = au_pin_hdir_lock(p);
21412+ if (!err)
21413+ goto out; /* success */
21414+
076b876e
AM
21415+ au_unpin(p);
21416+
4f0767ce 21417+out_err:
4a4d8108
AM
21418+ pr_err("err %d\n", err);
21419+ err = au_busy_or_stale();
4f0767ce 21420+out:
1facf9fc 21421+ return err;
21422+}
21423+
4a4d8108
AM
21424+void au_pin_init(struct au_pin *p, struct dentry *dentry,
21425+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
21426+ unsigned int udba, unsigned char flags)
21427+{
21428+ p->dentry = dentry;
21429+ p->udba = udba;
21430+ p->lsc_di = lsc_di;
21431+ p->lsc_hi = lsc_hi;
21432+ p->flags = flags;
21433+ p->bindex = bindex;
21434+
21435+ p->parent = NULL;
21436+ p->hdir = NULL;
21437+ p->h_mnt = NULL;
86dc4139
AM
21438+
21439+ p->h_dentry = NULL;
21440+ p->h_parent = NULL;
21441+ p->br = NULL;
21442+ p->task = current;
4a4d8108
AM
21443+}
21444+
21445+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
21446+ unsigned int udba, unsigned char flags)
21447+{
21448+ au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
21449+ udba, flags);
21450+ return au_do_pin(pin);
21451+}
21452+
dece6358
AM
21453+/* ---------------------------------------------------------------------- */
21454+
1308ab2a 21455+/*
4a4d8108
AM
21456+ * ->setattr() and ->getattr() are called in various cases.
21457+ * chmod, stat: dentry is revalidated.
21458+ * fchmod, fstat: file and dentry are not revalidated, additionally they may be
21459+ * unhashed.
21460+ * for ->setattr(), ia->ia_file is passed from ftruncate only.
1308ab2a 21461+ */
027c5e7a 21462+/* todo: consolidate with do_refresh() and simple_reval_dpath() */
c1595e42 21463+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
1facf9fc 21464+{
4a4d8108 21465+ int err;
4a4d8108 21466+ struct dentry *parent;
1facf9fc 21467+
1308ab2a 21468+ err = 0;
027c5e7a 21469+ if (au_digen_test(dentry, sigen)) {
4a4d8108
AM
21470+ parent = dget_parent(dentry);
21471+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 21472+ err = au_refresh_dentry(dentry, parent);
4a4d8108
AM
21473+ di_read_unlock(parent, AuLock_IR);
21474+ dput(parent);
dece6358 21475+ }
1facf9fc 21476+
4a4d8108 21477+ AuTraceErr(err);
1308ab2a 21478+ return err;
21479+}
dece6358 21480+
c1595e42
JR
21481+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
21482+ struct au_icpup_args *a)
1308ab2a 21483+{
21484+ int err;
4a4d8108 21485+ loff_t sz;
5afbbe0d 21486+ aufs_bindex_t btop, ibtop;
4a4d8108
AM
21487+ struct dentry *hi_wh, *parent;
21488+ struct inode *inode;
4a4d8108
AM
21489+ struct au_wr_dir_args wr_dir_args = {
21490+ .force_btgt = -1,
21491+ .flags = 0
21492+ };
21493+
2000de60 21494+ if (d_is_dir(dentry))
4a4d8108
AM
21495+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
21496+ /* plink or hi_wh() case */
5afbbe0d 21497+ btop = au_dbtop(dentry);
5527c038 21498+ inode = d_inode(dentry);
5afbbe0d
AM
21499+ ibtop = au_ibtop(inode);
21500+ if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
21501+ wr_dir_args.force_btgt = ibtop;
4a4d8108
AM
21502+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21503+ if (unlikely(err < 0))
21504+ goto out;
21505+ a->btgt = err;
5afbbe0d 21506+ if (err != btop)
4a4d8108
AM
21507+ au_fset_icpup(a->flags, DID_CPUP);
21508+
21509+ err = 0;
21510+ a->pin_flags = AuPin_MNT_WRITE;
21511+ parent = NULL;
21512+ if (!IS_ROOT(dentry)) {
21513+ au_fset_pin(a->pin_flags, DI_LOCKED);
21514+ parent = dget_parent(dentry);
21515+ di_write_lock_parent(parent);
21516+ }
21517+
21518+ err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
21519+ if (unlikely(err))
21520+ goto out_parent;
21521+
4a4d8108 21522+ sz = -1;
5afbbe0d 21523+ a->h_path.dentry = au_h_dptr(dentry, btop);
5527c038 21524+ a->h_inode = d_inode(a->h_path.dentry);
c1595e42 21525+ if (ia && (ia->ia_valid & ATTR_SIZE)) {
be118d29 21526+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
c1595e42
JR
21527+ if (ia->ia_size < i_size_read(a->h_inode))
21528+ sz = ia->ia_size;
3c1bdaff 21529+ inode_unlock_shared(a->h_inode);
c1595e42 21530+ }
4a4d8108 21531+
4a4d8108 21532+ hi_wh = NULL;
027c5e7a 21533+ if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
4a4d8108
AM
21534+ hi_wh = au_hi_wh(inode, a->btgt);
21535+ if (!hi_wh) {
c2b27bf2
AM
21536+ struct au_cp_generic cpg = {
21537+ .dentry = dentry,
21538+ .bdst = a->btgt,
21539+ .bsrc = -1,
21540+ .len = sz,
21541+ .pin = &a->pin
21542+ };
21543+ err = au_sio_cpup_wh(&cpg, /*file*/NULL);
4a4d8108
AM
21544+ if (unlikely(err))
21545+ goto out_unlock;
21546+ hi_wh = au_hi_wh(inode, a->btgt);
21547+ /* todo: revalidate hi_wh? */
21548+ }
21549+ }
21550+
21551+ if (parent) {
21552+ au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
21553+ di_downgrade_lock(parent, AuLock_IR);
21554+ dput(parent);
21555+ parent = NULL;
21556+ }
21557+ if (!au_ftest_icpup(a->flags, DID_CPUP))
21558+ goto out; /* success */
21559+
21560+ if (!d_unhashed(dentry)) {
c2b27bf2
AM
21561+ struct au_cp_generic cpg = {
21562+ .dentry = dentry,
21563+ .bdst = a->btgt,
5afbbe0d 21564+ .bsrc = btop,
c2b27bf2
AM
21565+ .len = sz,
21566+ .pin = &a->pin,
21567+ .flags = AuCpup_DTIME | AuCpup_HOPEN
21568+ };
21569+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
21570+ if (!err)
21571+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21572+ } else if (!hi_wh)
21573+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21574+ else
21575+ a->h_path.dentry = hi_wh; /* do not dget here */
1308ab2a 21576+
4f0767ce 21577+out_unlock:
5527c038 21578+ a->h_inode = d_inode(a->h_path.dentry);
86dc4139 21579+ if (!err)
dece6358 21580+ goto out; /* success */
4a4d8108 21581+ au_unpin(&a->pin);
4f0767ce 21582+out_parent:
4a4d8108
AM
21583+ if (parent) {
21584+ di_write_unlock(parent);
21585+ dput(parent);
21586+ }
4f0767ce 21587+out:
86dc4139 21588+ if (!err)
febd17d6 21589+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
1facf9fc 21590+ return err;
21591+}
21592+
4a4d8108 21593+static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
1facf9fc 21594+{
4a4d8108 21595+ int err;
523b37e3 21596+ struct inode *inode, *delegated;
4a4d8108
AM
21597+ struct super_block *sb;
21598+ struct file *file;
21599+ struct au_icpup_args *a;
1facf9fc 21600+
5527c038 21601+ inode = d_inode(dentry);
4a4d8108 21602+ IMustLock(inode);
dece6358 21603+
f2c43d5f
AM
21604+ err = setattr_prepare(dentry, ia);
21605+ if (unlikely(err))
21606+ goto out;
21607+
4a4d8108
AM
21608+ err = -ENOMEM;
21609+ a = kzalloc(sizeof(*a), GFP_NOFS);
21610+ if (unlikely(!a))
21611+ goto out;
1facf9fc 21612+
4a4d8108
AM
21613+ if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
21614+ ia->ia_valid &= ~ATTR_MODE;
dece6358 21615+
4a4d8108
AM
21616+ file = NULL;
21617+ sb = dentry->d_sb;
e49829fe
JR
21618+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21619+ if (unlikely(err))
21620+ goto out_kfree;
21621+
4a4d8108
AM
21622+ if (ia->ia_valid & ATTR_FILE) {
21623+ /* currently ftruncate(2) only */
7e9cd9fe 21624+ AuDebugOn(!d_is_reg(dentry));
4a4d8108 21625+ file = ia->ia_file;
521ced18
JR
21626+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
21627+ /*fi_lsc*/0);
4a4d8108
AM
21628+ if (unlikely(err))
21629+ goto out_si;
21630+ ia->ia_file = au_hf_top(file);
21631+ a->udba = AuOpt_UDBA_NONE;
21632+ } else {
21633+ /* fchmod() doesn't pass ia_file */
21634+ a->udba = au_opt_udba(sb);
027c5e7a
AM
21635+ di_write_lock_child(dentry);
21636+ /* no d_unlinked(), to set UDBA_NONE for root */
4a4d8108
AM
21637+ if (d_unhashed(dentry))
21638+ a->udba = AuOpt_UDBA_NONE;
4a4d8108
AM
21639+ if (a->udba != AuOpt_UDBA_NONE) {
21640+ AuDebugOn(IS_ROOT(dentry));
21641+ err = au_reval_for_attr(dentry, au_sigen(sb));
21642+ if (unlikely(err))
21643+ goto out_dentry;
21644+ }
dece6358 21645+ }
dece6358 21646+
4a4d8108
AM
21647+ err = au_pin_and_icpup(dentry, ia, a);
21648+ if (unlikely(err < 0))
21649+ goto out_dentry;
21650+ if (au_ftest_icpup(a->flags, DID_CPUP)) {
21651+ ia->ia_file = NULL;
21652+ ia->ia_valid &= ~ATTR_FILE;
1308ab2a 21653+ }
dece6358 21654+
4a4d8108
AM
21655+ a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
21656+ if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
21657+ == (ATTR_MODE | ATTR_CTIME)) {
7eafdf33 21658+ err = security_path_chmod(&a->h_path, ia->ia_mode);
4a4d8108
AM
21659+ if (unlikely(err))
21660+ goto out_unlock;
21661+ } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
21662+ && (ia->ia_valid & ATTR_CTIME)) {
86dc4139 21663+ err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
4a4d8108
AM
21664+ if (unlikely(err))
21665+ goto out_unlock;
21666+ }
dece6358 21667+
4a4d8108
AM
21668+ if (ia->ia_valid & ATTR_SIZE) {
21669+ struct file *f;
1308ab2a 21670+
953406b4 21671+ if (ia->ia_size < i_size_read(inode))
4a4d8108 21672+ /* unmap only */
953406b4 21673+ truncate_setsize(inode, ia->ia_size);
1308ab2a 21674+
4a4d8108
AM
21675+ f = NULL;
21676+ if (ia->ia_valid & ATTR_FILE)
21677+ f = ia->ia_file;
febd17d6 21678+ inode_unlock(a->h_inode);
4a4d8108 21679+ err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
febd17d6 21680+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
523b37e3
AM
21681+ } else {
21682+ delegated = NULL;
21683+ while (1) {
21684+ err = vfsub_notify_change(&a->h_path, ia, &delegated);
21685+ if (delegated) {
21686+ err = break_deleg_wait(&delegated);
21687+ if (!err)
21688+ continue;
21689+ }
21690+ break;
21691+ }
21692+ }
8cdd5066
JR
21693+ /*
21694+ * regardless aufs 'acl' option setting.
21695+ * why don't all acl-aware fs call this func from their ->setattr()?
21696+ */
21697+ if (!err && (ia->ia_valid & ATTR_MODE))
21698+ err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
4a4d8108
AM
21699+ if (!err)
21700+ au_cpup_attr_changeable(inode);
1308ab2a 21701+
4f0767ce 21702+out_unlock:
febd17d6 21703+ inode_unlock(a->h_inode);
4a4d8108 21704+ au_unpin(&a->pin);
027c5e7a 21705+ if (unlikely(err))
5afbbe0d 21706+ au_update_dbtop(dentry);
4f0767ce 21707+out_dentry:
4a4d8108
AM
21708+ di_write_unlock(dentry);
21709+ if (file) {
21710+ fi_write_unlock(file);
21711+ ia->ia_file = file;
21712+ ia->ia_valid |= ATTR_FILE;
21713+ }
4f0767ce 21714+out_si:
4a4d8108 21715+ si_read_unlock(sb);
e49829fe 21716+out_kfree:
e49925d1 21717+ au_kfree_rcu(a);
4f0767ce 21718+out:
4a4d8108
AM
21719+ AuTraceErr(err);
21720+ return err;
1facf9fc 21721+}
21722+
c1595e42
JR
21723+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
21724+static int au_h_path_to_set_attr(struct dentry *dentry,
21725+ struct au_icpup_args *a, struct path *h_path)
21726+{
21727+ int err;
21728+ struct super_block *sb;
21729+
21730+ sb = dentry->d_sb;
21731+ a->udba = au_opt_udba(sb);
21732+ /* no d_unlinked(), to set UDBA_NONE for root */
21733+ if (d_unhashed(dentry))
21734+ a->udba = AuOpt_UDBA_NONE;
21735+ if (a->udba != AuOpt_UDBA_NONE) {
21736+ AuDebugOn(IS_ROOT(dentry));
21737+ err = au_reval_for_attr(dentry, au_sigen(sb));
21738+ if (unlikely(err))
21739+ goto out;
21740+ }
21741+ err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
21742+ if (unlikely(err < 0))
21743+ goto out;
21744+
21745+ h_path->dentry = a->h_path.dentry;
21746+ h_path->mnt = au_sbr_mnt(sb, a->btgt);
21747+
21748+out:
21749+ return err;
21750+}
21751+
f2c43d5f
AM
21752+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
21753+ struct au_sxattr *arg)
c1595e42
JR
21754+{
21755+ int err;
21756+ struct path h_path;
21757+ struct super_block *sb;
21758+ struct au_icpup_args *a;
5afbbe0d 21759+ struct inode *h_inode;
c1595e42 21760+
c1595e42
JR
21761+ IMustLock(inode);
21762+
21763+ err = -ENOMEM;
21764+ a = kzalloc(sizeof(*a), GFP_NOFS);
21765+ if (unlikely(!a))
21766+ goto out;
21767+
21768+ sb = dentry->d_sb;
21769+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21770+ if (unlikely(err))
21771+ goto out_kfree;
21772+
21773+ h_path.dentry = NULL; /* silence gcc */
21774+ di_write_lock_child(dentry);
21775+ err = au_h_path_to_set_attr(dentry, a, &h_path);
21776+ if (unlikely(err))
21777+ goto out_di;
21778+
febd17d6 21779+ inode_unlock(a->h_inode);
c1595e42
JR
21780+ switch (arg->type) {
21781+ case AU_XATTR_SET:
5afbbe0d 21782+ AuDebugOn(d_is_negative(h_path.dentry));
c1595e42
JR
21783+ err = vfsub_setxattr(h_path.dentry,
21784+ arg->u.set.name, arg->u.set.value,
21785+ arg->u.set.size, arg->u.set.flags);
21786+ break;
c1595e42
JR
21787+ case AU_ACL_SET:
21788+ err = -EOPNOTSUPP;
5527c038 21789+ h_inode = d_inode(h_path.dentry);
c1595e42 21790+ if (h_inode->i_op->set_acl)
f2c43d5f 21791+ /* this will call posix_acl_update_mode */
c1595e42
JR
21792+ err = h_inode->i_op->set_acl(h_inode,
21793+ arg->u.acl_set.acl,
21794+ arg->u.acl_set.type);
21795+ break;
21796+ }
21797+ if (!err)
21798+ au_cpup_attr_timesizes(inode);
21799+
21800+ au_unpin(&a->pin);
21801+ if (unlikely(err))
5afbbe0d 21802+ au_update_dbtop(dentry);
c1595e42
JR
21803+
21804+out_di:
21805+ di_write_unlock(dentry);
21806+ si_read_unlock(sb);
21807+out_kfree:
e49925d1 21808+ au_kfree_rcu(a);
c1595e42
JR
21809+out:
21810+ AuTraceErr(err);
21811+ return err;
21812+}
21813+#endif
21814+
4a4d8108
AM
21815+static void au_refresh_iattr(struct inode *inode, struct kstat *st,
21816+ unsigned int nlink)
1facf9fc 21817+{
9dbd164d
AM
21818+ unsigned int n;
21819+
4a4d8108 21820+ inode->i_mode = st->mode;
86dc4139
AM
21821+ /* don't i_[ug]id_write() here */
21822+ inode->i_uid = st->uid;
21823+ inode->i_gid = st->gid;
4a4d8108
AM
21824+ inode->i_atime = st->atime;
21825+ inode->i_mtime = st->mtime;
21826+ inode->i_ctime = st->ctime;
1facf9fc 21827+
4a4d8108
AM
21828+ au_cpup_attr_nlink(inode, /*force*/0);
21829+ if (S_ISDIR(inode->i_mode)) {
9dbd164d
AM
21830+ n = inode->i_nlink;
21831+ n -= nlink;
21832+ n += st->nlink;
f6b6e03d 21833+ smp_mb(); /* for i_nlink */
7eafdf33 21834+ /* 0 can happen */
92d182d2 21835+ set_nlink(inode, n);
4a4d8108 21836+ }
1facf9fc 21837+
4a4d8108
AM
21838+ spin_lock(&inode->i_lock);
21839+ inode->i_blocks = st->blocks;
21840+ i_size_write(inode, st->size);
21841+ spin_unlock(&inode->i_lock);
1facf9fc 21842+}
21843+
c1595e42 21844+/*
f2c43d5f 21845+ * common routine for aufs_getattr() and au_getxattr().
c1595e42
JR
21846+ * returns zero or negative (an error).
21847+ * @dentry will be read-locked in success.
21848+ */
a2654f78
AM
21849+int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
21850+ int locked)
1facf9fc 21851+{
4a4d8108 21852+ int err;
076b876e 21853+ unsigned int mnt_flags, sigen;
c1595e42 21854+ unsigned char udba_none;
4a4d8108 21855+ aufs_bindex_t bindex;
4a4d8108
AM
21856+ struct super_block *sb, *h_sb;
21857+ struct inode *inode;
1facf9fc 21858+
c1595e42
JR
21859+ h_path->mnt = NULL;
21860+ h_path->dentry = NULL;
21861+
21862+ err = 0;
4a4d8108 21863+ sb = dentry->d_sb;
4a4d8108
AM
21864+ mnt_flags = au_mntflags(sb);
21865+ udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
1facf9fc 21866+
a2654f78
AM
21867+ if (unlikely(locked))
21868+ goto body; /* skip locking dinfo */
21869+
4a4d8108 21870+ /* support fstat(2) */
027c5e7a 21871+ if (!d_unlinked(dentry) && !udba_none) {
076b876e 21872+ sigen = au_sigen(sb);
027c5e7a
AM
21873+ err = au_digen_test(dentry, sigen);
21874+ if (!err) {
4a4d8108 21875+ di_read_lock_child(dentry, AuLock_IR);
027c5e7a 21876+ err = au_dbrange_test(dentry);
c1595e42
JR
21877+ if (unlikely(err)) {
21878+ di_read_unlock(dentry, AuLock_IR);
21879+ goto out;
21880+ }
027c5e7a 21881+ } else {
4a4d8108
AM
21882+ AuDebugOn(IS_ROOT(dentry));
21883+ di_write_lock_child(dentry);
027c5e7a
AM
21884+ err = au_dbrange_test(dentry);
21885+ if (!err)
21886+ err = au_reval_for_attr(dentry, sigen);
c1595e42
JR
21887+ if (!err)
21888+ di_downgrade_lock(dentry, AuLock_IR);
21889+ else {
21890+ di_write_unlock(dentry);
21891+ goto out;
21892+ }
4a4d8108
AM
21893+ }
21894+ } else
21895+ di_read_lock_child(dentry, AuLock_IR);
1facf9fc 21896+
a2654f78 21897+body:
5527c038 21898+ inode = d_inode(dentry);
5afbbe0d 21899+ bindex = au_ibtop(inode);
c1595e42
JR
21900+ h_path->mnt = au_sbr_mnt(sb, bindex);
21901+ h_sb = h_path->mnt->mnt_sb;
21902+ if (!force
21903+ && !au_test_fs_bad_iattr(h_sb)
21904+ && udba_none)
21905+ goto out; /* success */
1facf9fc 21906+
5afbbe0d 21907+ if (au_dbtop(dentry) == bindex)
c1595e42 21908+ h_path->dentry = au_h_dptr(dentry, bindex);
4a4d8108 21909+ else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
c1595e42
JR
21910+ h_path->dentry = au_plink_lkup(inode, bindex);
21911+ if (IS_ERR(h_path->dentry))
21912+ /* pretending success */
21913+ h_path->dentry = NULL;
21914+ else
21915+ dput(h_path->dentry);
4a4d8108 21916+ }
c1595e42
JR
21917+
21918+out:
21919+ return err;
21920+}
21921+
521ced18
JR
21922+static int aufs_getattr(const struct path *path, struct kstat *st,
21923+ u32 request, unsigned int query)
c1595e42
JR
21924+{
21925+ int err;
21926+ unsigned char positive;
21927+ struct path h_path;
521ced18 21928+ struct dentry *dentry;
c1595e42
JR
21929+ struct inode *inode;
21930+ struct super_block *sb;
21931+
521ced18 21932+ dentry = path->dentry;
5527c038 21933+ inode = d_inode(dentry);
c1595e42
JR
21934+ sb = dentry->d_sb;
21935+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21936+ if (unlikely(err))
21937+ goto out;
a2654f78 21938+ err = au_h_path_getattr(dentry, /*force*/0, &h_path, /*locked*/0);
c1595e42
JR
21939+ if (unlikely(err))
21940+ goto out_si;
c06a8ce3 21941+ if (unlikely(!h_path.dentry))
c1595e42 21942+ /* illegally overlapped or something */
4a4d8108
AM
21943+ goto out_fill; /* pretending success */
21944+
5527c038 21945+ positive = d_is_positive(h_path.dentry);
4a4d8108 21946+ if (positive)
521ced18
JR
21947+ /* no vfsub version */
21948+ err = vfs_getattr(&h_path, st, request, query);
4a4d8108
AM
21949+ if (!err) {
21950+ if (positive)
c06a8ce3 21951+ au_refresh_iattr(inode, st,
5527c038 21952+ d_inode(h_path.dentry)->i_nlink);
4a4d8108 21953+ goto out_fill; /* success */
1facf9fc 21954+ }
7f207e10 21955+ AuTraceErr(err);
c1595e42 21956+ goto out_di;
4a4d8108 21957+
4f0767ce 21958+out_fill:
4a4d8108 21959+ generic_fillattr(inode, st);
c1595e42 21960+out_di:
4a4d8108 21961+ di_read_unlock(dentry, AuLock_IR);
c1595e42 21962+out_si:
4a4d8108 21963+ si_read_unlock(sb);
7f207e10
AM
21964+out:
21965+ AuTraceErr(err);
4a4d8108 21966+ return err;
1facf9fc 21967+}
21968+
21969+/* ---------------------------------------------------------------------- */
21970+
febd17d6
JR
21971+static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
21972+ struct delayed_call *done)
4a4d8108 21973+{
c2c0f25c 21974+ const char *ret;
c2c0f25c 21975+ struct dentry *h_dentry;
febd17d6 21976+ struct inode *h_inode;
4a4d8108 21977+ int err;
c2c0f25c 21978+ aufs_bindex_t bindex;
1facf9fc 21979+
79b8bda9 21980+ ret = NULL; /* suppress a warning */
febd17d6
JR
21981+ err = -ECHILD;
21982+ if (!dentry)
21983+ goto out;
21984+
027c5e7a
AM
21985+ err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
21986+ if (unlikely(err))
c2c0f25c 21987+ goto out;
027c5e7a
AM
21988+
21989+ err = au_d_hashed_positive(dentry);
c2c0f25c
AM
21990+ if (unlikely(err))
21991+ goto out_unlock;
21992+
21993+ err = -EINVAL;
21994+ inode = d_inode(dentry);
5afbbe0d 21995+ bindex = au_ibtop(inode);
c2c0f25c 21996+ h_inode = au_h_iptr(inode, bindex);
febd17d6 21997+ if (unlikely(!h_inode->i_op->get_link))
c2c0f25c
AM
21998+ goto out_unlock;
21999+
22000+ err = -EBUSY;
22001+ h_dentry = NULL;
5afbbe0d 22002+ if (au_dbtop(dentry) <= bindex) {
c2c0f25c
AM
22003+ h_dentry = au_h_dptr(dentry, bindex);
22004+ if (h_dentry)
22005+ dget(h_dentry);
027c5e7a 22006+ }
c2c0f25c
AM
22007+ if (!h_dentry) {
22008+ h_dentry = d_find_any_alias(h_inode);
22009+ if (IS_ERR(h_dentry)) {
22010+ err = PTR_ERR(h_dentry);
febd17d6 22011+ goto out_unlock;
c2c0f25c
AM
22012+ }
22013+ }
22014+ if (unlikely(!h_dentry))
febd17d6 22015+ goto out_unlock;
1facf9fc 22016+
c2c0f25c 22017+ err = 0;
062440b3 22018+ AuDbg("%ps\n", h_inode->i_op->get_link);
c2c0f25c 22019+ AuDbgDentry(h_dentry);
f2c43d5f 22020+ ret = vfs_get_link(h_dentry, done);
c2c0f25c 22021+ dput(h_dentry);
febd17d6
JR
22022+ if (IS_ERR(ret))
22023+ err = PTR_ERR(ret);
c2c0f25c 22024+
c2c0f25c
AM
22025+out_unlock:
22026+ aufs_read_unlock(dentry, AuLock_IR);
4f0767ce 22027+out:
c2c0f25c
AM
22028+ if (unlikely(err))
22029+ ret = ERR_PTR(err);
22030+ AuTraceErrPtr(ret);
22031+ return ret;
4a4d8108 22032+}
1facf9fc 22033+
4a4d8108 22034+/* ---------------------------------------------------------------------- */
1facf9fc 22035+
e2f27e51
AM
22036+static int au_is_special(struct inode *inode)
22037+{
22038+ return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
22039+}
22040+
acd2b654
AM
22041+static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
22042+ int flags)
4a4d8108 22043+{
0c3ec466 22044+ int err;
e2f27e51 22045+ aufs_bindex_t bindex;
0c3ec466
AM
22046+ struct super_block *sb;
22047+ struct inode *h_inode;
e2f27e51 22048+ struct vfsmount *h_mnt;
0c3ec466
AM
22049+
22050+ sb = inode->i_sb;
e2f27e51
AM
22051+ WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
22052+ "unexpected s_flags 0x%lx", sb->s_flags);
22053+
0c3ec466
AM
22054+ /* mmap_sem might be acquired already, cf. aufs_mmap() */
22055+ lockdep_off();
22056+ si_read_lock(sb, AuLock_FLUSH);
22057+ ii_write_lock_child(inode);
e2f27e51
AM
22058+
22059+ err = 0;
22060+ bindex = au_ibtop(inode);
22061+ h_inode = au_h_iptr(inode, bindex);
22062+ if (!au_test_ro(sb, bindex, inode)) {
22063+ h_mnt = au_sbr_mnt(sb, bindex);
22064+ err = vfsub_mnt_want_write(h_mnt);
22065+ if (!err) {
22066+ err = vfsub_update_time(h_inode, ts, flags);
22067+ vfsub_mnt_drop_write(h_mnt);
22068+ }
22069+ } else if (au_is_special(h_inode)) {
22070+ /*
22071+ * Never copy-up here.
22072+ * These special files may already be opened and used for
22073+ * communicating. If we copied it up, then the communication
22074+ * would be corrupted.
22075+ */
22076+ AuWarn1("timestamps for i%lu are ignored "
22077+ "since it is on readonly branch (hi%lu).\n",
22078+ inode->i_ino, h_inode->i_ino);
22079+ } else if (flags & ~S_ATIME) {
22080+ err = -EIO;
22081+ AuIOErr1("unexpected flags 0x%x\n", flags);
22082+ AuDebugOn(1);
22083+ }
22084+
38d290e6
JR
22085+ if (!err)
22086+ au_cpup_attr_timesizes(inode);
0c3ec466
AM
22087+ ii_write_unlock(inode);
22088+ si_read_unlock(sb);
22089+ lockdep_on();
38d290e6
JR
22090+
22091+ if (!err && (flags & S_VERSION))
22092+ inode_inc_iversion(inode);
22093+
0c3ec466 22094+ return err;
4a4d8108 22095+}
1facf9fc 22096+
4a4d8108 22097+/* ---------------------------------------------------------------------- */
1308ab2a 22098+
b95c5147
AM
22099+/* no getattr version will be set by module.c:aufs_init() */
22100+struct inode_operations aufs_iop_nogetattr[AuIop_Last],
22101+ aufs_iop[] = {
22102+ [AuIop_SYMLINK] = {
22103+ .permission = aufs_permission,
c1595e42 22104+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
22105+ .get_acl = aufs_get_acl,
22106+ .set_acl = aufs_set_acl, /* unsupport for symlink? */
c1595e42
JR
22107+#endif
22108+
b95c5147
AM
22109+ .setattr = aufs_setattr,
22110+ .getattr = aufs_getattr,
0c3ec466 22111+
c1595e42 22112+#ifdef CONFIG_AUFS_XATTR
b95c5147 22113+ .listxattr = aufs_listxattr,
c1595e42
JR
22114+#endif
22115+
febd17d6 22116+ .get_link = aufs_get_link,
0c3ec466 22117+
b95c5147
AM
22118+ /* .update_time = aufs_update_time */
22119+ },
22120+ [AuIop_DIR] = {
22121+ .create = aufs_create,
22122+ .lookup = aufs_lookup,
22123+ .link = aufs_link,
22124+ .unlink = aufs_unlink,
22125+ .symlink = aufs_symlink,
22126+ .mkdir = aufs_mkdir,
22127+ .rmdir = aufs_rmdir,
22128+ .mknod = aufs_mknod,
22129+ .rename = aufs_rename,
22130+
22131+ .permission = aufs_permission,
c1595e42 22132+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
22133+ .get_acl = aufs_get_acl,
22134+ .set_acl = aufs_set_acl,
c1595e42
JR
22135+#endif
22136+
b95c5147
AM
22137+ .setattr = aufs_setattr,
22138+ .getattr = aufs_getattr,
0c3ec466 22139+
c1595e42 22140+#ifdef CONFIG_AUFS_XATTR
b95c5147 22141+ .listxattr = aufs_listxattr,
c1595e42
JR
22142+#endif
22143+
b95c5147
AM
22144+ .update_time = aufs_update_time,
22145+ .atomic_open = aufs_atomic_open,
22146+ .tmpfile = aufs_tmpfile
22147+ },
22148+ [AuIop_OTHER] = {
22149+ .permission = aufs_permission,
c1595e42 22150+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
22151+ .get_acl = aufs_get_acl,
22152+ .set_acl = aufs_set_acl,
c1595e42
JR
22153+#endif
22154+
b95c5147
AM
22155+ .setattr = aufs_setattr,
22156+ .getattr = aufs_getattr,
0c3ec466 22157+
c1595e42 22158+#ifdef CONFIG_AUFS_XATTR
b95c5147 22159+ .listxattr = aufs_listxattr,
c1595e42
JR
22160+#endif
22161+
b95c5147
AM
22162+ .update_time = aufs_update_time
22163+ }
4a4d8108 22164+};
7f207e10
AM
22165diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
22166--- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 22167+++ linux/fs/aufs/i_op_del.c 2019-01-28 14:36:12.222418047 +0100
062440b3 22168@@ -0,0 +1,512 @@
cd7a4cd9 22169+// SPDX-License-Identifier: GPL-2.0
1facf9fc 22170+/*
b00004a5 22171+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 22172+ *
22173+ * This program, aufs is free software; you can redistribute it and/or modify
22174+ * it under the terms of the GNU General Public License as published by
22175+ * the Free Software Foundation; either version 2 of the License, or
22176+ * (at your option) any later version.
dece6358
AM
22177+ *
22178+ * This program is distributed in the hope that it will be useful,
22179+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22180+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22181+ * GNU General Public License for more details.
22182+ *
22183+ * You should have received a copy of the GNU General Public License
523b37e3 22184+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 22185+ */
22186+
22187+/*
4a4d8108 22188+ * inode operations (del entry)
1308ab2a 22189+ */
dece6358 22190+
1308ab2a 22191+#include "aufs.h"
dece6358 22192+
4a4d8108
AM
22193+/*
22194+ * decide if a new whiteout for @dentry is necessary or not.
22195+ * when it is necessary, prepare the parent dir for the upper branch whose
22196+ * branch index is @bcpup for creation. the actual creation of the whiteout will
22197+ * be done by caller.
22198+ * return value:
22199+ * 0: wh is unnecessary
22200+ * plus: wh is necessary
22201+ * minus: error
22202+ */
22203+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
1308ab2a 22204+{
4a4d8108 22205+ int need_wh, err;
5afbbe0d 22206+ aufs_bindex_t btop;
4a4d8108 22207+ struct super_block *sb;
dece6358 22208+
4a4d8108 22209+ sb = dentry->d_sb;
5afbbe0d 22210+ btop = au_dbtop(dentry);
4a4d8108 22211+ if (*bcpup < 0) {
5afbbe0d
AM
22212+ *bcpup = btop;
22213+ if (au_test_ro(sb, btop, d_inode(dentry))) {
4a4d8108
AM
22214+ err = AuWbrCopyup(au_sbi(sb), dentry);
22215+ *bcpup = err;
22216+ if (unlikely(err < 0))
22217+ goto out;
22218+ }
22219+ } else
5afbbe0d 22220+ AuDebugOn(btop < *bcpup
5527c038 22221+ || au_test_ro(sb, *bcpup, d_inode(dentry)));
5afbbe0d 22222+ AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
1308ab2a 22223+
5afbbe0d 22224+ if (*bcpup != btop) {
4a4d8108
AM
22225+ err = au_cpup_dirs(dentry, *bcpup);
22226+ if (unlikely(err))
22227+ goto out;
22228+ need_wh = 1;
22229+ } else {
027c5e7a 22230+ struct au_dinfo *dinfo, *tmp;
4a4d8108 22231+
027c5e7a
AM
22232+ need_wh = -ENOMEM;
22233+ dinfo = au_di(dentry);
22234+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
22235+ if (tmp) {
22236+ au_di_cp(tmp, dinfo);
22237+ au_di_swap(tmp, dinfo);
22238+ /* returns the number of positive dentries */
5afbbe0d
AM
22239+ need_wh = au_lkup_dentry(dentry, btop + 1,
22240+ /* AuLkup_IGNORE_PERM */ 0);
027c5e7a
AM
22241+ au_di_swap(tmp, dinfo);
22242+ au_rw_write_unlock(&tmp->di_rwsem);
22243+ au_di_free(tmp);
4a4d8108
AM
22244+ }
22245+ }
22246+ AuDbg("need_wh %d\n", need_wh);
22247+ err = need_wh;
22248+
4f0767ce 22249+out:
4a4d8108 22250+ return err;
1facf9fc 22251+}
22252+
4a4d8108
AM
22253+/*
22254+ * simple tests for the del-entry operations.
22255+ * following the checks in vfs, plus the parent-child relationship.
22256+ */
22257+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
22258+ struct dentry *h_parent, int isdir)
1facf9fc 22259+{
4a4d8108
AM
22260+ int err;
22261+ umode_t h_mode;
22262+ struct dentry *h_dentry, *h_latest;
1308ab2a 22263+ struct inode *h_inode;
1facf9fc 22264+
4a4d8108 22265+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 22266+ if (d_really_is_positive(dentry)) {
4a4d8108 22267+ err = -ENOENT;
5527c038
JR
22268+ if (unlikely(d_is_negative(h_dentry)))
22269+ goto out;
22270+ h_inode = d_inode(h_dentry);
22271+ if (unlikely(!h_inode->i_nlink))
4a4d8108 22272+ goto out;
1facf9fc 22273+
4a4d8108
AM
22274+ h_mode = h_inode->i_mode;
22275+ if (!isdir) {
22276+ err = -EISDIR;
22277+ if (unlikely(S_ISDIR(h_mode)))
22278+ goto out;
22279+ } else if (unlikely(!S_ISDIR(h_mode))) {
22280+ err = -ENOTDIR;
22281+ goto out;
22282+ }
22283+ } else {
22284+ /* rename(2) case */
22285+ err = -EIO;
5527c038 22286+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
22287+ goto out;
22288+ }
1facf9fc 22289+
4a4d8108
AM
22290+ err = -ENOENT;
22291+ /* expected parent dir is locked */
22292+ if (unlikely(h_parent != h_dentry->d_parent))
22293+ goto out;
22294+ err = 0;
22295+
22296+ /*
22297+ * rmdir a dir may break the consistency on some filesystem.
22298+ * let's try heavy test.
22299+ */
22300+ err = -EACCES;
076b876e 22301+ if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
5527c038 22302+ && au_test_h_perm(d_inode(h_parent),
076b876e 22303+ MAY_EXEC | MAY_WRITE)))
4a4d8108
AM
22304+ goto out;
22305+
076b876e 22306+ h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
4a4d8108
AM
22307+ err = -EIO;
22308+ if (IS_ERR(h_latest))
22309+ goto out;
22310+ if (h_latest == h_dentry)
22311+ err = 0;
22312+ dput(h_latest);
22313+
4f0767ce 22314+out:
4a4d8108 22315+ return err;
1308ab2a 22316+}
1facf9fc 22317+
4a4d8108
AM
22318+/*
22319+ * decide the branch where we operate for @dentry. the branch index will be set
acd2b654 22320+ * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
4a4d8108
AM
22321+ * dir for reverting.
22322+ * when a new whiteout is necessary, create it.
22323+ */
22324+static struct dentry*
22325+lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
22326+ struct au_dtime *dt, struct au_pin *pin)
1308ab2a 22327+{
4a4d8108
AM
22328+ struct dentry *wh_dentry;
22329+ struct super_block *sb;
22330+ struct path h_path;
22331+ int err, need_wh;
22332+ unsigned int udba;
22333+ aufs_bindex_t bcpup;
dece6358 22334+
4a4d8108
AM
22335+ need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
22336+ wh_dentry = ERR_PTR(need_wh);
22337+ if (unlikely(need_wh < 0))
22338+ goto out;
22339+
22340+ sb = dentry->d_sb;
22341+ udba = au_opt_udba(sb);
22342+ bcpup = *rbcpup;
22343+ err = au_pin(pin, dentry, bcpup, udba,
22344+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22345+ wh_dentry = ERR_PTR(err);
22346+ if (unlikely(err))
22347+ goto out;
22348+
22349+ h_path.dentry = au_pinned_h_parent(pin);
22350+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 22351+ && au_dbtop(dentry) == bcpup) {
4a4d8108
AM
22352+ err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
22353+ wh_dentry = ERR_PTR(err);
22354+ if (unlikely(err))
22355+ goto out_unpin;
22356+ }
22357+
22358+ h_path.mnt = au_sbr_mnt(sb, bcpup);
22359+ au_dtime_store(dt, au_pinned_parent(pin), &h_path);
22360+ wh_dentry = NULL;
22361+ if (!need_wh)
22362+ goto out; /* success, no need to create whiteout */
22363+
22364+ wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
22365+ if (IS_ERR(wh_dentry))
22366+ goto out_unpin;
22367+
22368+ /* returns with the parent is locked and wh_dentry is dget-ed */
22369+ goto out; /* success */
22370+
4f0767ce 22371+out_unpin:
4a4d8108 22372+ au_unpin(pin);
4f0767ce 22373+out:
4a4d8108 22374+ return wh_dentry;
1facf9fc 22375+}
22376+
4a4d8108
AM
22377+/*
22378+ * when removing a dir, rename it to a unique temporary whiteout-ed name first
22379+ * in order to be revertible and save time for removing many child whiteouts
22380+ * under the dir.
22381+ * returns 1 when there are too many child whiteout and caller should remove
22382+ * them asynchronously. returns 0 when the number of children is enough small to
22383+ * remove now or the branch fs is a remote fs.
22384+ * otherwise return an error.
22385+ */
22386+static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
22387+ struct au_nhash *whlist, struct inode *dir)
1facf9fc 22388+{
4a4d8108
AM
22389+ int rmdir_later, err, dirwh;
22390+ struct dentry *h_dentry;
22391+ struct super_block *sb;
5527c038 22392+ struct inode *inode;
4a4d8108
AM
22393+
22394+ sb = dentry->d_sb;
22395+ SiMustAnyLock(sb);
22396+ h_dentry = au_h_dptr(dentry, bindex);
22397+ err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
22398+ if (unlikely(err))
22399+ goto out;
22400+
22401+ /* stop monitoring */
5527c038
JR
22402+ inode = d_inode(dentry);
22403+ au_hn_free(au_hi(inode, bindex));
4a4d8108
AM
22404+
22405+ if (!au_test_fs_remote(h_dentry->d_sb)) {
22406+ dirwh = au_sbi(sb)->si_dirwh;
22407+ rmdir_later = (dirwh <= 1);
22408+ if (!rmdir_later)
22409+ rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
22410+ dirwh);
22411+ if (rmdir_later)
22412+ return rmdir_later;
22413+ }
1facf9fc 22414+
4a4d8108
AM
22415+ err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
22416+ if (unlikely(err)) {
523b37e3
AM
22417+ AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
22418+ h_dentry, bindex, err);
4a4d8108
AM
22419+ err = 0;
22420+ }
dece6358 22421+
4f0767ce 22422+out:
4a4d8108
AM
22423+ AuTraceErr(err);
22424+ return err;
22425+}
1308ab2a 22426+
4a4d8108
AM
22427+/*
22428+ * final procedure for deleting a entry.
22429+ * maintain dentry and iattr.
22430+ */
22431+static void epilog(struct inode *dir, struct dentry *dentry,
22432+ aufs_bindex_t bindex)
22433+{
22434+ struct inode *inode;
1308ab2a 22435+
5527c038 22436+ inode = d_inode(dentry);
4a4d8108
AM
22437+ d_drop(dentry);
22438+ inode->i_ctime = dir->i_ctime;
1308ab2a 22439+
b912730e 22440+ au_dir_ts(dir, bindex);
be118d29 22441+ inode_inc_iversion(dir);
1facf9fc 22442+}
22443+
4a4d8108
AM
22444+/*
22445+ * when an error happened, remove the created whiteout and revert everything.
22446+ */
7f207e10
AM
22447+static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
22448+ aufs_bindex_t bwh, struct dentry *wh_dentry,
22449+ struct dentry *dentry, struct au_dtime *dt)
1facf9fc 22450+{
4a4d8108
AM
22451+ int rerr;
22452+ struct path h_path = {
22453+ .dentry = wh_dentry,
7f207e10 22454+ .mnt = au_sbr_mnt(dir->i_sb, bindex)
4a4d8108 22455+ };
dece6358 22456+
7f207e10 22457+ rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
4a4d8108
AM
22458+ if (!rerr) {
22459+ au_set_dbwh(dentry, bwh);
22460+ au_dtime_revert(dt);
22461+ return 0;
22462+ }
dece6358 22463+
523b37e3 22464+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
4a4d8108 22465+ return -EIO;
1facf9fc 22466+}
22467+
4a4d8108 22468+/* ---------------------------------------------------------------------- */
1facf9fc 22469+
4a4d8108 22470+int aufs_unlink(struct inode *dir, struct dentry *dentry)
1308ab2a 22471+{
4a4d8108 22472+ int err;
5afbbe0d 22473+ aufs_bindex_t bwh, bindex, btop;
523b37e3 22474+ struct inode *inode, *h_dir, *delegated;
4a4d8108 22475+ struct dentry *parent, *wh_dentry;
acd2b654 22476+ /* to reduce stack size */
c2b27bf2
AM
22477+ struct {
22478+ struct au_dtime dt;
22479+ struct au_pin pin;
22480+ struct path h_path;
22481+ } *a;
1facf9fc 22482+
4a4d8108 22483+ IMustLock(dir);
027c5e7a 22484+
c2b27bf2
AM
22485+ err = -ENOMEM;
22486+ a = kmalloc(sizeof(*a), GFP_NOFS);
22487+ if (unlikely(!a))
22488+ goto out;
22489+
027c5e7a
AM
22490+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
22491+ if (unlikely(err))
c2b27bf2 22492+ goto out_free;
027c5e7a
AM
22493+ err = au_d_hashed_positive(dentry);
22494+ if (unlikely(err))
22495+ goto out_unlock;
5527c038 22496+ inode = d_inode(dentry);
4a4d8108 22497+ IMustLock(inode);
027c5e7a 22498+ err = -EISDIR;
2000de60 22499+ if (unlikely(d_is_dir(dentry)))
027c5e7a 22500+ goto out_unlock; /* possible? */
1facf9fc 22501+
5afbbe0d 22502+ btop = au_dbtop(dentry);
4a4d8108
AM
22503+ bwh = au_dbwh(dentry);
22504+ bindex = -1;
027c5e7a
AM
22505+ parent = dentry->d_parent; /* dir inode is locked */
22506+ di_write_lock_parent(parent);
c2b27bf2
AM
22507+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
22508+ &a->pin);
4a4d8108
AM
22509+ err = PTR_ERR(wh_dentry);
22510+ if (IS_ERR(wh_dentry))
027c5e7a 22511+ goto out_parent;
1facf9fc 22512+
5afbbe0d
AM
22513+ a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
22514+ a->h_path.dentry = au_h_dptr(dentry, btop);
c2b27bf2 22515+ dget(a->h_path.dentry);
5afbbe0d 22516+ if (bindex == btop) {
c2b27bf2 22517+ h_dir = au_pinned_h_dir(&a->pin);
523b37e3
AM
22518+ delegated = NULL;
22519+ err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
22520+ if (unlikely(err == -EWOULDBLOCK)) {
22521+ pr_warn("cannot retry for NFSv4 delegation"
22522+ " for an internal unlink\n");
22523+ iput(delegated);
22524+ }
4a4d8108
AM
22525+ } else {
22526+ /* dir inode is locked */
5527c038 22527+ h_dir = d_inode(wh_dentry->d_parent);
4a4d8108
AM
22528+ IMustLock(h_dir);
22529+ err = 0;
22530+ }
dece6358 22531+
4a4d8108 22532+ if (!err) {
7f207e10 22533+ vfsub_drop_nlink(inode);
4a4d8108
AM
22534+ epilog(dir, dentry, bindex);
22535+
22536+ /* update target timestamps */
5afbbe0d 22537+ if (bindex == btop) {
c2b27bf2
AM
22538+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
22539+ /*ignore*/
5527c038 22540+ inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
4a4d8108
AM
22541+ } else
22542+ /* todo: this timestamp may be reverted later */
22543+ inode->i_ctime = h_dir->i_ctime;
027c5e7a 22544+ goto out_unpin; /* success */
1facf9fc 22545+ }
22546+
4a4d8108
AM
22547+ /* revert */
22548+ if (wh_dentry) {
22549+ int rerr;
22550+
c2b27bf2
AM
22551+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22552+ &a->dt);
4a4d8108
AM
22553+ if (rerr)
22554+ err = rerr;
dece6358 22555+ }
1facf9fc 22556+
027c5e7a 22557+out_unpin:
c2b27bf2 22558+ au_unpin(&a->pin);
4a4d8108 22559+ dput(wh_dentry);
c2b27bf2 22560+ dput(a->h_path.dentry);
027c5e7a 22561+out_parent:
4a4d8108 22562+ di_write_unlock(parent);
027c5e7a 22563+out_unlock:
4a4d8108 22564+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 22565+out_free:
e49925d1 22566+ au_kfree_rcu(a);
027c5e7a 22567+out:
4a4d8108 22568+ return err;
dece6358
AM
22569+}
22570+
4a4d8108 22571+int aufs_rmdir(struct inode *dir, struct dentry *dentry)
1308ab2a 22572+{
4a4d8108 22573+ int err, rmdir_later;
5afbbe0d 22574+ aufs_bindex_t bwh, bindex, btop;
4a4d8108
AM
22575+ struct inode *inode;
22576+ struct dentry *parent, *wh_dentry, *h_dentry;
22577+ struct au_whtmp_rmdir *args;
acd2b654 22578+ /* to reduce stack size */
c2b27bf2
AM
22579+ struct {
22580+ struct au_dtime dt;
22581+ struct au_pin pin;
22582+ } *a;
1facf9fc 22583+
4a4d8108 22584+ IMustLock(dir);
027c5e7a 22585+
c2b27bf2
AM
22586+ err = -ENOMEM;
22587+ a = kmalloc(sizeof(*a), GFP_NOFS);
22588+ if (unlikely(!a))
22589+ goto out;
22590+
027c5e7a
AM
22591+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22592+ if (unlikely(err))
c2b27bf2 22593+ goto out_free;
53392da6
AM
22594+ err = au_alive_dir(dentry);
22595+ if (unlikely(err))
027c5e7a 22596+ goto out_unlock;
5527c038 22597+ inode = d_inode(dentry);
4a4d8108 22598+ IMustLock(inode);
027c5e7a 22599+ err = -ENOTDIR;
2000de60 22600+ if (unlikely(!d_is_dir(dentry)))
027c5e7a 22601+ goto out_unlock; /* possible? */
dece6358 22602+
4a4d8108
AM
22603+ err = -ENOMEM;
22604+ args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
22605+ if (unlikely(!args))
22606+ goto out_unlock;
dece6358 22607+
4a4d8108
AM
22608+ parent = dentry->d_parent; /* dir inode is locked */
22609+ di_write_lock_parent(parent);
22610+ err = au_test_empty(dentry, &args->whlist);
22611+ if (unlikely(err))
027c5e7a 22612+ goto out_parent;
1facf9fc 22613+
5afbbe0d 22614+ btop = au_dbtop(dentry);
4a4d8108
AM
22615+ bwh = au_dbwh(dentry);
22616+ bindex = -1;
c2b27bf2
AM
22617+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
22618+ &a->pin);
4a4d8108
AM
22619+ err = PTR_ERR(wh_dentry);
22620+ if (IS_ERR(wh_dentry))
027c5e7a 22621+ goto out_parent;
1facf9fc 22622+
5afbbe0d 22623+ h_dentry = au_h_dptr(dentry, btop);
4a4d8108
AM
22624+ dget(h_dentry);
22625+ rmdir_later = 0;
5afbbe0d
AM
22626+ if (bindex == btop) {
22627+ err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
4a4d8108
AM
22628+ if (err > 0) {
22629+ rmdir_later = err;
22630+ err = 0;
22631+ }
22632+ } else {
22633+ /* stop monitoring */
5afbbe0d 22634+ au_hn_free(au_hi(inode, btop));
4a4d8108
AM
22635+
22636+ /* dir inode is locked */
5527c038 22637+ IMustLock(d_inode(wh_dentry->d_parent));
1facf9fc 22638+ err = 0;
22639+ }
22640+
4a4d8108 22641+ if (!err) {
027c5e7a 22642+ vfsub_dead_dir(inode);
4a4d8108
AM
22643+ au_set_dbdiropq(dentry, -1);
22644+ epilog(dir, dentry, bindex);
1308ab2a 22645+
4a4d8108 22646+ if (rmdir_later) {
5afbbe0d 22647+ au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
4a4d8108
AM
22648+ args = NULL;
22649+ }
1308ab2a 22650+
4a4d8108 22651+ goto out_unpin; /* success */
1facf9fc 22652+ }
22653+
4a4d8108
AM
22654+ /* revert */
22655+ AuLabel(revert);
22656+ if (wh_dentry) {
22657+ int rerr;
1308ab2a 22658+
c2b27bf2
AM
22659+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22660+ &a->dt);
4a4d8108
AM
22661+ if (rerr)
22662+ err = rerr;
1facf9fc 22663+ }
22664+
4f0767ce 22665+out_unpin:
c2b27bf2 22666+ au_unpin(&a->pin);
4a4d8108
AM
22667+ dput(wh_dentry);
22668+ dput(h_dentry);
027c5e7a 22669+out_parent:
4a4d8108
AM
22670+ di_write_unlock(parent);
22671+ if (args)
22672+ au_whtmp_rmdir_free(args);
4f0767ce 22673+out_unlock:
4a4d8108 22674+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 22675+out_free:
e49925d1 22676+ au_kfree_rcu(a);
4f0767ce 22677+out:
4a4d8108
AM
22678+ AuTraceErr(err);
22679+ return err;
dece6358 22680+}
7f207e10
AM
22681diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
22682--- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 22683+++ linux/fs/aufs/i_op_ren.c 2019-01-28 14:36:12.222418047 +0100
acd2b654 22684@@ -0,0 +1,1249 @@
cd7a4cd9 22685+// SPDX-License-Identifier: GPL-2.0
1facf9fc 22686+/*
b00004a5 22687+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 22688+ *
22689+ * This program, aufs is free software; you can redistribute it and/or modify
22690+ * it under the terms of the GNU General Public License as published by
22691+ * the Free Software Foundation; either version 2 of the License, or
22692+ * (at your option) any later version.
dece6358
AM
22693+ *
22694+ * This program is distributed in the hope that it will be useful,
22695+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22696+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22697+ * GNU General Public License for more details.
22698+ *
22699+ * You should have received a copy of the GNU General Public License
523b37e3 22700+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 22701+ */
22702+
22703+/*
4a4d8108
AM
22704+ * inode operation (rename entry)
22705+ * todo: this is crazy monster
1facf9fc 22706+ */
22707+
22708+#include "aufs.h"
22709+
4a4d8108
AM
22710+enum { AuSRC, AuDST, AuSrcDst };
22711+enum { AuPARENT, AuCHILD, AuParentChild };
1facf9fc 22712+
f2c43d5f
AM
22713+#define AuRen_ISDIR_SRC 1
22714+#define AuRen_ISDIR_DST (1 << 1)
22715+#define AuRen_ISSAMEDIR (1 << 2)
22716+#define AuRen_WHSRC (1 << 3)
22717+#define AuRen_WHDST (1 << 4)
22718+#define AuRen_MNT_WRITE (1 << 5)
22719+#define AuRen_DT_DSTDIR (1 << 6)
22720+#define AuRen_DIROPQ_SRC (1 << 7)
22721+#define AuRen_DIROPQ_DST (1 << 8)
8b6a4947
AM
22722+#define AuRen_DIRREN (1 << 9)
22723+#define AuRen_DROPPED_SRC (1 << 10)
22724+#define AuRen_DROPPED_DST (1 << 11)
4a4d8108 22725+#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
7f207e10
AM
22726+#define au_fset_ren(flags, name) \
22727+ do { (flags) |= AuRen_##name; } while (0)
22728+#define au_fclr_ren(flags, name) \
22729+ do { (flags) &= ~AuRen_##name; } while (0)
1facf9fc 22730+
8b6a4947
AM
22731+#ifndef CONFIG_AUFS_DIRREN
22732+#undef AuRen_DIRREN
22733+#define AuRen_DIRREN 0
22734+#endif
22735+
4a4d8108
AM
22736+struct au_ren_args {
22737+ struct {
22738+ struct dentry *dentry, *h_dentry, *parent, *h_parent,
22739+ *wh_dentry;
22740+ struct inode *dir, *inode;
f2c43d5f 22741+ struct au_hinode *hdir, *hinode;
4a4d8108 22742+ struct au_dtime dt[AuParentChild];
f2c43d5f 22743+ aufs_bindex_t btop, bdiropq;
4a4d8108 22744+ } sd[AuSrcDst];
1facf9fc 22745+
4a4d8108
AM
22746+#define src_dentry sd[AuSRC].dentry
22747+#define src_dir sd[AuSRC].dir
22748+#define src_inode sd[AuSRC].inode
22749+#define src_h_dentry sd[AuSRC].h_dentry
22750+#define src_parent sd[AuSRC].parent
22751+#define src_h_parent sd[AuSRC].h_parent
22752+#define src_wh_dentry sd[AuSRC].wh_dentry
22753+#define src_hdir sd[AuSRC].hdir
f2c43d5f 22754+#define src_hinode sd[AuSRC].hinode
4a4d8108
AM
22755+#define src_h_dir sd[AuSRC].hdir->hi_inode
22756+#define src_dt sd[AuSRC].dt
5afbbe0d 22757+#define src_btop sd[AuSRC].btop
f2c43d5f 22758+#define src_bdiropq sd[AuSRC].bdiropq
1facf9fc 22759+
4a4d8108
AM
22760+#define dst_dentry sd[AuDST].dentry
22761+#define dst_dir sd[AuDST].dir
22762+#define dst_inode sd[AuDST].inode
22763+#define dst_h_dentry sd[AuDST].h_dentry
22764+#define dst_parent sd[AuDST].parent
22765+#define dst_h_parent sd[AuDST].h_parent
22766+#define dst_wh_dentry sd[AuDST].wh_dentry
22767+#define dst_hdir sd[AuDST].hdir
f2c43d5f 22768+#define dst_hinode sd[AuDST].hinode
4a4d8108
AM
22769+#define dst_h_dir sd[AuDST].hdir->hi_inode
22770+#define dst_dt sd[AuDST].dt
5afbbe0d 22771+#define dst_btop sd[AuDST].btop
f2c43d5f 22772+#define dst_bdiropq sd[AuDST].bdiropq
4a4d8108
AM
22773+
22774+ struct dentry *h_trap;
22775+ struct au_branch *br;
4a4d8108
AM
22776+ struct path h_path;
22777+ struct au_nhash whlist;
f2c43d5f 22778+ aufs_bindex_t btgt, src_bwh;
1facf9fc 22779+
f2c43d5f
AM
22780+ struct {
22781+ unsigned short auren_flags;
22782+ unsigned char flags; /* syscall parameter */
22783+ unsigned char exchange;
22784+ } __packed;
1facf9fc 22785+
4a4d8108
AM
22786+ struct au_whtmp_rmdir *thargs;
22787+ struct dentry *h_dst;
8b6a4947 22788+ struct au_hinode *h_root;
4a4d8108 22789+};
1308ab2a 22790+
4a4d8108 22791+/* ---------------------------------------------------------------------- */
1308ab2a 22792+
4a4d8108
AM
22793+/*
22794+ * functions for reverting.
22795+ * when an error happened in a single rename systemcall, we should revert
79b8bda9 22796+ * everything as if nothing happened.
4a4d8108
AM
22797+ * we don't need to revert the copied-up/down the parent dir since they are
22798+ * harmless.
22799+ */
1facf9fc 22800+
4a4d8108
AM
22801+#define RevertFailure(fmt, ...) do { \
22802+ AuIOErr("revert failure: " fmt " (%d, %d)\n", \
22803+ ##__VA_ARGS__, err, rerr); \
22804+ err = -EIO; \
22805+} while (0)
1facf9fc 22806+
f2c43d5f 22807+static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
1facf9fc 22808+{
4a4d8108 22809+ int rerr;
f2c43d5f
AM
22810+ struct dentry *d;
22811+#define src_or_dst(member) a->sd[idx].member
1facf9fc 22812+
f2c43d5f
AM
22813+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22814+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22815+ rerr = au_diropq_remove(d, a->btgt);
22816+ au_hn_inode_unlock(src_or_dst(hinode));
22817+ au_set_dbdiropq(d, src_or_dst(bdiropq));
4a4d8108 22818+ if (rerr)
f2c43d5f
AM
22819+ RevertFailure("remove diropq %pd", d);
22820+
22821+#undef src_or_dst_
22822+}
22823+
22824+static void au_ren_rev_diropq(int err, struct au_ren_args *a)
22825+{
22826+ if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
22827+ au_ren_do_rev_diropq(err, a, AuSRC);
22828+ if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
22829+ au_ren_do_rev_diropq(err, a, AuDST);
4a4d8108 22830+}
1facf9fc 22831+
4a4d8108
AM
22832+static void au_ren_rev_rename(int err, struct au_ren_args *a)
22833+{
22834+ int rerr;
523b37e3 22835+ struct inode *delegated;
1facf9fc 22836+
b4510431
AM
22837+ a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
22838+ a->src_h_parent);
4a4d8108
AM
22839+ rerr = PTR_ERR(a->h_path.dentry);
22840+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 22841+ RevertFailure("lkup one %pd", a->src_dentry);
4a4d8108 22842+ return;
1facf9fc 22843+ }
22844+
523b37e3 22845+ delegated = NULL;
4a4d8108
AM
22846+ rerr = vfsub_rename(a->dst_h_dir,
22847+ au_h_dptr(a->src_dentry, a->btgt),
f2c43d5f 22848+ a->src_h_dir, &a->h_path, &delegated, a->flags);
523b37e3
AM
22849+ if (unlikely(rerr == -EWOULDBLOCK)) {
22850+ pr_warn("cannot retry for NFSv4 delegation"
22851+ " for an internal rename\n");
22852+ iput(delegated);
22853+ }
4a4d8108
AM
22854+ d_drop(a->h_path.dentry);
22855+ dput(a->h_path.dentry);
22856+ /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
22857+ if (rerr)
523b37e3 22858+ RevertFailure("rename %pd", a->src_dentry);
1facf9fc 22859+}
22860+
4a4d8108 22861+static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
1facf9fc 22862+{
4a4d8108 22863+ int rerr;
523b37e3 22864+ struct inode *delegated;
dece6358 22865+
b4510431
AM
22866+ a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
22867+ a->dst_h_parent);
4a4d8108
AM
22868+ rerr = PTR_ERR(a->h_path.dentry);
22869+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 22870+ RevertFailure("lkup one %pd", a->dst_dentry);
4a4d8108
AM
22871+ return;
22872+ }
5527c038 22873+ if (d_is_positive(a->h_path.dentry)) {
4a4d8108
AM
22874+ d_drop(a->h_path.dentry);
22875+ dput(a->h_path.dentry);
22876+ return;
dece6358
AM
22877+ }
22878+
523b37e3
AM
22879+ delegated = NULL;
22880+ rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
f2c43d5f 22881+ &delegated, a->flags);
523b37e3
AM
22882+ if (unlikely(rerr == -EWOULDBLOCK)) {
22883+ pr_warn("cannot retry for NFSv4 delegation"
22884+ " for an internal rename\n");
22885+ iput(delegated);
22886+ }
4a4d8108
AM
22887+ d_drop(a->h_path.dentry);
22888+ dput(a->h_path.dentry);
22889+ if (!rerr)
22890+ au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
22891+ else
523b37e3 22892+ RevertFailure("rename %pd", a->h_dst);
4a4d8108 22893+}
1308ab2a 22894+
4a4d8108
AM
22895+static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
22896+{
22897+ int rerr;
1308ab2a 22898+
4a4d8108
AM
22899+ a->h_path.dentry = a->src_wh_dentry;
22900+ rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
027c5e7a 22901+ au_set_dbwh(a->src_dentry, a->src_bwh);
4a4d8108 22902+ if (rerr)
523b37e3 22903+ RevertFailure("unlink %pd", a->src_wh_dentry);
4a4d8108 22904+}
4a4d8108 22905+#undef RevertFailure
1facf9fc 22906+
1308ab2a 22907+/* ---------------------------------------------------------------------- */
22908+
4a4d8108
AM
22909+/*
22910+ * when we have to copyup the renaming entry, do it with the rename-target name
22911+ * in order to minimize the cost (the later actual rename is unnecessary).
22912+ * otherwise rename it on the target branch.
22913+ */
22914+static int au_ren_or_cpup(struct au_ren_args *a)
1facf9fc 22915+{
dece6358 22916+ int err;
4a4d8108 22917+ struct dentry *d;
523b37e3 22918+ struct inode *delegated;
1facf9fc 22919+
4a4d8108 22920+ d = a->src_dentry;
5afbbe0d 22921+ if (au_dbtop(d) == a->btgt) {
4a4d8108 22922+ a->h_path.dentry = a->dst_h_dentry;
5afbbe0d 22923+ AuDebugOn(au_dbtop(d) != a->btgt);
523b37e3 22924+ delegated = NULL;
4a4d8108 22925+ err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
f2c43d5f
AM
22926+ a->dst_h_dir, &a->h_path, &delegated,
22927+ a->flags);
523b37e3
AM
22928+ if (unlikely(err == -EWOULDBLOCK)) {
22929+ pr_warn("cannot retry for NFSv4 delegation"
22930+ " for an internal rename\n");
22931+ iput(delegated);
22932+ }
c2b27bf2 22933+ } else
86dc4139 22934+ BUG();
1308ab2a 22935+
027c5e7a
AM
22936+ if (!err && a->h_dst)
22937+ /* it will be set to dinfo later */
22938+ dget(a->h_dst);
1facf9fc 22939+
dece6358
AM
22940+ return err;
22941+}
1facf9fc 22942+
4a4d8108
AM
22943+/* cf. aufs_rmdir() */
22944+static int au_ren_del_whtmp(struct au_ren_args *a)
dece6358 22945+{
4a4d8108
AM
22946+ int err;
22947+ struct inode *dir;
1facf9fc 22948+
4a4d8108
AM
22949+ dir = a->dst_dir;
22950+ SiMustAnyLock(dir->i_sb);
22951+ if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
22952+ au_sbi(dir->i_sb)->si_dirwh)
22953+ || au_test_fs_remote(a->h_dst->d_sb)) {
22954+ err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
22955+ if (unlikely(err))
523b37e3
AM
22956+ pr_warn("failed removing whtmp dir %pd (%d), "
22957+ "ignored.\n", a->h_dst, err);
4a4d8108
AM
22958+ } else {
22959+ au_nhash_wh_free(&a->thargs->whlist);
22960+ a->thargs->whlist = a->whlist;
22961+ a->whlist.nh_num = 0;
22962+ au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
22963+ dput(a->h_dst);
22964+ a->thargs = NULL;
22965+ }
22966+
22967+ return 0;
1308ab2a 22968+}
1facf9fc 22969+
4a4d8108 22970+/* make it 'opaque' dir. */
f2c43d5f 22971+static int au_ren_do_diropq(struct au_ren_args *a, int idx)
4a4d8108
AM
22972+{
22973+ int err;
f2c43d5f
AM
22974+ struct dentry *d, *diropq;
22975+#define src_or_dst(member) a->sd[idx].member
1facf9fc 22976+
4a4d8108 22977+ err = 0;
f2c43d5f
AM
22978+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22979+ src_or_dst(bdiropq) = au_dbdiropq(d);
22980+ src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
22981+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22982+ diropq = au_diropq_create(d, a->btgt);
22983+ au_hn_inode_unlock(src_or_dst(hinode));
4a4d8108
AM
22984+ if (IS_ERR(diropq))
22985+ err = PTR_ERR(diropq);
076b876e
AM
22986+ else
22987+ dput(diropq);
1facf9fc 22988+
f2c43d5f 22989+#undef src_or_dst_
4a4d8108
AM
22990+ return err;
22991+}
1facf9fc 22992+
f2c43d5f 22993+static int au_ren_diropq(struct au_ren_args *a)
4a4d8108
AM
22994+{
22995+ int err;
f2c43d5f
AM
22996+ unsigned char always;
22997+ struct dentry *d;
1facf9fc 22998+
f2c43d5f
AM
22999+ err = 0;
23000+ d = a->dst_dentry; /* already renamed on the branch */
23001+ always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
23002+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
8b6a4947 23003+ && !au_ftest_ren(a->auren_flags, DIRREN)
f2c43d5f
AM
23004+ && a->btgt != au_dbdiropq(a->src_dentry)
23005+ && (a->dst_wh_dentry
23006+ || a->btgt <= au_dbdiropq(d)
23007+ /* hide the lower to keep xino */
23008+ /* the lowers may not be a dir, but we hide them anyway */
23009+ || a->btgt < au_dbbot(d)
23010+ || always)) {
23011+ AuDbg("here\n");
23012+ err = au_ren_do_diropq(a, AuSRC);
23013+ if (unlikely(err))
4a4d8108 23014+ goto out;
f2c43d5f 23015+ au_fset_ren(a->auren_flags, DIROPQ_SRC);
4a4d8108 23016+ }
f2c43d5f
AM
23017+ if (!a->exchange)
23018+ goto out; /* success */
1facf9fc 23019+
f2c43d5f
AM
23020+ d = a->src_dentry; /* already renamed on the branch */
23021+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
23022+ && a->btgt != au_dbdiropq(a->dst_dentry)
23023+ && (a->btgt < au_dbdiropq(d)
23024+ || a->btgt < au_dbbot(d)
23025+ || always)) {
23026+ AuDbgDentry(a->src_dentry);
23027+ AuDbgDentry(a->dst_dentry);
23028+ err = au_ren_do_diropq(a, AuDST);
4a4d8108 23029+ if (unlikely(err))
f2c43d5f
AM
23030+ goto out_rev_src;
23031+ au_fset_ren(a->auren_flags, DIROPQ_DST);
23032+ }
23033+ goto out; /* success */
dece6358 23034+
f2c43d5f
AM
23035+out_rev_src:
23036+ AuDbg("err %d, reverting src\n", err);
23037+ au_ren_rev_diropq(err, a);
23038+out:
23039+ return err;
23040+}
23041+
23042+static int do_rename(struct au_ren_args *a)
23043+{
23044+ int err;
23045+ struct dentry *d, *h_d;
23046+
23047+ if (!a->exchange) {
23048+ /* prepare workqueue args for asynchronous rmdir */
23049+ h_d = a->dst_h_dentry;
23050+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
8b6a4947 23051+ /* && !au_ftest_ren(a->auren_flags, DIRREN) */
f2c43d5f
AM
23052+ && d_is_positive(h_d)) {
23053+ err = -ENOMEM;
23054+ a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
23055+ GFP_NOFS);
23056+ if (unlikely(!a->thargs))
23057+ goto out;
23058+ a->h_dst = dget(h_d);
23059+ }
23060+
23061+ /* create whiteout for src_dentry */
23062+ if (au_ftest_ren(a->auren_flags, WHSRC)) {
23063+ a->src_bwh = au_dbwh(a->src_dentry);
23064+ AuDebugOn(a->src_bwh >= 0);
23065+ a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
23066+ a->src_h_parent);
23067+ err = PTR_ERR(a->src_wh_dentry);
23068+ if (IS_ERR(a->src_wh_dentry))
23069+ goto out_thargs;
23070+ }
23071+
23072+ /* lookup whiteout for dentry */
23073+ if (au_ftest_ren(a->auren_flags, WHDST)) {
23074+ h_d = au_wh_lkup(a->dst_h_parent,
23075+ &a->dst_dentry->d_name, a->br);
23076+ err = PTR_ERR(h_d);
23077+ if (IS_ERR(h_d))
23078+ goto out_whsrc;
23079+ if (d_is_negative(h_d))
23080+ dput(h_d);
23081+ else
23082+ a->dst_wh_dentry = h_d;
23083+ }
23084+
23085+ /* rename dentry to tmpwh */
23086+ if (a->thargs) {
23087+ err = au_whtmp_ren(a->dst_h_dentry, a->br);
23088+ if (unlikely(err))
23089+ goto out_whdst;
23090+
23091+ d = a->dst_dentry;
23092+ au_set_h_dptr(d, a->btgt, NULL);
23093+ err = au_lkup_neg(d, a->btgt, /*wh*/0);
23094+ if (unlikely(err))
23095+ goto out_whtmp;
23096+ a->dst_h_dentry = au_h_dptr(d, a->btgt);
23097+ }
4a4d8108 23098+ }
1facf9fc 23099+
5afbbe0d 23100+ BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
8b6a4947
AM
23101+#if 0
23102+ BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
23103+ && d_is_positive(a->dst_h_dentry)
23104+ && a->src_btop != a->btgt);
23105+#endif
1facf9fc 23106+
4a4d8108 23107+ /* rename by vfs_rename or cpup */
4a4d8108
AM
23108+ err = au_ren_or_cpup(a);
23109+ if (unlikely(err))
23110+ /* leave the copied-up one */
23111+ goto out_whtmp;
1308ab2a 23112+
4a4d8108 23113+ /* make dir opaque */
f2c43d5f
AM
23114+ err = au_ren_diropq(a);
23115+ if (unlikely(err))
23116+ goto out_rename;
1308ab2a 23117+
4a4d8108 23118+ /* update target timestamps */
f2c43d5f
AM
23119+ if (a->exchange) {
23120+ AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
23121+ a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
23122+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
23123+ a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
23124+ }
5afbbe0d 23125+ AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
4a4d8108
AM
23126+ a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
23127+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
5527c038 23128+ a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
1facf9fc 23129+
f2c43d5f
AM
23130+ if (!a->exchange) {
23131+ /* remove whiteout for dentry */
23132+ if (a->dst_wh_dentry) {
23133+ a->h_path.dentry = a->dst_wh_dentry;
23134+ err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
23135+ a->dst_dentry);
23136+ if (unlikely(err))
23137+ goto out_diropq;
23138+ }
1facf9fc 23139+
f2c43d5f
AM
23140+ /* remove whtmp */
23141+ if (a->thargs)
23142+ au_ren_del_whtmp(a); /* ignore this error */
1308ab2a 23143+
f2c43d5f
AM
23144+ au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
23145+ }
4a4d8108
AM
23146+ err = 0;
23147+ goto out_success;
23148+
4f0767ce 23149+out_diropq:
f2c43d5f 23150+ au_ren_rev_diropq(err, a);
4f0767ce 23151+out_rename:
7e9cd9fe 23152+ au_ren_rev_rename(err, a);
027c5e7a 23153+ dput(a->h_dst);
4f0767ce 23154+out_whtmp:
4a4d8108
AM
23155+ if (a->thargs)
23156+ au_ren_rev_whtmp(err, a);
4f0767ce 23157+out_whdst:
4a4d8108
AM
23158+ dput(a->dst_wh_dentry);
23159+ a->dst_wh_dentry = NULL;
4f0767ce 23160+out_whsrc:
4a4d8108
AM
23161+ if (a->src_wh_dentry)
23162+ au_ren_rev_whsrc(err, a);
4f0767ce 23163+out_success:
4a4d8108
AM
23164+ dput(a->src_wh_dentry);
23165+ dput(a->dst_wh_dentry);
4f0767ce 23166+out_thargs:
4a4d8108
AM
23167+ if (a->thargs) {
23168+ dput(a->h_dst);
23169+ au_whtmp_rmdir_free(a->thargs);
23170+ a->thargs = NULL;
23171+ }
4f0767ce 23172+out:
4a4d8108 23173+ return err;
dece6358 23174+}
1facf9fc 23175+
1308ab2a 23176+/* ---------------------------------------------------------------------- */
1facf9fc 23177+
4a4d8108
AM
23178+/*
23179+ * test if @dentry dir can be rename destination or not.
23180+ * success means, it is a logically empty dir.
23181+ */
23182+static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
1308ab2a 23183+{
4a4d8108 23184+ return au_test_empty(dentry, whlist);
1308ab2a 23185+}
1facf9fc 23186+
4a4d8108 23187+/*
8b6a4947
AM
23188+ * test if @a->src_dentry dir can be rename source or not.
23189+ * if it can, return 0.
4a4d8108
AM
23190+ * success means,
23191+ * - it is a logically empty dir.
23192+ * - or, it exists on writable branch and has no children including whiteouts
8b6a4947 23193+ * on the lower branch unless DIRREN is on.
4a4d8108 23194+ */
8b6a4947 23195+static int may_rename_srcdir(struct au_ren_args *a)
4a4d8108
AM
23196+{
23197+ int err;
23198+ unsigned int rdhash;
8b6a4947
AM
23199+ aufs_bindex_t btop, btgt;
23200+ struct dentry *dentry;
23201+ struct super_block *sb;
23202+ struct au_sbinfo *sbinfo;
1facf9fc 23203+
8b6a4947
AM
23204+ dentry = a->src_dentry;
23205+ sb = dentry->d_sb;
23206+ sbinfo = au_sbi(sb);
23207+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
23208+ au_fset_ren(a->auren_flags, DIRREN);
23209+
23210+ btgt = a->btgt;
5afbbe0d
AM
23211+ btop = au_dbtop(dentry);
23212+ if (btop != btgt) {
4a4d8108 23213+ struct au_nhash whlist;
dece6358 23214+
8b6a4947
AM
23215+ SiMustAnyLock(sb);
23216+ rdhash = sbinfo->si_rdhash;
4a4d8108
AM
23217+ if (!rdhash)
23218+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
23219+ dentry));
23220+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
23221+ if (unlikely(err))
23222+ goto out;
23223+ err = au_test_empty(dentry, &whlist);
23224+ au_nhash_wh_free(&whlist);
23225+ goto out;
23226+ }
dece6358 23227+
5afbbe0d 23228+ if (btop == au_dbtaildir(dentry))
4a4d8108 23229+ return 0; /* success */
dece6358 23230+
4a4d8108 23231+ err = au_test_empty_lower(dentry);
1facf9fc 23232+
4f0767ce 23233+out:
4a4d8108 23234+ if (err == -ENOTEMPTY) {
8b6a4947
AM
23235+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23236+ err = 0;
23237+ } else {
23238+ AuWarn1("renaming dir who has child(ren) on multiple "
23239+ "branches, is not supported\n");
23240+ err = -EXDEV;
23241+ }
4a4d8108
AM
23242+ }
23243+ return err;
23244+}
1308ab2a 23245+
4a4d8108
AM
23246+/* side effect: sets whlist and h_dentry */
23247+static int au_ren_may_dir(struct au_ren_args *a)
1308ab2a 23248+{
4a4d8108
AM
23249+ int err;
23250+ unsigned int rdhash;
23251+ struct dentry *d;
1facf9fc 23252+
4a4d8108
AM
23253+ d = a->dst_dentry;
23254+ SiMustAnyLock(d->d_sb);
1facf9fc 23255+
4a4d8108 23256+ err = 0;
f2c43d5f 23257+ if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
4a4d8108
AM
23258+ rdhash = au_sbi(d->d_sb)->si_rdhash;
23259+ if (!rdhash)
23260+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
23261+ err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
23262+ if (unlikely(err))
23263+ goto out;
1308ab2a 23264+
f2c43d5f
AM
23265+ if (!a->exchange) {
23266+ au_set_dbtop(d, a->dst_btop);
23267+ err = may_rename_dstdir(d, &a->whlist);
23268+ au_set_dbtop(d, a->btgt);
23269+ } else
8b6a4947 23270+ err = may_rename_srcdir(a);
4a4d8108 23271+ }
5afbbe0d 23272+ a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
4a4d8108
AM
23273+ if (unlikely(err))
23274+ goto out;
23275+
23276+ d = a->src_dentry;
5afbbe0d 23277+ a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
f2c43d5f 23278+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
8b6a4947 23279+ err = may_rename_srcdir(a);
4a4d8108
AM
23280+ if (unlikely(err)) {
23281+ au_nhash_wh_free(&a->whlist);
23282+ a->whlist.nh_num = 0;
23283+ }
23284+ }
4f0767ce 23285+out:
4a4d8108 23286+ return err;
1facf9fc 23287+}
23288+
4a4d8108 23289+/* ---------------------------------------------------------------------- */
1facf9fc 23290+
4a4d8108
AM
23291+/*
23292+ * simple tests for rename.
23293+ * following the checks in vfs, plus the parent-child relationship.
23294+ */
23295+static int au_may_ren(struct au_ren_args *a)
23296+{
23297+ int err, isdir;
23298+ struct inode *h_inode;
1facf9fc 23299+
5afbbe0d 23300+ if (a->src_btop == a->btgt) {
4a4d8108 23301+ err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
f2c43d5f 23302+ au_ftest_ren(a->auren_flags, ISDIR_SRC));
4a4d8108
AM
23303+ if (unlikely(err))
23304+ goto out;
23305+ err = -EINVAL;
23306+ if (unlikely(a->src_h_dentry == a->h_trap))
23307+ goto out;
23308+ }
1facf9fc 23309+
4a4d8108 23310+ err = 0;
5afbbe0d 23311+ if (a->dst_btop != a->btgt)
4a4d8108 23312+ goto out;
1facf9fc 23313+
027c5e7a
AM
23314+ err = -ENOTEMPTY;
23315+ if (unlikely(a->dst_h_dentry == a->h_trap))
23316+ goto out;
23317+
4a4d8108 23318+ err = -EIO;
f2c43d5f 23319+ isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
5527c038
JR
23320+ if (d_really_is_negative(a->dst_dentry)) {
23321+ if (d_is_negative(a->dst_h_dentry))
23322+ err = au_may_add(a->dst_dentry, a->btgt,
23323+ a->dst_h_parent, isdir);
4a4d8108 23324+ } else {
5527c038 23325+ if (unlikely(d_is_negative(a->dst_h_dentry)))
4a4d8108 23326+ goto out;
5527c038
JR
23327+ h_inode = d_inode(a->dst_h_dentry);
23328+ if (h_inode->i_nlink)
23329+ err = au_may_del(a->dst_dentry, a->btgt,
23330+ a->dst_h_parent, isdir);
4a4d8108 23331+ }
1facf9fc 23332+
4f0767ce 23333+out:
4a4d8108
AM
23334+ if (unlikely(err == -ENOENT || err == -EEXIST))
23335+ err = -EIO;
23336+ AuTraceErr(err);
23337+ return err;
23338+}
1facf9fc 23339+
1308ab2a 23340+/* ---------------------------------------------------------------------- */
1facf9fc 23341+
4a4d8108
AM
23342+/*
23343+ * locking order
23344+ * (VFS)
23345+ * - src_dir and dir by lock_rename()
acd2b654 23346+ * - inode if exists
4a4d8108
AM
23347+ * (aufs)
23348+ * - lock all
23349+ * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
23350+ * + si_read_lock
23351+ * + di_write_lock2_child()
23352+ * + di_write_lock_child()
23353+ * + ii_write_lock_child()
23354+ * + di_write_lock_child2()
23355+ * + ii_write_lock_child2()
23356+ * + src_parent and parent
23357+ * + di_write_lock_parent()
23358+ * + ii_write_lock_parent()
23359+ * + di_write_lock_parent2()
23360+ * + ii_write_lock_parent2()
23361+ * + lower src_dir and dir by vfsub_lock_rename()
23362+ * + verify the every relationships between child and parent. if any
23363+ * of them failed, unlock all and return -EBUSY.
23364+ */
23365+static void au_ren_unlock(struct au_ren_args *a)
1308ab2a 23366+{
4a4d8108
AM
23367+ vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
23368+ a->dst_h_parent, a->dst_hdir);
8b6a4947
AM
23369+ if (au_ftest_ren(a->auren_flags, DIRREN)
23370+ && a->h_root)
23371+ au_hn_inode_unlock(a->h_root);
f2c43d5f 23372+ if (au_ftest_ren(a->auren_flags, MNT_WRITE))
86dc4139 23373+ vfsub_mnt_drop_write(au_br_mnt(a->br));
1308ab2a 23374+}
23375+
4a4d8108 23376+static int au_ren_lock(struct au_ren_args *a)
1308ab2a 23377+{
4a4d8108
AM
23378+ int err;
23379+ unsigned int udba;
1308ab2a 23380+
4a4d8108
AM
23381+ err = 0;
23382+ a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
23383+ a->src_hdir = au_hi(a->src_dir, a->btgt);
23384+ a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
23385+ a->dst_hdir = au_hi(a->dst_dir, a->btgt);
86dc4139
AM
23386+
23387+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
23388+ if (unlikely(err))
23389+ goto out;
f2c43d5f 23390+ au_fset_ren(a->auren_flags, MNT_WRITE);
8b6a4947
AM
23391+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23392+ struct dentry *root;
23393+ struct inode *dir;
23394+
23395+ /*
23396+ * sbinfo is already locked, so this ii_read_lock is
23397+ * unnecessary. but our debugging feature checks it.
23398+ */
23399+ root = a->src_inode->i_sb->s_root;
23400+ if (root != a->src_parent && root != a->dst_parent) {
23401+ dir = d_inode(root);
23402+ ii_read_lock_parent3(dir);
23403+ a->h_root = au_hi(dir, a->btgt);
23404+ ii_read_unlock(dir);
23405+ au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
23406+ }
23407+ }
4a4d8108
AM
23408+ a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
23409+ a->dst_h_parent, a->dst_hdir);
23410+ udba = au_opt_udba(a->src_dentry->d_sb);
5527c038
JR
23411+ if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
23412+ || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
4a4d8108 23413+ err = au_busy_or_stale();
5afbbe0d 23414+ if (!err && au_dbtop(a->src_dentry) == a->btgt)
4a4d8108 23415+ err = au_h_verify(a->src_h_dentry, udba,
5527c038 23416+ d_inode(a->src_h_parent), a->src_h_parent,
4a4d8108 23417+ a->br);
5afbbe0d 23418+ if (!err && au_dbtop(a->dst_dentry) == a->btgt)
4a4d8108 23419+ err = au_h_verify(a->dst_h_dentry, udba,
5527c038 23420+ d_inode(a->dst_h_parent), a->dst_h_parent,
4a4d8108 23421+ a->br);
86dc4139 23422+ if (!err)
4a4d8108 23423+ goto out; /* success */
4a4d8108
AM
23424+
23425+ err = au_busy_or_stale();
4a4d8108 23426+ au_ren_unlock(a);
86dc4139 23427+
4f0767ce 23428+out:
4a4d8108 23429+ return err;
1facf9fc 23430+}
23431+
23432+/* ---------------------------------------------------------------------- */
23433+
4a4d8108 23434+static void au_ren_refresh_dir(struct au_ren_args *a)
1facf9fc 23435+{
4a4d8108 23436+ struct inode *dir;
dece6358 23437+
4a4d8108 23438+ dir = a->dst_dir;
be118d29 23439+ inode_inc_iversion(dir);
f2c43d5f 23440+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
4a4d8108
AM
23441+ /* is this updating defined in POSIX? */
23442+ au_cpup_attr_timesizes(a->src_inode);
23443+ au_cpup_attr_nlink(dir, /*force*/1);
4a4d8108 23444+ }
b912730e 23445+ au_dir_ts(dir, a->btgt);
dece6358 23446+
f2c43d5f
AM
23447+ if (a->exchange) {
23448+ dir = a->src_dir;
be118d29 23449+ inode_inc_iversion(dir);
f2c43d5f
AM
23450+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23451+ /* is this updating defined in POSIX? */
23452+ au_cpup_attr_timesizes(a->dst_inode);
23453+ au_cpup_attr_nlink(dir, /*force*/1);
23454+ }
23455+ au_dir_ts(dir, a->btgt);
23456+ }
23457+
23458+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108 23459+ return;
dece6358 23460+
4a4d8108 23461+ dir = a->src_dir;
be118d29 23462+ inode_inc_iversion(dir);
f2c43d5f 23463+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
4a4d8108 23464+ au_cpup_attr_nlink(dir, /*force*/1);
b912730e 23465+ au_dir_ts(dir, a->btgt);
1facf9fc 23466+}
23467+
4a4d8108 23468+static void au_ren_refresh(struct au_ren_args *a)
1facf9fc 23469+{
5afbbe0d 23470+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
23471+ struct dentry *d, *h_d;
23472+ struct inode *i, *h_i;
23473+ struct super_block *sb;
dece6358 23474+
027c5e7a
AM
23475+ d = a->dst_dentry;
23476+ d_drop(d);
23477+ if (a->h_dst)
23478+ /* already dget-ed by au_ren_or_cpup() */
23479+ au_set_h_dptr(d, a->btgt, a->h_dst);
23480+
23481+ i = a->dst_inode;
23482+ if (i) {
f2c43d5f
AM
23483+ if (!a->exchange) {
23484+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
23485+ vfsub_drop_nlink(i);
23486+ else {
23487+ vfsub_dead_dir(i);
23488+ au_cpup_attr_timesizes(i);
23489+ }
23490+ au_update_dbrange(d, /*do_put_zero*/1);
23491+ } else
23492+ au_cpup_attr_nlink(i, /*force*/1);
027c5e7a 23493+ } else {
5afbbe0d
AM
23494+ bbot = a->btgt;
23495+ for (bindex = au_dbtop(d); bindex < bbot; bindex++)
027c5e7a 23496+ au_set_h_dptr(d, bindex, NULL);
5afbbe0d
AM
23497+ bbot = au_dbbot(d);
23498+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
027c5e7a
AM
23499+ au_set_h_dptr(d, bindex, NULL);
23500+ au_update_dbrange(d, /*do_put_zero*/0);
23501+ }
23502+
8b6a4947
AM
23503+ if (a->exchange
23504+ || au_ftest_ren(a->auren_flags, DIRREN)) {
23505+ d_drop(a->src_dentry);
23506+ if (au_ftest_ren(a->auren_flags, DIRREN))
23507+ au_set_dbwh(a->src_dentry, -1);
23508+ return;
23509+ }
23510+
4a4d8108 23511+ d = a->src_dentry;
8b6a4947
AM
23512+ au_set_dbwh(d, -1);
23513+ bbot = au_dbbot(d);
23514+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23515+ h_d = au_h_dptr(d, bindex);
23516+ if (h_d)
23517+ au_set_h_dptr(d, bindex, NULL);
23518+ }
23519+ au_set_dbbot(d, a->btgt);
4a4d8108 23520+
8b6a4947
AM
23521+ sb = d->d_sb;
23522+ i = a->src_inode;
23523+ if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
23524+ return; /* success */
4a4d8108 23525+
8b6a4947
AM
23526+ bbot = au_ibbot(i);
23527+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23528+ h_i = au_h_iptr(i, bindex);
23529+ if (h_i) {
23530+ au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
23531+ /* ignore this error */
23532+ au_set_h_iptr(i, bindex, NULL, 0);
4a4d8108
AM
23533+ }
23534+ }
8b6a4947 23535+ au_set_ibbot(i, a->btgt);
1308ab2a 23536+}
dece6358 23537+
4a4d8108
AM
23538+/* ---------------------------------------------------------------------- */
23539+
23540+/* mainly for link(2) and rename(2) */
23541+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
1308ab2a 23542+{
4a4d8108
AM
23543+ aufs_bindex_t bdiropq, bwh;
23544+ struct dentry *parent;
23545+ struct au_branch *br;
23546+
23547+ parent = dentry->d_parent;
5527c038 23548+ IMustLock(d_inode(parent)); /* dir is locked */
4a4d8108
AM
23549+
23550+ bdiropq = au_dbdiropq(parent);
23551+ bwh = au_dbwh(dentry);
23552+ br = au_sbr(dentry->d_sb, btgt);
23553+ if (au_br_rdonly(br)
23554+ || (0 <= bdiropq && bdiropq < btgt)
23555+ || (0 <= bwh && bwh < btgt))
23556+ btgt = -1;
23557+
23558+ AuDbg("btgt %d\n", btgt);
23559+ return btgt;
1facf9fc 23560+}
23561+
5afbbe0d 23562+/* sets src_btop, dst_btop and btgt */
4a4d8108 23563+static int au_ren_wbr(struct au_ren_args *a)
1facf9fc 23564+{
4a4d8108
AM
23565+ int err;
23566+ struct au_wr_dir_args wr_dir_args = {
23567+ /* .force_btgt = -1, */
23568+ .flags = AuWrDir_ADD_ENTRY
23569+ };
dece6358 23570+
5afbbe0d
AM
23571+ a->src_btop = au_dbtop(a->src_dentry);
23572+ a->dst_btop = au_dbtop(a->dst_dentry);
f2c43d5f
AM
23573+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
23574+ || au_ftest_ren(a->auren_flags, ISDIR_DST))
4a4d8108 23575+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
5afbbe0d
AM
23576+ wr_dir_args.force_btgt = a->src_btop;
23577+ if (a->dst_inode && a->dst_btop < a->src_btop)
23578+ wr_dir_args.force_btgt = a->dst_btop;
4a4d8108
AM
23579+ wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
23580+ err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
23581+ a->btgt = err;
f2c43d5f
AM
23582+ if (a->exchange)
23583+ au_update_dbtop(a->dst_dentry);
dece6358 23584+
4a4d8108 23585+ return err;
1facf9fc 23586+}
23587+
4a4d8108 23588+static void au_ren_dt(struct au_ren_args *a)
1facf9fc 23589+{
4a4d8108
AM
23590+ a->h_path.dentry = a->src_h_parent;
23591+ au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
f2c43d5f 23592+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
4a4d8108
AM
23593+ a->h_path.dentry = a->dst_h_parent;
23594+ au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
23595+ }
1facf9fc 23596+
f2c43d5f
AM
23597+ au_fclr_ren(a->auren_flags, DT_DSTDIR);
23598+ if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
23599+ && !a->exchange)
4a4d8108 23600+ return;
dece6358 23601+
4a4d8108
AM
23602+ a->h_path.dentry = a->src_h_dentry;
23603+ au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
5527c038 23604+ if (d_is_positive(a->dst_h_dentry)) {
f2c43d5f 23605+ au_fset_ren(a->auren_flags, DT_DSTDIR);
4a4d8108
AM
23606+ a->h_path.dentry = a->dst_h_dentry;
23607+ au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
23608+ }
1308ab2a 23609+}
dece6358 23610+
4a4d8108 23611+static void au_ren_rev_dt(int err, struct au_ren_args *a)
1308ab2a 23612+{
4a4d8108 23613+ struct dentry *h_d;
febd17d6 23614+ struct inode *h_inode;
4a4d8108
AM
23615+
23616+ au_dtime_revert(a->src_dt + AuPARENT);
f2c43d5f 23617+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108
AM
23618+ au_dtime_revert(a->dst_dt + AuPARENT);
23619+
f2c43d5f 23620+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
4a4d8108 23621+ h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
23622+ h_inode = d_inode(h_d);
23623+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 23624+ au_dtime_revert(a->src_dt + AuCHILD);
febd17d6 23625+ inode_unlock(h_inode);
4a4d8108 23626+
f2c43d5f 23627+ if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
4a4d8108 23628+ h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
23629+ h_inode = d_inode(h_d);
23630+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 23631+ au_dtime_revert(a->dst_dt + AuCHILD);
febd17d6 23632+ inode_unlock(h_inode);
1facf9fc 23633+ }
23634+ }
23635+}
23636+
4a4d8108
AM
23637+/* ---------------------------------------------------------------------- */
23638+
23639+int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
f2c43d5f
AM
23640+ struct inode *_dst_dir, struct dentry *_dst_dentry,
23641+ unsigned int _flags)
1facf9fc 23642+{
f2c43d5f 23643+ int err, lock_flags;
8b6a4947 23644+ void *rev;
4a4d8108
AM
23645+ /* reduce stack space */
23646+ struct au_ren_args *a;
f2c43d5f 23647+ struct au_pin pin;
4a4d8108 23648+
f2c43d5f 23649+ AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
4a4d8108
AM
23650+ IMustLock(_src_dir);
23651+ IMustLock(_dst_dir);
23652+
f2c43d5f
AM
23653+ err = -EINVAL;
23654+ if (unlikely(_flags & RENAME_WHITEOUT))
23655+ goto out;
23656+
4a4d8108
AM
23657+ err = -ENOMEM;
23658+ BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
23659+ a = kzalloc(sizeof(*a), GFP_NOFS);
23660+ if (unlikely(!a))
23661+ goto out;
23662+
f2c43d5f 23663+ a->flags = _flags;
acd2b654
AM
23664+ BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
23665+ && RENAME_EXCHANGE > U8_MAX);
f2c43d5f 23666+ a->exchange = _flags & RENAME_EXCHANGE;
4a4d8108
AM
23667+ a->src_dir = _src_dir;
23668+ a->src_dentry = _src_dentry;
5527c038
JR
23669+ a->src_inode = NULL;
23670+ if (d_really_is_positive(a->src_dentry))
23671+ a->src_inode = d_inode(a->src_dentry);
4a4d8108
AM
23672+ a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
23673+ a->dst_dir = _dst_dir;
23674+ a->dst_dentry = _dst_dentry;
5527c038
JR
23675+ a->dst_inode = NULL;
23676+ if (d_really_is_positive(a->dst_dentry))
23677+ a->dst_inode = d_inode(a->dst_dentry);
4a4d8108
AM
23678+ a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
23679+ if (a->dst_inode) {
f2c43d5f
AM
23680+ /*
23681+ * if EXCHANGE && src is non-dir && dst is dir,
23682+ * dst is not locked.
23683+ */
23684+ /* IMustLock(a->dst_inode); */
4a4d8108 23685+ au_igrab(a->dst_inode);
1facf9fc 23686+ }
1facf9fc 23687+
4a4d8108 23688+ err = -ENOTDIR;
f2c43d5f 23689+ lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
2000de60 23690+ if (d_is_dir(a->src_dentry)) {
f2c43d5f
AM
23691+ au_fset_ren(a->auren_flags, ISDIR_SRC);
23692+ if (unlikely(!a->exchange
23693+ && d_really_is_positive(a->dst_dentry)
2000de60 23694+ && !d_is_dir(a->dst_dentry)))
4a4d8108 23695+ goto out_free;
f2c43d5f
AM
23696+ lock_flags |= AuLock_DIRS;
23697+ }
23698+ if (a->dst_inode && d_is_dir(a->dst_dentry)) {
23699+ au_fset_ren(a->auren_flags, ISDIR_DST);
23700+ if (unlikely(!a->exchange
23701+ && d_really_is_positive(a->src_dentry)
23702+ && !d_is_dir(a->src_dentry)))
23703+ goto out_free;
23704+ lock_flags |= AuLock_DIRS;
b95c5147 23705+ }
8b6a4947
AM
23706+ err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
23707+ lock_flags);
e49829fe
JR
23708+ if (unlikely(err))
23709+ goto out_free;
1facf9fc 23710+
027c5e7a
AM
23711+ err = au_d_hashed_positive(a->src_dentry);
23712+ if (unlikely(err))
23713+ goto out_unlock;
23714+ err = -ENOENT;
23715+ if (a->dst_inode) {
23716+ /*
f2c43d5f 23717+ * If it is a dir, VFS unhash it before this
027c5e7a
AM
23718+ * function. It means we cannot rely upon d_unhashed().
23719+ */
23720+ if (unlikely(!a->dst_inode->i_nlink))
23721+ goto out_unlock;
f2c43d5f 23722+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
027c5e7a 23723+ err = au_d_hashed_positive(a->dst_dentry);
f2c43d5f 23724+ if (unlikely(err && !a->exchange))
027c5e7a
AM
23725+ goto out_unlock;
23726+ } else if (unlikely(IS_DEADDIR(a->dst_inode)))
23727+ goto out_unlock;
23728+ } else if (unlikely(d_unhashed(a->dst_dentry)))
23729+ goto out_unlock;
23730+
7eafdf33
AM
23731+ /*
23732+ * is it possible?
79b8bda9 23733+ * yes, it happened (in linux-3.3-rcN) but I don't know why.
7eafdf33
AM
23734+ * there may exist a problem somewhere else.
23735+ */
23736+ err = -EINVAL;
5527c038 23737+ if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
7eafdf33
AM
23738+ goto out_unlock;
23739+
f2c43d5f 23740+ au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
4a4d8108 23741+ di_write_lock_parent(a->dst_parent);
1facf9fc 23742+
4a4d8108
AM
23743+ /* which branch we process */
23744+ err = au_ren_wbr(a);
23745+ if (unlikely(err < 0))
027c5e7a 23746+ goto out_parent;
4a4d8108 23747+ a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
86dc4139 23748+ a->h_path.mnt = au_br_mnt(a->br);
1facf9fc 23749+
4a4d8108
AM
23750+ /* are they available to be renamed */
23751+ err = au_ren_may_dir(a);
23752+ if (unlikely(err))
23753+ goto out_children;
1facf9fc 23754+
4a4d8108 23755+ /* prepare the writable parent dir on the same branch */
5afbbe0d 23756+ if (a->dst_btop == a->btgt) {
f2c43d5f 23757+ au_fset_ren(a->auren_flags, WHDST);
4a4d8108
AM
23758+ } else {
23759+ err = au_cpup_dirs(a->dst_dentry, a->btgt);
23760+ if (unlikely(err))
23761+ goto out_children;
23762+ }
1facf9fc 23763+
f2c43d5f
AM
23764+ err = 0;
23765+ if (!a->exchange) {
23766+ if (a->src_dir != a->dst_dir) {
23767+ /*
23768+ * this temporary unlock is safe,
23769+ * because both dir->i_mutex are locked.
23770+ */
23771+ di_write_unlock(a->dst_parent);
23772+ di_write_lock_parent(a->src_parent);
23773+ err = au_wr_dir_need_wh(a->src_dentry,
23774+ au_ftest_ren(a->auren_flags,
23775+ ISDIR_SRC),
23776+ &a->btgt);
23777+ di_write_unlock(a->src_parent);
23778+ di_write_lock2_parent(a->src_parent, a->dst_parent,
23779+ /*isdir*/1);
23780+ au_fclr_ren(a->auren_flags, ISSAMEDIR);
23781+ } else
23782+ err = au_wr_dir_need_wh(a->src_dentry,
23783+ au_ftest_ren(a->auren_flags,
23784+ ISDIR_SRC),
23785+ &a->btgt);
23786+ }
4a4d8108
AM
23787+ if (unlikely(err < 0))
23788+ goto out_children;
23789+ if (err)
f2c43d5f 23790+ au_fset_ren(a->auren_flags, WHSRC);
1facf9fc 23791+
86dc4139 23792+ /* cpup src */
5afbbe0d 23793+ if (a->src_btop != a->btgt) {
86dc4139
AM
23794+ err = au_pin(&pin, a->src_dentry, a->btgt,
23795+ au_opt_udba(a->src_dentry->d_sb),
23796+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 23797+ if (!err) {
c2b27bf2
AM
23798+ struct au_cp_generic cpg = {
23799+ .dentry = a->src_dentry,
23800+ .bdst = a->btgt,
5afbbe0d 23801+ .bsrc = a->src_btop,
c2b27bf2
AM
23802+ .len = -1,
23803+ .pin = &pin,
23804+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23805+ };
5afbbe0d 23806+ AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
c2b27bf2 23807+ err = au_sio_cpup_simple(&cpg);
367653fa 23808+ au_unpin(&pin);
86dc4139 23809+ }
86dc4139
AM
23810+ if (unlikely(err))
23811+ goto out_children;
5afbbe0d 23812+ a->src_btop = a->btgt;
86dc4139 23813+ a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
f2c43d5f
AM
23814+ if (!a->exchange)
23815+ au_fset_ren(a->auren_flags, WHSRC);
23816+ }
23817+
23818+ /* cpup dst */
23819+ if (a->exchange && a->dst_inode
23820+ && a->dst_btop != a->btgt) {
23821+ err = au_pin(&pin, a->dst_dentry, a->btgt,
23822+ au_opt_udba(a->dst_dentry->d_sb),
23823+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23824+ if (!err) {
23825+ struct au_cp_generic cpg = {
23826+ .dentry = a->dst_dentry,
23827+ .bdst = a->btgt,
23828+ .bsrc = a->dst_btop,
23829+ .len = -1,
23830+ .pin = &pin,
23831+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23832+ };
23833+ err = au_sio_cpup_simple(&cpg);
23834+ au_unpin(&pin);
23835+ }
23836+ if (unlikely(err))
23837+ goto out_children;
23838+ a->dst_btop = a->btgt;
23839+ a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
86dc4139
AM
23840+ }
23841+
4a4d8108
AM
23842+ /* lock them all */
23843+ err = au_ren_lock(a);
23844+ if (unlikely(err))
86dc4139 23845+ /* leave the copied-up one */
4a4d8108 23846+ goto out_children;
1facf9fc 23847+
f2c43d5f
AM
23848+ if (!a->exchange) {
23849+ if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
23850+ err = au_may_ren(a);
23851+ else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
23852+ err = -ENAMETOOLONG;
23853+ if (unlikely(err))
23854+ goto out_hdir;
23855+ }
1facf9fc 23856+
4a4d8108
AM
23857+ /* store timestamps to be revertible */
23858+ au_ren_dt(a);
1facf9fc 23859+
8b6a4947
AM
23860+ /* store dirren info */
23861+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23862+ err = au_dr_rename(a->src_dentry, a->btgt,
23863+ &a->dst_dentry->d_name, &rev);
23864+ AuTraceErr(err);
23865+ if (unlikely(err))
23866+ goto out_dt;
23867+ }
23868+
4a4d8108
AM
23869+ /* here we go */
23870+ err = do_rename(a);
23871+ if (unlikely(err))
8b6a4947
AM
23872+ goto out_dirren;
23873+
23874+ if (au_ftest_ren(a->auren_flags, DIRREN))
23875+ au_dr_rename_fin(a->src_dentry, a->btgt, rev);
4a4d8108
AM
23876+
23877+ /* update dir attributes */
23878+ au_ren_refresh_dir(a);
23879+
23880+ /* dput/iput all lower dentries */
23881+ au_ren_refresh(a);
23882+
23883+ goto out_hdir; /* success */
23884+
8b6a4947
AM
23885+out_dirren:
23886+ if (au_ftest_ren(a->auren_flags, DIRREN))
23887+ au_dr_rename_rev(a->src_dentry, a->btgt, rev);
4f0767ce 23888+out_dt:
4a4d8108 23889+ au_ren_rev_dt(err, a);
4f0767ce 23890+out_hdir:
4a4d8108 23891+ au_ren_unlock(a);
4f0767ce 23892+out_children:
4a4d8108 23893+ au_nhash_wh_free(&a->whlist);
5afbbe0d
AM
23894+ if (err && a->dst_inode && a->dst_btop != a->btgt) {
23895+ AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
027c5e7a 23896+ au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
5afbbe0d 23897+ au_set_dbtop(a->dst_dentry, a->dst_btop);
4a4d8108 23898+ }
027c5e7a 23899+out_parent:
f2c43d5f 23900+ if (!err) {
8b6a4947
AM
23901+ if (d_unhashed(a->src_dentry))
23902+ au_fset_ren(a->auren_flags, DROPPED_SRC);
23903+ if (d_unhashed(a->dst_dentry))
23904+ au_fset_ren(a->auren_flags, DROPPED_DST);
f2c43d5f
AM
23905+ if (!a->exchange)
23906+ d_move(a->src_dentry, a->dst_dentry);
8b6a4947 23907+ else {
f2c43d5f 23908+ d_exchange(a->src_dentry, a->dst_dentry);
8b6a4947
AM
23909+ if (au_ftest_ren(a->auren_flags, DROPPED_DST))
23910+ d_drop(a->dst_dentry);
23911+ }
23912+ if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
23913+ d_drop(a->src_dentry);
f2c43d5f 23914+ } else {
5afbbe0d 23915+ au_update_dbtop(a->dst_dentry);
027c5e7a
AM
23916+ if (!a->dst_inode)
23917+ d_drop(a->dst_dentry);
23918+ }
f2c43d5f 23919+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108
AM
23920+ di_write_unlock(a->dst_parent);
23921+ else
23922+ di_write_unlock2(a->src_parent, a->dst_parent);
027c5e7a 23923+out_unlock:
4a4d8108 23924+ aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
4f0767ce 23925+out_free:
4a4d8108
AM
23926+ iput(a->dst_inode);
23927+ if (a->thargs)
23928+ au_whtmp_rmdir_free(a->thargs);
e49925d1 23929+ au_kfree_rcu(a);
4f0767ce 23930+out:
4a4d8108
AM
23931+ AuTraceErr(err);
23932+ return err;
1308ab2a 23933+}
7f207e10
AM
23934diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
23935--- /usr/share/empty/fs/aufs/Kconfig 1970-01-01 01:00:00.000000000 +0100
e49925d1 23936+++ linux/fs/aufs/Kconfig 2019-01-28 14:36:12.219084659 +0100
2121bcd9
AM
23937@@ -0,0 +1,199 @@
23938+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
23939+config AUFS_FS
23940+ tristate "Aufs (Advanced multi layered unification filesystem) support"
4a4d8108
AM
23941+ help
23942+ Aufs is a stackable unification filesystem such as Unionfs,
23943+ which unifies several directories and provides a merged single
23944+ directory.
23945+ In the early days, aufs was entirely re-designed and
23946+ re-implemented Unionfs Version 1.x series. Introducing many
23947+ original ideas, approaches and improvements, it becomes totally
23948+ different from Unionfs while keeping the basic features.
1facf9fc 23949+
4a4d8108
AM
23950+if AUFS_FS
23951+choice
23952+ prompt "Maximum number of branches"
23953+ default AUFS_BRANCH_MAX_127
23954+ help
23955+ Specifies the maximum number of branches (or member directories)
23956+ in a single aufs. The larger value consumes more system
23957+ resources and has a minor impact to performance.
23958+config AUFS_BRANCH_MAX_127
23959+ bool "127"
23960+ help
23961+ Specifies the maximum number of branches (or member directories)
23962+ in a single aufs. The larger value consumes more system
23963+ resources and has a minor impact to performance.
23964+config AUFS_BRANCH_MAX_511
23965+ bool "511"
23966+ help
23967+ Specifies the maximum number of branches (or member directories)
23968+ in a single aufs. The larger value consumes more system
23969+ resources and has a minor impact to performance.
23970+config AUFS_BRANCH_MAX_1023
23971+ bool "1023"
23972+ help
23973+ Specifies the maximum number of branches (or member directories)
23974+ in a single aufs. The larger value consumes more system
23975+ resources and has a minor impact to performance.
23976+config AUFS_BRANCH_MAX_32767
23977+ bool "32767"
23978+ help
23979+ Specifies the maximum number of branches (or member directories)
23980+ in a single aufs. The larger value consumes more system
23981+ resources and has a minor impact to performance.
23982+endchoice
1facf9fc 23983+
e49829fe
JR
23984+config AUFS_SBILIST
23985+ bool
23986+ depends on AUFS_MAGIC_SYSRQ || PROC_FS
23987+ default y
23988+ help
23989+ Automatic configuration for internal use.
23990+ When aufs supports Magic SysRq or /proc, enabled automatically.
23991+
4a4d8108
AM
23992+config AUFS_HNOTIFY
23993+ bool "Detect direct branch access (bypassing aufs)"
23994+ help
23995+ If you want to modify files on branches directly, eg. bypassing aufs,
23996+ and want aufs to detect the changes of them fully, then enable this
23997+ option and use 'udba=notify' mount option.
7f207e10 23998+ Currently there is only one available configuration, "fsnotify".
4a4d8108
AM
23999+ It will have a negative impact to the performance.
24000+ See detail in aufs.5.
dece6358 24001+
4a4d8108
AM
24002+choice
24003+ prompt "method" if AUFS_HNOTIFY
24004+ default AUFS_HFSNOTIFY
24005+config AUFS_HFSNOTIFY
24006+ bool "fsnotify"
24007+ select FSNOTIFY
4a4d8108 24008+endchoice
1facf9fc 24009+
4a4d8108
AM
24010+config AUFS_EXPORT
24011+ bool "NFS-exportable aufs"
2cbb1c4b 24012+ depends on EXPORTFS
4a4d8108
AM
24013+ help
24014+ If you want to export your mounted aufs via NFS, then enable this
24015+ option. There are several requirements for this configuration.
24016+ See detail in aufs.5.
1facf9fc 24017+
4a4d8108
AM
24018+config AUFS_INO_T_64
24019+ bool
24020+ depends on AUFS_EXPORT
24021+ depends on 64BIT && !(ALPHA || S390)
24022+ default y
24023+ help
24024+ Automatic configuration for internal use.
24025+ /* typedef unsigned long/int __kernel_ino_t */
24026+ /* alpha and s390x are int */
1facf9fc 24027+
c1595e42
JR
24028+config AUFS_XATTR
24029+ bool "support for XATTR/EA (including Security Labels)"
24030+ help
24031+ If your branch fs supports XATTR/EA and you want to make them
24032+ available in aufs too, then enable this opsion and specify the
24033+ branch attributes for EA.
24034+ See detail in aufs.5.
24035+
076b876e
AM
24036+config AUFS_FHSM
24037+ bool "File-based Hierarchical Storage Management"
24038+ help
24039+ Hierarchical Storage Management (or HSM) is a well-known feature
24040+ in the storage world. Aufs provides this feature as file-based.
24041+ with multiple branches.
24042+ These multiple branches are prioritized, ie. the topmost one
24043+ should be the fastest drive and be used heavily.
24044+
4a4d8108
AM
24045+config AUFS_RDU
24046+ bool "Readdir in userspace"
24047+ help
24048+ Aufs has two methods to provide a merged view for a directory,
24049+ by a user-space library and by kernel-space natively. The latter
24050+ is always enabled but sometimes large and slow.
24051+ If you enable this option, install the library in aufs2-util
24052+ package, and set some environment variables for your readdir(3),
24053+ then the work will be handled in user-space which generally
24054+ shows better performance in most cases.
24055+ See detail in aufs.5.
1facf9fc 24056+
8b6a4947
AM
24057+config AUFS_DIRREN
24058+ bool "Workaround for rename(2)-ing a directory"
24059+ help
24060+ By default, aufs returns EXDEV error in renameing a dir who has
24061+ his child on the lower branch, since it is a bad idea to issue
24062+ rename(2) internally for every lower branch. But user may not
24063+ accept this behaviour. So here is a workaround to allow such
24064+ rename(2) and store some extra infromation on the writable
24065+ branch. Obviously this costs high (and I don't like it).
24066+ To use this feature, you need to enable this configuration AND
24067+ to specify the mount option `dirren.'
24068+ See details in aufs.5 and the design documents.
24069+
4a4d8108
AM
24070+config AUFS_SHWH
24071+ bool "Show whiteouts"
24072+ help
24073+ If you want to make the whiteouts in aufs visible, then enable
24074+ this option and specify 'shwh' mount option. Although it may
24075+ sounds like philosophy or something, but in technically it
24076+ simply shows the name of whiteout with keeping its behaviour.
1facf9fc 24077+
4a4d8108
AM
24078+config AUFS_BR_RAMFS
24079+ bool "Ramfs (initramfs/rootfs) as an aufs branch"
24080+ help
24081+ If you want to use ramfs as an aufs branch fs, then enable this
24082+ option. Generally tmpfs is recommended.
24083+ Aufs prohibited them to be a branch fs by default, because
24084+ initramfs becomes unusable after switch_root or something
24085+ generally. If you sets initramfs as an aufs branch and boot your
24086+ system by switch_root, you will meet a problem easily since the
24087+ files in initramfs may be inaccessible.
24088+ Unless you are going to use ramfs as an aufs branch fs without
24089+ switch_root or something, leave it N.
1facf9fc 24090+
4a4d8108
AM
24091+config AUFS_BR_FUSE
24092+ bool "Fuse fs as an aufs branch"
24093+ depends on FUSE_FS
24094+ select AUFS_POLL
24095+ help
24096+ If you want to use fuse-based userspace filesystem as an aufs
24097+ branch fs, then enable this option.
24098+ It implements the internal poll(2) operation which is
24099+ implemented by fuse only (curretnly).
1facf9fc 24100+
4a4d8108
AM
24101+config AUFS_POLL
24102+ bool
24103+ help
24104+ Automatic configuration for internal use.
1facf9fc 24105+
4a4d8108
AM
24106+config AUFS_BR_HFSPLUS
24107+ bool "Hfsplus as an aufs branch"
24108+ depends on HFSPLUS_FS
24109+ default y
24110+ help
24111+ If you want to use hfsplus fs as an aufs branch fs, then enable
24112+ this option. This option introduces a small overhead at
24113+ copying-up a file on hfsplus.
1facf9fc 24114+
4a4d8108
AM
24115+config AUFS_BDEV_LOOP
24116+ bool
24117+ depends on BLK_DEV_LOOP
24118+ default y
24119+ help
24120+ Automatic configuration for internal use.
24121+ Convert =[ym] into =y.
1308ab2a 24122+
4a4d8108
AM
24123+config AUFS_DEBUG
24124+ bool "Debug aufs"
24125+ help
24126+ Enable this to compile aufs internal debug code.
24127+ It will have a negative impact to the performance.
24128+
24129+config AUFS_MAGIC_SYSRQ
24130+ bool
24131+ depends on AUFS_DEBUG && MAGIC_SYSRQ
24132+ default y
24133+ help
24134+ Automatic configuration for internal use.
24135+ When aufs supports Magic SysRq, enabled automatically.
24136+endif
acd2b654
AM
24137diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
24138--- /usr/share/empty/fs/aufs/lcnt.h 1970-01-01 01:00:00.000000000 +0100
e49925d1 24139+++ linux/fs/aufs/lcnt.h 2019-01-28 14:36:12.222418047 +0100
acd2b654
AM
24140@@ -0,0 +1,186 @@
24141+/* SPDX-License-Identifier: GPL-2.0 */
24142+/*
24143+ * Copyright (C) 2018 Junjiro R. Okajima
24144+ *
24145+ * This program, aufs is free software; you can redistribute it and/or modify
24146+ * it under the terms of the GNU General Public License as published by
24147+ * the Free Software Foundation; either version 2 of the License, or
24148+ * (at your option) any later version.
24149+ *
24150+ * This program is distributed in the hope that it will be useful,
24151+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24152+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24153+ * GNU General Public License for more details.
24154+ *
24155+ * You should have received a copy of the GNU General Public License
24156+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
24157+ */
24158+
24159+/*
24160+ * simple long counter wrapper
24161+ */
24162+
24163+#ifndef __AUFS_LCNT_H__
24164+#define __AUFS_LCNT_H__
24165+
24166+#ifdef __KERNEL__
24167+
24168+#include "debug.h"
24169+
24170+#define AuLCntATOMIC 1
24171+#define AuLCntPCPUCNT 2
24172+/*
24173+ * why does percpu_refcount require extra synchronize_rcu()s in
24174+ * au_br_do_free()
24175+ */
24176+#define AuLCntPCPUREF 3
24177+
24178+/* #define AuLCntChosen AuLCntATOMIC */
24179+#define AuLCntChosen AuLCntPCPUCNT
24180+/* #define AuLCntChosen AuLCntPCPUREF */
24181+
24182+#if AuLCntChosen == AuLCntATOMIC
24183+#include <linux/atomic.h>
24184+
24185+typedef atomic_long_t au_lcnt_t;
24186+
24187+static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24188+{
24189+ atomic_long_set(cnt, 0);
24190+ return 0;
24191+}
24192+
24193+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24194+{
24195+ /* empty */
24196+}
24197+
24198+static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
24199+ int do_sync __maybe_unused)
24200+{
24201+ /* empty */
24202+}
24203+
24204+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24205+{
24206+ atomic_long_inc(cnt);
24207+}
24208+
24209+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24210+{
24211+ atomic_long_dec(cnt);
24212+}
24213+
24214+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24215+{
24216+ return atomic_long_read(cnt);
24217+}
24218+#endif
24219+
24220+#if AuLCntChosen == AuLCntPCPUCNT
24221+#include <linux/percpu_counter.h>
24222+
24223+typedef struct percpu_counter au_lcnt_t;
24224+
24225+static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24226+{
24227+ return percpu_counter_init(cnt, 0, GFP_NOFS);
24228+}
24229+
24230+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24231+{
24232+ /* empty */
24233+}
24234+
24235+static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
24236+{
24237+ percpu_counter_destroy(cnt);
24238+}
24239+
24240+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24241+{
24242+ percpu_counter_inc(cnt);
24243+}
24244+
24245+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24246+{
24247+ percpu_counter_dec(cnt);
24248+}
24249+
24250+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24251+{
24252+ s64 n;
24253+
24254+ n = percpu_counter_sum(cnt);
24255+ BUG_ON(n < 0);
24256+ if (LONG_MAX != LLONG_MAX
24257+ && n > LONG_MAX)
24258+ AuWarn1("%s\n", "wrap-around");
24259+
24260+ return n;
24261+}
24262+#endif
24263+
24264+#if AuLCntChosen == AuLCntPCPUREF
24265+#include <linux/percpu-refcount.h>
24266+
24267+typedef struct percpu_ref au_lcnt_t;
24268+
24269+static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
24270+{
24271+ if (!release)
24272+ release = percpu_ref_exit;
24273+ return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
24274+}
24275+
24276+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24277+{
24278+ synchronize_rcu();
24279+}
24280+
24281+static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
24282+{
24283+ percpu_ref_kill(cnt);
24284+ if (do_sync)
24285+ au_lcnt_wait_for_fin(cnt);
24286+}
24287+
24288+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24289+{
24290+ percpu_ref_get(cnt);
24291+}
24292+
24293+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24294+{
24295+ percpu_ref_put(cnt);
24296+}
24297+
24298+/*
24299+ * avoid calling this func as possible.
24300+ */
24301+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
24302+{
24303+ long l;
24304+
24305+ percpu_ref_switch_to_atomic_sync(cnt);
24306+ l = atomic_long_read(&cnt->count);
24307+ if (do_rev)
24308+ percpu_ref_switch_to_percpu(cnt);
24309+
24310+ /* percpu_ref is initialized by 1 instead of 0 */
24311+ return l - 1;
24312+}
24313+#endif
24314+
24315+#ifdef CONFIG_AUFS_DEBUG
24316+#define AuLCntZero(val) do { \
24317+ long l = val; \
24318+ if (l) \
24319+ AuDbg("%s = %ld\n", #val, l); \
24320+} while (0)
24321+#else
24322+#define AuLCntZero(val) do {} while (0)
24323+#endif
24324+
24325+#endif /* __KERNEL__ */
24326+#endif /* __AUFS_LCNT_H__ */
7f207e10
AM
24327diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
24328--- /usr/share/empty/fs/aufs/loop.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 24329+++ linux/fs/aufs/loop.c 2019-01-28 14:36:12.222418047 +0100
062440b3 24330@@ -0,0 +1,148 @@
cd7a4cd9 24331+// SPDX-License-Identifier: GPL-2.0
1facf9fc 24332+/*
b00004a5 24333+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 24334+ *
24335+ * This program, aufs is free software; you can redistribute it and/or modify
24336+ * it under the terms of the GNU General Public License as published by
24337+ * the Free Software Foundation; either version 2 of the License, or
24338+ * (at your option) any later version.
dece6358
AM
24339+ *
24340+ * This program is distributed in the hope that it will be useful,
24341+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24342+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24343+ * GNU General Public License for more details.
24344+ *
24345+ * You should have received a copy of the GNU General Public License
523b37e3 24346+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24347+ */
24348+
24349+/*
24350+ * support for loopback block device as a branch
24351+ */
24352+
1facf9fc 24353+#include "aufs.h"
24354+
392086de
AM
24355+/* added into drivers/block/loop.c */
24356+static struct file *(*backing_file_func)(struct super_block *sb);
24357+
1facf9fc 24358+/*
24359+ * test if two lower dentries have overlapping branches.
24360+ */
b752ccd1 24361+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
1facf9fc 24362+{
b752ccd1 24363+ struct super_block *h_sb;
392086de
AM
24364+ struct file *backing_file;
24365+
24366+ if (unlikely(!backing_file_func)) {
24367+ /* don't load "loop" module here */
24368+ backing_file_func = symbol_get(loop_backing_file);
24369+ if (unlikely(!backing_file_func))
24370+ /* "loop" module is not loaded */
24371+ return 0;
24372+ }
1facf9fc 24373+
b752ccd1 24374+ h_sb = h_adding->d_sb;
392086de
AM
24375+ backing_file = backing_file_func(h_sb);
24376+ if (!backing_file)
1facf9fc 24377+ return 0;
24378+
2000de60 24379+ h_adding = backing_file->f_path.dentry;
b752ccd1
AM
24380+ /*
24381+ * h_adding can be local NFS.
24382+ * in this case aufs cannot detect the loop.
24383+ */
24384+ if (unlikely(h_adding->d_sb == sb))
1facf9fc 24385+ return 1;
b752ccd1 24386+ return !!au_test_subdir(h_adding, sb->s_root);
1facf9fc 24387+}
24388+
24389+/* true if a kernel thread named 'loop[0-9].*' accesses a file */
24390+int au_test_loopback_kthread(void)
24391+{
b752ccd1
AM
24392+ int ret;
24393+ struct task_struct *tsk = current;
a2a7ad62 24394+ char c, comm[sizeof(tsk->comm)];
b752ccd1
AM
24395+
24396+ ret = 0;
24397+ if (tsk->flags & PF_KTHREAD) {
a2a7ad62
AM
24398+ get_task_comm(comm, tsk);
24399+ c = comm[4];
b752ccd1 24400+ ret = ('0' <= c && c <= '9'
a2a7ad62 24401+ && !strncmp(comm, "loop", 4));
b752ccd1 24402+ }
1facf9fc 24403+
b752ccd1 24404+ return ret;
1facf9fc 24405+}
87a755f4
AM
24406+
24407+/* ---------------------------------------------------------------------- */
24408+
24409+#define au_warn_loopback_step 16
24410+static int au_warn_loopback_nelem = au_warn_loopback_step;
24411+static unsigned long *au_warn_loopback_array;
24412+
24413+void au_warn_loopback(struct super_block *h_sb)
24414+{
24415+ int i, new_nelem;
24416+ unsigned long *a, magic;
24417+ static DEFINE_SPINLOCK(spin);
24418+
24419+ magic = h_sb->s_magic;
24420+ spin_lock(&spin);
24421+ a = au_warn_loopback_array;
24422+ for (i = 0; i < au_warn_loopback_nelem && *a; i++)
24423+ if (a[i] == magic) {
24424+ spin_unlock(&spin);
24425+ return;
24426+ }
24427+
24428+ /* h_sb is new to us, print it */
24429+ if (i < au_warn_loopback_nelem) {
24430+ a[i] = magic;
24431+ goto pr;
24432+ }
24433+
24434+ /* expand the array */
24435+ new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
24436+ a = au_kzrealloc(au_warn_loopback_array,
24437+ au_warn_loopback_nelem * sizeof(unsigned long),
e2f27e51
AM
24438+ new_nelem * sizeof(unsigned long), GFP_ATOMIC,
24439+ /*may_shrink*/0);
87a755f4
AM
24440+ if (a) {
24441+ au_warn_loopback_nelem = new_nelem;
24442+ au_warn_loopback_array = a;
24443+ a[i] = magic;
24444+ goto pr;
24445+ }
24446+
24447+ spin_unlock(&spin);
24448+ AuWarn1("realloc failed, ignored\n");
24449+ return;
24450+
24451+pr:
24452+ spin_unlock(&spin);
0c3ec466
AM
24453+ pr_warn("you may want to try another patch for loopback file "
24454+ "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
87a755f4
AM
24455+}
24456+
24457+int au_loopback_init(void)
24458+{
24459+ int err;
24460+ struct super_block *sb __maybe_unused;
24461+
79b8bda9 24462+ BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(unsigned long));
87a755f4
AM
24463+
24464+ err = 0;
24465+ au_warn_loopback_array = kcalloc(au_warn_loopback_step,
24466+ sizeof(unsigned long), GFP_NOFS);
24467+ if (unlikely(!au_warn_loopback_array))
24468+ err = -ENOMEM;
24469+
24470+ return err;
24471+}
24472+
24473+void au_loopback_fin(void)
24474+{
79b8bda9
AM
24475+ if (backing_file_func)
24476+ symbol_put(loop_backing_file);
e49925d1 24477+ au_kfree_try_rcu(au_warn_loopback_array);
87a755f4 24478+}
7f207e10
AM
24479diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
24480--- /usr/share/empty/fs/aufs/loop.h 1970-01-01 01:00:00.000000000 +0100
e49925d1 24481+++ linux/fs/aufs/loop.h 2019-01-28 14:36:12.222418047 +0100
062440b3
AM
24482@@ -0,0 +1,53 @@
24483+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 24484+/*
b00004a5 24485+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 24486+ *
24487+ * This program, aufs is free software; you can redistribute it and/or modify
24488+ * it under the terms of the GNU General Public License as published by
24489+ * the Free Software Foundation; either version 2 of the License, or
24490+ * (at your option) any later version.
dece6358
AM
24491+ *
24492+ * This program is distributed in the hope that it will be useful,
24493+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24494+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24495+ * GNU General Public License for more details.
24496+ *
24497+ * You should have received a copy of the GNU General Public License
523b37e3 24498+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24499+ */
24500+
24501+/*
24502+ * support for loopback mount as a branch
24503+ */
24504+
24505+#ifndef __AUFS_LOOP_H__
24506+#define __AUFS_LOOP_H__
24507+
24508+#ifdef __KERNEL__
24509+
dece6358
AM
24510+struct dentry;
24511+struct super_block;
1facf9fc 24512+
24513+#ifdef CONFIG_AUFS_BDEV_LOOP
392086de
AM
24514+/* drivers/block/loop.c */
24515+struct file *loop_backing_file(struct super_block *sb);
24516+
1facf9fc 24517+/* loop.c */
b752ccd1 24518+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
1facf9fc 24519+int au_test_loopback_kthread(void);
87a755f4
AM
24520+void au_warn_loopback(struct super_block *h_sb);
24521+
24522+int au_loopback_init(void);
24523+void au_loopback_fin(void);
1facf9fc 24524+#else
4a4d8108 24525+AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
b752ccd1 24526+ struct dentry *h_adding)
4a4d8108 24527+AuStubInt0(au_test_loopback_kthread, void)
87a755f4
AM
24528+AuStubVoid(au_warn_loopback, struct super_block *h_sb)
24529+
24530+AuStubInt0(au_loopback_init, void)
24531+AuStubVoid(au_loopback_fin, void)
1facf9fc 24532+#endif /* BLK_DEV_LOOP */
24533+
24534+#endif /* __KERNEL__ */
24535+#endif /* __AUFS_LOOP_H__ */
7f207e10
AM
24536diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
24537--- /usr/share/empty/fs/aufs/magic.mk 1970-01-01 01:00:00.000000000 +0100
e49925d1 24538+++ linux/fs/aufs/magic.mk 2019-01-28 14:36:12.222418047 +0100
2121bcd9
AM
24539@@ -0,0 +1,31 @@
24540+# SPDX-License-Identifier: GPL-2.0
1facf9fc 24541+
24542+# defined in ${srctree}/fs/fuse/inode.c
24543+# tristate
24544+ifdef CONFIG_FUSE_FS
24545+ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
24546+endif
24547+
1facf9fc 24548+# defined in ${srctree}/fs/xfs/xfs_sb.h
24549+# tristate
24550+ifdef CONFIG_XFS_FS
24551+ccflags-y += -DXFS_SB_MAGIC=0x58465342
24552+endif
24553+
24554+# defined in ${srctree}/fs/configfs/mount.c
24555+# tristate
24556+ifdef CONFIG_CONFIGFS_FS
24557+ccflags-y += -DCONFIGFS_MAGIC=0x62656570
24558+endif
24559+
1facf9fc 24560+# defined in ${srctree}/fs/ubifs/ubifs.h
24561+# tristate
24562+ifdef CONFIG_UBIFS_FS
24563+ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
24564+endif
4a4d8108
AM
24565+
24566+# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
24567+# tristate
24568+ifdef CONFIG_HFSPLUS_FS
24569+ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
24570+endif
7f207e10
AM
24571diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
24572--- /usr/share/empty/fs/aufs/Makefile 1970-01-01 01:00:00.000000000 +0100
e49925d1 24573+++ linux/fs/aufs/Makefile 2019-01-28 14:36:12.219084659 +0100
2121bcd9
AM
24574@@ -0,0 +1,46 @@
24575+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
24576+
24577+include ${src}/magic.mk
24578+ifeq (${CONFIG_AUFS_FS},m)
24579+include ${src}/conf.mk
24580+endif
24581+-include ${src}/priv_def.mk
24582+
24583+# cf. include/linux/kernel.h
24584+# enable pr_debug
24585+ccflags-y += -DDEBUG
f6c5ef8b
AM
24586+# sparse requires the full pathname
24587+ifdef M
523b37e3 24588+ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
f6c5ef8b 24589+else
523b37e3 24590+ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
f6c5ef8b 24591+endif
4a4d8108
AM
24592+
24593+obj-$(CONFIG_AUFS_FS) += aufs.o
24594+aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
24595+ wkq.o vfsub.o dcsub.o \
e49829fe 24596+ cpup.o whout.o wbr_policy.o \
4a4d8108
AM
24597+ dinfo.o dentry.o \
24598+ dynop.o \
24599+ finfo.o file.o f_op.o \
24600+ dir.o vdir.o \
24601+ iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
c2b27bf2 24602+ mvdown.o ioctl.o
4a4d8108
AM
24603+
24604+# all are boolean
e49829fe 24605+aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
4a4d8108
AM
24606+aufs-$(CONFIG_SYSFS) += sysfs.o
24607+aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
24608+aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
24609+aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
24610+aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
4a4d8108 24611+aufs-$(CONFIG_AUFS_EXPORT) += export.o
c1595e42
JR
24612+aufs-$(CONFIG_AUFS_XATTR) += xattr.o
24613+aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
8b6a4947 24614+aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
076b876e 24615+aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
4a4d8108
AM
24616+aufs-$(CONFIG_AUFS_POLL) += poll.o
24617+aufs-$(CONFIG_AUFS_RDU) += rdu.o
4a4d8108
AM
24618+aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
24619+aufs-$(CONFIG_AUFS_DEBUG) += debug.o
24620+aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
7f207e10
AM
24621diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
24622--- /usr/share/empty/fs/aufs/module.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 24623+++ linux/fs/aufs/module.c 2019-01-28 14:36:12.222418047 +0100
062440b3 24624@@ -0,0 +1,273 @@
cd7a4cd9 24625+// SPDX-License-Identifier: GPL-2.0
1facf9fc 24626+/*
b00004a5 24627+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 24628+ *
24629+ * This program, aufs is free software; you can redistribute it and/or modify
24630+ * it under the terms of the GNU General Public License as published by
24631+ * the Free Software Foundation; either version 2 of the License, or
24632+ * (at your option) any later version.
dece6358
AM
24633+ *
24634+ * This program is distributed in the hope that it will be useful,
24635+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24636+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24637+ * GNU General Public License for more details.
24638+ *
24639+ * You should have received a copy of the GNU General Public License
523b37e3 24640+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24641+ */
24642+
24643+/*
24644+ * module global variables and operations
24645+ */
24646+
24647+#include <linux/module.h>
24648+#include <linux/seq_file.h>
24649+#include "aufs.h"
24650+
e2f27e51
AM
24651+/* shrinkable realloc */
24652+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
1facf9fc 24653+{
e2f27e51
AM
24654+ size_t sz;
24655+ int diff;
1facf9fc 24656+
e2f27e51
AM
24657+ sz = 0;
24658+ diff = -1;
24659+ if (p) {
24660+#if 0 /* unused */
24661+ if (!new_sz) {
e49925d1 24662+ au_kfree_rcu(p);
e2f27e51
AM
24663+ p = NULL;
24664+ goto out;
24665+ }
24666+#else
24667+ AuDebugOn(!new_sz);
24668+#endif
24669+ sz = ksize(p);
24670+ diff = au_kmidx_sub(sz, new_sz);
24671+ }
24672+ if (sz && !diff)
24673+ goto out;
24674+
24675+ if (sz < new_sz)
24676+ /* expand or SLOB */
24677+ p = krealloc(p, new_sz, gfp);
24678+ else if (new_sz < sz && may_shrink) {
24679+ /* shrink */
24680+ void *q;
24681+
24682+ q = kmalloc(new_sz, gfp);
24683+ if (q) {
24684+ if (p) {
24685+ memcpy(q, p, new_sz);
e49925d1 24686+ au_kfree_try_rcu(p);
e2f27e51
AM
24687+ }
24688+ p = q;
24689+ } else
24690+ p = NULL;
24691+ }
24692+
24693+out:
24694+ return p;
24695+}
24696+
24697+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24698+ int may_shrink)
24699+{
24700+ p = au_krealloc(p, new_sz, gfp, may_shrink);
24701+ if (p && new_sz > nused)
1facf9fc 24702+ memset(p + nused, 0, new_sz - nused);
24703+ return p;
24704+}
24705+
24706+/* ---------------------------------------------------------------------- */
1facf9fc 24707+/*
24708+ * aufs caches
24709+ */
1c60b727 24710+struct kmem_cache *au_cache[AuCache_Last];
5afbbe0d
AM
24711+
24712+static void au_cache_fin(void)
24713+{
24714+ int i;
24715+
24716+ /*
24717+ * Make sure all delayed rcu free inodes are flushed before we
24718+ * destroy cache.
24719+ */
24720+ rcu_barrier();
24721+
24722+ /* excluding AuCache_HNOTIFY */
24723+ BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
24724+ for (i = 0; i < AuCache_HNOTIFY; i++) {
1c60b727
AM
24725+ kmem_cache_destroy(au_cache[i]);
24726+ au_cache[i] = NULL;
5afbbe0d
AM
24727+ }
24728+}
24729+
1facf9fc 24730+static int __init au_cache_init(void)
24731+{
1c60b727
AM
24732+ au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
24733+ if (au_cache[AuCache_DINFO])
027c5e7a 24734+ /* SLAB_DESTROY_BY_RCU */
1c60b727 24735+ au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
f0c0a007 24736+ au_icntnr_init_once);
1c60b727
AM
24737+ if (au_cache[AuCache_ICNTNR])
24738+ au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
f0c0a007 24739+ au_fi_init_once);
1c60b727
AM
24740+ if (au_cache[AuCache_FINFO])
24741+ au_cache[AuCache_VDIR] = AuCache(au_vdir);
24742+ if (au_cache[AuCache_VDIR])
24743+ au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
24744+ if (au_cache[AuCache_DEHSTR])
1facf9fc 24745+ return 0;
24746+
5afbbe0d 24747+ au_cache_fin();
1facf9fc 24748+ return -ENOMEM;
24749+}
24750+
1facf9fc 24751+/* ---------------------------------------------------------------------- */
24752+
24753+int au_dir_roflags;
24754+
e49829fe 24755+#ifdef CONFIG_AUFS_SBILIST
1e00d052
AM
24756+/*
24757+ * iterate_supers_type() doesn't protect us from
24758+ * remounting (branch management)
24759+ */
8b6a4947 24760+struct hlist_bl_head au_sbilist;
e49829fe
JR
24761+#endif
24762+
1facf9fc 24763+/*
24764+ * functions for module interface.
24765+ */
24766+MODULE_LICENSE("GPL");
24767+/* MODULE_LICENSE("GPL v2"); */
dece6358 24768+MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
1facf9fc 24769+MODULE_DESCRIPTION(AUFS_NAME
24770+ " -- Advanced multi layered unification filesystem");
24771+MODULE_VERSION(AUFS_VERSION);
c06a8ce3 24772+MODULE_ALIAS_FS(AUFS_NAME);
1facf9fc 24773+
1facf9fc 24774+/* this module parameter has no meaning when SYSFS is disabled */
24775+int sysaufs_brs = 1;
24776+MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
cd7a4cd9 24777+module_param_named(brs, sysaufs_brs, int, 0444);
1facf9fc 24778+
076b876e 24779+/* this module parameter has no meaning when USER_NS is disabled */
8cdd5066 24780+bool au_userns;
076b876e 24781+MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
cd7a4cd9 24782+module_param_named(allow_userns, au_userns, bool, 0444);
076b876e 24783+
1facf9fc 24784+/* ---------------------------------------------------------------------- */
24785+
24786+static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
24787+
24788+int au_seq_path(struct seq_file *seq, struct path *path)
24789+{
79b8bda9
AM
24790+ int err;
24791+
24792+ err = seq_path(seq, path, au_esc_chars);
1c60b727 24793+ if (err >= 0)
79b8bda9 24794+ err = 0;
1c60b727 24795+ else
79b8bda9
AM
24796+ err = -ENOMEM;
24797+
24798+ return err;
1facf9fc 24799+}
24800+
24801+/* ---------------------------------------------------------------------- */
24802+
24803+static int __init aufs_init(void)
24804+{
24805+ int err, i;
24806+ char *p;
24807+
24808+ p = au_esc_chars;
24809+ for (i = 1; i <= ' '; i++)
24810+ *p++ = i;
24811+ *p++ = '\\';
24812+ *p++ = '\x7f';
24813+ *p = 0;
24814+
24815+ au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
24816+
b95c5147
AM
24817+ memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
24818+ for (i = 0; i < AuIop_Last; i++)
24819+ aufs_iop_nogetattr[i].getattr = NULL;
24820+
1c60b727 24821+ memset(au_cache, 0, sizeof(au_cache)); /* including hnotify */
f0c0a007 24822+
e49829fe 24823+ au_sbilist_init();
1facf9fc 24824+ sysaufs_brs_init();
24825+ au_debug_init();
4a4d8108 24826+ au_dy_init();
1facf9fc 24827+ err = sysaufs_init();
24828+ if (unlikely(err))
24829+ goto out;
062440b3 24830+ err = dbgaufs_init();
4f0767ce 24831+ if (unlikely(err))
953406b4 24832+ goto out_sysaufs;
062440b3
AM
24833+ err = au_procfs_init();
24834+ if (unlikely(err))
24835+ goto out_dbgaufs;
e49829fe
JR
24836+ err = au_wkq_init();
24837+ if (unlikely(err))
24838+ goto out_procfs;
87a755f4 24839+ err = au_loopback_init();
1facf9fc 24840+ if (unlikely(err))
24841+ goto out_wkq;
87a755f4
AM
24842+ err = au_hnotify_init();
24843+ if (unlikely(err))
24844+ goto out_loopback;
1facf9fc 24845+ err = au_sysrq_init();
24846+ if (unlikely(err))
24847+ goto out_hin;
24848+ err = au_cache_init();
24849+ if (unlikely(err))
24850+ goto out_sysrq;
076b876e
AM
24851+
24852+ aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
1facf9fc 24853+ err = register_filesystem(&aufs_fs_type);
24854+ if (unlikely(err))
24855+ goto out_cache;
076b876e 24856+
4a4d8108
AM
24857+ /* since we define pr_fmt, call printk directly */
24858+ printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
1facf9fc 24859+ goto out; /* success */
24860+
4f0767ce 24861+out_cache:
1facf9fc 24862+ au_cache_fin();
4f0767ce 24863+out_sysrq:
1facf9fc 24864+ au_sysrq_fin();
4f0767ce 24865+out_hin:
4a4d8108 24866+ au_hnotify_fin();
87a755f4
AM
24867+out_loopback:
24868+ au_loopback_fin();
4f0767ce 24869+out_wkq:
1facf9fc 24870+ au_wkq_fin();
e49829fe
JR
24871+out_procfs:
24872+ au_procfs_fin();
062440b3
AM
24873+out_dbgaufs:
24874+ dbgaufs_fin();
4f0767ce 24875+out_sysaufs:
1facf9fc 24876+ sysaufs_fin();
4a4d8108 24877+ au_dy_fin();
4f0767ce 24878+out:
1facf9fc 24879+ return err;
24880+}
24881+
24882+static void __exit aufs_exit(void)
24883+{
24884+ unregister_filesystem(&aufs_fs_type);
24885+ au_cache_fin();
24886+ au_sysrq_fin();
4a4d8108 24887+ au_hnotify_fin();
87a755f4 24888+ au_loopback_fin();
1facf9fc 24889+ au_wkq_fin();
e49829fe 24890+ au_procfs_fin();
062440b3 24891+ dbgaufs_fin();
1facf9fc 24892+ sysaufs_fin();
4a4d8108 24893+ au_dy_fin();
1facf9fc 24894+}
24895+
24896+module_init(aufs_init);
24897+module_exit(aufs_exit);
7f207e10
AM
24898diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
24899--- /usr/share/empty/fs/aufs/module.h 1970-01-01 01:00:00.000000000 +0100
e49925d1
JR
24900+++ linux/fs/aufs/module.h 2019-01-28 14:36:12.222418047 +0100
24901@@ -0,0 +1,166 @@
062440b3 24902+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 24903+/*
b00004a5 24904+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 24905+ *
24906+ * This program, aufs is free software; you can redistribute it and/or modify
24907+ * it under the terms of the GNU General Public License as published by
24908+ * the Free Software Foundation; either version 2 of the License, or
24909+ * (at your option) any later version.
dece6358
AM
24910+ *
24911+ * This program is distributed in the hope that it will be useful,
24912+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24913+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24914+ * GNU General Public License for more details.
24915+ *
24916+ * You should have received a copy of the GNU General Public License
523b37e3 24917+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24918+ */
24919+
24920+/*
24921+ * module initialization and module-global
24922+ */
24923+
24924+#ifndef __AUFS_MODULE_H__
24925+#define __AUFS_MODULE_H__
24926+
24927+#ifdef __KERNEL__
24928+
24929+#include <linux/slab.h>
e49925d1
JR
24930+#include "debug.h"
24931+#include "dentry.h"
24932+#include "dir.h"
24933+#include "file.h"
24934+#include "inode.h"
1facf9fc 24935+
dece6358
AM
24936+struct path;
24937+struct seq_file;
24938+
1facf9fc 24939+/* module parameters */
1facf9fc 24940+extern int sysaufs_brs;
8cdd5066 24941+extern bool au_userns;
1facf9fc 24942+
24943+/* ---------------------------------------------------------------------- */
24944+
24945+extern int au_dir_roflags;
24946+
e2f27e51
AM
24947+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
24948+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24949+ int may_shrink);
24950+
e49925d1
JR
24951+/*
24952+ * Comparing the size of the object with sizeof(struct rcu_head)
24953+ * case 1: object is always larger
24954+ * --> au_kfree_rcu() or au_kfree_do_rcu()
24955+ * case 2: object is always smaller
24956+ * --> au_kfree_small()
24957+ * case 3: object can be any size
24958+ * --> au_kfree_try_rcu()
24959+ */
24960+
24961+static inline void au_kfree_do_rcu(const void *p)
24962+{
24963+ struct {
24964+ struct rcu_head rcu;
24965+ } *a = (void *)p;
24966+
24967+ kfree_rcu(a, rcu);
24968+}
24969+
24970+#define au_kfree_rcu(_p) do { \
24971+ typeof(_p) p = (_p); \
24972+ BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head)); \
24973+ if (p) \
24974+ au_kfree_do_rcu(p); \
24975+ } while (0)
24976+
24977+#define au_kfree_do_sz_test(sz) (sz >= sizeof(struct rcu_head))
24978+#define au_kfree_sz_test(p) (p && au_kfree_do_sz_test(ksize(p)))
24979+
24980+static inline void au_kfree_try_rcu(const void *p)
24981+{
24982+ if (!p)
24983+ return;
24984+ if (au_kfree_sz_test(p))
24985+ au_kfree_do_rcu(p);
24986+ else
24987+ kfree(p);
24988+}
24989+
24990+static inline void au_kfree_small(const void *p)
24991+{
24992+ if (!p)
24993+ return;
24994+ AuDebugOn(au_kfree_sz_test(p));
24995+ kfree(p);
24996+}
24997+
e2f27e51
AM
24998+static inline int au_kmidx_sub(size_t sz, size_t new_sz)
24999+{
25000+#ifndef CONFIG_SLOB
25001+ return kmalloc_index(sz) - kmalloc_index(new_sz);
25002+#else
25003+ return -1; /* SLOB is untested */
25004+#endif
25005+}
25006+
1facf9fc 25007+int au_seq_path(struct seq_file *seq, struct path *path);
25008+
e49829fe
JR
25009+#ifdef CONFIG_PROC_FS
25010+/* procfs.c */
25011+int __init au_procfs_init(void);
25012+void au_procfs_fin(void);
25013+#else
25014+AuStubInt0(au_procfs_init, void);
25015+AuStubVoid(au_procfs_fin, void);
25016+#endif
25017+
4f0767ce
JR
25018+/* ---------------------------------------------------------------------- */
25019+
1c60b727 25020+/* kmem cache */
1facf9fc 25021+enum {
25022+ AuCache_DINFO,
25023+ AuCache_ICNTNR,
25024+ AuCache_FINFO,
25025+ AuCache_VDIR,
25026+ AuCache_DEHSTR,
7eafdf33 25027+ AuCache_HNOTIFY, /* must be last */
1facf9fc 25028+ AuCache_Last
25029+};
25030+
1c60b727 25031+extern struct kmem_cache *au_cache[AuCache_Last];
f0c0a007 25032+
4a4d8108
AM
25033+#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
25034+#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
25035+#define AuCacheCtor(type, ctor) \
25036+ kmem_cache_create(#type, sizeof(struct type), \
25037+ __alignof__(struct type), AuCacheFlags, ctor)
1facf9fc 25038+
e49925d1
JR
25039+#define AuCacheFuncs(name, index) \
25040+ static inline struct au_##name *au_cache_alloc_##name(void) \
25041+ { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
25042+ static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
25043+ { kmem_cache_free(au_cache[AuCache_##index], p); } \
25044+ \
25045+ static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
25046+ { void *p = rcu; \
25047+ p -= offsetof(struct au_##name, rcu); \
25048+ kmem_cache_free(au_cache[AuCache_##index], p); } \
25049+ static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
25050+ { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
25051+ call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); } \
25052+ \
25053+ static inline void au_cache_free_##name(struct au_##name *p) \
25054+ { /* au_cache_free_##name##_norcu(p); */ \
25055+ au_cache_free_##name##_rcu(p); }
1facf9fc 25056+
25057+AuCacheFuncs(dinfo, DINFO);
25058+AuCacheFuncs(icntnr, ICNTNR);
25059+AuCacheFuncs(finfo, FINFO);
25060+AuCacheFuncs(vdir, VDIR);
4a4d8108
AM
25061+AuCacheFuncs(vdir_dehstr, DEHSTR);
25062+#ifdef CONFIG_AUFS_HNOTIFY
25063+AuCacheFuncs(hnotify, HNOTIFY);
25064+#endif
1facf9fc 25065+
4a4d8108
AM
25066+#endif /* __KERNEL__ */
25067+#endif /* __AUFS_MODULE_H__ */
c2b27bf2
AM
25068diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
25069--- /usr/share/empty/fs/aufs/mvdown.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 25070+++ linux/fs/aufs/mvdown.c 2019-01-28 14:36:12.222418047 +0100
062440b3 25071@@ -0,0 +1,705 @@
cd7a4cd9 25072+// SPDX-License-Identifier: GPL-2.0
c2b27bf2 25073+/*
b00004a5 25074+ * Copyright (C) 2011-2018 Junjiro R. Okajima
c2b27bf2
AM
25075+ *
25076+ * This program, aufs is free software; you can redistribute it and/or modify
25077+ * it under the terms of the GNU General Public License as published by
25078+ * the Free Software Foundation; either version 2 of the License, or
25079+ * (at your option) any later version.
25080+ *
25081+ * This program is distributed in the hope that it will be useful,
25082+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25083+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25084+ * GNU General Public License for more details.
25085+ *
25086+ * You should have received a copy of the GNU General Public License
523b37e3
AM
25087+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
25088+ */
25089+
25090+/*
25091+ * move-down, opposite of copy-up
c2b27bf2
AM
25092+ */
25093+
25094+#include "aufs.h"
25095+
c2b27bf2
AM
25096+struct au_mvd_args {
25097+ struct {
c2b27bf2
AM
25098+ struct super_block *h_sb;
25099+ struct dentry *h_parent;
25100+ struct au_hinode *hdir;
392086de 25101+ struct inode *h_dir, *h_inode;
c1595e42 25102+ struct au_pin pin;
c2b27bf2
AM
25103+ } info[AUFS_MVDOWN_NARRAY];
25104+
25105+ struct aufs_mvdown mvdown;
25106+ struct dentry *dentry, *parent;
25107+ struct inode *inode, *dir;
25108+ struct super_block *sb;
25109+ aufs_bindex_t bopq, bwh, bfound;
25110+ unsigned char rename_lock;
c2b27bf2
AM
25111+};
25112+
392086de 25113+#define mvd_errno mvdown.au_errno
076b876e
AM
25114+#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
25115+#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
25116+#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
25117+#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
c2b27bf2 25118+
392086de
AM
25119+#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
25120+#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
25121+#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
25122+#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
25123+#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
c1595e42 25124+#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
392086de
AM
25125+
25126+#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
25127+#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
25128+#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
25129+#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
25130+#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
c1595e42 25131+#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
c2b27bf2
AM
25132+
25133+#define AU_MVD_PR(flag, ...) do { \
25134+ if (flag) \
25135+ pr_err(__VA_ARGS__); \
25136+ } while (0)
25137+
076b876e
AM
25138+static int find_lower_writable(struct au_mvd_args *a)
25139+{
25140+ struct super_block *sb;
5afbbe0d 25141+ aufs_bindex_t bindex, bbot;
076b876e
AM
25142+ struct au_branch *br;
25143+
25144+ sb = a->sb;
25145+ bindex = a->mvd_bsrc;
5afbbe0d 25146+ bbot = au_sbbot(sb);
076b876e 25147+ if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
5afbbe0d 25148+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
25149+ br = au_sbr(sb, bindex);
25150+ if (au_br_fhsm(br->br_perm)
8b6a4947 25151+ && !sb_rdonly(au_br_sb(br)))
076b876e
AM
25152+ return bindex;
25153+ }
25154+ else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
5afbbe0d 25155+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
25156+ br = au_sbr(sb, bindex);
25157+ if (!au_br_rdonly(br))
25158+ return bindex;
25159+ }
25160+ else
5afbbe0d 25161+ for (bindex++; bindex <= bbot; bindex++) {
076b876e 25162+ br = au_sbr(sb, bindex);
8b6a4947 25163+ if (!sb_rdonly(au_br_sb(br))) {
076b876e
AM
25164+ if (au_br_rdonly(br))
25165+ a->mvdown.flags
25166+ |= AUFS_MVDOWN_ROLOWER_R;
25167+ return bindex;
25168+ }
25169+ }
25170+
25171+ return -1;
25172+}
25173+
c2b27bf2 25174+/* make the parent dir on bdst */
392086de 25175+static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25176+{
25177+ int err;
25178+
25179+ err = 0;
25180+ a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
25181+ a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
25182+ a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
25183+ a->mvd_h_dst_parent = NULL;
5afbbe0d 25184+ if (au_dbbot(a->parent) >= a->mvd_bdst)
c2b27bf2
AM
25185+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25186+ if (!a->mvd_h_dst_parent) {
25187+ err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
25188+ if (unlikely(err)) {
392086de 25189+ AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
c2b27bf2
AM
25190+ goto out;
25191+ }
25192+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25193+ }
25194+
25195+out:
25196+ AuTraceErr(err);
25197+ return err;
25198+}
25199+
25200+/* lock them all */
392086de 25201+static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25202+{
25203+ int err;
25204+ struct dentry *h_trap;
25205+
25206+ a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
25207+ a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
c1595e42
JR
25208+ err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
25209+ au_opt_udba(a->sb),
25210+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25211+ AuTraceErr(err);
25212+ if (unlikely(err)) {
25213+ AU_MVD_PR(dmsg, "pin_dst failed\n");
25214+ goto out;
25215+ }
25216+
c2b27bf2
AM
25217+ if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
25218+ a->rename_lock = 0;
c1595e42
JR
25219+ au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25220+ AuLsc_DI_PARENT, AuLsc_I_PARENT3,
25221+ au_opt_udba(a->sb),
25222+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25223+ err = au_do_pin(&a->mvd_pin_src);
25224+ AuTraceErr(err);
5527c038 25225+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
25226+ if (unlikely(err)) {
25227+ AU_MVD_PR(dmsg, "pin_src failed\n");
25228+ goto out_dst;
25229+ }
25230+ goto out; /* success */
c2b27bf2
AM
25231+ }
25232+
c2b27bf2 25233+ a->rename_lock = 1;
c1595e42
JR
25234+ au_pin_hdir_unlock(&a->mvd_pin_dst);
25235+ err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25236+ au_opt_udba(a->sb),
25237+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25238+ AuTraceErr(err);
5527c038 25239+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
25240+ if (unlikely(err)) {
25241+ AU_MVD_PR(dmsg, "pin_src failed\n");
25242+ au_pin_hdir_lock(&a->mvd_pin_dst);
25243+ goto out_dst;
25244+ }
25245+ au_pin_hdir_unlock(&a->mvd_pin_src);
c2b27bf2
AM
25246+ h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25247+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
25248+ if (h_trap) {
25249+ err = (h_trap != a->mvd_h_src_parent);
25250+ if (err)
25251+ err = (h_trap != a->mvd_h_dst_parent);
25252+ }
25253+ BUG_ON(err); /* it should never happen */
c1595e42
JR
25254+ if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
25255+ err = -EBUSY;
25256+ AuTraceErr(err);
25257+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25258+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
25259+ au_pin_hdir_lock(&a->mvd_pin_src);
25260+ au_unpin(&a->mvd_pin_src);
25261+ au_pin_hdir_lock(&a->mvd_pin_dst);
25262+ goto out_dst;
25263+ }
25264+ goto out; /* success */
c2b27bf2 25265+
c1595e42
JR
25266+out_dst:
25267+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
25268+out:
25269+ AuTraceErr(err);
25270+ return err;
25271+}
25272+
392086de 25273+static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2 25274+{
c1595e42
JR
25275+ if (!a->rename_lock)
25276+ au_unpin(&a->mvd_pin_src);
25277+ else {
c2b27bf2
AM
25278+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25279+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
c1595e42
JR
25280+ au_pin_hdir_lock(&a->mvd_pin_src);
25281+ au_unpin(&a->mvd_pin_src);
25282+ au_pin_hdir_lock(&a->mvd_pin_dst);
25283+ }
25284+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
25285+}
25286+
25287+/* copy-down the file */
392086de 25288+static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25289+{
25290+ int err;
25291+ struct au_cp_generic cpg = {
25292+ .dentry = a->dentry,
25293+ .bdst = a->mvd_bdst,
25294+ .bsrc = a->mvd_bsrc,
25295+ .len = -1,
c1595e42 25296+ .pin = &a->mvd_pin_dst,
c2b27bf2
AM
25297+ .flags = AuCpup_DTIME | AuCpup_HOPEN
25298+ };
25299+
25300+ AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
392086de
AM
25301+ if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25302+ au_fset_cpup(cpg.flags, OVERWRITE);
25303+ if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
25304+ au_fset_cpup(cpg.flags, RWDST);
c2b27bf2
AM
25305+ err = au_sio_cpdown_simple(&cpg);
25306+ if (unlikely(err))
392086de 25307+ AU_MVD_PR(dmsg, "cpdown failed\n");
c2b27bf2
AM
25308+
25309+ AuTraceErr(err);
25310+ return err;
25311+}
25312+
25313+/*
25314+ * unlink the whiteout on bdst if exist which may be created by UDBA while we
25315+ * were sleeping
25316+ */
392086de 25317+static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25318+{
25319+ int err;
25320+ struct path h_path;
25321+ struct au_branch *br;
523b37e3 25322+ struct inode *delegated;
c2b27bf2
AM
25323+
25324+ br = au_sbr(a->sb, a->mvd_bdst);
25325+ h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
25326+ err = PTR_ERR(h_path.dentry);
25327+ if (IS_ERR(h_path.dentry)) {
392086de 25328+ AU_MVD_PR(dmsg, "wh_lkup failed\n");
c2b27bf2
AM
25329+ goto out;
25330+ }
25331+
25332+ err = 0;
5527c038 25333+ if (d_is_positive(h_path.dentry)) {
c2b27bf2 25334+ h_path.mnt = au_br_mnt(br);
523b37e3 25335+ delegated = NULL;
5527c038 25336+ err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
523b37e3
AM
25337+ &delegated, /*force*/0);
25338+ if (unlikely(err == -EWOULDBLOCK)) {
25339+ pr_warn("cannot retry for NFSv4 delegation"
25340+ " for an internal unlink\n");
25341+ iput(delegated);
25342+ }
c2b27bf2 25343+ if (unlikely(err))
392086de 25344+ AU_MVD_PR(dmsg, "wh_unlink failed\n");
c2b27bf2
AM
25345+ }
25346+ dput(h_path.dentry);
25347+
25348+out:
25349+ AuTraceErr(err);
25350+ return err;
25351+}
25352+
25353+/*
25354+ * unlink the topmost h_dentry
c2b27bf2 25355+ */
392086de 25356+static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25357+{
25358+ int err;
25359+ struct path h_path;
523b37e3 25360+ struct inode *delegated;
c2b27bf2
AM
25361+
25362+ h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
25363+ h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
523b37e3
AM
25364+ delegated = NULL;
25365+ err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
25366+ if (unlikely(err == -EWOULDBLOCK)) {
25367+ pr_warn("cannot retry for NFSv4 delegation"
25368+ " for an internal unlink\n");
25369+ iput(delegated);
25370+ }
c2b27bf2 25371+ if (unlikely(err))
392086de 25372+ AU_MVD_PR(dmsg, "unlink failed\n");
c2b27bf2
AM
25373+
25374+ AuTraceErr(err);
25375+ return err;
25376+}
25377+
076b876e
AM
25378+/* Since mvdown succeeded, we ignore an error of this function */
25379+static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
25380+{
25381+ int err;
25382+ struct au_branch *br;
25383+
25384+ a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
25385+ br = au_sbr(a->sb, a->mvd_bsrc);
25386+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
25387+ if (!err) {
25388+ br = au_sbr(a->sb, a->mvd_bdst);
25389+ a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
25390+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
25391+ }
25392+ if (!err)
25393+ a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
25394+ else
25395+ AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
25396+}
25397+
c2b27bf2
AM
25398+/*
25399+ * copy-down the file and unlink the bsrc file.
25400+ * - unlink the bdst whout if exist
25401+ * - copy-down the file (with whtmp name and rename)
25402+ * - unlink the bsrc file
25403+ */
392086de 25404+static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25405+{
25406+ int err;
25407+
392086de 25408+ err = au_do_mkdir(dmsg, a);
c2b27bf2 25409+ if (!err)
392086de 25410+ err = au_do_lock(dmsg, a);
c2b27bf2
AM
25411+ if (unlikely(err))
25412+ goto out;
25413+
25414+ /*
25415+ * do not revert the activities we made on bdst since they should be
25416+ * harmless in aufs.
25417+ */
25418+
392086de 25419+ err = au_do_cpdown(dmsg, a);
c2b27bf2 25420+ if (!err)
392086de
AM
25421+ err = au_do_unlink_wh(dmsg, a);
25422+ if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
25423+ err = au_do_unlink(dmsg, a);
c2b27bf2
AM
25424+ if (unlikely(err))
25425+ goto out_unlock;
25426+
c1595e42
JR
25427+ AuDbg("%pd2, 0x%x, %d --> %d\n",
25428+ a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
076b876e
AM
25429+ if (find_lower_writable(a) < 0)
25430+ a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
25431+
25432+ if (a->mvdown.flags & AUFS_MVDOWN_STFS)
25433+ au_do_stfs(dmsg, a);
25434+
c2b27bf2 25435+ /* maintain internal array */
392086de
AM
25436+ if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
25437+ au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
5afbbe0d 25438+ au_set_dbtop(a->dentry, a->mvd_bdst);
392086de 25439+ au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
5afbbe0d 25440+ au_set_ibtop(a->inode, a->mvd_bdst);
79b8bda9
AM
25441+ } else {
25442+ /* hide the lower */
25443+ au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
5afbbe0d 25444+ au_set_dbbot(a->dentry, a->mvd_bsrc);
79b8bda9 25445+ au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
5afbbe0d 25446+ au_set_ibbot(a->inode, a->mvd_bsrc);
392086de 25447+ }
5afbbe0d
AM
25448+ if (au_dbbot(a->dentry) < a->mvd_bdst)
25449+ au_set_dbbot(a->dentry, a->mvd_bdst);
25450+ if (au_ibbot(a->inode) < a->mvd_bdst)
25451+ au_set_ibbot(a->inode, a->mvd_bdst);
c2b27bf2
AM
25452+
25453+out_unlock:
392086de 25454+ au_do_unlock(dmsg, a);
c2b27bf2
AM
25455+out:
25456+ AuTraceErr(err);
25457+ return err;
25458+}
25459+
25460+/* ---------------------------------------------------------------------- */
25461+
c2b27bf2 25462+/* make sure the file is idle */
392086de 25463+static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25464+{
25465+ int err, plinked;
c2b27bf2
AM
25466+
25467+ err = 0;
c2b27bf2 25468+ plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
5afbbe0d 25469+ if (au_dbtop(a->dentry) == a->mvd_bsrc
c1595e42 25470+ && au_dcount(a->dentry) == 1
c2b27bf2 25471+ && atomic_read(&a->inode->i_count) == 1
392086de 25472+ /* && a->mvd_h_src_inode->i_nlink == 1 */
c2b27bf2
AM
25473+ && (!plinked || !au_plink_test(a->inode))
25474+ && a->inode->i_nlink == 1)
25475+ goto out;
25476+
25477+ err = -EBUSY;
392086de 25478+ AU_MVD_PR(dmsg,
c1595e42 25479+ "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
5afbbe0d 25480+ a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
c2b27bf2 25481+ atomic_read(&a->inode->i_count), a->inode->i_nlink,
392086de 25482+ a->mvd_h_src_inode->i_nlink,
c2b27bf2
AM
25483+ plinked, plinked ? au_plink_test(a->inode) : 0);
25484+
25485+out:
25486+ AuTraceErr(err);
25487+ return err;
25488+}
25489+
25490+/* make sure the parent dir is fine */
392086de 25491+static int au_mvd_args_parent(const unsigned char dmsg,
c2b27bf2
AM
25492+ struct au_mvd_args *a)
25493+{
25494+ int err;
25495+ aufs_bindex_t bindex;
25496+
25497+ err = 0;
25498+ if (unlikely(au_alive_dir(a->parent))) {
25499+ err = -ENOENT;
392086de 25500+ AU_MVD_PR(dmsg, "parent dir is dead\n");
c2b27bf2
AM
25501+ goto out;
25502+ }
25503+
25504+ a->bopq = au_dbdiropq(a->parent);
25505+ bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
25506+ AuDbg("b%d\n", bindex);
25507+ if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
25508+ || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
25509+ err = -EINVAL;
392086de
AM
25510+ a->mvd_errno = EAU_MVDOWN_OPAQUE;
25511+ AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
c2b27bf2
AM
25512+ a->bopq, a->mvd_bdst);
25513+ }
25514+
25515+out:
25516+ AuTraceErr(err);
25517+ return err;
25518+}
25519+
392086de 25520+static int au_mvd_args_intermediate(const unsigned char dmsg,
c2b27bf2
AM
25521+ struct au_mvd_args *a)
25522+{
25523+ int err;
25524+ struct au_dinfo *dinfo, *tmp;
25525+
25526+ /* lookup the next lower positive entry */
25527+ err = -ENOMEM;
25528+ tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
25529+ if (unlikely(!tmp))
25530+ goto out;
25531+
25532+ a->bfound = -1;
25533+ a->bwh = -1;
25534+ dinfo = au_di(a->dentry);
25535+ au_di_cp(tmp, dinfo);
25536+ au_di_swap(tmp, dinfo);
25537+
25538+ /* returns the number of positive dentries */
5afbbe0d
AM
25539+ err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
25540+ /* AuLkup_IGNORE_PERM */ 0);
c2b27bf2
AM
25541+ if (!err)
25542+ a->bwh = au_dbwh(a->dentry);
25543+ else if (err > 0)
5afbbe0d 25544+ a->bfound = au_dbtop(a->dentry);
c2b27bf2
AM
25545+
25546+ au_di_swap(tmp, dinfo);
25547+ au_rw_write_unlock(&tmp->di_rwsem);
25548+ au_di_free(tmp);
25549+ if (unlikely(err < 0))
392086de 25550+ AU_MVD_PR(dmsg, "failed look-up lower\n");
c2b27bf2
AM
25551+
25552+ /*
25553+ * here, we have these cases.
25554+ * bfound == -1
25555+ * no positive dentry under bsrc. there are more sub-cases.
25556+ * bwh < 0
25557+ * there no whiteout, we can safely move-down.
25558+ * bwh <= bsrc
25559+ * impossible
25560+ * bsrc < bwh && bwh < bdst
25561+ * there is a whiteout on RO branch. cannot proceed.
25562+ * bwh == bdst
25563+ * there is a whiteout on the RW target branch. it should
25564+ * be removed.
25565+ * bdst < bwh
25566+ * there is a whiteout somewhere unrelated branch.
25567+ * -1 < bfound && bfound <= bsrc
25568+ * impossible.
25569+ * bfound < bdst
25570+ * found, but it is on RO branch between bsrc and bdst. cannot
25571+ * proceed.
25572+ * bfound == bdst
25573+ * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
25574+ * error.
25575+ * bdst < bfound
25576+ * found, after we create the file on bdst, it will be hidden.
25577+ */
25578+
25579+ AuDebugOn(a->bfound == -1
25580+ && a->bwh != -1
25581+ && a->bwh <= a->mvd_bsrc);
25582+ AuDebugOn(-1 < a->bfound
25583+ && a->bfound <= a->mvd_bsrc);
25584+
25585+ err = -EINVAL;
25586+ if (a->bfound == -1
25587+ && a->mvd_bsrc < a->bwh
25588+ && a->bwh != -1
25589+ && a->bwh < a->mvd_bdst) {
392086de
AM
25590+ a->mvd_errno = EAU_MVDOWN_WHITEOUT;
25591+ AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
c2b27bf2
AM
25592+ a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
25593+ goto out;
25594+ } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
392086de
AM
25595+ a->mvd_errno = EAU_MVDOWN_UPPER;
25596+ AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
c2b27bf2
AM
25597+ a->mvd_bdst, a->bfound);
25598+ goto out;
25599+ }
25600+
25601+ err = 0; /* success */
25602+
25603+out:
25604+ AuTraceErr(err);
25605+ return err;
25606+}
25607+
392086de 25608+static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25609+{
25610+ int err;
25611+
392086de
AM
25612+ err = 0;
25613+ if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25614+ && a->bfound == a->mvd_bdst)
25615+ err = -EEXIST;
c2b27bf2
AM
25616+ AuTraceErr(err);
25617+ return err;
25618+}
25619+
392086de 25620+static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25621+{
25622+ int err;
25623+ struct au_branch *br;
25624+
25625+ err = -EISDIR;
25626+ if (unlikely(S_ISDIR(a->inode->i_mode)))
25627+ goto out;
25628+
25629+ err = -EINVAL;
392086de 25630+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
5afbbe0d 25631+ a->mvd_bsrc = au_ibtop(a->inode);
392086de
AM
25632+ else {
25633+ a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
25634+ if (unlikely(a->mvd_bsrc < 0
5afbbe0d
AM
25635+ || (a->mvd_bsrc < au_dbtop(a->dentry)
25636+ || au_dbbot(a->dentry) < a->mvd_bsrc
392086de 25637+ || !au_h_dptr(a->dentry, a->mvd_bsrc))
5afbbe0d
AM
25638+ || (a->mvd_bsrc < au_ibtop(a->inode)
25639+ || au_ibbot(a->inode) < a->mvd_bsrc
392086de
AM
25640+ || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
25641+ a->mvd_errno = EAU_MVDOWN_NOUPPER;
25642+ AU_MVD_PR(dmsg, "no upper\n");
25643+ goto out;
25644+ }
25645+ }
5afbbe0d 25646+ if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
392086de
AM
25647+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25648+ AU_MVD_PR(dmsg, "on the bottom\n");
c2b27bf2
AM
25649+ goto out;
25650+ }
392086de 25651+ a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
c2b27bf2
AM
25652+ br = au_sbr(a->sb, a->mvd_bsrc);
25653+ err = au_br_rdonly(br);
392086de
AM
25654+ if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
25655+ if (unlikely(err))
25656+ goto out;
25657+ } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
25658+ || IS_APPEND(a->mvd_h_src_inode))) {
25659+ if (err)
25660+ a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
25661+ /* go on */
25662+ } else
c2b27bf2
AM
25663+ goto out;
25664+
25665+ err = -EINVAL;
392086de
AM
25666+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
25667+ a->mvd_bdst = find_lower_writable(a);
25668+ if (unlikely(a->mvd_bdst < 0)) {
25669+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25670+ AU_MVD_PR(dmsg, "no writable lower branch\n");
25671+ goto out;
25672+ }
25673+ } else {
25674+ a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
25675+ if (unlikely(a->mvd_bdst < 0
5afbbe0d 25676+ || au_sbbot(a->sb) < a->mvd_bdst)) {
392086de
AM
25677+ a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
25678+ AU_MVD_PR(dmsg, "no lower brid\n");
25679+ goto out;
25680+ }
c2b27bf2
AM
25681+ }
25682+
392086de 25683+ err = au_mvd_args_busy(dmsg, a);
c2b27bf2 25684+ if (!err)
392086de 25685+ err = au_mvd_args_parent(dmsg, a);
c2b27bf2 25686+ if (!err)
392086de 25687+ err = au_mvd_args_intermediate(dmsg, a);
c2b27bf2 25688+ if (!err)
392086de 25689+ err = au_mvd_args_exist(dmsg, a);
c2b27bf2
AM
25690+ if (!err)
25691+ AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
25692+
25693+out:
25694+ AuTraceErr(err);
25695+ return err;
25696+}
25697+
25698+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
25699+{
392086de
AM
25700+ int err, e;
25701+ unsigned char dmsg;
25702+ struct au_mvd_args *args;
79b8bda9 25703+ struct inode *inode;
c2b27bf2 25704+
79b8bda9 25705+ inode = d_inode(dentry);
c2b27bf2
AM
25706+ err = -EPERM;
25707+ if (unlikely(!capable(CAP_SYS_ADMIN)))
25708+ goto out;
25709+
392086de
AM
25710+ err = -ENOMEM;
25711+ args = kmalloc(sizeof(*args), GFP_NOFS);
25712+ if (unlikely(!args))
25713+ goto out;
25714+
25715+ err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
25716+ if (!err)
25717+ err = !access_ok(VERIFY_WRITE, uarg, sizeof(*uarg));
c2b27bf2
AM
25718+ if (unlikely(err)) {
25719+ err = -EFAULT;
392086de
AM
25720+ AuTraceErr(err);
25721+ goto out_free;
c2b27bf2 25722+ }
392086de
AM
25723+ AuDbg("flags 0x%x\n", args->mvdown.flags);
25724+ args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
25725+ args->mvdown.au_errno = 0;
25726+ args->dentry = dentry;
79b8bda9 25727+ args->inode = inode;
392086de 25728+ args->sb = dentry->d_sb;
c2b27bf2 25729+
392086de
AM
25730+ err = -ENOENT;
25731+ dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
25732+ args->parent = dget_parent(dentry);
5527c038 25733+ args->dir = d_inode(args->parent);
febd17d6 25734+ inode_lock_nested(args->dir, I_MUTEX_PARENT);
392086de
AM
25735+ dput(args->parent);
25736+ if (unlikely(args->parent != dentry->d_parent)) {
25737+ AU_MVD_PR(dmsg, "parent dir is moved\n");
c2b27bf2
AM
25738+ goto out_dir;
25739+ }
25740+
febd17d6 25741+ inode_lock_nested(inode, I_MUTEX_CHILD);
b95c5147 25742+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
c2b27bf2
AM
25743+ if (unlikely(err))
25744+ goto out_inode;
25745+
392086de
AM
25746+ di_write_lock_parent(args->parent);
25747+ err = au_mvd_args(dmsg, args);
c2b27bf2
AM
25748+ if (unlikely(err))
25749+ goto out_parent;
25750+
392086de 25751+ err = au_do_mvdown(dmsg, args);
c2b27bf2
AM
25752+ if (unlikely(err))
25753+ goto out_parent;
c2b27bf2 25754+
392086de 25755+ au_cpup_attr_timesizes(args->dir);
79b8bda9
AM
25756+ au_cpup_attr_timesizes(inode);
25757+ if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
25758+ au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
c2b27bf2
AM
25759+ /* au_digen_dec(dentry); */
25760+
25761+out_parent:
392086de 25762+ di_write_unlock(args->parent);
c2b27bf2
AM
25763+ aufs_read_unlock(dentry, AuLock_DW);
25764+out_inode:
febd17d6 25765+ inode_unlock(inode);
c2b27bf2 25766+out_dir:
febd17d6 25767+ inode_unlock(args->dir);
392086de
AM
25768+out_free:
25769+ e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
25770+ if (unlikely(e))
25771+ err = -EFAULT;
e49925d1 25772+ au_kfree_rcu(args);
c2b27bf2
AM
25773+out:
25774+ AuTraceErr(err);
25775+ return err;
25776+}
25777diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
25778--- /usr/share/empty/fs/aufs/opts.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 25779+++ linux/fs/aufs/opts.c 2019-01-28 14:36:12.222418047 +0100
acd2b654 25780@@ -0,0 +1,1877 @@
cd7a4cd9 25781+// SPDX-License-Identifier: GPL-2.0
1facf9fc 25782+/*
b00004a5 25783+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 25784+ *
25785+ * This program, aufs is free software; you can redistribute it and/or modify
25786+ * it under the terms of the GNU General Public License as published by
25787+ * the Free Software Foundation; either version 2 of the License, or
25788+ * (at your option) any later version.
dece6358
AM
25789+ *
25790+ * This program is distributed in the hope that it will be useful,
25791+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25792+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25793+ * GNU General Public License for more details.
25794+ *
25795+ * You should have received a copy of the GNU General Public License
523b37e3 25796+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 25797+ */
25798+
25799+/*
25800+ * mount options/flags
25801+ */
25802+
dece6358 25803+#include <linux/namei.h>
1facf9fc 25804+#include <linux/types.h> /* a distribution requires */
25805+#include <linux/parser.h>
25806+#include "aufs.h"
25807+
25808+/* ---------------------------------------------------------------------- */
25809+
25810+enum {
25811+ Opt_br,
7e9cd9fe
AM
25812+ Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
25813+ Opt_idel, Opt_imod,
25814+ Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
dece6358 25815+ Opt_rdblk_def, Opt_rdhash_def,
7e9cd9fe 25816+ Opt_xino, Opt_noxino,
1facf9fc 25817+ Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
25818+ Opt_trunc_xino_path, Opt_itrunc_xino,
25819+ Opt_trunc_xib, Opt_notrunc_xib,
dece6358 25820+ Opt_shwh, Opt_noshwh,
1facf9fc 25821+ Opt_plink, Opt_noplink, Opt_list_plink,
25822+ Opt_udba,
4a4d8108 25823+ Opt_dio, Opt_nodio,
1facf9fc 25824+ Opt_diropq_a, Opt_diropq_w,
25825+ Opt_warn_perm, Opt_nowarn_perm,
25826+ Opt_wbr_copyup, Opt_wbr_create,
076b876e 25827+ Opt_fhsm_sec,
1facf9fc 25828+ Opt_verbose, Opt_noverbose,
25829+ Opt_sum, Opt_nosum, Opt_wsum,
076b876e 25830+ Opt_dirperm1, Opt_nodirperm1,
8b6a4947 25831+ Opt_dirren, Opt_nodirren,
c1595e42 25832+ Opt_acl, Opt_noacl,
1facf9fc 25833+ Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
25834+};
25835+
25836+static match_table_t options = {
25837+ {Opt_br, "br=%s"},
25838+ {Opt_br, "br:%s"},
25839+
25840+ {Opt_add, "add=%d:%s"},
25841+ {Opt_add, "add:%d:%s"},
25842+ {Opt_add, "ins=%d:%s"},
25843+ {Opt_add, "ins:%d:%s"},
25844+ {Opt_append, "append=%s"},
25845+ {Opt_append, "append:%s"},
25846+ {Opt_prepend, "prepend=%s"},
25847+ {Opt_prepend, "prepend:%s"},
25848+
25849+ {Opt_del, "del=%s"},
25850+ {Opt_del, "del:%s"},
25851+ /* {Opt_idel, "idel:%d"}, */
25852+ {Opt_mod, "mod=%s"},
25853+ {Opt_mod, "mod:%s"},
25854+ /* {Opt_imod, "imod:%d:%s"}, */
25855+
25856+ {Opt_dirwh, "dirwh=%d"},
25857+
25858+ {Opt_xino, "xino=%s"},
25859+ {Opt_noxino, "noxino"},
25860+ {Opt_trunc_xino, "trunc_xino"},
25861+ {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
25862+ {Opt_notrunc_xino, "notrunc_xino"},
25863+ {Opt_trunc_xino_path, "trunc_xino=%s"},
25864+ {Opt_itrunc_xino, "itrunc_xino=%d"},
25865+ /* {Opt_zxino, "zxino=%s"}, */
25866+ {Opt_trunc_xib, "trunc_xib"},
25867+ {Opt_notrunc_xib, "notrunc_xib"},
25868+
e49829fe 25869+#ifdef CONFIG_PROC_FS
1facf9fc 25870+ {Opt_plink, "plink"},
e49829fe
JR
25871+#else
25872+ {Opt_ignore_silent, "plink"},
25873+#endif
25874+
1facf9fc 25875+ {Opt_noplink, "noplink"},
e49829fe 25876+
1facf9fc 25877+#ifdef CONFIG_AUFS_DEBUG
25878+ {Opt_list_plink, "list_plink"},
25879+#endif
25880+
25881+ {Opt_udba, "udba=%s"},
25882+
4a4d8108
AM
25883+ {Opt_dio, "dio"},
25884+ {Opt_nodio, "nodio"},
25885+
8b6a4947
AM
25886+#ifdef CONFIG_AUFS_DIRREN
25887+ {Opt_dirren, "dirren"},
25888+ {Opt_nodirren, "nodirren"},
25889+#else
25890+ {Opt_ignore, "dirren"},
25891+ {Opt_ignore_silent, "nodirren"},
25892+#endif
25893+
076b876e
AM
25894+#ifdef CONFIG_AUFS_FHSM
25895+ {Opt_fhsm_sec, "fhsm_sec=%d"},
25896+#else
8b6a4947 25897+ {Opt_ignore, "fhsm_sec=%d"},
076b876e
AM
25898+#endif
25899+
1facf9fc 25900+ {Opt_diropq_a, "diropq=always"},
25901+ {Opt_diropq_a, "diropq=a"},
25902+ {Opt_diropq_w, "diropq=whiteouted"},
25903+ {Opt_diropq_w, "diropq=w"},
25904+
25905+ {Opt_warn_perm, "warn_perm"},
25906+ {Opt_nowarn_perm, "nowarn_perm"},
25907+
25908+ /* keep them temporary */
1facf9fc 25909+ {Opt_ignore_silent, "nodlgt"},
8b6a4947 25910+ {Opt_ignore, "clean_plink"},
1facf9fc 25911+
dece6358
AM
25912+#ifdef CONFIG_AUFS_SHWH
25913+ {Opt_shwh, "shwh"},
25914+#endif
25915+ {Opt_noshwh, "noshwh"},
25916+
076b876e
AM
25917+ {Opt_dirperm1, "dirperm1"},
25918+ {Opt_nodirperm1, "nodirperm1"},
25919+
1facf9fc 25920+ {Opt_verbose, "verbose"},
25921+ {Opt_verbose, "v"},
25922+ {Opt_noverbose, "noverbose"},
25923+ {Opt_noverbose, "quiet"},
25924+ {Opt_noverbose, "q"},
25925+ {Opt_noverbose, "silent"},
25926+
25927+ {Opt_sum, "sum"},
25928+ {Opt_nosum, "nosum"},
25929+ {Opt_wsum, "wsum"},
25930+
25931+ {Opt_rdcache, "rdcache=%d"},
25932+ {Opt_rdblk, "rdblk=%d"},
dece6358 25933+ {Opt_rdblk_def, "rdblk=def"},
1facf9fc 25934+ {Opt_rdhash, "rdhash=%d"},
dece6358 25935+ {Opt_rdhash_def, "rdhash=def"},
1facf9fc 25936+
25937+ {Opt_wbr_create, "create=%s"},
25938+ {Opt_wbr_create, "create_policy=%s"},
25939+ {Opt_wbr_copyup, "cpup=%s"},
25940+ {Opt_wbr_copyup, "copyup=%s"},
25941+ {Opt_wbr_copyup, "copyup_policy=%s"},
25942+
c1595e42
JR
25943+ /* generic VFS flag */
25944+#ifdef CONFIG_FS_POSIX_ACL
25945+ {Opt_acl, "acl"},
25946+ {Opt_noacl, "noacl"},
25947+#else
8b6a4947 25948+ {Opt_ignore, "acl"},
c1595e42
JR
25949+ {Opt_ignore_silent, "noacl"},
25950+#endif
25951+
1facf9fc 25952+ /* internal use for the scripts */
25953+ {Opt_ignore_silent, "si=%s"},
25954+
25955+ {Opt_br, "dirs=%s"},
25956+ {Opt_ignore, "debug=%d"},
25957+ {Opt_ignore, "delete=whiteout"},
25958+ {Opt_ignore, "delete=all"},
25959+ {Opt_ignore, "imap=%s"},
25960+
1308ab2a 25961+ /* temporary workaround, due to old mount(8)? */
25962+ {Opt_ignore_silent, "relatime"},
25963+
1facf9fc 25964+ {Opt_err, NULL}
25965+};
25966+
25967+/* ---------------------------------------------------------------------- */
25968+
076b876e 25969+static const char *au_parser_pattern(int val, match_table_t tbl)
1facf9fc 25970+{
076b876e
AM
25971+ struct match_token *p;
25972+
25973+ p = tbl;
25974+ while (p->pattern) {
25975+ if (p->token == val)
25976+ return p->pattern;
25977+ p++;
1facf9fc 25978+ }
25979+ BUG();
25980+ return "??";
25981+}
25982+
076b876e
AM
25983+static const char *au_optstr(int *val, match_table_t tbl)
25984+{
25985+ struct match_token *p;
25986+ int v;
25987+
25988+ v = *val;
2000de60
JR
25989+ if (!v)
25990+ goto out;
076b876e 25991+ p = tbl;
2000de60
JR
25992+ while (p->pattern) {
25993+ if (p->token
25994+ && (v & p->token) == p->token) {
076b876e
AM
25995+ *val &= ~p->token;
25996+ return p->pattern;
25997+ }
25998+ p++;
25999+ }
2000de60
JR
26000+
26001+out:
076b876e
AM
26002+ return NULL;
26003+}
26004+
1facf9fc 26005+/* ---------------------------------------------------------------------- */
26006+
1e00d052 26007+static match_table_t brperm = {
1facf9fc 26008+ {AuBrPerm_RO, AUFS_BRPERM_RO},
26009+ {AuBrPerm_RR, AUFS_BRPERM_RR},
26010+ {AuBrPerm_RW, AUFS_BRPERM_RW},
1e00d052
AM
26011+ {0, NULL}
26012+};
1facf9fc 26013+
86dc4139 26014+static match_table_t brattr = {
076b876e
AM
26015+ /* general */
26016+ {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
26017+ {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
c1595e42 26018+ /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
86dc4139 26019+ {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
2000de60 26020+#ifdef CONFIG_AUFS_FHSM
076b876e 26021+ {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
2000de60
JR
26022+#endif
26023+#ifdef CONFIG_AUFS_XATTR
c1595e42
JR
26024+ {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
26025+ {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
26026+ {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
26027+ {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
26028+ {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
26029+ {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
2000de60 26030+#endif
076b876e
AM
26031+
26032+ /* ro/rr branch */
1e00d052 26033+ {AuBrRAttr_WH, AUFS_BRRATTR_WH},
076b876e
AM
26034+
26035+ /* rw branch */
26036+ {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
1e00d052 26037+ {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
076b876e 26038+
1e00d052 26039+ {0, NULL}
1facf9fc 26040+};
26041+
1e00d052
AM
26042+static int br_attr_val(char *str, match_table_t table, substring_t args[])
26043+{
26044+ int attr, v;
26045+ char *p;
26046+
26047+ attr = 0;
26048+ do {
26049+ p = strchr(str, '+');
26050+ if (p)
26051+ *p = 0;
26052+ v = match_token(str, table, args);
076b876e
AM
26053+ if (v) {
26054+ if (v & AuBrAttr_CMOO_Mask)
26055+ attr &= ~AuBrAttr_CMOO_Mask;
1e00d052 26056+ attr |= v;
076b876e 26057+ } else {
1e00d052
AM
26058+ if (p)
26059+ *p = '+';
0c3ec466 26060+ pr_warn("ignored branch attribute %s\n", str);
1e00d052
AM
26061+ break;
26062+ }
26063+ if (p)
26064+ str = p + 1;
26065+ } while (p);
26066+
26067+ return attr;
26068+}
26069+
076b876e
AM
26070+static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
26071+{
26072+ int sz;
26073+ const char *p;
26074+ char *q;
26075+
076b876e
AM
26076+ q = str->a;
26077+ *q = 0;
26078+ p = au_optstr(&perm, brattr);
26079+ if (p) {
26080+ sz = strlen(p);
26081+ memcpy(q, p, sz + 1);
26082+ q += sz;
26083+ } else
26084+ goto out;
26085+
26086+ do {
26087+ p = au_optstr(&perm, brattr);
26088+ if (p) {
26089+ *q++ = '+';
26090+ sz = strlen(p);
26091+ memcpy(q, p, sz + 1);
26092+ q += sz;
26093+ }
26094+ } while (p);
26095+
26096+out:
c1595e42 26097+ return q - str->a;
076b876e
AM
26098+}
26099+
4a4d8108 26100+static int noinline_for_stack br_perm_val(char *perm)
1facf9fc 26101+{
076b876e
AM
26102+ int val, bad, sz;
26103+ char *p;
1facf9fc 26104+ substring_t args[MAX_OPT_ARGS];
076b876e 26105+ au_br_perm_str_t attr;
1facf9fc 26106+
1e00d052
AM
26107+ p = strchr(perm, '+');
26108+ if (p)
26109+ *p = 0;
26110+ val = match_token(perm, brperm, args);
26111+ if (!val) {
26112+ if (p)
26113+ *p = '+';
0c3ec466 26114+ pr_warn("ignored branch permission %s\n", perm);
1e00d052
AM
26115+ val = AuBrPerm_RO;
26116+ goto out;
26117+ }
26118+ if (!p)
26119+ goto out;
26120+
076b876e
AM
26121+ val |= br_attr_val(p + 1, brattr, args);
26122+
26123+ bad = 0;
86dc4139 26124+ switch (val & AuBrPerm_Mask) {
1e00d052
AM
26125+ case AuBrPerm_RO:
26126+ case AuBrPerm_RR:
076b876e
AM
26127+ bad = val & AuBrWAttr_Mask;
26128+ val &= ~AuBrWAttr_Mask;
1e00d052
AM
26129+ break;
26130+ case AuBrPerm_RW:
076b876e
AM
26131+ bad = val & AuBrRAttr_Mask;
26132+ val &= ~AuBrRAttr_Mask;
1e00d052
AM
26133+ break;
26134+ }
c1595e42
JR
26135+
26136+ /*
26137+ * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
26138+ * does not treat it as an error, just warning.
26139+ * this is a tiny guard for the user operation.
26140+ */
26141+ if (val & AuBrAttr_UNPIN) {
26142+ bad |= AuBrAttr_UNPIN;
26143+ val &= ~AuBrAttr_UNPIN;
26144+ }
26145+
076b876e
AM
26146+ if (unlikely(bad)) {
26147+ sz = au_do_optstr_br_attr(&attr, bad);
26148+ AuDebugOn(!sz);
26149+ pr_warn("ignored branch attribute %s\n", attr.a);
26150+ }
1e00d052
AM
26151+
26152+out:
1facf9fc 26153+ return val;
26154+}
26155+
076b876e 26156+void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
1facf9fc 26157+{
076b876e
AM
26158+ au_br_perm_str_t attr;
26159+ const char *p;
26160+ char *q;
1e00d052
AM
26161+ int sz;
26162+
076b876e
AM
26163+ q = str->a;
26164+ p = au_optstr(&perm, brperm);
26165+ AuDebugOn(!p || !*p);
26166+ sz = strlen(p);
26167+ memcpy(q, p, sz + 1);
26168+ q += sz;
1e00d052 26169+
076b876e
AM
26170+ sz = au_do_optstr_br_attr(&attr, perm);
26171+ if (sz) {
26172+ *q++ = '+';
26173+ memcpy(q, attr.a, sz + 1);
1e00d052
AM
26174+ }
26175+
076b876e 26176+ AuDebugOn(strlen(str->a) >= sizeof(str->a));
1facf9fc 26177+}
26178+
26179+/* ---------------------------------------------------------------------- */
26180+
26181+static match_table_t udbalevel = {
26182+ {AuOpt_UDBA_REVAL, "reval"},
26183+ {AuOpt_UDBA_NONE, "none"},
4a4d8108
AM
26184+#ifdef CONFIG_AUFS_HNOTIFY
26185+ {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
26186+#ifdef CONFIG_AUFS_HFSNOTIFY
26187+ {AuOpt_UDBA_HNOTIFY, "fsnotify"},
4a4d8108 26188+#endif
1facf9fc 26189+#endif
26190+ {-1, NULL}
26191+};
26192+
4a4d8108 26193+static int noinline_for_stack udba_val(char *str)
1facf9fc 26194+{
26195+ substring_t args[MAX_OPT_ARGS];
26196+
7f207e10 26197+ return match_token(str, udbalevel, args);
1facf9fc 26198+}
26199+
26200+const char *au_optstr_udba(int udba)
26201+{
076b876e 26202+ return au_parser_pattern(udba, udbalevel);
1facf9fc 26203+}
26204+
26205+/* ---------------------------------------------------------------------- */
26206+
26207+static match_table_t au_wbr_create_policy = {
26208+ {AuWbrCreate_TDP, "tdp"},
26209+ {AuWbrCreate_TDP, "top-down-parent"},
26210+ {AuWbrCreate_RR, "rr"},
26211+ {AuWbrCreate_RR, "round-robin"},
26212+ {AuWbrCreate_MFS, "mfs"},
26213+ {AuWbrCreate_MFS, "most-free-space"},
26214+ {AuWbrCreate_MFSV, "mfs:%d"},
26215+ {AuWbrCreate_MFSV, "most-free-space:%d"},
26216+
f2c43d5f
AM
26217+ /* top-down regardless the parent, and then mfs */
26218+ {AuWbrCreate_TDMFS, "tdmfs:%d"},
26219+ {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
26220+
1facf9fc 26221+ {AuWbrCreate_MFSRR, "mfsrr:%d"},
26222+ {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
26223+ {AuWbrCreate_PMFS, "pmfs"},
26224+ {AuWbrCreate_PMFSV, "pmfs:%d"},
392086de
AM
26225+ {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
26226+ {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
1facf9fc 26227+
26228+ {-1, NULL}
26229+};
26230+
1facf9fc 26231+static int au_wbr_mfs_wmark(substring_t *arg, char *str,
26232+ struct au_opt_wbr_create *create)
26233+{
26234+ int err;
26235+ unsigned long long ull;
26236+
26237+ err = 0;
a2654f78 26238+ if (!match_u64(arg, &ull))
1facf9fc 26239+ create->mfsrr_watermark = ull;
26240+ else {
4a4d8108 26241+ pr_err("bad integer in %s\n", str);
1facf9fc 26242+ err = -EINVAL;
26243+ }
26244+
26245+ return err;
26246+}
26247+
26248+static int au_wbr_mfs_sec(substring_t *arg, char *str,
26249+ struct au_opt_wbr_create *create)
26250+{
26251+ int n, err;
26252+
26253+ err = 0;
027c5e7a 26254+ if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
1facf9fc 26255+ create->mfs_second = n;
26256+ else {
4a4d8108 26257+ pr_err("bad integer in %s\n", str);
1facf9fc 26258+ err = -EINVAL;
26259+ }
26260+
26261+ return err;
26262+}
26263+
4a4d8108
AM
26264+static int noinline_for_stack
26265+au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
1facf9fc 26266+{
26267+ int err, e;
26268+ substring_t args[MAX_OPT_ARGS];
26269+
26270+ err = match_token(str, au_wbr_create_policy, args);
26271+ create->wbr_create = err;
26272+ switch (err) {
26273+ case AuWbrCreate_MFSRRV:
f2c43d5f 26274+ case AuWbrCreate_TDMFSV:
392086de 26275+ case AuWbrCreate_PMFSRRV:
1facf9fc 26276+ e = au_wbr_mfs_wmark(&args[0], str, create);
26277+ if (!e)
26278+ e = au_wbr_mfs_sec(&args[1], str, create);
26279+ if (unlikely(e))
26280+ err = e;
26281+ break;
26282+ case AuWbrCreate_MFSRR:
f2c43d5f 26283+ case AuWbrCreate_TDMFS:
392086de 26284+ case AuWbrCreate_PMFSRR:
1facf9fc 26285+ e = au_wbr_mfs_wmark(&args[0], str, create);
26286+ if (unlikely(e)) {
26287+ err = e;
26288+ break;
26289+ }
26290+ /*FALLTHROUGH*/
26291+ case AuWbrCreate_MFS:
26292+ case AuWbrCreate_PMFS:
027c5e7a 26293+ create->mfs_second = AUFS_MFS_DEF_SEC;
1facf9fc 26294+ break;
26295+ case AuWbrCreate_MFSV:
26296+ case AuWbrCreate_PMFSV:
26297+ e = au_wbr_mfs_sec(&args[0], str, create);
26298+ if (unlikely(e))
26299+ err = e;
26300+ break;
26301+ }
26302+
26303+ return err;
26304+}
26305+
26306+const char *au_optstr_wbr_create(int wbr_create)
26307+{
076b876e 26308+ return au_parser_pattern(wbr_create, au_wbr_create_policy);
1facf9fc 26309+}
26310+
26311+static match_table_t au_wbr_copyup_policy = {
26312+ {AuWbrCopyup_TDP, "tdp"},
26313+ {AuWbrCopyup_TDP, "top-down-parent"},
26314+ {AuWbrCopyup_BUP, "bup"},
26315+ {AuWbrCopyup_BUP, "bottom-up-parent"},
26316+ {AuWbrCopyup_BU, "bu"},
26317+ {AuWbrCopyup_BU, "bottom-up"},
26318+ {-1, NULL}
26319+};
26320+
4a4d8108 26321+static int noinline_for_stack au_wbr_copyup_val(char *str)
1facf9fc 26322+{
26323+ substring_t args[MAX_OPT_ARGS];
26324+
26325+ return match_token(str, au_wbr_copyup_policy, args);
26326+}
26327+
26328+const char *au_optstr_wbr_copyup(int wbr_copyup)
26329+{
076b876e 26330+ return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
1facf9fc 26331+}
26332+
26333+/* ---------------------------------------------------------------------- */
26334+
26335+static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
26336+
26337+static void dump_opts(struct au_opts *opts)
26338+{
26339+#ifdef CONFIG_AUFS_DEBUG
26340+ /* reduce stack space */
26341+ union {
26342+ struct au_opt_add *add;
26343+ struct au_opt_del *del;
26344+ struct au_opt_mod *mod;
26345+ struct au_opt_xino *xino;
26346+ struct au_opt_xino_itrunc *xino_itrunc;
26347+ struct au_opt_wbr_create *create;
26348+ } u;
26349+ struct au_opt *opt;
26350+
26351+ opt = opts->opt;
26352+ while (opt->type != Opt_tail) {
26353+ switch (opt->type) {
26354+ case Opt_add:
26355+ u.add = &opt->add;
26356+ AuDbg("add {b%d, %s, 0x%x, %p}\n",
26357+ u.add->bindex, u.add->pathname, u.add->perm,
26358+ u.add->path.dentry);
26359+ break;
26360+ case Opt_del:
26361+ case Opt_idel:
26362+ u.del = &opt->del;
26363+ AuDbg("del {%s, %p}\n",
26364+ u.del->pathname, u.del->h_path.dentry);
26365+ break;
26366+ case Opt_mod:
26367+ case Opt_imod:
26368+ u.mod = &opt->mod;
26369+ AuDbg("mod {%s, 0x%x, %p}\n",
26370+ u.mod->path, u.mod->perm, u.mod->h_root);
26371+ break;
26372+ case Opt_append:
26373+ u.add = &opt->add;
26374+ AuDbg("append {b%d, %s, 0x%x, %p}\n",
26375+ u.add->bindex, u.add->pathname, u.add->perm,
26376+ u.add->path.dentry);
26377+ break;
26378+ case Opt_prepend:
26379+ u.add = &opt->add;
26380+ AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
26381+ u.add->bindex, u.add->pathname, u.add->perm,
26382+ u.add->path.dentry);
26383+ break;
26384+ case Opt_dirwh:
26385+ AuDbg("dirwh %d\n", opt->dirwh);
26386+ break;
26387+ case Opt_rdcache:
26388+ AuDbg("rdcache %d\n", opt->rdcache);
26389+ break;
26390+ case Opt_rdblk:
26391+ AuDbg("rdblk %u\n", opt->rdblk);
26392+ break;
dece6358
AM
26393+ case Opt_rdblk_def:
26394+ AuDbg("rdblk_def\n");
26395+ break;
1facf9fc 26396+ case Opt_rdhash:
26397+ AuDbg("rdhash %u\n", opt->rdhash);
26398+ break;
dece6358
AM
26399+ case Opt_rdhash_def:
26400+ AuDbg("rdhash_def\n");
26401+ break;
1facf9fc 26402+ case Opt_xino:
26403+ u.xino = &opt->xino;
523b37e3 26404+ AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
1facf9fc 26405+ break;
26406+ case Opt_trunc_xino:
26407+ AuLabel(trunc_xino);
26408+ break;
26409+ case Opt_notrunc_xino:
26410+ AuLabel(notrunc_xino);
26411+ break;
26412+ case Opt_trunc_xino_path:
26413+ case Opt_itrunc_xino:
26414+ u.xino_itrunc = &opt->xino_itrunc;
26415+ AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
26416+ break;
1facf9fc 26417+ case Opt_noxino:
26418+ AuLabel(noxino);
26419+ break;
26420+ case Opt_trunc_xib:
26421+ AuLabel(trunc_xib);
26422+ break;
26423+ case Opt_notrunc_xib:
26424+ AuLabel(notrunc_xib);
26425+ break;
dece6358
AM
26426+ case Opt_shwh:
26427+ AuLabel(shwh);
26428+ break;
26429+ case Opt_noshwh:
26430+ AuLabel(noshwh);
26431+ break;
076b876e
AM
26432+ case Opt_dirperm1:
26433+ AuLabel(dirperm1);
26434+ break;
26435+ case Opt_nodirperm1:
26436+ AuLabel(nodirperm1);
26437+ break;
1facf9fc 26438+ case Opt_plink:
26439+ AuLabel(plink);
26440+ break;
26441+ case Opt_noplink:
26442+ AuLabel(noplink);
26443+ break;
26444+ case Opt_list_plink:
26445+ AuLabel(list_plink);
26446+ break;
26447+ case Opt_udba:
26448+ AuDbg("udba %d, %s\n",
26449+ opt->udba, au_optstr_udba(opt->udba));
26450+ break;
4a4d8108
AM
26451+ case Opt_dio:
26452+ AuLabel(dio);
26453+ break;
26454+ case Opt_nodio:
26455+ AuLabel(nodio);
26456+ break;
1facf9fc 26457+ case Opt_diropq_a:
26458+ AuLabel(diropq_a);
26459+ break;
26460+ case Opt_diropq_w:
26461+ AuLabel(diropq_w);
26462+ break;
26463+ case Opt_warn_perm:
26464+ AuLabel(warn_perm);
26465+ break;
26466+ case Opt_nowarn_perm:
26467+ AuLabel(nowarn_perm);
26468+ break;
1facf9fc 26469+ case Opt_verbose:
26470+ AuLabel(verbose);
26471+ break;
26472+ case Opt_noverbose:
26473+ AuLabel(noverbose);
26474+ break;
26475+ case Opt_sum:
26476+ AuLabel(sum);
26477+ break;
26478+ case Opt_nosum:
26479+ AuLabel(nosum);
26480+ break;
26481+ case Opt_wsum:
26482+ AuLabel(wsum);
26483+ break;
26484+ case Opt_wbr_create:
26485+ u.create = &opt->wbr_create;
26486+ AuDbg("create %d, %s\n", u.create->wbr_create,
26487+ au_optstr_wbr_create(u.create->wbr_create));
26488+ switch (u.create->wbr_create) {
26489+ case AuWbrCreate_MFSV:
26490+ case AuWbrCreate_PMFSV:
26491+ AuDbg("%d sec\n", u.create->mfs_second);
26492+ break;
26493+ case AuWbrCreate_MFSRR:
f2c43d5f 26494+ case AuWbrCreate_TDMFS:
1facf9fc 26495+ AuDbg("%llu watermark\n",
26496+ u.create->mfsrr_watermark);
26497+ break;
26498+ case AuWbrCreate_MFSRRV:
f2c43d5f 26499+ case AuWbrCreate_TDMFSV:
392086de 26500+ case AuWbrCreate_PMFSRRV:
1facf9fc 26501+ AuDbg("%llu watermark, %d sec\n",
26502+ u.create->mfsrr_watermark,
26503+ u.create->mfs_second);
26504+ break;
26505+ }
26506+ break;
26507+ case Opt_wbr_copyup:
26508+ AuDbg("copyup %d, %s\n", opt->wbr_copyup,
26509+ au_optstr_wbr_copyup(opt->wbr_copyup));
26510+ break;
076b876e
AM
26511+ case Opt_fhsm_sec:
26512+ AuDbg("fhsm_sec %u\n", opt->fhsm_second);
26513+ break;
8b6a4947
AM
26514+ case Opt_dirren:
26515+ AuLabel(dirren);
26516+ break;
26517+ case Opt_nodirren:
26518+ AuLabel(nodirren);
26519+ break;
c1595e42
JR
26520+ case Opt_acl:
26521+ AuLabel(acl);
26522+ break;
26523+ case Opt_noacl:
26524+ AuLabel(noacl);
26525+ break;
1facf9fc 26526+ default:
26527+ BUG();
26528+ }
26529+ opt++;
26530+ }
26531+#endif
26532+}
26533+
26534+void au_opts_free(struct au_opts *opts)
26535+{
26536+ struct au_opt *opt;
26537+
26538+ opt = opts->opt;
26539+ while (opt->type != Opt_tail) {
26540+ switch (opt->type) {
26541+ case Opt_add:
26542+ case Opt_append:
26543+ case Opt_prepend:
26544+ path_put(&opt->add.path);
26545+ break;
26546+ case Opt_del:
26547+ case Opt_idel:
26548+ path_put(&opt->del.h_path);
26549+ break;
26550+ case Opt_mod:
26551+ case Opt_imod:
26552+ dput(opt->mod.h_root);
26553+ break;
26554+ case Opt_xino:
26555+ fput(opt->xino.file);
26556+ break;
26557+ }
26558+ opt++;
26559+ }
26560+}
26561+
26562+static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
26563+ aufs_bindex_t bindex)
26564+{
26565+ int err;
26566+ struct au_opt_add *add = &opt->add;
26567+ char *p;
26568+
26569+ add->bindex = bindex;
1e00d052 26570+ add->perm = AuBrPerm_RO;
1facf9fc 26571+ add->pathname = opt_str;
26572+ p = strchr(opt_str, '=');
26573+ if (p) {
26574+ *p++ = 0;
26575+ if (*p)
26576+ add->perm = br_perm_val(p);
26577+ }
26578+
26579+ err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
26580+ if (!err) {
26581+ if (!p) {
26582+ add->perm = AuBrPerm_RO;
26583+ if (au_test_fs_rr(add->path.dentry->d_sb))
26584+ add->perm = AuBrPerm_RR;
2121bcd9 26585+ else if (!bindex && !(sb_flags & SB_RDONLY))
1facf9fc 26586+ add->perm = AuBrPerm_RW;
26587+ }
26588+ opt->type = Opt_add;
26589+ goto out;
26590+ }
4a4d8108 26591+ pr_err("lookup failed %s (%d)\n", add->pathname, err);
1facf9fc 26592+ err = -EINVAL;
26593+
4f0767ce 26594+out:
1facf9fc 26595+ return err;
26596+}
26597+
26598+static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
26599+{
26600+ int err;
26601+
26602+ del->pathname = args[0].from;
26603+ AuDbg("del path %s\n", del->pathname);
26604+
26605+ err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
26606+ if (unlikely(err))
4a4d8108 26607+ pr_err("lookup failed %s (%d)\n", del->pathname, err);
1facf9fc 26608+
26609+ return err;
26610+}
26611+
26612+#if 0 /* reserved for future use */
26613+static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
26614+ struct au_opt_del *del, substring_t args[])
26615+{
26616+ int err;
26617+ struct dentry *root;
26618+
26619+ err = -EINVAL;
26620+ root = sb->s_root;
26621+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26622+ if (bindex < 0 || au_sbbot(sb) < bindex) {
4a4d8108 26623+ pr_err("out of bounds, %d\n", bindex);
1facf9fc 26624+ goto out;
26625+ }
26626+
26627+ err = 0;
26628+ del->h_path.dentry = dget(au_h_dptr(root, bindex));
26629+ del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
26630+
4f0767ce 26631+out:
1facf9fc 26632+ aufs_read_unlock(root, !AuLock_IR);
26633+ return err;
26634+}
26635+#endif
26636+
4a4d8108
AM
26637+static int noinline_for_stack
26638+au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
1facf9fc 26639+{
26640+ int err;
26641+ struct path path;
26642+ char *p;
26643+
26644+ err = -EINVAL;
26645+ mod->path = args[0].from;
26646+ p = strchr(mod->path, '=');
26647+ if (unlikely(!p)) {
acd2b654 26648+ pr_err("no permission %s\n", args[0].from);
1facf9fc 26649+ goto out;
26650+ }
26651+
26652+ *p++ = 0;
26653+ err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
26654+ if (unlikely(err)) {
4a4d8108 26655+ pr_err("lookup failed %s (%d)\n", mod->path, err);
1facf9fc 26656+ goto out;
26657+ }
26658+
26659+ mod->perm = br_perm_val(p);
26660+ AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
26661+ mod->h_root = dget(path.dentry);
26662+ path_put(&path);
26663+
4f0767ce 26664+out:
1facf9fc 26665+ return err;
26666+}
26667+
26668+#if 0 /* reserved for future use */
26669+static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
26670+ struct au_opt_mod *mod, substring_t args[])
26671+{
26672+ int err;
26673+ struct dentry *root;
26674+
26675+ err = -EINVAL;
26676+ root = sb->s_root;
26677+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26678+ if (bindex < 0 || au_sbbot(sb) < bindex) {
4a4d8108 26679+ pr_err("out of bounds, %d\n", bindex);
1facf9fc 26680+ goto out;
26681+ }
26682+
26683+ err = 0;
26684+ mod->perm = br_perm_val(args[1].from);
26685+ AuDbg("mod path %s, perm 0x%x, %s\n",
26686+ mod->path, mod->perm, args[1].from);
26687+ mod->h_root = dget(au_h_dptr(root, bindex));
26688+
4f0767ce 26689+out:
1facf9fc 26690+ aufs_read_unlock(root, !AuLock_IR);
26691+ return err;
26692+}
26693+#endif
26694+
26695+static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
26696+ substring_t args[])
26697+{
26698+ int err;
26699+ struct file *file;
26700+
26701+ file = au_xino_create(sb, args[0].from, /*silent*/0);
26702+ err = PTR_ERR(file);
26703+ if (IS_ERR(file))
26704+ goto out;
26705+
26706+ err = -EINVAL;
2000de60 26707+ if (unlikely(file->f_path.dentry->d_sb == sb)) {
1facf9fc 26708+ fput(file);
4a4d8108 26709+ pr_err("%s must be outside\n", args[0].from);
1facf9fc 26710+ goto out;
26711+ }
26712+
26713+ err = 0;
26714+ xino->file = file;
26715+ xino->path = args[0].from;
26716+
4f0767ce 26717+out:
1facf9fc 26718+ return err;
26719+}
26720+
4a4d8108
AM
26721+static int noinline_for_stack
26722+au_opts_parse_xino_itrunc_path(struct super_block *sb,
26723+ struct au_opt_xino_itrunc *xino_itrunc,
26724+ substring_t args[])
1facf9fc 26725+{
26726+ int err;
5afbbe0d 26727+ aufs_bindex_t bbot, bindex;
1facf9fc 26728+ struct path path;
26729+ struct dentry *root;
26730+
26731+ err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
26732+ if (unlikely(err)) {
4a4d8108 26733+ pr_err("lookup failed %s (%d)\n", args[0].from, err);
1facf9fc 26734+ goto out;
26735+ }
26736+
26737+ xino_itrunc->bindex = -1;
26738+ root = sb->s_root;
26739+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d
AM
26740+ bbot = au_sbbot(sb);
26741+ for (bindex = 0; bindex <= bbot; bindex++) {
1facf9fc 26742+ if (au_h_dptr(root, bindex) == path.dentry) {
26743+ xino_itrunc->bindex = bindex;
26744+ break;
26745+ }
26746+ }
26747+ aufs_read_unlock(root, !AuLock_IR);
26748+ path_put(&path);
26749+
26750+ if (unlikely(xino_itrunc->bindex < 0)) {
4a4d8108 26751+ pr_err("no such branch %s\n", args[0].from);
1facf9fc 26752+ err = -EINVAL;
26753+ }
26754+
4f0767ce 26755+out:
1facf9fc 26756+ return err;
26757+}
26758+
26759+/* called without aufs lock */
26760+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
26761+{
26762+ int err, n, token;
26763+ aufs_bindex_t bindex;
26764+ unsigned char skipped;
26765+ struct dentry *root;
26766+ struct au_opt *opt, *opt_tail;
26767+ char *opt_str;
26768+ /* reduce the stack space */
26769+ union {
26770+ struct au_opt_xino_itrunc *xino_itrunc;
26771+ struct au_opt_wbr_create *create;
26772+ } u;
26773+ struct {
26774+ substring_t args[MAX_OPT_ARGS];
26775+ } *a;
26776+
26777+ err = -ENOMEM;
26778+ a = kmalloc(sizeof(*a), GFP_NOFS);
26779+ if (unlikely(!a))
26780+ goto out;
26781+
26782+ root = sb->s_root;
26783+ err = 0;
26784+ bindex = 0;
26785+ opt = opts->opt;
26786+ opt_tail = opt + opts->max_opt - 1;
26787+ opt->type = Opt_tail;
26788+ while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
26789+ err = -EINVAL;
26790+ skipped = 0;
26791+ token = match_token(opt_str, options, a->args);
26792+ switch (token) {
26793+ case Opt_br:
26794+ err = 0;
26795+ while (!err && (opt_str = strsep(&a->args[0].from, ":"))
26796+ && *opt_str) {
26797+ err = opt_add(opt, opt_str, opts->sb_flags,
26798+ bindex++);
26799+ if (unlikely(!err && ++opt > opt_tail)) {
26800+ err = -E2BIG;
26801+ break;
26802+ }
26803+ opt->type = Opt_tail;
26804+ skipped = 1;
26805+ }
26806+ break;
26807+ case Opt_add:
26808+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26809+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26810+ break;
26811+ }
26812+ bindex = n;
26813+ err = opt_add(opt, a->args[1].from, opts->sb_flags,
26814+ bindex);
26815+ if (!err)
26816+ opt->type = token;
26817+ break;
26818+ case Opt_append:
26819+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26820+ /*dummy bindex*/1);
26821+ if (!err)
26822+ opt->type = token;
26823+ break;
26824+ case Opt_prepend:
26825+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26826+ /*bindex*/0);
26827+ if (!err)
26828+ opt->type = token;
26829+ break;
26830+ case Opt_del:
26831+ err = au_opts_parse_del(&opt->del, a->args);
26832+ if (!err)
26833+ opt->type = token;
26834+ break;
26835+#if 0 /* reserved for future use */
26836+ case Opt_idel:
26837+ del->pathname = "(indexed)";
26838+ if (unlikely(match_int(&args[0], &n))) {
4a4d8108 26839+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26840+ break;
26841+ }
26842+ err = au_opts_parse_idel(sb, n, &opt->del, a->args);
26843+ if (!err)
26844+ opt->type = token;
26845+ break;
26846+#endif
26847+ case Opt_mod:
26848+ err = au_opts_parse_mod(&opt->mod, a->args);
26849+ if (!err)
26850+ opt->type = token;
26851+ break;
26852+#ifdef IMOD /* reserved for future use */
26853+ case Opt_imod:
26854+ u.mod->path = "(indexed)";
26855+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26856+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26857+ break;
26858+ }
26859+ err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
26860+ if (!err)
26861+ opt->type = token;
26862+ break;
26863+#endif
26864+ case Opt_xino:
26865+ err = au_opts_parse_xino(sb, &opt->xino, a->args);
26866+ if (!err)
26867+ opt->type = token;
26868+ break;
26869+
26870+ case Opt_trunc_xino_path:
26871+ err = au_opts_parse_xino_itrunc_path
26872+ (sb, &opt->xino_itrunc, a->args);
26873+ if (!err)
26874+ opt->type = token;
26875+ break;
26876+
26877+ case Opt_itrunc_xino:
26878+ u.xino_itrunc = &opt->xino_itrunc;
26879+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26880+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26881+ break;
26882+ }
26883+ u.xino_itrunc->bindex = n;
26884+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26885+ if (n < 0 || au_sbbot(sb) < n) {
4a4d8108 26886+ pr_err("out of bounds, %d\n", n);
1facf9fc 26887+ aufs_read_unlock(root, !AuLock_IR);
26888+ break;
26889+ }
26890+ aufs_read_unlock(root, !AuLock_IR);
26891+ err = 0;
26892+ opt->type = token;
26893+ break;
26894+
26895+ case Opt_dirwh:
26896+ if (unlikely(match_int(&a->args[0], &opt->dirwh)))
26897+ break;
26898+ err = 0;
26899+ opt->type = token;
26900+ break;
26901+
26902+ case Opt_rdcache:
027c5e7a
AM
26903+ if (unlikely(match_int(&a->args[0], &n))) {
26904+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26905+ break;
027c5e7a
AM
26906+ }
26907+ if (unlikely(n > AUFS_RDCACHE_MAX)) {
26908+ pr_err("rdcache must be smaller than %d\n",
26909+ AUFS_RDCACHE_MAX);
26910+ break;
26911+ }
26912+ opt->rdcache = n;
1facf9fc 26913+ err = 0;
26914+ opt->type = token;
26915+ break;
26916+ case Opt_rdblk:
26917+ if (unlikely(match_int(&a->args[0], &n)
1308ab2a 26918+ || n < 0
1facf9fc 26919+ || n > KMALLOC_MAX_SIZE)) {
4a4d8108 26920+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26921+ break;
26922+ }
1308ab2a 26923+ if (unlikely(n && n < NAME_MAX)) {
4a4d8108
AM
26924+ pr_err("rdblk must be larger than %d\n",
26925+ NAME_MAX);
1facf9fc 26926+ break;
26927+ }
26928+ opt->rdblk = n;
26929+ err = 0;
26930+ opt->type = token;
26931+ break;
26932+ case Opt_rdhash:
26933+ if (unlikely(match_int(&a->args[0], &n)
1308ab2a 26934+ || n < 0
1facf9fc 26935+ || n * sizeof(struct hlist_head)
26936+ > KMALLOC_MAX_SIZE)) {
4a4d8108 26937+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26938+ break;
26939+ }
26940+ opt->rdhash = n;
26941+ err = 0;
26942+ opt->type = token;
26943+ break;
26944+
26945+ case Opt_trunc_xino:
26946+ case Opt_notrunc_xino:
26947+ case Opt_noxino:
26948+ case Opt_trunc_xib:
26949+ case Opt_notrunc_xib:
dece6358
AM
26950+ case Opt_shwh:
26951+ case Opt_noshwh:
076b876e
AM
26952+ case Opt_dirperm1:
26953+ case Opt_nodirperm1:
1facf9fc 26954+ case Opt_plink:
26955+ case Opt_noplink:
26956+ case Opt_list_plink:
4a4d8108
AM
26957+ case Opt_dio:
26958+ case Opt_nodio:
1facf9fc 26959+ case Opt_diropq_a:
26960+ case Opt_diropq_w:
26961+ case Opt_warn_perm:
26962+ case Opt_nowarn_perm:
1facf9fc 26963+ case Opt_verbose:
26964+ case Opt_noverbose:
26965+ case Opt_sum:
26966+ case Opt_nosum:
26967+ case Opt_wsum:
dece6358
AM
26968+ case Opt_rdblk_def:
26969+ case Opt_rdhash_def:
8b6a4947
AM
26970+ case Opt_dirren:
26971+ case Opt_nodirren:
c1595e42
JR
26972+ case Opt_acl:
26973+ case Opt_noacl:
1facf9fc 26974+ err = 0;
26975+ opt->type = token;
26976+ break;
26977+
26978+ case Opt_udba:
26979+ opt->udba = udba_val(a->args[0].from);
26980+ if (opt->udba >= 0) {
26981+ err = 0;
26982+ opt->type = token;
26983+ } else
4a4d8108 26984+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26985+ break;
26986+
26987+ case Opt_wbr_create:
26988+ u.create = &opt->wbr_create;
26989+ u.create->wbr_create
26990+ = au_wbr_create_val(a->args[0].from, u.create);
26991+ if (u.create->wbr_create >= 0) {
26992+ err = 0;
26993+ opt->type = token;
26994+ } else
4a4d8108 26995+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26996+ break;
26997+ case Opt_wbr_copyup:
26998+ opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
26999+ if (opt->wbr_copyup >= 0) {
27000+ err = 0;
27001+ opt->type = token;
27002+ } else
4a4d8108 27003+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 27004+ break;
27005+
076b876e
AM
27006+ case Opt_fhsm_sec:
27007+ if (unlikely(match_int(&a->args[0], &n)
27008+ || n < 0)) {
27009+ pr_err("bad integer in %s\n", opt_str);
27010+ break;
27011+ }
27012+ if (sysaufs_brs) {
27013+ opt->fhsm_second = n;
27014+ opt->type = token;
27015+ } else
27016+ pr_warn("ignored %s\n", opt_str);
27017+ err = 0;
27018+ break;
27019+
1facf9fc 27020+ case Opt_ignore:
0c3ec466 27021+ pr_warn("ignored %s\n", opt_str);
1facf9fc 27022+ /*FALLTHROUGH*/
27023+ case Opt_ignore_silent:
27024+ skipped = 1;
27025+ err = 0;
27026+ break;
27027+ case Opt_err:
4a4d8108 27028+ pr_err("unknown option %s\n", opt_str);
1facf9fc 27029+ break;
27030+ }
27031+
27032+ if (!err && !skipped) {
27033+ if (unlikely(++opt > opt_tail)) {
27034+ err = -E2BIG;
27035+ opt--;
27036+ opt->type = Opt_tail;
27037+ break;
27038+ }
27039+ opt->type = Opt_tail;
27040+ }
27041+ }
27042+
e49925d1 27043+ au_kfree_rcu(a);
1facf9fc 27044+ dump_opts(opts);
27045+ if (unlikely(err))
27046+ au_opts_free(opts);
27047+
4f0767ce 27048+out:
1facf9fc 27049+ return err;
27050+}
27051+
27052+static int au_opt_wbr_create(struct super_block *sb,
27053+ struct au_opt_wbr_create *create)
27054+{
27055+ int err;
27056+ struct au_sbinfo *sbinfo;
27057+
dece6358
AM
27058+ SiMustWriteLock(sb);
27059+
1facf9fc 27060+ err = 1; /* handled */
27061+ sbinfo = au_sbi(sb);
27062+ if (sbinfo->si_wbr_create_ops->fin) {
27063+ err = sbinfo->si_wbr_create_ops->fin(sb);
27064+ if (!err)
27065+ err = 1;
27066+ }
27067+
27068+ sbinfo->si_wbr_create = create->wbr_create;
27069+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
27070+ switch (create->wbr_create) {
27071+ case AuWbrCreate_MFSRRV:
27072+ case AuWbrCreate_MFSRR:
f2c43d5f
AM
27073+ case AuWbrCreate_TDMFS:
27074+ case AuWbrCreate_TDMFSV:
392086de
AM
27075+ case AuWbrCreate_PMFSRR:
27076+ case AuWbrCreate_PMFSRRV:
1facf9fc 27077+ sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
27078+ /*FALLTHROUGH*/
27079+ case AuWbrCreate_MFS:
27080+ case AuWbrCreate_MFSV:
27081+ case AuWbrCreate_PMFS:
27082+ case AuWbrCreate_PMFSV:
e49829fe
JR
27083+ sbinfo->si_wbr_mfs.mfs_expire
27084+ = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
1facf9fc 27085+ break;
27086+ }
27087+
27088+ if (sbinfo->si_wbr_create_ops->init)
27089+ sbinfo->si_wbr_create_ops->init(sb); /* ignore */
27090+
27091+ return err;
27092+}
27093+
27094+/*
27095+ * returns,
27096+ * plus: processed without an error
27097+ * zero: unprocessed
27098+ */
27099+static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
27100+ struct au_opts *opts)
27101+{
27102+ int err;
27103+ struct au_sbinfo *sbinfo;
27104+
dece6358
AM
27105+ SiMustWriteLock(sb);
27106+
1facf9fc 27107+ err = 1; /* handled */
27108+ sbinfo = au_sbi(sb);
27109+ switch (opt->type) {
27110+ case Opt_udba:
27111+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27112+ sbinfo->si_mntflags |= opt->udba;
27113+ opts->given_udba |= opt->udba;
27114+ break;
27115+
27116+ case Opt_plink:
27117+ au_opt_set(sbinfo->si_mntflags, PLINK);
27118+ break;
27119+ case Opt_noplink:
27120+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
e49829fe 27121+ au_plink_put(sb, /*verbose*/1);
1facf9fc 27122+ au_opt_clr(sbinfo->si_mntflags, PLINK);
27123+ break;
27124+ case Opt_list_plink:
27125+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
27126+ au_plink_list(sb);
27127+ break;
27128+
4a4d8108
AM
27129+ case Opt_dio:
27130+ au_opt_set(sbinfo->si_mntflags, DIO);
27131+ au_fset_opts(opts->flags, REFRESH_DYAOP);
27132+ break;
27133+ case Opt_nodio:
27134+ au_opt_clr(sbinfo->si_mntflags, DIO);
27135+ au_fset_opts(opts->flags, REFRESH_DYAOP);
27136+ break;
27137+
076b876e
AM
27138+ case Opt_fhsm_sec:
27139+ au_fhsm_set(sbinfo, opt->fhsm_second);
27140+ break;
27141+
1facf9fc 27142+ case Opt_diropq_a:
27143+ au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27144+ break;
27145+ case Opt_diropq_w:
27146+ au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27147+ break;
27148+
27149+ case Opt_warn_perm:
27150+ au_opt_set(sbinfo->si_mntflags, WARN_PERM);
27151+ break;
27152+ case Opt_nowarn_perm:
27153+ au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
27154+ break;
27155+
1facf9fc 27156+ case Opt_verbose:
27157+ au_opt_set(sbinfo->si_mntflags, VERBOSE);
27158+ break;
27159+ case Opt_noverbose:
27160+ au_opt_clr(sbinfo->si_mntflags, VERBOSE);
27161+ break;
27162+
27163+ case Opt_sum:
27164+ au_opt_set(sbinfo->si_mntflags, SUM);
27165+ break;
27166+ case Opt_wsum:
27167+ au_opt_clr(sbinfo->si_mntflags, SUM);
27168+ au_opt_set(sbinfo->si_mntflags, SUM_W);
27169+ case Opt_nosum:
27170+ au_opt_clr(sbinfo->si_mntflags, SUM);
27171+ au_opt_clr(sbinfo->si_mntflags, SUM_W);
27172+ break;
27173+
27174+ case Opt_wbr_create:
27175+ err = au_opt_wbr_create(sb, &opt->wbr_create);
27176+ break;
27177+ case Opt_wbr_copyup:
27178+ sbinfo->si_wbr_copyup = opt->wbr_copyup;
27179+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
27180+ break;
27181+
27182+ case Opt_dirwh:
27183+ sbinfo->si_dirwh = opt->dirwh;
27184+ break;
27185+
27186+ case Opt_rdcache:
e49829fe
JR
27187+ sbinfo->si_rdcache
27188+ = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
1facf9fc 27189+ break;
27190+ case Opt_rdblk:
27191+ sbinfo->si_rdblk = opt->rdblk;
27192+ break;
dece6358
AM
27193+ case Opt_rdblk_def:
27194+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
27195+ break;
1facf9fc 27196+ case Opt_rdhash:
27197+ sbinfo->si_rdhash = opt->rdhash;
27198+ break;
dece6358
AM
27199+ case Opt_rdhash_def:
27200+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
27201+ break;
27202+
27203+ case Opt_shwh:
27204+ au_opt_set(sbinfo->si_mntflags, SHWH);
27205+ break;
27206+ case Opt_noshwh:
27207+ au_opt_clr(sbinfo->si_mntflags, SHWH);
27208+ break;
1facf9fc 27209+
076b876e
AM
27210+ case Opt_dirperm1:
27211+ au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27212+ break;
27213+ case Opt_nodirperm1:
27214+ au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27215+ break;
27216+
1facf9fc 27217+ case Opt_trunc_xino:
27218+ au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27219+ break;
27220+ case Opt_notrunc_xino:
27221+ au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27222+ break;
27223+
27224+ case Opt_trunc_xino_path:
27225+ case Opt_itrunc_xino:
acd2b654
AM
27226+ err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27227+ /*idx_begin*/0);
1facf9fc 27228+ if (!err)
27229+ err = 1;
27230+ break;
27231+
27232+ case Opt_trunc_xib:
27233+ au_fset_opts(opts->flags, TRUNC_XIB);
27234+ break;
27235+ case Opt_notrunc_xib:
27236+ au_fclr_opts(opts->flags, TRUNC_XIB);
27237+ break;
27238+
8b6a4947
AM
27239+ case Opt_dirren:
27240+ err = 1;
27241+ if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27242+ err = au_dr_opt_set(sb);
27243+ if (!err)
27244+ err = 1;
27245+ }
27246+ if (err == 1)
27247+ au_opt_set(sbinfo->si_mntflags, DIRREN);
27248+ break;
27249+ case Opt_nodirren:
27250+ err = 1;
27251+ if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27252+ err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27253+ DR_FLUSHED));
27254+ if (!err)
27255+ err = 1;
27256+ }
27257+ if (err == 1)
27258+ au_opt_clr(sbinfo->si_mntflags, DIRREN);
27259+ break;
27260+
c1595e42 27261+ case Opt_acl:
2121bcd9 27262+ sb->s_flags |= SB_POSIXACL;
c1595e42
JR
27263+ break;
27264+ case Opt_noacl:
2121bcd9 27265+ sb->s_flags &= ~SB_POSIXACL;
c1595e42
JR
27266+ break;
27267+
1facf9fc 27268+ default:
27269+ err = 0;
27270+ break;
27271+ }
27272+
27273+ return err;
27274+}
27275+
27276+/*
27277+ * returns tri-state.
27278+ * plus: processed without an error
27279+ * zero: unprocessed
27280+ * minus: error
27281+ */
27282+static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27283+ struct au_opts *opts)
27284+{
27285+ int err, do_refresh;
27286+
27287+ err = 0;
27288+ switch (opt->type) {
27289+ case Opt_append:
5afbbe0d 27290+ opt->add.bindex = au_sbbot(sb) + 1;
1facf9fc 27291+ if (opt->add.bindex < 0)
27292+ opt->add.bindex = 0;
27293+ goto add;
27294+ case Opt_prepend:
27295+ opt->add.bindex = 0;
f6b6e03d 27296+ add: /* indented label */
1facf9fc 27297+ case Opt_add:
27298+ err = au_br_add(sb, &opt->add,
27299+ au_ftest_opts(opts->flags, REMOUNT));
27300+ if (!err) {
27301+ err = 1;
027c5e7a 27302+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27303+ }
27304+ break;
27305+
27306+ case Opt_del:
27307+ case Opt_idel:
27308+ err = au_br_del(sb, &opt->del,
27309+ au_ftest_opts(opts->flags, REMOUNT));
27310+ if (!err) {
27311+ err = 1;
27312+ au_fset_opts(opts->flags, TRUNC_XIB);
027c5e7a 27313+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27314+ }
27315+ break;
27316+
27317+ case Opt_mod:
27318+ case Opt_imod:
27319+ err = au_br_mod(sb, &opt->mod,
27320+ au_ftest_opts(opts->flags, REMOUNT),
27321+ &do_refresh);
27322+ if (!err) {
27323+ err = 1;
027c5e7a
AM
27324+ if (do_refresh)
27325+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27326+ }
27327+ break;
27328+ }
1facf9fc 27329+ return err;
27330+}
27331+
27332+static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27333+ struct au_opt_xino **opt_xino,
27334+ struct au_opts *opts)
27335+{
27336+ int err;
1facf9fc 27337+
27338+ err = 0;
27339+ switch (opt->type) {
27340+ case Opt_xino:
062440b3
AM
27341+ err = au_xino_set(sb, &opt->xino,
27342+ !!au_ftest_opts(opts->flags, REMOUNT));
27343+ if (unlikely(err))
27344+ break;
27345+
27346+ *opt_xino = &opt->xino;
1facf9fc 27347+ break;
27348+
27349+ case Opt_noxino:
27350+ au_xino_clr(sb);
1facf9fc 27351+ *opt_xino = (void *)-1;
27352+ break;
27353+ }
27354+
27355+ return err;
27356+}
27357+
27358+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27359+ unsigned int pending)
27360+{
076b876e 27361+ int err, fhsm;
5afbbe0d 27362+ aufs_bindex_t bindex, bbot;
79b8bda9 27363+ unsigned char do_plink, skip, do_free, can_no_dreval;
1facf9fc 27364+ struct au_branch *br;
27365+ struct au_wbr *wbr;
79b8bda9 27366+ struct dentry *root, *dentry;
1facf9fc 27367+ struct inode *dir, *h_dir;
27368+ struct au_sbinfo *sbinfo;
27369+ struct au_hinode *hdir;
27370+
dece6358
AM
27371+ SiMustAnyLock(sb);
27372+
1facf9fc 27373+ sbinfo = au_sbi(sb);
27374+ AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27375+
2121bcd9 27376+ if (!(sb_flags & SB_RDONLY)) {
dece6358 27377+ if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
0c3ec466 27378+ pr_warn("first branch should be rw\n");
dece6358 27379+ if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
febd17d6 27380+ pr_warn_once("shwh should be used with ro\n");
dece6358 27381+ }
1facf9fc 27382+
4a4d8108 27383+ if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
1facf9fc 27384+ && !au_opt_test(sbinfo->si_mntflags, XINO))
febd17d6 27385+ pr_warn_once("udba=*notify requires xino\n");
1facf9fc 27386+
076b876e 27387+ if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
febd17d6
JR
27388+ pr_warn_once("dirperm1 breaks the protection"
27389+ " by the permission bits on the lower branch\n");
076b876e 27390+
1facf9fc 27391+ err = 0;
076b876e 27392+ fhsm = 0;
1facf9fc 27393+ root = sb->s_root;
5527c038 27394+ dir = d_inode(root);
1facf9fc 27395+ do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
79b8bda9
AM
27396+ can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27397+ UDBA_NONE);
5afbbe0d
AM
27398+ bbot = au_sbbot(sb);
27399+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
1facf9fc 27400+ skip = 0;
27401+ h_dir = au_h_iptr(dir, bindex);
27402+ br = au_sbr(sb, bindex);
1facf9fc 27403+
c1595e42
JR
27404+ if ((br->br_perm & AuBrAttr_ICEX)
27405+ && !h_dir->i_op->listxattr)
27406+ br->br_perm &= ~AuBrAttr_ICEX;
27407+#if 0
27408+ if ((br->br_perm & AuBrAttr_ICEX_SEC)
2121bcd9 27409+ && (au_br_sb(br)->s_flags & SB_NOSEC))
c1595e42
JR
27410+ br->br_perm &= ~AuBrAttr_ICEX_SEC;
27411+#endif
27412+
27413+ do_free = 0;
1facf9fc 27414+ wbr = br->br_wbr;
27415+ if (wbr)
27416+ wbr_wh_read_lock(wbr);
27417+
1e00d052 27418+ if (!au_br_writable(br->br_perm)) {
1facf9fc 27419+ do_free = !!wbr;
27420+ skip = (!wbr
27421+ || (!wbr->wbr_whbase
27422+ && !wbr->wbr_plink
27423+ && !wbr->wbr_orph));
1e00d052 27424+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 27425+ /* skip = (!br->br_whbase && !br->br_orph); */
27426+ skip = (!wbr || !wbr->wbr_whbase);
27427+ if (skip && wbr) {
27428+ if (do_plink)
27429+ skip = !!wbr->wbr_plink;
27430+ else
27431+ skip = !wbr->wbr_plink;
27432+ }
1e00d052 27433+ } else {
1facf9fc 27434+ /* skip = (br->br_whbase && br->br_ohph); */
27435+ skip = (wbr && wbr->wbr_whbase);
27436+ if (skip) {
27437+ if (do_plink)
27438+ skip = !!wbr->wbr_plink;
27439+ else
27440+ skip = !wbr->wbr_plink;
27441+ }
1facf9fc 27442+ }
27443+ if (wbr)
27444+ wbr_wh_read_unlock(wbr);
27445+
79b8bda9
AM
27446+ if (can_no_dreval) {
27447+ dentry = br->br_path.dentry;
27448+ spin_lock(&dentry->d_lock);
27449+ if (dentry->d_flags &
27450+ (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27451+ can_no_dreval = 0;
27452+ spin_unlock(&dentry->d_lock);
27453+ }
27454+
076b876e
AM
27455+ if (au_br_fhsm(br->br_perm)) {
27456+ fhsm++;
27457+ AuDebugOn(!br->br_fhsm);
27458+ }
27459+
1facf9fc 27460+ if (skip)
27461+ continue;
27462+
27463+ hdir = au_hi(dir, bindex);
5afbbe0d 27464+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 27465+ if (wbr)
27466+ wbr_wh_write_lock(wbr);
86dc4139 27467+ err = au_wh_init(br, sb);
1facf9fc 27468+ if (wbr)
27469+ wbr_wh_write_unlock(wbr);
5afbbe0d 27470+ au_hn_inode_unlock(hdir);
1facf9fc 27471+
27472+ if (!err && do_free) {
e49925d1 27473+ au_kfree_rcu(wbr);
1facf9fc 27474+ br->br_wbr = NULL;
27475+ }
27476+ }
27477+
79b8bda9
AM
27478+ if (can_no_dreval)
27479+ au_fset_si(sbinfo, NO_DREVAL);
27480+ else
27481+ au_fclr_si(sbinfo, NO_DREVAL);
27482+
c1595e42 27483+ if (fhsm >= 2) {
076b876e 27484+ au_fset_si(sbinfo, FHSM);
5afbbe0d 27485+ for (bindex = bbot; bindex >= 0; bindex--) {
c1595e42
JR
27486+ br = au_sbr(sb, bindex);
27487+ if (au_br_fhsm(br->br_perm)) {
27488+ au_fhsm_set_bottom(sb, bindex);
27489+ break;
27490+ }
27491+ }
27492+ } else {
076b876e 27493+ au_fclr_si(sbinfo, FHSM);
c1595e42
JR
27494+ au_fhsm_set_bottom(sb, -1);
27495+ }
076b876e 27496+
1facf9fc 27497+ return err;
27498+}
27499+
27500+int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27501+{
27502+ int err;
27503+ unsigned int tmp;
5afbbe0d 27504+ aufs_bindex_t bindex, bbot;
1facf9fc 27505+ struct au_opt *opt;
27506+ struct au_opt_xino *opt_xino, xino;
27507+ struct au_sbinfo *sbinfo;
027c5e7a 27508+ struct au_branch *br;
076b876e 27509+ struct inode *dir;
1facf9fc 27510+
dece6358
AM
27511+ SiMustWriteLock(sb);
27512+
1facf9fc 27513+ err = 0;
27514+ opt_xino = NULL;
27515+ opt = opts->opt;
27516+ while (err >= 0 && opt->type != Opt_tail)
27517+ err = au_opt_simple(sb, opt++, opts);
27518+ if (err > 0)
27519+ err = 0;
27520+ else if (unlikely(err < 0))
27521+ goto out;
27522+
27523+ /* disable xino and udba temporary */
27524+ sbinfo = au_sbi(sb);
27525+ tmp = sbinfo->si_mntflags;
27526+ au_opt_clr(sbinfo->si_mntflags, XINO);
27527+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27528+
27529+ opt = opts->opt;
27530+ while (err >= 0 && opt->type != Opt_tail)
27531+ err = au_opt_br(sb, opt++, opts);
27532+ if (err > 0)
27533+ err = 0;
27534+ else if (unlikely(err < 0))
27535+ goto out;
27536+
5afbbe0d
AM
27537+ bbot = au_sbbot(sb);
27538+ if (unlikely(bbot < 0)) {
1facf9fc 27539+ err = -EINVAL;
4a4d8108 27540+ pr_err("no branches\n");
1facf9fc 27541+ goto out;
27542+ }
27543+
27544+ if (au_opt_test(tmp, XINO))
27545+ au_opt_set(sbinfo->si_mntflags, XINO);
27546+ opt = opts->opt;
27547+ while (!err && opt->type != Opt_tail)
27548+ err = au_opt_xino(sb, opt++, &opt_xino, opts);
27549+ if (unlikely(err))
27550+ goto out;
27551+
27552+ err = au_opts_verify(sb, sb->s_flags, tmp);
27553+ if (unlikely(err))
27554+ goto out;
27555+
27556+ /* restore xino */
27557+ if (au_opt_test(tmp, XINO) && !opt_xino) {
27558+ xino.file = au_xino_def(sb);
27559+ err = PTR_ERR(xino.file);
27560+ if (IS_ERR(xino.file))
27561+ goto out;
27562+
27563+ err = au_xino_set(sb, &xino, /*remount*/0);
27564+ fput(xino.file);
27565+ if (unlikely(err))
27566+ goto out;
27567+ }
27568+
27569+ /* restore udba */
027c5e7a 27570+ tmp &= AuOptMask_UDBA;
1facf9fc 27571+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
027c5e7a 27572+ sbinfo->si_mntflags |= tmp;
5afbbe0d
AM
27573+ bbot = au_sbbot(sb);
27574+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
27575+ br = au_sbr(sb, bindex);
27576+ err = au_hnotify_reset_br(tmp, br, br->br_perm);
27577+ if (unlikely(err))
27578+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
27579+ bindex, err);
27580+ /* go on even if err */
27581+ }
4a4d8108 27582+ if (au_opt_test(tmp, UDBA_HNOTIFY)) {
5527c038 27583+ dir = d_inode(sb->s_root);
4a4d8108 27584+ au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
1facf9fc 27585+ }
27586+
4f0767ce 27587+out:
1facf9fc 27588+ return err;
27589+}
27590+
27591+int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27592+{
27593+ int err, rerr;
79b8bda9 27594+ unsigned char no_dreval;
1facf9fc 27595+ struct inode *dir;
27596+ struct au_opt_xino *opt_xino;
27597+ struct au_opt *opt;
27598+ struct au_sbinfo *sbinfo;
27599+
dece6358
AM
27600+ SiMustWriteLock(sb);
27601+
8b6a4947
AM
27602+ err = au_dr_opt_flush(sb);
27603+ if (unlikely(err))
27604+ goto out;
27605+ au_fset_opts(opts->flags, DR_FLUSHED);
27606+
5527c038 27607+ dir = d_inode(sb->s_root);
1facf9fc 27608+ sbinfo = au_sbi(sb);
1facf9fc 27609+ opt_xino = NULL;
27610+ opt = opts->opt;
27611+ while (err >= 0 && opt->type != Opt_tail) {
27612+ err = au_opt_simple(sb, opt, opts);
27613+ if (!err)
27614+ err = au_opt_br(sb, opt, opts);
27615+ if (!err)
27616+ err = au_opt_xino(sb, opt, &opt_xino, opts);
27617+ opt++;
27618+ }
27619+ if (err > 0)
27620+ err = 0;
27621+ AuTraceErr(err);
27622+ /* go on even err */
27623+
79b8bda9 27624+ no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
1facf9fc 27625+ rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27626+ if (unlikely(rerr && !err))
27627+ err = rerr;
27628+
79b8bda9 27629+ if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
b95c5147 27630+ au_fset_opts(opts->flags, REFRESH_IDOP);
79b8bda9 27631+
1facf9fc 27632+ if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27633+ rerr = au_xib_trunc(sb);
27634+ if (unlikely(rerr && !err))
27635+ err = rerr;
27636+ }
27637+
27638+ /* will be handled by the caller */
027c5e7a 27639+ if (!au_ftest_opts(opts->flags, REFRESH)
79b8bda9
AM
27640+ && (opts->given_udba
27641+ || au_opt_test(sbinfo->si_mntflags, XINO)
b95c5147 27642+ || au_ftest_opts(opts->flags, REFRESH_IDOP)
79b8bda9 27643+ ))
027c5e7a 27644+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27645+
27646+ AuDbg("status 0x%x\n", opts->flags);
8b6a4947
AM
27647+
27648+out:
1facf9fc 27649+ return err;
27650+}
27651+
27652+/* ---------------------------------------------------------------------- */
27653+
27654+unsigned int au_opt_udba(struct super_block *sb)
27655+{
27656+ return au_mntflags(sb) & AuOptMask_UDBA;
27657+}
7f207e10
AM
27658diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27659--- /usr/share/empty/fs/aufs/opts.h 1970-01-01 01:00:00.000000000 +0100
e49925d1 27660+++ linux/fs/aufs/opts.h 2019-01-28 14:36:12.222418047 +0100
062440b3
AM
27661@@ -0,0 +1,225 @@
27662+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 27663+/*
b00004a5 27664+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 27665+ *
27666+ * This program, aufs is free software; you can redistribute it and/or modify
27667+ * it under the terms of the GNU General Public License as published by
27668+ * the Free Software Foundation; either version 2 of the License, or
27669+ * (at your option) any later version.
dece6358
AM
27670+ *
27671+ * This program is distributed in the hope that it will be useful,
27672+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27673+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27674+ * GNU General Public License for more details.
27675+ *
27676+ * You should have received a copy of the GNU General Public License
523b37e3 27677+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 27678+ */
27679+
27680+/*
27681+ * mount options/flags
27682+ */
27683+
27684+#ifndef __AUFS_OPTS_H__
27685+#define __AUFS_OPTS_H__
27686+
27687+#ifdef __KERNEL__
27688+
dece6358 27689+#include <linux/path.h>
1facf9fc 27690+
dece6358 27691+struct file;
dece6358 27692+
1facf9fc 27693+/* ---------------------------------------------------------------------- */
27694+
27695+/* mount flags */
27696+#define AuOpt_XINO 1 /* external inode number bitmap
27697+ and translation table */
27698+#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
27699+#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
27700+#define AuOpt_UDBA_REVAL (1 << 3)
4a4d8108 27701+#define AuOpt_UDBA_HNOTIFY (1 << 4)
dece6358
AM
27702+#define AuOpt_SHWH (1 << 5) /* show whiteout */
27703+#define AuOpt_PLINK (1 << 6) /* pseudo-link */
076b876e
AM
27704+#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
27705+ bits */
dece6358
AM
27706+#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
27707+#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
27708+#define AuOpt_SUM_W (1 << 11) /* unimplemented */
27709+#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
27710+#define AuOpt_VERBOSE (1 << 13) /* busy inode when del-branch */
4a4d8108 27711+#define AuOpt_DIO (1 << 14) /* direct io */
8b6a4947 27712+#define AuOpt_DIRREN (1 << 15) /* directory rename */
1facf9fc 27713+
4a4d8108
AM
27714+#ifndef CONFIG_AUFS_HNOTIFY
27715+#undef AuOpt_UDBA_HNOTIFY
27716+#define AuOpt_UDBA_HNOTIFY 0
1facf9fc 27717+#endif
8b6a4947
AM
27718+#ifndef CONFIG_AUFS_DIRREN
27719+#undef AuOpt_DIRREN
27720+#define AuOpt_DIRREN 0
27721+#endif
dece6358
AM
27722+#ifndef CONFIG_AUFS_SHWH
27723+#undef AuOpt_SHWH
27724+#define AuOpt_SHWH 0
27725+#endif
1facf9fc 27726+
27727+#define AuOpt_Def (AuOpt_XINO \
27728+ | AuOpt_UDBA_REVAL \
27729+ | AuOpt_PLINK \
27730+ /* | AuOpt_DIRPERM1 */ \
27731+ | AuOpt_WARN_PERM)
27732+#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27733+ | AuOpt_UDBA_REVAL \
4a4d8108 27734+ | AuOpt_UDBA_HNOTIFY)
1facf9fc 27735+
27736+#define au_opt_test(flags, name) (flags & AuOpt_##name)
27737+#define au_opt_set(flags, name) do { \
27738+ BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27739+ ((flags) |= AuOpt_##name); \
27740+} while (0)
27741+#define au_opt_set_udba(flags, name) do { \
27742+ (flags) &= ~AuOptMask_UDBA; \
27743+ ((flags) |= AuOpt_##name); \
27744+} while (0)
7f207e10
AM
27745+#define au_opt_clr(flags, name) do { \
27746+ ((flags) &= ~AuOpt_##name); \
27747+} while (0)
1facf9fc 27748+
e49829fe
JR
27749+static inline unsigned int au_opts_plink(unsigned int mntflags)
27750+{
27751+#ifdef CONFIG_PROC_FS
27752+ return mntflags;
27753+#else
27754+ return mntflags & ~AuOpt_PLINK;
27755+#endif
27756+}
27757+
1facf9fc 27758+/* ---------------------------------------------------------------------- */
27759+
27760+/* policies to select one among multiple writable branches */
27761+enum {
27762+ AuWbrCreate_TDP, /* top down parent */
27763+ AuWbrCreate_RR, /* round robin */
27764+ AuWbrCreate_MFS, /* most free space */
27765+ AuWbrCreate_MFSV, /* mfs with seconds */
27766+ AuWbrCreate_MFSRR, /* mfs then rr */
27767+ AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
f2c43d5f
AM
27768+ AuWbrCreate_TDMFS, /* top down regardless parent and mfs */
27769+ AuWbrCreate_TDMFSV, /* top down regardless parent and mfs */
1facf9fc 27770+ AuWbrCreate_PMFS, /* parent and mfs */
27771+ AuWbrCreate_PMFSV, /* parent and mfs with seconds */
392086de
AM
27772+ AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
27773+ AuWbrCreate_PMFSRRV, /* plus seconds */
1facf9fc 27774+
27775+ AuWbrCreate_Def = AuWbrCreate_TDP
27776+};
27777+
27778+enum {
27779+ AuWbrCopyup_TDP, /* top down parent */
27780+ AuWbrCopyup_BUP, /* bottom up parent */
27781+ AuWbrCopyup_BU, /* bottom up */
27782+
27783+ AuWbrCopyup_Def = AuWbrCopyup_TDP
27784+};
27785+
27786+/* ---------------------------------------------------------------------- */
27787+
27788+struct au_opt_add {
27789+ aufs_bindex_t bindex;
27790+ char *pathname;
27791+ int perm;
27792+ struct path path;
27793+};
27794+
27795+struct au_opt_del {
27796+ char *pathname;
27797+ struct path h_path;
27798+};
27799+
27800+struct au_opt_mod {
27801+ char *path;
27802+ int perm;
27803+ struct dentry *h_root;
27804+};
27805+
27806+struct au_opt_xino {
27807+ char *path;
27808+ struct file *file;
27809+};
27810+
27811+struct au_opt_xino_itrunc {
27812+ aufs_bindex_t bindex;
27813+};
27814+
27815+struct au_opt_wbr_create {
27816+ int wbr_create;
27817+ int mfs_second;
27818+ unsigned long long mfsrr_watermark;
27819+};
27820+
27821+struct au_opt {
27822+ int type;
27823+ union {
27824+ struct au_opt_xino xino;
27825+ struct au_opt_xino_itrunc xino_itrunc;
27826+ struct au_opt_add add;
27827+ struct au_opt_del del;
27828+ struct au_opt_mod mod;
27829+ int dirwh;
27830+ int rdcache;
27831+ unsigned int rdblk;
27832+ unsigned int rdhash;
27833+ int udba;
27834+ struct au_opt_wbr_create wbr_create;
27835+ int wbr_copyup;
076b876e 27836+ unsigned int fhsm_second;
1facf9fc 27837+ };
27838+};
27839+
27840+/* opts flags */
27841+#define AuOpts_REMOUNT 1
027c5e7a
AM
27842+#define AuOpts_REFRESH (1 << 1)
27843+#define AuOpts_TRUNC_XIB (1 << 2)
27844+#define AuOpts_REFRESH_DYAOP (1 << 3)
b95c5147 27845+#define AuOpts_REFRESH_IDOP (1 << 4)
8b6a4947 27846+#define AuOpts_DR_FLUSHED (1 << 5)
1facf9fc 27847+#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
7f207e10
AM
27848+#define au_fset_opts(flags, name) \
27849+ do { (flags) |= AuOpts_##name; } while (0)
27850+#define au_fclr_opts(flags, name) \
27851+ do { (flags) &= ~AuOpts_##name; } while (0)
1facf9fc 27852+
8b6a4947
AM
27853+#ifndef CONFIG_AUFS_DIRREN
27854+#undef AuOpts_DR_FLUSHED
27855+#define AuOpts_DR_FLUSHED 0
27856+#endif
27857+
1facf9fc 27858+struct au_opts {
27859+ struct au_opt *opt;
27860+ int max_opt;
27861+
27862+ unsigned int given_udba;
27863+ unsigned int flags;
27864+ unsigned long sb_flags;
27865+};
27866+
27867+/* ---------------------------------------------------------------------- */
27868+
7e9cd9fe 27869+/* opts.c */
076b876e 27870+void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
1facf9fc 27871+const char *au_optstr_udba(int udba);
27872+const char *au_optstr_wbr_copyup(int wbr_copyup);
27873+const char *au_optstr_wbr_create(int wbr_create);
27874+
27875+void au_opts_free(struct au_opts *opts);
3c1bdaff 27876+struct super_block;
1facf9fc 27877+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
27878+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27879+ unsigned int pending);
27880+int au_opts_mount(struct super_block *sb, struct au_opts *opts);
27881+int au_opts_remount(struct super_block *sb, struct au_opts *opts);
27882+
27883+unsigned int au_opt_udba(struct super_block *sb);
27884+
1facf9fc 27885+#endif /* __KERNEL__ */
27886+#endif /* __AUFS_OPTS_H__ */
7f207e10
AM
27887diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
27888--- /usr/share/empty/fs/aufs/plink.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 27889+++ linux/fs/aufs/plink.c 2019-01-28 14:36:12.222418047 +0100
062440b3 27890@@ -0,0 +1,516 @@
cd7a4cd9 27891+// SPDX-License-Identifier: GPL-2.0
1facf9fc 27892+/*
b00004a5 27893+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 27894+ *
27895+ * This program, aufs is free software; you can redistribute it and/or modify
27896+ * it under the terms of the GNU General Public License as published by
27897+ * the Free Software Foundation; either version 2 of the License, or
27898+ * (at your option) any later version.
dece6358
AM
27899+ *
27900+ * This program is distributed in the hope that it will be useful,
27901+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27902+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27903+ * GNU General Public License for more details.
27904+ *
27905+ * You should have received a copy of the GNU General Public License
523b37e3 27906+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 27907+ */
27908+
27909+/*
27910+ * pseudo-link
27911+ */
27912+
27913+#include "aufs.h"
27914+
27915+/*
e49829fe 27916+ * the pseudo-link maintenance mode.
1facf9fc 27917+ * during a user process maintains the pseudo-links,
27918+ * prohibit adding a new plink and branch manipulation.
e49829fe
JR
27919+ *
27920+ * Flags
27921+ * NOPLM:
27922+ * For entry functions which will handle plink, and i_mutex is already held
27923+ * in VFS.
27924+ * They cannot wait and should return an error at once.
27925+ * Callers has to check the error.
27926+ * NOPLMW:
27927+ * For entry functions which will handle plink, but i_mutex is not held
27928+ * in VFS.
27929+ * They can wait the plink maintenance mode to finish.
27930+ *
27931+ * They behave like F_SETLK and F_SETLKW.
27932+ * If the caller never handle plink, then both flags are unnecessary.
1facf9fc 27933+ */
e49829fe
JR
27934+
27935+int au_plink_maint(struct super_block *sb, int flags)
1facf9fc 27936+{
e49829fe
JR
27937+ int err;
27938+ pid_t pid, ppid;
f0c0a007 27939+ struct task_struct *parent, *prev;
e49829fe 27940+ struct au_sbinfo *sbi;
dece6358
AM
27941+
27942+ SiMustAnyLock(sb);
27943+
e49829fe
JR
27944+ err = 0;
27945+ if (!au_opt_test(au_mntflags(sb), PLINK))
27946+ goto out;
27947+
27948+ sbi = au_sbi(sb);
27949+ pid = sbi->si_plink_maint_pid;
27950+ if (!pid || pid == current->pid)
27951+ goto out;
27952+
27953+ /* todo: it highly depends upon /sbin/mount.aufs */
f0c0a007
AM
27954+ prev = NULL;
27955+ parent = current;
27956+ ppid = 0;
e49829fe 27957+ rcu_read_lock();
f0c0a007
AM
27958+ while (1) {
27959+ parent = rcu_dereference(parent->real_parent);
27960+ if (parent == prev)
27961+ break;
27962+ ppid = task_pid_vnr(parent);
27963+ if (pid == ppid) {
27964+ rcu_read_unlock();
27965+ goto out;
27966+ }
27967+ prev = parent;
27968+ }
e49829fe 27969+ rcu_read_unlock();
e49829fe
JR
27970+
27971+ if (au_ftest_lock(flags, NOPLMW)) {
027c5e7a
AM
27972+ /* if there is no i_mutex lock in VFS, we don't need to wait */
27973+ /* AuDebugOn(!lockdep_depth(current)); */
e49829fe
JR
27974+ while (sbi->si_plink_maint_pid) {
27975+ si_read_unlock(sb);
27976+ /* gave up wake_up_bit() */
27977+ wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
27978+
27979+ if (au_ftest_lock(flags, FLUSH))
27980+ au_nwt_flush(&sbi->si_nowait);
27981+ si_noflush_read_lock(sb);
27982+ }
27983+ } else if (au_ftest_lock(flags, NOPLM)) {
27984+ AuDbg("ppid %d, pid %d\n", ppid, pid);
27985+ err = -EAGAIN;
27986+ }
27987+
27988+out:
27989+ return err;
4a4d8108
AM
27990+}
27991+
e49829fe 27992+void au_plink_maint_leave(struct au_sbinfo *sbinfo)
4a4d8108 27993+{
4a4d8108 27994+ spin_lock(&sbinfo->si_plink_maint_lock);
027c5e7a 27995+ sbinfo->si_plink_maint_pid = 0;
4a4d8108 27996+ spin_unlock(&sbinfo->si_plink_maint_lock);
027c5e7a 27997+ wake_up_all(&sbinfo->si_plink_wq);
4a4d8108
AM
27998+}
27999+
e49829fe 28000+int au_plink_maint_enter(struct super_block *sb)
4a4d8108
AM
28001+{
28002+ int err;
4a4d8108
AM
28003+ struct au_sbinfo *sbinfo;
28004+
28005+ err = 0;
4a4d8108
AM
28006+ sbinfo = au_sbi(sb);
28007+ /* make sure i am the only one in this fs */
e49829fe
JR
28008+ si_write_lock(sb, AuLock_FLUSH);
28009+ if (au_opt_test(au_mntflags(sb), PLINK)) {
28010+ spin_lock(&sbinfo->si_plink_maint_lock);
28011+ if (!sbinfo->si_plink_maint_pid)
28012+ sbinfo->si_plink_maint_pid = current->pid;
28013+ else
28014+ err = -EBUSY;
28015+ spin_unlock(&sbinfo->si_plink_maint_lock);
28016+ }
4a4d8108
AM
28017+ si_write_unlock(sb);
28018+
28019+ return err;
1facf9fc 28020+}
28021+
28022+/* ---------------------------------------------------------------------- */
28023+
1facf9fc 28024+#ifdef CONFIG_AUFS_DEBUG
28025+void au_plink_list(struct super_block *sb)
28026+{
86dc4139 28027+ int i;
1facf9fc 28028+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28029+ struct hlist_bl_head *hbl;
28030+ struct hlist_bl_node *pos;
5afbbe0d 28031+ struct au_icntnr *icntnr;
1facf9fc 28032+
dece6358
AM
28033+ SiMustAnyLock(sb);
28034+
1facf9fc 28035+ sbinfo = au_sbi(sb);
28036+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28037+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28038+
86dc4139 28039+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
28040+ hbl = sbinfo->si_plink + i;
28041+ hlist_bl_lock(hbl);
28042+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
5afbbe0d 28043+ AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
8b6a4947 28044+ hlist_bl_unlock(hbl);
86dc4139 28045+ }
1facf9fc 28046+}
28047+#endif
28048+
28049+/* is the inode pseudo-linked? */
28050+int au_plink_test(struct inode *inode)
28051+{
86dc4139 28052+ int found, i;
1facf9fc 28053+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28054+ struct hlist_bl_head *hbl;
28055+ struct hlist_bl_node *pos;
5afbbe0d 28056+ struct au_icntnr *icntnr;
1facf9fc 28057+
28058+ sbinfo = au_sbi(inode->i_sb);
dece6358 28059+ AuRwMustAnyLock(&sbinfo->si_rwsem);
1facf9fc 28060+ AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
e49829fe 28061+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
1facf9fc 28062+
28063+ found = 0;
86dc4139 28064+ i = au_plink_hash(inode->i_ino);
8b6a4947
AM
28065+ hbl = sbinfo->si_plink + i;
28066+ hlist_bl_lock(hbl);
28067+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
5afbbe0d 28068+ if (&icntnr->vfs_inode == inode) {
1facf9fc 28069+ found = 1;
28070+ break;
28071+ }
8b6a4947 28072+ hlist_bl_unlock(hbl);
1facf9fc 28073+ return found;
28074+}
28075+
28076+/* ---------------------------------------------------------------------- */
28077+
28078+/*
28079+ * generate a name for plink.
28080+ * the file will be stored under AUFS_WH_PLINKDIR.
28081+ */
28082+/* 20 is max digits length of ulong 64 */
28083+#define PLINK_NAME_LEN ((20 + 1) * 2)
28084+
28085+static int plink_name(char *name, int len, struct inode *inode,
28086+ aufs_bindex_t bindex)
28087+{
28088+ int rlen;
28089+ struct inode *h_inode;
28090+
28091+ h_inode = au_h_iptr(inode, bindex);
28092+ rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
28093+ return rlen;
28094+}
28095+
7f207e10
AM
28096+struct au_do_plink_lkup_args {
28097+ struct dentry **errp;
28098+ struct qstr *tgtname;
28099+ struct dentry *h_parent;
28100+ struct au_branch *br;
28101+};
28102+
28103+static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
28104+ struct dentry *h_parent,
28105+ struct au_branch *br)
28106+{
28107+ struct dentry *h_dentry;
febd17d6 28108+ struct inode *h_inode;
7f207e10 28109+
febd17d6 28110+ h_inode = d_inode(h_parent);
be118d29 28111+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
b4510431 28112+ h_dentry = vfsub_lkup_one(tgtname, h_parent);
3c1bdaff 28113+ inode_unlock_shared(h_inode);
7f207e10
AM
28114+ return h_dentry;
28115+}
28116+
28117+static void au_call_do_plink_lkup(void *args)
28118+{
28119+ struct au_do_plink_lkup_args *a = args;
28120+ *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
28121+}
28122+
1facf9fc 28123+/* lookup the plink-ed @inode under the branch at @bindex */
28124+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
28125+{
28126+ struct dentry *h_dentry, *h_parent;
28127+ struct au_branch *br;
7f207e10 28128+ int wkq_err;
1facf9fc 28129+ char a[PLINK_NAME_LEN];
0c3ec466 28130+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 28131+
e49829fe
JR
28132+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28133+
1facf9fc 28134+ br = au_sbr(inode->i_sb, bindex);
28135+ h_parent = br->br_wbr->wbr_plink;
1facf9fc 28136+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28137+
2dfbb274 28138+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
7f207e10
AM
28139+ struct au_do_plink_lkup_args args = {
28140+ .errp = &h_dentry,
28141+ .tgtname = &tgtname,
28142+ .h_parent = h_parent,
28143+ .br = br
28144+ };
28145+
28146+ wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
28147+ if (unlikely(wkq_err))
28148+ h_dentry = ERR_PTR(wkq_err);
28149+ } else
28150+ h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
28151+
1facf9fc 28152+ return h_dentry;
28153+}
28154+
28155+/* create a pseudo-link */
28156+static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
28157+ struct dentry *h_dentry, struct au_branch *br)
28158+{
28159+ int err;
28160+ struct path h_path = {
86dc4139 28161+ .mnt = au_br_mnt(br)
1facf9fc 28162+ };
523b37e3 28163+ struct inode *h_dir, *delegated;
1facf9fc 28164+
5527c038 28165+ h_dir = d_inode(h_parent);
febd17d6 28166+ inode_lock_nested(h_dir, AuLsc_I_CHILD2);
4f0767ce 28167+again:
b4510431 28168+ h_path.dentry = vfsub_lkup_one(tgt, h_parent);
1facf9fc 28169+ err = PTR_ERR(h_path.dentry);
28170+ if (IS_ERR(h_path.dentry))
28171+ goto out;
28172+
28173+ err = 0;
28174+ /* wh.plink dir is not monitored */
7f207e10 28175+ /* todo: is it really safe? */
5527c038
JR
28176+ if (d_is_positive(h_path.dentry)
28177+ && d_inode(h_path.dentry) != d_inode(h_dentry)) {
523b37e3
AM
28178+ delegated = NULL;
28179+ err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28180+ if (unlikely(err == -EWOULDBLOCK)) {
28181+ pr_warn("cannot retry for NFSv4 delegation"
28182+ " for an internal unlink\n");
28183+ iput(delegated);
28184+ }
1facf9fc 28185+ dput(h_path.dentry);
28186+ h_path.dentry = NULL;
28187+ if (!err)
28188+ goto again;
28189+ }
5527c038 28190+ if (!err && d_is_negative(h_path.dentry)) {
523b37e3
AM
28191+ delegated = NULL;
28192+ err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28193+ if (unlikely(err == -EWOULDBLOCK)) {
28194+ pr_warn("cannot retry for NFSv4 delegation"
28195+ " for an internal link\n");
28196+ iput(delegated);
28197+ }
28198+ }
1facf9fc 28199+ dput(h_path.dentry);
28200+
4f0767ce 28201+out:
febd17d6 28202+ inode_unlock(h_dir);
1facf9fc 28203+ return err;
28204+}
28205+
28206+struct do_whplink_args {
28207+ int *errp;
28208+ struct qstr *tgt;
28209+ struct dentry *h_parent;
28210+ struct dentry *h_dentry;
28211+ struct au_branch *br;
28212+};
28213+
28214+static void call_do_whplink(void *args)
28215+{
28216+ struct do_whplink_args *a = args;
28217+ *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
28218+}
28219+
28220+static int whplink(struct dentry *h_dentry, struct inode *inode,
28221+ aufs_bindex_t bindex, struct au_branch *br)
28222+{
28223+ int err, wkq_err;
28224+ struct au_wbr *wbr;
28225+ struct dentry *h_parent;
1facf9fc 28226+ char a[PLINK_NAME_LEN];
0c3ec466 28227+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 28228+
28229+ wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
28230+ h_parent = wbr->wbr_plink;
1facf9fc 28231+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28232+
28233+ /* always superio. */
2dfbb274 28234+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
1facf9fc 28235+ struct do_whplink_args args = {
28236+ .errp = &err,
28237+ .tgt = &tgtname,
28238+ .h_parent = h_parent,
28239+ .h_dentry = h_dentry,
28240+ .br = br
28241+ };
28242+ wkq_err = au_wkq_wait(call_do_whplink, &args);
28243+ if (unlikely(wkq_err))
28244+ err = wkq_err;
28245+ } else
28246+ err = do_whplink(&tgtname, h_parent, h_dentry, br);
1facf9fc 28247+
28248+ return err;
28249+}
28250+
1facf9fc 28251+/*
28252+ * create a new pseudo-link for @h_dentry on @bindex.
28253+ * the linked inode is held in aufs @inode.
28254+ */
28255+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28256+ struct dentry *h_dentry)
28257+{
28258+ struct super_block *sb;
28259+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28260+ struct hlist_bl_head *hbl;
28261+ struct hlist_bl_node *pos;
5afbbe0d 28262+ struct au_icntnr *icntnr;
86dc4139 28263+ int found, err, cnt, i;
1facf9fc 28264+
28265+ sb = inode->i_sb;
28266+ sbinfo = au_sbi(sb);
28267+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28268+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28269+
86dc4139 28270+ found = au_plink_test(inode);
4a4d8108 28271+ if (found)
1facf9fc 28272+ return;
4a4d8108 28273+
86dc4139 28274+ i = au_plink_hash(inode->i_ino);
8b6a4947 28275+ hbl = sbinfo->si_plink + i;
5afbbe0d 28276+ au_igrab(inode);
1facf9fc 28277+
8b6a4947
AM
28278+ hlist_bl_lock(hbl);
28279+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
5afbbe0d 28280+ if (&icntnr->vfs_inode == inode) {
4a4d8108
AM
28281+ found = 1;
28282+ break;
28283+ }
1facf9fc 28284+ }
5afbbe0d
AM
28285+ if (!found) {
28286+ icntnr = container_of(inode, struct au_icntnr, vfs_inode);
8b6a4947 28287+ hlist_bl_add_head(&icntnr->plink, hbl);
5afbbe0d 28288+ }
8b6a4947 28289+ hlist_bl_unlock(hbl);
4a4d8108 28290+ if (!found) {
8b6a4947 28291+ cnt = au_hbl_count(hbl);
acd2b654 28292+#define msg "unexpectedly unbalanced or too many pseudo-links"
86dc4139
AM
28293+ if (cnt > AUFS_PLINK_WARN)
28294+ AuWarn1(msg ", %d\n", cnt);
28295+#undef msg
1facf9fc 28296+ err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
5afbbe0d
AM
28297+ if (unlikely(err)) {
28298+ pr_warn("err %d, damaged pseudo link.\n", err);
8b6a4947 28299+ au_hbl_del(&icntnr->plink, hbl);
5afbbe0d 28300+ iput(&icntnr->vfs_inode);
4a4d8108 28301+ }
5afbbe0d
AM
28302+ } else
28303+ iput(&icntnr->vfs_inode);
1facf9fc 28304+}
28305+
28306+/* free all plinks */
e49829fe 28307+void au_plink_put(struct super_block *sb, int verbose)
1facf9fc 28308+{
86dc4139 28309+ int i, warned;
1facf9fc 28310+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28311+ struct hlist_bl_head *hbl;
28312+ struct hlist_bl_node *pos, *tmp;
5afbbe0d 28313+ struct au_icntnr *icntnr;
1facf9fc 28314+
dece6358
AM
28315+ SiMustWriteLock(sb);
28316+
1facf9fc 28317+ sbinfo = au_sbi(sb);
28318+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28319+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28320+
1facf9fc 28321+ /* no spin_lock since sbinfo is write-locked */
86dc4139
AM
28322+ warned = 0;
28323+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
28324+ hbl = sbinfo->si_plink + i;
28325+ if (!warned && verbose && !hlist_bl_empty(hbl)) {
86dc4139
AM
28326+ pr_warn("pseudo-link is not flushed");
28327+ warned = 1;
28328+ }
8b6a4947 28329+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
5afbbe0d 28330+ iput(&icntnr->vfs_inode);
8b6a4947 28331+ INIT_HLIST_BL_HEAD(hbl);
86dc4139 28332+ }
1facf9fc 28333+}
28334+
e49829fe
JR
28335+void au_plink_clean(struct super_block *sb, int verbose)
28336+{
28337+ struct dentry *root;
28338+
28339+ root = sb->s_root;
28340+ aufs_write_lock(root);
28341+ if (au_opt_test(au_mntflags(sb), PLINK))
28342+ au_plink_put(sb, verbose);
28343+ aufs_write_unlock(root);
28344+}
28345+
86dc4139
AM
28346+static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28347+{
28348+ int do_put;
5afbbe0d 28349+ aufs_bindex_t btop, bbot, bindex;
86dc4139
AM
28350+
28351+ do_put = 0;
5afbbe0d
AM
28352+ btop = au_ibtop(inode);
28353+ bbot = au_ibbot(inode);
28354+ if (btop >= 0) {
28355+ for (bindex = btop; bindex <= bbot; bindex++) {
86dc4139
AM
28356+ if (!au_h_iptr(inode, bindex)
28357+ || au_ii_br_id(inode, bindex) != br_id)
28358+ continue;
28359+ au_set_h_iptr(inode, bindex, NULL, 0);
28360+ do_put = 1;
28361+ break;
28362+ }
28363+ if (do_put)
5afbbe0d 28364+ for (bindex = btop; bindex <= bbot; bindex++)
86dc4139
AM
28365+ if (au_h_iptr(inode, bindex)) {
28366+ do_put = 0;
28367+ break;
28368+ }
28369+ } else
28370+ do_put = 1;
28371+
28372+ return do_put;
28373+}
28374+
1facf9fc 28375+/* free the plinks on a branch specified by @br_id */
28376+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28377+{
28378+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28379+ struct hlist_bl_head *hbl;
28380+ struct hlist_bl_node *pos, *tmp;
5afbbe0d 28381+ struct au_icntnr *icntnr;
1facf9fc 28382+ struct inode *inode;
86dc4139 28383+ int i, do_put;
1facf9fc 28384+
dece6358
AM
28385+ SiMustWriteLock(sb);
28386+
1facf9fc 28387+ sbinfo = au_sbi(sb);
28388+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28389+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28390+
8b6a4947 28391+ /* no bit_lock since sbinfo is write-locked */
86dc4139 28392+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
28393+ hbl = sbinfo->si_plink + i;
28394+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
5afbbe0d 28395+ inode = au_igrab(&icntnr->vfs_inode);
86dc4139
AM
28396+ ii_write_lock_child(inode);
28397+ do_put = au_plink_do_half_refresh(inode, br_id);
5afbbe0d 28398+ if (do_put) {
8b6a4947 28399+ hlist_bl_del(&icntnr->plink);
5afbbe0d
AM
28400+ iput(inode);
28401+ }
86dc4139
AM
28402+ ii_write_unlock(inode);
28403+ iput(inode);
dece6358 28404+ }
dece6358
AM
28405+ }
28406+}
7f207e10
AM
28407diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28408--- /usr/share/empty/fs/aufs/poll.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 28409+++ linux/fs/aufs/poll.c 2019-01-28 14:36:12.222418047 +0100
cd7a4cd9
AM
28410@@ -0,0 +1,51 @@
28411+// SPDX-License-Identifier: GPL-2.0
dece6358 28412+/*
b00004a5 28413+ * Copyright (C) 2005-2018 Junjiro R. Okajima
dece6358
AM
28414+ *
28415+ * This program, aufs is free software; you can redistribute it and/or modify
28416+ * it under the terms of the GNU General Public License as published by
28417+ * the Free Software Foundation; either version 2 of the License, or
28418+ * (at your option) any later version.
28419+ *
28420+ * This program is distributed in the hope that it will be useful,
28421+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28422+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28423+ * GNU General Public License for more details.
28424+ *
28425+ * You should have received a copy of the GNU General Public License
523b37e3 28426+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358
AM
28427+ */
28428+
1308ab2a 28429+/*
28430+ * poll operation
28431+ * There is only one filesystem which implements ->poll operation, currently.
28432+ */
28433+
28434+#include "aufs.h"
28435+
cd7a4cd9 28436+__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
1308ab2a 28437+{
be118d29 28438+ __poll_t mask;
1308ab2a 28439+ struct file *h_file;
1308ab2a 28440+ struct super_block *sb;
28441+
28442+ /* We should pretend an error happened. */
be118d29 28443+ mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
b912730e 28444+ sb = file->f_path.dentry->d_sb;
e49829fe 28445+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 28446+
521ced18 28447+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
062440b3
AM
28448+ if (IS_ERR(h_file)) {
28449+ AuDbg("h_file %ld\n", PTR_ERR(h_file));
1308ab2a 28450+ goto out;
062440b3 28451+ }
1308ab2a 28452+
cd7a4cd9 28453+ mask = vfs_poll(h_file, pt);
b912730e 28454+ fput(h_file); /* instead of au_read_post() */
1308ab2a 28455+
4f0767ce 28456+out:
1308ab2a 28457+ si_read_unlock(sb);
062440b3 28458+ if (mask & EPOLLERR)
b00004a5 28459+ AuDbg("mask 0x%x\n", mask);
1308ab2a 28460+ return mask;
28461+}
c1595e42
JR
28462diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28463--- /usr/share/empty/fs/aufs/posix_acl.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 28464+++ linux/fs/aufs/posix_acl.c 2019-01-28 14:36:12.222418047 +0100
062440b3 28465@@ -0,0 +1,103 @@
cd7a4cd9 28466+// SPDX-License-Identifier: GPL-2.0
c1595e42 28467+/*
b00004a5 28468+ * Copyright (C) 2014-2018 Junjiro R. Okajima
c1595e42
JR
28469+ *
28470+ * This program, aufs is free software; you can redistribute it and/or modify
28471+ * it under the terms of the GNU General Public License as published by
28472+ * the Free Software Foundation; either version 2 of the License, or
28473+ * (at your option) any later version.
28474+ *
28475+ * This program is distributed in the hope that it will be useful,
28476+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28477+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28478+ * GNU General Public License for more details.
28479+ *
28480+ * You should have received a copy of the GNU General Public License
28481+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
28482+ */
28483+
28484+/*
28485+ * posix acl operations
28486+ */
28487+
28488+#include <linux/fs.h>
c1595e42
JR
28489+#include "aufs.h"
28490+
28491+struct posix_acl *aufs_get_acl(struct inode *inode, int type)
28492+{
28493+ struct posix_acl *acl;
28494+ int err;
28495+ aufs_bindex_t bindex;
28496+ struct inode *h_inode;
28497+ struct super_block *sb;
28498+
28499+ acl = NULL;
28500+ sb = inode->i_sb;
28501+ si_read_lock(sb, AuLock_FLUSH);
28502+ ii_read_lock_child(inode);
2121bcd9 28503+ if (!(sb->s_flags & SB_POSIXACL))
c1595e42
JR
28504+ goto out;
28505+
5afbbe0d 28506+ bindex = au_ibtop(inode);
c1595e42
JR
28507+ h_inode = au_h_iptr(inode, bindex);
28508+ if (unlikely(!h_inode
28509+ || ((h_inode->i_mode & S_IFMT)
28510+ != (inode->i_mode & S_IFMT)))) {
28511+ err = au_busy_or_stale();
28512+ acl = ERR_PTR(err);
28513+ goto out;
28514+ }
28515+
28516+ /* always topmost only */
28517+ acl = get_acl(h_inode, type);
a2654f78
AM
28518+ if (!IS_ERR_OR_NULL(acl))
28519+ set_cached_acl(inode, type, acl);
c1595e42
JR
28520+
28521+out:
28522+ ii_read_unlock(inode);
28523+ si_read_unlock(sb);
28524+
28525+ AuTraceErrPtr(acl);
28526+ return acl;
28527+}
28528+
28529+int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
28530+{
28531+ int err;
28532+ ssize_t ssz;
28533+ struct dentry *dentry;
f2c43d5f 28534+ struct au_sxattr arg = {
c1595e42
JR
28535+ .type = AU_ACL_SET,
28536+ .u.acl_set = {
28537+ .acl = acl,
28538+ .type = type
28539+ },
28540+ };
28541+
5afbbe0d
AM
28542+ IMustLock(inode);
28543+
c1595e42
JR
28544+ if (inode->i_ino == AUFS_ROOT_INO)
28545+ dentry = dget(inode->i_sb->s_root);
28546+ else {
28547+ dentry = d_find_alias(inode);
28548+ if (!dentry)
28549+ dentry = d_find_any_alias(inode);
28550+ if (!dentry) {
28551+ pr_warn("cannot handle this inode, "
28552+ "please report to aufs-users ML\n");
28553+ err = -ENOENT;
28554+ goto out;
28555+ }
28556+ }
28557+
f2c43d5f 28558+ ssz = au_sxattr(dentry, inode, &arg);
c1595e42
JR
28559+ dput(dentry);
28560+ err = ssz;
a2654f78 28561+ if (ssz >= 0) {
c1595e42 28562+ err = 0;
a2654f78
AM
28563+ set_cached_acl(inode, type, acl);
28564+ }
c1595e42
JR
28565+
28566+out:
c1595e42
JR
28567+ return err;
28568+}
7f207e10
AM
28569diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28570--- /usr/share/empty/fs/aufs/procfs.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 28571+++ linux/fs/aufs/procfs.c 2019-01-28 14:36:12.222418047 +0100
062440b3 28572@@ -0,0 +1,171 @@
cd7a4cd9 28573+// SPDX-License-Identifier: GPL-2.0
e49829fe 28574+/*
b00004a5 28575+ * Copyright (C) 2010-2018 Junjiro R. Okajima
e49829fe
JR
28576+ *
28577+ * This program, aufs is free software; you can redistribute it and/or modify
28578+ * it under the terms of the GNU General Public License as published by
28579+ * the Free Software Foundation; either version 2 of the License, or
28580+ * (at your option) any later version.
28581+ *
28582+ * This program is distributed in the hope that it will be useful,
28583+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28584+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28585+ * GNU General Public License for more details.
28586+ *
28587+ * You should have received a copy of the GNU General Public License
523b37e3 28588+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
28589+ */
28590+
28591+/*
28592+ * procfs interfaces
28593+ */
28594+
28595+#include <linux/proc_fs.h>
28596+#include "aufs.h"
28597+
28598+static int au_procfs_plm_release(struct inode *inode, struct file *file)
28599+{
28600+ struct au_sbinfo *sbinfo;
28601+
28602+ sbinfo = file->private_data;
28603+ if (sbinfo) {
28604+ au_plink_maint_leave(sbinfo);
28605+ kobject_put(&sbinfo->si_kobj);
28606+ }
28607+
28608+ return 0;
28609+}
28610+
28611+static void au_procfs_plm_write_clean(struct file *file)
28612+{
28613+ struct au_sbinfo *sbinfo;
28614+
28615+ sbinfo = file->private_data;
28616+ if (sbinfo)
28617+ au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28618+}
28619+
28620+static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28621+{
28622+ int err;
28623+ struct super_block *sb;
28624+ struct au_sbinfo *sbinfo;
8b6a4947 28625+ struct hlist_bl_node *pos;
e49829fe
JR
28626+
28627+ err = -EBUSY;
28628+ if (unlikely(file->private_data))
28629+ goto out;
28630+
28631+ sb = NULL;
53392da6 28632+ /* don't use au_sbilist_lock() here */
8b6a4947
AM
28633+ hlist_bl_lock(&au_sbilist);
28634+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
e49829fe
JR
28635+ if (id == sysaufs_si_id(sbinfo)) {
28636+ kobject_get(&sbinfo->si_kobj);
28637+ sb = sbinfo->si_sb;
28638+ break;
28639+ }
8b6a4947 28640+ hlist_bl_unlock(&au_sbilist);
e49829fe
JR
28641+
28642+ err = -EINVAL;
28643+ if (unlikely(!sb))
28644+ goto out;
28645+
28646+ err = au_plink_maint_enter(sb);
28647+ if (!err)
28648+ /* keep kobject_get() */
28649+ file->private_data = sbinfo;
28650+ else
28651+ kobject_put(&sbinfo->si_kobj);
28652+out:
28653+ return err;
28654+}
28655+
28656+/*
28657+ * Accept a valid "si=xxxx" only.
28658+ * Once it is accepted successfully, accept "clean" too.
28659+ */
28660+static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28661+ size_t count, loff_t *ppos)
28662+{
28663+ ssize_t err;
28664+ unsigned long id;
28665+ /* last newline is allowed */
28666+ char buf[3 + sizeof(unsigned long) * 2 + 1];
28667+
28668+ err = -EACCES;
28669+ if (unlikely(!capable(CAP_SYS_ADMIN)))
28670+ goto out;
28671+
28672+ err = -EINVAL;
28673+ if (unlikely(count > sizeof(buf)))
28674+ goto out;
28675+
28676+ err = copy_from_user(buf, ubuf, count);
28677+ if (unlikely(err)) {
28678+ err = -EFAULT;
28679+ goto out;
28680+ }
28681+ buf[count] = 0;
28682+
28683+ err = -EINVAL;
28684+ if (!strcmp("clean", buf)) {
28685+ au_procfs_plm_write_clean(file);
28686+ goto out_success;
28687+ } else if (unlikely(strncmp("si=", buf, 3)))
28688+ goto out;
28689+
9dbd164d 28690+ err = kstrtoul(buf + 3, 16, &id);
e49829fe
JR
28691+ if (unlikely(err))
28692+ goto out;
28693+
28694+ err = au_procfs_plm_write_si(file, id);
28695+ if (unlikely(err))
28696+ goto out;
28697+
28698+out_success:
28699+ err = count; /* success */
28700+out:
28701+ return err;
28702+}
28703+
28704+static const struct file_operations au_procfs_plm_fop = {
28705+ .write = au_procfs_plm_write,
28706+ .release = au_procfs_plm_release,
28707+ .owner = THIS_MODULE
28708+};
28709+
28710+/* ---------------------------------------------------------------------- */
28711+
28712+static struct proc_dir_entry *au_procfs_dir;
28713+
28714+void au_procfs_fin(void)
28715+{
28716+ remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28717+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28718+}
28719+
28720+int __init au_procfs_init(void)
28721+{
28722+ int err;
28723+ struct proc_dir_entry *entry;
28724+
28725+ err = -ENOMEM;
28726+ au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28727+ if (unlikely(!au_procfs_dir))
28728+ goto out;
28729+
cd7a4cd9 28730+ entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
e49829fe
JR
28731+ au_procfs_dir, &au_procfs_plm_fop);
28732+ if (unlikely(!entry))
28733+ goto out_dir;
28734+
28735+ err = 0;
28736+ goto out; /* success */
28737+
28738+
28739+out_dir:
28740+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28741+out:
28742+ return err;
28743+}
7f207e10
AM
28744diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
28745--- /usr/share/empty/fs/aufs/rdu.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 28746+++ linux/fs/aufs/rdu.c 2019-01-28 14:36:12.222418047 +0100
062440b3 28747@@ -0,0 +1,382 @@
cd7a4cd9 28748+// SPDX-License-Identifier: GPL-2.0
1308ab2a 28749+/*
b00004a5 28750+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1308ab2a 28751+ *
28752+ * This program, aufs is free software; you can redistribute it and/or modify
28753+ * it under the terms of the GNU General Public License as published by
28754+ * the Free Software Foundation; either version 2 of the License, or
28755+ * (at your option) any later version.
28756+ *
28757+ * This program is distributed in the hope that it will be useful,
28758+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28759+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28760+ * GNU General Public License for more details.
28761+ *
28762+ * You should have received a copy of the GNU General Public License
523b37e3 28763+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1308ab2a 28764+ */
28765+
28766+/*
28767+ * readdir in userspace.
28768+ */
28769+
b752ccd1 28770+#include <linux/compat.h>
4a4d8108 28771+#include <linux/fs_stack.h>
1308ab2a 28772+#include <linux/security.h>
1308ab2a 28773+#include "aufs.h"
28774+
28775+/* bits for struct aufs_rdu.flags */
28776+#define AuRdu_CALLED 1
28777+#define AuRdu_CONT (1 << 1)
28778+#define AuRdu_FULL (1 << 2)
28779+#define au_ftest_rdu(flags, name) ((flags) & AuRdu_##name)
7f207e10
AM
28780+#define au_fset_rdu(flags, name) \
28781+ do { (flags) |= AuRdu_##name; } while (0)
28782+#define au_fclr_rdu(flags, name) \
28783+ do { (flags) &= ~AuRdu_##name; } while (0)
1308ab2a 28784+
28785+struct au_rdu_arg {
392086de 28786+ struct dir_context ctx;
1308ab2a 28787+ struct aufs_rdu *rdu;
28788+ union au_rdu_ent_ul ent;
28789+ unsigned long end;
28790+
28791+ struct super_block *sb;
28792+ int err;
28793+};
28794+
392086de 28795+static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
1308ab2a 28796+ loff_t offset, u64 h_ino, unsigned int d_type)
28797+{
28798+ int err, len;
392086de 28799+ struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
1308ab2a 28800+ struct aufs_rdu *rdu = arg->rdu;
28801+ struct au_rdu_ent ent;
28802+
28803+ err = 0;
28804+ arg->err = 0;
28805+ au_fset_rdu(rdu->cookie.flags, CALLED);
28806+ len = au_rdu_len(nlen);
28807+ if (arg->ent.ul + len < arg->end) {
28808+ ent.ino = h_ino;
28809+ ent.bindex = rdu->cookie.bindex;
28810+ ent.type = d_type;
28811+ ent.nlen = nlen;
4a4d8108
AM
28812+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
28813+ ent.type = DT_UNKNOWN;
1308ab2a 28814+
9dbd164d 28815+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 28816+ err = -EFAULT;
28817+ if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
28818+ goto out;
28819+ if (copy_to_user(arg->ent.e->name, name, nlen))
28820+ goto out;
28821+ /* the terminating NULL */
28822+ if (__put_user(0, arg->ent.e->name + nlen))
28823+ goto out;
28824+ err = 0;
28825+ /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
28826+ arg->ent.ul += len;
28827+ rdu->rent++;
28828+ } else {
28829+ err = -EFAULT;
28830+ au_fset_rdu(rdu->cookie.flags, FULL);
28831+ rdu->full = 1;
28832+ rdu->tail = arg->ent;
28833+ }
28834+
4f0767ce 28835+out:
1308ab2a 28836+ /* AuTraceErr(err); */
28837+ return err;
28838+}
28839+
28840+static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
28841+{
28842+ int err;
28843+ loff_t offset;
28844+ struct au_rdu_cookie *cookie = &arg->rdu->cookie;
28845+
92d182d2 28846+ /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
1308ab2a 28847+ offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
28848+ err = offset;
28849+ if (unlikely(offset != cookie->h_pos))
28850+ goto out;
28851+
28852+ err = 0;
28853+ do {
28854+ arg->err = 0;
28855+ au_fclr_rdu(cookie->flags, CALLED);
28856+ /* smp_mb(); */
392086de 28857+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1308ab2a 28858+ if (err >= 0)
28859+ err = arg->err;
28860+ } while (!err
28861+ && au_ftest_rdu(cookie->flags, CALLED)
28862+ && !au_ftest_rdu(cookie->flags, FULL));
28863+ cookie->h_pos = h_file->f_pos;
28864+
4f0767ce 28865+out:
1308ab2a 28866+ AuTraceErr(err);
28867+ return err;
28868+}
28869+
28870+static int au_rdu(struct file *file, struct aufs_rdu *rdu)
28871+{
28872+ int err;
5afbbe0d 28873+ aufs_bindex_t bbot;
392086de
AM
28874+ struct au_rdu_arg arg = {
28875+ .ctx = {
2000de60 28876+ .actor = au_rdu_fill
392086de
AM
28877+ }
28878+ };
1308ab2a 28879+ struct dentry *dentry;
28880+ struct inode *inode;
28881+ struct file *h_file;
28882+ struct au_rdu_cookie *cookie = &rdu->cookie;
28883+
28884+ err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
28885+ if (unlikely(err)) {
28886+ err = -EFAULT;
28887+ AuTraceErr(err);
28888+ goto out;
28889+ }
28890+ rdu->rent = 0;
28891+ rdu->tail = rdu->ent;
28892+ rdu->full = 0;
28893+ arg.rdu = rdu;
28894+ arg.ent = rdu->ent;
28895+ arg.end = arg.ent.ul;
28896+ arg.end += rdu->sz;
28897+
28898+ err = -ENOTDIR;
5afbbe0d 28899+ if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
1308ab2a 28900+ goto out;
28901+
28902+ err = security_file_permission(file, MAY_READ);
28903+ AuTraceErr(err);
28904+ if (unlikely(err))
28905+ goto out;
28906+
2000de60 28907+ dentry = file->f_path.dentry;
5527c038 28908+ inode = d_inode(dentry);
5afbbe0d 28909+ inode_lock_shared(inode);
1308ab2a 28910+
28911+ arg.sb = inode->i_sb;
e49829fe
JR
28912+ err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
28913+ if (unlikely(err))
28914+ goto out_mtx;
027c5e7a
AM
28915+ err = au_alive_dir(dentry);
28916+ if (unlikely(err))
28917+ goto out_si;
e49829fe 28918+ /* todo: reval? */
1308ab2a 28919+ fi_read_lock(file);
28920+
28921+ err = -EAGAIN;
28922+ if (unlikely(au_ftest_rdu(cookie->flags, CONT)
28923+ && cookie->generation != au_figen(file)))
28924+ goto out_unlock;
28925+
28926+ err = 0;
28927+ if (!rdu->blk) {
28928+ rdu->blk = au_sbi(arg.sb)->si_rdblk;
28929+ if (!rdu->blk)
28930+ rdu->blk = au_dir_size(file, /*dentry*/NULL);
28931+ }
5afbbe0d
AM
28932+ bbot = au_fbtop(file);
28933+ if (cookie->bindex < bbot)
28934+ cookie->bindex = bbot;
28935+ bbot = au_fbbot_dir(file);
28936+ /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
28937+ for (; !err && cookie->bindex <= bbot;
1308ab2a 28938+ cookie->bindex++, cookie->h_pos = 0) {
4a4d8108 28939+ h_file = au_hf_dir(file, cookie->bindex);
1308ab2a 28940+ if (!h_file)
28941+ continue;
28942+
28943+ au_fclr_rdu(cookie->flags, FULL);
28944+ err = au_rdu_do(h_file, &arg);
28945+ AuTraceErr(err);
28946+ if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
28947+ break;
28948+ }
28949+ AuDbg("rent %llu\n", rdu->rent);
28950+
28951+ if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
28952+ rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
28953+ au_fset_rdu(cookie->flags, CONT);
28954+ cookie->generation = au_figen(file);
28955+ }
28956+
28957+ ii_read_lock_child(inode);
5afbbe0d 28958+ fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
1308ab2a 28959+ ii_read_unlock(inode);
28960+
4f0767ce 28961+out_unlock:
1308ab2a 28962+ fi_read_unlock(file);
027c5e7a 28963+out_si:
1308ab2a 28964+ si_read_unlock(arg.sb);
4f0767ce 28965+out_mtx:
5afbbe0d 28966+ inode_unlock_shared(inode);
4f0767ce 28967+out:
1308ab2a 28968+ AuTraceErr(err);
28969+ return err;
28970+}
28971+
28972+static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
28973+{
28974+ int err;
28975+ ino_t ino;
28976+ unsigned long long nent;
28977+ union au_rdu_ent_ul *u;
28978+ struct au_rdu_ent ent;
28979+ struct super_block *sb;
28980+
28981+ err = 0;
28982+ nent = rdu->nent;
28983+ u = &rdu->ent;
2000de60 28984+ sb = file->f_path.dentry->d_sb;
1308ab2a 28985+ si_read_lock(sb, AuLock_FLUSH);
28986+ while (nent-- > 0) {
9dbd164d 28987+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 28988+ err = copy_from_user(&ent, u->e, sizeof(ent));
4a4d8108
AM
28989+ if (!err)
28990+ err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
1308ab2a 28991+ if (unlikely(err)) {
28992+ err = -EFAULT;
28993+ AuTraceErr(err);
28994+ break;
28995+ }
28996+
28997+ /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
28998+ if (!ent.wh)
28999+ err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
29000+ else
29001+ err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
29002+ &ino);
29003+ if (unlikely(err)) {
29004+ AuTraceErr(err);
29005+ break;
29006+ }
29007+
29008+ err = __put_user(ino, &u->e->ino);
29009+ if (unlikely(err)) {
29010+ err = -EFAULT;
29011+ AuTraceErr(err);
29012+ break;
29013+ }
29014+ u->ul += au_rdu_len(ent.nlen);
29015+ }
29016+ si_read_unlock(sb);
29017+
29018+ return err;
29019+}
29020+
29021+/* ---------------------------------------------------------------------- */
29022+
29023+static int au_rdu_verify(struct aufs_rdu *rdu)
29024+{
b752ccd1 29025+ AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
1308ab2a 29026+ "%llu, b%d, 0x%x, g%u}\n",
b752ccd1 29027+ rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
1308ab2a 29028+ rdu->blk,
29029+ rdu->rent, rdu->shwh, rdu->full,
29030+ rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
29031+ rdu->cookie.generation);
dece6358 29032+
b752ccd1 29033+ if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
1308ab2a 29034+ return 0;
dece6358 29035+
b752ccd1
AM
29036+ AuDbg("%u:%u\n",
29037+ rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
1308ab2a 29038+ return -EINVAL;
29039+}
29040+
29041+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
dece6358 29042+{
1308ab2a 29043+ long err, e;
29044+ struct aufs_rdu rdu;
29045+ void __user *p = (void __user *)arg;
dece6358 29046+
1308ab2a 29047+ err = copy_from_user(&rdu, p, sizeof(rdu));
29048+ if (unlikely(err)) {
29049+ err = -EFAULT;
29050+ AuTraceErr(err);
29051+ goto out;
29052+ }
29053+ err = au_rdu_verify(&rdu);
dece6358
AM
29054+ if (unlikely(err))
29055+ goto out;
29056+
1308ab2a 29057+ switch (cmd) {
29058+ case AUFS_CTL_RDU:
29059+ err = au_rdu(file, &rdu);
29060+ if (unlikely(err))
29061+ break;
dece6358 29062+
1308ab2a 29063+ e = copy_to_user(p, &rdu, sizeof(rdu));
29064+ if (unlikely(e)) {
29065+ err = -EFAULT;
29066+ AuTraceErr(err);
29067+ }
29068+ break;
29069+ case AUFS_CTL_RDU_INO:
29070+ err = au_rdu_ino(file, &rdu);
29071+ break;
29072+
29073+ default:
4a4d8108 29074+ /* err = -ENOTTY; */
1308ab2a 29075+ err = -EINVAL;
29076+ }
dece6358 29077+
4f0767ce 29078+out:
1308ab2a 29079+ AuTraceErr(err);
29080+ return err;
1facf9fc 29081+}
b752ccd1
AM
29082+
29083+#ifdef CONFIG_COMPAT
29084+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29085+{
29086+ long err, e;
29087+ struct aufs_rdu rdu;
29088+ void __user *p = compat_ptr(arg);
29089+
29090+ /* todo: get_user()? */
29091+ err = copy_from_user(&rdu, p, sizeof(rdu));
29092+ if (unlikely(err)) {
29093+ err = -EFAULT;
29094+ AuTraceErr(err);
29095+ goto out;
29096+ }
29097+ rdu.ent.e = compat_ptr(rdu.ent.ul);
29098+ err = au_rdu_verify(&rdu);
29099+ if (unlikely(err))
29100+ goto out;
29101+
29102+ switch (cmd) {
29103+ case AUFS_CTL_RDU:
29104+ err = au_rdu(file, &rdu);
29105+ if (unlikely(err))
29106+ break;
29107+
29108+ rdu.ent.ul = ptr_to_compat(rdu.ent.e);
29109+ rdu.tail.ul = ptr_to_compat(rdu.tail.e);
29110+ e = copy_to_user(p, &rdu, sizeof(rdu));
29111+ if (unlikely(e)) {
29112+ err = -EFAULT;
29113+ AuTraceErr(err);
29114+ }
29115+ break;
29116+ case AUFS_CTL_RDU_INO:
29117+ err = au_rdu_ino(file, &rdu);
29118+ break;
29119+
29120+ default:
29121+ /* err = -ENOTTY; */
29122+ err = -EINVAL;
29123+ }
29124+
4f0767ce 29125+out:
b752ccd1
AM
29126+ AuTraceErr(err);
29127+ return err;
29128+}
29129+#endif
7f207e10
AM
29130diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
29131--- /usr/share/empty/fs/aufs/rwsem.h 1970-01-01 01:00:00.000000000 +0100
e49925d1 29132+++ linux/fs/aufs/rwsem.h 2019-01-28 14:36:12.222418047 +0100
062440b3
AM
29133@@ -0,0 +1,73 @@
29134+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 29135+/*
b00004a5 29136+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 29137+ *
29138+ * This program, aufs is free software; you can redistribute it and/or modify
29139+ * it under the terms of the GNU General Public License as published by
29140+ * the Free Software Foundation; either version 2 of the License, or
29141+ * (at your option) any later version.
dece6358
AM
29142+ *
29143+ * This program is distributed in the hope that it will be useful,
29144+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29145+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29146+ * GNU General Public License for more details.
29147+ *
29148+ * You should have received a copy of the GNU General Public License
523b37e3 29149+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29150+ */
29151+
29152+/*
29153+ * simple read-write semaphore wrappers
29154+ */
29155+
29156+#ifndef __AUFS_RWSEM_H__
29157+#define __AUFS_RWSEM_H__
29158+
29159+#ifdef __KERNEL__
29160+
4a4d8108 29161+#include "debug.h"
dece6358 29162+
acd2b654 29163+/* in the future, the name 'au_rwsem' will be totally gone */
8b6a4947 29164+#define au_rwsem rw_semaphore
dece6358
AM
29165+
29166+/* to debug easier, do not make them inlined functions */
8b6a4947 29167+#define AuRwMustNoWaiters(rw) AuDebugOn(rwsem_is_contended(rw))
dece6358 29168+/* rwsem_is_locked() is unusable */
8b6a4947
AM
29169+#define AuRwMustReadLock(rw) AuDebugOn(!lockdep_recursing(current) \
29170+ && debug_locks \
29171+ && !lockdep_is_held_type(rw, 1))
29172+#define AuRwMustWriteLock(rw) AuDebugOn(!lockdep_recursing(current) \
29173+ && debug_locks \
29174+ && !lockdep_is_held_type(rw, 0))
29175+#define AuRwMustAnyLock(rw) AuDebugOn(!lockdep_recursing(current) \
29176+ && debug_locks \
29177+ && !lockdep_is_held(rw))
29178+#define AuRwDestroy(rw) AuDebugOn(!lockdep_recursing(current) \
29179+ && debug_locks \
29180+ && lockdep_is_held(rw))
29181+
29182+#define au_rw_init(rw) init_rwsem(rw)
dece6358 29183+
5afbbe0d
AM
29184+#define au_rw_init_wlock(rw) do { \
29185+ au_rw_init(rw); \
8b6a4947 29186+ down_write(rw); \
5afbbe0d 29187+ } while (0)
dece6358 29188+
8b6a4947
AM
29189+#define au_rw_init_wlock_nested(rw, lsc) do { \
29190+ au_rw_init(rw); \
29191+ down_write_nested(rw, lsc); \
5afbbe0d 29192+ } while (0)
dece6358 29193+
8b6a4947
AM
29194+#define au_rw_read_lock(rw) down_read(rw)
29195+#define au_rw_read_lock_nested(rw, lsc) down_read_nested(rw, lsc)
29196+#define au_rw_read_unlock(rw) up_read(rw)
29197+#define au_rw_dgrade_lock(rw) downgrade_write(rw)
29198+#define au_rw_write_lock(rw) down_write(rw)
29199+#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29200+#define au_rw_write_unlock(rw) up_write(rw)
29201+/* why is not _nested version defined? */
29202+#define au_rw_read_trylock(rw) down_read_trylock(rw)
29203+#define au_rw_write_trylock(rw) down_write_trylock(rw)
1facf9fc 29204+
29205+#endif /* __KERNEL__ */
29206+#endif /* __AUFS_RWSEM_H__ */
7f207e10
AM
29207diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
29208--- /usr/share/empty/fs/aufs/sbinfo.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 29209+++ linux/fs/aufs/sbinfo.c 2019-01-28 14:36:12.222418047 +0100
acd2b654 29210@@ -0,0 +1,313 @@
cd7a4cd9 29211+// SPDX-License-Identifier: GPL-2.0
1facf9fc 29212+/*
b00004a5 29213+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 29214+ *
29215+ * This program, aufs is free software; you can redistribute it and/or modify
29216+ * it under the terms of the GNU General Public License as published by
29217+ * the Free Software Foundation; either version 2 of the License, or
29218+ * (at your option) any later version.
dece6358
AM
29219+ *
29220+ * This program is distributed in the hope that it will be useful,
29221+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29222+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29223+ * GNU General Public License for more details.
29224+ *
29225+ * You should have received a copy of the GNU General Public License
523b37e3 29226+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29227+ */
29228+
29229+/*
29230+ * superblock private data
29231+ */
29232+
29233+#include "aufs.h"
29234+
29235+/*
29236+ * they are necessary regardless sysfs is disabled.
29237+ */
29238+void au_si_free(struct kobject *kobj)
29239+{
86dc4139 29240+ int i;
1facf9fc 29241+ struct au_sbinfo *sbinfo;
b752ccd1 29242+ char *locked __maybe_unused; /* debug only */
1facf9fc 29243+
29244+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
86dc4139 29245+ for (i = 0; i < AuPlink_NHASH; i++)
8b6a4947 29246+ AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
f0c0a007 29247+ AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
5afbbe0d 29248+
acd2b654
AM
29249+ AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29250+ au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29251+ AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29252+ au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
1facf9fc 29253+
062440b3 29254+ dbgaufs_si_fin(sbinfo);
e49829fe 29255+ au_rw_write_lock(&sbinfo->si_rwsem);
1facf9fc 29256+ au_br_free(sbinfo);
e49829fe 29257+ au_rw_write_unlock(&sbinfo->si_rwsem);
b752ccd1 29258+
e49925d1 29259+ au_kfree_try_rcu(sbinfo->si_branch);
1facf9fc 29260+ mutex_destroy(&sbinfo->si_xib_mtx);
dece6358 29261+ AuRwDestroy(&sbinfo->si_rwsem);
1facf9fc 29262+
acd2b654
AM
29263+ au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29264+ /* si_nfiles is waited too */
e49925d1 29265+ au_kfree_rcu(sbinfo);
1facf9fc 29266+}
29267+
29268+int au_si_alloc(struct super_block *sb)
29269+{
86dc4139 29270+ int err, i;
1facf9fc 29271+ struct au_sbinfo *sbinfo;
29272+
29273+ err = -ENOMEM;
4a4d8108 29274+ sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
1facf9fc 29275+ if (unlikely(!sbinfo))
29276+ goto out;
29277+
29278+ /* will be reallocated separately */
29279+ sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29280+ if (unlikely(!sbinfo->si_branch))
febd17d6 29281+ goto out_sbinfo;
1facf9fc 29282+
1facf9fc 29283+ err = sysaufs_si_init(sbinfo);
062440b3
AM
29284+ if (!err) {
29285+ dbgaufs_si_null(sbinfo);
29286+ err = dbgaufs_si_init(sbinfo);
29287+ if (unlikely(err))
29288+ kobject_put(&sbinfo->si_kobj);
29289+ }
1facf9fc 29290+ if (unlikely(err))
29291+ goto out_br;
29292+
29293+ au_nwt_init(&sbinfo->si_nowait);
dece6358 29294+ au_rw_init_wlock(&sbinfo->si_rwsem);
b752ccd1 29295+
acd2b654
AM
29296+ au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29297+ au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
7f207e10 29298+
5afbbe0d 29299+ sbinfo->si_bbot = -1;
392086de 29300+ sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
1facf9fc 29301+
29302+ sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29303+ sbinfo->si_wbr_create = AuWbrCreate_Def;
4a4d8108
AM
29304+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29305+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
1facf9fc 29306+
076b876e
AM
29307+ au_fhsm_init(sbinfo);
29308+
e49829fe 29309+ sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
1facf9fc 29310+
392086de
AM
29311+ sbinfo->si_xino_jiffy = jiffies;
29312+ sbinfo->si_xino_expire
29313+ = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
1facf9fc 29314+ mutex_init(&sbinfo->si_xib_mtx);
1facf9fc 29315+ /* leave si_xib_last_pindex and si_xib_next_bit */
29316+
8b6a4947 29317+ INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
b912730e 29318+
e49829fe 29319+ sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
1facf9fc 29320+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29321+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29322+ sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29323+
86dc4139 29324+ for (i = 0; i < AuPlink_NHASH; i++)
8b6a4947 29325+ INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
1facf9fc 29326+ init_waitqueue_head(&sbinfo->si_plink_wq);
4a4d8108 29327+ spin_lock_init(&sbinfo->si_plink_maint_lock);
1facf9fc 29328+
8b6a4947 29329+ INIT_HLIST_BL_HEAD(&sbinfo->si_files);
523b37e3 29330+
b95c5147
AM
29331+ /* with getattr by default */
29332+ sbinfo->si_iop_array = aufs_iop;
29333+
1facf9fc 29334+ /* leave other members for sysaufs and si_mnt. */
29335+ sbinfo->si_sb = sb;
29336+ sb->s_fs_info = sbinfo;
b752ccd1 29337+ si_pid_set(sb);
1facf9fc 29338+ return 0; /* success */
29339+
4f0767ce 29340+out_br:
e49925d1 29341+ au_kfree_try_rcu(sbinfo->si_branch);
4f0767ce 29342+out_sbinfo:
e49925d1 29343+ au_kfree_rcu(sbinfo);
4f0767ce 29344+out:
1facf9fc 29345+ return err;
29346+}
29347+
e2f27e51 29348+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
1facf9fc 29349+{
29350+ int err, sz;
29351+ struct au_branch **brp;
29352+
dece6358
AM
29353+ AuRwMustWriteLock(&sbinfo->si_rwsem);
29354+
1facf9fc 29355+ err = -ENOMEM;
5afbbe0d 29356+ sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
1facf9fc 29357+ if (unlikely(!sz))
29358+ sz = sizeof(*brp);
e2f27e51
AM
29359+ brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29360+ may_shrink);
1facf9fc 29361+ if (brp) {
29362+ sbinfo->si_branch = brp;
29363+ err = 0;
29364+ }
29365+
29366+ return err;
29367+}
29368+
29369+/* ---------------------------------------------------------------------- */
29370+
29371+unsigned int au_sigen_inc(struct super_block *sb)
29372+{
29373+ unsigned int gen;
5527c038 29374+ struct inode *inode;
1facf9fc 29375+
dece6358
AM
29376+ SiMustWriteLock(sb);
29377+
1facf9fc 29378+ gen = ++au_sbi(sb)->si_generation;
29379+ au_update_digen(sb->s_root);
5527c038
JR
29380+ inode = d_inode(sb->s_root);
29381+ au_update_iigen(inode, /*half*/0);
be118d29 29382+ inode_inc_iversion(inode);
1facf9fc 29383+ return gen;
29384+}
29385+
29386+aufs_bindex_t au_new_br_id(struct super_block *sb)
29387+{
29388+ aufs_bindex_t br_id;
29389+ int i;
29390+ struct au_sbinfo *sbinfo;
29391+
dece6358
AM
29392+ SiMustWriteLock(sb);
29393+
1facf9fc 29394+ sbinfo = au_sbi(sb);
29395+ for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29396+ br_id = ++sbinfo->si_last_br_id;
7f207e10 29397+ AuDebugOn(br_id < 0);
1facf9fc 29398+ if (br_id && au_br_index(sb, br_id) < 0)
29399+ return br_id;
29400+ }
29401+
29402+ return -1;
29403+}
29404+
29405+/* ---------------------------------------------------------------------- */
29406+
e49829fe
JR
29407+/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29408+int si_read_lock(struct super_block *sb, int flags)
29409+{
29410+ int err;
29411+
29412+ err = 0;
29413+ if (au_ftest_lock(flags, FLUSH))
29414+ au_nwt_flush(&au_sbi(sb)->si_nowait);
29415+
29416+ si_noflush_read_lock(sb);
29417+ err = au_plink_maint(sb, flags);
29418+ if (unlikely(err))
29419+ si_read_unlock(sb);
29420+
29421+ return err;
29422+}
29423+
29424+int si_write_lock(struct super_block *sb, int flags)
29425+{
29426+ int err;
29427+
29428+ if (au_ftest_lock(flags, FLUSH))
29429+ au_nwt_flush(&au_sbi(sb)->si_nowait);
29430+
29431+ si_noflush_write_lock(sb);
29432+ err = au_plink_maint(sb, flags);
29433+ if (unlikely(err))
29434+ si_write_unlock(sb);
29435+
29436+ return err;
29437+}
29438+
1facf9fc 29439+/* dentry and super_block lock. call at entry point */
e49829fe 29440+int aufs_read_lock(struct dentry *dentry, int flags)
1facf9fc 29441+{
e49829fe 29442+ int err;
027c5e7a 29443+ struct super_block *sb;
e49829fe 29444+
027c5e7a
AM
29445+ sb = dentry->d_sb;
29446+ err = si_read_lock(sb, flags);
29447+ if (unlikely(err))
29448+ goto out;
29449+
29450+ if (au_ftest_lock(flags, DW))
29451+ di_write_lock_child(dentry);
29452+ else
29453+ di_read_lock_child(dentry, flags);
29454+
29455+ if (au_ftest_lock(flags, GEN)) {
29456+ err = au_digen_test(dentry, au_sigen(sb));
79b8bda9
AM
29457+ if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29458+ AuDebugOn(!err && au_dbrange_test(dentry));
29459+ else if (!err)
29460+ err = au_dbrange_test(dentry);
027c5e7a
AM
29461+ if (unlikely(err))
29462+ aufs_read_unlock(dentry, flags);
e49829fe
JR
29463+ }
29464+
027c5e7a 29465+out:
e49829fe 29466+ return err;
1facf9fc 29467+}
29468+
29469+void aufs_read_unlock(struct dentry *dentry, int flags)
29470+{
29471+ if (au_ftest_lock(flags, DW))
29472+ di_write_unlock(dentry);
29473+ else
29474+ di_read_unlock(dentry, flags);
29475+ si_read_unlock(dentry->d_sb);
29476+}
29477+
29478+void aufs_write_lock(struct dentry *dentry)
29479+{
e49829fe 29480+ si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
1facf9fc 29481+ di_write_lock_child(dentry);
29482+}
29483+
29484+void aufs_write_unlock(struct dentry *dentry)
29485+{
29486+ di_write_unlock(dentry);
29487+ si_write_unlock(dentry->d_sb);
29488+}
29489+
e49829fe 29490+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
1facf9fc 29491+{
e49829fe 29492+ int err;
027c5e7a
AM
29493+ unsigned int sigen;
29494+ struct super_block *sb;
e49829fe 29495+
027c5e7a
AM
29496+ sb = d1->d_sb;
29497+ err = si_read_lock(sb, flags);
29498+ if (unlikely(err))
29499+ goto out;
29500+
b95c5147 29501+ di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
027c5e7a
AM
29502+
29503+ if (au_ftest_lock(flags, GEN)) {
29504+ sigen = au_sigen(sb);
29505+ err = au_digen_test(d1, sigen);
29506+ AuDebugOn(!err && au_dbrange_test(d1));
29507+ if (!err) {
29508+ err = au_digen_test(d2, sigen);
29509+ AuDebugOn(!err && au_dbrange_test(d2));
29510+ }
29511+ if (unlikely(err))
29512+ aufs_read_and_write_unlock2(d1, d2);
29513+ }
29514+
29515+out:
e49829fe 29516+ return err;
1facf9fc 29517+}
29518+
29519+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29520+{
29521+ di_write_unlock2(d1, d2);
29522+ si_read_unlock(d1->d_sb);
29523+}
7f207e10
AM
29524diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29525--- /usr/share/empty/fs/aufs/super.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 29526+++ linux/fs/aufs/super.c 2019-01-28 14:36:12.222418047 +0100
acd2b654 29527@@ -0,0 +1,1048 @@
cd7a4cd9 29528+// SPDX-License-Identifier: GPL-2.0
1facf9fc 29529+/*
b00004a5 29530+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 29531+ *
29532+ * This program, aufs is free software; you can redistribute it and/or modify
29533+ * it under the terms of the GNU General Public License as published by
29534+ * the Free Software Foundation; either version 2 of the License, or
29535+ * (at your option) any later version.
dece6358
AM
29536+ *
29537+ * This program is distributed in the hope that it will be useful,
29538+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29539+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29540+ * GNU General Public License for more details.
29541+ *
29542+ * You should have received a copy of the GNU General Public License
523b37e3 29543+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29544+ */
29545+
29546+/*
29547+ * mount and super_block operations
29548+ */
29549+
f6c5ef8b 29550+#include <linux/mm.h>
1facf9fc 29551+#include <linux/seq_file.h>
29552+#include <linux/statfs.h>
7f207e10 29553+#include <linux/vmalloc.h>
1facf9fc 29554+#include "aufs.h"
29555+
29556+/*
29557+ * super_operations
29558+ */
29559+static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29560+{
29561+ struct au_icntnr *c;
29562+
29563+ c = au_cache_alloc_icntnr();
29564+ if (c) {
027c5e7a 29565+ au_icntnr_init(c);
be118d29 29566+ inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
1facf9fc 29567+ c->iinfo.ii_hinode = NULL;
29568+ return &c->vfs_inode;
29569+ }
29570+ return NULL;
29571+}
29572+
027c5e7a
AM
29573+static void aufs_destroy_inode_cb(struct rcu_head *head)
29574+{
29575+ struct inode *inode = container_of(head, struct inode, i_rcu);
29576+
1c60b727 29577+ au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
027c5e7a
AM
29578+}
29579+
1facf9fc 29580+static void aufs_destroy_inode(struct inode *inode)
29581+{
5afbbe0d
AM
29582+ if (!au_is_bad_inode(inode))
29583+ au_iinfo_fin(inode);
027c5e7a 29584+ call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
1facf9fc 29585+}
29586+
29587+struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29588+{
29589+ struct inode *inode;
29590+ int err;
29591+
29592+ inode = iget_locked(sb, ino);
29593+ if (unlikely(!inode)) {
29594+ inode = ERR_PTR(-ENOMEM);
29595+ goto out;
29596+ }
29597+ if (!(inode->i_state & I_NEW))
29598+ goto out;
29599+
29600+ err = au_xigen_new(inode);
29601+ if (!err)
29602+ err = au_iinfo_init(inode);
29603+ if (!err)
be118d29 29604+ inode_inc_iversion(inode);
1facf9fc 29605+ else {
29606+ iget_failed(inode);
29607+ inode = ERR_PTR(err);
29608+ }
29609+
4f0767ce 29610+out:
1facf9fc 29611+ /* never return NULL */
29612+ AuDebugOn(!inode);
29613+ AuTraceErrPtr(inode);
29614+ return inode;
29615+}
29616+
29617+/* lock free root dinfo */
29618+static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29619+{
29620+ int err;
5afbbe0d 29621+ aufs_bindex_t bindex, bbot;
1facf9fc 29622+ struct path path;
4a4d8108 29623+ struct au_hdentry *hdp;
1facf9fc 29624+ struct au_branch *br;
076b876e 29625+ au_br_perm_str_t perm;
1facf9fc 29626+
29627+ err = 0;
5afbbe0d
AM
29628+ bbot = au_sbbot(sb);
29629+ bindex = 0;
29630+ hdp = au_hdentry(au_di(sb->s_root), bindex);
29631+ for (; !err && bindex <= bbot; bindex++, hdp++) {
1facf9fc 29632+ br = au_sbr(sb, bindex);
86dc4139 29633+ path.mnt = au_br_mnt(br);
5afbbe0d 29634+ path.dentry = hdp->hd_dentry;
1facf9fc 29635+ err = au_seq_path(seq, &path);
79b8bda9 29636+ if (!err) {
076b876e 29637+ au_optstr_br_perm(&perm, br->br_perm);
79b8bda9 29638+ seq_printf(seq, "=%s", perm.a);
5afbbe0d 29639+ if (bindex != bbot)
79b8bda9 29640+ seq_putc(seq, ':');
1e00d052 29641+ }
1facf9fc 29642+ }
79b8bda9
AM
29643+ if (unlikely(err || seq_has_overflowed(seq)))
29644+ err = -E2BIG;
1facf9fc 29645+
29646+ return err;
29647+}
29648+
f2c43d5f
AM
29649+static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29650+ const char *append)
29651+{
29652+ char *p;
29653+
29654+ p = fmt;
29655+ while (*pat != ':')
29656+ *p++ = *pat++;
29657+ *p++ = *pat++;
29658+ strcpy(p, append);
29659+ AuDebugOn(strlen(fmt) >= len);
29660+}
29661+
1facf9fc 29662+static void au_show_wbr_create(struct seq_file *m, int v,
29663+ struct au_sbinfo *sbinfo)
29664+{
29665+ const char *pat;
f2c43d5f
AM
29666+ char fmt[32];
29667+ struct au_wbr_mfs *mfs;
1facf9fc 29668+
dece6358
AM
29669+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29670+
c2b27bf2 29671+ seq_puts(m, ",create=");
1facf9fc 29672+ pat = au_optstr_wbr_create(v);
f2c43d5f 29673+ mfs = &sbinfo->si_wbr_mfs;
1facf9fc 29674+ switch (v) {
29675+ case AuWbrCreate_TDP:
29676+ case AuWbrCreate_RR:
29677+ case AuWbrCreate_MFS:
29678+ case AuWbrCreate_PMFS:
c2b27bf2 29679+ seq_puts(m, pat);
1facf9fc 29680+ break;
f2c43d5f
AM
29681+ case AuWbrCreate_MFSRR:
29682+ case AuWbrCreate_TDMFS:
29683+ case AuWbrCreate_PMFSRR:
29684+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29685+ seq_printf(m, fmt, mfs->mfsrr_watermark);
1facf9fc 29686+ break;
f2c43d5f 29687+ case AuWbrCreate_MFSV:
1facf9fc 29688+ case AuWbrCreate_PMFSV:
f2c43d5f
AM
29689+ au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29690+ seq_printf(m, fmt,
29691+ jiffies_to_msecs(mfs->mfs_expire)
e49829fe 29692+ / MSEC_PER_SEC);
1facf9fc 29693+ break;
1facf9fc 29694+ case AuWbrCreate_MFSRRV:
f2c43d5f 29695+ case AuWbrCreate_TDMFSV:
392086de 29696+ case AuWbrCreate_PMFSRRV:
f2c43d5f
AM
29697+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29698+ seq_printf(m, fmt, mfs->mfsrr_watermark,
29699+ jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
392086de 29700+ break;
f2c43d5f
AM
29701+ default:
29702+ BUG();
1facf9fc 29703+ }
29704+}
29705+
7eafdf33 29706+static int au_show_xino(struct seq_file *seq, struct super_block *sb)
1facf9fc 29707+{
29708+#ifdef CONFIG_SYSFS
29709+ return 0;
29710+#else
29711+ int err;
29712+ const int len = sizeof(AUFS_XINO_FNAME) - 1;
29713+ aufs_bindex_t bindex, brid;
1facf9fc 29714+ struct qstr *name;
29715+ struct file *f;
29716+ struct dentry *d, *h_root;
acd2b654 29717+ struct au_branch *br;
1facf9fc 29718+
dece6358
AM
29719+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29720+
1facf9fc 29721+ err = 0;
1facf9fc 29722+ f = au_sbi(sb)->si_xib;
29723+ if (!f)
29724+ goto out;
29725+
29726+ /* stop printing the default xino path on the first writable branch */
29727+ h_root = NULL;
acd2b654
AM
29728+ bindex = au_xi_root(sb, f->f_path.dentry);
29729+ if (bindex >= 0) {
29730+ br = au_sbr_sb(sb, bindex);
29731+ h_root = au_br_dentry(br);
1facf9fc 29732+ }
acd2b654 29733+
2000de60 29734+ d = f->f_path.dentry;
1facf9fc 29735+ name = &d->d_name;
29736+ /* safe ->d_parent because the file is unlinked */
29737+ if (d->d_parent == h_root
29738+ && name->len == len
29739+ && !memcmp(name->name, AUFS_XINO_FNAME, len))
29740+ goto out;
29741+
29742+ seq_puts(seq, ",xino=");
29743+ err = au_xino_path(seq, f);
29744+
4f0767ce 29745+out:
1facf9fc 29746+ return err;
29747+#endif
29748+}
29749+
29750+/* seq_file will re-call me in case of too long string */
7eafdf33 29751+static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
1facf9fc 29752+{
027c5e7a 29753+ int err;
1facf9fc 29754+ unsigned int mnt_flags, v;
29755+ struct super_block *sb;
29756+ struct au_sbinfo *sbinfo;
29757+
29758+#define AuBool(name, str) do { \
29759+ v = au_opt_test(mnt_flags, name); \
29760+ if (v != au_opt_test(AuOpt_Def, name)) \
29761+ seq_printf(m, ",%s" #str, v ? "" : "no"); \
29762+} while (0)
29763+
29764+#define AuStr(name, str) do { \
29765+ v = mnt_flags & AuOptMask_##name; \
29766+ if (v != (AuOpt_Def & AuOptMask_##name)) \
29767+ seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
29768+} while (0)
29769+
29770+#define AuUInt(name, str, val) do { \
29771+ if (val != AUFS_##name##_DEF) \
29772+ seq_printf(m, "," #str "=%u", val); \
29773+} while (0)
29774+
7eafdf33 29775+ sb = dentry->d_sb;
2121bcd9 29776+ if (sb->s_flags & SB_POSIXACL)
c1595e42 29777+ seq_puts(m, ",acl");
be118d29
JR
29778+#if 0
29779+ if (sb->s_flags & SB_I_VERSION)
29780+ seq_puts(m, ",i_version");
29781+#endif
c1595e42
JR
29782+
29783+ /* lock free root dinfo */
1facf9fc 29784+ si_noflush_read_lock(sb);
29785+ sbinfo = au_sbi(sb);
29786+ seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
29787+
29788+ mnt_flags = au_mntflags(sb);
29789+ if (au_opt_test(mnt_flags, XINO)) {
7eafdf33 29790+ err = au_show_xino(m, sb);
1facf9fc 29791+ if (unlikely(err))
29792+ goto out;
29793+ } else
29794+ seq_puts(m, ",noxino");
29795+
29796+ AuBool(TRUNC_XINO, trunc_xino);
29797+ AuStr(UDBA, udba);
dece6358 29798+ AuBool(SHWH, shwh);
1facf9fc 29799+ AuBool(PLINK, plink);
4a4d8108 29800+ AuBool(DIO, dio);
076b876e 29801+ AuBool(DIRPERM1, dirperm1);
1facf9fc 29802+
29803+ v = sbinfo->si_wbr_create;
29804+ if (v != AuWbrCreate_Def)
29805+ au_show_wbr_create(m, v, sbinfo);
29806+
29807+ v = sbinfo->si_wbr_copyup;
29808+ if (v != AuWbrCopyup_Def)
29809+ seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
29810+
29811+ v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
29812+ if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
29813+ seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
29814+
29815+ AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
29816+
027c5e7a
AM
29817+ v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
29818+ AuUInt(RDCACHE, rdcache, v);
1facf9fc 29819+
29820+ AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
29821+ AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
29822+
076b876e
AM
29823+ au_fhsm_show(m, sbinfo);
29824+
8b6a4947 29825+ AuBool(DIRREN, dirren);
1facf9fc 29826+ AuBool(SUM, sum);
29827+ /* AuBool(SUM_W, wsum); */
29828+ AuBool(WARN_PERM, warn_perm);
29829+ AuBool(VERBOSE, verbose);
29830+
4f0767ce 29831+out:
1facf9fc 29832+ /* be sure to print "br:" last */
29833+ if (!sysaufs_brs) {
29834+ seq_puts(m, ",br:");
29835+ au_show_brs(m, sb);
29836+ }
29837+ si_read_unlock(sb);
29838+ return 0;
29839+
1facf9fc 29840+#undef AuBool
29841+#undef AuStr
4a4d8108 29842+#undef AuUInt
1facf9fc 29843+}
29844+
29845+/* ---------------------------------------------------------------------- */
29846+
29847+/* sum mode which returns the summation for statfs(2) */
29848+
29849+static u64 au_add_till_max(u64 a, u64 b)
29850+{
29851+ u64 old;
29852+
29853+ old = a;
29854+ a += b;
92d182d2
AM
29855+ if (old <= a)
29856+ return a;
29857+ return ULLONG_MAX;
29858+}
29859+
29860+static u64 au_mul_till_max(u64 a, long mul)
29861+{
29862+ u64 old;
29863+
29864+ old = a;
29865+ a *= mul;
29866+ if (old <= a)
1facf9fc 29867+ return a;
29868+ return ULLONG_MAX;
29869+}
29870+
29871+static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
29872+{
29873+ int err;
92d182d2 29874+ long bsize, factor;
1facf9fc 29875+ u64 blocks, bfree, bavail, files, ffree;
5afbbe0d 29876+ aufs_bindex_t bbot, bindex, i;
1facf9fc 29877+ unsigned char shared;
7f207e10 29878+ struct path h_path;
1facf9fc 29879+ struct super_block *h_sb;
29880+
92d182d2
AM
29881+ err = 0;
29882+ bsize = LONG_MAX;
29883+ files = 0;
29884+ ffree = 0;
1facf9fc 29885+ blocks = 0;
29886+ bfree = 0;
29887+ bavail = 0;
5afbbe0d
AM
29888+ bbot = au_sbbot(sb);
29889+ for (bindex = 0; bindex <= bbot; bindex++) {
7f207e10
AM
29890+ h_path.mnt = au_sbr_mnt(sb, bindex);
29891+ h_sb = h_path.mnt->mnt_sb;
1facf9fc 29892+ shared = 0;
92d182d2 29893+ for (i = 0; !shared && i < bindex; i++)
1facf9fc 29894+ shared = (au_sbr_sb(sb, i) == h_sb);
29895+ if (shared)
29896+ continue;
29897+
29898+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
29899+ h_path.dentry = h_path.mnt->mnt_root;
29900+ err = vfs_statfs(&h_path, buf);
1facf9fc 29901+ if (unlikely(err))
29902+ goto out;
29903+
92d182d2
AM
29904+ if (bsize > buf->f_bsize) {
29905+ /*
29906+ * we will reduce bsize, so we have to expand blocks
29907+ * etc. to match them again
29908+ */
29909+ factor = (bsize / buf->f_bsize);
29910+ blocks = au_mul_till_max(blocks, factor);
29911+ bfree = au_mul_till_max(bfree, factor);
29912+ bavail = au_mul_till_max(bavail, factor);
29913+ bsize = buf->f_bsize;
29914+ }
29915+
29916+ factor = (buf->f_bsize / bsize);
29917+ blocks = au_add_till_max(blocks,
29918+ au_mul_till_max(buf->f_blocks, factor));
29919+ bfree = au_add_till_max(bfree,
29920+ au_mul_till_max(buf->f_bfree, factor));
29921+ bavail = au_add_till_max(bavail,
29922+ au_mul_till_max(buf->f_bavail, factor));
1facf9fc 29923+ files = au_add_till_max(files, buf->f_files);
29924+ ffree = au_add_till_max(ffree, buf->f_ffree);
29925+ }
29926+
92d182d2 29927+ buf->f_bsize = bsize;
1facf9fc 29928+ buf->f_blocks = blocks;
29929+ buf->f_bfree = bfree;
29930+ buf->f_bavail = bavail;
29931+ buf->f_files = files;
29932+ buf->f_ffree = ffree;
92d182d2 29933+ buf->f_frsize = 0;
1facf9fc 29934+
4f0767ce 29935+out:
1facf9fc 29936+ return err;
29937+}
29938+
29939+static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
29940+{
29941+ int err;
7f207e10 29942+ struct path h_path;
1facf9fc 29943+ struct super_block *sb;
29944+
29945+ /* lock free root dinfo */
29946+ sb = dentry->d_sb;
29947+ si_noflush_read_lock(sb);
7f207e10 29948+ if (!au_opt_test(au_mntflags(sb), SUM)) {
1facf9fc 29949+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
29950+ h_path.mnt = au_sbr_mnt(sb, 0);
29951+ h_path.dentry = h_path.mnt->mnt_root;
29952+ err = vfs_statfs(&h_path, buf);
29953+ } else
1facf9fc 29954+ err = au_statfs_sum(sb, buf);
29955+ si_read_unlock(sb);
29956+
29957+ if (!err) {
29958+ buf->f_type = AUFS_SUPER_MAGIC;
4a4d8108 29959+ buf->f_namelen = AUFS_MAX_NAMELEN;
1facf9fc 29960+ memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
29961+ }
29962+ /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
29963+
29964+ return err;
29965+}
29966+
29967+/* ---------------------------------------------------------------------- */
29968+
537831f9
AM
29969+static int aufs_sync_fs(struct super_block *sb, int wait)
29970+{
29971+ int err, e;
5afbbe0d 29972+ aufs_bindex_t bbot, bindex;
537831f9
AM
29973+ struct au_branch *br;
29974+ struct super_block *h_sb;
29975+
29976+ err = 0;
29977+ si_noflush_read_lock(sb);
5afbbe0d
AM
29978+ bbot = au_sbbot(sb);
29979+ for (bindex = 0; bindex <= bbot; bindex++) {
537831f9
AM
29980+ br = au_sbr(sb, bindex);
29981+ if (!au_br_writable(br->br_perm))
29982+ continue;
29983+
29984+ h_sb = au_sbr_sb(sb, bindex);
a2654f78
AM
29985+ e = vfsub_sync_filesystem(h_sb, wait);
29986+ if (unlikely(e && !err))
29987+ err = e;
29988+ /* go on even if an error happens */
537831f9
AM
29989+ }
29990+ si_read_unlock(sb);
29991+
29992+ return err;
29993+}
29994+
29995+/* ---------------------------------------------------------------------- */
29996+
1facf9fc 29997+/* final actions when unmounting a file system */
29998+static void aufs_put_super(struct super_block *sb)
29999+{
30000+ struct au_sbinfo *sbinfo;
30001+
30002+ sbinfo = au_sbi(sb);
062440b3
AM
30003+ if (sbinfo)
30004+ kobject_put(&sbinfo->si_kobj);
1facf9fc 30005+}
30006+
30007+/* ---------------------------------------------------------------------- */
30008+
79b8bda9
AM
30009+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30010+ struct super_block *sb, void *arg)
7f207e10
AM
30011+{
30012+ void *array;
076b876e 30013+ unsigned long long n, sz;
7f207e10
AM
30014+
30015+ array = NULL;
30016+ n = 0;
30017+ if (!*hint)
30018+ goto out;
30019+
30020+ if (*hint > ULLONG_MAX / sizeof(array)) {
30021+ array = ERR_PTR(-EMFILE);
30022+ pr_err("hint %llu\n", *hint);
30023+ goto out;
30024+ }
30025+
076b876e
AM
30026+ sz = sizeof(array) * *hint;
30027+ array = kzalloc(sz, GFP_NOFS);
7f207e10 30028+ if (unlikely(!array))
076b876e 30029+ array = vzalloc(sz);
7f207e10
AM
30030+ if (unlikely(!array)) {
30031+ array = ERR_PTR(-ENOMEM);
30032+ goto out;
30033+ }
30034+
79b8bda9 30035+ n = cb(sb, array, *hint, arg);
7f207e10
AM
30036+ AuDebugOn(n > *hint);
30037+
30038+out:
30039+ *hint = n;
30040+ return array;
30041+}
30042+
79b8bda9 30043+static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
7f207e10
AM
30044+ unsigned long long max __maybe_unused,
30045+ void *arg)
30046+{
30047+ unsigned long long n;
30048+ struct inode **p, *inode;
30049+ struct list_head *head;
30050+
30051+ n = 0;
30052+ p = a;
30053+ head = arg;
79b8bda9 30054+ spin_lock(&sb->s_inode_list_lock);
7f207e10 30055+ list_for_each_entry(inode, head, i_sb_list) {
5afbbe0d
AM
30056+ if (!au_is_bad_inode(inode)
30057+ && au_ii(inode)->ii_btop >= 0) {
2cbb1c4b
JR
30058+ spin_lock(&inode->i_lock);
30059+ if (atomic_read(&inode->i_count)) {
30060+ au_igrab(inode);
30061+ *p++ = inode;
30062+ n++;
30063+ AuDebugOn(n > max);
30064+ }
30065+ spin_unlock(&inode->i_lock);
7f207e10
AM
30066+ }
30067+ }
79b8bda9 30068+ spin_unlock(&sb->s_inode_list_lock);
7f207e10
AM
30069+
30070+ return n;
30071+}
30072+
30073+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
30074+{
acd2b654
AM
30075+ struct au_sbinfo *sbi;
30076+
30077+ sbi = au_sbi(sb);
30078+ *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
79b8bda9 30079+ return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
7f207e10
AM
30080+}
30081+
30082+void au_iarray_free(struct inode **a, unsigned long long max)
30083+{
30084+ unsigned long long ull;
30085+
30086+ for (ull = 0; ull < max; ull++)
30087+ iput(a[ull]);
be52b249 30088+ kvfree(a);
7f207e10
AM
30089+}
30090+
30091+/* ---------------------------------------------------------------------- */
30092+
1facf9fc 30093+/*
30094+ * refresh dentry and inode at remount time.
30095+ */
027c5e7a
AM
30096+/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
30097+static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
30098+ struct dentry *parent)
1facf9fc 30099+{
30100+ int err;
1facf9fc 30101+
30102+ di_write_lock_child(dentry);
1facf9fc 30103+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
30104+ err = au_refresh_dentry(dentry, parent);
30105+ if (!err && dir_flags)
5527c038 30106+ au_hn_reset(d_inode(dentry), dir_flags);
1facf9fc 30107+ di_read_unlock(parent, AuLock_IR);
1facf9fc 30108+ di_write_unlock(dentry);
30109+
30110+ return err;
30111+}
30112+
027c5e7a
AM
30113+static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
30114+ struct au_sbinfo *sbinfo,
b95c5147 30115+ const unsigned int dir_flags, unsigned int do_idop)
1facf9fc 30116+{
027c5e7a
AM
30117+ int err;
30118+ struct dentry *parent;
027c5e7a
AM
30119+
30120+ err = 0;
30121+ parent = dget_parent(dentry);
30122+ if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
5527c038
JR
30123+ if (d_really_is_positive(dentry)) {
30124+ if (!d_is_dir(dentry))
027c5e7a
AM
30125+ err = au_do_refresh(dentry, /*dir_flags*/0,
30126+ parent);
30127+ else {
30128+ err = au_do_refresh(dentry, dir_flags, parent);
30129+ if (unlikely(err))
30130+ au_fset_si(sbinfo, FAILED_REFRESH_DIR);
30131+ }
30132+ } else
30133+ err = au_do_refresh(dentry, /*dir_flags*/0, parent);
30134+ AuDbgDentry(dentry);
30135+ }
30136+ dput(parent);
30137+
79b8bda9 30138+ if (!err) {
b95c5147 30139+ if (do_idop)
79b8bda9
AM
30140+ au_refresh_dop(dentry, /*force_reval*/0);
30141+ } else
30142+ au_refresh_dop(dentry, /*force_reval*/1);
30143+
027c5e7a
AM
30144+ AuTraceErr(err);
30145+ return err;
1facf9fc 30146+}
30147+
b95c5147 30148+static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
1facf9fc 30149+{
30150+ int err, i, j, ndentry, e;
027c5e7a 30151+ unsigned int sigen;
1facf9fc 30152+ struct au_dcsub_pages dpages;
30153+ struct au_dpage *dpage;
027c5e7a
AM
30154+ struct dentry **dentries, *d;
30155+ struct au_sbinfo *sbinfo;
30156+ struct dentry *root = sb->s_root;
5527c038 30157+ const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
1facf9fc 30158+
b95c5147 30159+ if (do_idop)
79b8bda9
AM
30160+ au_refresh_dop(root, /*force_reval*/0);
30161+
027c5e7a
AM
30162+ err = au_dpages_init(&dpages, GFP_NOFS);
30163+ if (unlikely(err))
1facf9fc 30164+ goto out;
027c5e7a
AM
30165+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
30166+ if (unlikely(err))
1facf9fc 30167+ goto out_dpages;
1facf9fc 30168+
027c5e7a
AM
30169+ sigen = au_sigen(sb);
30170+ sbinfo = au_sbi(sb);
30171+ for (i = 0; i < dpages.ndpage; i++) {
1facf9fc 30172+ dpage = dpages.dpages + i;
30173+ dentries = dpage->dentries;
30174+ ndentry = dpage->ndentry;
027c5e7a 30175+ for (j = 0; j < ndentry; j++) {
1facf9fc 30176+ d = dentries[j];
79b8bda9 30177+ e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
b95c5147 30178+ do_idop);
027c5e7a
AM
30179+ if (unlikely(e && !err))
30180+ err = e;
30181+ /* go on even err */
1facf9fc 30182+ }
30183+ }
30184+
4f0767ce 30185+out_dpages:
1facf9fc 30186+ au_dpages_free(&dpages);
4f0767ce 30187+out:
1facf9fc 30188+ return err;
30189+}
30190+
b95c5147 30191+static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
1facf9fc 30192+{
027c5e7a
AM
30193+ int err, e;
30194+ unsigned int sigen;
30195+ unsigned long long max, ull;
30196+ struct inode *inode, **array;
1facf9fc 30197+
027c5e7a
AM
30198+ array = au_iarray_alloc(sb, &max);
30199+ err = PTR_ERR(array);
30200+ if (IS_ERR(array))
30201+ goto out;
1facf9fc 30202+
30203+ err = 0;
027c5e7a
AM
30204+ sigen = au_sigen(sb);
30205+ for (ull = 0; ull < max; ull++) {
30206+ inode = array[ull];
076b876e
AM
30207+ if (unlikely(!inode))
30208+ break;
b95c5147
AM
30209+
30210+ e = 0;
30211+ ii_write_lock_child(inode);
537831f9 30212+ if (au_iigen(inode, NULL) != sigen) {
027c5e7a 30213+ e = au_refresh_hinode_self(inode);
1facf9fc 30214+ if (unlikely(e)) {
b95c5147 30215+ au_refresh_iop(inode, /*force_getattr*/1);
027c5e7a 30216+ pr_err("error %d, i%lu\n", e, inode->i_ino);
1facf9fc 30217+ if (!err)
30218+ err = e;
30219+ /* go on even if err */
30220+ }
30221+ }
b95c5147
AM
30222+ if (!e && do_idop)
30223+ au_refresh_iop(inode, /*force_getattr*/0);
30224+ ii_write_unlock(inode);
1facf9fc 30225+ }
30226+
027c5e7a 30227+ au_iarray_free(array, max);
1facf9fc 30228+
4f0767ce 30229+out:
1facf9fc 30230+ return err;
30231+}
30232+
b95c5147 30233+static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
1facf9fc 30234+{
027c5e7a
AM
30235+ int err, e;
30236+ unsigned int udba;
5afbbe0d 30237+ aufs_bindex_t bindex, bbot;
1facf9fc 30238+ struct dentry *root;
30239+ struct inode *inode;
027c5e7a 30240+ struct au_branch *br;
79b8bda9 30241+ struct au_sbinfo *sbi;
1facf9fc 30242+
30243+ au_sigen_inc(sb);
79b8bda9
AM
30244+ sbi = au_sbi(sb);
30245+ au_fclr_si(sbi, FAILED_REFRESH_DIR);
1facf9fc 30246+
30247+ root = sb->s_root;
30248+ DiMustNoWaiters(root);
5527c038 30249+ inode = d_inode(root);
1facf9fc 30250+ IiMustNoWaiters(inode);
1facf9fc 30251+
027c5e7a 30252+ udba = au_opt_udba(sb);
5afbbe0d
AM
30253+ bbot = au_sbbot(sb);
30254+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
30255+ br = au_sbr(sb, bindex);
30256+ err = au_hnotify_reset_br(udba, br, br->br_perm);
1facf9fc 30257+ if (unlikely(err))
027c5e7a
AM
30258+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
30259+ bindex, err);
30260+ /* go on even if err */
1facf9fc 30261+ }
027c5e7a 30262+ au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
1facf9fc 30263+
b95c5147 30264+ if (do_idop) {
79b8bda9
AM
30265+ if (au_ftest_si(sbi, NO_DREVAL)) {
30266+ AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30267+ sb->s_d_op = &aufs_dop_noreval;
b95c5147
AM
30268+ AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30269+ sbi->si_iop_array = aufs_iop_nogetattr;
79b8bda9
AM
30270+ } else {
30271+ AuDebugOn(sb->s_d_op == &aufs_dop);
30272+ sb->s_d_op = &aufs_dop;
b95c5147
AM
30273+ AuDebugOn(sbi->si_iop_array == aufs_iop);
30274+ sbi->si_iop_array = aufs_iop;
79b8bda9 30275+ }
062440b3 30276+ pr_info("reset to %ps and %ps\n",
b95c5147 30277+ sb->s_d_op, sbi->si_iop_array);
79b8bda9
AM
30278+ }
30279+
027c5e7a 30280+ di_write_unlock(root);
b95c5147
AM
30281+ err = au_refresh_d(sb, do_idop);
30282+ e = au_refresh_i(sb, do_idop);
027c5e7a
AM
30283+ if (unlikely(e && !err))
30284+ err = e;
1facf9fc 30285+ /* aufs_write_lock() calls ..._child() */
30286+ di_write_lock_child(root);
027c5e7a
AM
30287+
30288+ au_cpup_attr_all(inode, /*force*/1);
30289+
30290+ if (unlikely(err))
30291+ AuIOErr("refresh failed, ignored, %d\n", err);
1facf9fc 30292+}
30293+
30294+/* stop extra interpretation of errno in mount(8), and strange error messages */
30295+static int cvt_err(int err)
30296+{
30297+ AuTraceErr(err);
30298+
30299+ switch (err) {
30300+ case -ENOENT:
30301+ case -ENOTDIR:
30302+ case -EEXIST:
30303+ case -EIO:
30304+ err = -EINVAL;
30305+ }
30306+ return err;
30307+}
30308+
30309+static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
30310+{
4a4d8108
AM
30311+ int err, do_dx;
30312+ unsigned int mntflags;
be52b249
AM
30313+ struct au_opts opts = {
30314+ .opt = NULL
30315+ };
1facf9fc 30316+ struct dentry *root;
30317+ struct inode *inode;
30318+ struct au_sbinfo *sbinfo;
30319+
30320+ err = 0;
30321+ root = sb->s_root;
30322+ if (!data || !*data) {
e49829fe
JR
30323+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30324+ if (!err) {
30325+ di_write_lock_child(root);
30326+ err = au_opts_verify(sb, *flags, /*pending*/0);
30327+ aufs_write_unlock(root);
30328+ }
1facf9fc 30329+ goto out;
30330+ }
30331+
30332+ err = -ENOMEM;
1facf9fc 30333+ opts.opt = (void *)__get_free_page(GFP_NOFS);
30334+ if (unlikely(!opts.opt))
30335+ goto out;
30336+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30337+ opts.flags = AuOpts_REMOUNT;
30338+ opts.sb_flags = *flags;
30339+
30340+ /* parse it before aufs lock */
30341+ err = au_opts_parse(sb, data, &opts);
30342+ if (unlikely(err))
30343+ goto out_opts;
30344+
30345+ sbinfo = au_sbi(sb);
5527c038 30346+ inode = d_inode(root);
febd17d6 30347+ inode_lock(inode);
e49829fe
JR
30348+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30349+ if (unlikely(err))
30350+ goto out_mtx;
30351+ di_write_lock_child(root);
1facf9fc 30352+
30353+ /* au_opts_remount() may return an error */
30354+ err = au_opts_remount(sb, &opts);
30355+ au_opts_free(&opts);
30356+
027c5e7a 30357+ if (au_ftest_opts(opts.flags, REFRESH))
b95c5147 30358+ au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
1facf9fc 30359+
4a4d8108
AM
30360+ if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
30361+ mntflags = au_mntflags(sb);
30362+ do_dx = !!au_opt_test(mntflags, DIO);
30363+ au_dy_arefresh(do_dx);
30364+ }
30365+
076b876e 30366+ au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
1facf9fc 30367+ aufs_write_unlock(root);
953406b4 30368+
e49829fe 30369+out_mtx:
febd17d6 30370+ inode_unlock(inode);
4f0767ce 30371+out_opts:
1c60b727 30372+ free_page((unsigned long)opts.opt);
4f0767ce 30373+out:
1facf9fc 30374+ err = cvt_err(err);
30375+ AuTraceErr(err);
30376+ return err;
30377+}
30378+
4a4d8108 30379+static const struct super_operations aufs_sop = {
1facf9fc 30380+ .alloc_inode = aufs_alloc_inode,
30381+ .destroy_inode = aufs_destroy_inode,
b752ccd1 30382+ /* always deleting, no clearing */
1facf9fc 30383+ .drop_inode = generic_delete_inode,
30384+ .show_options = aufs_show_options,
30385+ .statfs = aufs_statfs,
30386+ .put_super = aufs_put_super,
537831f9 30387+ .sync_fs = aufs_sync_fs,
1facf9fc 30388+ .remount_fs = aufs_remount_fs
30389+};
30390+
30391+/* ---------------------------------------------------------------------- */
30392+
30393+static int alloc_root(struct super_block *sb)
30394+{
30395+ int err;
30396+ struct inode *inode;
30397+ struct dentry *root;
30398+
30399+ err = -ENOMEM;
30400+ inode = au_iget_locked(sb, AUFS_ROOT_INO);
30401+ err = PTR_ERR(inode);
30402+ if (IS_ERR(inode))
30403+ goto out;
30404+
b95c5147 30405+ inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
1facf9fc 30406+ inode->i_fop = &aufs_dir_fop;
30407+ inode->i_mode = S_IFDIR;
9dbd164d 30408+ set_nlink(inode, 2);
1facf9fc 30409+ unlock_new_inode(inode);
30410+
92d182d2 30411+ root = d_make_root(inode);
1facf9fc 30412+ if (unlikely(!root))
92d182d2 30413+ goto out;
1facf9fc 30414+ err = PTR_ERR(root);
30415+ if (IS_ERR(root))
92d182d2 30416+ goto out;
1facf9fc 30417+
4a4d8108 30418+ err = au_di_init(root);
1facf9fc 30419+ if (!err) {
30420+ sb->s_root = root;
30421+ return 0; /* success */
30422+ }
30423+ dput(root);
1facf9fc 30424+
4f0767ce 30425+out:
1facf9fc 30426+ return err;
1facf9fc 30427+}
30428+
30429+static int aufs_fill_super(struct super_block *sb, void *raw_data,
30430+ int silent __maybe_unused)
30431+{
30432+ int err;
be52b249
AM
30433+ struct au_opts opts = {
30434+ .opt = NULL
30435+ };
79b8bda9 30436+ struct au_sbinfo *sbinfo;
1facf9fc 30437+ struct dentry *root;
30438+ struct inode *inode;
30439+ char *arg = raw_data;
30440+
30441+ if (unlikely(!arg || !*arg)) {
30442+ err = -EINVAL;
4a4d8108 30443+ pr_err("no arg\n");
1facf9fc 30444+ goto out;
30445+ }
30446+
30447+ err = -ENOMEM;
1facf9fc 30448+ opts.opt = (void *)__get_free_page(GFP_NOFS);
30449+ if (unlikely(!opts.opt))
30450+ goto out;
30451+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30452+ opts.sb_flags = sb->s_flags;
30453+
30454+ err = au_si_alloc(sb);
30455+ if (unlikely(err))
30456+ goto out_opts;
79b8bda9 30457+ sbinfo = au_sbi(sb);
1facf9fc 30458+
30459+ /* all timestamps always follow the ones on the branch */
2121bcd9 30460+ sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
be118d29 30461+ sb->s_flags |= SB_I_VERSION; /* do we really need this? */
1facf9fc 30462+ sb->s_op = &aufs_sop;
027c5e7a 30463+ sb->s_d_op = &aufs_dop;
1facf9fc 30464+ sb->s_magic = AUFS_SUPER_MAGIC;
30465+ sb->s_maxbytes = 0;
c1595e42 30466+ sb->s_stack_depth = 1;
1facf9fc 30467+ au_export_init(sb);
f2c43d5f 30468+ au_xattr_init(sb);
1facf9fc 30469+
30470+ err = alloc_root(sb);
30471+ if (unlikely(err)) {
30472+ si_write_unlock(sb);
30473+ goto out_info;
30474+ }
30475+ root = sb->s_root;
5527c038 30476+ inode = d_inode(root);
1facf9fc 30477+
30478+ /*
30479+ * actually we can parse options regardless aufs lock here.
30480+ * but at remount time, parsing must be done before aufs lock.
30481+ * so we follow the same rule.
30482+ */
30483+ ii_write_lock_parent(inode);
30484+ aufs_write_unlock(root);
30485+ err = au_opts_parse(sb, arg, &opts);
30486+ if (unlikely(err))
30487+ goto out_root;
30488+
30489+ /* lock vfs_inode first, then aufs. */
febd17d6 30490+ inode_lock(inode);
1facf9fc 30491+ aufs_write_lock(root);
30492+ err = au_opts_mount(sb, &opts);
30493+ au_opts_free(&opts);
79b8bda9
AM
30494+ if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
30495+ sb->s_d_op = &aufs_dop_noreval;
062440b3 30496+ pr_info("%ps\n", sb->s_d_op);
79b8bda9 30497+ au_refresh_dop(root, /*force_reval*/0);
b95c5147
AM
30498+ sbinfo->si_iop_array = aufs_iop_nogetattr;
30499+ au_refresh_iop(inode, /*force_getattr*/0);
79b8bda9 30500+ }
1facf9fc 30501+ aufs_write_unlock(root);
febd17d6 30502+ inode_unlock(inode);
4a4d8108
AM
30503+ if (!err)
30504+ goto out_opts; /* success */
1facf9fc 30505+
4f0767ce 30506+out_root:
1facf9fc 30507+ dput(root);
30508+ sb->s_root = NULL;
4f0767ce 30509+out_info:
79b8bda9 30510+ kobject_put(&sbinfo->si_kobj);
1facf9fc 30511+ sb->s_fs_info = NULL;
4f0767ce 30512+out_opts:
1c60b727 30513+ free_page((unsigned long)opts.opt);
4f0767ce 30514+out:
1facf9fc 30515+ AuTraceErr(err);
30516+ err = cvt_err(err);
30517+ AuTraceErr(err);
30518+ return err;
30519+}
30520+
30521+/* ---------------------------------------------------------------------- */
30522+
027c5e7a
AM
30523+static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
30524+ const char *dev_name __maybe_unused,
30525+ void *raw_data)
1facf9fc 30526+{
027c5e7a 30527+ struct dentry *root;
1facf9fc 30528+
30529+ /* all timestamps always follow the ones on the branch */
30530+ /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
027c5e7a
AM
30531+ root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
30532+ if (IS_ERR(root))
30533+ goto out;
30534+
062440b3 30535+ au_sbilist_add(root->d_sb);
027c5e7a
AM
30536+
30537+out:
30538+ return root;
1facf9fc 30539+}
30540+
e49829fe
JR
30541+static void aufs_kill_sb(struct super_block *sb)
30542+{
30543+ struct au_sbinfo *sbinfo;
30544+
30545+ sbinfo = au_sbi(sb);
30546+ if (sbinfo) {
30547+ au_sbilist_del(sb);
30548+ aufs_write_lock(sb->s_root);
076b876e 30549+ au_fhsm_fin(sb);
e49829fe
JR
30550+ if (sbinfo->si_wbr_create_ops->fin)
30551+ sbinfo->si_wbr_create_ops->fin(sb);
30552+ if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30553+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
b95c5147 30554+ au_remount_refresh(sb, /*do_idop*/0);
e49829fe
JR
30555+ }
30556+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
30557+ au_plink_put(sb, /*verbose*/1);
30558+ au_xino_clr(sb);
8b6a4947 30559+ au_dr_opt_flush(sb);
1e00d052 30560+ sbinfo->si_sb = NULL;
e49829fe 30561+ aufs_write_unlock(sb->s_root);
e49829fe
JR
30562+ au_nwt_flush(&sbinfo->si_nowait);
30563+ }
98d9a5b1 30564+ kill_anon_super(sb);
e49829fe
JR
30565+}
30566+
1facf9fc 30567+struct file_system_type aufs_fs_type = {
30568+ .name = AUFS_FSTYPE,
c06a8ce3
AM
30569+ /* a race between rename and others */
30570+ .fs_flags = FS_RENAME_DOES_D_MOVE,
027c5e7a 30571+ .mount = aufs_mount,
e49829fe 30572+ .kill_sb = aufs_kill_sb,
1facf9fc 30573+ /* no need to __module_get() and module_put(). */
30574+ .owner = THIS_MODULE,
30575+};
7f207e10
AM
30576diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30577--- /usr/share/empty/fs/aufs/super.h 1970-01-01 01:00:00.000000000 +0100
e49925d1 30578+++ linux/fs/aufs/super.h 2019-01-28 14:36:12.222418047 +0100
acd2b654 30579@@ -0,0 +1,589 @@
062440b3 30580+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 30581+/*
b00004a5 30582+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 30583+ *
30584+ * This program, aufs is free software; you can redistribute it and/or modify
30585+ * it under the terms of the GNU General Public License as published by
30586+ * the Free Software Foundation; either version 2 of the License, or
30587+ * (at your option) any later version.
dece6358
AM
30588+ *
30589+ * This program is distributed in the hope that it will be useful,
30590+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30591+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30592+ * GNU General Public License for more details.
30593+ *
30594+ * You should have received a copy of the GNU General Public License
523b37e3 30595+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30596+ */
30597+
30598+/*
30599+ * super_block operations
30600+ */
30601+
30602+#ifndef __AUFS_SUPER_H__
30603+#define __AUFS_SUPER_H__
30604+
30605+#ifdef __KERNEL__
30606+
30607+#include <linux/fs.h>
5527c038 30608+#include <linux/kobject.h>
8b6a4947 30609+#include "hbl.h"
acd2b654 30610+#include "lcnt.h"
1facf9fc 30611+#include "rwsem.h"
1facf9fc 30612+#include "wkq.h"
30613+
1facf9fc 30614+/* policies to select one among multiple writable branches */
30615+struct au_wbr_copyup_operations {
30616+ int (*copyup)(struct dentry *dentry);
30617+};
30618+
392086de
AM
30619+#define AuWbr_DIR 1 /* target is a dir */
30620+#define AuWbr_PARENT (1 << 1) /* always require a parent */
30621+
30622+#define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
30623+#define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
30624+#define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
30625+
1facf9fc 30626+struct au_wbr_create_operations {
392086de 30627+ int (*create)(struct dentry *dentry, unsigned int flags);
1facf9fc 30628+ int (*init)(struct super_block *sb);
30629+ int (*fin)(struct super_block *sb);
30630+};
30631+
30632+struct au_wbr_mfs {
30633+ struct mutex mfs_lock; /* protect this structure */
30634+ unsigned long mfs_jiffy;
30635+ unsigned long mfs_expire;
30636+ aufs_bindex_t mfs_bindex;
30637+
30638+ unsigned long long mfsrr_bytes;
30639+ unsigned long long mfsrr_watermark;
30640+};
30641+
86dc4139
AM
30642+#define AuPlink_NHASH 100
30643+static inline int au_plink_hash(ino_t ino)
30644+{
30645+ return ino % AuPlink_NHASH;
30646+}
30647+
076b876e
AM
30648+/* File-based Hierarchical Storage Management */
30649+struct au_fhsm {
30650+#ifdef CONFIG_AUFS_FHSM
30651+ /* allow only one process who can receive the notification */
30652+ spinlock_t fhsm_spin;
30653+ pid_t fhsm_pid;
30654+ wait_queue_head_t fhsm_wqh;
30655+ atomic_t fhsm_readable;
30656+
c1595e42 30657+ /* these are protected by si_rwsem */
076b876e 30658+ unsigned long fhsm_expire;
c1595e42 30659+ aufs_bindex_t fhsm_bottom;
076b876e
AM
30660+#endif
30661+};
30662+
1facf9fc 30663+struct au_branch;
30664+struct au_sbinfo {
30665+ /* nowait tasks in the system-wide workqueue */
30666+ struct au_nowait_tasks si_nowait;
30667+
b752ccd1 30668+ /*
acd2b654 30669+ * tried sb->s_umount, but failed due to the dependency between i_mutex.
b752ccd1
AM
30670+ * rwsem for au_sbinfo is necessary.
30671+ */
dece6358 30672+ struct au_rwsem si_rwsem;
1facf9fc 30673+
7f207e10 30674+ /*
523b37e3
AM
30675+ * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30676+ * remount.
7f207e10 30677+ */
acd2b654 30678+ au_lcnt_t si_ninodes, si_nfiles;
7f207e10 30679+
1facf9fc 30680+ /* branch management */
30681+ unsigned int si_generation;
30682+
2000de60 30683+ /* see AuSi_ flags */
1facf9fc 30684+ unsigned char au_si_status;
30685+
5afbbe0d 30686+ aufs_bindex_t si_bbot;
7f207e10
AM
30687+
30688+ /* dirty trick to keep br_id plus */
30689+ unsigned int si_last_br_id :
30690+ sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
1facf9fc 30691+ struct au_branch **si_branch;
30692+
30693+ /* policy to select a writable branch */
30694+ unsigned char si_wbr_copyup;
30695+ unsigned char si_wbr_create;
30696+ struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30697+ struct au_wbr_create_operations *si_wbr_create_ops;
30698+
30699+ /* round robin */
30700+ atomic_t si_wbr_rr_next;
30701+
30702+ /* most free space */
30703+ struct au_wbr_mfs si_wbr_mfs;
30704+
076b876e
AM
30705+ /* File-based Hierarchical Storage Management */
30706+ struct au_fhsm si_fhsm;
30707+
1facf9fc 30708+ /* mount flags */
30709+ /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30710+ unsigned int si_mntflags;
30711+
30712+ /* external inode number (bitmap and translation table) */
5527c038
JR
30713+ vfs_readf_t si_xread;
30714+ vfs_writef_t si_xwrite;
acd2b654
AM
30715+ loff_t si_ximaxent; /* max entries in a xino */
30716+
1facf9fc 30717+ struct file *si_xib;
30718+ struct mutex si_xib_mtx; /* protect xib members */
30719+ unsigned long *si_xib_buf;
30720+ unsigned long si_xib_last_pindex;
30721+ int si_xib_next_bit;
acd2b654 30722+
392086de
AM
30723+ unsigned long si_xino_jiffy;
30724+ unsigned long si_xino_expire;
1facf9fc 30725+ /* reserved for future use */
30726+ /* unsigned long long si_xib_limit; */ /* Max xib file size */
30727+
30728+#ifdef CONFIG_AUFS_EXPORT
30729+ /* i_generation */
acd2b654 30730+ /* todo: make xigen file an array to support many inode numbers */
1facf9fc 30731+ struct file *si_xigen;
30732+ atomic_t si_xigen_next;
30733+#endif
30734+
acd2b654 30735+ /* dirty trick to support atomic_open */
8b6a4947 30736+ struct hlist_bl_head si_aopen;
b912730e 30737+
1facf9fc 30738+ /* vdir parameters */
e49829fe 30739+ unsigned long si_rdcache; /* max cache time in jiffies */
1facf9fc 30740+ unsigned int si_rdblk; /* deblk size */
30741+ unsigned int si_rdhash; /* hash size */
30742+
30743+ /*
30744+ * If the number of whiteouts are larger than si_dirwh, leave all of
30745+ * them after au_whtmp_ren to reduce the cost of rmdir(2).
30746+ * future fsck.aufs or kernel thread will remove them later.
30747+ * Otherwise, remove all whiteouts and the dir in rmdir(2).
30748+ */
30749+ unsigned int si_dirwh;
30750+
1facf9fc 30751+ /* pseudo_link list */
8b6a4947 30752+ struct hlist_bl_head si_plink[AuPlink_NHASH];
1facf9fc 30753+ wait_queue_head_t si_plink_wq;
4a4d8108 30754+ spinlock_t si_plink_maint_lock;
e49829fe 30755+ pid_t si_plink_maint_pid;
1facf9fc 30756+
523b37e3 30757+ /* file list */
8b6a4947 30758+ struct hlist_bl_head si_files;
523b37e3 30759+
b95c5147
AM
30760+ /* with/without getattr, brother of sb->s_d_op */
30761+ struct inode_operations *si_iop_array;
30762+
1facf9fc 30763+ /*
30764+ * sysfs and lifetime management.
30765+ * this is not a small structure and it may be a waste of memory in case
acd2b654 30766+ * of sysfs is disabled, particularly when many aufs-es are mounted.
1facf9fc 30767+ * but using sysfs is majority.
30768+ */
30769+ struct kobject si_kobj;
30770+#ifdef CONFIG_DEBUG_FS
86dc4139
AM
30771+ struct dentry *si_dbgaufs;
30772+ struct dentry *si_dbgaufs_plink;
30773+ struct dentry *si_dbgaufs_xib;
1facf9fc 30774+#ifdef CONFIG_AUFS_EXPORT
30775+ struct dentry *si_dbgaufs_xigen;
30776+#endif
30777+#endif
30778+
e49829fe 30779+#ifdef CONFIG_AUFS_SBILIST
8b6a4947 30780+ struct hlist_bl_node si_list;
e49829fe
JR
30781+#endif
30782+
1facf9fc 30783+ /* dirty, necessary for unmounting, sysfs and sysrq */
30784+ struct super_block *si_sb;
30785+};
30786+
dece6358
AM
30787+/* sbinfo status flags */
30788+/*
30789+ * set true when refresh_dirs() failed at remount time.
30790+ * then try refreshing dirs at access time again.
062440b3 30791+ * if it is false, refreshing dirs at access time is unnecessary
dece6358 30792+ */
027c5e7a 30793+#define AuSi_FAILED_REFRESH_DIR 1
076b876e 30794+#define AuSi_FHSM (1 << 1) /* fhsm is active now */
79b8bda9 30795+#define AuSi_NO_DREVAL (1 << 2) /* disable all d_revalidate */
076b876e
AM
30796+
30797+#ifndef CONFIG_AUFS_FHSM
30798+#undef AuSi_FHSM
30799+#define AuSi_FHSM 0
30800+#endif
30801+
dece6358
AM
30802+static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30803+ unsigned int flag)
30804+{
30805+ AuRwMustAnyLock(&sbi->si_rwsem);
30806+ return sbi->au_si_status & flag;
30807+}
30808+#define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
30809+#define au_fset_si(sbinfo, name) do { \
30810+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30811+ (sbinfo)->au_si_status |= AuSi_##name; \
30812+} while (0)
30813+#define au_fclr_si(sbinfo, name) do { \
30814+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30815+ (sbinfo)->au_si_status &= ~AuSi_##name; \
30816+} while (0)
30817+
1facf9fc 30818+/* ---------------------------------------------------------------------- */
30819+
30820+/* policy to select one among writable branches */
4a4d8108
AM
30821+#define AuWbrCopyup(sbinfo, ...) \
30822+ ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30823+#define AuWbrCreate(sbinfo, ...) \
30824+ ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
1facf9fc 30825+
30826+/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30827+#define AuLock_DW 1 /* write-lock dentry */
30828+#define AuLock_IR (1 << 1) /* read-lock inode */
30829+#define AuLock_IW (1 << 2) /* write-lock inode */
30830+#define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
b95c5147 30831+#define AuLock_DIRS (1 << 4) /* target is a pair of dirs */
f2c43d5f 30832+ /* except RENAME_EXCHANGE */
e49829fe
JR
30833+#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
30834+#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
027c5e7a 30835+#define AuLock_GEN (1 << 7) /* test digen/iigen */
1facf9fc 30836+#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
7f207e10
AM
30837+#define au_fset_lock(flags, name) \
30838+ do { (flags) |= AuLock_##name; } while (0)
30839+#define au_fclr_lock(flags, name) \
30840+ do { (flags) &= ~AuLock_##name; } while (0)
1facf9fc 30841+
30842+/* ---------------------------------------------------------------------- */
30843+
30844+/* super.c */
30845+extern struct file_system_type aufs_fs_type;
30846+struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
79b8bda9
AM
30847+typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30848+ unsigned long long max, void *arg);
79b8bda9
AM
30849+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30850+ struct super_block *sb, void *arg);
7f207e10
AM
30851+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30852+void au_iarray_free(struct inode **a, unsigned long long max);
1facf9fc 30853+
30854+/* sbinfo.c */
30855+void au_si_free(struct kobject *kobj);
30856+int au_si_alloc(struct super_block *sb);
e2f27e51 30857+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
1facf9fc 30858+
30859+unsigned int au_sigen_inc(struct super_block *sb);
30860+aufs_bindex_t au_new_br_id(struct super_block *sb);
30861+
e49829fe
JR
30862+int si_read_lock(struct super_block *sb, int flags);
30863+int si_write_lock(struct super_block *sb, int flags);
30864+int aufs_read_lock(struct dentry *dentry, int flags);
1facf9fc 30865+void aufs_read_unlock(struct dentry *dentry, int flags);
30866+void aufs_write_lock(struct dentry *dentry);
30867+void aufs_write_unlock(struct dentry *dentry);
e49829fe 30868+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
1facf9fc 30869+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30870+
30871+/* wbr_policy.c */
30872+extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30873+extern struct au_wbr_create_operations au_wbr_create_ops[];
30874+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
c2b27bf2 30875+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
5afbbe0d 30876+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
c2b27bf2
AM
30877+
30878+/* mvdown.c */
30879+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
1facf9fc 30880+
076b876e
AM
30881+#ifdef CONFIG_AUFS_FHSM
30882+/* fhsm.c */
30883+
30884+static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30885+{
30886+ pid_t pid;
30887+
30888+ spin_lock(&fhsm->fhsm_spin);
30889+ pid = fhsm->fhsm_pid;
30890+ spin_unlock(&fhsm->fhsm_spin);
30891+
30892+ return pid;
30893+}
30894+
30895+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
30896+void au_fhsm_wrote_all(struct super_block *sb, int force);
30897+int au_fhsm_fd(struct super_block *sb, int oflags);
30898+int au_fhsm_br_alloc(struct au_branch *br);
c1595e42 30899+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
076b876e
AM
30900+void au_fhsm_fin(struct super_block *sb);
30901+void au_fhsm_init(struct au_sbinfo *sbinfo);
30902+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
30903+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
30904+#else
30905+AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
30906+ int force)
30907+AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
30908+AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
c1595e42
JR
30909+AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
30910+AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
30911+AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
076b876e
AM
30912+AuStubVoid(au_fhsm_fin, struct super_block *sb)
30913+AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
30914+AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
30915+AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
30916+#endif
30917+
1facf9fc 30918+/* ---------------------------------------------------------------------- */
30919+
30920+static inline struct au_sbinfo *au_sbi(struct super_block *sb)
30921+{
30922+ return sb->s_fs_info;
30923+}
30924+
30925+/* ---------------------------------------------------------------------- */
30926+
30927+#ifdef CONFIG_AUFS_EXPORT
a2a7ad62 30928+int au_test_nfsd(void);
1facf9fc 30929+void au_export_init(struct super_block *sb);
b752ccd1 30930+void au_xigen_inc(struct inode *inode);
1facf9fc 30931+int au_xigen_new(struct inode *inode);
062440b3 30932+int au_xigen_set(struct super_block *sb, struct path *path);
1facf9fc 30933+void au_xigen_clr(struct super_block *sb);
30934+
30935+static inline int au_busy_or_stale(void)
30936+{
b752ccd1 30937+ if (!au_test_nfsd())
1facf9fc 30938+ return -EBUSY;
30939+ return -ESTALE;
30940+}
30941+#else
b752ccd1 30942+AuStubInt0(au_test_nfsd, void)
a2a7ad62 30943+AuStubVoid(au_export_init, struct super_block *sb)
b752ccd1 30944+AuStubVoid(au_xigen_inc, struct inode *inode)
4a4d8108 30945+AuStubInt0(au_xigen_new, struct inode *inode)
062440b3 30946+AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
4a4d8108 30947+AuStubVoid(au_xigen_clr, struct super_block *sb)
c1595e42 30948+AuStub(int, au_busy_or_stale, return -EBUSY, void)
1facf9fc 30949+#endif /* CONFIG_AUFS_EXPORT */
30950+
30951+/* ---------------------------------------------------------------------- */
30952+
e49829fe
JR
30953+#ifdef CONFIG_AUFS_SBILIST
30954+/* module.c */
8b6a4947 30955+extern struct hlist_bl_head au_sbilist;
e49829fe
JR
30956+
30957+static inline void au_sbilist_init(void)
30958+{
8b6a4947 30959+ INIT_HLIST_BL_HEAD(&au_sbilist);
e49829fe
JR
30960+}
30961+
30962+static inline void au_sbilist_add(struct super_block *sb)
30963+{
8b6a4947 30964+ au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe
JR
30965+}
30966+
30967+static inline void au_sbilist_del(struct super_block *sb)
30968+{
8b6a4947 30969+ au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe 30970+}
53392da6
AM
30971+
30972+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
30973+static inline void au_sbilist_lock(void)
30974+{
8b6a4947 30975+ hlist_bl_lock(&au_sbilist);
53392da6
AM
30976+}
30977+
30978+static inline void au_sbilist_unlock(void)
30979+{
8b6a4947 30980+ hlist_bl_unlock(&au_sbilist);
53392da6
AM
30981+}
30982+#define AuGFP_SBILIST GFP_ATOMIC
30983+#else
30984+AuStubVoid(au_sbilist_lock, void)
30985+AuStubVoid(au_sbilist_unlock, void)
30986+#define AuGFP_SBILIST GFP_NOFS
30987+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
e49829fe
JR
30988+#else
30989+AuStubVoid(au_sbilist_init, void)
c1595e42
JR
30990+AuStubVoid(au_sbilist_add, struct super_block *sb)
30991+AuStubVoid(au_sbilist_del, struct super_block *sb)
53392da6
AM
30992+AuStubVoid(au_sbilist_lock, void)
30993+AuStubVoid(au_sbilist_unlock, void)
30994+#define AuGFP_SBILIST GFP_NOFS
e49829fe
JR
30995+#endif
30996+
30997+/* ---------------------------------------------------------------------- */
30998+
1facf9fc 30999+static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
31000+{
dece6358 31001+ /*
c1595e42 31002+ * This function is a dynamic '__init' function actually,
dece6358
AM
31003+ * so the tiny check for si_rwsem is unnecessary.
31004+ */
31005+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
1facf9fc 31006+#ifdef CONFIG_DEBUG_FS
31007+ sbinfo->si_dbgaufs = NULL;
86dc4139 31008+ sbinfo->si_dbgaufs_plink = NULL;
1facf9fc 31009+ sbinfo->si_dbgaufs_xib = NULL;
31010+#ifdef CONFIG_AUFS_EXPORT
31011+ sbinfo->si_dbgaufs_xigen = NULL;
31012+#endif
31013+#endif
31014+}
31015+
31016+/* ---------------------------------------------------------------------- */
31017+
a2654f78
AM
31018+/* current->atomic_flags */
31019+/* this value should never corrupt the ones defined in linux/sched.h */
31020+#define PFA_AUFS 7
31021+
31022+TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
31023+TASK_PFA_SET(AUFS, aufs) /* task_set_aufs */
31024+TASK_PFA_CLEAR(AUFS, aufs) /* task_clear_aufs */
b752ccd1
AM
31025+
31026+static inline int si_pid_test(struct super_block *sb)
31027+{
a2654f78 31028+ return !!task_test_aufs(current);
b752ccd1
AM
31029+}
31030+
31031+static inline void si_pid_clr(struct super_block *sb)
31032+{
a2654f78
AM
31033+ AuDebugOn(!task_test_aufs(current));
31034+ task_clear_aufs(current);
b752ccd1
AM
31035+}
31036+
a2654f78
AM
31037+static inline void si_pid_set(struct super_block *sb)
31038+{
31039+ AuDebugOn(task_test_aufs(current));
31040+ task_set_aufs(current);
31041+}
febd17d6 31042+
b752ccd1
AM
31043+/* ---------------------------------------------------------------------- */
31044+
1facf9fc 31045+/* lock superblock. mainly for entry point functions */
8b6a4947
AM
31046+#define __si_read_lock(sb) au_rw_read_lock(&au_sbi(sb)->si_rwsem)
31047+#define __si_write_lock(sb) au_rw_write_lock(&au_sbi(sb)->si_rwsem)
31048+#define __si_read_trylock(sb) au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
31049+#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
31050+/*
31051+#define __si_read_trylock_nested(sb) \
31052+ au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
31053+#define __si_write_trylock_nested(sb) \
31054+ au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
31055+*/
31056+
31057+#define __si_read_unlock(sb) au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
31058+#define __si_write_unlock(sb) au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
31059+#define __si_downgrade_lock(sb) au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
1facf9fc 31060+
dece6358
AM
31061+#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
31062+#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
31063+#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
31064+
b752ccd1
AM
31065+static inline void si_noflush_read_lock(struct super_block *sb)
31066+{
31067+ __si_read_lock(sb);
31068+ si_pid_set(sb);
31069+}
31070+
31071+static inline int si_noflush_read_trylock(struct super_block *sb)
31072+{
076b876e
AM
31073+ int locked;
31074+
31075+ locked = __si_read_trylock(sb);
b752ccd1
AM
31076+ if (locked)
31077+ si_pid_set(sb);
31078+ return locked;
31079+}
31080+
31081+static inline void si_noflush_write_lock(struct super_block *sb)
31082+{
31083+ __si_write_lock(sb);
31084+ si_pid_set(sb);
31085+}
31086+
31087+static inline int si_noflush_write_trylock(struct super_block *sb)
31088+{
076b876e
AM
31089+ int locked;
31090+
31091+ locked = __si_write_trylock(sb);
b752ccd1
AM
31092+ if (locked)
31093+ si_pid_set(sb);
31094+ return locked;
31095+}
31096+
7e9cd9fe 31097+#if 0 /* reserved */
1facf9fc 31098+static inline int si_read_trylock(struct super_block *sb, int flags)
31099+{
31100+ if (au_ftest_lock(flags, FLUSH))
31101+ au_nwt_flush(&au_sbi(sb)->si_nowait);
31102+ return si_noflush_read_trylock(sb);
31103+}
e49829fe 31104+#endif
1facf9fc 31105+
b752ccd1
AM
31106+static inline void si_read_unlock(struct super_block *sb)
31107+{
31108+ si_pid_clr(sb);
31109+ __si_read_unlock(sb);
31110+}
31111+
7e9cd9fe 31112+#if 0 /* reserved */
1facf9fc 31113+static inline int si_write_trylock(struct super_block *sb, int flags)
31114+{
31115+ if (au_ftest_lock(flags, FLUSH))
31116+ au_nwt_flush(&au_sbi(sb)->si_nowait);
31117+ return si_noflush_write_trylock(sb);
31118+}
b752ccd1
AM
31119+#endif
31120+
31121+static inline void si_write_unlock(struct super_block *sb)
31122+{
31123+ si_pid_clr(sb);
31124+ __si_write_unlock(sb);
31125+}
31126+
7e9cd9fe 31127+#if 0 /* reserved */
b752ccd1
AM
31128+static inline void si_downgrade_lock(struct super_block *sb)
31129+{
31130+ __si_downgrade_lock(sb);
31131+}
31132+#endif
1facf9fc 31133+
31134+/* ---------------------------------------------------------------------- */
31135+
5afbbe0d 31136+static inline aufs_bindex_t au_sbbot(struct super_block *sb)
1facf9fc 31137+{
dece6358 31138+ SiMustAnyLock(sb);
5afbbe0d 31139+ return au_sbi(sb)->si_bbot;
1facf9fc 31140+}
31141+
31142+static inline unsigned int au_mntflags(struct super_block *sb)
31143+{
dece6358 31144+ SiMustAnyLock(sb);
1facf9fc 31145+ return au_sbi(sb)->si_mntflags;
31146+}
31147+
31148+static inline unsigned int au_sigen(struct super_block *sb)
31149+{
dece6358 31150+ SiMustAnyLock(sb);
1facf9fc 31151+ return au_sbi(sb)->si_generation;
31152+}
31153+
31154+static inline struct au_branch *au_sbr(struct super_block *sb,
31155+ aufs_bindex_t bindex)
31156+{
dece6358 31157+ SiMustAnyLock(sb);
1facf9fc 31158+ return au_sbi(sb)->si_branch[0 + bindex];
31159+}
31160+
acd2b654 31161+static inline loff_t au_xi_maxent(struct super_block *sb)
1facf9fc 31162+{
dece6358 31163+ SiMustAnyLock(sb);
acd2b654 31164+ return au_sbi(sb)->si_ximaxent;
1facf9fc 31165+}
31166+
31167+#endif /* __KERNEL__ */
31168+#endif /* __AUFS_SUPER_H__ */
7f207e10
AM
31169diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
31170--- /usr/share/empty/fs/aufs/sysaufs.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 31171+++ linux/fs/aufs/sysaufs.c 2019-01-28 14:36:12.222418047 +0100
062440b3 31172@@ -0,0 +1,93 @@
cd7a4cd9 31173+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31174+/*
b00004a5 31175+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 31176+ *
31177+ * This program, aufs is free software; you can redistribute it and/or modify
31178+ * it under the terms of the GNU General Public License as published by
31179+ * the Free Software Foundation; either version 2 of the License, or
31180+ * (at your option) any later version.
dece6358
AM
31181+ *
31182+ * This program is distributed in the hope that it will be useful,
31183+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31184+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31185+ * GNU General Public License for more details.
31186+ *
31187+ * You should have received a copy of the GNU General Public License
523b37e3 31188+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31189+ */
31190+
31191+/*
31192+ * sysfs interface and lifetime management
31193+ * they are necessary regardless sysfs is disabled.
31194+ */
31195+
1facf9fc 31196+#include <linux/random.h>
1facf9fc 31197+#include "aufs.h"
31198+
31199+unsigned long sysaufs_si_mask;
e49829fe 31200+struct kset *sysaufs_kset;
1facf9fc 31201+
31202+#define AuSiAttr(_name) { \
31203+ .attr = { .name = __stringify(_name), .mode = 0444 }, \
31204+ .show = sysaufs_si_##_name, \
31205+}
31206+
31207+static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
31208+struct attribute *sysaufs_si_attrs[] = {
31209+ &sysaufs_si_attr_xi_path.attr,
31210+ NULL,
31211+};
31212+
4a4d8108 31213+static const struct sysfs_ops au_sbi_ops = {
1facf9fc 31214+ .show = sysaufs_si_show
31215+};
31216+
31217+static struct kobj_type au_sbi_ktype = {
31218+ .release = au_si_free,
31219+ .sysfs_ops = &au_sbi_ops,
31220+ .default_attrs = sysaufs_si_attrs
31221+};
31222+
31223+/* ---------------------------------------------------------------------- */
31224+
31225+int sysaufs_si_init(struct au_sbinfo *sbinfo)
31226+{
31227+ int err;
31228+
e49829fe 31229+ sbinfo->si_kobj.kset = sysaufs_kset;
1facf9fc 31230+ /* cf. sysaufs_name() */
31231+ err = kobject_init_and_add
e49829fe 31232+ (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
1facf9fc 31233+ SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31234+
1facf9fc 31235+ return err;
31236+}
31237+
31238+void sysaufs_fin(void)
31239+{
e49829fe
JR
31240+ sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31241+ kset_unregister(sysaufs_kset);
1facf9fc 31242+}
31243+
31244+int __init sysaufs_init(void)
31245+{
31246+ int err;
31247+
31248+ do {
31249+ get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31250+ } while (!sysaufs_si_mask);
31251+
4a4d8108 31252+ err = -EINVAL;
e49829fe
JR
31253+ sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31254+ if (unlikely(!sysaufs_kset))
4a4d8108 31255+ goto out;
e49829fe
JR
31256+ err = PTR_ERR(sysaufs_kset);
31257+ if (IS_ERR(sysaufs_kset))
1facf9fc 31258+ goto out;
e49829fe 31259+ err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
062440b3 31260+ if (unlikely(err))
e49829fe 31261+ kset_unregister(sysaufs_kset);
1facf9fc 31262+
4f0767ce 31263+out:
1facf9fc 31264+ return err;
31265+}
7f207e10
AM
31266diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
31267--- /usr/share/empty/fs/aufs/sysaufs.h 1970-01-01 01:00:00.000000000 +0100
e49925d1 31268+++ linux/fs/aufs/sysaufs.h 2019-01-28 14:36:12.222418047 +0100
062440b3
AM
31269@@ -0,0 +1,102 @@
31270+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 31271+/*
b00004a5 31272+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 31273+ *
31274+ * This program, aufs is free software; you can redistribute it and/or modify
31275+ * it under the terms of the GNU General Public License as published by
31276+ * the Free Software Foundation; either version 2 of the License, or
31277+ * (at your option) any later version.
dece6358
AM
31278+ *
31279+ * This program is distributed in the hope that it will be useful,
31280+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31281+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31282+ * GNU General Public License for more details.
31283+ *
31284+ * You should have received a copy of the GNU General Public License
523b37e3 31285+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31286+ */
31287+
31288+/*
31289+ * sysfs interface and mount lifetime management
31290+ */
31291+
31292+#ifndef __SYSAUFS_H__
31293+#define __SYSAUFS_H__
31294+
31295+#ifdef __KERNEL__
31296+
1facf9fc 31297+#include <linux/sysfs.h>
1facf9fc 31298+#include "module.h"
31299+
dece6358
AM
31300+struct super_block;
31301+struct au_sbinfo;
31302+
1facf9fc 31303+struct sysaufs_si_attr {
31304+ struct attribute attr;
31305+ int (*show)(struct seq_file *seq, struct super_block *sb);
31306+};
31307+
31308+/* ---------------------------------------------------------------------- */
31309+
31310+/* sysaufs.c */
31311+extern unsigned long sysaufs_si_mask;
e49829fe 31312+extern struct kset *sysaufs_kset;
1facf9fc 31313+extern struct attribute *sysaufs_si_attrs[];
31314+int sysaufs_si_init(struct au_sbinfo *sbinfo);
31315+int __init sysaufs_init(void);
31316+void sysaufs_fin(void);
31317+
31318+/* ---------------------------------------------------------------------- */
31319+
31320+/* some people doesn't like to show a pointer in kernel */
31321+static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31322+{
31323+ return sysaufs_si_mask ^ (unsigned long)sbinfo;
31324+}
31325+
31326+#define SysaufsSiNamePrefix "si_"
31327+#define SysaufsSiNameLen (sizeof(SysaufsSiNamePrefix) + 16)
31328+static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31329+{
31330+ snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31331+ sysaufs_si_id(sbinfo));
31332+}
31333+
31334+struct au_branch;
31335+#ifdef CONFIG_SYSFS
31336+/* sysfs.c */
31337+extern struct attribute_group *sysaufs_attr_group;
31338+
31339+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31340+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31341+ char *buf);
076b876e
AM
31342+long au_brinfo_ioctl(struct file *file, unsigned long arg);
31343+#ifdef CONFIG_COMPAT
31344+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31345+#endif
1facf9fc 31346+
31347+void sysaufs_br_init(struct au_branch *br);
31348+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31349+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31350+
31351+#define sysaufs_brs_init() do {} while (0)
31352+
31353+#else
31354+#define sysaufs_attr_group NULL
31355+
4a4d8108 31356+AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
c1595e42
JR
31357+AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31358+ struct attribute *attr, char *buf)
4a4d8108
AM
31359+AuStubVoid(sysaufs_br_init, struct au_branch *br)
31360+AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31361+AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
1facf9fc 31362+
31363+static inline void sysaufs_brs_init(void)
31364+{
31365+ sysaufs_brs = 0;
31366+}
31367+
31368+#endif /* CONFIG_SYSFS */
31369+
31370+#endif /* __KERNEL__ */
31371+#endif /* __SYSAUFS_H__ */
7f207e10
AM
31372diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31373--- /usr/share/empty/fs/aufs/sysfs.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 31374+++ linux/fs/aufs/sysfs.c 2019-01-28 14:36:12.222418047 +0100
062440b3 31375@@ -0,0 +1,373 @@
cd7a4cd9 31376+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31377+/*
b00004a5 31378+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 31379+ *
31380+ * This program, aufs is free software; you can redistribute it and/or modify
31381+ * it under the terms of the GNU General Public License as published by
31382+ * the Free Software Foundation; either version 2 of the License, or
31383+ * (at your option) any later version.
dece6358
AM
31384+ *
31385+ * This program is distributed in the hope that it will be useful,
31386+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31387+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31388+ * GNU General Public License for more details.
31389+ *
31390+ * You should have received a copy of the GNU General Public License
523b37e3 31391+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31392+ */
31393+
31394+/*
31395+ * sysfs interface
31396+ */
31397+
076b876e 31398+#include <linux/compat.h>
1facf9fc 31399+#include <linux/seq_file.h>
1facf9fc 31400+#include "aufs.h"
31401+
4a4d8108
AM
31402+#ifdef CONFIG_AUFS_FS_MODULE
31403+/* this entry violates the "one line per file" policy of sysfs */
31404+static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31405+ char *buf)
31406+{
31407+ ssize_t err;
31408+ static char *conf =
31409+/* this file is generated at compiling */
31410+#include "conf.str"
31411+ ;
31412+
31413+ err = snprintf(buf, PAGE_SIZE, conf);
31414+ if (unlikely(err >= PAGE_SIZE))
31415+ err = -EFBIG;
31416+ return err;
31417+}
31418+
31419+static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31420+#endif
31421+
1facf9fc 31422+static struct attribute *au_attr[] = {
4a4d8108
AM
31423+#ifdef CONFIG_AUFS_FS_MODULE
31424+ &au_config_attr.attr,
31425+#endif
1facf9fc 31426+ NULL, /* need to NULL terminate the list of attributes */
31427+};
31428+
31429+static struct attribute_group sysaufs_attr_group_body = {
31430+ .attrs = au_attr
31431+};
31432+
31433+struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31434+
31435+/* ---------------------------------------------------------------------- */
31436+
31437+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31438+{
31439+ int err;
31440+
dece6358
AM
31441+ SiMustAnyLock(sb);
31442+
1facf9fc 31443+ err = 0;
31444+ if (au_opt_test(au_mntflags(sb), XINO)) {
31445+ err = au_xino_path(seq, au_sbi(sb)->si_xib);
31446+ seq_putc(seq, '\n');
31447+ }
31448+ return err;
31449+}
31450+
31451+/*
31452+ * the lifetime of branch is independent from the entry under sysfs.
31453+ * sysfs handles the lifetime of the entry, and never call ->show() after it is
31454+ * unlinked.
31455+ */
31456+static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
392086de 31457+ aufs_bindex_t bindex, int idx)
1facf9fc 31458+{
1e00d052 31459+ int err;
1facf9fc 31460+ struct path path;
31461+ struct dentry *root;
31462+ struct au_branch *br;
076b876e 31463+ au_br_perm_str_t perm;
1facf9fc 31464+
31465+ AuDbg("b%d\n", bindex);
31466+
1e00d052 31467+ err = 0;
1facf9fc 31468+ root = sb->s_root;
31469+ di_read_lock_parent(root, !AuLock_IR);
31470+ br = au_sbr(sb, bindex);
392086de
AM
31471+
31472+ switch (idx) {
31473+ case AuBrSysfs_BR:
31474+ path.mnt = au_br_mnt(br);
31475+ path.dentry = au_h_dptr(root, bindex);
79b8bda9
AM
31476+ err = au_seq_path(seq, &path);
31477+ if (!err) {
31478+ au_optstr_br_perm(&perm, br->br_perm);
31479+ seq_printf(seq, "=%s\n", perm.a);
31480+ }
392086de
AM
31481+ break;
31482+ case AuBrSysfs_BRID:
79b8bda9 31483+ seq_printf(seq, "%d\n", br->br_id);
392086de
AM
31484+ break;
31485+ }
076b876e 31486+ di_read_unlock(root, !AuLock_IR);
79b8bda9 31487+ if (unlikely(err || seq_has_overflowed(seq)))
076b876e 31488+ err = -E2BIG;
392086de 31489+
1e00d052 31490+ return err;
1facf9fc 31491+}
31492+
31493+/* ---------------------------------------------------------------------- */
31494+
31495+static struct seq_file *au_seq(char *p, ssize_t len)
31496+{
31497+ struct seq_file *seq;
31498+
31499+ seq = kzalloc(sizeof(*seq), GFP_NOFS);
31500+ if (seq) {
31501+ /* mutex_init(&seq.lock); */
31502+ seq->buf = p;
31503+ seq->size = len;
31504+ return seq; /* success */
31505+ }
31506+
31507+ seq = ERR_PTR(-ENOMEM);
31508+ return seq;
31509+}
31510+
392086de
AM
31511+#define SysaufsBr_PREFIX "br"
31512+#define SysaufsBrid_PREFIX "brid"
1facf9fc 31513+
31514+/* todo: file size may exceed PAGE_SIZE */
31515+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
1308ab2a 31516+ char *buf)
1facf9fc 31517+{
31518+ ssize_t err;
392086de 31519+ int idx;
1facf9fc 31520+ long l;
5afbbe0d 31521+ aufs_bindex_t bbot;
1facf9fc 31522+ struct au_sbinfo *sbinfo;
31523+ struct super_block *sb;
31524+ struct seq_file *seq;
31525+ char *name;
31526+ struct attribute **cattr;
31527+
31528+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31529+ sb = sbinfo->si_sb;
1308ab2a 31530+
31531+ /*
31532+ * prevent a race condition between sysfs and aufs.
31533+ * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31534+ * prohibits maintaining the sysfs entries.
31535+ * hew we acquire read lock after sysfs_get_active_two().
31536+ * on the other hand, the remount process may maintain the sysfs/aufs
31537+ * entries after acquiring write lock.
31538+ * it can cause a deadlock.
31539+ * simply we gave up processing read here.
31540+ */
31541+ err = -EBUSY;
31542+ if (unlikely(!si_noflush_read_trylock(sb)))
31543+ goto out;
1facf9fc 31544+
31545+ seq = au_seq(buf, PAGE_SIZE);
31546+ err = PTR_ERR(seq);
31547+ if (IS_ERR(seq))
1308ab2a 31548+ goto out_unlock;
1facf9fc 31549+
31550+ name = (void *)attr->name;
31551+ cattr = sysaufs_si_attrs;
31552+ while (*cattr) {
31553+ if (!strcmp(name, (*cattr)->name)) {
31554+ err = container_of(*cattr, struct sysaufs_si_attr, attr)
31555+ ->show(seq, sb);
31556+ goto out_seq;
31557+ }
31558+ cattr++;
31559+ }
31560+
392086de
AM
31561+ if (!strncmp(name, SysaufsBrid_PREFIX,
31562+ sizeof(SysaufsBrid_PREFIX) - 1)) {
31563+ idx = AuBrSysfs_BRID;
31564+ name += sizeof(SysaufsBrid_PREFIX) - 1;
31565+ } else if (!strncmp(name, SysaufsBr_PREFIX,
31566+ sizeof(SysaufsBr_PREFIX) - 1)) {
31567+ idx = AuBrSysfs_BR;
1facf9fc 31568+ name += sizeof(SysaufsBr_PREFIX) - 1;
392086de
AM
31569+ } else
31570+ BUG();
31571+
31572+ err = kstrtol(name, 10, &l);
31573+ if (!err) {
5afbbe0d
AM
31574+ bbot = au_sbbot(sb);
31575+ if (l <= bbot)
392086de
AM
31576+ err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31577+ else
31578+ err = -ENOENT;
1facf9fc 31579+ }
1facf9fc 31580+
4f0767ce 31581+out_seq:
1facf9fc 31582+ if (!err) {
31583+ err = seq->count;
31584+ /* sysfs limit */
31585+ if (unlikely(err == PAGE_SIZE))
31586+ err = -EFBIG;
31587+ }
e49925d1 31588+ au_kfree_rcu(seq);
4f0767ce 31589+out_unlock:
1facf9fc 31590+ si_read_unlock(sb);
4f0767ce 31591+out:
1facf9fc 31592+ return err;
31593+}
31594+
31595+/* ---------------------------------------------------------------------- */
31596+
076b876e
AM
31597+static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31598+{
31599+ int err;
31600+ int16_t brid;
5afbbe0d 31601+ aufs_bindex_t bindex, bbot;
076b876e
AM
31602+ size_t sz;
31603+ char *buf;
31604+ struct seq_file *seq;
31605+ struct au_branch *br;
31606+
31607+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d
AM
31608+ bbot = au_sbbot(sb);
31609+ err = bbot + 1;
076b876e
AM
31610+ if (!arg)
31611+ goto out;
31612+
31613+ err = -ENOMEM;
31614+ buf = (void *)__get_free_page(GFP_NOFS);
31615+ if (unlikely(!buf))
31616+ goto out;
31617+
31618+ seq = au_seq(buf, PAGE_SIZE);
31619+ err = PTR_ERR(seq);
31620+ if (IS_ERR(seq))
31621+ goto out_buf;
31622+
31623+ sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
5afbbe0d 31624+ for (bindex = 0; bindex <= bbot; bindex++, arg++) {
076b876e
AM
31625+ err = !access_ok(VERIFY_WRITE, arg, sizeof(*arg));
31626+ if (unlikely(err))
31627+ break;
31628+
31629+ br = au_sbr(sb, bindex);
31630+ brid = br->br_id;
31631+ BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31632+ err = __put_user(brid, &arg->id);
31633+ if (unlikely(err))
31634+ break;
31635+
31636+ BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31637+ err = __put_user(br->br_perm, &arg->perm);
31638+ if (unlikely(err))
31639+ break;
31640+
79b8bda9
AM
31641+ err = au_seq_path(seq, &br->br_path);
31642+ if (unlikely(err))
31643+ break;
31644+ seq_putc(seq, '\0');
31645+ if (!seq_has_overflowed(seq)) {
076b876e
AM
31646+ err = copy_to_user(arg->path, seq->buf, seq->count);
31647+ seq->count = 0;
31648+ if (unlikely(err))
31649+ break;
31650+ } else {
31651+ err = -E2BIG;
31652+ goto out_seq;
31653+ }
31654+ }
31655+ if (unlikely(err))
31656+ err = -EFAULT;
31657+
31658+out_seq:
e49925d1 31659+ au_kfree_rcu(seq);
076b876e 31660+out_buf:
1c60b727 31661+ free_page((unsigned long)buf);
076b876e
AM
31662+out:
31663+ si_read_unlock(sb);
31664+ return err;
31665+}
31666+
31667+long au_brinfo_ioctl(struct file *file, unsigned long arg)
31668+{
2000de60 31669+ return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
076b876e
AM
31670+}
31671+
31672+#ifdef CONFIG_COMPAT
31673+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31674+{
2000de60 31675+ return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
076b876e
AM
31676+}
31677+#endif
31678+
31679+/* ---------------------------------------------------------------------- */
31680+
1facf9fc 31681+void sysaufs_br_init(struct au_branch *br)
31682+{
392086de
AM
31683+ int i;
31684+ struct au_brsysfs *br_sysfs;
31685+ struct attribute *attr;
4a4d8108 31686+
392086de
AM
31687+ br_sysfs = br->br_sysfs;
31688+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31689+ attr = &br_sysfs->attr;
31690+ sysfs_attr_init(attr);
31691+ attr->name = br_sysfs->name;
cd7a4cd9 31692+ attr->mode = 0444;
392086de
AM
31693+ br_sysfs++;
31694+ }
1facf9fc 31695+}
31696+
31697+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31698+{
31699+ struct au_branch *br;
31700+ struct kobject *kobj;
392086de
AM
31701+ struct au_brsysfs *br_sysfs;
31702+ int i;
5afbbe0d 31703+ aufs_bindex_t bbot;
1facf9fc 31704+
1facf9fc 31705+ if (!sysaufs_brs)
31706+ return;
31707+
31708+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
31709+ bbot = au_sbbot(sb);
31710+ for (; bindex <= bbot; bindex++) {
1facf9fc 31711+ br = au_sbr(sb, bindex);
392086de
AM
31712+ br_sysfs = br->br_sysfs;
31713+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31714+ sysfs_remove_file(kobj, &br_sysfs->attr);
31715+ br_sysfs++;
31716+ }
1facf9fc 31717+ }
31718+}
31719+
31720+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31721+{
392086de 31722+ int err, i;
5afbbe0d 31723+ aufs_bindex_t bbot;
1facf9fc 31724+ struct kobject *kobj;
31725+ struct au_branch *br;
392086de 31726+ struct au_brsysfs *br_sysfs;
1facf9fc 31727+
1facf9fc 31728+ if (!sysaufs_brs)
31729+ return;
31730+
31731+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
31732+ bbot = au_sbbot(sb);
31733+ for (; bindex <= bbot; bindex++) {
1facf9fc 31734+ br = au_sbr(sb, bindex);
392086de
AM
31735+ br_sysfs = br->br_sysfs;
31736+ snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31737+ SysaufsBr_PREFIX "%d", bindex);
31738+ snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31739+ SysaufsBrid_PREFIX "%d", bindex);
31740+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31741+ err = sysfs_create_file(kobj, &br_sysfs->attr);
31742+ if (unlikely(err))
31743+ pr_warn("failed %s under sysfs(%d)\n",
31744+ br_sysfs->name, err);
31745+ br_sysfs++;
31746+ }
1facf9fc 31747+ }
31748+}
7f207e10
AM
31749diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31750--- /usr/share/empty/fs/aufs/sysrq.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 31751+++ linux/fs/aufs/sysrq.c 2019-01-28 14:36:12.222418047 +0100
062440b3 31752@@ -0,0 +1,160 @@
cd7a4cd9 31753+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31754+/*
b00004a5 31755+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 31756+ *
31757+ * This program, aufs is free software; you can redistribute it and/or modify
31758+ * it under the terms of the GNU General Public License as published by
31759+ * the Free Software Foundation; either version 2 of the License, or
31760+ * (at your option) any later version.
dece6358
AM
31761+ *
31762+ * This program is distributed in the hope that it will be useful,
31763+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31764+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31765+ * GNU General Public License for more details.
31766+ *
31767+ * You should have received a copy of the GNU General Public License
523b37e3 31768+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31769+ */
31770+
31771+/*
acd2b654 31772+ * magic sysrq handler
1facf9fc 31773+ */
31774+
1facf9fc 31775+/* #include <linux/sysrq.h> */
027c5e7a 31776+#include <linux/writeback.h>
1facf9fc 31777+#include "aufs.h"
31778+
31779+/* ---------------------------------------------------------------------- */
31780+
31781+static void sysrq_sb(struct super_block *sb)
31782+{
31783+ char *plevel;
31784+ struct au_sbinfo *sbinfo;
31785+ struct file *file;
8b6a4947
AM
31786+ struct hlist_bl_head *files;
31787+ struct hlist_bl_node *pos;
523b37e3 31788+ struct au_finfo *finfo;
1facf9fc 31789+
31790+ plevel = au_plevel;
31791+ au_plevel = KERN_WARNING;
1facf9fc 31792+
4a4d8108 31793+ /* since we define pr_fmt, call printk directly */
c06a8ce3
AM
31794+#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31795+
31796+ sbinfo = au_sbi(sb);
4a4d8108 31797+ printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
c06a8ce3 31798+ pr("superblock\n");
1facf9fc 31799+ au_dpri_sb(sb);
027c5e7a
AM
31800+
31801+#if 0
c06a8ce3 31802+ pr("root dentry\n");
1facf9fc 31803+ au_dpri_dentry(sb->s_root);
c06a8ce3 31804+ pr("root inode\n");
5527c038 31805+ au_dpri_inode(d_inode(sb->s_root));
027c5e7a
AM
31806+#endif
31807+
1facf9fc 31808+#if 0
027c5e7a
AM
31809+ do {
31810+ int err, i, j, ndentry;
31811+ struct au_dcsub_pages dpages;
31812+ struct au_dpage *dpage;
31813+
31814+ err = au_dpages_init(&dpages, GFP_ATOMIC);
31815+ if (unlikely(err))
31816+ break;
31817+ err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31818+ if (!err)
31819+ for (i = 0; i < dpages.ndpage; i++) {
31820+ dpage = dpages.dpages + i;
31821+ ndentry = dpage->ndentry;
31822+ for (j = 0; j < ndentry; j++)
31823+ au_dpri_dentry(dpage->dentries[j]);
31824+ }
31825+ au_dpages_free(&dpages);
31826+ } while (0);
31827+#endif
31828+
31829+#if 1
31830+ {
31831+ struct inode *i;
076b876e 31832+
c06a8ce3 31833+ pr("isolated inode\n");
79b8bda9 31834+ spin_lock(&sb->s_inode_list_lock);
2cbb1c4b
JR
31835+ list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31836+ spin_lock(&i->i_lock);
b4510431 31837+ if (1 || hlist_empty(&i->i_dentry))
027c5e7a 31838+ au_dpri_inode(i);
2cbb1c4b
JR
31839+ spin_unlock(&i->i_lock);
31840+ }
79b8bda9 31841+ spin_unlock(&sb->s_inode_list_lock);
027c5e7a 31842+ }
1facf9fc 31843+#endif
c06a8ce3 31844+ pr("files\n");
523b37e3 31845+ files = &au_sbi(sb)->si_files;
8b6a4947
AM
31846+ hlist_bl_lock(files);
31847+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
4a4d8108 31848+ umode_t mode;
076b876e 31849+
523b37e3 31850+ file = finfo->fi_file;
c06a8ce3 31851+ mode = file_inode(file)->i_mode;
38d290e6 31852+ if (!special_file(mode))
1facf9fc 31853+ au_dpri_file(file);
523b37e3 31854+ }
8b6a4947 31855+ hlist_bl_unlock(files);
c06a8ce3 31856+ pr("done\n");
1facf9fc 31857+
c06a8ce3 31858+#undef pr
1facf9fc 31859+ au_plevel = plevel;
1facf9fc 31860+}
31861+
31862+/* ---------------------------------------------------------------------- */
31863+
31864+/* module parameter */
31865+static char *aufs_sysrq_key = "a";
cd7a4cd9 31866+module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
1facf9fc 31867+MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31868+
0c5527e5 31869+static void au_sysrq(int key __maybe_unused)
1facf9fc 31870+{
1facf9fc 31871+ struct au_sbinfo *sbinfo;
8b6a4947 31872+ struct hlist_bl_node *pos;
1facf9fc 31873+
027c5e7a 31874+ lockdep_off();
53392da6 31875+ au_sbilist_lock();
8b6a4947 31876+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
1facf9fc 31877+ sysrq_sb(sbinfo->si_sb);
53392da6 31878+ au_sbilist_unlock();
027c5e7a 31879+ lockdep_on();
1facf9fc 31880+}
31881+
31882+static struct sysrq_key_op au_sysrq_op = {
31883+ .handler = au_sysrq,
31884+ .help_msg = "Aufs",
31885+ .action_msg = "Aufs",
31886+ .enable_mask = SYSRQ_ENABLE_DUMP
31887+};
31888+
31889+/* ---------------------------------------------------------------------- */
31890+
31891+int __init au_sysrq_init(void)
31892+{
31893+ int err;
31894+ char key;
31895+
31896+ err = -1;
31897+ key = *aufs_sysrq_key;
31898+ if ('a' <= key && key <= 'z')
31899+ err = register_sysrq_key(key, &au_sysrq_op);
31900+ if (unlikely(err))
4a4d8108 31901+ pr_err("err %d, sysrq=%c\n", err, key);
1facf9fc 31902+ return err;
31903+}
31904+
31905+void au_sysrq_fin(void)
31906+{
31907+ int err;
076b876e 31908+
1facf9fc 31909+ err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31910+ if (unlikely(err))
4a4d8108 31911+ pr_err("err %d (ignored)\n", err);
1facf9fc 31912+}
7f207e10
AM
31913diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
31914--- /usr/share/empty/fs/aufs/vdir.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 31915+++ linux/fs/aufs/vdir.c 2019-01-28 14:36:12.222418047 +0100
acd2b654 31916@@ -0,0 +1,895 @@
cd7a4cd9 31917+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31918+/*
b00004a5 31919+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 31920+ *
31921+ * This program, aufs is free software; you can redistribute it and/or modify
31922+ * it under the terms of the GNU General Public License as published by
31923+ * the Free Software Foundation; either version 2 of the License, or
31924+ * (at your option) any later version.
dece6358
AM
31925+ *
31926+ * This program is distributed in the hope that it will be useful,
31927+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31928+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31929+ * GNU General Public License for more details.
31930+ *
31931+ * You should have received a copy of the GNU General Public License
523b37e3 31932+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31933+ */
31934+
31935+/*
31936+ * virtual or vertical directory
31937+ */
31938+
31939+#include "aufs.h"
31940+
dece6358 31941+static unsigned int calc_size(int nlen)
1facf9fc 31942+{
dece6358 31943+ return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
1facf9fc 31944+}
31945+
31946+static int set_deblk_end(union au_vdir_deblk_p *p,
31947+ union au_vdir_deblk_p *deblk_end)
31948+{
31949+ if (calc_size(0) <= deblk_end->deblk - p->deblk) {
31950+ p->de->de_str.len = 0;
31951+ /* smp_mb(); */
31952+ return 0;
31953+ }
31954+ return -1; /* error */
31955+}
31956+
31957+/* returns true or false */
31958+static int is_deblk_end(union au_vdir_deblk_p *p,
31959+ union au_vdir_deblk_p *deblk_end)
31960+{
31961+ if (calc_size(0) <= deblk_end->deblk - p->deblk)
31962+ return !p->de->de_str.len;
31963+ return 1;
31964+}
31965+
31966+static unsigned char *last_deblk(struct au_vdir *vdir)
31967+{
31968+ return vdir->vd_deblk[vdir->vd_nblk - 1];
31969+}
31970+
31971+/* ---------------------------------------------------------------------- */
31972+
79b8bda9 31973+/* estimate the appropriate size for name hash table */
1308ab2a 31974+unsigned int au_rdhash_est(loff_t sz)
31975+{
31976+ unsigned int n;
31977+
31978+ n = UINT_MAX;
31979+ sz >>= 10;
31980+ if (sz < n)
31981+ n = sz;
31982+ if (sz < AUFS_RDHASH_DEF)
31983+ n = AUFS_RDHASH_DEF;
4a4d8108 31984+ /* pr_info("n %u\n", n); */
1308ab2a 31985+ return n;
31986+}
31987+
1facf9fc 31988+/*
31989+ * the allocated memory has to be freed by
dece6358 31990+ * au_nhash_wh_free() or au_nhash_de_free().
1facf9fc 31991+ */
dece6358 31992+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
1facf9fc 31993+{
1facf9fc 31994+ struct hlist_head *head;
dece6358 31995+ unsigned int u;
076b876e 31996+ size_t sz;
1facf9fc 31997+
076b876e
AM
31998+ sz = sizeof(*nhash->nh_head) * num_hash;
31999+ head = kmalloc(sz, gfp);
dece6358
AM
32000+ if (head) {
32001+ nhash->nh_num = num_hash;
32002+ nhash->nh_head = head;
32003+ for (u = 0; u < num_hash; u++)
1facf9fc 32004+ INIT_HLIST_HEAD(head++);
dece6358 32005+ return 0; /* success */
1facf9fc 32006+ }
1facf9fc 32007+
dece6358 32008+ return -ENOMEM;
1facf9fc 32009+}
32010+
dece6358
AM
32011+static void nhash_count(struct hlist_head *head)
32012+{
32013+#if 0
32014+ unsigned long n;
32015+ struct hlist_node *pos;
32016+
32017+ n = 0;
32018+ hlist_for_each(pos, head)
32019+ n++;
4a4d8108 32020+ pr_info("%lu\n", n);
dece6358
AM
32021+#endif
32022+}
32023+
32024+static void au_nhash_wh_do_free(struct hlist_head *head)
1facf9fc 32025+{
c06a8ce3
AM
32026+ struct au_vdir_wh *pos;
32027+ struct hlist_node *node;
1facf9fc 32028+
c06a8ce3 32029+ hlist_for_each_entry_safe(pos, node, head, wh_hash)
e49925d1 32030+ au_kfree_rcu(pos);
1facf9fc 32031+}
32032+
dece6358 32033+static void au_nhash_de_do_free(struct hlist_head *head)
1facf9fc 32034+{
c06a8ce3
AM
32035+ struct au_vdir_dehstr *pos;
32036+ struct hlist_node *node;
1facf9fc 32037+
c06a8ce3 32038+ hlist_for_each_entry_safe(pos, node, head, hash)
1c60b727 32039+ au_cache_free_vdir_dehstr(pos);
1facf9fc 32040+}
32041+
dece6358
AM
32042+static void au_nhash_do_free(struct au_nhash *nhash,
32043+ void (*free)(struct hlist_head *head))
1facf9fc 32044+{
1308ab2a 32045+ unsigned int n;
1facf9fc 32046+ struct hlist_head *head;
1facf9fc 32047+
dece6358 32048+ n = nhash->nh_num;
1308ab2a 32049+ if (!n)
32050+ return;
32051+
dece6358 32052+ head = nhash->nh_head;
1308ab2a 32053+ while (n-- > 0) {
dece6358
AM
32054+ nhash_count(head);
32055+ free(head++);
1facf9fc 32056+ }
e49925d1 32057+ au_kfree_try_rcu(nhash->nh_head);
1facf9fc 32058+}
32059+
dece6358 32060+void au_nhash_wh_free(struct au_nhash *whlist)
1facf9fc 32061+{
dece6358
AM
32062+ au_nhash_do_free(whlist, au_nhash_wh_do_free);
32063+}
1facf9fc 32064+
dece6358
AM
32065+static void au_nhash_de_free(struct au_nhash *delist)
32066+{
32067+ au_nhash_do_free(delist, au_nhash_de_do_free);
1facf9fc 32068+}
32069+
32070+/* ---------------------------------------------------------------------- */
32071+
32072+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
32073+ int limit)
32074+{
32075+ int num;
32076+ unsigned int u, n;
32077+ struct hlist_head *head;
c06a8ce3 32078+ struct au_vdir_wh *pos;
1facf9fc 32079+
32080+ num = 0;
32081+ n = whlist->nh_num;
32082+ head = whlist->nh_head;
1308ab2a 32083+ for (u = 0; u < n; u++, head++)
c06a8ce3
AM
32084+ hlist_for_each_entry(pos, head, wh_hash)
32085+ if (pos->wh_bindex == btgt && ++num > limit)
1facf9fc 32086+ return 1;
1facf9fc 32087+ return 0;
32088+}
32089+
32090+static struct hlist_head *au_name_hash(struct au_nhash *nhash,
dece6358 32091+ unsigned char *name,
1facf9fc 32092+ unsigned int len)
32093+{
dece6358
AM
32094+ unsigned int v;
32095+ /* const unsigned int magic_bit = 12; */
32096+
1308ab2a 32097+ AuDebugOn(!nhash->nh_num || !nhash->nh_head);
32098+
dece6358 32099+ v = 0;
f0c0a007
AM
32100+ if (len > 8)
32101+ len = 8;
dece6358
AM
32102+ while (len--)
32103+ v += *name++;
32104+ /* v = hash_long(v, magic_bit); */
32105+ v %= nhash->nh_num;
32106+ return nhash->nh_head + v;
32107+}
32108+
32109+static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
32110+ int nlen)
32111+{
32112+ return str->len == nlen && !memcmp(str->name, name, nlen);
1facf9fc 32113+}
32114+
32115+/* returns found or not */
dece6358 32116+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
1facf9fc 32117+{
32118+ struct hlist_head *head;
c06a8ce3 32119+ struct au_vdir_wh *pos;
1facf9fc 32120+ struct au_vdir_destr *str;
32121+
dece6358 32122+ head = au_name_hash(whlist, name, nlen);
c06a8ce3
AM
32123+ hlist_for_each_entry(pos, head, wh_hash) {
32124+ str = &pos->wh_str;
1facf9fc 32125+ AuDbg("%.*s\n", str->len, str->name);
dece6358
AM
32126+ if (au_nhash_test_name(str, name, nlen))
32127+ return 1;
32128+ }
32129+ return 0;
32130+}
32131+
32132+/* returns found(true) or not */
32133+static int test_known(struct au_nhash *delist, char *name, int nlen)
32134+{
32135+ struct hlist_head *head;
c06a8ce3 32136+ struct au_vdir_dehstr *pos;
dece6358
AM
32137+ struct au_vdir_destr *str;
32138+
32139+ head = au_name_hash(delist, name, nlen);
c06a8ce3
AM
32140+ hlist_for_each_entry(pos, head, hash) {
32141+ str = pos->str;
dece6358
AM
32142+ AuDbg("%.*s\n", str->len, str->name);
32143+ if (au_nhash_test_name(str, name, nlen))
1facf9fc 32144+ return 1;
32145+ }
32146+ return 0;
32147+}
32148+
dece6358
AM
32149+static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
32150+ unsigned char d_type)
32151+{
32152+#ifdef CONFIG_AUFS_SHWH
32153+ wh->wh_ino = ino;
32154+ wh->wh_type = d_type;
32155+#endif
32156+}
32157+
32158+/* ---------------------------------------------------------------------- */
32159+
32160+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
32161+ unsigned int d_type, aufs_bindex_t bindex,
32162+ unsigned char shwh)
1facf9fc 32163+{
32164+ int err;
32165+ struct au_vdir_destr *str;
32166+ struct au_vdir_wh *wh;
32167+
dece6358 32168+ AuDbg("%.*s\n", nlen, name);
1308ab2a 32169+ AuDebugOn(!whlist->nh_num || !whlist->nh_head);
32170+
1facf9fc 32171+ err = -ENOMEM;
dece6358 32172+ wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
1facf9fc 32173+ if (unlikely(!wh))
32174+ goto out;
32175+
32176+ err = 0;
32177+ wh->wh_bindex = bindex;
dece6358
AM
32178+ if (shwh)
32179+ au_shwh_init_wh(wh, ino, d_type);
1facf9fc 32180+ str = &wh->wh_str;
dece6358
AM
32181+ str->len = nlen;
32182+ memcpy(str->name, name, nlen);
32183+ hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
1facf9fc 32184+ /* smp_mb(); */
32185+
4f0767ce 32186+out:
1facf9fc 32187+ return err;
32188+}
32189+
1facf9fc 32190+static int append_deblk(struct au_vdir *vdir)
32191+{
32192+ int err;
dece6358 32193+ unsigned long ul;
1facf9fc 32194+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
32195+ union au_vdir_deblk_p p, deblk_end;
32196+ unsigned char **o;
32197+
32198+ err = -ENOMEM;
e2f27e51
AM
32199+ o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32200+ GFP_NOFS, /*may_shrink*/0);
1facf9fc 32201+ if (unlikely(!o))
32202+ goto out;
32203+
32204+ vdir->vd_deblk = o;
32205+ p.deblk = kmalloc(deblk_sz, GFP_NOFS);
32206+ if (p.deblk) {
32207+ ul = vdir->vd_nblk++;
32208+ vdir->vd_deblk[ul] = p.deblk;
32209+ vdir->vd_last.ul = ul;
32210+ vdir->vd_last.p.deblk = p.deblk;
32211+ deblk_end.deblk = p.deblk + deblk_sz;
32212+ err = set_deblk_end(&p, &deblk_end);
32213+ }
32214+
4f0767ce 32215+out:
1facf9fc 32216+ return err;
32217+}
32218+
dece6358
AM
32219+static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
32220+ unsigned int d_type, struct au_nhash *delist)
32221+{
32222+ int err;
32223+ unsigned int sz;
32224+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
32225+ union au_vdir_deblk_p p, *room, deblk_end;
32226+ struct au_vdir_dehstr *dehstr;
32227+
32228+ p.deblk = last_deblk(vdir);
32229+ deblk_end.deblk = p.deblk + deblk_sz;
32230+ room = &vdir->vd_last.p;
32231+ AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32232+ || !is_deblk_end(room, &deblk_end));
32233+
32234+ sz = calc_size(nlen);
32235+ if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32236+ err = append_deblk(vdir);
32237+ if (unlikely(err))
32238+ goto out;
32239+
32240+ p.deblk = last_deblk(vdir);
32241+ deblk_end.deblk = p.deblk + deblk_sz;
32242+ /* smp_mb(); */
32243+ AuDebugOn(room->deblk != p.deblk);
32244+ }
32245+
32246+ err = -ENOMEM;
4a4d8108 32247+ dehstr = au_cache_alloc_vdir_dehstr();
dece6358
AM
32248+ if (unlikely(!dehstr))
32249+ goto out;
32250+
32251+ dehstr->str = &room->de->de_str;
32252+ hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32253+ room->de->de_ino = ino;
32254+ room->de->de_type = d_type;
32255+ room->de->de_str.len = nlen;
32256+ memcpy(room->de->de_str.name, name, nlen);
32257+
32258+ err = 0;
32259+ room->deblk += sz;
32260+ if (unlikely(set_deblk_end(room, &deblk_end)))
32261+ err = append_deblk(vdir);
32262+ /* smp_mb(); */
32263+
4f0767ce 32264+out:
dece6358
AM
32265+ return err;
32266+}
32267+
32268+/* ---------------------------------------------------------------------- */
32269+
1c60b727 32270+void au_vdir_free(struct au_vdir *vdir)
dece6358
AM
32271+{
32272+ unsigned char **deblk;
32273+
32274+ deblk = vdir->vd_deblk;
1c60b727 32275+ while (vdir->vd_nblk--)
e49925d1
JR
32276+ au_kfree_try_rcu(*deblk++);
32277+ au_kfree_try_rcu(vdir->vd_deblk);
1c60b727 32278+ au_cache_free_vdir(vdir);
dece6358
AM
32279+}
32280+
1308ab2a 32281+static struct au_vdir *alloc_vdir(struct file *file)
1facf9fc 32282+{
32283+ struct au_vdir *vdir;
1308ab2a 32284+ struct super_block *sb;
1facf9fc 32285+ int err;
32286+
2000de60 32287+ sb = file->f_path.dentry->d_sb;
dece6358
AM
32288+ SiMustAnyLock(sb);
32289+
1facf9fc 32290+ err = -ENOMEM;
32291+ vdir = au_cache_alloc_vdir();
32292+ if (unlikely(!vdir))
32293+ goto out;
32294+
32295+ vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32296+ if (unlikely(!vdir->vd_deblk))
32297+ goto out_free;
32298+
32299+ vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
1308ab2a 32300+ if (!vdir->vd_deblk_sz) {
79b8bda9 32301+ /* estimate the appropriate size for deblk */
1308ab2a 32302+ vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
4a4d8108 32303+ /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
1308ab2a 32304+ }
1facf9fc 32305+ vdir->vd_nblk = 0;
32306+ vdir->vd_version = 0;
32307+ vdir->vd_jiffy = 0;
32308+ err = append_deblk(vdir);
32309+ if (!err)
32310+ return vdir; /* success */
32311+
e49925d1 32312+ au_kfree_try_rcu(vdir->vd_deblk);
1facf9fc 32313+
4f0767ce 32314+out_free:
1c60b727 32315+ au_cache_free_vdir(vdir);
4f0767ce 32316+out:
1facf9fc 32317+ vdir = ERR_PTR(err);
32318+ return vdir;
32319+}
32320+
32321+static int reinit_vdir(struct au_vdir *vdir)
32322+{
32323+ int err;
32324+ union au_vdir_deblk_p p, deblk_end;
32325+
32326+ while (vdir->vd_nblk > 1) {
e49925d1 32327+ au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
1facf9fc 32328+ /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32329+ vdir->vd_nblk--;
32330+ }
32331+ p.deblk = vdir->vd_deblk[0];
32332+ deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32333+ err = set_deblk_end(&p, &deblk_end);
32334+ /* keep vd_dblk_sz */
32335+ vdir->vd_last.ul = 0;
32336+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32337+ vdir->vd_version = 0;
32338+ vdir->vd_jiffy = 0;
32339+ /* smp_mb(); */
32340+ return err;
32341+}
32342+
32343+/* ---------------------------------------------------------------------- */
32344+
1facf9fc 32345+#define AuFillVdir_CALLED 1
32346+#define AuFillVdir_WHABLE (1 << 1)
dece6358 32347+#define AuFillVdir_SHWH (1 << 2)
1facf9fc 32348+#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
7f207e10
AM
32349+#define au_fset_fillvdir(flags, name) \
32350+ do { (flags) |= AuFillVdir_##name; } while (0)
32351+#define au_fclr_fillvdir(flags, name) \
32352+ do { (flags) &= ~AuFillVdir_##name; } while (0)
1facf9fc 32353+
dece6358
AM
32354+#ifndef CONFIG_AUFS_SHWH
32355+#undef AuFillVdir_SHWH
32356+#define AuFillVdir_SHWH 0
32357+#endif
32358+
1facf9fc 32359+struct fillvdir_arg {
392086de 32360+ struct dir_context ctx;
1facf9fc 32361+ struct file *file;
32362+ struct au_vdir *vdir;
dece6358
AM
32363+ struct au_nhash delist;
32364+ struct au_nhash whlist;
1facf9fc 32365+ aufs_bindex_t bindex;
32366+ unsigned int flags;
32367+ int err;
32368+};
32369+
392086de 32370+static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
1facf9fc 32371+ loff_t offset __maybe_unused, u64 h_ino,
32372+ unsigned int d_type)
32373+{
392086de 32374+ struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
1facf9fc 32375+ char *name = (void *)__name;
32376+ struct super_block *sb;
1facf9fc 32377+ ino_t ino;
dece6358 32378+ const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
1facf9fc 32379+
1facf9fc 32380+ arg->err = 0;
2000de60 32381+ sb = arg->file->f_path.dentry->d_sb;
1facf9fc 32382+ au_fset_fillvdir(arg->flags, CALLED);
32383+ /* smp_mb(); */
dece6358 32384+ if (nlen <= AUFS_WH_PFX_LEN
1facf9fc 32385+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
dece6358
AM
32386+ if (test_known(&arg->delist, name, nlen)
32387+ || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32388+ goto out; /* already exists or whiteouted */
1facf9fc 32389+
dece6358 32390+ arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
4a4d8108
AM
32391+ if (!arg->err) {
32392+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
32393+ d_type = DT_UNKNOWN;
dece6358
AM
32394+ arg->err = append_de(arg->vdir, name, nlen, ino,
32395+ d_type, &arg->delist);
4a4d8108 32396+ }
1facf9fc 32397+ } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32398+ name += AUFS_WH_PFX_LEN;
dece6358
AM
32399+ nlen -= AUFS_WH_PFX_LEN;
32400+ if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32401+ goto out; /* already whiteouted */
1facf9fc 32402+
acd2b654 32403+ ino = 0; /* just to suppress a warning */
dece6358
AM
32404+ if (shwh)
32405+ arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32406+ &ino);
4a4d8108
AM
32407+ if (!arg->err) {
32408+ if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32409+ d_type = DT_UNKNOWN;
1facf9fc 32410+ arg->err = au_nhash_append_wh
dece6358
AM
32411+ (&arg->whlist, name, nlen, ino, d_type,
32412+ arg->bindex, shwh);
4a4d8108 32413+ }
1facf9fc 32414+ }
32415+
4f0767ce 32416+out:
1facf9fc 32417+ if (!arg->err)
32418+ arg->vdir->vd_jiffy = jiffies;
32419+ /* smp_mb(); */
32420+ AuTraceErr(arg->err);
32421+ return arg->err;
32422+}
32423+
dece6358
AM
32424+static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32425+ struct au_nhash *whlist, struct au_nhash *delist)
32426+{
32427+#ifdef CONFIG_AUFS_SHWH
32428+ int err;
32429+ unsigned int nh, u;
32430+ struct hlist_head *head;
c06a8ce3
AM
32431+ struct au_vdir_wh *pos;
32432+ struct hlist_node *n;
dece6358
AM
32433+ char *p, *o;
32434+ struct au_vdir_destr *destr;
32435+
32436+ AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32437+
32438+ err = -ENOMEM;
537831f9 32439+ o = p = (void *)__get_free_page(GFP_NOFS);
dece6358
AM
32440+ if (unlikely(!p))
32441+ goto out;
32442+
32443+ err = 0;
32444+ nh = whlist->nh_num;
32445+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32446+ p += AUFS_WH_PFX_LEN;
32447+ for (u = 0; u < nh; u++) {
32448+ head = whlist->nh_head + u;
c06a8ce3
AM
32449+ hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32450+ destr = &pos->wh_str;
dece6358
AM
32451+ memcpy(p, destr->name, destr->len);
32452+ err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
c06a8ce3 32453+ pos->wh_ino, pos->wh_type, delist);
dece6358
AM
32454+ if (unlikely(err))
32455+ break;
32456+ }
32457+ }
32458+
1c60b727 32459+ free_page((unsigned long)o);
dece6358 32460+
4f0767ce 32461+out:
dece6358
AM
32462+ AuTraceErr(err);
32463+ return err;
32464+#else
32465+ return 0;
32466+#endif
32467+}
32468+
1facf9fc 32469+static int au_do_read_vdir(struct fillvdir_arg *arg)
32470+{
32471+ int err;
dece6358 32472+ unsigned int rdhash;
1facf9fc 32473+ loff_t offset;
5afbbe0d 32474+ aufs_bindex_t bbot, bindex, btop;
dece6358 32475+ unsigned char shwh;
1facf9fc 32476+ struct file *hf, *file;
32477+ struct super_block *sb;
32478+
1facf9fc 32479+ file = arg->file;
2000de60 32480+ sb = file->f_path.dentry->d_sb;
dece6358
AM
32481+ SiMustAnyLock(sb);
32482+
32483+ rdhash = au_sbi(sb)->si_rdhash;
1308ab2a 32484+ if (!rdhash)
32485+ rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
dece6358
AM
32486+ err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32487+ if (unlikely(err))
1facf9fc 32488+ goto out;
dece6358
AM
32489+ err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32490+ if (unlikely(err))
1facf9fc 32491+ goto out_delist;
32492+
32493+ err = 0;
32494+ arg->flags = 0;
dece6358
AM
32495+ shwh = 0;
32496+ if (au_opt_test(au_mntflags(sb), SHWH)) {
32497+ shwh = 1;
32498+ au_fset_fillvdir(arg->flags, SHWH);
32499+ }
5afbbe0d
AM
32500+ btop = au_fbtop(file);
32501+ bbot = au_fbbot_dir(file);
32502+ for (bindex = btop; !err && bindex <= bbot; bindex++) {
4a4d8108 32503+ hf = au_hf_dir(file, bindex);
1facf9fc 32504+ if (!hf)
32505+ continue;
32506+
32507+ offset = vfsub_llseek(hf, 0, SEEK_SET);
32508+ err = offset;
32509+ if (unlikely(offset))
32510+ break;
32511+
32512+ arg->bindex = bindex;
32513+ au_fclr_fillvdir(arg->flags, WHABLE);
dece6358 32514+ if (shwh
5afbbe0d 32515+ || (bindex != bbot
dece6358 32516+ && au_br_whable(au_sbr_perm(sb, bindex))))
1facf9fc 32517+ au_fset_fillvdir(arg->flags, WHABLE);
32518+ do {
32519+ arg->err = 0;
32520+ au_fclr_fillvdir(arg->flags, CALLED);
32521+ /* smp_mb(); */
392086de 32522+ err = vfsub_iterate_dir(hf, &arg->ctx);
1facf9fc 32523+ if (err >= 0)
32524+ err = arg->err;
32525+ } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
392086de
AM
32526+
32527+ /*
32528+ * dir_relax() may be good for concurrency, but aufs should not
32529+ * use it since it will cause a lockdep problem.
32530+ */
1facf9fc 32531+ }
dece6358
AM
32532+
32533+ if (!err && shwh)
32534+ err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32535+
32536+ au_nhash_wh_free(&arg->whlist);
1facf9fc 32537+
4f0767ce 32538+out_delist:
dece6358 32539+ au_nhash_de_free(&arg->delist);
4f0767ce 32540+out:
1facf9fc 32541+ return err;
32542+}
32543+
32544+static int read_vdir(struct file *file, int may_read)
32545+{
32546+ int err;
32547+ unsigned long expire;
32548+ unsigned char do_read;
392086de
AM
32549+ struct fillvdir_arg arg = {
32550+ .ctx = {
2000de60 32551+ .actor = fillvdir
392086de
AM
32552+ }
32553+ };
1facf9fc 32554+ struct inode *inode;
32555+ struct au_vdir *vdir, *allocated;
32556+
32557+ err = 0;
c06a8ce3 32558+ inode = file_inode(file);
1facf9fc 32559+ IMustLock(inode);
5afbbe0d 32560+ IiMustWriteLock(inode);
dece6358
AM
32561+ SiMustAnyLock(inode->i_sb);
32562+
1facf9fc 32563+ allocated = NULL;
32564+ do_read = 0;
32565+ expire = au_sbi(inode->i_sb)->si_rdcache;
32566+ vdir = au_ivdir(inode);
32567+ if (!vdir) {
32568+ do_read = 1;
1308ab2a 32569+ vdir = alloc_vdir(file);
1facf9fc 32570+ err = PTR_ERR(vdir);
32571+ if (IS_ERR(vdir))
32572+ goto out;
32573+ err = 0;
32574+ allocated = vdir;
32575+ } else if (may_read
be118d29 32576+ && (!inode_eq_iversion(inode, vdir->vd_version)
1facf9fc 32577+ || time_after(jiffies, vdir->vd_jiffy + expire))) {
32578+ do_read = 1;
32579+ err = reinit_vdir(vdir);
32580+ if (unlikely(err))
32581+ goto out;
32582+ }
32583+
32584+ if (!do_read)
32585+ return 0; /* success */
32586+
32587+ arg.file = file;
32588+ arg.vdir = vdir;
32589+ err = au_do_read_vdir(&arg);
32590+ if (!err) {
392086de 32591+ /* file->f_pos = 0; */ /* todo: ctx->pos? */
be118d29 32592+ vdir->vd_version = inode_query_iversion(inode);
1facf9fc 32593+ vdir->vd_last.ul = 0;
32594+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32595+ if (allocated)
32596+ au_set_ivdir(inode, allocated);
32597+ } else if (allocated)
1c60b727 32598+ au_vdir_free(allocated);
1facf9fc 32599+
4f0767ce 32600+out:
1facf9fc 32601+ return err;
32602+}
32603+
32604+static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32605+{
32606+ int err, rerr;
32607+ unsigned long ul, n;
32608+ const unsigned int deblk_sz = src->vd_deblk_sz;
32609+
32610+ AuDebugOn(tgt->vd_nblk != 1);
32611+
32612+ err = -ENOMEM;
32613+ if (tgt->vd_nblk < src->vd_nblk) {
32614+ unsigned char **p;
32615+
e2f27e51
AM
32616+ p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32617+ GFP_NOFS, /*may_shrink*/0);
1facf9fc 32618+ if (unlikely(!p))
32619+ goto out;
32620+ tgt->vd_deblk = p;
32621+ }
32622+
1308ab2a 32623+ if (tgt->vd_deblk_sz != deblk_sz) {
32624+ unsigned char *p;
32625+
32626+ tgt->vd_deblk_sz = deblk_sz;
e2f27e51
AM
32627+ p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32628+ /*may_shrink*/1);
1308ab2a 32629+ if (unlikely(!p))
32630+ goto out;
32631+ tgt->vd_deblk[0] = p;
32632+ }
1facf9fc 32633+ memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
1facf9fc 32634+ tgt->vd_version = src->vd_version;
32635+ tgt->vd_jiffy = src->vd_jiffy;
32636+
32637+ n = src->vd_nblk;
32638+ for (ul = 1; ul < n; ul++) {
dece6358
AM
32639+ tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32640+ GFP_NOFS);
32641+ if (unlikely(!tgt->vd_deblk[ul]))
1facf9fc 32642+ goto out;
1308ab2a 32643+ tgt->vd_nblk++;
1facf9fc 32644+ }
1308ab2a 32645+ tgt->vd_nblk = n;
32646+ tgt->vd_last.ul = tgt->vd_last.ul;
32647+ tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32648+ tgt->vd_last.p.deblk += src->vd_last.p.deblk
32649+ - src->vd_deblk[src->vd_last.ul];
1facf9fc 32650+ /* smp_mb(); */
32651+ return 0; /* success */
32652+
4f0767ce 32653+out:
1facf9fc 32654+ rerr = reinit_vdir(tgt);
32655+ BUG_ON(rerr);
32656+ return err;
32657+}
32658+
32659+int au_vdir_init(struct file *file)
32660+{
32661+ int err;
32662+ struct inode *inode;
32663+ struct au_vdir *vdir_cache, *allocated;
32664+
392086de 32665+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 32666+ err = read_vdir(file, !file->f_pos);
32667+ if (unlikely(err))
32668+ goto out;
32669+
32670+ allocated = NULL;
32671+ vdir_cache = au_fvdir_cache(file);
32672+ if (!vdir_cache) {
1308ab2a 32673+ vdir_cache = alloc_vdir(file);
1facf9fc 32674+ err = PTR_ERR(vdir_cache);
32675+ if (IS_ERR(vdir_cache))
32676+ goto out;
32677+ allocated = vdir_cache;
32678+ } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
392086de 32679+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 32680+ err = reinit_vdir(vdir_cache);
32681+ if (unlikely(err))
32682+ goto out;
32683+ } else
32684+ return 0; /* success */
32685+
c06a8ce3 32686+ inode = file_inode(file);
1facf9fc 32687+ err = copy_vdir(vdir_cache, au_ivdir(inode));
32688+ if (!err) {
be118d29 32689+ file->f_version = inode_query_iversion(inode);
1facf9fc 32690+ if (allocated)
32691+ au_set_fvdir_cache(file, allocated);
32692+ } else if (allocated)
1c60b727 32693+ au_vdir_free(allocated);
1facf9fc 32694+
4f0767ce 32695+out:
1facf9fc 32696+ return err;
32697+}
32698+
32699+static loff_t calc_offset(struct au_vdir *vdir)
32700+{
32701+ loff_t offset;
32702+ union au_vdir_deblk_p p;
32703+
32704+ p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32705+ offset = vdir->vd_last.p.deblk - p.deblk;
32706+ offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32707+ return offset;
32708+}
32709+
32710+/* returns true or false */
392086de 32711+static int seek_vdir(struct file *file, struct dir_context *ctx)
1facf9fc 32712+{
32713+ int valid;
32714+ unsigned int deblk_sz;
32715+ unsigned long ul, n;
32716+ loff_t offset;
32717+ union au_vdir_deblk_p p, deblk_end;
32718+ struct au_vdir *vdir_cache;
32719+
32720+ valid = 1;
32721+ vdir_cache = au_fvdir_cache(file);
32722+ offset = calc_offset(vdir_cache);
32723+ AuDbg("offset %lld\n", offset);
392086de 32724+ if (ctx->pos == offset)
1facf9fc 32725+ goto out;
32726+
32727+ vdir_cache->vd_last.ul = 0;
32728+ vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
392086de 32729+ if (!ctx->pos)
1facf9fc 32730+ goto out;
32731+
32732+ valid = 0;
32733+ deblk_sz = vdir_cache->vd_deblk_sz;
392086de 32734+ ul = div64_u64(ctx->pos, deblk_sz);
1facf9fc 32735+ AuDbg("ul %lu\n", ul);
32736+ if (ul >= vdir_cache->vd_nblk)
32737+ goto out;
32738+
32739+ n = vdir_cache->vd_nblk;
32740+ for (; ul < n; ul++) {
32741+ p.deblk = vdir_cache->vd_deblk[ul];
32742+ deblk_end.deblk = p.deblk + deblk_sz;
32743+ offset = ul;
32744+ offset *= deblk_sz;
392086de 32745+ while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
1facf9fc 32746+ unsigned int l;
32747+
32748+ l = calc_size(p.de->de_str.len);
32749+ offset += l;
32750+ p.deblk += l;
32751+ }
32752+ if (!is_deblk_end(&p, &deblk_end)) {
32753+ valid = 1;
32754+ vdir_cache->vd_last.ul = ul;
32755+ vdir_cache->vd_last.p = p;
32756+ break;
32757+ }
32758+ }
32759+
4f0767ce 32760+out:
1facf9fc 32761+ /* smp_mb(); */
b00004a5
AM
32762+ if (!valid)
32763+ AuDbg("valid %d\n", !valid);
1facf9fc 32764+ return valid;
32765+}
32766+
392086de 32767+int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
1facf9fc 32768+{
1facf9fc 32769+ unsigned int l, deblk_sz;
32770+ union au_vdir_deblk_p deblk_end;
32771+ struct au_vdir *vdir_cache;
32772+ struct au_vdir_de *de;
32773+
392086de 32774+ if (!seek_vdir(file, ctx))
1facf9fc 32775+ return 0;
32776+
acd2b654 32777+ vdir_cache = au_fvdir_cache(file);
1facf9fc 32778+ deblk_sz = vdir_cache->vd_deblk_sz;
32779+ while (1) {
32780+ deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32781+ deblk_end.deblk += deblk_sz;
32782+ while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32783+ de = vdir_cache->vd_last.p.de;
32784+ AuDbg("%.*s, off%lld, i%lu, dt%d\n",
392086de 32785+ de->de_str.len, de->de_str.name, ctx->pos,
1facf9fc 32786+ (unsigned long)de->de_ino, de->de_type);
392086de
AM
32787+ if (unlikely(!dir_emit(ctx, de->de_str.name,
32788+ de->de_str.len, de->de_ino,
32789+ de->de_type))) {
1facf9fc 32790+ /* todo: ignore the error caused by udba? */
32791+ /* return err; */
32792+ return 0;
32793+ }
32794+
32795+ l = calc_size(de->de_str.len);
32796+ vdir_cache->vd_last.p.deblk += l;
392086de 32797+ ctx->pos += l;
1facf9fc 32798+ }
32799+ if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32800+ vdir_cache->vd_last.ul++;
32801+ vdir_cache->vd_last.p.deblk
32802+ = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
392086de 32803+ ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
1facf9fc 32804+ continue;
32805+ }
32806+ break;
32807+ }
32808+
32809+ /* smp_mb(); */
32810+ return 0;
32811+}
7f207e10
AM
32812diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32813--- /usr/share/empty/fs/aufs/vfsub.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 32814+++ linux/fs/aufs/vfsub.c 2019-01-28 14:36:12.222418047 +0100
acd2b654 32815@@ -0,0 +1,902 @@
cd7a4cd9 32816+// SPDX-License-Identifier: GPL-2.0
1facf9fc 32817+/*
b00004a5 32818+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 32819+ *
32820+ * This program, aufs is free software; you can redistribute it and/or modify
32821+ * it under the terms of the GNU General Public License as published by
32822+ * the Free Software Foundation; either version 2 of the License, or
32823+ * (at your option) any later version.
dece6358
AM
32824+ *
32825+ * This program is distributed in the hope that it will be useful,
32826+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
32827+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32828+ * GNU General Public License for more details.
32829+ *
32830+ * You should have received a copy of the GNU General Public License
523b37e3 32831+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 32832+ */
32833+
32834+/*
32835+ * sub-routines for VFS
32836+ */
32837+
8b6a4947 32838+#include <linux/mnt_namespace.h>
dece6358 32839+#include <linux/namei.h>
8cdd5066 32840+#include <linux/nsproxy.h>
dece6358
AM
32841+#include <linux/security.h>
32842+#include <linux/splice.h>
1facf9fc 32843+#include "aufs.h"
32844+
8cdd5066
JR
32845+#ifdef CONFIG_AUFS_BR_FUSE
32846+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32847+{
8cdd5066
JR
32848+ if (!au_test_fuse(h_sb) || !au_userns)
32849+ return 0;
32850+
8b6a4947 32851+ return is_current_mnt_ns(mnt) ? 0 : -EACCES;
8cdd5066
JR
32852+}
32853+#endif
32854+
a2654f78
AM
32855+int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
32856+{
32857+ int err;
32858+
32859+ lockdep_off();
32860+ down_read(&h_sb->s_umount);
32861+ err = __sync_filesystem(h_sb, wait);
32862+ up_read(&h_sb->s_umount);
32863+ lockdep_on();
32864+
32865+ return err;
32866+}
32867+
8cdd5066
JR
32868+/* ---------------------------------------------------------------------- */
32869+
1facf9fc 32870+int vfsub_update_h_iattr(struct path *h_path, int *did)
32871+{
32872+ int err;
32873+ struct kstat st;
32874+ struct super_block *h_sb;
32875+
32876+ /* for remote fs, leave work for its getattr or d_revalidate */
32877+ /* for bad i_attr fs, handle them in aufs_getattr() */
32878+ /* still some fs may acquire i_mutex. we need to skip them */
32879+ err = 0;
32880+ if (!did)
32881+ did = &err;
32882+ h_sb = h_path->dentry->d_sb;
32883+ *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32884+ if (*did)
521ced18 32885+ err = vfsub_getattr(h_path, &st);
1facf9fc 32886+
32887+ return err;
32888+}
32889+
32890+/* ---------------------------------------------------------------------- */
32891+
4a4d8108 32892+struct file *vfsub_dentry_open(struct path *path, int flags)
1308ab2a 32893+{
32894+ struct file *file;
32895+
b4510431 32896+ file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
7f207e10 32897+ current_cred());
2cbb1c4b
JR
32898+ if (!IS_ERR_OR_NULL(file)
32899+ && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
5527c038 32900+ i_readcount_inc(d_inode(path->dentry));
4a4d8108 32901+
1308ab2a 32902+ return file;
32903+}
32904+
1facf9fc 32905+struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32906+{
32907+ struct file *file;
32908+
2cbb1c4b 32909+ lockdep_off();
7f207e10 32910+ file = filp_open(path,
2cbb1c4b 32911+ oflags /* | __FMODE_NONOTIFY */,
7f207e10 32912+ mode);
2cbb1c4b 32913+ lockdep_on();
1facf9fc 32914+ if (IS_ERR(file))
32915+ goto out;
32916+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32917+
4f0767ce 32918+out:
1facf9fc 32919+ return file;
32920+}
32921+
b912730e
AM
32922+/*
32923+ * Ideally this function should call VFS:do_last() in order to keep all its
32924+ * checkings. But it is very hard for aufs to regenerate several VFS internal
32925+ * structure such as nameidata. This is a second (or third) best approach.
32926+ * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32927+ */
32928+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
acd2b654 32929+ struct vfsub_aopen_args *args)
b912730e
AM
32930+{
32931+ int err;
acd2b654 32932+ struct au_branch *br = args->br;
b912730e
AM
32933+ struct file *file = args->file;
32934+ /* copied from linux/fs/namei.c:atomic_open() */
32935+ struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
32936+
32937+ IMustLock(dir);
32938+ AuDebugOn(!dir->i_op->atomic_open);
32939+
32940+ err = au_br_test_oflag(args->open_flag, br);
32941+ if (unlikely(err))
32942+ goto out;
32943+
acd2b654
AM
32944+ au_lcnt_inc(&br->br_nfiles);
32945+ file->f_path.dentry = DENTRY_NOT_SET;
32946+ file->f_path.mnt = au_br_mnt(br);
32947+ AuDbg("%ps\n", dir->i_op->atomic_open);
b912730e 32948+ err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
acd2b654
AM
32949+ args->create_mode);
32950+ if (unlikely(err < 0)) {
32951+ au_lcnt_dec(&br->br_nfiles);
b912730e 32952+ goto out;
acd2b654 32953+ }
b912730e 32954+
acd2b654
AM
32955+ /* temporary workaround for nfsv4 branch */
32956+ if (au_test_nfs(dir->i_sb))
32957+ nfs_mark_for_revalidate(dir);
b912730e 32958+
acd2b654
AM
32959+ if (file->f_mode & FMODE_CREATED)
32960+ fsnotify_create(dir, dentry);
32961+ if (!(file->f_mode & FMODE_OPENED)) {
32962+ au_lcnt_dec(&br->br_nfiles);
32963+ goto out;
b912730e
AM
32964+ }
32965+
acd2b654
AM
32966+ /* todo: call VFS:may_open() here */
32967+ /* todo: ima_file_check() too? */
32968+ if (!err && (args->open_flag & __FMODE_EXEC))
32969+ err = deny_write_access(file);
32970+ if (!err)
32971+ fsnotify_open(file);
32972+ else
32973+ au_lcnt_dec(&br->br_nfiles);
32974+ /* note that the file is created and still opened */
b912730e
AM
32975+
32976+out:
32977+ return err;
32978+}
32979+
1facf9fc 32980+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
32981+{
32982+ int err;
32983+
1facf9fc 32984+ err = kern_path(name, flags, path);
5527c038 32985+ if (!err && d_is_positive(path->dentry))
1facf9fc 32986+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
32987+ return err;
32988+}
32989+
febd17d6
JR
32990+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
32991+ struct dentry *parent, int len)
32992+{
32993+ struct path path = {
32994+ .mnt = NULL
32995+ };
32996+
32997+ path.dentry = lookup_one_len_unlocked(name, parent, len);
32998+ if (IS_ERR(path.dentry))
32999+ goto out;
33000+ if (d_is_positive(path.dentry))
33001+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33002+
33003+out:
33004+ AuTraceErrPtr(path.dentry);
33005+ return path.dentry;
33006+}
33007+
1facf9fc 33008+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
33009+ int len)
33010+{
33011+ struct path path = {
33012+ .mnt = NULL
33013+ };
33014+
1308ab2a 33015+ /* VFS checks it too, but by WARN_ON_ONCE() */
5527c038 33016+ IMustLock(d_inode(parent));
1facf9fc 33017+
33018+ path.dentry = lookup_one_len(name, parent, len);
33019+ if (IS_ERR(path.dentry))
33020+ goto out;
5527c038 33021+ if (d_is_positive(path.dentry))
1facf9fc 33022+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33023+
4f0767ce 33024+out:
4a4d8108 33025+ AuTraceErrPtr(path.dentry);
1facf9fc 33026+ return path.dentry;
33027+}
33028+
b4510431 33029+void vfsub_call_lkup_one(void *args)
2cbb1c4b 33030+{
b4510431
AM
33031+ struct vfsub_lkup_one_args *a = args;
33032+ *a->errp = vfsub_lkup_one(a->name, a->parent);
2cbb1c4b
JR
33033+}
33034+
1facf9fc 33035+/* ---------------------------------------------------------------------- */
33036+
33037+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33038+ struct dentry *d2, struct au_hinode *hdir2)
33039+{
33040+ struct dentry *d;
33041+
2cbb1c4b 33042+ lockdep_off();
1facf9fc 33043+ d = lock_rename(d1, d2);
2cbb1c4b 33044+ lockdep_on();
4a4d8108 33045+ au_hn_suspend(hdir1);
1facf9fc 33046+ if (hdir1 != hdir2)
4a4d8108 33047+ au_hn_suspend(hdir2);
1facf9fc 33048+
33049+ return d;
33050+}
33051+
33052+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33053+ struct dentry *d2, struct au_hinode *hdir2)
33054+{
4a4d8108 33055+ au_hn_resume(hdir1);
1facf9fc 33056+ if (hdir1 != hdir2)
4a4d8108 33057+ au_hn_resume(hdir2);
2cbb1c4b 33058+ lockdep_off();
1facf9fc 33059+ unlock_rename(d1, d2);
2cbb1c4b 33060+ lockdep_on();
1facf9fc 33061+}
33062+
33063+/* ---------------------------------------------------------------------- */
33064+
b4510431 33065+int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
1facf9fc 33066+{
33067+ int err;
33068+ struct dentry *d;
33069+
33070+ IMustLock(dir);
33071+
33072+ d = path->dentry;
33073+ path->dentry = d->d_parent;
b752ccd1 33074+ err = security_path_mknod(path, d, mode, 0);
1facf9fc 33075+ path->dentry = d;
33076+ if (unlikely(err))
33077+ goto out;
33078+
c1595e42 33079+ lockdep_off();
b4510431 33080+ err = vfs_create(dir, path->dentry, mode, want_excl);
c1595e42 33081+ lockdep_on();
1facf9fc 33082+ if (!err) {
33083+ struct path tmp = *path;
33084+ int did;
33085+
33086+ vfsub_update_h_iattr(&tmp, &did);
33087+ if (did) {
33088+ tmp.dentry = path->dentry->d_parent;
33089+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33090+ }
33091+ /*ignore*/
33092+ }
33093+
4f0767ce 33094+out:
1facf9fc 33095+ return err;
33096+}
33097+
33098+int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
33099+{
33100+ int err;
33101+ struct dentry *d;
33102+
33103+ IMustLock(dir);
33104+
33105+ d = path->dentry;
33106+ path->dentry = d->d_parent;
b752ccd1 33107+ err = security_path_symlink(path, d, symname);
1facf9fc 33108+ path->dentry = d;
33109+ if (unlikely(err))
33110+ goto out;
33111+
c1595e42 33112+ lockdep_off();
1facf9fc 33113+ err = vfs_symlink(dir, path->dentry, symname);
c1595e42 33114+ lockdep_on();
1facf9fc 33115+ if (!err) {
33116+ struct path tmp = *path;
33117+ int did;
33118+
33119+ vfsub_update_h_iattr(&tmp, &did);
33120+ if (did) {
33121+ tmp.dentry = path->dentry->d_parent;
33122+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33123+ }
33124+ /*ignore*/
33125+ }
33126+
4f0767ce 33127+out:
1facf9fc 33128+ return err;
33129+}
33130+
33131+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
33132+{
33133+ int err;
33134+ struct dentry *d;
33135+
33136+ IMustLock(dir);
33137+
33138+ d = path->dentry;
33139+ path->dentry = d->d_parent;
027c5e7a 33140+ err = security_path_mknod(path, d, mode, new_encode_dev(dev));
1facf9fc 33141+ path->dentry = d;
33142+ if (unlikely(err))
33143+ goto out;
33144+
c1595e42 33145+ lockdep_off();
1facf9fc 33146+ err = vfs_mknod(dir, path->dentry, mode, dev);
c1595e42 33147+ lockdep_on();
1facf9fc 33148+ if (!err) {
33149+ struct path tmp = *path;
33150+ int did;
33151+
33152+ vfsub_update_h_iattr(&tmp, &did);
33153+ if (did) {
33154+ tmp.dentry = path->dentry->d_parent;
33155+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33156+ }
33157+ /*ignore*/
33158+ }
33159+
4f0767ce 33160+out:
1facf9fc 33161+ return err;
33162+}
33163+
33164+static int au_test_nlink(struct inode *inode)
33165+{
33166+ const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33167+
33168+ if (!au_test_fs_no_limit_nlink(inode->i_sb)
33169+ || inode->i_nlink < link_max)
33170+ return 0;
33171+ return -EMLINK;
33172+}
33173+
523b37e3
AM
33174+int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33175+ struct inode **delegated_inode)
1facf9fc 33176+{
33177+ int err;
33178+ struct dentry *d;
33179+
33180+ IMustLock(dir);
33181+
5527c038 33182+ err = au_test_nlink(d_inode(src_dentry));
1facf9fc 33183+ if (unlikely(err))
33184+ return err;
33185+
b4510431 33186+ /* we don't call may_linkat() */
1facf9fc 33187+ d = path->dentry;
33188+ path->dentry = d->d_parent;
b752ccd1 33189+ err = security_path_link(src_dentry, path, d);
1facf9fc 33190+ path->dentry = d;
33191+ if (unlikely(err))
33192+ goto out;
33193+
2cbb1c4b 33194+ lockdep_off();
523b37e3 33195+ err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
2cbb1c4b 33196+ lockdep_on();
1facf9fc 33197+ if (!err) {
33198+ struct path tmp = *path;
33199+ int did;
33200+
33201+ /* fuse has different memory inode for the same inumber */
33202+ vfsub_update_h_iattr(&tmp, &did);
33203+ if (did) {
33204+ tmp.dentry = path->dentry->d_parent;
33205+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33206+ tmp.dentry = src_dentry;
33207+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33208+ }
33209+ /*ignore*/
33210+ }
33211+
4f0767ce 33212+out:
1facf9fc 33213+ return err;
33214+}
33215+
33216+int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
523b37e3 33217+ struct inode *dir, struct path *path,
f2c43d5f 33218+ struct inode **delegated_inode, unsigned int flags)
1facf9fc 33219+{
33220+ int err;
33221+ struct path tmp = {
33222+ .mnt = path->mnt
33223+ };
33224+ struct dentry *d;
33225+
33226+ IMustLock(dir);
33227+ IMustLock(src_dir);
33228+
33229+ d = path->dentry;
33230+ path->dentry = d->d_parent;
33231+ tmp.dentry = src_dentry->d_parent;
38d290e6 33232+ err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
1facf9fc 33233+ path->dentry = d;
33234+ if (unlikely(err))
33235+ goto out;
33236+
2cbb1c4b 33237+ lockdep_off();
523b37e3 33238+ err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
f2c43d5f 33239+ delegated_inode, flags);
2cbb1c4b 33240+ lockdep_on();
1facf9fc 33241+ if (!err) {
33242+ int did;
33243+
33244+ tmp.dentry = d->d_parent;
33245+ vfsub_update_h_iattr(&tmp, &did);
33246+ if (did) {
33247+ tmp.dentry = src_dentry;
33248+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33249+ tmp.dentry = src_dentry->d_parent;
33250+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33251+ }
33252+ /*ignore*/
33253+ }
33254+
4f0767ce 33255+out:
1facf9fc 33256+ return err;
33257+}
33258+
33259+int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33260+{
33261+ int err;
33262+ struct dentry *d;
33263+
33264+ IMustLock(dir);
33265+
33266+ d = path->dentry;
33267+ path->dentry = d->d_parent;
b752ccd1 33268+ err = security_path_mkdir(path, d, mode);
1facf9fc 33269+ path->dentry = d;
33270+ if (unlikely(err))
33271+ goto out;
33272+
c1595e42 33273+ lockdep_off();
1facf9fc 33274+ err = vfs_mkdir(dir, path->dentry, mode);
c1595e42 33275+ lockdep_on();
1facf9fc 33276+ if (!err) {
33277+ struct path tmp = *path;
33278+ int did;
33279+
33280+ vfsub_update_h_iattr(&tmp, &did);
33281+ if (did) {
33282+ tmp.dentry = path->dentry->d_parent;
33283+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33284+ }
33285+ /*ignore*/
33286+ }
33287+
4f0767ce 33288+out:
1facf9fc 33289+ return err;
33290+}
33291+
33292+int vfsub_rmdir(struct inode *dir, struct path *path)
33293+{
33294+ int err;
33295+ struct dentry *d;
33296+
33297+ IMustLock(dir);
33298+
33299+ d = path->dentry;
33300+ path->dentry = d->d_parent;
b752ccd1 33301+ err = security_path_rmdir(path, d);
1facf9fc 33302+ path->dentry = d;
33303+ if (unlikely(err))
33304+ goto out;
33305+
2cbb1c4b 33306+ lockdep_off();
1facf9fc 33307+ err = vfs_rmdir(dir, path->dentry);
2cbb1c4b 33308+ lockdep_on();
1facf9fc 33309+ if (!err) {
33310+ struct path tmp = {
33311+ .dentry = path->dentry->d_parent,
33312+ .mnt = path->mnt
33313+ };
33314+
33315+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33316+ }
33317+
4f0767ce 33318+out:
1facf9fc 33319+ return err;
33320+}
33321+
33322+/* ---------------------------------------------------------------------- */
33323+
9dbd164d 33324+/* todo: support mmap_sem? */
1facf9fc 33325+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33326+ loff_t *ppos)
33327+{
33328+ ssize_t err;
33329+
2cbb1c4b 33330+ lockdep_off();
1facf9fc 33331+ err = vfs_read(file, ubuf, count, ppos);
2cbb1c4b 33332+ lockdep_on();
1facf9fc 33333+ if (err >= 0)
33334+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33335+ return err;
33336+}
33337+
33338+/* todo: kernel_read()? */
33339+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33340+ loff_t *ppos)
33341+{
33342+ ssize_t err;
33343+ mm_segment_t oldfs;
b752ccd1
AM
33344+ union {
33345+ void *k;
33346+ char __user *u;
33347+ } buf;
1facf9fc 33348+
b752ccd1 33349+ buf.k = kbuf;
1facf9fc 33350+ oldfs = get_fs();
33351+ set_fs(KERNEL_DS);
b752ccd1 33352+ err = vfsub_read_u(file, buf.u, count, ppos);
1facf9fc 33353+ set_fs(oldfs);
33354+ return err;
33355+}
33356+
33357+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33358+ loff_t *ppos)
33359+{
33360+ ssize_t err;
33361+
2cbb1c4b 33362+ lockdep_off();
1facf9fc 33363+ err = vfs_write(file, ubuf, count, ppos);
2cbb1c4b 33364+ lockdep_on();
1facf9fc 33365+ if (err >= 0)
33366+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33367+ return err;
33368+}
33369+
33370+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33371+{
33372+ ssize_t err;
33373+ mm_segment_t oldfs;
b752ccd1
AM
33374+ union {
33375+ void *k;
33376+ const char __user *u;
33377+ } buf;
1facf9fc 33378+
b752ccd1 33379+ buf.k = kbuf;
1facf9fc 33380+ oldfs = get_fs();
33381+ set_fs(KERNEL_DS);
b752ccd1 33382+ err = vfsub_write_u(file, buf.u, count, ppos);
1facf9fc 33383+ set_fs(oldfs);
33384+ return err;
33385+}
33386+
4a4d8108
AM
33387+int vfsub_flush(struct file *file, fl_owner_t id)
33388+{
33389+ int err;
33390+
33391+ err = 0;
523b37e3 33392+ if (file->f_op->flush) {
2000de60 33393+ if (!au_test_nfs(file->f_path.dentry->d_sb))
2cbb1c4b
JR
33394+ err = file->f_op->flush(file, id);
33395+ else {
33396+ lockdep_off();
33397+ err = file->f_op->flush(file, id);
33398+ lockdep_on();
33399+ }
4a4d8108
AM
33400+ if (!err)
33401+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33402+ /*ignore*/
33403+ }
33404+ return err;
33405+}
33406+
392086de 33407+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
1facf9fc 33408+{
33409+ int err;
33410+
062440b3 33411+ AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
392086de 33412+
2cbb1c4b 33413+ lockdep_off();
392086de 33414+ err = iterate_dir(file, ctx);
2cbb1c4b 33415+ lockdep_on();
1facf9fc 33416+ if (err >= 0)
33417+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
1c60b727 33418+
1facf9fc 33419+ return err;
33420+}
33421+
33422+long vfsub_splice_to(struct file *in, loff_t *ppos,
33423+ struct pipe_inode_info *pipe, size_t len,
33424+ unsigned int flags)
33425+{
33426+ long err;
33427+
2cbb1c4b 33428+ lockdep_off();
0fc653ad 33429+ err = do_splice_to(in, ppos, pipe, len, flags);
2cbb1c4b 33430+ lockdep_on();
4a4d8108 33431+ file_accessed(in);
1facf9fc 33432+ if (err >= 0)
33433+ vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33434+ return err;
33435+}
33436+
33437+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33438+ loff_t *ppos, size_t len, unsigned int flags)
33439+{
33440+ long err;
33441+
2cbb1c4b 33442+ lockdep_off();
0fc653ad 33443+ err = do_splice_from(pipe, out, ppos, len, flags);
2cbb1c4b 33444+ lockdep_on();
1facf9fc 33445+ if (err >= 0)
33446+ vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33447+ return err;
33448+}
33449+
53392da6
AM
33450+int vfsub_fsync(struct file *file, struct path *path, int datasync)
33451+{
33452+ int err;
33453+
33454+ /* file can be NULL */
33455+ lockdep_off();
33456+ err = vfs_fsync(file, datasync);
33457+ lockdep_on();
33458+ if (!err) {
33459+ if (!path) {
33460+ AuDebugOn(!file);
33461+ path = &file->f_path;
33462+ }
33463+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33464+ }
33465+ return err;
33466+}
33467+
1facf9fc 33468+/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33469+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33470+ struct file *h_file)
33471+{
33472+ int err;
33473+ struct inode *h_inode;
c06a8ce3 33474+ struct super_block *h_sb;
1facf9fc 33475+
1facf9fc 33476+ if (!h_file) {
c06a8ce3
AM
33477+ err = vfsub_truncate(h_path, length);
33478+ goto out;
1facf9fc 33479+ }
33480+
5527c038 33481+ h_inode = d_inode(h_path->dentry);
c06a8ce3
AM
33482+ h_sb = h_inode->i_sb;
33483+ lockdep_off();
33484+ sb_start_write(h_sb);
33485+ lockdep_on();
1facf9fc 33486+ err = locks_verify_truncate(h_inode, h_file, length);
33487+ if (!err)
953406b4 33488+ err = security_path_truncate(h_path);
2cbb1c4b
JR
33489+ if (!err) {
33490+ lockdep_off();
1facf9fc 33491+ err = do_truncate(h_path->dentry, length, attr, h_file);
2cbb1c4b
JR
33492+ lockdep_on();
33493+ }
c06a8ce3
AM
33494+ lockdep_off();
33495+ sb_end_write(h_sb);
33496+ lockdep_on();
1facf9fc 33497+
4f0767ce 33498+out:
1facf9fc 33499+ return err;
33500+}
33501+
33502+/* ---------------------------------------------------------------------- */
33503+
33504+struct au_vfsub_mkdir_args {
33505+ int *errp;
33506+ struct inode *dir;
33507+ struct path *path;
33508+ int mode;
33509+};
33510+
33511+static void au_call_vfsub_mkdir(void *args)
33512+{
33513+ struct au_vfsub_mkdir_args *a = args;
33514+ *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33515+}
33516+
33517+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33518+{
33519+ int err, do_sio, wkq_err;
33520+
33521+ do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
33522+ if (!do_sio) {
33523+ lockdep_off();
1facf9fc 33524+ err = vfsub_mkdir(dir, path, mode);
c1595e42
JR
33525+ lockdep_on();
33526+ } else {
1facf9fc 33527+ struct au_vfsub_mkdir_args args = {
33528+ .errp = &err,
33529+ .dir = dir,
33530+ .path = path,
33531+ .mode = mode
33532+ };
33533+ wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33534+ if (unlikely(wkq_err))
33535+ err = wkq_err;
33536+ }
33537+
33538+ return err;
33539+}
33540+
33541+struct au_vfsub_rmdir_args {
33542+ int *errp;
33543+ struct inode *dir;
33544+ struct path *path;
33545+};
33546+
33547+static void au_call_vfsub_rmdir(void *args)
33548+{
33549+ struct au_vfsub_rmdir_args *a = args;
33550+ *a->errp = vfsub_rmdir(a->dir, a->path);
33551+}
33552+
33553+int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33554+{
33555+ int err, do_sio, wkq_err;
33556+
33557+ do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
33558+ if (!do_sio) {
33559+ lockdep_off();
1facf9fc 33560+ err = vfsub_rmdir(dir, path);
c1595e42
JR
33561+ lockdep_on();
33562+ } else {
1facf9fc 33563+ struct au_vfsub_rmdir_args args = {
33564+ .errp = &err,
33565+ .dir = dir,
33566+ .path = path
33567+ };
33568+ wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33569+ if (unlikely(wkq_err))
33570+ err = wkq_err;
33571+ }
33572+
33573+ return err;
33574+}
33575+
33576+/* ---------------------------------------------------------------------- */
33577+
33578+struct notify_change_args {
33579+ int *errp;
33580+ struct path *path;
33581+ struct iattr *ia;
523b37e3 33582+ struct inode **delegated_inode;
1facf9fc 33583+};
33584+
33585+static void call_notify_change(void *args)
33586+{
33587+ struct notify_change_args *a = args;
33588+ struct inode *h_inode;
33589+
5527c038 33590+ h_inode = d_inode(a->path->dentry);
1facf9fc 33591+ IMustLock(h_inode);
33592+
33593+ *a->errp = -EPERM;
33594+ if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
c1595e42 33595+ lockdep_off();
523b37e3
AM
33596+ *a->errp = notify_change(a->path->dentry, a->ia,
33597+ a->delegated_inode);
c1595e42 33598+ lockdep_on();
1facf9fc 33599+ if (!*a->errp)
33600+ vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33601+ }
33602+ AuTraceErr(*a->errp);
33603+}
33604+
523b37e3
AM
33605+int vfsub_notify_change(struct path *path, struct iattr *ia,
33606+ struct inode **delegated_inode)
1facf9fc 33607+{
33608+ int err;
33609+ struct notify_change_args args = {
523b37e3
AM
33610+ .errp = &err,
33611+ .path = path,
33612+ .ia = ia,
33613+ .delegated_inode = delegated_inode
1facf9fc 33614+ };
33615+
33616+ call_notify_change(&args);
33617+
33618+ return err;
33619+}
33620+
523b37e3
AM
33621+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33622+ struct inode **delegated_inode)
1facf9fc 33623+{
33624+ int err, wkq_err;
33625+ struct notify_change_args args = {
523b37e3
AM
33626+ .errp = &err,
33627+ .path = path,
33628+ .ia = ia,
33629+ .delegated_inode = delegated_inode
1facf9fc 33630+ };
33631+
33632+ wkq_err = au_wkq_wait(call_notify_change, &args);
33633+ if (unlikely(wkq_err))
33634+ err = wkq_err;
33635+
33636+ return err;
33637+}
33638+
33639+/* ---------------------------------------------------------------------- */
33640+
33641+struct unlink_args {
33642+ int *errp;
33643+ struct inode *dir;
33644+ struct path *path;
523b37e3 33645+ struct inode **delegated_inode;
1facf9fc 33646+};
33647+
33648+static void call_unlink(void *args)
33649+{
33650+ struct unlink_args *a = args;
33651+ struct dentry *d = a->path->dentry;
33652+ struct inode *h_inode;
33653+ const int stop_sillyrename = (au_test_nfs(d->d_sb)
c1595e42 33654+ && au_dcount(d) == 1);
1facf9fc 33655+
33656+ IMustLock(a->dir);
33657+
33658+ a->path->dentry = d->d_parent;
33659+ *a->errp = security_path_unlink(a->path, d);
33660+ a->path->dentry = d;
33661+ if (unlikely(*a->errp))
33662+ return;
33663+
33664+ if (!stop_sillyrename)
33665+ dget(d);
5527c038
JR
33666+ h_inode = NULL;
33667+ if (d_is_positive(d)) {
33668+ h_inode = d_inode(d);
027c5e7a 33669+ ihold(h_inode);
5527c038 33670+ }
1facf9fc 33671+
2cbb1c4b 33672+ lockdep_off();
523b37e3 33673+ *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
2cbb1c4b 33674+ lockdep_on();
1facf9fc 33675+ if (!*a->errp) {
33676+ struct path tmp = {
33677+ .dentry = d->d_parent,
33678+ .mnt = a->path->mnt
33679+ };
33680+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33681+ }
33682+
33683+ if (!stop_sillyrename)
33684+ dput(d);
33685+ if (h_inode)
33686+ iput(h_inode);
33687+
33688+ AuTraceErr(*a->errp);
33689+}
33690+
33691+/*
33692+ * @dir: must be locked.
33693+ * @dentry: target dentry.
33694+ */
523b37e3
AM
33695+int vfsub_unlink(struct inode *dir, struct path *path,
33696+ struct inode **delegated_inode, int force)
1facf9fc 33697+{
33698+ int err;
33699+ struct unlink_args args = {
523b37e3
AM
33700+ .errp = &err,
33701+ .dir = dir,
33702+ .path = path,
33703+ .delegated_inode = delegated_inode
1facf9fc 33704+ };
33705+
33706+ if (!force)
33707+ call_unlink(&args);
33708+ else {
33709+ int wkq_err;
33710+
33711+ wkq_err = au_wkq_wait(call_unlink, &args);
33712+ if (unlikely(wkq_err))
33713+ err = wkq_err;
33714+ }
33715+
33716+ return err;
33717+}
7f207e10
AM
33718diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33719--- /usr/share/empty/fs/aufs/vfsub.h 1970-01-01 01:00:00.000000000 +0100
e49925d1 33720+++ linux/fs/aufs/vfsub.h 2019-01-28 14:36:12.222418047 +0100
062440b3
AM
33721@@ -0,0 +1,355 @@
33722+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 33723+/*
b00004a5 33724+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 33725+ *
33726+ * This program, aufs is free software; you can redistribute it and/or modify
33727+ * it under the terms of the GNU General Public License as published by
33728+ * the Free Software Foundation; either version 2 of the License, or
33729+ * (at your option) any later version.
dece6358
AM
33730+ *
33731+ * This program is distributed in the hope that it will be useful,
33732+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33733+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33734+ * GNU General Public License for more details.
33735+ *
33736+ * You should have received a copy of the GNU General Public License
523b37e3 33737+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 33738+ */
33739+
33740+/*
33741+ * sub-routines for VFS
33742+ */
33743+
33744+#ifndef __AUFS_VFSUB_H__
33745+#define __AUFS_VFSUB_H__
33746+
33747+#ifdef __KERNEL__
33748+
33749+#include <linux/fs.h>
be118d29 33750+#include <linux/iversion.h>
b4510431 33751+#include <linux/mount.h>
8cdd5066 33752+#include <linux/posix_acl.h>
c1595e42 33753+#include <linux/xattr.h>
7f207e10 33754+#include "debug.h"
1facf9fc 33755+
7f207e10 33756+/* copied from linux/fs/internal.h */
2cbb1c4b 33757+/* todo: BAD approach!! */
c06a8ce3 33758+extern void __mnt_drop_write(struct vfsmount *);
acd2b654 33759+extern struct file *alloc_empty_file(int, const struct cred *);
7f207e10
AM
33760+
33761+/* ---------------------------------------------------------------------- */
1facf9fc 33762+
33763+/* lock subclass for lower inode */
33764+/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33765+/* reduce? gave up. */
33766+enum {
c1595e42 33767+ AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
1facf9fc 33768+ AuLsc_I_PARENT, /* lower inode, parent first */
33769+ AuLsc_I_PARENT2, /* copyup dirs */
dece6358 33770+ AuLsc_I_PARENT3, /* copyup wh */
1facf9fc 33771+ AuLsc_I_CHILD,
33772+ AuLsc_I_CHILD2,
33773+ AuLsc_I_End
33774+};
33775+
33776+/* to debug easier, do not make them inlined functions */
33777+#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
febd17d6 33778+#define IMustLock(i) AuDebugOn(!inode_is_locked(i))
1facf9fc 33779+
33780+/* ---------------------------------------------------------------------- */
33781+
7f207e10
AM
33782+static inline void vfsub_drop_nlink(struct inode *inode)
33783+{
33784+ AuDebugOn(!inode->i_nlink);
33785+ drop_nlink(inode);
33786+}
33787+
027c5e7a
AM
33788+static inline void vfsub_dead_dir(struct inode *inode)
33789+{
33790+ AuDebugOn(!S_ISDIR(inode->i_mode));
33791+ inode->i_flags |= S_DEAD;
33792+ clear_nlink(inode);
33793+}
33794+
392086de
AM
33795+static inline int vfsub_native_ro(struct inode *inode)
33796+{
8b6a4947 33797+ return sb_rdonly(inode->i_sb)
392086de
AM
33798+ || IS_RDONLY(inode)
33799+ /* || IS_APPEND(inode) */
33800+ || IS_IMMUTABLE(inode);
33801+}
33802+
8cdd5066
JR
33803+#ifdef CONFIG_AUFS_BR_FUSE
33804+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33805+#else
33806+AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33807+#endif
33808+
a2654f78
AM
33809+int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
33810+
7f207e10
AM
33811+/* ---------------------------------------------------------------------- */
33812+
33813+int vfsub_update_h_iattr(struct path *h_path, int *did);
33814+struct file *vfsub_dentry_open(struct path *path, int flags);
33815+struct file *vfsub_filp_open(const char *path, int oflags, int mode);
acd2b654 33816+struct au_branch;
b912730e 33817+struct vfsub_aopen_args {
acd2b654
AM
33818+ struct file *file;
33819+ unsigned int open_flag;
33820+ umode_t create_mode;
33821+ struct au_branch *br;
b912730e 33822+};
b912730e 33823+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
acd2b654 33824+ struct vfsub_aopen_args *args);
1facf9fc 33825+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
b4510431 33826+
febd17d6
JR
33827+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33828+ struct dentry *parent, int len);
1facf9fc 33829+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
33830+ int len);
b4510431
AM
33831+
33832+struct vfsub_lkup_one_args {
33833+ struct dentry **errp;
33834+ struct qstr *name;
33835+ struct dentry *parent;
33836+};
33837+
33838+static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33839+ struct dentry *parent)
33840+{
33841+ return vfsub_lookup_one_len(name->name, parent, name->len);
33842+}
33843+
33844+void vfsub_call_lkup_one(void *args);
33845+
33846+/* ---------------------------------------------------------------------- */
33847+
33848+static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33849+{
33850+ int err;
076b876e 33851+
b4510431
AM
33852+ lockdep_off();
33853+ err = mnt_want_write(mnt);
33854+ lockdep_on();
33855+ return err;
33856+}
33857+
33858+static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33859+{
33860+ lockdep_off();
33861+ mnt_drop_write(mnt);
33862+ lockdep_on();
33863+}
1facf9fc 33864+
7e9cd9fe 33865+#if 0 /* reserved */
c06a8ce3
AM
33866+static inline void vfsub_mnt_drop_write_file(struct file *file)
33867+{
33868+ lockdep_off();
33869+ mnt_drop_write_file(file);
33870+ lockdep_on();
33871+}
7e9cd9fe 33872+#endif
c06a8ce3 33873+
1facf9fc 33874+/* ---------------------------------------------------------------------- */
33875+
33876+struct au_hinode;
33877+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33878+ struct dentry *d2, struct au_hinode *hdir2);
33879+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33880+ struct dentry *d2, struct au_hinode *hdir2);
33881+
537831f9
AM
33882+int vfsub_create(struct inode *dir, struct path *path, int mode,
33883+ bool want_excl);
1facf9fc 33884+int vfsub_symlink(struct inode *dir, struct path *path,
33885+ const char *symname);
33886+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33887+int vfsub_link(struct dentry *src_dentry, struct inode *dir,
523b37e3 33888+ struct path *path, struct inode **delegated_inode);
1facf9fc 33889+int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
523b37e3 33890+ struct inode *hdir, struct path *path,
f2c43d5f 33891+ struct inode **delegated_inode, unsigned int flags);
1facf9fc 33892+int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33893+int vfsub_rmdir(struct inode *dir, struct path *path);
33894+
33895+/* ---------------------------------------------------------------------- */
33896+
33897+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33898+ loff_t *ppos);
33899+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33900+ loff_t *ppos);
33901+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33902+ loff_t *ppos);
33903+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33904+ loff_t *ppos);
4a4d8108 33905+int vfsub_flush(struct file *file, fl_owner_t id);
392086de
AM
33906+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33907+
c06a8ce3
AM
33908+static inline loff_t vfsub_f_size_read(struct file *file)
33909+{
33910+ return i_size_read(file_inode(file));
33911+}
33912+
4a4d8108
AM
33913+static inline unsigned int vfsub_file_flags(struct file *file)
33914+{
33915+ unsigned int flags;
33916+
33917+ spin_lock(&file->f_lock);
33918+ flags = file->f_flags;
33919+ spin_unlock(&file->f_lock);
33920+
33921+ return flags;
33922+}
1308ab2a 33923+
f0c0a007
AM
33924+static inline int vfsub_file_execed(struct file *file)
33925+{
33926+ /* todo: direct access f_flags */
33927+ return !!(vfsub_file_flags(file) & __FMODE_EXEC);
33928+}
33929+
7e9cd9fe 33930+#if 0 /* reserved */
1facf9fc 33931+static inline void vfsub_file_accessed(struct file *h_file)
33932+{
33933+ file_accessed(h_file);
33934+ vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
33935+}
7e9cd9fe 33936+#endif
1facf9fc 33937+
79b8bda9 33938+#if 0 /* reserved */
1facf9fc 33939+static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
33940+ struct dentry *h_dentry)
33941+{
33942+ struct path h_path = {
33943+ .dentry = h_dentry,
33944+ .mnt = h_mnt
33945+ };
92d182d2 33946+ touch_atime(&h_path);
1facf9fc 33947+ vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
33948+}
79b8bda9 33949+#endif
1facf9fc 33950+
cd7a4cd9
AM
33951+static inline int vfsub_update_time(struct inode *h_inode,
33952+ struct timespec64 *ts, int flags)
0c3ec466 33953+{
5afbbe0d 33954+ return update_time(h_inode, ts, flags);
0c3ec466
AM
33955+ /* no vfsub_update_h_iattr() since we don't have struct path */
33956+}
33957+
8cdd5066
JR
33958+#ifdef CONFIG_FS_POSIX_ACL
33959+static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
33960+{
33961+ int err;
33962+
33963+ err = posix_acl_chmod(h_inode, h_mode);
33964+ if (err == -EOPNOTSUPP)
33965+ err = 0;
33966+ return err;
33967+}
33968+#else
33969+AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
33970+#endif
33971+
4a4d8108
AM
33972+long vfsub_splice_to(struct file *in, loff_t *ppos,
33973+ struct pipe_inode_info *pipe, size_t len,
33974+ unsigned int flags);
33975+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33976+ loff_t *ppos, size_t len, unsigned int flags);
c06a8ce3
AM
33977+
33978+static inline long vfsub_truncate(struct path *path, loff_t length)
33979+{
33980+ long err;
076b876e 33981+
c06a8ce3
AM
33982+ lockdep_off();
33983+ err = vfs_truncate(path, length);
33984+ lockdep_on();
33985+ return err;
33986+}
33987+
4a4d8108
AM
33988+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33989+ struct file *h_file);
53392da6 33990+int vfsub_fsync(struct file *file, struct path *path, int datasync);
4a4d8108 33991+
521ced18
JR
33992+/*
33993+ * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
33994+ * ioctl.
33995+ */
33996+static inline int vfsub_clone_file_range(struct file *src, struct file *dst,
33997+ u64 len)
33998+{
33999+ int err;
34000+
34001+ lockdep_off();
34002+ err = vfs_clone_file_range(src, 0, dst, 0, len);
34003+ lockdep_on();
34004+
34005+ return err;
34006+}
34007+
34008+/* copy_file_range(2) is a systemcall */
34009+static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
34010+ struct file *dst, loff_t dst_pos,
34011+ size_t len, unsigned int flags)
34012+{
34013+ ssize_t ssz;
34014+
34015+ lockdep_off();
34016+ ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
34017+ lockdep_on();
34018+
34019+ return ssz;
34020+}
34021+
1facf9fc 34022+/* ---------------------------------------------------------------------- */
34023+
34024+static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
34025+{
34026+ loff_t err;
34027+
2cbb1c4b 34028+ lockdep_off();
1facf9fc 34029+ err = vfs_llseek(file, offset, origin);
2cbb1c4b 34030+ lockdep_on();
1facf9fc 34031+ return err;
34032+}
34033+
34034+/* ---------------------------------------------------------------------- */
34035+
4a4d8108
AM
34036+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
34037+int vfsub_sio_rmdir(struct inode *dir, struct path *path);
523b37e3
AM
34038+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
34039+ struct inode **delegated_inode);
34040+int vfsub_notify_change(struct path *path, struct iattr *ia,
34041+ struct inode **delegated_inode);
34042+int vfsub_unlink(struct inode *dir, struct path *path,
34043+ struct inode **delegated_inode, int force);
4a4d8108 34044+
521ced18
JR
34045+static inline int vfsub_getattr(const struct path *path, struct kstat *st)
34046+{
34047+ return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
34048+}
34049+
c1595e42
JR
34050+/* ---------------------------------------------------------------------- */
34051+
34052+static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
34053+ const void *value, size_t size, int flags)
34054+{
34055+ int err;
34056+
34057+ lockdep_off();
34058+ err = vfs_setxattr(dentry, name, value, size, flags);
34059+ lockdep_on();
34060+
34061+ return err;
34062+}
34063+
34064+static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
34065+{
34066+ int err;
34067+
34068+ lockdep_off();
34069+ err = vfs_removexattr(dentry, name);
34070+ lockdep_on();
34071+
34072+ return err;
34073+}
34074+
1facf9fc 34075+#endif /* __KERNEL__ */
34076+#endif /* __AUFS_VFSUB_H__ */
7f207e10
AM
34077diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
34078--- /usr/share/empty/fs/aufs/wbr_policy.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 34079+++ linux/fs/aufs/wbr_policy.c 2019-01-28 14:36:12.222418047 +0100
cd7a4cd9
AM
34080@@ -0,0 +1,830 @@
34081+// SPDX-License-Identifier: GPL-2.0
1facf9fc 34082+/*
b00004a5 34083+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 34084+ *
34085+ * This program, aufs is free software; you can redistribute it and/or modify
34086+ * it under the terms of the GNU General Public License as published by
34087+ * the Free Software Foundation; either version 2 of the License, or
34088+ * (at your option) any later version.
dece6358
AM
34089+ *
34090+ * This program is distributed in the hope that it will be useful,
34091+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
34092+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34093+ * GNU General Public License for more details.
34094+ *
34095+ * You should have received a copy of the GNU General Public License
523b37e3 34096+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 34097+ */
34098+
34099+/*
34100+ * policies for selecting one among multiple writable branches
34101+ */
34102+
34103+#include <linux/statfs.h>
34104+#include "aufs.h"
34105+
34106+/* subset of cpup_attr() */
34107+static noinline_for_stack
34108+int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
34109+{
34110+ int err, sbits;
34111+ struct iattr ia;
34112+ struct inode *h_isrc;
34113+
5527c038 34114+ h_isrc = d_inode(h_src);
1facf9fc 34115+ ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
34116+ ia.ia_mode = h_isrc->i_mode;
34117+ ia.ia_uid = h_isrc->i_uid;
34118+ ia.ia_gid = h_isrc->i_gid;
34119+ sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
5527c038 34120+ au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
523b37e3
AM
34121+ /* no delegation since it is just created */
34122+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 34123+
34124+ /* is this nfs only? */
34125+ if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
34126+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
34127+ ia.ia_mode = h_isrc->i_mode;
523b37e3 34128+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 34129+ }
34130+
34131+ return err;
34132+}
34133+
34134+#define AuCpdown_PARENT_OPQ 1
34135+#define AuCpdown_WHED (1 << 1)
34136+#define AuCpdown_MADE_DIR (1 << 2)
34137+#define AuCpdown_DIROPQ (1 << 3)
34138+#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
7f207e10
AM
34139+#define au_fset_cpdown(flags, name) \
34140+ do { (flags) |= AuCpdown_##name; } while (0)
34141+#define au_fclr_cpdown(flags, name) \
34142+ do { (flags) &= ~AuCpdown_##name; } while (0)
1facf9fc 34143+
1facf9fc 34144+static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
c2b27bf2 34145+ unsigned int *flags)
1facf9fc 34146+{
34147+ int err;
34148+ struct dentry *opq_dentry;
34149+
34150+ opq_dentry = au_diropq_create(dentry, bdst);
34151+ err = PTR_ERR(opq_dentry);
34152+ if (IS_ERR(opq_dentry))
34153+ goto out;
34154+ dput(opq_dentry);
c2b27bf2 34155+ au_fset_cpdown(*flags, DIROPQ);
1facf9fc 34156+
4f0767ce 34157+out:
1facf9fc 34158+ return err;
34159+}
34160+
34161+static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34162+ struct inode *dir, aufs_bindex_t bdst)
34163+{
34164+ int err;
34165+ struct path h_path;
34166+ struct au_branch *br;
34167+
34168+ br = au_sbr(dentry->d_sb, bdst);
34169+ h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34170+ err = PTR_ERR(h_path.dentry);
34171+ if (IS_ERR(h_path.dentry))
34172+ goto out;
34173+
34174+ err = 0;
5527c038 34175+ if (d_is_positive(h_path.dentry)) {
86dc4139 34176+ h_path.mnt = au_br_mnt(br);
1facf9fc 34177+ err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34178+ dentry);
34179+ }
34180+ dput(h_path.dentry);
34181+
4f0767ce 34182+out:
1facf9fc 34183+ return err;
34184+}
34185+
34186+static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 34187+ struct au_pin *pin,
1facf9fc 34188+ struct dentry *h_parent, void *arg)
34189+{
34190+ int err, rerr;
5afbbe0d 34191+ aufs_bindex_t bopq, btop;
1facf9fc 34192+ struct path h_path;
34193+ struct dentry *parent;
34194+ struct inode *h_dir, *h_inode, *inode, *dir;
c2b27bf2 34195+ unsigned int *flags = arg;
1facf9fc 34196+
5afbbe0d 34197+ btop = au_dbtop(dentry);
1facf9fc 34198+ /* dentry is di-locked */
34199+ parent = dget_parent(dentry);
5527c038
JR
34200+ dir = d_inode(parent);
34201+ h_dir = d_inode(h_parent);
1facf9fc 34202+ AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34203+ IMustLock(h_dir);
34204+
86dc4139 34205+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
1facf9fc 34206+ if (unlikely(err < 0))
34207+ goto out;
34208+ h_path.dentry = au_h_dptr(dentry, bdst);
34209+ h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
cd7a4cd9 34210+ err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
1facf9fc 34211+ if (unlikely(err))
34212+ goto out_put;
c2b27bf2 34213+ au_fset_cpdown(*flags, MADE_DIR);
1facf9fc 34214+
1facf9fc 34215+ bopq = au_dbdiropq(dentry);
c2b27bf2
AM
34216+ au_fclr_cpdown(*flags, WHED);
34217+ au_fclr_cpdown(*flags, DIROPQ);
1facf9fc 34218+ if (au_dbwh(dentry) == bdst)
c2b27bf2
AM
34219+ au_fset_cpdown(*flags, WHED);
34220+ if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34221+ au_fset_cpdown(*flags, PARENT_OPQ);
5527c038 34222+ h_inode = d_inode(h_path.dentry);
febd17d6 34223+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
c2b27bf2
AM
34224+ if (au_ftest_cpdown(*flags, WHED)) {
34225+ err = au_cpdown_dir_opq(dentry, bdst, flags);
1facf9fc 34226+ if (unlikely(err)) {
febd17d6 34227+ inode_unlock(h_inode);
1facf9fc 34228+ goto out_dir;
34229+ }
34230+ }
34231+
5afbbe0d 34232+ err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
febd17d6 34233+ inode_unlock(h_inode);
1facf9fc 34234+ if (unlikely(err))
34235+ goto out_opq;
34236+
c2b27bf2 34237+ if (au_ftest_cpdown(*flags, WHED)) {
1facf9fc 34238+ err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34239+ if (unlikely(err))
34240+ goto out_opq;
34241+ }
34242+
5527c038 34243+ inode = d_inode(dentry);
5afbbe0d
AM
34244+ if (au_ibbot(inode) < bdst)
34245+ au_set_ibbot(inode, bdst);
1facf9fc 34246+ au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34247+ au_hi_flags(inode, /*isdir*/1));
076b876e 34248+ au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
1facf9fc 34249+ goto out; /* success */
34250+
34251+ /* revert */
4f0767ce 34252+out_opq:
c2b27bf2 34253+ if (au_ftest_cpdown(*flags, DIROPQ)) {
febd17d6 34254+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 34255+ rerr = au_diropq_remove(dentry, bdst);
febd17d6 34256+ inode_unlock(h_inode);
1facf9fc 34257+ if (unlikely(rerr)) {
523b37e3
AM
34258+ AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34259+ dentry, bdst, rerr);
1facf9fc 34260+ err = -EIO;
34261+ goto out;
34262+ }
34263+ }
4f0767ce 34264+out_dir:
c2b27bf2 34265+ if (au_ftest_cpdown(*flags, MADE_DIR)) {
1facf9fc 34266+ rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34267+ if (unlikely(rerr)) {
523b37e3
AM
34268+ AuIOErr("failed removing %pd b%d (%d)\n",
34269+ dentry, bdst, rerr);
1facf9fc 34270+ err = -EIO;
34271+ }
34272+ }
4f0767ce 34273+out_put:
1facf9fc 34274+ au_set_h_dptr(dentry, bdst, NULL);
5afbbe0d
AM
34275+ if (au_dbbot(dentry) == bdst)
34276+ au_update_dbbot(dentry);
4f0767ce 34277+out:
1facf9fc 34278+ dput(parent);
34279+ return err;
34280+}
34281+
34282+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34283+{
34284+ int err;
c2b27bf2 34285+ unsigned int flags;
1facf9fc 34286+
c2b27bf2
AM
34287+ flags = 0;
34288+ err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
1facf9fc 34289+
34290+ return err;
34291+}
34292+
34293+/* ---------------------------------------------------------------------- */
34294+
34295+/* policies for create */
34296+
c2b27bf2 34297+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
4a4d8108
AM
34298+{
34299+ int err, i, j, ndentry;
34300+ aufs_bindex_t bopq;
34301+ struct au_dcsub_pages dpages;
34302+ struct au_dpage *dpage;
34303+ struct dentry **dentries, *parent, *d;
34304+
34305+ err = au_dpages_init(&dpages, GFP_NOFS);
34306+ if (unlikely(err))
34307+ goto out;
34308+ parent = dget_parent(dentry);
027c5e7a 34309+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
4a4d8108
AM
34310+ if (unlikely(err))
34311+ goto out_free;
34312+
34313+ err = bindex;
34314+ for (i = 0; i < dpages.ndpage; i++) {
34315+ dpage = dpages.dpages + i;
34316+ dentries = dpage->dentries;
34317+ ndentry = dpage->ndentry;
34318+ for (j = 0; j < ndentry; j++) {
34319+ d = dentries[j];
34320+ di_read_lock_parent2(d, !AuLock_IR);
34321+ bopq = au_dbdiropq(d);
34322+ di_read_unlock(d, !AuLock_IR);
34323+ if (bopq >= 0 && bopq < err)
34324+ err = bopq;
34325+ }
34326+ }
34327+
34328+out_free:
34329+ dput(parent);
34330+ au_dpages_free(&dpages);
34331+out:
34332+ return err;
34333+}
34334+
1facf9fc 34335+static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34336+{
34337+ for (; bindex >= 0; bindex--)
34338+ if (!au_br_rdonly(au_sbr(sb, bindex)))
34339+ return bindex;
34340+ return -EROFS;
34341+}
34342+
34343+/* top down parent */
392086de
AM
34344+static int au_wbr_create_tdp(struct dentry *dentry,
34345+ unsigned int flags __maybe_unused)
1facf9fc 34346+{
34347+ int err;
5afbbe0d 34348+ aufs_bindex_t btop, bindex;
1facf9fc 34349+ struct super_block *sb;
34350+ struct dentry *parent, *h_parent;
34351+
34352+ sb = dentry->d_sb;
5afbbe0d
AM
34353+ btop = au_dbtop(dentry);
34354+ err = btop;
34355+ if (!au_br_rdonly(au_sbr(sb, btop)))
1facf9fc 34356+ goto out;
34357+
34358+ err = -EROFS;
34359+ parent = dget_parent(dentry);
5afbbe0d 34360+ for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
1facf9fc 34361+ h_parent = au_h_dptr(parent, bindex);
5527c038 34362+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34363+ continue;
34364+
34365+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34366+ err = bindex;
34367+ break;
34368+ }
34369+ }
34370+ dput(parent);
34371+
34372+ /* bottom up here */
4a4d8108 34373+ if (unlikely(err < 0)) {
5afbbe0d 34374+ err = au_wbr_bu(sb, btop - 1);
4a4d8108
AM
34375+ if (err >= 0)
34376+ err = au_wbr_nonopq(dentry, err);
34377+ }
1facf9fc 34378+
4f0767ce 34379+out:
1facf9fc 34380+ AuDbg("b%d\n", err);
34381+ return err;
34382+}
34383+
34384+/* ---------------------------------------------------------------------- */
34385+
34386+/* an exception for the policy other than tdp */
34387+static int au_wbr_create_exp(struct dentry *dentry)
34388+{
34389+ int err;
34390+ aufs_bindex_t bwh, bdiropq;
34391+ struct dentry *parent;
34392+
34393+ err = -1;
34394+ bwh = au_dbwh(dentry);
34395+ parent = dget_parent(dentry);
34396+ bdiropq = au_dbdiropq(parent);
34397+ if (bwh >= 0) {
34398+ if (bdiropq >= 0)
34399+ err = min(bdiropq, bwh);
34400+ else
34401+ err = bwh;
34402+ AuDbg("%d\n", err);
34403+ } else if (bdiropq >= 0) {
34404+ err = bdiropq;
34405+ AuDbg("%d\n", err);
34406+ }
34407+ dput(parent);
34408+
4a4d8108
AM
34409+ if (err >= 0)
34410+ err = au_wbr_nonopq(dentry, err);
34411+
1facf9fc 34412+ if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34413+ err = -1;
34414+
34415+ AuDbg("%d\n", err);
34416+ return err;
34417+}
34418+
34419+/* ---------------------------------------------------------------------- */
34420+
34421+/* round robin */
34422+static int au_wbr_create_init_rr(struct super_block *sb)
34423+{
34424+ int err;
34425+
5afbbe0d 34426+ err = au_wbr_bu(sb, au_sbbot(sb));
1facf9fc 34427+ atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
dece6358 34428+ /* smp_mb(); */
1facf9fc 34429+
34430+ AuDbg("b%d\n", err);
34431+ return err;
34432+}
34433+
392086de 34434+static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
1facf9fc 34435+{
34436+ int err, nbr;
34437+ unsigned int u;
5afbbe0d 34438+ aufs_bindex_t bindex, bbot;
1facf9fc 34439+ struct super_block *sb;
34440+ atomic_t *next;
34441+
34442+ err = au_wbr_create_exp(dentry);
34443+ if (err >= 0)
34444+ goto out;
34445+
34446+ sb = dentry->d_sb;
34447+ next = &au_sbi(sb)->si_wbr_rr_next;
5afbbe0d
AM
34448+ bbot = au_sbbot(sb);
34449+ nbr = bbot + 1;
34450+ for (bindex = 0; bindex <= bbot; bindex++) {
392086de 34451+ if (!au_ftest_wbr(flags, DIR)) {
1facf9fc 34452+ err = atomic_dec_return(next) + 1;
34453+ /* modulo for 0 is meaningless */
34454+ if (unlikely(!err))
34455+ err = atomic_dec_return(next) + 1;
34456+ } else
34457+ err = atomic_read(next);
34458+ AuDbg("%d\n", err);
34459+ u = err;
34460+ err = u % nbr;
34461+ AuDbg("%d\n", err);
34462+ if (!au_br_rdonly(au_sbr(sb, err)))
34463+ break;
34464+ err = -EROFS;
34465+ }
34466+
4a4d8108
AM
34467+ if (err >= 0)
34468+ err = au_wbr_nonopq(dentry, err);
34469+
4f0767ce 34470+out:
1facf9fc 34471+ AuDbg("%d\n", err);
34472+ return err;
34473+}
34474+
34475+/* ---------------------------------------------------------------------- */
34476+
34477+/* most free space */
392086de 34478+static void au_mfs(struct dentry *dentry, struct dentry *parent)
1facf9fc 34479+{
34480+ struct super_block *sb;
34481+ struct au_branch *br;
34482+ struct au_wbr_mfs *mfs;
392086de 34483+ struct dentry *h_parent;
5afbbe0d 34484+ aufs_bindex_t bindex, bbot;
1facf9fc 34485+ int err;
34486+ unsigned long long b, bavail;
7f207e10 34487+ struct path h_path;
1facf9fc 34488+ /* reduce the stack usage */
34489+ struct kstatfs *st;
34490+
34491+ st = kmalloc(sizeof(*st), GFP_NOFS);
34492+ if (unlikely(!st)) {
34493+ AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34494+ return;
34495+ }
34496+
34497+ bavail = 0;
34498+ sb = dentry->d_sb;
34499+ mfs = &au_sbi(sb)->si_wbr_mfs;
dece6358 34500+ MtxMustLock(&mfs->mfs_lock);
1facf9fc 34501+ mfs->mfs_bindex = -EROFS;
34502+ mfs->mfsrr_bytes = 0;
392086de
AM
34503+ if (!parent) {
34504+ bindex = 0;
5afbbe0d 34505+ bbot = au_sbbot(sb);
392086de 34506+ } else {
5afbbe0d
AM
34507+ bindex = au_dbtop(parent);
34508+ bbot = au_dbtaildir(parent);
392086de
AM
34509+ }
34510+
5afbbe0d 34511+ for (; bindex <= bbot; bindex++) {
392086de
AM
34512+ if (parent) {
34513+ h_parent = au_h_dptr(parent, bindex);
5527c038 34514+ if (!h_parent || d_is_negative(h_parent))
392086de
AM
34515+ continue;
34516+ }
1facf9fc 34517+ br = au_sbr(sb, bindex);
34518+ if (au_br_rdonly(br))
34519+ continue;
34520+
34521+ /* sb->s_root for NFS is unreliable */
86dc4139 34522+ h_path.mnt = au_br_mnt(br);
7f207e10
AM
34523+ h_path.dentry = h_path.mnt->mnt_root;
34524+ err = vfs_statfs(&h_path, st);
1facf9fc 34525+ if (unlikely(err)) {
34526+ AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34527+ continue;
34528+ }
34529+
34530+ /* when the available size is equal, select the lower one */
34531+ BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34532+ || sizeof(b) < sizeof(st->f_bsize));
34533+ b = st->f_bavail * st->f_bsize;
34534+ br->br_wbr->wbr_bytes = b;
34535+ if (b >= bavail) {
34536+ bavail = b;
34537+ mfs->mfs_bindex = bindex;
34538+ mfs->mfs_jiffy = jiffies;
34539+ }
34540+ }
34541+
34542+ mfs->mfsrr_bytes = bavail;
34543+ AuDbg("b%d\n", mfs->mfs_bindex);
e49925d1 34544+ au_kfree_rcu(st);
1facf9fc 34545+}
34546+
392086de 34547+static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
1facf9fc 34548+{
34549+ int err;
392086de 34550+ struct dentry *parent;
1facf9fc 34551+ struct super_block *sb;
34552+ struct au_wbr_mfs *mfs;
34553+
34554+ err = au_wbr_create_exp(dentry);
34555+ if (err >= 0)
34556+ goto out;
34557+
34558+ sb = dentry->d_sb;
392086de
AM
34559+ parent = NULL;
34560+ if (au_ftest_wbr(flags, PARENT))
34561+ parent = dget_parent(dentry);
1facf9fc 34562+ mfs = &au_sbi(sb)->si_wbr_mfs;
34563+ mutex_lock(&mfs->mfs_lock);
34564+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34565+ || mfs->mfs_bindex < 0
34566+ || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
392086de 34567+ au_mfs(dentry, parent);
1facf9fc 34568+ mutex_unlock(&mfs->mfs_lock);
34569+ err = mfs->mfs_bindex;
392086de 34570+ dput(parent);
1facf9fc 34571+
4a4d8108
AM
34572+ if (err >= 0)
34573+ err = au_wbr_nonopq(dentry, err);
34574+
4f0767ce 34575+out:
1facf9fc 34576+ AuDbg("b%d\n", err);
34577+ return err;
34578+}
34579+
34580+static int au_wbr_create_init_mfs(struct super_block *sb)
34581+{
34582+ struct au_wbr_mfs *mfs;
34583+
34584+ mfs = &au_sbi(sb)->si_wbr_mfs;
34585+ mutex_init(&mfs->mfs_lock);
34586+ mfs->mfs_jiffy = 0;
34587+ mfs->mfs_bindex = -EROFS;
34588+
34589+ return 0;
34590+}
34591+
34592+static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34593+{
34594+ mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34595+ return 0;
34596+}
34597+
34598+/* ---------------------------------------------------------------------- */
34599+
f2c43d5f
AM
34600+/* top down regardless parent, and then mfs */
34601+static int au_wbr_create_tdmfs(struct dentry *dentry,
34602+ unsigned int flags __maybe_unused)
34603+{
34604+ int err;
34605+ aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34606+ unsigned long long watermark;
34607+ struct super_block *sb;
34608+ struct au_wbr_mfs *mfs;
34609+ struct au_branch *br;
34610+ struct dentry *parent;
34611+
34612+ sb = dentry->d_sb;
34613+ mfs = &au_sbi(sb)->si_wbr_mfs;
34614+ mutex_lock(&mfs->mfs_lock);
34615+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34616+ || mfs->mfs_bindex < 0)
34617+ au_mfs(dentry, /*parent*/NULL);
34618+ watermark = mfs->mfsrr_watermark;
34619+ bmfs = mfs->mfs_bindex;
34620+ mutex_unlock(&mfs->mfs_lock);
34621+
34622+ /* another style of au_wbr_create_exp() */
34623+ bwh = au_dbwh(dentry);
34624+ parent = dget_parent(dentry);
34625+ btail = au_dbtaildir(parent);
34626+ if (bwh >= 0 && bwh < btail)
34627+ btail = bwh;
34628+
34629+ err = au_wbr_nonopq(dentry, btail);
34630+ if (unlikely(err < 0))
34631+ goto out;
34632+ btail = err;
34633+ bfound = -1;
34634+ for (bindex = 0; bindex <= btail; bindex++) {
34635+ br = au_sbr(sb, bindex);
34636+ if (au_br_rdonly(br))
34637+ continue;
34638+ if (br->br_wbr->wbr_bytes > watermark) {
34639+ bfound = bindex;
34640+ break;
34641+ }
34642+ }
34643+ err = bfound;
34644+ if (err < 0)
34645+ err = bmfs;
34646+
34647+out:
34648+ dput(parent);
34649+ AuDbg("b%d\n", err);
34650+ return err;
34651+}
34652+
34653+/* ---------------------------------------------------------------------- */
34654+
1facf9fc 34655+/* most free space and then round robin */
392086de 34656+static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
1facf9fc 34657+{
34658+ int err;
34659+ struct au_wbr_mfs *mfs;
34660+
392086de 34661+ err = au_wbr_create_mfs(dentry, flags);
1facf9fc 34662+ if (err >= 0) {
34663+ mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
dece6358 34664+ mutex_lock(&mfs->mfs_lock);
1facf9fc 34665+ if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
392086de 34666+ err = au_wbr_create_rr(dentry, flags);
dece6358 34667+ mutex_unlock(&mfs->mfs_lock);
1facf9fc 34668+ }
34669+
34670+ AuDbg("b%d\n", err);
34671+ return err;
34672+}
34673+
34674+static int au_wbr_create_init_mfsrr(struct super_block *sb)
34675+{
34676+ int err;
34677+
34678+ au_wbr_create_init_mfs(sb); /* ignore */
34679+ err = au_wbr_create_init_rr(sb);
34680+
34681+ return err;
34682+}
34683+
34684+/* ---------------------------------------------------------------------- */
34685+
34686+/* top down parent and most free space */
392086de 34687+static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
1facf9fc 34688+{
34689+ int err, e2;
34690+ unsigned long long b;
5afbbe0d 34691+ aufs_bindex_t bindex, btop, bbot;
1facf9fc 34692+ struct super_block *sb;
34693+ struct dentry *parent, *h_parent;
34694+ struct au_branch *br;
34695+
392086de 34696+ err = au_wbr_create_tdp(dentry, flags);
1facf9fc 34697+ if (unlikely(err < 0))
34698+ goto out;
34699+ parent = dget_parent(dentry);
5afbbe0d
AM
34700+ btop = au_dbtop(parent);
34701+ bbot = au_dbtaildir(parent);
34702+ if (btop == bbot)
1facf9fc 34703+ goto out_parent; /* success */
34704+
392086de 34705+ e2 = au_wbr_create_mfs(dentry, flags);
1facf9fc 34706+ if (e2 < 0)
34707+ goto out_parent; /* success */
34708+
34709+ /* when the available size is equal, select upper one */
34710+ sb = dentry->d_sb;
34711+ br = au_sbr(sb, err);
34712+ b = br->br_wbr->wbr_bytes;
34713+ AuDbg("b%d, %llu\n", err, b);
34714+
5afbbe0d 34715+ for (bindex = btop; bindex <= bbot; bindex++) {
1facf9fc 34716+ h_parent = au_h_dptr(parent, bindex);
5527c038 34717+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34718+ continue;
34719+
34720+ br = au_sbr(sb, bindex);
34721+ if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34722+ b = br->br_wbr->wbr_bytes;
34723+ err = bindex;
34724+ AuDbg("b%d, %llu\n", err, b);
34725+ }
34726+ }
34727+
4a4d8108
AM
34728+ if (err >= 0)
34729+ err = au_wbr_nonopq(dentry, err);
34730+
4f0767ce 34731+out_parent:
1facf9fc 34732+ dput(parent);
4f0767ce 34733+out:
1facf9fc 34734+ AuDbg("b%d\n", err);
34735+ return err;
34736+}
34737+
34738+/* ---------------------------------------------------------------------- */
34739+
392086de
AM
34740+/*
34741+ * - top down parent
34742+ * - most free space with parent
34743+ * - most free space round-robin regardless parent
34744+ */
34745+static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34746+{
34747+ int err;
34748+ unsigned long long watermark;
34749+ struct super_block *sb;
34750+ struct au_branch *br;
34751+ struct au_wbr_mfs *mfs;
34752+
34753+ err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34754+ if (unlikely(err < 0))
34755+ goto out;
34756+
34757+ sb = dentry->d_sb;
34758+ br = au_sbr(sb, err);
34759+ mfs = &au_sbi(sb)->si_wbr_mfs;
34760+ mutex_lock(&mfs->mfs_lock);
34761+ watermark = mfs->mfsrr_watermark;
34762+ mutex_unlock(&mfs->mfs_lock);
34763+ if (br->br_wbr->wbr_bytes < watermark)
34764+ /* regardless the parent dir */
34765+ err = au_wbr_create_mfsrr(dentry, flags);
34766+
34767+out:
34768+ AuDbg("b%d\n", err);
34769+ return err;
34770+}
34771+
34772+/* ---------------------------------------------------------------------- */
34773+
1facf9fc 34774+/* policies for copyup */
34775+
34776+/* top down parent */
34777+static int au_wbr_copyup_tdp(struct dentry *dentry)
34778+{
392086de 34779+ return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
1facf9fc 34780+}
34781+
34782+/* bottom up parent */
34783+static int au_wbr_copyup_bup(struct dentry *dentry)
34784+{
34785+ int err;
5afbbe0d 34786+ aufs_bindex_t bindex, btop;
1facf9fc 34787+ struct dentry *parent, *h_parent;
34788+ struct super_block *sb;
34789+
34790+ err = -EROFS;
34791+ sb = dentry->d_sb;
34792+ parent = dget_parent(dentry);
5afbbe0d
AM
34793+ btop = au_dbtop(parent);
34794+ for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
1facf9fc 34795+ h_parent = au_h_dptr(parent, bindex);
5527c038 34796+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34797+ continue;
34798+
34799+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34800+ err = bindex;
34801+ break;
34802+ }
34803+ }
34804+ dput(parent);
34805+
34806+ /* bottom up here */
34807+ if (unlikely(err < 0))
5afbbe0d 34808+ err = au_wbr_bu(sb, btop - 1);
1facf9fc 34809+
34810+ AuDbg("b%d\n", err);
34811+ return err;
34812+}
34813+
34814+/* bottom up */
5afbbe0d 34815+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
1facf9fc 34816+{
34817+ int err;
34818+
5afbbe0d 34819+ err = au_wbr_bu(dentry->d_sb, btop);
4a4d8108 34820+ AuDbg("b%d\n", err);
5afbbe0d 34821+ if (err > btop)
4a4d8108 34822+ err = au_wbr_nonopq(dentry, err);
1facf9fc 34823+
34824+ AuDbg("b%d\n", err);
34825+ return err;
34826+}
34827+
076b876e
AM
34828+static int au_wbr_copyup_bu(struct dentry *dentry)
34829+{
34830+ int err;
5afbbe0d 34831+ aufs_bindex_t btop;
076b876e 34832+
5afbbe0d
AM
34833+ btop = au_dbtop(dentry);
34834+ err = au_wbr_do_copyup_bu(dentry, btop);
076b876e
AM
34835+ return err;
34836+}
34837+
1facf9fc 34838+/* ---------------------------------------------------------------------- */
34839+
34840+struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34841+ [AuWbrCopyup_TDP] = {
34842+ .copyup = au_wbr_copyup_tdp
34843+ },
34844+ [AuWbrCopyup_BUP] = {
34845+ .copyup = au_wbr_copyup_bup
34846+ },
34847+ [AuWbrCopyup_BU] = {
34848+ .copyup = au_wbr_copyup_bu
34849+ }
34850+};
34851+
34852+struct au_wbr_create_operations au_wbr_create_ops[] = {
34853+ [AuWbrCreate_TDP] = {
34854+ .create = au_wbr_create_tdp
34855+ },
34856+ [AuWbrCreate_RR] = {
34857+ .create = au_wbr_create_rr,
34858+ .init = au_wbr_create_init_rr
34859+ },
34860+ [AuWbrCreate_MFS] = {
34861+ .create = au_wbr_create_mfs,
34862+ .init = au_wbr_create_init_mfs,
34863+ .fin = au_wbr_create_fin_mfs
34864+ },
34865+ [AuWbrCreate_MFSV] = {
34866+ .create = au_wbr_create_mfs,
34867+ .init = au_wbr_create_init_mfs,
34868+ .fin = au_wbr_create_fin_mfs
34869+ },
34870+ [AuWbrCreate_MFSRR] = {
34871+ .create = au_wbr_create_mfsrr,
34872+ .init = au_wbr_create_init_mfsrr,
34873+ .fin = au_wbr_create_fin_mfs
34874+ },
34875+ [AuWbrCreate_MFSRRV] = {
34876+ .create = au_wbr_create_mfsrr,
34877+ .init = au_wbr_create_init_mfsrr,
34878+ .fin = au_wbr_create_fin_mfs
34879+ },
f2c43d5f
AM
34880+ [AuWbrCreate_TDMFS] = {
34881+ .create = au_wbr_create_tdmfs,
34882+ .init = au_wbr_create_init_mfs,
34883+ .fin = au_wbr_create_fin_mfs
34884+ },
34885+ [AuWbrCreate_TDMFSV] = {
34886+ .create = au_wbr_create_tdmfs,
34887+ .init = au_wbr_create_init_mfs,
34888+ .fin = au_wbr_create_fin_mfs
34889+ },
1facf9fc 34890+ [AuWbrCreate_PMFS] = {
34891+ .create = au_wbr_create_pmfs,
34892+ .init = au_wbr_create_init_mfs,
34893+ .fin = au_wbr_create_fin_mfs
34894+ },
34895+ [AuWbrCreate_PMFSV] = {
34896+ .create = au_wbr_create_pmfs,
34897+ .init = au_wbr_create_init_mfs,
34898+ .fin = au_wbr_create_fin_mfs
392086de
AM
34899+ },
34900+ [AuWbrCreate_PMFSRR] = {
34901+ .create = au_wbr_create_pmfsrr,
34902+ .init = au_wbr_create_init_mfsrr,
34903+ .fin = au_wbr_create_fin_mfs
34904+ },
34905+ [AuWbrCreate_PMFSRRV] = {
34906+ .create = au_wbr_create_pmfsrr,
34907+ .init = au_wbr_create_init_mfsrr,
34908+ .fin = au_wbr_create_fin_mfs
1facf9fc 34909+ }
34910+};
7f207e10
AM
34911diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
34912--- /usr/share/empty/fs/aufs/whout.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 34913+++ linux/fs/aufs/whout.c 2019-01-28 14:36:12.222418047 +0100
062440b3 34914@@ -0,0 +1,1062 @@
cd7a4cd9 34915+// SPDX-License-Identifier: GPL-2.0
1facf9fc 34916+/*
b00004a5 34917+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 34918+ *
34919+ * This program, aufs is free software; you can redistribute it and/or modify
34920+ * it under the terms of the GNU General Public License as published by
34921+ * the Free Software Foundation; either version 2 of the License, or
34922+ * (at your option) any later version.
dece6358
AM
34923+ *
34924+ * This program is distributed in the hope that it will be useful,
34925+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
34926+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34927+ * GNU General Public License for more details.
34928+ *
34929+ * You should have received a copy of the GNU General Public License
523b37e3 34930+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 34931+ */
34932+
34933+/*
34934+ * whiteout for logical deletion and opaque directory
34935+ */
34936+
1facf9fc 34937+#include "aufs.h"
34938+
cd7a4cd9 34939+#define WH_MASK 0444
1facf9fc 34940+
34941+/*
34942+ * If a directory contains this file, then it is opaque. We start with the
34943+ * .wh. flag so that it is blocked by lookup.
34944+ */
0c3ec466
AM
34945+static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
34946+ sizeof(AUFS_WH_DIROPQ) - 1);
1facf9fc 34947+
34948+/*
34949+ * generate whiteout name, which is NOT terminated by NULL.
34950+ * @name: original d_name.name
34951+ * @len: original d_name.len
34952+ * @wh: whiteout qstr
34953+ * returns zero when succeeds, otherwise error.
34954+ * succeeded value as wh->name should be freed by kfree().
34955+ */
34956+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
34957+{
34958+ char *p;
34959+
34960+ if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
34961+ return -ENAMETOOLONG;
34962+
34963+ wh->len = name->len + AUFS_WH_PFX_LEN;
34964+ p = kmalloc(wh->len, GFP_NOFS);
34965+ wh->name = p;
34966+ if (p) {
34967+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
34968+ memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
34969+ /* smp_mb(); */
34970+ return 0;
34971+ }
34972+ return -ENOMEM;
34973+}
34974+
34975+/* ---------------------------------------------------------------------- */
34976+
34977+/*
34978+ * test if the @wh_name exists under @h_parent.
34979+ * @try_sio specifies the necessary of super-io.
34980+ */
076b876e 34981+int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
1facf9fc 34982+{
34983+ int err;
34984+ struct dentry *wh_dentry;
1facf9fc 34985+
1facf9fc 34986+ if (!try_sio)
b4510431 34987+ wh_dentry = vfsub_lkup_one(wh_name, h_parent);
1facf9fc 34988+ else
076b876e 34989+ wh_dentry = au_sio_lkup_one(wh_name, h_parent);
1facf9fc 34990+ err = PTR_ERR(wh_dentry);
2000de60
JR
34991+ if (IS_ERR(wh_dentry)) {
34992+ if (err == -ENAMETOOLONG)
34993+ err = 0;
1facf9fc 34994+ goto out;
2000de60 34995+ }
1facf9fc 34996+
34997+ err = 0;
5527c038 34998+ if (d_is_negative(wh_dentry))
1facf9fc 34999+ goto out_wh; /* success */
35000+
35001+ err = 1;
7e9cd9fe 35002+ if (d_is_reg(wh_dentry))
1facf9fc 35003+ goto out_wh; /* success */
35004+
35005+ err = -EIO;
523b37e3 35006+ AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
5527c038 35007+ wh_dentry, d_inode(wh_dentry)->i_mode);
1facf9fc 35008+
4f0767ce 35009+out_wh:
1facf9fc 35010+ dput(wh_dentry);
4f0767ce 35011+out:
1facf9fc 35012+ return err;
35013+}
35014+
35015+/*
35016+ * test if the @h_dentry sets opaque or not.
35017+ */
076b876e 35018+int au_diropq_test(struct dentry *h_dentry)
1facf9fc 35019+{
35020+ int err;
35021+ struct inode *h_dir;
35022+
5527c038 35023+ h_dir = d_inode(h_dentry);
076b876e 35024+ err = au_wh_test(h_dentry, &diropq_name,
1facf9fc 35025+ au_test_h_perm_sio(h_dir, MAY_EXEC));
35026+ return err;
35027+}
35028+
35029+/*
35030+ * returns a negative dentry whose name is unique and temporary.
35031+ */
35032+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35033+ struct qstr *prefix)
35034+{
1facf9fc 35035+ struct dentry *dentry;
35036+ int i;
027c5e7a 35037+ char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
4a4d8108 35038+ *name, *p;
027c5e7a 35039+ /* strict atomic_t is unnecessary here */
1facf9fc 35040+ static unsigned short cnt;
35041+ struct qstr qs;
35042+
4a4d8108
AM
35043+ BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
35044+
1facf9fc 35045+ name = defname;
027c5e7a
AM
35046+ qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
35047+ if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
1facf9fc 35048+ dentry = ERR_PTR(-ENAMETOOLONG);
4a4d8108 35049+ if (unlikely(qs.len > NAME_MAX))
1facf9fc 35050+ goto out;
35051+ dentry = ERR_PTR(-ENOMEM);
35052+ name = kmalloc(qs.len + 1, GFP_NOFS);
35053+ if (unlikely(!name))
35054+ goto out;
35055+ }
35056+
35057+ /* doubly whiteout-ed */
35058+ memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
35059+ p = name + AUFS_WH_PFX_LEN * 2;
35060+ memcpy(p, prefix->name, prefix->len);
35061+ p += prefix->len;
35062+ *p++ = '.';
4a4d8108 35063+ AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
1facf9fc 35064+
35065+ qs.name = name;
35066+ for (i = 0; i < 3; i++) {
b752ccd1 35067+ sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
076b876e 35068+ dentry = au_sio_lkup_one(&qs, h_parent);
5527c038 35069+ if (IS_ERR(dentry) || d_is_negative(dentry))
1facf9fc 35070+ goto out_name;
35071+ dput(dentry);
35072+ }
0c3ec466 35073+ /* pr_warn("could not get random name\n"); */
1facf9fc 35074+ dentry = ERR_PTR(-EEXIST);
35075+ AuDbg("%.*s\n", AuLNPair(&qs));
35076+ BUG();
35077+
4f0767ce 35078+out_name:
1facf9fc 35079+ if (name != defname)
e49925d1 35080+ au_kfree_try_rcu(name);
4f0767ce 35081+out:
4a4d8108 35082+ AuTraceErrPtr(dentry);
1facf9fc 35083+ return dentry;
1facf9fc 35084+}
35085+
35086+/*
35087+ * rename the @h_dentry on @br to the whiteouted temporary name.
35088+ */
35089+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
35090+{
35091+ int err;
35092+ struct path h_path = {
86dc4139 35093+ .mnt = au_br_mnt(br)
1facf9fc 35094+ };
523b37e3 35095+ struct inode *h_dir, *delegated;
1facf9fc 35096+ struct dentry *h_parent;
35097+
35098+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 35099+ h_dir = d_inode(h_parent);
1facf9fc 35100+ IMustLock(h_dir);
35101+
35102+ h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
35103+ err = PTR_ERR(h_path.dentry);
35104+ if (IS_ERR(h_path.dentry))
35105+ goto out;
35106+
35107+ /* under the same dir, no need to lock_rename() */
523b37e3 35108+ delegated = NULL;
f2c43d5f
AM
35109+ err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
35110+ /*flags*/0);
1facf9fc 35111+ AuTraceErr(err);
523b37e3
AM
35112+ if (unlikely(err == -EWOULDBLOCK)) {
35113+ pr_warn("cannot retry for NFSv4 delegation"
35114+ " for an internal rename\n");
35115+ iput(delegated);
35116+ }
1facf9fc 35117+ dput(h_path.dentry);
35118+
4f0767ce 35119+out:
4a4d8108 35120+ AuTraceErr(err);
1facf9fc 35121+ return err;
35122+}
35123+
35124+/* ---------------------------------------------------------------------- */
35125+/*
35126+ * functions for removing a whiteout
35127+ */
35128+
35129+static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
35130+{
523b37e3
AM
35131+ int err, force;
35132+ struct inode *delegated;
1facf9fc 35133+
35134+ /*
35135+ * forces superio when the dir has a sticky bit.
35136+ * this may be a violation of unix fs semantics.
35137+ */
35138+ force = (h_dir->i_mode & S_ISVTX)
5527c038 35139+ && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
523b37e3
AM
35140+ delegated = NULL;
35141+ err = vfsub_unlink(h_dir, h_path, &delegated, force);
35142+ if (unlikely(err == -EWOULDBLOCK)) {
35143+ pr_warn("cannot retry for NFSv4 delegation"
35144+ " for an internal unlink\n");
35145+ iput(delegated);
35146+ }
35147+ return err;
1facf9fc 35148+}
35149+
35150+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35151+ struct dentry *dentry)
35152+{
35153+ int err;
35154+
35155+ err = do_unlink_wh(h_dir, h_path);
35156+ if (!err && dentry)
35157+ au_set_dbwh(dentry, -1);
35158+
35159+ return err;
35160+}
35161+
35162+static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
35163+ struct au_branch *br)
35164+{
35165+ int err;
35166+ struct path h_path = {
86dc4139 35167+ .mnt = au_br_mnt(br)
1facf9fc 35168+ };
35169+
35170+ err = 0;
b4510431 35171+ h_path.dentry = vfsub_lkup_one(wh, h_parent);
1facf9fc 35172+ if (IS_ERR(h_path.dentry))
35173+ err = PTR_ERR(h_path.dentry);
35174+ else {
5527c038
JR
35175+ if (d_is_reg(h_path.dentry))
35176+ err = do_unlink_wh(d_inode(h_parent), &h_path);
1facf9fc 35177+ dput(h_path.dentry);
35178+ }
35179+
35180+ return err;
35181+}
35182+
35183+/* ---------------------------------------------------------------------- */
35184+/*
35185+ * initialize/clean whiteout for a branch
35186+ */
35187+
35188+static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35189+ const int isdir)
35190+{
35191+ int err;
523b37e3 35192+ struct inode *delegated;
1facf9fc 35193+
5527c038 35194+ if (d_is_negative(whpath->dentry))
1facf9fc 35195+ return;
35196+
86dc4139
AM
35197+ if (isdir)
35198+ err = vfsub_rmdir(h_dir, whpath);
523b37e3
AM
35199+ else {
35200+ delegated = NULL;
35201+ err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35202+ if (unlikely(err == -EWOULDBLOCK)) {
35203+ pr_warn("cannot retry for NFSv4 delegation"
35204+ " for an internal unlink\n");
35205+ iput(delegated);
35206+ }
35207+ }
1facf9fc 35208+ if (unlikely(err))
523b37e3
AM
35209+ pr_warn("failed removing %pd (%d), ignored.\n",
35210+ whpath->dentry, err);
1facf9fc 35211+}
35212+
35213+static int test_linkable(struct dentry *h_root)
35214+{
5527c038 35215+ struct inode *h_dir = d_inode(h_root);
1facf9fc 35216+
35217+ if (h_dir->i_op->link)
35218+ return 0;
35219+
523b37e3
AM
35220+ pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35221+ h_root, au_sbtype(h_root->d_sb));
1facf9fc 35222+ return -ENOSYS;
35223+}
35224+
35225+/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35226+static int au_whdir(struct inode *h_dir, struct path *path)
35227+{
35228+ int err;
35229+
35230+ err = -EEXIST;
5527c038 35231+ if (d_is_negative(path->dentry)) {
cd7a4cd9 35232+ int mode = 0700;
1facf9fc 35233+
35234+ if (au_test_nfs(path->dentry->d_sb))
cd7a4cd9 35235+ mode |= 0111;
86dc4139 35236+ err = vfsub_mkdir(h_dir, path, mode);
2000de60 35237+ } else if (d_is_dir(path->dentry))
1facf9fc 35238+ err = 0;
35239+ else
523b37e3 35240+ pr_err("unknown %pd exists\n", path->dentry);
1facf9fc 35241+
35242+ return err;
35243+}
35244+
35245+struct au_wh_base {
35246+ const struct qstr *name;
35247+ struct dentry *dentry;
35248+};
35249+
35250+static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35251+ struct path *h_path)
35252+{
35253+ h_path->dentry = base[AuBrWh_BASE].dentry;
35254+ au_wh_clean(h_dir, h_path, /*isdir*/0);
35255+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35256+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35257+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35258+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35259+}
35260+
35261+/*
35262+ * returns tri-state,
c1595e42 35263+ * minus: error, caller should print the message
acd2b654 35264+ * zero: success
c1595e42 35265+ * plus: error, caller should NOT print the message
1facf9fc 35266+ */
35267+static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35268+ int do_plink, struct au_wh_base base[],
35269+ struct path *h_path)
35270+{
35271+ int err;
35272+ struct inode *h_dir;
35273+
5527c038 35274+ h_dir = d_inode(h_root);
1facf9fc 35275+ h_path->dentry = base[AuBrWh_BASE].dentry;
35276+ au_wh_clean(h_dir, h_path, /*isdir*/0);
35277+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35278+ if (do_plink) {
35279+ err = test_linkable(h_root);
35280+ if (unlikely(err)) {
35281+ err = 1;
35282+ goto out;
35283+ }
35284+
35285+ err = au_whdir(h_dir, h_path);
35286+ if (unlikely(err))
35287+ goto out;
35288+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35289+ } else
35290+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35291+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35292+ err = au_whdir(h_dir, h_path);
35293+ if (unlikely(err))
35294+ goto out;
35295+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35296+
4f0767ce 35297+out:
1facf9fc 35298+ return err;
35299+}
35300+
35301+/*
35302+ * for the moment, aufs supports the branch filesystem which does not support
35303+ * link(2). testing on FAT which does not support i_op->setattr() fully either,
35304+ * copyup failed. finally, such filesystem will not be used as the writable
35305+ * branch.
35306+ *
35307+ * returns tri-state, see above.
35308+ */
35309+static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35310+ int do_plink, struct au_wh_base base[],
35311+ struct path *h_path)
35312+{
35313+ int err;
35314+ struct inode *h_dir;
35315+
1308ab2a 35316+ WbrWhMustWriteLock(wbr);
35317+
1facf9fc 35318+ err = test_linkable(h_root);
35319+ if (unlikely(err)) {
35320+ err = 1;
35321+ goto out;
35322+ }
35323+
35324+ /*
35325+ * todo: should this create be done in /sbin/mount.aufs helper?
35326+ */
35327+ err = -EEXIST;
5527c038
JR
35328+ h_dir = d_inode(h_root);
35329+ if (d_is_negative(base[AuBrWh_BASE].dentry)) {
86dc4139
AM
35330+ h_path->dentry = base[AuBrWh_BASE].dentry;
35331+ err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
7e9cd9fe 35332+ } else if (d_is_reg(base[AuBrWh_BASE].dentry))
1facf9fc 35333+ err = 0;
35334+ else
523b37e3 35335+ pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
1facf9fc 35336+ if (unlikely(err))
35337+ goto out;
35338+
35339+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35340+ if (do_plink) {
35341+ err = au_whdir(h_dir, h_path);
35342+ if (unlikely(err))
35343+ goto out;
35344+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35345+ } else
35346+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35347+ wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35348+
35349+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35350+ err = au_whdir(h_dir, h_path);
35351+ if (unlikely(err))
35352+ goto out;
35353+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35354+
4f0767ce 35355+out:
1facf9fc 35356+ return err;
35357+}
35358+
35359+/*
35360+ * initialize the whiteout base file/dir for @br.
35361+ */
86dc4139 35362+int au_wh_init(struct au_branch *br, struct super_block *sb)
1facf9fc 35363+{
35364+ int err, i;
35365+ const unsigned char do_plink
35366+ = !!au_opt_test(au_mntflags(sb), PLINK);
1facf9fc 35367+ struct inode *h_dir;
86dc4139
AM
35368+ struct path path = br->br_path;
35369+ struct dentry *h_root = path.dentry;
1facf9fc 35370+ struct au_wbr *wbr = br->br_wbr;
35371+ static const struct qstr base_name[] = {
0c3ec466
AM
35372+ [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35373+ sizeof(AUFS_BASE_NAME) - 1),
35374+ [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35375+ sizeof(AUFS_PLINKDIR_NAME) - 1),
35376+ [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35377+ sizeof(AUFS_ORPHDIR_NAME) - 1)
1facf9fc 35378+ };
35379+ struct au_wh_base base[] = {
35380+ [AuBrWh_BASE] = {
35381+ .name = base_name + AuBrWh_BASE,
35382+ .dentry = NULL
35383+ },
35384+ [AuBrWh_PLINK] = {
35385+ .name = base_name + AuBrWh_PLINK,
35386+ .dentry = NULL
35387+ },
35388+ [AuBrWh_ORPH] = {
35389+ .name = base_name + AuBrWh_ORPH,
35390+ .dentry = NULL
35391+ }
35392+ };
35393+
1308ab2a 35394+ if (wbr)
35395+ WbrWhMustWriteLock(wbr);
1facf9fc 35396+
1facf9fc 35397+ for (i = 0; i < AuBrWh_Last; i++) {
35398+ /* doubly whiteouted */
35399+ struct dentry *d;
35400+
35401+ d = au_wh_lkup(h_root, (void *)base[i].name, br);
35402+ err = PTR_ERR(d);
35403+ if (IS_ERR(d))
35404+ goto out;
35405+
35406+ base[i].dentry = d;
35407+ AuDebugOn(wbr
35408+ && wbr->wbr_wh[i]
35409+ && wbr->wbr_wh[i] != base[i].dentry);
35410+ }
35411+
35412+ if (wbr)
35413+ for (i = 0; i < AuBrWh_Last; i++) {
35414+ dput(wbr->wbr_wh[i]);
35415+ wbr->wbr_wh[i] = NULL;
35416+ }
35417+
35418+ err = 0;
1e00d052 35419+ if (!au_br_writable(br->br_perm)) {
5527c038 35420+ h_dir = d_inode(h_root);
1facf9fc 35421+ au_wh_init_ro(h_dir, base, &path);
1e00d052 35422+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 35423+ err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35424+ if (err > 0)
35425+ goto out;
35426+ else if (err)
35427+ goto out_err;
1e00d052 35428+ } else {
1facf9fc 35429+ err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35430+ if (err > 0)
35431+ goto out;
35432+ else if (err)
35433+ goto out_err;
1facf9fc 35434+ }
35435+ goto out; /* success */
35436+
4f0767ce 35437+out_err:
523b37e3
AM
35438+ pr_err("an error(%d) on the writable branch %pd(%s)\n",
35439+ err, h_root, au_sbtype(h_root->d_sb));
4f0767ce 35440+out:
1facf9fc 35441+ for (i = 0; i < AuBrWh_Last; i++)
35442+ dput(base[i].dentry);
35443+ return err;
35444+}
35445+
35446+/* ---------------------------------------------------------------------- */
35447+/*
35448+ * whiteouts are all hard-linked usually.
35449+ * when its link count reaches a ceiling, we create a new whiteout base
35450+ * asynchronously.
35451+ */
35452+
35453+struct reinit_br_wh {
35454+ struct super_block *sb;
35455+ struct au_branch *br;
35456+};
35457+
35458+static void reinit_br_wh(void *arg)
35459+{
35460+ int err;
35461+ aufs_bindex_t bindex;
35462+ struct path h_path;
35463+ struct reinit_br_wh *a = arg;
35464+ struct au_wbr *wbr;
523b37e3 35465+ struct inode *dir, *delegated;
1facf9fc 35466+ struct dentry *h_root;
35467+ struct au_hinode *hdir;
35468+
35469+ err = 0;
35470+ wbr = a->br->br_wbr;
35471+ /* big aufs lock */
35472+ si_noflush_write_lock(a->sb);
35473+ if (!au_br_writable(a->br->br_perm))
35474+ goto out;
35475+ bindex = au_br_index(a->sb, a->br->br_id);
35476+ if (unlikely(bindex < 0))
35477+ goto out;
35478+
1308ab2a 35479+ di_read_lock_parent(a->sb->s_root, AuLock_IR);
5527c038 35480+ dir = d_inode(a->sb->s_root);
1facf9fc 35481+ hdir = au_hi(dir, bindex);
35482+ h_root = au_h_dptr(a->sb->s_root, bindex);
86dc4139 35483+ AuDebugOn(h_root != au_br_dentry(a->br));
1facf9fc 35484+
5afbbe0d 35485+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 35486+ wbr_wh_write_lock(wbr);
35487+ err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35488+ h_root, a->br);
35489+ if (!err) {
86dc4139
AM
35490+ h_path.dentry = wbr->wbr_whbase;
35491+ h_path.mnt = au_br_mnt(a->br);
523b37e3
AM
35492+ delegated = NULL;
35493+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35494+ /*force*/0);
35495+ if (unlikely(err == -EWOULDBLOCK)) {
35496+ pr_warn("cannot retry for NFSv4 delegation"
35497+ " for an internal unlink\n");
35498+ iput(delegated);
35499+ }
1facf9fc 35500+ } else {
523b37e3 35501+ pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
1facf9fc 35502+ err = 0;
35503+ }
35504+ dput(wbr->wbr_whbase);
35505+ wbr->wbr_whbase = NULL;
35506+ if (!err)
86dc4139 35507+ err = au_wh_init(a->br, a->sb);
1facf9fc 35508+ wbr_wh_write_unlock(wbr);
5afbbe0d 35509+ au_hn_inode_unlock(hdir);
1308ab2a 35510+ di_read_unlock(a->sb->s_root, AuLock_IR);
076b876e
AM
35511+ if (!err)
35512+ au_fhsm_wrote(a->sb, bindex, /*force*/0);
1facf9fc 35513+
4f0767ce 35514+out:
1facf9fc 35515+ if (wbr)
35516+ atomic_dec(&wbr->wbr_wh_running);
acd2b654 35517+ au_lcnt_dec(&a->br->br_count);
1facf9fc 35518+ si_write_unlock(a->sb);
027c5e7a 35519+ au_nwt_done(&au_sbi(a->sb)->si_nowait);
e49925d1 35520+ au_kfree_rcu(a);
1facf9fc 35521+ if (unlikely(err))
35522+ AuIOErr("err %d\n", err);
35523+}
35524+
35525+static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35526+{
35527+ int do_dec, wkq_err;
35528+ struct reinit_br_wh *arg;
35529+
35530+ do_dec = 1;
35531+ if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35532+ goto out;
35533+
35534+ /* ignore ENOMEM */
35535+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
35536+ if (arg) {
35537+ /*
35538+ * dec(wh_running), kfree(arg) and dec(br_count)
35539+ * in reinit function
35540+ */
35541+ arg->sb = sb;
35542+ arg->br = br;
acd2b654 35543+ au_lcnt_inc(&br->br_count);
53392da6 35544+ wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
1facf9fc 35545+ if (unlikely(wkq_err)) {
35546+ atomic_dec(&br->br_wbr->wbr_wh_running);
acd2b654 35547+ au_lcnt_dec(&br->br_count);
e49925d1 35548+ au_kfree_rcu(arg);
1facf9fc 35549+ }
35550+ do_dec = 0;
35551+ }
35552+
4f0767ce 35553+out:
1facf9fc 35554+ if (do_dec)
35555+ atomic_dec(&br->br_wbr->wbr_wh_running);
35556+}
35557+
35558+/* ---------------------------------------------------------------------- */
35559+
35560+/*
35561+ * create the whiteout @wh.
35562+ */
35563+static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35564+ struct dentry *wh)
35565+{
35566+ int err;
35567+ struct path h_path = {
35568+ .dentry = wh
35569+ };
35570+ struct au_branch *br;
35571+ struct au_wbr *wbr;
35572+ struct dentry *h_parent;
523b37e3 35573+ struct inode *h_dir, *delegated;
1facf9fc 35574+
35575+ h_parent = wh->d_parent; /* dir inode is locked */
5527c038 35576+ h_dir = d_inode(h_parent);
1facf9fc 35577+ IMustLock(h_dir);
35578+
35579+ br = au_sbr(sb, bindex);
86dc4139 35580+ h_path.mnt = au_br_mnt(br);
1facf9fc 35581+ wbr = br->br_wbr;
35582+ wbr_wh_read_lock(wbr);
35583+ if (wbr->wbr_whbase) {
523b37e3
AM
35584+ delegated = NULL;
35585+ err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35586+ if (unlikely(err == -EWOULDBLOCK)) {
35587+ pr_warn("cannot retry for NFSv4 delegation"
35588+ " for an internal link\n");
35589+ iput(delegated);
35590+ }
1facf9fc 35591+ if (!err || err != -EMLINK)
35592+ goto out;
35593+
35594+ /* link count full. re-initialize br_whbase. */
35595+ kick_reinit_br_wh(sb, br);
35596+ }
35597+
35598+ /* return this error in this context */
b4510431 35599+ err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
076b876e
AM
35600+ if (!err)
35601+ au_fhsm_wrote(sb, bindex, /*force*/0);
1facf9fc 35602+
4f0767ce 35603+out:
1facf9fc 35604+ wbr_wh_read_unlock(wbr);
35605+ return err;
35606+}
35607+
35608+/* ---------------------------------------------------------------------- */
35609+
35610+/*
35611+ * create or remove the diropq.
35612+ */
35613+static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35614+ unsigned int flags)
35615+{
35616+ struct dentry *opq_dentry, *h_dentry;
35617+ struct super_block *sb;
35618+ struct au_branch *br;
35619+ int err;
35620+
35621+ sb = dentry->d_sb;
35622+ br = au_sbr(sb, bindex);
35623+ h_dentry = au_h_dptr(dentry, bindex);
b4510431 35624+ opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
1facf9fc 35625+ if (IS_ERR(opq_dentry))
35626+ goto out;
35627+
35628+ if (au_ftest_diropq(flags, CREATE)) {
35629+ err = link_or_create_wh(sb, bindex, opq_dentry);
35630+ if (!err) {
35631+ au_set_dbdiropq(dentry, bindex);
35632+ goto out; /* success */
35633+ }
35634+ } else {
35635+ struct path tmp = {
35636+ .dentry = opq_dentry,
86dc4139 35637+ .mnt = au_br_mnt(br)
1facf9fc 35638+ };
5527c038 35639+ err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
1facf9fc 35640+ if (!err)
35641+ au_set_dbdiropq(dentry, -1);
35642+ }
35643+ dput(opq_dentry);
35644+ opq_dentry = ERR_PTR(err);
35645+
4f0767ce 35646+out:
1facf9fc 35647+ return opq_dentry;
35648+}
35649+
35650+struct do_diropq_args {
35651+ struct dentry **errp;
35652+ struct dentry *dentry;
35653+ aufs_bindex_t bindex;
35654+ unsigned int flags;
35655+};
35656+
35657+static void call_do_diropq(void *args)
35658+{
35659+ struct do_diropq_args *a = args;
35660+ *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35661+}
35662+
35663+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35664+ unsigned int flags)
35665+{
35666+ struct dentry *diropq, *h_dentry;
35667+
35668+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 35669+ if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
1facf9fc 35670+ diropq = do_diropq(dentry, bindex, flags);
35671+ else {
35672+ int wkq_err;
35673+ struct do_diropq_args args = {
35674+ .errp = &diropq,
35675+ .dentry = dentry,
35676+ .bindex = bindex,
35677+ .flags = flags
35678+ };
35679+
35680+ wkq_err = au_wkq_wait(call_do_diropq, &args);
35681+ if (unlikely(wkq_err))
35682+ diropq = ERR_PTR(wkq_err);
35683+ }
35684+
35685+ return diropq;
35686+}
35687+
35688+/* ---------------------------------------------------------------------- */
35689+
35690+/*
35691+ * lookup whiteout dentry.
35692+ * @h_parent: lower parent dentry which must exist and be locked
35693+ * @base_name: name of dentry which will be whiteouted
35694+ * returns dentry for whiteout.
35695+ */
35696+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35697+ struct au_branch *br)
35698+{
35699+ int err;
35700+ struct qstr wh_name;
35701+ struct dentry *wh_dentry;
35702+
35703+ err = au_wh_name_alloc(&wh_name, base_name);
35704+ wh_dentry = ERR_PTR(err);
35705+ if (!err) {
b4510431 35706+ wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
e49925d1 35707+ au_kfree_try_rcu(wh_name.name);
1facf9fc 35708+ }
35709+ return wh_dentry;
35710+}
35711+
35712+/*
35713+ * link/create a whiteout for @dentry on @bindex.
35714+ */
35715+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35716+ struct dentry *h_parent)
35717+{
35718+ struct dentry *wh_dentry;
35719+ struct super_block *sb;
35720+ int err;
35721+
35722+ sb = dentry->d_sb;
35723+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
5527c038 35724+ if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
1facf9fc 35725+ err = link_or_create_wh(sb, bindex, wh_dentry);
076b876e 35726+ if (!err) {
1facf9fc 35727+ au_set_dbwh(dentry, bindex);
076b876e
AM
35728+ au_fhsm_wrote(sb, bindex, /*force*/0);
35729+ } else {
1facf9fc 35730+ dput(wh_dentry);
35731+ wh_dentry = ERR_PTR(err);
35732+ }
35733+ }
35734+
35735+ return wh_dentry;
35736+}
35737+
35738+/* ---------------------------------------------------------------------- */
35739+
35740+/* Delete all whiteouts in this directory on branch bindex. */
35741+static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
35742+ aufs_bindex_t bindex, struct au_branch *br)
35743+{
35744+ int err;
35745+ unsigned long ul, n;
35746+ struct qstr wh_name;
35747+ char *p;
35748+ struct hlist_head *head;
c06a8ce3 35749+ struct au_vdir_wh *pos;
1facf9fc 35750+ struct au_vdir_destr *str;
35751+
35752+ err = -ENOMEM;
537831f9 35753+ p = (void *)__get_free_page(GFP_NOFS);
1facf9fc 35754+ wh_name.name = p;
35755+ if (unlikely(!wh_name.name))
35756+ goto out;
35757+
35758+ err = 0;
35759+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35760+ p += AUFS_WH_PFX_LEN;
35761+ n = whlist->nh_num;
35762+ head = whlist->nh_head;
35763+ for (ul = 0; !err && ul < n; ul++, head++) {
c06a8ce3
AM
35764+ hlist_for_each_entry(pos, head, wh_hash) {
35765+ if (pos->wh_bindex != bindex)
1facf9fc 35766+ continue;
35767+
c06a8ce3 35768+ str = &pos->wh_str;
1facf9fc 35769+ if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35770+ memcpy(p, str->name, str->len);
35771+ wh_name.len = AUFS_WH_PFX_LEN + str->len;
35772+ err = unlink_wh_name(h_dentry, &wh_name, br);
35773+ if (!err)
35774+ continue;
35775+ break;
35776+ }
35777+ AuIOErr("whiteout name too long %.*s\n",
35778+ str->len, str->name);
35779+ err = -EIO;
35780+ break;
35781+ }
35782+ }
1c60b727 35783+ free_page((unsigned long)wh_name.name);
1facf9fc 35784+
4f0767ce 35785+out:
1facf9fc 35786+ return err;
35787+}
35788+
35789+struct del_wh_children_args {
35790+ int *errp;
35791+ struct dentry *h_dentry;
1308ab2a 35792+ struct au_nhash *whlist;
1facf9fc 35793+ aufs_bindex_t bindex;
35794+ struct au_branch *br;
35795+};
35796+
35797+static void call_del_wh_children(void *args)
35798+{
35799+ struct del_wh_children_args *a = args;
1308ab2a 35800+ *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
1facf9fc 35801+}
35802+
35803+/* ---------------------------------------------------------------------- */
35804+
35805+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35806+{
35807+ struct au_whtmp_rmdir *whtmp;
dece6358 35808+ int err;
1308ab2a 35809+ unsigned int rdhash;
dece6358
AM
35810+
35811+ SiMustAnyLock(sb);
1facf9fc 35812+
be52b249 35813+ whtmp = kzalloc(sizeof(*whtmp), gfp);
dece6358
AM
35814+ if (unlikely(!whtmp)) {
35815+ whtmp = ERR_PTR(-ENOMEM);
1facf9fc 35816+ goto out;
dece6358 35817+ }
1facf9fc 35818+
1308ab2a 35819+ /* no estimation for dir size */
35820+ rdhash = au_sbi(sb)->si_rdhash;
35821+ if (!rdhash)
35822+ rdhash = AUFS_RDHASH_DEF;
35823+ err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35824+ if (unlikely(err)) {
e49925d1 35825+ au_kfree_rcu(whtmp);
1308ab2a 35826+ whtmp = ERR_PTR(err);
35827+ }
dece6358 35828+
4f0767ce 35829+out:
dece6358 35830+ return whtmp;
1facf9fc 35831+}
35832+
35833+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35834+{
027c5e7a 35835+ if (whtmp->br)
acd2b654 35836+ au_lcnt_dec(&whtmp->br->br_count);
1facf9fc 35837+ dput(whtmp->wh_dentry);
35838+ iput(whtmp->dir);
dece6358 35839+ au_nhash_wh_free(&whtmp->whlist);
e49925d1 35840+ au_kfree_rcu(whtmp);
1facf9fc 35841+}
35842+
35843+/*
35844+ * rmdir the whiteouted temporary named dir @h_dentry.
35845+ * @whlist: whiteouted children.
35846+ */
35847+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35848+ struct dentry *wh_dentry, struct au_nhash *whlist)
35849+{
35850+ int err;
2000de60 35851+ unsigned int h_nlink;
1facf9fc 35852+ struct path h_tmp;
35853+ struct inode *wh_inode, *h_dir;
35854+ struct au_branch *br;
35855+
5527c038 35856+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
1facf9fc 35857+ IMustLock(h_dir);
35858+
35859+ br = au_sbr(dir->i_sb, bindex);
5527c038 35860+ wh_inode = d_inode(wh_dentry);
febd17d6 35861+ inode_lock_nested(wh_inode, AuLsc_I_CHILD);
1facf9fc 35862+
35863+ /*
35864+ * someone else might change some whiteouts while we were sleeping.
35865+ * it means this whlist may have an obsoleted entry.
35866+ */
35867+ if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
35868+ err = del_wh_children(wh_dentry, whlist, bindex, br);
35869+ else {
35870+ int wkq_err;
35871+ struct del_wh_children_args args = {
35872+ .errp = &err,
35873+ .h_dentry = wh_dentry,
1308ab2a 35874+ .whlist = whlist,
1facf9fc 35875+ .bindex = bindex,
35876+ .br = br
35877+ };
35878+
35879+ wkq_err = au_wkq_wait(call_del_wh_children, &args);
35880+ if (unlikely(wkq_err))
35881+ err = wkq_err;
35882+ }
febd17d6 35883+ inode_unlock(wh_inode);
1facf9fc 35884+
35885+ if (!err) {
35886+ h_tmp.dentry = wh_dentry;
86dc4139 35887+ h_tmp.mnt = au_br_mnt(br);
2000de60 35888+ h_nlink = h_dir->i_nlink;
1facf9fc 35889+ err = vfsub_rmdir(h_dir, &h_tmp);
2000de60
JR
35890+ /* some fs doesn't change the parent nlink in some cases */
35891+ h_nlink -= h_dir->i_nlink;
1facf9fc 35892+ }
35893+
35894+ if (!err) {
5afbbe0d 35895+ if (au_ibtop(dir) == bindex) {
7f207e10 35896+ /* todo: dir->i_mutex is necessary */
1facf9fc 35897+ au_cpup_attr_timesizes(dir);
2000de60
JR
35898+ if (h_nlink)
35899+ vfsub_drop_nlink(dir);
1facf9fc 35900+ }
35901+ return 0; /* success */
35902+ }
35903+
523b37e3 35904+ pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
1facf9fc 35905+ return err;
35906+}
35907+
35908+static void call_rmdir_whtmp(void *args)
35909+{
35910+ int err;
e49829fe 35911+ aufs_bindex_t bindex;
1facf9fc 35912+ struct au_whtmp_rmdir *a = args;
35913+ struct super_block *sb;
35914+ struct dentry *h_parent;
35915+ struct inode *h_dir;
1facf9fc 35916+ struct au_hinode *hdir;
35917+
35918+ /* rmdir by nfsd may cause deadlock with this i_mutex */
febd17d6 35919+ /* inode_lock(a->dir); */
e49829fe 35920+ err = -EROFS;
1facf9fc 35921+ sb = a->dir->i_sb;
e49829fe
JR
35922+ si_read_lock(sb, !AuLock_FLUSH);
35923+ if (!au_br_writable(a->br->br_perm))
35924+ goto out;
35925+ bindex = au_br_index(sb, a->br->br_id);
35926+ if (unlikely(bindex < 0))
1facf9fc 35927+ goto out;
35928+
35929+ err = -EIO;
1facf9fc 35930+ ii_write_lock_parent(a->dir);
35931+ h_parent = dget_parent(a->wh_dentry);
5527c038 35932+ h_dir = d_inode(h_parent);
e49829fe 35933+ hdir = au_hi(a->dir, bindex);
86dc4139
AM
35934+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
35935+ if (unlikely(err))
35936+ goto out_mnt;
5afbbe0d 35937+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
e49829fe
JR
35938+ err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
35939+ a->br);
86dc4139
AM
35940+ if (!err)
35941+ err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
5afbbe0d 35942+ au_hn_inode_unlock(hdir);
86dc4139
AM
35943+ vfsub_mnt_drop_write(au_br_mnt(a->br));
35944+
35945+out_mnt:
1facf9fc 35946+ dput(h_parent);
35947+ ii_write_unlock(a->dir);
4f0767ce 35948+out:
febd17d6 35949+ /* inode_unlock(a->dir); */
1facf9fc 35950+ au_whtmp_rmdir_free(a);
027c5e7a
AM
35951+ si_read_unlock(sb);
35952+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 35953+ if (unlikely(err))
35954+ AuIOErr("err %d\n", err);
35955+}
35956+
35957+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35958+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
35959+{
35960+ int wkq_err;
e49829fe 35961+ struct super_block *sb;
1facf9fc 35962+
35963+ IMustLock(dir);
35964+
35965+ /* all post-process will be done in do_rmdir_whtmp(). */
e49829fe 35966+ sb = dir->i_sb;
1facf9fc 35967+ args->dir = au_igrab(dir);
e49829fe 35968+ args->br = au_sbr(sb, bindex);
acd2b654 35969+ au_lcnt_inc(&args->br->br_count);
1facf9fc 35970+ args->wh_dentry = dget(wh_dentry);
53392da6 35971+ wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
1facf9fc 35972+ if (unlikely(wkq_err)) {
523b37e3 35973+ pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
1facf9fc 35974+ au_whtmp_rmdir_free(args);
35975+ }
35976+}
7f207e10
AM
35977diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
35978--- /usr/share/empty/fs/aufs/whout.h 1970-01-01 01:00:00.000000000 +0100
e49925d1 35979+++ linux/fs/aufs/whout.h 2019-01-28 14:36:12.222418047 +0100
062440b3
AM
35980@@ -0,0 +1,86 @@
35981+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 35982+/*
b00004a5 35983+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 35984+ *
35985+ * This program, aufs is free software; you can redistribute it and/or modify
35986+ * it under the terms of the GNU General Public License as published by
35987+ * the Free Software Foundation; either version 2 of the License, or
35988+ * (at your option) any later version.
dece6358
AM
35989+ *
35990+ * This program is distributed in the hope that it will be useful,
35991+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
35992+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35993+ * GNU General Public License for more details.
35994+ *
35995+ * You should have received a copy of the GNU General Public License
523b37e3 35996+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 35997+ */
35998+
35999+/*
36000+ * whiteout for logical deletion and opaque directory
36001+ */
36002+
36003+#ifndef __AUFS_WHOUT_H__
36004+#define __AUFS_WHOUT_H__
36005+
36006+#ifdef __KERNEL__
36007+
1facf9fc 36008+#include "dir.h"
36009+
36010+/* whout.c */
36011+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
076b876e
AM
36012+int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
36013+int au_diropq_test(struct dentry *h_dentry);
3c1bdaff 36014+struct au_branch;
1facf9fc 36015+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
36016+ struct qstr *prefix);
36017+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
36018+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
36019+ struct dentry *dentry);
86dc4139 36020+int au_wh_init(struct au_branch *br, struct super_block *sb);
1facf9fc 36021+
36022+/* diropq flags */
36023+#define AuDiropq_CREATE 1
36024+#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
7f207e10
AM
36025+#define au_fset_diropq(flags, name) \
36026+ do { (flags) |= AuDiropq_##name; } while (0)
36027+#define au_fclr_diropq(flags, name) \
36028+ do { (flags) &= ~AuDiropq_##name; } while (0)
1facf9fc 36029+
36030+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
36031+ unsigned int flags);
36032+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
36033+ struct au_branch *br);
36034+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
36035+ struct dentry *h_parent);
36036+
36037+/* real rmdir for the whiteout-ed dir */
36038+struct au_whtmp_rmdir {
36039+ struct inode *dir;
e49829fe 36040+ struct au_branch *br;
1facf9fc 36041+ struct dentry *wh_dentry;
dece6358 36042+ struct au_nhash whlist;
1facf9fc 36043+};
36044+
36045+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
36046+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
36047+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
36048+ struct dentry *wh_dentry, struct au_nhash *whlist);
36049+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36050+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
36051+
36052+/* ---------------------------------------------------------------------- */
36053+
36054+static inline struct dentry *au_diropq_create(struct dentry *dentry,
36055+ aufs_bindex_t bindex)
36056+{
36057+ return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
36058+}
36059+
36060+static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
36061+{
36062+ return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
36063+}
36064+
36065+#endif /* __KERNEL__ */
36066+#endif /* __AUFS_WHOUT_H__ */
7f207e10
AM
36067diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
36068--- /usr/share/empty/fs/aufs/wkq.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 36069+++ linux/fs/aufs/wkq.c 2019-01-28 14:36:12.222418047 +0100
acd2b654 36070@@ -0,0 +1,392 @@
cd7a4cd9 36071+// SPDX-License-Identifier: GPL-2.0
1facf9fc 36072+/*
b00004a5 36073+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 36074+ *
36075+ * This program, aufs is free software; you can redistribute it and/or modify
36076+ * it under the terms of the GNU General Public License as published by
36077+ * the Free Software Foundation; either version 2 of the License, or
36078+ * (at your option) any later version.
dece6358
AM
36079+ *
36080+ * This program is distributed in the hope that it will be useful,
36081+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36082+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36083+ * GNU General Public License for more details.
36084+ *
36085+ * You should have received a copy of the GNU General Public License
523b37e3 36086+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36087+ */
36088+
36089+/*
36090+ * workqueue for asynchronous/super-io operations
acd2b654 36091+ * todo: try new credential scheme
1facf9fc 36092+ */
36093+
dece6358 36094+#include <linux/module.h>
1facf9fc 36095+#include "aufs.h"
36096+
9dbd164d 36097+/* internal workqueue named AUFS_WKQ_NAME */
b752ccd1 36098+
9dbd164d 36099+static struct workqueue_struct *au_wkq;
1facf9fc 36100+
36101+struct au_wkinfo {
36102+ struct work_struct wk;
7f207e10 36103+ struct kobject *kobj;
1facf9fc 36104+
36105+ unsigned int flags; /* see wkq.h */
36106+
36107+ au_wkq_func_t func;
36108+ void *args;
36109+
8b6a4947
AM
36110+#ifdef CONFIG_LOCKDEP
36111+ int dont_check;
36112+ struct held_lock **hlock;
36113+#endif
36114+
1facf9fc 36115+ struct completion *comp;
36116+};
36117+
36118+/* ---------------------------------------------------------------------- */
8b6a4947
AM
36119+/*
36120+ * Aufs passes some operations to the workqueue such as the internal copyup.
36121+ * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
36122+ * job run by workqueue depends upon the locks acquired in the other task.
36123+ * Delegating a small operation to the workqueue, aufs passes its lockdep
36124+ * information too. And the job in the workqueue restores the info in order to
36125+ * pretend as if it acquired those locks. This is just to make LOCKDEP work
36126+ * correctly and expectedly.
36127+ */
36128+
36129+#ifndef CONFIG_LOCKDEP
36130+AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
36131+AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
36132+AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
36133+AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
36134+AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
36135+#else
36136+static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
36137+{
36138+ wkinfo->hlock = NULL;
36139+ wkinfo->dont_check = 0;
36140+}
36141+
36142+/*
36143+ * 1: matched
36144+ * 0: unmatched
36145+ */
36146+static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
36147+{
36148+ static DEFINE_SPINLOCK(spin);
36149+ static struct {
36150+ char *name;
36151+ struct lock_class_key *key;
36152+ } a[] = {
36153+ { .name = "&sbinfo->si_rwsem" },
36154+ { .name = "&finfo->fi_rwsem" },
36155+ { .name = "&dinfo->di_rwsem" },
36156+ { .name = "&iinfo->ii_rwsem" }
36157+ };
36158+ static int set;
36159+ int i;
36160+
36161+ /* lockless read from 'set.' see below */
36162+ if (set == ARRAY_SIZE(a)) {
36163+ for (i = 0; i < ARRAY_SIZE(a); i++)
36164+ if (a[i].key == key)
36165+ goto match;
36166+ goto unmatch;
36167+ }
36168+
36169+ spin_lock(&spin);
36170+ if (set)
36171+ for (i = 0; i < ARRAY_SIZE(a); i++)
36172+ if (a[i].key == key) {
36173+ spin_unlock(&spin);
36174+ goto match;
36175+ }
36176+ for (i = 0; i < ARRAY_SIZE(a); i++) {
36177+ if (a[i].key) {
36178+ if (unlikely(a[i].key == key)) { /* rare but possible */
36179+ spin_unlock(&spin);
36180+ goto match;
36181+ } else
36182+ continue;
36183+ }
36184+ if (strstr(a[i].name, name)) {
36185+ /*
36186+ * the order of these three lines is important for the
36187+ * lockless read above.
36188+ */
36189+ a[i].key = key;
36190+ spin_unlock(&spin);
36191+ set++;
36192+ /* AuDbg("%d, %s\n", set, name); */
36193+ goto match;
36194+ }
36195+ }
36196+ spin_unlock(&spin);
36197+ goto unmatch;
36198+
36199+match:
36200+ return 1;
36201+unmatch:
36202+ return 0;
36203+}
36204+
36205+static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36206+{
36207+ int err, n;
36208+ struct task_struct *curr;
36209+ struct held_lock **hl, *held_locks, *p;
36210+
36211+ err = 0;
36212+ curr = current;
36213+ wkinfo->dont_check = lockdep_recursing(curr);
36214+ if (wkinfo->dont_check)
36215+ goto out;
36216+ n = curr->lockdep_depth;
36217+ if (!n)
36218+ goto out;
36219+
36220+ err = -ENOMEM;
36221+ wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36222+ if (unlikely(!wkinfo->hlock))
36223+ goto out;
36224+
36225+ err = 0;
36226+#if 0
36227+ if (0 && au_debug_test()) /* left for debugging */
36228+ lockdep_print_held_locks(curr);
36229+#endif
36230+ held_locks = curr->held_locks;
36231+ hl = wkinfo->hlock;
36232+ while (n--) {
36233+ p = held_locks++;
36234+ if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36235+ *hl++ = p;
36236+ }
36237+ *hl = NULL;
36238+
36239+out:
36240+ return err;
36241+}
36242+
36243+static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36244+{
e49925d1 36245+ au_kfree_try_rcu(wkinfo->hlock);
8b6a4947
AM
36246+}
36247+
36248+static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36249+{
36250+ struct held_lock *p, **hl = wkinfo->hlock;
36251+ int subclass;
36252+
36253+ if (wkinfo->dont_check)
36254+ lockdep_off();
36255+ if (!hl)
36256+ return;
36257+ while ((p = *hl++)) { /* assignment */
36258+ subclass = lockdep_hlock_class(p)->subclass;
36259+ /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36260+ if (p->read)
36261+ rwsem_acquire_read(p->instance, subclass, 0,
36262+ /*p->acquire_ip*/_RET_IP_);
36263+ else
36264+ rwsem_acquire(p->instance, subclass, 0,
36265+ /*p->acquire_ip*/_RET_IP_);
36266+ }
36267+}
36268+
36269+static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36270+{
36271+ struct held_lock *p, **hl = wkinfo->hlock;
36272+
36273+ if (wkinfo->dont_check)
36274+ lockdep_on();
36275+ if (!hl)
36276+ return;
36277+ while ((p = *hl++)) /* assignment */
36278+ rwsem_release(p->instance, 0, /*p->acquire_ip*/_RET_IP_);
36279+}
36280+#endif
1facf9fc 36281+
1facf9fc 36282+static void wkq_func(struct work_struct *wk)
36283+{
36284+ struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36285+
2dfbb274 36286+ AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
7f207e10
AM
36287+ AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36288+
8b6a4947 36289+ au_wkq_lockdep_pre(wkinfo);
1facf9fc 36290+ wkinfo->func(wkinfo->args);
8b6a4947 36291+ au_wkq_lockdep_post(wkinfo);
1facf9fc 36292+ if (au_ftest_wkq(wkinfo->flags, WAIT))
36293+ complete(wkinfo->comp);
36294+ else {
7f207e10 36295+ kobject_put(wkinfo->kobj);
9dbd164d 36296+ module_put(THIS_MODULE); /* todo: ?? */
e49925d1 36297+ au_kfree_rcu(wkinfo);
1facf9fc 36298+ }
36299+}
36300+
36301+/*
36302+ * Since struct completion is large, try allocating it dynamically.
36303+ */
c2b27bf2 36304+#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
1facf9fc 36305+#define AuWkqCompDeclare(name) struct completion *comp = NULL
36306+
36307+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36308+{
36309+ *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36310+ if (*comp) {
36311+ init_completion(*comp);
36312+ wkinfo->comp = *comp;
36313+ return 0;
36314+ }
36315+ return -ENOMEM;
36316+}
36317+
36318+static void au_wkq_comp_free(struct completion *comp)
36319+{
e49925d1 36320+ au_kfree_rcu(comp);
1facf9fc 36321+}
36322+
36323+#else
36324+
36325+/* no braces */
36326+#define AuWkqCompDeclare(name) \
36327+ DECLARE_COMPLETION_ONSTACK(_ ## name); \
36328+ struct completion *comp = &_ ## name
36329+
36330+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36331+{
36332+ wkinfo->comp = *comp;
36333+ return 0;
36334+}
36335+
36336+static void au_wkq_comp_free(struct completion *comp __maybe_unused)
36337+{
36338+ /* empty */
36339+}
36340+#endif /* 4KSTACKS */
36341+
53392da6 36342+static void au_wkq_run(struct au_wkinfo *wkinfo)
1facf9fc 36343+{
53392da6
AM
36344+ if (au_ftest_wkq(wkinfo->flags, NEST)) {
36345+ if (au_wkq_test()) {
38d290e6 36346+ AuWarn1("wkq from wkq, unless silly-rename on NFS,"
acd2b654
AM
36347+ " due to a dead dir by UDBA,"
36348+ " or async xino write?\n");
53392da6
AM
36349+ AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36350+ }
36351+ } else
36352+ au_dbg_verify_kthread();
36353+
36354+ if (au_ftest_wkq(wkinfo->flags, WAIT)) {
a1f66529 36355+ INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
9dbd164d 36356+ queue_work(au_wkq, &wkinfo->wk);
4a4d8108
AM
36357+ } else {
36358+ INIT_WORK(&wkinfo->wk, wkq_func);
36359+ schedule_work(&wkinfo->wk);
36360+ }
1facf9fc 36361+}
36362+
7f207e10
AM
36363+/*
36364+ * Be careful. It is easy to make deadlock happen.
36365+ * processA: lock, wkq and wait
36366+ * processB: wkq and wait, lock in wkq
36367+ * --> deadlock
36368+ */
b752ccd1 36369+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
1facf9fc 36370+{
36371+ int err;
36372+ AuWkqCompDeclare(comp);
36373+ struct au_wkinfo wkinfo = {
b752ccd1 36374+ .flags = flags,
1facf9fc 36375+ .func = func,
36376+ .args = args
36377+ };
36378+
36379+ err = au_wkq_comp_alloc(&wkinfo, &comp);
8b6a4947
AM
36380+ if (unlikely(err))
36381+ goto out;
36382+ err = au_wkq_lockdep_alloc(&wkinfo);
36383+ if (unlikely(err))
36384+ goto out_comp;
1facf9fc 36385+ if (!err) {
53392da6 36386+ au_wkq_run(&wkinfo);
1facf9fc 36387+ /* no timeout, no interrupt */
36388+ wait_for_completion(wkinfo.comp);
1facf9fc 36389+ }
8b6a4947 36390+ au_wkq_lockdep_free(&wkinfo);
1facf9fc 36391+
8b6a4947
AM
36392+out_comp:
36393+ au_wkq_comp_free(comp);
36394+out:
36395+ destroy_work_on_stack(&wkinfo.wk);
1facf9fc 36396+ return err;
1facf9fc 36397+}
36398+
027c5e7a
AM
36399+/*
36400+ * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36401+ * problem in a concurrent umounting.
36402+ */
53392da6
AM
36403+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36404+ unsigned int flags)
1facf9fc 36405+{
36406+ int err;
36407+ struct au_wkinfo *wkinfo;
36408+
f0c0a007 36409+ atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
1facf9fc 36410+
36411+ /*
36412+ * wkq_func() must free this wkinfo.
36413+ * it highly depends upon the implementation of workqueue.
36414+ */
36415+ err = 0;
36416+ wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36417+ if (wkinfo) {
7f207e10 36418+ wkinfo->kobj = &au_sbi(sb)->si_kobj;
53392da6 36419+ wkinfo->flags = flags & ~AuWkq_WAIT;
1facf9fc 36420+ wkinfo->func = func;
36421+ wkinfo->args = args;
36422+ wkinfo->comp = NULL;
8b6a4947 36423+ au_wkq_lockdep_init(wkinfo);
7f207e10 36424+ kobject_get(wkinfo->kobj);
9dbd164d 36425+ __module_get(THIS_MODULE); /* todo: ?? */
1facf9fc 36426+
53392da6 36427+ au_wkq_run(wkinfo);
1facf9fc 36428+ } else {
36429+ err = -ENOMEM;
e49829fe 36430+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 36431+ }
36432+
36433+ return err;
36434+}
36435+
36436+/* ---------------------------------------------------------------------- */
36437+
36438+void au_nwt_init(struct au_nowait_tasks *nwt)
36439+{
f0c0a007
AM
36440+ atomic_set(&nwt->nw_len, 0);
36441+ /* smp_mb(); */ /* atomic_set */
1facf9fc 36442+ init_waitqueue_head(&nwt->nw_wq);
36443+}
36444+
36445+void au_wkq_fin(void)
36446+{
9dbd164d 36447+ destroy_workqueue(au_wkq);
1facf9fc 36448+}
36449+
36450+int __init au_wkq_init(void)
36451+{
9dbd164d 36452+ int err;
b752ccd1
AM
36453+
36454+ err = 0;
86dc4139 36455+ au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
9dbd164d
AM
36456+ if (IS_ERR(au_wkq))
36457+ err = PTR_ERR(au_wkq);
36458+ else if (!au_wkq)
36459+ err = -ENOMEM;
b752ccd1
AM
36460+
36461+ return err;
1facf9fc 36462+}
7f207e10
AM
36463diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36464--- /usr/share/empty/fs/aufs/wkq.h 1970-01-01 01:00:00.000000000 +0100
e49925d1 36465+++ linux/fs/aufs/wkq.h 2019-01-28 14:36:12.225751436 +0100
acd2b654 36466@@ -0,0 +1,89 @@
062440b3 36467+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 36468+/*
b00004a5 36469+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 36470+ *
36471+ * This program, aufs is free software; you can redistribute it and/or modify
36472+ * it under the terms of the GNU General Public License as published by
36473+ * the Free Software Foundation; either version 2 of the License, or
36474+ * (at your option) any later version.
dece6358
AM
36475+ *
36476+ * This program is distributed in the hope that it will be useful,
36477+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36478+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36479+ * GNU General Public License for more details.
36480+ *
36481+ * You should have received a copy of the GNU General Public License
523b37e3 36482+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36483+ */
36484+
36485+/*
36486+ * workqueue for asynchronous/super-io operations
36487+ * todo: try new credentials management scheme
36488+ */
36489+
36490+#ifndef __AUFS_WKQ_H__
36491+#define __AUFS_WKQ_H__
36492+
36493+#ifdef __KERNEL__
36494+
8b6a4947 36495+#include <linux/wait.h>
5afbbe0d 36496+
dece6358
AM
36497+struct super_block;
36498+
1facf9fc 36499+/* ---------------------------------------------------------------------- */
36500+
36501+/*
36502+ * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36503+ */
36504+struct au_nowait_tasks {
f0c0a007 36505+ atomic_t nw_len;
1facf9fc 36506+ wait_queue_head_t nw_wq;
36507+};
36508+
36509+/* ---------------------------------------------------------------------- */
36510+
36511+typedef void (*au_wkq_func_t)(void *args);
36512+
36513+/* wkq flags */
36514+#define AuWkq_WAIT 1
9dbd164d 36515+#define AuWkq_NEST (1 << 1)
1facf9fc 36516+#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
7f207e10
AM
36517+#define au_fset_wkq(flags, name) \
36518+ do { (flags) |= AuWkq_##name; } while (0)
36519+#define au_fclr_wkq(flags, name) \
36520+ do { (flags) &= ~AuWkq_##name; } while (0)
1facf9fc 36521+
36522+/* wkq.c */
b752ccd1 36523+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
53392da6
AM
36524+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36525+ unsigned int flags);
1facf9fc 36526+void au_nwt_init(struct au_nowait_tasks *nwt);
36527+int __init au_wkq_init(void);
36528+void au_wkq_fin(void);
36529+
36530+/* ---------------------------------------------------------------------- */
36531+
53392da6
AM
36532+static inline int au_wkq_test(void)
36533+{
36534+ return current->flags & PF_WQ_WORKER;
36535+}
36536+
b752ccd1 36537+static inline int au_wkq_wait(au_wkq_func_t func, void *args)
1facf9fc 36538+{
b752ccd1 36539+ return au_wkq_do_wait(AuWkq_WAIT, func, args);
1facf9fc 36540+}
36541+
36542+static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36543+{
f0c0a007 36544+ if (atomic_dec_and_test(&nwt->nw_len))
1facf9fc 36545+ wake_up_all(&nwt->nw_wq);
36546+}
36547+
36548+static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36549+{
f0c0a007 36550+ wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
1facf9fc 36551+ return 0;
36552+}
36553+
36554+#endif /* __KERNEL__ */
36555+#endif /* __AUFS_WKQ_H__ */
c1595e42
JR
36556diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36557--- /usr/share/empty/fs/aufs/xattr.c 1970-01-01 01:00:00.000000000 +0100
e49925d1 36558+++ linux/fs/aufs/xattr.c 2019-01-28 14:36:12.225751436 +0100
062440b3 36559@@ -0,0 +1,356 @@
cd7a4cd9 36560+// SPDX-License-Identifier: GPL-2.0
c1595e42 36561+/*
b00004a5 36562+ * Copyright (C) 2014-2018 Junjiro R. Okajima
c1595e42
JR
36563+ *
36564+ * This program, aufs is free software; you can redistribute it and/or modify
36565+ * it under the terms of the GNU General Public License as published by
36566+ * the Free Software Foundation; either version 2 of the License, or
36567+ * (at your option) any later version.
36568+ *
36569+ * This program is distributed in the hope that it will be useful,
36570+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36571+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36572+ * GNU General Public License for more details.
36573+ *
36574+ * You should have received a copy of the GNU General Public License
36575+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
36576+ */
36577+
36578+/*
36579+ * handling xattr functions
36580+ */
36581+
a2654f78
AM
36582+#include <linux/fs.h>
36583+#include <linux/posix_acl_xattr.h>
c1595e42
JR
36584+#include <linux/xattr.h>
36585+#include "aufs.h"
36586+
36587+static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36588+{
36589+ if (!ignore_flags)
36590+ goto out;
36591+ switch (err) {
36592+ case -ENOMEM:
36593+ case -EDQUOT:
36594+ goto out;
36595+ }
36596+
36597+ if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36598+ err = 0;
36599+ goto out;
36600+ }
36601+
36602+#define cmp(brattr, prefix) do { \
36603+ if (!strncmp(name, XATTR_##prefix##_PREFIX, \
36604+ XATTR_##prefix##_PREFIX_LEN)) { \
36605+ if (ignore_flags & AuBrAttr_ICEX_##brattr) \
36606+ err = 0; \
36607+ goto out; \
36608+ } \
36609+ } while (0)
36610+
36611+ cmp(SEC, SECURITY);
36612+ cmp(SYS, SYSTEM);
36613+ cmp(TR, TRUSTED);
36614+ cmp(USR, USER);
36615+#undef cmp
36616+
36617+ if (ignore_flags & AuBrAttr_ICEX_OTH)
36618+ err = 0;
36619+
36620+out:
36621+ return err;
36622+}
36623+
36624+static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36625+
36626+static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
7e9cd9fe
AM
36627+ char *name, char **buf, unsigned int ignore_flags,
36628+ unsigned int verbose)
c1595e42
JR
36629+{
36630+ int err;
36631+ ssize_t ssz;
36632+ struct inode *h_idst;
36633+
36634+ ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
36635+ err = ssz;
36636+ if (unlikely(err <= 0)) {
c1595e42
JR
36637+ if (err == -ENODATA
36638+ || (err == -EOPNOTSUPP
b912730e 36639+ && ((ignore_flags & au_xattr_out_of_list)
5527c038 36640+ || (au_test_nfs_noacl(d_inode(h_src))
b912730e
AM
36641+ && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36642+ || !strcmp(name,
36643+ XATTR_NAME_POSIX_ACL_DEFAULT))))
36644+ ))
c1595e42 36645+ err = 0;
b912730e
AM
36646+ if (err && (verbose || au_debug_test()))
36647+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
36648+ goto out;
36649+ }
36650+
36651+ /* unlock it temporary */
5527c038 36652+ h_idst = d_inode(h_dst);
febd17d6 36653+ inode_unlock(h_idst);
c1595e42 36654+ err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
febd17d6 36655+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42 36656+ if (unlikely(err)) {
7e9cd9fe
AM
36657+ if (verbose || au_debug_test())
36658+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
36659+ err = au_xattr_ignore(err, name, ignore_flags);
36660+ }
36661+
36662+out:
36663+ return err;
36664+}
36665+
7e9cd9fe
AM
36666+int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
36667+ unsigned int verbose)
c1595e42
JR
36668+{
36669+ int err, unlocked, acl_access, acl_default;
36670+ ssize_t ssz;
36671+ struct inode *h_isrc, *h_idst;
36672+ char *value, *p, *o, *e;
36673+
36674+ /* try stopping to update the source inode while we are referencing */
7e9cd9fe 36675+ /* there should not be the parent-child relationship between them */
5527c038
JR
36676+ h_isrc = d_inode(h_src);
36677+ h_idst = d_inode(h_dst);
febd17d6 36678+ inode_unlock(h_idst);
be118d29 36679+ inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
febd17d6 36680+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42
JR
36681+ unlocked = 0;
36682+
36683+ /* some filesystems don't list POSIX ACL, for example tmpfs */
36684+ ssz = vfs_listxattr(h_src, NULL, 0);
36685+ err = ssz;
36686+ if (unlikely(err < 0)) {
36687+ AuTraceErr(err);
36688+ if (err == -ENODATA
36689+ || err == -EOPNOTSUPP)
36690+ err = 0; /* ignore */
36691+ goto out;
36692+ }
36693+
36694+ err = 0;
36695+ p = NULL;
36696+ o = NULL;
36697+ if (ssz) {
36698+ err = -ENOMEM;
36699+ p = kmalloc(ssz, GFP_NOFS);
36700+ o = p;
36701+ if (unlikely(!p))
36702+ goto out;
36703+ err = vfs_listxattr(h_src, p, ssz);
36704+ }
3c1bdaff 36705+ inode_unlock_shared(h_isrc);
c1595e42
JR
36706+ unlocked = 1;
36707+ AuDbg("err %d, ssz %zd\n", err, ssz);
36708+ if (unlikely(err < 0))
36709+ goto out_free;
36710+
36711+ err = 0;
36712+ e = p + ssz;
36713+ value = NULL;
36714+ acl_access = 0;
36715+ acl_default = 0;
36716+ while (!err && p < e) {
36717+ acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36718+ sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36719+ acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36720+ sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36721+ - 1);
7e9cd9fe
AM
36722+ err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36723+ verbose);
c1595e42
JR
36724+ p += strlen(p) + 1;
36725+ }
36726+ AuTraceErr(err);
36727+ ignore_flags |= au_xattr_out_of_list;
36728+ if (!err && !acl_access) {
36729+ err = au_do_cpup_xattr(h_dst, h_src,
36730+ XATTR_NAME_POSIX_ACL_ACCESS, &value,
7e9cd9fe 36731+ ignore_flags, verbose);
c1595e42
JR
36732+ AuTraceErr(err);
36733+ }
36734+ if (!err && !acl_default) {
36735+ err = au_do_cpup_xattr(h_dst, h_src,
36736+ XATTR_NAME_POSIX_ACL_DEFAULT, &value,
7e9cd9fe 36737+ ignore_flags, verbose);
c1595e42
JR
36738+ AuTraceErr(err);
36739+ }
36740+
e49925d1 36741+ au_kfree_try_rcu(value);
c1595e42
JR
36742+
36743+out_free:
e49925d1 36744+ au_kfree_try_rcu(o);
c1595e42
JR
36745+out:
36746+ if (!unlocked)
3c1bdaff 36747+ inode_unlock_shared(h_isrc);
c1595e42
JR
36748+ AuTraceErr(err);
36749+ return err;
36750+}
36751+
36752+/* ---------------------------------------------------------------------- */
36753+
a2654f78
AM
36754+static int au_smack_reentering(struct super_block *sb)
36755+{
36756+#if IS_ENABLED(CONFIG_SECURITY_SMACK)
36757+ /*
36758+ * as a part of lookup, smack_d_instantiate() is called, and it calls
36759+ * i_op->getxattr(). ouch.
36760+ */
36761+ return si_pid_test(sb);
36762+#else
36763+ return 0;
36764+#endif
36765+}
36766+
c1595e42
JR
36767+enum {
36768+ AU_XATTR_LIST,
36769+ AU_XATTR_GET
36770+};
36771+
36772+struct au_lgxattr {
36773+ int type;
36774+ union {
36775+ struct {
36776+ char *list;
36777+ size_t size;
36778+ } list;
36779+ struct {
36780+ const char *name;
36781+ void *value;
36782+ size_t size;
36783+ } get;
36784+ } u;
36785+};
36786+
36787+static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
36788+{
36789+ ssize_t err;
a2654f78 36790+ int reenter;
c1595e42
JR
36791+ struct path h_path;
36792+ struct super_block *sb;
36793+
36794+ sb = dentry->d_sb;
a2654f78
AM
36795+ reenter = au_smack_reentering(sb);
36796+ if (!reenter) {
36797+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36798+ if (unlikely(err))
36799+ goto out;
36800+ }
36801+ err = au_h_path_getattr(dentry, /*force*/1, &h_path, reenter);
c1595e42
JR
36802+ if (unlikely(err))
36803+ goto out_si;
36804+ if (unlikely(!h_path.dentry))
36805+ /* illegally overlapped or something */
36806+ goto out_di; /* pretending success */
36807+
36808+ /* always topmost entry only */
36809+ switch (arg->type) {
36810+ case AU_XATTR_LIST:
36811+ err = vfs_listxattr(h_path.dentry,
36812+ arg->u.list.list, arg->u.list.size);
36813+ break;
36814+ case AU_XATTR_GET:
5afbbe0d 36815+ AuDebugOn(d_is_negative(h_path.dentry));
c1595e42
JR
36816+ err = vfs_getxattr(h_path.dentry,
36817+ arg->u.get.name, arg->u.get.value,
36818+ arg->u.get.size);
36819+ break;
36820+ }
36821+
36822+out_di:
a2654f78
AM
36823+ if (!reenter)
36824+ di_read_unlock(dentry, AuLock_IR);
c1595e42 36825+out_si:
a2654f78
AM
36826+ if (!reenter)
36827+ si_read_unlock(sb);
c1595e42
JR
36828+out:
36829+ AuTraceErr(err);
36830+ return err;
36831+}
36832+
36833+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36834+{
36835+ struct au_lgxattr arg = {
36836+ .type = AU_XATTR_LIST,
36837+ .u.list = {
36838+ .list = list,
36839+ .size = size
36840+ },
36841+ };
36842+
36843+ return au_lgxattr(dentry, &arg);
36844+}
36845+
f2c43d5f
AM
36846+static ssize_t au_getxattr(struct dentry *dentry,
36847+ struct inode *inode __maybe_unused,
36848+ const char *name, void *value, size_t size)
c1595e42
JR
36849+{
36850+ struct au_lgxattr arg = {
36851+ .type = AU_XATTR_GET,
36852+ .u.get = {
36853+ .name = name,
36854+ .value = value,
36855+ .size = size
36856+ },
36857+ };
36858+
36859+ return au_lgxattr(dentry, &arg);
36860+}
36861+
f2c43d5f
AM
36862+static int au_setxattr(struct dentry *dentry, struct inode *inode,
36863+ const char *name, const void *value, size_t size,
36864+ int flags)
c1595e42 36865+{
f2c43d5f 36866+ struct au_sxattr arg = {
c1595e42
JR
36867+ .type = AU_XATTR_SET,
36868+ .u.set = {
36869+ .name = name,
36870+ .value = value,
36871+ .size = size,
36872+ .flags = flags
36873+ },
36874+ };
36875+
f2c43d5f 36876+ return au_sxattr(dentry, inode, &arg);
c1595e42
JR
36877+}
36878+
36879+/* ---------------------------------------------------------------------- */
36880+
f2c43d5f
AM
36881+static int au_xattr_get(const struct xattr_handler *handler,
36882+ struct dentry *dentry, struct inode *inode,
36883+ const char *name, void *buffer, size_t size)
c1595e42 36884+{
f2c43d5f 36885+ return au_getxattr(dentry, inode, name, buffer, size);
c1595e42
JR
36886+}
36887+
f2c43d5f
AM
36888+static int au_xattr_set(const struct xattr_handler *handler,
36889+ struct dentry *dentry, struct inode *inode,
36890+ const char *name, const void *value, size_t size,
36891+ int flags)
c1595e42 36892+{
f2c43d5f 36893+ return au_setxattr(dentry, inode, name, value, size, flags);
c1595e42
JR
36894+}
36895+
36896+static const struct xattr_handler au_xattr_handler = {
f2c43d5f
AM
36897+ .name = "",
36898+ .prefix = "",
c1595e42
JR
36899+ .get = au_xattr_get,
36900+ .set = au_xattr_set
c1595e42
JR
36901+};
36902+
36903+static const struct xattr_handler *au_xattr_handlers[] = {
a2654f78
AM
36904+#ifdef CONFIG_FS_POSIX_ACL
36905+ &posix_acl_access_xattr_handler,
36906+ &posix_acl_default_xattr_handler,
36907+#endif
36908+ &au_xattr_handler, /* must be last */
f2c43d5f 36909+ NULL
c1595e42
JR
36910+};
36911+
36912+void au_xattr_init(struct super_block *sb)
36913+{
f2c43d5f 36914+ sb->s_xattr = au_xattr_handlers;
c1595e42 36915+}
7f207e10
AM
36916diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
36917--- /usr/share/empty/fs/aufs/xino.c 1970-01-01 01:00:00.000000000 +0100
e49925d1
JR
36918+++ linux/fs/aufs/xino.c 2019-01-28 14:36:12.225751436 +0100
36919@@ -0,0 +1,1956 @@
cd7a4cd9 36920+// SPDX-License-Identifier: GPL-2.0
1facf9fc 36921+/*
b00004a5 36922+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 36923+ *
36924+ * This program, aufs is free software; you can redistribute it and/or modify
36925+ * it under the terms of the GNU General Public License as published by
36926+ * the Free Software Foundation; either version 2 of the License, or
36927+ * (at your option) any later version.
dece6358
AM
36928+ *
36929+ * This program is distributed in the hope that it will be useful,
36930+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36931+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36932+ * GNU General Public License for more details.
36933+ *
36934+ * You should have received a copy of the GNU General Public License
523b37e3 36935+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36936+ */
36937+
36938+/*
36939+ * external inode number translation table and bitmap
062440b3
AM
36940+ *
36941+ * things to consider
36942+ * - the lifetime
36943+ * + au_xino object
36944+ * + XINO files (xino, xib, xigen)
36945+ * + dynamic debugfs entries (xiN)
36946+ * + static debugfs entries (xib, xigen)
36947+ * + static sysfs entry (xi_path)
36948+ * - several entry points to handle them.
36949+ * + mount(2) without xino option (default)
36950+ * + mount(2) with xino option
36951+ * + mount(2) with noxino option
36952+ * + umount(2)
36953+ * + remount with add/del branches
36954+ * + remount with xino/noxino options
1facf9fc 36955+ */
36956+
36957+#include <linux/seq_file.h>
392086de 36958+#include <linux/statfs.h>
1facf9fc 36959+#include "aufs.h"
36960+
062440b3
AM
36961+static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
36962+ aufs_bindex_t bbot,
36963+ struct super_block *h_sb)
1facf9fc 36964+{
062440b3
AM
36965+ /* todo: try binary-search if the branches are many */
36966+ for (; btop <= bbot; btop++)
36967+ if (h_sb == au_sbr_sb(sb, btop))
36968+ return btop;
36969+ return -1;
be52b249
AM
36970+}
36971+
062440b3
AM
36972+/*
36973+ * find another branch who is on the same filesystem of the specified
36974+ * branch{@btgt}. search until @bbot.
36975+ */
36976+static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
36977+ aufs_bindex_t bbot)
1facf9fc 36978+{
062440b3
AM
36979+ aufs_bindex_t bindex;
36980+ struct super_block *tgt_sb;
1facf9fc 36981+
062440b3
AM
36982+ tgt_sb = au_sbr_sb(sb, btgt);
36983+ bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
36984+ if (bindex < 0)
36985+ bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
1facf9fc 36986+
062440b3 36987+ return bindex;
1facf9fc 36988+}
36989+
36990+/* ---------------------------------------------------------------------- */
36991+
36992+/*
062440b3 36993+ * stop unnecessary notify events at creating xino files
1facf9fc 36994+ */
acd2b654
AM
36995+
36996+aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
36997+{
36998+ aufs_bindex_t bfound, bindex, bbot;
36999+ struct dentry *parent;
37000+ struct au_branch *br;
37001+
37002+ bfound = -1;
37003+ parent = dentry->d_parent; /* safe d_parent access */
37004+ bbot = au_sbbot(sb);
37005+ for (bindex = 0; bindex <= bbot; bindex++) {
37006+ br = au_sbr(sb, bindex);
37007+ if (au_br_dentry(br) == parent) {
37008+ bfound = bindex;
37009+ break;
37010+ }
37011+ }
37012+
37013+ AuDbg("bfound b%d\n", bfound);
37014+ return bfound;
37015+}
37016+
062440b3
AM
37017+struct au_xino_lock_dir {
37018+ struct au_hinode *hdir;
37019+ struct dentry *parent;
37020+ struct inode *dir;
37021+};
37022+
37023+static struct dentry *au_dget_parent_lock(struct dentry *dentry,
37024+ unsigned int lsc)
1facf9fc 37025+{
062440b3
AM
37026+ struct dentry *parent;
37027+ struct inode *dir;
1facf9fc 37028+
062440b3 37029+ parent = dget_parent(dentry);
5527c038 37030+ dir = d_inode(parent);
062440b3
AM
37031+ inode_lock_nested(dir, lsc);
37032+#if 0 /* it should not happen */
37033+ spin_lock(&dentry->d_lock);
37034+ if (unlikely(dentry->d_parent != parent)) {
37035+ spin_unlock(&dentry->d_lock);
37036+ inode_unlock(dir);
37037+ dput(parent);
37038+ parent = NULL;
1facf9fc 37039+ goto out;
37040+ }
062440b3 37041+ spin_unlock(&dentry->d_lock);
1facf9fc 37042+
4f0767ce 37043+out:
062440b3
AM
37044+#endif
37045+ return parent;
1facf9fc 37046+}
37047+
062440b3 37048+static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
1facf9fc 37049+ struct au_xino_lock_dir *ldir)
37050+{
acd2b654 37051+ aufs_bindex_t bindex;
1facf9fc 37052+
37053+ ldir->hdir = NULL;
acd2b654 37054+ bindex = au_xi_root(sb, xipath->dentry);
1facf9fc 37055+ if (bindex >= 0) {
062440b3 37056+ /* rw branch root */
5527c038 37057+ ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 37058+ au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
1facf9fc 37059+ } else {
062440b3
AM
37060+ /* other */
37061+ ldir->parent = au_dget_parent_lock(xipath->dentry,
37062+ AuLsc_I_PARENT);
febd17d6 37063+ ldir->dir = d_inode(ldir->parent);
1facf9fc 37064+ }
37065+}
37066+
37067+static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
37068+{
37069+ if (ldir->hdir)
5afbbe0d 37070+ au_hn_inode_unlock(ldir->hdir);
1facf9fc 37071+ else {
febd17d6 37072+ inode_unlock(ldir->dir);
1facf9fc 37073+ dput(ldir->parent);
37074+ }
37075+}
37076+
37077+/* ---------------------------------------------------------------------- */
37078+
062440b3
AM
37079+/*
37080+ * create and set a new xino file
37081+ */
37082+struct file *au_xino_create(struct super_block *sb, char *fpath, int silent)
37083+{
37084+ struct file *file;
37085+ struct dentry *h_parent, *d;
37086+ struct inode *h_dir, *inode;
37087+ int err;
37088+
37089+ /*
37090+ * at mount-time, and the xino file is the default path,
37091+ * hnotify is disabled so we have no notify events to ignore.
37092+ * when a user specified the xino, we cannot get au_hdir to be ignored.
37093+ */
37094+ file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37095+ /* | __FMODE_NONOTIFY */,
cd7a4cd9 37096+ 0666);
062440b3
AM
37097+ if (IS_ERR(file)) {
37098+ if (!silent)
37099+ pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
37100+ return file;
37101+ }
37102+
37103+ /* keep file count */
37104+ err = 0;
37105+ d = file->f_path.dentry;
37106+ h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
37107+ /* mnt_want_write() is unnecessary here */
37108+ h_dir = d_inode(h_parent);
37109+ inode = file_inode(file);
37110+ /* no delegation since it is just created */
37111+ if (inode->i_nlink)
37112+ err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
37113+ /*force*/0);
37114+ inode_unlock(h_dir);
37115+ dput(h_parent);
37116+ if (unlikely(err)) {
37117+ if (!silent)
37118+ pr_err("unlink %s(%d)\n", fpath, err);
37119+ goto out;
37120+ }
37121+
37122+ err = -EINVAL;
37123+ if (unlikely(sb == d->d_sb)) {
37124+ if (!silent)
37125+ pr_err("%s must be outside\n", fpath);
37126+ goto out;
37127+ }
37128+ if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
37129+ if (!silent)
37130+ pr_err("xino doesn't support %s(%s)\n",
37131+ fpath, au_sbtype(d->d_sb));
37132+ goto out;
37133+ }
37134+ return file; /* success */
37135+
37136+out:
37137+ fput(file);
37138+ file = ERR_PTR(err);
37139+ return file;
37140+}
37141+
37142+/*
37143+ * create a new xinofile at the same place/path as @base.
37144+ */
37145+struct file *au_xino_create2(struct super_block *sb, struct path *base,
37146+ struct file *copy_src)
37147+{
37148+ struct file *file;
37149+ struct dentry *dentry, *parent;
37150+ struct inode *dir, *delegated;
37151+ struct qstr *name;
37152+ struct path path;
37153+ int err, do_unlock;
37154+ struct au_xino_lock_dir ldir;
37155+
37156+ do_unlock = 1;
37157+ au_xino_lock_dir(sb, base, &ldir);
37158+ dentry = base->dentry;
37159+ parent = dentry->d_parent; /* dir inode is locked */
37160+ dir = d_inode(parent);
37161+ IMustLock(dir);
37162+
37163+ name = &dentry->d_name;
37164+ path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
37165+ if (IS_ERR(path.dentry)) {
37166+ file = (void *)path.dentry;
37167+ pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37168+ goto out;
37169+ }
37170+
37171+ /* no need to mnt_want_write() since we call dentry_open() later */
cd7a4cd9 37172+ err = vfs_create(dir, path.dentry, 0666, NULL);
062440b3
AM
37173+ if (unlikely(err)) {
37174+ file = ERR_PTR(err);
37175+ pr_err("%pd create err %d\n", dentry, err);
37176+ goto out_dput;
37177+ }
37178+
37179+ path.mnt = base->mnt;
37180+ file = vfsub_dentry_open(&path,
37181+ O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37182+ /* | __FMODE_NONOTIFY */);
37183+ if (IS_ERR(file)) {
37184+ pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37185+ goto out_dput;
37186+ }
37187+
37188+ delegated = NULL;
37189+ err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37190+ au_xino_unlock_dir(&ldir);
37191+ do_unlock = 0;
37192+ if (unlikely(err == -EWOULDBLOCK)) {
37193+ pr_warn("cannot retry for NFSv4 delegation"
37194+ " for an internal unlink\n");
37195+ iput(delegated);
37196+ }
37197+ if (unlikely(err)) {
37198+ pr_err("%pd unlink err %d\n", dentry, err);
37199+ goto out_fput;
37200+ }
37201+
37202+ if (copy_src) {
37203+ /* no one can touch copy_src xino */
37204+ err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37205+ if (unlikely(err)) {
37206+ pr_err("%pd copy err %d\n", dentry, err);
37207+ goto out_fput;
37208+ }
37209+ }
37210+ goto out_dput; /* success */
1facf9fc 37211+
062440b3
AM
37212+out_fput:
37213+ fput(file);
37214+ file = ERR_PTR(err);
37215+out_dput:
37216+ dput(path.dentry);
37217+out:
37218+ if (do_unlock)
37219+ au_xino_unlock_dir(&ldir);
37220+ return file;
37221+}
37222+
acd2b654
AM
37223+struct file *au_xino_file1(struct au_xino *xi)
37224+{
37225+ struct file *file;
37226+ unsigned int u, nfile;
37227+
37228+ file = NULL;
37229+ nfile = xi->xi_nfile;
37230+ for (u = 0; u < nfile; u++) {
37231+ file = xi->xi_file[u];
37232+ if (file)
37233+ break;
37234+ }
37235+
37236+ return file;
37237+}
37238+
37239+static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37240+{
37241+ int err;
37242+ struct file *f;
37243+ void *p;
37244+
37245+ if (file)
37246+ get_file(file);
37247+
37248+ err = 0;
37249+ f = NULL;
37250+ if (idx < xi->xi_nfile) {
37251+ f = xi->xi_file[idx];
37252+ if (f)
37253+ fput(f);
37254+ } else {
37255+ p = au_kzrealloc(xi->xi_file,
37256+ sizeof(*xi->xi_file) * xi->xi_nfile,
37257+ sizeof(*xi->xi_file) * (idx + 1),
37258+ GFP_NOFS, /*may_shrink*/0);
37259+ if (p) {
37260+ MtxMustLock(&xi->xi_mtx);
37261+ xi->xi_file = p;
37262+ xi->xi_nfile = idx + 1;
37263+ } else {
37264+ err = -ENOMEM;
37265+ if (file)
37266+ fput(file);
37267+ goto out;
37268+ }
37269+ }
37270+ xi->xi_file[idx] = file;
37271+
37272+out:
37273+ return err;
37274+}
37275+
37276+/*
37277+ * if @xinew->xi is not set, then create new xigen file.
37278+ */
37279+struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37280+{
37281+ struct file *file;
37282+ int err;
37283+
37284+ SiMustAnyLock(sb);
37285+
37286+ file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37287+ if (IS_ERR(file)) {
37288+ err = PTR_ERR(file);
37289+ pr_err("%s[%d], err %d\n",
37290+ xinew->xi ? "xino" : "xigen",
37291+ xinew->idx, err);
37292+ goto out;
37293+ }
37294+
37295+ if (xinew->xi)
37296+ err = au_xino_file_set(xinew->xi, xinew->idx, file);
37297+ else {
37298+ BUG();
37299+ /* todo: make xigen file an array */
37300+ /* err = au_xigen_file_set(sb, xinew->idx, file); */
37301+ }
37302+ fput(file);
37303+ if (unlikely(err))
37304+ file = ERR_PTR(err);
37305+
37306+out:
37307+ return file;
37308+}
37309+
062440b3
AM
37310+/* ---------------------------------------------------------------------- */
37311+
37312+/*
37313+ * truncate xino files
37314+ */
acd2b654
AM
37315+static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37316+ int idx, struct kstatfs *st)
1facf9fc 37317+{
37318+ int err;
392086de 37319+ blkcnt_t blocks;
acd2b654
AM
37320+ struct file *file, *new_xino;
37321+ struct au_xi_new xinew = {
37322+ .idx = idx
37323+ };
37324+
37325+ err = 0;
37326+ xinew.xi = au_sbr(sb, bindex)->br_xino;
37327+ file = au_xino_file(xinew.xi, idx);
37328+ if (!file)
37329+ goto out;
37330+
37331+ xinew.base = &file->f_path;
37332+ err = vfs_statfs(xinew.base, st);
37333+ if (unlikely(err)) {
37334+ AuErr1("statfs err %d, ignored\n", err);
37335+ err = 0;
37336+ goto out;
37337+ }
37338+
37339+ blocks = file_inode(file)->i_blocks;
37340+ pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37341+ bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37342+
37343+ xinew.copy_src = file;
37344+ new_xino = au_xi_new(sb, &xinew);
37345+ if (IS_ERR(new_xino)) {
37346+ err = PTR_ERR(new_xino);
37347+ pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37348+ goto out;
37349+ }
37350+
37351+ err = vfs_statfs(&new_xino->f_path, st);
37352+ if (!err)
37353+ pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37354+ bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37355+ st->f_bfree, st->f_blocks);
37356+ else {
37357+ AuErr1("statfs err %d, ignored\n", err);
37358+ err = 0;
37359+ }
37360+
37361+out:
37362+ return err;
37363+}
37364+
37365+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37366+{
37367+ int err, i;
37368+ unsigned long jiffy;
062440b3 37369+ aufs_bindex_t bbot;
392086de 37370+ struct kstatfs *st;
1facf9fc 37371+ struct au_branch *br;
acd2b654 37372+ struct au_xino *xi;
1facf9fc 37373+
392086de 37374+ err = -ENOMEM;
be52b249 37375+ st = kmalloc(sizeof(*st), GFP_NOFS);
392086de
AM
37376+ if (unlikely(!st))
37377+ goto out;
37378+
1facf9fc 37379+ err = -EINVAL;
5afbbe0d
AM
37380+ bbot = au_sbbot(sb);
37381+ if (unlikely(bindex < 0 || bbot < bindex))
392086de 37382+ goto out_st;
392086de 37383+
1facf9fc 37384+ err = 0;
acd2b654
AM
37385+ jiffy = jiffies;
37386+ br = au_sbr(sb, bindex);
37387+ xi = br->br_xino;
37388+ for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37389+ err = au_xino_do_trunc(sb, bindex, i, st);
37390+ if (!err)
37391+ au_sbi(sb)->si_xino_jiffy = jiffy;
392086de
AM
37392+
37393+out_st:
e49925d1 37394+ au_kfree_rcu(st);
4f0767ce 37395+out:
1facf9fc 37396+ return err;
37397+}
37398+
37399+struct xino_do_trunc_args {
37400+ struct super_block *sb;
37401+ struct au_branch *br;
acd2b654 37402+ int idx;
1facf9fc 37403+};
37404+
37405+static void xino_do_trunc(void *_args)
37406+{
37407+ struct xino_do_trunc_args *args = _args;
37408+ struct super_block *sb;
37409+ struct au_branch *br;
37410+ struct inode *dir;
acd2b654 37411+ int err, idx;
1facf9fc 37412+ aufs_bindex_t bindex;
37413+
37414+ err = 0;
37415+ sb = args->sb;
5527c038 37416+ dir = d_inode(sb->s_root);
1facf9fc 37417+ br = args->br;
acd2b654 37418+ idx = args->idx;
1facf9fc 37419+
37420+ si_noflush_write_lock(sb);
37421+ ii_read_lock_parent(dir);
37422+ bindex = au_br_index(sb, br->br_id);
acd2b654 37423+ err = au_xino_trunc(sb, bindex, idx);
1facf9fc 37424+ ii_read_unlock(dir);
37425+ if (unlikely(err))
392086de 37426+ pr_warn("err b%d, (%d)\n", bindex, err);
062440b3 37427+ atomic_dec(&br->br_xino->xi_truncating);
acd2b654 37428+ au_lcnt_dec(&br->br_count);
1facf9fc 37429+ si_write_unlock(sb);
027c5e7a 37430+ au_nwt_done(&au_sbi(sb)->si_nowait);
e49925d1 37431+ au_kfree_rcu(args);
1facf9fc 37432+}
37433+
acd2b654
AM
37434+/*
37435+ * returns the index in the xi_file array whose corresponding file is necessary
37436+ * to truncate, or -1 which means no need to truncate.
37437+ */
392086de
AM
37438+static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37439+{
37440+ int err;
acd2b654 37441+ unsigned int u;
392086de
AM
37442+ struct kstatfs st;
37443+ struct au_sbinfo *sbinfo;
acd2b654 37444+ struct au_xino *xi;
062440b3 37445+ struct file *file;
392086de
AM
37446+
37447+ /* todo: si_xino_expire and the ratio should be customizable */
37448+ sbinfo = au_sbi(sb);
37449+ if (time_before(jiffies,
37450+ sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
acd2b654 37451+ return -1;
392086de
AM
37452+
37453+ /* truncation border */
acd2b654
AM
37454+ xi = br->br_xino;
37455+ for (u = 0; u < xi->xi_nfile; u++) {
37456+ file = au_xino_file(xi, u);
37457+ if (!file)
37458+ continue;
37459+
37460+ err = vfs_statfs(&file->f_path, &st);
37461+ if (unlikely(err)) {
37462+ AuErr1("statfs err %d, ignored\n", err);
37463+ return -1;
37464+ }
37465+ if (div64_u64(st.f_bfree * 100, st.f_blocks)
37466+ >= AUFS_XINO_DEF_TRUNC)
37467+ return u;
392086de 37468+ }
392086de 37469+
acd2b654 37470+ return -1;
392086de
AM
37471+}
37472+
1facf9fc 37473+static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37474+{
acd2b654 37475+ int idx;
1facf9fc 37476+ struct xino_do_trunc_args *args;
37477+ int wkq_err;
37478+
acd2b654
AM
37479+ idx = xino_trunc_test(sb, br);
37480+ if (idx < 0)
1facf9fc 37481+ return;
37482+
062440b3 37483+ if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
1facf9fc 37484+ goto out;
37485+
37486+ /* lock and kfree() will be called in trunc_xino() */
37487+ args = kmalloc(sizeof(*args), GFP_NOFS);
37488+ if (unlikely(!args)) {
37489+ AuErr1("no memory\n");
f0c0a007 37490+ goto out;
1facf9fc 37491+ }
37492+
acd2b654 37493+ au_lcnt_inc(&br->br_count);
1facf9fc 37494+ args->sb = sb;
37495+ args->br = br;
acd2b654 37496+ args->idx = idx;
53392da6 37497+ wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
1facf9fc 37498+ if (!wkq_err)
37499+ return; /* success */
37500+
4a4d8108 37501+ pr_err("wkq %d\n", wkq_err);
acd2b654 37502+ au_lcnt_dec(&br->br_count);
e49925d1 37503+ au_kfree_rcu(args);
1facf9fc 37504+
4f0767ce 37505+out:
062440b3 37506+ atomic_dec(&br->br_xino->xi_truncating);
1facf9fc 37507+}
37508+
37509+/* ---------------------------------------------------------------------- */
37510+
acd2b654
AM
37511+struct au_xi_calc {
37512+ int idx;
37513+ loff_t pos;
37514+};
37515+
37516+static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37517+ struct au_xi_calc *calc)
37518+{
37519+ loff_t maxent;
37520+
37521+ maxent = au_xi_maxent(sb);
37522+ calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37523+ calc->pos *= sizeof(ino_t);
37524+}
37525+
37526+static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37527+ struct au_xi_calc *calc)
37528+{
37529+ int err;
37530+ struct file *file;
37531+ struct au_xino *xi = br->br_xino;
37532+ struct au_xi_new xinew = {
37533+ .xi = xi
37534+ };
37535+
37536+ SiMustAnyLock(sb);
37537+
37538+ err = 0;
37539+ if (!xi)
37540+ goto out;
37541+
37542+ mutex_lock(&xi->xi_mtx);
37543+ file = au_xino_file(xi, calc->idx);
37544+ if (file)
37545+ goto out_mtx;
37546+
37547+ file = au_xino_file(xi, /*idx*/-1);
37548+ AuDebugOn(!file);
37549+ xinew.idx = calc->idx;
37550+ xinew.base = &file->f_path;
37551+ /* xinew.copy_src = NULL; */
37552+ file = au_xi_new(sb, &xinew);
37553+ if (IS_ERR(file))
37554+ err = PTR_ERR(file);
37555+
37556+out_mtx:
37557+ mutex_unlock(&xi->xi_mtx);
37558+out:
37559+ return err;
37560+}
37561+
37562+struct au_xino_do_new_async_args {
37563+ struct super_block *sb;
37564+ struct au_branch *br;
37565+ struct au_xi_calc calc;
37566+ ino_t ino;
37567+};
37568+
e49925d1
JR
37569+struct au_xi_writing {
37570+ struct hlist_bl_node node;
37571+ ino_t h_ino, ino;
37572+};
37573+
acd2b654
AM
37574+static int au_xino_do_write(vfs_writef_t write, struct file *file,
37575+ struct au_xi_calc *calc, ino_t ino);
37576+
37577+static void au_xino_call_do_new_async(void *args)
37578+{
37579+ struct au_xino_do_new_async_args *a = args;
37580+ struct au_branch *br;
37581+ struct super_block *sb;
37582+ struct au_sbinfo *sbi;
37583+ struct inode *root;
37584+ struct file *file;
e49925d1
JR
37585+ struct au_xi_writing *del, *p;
37586+ struct hlist_bl_head *hbl;
37587+ struct hlist_bl_node *pos;
acd2b654
AM
37588+ int err;
37589+
37590+ br = a->br;
37591+ sb = a->sb;
37592+ sbi = au_sbi(sb);
37593+ si_noflush_read_lock(sb);
37594+ root = d_inode(sb->s_root);
37595+ ii_read_lock_child(root);
37596+ err = au_xino_do_new_async(sb, br, &a->calc);
e49925d1 37597+ if (unlikely(err)) {
acd2b654 37598+ AuIOErr("err %d\n", err);
e49925d1
JR
37599+ goto out;
37600+ }
37601+
37602+ file = au_xino_file(br->br_xino, a->calc.idx);
37603+ AuDebugOn(!file);
37604+ err = au_xino_do_write(sbi->si_xwrite, file, &a->calc, a->ino);
37605+ if (unlikely(err)) {
37606+ AuIOErr("err %d\n", err);
37607+ goto out;
37608+ }
37609+
37610+ del = NULL;
37611+ hbl = &br->br_xino->xi_writing;
37612+ hlist_bl_lock(hbl);
37613+ au_hbl_for_each(pos, hbl) {
37614+ p = container_of(pos, struct au_xi_writing, node);
37615+ if (p->ino == a->ino) {
37616+ del = p;
37617+ hlist_bl_del(&p->node);
37618+ break;
37619+ }
37620+ }
37621+ hlist_bl_unlock(hbl);
37622+ au_kfree_rcu(del);
37623+
37624+out:
acd2b654
AM
37625+ au_lcnt_dec(&br->br_count);
37626+ ii_read_unlock(root);
37627+ si_read_unlock(sb);
37628+ au_nwt_done(&sbi->si_nowait);
e49925d1 37629+ au_kfree_rcu(a);
acd2b654
AM
37630+}
37631+
37632+/*
37633+ * create a new xino file asynchronously
37634+ */
37635+static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37636+ struct au_xi_calc *calc, ino_t ino)
37637+{
37638+ int err;
37639+ struct au_xino_do_new_async_args *arg;
37640+
37641+ err = -ENOMEM;
37642+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
37643+ if (unlikely(!arg))
37644+ goto out;
37645+
37646+ arg->sb = sb;
37647+ arg->br = br;
37648+ arg->calc = *calc;
37649+ arg->ino = ino;
37650+ au_lcnt_inc(&br->br_count);
37651+ err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37652+ if (unlikely(err)) {
37653+ pr_err("wkq %d\n", err);
37654+ au_lcnt_dec(&br->br_count);
e49925d1 37655+ au_kfree_rcu(arg);
acd2b654
AM
37656+ }
37657+
37658+out:
37659+ return err;
37660+}
37661+
062440b3
AM
37662+/*
37663+ * read @ino from xinofile for the specified branch{@sb, @bindex}
37664+ * at the position of @h_ino.
37665+ */
37666+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37667+ ino_t *ino)
37668+{
37669+ int err;
37670+ ssize_t sz;
acd2b654 37671+ struct au_xi_calc calc;
062440b3 37672+ struct au_sbinfo *sbinfo;
acd2b654 37673+ struct file *file;
e49925d1
JR
37674+ struct au_xino *xi;
37675+ struct hlist_bl_head *hbl;
37676+ struct hlist_bl_node *pos;
37677+ struct au_xi_writing *p;
062440b3
AM
37678+
37679+ *ino = 0;
37680+ if (!au_opt_test(au_mntflags(sb), XINO))
37681+ return 0; /* no xino */
37682+
37683+ err = 0;
acd2b654 37684+ au_xi_calc(sb, h_ino, &calc);
e49925d1
JR
37685+ xi = au_sbr(sb, bindex)->br_xino;
37686+ file = au_xino_file(xi, calc.idx);
37687+ if (!file) {
37688+ hbl = &xi->xi_writing;
37689+ hlist_bl_lock(hbl);
37690+ au_hbl_for_each(pos, hbl) {
37691+ p = container_of(pos, struct au_xi_writing, node);
37692+ if (p->h_ino == h_ino) {
37693+ AuDbg("hi%llu, i%llu, found\n",
37694+ (u64)p->h_ino, (u64)p->ino);
37695+ *ino = p->ino;
37696+ break;
37697+ }
37698+ }
37699+ hlist_bl_unlock(hbl);
37700+ return 0;
37701+ } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37702+ return 0; /* no xino */
062440b3 37703+
acd2b654
AM
37704+ sbinfo = au_sbi(sb);
37705+ sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &calc.pos);
062440b3
AM
37706+ if (sz == sizeof(*ino))
37707+ return 0; /* success */
37708+
37709+ err = sz;
37710+ if (unlikely(sz >= 0)) {
37711+ err = -EIO;
37712+ AuIOErr("xino read error (%zd)\n", sz);
37713+ }
062440b3
AM
37714+ return err;
37715+}
37716+
5527c038 37717+static int au_xino_do_write(vfs_writef_t write, struct file *file,
acd2b654 37718+ struct au_xi_calc *calc, ino_t ino)
1facf9fc 37719+{
1facf9fc 37720+ ssize_t sz;
37721+
acd2b654 37722+ sz = xino_fwrite(write, file, &ino, sizeof(ino), &calc->pos);
1facf9fc 37723+ if (sz == sizeof(ino))
37724+ return 0; /* success */
37725+
37726+ AuIOErr("write failed (%zd)\n", sz);
37727+ return -EIO;
37728+}
37729+
37730+/*
37731+ * write @ino to the xinofile for the specified branch{@sb, @bindex}
37732+ * at the position of @h_ino.
37733+ * even if @ino is zero, it is written to the xinofile and means no entry.
37734+ * if the size of the xino file on a specific filesystem exceeds the watermark,
37735+ * try truncating it.
37736+ */
37737+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37738+ ino_t ino)
37739+{
37740+ int err;
37741+ unsigned int mnt_flags;
acd2b654
AM
37742+ struct au_xi_calc calc;
37743+ struct file *file;
1facf9fc 37744+ struct au_branch *br;
acd2b654 37745+ struct au_xino *xi;
e49925d1 37746+ struct au_xi_writing *p;
1facf9fc 37747+
dece6358 37748+ SiMustAnyLock(sb);
1facf9fc 37749+
37750+ mnt_flags = au_mntflags(sb);
37751+ if (!au_opt_test(mnt_flags, XINO))
37752+ return 0;
37753+
acd2b654 37754+ au_xi_calc(sb, h_ino, &calc);
1facf9fc 37755+ br = au_sbr(sb, bindex);
acd2b654
AM
37756+ xi = br->br_xino;
37757+ file = au_xino_file(xi, calc.idx);
37758+ if (!file) {
e49925d1
JR
37759+ /* store the inum pair into the list */
37760+ p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37761+ p->h_ino = h_ino;
37762+ p->ino = ino;
37763+ au_hbl_add(&p->node, &xi->xi_writing);
37764+
acd2b654
AM
37765+ /* create and write a new xino file asynchronously */
37766+ err = au_xino_new_async(sb, br, &calc, ino);
37767+ if (!err)
37768+ return 0; /* success */
37769+ goto out;
37770+ }
37771+
37772+ err = au_xino_do_write(au_sbi(sb)->si_xwrite, file, &calc, ino);
1facf9fc 37773+ if (!err) {
acd2b654 37774+ br = au_sbr(sb, bindex);
1facf9fc 37775+ if (au_opt_test(mnt_flags, TRUNC_XINO)
86dc4139 37776+ && au_test_fs_trunc_xino(au_br_sb(br)))
1facf9fc 37777+ xino_try_trunc(sb, br);
37778+ return 0; /* success */
37779+ }
37780+
acd2b654 37781+out:
1facf9fc 37782+ AuIOErr("write failed (%d)\n", err);
37783+ return -EIO;
37784+}
37785+
062440b3
AM
37786+static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37787+ size_t size, loff_t *pos);
1facf9fc 37788+
062440b3
AM
37789+/* todo: unnecessary to support mmap_sem since kernel-space? */
37790+ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
37791+ loff_t *pos)
37792+{
37793+ ssize_t err;
37794+ mm_segment_t oldfs;
37795+ union {
37796+ void *k;
37797+ char __user *u;
37798+ } buf;
37799+ int i;
37800+ const int prevent_endless = 10;
1facf9fc 37801+
062440b3
AM
37802+ i = 0;
37803+ buf.k = kbuf;
37804+ oldfs = get_fs();
37805+ set_fs(KERNEL_DS);
37806+ do {
37807+ err = func(file, buf.u, size, pos);
37808+ if (err == -EINTR
37809+ && !au_wkq_test()
37810+ && fatal_signal_pending(current)) {
37811+ set_fs(oldfs);
37812+ err = xino_fread_wkq(func, file, kbuf, size, pos);
37813+ BUG_ON(err == -EINTR);
37814+ oldfs = get_fs();
37815+ set_fs(KERNEL_DS);
37816+ }
37817+ } while (i++ < prevent_endless
37818+ && (err == -EAGAIN || err == -EINTR));
37819+ set_fs(oldfs);
37820+
37821+#if 0 /* reserved for future use */
37822+ if (err > 0)
37823+ fsnotify_access(file->f_path.dentry);
37824+#endif
37825+
37826+ return err;
37827+}
37828+
37829+struct xino_fread_args {
37830+ ssize_t *errp;
37831+ vfs_readf_t func;
37832+ struct file *file;
37833+ void *buf;
37834+ size_t size;
37835+ loff_t *pos;
37836+};
37837+
37838+static void call_xino_fread(void *args)
37839+{
37840+ struct xino_fread_args *a = args;
37841+ *a->errp = xino_fread(a->func, a->file, a->buf, a->size, a->pos);
37842+}
37843+
37844+static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37845+ size_t size, loff_t *pos)
37846+{
37847+ ssize_t err;
37848+ int wkq_err;
37849+ struct xino_fread_args args = {
37850+ .errp = &err,
37851+ .func = func,
37852+ .file = file,
37853+ .buf = buf,
37854+ .size = size,
37855+ .pos = pos
37856+ };
37857+
37858+ wkq_err = au_wkq_wait(call_xino_fread, &args);
37859+ if (unlikely(wkq_err))
37860+ err = wkq_err;
37861+
37862+ return err;
37863+}
37864+
37865+static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37866+ size_t size, loff_t *pos);
37867+
37868+static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
37869+ size_t size, loff_t *pos)
37870+{
37871+ ssize_t err;
37872+ mm_segment_t oldfs;
37873+ union {
37874+ void *k;
37875+ const char __user *u;
37876+ } buf;
37877+ int i;
37878+ const int prevent_endless = 10;
37879+
37880+ i = 0;
37881+ buf.k = kbuf;
37882+ oldfs = get_fs();
37883+ set_fs(KERNEL_DS);
37884+ do {
37885+ err = func(file, buf.u, size, pos);
37886+ if (err == -EINTR
37887+ && !au_wkq_test()
37888+ && fatal_signal_pending(current)) {
37889+ set_fs(oldfs);
37890+ err = xino_fwrite_wkq(func, file, kbuf, size, pos);
37891+ BUG_ON(err == -EINTR);
37892+ oldfs = get_fs();
37893+ set_fs(KERNEL_DS);
37894+ }
37895+ } while (i++ < prevent_endless
37896+ && (err == -EAGAIN || err == -EINTR));
37897+ set_fs(oldfs);
37898+
37899+#if 0 /* reserved for future use */
37900+ if (err > 0)
37901+ fsnotify_modify(file->f_path.dentry);
37902+#endif
37903+
37904+ return err;
37905+}
37906+
37907+struct do_xino_fwrite_args {
37908+ ssize_t *errp;
37909+ vfs_writef_t func;
37910+ struct file *file;
37911+ void *buf;
37912+ size_t size;
37913+ loff_t *pos;
37914+};
37915+
37916+static void call_do_xino_fwrite(void *args)
37917+{
37918+ struct do_xino_fwrite_args *a = args;
37919+ *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
37920+}
37921+
37922+static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37923+ size_t size, loff_t *pos)
37924+{
37925+ ssize_t err;
37926+ int wkq_err;
37927+ struct do_xino_fwrite_args args = {
37928+ .errp = &err,
37929+ .func = func,
37930+ .file = file,
37931+ .buf = buf,
37932+ .size = size,
37933+ .pos = pos
37934+ };
37935+
37936+ /*
37937+ * it breaks RLIMIT_FSIZE and normal user's limit,
37938+ * users should care about quota and real 'filesystem full.'
37939+ */
37940+ wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
37941+ if (unlikely(wkq_err))
37942+ err = wkq_err;
37943+
37944+ return err;
37945+}
37946+
37947+ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
37948+ size_t size, loff_t *pos)
37949+{
37950+ ssize_t err;
37951+
37952+ if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
37953+ lockdep_off();
37954+ err = do_xino_fwrite(func, file, buf, size, pos);
37955+ lockdep_on();
37956+ } else {
37957+ lockdep_off();
37958+ err = xino_fwrite_wkq(func, file, buf, size, pos);
37959+ lockdep_on();
37960+ }
37961+
37962+ return err;
37963+}
37964+
37965+/* ---------------------------------------------------------------------- */
37966+
37967+/*
37968+ * inode number bitmap
37969+ */
1facf9fc 37970+static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
37971+static ino_t xib_calc_ino(unsigned long pindex, int bit)
37972+{
37973+ ino_t ino;
37974+
37975+ AuDebugOn(bit < 0 || page_bits <= bit);
37976+ ino = AUFS_FIRST_INO + pindex * page_bits + bit;
37977+ return ino;
37978+}
37979+
37980+static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
37981+{
37982+ AuDebugOn(ino < AUFS_FIRST_INO);
37983+ ino -= AUFS_FIRST_INO;
37984+ *pindex = ino / page_bits;
37985+ *bit = ino % page_bits;
37986+}
37987+
37988+static int xib_pindex(struct super_block *sb, unsigned long pindex)
37989+{
37990+ int err;
37991+ loff_t pos;
37992+ ssize_t sz;
37993+ struct au_sbinfo *sbinfo;
37994+ struct file *xib;
37995+ unsigned long *p;
37996+
37997+ sbinfo = au_sbi(sb);
37998+ MtxMustLock(&sbinfo->si_xib_mtx);
37999+ AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
38000+ || !au_opt_test(sbinfo->si_mntflags, XINO));
38001+
38002+ if (pindex == sbinfo->si_xib_last_pindex)
38003+ return 0;
38004+
38005+ xib = sbinfo->si_xib;
38006+ p = sbinfo->si_xib_buf;
38007+ pos = sbinfo->si_xib_last_pindex;
38008+ pos *= PAGE_SIZE;
38009+ sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
38010+ if (unlikely(sz != PAGE_SIZE))
38011+ goto out;
38012+
38013+ pos = pindex;
38014+ pos *= PAGE_SIZE;
c06a8ce3 38015+ if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
1facf9fc 38016+ sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
38017+ else {
38018+ memset(p, 0, PAGE_SIZE);
38019+ sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
38020+ }
38021+ if (sz == PAGE_SIZE) {
38022+ sbinfo->si_xib_last_pindex = pindex;
38023+ return 0; /* success */
38024+ }
38025+
4f0767ce 38026+out:
b752ccd1
AM
38027+ AuIOErr1("write failed (%zd)\n", sz);
38028+ err = sz;
38029+ if (sz >= 0)
38030+ err = -EIO;
38031+ return err;
38032+}
38033+
b752ccd1
AM
38034+static void au_xib_clear_bit(struct inode *inode)
38035+{
38036+ int err, bit;
38037+ unsigned long pindex;
38038+ struct super_block *sb;
38039+ struct au_sbinfo *sbinfo;
38040+
38041+ AuDebugOn(inode->i_nlink);
38042+
38043+ sb = inode->i_sb;
38044+ xib_calc_bit(inode->i_ino, &pindex, &bit);
38045+ AuDebugOn(page_bits <= bit);
38046+ sbinfo = au_sbi(sb);
38047+ mutex_lock(&sbinfo->si_xib_mtx);
38048+ err = xib_pindex(sb, pindex);
38049+ if (!err) {
38050+ clear_bit(bit, sbinfo->si_xib_buf);
38051+ sbinfo->si_xib_next_bit = bit;
38052+ }
38053+ mutex_unlock(&sbinfo->si_xib_mtx);
38054+}
38055+
1facf9fc 38056+/* ---------------------------------------------------------------------- */
38057+
1facf9fc 38058+/*
062440b3 38059+ * truncate a xino bitmap file
1facf9fc 38060+ */
1facf9fc 38061+
38062+/* todo: slow */
38063+static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
38064+{
38065+ int err, bit;
38066+ ssize_t sz;
38067+ unsigned long pindex;
38068+ loff_t pos, pend;
38069+ struct au_sbinfo *sbinfo;
5527c038 38070+ vfs_readf_t func;
1facf9fc 38071+ ino_t *ino;
38072+ unsigned long *p;
38073+
38074+ err = 0;
38075+ sbinfo = au_sbi(sb);
dece6358 38076+ MtxMustLock(&sbinfo->si_xib_mtx);
1facf9fc 38077+ p = sbinfo->si_xib_buf;
38078+ func = sbinfo->si_xread;
c06a8ce3 38079+ pend = vfsub_f_size_read(file);
1facf9fc 38080+ pos = 0;
38081+ while (pos < pend) {
38082+ sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
38083+ err = sz;
38084+ if (unlikely(sz <= 0))
38085+ goto out;
38086+
38087+ err = 0;
38088+ for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
38089+ if (unlikely(*ino < AUFS_FIRST_INO))
38090+ continue;
38091+
38092+ xib_calc_bit(*ino, &pindex, &bit);
38093+ AuDebugOn(page_bits <= bit);
38094+ err = xib_pindex(sb, pindex);
38095+ if (!err)
38096+ set_bit(bit, p);
38097+ else
38098+ goto out;
38099+ }
38100+ }
38101+
4f0767ce 38102+out:
1facf9fc 38103+ return err;
38104+}
38105+
38106+static int xib_restore(struct super_block *sb)
38107+{
acd2b654
AM
38108+ int err, i;
38109+ unsigned int nfile;
5afbbe0d 38110+ aufs_bindex_t bindex, bbot;
1facf9fc 38111+ void *page;
062440b3 38112+ struct au_branch *br;
acd2b654
AM
38113+ struct au_xino *xi;
38114+ struct file *file;
1facf9fc 38115+
38116+ err = -ENOMEM;
38117+ page = (void *)__get_free_page(GFP_NOFS);
38118+ if (unlikely(!page))
38119+ goto out;
38120+
38121+ err = 0;
5afbbe0d
AM
38122+ bbot = au_sbbot(sb);
38123+ for (bindex = 0; !err && bindex <= bbot; bindex++)
062440b3
AM
38124+ if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
38125+ br = au_sbr(sb, bindex);
acd2b654
AM
38126+ xi = br->br_xino;
38127+ nfile = xi->xi_nfile;
38128+ for (i = 0; i < nfile; i++) {
38129+ file = au_xino_file(xi, i);
38130+ if (file)
38131+ err = do_xib_restore(sb, file, page);
38132+ }
062440b3
AM
38133+ } else
38134+ AuDbg("skip shared b%d\n", bindex);
1c60b727 38135+ free_page((unsigned long)page);
1facf9fc 38136+
4f0767ce 38137+out:
1facf9fc 38138+ return err;
38139+}
38140+
38141+int au_xib_trunc(struct super_block *sb)
38142+{
38143+ int err;
38144+ ssize_t sz;
38145+ loff_t pos;
1facf9fc 38146+ struct au_sbinfo *sbinfo;
38147+ unsigned long *p;
38148+ struct file *file;
38149+
dece6358
AM
38150+ SiMustWriteLock(sb);
38151+
1facf9fc 38152+ err = 0;
38153+ sbinfo = au_sbi(sb);
38154+ if (!au_opt_test(sbinfo->si_mntflags, XINO))
38155+ goto out;
38156+
38157+ file = sbinfo->si_xib;
c06a8ce3 38158+ if (vfsub_f_size_read(file) <= PAGE_SIZE)
1facf9fc 38159+ goto out;
38160+
062440b3 38161+ file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
1facf9fc 38162+ err = PTR_ERR(file);
38163+ if (IS_ERR(file))
38164+ goto out;
38165+ fput(sbinfo->si_xib);
38166+ sbinfo->si_xib = file;
38167+
38168+ p = sbinfo->si_xib_buf;
38169+ memset(p, 0, PAGE_SIZE);
38170+ pos = 0;
38171+ sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
38172+ if (unlikely(sz != PAGE_SIZE)) {
38173+ err = sz;
38174+ AuIOErr("err %d\n", err);
38175+ if (sz >= 0)
38176+ err = -EIO;
38177+ goto out;
38178+ }
38179+
38180+ mutex_lock(&sbinfo->si_xib_mtx);
38181+ /* mnt_want_write() is unnecessary here */
38182+ err = xib_restore(sb);
38183+ mutex_unlock(&sbinfo->si_xib_mtx);
38184+
38185+out:
38186+ return err;
38187+}
38188+
38189+/* ---------------------------------------------------------------------- */
38190+
acd2b654 38191+struct au_xino *au_xino_alloc(unsigned int nfile)
062440b3
AM
38192+{
38193+ struct au_xino *xi;
38194+
38195+ xi = kzalloc(sizeof(*xi), GFP_NOFS);
38196+ if (unlikely(!xi))
38197+ goto out;
acd2b654
AM
38198+ xi->xi_nfile = nfile;
38199+ xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38200+ if (unlikely(!xi->xi_file))
38201+ goto out_free;
062440b3
AM
38202+
38203+ xi->xi_nondir.total = 8; /* initial size */
38204+ xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38205+ GFP_NOFS);
38206+ if (unlikely(!xi->xi_nondir.array))
acd2b654 38207+ goto out_file;
062440b3
AM
38208+
38209+ spin_lock_init(&xi->xi_nondir.spin);
38210+ init_waitqueue_head(&xi->xi_nondir.wqh);
acd2b654 38211+ mutex_init(&xi->xi_mtx);
e49925d1 38212+ INIT_HLIST_BL_HEAD(&xi->xi_writing);
062440b3
AM
38213+ atomic_set(&xi->xi_truncating, 0);
38214+ kref_init(&xi->xi_kref);
38215+ goto out; /* success */
38216+
acd2b654 38217+out_file:
e49925d1 38218+ au_kfree_try_rcu(xi->xi_file);
062440b3 38219+out_free:
e49925d1 38220+ au_kfree_rcu(xi);
062440b3
AM
38221+ xi = NULL;
38222+out:
38223+ return xi;
38224+}
38225+
acd2b654 38226+static int au_xino_init(struct au_branch *br, int idx, struct file *file)
062440b3
AM
38227+{
38228+ int err;
38229+ struct au_xino *xi;
38230+
38231+ err = 0;
acd2b654 38232+ xi = au_xino_alloc(idx + 1);
062440b3
AM
38233+ if (unlikely(!xi)) {
38234+ err = -ENOMEM;
38235+ goto out;
38236+ }
38237+
acd2b654
AM
38238+ if (file)
38239+ get_file(file);
38240+ xi->xi_file[idx] = file;
062440b3
AM
38241+ AuDebugOn(br->br_xino);
38242+ br->br_xino = xi;
38243+
38244+out:
38245+ return err;
38246+}
38247+
38248+static void au_xino_release(struct kref *kref)
38249+{
38250+ struct au_xino *xi;
38251+ int i;
e49925d1
JR
38252+ unsigned long ul;
38253+ struct hlist_bl_head *hbl;
38254+ struct hlist_bl_node *pos, *n;
38255+ struct au_xi_writing *p;
062440b3
AM
38256+
38257+ xi = container_of(kref, struct au_xino, xi_kref);
acd2b654
AM
38258+ for (i = 0; i < xi->xi_nfile; i++)
38259+ if (xi->xi_file[i])
38260+ fput(xi->xi_file[i]);
062440b3
AM
38261+ for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38262+ AuDebugOn(xi->xi_nondir.array[i]);
acd2b654 38263+ mutex_destroy(&xi->xi_mtx);
e49925d1
JR
38264+ hbl = &xi->xi_writing;
38265+ ul = au_hbl_count(hbl);
38266+ if (unlikely(ul)) {
38267+ pr_warn("xi_writing %lu\n", ul);
38268+ hlist_bl_lock(hbl);
38269+ hlist_bl_for_each_entry_safe (p, pos, n, hbl, node) {
38270+ hlist_bl_del(&p->node);
38271+ au_kfree_rcu(p);
38272+ }
38273+ hlist_bl_unlock(hbl);
38274+ }
38275+ au_kfree_try_rcu(xi->xi_file);
38276+ au_kfree_try_rcu(xi->xi_nondir.array);
38277+ au_kfree_rcu(xi);
062440b3
AM
38278+}
38279+
38280+int au_xino_put(struct au_branch *br)
38281+{
38282+ int ret;
38283+ struct au_xino *xi;
38284+
38285+ ret = 0;
38286+ xi = br->br_xino;
38287+ if (xi) {
38288+ br->br_xino = NULL;
38289+ ret = kref_put(&xi->xi_kref, au_xino_release);
38290+ }
38291+
38292+ return ret;
38293+}
38294+
062440b3
AM
38295+/* ---------------------------------------------------------------------- */
38296+
1facf9fc 38297+/*
38298+ * xino mount option handlers
38299+ */
1facf9fc 38300+
38301+/* xino bitmap */
38302+static void xino_clear_xib(struct super_block *sb)
38303+{
38304+ struct au_sbinfo *sbinfo;
38305+
dece6358
AM
38306+ SiMustWriteLock(sb);
38307+
1facf9fc 38308+ sbinfo = au_sbi(sb);
062440b3 38309+ /* unnecessary to clear sbinfo->si_xread and ->si_xwrite */
1facf9fc 38310+ if (sbinfo->si_xib)
38311+ fput(sbinfo->si_xib);
38312+ sbinfo->si_xib = NULL;
f0c0a007 38313+ if (sbinfo->si_xib_buf)
1c60b727 38314+ free_page((unsigned long)sbinfo->si_xib_buf);
1facf9fc 38315+ sbinfo->si_xib_buf = NULL;
38316+}
38317+
062440b3 38318+static int au_xino_set_xib(struct super_block *sb, struct path *path)
1facf9fc 38319+{
38320+ int err;
38321+ loff_t pos;
38322+ struct au_sbinfo *sbinfo;
38323+ struct file *file;
acd2b654 38324+ struct super_block *xi_sb;
1facf9fc 38325+
dece6358
AM
38326+ SiMustWriteLock(sb);
38327+
1facf9fc 38328+ sbinfo = au_sbi(sb);
062440b3 38329+ file = au_xino_create2(sb, path, sbinfo->si_xib);
1facf9fc 38330+ err = PTR_ERR(file);
38331+ if (IS_ERR(file))
38332+ goto out;
38333+ if (sbinfo->si_xib)
38334+ fput(sbinfo->si_xib);
38335+ sbinfo->si_xib = file;
5527c038
JR
38336+ sbinfo->si_xread = vfs_readf(file);
38337+ sbinfo->si_xwrite = vfs_writef(file);
acd2b654
AM
38338+ xi_sb = file_inode(file)->i_sb;
38339+ sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38340+ if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38341+ err = -EIO;
38342+ pr_err("s_maxbytes(%llu) on %s is too small\n",
38343+ (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38344+ goto out_unset;
38345+ }
38346+ sbinfo->si_ximaxent /= sizeof(ino_t);
1facf9fc 38347+
38348+ err = -ENOMEM;
38349+ if (!sbinfo->si_xib_buf)
38350+ sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38351+ if (unlikely(!sbinfo->si_xib_buf))
38352+ goto out_unset;
38353+
38354+ sbinfo->si_xib_last_pindex = 0;
38355+ sbinfo->si_xib_next_bit = 0;
c06a8ce3 38356+ if (vfsub_f_size_read(file) < PAGE_SIZE) {
1facf9fc 38357+ pos = 0;
38358+ err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
38359+ PAGE_SIZE, &pos);
38360+ if (unlikely(err != PAGE_SIZE))
38361+ goto out_free;
38362+ }
38363+ err = 0;
38364+ goto out; /* success */
38365+
4f0767ce 38366+out_free:
f0c0a007 38367+ if (sbinfo->si_xib_buf)
1c60b727 38368+ free_page((unsigned long)sbinfo->si_xib_buf);
b752ccd1
AM
38369+ sbinfo->si_xib_buf = NULL;
38370+ if (err >= 0)
38371+ err = -EIO;
4f0767ce 38372+out_unset:
b752ccd1
AM
38373+ fput(sbinfo->si_xib);
38374+ sbinfo->si_xib = NULL;
4f0767ce 38375+out:
062440b3 38376+ AuTraceErr(err);
b752ccd1 38377+ return err;
1facf9fc 38378+}
38379+
b752ccd1
AM
38380+/* xino for each branch */
38381+static void xino_clear_br(struct super_block *sb)
38382+{
5afbbe0d 38383+ aufs_bindex_t bindex, bbot;
b752ccd1 38384+ struct au_branch *br;
1facf9fc 38385+
5afbbe0d
AM
38386+ bbot = au_sbbot(sb);
38387+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1 38388+ br = au_sbr(sb, bindex);
062440b3
AM
38389+ AuDebugOn(!br);
38390+ au_xino_put(br);
38391+ }
38392+}
38393+
38394+static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38395+ aufs_bindex_t bshared)
38396+{
38397+ struct au_branch *brshared;
b752ccd1 38398+
062440b3
AM
38399+ brshared = au_sbr(sb, bshared);
38400+ AuDebugOn(!brshared->br_xino);
38401+ AuDebugOn(!brshared->br_xino->xi_file);
38402+ if (br->br_xino != brshared->br_xino) {
38403+ au_xino_get(brshared);
38404+ au_xino_put(br);
38405+ br->br_xino = brshared->br_xino;
b752ccd1
AM
38406+ }
38407+}
38408+
062440b3
AM
38409+struct au_xino_do_set_br {
38410+ vfs_writef_t writef;
38411+ struct au_branch *br;
38412+ ino_t h_ino;
38413+ aufs_bindex_t bshared;
38414+};
38415+
38416+static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38417+ struct au_xino_do_set_br *args)
1facf9fc 38418+{
38419+ int err;
acd2b654 38420+ struct au_xi_calc calc;
062440b3 38421+ struct file *file;
acd2b654
AM
38422+ struct au_branch *br;
38423+ struct au_xi_new xinew = {
38424+ .base = path
38425+ };
062440b3 38426+
acd2b654
AM
38427+ br = args->br;
38428+ xinew.xi = br->br_xino;
38429+ au_xi_calc(sb, args->h_ino, &calc);
38430+ xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38431+ if (args->bshared >= 0)
062440b3 38432+ /* shared xino */
acd2b654
AM
38433+ au_xino_set_br_shared(sb, br, args->bshared);
38434+ else if (!xinew.xi) {
38435+ /* new xino */
38436+ err = au_xino_init(br, calc.idx, xinew.copy_src);
38437+ if (unlikely(err))
38438+ goto out;
062440b3
AM
38439+ }
38440+
acd2b654
AM
38441+ /* force re-creating */
38442+ xinew.xi = br->br_xino;
38443+ xinew.idx = calc.idx;
38444+ mutex_lock(&xinew.xi->xi_mtx);
38445+ file = au_xi_new(sb, &xinew);
38446+ mutex_unlock(&xinew.xi->xi_mtx);
062440b3
AM
38447+ err = PTR_ERR(file);
38448+ if (IS_ERR(file))
38449+ goto out;
acd2b654
AM
38450+ AuDebugOn(!file);
38451+
38452+ err = au_xino_do_write(args->writef, file, &calc, AUFS_ROOT_INO);
38453+ if (unlikely(err))
38454+ au_xino_put(br);
062440b3 38455+
062440b3
AM
38456+out:
38457+ AuTraceErr(err);
38458+ return err;
38459+}
38460+
38461+static int au_xino_set_br(struct super_block *sb, struct path *path)
38462+{
38463+ int err;
38464+ aufs_bindex_t bindex, bbot;
38465+ struct au_xino_do_set_br args;
b752ccd1 38466+ struct inode *inode;
1facf9fc 38467+
b752ccd1
AM
38468+ SiMustWriteLock(sb);
38469+
5afbbe0d 38470+ bbot = au_sbbot(sb);
5527c038 38471+ inode = d_inode(sb->s_root);
062440b3
AM
38472+ args.writef = au_sbi(sb)->si_xwrite;
38473+ for (bindex = 0; bindex <= bbot; bindex++) {
38474+ args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38475+ args.br = au_sbr(sb, bindex);
38476+ args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38477+ err = au_xino_do_set_br(sb, path, &args);
b752ccd1 38478+ if (unlikely(err))
062440b3 38479+ break;
b752ccd1 38480+ }
1facf9fc 38481+
062440b3 38482+ AuTraceErr(err);
1facf9fc 38483+ return err;
38484+}
b752ccd1
AM
38485+
38486+void au_xino_clr(struct super_block *sb)
38487+{
38488+ struct au_sbinfo *sbinfo;
38489+
38490+ au_xigen_clr(sb);
38491+ xino_clear_xib(sb);
38492+ xino_clear_br(sb);
062440b3 38493+ dbgaufs_brs_del(sb, 0);
b752ccd1
AM
38494+ sbinfo = au_sbi(sb);
38495+ /* lvalue, do not call au_mntflags() */
38496+ au_opt_clr(sbinfo->si_mntflags, XINO);
38497+}
38498+
062440b3 38499+int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
b752ccd1
AM
38500+{
38501+ int err, skip;
062440b3 38502+ struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
b752ccd1
AM
38503+ struct qstr *dname, *cur_name;
38504+ struct file *cur_xino;
b752ccd1 38505+ struct au_sbinfo *sbinfo;
062440b3 38506+ struct path *path, *cur_path;
b752ccd1
AM
38507+
38508+ SiMustWriteLock(sb);
38509+
38510+ err = 0;
38511+ sbinfo = au_sbi(sb);
062440b3
AM
38512+ path = &xiopt->file->f_path;
38513+ dentry = path->dentry;
38514+ parent = dget_parent(dentry);
b752ccd1
AM
38515+ if (remount) {
38516+ skip = 0;
b752ccd1
AM
38517+ cur_xino = sbinfo->si_xib;
38518+ if (cur_xino) {
062440b3
AM
38519+ cur_path = &cur_xino->f_path;
38520+ cur_dentry = cur_path->dentry;
38521+ cur_parent = dget_parent(cur_dentry);
38522+ cur_name = &cur_dentry->d_name;
38523+ dname = &dentry->d_name;
b752ccd1 38524+ skip = (cur_parent == parent
38d290e6 38525+ && au_qstreq(dname, cur_name));
b752ccd1
AM
38526+ dput(cur_parent);
38527+ }
38528+ if (skip)
38529+ goto out;
38530+ }
38531+
38532+ au_opt_set(sbinfo->si_mntflags, XINO);
062440b3
AM
38533+ err = au_xino_set_xib(sb, path);
38534+ /* si_x{read,write} are set */
b752ccd1 38535+ if (!err)
062440b3 38536+ err = au_xigen_set(sb, path);
b752ccd1 38537+ if (!err)
062440b3
AM
38538+ err = au_xino_set_br(sb, path);
38539+ if (!err) {
38540+ dbgaufs_brs_add(sb, 0, /*topdown*/1);
b752ccd1 38541+ goto out; /* success */
062440b3 38542+ }
b752ccd1
AM
38543+
38544+ /* reset all */
062440b3
AM
38545+ AuIOErr("failed setting xino(%d).\n", err);
38546+ au_xino_clr(sb);
b752ccd1 38547+
4f0767ce 38548+out:
b752ccd1
AM
38549+ dput(parent);
38550+ return err;
38551+}
38552+
b752ccd1
AM
38553+/*
38554+ * create a xinofile at the default place/path.
38555+ */
38556+struct file *au_xino_def(struct super_block *sb)
38557+{
38558+ struct file *file;
38559+ char *page, *p;
38560+ struct au_branch *br;
38561+ struct super_block *h_sb;
38562+ struct path path;
5afbbe0d 38563+ aufs_bindex_t bbot, bindex, bwr;
b752ccd1
AM
38564+
38565+ br = NULL;
5afbbe0d 38566+ bbot = au_sbbot(sb);
b752ccd1 38567+ bwr = -1;
5afbbe0d 38568+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1
AM
38569+ br = au_sbr(sb, bindex);
38570+ if (au_br_writable(br->br_perm)
86dc4139 38571+ && !au_test_fs_bad_xino(au_br_sb(br))) {
b752ccd1
AM
38572+ bwr = bindex;
38573+ break;
38574+ }
38575+ }
38576+
7f207e10
AM
38577+ if (bwr >= 0) {
38578+ file = ERR_PTR(-ENOMEM);
537831f9 38579+ page = (void *)__get_free_page(GFP_NOFS);
7f207e10
AM
38580+ if (unlikely(!page))
38581+ goto out;
86dc4139 38582+ path.mnt = au_br_mnt(br);
7f207e10
AM
38583+ path.dentry = au_h_dptr(sb->s_root, bwr);
38584+ p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38585+ file = (void *)p;
38586+ if (!IS_ERR(p)) {
38587+ strcat(p, "/" AUFS_XINO_FNAME);
38588+ AuDbg("%s\n", p);
38589+ file = au_xino_create(sb, p, /*silent*/0);
7f207e10 38590+ }
1c60b727 38591+ free_page((unsigned long)page);
7f207e10
AM
38592+ } else {
38593+ file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
38594+ if (IS_ERR(file))
38595+ goto out;
2000de60 38596+ h_sb = file->f_path.dentry->d_sb;
7f207e10
AM
38597+ if (unlikely(au_test_fs_bad_xino(h_sb))) {
38598+ pr_err("xino doesn't support %s(%s)\n",
38599+ AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38600+ fput(file);
38601+ file = ERR_PTR(-EINVAL);
38602+ }
7f207e10 38603+ }
0c5527e5 38604+
7f207e10
AM
38605+out:
38606+ return file;
38607+}
38608+
38609+/* ---------------------------------------------------------------------- */
38610+
062440b3
AM
38611+/*
38612+ * initialize the xinofile for the specified branch @br
38613+ * at the place/path where @base_file indicates.
38614+ * test whether another branch is on the same filesystem or not,
38615+ * if found then share the xinofile with another branch.
38616+ */
38617+int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38618+ struct path *base)
7f207e10
AM
38619+{
38620+ int err;
062440b3
AM
38621+ struct au_xino_do_set_br args = {
38622+ .h_ino = h_ino,
38623+ .br = br
38624+ };
7f207e10 38625+
062440b3
AM
38626+ args.writef = au_sbi(sb)->si_xwrite;
38627+ args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38628+ au_br_sb(br));
38629+ err = au_xino_do_set_br(sb, base, &args);
79b8bda9 38630+ if (unlikely(err))
062440b3 38631+ au_xino_put(br);
7f207e10 38632+
7f207e10
AM
38633+ return err;
38634+}
521ced18
JR
38635+
38636+/* ---------------------------------------------------------------------- */
38637+
062440b3
AM
38638+/*
38639+ * get an unused inode number from bitmap
38640+ */
38641+ino_t au_xino_new_ino(struct super_block *sb)
38642+{
38643+ ino_t ino;
38644+ unsigned long *p, pindex, ul, pend;
38645+ struct au_sbinfo *sbinfo;
38646+ struct file *file;
38647+ int free_bit, err;
38648+
38649+ if (!au_opt_test(au_mntflags(sb), XINO))
38650+ return iunique(sb, AUFS_FIRST_INO);
38651+
38652+ sbinfo = au_sbi(sb);
38653+ mutex_lock(&sbinfo->si_xib_mtx);
38654+ p = sbinfo->si_xib_buf;
38655+ free_bit = sbinfo->si_xib_next_bit;
38656+ if (free_bit < page_bits && !test_bit(free_bit, p))
38657+ goto out; /* success */
38658+ free_bit = find_first_zero_bit(p, page_bits);
38659+ if (free_bit < page_bits)
38660+ goto out; /* success */
38661+
38662+ pindex = sbinfo->si_xib_last_pindex;
38663+ for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38664+ err = xib_pindex(sb, ul);
38665+ if (unlikely(err))
38666+ goto out_err;
38667+ free_bit = find_first_zero_bit(p, page_bits);
38668+ if (free_bit < page_bits)
38669+ goto out; /* success */
38670+ }
38671+
38672+ file = sbinfo->si_xib;
38673+ pend = vfsub_f_size_read(file) / PAGE_SIZE;
38674+ for (ul = pindex + 1; ul <= pend; ul++) {
38675+ err = xib_pindex(sb, ul);
38676+ if (unlikely(err))
38677+ goto out_err;
38678+ free_bit = find_first_zero_bit(p, page_bits);
38679+ if (free_bit < page_bits)
38680+ goto out; /* success */
38681+ }
38682+ BUG();
38683+
38684+out:
38685+ set_bit(free_bit, p);
38686+ sbinfo->si_xib_next_bit = free_bit + 1;
38687+ pindex = sbinfo->si_xib_last_pindex;
38688+ mutex_unlock(&sbinfo->si_xib_mtx);
38689+ ino = xib_calc_ino(pindex, free_bit);
38690+ AuDbg("i%lu\n", (unsigned long)ino);
38691+ return ino;
38692+out_err:
38693+ mutex_unlock(&sbinfo->si_xib_mtx);
38694+ AuDbg("i0\n");
38695+ return 0;
38696+}
38697+
38698+/* for s_op->delete_inode() */
38699+void au_xino_delete_inode(struct inode *inode, const int unlinked)
521ced18 38700+{
062440b3
AM
38701+ int err;
38702+ unsigned int mnt_flags;
38703+ aufs_bindex_t bindex, bbot, bi;
38704+ unsigned char try_trunc;
38705+ struct au_iinfo *iinfo;
38706+ struct super_block *sb;
38707+ struct au_hinode *hi;
38708+ struct inode *h_inode;
38709+ struct au_branch *br;
38710+ vfs_writef_t xwrite;
acd2b654
AM
38711+ struct au_xi_calc calc;
38712+ struct file *file;
521ced18 38713+
062440b3 38714+ AuDebugOn(au_is_bad_inode(inode));
521ced18 38715+
062440b3
AM
38716+ sb = inode->i_sb;
38717+ mnt_flags = au_mntflags(sb);
38718+ if (!au_opt_test(mnt_flags, XINO)
38719+ || inode->i_ino == AUFS_ROOT_INO)
38720+ return;
38721+
38722+ if (unlinked) {
38723+ au_xigen_inc(inode);
38724+ au_xib_clear_bit(inode);
38725+ }
38726+
38727+ iinfo = au_ii(inode);
38728+ bindex = iinfo->ii_btop;
38729+ if (bindex < 0)
38730+ return;
38731+
38732+ xwrite = au_sbi(sb)->si_xwrite;
38733+ try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38734+ hi = au_hinode(iinfo, bindex);
38735+ bbot = iinfo->ii_bbot;
38736+ for (; bindex <= bbot; bindex++, hi++) {
38737+ h_inode = hi->hi_inode;
38738+ if (!h_inode
38739+ || (!unlinked && h_inode->i_nlink))
38740+ continue;
38741+
38742+ /* inode may not be revalidated */
38743+ bi = au_br_index(sb, hi->hi_id);
38744+ if (bi < 0)
38745+ continue;
38746+
38747+ br = au_sbr(sb, bi);
acd2b654
AM
38748+ au_xi_calc(sb, h_inode->i_ino, &calc);
38749+ file = au_xino_file(br->br_xino, calc.idx);
38750+ if (IS_ERR_OR_NULL(file))
38751+ continue;
38752+
38753+ err = au_xino_do_write(xwrite, file, &calc, /*ino*/0);
062440b3
AM
38754+ if (!err && try_trunc
38755+ && au_test_fs_trunc_xino(au_br_sb(br)))
38756+ xino_try_trunc(sb, br);
38757+ }
521ced18
JR
38758+}
38759+
062440b3
AM
38760+/* ---------------------------------------------------------------------- */
38761+
38762+static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
521ced18
JR
38763+{
38764+ int found, total, i;
38765+
38766+ found = -1;
062440b3 38767+ total = xi->xi_nondir.total;
521ced18 38768+ for (i = 0; i < total; i++) {
062440b3 38769+ if (xi->xi_nondir.array[i] != h_ino)
521ced18
JR
38770+ continue;
38771+ found = i;
38772+ break;
38773+ }
38774+
38775+ return found;
38776+}
38777+
062440b3 38778+static int au_xinondir_expand(struct au_xino *xi)
521ced18
JR
38779+{
38780+ int err, sz;
38781+ ino_t *p;
38782+
38783+ BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38784+
38785+ err = -ENOMEM;
062440b3 38786+ sz = xi->xi_nondir.total * sizeof(ino_t);
521ced18
JR
38787+ if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38788+ goto out;
062440b3 38789+ p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
521ced18
JR
38790+ /*may_shrink*/0);
38791+ if (p) {
062440b3
AM
38792+ xi->xi_nondir.array = p;
38793+ xi->xi_nondir.total <<= 1;
38794+ AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
521ced18
JR
38795+ err = 0;
38796+ }
38797+
38798+out:
38799+ return err;
38800+}
38801+
062440b3
AM
38802+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38803+ ino_t h_ino, int idx)
38804+{
38805+ struct au_xino *xi;
38806+
38807+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38808+ xi = au_sbr(sb, bindex)->br_xino;
38809+ AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38810+
38811+ spin_lock(&xi->xi_nondir.spin);
38812+ AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38813+ xi->xi_nondir.array[idx] = 0;
38814+ spin_unlock(&xi->xi_nondir.spin);
38815+ wake_up_all(&xi->xi_nondir.wqh);
38816+}
38817+
521ced18
JR
38818+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38819+ int *idx)
38820+{
38821+ int err, found, empty;
062440b3 38822+ struct au_xino *xi;
521ced18
JR
38823+
38824+ err = 0;
38825+ *idx = -1;
38826+ if (!au_opt_test(au_mntflags(sb), XINO))
38827+ goto out; /* no xino */
38828+
062440b3 38829+ xi = au_sbr(sb, bindex)->br_xino;
521ced18
JR
38830+
38831+again:
062440b3
AM
38832+ spin_lock(&xi->xi_nondir.spin);
38833+ found = au_xinondir_find(xi, h_ino);
521ced18 38834+ if (found == -1) {
062440b3 38835+ empty = au_xinondir_find(xi, /*h_ino*/0);
521ced18 38836+ if (empty == -1) {
062440b3
AM
38837+ empty = xi->xi_nondir.total;
38838+ err = au_xinondir_expand(xi);
521ced18
JR
38839+ if (unlikely(err))
38840+ goto out_unlock;
38841+ }
062440b3 38842+ xi->xi_nondir.array[empty] = h_ino;
521ced18
JR
38843+ *idx = empty;
38844+ } else {
062440b3
AM
38845+ spin_unlock(&xi->xi_nondir.spin);
38846+ wait_event(xi->xi_nondir.wqh,
38847+ xi->xi_nondir.array[found] != h_ino);
521ced18
JR
38848+ goto again;
38849+ }
38850+
38851+out_unlock:
062440b3
AM
38852+ spin_unlock(&xi->xi_nondir.spin);
38853+out:
38854+ return err;
38855+}
38856+
38857+/* ---------------------------------------------------------------------- */
38858+
38859+int au_xino_path(struct seq_file *seq, struct file *file)
38860+{
38861+ int err;
38862+
38863+ err = au_seq_path(seq, &file->f_path);
38864+ if (unlikely(err))
38865+ goto out;
38866+
38867+#define Deleted "\\040(deleted)"
38868+ seq->count -= sizeof(Deleted) - 1;
38869+ AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38870+ sizeof(Deleted) - 1));
38871+#undef Deleted
38872+
521ced18
JR
38873+out:
38874+ return err;
38875+}
537831f9
AM
38876diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
38877--- /usr/share/empty/include/uapi/linux/aufs_type.h 1970-01-01 01:00:00.000000000 +0100
e49925d1 38878+++ linux/include/uapi/linux/aufs_type.h 2019-01-28 14:36:12.225751436 +0100
062440b3
AM
38879@@ -0,0 +1,448 @@
38880+/* SPDX-License-Identifier: GPL-2.0 */
7f207e10 38881+/*
b00004a5 38882+ * Copyright (C) 2005-2018 Junjiro R. Okajima
7f207e10
AM
38883+ *
38884+ * This program, aufs is free software; you can redistribute it and/or modify
38885+ * it under the terms of the GNU General Public License as published by
38886+ * the Free Software Foundation; either version 2 of the License, or
38887+ * (at your option) any later version.
38888+ *
38889+ * This program is distributed in the hope that it will be useful,
38890+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
38891+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38892+ * GNU General Public License for more details.
38893+ *
38894+ * You should have received a copy of the GNU General Public License
523b37e3 38895+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
38896+ */
38897+
38898+#ifndef __AUFS_TYPE_H__
38899+#define __AUFS_TYPE_H__
38900+
f6c5ef8b
AM
38901+#define AUFS_NAME "aufs"
38902+
9dbd164d 38903+#ifdef __KERNEL__
f6c5ef8b
AM
38904+/*
38905+ * define it before including all other headers.
38906+ * sched.h may use pr_* macros before defining "current", so define the
38907+ * no-current version first, and re-define later.
38908+ */
38909+#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
38910+#include <linux/sched.h>
38911+#undef pr_fmt
a2a7ad62
AM
38912+#define pr_fmt(fmt) \
38913+ AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
38914+ (int)sizeof(current->comm), current->comm, current->pid
9dbd164d
AM
38915+#else
38916+#include <stdint.h>
38917+#include <sys/types.h>
f6c5ef8b 38918+#endif /* __KERNEL__ */
7f207e10 38919+
f6c5ef8b
AM
38920+#include <linux/limits.h>
38921+
e49925d1 38922+#define AUFS_VERSION "4.19-20181217"
7f207e10
AM
38923+
38924+/* todo? move this to linux-2.6.19/include/magic.h */
38925+#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
38926+
38927+/* ---------------------------------------------------------------------- */
38928+
38929+#ifdef CONFIG_AUFS_BRANCH_MAX_127
9dbd164d 38930+typedef int8_t aufs_bindex_t;
7f207e10
AM
38931+#define AUFS_BRANCH_MAX 127
38932+#else
9dbd164d 38933+typedef int16_t aufs_bindex_t;
7f207e10
AM
38934+#ifdef CONFIG_AUFS_BRANCH_MAX_511
38935+#define AUFS_BRANCH_MAX 511
38936+#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
38937+#define AUFS_BRANCH_MAX 1023
38938+#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
38939+#define AUFS_BRANCH_MAX 32767
38940+#endif
38941+#endif
38942+
38943+#ifdef __KERNEL__
38944+#ifndef AUFS_BRANCH_MAX
38945+#error unknown CONFIG_AUFS_BRANCH_MAX value
38946+#endif
38947+#endif /* __KERNEL__ */
38948+
38949+/* ---------------------------------------------------------------------- */
38950+
7f207e10
AM
38951+#define AUFS_FSTYPE AUFS_NAME
38952+
38953+#define AUFS_ROOT_INO 2
38954+#define AUFS_FIRST_INO 11
38955+
38956+#define AUFS_WH_PFX ".wh."
38957+#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
38958+#define AUFS_WH_TMP_LEN 4
86dc4139 38959+/* a limit for rmdir/rename a dir and copyup */
7f207e10
AM
38960+#define AUFS_MAX_NAMELEN (NAME_MAX \
38961+ - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
38962+ - 1 /* dot */\
38963+ - AUFS_WH_TMP_LEN) /* hex */
38964+#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
38965+#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
392086de
AM
38966+#define AUFS_XINO_DEF_SEC 30 /* seconds */
38967+#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
7f207e10
AM
38968+#define AUFS_DIRWH_DEF 3
38969+#define AUFS_RDCACHE_DEF 10 /* seconds */
027c5e7a 38970+#define AUFS_RDCACHE_MAX 3600 /* seconds */
7f207e10
AM
38971+#define AUFS_RDBLK_DEF 512 /* bytes */
38972+#define AUFS_RDHASH_DEF 32
38973+#define AUFS_WKQ_NAME AUFS_NAME "d"
027c5e7a
AM
38974+#define AUFS_MFS_DEF_SEC 30 /* seconds */
38975+#define AUFS_MFS_MAX_SEC 3600 /* seconds */
076b876e 38976+#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
86dc4139 38977+#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
7f207e10
AM
38978+
38979+/* pseudo-link maintenace under /proc */
38980+#define AUFS_PLINK_MAINT_NAME "plink_maint"
38981+#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
38982+#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
38983+
8b6a4947
AM
38984+/* dirren, renamed dir */
38985+#define AUFS_DR_INFO_PFX AUFS_WH_PFX ".dr."
38986+#define AUFS_DR_BRHINO_NAME AUFS_WH_PFX "hino"
38987+/* whiteouted doubly */
38988+#define AUFS_WH_DR_INFO_PFX AUFS_WH_PFX AUFS_DR_INFO_PFX
38989+#define AUFS_WH_DR_BRHINO AUFS_WH_PFX AUFS_DR_BRHINO_NAME
38990+
7f207e10
AM
38991+#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
38992+#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
38993+
38994+#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
38995+#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
38996+#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
38997+
38998+/* doubly whiteouted */
38999+#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
39000+#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
39001+#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
39002+
1e00d052 39003+/* branch permissions and attributes */
7f207e10
AM
39004+#define AUFS_BRPERM_RW "rw"
39005+#define AUFS_BRPERM_RO "ro"
39006+#define AUFS_BRPERM_RR "rr"
076b876e
AM
39007+#define AUFS_BRATTR_COO_REG "coo_reg"
39008+#define AUFS_BRATTR_COO_ALL "coo_all"
39009+#define AUFS_BRATTR_FHSM "fhsm"
39010+#define AUFS_BRATTR_UNPIN "unpin"
c1595e42
JR
39011+#define AUFS_BRATTR_ICEX "icex"
39012+#define AUFS_BRATTR_ICEX_SEC "icexsec"
39013+#define AUFS_BRATTR_ICEX_SYS "icexsys"
39014+#define AUFS_BRATTR_ICEX_TR "icextr"
39015+#define AUFS_BRATTR_ICEX_USR "icexusr"
39016+#define AUFS_BRATTR_ICEX_OTH "icexoth"
1e00d052
AM
39017+#define AUFS_BRRATTR_WH "wh"
39018+#define AUFS_BRWATTR_NLWH "nolwh"
076b876e
AM
39019+#define AUFS_BRWATTR_MOO "moo"
39020+
39021+#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
39022+#define AuBrPerm_RO (1 << 1) /* readonly */
39023+#define AuBrPerm_RR (1 << 2) /* natively readonly */
39024+#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
39025+
39026+#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
39027+#define AuBrAttr_COO_ALL (1 << 4)
39028+#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
39029+
39030+#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
39031+#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
c1595e42
JR
39032+ branch. meaningless since
39033+ linux-3.18-rc1 */
39034+
39035+/* ignore error in copying XATTR */
39036+#define AuBrAttr_ICEX_SEC (1 << 7)
39037+#define AuBrAttr_ICEX_SYS (1 << 8)
39038+#define AuBrAttr_ICEX_TR (1 << 9)
39039+#define AuBrAttr_ICEX_USR (1 << 10)
39040+#define AuBrAttr_ICEX_OTH (1 << 11)
39041+#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
39042+ | AuBrAttr_ICEX_SYS \
39043+ | AuBrAttr_ICEX_TR \
39044+ | AuBrAttr_ICEX_USR \
39045+ | AuBrAttr_ICEX_OTH)
39046+
39047+#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
076b876e
AM
39048+#define AuBrRAttr_Mask AuBrRAttr_WH
39049+
c1595e42
JR
39050+#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
39051+#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
076b876e
AM
39052+#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
39053+
39054+#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
39055+
c1595e42 39056+/* #warning test userspace */
076b876e
AM
39057+#ifdef __KERNEL__
39058+#ifndef CONFIG_AUFS_FHSM
39059+#undef AuBrAttr_FHSM
39060+#define AuBrAttr_FHSM 0
39061+#endif
c1595e42
JR
39062+#ifndef CONFIG_AUFS_XATTR
39063+#undef AuBrAttr_ICEX
39064+#define AuBrAttr_ICEX 0
39065+#undef AuBrAttr_ICEX_SEC
39066+#define AuBrAttr_ICEX_SEC 0
39067+#undef AuBrAttr_ICEX_SYS
39068+#define AuBrAttr_ICEX_SYS 0
39069+#undef AuBrAttr_ICEX_TR
39070+#define AuBrAttr_ICEX_TR 0
39071+#undef AuBrAttr_ICEX_USR
39072+#define AuBrAttr_ICEX_USR 0
39073+#undef AuBrAttr_ICEX_OTH
39074+#define AuBrAttr_ICEX_OTH 0
39075+#endif
076b876e
AM
39076+#endif
39077+
39078+/* the longest combination */
c1595e42
JR
39079+/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
39080+#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
39081+ "+" AUFS_BRATTR_COO_REG \
39082+ "+" AUFS_BRATTR_FHSM \
39083+ "+" AUFS_BRATTR_UNPIN \
7e9cd9fe
AM
39084+ "+" AUFS_BRATTR_ICEX_SEC \
39085+ "+" AUFS_BRATTR_ICEX_SYS \
39086+ "+" AUFS_BRATTR_ICEX_USR \
39087+ "+" AUFS_BRATTR_ICEX_OTH \
076b876e
AM
39088+ "+" AUFS_BRWATTR_NLWH)
39089+
39090+typedef struct {
39091+ char a[AuBrPermStrSz];
39092+} au_br_perm_str_t;
39093+
39094+static inline int au_br_writable(int brperm)
39095+{
39096+ return brperm & AuBrPerm_RW;
39097+}
39098+
39099+static inline int au_br_whable(int brperm)
39100+{
39101+ return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
39102+}
39103+
39104+static inline int au_br_wh_linkable(int brperm)
39105+{
39106+ return !(brperm & AuBrWAttr_NoLinkWH);
39107+}
39108+
39109+static inline int au_br_cmoo(int brperm)
39110+{
39111+ return brperm & AuBrAttr_CMOO_Mask;
39112+}
39113+
39114+static inline int au_br_fhsm(int brperm)
39115+{
39116+ return brperm & AuBrAttr_FHSM;
39117+}
7f207e10
AM
39118+
39119+/* ---------------------------------------------------------------------- */
39120+
39121+/* ioctl */
39122+enum {
39123+ /* readdir in userspace */
39124+ AuCtl_RDU,
39125+ AuCtl_RDU_INO,
39126+
076b876e
AM
39127+ AuCtl_WBR_FD, /* pathconf wrapper */
39128+ AuCtl_IBUSY, /* busy inode */
39129+ AuCtl_MVDOWN, /* move-down */
39130+ AuCtl_BR, /* info about branches */
39131+ AuCtl_FHSM_FD /* connection for fhsm */
7f207e10
AM
39132+};
39133+
39134+/* borrowed from linux/include/linux/kernel.h */
39135+#ifndef ALIGN
39136+#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
39137+#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
39138+#endif
39139+
39140+/* borrowed from linux/include/linux/compiler-gcc3.h */
39141+#ifndef __aligned
39142+#define __aligned(x) __attribute__((aligned(x)))
53392da6
AM
39143+#endif
39144+
39145+#ifdef __KERNEL__
39146+#ifndef __packed
7f207e10
AM
39147+#define __packed __attribute__((packed))
39148+#endif
53392da6 39149+#endif
7f207e10
AM
39150+
39151+struct au_rdu_cookie {
9dbd164d
AM
39152+ uint64_t h_pos;
39153+ int16_t bindex;
39154+ uint8_t flags;
39155+ uint8_t pad;
39156+ uint32_t generation;
7f207e10
AM
39157+} __aligned(8);
39158+
39159+struct au_rdu_ent {
9dbd164d
AM
39160+ uint64_t ino;
39161+ int16_t bindex;
39162+ uint8_t type;
39163+ uint8_t nlen;
39164+ uint8_t wh;
7f207e10
AM
39165+ char name[0];
39166+} __aligned(8);
39167+
39168+static inline int au_rdu_len(int nlen)
39169+{
39170+ /* include the terminating NULL */
39171+ return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
9dbd164d 39172+ sizeof(uint64_t));
7f207e10
AM
39173+}
39174+
39175+union au_rdu_ent_ul {
39176+ struct au_rdu_ent __user *e;
9dbd164d 39177+ uint64_t ul;
7f207e10
AM
39178+};
39179+
39180+enum {
39181+ AufsCtlRduV_SZ,
39182+ AufsCtlRduV_End
39183+};
39184+
39185+struct aufs_rdu {
39186+ /* input */
39187+ union {
9dbd164d
AM
39188+ uint64_t sz; /* AuCtl_RDU */
39189+ uint64_t nent; /* AuCtl_RDU_INO */
7f207e10
AM
39190+ };
39191+ union au_rdu_ent_ul ent;
9dbd164d 39192+ uint16_t verify[AufsCtlRduV_End];
7f207e10
AM
39193+
39194+ /* input/output */
9dbd164d 39195+ uint32_t blk;
7f207e10
AM
39196+
39197+ /* output */
39198+ union au_rdu_ent_ul tail;
39199+ /* number of entries which were added in a single call */
9dbd164d
AM
39200+ uint64_t rent;
39201+ uint8_t full;
39202+ uint8_t shwh;
7f207e10
AM
39203+
39204+ struct au_rdu_cookie cookie;
39205+} __aligned(8);
39206+
1e00d052
AM
39207+/* ---------------------------------------------------------------------- */
39208+
8b6a4947
AM
39209+/* dirren. the branch is identified by the filename who contains this */
39210+struct au_drinfo {
39211+ uint64_t ino;
39212+ union {
39213+ uint8_t oldnamelen;
39214+ uint64_t _padding;
39215+ };
39216+ uint8_t oldname[0];
39217+} __aligned(8);
39218+
39219+struct au_drinfo_fdata {
39220+ uint32_t magic;
39221+ struct au_drinfo drinfo;
39222+} __aligned(8);
39223+
39224+#define AUFS_DRINFO_MAGIC_V1 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39225+/* future */
39226+#define AUFS_DRINFO_MAGIC_V2 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39227+
39228+/* ---------------------------------------------------------------------- */
39229+
1e00d052 39230+struct aufs_wbr_fd {
9dbd164d
AM
39231+ uint32_t oflags;
39232+ int16_t brid;
1e00d052
AM
39233+} __aligned(8);
39234+
39235+/* ---------------------------------------------------------------------- */
39236+
027c5e7a 39237+struct aufs_ibusy {
9dbd164d
AM
39238+ uint64_t ino, h_ino;
39239+ int16_t bindex;
027c5e7a
AM
39240+} __aligned(8);
39241+
1e00d052
AM
39242+/* ---------------------------------------------------------------------- */
39243+
392086de
AM
39244+/* error code for move-down */
39245+/* the actual message strings are implemented in aufs-util.git */
39246+enum {
39247+ EAU_MVDOWN_OPAQUE = 1,
39248+ EAU_MVDOWN_WHITEOUT,
39249+ EAU_MVDOWN_UPPER,
39250+ EAU_MVDOWN_BOTTOM,
39251+ EAU_MVDOWN_NOUPPER,
39252+ EAU_MVDOWN_NOLOWERBR,
39253+ EAU_Last
39254+};
39255+
c2b27bf2 39256+/* flags for move-down */
392086de
AM
39257+#define AUFS_MVDOWN_DMSG 1
39258+#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
39259+#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
39260+#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
39261+#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
39262+#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
39263+#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
39264+#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
39265+#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
076b876e
AM
39266+#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
39267+#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
39268+#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
39269+#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
c2b27bf2 39270+
076b876e 39271+/* index for move-down */
392086de
AM
39272+enum {
39273+ AUFS_MVDOWN_UPPER,
39274+ AUFS_MVDOWN_LOWER,
39275+ AUFS_MVDOWN_NARRAY
39276+};
39277+
076b876e
AM
39278+/*
39279+ * additional info of move-down
39280+ * number of free blocks and inodes.
39281+ * subset of struct kstatfs, but smaller and always 64bit.
39282+ */
39283+struct aufs_stfs {
39284+ uint64_t f_blocks;
39285+ uint64_t f_bavail;
39286+ uint64_t f_files;
39287+ uint64_t f_ffree;
39288+};
39289+
39290+struct aufs_stbr {
39291+ int16_t brid; /* optional input */
39292+ int16_t bindex; /* output */
39293+ struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
39294+} __aligned(8);
39295+
c2b27bf2 39296+struct aufs_mvdown {
076b876e
AM
39297+ uint32_t flags; /* input/output */
39298+ struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39299+ int8_t au_errno; /* output */
39300+} __aligned(8);
39301+
39302+/* ---------------------------------------------------------------------- */
39303+
39304+union aufs_brinfo {
39305+ /* PATH_MAX may differ between kernel-space and user-space */
39306+ char _spacer[4096];
392086de 39307+ struct {
076b876e
AM
39308+ int16_t id;
39309+ int perm;
39310+ char path[0];
39311+ };
c2b27bf2
AM
39312+} __aligned(8);
39313+
39314+/* ---------------------------------------------------------------------- */
39315+
7f207e10
AM
39316+#define AuCtlType 'A'
39317+#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39318+#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
1e00d052
AM
39319+#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
39320+ struct aufs_wbr_fd)
027c5e7a 39321+#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
392086de
AM
39322+#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
39323+ struct aufs_mvdown)
076b876e
AM
39324+#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39325+#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
7f207e10
AM
39326+
39327+#endif /* __AUFS_TYPE_H__ */
2121bcd9 39328SPDX-License-Identifier: GPL-2.0
e49925d1 39329aufs4.19 loopback patch
5527c038
JR
39330
39331diff --git a/drivers/block/loop.c b/drivers/block/loop.c
acd2b654 39332index 9e534a3..74cd74e 100644
5527c038
JR
39333--- a/drivers/block/loop.c
39334+++ b/drivers/block/loop.c
cd7a4cd9 39335@@ -626,6 +626,15 @@ static inline void loop_update_dio(struct loop_device *lo)
8b6a4947 39336 lo->use_dio);
5527c038
JR
39337 }
39338
5527c038
JR
39339+static struct file *loop_real_file(struct file *file)
39340+{
39341+ struct file *f = NULL;
39342+
39343+ if (file->f_path.dentry->d_sb->s_op->real_loop)
39344+ f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39345+ return f;
8b6a4947
AM
39346+}
39347+
c2c0f25c 39348 static void loop_reread_partitions(struct loop_device *lo,
8b6a4947
AM
39349 struct block_device *bdev)
39350 {
cd7a4cd9 39351@@ -690,6 +699,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
5527c038
JR
39352 unsigned int arg)
39353 {
e49925d1 39354 struct file *file = NULL, *old_file;
5527c038 39355+ struct file *f, *virt_file = NULL, *old_virt_file;
5527c038 39356 int error;
e49925d1 39357 bool partscan;
5527c038 39358
acd2b654 39359@@ -705,12 +715,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
cd7a4cd9
AM
39360 file = fget(arg);
39361 if (!file)
e49925d1 39362 goto out_err;
5527c038
JR
39363+ f = loop_real_file(file);
39364+ if (f) {
39365+ virt_file = file;
39366+ file = f;
39367+ get_file(file);
39368+ }
39369
cd7a4cd9
AM
39370 error = loop_validate_file(file, bdev);
39371 if (error)
e49925d1 39372 goto out_err;
cd7a4cd9 39373
5527c038
JR
39374 old_file = lo->lo_backing_file;
39375+ old_virt_file = lo->lo_backing_virt_file;
39376
39377 error = -EINVAL;
39378
acd2b654 39379@@ -722,6 +739,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
8b6a4947
AM
39380 blk_mq_freeze_queue(lo->lo_queue);
39381 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39382 lo->lo_backing_file = file;
39383+ lo->lo_backing_virt_file = virt_file;
39384 lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39385 mapping_set_gfp_mask(file->f_mapping,
39386 lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
e49925d1
JR
39387@@ -729,14 +747,18 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39388 * dependency.
39389 */
5527c038
JR
39390 fput(old_file);
39391+ if (old_virt_file)
39392+ fput(old_virt_file);
e49925d1 39393 if (partscan)
c2c0f25c 39394 loop_reread_partitions(lo, bdev);
5527c038
JR
39395 return 0;
39396
e49925d1
JR
39397 out_err:
39398 mutex_unlock(&loop_ctl_mutex);
39399 if (file)
39400 fput(file);
5527c038
JR
39401+ if (virt_file)
39402+ fput(virt_file);
5527c038
JR
39403 return error;
39404 }
e49925d1 39405
acd2b654 39406@@ -922,7 +944,7 @@ static int loop_prepare_queue(struct loop_device *lo)
5527c038
JR
39407 static int loop_set_fd(struct loop_device *lo, fmode_t mode,
39408 struct block_device *bdev, unsigned int arg)
39409 {
062440b3 39410- struct file *file;
5527c038
JR
39411+ struct file *file, *f, *virt_file = NULL;
39412 struct inode *inode;
39413 struct address_space *mapping;
8b6a4947 39414 int lo_flags = 0;
acd2b654 39415@@ -936,6 +958,12 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
39416 file = fget(arg);
39417 if (!file)
39418 goto out;
39419+ f = loop_real_file(file);
39420+ if (f) {
39421+ virt_file = file;
39422+ file = f;
39423+ get_file(file);
39424+ }
39425
39426 error = -EBUSY;
39427 if (lo->lo_state != Lo_unbound)
acd2b654 39428@@ -968,6 +996,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
39429 lo->lo_device = bdev;
39430 lo->lo_flags = lo_flags;
39431 lo->lo_backing_file = file;
39432+ lo->lo_backing_virt_file = virt_file;
39433 lo->transfer = NULL;
39434 lo->ioctl = NULL;
39435 lo->lo_sizelimit = 0;
acd2b654 39436@@ -1001,6 +1030,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
e49925d1
JR
39437 mutex_unlock(&loop_ctl_mutex);
39438 out_putf:
5527c038
JR
39439 fput(file);
39440+ if (virt_file)
39441+ fput(virt_file);
e49925d1 39442 out:
5527c038
JR
39443 /* This is safe: open() is still holding a reference. */
39444 module_put(THIS_MODULE);
acd2b654 39445@@ -1047,6 +1078,7 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
e49925d1 39446 static int __loop_clr_fd(struct loop_device *lo)
5527c038 39447 {
e49925d1 39448 struct file *filp = NULL;
5527c038
JR
39449+ struct file *virt_filp = lo->lo_backing_virt_file;
39450 gfp_t gfp = lo->old_gfp_mask;
39451 struct block_device *bdev = lo->lo_device;
39452
e49925d1 39453@@ -1078,6 +1110,7 @@ static int __loop_clr_fd(struct loop_device *lo)
5527c038
JR
39454 spin_lock_irq(&lo->lo_lock);
39455 lo->lo_state = Lo_rundown;
39456 lo->lo_backing_file = NULL;
39457+ lo->lo_backing_virt_file = NULL;
39458 spin_unlock_irq(&lo->lo_lock);
39459
39460 loop_release_xfer(lo);
e49925d1 39461@@ -1126,6 +1159,8 @@ static int __loop_clr_fd(struct loop_device *lo)
5527c038 39462 */
e49925d1
JR
39463 if (filp)
39464 fput(filp);
5527c038
JR
39465+ if (virt_filp)
39466+ fput(virt_filp);
e49925d1 39467 return err;
5527c038
JR
39468 }
39469
39470diff --git a/drivers/block/loop.h b/drivers/block/loop.h
cd7a4cd9 39471index 4d42c7a..a4974ee 100644
5527c038
JR
39472--- a/drivers/block/loop.h
39473+++ b/drivers/block/loop.h
39474@@ -46,7 +46,7 @@ struct loop_device {
39475 int (*ioctl)(struct loop_device *, int cmd,
39476 unsigned long arg);
39477
39478- struct file * lo_backing_file;
39479+ struct file * lo_backing_file, *lo_backing_virt_file;
39480 struct block_device *lo_device;
5527c038 39481 void *key_data;
8b6a4947 39482
5527c038 39483diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
acd2b654 39484index 5309874..1a334cf 100644
5527c038
JR
39485--- a/fs/aufs/f_op.c
39486+++ b/fs/aufs/f_op.c
acd2b654 39487@@ -359,7 +359,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
5527c038
JR
39488 if (IS_ERR(h_file))
39489 goto out;
39490
39491- if (au_test_loopback_kthread()) {
39492+ if (0 && au_test_loopback_kthread()) {
39493 au_warn_loopback(h_file->f_path.dentry->d_sb);
39494 if (file->f_mapping != h_file->f_mapping) {
39495 file->f_mapping = h_file->f_mapping;
39496diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
e49925d1 39497index 3f3577d..3b44158 100644
5527c038
JR
39498--- a/fs/aufs/loop.c
39499+++ b/fs/aufs/loop.c
2121bcd9 39500@@ -133,3 +133,19 @@ void au_loopback_fin(void)
79b8bda9 39501 symbol_put(loop_backing_file);
e49925d1 39502 au_kfree_try_rcu(au_warn_loopback_array);
5527c038
JR
39503 }
39504+
39505+/* ---------------------------------------------------------------------- */
39506+
39507+/* support the loopback block device insude aufs */
39508+
39509+struct file *aufs_real_loop(struct file *file)
39510+{
39511+ struct file *f;
39512+
39513+ BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39514+ fi_read_lock(file);
39515+ f = au_hf_top(file);
39516+ fi_read_unlock(file);
39517+ AuDebugOn(!f);
39518+ return f;
39519+}
39520diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
062440b3 39521index 05d703d..6bb23c8 100644
5527c038
JR
39522--- a/fs/aufs/loop.h
39523+++ b/fs/aufs/loop.h
2121bcd9 39524@@ -26,7 +26,11 @@ void au_warn_loopback(struct super_block *h_sb);
5527c038
JR
39525
39526 int au_loopback_init(void);
39527 void au_loopback_fin(void);
39528+
39529+struct file *aufs_real_loop(struct file *file);
39530 #else
39531+AuStub(struct file *, loop_backing_file, return NULL)
39532+
39533 AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
39534 struct dentry *h_adding)
39535 AuStubInt0(au_test_loopback_kthread, void)
2121bcd9 39536@@ -34,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
5527c038
JR
39537
39538 AuStubInt0(au_loopback_init, void)
39539 AuStubVoid(au_loopback_fin, void)
39540+
39541+AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39542 #endif /* BLK_DEV_LOOP */
39543
39544 #endif /* __KERNEL__ */
39545diff --git a/fs/aufs/super.c b/fs/aufs/super.c
acd2b654 39546index 777503e..7130061 100644
5527c038
JR
39547--- a/fs/aufs/super.c
39548+++ b/fs/aufs/super.c
acd2b654 39549@@ -845,7 +845,10 @@ static const struct super_operations aufs_sop = {
5527c038
JR
39550 .statfs = aufs_statfs,
39551 .put_super = aufs_put_super,
39552 .sync_fs = aufs_sync_fs,
39553- .remount_fs = aufs_remount_fs
39554+ .remount_fs = aufs_remount_fs,
39555+#ifdef CONFIG_AUFS_BDEV_LOOP
39556+ .real_loop = aufs_real_loop
39557+#endif
39558 };
39559
39560 /* ---------------------------------------------------------------------- */
39561diff --git a/include/linux/fs.h b/include/linux/fs.h
acd2b654 39562index 7fb92a9..cff3ca3 100644
5527c038
JR
39563--- a/include/linux/fs.h
39564+++ b/include/linux/fs.h
acd2b654 39565@@ -1882,6 +1882,10 @@ struct super_operations {
5527c038
JR
39566 struct shrink_control *);
39567 long (*free_cached_objects)(struct super_block *,
39568 struct shrink_control *);
39569+#if defined(CONFIG_BLK_DEV_LOOP) || defined(CONFIG_BLK_DEV_LOOP_MODULE)
39570+ /* and aufs */
39571+ struct file *(*real_loop)(struct file *);
39572+#endif
39573 };
39574
39575 /*
This page took 7.12226 seconds and 4 git commands to generate.