]> git.pld-linux.org Git - packages/kernel.git/blame - kernel-aufs4.patch
- update aufs patch
[packages/kernel.git] / kernel-aufs4.patch
CommitLineData
2121bcd9 1SPDX-License-Identifier: GPL-2.0
cd7a4cd9 2aufs4.x-rcN kbuild patch
7f207e10
AM
3
4diff --git a/fs/Kconfig b/fs/Kconfig
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
cd7a4cd9 26aufs4.x-rcN base patch
7f207e10 27
c1595e42 28diff --git a/MAINTAINERS b/MAINTAINERS
9f237c51 29index 8119141..5e84420 100644
c1595e42
JR
30--- a/MAINTAINERS
31+++ b/MAINTAINERS
9f237c51 32@@ -2590,6 +2590,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
9f237c51 53index cb0cc86..470dd02 100644
392086de
AM
54--- a/drivers/block/loop.c
55+++ b/drivers/block/loop.c
9f237c51 56@@ -738,6 +738,24 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
cd7a4cd9 57 return error;
392086de
AM
58 }
59
60+/*
61+ * for AUFS
62+ * no get/put for file.
63+ */
64+struct file *loop_backing_file(struct super_block *sb)
65+{
66+ struct file *ret;
67+ struct loop_device *l;
68+
69+ ret = NULL;
70+ if (MAJOR(sb->s_dev) == LOOP_MAJOR) {
71+ l = sb->s_bdev->bd_disk->private_data;
72+ ret = l->lo_backing_file;
73+ }
74+ return ret;
75+}
febd17d6 76+EXPORT_SYMBOL_GPL(loop_backing_file);
392086de
AM
77+
78 /* loop sysfs attributes */
79
80 static ssize_t loop_attr_show(struct device *dev, char *page,
c1595e42 81diff --git a/fs/dcache.c b/fs/dcache.c
9f237c51 82index 2593153..6369b30 100644
c1595e42
JR
83--- a/fs/dcache.c
84+++ b/fs/dcache.c
9f237c51 85@@ -1224,7 +1224,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
9f237c51 95index 0831851..78234ee 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
9f237c51 117index 35d2108..d2395eb 100644
5afbbe0d
AM
118--- a/fs/inode.c
119+++ b/fs/inode.c
9f237c51 120@@ -1660,7 +1660,7 @@ EXPORT_SYMBOL(generic_update_time);
5afbbe0d
AM
121 * This does the actual work of updating an inodes time or version. Must have
122 * had called mnt_want_write() before calling this.
123 */
cd7a4cd9
AM
124-static int update_time(struct inode *inode, struct timespec64 *time, int flags)
125+int update_time(struct inode *inode, struct timespec64 *time, int flags)
5afbbe0d 126 {
cd7a4cd9 127 int (*update_time)(struct inode *, struct timespec64 *, int);
5afbbe0d 128
8b6a4947 129diff --git a/fs/namespace.c b/fs/namespace.c
9f237c51 130index a7f9126..46ed643 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
9f237c51 147index 58f3053..a2a55ea 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
9f237c51 180index de2ede0..5dcf77b 100644
7f207e10
AM
181--- a/fs/splice.c
182+++ b/fs/splice.c
9f237c51 183@@ -837,8 +837,8 @@ EXPORT_SYMBOL(generic_splice_sendpage);
7f207e10
AM
184 /*
185 * Attempt to initiate a splice from pipe to file.
186 */
187-static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
188- loff_t *ppos, size_t len, unsigned int flags)
189+long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
190+ loff_t *ppos, size_t len, unsigned int flags)
191 {
192 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
193 loff_t *, size_t, unsigned int);
9f237c51 194@@ -854,9 +854,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
7f207e10
AM
195 /*
196 * Attempt to initiate a splice from a file to a pipe.
197 */
198-static long do_splice_to(struct file *in, loff_t *ppos,
199- struct pipe_inode_info *pipe, size_t len,
200- unsigned int flags)
201+long do_splice_to(struct file *in, loff_t *ppos,
202+ struct pipe_inode_info *pipe, size_t len,
203+ unsigned int flags)
204 {
205 ssize_t (*splice_read)(struct file *, loff_t *,
206 struct pipe_inode_info *, size_t, unsigned int);
a2654f78 207diff --git a/fs/sync.c b/fs/sync.c
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
9f237c51 221index c95c080..0e44705 100644
5527c038
JR
222--- a/include/linux/fs.h
223+++ b/include/linux/fs.h
9f237c51 224@@ -1305,6 +1305,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);
9f237c51 232@@ -1797,6 +1798,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);
9f237c51 240@@ -1867,6 +1869,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 *);
9f237c51 253@@ -2292,6 +2300,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;
9f237c51 261@@ -2579,6 +2588,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
9f237c51 270index 1fd82ff..a5ccac5 100644
8b6a4947
AM
271--- a/include/linux/lockdep.h
272+++ b/include/linux/lockdep.h
9f237c51 273@@ -308,6 +308,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 *
9f237c51 282@@ -434,6 +436,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
9f237c51 325index 1efada2..447bc0b 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 329 unsigned long nr_lock_classes;
9f237c51 330 struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
8b6a4947
AM
331
332-static inline struct lock_class *hlock_class(struct held_lock *hlock)
333+inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
334 {
335 if (!hlock->class_idx) {
336 /*
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
cd7a4cd9 346aufs4.x-rcN mmap patch
fb47a38f 347
c1595e42 348diff --git a/fs/proc/base.c b/fs/proc/base.c
9f237c51 349index ce34654..28508b1 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
9f237c51 378index 47c3764..e37e4b5 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;
9f237c51 393@@ -1729,7 +1732,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
9f237c51 419index 5411de9..b3cd025 100644
fb47a38f
JR
420--- a/include/linux/mm.h
421+++ b/include/linux/mm.h
9f237c51 422@@ -1460,6 +1460,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
9f237c51 472index 07cddff..d837e55 100644
fb47a38f
JR
473--- a/kernel/fork.c
474+++ b/kernel/fork.c
9f237c51 475@@ -546,7 +546,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
9f237c51 485index d210cc9..e77e80c 100644
076b876e
AM
486--- a/mm/Makefile
487+++ b/mm/Makefile
acd2b654 488@@ -39,7 +39,7 @@ obj-y := filemap.o mempool.o oom_kill.o fadvise.o \
076b876e 489 mm_init.o mmu_context.o percpu.o slab_common.o \
b00004a5 490 compaction.o vmacache.o \
076b876e 491 interval_tree.o list_lru.o workingset.o \
7e9cd9fe
AM
492- debug.o $(mmu-y)
493+ prfile.o debug.o $(mmu-y)
076b876e
AM
494
495 obj-y += init-mm.o
9f237c51 496 obj-y += memblock.o
fb47a38f 497diff --git a/mm/filemap.c b/mm/filemap.c
9f237c51 498index 81adec8..8507cec 100644
fb47a38f
JR
499--- a/mm/filemap.c
500+++ b/mm/filemap.c
9f237c51 501@@ -2609,7 +2609,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
cd7a4cd9 502 vm_fault_t ret = VM_FAULT_LOCKED;
fb47a38f
JR
503
504 sb_start_pagefault(inode->i_sb);
521ced18
JR
505- file_update_time(vmf->vma->vm_file);
506+ vma_file_update_time(vmf->vma);
fb47a38f
JR
507 lock_page(page);
508 if (page->mapping != inode->i_mapping) {
509 unlock_page(page);
fb47a38f 510diff --git a/mm/mmap.c b/mm/mmap.c
9f237c51 511index 6c04292..f3629c1 100644
fb47a38f
JR
512--- a/mm/mmap.c
513+++ b/mm/mmap.c
c4adf169 514@@ -180,7 +180,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
fb47a38f
JR
515 if (vma->vm_ops && vma->vm_ops->close)
516 vma->vm_ops->close(vma);
517 if (vma->vm_file)
518- fput(vma->vm_file);
519+ vma_fput(vma);
520 mpol_put(vma_policy(vma));
cd7a4cd9 521 vm_area_free(vma);
fb47a38f 522 return next;
9f237c51 523@@ -929,7 +929,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
fb47a38f
JR
524 if (remove_next) {
525 if (file) {
526 uprobe_munmap(next, next->vm_start, next->vm_end);
527- fput(file);
528+ vma_fput(vma);
529 }
530 if (next->anon_vma)
531 anon_vma_merge(vma, next);
9f237c51 532@@ -1845,8 +1845,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
35939ee7
JR
533 return addr;
534
fb47a38f 535 unmap_and_free_vma:
fb47a38f
JR
536+ vma_fput(vma);
537 vma->vm_file = NULL;
538- fput(file);
539
540 /* Undo any partial mapping done by a device driver. */
541 unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
9f237c51 542@@ -2665,7 +2665,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);
9f237c51 551@@ -2684,7 +2684,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));
9f237c51 560@@ -2874,7 +2874,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);
9f237c51 569@@ -2949,10 +2949,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)
9f237c51 598@@ -3258,7 +3275,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
9f237c51 608index 749276b..d56f8f2 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
cd7a4cd9 740aufs4.x-rcN standalone patch
7f207e10 741
c1595e42 742diff --git a/fs/dcache.c b/fs/dcache.c
9f237c51 743index 6369b30..df4a5fe 100644
c1595e42
JR
744--- a/fs/dcache.c
745+++ b/fs/dcache.c
9f237c51 746@@ -1329,6 +1329,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;
9f237c51 754@@ -2817,6 +2818,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
9f237c51 763index fc281b7..65eaaca 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
9f237c51 775index 78234ee..2072f69 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
9f237c51 815index d2395eb..b8be7be 100644
5afbbe0d
AM
816--- a/fs/inode.c
817+++ b/fs/inode.c
9f237c51 818@@ -1669,6 +1669,7 @@ int update_time(struct inode *inode, struct timespec64 *time, int flags)
5afbbe0d
AM
819
820 return update_time(inode, time, flags);
821 }
822+EXPORT_SYMBOL_GPL(update_time);
823
824 /**
825 * touch_atime - update the access time
7f207e10 826diff --git a/fs/namespace.c b/fs/namespace.c
9f237c51 827index 46ed643..44502c2 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
9f237c51 846@@ -1844,6 +1846,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
9f237c51 891index d2dd16c..cf709b7 100644
7f207e10
AM
892--- a/fs/notify/mark.c
893+++ b/fs/notify/mark.c
9f237c51 894@@ -289,6 +289,7 @@ void fsnotify_put_mark(struct fsnotify_mark *mark)
cd7a4cd9
AM
895 queue_delayed_work(system_unbound_wq, &reaper_work,
896 FSNOTIFY_REAPER_DELAY);
7f207e10 897 }
febd17d6 898+EXPORT_SYMBOL_GPL(fsnotify_put_mark);
7f207e10 899
cd7a4cd9
AM
900 /*
901 * Get mark reference when we found the mark via lockless traversal of object
9f237c51 902@@ -443,6 +444,7 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark,
1716fcea 903 mutex_unlock(&group->mark_mutex);
79b8bda9 904 fsnotify_free_mark(mark);
7f207e10 905 }
febd17d6 906+EXPORT_SYMBOL_GPL(fsnotify_destroy_mark);
7f207e10 907
ffa93bbd
AM
908 /*
909 * Sorting function for lists of fsnotify marks.
9f237c51 910@@ -658,6 +660,7 @@ int fsnotify_add_mark(struct fsnotify_mark *mark, fsnotify_connp_t *connp,
cd7a4cd9 911 mutex_unlock(&group->mark_mutex);
7f207e10
AM
912 return ret;
913 }
febd17d6 914+EXPORT_SYMBOL_GPL(fsnotify_add_mark);
7f207e10 915
cd7a4cd9
AM
916 /*
917 * Given a list of marks, find the mark associated with given group. If found
9f237c51 918@@ -781,6 +784,7 @@ void fsnotify_init_mark(struct fsnotify_mark *mark,
ffa93bbd
AM
919 fsnotify_get_group(group);
920 mark->group = group;
7f207e10 921 }
febd17d6 922+EXPORT_SYMBOL_GPL(fsnotify_init_mark);
7f207e10 923
5afbbe0d
AM
924 /*
925 * Destroy all marks in destroy_list, waits for SRCU period to finish before
7f207e10 926diff --git a/fs/open.c b/fs/open.c
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
9f237c51 939index a2a55ea..a1366ed 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
9f237c51 975index 5dcf77b..63fe265 100644
7f207e10
AM
976--- a/fs/splice.c
977+++ b/fs/splice.c
9f237c51 978@@ -850,6 +850,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
392086de
AM
979
980 return splice_write(pipe, out, ppos, len, flags);
7f207e10 981 }
febd17d6 982+EXPORT_SYMBOL_GPL(do_splice_from);
7f207e10
AM
983
984 /*
985 * Attempt to initiate a splice from a file to a pipe.
9f237c51 986@@ -879,6 +880,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
7f207e10
AM
987
988 return splice_read(in, ppos, pipe, len, flags);
989 }
febd17d6 990+EXPORT_SYMBOL_GPL(do_splice_to);
7f207e10
AM
991
992 /**
993 * splice_direct_to_actor - splices data directly between two non-pipes
a2654f78 994diff --git a/fs/sync.c b/fs/sync.c
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
9f237c51 1019index 447bc0b..4e7581c 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
9f237c51 1040index 18a4fdf..e49f723 100644
7f207e10
AM
1041--- a/security/commoncap.c
1042+++ b/security/commoncap.c
9f237c51 1043@@ -1333,12 +1333,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
9f237c51 1076index 04d173e..470af62 100644
7f207e10
AM
1077--- a/security/security.c
1078+++ b/security/security.c
9f237c51 1079@@ -553,6 +553,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 {
9f237c51 1087@@ -569,6 +570,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)
9f237c51 1095@@ -577,6 +579,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,
9f237c51 1103@@ -604,6 +607,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 {
9f237c51 1111@@ -611,6 +615,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 {
9f237c51 1119@@ -618,6 +623,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 {
9f237c51 1127@@ -703,6 +709,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)
9f237c51 1135@@ -718,6 +725,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 {
9f237c51 1143@@ -889,6 +897,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 {
9f237c51 1151@@ -948,6 +957,7 @@ int security_mmap_file(struct file *file, unsigned long prot,
7f207e10
AM
1152 return ret;
1153 return ima_file_mmap(file, prot);
1154 }
febd17d6 1155+EXPORT_SYMBOL_GPL(security_mmap_file);
7f207e10 1156
0c3ec466
AM
1157 int security_mmap_addr(unsigned long addr)
1158 {
7f207e10
AM
1159diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
1160--- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 1161+++ linux/Documentation/ABI/testing/debugfs-aufs 2018-08-12 23:43:05.450124426 +0200
062440b3 1162@@ -0,0 +1,55 @@
7f207e10
AM
1163+What: /debug/aufs/si_<id>/
1164+Date: March 2009
f6b6e03d 1165+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1166+Description:
1167+ Under /debug/aufs, a directory named si_<id> is created
1168+ per aufs mount, where <id> is a unique id generated
1169+ internally.
1facf9fc 1170+
86dc4139
AM
1171+What: /debug/aufs/si_<id>/plink
1172+Date: Apr 2013
f6b6e03d 1173+Contact: J. R. Okajima <hooanon05g@gmail.com>
86dc4139
AM
1174+Description:
1175+ It has three lines and shows the information about the
1176+ pseudo-link. The first line is a single number
1177+ representing a number of buckets. The second line is a
1178+ number of pseudo-links per buckets (separated by a
1179+ blank). The last line is a single number representing a
1180+ total number of psedo-links.
1181+ When the aufs mount option 'noplink' is specified, it
1182+ will show "1\n0\n0\n".
1183+
7f207e10
AM
1184+What: /debug/aufs/si_<id>/xib
1185+Date: March 2009
f6b6e03d 1186+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1187+Description:
1188+ It shows the consumed blocks by xib (External Inode Number
1189+ Bitmap), its block size and file size.
1190+ When the aufs mount option 'noxino' is specified, it
1191+ will be empty. About XINO files, see the aufs manual.
1192+
062440b3 1193+What: /debug/aufs/si_<id>/xi0, xi1 ... xiN and xiN-N
7f207e10 1194+Date: March 2009
f6b6e03d 1195+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1196+Description:
1197+ It shows the consumed blocks by xino (External Inode Number
1198+ Translation Table), its link count, block size and file
1199+ size.
062440b3
AM
1200+ Due to the file size limit, there may exist multiple
1201+ xino files per branch. In this case, "-N" is added to
1202+ the filename and it corresponds to the index of the
1203+ internal xino array. "-0" is omitted.
1204+ When the aufs mount option 'noxino' is specified, Those
1205+ entries won't exist. About XINO files, see the aufs
1206+ manual.
7f207e10
AM
1207+
1208+What: /debug/aufs/si_<id>/xigen
1209+Date: March 2009
f6b6e03d 1210+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1211+Description:
1212+ It shows the consumed blocks by xigen (External Inode
1213+ Generation Table), its block size and file size.
1214+ If CONFIG_AUFS_EXPORT is disabled, this entry will not
1215+ be created.
1216+ When the aufs mount option 'noxino' is specified, it
1217+ will be empty. About XINO files, see the aufs manual.
1218diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
1219--- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs 1970-01-01 01:00:00.000000000 +0100
b00004a5 1220+++ linux/Documentation/ABI/testing/sysfs-aufs 2017-07-29 12:14:25.893041746 +0200
392086de 1221@@ -0,0 +1,31 @@
7f207e10
AM
1222+What: /sys/fs/aufs/si_<id>/
1223+Date: March 2009
f6b6e03d 1224+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1225+Description:
1226+ Under /sys/fs/aufs, a directory named si_<id> is created
1227+ per aufs mount, where <id> is a unique id generated
1228+ internally.
1229+
1230+What: /sys/fs/aufs/si_<id>/br0, br1 ... brN
1231+Date: March 2009
f6b6e03d 1232+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1233+Description:
1234+ It shows the abolute path of a member directory (which
1235+ is called branch) in aufs, and its permission.
1236+
392086de
AM
1237+What: /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
1238+Date: July 2013
f6b6e03d 1239+Contact: J. R. Okajima <hooanon05g@gmail.com>
392086de
AM
1240+Description:
1241+ It shows the id of a member directory (which is called
1242+ branch) in aufs.
1243+
7f207e10
AM
1244+What: /sys/fs/aufs/si_<id>/xi_path
1245+Date: March 2009
f6b6e03d 1246+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1247+Description:
1248+ It shows the abolute path of XINO (External Inode Number
1249+ Bitmap, Translation Table and Generation Table) file
1250+ even if it is the default path.
1251+ When the aufs mount option 'noxino' is specified, it
1252+ will be empty. About XINO files, see the aufs manual.
53392da6
AM
1253diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1254--- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 1255+++ linux/Documentation/filesystems/aufs/design/01intro.txt 2018-04-15 08:49:13.394483860 +0200
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
b00004a5 1430+++ linux/Documentation/filesystems/aufs/design/02struct.txt 2018-04-15 08:49:13.394483860 +0200
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
b00004a5 1692+++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt 2018-04-15 08:49:13.394483860 +0200
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
b00004a5 1781+++ linux/Documentation/filesystems/aufs/design/03lookup.txt 2018-04-15 08:49:13.394483860 +0200
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
b00004a5 1898+++ linux/Documentation/filesystems/aufs/design/04branch.txt 2018-04-15 08:49:13.394483860 +0200
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
b00004a5 1976+++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt 2018-04-15 08:49:13.394483860 +0200
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
b00004a5 2044+++ linux/Documentation/filesystems/aufs/design/06dirren.dot 2018-04-15 08:49:13.394483860 +0200
8b6a4947
AM
2045@@ -0,0 +1,31 @@
2046+
2047+// to view this graph, run dot(1) command in GRAPHVIZ.
2048+
2049+digraph G {
2050+node [shape=box];
2051+whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
2052+
2053+node [shape=oval];
2054+
2055+aufs_rename -> whinfo [label="store/remove"];
2056+
2057+node [shape=oval];
2058+inode_list [label="h_inum list in branch\ncache"];
2059+
2060+node [shape=box];
2061+whinode [label="h_inum list file"];
2062+
2063+node [shape=oval];
2064+brmgmt [label="br_add/del/mod/umount"];
2065+
2066+brmgmt -> inode_list [label="create/remove"];
2067+brmgmt -> whinode [label="load/store"];
2068+
2069+inode_list -> whinode [style=dashed,dir=both];
2070+
2071+aufs_rename -> inode_list [label="add/del"];
2072+
2073+aufs_lookup -> inode_list [label="search"];
2074+
2075+aufs_lookup -> whinfo [label="load/remove"];
2076+}
2077diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
2078--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 2079+++ linux/Documentation/filesystems/aufs/design/06dirren.txt 2018-04-15 08:49:13.394483860 +0200
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
b00004a5 2185+++ linux/Documentation/filesystems/aufs/design/06fhsm.txt 2018-04-15 08:49:13.394483860 +0200
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
b00004a5 2309+++ linux/Documentation/filesystems/aufs/design/06mmap.txt 2018-04-15 08:49:13.394483860 +0200
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
b00004a5 2385+++ linux/Documentation/filesystems/aufs/design/06xattr.txt 2018-04-15 08:49:13.394483860 +0200
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
b00004a5 2485+++ linux/Documentation/filesystems/aufs/design/07export.txt 2018-04-15 08:49:13.394483860 +0200
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
b00004a5 2547+++ linux/Documentation/filesystems/aufs/design/08shwh.txt 2018-04-15 08:49:13.394483860 +0200
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
b00004a5 2603+++ linux/Documentation/filesystems/aufs/design/10dynop.txt 2018-04-15 08:49:13.394483860 +0200
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
9f237c51
AM
2654+++ linux/Documentation/filesystems/aufs/README 2018-12-27 13:19:17.705082621 +0100
2655@@ -0,0 +1,395 @@
53392da6 2656+
5527c038 2657+Aufs4 -- advanced multi layered unification filesystem version 4.x
53392da6
AM
2658+http://aufs.sf.net
2659+Junjiro R. Okajima
2660+
2661+
2662+0. Introduction
2663+----------------------------------------
2664+In the early days, aufs was entirely re-designed and re-implemented
7e9cd9fe 2665+Unionfs Version 1.x series. Adding many original ideas, approaches,
53392da6
AM
2666+improvements and implementations, it becomes totally different from
2667+Unionfs while keeping the basic features.
2668+Recently, Unionfs Version 2.x series begin taking some of the same
2669+approaches to aufs1's.
2670+Unionfs is being developed by Professor Erez Zadok at Stony Brook
2671+University and his team.
2672+
5527c038 2673+Aufs4 supports linux-4.0 and later, and for linux-3.x series try aufs3.
53392da6
AM
2674+If you want older kernel version support, try aufs2-2.6.git or
2675+aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
2676+
2677+Note: it becomes clear that "Aufs was rejected. Let's give it up."
38d290e6
JR
2678+ According to Christoph Hellwig, linux rejects all union-type
2679+ filesystems but UnionMount.
53392da6
AM
2680+<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2681+
38d290e6
JR
2682+PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2683+ UnionMount, and he pointed out an issue around a directory mutex
2684+ lock and aufs addressed it. But it is still unsure whether aufs will
2685+ be merged (or any other union solution).
076b876e 2686+<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
38d290e6 2687+
53392da6
AM
2688+
2689+1. Features
2690+----------------------------------------
2691+- unite several directories into a single virtual filesystem. The member
2692+ directory is called as a branch.
2693+- you can specify the permission flags to the branch, which are 'readonly',
2694+ 'readwrite' and 'whiteout-able.'
2695+- by upper writable branch, internal copyup and whiteout, files/dirs on
2696+ readonly branch are modifiable logically.
2697+- dynamic branch manipulation, add, del.
2698+- etc...
2699+
7e9cd9fe
AM
2700+Also there are many enhancements in aufs, such as:
2701+- test only the highest one for the directory permission (dirperm1)
2702+- copyup on open (coo=)
2703+- 'move' policy for copy-up between two writable branches, after
2704+ checking free space.
2705+- xattr, acl
53392da6
AM
2706+- readdir(3) in userspace.
2707+- keep inode number by external inode number table
2708+- keep the timestamps of file/dir in internal copyup operation
2709+- seekable directory, supporting NFS readdir.
2710+- whiteout is hardlinked in order to reduce the consumption of inodes
2711+ on branch
2712+- do not copyup, nor create a whiteout when it is unnecessary
2713+- revert a single systemcall when an error occurs in aufs
2714+- remount interface instead of ioctl
2715+- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2716+- loopback mounted filesystem as a branch
2717+- kernel thread for removing the dir who has a plenty of whiteouts
2718+- support copyup sparse file (a file which has a 'hole' in it)
2719+- default permission flags for branches
2720+- selectable permission flags for ro branch, whether whiteout can
2721+ exist or not
2722+- export via NFS.
2723+- support <sysfs>/fs/aufs and <debugfs>/aufs.
2724+- support multiple writable branches, some policies to select one
2725+ among multiple writable branches.
2726+- a new semantics for link(2) and rename(2) to support multiple
2727+ writable branches.
2728+- no glibc changes are required.
2729+- pseudo hardlink (hardlink over branches)
2730+- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2731+ including NFS or remote filesystem branch.
2732+- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2733+- and more...
2734+
5527c038 2735+Currently these features are dropped temporary from aufs4.
53392da6 2736+See design/08plan.txt in detail.
53392da6
AM
2737+- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2738+ (robr)
2739+- statistics of aufs thread (/sys/fs/aufs/stat)
53392da6
AM
2740+
2741+Features or just an idea in the future (see also design/*.txt),
2742+- reorder the branch index without del/re-add.
2743+- permanent xino files for NFSD
2744+- an option for refreshing the opened files after add/del branches
53392da6
AM
2745+- light version, without branch manipulation. (unnecessary?)
2746+- copyup in userspace
2747+- inotify in userspace
2748+- readv/writev
53392da6
AM
2749+
2750+
2751+2. Download
2752+----------------------------------------
5527c038
JR
2753+There are three GIT trees for aufs4, aufs4-linux.git,
2754+aufs4-standalone.git, and aufs-util.git. Note that there is no "4" in
1e00d052 2755+"aufs-util.git."
5527c038
JR
2756+While the aufs-util is always necessary, you need either of aufs4-linux
2757+or aufs4-standalone.
1e00d052 2758+
5527c038 2759+The aufs4-linux tree includes the whole linux mainline GIT tree,
1e00d052
AM
2760+git://git.kernel.org/.../torvalds/linux.git.
2761+And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
5527c038 2762+build aufs4 as an external kernel module.
2000de60 2763+Several extra patches are not included in this tree. Only
be52b249 2764+aufs4-standalone tree contains them. They are described in the later
2000de60 2765+section "Configuration and Compilation."
1e00d052 2766+
5527c038 2767+On the other hand, the aufs4-standalone tree has only aufs source files
53392da6 2768+and necessary patches, and you can select CONFIG_AUFS_FS=m.
2000de60 2769+But you need to apply all aufs patches manually.
53392da6 2770+
5527c038
JR
2771+You will find GIT branches whose name is in form of "aufs4.x" where "x"
2772+represents the linux kernel version, "linux-4.x". For instance,
2773+"aufs4.0" is for linux-4.0. For latest "linux-4.x-rcN", use
2774+"aufs4.x-rcN" branch.
1e00d052 2775+
5527c038 2776+o aufs4-linux tree
1e00d052 2777+$ git clone --reference /your/linux/git/tree \
5527c038 2778+ git://github.com/sfjro/aufs4-linux.git aufs4-linux.git
1e00d052 2779+- if you don't have linux GIT tree, then remove "--reference ..."
5527c038
JR
2780+$ cd aufs4-linux.git
2781+$ git checkout origin/aufs4.0
53392da6 2782+
2000de60
JR
2783+Or You may want to directly git-pull aufs into your linux GIT tree, and
2784+leave the patch-work to GIT.
2785+$ cd /your/linux/git/tree
5527c038
JR
2786+$ git remote add aufs4 git://github.com/sfjro/aufs4-linux.git
2787+$ git fetch aufs4
2788+$ git checkout -b my4.0 v4.0
2789+$ (add your local change...)
2790+$ git pull aufs4 aufs4.0
2791+- now you have v4.0 + your_changes + aufs4.0 in you my4.0 branch.
2000de60 2792+- you may need to solve some conflicts between your_changes and
5527c038
JR
2793+ aufs4.0. in this case, git-rerere is recommended so that you can
2794+ solve the similar conflicts automatically when you upgrade to 4.1 or
2000de60
JR
2795+ later in the future.
2796+
5527c038
JR
2797+o aufs4-standalone tree
2798+$ git clone git://github.com/sfjro/aufs4-standalone.git aufs4-standalone.git
2799+$ cd aufs4-standalone.git
2800+$ git checkout origin/aufs4.0
53392da6
AM
2801+
2802+o aufs-util tree
5527c038
JR
2803+$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2804+- note that the public aufs-util.git is on SourceForge instead of
2805+ GitHUB.
53392da6 2806+$ cd aufs-util.git
5527c038 2807+$ git checkout origin/aufs4.0
53392da6 2808+
5527c038
JR
2809+Note: The 4.x-rcN branch is to be used with `rc' kernel versions ONLY.
2810+The minor version number, 'x' in '4.x', of aufs may not always
9dbd164d
AM
2811+follow the minor version number of the kernel.
2812+Because changes in the kernel that cause the use of a new
2813+minor version number do not always require changes to aufs-util.
2814+
2815+Since aufs-util has its own minor version number, you may not be
2816+able to find a GIT branch in aufs-util for your kernel's
2817+exact minor version number.
2818+In this case, you should git-checkout the branch for the
53392da6 2819+nearest lower number.
9dbd164d
AM
2820+
2821+For (an unreleased) example:
5527c038
JR
2822+If you are using "linux-4.10" and the "aufs4.10" branch
2823+does not exist in aufs-util repository, then "aufs4.9", "aufs4.8"
9dbd164d
AM
2824+or something numerically smaller is the branch for your kernel.
2825+
53392da6
AM
2826+Also you can view all branches by
2827+ $ git branch -a
2828+
2829+
2830+3. Configuration and Compilation
2831+----------------------------------------
2832+Make sure you have git-checkout'ed the correct branch.
2833+
5527c038 2834+For aufs4-linux tree,
c06a8ce3 2835+- enable CONFIG_AUFS_FS.
1e00d052
AM
2836+- set other aufs configurations if necessary.
2837+
5527c038 2838+For aufs4-standalone tree,
53392da6
AM
2839+There are several ways to build.
2840+
2841+1.
5527c038
JR
2842+- apply ./aufs4-kbuild.patch to your kernel source files.
2843+- apply ./aufs4-base.patch too.
2844+- apply ./aufs4-mmap.patch too.
2845+- apply ./aufs4-standalone.patch too, if you have a plan to set
2846+ CONFIG_AUFS_FS=m. otherwise you don't need ./aufs4-standalone.patch.
537831f9
AM
2847+- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2848+ kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
c06a8ce3 2849+- enable CONFIG_AUFS_FS, you can select either
53392da6
AM
2850+ =m or =y.
2851+- and build your kernel as usual.
2852+- install the built kernel.
c06a8ce3
AM
2853+ Note: Since linux-3.9, every filesystem module requires an alias
2854+ "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2855+ modules.aliases file if you set CONFIG_AUFS_FS=m.
7eafdf33
AM
2856+- install the header files too by "make headers_install" to the
2857+ directory where you specify. By default, it is $PWD/usr.
b4510431 2858+ "make help" shows a brief note for headers_install.
53392da6
AM
2859+- and reboot your system.
2860+
2861+2.
2862+- module only (CONFIG_AUFS_FS=m).
5527c038
JR
2863+- apply ./aufs4-base.patch to your kernel source files.
2864+- apply ./aufs4-mmap.patch too.
2865+- apply ./aufs4-standalone.patch too.
53392da6
AM
2866+- build your kernel, don't forget "make headers_install", and reboot.
2867+- edit ./config.mk and set other aufs configurations if necessary.
b4510431 2868+ Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
53392da6
AM
2869+ every aufs configurations.
2870+- build the module by simple "make".
c06a8ce3
AM
2871+ Note: Since linux-3.9, every filesystem module requires an alias
2872+ "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2873+ modules.aliases file.
53392da6
AM
2874+- you can specify ${KDIR} make variable which points to your kernel
2875+ source tree.
2876+- install the files
2877+ + run "make install" to install the aufs module, or copy the built
b4510431
AM
2878+ $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2879+ + run "make install_headers" (instead of headers_install) to install
2880+ the modified aufs header file (you can specify DESTDIR which is
2881+ available in aufs standalone version's Makefile only), or copy
2882+ $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2883+ you like manually. By default, the target directory is $PWD/usr.
5527c038 2884+- no need to apply aufs4-kbuild.patch, nor copying source files to your
53392da6
AM
2885+ kernel source tree.
2886+
b4510431 2887+Note: The header file aufs_type.h is necessary to build aufs-util
53392da6
AM
2888+ as well as "make headers_install" in the kernel source tree.
2889+ headers_install is subject to be forgotten, but it is essentially
2890+ necessary, not only for building aufs-util.
2891+ You may not meet problems without headers_install in some older
2892+ version though.
2893+
2894+And then,
2895+- read README in aufs-util, build and install it
9dbd164d
AM
2896+- note that your distribution may contain an obsoleted version of
2897+ aufs_type.h in /usr/include/linux or something. When you build aufs
2898+ utilities, make sure that your compiler refers the correct aufs header
2899+ file which is built by "make headers_install."
53392da6
AM
2900+- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2901+ then run "make install_ulib" too. And refer to the aufs manual in
2902+ detail.
2903+
5527c038 2904+There several other patches in aufs4-standalone.git. They are all
38d290e6 2905+optional. When you meet some problems, they will help you.
5527c038 2906+- aufs4-loopback.patch
38d290e6
JR
2907+ Supports a nested loopback mount in a branch-fs. This patch is
2908+ unnecessary until aufs produces a message like "you may want to try
2909+ another patch for loopback file".
2910+- vfs-ino.patch
2911+ Modifies a system global kernel internal function get_next_ino() in
2912+ order to stop assigning 0 for an inode-number. Not directly related to
2913+ aufs, but recommended generally.
2914+- tmpfs-idr.patch
2915+ Keeps the tmpfs inode number as the lowest value. Effective to reduce
2916+ the size of aufs XINO files for tmpfs branch. Also it prevents the
2917+ duplication of inode number, which is important for backup tools and
2918+ other utilities. When you find aufs XINO files for tmpfs branch
2919+ growing too much, try this patch.
be52b249
AM
2920+- lockdep-debug.patch
2921+ Because aufs is not only an ordinary filesystem (callee of VFS), but
2922+ also a caller of VFS functions for branch filesystems, subclassing of
2923+ the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2924+ feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2925+ need to apply this debug patch to expand several constant values.
2926+ If don't know what LOCKDEP, then you don't have apply this patch.
38d290e6 2927+
53392da6
AM
2928+
2929+4. Usage
2930+----------------------------------------
2931+At first, make sure aufs-util are installed, and please read the aufs
2932+manual, aufs.5 in aufs-util.git tree.
2933+$ man -l aufs.5
2934+
2935+And then,
2936+$ mkdir /tmp/rw /tmp/aufs
2937+# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2938+
2939+Here is another example. The result is equivalent.
2940+# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2941+ Or
2942+# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2943+# mount -o remount,append:${HOME} /tmp/aufs
2944+
2945+Then, you can see whole tree of your home dir through /tmp/aufs. If
2946+you modify a file under /tmp/aufs, the one on your home directory is
2947+not affected, instead the same named file will be newly created under
2948+/tmp/rw. And all of your modification to a file will be applied to
2949+the one under /tmp/rw. This is called the file based Copy on Write
2950+(COW) method.
2951+Aufs mount options are described in aufs.5.
2952+If you run chroot or something and make your aufs as a root directory,
2953+then you need to customize the shutdown script. See the aufs manual in
2954+detail.
2955+
2956+Additionally, there are some sample usages of aufs which are a
2957+diskless system with network booting, and LiveCD over NFS.
2958+See sample dir in CVS tree on SourceForge.
2959+
2960+
2961+5. Contact
2962+----------------------------------------
2963+When you have any problems or strange behaviour in aufs, please let me
2964+know with:
2965+- /proc/mounts (instead of the output of mount(8))
2966+- /sys/module/aufs/*
2967+- /sys/fs/aufs/* (if you have them)
2968+- /debug/aufs/* (if you have them)
2969+- linux kernel version
2970+ if your kernel is not plain, for example modified by distributor,
2971+ the url where i can download its source is necessary too.
2972+- aufs version which was printed at loading the module or booting the
2973+ system, instead of the date you downloaded.
2974+- configuration (define/undefine CONFIG_AUFS_xxx)
2975+- kernel configuration or /proc/config.gz (if you have it)
2976+- behaviour which you think to be incorrect
2977+- actual operation, reproducible one is better
2978+- mailto: aufs-users at lists.sourceforge.net
2979+
2980+Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2981+and Feature Requests) on SourceForge. Please join and write to
2982+aufs-users ML.
2983+
2984+
2985+6. Acknowledgements
2986+----------------------------------------
2987+Thanks to everyone who have tried and are using aufs, whoever
2988+have reported a bug or any feedback.
2989+
2990+Especially donators:
2991+Tomas Matejicek(slax.org) made a donation (much more than once).
2992+ Since Apr 2010, Tomas M (the author of Slax and Linux Live
2993+ scripts) is making "doubling" donations.
2994+ Unfortunately I cannot list all of the donators, but I really
b4510431 2995+ appreciate.
53392da6
AM
2996+ It ends Aug 2010, but the ordinary donation URL is still available.
2997+ <http://sourceforge.net/donate/index.php?group_id=167503>
2998+Dai Itasaka made a donation (2007/8).
2999+Chuck Smith made a donation (2008/4, 10 and 12).
3000+Henk Schoneveld made a donation (2008/9).
3001+Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
3002+Francois Dupoux made a donation (2008/11).
3003+Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
3004+ aufs2 GIT tree (2009/2).
3005+William Grant made a donation (2009/3).
3006+Patrick Lane made a donation (2009/4).
3007+The Mail Archive (mail-archive.com) made donations (2009/5).
3008+Nippy Networks (Ed Wildgoose) made a donation (2009/7).
3009+New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
3010+Pavel Pronskiy made a donation (2011/2).
3011+Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
3012+ Networks (Ed Wildgoose) made a donation for hardware (2011/3).
537831f9
AM
3013+Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
3014+11).
1e00d052 3015+Sam Liddicott made a donation (2011/9).
86dc4139
AM
3016+Era Scarecrow made a donation (2013/4).
3017+Bor Ratajc made a donation (2013/4).
3018+Alessandro Gorreta made a donation (2013/4).
3019+POIRETTE Marc made a donation (2013/4).
3020+Alessandro Gorreta made a donation (2013/4).
3021+lauri kasvandik made a donation (2013/5).
392086de 3022+"pemasu from Finland" made a donation (2013/7).
523b37e3
AM
3023+The Parted Magic Project made a donation (2013/9 and 11).
3024+Pavel Barta made a donation (2013/10).
38d290e6 3025+Nikolay Pertsev made a donation (2014/5).
c2c0f25c 3026+James B made a donation (2014/7 and 2015/7).
076b876e 3027+Stefano Di Biase made a donation (2014/8).
2000de60 3028+Daniel Epellei made a donation (2015/1).
c4adf169 3029+OmegaPhil made a donation (2016/1, 2018/4).
5afbbe0d 3030+Tomasz Szewczyk made a donation (2016/4).
f2c43d5f 3031+James Burry made a donation (2016/12).
acd2b654 3032+Carsten Rose made a donation (2018/9).
9f237c51 3033+Porteus Kiosk made a donation (2018/10).
53392da6
AM
3034+
3035+Thank you very much.
3036+Donations are always, including future donations, very important and
3037+helpful for me to keep on developing aufs.
3038+
3039+
3040+7.
3041+----------------------------------------
3042+If you are an experienced user, no explanation is needed. Aufs is
3043+just a linux filesystem.
3044+
3045+
3046+Enjoy!
3047+
3048+# Local variables: ;
3049+# mode: text;
3050+# End: ;
7f207e10
AM
3051diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
3052--- /usr/share/empty/fs/aufs/aufs.h 1970-01-01 01:00:00.000000000 +0100
acd2b654
AM
3053+++ linux/fs/aufs/aufs.h 2018-10-23 12:33:35.592708932 +0200
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
9f237c51 3119+++ linux/fs/aufs/branch.c 2018-12-27 13:19:17.708416053 +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);
9f237c51 3176+ au_kfree_try_rcu(br->br_fhsm);
076b876e
AM
3177+ }
3178+
4a4d8108
AM
3179+ key = br->br_dykey;
3180+ for (i = 0; i < AuBrDynOp; i++, key++)
3181+ if (*key)
3182+ au_dy_put(*key);
3183+ else
3184+ break;
3185+
537831f9 3186+ /* recursive lock, s_umount of branch's */
acd2b654 3187+ /* synchronize_rcu(); */ /* why? */
537831f9 3188+ lockdep_off();
86dc4139 3189+ path_put(&br->br_path);
537831f9 3190+ lockdep_on();
9f237c51 3191+ au_kfree_rcu(wbr);
acd2b654
AM
3192+ au_lcnt_wait_for_fin(&br->br_nfiles);
3193+ au_lcnt_wait_for_fin(&br->br_count);
3194+ /* I don't know why, but percpu_refcount requires this */
3195+ /* synchronize_rcu(); */
9f237c51 3196+ au_kfree_rcu(br);
1facf9fc 3197+}
3198+
3199+/*
3200+ * frees all branches
3201+ */
3202+void au_br_free(struct au_sbinfo *sbinfo)
3203+{
3204+ aufs_bindex_t bmax;
3205+ struct au_branch **br;
3206+
dece6358
AM
3207+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3208+
5afbbe0d 3209+ bmax = sbinfo->si_bbot + 1;
1facf9fc 3210+ br = sbinfo->si_branch;
3211+ while (bmax--)
3212+ au_br_do_free(*br++);
3213+}
3214+
3215+/*
3216+ * find the index of a branch which is specified by @br_id.
3217+ */
3218+int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3219+{
5afbbe0d 3220+ aufs_bindex_t bindex, bbot;
1facf9fc 3221+
5afbbe0d
AM
3222+ bbot = au_sbbot(sb);
3223+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 3224+ if (au_sbr_id(sb, bindex) == br_id)
3225+ return bindex;
3226+ return -1;
3227+}
3228+
3229+/* ---------------------------------------------------------------------- */
3230+
3231+/*
3232+ * add a branch
3233+ */
3234+
b752ccd1
AM
3235+static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3236+ struct dentry *h_root)
1facf9fc 3237+{
b752ccd1
AM
3238+ if (unlikely(h_adding == h_root
3239+ || au_test_loopback_overlap(sb, h_adding)))
1facf9fc 3240+ return 1;
b752ccd1
AM
3241+ if (h_adding->d_sb != h_root->d_sb)
3242+ return 0;
3243+ return au_test_subdir(h_adding, h_root)
3244+ || au_test_subdir(h_root, h_adding);
1facf9fc 3245+}
3246+
3247+/*
3248+ * returns a newly allocated branch. @new_nbranch is a number of branches
3249+ * after adding a branch.
3250+ */
3251+static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3252+ int perm)
3253+{
3254+ struct au_branch *add_branch;
3255+ struct dentry *root;
5527c038 3256+ struct inode *inode;
4a4d8108 3257+ int err;
1facf9fc 3258+
4a4d8108 3259+ err = -ENOMEM;
be52b249 3260+ add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
1facf9fc 3261+ if (unlikely(!add_branch))
3262+ goto out;
acd2b654 3263+ add_branch->br_xino = au_xino_alloc(/*nfile*/1);
062440b3 3264+ if (unlikely(!add_branch->br_xino))
521ced18 3265+ goto out_br;
027c5e7a
AM
3266+ err = au_hnotify_init_br(add_branch, perm);
3267+ if (unlikely(err))
062440b3 3268+ goto out_xino;
027c5e7a 3269+
1facf9fc 3270+ if (au_br_writable(perm)) {
3271+ /* may be freed separately at changing the branch permission */
be52b249 3272+ add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
1facf9fc 3273+ GFP_NOFS);
3274+ if (unlikely(!add_branch->br_wbr))
027c5e7a 3275+ goto out_hnotify;
1facf9fc 3276+ }
3277+
076b876e
AM
3278+ if (au_br_fhsm(perm)) {
3279+ err = au_fhsm_br_alloc(add_branch);
3280+ if (unlikely(err))
3281+ goto out_wbr;
3282+ }
3283+
521ced18 3284+ root = sb->s_root;
e2f27e51 3285+ err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
4a4d8108 3286+ if (!err)
e2f27e51 3287+ err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
5527c038
JR
3288+ if (!err) {
3289+ inode = d_inode(root);
1c60b727
AM
3290+ err = au_hinode_realloc(au_ii(inode), new_nbranch,
3291+ /*may_shrink*/0);
5527c038 3292+ }
4a4d8108
AM
3293+ if (!err)
3294+ return add_branch; /* success */
1facf9fc 3295+
076b876e 3296+out_wbr:
9f237c51 3297+ au_kfree_rcu(add_branch->br_wbr);
027c5e7a
AM
3298+out_hnotify:
3299+ au_hnotify_fin_br(add_branch);
062440b3
AM
3300+out_xino:
3301+ au_xino_put(add_branch);
4f0767ce 3302+out_br:
9f237c51 3303+ au_kfree_rcu(add_branch);
4f0767ce 3304+out:
4a4d8108 3305+ return ERR_PTR(err);
1facf9fc 3306+}
3307+
3308+/*
3309+ * test if the branch permission is legal or not.
3310+ */
3311+static int test_br(struct inode *inode, int brperm, char *path)
3312+{
3313+ int err;
3314+
4a4d8108
AM
3315+ err = (au_br_writable(brperm) && IS_RDONLY(inode));
3316+ if (!err)
3317+ goto out;
1facf9fc 3318+
4a4d8108
AM
3319+ err = -EINVAL;
3320+ pr_err("write permission for readonly mount or inode, %s\n", path);
3321+
4f0767ce 3322+out:
1facf9fc 3323+ return err;
3324+}
3325+
3326+/*
3327+ * returns:
3328+ * 0: success, the caller will add it
3329+ * plus: success, it is already unified, the caller should ignore it
3330+ * minus: error
3331+ */
3332+static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3333+{
3334+ int err;
5afbbe0d 3335+ aufs_bindex_t bbot, bindex;
5527c038 3336+ struct dentry *root, *h_dentry;
1facf9fc 3337+ struct inode *inode, *h_inode;
3338+
3339+ root = sb->s_root;
5afbbe0d
AM
3340+ bbot = au_sbbot(sb);
3341+ if (unlikely(bbot >= 0
1facf9fc 3342+ && au_find_dbindex(root, add->path.dentry) >= 0)) {
3343+ err = 1;
3344+ if (!remount) {
3345+ err = -EINVAL;
4a4d8108 3346+ pr_err("%s duplicated\n", add->pathname);
1facf9fc 3347+ }
3348+ goto out;
3349+ }
3350+
3351+ err = -ENOSPC; /* -E2BIG; */
3352+ if (unlikely(AUFS_BRANCH_MAX <= add->bindex
5afbbe0d 3353+ || AUFS_BRANCH_MAX - 1 <= bbot)) {
4a4d8108 3354+ pr_err("number of branches exceeded %s\n", add->pathname);
1facf9fc 3355+ goto out;
3356+ }
3357+
3358+ err = -EDOM;
5afbbe0d 3359+ if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
4a4d8108 3360+ pr_err("bad index %d\n", add->bindex);
1facf9fc 3361+ goto out;
3362+ }
3363+
5527c038 3364+ inode = d_inode(add->path.dentry);
1facf9fc 3365+ err = -ENOENT;
3366+ if (unlikely(!inode->i_nlink)) {
4a4d8108 3367+ pr_err("no existence %s\n", add->pathname);
1facf9fc 3368+ goto out;
3369+ }
3370+
3371+ err = -EINVAL;
3372+ if (unlikely(inode->i_sb == sb)) {
4a4d8108 3373+ pr_err("%s must be outside\n", add->pathname);
1facf9fc 3374+ goto out;
3375+ }
3376+
3377+ if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
4a4d8108
AM
3378+ pr_err("unsupported filesystem, %s (%s)\n",
3379+ add->pathname, au_sbtype(inode->i_sb));
1facf9fc 3380+ goto out;
3381+ }
3382+
c1595e42
JR
3383+ if (unlikely(inode->i_sb->s_stack_depth)) {
3384+ pr_err("already stacked, %s (%s)\n",
3385+ add->pathname, au_sbtype(inode->i_sb));
3386+ goto out;
3387+ }
3388+
5527c038 3389+ err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
1facf9fc 3390+ if (unlikely(err))
3391+ goto out;
3392+
5afbbe0d 3393+ if (bbot < 0)
1facf9fc 3394+ return 0; /* success */
3395+
3396+ err = -EINVAL;
5afbbe0d 3397+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 3398+ if (unlikely(test_overlap(sb, add->path.dentry,
3399+ au_h_dptr(root, bindex)))) {
4a4d8108 3400+ pr_err("%s is overlapped\n", add->pathname);
1facf9fc 3401+ goto out;
3402+ }
3403+
3404+ err = 0;
3405+ if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
5527c038
JR
3406+ h_dentry = au_h_dptr(root, 0);
3407+ h_inode = d_inode(h_dentry);
1facf9fc 3408+ if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
0c3ec466
AM
3409+ || !uid_eq(h_inode->i_uid, inode->i_uid)
3410+ || !gid_eq(h_inode->i_gid, inode->i_gid))
3411+ pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3412+ add->pathname,
3413+ i_uid_read(inode), i_gid_read(inode),
3414+ (inode->i_mode & S_IALLUGO),
3415+ i_uid_read(h_inode), i_gid_read(h_inode),
3416+ (h_inode->i_mode & S_IALLUGO));
1facf9fc 3417+ }
3418+
4f0767ce 3419+out:
1facf9fc 3420+ return err;
3421+}
3422+
3423+/*
3424+ * initialize or clean the whiteouts for an adding branch
3425+ */
3426+static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
86dc4139 3427+ int new_perm)
1facf9fc 3428+{
3429+ int err, old_perm;
3430+ aufs_bindex_t bindex;
febd17d6 3431+ struct inode *h_inode;
1facf9fc 3432+ struct au_wbr *wbr;
3433+ struct au_hinode *hdir;
5527c038 3434+ struct dentry *h_dentry;
1facf9fc 3435+
86dc4139
AM
3436+ err = vfsub_mnt_want_write(au_br_mnt(br));
3437+ if (unlikely(err))
3438+ goto out;
3439+
1facf9fc 3440+ wbr = br->br_wbr;
3441+ old_perm = br->br_perm;
3442+ br->br_perm = new_perm;
3443+ hdir = NULL;
febd17d6 3444+ h_inode = NULL;
1facf9fc 3445+ bindex = au_br_index(sb, br->br_id);
3446+ if (0 <= bindex) {
5527c038 3447+ hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 3448+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 3449+ } else {
5527c038 3450+ h_dentry = au_br_dentry(br);
febd17d6
JR
3451+ h_inode = d_inode(h_dentry);
3452+ inode_lock_nested(h_inode, AuLsc_I_PARENT);
1facf9fc 3453+ }
3454+ if (!wbr)
86dc4139 3455+ err = au_wh_init(br, sb);
1facf9fc 3456+ else {
3457+ wbr_wh_write_lock(wbr);
86dc4139 3458+ err = au_wh_init(br, sb);
1facf9fc 3459+ wbr_wh_write_unlock(wbr);
3460+ }
3461+ if (hdir)
5afbbe0d 3462+ au_hn_inode_unlock(hdir);
1facf9fc 3463+ else
febd17d6 3464+ inode_unlock(h_inode);
86dc4139 3465+ vfsub_mnt_drop_write(au_br_mnt(br));
1facf9fc 3466+ br->br_perm = old_perm;
3467+
3468+ if (!err && wbr && !au_br_writable(new_perm)) {
9f237c51 3469+ au_kfree_rcu(wbr);
1facf9fc 3470+ br->br_wbr = NULL;
3471+ }
3472+
86dc4139 3473+out:
1facf9fc 3474+ return err;
3475+}
3476+
3477+static int au_wbr_init(struct au_branch *br, struct super_block *sb,
86dc4139 3478+ int perm)
1facf9fc 3479+{
3480+ int err;
4a4d8108 3481+ struct kstatfs kst;
1facf9fc 3482+ struct au_wbr *wbr;
3483+
3484+ wbr = br->br_wbr;
dece6358 3485+ au_rw_init(&wbr->wbr_wh_rwsem);
1facf9fc 3486+ atomic_set(&wbr->wbr_wh_running, 0);
1facf9fc 3487+
4a4d8108
AM
3488+ /*
3489+ * a limit for rmdir/rename a dir
523b37e3 3490+ * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
4a4d8108 3491+ */
86dc4139 3492+ err = vfs_statfs(&br->br_path, &kst);
4a4d8108
AM
3493+ if (unlikely(err))
3494+ goto out;
3495+ err = -EINVAL;
3496+ if (kst.f_namelen >= NAME_MAX)
86dc4139 3497+ err = au_br_init_wh(sb, br, perm);
4a4d8108 3498+ else
523b37e3
AM
3499+ pr_err("%pd(%s), unsupported namelen %ld\n",
3500+ au_br_dentry(br),
86dc4139 3501+ au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
1facf9fc 3502+
4f0767ce 3503+out:
1facf9fc 3504+ return err;
3505+}
3506+
c1595e42 3507+/* initialize a new branch */
1facf9fc 3508+static int au_br_init(struct au_branch *br, struct super_block *sb,
3509+ struct au_opt_add *add)
3510+{
3511+ int err;
062440b3
AM
3512+ struct au_branch *brbase;
3513+ struct file *xf;
5527c038 3514+ struct inode *h_inode;
1facf9fc 3515+
3516+ err = 0;
1facf9fc 3517+ br->br_perm = add->perm;
86dc4139 3518+ br->br_path = add->path; /* set first, path_get() later */
4a4d8108 3519+ spin_lock_init(&br->br_dykey_lock);
acd2b654
AM
3520+ au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3521+ au_lcnt_init(&br->br_count, /*release*/NULL);
1facf9fc 3522+ br->br_id = au_new_br_id(sb);
7f207e10 3523+ AuDebugOn(br->br_id < 0);
1facf9fc 3524+
8b6a4947
AM
3525+ /* always, regardless the given option */
3526+ err = au_dr_br_init(sb, br, &add->path);
3527+ if (unlikely(err))
3528+ goto out_err;
3529+
1facf9fc 3530+ if (au_br_writable(add->perm)) {
86dc4139 3531+ err = au_wbr_init(br, sb, add->perm);
1facf9fc 3532+ if (unlikely(err))
b752ccd1 3533+ goto out_err;
1facf9fc 3534+ }
3535+
3536+ if (au_opt_test(au_mntflags(sb), XINO)) {
062440b3 3537+ brbase = au_sbr(sb, 0);
acd2b654 3538+ xf = au_xino_file(brbase->br_xino, /*idx*/-1);
062440b3 3539+ AuDebugOn(!xf);
5527c038 3540+ h_inode = d_inode(add->path.dentry);
062440b3 3541+ err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
1facf9fc 3542+ if (unlikely(err)) {
acd2b654 3543+ AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
b752ccd1 3544+ goto out_err;
1facf9fc 3545+ }
3546+ }
3547+
3548+ sysaufs_br_init(br);
86dc4139 3549+ path_get(&br->br_path);
b752ccd1 3550+ goto out; /* success */
1facf9fc 3551+
4f0767ce 3552+out_err:
86dc4139 3553+ memset(&br->br_path, 0, sizeof(br->br_path));
4f0767ce 3554+out:
1facf9fc 3555+ return err;
3556+}
3557+
3558+static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
5afbbe0d 3559+ struct au_branch *br, aufs_bindex_t bbot,
1facf9fc 3560+ aufs_bindex_t amount)
3561+{
3562+ struct au_branch **brp;
3563+
dece6358
AM
3564+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3565+
1facf9fc 3566+ brp = sbinfo->si_branch + bindex;
3567+ memmove(brp + 1, brp, sizeof(*brp) * amount);
3568+ *brp = br;
5afbbe0d
AM
3569+ sbinfo->si_bbot++;
3570+ if (unlikely(bbot < 0))
3571+ sbinfo->si_bbot = 0;
1facf9fc 3572+}
3573+
3574+static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
5afbbe0d 3575+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3576+{
3577+ struct au_hdentry *hdp;
3578+
1308ab2a 3579+ AuRwMustWriteLock(&dinfo->di_rwsem);
3580+
5afbbe0d 3581+ hdp = au_hdentry(dinfo, bindex);
1facf9fc 3582+ memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3583+ au_h_dentry_init(hdp);
5afbbe0d
AM
3584+ dinfo->di_bbot++;
3585+ if (unlikely(bbot < 0))
3586+ dinfo->di_btop = 0;
1facf9fc 3587+}
3588+
3589+static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
5afbbe0d 3590+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3591+{
3592+ struct au_hinode *hip;
3593+
1308ab2a 3594+ AuRwMustWriteLock(&iinfo->ii_rwsem);
3595+
5afbbe0d 3596+ hip = au_hinode(iinfo, bindex);
1facf9fc 3597+ memmove(hip + 1, hip, sizeof(*hip) * amount);
5afbbe0d
AM
3598+ au_hinode_init(hip);
3599+ iinfo->ii_bbot++;
3600+ if (unlikely(bbot < 0))
3601+ iinfo->ii_btop = 0;
1facf9fc 3602+}
3603+
86dc4139
AM
3604+static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3605+ aufs_bindex_t bindex)
1facf9fc 3606+{
86dc4139 3607+ struct dentry *root, *h_dentry;
5527c038 3608+ struct inode *root_inode, *h_inode;
5afbbe0d 3609+ aufs_bindex_t bbot, amount;
1facf9fc 3610+
3611+ root = sb->s_root;
5527c038 3612+ root_inode = d_inode(root);
5afbbe0d
AM
3613+ bbot = au_sbbot(sb);
3614+ amount = bbot + 1 - bindex;
86dc4139 3615+ h_dentry = au_br_dentry(br);
53392da6 3616+ au_sbilist_lock();
5afbbe0d
AM
3617+ au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3618+ au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3619+ au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
1facf9fc 3620+ au_set_h_dptr(root, bindex, dget(h_dentry));
5527c038
JR
3621+ h_inode = d_inode(h_dentry);
3622+ au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
53392da6 3623+ au_sbilist_unlock();
1facf9fc 3624+}
3625+
3626+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3627+{
3628+ int err;
5afbbe0d 3629+ aufs_bindex_t bbot, add_bindex;
1facf9fc 3630+ struct dentry *root, *h_dentry;
3631+ struct inode *root_inode;
3632+ struct au_branch *add_branch;
3633+
3634+ root = sb->s_root;
5527c038 3635+ root_inode = d_inode(root);
1facf9fc 3636+ IMustLock(root_inode);
5afbbe0d 3637+ IiMustWriteLock(root_inode);
1facf9fc 3638+ err = test_add(sb, add, remount);
3639+ if (unlikely(err < 0))
3640+ goto out;
3641+ if (err) {
3642+ err = 0;
3643+ goto out; /* success */
3644+ }
3645+
5afbbe0d
AM
3646+ bbot = au_sbbot(sb);
3647+ add_branch = au_br_alloc(sb, bbot + 2, add->perm);
1facf9fc 3648+ err = PTR_ERR(add_branch);
3649+ if (IS_ERR(add_branch))
3650+ goto out;
3651+
3652+ err = au_br_init(add_branch, sb, add);
3653+ if (unlikely(err)) {
3654+ au_br_do_free(add_branch);
3655+ goto out;
3656+ }
3657+
3658+ add_bindex = add->bindex;
062440b3
AM
3659+ sysaufs_brs_del(sb, add_bindex); /* remove successors */
3660+ au_br_do_add(sb, add_branch, add_bindex);
3661+ sysaufs_brs_add(sb, add_bindex); /* append successors */
3662+ dbgaufs_brs_add(sb, add_bindex, /*topdown*/0); /* rename successors */
1facf9fc 3663+
86dc4139 3664+ h_dentry = add->path.dentry;
1308ab2a 3665+ if (!add_bindex) {
1facf9fc 3666+ au_cpup_attr_all(root_inode, /*force*/1);
1308ab2a 3667+ sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3668+ } else
5527c038 3669+ au_add_nlink(root_inode, d_inode(h_dentry));
1facf9fc 3670+
4f0767ce 3671+out:
1facf9fc 3672+ return err;
3673+}
3674+
3675+/* ---------------------------------------------------------------------- */
3676+
79b8bda9 3677+static unsigned long long au_farray_cb(struct super_block *sb, void *a,
076b876e
AM
3678+ unsigned long long max __maybe_unused,
3679+ void *arg)
3680+{
3681+ unsigned long long n;
3682+ struct file **p, *f;
8b6a4947
AM
3683+ struct hlist_bl_head *files;
3684+ struct hlist_bl_node *pos;
076b876e 3685+ struct au_finfo *finfo;
076b876e
AM
3686+
3687+ n = 0;
3688+ p = a;
3689+ files = &au_sbi(sb)->si_files;
8b6a4947
AM
3690+ hlist_bl_lock(files);
3691+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
076b876e
AM
3692+ f = finfo->fi_file;
3693+ if (file_count(f)
3694+ && !special_file(file_inode(f)->i_mode)) {
3695+ get_file(f);
3696+ *p++ = f;
3697+ n++;
3698+ AuDebugOn(n > max);
3699+ }
3700+ }
8b6a4947 3701+ hlist_bl_unlock(files);
076b876e
AM
3702+
3703+ return n;
3704+}
3705+
3706+static struct file **au_farray_alloc(struct super_block *sb,
3707+ unsigned long long *max)
3708+{
acd2b654
AM
3709+ struct au_sbinfo *sbi;
3710+
3711+ sbi = au_sbi(sb);
3712+ *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
79b8bda9 3713+ return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
076b876e
AM
3714+}
3715+
3716+static void au_farray_free(struct file **a, unsigned long long max)
3717+{
3718+ unsigned long long ull;
3719+
3720+ for (ull = 0; ull < max; ull++)
3721+ if (a[ull])
3722+ fput(a[ull]);
be52b249 3723+ kvfree(a);
076b876e
AM
3724+}
3725+
3726+/* ---------------------------------------------------------------------- */
3727+
1facf9fc 3728+/*
3729+ * delete a branch
3730+ */
3731+
3732+/* to show the line number, do not make it inlined function */
4a4d8108 3733+#define AuVerbose(do_info, fmt, ...) do { \
1facf9fc 3734+ if (do_info) \
4a4d8108 3735+ pr_info(fmt, ##__VA_ARGS__); \
1facf9fc 3736+} while (0)
3737+
5afbbe0d
AM
3738+static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3739+ aufs_bindex_t bbot)
027c5e7a 3740+{
5afbbe0d 3741+ return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
027c5e7a
AM
3742+}
3743+
5afbbe0d
AM
3744+static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3745+ aufs_bindex_t bbot)
027c5e7a 3746+{
5afbbe0d 3747+ return au_test_ibusy(d_inode(dentry), btop, bbot);
027c5e7a
AM
3748+}
3749+
1facf9fc 3750+/*
3751+ * test if the branch is deletable or not.
3752+ */
3753+static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
b752ccd1 3754+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3755+{
3756+ int err, i, j, ndentry;
5afbbe0d 3757+ aufs_bindex_t btop, bbot;
1facf9fc 3758+ struct au_dcsub_pages dpages;
3759+ struct au_dpage *dpage;
3760+ struct dentry *d;
1facf9fc 3761+
3762+ err = au_dpages_init(&dpages, GFP_NOFS);
3763+ if (unlikely(err))
3764+ goto out;
3765+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
3766+ if (unlikely(err))
3767+ goto out_dpages;
3768+
1facf9fc 3769+ for (i = 0; !err && i < dpages.ndpage; i++) {
3770+ dpage = dpages.dpages + i;
3771+ ndentry = dpage->ndentry;
3772+ for (j = 0; !err && j < ndentry; j++) {
3773+ d = dpage->dentries[j];
c1595e42 3774+ AuDebugOn(au_dcount(d) <= 0);
027c5e7a 3775+ if (!au_digen_test(d, sigen)) {
1facf9fc 3776+ di_read_lock_child(d, AuLock_IR);
027c5e7a
AM
3777+ if (unlikely(au_dbrange_test(d))) {
3778+ di_read_unlock(d, AuLock_IR);
3779+ continue;
3780+ }
3781+ } else {
1facf9fc 3782+ di_write_lock_child(d);
027c5e7a
AM
3783+ if (unlikely(au_dbrange_test(d))) {
3784+ di_write_unlock(d);
3785+ continue;
3786+ }
1facf9fc 3787+ err = au_reval_dpath(d, sigen);
3788+ if (!err)
3789+ di_downgrade_lock(d, AuLock_IR);
3790+ else {
3791+ di_write_unlock(d);
3792+ break;
3793+ }
3794+ }
3795+
027c5e7a 3796+ /* AuDbgDentry(d); */
5afbbe0d
AM
3797+ btop = au_dbtop(d);
3798+ bbot = au_dbbot(d);
3799+ if (btop <= bindex
3800+ && bindex <= bbot
1facf9fc 3801+ && au_h_dptr(d, bindex)
5afbbe0d 3802+ && au_test_dbusy(d, btop, bbot)) {
1facf9fc 3803+ err = -EBUSY;
523b37e3 3804+ AuVerbose(verbose, "busy %pd\n", d);
027c5e7a 3805+ AuDbgDentry(d);
1facf9fc 3806+ }
3807+ di_read_unlock(d, AuLock_IR);
3808+ }
3809+ }
3810+
4f0767ce 3811+out_dpages:
1facf9fc 3812+ au_dpages_free(&dpages);
4f0767ce 3813+out:
1facf9fc 3814+ return err;
3815+}
3816+
3817+static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
b752ccd1 3818+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3819+{
3820+ int err;
7f207e10
AM
3821+ unsigned long long max, ull;
3822+ struct inode *i, **array;
5afbbe0d 3823+ aufs_bindex_t btop, bbot;
1facf9fc 3824+
7f207e10
AM
3825+ array = au_iarray_alloc(sb, &max);
3826+ err = PTR_ERR(array);
3827+ if (IS_ERR(array))
3828+ goto out;
3829+
1facf9fc 3830+ err = 0;
7f207e10
AM
3831+ AuDbg("b%d\n", bindex);
3832+ for (ull = 0; !err && ull < max; ull++) {
3833+ i = array[ull];
076b876e
AM
3834+ if (unlikely(!i))
3835+ break;
7f207e10 3836+ if (i->i_ino == AUFS_ROOT_INO)
1facf9fc 3837+ continue;
3838+
7f207e10 3839+ /* AuDbgInode(i); */
537831f9 3840+ if (au_iigen(i, NULL) == sigen)
1facf9fc 3841+ ii_read_lock_child(i);
3842+ else {
3843+ ii_write_lock_child(i);
027c5e7a
AM
3844+ err = au_refresh_hinode_self(i);
3845+ au_iigen_dec(i);
1facf9fc 3846+ if (!err)
3847+ ii_downgrade_lock(i);
3848+ else {
3849+ ii_write_unlock(i);
3850+ break;
3851+ }
3852+ }
3853+
5afbbe0d
AM
3854+ btop = au_ibtop(i);
3855+ bbot = au_ibbot(i);
3856+ if (btop <= bindex
3857+ && bindex <= bbot
1facf9fc 3858+ && au_h_iptr(i, bindex)
5afbbe0d 3859+ && au_test_ibusy(i, btop, bbot)) {
1facf9fc 3860+ err = -EBUSY;
3861+ AuVerbose(verbose, "busy i%lu\n", i->i_ino);
7f207e10 3862+ AuDbgInode(i);
1facf9fc 3863+ }
3864+ ii_read_unlock(i);
3865+ }
7f207e10 3866+ au_iarray_free(array, max);
1facf9fc 3867+
7f207e10 3868+out:
1facf9fc 3869+ return err;
3870+}
3871+
b752ccd1
AM
3872+static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3873+ const unsigned int verbose)
1facf9fc 3874+{
3875+ int err;
3876+ unsigned int sigen;
3877+
3878+ sigen = au_sigen(root->d_sb);
3879+ DiMustNoWaiters(root);
5527c038 3880+ IiMustNoWaiters(d_inode(root));
1facf9fc 3881+ di_write_unlock(root);
b752ccd1 3882+ err = test_dentry_busy(root, bindex, sigen, verbose);
1facf9fc 3883+ if (!err)
b752ccd1 3884+ err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
1facf9fc 3885+ di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3886+
3887+ return err;
3888+}
3889+
076b876e
AM
3890+static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3891+ struct file **to_free, int *idx)
3892+{
3893+ int err;
c1595e42 3894+ unsigned char matched, root;
5afbbe0d 3895+ aufs_bindex_t bindex, bbot;
076b876e
AM
3896+ struct au_fidir *fidir;
3897+ struct au_hfile *hfile;
3898+
3899+ err = 0;
2000de60 3900+ root = IS_ROOT(file->f_path.dentry);
c1595e42
JR
3901+ if (root) {
3902+ get_file(file);
3903+ to_free[*idx] = file;
3904+ (*idx)++;
3905+ goto out;
3906+ }
3907+
076b876e 3908+ matched = 0;
076b876e
AM
3909+ fidir = au_fi(file)->fi_hdir;
3910+ AuDebugOn(!fidir);
5afbbe0d
AM
3911+ bbot = au_fbbot_dir(file);
3912+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
076b876e
AM
3913+ hfile = fidir->fd_hfile + bindex;
3914+ if (!hfile->hf_file)
3915+ continue;
3916+
c1595e42 3917+ if (hfile->hf_br->br_id == br_id) {
076b876e 3918+ matched = 1;
076b876e 3919+ break;
c1595e42 3920+ }
076b876e 3921+ }
c1595e42 3922+ if (matched)
076b876e
AM
3923+ err = -EBUSY;
3924+
3925+out:
3926+ return err;
3927+}
3928+
3929+static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3930+ struct file **to_free, int opened)
3931+{
3932+ int err, idx;
3933+ unsigned long long ull, max;
5afbbe0d 3934+ aufs_bindex_t btop;
076b876e 3935+ struct file *file, **array;
076b876e
AM
3936+ struct dentry *root;
3937+ struct au_hfile *hfile;
3938+
3939+ array = au_farray_alloc(sb, &max);
3940+ err = PTR_ERR(array);
3941+ if (IS_ERR(array))
3942+ goto out;
3943+
3944+ err = 0;
3945+ idx = 0;
3946+ root = sb->s_root;
3947+ di_write_unlock(root);
3948+ for (ull = 0; ull < max; ull++) {
3949+ file = array[ull];
3950+ if (unlikely(!file))
3951+ break;
3952+
3953+ /* AuDbg("%pD\n", file); */
3954+ fi_read_lock(file);
5afbbe0d 3955+ btop = au_fbtop(file);
2000de60 3956+ if (!d_is_dir(file->f_path.dentry)) {
076b876e
AM
3957+ hfile = &au_fi(file)->fi_htop;
3958+ if (hfile->hf_br->br_id == br_id)
3959+ err = -EBUSY;
3960+ } else
3961+ err = test_dir_busy(file, br_id, to_free, &idx);
3962+ fi_read_unlock(file);
3963+ if (unlikely(err))
3964+ break;
3965+ }
3966+ di_write_lock_child(root);
3967+ au_farray_free(array, max);
3968+ AuDebugOn(idx > opened);
3969+
3970+out:
3971+ return err;
3972+}
3973+
3974+static void br_del_file(struct file **to_free, unsigned long long opened,
062440b3 3975+ aufs_bindex_t br_id)
076b876e
AM
3976+{
3977+ unsigned long long ull;
5afbbe0d 3978+ aufs_bindex_t bindex, btop, bbot, bfound;
076b876e
AM
3979+ struct file *file;
3980+ struct au_fidir *fidir;
3981+ struct au_hfile *hfile;
3982+
3983+ for (ull = 0; ull < opened; ull++) {
3984+ file = to_free[ull];
3985+ if (unlikely(!file))
3986+ break;
3987+
3988+ /* AuDbg("%pD\n", file); */
2000de60 3989+ AuDebugOn(!d_is_dir(file->f_path.dentry));
076b876e
AM
3990+ bfound = -1;
3991+ fidir = au_fi(file)->fi_hdir;
3992+ AuDebugOn(!fidir);
3993+ fi_write_lock(file);
5afbbe0d
AM
3994+ btop = au_fbtop(file);
3995+ bbot = au_fbbot_dir(file);
3996+ for (bindex = btop; bindex <= bbot; bindex++) {
076b876e
AM
3997+ hfile = fidir->fd_hfile + bindex;
3998+ if (!hfile->hf_file)
3999+ continue;
4000+
4001+ if (hfile->hf_br->br_id == br_id) {
4002+ bfound = bindex;
4003+ break;
4004+ }
4005+ }
4006+ AuDebugOn(bfound < 0);
4007+ au_set_h_fptr(file, bfound, NULL);
5afbbe0d
AM
4008+ if (bfound == btop) {
4009+ for (btop++; btop <= bbot; btop++)
4010+ if (au_hf_dir(file, btop)) {
4011+ au_set_fbtop(file, btop);
076b876e
AM
4012+ break;
4013+ }
4014+ }
4015+ fi_write_unlock(file);
4016+ }
4017+}
4018+
1facf9fc 4019+static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
4020+ const aufs_bindex_t bindex,
5afbbe0d 4021+ const aufs_bindex_t bbot)
1facf9fc 4022+{
4023+ struct au_branch **brp, **p;
4024+
dece6358
AM
4025+ AuRwMustWriteLock(&sbinfo->si_rwsem);
4026+
1facf9fc 4027+ brp = sbinfo->si_branch + bindex;
5afbbe0d
AM
4028+ if (bindex < bbot)
4029+ memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
4030+ sbinfo->si_branch[0 + bbot] = NULL;
4031+ sbinfo->si_bbot--;
1facf9fc 4032+
e2f27e51
AM
4033+ p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
4034+ /*may_shrink*/1);
1facf9fc 4035+ if (p)
4036+ sbinfo->si_branch = p;
4a4d8108 4037+ /* harmless error */
1facf9fc 4038+}
4039+
4040+static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
5afbbe0d 4041+ const aufs_bindex_t bbot)
1facf9fc 4042+{
4043+ struct au_hdentry *hdp, *p;
4044+
1308ab2a 4045+ AuRwMustWriteLock(&dinfo->di_rwsem);
4046+
5afbbe0d
AM
4047+ hdp = au_hdentry(dinfo, bindex);
4048+ if (bindex < bbot)
4049+ memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
4050+ /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
4051+ dinfo->di_bbot--;
1facf9fc 4052+
e2f27e51
AM
4053+ p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
4054+ /*may_shrink*/1);
1facf9fc 4055+ if (p)
4056+ dinfo->di_hdentry = p;
4a4d8108 4057+ /* harmless error */
1facf9fc 4058+}
4059+
4060+static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
5afbbe0d 4061+ const aufs_bindex_t bbot)
1facf9fc 4062+{
4063+ struct au_hinode *hip, *p;
4064+
1308ab2a 4065+ AuRwMustWriteLock(&iinfo->ii_rwsem);
4066+
5afbbe0d
AM
4067+ hip = au_hinode(iinfo, bindex);
4068+ if (bindex < bbot)
4069+ memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
4070+ /* au_hinode_init(au_hinode(iinfo, bbot)); */
4071+ iinfo->ii_bbot--;
1facf9fc 4072+
e2f27e51
AM
4073+ p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
4074+ /*may_shrink*/1);
1facf9fc 4075+ if (p)
4076+ iinfo->ii_hinode = p;
4a4d8108 4077+ /* harmless error */
1facf9fc 4078+}
4079+
4080+static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
4081+ struct au_branch *br)
4082+{
5afbbe0d 4083+ aufs_bindex_t bbot;
1facf9fc 4084+ struct au_sbinfo *sbinfo;
53392da6
AM
4085+ struct dentry *root, *h_root;
4086+ struct inode *inode, *h_inode;
4087+ struct au_hinode *hinode;
1facf9fc 4088+
dece6358
AM
4089+ SiMustWriteLock(sb);
4090+
1facf9fc 4091+ root = sb->s_root;
5527c038 4092+ inode = d_inode(root);
1facf9fc 4093+ sbinfo = au_sbi(sb);
5afbbe0d 4094+ bbot = sbinfo->si_bbot;
1facf9fc 4095+
53392da6
AM
4096+ h_root = au_h_dptr(root, bindex);
4097+ hinode = au_hi(inode, bindex);
4098+ h_inode = au_igrab(hinode->hi_inode);
4099+ au_hiput(hinode);
1facf9fc 4100+
53392da6 4101+ au_sbilist_lock();
5afbbe0d
AM
4102+ au_br_do_del_brp(sbinfo, bindex, bbot);
4103+ au_br_do_del_hdp(au_di(root), bindex, bbot);
4104+ au_br_do_del_hip(au_ii(inode), bindex, bbot);
53392da6
AM
4105+ au_sbilist_unlock();
4106+
8b6a4947
AM
4107+ /* ignore an error */
4108+ au_dr_br_fin(sb, br); /* always, regardless the mount option */
4109+
53392da6
AM
4110+ dput(h_root);
4111+ iput(h_inode);
4112+ au_br_do_free(br);
1facf9fc 4113+}
4114+
79b8bda9
AM
4115+static unsigned long long empty_cb(struct super_block *sb, void *array,
4116+ unsigned long long max, void *arg)
076b876e
AM
4117+{
4118+ return max;
4119+}
4120+
1facf9fc 4121+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
4122+{
4123+ int err, rerr, i;
076b876e 4124+ unsigned long long opened;
1facf9fc 4125+ unsigned int mnt_flags;
5afbbe0d 4126+ aufs_bindex_t bindex, bbot, br_id;
1facf9fc 4127+ unsigned char do_wh, verbose;
4128+ struct au_branch *br;
4129+ struct au_wbr *wbr;
076b876e
AM
4130+ struct dentry *root;
4131+ struct file **to_free;
1facf9fc 4132+
4133+ err = 0;
076b876e
AM
4134+ opened = 0;
4135+ to_free = NULL;
4136+ root = sb->s_root;
4137+ bindex = au_find_dbindex(root, del->h_path.dentry);
1facf9fc 4138+ if (bindex < 0) {
4139+ if (remount)
4140+ goto out; /* success */
4141+ err = -ENOENT;
4a4d8108 4142+ pr_err("%s no such branch\n", del->pathname);
1facf9fc 4143+ goto out;
4144+ }
4145+ AuDbg("bindex b%d\n", bindex);
4146+
4147+ err = -EBUSY;
4148+ mnt_flags = au_mntflags(sb);
4149+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
5afbbe0d
AM
4150+ bbot = au_sbbot(sb);
4151+ if (unlikely(!bbot)) {
1facf9fc 4152+ AuVerbose(verbose, "no more branches left\n");
4153+ goto out;
4154+ }
acd2b654 4155+
1facf9fc 4156+ br = au_sbr(sb, bindex);
86dc4139 4157+ AuDebugOn(!path_equal(&br->br_path, &del->h_path));
acd2b654
AM
4158+ if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
4159+ AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
4160+ goto out;
4161+ }
076b876e
AM
4162+
4163+ br_id = br->br_id;
acd2b654 4164+ opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
076b876e 4165+ if (unlikely(opened)) {
79b8bda9 4166+ to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
076b876e
AM
4167+ err = PTR_ERR(to_free);
4168+ if (IS_ERR(to_free))
4169+ goto out;
4170+
4171+ err = test_file_busy(sb, br_id, to_free, opened);
4172+ if (unlikely(err)) {
4173+ AuVerbose(verbose, "%llu file(s) opened\n", opened);
4174+ goto out;
4175+ }
1facf9fc 4176+ }
4177+
4178+ wbr = br->br_wbr;
4179+ do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
4180+ if (do_wh) {
1308ab2a 4181+ /* instead of WbrWhMustWriteLock(wbr) */
4182+ SiMustWriteLock(sb);
1facf9fc 4183+ for (i = 0; i < AuBrWh_Last; i++) {
4184+ dput(wbr->wbr_wh[i]);
4185+ wbr->wbr_wh[i] = NULL;
4186+ }
4187+ }
4188+
076b876e 4189+ err = test_children_busy(root, bindex, verbose);
1facf9fc 4190+ if (unlikely(err)) {
4191+ if (do_wh)
4192+ goto out_wh;
4193+ goto out;
4194+ }
4195+
4196+ err = 0;
076b876e
AM
4197+ if (to_free) {
4198+ /*
4199+ * now we confirmed the branch is deletable.
4200+ * let's free the remaining opened dirs on the branch.
4201+ */
4202+ di_write_unlock(root);
4203+ br_del_file(to_free, opened, br_id);
4204+ di_write_lock_child(root);
4205+ }
4206+
062440b3
AM
4207+ sysaufs_brs_del(sb, bindex); /* remove successors */
4208+ dbgaufs_xino_del(br); /* remove one */
4209+ au_br_do_del(sb, bindex, br);
4210+ sysaufs_brs_add(sb, bindex); /* append successors */
4211+ dbgaufs_brs_add(sb, bindex, /*topdown*/1); /* rename successors */
1facf9fc 4212+
1308ab2a 4213+ if (!bindex) {
5527c038 4214+ au_cpup_attr_all(d_inode(root), /*force*/1);
1308ab2a 4215+ sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
4216+ } else
5527c038 4217+ au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
1facf9fc 4218+ if (au_opt_test(mnt_flags, PLINK))
4219+ au_plink_half_refresh(sb, br_id);
4220+
1facf9fc 4221+ goto out; /* success */
4222+
4f0767ce 4223+out_wh:
1facf9fc 4224+ /* revert */
86dc4139 4225+ rerr = au_br_init_wh(sb, br, br->br_perm);
1facf9fc 4226+ if (rerr)
0c3ec466
AM
4227+ pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4228+ del->pathname, rerr);
4f0767ce 4229+out:
076b876e
AM
4230+ if (to_free)
4231+ au_farray_free(to_free, opened);
1facf9fc 4232+ return err;
4233+}
4234+
4235+/* ---------------------------------------------------------------------- */
4236+
027c5e7a
AM
4237+static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4238+{
4239+ int err;
5afbbe0d 4240+ aufs_bindex_t btop, bbot;
027c5e7a
AM
4241+ struct aufs_ibusy ibusy;
4242+ struct inode *inode, *h_inode;
4243+
4244+ err = -EPERM;
4245+ if (unlikely(!capable(CAP_SYS_ADMIN)))
4246+ goto out;
4247+
4248+ err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4249+ if (!err)
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)) {
9f237c51 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);
9f237c51 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:
9f237c51 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
9f237c51
AM
4545+++ linux/fs/aufs/branch.h 2018-12-27 13:19:17.708416053 +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 */
9f237c51 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
b00004a5 4914+++ linux/fs/aufs/conf.mk 2018-06-04 09:08:09.181412645 +0200
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
9f237c51
AM
4958+++ linux/fs/aufs/cpup.c 2018-12-27 13:19:17.708416053 +0100
4959@@ -0,0 +1,1458 @@
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;
9f237c51 5299+ struct super_block *h_sb;
1facf9fc 5300+
5301+ err = -ENOMEM;
9f237c51
AM
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);
9f237c51
AM
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;
9f237c51 5355+ loff_t lo;
1c60b727
AM
5356+ struct super_block *h_src_sb;
5357+ struct inode *h_src_inode;
5358+
5359+ h_src_inode = file_inode(src);
5360+ h_src_sb = h_src_inode->i_sb;
5361+ if (h_src_sb != file_inode(dst)->i_sb
9f237c51 5362+ || !dst->f_op->remap_file_range) {
1c60b727
AM
5363+ err = au_do_copy(dst, src, len);
5364+ goto out;
5365+ }
5366+
5367+ if (!au_test_nfs(h_src_sb)) {
3c1bdaff 5368+ inode_unlock_shared(h_src_inode);
9f237c51 5369+ lo = vfsub_clone_file_range(src, dst, len);
be118d29 5370+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
1c60b727 5371+ } else
9f237c51
AM
5372+ lo = vfsub_clone_file_range(src, dst, len);
5373+ if (lo == len) {
5374+ err = 0;
5375+ goto out; /* success */
5376+ } else if (lo >= 0)
5377+ /* todo: possible? */
5378+ /* paritially succeeded */
5379+ AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5380+ else if (lo != -EOPNOTSUPP) {
5381+ /* older XFS has a condition in cloning */
5382+ err = lo;
1c60b727 5383+ goto out;
9f237c51 5384+ }
1c60b727
AM
5385+
5386+ /* the backend fs on NFS may not support cloning */
5387+ err = au_do_copy(dst, src, len);
5388+
5389+out:
5390+ AuTraceErr(err);
5391+ return err;
5392+}
5393+
1facf9fc 5394+/*
5395+ * to support a sparse file which is opened with O_APPEND,
5396+ * we need to close the file.
5397+ */
c2b27bf2 5398+static int au_cp_regular(struct au_cp_generic *cpg)
1facf9fc 5399+{
5400+ int err, i;
5401+ enum { SRC, DST };
5402+ struct {
5403+ aufs_bindex_t bindex;
5404+ unsigned int flags;
5405+ struct dentry *dentry;
392086de 5406+ int force_wr;
1facf9fc 5407+ struct file *file;
1facf9fc 5408+ } *f, file[] = {
5409+ {
c2b27bf2 5410+ .bindex = cpg->bsrc,
1facf9fc 5411+ .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
1facf9fc 5412+ },
5413+ {
c2b27bf2 5414+ .bindex = cpg->bdst,
1facf9fc 5415+ .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
392086de 5416+ .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
1facf9fc 5417+ }
5418+ };
acd2b654 5419+ struct au_branch *br;
521ced18 5420+ struct super_block *sb, *h_src_sb;
e2f27e51 5421+ struct inode *h_src_inode;
8cdd5066 5422+ struct task_struct *tsk = current;
1facf9fc 5423+
5424+ /* bsrc branch can be ro/rw. */
c2b27bf2 5425+ sb = cpg->dentry->d_sb;
1facf9fc 5426+ f = file;
5427+ for (i = 0; i < 2; i++, f++) {
c2b27bf2
AM
5428+ f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5429+ f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
392086de 5430+ /*file*/NULL, f->force_wr);
9f237c51
AM
5431+ if (IS_ERR(f->file)) {
5432+ err = PTR_ERR(f->file);
5433+ if (i == SRC)
5434+ goto out;
5435+ else
5436+ goto out_src;
5437+ }
1facf9fc 5438+ }
5439+
5440+ /* try stopping to update while we copyup */
e2f27e51 5441+ h_src_inode = d_inode(file[SRC].dentry);
521ced18
JR
5442+ h_src_sb = h_src_inode->i_sb;
5443+ if (!au_test_nfs(h_src_sb))
e2f27e51 5444+ IMustLock(h_src_inode);
1c60b727 5445+ err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
1facf9fc 5446+
8cdd5066
JR
5447+ /* i wonder if we had O_NO_DELAY_FPUT flag */
5448+ if (tsk->flags & PF_KTHREAD)
5449+ __fput_sync(file[DST].file);
5450+ else {
062440b3 5451+ /* it happened actually */
8cdd5066
JR
5452+ fput(file[DST].file);
5453+ /*
5454+ * too bad.
5455+ * we have to call both since we don't know which place the file
5456+ * was added to.
5457+ */
5458+ task_work_run();
5459+ flush_delayed_fput();
5460+ }
acd2b654
AM
5461+ br = au_sbr(sb, file[DST].bindex);
5462+ au_lcnt_dec(&br->br_nfiles);
523b37e3 5463+
4f0767ce 5464+out_src:
1facf9fc 5465+ fput(file[SRC].file);
acd2b654
AM
5466+ br = au_sbr(sb, file[SRC].bindex);
5467+ au_lcnt_dec(&br->br_nfiles);
4f0767ce 5468+out:
1facf9fc 5469+ return err;
5470+}
5471+
c2b27bf2 5472+static int au_do_cpup_regular(struct au_cp_generic *cpg,
86dc4139 5473+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5474+{
5475+ int err, rerr;
5476+ loff_t l;
86dc4139 5477+ struct path h_path;
38d290e6 5478+ struct inode *h_src_inode, *h_dst_inode;
1facf9fc 5479+
5480+ err = 0;
5527c038 5481+ h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
86dc4139 5482+ l = i_size_read(h_src_inode);
c2b27bf2
AM
5483+ if (cpg->len == -1 || l < cpg->len)
5484+ cpg->len = l;
5485+ if (cpg->len) {
86dc4139 5486+ /* try stopping to update while we are referencing */
be118d29 5487+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
c2b27bf2 5488+ au_pin_hdir_unlock(cpg->pin);
1facf9fc 5489+
c2b27bf2
AM
5490+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5491+ h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
86dc4139 5492+ h_src_attr->iflags = h_src_inode->i_flags;
5527c038 5493+ if (!au_test_nfs(h_src_inode->i_sb))
521ced18 5494+ err = vfsub_getattr(&h_path, &h_src_attr->st);
5527c038 5495+ else {
3c1bdaff 5496+ inode_unlock_shared(h_src_inode);
521ced18 5497+ err = vfsub_getattr(&h_path, &h_src_attr->st);
be118d29 5498+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5527c038 5499+ }
86dc4139 5500+ if (unlikely(err)) {
3c1bdaff 5501+ inode_unlock_shared(h_src_inode);
86dc4139
AM
5502+ goto out;
5503+ }
5504+ h_src_attr->valid = 1;
e2f27e51
AM
5505+ if (!au_test_nfs(h_src_inode->i_sb)) {
5506+ err = au_cp_regular(cpg);
3c1bdaff 5507+ inode_unlock_shared(h_src_inode);
e2f27e51 5508+ } else {
3c1bdaff 5509+ inode_unlock_shared(h_src_inode);
e2f27e51
AM
5510+ err = au_cp_regular(cpg);
5511+ }
c2b27bf2 5512+ rerr = au_pin_hdir_relock(cpg->pin);
86dc4139
AM
5513+ if (!err && rerr)
5514+ err = rerr;
1facf9fc 5515+ }
38d290e6
JR
5516+ if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5517+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5527c038 5518+ h_dst_inode = d_inode(h_path.dentry);
38d290e6
JR
5519+ spin_lock(&h_dst_inode->i_lock);
5520+ h_dst_inode->i_state |= I_LINKABLE;
5521+ spin_unlock(&h_dst_inode->i_lock);
5522+ }
1facf9fc 5523+
4f0767ce 5524+out:
1facf9fc 5525+ return err;
5526+}
5527+
5528+static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5529+ struct inode *h_dir)
5530+{
5531+ int err, symlen;
5532+ mm_segment_t old_fs;
b752ccd1
AM
5533+ union {
5534+ char *k;
5535+ char __user *u;
5536+ } sym;
1facf9fc 5537+
5538+ err = -ENOMEM;
537831f9 5539+ sym.k = (void *)__get_free_page(GFP_NOFS);
b752ccd1 5540+ if (unlikely(!sym.k))
1facf9fc 5541+ goto out;
5542+
9dbd164d 5543+ /* unnecessary to support mmap_sem since symlink is not mmap-able */
1facf9fc 5544+ old_fs = get_fs();
5545+ set_fs(KERNEL_DS);
a2654f78 5546+ symlen = vfs_readlink(h_src, sym.u, PATH_MAX);
1facf9fc 5547+ err = symlen;
5548+ set_fs(old_fs);
5549+
5550+ if (symlen > 0) {
b752ccd1
AM
5551+ sym.k[symlen] = 0;
5552+ err = vfsub_symlink(h_dir, h_path, sym.k);
1facf9fc 5553+ }
1c60b727 5554+ free_page((unsigned long)sym.k);
1facf9fc 5555+
4f0767ce 5556+out:
1facf9fc 5557+ return err;
5558+}
5559+
8cdd5066
JR
5560+/*
5561+ * regardless 'acl' option, reset all ACL.
5562+ * All ACL will be copied up later from the original entry on the lower branch.
5563+ */
5564+static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5565+{
5566+ int err;
5567+ struct dentry *h_dentry;
5568+ struct inode *h_inode;
5569+
5570+ h_dentry = h_path->dentry;
5571+ h_inode = d_inode(h_dentry);
5572+ /* forget_all_cached_acls(h_inode)); */
5573+ err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5574+ AuTraceErr(err);
5575+ if (err == -EOPNOTSUPP)
5576+ err = 0;
5577+ if (!err)
5578+ err = vfsub_acl_chmod(h_inode, mode);
5579+
5580+ AuTraceErr(err);
5581+ return err;
5582+}
5583+
5584+static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5585+ struct inode *h_dir, struct path *h_path)
5586+{
5587+ int err;
5588+ struct inode *dir, *inode;
5589+
5590+ err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
5591+ AuTraceErr(err);
5592+ if (err == -EOPNOTSUPP)
5593+ err = 0;
5594+ if (unlikely(err))
5595+ goto out;
5596+
5597+ /*
5598+ * strange behaviour from the users view,
acd2b654 5599+ * particularly setattr case
8cdd5066
JR
5600+ */
5601+ dir = d_inode(dst_parent);
5afbbe0d 5602+ if (au_ibtop(dir) == cpg->bdst)
8cdd5066
JR
5603+ au_cpup_attr_nlink(dir, /*force*/1);
5604+ inode = d_inode(cpg->dentry);
5605+ au_cpup_attr_nlink(inode, /*force*/1);
5606+
5607+out:
5608+ return err;
5609+}
5610+
1facf9fc 5611+static noinline_for_stack
c2b27bf2 5612+int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
86dc4139 5613+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5614+{
5615+ int err;
5616+ umode_t mode;
5617+ unsigned int mnt_flags;
076b876e 5618+ unsigned char isdir, isreg, force;
c2b27bf2 5619+ const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5620+ struct au_dtime dt;
5621+ struct path h_path;
5622+ struct dentry *h_src, *h_dst, *h_parent;
8cdd5066 5623+ struct inode *h_inode, *h_dir;
1facf9fc 5624+ struct super_block *sb;
5625+
5626+ /* bsrc branch can be ro/rw. */
c2b27bf2 5627+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038
JR
5628+ h_inode = d_inode(h_src);
5629+ AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
1facf9fc 5630+
5631+ /* try stopping to be referenced while we are creating */
c2b27bf2
AM
5632+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5633+ if (au_ftest_cpup(cpg->flags, RENAME))
86dc4139
AM
5634+ AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5635+ AUFS_WH_PFX_LEN));
1facf9fc 5636+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5637+ h_dir = d_inode(h_parent);
1facf9fc 5638+ IMustLock(h_dir);
5639+ AuDebugOn(h_parent != h_dst->d_parent);
5640+
c2b27bf2
AM
5641+ sb = cpg->dentry->d_sb;
5642+ h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
1facf9fc 5643+ if (do_dt) {
5644+ h_path.dentry = h_parent;
5645+ au_dtime_store(&dt, dst_parent, &h_path);
5646+ }
5647+ h_path.dentry = h_dst;
5648+
076b876e 5649+ isreg = 0;
1facf9fc 5650+ isdir = 0;
5651+ mode = h_inode->i_mode;
5652+ switch (mode & S_IFMT) {
5653+ case S_IFREG:
076b876e 5654+ isreg = 1;
cd7a4cd9 5655+ err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
1facf9fc 5656+ if (!err)
c2b27bf2 5657+ err = au_do_cpup_regular(cpg, h_src_attr);
1facf9fc 5658+ break;
5659+ case S_IFDIR:
5660+ isdir = 1;
5661+ err = vfsub_mkdir(h_dir, &h_path, mode);
8cdd5066
JR
5662+ if (!err)
5663+ err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
1facf9fc 5664+ break;
5665+ case S_IFLNK:
5666+ err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5667+ break;
5668+ case S_IFCHR:
5669+ case S_IFBLK:
5670+ AuDebugOn(!capable(CAP_MKNOD));
5671+ /*FALLTHROUGH*/
5672+ case S_IFIFO:
5673+ case S_IFSOCK:
5674+ err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5675+ break;
5676+ default:
5677+ AuIOErr("Unknown inode type 0%o\n", mode);
5678+ err = -EIO;
5679+ }
8cdd5066
JR
5680+ if (!err)
5681+ err = au_reset_acl(h_dir, &h_path, mode);
1facf9fc 5682+
5683+ mnt_flags = au_mntflags(sb);
5684+ if (!au_opt_test(mnt_flags, UDBA_NONE)
5685+ && !isdir
5686+ && au_opt_test(mnt_flags, XINO)
38d290e6
JR
5687+ && (h_inode->i_nlink == 1
5688+ || (h_inode->i_state & I_LINKABLE))
1facf9fc 5689+ /* todo: unnecessary? */
5527c038 5690+ /* && d_inode(cpg->dentry)->i_nlink == 1 */
c2b27bf2
AM
5691+ && cpg->bdst < cpg->bsrc
5692+ && !au_ftest_cpup(cpg->flags, KEEPLINO))
5693+ au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
1facf9fc 5694+ /* ignore this error */
5695+
076b876e
AM
5696+ if (!err) {
5697+ force = 0;
5698+ if (isreg) {
5699+ force = !!cpg->len;
5700+ if (cpg->len == -1)
5701+ force = !!i_size_read(h_inode);
5702+ }
5703+ au_fhsm_wrote(sb, cpg->bdst, force);
5704+ }
5705+
1facf9fc 5706+ if (do_dt)
5707+ au_dtime_revert(&dt);
5708+ return err;
5709+}
5710+
392086de 5711+static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
86dc4139
AM
5712+{
5713+ int err;
392086de 5714+ struct dentry *dentry, *h_dentry, *h_parent, *parent;
86dc4139 5715+ struct inode *h_dir;
392086de 5716+ aufs_bindex_t bdst;
86dc4139 5717+
392086de
AM
5718+ dentry = cpg->dentry;
5719+ bdst = cpg->bdst;
5720+ h_dentry = au_h_dptr(dentry, bdst);
5721+ if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5722+ dget(h_dentry);
5723+ au_set_h_dptr(dentry, bdst, NULL);
5724+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
5725+ if (!err)
5726+ h_path->dentry = dget(au_h_dptr(dentry, bdst));
86dc4139 5727+ au_set_h_dptr(dentry, bdst, h_dentry);
392086de
AM
5728+ } else {
5729+ err = 0;
5730+ parent = dget_parent(dentry);
5731+ h_parent = au_h_dptr(parent, bdst);
5732+ dput(parent);
5733+ h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5734+ if (IS_ERR(h_path->dentry))
5735+ err = PTR_ERR(h_path->dentry);
86dc4139 5736+ }
392086de
AM
5737+ if (unlikely(err))
5738+ goto out;
86dc4139 5739+
86dc4139 5740+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 5741+ h_dir = d_inode(h_parent);
86dc4139 5742+ IMustLock(h_dir);
523b37e3
AM
5743+ AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5744+ /* no delegation since it is just created */
f2c43d5f
AM
5745+ err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5746+ /*flags*/0);
86dc4139
AM
5747+ dput(h_path->dentry);
5748+
5749+out:
5750+ return err;
5751+}
5752+
1facf9fc 5753+/*
5754+ * copyup the @dentry from @bsrc to @bdst.
5755+ * the caller must set the both of lower dentries.
5756+ * @len is for truncating when it is -1 copyup the entire file.
5757+ * in link/rename cases, @dst_parent may be different from the real one.
c2b27bf2 5758+ * basic->bsrc can be larger than basic->bdst.
f2c43d5f 5759+ * aufs doesn't touch the credential so
acd2b654 5760+ * security_inode_copy_up{,_xattr}() are unnecessary.
1facf9fc 5761+ */
c2b27bf2 5762+static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5763+{
5764+ int err, rerr;
5afbbe0d 5765+ aufs_bindex_t old_ibtop;
1facf9fc 5766+ unsigned char isdir, plink;
1facf9fc 5767+ struct dentry *h_src, *h_dst, *h_parent;
5527c038 5768+ struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
1facf9fc 5769+ struct super_block *sb;
86dc4139 5770+ struct au_branch *br;
acd2b654 5771+ /* to reduce stack size */
c2b27bf2
AM
5772+ struct {
5773+ struct au_dtime dt;
5774+ struct path h_path;
5775+ struct au_cpup_reg_attr h_src_attr;
5776+ } *a;
1facf9fc 5777+
c2b27bf2
AM
5778+ err = -ENOMEM;
5779+ a = kmalloc(sizeof(*a), GFP_NOFS);
5780+ if (unlikely(!a))
5781+ goto out;
5782+ a->h_src_attr.valid = 0;
1facf9fc 5783+
c2b27bf2
AM
5784+ sb = cpg->dentry->d_sb;
5785+ br = au_sbr(sb, cpg->bdst);
5786+ a->h_path.mnt = au_br_mnt(br);
5787+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
1facf9fc 5788+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5789+ h_dir = d_inode(h_parent);
1facf9fc 5790+ IMustLock(h_dir);
5791+
c2b27bf2 5792+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5793+ inode = d_inode(cpg->dentry);
1facf9fc 5794+
5795+ if (!dst_parent)
c2b27bf2 5796+ dst_parent = dget_parent(cpg->dentry);
1facf9fc 5797+ else
5798+ dget(dst_parent);
5799+
5800+ plink = !!au_opt_test(au_mntflags(sb), PLINK);
c2b27bf2 5801+ dst_inode = au_h_iptr(inode, cpg->bdst);
1facf9fc 5802+ if (dst_inode) {
5803+ if (unlikely(!plink)) {
5804+ err = -EIO;
027c5e7a
AM
5805+ AuIOErr("hi%lu(i%lu) exists on b%d "
5806+ "but plink is disabled\n",
c2b27bf2
AM
5807+ dst_inode->i_ino, inode->i_ino, cpg->bdst);
5808+ goto out_parent;
1facf9fc 5809+ }
5810+
5811+ if (dst_inode->i_nlink) {
c2b27bf2 5812+ const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5813+
c2b27bf2 5814+ h_src = au_plink_lkup(inode, cpg->bdst);
1facf9fc 5815+ err = PTR_ERR(h_src);
5816+ if (IS_ERR(h_src))
c2b27bf2 5817+ goto out_parent;
5527c038 5818+ if (unlikely(d_is_negative(h_src))) {
1facf9fc 5819+ err = -EIO;
79b8bda9 5820+ AuIOErr("i%lu exists on b%d "
027c5e7a 5821+ "but not pseudo-linked\n",
79b8bda9 5822+ inode->i_ino, cpg->bdst);
1facf9fc 5823+ dput(h_src);
c2b27bf2 5824+ goto out_parent;
1facf9fc 5825+ }
5826+
5827+ if (do_dt) {
c2b27bf2
AM
5828+ a->h_path.dentry = h_parent;
5829+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
1facf9fc 5830+ }
86dc4139 5831+
c2b27bf2 5832+ a->h_path.dentry = h_dst;
523b37e3
AM
5833+ delegated = NULL;
5834+ err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
c2b27bf2 5835+ if (!err && au_ftest_cpup(cpg->flags, RENAME))
392086de 5836+ err = au_do_ren_after_cpup(cpg, &a->h_path);
1facf9fc 5837+ if (do_dt)
c2b27bf2 5838+ au_dtime_revert(&a->dt);
523b37e3
AM
5839+ if (unlikely(err == -EWOULDBLOCK)) {
5840+ pr_warn("cannot retry for NFSv4 delegation"
5841+ " for an internal link\n");
5842+ iput(delegated);
5843+ }
1facf9fc 5844+ dput(h_src);
c2b27bf2 5845+ goto out_parent;
1facf9fc 5846+ } else
5847+ /* todo: cpup_wh_file? */
5848+ /* udba work */
4a4d8108 5849+ au_update_ibrange(inode, /*do_put_zero*/1);
1facf9fc 5850+ }
5851+
86dc4139 5852+ isdir = S_ISDIR(inode->i_mode);
5afbbe0d 5853+ old_ibtop = au_ibtop(inode);
c2b27bf2 5854+ err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
1facf9fc 5855+ if (unlikely(err))
86dc4139 5856+ goto out_rev;
5527c038 5857+ dst_inode = d_inode(h_dst);
febd17d6 5858+ inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
86dc4139 5859+ /* todo: necessary? */
c2b27bf2 5860+ /* au_pin_hdir_unlock(cpg->pin); */
1facf9fc 5861+
c2b27bf2 5862+ err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
86dc4139
AM
5863+ if (unlikely(err)) {
5864+ /* todo: necessary? */
c2b27bf2 5865+ /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
febd17d6 5866+ inode_unlock(dst_inode);
86dc4139
AM
5867+ goto out_rev;
5868+ }
5869+
5afbbe0d 5870+ if (cpg->bdst < old_ibtop) {
86dc4139 5871+ if (S_ISREG(inode->i_mode)) {
c2b27bf2 5872+ err = au_dy_iaop(inode, cpg->bdst, dst_inode);
86dc4139 5873+ if (unlikely(err)) {
c2b27bf2
AM
5874+ /* ignore an error */
5875+ /* au_pin_hdir_relock(cpg->pin); */
febd17d6 5876+ inode_unlock(dst_inode);
86dc4139 5877+ goto out_rev;
4a4d8108 5878+ }
4a4d8108 5879+ }
5afbbe0d 5880+ au_set_ibtop(inode, cpg->bdst);
c2b27bf2 5881+ } else
5afbbe0d 5882+ au_set_ibbot(inode, cpg->bdst);
c2b27bf2 5883+ au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
86dc4139
AM
5884+ au_hi_flags(inode, isdir));
5885+
5886+ /* todo: necessary? */
c2b27bf2 5887+ /* err = au_pin_hdir_relock(cpg->pin); */
febd17d6 5888+ inode_unlock(dst_inode);
86dc4139
AM
5889+ if (unlikely(err))
5890+ goto out_rev;
5891+
5527c038 5892+ src_inode = d_inode(h_src);
86dc4139 5893+ if (!isdir
5527c038
JR
5894+ && (src_inode->i_nlink > 1
5895+ || src_inode->i_state & I_LINKABLE)
86dc4139 5896+ && plink)
c2b27bf2 5897+ au_plink_append(inode, cpg->bdst, h_dst);
86dc4139 5898+
c2b27bf2
AM
5899+ if (au_ftest_cpup(cpg->flags, RENAME)) {
5900+ a->h_path.dentry = h_dst;
392086de 5901+ err = au_do_ren_after_cpup(cpg, &a->h_path);
86dc4139
AM
5902+ }
5903+ if (!err)
c2b27bf2 5904+ goto out_parent; /* success */
1facf9fc 5905+
5906+ /* revert */
4a4d8108 5907+out_rev:
c2b27bf2
AM
5908+ a->h_path.dentry = h_parent;
5909+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
5910+ a->h_path.dentry = h_dst;
86dc4139 5911+ rerr = 0;
5527c038 5912+ if (d_is_positive(h_dst)) {
523b37e3
AM
5913+ if (!isdir) {
5914+ /* no delegation since it is just created */
5915+ rerr = vfsub_unlink(h_dir, &a->h_path,
5916+ /*delegated*/NULL, /*force*/0);
5917+ } else
c2b27bf2 5918+ rerr = vfsub_rmdir(h_dir, &a->h_path);
86dc4139 5919+ }
c2b27bf2 5920+ au_dtime_revert(&a->dt);
1facf9fc 5921+ if (rerr) {
5922+ AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5923+ err = -EIO;
5924+ }
c2b27bf2 5925+out_parent:
1facf9fc 5926+ dput(dst_parent);
9f237c51 5927+ au_kfree_rcu(a);
c2b27bf2 5928+out:
1facf9fc 5929+ return err;
5930+}
5931+
7e9cd9fe 5932+#if 0 /* reserved */
1facf9fc 5933+struct au_cpup_single_args {
5934+ int *errp;
c2b27bf2 5935+ struct au_cp_generic *cpg;
1facf9fc 5936+ struct dentry *dst_parent;
5937+};
5938+
5939+static void au_call_cpup_single(void *args)
5940+{
5941+ struct au_cpup_single_args *a = args;
86dc4139 5942+
c2b27bf2
AM
5943+ au_pin_hdir_acquire_nest(a->cpg->pin);
5944+ *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5945+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 5946+}
c2b27bf2 5947+#endif
1facf9fc 5948+
53392da6
AM
5949+/*
5950+ * prevent SIGXFSZ in copy-up.
5951+ * testing CAP_MKNOD is for generic fs,
5952+ * but CAP_FSETID is for xfs only, currently.
5953+ */
86dc4139 5954+static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
53392da6
AM
5955+{
5956+ int do_sio;
86dc4139
AM
5957+ struct super_block *sb;
5958+ struct inode *h_dir;
53392da6
AM
5959+
5960+ do_sio = 0;
86dc4139 5961+ sb = au_pinned_parent(pin)->d_sb;
53392da6
AM
5962+ if (!au_wkq_test()
5963+ && (!au_sbi(sb)->si_plink_maint_pid
5964+ || au_plink_maint(sb, AuLock_NOPLM))) {
5965+ switch (mode & S_IFMT) {
5966+ case S_IFREG:
5967+ /* no condition about RLIMIT_FSIZE and the file size */
5968+ do_sio = 1;
5969+ break;
5970+ case S_IFCHR:
5971+ case S_IFBLK:
5972+ do_sio = !capable(CAP_MKNOD);
5973+ break;
5974+ }
5975+ if (!do_sio)
5976+ do_sio = ((mode & (S_ISUID | S_ISGID))
5977+ && !capable(CAP_FSETID));
86dc4139
AM
5978+ /* this workaround may be removed in the future */
5979+ if (!do_sio) {
5980+ h_dir = au_pinned_h_dir(pin);
5981+ do_sio = h_dir->i_mode & S_ISVTX;
5982+ }
53392da6
AM
5983+ }
5984+
5985+ return do_sio;
5986+}
5987+
7e9cd9fe 5988+#if 0 /* reserved */
c2b27bf2 5989+int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5990+{
5991+ int err, wkq_err;
1facf9fc 5992+ struct dentry *h_dentry;
5993+
c2b27bf2 5994+ h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5995+ if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
c2b27bf2 5996+ err = au_cpup_single(cpg, dst_parent);
1facf9fc 5997+ else {
5998+ struct au_cpup_single_args args = {
5999+ .errp = &err,
c2b27bf2
AM
6000+ .cpg = cpg,
6001+ .dst_parent = dst_parent
1facf9fc 6002+ };
6003+ wkq_err = au_wkq_wait(au_call_cpup_single, &args);
6004+ if (unlikely(wkq_err))
6005+ err = wkq_err;
6006+ }
6007+
6008+ return err;
6009+}
c2b27bf2 6010+#endif
1facf9fc 6011+
6012+/*
6013+ * copyup the @dentry from the first active lower branch to @bdst,
6014+ * using au_cpup_single().
6015+ */
c2b27bf2 6016+static int au_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 6017+{
6018+ int err;
c2b27bf2
AM
6019+ unsigned int flags_orig;
6020+ struct dentry *dentry;
6021+
6022+ AuDebugOn(cpg->bsrc < 0);
1facf9fc 6023+
c2b27bf2 6024+ dentry = cpg->dentry;
86dc4139 6025+ DiMustWriteLock(dentry);
1facf9fc 6026+
c2b27bf2 6027+ err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
1facf9fc 6028+ if (!err) {
c2b27bf2
AM
6029+ flags_orig = cpg->flags;
6030+ au_fset_cpup(cpg->flags, RENAME);
6031+ err = au_cpup_single(cpg, NULL);
6032+ cpg->flags = flags_orig;
1facf9fc 6033+ if (!err)
6034+ return 0; /* success */
6035+
6036+ /* revert */
c2b27bf2 6037+ au_set_h_dptr(dentry, cpg->bdst, NULL);
5afbbe0d 6038+ au_set_dbtop(dentry, cpg->bsrc);
1facf9fc 6039+ }
6040+
6041+ return err;
6042+}
6043+
6044+struct au_cpup_simple_args {
6045+ int *errp;
c2b27bf2 6046+ struct au_cp_generic *cpg;
1facf9fc 6047+};
6048+
6049+static void au_call_cpup_simple(void *args)
6050+{
6051+ struct au_cpup_simple_args *a = args;
86dc4139 6052+
c2b27bf2
AM
6053+ au_pin_hdir_acquire_nest(a->cpg->pin);
6054+ *a->errp = au_cpup_simple(a->cpg);
6055+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 6056+}
6057+
c2b27bf2 6058+static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 6059+{
6060+ int err, wkq_err;
c2b27bf2
AM
6061+ struct dentry *dentry, *parent;
6062+ struct file *h_file;
1facf9fc 6063+ struct inode *h_dir;
6064+
c2b27bf2
AM
6065+ dentry = cpg->dentry;
6066+ h_file = NULL;
6067+ if (au_ftest_cpup(cpg->flags, HOPEN)) {
6068+ AuDebugOn(cpg->bsrc < 0);
392086de 6069+ h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
c2b27bf2
AM
6070+ err = PTR_ERR(h_file);
6071+ if (IS_ERR(h_file))
6072+ goto out;
6073+ }
6074+
1facf9fc 6075+ parent = dget_parent(dentry);
5527c038 6076+ h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
53392da6 6077+ if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
5527c038 6078+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 6079+ err = au_cpup_simple(cpg);
1facf9fc 6080+ else {
6081+ struct au_cpup_simple_args args = {
6082+ .errp = &err,
c2b27bf2 6083+ .cpg = cpg
1facf9fc 6084+ };
6085+ wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
6086+ if (unlikely(wkq_err))
6087+ err = wkq_err;
6088+ }
6089+
6090+ dput(parent);
c2b27bf2
AM
6091+ if (h_file)
6092+ au_h_open_post(dentry, cpg->bsrc, h_file);
6093+
6094+out:
1facf9fc 6095+ return err;
6096+}
6097+
c2b27bf2 6098+int au_sio_cpup_simple(struct au_cp_generic *cpg)
367653fa 6099+{
5afbbe0d 6100+ aufs_bindex_t bsrc, bbot;
c2b27bf2 6101+ struct dentry *dentry, *h_dentry;
367653fa 6102+
c2b27bf2
AM
6103+ if (cpg->bsrc < 0) {
6104+ dentry = cpg->dentry;
5afbbe0d
AM
6105+ bbot = au_dbbot(dentry);
6106+ for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
c2b27bf2
AM
6107+ h_dentry = au_h_dptr(dentry, bsrc);
6108+ if (h_dentry) {
5527c038 6109+ AuDebugOn(d_is_negative(h_dentry));
c2b27bf2
AM
6110+ break;
6111+ }
6112+ }
5afbbe0d 6113+ AuDebugOn(bsrc > bbot);
c2b27bf2 6114+ cpg->bsrc = bsrc;
367653fa 6115+ }
c2b27bf2
AM
6116+ AuDebugOn(cpg->bsrc <= cpg->bdst);
6117+ return au_do_sio_cpup_simple(cpg);
6118+}
367653fa 6119+
c2b27bf2
AM
6120+int au_sio_cpdown_simple(struct au_cp_generic *cpg)
6121+{
6122+ AuDebugOn(cpg->bdst <= cpg->bsrc);
6123+ return au_do_sio_cpup_simple(cpg);
367653fa
AM
6124+}
6125+
1facf9fc 6126+/* ---------------------------------------------------------------------- */
6127+
6128+/*
6129+ * copyup the deleted file for writing.
6130+ */
c2b27bf2
AM
6131+static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
6132+ struct file *file)
1facf9fc 6133+{
6134+ int err;
c2b27bf2
AM
6135+ unsigned int flags_orig;
6136+ aufs_bindex_t bsrc_orig;
c2b27bf2 6137+ struct au_dinfo *dinfo;
5afbbe0d
AM
6138+ struct {
6139+ struct au_hdentry *hd;
6140+ struct dentry *h_dentry;
6141+ } hdst, hsrc;
1facf9fc 6142+
c2b27bf2 6143+ dinfo = au_di(cpg->dentry);
1308ab2a 6144+ AuRwMustWriteLock(&dinfo->di_rwsem);
6145+
c2b27bf2 6146+ bsrc_orig = cpg->bsrc;
5afbbe0d
AM
6147+ cpg->bsrc = dinfo->di_btop;
6148+ hdst.hd = au_hdentry(dinfo, cpg->bdst);
6149+ hdst.h_dentry = hdst.hd->hd_dentry;
6150+ hdst.hd->hd_dentry = wh_dentry;
6151+ dinfo->di_btop = cpg->bdst;
6152+
6153+ hsrc.h_dentry = NULL;
027c5e7a 6154+ if (file) {
5afbbe0d
AM
6155+ hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
6156+ hsrc.h_dentry = hsrc.hd->hd_dentry;
6157+ hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
027c5e7a 6158+ }
c2b27bf2
AM
6159+ flags_orig = cpg->flags;
6160+ cpg->flags = !AuCpup_DTIME;
6161+ err = au_cpup_single(cpg, /*h_parent*/NULL);
6162+ cpg->flags = flags_orig;
027c5e7a
AM
6163+ if (file) {
6164+ if (!err)
6165+ err = au_reopen_nondir(file);
5afbbe0d 6166+ hsrc.hd->hd_dentry = hsrc.h_dentry;
1facf9fc 6167+ }
5afbbe0d
AM
6168+ hdst.hd->hd_dentry = hdst.h_dentry;
6169+ dinfo->di_btop = cpg->bsrc;
c2b27bf2 6170+ cpg->bsrc = bsrc_orig;
1facf9fc 6171+
6172+ return err;
6173+}
6174+
c2b27bf2 6175+static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 6176+{
6177+ int err;
c2b27bf2 6178+ aufs_bindex_t bdst;
1facf9fc 6179+ struct au_dtime dt;
c2b27bf2 6180+ struct dentry *dentry, *parent, *h_parent, *wh_dentry;
1facf9fc 6181+ struct au_branch *br;
6182+ struct path h_path;
6183+
c2b27bf2
AM
6184+ dentry = cpg->dentry;
6185+ bdst = cpg->bdst;
1facf9fc 6186+ br = au_sbr(dentry->d_sb, bdst);
6187+ parent = dget_parent(dentry);
6188+ h_parent = au_h_dptr(parent, bdst);
6189+ wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
6190+ err = PTR_ERR(wh_dentry);
6191+ if (IS_ERR(wh_dentry))
6192+ goto out;
6193+
6194+ h_path.dentry = h_parent;
86dc4139 6195+ h_path.mnt = au_br_mnt(br);
1facf9fc 6196+ au_dtime_store(&dt, parent, &h_path);
c2b27bf2 6197+ err = au_do_cpup_wh(cpg, wh_dentry, file);
1facf9fc 6198+ if (unlikely(err))
6199+ goto out_wh;
6200+
6201+ dget(wh_dentry);
6202+ h_path.dentry = wh_dentry;
2000de60 6203+ if (!d_is_dir(wh_dentry)) {
523b37e3 6204+ /* no delegation since it is just created */
5527c038 6205+ err = vfsub_unlink(d_inode(h_parent), &h_path,
523b37e3
AM
6206+ /*delegated*/NULL, /*force*/0);
6207+ } else
5527c038 6208+ err = vfsub_rmdir(d_inode(h_parent), &h_path);
1facf9fc 6209+ if (unlikely(err)) {
523b37e3
AM
6210+ AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6211+ wh_dentry, err);
1facf9fc 6212+ err = -EIO;
6213+ }
6214+ au_dtime_revert(&dt);
5527c038 6215+ au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
1facf9fc 6216+
4f0767ce 6217+out_wh:
1facf9fc 6218+ dput(wh_dentry);
4f0767ce 6219+out:
1facf9fc 6220+ dput(parent);
6221+ return err;
6222+}
6223+
6224+struct au_cpup_wh_args {
6225+ int *errp;
c2b27bf2 6226+ struct au_cp_generic *cpg;
1facf9fc 6227+ struct file *file;
6228+};
6229+
6230+static void au_call_cpup_wh(void *args)
6231+{
6232+ struct au_cpup_wh_args *a = args;
86dc4139 6233+
c2b27bf2
AM
6234+ au_pin_hdir_acquire_nest(a->cpg->pin);
6235+ *a->errp = au_cpup_wh(a->cpg, a->file);
6236+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 6237+}
6238+
c2b27bf2 6239+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 6240+{
6241+ int err, wkq_err;
c2b27bf2 6242+ aufs_bindex_t bdst;
c1595e42 6243+ struct dentry *dentry, *parent, *h_orph, *h_parent;
86dc4139 6244+ struct inode *dir, *h_dir, *h_tmpdir;
1facf9fc 6245+ struct au_wbr *wbr;
c2b27bf2 6246+ struct au_pin wh_pin, *pin_orig;
1facf9fc 6247+
c2b27bf2
AM
6248+ dentry = cpg->dentry;
6249+ bdst = cpg->bdst;
1facf9fc 6250+ parent = dget_parent(dentry);
5527c038 6251+ dir = d_inode(parent);
1facf9fc 6252+ h_orph = NULL;
6253+ h_parent = NULL;
6254+ h_dir = au_igrab(au_h_iptr(dir, bdst));
6255+ h_tmpdir = h_dir;
c2b27bf2 6256+ pin_orig = NULL;
1facf9fc 6257+ if (!h_dir->i_nlink) {
6258+ wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6259+ h_orph = wbr->wbr_orph;
6260+
6261+ h_parent = dget(au_h_dptr(parent, bdst));
1facf9fc 6262+ au_set_h_dptr(parent, bdst, dget(h_orph));
5527c038 6263+ h_tmpdir = d_inode(h_orph);
1facf9fc 6264+ au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6265+
febd17d6 6266+ inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
4a4d8108 6267+ /* todo: au_h_open_pre()? */
86dc4139 6268+
c2b27bf2 6269+ pin_orig = cpg->pin;
86dc4139 6270+ au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
c2b27bf2
AM
6271+ AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6272+ cpg->pin = &wh_pin;
1facf9fc 6273+ }
6274+
53392da6 6275+ if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
5527c038 6276+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 6277+ err = au_cpup_wh(cpg, file);
1facf9fc 6278+ else {
6279+ struct au_cpup_wh_args args = {
6280+ .errp = &err,
c2b27bf2
AM
6281+ .cpg = cpg,
6282+ .file = file
1facf9fc 6283+ };
6284+ wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6285+ if (unlikely(wkq_err))
6286+ err = wkq_err;
6287+ }
6288+
6289+ if (h_orph) {
febd17d6 6290+ inode_unlock(h_tmpdir);
4a4d8108 6291+ /* todo: au_h_open_post()? */
1facf9fc 6292+ au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
1facf9fc 6293+ au_set_h_dptr(parent, bdst, h_parent);
c2b27bf2
AM
6294+ AuDebugOn(!pin_orig);
6295+ cpg->pin = pin_orig;
1facf9fc 6296+ }
6297+ iput(h_dir);
6298+ dput(parent);
6299+
6300+ return err;
6301+}
6302+
6303+/* ---------------------------------------------------------------------- */
6304+
6305+/*
6306+ * generic routine for both of copy-up and copy-down.
6307+ */
6308+/* cf. revalidate function in file.c */
6309+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6310+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6311+ struct au_pin *pin,
1facf9fc 6312+ struct dentry *h_parent, void *arg),
6313+ void *arg)
6314+{
6315+ int err;
6316+ struct au_pin pin;
5527c038 6317+ struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
1facf9fc 6318+
6319+ err = 0;
6320+ parent = dget_parent(dentry);
6321+ if (IS_ROOT(parent))
6322+ goto out;
6323+
6324+ au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6325+ au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6326+
6327+ /* do not use au_dpage */
6328+ real_parent = parent;
6329+ while (1) {
6330+ dput(parent);
6331+ parent = dget_parent(dentry);
6332+ h_parent = au_h_dptr(parent, bdst);
6333+ if (h_parent)
6334+ goto out; /* success */
6335+
6336+ /* find top dir which is necessary to cpup */
6337+ do {
6338+ d = parent;
6339+ dput(parent);
6340+ parent = dget_parent(d);
6341+ di_read_lock_parent3(parent, !AuLock_IR);
6342+ h_parent = au_h_dptr(parent, bdst);
6343+ di_read_unlock(parent, !AuLock_IR);
6344+ } while (!h_parent);
6345+
6346+ if (d != real_parent)
6347+ di_write_lock_child3(d);
6348+
6349+ /* somebody else might create while we were sleeping */
5527c038
JR
6350+ h_dentry = au_h_dptr(d, bdst);
6351+ if (!h_dentry || d_is_negative(h_dentry)) {
6352+ if (h_dentry)
5afbbe0d 6353+ au_update_dbtop(d);
1facf9fc 6354+
6355+ au_pin_set_dentry(&pin, d);
6356+ err = au_do_pin(&pin);
6357+ if (!err) {
86dc4139 6358+ err = cp(d, bdst, &pin, h_parent, arg);
1facf9fc 6359+ au_unpin(&pin);
6360+ }
6361+ }
6362+
6363+ if (d != real_parent)
6364+ di_write_unlock(d);
6365+ if (unlikely(err))
6366+ break;
6367+ }
6368+
4f0767ce 6369+out:
1facf9fc 6370+ dput(parent);
6371+ return err;
6372+}
6373+
6374+static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6375+ struct au_pin *pin,
2000de60 6376+ struct dentry *h_parent __maybe_unused,
1facf9fc 6377+ void *arg __maybe_unused)
6378+{
c2b27bf2
AM
6379+ struct au_cp_generic cpg = {
6380+ .dentry = dentry,
6381+ .bdst = bdst,
6382+ .bsrc = -1,
6383+ .len = 0,
6384+ .pin = pin,
6385+ .flags = AuCpup_DTIME
6386+ };
6387+ return au_sio_cpup_simple(&cpg);
1facf9fc 6388+}
6389+
6390+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6391+{
6392+ return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6393+}
6394+
6395+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6396+{
6397+ int err;
6398+ struct dentry *parent;
6399+ struct inode *dir;
6400+
6401+ parent = dget_parent(dentry);
5527c038 6402+ dir = d_inode(parent);
1facf9fc 6403+ err = 0;
6404+ if (au_h_iptr(dir, bdst))
6405+ goto out;
6406+
6407+ di_read_unlock(parent, AuLock_IR);
6408+ di_write_lock_parent(parent);
6409+ /* someone else might change our inode while we were sleeping */
6410+ if (!au_h_iptr(dir, bdst))
6411+ err = au_cpup_dirs(dentry, bdst);
6412+ di_downgrade_lock(parent, AuLock_IR);
6413+
4f0767ce 6414+out:
1facf9fc 6415+ dput(parent);
6416+ return err;
6417+}
7f207e10
AM
6418diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6419--- /usr/share/empty/fs/aufs/cpup.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 6420+++ linux/fs/aufs/cpup.h 2018-08-12 23:43:05.453457863 +0200
062440b3
AM
6421@@ -0,0 +1,100 @@
6422+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 6423+/*
b00004a5 6424+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 6425+ *
6426+ * This program, aufs is free software; you can redistribute it and/or modify
6427+ * it under the terms of the GNU General Public License as published by
6428+ * the Free Software Foundation; either version 2 of the License, or
6429+ * (at your option) any later version.
dece6358
AM
6430+ *
6431+ * This program is distributed in the hope that it will be useful,
6432+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6433+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6434+ * GNU General Public License for more details.
6435+ *
6436+ * You should have received a copy of the GNU General Public License
523b37e3 6437+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6438+ */
6439+
6440+/*
6441+ * copy-up/down functions
6442+ */
6443+
6444+#ifndef __AUFS_CPUP_H__
6445+#define __AUFS_CPUP_H__
6446+
6447+#ifdef __KERNEL__
6448+
dece6358 6449+#include <linux/path.h>
1facf9fc 6450+
dece6358
AM
6451+struct inode;
6452+struct file;
86dc4139 6453+struct au_pin;
dece6358 6454+
86dc4139 6455+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
1facf9fc 6456+void au_cpup_attr_timesizes(struct inode *inode);
6457+void au_cpup_attr_nlink(struct inode *inode, int force);
6458+void au_cpup_attr_changeable(struct inode *inode);
6459+void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6460+void au_cpup_attr_all(struct inode *inode, int force);
6461+
6462+/* ---------------------------------------------------------------------- */
6463+
c2b27bf2
AM
6464+struct au_cp_generic {
6465+ struct dentry *dentry;
6466+ aufs_bindex_t bdst, bsrc;
6467+ loff_t len;
6468+ struct au_pin *pin;
6469+ unsigned int flags;
6470+};
6471+
1facf9fc 6472+/* cpup flags */
392086de
AM
6473+#define AuCpup_DTIME 1 /* do dtime_store/revert */
6474+#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
6475+ for link(2) */
6476+#define AuCpup_RENAME (1 << 2) /* rename after cpup */
6477+#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
6478+ cpup */
6479+#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
6480+ existing entry */
6481+#define AuCpup_RWDST (1 << 5) /* force write target even if
6482+ the branch is marked as RO */
c2b27bf2 6483+
8b6a4947
AM
6484+#ifndef CONFIG_AUFS_BR_HFSPLUS
6485+#undef AuCpup_HOPEN
6486+#define AuCpup_HOPEN 0
6487+#endif
6488+
1facf9fc 6489+#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
7f207e10
AM
6490+#define au_fset_cpup(flags, name) \
6491+ do { (flags) |= AuCpup_##name; } while (0)
6492+#define au_fclr_cpup(flags, name) \
6493+ do { (flags) &= ~AuCpup_##name; } while (0)
1facf9fc 6494+
6495+int au_copy_file(struct file *dst, struct file *src, loff_t len);
c2b27bf2
AM
6496+int au_sio_cpup_simple(struct au_cp_generic *cpg);
6497+int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6498+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
1facf9fc 6499+
6500+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6501+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6502+ struct au_pin *pin,
1facf9fc 6503+ struct dentry *h_parent, void *arg),
6504+ void *arg);
6505+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6506+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6507+
6508+/* ---------------------------------------------------------------------- */
6509+
6510+/* keep timestamps when copyup */
6511+struct au_dtime {
6512+ struct dentry *dt_dentry;
6513+ struct path dt_h_path;
cd7a4cd9 6514+ struct timespec64 dt_atime, dt_mtime;
1facf9fc 6515+};
6516+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6517+ struct path *h_path);
6518+void au_dtime_revert(struct au_dtime *dt);
6519+
6520+#endif /* __KERNEL__ */
6521+#endif /* __AUFS_CPUP_H__ */
7f207e10
AM
6522diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6523--- /usr/share/empty/fs/aufs/dbgaufs.c 1970-01-01 01:00:00.000000000 +0100
9f237c51
AM
6524+++ linux/fs/aufs/dbgaufs.c 2018-12-27 13:19:17.708416053 +0100
6525@@ -0,0 +1,526 @@
cd7a4cd9 6526+// SPDX-License-Identifier: GPL-2.0
1facf9fc 6527+/*
b00004a5 6528+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 6529+ *
6530+ * This program, aufs is free software; you can redistribute it and/or modify
6531+ * it under the terms of the GNU General Public License as published by
6532+ * the Free Software Foundation; either version 2 of the License, or
6533+ * (at your option) any later version.
dece6358
AM
6534+ *
6535+ * This program is distributed in the hope that it will be useful,
6536+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6537+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6538+ * GNU General Public License for more details.
6539+ *
6540+ * You should have received a copy of the GNU General Public License
523b37e3 6541+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6542+ */
6543+
6544+/*
6545+ * debugfs interface
6546+ */
6547+
6548+#include <linux/debugfs.h>
6549+#include "aufs.h"
6550+
6551+#ifndef CONFIG_SYSFS
6552+#error DEBUG_FS depends upon SYSFS
6553+#endif
6554+
6555+static struct dentry *dbgaufs;
cd7a4cd9 6556+static const mode_t dbgaufs_mode = 0444;
1facf9fc 6557+
6558+/* 20 is max digits length of ulong 64 */
6559+struct dbgaufs_arg {
6560+ int n;
6561+ char a[20 * 4];
6562+};
6563+
6564+/*
6565+ * common function for all XINO files
6566+ */
6567+static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6568+ struct file *file)
6569+{
9f237c51
AM
6570+ void *p;
6571+
6572+ p = file->private_data;
6573+ if (p) {
6574+ /* this is struct dbgaufs_arg */
6575+ AuDebugOn(!au_kfree_sz_test(p));
6576+ au_kfree_do_rcu(p);
6577+ }
1facf9fc 6578+ return 0;
6579+}
6580+
062440b3
AM
6581+static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6582+ int cnt)
1facf9fc 6583+{
6584+ int err;
6585+ struct kstat st;
6586+ struct dbgaufs_arg *p;
6587+
6588+ err = -ENOMEM;
6589+ p = kmalloc(sizeof(*p), GFP_NOFS);
6590+ if (unlikely(!p))
6591+ goto out;
6592+
6593+ err = 0;
6594+ p->n = 0;
6595+ file->private_data = p;
6596+ if (!xf)
6597+ goto out;
6598+
521ced18 6599+ err = vfsub_getattr(&xf->f_path, &st);
1facf9fc 6600+ if (!err) {
6601+ if (do_fcnt)
6602+ p->n = snprintf
062440b3
AM
6603+ (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6604+ cnt, st.blocks, st.blksize,
1facf9fc 6605+ (long long)st.size);
6606+ else
521ced18 6607+ p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
1facf9fc 6608+ st.blocks, st.blksize,
6609+ (long long)st.size);
6610+ AuDebugOn(p->n >= sizeof(p->a));
6611+ } else {
6612+ p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6613+ err = 0;
6614+ }
6615+
4f0767ce 6616+out:
1facf9fc 6617+ return err;
1facf9fc 6618+}
6619+
6620+static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6621+ size_t count, loff_t *ppos)
6622+{
6623+ struct dbgaufs_arg *p;
6624+
6625+ p = file->private_data;
6626+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6627+}
6628+
6629+/* ---------------------------------------------------------------------- */
6630+
86dc4139
AM
6631+struct dbgaufs_plink_arg {
6632+ int n;
6633+ char a[];
6634+};
6635+
6636+static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6637+ struct file *file)
6638+{
1c60b727 6639+ free_page((unsigned long)file->private_data);
86dc4139
AM
6640+ return 0;
6641+}
6642+
6643+static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6644+{
6645+ int err, i, limit;
6646+ unsigned long n, sum;
6647+ struct dbgaufs_plink_arg *p;
6648+ struct au_sbinfo *sbinfo;
6649+ struct super_block *sb;
8b6a4947 6650+ struct hlist_bl_head *hbl;
86dc4139
AM
6651+
6652+ err = -ENOMEM;
6653+ p = (void *)get_zeroed_page(GFP_NOFS);
6654+ if (unlikely(!p))
6655+ goto out;
6656+
6657+ err = -EFBIG;
6658+ sbinfo = inode->i_private;
6659+ sb = sbinfo->si_sb;
6660+ si_noflush_read_lock(sb);
6661+ if (au_opt_test(au_mntflags(sb), PLINK)) {
6662+ limit = PAGE_SIZE - sizeof(p->n);
6663+
6664+ /* the number of buckets */
6665+ n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6666+ p->n += n;
6667+ limit -= n;
6668+
6669+ sum = 0;
8b6a4947
AM
6670+ for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6671+ i++, hbl++) {
6672+ n = au_hbl_count(hbl);
86dc4139
AM
6673+ sum += n;
6674+
6675+ n = snprintf(p->a + p->n, limit, "%lu ", n);
6676+ p->n += n;
6677+ limit -= n;
6678+ if (unlikely(limit <= 0))
6679+ goto out_free;
6680+ }
6681+ p->a[p->n - 1] = '\n';
6682+
6683+ /* the sum of plinks */
6684+ n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6685+ p->n += n;
6686+ limit -= n;
6687+ if (unlikely(limit <= 0))
6688+ goto out_free;
6689+ } else {
6690+#define str "1\n0\n0\n"
6691+ p->n = sizeof(str) - 1;
6692+ strcpy(p->a, str);
6693+#undef str
6694+ }
6695+ si_read_unlock(sb);
6696+
6697+ err = 0;
6698+ file->private_data = p;
6699+ goto out; /* success */
6700+
6701+out_free:
1c60b727 6702+ free_page((unsigned long)p);
86dc4139
AM
6703+out:
6704+ return err;
6705+}
6706+
6707+static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6708+ size_t count, loff_t *ppos)
6709+{
6710+ struct dbgaufs_plink_arg *p;
6711+
6712+ p = file->private_data;
6713+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6714+}
6715+
6716+static const struct file_operations dbgaufs_plink_fop = {
6717+ .owner = THIS_MODULE,
6718+ .open = dbgaufs_plink_open,
6719+ .release = dbgaufs_plink_release,
6720+ .read = dbgaufs_plink_read
6721+};
6722+
6723+/* ---------------------------------------------------------------------- */
6724+
1facf9fc 6725+static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6726+{
6727+ int err;
6728+ struct au_sbinfo *sbinfo;
6729+ struct super_block *sb;
6730+
6731+ sbinfo = inode->i_private;
6732+ sb = sbinfo->si_sb;
6733+ si_noflush_read_lock(sb);
062440b3 6734+ err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
1facf9fc 6735+ si_read_unlock(sb);
6736+ return err;
6737+}
6738+
6739+static const struct file_operations dbgaufs_xib_fop = {
4a4d8108 6740+ .owner = THIS_MODULE,
1facf9fc 6741+ .open = dbgaufs_xib_open,
6742+ .release = dbgaufs_xi_release,
6743+ .read = dbgaufs_xi_read
6744+};
6745+
6746+/* ---------------------------------------------------------------------- */
6747+
6748+#define DbgaufsXi_PREFIX "xi"
6749+
6750+static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6751+{
acd2b654 6752+ int err, idx;
1facf9fc 6753+ long l;
acd2b654
AM
6754+ aufs_bindex_t bindex;
6755+ char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
1facf9fc 6756+ struct au_sbinfo *sbinfo;
6757+ struct super_block *sb;
acd2b654 6758+ struct au_xino *xi;
1facf9fc 6759+ struct file *xf;
6760+ struct qstr *name;
062440b3 6761+ struct au_branch *br;
1facf9fc 6762+
6763+ err = -ENOENT;
2000de60 6764+ name = &file->f_path.dentry->d_name;
1facf9fc 6765+ if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6766+ || memcmp(name->name, DbgaufsXi_PREFIX,
6767+ sizeof(DbgaufsXi_PREFIX) - 1)))
6768+ goto out;
acd2b654
AM
6769+
6770+ AuDebugOn(name->len >= sizeof(a));
6771+ memcpy(a, name->name, name->len);
6772+ a[name->len] = '\0';
6773+ p = strchr(a, '-');
6774+ if (p)
6775+ *p = '\0';
6776+ err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
1facf9fc 6777+ if (unlikely(err))
6778+ goto out;
acd2b654
AM
6779+ bindex = l;
6780+ idx = 0;
6781+ if (p) {
6782+ err = kstrtol(p + 1, 10, &l);
6783+ if (unlikely(err))
6784+ goto out;
6785+ idx = l;
6786+ }
1facf9fc 6787+
acd2b654 6788+ err = -ENOENT;
1facf9fc 6789+ sbinfo = inode->i_private;
6790+ sb = sbinfo->si_sb;
6791+ si_noflush_read_lock(sb);
acd2b654
AM
6792+ if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6793+ goto out_si;
6794+ br = au_sbr(sb, bindex);
6795+ xi = br->br_xino;
6796+ if (unlikely(idx >= xi->xi_nfile))
6797+ goto out_si;
6798+ xf = au_xino_file(xi, idx);
6799+ if (xf)
062440b3
AM
6800+ err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6801+ au_xino_count(br));
1facf9fc 6802+
acd2b654
AM
6803+out_si:
6804+ si_read_unlock(sb);
4f0767ce 6805+out:
acd2b654 6806+ AuTraceErr(err);
1facf9fc 6807+ return err;
6808+}
6809+
6810+static const struct file_operations dbgaufs_xino_fop = {
4a4d8108 6811+ .owner = THIS_MODULE,
1facf9fc 6812+ .open = dbgaufs_xino_open,
6813+ .release = dbgaufs_xi_release,
6814+ .read = dbgaufs_xi_read
6815+};
6816+
062440b3
AM
6817+void dbgaufs_xino_del(struct au_branch *br)
6818+{
6819+ struct dentry *dbgaufs;
6820+
6821+ dbgaufs = br->br_dbgaufs;
6822+ if (!dbgaufs)
6823+ return;
6824+
6825+ br->br_dbgaufs = NULL;
6826+ /* debugfs acquires the parent i_mutex */
6827+ lockdep_off();
6828+ debugfs_remove(dbgaufs);
6829+ lockdep_on();
6830+}
6831+
1facf9fc 6832+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6833+{
5afbbe0d 6834+ aufs_bindex_t bbot;
1facf9fc 6835+ struct au_branch *br;
1facf9fc 6836+
6837+ if (!au_sbi(sb)->si_dbgaufs)
6838+ return;
6839+
5afbbe0d
AM
6840+ bbot = au_sbbot(sb);
6841+ for (; bindex <= bbot; bindex++) {
1facf9fc 6842+ br = au_sbr(sb, bindex);
062440b3
AM
6843+ dbgaufs_xino_del(br);
6844+ }
6845+}
6846+
acd2b654
AM
6847+static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6848+ unsigned int idx, struct dentry *parent,
6849+ struct au_sbinfo *sbinfo)
062440b3
AM
6850+{
6851+ struct au_branch *br;
6852+ struct dentry *d;
acd2b654
AM
6853+ /* "xi" bindex(5) "-" idx(2) NULL */
6854+ char name[sizeof(DbgaufsXi_PREFIX) + 8];
062440b3 6855+
acd2b654
AM
6856+ if (!idx)
6857+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6858+ else
6859+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6860+ bindex, idx);
062440b3
AM
6861+ br = au_sbr(sb, bindex);
6862+ if (br->br_dbgaufs) {
6863+ struct qstr qstr = QSTR_INIT(name, strlen(name));
6864+
6865+ if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6866+ /* debugfs acquires the parent i_mutex */
6867+ lockdep_off();
6868+ d = debugfs_rename(parent, br->br_dbgaufs, parent,
6869+ name);
6870+ lockdep_on();
6871+ if (unlikely(!d))
6872+ pr_warn("failed renaming %pd/%s, ignored.\n",
6873+ parent, name);
6874+ }
6875+ } else {
e2f27e51 6876+ lockdep_off();
062440b3
AM
6877+ br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6878+ sbinfo, &dbgaufs_xino_fop);
e2f27e51 6879+ lockdep_on();
062440b3 6880+ if (unlikely(!br->br_dbgaufs))
acd2b654 6881+ pr_warn("failed creating %pd/%s, ignored.\n",
062440b3 6882+ parent, name);
1facf9fc 6883+ }
6884+}
6885+
acd2b654
AM
6886+static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6887+ struct dentry *parent, struct au_sbinfo *sbinfo)
6888+{
6889+ struct au_branch *br;
6890+ struct au_xino *xi;
6891+ unsigned int u;
6892+
6893+ br = au_sbr(sb, bindex);
6894+ xi = br->br_xino;
6895+ for (u = 0; u < xi->xi_nfile; u++)
6896+ dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6897+}
6898+
062440b3 6899+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
1facf9fc 6900+{
6901+ struct au_sbinfo *sbinfo;
6902+ struct dentry *parent;
5afbbe0d 6903+ aufs_bindex_t bbot;
062440b3
AM
6904+
6905+ if (!au_opt_test(au_mntflags(sb), XINO))
6906+ return;
1facf9fc 6907+
6908+ sbinfo = au_sbi(sb);
6909+ parent = sbinfo->si_dbgaufs;
6910+ if (!parent)
6911+ return;
6912+
5afbbe0d 6913+ bbot = au_sbbot(sb);
062440b3
AM
6914+ if (topdown)
6915+ for (; bindex <= bbot; bindex++)
6916+ dbgaufs_br_add(sb, bindex, parent, sbinfo);
6917+ else
6918+ for (; bbot >= bindex; bbot--)
6919+ dbgaufs_br_add(sb, bbot, parent, sbinfo);
1facf9fc 6920+}
6921+
6922+/* ---------------------------------------------------------------------- */
6923+
6924+#ifdef CONFIG_AUFS_EXPORT
6925+static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6926+{
6927+ int err;
6928+ struct au_sbinfo *sbinfo;
6929+ struct super_block *sb;
6930+
6931+ sbinfo = inode->i_private;
6932+ sb = sbinfo->si_sb;
6933+ si_noflush_read_lock(sb);
062440b3 6934+ err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
1facf9fc 6935+ si_read_unlock(sb);
6936+ return err;
6937+}
6938+
6939+static const struct file_operations dbgaufs_xigen_fop = {
4a4d8108 6940+ .owner = THIS_MODULE,
1facf9fc 6941+ .open = dbgaufs_xigen_open,
6942+ .release = dbgaufs_xi_release,
6943+ .read = dbgaufs_xi_read
6944+};
6945+
6946+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6947+{
6948+ int err;
6949+
dece6358 6950+ /*
c1595e42 6951+ * This function is a dynamic '__init' function actually,
dece6358
AM
6952+ * so the tiny check for si_rwsem is unnecessary.
6953+ */
6954+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6955+
1facf9fc 6956+ err = -EIO;
6957+ sbinfo->si_dbgaufs_xigen = debugfs_create_file
6958+ ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6959+ &dbgaufs_xigen_fop);
6960+ if (sbinfo->si_dbgaufs_xigen)
6961+ err = 0;
6962+
6963+ return err;
6964+}
6965+#else
6966+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6967+{
6968+ return 0;
6969+}
6970+#endif /* CONFIG_AUFS_EXPORT */
6971+
6972+/* ---------------------------------------------------------------------- */
6973+
6974+void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6975+{
dece6358 6976+ /*
7e9cd9fe 6977+ * This function is a dynamic '__fin' function actually,
dece6358
AM
6978+ * so the tiny check for si_rwsem is unnecessary.
6979+ */
6980+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6981+
1facf9fc 6982+ debugfs_remove_recursive(sbinfo->si_dbgaufs);
6983+ sbinfo->si_dbgaufs = NULL;
1facf9fc 6984+}
6985+
6986+int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6987+{
6988+ int err;
6989+ char name[SysaufsSiNameLen];
6990+
dece6358 6991+ /*
c1595e42 6992+ * This function is a dynamic '__init' function actually,
dece6358
AM
6993+ * so the tiny check for si_rwsem is unnecessary.
6994+ */
6995+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6996+
1facf9fc 6997+ err = -ENOENT;
6998+ if (!dbgaufs) {
6999+ AuErr1("/debug/aufs is uninitialized\n");
7000+ goto out;
7001+ }
7002+
7003+ err = -EIO;
7004+ sysaufs_name(sbinfo, name);
7005+ sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
7006+ if (unlikely(!sbinfo->si_dbgaufs))
7007+ goto out;
1facf9fc 7008+
062440b3 7009+ /* regardless plink/noplink option */
86dc4139
AM
7010+ sbinfo->si_dbgaufs_plink = debugfs_create_file
7011+ ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
7012+ &dbgaufs_plink_fop);
7013+ if (unlikely(!sbinfo->si_dbgaufs_plink))
7014+ goto out_dir;
7015+
062440b3
AM
7016+ /* regardless xino/noxino option */
7017+ sbinfo->si_dbgaufs_xib = debugfs_create_file
7018+ ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
7019+ &dbgaufs_xib_fop);
7020+ if (unlikely(!sbinfo->si_dbgaufs_xib))
7021+ goto out_dir;
7022+
1facf9fc 7023+ err = dbgaufs_xigen_init(sbinfo);
7024+ if (!err)
7025+ goto out; /* success */
7026+
4f0767ce 7027+out_dir:
1facf9fc 7028+ dbgaufs_si_fin(sbinfo);
4f0767ce 7029+out:
062440b3
AM
7030+ if (unlikely(err))
7031+ pr_err("debugfs/aufs failed\n");
1facf9fc 7032+ return err;
7033+}
7034+
7035+/* ---------------------------------------------------------------------- */
7036+
7037+void dbgaufs_fin(void)
7038+{
7039+ debugfs_remove(dbgaufs);
7040+}
7041+
7042+int __init dbgaufs_init(void)
7043+{
7044+ int err;
7045+
7046+ err = -EIO;
7047+ dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
7048+ if (dbgaufs)
7049+ err = 0;
7050+ return err;
7051+}
7f207e10
AM
7052diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
7053--- /usr/share/empty/fs/aufs/dbgaufs.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 7054+++ linux/fs/aufs/dbgaufs.h 2018-08-12 23:43:05.453457863 +0200
062440b3
AM
7055@@ -0,0 +1,53 @@
7056+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7057+/*
b00004a5 7058+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 7059+ *
7060+ * This program, aufs is free software; you can redistribute it and/or modify
7061+ * it under the terms of the GNU General Public License as published by
7062+ * the Free Software Foundation; either version 2 of the License, or
7063+ * (at your option) any later version.
dece6358
AM
7064+ *
7065+ * This program is distributed in the hope that it will be useful,
7066+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7067+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7068+ * GNU General Public License for more details.
7069+ *
7070+ * You should have received a copy of the GNU General Public License
523b37e3 7071+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7072+ */
7073+
7074+/*
7075+ * debugfs interface
7076+ */
7077+
7078+#ifndef __DBGAUFS_H__
7079+#define __DBGAUFS_H__
7080+
7081+#ifdef __KERNEL__
7082+
dece6358 7083+struct super_block;
1facf9fc 7084+struct au_sbinfo;
062440b3 7085+struct au_branch;
dece6358 7086+
1facf9fc 7087+#ifdef CONFIG_DEBUG_FS
7088+/* dbgaufs.c */
062440b3 7089+void dbgaufs_xino_del(struct au_branch *br);
1facf9fc 7090+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
062440b3 7091+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
1facf9fc 7092+void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
7093+int dbgaufs_si_init(struct au_sbinfo *sbinfo);
7094+void dbgaufs_fin(void);
7095+int __init dbgaufs_init(void);
1facf9fc 7096+#else
062440b3 7097+AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
4a4d8108 7098+AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
062440b3
AM
7099+AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
7100+ int topdown)
4a4d8108
AM
7101+AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
7102+AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
7103+AuStubVoid(dbgaufs_fin, void)
7104+AuStubInt0(__init dbgaufs_init, void)
1facf9fc 7105+#endif /* CONFIG_DEBUG_FS */
7106+
7107+#endif /* __KERNEL__ */
7108+#endif /* __DBGAUFS_H__ */
7f207e10
AM
7109diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
7110--- /usr/share/empty/fs/aufs/dcsub.c 1970-01-01 01:00:00.000000000 +0100
9f237c51 7111+++ linux/fs/aufs/dcsub.c 2018-12-27 13:19:17.708416053 +0100
cd7a4cd9
AM
7112@@ -0,0 +1,225 @@
7113+// SPDX-License-Identifier: GPL-2.0
1facf9fc 7114+/*
b00004a5 7115+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 7116+ *
7117+ * This program, aufs is free software; you can redistribute it and/or modify
7118+ * it under the terms of the GNU General Public License as published by
7119+ * the Free Software Foundation; either version 2 of the License, or
7120+ * (at your option) any later version.
dece6358
AM
7121+ *
7122+ * This program is distributed in the hope that it will be useful,
7123+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7124+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7125+ * GNU General Public License for more details.
7126+ *
7127+ * You should have received a copy of the GNU General Public License
523b37e3 7128+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7129+ */
7130+
7131+/*
7132+ * sub-routines for dentry cache
7133+ */
7134+
7135+#include "aufs.h"
7136+
7137+static void au_dpage_free(struct au_dpage *dpage)
7138+{
7139+ int i;
7140+ struct dentry **p;
7141+
7142+ p = dpage->dentries;
7143+ for (i = 0; i < dpage->ndentry; i++)
7144+ dput(*p++);
1c60b727 7145+ free_page((unsigned long)dpage->dentries);
1facf9fc 7146+}
7147+
7148+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
7149+{
7150+ int err;
7151+ void *p;
7152+
7153+ err = -ENOMEM;
7154+ dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
7155+ if (unlikely(!dpages->dpages))
7156+ goto out;
7157+
7158+ p = (void *)__get_free_page(gfp);
7159+ if (unlikely(!p))
7160+ goto out_dpages;
7161+
7162+ dpages->dpages[0].ndentry = 0;
7163+ dpages->dpages[0].dentries = p;
7164+ dpages->ndpage = 1;
7165+ return 0; /* success */
7166+
4f0767ce 7167+out_dpages:
9f237c51 7168+ au_kfree_try_rcu(dpages->dpages);
4f0767ce 7169+out:
1facf9fc 7170+ return err;
7171+}
7172+
7173+void au_dpages_free(struct au_dcsub_pages *dpages)
7174+{
7175+ int i;
7176+ struct au_dpage *p;
7177+
7178+ p = dpages->dpages;
7179+ for (i = 0; i < dpages->ndpage; i++)
7180+ au_dpage_free(p++);
9f237c51 7181+ au_kfree_try_rcu(dpages->dpages);
1facf9fc 7182+}
7183+
7184+static int au_dpages_append(struct au_dcsub_pages *dpages,
7185+ struct dentry *dentry, gfp_t gfp)
7186+{
7187+ int err, sz;
7188+ struct au_dpage *dpage;
7189+ void *p;
7190+
7191+ dpage = dpages->dpages + dpages->ndpage - 1;
7192+ sz = PAGE_SIZE / sizeof(dentry);
7193+ if (unlikely(dpage->ndentry >= sz)) {
7194+ AuLabel(new dpage);
7195+ err = -ENOMEM;
7196+ sz = dpages->ndpage * sizeof(*dpages->dpages);
7197+ p = au_kzrealloc(dpages->dpages, sz,
e2f27e51
AM
7198+ sz + sizeof(*dpages->dpages), gfp,
7199+ /*may_shrink*/0);
1facf9fc 7200+ if (unlikely(!p))
7201+ goto out;
7202+
7203+ dpages->dpages = p;
7204+ dpage = dpages->dpages + dpages->ndpage;
7205+ p = (void *)__get_free_page(gfp);
7206+ if (unlikely(!p))
7207+ goto out;
7208+
7209+ dpage->ndentry = 0;
7210+ dpage->dentries = p;
7211+ dpages->ndpage++;
7212+ }
7213+
c1595e42 7214+ AuDebugOn(au_dcount(dentry) <= 0);
027c5e7a 7215+ dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
1facf9fc 7216+ return 0; /* success */
7217+
4f0767ce 7218+out:
1facf9fc 7219+ return err;
7220+}
7221+
c1595e42
JR
7222+/* todo: BAD approach */
7223+/* copied from linux/fs/dcache.c */
7224+enum d_walk_ret {
7225+ D_WALK_CONTINUE,
7226+ D_WALK_QUIT,
7227+ D_WALK_NORETRY,
7228+ D_WALK_SKIP,
7229+};
7230+
7231+extern void d_walk(struct dentry *parent, void *data,
cd7a4cd9 7232+ enum d_walk_ret (*enter)(void *, struct dentry *));
c1595e42
JR
7233+
7234+struct ac_dpages_arg {
1facf9fc 7235+ int err;
c1595e42
JR
7236+ struct au_dcsub_pages *dpages;
7237+ struct super_block *sb;
7238+ au_dpages_test test;
7239+ void *arg;
7240+};
1facf9fc 7241+
c1595e42
JR
7242+static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7243+{
7244+ enum d_walk_ret ret;
7245+ struct ac_dpages_arg *arg = _arg;
1facf9fc 7246+
c1595e42
JR
7247+ ret = D_WALK_CONTINUE;
7248+ if (dentry->d_sb == arg->sb
7249+ && !IS_ROOT(dentry)
7250+ && au_dcount(dentry) > 0
7251+ && au_di(dentry)
7252+ && (!arg->test || arg->test(dentry, arg->arg))) {
7253+ arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7254+ if (unlikely(arg->err))
7255+ ret = D_WALK_QUIT;
1facf9fc 7256+ }
7257+
c1595e42
JR
7258+ return ret;
7259+}
027c5e7a 7260+
c1595e42
JR
7261+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7262+ au_dpages_test test, void *arg)
7263+{
7264+ struct ac_dpages_arg args = {
7265+ .err = 0,
7266+ .dpages = dpages,
7267+ .sb = root->d_sb,
7268+ .test = test,
7269+ .arg = arg
7270+ };
027c5e7a 7271+
cd7a4cd9 7272+ d_walk(root, &args, au_call_dpages_append);
c1595e42
JR
7273+
7274+ return args.err;
1facf9fc 7275+}
7276+
7277+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7278+ int do_include, au_dpages_test test, void *arg)
7279+{
7280+ int err;
7281+
7282+ err = 0;
027c5e7a
AM
7283+ write_seqlock(&rename_lock);
7284+ spin_lock(&dentry->d_lock);
7285+ if (do_include
c1595e42 7286+ && au_dcount(dentry) > 0
027c5e7a 7287+ && (!test || test(dentry, arg)))
1facf9fc 7288+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
7289+ spin_unlock(&dentry->d_lock);
7290+ if (unlikely(err))
7291+ goto out;
7292+
7293+ /*
523b37e3 7294+ * RCU for vfsmount is unnecessary since this is a traverse in a single
027c5e7a
AM
7295+ * mount
7296+ */
1facf9fc 7297+ while (!IS_ROOT(dentry)) {
027c5e7a
AM
7298+ dentry = dentry->d_parent; /* rename_lock is locked */
7299+ spin_lock(&dentry->d_lock);
c1595e42 7300+ if (au_dcount(dentry) > 0
027c5e7a 7301+ && (!test || test(dentry, arg)))
1facf9fc 7302+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
7303+ spin_unlock(&dentry->d_lock);
7304+ if (unlikely(err))
7305+ break;
1facf9fc 7306+ }
7307+
4f0767ce 7308+out:
027c5e7a 7309+ write_sequnlock(&rename_lock);
1facf9fc 7310+ return err;
7311+}
7312+
027c5e7a
AM
7313+static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7314+{
7315+ return au_di(dentry) && dentry->d_sb == arg;
7316+}
7317+
7318+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7319+ struct dentry *dentry, int do_include)
7320+{
7321+ return au_dcsub_pages_rev(dpages, dentry, do_include,
7322+ au_dcsub_dpages_aufs, dentry->d_sb);
7323+}
7324+
4a4d8108 7325+int au_test_subdir(struct dentry *d1, struct dentry *d2)
1facf9fc 7326+{
4a4d8108
AM
7327+ struct path path[2] = {
7328+ {
7329+ .dentry = d1
7330+ },
7331+ {
7332+ .dentry = d2
7333+ }
7334+ };
1facf9fc 7335+
4a4d8108 7336+ return path_is_under(path + 0, path + 1);
1facf9fc 7337+}
7f207e10
AM
7338diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7339--- /usr/share/empty/fs/aufs/dcsub.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 7340+++ linux/fs/aufs/dcsub.h 2018-08-12 23:43:05.453457863 +0200
062440b3
AM
7341@@ -0,0 +1,137 @@
7342+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7343+/*
b00004a5 7344+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 7345+ *
7346+ * This program, aufs is free software; you can redistribute it and/or modify
7347+ * it under the terms of the GNU General Public License as published by
7348+ * the Free Software Foundation; either version 2 of the License, or
7349+ * (at your option) any later version.
dece6358
AM
7350+ *
7351+ * This program is distributed in the hope that it will be useful,
7352+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7353+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7354+ * GNU General Public License for more details.
7355+ *
7356+ * You should have received a copy of the GNU General Public License
523b37e3 7357+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7358+ */
7359+
7360+/*
7361+ * sub-routines for dentry cache
7362+ */
7363+
7364+#ifndef __AUFS_DCSUB_H__
7365+#define __AUFS_DCSUB_H__
7366+
7367+#ifdef __KERNEL__
7368+
7f207e10 7369+#include <linux/dcache.h>
027c5e7a 7370+#include <linux/fs.h>
dece6358 7371+
1facf9fc 7372+struct au_dpage {
7373+ int ndentry;
7374+ struct dentry **dentries;
7375+};
7376+
7377+struct au_dcsub_pages {
7378+ int ndpage;
7379+ struct au_dpage *dpages;
7380+};
7381+
7382+/* ---------------------------------------------------------------------- */
7383+
7f207e10 7384+/* dcsub.c */
1facf9fc 7385+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7386+void au_dpages_free(struct au_dcsub_pages *dpages);
7387+typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7388+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7389+ au_dpages_test test, void *arg);
7390+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7391+ int do_include, au_dpages_test test, void *arg);
027c5e7a
AM
7392+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7393+ struct dentry *dentry, int do_include);
4a4d8108 7394+int au_test_subdir(struct dentry *d1, struct dentry *d2);
1facf9fc 7395+
7f207e10
AM
7396+/* ---------------------------------------------------------------------- */
7397+
523b37e3
AM
7398+/*
7399+ * todo: in linux-3.13, several similar (but faster) helpers are added to
7400+ * include/linux/dcache.h. Try them (in the future).
7401+ */
7402+
027c5e7a
AM
7403+static inline int au_d_hashed_positive(struct dentry *d)
7404+{
7405+ int err;
5527c038 7406+ struct inode *inode = d_inode(d);
076b876e 7407+
027c5e7a 7408+ err = 0;
5527c038
JR
7409+ if (unlikely(d_unhashed(d)
7410+ || d_is_negative(d)
7411+ || !inode->i_nlink))
027c5e7a
AM
7412+ err = -ENOENT;
7413+ return err;
7414+}
7415+
38d290e6
JR
7416+static inline int au_d_linkable(struct dentry *d)
7417+{
7418+ int err;
5527c038 7419+ struct inode *inode = d_inode(d);
076b876e 7420+
38d290e6
JR
7421+ err = au_d_hashed_positive(d);
7422+ if (err
5527c038 7423+ && d_is_positive(d)
38d290e6
JR
7424+ && (inode->i_state & I_LINKABLE))
7425+ err = 0;
7426+ return err;
7427+}
7428+
027c5e7a
AM
7429+static inline int au_d_alive(struct dentry *d)
7430+{
7431+ int err;
7432+ struct inode *inode;
076b876e 7433+
027c5e7a
AM
7434+ err = 0;
7435+ if (!IS_ROOT(d))
7436+ err = au_d_hashed_positive(d);
7437+ else {
5527c038
JR
7438+ inode = d_inode(d);
7439+ if (unlikely(d_unlinked(d)
7440+ || d_is_negative(d)
7441+ || !inode->i_nlink))
027c5e7a
AM
7442+ err = -ENOENT;
7443+ }
7444+ return err;
7445+}
7446+
7447+static inline int au_alive_dir(struct dentry *d)
7f207e10 7448+{
027c5e7a 7449+ int err;
076b876e 7450+
027c5e7a 7451+ err = au_d_alive(d);
5527c038 7452+ if (unlikely(err || IS_DEADDIR(d_inode(d))))
027c5e7a
AM
7453+ err = -ENOENT;
7454+ return err;
7f207e10
AM
7455+}
7456+
38d290e6
JR
7457+static inline int au_qstreq(struct qstr *a, struct qstr *b)
7458+{
7459+ return a->len == b->len
7460+ && !memcmp(a->name, b->name, a->len);
7461+}
7462+
7e9cd9fe
AM
7463+/*
7464+ * by the commit
7465+ * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7466+ * taking d_lock
7467+ * the type of d_lockref.count became int, but the inlined function d_count()
7468+ * still returns unsigned int.
7469+ * I don't know why. Maybe it is for every d_count() users?
7470+ * Anyway au_dcount() lives on.
7471+ */
c1595e42
JR
7472+static inline int au_dcount(struct dentry *d)
7473+{
7474+ return (int)d_count(d);
7475+}
7476+
1facf9fc 7477+#endif /* __KERNEL__ */
7478+#endif /* __AUFS_DCSUB_H__ */
7f207e10
AM
7479diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7480--- /usr/share/empty/fs/aufs/debug.c 1970-01-01 01:00:00.000000000 +0100
9f237c51 7481+++ linux/fs/aufs/debug.c 2018-12-27 13:19:17.708416053 +0100
acd2b654 7482@@ -0,0 +1,440 @@
cd7a4cd9 7483+// SPDX-License-Identifier: GPL-2.0
1facf9fc 7484+/*
b00004a5 7485+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 7486+ *
7487+ * This program, aufs is free software; you can redistribute it and/or modify
7488+ * it under the terms of the GNU General Public License as published by
7489+ * the Free Software Foundation; either version 2 of the License, or
7490+ * (at your option) any later version.
dece6358
AM
7491+ *
7492+ * This program is distributed in the hope that it will be useful,
7493+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7494+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7495+ * GNU General Public License for more details.
7496+ *
7497+ * You should have received a copy of the GNU General Public License
523b37e3 7498+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7499+ */
7500+
7501+/*
7502+ * debug print functions
7503+ */
7504+
7505+#include "aufs.h"
7506+
392086de
AM
7507+/* Returns 0, or -errno. arg is in kp->arg. */
7508+static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7509+{
7510+ int err, n;
7511+
7512+ err = kstrtoint(val, 0, &n);
7513+ if (!err) {
7514+ if (n > 0)
7515+ au_debug_on();
7516+ else
7517+ au_debug_off();
7518+ }
7519+ return err;
7520+}
7521+
7522+/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
7523+static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7524+{
7525+ atomic_t *a;
7526+
7527+ a = kp->arg;
7528+ return sprintf(buffer, "%d", atomic_read(a));
7529+}
7530+
7531+static struct kernel_param_ops param_ops_atomic_t = {
7532+ .set = param_atomic_t_set,
7533+ .get = param_atomic_t_get
7534+ /* void (*free)(void *arg) */
7535+};
7536+
7537+atomic_t aufs_debug = ATOMIC_INIT(0);
1facf9fc 7538+MODULE_PARM_DESC(debug, "debug print");
cd7a4cd9 7539+module_param_named(debug, aufs_debug, atomic_t, 0664);
1facf9fc 7540+
c1595e42 7541+DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
1facf9fc 7542+char *au_plevel = KERN_DEBUG;
e49829fe
JR
7543+#define dpri(fmt, ...) do { \
7544+ if ((au_plevel \
7545+ && strcmp(au_plevel, KERN_DEBUG)) \
7546+ || au_debug_test()) \
7547+ printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
1facf9fc 7548+} while (0)
7549+
7550+/* ---------------------------------------------------------------------- */
7551+
7552+void au_dpri_whlist(struct au_nhash *whlist)
7553+{
7554+ unsigned long ul, n;
7555+ struct hlist_head *head;
c06a8ce3 7556+ struct au_vdir_wh *pos;
1facf9fc 7557+
7558+ n = whlist->nh_num;
7559+ head = whlist->nh_head;
7560+ for (ul = 0; ul < n; ul++) {
c06a8ce3 7561+ hlist_for_each_entry(pos, head, wh_hash)
1facf9fc 7562+ dpri("b%d, %.*s, %d\n",
c06a8ce3
AM
7563+ pos->wh_bindex,
7564+ pos->wh_str.len, pos->wh_str.name,
7565+ pos->wh_str.len);
1facf9fc 7566+ head++;
7567+ }
7568+}
7569+
7570+void au_dpri_vdir(struct au_vdir *vdir)
7571+{
7572+ unsigned long ul;
7573+ union au_vdir_deblk_p p;
7574+ unsigned char *o;
7575+
7576+ if (!vdir || IS_ERR(vdir)) {
7577+ dpri("err %ld\n", PTR_ERR(vdir));
7578+ return;
7579+ }
7580+
be118d29 7581+ dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
1facf9fc 7582+ vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7583+ vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7584+ for (ul = 0; ul < vdir->vd_nblk; ul++) {
7585+ p.deblk = vdir->vd_deblk[ul];
7586+ o = p.deblk;
7587+ dpri("[%lu]: %p\n", ul, o);
7588+ }
7589+}
7590+
53392da6 7591+static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
1facf9fc 7592+ struct dentry *wh)
7593+{
7594+ char *n = NULL;
7595+ int l = 0;
7596+
7597+ if (!inode || IS_ERR(inode)) {
7598+ dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7599+ return -1;
7600+ }
7601+
c2b27bf2 7602+ /* the type of i_blocks depends upon CONFIG_LBDAF */
1facf9fc 7603+ BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7604+ && sizeof(inode->i_blocks) != sizeof(u64));
7605+ if (wh) {
7606+ n = (void *)wh->d_name.name;
7607+ l = wh->d_name.len;
7608+ }
7609+
53392da6
AM
7610+ dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7611+ " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7612+ bindex, inode,
1facf9fc 7613+ inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7614+ atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7615+ i_size_read(inode), (unsigned long long)inode->i_blocks,
cd7a4cd9 7616+ hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
1facf9fc 7617+ inode->i_mapping ? inode->i_mapping->nrpages : 0,
be118d29 7618+ inode->i_state, inode->i_flags, inode_peek_iversion(inode),
b752ccd1 7619+ inode->i_generation,
1facf9fc 7620+ l ? ", wh " : "", l, n);
7621+ return 0;
7622+}
7623+
7624+void au_dpri_inode(struct inode *inode)
7625+{
7626+ struct au_iinfo *iinfo;
5afbbe0d 7627+ struct au_hinode *hi;
1facf9fc 7628+ aufs_bindex_t bindex;
53392da6 7629+ int err, hn;
1facf9fc 7630+
53392da6 7631+ err = do_pri_inode(-1, inode, -1, NULL);
5afbbe0d 7632+ if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
1facf9fc 7633+ return;
7634+
7635+ iinfo = au_ii(inode);
5afbbe0d
AM
7636+ dpri("i-1: btop %d, bbot %d, gen %d\n",
7637+ iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7638+ if (iinfo->ii_btop < 0)
1facf9fc 7639+ return;
53392da6 7640+ hn = 0;
5afbbe0d
AM
7641+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7642+ hi = au_hinode(iinfo, bindex);
7643+ hn = !!au_hn(hi);
7644+ do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
53392da6 7645+ }
1facf9fc 7646+}
7647+
2cbb1c4b
JR
7648+void au_dpri_dalias(struct inode *inode)
7649+{
7650+ struct dentry *d;
7651+
7652+ spin_lock(&inode->i_lock);
c1595e42 7653+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
2cbb1c4b
JR
7654+ au_dpri_dentry(d);
7655+ spin_unlock(&inode->i_lock);
7656+}
7657+
1facf9fc 7658+static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7659+{
7660+ struct dentry *wh = NULL;
53392da6 7661+ int hn;
5afbbe0d 7662+ struct inode *inode;
076b876e 7663+ struct au_iinfo *iinfo;
5afbbe0d 7664+ struct au_hinode *hi;
1facf9fc 7665+
7666+ if (!dentry || IS_ERR(dentry)) {
7667+ dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7668+ return -1;
7669+ }
7670+ /* do not call dget_parent() here */
027c5e7a 7671+ /* note: access d_xxx without d_lock */
523b37e3
AM
7672+ dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7673+ bindex, dentry, dentry,
1facf9fc 7674+ dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
c1595e42 7675+ au_dcount(dentry), dentry->d_flags,
523b37e3 7676+ d_unhashed(dentry) ? "un" : "");
53392da6 7677+ hn = -1;
5afbbe0d
AM
7678+ inode = NULL;
7679+ if (d_is_positive(dentry))
7680+ inode = d_inode(dentry);
7681+ if (inode
7682+ && au_test_aufs(dentry->d_sb)
7683+ && bindex >= 0
7684+ && !au_is_bad_inode(inode)) {
7685+ iinfo = au_ii(inode);
7686+ hi = au_hinode(iinfo, bindex);
7687+ hn = !!au_hn(hi);
7688+ wh = hi->hi_whdentry;
7689+ }
7690+ do_pri_inode(bindex, inode, hn, wh);
1facf9fc 7691+ return 0;
7692+}
7693+
7694+void au_dpri_dentry(struct dentry *dentry)
7695+{
7696+ struct au_dinfo *dinfo;
7697+ aufs_bindex_t bindex;
7698+ int err;
7699+
7700+ err = do_pri_dentry(-1, dentry);
7701+ if (err || !au_test_aufs(dentry->d_sb))
7702+ return;
7703+
7704+ dinfo = au_di(dentry);
7705+ if (!dinfo)
7706+ return;
5afbbe0d
AM
7707+ dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7708+ dinfo->di_btop, dinfo->di_bbot,
38d290e6
JR
7709+ dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7710+ dinfo->di_tmpfile);
5afbbe0d 7711+ if (dinfo->di_btop < 0)
1facf9fc 7712+ return;
5afbbe0d
AM
7713+ for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7714+ do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
1facf9fc 7715+}
7716+
7717+static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7718+{
7719+ char a[32];
7720+
7721+ if (!file || IS_ERR(file)) {
7722+ dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7723+ return -1;
7724+ }
7725+ a[0] = 0;
7726+ if (bindex < 0
b912730e 7727+ && !IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7728+ && au_test_aufs(file->f_path.dentry->d_sb)
1facf9fc 7729+ && au_fi(file))
e49829fe 7730+ snprintf(a, sizeof(a), ", gen %d, mmapped %d",
2cbb1c4b 7731+ au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
b752ccd1 7732+ dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
1facf9fc 7733+ bindex, file->f_mode, file->f_flags, (long)file_count(file),
b752ccd1 7734+ file->f_version, file->f_pos, a);
b912730e 7735+ if (!IS_ERR_OR_NULL(file->f_path.dentry))
2000de60 7736+ do_pri_dentry(bindex, file->f_path.dentry);
1facf9fc 7737+ return 0;
7738+}
7739+
7740+void au_dpri_file(struct file *file)
7741+{
7742+ struct au_finfo *finfo;
4a4d8108
AM
7743+ struct au_fidir *fidir;
7744+ struct au_hfile *hfile;
1facf9fc 7745+ aufs_bindex_t bindex;
7746+ int err;
7747+
7748+ err = do_pri_file(-1, file);
2000de60 7749+ if (err
b912730e 7750+ || IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7751+ || !au_test_aufs(file->f_path.dentry->d_sb))
1facf9fc 7752+ return;
7753+
7754+ finfo = au_fi(file);
7755+ if (!finfo)
7756+ return;
4a4d8108 7757+ if (finfo->fi_btop < 0)
1facf9fc 7758+ return;
4a4d8108
AM
7759+ fidir = finfo->fi_hdir;
7760+ if (!fidir)
7761+ do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7762+ else
e49829fe
JR
7763+ for (bindex = finfo->fi_btop;
7764+ bindex >= 0 && bindex <= fidir->fd_bbot;
4a4d8108
AM
7765+ bindex++) {
7766+ hfile = fidir->fd_hfile + bindex;
7767+ do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7768+ }
1facf9fc 7769+}
7770+
7771+static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7772+{
7773+ struct vfsmount *mnt;
7774+ struct super_block *sb;
7775+
7776+ if (!br || IS_ERR(br))
7777+ goto out;
86dc4139 7778+ mnt = au_br_mnt(br);
1facf9fc 7779+ if (!mnt || IS_ERR(mnt))
7780+ goto out;
7781+ sb = mnt->mnt_sb;
7782+ if (!sb || IS_ERR(sb))
7783+ goto out;
7784+
acd2b654 7785+ dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
b752ccd1 7786+ "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
1facf9fc 7787+ "xino %d\n",
acd2b654
AM
7788+ bindex, br->br_perm, br->br_id, br->br_wbr,
7789+ au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
b752ccd1 7790+ sb->s_flags, sb->s_count,
acd2b654
AM
7791+ atomic_read(&sb->s_active),
7792+ !!au_xino_file(br->br_xino, /*idx*/-1));
1facf9fc 7793+ return 0;
7794+
4f0767ce 7795+out:
1facf9fc 7796+ dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7797+ return -1;
7798+}
7799+
7800+void au_dpri_sb(struct super_block *sb)
7801+{
7802+ struct au_sbinfo *sbinfo;
7803+ aufs_bindex_t bindex;
7804+ int err;
acd2b654 7805+ /* to reduce stack size */
1facf9fc 7806+ struct {
7807+ struct vfsmount mnt;
7808+ struct au_branch fake;
7809+ } *a;
7810+
7811+ /* this function can be called from magic sysrq */
7812+ a = kzalloc(sizeof(*a), GFP_ATOMIC);
7813+ if (unlikely(!a)) {
7814+ dpri("no memory\n");
7815+ return;
7816+ }
7817+
7818+ a->mnt.mnt_sb = sb;
86dc4139 7819+ a->fake.br_path.mnt = &a->mnt;
1facf9fc 7820+ err = do_pri_br(-1, &a->fake);
9f237c51 7821+ au_kfree_rcu(a);
1facf9fc 7822+ dpri("dev 0x%x\n", sb->s_dev);
7823+ if (err || !au_test_aufs(sb))
7824+ return;
7825+
7826+ sbinfo = au_sbi(sb);
7827+ if (!sbinfo)
7828+ return;
f0c0a007
AM
7829+ dpri("nw %d, gen %u, kobj %d\n",
7830+ atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
521ced18 7831+ kref_read(&sbinfo->si_kobj.kref));
5afbbe0d 7832+ for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
1facf9fc 7833+ do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7834+}
7835+
7836+/* ---------------------------------------------------------------------- */
7837+
027c5e7a
AM
7838+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7839+{
5527c038 7840+ struct inode *h_inode, *inode = d_inode(dentry);
027c5e7a 7841+ struct dentry *h_dentry;
5afbbe0d 7842+ aufs_bindex_t bindex, bbot, bi;
027c5e7a
AM
7843+
7844+ if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7845+ return;
7846+
5afbbe0d
AM
7847+ bbot = au_dbbot(dentry);
7848+ bi = au_ibbot(inode);
7849+ if (bi < bbot)
7850+ bbot = bi;
7851+ bindex = au_dbtop(dentry);
7852+ bi = au_ibtop(inode);
027c5e7a
AM
7853+ if (bi > bindex)
7854+ bindex = bi;
7855+
5afbbe0d 7856+ for (; bindex <= bbot; bindex++) {
027c5e7a
AM
7857+ h_dentry = au_h_dptr(dentry, bindex);
7858+ if (!h_dentry)
7859+ continue;
7860+ h_inode = au_h_iptr(inode, bindex);
5527c038 7861+ if (unlikely(h_inode != d_inode(h_dentry))) {
392086de 7862+ au_debug_on();
027c5e7a
AM
7863+ AuDbg("b%d, %s:%d\n", bindex, func, line);
7864+ AuDbgDentry(dentry);
7865+ AuDbgInode(inode);
392086de 7866+ au_debug_off();
027c5e7a
AM
7867+ BUG();
7868+ }
7869+ }
7870+}
7871+
1facf9fc 7872+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7873+{
7874+ int err, i, j;
7875+ struct au_dcsub_pages dpages;
7876+ struct au_dpage *dpage;
7877+ struct dentry **dentries;
7878+
7879+ err = au_dpages_init(&dpages, GFP_NOFS);
7880+ AuDebugOn(err);
027c5e7a 7881+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
1facf9fc 7882+ AuDebugOn(err);
7883+ for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7884+ dpage = dpages.dpages + i;
7885+ dentries = dpage->dentries;
7886+ for (j = dpage->ndentry - 1; !err && j >= 0; j--)
027c5e7a 7887+ AuDebugOn(au_digen_test(dentries[j], sigen));
1facf9fc 7888+ }
7889+ au_dpages_free(&dpages);
7890+}
7891+
1facf9fc 7892+void au_dbg_verify_kthread(void)
7893+{
53392da6 7894+ if (au_wkq_test()) {
1facf9fc 7895+ au_dbg_blocked();
1e00d052
AM
7896+ /*
7897+ * It may be recursive, but udba=notify between two aufs mounts,
7898+ * where a single ro branch is shared, is not a problem.
7899+ */
7900+ /* WARN_ON(1); */
1facf9fc 7901+ }
7902+}
7903+
7904+/* ---------------------------------------------------------------------- */
7905+
1facf9fc 7906+int __init au_debug_init(void)
7907+{
7908+ aufs_bindex_t bindex;
7909+ struct au_vdir_destr destr;
7910+
7911+ bindex = -1;
7912+ AuDebugOn(bindex >= 0);
7913+
7914+ destr.len = -1;
7915+ AuDebugOn(destr.len < NAME_MAX);
7916+
7917+#ifdef CONFIG_4KSTACKS
0c3ec466 7918+ pr_warn("CONFIG_4KSTACKS is defined.\n");
1facf9fc 7919+#endif
7920+
1facf9fc 7921+ return 0;
7922+}
7f207e10
AM
7923diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7924--- /usr/share/empty/fs/aufs/debug.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 7925+++ linux/fs/aufs/debug.h 2018-08-12 23:43:05.453457863 +0200
062440b3
AM
7926@@ -0,0 +1,226 @@
7927+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7928+/*
b00004a5 7929+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 7930+ *
7931+ * This program, aufs is free software; you can redistribute it and/or modify
7932+ * it under the terms of the GNU General Public License as published by
7933+ * the Free Software Foundation; either version 2 of the License, or
7934+ * (at your option) any later version.
dece6358
AM
7935+ *
7936+ * This program is distributed in the hope that it will be useful,
7937+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7938+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7939+ * GNU General Public License for more details.
7940+ *
7941+ * You should have received a copy of the GNU General Public License
523b37e3 7942+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7943+ */
7944+
7945+/*
7946+ * debug print functions
7947+ */
7948+
7949+#ifndef __AUFS_DEBUG_H__
7950+#define __AUFS_DEBUG_H__
7951+
7952+#ifdef __KERNEL__
7953+
392086de 7954+#include <linux/atomic.h>
4a4d8108
AM
7955+#include <linux/module.h>
7956+#include <linux/kallsyms.h>
1facf9fc 7957+#include <linux/sysrq.h>
4a4d8108 7958+
1facf9fc 7959+#ifdef CONFIG_AUFS_DEBUG
7960+#define AuDebugOn(a) BUG_ON(a)
7961+
7962+/* module parameter */
392086de
AM
7963+extern atomic_t aufs_debug;
7964+static inline void au_debug_on(void)
1facf9fc 7965+{
392086de
AM
7966+ atomic_inc(&aufs_debug);
7967+}
7968+static inline void au_debug_off(void)
7969+{
7970+ atomic_dec_if_positive(&aufs_debug);
1facf9fc 7971+}
7972+
7973+static inline int au_debug_test(void)
7974+{
392086de 7975+ return atomic_read(&aufs_debug) > 0;
1facf9fc 7976+}
7977+#else
7978+#define AuDebugOn(a) do {} while (0)
392086de
AM
7979+AuStubVoid(au_debug_on, void)
7980+AuStubVoid(au_debug_off, void)
4a4d8108 7981+AuStubInt0(au_debug_test, void)
1facf9fc 7982+#endif /* CONFIG_AUFS_DEBUG */
7983+
392086de
AM
7984+#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7985+
1facf9fc 7986+/* ---------------------------------------------------------------------- */
7987+
7988+/* debug print */
7989+
4a4d8108 7990+#define AuDbg(fmt, ...) do { \
1facf9fc 7991+ if (au_debug_test()) \
4a4d8108 7992+ pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
1facf9fc 7993+} while (0)
4a4d8108
AM
7994+#define AuLabel(l) AuDbg(#l "\n")
7995+#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7996+#define AuWarn1(fmt, ...) do { \
1facf9fc 7997+ static unsigned char _c; \
7998+ if (!_c++) \
0c3ec466 7999+ pr_warn(fmt, ##__VA_ARGS__); \
1facf9fc 8000+} while (0)
8001+
4a4d8108 8002+#define AuErr1(fmt, ...) do { \
1facf9fc 8003+ static unsigned char _c; \
8004+ if (!_c++) \
4a4d8108 8005+ pr_err(fmt, ##__VA_ARGS__); \
1facf9fc 8006+} while (0)
8007+
4a4d8108 8008+#define AuIOErr1(fmt, ...) do { \
1facf9fc 8009+ static unsigned char _c; \
8010+ if (!_c++) \
4a4d8108 8011+ AuIOErr(fmt, ##__VA_ARGS__); \
1facf9fc 8012+} while (0)
8013+
8014+#define AuUnsupportMsg "This operation is not supported." \
8015+ " Please report this application to aufs-users ML."
4a4d8108
AM
8016+#define AuUnsupport(fmt, ...) do { \
8017+ pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
1facf9fc 8018+ dump_stack(); \
8019+} while (0)
8020+
8021+#define AuTraceErr(e) do { \
8022+ if (unlikely((e) < 0)) \
8023+ AuDbg("err %d\n", (int)(e)); \
8024+} while (0)
8025+
8026+#define AuTraceErrPtr(p) do { \
8027+ if (IS_ERR(p)) \
8028+ AuDbg("err %ld\n", PTR_ERR(p)); \
8029+} while (0)
8030+
8031+/* dirty macros for debug print, use with "%.*s" and caution */
8032+#define AuLNPair(qstr) (qstr)->len, (qstr)->name
1facf9fc 8033+
8034+/* ---------------------------------------------------------------------- */
8035+
dece6358 8036+struct dentry;
1facf9fc 8037+#ifdef CONFIG_AUFS_DEBUG
c1595e42 8038+extern struct mutex au_dbg_mtx;
1facf9fc 8039+extern char *au_plevel;
8040+struct au_nhash;
8041+void au_dpri_whlist(struct au_nhash *whlist);
8042+struct au_vdir;
8043+void au_dpri_vdir(struct au_vdir *vdir);
dece6358 8044+struct inode;
1facf9fc 8045+void au_dpri_inode(struct inode *inode);
2cbb1c4b 8046+void au_dpri_dalias(struct inode *inode);
1facf9fc 8047+void au_dpri_dentry(struct dentry *dentry);
dece6358 8048+struct file;
1facf9fc 8049+void au_dpri_file(struct file *filp);
dece6358 8050+struct super_block;
1facf9fc 8051+void au_dpri_sb(struct super_block *sb);
8052+
027c5e7a
AM
8053+#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
8054+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
1facf9fc 8055+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
1facf9fc 8056+void au_dbg_verify_kthread(void);
8057+
8058+int __init au_debug_init(void);
7e9cd9fe 8059+
1facf9fc 8060+#define AuDbgWhlist(w) do { \
c1595e42 8061+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8062+ AuDbg(#w "\n"); \
8063+ au_dpri_whlist(w); \
c1595e42 8064+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8065+} while (0)
8066+
8067+#define AuDbgVdir(v) do { \
c1595e42 8068+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8069+ AuDbg(#v "\n"); \
8070+ au_dpri_vdir(v); \
c1595e42 8071+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8072+} while (0)
8073+
8074+#define AuDbgInode(i) do { \
c1595e42 8075+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8076+ AuDbg(#i "\n"); \
8077+ au_dpri_inode(i); \
c1595e42 8078+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8079+} while (0)
8080+
2cbb1c4b 8081+#define AuDbgDAlias(i) do { \
c1595e42 8082+ mutex_lock(&au_dbg_mtx); \
2cbb1c4b
JR
8083+ AuDbg(#i "\n"); \
8084+ au_dpri_dalias(i); \
c1595e42 8085+ mutex_unlock(&au_dbg_mtx); \
2cbb1c4b
JR
8086+} while (0)
8087+
1facf9fc 8088+#define AuDbgDentry(d) do { \
c1595e42 8089+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8090+ AuDbg(#d "\n"); \
8091+ au_dpri_dentry(d); \
c1595e42 8092+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8093+} while (0)
8094+
8095+#define AuDbgFile(f) do { \
c1595e42 8096+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8097+ AuDbg(#f "\n"); \
8098+ au_dpri_file(f); \
c1595e42 8099+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8100+} while (0)
8101+
8102+#define AuDbgSb(sb) do { \
c1595e42 8103+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8104+ AuDbg(#sb "\n"); \
8105+ au_dpri_sb(sb); \
c1595e42 8106+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8107+} while (0)
8108+
4a4d8108
AM
8109+#define AuDbgSym(addr) do { \
8110+ char sym[KSYM_SYMBOL_LEN]; \
8111+ sprint_symbol(sym, (unsigned long)addr); \
8112+ AuDbg("%s\n", sym); \
8113+} while (0)
1facf9fc 8114+#else
027c5e7a 8115+AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
4a4d8108
AM
8116+AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
8117+AuStubVoid(au_dbg_verify_kthread, void)
8118+AuStubInt0(__init au_debug_init, void)
1facf9fc 8119+
1facf9fc 8120+#define AuDbgWhlist(w) do {} while (0)
8121+#define AuDbgVdir(v) do {} while (0)
8122+#define AuDbgInode(i) do {} while (0)
2cbb1c4b 8123+#define AuDbgDAlias(i) do {} while (0)
1facf9fc 8124+#define AuDbgDentry(d) do {} while (0)
8125+#define AuDbgFile(f) do {} while (0)
8126+#define AuDbgSb(sb) do {} while (0)
4a4d8108 8127+#define AuDbgSym(addr) do {} while (0)
1facf9fc 8128+#endif /* CONFIG_AUFS_DEBUG */
8129+
8130+/* ---------------------------------------------------------------------- */
8131+
8132+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
8133+int __init au_sysrq_init(void);
8134+void au_sysrq_fin(void);
8135+
8136+#ifdef CONFIG_HW_CONSOLE
8137+#define au_dbg_blocked() do { \
8138+ WARN_ON(1); \
0c5527e5 8139+ handle_sysrq('w'); \
1facf9fc 8140+} while (0)
8141+#else
4a4d8108 8142+AuStubVoid(au_dbg_blocked, void)
1facf9fc 8143+#endif
8144+
8145+#else
4a4d8108
AM
8146+AuStubInt0(__init au_sysrq_init, void)
8147+AuStubVoid(au_sysrq_fin, void)
8148+AuStubVoid(au_dbg_blocked, void)
1facf9fc 8149+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
8150+
8151+#endif /* __KERNEL__ */
8152+#endif /* __AUFS_DEBUG_H__ */
7f207e10
AM
8153diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
8154--- /usr/share/empty/fs/aufs/dentry.c 1970-01-01 01:00:00.000000000 +0100
9f237c51 8155+++ linux/fs/aufs/dentry.c 2018-12-27 13:19:17.708416053 +0100
062440b3 8156@@ -0,0 +1,1153 @@
cd7a4cd9 8157+// SPDX-License-Identifier: GPL-2.0
1facf9fc 8158+/*
b00004a5 8159+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 8160+ *
8161+ * This program, aufs is free software; you can redistribute it and/or modify
8162+ * it under the terms of the GNU General Public License as published by
8163+ * the Free Software Foundation; either version 2 of the License, or
8164+ * (at your option) any later version.
dece6358
AM
8165+ *
8166+ * This program is distributed in the hope that it will be useful,
8167+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
8168+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8169+ * GNU General Public License for more details.
8170+ *
8171+ * You should have received a copy of the GNU General Public License
523b37e3 8172+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 8173+ */
8174+
8175+/*
8176+ * lookup and dentry operations
8177+ */
8178+
dece6358 8179+#include <linux/namei.h>
1facf9fc 8180+#include "aufs.h"
8181+
1facf9fc 8182+/*
8183+ * returns positive/negative dentry, NULL or an error.
8184+ * NULL means whiteout-ed or not-found.
8185+ */
8186+static struct dentry*
8187+au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
8b6a4947 8188+ aufs_bindex_t bindex, struct au_do_lookup_args *args)
1facf9fc 8189+{
8190+ struct dentry *h_dentry;
2000de60 8191+ struct inode *h_inode;
1facf9fc 8192+ struct au_branch *br;
8193+ int wh_found, opq;
8194+ unsigned char wh_able;
8195+ const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
076b876e
AM
8196+ const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8197+ IGNORE_PERM);
1facf9fc 8198+
1facf9fc 8199+ wh_found = 0;
8200+ br = au_sbr(dentry->d_sb, bindex);
8201+ wh_able = !!au_br_whable(br->br_perm);
8202+ if (wh_able)
8b6a4947 8203+ wh_found = au_wh_test(h_parent, &args->whname, ignore_perm);
1facf9fc 8204+ h_dentry = ERR_PTR(wh_found);
8205+ if (!wh_found)
8206+ goto real_lookup;
8207+ if (unlikely(wh_found < 0))
8208+ goto out;
8209+
8210+ /* We found a whiteout */
5afbbe0d 8211+ /* au_set_dbbot(dentry, bindex); */
1facf9fc 8212+ au_set_dbwh(dentry, bindex);
8213+ if (!allow_neg)
8214+ return NULL; /* success */
8215+
4f0767ce 8216+real_lookup:
076b876e 8217+ if (!ignore_perm)
8b6a4947 8218+ h_dentry = vfsub_lkup_one(args->name, h_parent);
076b876e 8219+ else
8b6a4947 8220+ h_dentry = au_sio_lkup_one(args->name, h_parent);
2000de60
JR
8221+ if (IS_ERR(h_dentry)) {
8222+ if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8223+ && !allow_neg)
8224+ h_dentry = NULL;
1facf9fc 8225+ goto out;
2000de60 8226+ }
1facf9fc 8227+
5527c038
JR
8228+ h_inode = d_inode(h_dentry);
8229+ if (d_is_negative(h_dentry)) {
1facf9fc 8230+ if (!allow_neg)
8231+ goto out_neg;
8232+ } else if (wh_found
8233+ || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8234+ goto out_neg;
8b6a4947
AM
8235+ else if (au_ftest_lkup(args->flags, DIRREN)
8236+ /* && h_inode */
8237+ && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8238+ AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8239+ (unsigned long long)h_inode->i_ino);
8240+ goto out_neg;
8241+ }
1facf9fc 8242+
5afbbe0d
AM
8243+ if (au_dbbot(dentry) <= bindex)
8244+ au_set_dbbot(dentry, bindex);
8245+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8246+ au_set_dbtop(dentry, bindex);
1facf9fc 8247+ au_set_h_dptr(dentry, bindex, h_dentry);
8248+
2000de60
JR
8249+ if (!d_is_dir(h_dentry)
8250+ || !wh_able
5527c038 8251+ || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
1facf9fc 8252+ goto out; /* success */
8253+
be118d29 8254+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
076b876e 8255+ opq = au_diropq_test(h_dentry);
3c1bdaff 8256+ inode_unlock_shared(h_inode);
1facf9fc 8257+ if (opq > 0)
8258+ au_set_dbdiropq(dentry, bindex);
8259+ else if (unlikely(opq < 0)) {
8260+ au_set_h_dptr(dentry, bindex, NULL);
8261+ h_dentry = ERR_PTR(opq);
8262+ }
8263+ goto out;
8264+
4f0767ce 8265+out_neg:
1facf9fc 8266+ dput(h_dentry);
8267+ h_dentry = NULL;
4f0767ce 8268+out:
1facf9fc 8269+ return h_dentry;
8270+}
8271+
dece6358
AM
8272+static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8273+{
8274+ if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8275+ && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8276+ return -EPERM;
8277+ return 0;
8278+}
8279+
1facf9fc 8280+/*
8281+ * returns the number of lower positive dentries,
8282+ * otherwise an error.
8283+ * can be called at unlinking with @type is zero.
8284+ */
5afbbe0d
AM
8285+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8286+ unsigned int flags)
1facf9fc 8287+{
8288+ int npositive, err;
8289+ aufs_bindex_t bindex, btail, bdiropq;
8b6a4947 8290+ unsigned char isdir, dirperm1, dirren;
1facf9fc 8291+ struct au_do_lookup_args args = {
8b6a4947
AM
8292+ .flags = flags,
8293+ .name = &dentry->d_name
1facf9fc 8294+ };
1facf9fc 8295+ struct dentry *parent;
076b876e 8296+ struct super_block *sb;
1facf9fc 8297+
076b876e 8298+ sb = dentry->d_sb;
8b6a4947 8299+ err = au_test_shwh(sb, args.name);
dece6358 8300+ if (unlikely(err))
1facf9fc 8301+ goto out;
8302+
8b6a4947 8303+ err = au_wh_name_alloc(&args.whname, args.name);
1facf9fc 8304+ if (unlikely(err))
8305+ goto out;
8306+
2000de60 8307+ isdir = !!d_is_dir(dentry);
076b876e 8308+ dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8b6a4947
AM
8309+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8310+ if (dirren)
8311+ au_fset_lkup(args.flags, DIRREN);
1facf9fc 8312+
8313+ npositive = 0;
4a4d8108 8314+ parent = dget_parent(dentry);
1facf9fc 8315+ btail = au_dbtaildir(parent);
5afbbe0d 8316+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 8317+ struct dentry *h_parent, *h_dentry;
8318+ struct inode *h_inode, *h_dir;
8b6a4947 8319+ struct au_branch *br;
1facf9fc 8320+
8321+ h_dentry = au_h_dptr(dentry, bindex);
8322+ if (h_dentry) {
5527c038 8323+ if (d_is_positive(h_dentry))
1facf9fc 8324+ npositive++;
5afbbe0d 8325+ break;
1facf9fc 8326+ }
8327+ h_parent = au_h_dptr(parent, bindex);
2000de60 8328+ if (!h_parent || !d_is_dir(h_parent))
1facf9fc 8329+ continue;
8330+
8b6a4947
AM
8331+ if (dirren) {
8332+ /* if the inum matches, then use the prepared name */
8333+ err = au_dr_lkup_name(&args, bindex);
8334+ if (unlikely(err))
8335+ goto out_parent;
8336+ }
8337+
5527c038 8338+ h_dir = d_inode(h_parent);
be118d29 8339+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8b6a4947 8340+ h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
3c1bdaff 8341+ inode_unlock_shared(h_dir);
1facf9fc 8342+ err = PTR_ERR(h_dentry);
8343+ if (IS_ERR(h_dentry))
4a4d8108 8344+ goto out_parent;
2000de60
JR
8345+ if (h_dentry)
8346+ au_fclr_lkup(args.flags, ALLOW_NEG);
076b876e
AM
8347+ if (dirperm1)
8348+ au_fset_lkup(args.flags, IGNORE_PERM);
1facf9fc 8349+
79b8bda9 8350+ if (au_dbwh(dentry) == bindex)
1facf9fc 8351+ break;
8352+ if (!h_dentry)
8353+ continue;
5527c038 8354+ if (d_is_negative(h_dentry))
1facf9fc 8355+ continue;
5527c038 8356+ h_inode = d_inode(h_dentry);
1facf9fc 8357+ npositive++;
8358+ if (!args.type)
8359+ args.type = h_inode->i_mode & S_IFMT;
8360+ if (args.type != S_IFDIR)
8361+ break;
8362+ else if (isdir) {
8363+ /* the type of lower may be different */
8364+ bdiropq = au_dbdiropq(dentry);
8365+ if (bdiropq >= 0 && bdiropq <= bindex)
8366+ break;
8367+ }
8b6a4947
AM
8368+ br = au_sbr(sb, bindex);
8369+ if (dirren
8370+ && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8371+ /*add_ent*/NULL)) {
8372+ /* prepare next name to lookup */
8373+ err = au_dr_lkup(&args, dentry, bindex);
8374+ if (unlikely(err))
8375+ goto out_parent;
8376+ }
1facf9fc 8377+ }
8378+
8379+ if (npositive) {
8380+ AuLabel(positive);
5afbbe0d 8381+ au_update_dbtop(dentry);
1facf9fc 8382+ }
8383+ err = npositive;
076b876e 8384+ if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
5afbbe0d 8385+ && au_dbtop(dentry) < 0)) {
1facf9fc 8386+ err = -EIO;
523b37e3
AM
8387+ AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8388+ dentry, err);
027c5e7a 8389+ }
1facf9fc 8390+
4f0767ce 8391+out_parent:
4a4d8108 8392+ dput(parent);
9f237c51 8393+ au_kfree_try_rcu(args.whname.name);
8b6a4947
AM
8394+ if (dirren)
8395+ au_dr_lkup_fin(&args);
4f0767ce 8396+out:
1facf9fc 8397+ return err;
8398+}
8399+
076b876e 8400+struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
1facf9fc 8401+{
8402+ struct dentry *dentry;
8403+ int wkq_err;
8404+
5527c038 8405+ if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
b4510431 8406+ dentry = vfsub_lkup_one(name, parent);
1facf9fc 8407+ else {
b4510431
AM
8408+ struct vfsub_lkup_one_args args = {
8409+ .errp = &dentry,
8410+ .name = name,
8411+ .parent = parent
1facf9fc 8412+ };
8413+
b4510431 8414+ wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
1facf9fc 8415+ if (unlikely(wkq_err))
8416+ dentry = ERR_PTR(wkq_err);
8417+ }
8418+
8419+ return dentry;
8420+}
8421+
8422+/*
8423+ * lookup @dentry on @bindex which should be negative.
8424+ */
86dc4139 8425+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
1facf9fc 8426+{
8427+ int err;
8428+ struct dentry *parent, *h_parent, *h_dentry;
86dc4139 8429+ struct au_branch *br;
1facf9fc 8430+
1facf9fc 8431+ parent = dget_parent(dentry);
8432+ h_parent = au_h_dptr(parent, bindex);
86dc4139
AM
8433+ br = au_sbr(dentry->d_sb, bindex);
8434+ if (wh)
8435+ h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
8436+ else
076b876e 8437+ h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
1facf9fc 8438+ err = PTR_ERR(h_dentry);
8439+ if (IS_ERR(h_dentry))
8440+ goto out;
5527c038 8441+ if (unlikely(d_is_positive(h_dentry))) {
1facf9fc 8442+ err = -EIO;
523b37e3 8443+ AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
1facf9fc 8444+ dput(h_dentry);
8445+ goto out;
8446+ }
8447+
4a4d8108 8448+ err = 0;
5afbbe0d
AM
8449+ if (bindex < au_dbtop(dentry))
8450+ au_set_dbtop(dentry, bindex);
8451+ if (au_dbbot(dentry) < bindex)
8452+ au_set_dbbot(dentry, bindex);
1facf9fc 8453+ au_set_h_dptr(dentry, bindex, h_dentry);
1facf9fc 8454+
4f0767ce 8455+out:
1facf9fc 8456+ dput(parent);
8457+ return err;
8458+}
8459+
8460+/* ---------------------------------------------------------------------- */
8461+
8462+/* subset of struct inode */
8463+struct au_iattr {
8464+ unsigned long i_ino;
8465+ /* unsigned int i_nlink; */
0c3ec466
AM
8466+ kuid_t i_uid;
8467+ kgid_t i_gid;
1facf9fc 8468+ u64 i_version;
8469+/*
8470+ loff_t i_size;
8471+ blkcnt_t i_blocks;
8472+*/
8473+ umode_t i_mode;
8474+};
8475+
8476+static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8477+{
8478+ ia->i_ino = h_inode->i_ino;
8479+ /* ia->i_nlink = h_inode->i_nlink; */
8480+ ia->i_uid = h_inode->i_uid;
8481+ ia->i_gid = h_inode->i_gid;
be118d29 8482+ ia->i_version = inode_query_iversion(h_inode);
1facf9fc 8483+/*
8484+ ia->i_size = h_inode->i_size;
8485+ ia->i_blocks = h_inode->i_blocks;
8486+*/
8487+ ia->i_mode = (h_inode->i_mode & S_IFMT);
8488+}
8489+
8490+static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8491+{
8492+ return ia->i_ino != h_inode->i_ino
8493+ /* || ia->i_nlink != h_inode->i_nlink */
0c3ec466 8494+ || !uid_eq(ia->i_uid, h_inode->i_uid)
2dfbb274 8495+ || !gid_eq(ia->i_gid, h_inode->i_gid)
be118d29 8496+ || !inode_eq_iversion(h_inode, ia->i_version)
1facf9fc 8497+/*
8498+ || ia->i_size != h_inode->i_size
8499+ || ia->i_blocks != h_inode->i_blocks
8500+*/
8501+ || ia->i_mode != (h_inode->i_mode & S_IFMT);
8502+}
8503+
8504+static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8505+ struct au_branch *br)
8506+{
8507+ int err;
8508+ struct au_iattr ia;
8509+ struct inode *h_inode;
8510+ struct dentry *h_d;
8511+ struct super_block *h_sb;
8512+
8513+ err = 0;
8514+ memset(&ia, -1, sizeof(ia));
8515+ h_sb = h_dentry->d_sb;
5527c038
JR
8516+ h_inode = NULL;
8517+ if (d_is_positive(h_dentry)) {
8518+ h_inode = d_inode(h_dentry);
1facf9fc 8519+ au_iattr_save(&ia, h_inode);
5527c038 8520+ } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
1facf9fc 8521+ /* nfs d_revalidate may return 0 for negative dentry */
8522+ /* fuse d_revalidate always return 0 for negative dentry */
8523+ goto out;
8524+
8525+ /* main purpose is namei.c:cached_lookup() and d_revalidate */
b4510431 8526+ h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
1facf9fc 8527+ err = PTR_ERR(h_d);
8528+ if (IS_ERR(h_d))
8529+ goto out;
8530+
8531+ err = 0;
8532+ if (unlikely(h_d != h_dentry
5527c038 8533+ || d_inode(h_d) != h_inode
1facf9fc 8534+ || (h_inode && au_iattr_test(&ia, h_inode))))
8535+ err = au_busy_or_stale();
8536+ dput(h_d);
8537+
4f0767ce 8538+out:
1facf9fc 8539+ AuTraceErr(err);
8540+ return err;
8541+}
8542+
8543+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8544+ struct dentry *h_parent, struct au_branch *br)
8545+{
8546+ int err;
8547+
8548+ err = 0;
027c5e7a
AM
8549+ if (udba == AuOpt_UDBA_REVAL
8550+ && !au_test_fs_remote(h_dentry->d_sb)) {
1facf9fc 8551+ IMustLock(h_dir);
5527c038 8552+ err = (d_inode(h_dentry->d_parent) != h_dir);
027c5e7a 8553+ } else if (udba != AuOpt_UDBA_NONE)
1facf9fc 8554+ err = au_h_verify_dentry(h_dentry, h_parent, br);
8555+
8556+ return err;
8557+}
8558+
8559+/* ---------------------------------------------------------------------- */
8560+
027c5e7a 8561+static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
1facf9fc 8562+{
027c5e7a 8563+ int err;
5afbbe0d 8564+ aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
027c5e7a
AM
8565+ struct au_hdentry tmp, *p, *q;
8566+ struct au_dinfo *dinfo;
8567+ struct super_block *sb;
1facf9fc 8568+
027c5e7a 8569+ DiMustWriteLock(dentry);
1308ab2a 8570+
027c5e7a
AM
8571+ sb = dentry->d_sb;
8572+ dinfo = au_di(dentry);
5afbbe0d 8573+ bbot = dinfo->di_bbot;
1facf9fc 8574+ bwh = dinfo->di_bwh;
8575+ bdiropq = dinfo->di_bdiropq;
5afbbe0d
AM
8576+ bindex = dinfo->di_btop;
8577+ p = au_hdentry(dinfo, bindex);
8578+ for (; bindex <= bbot; bindex++, p++) {
027c5e7a 8579+ if (!p->hd_dentry)
1facf9fc 8580+ continue;
8581+
027c5e7a
AM
8582+ new_bindex = au_br_index(sb, p->hd_id);
8583+ if (new_bindex == bindex)
1facf9fc 8584+ continue;
1facf9fc 8585+
1facf9fc 8586+ if (dinfo->di_bwh == bindex)
8587+ bwh = new_bindex;
8588+ if (dinfo->di_bdiropq == bindex)
8589+ bdiropq = new_bindex;
8590+ if (new_bindex < 0) {
8591+ au_hdput(p);
8592+ p->hd_dentry = NULL;
8593+ continue;
8594+ }
8595+
8596+ /* swap two lower dentries, and loop again */
5afbbe0d 8597+ q = au_hdentry(dinfo, new_bindex);
1facf9fc 8598+ tmp = *q;
8599+ *q = *p;
8600+ *p = tmp;
8601+ if (tmp.hd_dentry) {
8602+ bindex--;
8603+ p--;
8604+ }
8605+ }
8606+
1facf9fc 8607+ dinfo->di_bwh = -1;
5afbbe0d 8608+ if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
1facf9fc 8609+ dinfo->di_bwh = bwh;
8610+
8611+ dinfo->di_bdiropq = -1;
8612+ if (bdiropq >= 0
5afbbe0d 8613+ && bdiropq <= au_sbbot(sb)
1facf9fc 8614+ && au_sbr_whable(sb, bdiropq))
8615+ dinfo->di_bdiropq = bdiropq;
8616+
027c5e7a 8617+ err = -EIO;
5afbbe0d
AM
8618+ dinfo->di_btop = -1;
8619+ dinfo->di_bbot = -1;
8620+ bbot = au_dbbot(parent);
8621+ bindex = 0;
8622+ p = au_hdentry(dinfo, bindex);
8623+ for (; bindex <= bbot; bindex++, p++)
1facf9fc 8624+ if (p->hd_dentry) {
5afbbe0d 8625+ dinfo->di_btop = bindex;
1facf9fc 8626+ break;
8627+ }
8628+
5afbbe0d
AM
8629+ if (dinfo->di_btop >= 0) {
8630+ bindex = bbot;
8631+ p = au_hdentry(dinfo, bindex);
8632+ for (; bindex >= 0; bindex--, p--)
027c5e7a 8633+ if (p->hd_dentry) {
5afbbe0d 8634+ dinfo->di_bbot = bindex;
027c5e7a
AM
8635+ err = 0;
8636+ break;
8637+ }
8638+ }
8639+
8640+ return err;
1facf9fc 8641+}
8642+
027c5e7a 8643+static void au_do_hide(struct dentry *dentry)
1facf9fc 8644+{
027c5e7a 8645+ struct inode *inode;
1facf9fc 8646+
5527c038
JR
8647+ if (d_really_is_positive(dentry)) {
8648+ inode = d_inode(dentry);
8649+ if (!d_is_dir(dentry)) {
027c5e7a
AM
8650+ if (inode->i_nlink && !d_unhashed(dentry))
8651+ drop_nlink(inode);
8652+ } else {
8653+ clear_nlink(inode);
8654+ /* stop next lookup */
8655+ inode->i_flags |= S_DEAD;
8656+ }
8657+ smp_mb(); /* necessary? */
8658+ }
8659+ d_drop(dentry);
8660+}
1308ab2a 8661+
027c5e7a
AM
8662+static int au_hide_children(struct dentry *parent)
8663+{
8664+ int err, i, j, ndentry;
8665+ struct au_dcsub_pages dpages;
8666+ struct au_dpage *dpage;
8667+ struct dentry *dentry;
1facf9fc 8668+
027c5e7a 8669+ err = au_dpages_init(&dpages, GFP_NOFS);
1facf9fc 8670+ if (unlikely(err))
8671+ goto out;
027c5e7a
AM
8672+ err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8673+ if (unlikely(err))
8674+ goto out_dpages;
1facf9fc 8675+
027c5e7a
AM
8676+ /* in reverse order */
8677+ for (i = dpages.ndpage - 1; i >= 0; i--) {
8678+ dpage = dpages.dpages + i;
8679+ ndentry = dpage->ndentry;
8680+ for (j = ndentry - 1; j >= 0; j--) {
8681+ dentry = dpage->dentries[j];
8682+ if (dentry != parent)
8683+ au_do_hide(dentry);
8684+ }
8685+ }
1facf9fc 8686+
027c5e7a
AM
8687+out_dpages:
8688+ au_dpages_free(&dpages);
4f0767ce 8689+out:
027c5e7a 8690+ return err;
1facf9fc 8691+}
8692+
027c5e7a 8693+static void au_hide(struct dentry *dentry)
1facf9fc 8694+{
027c5e7a 8695+ int err;
1facf9fc 8696+
027c5e7a 8697+ AuDbgDentry(dentry);
2000de60 8698+ if (d_is_dir(dentry)) {
027c5e7a
AM
8699+ /* shrink_dcache_parent(dentry); */
8700+ err = au_hide_children(dentry);
8701+ if (unlikely(err))
523b37e3
AM
8702+ AuIOErr("%pd, failed hiding children, ignored %d\n",
8703+ dentry, err);
027c5e7a
AM
8704+ }
8705+ au_do_hide(dentry);
8706+}
1facf9fc 8707+
027c5e7a
AM
8708+/*
8709+ * By adding a dirty branch, a cached dentry may be affected in various ways.
8710+ *
8711+ * a dirty branch is added
8712+ * - on the top of layers
8713+ * - in the middle of layers
8714+ * - to the bottom of layers
8715+ *
8716+ * on the added branch there exists
8717+ * - a whiteout
8718+ * - a diropq
8719+ * - a same named entry
8720+ * + exist
8721+ * * negative --> positive
8722+ * * positive --> positive
8723+ * - type is unchanged
8724+ * - type is changed
8725+ * + doesn't exist
8726+ * * negative --> negative
8727+ * * positive --> negative (rejected by au_br_del() for non-dir case)
8728+ * - none
8729+ */
8730+static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8731+ struct au_dinfo *tmp)
8732+{
8733+ int err;
5afbbe0d 8734+ aufs_bindex_t bindex, bbot;
027c5e7a
AM
8735+ struct {
8736+ struct dentry *dentry;
8737+ struct inode *inode;
8738+ mode_t mode;
be52b249
AM
8739+ } orig_h, tmp_h = {
8740+ .dentry = NULL
8741+ };
027c5e7a
AM
8742+ struct au_hdentry *hd;
8743+ struct inode *inode, *h_inode;
8744+ struct dentry *h_dentry;
8745+
8746+ err = 0;
5afbbe0d 8747+ AuDebugOn(dinfo->di_btop < 0);
027c5e7a 8748+ orig_h.mode = 0;
5afbbe0d 8749+ orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
5527c038
JR
8750+ orig_h.inode = NULL;
8751+ if (d_is_positive(orig_h.dentry)) {
8752+ orig_h.inode = d_inode(orig_h.dentry);
027c5e7a 8753+ orig_h.mode = orig_h.inode->i_mode & S_IFMT;
5527c038 8754+ }
5afbbe0d
AM
8755+ if (tmp->di_btop >= 0) {
8756+ tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
5527c038
JR
8757+ if (d_is_positive(tmp_h.dentry)) {
8758+ tmp_h.inode = d_inode(tmp_h.dentry);
027c5e7a 8759+ tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
5527c038 8760+ }
027c5e7a
AM
8761+ }
8762+
5527c038
JR
8763+ inode = NULL;
8764+ if (d_really_is_positive(dentry))
8765+ inode = d_inode(dentry);
027c5e7a 8766+ if (!orig_h.inode) {
acd2b654 8767+ AuDbg("negative originally\n");
027c5e7a
AM
8768+ if (inode) {
8769+ au_hide(dentry);
8770+ goto out;
8771+ }
8772+ AuDebugOn(inode);
5afbbe0d 8773+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
027c5e7a
AM
8774+ AuDebugOn(dinfo->di_bdiropq != -1);
8775+
8776+ if (!tmp_h.inode) {
8777+ AuDbg("negative --> negative\n");
8778+ /* should have only one negative lower */
5afbbe0d
AM
8779+ if (tmp->di_btop >= 0
8780+ && tmp->di_btop < dinfo->di_btop) {
8781+ AuDebugOn(tmp->di_btop != tmp->di_bbot);
8782+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8783+ au_set_h_dptr(dentry, dinfo->di_btop, NULL);
027c5e7a 8784+ au_di_cp(dinfo, tmp);
5afbbe0d
AM
8785+ hd = au_hdentry(tmp, tmp->di_btop);
8786+ au_set_h_dptr(dentry, tmp->di_btop,
027c5e7a
AM
8787+ dget(hd->hd_dentry));
8788+ }
8789+ au_dbg_verify_dinode(dentry);
8790+ } else {
8791+ AuDbg("negative --> positive\n");
8792+ /*
8793+ * similar to the behaviour of creating with bypassing
8794+ * aufs.
8795+ * unhash it in order to force an error in the
8796+ * succeeding create operation.
8797+ * we should not set S_DEAD here.
8798+ */
8799+ d_drop(dentry);
8800+ /* au_di_swap(tmp, dinfo); */
8801+ au_dbg_verify_dinode(dentry);
8802+ }
8803+ } else {
8804+ AuDbg("positive originally\n");
8805+ /* inode may be NULL */
8806+ AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8807+ if (!tmp_h.inode) {
8808+ AuDbg("positive --> negative\n");
8809+ /* or bypassing aufs */
8810+ au_hide(dentry);
5afbbe0d 8811+ if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
027c5e7a
AM
8812+ dinfo->di_bwh = tmp->di_bwh;
8813+ if (inode)
8814+ err = au_refresh_hinode_self(inode);
8815+ au_dbg_verify_dinode(dentry);
8816+ } else if (orig_h.mode == tmp_h.mode) {
8817+ AuDbg("positive --> positive, same type\n");
8818+ if (!S_ISDIR(orig_h.mode)
5afbbe0d 8819+ && dinfo->di_btop > tmp->di_btop) {
027c5e7a
AM
8820+ /*
8821+ * similar to the behaviour of removing and
8822+ * creating.
8823+ */
8824+ au_hide(dentry);
8825+ if (inode)
8826+ err = au_refresh_hinode_self(inode);
8827+ au_dbg_verify_dinode(dentry);
8828+ } else {
8829+ /* fill empty slots */
5afbbe0d
AM
8830+ if (dinfo->di_btop > tmp->di_btop)
8831+ dinfo->di_btop = tmp->di_btop;
8832+ if (dinfo->di_bbot < tmp->di_bbot)
8833+ dinfo->di_bbot = tmp->di_bbot;
027c5e7a
AM
8834+ dinfo->di_bwh = tmp->di_bwh;
8835+ dinfo->di_bdiropq = tmp->di_bdiropq;
5afbbe0d
AM
8836+ bbot = dinfo->di_bbot;
8837+ bindex = tmp->di_btop;
8838+ hd = au_hdentry(tmp, bindex);
8839+ for (; bindex <= bbot; bindex++, hd++) {
027c5e7a
AM
8840+ if (au_h_dptr(dentry, bindex))
8841+ continue;
5afbbe0d 8842+ h_dentry = hd->hd_dentry;
027c5e7a
AM
8843+ if (!h_dentry)
8844+ continue;
5527c038
JR
8845+ AuDebugOn(d_is_negative(h_dentry));
8846+ h_inode = d_inode(h_dentry);
027c5e7a
AM
8847+ AuDebugOn(orig_h.mode
8848+ != (h_inode->i_mode
8849+ & S_IFMT));
8850+ au_set_h_dptr(dentry, bindex,
8851+ dget(h_dentry));
8852+ }
5afbbe0d
AM
8853+ if (inode)
8854+ err = au_refresh_hinode(inode, dentry);
027c5e7a
AM
8855+ au_dbg_verify_dinode(dentry);
8856+ }
8857+ } else {
8858+ AuDbg("positive --> positive, different type\n");
8859+ /* similar to the behaviour of removing and creating */
8860+ au_hide(dentry);
8861+ if (inode)
8862+ err = au_refresh_hinode_self(inode);
8863+ au_dbg_verify_dinode(dentry);
8864+ }
8865+ }
8866+
8867+out:
8868+ return err;
8869+}
8870+
79b8bda9
AM
8871+void au_refresh_dop(struct dentry *dentry, int force_reval)
8872+{
8873+ const struct dentry_operations *dop
8874+ = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8875+ static const unsigned int mask
8876+ = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8877+
8878+ BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8879+
8880+ if (dentry->d_op == dop)
8881+ return;
8882+
8883+ AuDbg("%pd\n", dentry);
8884+ spin_lock(&dentry->d_lock);
8885+ if (dop == &aufs_dop)
8886+ dentry->d_flags |= mask;
8887+ else
8888+ dentry->d_flags &= ~mask;
8889+ dentry->d_op = dop;
8890+ spin_unlock(&dentry->d_lock);
8891+}
8892+
027c5e7a
AM
8893+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8894+{
e2f27e51 8895+ int err, ebrange, nbr;
027c5e7a
AM
8896+ unsigned int sigen;
8897+ struct au_dinfo *dinfo, *tmp;
8898+ struct super_block *sb;
8899+ struct inode *inode;
8900+
8901+ DiMustWriteLock(dentry);
8902+ AuDebugOn(IS_ROOT(dentry));
5527c038 8903+ AuDebugOn(d_really_is_negative(parent));
027c5e7a
AM
8904+
8905+ sb = dentry->d_sb;
027c5e7a
AM
8906+ sigen = au_sigen(sb);
8907+ err = au_digen_test(parent, sigen);
8908+ if (unlikely(err))
8909+ goto out;
8910+
e2f27e51 8911+ nbr = au_sbbot(sb) + 1;
027c5e7a 8912+ dinfo = au_di(dentry);
e2f27e51 8913+ err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
027c5e7a
AM
8914+ if (unlikely(err))
8915+ goto out;
8916+ ebrange = au_dbrange_test(dentry);
8917+ if (!ebrange)
8918+ ebrange = au_do_refresh_hdentry(dentry, parent);
8919+
38d290e6 8920+ if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
5afbbe0d 8921+ AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
5527c038
JR
8922+ if (d_really_is_positive(dentry)) {
8923+ inode = d_inode(dentry);
027c5e7a 8924+ err = au_refresh_hinode_self(inode);
5527c038 8925+ }
027c5e7a
AM
8926+ au_dbg_verify_dinode(dentry);
8927+ if (!err)
8928+ goto out_dgen; /* success */
8929+ goto out;
8930+ }
8931+
8932+ /* temporary dinfo */
8933+ AuDbgDentry(dentry);
8934+ err = -ENOMEM;
8935+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8936+ if (unlikely(!tmp))
8937+ goto out;
8938+ au_di_swap(tmp, dinfo);
8939+ /* returns the number of positive dentries */
8940+ /*
8941+ * if current working dir is removed, it returns an error.
8942+ * but the dentry is legal.
8943+ */
5afbbe0d 8944+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
027c5e7a
AM
8945+ AuDbgDentry(dentry);
8946+ au_di_swap(tmp, dinfo);
8947+ if (err == -ENOENT)
8948+ err = 0;
8949+ if (err >= 0) {
8950+ /* compare/refresh by dinfo */
8951+ AuDbgDentry(dentry);
8952+ err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8953+ au_dbg_verify_dinode(dentry);
8954+ AuTraceErr(err);
8955+ }
e2f27e51 8956+ au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
027c5e7a
AM
8957+ au_rw_write_unlock(&tmp->di_rwsem);
8958+ au_di_free(tmp);
8959+ if (unlikely(err))
8960+ goto out;
8961+
8962+out_dgen:
8963+ au_update_digen(dentry);
8964+out:
8965+ if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
523b37e3 8966+ AuIOErr("failed refreshing %pd, %d\n", dentry, err);
027c5e7a
AM
8967+ AuDbgDentry(dentry);
8968+ }
8969+ AuTraceErr(err);
8970+ return err;
8971+}
8972+
b4510431
AM
8973+static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8974+ struct dentry *dentry, aufs_bindex_t bindex)
027c5e7a
AM
8975+{
8976+ int err, valid;
027c5e7a
AM
8977+
8978+ err = 0;
8979+ if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8980+ goto out;
027c5e7a
AM
8981+
8982+ AuDbg("b%d\n", bindex);
b4510431
AM
8983+ /*
8984+ * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8985+ * due to whiteout and branch permission.
8986+ */
8987+ flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8988+ | LOOKUP_FOLLOW | LOOKUP_EXCL);
8989+ /* it may return tri-state */
8990+ valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
1facf9fc 8991+
8992+ if (unlikely(valid < 0))
8993+ err = valid;
8994+ else if (!valid)
8995+ err = -EINVAL;
8996+
4f0767ce 8997+out:
1facf9fc 8998+ AuTraceErr(err);
8999+ return err;
9000+}
9001+
9002+/* todo: remove this */
9003+static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8b6a4947 9004+ unsigned int flags, int do_udba, int dirren)
1facf9fc 9005+{
9006+ int err;
9007+ umode_t mode, h_mode;
5afbbe0d 9008+ aufs_bindex_t bindex, btail, btop, ibs, ibe;
38d290e6 9009+ unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
4a4d8108 9010+ struct inode *h_inode, *h_cached_inode;
1facf9fc 9011+ struct dentry *h_dentry;
9012+ struct qstr *name, *h_name;
9013+
9014+ err = 0;
9015+ plus = 0;
9016+ mode = 0;
1facf9fc 9017+ ibs = -1;
9018+ ibe = -1;
9019+ unhashed = !!d_unhashed(dentry);
9020+ is_root = !!IS_ROOT(dentry);
9021+ name = &dentry->d_name;
38d290e6 9022+ tmpfile = au_di(dentry)->di_tmpfile;
1facf9fc 9023+
9024+ /*
7f207e10
AM
9025+ * Theoretically, REVAL test should be unnecessary in case of
9026+ * {FS,I}NOTIFY.
9027+ * But {fs,i}notify doesn't fire some necessary events,
1facf9fc 9028+ * IN_ATTRIB for atime/nlink/pageio
1facf9fc 9029+ * Let's do REVAL test too.
9030+ */
9031+ if (do_udba && inode) {
9032+ mode = (inode->i_mode & S_IFMT);
9033+ plus = (inode->i_nlink > 0);
5afbbe0d
AM
9034+ ibs = au_ibtop(inode);
9035+ ibe = au_ibbot(inode);
1facf9fc 9036+ }
9037+
5afbbe0d
AM
9038+ btop = au_dbtop(dentry);
9039+ btail = btop;
1facf9fc 9040+ if (inode && S_ISDIR(inode->i_mode))
9041+ btail = au_dbtaildir(dentry);
5afbbe0d 9042+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 9043+ h_dentry = au_h_dptr(dentry, bindex);
9044+ if (!h_dentry)
9045+ continue;
9046+
523b37e3
AM
9047+ AuDbg("b%d, %pd\n", bindex, h_dentry);
9048+ h_nfs = !!au_test_nfs(h_dentry->d_sb);
027c5e7a 9049+ spin_lock(&h_dentry->d_lock);
1facf9fc 9050+ h_name = &h_dentry->d_name;
9051+ if (unlikely(do_udba
9052+ && !is_root
523b37e3
AM
9053+ && ((!h_nfs
9054+ && (unhashed != !!d_unhashed(h_dentry)
8b6a4947 9055+ || (!tmpfile && !dirren
38d290e6
JR
9056+ && !au_qstreq(name, h_name))
9057+ ))
523b37e3
AM
9058+ || (h_nfs
9059+ && !(flags & LOOKUP_OPEN)
9060+ && (h_dentry->d_flags
9061+ & DCACHE_NFSFS_RENAMED)))
1facf9fc 9062+ )) {
38d290e6
JR
9063+ int h_unhashed;
9064+
9065+ h_unhashed = d_unhashed(h_dentry);
027c5e7a 9066+ spin_unlock(&h_dentry->d_lock);
38d290e6
JR
9067+ AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
9068+ unhashed, h_unhashed, dentry, h_dentry);
1facf9fc 9069+ goto err;
9070+ }
027c5e7a 9071+ spin_unlock(&h_dentry->d_lock);
1facf9fc 9072+
b4510431 9073+ err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
1facf9fc 9074+ if (unlikely(err))
9075+ /* do not goto err, to keep the errno */
9076+ break;
9077+
9078+ /* todo: plink too? */
9079+ if (!do_udba)
9080+ continue;
9081+
9082+ /* UDBA tests */
5527c038 9083+ if (unlikely(!!inode != d_is_positive(h_dentry)))
1facf9fc 9084+ goto err;
9085+
5527c038
JR
9086+ h_inode = NULL;
9087+ if (d_is_positive(h_dentry))
9088+ h_inode = d_inode(h_dentry);
1facf9fc 9089+ h_plus = plus;
9090+ h_mode = mode;
9091+ h_cached_inode = h_inode;
9092+ if (h_inode) {
9093+ h_mode = (h_inode->i_mode & S_IFMT);
9094+ h_plus = (h_inode->i_nlink > 0);
9095+ }
9096+ if (inode && ibs <= bindex && bindex <= ibe)
9097+ h_cached_inode = au_h_iptr(inode, bindex);
9098+
523b37e3 9099+ if (!h_nfs) {
38d290e6 9100+ if (unlikely(plus != h_plus && !tmpfile))
523b37e3
AM
9101+ goto err;
9102+ } else {
9103+ if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
9104+ && !is_root
9105+ && !IS_ROOT(h_dentry)
9106+ && unhashed != d_unhashed(h_dentry)))
9107+ goto err;
9108+ }
9109+ if (unlikely(mode != h_mode
1facf9fc 9110+ || h_cached_inode != h_inode))
9111+ goto err;
9112+ continue;
9113+
f6b6e03d 9114+err:
1facf9fc 9115+ err = -EINVAL;
9116+ break;
9117+ }
9118+
523b37e3 9119+ AuTraceErr(err);
1facf9fc 9120+ return err;
9121+}
9122+
027c5e7a 9123+/* todo: consolidate with do_refresh() and au_reval_for_attr() */
1facf9fc 9124+static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
9125+{
9126+ int err;
9127+ struct dentry *parent;
1facf9fc 9128+
027c5e7a 9129+ if (!au_digen_test(dentry, sigen))
1facf9fc 9130+ return 0;
9131+
9132+ parent = dget_parent(dentry);
9133+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 9134+ AuDebugOn(au_digen_test(parent, sigen));
1facf9fc 9135+ au_dbg_verify_gen(parent, sigen);
027c5e7a 9136+ err = au_refresh_dentry(dentry, parent);
1facf9fc 9137+ di_read_unlock(parent, AuLock_IR);
9138+ dput(parent);
027c5e7a 9139+ AuTraceErr(err);
1facf9fc 9140+ return err;
9141+}
9142+
9143+int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
9144+{
9145+ int err;
9146+ struct dentry *d, *parent;
1facf9fc 9147+
027c5e7a 9148+ if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
1facf9fc 9149+ return simple_reval_dpath(dentry, sigen);
9150+
9151+ /* slow loop, keep it simple and stupid */
9152+ /* cf: au_cpup_dirs() */
9153+ err = 0;
9154+ parent = NULL;
027c5e7a 9155+ while (au_digen_test(dentry, sigen)) {
1facf9fc 9156+ d = dentry;
9157+ while (1) {
9158+ dput(parent);
9159+ parent = dget_parent(d);
027c5e7a 9160+ if (!au_digen_test(parent, sigen))
1facf9fc 9161+ break;
9162+ d = parent;
9163+ }
9164+
1facf9fc 9165+ if (d != dentry)
027c5e7a 9166+ di_write_lock_child2(d);
1facf9fc 9167+
9168+ /* someone might update our dentry while we were sleeping */
027c5e7a
AM
9169+ if (au_digen_test(d, sigen)) {
9170+ /*
9171+ * todo: consolidate with simple_reval_dpath(),
9172+ * do_refresh() and au_reval_for_attr().
9173+ */
1facf9fc 9174+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 9175+ err = au_refresh_dentry(d, parent);
1facf9fc 9176+ di_read_unlock(parent, AuLock_IR);
9177+ }
9178+
9179+ if (d != dentry)
9180+ di_write_unlock(d);
9181+ dput(parent);
9182+ if (unlikely(err))
9183+ break;
9184+ }
9185+
9186+ return err;
9187+}
9188+
9189+/*
9190+ * if valid returns 1, otherwise 0.
9191+ */
b4510431 9192+static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
1facf9fc 9193+{
9194+ int valid, err;
9195+ unsigned int sigen;
8b6a4947 9196+ unsigned char do_udba, dirren;
1facf9fc 9197+ struct super_block *sb;
9198+ struct inode *inode;
9199+
027c5e7a 9200+ /* todo: support rcu-walk? */
b4510431 9201+ if (flags & LOOKUP_RCU)
027c5e7a
AM
9202+ return -ECHILD;
9203+
9204+ valid = 0;
9205+ if (unlikely(!au_di(dentry)))
9206+ goto out;
9207+
e49829fe 9208+ valid = 1;
1facf9fc 9209+ sb = dentry->d_sb;
e49829fe
JR
9210+ /*
9211+ * todo: very ugly
9212+ * i_mutex of parent dir may be held,
9213+ * but we should not return 'invalid' due to busy.
9214+ */
9215+ err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9216+ if (unlikely(err)) {
9217+ valid = err;
027c5e7a 9218+ AuTraceErr(err);
e49829fe
JR
9219+ goto out;
9220+ }
5527c038
JR
9221+ inode = NULL;
9222+ if (d_really_is_positive(dentry))
9223+ inode = d_inode(dentry);
5afbbe0d 9224+ if (unlikely(inode && au_is_bad_inode(inode))) {
c1595e42
JR
9225+ err = -EINVAL;
9226+ AuTraceErr(err);
9227+ goto out_dgrade;
9228+ }
027c5e7a
AM
9229+ if (unlikely(au_dbrange_test(dentry))) {
9230+ err = -EINVAL;
9231+ AuTraceErr(err);
9232+ goto out_dgrade;
1facf9fc 9233+ }
027c5e7a
AM
9234+
9235+ sigen = au_sigen(sb);
9236+ if (au_digen_test(dentry, sigen)) {
1facf9fc 9237+ AuDebugOn(IS_ROOT(dentry));
027c5e7a
AM
9238+ err = au_reval_dpath(dentry, sigen);
9239+ if (unlikely(err)) {
9240+ AuTraceErr(err);
1facf9fc 9241+ goto out_dgrade;
027c5e7a 9242+ }
1facf9fc 9243+ }
9244+ di_downgrade_lock(dentry, AuLock_IR);
9245+
1facf9fc 9246+ err = -EINVAL;
c1595e42 9247+ if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
523b37e3 9248+ && inode
38d290e6 9249+ && !(inode->i_state && I_LINKABLE)
79b8bda9
AM
9250+ && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9251+ AuTraceErr(err);
027c5e7a 9252+ goto out_inval;
79b8bda9 9253+ }
027c5e7a 9254+
1facf9fc 9255+ do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9256+ if (do_udba && inode) {
5afbbe0d 9257+ aufs_bindex_t btop = au_ibtop(inode);
027c5e7a 9258+ struct inode *h_inode;
1facf9fc 9259+
5afbbe0d
AM
9260+ if (btop >= 0) {
9261+ h_inode = au_h_iptr(inode, btop);
79b8bda9
AM
9262+ if (h_inode && au_test_higen(inode, h_inode)) {
9263+ AuTraceErr(err);
027c5e7a 9264+ goto out_inval;
79b8bda9 9265+ }
027c5e7a 9266+ }
1facf9fc 9267+ }
9268+
8b6a4947
AM
9269+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9270+ err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
5afbbe0d 9271+ if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
1facf9fc 9272+ err = -EIO;
523b37e3
AM
9273+ AuDbg("both of real entry and whiteout found, %p, err %d\n",
9274+ dentry, err);
027c5e7a 9275+ }
e49829fe 9276+ goto out_inval;
1facf9fc 9277+
4f0767ce 9278+out_dgrade:
1facf9fc 9279+ di_downgrade_lock(dentry, AuLock_IR);
e49829fe 9280+out_inval:
1facf9fc 9281+ aufs_read_unlock(dentry, AuLock_IR);
9282+ AuTraceErr(err);
9283+ valid = !err;
e49829fe 9284+out:
027c5e7a 9285+ if (!valid) {
523b37e3 9286+ AuDbg("%pd invalid, %d\n", dentry, valid);
027c5e7a
AM
9287+ d_drop(dentry);
9288+ }
1facf9fc 9289+ return valid;
9290+}
9291+
9292+static void aufs_d_release(struct dentry *dentry)
9293+{
027c5e7a 9294+ if (au_di(dentry)) {
4a4d8108
AM
9295+ au_di_fin(dentry);
9296+ au_hn_di_reinit(dentry);
1facf9fc 9297+ }
1facf9fc 9298+}
9299+
4a4d8108 9300+const struct dentry_operations aufs_dop = {
c06a8ce3
AM
9301+ .d_revalidate = aufs_d_revalidate,
9302+ .d_weak_revalidate = aufs_d_revalidate,
9303+ .d_release = aufs_d_release
1facf9fc 9304+};
79b8bda9
AM
9305+
9306+/* aufs_dop without d_revalidate */
9307+const struct dentry_operations aufs_dop_noreval = {
9308+ .d_release = aufs_d_release
9309+};
7f207e10
AM
9310diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9311--- /usr/share/empty/fs/aufs/dentry.h 1970-01-01 01:00:00.000000000 +0100
9f237c51
AM
9312+++ linux/fs/aufs/dentry.h 2018-12-27 13:19:17.708416053 +0100
9313@@ -0,0 +1,268 @@
062440b3 9314+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 9315+/*
b00004a5 9316+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 9317+ *
9318+ * This program, aufs is free software; you can redistribute it and/or modify
9319+ * it under the terms of the GNU General Public License as published by
9320+ * the Free Software Foundation; either version 2 of the License, or
9321+ * (at your option) any later version.
dece6358
AM
9322+ *
9323+ * This program is distributed in the hope that it will be useful,
9324+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9325+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9326+ * GNU General Public License for more details.
9327+ *
9328+ * You should have received a copy of the GNU General Public License
523b37e3 9329+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9330+ */
9331+
9332+/*
9333+ * lookup and dentry operations
9334+ */
9335+
9336+#ifndef __AUFS_DENTRY_H__
9337+#define __AUFS_DENTRY_H__
9338+
9339+#ifdef __KERNEL__
9340+
dece6358 9341+#include <linux/dcache.h>
8b6a4947 9342+#include "dirren.h"
1facf9fc 9343+#include "rwsem.h"
9344+
1facf9fc 9345+struct au_hdentry {
9346+ struct dentry *hd_dentry;
027c5e7a 9347+ aufs_bindex_t hd_id;
1facf9fc 9348+};
9349+
9350+struct au_dinfo {
9351+ atomic_t di_generation;
9352+
dece6358 9353+ struct au_rwsem di_rwsem;
5afbbe0d 9354+ aufs_bindex_t di_btop, di_bbot, di_bwh, di_bdiropq;
38d290e6 9355+ unsigned char di_tmpfile; /* to allow the different name */
1c60b727 9356+ struct au_hdentry *di_hdentry;
9f237c51 9357+ struct rcu_head rcu;
4a4d8108 9358+} ____cacheline_aligned_in_smp;
1facf9fc 9359+
9360+/* ---------------------------------------------------------------------- */
9361+
5afbbe0d
AM
9362+/* flags for au_lkup_dentry() */
9363+#define AuLkup_ALLOW_NEG 1
9364+#define AuLkup_IGNORE_PERM (1 << 1)
8b6a4947 9365+#define AuLkup_DIRREN (1 << 2)
5afbbe0d
AM
9366+#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
9367+#define au_fset_lkup(flags, name) \
9368+ do { (flags) |= AuLkup_##name; } while (0)
9369+#define au_fclr_lkup(flags, name) \
9370+ do { (flags) &= ~AuLkup_##name; } while (0)
9371+
8b6a4947
AM
9372+#ifndef CONFIG_AUFS_DIRREN
9373+#undef AuLkup_DIRREN
9374+#define AuLkup_DIRREN 0
9375+#endif
9376+
9377+struct au_do_lookup_args {
9378+ unsigned int flags;
9379+ mode_t type;
9380+ struct qstr whname, *name;
9381+ struct au_dr_lookup dirren;
9382+};
9383+
5afbbe0d
AM
9384+/* ---------------------------------------------------------------------- */
9385+
1facf9fc 9386+/* dentry.c */
79b8bda9 9387+extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
1facf9fc 9388+struct au_branch;
076b876e 9389+struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
1facf9fc 9390+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9391+ struct dentry *h_parent, struct au_branch *br);
9392+
5afbbe0d
AM
9393+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9394+ unsigned int flags);
86dc4139 9395+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
027c5e7a 9396+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
1facf9fc 9397+int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
79b8bda9 9398+void au_refresh_dop(struct dentry *dentry, int force_reval);
1facf9fc 9399+
9400+/* dinfo.c */
4a4d8108 9401+void au_di_init_once(void *_di);
027c5e7a
AM
9402+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9403+void au_di_free(struct au_dinfo *dinfo);
9404+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9405+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
4a4d8108
AM
9406+int au_di_init(struct dentry *dentry);
9407+void au_di_fin(struct dentry *dentry);
e2f27e51 9408+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
1facf9fc 9409+
9410+void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9411+void di_read_unlock(struct dentry *d, int flags);
9412+void di_downgrade_lock(struct dentry *d, int flags);
9413+void di_write_lock(struct dentry *d, unsigned int lsc);
9414+void di_write_unlock(struct dentry *d);
9415+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9416+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9417+void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9418+
9419+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
2cbb1c4b 9420+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
1facf9fc 9421+aufs_bindex_t au_dbtail(struct dentry *dentry);
9422+aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9423+
9424+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9425+ struct dentry *h_dentry);
027c5e7a
AM
9426+int au_digen_test(struct dentry *dentry, unsigned int sigen);
9427+int au_dbrange_test(struct dentry *dentry);
1facf9fc 9428+void au_update_digen(struct dentry *dentry);
9429+void au_update_dbrange(struct dentry *dentry, int do_put_zero);
5afbbe0d
AM
9430+void au_update_dbtop(struct dentry *dentry);
9431+void au_update_dbbot(struct dentry *dentry);
1facf9fc 9432+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9433+
9434+/* ---------------------------------------------------------------------- */
9435+
9436+static inline struct au_dinfo *au_di(struct dentry *dentry)
9437+{
9438+ return dentry->d_fsdata;
9439+}
9440+
9441+/* ---------------------------------------------------------------------- */
9442+
9443+/* lock subclass for dinfo */
9444+enum {
9445+ AuLsc_DI_CHILD, /* child first */
4a4d8108 9446+ AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
1facf9fc 9447+ AuLsc_DI_CHILD3, /* copyup dirs */
9448+ AuLsc_DI_PARENT,
9449+ AuLsc_DI_PARENT2,
027c5e7a
AM
9450+ AuLsc_DI_PARENT3,
9451+ AuLsc_DI_TMP /* temp for replacing dinfo */
1facf9fc 9452+};
9453+
9454+/*
9455+ * di_read_lock_child, di_write_lock_child,
9456+ * di_read_lock_child2, di_write_lock_child2,
9457+ * di_read_lock_child3, di_write_lock_child3,
9458+ * di_read_lock_parent, di_write_lock_parent,
9459+ * di_read_lock_parent2, di_write_lock_parent2,
9460+ * di_read_lock_parent3, di_write_lock_parent3,
9461+ */
9462+#define AuReadLockFunc(name, lsc) \
9463+static inline void di_read_lock_##name(struct dentry *d, int flags) \
9464+{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9465+
9466+#define AuWriteLockFunc(name, lsc) \
9467+static inline void di_write_lock_##name(struct dentry *d) \
9468+{ di_write_lock(d, AuLsc_DI_##lsc); }
9469+
9470+#define AuRWLockFuncs(name, lsc) \
9471+ AuReadLockFunc(name, lsc) \
9472+ AuWriteLockFunc(name, lsc)
9473+
9474+AuRWLockFuncs(child, CHILD);
9475+AuRWLockFuncs(child2, CHILD2);
9476+AuRWLockFuncs(child3, CHILD3);
9477+AuRWLockFuncs(parent, PARENT);
9478+AuRWLockFuncs(parent2, PARENT2);
9479+AuRWLockFuncs(parent3, PARENT3);
9480+
9481+#undef AuReadLockFunc
9482+#undef AuWriteLockFunc
9483+#undef AuRWLockFuncs
9484+
9485+#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
dece6358
AM
9486+#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
9487+#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
1facf9fc 9488+
9489+/* ---------------------------------------------------------------------- */
9490+
9491+/* todo: memory barrier? */
9492+static inline unsigned int au_digen(struct dentry *d)
9493+{
9494+ return atomic_read(&au_di(d)->di_generation);
9495+}
9496+
9497+static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9498+{
9499+ hdentry->hd_dentry = NULL;
9500+}
9501+
5afbbe0d
AM
9502+static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9503+ aufs_bindex_t bindex)
9504+{
9505+ return di->di_hdentry + bindex;
9506+}
9507+
1facf9fc 9508+static inline void au_hdput(struct au_hdentry *hd)
9509+{
4a4d8108
AM
9510+ if (hd)
9511+ dput(hd->hd_dentry);
1facf9fc 9512+}
9513+
5afbbe0d 9514+static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
1facf9fc 9515+{
1308ab2a 9516+ DiMustAnyLock(dentry);
5afbbe0d 9517+ return au_di(dentry)->di_btop;
1facf9fc 9518+}
9519+
5afbbe0d 9520+static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
1facf9fc 9521+{
1308ab2a 9522+ DiMustAnyLock(dentry);
5afbbe0d 9523+ return au_di(dentry)->di_bbot;
1facf9fc 9524+}
9525+
9526+static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9527+{
1308ab2a 9528+ DiMustAnyLock(dentry);
1facf9fc 9529+ return au_di(dentry)->di_bwh;
9530+}
9531+
9532+static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9533+{
1308ab2a 9534+ DiMustAnyLock(dentry);
1facf9fc 9535+ return au_di(dentry)->di_bdiropq;
9536+}
9537+
9538+/* todo: hard/soft set? */
5afbbe0d 9539+static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9540+{
1308ab2a 9541+ DiMustWriteLock(dentry);
5afbbe0d 9542+ au_di(dentry)->di_btop = bindex;
1facf9fc 9543+}
9544+
5afbbe0d 9545+static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9546+{
1308ab2a 9547+ DiMustWriteLock(dentry);
5afbbe0d 9548+ au_di(dentry)->di_bbot = bindex;
1facf9fc 9549+}
9550+
9551+static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9552+{
1308ab2a 9553+ DiMustWriteLock(dentry);
5afbbe0d 9554+ /* dbwh can be outside of btop - bbot range */
1facf9fc 9555+ au_di(dentry)->di_bwh = bindex;
9556+}
9557+
9558+static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9559+{
1308ab2a 9560+ DiMustWriteLock(dentry);
1facf9fc 9561+ au_di(dentry)->di_bdiropq = bindex;
9562+}
9563+
9564+/* ---------------------------------------------------------------------- */
9565+
4a4d8108 9566+#ifdef CONFIG_AUFS_HNOTIFY
1facf9fc 9567+static inline void au_digen_dec(struct dentry *d)
9568+{
e49829fe 9569+ atomic_dec(&au_di(d)->di_generation);
1facf9fc 9570+}
9571+
4a4d8108 9572+static inline void au_hn_di_reinit(struct dentry *dentry)
1facf9fc 9573+{
9574+ dentry->d_fsdata = NULL;
9575+}
9576+#else
4a4d8108
AM
9577+AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9578+#endif /* CONFIG_AUFS_HNOTIFY */
1facf9fc 9579+
9580+#endif /* __KERNEL__ */
9581+#endif /* __AUFS_DENTRY_H__ */
7f207e10
AM
9582diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9583--- /usr/share/empty/fs/aufs/dinfo.c 1970-01-01 01:00:00.000000000 +0100
9f237c51 9584+++ linux/fs/aufs/dinfo.c 2018-12-27 13:19:17.708416053 +0100
062440b3 9585@@ -0,0 +1,554 @@
cd7a4cd9 9586+// SPDX-License-Identifier: GPL-2.0
1facf9fc 9587+/*
b00004a5 9588+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 9589+ *
9590+ * This program, aufs is free software; you can redistribute it and/or modify
9591+ * it under the terms of the GNU General Public License as published by
9592+ * the Free Software Foundation; either version 2 of the License, or
9593+ * (at your option) any later version.
dece6358
AM
9594+ *
9595+ * This program is distributed in the hope that it will be useful,
9596+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9597+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9598+ * GNU General Public License for more details.
9599+ *
9600+ * You should have received a copy of the GNU General Public License
523b37e3 9601+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9602+ */
9603+
9604+/*
9605+ * dentry private data
9606+ */
9607+
9608+#include "aufs.h"
9609+
e49829fe 9610+void au_di_init_once(void *_dinfo)
4a4d8108 9611+{
e49829fe 9612+ struct au_dinfo *dinfo = _dinfo;
4a4d8108 9613+
e49829fe 9614+ au_rw_init(&dinfo->di_rwsem);
4a4d8108
AM
9615+}
9616+
027c5e7a 9617+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
1facf9fc 9618+{
9619+ struct au_dinfo *dinfo;
027c5e7a 9620+ int nbr, i;
1facf9fc 9621+
9622+ dinfo = au_cache_alloc_dinfo();
9623+ if (unlikely(!dinfo))
9624+ goto out;
9625+
5afbbe0d 9626+ nbr = au_sbbot(sb) + 1;
1facf9fc 9627+ if (nbr <= 0)
9628+ nbr = 1;
9629+ dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
027c5e7a
AM
9630+ if (dinfo->di_hdentry) {
9631+ au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
5afbbe0d
AM
9632+ dinfo->di_btop = -1;
9633+ dinfo->di_bbot = -1;
027c5e7a
AM
9634+ dinfo->di_bwh = -1;
9635+ dinfo->di_bdiropq = -1;
38d290e6 9636+ dinfo->di_tmpfile = 0;
027c5e7a
AM
9637+ for (i = 0; i < nbr; i++)
9638+ dinfo->di_hdentry[i].hd_id = -1;
9639+ goto out;
9640+ }
1facf9fc 9641+
1c60b727 9642+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9643+ dinfo = NULL;
9644+
4f0767ce 9645+out:
027c5e7a 9646+ return dinfo;
1facf9fc 9647+}
9648+
027c5e7a 9649+void au_di_free(struct au_dinfo *dinfo)
4a4d8108 9650+{
4a4d8108 9651+ struct au_hdentry *p;
5afbbe0d 9652+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
9653+
9654+ /* dentry may not be revalidated */
5afbbe0d 9655+ bindex = dinfo->di_btop;
4a4d8108 9656+ if (bindex >= 0) {
5afbbe0d
AM
9657+ bbot = dinfo->di_bbot;
9658+ p = au_hdentry(dinfo, bindex);
9659+ while (bindex++ <= bbot)
4a4d8108
AM
9660+ au_hdput(p++);
9661+ }
9f237c51 9662+ au_kfree_try_rcu(dinfo->di_hdentry);
1c60b727 9663+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9664+}
9665+
9666+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9667+{
9668+ struct au_hdentry *p;
9669+ aufs_bindex_t bi;
9670+
9671+ AuRwMustWriteLock(&a->di_rwsem);
9672+ AuRwMustWriteLock(&b->di_rwsem);
9673+
9674+#define DiSwap(v, name) \
9675+ do { \
9676+ v = a->di_##name; \
9677+ a->di_##name = b->di_##name; \
9678+ b->di_##name = v; \
9679+ } while (0)
9680+
9681+ DiSwap(p, hdentry);
5afbbe0d
AM
9682+ DiSwap(bi, btop);
9683+ DiSwap(bi, bbot);
027c5e7a
AM
9684+ DiSwap(bi, bwh);
9685+ DiSwap(bi, bdiropq);
9686+ /* smp_mb(); */
9687+
9688+#undef DiSwap
9689+}
9690+
9691+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9692+{
9693+ AuRwMustWriteLock(&dst->di_rwsem);
9694+ AuRwMustWriteLock(&src->di_rwsem);
9695+
5afbbe0d
AM
9696+ dst->di_btop = src->di_btop;
9697+ dst->di_bbot = src->di_bbot;
027c5e7a
AM
9698+ dst->di_bwh = src->di_bwh;
9699+ dst->di_bdiropq = src->di_bdiropq;
9700+ /* smp_mb(); */
9701+}
9702+
9703+int au_di_init(struct dentry *dentry)
9704+{
9705+ int err;
9706+ struct super_block *sb;
9707+ struct au_dinfo *dinfo;
9708+
9709+ err = 0;
9710+ sb = dentry->d_sb;
9711+ dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9712+ if (dinfo) {
9713+ atomic_set(&dinfo->di_generation, au_sigen(sb));
9714+ /* smp_mb(); */ /* atomic_set */
9715+ dentry->d_fsdata = dinfo;
9716+ } else
9717+ err = -ENOMEM;
9718+
9719+ return err;
9720+}
9721+
9722+void au_di_fin(struct dentry *dentry)
9723+{
9724+ struct au_dinfo *dinfo;
9725+
9726+ dinfo = au_di(dentry);
9727+ AuRwDestroy(&dinfo->di_rwsem);
9728+ au_di_free(dinfo);
4a4d8108
AM
9729+}
9730+
e2f27e51 9731+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
1facf9fc 9732+{
9733+ int err, sz;
9734+ struct au_hdentry *hdp;
9735+
1308ab2a 9736+ AuRwMustWriteLock(&dinfo->di_rwsem);
9737+
1facf9fc 9738+ err = -ENOMEM;
5afbbe0d 9739+ sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
1facf9fc 9740+ if (!sz)
9741+ sz = sizeof(*hdp);
e2f27e51
AM
9742+ hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9743+ may_shrink);
1facf9fc 9744+ if (hdp) {
9745+ dinfo->di_hdentry = hdp;
9746+ err = 0;
9747+ }
9748+
9749+ return err;
9750+}
9751+
9752+/* ---------------------------------------------------------------------- */
9753+
9754+static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9755+{
9756+ switch (lsc) {
9757+ case AuLsc_DI_CHILD:
9758+ ii_write_lock_child(inode);
9759+ break;
9760+ case AuLsc_DI_CHILD2:
9761+ ii_write_lock_child2(inode);
9762+ break;
9763+ case AuLsc_DI_CHILD3:
9764+ ii_write_lock_child3(inode);
9765+ break;
9766+ case AuLsc_DI_PARENT:
9767+ ii_write_lock_parent(inode);
9768+ break;
9769+ case AuLsc_DI_PARENT2:
9770+ ii_write_lock_parent2(inode);
9771+ break;
9772+ case AuLsc_DI_PARENT3:
9773+ ii_write_lock_parent3(inode);
9774+ break;
9775+ default:
9776+ BUG();
9777+ }
9778+}
9779+
9780+static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9781+{
9782+ switch (lsc) {
9783+ case AuLsc_DI_CHILD:
9784+ ii_read_lock_child(inode);
9785+ break;
9786+ case AuLsc_DI_CHILD2:
9787+ ii_read_lock_child2(inode);
9788+ break;
9789+ case AuLsc_DI_CHILD3:
9790+ ii_read_lock_child3(inode);
9791+ break;
9792+ case AuLsc_DI_PARENT:
9793+ ii_read_lock_parent(inode);
9794+ break;
9795+ case AuLsc_DI_PARENT2:
9796+ ii_read_lock_parent2(inode);
9797+ break;
9798+ case AuLsc_DI_PARENT3:
9799+ ii_read_lock_parent3(inode);
9800+ break;
9801+ default:
9802+ BUG();
9803+ }
9804+}
9805+
9806+void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9807+{
5527c038
JR
9808+ struct inode *inode;
9809+
dece6358 9810+ au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9811+ if (d_really_is_positive(d)) {
9812+ inode = d_inode(d);
1facf9fc 9813+ if (au_ftest_lock(flags, IW))
5527c038 9814+ do_ii_write_lock(inode, lsc);
1facf9fc 9815+ else if (au_ftest_lock(flags, IR))
5527c038 9816+ do_ii_read_lock(inode, lsc);
1facf9fc 9817+ }
9818+}
9819+
9820+void di_read_unlock(struct dentry *d, int flags)
9821+{
5527c038
JR
9822+ struct inode *inode;
9823+
9824+ if (d_really_is_positive(d)) {
9825+ inode = d_inode(d);
027c5e7a
AM
9826+ if (au_ftest_lock(flags, IW)) {
9827+ au_dbg_verify_dinode(d);
5527c038 9828+ ii_write_unlock(inode);
027c5e7a
AM
9829+ } else if (au_ftest_lock(flags, IR)) {
9830+ au_dbg_verify_dinode(d);
5527c038 9831+ ii_read_unlock(inode);
027c5e7a 9832+ }
1facf9fc 9833+ }
dece6358 9834+ au_rw_read_unlock(&au_di(d)->di_rwsem);
1facf9fc 9835+}
9836+
9837+void di_downgrade_lock(struct dentry *d, int flags)
9838+{
5527c038
JR
9839+ if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9840+ ii_downgrade_lock(d_inode(d));
dece6358 9841+ au_rw_dgrade_lock(&au_di(d)->di_rwsem);
1facf9fc 9842+}
9843+
9844+void di_write_lock(struct dentry *d, unsigned int lsc)
9845+{
dece6358 9846+ au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9847+ if (d_really_is_positive(d))
9848+ do_ii_write_lock(d_inode(d), lsc);
1facf9fc 9849+}
9850+
9851+void di_write_unlock(struct dentry *d)
9852+{
027c5e7a 9853+ au_dbg_verify_dinode(d);
5527c038
JR
9854+ if (d_really_is_positive(d))
9855+ ii_write_unlock(d_inode(d));
dece6358 9856+ au_rw_write_unlock(&au_di(d)->di_rwsem);
1facf9fc 9857+}
9858+
9859+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9860+{
9861+ AuDebugOn(d1 == d2
5527c038 9862+ || d_inode(d1) == d_inode(d2)
1facf9fc 9863+ || d1->d_sb != d2->d_sb);
9864+
521ced18
JR
9865+ if ((isdir && au_test_subdir(d1, d2))
9866+ || d1 < d2) {
1facf9fc 9867+ di_write_lock_child(d1);
9868+ di_write_lock_child2(d2);
9869+ } else {
1facf9fc 9870+ di_write_lock_child(d2);
9871+ di_write_lock_child2(d1);
9872+ }
9873+}
9874+
9875+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9876+{
9877+ AuDebugOn(d1 == d2
5527c038 9878+ || d_inode(d1) == d_inode(d2)
1facf9fc 9879+ || d1->d_sb != d2->d_sb);
9880+
521ced18
JR
9881+ if ((isdir && au_test_subdir(d1, d2))
9882+ || d1 < d2) {
1facf9fc 9883+ di_write_lock_parent(d1);
9884+ di_write_lock_parent2(d2);
9885+ } else {
1facf9fc 9886+ di_write_lock_parent(d2);
9887+ di_write_lock_parent2(d1);
9888+ }
9889+}
9890+
9891+void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9892+{
9893+ di_write_unlock(d1);
5527c038 9894+ if (d_inode(d1) == d_inode(d2))
dece6358 9895+ au_rw_write_unlock(&au_di(d2)->di_rwsem);
1facf9fc 9896+ else
9897+ di_write_unlock(d2);
9898+}
9899+
9900+/* ---------------------------------------------------------------------- */
9901+
9902+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9903+{
9904+ struct dentry *d;
9905+
1308ab2a 9906+ DiMustAnyLock(dentry);
9907+
5afbbe0d 9908+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
1facf9fc 9909+ return NULL;
9910+ AuDebugOn(bindex < 0);
5afbbe0d 9911+ d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
c1595e42 9912+ AuDebugOn(d && au_dcount(d) <= 0);
1facf9fc 9913+ return d;
9914+}
9915+
2cbb1c4b
JR
9916+/*
9917+ * extended version of au_h_dptr().
38d290e6
JR
9918+ * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9919+ * error.
2cbb1c4b
JR
9920+ */
9921+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9922+{
9923+ struct dentry *h_dentry;
9924+ struct inode *inode, *h_inode;
9925+
5527c038 9926+ AuDebugOn(d_really_is_negative(dentry));
2cbb1c4b
JR
9927+
9928+ h_dentry = NULL;
5afbbe0d
AM
9929+ if (au_dbtop(dentry) <= bindex
9930+ && bindex <= au_dbbot(dentry))
2cbb1c4b 9931+ h_dentry = au_h_dptr(dentry, bindex);
38d290e6 9932+ if (h_dentry && !au_d_linkable(h_dentry)) {
2cbb1c4b
JR
9933+ dget(h_dentry);
9934+ goto out; /* success */
9935+ }
9936+
5527c038 9937+ inode = d_inode(dentry);
5afbbe0d
AM
9938+ AuDebugOn(bindex < au_ibtop(inode));
9939+ AuDebugOn(au_ibbot(inode) < bindex);
2cbb1c4b
JR
9940+ h_inode = au_h_iptr(inode, bindex);
9941+ h_dentry = d_find_alias(h_inode);
9942+ if (h_dentry) {
9943+ if (!IS_ERR(h_dentry)) {
38d290e6 9944+ if (!au_d_linkable(h_dentry))
2cbb1c4b
JR
9945+ goto out; /* success */
9946+ dput(h_dentry);
9947+ } else
9948+ goto out;
9949+ }
9950+
9951+ if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9952+ h_dentry = au_plink_lkup(inode, bindex);
9953+ AuDebugOn(!h_dentry);
9954+ if (!IS_ERR(h_dentry)) {
9955+ if (!au_d_hashed_positive(h_dentry))
9956+ goto out; /* success */
9957+ dput(h_dentry);
9958+ h_dentry = NULL;
9959+ }
9960+ }
9961+
9962+out:
9963+ AuDbgDentry(h_dentry);
9964+ return h_dentry;
9965+}
9966+
1facf9fc 9967+aufs_bindex_t au_dbtail(struct dentry *dentry)
9968+{
5afbbe0d 9969+ aufs_bindex_t bbot, bwh;
1facf9fc 9970+
5afbbe0d
AM
9971+ bbot = au_dbbot(dentry);
9972+ if (0 <= bbot) {
1facf9fc 9973+ bwh = au_dbwh(dentry);
9974+ if (!bwh)
9975+ return bwh;
5afbbe0d 9976+ if (0 < bwh && bwh < bbot)
1facf9fc 9977+ return bwh - 1;
9978+ }
5afbbe0d 9979+ return bbot;
1facf9fc 9980+}
9981+
9982+aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9983+{
5afbbe0d 9984+ aufs_bindex_t bbot, bopq;
1facf9fc 9985+
5afbbe0d
AM
9986+ bbot = au_dbtail(dentry);
9987+ if (0 <= bbot) {
1facf9fc 9988+ bopq = au_dbdiropq(dentry);
5afbbe0d
AM
9989+ if (0 <= bopq && bopq < bbot)
9990+ bbot = bopq;
1facf9fc 9991+ }
5afbbe0d 9992+ return bbot;
1facf9fc 9993+}
9994+
9995+/* ---------------------------------------------------------------------- */
9996+
9997+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9998+ struct dentry *h_dentry)
9999+{
5afbbe0d
AM
10000+ struct au_dinfo *dinfo;
10001+ struct au_hdentry *hd;
027c5e7a 10002+ struct au_branch *br;
1facf9fc 10003+
1308ab2a 10004+ DiMustWriteLock(dentry);
10005+
5afbbe0d
AM
10006+ dinfo = au_di(dentry);
10007+ hd = au_hdentry(dinfo, bindex);
4a4d8108 10008+ au_hdput(hd);
1facf9fc 10009+ hd->hd_dentry = h_dentry;
027c5e7a
AM
10010+ if (h_dentry) {
10011+ br = au_sbr(dentry->d_sb, bindex);
10012+ hd->hd_id = br->br_id;
10013+ }
10014+}
10015+
10016+int au_dbrange_test(struct dentry *dentry)
10017+{
10018+ int err;
5afbbe0d 10019+ aufs_bindex_t btop, bbot;
027c5e7a
AM
10020+
10021+ err = 0;
5afbbe0d
AM
10022+ btop = au_dbtop(dentry);
10023+ bbot = au_dbbot(dentry);
10024+ if (btop >= 0)
10025+ AuDebugOn(bbot < 0 && btop > bbot);
027c5e7a
AM
10026+ else {
10027+ err = -EIO;
5afbbe0d 10028+ AuDebugOn(bbot >= 0);
027c5e7a
AM
10029+ }
10030+
10031+ return err;
10032+}
10033+
10034+int au_digen_test(struct dentry *dentry, unsigned int sigen)
10035+{
10036+ int err;
10037+
10038+ err = 0;
10039+ if (unlikely(au_digen(dentry) != sigen
5527c038 10040+ || au_iigen_test(d_inode(dentry), sigen)))
027c5e7a
AM
10041+ err = -EIO;
10042+
10043+ return err;
1facf9fc 10044+}
10045+
10046+void au_update_digen(struct dentry *dentry)
10047+{
10048+ atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
10049+ /* smp_mb(); */ /* atomic_set */
10050+}
10051+
10052+void au_update_dbrange(struct dentry *dentry, int do_put_zero)
10053+{
10054+ struct au_dinfo *dinfo;
10055+ struct dentry *h_d;
4a4d8108 10056+ struct au_hdentry *hdp;
5afbbe0d 10057+ aufs_bindex_t bindex, bbot;
1facf9fc 10058+
1308ab2a 10059+ DiMustWriteLock(dentry);
10060+
1facf9fc 10061+ dinfo = au_di(dentry);
5afbbe0d 10062+ if (!dinfo || dinfo->di_btop < 0)
1facf9fc 10063+ return;
10064+
10065+ if (do_put_zero) {
5afbbe0d
AM
10066+ bbot = dinfo->di_bbot;
10067+ bindex = dinfo->di_btop;
10068+ hdp = au_hdentry(dinfo, bindex);
10069+ for (; bindex <= bbot; bindex++, hdp++) {
10070+ h_d = hdp->hd_dentry;
5527c038 10071+ if (h_d && d_is_negative(h_d))
1facf9fc 10072+ au_set_h_dptr(dentry, bindex, NULL);
10073+ }
10074+ }
10075+
5afbbe0d
AM
10076+ dinfo->di_btop = 0;
10077+ hdp = au_hdentry(dinfo, dinfo->di_btop);
10078+ for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
10079+ if (hdp->hd_dentry)
1facf9fc 10080+ break;
5afbbe0d
AM
10081+ if (dinfo->di_btop > dinfo->di_bbot) {
10082+ dinfo->di_btop = -1;
10083+ dinfo->di_bbot = -1;
1facf9fc 10084+ return;
10085+ }
10086+
5afbbe0d
AM
10087+ hdp = au_hdentry(dinfo, dinfo->di_bbot);
10088+ for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
10089+ if (hdp->hd_dentry)
1facf9fc 10090+ break;
5afbbe0d 10091+ AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
1facf9fc 10092+}
10093+
5afbbe0d 10094+void au_update_dbtop(struct dentry *dentry)
1facf9fc 10095+{
5afbbe0d 10096+ aufs_bindex_t bindex, bbot;
1facf9fc 10097+ struct dentry *h_dentry;
10098+
5afbbe0d
AM
10099+ bbot = au_dbbot(dentry);
10100+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
1facf9fc 10101+ h_dentry = au_h_dptr(dentry, bindex);
10102+ if (!h_dentry)
10103+ continue;
5527c038 10104+ if (d_is_positive(h_dentry)) {
5afbbe0d 10105+ au_set_dbtop(dentry, bindex);
1facf9fc 10106+ return;
10107+ }
10108+ au_set_h_dptr(dentry, bindex, NULL);
10109+ }
10110+}
10111+
5afbbe0d 10112+void au_update_dbbot(struct dentry *dentry)
1facf9fc 10113+{
5afbbe0d 10114+ aufs_bindex_t bindex, btop;
1facf9fc 10115+ struct dentry *h_dentry;
10116+
5afbbe0d
AM
10117+ btop = au_dbtop(dentry);
10118+ for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
1facf9fc 10119+ h_dentry = au_h_dptr(dentry, bindex);
10120+ if (!h_dentry)
10121+ continue;
5527c038 10122+ if (d_is_positive(h_dentry)) {
5afbbe0d 10123+ au_set_dbbot(dentry, bindex);
1facf9fc 10124+ return;
10125+ }
10126+ au_set_h_dptr(dentry, bindex, NULL);
10127+ }
10128+}
10129+
10130+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
10131+{
5afbbe0d 10132+ aufs_bindex_t bindex, bbot;
1facf9fc 10133+
5afbbe0d
AM
10134+ bbot = au_dbbot(dentry);
10135+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
1facf9fc 10136+ if (au_h_dptr(dentry, bindex) == h_dentry)
10137+ return bindex;
10138+ return -1;
10139+}
7f207e10
AM
10140diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
10141--- /usr/share/empty/fs/aufs/dir.c 1970-01-01 01:00:00.000000000 +0100
9f237c51 10142+++ linux/fs/aufs/dir.c 2018-12-27 13:19:17.708416053 +0100
acd2b654 10143@@ -0,0 +1,762 @@
cd7a4cd9 10144+// SPDX-License-Identifier: GPL-2.0
1facf9fc 10145+/*
b00004a5 10146+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 10147+ *
10148+ * This program, aufs is free software; you can redistribute it and/or modify
10149+ * it under the terms of the GNU General Public License as published by
10150+ * the Free Software Foundation; either version 2 of the License, or
10151+ * (at your option) any later version.
dece6358
AM
10152+ *
10153+ * This program is distributed in the hope that it will be useful,
10154+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10155+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10156+ * GNU General Public License for more details.
10157+ *
10158+ * You should have received a copy of the GNU General Public License
523b37e3 10159+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10160+ */
10161+
10162+/*
10163+ * directory operations
10164+ */
10165+
10166+#include <linux/fs_stack.h>
10167+#include "aufs.h"
10168+
10169+void au_add_nlink(struct inode *dir, struct inode *h_dir)
10170+{
9dbd164d
AM
10171+ unsigned int nlink;
10172+
1facf9fc 10173+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10174+
9dbd164d
AM
10175+ nlink = dir->i_nlink;
10176+ nlink += h_dir->i_nlink - 2;
1facf9fc 10177+ if (h_dir->i_nlink < 2)
9dbd164d 10178+ nlink += 2;
f6b6e03d 10179+ smp_mb(); /* for i_nlink */
7eafdf33 10180+ /* 0 can happen in revaliding */
92d182d2 10181+ set_nlink(dir, nlink);
1facf9fc 10182+}
10183+
10184+void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10185+{
9dbd164d
AM
10186+ unsigned int nlink;
10187+
1facf9fc 10188+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10189+
9dbd164d
AM
10190+ nlink = dir->i_nlink;
10191+ nlink -= h_dir->i_nlink - 2;
1facf9fc 10192+ if (h_dir->i_nlink < 2)
9dbd164d 10193+ nlink -= 2;
f6b6e03d 10194+ smp_mb(); /* for i_nlink */
92d182d2 10195+ /* nlink == 0 means the branch-fs is broken */
9dbd164d 10196+ set_nlink(dir, nlink);
1facf9fc 10197+}
10198+
1308ab2a 10199+loff_t au_dir_size(struct file *file, struct dentry *dentry)
10200+{
10201+ loff_t sz;
5afbbe0d 10202+ aufs_bindex_t bindex, bbot;
1308ab2a 10203+ struct file *h_file;
10204+ struct dentry *h_dentry;
10205+
10206+ sz = 0;
10207+ if (file) {
2000de60 10208+ AuDebugOn(!d_is_dir(file->f_path.dentry));
1308ab2a 10209+
5afbbe0d
AM
10210+ bbot = au_fbbot_dir(file);
10211+ for (bindex = au_fbtop(file);
10212+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 10213+ bindex++) {
4a4d8108 10214+ h_file = au_hf_dir(file, bindex);
c06a8ce3
AM
10215+ if (h_file && file_inode(h_file))
10216+ sz += vfsub_f_size_read(h_file);
1308ab2a 10217+ }
10218+ } else {
10219+ AuDebugOn(!dentry);
2000de60 10220+ AuDebugOn(!d_is_dir(dentry));
1308ab2a 10221+
5afbbe0d
AM
10222+ bbot = au_dbtaildir(dentry);
10223+ for (bindex = au_dbtop(dentry);
10224+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 10225+ bindex++) {
10226+ h_dentry = au_h_dptr(dentry, bindex);
5527c038
JR
10227+ if (h_dentry && d_is_positive(h_dentry))
10228+ sz += i_size_read(d_inode(h_dentry));
1308ab2a 10229+ }
10230+ }
10231+ if (sz < KMALLOC_MAX_SIZE)
10232+ sz = roundup_pow_of_two(sz);
10233+ if (sz > KMALLOC_MAX_SIZE)
10234+ sz = KMALLOC_MAX_SIZE;
10235+ else if (sz < NAME_MAX) {
10236+ BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10237+ sz = AUFS_RDBLK_DEF;
10238+ }
10239+ return sz;
10240+}
10241+
b912730e
AM
10242+struct au_dir_ts_arg {
10243+ struct dentry *dentry;
10244+ aufs_bindex_t brid;
10245+};
10246+
10247+static void au_do_dir_ts(void *arg)
10248+{
10249+ struct au_dir_ts_arg *a = arg;
10250+ struct au_dtime dt;
10251+ struct path h_path;
10252+ struct inode *dir, *h_dir;
10253+ struct super_block *sb;
10254+ struct au_branch *br;
10255+ struct au_hinode *hdir;
10256+ int err;
5afbbe0d 10257+ aufs_bindex_t btop, bindex;
b912730e
AM
10258+
10259+ sb = a->dentry->d_sb;
5527c038 10260+ if (d_really_is_negative(a->dentry))
b912730e 10261+ goto out;
5527c038 10262+ /* no dir->i_mutex lock */
b95c5147
AM
10263+ aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10264+
5527c038 10265+ dir = d_inode(a->dentry);
5afbbe0d 10266+ btop = au_ibtop(dir);
b912730e 10267+ bindex = au_br_index(sb, a->brid);
5afbbe0d 10268+ if (bindex < btop)
b912730e
AM
10269+ goto out_unlock;
10270+
10271+ br = au_sbr(sb, bindex);
10272+ h_path.dentry = au_h_dptr(a->dentry, bindex);
10273+ if (!h_path.dentry)
10274+ goto out_unlock;
10275+ h_path.mnt = au_br_mnt(br);
10276+ au_dtime_store(&dt, a->dentry, &h_path);
10277+
5afbbe0d 10278+ br = au_sbr(sb, btop);
b912730e
AM
10279+ if (!au_br_writable(br->br_perm))
10280+ goto out_unlock;
5afbbe0d 10281+ h_path.dentry = au_h_dptr(a->dentry, btop);
b912730e
AM
10282+ h_path.mnt = au_br_mnt(br);
10283+ err = vfsub_mnt_want_write(h_path.mnt);
10284+ if (err)
10285+ goto out_unlock;
5afbbe0d
AM
10286+ hdir = au_hi(dir, btop);
10287+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10288+ h_dir = au_h_iptr(dir, btop);
b912730e 10289+ if (h_dir->i_nlink
cd7a4cd9 10290+ && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
b912730e
AM
10291+ dt.dt_h_path = h_path;
10292+ au_dtime_revert(&dt);
10293+ }
5afbbe0d 10294+ au_hn_inode_unlock(hdir);
b912730e
AM
10295+ vfsub_mnt_drop_write(h_path.mnt);
10296+ au_cpup_attr_timesizes(dir);
10297+
10298+out_unlock:
10299+ aufs_read_unlock(a->dentry, AuLock_DW);
10300+out:
10301+ dput(a->dentry);
10302+ au_nwt_done(&au_sbi(sb)->si_nowait);
9f237c51 10303+ au_kfree_try_rcu(arg);
b912730e
AM
10304+}
10305+
10306+void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10307+{
10308+ int perm, wkq_err;
5afbbe0d 10309+ aufs_bindex_t btop;
b912730e
AM
10310+ struct au_dir_ts_arg *arg;
10311+ struct dentry *dentry;
10312+ struct super_block *sb;
10313+
10314+ IMustLock(dir);
10315+
10316+ dentry = d_find_any_alias(dir);
10317+ AuDebugOn(!dentry);
10318+ sb = dentry->d_sb;
5afbbe0d
AM
10319+ btop = au_ibtop(dir);
10320+ if (btop == bindex) {
b912730e
AM
10321+ au_cpup_attr_timesizes(dir);
10322+ goto out;
10323+ }
10324+
5afbbe0d 10325+ perm = au_sbr_perm(sb, btop);
b912730e
AM
10326+ if (!au_br_writable(perm))
10327+ goto out;
10328+
10329+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
10330+ if (!arg)
10331+ goto out;
10332+
10333+ arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10334+ arg->brid = au_sbr_id(sb, bindex);
10335+ wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10336+ if (unlikely(wkq_err)) {
10337+ pr_err("wkq %d\n", wkq_err);
10338+ dput(dentry);
9f237c51 10339+ au_kfree_try_rcu(arg);
b912730e
AM
10340+ }
10341+
10342+out:
10343+ dput(dentry);
10344+}
10345+
1facf9fc 10346+/* ---------------------------------------------------------------------- */
10347+
10348+static int reopen_dir(struct file *file)
10349+{
10350+ int err;
10351+ unsigned int flags;
5afbbe0d 10352+ aufs_bindex_t bindex, btail, btop;
1facf9fc 10353+ struct dentry *dentry, *h_dentry;
10354+ struct file *h_file;
10355+
10356+ /* open all lower dirs */
2000de60 10357+ dentry = file->f_path.dentry;
5afbbe0d
AM
10358+ btop = au_dbtop(dentry);
10359+ for (bindex = au_fbtop(file); bindex < btop; bindex++)
1facf9fc 10360+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 10361+ au_set_fbtop(file, btop);
1facf9fc 10362+
10363+ btail = au_dbtaildir(dentry);
5afbbe0d 10364+ for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
1facf9fc 10365+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 10366+ au_set_fbbot_dir(file, btail);
1facf9fc 10367+
4a4d8108 10368+ flags = vfsub_file_flags(file);
5afbbe0d 10369+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 10370+ h_dentry = au_h_dptr(dentry, bindex);
10371+ if (!h_dentry)
10372+ continue;
4a4d8108 10373+ h_file = au_hf_dir(file, bindex);
1facf9fc 10374+ if (h_file)
10375+ continue;
10376+
392086de 10377+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 10378+ err = PTR_ERR(h_file);
10379+ if (IS_ERR(h_file))
10380+ goto out; /* close all? */
10381+ au_set_h_fptr(file, bindex, h_file);
10382+ }
10383+ au_update_figen(file);
10384+ /* todo: necessary? */
10385+ /* file->f_ra = h_file->f_ra; */
10386+ err = 0;
10387+
4f0767ce 10388+out:
1facf9fc 10389+ return err;
10390+}
10391+
b912730e 10392+static int do_open_dir(struct file *file, int flags, struct file *h_file)
1facf9fc 10393+{
10394+ int err;
10395+ aufs_bindex_t bindex, btail;
10396+ struct dentry *dentry, *h_dentry;
8cdd5066 10397+ struct vfsmount *mnt;
1facf9fc 10398+
1308ab2a 10399+ FiMustWriteLock(file);
b912730e 10400+ AuDebugOn(h_file);
1308ab2a 10401+
523b37e3 10402+ err = 0;
8cdd5066 10403+ mnt = file->f_path.mnt;
2000de60 10404+ dentry = file->f_path.dentry;
be118d29 10405+ file->f_version = inode_query_iversion(d_inode(dentry));
5afbbe0d
AM
10406+ bindex = au_dbtop(dentry);
10407+ au_set_fbtop(file, bindex);
1facf9fc 10408+ btail = au_dbtaildir(dentry);
5afbbe0d 10409+ au_set_fbbot_dir(file, btail);
1facf9fc 10410+ for (; !err && bindex <= btail; bindex++) {
10411+ h_dentry = au_h_dptr(dentry, bindex);
10412+ if (!h_dentry)
10413+ continue;
10414+
8cdd5066
JR
10415+ err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10416+ if (unlikely(err))
10417+ break;
392086de 10418+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 10419+ if (IS_ERR(h_file)) {
10420+ err = PTR_ERR(h_file);
10421+ break;
10422+ }
10423+ au_set_h_fptr(file, bindex, h_file);
10424+ }
10425+ au_update_figen(file);
10426+ /* todo: necessary? */
10427+ /* file->f_ra = h_file->f_ra; */
10428+ if (!err)
10429+ return 0; /* success */
10430+
10431+ /* close all */
5afbbe0d 10432+ for (bindex = au_fbtop(file); bindex <= btail; bindex++)
1facf9fc 10433+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d
AM
10434+ au_set_fbtop(file, -1);
10435+ au_set_fbbot_dir(file, -1);
4a4d8108 10436+
1facf9fc 10437+ return err;
10438+}
10439+
10440+static int aufs_open_dir(struct inode *inode __maybe_unused,
10441+ struct file *file)
10442+{
4a4d8108
AM
10443+ int err;
10444+ struct super_block *sb;
10445+ struct au_fidir *fidir;
10446+
10447+ err = -ENOMEM;
2000de60 10448+ sb = file->f_path.dentry->d_sb;
4a4d8108 10449+ si_read_lock(sb, AuLock_FLUSH);
e49829fe 10450+ fidir = au_fidir_alloc(sb);
4a4d8108 10451+ if (fidir) {
b912730e
AM
10452+ struct au_do_open_args args = {
10453+ .open = do_open_dir,
10454+ .fidir = fidir
10455+ };
10456+ err = au_do_open(file, &args);
4a4d8108 10457+ if (unlikely(err))
9f237c51 10458+ au_kfree_rcu(fidir);
4a4d8108
AM
10459+ }
10460+ si_read_unlock(sb);
10461+ return err;
1facf9fc 10462+}
10463+
10464+static int aufs_release_dir(struct inode *inode __maybe_unused,
10465+ struct file *file)
10466+{
10467+ struct au_vdir *vdir_cache;
4a4d8108
AM
10468+ struct au_finfo *finfo;
10469+ struct au_fidir *fidir;
f0c0a007 10470+ struct au_hfile *hf;
5afbbe0d 10471+ aufs_bindex_t bindex, bbot;
1facf9fc 10472+
4a4d8108
AM
10473+ finfo = au_fi(file);
10474+ fidir = finfo->fi_hdir;
10475+ if (fidir) {
8b6a4947
AM
10476+ au_hbl_del(&finfo->fi_hlist,
10477+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108
AM
10478+ vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10479+ if (vdir_cache)
1c60b727 10480+ au_vdir_free(vdir_cache);
4a4d8108
AM
10481+
10482+ bindex = finfo->fi_btop;
10483+ if (bindex >= 0) {
f0c0a007 10484+ hf = fidir->fd_hfile + bindex;
4a4d8108
AM
10485+ /*
10486+ * calls fput() instead of filp_close(),
10487+ * since no dnotify or lock for the lower file.
10488+ */
5afbbe0d 10489+ bbot = fidir->fd_bbot;
f0c0a007
AM
10490+ for (; bindex <= bbot; bindex++, hf++)
10491+ if (hf->hf_file)
1c60b727 10492+ au_hfput(hf, /*execed*/0);
4a4d8108 10493+ }
9f237c51 10494+ au_kfree_rcu(fidir);
4a4d8108 10495+ finfo->fi_hdir = NULL;
1facf9fc 10496+ }
1c60b727 10497+ au_finfo_fin(file);
1facf9fc 10498+ return 0;
10499+}
10500+
10501+/* ---------------------------------------------------------------------- */
10502+
4a4d8108
AM
10503+static int au_do_flush_dir(struct file *file, fl_owner_t id)
10504+{
10505+ int err;
5afbbe0d 10506+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
10507+ struct file *h_file;
10508+
10509+ err = 0;
5afbbe0d
AM
10510+ bbot = au_fbbot_dir(file);
10511+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
10512+ h_file = au_hf_dir(file, bindex);
10513+ if (h_file)
10514+ err = vfsub_flush(h_file, id);
10515+ }
10516+ return err;
10517+}
10518+
10519+static int aufs_flush_dir(struct file *file, fl_owner_t id)
10520+{
10521+ return au_do_flush(file, id, au_do_flush_dir);
10522+}
10523+
10524+/* ---------------------------------------------------------------------- */
10525+
1facf9fc 10526+static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10527+{
10528+ int err;
5afbbe0d 10529+ aufs_bindex_t bbot, bindex;
1facf9fc 10530+ struct inode *inode;
10531+ struct super_block *sb;
10532+
10533+ err = 0;
10534+ sb = dentry->d_sb;
5527c038 10535+ inode = d_inode(dentry);
1facf9fc 10536+ IMustLock(inode);
5afbbe0d
AM
10537+ bbot = au_dbbot(dentry);
10538+ for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
1facf9fc 10539+ struct path h_path;
1facf9fc 10540+
10541+ if (au_test_ro(sb, bindex, inode))
10542+ continue;
10543+ h_path.dentry = au_h_dptr(dentry, bindex);
10544+ if (!h_path.dentry)
10545+ continue;
1facf9fc 10546+
1facf9fc 10547+ h_path.mnt = au_sbr_mnt(sb, bindex);
53392da6 10548+ err = vfsub_fsync(NULL, &h_path, datasync);
1facf9fc 10549+ }
10550+
10551+ return err;
10552+}
10553+
10554+static int au_do_fsync_dir(struct file *file, int datasync)
10555+{
10556+ int err;
5afbbe0d 10557+ aufs_bindex_t bbot, bindex;
1facf9fc 10558+ struct file *h_file;
10559+ struct super_block *sb;
10560+ struct inode *inode;
1facf9fc 10561+
521ced18 10562+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
1facf9fc 10563+ if (unlikely(err))
10564+ goto out;
10565+
c06a8ce3 10566+ inode = file_inode(file);
b912730e 10567+ sb = inode->i_sb;
5afbbe0d
AM
10568+ bbot = au_fbbot_dir(file);
10569+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108 10570+ h_file = au_hf_dir(file, bindex);
1facf9fc 10571+ if (!h_file || au_test_ro(sb, bindex, inode))
10572+ continue;
10573+
53392da6 10574+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
1facf9fc 10575+ }
10576+
4f0767ce 10577+out:
1facf9fc 10578+ return err;
10579+}
10580+
10581+/*
10582+ * @file may be NULL
10583+ */
1e00d052
AM
10584+static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10585+ int datasync)
1facf9fc 10586+{
10587+ int err;
b752ccd1 10588+ struct dentry *dentry;
5527c038 10589+ struct inode *inode;
1facf9fc 10590+ struct super_block *sb;
1facf9fc 10591+
10592+ err = 0;
2000de60 10593+ dentry = file->f_path.dentry;
5527c038 10594+ inode = d_inode(dentry);
febd17d6 10595+ inode_lock(inode);
1facf9fc 10596+ sb = dentry->d_sb;
10597+ si_noflush_read_lock(sb);
10598+ if (file)
10599+ err = au_do_fsync_dir(file, datasync);
10600+ else {
10601+ di_write_lock_child(dentry);
10602+ err = au_do_fsync_dir_no_file(dentry, datasync);
10603+ }
5527c038 10604+ au_cpup_attr_timesizes(inode);
1facf9fc 10605+ di_write_unlock(dentry);
10606+ if (file)
10607+ fi_write_unlock(file);
10608+
10609+ si_read_unlock(sb);
febd17d6 10610+ inode_unlock(inode);
1facf9fc 10611+ return err;
10612+}
10613+
10614+/* ---------------------------------------------------------------------- */
10615+
5afbbe0d 10616+static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
1facf9fc 10617+{
10618+ int err;
10619+ struct dentry *dentry;
9dbd164d 10620+ struct inode *inode, *h_inode;
1facf9fc 10621+ struct super_block *sb;
10622+
062440b3 10623+ AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
392086de 10624+
2000de60 10625+ dentry = file->f_path.dentry;
5527c038 10626+ inode = d_inode(dentry);
1facf9fc 10627+ IMustLock(inode);
10628+
10629+ sb = dentry->d_sb;
10630+ si_read_lock(sb, AuLock_FLUSH);
521ced18 10631+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
1facf9fc 10632+ if (unlikely(err))
10633+ goto out;
027c5e7a
AM
10634+ err = au_alive_dir(dentry);
10635+ if (!err)
10636+ err = au_vdir_init(file);
1facf9fc 10637+ di_downgrade_lock(dentry, AuLock_IR);
10638+ if (unlikely(err))
10639+ goto out_unlock;
10640+
5afbbe0d 10641+ h_inode = au_h_iptr(inode, au_ibtop(inode));
b752ccd1 10642+ if (!au_test_nfsd()) {
392086de 10643+ err = au_vdir_fill_de(file, ctx);
9dbd164d 10644+ fsstack_copy_attr_atime(inode, h_inode);
1facf9fc 10645+ } else {
10646+ /*
10647+ * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10648+ * encode_fh() and others.
10649+ */
9dbd164d 10650+ atomic_inc(&h_inode->i_count);
1facf9fc 10651+ di_read_unlock(dentry, AuLock_IR);
10652+ si_read_unlock(sb);
392086de 10653+ err = au_vdir_fill_de(file, ctx);
1facf9fc 10654+ fsstack_copy_attr_atime(inode, h_inode);
10655+ fi_write_unlock(file);
9dbd164d 10656+ iput(h_inode);
1facf9fc 10657+
10658+ AuTraceErr(err);
10659+ return err;
10660+ }
10661+
4f0767ce 10662+out_unlock:
1facf9fc 10663+ di_read_unlock(dentry, AuLock_IR);
10664+ fi_write_unlock(file);
4f0767ce 10665+out:
1facf9fc 10666+ si_read_unlock(sb);
10667+ return err;
10668+}
10669+
10670+/* ---------------------------------------------------------------------- */
10671+
10672+#define AuTestEmpty_WHONLY 1
dece6358
AM
10673+#define AuTestEmpty_CALLED (1 << 1)
10674+#define AuTestEmpty_SHWH (1 << 2)
1facf9fc 10675+#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
7f207e10
AM
10676+#define au_fset_testempty(flags, name) \
10677+ do { (flags) |= AuTestEmpty_##name; } while (0)
10678+#define au_fclr_testempty(flags, name) \
10679+ do { (flags) &= ~AuTestEmpty_##name; } while (0)
1facf9fc 10680+
dece6358
AM
10681+#ifndef CONFIG_AUFS_SHWH
10682+#undef AuTestEmpty_SHWH
10683+#define AuTestEmpty_SHWH 0
10684+#endif
10685+
1facf9fc 10686+struct test_empty_arg {
392086de 10687+ struct dir_context ctx;
1308ab2a 10688+ struct au_nhash *whlist;
1facf9fc 10689+ unsigned int flags;
10690+ int err;
10691+ aufs_bindex_t bindex;
10692+};
10693+
392086de
AM
10694+static int test_empty_cb(struct dir_context *ctx, const char *__name,
10695+ int namelen, loff_t offset __maybe_unused, u64 ino,
dece6358 10696+ unsigned int d_type)
1facf9fc 10697+{
392086de
AM
10698+ struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10699+ ctx);
1facf9fc 10700+ char *name = (void *)__name;
10701+
10702+ arg->err = 0;
10703+ au_fset_testempty(arg->flags, CALLED);
10704+ /* smp_mb(); */
10705+ if (name[0] == '.'
10706+ && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10707+ goto out; /* success */
10708+
10709+ if (namelen <= AUFS_WH_PFX_LEN
10710+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10711+ if (au_ftest_testempty(arg->flags, WHONLY)
1308ab2a 10712+ && !au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10713+ arg->err = -ENOTEMPTY;
10714+ goto out;
10715+ }
10716+
10717+ name += AUFS_WH_PFX_LEN;
10718+ namelen -= AUFS_WH_PFX_LEN;
1308ab2a 10719+ if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10720+ arg->err = au_nhash_append_wh
1308ab2a 10721+ (arg->whlist, name, namelen, ino, d_type, arg->bindex,
dece6358 10722+ au_ftest_testempty(arg->flags, SHWH));
1facf9fc 10723+
4f0767ce 10724+out:
1facf9fc 10725+ /* smp_mb(); */
10726+ AuTraceErr(arg->err);
10727+ return arg->err;
10728+}
10729+
10730+static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10731+{
10732+ int err;
10733+ struct file *h_file;
acd2b654 10734+ struct au_branch *br;
1facf9fc 10735+
10736+ h_file = au_h_open(dentry, arg->bindex,
10737+ O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
392086de 10738+ /*file*/NULL, /*force_wr*/0);
1facf9fc 10739+ err = PTR_ERR(h_file);
10740+ if (IS_ERR(h_file))
10741+ goto out;
10742+
10743+ err = 0;
10744+ if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
c06a8ce3 10745+ && !file_inode(h_file)->i_nlink)
1facf9fc 10746+ goto out_put;
10747+
10748+ do {
10749+ arg->err = 0;
10750+ au_fclr_testempty(arg->flags, CALLED);
10751+ /* smp_mb(); */
392086de 10752+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1facf9fc 10753+ if (err >= 0)
10754+ err = arg->err;
10755+ } while (!err && au_ftest_testempty(arg->flags, CALLED));
10756+
4f0767ce 10757+out_put:
1facf9fc 10758+ fput(h_file);
acd2b654
AM
10759+ br = au_sbr(dentry->d_sb, arg->bindex);
10760+ au_lcnt_dec(&br->br_nfiles);
4f0767ce 10761+out:
1facf9fc 10762+ return err;
10763+}
10764+
10765+struct do_test_empty_args {
10766+ int *errp;
10767+ struct dentry *dentry;
10768+ struct test_empty_arg *arg;
10769+};
10770+
10771+static void call_do_test_empty(void *args)
10772+{
10773+ struct do_test_empty_args *a = args;
10774+ *a->errp = do_test_empty(a->dentry, a->arg);
10775+}
10776+
10777+static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10778+{
10779+ int err, wkq_err;
10780+ struct dentry *h_dentry;
10781+ struct inode *h_inode;
10782+
10783+ h_dentry = au_h_dptr(dentry, arg->bindex);
5527c038 10784+ h_inode = d_inode(h_dentry);
53392da6 10785+ /* todo: i_mode changes anytime? */
be118d29 10786+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 10787+ err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
3c1bdaff 10788+ inode_unlock_shared(h_inode);
1facf9fc 10789+ if (!err)
10790+ err = do_test_empty(dentry, arg);
10791+ else {
10792+ struct do_test_empty_args args = {
10793+ .errp = &err,
10794+ .dentry = dentry,
10795+ .arg = arg
10796+ };
10797+ unsigned int flags = arg->flags;
10798+
10799+ wkq_err = au_wkq_wait(call_do_test_empty, &args);
10800+ if (unlikely(wkq_err))
10801+ err = wkq_err;
10802+ arg->flags = flags;
10803+ }
10804+
10805+ return err;
10806+}
10807+
10808+int au_test_empty_lower(struct dentry *dentry)
10809+{
10810+ int err;
1308ab2a 10811+ unsigned int rdhash;
5afbbe0d 10812+ aufs_bindex_t bindex, btop, btail;
1308ab2a 10813+ struct au_nhash whlist;
392086de
AM
10814+ struct test_empty_arg arg = {
10815+ .ctx = {
2000de60 10816+ .actor = test_empty_cb
392086de
AM
10817+ }
10818+ };
076b876e 10819+ int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
1facf9fc 10820+
dece6358
AM
10821+ SiMustAnyLock(dentry->d_sb);
10822+
1308ab2a 10823+ rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10824+ if (!rdhash)
10825+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10826+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
dece6358 10827+ if (unlikely(err))
1facf9fc 10828+ goto out;
10829+
1facf9fc 10830+ arg.flags = 0;
1308ab2a 10831+ arg.whlist = &whlist;
5afbbe0d 10832+ btop = au_dbtop(dentry);
dece6358
AM
10833+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10834+ au_fset_testempty(arg.flags, SHWH);
076b876e
AM
10835+ test_empty = do_test_empty;
10836+ if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10837+ test_empty = sio_test_empty;
5afbbe0d 10838+ arg.bindex = btop;
076b876e 10839+ err = test_empty(dentry, &arg);
1facf9fc 10840+ if (unlikely(err))
10841+ goto out_whlist;
10842+
10843+ au_fset_testempty(arg.flags, WHONLY);
10844+ btail = au_dbtaildir(dentry);
5afbbe0d 10845+ for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
1facf9fc 10846+ struct dentry *h_dentry;
10847+
10848+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10849+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10850+ arg.bindex = bindex;
076b876e 10851+ err = test_empty(dentry, &arg);
1facf9fc 10852+ }
10853+ }
10854+
4f0767ce 10855+out_whlist:
1308ab2a 10856+ au_nhash_wh_free(&whlist);
4f0767ce 10857+out:
1facf9fc 10858+ return err;
10859+}
10860+
10861+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10862+{
10863+ int err;
392086de
AM
10864+ struct test_empty_arg arg = {
10865+ .ctx = {
2000de60 10866+ .actor = test_empty_cb
392086de
AM
10867+ }
10868+ };
1facf9fc 10869+ aufs_bindex_t bindex, btail;
10870+
10871+ err = 0;
1308ab2a 10872+ arg.whlist = whlist;
1facf9fc 10873+ arg.flags = AuTestEmpty_WHONLY;
dece6358
AM
10874+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10875+ au_fset_testempty(arg.flags, SHWH);
1facf9fc 10876+ btail = au_dbtaildir(dentry);
5afbbe0d 10877+ for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
1facf9fc 10878+ struct dentry *h_dentry;
10879+
10880+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10881+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10882+ arg.bindex = bindex;
10883+ err = sio_test_empty(dentry, &arg);
10884+ }
10885+ }
10886+
10887+ return err;
10888+}
10889+
10890+/* ---------------------------------------------------------------------- */
10891+
10892+const struct file_operations aufs_dir_fop = {
4a4d8108 10893+ .owner = THIS_MODULE,
027c5e7a 10894+ .llseek = default_llseek,
1facf9fc 10895+ .read = generic_read_dir,
5afbbe0d 10896+ .iterate_shared = aufs_iterate_shared,
1facf9fc 10897+ .unlocked_ioctl = aufs_ioctl_dir,
b752ccd1
AM
10898+#ifdef CONFIG_COMPAT
10899+ .compat_ioctl = aufs_compat_ioctl_dir,
10900+#endif
1facf9fc 10901+ .open = aufs_open_dir,
10902+ .release = aufs_release_dir,
4a4d8108 10903+ .flush = aufs_flush_dir,
1facf9fc 10904+ .fsync = aufs_fsync_dir
10905+};
7f207e10
AM
10906diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10907--- /usr/share/empty/fs/aufs/dir.h 1970-01-01 01:00:00.000000000 +0100
9f237c51
AM
10908+++ linux/fs/aufs/dir.h 2018-12-27 13:19:17.708416053 +0100
10909@@ -0,0 +1,134 @@
062440b3 10910+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 10911+/*
b00004a5 10912+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 10913+ *
10914+ * This program, aufs is free software; you can redistribute it and/or modify
10915+ * it under the terms of the GNU General Public License as published by
10916+ * the Free Software Foundation; either version 2 of the License, or
10917+ * (at your option) any later version.
dece6358
AM
10918+ *
10919+ * This program is distributed in the hope that it will be useful,
10920+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10921+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10922+ * GNU General Public License for more details.
10923+ *
10924+ * You should have received a copy of the GNU General Public License
523b37e3 10925+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10926+ */
10927+
10928+/*
10929+ * directory operations
10930+ */
10931+
10932+#ifndef __AUFS_DIR_H__
10933+#define __AUFS_DIR_H__
10934+
10935+#ifdef __KERNEL__
10936+
10937+#include <linux/fs.h>
1facf9fc 10938+
10939+/* ---------------------------------------------------------------------- */
10940+
10941+/* need to be faster and smaller */
10942+
10943+struct au_nhash {
dece6358
AM
10944+ unsigned int nh_num;
10945+ struct hlist_head *nh_head;
1facf9fc 10946+};
10947+
10948+struct au_vdir_destr {
10949+ unsigned char len;
10950+ unsigned char name[0];
10951+} __packed;
10952+
10953+struct au_vdir_dehstr {
10954+ struct hlist_node hash;
1c60b727 10955+ struct au_vdir_destr *str;
9f237c51 10956+ struct rcu_head rcu;
4a4d8108 10957+} ____cacheline_aligned_in_smp;
1facf9fc 10958+
10959+struct au_vdir_de {
10960+ ino_t de_ino;
10961+ unsigned char de_type;
10962+ /* caution: packed */
10963+ struct au_vdir_destr de_str;
10964+} __packed;
10965+
10966+struct au_vdir_wh {
10967+ struct hlist_node wh_hash;
dece6358
AM
10968+#ifdef CONFIG_AUFS_SHWH
10969+ ino_t wh_ino;
1facf9fc 10970+ aufs_bindex_t wh_bindex;
dece6358
AM
10971+ unsigned char wh_type;
10972+#else
10973+ aufs_bindex_t wh_bindex;
10974+#endif
10975+ /* caution: packed */
1facf9fc 10976+ struct au_vdir_destr wh_str;
10977+} __packed;
10978+
10979+union au_vdir_deblk_p {
10980+ unsigned char *deblk;
10981+ struct au_vdir_de *de;
10982+};
10983+
10984+struct au_vdir {
10985+ unsigned char **vd_deblk;
10986+ unsigned long vd_nblk;
1facf9fc 10987+ struct {
10988+ unsigned long ul;
10989+ union au_vdir_deblk_p p;
10990+ } vd_last;
10991+
be118d29 10992+ u64 vd_version;
dece6358 10993+ unsigned int vd_deblk_sz;
9f237c51
AM
10994+ unsigned long vd_jiffy;
10995+ struct rcu_head rcu;
4a4d8108 10996+} ____cacheline_aligned_in_smp;
1facf9fc 10997+
10998+/* ---------------------------------------------------------------------- */
10999+
11000+/* dir.c */
11001+extern const struct file_operations aufs_dir_fop;
11002+void au_add_nlink(struct inode *dir, struct inode *h_dir);
11003+void au_sub_nlink(struct inode *dir, struct inode *h_dir);
1308ab2a 11004+loff_t au_dir_size(struct file *file, struct dentry *dentry);
b912730e 11005+void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
1facf9fc 11006+int au_test_empty_lower(struct dentry *dentry);
11007+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
11008+
11009+/* vdir.c */
1308ab2a 11010+unsigned int au_rdhash_est(loff_t sz);
dece6358
AM
11011+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
11012+void au_nhash_wh_free(struct au_nhash *whlist);
1facf9fc 11013+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
11014+ int limit);
dece6358
AM
11015+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
11016+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
11017+ unsigned int d_type, aufs_bindex_t bindex,
11018+ unsigned char shwh);
1c60b727 11019+void au_vdir_free(struct au_vdir *vdir);
1facf9fc 11020+int au_vdir_init(struct file *file);
392086de 11021+int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
1facf9fc 11022+
11023+/* ioctl.c */
11024+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
11025+
1308ab2a 11026+#ifdef CONFIG_AUFS_RDU
11027+/* rdu.c */
11028+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
11029+#ifdef CONFIG_COMPAT
11030+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
11031+ unsigned long arg);
11032+#endif
1308ab2a 11033+#else
c1595e42
JR
11034+AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
11035+ unsigned int cmd, unsigned long arg)
b752ccd1 11036+#ifdef CONFIG_COMPAT
c1595e42
JR
11037+AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
11038+ unsigned int cmd, unsigned long arg)
b752ccd1 11039+#endif
1308ab2a 11040+#endif
11041+
1facf9fc 11042+#endif /* __KERNEL__ */
11043+#endif /* __AUFS_DIR_H__ */
8b6a4947
AM
11044diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
11045--- /usr/share/empty/fs/aufs/dirren.c 1970-01-01 01:00:00.000000000 +0100
9f237c51 11046+++ linux/fs/aufs/dirren.c 2018-12-27 13:19:17.708416053 +0100
062440b3 11047@@ -0,0 +1,1316 @@
cd7a4cd9 11048+// SPDX-License-Identifier: GPL-2.0
8b6a4947 11049+/*
b00004a5 11050+ * Copyright (C) 2017-2018 Junjiro R. Okajima
8b6a4947
AM
11051+ *
11052+ * This program, aufs is free software; you can redistribute it and/or modify
11053+ * it under the terms of the GNU General Public License as published by
11054+ * the Free Software Foundation; either version 2 of the License, or
11055+ * (at your option) any later version.
11056+ *
11057+ * This program is distributed in the hope that it will be useful,
11058+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11059+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11060+ * GNU General Public License for more details.
11061+ *
11062+ * You should have received a copy of the GNU General Public License
11063+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
11064+ */
11065+
11066+/*
acd2b654 11067+ * special handling in renaming a directory
8b6a4947
AM
11068+ * in order to support looking-up the before-renamed name on the lower readonly
11069+ * branches
11070+ */
11071+
11072+#include <linux/byteorder/generic.h>
11073+#include "aufs.h"
11074+
11075+static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
11076+{
11077+ int idx;
11078+
11079+ idx = au_dr_ihash(ent->dr_h_ino);
11080+ au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
11081+}
11082+
11083+static int au_dr_hino_test_empty(struct au_dr_br *dr)
11084+{
11085+ int ret, i;
11086+ struct hlist_bl_head *hbl;
11087+
11088+ ret = 1;
11089+ for (i = 0; ret && i < AuDirren_NHASH; i++) {
11090+ hbl = dr->dr_h_ino + i;
11091+ hlist_bl_lock(hbl);
11092+ ret &= hlist_bl_empty(hbl);
11093+ hlist_bl_unlock(hbl);
11094+ }
11095+
11096+ return ret;
11097+}
11098+
11099+static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
11100+{
11101+ struct au_dr_hino *found, *ent;
11102+ struct hlist_bl_head *hbl;
11103+ struct hlist_bl_node *pos;
11104+ int idx;
11105+
11106+ found = NULL;
11107+ idx = au_dr_ihash(ino);
11108+ hbl = dr->dr_h_ino + idx;
11109+ hlist_bl_lock(hbl);
11110+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11111+ if (ent->dr_h_ino == ino) {
11112+ found = ent;
11113+ break;
11114+ }
11115+ hlist_bl_unlock(hbl);
11116+
11117+ return found;
11118+}
11119+
11120+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
11121+ struct au_dr_hino *add_ent)
11122+{
11123+ int found, idx;
11124+ struct hlist_bl_head *hbl;
11125+ struct hlist_bl_node *pos;
11126+ struct au_dr_hino *ent;
11127+
11128+ found = 0;
11129+ idx = au_dr_ihash(ino);
11130+ hbl = dr->dr_h_ino + idx;
11131+#if 0
11132+ {
11133+ struct hlist_bl_node *tmp;
11134+
11135+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11136+ AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
11137+ }
11138+#endif
11139+ hlist_bl_lock(hbl);
11140+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11141+ if (ent->dr_h_ino == ino) {
11142+ found = 1;
11143+ break;
11144+ }
11145+ if (!found && add_ent)
11146+ hlist_bl_add_head(&add_ent->dr_hnode, hbl);
11147+ hlist_bl_unlock(hbl);
11148+
11149+ if (!found && add_ent)
11150+ AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
11151+
11152+ return found;
11153+}
11154+
11155+void au_dr_hino_free(struct au_dr_br *dr)
11156+{
11157+ int i;
11158+ struct hlist_bl_head *hbl;
11159+ struct hlist_bl_node *pos, *tmp;
11160+ struct au_dr_hino *ent;
11161+
11162+ /* SiMustWriteLock(sb); */
11163+
11164+ for (i = 0; i < AuDirren_NHASH; i++) {
11165+ hbl = dr->dr_h_ino + i;
11166+ /* no spinlock since sbinfo must be write-locked */
11167+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
9f237c51 11168+ au_kfree_rcu(ent);
8b6a4947
AM
11169+ INIT_HLIST_BL_HEAD(hbl);
11170+ }
11171+}
11172+
11173+/* returns the number of inodes or an error */
11174+static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
11175+ struct file *hinofile)
11176+{
11177+ int err, i;
11178+ ssize_t ssz;
11179+ loff_t pos, oldsize;
11180+ __be64 u64;
11181+ struct inode *hinoinode;
11182+ struct hlist_bl_head *hbl;
11183+ struct hlist_bl_node *n1, *n2;
11184+ struct au_dr_hino *ent;
11185+
11186+ SiMustWriteLock(sb);
11187+ AuDebugOn(!au_br_writable(br->br_perm));
11188+
11189+ hinoinode = file_inode(hinofile);
11190+ oldsize = i_size_read(hinoinode);
11191+
11192+ err = 0;
11193+ pos = 0;
11194+ hbl = br->br_dirren.dr_h_ino;
11195+ for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11196+ /* no bit-lock since sbinfo must be write-locked */
11197+ hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11198+ AuDbg("hi%llu, %pD2\n",
11199+ (unsigned long long)ent->dr_h_ino, hinofile);
11200+ u64 = cpu_to_be64(ent->dr_h_ino);
11201+ ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11202+ if (ssz == sizeof(u64))
11203+ continue;
11204+
11205+ /* write error */
11206+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11207+ err = -ENOSPC;
11208+ if (ssz < 0)
11209+ err = ssz;
11210+ break;
11211+ }
11212+ }
11213+ /* regardless the error */
11214+ if (pos < oldsize) {
11215+ err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11216+ AuTraceErr(err);
11217+ }
11218+
11219+ AuTraceErr(err);
11220+ return err;
11221+}
11222+
11223+static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11224+{
11225+ int err, hidx;
11226+ ssize_t ssz;
11227+ size_t sz, n;
11228+ loff_t pos;
11229+ uint64_t u64;
11230+ struct au_dr_hino *ent;
11231+ struct inode *hinoinode;
11232+ struct hlist_bl_head *hbl;
11233+
11234+ err = 0;
11235+ pos = 0;
11236+ hbl = dr->dr_h_ino;
11237+ hinoinode = file_inode(hinofile);
11238+ sz = i_size_read(hinoinode);
11239+ AuDebugOn(sz % sizeof(u64));
11240+ n = sz / sizeof(u64);
11241+ while (n--) {
11242+ ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11243+ if (unlikely(ssz != sizeof(u64))) {
11244+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11245+ err = -EINVAL;
11246+ if (ssz < 0)
11247+ err = ssz;
11248+ goto out_free;
11249+ }
11250+
11251+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11252+ if (!ent) {
11253+ err = -ENOMEM;
11254+ AuTraceErr(err);
11255+ goto out_free;
11256+ }
11257+ ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11258+ AuDbg("hi%llu, %pD2\n",
11259+ (unsigned long long)ent->dr_h_ino, hinofile);
11260+ hidx = au_dr_ihash(ent->dr_h_ino);
11261+ au_hbl_add(&ent->dr_hnode, hbl + hidx);
11262+ }
11263+ goto out; /* success */
11264+
11265+out_free:
11266+ au_dr_hino_free(dr);
11267+out:
11268+ AuTraceErr(err);
11269+ return err;
11270+}
11271+
11272+/*
11273+ * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11274+ * @path is a switch to distinguish load and store.
11275+ */
11276+static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11277+ struct au_branch *br, const struct path *path)
11278+{
11279+ int err, flags;
11280+ unsigned char load, suspend;
11281+ struct file *hinofile;
11282+ struct au_hinode *hdir;
11283+ struct inode *dir, *delegated;
11284+ struct path hinopath;
11285+ struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11286+ sizeof(AUFS_WH_DR_BRHINO) - 1);
11287+
11288+ AuDebugOn(bindex < 0 && !br);
11289+ AuDebugOn(bindex >= 0 && br);
11290+
11291+ err = -EINVAL;
11292+ suspend = !br;
11293+ if (suspend)
11294+ br = au_sbr(sb, bindex);
11295+ load = !!path;
11296+ if (!load) {
11297+ path = &br->br_path;
11298+ AuDebugOn(!au_br_writable(br->br_perm));
11299+ if (unlikely(!au_br_writable(br->br_perm)))
11300+ goto out;
11301+ }
11302+
11303+ hdir = NULL;
11304+ if (suspend) {
11305+ dir = d_inode(sb->s_root);
11306+ hdir = au_hinode(au_ii(dir), bindex);
11307+ dir = hdir->hi_inode;
11308+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11309+ } else {
11310+ dir = d_inode(path->dentry);
11311+ inode_lock_nested(dir, AuLsc_I_CHILD);
11312+ }
11313+ hinopath.dentry = vfsub_lkup_one(&hinoname, path->dentry);
11314+ err = PTR_ERR(hinopath.dentry);
11315+ if (IS_ERR(hinopath.dentry))
11316+ goto out_unlock;
11317+
11318+ err = 0;
11319+ flags = O_RDONLY;
11320+ if (load) {
11321+ if (d_is_negative(hinopath.dentry))
11322+ goto out_dput; /* success */
11323+ } else {
11324+ if (au_dr_hino_test_empty(&br->br_dirren)) {
11325+ if (d_is_positive(hinopath.dentry)) {
11326+ delegated = NULL;
11327+ err = vfsub_unlink(dir, &hinopath, &delegated,
11328+ /*force*/0);
11329+ AuTraceErr(err);
11330+ if (unlikely(err))
11331+ pr_err("ignored err %d, %pd2\n",
11332+ err, hinopath.dentry);
11333+ if (unlikely(err == -EWOULDBLOCK))
11334+ iput(delegated);
11335+ err = 0;
11336+ }
11337+ goto out_dput;
11338+ } else if (!d_is_positive(hinopath.dentry)) {
11339+ err = vfsub_create(dir, &hinopath, 0600,
11340+ /*want_excl*/false);
11341+ AuTraceErr(err);
11342+ if (unlikely(err))
11343+ goto out_dput;
11344+ }
11345+ flags = O_WRONLY;
11346+ }
11347+ hinopath.mnt = path->mnt;
11348+ hinofile = vfsub_dentry_open(&hinopath, flags);
11349+ if (suspend)
11350+ au_hn_inode_unlock(hdir);
11351+ else
11352+ inode_unlock(dir);
11353+ dput(hinopath.dentry);
11354+ AuTraceErrPtr(hinofile);
11355+ if (IS_ERR(hinofile)) {
11356+ err = PTR_ERR(hinofile);
11357+ goto out;
11358+ }
11359+
11360+ if (load)
11361+ err = au_dr_hino_load(&br->br_dirren, hinofile);
11362+ else
11363+ err = au_dr_hino_store(sb, br, hinofile);
11364+ fput(hinofile);
11365+ goto out;
11366+
11367+out_dput:
11368+ dput(hinopath.dentry);
11369+out_unlock:
11370+ if (suspend)
11371+ au_hn_inode_unlock(hdir);
11372+ else
11373+ inode_unlock(dir);
11374+out:
11375+ AuTraceErr(err);
11376+ return err;
11377+}
11378+
11379+/* ---------------------------------------------------------------------- */
11380+
11381+static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11382+{
11383+ int err;
11384+ struct kstatfs kstfs;
11385+ dev_t dev;
11386+ struct dentry *dentry;
11387+ struct super_block *sb;
11388+
11389+ err = vfs_statfs((void *)path, &kstfs);
11390+ AuTraceErr(err);
11391+ if (unlikely(err))
11392+ goto out;
11393+
11394+ /* todo: support for UUID */
11395+
11396+ if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11397+ brid->type = AuBrid_FSID;
11398+ brid->fsid = kstfs.f_fsid;
11399+ } else {
11400+ dentry = path->dentry;
11401+ sb = dentry->d_sb;
11402+ dev = sb->s_dev;
11403+ if (dev) {
11404+ brid->type = AuBrid_DEV;
11405+ brid->dev = dev;
11406+ }
11407+ }
11408+
11409+out:
11410+ return err;
11411+}
11412+
11413+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11414+ const struct path *path)
11415+{
11416+ int err, i;
11417+ struct au_dr_br *dr;
11418+ struct hlist_bl_head *hbl;
11419+
11420+ dr = &br->br_dirren;
11421+ hbl = dr->dr_h_ino;
11422+ for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11423+ INIT_HLIST_BL_HEAD(hbl);
11424+
11425+ err = au_dr_brid_init(&dr->dr_brid, path);
11426+ if (unlikely(err))
11427+ goto out;
11428+
11429+ if (au_opt_test(au_mntflags(sb), DIRREN))
11430+ err = au_dr_hino(sb, /*bindex*/-1, br, path);
11431+
11432+out:
11433+ AuTraceErr(err);
11434+ return err;
11435+}
11436+
11437+int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11438+{
11439+ int err;
11440+
11441+ err = 0;
11442+ if (au_br_writable(br->br_perm))
11443+ err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11444+ if (!err)
11445+ au_dr_hino_free(&br->br_dirren);
11446+
11447+ return err;
11448+}
11449+
11450+/* ---------------------------------------------------------------------- */
11451+
11452+static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11453+ char *buf, size_t sz)
11454+{
11455+ int err;
11456+ unsigned int major, minor;
11457+ char *p;
11458+
11459+ p = buf;
11460+ err = snprintf(p, sz, "%d_", brid->type);
11461+ AuDebugOn(err > sz);
11462+ p += err;
11463+ sz -= err;
11464+ switch (brid->type) {
11465+ case AuBrid_Unset:
11466+ return -EINVAL;
11467+ case AuBrid_UUID:
11468+ err = snprintf(p, sz, "%pU", brid->uuid.b);
11469+ break;
11470+ case AuBrid_FSID:
11471+ err = snprintf(p, sz, "%08x-%08x",
11472+ brid->fsid.val[0], brid->fsid.val[1]);
11473+ break;
11474+ case AuBrid_DEV:
11475+ major = MAJOR(brid->dev);
11476+ minor = MINOR(brid->dev);
11477+ if (major <= 0xff && minor <= 0xff)
11478+ err = snprintf(p, sz, "%02x%02x", major, minor);
11479+ else
11480+ err = snprintf(p, sz, "%03x:%05x", major, minor);
11481+ break;
11482+ }
11483+ AuDebugOn(err > sz);
11484+ p += err;
11485+ sz -= err;
11486+ err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11487+ AuDebugOn(err > sz);
11488+ p += err;
11489+ sz -= err;
11490+
11491+ return p - buf;
11492+}
11493+
11494+static int au_drinfo_name(struct au_branch *br, char *name, int len)
11495+{
11496+ int rlen;
11497+ struct dentry *br_dentry;
11498+ struct inode *br_inode;
11499+
11500+ br_dentry = au_br_dentry(br);
11501+ br_inode = d_inode(br_dentry);
11502+ rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11503+ AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11504+ AuDebugOn(rlen > len);
11505+
11506+ return rlen;
11507+}
11508+
11509+/* ---------------------------------------------------------------------- */
11510+
11511+/*
11512+ * from the given @h_dentry, construct drinfo at @*fdata.
11513+ * when the size of @*fdata is not enough, reallocate and return new @fdata and
11514+ * @allocated.
11515+ */
11516+static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11517+ struct dentry *h_dentry,
11518+ unsigned char *allocated)
11519+{
11520+ int err, v;
11521+ struct au_drinfo_fdata *f, *p;
11522+ struct au_drinfo *drinfo;
11523+ struct inode *h_inode;
11524+ struct qstr *qname;
11525+
11526+ err = 0;
11527+ f = *fdata;
11528+ h_inode = d_inode(h_dentry);
11529+ qname = &h_dentry->d_name;
11530+ drinfo = &f->drinfo;
11531+ drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11532+ drinfo->oldnamelen = qname->len;
11533+ if (*allocated < sizeof(*f) + qname->len) {
11534+ v = roundup_pow_of_two(*allocated + qname->len);
11535+ p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11536+ if (unlikely(!p)) {
11537+ err = -ENOMEM;
11538+ AuTraceErr(err);
11539+ goto out;
11540+ }
11541+ f = p;
11542+ *fdata = f;
11543+ *allocated = v;
11544+ drinfo = &f->drinfo;
11545+ }
11546+ memcpy(drinfo->oldname, qname->name, qname->len);
11547+ AuDbg("i%llu, %.*s\n",
11548+ be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11549+ drinfo->oldname);
11550+
11551+out:
11552+ AuTraceErr(err);
11553+ return err;
11554+}
11555+
11556+/* callers have to free the return value */
11557+static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11558+{
11559+ struct au_drinfo *ret, *drinfo;
11560+ struct au_drinfo_fdata fdata;
11561+ int len;
11562+ loff_t pos;
11563+ ssize_t ssz;
11564+
11565+ ret = ERR_PTR(-EIO);
11566+ pos = 0;
11567+ ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11568+ if (unlikely(ssz != sizeof(fdata))) {
11569+ AuIOErr("ssz %zd, %u, %pD2\n",
11570+ ssz, (unsigned int)sizeof(fdata), file);
11571+ goto out;
11572+ }
11573+
11574+ fdata.magic = ntohl((__force __be32)fdata.magic);
11575+ switch (fdata.magic) {
11576+ case AUFS_DRINFO_MAGIC_V1:
11577+ break;
11578+ default:
11579+ AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11580+ fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11581+ goto out;
11582+ }
11583+
11584+ drinfo = &fdata.drinfo;
11585+ len = drinfo->oldnamelen;
11586+ if (!len) {
11587+ AuIOErr("broken drinfo %pD2\n", file);
11588+ goto out;
11589+ }
11590+
11591+ ret = NULL;
11592+ drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11593+ if (unlikely(h_ino && drinfo->ino != h_ino)) {
11594+ AuDbg("ignored i%llu, i%llu, %pD2\n",
11595+ (unsigned long long)drinfo->ino,
11596+ (unsigned long long)h_ino, file);
11597+ goto out; /* success */
11598+ }
11599+
11600+ ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11601+ if (unlikely(!ret)) {
11602+ ret = ERR_PTR(-ENOMEM);
11603+ AuTraceErrPtr(ret);
11604+ goto out;
11605+ }
11606+
11607+ *ret = *drinfo;
11608+ ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11609+ if (unlikely(ssz != len)) {
9f237c51 11610+ au_kfree_rcu(ret);
8b6a4947
AM
11611+ ret = ERR_PTR(-EIO);
11612+ AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11613+ goto out;
11614+ }
11615+
11616+ AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11617+
11618+out:
11619+ return ret;
11620+}
11621+
11622+/* ---------------------------------------------------------------------- */
11623+
11624+/* in order to be revertible */
11625+struct au_drinfo_rev_elm {
11626+ int created;
11627+ struct dentry *info_dentry;
11628+ struct au_drinfo *info_last;
11629+};
11630+
11631+struct au_drinfo_rev {
11632+ unsigned char already;
11633+ aufs_bindex_t nelm;
11634+ struct au_drinfo_rev_elm elm[0];
11635+};
11636+
11637+/* todo: isn't it too large? */
11638+struct au_drinfo_store {
11639+ struct path h_ppath;
11640+ struct dentry *h_dentry;
11641+ struct au_drinfo_fdata *fdata;
11642+ char *infoname; /* inside of whname, just after PFX */
11643+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11644+ aufs_bindex_t btgt, btail;
11645+ unsigned char no_sio,
11646+ allocated, /* current size of *fdata */
11647+ infonamelen, /* room size for p */
acd2b654 11648+ whnamelen, /* length of the generated name */
8b6a4947
AM
11649+ renameback; /* renamed back */
11650+};
11651+
11652+/* on rename(2) error, the caller should revert it using @elm */
11653+static int au_drinfo_do_store(struct au_drinfo_store *w,
11654+ struct au_drinfo_rev_elm *elm)
11655+{
11656+ int err, len;
11657+ ssize_t ssz;
11658+ loff_t pos;
11659+ struct path infopath = {
11660+ .mnt = w->h_ppath.mnt
11661+ };
11662+ struct inode *h_dir, *h_inode, *delegated;
11663+ struct file *infofile;
11664+ struct qstr *qname;
11665+
11666+ AuDebugOn(elm
11667+ && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11668+
11669+ infopath.dentry = vfsub_lookup_one_len(w->whname, w->h_ppath.dentry,
11670+ w->whnamelen);
11671+ AuTraceErrPtr(infopath.dentry);
11672+ if (IS_ERR(infopath.dentry)) {
11673+ err = PTR_ERR(infopath.dentry);
11674+ goto out;
11675+ }
11676+
11677+ err = 0;
11678+ h_dir = d_inode(w->h_ppath.dentry);
11679+ if (elm && d_is_negative(infopath.dentry)) {
11680+ err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11681+ AuTraceErr(err);
11682+ if (unlikely(err))
11683+ goto out_dput;
11684+ elm->created = 1;
11685+ elm->info_dentry = dget(infopath.dentry);
11686+ }
11687+
11688+ infofile = vfsub_dentry_open(&infopath, O_RDWR);
11689+ AuTraceErrPtr(infofile);
11690+ if (IS_ERR(infofile)) {
11691+ err = PTR_ERR(infofile);
11692+ goto out_dput;
11693+ }
11694+
11695+ h_inode = d_inode(infopath.dentry);
11696+ if (elm && i_size_read(h_inode)) {
11697+ h_inode = d_inode(w->h_dentry);
11698+ elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11699+ AuTraceErrPtr(elm->info_last);
11700+ if (IS_ERR(elm->info_last)) {
11701+ err = PTR_ERR(elm->info_last);
11702+ elm->info_last = NULL;
11703+ AuDebugOn(elm->info_dentry);
11704+ goto out_fput;
11705+ }
11706+ }
11707+
11708+ if (elm && w->renameback) {
11709+ delegated = NULL;
11710+ err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11711+ AuTraceErr(err);
11712+ if (unlikely(err == -EWOULDBLOCK))
11713+ iput(delegated);
11714+ goto out_fput;
11715+ }
11716+
11717+ pos = 0;
11718+ qname = &w->h_dentry->d_name;
11719+ len = sizeof(*w->fdata) + qname->len;
11720+ if (!elm)
11721+ len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11722+ ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11723+ if (ssz == len) {
11724+ AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11725+ w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11726+ goto out_fput; /* success */
11727+ } else {
11728+ err = -EIO;
11729+ if (ssz < 0)
11730+ err = ssz;
11731+ /* the caller should revert it using @elm */
11732+ }
11733+
11734+out_fput:
11735+ fput(infofile);
11736+out_dput:
11737+ dput(infopath.dentry);
11738+out:
11739+ AuTraceErr(err);
11740+ return err;
11741+}
11742+
11743+struct au_call_drinfo_do_store_args {
11744+ int *errp;
11745+ struct au_drinfo_store *w;
11746+ struct au_drinfo_rev_elm *elm;
11747+};
11748+
11749+static void au_call_drinfo_do_store(void *args)
11750+{
11751+ struct au_call_drinfo_do_store_args *a = args;
11752+
11753+ *a->errp = au_drinfo_do_store(a->w, a->elm);
11754+}
11755+
11756+static int au_drinfo_store_sio(struct au_drinfo_store *w,
11757+ struct au_drinfo_rev_elm *elm)
11758+{
11759+ int err, wkq_err;
11760+
11761+ if (w->no_sio)
11762+ err = au_drinfo_do_store(w, elm);
11763+ else {
11764+ struct au_call_drinfo_do_store_args a = {
11765+ .errp = &err,
11766+ .w = w,
11767+ .elm = elm
11768+ };
11769+ wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11770+ if (unlikely(wkq_err))
11771+ err = wkq_err;
11772+ }
11773+ AuTraceErr(err);
11774+
11775+ return err;
11776+}
11777+
11778+static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11779+ aufs_bindex_t btgt)
11780+{
11781+ int err;
11782+
11783+ memset(w, 0, sizeof(*w));
11784+ w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11785+ strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11786+ w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11787+ w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11788+ w->btgt = btgt;
11789+ w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11790+
11791+ err = -ENOMEM;
11792+ w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11793+ if (unlikely(!w->fdata)) {
11794+ AuTraceErr(err);
11795+ goto out;
11796+ }
11797+ w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11798+ err = 0;
11799+
11800+out:
11801+ return err;
11802+}
11803+
11804+static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11805+{
9f237c51 11806+ au_kfree_rcu(w->fdata);
8b6a4947
AM
11807+}
11808+
11809+static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11810+ struct au_drinfo_store *w)
11811+{
11812+ struct au_drinfo_rev_elm *elm;
11813+ struct inode *h_dir, *delegated;
11814+ int err, nelm;
11815+ struct path infopath = {
11816+ .mnt = w->h_ppath.mnt
11817+ };
11818+
11819+ h_dir = d_inode(w->h_ppath.dentry);
11820+ IMustLock(h_dir);
11821+
11822+ err = 0;
11823+ elm = rev->elm;
11824+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11825+ AuDebugOn(elm->created && elm->info_last);
11826+ if (elm->created) {
11827+ AuDbg("here\n");
11828+ delegated = NULL;
11829+ infopath.dentry = elm->info_dentry;
11830+ err = vfsub_unlink(h_dir, &infopath, &delegated,
11831+ !w->no_sio);
11832+ AuTraceErr(err);
11833+ if (unlikely(err == -EWOULDBLOCK))
11834+ iput(delegated);
11835+ dput(elm->info_dentry);
11836+ } else if (elm->info_last) {
11837+ AuDbg("here\n");
11838+ w->fdata->drinfo = *elm->info_last;
11839+ memcpy(w->fdata->drinfo.oldname,
11840+ elm->info_last->oldname,
11841+ elm->info_last->oldnamelen);
11842+ err = au_drinfo_store_sio(w, /*elm*/NULL);
9f237c51 11843+ au_kfree_rcu(elm->info_last);
8b6a4947
AM
11844+ }
11845+ if (unlikely(err))
11846+ AuIOErr("%d, %s\n", err, w->whname);
11847+ /* go on even if err */
11848+ }
11849+}
11850+
11851+/* caller has to call au_dr_rename_fin() later */
11852+static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11853+ struct qstr *dst_name, void *_rev)
11854+{
11855+ int err, sz, nelm;
11856+ aufs_bindex_t bindex, btail;
11857+ struct au_drinfo_store work;
11858+ struct au_drinfo_rev *rev, **p;
11859+ struct au_drinfo_rev_elm *elm;
11860+ struct super_block *sb;
11861+ struct au_branch *br;
11862+ struct au_hinode *hdir;
11863+
11864+ err = au_drinfo_store_work_init(&work, btgt);
11865+ AuTraceErr(err);
11866+ if (unlikely(err))
11867+ goto out;
11868+
11869+ err = -ENOMEM;
11870+ btail = au_dbtaildir(dentry);
11871+ nelm = btail - btgt;
11872+ sz = sizeof(*rev) + sizeof(*elm) * nelm;
11873+ rev = kcalloc(1, sz, GFP_NOFS);
11874+ if (unlikely(!rev)) {
11875+ AuTraceErr(err);
11876+ goto out_args;
11877+ }
11878+ rev->nelm = nelm;
11879+ elm = rev->elm;
11880+ p = _rev;
11881+ *p = rev;
11882+
11883+ err = 0;
11884+ sb = dentry->d_sb;
11885+ work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11886+ work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11887+ hdir = au_hi(d_inode(dentry), btgt);
11888+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11889+ for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11890+ work.h_dentry = au_h_dptr(dentry, bindex);
11891+ if (!work.h_dentry)
11892+ continue;
11893+
11894+ err = au_drinfo_construct(&work.fdata, work.h_dentry,
11895+ &work.allocated);
11896+ AuTraceErr(err);
11897+ if (unlikely(err))
11898+ break;
11899+
11900+ work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11901+ br = au_sbr(sb, bindex);
11902+ work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11903+ work.whnamelen += au_drinfo_name(br, work.infoname,
11904+ work.infonamelen);
11905+ AuDbg("whname %.*s, i%llu, %.*s\n",
11906+ work.whnamelen, work.whname,
11907+ be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11908+ work.fdata->drinfo.oldnamelen,
11909+ work.fdata->drinfo.oldname);
11910+
11911+ err = au_drinfo_store_sio(&work, elm);
11912+ AuTraceErr(err);
11913+ if (unlikely(err))
11914+ break;
11915+ }
11916+ if (unlikely(err)) {
11917+ /* revert all drinfo */
11918+ au_drinfo_store_rev(rev, &work);
9f237c51 11919+ au_kfree_try_rcu(rev);
8b6a4947
AM
11920+ *p = NULL;
11921+ }
11922+ au_hn_inode_unlock(hdir);
11923+
11924+out_args:
11925+ au_drinfo_store_work_fin(&work);
11926+out:
11927+ return err;
11928+}
11929+
11930+/* ---------------------------------------------------------------------- */
11931+
11932+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11933+ struct qstr *dst_name, void *_rev)
11934+{
11935+ int err, already;
11936+ ino_t ino;
11937+ struct super_block *sb;
11938+ struct au_branch *br;
11939+ struct au_dr_br *dr;
11940+ struct dentry *h_dentry;
11941+ struct inode *h_inode;
11942+ struct au_dr_hino *ent;
11943+ struct au_drinfo_rev *rev, **p;
11944+
11945+ AuDbg("bindex %d\n", bindex);
11946+
11947+ err = -ENOMEM;
11948+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11949+ if (unlikely(!ent))
11950+ goto out;
11951+
11952+ sb = src->d_sb;
11953+ br = au_sbr(sb, bindex);
11954+ dr = &br->br_dirren;
11955+ h_dentry = au_h_dptr(src, bindex);
11956+ h_inode = d_inode(h_dentry);
11957+ ino = h_inode->i_ino;
11958+ ent->dr_h_ino = ino;
11959+ already = au_dr_hino_test_add(dr, ino, ent);
11960+ AuDbg("b%d, hi%llu, already %d\n",
11961+ bindex, (unsigned long long)ino, already);
11962+
11963+ err = au_drinfo_store(src, bindex, dst_name, _rev);
11964+ AuTraceErr(err);
11965+ if (!err) {
11966+ p = _rev;
11967+ rev = *p;
11968+ rev->already = already;
11969+ goto out; /* success */
11970+ }
11971+
11972+ /* revert */
11973+ if (!already)
11974+ au_dr_hino_del(dr, ent);
9f237c51 11975+ au_kfree_rcu(ent);
8b6a4947
AM
11976+
11977+out:
11978+ AuTraceErr(err);
11979+ return err;
11980+}
11981+
11982+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11983+{
11984+ struct au_drinfo_rev *rev;
11985+ struct au_drinfo_rev_elm *elm;
11986+ int nelm;
11987+
11988+ rev = _rev;
11989+ elm = rev->elm;
11990+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11991+ dput(elm->info_dentry);
9f237c51 11992+ au_kfree_rcu(elm->info_last);
8b6a4947 11993+ }
9f237c51 11994+ au_kfree_try_rcu(rev);
8b6a4947
AM
11995+}
11996+
11997+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11998+{
11999+ int err;
12000+ struct au_drinfo_store work;
12001+ struct au_drinfo_rev *rev = _rev;
12002+ struct super_block *sb;
12003+ struct au_branch *br;
12004+ struct inode *h_inode;
12005+ struct au_dr_br *dr;
12006+ struct au_dr_hino *ent;
12007+
12008+ err = au_drinfo_store_work_init(&work, btgt);
12009+ if (unlikely(err))
12010+ goto out;
12011+
12012+ sb = src->d_sb;
12013+ br = au_sbr(sb, btgt);
12014+ work.h_ppath.dentry = au_h_dptr(src, btgt);
12015+ work.h_ppath.mnt = au_br_mnt(br);
12016+ au_drinfo_store_rev(rev, &work);
12017+ au_drinfo_store_work_fin(&work);
12018+ if (rev->already)
12019+ goto out;
12020+
12021+ dr = &br->br_dirren;
12022+ h_inode = d_inode(work.h_ppath.dentry);
12023+ ent = au_dr_hino_find(dr, h_inode->i_ino);
12024+ BUG_ON(!ent);
12025+ au_dr_hino_del(dr, ent);
9f237c51 12026+ au_kfree_rcu(ent);
8b6a4947
AM
12027+
12028+out:
9f237c51 12029+ au_kfree_try_rcu(rev);
8b6a4947
AM
12030+ if (unlikely(err))
12031+ pr_err("failed to remove dirren info\n");
12032+}
12033+
12034+/* ---------------------------------------------------------------------- */
12035+
12036+static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
12037+ char *whname, int whnamelen,
12038+ struct dentry **info_dentry)
12039+{
12040+ struct au_drinfo *drinfo;
12041+ struct file *f;
12042+ struct inode *h_dir;
12043+ struct path infopath;
12044+ int unlocked;
12045+
12046+ AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
12047+
12048+ *info_dentry = NULL;
12049+ drinfo = NULL;
12050+ unlocked = 0;
12051+ h_dir = d_inode(h_ppath->dentry);
be118d29 12052+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8b6a4947
AM
12053+ infopath.dentry = vfsub_lookup_one_len(whname, h_ppath->dentry,
12054+ whnamelen);
12055+ if (IS_ERR(infopath.dentry)) {
12056+ drinfo = (void *)infopath.dentry;
12057+ goto out;
12058+ }
12059+
12060+ if (d_is_negative(infopath.dentry))
12061+ goto out_dput; /* success */
12062+
12063+ infopath.mnt = h_ppath->mnt;
12064+ f = vfsub_dentry_open(&infopath, O_RDONLY);
12065+ inode_unlock_shared(h_dir);
12066+ unlocked = 1;
12067+ if (IS_ERR(f)) {
12068+ drinfo = (void *)f;
12069+ goto out_dput;
12070+ }
12071+
12072+ drinfo = au_drinfo_read_k(f, /*h_ino*/0);
12073+ if (IS_ERR_OR_NULL(drinfo))
12074+ goto out_fput;
12075+
12076+ AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
12077+ *info_dentry = dget(infopath.dentry); /* keep it alive */
12078+
12079+out_fput:
12080+ fput(f);
12081+out_dput:
12082+ dput(infopath.dentry);
12083+out:
12084+ if (!unlocked)
12085+ inode_unlock_shared(h_dir);
12086+ AuTraceErrPtr(drinfo);
12087+ return drinfo;
12088+}
12089+
12090+struct au_drinfo_do_load_args {
12091+ struct au_drinfo **drinfop;
12092+ struct path *h_ppath;
12093+ char *whname;
12094+ int whnamelen;
12095+ struct dentry **info_dentry;
12096+};
12097+
12098+static void au_call_drinfo_do_load(void *args)
12099+{
12100+ struct au_drinfo_do_load_args *a = args;
12101+
12102+ *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
12103+ a->info_dentry);
12104+}
12105+
12106+struct au_drinfo_load {
12107+ struct path h_ppath;
12108+ struct qstr *qname;
12109+ unsigned char no_sio;
12110+
12111+ aufs_bindex_t ninfo;
12112+ struct au_drinfo **drinfo;
12113+};
12114+
12115+static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
12116+ struct au_branch *br)
12117+{
12118+ int err, wkq_err, whnamelen, e;
12119+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
12120+ = AUFS_WH_DR_INFO_PFX;
12121+ struct au_drinfo *drinfo;
12122+ struct qstr oldname;
12123+ struct inode *h_dir, *delegated;
12124+ struct dentry *info_dentry;
12125+ struct path infopath;
12126+
12127+ whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
12128+ whnamelen += au_drinfo_name(br, whname + whnamelen,
12129+ sizeof(whname) - whnamelen);
12130+ if (w->no_sio)
12131+ drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
12132+ &info_dentry);
12133+ else {
12134+ struct au_drinfo_do_load_args args = {
12135+ .drinfop = &drinfo,
12136+ .h_ppath = &w->h_ppath,
12137+ .whname = whname,
12138+ .whnamelen = whnamelen,
12139+ .info_dentry = &info_dentry
12140+ };
12141+ wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
12142+ if (unlikely(wkq_err))
12143+ drinfo = ERR_PTR(wkq_err);
12144+ }
12145+ err = PTR_ERR(drinfo);
12146+ if (IS_ERR_OR_NULL(drinfo))
12147+ goto out;
12148+
12149+ err = 0;
12150+ oldname.len = drinfo->oldnamelen;
12151+ oldname.name = drinfo->oldname;
12152+ if (au_qstreq(w->qname, &oldname)) {
12153+ /* the name is renamed back */
9f237c51 12154+ au_kfree_rcu(drinfo);
8b6a4947
AM
12155+ drinfo = NULL;
12156+
12157+ infopath.dentry = info_dentry;
12158+ infopath.mnt = w->h_ppath.mnt;
12159+ h_dir = d_inode(w->h_ppath.dentry);
12160+ delegated = NULL;
12161+ inode_lock_nested(h_dir, AuLsc_I_PARENT);
12162+ e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
12163+ inode_unlock(h_dir);
12164+ if (unlikely(e))
12165+ AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
12166+ if (unlikely(e == -EWOULDBLOCK))
12167+ iput(delegated);
12168+ }
9f237c51 12169+ au_kfree_rcu(w->drinfo[bindex]);
8b6a4947
AM
12170+ w->drinfo[bindex] = drinfo;
12171+ dput(info_dentry);
12172+
12173+out:
12174+ AuTraceErr(err);
12175+ return err;
12176+}
12177+
12178+/* ---------------------------------------------------------------------- */
12179+
12180+static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12181+{
12182+ struct au_drinfo **p = drinfo;
12183+
12184+ while (n-- > 0)
9f237c51
AM
12185+ au_kfree_rcu(*drinfo++);
12186+ au_kfree_try_rcu(p);
8b6a4947
AM
12187+}
12188+
12189+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12190+ aufs_bindex_t btgt)
12191+{
12192+ int err, ninfo;
12193+ struct au_drinfo_load w;
12194+ aufs_bindex_t bindex, bbot;
12195+ struct au_branch *br;
12196+ struct inode *h_dir;
12197+ struct au_dr_hino *ent;
12198+ struct super_block *sb;
12199+
12200+ AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12201+ AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12202+ AuLNPair(&lkup->whname), btgt);
12203+
12204+ sb = dentry->d_sb;
12205+ bbot = au_sbbot(sb);
12206+ w.ninfo = bbot + 1;
12207+ if (!lkup->dirren.drinfo) {
12208+ lkup->dirren.drinfo = kcalloc(w.ninfo,
12209+ sizeof(*lkup->dirren.drinfo),
12210+ GFP_NOFS);
12211+ if (unlikely(!lkup->dirren.drinfo)) {
12212+ err = -ENOMEM;
12213+ goto out;
12214+ }
12215+ lkup->dirren.ninfo = w.ninfo;
12216+ }
12217+ w.drinfo = lkup->dirren.drinfo;
12218+ w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12219+ w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12220+ AuDebugOn(!w.h_ppath.dentry);
12221+ w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12222+ w.qname = &dentry->d_name;
12223+
12224+ ninfo = 0;
12225+ for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12226+ br = au_sbr(sb, bindex);
12227+ err = au_drinfo_load(&w, bindex, br);
12228+ if (unlikely(err))
12229+ goto out_free;
12230+ if (w.drinfo[bindex])
12231+ ninfo++;
12232+ }
12233+ if (!ninfo) {
12234+ br = au_sbr(sb, btgt);
12235+ h_dir = d_inode(w.h_ppath.dentry);
12236+ ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12237+ AuDebugOn(!ent);
12238+ au_dr_hino_del(&br->br_dirren, ent);
9f237c51 12239+ au_kfree_rcu(ent);
8b6a4947
AM
12240+ }
12241+ goto out; /* success */
12242+
12243+out_free:
12244+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12245+ lkup->dirren.ninfo = 0;
12246+ lkup->dirren.drinfo = NULL;
12247+out:
12248+ AuTraceErr(err);
12249+ return err;
12250+}
12251+
12252+void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12253+{
12254+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12255+}
12256+
12257+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12258+{
12259+ int err;
12260+ struct au_drinfo *drinfo;
12261+
12262+ err = 0;
12263+ if (!lkup->dirren.drinfo)
12264+ goto out;
12265+ AuDebugOn(lkup->dirren.ninfo < btgt + 1);
12266+ drinfo = lkup->dirren.drinfo[btgt + 1];
12267+ if (!drinfo)
12268+ goto out;
12269+
9f237c51 12270+ au_kfree_try_rcu(lkup->whname.name);
8b6a4947
AM
12271+ lkup->whname.name = NULL;
12272+ lkup->dirren.dr_name.len = drinfo->oldnamelen;
12273+ lkup->dirren.dr_name.name = drinfo->oldname;
12274+ lkup->name = &lkup->dirren.dr_name;
12275+ err = au_wh_name_alloc(&lkup->whname, lkup->name);
12276+ if (!err)
12277+ AuDbg("name %.*s, whname %.*s, b%d\n",
12278+ AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12279+ btgt);
12280+
12281+out:
12282+ AuTraceErr(err);
12283+ return err;
12284+}
12285+
12286+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12287+ ino_t h_ino)
12288+{
12289+ int match;
12290+ struct au_drinfo *drinfo;
12291+
12292+ match = 1;
12293+ if (!lkup->dirren.drinfo)
12294+ goto out;
12295+ AuDebugOn(lkup->dirren.ninfo < bindex + 1);
12296+ drinfo = lkup->dirren.drinfo[bindex + 1];
12297+ if (!drinfo)
12298+ goto out;
12299+
12300+ match = (drinfo->ino == h_ino);
12301+ AuDbg("match %d\n", match);
12302+
12303+out:
12304+ return match;
12305+}
12306+
12307+/* ---------------------------------------------------------------------- */
12308+
12309+int au_dr_opt_set(struct super_block *sb)
12310+{
12311+ int err;
12312+ aufs_bindex_t bindex, bbot;
12313+ struct au_branch *br;
12314+
12315+ err = 0;
12316+ bbot = au_sbbot(sb);
12317+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12318+ br = au_sbr(sb, bindex);
12319+ err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12320+ }
12321+
12322+ return err;
12323+}
12324+
12325+int au_dr_opt_flush(struct super_block *sb)
12326+{
12327+ int err;
12328+ aufs_bindex_t bindex, bbot;
12329+ struct au_branch *br;
12330+
12331+ err = 0;
12332+ bbot = au_sbbot(sb);
12333+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12334+ br = au_sbr(sb, bindex);
12335+ if (au_br_writable(br->br_perm))
12336+ err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12337+ }
12338+
12339+ return err;
12340+}
12341+
12342+int au_dr_opt_clr(struct super_block *sb, int no_flush)
12343+{
12344+ int err;
12345+ aufs_bindex_t bindex, bbot;
12346+ struct au_branch *br;
12347+
12348+ err = 0;
12349+ if (!no_flush) {
12350+ err = au_dr_opt_flush(sb);
12351+ if (unlikely(err))
12352+ goto out;
12353+ }
12354+
12355+ bbot = au_sbbot(sb);
12356+ for (bindex = 0; bindex <= bbot; bindex++) {
12357+ br = au_sbr(sb, bindex);
12358+ au_dr_hino_free(&br->br_dirren);
12359+ }
12360+
12361+out:
12362+ return err;
12363+}
12364diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12365--- /usr/share/empty/fs/aufs/dirren.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 12366+++ linux/fs/aufs/dirren.h 2018-08-12 23:43:05.453457863 +0200
062440b3
AM
12367@@ -0,0 +1,140 @@
12368+/* SPDX-License-Identifier: GPL-2.0 */
8b6a4947 12369+/*
b00004a5 12370+ * Copyright (C) 2017-2018 Junjiro R. Okajima
8b6a4947
AM
12371+ *
12372+ * This program, aufs is free software; you can redistribute it and/or modify
12373+ * it under the terms of the GNU General Public License as published by
12374+ * the Free Software Foundation; either version 2 of the License, or
12375+ * (at your option) any later version.
12376+ *
12377+ * This program is distributed in the hope that it will be useful,
12378+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12379+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12380+ * GNU General Public License for more details.
12381+ *
12382+ * You should have received a copy of the GNU General Public License
12383+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
12384+ */
12385+
12386+/*
12387+ * renamed dir info
12388+ */
12389+
12390+#ifndef __AUFS_DIRREN_H__
12391+#define __AUFS_DIRREN_H__
12392+
12393+#ifdef __KERNEL__
12394+
12395+#include <linux/dcache.h>
12396+#include <linux/statfs.h>
12397+#include <linux/uuid.h>
12398+#include "hbl.h"
12399+
12400+#define AuDirren_NHASH 100
12401+
12402+#ifdef CONFIG_AUFS_DIRREN
12403+enum au_brid_type {
12404+ AuBrid_Unset,
12405+ AuBrid_UUID,
12406+ AuBrid_FSID,
12407+ AuBrid_DEV
12408+};
12409+
12410+struct au_dr_brid {
12411+ enum au_brid_type type;
12412+ union {
12413+ uuid_t uuid; /* unimplemented yet */
12414+ fsid_t fsid;
12415+ dev_t dev;
12416+ };
12417+};
12418+
12419+/* 20 is the max digits length of ulong 64 */
12420+/* brid-type "_" uuid "_" inum */
12421+#define AUFS_DIRREN_FNAME_SZ (1 + 1 + UUID_STRING_LEN + 20)
12422+#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12423+
12424+struct au_dr_hino {
12425+ struct hlist_bl_node dr_hnode;
12426+ ino_t dr_h_ino;
12427+};
12428+
12429+struct au_dr_br {
12430+ struct hlist_bl_head dr_h_ino[AuDirren_NHASH];
12431+ struct au_dr_brid dr_brid;
12432+};
12433+
12434+struct au_dr_lookup {
12435+ /* dr_name is pointed by struct au_do_lookup_args.name */
12436+ struct qstr dr_name; /* subset of dr_info */
12437+ aufs_bindex_t ninfo;
12438+ struct au_drinfo **drinfo;
12439+};
12440+#else
12441+struct au_dr_hino;
12442+/* empty */
12443+struct au_dr_br { };
12444+struct au_dr_lookup { };
12445+#endif
12446+
12447+/* ---------------------------------------------------------------------- */
12448+
12449+struct au_branch;
12450+struct au_do_lookup_args;
12451+struct au_hinode;
12452+#ifdef CONFIG_AUFS_DIRREN
12453+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12454+ struct au_dr_hino *add_ent);
12455+void au_dr_hino_free(struct au_dr_br *dr);
12456+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12457+ const struct path *path);
12458+int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12459+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12460+ struct qstr *dst_name, void *_rev);
12461+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12462+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12463+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12464+ aufs_bindex_t bindex);
12465+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12466+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12467+ ino_t h_ino);
12468+void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12469+int au_dr_opt_set(struct super_block *sb);
12470+int au_dr_opt_flush(struct super_block *sb);
12471+int au_dr_opt_clr(struct super_block *sb, int no_flush);
12472+#else
12473+AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12474+ struct au_dr_hino *add_ent);
12475+AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12476+AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12477+ const struct path *path);
12478+AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12479+AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12480+ struct qstr *dst_name, void *_rev);
12481+AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12482+AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12483+ void *rev);
12484+AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12485+ aufs_bindex_t bindex);
12486+AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12487+AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12488+ aufs_bindex_t bindex, ino_t h_ino);
12489+AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12490+AuStubInt0(au_dr_opt_set, struct super_block *sb);
12491+AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12492+AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12493+#endif
12494+
12495+/* ---------------------------------------------------------------------- */
12496+
12497+#ifdef CONFIG_AUFS_DIRREN
12498+static inline int au_dr_ihash(ino_t h_ino)
12499+{
12500+ return h_ino % AuDirren_NHASH;
12501+}
12502+#else
12503+AuStubInt0(au_dr_ihash, ino_t h_ino);
12504+#endif
12505+
12506+#endif /* __KERNEL__ */
12507+#endif /* __AUFS_DIRREN_H__ */
7f207e10
AM
12508diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12509--- /usr/share/empty/fs/aufs/dynop.c 1970-01-01 01:00:00.000000000 +0100
9f237c51 12510+++ linux/fs/aufs/dynop.c 2018-12-27 13:19:17.708416053 +0100
062440b3 12511@@ -0,0 +1,370 @@
cd7a4cd9 12512+// SPDX-License-Identifier: GPL-2.0
1facf9fc 12513+/*
b00004a5 12514+ * Copyright (C) 2010-2018 Junjiro R. Okajima
1facf9fc 12515+ *
12516+ * This program, aufs is free software; you can redistribute it and/or modify
12517+ * it under the terms of the GNU General Public License as published by
12518+ * the Free Software Foundation; either version 2 of the License, or
12519+ * (at your option) any later version.
dece6358
AM
12520+ *
12521+ * This program is distributed in the hope that it will be useful,
12522+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12523+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12524+ * GNU General Public License for more details.
12525+ *
12526+ * You should have received a copy of the GNU General Public License
523b37e3 12527+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 12528+ */
12529+
12530+/*
4a4d8108 12531+ * dynamically customizable operations for regular files
1facf9fc 12532+ */
12533+
1facf9fc 12534+#include "aufs.h"
12535+
4a4d8108 12536+#define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
1facf9fc 12537+
4a4d8108
AM
12538+/*
12539+ * How large will these lists be?
12540+ * Usually just a few elements, 20-30 at most for each, I guess.
12541+ */
8b6a4947 12542+static struct hlist_bl_head dynop[AuDyLast];
4a4d8108 12543+
8b6a4947
AM
12544+static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12545+ const void *h_op)
1facf9fc 12546+{
4a4d8108 12547+ struct au_dykey *key, *tmp;
8b6a4947 12548+ struct hlist_bl_node *pos;
1facf9fc 12549+
4a4d8108 12550+ key = NULL;
8b6a4947
AM
12551+ hlist_bl_lock(hbl);
12552+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
4a4d8108
AM
12553+ if (tmp->dk_op.dy_hop == h_op) {
12554+ key = tmp;
12555+ kref_get(&key->dk_kref);
12556+ break;
12557+ }
8b6a4947 12558+ hlist_bl_unlock(hbl);
4a4d8108
AM
12559+
12560+ return key;
1facf9fc 12561+}
12562+
4a4d8108 12563+static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
1facf9fc 12564+{
4a4d8108
AM
12565+ struct au_dykey **k, *found;
12566+ const void *h_op = key->dk_op.dy_hop;
12567+ int i;
1facf9fc 12568+
4a4d8108
AM
12569+ found = NULL;
12570+ k = br->br_dykey;
12571+ for (i = 0; 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+ break;
12579+ if (!found) {
12580+ spin_lock(&br->br_dykey_lock);
12581+ for (; i < AuBrDynOp; i++)
12582+ if (k[i]) {
12583+ if (k[i]->dk_op.dy_hop == h_op) {
12584+ found = k[i];
12585+ break;
12586+ }
12587+ } else {
12588+ k[i] = key;
12589+ break;
12590+ }
12591+ spin_unlock(&br->br_dykey_lock);
12592+ BUG_ON(i == AuBrDynOp); /* expand the array */
12593+ }
12594+
12595+ return found;
1facf9fc 12596+}
12597+
4a4d8108 12598+/* kref_get() if @key is already added */
8b6a4947 12599+static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
4a4d8108
AM
12600+{
12601+ struct au_dykey *tmp, *found;
8b6a4947 12602+ struct hlist_bl_node *pos;
4a4d8108 12603+ const void *h_op = key->dk_op.dy_hop;
1facf9fc 12604+
4a4d8108 12605+ found = NULL;
8b6a4947
AM
12606+ hlist_bl_lock(hbl);
12607+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
4a4d8108
AM
12608+ if (tmp->dk_op.dy_hop == h_op) {
12609+ kref_get(&tmp->dk_kref);
12610+ found = tmp;
12611+ break;
12612+ }
12613+ if (!found)
8b6a4947
AM
12614+ hlist_bl_add_head(&key->dk_hnode, hbl);
12615+ hlist_bl_unlock(hbl);
1facf9fc 12616+
4a4d8108
AM
12617+ if (!found)
12618+ DyPrSym(key);
12619+ return found;
12620+}
12621+
12622+static void dy_free_rcu(struct rcu_head *rcu)
1facf9fc 12623+{
4a4d8108
AM
12624+ struct au_dykey *key;
12625+
12626+ key = container_of(rcu, struct au_dykey, dk_rcu);
12627+ DyPrSym(key);
9f237c51 12628+ au_kfree_rcu(key);
1facf9fc 12629+}
12630+
4a4d8108
AM
12631+static void dy_free(struct kref *kref)
12632+{
12633+ struct au_dykey *key;
8b6a4947 12634+ struct hlist_bl_head *hbl;
1facf9fc 12635+
4a4d8108 12636+ key = container_of(kref, struct au_dykey, dk_kref);
8b6a4947
AM
12637+ hbl = dynop + key->dk_op.dy_type;
12638+ au_hbl_del(&key->dk_hnode, hbl);
4a4d8108
AM
12639+ call_rcu(&key->dk_rcu, dy_free_rcu);
12640+}
12641+
12642+void au_dy_put(struct au_dykey *key)
1facf9fc 12643+{
4a4d8108
AM
12644+ kref_put(&key->dk_kref, dy_free);
12645+}
1facf9fc 12646+
4a4d8108
AM
12647+/* ---------------------------------------------------------------------- */
12648+
12649+#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12650+
12651+#ifdef CONFIG_AUFS_DEBUG
12652+#define DyDbgDeclare(cnt) unsigned int cnt = 0
4f0767ce 12653+#define DyDbgInc(cnt) do { cnt++; } while (0)
4a4d8108
AM
12654+#else
12655+#define DyDbgDeclare(cnt) do {} while (0)
12656+#define DyDbgInc(cnt) do {} while (0)
12657+#endif
12658+
12659+#define DySet(func, dst, src, h_op, h_sb) do { \
12660+ DyDbgInc(cnt); \
12661+ if (h_op->func) { \
12662+ if (src.func) \
12663+ dst.func = src.func; \
12664+ else \
12665+ AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
12666+ } \
12667+} while (0)
12668+
12669+#define DySetForce(func, dst, src) do { \
12670+ AuDebugOn(!src.func); \
12671+ DyDbgInc(cnt); \
12672+ dst.func = src.func; \
12673+} while (0)
12674+
12675+#define DySetAop(func) \
12676+ DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12677+#define DySetAopForce(func) \
12678+ DySetForce(func, dyaop->da_op, aufs_aop)
12679+
12680+static void dy_aop(struct au_dykey *key, const void *h_op,
12681+ struct super_block *h_sb __maybe_unused)
12682+{
12683+ struct au_dyaop *dyaop = (void *)key;
12684+ const struct address_space_operations *h_aop = h_op;
12685+ DyDbgDeclare(cnt);
12686+
12687+ AuDbg("%s\n", au_sbtype(h_sb));
12688+
12689+ DySetAop(writepage);
12690+ DySetAopForce(readpage); /* force */
4a4d8108
AM
12691+ DySetAop(writepages);
12692+ DySetAop(set_page_dirty);
12693+ DySetAop(readpages);
12694+ DySetAop(write_begin);
12695+ DySetAop(write_end);
12696+ DySetAop(bmap);
12697+ DySetAop(invalidatepage);
12698+ DySetAop(releasepage);
027c5e7a 12699+ DySetAop(freepage);
7e9cd9fe 12700+ /* this one will be changed according to an aufs mount option */
4a4d8108 12701+ DySetAop(direct_IO);
4a4d8108 12702+ DySetAop(migratepage);
e2f27e51
AM
12703+ DySetAop(isolate_page);
12704+ DySetAop(putback_page);
4a4d8108
AM
12705+ DySetAop(launder_page);
12706+ DySetAop(is_partially_uptodate);
392086de 12707+ DySetAop(is_dirty_writeback);
4a4d8108 12708+ DySetAop(error_remove_page);
b4510431
AM
12709+ DySetAop(swap_activate);
12710+ DySetAop(swap_deactivate);
4a4d8108
AM
12711+
12712+ DyDbgSize(cnt, *h_aop);
4a4d8108
AM
12713+}
12714+
4a4d8108
AM
12715+/* ---------------------------------------------------------------------- */
12716+
12717+static void dy_bug(struct kref *kref)
12718+{
12719+ BUG();
12720+}
12721+
12722+static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12723+{
12724+ struct au_dykey *key, *old;
8b6a4947 12725+ struct hlist_bl_head *hbl;
b752ccd1 12726+ struct op {
4a4d8108 12727+ unsigned int sz;
b752ccd1
AM
12728+ void (*set)(struct au_dykey *key, const void *h_op,
12729+ struct super_block *h_sb __maybe_unused);
12730+ };
12731+ static const struct op a[] = {
4a4d8108
AM
12732+ [AuDy_AOP] = {
12733+ .sz = sizeof(struct au_dyaop),
b752ccd1 12734+ .set = dy_aop
4a4d8108 12735+ }
b752ccd1
AM
12736+ };
12737+ const struct op *p;
4a4d8108 12738+
8b6a4947
AM
12739+ hbl = dynop + op->dy_type;
12740+ key = dy_gfind_get(hbl, op->dy_hop);
4a4d8108
AM
12741+ if (key)
12742+ goto out_add; /* success */
12743+
12744+ p = a + op->dy_type;
12745+ key = kzalloc(p->sz, GFP_NOFS);
12746+ if (unlikely(!key)) {
12747+ key = ERR_PTR(-ENOMEM);
12748+ goto out;
12749+ }
12750+
12751+ key->dk_op.dy_hop = op->dy_hop;
12752+ kref_init(&key->dk_kref);
86dc4139 12753+ p->set(key, op->dy_hop, au_br_sb(br));
8b6a4947 12754+ old = dy_gadd(hbl, key);
4a4d8108 12755+ if (old) {
9f237c51 12756+ au_kfree_rcu(key);
4a4d8108
AM
12757+ key = old;
12758+ }
12759+
12760+out_add:
12761+ old = dy_bradd(br, key);
12762+ if (old)
12763+ /* its ref-count should never be zero here */
12764+ kref_put(&key->dk_kref, dy_bug);
12765+out:
12766+ return key;
12767+}
12768+
12769+/* ---------------------------------------------------------------------- */
12770+/*
acd2b654 12771+ * Aufs prohibits O_DIRECT by default even if the branch supports it.
c1595e42 12772+ * This behaviour is necessary to return an error from open(O_DIRECT) instead
4a4d8108
AM
12773+ * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12774+ * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12775+ * See the aufs manual in detail.
4a4d8108
AM
12776+ */
12777+static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12778+{
7e9cd9fe 12779+ if (!do_dx)
4a4d8108 12780+ dyaop->da_op.direct_IO = NULL;
7e9cd9fe 12781+ else
4a4d8108 12782+ dyaop->da_op.direct_IO = aufs_aop.direct_IO;
4a4d8108
AM
12783+}
12784+
12785+static struct au_dyaop *dy_aget(struct au_branch *br,
12786+ const struct address_space_operations *h_aop,
12787+ int do_dx)
12788+{
12789+ struct au_dyaop *dyaop;
12790+ struct au_dynop op;
12791+
12792+ op.dy_type = AuDy_AOP;
12793+ op.dy_haop = h_aop;
12794+ dyaop = (void *)dy_get(&op, br);
12795+ if (IS_ERR(dyaop))
12796+ goto out;
12797+ dy_adx(dyaop, do_dx);
12798+
12799+out:
12800+ return dyaop;
12801+}
12802+
12803+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12804+ struct inode *h_inode)
12805+{
12806+ int err, do_dx;
12807+ struct super_block *sb;
12808+ struct au_branch *br;
12809+ struct au_dyaop *dyaop;
12810+
12811+ AuDebugOn(!S_ISREG(h_inode->i_mode));
12812+ IiMustWriteLock(inode);
12813+
12814+ sb = inode->i_sb;
12815+ br = au_sbr(sb, bindex);
12816+ do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12817+ dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12818+ err = PTR_ERR(dyaop);
12819+ if (IS_ERR(dyaop))
12820+ /* unnecessary to call dy_fput() */
12821+ goto out;
12822+
12823+ err = 0;
12824+ inode->i_mapping->a_ops = &dyaop->da_op;
12825+
12826+out:
12827+ return err;
12828+}
12829+
b752ccd1
AM
12830+/*
12831+ * Is it safe to replace a_ops during the inode/file is in operation?
12832+ * Yes, I hope so.
12833+ */
12834+int au_dy_irefresh(struct inode *inode)
12835+{
12836+ int err;
5afbbe0d 12837+ aufs_bindex_t btop;
b752ccd1
AM
12838+ struct inode *h_inode;
12839+
12840+ err = 0;
12841+ if (S_ISREG(inode->i_mode)) {
5afbbe0d
AM
12842+ btop = au_ibtop(inode);
12843+ h_inode = au_h_iptr(inode, btop);
12844+ err = au_dy_iaop(inode, btop, h_inode);
b752ccd1
AM
12845+ }
12846+ return err;
12847+}
12848+
4a4d8108
AM
12849+void au_dy_arefresh(int do_dx)
12850+{
8b6a4947
AM
12851+ struct hlist_bl_head *hbl;
12852+ struct hlist_bl_node *pos;
4a4d8108
AM
12853+ struct au_dykey *key;
12854+
8b6a4947
AM
12855+ hbl = dynop + AuDy_AOP;
12856+ hlist_bl_lock(hbl);
12857+ hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
4a4d8108 12858+ dy_adx((void *)key, do_dx);
8b6a4947 12859+ hlist_bl_unlock(hbl);
4a4d8108
AM
12860+}
12861+
4a4d8108
AM
12862+/* ---------------------------------------------------------------------- */
12863+
12864+void __init au_dy_init(void)
12865+{
12866+ int i;
12867+
12868+ /* make sure that 'struct au_dykey *' can be any type */
12869+ BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
4a4d8108
AM
12870+
12871+ for (i = 0; i < AuDyLast; i++)
8b6a4947 12872+ INIT_HLIST_BL_HEAD(dynop + i);
4a4d8108
AM
12873+}
12874+
12875+void au_dy_fin(void)
12876+{
12877+ int i;
12878+
12879+ for (i = 0; i < AuDyLast; i++)
8b6a4947 12880+ WARN_ON(!hlist_bl_empty(dynop + i));
4a4d8108 12881+}
7f207e10
AM
12882diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12883--- /usr/share/empty/fs/aufs/dynop.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 12884+++ linux/fs/aufs/dynop.h 2018-08-12 23:43:05.456791299 +0200
062440b3
AM
12885@@ -0,0 +1,75 @@
12886+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 12887+/*
b00004a5 12888+ * Copyright (C) 2010-2018 Junjiro R. Okajima
4a4d8108
AM
12889+ *
12890+ * This program, aufs is free software; you can redistribute it and/or modify
12891+ * it under the terms of the GNU General Public License as published by
12892+ * the Free Software Foundation; either version 2 of the License, or
12893+ * (at your option) any later version.
12894+ *
12895+ * This program is distributed in the hope that it will be useful,
12896+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12897+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12898+ * GNU General Public License for more details.
12899+ *
12900+ * You should have received a copy of the GNU General Public License
523b37e3 12901+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
12902+ */
12903+
12904+/*
12905+ * dynamically customizable operations (for regular files only)
12906+ */
12907+
12908+#ifndef __AUFS_DYNOP_H__
12909+#define __AUFS_DYNOP_H__
12910+
12911+#ifdef __KERNEL__
12912+
7e9cd9fe
AM
12913+#include <linux/fs.h>
12914+#include <linux/kref.h>
4a4d8108 12915+
2cbb1c4b 12916+enum {AuDy_AOP, AuDyLast};
4a4d8108
AM
12917+
12918+struct au_dynop {
12919+ int dy_type;
12920+ union {
12921+ const void *dy_hop;
12922+ const struct address_space_operations *dy_haop;
4a4d8108
AM
12923+ };
12924+};
12925+
12926+struct au_dykey {
12927+ union {
8b6a4947 12928+ struct hlist_bl_node dk_hnode;
4a4d8108
AM
12929+ struct rcu_head dk_rcu;
12930+ };
12931+ struct au_dynop dk_op;
12932+
12933+ /*
12934+ * during I am in the branch local array, kref is gotten. when the
12935+ * branch is removed, kref is put.
12936+ */
12937+ struct kref dk_kref;
12938+};
12939+
12940+/* stop unioning since their sizes are very different from each other */
12941+struct au_dyaop {
12942+ struct au_dykey da_key;
12943+ struct address_space_operations da_op; /* not const */
4a4d8108
AM
12944+};
12945+
4a4d8108
AM
12946+/* ---------------------------------------------------------------------- */
12947+
12948+/* dynop.c */
12949+struct au_branch;
12950+void au_dy_put(struct au_dykey *key);
12951+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12952+ struct inode *h_inode);
b752ccd1 12953+int au_dy_irefresh(struct inode *inode);
4a4d8108 12954+void au_dy_arefresh(int do_dio);
4a4d8108
AM
12955+
12956+void __init au_dy_init(void);
12957+void au_dy_fin(void);
12958+
4a4d8108
AM
12959+#endif /* __KERNEL__ */
12960+#endif /* __AUFS_DYNOP_H__ */
7f207e10
AM
12961diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12962--- /usr/share/empty/fs/aufs/export.c 1970-01-01 01:00:00.000000000 +0100
acd2b654 12963+++ linux/fs/aufs/export.c 2018-10-23 12:33:35.596042364 +0200
062440b3 12964@@ -0,0 +1,838 @@
cd7a4cd9 12965+// SPDX-License-Identifier: GPL-2.0
4a4d8108 12966+/*
b00004a5 12967+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
12968+ *
12969+ * This program, aufs is free software; you can redistribute it and/or modify
12970+ * it under the terms of the GNU General Public License as published by
12971+ * the Free Software Foundation; either version 2 of the License, or
12972+ * (at your option) any later version.
12973+ *
12974+ * This program is distributed in the hope that it will be useful,
12975+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12976+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12977+ * GNU General Public License for more details.
12978+ *
12979+ * You should have received a copy of the GNU General Public License
523b37e3 12980+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
12981+ */
12982+
12983+/*
12984+ * export via nfs
12985+ */
12986+
12987+#include <linux/exportfs.h>
7eafdf33 12988+#include <linux/fs_struct.h>
4a4d8108
AM
12989+#include <linux/namei.h>
12990+#include <linux/nsproxy.h>
12991+#include <linux/random.h>
12992+#include <linux/writeback.h>
12993+#include "aufs.h"
12994+
12995+union conv {
12996+#ifdef CONFIG_AUFS_INO_T_64
12997+ __u32 a[2];
12998+#else
12999+ __u32 a[1];
13000+#endif
13001+ ino_t ino;
13002+};
13003+
13004+static ino_t decode_ino(__u32 *a)
13005+{
13006+ union conv u;
13007+
13008+ BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
13009+ u.a[0] = a[0];
13010+#ifdef CONFIG_AUFS_INO_T_64
13011+ u.a[1] = a[1];
13012+#endif
13013+ return u.ino;
13014+}
13015+
13016+static void encode_ino(__u32 *a, ino_t ino)
13017+{
13018+ union conv u;
13019+
13020+ u.ino = ino;
13021+ a[0] = u.a[0];
13022+#ifdef CONFIG_AUFS_INO_T_64
13023+ a[1] = u.a[1];
13024+#endif
13025+}
13026+
13027+/* NFS file handle */
13028+enum {
13029+ Fh_br_id,
13030+ Fh_sigen,
13031+#ifdef CONFIG_AUFS_INO_T_64
13032+ /* support 64bit inode number */
13033+ Fh_ino1,
13034+ Fh_ino2,
13035+ Fh_dir_ino1,
13036+ Fh_dir_ino2,
13037+#else
13038+ Fh_ino1,
13039+ Fh_dir_ino1,
13040+#endif
13041+ Fh_igen,
13042+ Fh_h_type,
13043+ Fh_tail,
13044+
13045+ Fh_ino = Fh_ino1,
13046+ Fh_dir_ino = Fh_dir_ino1
13047+};
13048+
13049+static int au_test_anon(struct dentry *dentry)
13050+{
027c5e7a 13051+ /* note: read d_flags without d_lock */
4a4d8108
AM
13052+ return !!(dentry->d_flags & DCACHE_DISCONNECTED);
13053+}
13054+
a2a7ad62
AM
13055+int au_test_nfsd(void)
13056+{
13057+ int ret;
13058+ struct task_struct *tsk = current;
13059+ char comm[sizeof(tsk->comm)];
13060+
13061+ ret = 0;
13062+ if (tsk->flags & PF_KTHREAD) {
13063+ get_task_comm(comm, tsk);
13064+ ret = !strcmp(comm, "nfsd");
13065+ }
13066+
13067+ return ret;
13068+}
13069+
4a4d8108
AM
13070+/* ---------------------------------------------------------------------- */
13071+/* inode generation external table */
13072+
b752ccd1 13073+void au_xigen_inc(struct inode *inode)
4a4d8108 13074+{
4a4d8108
AM
13075+ loff_t pos;
13076+ ssize_t sz;
13077+ __u32 igen;
13078+ struct super_block *sb;
13079+ struct au_sbinfo *sbinfo;
13080+
4a4d8108 13081+ sb = inode->i_sb;
b752ccd1 13082+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
1facf9fc 13083+
b752ccd1 13084+ sbinfo = au_sbi(sb);
1facf9fc 13085+ pos = inode->i_ino;
13086+ pos *= sizeof(igen);
13087+ igen = inode->i_generation + 1;
1facf9fc 13088+ sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
13089+ sizeof(igen), &pos);
13090+ if (sz == sizeof(igen))
b752ccd1 13091+ return; /* success */
1facf9fc 13092+
b752ccd1 13093+ if (unlikely(sz >= 0))
1facf9fc 13094+ AuIOErr("xigen error (%zd)\n", sz);
1facf9fc 13095+}
13096+
13097+int au_xigen_new(struct inode *inode)
13098+{
13099+ int err;
13100+ loff_t pos;
13101+ ssize_t sz;
13102+ struct super_block *sb;
13103+ struct au_sbinfo *sbinfo;
13104+ struct file *file;
13105+
13106+ err = 0;
13107+ /* todo: dirty, at mount time */
13108+ if (inode->i_ino == AUFS_ROOT_INO)
13109+ goto out;
13110+ sb = inode->i_sb;
dece6358 13111+ SiMustAnyLock(sb);
1facf9fc 13112+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13113+ goto out;
13114+
13115+ err = -EFBIG;
13116+ pos = inode->i_ino;
13117+ if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
13118+ AuIOErr1("too large i%lld\n", pos);
13119+ goto out;
13120+ }
13121+ pos *= sizeof(inode->i_generation);
13122+
13123+ err = 0;
13124+ sbinfo = au_sbi(sb);
13125+ file = sbinfo->si_xigen;
13126+ BUG_ON(!file);
13127+
c06a8ce3 13128+ if (vfsub_f_size_read(file)
1facf9fc 13129+ < pos + sizeof(inode->i_generation)) {
13130+ inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
13131+ sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
13132+ sizeof(inode->i_generation), &pos);
13133+ } else
13134+ sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
13135+ sizeof(inode->i_generation), &pos);
13136+ if (sz == sizeof(inode->i_generation))
13137+ goto out; /* success */
13138+
13139+ err = sz;
13140+ if (unlikely(sz >= 0)) {
13141+ err = -EIO;
13142+ AuIOErr("xigen error (%zd)\n", sz);
13143+ }
13144+
4f0767ce 13145+out:
1facf9fc 13146+ return err;
13147+}
13148+
062440b3 13149+int au_xigen_set(struct super_block *sb, struct path *path)
1facf9fc 13150+{
13151+ int err;
13152+ struct au_sbinfo *sbinfo;
13153+ struct file *file;
13154+
dece6358
AM
13155+ SiMustWriteLock(sb);
13156+
1facf9fc 13157+ sbinfo = au_sbi(sb);
062440b3 13158+ file = au_xino_create2(sb, path, sbinfo->si_xigen);
1facf9fc 13159+ err = PTR_ERR(file);
13160+ if (IS_ERR(file))
13161+ goto out;
13162+ err = 0;
13163+ if (sbinfo->si_xigen)
13164+ fput(sbinfo->si_xigen);
13165+ sbinfo->si_xigen = file;
13166+
4f0767ce 13167+out:
062440b3 13168+ AuTraceErr(err);
1facf9fc 13169+ return err;
13170+}
13171+
13172+void au_xigen_clr(struct super_block *sb)
13173+{
13174+ struct au_sbinfo *sbinfo;
13175+
dece6358
AM
13176+ SiMustWriteLock(sb);
13177+
1facf9fc 13178+ sbinfo = au_sbi(sb);
13179+ if (sbinfo->si_xigen) {
13180+ fput(sbinfo->si_xigen);
13181+ sbinfo->si_xigen = NULL;
13182+ }
13183+}
13184+
13185+/* ---------------------------------------------------------------------- */
13186+
13187+static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13188+ ino_t dir_ino)
13189+{
13190+ struct dentry *dentry, *d;
13191+ struct inode *inode;
13192+ unsigned int sigen;
13193+
13194+ dentry = NULL;
13195+ inode = ilookup(sb, ino);
13196+ if (!inode)
13197+ goto out;
13198+
13199+ dentry = ERR_PTR(-ESTALE);
13200+ sigen = au_sigen(sb);
5afbbe0d 13201+ if (unlikely(au_is_bad_inode(inode)
1facf9fc 13202+ || IS_DEADDIR(inode)
537831f9 13203+ || sigen != au_iigen(inode, NULL)))
1facf9fc 13204+ goto out_iput;
13205+
13206+ dentry = NULL;
13207+ if (!dir_ino || S_ISDIR(inode->i_mode))
13208+ dentry = d_find_alias(inode);
13209+ else {
027c5e7a 13210+ spin_lock(&inode->i_lock);
c1595e42 13211+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 13212+ spin_lock(&d->d_lock);
1facf9fc 13213+ if (!au_test_anon(d)
5527c038 13214+ && d_inode(d->d_parent)->i_ino == dir_ino) {
027c5e7a
AM
13215+ dentry = dget_dlock(d);
13216+ spin_unlock(&d->d_lock);
1facf9fc 13217+ break;
13218+ }
027c5e7a
AM
13219+ spin_unlock(&d->d_lock);
13220+ }
13221+ spin_unlock(&inode->i_lock);
1facf9fc 13222+ }
027c5e7a 13223+ if (unlikely(dentry && au_digen_test(dentry, sigen))) {
2cbb1c4b 13224+ /* need to refresh */
1facf9fc 13225+ dput(dentry);
2cbb1c4b 13226+ dentry = NULL;
1facf9fc 13227+ }
13228+
4f0767ce 13229+out_iput:
1facf9fc 13230+ iput(inode);
4f0767ce 13231+out:
2cbb1c4b 13232+ AuTraceErrPtr(dentry);
1facf9fc 13233+ return dentry;
13234+}
13235+
13236+/* ---------------------------------------------------------------------- */
13237+
13238+/* todo: dirty? */
13239+/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
4a4d8108
AM
13240+
13241+struct au_compare_mnt_args {
13242+ /* input */
13243+ struct super_block *sb;
13244+
13245+ /* output */
13246+ struct vfsmount *mnt;
13247+};
13248+
13249+static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13250+{
13251+ struct au_compare_mnt_args *a = arg;
13252+
13253+ if (mnt->mnt_sb != a->sb)
13254+ return 0;
13255+ a->mnt = mntget(mnt);
13256+ return 1;
13257+}
13258+
1facf9fc 13259+static struct vfsmount *au_mnt_get(struct super_block *sb)
13260+{
4a4d8108 13261+ int err;
7eafdf33 13262+ struct path root;
4a4d8108
AM
13263+ struct au_compare_mnt_args args = {
13264+ .sb = sb
13265+ };
1facf9fc 13266+
7eafdf33 13267+ get_fs_root(current->fs, &root);
523b37e3 13268+ rcu_read_lock();
7eafdf33 13269+ err = iterate_mounts(au_compare_mnt, &args, root.mnt);
523b37e3 13270+ rcu_read_unlock();
7eafdf33 13271+ path_put(&root);
4a4d8108
AM
13272+ AuDebugOn(!err);
13273+ AuDebugOn(!args.mnt);
13274+ return args.mnt;
1facf9fc 13275+}
13276+
13277+struct au_nfsd_si_lock {
4a4d8108 13278+ unsigned int sigen;
027c5e7a 13279+ aufs_bindex_t bindex, br_id;
1facf9fc 13280+ unsigned char force_lock;
13281+};
13282+
027c5e7a
AM
13283+static int si_nfsd_read_lock(struct super_block *sb,
13284+ struct au_nfsd_si_lock *nsi_lock)
1facf9fc 13285+{
027c5e7a 13286+ int err;
1facf9fc 13287+ aufs_bindex_t bindex;
13288+
13289+ si_read_lock(sb, AuLock_FLUSH);
13290+
13291+ /* branch id may be wrapped around */
027c5e7a 13292+ err = 0;
1facf9fc 13293+ bindex = au_br_index(sb, nsi_lock->br_id);
13294+ if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13295+ goto out; /* success */
13296+
027c5e7a
AM
13297+ err = -ESTALE;
13298+ bindex = -1;
1facf9fc 13299+ if (!nsi_lock->force_lock)
13300+ si_read_unlock(sb);
1facf9fc 13301+
4f0767ce 13302+out:
027c5e7a
AM
13303+ nsi_lock->bindex = bindex;
13304+ return err;
1facf9fc 13305+}
13306+
13307+struct find_name_by_ino {
392086de 13308+ struct dir_context ctx;
1facf9fc 13309+ int called, found;
13310+ ino_t ino;
13311+ char *name;
13312+ int namelen;
13313+};
13314+
13315+static int
392086de
AM
13316+find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13317+ loff_t offset, u64 ino, unsigned int d_type)
1facf9fc 13318+{
392086de
AM
13319+ struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13320+ ctx);
1facf9fc 13321+
13322+ a->called++;
13323+ if (a->ino != ino)
13324+ return 0;
13325+
13326+ memcpy(a->name, name, namelen);
13327+ a->namelen = namelen;
13328+ a->found = 1;
13329+ return 1;
13330+}
13331+
13332+static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13333+ struct au_nfsd_si_lock *nsi_lock)
13334+{
13335+ struct dentry *dentry, *parent;
13336+ struct file *file;
13337+ struct inode *dir;
392086de
AM
13338+ struct find_name_by_ino arg = {
13339+ .ctx = {
2000de60 13340+ .actor = find_name_by_ino
392086de
AM
13341+ }
13342+ };
1facf9fc 13343+ int err;
13344+
13345+ parent = path->dentry;
13346+ if (nsi_lock)
13347+ si_read_unlock(parent->d_sb);
4a4d8108 13348+ file = vfsub_dentry_open(path, au_dir_roflags);
1facf9fc 13349+ dentry = (void *)file;
13350+ if (IS_ERR(file))
13351+ goto out;
13352+
13353+ dentry = ERR_PTR(-ENOMEM);
537831f9 13354+ arg.name = (void *)__get_free_page(GFP_NOFS);
1facf9fc 13355+ if (unlikely(!arg.name))
13356+ goto out_file;
13357+ arg.ino = ino;
13358+ arg.found = 0;
13359+ do {
13360+ arg.called = 0;
13361+ /* smp_mb(); */
392086de 13362+ err = vfsub_iterate_dir(file, &arg.ctx);
1facf9fc 13363+ } while (!err && !arg.found && arg.called);
13364+ dentry = ERR_PTR(err);
13365+ if (unlikely(err))
13366+ goto out_name;
1716fcea
AM
13367+ /* instead of ENOENT */
13368+ dentry = ERR_PTR(-ESTALE);
1facf9fc 13369+ if (!arg.found)
13370+ goto out_name;
13371+
b4510431 13372+ /* do not call vfsub_lkup_one() */
5527c038 13373+ dir = d_inode(parent);
febd17d6 13374+ dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
1facf9fc 13375+ AuTraceErrPtr(dentry);
13376+ if (IS_ERR(dentry))
13377+ goto out_name;
13378+ AuDebugOn(au_test_anon(dentry));
5527c038 13379+ if (unlikely(d_really_is_negative(dentry))) {
1facf9fc 13380+ dput(dentry);
13381+ dentry = ERR_PTR(-ENOENT);
13382+ }
13383+
4f0767ce 13384+out_name:
1c60b727 13385+ free_page((unsigned long)arg.name);
4f0767ce 13386+out_file:
1facf9fc 13387+ fput(file);
4f0767ce 13388+out:
1facf9fc 13389+ if (unlikely(nsi_lock
13390+ && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13391+ if (!IS_ERR(dentry)) {
13392+ dput(dentry);
13393+ dentry = ERR_PTR(-ESTALE);
13394+ }
13395+ AuTraceErrPtr(dentry);
13396+ return dentry;
13397+}
13398+
13399+static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13400+ ino_t dir_ino,
13401+ struct au_nfsd_si_lock *nsi_lock)
13402+{
13403+ struct dentry *dentry;
13404+ struct path path;
13405+
13406+ if (dir_ino != AUFS_ROOT_INO) {
13407+ path.dentry = decode_by_ino(sb, dir_ino, 0);
13408+ dentry = path.dentry;
13409+ if (!path.dentry || IS_ERR(path.dentry))
13410+ goto out;
13411+ AuDebugOn(au_test_anon(path.dentry));
13412+ } else
13413+ path.dentry = dget(sb->s_root);
13414+
13415+ path.mnt = au_mnt_get(sb);
13416+ dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13417+ path_put(&path);
13418+
4f0767ce 13419+out:
1facf9fc 13420+ AuTraceErrPtr(dentry);
13421+ return dentry;
13422+}
13423+
13424+/* ---------------------------------------------------------------------- */
13425+
13426+static int h_acceptable(void *expv, struct dentry *dentry)
13427+{
13428+ return 1;
13429+}
13430+
13431+static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13432+ char *buf, int len, struct super_block *sb)
13433+{
13434+ char *p;
13435+ int n;
13436+ struct path path;
13437+
13438+ p = d_path(h_rootpath, buf, len);
13439+ if (IS_ERR(p))
13440+ goto out;
13441+ n = strlen(p);
13442+
13443+ path.mnt = h_rootpath->mnt;
13444+ path.dentry = h_parent;
13445+ p = d_path(&path, buf, len);
13446+ if (IS_ERR(p))
13447+ goto out;
13448+ if (n != 1)
13449+ p += n;
13450+
13451+ path.mnt = au_mnt_get(sb);
13452+ path.dentry = sb->s_root;
13453+ p = d_path(&path, buf, len - strlen(p));
13454+ mntput(path.mnt);
13455+ if (IS_ERR(p))
13456+ goto out;
13457+ if (n != 1)
13458+ p[strlen(p)] = '/';
13459+
4f0767ce 13460+out:
1facf9fc 13461+ AuTraceErrPtr(p);
13462+ return p;
13463+}
13464+
13465+static
027c5e7a
AM
13466+struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13467+ int fh_len, struct au_nfsd_si_lock *nsi_lock)
1facf9fc 13468+{
13469+ struct dentry *dentry, *h_parent, *root;
13470+ struct super_block *h_sb;
13471+ char *pathname, *p;
13472+ struct vfsmount *h_mnt;
13473+ struct au_branch *br;
13474+ int err;
13475+ struct path path;
13476+
027c5e7a 13477+ br = au_sbr(sb, nsi_lock->bindex);
86dc4139 13478+ h_mnt = au_br_mnt(br);
1facf9fc 13479+ h_sb = h_mnt->mnt_sb;
13480+ /* todo: call lower fh_to_dentry()? fh_to_parent()? */
5afbbe0d 13481+ lockdep_off();
1facf9fc 13482+ h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13483+ fh_len - Fh_tail, fh[Fh_h_type],
13484+ h_acceptable, /*context*/NULL);
5afbbe0d 13485+ lockdep_on();
1facf9fc 13486+ dentry = h_parent;
13487+ if (unlikely(!h_parent || IS_ERR(h_parent))) {
13488+ AuWarn1("%s decode_fh failed, %ld\n",
13489+ au_sbtype(h_sb), PTR_ERR(h_parent));
13490+ goto out;
13491+ }
13492+ dentry = NULL;
13493+ if (unlikely(au_test_anon(h_parent))) {
13494+ AuWarn1("%s decode_fh returned a disconnected dentry\n",
13495+ au_sbtype(h_sb));
13496+ goto out_h_parent;
13497+ }
13498+
13499+ dentry = ERR_PTR(-ENOMEM);
13500+ pathname = (void *)__get_free_page(GFP_NOFS);
13501+ if (unlikely(!pathname))
13502+ goto out_h_parent;
13503+
13504+ root = sb->s_root;
13505+ path.mnt = h_mnt;
13506+ di_read_lock_parent(root, !AuLock_IR);
027c5e7a 13507+ path.dentry = au_h_dptr(root, nsi_lock->bindex);
1facf9fc 13508+ di_read_unlock(root, !AuLock_IR);
13509+ p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13510+ dentry = (void *)p;
13511+ if (IS_ERR(p))
13512+ goto out_pathname;
13513+
13514+ si_read_unlock(sb);
13515+ err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13516+ dentry = ERR_PTR(err);
13517+ if (unlikely(err))
13518+ goto out_relock;
13519+
13520+ dentry = ERR_PTR(-ENOENT);
13521+ AuDebugOn(au_test_anon(path.dentry));
5527c038 13522+ if (unlikely(d_really_is_negative(path.dentry)))
1facf9fc 13523+ goto out_path;
13524+
5527c038 13525+ if (ino != d_inode(path.dentry)->i_ino)
1facf9fc 13526+ dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13527+ else
13528+ dentry = dget(path.dentry);
13529+
4f0767ce 13530+out_path:
1facf9fc 13531+ path_put(&path);
4f0767ce 13532+out_relock:
1facf9fc 13533+ if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13534+ if (!IS_ERR(dentry)) {
13535+ dput(dentry);
13536+ dentry = ERR_PTR(-ESTALE);
13537+ }
4f0767ce 13538+out_pathname:
1c60b727 13539+ free_page((unsigned long)pathname);
4f0767ce 13540+out_h_parent:
1facf9fc 13541+ dput(h_parent);
4f0767ce 13542+out:
1facf9fc 13543+ AuTraceErrPtr(dentry);
13544+ return dentry;
13545+}
13546+
13547+/* ---------------------------------------------------------------------- */
13548+
13549+static struct dentry *
13550+aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13551+ int fh_type)
13552+{
13553+ struct dentry *dentry;
13554+ __u32 *fh = fid->raw;
027c5e7a 13555+ struct au_branch *br;
1facf9fc 13556+ ino_t ino, dir_ino;
1facf9fc 13557+ struct au_nfsd_si_lock nsi_lock = {
1facf9fc 13558+ .force_lock = 0
13559+ };
13560+
1facf9fc 13561+ dentry = ERR_PTR(-ESTALE);
4a4d8108
AM
13562+ /* it should never happen, but the file handle is unreliable */
13563+ if (unlikely(fh_len < Fh_tail))
13564+ goto out;
13565+ nsi_lock.sigen = fh[Fh_sigen];
13566+ nsi_lock.br_id = fh[Fh_br_id];
13567+
1facf9fc 13568+ /* branch id may be wrapped around */
027c5e7a
AM
13569+ br = NULL;
13570+ if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
1facf9fc 13571+ goto out;
13572+ nsi_lock.force_lock = 1;
13573+
13574+ /* is this inode still cached? */
13575+ ino = decode_ino(fh + Fh_ino);
4a4d8108
AM
13576+ /* it should never happen */
13577+ if (unlikely(ino == AUFS_ROOT_INO))
8cdd5066 13578+ goto out_unlock;
4a4d8108 13579+
1facf9fc 13580+ dir_ino = decode_ino(fh + Fh_dir_ino);
13581+ dentry = decode_by_ino(sb, ino, dir_ino);
13582+ if (IS_ERR(dentry))
13583+ goto out_unlock;
13584+ if (dentry)
13585+ goto accept;
13586+
13587+ /* is the parent dir cached? */
027c5e7a 13588+ br = au_sbr(sb, nsi_lock.bindex);
acd2b654 13589+ au_lcnt_inc(&br->br_nfiles);
1facf9fc 13590+ dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13591+ if (IS_ERR(dentry))
13592+ goto out_unlock;
13593+ if (dentry)
13594+ goto accept;
13595+
13596+ /* lookup path */
027c5e7a 13597+ dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
1facf9fc 13598+ if (IS_ERR(dentry))
13599+ goto out_unlock;
13600+ if (unlikely(!dentry))
13601+ /* todo?: make it ESTALE */
13602+ goto out_unlock;
13603+
4f0767ce 13604+accept:
027c5e7a 13605+ if (!au_digen_test(dentry, au_sigen(sb))
5527c038 13606+ && d_inode(dentry)->i_generation == fh[Fh_igen])
1facf9fc 13607+ goto out_unlock; /* success */
13608+
13609+ dput(dentry);
13610+ dentry = ERR_PTR(-ESTALE);
4f0767ce 13611+out_unlock:
027c5e7a 13612+ if (br)
acd2b654 13613+ au_lcnt_dec(&br->br_nfiles);
1facf9fc 13614+ si_read_unlock(sb);
4f0767ce 13615+out:
1facf9fc 13616+ AuTraceErrPtr(dentry);
13617+ return dentry;
13618+}
13619+
13620+#if 0 /* reserved for future use */
13621+/* support subtreecheck option */
13622+static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13623+ int fh_len, int fh_type)
13624+{
13625+ struct dentry *parent;
13626+ __u32 *fh = fid->raw;
13627+ ino_t dir_ino;
13628+
13629+ dir_ino = decode_ino(fh + Fh_dir_ino);
13630+ parent = decode_by_ino(sb, dir_ino, 0);
13631+ if (IS_ERR(parent))
13632+ goto out;
13633+ if (!parent)
13634+ parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13635+ dir_ino, fh, fh_len);
13636+
4f0767ce 13637+out:
1facf9fc 13638+ AuTraceErrPtr(parent);
13639+ return parent;
13640+}
13641+#endif
13642+
13643+/* ---------------------------------------------------------------------- */
13644+
0c3ec466
AM
13645+static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13646+ struct inode *dir)
1facf9fc 13647+{
13648+ int err;
0c3ec466 13649+ aufs_bindex_t bindex;
1facf9fc 13650+ struct super_block *sb, *h_sb;
0c3ec466
AM
13651+ struct dentry *dentry, *parent, *h_parent;
13652+ struct inode *h_dir;
1facf9fc 13653+ struct au_branch *br;
13654+
1facf9fc 13655+ err = -ENOSPC;
13656+ if (unlikely(*max_len <= Fh_tail)) {
13657+ AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13658+ goto out;
13659+ }
13660+
13661+ err = FILEID_ROOT;
0c3ec466
AM
13662+ if (inode->i_ino == AUFS_ROOT_INO) {
13663+ AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
1facf9fc 13664+ goto out;
13665+ }
13666+
1facf9fc 13667+ h_parent = NULL;
0c3ec466
AM
13668+ sb = inode->i_sb;
13669+ err = si_read_lock(sb, AuLock_FLUSH);
027c5e7a
AM
13670+ if (unlikely(err))
13671+ goto out;
13672+
1facf9fc 13673+#ifdef CONFIG_AUFS_DEBUG
13674+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13675+ AuWarn1("NFS-exporting requires xino\n");
13676+#endif
027c5e7a 13677+ err = -EIO;
0c3ec466
AM
13678+ parent = NULL;
13679+ ii_read_lock_child(inode);
5afbbe0d 13680+ bindex = au_ibtop(inode);
0c3ec466 13681+ if (!dir) {
c1595e42 13682+ dentry = d_find_any_alias(inode);
0c3ec466
AM
13683+ if (unlikely(!dentry))
13684+ goto out_unlock;
13685+ AuDebugOn(au_test_anon(dentry));
13686+ parent = dget_parent(dentry);
13687+ dput(dentry);
13688+ if (unlikely(!parent))
13689+ goto out_unlock;
5527c038
JR
13690+ if (d_really_is_positive(parent))
13691+ dir = d_inode(parent);
1facf9fc 13692+ }
0c3ec466
AM
13693+
13694+ ii_read_lock_parent(dir);
13695+ h_dir = au_h_iptr(dir, bindex);
13696+ ii_read_unlock(dir);
13697+ if (unlikely(!h_dir))
13698+ goto out_parent;
c1595e42 13699+ h_parent = d_find_any_alias(h_dir);
1facf9fc 13700+ if (unlikely(!h_parent))
0c3ec466 13701+ goto out_hparent;
1facf9fc 13702+
13703+ err = -EPERM;
13704+ br = au_sbr(sb, bindex);
86dc4139 13705+ h_sb = au_br_sb(br);
1facf9fc 13706+ if (unlikely(!h_sb->s_export_op)) {
13707+ AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
0c3ec466 13708+ goto out_hparent;
1facf9fc 13709+ }
13710+
13711+ fh[Fh_br_id] = br->br_id;
13712+ fh[Fh_sigen] = au_sigen(sb);
13713+ encode_ino(fh + Fh_ino, inode->i_ino);
0c3ec466 13714+ encode_ino(fh + Fh_dir_ino, dir->i_ino);
1facf9fc 13715+ fh[Fh_igen] = inode->i_generation;
13716+
13717+ *max_len -= Fh_tail;
13718+ fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13719+ max_len,
13720+ /*connectable or subtreecheck*/0);
13721+ err = fh[Fh_h_type];
13722+ *max_len += Fh_tail;
13723+ /* todo: macros? */
1716fcea 13724+ if (err != FILEID_INVALID)
1facf9fc 13725+ err = 99;
13726+ else
13727+ AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13728+
0c3ec466 13729+out_hparent:
1facf9fc 13730+ dput(h_parent);
0c3ec466 13731+out_parent:
1facf9fc 13732+ dput(parent);
0c3ec466
AM
13733+out_unlock:
13734+ ii_read_unlock(inode);
13735+ si_read_unlock(sb);
4f0767ce 13736+out:
1facf9fc 13737+ if (unlikely(err < 0))
1716fcea 13738+ err = FILEID_INVALID;
1facf9fc 13739+ return err;
13740+}
13741+
13742+/* ---------------------------------------------------------------------- */
13743+
4a4d8108
AM
13744+static int aufs_commit_metadata(struct inode *inode)
13745+{
13746+ int err;
13747+ aufs_bindex_t bindex;
13748+ struct super_block *sb;
13749+ struct inode *h_inode;
13750+ int (*f)(struct inode *inode);
13751+
13752+ sb = inode->i_sb;
e49829fe 13753+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 13754+ ii_write_lock_child(inode);
5afbbe0d 13755+ bindex = au_ibtop(inode);
4a4d8108
AM
13756+ AuDebugOn(bindex < 0);
13757+ h_inode = au_h_iptr(inode, bindex);
13758+
13759+ f = h_inode->i_sb->s_export_op->commit_metadata;
13760+ if (f)
13761+ err = f(h_inode);
13762+ else {
13763+ struct writeback_control wbc = {
13764+ .sync_mode = WB_SYNC_ALL,
13765+ .nr_to_write = 0 /* metadata only */
13766+ };
13767+
13768+ err = sync_inode(h_inode, &wbc);
13769+ }
13770+
13771+ au_cpup_attr_timesizes(inode);
13772+ ii_write_unlock(inode);
13773+ si_read_unlock(sb);
13774+ return err;
13775+}
13776+
13777+/* ---------------------------------------------------------------------- */
13778+
1facf9fc 13779+static struct export_operations aufs_export_op = {
4a4d8108 13780+ .fh_to_dentry = aufs_fh_to_dentry,
1facf9fc 13781+ /* .fh_to_parent = aufs_fh_to_parent, */
4a4d8108
AM
13782+ .encode_fh = aufs_encode_fh,
13783+ .commit_metadata = aufs_commit_metadata
1facf9fc 13784+};
13785+
13786+void au_export_init(struct super_block *sb)
13787+{
13788+ struct au_sbinfo *sbinfo;
13789+ __u32 u;
13790+
5afbbe0d
AM
13791+ BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13792+ && IS_MODULE(CONFIG_EXPORTFS),
13793+ AUFS_NAME ": unsupported configuration "
13794+ "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13795+
1facf9fc 13796+ sb->s_export_op = &aufs_export_op;
13797+ sbinfo = au_sbi(sb);
13798+ sbinfo->si_xigen = NULL;
13799+ get_random_bytes(&u, sizeof(u));
13800+ BUILD_BUG_ON(sizeof(u) != sizeof(int));
13801+ atomic_set(&sbinfo->si_xigen_next, u);
13802+}
076b876e
AM
13803diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13804--- /usr/share/empty/fs/aufs/fhsm.c 1970-01-01 01:00:00.000000000 +0100
acd2b654 13805+++ linux/fs/aufs/fhsm.c 2018-10-23 12:33:35.596042364 +0200
062440b3 13806@@ -0,0 +1,427 @@
cd7a4cd9 13807+// SPDX-License-Identifier: GPL-2.0
076b876e 13808+/*
b00004a5 13809+ * Copyright (C) 2011-2018 Junjiro R. Okajima
076b876e
AM
13810+ *
13811+ * This program, aufs is free software; you can redistribute it and/or modify
13812+ * it under the terms of the GNU General Public License as published by
13813+ * the Free Software Foundation; either version 2 of the License, or
13814+ * (at your option) any later version.
13815+ *
13816+ * This program is distributed in the hope that it will be useful,
13817+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13818+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13819+ * GNU General Public License for more details.
13820+ *
13821+ * You should have received a copy of the GNU General Public License
13822+ * along with this program; if not, write to the Free Software
13823+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
13824+ */
13825+
13826+/*
13827+ * File-based Hierarchy Storage Management
13828+ */
13829+
13830+#include <linux/anon_inodes.h>
13831+#include <linux/poll.h>
13832+#include <linux/seq_file.h>
13833+#include <linux/statfs.h>
13834+#include "aufs.h"
13835+
c1595e42
JR
13836+static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13837+{
13838+ struct au_sbinfo *sbinfo;
13839+ struct au_fhsm *fhsm;
13840+
13841+ SiMustAnyLock(sb);
13842+
13843+ sbinfo = au_sbi(sb);
13844+ fhsm = &sbinfo->si_fhsm;
13845+ AuDebugOn(!fhsm);
13846+ return fhsm->fhsm_bottom;
13847+}
13848+
13849+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13850+{
13851+ struct au_sbinfo *sbinfo;
13852+ struct au_fhsm *fhsm;
13853+
13854+ SiMustWriteLock(sb);
13855+
13856+ sbinfo = au_sbi(sb);
13857+ fhsm = &sbinfo->si_fhsm;
13858+ AuDebugOn(!fhsm);
13859+ fhsm->fhsm_bottom = bindex;
13860+}
13861+
13862+/* ---------------------------------------------------------------------- */
13863+
076b876e
AM
13864+static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13865+{
13866+ struct au_br_fhsm *bf;
13867+
13868+ bf = br->br_fhsm;
13869+ MtxMustLock(&bf->bf_lock);
13870+
13871+ return !bf->bf_readable
13872+ || time_after(jiffies,
13873+ bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13874+}
13875+
13876+/* ---------------------------------------------------------------------- */
13877+
13878+static void au_fhsm_notify(struct super_block *sb, int val)
13879+{
13880+ struct au_sbinfo *sbinfo;
13881+ struct au_fhsm *fhsm;
13882+
13883+ SiMustAnyLock(sb);
13884+
13885+ sbinfo = au_sbi(sb);
13886+ fhsm = &sbinfo->si_fhsm;
13887+ if (au_fhsm_pid(fhsm)
13888+ && atomic_read(&fhsm->fhsm_readable) != -1) {
13889+ atomic_set(&fhsm->fhsm_readable, val);
13890+ if (val)
13891+ wake_up(&fhsm->fhsm_wqh);
13892+ }
13893+}
13894+
13895+static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13896+ struct aufs_stfs *rstfs, int do_lock, int do_notify)
13897+{
13898+ int err;
13899+ struct au_branch *br;
13900+ struct au_br_fhsm *bf;
13901+
13902+ br = au_sbr(sb, bindex);
13903+ AuDebugOn(au_br_rdonly(br));
13904+ bf = br->br_fhsm;
13905+ AuDebugOn(!bf);
13906+
13907+ if (do_lock)
13908+ mutex_lock(&bf->bf_lock);
13909+ else
13910+ MtxMustLock(&bf->bf_lock);
13911+
13912+ /* sb->s_root for NFS is unreliable */
13913+ err = au_br_stfs(br, &bf->bf_stfs);
13914+ if (unlikely(err)) {
13915+ AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13916+ goto out;
13917+ }
13918+
13919+ bf->bf_jiffy = jiffies;
13920+ bf->bf_readable = 1;
13921+ if (do_notify)
13922+ au_fhsm_notify(sb, /*val*/1);
13923+ if (rstfs)
13924+ *rstfs = bf->bf_stfs;
13925+
13926+out:
13927+ if (do_lock)
13928+ mutex_unlock(&bf->bf_lock);
13929+ au_fhsm_notify(sb, /*val*/1);
13930+
13931+ return err;
13932+}
13933+
13934+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13935+{
13936+ int err;
076b876e
AM
13937+ struct au_sbinfo *sbinfo;
13938+ struct au_fhsm *fhsm;
13939+ struct au_branch *br;
13940+ struct au_br_fhsm *bf;
13941+
13942+ AuDbg("b%d, force %d\n", bindex, force);
13943+ SiMustAnyLock(sb);
13944+
13945+ sbinfo = au_sbi(sb);
13946+ fhsm = &sbinfo->si_fhsm;
c1595e42
JR
13947+ if (!au_ftest_si(sbinfo, FHSM)
13948+ || fhsm->fhsm_bottom == bindex)
076b876e
AM
13949+ return;
13950+
13951+ br = au_sbr(sb, bindex);
13952+ bf = br->br_fhsm;
13953+ AuDebugOn(!bf);
13954+ mutex_lock(&bf->bf_lock);
13955+ if (force
13956+ || au_fhsm_pid(fhsm)
13957+ || au_fhsm_test_jiffy(sbinfo, br))
13958+ err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13959+ /*do_notify*/1);
13960+ mutex_unlock(&bf->bf_lock);
13961+}
13962+
13963+void au_fhsm_wrote_all(struct super_block *sb, int force)
13964+{
5afbbe0d 13965+ aufs_bindex_t bindex, bbot;
076b876e
AM
13966+ struct au_branch *br;
13967+
13968+ /* exclude the bottom */
5afbbe0d
AM
13969+ bbot = au_fhsm_bottom(sb);
13970+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
13971+ br = au_sbr(sb, bindex);
13972+ if (au_br_fhsm(br->br_perm))
13973+ au_fhsm_wrote(sb, bindex, force);
13974+ }
13975+}
13976+
13977+/* ---------------------------------------------------------------------- */
13978+
be118d29 13979+static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
076b876e 13980+{
be118d29 13981+ __poll_t mask;
076b876e
AM
13982+ struct au_sbinfo *sbinfo;
13983+ struct au_fhsm *fhsm;
13984+
13985+ mask = 0;
13986+ sbinfo = file->private_data;
13987+ fhsm = &sbinfo->si_fhsm;
13988+ poll_wait(file, &fhsm->fhsm_wqh, wait);
13989+ if (atomic_read(&fhsm->fhsm_readable))
be118d29 13990+ mask = EPOLLIN /* | EPOLLRDNORM */;
076b876e 13991+
b00004a5
AM
13992+ if (!mask)
13993+ AuDbg("mask 0x%x\n", mask);
076b876e
AM
13994+ return mask;
13995+}
13996+
13997+static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13998+ struct aufs_stfs *stfs, __s16 brid)
13999+{
14000+ int err;
14001+
14002+ err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
14003+ if (!err)
14004+ err = __put_user(brid, &stbr->brid);
14005+ if (unlikely(err))
14006+ err = -EFAULT;
14007+
14008+ return err;
14009+}
14010+
14011+static ssize_t au_fhsm_do_read(struct super_block *sb,
14012+ struct aufs_stbr __user *stbr, size_t count)
14013+{
14014+ ssize_t err;
14015+ int nstbr;
5afbbe0d 14016+ aufs_bindex_t bindex, bbot;
076b876e
AM
14017+ struct au_branch *br;
14018+ struct au_br_fhsm *bf;
14019+
14020+ /* except the bottom branch */
14021+ err = 0;
14022+ nstbr = 0;
5afbbe0d
AM
14023+ bbot = au_fhsm_bottom(sb);
14024+ for (bindex = 0; !err && bindex < bbot; bindex++) {
076b876e
AM
14025+ br = au_sbr(sb, bindex);
14026+ if (!au_br_fhsm(br->br_perm))
14027+ continue;
14028+
14029+ bf = br->br_fhsm;
14030+ mutex_lock(&bf->bf_lock);
14031+ if (bf->bf_readable) {
14032+ err = -EFAULT;
14033+ if (count >= sizeof(*stbr))
14034+ err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
14035+ br->br_id);
14036+ if (!err) {
14037+ bf->bf_readable = 0;
14038+ count -= sizeof(*stbr);
14039+ nstbr++;
14040+ }
14041+ }
14042+ mutex_unlock(&bf->bf_lock);
14043+ }
14044+ if (!err)
14045+ err = sizeof(*stbr) * nstbr;
14046+
14047+ return err;
14048+}
14049+
14050+static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
14051+ loff_t *pos)
14052+{
14053+ ssize_t err;
14054+ int readable;
5afbbe0d 14055+ aufs_bindex_t nfhsm, bindex, bbot;
076b876e
AM
14056+ struct au_sbinfo *sbinfo;
14057+ struct au_fhsm *fhsm;
14058+ struct au_branch *br;
14059+ struct super_block *sb;
14060+
14061+ err = 0;
14062+ sbinfo = file->private_data;
14063+ fhsm = &sbinfo->si_fhsm;
14064+need_data:
14065+ spin_lock_irq(&fhsm->fhsm_wqh.lock);
14066+ if (!atomic_read(&fhsm->fhsm_readable)) {
14067+ if (vfsub_file_flags(file) & O_NONBLOCK)
14068+ err = -EAGAIN;
14069+ else
14070+ err = wait_event_interruptible_locked_irq
14071+ (fhsm->fhsm_wqh,
14072+ atomic_read(&fhsm->fhsm_readable));
14073+ }
14074+ spin_unlock_irq(&fhsm->fhsm_wqh.lock);
14075+ if (unlikely(err))
14076+ goto out;
14077+
14078+ /* sb may already be dead */
14079+ au_rw_read_lock(&sbinfo->si_rwsem);
14080+ readable = atomic_read(&fhsm->fhsm_readable);
14081+ if (readable > 0) {
14082+ sb = sbinfo->si_sb;
14083+ AuDebugOn(!sb);
14084+ /* exclude the bottom branch */
14085+ nfhsm = 0;
5afbbe0d
AM
14086+ bbot = au_fhsm_bottom(sb);
14087+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
14088+ br = au_sbr(sb, bindex);
14089+ if (au_br_fhsm(br->br_perm))
14090+ nfhsm++;
14091+ }
14092+ err = -EMSGSIZE;
14093+ if (nfhsm * sizeof(struct aufs_stbr) <= count) {
14094+ atomic_set(&fhsm->fhsm_readable, 0);
14095+ err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
14096+ count);
14097+ }
14098+ }
14099+ au_rw_read_unlock(&sbinfo->si_rwsem);
14100+ if (!readable)
14101+ goto need_data;
14102+
14103+out:
14104+ return err;
14105+}
14106+
14107+static int au_fhsm_release(struct inode *inode, struct file *file)
14108+{
14109+ struct au_sbinfo *sbinfo;
14110+ struct au_fhsm *fhsm;
14111+
14112+ /* sb may already be dead */
14113+ sbinfo = file->private_data;
14114+ fhsm = &sbinfo->si_fhsm;
14115+ spin_lock(&fhsm->fhsm_spin);
14116+ fhsm->fhsm_pid = 0;
14117+ spin_unlock(&fhsm->fhsm_spin);
14118+ kobject_put(&sbinfo->si_kobj);
14119+
14120+ return 0;
14121+}
14122+
14123+static const struct file_operations au_fhsm_fops = {
14124+ .owner = THIS_MODULE,
14125+ .llseek = noop_llseek,
14126+ .read = au_fhsm_read,
14127+ .poll = au_fhsm_poll,
14128+ .release = au_fhsm_release
14129+};
14130+
14131+int au_fhsm_fd(struct super_block *sb, int oflags)
14132+{
14133+ int err, fd;
14134+ struct au_sbinfo *sbinfo;
14135+ struct au_fhsm *fhsm;
14136+
14137+ err = -EPERM;
14138+ if (unlikely(!capable(CAP_SYS_ADMIN)))
14139+ goto out;
14140+
14141+ err = -EINVAL;
14142+ if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
14143+ goto out;
14144+
14145+ err = 0;
14146+ sbinfo = au_sbi(sb);
14147+ fhsm = &sbinfo->si_fhsm;
14148+ spin_lock(&fhsm->fhsm_spin);
14149+ if (!fhsm->fhsm_pid)
14150+ fhsm->fhsm_pid = current->pid;
14151+ else
14152+ err = -EBUSY;
14153+ spin_unlock(&fhsm->fhsm_spin);
14154+ if (unlikely(err))
14155+ goto out;
14156+
14157+ oflags |= O_RDONLY;
14158+ /* oflags |= FMODE_NONOTIFY; */
14159+ fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
14160+ err = fd;
14161+ if (unlikely(fd < 0))
14162+ goto out_pid;
14163+
acd2b654 14164+ /* succeed regardless 'fhsm' status */
076b876e
AM
14165+ kobject_get(&sbinfo->si_kobj);
14166+ si_noflush_read_lock(sb);
14167+ if (au_ftest_si(sbinfo, FHSM))
14168+ au_fhsm_wrote_all(sb, /*force*/0);
14169+ si_read_unlock(sb);
14170+ goto out; /* success */
14171+
14172+out_pid:
14173+ spin_lock(&fhsm->fhsm_spin);
14174+ fhsm->fhsm_pid = 0;
14175+ spin_unlock(&fhsm->fhsm_spin);
14176+out:
14177+ AuTraceErr(err);
14178+ return err;
14179+}
14180+
14181+/* ---------------------------------------------------------------------- */
14182+
14183+int au_fhsm_br_alloc(struct au_branch *br)
14184+{
14185+ int err;
14186+
14187+ err = 0;
14188+ br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14189+ if (br->br_fhsm)
14190+ au_br_fhsm_init(br->br_fhsm);
14191+ else
14192+ err = -ENOMEM;
14193+
14194+ return err;
14195+}
14196+
14197+/* ---------------------------------------------------------------------- */
14198+
14199+void au_fhsm_fin(struct super_block *sb)
14200+{
14201+ au_fhsm_notify(sb, /*val*/-1);
14202+}
14203+
14204+void au_fhsm_init(struct au_sbinfo *sbinfo)
14205+{
14206+ struct au_fhsm *fhsm;
14207+
14208+ fhsm = &sbinfo->si_fhsm;
14209+ spin_lock_init(&fhsm->fhsm_spin);
14210+ init_waitqueue_head(&fhsm->fhsm_wqh);
14211+ atomic_set(&fhsm->fhsm_readable, 0);
14212+ fhsm->fhsm_expire
14213+ = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
c1595e42 14214+ fhsm->fhsm_bottom = -1;
076b876e
AM
14215+}
14216+
14217+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14218+{
14219+ sbinfo->si_fhsm.fhsm_expire
14220+ = msecs_to_jiffies(sec * MSEC_PER_SEC);
14221+}
14222+
14223+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14224+{
14225+ unsigned int u;
14226+
14227+ if (!au_ftest_si(sbinfo, FHSM))
14228+ return;
14229+
14230+ u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14231+ if (u != AUFS_FHSM_CACHE_DEF_SEC)
14232+ seq_printf(seq, ",fhsm_sec=%u", u);
14233+}
7f207e10
AM
14234diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14235--- /usr/share/empty/fs/aufs/file.c 1970-01-01 01:00:00.000000000 +0100
acd2b654
AM
14236+++ linux/fs/aufs/file.c 2018-10-23 12:33:35.596042364 +0200
14237@@ -0,0 +1,863 @@
cd7a4cd9 14238+// SPDX-License-Identifier: GPL-2.0
1facf9fc 14239+/*
b00004a5 14240+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 14241+ *
14242+ * This program, aufs is free software; you can redistribute it and/or modify
14243+ * it under the terms of the GNU General Public License as published by
14244+ * the Free Software Foundation; either version 2 of the License, or
14245+ * (at your option) any later version.
dece6358
AM
14246+ *
14247+ * This program is distributed in the hope that it will be useful,
14248+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14249+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14250+ * GNU General Public License for more details.
14251+ *
14252+ * You should have received a copy of the GNU General Public License
523b37e3 14253+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 14254+ */
14255+
14256+/*
4a4d8108 14257+ * handling file/dir, and address_space operation
1facf9fc 14258+ */
14259+
7eafdf33
AM
14260+#ifdef CONFIG_AUFS_DEBUG
14261+#include <linux/migrate.h>
14262+#endif
4a4d8108 14263+#include <linux/pagemap.h>
1facf9fc 14264+#include "aufs.h"
14265+
4a4d8108
AM
14266+/* drop flags for writing */
14267+unsigned int au_file_roflags(unsigned int flags)
14268+{
14269+ flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14270+ flags |= O_RDONLY | O_NOATIME;
14271+ return flags;
14272+}
14273+
14274+/* common functions to regular file and dir */
14275+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 14276+ struct file *file, int force_wr)
1facf9fc 14277+{
1308ab2a 14278+ struct file *h_file;
4a4d8108
AM
14279+ struct dentry *h_dentry;
14280+ struct inode *h_inode;
14281+ struct super_block *sb;
14282+ struct au_branch *br;
14283+ struct path h_path;
b912730e 14284+ int err;
1facf9fc 14285+
4a4d8108
AM
14286+ /* a race condition can happen between open and unlink/rmdir */
14287+ h_file = ERR_PTR(-ENOENT);
14288+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 14289+ if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
4a4d8108 14290+ goto out;
5527c038 14291+ h_inode = d_inode(h_dentry);
027c5e7a
AM
14292+ spin_lock(&h_dentry->d_lock);
14293+ err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
5527c038 14294+ /* || !d_inode(dentry)->i_nlink */
027c5e7a
AM
14295+ ;
14296+ spin_unlock(&h_dentry->d_lock);
14297+ if (unlikely(err))
4a4d8108 14298+ goto out;
1facf9fc 14299+
4a4d8108
AM
14300+ sb = dentry->d_sb;
14301+ br = au_sbr(sb, bindex);
b912730e
AM
14302+ err = au_br_test_oflag(flags, br);
14303+ h_file = ERR_PTR(err);
14304+ if (unlikely(err))
027c5e7a 14305+ goto out;
1facf9fc 14306+
4a4d8108 14307+ /* drop flags for writing */
5527c038 14308+ if (au_test_ro(sb, bindex, d_inode(dentry))) {
392086de
AM
14309+ if (force_wr && !(flags & O_WRONLY))
14310+ force_wr = 0;
4a4d8108 14311+ flags = au_file_roflags(flags);
392086de
AM
14312+ if (force_wr) {
14313+ h_file = ERR_PTR(-EROFS);
14314+ flags = au_file_roflags(flags);
14315+ if (unlikely(vfsub_native_ro(h_inode)
14316+ || IS_APPEND(h_inode)))
14317+ goto out;
14318+ flags &= ~O_ACCMODE;
14319+ flags |= O_WRONLY;
14320+ }
14321+ }
4a4d8108 14322+ flags &= ~O_CREAT;
acd2b654 14323+ au_lcnt_inc(&br->br_nfiles);
4a4d8108 14324+ h_path.dentry = h_dentry;
86dc4139 14325+ h_path.mnt = au_br_mnt(br);
38d290e6 14326+ h_file = vfsub_dentry_open(&h_path, flags);
4a4d8108
AM
14327+ if (IS_ERR(h_file))
14328+ goto out_br;
dece6358 14329+
b912730e 14330+ if (flags & __FMODE_EXEC) {
4a4d8108
AM
14331+ err = deny_write_access(h_file);
14332+ if (unlikely(err)) {
14333+ fput(h_file);
14334+ h_file = ERR_PTR(err);
14335+ goto out_br;
14336+ }
14337+ }
953406b4 14338+ fsnotify_open(h_file);
4a4d8108 14339+ goto out; /* success */
1facf9fc 14340+
4f0767ce 14341+out_br:
acd2b654 14342+ au_lcnt_dec(&br->br_nfiles);
4f0767ce 14343+out:
4a4d8108
AM
14344+ return h_file;
14345+}
1308ab2a 14346+
076b876e
AM
14347+static int au_cmoo(struct dentry *dentry)
14348+{
8b6a4947 14349+ int err, cmoo, matched;
076b876e
AM
14350+ unsigned int udba;
14351+ struct path h_path;
14352+ struct au_pin pin;
14353+ struct au_cp_generic cpg = {
14354+ .dentry = dentry,
14355+ .bdst = -1,
14356+ .bsrc = -1,
14357+ .len = -1,
14358+ .pin = &pin,
14359+ .flags = AuCpup_DTIME | AuCpup_HOPEN
14360+ };
7e9cd9fe 14361+ struct inode *delegated;
076b876e
AM
14362+ struct super_block *sb;
14363+ struct au_sbinfo *sbinfo;
14364+ struct au_fhsm *fhsm;
14365+ pid_t pid;
14366+ struct au_branch *br;
14367+ struct dentry *parent;
14368+ struct au_hinode *hdir;
14369+
14370+ DiMustWriteLock(dentry);
5527c038 14371+ IiMustWriteLock(d_inode(dentry));
076b876e
AM
14372+
14373+ err = 0;
14374+ if (IS_ROOT(dentry))
14375+ goto out;
5afbbe0d 14376+ cpg.bsrc = au_dbtop(dentry);
076b876e
AM
14377+ if (!cpg.bsrc)
14378+ goto out;
14379+
14380+ sb = dentry->d_sb;
14381+ sbinfo = au_sbi(sb);
14382+ fhsm = &sbinfo->si_fhsm;
14383+ pid = au_fhsm_pid(fhsm);
8b6a4947
AM
14384+ rcu_read_lock();
14385+ matched = (pid
14386+ && (current->pid == pid
14387+ || rcu_dereference(current->real_parent)->pid == pid));
14388+ rcu_read_unlock();
14389+ if (matched)
076b876e
AM
14390+ goto out;
14391+
14392+ br = au_sbr(sb, cpg.bsrc);
14393+ cmoo = au_br_cmoo(br->br_perm);
14394+ if (!cmoo)
14395+ goto out;
7e9cd9fe 14396+ if (!d_is_reg(dentry))
076b876e
AM
14397+ cmoo &= AuBrAttr_COO_ALL;
14398+ if (!cmoo)
14399+ goto out;
14400+
14401+ parent = dget_parent(dentry);
14402+ di_write_lock_parent(parent);
14403+ err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14404+ cpg.bdst = err;
14405+ if (unlikely(err < 0)) {
14406+ err = 0; /* there is no upper writable branch */
14407+ goto out_dgrade;
14408+ }
14409+ AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14410+
14411+ /* do not respect the coo attrib for the target branch */
14412+ err = au_cpup_dirs(dentry, cpg.bdst);
14413+ if (unlikely(err))
14414+ goto out_dgrade;
14415+
14416+ di_downgrade_lock(parent, AuLock_IR);
14417+ udba = au_opt_udba(sb);
14418+ err = au_pin(&pin, dentry, cpg.bdst, udba,
14419+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14420+ if (unlikely(err))
14421+ goto out_parent;
14422+
14423+ err = au_sio_cpup_simple(&cpg);
14424+ au_unpin(&pin);
14425+ if (unlikely(err))
14426+ goto out_parent;
14427+ if (!(cmoo & AuBrWAttr_MOO))
14428+ goto out_parent; /* success */
14429+
14430+ err = au_pin(&pin, dentry, cpg.bsrc, udba,
14431+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14432+ if (unlikely(err))
14433+ goto out_parent;
14434+
14435+ h_path.mnt = au_br_mnt(br);
14436+ h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
5527c038 14437+ hdir = au_hi(d_inode(parent), cpg.bsrc);
076b876e
AM
14438+ delegated = NULL;
14439+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14440+ au_unpin(&pin);
14441+ /* todo: keep h_dentry or not? */
14442+ if (unlikely(err == -EWOULDBLOCK)) {
14443+ pr_warn("cannot retry for NFSv4 delegation"
14444+ " for an internal unlink\n");
14445+ iput(delegated);
14446+ }
14447+ if (unlikely(err)) {
14448+ pr_err("unlink %pd after coo failed (%d), ignored\n",
14449+ dentry, err);
14450+ err = 0;
14451+ }
14452+ goto out_parent; /* success */
14453+
14454+out_dgrade:
14455+ di_downgrade_lock(parent, AuLock_IR);
14456+out_parent:
14457+ di_read_unlock(parent, AuLock_IR);
14458+ dput(parent);
14459+out:
14460+ AuTraceErr(err);
14461+ return err;
14462+}
14463+
b912730e 14464+int au_do_open(struct file *file, struct au_do_open_args *args)
1facf9fc 14465+{
8b6a4947 14466+ int err, aopen = args->aopen;
1facf9fc 14467+ struct dentry *dentry;
076b876e 14468+ struct au_finfo *finfo;
1308ab2a 14469+
8b6a4947 14470+ if (!aopen)
b912730e
AM
14471+ err = au_finfo_init(file, args->fidir);
14472+ else {
14473+ lockdep_off();
14474+ err = au_finfo_init(file, args->fidir);
14475+ lockdep_on();
14476+ }
4a4d8108
AM
14477+ if (unlikely(err))
14478+ goto out;
1facf9fc 14479+
2000de60 14480+ dentry = file->f_path.dentry;
b912730e 14481+ AuDebugOn(IS_ERR_OR_NULL(dentry));
8b6a4947
AM
14482+ di_write_lock_child(dentry);
14483+ err = au_cmoo(dentry);
14484+ di_downgrade_lock(dentry, AuLock_IR);
14485+ if (!err) {
14486+ if (!aopen)
b912730e 14487+ err = args->open(file, vfsub_file_flags(file), NULL);
8b6a4947
AM
14488+ else {
14489+ lockdep_off();
acd2b654
AM
14490+ err = args->open(file, vfsub_file_flags(file),
14491+ args->h_file);
8b6a4947
AM
14492+ lockdep_on();
14493+ }
b912730e 14494+ }
8b6a4947 14495+ di_read_unlock(dentry, AuLock_IR);
1facf9fc 14496+
076b876e
AM
14497+ finfo = au_fi(file);
14498+ if (!err) {
14499+ finfo->fi_file = file;
8b6a4947
AM
14500+ au_hbl_add(&finfo->fi_hlist,
14501+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
076b876e 14502+ }
8b6a4947 14503+ if (!aopen)
b912730e
AM
14504+ fi_write_unlock(file);
14505+ else {
14506+ lockdep_off();
14507+ fi_write_unlock(file);
14508+ lockdep_on();
14509+ }
4a4d8108 14510+ if (unlikely(err)) {
076b876e 14511+ finfo->fi_hdir = NULL;
1c60b727 14512+ au_finfo_fin(file);
1308ab2a 14513+ }
4a4d8108 14514+
4f0767ce 14515+out:
8b6a4947 14516+ AuTraceErr(err);
1308ab2a 14517+ return err;
14518+}
dece6358 14519+
4a4d8108 14520+int au_reopen_nondir(struct file *file)
1308ab2a 14521+{
4a4d8108 14522+ int err;
5afbbe0d 14523+ aufs_bindex_t btop;
4a4d8108 14524+ struct dentry *dentry;
acd2b654 14525+ struct au_branch *br;
4a4d8108 14526+ struct file *h_file, *h_file_tmp;
1308ab2a 14527+
2000de60 14528+ dentry = file->f_path.dentry;
5afbbe0d 14529+ btop = au_dbtop(dentry);
acd2b654 14530+ br = au_sbr(dentry->d_sb, btop);
4a4d8108 14531+ h_file_tmp = NULL;
5afbbe0d 14532+ if (au_fbtop(file) == btop) {
4a4d8108
AM
14533+ h_file = au_hf_top(file);
14534+ if (file->f_mode == h_file->f_mode)
14535+ return 0; /* success */
14536+ h_file_tmp = h_file;
14537+ get_file(h_file_tmp);
acd2b654 14538+ au_lcnt_inc(&br->br_nfiles);
5afbbe0d 14539+ au_set_h_fptr(file, btop, NULL);
4a4d8108
AM
14540+ }
14541+ AuDebugOn(au_fi(file)->fi_hdir);
86dc4139
AM
14542+ /*
14543+ * it can happen
14544+ * file exists on both of rw and ro
5afbbe0d 14545+ * open --> dbtop and fbtop are both 0
86dc4139
AM
14546+ * prepend a branch as rw, "rw" become ro
14547+ * remove rw/file
14548+ * delete the top branch, "rw" becomes rw again
5afbbe0d
AM
14549+ * --> dbtop is 1, fbtop is still 0
14550+ * write --> fbtop is 0 but dbtop is 1
86dc4139 14551+ */
5afbbe0d 14552+ /* AuDebugOn(au_fbtop(file) < btop); */
1308ab2a 14553+
5afbbe0d 14554+ h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
392086de 14555+ file, /*force_wr*/0);
4a4d8108 14556+ err = PTR_ERR(h_file);
86dc4139
AM
14557+ if (IS_ERR(h_file)) {
14558+ if (h_file_tmp) {
acd2b654 14559+ /* revert */
5afbbe0d 14560+ au_set_h_fptr(file, btop, h_file_tmp);
86dc4139
AM
14561+ h_file_tmp = NULL;
14562+ }
4a4d8108 14563+ goto out; /* todo: close all? */
86dc4139 14564+ }
4a4d8108
AM
14565+
14566+ err = 0;
5afbbe0d
AM
14567+ au_set_fbtop(file, btop);
14568+ au_set_h_fptr(file, btop, h_file);
4a4d8108
AM
14569+ au_update_figen(file);
14570+ /* todo: necessary? */
14571+ /* file->f_ra = h_file->f_ra; */
14572+
4f0767ce 14573+out:
acd2b654 14574+ if (h_file_tmp) {
4a4d8108 14575+ fput(h_file_tmp);
acd2b654
AM
14576+ au_lcnt_dec(&br->br_nfiles);
14577+ }
4a4d8108 14578+ return err;
1facf9fc 14579+}
14580+
1308ab2a 14581+/* ---------------------------------------------------------------------- */
14582+
4a4d8108
AM
14583+static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14584+ struct dentry *hi_wh)
1facf9fc 14585+{
4a4d8108 14586+ int err;
5afbbe0d 14587+ aufs_bindex_t btop;
4a4d8108
AM
14588+ struct au_dinfo *dinfo;
14589+ struct dentry *h_dentry;
14590+ struct au_hdentry *hdp;
1facf9fc 14591+
2000de60 14592+ dinfo = au_di(file->f_path.dentry);
4a4d8108 14593+ AuRwMustWriteLock(&dinfo->di_rwsem);
dece6358 14594+
5afbbe0d
AM
14595+ btop = dinfo->di_btop;
14596+ dinfo->di_btop = btgt;
14597+ hdp = au_hdentry(dinfo, btgt);
14598+ h_dentry = hdp->hd_dentry;
14599+ hdp->hd_dentry = hi_wh;
4a4d8108 14600+ err = au_reopen_nondir(file);
5afbbe0d
AM
14601+ hdp->hd_dentry = h_dentry;
14602+ dinfo->di_btop = btop;
1facf9fc 14603+
1facf9fc 14604+ return err;
14605+}
14606+
4a4d8108 14607+static int au_ready_to_write_wh(struct file *file, loff_t len,
86dc4139 14608+ aufs_bindex_t bcpup, struct au_pin *pin)
1facf9fc 14609+{
4a4d8108 14610+ int err;
027c5e7a 14611+ struct inode *inode, *h_inode;
c2b27bf2
AM
14612+ struct dentry *h_dentry, *hi_wh;
14613+ struct au_cp_generic cpg = {
2000de60 14614+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14615+ .bdst = bcpup,
14616+ .bsrc = -1,
14617+ .len = len,
14618+ .pin = pin
14619+ };
1facf9fc 14620+
5afbbe0d 14621+ au_update_dbtop(cpg.dentry);
5527c038 14622+ inode = d_inode(cpg.dentry);
027c5e7a 14623+ h_inode = NULL;
5afbbe0d
AM
14624+ if (au_dbtop(cpg.dentry) <= bcpup
14625+ && au_dbbot(cpg.dentry) >= bcpup) {
c2b27bf2 14626+ h_dentry = au_h_dptr(cpg.dentry, bcpup);
5527c038
JR
14627+ if (h_dentry && d_is_positive(h_dentry))
14628+ h_inode = d_inode(h_dentry);
027c5e7a 14629+ }
4a4d8108 14630+ hi_wh = au_hi_wh(inode, bcpup);
027c5e7a 14631+ if (!hi_wh && !h_inode)
c2b27bf2 14632+ err = au_sio_cpup_wh(&cpg, file);
4a4d8108
AM
14633+ else
14634+ /* already copied-up after unlink */
14635+ err = au_reopen_wh(file, bcpup, hi_wh);
1facf9fc 14636+
4a4d8108 14637+ if (!err
38d290e6
JR
14638+ && (inode->i_nlink > 1
14639+ || (inode->i_state & I_LINKABLE))
c2b27bf2
AM
14640+ && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14641+ au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
1308ab2a 14642+
dece6358 14643+ return err;
1facf9fc 14644+}
14645+
4a4d8108
AM
14646+/*
14647+ * prepare the @file for writing.
14648+ */
14649+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
1facf9fc 14650+{
4a4d8108 14651+ int err;
5afbbe0d 14652+ aufs_bindex_t dbtop;
c1595e42 14653+ struct dentry *parent;
86dc4139 14654+ struct inode *inode;
1facf9fc 14655+ struct super_block *sb;
4a4d8108 14656+ struct file *h_file;
c2b27bf2 14657+ struct au_cp_generic cpg = {
2000de60 14658+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14659+ .bdst = -1,
14660+ .bsrc = -1,
14661+ .len = len,
14662+ .pin = pin,
14663+ .flags = AuCpup_DTIME
14664+ };
1facf9fc 14665+
c2b27bf2 14666+ sb = cpg.dentry->d_sb;
5527c038 14667+ inode = d_inode(cpg.dentry);
5afbbe0d 14668+ cpg.bsrc = au_fbtop(file);
c2b27bf2 14669+ err = au_test_ro(sb, cpg.bsrc, inode);
4a4d8108 14670+ if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
c2b27bf2
AM
14671+ err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14672+ /*flags*/0);
1facf9fc 14673+ goto out;
4a4d8108 14674+ }
1facf9fc 14675+
027c5e7a 14676+ /* need to cpup or reopen */
c2b27bf2 14677+ parent = dget_parent(cpg.dentry);
4a4d8108 14678+ di_write_lock_parent(parent);
c2b27bf2
AM
14679+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14680+ cpg.bdst = err;
4a4d8108
AM
14681+ if (unlikely(err < 0))
14682+ goto out_dgrade;
14683+ err = 0;
14684+
c2b27bf2
AM
14685+ if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14686+ err = au_cpup_dirs(cpg.dentry, cpg.bdst);
1facf9fc 14687+ if (unlikely(err))
4a4d8108
AM
14688+ goto out_dgrade;
14689+ }
14690+
c2b27bf2 14691+ err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108
AM
14692+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14693+ if (unlikely(err))
14694+ goto out_dgrade;
14695+
5afbbe0d
AM
14696+ dbtop = au_dbtop(cpg.dentry);
14697+ if (dbtop <= cpg.bdst)
c2b27bf2 14698+ cpg.bsrc = cpg.bdst;
027c5e7a 14699+
5afbbe0d 14700+ if (dbtop <= cpg.bdst /* just reopen */
c2b27bf2 14701+ || !d_unhashed(cpg.dentry) /* copyup and reopen */
027c5e7a 14702+ ) {
392086de 14703+ h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
86dc4139 14704+ if (IS_ERR(h_file))
027c5e7a 14705+ err = PTR_ERR(h_file);
86dc4139 14706+ else {
027c5e7a 14707+ di_downgrade_lock(parent, AuLock_IR);
5afbbe0d 14708+ if (dbtop > cpg.bdst)
c2b27bf2 14709+ err = au_sio_cpup_simple(&cpg);
027c5e7a
AM
14710+ if (!err)
14711+ err = au_reopen_nondir(file);
c2b27bf2 14712+ au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
027c5e7a 14713+ }
027c5e7a
AM
14714+ } else { /* copyup as wh and reopen */
14715+ /*
14716+ * since writable hfsplus branch is not supported,
14717+ * h_open_pre/post() are unnecessary.
14718+ */
c2b27bf2 14719+ err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
4a4d8108 14720+ di_downgrade_lock(parent, AuLock_IR);
4a4d8108 14721+ }
4a4d8108
AM
14722+
14723+ if (!err) {
14724+ au_pin_set_parent_lflag(pin, /*lflag*/0);
14725+ goto out_dput; /* success */
14726+ }
14727+ au_unpin(pin);
14728+ goto out_unlock;
1facf9fc 14729+
4f0767ce 14730+out_dgrade:
4a4d8108 14731+ di_downgrade_lock(parent, AuLock_IR);
4f0767ce 14732+out_unlock:
4a4d8108 14733+ di_read_unlock(parent, AuLock_IR);
4f0767ce 14734+out_dput:
4a4d8108 14735+ dput(parent);
4f0767ce 14736+out:
1facf9fc 14737+ return err;
14738+}
14739+
4a4d8108
AM
14740+/* ---------------------------------------------------------------------- */
14741+
14742+int au_do_flush(struct file *file, fl_owner_t id,
14743+ int (*flush)(struct file *file, fl_owner_t id))
1facf9fc 14744+{
4a4d8108 14745+ int err;
1facf9fc 14746+ struct super_block *sb;
4a4d8108 14747+ struct inode *inode;
1facf9fc 14748+
c06a8ce3
AM
14749+ inode = file_inode(file);
14750+ sb = inode->i_sb;
4a4d8108
AM
14751+ si_noflush_read_lock(sb);
14752+ fi_read_lock(file);
b752ccd1 14753+ ii_read_lock_child(inode);
1facf9fc 14754+
4a4d8108
AM
14755+ err = flush(file, id);
14756+ au_cpup_attr_timesizes(inode);
1facf9fc 14757+
b752ccd1 14758+ ii_read_unlock(inode);
4a4d8108 14759+ fi_read_unlock(file);
1308ab2a 14760+ si_read_unlock(sb);
dece6358 14761+ return err;
1facf9fc 14762+}
14763+
4a4d8108
AM
14764+/* ---------------------------------------------------------------------- */
14765+
14766+static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
1facf9fc 14767+{
4a4d8108 14768+ int err;
4a4d8108
AM
14769+ struct au_pin pin;
14770+ struct au_finfo *finfo;
c2b27bf2 14771+ struct dentry *parent, *hi_wh;
4a4d8108 14772+ struct inode *inode;
1facf9fc 14773+ struct super_block *sb;
c2b27bf2 14774+ struct au_cp_generic cpg = {
2000de60 14775+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14776+ .bdst = -1,
14777+ .bsrc = -1,
14778+ .len = -1,
14779+ .pin = &pin,
14780+ .flags = AuCpup_DTIME
14781+ };
1facf9fc 14782+
4a4d8108
AM
14783+ FiMustWriteLock(file);
14784+
14785+ err = 0;
14786+ finfo = au_fi(file);
c2b27bf2 14787+ sb = cpg.dentry->d_sb;
5527c038 14788+ inode = d_inode(cpg.dentry);
5afbbe0d 14789+ cpg.bdst = au_ibtop(inode);
c2b27bf2 14790+ if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
1308ab2a 14791+ goto out;
dece6358 14792+
c2b27bf2
AM
14793+ parent = dget_parent(cpg.dentry);
14794+ if (au_test_ro(sb, cpg.bdst, inode)) {
4a4d8108 14795+ di_read_lock_parent(parent, !AuLock_IR);
c2b27bf2
AM
14796+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14797+ cpg.bdst = err;
4a4d8108
AM
14798+ di_read_unlock(parent, !AuLock_IR);
14799+ if (unlikely(err < 0))
14800+ goto out_parent;
14801+ err = 0;
1facf9fc 14802+ }
1facf9fc 14803+
4a4d8108 14804+ di_read_lock_parent(parent, AuLock_IR);
c2b27bf2 14805+ hi_wh = au_hi_wh(inode, cpg.bdst);
7f207e10
AM
14806+ if (!S_ISDIR(inode->i_mode)
14807+ && au_opt_test(au_mntflags(sb), PLINK)
4a4d8108 14808+ && au_plink_test(inode)
c2b27bf2 14809+ && !d_unhashed(cpg.dentry)
5afbbe0d 14810+ && cpg.bdst < au_dbtop(cpg.dentry)) {
c2b27bf2 14811+ err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
4a4d8108
AM
14812+ if (unlikely(err))
14813+ goto out_unlock;
14814+
14815+ /* always superio. */
c2b27bf2 14816+ err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108 14817+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 14818+ if (!err) {
c2b27bf2 14819+ err = au_sio_cpup_simple(&cpg);
367653fa
AM
14820+ au_unpin(&pin);
14821+ }
4a4d8108
AM
14822+ } else if (hi_wh) {
14823+ /* already copied-up after unlink */
c2b27bf2 14824+ err = au_reopen_wh(file, cpg.bdst, hi_wh);
4a4d8108
AM
14825+ *need_reopen = 0;
14826+ }
1facf9fc 14827+
4f0767ce 14828+out_unlock:
4a4d8108 14829+ di_read_unlock(parent, AuLock_IR);
4f0767ce 14830+out_parent:
4a4d8108 14831+ dput(parent);
4f0767ce 14832+out:
1308ab2a 14833+ return err;
dece6358 14834+}
1facf9fc 14835+
4a4d8108 14836+static void au_do_refresh_dir(struct file *file)
dece6358 14837+{
5afbbe0d 14838+ aufs_bindex_t bindex, bbot, new_bindex, brid;
4a4d8108
AM
14839+ struct au_hfile *p, tmp, *q;
14840+ struct au_finfo *finfo;
1308ab2a 14841+ struct super_block *sb;
4a4d8108 14842+ struct au_fidir *fidir;
1facf9fc 14843+
4a4d8108 14844+ FiMustWriteLock(file);
1facf9fc 14845+
2000de60 14846+ sb = file->f_path.dentry->d_sb;
4a4d8108
AM
14847+ finfo = au_fi(file);
14848+ fidir = finfo->fi_hdir;
14849+ AuDebugOn(!fidir);
14850+ p = fidir->fd_hfile + finfo->fi_btop;
14851+ brid = p->hf_br->br_id;
5afbbe0d
AM
14852+ bbot = fidir->fd_bbot;
14853+ for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
4a4d8108
AM
14854+ if (!p->hf_file)
14855+ continue;
1308ab2a 14856+
4a4d8108
AM
14857+ new_bindex = au_br_index(sb, p->hf_br->br_id);
14858+ if (new_bindex == bindex)
14859+ continue;
14860+ if (new_bindex < 0) {
14861+ au_set_h_fptr(file, bindex, NULL);
14862+ continue;
14863+ }
1308ab2a 14864+
4a4d8108
AM
14865+ /* swap two lower inode, and loop again */
14866+ q = fidir->fd_hfile + new_bindex;
14867+ tmp = *q;
14868+ *q = *p;
14869+ *p = tmp;
14870+ if (tmp.hf_file) {
14871+ bindex--;
14872+ p--;
14873+ }
14874+ }
1308ab2a 14875+
4a4d8108 14876+ p = fidir->fd_hfile;
2000de60 14877+ if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
5afbbe0d
AM
14878+ bbot = au_sbbot(sb);
14879+ for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
4a4d8108
AM
14880+ finfo->fi_btop++, p++)
14881+ if (p->hf_file) {
c06a8ce3 14882+ if (file_inode(p->hf_file))
4a4d8108 14883+ break;
1c60b727 14884+ au_hfput(p, /*execed*/0);
4a4d8108
AM
14885+ }
14886+ } else {
5afbbe0d
AM
14887+ bbot = au_br_index(sb, brid);
14888+ for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
4a4d8108
AM
14889+ finfo->fi_btop++, p++)
14890+ if (p->hf_file)
1c60b727 14891+ au_hfput(p, /*execed*/0);
5afbbe0d 14892+ bbot = au_sbbot(sb);
4a4d8108 14893+ }
1308ab2a 14894+
5afbbe0d
AM
14895+ p = fidir->fd_hfile + bbot;
14896+ for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
4a4d8108
AM
14897+ fidir->fd_bbot--, p--)
14898+ if (p->hf_file) {
c06a8ce3 14899+ if (file_inode(p->hf_file))
4a4d8108 14900+ break;
1c60b727 14901+ au_hfput(p, /*execed*/0);
4a4d8108
AM
14902+ }
14903+ AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
1308ab2a 14904+}
14905+
4a4d8108
AM
14906+/*
14907+ * after branch manipulating, refresh the file.
14908+ */
14909+static int refresh_file(struct file *file, int (*reopen)(struct file *file))
1facf9fc 14910+{
e2f27e51 14911+ int err, need_reopen, nbr;
5afbbe0d 14912+ aufs_bindex_t bbot, bindex;
4a4d8108 14913+ struct dentry *dentry;
e2f27e51 14914+ struct super_block *sb;
1308ab2a 14915+ struct au_finfo *finfo;
4a4d8108 14916+ struct au_hfile *hfile;
1facf9fc 14917+
2000de60 14918+ dentry = file->f_path.dentry;
e2f27e51
AM
14919+ sb = dentry->d_sb;
14920+ nbr = au_sbbot(sb) + 1;
1308ab2a 14921+ finfo = au_fi(file);
4a4d8108
AM
14922+ if (!finfo->fi_hdir) {
14923+ hfile = &finfo->fi_htop;
14924+ AuDebugOn(!hfile->hf_file);
e2f27e51 14925+ bindex = au_br_index(sb, hfile->hf_br->br_id);
4a4d8108
AM
14926+ AuDebugOn(bindex < 0);
14927+ if (bindex != finfo->fi_btop)
5afbbe0d 14928+ au_set_fbtop(file, bindex);
4a4d8108 14929+ } else {
e2f27e51 14930+ err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
4a4d8108
AM
14931+ if (unlikely(err))
14932+ goto out;
14933+ au_do_refresh_dir(file);
14934+ }
1facf9fc 14935+
4a4d8108
AM
14936+ err = 0;
14937+ need_reopen = 1;
14938+ if (!au_test_mmapped(file))
14939+ err = au_file_refresh_by_inode(file, &need_reopen);
e2f27e51
AM
14940+ if (finfo->fi_hdir)
14941+ /* harmless if err */
14942+ au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
027c5e7a 14943+ if (!err && need_reopen && !d_unlinked(dentry))
4a4d8108
AM
14944+ err = reopen(file);
14945+ if (!err) {
14946+ au_update_figen(file);
14947+ goto out; /* success */
14948+ }
14949+
14950+ /* error, close all lower files */
14951+ if (finfo->fi_hdir) {
5afbbe0d
AM
14952+ bbot = au_fbbot_dir(file);
14953+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
4a4d8108
AM
14954+ au_set_h_fptr(file, bindex, NULL);
14955+ }
1facf9fc 14956+
4f0767ce 14957+out:
1facf9fc 14958+ return err;
14959+}
14960+
4a4d8108
AM
14961+/* common function to regular file and dir */
14962+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
521ced18 14963+ int wlock, unsigned int fi_lsc)
dece6358 14964+{
1308ab2a 14965+ int err;
4a4d8108 14966+ unsigned int sigen, figen;
5afbbe0d 14967+ aufs_bindex_t btop;
4a4d8108
AM
14968+ unsigned char pseudo_link;
14969+ struct dentry *dentry;
14970+ struct inode *inode;
1facf9fc 14971+
4a4d8108 14972+ err = 0;
2000de60 14973+ dentry = file->f_path.dentry;
5527c038 14974+ inode = d_inode(dentry);
4a4d8108 14975+ sigen = au_sigen(dentry->d_sb);
521ced18 14976+ fi_write_lock_nested(file, fi_lsc);
4a4d8108 14977+ figen = au_figen(file);
521ced18
JR
14978+ if (!fi_lsc)
14979+ di_write_lock_child(dentry);
14980+ else
14981+ di_write_lock_child2(dentry);
5afbbe0d
AM
14982+ btop = au_dbtop(dentry);
14983+ pseudo_link = (btop != au_ibtop(inode));
14984+ if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
4a4d8108
AM
14985+ if (!wlock) {
14986+ di_downgrade_lock(dentry, AuLock_IR);
14987+ fi_downgrade_lock(file);
14988+ }
14989+ goto out; /* success */
14990+ }
dece6358 14991+
4a4d8108 14992+ AuDbg("sigen %d, figen %d\n", sigen, figen);
027c5e7a 14993+ if (au_digen_test(dentry, sigen)) {
4a4d8108 14994+ err = au_reval_dpath(dentry, sigen);
027c5e7a 14995+ AuDebugOn(!err && au_digen_test(dentry, sigen));
4a4d8108 14996+ }
dece6358 14997+
027c5e7a
AM
14998+ if (!err)
14999+ err = refresh_file(file, reopen);
4a4d8108
AM
15000+ if (!err) {
15001+ if (!wlock) {
15002+ di_downgrade_lock(dentry, AuLock_IR);
15003+ fi_downgrade_lock(file);
15004+ }
15005+ } else {
15006+ di_write_unlock(dentry);
15007+ fi_write_unlock(file);
15008+ }
1facf9fc 15009+
4f0767ce 15010+out:
1308ab2a 15011+ return err;
15012+}
1facf9fc 15013+
4a4d8108
AM
15014+/* ---------------------------------------------------------------------- */
15015+
15016+/* cf. aufs_nopage() */
15017+/* for madvise(2) */
15018+static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
1308ab2a 15019+{
4a4d8108
AM
15020+ unlock_page(page);
15021+ return 0;
15022+}
1facf9fc 15023+
4a4d8108 15024+/* it will never be called, but necessary to support O_DIRECT */
5afbbe0d 15025+static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
4a4d8108 15026+{ BUG(); return 0; }
1facf9fc 15027+
4a4d8108
AM
15028+/* they will never be called. */
15029+#ifdef CONFIG_AUFS_DEBUG
15030+static int aufs_write_begin(struct file *file, struct address_space *mapping,
15031+ loff_t pos, unsigned len, unsigned flags,
15032+ struct page **pagep, void **fsdata)
15033+{ AuUnsupport(); return 0; }
15034+static int aufs_write_end(struct file *file, struct address_space *mapping,
15035+ loff_t pos, unsigned len, unsigned copied,
15036+ struct page *page, void *fsdata)
15037+{ AuUnsupport(); return 0; }
15038+static int aufs_writepage(struct page *page, struct writeback_control *wbc)
15039+{ AuUnsupport(); return 0; }
1308ab2a 15040+
4a4d8108
AM
15041+static int aufs_set_page_dirty(struct page *page)
15042+{ AuUnsupport(); return 0; }
392086de
AM
15043+static void aufs_invalidatepage(struct page *page, unsigned int offset,
15044+ unsigned int length)
4a4d8108
AM
15045+{ AuUnsupport(); }
15046+static int aufs_releasepage(struct page *page, gfp_t gfp)
15047+{ AuUnsupport(); return 0; }
79b8bda9 15048+#if 0 /* called by memory compaction regardless file */
4a4d8108 15049+static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
7eafdf33 15050+ struct page *page, enum migrate_mode mode)
4a4d8108 15051+{ AuUnsupport(); return 0; }
79b8bda9 15052+#endif
e2f27e51
AM
15053+static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
15054+{ AuUnsupport(); return true; }
15055+static void aufs_putback_page(struct page *page)
15056+{ AuUnsupport(); }
4a4d8108
AM
15057+static int aufs_launder_page(struct page *page)
15058+{ AuUnsupport(); return 0; }
15059+static int aufs_is_partially_uptodate(struct page *page,
38d290e6
JR
15060+ unsigned long from,
15061+ unsigned long count)
4a4d8108 15062+{ AuUnsupport(); return 0; }
392086de
AM
15063+static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
15064+ bool *writeback)
15065+{ AuUnsupport(); }
4a4d8108
AM
15066+static int aufs_error_remove_page(struct address_space *mapping,
15067+ struct page *page)
15068+{ AuUnsupport(); return 0; }
b4510431
AM
15069+static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
15070+ sector_t *span)
15071+{ AuUnsupport(); return 0; }
15072+static void aufs_swap_deactivate(struct file *file)
15073+{ AuUnsupport(); }
4a4d8108
AM
15074+#endif /* CONFIG_AUFS_DEBUG */
15075+
15076+const struct address_space_operations aufs_aop = {
15077+ .readpage = aufs_readpage,
15078+ .direct_IO = aufs_direct_IO,
4a4d8108
AM
15079+#ifdef CONFIG_AUFS_DEBUG
15080+ .writepage = aufs_writepage,
4a4d8108
AM
15081+ /* no writepages, because of writepage */
15082+ .set_page_dirty = aufs_set_page_dirty,
15083+ /* no readpages, because of readpage */
15084+ .write_begin = aufs_write_begin,
15085+ .write_end = aufs_write_end,
15086+ /* no bmap, no block device */
15087+ .invalidatepage = aufs_invalidatepage,
15088+ .releasepage = aufs_releasepage,
79b8bda9
AM
15089+ /* is fallback_migrate_page ok? */
15090+ /* .migratepage = aufs_migratepage, */
e2f27e51
AM
15091+ .isolate_page = aufs_isolate_page,
15092+ .putback_page = aufs_putback_page,
4a4d8108
AM
15093+ .launder_page = aufs_launder_page,
15094+ .is_partially_uptodate = aufs_is_partially_uptodate,
392086de 15095+ .is_dirty_writeback = aufs_is_dirty_writeback,
b4510431
AM
15096+ .error_remove_page = aufs_error_remove_page,
15097+ .swap_activate = aufs_swap_activate,
15098+ .swap_deactivate = aufs_swap_deactivate
4a4d8108 15099+#endif /* CONFIG_AUFS_DEBUG */
dece6358 15100+};
7f207e10
AM
15101diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
15102--- /usr/share/empty/fs/aufs/file.h 1970-01-01 01:00:00.000000000 +0100
9f237c51
AM
15103+++ linux/fs/aufs/file.h 2018-12-27 13:19:17.708416053 +0100
15104@@ -0,0 +1,342 @@
062440b3 15105+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 15106+/*
b00004a5 15107+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
15108+ *
15109+ * This program, aufs is free software; you can redistribute it and/or modify
15110+ * it under the terms of the GNU General Public License as published by
15111+ * the Free Software Foundation; either version 2 of the License, or
15112+ * (at your option) any later version.
15113+ *
15114+ * This program is distributed in the hope that it will be useful,
15115+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15116+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15117+ * GNU General Public License for more details.
15118+ *
15119+ * You should have received a copy of the GNU General Public License
523b37e3 15120+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 15121+ */
1facf9fc 15122+
4a4d8108
AM
15123+/*
15124+ * file operations
15125+ */
1facf9fc 15126+
4a4d8108
AM
15127+#ifndef __AUFS_FILE_H__
15128+#define __AUFS_FILE_H__
1facf9fc 15129+
4a4d8108 15130+#ifdef __KERNEL__
1facf9fc 15131+
2cbb1c4b 15132+#include <linux/file.h>
4a4d8108 15133+#include <linux/fs.h>
3c1bdaff 15134+#include <linux/mm_types.h>
4a4d8108 15135+#include <linux/poll.h>
4a4d8108 15136+#include "rwsem.h"
1facf9fc 15137+
4a4d8108
AM
15138+struct au_branch;
15139+struct au_hfile {
15140+ struct file *hf_file;
15141+ struct au_branch *hf_br;
15142+};
1facf9fc 15143+
4a4d8108
AM
15144+struct au_vdir;
15145+struct au_fidir {
15146+ aufs_bindex_t fd_bbot;
15147+ aufs_bindex_t fd_nent;
15148+ struct au_vdir *fd_vdir_cache;
15149+ struct au_hfile fd_hfile[];
15150+};
1facf9fc 15151+
4a4d8108 15152+static inline int au_fidir_sz(int nent)
dece6358 15153+{
4f0767ce
JR
15154+ AuDebugOn(nent < 0);
15155+ return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
4a4d8108 15156+}
1facf9fc 15157+
4a4d8108
AM
15158+struct au_finfo {
15159+ atomic_t fi_generation;
dece6358 15160+
4a4d8108
AM
15161+ struct au_rwsem fi_rwsem;
15162+ aufs_bindex_t fi_btop;
15163+
15164+ /* do not union them */
15165+ struct { /* for non-dir */
15166+ struct au_hfile fi_htop;
2cbb1c4b 15167+ atomic_t fi_mmapped;
4a4d8108
AM
15168+ };
15169+ struct au_fidir *fi_hdir; /* for dir only */
523b37e3 15170+
8b6a4947 15171+ struct hlist_bl_node fi_hlist;
1c60b727 15172+ struct file *fi_file; /* very ugly */
9f237c51 15173+ struct rcu_head rcu;
4a4d8108 15174+} ____cacheline_aligned_in_smp;
1facf9fc 15175+
4a4d8108 15176+/* ---------------------------------------------------------------------- */
1facf9fc 15177+
4a4d8108
AM
15178+/* file.c */
15179+extern const struct address_space_operations aufs_aop;
15180+unsigned int au_file_roflags(unsigned int flags);
15181+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 15182+ struct file *file, int force_wr);
b912730e 15183+struct au_do_open_args {
8b6a4947 15184+ int aopen;
b912730e
AM
15185+ int (*open)(struct file *file, int flags,
15186+ struct file *h_file);
15187+ struct au_fidir *fidir;
15188+ struct file *h_file;
15189+};
15190+int au_do_open(struct file *file, struct au_do_open_args *args);
4a4d8108
AM
15191+int au_reopen_nondir(struct file *file);
15192+struct au_pin;
15193+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15194+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
521ced18 15195+ int wlock, unsigned int fi_lsc);
4a4d8108
AM
15196+int au_do_flush(struct file *file, fl_owner_t id,
15197+ int (*flush)(struct file *file, fl_owner_t id));
1facf9fc 15198+
4a4d8108
AM
15199+/* poll.c */
15200+#ifdef CONFIG_AUFS_POLL
cd7a4cd9 15201+__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
4a4d8108 15202+#endif
1facf9fc 15203+
4a4d8108
AM
15204+#ifdef CONFIG_AUFS_BR_HFSPLUS
15205+/* hfsplus.c */
392086de
AM
15206+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15207+ int force_wr);
4a4d8108
AM
15208+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15209+ struct file *h_file);
15210+#else
c1595e42
JR
15211+AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15212+ aufs_bindex_t bindex, int force_wr)
4a4d8108
AM
15213+AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15214+ struct file *h_file);
15215+#endif
1facf9fc 15216+
4a4d8108
AM
15217+/* f_op.c */
15218+extern const struct file_operations aufs_file_fop;
b912730e 15219+int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
4a4d8108 15220+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
521ced18 15221+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
4a4d8108 15222+
4a4d8108 15223+/* finfo.c */
f0c0a007 15224+void au_hfput(struct au_hfile *hf, int execed);
4a4d8108
AM
15225+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15226+ struct file *h_file);
1facf9fc 15227+
4a4d8108 15228+void au_update_figen(struct file *file);
4a4d8108 15229+struct au_fidir *au_fidir_alloc(struct super_block *sb);
e2f27e51 15230+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
1facf9fc 15231+
4a4d8108 15232+void au_fi_init_once(void *_fi);
1c60b727 15233+void au_finfo_fin(struct file *file);
4a4d8108 15234+int au_finfo_init(struct file *file, struct au_fidir *fidir);
1facf9fc 15235+
4a4d8108
AM
15236+/* ioctl.c */
15237+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
15238+#ifdef CONFIG_COMPAT
15239+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15240+ unsigned long arg);
c2b27bf2
AM
15241+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15242+ unsigned long arg);
b752ccd1 15243+#endif
1facf9fc 15244+
4a4d8108 15245+/* ---------------------------------------------------------------------- */
1facf9fc 15246+
4a4d8108
AM
15247+static inline struct au_finfo *au_fi(struct file *file)
15248+{
38d290e6 15249+ return file->private_data;
4a4d8108 15250+}
1facf9fc 15251+
4a4d8108 15252+/* ---------------------------------------------------------------------- */
1facf9fc 15253+
8b6a4947
AM
15254+#define fi_read_lock(f) au_rw_read_lock(&au_fi(f)->fi_rwsem)
15255+#define fi_write_lock(f) au_rw_write_lock(&au_fi(f)->fi_rwsem)
15256+#define fi_read_trylock(f) au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15257+#define fi_write_trylock(f) au_rw_write_trylock(&au_fi(f)->fi_rwsem)
4a4d8108 15258+/*
8b6a4947
AM
15259+#define fi_read_trylock_nested(f) \
15260+ au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15261+#define fi_write_trylock_nested(f) \
15262+ au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15263+*/
15264+
15265+#define fi_read_unlock(f) au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15266+#define fi_write_unlock(f) au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15267+#define fi_downgrade_lock(f) au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
1308ab2a 15268+
521ced18
JR
15269+/* lock subclass for finfo */
15270+enum {
15271+ AuLsc_FI_1,
15272+ AuLsc_FI_2
15273+};
15274+
15275+static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15276+{
15277+ au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15278+}
15279+
15280+static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15281+{
15282+ au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15283+}
15284+
15285+/*
15286+ * fi_read_lock_1, fi_write_lock_1,
15287+ * fi_read_lock_2, fi_write_lock_2
15288+ */
15289+#define AuReadLockFunc(name) \
15290+static inline void fi_read_lock_##name(struct file *f) \
15291+{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15292+
15293+#define AuWriteLockFunc(name) \
15294+static inline void fi_write_lock_##name(struct file *f) \
15295+{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15296+
15297+#define AuRWLockFuncs(name) \
15298+ AuReadLockFunc(name) \
15299+ AuWriteLockFunc(name)
15300+
15301+AuRWLockFuncs(1);
15302+AuRWLockFuncs(2);
15303+
15304+#undef AuReadLockFunc
15305+#undef AuWriteLockFunc
15306+#undef AuRWLockFuncs
15307+
4a4d8108
AM
15308+#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15309+#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15310+#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
1facf9fc 15311+
1308ab2a 15312+/* ---------------------------------------------------------------------- */
15313+
4a4d8108 15314+/* todo: hard/soft set? */
5afbbe0d 15315+static inline aufs_bindex_t au_fbtop(struct file *file)
dece6358 15316+{
4a4d8108
AM
15317+ FiMustAnyLock(file);
15318+ return au_fi(file)->fi_btop;
15319+}
dece6358 15320+
5afbbe0d 15321+static inline aufs_bindex_t au_fbbot_dir(struct file *file)
4a4d8108
AM
15322+{
15323+ FiMustAnyLock(file);
15324+ AuDebugOn(!au_fi(file)->fi_hdir);
15325+ return au_fi(file)->fi_hdir->fd_bbot;
15326+}
1facf9fc 15327+
4a4d8108
AM
15328+static inline struct au_vdir *au_fvdir_cache(struct file *file)
15329+{
15330+ FiMustAnyLock(file);
15331+ AuDebugOn(!au_fi(file)->fi_hdir);
15332+ return au_fi(file)->fi_hdir->fd_vdir_cache;
15333+}
1facf9fc 15334+
5afbbe0d 15335+static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
15336+{
15337+ FiMustWriteLock(file);
15338+ au_fi(file)->fi_btop = bindex;
15339+}
1facf9fc 15340+
5afbbe0d 15341+static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
15342+{
15343+ FiMustWriteLock(file);
15344+ AuDebugOn(!au_fi(file)->fi_hdir);
15345+ au_fi(file)->fi_hdir->fd_bbot = bindex;
15346+}
1308ab2a 15347+
4a4d8108
AM
15348+static inline void au_set_fvdir_cache(struct file *file,
15349+ struct au_vdir *vdir_cache)
15350+{
15351+ FiMustWriteLock(file);
15352+ AuDebugOn(!au_fi(file)->fi_hdir);
15353+ au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15354+}
dece6358 15355+
4a4d8108
AM
15356+static inline struct file *au_hf_top(struct file *file)
15357+{
15358+ FiMustAnyLock(file);
15359+ AuDebugOn(au_fi(file)->fi_hdir);
15360+ return au_fi(file)->fi_htop.hf_file;
15361+}
1facf9fc 15362+
4a4d8108
AM
15363+static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15364+{
15365+ FiMustAnyLock(file);
15366+ AuDebugOn(!au_fi(file)->fi_hdir);
15367+ return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
dece6358
AM
15368+}
15369+
4a4d8108
AM
15370+/* todo: memory barrier? */
15371+static inline unsigned int au_figen(struct file *f)
dece6358 15372+{
4a4d8108
AM
15373+ return atomic_read(&au_fi(f)->fi_generation);
15374+}
dece6358 15375+
2cbb1c4b
JR
15376+static inline void au_set_mmapped(struct file *f)
15377+{
15378+ if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15379+ return;
0c3ec466 15380+ pr_warn("fi_mmapped wrapped around\n");
2cbb1c4b
JR
15381+ while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15382+ ;
15383+}
15384+
15385+static inline void au_unset_mmapped(struct file *f)
15386+{
15387+ atomic_dec(&au_fi(f)->fi_mmapped);
15388+}
15389+
4a4d8108
AM
15390+static inline int au_test_mmapped(struct file *f)
15391+{
2cbb1c4b
JR
15392+ return atomic_read(&au_fi(f)->fi_mmapped);
15393+}
15394+
15395+/* customize vma->vm_file */
15396+
15397+static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15398+ struct file *file)
15399+{
53392da6
AM
15400+ struct file *f;
15401+
15402+ f = vma->vm_file;
2cbb1c4b
JR
15403+ get_file(file);
15404+ vma->vm_file = file;
53392da6 15405+ fput(f);
2cbb1c4b
JR
15406+}
15407+
15408+#ifdef CONFIG_MMU
15409+#define AuDbgVmRegion(file, vma) do {} while (0)
15410+
15411+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15412+ struct file *file)
15413+{
15414+ au_do_vm_file_reset(vma, file);
15415+}
15416+#else
15417+#define AuDbgVmRegion(file, vma) \
15418+ AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15419+
15420+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15421+ struct file *file)
15422+{
53392da6
AM
15423+ struct file *f;
15424+
2cbb1c4b 15425+ au_do_vm_file_reset(vma, file);
53392da6 15426+ f = vma->vm_region->vm_file;
2cbb1c4b
JR
15427+ get_file(file);
15428+ vma->vm_region->vm_file = file;
53392da6 15429+ fput(f);
2cbb1c4b
JR
15430+}
15431+#endif /* CONFIG_MMU */
15432+
15433+/* handle vma->vm_prfile */
fb47a38f 15434+static inline void au_vm_prfile_set(struct vm_area_struct *vma,
2cbb1c4b
JR
15435+ struct file *file)
15436+{
2cbb1c4b
JR
15437+ get_file(file);
15438+ vma->vm_prfile = file;
15439+#ifndef CONFIG_MMU
15440+ get_file(file);
15441+ vma->vm_region->vm_prfile = file;
15442+#endif
fb47a38f 15443+}
1308ab2a 15444+
4a4d8108
AM
15445+#endif /* __KERNEL__ */
15446+#endif /* __AUFS_FILE_H__ */
7f207e10
AM
15447diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15448--- /usr/share/empty/fs/aufs/finfo.c 1970-01-01 01:00:00.000000000 +0100
acd2b654 15449+++ linux/fs/aufs/finfo.c 2018-10-23 12:33:35.596042364 +0200
062440b3 15450@@ -0,0 +1,149 @@
cd7a4cd9 15451+// SPDX-License-Identifier: GPL-2.0
4a4d8108 15452+/*
b00004a5 15453+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
15454+ *
15455+ * This program, aufs is free software; you can redistribute it and/or modify
15456+ * it under the terms of the GNU General Public License as published by
15457+ * the Free Software Foundation; either version 2 of the License, or
15458+ * (at your option) any later version.
15459+ *
15460+ * This program is distributed in the hope that it will be useful,
15461+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15462+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15463+ * GNU General Public License for more details.
15464+ *
15465+ * You should have received a copy of the GNU General Public License
523b37e3 15466+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 15467+ */
1308ab2a 15468+
4a4d8108
AM
15469+/*
15470+ * file private data
15471+ */
1facf9fc 15472+
4a4d8108 15473+#include "aufs.h"
1facf9fc 15474+
f0c0a007 15475+void au_hfput(struct au_hfile *hf, int execed)
4a4d8108 15476+{
f0c0a007 15477+ if (execed)
4a4d8108
AM
15478+ allow_write_access(hf->hf_file);
15479+ fput(hf->hf_file);
15480+ hf->hf_file = NULL;
acd2b654 15481+ au_lcnt_dec(&hf->hf_br->br_nfiles);
4a4d8108
AM
15482+ hf->hf_br = NULL;
15483+}
1facf9fc 15484+
4a4d8108
AM
15485+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15486+{
15487+ struct au_finfo *finfo = au_fi(file);
15488+ struct au_hfile *hf;
15489+ struct au_fidir *fidir;
15490+
15491+ fidir = finfo->fi_hdir;
15492+ if (!fidir) {
15493+ AuDebugOn(finfo->fi_btop != bindex);
15494+ hf = &finfo->fi_htop;
15495+ } else
15496+ hf = fidir->fd_hfile + bindex;
15497+
15498+ if (hf && hf->hf_file)
f0c0a007 15499+ au_hfput(hf, vfsub_file_execed(file));
4a4d8108
AM
15500+ if (val) {
15501+ FiMustWriteLock(file);
b912730e 15502+ AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
4a4d8108 15503+ hf->hf_file = val;
2000de60 15504+ hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
1308ab2a 15505+ }
4a4d8108 15506+}
1facf9fc 15507+
4a4d8108
AM
15508+void au_update_figen(struct file *file)
15509+{
2000de60 15510+ atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
4a4d8108 15511+ /* smp_mb(); */ /* atomic_set */
1facf9fc 15512+}
15513+
4a4d8108
AM
15514+/* ---------------------------------------------------------------------- */
15515+
4a4d8108
AM
15516+struct au_fidir *au_fidir_alloc(struct super_block *sb)
15517+{
15518+ struct au_fidir *fidir;
15519+ int nbr;
15520+
5afbbe0d 15521+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
15522+ if (nbr < 2)
15523+ nbr = 2; /* initial allocate for 2 branches */
15524+ fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15525+ if (fidir) {
15526+ fidir->fd_bbot = -1;
15527+ fidir->fd_nent = nbr;
4a4d8108
AM
15528+ }
15529+
15530+ return fidir;
15531+}
15532+
e2f27e51 15533+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
4a4d8108
AM
15534+{
15535+ int err;
15536+ struct au_fidir *fidir, *p;
15537+
15538+ AuRwMustWriteLock(&finfo->fi_rwsem);
15539+ fidir = finfo->fi_hdir;
15540+ AuDebugOn(!fidir);
15541+
15542+ err = -ENOMEM;
15543+ p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
e2f27e51 15544+ GFP_NOFS, may_shrink);
4a4d8108
AM
15545+ if (p) {
15546+ p->fd_nent = nbr;
15547+ finfo->fi_hdir = p;
15548+ err = 0;
15549+ }
1facf9fc 15550+
dece6358 15551+ return err;
1facf9fc 15552+}
1308ab2a 15553+
15554+/* ---------------------------------------------------------------------- */
15555+
1c60b727 15556+void au_finfo_fin(struct file *file)
1308ab2a 15557+{
4a4d8108
AM
15558+ struct au_finfo *finfo;
15559+
acd2b654 15560+ au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
7f207e10 15561+
4a4d8108
AM
15562+ finfo = au_fi(file);
15563+ AuDebugOn(finfo->fi_hdir);
15564+ AuRwDestroy(&finfo->fi_rwsem);
1c60b727 15565+ au_cache_free_finfo(finfo);
1308ab2a 15566+}
1308ab2a 15567+
e49829fe 15568+void au_fi_init_once(void *_finfo)
4a4d8108 15569+{
e49829fe 15570+ struct au_finfo *finfo = _finfo;
1308ab2a 15571+
e49829fe 15572+ au_rw_init(&finfo->fi_rwsem);
4a4d8108 15573+}
1308ab2a 15574+
4a4d8108
AM
15575+int au_finfo_init(struct file *file, struct au_fidir *fidir)
15576+{
1716fcea 15577+ int err;
4a4d8108
AM
15578+ struct au_finfo *finfo;
15579+ struct dentry *dentry;
15580+
15581+ err = -ENOMEM;
2000de60 15582+ dentry = file->f_path.dentry;
4a4d8108
AM
15583+ finfo = au_cache_alloc_finfo();
15584+ if (unlikely(!finfo))
15585+ goto out;
15586+
15587+ err = 0;
acd2b654 15588+ au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
4a4d8108
AM
15589+ au_rw_write_lock(&finfo->fi_rwsem);
15590+ finfo->fi_btop = -1;
15591+ finfo->fi_hdir = fidir;
15592+ atomic_set(&finfo->fi_generation, au_digen(dentry));
15593+ /* smp_mb(); */ /* atomic_set */
15594+
15595+ file->private_data = finfo;
15596+
15597+out:
15598+ return err;
15599+}
7f207e10
AM
15600diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15601--- /usr/share/empty/fs/aufs/f_op.c 1970-01-01 01:00:00.000000000 +0100
acd2b654
AM
15602+++ linux/fs/aufs/f_op.c 2018-10-23 12:33:35.596042364 +0200
15603@@ -0,0 +1,819 @@
cd7a4cd9 15604+// SPDX-License-Identifier: GPL-2.0
dece6358 15605+/*
b00004a5 15606+ * Copyright (C) 2005-2018 Junjiro R. Okajima
dece6358
AM
15607+ *
15608+ * This program, aufs is free software; you can redistribute it and/or modify
15609+ * it under the terms of the GNU General Public License as published by
15610+ * the Free Software Foundation; either version 2 of the License, or
15611+ * (at your option) any later version.
15612+ *
15613+ * This program is distributed in the hope that it will be useful,
15614+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15615+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15616+ * GNU General Public License for more details.
15617+ *
15618+ * You should have received a copy of the GNU General Public License
523b37e3 15619+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 15620+ */
1facf9fc 15621+
15622+/*
4a4d8108 15623+ * file and vm operations
1facf9fc 15624+ */
dece6358 15625+
86dc4139 15626+#include <linux/aio.h>
4a4d8108
AM
15627+#include <linux/fs_stack.h>
15628+#include <linux/mman.h>
4a4d8108 15629+#include <linux/security.h>
dece6358
AM
15630+#include "aufs.h"
15631+
b912730e 15632+int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
1facf9fc 15633+{
4a4d8108
AM
15634+ int err;
15635+ aufs_bindex_t bindex;
8cdd5066 15636+ struct dentry *dentry, *h_dentry;
4a4d8108 15637+ struct au_finfo *finfo;
38d290e6 15638+ struct inode *h_inode;
4a4d8108
AM
15639+
15640+ FiMustWriteLock(file);
15641+
523b37e3 15642+ err = 0;
2000de60 15643+ dentry = file->f_path.dentry;
b912730e 15644+ AuDebugOn(IS_ERR_OR_NULL(dentry));
4a4d8108
AM
15645+ finfo = au_fi(file);
15646+ memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
2cbb1c4b 15647+ atomic_set(&finfo->fi_mmapped, 0);
5afbbe0d 15648+ bindex = au_dbtop(dentry);
8cdd5066
JR
15649+ if (!h_file) {
15650+ h_dentry = au_h_dptr(dentry, bindex);
15651+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15652+ if (unlikely(err))
15653+ goto out;
b912730e 15654+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
acd2b654
AM
15655+ if (IS_ERR(h_file)) {
15656+ err = PTR_ERR(h_file);
15657+ goto out;
15658+ }
8cdd5066
JR
15659+ } else {
15660+ h_dentry = h_file->f_path.dentry;
15661+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15662+ if (unlikely(err))
15663+ goto out;
acd2b654 15664+ /* br ref is already inc-ed */
8cdd5066 15665+ }
acd2b654
AM
15666+
15667+ if ((flags & __O_TMPFILE)
15668+ && !(flags & O_EXCL)) {
15669+ h_inode = file_inode(h_file);
15670+ spin_lock(&h_inode->i_lock);
15671+ h_inode->i_state |= I_LINKABLE;
15672+ spin_unlock(&h_inode->i_lock);
4a4d8108 15673+ }
acd2b654
AM
15674+ au_set_fbtop(file, bindex);
15675+ au_set_h_fptr(file, bindex, h_file);
15676+ au_update_figen(file);
15677+ /* todo: necessary? */
15678+ /* file->f_ra = h_file->f_ra; */
027c5e7a 15679+
8cdd5066 15680+out:
4a4d8108 15681+ return err;
1facf9fc 15682+}
15683+
4a4d8108
AM
15684+static int aufs_open_nondir(struct inode *inode __maybe_unused,
15685+ struct file *file)
1facf9fc 15686+{
4a4d8108 15687+ int err;
1308ab2a 15688+ struct super_block *sb;
b912730e
AM
15689+ struct au_do_open_args args = {
15690+ .open = au_do_open_nondir
15691+ };
1facf9fc 15692+
523b37e3
AM
15693+ AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15694+ file, vfsub_file_flags(file), file->f_mode);
1facf9fc 15695+
2000de60 15696+ sb = file->f_path.dentry->d_sb;
4a4d8108 15697+ si_read_lock(sb, AuLock_FLUSH);
b912730e 15698+ err = au_do_open(file, &args);
4a4d8108
AM
15699+ si_read_unlock(sb);
15700+ return err;
15701+}
1facf9fc 15702+
4a4d8108
AM
15703+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15704+{
15705+ struct au_finfo *finfo;
15706+ aufs_bindex_t bindex;
1facf9fc 15707+
4a4d8108 15708+ finfo = au_fi(file);
8b6a4947
AM
15709+ au_hbl_del(&finfo->fi_hlist,
15710+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108 15711+ bindex = finfo->fi_btop;
b4510431 15712+ if (bindex >= 0)
4a4d8108 15713+ au_set_h_fptr(file, bindex, NULL);
7f207e10 15714+
1c60b727 15715+ au_finfo_fin(file);
4a4d8108 15716+ return 0;
1facf9fc 15717+}
15718+
4a4d8108
AM
15719+/* ---------------------------------------------------------------------- */
15720+
15721+static int au_do_flush_nondir(struct file *file, fl_owner_t id)
dece6358 15722+{
1308ab2a 15723+ int err;
4a4d8108
AM
15724+ struct file *h_file;
15725+
15726+ err = 0;
15727+ h_file = au_hf_top(file);
15728+ if (h_file)
15729+ err = vfsub_flush(h_file, id);
15730+ return err;
15731+}
15732+
15733+static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15734+{
15735+ return au_do_flush(file, id, au_do_flush_nondir);
15736+}
15737+
15738+/* ---------------------------------------------------------------------- */
9dbd164d
AM
15739+/*
15740+ * read and write functions acquire [fdi]_rwsem once, but release before
15741+ * mmap_sem. This is because to stop a race condition between mmap(2).
acd2b654 15742+ * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
9dbd164d
AM
15743+ * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15744+ * read functions after [fdi]_rwsem are released, but it should be harmless.
15745+ */
4a4d8108 15746+
b912730e 15747+/* Callers should call au_read_post() or fput() in the end */
521ced18 15748+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
4a4d8108 15749+{
4a4d8108 15750+ struct file *h_file;
b912730e 15751+ int err;
1facf9fc 15752+
521ced18 15753+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
b912730e
AM
15754+ if (!err) {
15755+ di_read_unlock(file->f_path.dentry, AuLock_IR);
15756+ h_file = au_hf_top(file);
15757+ get_file(h_file);
15758+ if (!keep_fi)
15759+ fi_read_unlock(file);
15760+ } else
15761+ h_file = ERR_PTR(err);
15762+
15763+ return h_file;
15764+}
15765+
15766+static void au_read_post(struct inode *inode, struct file *h_file)
15767+{
15768+ /* update without lock, I don't think it a problem */
15769+ fsstack_copy_attr_atime(inode, file_inode(h_file));
15770+ fput(h_file);
15771+}
15772+
15773+struct au_write_pre {
521ced18
JR
15774+ /* input */
15775+ unsigned int lsc;
15776+
15777+ /* output */
b912730e 15778+ blkcnt_t blks;
5afbbe0d 15779+ aufs_bindex_t btop;
b912730e
AM
15780+};
15781+
15782+/*
15783+ * return with iinfo is write-locked
15784+ * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15785+ * end
15786+ */
15787+static struct file *au_write_pre(struct file *file, int do_ready,
15788+ struct au_write_pre *wpre)
15789+{
15790+ struct file *h_file;
15791+ struct dentry *dentry;
15792+ int err;
521ced18 15793+ unsigned int lsc;
b912730e
AM
15794+ struct au_pin pin;
15795+
521ced18
JR
15796+ lsc = 0;
15797+ if (wpre)
15798+ lsc = wpre->lsc;
15799+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
b912730e 15800+ h_file = ERR_PTR(err);
dece6358
AM
15801+ if (unlikely(err))
15802+ goto out;
1facf9fc 15803+
b912730e
AM
15804+ dentry = file->f_path.dentry;
15805+ if (do_ready) {
15806+ err = au_ready_to_write(file, -1, &pin);
15807+ if (unlikely(err)) {
15808+ h_file = ERR_PTR(err);
15809+ di_write_unlock(dentry);
15810+ goto out_fi;
15811+ }
15812+ }
15813+
15814+ di_downgrade_lock(dentry, /*flags*/0);
15815+ if (wpre)
5afbbe0d 15816+ wpre->btop = au_fbtop(file);
4a4d8108 15817+ h_file = au_hf_top(file);
9dbd164d 15818+ get_file(h_file);
b912730e
AM
15819+ if (wpre)
15820+ wpre->blks = file_inode(h_file)->i_blocks;
15821+ if (do_ready)
15822+ au_unpin(&pin);
15823+ di_read_unlock(dentry, /*flags*/0);
15824+
15825+out_fi:
15826+ fi_write_unlock(file);
15827+out:
15828+ return h_file;
15829+}
15830+
15831+static void au_write_post(struct inode *inode, struct file *h_file,
15832+ struct au_write_pre *wpre, ssize_t written)
15833+{
15834+ struct inode *h_inode;
15835+
15836+ au_cpup_attr_timesizes(inode);
5afbbe0d 15837+ AuDebugOn(au_ibtop(inode) != wpre->btop);
b912730e
AM
15838+ h_inode = file_inode(h_file);
15839+ inode->i_mode = h_inode->i_mode;
15840+ ii_write_unlock(inode);
b912730e
AM
15841+ /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15842+ if (written > 0)
5afbbe0d 15843+ au_fhsm_wrote(inode->i_sb, wpre->btop,
b912730e 15844+ /*force*/h_inode->i_blocks > wpre->blks);
1c60b727 15845+ fput(h_file);
b912730e
AM
15846+}
15847+
15848+static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
15849+ loff_t *ppos)
15850+{
15851+ ssize_t err;
15852+ struct inode *inode;
15853+ struct file *h_file;
15854+ struct super_block *sb;
15855+
15856+ inode = file_inode(file);
15857+ sb = inode->i_sb;
15858+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15859+
521ced18 15860+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
15861+ err = PTR_ERR(h_file);
15862+ if (IS_ERR(h_file))
15863+ goto out;
9dbd164d
AM
15864+
15865+ /* filedata may be obsoleted by concurrent copyup, but no problem */
4a4d8108
AM
15866+ err = vfsub_read_u(h_file, buf, count, ppos);
15867+ /* todo: necessary? */
15868+ /* file->f_ra = h_file->f_ra; */
b912730e 15869+ au_read_post(inode, h_file);
1308ab2a 15870+
4f0767ce 15871+out:
dece6358
AM
15872+ si_read_unlock(sb);
15873+ return err;
15874+}
1facf9fc 15875+
e49829fe
JR
15876+/*
15877+ * todo: very ugly
15878+ * it locks both of i_mutex and si_rwsem for read in safe.
15879+ * if the plink maintenance mode continues forever (that is the problem),
15880+ * may loop forever.
15881+ */
15882+static void au_mtx_and_read_lock(struct inode *inode)
15883+{
15884+ int err;
15885+ struct super_block *sb = inode->i_sb;
15886+
15887+ while (1) {
febd17d6 15888+ inode_lock(inode);
e49829fe
JR
15889+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15890+ if (!err)
15891+ break;
febd17d6 15892+ inode_unlock(inode);
e49829fe
JR
15893+ si_read_lock(sb, AuLock_NOPLMW);
15894+ si_read_unlock(sb);
15895+ }
15896+}
15897+
4a4d8108
AM
15898+static ssize_t aufs_write(struct file *file, const char __user *ubuf,
15899+ size_t count, loff_t *ppos)
dece6358 15900+{
4a4d8108 15901+ ssize_t err;
b912730e
AM
15902+ struct au_write_pre wpre;
15903+ struct inode *inode;
4a4d8108
AM
15904+ struct file *h_file;
15905+ char __user *buf = (char __user *)ubuf;
1facf9fc 15906+
b912730e 15907+ inode = file_inode(file);
e49829fe 15908+ au_mtx_and_read_lock(inode);
1facf9fc 15909+
521ced18 15910+ wpre.lsc = 0;
b912730e
AM
15911+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15912+ err = PTR_ERR(h_file);
15913+ if (IS_ERR(h_file))
9dbd164d 15914+ goto out;
9dbd164d 15915+
4a4d8108 15916+ err = vfsub_write_u(h_file, buf, count, ppos);
b912730e 15917+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15918+
4f0767ce 15919+out:
b912730e 15920+ si_read_unlock(inode->i_sb);
febd17d6 15921+ inode_unlock(inode);
dece6358
AM
15922+ return err;
15923+}
1facf9fc 15924+
076b876e
AM
15925+static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15926+ struct iov_iter *iov_iter)
dece6358 15927+{
4a4d8108
AM
15928+ ssize_t err;
15929+ struct file *file;
076b876e 15930+ ssize_t (*iter)(struct kiocb *, struct iov_iter *);
1facf9fc 15931+
4a4d8108
AM
15932+ err = security_file_permission(h_file, rw);
15933+ if (unlikely(err))
15934+ goto out;
1facf9fc 15935+
4a4d8108 15936+ err = -ENOSYS;
076b876e 15937+ iter = NULL;
5527c038 15938+ if (rw == MAY_READ)
076b876e 15939+ iter = h_file->f_op->read_iter;
5527c038 15940+ else if (rw == MAY_WRITE)
076b876e 15941+ iter = h_file->f_op->write_iter;
076b876e
AM
15942+
15943+ file = kio->ki_filp;
15944+ kio->ki_filp = h_file;
15945+ if (iter) {
2cbb1c4b 15946+ lockdep_off();
076b876e
AM
15947+ err = iter(kio, iov_iter);
15948+ lockdep_on();
4a4d8108
AM
15949+ } else
15950+ /* currently there is no such fs */
15951+ WARN_ON_ONCE(1);
076b876e 15952+ kio->ki_filp = file;
1facf9fc 15953+
4f0767ce 15954+out:
dece6358
AM
15955+ return err;
15956+}
1facf9fc 15957+
076b876e 15958+static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1facf9fc 15959+{
4a4d8108
AM
15960+ ssize_t err;
15961+ struct file *file, *h_file;
b912730e 15962+ struct inode *inode;
dece6358 15963+ struct super_block *sb;
1facf9fc 15964+
4a4d8108 15965+ file = kio->ki_filp;
b912730e
AM
15966+ inode = file_inode(file);
15967+ sb = inode->i_sb;
e49829fe 15968+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 15969+
521ced18 15970+ h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
b912730e
AM
15971+ err = PTR_ERR(h_file);
15972+ if (IS_ERR(h_file))
15973+ goto out;
9dbd164d 15974+
5afbbe0d
AM
15975+ if (au_test_loopback_kthread()) {
15976+ au_warn_loopback(h_file->f_path.dentry->d_sb);
15977+ if (file->f_mapping != h_file->f_mapping) {
15978+ file->f_mapping = h_file->f_mapping;
15979+ smp_mb(); /* unnecessary? */
15980+ }
15981+ }
15982+ fi_read_unlock(file);
15983+
076b876e 15984+ err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
4a4d8108
AM
15985+ /* todo: necessary? */
15986+ /* file->f_ra = h_file->f_ra; */
b912730e 15987+ au_read_post(inode, h_file);
1facf9fc 15988+
4f0767ce 15989+out:
4a4d8108 15990+ si_read_unlock(sb);
1308ab2a 15991+ return err;
15992+}
1facf9fc 15993+
076b876e 15994+static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1308ab2a 15995+{
4a4d8108 15996+ ssize_t err;
b912730e
AM
15997+ struct au_write_pre wpre;
15998+ struct inode *inode;
4a4d8108 15999+ struct file *file, *h_file;
1308ab2a 16000+
4a4d8108 16001+ file = kio->ki_filp;
b912730e 16002+ inode = file_inode(file);
e49829fe
JR
16003+ au_mtx_and_read_lock(inode);
16004+
521ced18 16005+ wpre.lsc = 0;
b912730e
AM
16006+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16007+ err = PTR_ERR(h_file);
16008+ if (IS_ERR(h_file))
9dbd164d 16009+ goto out;
9dbd164d 16010+
076b876e 16011+ err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
b912730e 16012+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 16013+
4f0767ce 16014+out:
b912730e 16015+ si_read_unlock(inode->i_sb);
febd17d6 16016+ inode_unlock(inode);
dece6358 16017+ return err;
1facf9fc 16018+}
16019+
4a4d8108
AM
16020+static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
16021+ struct pipe_inode_info *pipe, size_t len,
16022+ unsigned int flags)
1facf9fc 16023+{
4a4d8108
AM
16024+ ssize_t err;
16025+ struct file *h_file;
b912730e 16026+ struct inode *inode;
dece6358 16027+ struct super_block *sb;
1facf9fc 16028+
b912730e
AM
16029+ inode = file_inode(file);
16030+ sb = inode->i_sb;
e49829fe 16031+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 16032+
521ced18 16033+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
16034+ err = PTR_ERR(h_file);
16035+ if (IS_ERR(h_file))
dece6358 16036+ goto out;
1facf9fc 16037+
4a4d8108 16038+ err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
acd2b654 16039+ /* todo: necessary? */
4a4d8108 16040+ /* file->f_ra = h_file->f_ra; */
b912730e 16041+ au_read_post(inode, h_file);
1facf9fc 16042+
4f0767ce 16043+out:
4a4d8108 16044+ si_read_unlock(sb);
dece6358 16045+ return err;
1facf9fc 16046+}
16047+
4a4d8108
AM
16048+static ssize_t
16049+aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
16050+ size_t len, unsigned int flags)
1facf9fc 16051+{
4a4d8108 16052+ ssize_t err;
b912730e
AM
16053+ struct au_write_pre wpre;
16054+ struct inode *inode;
076b876e 16055+ struct file *h_file;
1facf9fc 16056+
b912730e 16057+ inode = file_inode(file);
e49829fe 16058+ au_mtx_and_read_lock(inode);
9dbd164d 16059+
521ced18 16060+ wpre.lsc = 0;
b912730e
AM
16061+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16062+ err = PTR_ERR(h_file);
16063+ if (IS_ERR(h_file))
9dbd164d 16064+ goto out;
9dbd164d 16065+
4a4d8108 16066+ err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
b912730e 16067+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 16068+
4f0767ce 16069+out:
b912730e 16070+ si_read_unlock(inode->i_sb);
febd17d6 16071+ inode_unlock(inode);
4a4d8108
AM
16072+ return err;
16073+}
1facf9fc 16074+
38d290e6
JR
16075+static long aufs_fallocate(struct file *file, int mode, loff_t offset,
16076+ loff_t len)
16077+{
16078+ long err;
b912730e 16079+ struct au_write_pre wpre;
38d290e6
JR
16080+ struct inode *inode;
16081+ struct file *h_file;
16082+
b912730e 16083+ inode = file_inode(file);
38d290e6
JR
16084+ au_mtx_and_read_lock(inode);
16085+
521ced18 16086+ wpre.lsc = 0;
b912730e
AM
16087+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16088+ err = PTR_ERR(h_file);
16089+ if (IS_ERR(h_file))
38d290e6 16090+ goto out;
38d290e6
JR
16091+
16092+ lockdep_off();
03673fb0 16093+ err = vfs_fallocate(h_file, mode, offset, len);
38d290e6 16094+ lockdep_on();
b912730e 16095+ au_write_post(inode, h_file, &wpre, /*written*/1);
38d290e6
JR
16096+
16097+out:
b912730e 16098+ si_read_unlock(inode->i_sb);
febd17d6 16099+ inode_unlock(inode);
38d290e6
JR
16100+ return err;
16101+}
16102+
521ced18
JR
16103+static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
16104+ struct file *dst, loff_t dst_pos,
16105+ size_t len, unsigned int flags)
16106+{
16107+ ssize_t err;
16108+ struct au_write_pre wpre;
16109+ enum { SRC, DST };
16110+ struct {
16111+ struct inode *inode;
16112+ struct file *h_file;
16113+ struct super_block *h_sb;
16114+ } a[2];
16115+#define a_src a[SRC]
16116+#define a_dst a[DST]
16117+
16118+ err = -EINVAL;
16119+ a_src.inode = file_inode(src);
16120+ if (unlikely(!S_ISREG(a_src.inode->i_mode)))
16121+ goto out;
16122+ a_dst.inode = file_inode(dst);
16123+ if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
16124+ goto out;
16125+
16126+ au_mtx_and_read_lock(a_dst.inode);
16127+ /*
16128+ * in order to match the order in di_write_lock2_{child,parent}(),
acd2b654 16129+ * use f_path.dentry for this comparison.
521ced18
JR
16130+ */
16131+ if (src->f_path.dentry < dst->f_path.dentry) {
16132+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
16133+ err = PTR_ERR(a_src.h_file);
16134+ if (IS_ERR(a_src.h_file))
16135+ goto out_si;
16136+
16137+ wpre.lsc = AuLsc_FI_2;
16138+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16139+ err = PTR_ERR(a_dst.h_file);
16140+ if (IS_ERR(a_dst.h_file)) {
16141+ au_read_post(a_src.inode, a_src.h_file);
16142+ goto out_si;
16143+ }
16144+ } else {
16145+ wpre.lsc = AuLsc_FI_1;
16146+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16147+ err = PTR_ERR(a_dst.h_file);
16148+ if (IS_ERR(a_dst.h_file))
16149+ goto out_si;
16150+
16151+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
16152+ err = PTR_ERR(a_src.h_file);
16153+ if (IS_ERR(a_src.h_file)) {
16154+ au_write_post(a_dst.inode, a_dst.h_file, &wpre,
16155+ /*written*/0);
16156+ goto out_si;
16157+ }
16158+ }
16159+
16160+ err = -EXDEV;
16161+ a_src.h_sb = file_inode(a_src.h_file)->i_sb;
16162+ a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
16163+ if (unlikely(a_src.h_sb != a_dst.h_sb)) {
16164+ AuDbgFile(src);
16165+ AuDbgFile(dst);
16166+ goto out_file;
16167+ }
16168+
16169+ err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
16170+ dst_pos, len, flags);
16171+
16172+out_file:
16173+ au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
16174+ fi_read_unlock(src);
16175+ au_read_post(a_src.inode, a_src.h_file);
16176+out_si:
16177+ si_read_unlock(a_dst.inode->i_sb);
16178+ inode_unlock(a_dst.inode);
16179+out:
16180+ return err;
16181+#undef a_src
16182+#undef a_dst
16183+}
16184+
4a4d8108
AM
16185+/* ---------------------------------------------------------------------- */
16186+
9dbd164d
AM
16187+/*
16188+ * The locking order around current->mmap_sem.
16189+ * - in most and regular cases
16190+ * file I/O syscall -- aufs_read() or something
16191+ * -- si_rwsem for read -- mmap_sem
16192+ * (Note that [fdi]i_rwsem are released before mmap_sem).
16193+ * - in mmap case
16194+ * mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
acd2b654
AM
16195+ * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
16196+ * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
16197+ * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
9dbd164d
AM
16198+ * It means that when aufs acquires si_rwsem for write, the process should never
16199+ * acquire mmap_sem.
16200+ *
392086de 16201+ * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
9dbd164d
AM
16202+ * problem either since any directory is not able to be mmap-ed.
16203+ * The similar scenario is applied to aufs_readlink() too.
16204+ */
16205+
38d290e6 16206+#if 0 /* stop calling security_file_mmap() */
2dfbb274
AM
16207+/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
16208+#define AuConv_VM_PROT(f, b) _calc_vm_trans(f, VM_##b, PROT_##b)
16209+
16210+static unsigned long au_arch_prot_conv(unsigned long flags)
16211+{
16212+ /* currently ppc64 only */
16213+#ifdef CONFIG_PPC64
16214+ /* cf. linux/arch/powerpc/include/asm/mman.h */
16215+ AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
16216+ return AuConv_VM_PROT(flags, SAO);
16217+#else
16218+ AuDebugOn(arch_calc_vm_prot_bits(-1));
16219+ return 0;
16220+#endif
16221+}
16222+
16223+static unsigned long au_prot_conv(unsigned long flags)
16224+{
16225+ return AuConv_VM_PROT(flags, READ)
16226+ | AuConv_VM_PROT(flags, WRITE)
16227+ | AuConv_VM_PROT(flags, EXEC)
16228+ | au_arch_prot_conv(flags);
16229+}
16230+
16231+/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16232+#define AuConv_VM_MAP(f, b) _calc_vm_trans(f, VM_##b, MAP_##b)
16233+
16234+static unsigned long au_flag_conv(unsigned long flags)
16235+{
16236+ return AuConv_VM_MAP(flags, GROWSDOWN)
16237+ | AuConv_VM_MAP(flags, DENYWRITE)
2dfbb274
AM
16238+ | AuConv_VM_MAP(flags, LOCKED);
16239+}
38d290e6 16240+#endif
2dfbb274 16241+
9dbd164d 16242+static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
dece6358 16243+{
4a4d8108 16244+ int err;
4a4d8108 16245+ const unsigned char wlock
9dbd164d 16246+ = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
4a4d8108 16247+ struct super_block *sb;
9dbd164d 16248+ struct file *h_file;
b912730e 16249+ struct inode *inode;
9dbd164d
AM
16250+
16251+ AuDbgVmRegion(file, vma);
1308ab2a 16252+
b912730e
AM
16253+ inode = file_inode(file);
16254+ sb = inode->i_sb;
9dbd164d 16255+ lockdep_off();
e49829fe 16256+ si_read_lock(sb, AuLock_NOPLMW);
4a4d8108 16257+
b912730e 16258+ h_file = au_write_pre(file, wlock, /*wpre*/NULL);
9dbd164d 16259+ lockdep_on();
b912730e
AM
16260+ err = PTR_ERR(h_file);
16261+ if (IS_ERR(h_file))
16262+ goto out;
1308ab2a 16263+
b912730e
AM
16264+ err = 0;
16265+ au_set_mmapped(file);
9dbd164d 16266+ au_vm_file_reset(vma, h_file);
38d290e6
JR
16267+ /*
16268+ * we cannot call security_mmap_file() here since it may acquire
16269+ * mmap_sem or i_mutex.
16270+ *
16271+ * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16272+ * au_flag_conv(vma->vm_flags));
16273+ */
9dbd164d 16274+ if (!err)
521ced18 16275+ err = call_mmap(h_file, vma);
b912730e
AM
16276+ if (!err) {
16277+ au_vm_prfile_set(vma, file);
16278+ fsstack_copy_attr_atime(inode, file_inode(h_file));
16279+ goto out_fput; /* success */
16280+ }
2cbb1c4b
JR
16281+ au_unset_mmapped(file);
16282+ au_vm_file_reset(vma, file);
b912730e 16283+
2cbb1c4b 16284+out_fput:
9dbd164d 16285+ lockdep_off();
b912730e
AM
16286+ ii_write_unlock(inode);
16287+ lockdep_on();
16288+ fput(h_file);
4f0767ce 16289+out:
b912730e 16290+ lockdep_off();
9dbd164d
AM
16291+ si_read_unlock(sb);
16292+ lockdep_on();
16293+ AuTraceErr(err);
4a4d8108
AM
16294+ return err;
16295+}
16296+
16297+/* ---------------------------------------------------------------------- */
16298+
1e00d052
AM
16299+static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16300+ int datasync)
4a4d8108
AM
16301+{
16302+ int err;
b912730e 16303+ struct au_write_pre wpre;
4a4d8108
AM
16304+ struct inode *inode;
16305+ struct file *h_file;
4a4d8108
AM
16306+
16307+ err = 0; /* -EBADF; */ /* posix? */
16308+ if (unlikely(!(file->f_mode & FMODE_WRITE)))
b912730e 16309+ goto out;
4a4d8108 16310+
b912730e
AM
16311+ inode = file_inode(file);
16312+ au_mtx_and_read_lock(inode);
16313+
521ced18 16314+ wpre.lsc = 0;
b912730e
AM
16315+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16316+ err = PTR_ERR(h_file);
16317+ if (IS_ERR(h_file))
4a4d8108 16318+ goto out_unlock;
4a4d8108 16319+
53392da6 16320+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
b912730e 16321+ au_write_post(inode, h_file, &wpre, /*written*/0);
4a4d8108 16322+
4f0767ce 16323+out_unlock:
b912730e 16324+ si_read_unlock(inode->i_sb);
febd17d6 16325+ inode_unlock(inode);
b912730e 16326+out:
4a4d8108 16327+ return err;
dece6358
AM
16328+}
16329+
4a4d8108 16330+static int aufs_fasync(int fd, struct file *file, int flag)
dece6358 16331+{
4a4d8108
AM
16332+ int err;
16333+ struct file *h_file;
4a4d8108 16334+ struct super_block *sb;
1308ab2a 16335+
b912730e 16336+ sb = file->f_path.dentry->d_sb;
e49829fe 16337+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 16338+
521ced18 16339+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
16340+ err = PTR_ERR(h_file);
16341+ if (IS_ERR(h_file))
4a4d8108
AM
16342+ goto out;
16343+
523b37e3 16344+ if (h_file->f_op->fasync)
4a4d8108 16345+ err = h_file->f_op->fasync(fd, h_file, flag);
b912730e 16346+ fput(h_file); /* instead of au_read_post() */
1308ab2a 16347+
4f0767ce 16348+out:
4a4d8108 16349+ si_read_unlock(sb);
1308ab2a 16350+ return err;
dece6358 16351+}
4a4d8108 16352+
febd17d6
JR
16353+static int aufs_setfl(struct file *file, unsigned long arg)
16354+{
16355+ int err;
16356+ struct file *h_file;
16357+ struct super_block *sb;
16358+
16359+ sb = file->f_path.dentry->d_sb;
16360+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16361+
521ced18 16362+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
febd17d6
JR
16363+ err = PTR_ERR(h_file);
16364+ if (IS_ERR(h_file))
16365+ goto out;
16366+
1c60b727
AM
16367+ /* stop calling h_file->fasync */
16368+ arg |= vfsub_file_flags(file) & FASYNC;
febd17d6
JR
16369+ err = setfl(/*unused fd*/-1, h_file, arg);
16370+ fput(h_file); /* instead of au_read_post() */
16371+
16372+out:
16373+ si_read_unlock(sb);
16374+ return err;
16375+}
16376+
4a4d8108
AM
16377+/* ---------------------------------------------------------------------- */
16378+
16379+/* no one supports this operation, currently */
16380+#if 0
16381+static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
2000de60 16382+ size_t len, loff_t *pos, int more)
4a4d8108
AM
16383+{
16384+}
16385+#endif
16386+
16387+/* ---------------------------------------------------------------------- */
16388+
16389+const struct file_operations aufs_file_fop = {
16390+ .owner = THIS_MODULE,
2cbb1c4b 16391+
027c5e7a 16392+ .llseek = default_llseek,
4a4d8108
AM
16393+
16394+ .read = aufs_read,
16395+ .write = aufs_write,
076b876e
AM
16396+ .read_iter = aufs_read_iter,
16397+ .write_iter = aufs_write_iter,
16398+
4a4d8108
AM
16399+#ifdef CONFIG_AUFS_POLL
16400+ .poll = aufs_poll,
16401+#endif
16402+ .unlocked_ioctl = aufs_ioctl_nondir,
b752ccd1 16403+#ifdef CONFIG_COMPAT
c2b27bf2 16404+ .compat_ioctl = aufs_compat_ioctl_nondir,
b752ccd1 16405+#endif
4a4d8108
AM
16406+ .mmap = aufs_mmap,
16407+ .open = aufs_open_nondir,
16408+ .flush = aufs_flush_nondir,
16409+ .release = aufs_release_nondir,
16410+ .fsync = aufs_fsync_nondir,
4a4d8108
AM
16411+ .fasync = aufs_fasync,
16412+ /* .sendpage = aufs_sendpage, */
febd17d6 16413+ .setfl = aufs_setfl,
4a4d8108
AM
16414+ .splice_write = aufs_splice_write,
16415+ .splice_read = aufs_splice_read,
16416+#if 0
16417+ .aio_splice_write = aufs_aio_splice_write,
38d290e6 16418+ .aio_splice_read = aufs_aio_splice_read,
4a4d8108 16419+#endif
521ced18
JR
16420+ .fallocate = aufs_fallocate,
16421+ .copy_file_range = aufs_copy_file_range
4a4d8108 16422+};
7f207e10
AM
16423diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
16424--- /usr/share/empty/fs/aufs/fstype.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 16425+++ linux/fs/aufs/fstype.h 2018-08-12 23:43:05.456791299 +0200
062440b3
AM
16426@@ -0,0 +1,401 @@
16427+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 16428+/*
b00004a5 16429+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
16430+ *
16431+ * This program, aufs is free software; you can redistribute it and/or modify
16432+ * it under the terms of the GNU General Public License as published by
16433+ * the Free Software Foundation; either version 2 of the License, or
16434+ * (at your option) any later version.
16435+ *
16436+ * This program is distributed in the hope that it will be useful,
16437+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16438+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16439+ * GNU General Public License for more details.
16440+ *
16441+ * You should have received a copy of the GNU General Public License
523b37e3 16442+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
16443+ */
16444+
16445+/*
16446+ * judging filesystem type
16447+ */
16448+
16449+#ifndef __AUFS_FSTYPE_H__
16450+#define __AUFS_FSTYPE_H__
16451+
16452+#ifdef __KERNEL__
16453+
16454+#include <linux/fs.h>
16455+#include <linux/magic.h>
b912730e 16456+#include <linux/nfs_fs.h>
b95c5147 16457+#include <linux/romfs_fs.h>
4a4d8108
AM
16458+
16459+static inline int au_test_aufs(struct super_block *sb)
16460+{
16461+ return sb->s_magic == AUFS_SUPER_MAGIC;
16462+}
16463+
16464+static inline const char *au_sbtype(struct super_block *sb)
16465+{
16466+ return sb->s_type->name;
16467+}
1308ab2a 16468+
16469+static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
16470+{
f0c0a007 16471+#if IS_ENABLED(CONFIG_ISO9660_FS)
2000de60 16472+ return sb->s_magic == ISOFS_SUPER_MAGIC;
dece6358
AM
16473+#else
16474+ return 0;
16475+#endif
16476+}
16477+
1308ab2a 16478+static inline int au_test_romfs(struct super_block *sb __maybe_unused)
dece6358 16479+{
f0c0a007 16480+#if IS_ENABLED(CONFIG_ROMFS_FS)
2000de60 16481+ return sb->s_magic == ROMFS_MAGIC;
dece6358
AM
16482+#else
16483+ return 0;
16484+#endif
16485+}
16486+
1308ab2a 16487+static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
dece6358 16488+{
f0c0a007 16489+#if IS_ENABLED(CONFIG_CRAMFS)
1308ab2a 16490+ return sb->s_magic == CRAMFS_MAGIC;
16491+#endif
16492+ return 0;
16493+}
16494+
16495+static inline int au_test_nfs(struct super_block *sb __maybe_unused)
16496+{
f0c0a007 16497+#if IS_ENABLED(CONFIG_NFS_FS)
1308ab2a 16498+ return sb->s_magic == NFS_SUPER_MAGIC;
dece6358
AM
16499+#else
16500+ return 0;
16501+#endif
16502+}
16503+
1308ab2a 16504+static inline int au_test_fuse(struct super_block *sb __maybe_unused)
dece6358 16505+{
f0c0a007 16506+#if IS_ENABLED(CONFIG_FUSE_FS)
1308ab2a 16507+ return sb->s_magic == FUSE_SUPER_MAGIC;
dece6358
AM
16508+#else
16509+ return 0;
16510+#endif
16511+}
16512+
1308ab2a 16513+static inline int au_test_xfs(struct super_block *sb __maybe_unused)
dece6358 16514+{
f0c0a007 16515+#if IS_ENABLED(CONFIG_XFS_FS)
1308ab2a 16516+ return sb->s_magic == XFS_SB_MAGIC;
dece6358
AM
16517+#else
16518+ return 0;
16519+#endif
16520+}
16521+
1308ab2a 16522+static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
dece6358 16523+{
1308ab2a 16524+#ifdef CONFIG_TMPFS
16525+ return sb->s_magic == TMPFS_MAGIC;
16526+#else
16527+ return 0;
dece6358 16528+#endif
dece6358
AM
16529+}
16530+
1308ab2a 16531+static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
1facf9fc 16532+{
f0c0a007 16533+#if IS_ENABLED(CONFIG_ECRYPT_FS)
1308ab2a 16534+ return !strcmp(au_sbtype(sb), "ecryptfs");
16535+#else
16536+ return 0;
16537+#endif
1facf9fc 16538+}
16539+
1308ab2a 16540+static inline int au_test_ramfs(struct super_block *sb)
16541+{
16542+ return sb->s_magic == RAMFS_MAGIC;
16543+}
16544+
16545+static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
16546+{
f0c0a007 16547+#if IS_ENABLED(CONFIG_UBIFS_FS)
1308ab2a 16548+ return sb->s_magic == UBIFS_SUPER_MAGIC;
16549+#else
16550+ return 0;
16551+#endif
16552+}
16553+
16554+static inline int au_test_procfs(struct super_block *sb __maybe_unused)
16555+{
16556+#ifdef CONFIG_PROC_FS
16557+ return sb->s_magic == PROC_SUPER_MAGIC;
16558+#else
16559+ return 0;
16560+#endif
16561+}
16562+
16563+static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
16564+{
16565+#ifdef CONFIG_SYSFS
16566+ return sb->s_magic == SYSFS_MAGIC;
16567+#else
16568+ return 0;
16569+#endif
16570+}
16571+
16572+static inline int au_test_configfs(struct super_block *sb __maybe_unused)
16573+{
f0c0a007 16574+#if IS_ENABLED(CONFIG_CONFIGFS_FS)
1308ab2a 16575+ return sb->s_magic == CONFIGFS_MAGIC;
16576+#else
16577+ return 0;
16578+#endif
16579+}
16580+
16581+static inline int au_test_minix(struct super_block *sb __maybe_unused)
16582+{
f0c0a007 16583+#if IS_ENABLED(CONFIG_MINIX_FS)
1308ab2a 16584+ return sb->s_magic == MINIX3_SUPER_MAGIC
16585+ || sb->s_magic == MINIX2_SUPER_MAGIC
16586+ || sb->s_magic == MINIX2_SUPER_MAGIC2
16587+ || sb->s_magic == MINIX_SUPER_MAGIC
16588+ || sb->s_magic == MINIX_SUPER_MAGIC2;
16589+#else
16590+ return 0;
16591+#endif
16592+}
16593+
1308ab2a 16594+static inline int au_test_fat(struct super_block *sb __maybe_unused)
16595+{
f0c0a007 16596+#if IS_ENABLED(CONFIG_FAT_FS)
1308ab2a 16597+ return sb->s_magic == MSDOS_SUPER_MAGIC;
16598+#else
16599+ return 0;
16600+#endif
16601+}
16602+
16603+static inline int au_test_msdos(struct super_block *sb)
16604+{
16605+ return au_test_fat(sb);
16606+}
16607+
16608+static inline int au_test_vfat(struct super_block *sb)
16609+{
16610+ return au_test_fat(sb);
16611+}
16612+
16613+static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
16614+{
16615+#ifdef CONFIG_SECURITYFS
16616+ return sb->s_magic == SECURITYFS_MAGIC;
16617+#else
16618+ return 0;
16619+#endif
16620+}
16621+
16622+static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
16623+{
f0c0a007 16624+#if IS_ENABLED(CONFIG_SQUASHFS)
1308ab2a 16625+ return sb->s_magic == SQUASHFS_MAGIC;
16626+#else
16627+ return 0;
16628+#endif
16629+}
16630+
16631+static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
16632+{
f0c0a007 16633+#if IS_ENABLED(CONFIG_BTRFS_FS)
1308ab2a 16634+ return sb->s_magic == BTRFS_SUPER_MAGIC;
16635+#else
16636+ return 0;
16637+#endif
16638+}
16639+
16640+static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
16641+{
f0c0a007 16642+#if IS_ENABLED(CONFIG_XENFS)
1308ab2a 16643+ return sb->s_magic == XENFS_SUPER_MAGIC;
16644+#else
16645+ return 0;
16646+#endif
16647+}
16648+
16649+static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
16650+{
16651+#ifdef CONFIG_DEBUG_FS
16652+ return sb->s_magic == DEBUGFS_MAGIC;
16653+#else
16654+ return 0;
16655+#endif
16656+}
16657+
16658+static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
16659+{
f0c0a007 16660+#if IS_ENABLED(CONFIG_NILFS)
1308ab2a 16661+ return sb->s_magic == NILFS_SUPER_MAGIC;
16662+#else
16663+ return 0;
16664+#endif
16665+}
16666+
4a4d8108
AM
16667+static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
16668+{
f0c0a007 16669+#if IS_ENABLED(CONFIG_HFSPLUS_FS)
4a4d8108
AM
16670+ return sb->s_magic == HFSPLUS_SUPER_MAGIC;
16671+#else
16672+ return 0;
16673+#endif
16674+}
16675+
1308ab2a 16676+/* ---------------------------------------------------------------------- */
16677+/*
16678+ * they can't be an aufs branch.
16679+ */
16680+static inline int au_test_fs_unsuppoted(struct super_block *sb)
16681+{
16682+ return
16683+#ifndef CONFIG_AUFS_BR_RAMFS
16684+ au_test_ramfs(sb) ||
16685+#endif
16686+ au_test_procfs(sb)
16687+ || au_test_sysfs(sb)
16688+ || au_test_configfs(sb)
16689+ || au_test_debugfs(sb)
16690+ || au_test_securityfs(sb)
16691+ || au_test_xenfs(sb)
16692+ || au_test_ecryptfs(sb)
16693+ /* || !strcmp(au_sbtype(sb), "unionfs") */
16694+ || au_test_aufs(sb); /* will be supported in next version */
16695+}
16696+
1308ab2a 16697+static inline int au_test_fs_remote(struct super_block *sb)
16698+{
16699+ return !au_test_tmpfs(sb)
16700+#ifdef CONFIG_AUFS_BR_RAMFS
16701+ && !au_test_ramfs(sb)
16702+#endif
16703+ && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
16704+}
16705+
16706+/* ---------------------------------------------------------------------- */
16707+
16708+/*
16709+ * Note: these functions (below) are created after reading ->getattr() in all
16710+ * filesystems under linux/fs. it means we have to do so in every update...
16711+ */
16712+
16713+/*
16714+ * some filesystems require getattr to refresh the inode attributes before
16715+ * referencing.
16716+ * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
16717+ * and leave the work for d_revalidate()
16718+ */
16719+static inline int au_test_fs_refresh_iattr(struct super_block *sb)
16720+{
16721+ return au_test_nfs(sb)
16722+ || au_test_fuse(sb)
1308ab2a 16723+ /* || au_test_btrfs(sb) */ /* untested */
1308ab2a 16724+ ;
16725+}
16726+
16727+/*
16728+ * filesystems which don't maintain i_size or i_blocks.
16729+ */
16730+static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
16731+{
16732+ return au_test_xfs(sb)
4a4d8108
AM
16733+ || au_test_btrfs(sb)
16734+ || au_test_ubifs(sb)
16735+ || au_test_hfsplus(sb) /* maintained, but incorrect */
1308ab2a 16736+ /* || au_test_minix(sb) */ /* untested */
16737+ ;
16738+}
16739+
16740+/*
16741+ * filesystems which don't store the correct value in some of their inode
16742+ * attributes.
16743+ */
16744+static inline int au_test_fs_bad_iattr(struct super_block *sb)
16745+{
16746+ return au_test_fs_bad_iattr_size(sb)
1308ab2a 16747+ || au_test_fat(sb)
16748+ || au_test_msdos(sb)
16749+ || au_test_vfat(sb);
1facf9fc 16750+}
16751+
16752+/* they don't check i_nlink in link(2) */
16753+static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
16754+{
16755+ return au_test_tmpfs(sb)
16756+#ifdef CONFIG_AUFS_BR_RAMFS
16757+ || au_test_ramfs(sb)
16758+#endif
4a4d8108 16759+ || au_test_ubifs(sb)
4a4d8108 16760+ || au_test_hfsplus(sb);
1facf9fc 16761+}
16762+
16763+/*
16764+ * filesystems which sets S_NOATIME and S_NOCMTIME.
16765+ */
16766+static inline int au_test_fs_notime(struct super_block *sb)
16767+{
16768+ return au_test_nfs(sb)
16769+ || au_test_fuse(sb)
dece6358 16770+ || au_test_ubifs(sb)
1facf9fc 16771+ ;
16772+}
16773+
1facf9fc 16774+/* temporary support for i#1 in cramfs */
16775+static inline int au_test_fs_unique_ino(struct inode *inode)
16776+{
16777+ if (au_test_cramfs(inode->i_sb))
16778+ return inode->i_ino != 1;
16779+ return 1;
16780+}
16781+
16782+/* ---------------------------------------------------------------------- */
16783+
16784+/*
16785+ * the filesystem where the xino files placed must support i/o after unlink and
16786+ * maintain i_size and i_blocks.
16787+ */
16788+static inline int au_test_fs_bad_xino(struct super_block *sb)
16789+{
16790+ return au_test_fs_remote(sb)
16791+ || au_test_fs_bad_iattr_size(sb)
1facf9fc 16792+ /* don't want unnecessary work for xino */
16793+ || au_test_aufs(sb)
1308ab2a 16794+ || au_test_ecryptfs(sb)
16795+ || au_test_nilfs(sb);
1facf9fc 16796+}
16797+
16798+static inline int au_test_fs_trunc_xino(struct super_block *sb)
16799+{
16800+ return au_test_tmpfs(sb)
16801+ || au_test_ramfs(sb);
16802+}
16803+
16804+/*
16805+ * test if the @sb is real-readonly.
16806+ */
16807+static inline int au_test_fs_rr(struct super_block *sb)
16808+{
16809+ return au_test_squashfs(sb)
16810+ || au_test_iso9660(sb)
16811+ || au_test_cramfs(sb)
16812+ || au_test_romfs(sb);
16813+}
16814+
b912730e
AM
16815+/*
16816+ * test if the @inode is nfs with 'noacl' option
2121bcd9 16817+ * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
b912730e
AM
16818+ */
16819+static inline int au_test_nfs_noacl(struct inode *inode)
16820+{
16821+ return au_test_nfs(inode->i_sb)
16822+ /* && IS_POSIXACL(inode) */
16823+ && !nfs_server_capable(inode, NFS_CAP_ACLS);
16824+}
16825+
1facf9fc 16826+#endif /* __KERNEL__ */
16827+#endif /* __AUFS_FSTYPE_H__ */
8b6a4947
AM
16828diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
16829--- /usr/share/empty/fs/aufs/hbl.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 16830+++ linux/fs/aufs/hbl.h 2018-08-12 23:43:05.456791299 +0200
062440b3
AM
16831@@ -0,0 +1,65 @@
16832+/* SPDX-License-Identifier: GPL-2.0 */
8b6a4947 16833+/*
b00004a5 16834+ * Copyright (C) 2017-2018 Junjiro R. Okajima
8b6a4947
AM
16835+ *
16836+ * This program, aufs is free software; you can redistribute it and/or modify
16837+ * it under the terms of the GNU General Public License as published by
16838+ * the Free Software Foundation; either version 2 of the License, or
16839+ * (at your option) any later version.
16840+ *
16841+ * This program is distributed in the hope that it will be useful,
16842+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16843+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16844+ * GNU General Public License for more details.
16845+ *
16846+ * You should have received a copy of the GNU General Public License
16847+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16848+ */
16849+
16850+/*
16851+ * helpers for hlist_bl.h
16852+ */
16853+
16854+#ifndef __AUFS_HBL_H__
16855+#define __AUFS_HBL_H__
16856+
16857+#ifdef __KERNEL__
16858+
16859+#include <linux/list_bl.h>
16860+
16861+static inline void au_hbl_add(struct hlist_bl_node *node,
16862+ struct hlist_bl_head *hbl)
16863+{
16864+ hlist_bl_lock(hbl);
16865+ hlist_bl_add_head(node, hbl);
16866+ hlist_bl_unlock(hbl);
16867+}
16868+
16869+static inline void au_hbl_del(struct hlist_bl_node *node,
16870+ struct hlist_bl_head *hbl)
16871+{
16872+ hlist_bl_lock(hbl);
16873+ hlist_bl_del(node);
16874+ hlist_bl_unlock(hbl);
16875+}
16876+
16877+#define au_hbl_for_each(pos, head) \
16878+ for (pos = hlist_bl_first(head); \
16879+ pos; \
16880+ pos = pos->next)
16881+
16882+static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
16883+{
16884+ unsigned long cnt;
16885+ struct hlist_bl_node *pos;
16886+
16887+ cnt = 0;
16888+ hlist_bl_lock(hbl);
16889+ au_hbl_for_each(pos, hbl)
16890+ cnt++;
16891+ hlist_bl_unlock(hbl);
16892+ return cnt;
16893+}
16894+
16895+#endif /* __KERNEL__ */
16896+#endif /* __AUFS_HBL_H__ */
7f207e10
AM
16897diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
16898--- /usr/share/empty/fs/aufs/hfsnotify.c 1970-01-01 01:00:00.000000000 +0100
9f237c51 16899+++ linux/fs/aufs/hfsnotify.c 2018-12-27 13:19:17.708416053 +0100
acd2b654 16900@@ -0,0 +1,289 @@
cd7a4cd9 16901+// SPDX-License-Identifier: GPL-2.0
1facf9fc 16902+/*
b00004a5 16903+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 16904+ *
16905+ * This program, aufs is free software; you can redistribute it and/or modify
16906+ * it under the terms of the GNU General Public License as published by
16907+ * the Free Software Foundation; either version 2 of the License, or
16908+ * (at your option) any later version.
dece6358
AM
16909+ *
16910+ * This program is distributed in the hope that it will be useful,
16911+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16912+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16913+ * GNU General Public License for more details.
16914+ *
16915+ * You should have received a copy of the GNU General Public License
523b37e3 16916+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 16917+ */
16918+
16919+/*
4a4d8108 16920+ * fsnotify for the lower directories
1facf9fc 16921+ */
16922+
16923+#include "aufs.h"
16924+
4a4d8108
AM
16925+/* FS_IN_IGNORED is unnecessary */
16926+static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
16927+ | FS_CREATE | FS_EVENT_ON_CHILD);
7f207e10 16928+static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
7eafdf33 16929+static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
1facf9fc 16930+
0c5527e5 16931+static void au_hfsn_free_mark(struct fsnotify_mark *mark)
1facf9fc 16932+{
0c5527e5
AM
16933+ struct au_hnotify *hn = container_of(mark, struct au_hnotify,
16934+ hn_mark);
5afbbe0d 16935+ /* AuDbg("here\n"); */
1c60b727 16936+ au_cache_free_hnotify(hn);
8b6a4947 16937+ smp_mb__before_atomic(); /* for atomic64_dec */
1716fcea
AM
16938+ if (atomic64_dec_and_test(&au_hfsn_ifree))
16939+ wake_up(&au_hfsn_wq);
4a4d8108 16940+}
1facf9fc 16941+
027c5e7a 16942+static int au_hfsn_alloc(struct au_hinode *hinode)
4a4d8108 16943+{
1716fcea 16944+ int err;
027c5e7a
AM
16945+ struct au_hnotify *hn;
16946+ struct super_block *sb;
16947+ struct au_branch *br;
0c5527e5 16948+ struct fsnotify_mark *mark;
027c5e7a 16949+ aufs_bindex_t bindex;
1facf9fc 16950+
027c5e7a
AM
16951+ hn = hinode->hi_notify;
16952+ sb = hn->hn_aufs_inode->i_sb;
16953+ bindex = au_br_index(sb, hinode->hi_id);
16954+ br = au_sbr(sb, bindex);
1716fcea
AM
16955+ AuDebugOn(!br->br_hfsn);
16956+
0c5527e5 16957+ mark = &hn->hn_mark;
ffa93bbd 16958+ fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
0c5527e5 16959+ mark->mask = AuHfsnMask;
7f207e10
AM
16960+ /*
16961+ * by udba rename or rmdir, aufs assign a new inode to the known
16962+ * h_inode, so specify 1 to allow dups.
16963+ */
c1595e42 16964+ lockdep_off();
acd2b654 16965+ err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
c1595e42 16966+ lockdep_on();
1716fcea
AM
16967+
16968+ return err;
1facf9fc 16969+}
16970+
7eafdf33 16971+static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
1facf9fc 16972+{
0c5527e5 16973+ struct fsnotify_mark *mark;
7eafdf33 16974+ unsigned long long ull;
1716fcea 16975+ struct fsnotify_group *group;
7eafdf33
AM
16976+
16977+ ull = atomic64_inc_return(&au_hfsn_ifree);
16978+ BUG_ON(!ull);
953406b4 16979+
0c5527e5 16980+ mark = &hn->hn_mark;
1716fcea
AM
16981+ spin_lock(&mark->lock);
16982+ group = mark->group;
16983+ fsnotify_get_group(group);
16984+ spin_unlock(&mark->lock);
c1595e42 16985+ lockdep_off();
1716fcea 16986+ fsnotify_destroy_mark(mark, group);
5afbbe0d 16987+ fsnotify_put_mark(mark);
1716fcea 16988+ fsnotify_put_group(group);
c1595e42 16989+ lockdep_on();
7f207e10 16990+
7eafdf33
AM
16991+ /* free hn by myself */
16992+ return 0;
1facf9fc 16993+}
16994+
16995+/* ---------------------------------------------------------------------- */
16996+
4a4d8108 16997+static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
1facf9fc 16998+{
0c5527e5 16999+ struct fsnotify_mark *mark;
1facf9fc 17000+
0c5527e5
AM
17001+ mark = &hinode->hi_notify->hn_mark;
17002+ spin_lock(&mark->lock);
1facf9fc 17003+ if (do_set) {
0c5527e5
AM
17004+ AuDebugOn(mark->mask & AuHfsnMask);
17005+ mark->mask |= AuHfsnMask;
1facf9fc 17006+ } else {
0c5527e5
AM
17007+ AuDebugOn(!(mark->mask & AuHfsnMask));
17008+ mark->mask &= ~AuHfsnMask;
1facf9fc 17009+ }
0c5527e5 17010+ spin_unlock(&mark->lock);
4a4d8108 17011+ /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
1facf9fc 17012+}
17013+
4a4d8108 17014+/* ---------------------------------------------------------------------- */
1facf9fc 17015+
4a4d8108
AM
17016+/* #define AuDbgHnotify */
17017+#ifdef AuDbgHnotify
17018+static char *au_hfsn_name(u32 mask)
17019+{
17020+#ifdef CONFIG_AUFS_DEBUG
c06a8ce3
AM
17021+#define test_ret(flag) \
17022+ do { \
17023+ if (mask & flag) \
17024+ return #flag; \
17025+ } while (0)
4a4d8108
AM
17026+ test_ret(FS_ACCESS);
17027+ test_ret(FS_MODIFY);
17028+ test_ret(FS_ATTRIB);
17029+ test_ret(FS_CLOSE_WRITE);
17030+ test_ret(FS_CLOSE_NOWRITE);
17031+ test_ret(FS_OPEN);
17032+ test_ret(FS_MOVED_FROM);
17033+ test_ret(FS_MOVED_TO);
17034+ test_ret(FS_CREATE);
17035+ test_ret(FS_DELETE);
17036+ test_ret(FS_DELETE_SELF);
17037+ test_ret(FS_MOVE_SELF);
17038+ test_ret(FS_UNMOUNT);
17039+ test_ret(FS_Q_OVERFLOW);
17040+ test_ret(FS_IN_IGNORED);
b912730e 17041+ test_ret(FS_ISDIR);
4a4d8108
AM
17042+ test_ret(FS_IN_ONESHOT);
17043+ test_ret(FS_EVENT_ON_CHILD);
17044+ return "";
17045+#undef test_ret
17046+#else
17047+ return "??";
17048+#endif
1facf9fc 17049+}
4a4d8108 17050+#endif
1facf9fc 17051+
17052+/* ---------------------------------------------------------------------- */
17053+
1716fcea
AM
17054+static void au_hfsn_free_group(struct fsnotify_group *group)
17055+{
17056+ struct au_br_hfsnotify *hfsn = group->private;
17057+
5afbbe0d 17058+ /* AuDbg("here\n"); */
9f237c51 17059+ au_kfree_try_rcu(hfsn);
1716fcea
AM
17060+}
17061+
4a4d8108 17062+static int au_hfsn_handle_event(struct fsnotify_group *group,
fb47a38f 17063+ struct inode *inode,
a2654f78 17064+ u32 mask, const void *data, int data_type,
ffa93bbd
AM
17065+ const unsigned char *file_name, u32 cookie,
17066+ struct fsnotify_iter_info *iter_info)
1facf9fc 17067+{
17068+ int err;
4a4d8108
AM
17069+ struct au_hnotify *hnotify;
17070+ struct inode *h_dir, *h_inode;
fb47a38f 17071+ struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
cd7a4cd9 17072+ struct fsnotify_mark *inode_mark;
4a4d8108 17073+
fb47a38f 17074+ AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
1facf9fc 17075+
17076+ err = 0;
0c5527e5 17077+ /* if FS_UNMOUNT happens, there must be another bug */
4a4d8108 17078+ AuDebugOn(mask & FS_UNMOUNT);
0c5527e5 17079+ if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
1facf9fc 17080+ goto out;
1facf9fc 17081+
fb47a38f
JR
17082+ h_dir = inode;
17083+ h_inode = NULL;
4a4d8108 17084+#ifdef AuDbgHnotify
392086de 17085+ au_debug_on();
4a4d8108
AM
17086+ if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
17087+ || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
17088+ AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
17089+ h_dir->i_ino, mask, au_hfsn_name(mask),
17090+ AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
17091+ /* WARN_ON(1); */
1facf9fc 17092+ }
392086de 17093+ au_debug_off();
1facf9fc 17094+#endif
4a4d8108 17095+
cd7a4cd9 17096+ inode_mark = fsnotify_iter_inode_mark(iter_info);
0c5527e5
AM
17097+ AuDebugOn(!inode_mark);
17098+ hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
17099+ err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
1facf9fc 17100+
4a4d8108
AM
17101+out:
17102+ return err;
17103+}
1facf9fc 17104+
4a4d8108 17105+static struct fsnotify_ops au_hfsn_ops = {
1716fcea 17106+ .handle_event = au_hfsn_handle_event,
ffa93bbd
AM
17107+ .free_group_priv = au_hfsn_free_group,
17108+ .free_mark = au_hfsn_free_mark
4a4d8108
AM
17109+};
17110+
17111+/* ---------------------------------------------------------------------- */
17112+
027c5e7a
AM
17113+static void au_hfsn_fin_br(struct au_branch *br)
17114+{
1716fcea 17115+ struct au_br_hfsnotify *hfsn;
027c5e7a 17116+
1716fcea 17117+ hfsn = br->br_hfsn;
c1595e42
JR
17118+ if (hfsn) {
17119+ lockdep_off();
1716fcea 17120+ fsnotify_put_group(hfsn->hfsn_group);
c1595e42
JR
17121+ lockdep_on();
17122+ }
027c5e7a
AM
17123+}
17124+
1716fcea 17125+static int au_hfsn_init_br(struct au_branch *br, int perm)
4a4d8108
AM
17126+{
17127+ int err;
1716fcea
AM
17128+ struct fsnotify_group *group;
17129+ struct au_br_hfsnotify *hfsn;
1facf9fc 17130+
4a4d8108 17131+ err = 0;
1716fcea
AM
17132+ br->br_hfsn = NULL;
17133+ if (!au_br_hnotifyable(perm))
027c5e7a 17134+ goto out;
027c5e7a 17135+
1716fcea
AM
17136+ err = -ENOMEM;
17137+ hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
17138+ if (unlikely(!hfsn))
027c5e7a
AM
17139+ goto out;
17140+
1716fcea
AM
17141+ err = 0;
17142+ group = fsnotify_alloc_group(&au_hfsn_ops);
17143+ if (IS_ERR(group)) {
17144+ err = PTR_ERR(group);
0c5527e5 17145+ pr_err("fsnotify_alloc_group() failed, %d\n", err);
1716fcea 17146+ goto out_hfsn;
4a4d8108 17147+ }
1facf9fc 17148+
1716fcea
AM
17149+ group->private = hfsn;
17150+ hfsn->hfsn_group = group;
17151+ br->br_hfsn = hfsn;
17152+ goto out; /* success */
17153+
17154+out_hfsn:
9f237c51 17155+ au_kfree_try_rcu(hfsn);
027c5e7a 17156+out:
1716fcea
AM
17157+ return err;
17158+}
17159+
17160+static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
17161+{
17162+ int err;
17163+
17164+ err = 0;
17165+ if (!br->br_hfsn)
17166+ err = au_hfsn_init_br(br, perm);
17167+
1facf9fc 17168+ return err;
17169+}
17170+
7eafdf33
AM
17171+/* ---------------------------------------------------------------------- */
17172+
17173+static void au_hfsn_fin(void)
17174+{
17175+ AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
17176+ wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
17177+}
17178+
4a4d8108
AM
17179+const struct au_hnotify_op au_hnotify_op = {
17180+ .ctl = au_hfsn_ctl,
17181+ .alloc = au_hfsn_alloc,
17182+ .free = au_hfsn_free,
1facf9fc 17183+
7eafdf33
AM
17184+ .fin = au_hfsn_fin,
17185+
027c5e7a
AM
17186+ .reset_br = au_hfsn_reset_br,
17187+ .fin_br = au_hfsn_fin_br,
17188+ .init_br = au_hfsn_init_br
4a4d8108 17189+};
7f207e10
AM
17190diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
17191--- /usr/share/empty/fs/aufs/hfsplus.c 1970-01-01 01:00:00.000000000 +0100
acd2b654
AM
17192+++ linux/fs/aufs/hfsplus.c 2018-10-23 12:33:35.596042364 +0200
17193@@ -0,0 +1,60 @@
cd7a4cd9 17194+// SPDX-License-Identifier: GPL-2.0
4a4d8108 17195+/*
b00004a5 17196+ * Copyright (C) 2010-2018 Junjiro R. Okajima
4a4d8108
AM
17197+ *
17198+ * This program, aufs is free software; you can redistribute it and/or modify
17199+ * it under the terms of the GNU General Public License as published by
17200+ * the Free Software Foundation; either version 2 of the License, or
17201+ * (at your option) any later version.
17202+ *
17203+ * This program is distributed in the hope that it will be useful,
17204+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17205+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17206+ * GNU General Public License for more details.
17207+ *
17208+ * You should have received a copy of the GNU General Public License
523b37e3 17209+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 17210+ */
1facf9fc 17211+
4a4d8108 17212+/*
acd2b654 17213+ * special support for filesystems which acquires an inode mutex
4a4d8108
AM
17214+ * at final closing a file, eg, hfsplus.
17215+ *
17216+ * This trick is very simple and stupid, just to open the file before really
acd2b654 17217+ * necessary open to tell hfsplus that this is not the final closing.
4a4d8108
AM
17218+ * The caller should call au_h_open_pre() after acquiring the inode mutex,
17219+ * and au_h_open_post() after releasing it.
17220+ */
1facf9fc 17221+
4a4d8108 17222+#include "aufs.h"
1facf9fc 17223+
392086de
AM
17224+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
17225+ int force_wr)
4a4d8108
AM
17226+{
17227+ struct file *h_file;
17228+ struct dentry *h_dentry;
1facf9fc 17229+
4a4d8108
AM
17230+ h_dentry = au_h_dptr(dentry, bindex);
17231+ AuDebugOn(!h_dentry);
5527c038 17232+ AuDebugOn(d_is_negative(h_dentry));
4a4d8108
AM
17233+
17234+ h_file = NULL;
17235+ if (au_test_hfsplus(h_dentry->d_sb)
7e9cd9fe 17236+ && d_is_reg(h_dentry))
4a4d8108
AM
17237+ h_file = au_h_open(dentry, bindex,
17238+ O_RDONLY | O_NOATIME | O_LARGEFILE,
392086de 17239+ /*file*/NULL, force_wr);
4a4d8108 17240+ return h_file;
1facf9fc 17241+}
17242+
4a4d8108
AM
17243+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
17244+ struct file *h_file)
17245+{
acd2b654
AM
17246+ struct au_branch *br;
17247+
4a4d8108
AM
17248+ if (h_file) {
17249+ fput(h_file);
acd2b654
AM
17250+ br = au_sbr(dentry->d_sb, bindex);
17251+ au_lcnt_dec(&br->br_nfiles);
4a4d8108
AM
17252+ }
17253+}
7f207e10
AM
17254diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
17255--- /usr/share/empty/fs/aufs/hnotify.c 1970-01-01 01:00:00.000000000 +0100
9f237c51 17256+++ linux/fs/aufs/hnotify.c 2018-12-27 13:19:17.708416053 +0100
062440b3 17257@@ -0,0 +1,720 @@
cd7a4cd9 17258+// SPDX-License-Identifier: GPL-2.0
e49829fe 17259+/*
b00004a5 17260+ * Copyright (C) 2005-2018 Junjiro R. Okajima
e49829fe
JR
17261+ *
17262+ * This program, aufs is free software; you can redistribute it and/or modify
17263+ * it under the terms of the GNU General Public License as published by
17264+ * the Free Software Foundation; either version 2 of the License, or
17265+ * (at your option) any later version.
17266+ *
17267+ * This program is distributed in the hope that it will be useful,
17268+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17269+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17270+ * GNU General Public License for more details.
17271+ *
17272+ * You should have received a copy of the GNU General Public License
523b37e3 17273+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
17274+ */
17275+
17276+/*
7f207e10 17277+ * abstraction to notify the direct changes on lower directories
e49829fe
JR
17278+ */
17279+
17280+#include "aufs.h"
17281+
027c5e7a 17282+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
e49829fe
JR
17283+{
17284+ int err;
7f207e10 17285+ struct au_hnotify *hn;
1facf9fc 17286+
4a4d8108
AM
17287+ err = -ENOMEM;
17288+ hn = au_cache_alloc_hnotify();
17289+ if (hn) {
17290+ hn->hn_aufs_inode = inode;
027c5e7a
AM
17291+ hinode->hi_notify = hn;
17292+ err = au_hnotify_op.alloc(hinode);
17293+ AuTraceErr(err);
17294+ if (unlikely(err)) {
17295+ hinode->hi_notify = NULL;
1c60b727 17296+ au_cache_free_hnotify(hn);
4a4d8108
AM
17297+ /*
17298+ * The upper dir was removed by udba, but the same named
acd2b654 17299+ * dir left. In this case, aufs assigns a new inode
4a4d8108 17300+ * number and set the monitor again.
acd2b654 17301+ * For the lower dir, the old monitor is still left.
4a4d8108
AM
17302+ */
17303+ if (err == -EEXIST)
17304+ err = 0;
17305+ }
1308ab2a 17306+ }
1308ab2a 17307+
027c5e7a 17308+ AuTraceErr(err);
1308ab2a 17309+ return err;
dece6358 17310+}
1facf9fc 17311+
4a4d8108 17312+void au_hn_free(struct au_hinode *hinode)
dece6358 17313+{
4a4d8108 17314+ struct au_hnotify *hn;
1facf9fc 17315+
4a4d8108
AM
17316+ hn = hinode->hi_notify;
17317+ if (hn) {
4a4d8108 17318+ hinode->hi_notify = NULL;
7eafdf33 17319+ if (au_hnotify_op.free(hinode, hn))
1c60b727 17320+ au_cache_free_hnotify(hn);
4a4d8108
AM
17321+ }
17322+}
dece6358 17323+
4a4d8108 17324+/* ---------------------------------------------------------------------- */
dece6358 17325+
4a4d8108
AM
17326+void au_hn_ctl(struct au_hinode *hinode, int do_set)
17327+{
17328+ if (hinode->hi_notify)
17329+ au_hnotify_op.ctl(hinode, do_set);
17330+}
17331+
17332+void au_hn_reset(struct inode *inode, unsigned int flags)
17333+{
5afbbe0d 17334+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
17335+ struct inode *hi;
17336+ struct dentry *iwhdentry;
1facf9fc 17337+
5afbbe0d
AM
17338+ bbot = au_ibbot(inode);
17339+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108
AM
17340+ hi = au_h_iptr(inode, bindex);
17341+ if (!hi)
17342+ continue;
1308ab2a 17343+
febd17d6 17344+ /* inode_lock_nested(hi, AuLsc_I_CHILD); */
4a4d8108
AM
17345+ iwhdentry = au_hi_wh(inode, bindex);
17346+ if (iwhdentry)
17347+ dget(iwhdentry);
17348+ au_igrab(hi);
17349+ au_set_h_iptr(inode, bindex, NULL, 0);
17350+ au_set_h_iptr(inode, bindex, au_igrab(hi),
17351+ flags & ~AuHi_XINO);
17352+ iput(hi);
17353+ dput(iwhdentry);
febd17d6 17354+ /* inode_unlock(hi); */
1facf9fc 17355+ }
1facf9fc 17356+}
17357+
1308ab2a 17358+/* ---------------------------------------------------------------------- */
1facf9fc 17359+
4a4d8108 17360+static int hn_xino(struct inode *inode, struct inode *h_inode)
1facf9fc 17361+{
4a4d8108 17362+ int err;
5afbbe0d 17363+ aufs_bindex_t bindex, bbot, bfound, btop;
4a4d8108 17364+ struct inode *h_i;
1facf9fc 17365+
4a4d8108
AM
17366+ err = 0;
17367+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17368+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17369+ goto out;
17370+ }
1facf9fc 17371+
4a4d8108 17372+ bfound = -1;
5afbbe0d
AM
17373+ bbot = au_ibbot(inode);
17374+ btop = au_ibtop(inode);
4a4d8108 17375+#if 0 /* reserved for future use */
5afbbe0d 17376+ if (bindex == bbot) {
4a4d8108
AM
17377+ /* keep this ino in rename case */
17378+ goto out;
17379+ }
17380+#endif
5afbbe0d 17381+ for (bindex = btop; bindex <= bbot; bindex++)
4a4d8108
AM
17382+ if (au_h_iptr(inode, bindex) == h_inode) {
17383+ bfound = bindex;
17384+ break;
17385+ }
17386+ if (bfound < 0)
1308ab2a 17387+ goto out;
1facf9fc 17388+
5afbbe0d 17389+ for (bindex = btop; bindex <= bbot; bindex++) {
4a4d8108
AM
17390+ h_i = au_h_iptr(inode, bindex);
17391+ if (!h_i)
17392+ continue;
1facf9fc 17393+
4a4d8108
AM
17394+ err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
17395+ /* ignore this error */
17396+ /* bad action? */
1facf9fc 17397+ }
1facf9fc 17398+
4a4d8108 17399+ /* children inode number will be broken */
1facf9fc 17400+
4f0767ce 17401+out:
4a4d8108
AM
17402+ AuTraceErr(err);
17403+ return err;
1facf9fc 17404+}
17405+
4a4d8108 17406+static int hn_gen_tree(struct dentry *dentry)
1facf9fc 17407+{
4a4d8108
AM
17408+ int err, i, j, ndentry;
17409+ struct au_dcsub_pages dpages;
17410+ struct au_dpage *dpage;
17411+ struct dentry **dentries;
1facf9fc 17412+
4a4d8108
AM
17413+ err = au_dpages_init(&dpages, GFP_NOFS);
17414+ if (unlikely(err))
17415+ goto out;
17416+ err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
17417+ if (unlikely(err))
17418+ goto out_dpages;
1facf9fc 17419+
4a4d8108
AM
17420+ for (i = 0; i < dpages.ndpage; i++) {
17421+ dpage = dpages.dpages + i;
17422+ dentries = dpage->dentries;
17423+ ndentry = dpage->ndentry;
17424+ for (j = 0; j < ndentry; j++) {
17425+ struct dentry *d;
17426+
17427+ d = dentries[j];
17428+ if (IS_ROOT(d))
17429+ continue;
17430+
4a4d8108 17431+ au_digen_dec(d);
5527c038 17432+ if (d_really_is_positive(d))
4a4d8108
AM
17433+ /* todo: reset children xino?
17434+ cached children only? */
5527c038 17435+ au_iigen_dec(d_inode(d));
1308ab2a 17436+ }
dece6358 17437+ }
1facf9fc 17438+
4f0767ce 17439+out_dpages:
4a4d8108 17440+ au_dpages_free(&dpages);
dece6358 17441+
027c5e7a 17442+#if 0
4a4d8108
AM
17443+ /* discard children */
17444+ dentry_unhash(dentry);
17445+ dput(dentry);
027c5e7a 17446+#endif
4f0767ce 17447+out:
dece6358
AM
17448+ return err;
17449+}
17450+
1308ab2a 17451+/*
4a4d8108 17452+ * return 0 if processed.
1308ab2a 17453+ */
4a4d8108
AM
17454+static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
17455+ const unsigned int isdir)
dece6358 17456+{
1308ab2a 17457+ int err;
4a4d8108
AM
17458+ struct dentry *d;
17459+ struct qstr *dname;
1facf9fc 17460+
4a4d8108
AM
17461+ err = 1;
17462+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17463+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17464+ err = 0;
17465+ goto out;
17466+ }
dece6358 17467+
4a4d8108
AM
17468+ if (!isdir) {
17469+ AuDebugOn(!name);
17470+ au_iigen_dec(inode);
027c5e7a 17471+ spin_lock(&inode->i_lock);
c1595e42 17472+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 17473+ spin_lock(&d->d_lock);
4a4d8108
AM
17474+ dname = &d->d_name;
17475+ if (dname->len != nlen
027c5e7a
AM
17476+ && memcmp(dname->name, name, nlen)) {
17477+ spin_unlock(&d->d_lock);
4a4d8108 17478+ continue;
027c5e7a 17479+ }
4a4d8108 17480+ err = 0;
4a4d8108
AM
17481+ au_digen_dec(d);
17482+ spin_unlock(&d->d_lock);
17483+ break;
1facf9fc 17484+ }
027c5e7a 17485+ spin_unlock(&inode->i_lock);
1308ab2a 17486+ } else {
027c5e7a 17487+ au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
c1595e42 17488+ d = d_find_any_alias(inode);
4a4d8108
AM
17489+ if (!d) {
17490+ au_iigen_dec(inode);
17491+ goto out;
17492+ }
1facf9fc 17493+
027c5e7a 17494+ spin_lock(&d->d_lock);
4a4d8108 17495+ dname = &d->d_name;
027c5e7a
AM
17496+ if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
17497+ spin_unlock(&d->d_lock);
4a4d8108 17498+ err = hn_gen_tree(d);
027c5e7a
AM
17499+ spin_lock(&d->d_lock);
17500+ }
17501+ spin_unlock(&d->d_lock);
4a4d8108
AM
17502+ dput(d);
17503+ }
1facf9fc 17504+
4f0767ce 17505+out:
4a4d8108 17506+ AuTraceErr(err);
1308ab2a 17507+ return err;
17508+}
dece6358 17509+
4a4d8108 17510+static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
1facf9fc 17511+{
4a4d8108 17512+ int err;
1facf9fc 17513+
5527c038 17514+ if (IS_ROOT(dentry)) {
0c3ec466 17515+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17516+ return 0;
17517+ }
1308ab2a 17518+
4a4d8108
AM
17519+ err = 0;
17520+ if (!isdir) {
4a4d8108 17521+ au_digen_dec(dentry);
5527c038
JR
17522+ if (d_really_is_positive(dentry))
17523+ au_iigen_dec(d_inode(dentry));
4a4d8108 17524+ } else {
027c5e7a 17525+ au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
5527c038 17526+ if (d_really_is_positive(dentry))
4a4d8108
AM
17527+ err = hn_gen_tree(dentry);
17528+ }
17529+
17530+ AuTraceErr(err);
17531+ return err;
1facf9fc 17532+}
17533+
4a4d8108 17534+/* ---------------------------------------------------------------------- */
1facf9fc 17535+
4a4d8108
AM
17536+/* hnotify job flags */
17537+#define AuHnJob_XINO0 1
17538+#define AuHnJob_GEN (1 << 1)
17539+#define AuHnJob_DIRENT (1 << 2)
17540+#define AuHnJob_ISDIR (1 << 3)
17541+#define AuHnJob_TRYXINO0 (1 << 4)
17542+#define AuHnJob_MNTPNT (1 << 5)
17543+#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
7f207e10
AM
17544+#define au_fset_hnjob(flags, name) \
17545+ do { (flags) |= AuHnJob_##name; } while (0)
17546+#define au_fclr_hnjob(flags, name) \
17547+ do { (flags) &= ~AuHnJob_##name; } while (0)
1facf9fc 17548+
4a4d8108
AM
17549+enum {
17550+ AuHn_CHILD,
17551+ AuHn_PARENT,
17552+ AuHnLast
17553+};
1facf9fc 17554+
4a4d8108
AM
17555+struct au_hnotify_args {
17556+ struct inode *h_dir, *dir, *h_child_inode;
17557+ u32 mask;
17558+ unsigned int flags[AuHnLast];
17559+ unsigned int h_child_nlen;
17560+ char h_child_name[];
17561+};
1facf9fc 17562+
4a4d8108
AM
17563+struct hn_job_args {
17564+ unsigned int flags;
17565+ struct inode *inode, *h_inode, *dir, *h_dir;
17566+ struct dentry *dentry;
17567+ char *h_name;
17568+ int h_nlen;
17569+};
1308ab2a 17570+
4a4d8108
AM
17571+static int hn_job(struct hn_job_args *a)
17572+{
17573+ const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
076b876e 17574+ int e;
1308ab2a 17575+
4a4d8108
AM
17576+ /* reset xino */
17577+ if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
17578+ hn_xino(a->inode, a->h_inode); /* ignore this error */
1308ab2a 17579+
4a4d8108
AM
17580+ if (au_ftest_hnjob(a->flags, TRYXINO0)
17581+ && a->inode
17582+ && a->h_inode) {
be118d29 17583+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
38d290e6
JR
17584+ if (!a->h_inode->i_nlink
17585+ && !(a->h_inode->i_state & I_LINKABLE))
4a4d8108 17586+ hn_xino(a->inode, a->h_inode); /* ignore this error */
3c1bdaff 17587+ inode_unlock_shared(a->h_inode);
1308ab2a 17588+ }
1facf9fc 17589+
4a4d8108
AM
17590+ /* make the generation obsolete */
17591+ if (au_ftest_hnjob(a->flags, GEN)) {
076b876e 17592+ e = -1;
4a4d8108 17593+ if (a->inode)
076b876e 17594+ e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
4a4d8108 17595+ isdir);
076b876e 17596+ if (e && a->dentry)
4a4d8108
AM
17597+ hn_gen_by_name(a->dentry, isdir);
17598+ /* ignore this error */
1facf9fc 17599+ }
1facf9fc 17600+
4a4d8108
AM
17601+ /* make dir entries obsolete */
17602+ if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
17603+ struct au_vdir *vdir;
1facf9fc 17604+
4a4d8108
AM
17605+ vdir = au_ivdir(a->inode);
17606+ if (vdir)
17607+ vdir->vd_jiffy = 0;
17608+ /* IMustLock(a->inode); */
be118d29 17609+ /* inode_inc_iversion(a->inode); */
4a4d8108 17610+ }
1facf9fc 17611+
4a4d8108
AM
17612+ /* can do nothing but warn */
17613+ if (au_ftest_hnjob(a->flags, MNTPNT)
17614+ && a->dentry
17615+ && d_mountpoint(a->dentry))
523b37e3 17616+ pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
1facf9fc 17617+
4a4d8108 17618+ return 0;
1308ab2a 17619+}
1facf9fc 17620+
1308ab2a 17621+/* ---------------------------------------------------------------------- */
1facf9fc 17622+
4a4d8108
AM
17623+static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
17624+ struct inode *dir)
1308ab2a 17625+{
4a4d8108
AM
17626+ struct dentry *dentry, *d, *parent;
17627+ struct qstr *dname;
1308ab2a 17628+
c1595e42 17629+ parent = d_find_any_alias(dir);
4a4d8108
AM
17630+ if (!parent)
17631+ return NULL;
1308ab2a 17632+
4a4d8108 17633+ dentry = NULL;
027c5e7a 17634+ spin_lock(&parent->d_lock);
c1595e42 17635+ list_for_each_entry(d, &parent->d_subdirs, d_child) {
523b37e3 17636+ /* AuDbg("%pd\n", d); */
027c5e7a 17637+ spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
4a4d8108
AM
17638+ dname = &d->d_name;
17639+ if (dname->len != nlen || memcmp(dname->name, name, nlen))
027c5e7a
AM
17640+ goto cont_unlock;
17641+ if (au_di(d))
17642+ au_digen_dec(d);
17643+ else
17644+ goto cont_unlock;
c1595e42 17645+ if (au_dcount(d) > 0) {
027c5e7a 17646+ dentry = dget_dlock(d);
4a4d8108 17647+ spin_unlock(&d->d_lock);
027c5e7a 17648+ break;
dece6358 17649+ }
1facf9fc 17650+
f6b6e03d 17651+cont_unlock:
027c5e7a 17652+ spin_unlock(&d->d_lock);
1308ab2a 17653+ }
027c5e7a 17654+ spin_unlock(&parent->d_lock);
4a4d8108 17655+ dput(parent);
1facf9fc 17656+
4a4d8108
AM
17657+ if (dentry)
17658+ di_write_lock_child(dentry);
1308ab2a 17659+
4a4d8108
AM
17660+ return dentry;
17661+}
dece6358 17662+
4a4d8108
AM
17663+static struct inode *lookup_wlock_by_ino(struct super_block *sb,
17664+ aufs_bindex_t bindex, ino_t h_ino)
17665+{
17666+ struct inode *inode;
17667+ ino_t ino;
17668+ int err;
17669+
17670+ inode = NULL;
17671+ err = au_xino_read(sb, bindex, h_ino, &ino);
17672+ if (!err && ino)
17673+ inode = ilookup(sb, ino);
17674+ if (!inode)
17675+ goto out;
17676+
17677+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17678+ pr_warn("wrong root branch\n");
4a4d8108
AM
17679+ iput(inode);
17680+ inode = NULL;
17681+ goto out;
1308ab2a 17682+ }
17683+
4a4d8108 17684+ ii_write_lock_child(inode);
1308ab2a 17685+
4f0767ce 17686+out:
4a4d8108 17687+ return inode;
dece6358
AM
17688+}
17689+
4a4d8108 17690+static void au_hn_bh(void *_args)
1facf9fc 17691+{
4a4d8108
AM
17692+ struct au_hnotify_args *a = _args;
17693+ struct super_block *sb;
5afbbe0d 17694+ aufs_bindex_t bindex, bbot, bfound;
4a4d8108 17695+ unsigned char xino, try_iput;
1facf9fc 17696+ int err;
1308ab2a 17697+ struct inode *inode;
4a4d8108
AM
17698+ ino_t h_ino;
17699+ struct hn_job_args args;
17700+ struct dentry *dentry;
17701+ struct au_sbinfo *sbinfo;
1facf9fc 17702+
4a4d8108
AM
17703+ AuDebugOn(!_args);
17704+ AuDebugOn(!a->h_dir);
17705+ AuDebugOn(!a->dir);
17706+ AuDebugOn(!a->mask);
17707+ AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
17708+ a->mask, a->dir->i_ino, a->h_dir->i_ino,
17709+ a->h_child_inode ? a->h_child_inode->i_ino : 0);
1facf9fc 17710+
4a4d8108
AM
17711+ inode = NULL;
17712+ dentry = NULL;
17713+ /*
17714+ * do not lock a->dir->i_mutex here
17715+ * because of d_revalidate() may cause a deadlock.
17716+ */
17717+ sb = a->dir->i_sb;
17718+ AuDebugOn(!sb);
17719+ sbinfo = au_sbi(sb);
17720+ AuDebugOn(!sbinfo);
7f207e10 17721+ si_write_lock(sb, AuLock_NOPLMW);
1facf9fc 17722+
8b6a4947
AM
17723+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
17724+ switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
17725+ case FS_MOVED_FROM:
17726+ case FS_MOVED_TO:
17727+ AuWarn1("DIRREN with UDBA may not work correctly "
17728+ "for the direct rename(2)\n");
17729+ }
17730+
4a4d8108
AM
17731+ ii_read_lock_parent(a->dir);
17732+ bfound = -1;
5afbbe0d
AM
17733+ bbot = au_ibbot(a->dir);
17734+ for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
4a4d8108
AM
17735+ if (au_h_iptr(a->dir, bindex) == a->h_dir) {
17736+ bfound = bindex;
17737+ break;
17738+ }
17739+ ii_read_unlock(a->dir);
17740+ if (unlikely(bfound < 0))
17741+ goto out;
1facf9fc 17742+
4a4d8108
AM
17743+ xino = !!au_opt_test(au_mntflags(sb), XINO);
17744+ h_ino = 0;
17745+ if (a->h_child_inode)
17746+ h_ino = a->h_child_inode->i_ino;
1facf9fc 17747+
4a4d8108
AM
17748+ if (a->h_child_nlen
17749+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
17750+ || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
17751+ dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
17752+ a->dir);
17753+ try_iput = 0;
5527c038
JR
17754+ if (dentry && d_really_is_positive(dentry))
17755+ inode = d_inode(dentry);
4a4d8108
AM
17756+ if (xino && !inode && h_ino
17757+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
17758+ || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
17759+ || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
17760+ inode = lookup_wlock_by_ino(sb, bfound, h_ino);
17761+ try_iput = 1;
f0c0a007 17762+ }
1facf9fc 17763+
4a4d8108
AM
17764+ args.flags = a->flags[AuHn_CHILD];
17765+ args.dentry = dentry;
17766+ args.inode = inode;
17767+ args.h_inode = a->h_child_inode;
17768+ args.dir = a->dir;
17769+ args.h_dir = a->h_dir;
17770+ args.h_name = a->h_child_name;
17771+ args.h_nlen = a->h_child_nlen;
17772+ err = hn_job(&args);
17773+ if (dentry) {
027c5e7a 17774+ if (au_di(dentry))
4a4d8108
AM
17775+ di_write_unlock(dentry);
17776+ dput(dentry);
17777+ }
17778+ if (inode && try_iput) {
17779+ ii_write_unlock(inode);
17780+ iput(inode);
17781+ }
1facf9fc 17782+
4a4d8108
AM
17783+ ii_write_lock_parent(a->dir);
17784+ args.flags = a->flags[AuHn_PARENT];
17785+ args.dentry = NULL;
17786+ args.inode = a->dir;
17787+ args.h_inode = a->h_dir;
17788+ args.dir = NULL;
17789+ args.h_dir = NULL;
17790+ args.h_name = NULL;
17791+ args.h_nlen = 0;
17792+ err = hn_job(&args);
17793+ ii_write_unlock(a->dir);
1facf9fc 17794+
4f0767ce 17795+out:
4a4d8108
AM
17796+ iput(a->h_child_inode);
17797+ iput(a->h_dir);
17798+ iput(a->dir);
027c5e7a
AM
17799+ si_write_unlock(sb);
17800+ au_nwt_done(&sbinfo->si_nowait);
9f237c51 17801+ au_kfree_rcu(a);
dece6358 17802+}
1facf9fc 17803+
4a4d8108
AM
17804+/* ---------------------------------------------------------------------- */
17805+
17806+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17807+ struct qstr *h_child_qstr, struct inode *h_child_inode)
dece6358 17808+{
4a4d8108 17809+ int err, len;
53392da6 17810+ unsigned int flags[AuHnLast], f;
4a4d8108
AM
17811+ unsigned char isdir, isroot, wh;
17812+ struct inode *dir;
17813+ struct au_hnotify_args *args;
17814+ char *p, *h_child_name;
dece6358 17815+
1308ab2a 17816+ err = 0;
4a4d8108
AM
17817+ AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
17818+ dir = igrab(hnotify->hn_aufs_inode);
17819+ if (!dir)
17820+ goto out;
1facf9fc 17821+
4a4d8108
AM
17822+ isroot = (dir->i_ino == AUFS_ROOT_INO);
17823+ wh = 0;
17824+ h_child_name = (void *)h_child_qstr->name;
17825+ len = h_child_qstr->len;
17826+ if (h_child_name) {
17827+ if (len > AUFS_WH_PFX_LEN
17828+ && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
17829+ h_child_name += AUFS_WH_PFX_LEN;
17830+ len -= AUFS_WH_PFX_LEN;
17831+ wh = 1;
17832+ }
1facf9fc 17833+ }
dece6358 17834+
4a4d8108
AM
17835+ isdir = 0;
17836+ if (h_child_inode)
17837+ isdir = !!S_ISDIR(h_child_inode->i_mode);
17838+ flags[AuHn_PARENT] = AuHnJob_ISDIR;
17839+ flags[AuHn_CHILD] = 0;
17840+ if (isdir)
17841+ flags[AuHn_CHILD] = AuHnJob_ISDIR;
17842+ au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
17843+ au_fset_hnjob(flags[AuHn_CHILD], GEN);
17844+ switch (mask & FS_EVENTS_POSS_ON_CHILD) {
17845+ case FS_MOVED_FROM:
17846+ case FS_MOVED_TO:
17847+ au_fset_hnjob(flags[AuHn_CHILD], XINO0);
17848+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17849+ /*FALLTHROUGH*/
17850+ case FS_CREATE:
fb47a38f 17851+ AuDebugOn(!h_child_name);
4a4d8108 17852+ break;
1facf9fc 17853+
4a4d8108
AM
17854+ case FS_DELETE:
17855+ /*
17856+ * aufs never be able to get this child inode.
17857+ * revalidation should be in d_revalidate()
17858+ * by checking i_nlink, i_generation or d_unhashed().
17859+ */
17860+ AuDebugOn(!h_child_name);
17861+ au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
17862+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17863+ break;
dece6358 17864+
4a4d8108
AM
17865+ default:
17866+ AuDebugOn(1);
17867+ }
1308ab2a 17868+
4a4d8108
AM
17869+ if (wh)
17870+ h_child_inode = NULL;
1308ab2a 17871+
4a4d8108
AM
17872+ err = -ENOMEM;
17873+ /* iput() and kfree() will be called in au_hnotify() */
4a4d8108 17874+ args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
4a4d8108
AM
17875+ if (unlikely(!args)) {
17876+ AuErr1("no memory\n");
17877+ iput(dir);
17878+ goto out;
17879+ }
17880+ args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
17881+ args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
17882+ args->mask = mask;
17883+ args->dir = dir;
17884+ args->h_dir = igrab(h_dir);
17885+ if (h_child_inode)
17886+ h_child_inode = igrab(h_child_inode); /* can be NULL */
17887+ args->h_child_inode = h_child_inode;
17888+ args->h_child_nlen = len;
17889+ if (len) {
17890+ p = (void *)args;
17891+ p += sizeof(*args);
17892+ memcpy(p, h_child_name, len);
17893+ p[len] = 0;
1308ab2a 17894+ }
1308ab2a 17895+
38d290e6 17896+ /* NFS fires the event for silly-renamed one from kworker */
53392da6 17897+ f = 0;
38d290e6
JR
17898+ if (!dir->i_nlink
17899+ || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
53392da6
AM
17900+ f = AuWkq_NEST;
17901+ err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
4a4d8108
AM
17902+ if (unlikely(err)) {
17903+ pr_err("wkq %d\n", err);
17904+ iput(args->h_child_inode);
17905+ iput(args->h_dir);
17906+ iput(args->dir);
9f237c51 17907+ au_kfree_rcu(args);
1facf9fc 17908+ }
1facf9fc 17909+
4a4d8108 17910+out:
1facf9fc 17911+ return err;
17912+}
17913+
027c5e7a
AM
17914+/* ---------------------------------------------------------------------- */
17915+
17916+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
17917+{
17918+ int err;
17919+
17920+ AuDebugOn(!(udba & AuOptMask_UDBA));
17921+
17922+ err = 0;
17923+ if (au_hnotify_op.reset_br)
17924+ err = au_hnotify_op.reset_br(udba, br, perm);
17925+
17926+ return err;
17927+}
17928+
17929+int au_hnotify_init_br(struct au_branch *br, int perm)
17930+{
17931+ int err;
17932+
17933+ err = 0;
17934+ if (au_hnotify_op.init_br)
17935+ err = au_hnotify_op.init_br(br, perm);
17936+
17937+ return err;
17938+}
17939+
17940+void au_hnotify_fin_br(struct au_branch *br)
17941+{
17942+ if (au_hnotify_op.fin_br)
17943+ au_hnotify_op.fin_br(br);
17944+}
17945+
4a4d8108
AM
17946+static void au_hn_destroy_cache(void)
17947+{
1c60b727
AM
17948+ kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
17949+ au_cache[AuCache_HNOTIFY] = NULL;
4a4d8108 17950+}
1308ab2a 17951+
4a4d8108 17952+int __init au_hnotify_init(void)
1facf9fc 17953+{
1308ab2a 17954+ int err;
1308ab2a 17955+
4a4d8108 17956+ err = -ENOMEM;
1c60b727
AM
17957+ au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
17958+ if (au_cache[AuCache_HNOTIFY]) {
027c5e7a
AM
17959+ err = 0;
17960+ if (au_hnotify_op.init)
17961+ err = au_hnotify_op.init();
4a4d8108
AM
17962+ if (unlikely(err))
17963+ au_hn_destroy_cache();
1308ab2a 17964+ }
1308ab2a 17965+ AuTraceErr(err);
4a4d8108 17966+ return err;
1308ab2a 17967+}
17968+
4a4d8108 17969+void au_hnotify_fin(void)
1308ab2a 17970+{
027c5e7a
AM
17971+ if (au_hnotify_op.fin)
17972+ au_hnotify_op.fin();
f0c0a007 17973+
4a4d8108 17974+ /* cf. au_cache_fin() */
1c60b727 17975+ if (au_cache[AuCache_HNOTIFY])
4a4d8108 17976+ au_hn_destroy_cache();
dece6358 17977+}
7f207e10
AM
17978diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
17979--- /usr/share/empty/fs/aufs/iinfo.c 1970-01-01 01:00:00.000000000 +0100
9f237c51 17980+++ linux/fs/aufs/iinfo.c 2018-12-27 13:19:17.711749485 +0100
062440b3 17981@@ -0,0 +1,286 @@
cd7a4cd9 17982+// SPDX-License-Identifier: GPL-2.0
dece6358 17983+/*
b00004a5 17984+ * Copyright (C) 2005-2018 Junjiro R. Okajima
dece6358
AM
17985+ *
17986+ * This program, aufs is free software; you can redistribute it and/or modify
17987+ * it under the terms of the GNU General Public License as published by
17988+ * the Free Software Foundation; either version 2 of the License, or
17989+ * (at your option) any later version.
17990+ *
17991+ * This program is distributed in the hope that it will be useful,
17992+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17993+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17994+ * GNU General Public License for more details.
17995+ *
17996+ * You should have received a copy of the GNU General Public License
523b37e3 17997+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 17998+ */
1facf9fc 17999+
dece6358 18000+/*
4a4d8108 18001+ * inode private data
dece6358 18002+ */
1facf9fc 18003+
1308ab2a 18004+#include "aufs.h"
1facf9fc 18005+
4a4d8108 18006+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 18007+{
4a4d8108 18008+ struct inode *h_inode;
5afbbe0d 18009+ struct au_hinode *hinode;
1facf9fc 18010+
4a4d8108 18011+ IiMustAnyLock(inode);
1facf9fc 18012+
5afbbe0d
AM
18013+ hinode = au_hinode(au_ii(inode), bindex);
18014+ h_inode = hinode->hi_inode;
4a4d8108
AM
18015+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
18016+ return h_inode;
18017+}
1facf9fc 18018+
4a4d8108
AM
18019+/* todo: hard/soft set? */
18020+void au_hiput(struct au_hinode *hinode)
18021+{
18022+ au_hn_free(hinode);
18023+ dput(hinode->hi_whdentry);
18024+ iput(hinode->hi_inode);
18025+}
1facf9fc 18026+
4a4d8108
AM
18027+unsigned int au_hi_flags(struct inode *inode, int isdir)
18028+{
18029+ unsigned int flags;
18030+ const unsigned int mnt_flags = au_mntflags(inode->i_sb);
1facf9fc 18031+
4a4d8108
AM
18032+ flags = 0;
18033+ if (au_opt_test(mnt_flags, XINO))
18034+ au_fset_hi(flags, XINO);
18035+ if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
18036+ au_fset_hi(flags, HNOTIFY);
18037+ return flags;
1facf9fc 18038+}
18039+
4a4d8108
AM
18040+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
18041+ struct inode *h_inode, unsigned int flags)
1308ab2a 18042+{
4a4d8108
AM
18043+ struct au_hinode *hinode;
18044+ struct inode *hi;
18045+ struct au_iinfo *iinfo = au_ii(inode);
1facf9fc 18046+
4a4d8108 18047+ IiMustWriteLock(inode);
dece6358 18048+
5afbbe0d 18049+ hinode = au_hinode(iinfo, bindex);
4a4d8108
AM
18050+ hi = hinode->hi_inode;
18051+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
18052+
18053+ if (hi)
18054+ au_hiput(hinode);
18055+ hinode->hi_inode = h_inode;
18056+ if (h_inode) {
18057+ int err;
18058+ struct super_block *sb = inode->i_sb;
18059+ struct au_branch *br;
18060+
027c5e7a
AM
18061+ AuDebugOn(inode->i_mode
18062+ && (h_inode->i_mode & S_IFMT)
18063+ != (inode->i_mode & S_IFMT));
5afbbe0d 18064+ if (bindex == iinfo->ii_btop)
4a4d8108
AM
18065+ au_cpup_igen(inode, h_inode);
18066+ br = au_sbr(sb, bindex);
18067+ hinode->hi_id = br->br_id;
18068+ if (au_ftest_hi(flags, XINO)) {
18069+ err = au_xino_write(sb, bindex, h_inode->i_ino,
18070+ inode->i_ino);
18071+ if (unlikely(err))
18072+ AuIOErr1("failed au_xino_write() %d\n", err);
18073+ }
18074+
18075+ if (au_ftest_hi(flags, HNOTIFY)
18076+ && au_br_hnotifyable(br->br_perm)) {
027c5e7a 18077+ err = au_hn_alloc(hinode, inode);
4a4d8108
AM
18078+ if (unlikely(err))
18079+ AuIOErr1("au_hn_alloc() %d\n", err);
1308ab2a 18080+ }
18081+ }
4a4d8108 18082+}
dece6358 18083+
4a4d8108
AM
18084+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
18085+ struct dentry *h_wh)
18086+{
18087+ struct au_hinode *hinode;
dece6358 18088+
4a4d8108
AM
18089+ IiMustWriteLock(inode);
18090+
5afbbe0d 18091+ hinode = au_hinode(au_ii(inode), bindex);
4a4d8108
AM
18092+ AuDebugOn(hinode->hi_whdentry);
18093+ hinode->hi_whdentry = h_wh;
1facf9fc 18094+}
18095+
537831f9 18096+void au_update_iigen(struct inode *inode, int half)
1308ab2a 18097+{
537831f9
AM
18098+ struct au_iinfo *iinfo;
18099+ struct au_iigen *iigen;
18100+ unsigned int sigen;
18101+
18102+ sigen = au_sigen(inode->i_sb);
18103+ iinfo = au_ii(inode);
18104+ iigen = &iinfo->ii_generation;
be52b249 18105+ spin_lock(&iigen->ig_spin);
537831f9
AM
18106+ iigen->ig_generation = sigen;
18107+ if (half)
18108+ au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
18109+ else
18110+ au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
be52b249 18111+ spin_unlock(&iigen->ig_spin);
4a4d8108 18112+}
1facf9fc 18113+
4a4d8108
AM
18114+/* it may be called at remount time, too */
18115+void au_update_ibrange(struct inode *inode, int do_put_zero)
18116+{
18117+ struct au_iinfo *iinfo;
5afbbe0d 18118+ aufs_bindex_t bindex, bbot;
1facf9fc 18119+
5afbbe0d 18120+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 18121+ IiMustWriteLock(inode);
1facf9fc 18122+
5afbbe0d
AM
18123+ iinfo = au_ii(inode);
18124+ if (do_put_zero && iinfo->ii_btop >= 0) {
18125+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
18126+ bindex++) {
18127+ struct inode *h_i;
1facf9fc 18128+
5afbbe0d 18129+ h_i = au_hinode(iinfo, bindex)->hi_inode;
38d290e6
JR
18130+ if (h_i
18131+ && !h_i->i_nlink
18132+ && !(h_i->i_state & I_LINKABLE))
027c5e7a
AM
18133+ au_set_h_iptr(inode, bindex, NULL, 0);
18134+ }
4a4d8108
AM
18135+ }
18136+
5afbbe0d
AM
18137+ iinfo->ii_btop = -1;
18138+ iinfo->ii_bbot = -1;
18139+ bbot = au_sbbot(inode->i_sb);
18140+ for (bindex = 0; bindex <= bbot; bindex++)
18141+ if (au_hinode(iinfo, bindex)->hi_inode) {
18142+ iinfo->ii_btop = bindex;
4a4d8108 18143+ break;
027c5e7a 18144+ }
5afbbe0d
AM
18145+ if (iinfo->ii_btop >= 0)
18146+ for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
18147+ if (au_hinode(iinfo, bindex)->hi_inode) {
18148+ iinfo->ii_bbot = bindex;
027c5e7a
AM
18149+ break;
18150+ }
5afbbe0d 18151+ AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
1308ab2a 18152+}
1facf9fc 18153+
dece6358 18154+/* ---------------------------------------------------------------------- */
1facf9fc 18155+
4a4d8108 18156+void au_icntnr_init_once(void *_c)
dece6358 18157+{
4a4d8108
AM
18158+ struct au_icntnr *c = _c;
18159+ struct au_iinfo *iinfo = &c->iinfo;
1facf9fc 18160+
be52b249 18161+ spin_lock_init(&iinfo->ii_generation.ig_spin);
4a4d8108
AM
18162+ au_rw_init(&iinfo->ii_rwsem);
18163+ inode_init_once(&c->vfs_inode);
18164+}
1facf9fc 18165+
5afbbe0d
AM
18166+void au_hinode_init(struct au_hinode *hinode)
18167+{
18168+ hinode->hi_inode = NULL;
18169+ hinode->hi_id = -1;
18170+ au_hn_init(hinode);
18171+ hinode->hi_whdentry = NULL;
18172+}
18173+
4a4d8108
AM
18174+int au_iinfo_init(struct inode *inode)
18175+{
18176+ struct au_iinfo *iinfo;
18177+ struct super_block *sb;
5afbbe0d 18178+ struct au_hinode *hi;
4a4d8108 18179+ int nbr, i;
1facf9fc 18180+
4a4d8108
AM
18181+ sb = inode->i_sb;
18182+ iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
5afbbe0d 18183+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
18184+ if (unlikely(nbr <= 0))
18185+ nbr = 1;
5afbbe0d
AM
18186+ hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
18187+ if (hi) {
acd2b654 18188+ au_lcnt_inc(&au_sbi(sb)->si_ninodes);
5afbbe0d
AM
18189+
18190+ iinfo->ii_hinode = hi;
18191+ for (i = 0; i < nbr; i++, hi++)
18192+ au_hinode_init(hi);
1facf9fc 18193+
537831f9 18194+ iinfo->ii_generation.ig_generation = au_sigen(sb);
5afbbe0d
AM
18195+ iinfo->ii_btop = -1;
18196+ iinfo->ii_bbot = -1;
4a4d8108
AM
18197+ iinfo->ii_vdir = NULL;
18198+ return 0;
1308ab2a 18199+ }
4a4d8108
AM
18200+ return -ENOMEM;
18201+}
1facf9fc 18202+
e2f27e51 18203+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
4a4d8108 18204+{
5afbbe0d 18205+ int err, i;
4a4d8108 18206+ struct au_hinode *hip;
1facf9fc 18207+
4a4d8108
AM
18208+ AuRwMustWriteLock(&iinfo->ii_rwsem);
18209+
18210+ err = -ENOMEM;
e2f27e51
AM
18211+ hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
18212+ may_shrink);
4a4d8108
AM
18213+ if (hip) {
18214+ iinfo->ii_hinode = hip;
5afbbe0d
AM
18215+ i = iinfo->ii_bbot + 1;
18216+ hip += i;
18217+ for (; i < nbr; i++, hip++)
18218+ au_hinode_init(hip);
4a4d8108 18219+ err = 0;
1308ab2a 18220+ }
4a4d8108 18221+
1308ab2a 18222+ return err;
1facf9fc 18223+}
18224+
4a4d8108 18225+void au_iinfo_fin(struct inode *inode)
1facf9fc 18226+{
4a4d8108
AM
18227+ struct au_iinfo *iinfo;
18228+ struct au_hinode *hi;
18229+ struct super_block *sb;
5afbbe0d 18230+ aufs_bindex_t bindex, bbot;
b752ccd1 18231+ const unsigned char unlinked = !inode->i_nlink;
1308ab2a 18232+
5afbbe0d 18233+ AuDebugOn(au_is_bad_inode(inode));
1308ab2a 18234+
b752ccd1 18235+ sb = inode->i_sb;
acd2b654 18236+ au_lcnt_dec(&au_sbi(sb)->si_ninodes);
b752ccd1
AM
18237+ if (si_pid_test(sb))
18238+ au_xino_delete_inode(inode, unlinked);
18239+ else {
18240+ /*
18241+ * it is safe to hide the dependency between sbinfo and
18242+ * sb->s_umount.
18243+ */
18244+ lockdep_off();
18245+ si_noflush_read_lock(sb);
18246+ au_xino_delete_inode(inode, unlinked);
18247+ si_read_unlock(sb);
18248+ lockdep_on();
18249+ }
18250+
5afbbe0d 18251+ iinfo = au_ii(inode);
4a4d8108 18252+ if (iinfo->ii_vdir)
1c60b727 18253+ au_vdir_free(iinfo->ii_vdir);
1308ab2a 18254+
5afbbe0d 18255+ bindex = iinfo->ii_btop;
b752ccd1 18256+ if (bindex >= 0) {
5afbbe0d
AM
18257+ hi = au_hinode(iinfo, bindex);
18258+ bbot = iinfo->ii_bbot;
18259+ while (bindex++ <= bbot) {
b752ccd1 18260+ if (hi->hi_inode)
4a4d8108 18261+ au_hiput(hi);
4a4d8108
AM
18262+ hi++;
18263+ }
18264+ }
9f237c51 18265+ au_kfree_rcu(iinfo->ii_hinode);
4a4d8108 18266+ AuRwDestroy(&iinfo->ii_rwsem);
dece6358 18267+}
7f207e10
AM
18268diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
18269--- /usr/share/empty/fs/aufs/inode.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 18270+++ linux/fs/aufs/inode.c 2018-08-12 23:43:05.460124736 +0200
062440b3 18271@@ -0,0 +1,528 @@
cd7a4cd9 18272+// SPDX-License-Identifier: GPL-2.0
4a4d8108 18273+/*
b00004a5 18274+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
18275+ *
18276+ * This program, aufs is free software; you can redistribute it and/or modify
18277+ * it under the terms of the GNU General Public License as published by
18278+ * the Free Software Foundation; either version 2 of the License, or
18279+ * (at your option) any later version.
18280+ *
18281+ * This program is distributed in the hope that it will be useful,
18282+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18283+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18284+ * GNU General Public License for more details.
18285+ *
18286+ * You should have received a copy of the GNU General Public License
523b37e3 18287+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 18288+ */
1facf9fc 18289+
4a4d8108
AM
18290+/*
18291+ * inode functions
18292+ */
1facf9fc 18293+
4a4d8108 18294+#include "aufs.h"
1308ab2a 18295+
4a4d8108
AM
18296+struct inode *au_igrab(struct inode *inode)
18297+{
18298+ if (inode) {
18299+ AuDebugOn(!atomic_read(&inode->i_count));
027c5e7a 18300+ ihold(inode);
1facf9fc 18301+ }
4a4d8108
AM
18302+ return inode;
18303+}
1facf9fc 18304+
4a4d8108
AM
18305+static void au_refresh_hinode_attr(struct inode *inode, int do_version)
18306+{
18307+ au_cpup_attr_all(inode, /*force*/0);
537831f9 18308+ au_update_iigen(inode, /*half*/1);
4a4d8108 18309+ if (do_version)
be118d29 18310+ inode_inc_iversion(inode);
dece6358 18311+}
1facf9fc 18312+
027c5e7a 18313+static int au_ii_refresh(struct inode *inode, int *update)
dece6358 18314+{
e2f27e51 18315+ int err, e, nbr;
027c5e7a 18316+ umode_t type;
4a4d8108 18317+ aufs_bindex_t bindex, new_bindex;
1308ab2a 18318+ struct super_block *sb;
4a4d8108 18319+ struct au_iinfo *iinfo;
027c5e7a 18320+ struct au_hinode *p, *q, tmp;
1facf9fc 18321+
5afbbe0d 18322+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 18323+ IiMustWriteLock(inode);
1facf9fc 18324+
027c5e7a 18325+ *update = 0;
4a4d8108 18326+ sb = inode->i_sb;
e2f27e51 18327+ nbr = au_sbbot(sb) + 1;
027c5e7a 18328+ type = inode->i_mode & S_IFMT;
4a4d8108 18329+ iinfo = au_ii(inode);
e2f27e51 18330+ err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
4a4d8108 18331+ if (unlikely(err))
1308ab2a 18332+ goto out;
1facf9fc 18333+
5afbbe0d
AM
18334+ AuDebugOn(iinfo->ii_btop < 0);
18335+ p = au_hinode(iinfo, iinfo->ii_btop);
18336+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
18337+ bindex++, p++) {
18338+ if (!p->hi_inode)
18339+ continue;
1facf9fc 18340+
027c5e7a 18341+ AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
4a4d8108
AM
18342+ new_bindex = au_br_index(sb, p->hi_id);
18343+ if (new_bindex == bindex)
18344+ continue;
1facf9fc 18345+
4a4d8108 18346+ if (new_bindex < 0) {
027c5e7a 18347+ *update = 1;
4a4d8108
AM
18348+ au_hiput(p);
18349+ p->hi_inode = NULL;
18350+ continue;
1308ab2a 18351+ }
4a4d8108 18352+
5afbbe0d
AM
18353+ if (new_bindex < iinfo->ii_btop)
18354+ iinfo->ii_btop = new_bindex;
18355+ if (iinfo->ii_bbot < new_bindex)
18356+ iinfo->ii_bbot = new_bindex;
4a4d8108 18357+ /* swap two lower inode, and loop again */
5afbbe0d 18358+ q = au_hinode(iinfo, new_bindex);
4a4d8108
AM
18359+ tmp = *q;
18360+ *q = *p;
18361+ *p = tmp;
18362+ if (tmp.hi_inode) {
18363+ bindex--;
18364+ p--;
1308ab2a 18365+ }
18366+ }
4a4d8108 18367+ au_update_ibrange(inode, /*do_put_zero*/0);
e2f27e51 18368+ au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
4a4d8108
AM
18369+ e = au_dy_irefresh(inode);
18370+ if (unlikely(e && !err))
18371+ err = e;
1facf9fc 18372+
4f0767ce 18373+out:
027c5e7a
AM
18374+ AuTraceErr(err);
18375+ return err;
18376+}
18377+
b95c5147
AM
18378+void au_refresh_iop(struct inode *inode, int force_getattr)
18379+{
18380+ int type;
18381+ struct au_sbinfo *sbi = au_sbi(inode->i_sb);
18382+ const struct inode_operations *iop
18383+ = force_getattr ? aufs_iop : sbi->si_iop_array;
18384+
18385+ if (inode->i_op == iop)
18386+ return;
18387+
18388+ switch (inode->i_mode & S_IFMT) {
18389+ case S_IFDIR:
18390+ type = AuIop_DIR;
18391+ break;
18392+ case S_IFLNK:
18393+ type = AuIop_SYMLINK;
18394+ break;
18395+ default:
18396+ type = AuIop_OTHER;
18397+ break;
18398+ }
18399+
18400+ inode->i_op = iop + type;
18401+ /* unnecessary smp_wmb() */
18402+}
18403+
027c5e7a
AM
18404+int au_refresh_hinode_self(struct inode *inode)
18405+{
18406+ int err, update;
18407+
18408+ err = au_ii_refresh(inode, &update);
18409+ if (!err)
18410+ au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
18411+
18412+ AuTraceErr(err);
4a4d8108
AM
18413+ return err;
18414+}
1facf9fc 18415+
4a4d8108
AM
18416+int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
18417+{
027c5e7a 18418+ int err, e, update;
4a4d8108 18419+ unsigned int flags;
027c5e7a 18420+ umode_t mode;
5afbbe0d 18421+ aufs_bindex_t bindex, bbot;
027c5e7a 18422+ unsigned char isdir;
4a4d8108
AM
18423+ struct au_hinode *p;
18424+ struct au_iinfo *iinfo;
1facf9fc 18425+
027c5e7a 18426+ err = au_ii_refresh(inode, &update);
4a4d8108
AM
18427+ if (unlikely(err))
18428+ goto out;
18429+
18430+ update = 0;
18431+ iinfo = au_ii(inode);
5afbbe0d 18432+ p = au_hinode(iinfo, iinfo->ii_btop);
027c5e7a
AM
18433+ mode = (inode->i_mode & S_IFMT);
18434+ isdir = S_ISDIR(mode);
4a4d8108 18435+ flags = au_hi_flags(inode, isdir);
5afbbe0d
AM
18436+ bbot = au_dbbot(dentry);
18437+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
5527c038 18438+ struct inode *h_i, *h_inode;
4a4d8108
AM
18439+ struct dentry *h_d;
18440+
18441+ h_d = au_h_dptr(dentry, bindex);
5527c038 18442+ if (!h_d || d_is_negative(h_d))
4a4d8108
AM
18443+ continue;
18444+
5527c038
JR
18445+ h_inode = d_inode(h_d);
18446+ AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
5afbbe0d 18447+ if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
4a4d8108
AM
18448+ h_i = au_h_iptr(inode, bindex);
18449+ if (h_i) {
5527c038 18450+ if (h_i == h_inode)
4a4d8108
AM
18451+ continue;
18452+ err = -EIO;
18453+ break;
18454+ }
18455+ }
5afbbe0d
AM
18456+ if (bindex < iinfo->ii_btop)
18457+ iinfo->ii_btop = bindex;
18458+ if (iinfo->ii_bbot < bindex)
18459+ iinfo->ii_bbot = bindex;
5527c038 18460+ au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
4a4d8108 18461+ update = 1;
1308ab2a 18462+ }
4a4d8108
AM
18463+ au_update_ibrange(inode, /*do_put_zero*/0);
18464+ e = au_dy_irefresh(inode);
18465+ if (unlikely(e && !err))
18466+ err = e;
027c5e7a
AM
18467+ if (!err)
18468+ au_refresh_hinode_attr(inode, update && isdir);
4a4d8108 18469+
4f0767ce 18470+out:
4a4d8108 18471+ AuTraceErr(err);
1308ab2a 18472+ return err;
dece6358
AM
18473+}
18474+
4a4d8108 18475+static int set_inode(struct inode *inode, struct dentry *dentry)
dece6358 18476+{
4a4d8108
AM
18477+ int err;
18478+ unsigned int flags;
18479+ umode_t mode;
5afbbe0d 18480+ aufs_bindex_t bindex, btop, btail;
4a4d8108
AM
18481+ unsigned char isdir;
18482+ struct dentry *h_dentry;
18483+ struct inode *h_inode;
18484+ struct au_iinfo *iinfo;
b95c5147 18485+ struct inode_operations *iop;
dece6358 18486+
4a4d8108 18487+ IiMustWriteLock(inode);
dece6358 18488+
4a4d8108
AM
18489+ err = 0;
18490+ isdir = 0;
b95c5147 18491+ iop = au_sbi(inode->i_sb)->si_iop_array;
5afbbe0d
AM
18492+ btop = au_dbtop(dentry);
18493+ h_dentry = au_h_dptr(dentry, btop);
5527c038 18494+ h_inode = d_inode(h_dentry);
4a4d8108
AM
18495+ mode = h_inode->i_mode;
18496+ switch (mode & S_IFMT) {
18497+ case S_IFREG:
18498+ btail = au_dbtail(dentry);
b95c5147 18499+ inode->i_op = iop + AuIop_OTHER;
4a4d8108 18500+ inode->i_fop = &aufs_file_fop;
5afbbe0d 18501+ err = au_dy_iaop(inode, btop, h_inode);
4a4d8108
AM
18502+ if (unlikely(err))
18503+ goto out;
18504+ break;
18505+ case S_IFDIR:
18506+ isdir = 1;
18507+ btail = au_dbtaildir(dentry);
b95c5147 18508+ inode->i_op = iop + AuIop_DIR;
4a4d8108
AM
18509+ inode->i_fop = &aufs_dir_fop;
18510+ break;
18511+ case S_IFLNK:
18512+ btail = au_dbtail(dentry);
b95c5147 18513+ inode->i_op = iop + AuIop_SYMLINK;
4a4d8108
AM
18514+ break;
18515+ case S_IFBLK:
18516+ case S_IFCHR:
18517+ case S_IFIFO:
18518+ case S_IFSOCK:
18519+ btail = au_dbtail(dentry);
b95c5147 18520+ inode->i_op = iop + AuIop_OTHER;
38d290e6 18521+ init_special_inode(inode, mode, h_inode->i_rdev);
4a4d8108
AM
18522+ break;
18523+ default:
18524+ AuIOErr("Unknown file type 0%o\n", mode);
18525+ err = -EIO;
1308ab2a 18526+ goto out;
4a4d8108 18527+ }
dece6358 18528+
4a4d8108
AM
18529+ /* do not set hnotify for whiteouted dirs (SHWH mode) */
18530+ flags = au_hi_flags(inode, isdir);
18531+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
18532+ && au_ftest_hi(flags, HNOTIFY)
18533+ && dentry->d_name.len > AUFS_WH_PFX_LEN
18534+ && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
18535+ au_fclr_hi(flags, HNOTIFY);
18536+ iinfo = au_ii(inode);
5afbbe0d
AM
18537+ iinfo->ii_btop = btop;
18538+ iinfo->ii_bbot = btail;
18539+ for (bindex = btop; bindex <= btail; bindex++) {
4a4d8108
AM
18540+ h_dentry = au_h_dptr(dentry, bindex);
18541+ if (h_dentry)
18542+ au_set_h_iptr(inode, bindex,
5527c038 18543+ au_igrab(d_inode(h_dentry)), flags);
4a4d8108
AM
18544+ }
18545+ au_cpup_attr_all(inode, /*force*/1);
c1595e42
JR
18546+ /*
18547+ * to force calling aufs_get_acl() every time,
18548+ * do not call cache_no_acl() for aufs inode.
18549+ */
dece6358 18550+
4f0767ce 18551+out:
4a4d8108
AM
18552+ return err;
18553+}
dece6358 18554+
027c5e7a
AM
18555+/*
18556+ * successful returns with iinfo write_locked
18557+ * minus: errno
18558+ * zero: success, matched
18559+ * plus: no error, but unmatched
18560+ */
18561+static int reval_inode(struct inode *inode, struct dentry *dentry)
4a4d8108
AM
18562+{
18563+ int err;
cfc41e69 18564+ unsigned int gen, igflags;
5afbbe0d 18565+ aufs_bindex_t bindex, bbot;
4a4d8108 18566+ struct inode *h_inode, *h_dinode;
5527c038 18567+ struct dentry *h_dentry;
dece6358 18568+
4a4d8108
AM
18569+ /*
18570+ * before this function, if aufs got any iinfo lock, it must be only
18571+ * one, the parent dir.
18572+ * it can happen by UDBA and the obsoleted inode number.
18573+ */
18574+ err = -EIO;
18575+ if (unlikely(inode->i_ino == parent_ino(dentry)))
18576+ goto out;
18577+
027c5e7a 18578+ err = 1;
4a4d8108 18579+ ii_write_lock_new_child(inode);
5afbbe0d 18580+ h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
5527c038 18581+ h_dinode = d_inode(h_dentry);
5afbbe0d
AM
18582+ bbot = au_ibbot(inode);
18583+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108 18584+ h_inode = au_h_iptr(inode, bindex);
537831f9
AM
18585+ if (!h_inode || h_inode != h_dinode)
18586+ continue;
18587+
18588+ err = 0;
cfc41e69 18589+ gen = au_iigen(inode, &igflags);
537831f9 18590+ if (gen == au_digen(dentry)
cfc41e69 18591+ && !au_ig_ftest(igflags, HALF_REFRESHED))
4a4d8108 18592+ break;
537831f9
AM
18593+
18594+ /* fully refresh inode using dentry */
18595+ err = au_refresh_hinode(inode, dentry);
18596+ if (!err)
18597+ au_update_iigen(inode, /*half*/0);
18598+ break;
1facf9fc 18599+ }
dece6358 18600+
4a4d8108
AM
18601+ if (unlikely(err))
18602+ ii_write_unlock(inode);
4f0767ce 18603+out:
1facf9fc 18604+ return err;
18605+}
1facf9fc 18606+
4a4d8108
AM
18607+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18608+ unsigned int d_type, ino_t *ino)
1facf9fc 18609+{
521ced18
JR
18610+ int err, idx;
18611+ const int isnondir = d_type != DT_DIR;
1facf9fc 18612+
b752ccd1 18613+ /* prevent hardlinked inode number from race condition */
521ced18
JR
18614+ if (isnondir) {
18615+ err = au_xinondir_enter(sb, bindex, h_ino, &idx);
18616+ if (unlikely(err))
18617+ goto out;
4a4d8108 18618+ }
521ced18 18619+
4a4d8108
AM
18620+ err = au_xino_read(sb, bindex, h_ino, ino);
18621+ if (unlikely(err))
521ced18 18622+ goto out_xinondir;
1308ab2a 18623+
4a4d8108
AM
18624+ if (!*ino) {
18625+ err = -EIO;
18626+ *ino = au_xino_new_ino(sb);
18627+ if (unlikely(!*ino))
521ced18 18628+ goto out_xinondir;
4a4d8108
AM
18629+ err = au_xino_write(sb, bindex, h_ino, *ino);
18630+ if (unlikely(err))
521ced18 18631+ goto out_xinondir;
1308ab2a 18632+ }
1facf9fc 18633+
521ced18
JR
18634+out_xinondir:
18635+ if (isnondir && idx >= 0)
18636+ au_xinondir_leave(sb, bindex, h_ino, idx);
4f0767ce 18637+out:
1facf9fc 18638+ return err;
18639+}
18640+
4a4d8108
AM
18641+/* successful returns with iinfo write_locked */
18642+/* todo: return with unlocked? */
18643+struct inode *au_new_inode(struct dentry *dentry, int must_new)
1facf9fc 18644+{
5527c038 18645+ struct inode *inode, *h_inode;
4a4d8108
AM
18646+ struct dentry *h_dentry;
18647+ struct super_block *sb;
18648+ ino_t h_ino, ino;
521ced18 18649+ int err, idx, hlinked;
5afbbe0d 18650+ aufs_bindex_t btop;
1facf9fc 18651+
4a4d8108 18652+ sb = dentry->d_sb;
5afbbe0d
AM
18653+ btop = au_dbtop(dentry);
18654+ h_dentry = au_h_dptr(dentry, btop);
5527c038
JR
18655+ h_inode = d_inode(h_dentry);
18656+ h_ino = h_inode->i_ino;
521ced18 18657+ hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
b752ccd1 18658+
521ced18 18659+new_ino:
b752ccd1
AM
18660+ /*
18661+ * stop 'race'-ing between hardlinks under different
18662+ * parents.
18663+ */
521ced18
JR
18664+ if (hlinked) {
18665+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18666+ inode = ERR_PTR(err);
18667+ if (unlikely(err))
18668+ goto out;
18669+ }
b752ccd1 18670+
5afbbe0d 18671+ err = au_xino_read(sb, btop, h_ino, &ino);
4a4d8108
AM
18672+ inode = ERR_PTR(err);
18673+ if (unlikely(err))
521ced18 18674+ goto out_xinondir;
b752ccd1 18675+
4a4d8108
AM
18676+ if (!ino) {
18677+ ino = au_xino_new_ino(sb);
18678+ if (unlikely(!ino)) {
18679+ inode = ERR_PTR(-EIO);
521ced18 18680+ goto out_xinondir;
dece6358
AM
18681+ }
18682+ }
1facf9fc 18683+
4a4d8108
AM
18684+ AuDbg("i%lu\n", (unsigned long)ino);
18685+ inode = au_iget_locked(sb, ino);
18686+ err = PTR_ERR(inode);
18687+ if (IS_ERR(inode))
521ced18 18688+ goto out_xinondir;
1facf9fc 18689+
4a4d8108
AM
18690+ AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
18691+ if (inode->i_state & I_NEW) {
18692+ ii_write_lock_new_child(inode);
18693+ err = set_inode(inode, dentry);
18694+ if (!err) {
18695+ unlock_new_inode(inode);
521ced18 18696+ goto out_xinondir; /* success */
4a4d8108 18697+ }
1308ab2a 18698+
027c5e7a
AM
18699+ /*
18700+ * iget_failed() calls iput(), but we need to call
18701+ * ii_write_unlock() after iget_failed(). so dirty hack for
18702+ * i_count.
18703+ */
18704+ atomic_inc(&inode->i_count);
4a4d8108 18705+ iget_failed(inode);
027c5e7a 18706+ ii_write_unlock(inode);
5afbbe0d 18707+ au_xino_write(sb, btop, h_ino, /*ino*/0);
027c5e7a
AM
18708+ /* ignore this error */
18709+ goto out_iput;
18710+ } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
b752ccd1
AM
18711+ /*
18712+ * horrible race condition between lookup, readdir and copyup
18713+ * (or something).
18714+ */
521ced18
JR
18715+ if (hlinked && idx >= 0)
18716+ au_xinondir_leave(sb, btop, h_ino, idx);
027c5e7a
AM
18717+ err = reval_inode(inode, dentry);
18718+ if (unlikely(err < 0)) {
521ced18 18719+ hlinked = 0;
027c5e7a
AM
18720+ goto out_iput;
18721+ }
521ced18 18722+ if (!err)
4a4d8108 18723+ goto out; /* success */
521ced18
JR
18724+ else if (hlinked && idx >= 0) {
18725+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18726+ if (unlikely(err)) {
18727+ iput(inode);
18728+ inode = ERR_PTR(err);
18729+ goto out;
18730+ }
18731+ }
4a4d8108
AM
18732+ }
18733+
5527c038 18734+ if (unlikely(au_test_fs_unique_ino(h_inode)))
4a4d8108 18735+ AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
523b37e3 18736+ " b%d, %s, %pd, hi%lu, i%lu.\n",
5afbbe0d 18737+ btop, au_sbtype(h_dentry->d_sb), dentry,
4a4d8108
AM
18738+ (unsigned long)h_ino, (unsigned long)ino);
18739+ ino = 0;
5afbbe0d 18740+ err = au_xino_write(sb, btop, h_ino, /*ino*/0);
4a4d8108
AM
18741+ if (!err) {
18742+ iput(inode);
521ced18
JR
18743+ if (hlinked && idx >= 0)
18744+ au_xinondir_leave(sb, btop, h_ino, idx);
4a4d8108
AM
18745+ goto new_ino;
18746+ }
1308ab2a 18747+
4f0767ce 18748+out_iput:
4a4d8108 18749+ iput(inode);
4a4d8108 18750+ inode = ERR_PTR(err);
521ced18
JR
18751+out_xinondir:
18752+ if (hlinked && idx >= 0)
18753+ au_xinondir_leave(sb, btop, h_ino, idx);
4f0767ce 18754+out:
4a4d8108 18755+ return inode;
1facf9fc 18756+}
18757+
4a4d8108 18758+/* ---------------------------------------------------------------------- */
1facf9fc 18759+
4a4d8108
AM
18760+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18761+ struct inode *inode)
18762+{
18763+ int err;
076b876e 18764+ struct inode *hi;
1facf9fc 18765+
4a4d8108 18766+ err = au_br_rdonly(au_sbr(sb, bindex));
1facf9fc 18767+
4a4d8108
AM
18768+ /* pseudo-link after flushed may happen out of bounds */
18769+ if (!err
18770+ && inode
5afbbe0d
AM
18771+ && au_ibtop(inode) <= bindex
18772+ && bindex <= au_ibbot(inode)) {
4a4d8108
AM
18773+ /*
18774+ * permission check is unnecessary since vfsub routine
18775+ * will be called later
18776+ */
076b876e 18777+ hi = au_h_iptr(inode, bindex);
4a4d8108
AM
18778+ if (hi)
18779+ err = IS_IMMUTABLE(hi) ? -EROFS : 0;
1facf9fc 18780+ }
18781+
4a4d8108
AM
18782+ return err;
18783+}
dece6358 18784+
4a4d8108
AM
18785+int au_test_h_perm(struct inode *h_inode, int mask)
18786+{
2dfbb274 18787+ if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
4a4d8108
AM
18788+ return 0;
18789+ return inode_permission(h_inode, mask);
18790+}
1facf9fc 18791+
4a4d8108
AM
18792+int au_test_h_perm_sio(struct inode *h_inode, int mask)
18793+{
18794+ if (au_test_nfs(h_inode->i_sb)
18795+ && (mask & MAY_WRITE)
18796+ && S_ISDIR(h_inode->i_mode))
18797+ mask |= MAY_READ; /* force permission check */
18798+ return au_test_h_perm(h_inode, mask);
1facf9fc 18799+}
7f207e10
AM
18800diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
18801--- /usr/share/empty/fs/aufs/inode.h 1970-01-01 01:00:00.000000000 +0100
9f237c51
AM
18802+++ linux/fs/aufs/inode.h 2018-12-27 13:19:17.711749485 +0100
18803@@ -0,0 +1,698 @@
062440b3 18804+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 18805+/*
b00004a5 18806+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
18807+ *
18808+ * This program, aufs is free software; you can redistribute it and/or modify
18809+ * it under the terms of the GNU General Public License as published by
18810+ * the Free Software Foundation; either version 2 of the License, or
18811+ * (at your option) any later version.
18812+ *
18813+ * This program is distributed in the hope that it will be useful,
18814+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18815+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18816+ * GNU General Public License for more details.
18817+ *
18818+ * You should have received a copy of the GNU General Public License
523b37e3 18819+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 18820+ */
1facf9fc 18821+
1308ab2a 18822+/*
4a4d8108 18823+ * inode operations
1308ab2a 18824+ */
dece6358 18825+
4a4d8108
AM
18826+#ifndef __AUFS_INODE_H__
18827+#define __AUFS_INODE_H__
dece6358 18828+
4a4d8108 18829+#ifdef __KERNEL__
1308ab2a 18830+
4a4d8108 18831+#include <linux/fsnotify.h>
4a4d8108 18832+#include "rwsem.h"
1308ab2a 18833+
4a4d8108 18834+struct vfsmount;
1facf9fc 18835+
4a4d8108
AM
18836+struct au_hnotify {
18837+#ifdef CONFIG_AUFS_HNOTIFY
18838+#ifdef CONFIG_AUFS_HFSNOTIFY
7f207e10 18839+ /* never use fsnotify_add_vfsmount_mark() */
0c5527e5 18840+ struct fsnotify_mark hn_mark;
4a4d8108 18841+#endif
1c60b727 18842+ struct inode *hn_aufs_inode; /* no get/put */
9f237c51 18843+ struct rcu_head rcu;
4a4d8108
AM
18844+#endif
18845+} ____cacheline_aligned_in_smp;
1facf9fc 18846+
4a4d8108
AM
18847+struct au_hinode {
18848+ struct inode *hi_inode;
18849+ aufs_bindex_t hi_id;
18850+#ifdef CONFIG_AUFS_HNOTIFY
18851+ struct au_hnotify *hi_notify;
18852+#endif
dece6358 18853+
4a4d8108
AM
18854+ /* reference to the copied-up whiteout with get/put */
18855+ struct dentry *hi_whdentry;
18856+};
dece6358 18857+
537831f9
AM
18858+/* ig_flags */
18859+#define AuIG_HALF_REFRESHED 1
18860+#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
18861+#define au_ig_fset(flags, name) \
18862+ do { (flags) |= AuIG_##name; } while (0)
18863+#define au_ig_fclr(flags, name) \
18864+ do { (flags) &= ~AuIG_##name; } while (0)
18865+
18866+struct au_iigen {
be52b249 18867+ spinlock_t ig_spin;
537831f9
AM
18868+ __u32 ig_generation, ig_flags;
18869+};
18870+
4a4d8108
AM
18871+struct au_vdir;
18872+struct au_iinfo {
7a9e40b8 18873+ struct au_iigen ii_generation;
4a4d8108 18874+ struct super_block *ii_hsb1; /* no get/put */
1facf9fc 18875+
4a4d8108 18876+ struct au_rwsem ii_rwsem;
5afbbe0d 18877+ aufs_bindex_t ii_btop, ii_bbot;
4a4d8108
AM
18878+ __u32 ii_higen;
18879+ struct au_hinode *ii_hinode;
18880+ struct au_vdir *ii_vdir;
18881+};
1facf9fc 18882+
4a4d8108 18883+struct au_icntnr {
9f237c51
AM
18884+ struct au_iinfo iinfo;
18885+ struct inode vfs_inode;
18886+ struct hlist_bl_node plink;
18887+ struct rcu_head rcu;
4a4d8108 18888+} ____cacheline_aligned_in_smp;
1308ab2a 18889+
4a4d8108
AM
18890+/* au_pin flags */
18891+#define AuPin_DI_LOCKED 1
18892+#define AuPin_MNT_WRITE (1 << 1)
18893+#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
7f207e10
AM
18894+#define au_fset_pin(flags, name) \
18895+ do { (flags) |= AuPin_##name; } while (0)
18896+#define au_fclr_pin(flags, name) \
18897+ do { (flags) &= ~AuPin_##name; } while (0)
4a4d8108
AM
18898+
18899+struct au_pin {
18900+ /* input */
18901+ struct dentry *dentry;
18902+ unsigned int udba;
18903+ unsigned char lsc_di, lsc_hi, flags;
18904+ aufs_bindex_t bindex;
18905+
18906+ /* output */
18907+ struct dentry *parent;
18908+ struct au_hinode *hdir;
18909+ struct vfsmount *h_mnt;
86dc4139
AM
18910+
18911+ /* temporary unlock/relock for copyup */
18912+ struct dentry *h_dentry, *h_parent;
18913+ struct au_branch *br;
18914+ struct task_struct *task;
4a4d8108 18915+};
1facf9fc 18916+
86dc4139 18917+void au_pin_hdir_unlock(struct au_pin *p);
c1595e42 18918+int au_pin_hdir_lock(struct au_pin *p);
86dc4139 18919+int au_pin_hdir_relock(struct au_pin *p);
86dc4139
AM
18920+void au_pin_hdir_acquire_nest(struct au_pin *p);
18921+void au_pin_hdir_release(struct au_pin *p);
18922+
1308ab2a 18923+/* ---------------------------------------------------------------------- */
18924+
4a4d8108 18925+static inline struct au_iinfo *au_ii(struct inode *inode)
1facf9fc 18926+{
5afbbe0d
AM
18927+ BUG_ON(is_bad_inode(inode));
18928+ return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
4a4d8108 18929+}
1facf9fc 18930+
4a4d8108 18931+/* ---------------------------------------------------------------------- */
1facf9fc 18932+
4a4d8108
AM
18933+/* inode.c */
18934+struct inode *au_igrab(struct inode *inode);
b95c5147 18935+void au_refresh_iop(struct inode *inode, int force_getattr);
027c5e7a 18936+int au_refresh_hinode_self(struct inode *inode);
4a4d8108
AM
18937+int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
18938+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18939+ unsigned int d_type, ino_t *ino);
18940+struct inode *au_new_inode(struct dentry *dentry, int must_new);
18941+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18942+ struct inode *inode);
18943+int au_test_h_perm(struct inode *h_inode, int mask);
18944+int au_test_h_perm_sio(struct inode *h_inode, int mask);
1facf9fc 18945+
4a4d8108
AM
18946+static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
18947+ ino_t h_ino, unsigned int d_type, ino_t *ino)
18948+{
18949+#ifdef CONFIG_AUFS_SHWH
18950+ return au_ino(sb, bindex, h_ino, d_type, ino);
18951+#else
18952+ return 0;
18953+#endif
18954+}
1facf9fc 18955+
4a4d8108 18956+/* i_op.c */
b95c5147
AM
18957+enum {
18958+ AuIop_SYMLINK,
18959+ AuIop_DIR,
18960+ AuIop_OTHER,
18961+ AuIop_Last
18962+};
18963+extern struct inode_operations aufs_iop[AuIop_Last],
18964+ aufs_iop_nogetattr[AuIop_Last];
1308ab2a 18965+
4a4d8108
AM
18966+/* au_wr_dir flags */
18967+#define AuWrDir_ADD_ENTRY 1
7e9cd9fe
AM
18968+#define AuWrDir_ISDIR (1 << 1)
18969+#define AuWrDir_TMPFILE (1 << 2)
4a4d8108 18970+#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
7f207e10
AM
18971+#define au_fset_wrdir(flags, name) \
18972+ do { (flags) |= AuWrDir_##name; } while (0)
18973+#define au_fclr_wrdir(flags, name) \
18974+ do { (flags) &= ~AuWrDir_##name; } while (0)
1facf9fc 18975+
4a4d8108
AM
18976+struct au_wr_dir_args {
18977+ aufs_bindex_t force_btgt;
18978+ unsigned char flags;
18979+};
18980+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18981+ struct au_wr_dir_args *args);
dece6358 18982+
4a4d8108
AM
18983+struct dentry *au_pinned_h_parent(struct au_pin *pin);
18984+void au_pin_init(struct au_pin *pin, struct dentry *dentry,
18985+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18986+ unsigned int udba, unsigned char flags);
18987+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18988+ unsigned int udba, unsigned char flags) __must_check;
18989+int au_do_pin(struct au_pin *pin) __must_check;
18990+void au_unpin(struct au_pin *pin);
c1595e42
JR
18991+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
18992+
18993+#define AuIcpup_DID_CPUP 1
18994+#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
18995+#define au_fset_icpup(flags, name) \
18996+ do { (flags) |= AuIcpup_##name; } while (0)
18997+#define au_fclr_icpup(flags, name) \
18998+ do { (flags) &= ~AuIcpup_##name; } while (0)
18999+
19000+struct au_icpup_args {
19001+ unsigned char flags;
19002+ unsigned char pin_flags;
19003+ aufs_bindex_t btgt;
19004+ unsigned int udba;
19005+ struct au_pin pin;
19006+ struct path h_path;
19007+ struct inode *h_inode;
19008+};
19009+
19010+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
19011+ struct au_icpup_args *a);
19012+
a2654f78
AM
19013+int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
19014+ int locked);
1facf9fc 19015+
4a4d8108
AM
19016+/* i_op_add.c */
19017+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19018+ struct dentry *h_parent, int isdir);
7eafdf33
AM
19019+int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
19020+ dev_t dev);
4a4d8108 19021+int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
7eafdf33 19022+int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
b4510431 19023+ bool want_excl);
b912730e
AM
19024+struct vfsub_aopen_args;
19025+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
19026+ struct vfsub_aopen_args *args);
38d290e6 19027+int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
4a4d8108
AM
19028+int aufs_link(struct dentry *src_dentry, struct inode *dir,
19029+ struct dentry *dentry);
7eafdf33 19030+int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
1facf9fc 19031+
4a4d8108
AM
19032+/* i_op_del.c */
19033+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
19034+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
19035+ struct dentry *h_parent, int isdir);
19036+int aufs_unlink(struct inode *dir, struct dentry *dentry);
19037+int aufs_rmdir(struct inode *dir, struct dentry *dentry);
1308ab2a 19038+
4a4d8108
AM
19039+/* i_op_ren.c */
19040+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
19041+int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
f2c43d5f
AM
19042+ struct inode *dir, struct dentry *dentry,
19043+ unsigned int flags);
1facf9fc 19044+
4a4d8108
AM
19045+/* iinfo.c */
19046+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
19047+void au_hiput(struct au_hinode *hinode);
19048+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
19049+ struct dentry *h_wh);
19050+unsigned int au_hi_flags(struct inode *inode, int isdir);
1308ab2a 19051+
4a4d8108
AM
19052+/* hinode flags */
19053+#define AuHi_XINO 1
19054+#define AuHi_HNOTIFY (1 << 1)
19055+#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
7f207e10
AM
19056+#define au_fset_hi(flags, name) \
19057+ do { (flags) |= AuHi_##name; } while (0)
19058+#define au_fclr_hi(flags, name) \
19059+ do { (flags) &= ~AuHi_##name; } while (0)
1facf9fc 19060+
4a4d8108
AM
19061+#ifndef CONFIG_AUFS_HNOTIFY
19062+#undef AuHi_HNOTIFY
19063+#define AuHi_HNOTIFY 0
19064+#endif
1facf9fc 19065+
4a4d8108
AM
19066+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
19067+ struct inode *h_inode, unsigned int flags);
1facf9fc 19068+
537831f9 19069+void au_update_iigen(struct inode *inode, int half);
4a4d8108 19070+void au_update_ibrange(struct inode *inode, int do_put_zero);
1facf9fc 19071+
4a4d8108 19072+void au_icntnr_init_once(void *_c);
5afbbe0d 19073+void au_hinode_init(struct au_hinode *hinode);
4a4d8108
AM
19074+int au_iinfo_init(struct inode *inode);
19075+void au_iinfo_fin(struct inode *inode);
e2f27e51 19076+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
1308ab2a 19077+
e49829fe 19078+#ifdef CONFIG_PROC_FS
4a4d8108 19079+/* plink.c */
e49829fe 19080+int au_plink_maint(struct super_block *sb, int flags);
7e9cd9fe 19081+struct au_sbinfo;
e49829fe
JR
19082+void au_plink_maint_leave(struct au_sbinfo *sbinfo);
19083+int au_plink_maint_enter(struct super_block *sb);
4a4d8108
AM
19084+#ifdef CONFIG_AUFS_DEBUG
19085+void au_plink_list(struct super_block *sb);
19086+#else
19087+AuStubVoid(au_plink_list, struct super_block *sb)
19088+#endif
19089+int au_plink_test(struct inode *inode);
19090+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
19091+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
19092+ struct dentry *h_dentry);
e49829fe
JR
19093+void au_plink_put(struct super_block *sb, int verbose);
19094+void au_plink_clean(struct super_block *sb, int verbose);
4a4d8108 19095+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
e49829fe
JR
19096+#else
19097+AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
19098+AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
19099+AuStubInt0(au_plink_maint_enter, struct super_block *sb);
19100+AuStubVoid(au_plink_list, struct super_block *sb);
19101+AuStubInt0(au_plink_test, struct inode *inode);
19102+AuStub(struct dentry *, au_plink_lkup, return NULL,
19103+ struct inode *inode, aufs_bindex_t bindex);
19104+AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
19105+ struct dentry *h_dentry);
19106+AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
19107+AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
19108+AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
19109+#endif /* CONFIG_PROC_FS */
1facf9fc 19110+
c1595e42
JR
19111+#ifdef CONFIG_AUFS_XATTR
19112+/* xattr.c */
7e9cd9fe
AM
19113+int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
19114+ unsigned int verbose);
c1595e42 19115+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
f2c43d5f 19116+void au_xattr_init(struct super_block *sb);
c1595e42
JR
19117+#else
19118+AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
7e9cd9fe 19119+ int ignore_flags, unsigned int verbose);
f2c43d5f 19120+AuStubVoid(au_xattr_init, struct super_block *sb);
c1595e42
JR
19121+#endif
19122+
19123+#ifdef CONFIG_FS_POSIX_ACL
19124+struct posix_acl *aufs_get_acl(struct inode *inode, int type);
19125+int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
19126+#endif
19127+
19128+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
19129+enum {
19130+ AU_XATTR_SET,
c1595e42
JR
19131+ AU_ACL_SET
19132+};
19133+
f2c43d5f 19134+struct au_sxattr {
c1595e42
JR
19135+ int type;
19136+ union {
19137+ struct {
19138+ const char *name;
19139+ const void *value;
19140+ size_t size;
19141+ int flags;
19142+ } set;
19143+ struct {
c1595e42
JR
19144+ struct posix_acl *acl;
19145+ int type;
19146+ } acl_set;
19147+ } u;
19148+};
f2c43d5f
AM
19149+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
19150+ struct au_sxattr *arg);
c1595e42
JR
19151+#endif
19152+
4a4d8108 19153+/* ---------------------------------------------------------------------- */
1308ab2a 19154+
4a4d8108
AM
19155+/* lock subclass for iinfo */
19156+enum {
19157+ AuLsc_II_CHILD, /* child first */
19158+ AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
19159+ AuLsc_II_CHILD3, /* copyup dirs */
19160+ AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
19161+ AuLsc_II_PARENT2,
19162+ AuLsc_II_PARENT3, /* copyup dirs */
19163+ AuLsc_II_NEW_CHILD
19164+};
1308ab2a 19165+
1facf9fc 19166+/*
4a4d8108
AM
19167+ * ii_read_lock_child, ii_write_lock_child,
19168+ * ii_read_lock_child2, ii_write_lock_child2,
19169+ * ii_read_lock_child3, ii_write_lock_child3,
19170+ * ii_read_lock_parent, ii_write_lock_parent,
19171+ * ii_read_lock_parent2, ii_write_lock_parent2,
19172+ * ii_read_lock_parent3, ii_write_lock_parent3,
19173+ * ii_read_lock_new_child, ii_write_lock_new_child,
1facf9fc 19174+ */
4a4d8108
AM
19175+#define AuReadLockFunc(name, lsc) \
19176+static inline void ii_read_lock_##name(struct inode *i) \
19177+{ \
19178+ au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19179+}
19180+
19181+#define AuWriteLockFunc(name, lsc) \
19182+static inline void ii_write_lock_##name(struct inode *i) \
19183+{ \
19184+ au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19185+}
19186+
19187+#define AuRWLockFuncs(name, lsc) \
19188+ AuReadLockFunc(name, lsc) \
19189+ AuWriteLockFunc(name, lsc)
19190+
19191+AuRWLockFuncs(child, CHILD);
19192+AuRWLockFuncs(child2, CHILD2);
19193+AuRWLockFuncs(child3, CHILD3);
19194+AuRWLockFuncs(parent, PARENT);
19195+AuRWLockFuncs(parent2, PARENT2);
19196+AuRWLockFuncs(parent3, PARENT3);
19197+AuRWLockFuncs(new_child, NEW_CHILD);
19198+
19199+#undef AuReadLockFunc
19200+#undef AuWriteLockFunc
19201+#undef AuRWLockFuncs
1facf9fc 19202+
8b6a4947
AM
19203+#define ii_read_unlock(i) au_rw_read_unlock(&au_ii(i)->ii_rwsem)
19204+#define ii_write_unlock(i) au_rw_write_unlock(&au_ii(i)->ii_rwsem)
19205+#define ii_downgrade_lock(i) au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
1facf9fc 19206+
4a4d8108
AM
19207+#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
19208+#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
19209+#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
1facf9fc 19210+
4a4d8108 19211+/* ---------------------------------------------------------------------- */
1308ab2a 19212+
027c5e7a
AM
19213+static inline void au_icntnr_init(struct au_icntnr *c)
19214+{
19215+#ifdef CONFIG_AUFS_DEBUG
19216+ c->vfs_inode.i_mode = 0;
19217+#endif
19218+}
19219+
cfc41e69 19220+static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
4a4d8108 19221+{
537831f9
AM
19222+ unsigned int gen;
19223+ struct au_iinfo *iinfo;
be52b249 19224+ struct au_iigen *iigen;
537831f9
AM
19225+
19226+ iinfo = au_ii(inode);
be52b249
AM
19227+ iigen = &iinfo->ii_generation;
19228+ spin_lock(&iigen->ig_spin);
cfc41e69
AM
19229+ if (igflags)
19230+ *igflags = iigen->ig_flags;
be52b249
AM
19231+ gen = iigen->ig_generation;
19232+ spin_unlock(&iigen->ig_spin);
537831f9
AM
19233+
19234+ return gen;
4a4d8108 19235+}
1308ab2a 19236+
4a4d8108
AM
19237+/* tiny test for inode number */
19238+/* tmpfs generation is too rough */
19239+static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
19240+{
19241+ struct au_iinfo *iinfo;
1308ab2a 19242+
4a4d8108
AM
19243+ iinfo = au_ii(inode);
19244+ AuRwMustAnyLock(&iinfo->ii_rwsem);
19245+ return !(iinfo->ii_hsb1 == h_inode->i_sb
19246+ && iinfo->ii_higen == h_inode->i_generation);
19247+}
1308ab2a 19248+
4a4d8108
AM
19249+static inline void au_iigen_dec(struct inode *inode)
19250+{
537831f9 19251+ struct au_iinfo *iinfo;
be52b249 19252+ struct au_iigen *iigen;
537831f9
AM
19253+
19254+ iinfo = au_ii(inode);
be52b249
AM
19255+ iigen = &iinfo->ii_generation;
19256+ spin_lock(&iigen->ig_spin);
19257+ iigen->ig_generation--;
19258+ spin_unlock(&iigen->ig_spin);
027c5e7a
AM
19259+}
19260+
19261+static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
19262+{
19263+ int err;
19264+
19265+ err = 0;
537831f9 19266+ if (unlikely(inode && au_iigen(inode, NULL) != sigen))
027c5e7a
AM
19267+ err = -EIO;
19268+
19269+ return err;
4a4d8108 19270+}
1308ab2a 19271+
4a4d8108 19272+/* ---------------------------------------------------------------------- */
1308ab2a 19273+
5afbbe0d
AM
19274+static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
19275+ aufs_bindex_t bindex)
19276+{
19277+ return iinfo->ii_hinode + bindex;
19278+}
19279+
19280+static inline int au_is_bad_inode(struct inode *inode)
19281+{
19282+ return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
19283+}
19284+
4a4d8108
AM
19285+static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
19286+ aufs_bindex_t bindex)
19287+{
19288+ IiMustAnyLock(inode);
5afbbe0d 19289+ return au_hinode(au_ii(inode), bindex)->hi_id;
4a4d8108 19290+}
1308ab2a 19291+
5afbbe0d 19292+static inline aufs_bindex_t au_ibtop(struct inode *inode)
4a4d8108
AM
19293+{
19294+ IiMustAnyLock(inode);
5afbbe0d 19295+ return au_ii(inode)->ii_btop;
4a4d8108 19296+}
1308ab2a 19297+
5afbbe0d 19298+static inline aufs_bindex_t au_ibbot(struct inode *inode)
4a4d8108
AM
19299+{
19300+ IiMustAnyLock(inode);
5afbbe0d 19301+ return au_ii(inode)->ii_bbot;
4a4d8108 19302+}
1308ab2a 19303+
4a4d8108
AM
19304+static inline struct au_vdir *au_ivdir(struct inode *inode)
19305+{
19306+ IiMustAnyLock(inode);
19307+ return au_ii(inode)->ii_vdir;
19308+}
1308ab2a 19309+
4a4d8108
AM
19310+static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
19311+{
19312+ IiMustAnyLock(inode);
5afbbe0d 19313+ return au_hinode(au_ii(inode), bindex)->hi_whdentry;
4a4d8108 19314+}
1308ab2a 19315+
5afbbe0d 19316+static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 19317+{
4a4d8108 19318+ IiMustWriteLock(inode);
5afbbe0d 19319+ au_ii(inode)->ii_btop = bindex;
4a4d8108 19320+}
1308ab2a 19321+
5afbbe0d 19322+static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
4a4d8108
AM
19323+{
19324+ IiMustWriteLock(inode);
5afbbe0d 19325+ au_ii(inode)->ii_bbot = bindex;
1308ab2a 19326+}
19327+
4a4d8108
AM
19328+static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
19329+{
19330+ IiMustWriteLock(inode);
19331+ au_ii(inode)->ii_vdir = vdir;
19332+}
1facf9fc 19333+
4a4d8108 19334+static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 19335+{
4a4d8108 19336+ IiMustAnyLock(inode);
5afbbe0d 19337+ return au_hinode(au_ii(inode), bindex);
4a4d8108 19338+}
dece6358 19339+
4a4d8108 19340+/* ---------------------------------------------------------------------- */
1facf9fc 19341+
4a4d8108
AM
19342+static inline struct dentry *au_pinned_parent(struct au_pin *pin)
19343+{
19344+ if (pin)
19345+ return pin->parent;
19346+ return NULL;
1facf9fc 19347+}
19348+
4a4d8108 19349+static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
1facf9fc 19350+{
4a4d8108
AM
19351+ if (pin && pin->hdir)
19352+ return pin->hdir->hi_inode;
19353+ return NULL;
1308ab2a 19354+}
1facf9fc 19355+
4a4d8108
AM
19356+static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
19357+{
19358+ if (pin)
19359+ return pin->hdir;
19360+ return NULL;
19361+}
1facf9fc 19362+
4a4d8108 19363+static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
1308ab2a 19364+{
4a4d8108
AM
19365+ if (pin)
19366+ pin->dentry = dentry;
19367+}
1308ab2a 19368+
4a4d8108
AM
19369+static inline void au_pin_set_parent_lflag(struct au_pin *pin,
19370+ unsigned char lflag)
19371+{
19372+ if (pin) {
7f207e10 19373+ if (lflag)
4a4d8108 19374+ au_fset_pin(pin->flags, DI_LOCKED);
7f207e10 19375+ else
4a4d8108 19376+ au_fclr_pin(pin->flags, DI_LOCKED);
1308ab2a 19377+ }
4a4d8108
AM
19378+}
19379+
7e9cd9fe 19380+#if 0 /* reserved */
4a4d8108
AM
19381+static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
19382+{
19383+ if (pin) {
19384+ dput(pin->parent);
19385+ pin->parent = dget(parent);
1facf9fc 19386+ }
4a4d8108 19387+}
7e9cd9fe 19388+#endif
1facf9fc 19389+
4a4d8108
AM
19390+/* ---------------------------------------------------------------------- */
19391+
027c5e7a 19392+struct au_branch;
4a4d8108
AM
19393+#ifdef CONFIG_AUFS_HNOTIFY
19394+struct au_hnotify_op {
19395+ void (*ctl)(struct au_hinode *hinode, int do_set);
027c5e7a 19396+ int (*alloc)(struct au_hinode *hinode);
7eafdf33
AM
19397+
19398+ /*
19399+ * if it returns true, the the caller should free hinode->hi_notify,
19400+ * otherwise ->free() frees it.
19401+ */
19402+ int (*free)(struct au_hinode *hinode,
19403+ struct au_hnotify *hn) __must_check;
4a4d8108
AM
19404+
19405+ void (*fin)(void);
19406+ int (*init)(void);
027c5e7a
AM
19407+
19408+ int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
19409+ void (*fin_br)(struct au_branch *br);
19410+ int (*init_br)(struct au_branch *br, int perm);
4a4d8108
AM
19411+};
19412+
19413+/* hnotify.c */
027c5e7a 19414+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
4a4d8108
AM
19415+void au_hn_free(struct au_hinode *hinode);
19416+void au_hn_ctl(struct au_hinode *hinode, int do_set);
19417+void au_hn_reset(struct inode *inode, unsigned int flags);
19418+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
19419+ struct qstr *h_child_qstr, struct inode *h_child_inode);
027c5e7a
AM
19420+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
19421+int au_hnotify_init_br(struct au_branch *br, int perm);
19422+void au_hnotify_fin_br(struct au_branch *br);
4a4d8108
AM
19423+int __init au_hnotify_init(void);
19424+void au_hnotify_fin(void);
19425+
7f207e10 19426+/* hfsnotify.c */
4a4d8108
AM
19427+extern const struct au_hnotify_op au_hnotify_op;
19428+
19429+static inline
19430+void au_hn_init(struct au_hinode *hinode)
19431+{
19432+ hinode->hi_notify = NULL;
1308ab2a 19433+}
19434+
53392da6
AM
19435+static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
19436+{
19437+ return hinode->hi_notify;
19438+}
19439+
4a4d8108 19440+#else
c1595e42
JR
19441+AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
19442+ struct au_hinode *hinode __maybe_unused,
19443+ struct inode *inode __maybe_unused)
19444+AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
4a4d8108
AM
19445+AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
19446+AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
19447+ int do_set __maybe_unused)
19448+AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
19449+ unsigned int flags __maybe_unused)
027c5e7a
AM
19450+AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
19451+ struct au_branch *br __maybe_unused,
19452+ int perm __maybe_unused)
19453+AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
19454+ int perm __maybe_unused)
19455+AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
4a4d8108
AM
19456+AuStubInt0(__init au_hnotify_init, void)
19457+AuStubVoid(au_hnotify_fin, void)
19458+AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
19459+#endif /* CONFIG_AUFS_HNOTIFY */
19460+
19461+static inline void au_hn_suspend(struct au_hinode *hdir)
19462+{
19463+ au_hn_ctl(hdir, /*do_set*/0);
1308ab2a 19464+}
19465+
4a4d8108 19466+static inline void au_hn_resume(struct au_hinode *hdir)
1308ab2a 19467+{
4a4d8108
AM
19468+ au_hn_ctl(hdir, /*do_set*/1);
19469+}
1308ab2a 19470+
5afbbe0d 19471+static inline void au_hn_inode_lock(struct au_hinode *hdir)
4a4d8108 19472+{
febd17d6 19473+ inode_lock(hdir->hi_inode);
4a4d8108
AM
19474+ au_hn_suspend(hdir);
19475+}
dece6358 19476+
5afbbe0d 19477+static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
4a4d8108
AM
19478+ unsigned int sc __maybe_unused)
19479+{
febd17d6 19480+ inode_lock_nested(hdir->hi_inode, sc);
4a4d8108 19481+ au_hn_suspend(hdir);
1facf9fc 19482+}
1facf9fc 19483+
8b6a4947
AM
19484+#if 0 /* unused */
19485+#include "vfsub.h"
3c1bdaff
AM
19486+static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
19487+ unsigned int sc)
19488+{
be118d29 19489+ inode_lock_shared_nested(hdir->hi_inode, sc);
3c1bdaff
AM
19490+ au_hn_suspend(hdir);
19491+}
8b6a4947 19492+#endif
3c1bdaff 19493+
5afbbe0d 19494+static inline void au_hn_inode_unlock(struct au_hinode *hdir)
4a4d8108
AM
19495+{
19496+ au_hn_resume(hdir);
febd17d6 19497+ inode_unlock(hdir->hi_inode);
4a4d8108
AM
19498+}
19499+
19500+#endif /* __KERNEL__ */
19501+#endif /* __AUFS_INODE_H__ */
7f207e10
AM
19502diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
19503--- /usr/share/empty/fs/aufs/ioctl.c 1970-01-01 01:00:00.000000000 +0100
acd2b654 19504+++ linux/fs/aufs/ioctl.c 2018-10-23 12:33:35.599375796 +0200
062440b3 19505@@ -0,0 +1,220 @@
cd7a4cd9 19506+// SPDX-License-Identifier: GPL-2.0
4a4d8108 19507+/*
b00004a5 19508+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
19509+ *
19510+ * This program, aufs is free software; you can redistribute it and/or modify
19511+ * it under the terms of the GNU General Public License as published by
19512+ * the Free Software Foundation; either version 2 of the License, or
19513+ * (at your option) any later version.
19514+ *
19515+ * This program is distributed in the hope that it will be useful,
19516+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19517+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19518+ * GNU General Public License for more details.
19519+ *
19520+ * You should have received a copy of the GNU General Public License
523b37e3 19521+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
19522+ */
19523+
19524+/*
19525+ * ioctl
19526+ * plink-management and readdir in userspace.
19527+ * assist the pathconf(3) wrapper library.
c2b27bf2 19528+ * move-down
076b876e 19529+ * File-based Hierarchical Storage Management.
4a4d8108
AM
19530+ */
19531+
c2b27bf2
AM
19532+#include <linux/compat.h>
19533+#include <linux/file.h>
4a4d8108
AM
19534+#include "aufs.h"
19535+
1e00d052 19536+static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
4a4d8108
AM
19537+{
19538+ int err, fd;
5afbbe0d 19539+ aufs_bindex_t wbi, bindex, bbot;
4a4d8108
AM
19540+ struct file *h_file;
19541+ struct super_block *sb;
19542+ struct dentry *root;
1e00d052
AM
19543+ struct au_branch *br;
19544+ struct aufs_wbr_fd wbrfd = {
19545+ .oflags = au_dir_roflags,
19546+ .brid = -1
19547+ };
19548+ const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
19549+ | O_NOATIME | O_CLOEXEC;
4a4d8108 19550+
1e00d052
AM
19551+ AuDebugOn(wbrfd.oflags & ~valid);
19552+
19553+ if (arg) {
19554+ err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
19555+ if (unlikely(err)) {
19556+ err = -EFAULT;
19557+ goto out;
19558+ }
19559+
19560+ err = -EINVAL;
19561+ AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
19562+ wbrfd.oflags |= au_dir_roflags;
19563+ AuDbg("0%o\n", wbrfd.oflags);
19564+ if (unlikely(wbrfd.oflags & ~valid))
19565+ goto out;
19566+ }
19567+
2000de60 19568+ fd = get_unused_fd_flags(0);
1e00d052
AM
19569+ err = fd;
19570+ if (unlikely(fd < 0))
4a4d8108 19571+ goto out;
4a4d8108 19572+
1e00d052 19573+ h_file = ERR_PTR(-EINVAL);
4a4d8108 19574+ wbi = 0;
1e00d052 19575+ br = NULL;
4a4d8108
AM
19576+ sb = path->dentry->d_sb;
19577+ root = sb->s_root;
19578+ aufs_read_lock(root, AuLock_IR);
5afbbe0d 19579+ bbot = au_sbbot(sb);
1e00d052
AM
19580+ if (wbrfd.brid >= 0) {
19581+ wbi = au_br_index(sb, wbrfd.brid);
5afbbe0d 19582+ if (unlikely(wbi < 0 || wbi > bbot))
1e00d052
AM
19583+ goto out_unlock;
19584+ }
19585+
19586+ h_file = ERR_PTR(-ENOENT);
19587+ br = au_sbr(sb, wbi);
19588+ if (!au_br_writable(br->br_perm)) {
19589+ if (arg)
19590+ goto out_unlock;
19591+
19592+ bindex = wbi + 1;
19593+ wbi = -1;
5afbbe0d 19594+ for (; bindex <= bbot; bindex++) {
1e00d052
AM
19595+ br = au_sbr(sb, bindex);
19596+ if (au_br_writable(br->br_perm)) {
4a4d8108 19597+ wbi = bindex;
1e00d052 19598+ br = au_sbr(sb, wbi);
4a4d8108
AM
19599+ break;
19600+ }
19601+ }
4a4d8108
AM
19602+ }
19603+ AuDbg("wbi %d\n", wbi);
1e00d052 19604+ if (wbi >= 0)
392086de
AM
19605+ h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
19606+ /*force_wr*/0);
1e00d052
AM
19607+
19608+out_unlock:
4a4d8108
AM
19609+ aufs_read_unlock(root, AuLock_IR);
19610+ err = PTR_ERR(h_file);
19611+ if (IS_ERR(h_file))
19612+ goto out_fd;
19613+
acd2b654 19614+ au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
4a4d8108
AM
19615+ fd_install(fd, h_file);
19616+ err = fd;
19617+ goto out; /* success */
19618+
4f0767ce 19619+out_fd:
4a4d8108 19620+ put_unused_fd(fd);
4f0767ce 19621+out:
1e00d052 19622+ AuTraceErr(err);
4a4d8108
AM
19623+ return err;
19624+}
19625+
19626+/* ---------------------------------------------------------------------- */
19627+
19628+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
19629+{
19630+ long err;
c1595e42 19631+ struct dentry *dentry;
4a4d8108
AM
19632+
19633+ switch (cmd) {
4a4d8108
AM
19634+ case AUFS_CTL_RDU:
19635+ case AUFS_CTL_RDU_INO:
19636+ err = au_rdu_ioctl(file, cmd, arg);
19637+ break;
19638+
19639+ case AUFS_CTL_WBR_FD:
1e00d052 19640+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
19641+ break;
19642+
027c5e7a
AM
19643+ case AUFS_CTL_IBUSY:
19644+ err = au_ibusy_ioctl(file, arg);
19645+ break;
19646+
076b876e
AM
19647+ case AUFS_CTL_BRINFO:
19648+ err = au_brinfo_ioctl(file, arg);
19649+ break;
19650+
19651+ case AUFS_CTL_FHSM_FD:
2000de60 19652+ dentry = file->f_path.dentry;
c1595e42
JR
19653+ if (IS_ROOT(dentry))
19654+ err = au_fhsm_fd(dentry->d_sb, arg);
19655+ else
19656+ err = -ENOTTY;
076b876e
AM
19657+ break;
19658+
4a4d8108
AM
19659+ default:
19660+ /* do not call the lower */
19661+ AuDbg("0x%x\n", cmd);
19662+ err = -ENOTTY;
19663+ }
19664+
19665+ AuTraceErr(err);
19666+ return err;
19667+}
19668+
19669+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
19670+{
19671+ long err;
19672+
19673+ switch (cmd) {
c2b27bf2 19674+ case AUFS_CTL_MVDOWN:
2000de60 19675+ err = au_mvdown(file->f_path.dentry, (void __user *)arg);
c2b27bf2
AM
19676+ break;
19677+
4a4d8108 19678+ case AUFS_CTL_WBR_FD:
1e00d052 19679+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
19680+ break;
19681+
19682+ default:
19683+ /* do not call the lower */
19684+ AuDbg("0x%x\n", cmd);
19685+ err = -ENOTTY;
19686+ }
19687+
19688+ AuTraceErr(err);
19689+ return err;
19690+}
b752ccd1
AM
19691+
19692+#ifdef CONFIG_COMPAT
19693+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
19694+ unsigned long arg)
19695+{
19696+ long err;
19697+
19698+ switch (cmd) {
19699+ case AUFS_CTL_RDU:
19700+ case AUFS_CTL_RDU_INO:
19701+ err = au_rdu_compat_ioctl(file, cmd, arg);
19702+ break;
19703+
027c5e7a
AM
19704+ case AUFS_CTL_IBUSY:
19705+ err = au_ibusy_compat_ioctl(file, arg);
19706+ break;
19707+
076b876e
AM
19708+ case AUFS_CTL_BRINFO:
19709+ err = au_brinfo_compat_ioctl(file, arg);
19710+ break;
19711+
b752ccd1
AM
19712+ default:
19713+ err = aufs_ioctl_dir(file, cmd, arg);
19714+ }
19715+
19716+ AuTraceErr(err);
19717+ return err;
19718+}
19719+
b752ccd1
AM
19720+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
19721+ unsigned long arg)
19722+{
19723+ return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
19724+}
19725+#endif
7f207e10
AM
19726diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
19727--- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
9f237c51 19728+++ linux/fs/aufs/i_op_add.c 2018-12-27 13:19:17.711749485 +0100
acd2b654 19729@@ -0,0 +1,935 @@
cd7a4cd9 19730+// SPDX-License-Identifier: GPL-2.0
4a4d8108 19731+/*
b00004a5 19732+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
19733+ *
19734+ * This program, aufs is free software; you can redistribute it and/or modify
19735+ * it under the terms of the GNU General Public License as published by
19736+ * the Free Software Foundation; either version 2 of the License, or
19737+ * (at your option) any later version.
19738+ *
19739+ * This program is distributed in the hope that it will be useful,
19740+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19741+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19742+ * GNU General Public License for more details.
19743+ *
19744+ * You should have received a copy of the GNU General Public License
523b37e3 19745+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
19746+ */
19747+
19748+/*
19749+ * inode operations (add entry)
19750+ */
19751+
19752+#include "aufs.h"
19753+
19754+/*
19755+ * final procedure of adding a new entry, except link(2).
19756+ * remove whiteout, instantiate, copyup the parent dir's times and size
19757+ * and update version.
19758+ * if it failed, re-create the removed whiteout.
19759+ */
19760+static int epilog(struct inode *dir, aufs_bindex_t bindex,
19761+ struct dentry *wh_dentry, struct dentry *dentry)
19762+{
19763+ int err, rerr;
19764+ aufs_bindex_t bwh;
19765+ struct path h_path;
076b876e 19766+ struct super_block *sb;
4a4d8108
AM
19767+ struct inode *inode, *h_dir;
19768+ struct dentry *wh;
19769+
19770+ bwh = -1;
076b876e 19771+ sb = dir->i_sb;
4a4d8108 19772+ if (wh_dentry) {
5527c038 19773+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
4a4d8108
AM
19774+ IMustLock(h_dir);
19775+ AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
19776+ bwh = au_dbwh(dentry);
19777+ h_path.dentry = wh_dentry;
076b876e 19778+ h_path.mnt = au_sbr_mnt(sb, bindex);
4a4d8108
AM
19779+ err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
19780+ dentry);
19781+ if (unlikely(err))
19782+ goto out;
19783+ }
19784+
19785+ inode = au_new_inode(dentry, /*must_new*/1);
19786+ if (!IS_ERR(inode)) {
19787+ d_instantiate(dentry, inode);
5527c038 19788+ dir = d_inode(dentry->d_parent); /* dir inode is locked */
4a4d8108 19789+ IMustLock(dir);
b912730e 19790+ au_dir_ts(dir, bindex);
be118d29 19791+ inode_inc_iversion(dir);
076b876e 19792+ au_fhsm_wrote(sb, bindex, /*force*/0);
4a4d8108
AM
19793+ return 0; /* success */
19794+ }
19795+
19796+ err = PTR_ERR(inode);
19797+ if (!wh_dentry)
19798+ goto out;
19799+
19800+ /* revert */
19801+ /* dir inode is locked */
19802+ wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
19803+ rerr = PTR_ERR(wh);
19804+ if (IS_ERR(wh)) {
523b37e3
AM
19805+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
19806+ dentry, err, rerr);
4a4d8108
AM
19807+ err = -EIO;
19808+ } else
19809+ dput(wh);
19810+
4f0767ce 19811+out:
4a4d8108
AM
19812+ return err;
19813+}
19814+
027c5e7a
AM
19815+static int au_d_may_add(struct dentry *dentry)
19816+{
19817+ int err;
19818+
19819+ err = 0;
19820+ if (unlikely(d_unhashed(dentry)))
19821+ err = -ENOENT;
5527c038 19822+ if (unlikely(d_really_is_positive(dentry)))
027c5e7a
AM
19823+ err = -EEXIST;
19824+ return err;
19825+}
19826+
4a4d8108
AM
19827+/*
19828+ * simple tests for the adding inode operations.
19829+ * following the checks in vfs, plus the parent-child relationship.
19830+ */
19831+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19832+ struct dentry *h_parent, int isdir)
19833+{
19834+ int err;
19835+ umode_t h_mode;
19836+ struct dentry *h_dentry;
19837+ struct inode *h_inode;
19838+
19839+ err = -ENAMETOOLONG;
19840+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19841+ goto out;
19842+
19843+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 19844+ if (d_really_is_negative(dentry)) {
4a4d8108 19845+ err = -EEXIST;
5527c038 19846+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
19847+ goto out;
19848+ } else {
19849+ /* rename(2) case */
19850+ err = -EIO;
5527c038
JR
19851+ if (unlikely(d_is_negative(h_dentry)))
19852+ goto out;
19853+ h_inode = d_inode(h_dentry);
19854+ if (unlikely(!h_inode->i_nlink))
4a4d8108
AM
19855+ goto out;
19856+
19857+ h_mode = h_inode->i_mode;
19858+ if (!isdir) {
19859+ err = -EISDIR;
19860+ if (unlikely(S_ISDIR(h_mode)))
19861+ goto out;
19862+ } else if (unlikely(!S_ISDIR(h_mode))) {
19863+ err = -ENOTDIR;
19864+ goto out;
19865+ }
19866+ }
19867+
19868+ err = 0;
19869+ /* expected parent dir is locked */
19870+ if (unlikely(h_parent != h_dentry->d_parent))
19871+ err = -EIO;
19872+
4f0767ce 19873+out:
4a4d8108
AM
19874+ AuTraceErr(err);
19875+ return err;
19876+}
19877+
19878+/*
19879+ * initial procedure of adding a new entry.
19880+ * prepare writable branch and the parent dir, lock it,
19881+ * and lookup whiteout for the new entry.
19882+ */
19883+static struct dentry*
19884+lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
19885+ struct dentry *src_dentry, struct au_pin *pin,
19886+ struct au_wr_dir_args *wr_dir_args)
19887+{
19888+ struct dentry *wh_dentry, *h_parent;
19889+ struct super_block *sb;
19890+ struct au_branch *br;
19891+ int err;
19892+ unsigned int udba;
19893+ aufs_bindex_t bcpup;
19894+
523b37e3 19895+ AuDbg("%pd\n", dentry);
4a4d8108
AM
19896+
19897+ err = au_wr_dir(dentry, src_dentry, wr_dir_args);
19898+ bcpup = err;
19899+ wh_dentry = ERR_PTR(err);
19900+ if (unlikely(err < 0))
19901+ goto out;
19902+
19903+ sb = dentry->d_sb;
19904+ udba = au_opt_udba(sb);
19905+ err = au_pin(pin, dentry, bcpup, udba,
19906+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19907+ wh_dentry = ERR_PTR(err);
19908+ if (unlikely(err))
19909+ goto out;
19910+
19911+ h_parent = au_pinned_h_parent(pin);
19912+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 19913+ && au_dbtop(dentry) == bcpup)
4a4d8108
AM
19914+ err = au_may_add(dentry, bcpup, h_parent,
19915+ au_ftest_wrdir(wr_dir_args->flags, ISDIR));
19916+ else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19917+ err = -ENAMETOOLONG;
19918+ wh_dentry = ERR_PTR(err);
19919+ if (unlikely(err))
19920+ goto out_unpin;
19921+
19922+ br = au_sbr(sb, bcpup);
19923+ if (dt) {
19924+ struct path tmp = {
19925+ .dentry = h_parent,
86dc4139 19926+ .mnt = au_br_mnt(br)
4a4d8108
AM
19927+ };
19928+ au_dtime_store(dt, au_pinned_parent(pin), &tmp);
19929+ }
19930+
19931+ wh_dentry = NULL;
19932+ if (bcpup != au_dbwh(dentry))
19933+ goto out; /* success */
19934+
2000de60
JR
19935+ /*
19936+ * ENAMETOOLONG here means that if we allowed create such name, then it
19937+ * would not be able to removed in the future. So we don't allow such
19938+ * name here and we don't handle ENAMETOOLONG differently here.
19939+ */
4a4d8108
AM
19940+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
19941+
4f0767ce 19942+out_unpin:
4a4d8108
AM
19943+ if (IS_ERR(wh_dentry))
19944+ au_unpin(pin);
4f0767ce 19945+out:
4a4d8108
AM
19946+ return wh_dentry;
19947+}
19948+
19949+/* ---------------------------------------------------------------------- */
19950+
19951+enum { Mknod, Symlink, Creat };
19952+struct simple_arg {
19953+ int type;
19954+ union {
19955+ struct {
b912730e
AM
19956+ umode_t mode;
19957+ bool want_excl;
19958+ bool try_aopen;
19959+ struct vfsub_aopen_args *aopen;
4a4d8108
AM
19960+ } c;
19961+ struct {
19962+ const char *symname;
19963+ } s;
19964+ struct {
7eafdf33 19965+ umode_t mode;
4a4d8108
AM
19966+ dev_t dev;
19967+ } m;
19968+ } u;
19969+};
19970+
19971+static int add_simple(struct inode *dir, struct dentry *dentry,
19972+ struct simple_arg *arg)
19973+{
076b876e 19974+ int err, rerr;
5afbbe0d 19975+ aufs_bindex_t btop;
4a4d8108 19976+ unsigned char created;
b912730e
AM
19977+ const unsigned char try_aopen
19978+ = (arg->type == Creat && arg->u.c.try_aopen);
acd2b654 19979+ struct vfsub_aopen_args *aopen = arg->u.c.aopen;
4a4d8108
AM
19980+ struct dentry *wh_dentry, *parent;
19981+ struct inode *h_dir;
b912730e
AM
19982+ struct super_block *sb;
19983+ struct au_branch *br;
acd2b654 19984+ /* to reduce stack size */
c2b27bf2
AM
19985+ struct {
19986+ struct au_dtime dt;
19987+ struct au_pin pin;
19988+ struct path h_path;
19989+ struct au_wr_dir_args wr_dir_args;
19990+ } *a;
4a4d8108 19991+
523b37e3 19992+ AuDbg("%pd\n", dentry);
4a4d8108
AM
19993+ IMustLock(dir);
19994+
c2b27bf2
AM
19995+ err = -ENOMEM;
19996+ a = kmalloc(sizeof(*a), GFP_NOFS);
19997+ if (unlikely(!a))
19998+ goto out;
19999+ a->wr_dir_args.force_btgt = -1;
20000+ a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
20001+
4a4d8108 20002+ parent = dentry->d_parent; /* dir inode is locked */
b912730e
AM
20003+ if (!try_aopen) {
20004+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20005+ if (unlikely(err))
20006+ goto out_free;
20007+ }
027c5e7a
AM
20008+ err = au_d_may_add(dentry);
20009+ if (unlikely(err))
20010+ goto out_unlock;
b912730e
AM
20011+ if (!try_aopen)
20012+ di_write_lock_parent(parent);
c2b27bf2
AM
20013+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20014+ &a->pin, &a->wr_dir_args);
4a4d8108
AM
20015+ err = PTR_ERR(wh_dentry);
20016+ if (IS_ERR(wh_dentry))
027c5e7a 20017+ goto out_parent;
4a4d8108 20018+
5afbbe0d 20019+ btop = au_dbtop(dentry);
b912730e 20020+ sb = dentry->d_sb;
5afbbe0d
AM
20021+ br = au_sbr(sb, btop);
20022+ a->h_path.dentry = au_h_dptr(dentry, btop);
b912730e 20023+ a->h_path.mnt = au_br_mnt(br);
c2b27bf2 20024+ h_dir = au_pinned_h_dir(&a->pin);
4a4d8108
AM
20025+ switch (arg->type) {
20026+ case Creat:
acd2b654 20027+ if (!try_aopen || !h_dir->i_op->atomic_open) {
b912730e
AM
20028+ err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
20029+ arg->u.c.want_excl);
acd2b654
AM
20030+ created = !err;
20031+ if (!err && try_aopen)
20032+ aopen->file->f_mode |= FMODE_CREATED;
20033+ } else {
20034+ aopen->br = br;
20035+ err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
20036+ AuDbg("err %d\n", err);
20037+ AuDbgFile(aopen->file);
20038+ created = err >= 0
20039+ && !!(aopen->file->f_mode & FMODE_CREATED);
20040+ }
4a4d8108
AM
20041+ break;
20042+ case Symlink:
c2b27bf2 20043+ err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
acd2b654 20044+ created = !err;
4a4d8108
AM
20045+ break;
20046+ case Mknod:
c2b27bf2
AM
20047+ err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
20048+ arg->u.m.dev);
acd2b654 20049+ created = !err;
4a4d8108
AM
20050+ break;
20051+ default:
20052+ BUG();
20053+ }
acd2b654
AM
20054+ if (unlikely(err < 0))
20055+ goto out_unpin;
20056+
20057+ err = epilog(dir, btop, wh_dentry, dentry);
4a4d8108 20058+ if (!err)
acd2b654 20059+ goto out_unpin; /* success */
4a4d8108
AM
20060+
20061+ /* revert */
acd2b654 20062+ if (created /* && d_is_positive(a->h_path.dentry) */) {
523b37e3
AM
20063+ /* no delegation since it is just created */
20064+ rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
20065+ /*force*/0);
4a4d8108 20066+ if (rerr) {
523b37e3
AM
20067+ AuIOErr("%pd revert failure(%d, %d)\n",
20068+ dentry, err, rerr);
4a4d8108
AM
20069+ err = -EIO;
20070+ }
c2b27bf2 20071+ au_dtime_revert(&a->dt);
4a4d8108 20072+ }
acd2b654
AM
20073+ if (try_aopen && h_dir->i_op->atomic_open
20074+ && (aopen->file->f_mode & FMODE_OPENED))
20075+ /* aopen->file is still opened */
20076+ au_lcnt_dec(&aopen->br->br_nfiles);
4a4d8108 20077+
acd2b654 20078+out_unpin:
c2b27bf2 20079+ au_unpin(&a->pin);
4a4d8108 20080+ dput(wh_dentry);
027c5e7a 20081+out_parent:
b912730e
AM
20082+ if (!try_aopen)
20083+ di_write_unlock(parent);
027c5e7a 20084+out_unlock:
4a4d8108 20085+ if (unlikely(err)) {
5afbbe0d 20086+ au_update_dbtop(dentry);
4a4d8108
AM
20087+ d_drop(dentry);
20088+ }
b912730e
AM
20089+ if (!try_aopen)
20090+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 20091+out_free:
9f237c51 20092+ au_kfree_rcu(a);
027c5e7a 20093+out:
4a4d8108
AM
20094+ return err;
20095+}
20096+
7eafdf33
AM
20097+int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
20098+ dev_t dev)
4a4d8108
AM
20099+{
20100+ struct simple_arg arg = {
20101+ .type = Mknod,
20102+ .u.m = {
20103+ .mode = mode,
20104+ .dev = dev
20105+ }
20106+ };
20107+ return add_simple(dir, dentry, &arg);
20108+}
20109+
20110+int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
20111+{
20112+ struct simple_arg arg = {
20113+ .type = Symlink,
20114+ .u.s.symname = symname
20115+ };
20116+ return add_simple(dir, dentry, &arg);
20117+}
20118+
7eafdf33 20119+int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
b4510431 20120+ bool want_excl)
4a4d8108
AM
20121+{
20122+ struct simple_arg arg = {
20123+ .type = Creat,
20124+ .u.c = {
b4510431
AM
20125+ .mode = mode,
20126+ .want_excl = want_excl
4a4d8108
AM
20127+ }
20128+ };
20129+ return add_simple(dir, dentry, &arg);
20130+}
20131+
b912730e
AM
20132+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20133+ struct vfsub_aopen_args *aopen_args)
20134+{
20135+ struct simple_arg arg = {
20136+ .type = Creat,
20137+ .u.c = {
20138+ .mode = aopen_args->create_mode,
20139+ .want_excl = aopen_args->open_flag & O_EXCL,
20140+ .try_aopen = true,
20141+ .aopen = aopen_args
20142+ }
20143+ };
20144+ return add_simple(dir, dentry, &arg);
20145+}
20146+
38d290e6
JR
20147+int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
20148+{
20149+ int err;
20150+ aufs_bindex_t bindex;
20151+ struct super_block *sb;
20152+ struct dentry *parent, *h_parent, *h_dentry;
20153+ struct inode *h_dir, *inode;
20154+ struct vfsmount *h_mnt;
20155+ struct au_wr_dir_args wr_dir_args = {
20156+ .force_btgt = -1,
20157+ .flags = AuWrDir_TMPFILE
20158+ };
20159+
20160+ /* copy-up may happen */
febd17d6 20161+ inode_lock(dir);
38d290e6
JR
20162+
20163+ sb = dir->i_sb;
20164+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20165+ if (unlikely(err))
20166+ goto out;
20167+
20168+ err = au_di_init(dentry);
20169+ if (unlikely(err))
20170+ goto out_si;
20171+
20172+ err = -EBUSY;
20173+ parent = d_find_any_alias(dir);
20174+ AuDebugOn(!parent);
20175+ di_write_lock_parent(parent);
5527c038 20176+ if (unlikely(d_inode(parent) != dir))
38d290e6
JR
20177+ goto out_parent;
20178+
20179+ err = au_digen_test(parent, au_sigen(sb));
20180+ if (unlikely(err))
20181+ goto out_parent;
20182+
5afbbe0d
AM
20183+ bindex = au_dbtop(parent);
20184+ au_set_dbtop(dentry, bindex);
20185+ au_set_dbbot(dentry, bindex);
38d290e6
JR
20186+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
20187+ bindex = err;
20188+ if (unlikely(err < 0))
20189+ goto out_parent;
20190+
20191+ err = -EOPNOTSUPP;
20192+ h_dir = au_h_iptr(dir, bindex);
20193+ if (unlikely(!h_dir->i_op->tmpfile))
20194+ goto out_parent;
20195+
20196+ h_mnt = au_sbr_mnt(sb, bindex);
20197+ err = vfsub_mnt_want_write(h_mnt);
20198+ if (unlikely(err))
20199+ goto out_parent;
20200+
20201+ h_parent = au_h_dptr(parent, bindex);
521ced18
JR
20202+ h_dentry = vfs_tmpfile(h_parent, mode, /*open_flag*/0);
20203+ if (IS_ERR(h_dentry)) {
20204+ err = PTR_ERR(h_dentry);
38d290e6 20205+ goto out_mnt;
521ced18 20206+ }
38d290e6 20207+
5afbbe0d
AM
20208+ au_set_dbtop(dentry, bindex);
20209+ au_set_dbbot(dentry, bindex);
38d290e6
JR
20210+ au_set_h_dptr(dentry, bindex, dget(h_dentry));
20211+ inode = au_new_inode(dentry, /*must_new*/1);
20212+ if (IS_ERR(inode)) {
20213+ err = PTR_ERR(inode);
20214+ au_set_h_dptr(dentry, bindex, NULL);
5afbbe0d
AM
20215+ au_set_dbtop(dentry, -1);
20216+ au_set_dbbot(dentry, -1);
38d290e6
JR
20217+ } else {
20218+ if (!inode->i_nlink)
20219+ set_nlink(inode, 1);
20220+ d_tmpfile(dentry, inode);
20221+ au_di(dentry)->di_tmpfile = 1;
20222+
20223+ /* update without i_mutex */
5afbbe0d 20224+ if (au_ibtop(dir) == au_dbtop(dentry))
38d290e6
JR
20225+ au_cpup_attr_timesizes(dir);
20226+ }
38d290e6 20227+ dput(h_dentry);
521ced18 20228+
38d290e6
JR
20229+out_mnt:
20230+ vfsub_mnt_drop_write(h_mnt);
20231+out_parent:
20232+ di_write_unlock(parent);
20233+ dput(parent);
20234+ di_write_unlock(dentry);
5afbbe0d 20235+ if (unlikely(err)) {
38d290e6
JR
20236+ au_di_fin(dentry);
20237+ dentry->d_fsdata = NULL;
20238+ }
20239+out_si:
20240+ si_read_unlock(sb);
20241+out:
febd17d6 20242+ inode_unlock(dir);
38d290e6
JR
20243+ return err;
20244+}
20245+
4a4d8108
AM
20246+/* ---------------------------------------------------------------------- */
20247+
20248+struct au_link_args {
20249+ aufs_bindex_t bdst, bsrc;
20250+ struct au_pin pin;
20251+ struct path h_path;
20252+ struct dentry *src_parent, *parent;
20253+};
20254+
20255+static int au_cpup_before_link(struct dentry *src_dentry,
20256+ struct au_link_args *a)
20257+{
20258+ int err;
20259+ struct dentry *h_src_dentry;
c2b27bf2
AM
20260+ struct au_cp_generic cpg = {
20261+ .dentry = src_dentry,
20262+ .bdst = a->bdst,
20263+ .bsrc = a->bsrc,
20264+ .len = -1,
20265+ .pin = &a->pin,
20266+ .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
20267+ };
4a4d8108
AM
20268+
20269+ di_read_lock_parent(a->src_parent, AuLock_IR);
20270+ err = au_test_and_cpup_dirs(src_dentry, a->bdst);
20271+ if (unlikely(err))
20272+ goto out;
20273+
20274+ h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
4a4d8108
AM
20275+ err = au_pin(&a->pin, src_dentry, a->bdst,
20276+ au_opt_udba(src_dentry->d_sb),
20277+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20278+ if (unlikely(err))
20279+ goto out;
367653fa 20280+
c2b27bf2 20281+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
20282+ au_unpin(&a->pin);
20283+
4f0767ce 20284+out:
4a4d8108
AM
20285+ di_read_unlock(a->src_parent, AuLock_IR);
20286+ return err;
20287+}
20288+
86dc4139
AM
20289+static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
20290+ struct au_link_args *a)
4a4d8108
AM
20291+{
20292+ int err;
20293+ unsigned char plink;
5afbbe0d 20294+ aufs_bindex_t bbot;
4a4d8108 20295+ struct dentry *h_src_dentry;
523b37e3 20296+ struct inode *h_inode, *inode, *delegated;
4a4d8108
AM
20297+ struct super_block *sb;
20298+ struct file *h_file;
20299+
20300+ plink = 0;
20301+ h_inode = NULL;
20302+ sb = src_dentry->d_sb;
5527c038 20303+ inode = d_inode(src_dentry);
5afbbe0d 20304+ if (au_ibtop(inode) <= a->bdst)
4a4d8108
AM
20305+ h_inode = au_h_iptr(inode, a->bdst);
20306+ if (!h_inode || !h_inode->i_nlink) {
20307+ /* copyup src_dentry as the name of dentry. */
5afbbe0d
AM
20308+ bbot = au_dbbot(dentry);
20309+ if (bbot < a->bsrc)
20310+ au_set_dbbot(dentry, a->bsrc);
86dc4139
AM
20311+ au_set_h_dptr(dentry, a->bsrc,
20312+ dget(au_h_dptr(src_dentry, a->bsrc)));
20313+ dget(a->h_path.dentry);
20314+ au_set_h_dptr(dentry, a->bdst, NULL);
c1595e42
JR
20315+ AuDbg("temporary d_inode...\n");
20316+ spin_lock(&dentry->d_lock);
5527c038 20317+ dentry->d_inode = d_inode(src_dentry); /* tmp */
c1595e42 20318+ spin_unlock(&dentry->d_lock);
392086de 20319+ h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
86dc4139 20320+ if (IS_ERR(h_file))
4a4d8108 20321+ err = PTR_ERR(h_file);
86dc4139 20322+ else {
c2b27bf2
AM
20323+ struct au_cp_generic cpg = {
20324+ .dentry = dentry,
20325+ .bdst = a->bdst,
20326+ .bsrc = -1,
20327+ .len = -1,
20328+ .pin = &a->pin,
20329+ .flags = AuCpup_KEEPLINO
20330+ };
20331+ err = au_sio_cpup_simple(&cpg);
86dc4139
AM
20332+ au_h_open_post(dentry, a->bsrc, h_file);
20333+ if (!err) {
20334+ dput(a->h_path.dentry);
20335+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20336+ } else
20337+ au_set_h_dptr(dentry, a->bdst,
20338+ a->h_path.dentry);
20339+ }
c1595e42 20340+ spin_lock(&dentry->d_lock);
86dc4139 20341+ dentry->d_inode = NULL; /* restore */
c1595e42
JR
20342+ spin_unlock(&dentry->d_lock);
20343+ AuDbg("temporary d_inode...done\n");
86dc4139 20344+ au_set_h_dptr(dentry, a->bsrc, NULL);
5afbbe0d 20345+ au_set_dbbot(dentry, bbot);
4a4d8108
AM
20346+ } else {
20347+ /* the inode of src_dentry already exists on a.bdst branch */
20348+ h_src_dentry = d_find_alias(h_inode);
20349+ if (!h_src_dentry && au_plink_test(inode)) {
20350+ plink = 1;
20351+ h_src_dentry = au_plink_lkup(inode, a->bdst);
20352+ err = PTR_ERR(h_src_dentry);
20353+ if (IS_ERR(h_src_dentry))
20354+ goto out;
20355+
5527c038 20356+ if (unlikely(d_is_negative(h_src_dentry))) {
4a4d8108
AM
20357+ dput(h_src_dentry);
20358+ h_src_dentry = NULL;
20359+ }
20360+
20361+ }
20362+ if (h_src_dentry) {
523b37e3 20363+ delegated = NULL;
4a4d8108 20364+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
20365+ &a->h_path, &delegated);
20366+ if (unlikely(err == -EWOULDBLOCK)) {
20367+ pr_warn("cannot retry for NFSv4 delegation"
20368+ " for an internal link\n");
20369+ iput(delegated);
20370+ }
4a4d8108
AM
20371+ dput(h_src_dentry);
20372+ } else {
20373+ AuIOErr("no dentry found for hi%lu on b%d\n",
20374+ h_inode->i_ino, a->bdst);
20375+ err = -EIO;
20376+ }
20377+ }
20378+
20379+ if (!err && !plink)
20380+ au_plink_append(inode, a->bdst, a->h_path.dentry);
20381+
20382+out:
2cbb1c4b 20383+ AuTraceErr(err);
4a4d8108
AM
20384+ return err;
20385+}
20386+
20387+int aufs_link(struct dentry *src_dentry, struct inode *dir,
20388+ struct dentry *dentry)
20389+{
20390+ int err, rerr;
20391+ struct au_dtime dt;
20392+ struct au_link_args *a;
20393+ struct dentry *wh_dentry, *h_src_dentry;
523b37e3 20394+ struct inode *inode, *delegated;
4a4d8108
AM
20395+ struct super_block *sb;
20396+ struct au_wr_dir_args wr_dir_args = {
20397+ /* .force_btgt = -1, */
20398+ .flags = AuWrDir_ADD_ENTRY
20399+ };
20400+
20401+ IMustLock(dir);
5527c038 20402+ inode = d_inode(src_dentry);
4a4d8108
AM
20403+ IMustLock(inode);
20404+
4a4d8108
AM
20405+ err = -ENOMEM;
20406+ a = kzalloc(sizeof(*a), GFP_NOFS);
20407+ if (unlikely(!a))
20408+ goto out;
20409+
20410+ a->parent = dentry->d_parent; /* dir inode is locked */
027c5e7a
AM
20411+ err = aufs_read_and_write_lock2(dentry, src_dentry,
20412+ AuLock_NOPLM | AuLock_GEN);
e49829fe
JR
20413+ if (unlikely(err))
20414+ goto out_kfree;
38d290e6 20415+ err = au_d_linkable(src_dentry);
027c5e7a
AM
20416+ if (unlikely(err))
20417+ goto out_unlock;
20418+ err = au_d_may_add(dentry);
20419+ if (unlikely(err))
20420+ goto out_unlock;
e49829fe 20421+
4a4d8108 20422+ a->src_parent = dget_parent(src_dentry);
5afbbe0d 20423+ wr_dir_args.force_btgt = au_ibtop(inode);
4a4d8108
AM
20424+
20425+ di_write_lock_parent(a->parent);
20426+ wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
20427+ wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
20428+ &wr_dir_args);
20429+ err = PTR_ERR(wh_dentry);
20430+ if (IS_ERR(wh_dentry))
027c5e7a 20431+ goto out_parent;
4a4d8108
AM
20432+
20433+ err = 0;
20434+ sb = dentry->d_sb;
5afbbe0d 20435+ a->bdst = au_dbtop(dentry);
4a4d8108
AM
20436+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20437+ a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
5afbbe0d 20438+ a->bsrc = au_ibtop(inode);
2cbb1c4b 20439+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
38d290e6
JR
20440+ if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
20441+ h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
2cbb1c4b 20442+ if (!h_src_dentry) {
5afbbe0d 20443+ a->bsrc = au_dbtop(src_dentry);
2cbb1c4b
JR
20444+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20445+ AuDebugOn(!h_src_dentry);
38d290e6
JR
20446+ } else if (IS_ERR(h_src_dentry)) {
20447+ err = PTR_ERR(h_src_dentry);
2cbb1c4b 20448+ goto out_parent;
38d290e6 20449+ }
2cbb1c4b 20450+
f2c43d5f
AM
20451+ /*
20452+ * aufs doesn't touch the credential so
acd2b654 20453+ * security_dentry_create_files_as() is unnecessary.
f2c43d5f 20454+ */
4a4d8108
AM
20455+ if (au_opt_test(au_mntflags(sb), PLINK)) {
20456+ if (a->bdst < a->bsrc
20457+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
86dc4139 20458+ err = au_cpup_or_link(src_dentry, dentry, a);
523b37e3
AM
20459+ else {
20460+ delegated = NULL;
4a4d8108 20461+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
20462+ &a->h_path, &delegated);
20463+ if (unlikely(err == -EWOULDBLOCK)) {
20464+ pr_warn("cannot retry for NFSv4 delegation"
20465+ " for an internal link\n");
20466+ iput(delegated);
20467+ }
20468+ }
2cbb1c4b 20469+ dput(h_src_dentry);
4a4d8108
AM
20470+ } else {
20471+ /*
20472+ * copyup src_dentry to the branch we process,
20473+ * and then link(2) to it.
20474+ */
2cbb1c4b 20475+ dput(h_src_dentry);
4a4d8108
AM
20476+ if (a->bdst < a->bsrc
20477+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
20478+ au_unpin(&a->pin);
20479+ di_write_unlock(a->parent);
20480+ err = au_cpup_before_link(src_dentry, a);
20481+ di_write_lock_parent(a->parent);
20482+ if (!err)
20483+ err = au_pin(&a->pin, dentry, a->bdst,
20484+ au_opt_udba(sb),
20485+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20486+ if (unlikely(err))
20487+ goto out_wh;
20488+ }
20489+ if (!err) {
20490+ h_src_dentry = au_h_dptr(src_dentry, a->bdst);
20491+ err = -ENOENT;
5527c038 20492+ if (h_src_dentry && d_is_positive(h_src_dentry)) {
523b37e3 20493+ delegated = NULL;
4a4d8108
AM
20494+ err = vfsub_link(h_src_dentry,
20495+ au_pinned_h_dir(&a->pin),
523b37e3
AM
20496+ &a->h_path, &delegated);
20497+ if (unlikely(err == -EWOULDBLOCK)) {
20498+ pr_warn("cannot retry"
20499+ " for NFSv4 delegation"
20500+ " for an internal link\n");
20501+ iput(delegated);
20502+ }
20503+ }
4a4d8108
AM
20504+ }
20505+ }
20506+ if (unlikely(err))
20507+ goto out_unpin;
20508+
20509+ if (wh_dentry) {
20510+ a->h_path.dentry = wh_dentry;
20511+ err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
20512+ dentry);
20513+ if (unlikely(err))
20514+ goto out_revert;
20515+ }
20516+
b912730e 20517+ au_dir_ts(dir, a->bdst);
be118d29 20518+ inode_inc_iversion(dir);
4a4d8108
AM
20519+ inc_nlink(inode);
20520+ inode->i_ctime = dir->i_ctime;
027c5e7a
AM
20521+ d_instantiate(dentry, au_igrab(inode));
20522+ if (d_unhashed(a->h_path.dentry))
4a4d8108
AM
20523+ /* some filesystem calls d_drop() */
20524+ d_drop(dentry);
076b876e
AM
20525+ /* some filesystems consume an inode even hardlink */
20526+ au_fhsm_wrote(sb, a->bdst, /*force*/0);
4a4d8108
AM
20527+ goto out_unpin; /* success */
20528+
4f0767ce 20529+out_revert:
523b37e3
AM
20530+ /* no delegation since it is just created */
20531+ rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
20532+ /*delegated*/NULL, /*force*/0);
027c5e7a 20533+ if (unlikely(rerr)) {
523b37e3 20534+ AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
027c5e7a
AM
20535+ err = -EIO;
20536+ }
4a4d8108 20537+ au_dtime_revert(&dt);
4f0767ce 20538+out_unpin:
4a4d8108 20539+ au_unpin(&a->pin);
4f0767ce 20540+out_wh:
4a4d8108 20541+ dput(wh_dentry);
027c5e7a
AM
20542+out_parent:
20543+ di_write_unlock(a->parent);
20544+ dput(a->src_parent);
4f0767ce 20545+out_unlock:
4a4d8108 20546+ if (unlikely(err)) {
5afbbe0d 20547+ au_update_dbtop(dentry);
4a4d8108
AM
20548+ d_drop(dentry);
20549+ }
4a4d8108 20550+ aufs_read_and_write_unlock2(dentry, src_dentry);
e49829fe 20551+out_kfree:
9f237c51 20552+ au_kfree_rcu(a);
4f0767ce 20553+out:
86dc4139 20554+ AuTraceErr(err);
4a4d8108
AM
20555+ return err;
20556+}
20557+
7eafdf33 20558+int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
4a4d8108
AM
20559+{
20560+ int err, rerr;
20561+ aufs_bindex_t bindex;
20562+ unsigned char diropq;
20563+ struct path h_path;
20564+ struct dentry *wh_dentry, *parent, *opq_dentry;
febd17d6 20565+ struct inode *h_inode;
4a4d8108
AM
20566+ struct super_block *sb;
20567+ struct {
20568+ struct au_pin pin;
20569+ struct au_dtime dt;
20570+ } *a; /* reduce the stack usage */
20571+ struct au_wr_dir_args wr_dir_args = {
20572+ .force_btgt = -1,
20573+ .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
20574+ };
20575+
20576+ IMustLock(dir);
20577+
20578+ err = -ENOMEM;
20579+ a = kmalloc(sizeof(*a), GFP_NOFS);
20580+ if (unlikely(!a))
20581+ goto out;
20582+
027c5e7a
AM
20583+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20584+ if (unlikely(err))
20585+ goto out_free;
20586+ err = au_d_may_add(dentry);
20587+ if (unlikely(err))
20588+ goto out_unlock;
20589+
4a4d8108
AM
20590+ parent = dentry->d_parent; /* dir inode is locked */
20591+ di_write_lock_parent(parent);
20592+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20593+ &a->pin, &wr_dir_args);
20594+ err = PTR_ERR(wh_dentry);
20595+ if (IS_ERR(wh_dentry))
027c5e7a 20596+ goto out_parent;
4a4d8108
AM
20597+
20598+ sb = dentry->d_sb;
5afbbe0d 20599+ bindex = au_dbtop(dentry);
4a4d8108
AM
20600+ h_path.dentry = au_h_dptr(dentry, bindex);
20601+ h_path.mnt = au_sbr_mnt(sb, bindex);
20602+ err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
20603+ if (unlikely(err))
027c5e7a 20604+ goto out_unpin;
4a4d8108
AM
20605+
20606+ /* make the dir opaque */
20607+ diropq = 0;
febd17d6 20608+ h_inode = d_inode(h_path.dentry);
4a4d8108
AM
20609+ if (wh_dentry
20610+ || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
febd17d6 20611+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 20612+ opq_dentry = au_diropq_create(dentry, bindex);
febd17d6 20613+ inode_unlock(h_inode);
4a4d8108
AM
20614+ err = PTR_ERR(opq_dentry);
20615+ if (IS_ERR(opq_dentry))
20616+ goto out_dir;
20617+ dput(opq_dentry);
20618+ diropq = 1;
20619+ }
20620+
20621+ err = epilog(dir, bindex, wh_dentry, dentry);
20622+ if (!err) {
20623+ inc_nlink(dir);
027c5e7a 20624+ goto out_unpin; /* success */
4a4d8108
AM
20625+ }
20626+
20627+ /* revert */
20628+ if (diropq) {
20629+ AuLabel(revert opq);
febd17d6 20630+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 20631+ rerr = au_diropq_remove(dentry, bindex);
febd17d6 20632+ inode_unlock(h_inode);
4a4d8108 20633+ if (rerr) {
523b37e3
AM
20634+ AuIOErr("%pd reverting diropq failed(%d, %d)\n",
20635+ dentry, err, rerr);
4a4d8108
AM
20636+ err = -EIO;
20637+ }
20638+ }
20639+
4f0767ce 20640+out_dir:
4a4d8108
AM
20641+ AuLabel(revert dir);
20642+ rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
20643+ if (rerr) {
523b37e3
AM
20644+ AuIOErr("%pd reverting dir failed(%d, %d)\n",
20645+ dentry, err, rerr);
4a4d8108
AM
20646+ err = -EIO;
20647+ }
4a4d8108 20648+ au_dtime_revert(&a->dt);
027c5e7a 20649+out_unpin:
4a4d8108
AM
20650+ au_unpin(&a->pin);
20651+ dput(wh_dentry);
027c5e7a
AM
20652+out_parent:
20653+ di_write_unlock(parent);
20654+out_unlock:
4a4d8108 20655+ if (unlikely(err)) {
5afbbe0d 20656+ au_update_dbtop(dentry);
4a4d8108
AM
20657+ d_drop(dentry);
20658+ }
4a4d8108 20659+ aufs_read_unlock(dentry, AuLock_DW);
027c5e7a 20660+out_free:
9f237c51 20661+ au_kfree_rcu(a);
4f0767ce 20662+out:
4a4d8108
AM
20663+ return err;
20664+}
7f207e10
AM
20665diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
20666--- /usr/share/empty/fs/aufs/i_op.c 1970-01-01 01:00:00.000000000 +0100
9f237c51 20667+++ linux/fs/aufs/i_op.c 2018-12-27 13:19:17.708416053 +0100
acd2b654 20668@@ -0,0 +1,1506 @@
cd7a4cd9 20669+// SPDX-License-Identifier: GPL-2.0
4a4d8108 20670+/*
b00004a5 20671+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
20672+ *
20673+ * This program, aufs is free software; you can redistribute it and/or modify
20674+ * it under the terms of the GNU General Public License as published by
20675+ * the Free Software Foundation; either version 2 of the License, or
20676+ * (at your option) any later version.
20677+ *
20678+ * This program is distributed in the hope that it will be useful,
20679+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20680+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20681+ * GNU General Public License for more details.
20682+ *
20683+ * You should have received a copy of the GNU General Public License
523b37e3 20684+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 20685+ */
1facf9fc 20686+
1308ab2a 20687+/*
4a4d8108 20688+ * inode operations (except add/del/rename)
1308ab2a 20689+ */
4a4d8108
AM
20690+
20691+#include <linux/device_cgroup.h>
20692+#include <linux/fs_stack.h>
4a4d8108
AM
20693+#include <linux/namei.h>
20694+#include <linux/security.h>
4a4d8108
AM
20695+#include "aufs.h"
20696+
1e00d052 20697+static int h_permission(struct inode *h_inode, int mask,
79b8bda9 20698+ struct path *h_path, int brperm)
1facf9fc 20699+{
1308ab2a 20700+ int err;
4a4d8108 20701+ const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
1facf9fc 20702+
e2f27e51
AM
20703+ err = -EPERM;
20704+ if (write_mask && IS_IMMUTABLE(h_inode))
20705+ goto out;
20706+
4a4d8108 20707+ err = -EACCES;
e2f27e51
AM
20708+ if (((mask & MAY_EXEC)
20709+ && S_ISREG(h_inode->i_mode)
20710+ && (path_noexec(h_path)
cd7a4cd9 20711+ || !(h_inode->i_mode & 0111))))
4a4d8108
AM
20712+ goto out;
20713+
20714+ /*
20715+ * - skip the lower fs test in the case of write to ro branch.
20716+ * - nfs dir permission write check is optimized, but a policy for
20717+ * link/rename requires a real check.
2121bcd9 20718+ * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
b912730e 20719+ * in this case, generic_permission() returns -EOPNOTSUPP.
4a4d8108
AM
20720+ */
20721+ if ((write_mask && !au_br_writable(brperm))
20722+ || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
20723+ && write_mask && !(mask & MAY_READ))
20724+ || !h_inode->i_op->permission) {
20725+ /* AuLabel(generic_permission); */
062440b3 20726+ /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
1e00d052 20727+ err = generic_permission(h_inode, mask);
b912730e
AM
20728+ if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
20729+ err = h_inode->i_op->permission(h_inode, mask);
20730+ AuTraceErr(err);
1308ab2a 20731+ } else {
4a4d8108 20732+ /* AuLabel(h_inode->permission); */
1e00d052 20733+ err = h_inode->i_op->permission(h_inode, mask);
4a4d8108
AM
20734+ AuTraceErr(err);
20735+ }
1facf9fc 20736+
4a4d8108
AM
20737+ if (!err)
20738+ err = devcgroup_inode_permission(h_inode, mask);
7f207e10 20739+ if (!err)
4a4d8108 20740+ err = security_inode_permission(h_inode, mask);
4a4d8108
AM
20741+
20742+#if 0
20743+ if (!err) {
20744+ /* todo: do we need to call ima_path_check()? */
20745+ struct path h_path = {
20746+ .dentry =
20747+ .mnt = h_mnt
20748+ };
20749+ err = ima_path_check(&h_path,
20750+ mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
20751+ IMA_COUNT_LEAVE);
1308ab2a 20752+ }
4a4d8108 20753+#endif
dece6358 20754+
4f0767ce 20755+out:
1308ab2a 20756+ return err;
20757+}
dece6358 20758+
1e00d052 20759+static int aufs_permission(struct inode *inode, int mask)
1308ab2a 20760+{
20761+ int err;
5afbbe0d 20762+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
20763+ const unsigned char isdir = !!S_ISDIR(inode->i_mode),
20764+ write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20765+ struct inode *h_inode;
20766+ struct super_block *sb;
20767+ struct au_branch *br;
1facf9fc 20768+
027c5e7a 20769+ /* todo: support rcu-walk? */
1e00d052 20770+ if (mask & MAY_NOT_BLOCK)
027c5e7a
AM
20771+ return -ECHILD;
20772+
4a4d8108
AM
20773+ sb = inode->i_sb;
20774+ si_read_lock(sb, AuLock_FLUSH);
20775+ ii_read_lock_child(inode);
027c5e7a
AM
20776+#if 0
20777+ err = au_iigen_test(inode, au_sigen(sb));
20778+ if (unlikely(err))
20779+ goto out;
20780+#endif
dece6358 20781+
076b876e
AM
20782+ if (!isdir
20783+ || write_mask
20784+ || au_opt_test(au_mntflags(sb), DIRPERM1)) {
4a4d8108 20785+ err = au_busy_or_stale();
5afbbe0d 20786+ h_inode = au_h_iptr(inode, au_ibtop(inode));
4a4d8108
AM
20787+ if (unlikely(!h_inode
20788+ || (h_inode->i_mode & S_IFMT)
20789+ != (inode->i_mode & S_IFMT)))
20790+ goto out;
1facf9fc 20791+
4a4d8108 20792+ err = 0;
5afbbe0d 20793+ bindex = au_ibtop(inode);
4a4d8108 20794+ br = au_sbr(sb, bindex);
79b8bda9 20795+ err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
4a4d8108
AM
20796+ if (write_mask
20797+ && !err
20798+ && !special_file(h_inode->i_mode)) {
20799+ /* test whether the upper writable branch exists */
20800+ err = -EROFS;
20801+ for (; bindex >= 0; bindex--)
20802+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
20803+ err = 0;
20804+ break;
20805+ }
20806+ }
20807+ goto out;
20808+ }
dece6358 20809+
4a4d8108 20810+ /* non-write to dir */
1308ab2a 20811+ err = 0;
5afbbe0d
AM
20812+ bbot = au_ibbot(inode);
20813+ for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
20814+ h_inode = au_h_iptr(inode, bindex);
20815+ if (h_inode) {
20816+ err = au_busy_or_stale();
20817+ if (unlikely(!S_ISDIR(h_inode->i_mode)))
20818+ break;
20819+
20820+ br = au_sbr(sb, bindex);
79b8bda9 20821+ err = h_permission(h_inode, mask, &br->br_path,
4a4d8108
AM
20822+ br->br_perm);
20823+ }
20824+ }
1308ab2a 20825+
4f0767ce 20826+out:
4a4d8108
AM
20827+ ii_read_unlock(inode);
20828+ si_read_unlock(sb);
1308ab2a 20829+ return err;
20830+}
20831+
4a4d8108 20832+/* ---------------------------------------------------------------------- */
1facf9fc 20833+
4a4d8108 20834+static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
b4510431 20835+ unsigned int flags)
4a4d8108
AM
20836+{
20837+ struct dentry *ret, *parent;
b752ccd1 20838+ struct inode *inode;
4a4d8108 20839+ struct super_block *sb;
1716fcea 20840+ int err, npositive;
dece6358 20841+
4a4d8108 20842+ IMustLock(dir);
1308ab2a 20843+
537831f9
AM
20844+ /* todo: support rcu-walk? */
20845+ ret = ERR_PTR(-ECHILD);
20846+ if (flags & LOOKUP_RCU)
20847+ goto out;
20848+
20849+ ret = ERR_PTR(-ENAMETOOLONG);
20850+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20851+ goto out;
20852+
4a4d8108 20853+ sb = dir->i_sb;
7f207e10
AM
20854+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20855+ ret = ERR_PTR(err);
20856+ if (unlikely(err))
20857+ goto out;
20858+
4a4d8108
AM
20859+ err = au_di_init(dentry);
20860+ ret = ERR_PTR(err);
20861+ if (unlikely(err))
7f207e10 20862+ goto out_si;
1308ab2a 20863+
9dbd164d 20864+ inode = NULL;
027c5e7a 20865+ npositive = 0; /* suppress a warning */
4a4d8108
AM
20866+ parent = dentry->d_parent; /* dir inode is locked */
20867+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
20868+ err = au_alive_dir(parent);
20869+ if (!err)
20870+ err = au_digen_test(parent, au_sigen(sb));
20871+ if (!err) {
5afbbe0d
AM
20872+ /* regardless LOOKUP_CREATE, always ALLOW_NEG */
20873+ npositive = au_lkup_dentry(dentry, au_dbtop(parent),
20874+ AuLkup_ALLOW_NEG);
027c5e7a
AM
20875+ err = npositive;
20876+ }
4a4d8108 20877+ di_read_unlock(parent, AuLock_IR);
4a4d8108
AM
20878+ ret = ERR_PTR(err);
20879+ if (unlikely(err < 0))
20880+ goto out_unlock;
1308ab2a 20881+
4a4d8108 20882+ if (npositive) {
b752ccd1 20883+ inode = au_new_inode(dentry, /*must_new*/0);
c1595e42
JR
20884+ if (IS_ERR(inode)) {
20885+ ret = (void *)inode;
20886+ inode = NULL;
20887+ goto out_unlock;
20888+ }
9dbd164d 20889+ }
4a4d8108 20890+
c1595e42
JR
20891+ if (inode)
20892+ atomic_inc(&inode->i_count);
4a4d8108 20893+ ret = d_splice_alias(inode, dentry);
537831f9
AM
20894+#if 0
20895+ if (unlikely(d_need_lookup(dentry))) {
20896+ spin_lock(&dentry->d_lock);
20897+ dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
20898+ spin_unlock(&dentry->d_lock);
20899+ } else
20900+#endif
c1595e42 20901+ if (inode) {
2000de60 20902+ if (!IS_ERR(ret)) {
c1595e42 20903+ iput(inode);
2000de60
JR
20904+ if (ret && ret != dentry)
20905+ ii_write_unlock(inode);
20906+ } else {
c1595e42
JR
20907+ ii_write_unlock(inode);
20908+ iput(inode);
20909+ inode = NULL;
20910+ }
7f207e10 20911+ }
1facf9fc 20912+
4f0767ce 20913+out_unlock:
4a4d8108 20914+ di_write_unlock(dentry);
7f207e10 20915+out_si:
4a4d8108 20916+ si_read_unlock(sb);
7f207e10 20917+out:
4a4d8108
AM
20918+ return ret;
20919+}
1facf9fc 20920+
4a4d8108 20921+/* ---------------------------------------------------------------------- */
1facf9fc 20922+
acd2b654
AM
20923+/*
20924+ * very dirty and complicated aufs ->atomic_open().
20925+ * aufs_atomic_open()
20926+ * + au_aopen_or_create()
20927+ * + add_simple()
20928+ * + vfsub_atomic_open()
20929+ * + branch fs ->atomic_open()
20930+ * may call the actual 'open' for h_file
20931+ * + inc br_nfiles only if opened
20932+ * + au_aopen_no_open() or au_aopen_do_open()
20933+ *
20934+ * au_aopen_do_open()
20935+ * + finish_open()
20936+ * + au_do_aopen()
20937+ * + au_do_open() the body of all 'open'
20938+ * + au_do_open_nondir()
20939+ * set the passed h_file
20940+ *
20941+ * au_aopen_no_open()
20942+ * + finish_no_open()
20943+ */
20944+
b912730e 20945+struct aopen_node {
8b6a4947 20946+ struct hlist_bl_node hblist;
b912730e
AM
20947+ struct file *file, *h_file;
20948+};
20949+
20950+static int au_do_aopen(struct inode *inode, struct file *file)
20951+{
8b6a4947
AM
20952+ struct hlist_bl_head *aopen;
20953+ struct hlist_bl_node *pos;
b912730e
AM
20954+ struct aopen_node *node;
20955+ struct au_do_open_args args = {
8b6a4947
AM
20956+ .aopen = 1,
20957+ .open = au_do_open_nondir
b912730e
AM
20958+ };
20959+
20960+ aopen = &au_sbi(inode->i_sb)->si_aopen;
8b6a4947
AM
20961+ hlist_bl_lock(aopen);
20962+ hlist_bl_for_each_entry(node, pos, aopen, hblist)
b912730e
AM
20963+ if (node->file == file) {
20964+ args.h_file = node->h_file;
20965+ break;
20966+ }
8b6a4947 20967+ hlist_bl_unlock(aopen);
b912730e
AM
20968+ /* AuDebugOn(!args.h_file); */
20969+
20970+ return au_do_open(file, &args);
20971+}
20972+
acd2b654
AM
20973+static int au_aopen_do_open(struct file *file, struct dentry *dentry,
20974+ struct aopen_node *aopen_node)
20975+{
20976+ int err;
20977+ struct hlist_bl_head *aopen;
20978+
20979+ AuLabel(here);
20980+ aopen = &au_sbi(dentry->d_sb)->si_aopen;
20981+ au_hbl_add(&aopen_node->hblist, aopen);
20982+ err = finish_open(file, dentry, au_do_aopen);
20983+ au_hbl_del(&aopen_node->hblist, aopen);
20984+ /* AuDbgFile(file); */
20985+ AuDbg("%pd%s%s\n", dentry,
20986+ (file->f_mode & FMODE_CREATED) ? " created" : "",
20987+ (file->f_mode & FMODE_OPENED) ? " opened" : "");
20988+
20989+ AuTraceErr(err);
20990+ return err;
20991+}
20992+
20993+static int au_aopen_no_open(struct file *file, struct dentry *dentry)
20994+{
20995+ int err;
20996+
20997+ AuLabel(here);
20998+ dget(dentry);
20999+ err = finish_no_open(file, dentry);
21000+
21001+ AuTraceErr(err);
21002+ return err;
21003+}
21004+
b912730e
AM
21005+static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
21006+ struct file *file, unsigned int open_flag,
acd2b654 21007+ umode_t create_mode)
b912730e 21008+{
acd2b654 21009+ int err, did_open;
5afbbe0d 21010+ unsigned int lkup_flags;
acd2b654
AM
21011+ aufs_bindex_t bindex;
21012+ struct super_block *sb;
f0c0a007 21013+ struct dentry *parent, *d;
b912730e
AM
21014+ struct vfsub_aopen_args args = {
21015+ .open_flag = open_flag,
acd2b654 21016+ .create_mode = create_mode
b912730e
AM
21017+ };
21018+ struct aopen_node aopen_node = {
21019+ .file = file
21020+ };
21021+
21022+ IMustLock(dir);
5afbbe0d 21023+ AuDbg("open_flag 0%o\n", open_flag);
b912730e
AM
21024+ AuDbgDentry(dentry);
21025+
21026+ err = 0;
21027+ if (!au_di(dentry)) {
5afbbe0d
AM
21028+ lkup_flags = LOOKUP_OPEN;
21029+ if (open_flag & O_CREAT)
21030+ lkup_flags |= LOOKUP_CREATE;
21031+ d = aufs_lookup(dir, dentry, lkup_flags);
b912730e
AM
21032+ if (IS_ERR(d)) {
21033+ err = PTR_ERR(d);
5afbbe0d 21034+ AuTraceErr(err);
b912730e
AM
21035+ goto out;
21036+ } else if (d) {
21037+ /*
21038+ * obsoleted dentry found.
21039+ * another error will be returned later.
21040+ */
21041+ d_drop(d);
b912730e 21042+ AuDbgDentry(d);
5afbbe0d 21043+ dput(d);
b912730e
AM
21044+ }
21045+ AuDbgDentry(dentry);
21046+ }
21047+
21048+ if (d_is_positive(dentry)
21049+ || d_unhashed(dentry)
21050+ || d_unlinked(dentry)
acd2b654
AM
21051+ || !(open_flag & O_CREAT)) {
21052+ err = au_aopen_no_open(file, dentry);
21053+ goto out; /* success */
21054+ }
b912730e
AM
21055+
21056+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
21057+ if (unlikely(err))
21058+ goto out;
21059+
acd2b654 21060+ sb = dentry->d_sb;
b912730e
AM
21061+ parent = dentry->d_parent; /* dir is locked */
21062+ di_write_lock_parent(parent);
5afbbe0d 21063+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
acd2b654
AM
21064+ if (unlikely(err < 0))
21065+ goto out_parent;
b912730e
AM
21066+
21067+ AuDbgDentry(dentry);
acd2b654
AM
21068+ if (d_is_positive(dentry)) {
21069+ err = au_aopen_no_open(file, dentry);
21070+ goto out_parent; /* success */
21071+ }
b912730e 21072+
acd2b654 21073+ args.file = alloc_empty_file(file->f_flags, current_cred());
b912730e
AM
21074+ err = PTR_ERR(args.file);
21075+ if (IS_ERR(args.file))
acd2b654 21076+ goto out_parent;
b912730e 21077+
acd2b654 21078+ bindex = au_dbtop(dentry);
b912730e
AM
21079+ err = au_aopen_or_create(dir, dentry, &args);
21080+ AuTraceErr(err);
21081+ AuDbgFile(args.file);
acd2b654
AM
21082+ file->f_mode = args.file->f_mode & ~FMODE_OPENED;
21083+ did_open = !!(args.file->f_mode & FMODE_OPENED);
21084+ if (!did_open) {
21085+ fput(args.file);
21086+ args.file = NULL;
b912730e 21087+ }
8b6a4947
AM
21088+ di_write_unlock(parent);
21089+ di_write_unlock(dentry);
acd2b654
AM
21090+ if (unlikely(err < 0)) {
21091+ if (args.file)
21092+ fput(args.file);
21093+ goto out_sb;
b912730e 21094+ }
b912730e 21095+
acd2b654
AM
21096+ if (!did_open)
21097+ err = au_aopen_no_open(file, dentry);
8b6a4947 21098+ else {
acd2b654
AM
21099+ aopen_node.h_file = args.file;
21100+ err = au_aopen_do_open(file, dentry, &aopen_node);
8b6a4947 21101+ }
acd2b654
AM
21102+ if (unlikely(err < 0)) {
21103+ if (args.file)
21104+ fput(args.file);
21105+ if (did_open)
21106+ au_lcnt_dec(&args.br->br_nfiles);
b912730e 21107+ }
acd2b654
AM
21108+ goto out_sb; /* success */
21109+
21110+out_parent:
21111+ di_write_unlock(parent);
21112+ di_write_unlock(dentry);
21113+out_sb:
21114+ si_read_unlock(sb);
b912730e 21115+out:
b912730e 21116+ AuTraceErr(err);
acd2b654 21117+ AuDbgFile(file);
b912730e
AM
21118+ return err;
21119+}
21120+
21121+
21122+/* ---------------------------------------------------------------------- */
21123+
4a4d8108
AM
21124+static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
21125+ const unsigned char add_entry, aufs_bindex_t bcpup,
5afbbe0d 21126+ aufs_bindex_t btop)
4a4d8108
AM
21127+{
21128+ int err;
21129+ struct dentry *h_parent;
21130+ struct inode *h_dir;
1facf9fc 21131+
027c5e7a 21132+ if (add_entry)
5527c038 21133+ IMustLock(d_inode(parent));
027c5e7a 21134+ else
4a4d8108
AM
21135+ di_write_lock_parent(parent);
21136+
21137+ err = 0;
21138+ if (!au_h_dptr(parent, bcpup)) {
5afbbe0d 21139+ if (btop > bcpup)
c2b27bf2 21140+ err = au_cpup_dirs(dentry, bcpup);
5afbbe0d 21141+ else if (btop < bcpup)
4a4d8108
AM
21142+ err = au_cpdown_dirs(dentry, bcpup);
21143+ else
c2b27bf2 21144+ BUG();
4a4d8108 21145+ }
38d290e6 21146+ if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
4a4d8108 21147+ h_parent = au_h_dptr(parent, bcpup);
5527c038 21148+ h_dir = d_inode(h_parent);
be118d29 21149+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
7e9cd9fe 21150+ err = au_lkup_neg(dentry, bcpup, /*wh*/0);
4a4d8108 21151+ /* todo: no unlock here */
3c1bdaff 21152+ inode_unlock_shared(h_dir);
027c5e7a
AM
21153+
21154+ AuDbg("bcpup %d\n", bcpup);
21155+ if (!err) {
5527c038 21156+ if (d_really_is_negative(dentry))
5afbbe0d 21157+ au_set_h_dptr(dentry, btop, NULL);
4a4d8108
AM
21158+ au_update_dbrange(dentry, /*do_put_zero*/0);
21159+ }
1308ab2a 21160+ }
1facf9fc 21161+
4a4d8108
AM
21162+ if (!add_entry)
21163+ di_write_unlock(parent);
21164+ if (!err)
21165+ err = bcpup; /* success */
1308ab2a 21166+
027c5e7a 21167+ AuTraceErr(err);
4a4d8108
AM
21168+ return err;
21169+}
1facf9fc 21170+
4a4d8108
AM
21171+/*
21172+ * decide the branch and the parent dir where we will create a new entry.
21173+ * returns new bindex or an error.
21174+ * copyup the parent dir if needed.
21175+ */
21176+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
21177+ struct au_wr_dir_args *args)
21178+{
21179+ int err;
392086de 21180+ unsigned int flags;
5afbbe0d 21181+ aufs_bindex_t bcpup, btop, src_btop;
86dc4139
AM
21182+ const unsigned char add_entry
21183+ = au_ftest_wrdir(args->flags, ADD_ENTRY)
38d290e6 21184+ | au_ftest_wrdir(args->flags, TMPFILE);
4a4d8108
AM
21185+ struct super_block *sb;
21186+ struct dentry *parent;
21187+ struct au_sbinfo *sbinfo;
1facf9fc 21188+
4a4d8108
AM
21189+ sb = dentry->d_sb;
21190+ sbinfo = au_sbi(sb);
21191+ parent = dget_parent(dentry);
5afbbe0d
AM
21192+ btop = au_dbtop(dentry);
21193+ bcpup = btop;
4a4d8108
AM
21194+ if (args->force_btgt < 0) {
21195+ if (src_dentry) {
5afbbe0d
AM
21196+ src_btop = au_dbtop(src_dentry);
21197+ if (src_btop < btop)
21198+ bcpup = src_btop;
4a4d8108 21199+ } else if (add_entry) {
392086de
AM
21200+ flags = 0;
21201+ if (au_ftest_wrdir(args->flags, ISDIR))
21202+ au_fset_wbr(flags, DIR);
21203+ err = AuWbrCreate(sbinfo, dentry, flags);
4a4d8108
AM
21204+ bcpup = err;
21205+ }
1facf9fc 21206+
5527c038 21207+ if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
4a4d8108
AM
21208+ if (add_entry)
21209+ err = AuWbrCopyup(sbinfo, dentry);
21210+ else {
21211+ if (!IS_ROOT(dentry)) {
21212+ di_read_lock_parent(parent, !AuLock_IR);
21213+ err = AuWbrCopyup(sbinfo, dentry);
21214+ di_read_unlock(parent, !AuLock_IR);
21215+ } else
21216+ err = AuWbrCopyup(sbinfo, dentry);
21217+ }
21218+ bcpup = err;
21219+ if (unlikely(err < 0))
21220+ goto out;
21221+ }
21222+ } else {
21223+ bcpup = args->force_btgt;
5527c038 21224+ AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
1308ab2a 21225+ }
027c5e7a 21226+
5afbbe0d 21227+ AuDbg("btop %d, bcpup %d\n", btop, bcpup);
4a4d8108 21228+ err = bcpup;
5afbbe0d 21229+ if (bcpup == btop)
4a4d8108 21230+ goto out; /* success */
4a4d8108
AM
21231+
21232+ /* copyup the new parent into the branch we process */
5afbbe0d 21233+ err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
027c5e7a 21234+ if (err >= 0) {
5527c038 21235+ if (d_really_is_negative(dentry)) {
5afbbe0d
AM
21236+ au_set_h_dptr(dentry, btop, NULL);
21237+ au_set_dbtop(dentry, bcpup);
21238+ au_set_dbbot(dentry, bcpup);
027c5e7a 21239+ }
38d290e6
JR
21240+ AuDebugOn(add_entry
21241+ && !au_ftest_wrdir(args->flags, TMPFILE)
21242+ && !au_h_dptr(dentry, bcpup));
027c5e7a 21243+ }
86dc4139
AM
21244+
21245+out:
21246+ dput(parent);
21247+ return err;
21248+}
21249+
21250+/* ---------------------------------------------------------------------- */
21251+
21252+void au_pin_hdir_unlock(struct au_pin *p)
21253+{
21254+ if (p->hdir)
5afbbe0d 21255+ au_hn_inode_unlock(p->hdir);
86dc4139
AM
21256+}
21257+
c1595e42 21258+int au_pin_hdir_lock(struct au_pin *p)
86dc4139
AM
21259+{
21260+ int err;
21261+
21262+ err = 0;
21263+ if (!p->hdir)
21264+ goto out;
21265+
21266+ /* even if an error happens later, keep this lock */
5afbbe0d 21267+ au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
86dc4139
AM
21268+
21269+ err = -EBUSY;
5527c038 21270+ if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
86dc4139
AM
21271+ goto out;
21272+
21273+ err = 0;
21274+ if (p->h_dentry)
21275+ err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
21276+ p->h_parent, p->br);
21277+
21278+out:
21279+ return err;
21280+}
21281+
21282+int au_pin_hdir_relock(struct au_pin *p)
21283+{
21284+ int err, i;
21285+ struct inode *h_i;
21286+ struct dentry *h_d[] = {
21287+ p->h_dentry,
21288+ p->h_parent
21289+ };
21290+
21291+ err = au_pin_hdir_lock(p);
21292+ if (unlikely(err))
21293+ goto out;
21294+
21295+ for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
21296+ if (!h_d[i])
21297+ continue;
5527c038
JR
21298+ if (d_is_positive(h_d[i])) {
21299+ h_i = d_inode(h_d[i]);
86dc4139 21300+ err = !h_i->i_nlink;
5527c038 21301+ }
86dc4139
AM
21302+ }
21303+
21304+out:
21305+ return err;
21306+}
21307+
5afbbe0d 21308+static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
86dc4139 21309+{
5afbbe0d
AM
21310+#if !defined(CONFIG_RWSEM_GENERIC_SPINLOCK) && defined(CONFIG_RWSEM_SPIN_ON_OWNER)
21311+ p->hdir->hi_inode->i_rwsem.owner = task;
86dc4139
AM
21312+#endif
21313+}
21314+
21315+void au_pin_hdir_acquire_nest(struct au_pin *p)
21316+{
21317+ if (p->hdir) {
5afbbe0d 21318+ rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
86dc4139
AM
21319+ p->lsc_hi, 0, NULL, _RET_IP_);
21320+ au_pin_hdir_set_owner(p, current);
21321+ }
dece6358 21322+}
1facf9fc 21323+
86dc4139
AM
21324+void au_pin_hdir_release(struct au_pin *p)
21325+{
21326+ if (p->hdir) {
21327+ au_pin_hdir_set_owner(p, p->task);
5afbbe0d 21328+ rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, 1, _RET_IP_);
86dc4139
AM
21329+ }
21330+}
1308ab2a 21331+
4a4d8108 21332+struct dentry *au_pinned_h_parent(struct au_pin *pin)
1308ab2a 21333+{
4a4d8108
AM
21334+ if (pin && pin->parent)
21335+ return au_h_dptr(pin->parent, pin->bindex);
21336+ return NULL;
dece6358 21337+}
1facf9fc 21338+
4a4d8108 21339+void au_unpin(struct au_pin *p)
dece6358 21340+{
86dc4139
AM
21341+ if (p->hdir)
21342+ au_pin_hdir_unlock(p);
e49829fe 21343+ if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
b4510431 21344+ vfsub_mnt_drop_write(p->h_mnt);
4a4d8108
AM
21345+ if (!p->hdir)
21346+ return;
1facf9fc 21347+
4a4d8108
AM
21348+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21349+ di_read_unlock(p->parent, AuLock_IR);
21350+ iput(p->hdir->hi_inode);
21351+ dput(p->parent);
21352+ p->parent = NULL;
21353+ p->hdir = NULL;
21354+ p->h_mnt = NULL;
86dc4139 21355+ /* do not clear p->task */
4a4d8108 21356+}
1308ab2a 21357+
4a4d8108
AM
21358+int au_do_pin(struct au_pin *p)
21359+{
21360+ int err;
21361+ struct super_block *sb;
4a4d8108
AM
21362+ struct inode *h_dir;
21363+
21364+ err = 0;
21365+ sb = p->dentry->d_sb;
86dc4139 21366+ p->br = au_sbr(sb, p->bindex);
4a4d8108
AM
21367+ if (IS_ROOT(p->dentry)) {
21368+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 21369+ p->h_mnt = au_br_mnt(p->br);
b4510431 21370+ err = vfsub_mnt_want_write(p->h_mnt);
4a4d8108
AM
21371+ if (unlikely(err)) {
21372+ au_fclr_pin(p->flags, MNT_WRITE);
21373+ goto out_err;
21374+ }
21375+ }
dece6358 21376+ goto out;
1facf9fc 21377+ }
21378+
86dc4139 21379+ p->h_dentry = NULL;
5afbbe0d 21380+ if (p->bindex <= au_dbbot(p->dentry))
86dc4139 21381+ p->h_dentry = au_h_dptr(p->dentry, p->bindex);
dece6358 21382+
4a4d8108
AM
21383+ p->parent = dget_parent(p->dentry);
21384+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21385+ di_read_lock(p->parent, AuLock_IR, p->lsc_di);
dece6358 21386+
4a4d8108 21387+ h_dir = NULL;
86dc4139 21388+ p->h_parent = au_h_dptr(p->parent, p->bindex);
5527c038 21389+ p->hdir = au_hi(d_inode(p->parent), p->bindex);
4a4d8108
AM
21390+ if (p->hdir)
21391+ h_dir = p->hdir->hi_inode;
dece6358 21392+
b752ccd1
AM
21393+ /*
21394+ * udba case, or
21395+ * if DI_LOCKED is not set, then p->parent may be different
21396+ * and h_parent can be NULL.
21397+ */
86dc4139 21398+ if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
e49829fe 21399+ err = -EBUSY;
4a4d8108
AM
21400+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21401+ di_read_unlock(p->parent, AuLock_IR);
21402+ dput(p->parent);
21403+ p->parent = NULL;
21404+ goto out_err;
21405+ }
1308ab2a 21406+
4a4d8108 21407+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 21408+ p->h_mnt = au_br_mnt(p->br);
b4510431 21409+ err = vfsub_mnt_want_write(p->h_mnt);
dece6358 21410+ if (unlikely(err)) {
4a4d8108 21411+ au_fclr_pin(p->flags, MNT_WRITE);
86dc4139
AM
21412+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21413+ di_read_unlock(p->parent, AuLock_IR);
21414+ dput(p->parent);
21415+ p->parent = NULL;
21416+ goto out_err;
dece6358
AM
21417+ }
21418+ }
4a4d8108 21419+
86dc4139
AM
21420+ au_igrab(h_dir);
21421+ err = au_pin_hdir_lock(p);
21422+ if (!err)
21423+ goto out; /* success */
21424+
076b876e
AM
21425+ au_unpin(p);
21426+
4f0767ce 21427+out_err:
4a4d8108
AM
21428+ pr_err("err %d\n", err);
21429+ err = au_busy_or_stale();
4f0767ce 21430+out:
1facf9fc 21431+ return err;
21432+}
21433+
4a4d8108
AM
21434+void au_pin_init(struct au_pin *p, struct dentry *dentry,
21435+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
21436+ unsigned int udba, unsigned char flags)
21437+{
21438+ p->dentry = dentry;
21439+ p->udba = udba;
21440+ p->lsc_di = lsc_di;
21441+ p->lsc_hi = lsc_hi;
21442+ p->flags = flags;
21443+ p->bindex = bindex;
21444+
21445+ p->parent = NULL;
21446+ p->hdir = NULL;
21447+ p->h_mnt = NULL;
86dc4139
AM
21448+
21449+ p->h_dentry = NULL;
21450+ p->h_parent = NULL;
21451+ p->br = NULL;
21452+ p->task = current;
4a4d8108
AM
21453+}
21454+
21455+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
21456+ unsigned int udba, unsigned char flags)
21457+{
21458+ au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
21459+ udba, flags);
21460+ return au_do_pin(pin);
21461+}
21462+
dece6358
AM
21463+/* ---------------------------------------------------------------------- */
21464+
1308ab2a 21465+/*
4a4d8108
AM
21466+ * ->setattr() and ->getattr() are called in various cases.
21467+ * chmod, stat: dentry is revalidated.
21468+ * fchmod, fstat: file and dentry are not revalidated, additionally they may be
21469+ * unhashed.
21470+ * for ->setattr(), ia->ia_file is passed from ftruncate only.
1308ab2a 21471+ */
027c5e7a 21472+/* todo: consolidate with do_refresh() and simple_reval_dpath() */
c1595e42 21473+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
1facf9fc 21474+{
4a4d8108 21475+ int err;
4a4d8108 21476+ struct dentry *parent;
1facf9fc 21477+
1308ab2a 21478+ err = 0;
027c5e7a 21479+ if (au_digen_test(dentry, sigen)) {
4a4d8108
AM
21480+ parent = dget_parent(dentry);
21481+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 21482+ err = au_refresh_dentry(dentry, parent);
4a4d8108
AM
21483+ di_read_unlock(parent, AuLock_IR);
21484+ dput(parent);
dece6358 21485+ }
1facf9fc 21486+
4a4d8108 21487+ AuTraceErr(err);
1308ab2a 21488+ return err;
21489+}
dece6358 21490+
c1595e42
JR
21491+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
21492+ struct au_icpup_args *a)
1308ab2a 21493+{
21494+ int err;
4a4d8108 21495+ loff_t sz;
5afbbe0d 21496+ aufs_bindex_t btop, ibtop;
4a4d8108
AM
21497+ struct dentry *hi_wh, *parent;
21498+ struct inode *inode;
4a4d8108
AM
21499+ struct au_wr_dir_args wr_dir_args = {
21500+ .force_btgt = -1,
21501+ .flags = 0
21502+ };
21503+
2000de60 21504+ if (d_is_dir(dentry))
4a4d8108
AM
21505+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
21506+ /* plink or hi_wh() case */
5afbbe0d 21507+ btop = au_dbtop(dentry);
5527c038 21508+ inode = d_inode(dentry);
5afbbe0d
AM
21509+ ibtop = au_ibtop(inode);
21510+ if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
21511+ wr_dir_args.force_btgt = ibtop;
4a4d8108
AM
21512+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21513+ if (unlikely(err < 0))
21514+ goto out;
21515+ a->btgt = err;
5afbbe0d 21516+ if (err != btop)
4a4d8108
AM
21517+ au_fset_icpup(a->flags, DID_CPUP);
21518+
21519+ err = 0;
21520+ a->pin_flags = AuPin_MNT_WRITE;
21521+ parent = NULL;
21522+ if (!IS_ROOT(dentry)) {
21523+ au_fset_pin(a->pin_flags, DI_LOCKED);
21524+ parent = dget_parent(dentry);
21525+ di_write_lock_parent(parent);
21526+ }
21527+
21528+ err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
21529+ if (unlikely(err))
21530+ goto out_parent;
21531+
4a4d8108 21532+ sz = -1;
5afbbe0d 21533+ a->h_path.dentry = au_h_dptr(dentry, btop);
5527c038 21534+ a->h_inode = d_inode(a->h_path.dentry);
c1595e42 21535+ if (ia && (ia->ia_valid & ATTR_SIZE)) {
be118d29 21536+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
c1595e42
JR
21537+ if (ia->ia_size < i_size_read(a->h_inode))
21538+ sz = ia->ia_size;
3c1bdaff 21539+ inode_unlock_shared(a->h_inode);
c1595e42 21540+ }
4a4d8108 21541+
4a4d8108 21542+ hi_wh = NULL;
027c5e7a 21543+ if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
4a4d8108
AM
21544+ hi_wh = au_hi_wh(inode, a->btgt);
21545+ if (!hi_wh) {
c2b27bf2
AM
21546+ struct au_cp_generic cpg = {
21547+ .dentry = dentry,
21548+ .bdst = a->btgt,
21549+ .bsrc = -1,
21550+ .len = sz,
21551+ .pin = &a->pin
21552+ };
21553+ err = au_sio_cpup_wh(&cpg, /*file*/NULL);
4a4d8108
AM
21554+ if (unlikely(err))
21555+ goto out_unlock;
21556+ hi_wh = au_hi_wh(inode, a->btgt);
21557+ /* todo: revalidate hi_wh? */
21558+ }
21559+ }
21560+
21561+ if (parent) {
21562+ au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
21563+ di_downgrade_lock(parent, AuLock_IR);
21564+ dput(parent);
21565+ parent = NULL;
21566+ }
21567+ if (!au_ftest_icpup(a->flags, DID_CPUP))
21568+ goto out; /* success */
21569+
21570+ if (!d_unhashed(dentry)) {
c2b27bf2
AM
21571+ struct au_cp_generic cpg = {
21572+ .dentry = dentry,
21573+ .bdst = a->btgt,
5afbbe0d 21574+ .bsrc = btop,
c2b27bf2
AM
21575+ .len = sz,
21576+ .pin = &a->pin,
21577+ .flags = AuCpup_DTIME | AuCpup_HOPEN
21578+ };
21579+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
21580+ if (!err)
21581+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21582+ } else if (!hi_wh)
21583+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21584+ else
21585+ a->h_path.dentry = hi_wh; /* do not dget here */
1308ab2a 21586+
4f0767ce 21587+out_unlock:
5527c038 21588+ a->h_inode = d_inode(a->h_path.dentry);
86dc4139 21589+ if (!err)
dece6358 21590+ goto out; /* success */
4a4d8108 21591+ au_unpin(&a->pin);
4f0767ce 21592+out_parent:
4a4d8108
AM
21593+ if (parent) {
21594+ di_write_unlock(parent);
21595+ dput(parent);
21596+ }
4f0767ce 21597+out:
86dc4139 21598+ if (!err)
febd17d6 21599+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
1facf9fc 21600+ return err;
21601+}
21602+
4a4d8108 21603+static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
1facf9fc 21604+{
4a4d8108 21605+ int err;
523b37e3 21606+ struct inode *inode, *delegated;
4a4d8108
AM
21607+ struct super_block *sb;
21608+ struct file *file;
21609+ struct au_icpup_args *a;
1facf9fc 21610+
5527c038 21611+ inode = d_inode(dentry);
4a4d8108 21612+ IMustLock(inode);
dece6358 21613+
f2c43d5f
AM
21614+ err = setattr_prepare(dentry, ia);
21615+ if (unlikely(err))
21616+ goto out;
21617+
4a4d8108
AM
21618+ err = -ENOMEM;
21619+ a = kzalloc(sizeof(*a), GFP_NOFS);
21620+ if (unlikely(!a))
21621+ goto out;
1facf9fc 21622+
4a4d8108
AM
21623+ if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
21624+ ia->ia_valid &= ~ATTR_MODE;
dece6358 21625+
4a4d8108
AM
21626+ file = NULL;
21627+ sb = dentry->d_sb;
e49829fe
JR
21628+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21629+ if (unlikely(err))
21630+ goto out_kfree;
21631+
4a4d8108
AM
21632+ if (ia->ia_valid & ATTR_FILE) {
21633+ /* currently ftruncate(2) only */
7e9cd9fe 21634+ AuDebugOn(!d_is_reg(dentry));
4a4d8108 21635+ file = ia->ia_file;
521ced18
JR
21636+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
21637+ /*fi_lsc*/0);
4a4d8108
AM
21638+ if (unlikely(err))
21639+ goto out_si;
21640+ ia->ia_file = au_hf_top(file);
21641+ a->udba = AuOpt_UDBA_NONE;
21642+ } else {
21643+ /* fchmod() doesn't pass ia_file */
21644+ a->udba = au_opt_udba(sb);
027c5e7a
AM
21645+ di_write_lock_child(dentry);
21646+ /* no d_unlinked(), to set UDBA_NONE for root */
4a4d8108
AM
21647+ if (d_unhashed(dentry))
21648+ a->udba = AuOpt_UDBA_NONE;
4a4d8108
AM
21649+ if (a->udba != AuOpt_UDBA_NONE) {
21650+ AuDebugOn(IS_ROOT(dentry));
21651+ err = au_reval_for_attr(dentry, au_sigen(sb));
21652+ if (unlikely(err))
21653+ goto out_dentry;
21654+ }
dece6358 21655+ }
dece6358 21656+
4a4d8108
AM
21657+ err = au_pin_and_icpup(dentry, ia, a);
21658+ if (unlikely(err < 0))
21659+ goto out_dentry;
21660+ if (au_ftest_icpup(a->flags, DID_CPUP)) {
21661+ ia->ia_file = NULL;
21662+ ia->ia_valid &= ~ATTR_FILE;
1308ab2a 21663+ }
dece6358 21664+
4a4d8108
AM
21665+ a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
21666+ if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
21667+ == (ATTR_MODE | ATTR_CTIME)) {
7eafdf33 21668+ err = security_path_chmod(&a->h_path, ia->ia_mode);
4a4d8108
AM
21669+ if (unlikely(err))
21670+ goto out_unlock;
21671+ } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
21672+ && (ia->ia_valid & ATTR_CTIME)) {
86dc4139 21673+ err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
4a4d8108
AM
21674+ if (unlikely(err))
21675+ goto out_unlock;
21676+ }
dece6358 21677+
4a4d8108
AM
21678+ if (ia->ia_valid & ATTR_SIZE) {
21679+ struct file *f;
1308ab2a 21680+
953406b4 21681+ if (ia->ia_size < i_size_read(inode))
4a4d8108 21682+ /* unmap only */
953406b4 21683+ truncate_setsize(inode, ia->ia_size);
1308ab2a 21684+
4a4d8108
AM
21685+ f = NULL;
21686+ if (ia->ia_valid & ATTR_FILE)
21687+ f = ia->ia_file;
febd17d6 21688+ inode_unlock(a->h_inode);
4a4d8108 21689+ err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
febd17d6 21690+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
523b37e3
AM
21691+ } else {
21692+ delegated = NULL;
21693+ while (1) {
21694+ err = vfsub_notify_change(&a->h_path, ia, &delegated);
21695+ if (delegated) {
21696+ err = break_deleg_wait(&delegated);
21697+ if (!err)
21698+ continue;
21699+ }
21700+ break;
21701+ }
21702+ }
8cdd5066
JR
21703+ /*
21704+ * regardless aufs 'acl' option setting.
21705+ * why don't all acl-aware fs call this func from their ->setattr()?
21706+ */
21707+ if (!err && (ia->ia_valid & ATTR_MODE))
21708+ err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
4a4d8108
AM
21709+ if (!err)
21710+ au_cpup_attr_changeable(inode);
1308ab2a 21711+
4f0767ce 21712+out_unlock:
febd17d6 21713+ inode_unlock(a->h_inode);
4a4d8108 21714+ au_unpin(&a->pin);
027c5e7a 21715+ if (unlikely(err))
5afbbe0d 21716+ au_update_dbtop(dentry);
4f0767ce 21717+out_dentry:
4a4d8108
AM
21718+ di_write_unlock(dentry);
21719+ if (file) {
21720+ fi_write_unlock(file);
21721+ ia->ia_file = file;
21722+ ia->ia_valid |= ATTR_FILE;
21723+ }
4f0767ce 21724+out_si:
4a4d8108 21725+ si_read_unlock(sb);
e49829fe 21726+out_kfree:
9f237c51 21727+ au_kfree_rcu(a);
4f0767ce 21728+out:
4a4d8108
AM
21729+ AuTraceErr(err);
21730+ return err;
1facf9fc 21731+}
21732+
c1595e42
JR
21733+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
21734+static int au_h_path_to_set_attr(struct dentry *dentry,
21735+ struct au_icpup_args *a, struct path *h_path)
21736+{
21737+ int err;
21738+ struct super_block *sb;
21739+
21740+ sb = dentry->d_sb;
21741+ a->udba = au_opt_udba(sb);
21742+ /* no d_unlinked(), to set UDBA_NONE for root */
21743+ if (d_unhashed(dentry))
21744+ a->udba = AuOpt_UDBA_NONE;
21745+ if (a->udba != AuOpt_UDBA_NONE) {
21746+ AuDebugOn(IS_ROOT(dentry));
21747+ err = au_reval_for_attr(dentry, au_sigen(sb));
21748+ if (unlikely(err))
21749+ goto out;
21750+ }
21751+ err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
21752+ if (unlikely(err < 0))
21753+ goto out;
21754+
21755+ h_path->dentry = a->h_path.dentry;
21756+ h_path->mnt = au_sbr_mnt(sb, a->btgt);
21757+
21758+out:
21759+ return err;
21760+}
21761+
f2c43d5f
AM
21762+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
21763+ struct au_sxattr *arg)
c1595e42
JR
21764+{
21765+ int err;
21766+ struct path h_path;
21767+ struct super_block *sb;
21768+ struct au_icpup_args *a;
5afbbe0d 21769+ struct inode *h_inode;
c1595e42 21770+
c1595e42
JR
21771+ IMustLock(inode);
21772+
21773+ err = -ENOMEM;
21774+ a = kzalloc(sizeof(*a), GFP_NOFS);
21775+ if (unlikely(!a))
21776+ goto out;
21777+
21778+ sb = dentry->d_sb;
21779+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21780+ if (unlikely(err))
21781+ goto out_kfree;
21782+
21783+ h_path.dentry = NULL; /* silence gcc */
21784+ di_write_lock_child(dentry);
21785+ err = au_h_path_to_set_attr(dentry, a, &h_path);
21786+ if (unlikely(err))
21787+ goto out_di;
21788+
febd17d6 21789+ inode_unlock(a->h_inode);
c1595e42
JR
21790+ switch (arg->type) {
21791+ case AU_XATTR_SET:
5afbbe0d 21792+ AuDebugOn(d_is_negative(h_path.dentry));
c1595e42
JR
21793+ err = vfsub_setxattr(h_path.dentry,
21794+ arg->u.set.name, arg->u.set.value,
21795+ arg->u.set.size, arg->u.set.flags);
21796+ break;
c1595e42
JR
21797+ case AU_ACL_SET:
21798+ err = -EOPNOTSUPP;
5527c038 21799+ h_inode = d_inode(h_path.dentry);
c1595e42 21800+ if (h_inode->i_op->set_acl)
f2c43d5f 21801+ /* this will call posix_acl_update_mode */
c1595e42
JR
21802+ err = h_inode->i_op->set_acl(h_inode,
21803+ arg->u.acl_set.acl,
21804+ arg->u.acl_set.type);
21805+ break;
21806+ }
21807+ if (!err)
21808+ au_cpup_attr_timesizes(inode);
21809+
21810+ au_unpin(&a->pin);
21811+ if (unlikely(err))
5afbbe0d 21812+ au_update_dbtop(dentry);
c1595e42
JR
21813+
21814+out_di:
21815+ di_write_unlock(dentry);
21816+ si_read_unlock(sb);
21817+out_kfree:
9f237c51 21818+ au_kfree_rcu(a);
c1595e42
JR
21819+out:
21820+ AuTraceErr(err);
21821+ return err;
21822+}
21823+#endif
21824+
4a4d8108
AM
21825+static void au_refresh_iattr(struct inode *inode, struct kstat *st,
21826+ unsigned int nlink)
1facf9fc 21827+{
9dbd164d
AM
21828+ unsigned int n;
21829+
4a4d8108 21830+ inode->i_mode = st->mode;
86dc4139
AM
21831+ /* don't i_[ug]id_write() here */
21832+ inode->i_uid = st->uid;
21833+ inode->i_gid = st->gid;
4a4d8108
AM
21834+ inode->i_atime = st->atime;
21835+ inode->i_mtime = st->mtime;
21836+ inode->i_ctime = st->ctime;
1facf9fc 21837+
4a4d8108
AM
21838+ au_cpup_attr_nlink(inode, /*force*/0);
21839+ if (S_ISDIR(inode->i_mode)) {
9dbd164d
AM
21840+ n = inode->i_nlink;
21841+ n -= nlink;
21842+ n += st->nlink;
f6b6e03d 21843+ smp_mb(); /* for i_nlink */
7eafdf33 21844+ /* 0 can happen */
92d182d2 21845+ set_nlink(inode, n);
4a4d8108 21846+ }
1facf9fc 21847+
4a4d8108
AM
21848+ spin_lock(&inode->i_lock);
21849+ inode->i_blocks = st->blocks;
21850+ i_size_write(inode, st->size);
21851+ spin_unlock(&inode->i_lock);
1facf9fc 21852+}
21853+
c1595e42 21854+/*
f2c43d5f 21855+ * common routine for aufs_getattr() and au_getxattr().
c1595e42
JR
21856+ * returns zero or negative (an error).
21857+ * @dentry will be read-locked in success.
21858+ */
a2654f78
AM
21859+int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
21860+ int locked)
1facf9fc 21861+{
4a4d8108 21862+ int err;
076b876e 21863+ unsigned int mnt_flags, sigen;
c1595e42 21864+ unsigned char udba_none;
4a4d8108 21865+ aufs_bindex_t bindex;
4a4d8108
AM
21866+ struct super_block *sb, *h_sb;
21867+ struct inode *inode;
1facf9fc 21868+
c1595e42
JR
21869+ h_path->mnt = NULL;
21870+ h_path->dentry = NULL;
21871+
21872+ err = 0;
4a4d8108 21873+ sb = dentry->d_sb;
4a4d8108
AM
21874+ mnt_flags = au_mntflags(sb);
21875+ udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
1facf9fc 21876+
a2654f78
AM
21877+ if (unlikely(locked))
21878+ goto body; /* skip locking dinfo */
21879+
4a4d8108 21880+ /* support fstat(2) */
027c5e7a 21881+ if (!d_unlinked(dentry) && !udba_none) {
076b876e 21882+ sigen = au_sigen(sb);
027c5e7a
AM
21883+ err = au_digen_test(dentry, sigen);
21884+ if (!err) {
4a4d8108 21885+ di_read_lock_child(dentry, AuLock_IR);
027c5e7a 21886+ err = au_dbrange_test(dentry);
c1595e42
JR
21887+ if (unlikely(err)) {
21888+ di_read_unlock(dentry, AuLock_IR);
21889+ goto out;
21890+ }
027c5e7a 21891+ } else {
4a4d8108
AM
21892+ AuDebugOn(IS_ROOT(dentry));
21893+ di_write_lock_child(dentry);
027c5e7a
AM
21894+ err = au_dbrange_test(dentry);
21895+ if (!err)
21896+ err = au_reval_for_attr(dentry, sigen);
c1595e42
JR
21897+ if (!err)
21898+ di_downgrade_lock(dentry, AuLock_IR);
21899+ else {
21900+ di_write_unlock(dentry);
21901+ goto out;
21902+ }
4a4d8108
AM
21903+ }
21904+ } else
21905+ di_read_lock_child(dentry, AuLock_IR);
1facf9fc 21906+
a2654f78 21907+body:
5527c038 21908+ inode = d_inode(dentry);
5afbbe0d 21909+ bindex = au_ibtop(inode);
c1595e42
JR
21910+ h_path->mnt = au_sbr_mnt(sb, bindex);
21911+ h_sb = h_path->mnt->mnt_sb;
21912+ if (!force
21913+ && !au_test_fs_bad_iattr(h_sb)
21914+ && udba_none)
21915+ goto out; /* success */
1facf9fc 21916+
5afbbe0d 21917+ if (au_dbtop(dentry) == bindex)
c1595e42 21918+ h_path->dentry = au_h_dptr(dentry, bindex);
4a4d8108 21919+ else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
c1595e42
JR
21920+ h_path->dentry = au_plink_lkup(inode, bindex);
21921+ if (IS_ERR(h_path->dentry))
21922+ /* pretending success */
21923+ h_path->dentry = NULL;
21924+ else
21925+ dput(h_path->dentry);
4a4d8108 21926+ }
c1595e42
JR
21927+
21928+out:
21929+ return err;
21930+}
21931+
521ced18
JR
21932+static int aufs_getattr(const struct path *path, struct kstat *st,
21933+ u32 request, unsigned int query)
c1595e42
JR
21934+{
21935+ int err;
21936+ unsigned char positive;
21937+ struct path h_path;
521ced18 21938+ struct dentry *dentry;
c1595e42
JR
21939+ struct inode *inode;
21940+ struct super_block *sb;
21941+
521ced18 21942+ dentry = path->dentry;
5527c038 21943+ inode = d_inode(dentry);
c1595e42
JR
21944+ sb = dentry->d_sb;
21945+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21946+ if (unlikely(err))
21947+ goto out;
a2654f78 21948+ err = au_h_path_getattr(dentry, /*force*/0, &h_path, /*locked*/0);
c1595e42
JR
21949+ if (unlikely(err))
21950+ goto out_si;
c06a8ce3 21951+ if (unlikely(!h_path.dentry))
c1595e42 21952+ /* illegally overlapped or something */
4a4d8108
AM
21953+ goto out_fill; /* pretending success */
21954+
5527c038 21955+ positive = d_is_positive(h_path.dentry);
4a4d8108 21956+ if (positive)
521ced18
JR
21957+ /* no vfsub version */
21958+ err = vfs_getattr(&h_path, st, request, query);
4a4d8108
AM
21959+ if (!err) {
21960+ if (positive)
c06a8ce3 21961+ au_refresh_iattr(inode, st,
5527c038 21962+ d_inode(h_path.dentry)->i_nlink);
4a4d8108 21963+ goto out_fill; /* success */
1facf9fc 21964+ }
7f207e10 21965+ AuTraceErr(err);
c1595e42 21966+ goto out_di;
4a4d8108 21967+
4f0767ce 21968+out_fill:
4a4d8108 21969+ generic_fillattr(inode, st);
c1595e42 21970+out_di:
4a4d8108 21971+ di_read_unlock(dentry, AuLock_IR);
c1595e42 21972+out_si:
4a4d8108 21973+ si_read_unlock(sb);
7f207e10
AM
21974+out:
21975+ AuTraceErr(err);
4a4d8108 21976+ return err;
1facf9fc 21977+}
21978+
21979+/* ---------------------------------------------------------------------- */
21980+
febd17d6
JR
21981+static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
21982+ struct delayed_call *done)
4a4d8108 21983+{
c2c0f25c 21984+ const char *ret;
c2c0f25c 21985+ struct dentry *h_dentry;
febd17d6 21986+ struct inode *h_inode;
4a4d8108 21987+ int err;
c2c0f25c 21988+ aufs_bindex_t bindex;
1facf9fc 21989+
79b8bda9 21990+ ret = NULL; /* suppress a warning */
febd17d6
JR
21991+ err = -ECHILD;
21992+ if (!dentry)
21993+ goto out;
21994+
027c5e7a
AM
21995+ err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
21996+ if (unlikely(err))
c2c0f25c 21997+ goto out;
027c5e7a
AM
21998+
21999+ err = au_d_hashed_positive(dentry);
c2c0f25c
AM
22000+ if (unlikely(err))
22001+ goto out_unlock;
22002+
22003+ err = -EINVAL;
22004+ inode = d_inode(dentry);
5afbbe0d 22005+ bindex = au_ibtop(inode);
c2c0f25c 22006+ h_inode = au_h_iptr(inode, bindex);
febd17d6 22007+ if (unlikely(!h_inode->i_op->get_link))
c2c0f25c
AM
22008+ goto out_unlock;
22009+
22010+ err = -EBUSY;
22011+ h_dentry = NULL;
5afbbe0d 22012+ if (au_dbtop(dentry) <= bindex) {
c2c0f25c
AM
22013+ h_dentry = au_h_dptr(dentry, bindex);
22014+ if (h_dentry)
22015+ dget(h_dentry);
027c5e7a 22016+ }
c2c0f25c
AM
22017+ if (!h_dentry) {
22018+ h_dentry = d_find_any_alias(h_inode);
22019+ if (IS_ERR(h_dentry)) {
22020+ err = PTR_ERR(h_dentry);
febd17d6 22021+ goto out_unlock;
c2c0f25c
AM
22022+ }
22023+ }
22024+ if (unlikely(!h_dentry))
febd17d6 22025+ goto out_unlock;
1facf9fc 22026+
c2c0f25c 22027+ err = 0;
062440b3 22028+ AuDbg("%ps\n", h_inode->i_op->get_link);
c2c0f25c 22029+ AuDbgDentry(h_dentry);
f2c43d5f 22030+ ret = vfs_get_link(h_dentry, done);
c2c0f25c 22031+ dput(h_dentry);
febd17d6
JR
22032+ if (IS_ERR(ret))
22033+ err = PTR_ERR(ret);
c2c0f25c 22034+
c2c0f25c
AM
22035+out_unlock:
22036+ aufs_read_unlock(dentry, AuLock_IR);
4f0767ce 22037+out:
c2c0f25c
AM
22038+ if (unlikely(err))
22039+ ret = ERR_PTR(err);
22040+ AuTraceErrPtr(ret);
22041+ return ret;
4a4d8108 22042+}
1facf9fc 22043+
4a4d8108 22044+/* ---------------------------------------------------------------------- */
1facf9fc 22045+
e2f27e51
AM
22046+static int au_is_special(struct inode *inode)
22047+{
22048+ return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
22049+}
22050+
acd2b654
AM
22051+static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
22052+ int flags)
4a4d8108 22053+{
0c3ec466 22054+ int err;
e2f27e51 22055+ aufs_bindex_t bindex;
0c3ec466
AM
22056+ struct super_block *sb;
22057+ struct inode *h_inode;
e2f27e51 22058+ struct vfsmount *h_mnt;
0c3ec466
AM
22059+
22060+ sb = inode->i_sb;
e2f27e51
AM
22061+ WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
22062+ "unexpected s_flags 0x%lx", sb->s_flags);
22063+
0c3ec466
AM
22064+ /* mmap_sem might be acquired already, cf. aufs_mmap() */
22065+ lockdep_off();
22066+ si_read_lock(sb, AuLock_FLUSH);
22067+ ii_write_lock_child(inode);
e2f27e51
AM
22068+
22069+ err = 0;
22070+ bindex = au_ibtop(inode);
22071+ h_inode = au_h_iptr(inode, bindex);
22072+ if (!au_test_ro(sb, bindex, inode)) {
22073+ h_mnt = au_sbr_mnt(sb, bindex);
22074+ err = vfsub_mnt_want_write(h_mnt);
22075+ if (!err) {
22076+ err = vfsub_update_time(h_inode, ts, flags);
22077+ vfsub_mnt_drop_write(h_mnt);
22078+ }
22079+ } else if (au_is_special(h_inode)) {
22080+ /*
22081+ * Never copy-up here.
22082+ * These special files may already be opened and used for
22083+ * communicating. If we copied it up, then the communication
22084+ * would be corrupted.
22085+ */
22086+ AuWarn1("timestamps for i%lu are ignored "
22087+ "since it is on readonly branch (hi%lu).\n",
22088+ inode->i_ino, h_inode->i_ino);
22089+ } else if (flags & ~S_ATIME) {
22090+ err = -EIO;
22091+ AuIOErr1("unexpected flags 0x%x\n", flags);
22092+ AuDebugOn(1);
22093+ }
22094+
38d290e6
JR
22095+ if (!err)
22096+ au_cpup_attr_timesizes(inode);
0c3ec466
AM
22097+ ii_write_unlock(inode);
22098+ si_read_unlock(sb);
22099+ lockdep_on();
38d290e6
JR
22100+
22101+ if (!err && (flags & S_VERSION))
22102+ inode_inc_iversion(inode);
22103+
0c3ec466 22104+ return err;
4a4d8108 22105+}
1facf9fc 22106+
4a4d8108 22107+/* ---------------------------------------------------------------------- */
1308ab2a 22108+
b95c5147
AM
22109+/* no getattr version will be set by module.c:aufs_init() */
22110+struct inode_operations aufs_iop_nogetattr[AuIop_Last],
22111+ aufs_iop[] = {
22112+ [AuIop_SYMLINK] = {
22113+ .permission = aufs_permission,
c1595e42 22114+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
22115+ .get_acl = aufs_get_acl,
22116+ .set_acl = aufs_set_acl, /* unsupport for symlink? */
c1595e42
JR
22117+#endif
22118+
b95c5147
AM
22119+ .setattr = aufs_setattr,
22120+ .getattr = aufs_getattr,
0c3ec466 22121+
c1595e42 22122+#ifdef CONFIG_AUFS_XATTR
b95c5147 22123+ .listxattr = aufs_listxattr,
c1595e42
JR
22124+#endif
22125+
febd17d6 22126+ .get_link = aufs_get_link,
0c3ec466 22127+
b95c5147
AM
22128+ /* .update_time = aufs_update_time */
22129+ },
22130+ [AuIop_DIR] = {
22131+ .create = aufs_create,
22132+ .lookup = aufs_lookup,
22133+ .link = aufs_link,
22134+ .unlink = aufs_unlink,
22135+ .symlink = aufs_symlink,
22136+ .mkdir = aufs_mkdir,
22137+ .rmdir = aufs_rmdir,
22138+ .mknod = aufs_mknod,
22139+ .rename = aufs_rename,
22140+
22141+ .permission = aufs_permission,
c1595e42 22142+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
22143+ .get_acl = aufs_get_acl,
22144+ .set_acl = aufs_set_acl,
c1595e42
JR
22145+#endif
22146+
b95c5147
AM
22147+ .setattr = aufs_setattr,
22148+ .getattr = aufs_getattr,
0c3ec466 22149+
c1595e42 22150+#ifdef CONFIG_AUFS_XATTR
b95c5147 22151+ .listxattr = aufs_listxattr,
c1595e42
JR
22152+#endif
22153+
b95c5147
AM
22154+ .update_time = aufs_update_time,
22155+ .atomic_open = aufs_atomic_open,
22156+ .tmpfile = aufs_tmpfile
22157+ },
22158+ [AuIop_OTHER] = {
22159+ .permission = aufs_permission,
c1595e42 22160+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
22161+ .get_acl = aufs_get_acl,
22162+ .set_acl = aufs_set_acl,
c1595e42
JR
22163+#endif
22164+
b95c5147
AM
22165+ .setattr = aufs_setattr,
22166+ .getattr = aufs_getattr,
0c3ec466 22167+
c1595e42 22168+#ifdef CONFIG_AUFS_XATTR
b95c5147 22169+ .listxattr = aufs_listxattr,
c1595e42
JR
22170+#endif
22171+
b95c5147
AM
22172+ .update_time = aufs_update_time
22173+ }
4a4d8108 22174+};
7f207e10
AM
22175diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
22176--- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
9f237c51 22177+++ linux/fs/aufs/i_op_del.c 2018-12-27 13:19:17.711749485 +0100
062440b3 22178@@ -0,0 +1,512 @@
cd7a4cd9 22179+// SPDX-License-Identifier: GPL-2.0
1facf9fc 22180+/*
b00004a5 22181+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 22182+ *
22183+ * This program, aufs is free software; you can redistribute it and/or modify
22184+ * it under the terms of the GNU General Public License as published by
22185+ * the Free Software Foundation; either version 2 of the License, or
22186+ * (at your option) any later version.
dece6358
AM
22187+ *
22188+ * This program is distributed in the hope that it will be useful,
22189+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22190+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22191+ * GNU General Public License for more details.
22192+ *
22193+ * You should have received a copy of the GNU General Public License
523b37e3 22194+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 22195+ */
22196+
22197+/*
4a4d8108 22198+ * inode operations (del entry)
1308ab2a 22199+ */
dece6358 22200+
1308ab2a 22201+#include "aufs.h"
dece6358 22202+
4a4d8108
AM
22203+/*
22204+ * decide if a new whiteout for @dentry is necessary or not.
22205+ * when it is necessary, prepare the parent dir for the upper branch whose
22206+ * branch index is @bcpup for creation. the actual creation of the whiteout will
22207+ * be done by caller.
22208+ * return value:
22209+ * 0: wh is unnecessary
22210+ * plus: wh is necessary
22211+ * minus: error
22212+ */
22213+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
1308ab2a 22214+{
4a4d8108 22215+ int need_wh, err;
5afbbe0d 22216+ aufs_bindex_t btop;
4a4d8108 22217+ struct super_block *sb;
dece6358 22218+
4a4d8108 22219+ sb = dentry->d_sb;
5afbbe0d 22220+ btop = au_dbtop(dentry);
4a4d8108 22221+ if (*bcpup < 0) {
5afbbe0d
AM
22222+ *bcpup = btop;
22223+ if (au_test_ro(sb, btop, d_inode(dentry))) {
4a4d8108
AM
22224+ err = AuWbrCopyup(au_sbi(sb), dentry);
22225+ *bcpup = err;
22226+ if (unlikely(err < 0))
22227+ goto out;
22228+ }
22229+ } else
5afbbe0d 22230+ AuDebugOn(btop < *bcpup
5527c038 22231+ || au_test_ro(sb, *bcpup, d_inode(dentry)));
5afbbe0d 22232+ AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
1308ab2a 22233+
5afbbe0d 22234+ if (*bcpup != btop) {
4a4d8108
AM
22235+ err = au_cpup_dirs(dentry, *bcpup);
22236+ if (unlikely(err))
22237+ goto out;
22238+ need_wh = 1;
22239+ } else {
027c5e7a 22240+ struct au_dinfo *dinfo, *tmp;
4a4d8108 22241+
027c5e7a
AM
22242+ need_wh = -ENOMEM;
22243+ dinfo = au_di(dentry);
22244+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
22245+ if (tmp) {
22246+ au_di_cp(tmp, dinfo);
22247+ au_di_swap(tmp, dinfo);
22248+ /* returns the number of positive dentries */
5afbbe0d
AM
22249+ need_wh = au_lkup_dentry(dentry, btop + 1,
22250+ /* AuLkup_IGNORE_PERM */ 0);
027c5e7a
AM
22251+ au_di_swap(tmp, dinfo);
22252+ au_rw_write_unlock(&tmp->di_rwsem);
22253+ au_di_free(tmp);
4a4d8108
AM
22254+ }
22255+ }
22256+ AuDbg("need_wh %d\n", need_wh);
22257+ err = need_wh;
22258+
4f0767ce 22259+out:
4a4d8108 22260+ return err;
1facf9fc 22261+}
22262+
4a4d8108
AM
22263+/*
22264+ * simple tests for the del-entry operations.
22265+ * following the checks in vfs, plus the parent-child relationship.
22266+ */
22267+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
22268+ struct dentry *h_parent, int isdir)
1facf9fc 22269+{
4a4d8108
AM
22270+ int err;
22271+ umode_t h_mode;
22272+ struct dentry *h_dentry, *h_latest;
1308ab2a 22273+ struct inode *h_inode;
1facf9fc 22274+
4a4d8108 22275+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 22276+ if (d_really_is_positive(dentry)) {
4a4d8108 22277+ err = -ENOENT;
5527c038
JR
22278+ if (unlikely(d_is_negative(h_dentry)))
22279+ goto out;
22280+ h_inode = d_inode(h_dentry);
22281+ if (unlikely(!h_inode->i_nlink))
4a4d8108 22282+ goto out;
1facf9fc 22283+
4a4d8108
AM
22284+ h_mode = h_inode->i_mode;
22285+ if (!isdir) {
22286+ err = -EISDIR;
22287+ if (unlikely(S_ISDIR(h_mode)))
22288+ goto out;
22289+ } else if (unlikely(!S_ISDIR(h_mode))) {
22290+ err = -ENOTDIR;
22291+ goto out;
22292+ }
22293+ } else {
22294+ /* rename(2) case */
22295+ err = -EIO;
5527c038 22296+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
22297+ goto out;
22298+ }
1facf9fc 22299+
4a4d8108
AM
22300+ err = -ENOENT;
22301+ /* expected parent dir is locked */
22302+ if (unlikely(h_parent != h_dentry->d_parent))
22303+ goto out;
22304+ err = 0;
22305+
22306+ /*
22307+ * rmdir a dir may break the consistency on some filesystem.
22308+ * let's try heavy test.
22309+ */
22310+ err = -EACCES;
076b876e 22311+ if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
5527c038 22312+ && au_test_h_perm(d_inode(h_parent),
076b876e 22313+ MAY_EXEC | MAY_WRITE)))
4a4d8108
AM
22314+ goto out;
22315+
076b876e 22316+ h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
4a4d8108
AM
22317+ err = -EIO;
22318+ if (IS_ERR(h_latest))
22319+ goto out;
22320+ if (h_latest == h_dentry)
22321+ err = 0;
22322+ dput(h_latest);
22323+
4f0767ce 22324+out:
4a4d8108 22325+ return err;
1308ab2a 22326+}
1facf9fc 22327+
4a4d8108
AM
22328+/*
22329+ * decide the branch where we operate for @dentry. the branch index will be set
acd2b654 22330+ * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
4a4d8108
AM
22331+ * dir for reverting.
22332+ * when a new whiteout is necessary, create it.
22333+ */
22334+static struct dentry*
22335+lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
22336+ struct au_dtime *dt, struct au_pin *pin)
1308ab2a 22337+{
4a4d8108
AM
22338+ struct dentry *wh_dentry;
22339+ struct super_block *sb;
22340+ struct path h_path;
22341+ int err, need_wh;
22342+ unsigned int udba;
22343+ aufs_bindex_t bcpup;
dece6358 22344+
4a4d8108
AM
22345+ need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
22346+ wh_dentry = ERR_PTR(need_wh);
22347+ if (unlikely(need_wh < 0))
22348+ goto out;
22349+
22350+ sb = dentry->d_sb;
22351+ udba = au_opt_udba(sb);
22352+ bcpup = *rbcpup;
22353+ err = au_pin(pin, dentry, bcpup, udba,
22354+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22355+ wh_dentry = ERR_PTR(err);
22356+ if (unlikely(err))
22357+ goto out;
22358+
22359+ h_path.dentry = au_pinned_h_parent(pin);
22360+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 22361+ && au_dbtop(dentry) == bcpup) {
4a4d8108
AM
22362+ err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
22363+ wh_dentry = ERR_PTR(err);
22364+ if (unlikely(err))
22365+ goto out_unpin;
22366+ }
22367+
22368+ h_path.mnt = au_sbr_mnt(sb, bcpup);
22369+ au_dtime_store(dt, au_pinned_parent(pin), &h_path);
22370+ wh_dentry = NULL;
22371+ if (!need_wh)
22372+ goto out; /* success, no need to create whiteout */
22373+
22374+ wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
22375+ if (IS_ERR(wh_dentry))
22376+ goto out_unpin;
22377+
22378+ /* returns with the parent is locked and wh_dentry is dget-ed */
22379+ goto out; /* success */
22380+
4f0767ce 22381+out_unpin:
4a4d8108 22382+ au_unpin(pin);
4f0767ce 22383+out:
4a4d8108 22384+ return wh_dentry;
1facf9fc 22385+}
22386+
4a4d8108
AM
22387+/*
22388+ * when removing a dir, rename it to a unique temporary whiteout-ed name first
22389+ * in order to be revertible and save time for removing many child whiteouts
22390+ * under the dir.
22391+ * returns 1 when there are too many child whiteout and caller should remove
22392+ * them asynchronously. returns 0 when the number of children is enough small to
22393+ * remove now or the branch fs is a remote fs.
22394+ * otherwise return an error.
22395+ */
22396+static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
22397+ struct au_nhash *whlist, struct inode *dir)
1facf9fc 22398+{
4a4d8108
AM
22399+ int rmdir_later, err, dirwh;
22400+ struct dentry *h_dentry;
22401+ struct super_block *sb;
5527c038 22402+ struct inode *inode;
4a4d8108
AM
22403+
22404+ sb = dentry->d_sb;
22405+ SiMustAnyLock(sb);
22406+ h_dentry = au_h_dptr(dentry, bindex);
22407+ err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
22408+ if (unlikely(err))
22409+ goto out;
22410+
22411+ /* stop monitoring */
5527c038
JR
22412+ inode = d_inode(dentry);
22413+ au_hn_free(au_hi(inode, bindex));
4a4d8108
AM
22414+
22415+ if (!au_test_fs_remote(h_dentry->d_sb)) {
22416+ dirwh = au_sbi(sb)->si_dirwh;
22417+ rmdir_later = (dirwh <= 1);
22418+ if (!rmdir_later)
22419+ rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
22420+ dirwh);
22421+ if (rmdir_later)
22422+ return rmdir_later;
22423+ }
1facf9fc 22424+
4a4d8108
AM
22425+ err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
22426+ if (unlikely(err)) {
523b37e3
AM
22427+ AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
22428+ h_dentry, bindex, err);
4a4d8108
AM
22429+ err = 0;
22430+ }
dece6358 22431+
4f0767ce 22432+out:
4a4d8108
AM
22433+ AuTraceErr(err);
22434+ return err;
22435+}
1308ab2a 22436+
4a4d8108
AM
22437+/*
22438+ * final procedure for deleting a entry.
22439+ * maintain dentry and iattr.
22440+ */
22441+static void epilog(struct inode *dir, struct dentry *dentry,
22442+ aufs_bindex_t bindex)
22443+{
22444+ struct inode *inode;
1308ab2a 22445+
5527c038 22446+ inode = d_inode(dentry);
4a4d8108
AM
22447+ d_drop(dentry);
22448+ inode->i_ctime = dir->i_ctime;
1308ab2a 22449+
b912730e 22450+ au_dir_ts(dir, bindex);
be118d29 22451+ inode_inc_iversion(dir);
1facf9fc 22452+}
22453+
4a4d8108
AM
22454+/*
22455+ * when an error happened, remove the created whiteout and revert everything.
22456+ */
7f207e10
AM
22457+static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
22458+ aufs_bindex_t bwh, struct dentry *wh_dentry,
22459+ struct dentry *dentry, struct au_dtime *dt)
1facf9fc 22460+{
4a4d8108
AM
22461+ int rerr;
22462+ struct path h_path = {
22463+ .dentry = wh_dentry,
7f207e10 22464+ .mnt = au_sbr_mnt(dir->i_sb, bindex)
4a4d8108 22465+ };
dece6358 22466+
7f207e10 22467+ rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
4a4d8108
AM
22468+ if (!rerr) {
22469+ au_set_dbwh(dentry, bwh);
22470+ au_dtime_revert(dt);
22471+ return 0;
22472+ }
dece6358 22473+
523b37e3 22474+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
4a4d8108 22475+ return -EIO;
1facf9fc 22476+}
22477+
4a4d8108 22478+/* ---------------------------------------------------------------------- */
1facf9fc 22479+
4a4d8108 22480+int aufs_unlink(struct inode *dir, struct dentry *dentry)
1308ab2a 22481+{
4a4d8108 22482+ int err;
5afbbe0d 22483+ aufs_bindex_t bwh, bindex, btop;
523b37e3 22484+ struct inode *inode, *h_dir, *delegated;
4a4d8108 22485+ struct dentry *parent, *wh_dentry;
acd2b654 22486+ /* to reduce stack size */
c2b27bf2
AM
22487+ struct {
22488+ struct au_dtime dt;
22489+ struct au_pin pin;
22490+ struct path h_path;
22491+ } *a;
1facf9fc 22492+
4a4d8108 22493+ IMustLock(dir);
027c5e7a 22494+
c2b27bf2
AM
22495+ err = -ENOMEM;
22496+ a = kmalloc(sizeof(*a), GFP_NOFS);
22497+ if (unlikely(!a))
22498+ goto out;
22499+
027c5e7a
AM
22500+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
22501+ if (unlikely(err))
c2b27bf2 22502+ goto out_free;
027c5e7a
AM
22503+ err = au_d_hashed_positive(dentry);
22504+ if (unlikely(err))
22505+ goto out_unlock;
5527c038 22506+ inode = d_inode(dentry);
4a4d8108 22507+ IMustLock(inode);
027c5e7a 22508+ err = -EISDIR;
2000de60 22509+ if (unlikely(d_is_dir(dentry)))
027c5e7a 22510+ goto out_unlock; /* possible? */
1facf9fc 22511+
5afbbe0d 22512+ btop = au_dbtop(dentry);
4a4d8108
AM
22513+ bwh = au_dbwh(dentry);
22514+ bindex = -1;
027c5e7a
AM
22515+ parent = dentry->d_parent; /* dir inode is locked */
22516+ di_write_lock_parent(parent);
c2b27bf2
AM
22517+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
22518+ &a->pin);
4a4d8108
AM
22519+ err = PTR_ERR(wh_dentry);
22520+ if (IS_ERR(wh_dentry))
027c5e7a 22521+ goto out_parent;
1facf9fc 22522+
5afbbe0d
AM
22523+ a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
22524+ a->h_path.dentry = au_h_dptr(dentry, btop);
c2b27bf2 22525+ dget(a->h_path.dentry);
5afbbe0d 22526+ if (bindex == btop) {
c2b27bf2 22527+ h_dir = au_pinned_h_dir(&a->pin);
523b37e3
AM
22528+ delegated = NULL;
22529+ err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
22530+ if (unlikely(err == -EWOULDBLOCK)) {
22531+ pr_warn("cannot retry for NFSv4 delegation"
22532+ " for an internal unlink\n");
22533+ iput(delegated);
22534+ }
4a4d8108
AM
22535+ } else {
22536+ /* dir inode is locked */
5527c038 22537+ h_dir = d_inode(wh_dentry->d_parent);
4a4d8108
AM
22538+ IMustLock(h_dir);
22539+ err = 0;
22540+ }
dece6358 22541+
4a4d8108 22542+ if (!err) {
7f207e10 22543+ vfsub_drop_nlink(inode);
4a4d8108
AM
22544+ epilog(dir, dentry, bindex);
22545+
22546+ /* update target timestamps */
5afbbe0d 22547+ if (bindex == btop) {
c2b27bf2
AM
22548+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
22549+ /*ignore*/
5527c038 22550+ inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
4a4d8108
AM
22551+ } else
22552+ /* todo: this timestamp may be reverted later */
22553+ inode->i_ctime = h_dir->i_ctime;
027c5e7a 22554+ goto out_unpin; /* success */
1facf9fc 22555+ }
22556+
4a4d8108
AM
22557+ /* revert */
22558+ if (wh_dentry) {
22559+ int rerr;
22560+
c2b27bf2
AM
22561+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22562+ &a->dt);
4a4d8108
AM
22563+ if (rerr)
22564+ err = rerr;
dece6358 22565+ }
1facf9fc 22566+
027c5e7a 22567+out_unpin:
c2b27bf2 22568+ au_unpin(&a->pin);
4a4d8108 22569+ dput(wh_dentry);
c2b27bf2 22570+ dput(a->h_path.dentry);
027c5e7a 22571+out_parent:
4a4d8108 22572+ di_write_unlock(parent);
027c5e7a 22573+out_unlock:
4a4d8108 22574+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 22575+out_free:
9f237c51 22576+ au_kfree_rcu(a);
027c5e7a 22577+out:
4a4d8108 22578+ return err;
dece6358
AM
22579+}
22580+
4a4d8108 22581+int aufs_rmdir(struct inode *dir, struct dentry *dentry)
1308ab2a 22582+{
4a4d8108 22583+ int err, rmdir_later;
5afbbe0d 22584+ aufs_bindex_t bwh, bindex, btop;
4a4d8108
AM
22585+ struct inode *inode;
22586+ struct dentry *parent, *wh_dentry, *h_dentry;
22587+ struct au_whtmp_rmdir *args;
acd2b654 22588+ /* to reduce stack size */
c2b27bf2
AM
22589+ struct {
22590+ struct au_dtime dt;
22591+ struct au_pin pin;
22592+ } *a;
1facf9fc 22593+
4a4d8108 22594+ IMustLock(dir);
027c5e7a 22595+
c2b27bf2
AM
22596+ err = -ENOMEM;
22597+ a = kmalloc(sizeof(*a), GFP_NOFS);
22598+ if (unlikely(!a))
22599+ goto out;
22600+
027c5e7a
AM
22601+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22602+ if (unlikely(err))
c2b27bf2 22603+ goto out_free;
53392da6
AM
22604+ err = au_alive_dir(dentry);
22605+ if (unlikely(err))
027c5e7a 22606+ goto out_unlock;
5527c038 22607+ inode = d_inode(dentry);
4a4d8108 22608+ IMustLock(inode);
027c5e7a 22609+ err = -ENOTDIR;
2000de60 22610+ if (unlikely(!d_is_dir(dentry)))
027c5e7a 22611+ goto out_unlock; /* possible? */
dece6358 22612+
4a4d8108
AM
22613+ err = -ENOMEM;
22614+ args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
22615+ if (unlikely(!args))
22616+ goto out_unlock;
dece6358 22617+
4a4d8108
AM
22618+ parent = dentry->d_parent; /* dir inode is locked */
22619+ di_write_lock_parent(parent);
22620+ err = au_test_empty(dentry, &args->whlist);
22621+ if (unlikely(err))
027c5e7a 22622+ goto out_parent;
1facf9fc 22623+
5afbbe0d 22624+ btop = au_dbtop(dentry);
4a4d8108
AM
22625+ bwh = au_dbwh(dentry);
22626+ bindex = -1;
c2b27bf2
AM
22627+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
22628+ &a->pin);
4a4d8108
AM
22629+ err = PTR_ERR(wh_dentry);
22630+ if (IS_ERR(wh_dentry))
027c5e7a 22631+ goto out_parent;
1facf9fc 22632+
5afbbe0d 22633+ h_dentry = au_h_dptr(dentry, btop);
4a4d8108
AM
22634+ dget(h_dentry);
22635+ rmdir_later = 0;
5afbbe0d
AM
22636+ if (bindex == btop) {
22637+ err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
4a4d8108
AM
22638+ if (err > 0) {
22639+ rmdir_later = err;
22640+ err = 0;
22641+ }
22642+ } else {
22643+ /* stop monitoring */
5afbbe0d 22644+ au_hn_free(au_hi(inode, btop));
4a4d8108
AM
22645+
22646+ /* dir inode is locked */
5527c038 22647+ IMustLock(d_inode(wh_dentry->d_parent));
1facf9fc 22648+ err = 0;
22649+ }
22650+
4a4d8108 22651+ if (!err) {
027c5e7a 22652+ vfsub_dead_dir(inode);
4a4d8108
AM
22653+ au_set_dbdiropq(dentry, -1);
22654+ epilog(dir, dentry, bindex);
1308ab2a 22655+
4a4d8108 22656+ if (rmdir_later) {
5afbbe0d 22657+ au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
4a4d8108
AM
22658+ args = NULL;
22659+ }
1308ab2a 22660+
4a4d8108 22661+ goto out_unpin; /* success */
1facf9fc 22662+ }
22663+
4a4d8108
AM
22664+ /* revert */
22665+ AuLabel(revert);
22666+ if (wh_dentry) {
22667+ int rerr;
1308ab2a 22668+
c2b27bf2
AM
22669+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22670+ &a->dt);
4a4d8108
AM
22671+ if (rerr)
22672+ err = rerr;
1facf9fc 22673+ }
22674+
4f0767ce 22675+out_unpin:
c2b27bf2 22676+ au_unpin(&a->pin);
4a4d8108
AM
22677+ dput(wh_dentry);
22678+ dput(h_dentry);
027c5e7a 22679+out_parent:
4a4d8108
AM
22680+ di_write_unlock(parent);
22681+ if (args)
22682+ au_whtmp_rmdir_free(args);
4f0767ce 22683+out_unlock:
4a4d8108 22684+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 22685+out_free:
9f237c51 22686+ au_kfree_rcu(a);
4f0767ce 22687+out:
4a4d8108
AM
22688+ AuTraceErr(err);
22689+ return err;
dece6358 22690+}
7f207e10
AM
22691diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
22692--- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
9f237c51 22693+++ linux/fs/aufs/i_op_ren.c 2018-12-27 13:19:17.711749485 +0100
acd2b654 22694@@ -0,0 +1,1249 @@
cd7a4cd9 22695+// SPDX-License-Identifier: GPL-2.0
1facf9fc 22696+/*
b00004a5 22697+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 22698+ *
22699+ * This program, aufs is free software; you can redistribute it and/or modify
22700+ * it under the terms of the GNU General Public License as published by
22701+ * the Free Software Foundation; either version 2 of the License, or
22702+ * (at your option) any later version.
dece6358
AM
22703+ *
22704+ * This program is distributed in the hope that it will be useful,
22705+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22706+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22707+ * GNU General Public License for more details.
22708+ *
22709+ * You should have received a copy of the GNU General Public License
523b37e3 22710+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 22711+ */
22712+
22713+/*
4a4d8108
AM
22714+ * inode operation (rename entry)
22715+ * todo: this is crazy monster
1facf9fc 22716+ */
22717+
22718+#include "aufs.h"
22719+
4a4d8108
AM
22720+enum { AuSRC, AuDST, AuSrcDst };
22721+enum { AuPARENT, AuCHILD, AuParentChild };
1facf9fc 22722+
f2c43d5f
AM
22723+#define AuRen_ISDIR_SRC 1
22724+#define AuRen_ISDIR_DST (1 << 1)
22725+#define AuRen_ISSAMEDIR (1 << 2)
22726+#define AuRen_WHSRC (1 << 3)
22727+#define AuRen_WHDST (1 << 4)
22728+#define AuRen_MNT_WRITE (1 << 5)
22729+#define AuRen_DT_DSTDIR (1 << 6)
22730+#define AuRen_DIROPQ_SRC (1 << 7)
22731+#define AuRen_DIROPQ_DST (1 << 8)
8b6a4947
AM
22732+#define AuRen_DIRREN (1 << 9)
22733+#define AuRen_DROPPED_SRC (1 << 10)
22734+#define AuRen_DROPPED_DST (1 << 11)
4a4d8108 22735+#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
7f207e10
AM
22736+#define au_fset_ren(flags, name) \
22737+ do { (flags) |= AuRen_##name; } while (0)
22738+#define au_fclr_ren(flags, name) \
22739+ do { (flags) &= ~AuRen_##name; } while (0)
1facf9fc 22740+
8b6a4947
AM
22741+#ifndef CONFIG_AUFS_DIRREN
22742+#undef AuRen_DIRREN
22743+#define AuRen_DIRREN 0
22744+#endif
22745+
4a4d8108
AM
22746+struct au_ren_args {
22747+ struct {
22748+ struct dentry *dentry, *h_dentry, *parent, *h_parent,
22749+ *wh_dentry;
22750+ struct inode *dir, *inode;
f2c43d5f 22751+ struct au_hinode *hdir, *hinode;
4a4d8108 22752+ struct au_dtime dt[AuParentChild];
f2c43d5f 22753+ aufs_bindex_t btop, bdiropq;
4a4d8108 22754+ } sd[AuSrcDst];
1facf9fc 22755+
4a4d8108
AM
22756+#define src_dentry sd[AuSRC].dentry
22757+#define src_dir sd[AuSRC].dir
22758+#define src_inode sd[AuSRC].inode
22759+#define src_h_dentry sd[AuSRC].h_dentry
22760+#define src_parent sd[AuSRC].parent
22761+#define src_h_parent sd[AuSRC].h_parent
22762+#define src_wh_dentry sd[AuSRC].wh_dentry
22763+#define src_hdir sd[AuSRC].hdir
f2c43d5f 22764+#define src_hinode sd[AuSRC].hinode
4a4d8108
AM
22765+#define src_h_dir sd[AuSRC].hdir->hi_inode
22766+#define src_dt sd[AuSRC].dt
5afbbe0d 22767+#define src_btop sd[AuSRC].btop
f2c43d5f 22768+#define src_bdiropq sd[AuSRC].bdiropq
1facf9fc 22769+
4a4d8108
AM
22770+#define dst_dentry sd[AuDST].dentry
22771+#define dst_dir sd[AuDST].dir
22772+#define dst_inode sd[AuDST].inode
22773+#define dst_h_dentry sd[AuDST].h_dentry
22774+#define dst_parent sd[AuDST].parent
22775+#define dst_h_parent sd[AuDST].h_parent
22776+#define dst_wh_dentry sd[AuDST].wh_dentry
22777+#define dst_hdir sd[AuDST].hdir
f2c43d5f 22778+#define dst_hinode sd[AuDST].hinode
4a4d8108
AM
22779+#define dst_h_dir sd[AuDST].hdir->hi_inode
22780+#define dst_dt sd[AuDST].dt
5afbbe0d 22781+#define dst_btop sd[AuDST].btop
f2c43d5f 22782+#define dst_bdiropq sd[AuDST].bdiropq
4a4d8108
AM
22783+
22784+ struct dentry *h_trap;
22785+ struct au_branch *br;
4a4d8108
AM
22786+ struct path h_path;
22787+ struct au_nhash whlist;
f2c43d5f 22788+ aufs_bindex_t btgt, src_bwh;
1facf9fc 22789+
f2c43d5f
AM
22790+ struct {
22791+ unsigned short auren_flags;
22792+ unsigned char flags; /* syscall parameter */
22793+ unsigned char exchange;
22794+ } __packed;
1facf9fc 22795+
4a4d8108
AM
22796+ struct au_whtmp_rmdir *thargs;
22797+ struct dentry *h_dst;
8b6a4947 22798+ struct au_hinode *h_root;
4a4d8108 22799+};
1308ab2a 22800+
4a4d8108 22801+/* ---------------------------------------------------------------------- */
1308ab2a 22802+
4a4d8108
AM
22803+/*
22804+ * functions for reverting.
22805+ * when an error happened in a single rename systemcall, we should revert
79b8bda9 22806+ * everything as if nothing happened.
4a4d8108
AM
22807+ * we don't need to revert the copied-up/down the parent dir since they are
22808+ * harmless.
22809+ */
1facf9fc 22810+
4a4d8108
AM
22811+#define RevertFailure(fmt, ...) do { \
22812+ AuIOErr("revert failure: " fmt " (%d, %d)\n", \
22813+ ##__VA_ARGS__, err, rerr); \
22814+ err = -EIO; \
22815+} while (0)
1facf9fc 22816+
f2c43d5f 22817+static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
1facf9fc 22818+{
4a4d8108 22819+ int rerr;
f2c43d5f
AM
22820+ struct dentry *d;
22821+#define src_or_dst(member) a->sd[idx].member
1facf9fc 22822+
f2c43d5f
AM
22823+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22824+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22825+ rerr = au_diropq_remove(d, a->btgt);
22826+ au_hn_inode_unlock(src_or_dst(hinode));
22827+ au_set_dbdiropq(d, src_or_dst(bdiropq));
4a4d8108 22828+ if (rerr)
f2c43d5f
AM
22829+ RevertFailure("remove diropq %pd", d);
22830+
22831+#undef src_or_dst_
22832+}
22833+
22834+static void au_ren_rev_diropq(int err, struct au_ren_args *a)
22835+{
22836+ if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
22837+ au_ren_do_rev_diropq(err, a, AuSRC);
22838+ if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
22839+ au_ren_do_rev_diropq(err, a, AuDST);
4a4d8108 22840+}
1facf9fc 22841+
4a4d8108
AM
22842+static void au_ren_rev_rename(int err, struct au_ren_args *a)
22843+{
22844+ int rerr;
523b37e3 22845+ struct inode *delegated;
1facf9fc 22846+
b4510431
AM
22847+ a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
22848+ a->src_h_parent);
4a4d8108
AM
22849+ rerr = PTR_ERR(a->h_path.dentry);
22850+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 22851+ RevertFailure("lkup one %pd", a->src_dentry);
4a4d8108 22852+ return;
1facf9fc 22853+ }
22854+
523b37e3 22855+ delegated = NULL;
4a4d8108
AM
22856+ rerr = vfsub_rename(a->dst_h_dir,
22857+ au_h_dptr(a->src_dentry, a->btgt),
f2c43d5f 22858+ a->src_h_dir, &a->h_path, &delegated, a->flags);
523b37e3
AM
22859+ if (unlikely(rerr == -EWOULDBLOCK)) {
22860+ pr_warn("cannot retry for NFSv4 delegation"
22861+ " for an internal rename\n");
22862+ iput(delegated);
22863+ }
4a4d8108
AM
22864+ d_drop(a->h_path.dentry);
22865+ dput(a->h_path.dentry);
22866+ /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
22867+ if (rerr)
523b37e3 22868+ RevertFailure("rename %pd", a->src_dentry);
1facf9fc 22869+}
22870+
4a4d8108 22871+static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
1facf9fc 22872+{
4a4d8108 22873+ int rerr;
523b37e3 22874+ struct inode *delegated;
dece6358 22875+
b4510431
AM
22876+ a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
22877+ a->dst_h_parent);
4a4d8108
AM
22878+ rerr = PTR_ERR(a->h_path.dentry);
22879+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 22880+ RevertFailure("lkup one %pd", a->dst_dentry);
4a4d8108
AM
22881+ return;
22882+ }
5527c038 22883+ if (d_is_positive(a->h_path.dentry)) {
4a4d8108
AM
22884+ d_drop(a->h_path.dentry);
22885+ dput(a->h_path.dentry);
22886+ return;
dece6358
AM
22887+ }
22888+
523b37e3
AM
22889+ delegated = NULL;
22890+ rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
f2c43d5f 22891+ &delegated, a->flags);
523b37e3
AM
22892+ if (unlikely(rerr == -EWOULDBLOCK)) {
22893+ pr_warn("cannot retry for NFSv4 delegation"
22894+ " for an internal rename\n");
22895+ iput(delegated);
22896+ }
4a4d8108
AM
22897+ d_drop(a->h_path.dentry);
22898+ dput(a->h_path.dentry);
22899+ if (!rerr)
22900+ au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
22901+ else
523b37e3 22902+ RevertFailure("rename %pd", a->h_dst);
4a4d8108 22903+}
1308ab2a 22904+
4a4d8108
AM
22905+static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
22906+{
22907+ int rerr;
1308ab2a 22908+
4a4d8108
AM
22909+ a->h_path.dentry = a->src_wh_dentry;
22910+ rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
027c5e7a 22911+ au_set_dbwh(a->src_dentry, a->src_bwh);
4a4d8108 22912+ if (rerr)
523b37e3 22913+ RevertFailure("unlink %pd", a->src_wh_dentry);
4a4d8108 22914+}
4a4d8108 22915+#undef RevertFailure
1facf9fc 22916+
1308ab2a 22917+/* ---------------------------------------------------------------------- */
22918+
4a4d8108
AM
22919+/*
22920+ * when we have to copyup the renaming entry, do it with the rename-target name
22921+ * in order to minimize the cost (the later actual rename is unnecessary).
22922+ * otherwise rename it on the target branch.
22923+ */
22924+static int au_ren_or_cpup(struct au_ren_args *a)
1facf9fc 22925+{
dece6358 22926+ int err;
4a4d8108 22927+ struct dentry *d;
523b37e3 22928+ struct inode *delegated;
1facf9fc 22929+
4a4d8108 22930+ d = a->src_dentry;
5afbbe0d 22931+ if (au_dbtop(d) == a->btgt) {
4a4d8108 22932+ a->h_path.dentry = a->dst_h_dentry;
5afbbe0d 22933+ AuDebugOn(au_dbtop(d) != a->btgt);
523b37e3 22934+ delegated = NULL;
4a4d8108 22935+ err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
f2c43d5f
AM
22936+ a->dst_h_dir, &a->h_path, &delegated,
22937+ a->flags);
523b37e3
AM
22938+ if (unlikely(err == -EWOULDBLOCK)) {
22939+ pr_warn("cannot retry for NFSv4 delegation"
22940+ " for an internal rename\n");
22941+ iput(delegated);
22942+ }
c2b27bf2 22943+ } else
86dc4139 22944+ BUG();
1308ab2a 22945+
027c5e7a
AM
22946+ if (!err && a->h_dst)
22947+ /* it will be set to dinfo later */
22948+ dget(a->h_dst);
1facf9fc 22949+
dece6358
AM
22950+ return err;
22951+}
1facf9fc 22952+
4a4d8108
AM
22953+/* cf. aufs_rmdir() */
22954+static int au_ren_del_whtmp(struct au_ren_args *a)
dece6358 22955+{
4a4d8108
AM
22956+ int err;
22957+ struct inode *dir;
1facf9fc 22958+
4a4d8108
AM
22959+ dir = a->dst_dir;
22960+ SiMustAnyLock(dir->i_sb);
22961+ if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
22962+ au_sbi(dir->i_sb)->si_dirwh)
22963+ || au_test_fs_remote(a->h_dst->d_sb)) {
22964+ err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
22965+ if (unlikely(err))
523b37e3
AM
22966+ pr_warn("failed removing whtmp dir %pd (%d), "
22967+ "ignored.\n", a->h_dst, err);
4a4d8108
AM
22968+ } else {
22969+ au_nhash_wh_free(&a->thargs->whlist);
22970+ a->thargs->whlist = a->whlist;
22971+ a->whlist.nh_num = 0;
22972+ au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
22973+ dput(a->h_dst);
22974+ a->thargs = NULL;
22975+ }
22976+
22977+ return 0;
1308ab2a 22978+}
1facf9fc 22979+
4a4d8108 22980+/* make it 'opaque' dir. */
f2c43d5f 22981+static int au_ren_do_diropq(struct au_ren_args *a, int idx)
4a4d8108
AM
22982+{
22983+ int err;
f2c43d5f
AM
22984+ struct dentry *d, *diropq;
22985+#define src_or_dst(member) a->sd[idx].member
1facf9fc 22986+
4a4d8108 22987+ err = 0;
f2c43d5f
AM
22988+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22989+ src_or_dst(bdiropq) = au_dbdiropq(d);
22990+ src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
22991+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22992+ diropq = au_diropq_create(d, a->btgt);
22993+ au_hn_inode_unlock(src_or_dst(hinode));
4a4d8108
AM
22994+ if (IS_ERR(diropq))
22995+ err = PTR_ERR(diropq);
076b876e
AM
22996+ else
22997+ dput(diropq);
1facf9fc 22998+
f2c43d5f 22999+#undef src_or_dst_
4a4d8108
AM
23000+ return err;
23001+}
1facf9fc 23002+
f2c43d5f 23003+static int au_ren_diropq(struct au_ren_args *a)
4a4d8108
AM
23004+{
23005+ int err;
f2c43d5f
AM
23006+ unsigned char always;
23007+ struct dentry *d;
1facf9fc 23008+
f2c43d5f
AM
23009+ err = 0;
23010+ d = a->dst_dentry; /* already renamed on the branch */
23011+ always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
23012+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
8b6a4947 23013+ && !au_ftest_ren(a->auren_flags, DIRREN)
f2c43d5f
AM
23014+ && a->btgt != au_dbdiropq(a->src_dentry)
23015+ && (a->dst_wh_dentry
23016+ || a->btgt <= au_dbdiropq(d)
23017+ /* hide the lower to keep xino */
23018+ /* the lowers may not be a dir, but we hide them anyway */
23019+ || a->btgt < au_dbbot(d)
23020+ || always)) {
23021+ AuDbg("here\n");
23022+ err = au_ren_do_diropq(a, AuSRC);
23023+ if (unlikely(err))
4a4d8108 23024+ goto out;
f2c43d5f 23025+ au_fset_ren(a->auren_flags, DIROPQ_SRC);
4a4d8108 23026+ }
f2c43d5f
AM
23027+ if (!a->exchange)
23028+ goto out; /* success */
1facf9fc 23029+
f2c43d5f
AM
23030+ d = a->src_dentry; /* already renamed on the branch */
23031+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
23032+ && a->btgt != au_dbdiropq(a->dst_dentry)
23033+ && (a->btgt < au_dbdiropq(d)
23034+ || a->btgt < au_dbbot(d)
23035+ || always)) {
23036+ AuDbgDentry(a->src_dentry);
23037+ AuDbgDentry(a->dst_dentry);
23038+ err = au_ren_do_diropq(a, AuDST);
4a4d8108 23039+ if (unlikely(err))
f2c43d5f
AM
23040+ goto out_rev_src;
23041+ au_fset_ren(a->auren_flags, DIROPQ_DST);
23042+ }
23043+ goto out; /* success */
dece6358 23044+
f2c43d5f
AM
23045+out_rev_src:
23046+ AuDbg("err %d, reverting src\n", err);
23047+ au_ren_rev_diropq(err, a);
23048+out:
23049+ return err;
23050+}
23051+
23052+static int do_rename(struct au_ren_args *a)
23053+{
23054+ int err;
23055+ struct dentry *d, *h_d;
23056+
23057+ if (!a->exchange) {
23058+ /* prepare workqueue args for asynchronous rmdir */
23059+ h_d = a->dst_h_dentry;
23060+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
8b6a4947 23061+ /* && !au_ftest_ren(a->auren_flags, DIRREN) */
f2c43d5f
AM
23062+ && d_is_positive(h_d)) {
23063+ err = -ENOMEM;
23064+ a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
23065+ GFP_NOFS);
23066+ if (unlikely(!a->thargs))
23067+ goto out;
23068+ a->h_dst = dget(h_d);
23069+ }
23070+
23071+ /* create whiteout for src_dentry */
23072+ if (au_ftest_ren(a->auren_flags, WHSRC)) {
23073+ a->src_bwh = au_dbwh(a->src_dentry);
23074+ AuDebugOn(a->src_bwh >= 0);
23075+ a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
23076+ a->src_h_parent);
23077+ err = PTR_ERR(a->src_wh_dentry);
23078+ if (IS_ERR(a->src_wh_dentry))
23079+ goto out_thargs;
23080+ }
23081+
23082+ /* lookup whiteout for dentry */
23083+ if (au_ftest_ren(a->auren_flags, WHDST)) {
23084+ h_d = au_wh_lkup(a->dst_h_parent,
23085+ &a->dst_dentry->d_name, a->br);
23086+ err = PTR_ERR(h_d);
23087+ if (IS_ERR(h_d))
23088+ goto out_whsrc;
23089+ if (d_is_negative(h_d))
23090+ dput(h_d);
23091+ else
23092+ a->dst_wh_dentry = h_d;
23093+ }
23094+
23095+ /* rename dentry to tmpwh */
23096+ if (a->thargs) {
23097+ err = au_whtmp_ren(a->dst_h_dentry, a->br);
23098+ if (unlikely(err))
23099+ goto out_whdst;
23100+
23101+ d = a->dst_dentry;
23102+ au_set_h_dptr(d, a->btgt, NULL);
23103+ err = au_lkup_neg(d, a->btgt, /*wh*/0);
23104+ if (unlikely(err))
23105+ goto out_whtmp;
23106+ a->dst_h_dentry = au_h_dptr(d, a->btgt);
23107+ }
4a4d8108 23108+ }
1facf9fc 23109+
5afbbe0d 23110+ BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
8b6a4947
AM
23111+#if 0
23112+ BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
23113+ && d_is_positive(a->dst_h_dentry)
23114+ && a->src_btop != a->btgt);
23115+#endif
1facf9fc 23116+
4a4d8108 23117+ /* rename by vfs_rename or cpup */
4a4d8108
AM
23118+ err = au_ren_or_cpup(a);
23119+ if (unlikely(err))
23120+ /* leave the copied-up one */
23121+ goto out_whtmp;
1308ab2a 23122+
4a4d8108 23123+ /* make dir opaque */
f2c43d5f
AM
23124+ err = au_ren_diropq(a);
23125+ if (unlikely(err))
23126+ goto out_rename;
1308ab2a 23127+
4a4d8108 23128+ /* update target timestamps */
f2c43d5f
AM
23129+ if (a->exchange) {
23130+ AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
23131+ a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
23132+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
23133+ a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
23134+ }
5afbbe0d 23135+ AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
4a4d8108
AM
23136+ a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
23137+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
5527c038 23138+ a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
1facf9fc 23139+
f2c43d5f
AM
23140+ if (!a->exchange) {
23141+ /* remove whiteout for dentry */
23142+ if (a->dst_wh_dentry) {
23143+ a->h_path.dentry = a->dst_wh_dentry;
23144+ err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
23145+ a->dst_dentry);
23146+ if (unlikely(err))
23147+ goto out_diropq;
23148+ }
1facf9fc 23149+
f2c43d5f
AM
23150+ /* remove whtmp */
23151+ if (a->thargs)
23152+ au_ren_del_whtmp(a); /* ignore this error */
1308ab2a 23153+
f2c43d5f
AM
23154+ au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
23155+ }
4a4d8108
AM
23156+ err = 0;
23157+ goto out_success;
23158+
4f0767ce 23159+out_diropq:
f2c43d5f 23160+ au_ren_rev_diropq(err, a);
4f0767ce 23161+out_rename:
7e9cd9fe 23162+ au_ren_rev_rename(err, a);
027c5e7a 23163+ dput(a->h_dst);
4f0767ce 23164+out_whtmp:
4a4d8108
AM
23165+ if (a->thargs)
23166+ au_ren_rev_whtmp(err, a);
4f0767ce 23167+out_whdst:
4a4d8108
AM
23168+ dput(a->dst_wh_dentry);
23169+ a->dst_wh_dentry = NULL;
4f0767ce 23170+out_whsrc:
4a4d8108
AM
23171+ if (a->src_wh_dentry)
23172+ au_ren_rev_whsrc(err, a);
4f0767ce 23173+out_success:
4a4d8108
AM
23174+ dput(a->src_wh_dentry);
23175+ dput(a->dst_wh_dentry);
4f0767ce 23176+out_thargs:
4a4d8108
AM
23177+ if (a->thargs) {
23178+ dput(a->h_dst);
23179+ au_whtmp_rmdir_free(a->thargs);
23180+ a->thargs = NULL;
23181+ }
4f0767ce 23182+out:
4a4d8108 23183+ return err;
dece6358 23184+}
1facf9fc 23185+
1308ab2a 23186+/* ---------------------------------------------------------------------- */
1facf9fc 23187+
4a4d8108
AM
23188+/*
23189+ * test if @dentry dir can be rename destination or not.
23190+ * success means, it is a logically empty dir.
23191+ */
23192+static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
1308ab2a 23193+{
4a4d8108 23194+ return au_test_empty(dentry, whlist);
1308ab2a 23195+}
1facf9fc 23196+
4a4d8108 23197+/*
8b6a4947
AM
23198+ * test if @a->src_dentry dir can be rename source or not.
23199+ * if it can, return 0.
4a4d8108
AM
23200+ * success means,
23201+ * - it is a logically empty dir.
23202+ * - or, it exists on writable branch and has no children including whiteouts
8b6a4947 23203+ * on the lower branch unless DIRREN is on.
4a4d8108 23204+ */
8b6a4947 23205+static int may_rename_srcdir(struct au_ren_args *a)
4a4d8108
AM
23206+{
23207+ int err;
23208+ unsigned int rdhash;
8b6a4947
AM
23209+ aufs_bindex_t btop, btgt;
23210+ struct dentry *dentry;
23211+ struct super_block *sb;
23212+ struct au_sbinfo *sbinfo;
1facf9fc 23213+
8b6a4947
AM
23214+ dentry = a->src_dentry;
23215+ sb = dentry->d_sb;
23216+ sbinfo = au_sbi(sb);
23217+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
23218+ au_fset_ren(a->auren_flags, DIRREN);
23219+
23220+ btgt = a->btgt;
5afbbe0d
AM
23221+ btop = au_dbtop(dentry);
23222+ if (btop != btgt) {
4a4d8108 23223+ struct au_nhash whlist;
dece6358 23224+
8b6a4947
AM
23225+ SiMustAnyLock(sb);
23226+ rdhash = sbinfo->si_rdhash;
4a4d8108
AM
23227+ if (!rdhash)
23228+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
23229+ dentry));
23230+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
23231+ if (unlikely(err))
23232+ goto out;
23233+ err = au_test_empty(dentry, &whlist);
23234+ au_nhash_wh_free(&whlist);
23235+ goto out;
23236+ }
dece6358 23237+
5afbbe0d 23238+ if (btop == au_dbtaildir(dentry))
4a4d8108 23239+ return 0; /* success */
dece6358 23240+
4a4d8108 23241+ err = au_test_empty_lower(dentry);
1facf9fc 23242+
4f0767ce 23243+out:
4a4d8108 23244+ if (err == -ENOTEMPTY) {
8b6a4947
AM
23245+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23246+ err = 0;
23247+ } else {
23248+ AuWarn1("renaming dir who has child(ren) on multiple "
23249+ "branches, is not supported\n");
23250+ err = -EXDEV;
23251+ }
4a4d8108
AM
23252+ }
23253+ return err;
23254+}
1308ab2a 23255+
4a4d8108
AM
23256+/* side effect: sets whlist and h_dentry */
23257+static int au_ren_may_dir(struct au_ren_args *a)
1308ab2a 23258+{
4a4d8108
AM
23259+ int err;
23260+ unsigned int rdhash;
23261+ struct dentry *d;
1facf9fc 23262+
4a4d8108
AM
23263+ d = a->dst_dentry;
23264+ SiMustAnyLock(d->d_sb);
1facf9fc 23265+
4a4d8108 23266+ err = 0;
f2c43d5f 23267+ if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
4a4d8108
AM
23268+ rdhash = au_sbi(d->d_sb)->si_rdhash;
23269+ if (!rdhash)
23270+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
23271+ err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
23272+ if (unlikely(err))
23273+ goto out;
1308ab2a 23274+
f2c43d5f
AM
23275+ if (!a->exchange) {
23276+ au_set_dbtop(d, a->dst_btop);
23277+ err = may_rename_dstdir(d, &a->whlist);
23278+ au_set_dbtop(d, a->btgt);
23279+ } else
8b6a4947 23280+ err = may_rename_srcdir(a);
4a4d8108 23281+ }
5afbbe0d 23282+ a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
4a4d8108
AM
23283+ if (unlikely(err))
23284+ goto out;
23285+
23286+ d = a->src_dentry;
5afbbe0d 23287+ a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
f2c43d5f 23288+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
8b6a4947 23289+ err = may_rename_srcdir(a);
4a4d8108
AM
23290+ if (unlikely(err)) {
23291+ au_nhash_wh_free(&a->whlist);
23292+ a->whlist.nh_num = 0;
23293+ }
23294+ }
4f0767ce 23295+out:
4a4d8108 23296+ return err;
1facf9fc 23297+}
23298+
4a4d8108 23299+/* ---------------------------------------------------------------------- */
1facf9fc 23300+
4a4d8108
AM
23301+/*
23302+ * simple tests for rename.
23303+ * following the checks in vfs, plus the parent-child relationship.
23304+ */
23305+static int au_may_ren(struct au_ren_args *a)
23306+{
23307+ int err, isdir;
23308+ struct inode *h_inode;
1facf9fc 23309+
5afbbe0d 23310+ if (a->src_btop == a->btgt) {
4a4d8108 23311+ err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
f2c43d5f 23312+ au_ftest_ren(a->auren_flags, ISDIR_SRC));
4a4d8108
AM
23313+ if (unlikely(err))
23314+ goto out;
23315+ err = -EINVAL;
23316+ if (unlikely(a->src_h_dentry == a->h_trap))
23317+ goto out;
23318+ }
1facf9fc 23319+
4a4d8108 23320+ err = 0;
5afbbe0d 23321+ if (a->dst_btop != a->btgt)
4a4d8108 23322+ goto out;
1facf9fc 23323+
027c5e7a
AM
23324+ err = -ENOTEMPTY;
23325+ if (unlikely(a->dst_h_dentry == a->h_trap))
23326+ goto out;
23327+
4a4d8108 23328+ err = -EIO;
f2c43d5f 23329+ isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
5527c038
JR
23330+ if (d_really_is_negative(a->dst_dentry)) {
23331+ if (d_is_negative(a->dst_h_dentry))
23332+ err = au_may_add(a->dst_dentry, a->btgt,
23333+ a->dst_h_parent, isdir);
4a4d8108 23334+ } else {
5527c038 23335+ if (unlikely(d_is_negative(a->dst_h_dentry)))
4a4d8108 23336+ goto out;
5527c038
JR
23337+ h_inode = d_inode(a->dst_h_dentry);
23338+ if (h_inode->i_nlink)
23339+ err = au_may_del(a->dst_dentry, a->btgt,
23340+ a->dst_h_parent, isdir);
4a4d8108 23341+ }
1facf9fc 23342+
4f0767ce 23343+out:
4a4d8108
AM
23344+ if (unlikely(err == -ENOENT || err == -EEXIST))
23345+ err = -EIO;
23346+ AuTraceErr(err);
23347+ return err;
23348+}
1facf9fc 23349+
1308ab2a 23350+/* ---------------------------------------------------------------------- */
1facf9fc 23351+
4a4d8108
AM
23352+/*
23353+ * locking order
23354+ * (VFS)
23355+ * - src_dir and dir by lock_rename()
acd2b654 23356+ * - inode if exists
4a4d8108
AM
23357+ * (aufs)
23358+ * - lock all
23359+ * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
23360+ * + si_read_lock
23361+ * + di_write_lock2_child()
23362+ * + di_write_lock_child()
23363+ * + ii_write_lock_child()
23364+ * + di_write_lock_child2()
23365+ * + ii_write_lock_child2()
23366+ * + src_parent and parent
23367+ * + di_write_lock_parent()
23368+ * + ii_write_lock_parent()
23369+ * + di_write_lock_parent2()
23370+ * + ii_write_lock_parent2()
23371+ * + lower src_dir and dir by vfsub_lock_rename()
23372+ * + verify the every relationships between child and parent. if any
23373+ * of them failed, unlock all and return -EBUSY.
23374+ */
23375+static void au_ren_unlock(struct au_ren_args *a)
1308ab2a 23376+{
4a4d8108
AM
23377+ vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
23378+ a->dst_h_parent, a->dst_hdir);
8b6a4947
AM
23379+ if (au_ftest_ren(a->auren_flags, DIRREN)
23380+ && a->h_root)
23381+ au_hn_inode_unlock(a->h_root);
f2c43d5f 23382+ if (au_ftest_ren(a->auren_flags, MNT_WRITE))
86dc4139 23383+ vfsub_mnt_drop_write(au_br_mnt(a->br));
1308ab2a 23384+}
23385+
4a4d8108 23386+static int au_ren_lock(struct au_ren_args *a)
1308ab2a 23387+{
4a4d8108
AM
23388+ int err;
23389+ unsigned int udba;
1308ab2a 23390+
4a4d8108
AM
23391+ err = 0;
23392+ a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
23393+ a->src_hdir = au_hi(a->src_dir, a->btgt);
23394+ a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
23395+ a->dst_hdir = au_hi(a->dst_dir, a->btgt);
86dc4139
AM
23396+
23397+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
23398+ if (unlikely(err))
23399+ goto out;
f2c43d5f 23400+ au_fset_ren(a->auren_flags, MNT_WRITE);
8b6a4947
AM
23401+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23402+ struct dentry *root;
23403+ struct inode *dir;
23404+
23405+ /*
23406+ * sbinfo is already locked, so this ii_read_lock is
23407+ * unnecessary. but our debugging feature checks it.
23408+ */
23409+ root = a->src_inode->i_sb->s_root;
23410+ if (root != a->src_parent && root != a->dst_parent) {
23411+ dir = d_inode(root);
23412+ ii_read_lock_parent3(dir);
23413+ a->h_root = au_hi(dir, a->btgt);
23414+ ii_read_unlock(dir);
23415+ au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
23416+ }
23417+ }
4a4d8108
AM
23418+ a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
23419+ a->dst_h_parent, a->dst_hdir);
23420+ udba = au_opt_udba(a->src_dentry->d_sb);
5527c038
JR
23421+ if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
23422+ || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
4a4d8108 23423+ err = au_busy_or_stale();
5afbbe0d 23424+ if (!err && au_dbtop(a->src_dentry) == a->btgt)
4a4d8108 23425+ err = au_h_verify(a->src_h_dentry, udba,
5527c038 23426+ d_inode(a->src_h_parent), a->src_h_parent,
4a4d8108 23427+ a->br);
5afbbe0d 23428+ if (!err && au_dbtop(a->dst_dentry) == a->btgt)
4a4d8108 23429+ err = au_h_verify(a->dst_h_dentry, udba,
5527c038 23430+ d_inode(a->dst_h_parent), a->dst_h_parent,
4a4d8108 23431+ a->br);
86dc4139 23432+ if (!err)
4a4d8108 23433+ goto out; /* success */
4a4d8108
AM
23434+
23435+ err = au_busy_or_stale();
4a4d8108 23436+ au_ren_unlock(a);
86dc4139 23437+
4f0767ce 23438+out:
4a4d8108 23439+ return err;
1facf9fc 23440+}
23441+
23442+/* ---------------------------------------------------------------------- */
23443+
4a4d8108 23444+static void au_ren_refresh_dir(struct au_ren_args *a)
1facf9fc 23445+{
4a4d8108 23446+ struct inode *dir;
dece6358 23447+
4a4d8108 23448+ dir = a->dst_dir;
be118d29 23449+ inode_inc_iversion(dir);
f2c43d5f 23450+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
4a4d8108
AM
23451+ /* is this updating defined in POSIX? */
23452+ au_cpup_attr_timesizes(a->src_inode);
23453+ au_cpup_attr_nlink(dir, /*force*/1);
4a4d8108 23454+ }
b912730e 23455+ au_dir_ts(dir, a->btgt);
dece6358 23456+
f2c43d5f
AM
23457+ if (a->exchange) {
23458+ dir = a->src_dir;
be118d29 23459+ inode_inc_iversion(dir);
f2c43d5f
AM
23460+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23461+ /* is this updating defined in POSIX? */
23462+ au_cpup_attr_timesizes(a->dst_inode);
23463+ au_cpup_attr_nlink(dir, /*force*/1);
23464+ }
23465+ au_dir_ts(dir, a->btgt);
23466+ }
23467+
23468+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108 23469+ return;
dece6358 23470+
4a4d8108 23471+ dir = a->src_dir;
be118d29 23472+ inode_inc_iversion(dir);
f2c43d5f 23473+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
4a4d8108 23474+ au_cpup_attr_nlink(dir, /*force*/1);
b912730e 23475+ au_dir_ts(dir, a->btgt);
1facf9fc 23476+}
23477+
4a4d8108 23478+static void au_ren_refresh(struct au_ren_args *a)
1facf9fc 23479+{
5afbbe0d 23480+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
23481+ struct dentry *d, *h_d;
23482+ struct inode *i, *h_i;
23483+ struct super_block *sb;
dece6358 23484+
027c5e7a
AM
23485+ d = a->dst_dentry;
23486+ d_drop(d);
23487+ if (a->h_dst)
23488+ /* already dget-ed by au_ren_or_cpup() */
23489+ au_set_h_dptr(d, a->btgt, a->h_dst);
23490+
23491+ i = a->dst_inode;
23492+ if (i) {
f2c43d5f
AM
23493+ if (!a->exchange) {
23494+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
23495+ vfsub_drop_nlink(i);
23496+ else {
23497+ vfsub_dead_dir(i);
23498+ au_cpup_attr_timesizes(i);
23499+ }
23500+ au_update_dbrange(d, /*do_put_zero*/1);
23501+ } else
23502+ au_cpup_attr_nlink(i, /*force*/1);
027c5e7a 23503+ } else {
5afbbe0d
AM
23504+ bbot = a->btgt;
23505+ for (bindex = au_dbtop(d); bindex < bbot; bindex++)
027c5e7a 23506+ au_set_h_dptr(d, bindex, NULL);
5afbbe0d
AM
23507+ bbot = au_dbbot(d);
23508+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
027c5e7a
AM
23509+ au_set_h_dptr(d, bindex, NULL);
23510+ au_update_dbrange(d, /*do_put_zero*/0);
23511+ }
23512+
8b6a4947
AM
23513+ if (a->exchange
23514+ || au_ftest_ren(a->auren_flags, DIRREN)) {
23515+ d_drop(a->src_dentry);
23516+ if (au_ftest_ren(a->auren_flags, DIRREN))
23517+ au_set_dbwh(a->src_dentry, -1);
23518+ return;
23519+ }
23520+
4a4d8108 23521+ d = a->src_dentry;
8b6a4947
AM
23522+ au_set_dbwh(d, -1);
23523+ bbot = au_dbbot(d);
23524+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23525+ h_d = au_h_dptr(d, bindex);
23526+ if (h_d)
23527+ au_set_h_dptr(d, bindex, NULL);
23528+ }
23529+ au_set_dbbot(d, a->btgt);
4a4d8108 23530+
8b6a4947
AM
23531+ sb = d->d_sb;
23532+ i = a->src_inode;
23533+ if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
23534+ return; /* success */
4a4d8108 23535+
8b6a4947
AM
23536+ bbot = au_ibbot(i);
23537+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23538+ h_i = au_h_iptr(i, bindex);
23539+ if (h_i) {
23540+ au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
23541+ /* ignore this error */
23542+ au_set_h_iptr(i, bindex, NULL, 0);
4a4d8108
AM
23543+ }
23544+ }
8b6a4947 23545+ au_set_ibbot(i, a->btgt);
1308ab2a 23546+}
dece6358 23547+
4a4d8108
AM
23548+/* ---------------------------------------------------------------------- */
23549+
23550+/* mainly for link(2) and rename(2) */
23551+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
1308ab2a 23552+{
4a4d8108
AM
23553+ aufs_bindex_t bdiropq, bwh;
23554+ struct dentry *parent;
23555+ struct au_branch *br;
23556+
23557+ parent = dentry->d_parent;
5527c038 23558+ IMustLock(d_inode(parent)); /* dir is locked */
4a4d8108
AM
23559+
23560+ bdiropq = au_dbdiropq(parent);
23561+ bwh = au_dbwh(dentry);
23562+ br = au_sbr(dentry->d_sb, btgt);
23563+ if (au_br_rdonly(br)
23564+ || (0 <= bdiropq && bdiropq < btgt)
23565+ || (0 <= bwh && bwh < btgt))
23566+ btgt = -1;
23567+
23568+ AuDbg("btgt %d\n", btgt);
23569+ return btgt;
1facf9fc 23570+}
23571+
5afbbe0d 23572+/* sets src_btop, dst_btop and btgt */
4a4d8108 23573+static int au_ren_wbr(struct au_ren_args *a)
1facf9fc 23574+{
4a4d8108
AM
23575+ int err;
23576+ struct au_wr_dir_args wr_dir_args = {
23577+ /* .force_btgt = -1, */
23578+ .flags = AuWrDir_ADD_ENTRY
23579+ };
dece6358 23580+
5afbbe0d
AM
23581+ a->src_btop = au_dbtop(a->src_dentry);
23582+ a->dst_btop = au_dbtop(a->dst_dentry);
f2c43d5f
AM
23583+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
23584+ || au_ftest_ren(a->auren_flags, ISDIR_DST))
4a4d8108 23585+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
5afbbe0d
AM
23586+ wr_dir_args.force_btgt = a->src_btop;
23587+ if (a->dst_inode && a->dst_btop < a->src_btop)
23588+ wr_dir_args.force_btgt = a->dst_btop;
4a4d8108
AM
23589+ wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
23590+ err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
23591+ a->btgt = err;
f2c43d5f
AM
23592+ if (a->exchange)
23593+ au_update_dbtop(a->dst_dentry);
dece6358 23594+
4a4d8108 23595+ return err;
1facf9fc 23596+}
23597+
4a4d8108 23598+static void au_ren_dt(struct au_ren_args *a)
1facf9fc 23599+{
4a4d8108
AM
23600+ a->h_path.dentry = a->src_h_parent;
23601+ au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
f2c43d5f 23602+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
4a4d8108
AM
23603+ a->h_path.dentry = a->dst_h_parent;
23604+ au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
23605+ }
1facf9fc 23606+
f2c43d5f
AM
23607+ au_fclr_ren(a->auren_flags, DT_DSTDIR);
23608+ if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
23609+ && !a->exchange)
4a4d8108 23610+ return;
dece6358 23611+
4a4d8108
AM
23612+ a->h_path.dentry = a->src_h_dentry;
23613+ au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
5527c038 23614+ if (d_is_positive(a->dst_h_dentry)) {
f2c43d5f 23615+ au_fset_ren(a->auren_flags, DT_DSTDIR);
4a4d8108
AM
23616+ a->h_path.dentry = a->dst_h_dentry;
23617+ au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
23618+ }
1308ab2a 23619+}
dece6358 23620+
4a4d8108 23621+static void au_ren_rev_dt(int err, struct au_ren_args *a)
1308ab2a 23622+{
4a4d8108 23623+ struct dentry *h_d;
febd17d6 23624+ struct inode *h_inode;
4a4d8108
AM
23625+
23626+ au_dtime_revert(a->src_dt + AuPARENT);
f2c43d5f 23627+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108
AM
23628+ au_dtime_revert(a->dst_dt + AuPARENT);
23629+
f2c43d5f 23630+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
4a4d8108 23631+ h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
23632+ h_inode = d_inode(h_d);
23633+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 23634+ au_dtime_revert(a->src_dt + AuCHILD);
febd17d6 23635+ inode_unlock(h_inode);
4a4d8108 23636+
f2c43d5f 23637+ if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
4a4d8108 23638+ h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
23639+ h_inode = d_inode(h_d);
23640+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 23641+ au_dtime_revert(a->dst_dt + AuCHILD);
febd17d6 23642+ inode_unlock(h_inode);
1facf9fc 23643+ }
23644+ }
23645+}
23646+
4a4d8108
AM
23647+/* ---------------------------------------------------------------------- */
23648+
23649+int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
f2c43d5f
AM
23650+ struct inode *_dst_dir, struct dentry *_dst_dentry,
23651+ unsigned int _flags)
1facf9fc 23652+{
f2c43d5f 23653+ int err, lock_flags;
8b6a4947 23654+ void *rev;
4a4d8108
AM
23655+ /* reduce stack space */
23656+ struct au_ren_args *a;
f2c43d5f 23657+ struct au_pin pin;
4a4d8108 23658+
f2c43d5f 23659+ AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
4a4d8108
AM
23660+ IMustLock(_src_dir);
23661+ IMustLock(_dst_dir);
23662+
f2c43d5f
AM
23663+ err = -EINVAL;
23664+ if (unlikely(_flags & RENAME_WHITEOUT))
23665+ goto out;
23666+
4a4d8108
AM
23667+ err = -ENOMEM;
23668+ BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
23669+ a = kzalloc(sizeof(*a), GFP_NOFS);
23670+ if (unlikely(!a))
23671+ goto out;
23672+
f2c43d5f 23673+ a->flags = _flags;
acd2b654
AM
23674+ BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
23675+ && RENAME_EXCHANGE > U8_MAX);
f2c43d5f 23676+ a->exchange = _flags & RENAME_EXCHANGE;
4a4d8108
AM
23677+ a->src_dir = _src_dir;
23678+ a->src_dentry = _src_dentry;
5527c038
JR
23679+ a->src_inode = NULL;
23680+ if (d_really_is_positive(a->src_dentry))
23681+ a->src_inode = d_inode(a->src_dentry);
4a4d8108
AM
23682+ a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
23683+ a->dst_dir = _dst_dir;
23684+ a->dst_dentry = _dst_dentry;
5527c038
JR
23685+ a->dst_inode = NULL;
23686+ if (d_really_is_positive(a->dst_dentry))
23687+ a->dst_inode = d_inode(a->dst_dentry);
4a4d8108
AM
23688+ a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
23689+ if (a->dst_inode) {
f2c43d5f
AM
23690+ /*
23691+ * if EXCHANGE && src is non-dir && dst is dir,
23692+ * dst is not locked.
23693+ */
23694+ /* IMustLock(a->dst_inode); */
4a4d8108 23695+ au_igrab(a->dst_inode);
1facf9fc 23696+ }
1facf9fc 23697+
4a4d8108 23698+ err = -ENOTDIR;
f2c43d5f 23699+ lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
2000de60 23700+ if (d_is_dir(a->src_dentry)) {
f2c43d5f
AM
23701+ au_fset_ren(a->auren_flags, ISDIR_SRC);
23702+ if (unlikely(!a->exchange
23703+ && d_really_is_positive(a->dst_dentry)
2000de60 23704+ && !d_is_dir(a->dst_dentry)))
4a4d8108 23705+ goto out_free;
f2c43d5f
AM
23706+ lock_flags |= AuLock_DIRS;
23707+ }
23708+ if (a->dst_inode && d_is_dir(a->dst_dentry)) {
23709+ au_fset_ren(a->auren_flags, ISDIR_DST);
23710+ if (unlikely(!a->exchange
23711+ && d_really_is_positive(a->src_dentry)
23712+ && !d_is_dir(a->src_dentry)))
23713+ goto out_free;
23714+ lock_flags |= AuLock_DIRS;
b95c5147 23715+ }
8b6a4947
AM
23716+ err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
23717+ lock_flags);
e49829fe
JR
23718+ if (unlikely(err))
23719+ goto out_free;
1facf9fc 23720+
027c5e7a
AM
23721+ err = au_d_hashed_positive(a->src_dentry);
23722+ if (unlikely(err))
23723+ goto out_unlock;
23724+ err = -ENOENT;
23725+ if (a->dst_inode) {
23726+ /*
f2c43d5f 23727+ * If it is a dir, VFS unhash it before this
027c5e7a
AM
23728+ * function. It means we cannot rely upon d_unhashed().
23729+ */
23730+ if (unlikely(!a->dst_inode->i_nlink))
23731+ goto out_unlock;
f2c43d5f 23732+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
027c5e7a 23733+ err = au_d_hashed_positive(a->dst_dentry);
f2c43d5f 23734+ if (unlikely(err && !a->exchange))
027c5e7a
AM
23735+ goto out_unlock;
23736+ } else if (unlikely(IS_DEADDIR(a->dst_inode)))
23737+ goto out_unlock;
23738+ } else if (unlikely(d_unhashed(a->dst_dentry)))
23739+ goto out_unlock;
23740+
7eafdf33
AM
23741+ /*
23742+ * is it possible?
79b8bda9 23743+ * yes, it happened (in linux-3.3-rcN) but I don't know why.
7eafdf33
AM
23744+ * there may exist a problem somewhere else.
23745+ */
23746+ err = -EINVAL;
5527c038 23747+ if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
7eafdf33
AM
23748+ goto out_unlock;
23749+
f2c43d5f 23750+ au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
4a4d8108 23751+ di_write_lock_parent(a->dst_parent);
1facf9fc 23752+
4a4d8108
AM
23753+ /* which branch we process */
23754+ err = au_ren_wbr(a);
23755+ if (unlikely(err < 0))
027c5e7a 23756+ goto out_parent;
4a4d8108 23757+ a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
86dc4139 23758+ a->h_path.mnt = au_br_mnt(a->br);
1facf9fc 23759+
4a4d8108
AM
23760+ /* are they available to be renamed */
23761+ err = au_ren_may_dir(a);
23762+ if (unlikely(err))
23763+ goto out_children;
1facf9fc 23764+
4a4d8108 23765+ /* prepare the writable parent dir on the same branch */
5afbbe0d 23766+ if (a->dst_btop == a->btgt) {
f2c43d5f 23767+ au_fset_ren(a->auren_flags, WHDST);
4a4d8108
AM
23768+ } else {
23769+ err = au_cpup_dirs(a->dst_dentry, a->btgt);
23770+ if (unlikely(err))
23771+ goto out_children;
23772+ }
1facf9fc 23773+
f2c43d5f
AM
23774+ err = 0;
23775+ if (!a->exchange) {
23776+ if (a->src_dir != a->dst_dir) {
23777+ /*
23778+ * this temporary unlock is safe,
23779+ * because both dir->i_mutex are locked.
23780+ */
23781+ di_write_unlock(a->dst_parent);
23782+ di_write_lock_parent(a->src_parent);
23783+ err = au_wr_dir_need_wh(a->src_dentry,
23784+ au_ftest_ren(a->auren_flags,
23785+ ISDIR_SRC),
23786+ &a->btgt);
23787+ di_write_unlock(a->src_parent);
23788+ di_write_lock2_parent(a->src_parent, a->dst_parent,
23789+ /*isdir*/1);
23790+ au_fclr_ren(a->auren_flags, ISSAMEDIR);
23791+ } else
23792+ err = au_wr_dir_need_wh(a->src_dentry,
23793+ au_ftest_ren(a->auren_flags,
23794+ ISDIR_SRC),
23795+ &a->btgt);
23796+ }
4a4d8108
AM
23797+ if (unlikely(err < 0))
23798+ goto out_children;
23799+ if (err)
f2c43d5f 23800+ au_fset_ren(a->auren_flags, WHSRC);
1facf9fc 23801+
86dc4139 23802+ /* cpup src */
5afbbe0d 23803+ if (a->src_btop != a->btgt) {
86dc4139
AM
23804+ err = au_pin(&pin, a->src_dentry, a->btgt,
23805+ au_opt_udba(a->src_dentry->d_sb),
23806+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 23807+ if (!err) {
c2b27bf2
AM
23808+ struct au_cp_generic cpg = {
23809+ .dentry = a->src_dentry,
23810+ .bdst = a->btgt,
5afbbe0d 23811+ .bsrc = a->src_btop,
c2b27bf2
AM
23812+ .len = -1,
23813+ .pin = &pin,
23814+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23815+ };
5afbbe0d 23816+ AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
c2b27bf2 23817+ err = au_sio_cpup_simple(&cpg);
367653fa 23818+ au_unpin(&pin);
86dc4139 23819+ }
86dc4139
AM
23820+ if (unlikely(err))
23821+ goto out_children;
5afbbe0d 23822+ a->src_btop = a->btgt;
86dc4139 23823+ a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
f2c43d5f
AM
23824+ if (!a->exchange)
23825+ au_fset_ren(a->auren_flags, WHSRC);
23826+ }
23827+
23828+ /* cpup dst */
23829+ if (a->exchange && a->dst_inode
23830+ && a->dst_btop != a->btgt) {
23831+ err = au_pin(&pin, a->dst_dentry, a->btgt,
23832+ au_opt_udba(a->dst_dentry->d_sb),
23833+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23834+ if (!err) {
23835+ struct au_cp_generic cpg = {
23836+ .dentry = a->dst_dentry,
23837+ .bdst = a->btgt,
23838+ .bsrc = a->dst_btop,
23839+ .len = -1,
23840+ .pin = &pin,
23841+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23842+ };
23843+ err = au_sio_cpup_simple(&cpg);
23844+ au_unpin(&pin);
23845+ }
23846+ if (unlikely(err))
23847+ goto out_children;
23848+ a->dst_btop = a->btgt;
23849+ a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
86dc4139
AM
23850+ }
23851+
4a4d8108
AM
23852+ /* lock them all */
23853+ err = au_ren_lock(a);
23854+ if (unlikely(err))
86dc4139 23855+ /* leave the copied-up one */
4a4d8108 23856+ goto out_children;
1facf9fc 23857+
f2c43d5f
AM
23858+ if (!a->exchange) {
23859+ if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
23860+ err = au_may_ren(a);
23861+ else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
23862+ err = -ENAMETOOLONG;
23863+ if (unlikely(err))
23864+ goto out_hdir;
23865+ }
1facf9fc 23866+
4a4d8108
AM
23867+ /* store timestamps to be revertible */
23868+ au_ren_dt(a);
1facf9fc 23869+
8b6a4947
AM
23870+ /* store dirren info */
23871+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23872+ err = au_dr_rename(a->src_dentry, a->btgt,
23873+ &a->dst_dentry->d_name, &rev);
23874+ AuTraceErr(err);
23875+ if (unlikely(err))
23876+ goto out_dt;
23877+ }
23878+
4a4d8108
AM
23879+ /* here we go */
23880+ err = do_rename(a);
23881+ if (unlikely(err))
8b6a4947
AM
23882+ goto out_dirren;
23883+
23884+ if (au_ftest_ren(a->auren_flags, DIRREN))
23885+ au_dr_rename_fin(a->src_dentry, a->btgt, rev);
4a4d8108
AM
23886+
23887+ /* update dir attributes */
23888+ au_ren_refresh_dir(a);
23889+
23890+ /* dput/iput all lower dentries */
23891+ au_ren_refresh(a);
23892+
23893+ goto out_hdir; /* success */
23894+
8b6a4947
AM
23895+out_dirren:
23896+ if (au_ftest_ren(a->auren_flags, DIRREN))
23897+ au_dr_rename_rev(a->src_dentry, a->btgt, rev);
4f0767ce 23898+out_dt:
4a4d8108 23899+ au_ren_rev_dt(err, a);
4f0767ce 23900+out_hdir:
4a4d8108 23901+ au_ren_unlock(a);
4f0767ce 23902+out_children:
4a4d8108 23903+ au_nhash_wh_free(&a->whlist);
5afbbe0d
AM
23904+ if (err && a->dst_inode && a->dst_btop != a->btgt) {
23905+ AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
027c5e7a 23906+ au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
5afbbe0d 23907+ au_set_dbtop(a->dst_dentry, a->dst_btop);
4a4d8108 23908+ }
027c5e7a 23909+out_parent:
f2c43d5f 23910+ if (!err) {
8b6a4947
AM
23911+ if (d_unhashed(a->src_dentry))
23912+ au_fset_ren(a->auren_flags, DROPPED_SRC);
23913+ if (d_unhashed(a->dst_dentry))
23914+ au_fset_ren(a->auren_flags, DROPPED_DST);
f2c43d5f
AM
23915+ if (!a->exchange)
23916+ d_move(a->src_dentry, a->dst_dentry);
8b6a4947 23917+ else {
f2c43d5f 23918+ d_exchange(a->src_dentry, a->dst_dentry);
8b6a4947
AM
23919+ if (au_ftest_ren(a->auren_flags, DROPPED_DST))
23920+ d_drop(a->dst_dentry);
23921+ }
23922+ if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
23923+ d_drop(a->src_dentry);
f2c43d5f 23924+ } else {
5afbbe0d 23925+ au_update_dbtop(a->dst_dentry);
027c5e7a
AM
23926+ if (!a->dst_inode)
23927+ d_drop(a->dst_dentry);
23928+ }
f2c43d5f 23929+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108
AM
23930+ di_write_unlock(a->dst_parent);
23931+ else
23932+ di_write_unlock2(a->src_parent, a->dst_parent);
027c5e7a 23933+out_unlock:
4a4d8108 23934+ aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
4f0767ce 23935+out_free:
4a4d8108
AM
23936+ iput(a->dst_inode);
23937+ if (a->thargs)
23938+ au_whtmp_rmdir_free(a->thargs);
9f237c51 23939+ au_kfree_rcu(a);
4f0767ce 23940+out:
4a4d8108
AM
23941+ AuTraceErr(err);
23942+ return err;
1308ab2a 23943+}
7f207e10
AM
23944diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
23945--- /usr/share/empty/fs/aufs/Kconfig 1970-01-01 01:00:00.000000000 +0100
b00004a5 23946+++ linux/fs/aufs/Kconfig 2018-06-04 09:08:09.181412645 +0200
2121bcd9
AM
23947@@ -0,0 +1,199 @@
23948+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
23949+config AUFS_FS
23950+ tristate "Aufs (Advanced multi layered unification filesystem) support"
4a4d8108
AM
23951+ help
23952+ Aufs is a stackable unification filesystem such as Unionfs,
23953+ which unifies several directories and provides a merged single
23954+ directory.
23955+ In the early days, aufs was entirely re-designed and
23956+ re-implemented Unionfs Version 1.x series. Introducing many
23957+ original ideas, approaches and improvements, it becomes totally
23958+ different from Unionfs while keeping the basic features.
1facf9fc 23959+
4a4d8108
AM
23960+if AUFS_FS
23961+choice
23962+ prompt "Maximum number of branches"
23963+ default AUFS_BRANCH_MAX_127
23964+ help
23965+ Specifies the maximum number of branches (or member directories)
23966+ in a single aufs. The larger value consumes more system
23967+ resources and has a minor impact to performance.
23968+config AUFS_BRANCH_MAX_127
23969+ bool "127"
23970+ help
23971+ Specifies the maximum number of branches (or member directories)
23972+ in a single aufs. The larger value consumes more system
23973+ resources and has a minor impact to performance.
23974+config AUFS_BRANCH_MAX_511
23975+ bool "511"
23976+ help
23977+ Specifies the maximum number of branches (or member directories)
23978+ in a single aufs. The larger value consumes more system
23979+ resources and has a minor impact to performance.
23980+config AUFS_BRANCH_MAX_1023
23981+ bool "1023"
23982+ help
23983+ Specifies the maximum number of branches (or member directories)
23984+ in a single aufs. The larger value consumes more system
23985+ resources and has a minor impact to performance.
23986+config AUFS_BRANCH_MAX_32767
23987+ bool "32767"
23988+ help
23989+ Specifies the maximum number of branches (or member directories)
23990+ in a single aufs. The larger value consumes more system
23991+ resources and has a minor impact to performance.
23992+endchoice
1facf9fc 23993+
e49829fe
JR
23994+config AUFS_SBILIST
23995+ bool
23996+ depends on AUFS_MAGIC_SYSRQ || PROC_FS
23997+ default y
23998+ help
23999+ Automatic configuration for internal use.
24000+ When aufs supports Magic SysRq or /proc, enabled automatically.
24001+
4a4d8108
AM
24002+config AUFS_HNOTIFY
24003+ bool "Detect direct branch access (bypassing aufs)"
24004+ help
24005+ If you want to modify files on branches directly, eg. bypassing aufs,
24006+ and want aufs to detect the changes of them fully, then enable this
24007+ option and use 'udba=notify' mount option.
7f207e10 24008+ Currently there is only one available configuration, "fsnotify".
4a4d8108
AM
24009+ It will have a negative impact to the performance.
24010+ See detail in aufs.5.
dece6358 24011+
4a4d8108
AM
24012+choice
24013+ prompt "method" if AUFS_HNOTIFY
24014+ default AUFS_HFSNOTIFY
24015+config AUFS_HFSNOTIFY
24016+ bool "fsnotify"
24017+ select FSNOTIFY
4a4d8108 24018+endchoice
1facf9fc 24019+
4a4d8108
AM
24020+config AUFS_EXPORT
24021+ bool "NFS-exportable aufs"
2cbb1c4b 24022+ depends on EXPORTFS
4a4d8108
AM
24023+ help
24024+ If you want to export your mounted aufs via NFS, then enable this
24025+ option. There are several requirements for this configuration.
24026+ See detail in aufs.5.
1facf9fc 24027+
4a4d8108
AM
24028+config AUFS_INO_T_64
24029+ bool
24030+ depends on AUFS_EXPORT
24031+ depends on 64BIT && !(ALPHA || S390)
24032+ default y
24033+ help
24034+ Automatic configuration for internal use.
24035+ /* typedef unsigned long/int __kernel_ino_t */
24036+ /* alpha and s390x are int */
1facf9fc 24037+
c1595e42
JR
24038+config AUFS_XATTR
24039+ bool "support for XATTR/EA (including Security Labels)"
24040+ help
24041+ If your branch fs supports XATTR/EA and you want to make them
24042+ available in aufs too, then enable this opsion and specify the
24043+ branch attributes for EA.
24044+ See detail in aufs.5.
24045+
076b876e
AM
24046+config AUFS_FHSM
24047+ bool "File-based Hierarchical Storage Management"
24048+ help
24049+ Hierarchical Storage Management (or HSM) is a well-known feature
24050+ in the storage world. Aufs provides this feature as file-based.
24051+ with multiple branches.
24052+ These multiple branches are prioritized, ie. the topmost one
24053+ should be the fastest drive and be used heavily.
24054+
4a4d8108
AM
24055+config AUFS_RDU
24056+ bool "Readdir in userspace"
24057+ help
24058+ Aufs has two methods to provide a merged view for a directory,
24059+ by a user-space library and by kernel-space natively. The latter
24060+ is always enabled but sometimes large and slow.
24061+ If you enable this option, install the library in aufs2-util
24062+ package, and set some environment variables for your readdir(3),
24063+ then the work will be handled in user-space which generally
24064+ shows better performance in most cases.
24065+ See detail in aufs.5.
1facf9fc 24066+
8b6a4947
AM
24067+config AUFS_DIRREN
24068+ bool "Workaround for rename(2)-ing a directory"
24069+ help
24070+ By default, aufs returns EXDEV error in renameing a dir who has
24071+ his child on the lower branch, since it is a bad idea to issue
24072+ rename(2) internally for every lower branch. But user may not
24073+ accept this behaviour. So here is a workaround to allow such
24074+ rename(2) and store some extra infromation on the writable
24075+ branch. Obviously this costs high (and I don't like it).
24076+ To use this feature, you need to enable this configuration AND
24077+ to specify the mount option `dirren.'
24078+ See details in aufs.5 and the design documents.
24079+
4a4d8108
AM
24080+config AUFS_SHWH
24081+ bool "Show whiteouts"
24082+ help
24083+ If you want to make the whiteouts in aufs visible, then enable
24084+ this option and specify 'shwh' mount option. Although it may
24085+ sounds like philosophy or something, but in technically it
24086+ simply shows the name of whiteout with keeping its behaviour.
1facf9fc 24087+
4a4d8108
AM
24088+config AUFS_BR_RAMFS
24089+ bool "Ramfs (initramfs/rootfs) as an aufs branch"
24090+ help
24091+ If you want to use ramfs as an aufs branch fs, then enable this
24092+ option. Generally tmpfs is recommended.
24093+ Aufs prohibited them to be a branch fs by default, because
24094+ initramfs becomes unusable after switch_root or something
24095+ generally. If you sets initramfs as an aufs branch and boot your
24096+ system by switch_root, you will meet a problem easily since the
24097+ files in initramfs may be inaccessible.
24098+ Unless you are going to use ramfs as an aufs branch fs without
24099+ switch_root or something, leave it N.
1facf9fc 24100+
4a4d8108
AM
24101+config AUFS_BR_FUSE
24102+ bool "Fuse fs as an aufs branch"
24103+ depends on FUSE_FS
24104+ select AUFS_POLL
24105+ help
24106+ If you want to use fuse-based userspace filesystem as an aufs
24107+ branch fs, then enable this option.
24108+ It implements the internal poll(2) operation which is
24109+ implemented by fuse only (curretnly).
1facf9fc 24110+
4a4d8108
AM
24111+config AUFS_POLL
24112+ bool
24113+ help
24114+ Automatic configuration for internal use.
1facf9fc 24115+
4a4d8108
AM
24116+config AUFS_BR_HFSPLUS
24117+ bool "Hfsplus as an aufs branch"
24118+ depends on HFSPLUS_FS
24119+ default y
24120+ help
24121+ If you want to use hfsplus fs as an aufs branch fs, then enable
24122+ this option. This option introduces a small overhead at
24123+ copying-up a file on hfsplus.
1facf9fc 24124+
4a4d8108
AM
24125+config AUFS_BDEV_LOOP
24126+ bool
24127+ depends on BLK_DEV_LOOP
24128+ default y
24129+ help
24130+ Automatic configuration for internal use.
24131+ Convert =[ym] into =y.
1308ab2a 24132+
4a4d8108
AM
24133+config AUFS_DEBUG
24134+ bool "Debug aufs"
24135+ help
24136+ Enable this to compile aufs internal debug code.
24137+ It will have a negative impact to the performance.
24138+
24139+config AUFS_MAGIC_SYSRQ
24140+ bool
24141+ depends on AUFS_DEBUG && MAGIC_SYSRQ
24142+ default y
24143+ help
24144+ Automatic configuration for internal use.
24145+ When aufs supports Magic SysRq, enabled automatically.
24146+endif
acd2b654
AM
24147diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
24148--- /usr/share/empty/fs/aufs/lcnt.h 1970-01-01 01:00:00.000000000 +0100
24149+++ linux/fs/aufs/lcnt.h 2018-10-23 12:33:35.599375796 +0200
24150@@ -0,0 +1,186 @@
24151+/* SPDX-License-Identifier: GPL-2.0 */
24152+/*
24153+ * Copyright (C) 2018 Junjiro R. Okajima
24154+ *
24155+ * This program, aufs is free software; you can redistribute it and/or modify
24156+ * it under the terms of the GNU General Public License as published by
24157+ * the Free Software Foundation; either version 2 of the License, or
24158+ * (at your option) any later version.
24159+ *
24160+ * This program is distributed in the hope that it will be useful,
24161+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24162+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24163+ * GNU General Public License for more details.
24164+ *
24165+ * You should have received a copy of the GNU General Public License
24166+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
24167+ */
24168+
24169+/*
24170+ * simple long counter wrapper
24171+ */
24172+
24173+#ifndef __AUFS_LCNT_H__
24174+#define __AUFS_LCNT_H__
24175+
24176+#ifdef __KERNEL__
24177+
24178+#include "debug.h"
24179+
24180+#define AuLCntATOMIC 1
24181+#define AuLCntPCPUCNT 2
24182+/*
24183+ * why does percpu_refcount require extra synchronize_rcu()s in
24184+ * au_br_do_free()
24185+ */
24186+#define AuLCntPCPUREF 3
24187+
24188+/* #define AuLCntChosen AuLCntATOMIC */
24189+#define AuLCntChosen AuLCntPCPUCNT
24190+/* #define AuLCntChosen AuLCntPCPUREF */
24191+
24192+#if AuLCntChosen == AuLCntATOMIC
24193+#include <linux/atomic.h>
24194+
24195+typedef atomic_long_t au_lcnt_t;
24196+
24197+static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24198+{
24199+ atomic_long_set(cnt, 0);
24200+ return 0;
24201+}
24202+
24203+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24204+{
24205+ /* empty */
24206+}
24207+
24208+static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
24209+ int do_sync __maybe_unused)
24210+{
24211+ /* empty */
24212+}
24213+
24214+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24215+{
24216+ atomic_long_inc(cnt);
24217+}
24218+
24219+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24220+{
24221+ atomic_long_dec(cnt);
24222+}
24223+
24224+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24225+{
24226+ return atomic_long_read(cnt);
24227+}
24228+#endif
24229+
24230+#if AuLCntChosen == AuLCntPCPUCNT
24231+#include <linux/percpu_counter.h>
24232+
24233+typedef struct percpu_counter au_lcnt_t;
24234+
24235+static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24236+{
24237+ return percpu_counter_init(cnt, 0, GFP_NOFS);
24238+}
24239+
24240+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24241+{
24242+ /* empty */
24243+}
24244+
24245+static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
24246+{
24247+ percpu_counter_destroy(cnt);
24248+}
24249+
24250+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24251+{
24252+ percpu_counter_inc(cnt);
24253+}
24254+
24255+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24256+{
24257+ percpu_counter_dec(cnt);
24258+}
24259+
24260+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24261+{
24262+ s64 n;
24263+
24264+ n = percpu_counter_sum(cnt);
24265+ BUG_ON(n < 0);
24266+ if (LONG_MAX != LLONG_MAX
24267+ && n > LONG_MAX)
24268+ AuWarn1("%s\n", "wrap-around");
24269+
24270+ return n;
24271+}
24272+#endif
24273+
24274+#if AuLCntChosen == AuLCntPCPUREF
24275+#include <linux/percpu-refcount.h>
24276+
24277+typedef struct percpu_ref au_lcnt_t;
24278+
24279+static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
24280+{
24281+ if (!release)
24282+ release = percpu_ref_exit;
24283+ return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
24284+}
24285+
24286+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24287+{
24288+ synchronize_rcu();
24289+}
24290+
24291+static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
24292+{
24293+ percpu_ref_kill(cnt);
24294+ if (do_sync)
24295+ au_lcnt_wait_for_fin(cnt);
24296+}
24297+
24298+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24299+{
24300+ percpu_ref_get(cnt);
24301+}
24302+
24303+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24304+{
24305+ percpu_ref_put(cnt);
24306+}
24307+
24308+/*
24309+ * avoid calling this func as possible.
24310+ */
24311+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
24312+{
24313+ long l;
24314+
24315+ percpu_ref_switch_to_atomic_sync(cnt);
24316+ l = atomic_long_read(&cnt->count);
24317+ if (do_rev)
24318+ percpu_ref_switch_to_percpu(cnt);
24319+
24320+ /* percpu_ref is initialized by 1 instead of 0 */
24321+ return l - 1;
24322+}
24323+#endif
24324+
24325+#ifdef CONFIG_AUFS_DEBUG
24326+#define AuLCntZero(val) do { \
24327+ long l = val; \
24328+ if (l) \
24329+ AuDbg("%s = %ld\n", #val, l); \
24330+} while (0)
24331+#else
24332+#define AuLCntZero(val) do {} while (0)
24333+#endif
24334+
24335+#endif /* __KERNEL__ */
24336+#endif /* __AUFS_LCNT_H__ */
7f207e10
AM
24337diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
24338--- /usr/share/empty/fs/aufs/loop.c 1970-01-01 01:00:00.000000000 +0100
9f237c51 24339+++ linux/fs/aufs/loop.c 2018-12-27 13:19:17.711749485 +0100
062440b3 24340@@ -0,0 +1,148 @@
cd7a4cd9 24341+// SPDX-License-Identifier: GPL-2.0
1facf9fc 24342+/*
b00004a5 24343+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 24344+ *
24345+ * This program, aufs is free software; you can redistribute it and/or modify
24346+ * it under the terms of the GNU General Public License as published by
24347+ * the Free Software Foundation; either version 2 of the License, or
24348+ * (at your option) any later version.
dece6358
AM
24349+ *
24350+ * This program is distributed in the hope that it will be useful,
24351+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24352+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24353+ * GNU General Public License for more details.
24354+ *
24355+ * You should have received a copy of the GNU General Public License
523b37e3 24356+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24357+ */
24358+
24359+/*
24360+ * support for loopback block device as a branch
24361+ */
24362+
1facf9fc 24363+#include "aufs.h"
24364+
392086de
AM
24365+/* added into drivers/block/loop.c */
24366+static struct file *(*backing_file_func)(struct super_block *sb);
24367+
1facf9fc 24368+/*
24369+ * test if two lower dentries have overlapping branches.
24370+ */
b752ccd1 24371+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
1facf9fc 24372+{
b752ccd1 24373+ struct super_block *h_sb;
392086de
AM
24374+ struct file *backing_file;
24375+
24376+ if (unlikely(!backing_file_func)) {
24377+ /* don't load "loop" module here */
24378+ backing_file_func = symbol_get(loop_backing_file);
24379+ if (unlikely(!backing_file_func))
24380+ /* "loop" module is not loaded */
24381+ return 0;
24382+ }
1facf9fc 24383+
b752ccd1 24384+ h_sb = h_adding->d_sb;
392086de
AM
24385+ backing_file = backing_file_func(h_sb);
24386+ if (!backing_file)
1facf9fc 24387+ return 0;
24388+
2000de60 24389+ h_adding = backing_file->f_path.dentry;
b752ccd1
AM
24390+ /*
24391+ * h_adding can be local NFS.
24392+ * in this case aufs cannot detect the loop.
24393+ */
24394+ if (unlikely(h_adding->d_sb == sb))
1facf9fc 24395+ return 1;
b752ccd1 24396+ return !!au_test_subdir(h_adding, sb->s_root);
1facf9fc 24397+}
24398+
24399+/* true if a kernel thread named 'loop[0-9].*' accesses a file */
24400+int au_test_loopback_kthread(void)
24401+{
b752ccd1
AM
24402+ int ret;
24403+ struct task_struct *tsk = current;
a2a7ad62 24404+ char c, comm[sizeof(tsk->comm)];
b752ccd1
AM
24405+
24406+ ret = 0;
24407+ if (tsk->flags & PF_KTHREAD) {
a2a7ad62
AM
24408+ get_task_comm(comm, tsk);
24409+ c = comm[4];
b752ccd1 24410+ ret = ('0' <= c && c <= '9'
a2a7ad62 24411+ && !strncmp(comm, "loop", 4));
b752ccd1 24412+ }
1facf9fc 24413+
b752ccd1 24414+ return ret;
1facf9fc 24415+}
87a755f4
AM
24416+
24417+/* ---------------------------------------------------------------------- */
24418+
24419+#define au_warn_loopback_step 16
24420+static int au_warn_loopback_nelem = au_warn_loopback_step;
24421+static unsigned long *au_warn_loopback_array;
24422+
24423+void au_warn_loopback(struct super_block *h_sb)
24424+{
24425+ int i, new_nelem;
24426+ unsigned long *a, magic;
24427+ static DEFINE_SPINLOCK(spin);
24428+
24429+ magic = h_sb->s_magic;
24430+ spin_lock(&spin);
24431+ a = au_warn_loopback_array;
24432+ for (i = 0; i < au_warn_loopback_nelem && *a; i++)
24433+ if (a[i] == magic) {
24434+ spin_unlock(&spin);
24435+ return;
24436+ }
24437+
24438+ /* h_sb is new to us, print it */
24439+ if (i < au_warn_loopback_nelem) {
24440+ a[i] = magic;
24441+ goto pr;
24442+ }
24443+
24444+ /* expand the array */
24445+ new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
24446+ a = au_kzrealloc(au_warn_loopback_array,
24447+ au_warn_loopback_nelem * sizeof(unsigned long),
e2f27e51
AM
24448+ new_nelem * sizeof(unsigned long), GFP_ATOMIC,
24449+ /*may_shrink*/0);
87a755f4
AM
24450+ if (a) {
24451+ au_warn_loopback_nelem = new_nelem;
24452+ au_warn_loopback_array = a;
24453+ a[i] = magic;
24454+ goto pr;
24455+ }
24456+
24457+ spin_unlock(&spin);
24458+ AuWarn1("realloc failed, ignored\n");
24459+ return;
24460+
24461+pr:
24462+ spin_unlock(&spin);
0c3ec466
AM
24463+ pr_warn("you may want to try another patch for loopback file "
24464+ "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
87a755f4
AM
24465+}
24466+
24467+int au_loopback_init(void)
24468+{
24469+ int err;
24470+ struct super_block *sb __maybe_unused;
24471+
79b8bda9 24472+ BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(unsigned long));
87a755f4
AM
24473+
24474+ err = 0;
24475+ au_warn_loopback_array = kcalloc(au_warn_loopback_step,
24476+ sizeof(unsigned long), GFP_NOFS);
24477+ if (unlikely(!au_warn_loopback_array))
24478+ err = -ENOMEM;
24479+
24480+ return err;
24481+}
24482+
24483+void au_loopback_fin(void)
24484+{
79b8bda9
AM
24485+ if (backing_file_func)
24486+ symbol_put(loop_backing_file);
9f237c51 24487+ au_kfree_try_rcu(au_warn_loopback_array);
87a755f4 24488+}
7f207e10
AM
24489diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
24490--- /usr/share/empty/fs/aufs/loop.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 24491+++ linux/fs/aufs/loop.h 2018-08-12 23:43:05.460124736 +0200
062440b3
AM
24492@@ -0,0 +1,53 @@
24493+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 24494+/*
b00004a5 24495+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 24496+ *
24497+ * This program, aufs is free software; you can redistribute it and/or modify
24498+ * it under the terms of the GNU General Public License as published by
24499+ * the Free Software Foundation; either version 2 of the License, or
24500+ * (at your option) any later version.
dece6358
AM
24501+ *
24502+ * This program is distributed in the hope that it will be useful,
24503+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24504+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24505+ * GNU General Public License for more details.
24506+ *
24507+ * You should have received a copy of the GNU General Public License
523b37e3 24508+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24509+ */
24510+
24511+/*
24512+ * support for loopback mount as a branch
24513+ */
24514+
24515+#ifndef __AUFS_LOOP_H__
24516+#define __AUFS_LOOP_H__
24517+
24518+#ifdef __KERNEL__
24519+
dece6358
AM
24520+struct dentry;
24521+struct super_block;
1facf9fc 24522+
24523+#ifdef CONFIG_AUFS_BDEV_LOOP
392086de
AM
24524+/* drivers/block/loop.c */
24525+struct file *loop_backing_file(struct super_block *sb);
24526+
1facf9fc 24527+/* loop.c */
b752ccd1 24528+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
1facf9fc 24529+int au_test_loopback_kthread(void);
87a755f4
AM
24530+void au_warn_loopback(struct super_block *h_sb);
24531+
24532+int au_loopback_init(void);
24533+void au_loopback_fin(void);
1facf9fc 24534+#else
4a4d8108 24535+AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
b752ccd1 24536+ struct dentry *h_adding)
4a4d8108 24537+AuStubInt0(au_test_loopback_kthread, void)
87a755f4
AM
24538+AuStubVoid(au_warn_loopback, struct super_block *h_sb)
24539+
24540+AuStubInt0(au_loopback_init, void)
24541+AuStubVoid(au_loopback_fin, void)
1facf9fc 24542+#endif /* BLK_DEV_LOOP */
24543+
24544+#endif /* __KERNEL__ */
24545+#endif /* __AUFS_LOOP_H__ */
7f207e10
AM
24546diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
24547--- /usr/share/empty/fs/aufs/magic.mk 1970-01-01 01:00:00.000000000 +0100
b00004a5 24548+++ linux/fs/aufs/magic.mk 2018-06-04 09:08:09.188079511 +0200
2121bcd9
AM
24549@@ -0,0 +1,31 @@
24550+# SPDX-License-Identifier: GPL-2.0
1facf9fc 24551+
24552+# defined in ${srctree}/fs/fuse/inode.c
24553+# tristate
24554+ifdef CONFIG_FUSE_FS
24555+ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
24556+endif
24557+
1facf9fc 24558+# defined in ${srctree}/fs/xfs/xfs_sb.h
24559+# tristate
24560+ifdef CONFIG_XFS_FS
24561+ccflags-y += -DXFS_SB_MAGIC=0x58465342
24562+endif
24563+
24564+# defined in ${srctree}/fs/configfs/mount.c
24565+# tristate
24566+ifdef CONFIG_CONFIGFS_FS
24567+ccflags-y += -DCONFIGFS_MAGIC=0x62656570
24568+endif
24569+
1facf9fc 24570+# defined in ${srctree}/fs/ubifs/ubifs.h
24571+# tristate
24572+ifdef CONFIG_UBIFS_FS
24573+ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
24574+endif
4a4d8108
AM
24575+
24576+# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
24577+# tristate
24578+ifdef CONFIG_HFSPLUS_FS
24579+ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
24580+endif
7f207e10
AM
24581diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
24582--- /usr/share/empty/fs/aufs/Makefile 1970-01-01 01:00:00.000000000 +0100
b00004a5 24583+++ linux/fs/aufs/Makefile 2018-06-04 09:08:09.181412645 +0200
2121bcd9
AM
24584@@ -0,0 +1,46 @@
24585+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
24586+
24587+include ${src}/magic.mk
24588+ifeq (${CONFIG_AUFS_FS},m)
24589+include ${src}/conf.mk
24590+endif
24591+-include ${src}/priv_def.mk
24592+
24593+# cf. include/linux/kernel.h
24594+# enable pr_debug
24595+ccflags-y += -DDEBUG
f6c5ef8b
AM
24596+# sparse requires the full pathname
24597+ifdef M
523b37e3 24598+ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
f6c5ef8b 24599+else
523b37e3 24600+ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
f6c5ef8b 24601+endif
4a4d8108
AM
24602+
24603+obj-$(CONFIG_AUFS_FS) += aufs.o
24604+aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
24605+ wkq.o vfsub.o dcsub.o \
e49829fe 24606+ cpup.o whout.o wbr_policy.o \
4a4d8108
AM
24607+ dinfo.o dentry.o \
24608+ dynop.o \
24609+ finfo.o file.o f_op.o \
24610+ dir.o vdir.o \
24611+ iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
c2b27bf2 24612+ mvdown.o ioctl.o
4a4d8108
AM
24613+
24614+# all are boolean
e49829fe 24615+aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
4a4d8108
AM
24616+aufs-$(CONFIG_SYSFS) += sysfs.o
24617+aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
24618+aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
24619+aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
24620+aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
4a4d8108 24621+aufs-$(CONFIG_AUFS_EXPORT) += export.o
c1595e42
JR
24622+aufs-$(CONFIG_AUFS_XATTR) += xattr.o
24623+aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
8b6a4947 24624+aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
076b876e 24625+aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
4a4d8108
AM
24626+aufs-$(CONFIG_AUFS_POLL) += poll.o
24627+aufs-$(CONFIG_AUFS_RDU) += rdu.o
4a4d8108
AM
24628+aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
24629+aufs-$(CONFIG_AUFS_DEBUG) += debug.o
24630+aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
7f207e10
AM
24631diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
24632--- /usr/share/empty/fs/aufs/module.c 1970-01-01 01:00:00.000000000 +0100
9f237c51 24633+++ linux/fs/aufs/module.c 2018-12-27 13:19:17.711749485 +0100
062440b3 24634@@ -0,0 +1,273 @@
cd7a4cd9 24635+// SPDX-License-Identifier: GPL-2.0
1facf9fc 24636+/*
b00004a5 24637+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 24638+ *
24639+ * This program, aufs is free software; you can redistribute it and/or modify
24640+ * it under the terms of the GNU General Public License as published by
24641+ * the Free Software Foundation; either version 2 of the License, or
24642+ * (at your option) any later version.
dece6358
AM
24643+ *
24644+ * This program is distributed in the hope that it will be useful,
24645+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24646+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24647+ * GNU General Public License for more details.
24648+ *
24649+ * You should have received a copy of the GNU General Public License
523b37e3 24650+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24651+ */
24652+
24653+/*
24654+ * module global variables and operations
24655+ */
24656+
24657+#include <linux/module.h>
24658+#include <linux/seq_file.h>
24659+#include "aufs.h"
24660+
e2f27e51
AM
24661+/* shrinkable realloc */
24662+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
1facf9fc 24663+{
e2f27e51
AM
24664+ size_t sz;
24665+ int diff;
1facf9fc 24666+
e2f27e51
AM
24667+ sz = 0;
24668+ diff = -1;
24669+ if (p) {
24670+#if 0 /* unused */
24671+ if (!new_sz) {
9f237c51 24672+ au_kfree_rcu(p);
e2f27e51
AM
24673+ p = NULL;
24674+ goto out;
24675+ }
24676+#else
24677+ AuDebugOn(!new_sz);
24678+#endif
24679+ sz = ksize(p);
24680+ diff = au_kmidx_sub(sz, new_sz);
24681+ }
24682+ if (sz && !diff)
24683+ goto out;
24684+
24685+ if (sz < new_sz)
24686+ /* expand or SLOB */
24687+ p = krealloc(p, new_sz, gfp);
24688+ else if (new_sz < sz && may_shrink) {
24689+ /* shrink */
24690+ void *q;
24691+
24692+ q = kmalloc(new_sz, gfp);
24693+ if (q) {
24694+ if (p) {
24695+ memcpy(q, p, new_sz);
9f237c51 24696+ au_kfree_try_rcu(p);
e2f27e51
AM
24697+ }
24698+ p = q;
24699+ } else
24700+ p = NULL;
24701+ }
24702+
24703+out:
24704+ return p;
24705+}
24706+
24707+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24708+ int may_shrink)
24709+{
24710+ p = au_krealloc(p, new_sz, gfp, may_shrink);
24711+ if (p && new_sz > nused)
1facf9fc 24712+ memset(p + nused, 0, new_sz - nused);
24713+ return p;
24714+}
24715+
24716+/* ---------------------------------------------------------------------- */
1facf9fc 24717+/*
24718+ * aufs caches
24719+ */
1c60b727 24720+struct kmem_cache *au_cache[AuCache_Last];
5afbbe0d
AM
24721+
24722+static void au_cache_fin(void)
24723+{
24724+ int i;
24725+
24726+ /*
24727+ * Make sure all delayed rcu free inodes are flushed before we
24728+ * destroy cache.
24729+ */
24730+ rcu_barrier();
24731+
24732+ /* excluding AuCache_HNOTIFY */
24733+ BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
24734+ for (i = 0; i < AuCache_HNOTIFY; i++) {
1c60b727
AM
24735+ kmem_cache_destroy(au_cache[i]);
24736+ au_cache[i] = NULL;
5afbbe0d
AM
24737+ }
24738+}
24739+
1facf9fc 24740+static int __init au_cache_init(void)
24741+{
1c60b727
AM
24742+ au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
24743+ if (au_cache[AuCache_DINFO])
027c5e7a 24744+ /* SLAB_DESTROY_BY_RCU */
1c60b727 24745+ au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
f0c0a007 24746+ au_icntnr_init_once);
1c60b727
AM
24747+ if (au_cache[AuCache_ICNTNR])
24748+ au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
f0c0a007 24749+ au_fi_init_once);
1c60b727
AM
24750+ if (au_cache[AuCache_FINFO])
24751+ au_cache[AuCache_VDIR] = AuCache(au_vdir);
24752+ if (au_cache[AuCache_VDIR])
24753+ au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
24754+ if (au_cache[AuCache_DEHSTR])
1facf9fc 24755+ return 0;
24756+
5afbbe0d 24757+ au_cache_fin();
1facf9fc 24758+ return -ENOMEM;
24759+}
24760+
1facf9fc 24761+/* ---------------------------------------------------------------------- */
24762+
24763+int au_dir_roflags;
24764+
e49829fe 24765+#ifdef CONFIG_AUFS_SBILIST
1e00d052
AM
24766+/*
24767+ * iterate_supers_type() doesn't protect us from
24768+ * remounting (branch management)
24769+ */
8b6a4947 24770+struct hlist_bl_head au_sbilist;
e49829fe
JR
24771+#endif
24772+
1facf9fc 24773+/*
24774+ * functions for module interface.
24775+ */
24776+MODULE_LICENSE("GPL");
24777+/* MODULE_LICENSE("GPL v2"); */
dece6358 24778+MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
1facf9fc 24779+MODULE_DESCRIPTION(AUFS_NAME
24780+ " -- Advanced multi layered unification filesystem");
24781+MODULE_VERSION(AUFS_VERSION);
c06a8ce3 24782+MODULE_ALIAS_FS(AUFS_NAME);
1facf9fc 24783+
1facf9fc 24784+/* this module parameter has no meaning when SYSFS is disabled */
24785+int sysaufs_brs = 1;
24786+MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
cd7a4cd9 24787+module_param_named(brs, sysaufs_brs, int, 0444);
1facf9fc 24788+
076b876e 24789+/* this module parameter has no meaning when USER_NS is disabled */
8cdd5066 24790+bool au_userns;
076b876e 24791+MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
cd7a4cd9 24792+module_param_named(allow_userns, au_userns, bool, 0444);
076b876e 24793+
1facf9fc 24794+/* ---------------------------------------------------------------------- */
24795+
24796+static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
24797+
24798+int au_seq_path(struct seq_file *seq, struct path *path)
24799+{
79b8bda9
AM
24800+ int err;
24801+
24802+ err = seq_path(seq, path, au_esc_chars);
1c60b727 24803+ if (err >= 0)
79b8bda9 24804+ err = 0;
1c60b727 24805+ else
79b8bda9
AM
24806+ err = -ENOMEM;
24807+
24808+ return err;
1facf9fc 24809+}
24810+
24811+/* ---------------------------------------------------------------------- */
24812+
24813+static int __init aufs_init(void)
24814+{
24815+ int err, i;
24816+ char *p;
24817+
24818+ p = au_esc_chars;
24819+ for (i = 1; i <= ' '; i++)
24820+ *p++ = i;
24821+ *p++ = '\\';
24822+ *p++ = '\x7f';
24823+ *p = 0;
24824+
24825+ au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
24826+
b95c5147
AM
24827+ memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
24828+ for (i = 0; i < AuIop_Last; i++)
24829+ aufs_iop_nogetattr[i].getattr = NULL;
24830+
1c60b727 24831+ memset(au_cache, 0, sizeof(au_cache)); /* including hnotify */
f0c0a007 24832+
e49829fe 24833+ au_sbilist_init();
1facf9fc 24834+ sysaufs_brs_init();
24835+ au_debug_init();
4a4d8108 24836+ au_dy_init();
1facf9fc 24837+ err = sysaufs_init();
24838+ if (unlikely(err))
24839+ goto out;
062440b3 24840+ err = dbgaufs_init();
4f0767ce 24841+ if (unlikely(err))
953406b4 24842+ goto out_sysaufs;
062440b3
AM
24843+ err = au_procfs_init();
24844+ if (unlikely(err))
24845+ goto out_dbgaufs;
e49829fe
JR
24846+ err = au_wkq_init();
24847+ if (unlikely(err))
24848+ goto out_procfs;
87a755f4 24849+ err = au_loopback_init();
1facf9fc 24850+ if (unlikely(err))
24851+ goto out_wkq;
87a755f4
AM
24852+ err = au_hnotify_init();
24853+ if (unlikely(err))
24854+ goto out_loopback;
1facf9fc 24855+ err = au_sysrq_init();
24856+ if (unlikely(err))
24857+ goto out_hin;
24858+ err = au_cache_init();
24859+ if (unlikely(err))
24860+ goto out_sysrq;
076b876e
AM
24861+
24862+ aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
1facf9fc 24863+ err = register_filesystem(&aufs_fs_type);
24864+ if (unlikely(err))
24865+ goto out_cache;
076b876e 24866+
4a4d8108
AM
24867+ /* since we define pr_fmt, call printk directly */
24868+ printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
1facf9fc 24869+ goto out; /* success */
24870+
4f0767ce 24871+out_cache:
1facf9fc 24872+ au_cache_fin();
4f0767ce 24873+out_sysrq:
1facf9fc 24874+ au_sysrq_fin();
4f0767ce 24875+out_hin:
4a4d8108 24876+ au_hnotify_fin();
87a755f4
AM
24877+out_loopback:
24878+ au_loopback_fin();
4f0767ce 24879+out_wkq:
1facf9fc 24880+ au_wkq_fin();
e49829fe
JR
24881+out_procfs:
24882+ au_procfs_fin();
062440b3
AM
24883+out_dbgaufs:
24884+ dbgaufs_fin();
4f0767ce 24885+out_sysaufs:
1facf9fc 24886+ sysaufs_fin();
4a4d8108 24887+ au_dy_fin();
4f0767ce 24888+out:
1facf9fc 24889+ return err;
24890+}
24891+
24892+static void __exit aufs_exit(void)
24893+{
24894+ unregister_filesystem(&aufs_fs_type);
24895+ au_cache_fin();
24896+ au_sysrq_fin();
4a4d8108 24897+ au_hnotify_fin();
87a755f4 24898+ au_loopback_fin();
1facf9fc 24899+ au_wkq_fin();
e49829fe 24900+ au_procfs_fin();
062440b3 24901+ dbgaufs_fin();
1facf9fc 24902+ sysaufs_fin();
4a4d8108 24903+ au_dy_fin();
1facf9fc 24904+}
24905+
24906+module_init(aufs_init);
24907+module_exit(aufs_exit);
7f207e10
AM
24908diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
24909--- /usr/share/empty/fs/aufs/module.h 1970-01-01 01:00:00.000000000 +0100
9f237c51
AM
24910+++ linux/fs/aufs/module.h 2018-12-27 13:19:17.711749485 +0100
24911@@ -0,0 +1,166 @@
062440b3 24912+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 24913+/*
b00004a5 24914+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 24915+ *
24916+ * This program, aufs is free software; you can redistribute it and/or modify
24917+ * it under the terms of the GNU General Public License as published by
24918+ * the Free Software Foundation; either version 2 of the License, or
24919+ * (at your option) any later version.
dece6358
AM
24920+ *
24921+ * This program is distributed in the hope that it will be useful,
24922+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24923+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24924+ * GNU General Public License for more details.
24925+ *
24926+ * You should have received a copy of the GNU General Public License
523b37e3 24927+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24928+ */
24929+
24930+/*
24931+ * module initialization and module-global
24932+ */
24933+
24934+#ifndef __AUFS_MODULE_H__
24935+#define __AUFS_MODULE_H__
24936+
24937+#ifdef __KERNEL__
24938+
24939+#include <linux/slab.h>
9f237c51
AM
24940+#include "debug.h"
24941+#include "dentry.h"
24942+#include "dir.h"
24943+#include "file.h"
24944+#include "inode.h"
1facf9fc 24945+
dece6358
AM
24946+struct path;
24947+struct seq_file;
24948+
1facf9fc 24949+/* module parameters */
1facf9fc 24950+extern int sysaufs_brs;
8cdd5066 24951+extern bool au_userns;
1facf9fc 24952+
24953+/* ---------------------------------------------------------------------- */
24954+
24955+extern int au_dir_roflags;
24956+
e2f27e51
AM
24957+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
24958+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24959+ int may_shrink);
24960+
9f237c51
AM
24961+/*
24962+ * Comparing the size of the object with sizeof(struct rcu_head)
24963+ * case 1: object is always larger
24964+ * --> au_kfree_rcu() or au_kfree_do_rcu()
24965+ * case 2: object is always smaller
24966+ * --> au_kfree_small()
24967+ * case 3: object can be any size
24968+ * --> au_kfree_try_rcu()
24969+ */
24970+
24971+static inline void au_kfree_do_rcu(const void *p)
24972+{
24973+ struct {
24974+ struct rcu_head rcu;
24975+ } *a = (void *)p;
24976+
24977+ kfree_rcu(a, rcu);
24978+}
24979+
24980+#define au_kfree_rcu(_p) do { \
24981+ typeof(_p) p = (_p); \
24982+ BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head)); \
24983+ if (p) \
24984+ au_kfree_do_rcu(p); \
24985+ } while (0)
24986+
24987+#define au_kfree_do_sz_test(sz) (sz >= sizeof(struct rcu_head))
24988+#define au_kfree_sz_test(p) (p && au_kfree_do_sz_test(ksize(p)))
24989+
24990+static inline void au_kfree_try_rcu(const void *p)
24991+{
24992+ if (!p)
24993+ return;
24994+ if (au_kfree_sz_test(p))
24995+ au_kfree_do_rcu(p);
24996+ else
24997+ kfree(p);
24998+}
24999+
25000+static inline void au_kfree_small(const void *p)
25001+{
25002+ if (!p)
25003+ return;
25004+ AuDebugOn(au_kfree_sz_test(p));
25005+ kfree(p);
25006+}
25007+
e2f27e51
AM
25008+static inline int au_kmidx_sub(size_t sz, size_t new_sz)
25009+{
25010+#ifndef CONFIG_SLOB
25011+ return kmalloc_index(sz) - kmalloc_index(new_sz);
25012+#else
25013+ return -1; /* SLOB is untested */
25014+#endif
25015+}
25016+
1facf9fc 25017+int au_seq_path(struct seq_file *seq, struct path *path);
25018+
e49829fe
JR
25019+#ifdef CONFIG_PROC_FS
25020+/* procfs.c */
25021+int __init au_procfs_init(void);
25022+void au_procfs_fin(void);
25023+#else
25024+AuStubInt0(au_procfs_init, void);
25025+AuStubVoid(au_procfs_fin, void);
25026+#endif
25027+
4f0767ce
JR
25028+/* ---------------------------------------------------------------------- */
25029+
1c60b727 25030+/* kmem cache */
1facf9fc 25031+enum {
25032+ AuCache_DINFO,
25033+ AuCache_ICNTNR,
25034+ AuCache_FINFO,
25035+ AuCache_VDIR,
25036+ AuCache_DEHSTR,
7eafdf33 25037+ AuCache_HNOTIFY, /* must be last */
1facf9fc 25038+ AuCache_Last
25039+};
25040+
1c60b727 25041+extern struct kmem_cache *au_cache[AuCache_Last];
f0c0a007 25042+
4a4d8108
AM
25043+#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
25044+#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
25045+#define AuCacheCtor(type, ctor) \
25046+ kmem_cache_create(#type, sizeof(struct type), \
25047+ __alignof__(struct type), AuCacheFlags, ctor)
1facf9fc 25048+
9f237c51
AM
25049+#define AuCacheFuncs(name, index) \
25050+ static inline struct au_##name *au_cache_alloc_##name(void) \
25051+ { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
25052+ static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
25053+ { kmem_cache_free(au_cache[AuCache_##index], p); } \
25054+ \
25055+ static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
25056+ { void *p = rcu; \
25057+ p -= offsetof(struct au_##name, rcu); \
25058+ kmem_cache_free(au_cache[AuCache_##index], p); } \
25059+ static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
25060+ { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
25061+ call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); } \
25062+ \
25063+ static inline void au_cache_free_##name(struct au_##name *p) \
25064+ { /* au_cache_free_##name##_norcu(p); */ \
25065+ au_cache_free_##name##_rcu(p); }
1facf9fc 25066+
25067+AuCacheFuncs(dinfo, DINFO);
25068+AuCacheFuncs(icntnr, ICNTNR);
25069+AuCacheFuncs(finfo, FINFO);
25070+AuCacheFuncs(vdir, VDIR);
4a4d8108
AM
25071+AuCacheFuncs(vdir_dehstr, DEHSTR);
25072+#ifdef CONFIG_AUFS_HNOTIFY
25073+AuCacheFuncs(hnotify, HNOTIFY);
25074+#endif
1facf9fc 25075+
4a4d8108
AM
25076+#endif /* __KERNEL__ */
25077+#endif /* __AUFS_MODULE_H__ */
c2b27bf2
AM
25078diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
25079--- /usr/share/empty/fs/aufs/mvdown.c 1970-01-01 01:00:00.000000000 +0100
9f237c51 25080+++ linux/fs/aufs/mvdown.c 2018-12-27 13:19:17.711749485 +0100
062440b3 25081@@ -0,0 +1,705 @@
cd7a4cd9 25082+// SPDX-License-Identifier: GPL-2.0
c2b27bf2 25083+/*
b00004a5 25084+ * Copyright (C) 2011-2018 Junjiro R. Okajima
c2b27bf2
AM
25085+ *
25086+ * This program, aufs is free software; you can redistribute it and/or modify
25087+ * it under the terms of the GNU General Public License as published by
25088+ * the Free Software Foundation; either version 2 of the License, or
25089+ * (at your option) any later version.
25090+ *
25091+ * This program is distributed in the hope that it will be useful,
25092+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25093+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25094+ * GNU General Public License for more details.
25095+ *
25096+ * You should have received a copy of the GNU General Public License
523b37e3
AM
25097+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
25098+ */
25099+
25100+/*
25101+ * move-down, opposite of copy-up
c2b27bf2
AM
25102+ */
25103+
25104+#include "aufs.h"
25105+
c2b27bf2
AM
25106+struct au_mvd_args {
25107+ struct {
c2b27bf2
AM
25108+ struct super_block *h_sb;
25109+ struct dentry *h_parent;
25110+ struct au_hinode *hdir;
392086de 25111+ struct inode *h_dir, *h_inode;
c1595e42 25112+ struct au_pin pin;
c2b27bf2
AM
25113+ } info[AUFS_MVDOWN_NARRAY];
25114+
25115+ struct aufs_mvdown mvdown;
25116+ struct dentry *dentry, *parent;
25117+ struct inode *inode, *dir;
25118+ struct super_block *sb;
25119+ aufs_bindex_t bopq, bwh, bfound;
25120+ unsigned char rename_lock;
c2b27bf2
AM
25121+};
25122+
392086de 25123+#define mvd_errno mvdown.au_errno
076b876e
AM
25124+#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
25125+#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
25126+#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
25127+#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
c2b27bf2 25128+
392086de
AM
25129+#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
25130+#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
25131+#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
25132+#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
25133+#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
c1595e42 25134+#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
392086de
AM
25135+
25136+#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
25137+#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
25138+#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
25139+#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
25140+#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
c1595e42 25141+#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
c2b27bf2
AM
25142+
25143+#define AU_MVD_PR(flag, ...) do { \
25144+ if (flag) \
25145+ pr_err(__VA_ARGS__); \
25146+ } while (0)
25147+
076b876e
AM
25148+static int find_lower_writable(struct au_mvd_args *a)
25149+{
25150+ struct super_block *sb;
5afbbe0d 25151+ aufs_bindex_t bindex, bbot;
076b876e
AM
25152+ struct au_branch *br;
25153+
25154+ sb = a->sb;
25155+ bindex = a->mvd_bsrc;
5afbbe0d 25156+ bbot = au_sbbot(sb);
076b876e 25157+ if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
5afbbe0d 25158+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
25159+ br = au_sbr(sb, bindex);
25160+ if (au_br_fhsm(br->br_perm)
8b6a4947 25161+ && !sb_rdonly(au_br_sb(br)))
076b876e
AM
25162+ return bindex;
25163+ }
25164+ else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
5afbbe0d 25165+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
25166+ br = au_sbr(sb, bindex);
25167+ if (!au_br_rdonly(br))
25168+ return bindex;
25169+ }
25170+ else
5afbbe0d 25171+ for (bindex++; bindex <= bbot; bindex++) {
076b876e 25172+ br = au_sbr(sb, bindex);
8b6a4947 25173+ if (!sb_rdonly(au_br_sb(br))) {
076b876e
AM
25174+ if (au_br_rdonly(br))
25175+ a->mvdown.flags
25176+ |= AUFS_MVDOWN_ROLOWER_R;
25177+ return bindex;
25178+ }
25179+ }
25180+
25181+ return -1;
25182+}
25183+
c2b27bf2 25184+/* make the parent dir on bdst */
392086de 25185+static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25186+{
25187+ int err;
25188+
25189+ err = 0;
25190+ a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
25191+ a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
25192+ a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
25193+ a->mvd_h_dst_parent = NULL;
5afbbe0d 25194+ if (au_dbbot(a->parent) >= a->mvd_bdst)
c2b27bf2
AM
25195+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25196+ if (!a->mvd_h_dst_parent) {
25197+ err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
25198+ if (unlikely(err)) {
392086de 25199+ AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
c2b27bf2
AM
25200+ goto out;
25201+ }
25202+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25203+ }
25204+
25205+out:
25206+ AuTraceErr(err);
25207+ return err;
25208+}
25209+
25210+/* lock them all */
392086de 25211+static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25212+{
25213+ int err;
25214+ struct dentry *h_trap;
25215+
25216+ a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
25217+ a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
c1595e42
JR
25218+ err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
25219+ au_opt_udba(a->sb),
25220+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25221+ AuTraceErr(err);
25222+ if (unlikely(err)) {
25223+ AU_MVD_PR(dmsg, "pin_dst failed\n");
25224+ goto out;
25225+ }
25226+
c2b27bf2
AM
25227+ if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
25228+ a->rename_lock = 0;
c1595e42
JR
25229+ au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25230+ AuLsc_DI_PARENT, AuLsc_I_PARENT3,
25231+ au_opt_udba(a->sb),
25232+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25233+ err = au_do_pin(&a->mvd_pin_src);
25234+ AuTraceErr(err);
5527c038 25235+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
25236+ if (unlikely(err)) {
25237+ AU_MVD_PR(dmsg, "pin_src failed\n");
25238+ goto out_dst;
25239+ }
25240+ goto out; /* success */
c2b27bf2
AM
25241+ }
25242+
c2b27bf2 25243+ a->rename_lock = 1;
c1595e42
JR
25244+ au_pin_hdir_unlock(&a->mvd_pin_dst);
25245+ err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25246+ au_opt_udba(a->sb),
25247+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25248+ AuTraceErr(err);
5527c038 25249+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
25250+ if (unlikely(err)) {
25251+ AU_MVD_PR(dmsg, "pin_src failed\n");
25252+ au_pin_hdir_lock(&a->mvd_pin_dst);
25253+ goto out_dst;
25254+ }
25255+ au_pin_hdir_unlock(&a->mvd_pin_src);
c2b27bf2
AM
25256+ h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25257+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
25258+ if (h_trap) {
25259+ err = (h_trap != a->mvd_h_src_parent);
25260+ if (err)
25261+ err = (h_trap != a->mvd_h_dst_parent);
25262+ }
25263+ BUG_ON(err); /* it should never happen */
c1595e42
JR
25264+ if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
25265+ err = -EBUSY;
25266+ AuTraceErr(err);
25267+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25268+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
25269+ au_pin_hdir_lock(&a->mvd_pin_src);
25270+ au_unpin(&a->mvd_pin_src);
25271+ au_pin_hdir_lock(&a->mvd_pin_dst);
25272+ goto out_dst;
25273+ }
25274+ goto out; /* success */
c2b27bf2 25275+
c1595e42
JR
25276+out_dst:
25277+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
25278+out:
25279+ AuTraceErr(err);
25280+ return err;
25281+}
25282+
392086de 25283+static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2 25284+{
c1595e42
JR
25285+ if (!a->rename_lock)
25286+ au_unpin(&a->mvd_pin_src);
25287+ else {
c2b27bf2
AM
25288+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25289+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
c1595e42
JR
25290+ au_pin_hdir_lock(&a->mvd_pin_src);
25291+ au_unpin(&a->mvd_pin_src);
25292+ au_pin_hdir_lock(&a->mvd_pin_dst);
25293+ }
25294+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
25295+}
25296+
25297+/* copy-down the file */
392086de 25298+static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25299+{
25300+ int err;
25301+ struct au_cp_generic cpg = {
25302+ .dentry = a->dentry,
25303+ .bdst = a->mvd_bdst,
25304+ .bsrc = a->mvd_bsrc,
25305+ .len = -1,
c1595e42 25306+ .pin = &a->mvd_pin_dst,
c2b27bf2
AM
25307+ .flags = AuCpup_DTIME | AuCpup_HOPEN
25308+ };
25309+
25310+ AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
392086de
AM
25311+ if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25312+ au_fset_cpup(cpg.flags, OVERWRITE);
25313+ if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
25314+ au_fset_cpup(cpg.flags, RWDST);
c2b27bf2
AM
25315+ err = au_sio_cpdown_simple(&cpg);
25316+ if (unlikely(err))
392086de 25317+ AU_MVD_PR(dmsg, "cpdown failed\n");
c2b27bf2
AM
25318+
25319+ AuTraceErr(err);
25320+ return err;
25321+}
25322+
25323+/*
25324+ * unlink the whiteout on bdst if exist which may be created by UDBA while we
25325+ * were sleeping
25326+ */
392086de 25327+static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25328+{
25329+ int err;
25330+ struct path h_path;
25331+ struct au_branch *br;
523b37e3 25332+ struct inode *delegated;
c2b27bf2
AM
25333+
25334+ br = au_sbr(a->sb, a->mvd_bdst);
25335+ h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
25336+ err = PTR_ERR(h_path.dentry);
25337+ if (IS_ERR(h_path.dentry)) {
392086de 25338+ AU_MVD_PR(dmsg, "wh_lkup failed\n");
c2b27bf2
AM
25339+ goto out;
25340+ }
25341+
25342+ err = 0;
5527c038 25343+ if (d_is_positive(h_path.dentry)) {
c2b27bf2 25344+ h_path.mnt = au_br_mnt(br);
523b37e3 25345+ delegated = NULL;
5527c038 25346+ err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
523b37e3
AM
25347+ &delegated, /*force*/0);
25348+ if (unlikely(err == -EWOULDBLOCK)) {
25349+ pr_warn("cannot retry for NFSv4 delegation"
25350+ " for an internal unlink\n");
25351+ iput(delegated);
25352+ }
c2b27bf2 25353+ if (unlikely(err))
392086de 25354+ AU_MVD_PR(dmsg, "wh_unlink failed\n");
c2b27bf2
AM
25355+ }
25356+ dput(h_path.dentry);
25357+
25358+out:
25359+ AuTraceErr(err);
25360+ return err;
25361+}
25362+
25363+/*
25364+ * unlink the topmost h_dentry
c2b27bf2 25365+ */
392086de 25366+static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25367+{
25368+ int err;
25369+ struct path h_path;
523b37e3 25370+ struct inode *delegated;
c2b27bf2
AM
25371+
25372+ h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
25373+ h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
523b37e3
AM
25374+ delegated = NULL;
25375+ err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
25376+ if (unlikely(err == -EWOULDBLOCK)) {
25377+ pr_warn("cannot retry for NFSv4 delegation"
25378+ " for an internal unlink\n");
25379+ iput(delegated);
25380+ }
c2b27bf2 25381+ if (unlikely(err))
392086de 25382+ AU_MVD_PR(dmsg, "unlink failed\n");
c2b27bf2
AM
25383+
25384+ AuTraceErr(err);
25385+ return err;
25386+}
25387+
076b876e
AM
25388+/* Since mvdown succeeded, we ignore an error of this function */
25389+static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
25390+{
25391+ int err;
25392+ struct au_branch *br;
25393+
25394+ a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
25395+ br = au_sbr(a->sb, a->mvd_bsrc);
25396+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
25397+ if (!err) {
25398+ br = au_sbr(a->sb, a->mvd_bdst);
25399+ a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
25400+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
25401+ }
25402+ if (!err)
25403+ a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
25404+ else
25405+ AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
25406+}
25407+
c2b27bf2
AM
25408+/*
25409+ * copy-down the file and unlink the bsrc file.
25410+ * - unlink the bdst whout if exist
25411+ * - copy-down the file (with whtmp name and rename)
25412+ * - unlink the bsrc file
25413+ */
392086de 25414+static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25415+{
25416+ int err;
25417+
392086de 25418+ err = au_do_mkdir(dmsg, a);
c2b27bf2 25419+ if (!err)
392086de 25420+ err = au_do_lock(dmsg, a);
c2b27bf2
AM
25421+ if (unlikely(err))
25422+ goto out;
25423+
25424+ /*
25425+ * do not revert the activities we made on bdst since they should be
25426+ * harmless in aufs.
25427+ */
25428+
392086de 25429+ err = au_do_cpdown(dmsg, a);
c2b27bf2 25430+ if (!err)
392086de
AM
25431+ err = au_do_unlink_wh(dmsg, a);
25432+ if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
25433+ err = au_do_unlink(dmsg, a);
c2b27bf2
AM
25434+ if (unlikely(err))
25435+ goto out_unlock;
25436+
c1595e42
JR
25437+ AuDbg("%pd2, 0x%x, %d --> %d\n",
25438+ a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
076b876e
AM
25439+ if (find_lower_writable(a) < 0)
25440+ a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
25441+
25442+ if (a->mvdown.flags & AUFS_MVDOWN_STFS)
25443+ au_do_stfs(dmsg, a);
25444+
c2b27bf2 25445+ /* maintain internal array */
392086de
AM
25446+ if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
25447+ au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
5afbbe0d 25448+ au_set_dbtop(a->dentry, a->mvd_bdst);
392086de 25449+ au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
5afbbe0d 25450+ au_set_ibtop(a->inode, a->mvd_bdst);
79b8bda9
AM
25451+ } else {
25452+ /* hide the lower */
25453+ au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
5afbbe0d 25454+ au_set_dbbot(a->dentry, a->mvd_bsrc);
79b8bda9 25455+ au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
5afbbe0d 25456+ au_set_ibbot(a->inode, a->mvd_bsrc);
392086de 25457+ }
5afbbe0d
AM
25458+ if (au_dbbot(a->dentry) < a->mvd_bdst)
25459+ au_set_dbbot(a->dentry, a->mvd_bdst);
25460+ if (au_ibbot(a->inode) < a->mvd_bdst)
25461+ au_set_ibbot(a->inode, a->mvd_bdst);
c2b27bf2
AM
25462+
25463+out_unlock:
392086de 25464+ au_do_unlock(dmsg, a);
c2b27bf2
AM
25465+out:
25466+ AuTraceErr(err);
25467+ return err;
25468+}
25469+
25470+/* ---------------------------------------------------------------------- */
25471+
c2b27bf2 25472+/* make sure the file is idle */
392086de 25473+static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25474+{
25475+ int err, plinked;
c2b27bf2
AM
25476+
25477+ err = 0;
c2b27bf2 25478+ plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
5afbbe0d 25479+ if (au_dbtop(a->dentry) == a->mvd_bsrc
c1595e42 25480+ && au_dcount(a->dentry) == 1
c2b27bf2 25481+ && atomic_read(&a->inode->i_count) == 1
392086de 25482+ /* && a->mvd_h_src_inode->i_nlink == 1 */
c2b27bf2
AM
25483+ && (!plinked || !au_plink_test(a->inode))
25484+ && a->inode->i_nlink == 1)
25485+ goto out;
25486+
25487+ err = -EBUSY;
392086de 25488+ AU_MVD_PR(dmsg,
c1595e42 25489+ "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
5afbbe0d 25490+ a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
c2b27bf2 25491+ atomic_read(&a->inode->i_count), a->inode->i_nlink,
392086de 25492+ a->mvd_h_src_inode->i_nlink,
c2b27bf2
AM
25493+ plinked, plinked ? au_plink_test(a->inode) : 0);
25494+
25495+out:
25496+ AuTraceErr(err);
25497+ return err;
25498+}
25499+
25500+/* make sure the parent dir is fine */
392086de 25501+static int au_mvd_args_parent(const unsigned char dmsg,
c2b27bf2
AM
25502+ struct au_mvd_args *a)
25503+{
25504+ int err;
25505+ aufs_bindex_t bindex;
25506+
25507+ err = 0;
25508+ if (unlikely(au_alive_dir(a->parent))) {
25509+ err = -ENOENT;
392086de 25510+ AU_MVD_PR(dmsg, "parent dir is dead\n");
c2b27bf2
AM
25511+ goto out;
25512+ }
25513+
25514+ a->bopq = au_dbdiropq(a->parent);
25515+ bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
25516+ AuDbg("b%d\n", bindex);
25517+ if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
25518+ || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
25519+ err = -EINVAL;
392086de
AM
25520+ a->mvd_errno = EAU_MVDOWN_OPAQUE;
25521+ AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
c2b27bf2
AM
25522+ a->bopq, a->mvd_bdst);
25523+ }
25524+
25525+out:
25526+ AuTraceErr(err);
25527+ return err;
25528+}
25529+
392086de 25530+static int au_mvd_args_intermediate(const unsigned char dmsg,
c2b27bf2
AM
25531+ struct au_mvd_args *a)
25532+{
25533+ int err;
25534+ struct au_dinfo *dinfo, *tmp;
25535+
25536+ /* lookup the next lower positive entry */
25537+ err = -ENOMEM;
25538+ tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
25539+ if (unlikely(!tmp))
25540+ goto out;
25541+
25542+ a->bfound = -1;
25543+ a->bwh = -1;
25544+ dinfo = au_di(a->dentry);
25545+ au_di_cp(tmp, dinfo);
25546+ au_di_swap(tmp, dinfo);
25547+
25548+ /* returns the number of positive dentries */
5afbbe0d
AM
25549+ err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
25550+ /* AuLkup_IGNORE_PERM */ 0);
c2b27bf2
AM
25551+ if (!err)
25552+ a->bwh = au_dbwh(a->dentry);
25553+ else if (err > 0)
5afbbe0d 25554+ a->bfound = au_dbtop(a->dentry);
c2b27bf2
AM
25555+
25556+ au_di_swap(tmp, dinfo);
25557+ au_rw_write_unlock(&tmp->di_rwsem);
25558+ au_di_free(tmp);
25559+ if (unlikely(err < 0))
392086de 25560+ AU_MVD_PR(dmsg, "failed look-up lower\n");
c2b27bf2
AM
25561+
25562+ /*
25563+ * here, we have these cases.
25564+ * bfound == -1
25565+ * no positive dentry under bsrc. there are more sub-cases.
25566+ * bwh < 0
25567+ * there no whiteout, we can safely move-down.
25568+ * bwh <= bsrc
25569+ * impossible
25570+ * bsrc < bwh && bwh < bdst
25571+ * there is a whiteout on RO branch. cannot proceed.
25572+ * bwh == bdst
25573+ * there is a whiteout on the RW target branch. it should
25574+ * be removed.
25575+ * bdst < bwh
25576+ * there is a whiteout somewhere unrelated branch.
25577+ * -1 < bfound && bfound <= bsrc
25578+ * impossible.
25579+ * bfound < bdst
25580+ * found, but it is on RO branch between bsrc and bdst. cannot
25581+ * proceed.
25582+ * bfound == bdst
25583+ * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
25584+ * error.
25585+ * bdst < bfound
25586+ * found, after we create the file on bdst, it will be hidden.
25587+ */
25588+
25589+ AuDebugOn(a->bfound == -1
25590+ && a->bwh != -1
25591+ && a->bwh <= a->mvd_bsrc);
25592+ AuDebugOn(-1 < a->bfound
25593+ && a->bfound <= a->mvd_bsrc);
25594+
25595+ err = -EINVAL;
25596+ if (a->bfound == -1
25597+ && a->mvd_bsrc < a->bwh
25598+ && a->bwh != -1
25599+ && a->bwh < a->mvd_bdst) {
392086de
AM
25600+ a->mvd_errno = EAU_MVDOWN_WHITEOUT;
25601+ AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
c2b27bf2
AM
25602+ a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
25603+ goto out;
25604+ } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
392086de
AM
25605+ a->mvd_errno = EAU_MVDOWN_UPPER;
25606+ AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
c2b27bf2
AM
25607+ a->mvd_bdst, a->bfound);
25608+ goto out;
25609+ }
25610+
25611+ err = 0; /* success */
25612+
25613+out:
25614+ AuTraceErr(err);
25615+ return err;
25616+}
25617+
392086de 25618+static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25619+{
25620+ int err;
25621+
392086de
AM
25622+ err = 0;
25623+ if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25624+ && a->bfound == a->mvd_bdst)
25625+ err = -EEXIST;
c2b27bf2
AM
25626+ AuTraceErr(err);
25627+ return err;
25628+}
25629+
392086de 25630+static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25631+{
25632+ int err;
25633+ struct au_branch *br;
25634+
25635+ err = -EISDIR;
25636+ if (unlikely(S_ISDIR(a->inode->i_mode)))
25637+ goto out;
25638+
25639+ err = -EINVAL;
392086de 25640+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
5afbbe0d 25641+ a->mvd_bsrc = au_ibtop(a->inode);
392086de
AM
25642+ else {
25643+ a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
25644+ if (unlikely(a->mvd_bsrc < 0
5afbbe0d
AM
25645+ || (a->mvd_bsrc < au_dbtop(a->dentry)
25646+ || au_dbbot(a->dentry) < a->mvd_bsrc
392086de 25647+ || !au_h_dptr(a->dentry, a->mvd_bsrc))
5afbbe0d
AM
25648+ || (a->mvd_bsrc < au_ibtop(a->inode)
25649+ || au_ibbot(a->inode) < a->mvd_bsrc
392086de
AM
25650+ || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
25651+ a->mvd_errno = EAU_MVDOWN_NOUPPER;
25652+ AU_MVD_PR(dmsg, "no upper\n");
25653+ goto out;
25654+ }
25655+ }
5afbbe0d 25656+ if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
392086de
AM
25657+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25658+ AU_MVD_PR(dmsg, "on the bottom\n");
c2b27bf2
AM
25659+ goto out;
25660+ }
392086de 25661+ a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
c2b27bf2
AM
25662+ br = au_sbr(a->sb, a->mvd_bsrc);
25663+ err = au_br_rdonly(br);
392086de
AM
25664+ if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
25665+ if (unlikely(err))
25666+ goto out;
25667+ } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
25668+ || IS_APPEND(a->mvd_h_src_inode))) {
25669+ if (err)
25670+ a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
25671+ /* go on */
25672+ } else
c2b27bf2
AM
25673+ goto out;
25674+
25675+ err = -EINVAL;
392086de
AM
25676+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
25677+ a->mvd_bdst = find_lower_writable(a);
25678+ if (unlikely(a->mvd_bdst < 0)) {
25679+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25680+ AU_MVD_PR(dmsg, "no writable lower branch\n");
25681+ goto out;
25682+ }
25683+ } else {
25684+ a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
25685+ if (unlikely(a->mvd_bdst < 0
5afbbe0d 25686+ || au_sbbot(a->sb) < a->mvd_bdst)) {
392086de
AM
25687+ a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
25688+ AU_MVD_PR(dmsg, "no lower brid\n");
25689+ goto out;
25690+ }
c2b27bf2
AM
25691+ }
25692+
392086de 25693+ err = au_mvd_args_busy(dmsg, a);
c2b27bf2 25694+ if (!err)
392086de 25695+ err = au_mvd_args_parent(dmsg, a);
c2b27bf2 25696+ if (!err)
392086de 25697+ err = au_mvd_args_intermediate(dmsg, a);
c2b27bf2 25698+ if (!err)
392086de 25699+ err = au_mvd_args_exist(dmsg, a);
c2b27bf2
AM
25700+ if (!err)
25701+ AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
25702+
25703+out:
25704+ AuTraceErr(err);
25705+ return err;
25706+}
25707+
25708+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
25709+{
392086de
AM
25710+ int err, e;
25711+ unsigned char dmsg;
25712+ struct au_mvd_args *args;
79b8bda9 25713+ struct inode *inode;
c2b27bf2 25714+
79b8bda9 25715+ inode = d_inode(dentry);
c2b27bf2
AM
25716+ err = -EPERM;
25717+ if (unlikely(!capable(CAP_SYS_ADMIN)))
25718+ goto out;
25719+
392086de
AM
25720+ err = -ENOMEM;
25721+ args = kmalloc(sizeof(*args), GFP_NOFS);
25722+ if (unlikely(!args))
25723+ goto out;
25724+
25725+ err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
25726+ if (!err)
25727+ err = !access_ok(VERIFY_WRITE, uarg, sizeof(*uarg));
c2b27bf2
AM
25728+ if (unlikely(err)) {
25729+ err = -EFAULT;
392086de
AM
25730+ AuTraceErr(err);
25731+ goto out_free;
c2b27bf2 25732+ }
392086de
AM
25733+ AuDbg("flags 0x%x\n", args->mvdown.flags);
25734+ args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
25735+ args->mvdown.au_errno = 0;
25736+ args->dentry = dentry;
79b8bda9 25737+ args->inode = inode;
392086de 25738+ args->sb = dentry->d_sb;
c2b27bf2 25739+
392086de
AM
25740+ err = -ENOENT;
25741+ dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
25742+ args->parent = dget_parent(dentry);
5527c038 25743+ args->dir = d_inode(args->parent);
febd17d6 25744+ inode_lock_nested(args->dir, I_MUTEX_PARENT);
392086de
AM
25745+ dput(args->parent);
25746+ if (unlikely(args->parent != dentry->d_parent)) {
25747+ AU_MVD_PR(dmsg, "parent dir is moved\n");
c2b27bf2
AM
25748+ goto out_dir;
25749+ }
25750+
febd17d6 25751+ inode_lock_nested(inode, I_MUTEX_CHILD);
b95c5147 25752+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
c2b27bf2
AM
25753+ if (unlikely(err))
25754+ goto out_inode;
25755+
392086de
AM
25756+ di_write_lock_parent(args->parent);
25757+ err = au_mvd_args(dmsg, args);
c2b27bf2
AM
25758+ if (unlikely(err))
25759+ goto out_parent;
25760+
392086de 25761+ err = au_do_mvdown(dmsg, args);
c2b27bf2
AM
25762+ if (unlikely(err))
25763+ goto out_parent;
c2b27bf2 25764+
392086de 25765+ au_cpup_attr_timesizes(args->dir);
79b8bda9
AM
25766+ au_cpup_attr_timesizes(inode);
25767+ if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
25768+ au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
c2b27bf2
AM
25769+ /* au_digen_dec(dentry); */
25770+
25771+out_parent:
392086de 25772+ di_write_unlock(args->parent);
c2b27bf2
AM
25773+ aufs_read_unlock(dentry, AuLock_DW);
25774+out_inode:
febd17d6 25775+ inode_unlock(inode);
c2b27bf2 25776+out_dir:
febd17d6 25777+ inode_unlock(args->dir);
392086de
AM
25778+out_free:
25779+ e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
25780+ if (unlikely(e))
25781+ err = -EFAULT;
9f237c51 25782+ au_kfree_rcu(args);
c2b27bf2
AM
25783+out:
25784+ AuTraceErr(err);
25785+ return err;
25786+}
25787diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
25788--- /usr/share/empty/fs/aufs/opts.c 1970-01-01 01:00:00.000000000 +0100
9f237c51 25789+++ linux/fs/aufs/opts.c 2018-12-27 13:19:17.711749485 +0100
acd2b654 25790@@ -0,0 +1,1877 @@
cd7a4cd9 25791+// SPDX-License-Identifier: GPL-2.0
1facf9fc 25792+/*
b00004a5 25793+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 25794+ *
25795+ * This program, aufs is free software; you can redistribute it and/or modify
25796+ * it under the terms of the GNU General Public License as published by
25797+ * the Free Software Foundation; either version 2 of the License, or
25798+ * (at your option) any later version.
dece6358
AM
25799+ *
25800+ * This program is distributed in the hope that it will be useful,
25801+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25802+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25803+ * GNU General Public License for more details.
25804+ *
25805+ * You should have received a copy of the GNU General Public License
523b37e3 25806+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 25807+ */
25808+
25809+/*
25810+ * mount options/flags
25811+ */
25812+
dece6358 25813+#include <linux/namei.h>
1facf9fc 25814+#include <linux/types.h> /* a distribution requires */
25815+#include <linux/parser.h>
25816+#include "aufs.h"
25817+
25818+/* ---------------------------------------------------------------------- */
25819+
25820+enum {
25821+ Opt_br,
7e9cd9fe
AM
25822+ Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
25823+ Opt_idel, Opt_imod,
25824+ Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
dece6358 25825+ Opt_rdblk_def, Opt_rdhash_def,
7e9cd9fe 25826+ Opt_xino, Opt_noxino,
1facf9fc 25827+ Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
25828+ Opt_trunc_xino_path, Opt_itrunc_xino,
25829+ Opt_trunc_xib, Opt_notrunc_xib,
dece6358 25830+ Opt_shwh, Opt_noshwh,
1facf9fc 25831+ Opt_plink, Opt_noplink, Opt_list_plink,
25832+ Opt_udba,
4a4d8108 25833+ Opt_dio, Opt_nodio,
1facf9fc 25834+ Opt_diropq_a, Opt_diropq_w,
25835+ Opt_warn_perm, Opt_nowarn_perm,
25836+ Opt_wbr_copyup, Opt_wbr_create,
076b876e 25837+ Opt_fhsm_sec,
1facf9fc 25838+ Opt_verbose, Opt_noverbose,
25839+ Opt_sum, Opt_nosum, Opt_wsum,
076b876e 25840+ Opt_dirperm1, Opt_nodirperm1,
8b6a4947 25841+ Opt_dirren, Opt_nodirren,
c1595e42 25842+ Opt_acl, Opt_noacl,
1facf9fc 25843+ Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
25844+};
25845+
25846+static match_table_t options = {
25847+ {Opt_br, "br=%s"},
25848+ {Opt_br, "br:%s"},
25849+
25850+ {Opt_add, "add=%d:%s"},
25851+ {Opt_add, "add:%d:%s"},
25852+ {Opt_add, "ins=%d:%s"},
25853+ {Opt_add, "ins:%d:%s"},
25854+ {Opt_append, "append=%s"},
25855+ {Opt_append, "append:%s"},
25856+ {Opt_prepend, "prepend=%s"},
25857+ {Opt_prepend, "prepend:%s"},
25858+
25859+ {Opt_del, "del=%s"},
25860+ {Opt_del, "del:%s"},
25861+ /* {Opt_idel, "idel:%d"}, */
25862+ {Opt_mod, "mod=%s"},
25863+ {Opt_mod, "mod:%s"},
25864+ /* {Opt_imod, "imod:%d:%s"}, */
25865+
25866+ {Opt_dirwh, "dirwh=%d"},
25867+
25868+ {Opt_xino, "xino=%s"},
25869+ {Opt_noxino, "noxino"},
25870+ {Opt_trunc_xino, "trunc_xino"},
25871+ {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
25872+ {Opt_notrunc_xino, "notrunc_xino"},
25873+ {Opt_trunc_xino_path, "trunc_xino=%s"},
25874+ {Opt_itrunc_xino, "itrunc_xino=%d"},
25875+ /* {Opt_zxino, "zxino=%s"}, */
25876+ {Opt_trunc_xib, "trunc_xib"},
25877+ {Opt_notrunc_xib, "notrunc_xib"},
25878+
e49829fe 25879+#ifdef CONFIG_PROC_FS
1facf9fc 25880+ {Opt_plink, "plink"},
e49829fe
JR
25881+#else
25882+ {Opt_ignore_silent, "plink"},
25883+#endif
25884+
1facf9fc 25885+ {Opt_noplink, "noplink"},
e49829fe 25886+
1facf9fc 25887+#ifdef CONFIG_AUFS_DEBUG
25888+ {Opt_list_plink, "list_plink"},
25889+#endif
25890+
25891+ {Opt_udba, "udba=%s"},
25892+
4a4d8108
AM
25893+ {Opt_dio, "dio"},
25894+ {Opt_nodio, "nodio"},
25895+
8b6a4947
AM
25896+#ifdef CONFIG_AUFS_DIRREN
25897+ {Opt_dirren, "dirren"},
25898+ {Opt_nodirren, "nodirren"},
25899+#else
25900+ {Opt_ignore, "dirren"},
25901+ {Opt_ignore_silent, "nodirren"},
25902+#endif
25903+
076b876e
AM
25904+#ifdef CONFIG_AUFS_FHSM
25905+ {Opt_fhsm_sec, "fhsm_sec=%d"},
25906+#else
8b6a4947 25907+ {Opt_ignore, "fhsm_sec=%d"},
076b876e
AM
25908+#endif
25909+
1facf9fc 25910+ {Opt_diropq_a, "diropq=always"},
25911+ {Opt_diropq_a, "diropq=a"},
25912+ {Opt_diropq_w, "diropq=whiteouted"},
25913+ {Opt_diropq_w, "diropq=w"},
25914+
25915+ {Opt_warn_perm, "warn_perm"},
25916+ {Opt_nowarn_perm, "nowarn_perm"},
25917+
25918+ /* keep them temporary */
1facf9fc 25919+ {Opt_ignore_silent, "nodlgt"},
8b6a4947 25920+ {Opt_ignore, "clean_plink"},
1facf9fc 25921+
dece6358
AM
25922+#ifdef CONFIG_AUFS_SHWH
25923+ {Opt_shwh, "shwh"},
25924+#endif
25925+ {Opt_noshwh, "noshwh"},
25926+
076b876e
AM
25927+ {Opt_dirperm1, "dirperm1"},
25928+ {Opt_nodirperm1, "nodirperm1"},
25929+
1facf9fc 25930+ {Opt_verbose, "verbose"},
25931+ {Opt_verbose, "v"},
25932+ {Opt_noverbose, "noverbose"},
25933+ {Opt_noverbose, "quiet"},
25934+ {Opt_noverbose, "q"},
25935+ {Opt_noverbose, "silent"},
25936+
25937+ {Opt_sum, "sum"},
25938+ {Opt_nosum, "nosum"},
25939+ {Opt_wsum, "wsum"},
25940+
25941+ {Opt_rdcache, "rdcache=%d"},
25942+ {Opt_rdblk, "rdblk=%d"},
dece6358 25943+ {Opt_rdblk_def, "rdblk=def"},
1facf9fc 25944+ {Opt_rdhash, "rdhash=%d"},
dece6358 25945+ {Opt_rdhash_def, "rdhash=def"},
1facf9fc 25946+
25947+ {Opt_wbr_create, "create=%s"},
25948+ {Opt_wbr_create, "create_policy=%s"},
25949+ {Opt_wbr_copyup, "cpup=%s"},
25950+ {Opt_wbr_copyup, "copyup=%s"},
25951+ {Opt_wbr_copyup, "copyup_policy=%s"},
25952+
c1595e42
JR
25953+ /* generic VFS flag */
25954+#ifdef CONFIG_FS_POSIX_ACL
25955+ {Opt_acl, "acl"},
25956+ {Opt_noacl, "noacl"},
25957+#else
8b6a4947 25958+ {Opt_ignore, "acl"},
c1595e42
JR
25959+ {Opt_ignore_silent, "noacl"},
25960+#endif
25961+
1facf9fc 25962+ /* internal use for the scripts */
25963+ {Opt_ignore_silent, "si=%s"},
25964+
25965+ {Opt_br, "dirs=%s"},
25966+ {Opt_ignore, "debug=%d"},
25967+ {Opt_ignore, "delete=whiteout"},
25968+ {Opt_ignore, "delete=all"},
25969+ {Opt_ignore, "imap=%s"},
25970+
1308ab2a 25971+ /* temporary workaround, due to old mount(8)? */
25972+ {Opt_ignore_silent, "relatime"},
25973+
1facf9fc 25974+ {Opt_err, NULL}
25975+};
25976+
25977+/* ---------------------------------------------------------------------- */
25978+
076b876e 25979+static const char *au_parser_pattern(int val, match_table_t tbl)
1facf9fc 25980+{
076b876e
AM
25981+ struct match_token *p;
25982+
25983+ p = tbl;
25984+ while (p->pattern) {
25985+ if (p->token == val)
25986+ return p->pattern;
25987+ p++;
1facf9fc 25988+ }
25989+ BUG();
25990+ return "??";
25991+}
25992+
076b876e
AM
25993+static const char *au_optstr(int *val, match_table_t tbl)
25994+{
25995+ struct match_token *p;
25996+ int v;
25997+
25998+ v = *val;
2000de60
JR
25999+ if (!v)
26000+ goto out;
076b876e 26001+ p = tbl;
2000de60
JR
26002+ while (p->pattern) {
26003+ if (p->token
26004+ && (v & p->token) == p->token) {
076b876e
AM
26005+ *val &= ~p->token;
26006+ return p->pattern;
26007+ }
26008+ p++;
26009+ }
2000de60
JR
26010+
26011+out:
076b876e
AM
26012+ return NULL;
26013+}
26014+
1facf9fc 26015+/* ---------------------------------------------------------------------- */
26016+
1e00d052 26017+static match_table_t brperm = {
1facf9fc 26018+ {AuBrPerm_RO, AUFS_BRPERM_RO},
26019+ {AuBrPerm_RR, AUFS_BRPERM_RR},
26020+ {AuBrPerm_RW, AUFS_BRPERM_RW},
1e00d052
AM
26021+ {0, NULL}
26022+};
1facf9fc 26023+
86dc4139 26024+static match_table_t brattr = {
076b876e
AM
26025+ /* general */
26026+ {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
26027+ {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
c1595e42 26028+ /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
86dc4139 26029+ {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
2000de60 26030+#ifdef CONFIG_AUFS_FHSM
076b876e 26031+ {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
2000de60
JR
26032+#endif
26033+#ifdef CONFIG_AUFS_XATTR
c1595e42
JR
26034+ {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
26035+ {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
26036+ {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
26037+ {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
26038+ {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
26039+ {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
2000de60 26040+#endif
076b876e
AM
26041+
26042+ /* ro/rr branch */
1e00d052 26043+ {AuBrRAttr_WH, AUFS_BRRATTR_WH},
076b876e
AM
26044+
26045+ /* rw branch */
26046+ {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
1e00d052 26047+ {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
076b876e 26048+
1e00d052 26049+ {0, NULL}
1facf9fc 26050+};
26051+
1e00d052
AM
26052+static int br_attr_val(char *str, match_table_t table, substring_t args[])
26053+{
26054+ int attr, v;
26055+ char *p;
26056+
26057+ attr = 0;
26058+ do {
26059+ p = strchr(str, '+');
26060+ if (p)
26061+ *p = 0;
26062+ v = match_token(str, table, args);
076b876e
AM
26063+ if (v) {
26064+ if (v & AuBrAttr_CMOO_Mask)
26065+ attr &= ~AuBrAttr_CMOO_Mask;
1e00d052 26066+ attr |= v;
076b876e 26067+ } else {
1e00d052
AM
26068+ if (p)
26069+ *p = '+';
0c3ec466 26070+ pr_warn("ignored branch attribute %s\n", str);
1e00d052
AM
26071+ break;
26072+ }
26073+ if (p)
26074+ str = p + 1;
26075+ } while (p);
26076+
26077+ return attr;
26078+}
26079+
076b876e
AM
26080+static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
26081+{
26082+ int sz;
26083+ const char *p;
26084+ char *q;
26085+
076b876e
AM
26086+ q = str->a;
26087+ *q = 0;
26088+ p = au_optstr(&perm, brattr);
26089+ if (p) {
26090+ sz = strlen(p);
26091+ memcpy(q, p, sz + 1);
26092+ q += sz;
26093+ } else
26094+ goto out;
26095+
26096+ do {
26097+ p = au_optstr(&perm, brattr);
26098+ if (p) {
26099+ *q++ = '+';
26100+ sz = strlen(p);
26101+ memcpy(q, p, sz + 1);
26102+ q += sz;
26103+ }
26104+ } while (p);
26105+
26106+out:
c1595e42 26107+ return q - str->a;
076b876e
AM
26108+}
26109+
4a4d8108 26110+static int noinline_for_stack br_perm_val(char *perm)
1facf9fc 26111+{
076b876e
AM
26112+ int val, bad, sz;
26113+ char *p;
1facf9fc 26114+ substring_t args[MAX_OPT_ARGS];
076b876e 26115+ au_br_perm_str_t attr;
1facf9fc 26116+
1e00d052
AM
26117+ p = strchr(perm, '+');
26118+ if (p)
26119+ *p = 0;
26120+ val = match_token(perm, brperm, args);
26121+ if (!val) {
26122+ if (p)
26123+ *p = '+';
0c3ec466 26124+ pr_warn("ignored branch permission %s\n", perm);
1e00d052
AM
26125+ val = AuBrPerm_RO;
26126+ goto out;
26127+ }
26128+ if (!p)
26129+ goto out;
26130+
076b876e
AM
26131+ val |= br_attr_val(p + 1, brattr, args);
26132+
26133+ bad = 0;
86dc4139 26134+ switch (val & AuBrPerm_Mask) {
1e00d052
AM
26135+ case AuBrPerm_RO:
26136+ case AuBrPerm_RR:
076b876e
AM
26137+ bad = val & AuBrWAttr_Mask;
26138+ val &= ~AuBrWAttr_Mask;
1e00d052
AM
26139+ break;
26140+ case AuBrPerm_RW:
076b876e
AM
26141+ bad = val & AuBrRAttr_Mask;
26142+ val &= ~AuBrRAttr_Mask;
1e00d052
AM
26143+ break;
26144+ }
c1595e42
JR
26145+
26146+ /*
26147+ * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
26148+ * does not treat it as an error, just warning.
26149+ * this is a tiny guard for the user operation.
26150+ */
26151+ if (val & AuBrAttr_UNPIN) {
26152+ bad |= AuBrAttr_UNPIN;
26153+ val &= ~AuBrAttr_UNPIN;
26154+ }
26155+
076b876e
AM
26156+ if (unlikely(bad)) {
26157+ sz = au_do_optstr_br_attr(&attr, bad);
26158+ AuDebugOn(!sz);
26159+ pr_warn("ignored branch attribute %s\n", attr.a);
26160+ }
1e00d052
AM
26161+
26162+out:
1facf9fc 26163+ return val;
26164+}
26165+
076b876e 26166+void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
1facf9fc 26167+{
076b876e
AM
26168+ au_br_perm_str_t attr;
26169+ const char *p;
26170+ char *q;
1e00d052
AM
26171+ int sz;
26172+
076b876e
AM
26173+ q = str->a;
26174+ p = au_optstr(&perm, brperm);
26175+ AuDebugOn(!p || !*p);
26176+ sz = strlen(p);
26177+ memcpy(q, p, sz + 1);
26178+ q += sz;
1e00d052 26179+
076b876e
AM
26180+ sz = au_do_optstr_br_attr(&attr, perm);
26181+ if (sz) {
26182+ *q++ = '+';
26183+ memcpy(q, attr.a, sz + 1);
1e00d052
AM
26184+ }
26185+
076b876e 26186+ AuDebugOn(strlen(str->a) >= sizeof(str->a));
1facf9fc 26187+}
26188+
26189+/* ---------------------------------------------------------------------- */
26190+
26191+static match_table_t udbalevel = {
26192+ {AuOpt_UDBA_REVAL, "reval"},
26193+ {AuOpt_UDBA_NONE, "none"},
4a4d8108
AM
26194+#ifdef CONFIG_AUFS_HNOTIFY
26195+ {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
26196+#ifdef CONFIG_AUFS_HFSNOTIFY
26197+ {AuOpt_UDBA_HNOTIFY, "fsnotify"},
4a4d8108 26198+#endif
1facf9fc 26199+#endif
26200+ {-1, NULL}
26201+};
26202+
4a4d8108 26203+static int noinline_for_stack udba_val(char *str)
1facf9fc 26204+{
26205+ substring_t args[MAX_OPT_ARGS];
26206+
7f207e10 26207+ return match_token(str, udbalevel, args);
1facf9fc 26208+}
26209+
26210+const char *au_optstr_udba(int udba)
26211+{
076b876e 26212+ return au_parser_pattern(udba, udbalevel);
1facf9fc 26213+}
26214+
26215+/* ---------------------------------------------------------------------- */
26216+
26217+static match_table_t au_wbr_create_policy = {
26218+ {AuWbrCreate_TDP, "tdp"},
26219+ {AuWbrCreate_TDP, "top-down-parent"},
26220+ {AuWbrCreate_RR, "rr"},
26221+ {AuWbrCreate_RR, "round-robin"},
26222+ {AuWbrCreate_MFS, "mfs"},
26223+ {AuWbrCreate_MFS, "most-free-space"},
26224+ {AuWbrCreate_MFSV, "mfs:%d"},
26225+ {AuWbrCreate_MFSV, "most-free-space:%d"},
26226+
f2c43d5f
AM
26227+ /* top-down regardless the parent, and then mfs */
26228+ {AuWbrCreate_TDMFS, "tdmfs:%d"},
26229+ {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
26230+
1facf9fc 26231+ {AuWbrCreate_MFSRR, "mfsrr:%d"},
26232+ {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
26233+ {AuWbrCreate_PMFS, "pmfs"},
26234+ {AuWbrCreate_PMFSV, "pmfs:%d"},
392086de
AM
26235+ {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
26236+ {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
1facf9fc 26237+
26238+ {-1, NULL}
26239+};
26240+
1facf9fc 26241+static int au_wbr_mfs_wmark(substring_t *arg, char *str,
26242+ struct au_opt_wbr_create *create)
26243+{
26244+ int err;
26245+ unsigned long long ull;
26246+
26247+ err = 0;
a2654f78 26248+ if (!match_u64(arg, &ull))
1facf9fc 26249+ create->mfsrr_watermark = ull;
26250+ else {
4a4d8108 26251+ pr_err("bad integer in %s\n", str);
1facf9fc 26252+ err = -EINVAL;
26253+ }
26254+
26255+ return err;
26256+}
26257+
26258+static int au_wbr_mfs_sec(substring_t *arg, char *str,
26259+ struct au_opt_wbr_create *create)
26260+{
26261+ int n, err;
26262+
26263+ err = 0;
027c5e7a 26264+ if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
1facf9fc 26265+ create->mfs_second = n;
26266+ else {
4a4d8108 26267+ pr_err("bad integer in %s\n", str);
1facf9fc 26268+ err = -EINVAL;
26269+ }
26270+
26271+ return err;
26272+}
26273+
4a4d8108
AM
26274+static int noinline_for_stack
26275+au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
1facf9fc 26276+{
26277+ int err, e;
26278+ substring_t args[MAX_OPT_ARGS];
26279+
26280+ err = match_token(str, au_wbr_create_policy, args);
26281+ create->wbr_create = err;
26282+ switch (err) {
26283+ case AuWbrCreate_MFSRRV:
f2c43d5f 26284+ case AuWbrCreate_TDMFSV:
392086de 26285+ case AuWbrCreate_PMFSRRV:
1facf9fc 26286+ e = au_wbr_mfs_wmark(&args[0], str, create);
26287+ if (!e)
26288+ e = au_wbr_mfs_sec(&args[1], str, create);
26289+ if (unlikely(e))
26290+ err = e;
26291+ break;
26292+ case AuWbrCreate_MFSRR:
f2c43d5f 26293+ case AuWbrCreate_TDMFS:
392086de 26294+ case AuWbrCreate_PMFSRR:
1facf9fc 26295+ e = au_wbr_mfs_wmark(&args[0], str, create);
26296+ if (unlikely(e)) {
26297+ err = e;
26298+ break;
26299+ }
26300+ /*FALLTHROUGH*/
26301+ case AuWbrCreate_MFS:
26302+ case AuWbrCreate_PMFS:
027c5e7a 26303+ create->mfs_second = AUFS_MFS_DEF_SEC;
1facf9fc 26304+ break;
26305+ case AuWbrCreate_MFSV:
26306+ case AuWbrCreate_PMFSV:
26307+ e = au_wbr_mfs_sec(&args[0], str, create);
26308+ if (unlikely(e))
26309+ err = e;
26310+ break;
26311+ }
26312+
26313+ return err;
26314+}
26315+
26316+const char *au_optstr_wbr_create(int wbr_create)
26317+{
076b876e 26318+ return au_parser_pattern(wbr_create, au_wbr_create_policy);
1facf9fc 26319+}
26320+
26321+static match_table_t au_wbr_copyup_policy = {
26322+ {AuWbrCopyup_TDP, "tdp"},
26323+ {AuWbrCopyup_TDP, "top-down-parent"},
26324+ {AuWbrCopyup_BUP, "bup"},
26325+ {AuWbrCopyup_BUP, "bottom-up-parent"},
26326+ {AuWbrCopyup_BU, "bu"},
26327+ {AuWbrCopyup_BU, "bottom-up"},
26328+ {-1, NULL}
26329+};
26330+
4a4d8108 26331+static int noinline_for_stack au_wbr_copyup_val(char *str)
1facf9fc 26332+{
26333+ substring_t args[MAX_OPT_ARGS];
26334+
26335+ return match_token(str, au_wbr_copyup_policy, args);
26336+}
26337+
26338+const char *au_optstr_wbr_copyup(int wbr_copyup)
26339+{
076b876e 26340+ return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
1facf9fc 26341+}
26342+
26343+/* ---------------------------------------------------------------------- */
26344+
26345+static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
26346+
26347+static void dump_opts(struct au_opts *opts)
26348+{
26349+#ifdef CONFIG_AUFS_DEBUG
26350+ /* reduce stack space */
26351+ union {
26352+ struct au_opt_add *add;
26353+ struct au_opt_del *del;
26354+ struct au_opt_mod *mod;
26355+ struct au_opt_xino *xino;
26356+ struct au_opt_xino_itrunc *xino_itrunc;
26357+ struct au_opt_wbr_create *create;
26358+ } u;
26359+ struct au_opt *opt;
26360+
26361+ opt = opts->opt;
26362+ while (opt->type != Opt_tail) {
26363+ switch (opt->type) {
26364+ case Opt_add:
26365+ u.add = &opt->add;
26366+ AuDbg("add {b%d, %s, 0x%x, %p}\n",
26367+ u.add->bindex, u.add->pathname, u.add->perm,
26368+ u.add->path.dentry);
26369+ break;
26370+ case Opt_del:
26371+ case Opt_idel:
26372+ u.del = &opt->del;
26373+ AuDbg("del {%s, %p}\n",
26374+ u.del->pathname, u.del->h_path.dentry);
26375+ break;
26376+ case Opt_mod:
26377+ case Opt_imod:
26378+ u.mod = &opt->mod;
26379+ AuDbg("mod {%s, 0x%x, %p}\n",
26380+ u.mod->path, u.mod->perm, u.mod->h_root);
26381+ break;
26382+ case Opt_append:
26383+ u.add = &opt->add;
26384+ AuDbg("append {b%d, %s, 0x%x, %p}\n",
26385+ u.add->bindex, u.add->pathname, u.add->perm,
26386+ u.add->path.dentry);
26387+ break;
26388+ case Opt_prepend:
26389+ u.add = &opt->add;
26390+ AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
26391+ u.add->bindex, u.add->pathname, u.add->perm,
26392+ u.add->path.dentry);
26393+ break;
26394+ case Opt_dirwh:
26395+ AuDbg("dirwh %d\n", opt->dirwh);
26396+ break;
26397+ case Opt_rdcache:
26398+ AuDbg("rdcache %d\n", opt->rdcache);
26399+ break;
26400+ case Opt_rdblk:
26401+ AuDbg("rdblk %u\n", opt->rdblk);
26402+ break;
dece6358
AM
26403+ case Opt_rdblk_def:
26404+ AuDbg("rdblk_def\n");
26405+ break;
1facf9fc 26406+ case Opt_rdhash:
26407+ AuDbg("rdhash %u\n", opt->rdhash);
26408+ break;
dece6358
AM
26409+ case Opt_rdhash_def:
26410+ AuDbg("rdhash_def\n");
26411+ break;
1facf9fc 26412+ case Opt_xino:
26413+ u.xino = &opt->xino;
523b37e3 26414+ AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
1facf9fc 26415+ break;
26416+ case Opt_trunc_xino:
26417+ AuLabel(trunc_xino);
26418+ break;
26419+ case Opt_notrunc_xino:
26420+ AuLabel(notrunc_xino);
26421+ break;
26422+ case Opt_trunc_xino_path:
26423+ case Opt_itrunc_xino:
26424+ u.xino_itrunc = &opt->xino_itrunc;
26425+ AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
26426+ break;
1facf9fc 26427+ case Opt_noxino:
26428+ AuLabel(noxino);
26429+ break;
26430+ case Opt_trunc_xib:
26431+ AuLabel(trunc_xib);
26432+ break;
26433+ case Opt_notrunc_xib:
26434+ AuLabel(notrunc_xib);
26435+ break;
dece6358
AM
26436+ case Opt_shwh:
26437+ AuLabel(shwh);
26438+ break;
26439+ case Opt_noshwh:
26440+ AuLabel(noshwh);
26441+ break;
076b876e
AM
26442+ case Opt_dirperm1:
26443+ AuLabel(dirperm1);
26444+ break;
26445+ case Opt_nodirperm1:
26446+ AuLabel(nodirperm1);
26447+ break;
1facf9fc 26448+ case Opt_plink:
26449+ AuLabel(plink);
26450+ break;
26451+ case Opt_noplink:
26452+ AuLabel(noplink);
26453+ break;
26454+ case Opt_list_plink:
26455+ AuLabel(list_plink);
26456+ break;
26457+ case Opt_udba:
26458+ AuDbg("udba %d, %s\n",
26459+ opt->udba, au_optstr_udba(opt->udba));
26460+ break;
4a4d8108
AM
26461+ case Opt_dio:
26462+ AuLabel(dio);
26463+ break;
26464+ case Opt_nodio:
26465+ AuLabel(nodio);
26466+ break;
1facf9fc 26467+ case Opt_diropq_a:
26468+ AuLabel(diropq_a);
26469+ break;
26470+ case Opt_diropq_w:
26471+ AuLabel(diropq_w);
26472+ break;
26473+ case Opt_warn_perm:
26474+ AuLabel(warn_perm);
26475+ break;
26476+ case Opt_nowarn_perm:
26477+ AuLabel(nowarn_perm);
26478+ break;
1facf9fc 26479+ case Opt_verbose:
26480+ AuLabel(verbose);
26481+ break;
26482+ case Opt_noverbose:
26483+ AuLabel(noverbose);
26484+ break;
26485+ case Opt_sum:
26486+ AuLabel(sum);
26487+ break;
26488+ case Opt_nosum:
26489+ AuLabel(nosum);
26490+ break;
26491+ case Opt_wsum:
26492+ AuLabel(wsum);
26493+ break;
26494+ case Opt_wbr_create:
26495+ u.create = &opt->wbr_create;
26496+ AuDbg("create %d, %s\n", u.create->wbr_create,
26497+ au_optstr_wbr_create(u.create->wbr_create));
26498+ switch (u.create->wbr_create) {
26499+ case AuWbrCreate_MFSV:
26500+ case AuWbrCreate_PMFSV:
26501+ AuDbg("%d sec\n", u.create->mfs_second);
26502+ break;
26503+ case AuWbrCreate_MFSRR:
f2c43d5f 26504+ case AuWbrCreate_TDMFS:
1facf9fc 26505+ AuDbg("%llu watermark\n",
26506+ u.create->mfsrr_watermark);
26507+ break;
26508+ case AuWbrCreate_MFSRRV:
f2c43d5f 26509+ case AuWbrCreate_TDMFSV:
392086de 26510+ case AuWbrCreate_PMFSRRV:
1facf9fc 26511+ AuDbg("%llu watermark, %d sec\n",
26512+ u.create->mfsrr_watermark,
26513+ u.create->mfs_second);
26514+ break;
26515+ }
26516+ break;
26517+ case Opt_wbr_copyup:
26518+ AuDbg("copyup %d, %s\n", opt->wbr_copyup,
26519+ au_optstr_wbr_copyup(opt->wbr_copyup));
26520+ break;
076b876e
AM
26521+ case Opt_fhsm_sec:
26522+ AuDbg("fhsm_sec %u\n", opt->fhsm_second);
26523+ break;
8b6a4947
AM
26524+ case Opt_dirren:
26525+ AuLabel(dirren);
26526+ break;
26527+ case Opt_nodirren:
26528+ AuLabel(nodirren);
26529+ break;
c1595e42
JR
26530+ case Opt_acl:
26531+ AuLabel(acl);
26532+ break;
26533+ case Opt_noacl:
26534+ AuLabel(noacl);
26535+ break;
1facf9fc 26536+ default:
26537+ BUG();
26538+ }
26539+ opt++;
26540+ }
26541+#endif
26542+}
26543+
26544+void au_opts_free(struct au_opts *opts)
26545+{
26546+ struct au_opt *opt;
26547+
26548+ opt = opts->opt;
26549+ while (opt->type != Opt_tail) {
26550+ switch (opt->type) {
26551+ case Opt_add:
26552+ case Opt_append:
26553+ case Opt_prepend:
26554+ path_put(&opt->add.path);
26555+ break;
26556+ case Opt_del:
26557+ case Opt_idel:
26558+ path_put(&opt->del.h_path);
26559+ break;
26560+ case Opt_mod:
26561+ case Opt_imod:
26562+ dput(opt->mod.h_root);
26563+ break;
26564+ case Opt_xino:
26565+ fput(opt->xino.file);
26566+ break;
26567+ }
26568+ opt++;
26569+ }
26570+}
26571+
26572+static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
26573+ aufs_bindex_t bindex)
26574+{
26575+ int err;
26576+ struct au_opt_add *add = &opt->add;
26577+ char *p;
26578+
26579+ add->bindex = bindex;
1e00d052 26580+ add->perm = AuBrPerm_RO;
1facf9fc 26581+ add->pathname = opt_str;
26582+ p = strchr(opt_str, '=');
26583+ if (p) {
26584+ *p++ = 0;
26585+ if (*p)
26586+ add->perm = br_perm_val(p);
26587+ }
26588+
26589+ err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
26590+ if (!err) {
26591+ if (!p) {
26592+ add->perm = AuBrPerm_RO;
26593+ if (au_test_fs_rr(add->path.dentry->d_sb))
26594+ add->perm = AuBrPerm_RR;
2121bcd9 26595+ else if (!bindex && !(sb_flags & SB_RDONLY))
1facf9fc 26596+ add->perm = AuBrPerm_RW;
26597+ }
26598+ opt->type = Opt_add;
26599+ goto out;
26600+ }
4a4d8108 26601+ pr_err("lookup failed %s (%d)\n", add->pathname, err);
1facf9fc 26602+ err = -EINVAL;
26603+
4f0767ce 26604+out:
1facf9fc 26605+ return err;
26606+}
26607+
26608+static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
26609+{
26610+ int err;
26611+
26612+ del->pathname = args[0].from;
26613+ AuDbg("del path %s\n", del->pathname);
26614+
26615+ err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
26616+ if (unlikely(err))
4a4d8108 26617+ pr_err("lookup failed %s (%d)\n", del->pathname, err);
1facf9fc 26618+
26619+ return err;
26620+}
26621+
26622+#if 0 /* reserved for future use */
26623+static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
26624+ struct au_opt_del *del, substring_t args[])
26625+{
26626+ int err;
26627+ struct dentry *root;
26628+
26629+ err = -EINVAL;
26630+ root = sb->s_root;
26631+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26632+ if (bindex < 0 || au_sbbot(sb) < bindex) {
4a4d8108 26633+ pr_err("out of bounds, %d\n", bindex);
1facf9fc 26634+ goto out;
26635+ }
26636+
26637+ err = 0;
26638+ del->h_path.dentry = dget(au_h_dptr(root, bindex));
26639+ del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
26640+
4f0767ce 26641+out:
1facf9fc 26642+ aufs_read_unlock(root, !AuLock_IR);
26643+ return err;
26644+}
26645+#endif
26646+
4a4d8108
AM
26647+static int noinline_for_stack
26648+au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
1facf9fc 26649+{
26650+ int err;
26651+ struct path path;
26652+ char *p;
26653+
26654+ err = -EINVAL;
26655+ mod->path = args[0].from;
26656+ p = strchr(mod->path, '=');
26657+ if (unlikely(!p)) {
acd2b654 26658+ pr_err("no permission %s\n", args[0].from);
1facf9fc 26659+ goto out;
26660+ }
26661+
26662+ *p++ = 0;
26663+ err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
26664+ if (unlikely(err)) {
4a4d8108 26665+ pr_err("lookup failed %s (%d)\n", mod->path, err);
1facf9fc 26666+ goto out;
26667+ }
26668+
26669+ mod->perm = br_perm_val(p);
26670+ AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
26671+ mod->h_root = dget(path.dentry);
26672+ path_put(&path);
26673+
4f0767ce 26674+out:
1facf9fc 26675+ return err;
26676+}
26677+
26678+#if 0 /* reserved for future use */
26679+static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
26680+ struct au_opt_mod *mod, substring_t args[])
26681+{
26682+ int err;
26683+ struct dentry *root;
26684+
26685+ err = -EINVAL;
26686+ root = sb->s_root;
26687+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26688+ if (bindex < 0 || au_sbbot(sb) < bindex) {
4a4d8108 26689+ pr_err("out of bounds, %d\n", bindex);
1facf9fc 26690+ goto out;
26691+ }
26692+
26693+ err = 0;
26694+ mod->perm = br_perm_val(args[1].from);
26695+ AuDbg("mod path %s, perm 0x%x, %s\n",
26696+ mod->path, mod->perm, args[1].from);
26697+ mod->h_root = dget(au_h_dptr(root, bindex));
26698+
4f0767ce 26699+out:
1facf9fc 26700+ aufs_read_unlock(root, !AuLock_IR);
26701+ return err;
26702+}
26703+#endif
26704+
26705+static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
26706+ substring_t args[])
26707+{
26708+ int err;
26709+ struct file *file;
26710+
26711+ file = au_xino_create(sb, args[0].from, /*silent*/0);
26712+ err = PTR_ERR(file);
26713+ if (IS_ERR(file))
26714+ goto out;
26715+
26716+ err = -EINVAL;
2000de60 26717+ if (unlikely(file->f_path.dentry->d_sb == sb)) {
1facf9fc 26718+ fput(file);
4a4d8108 26719+ pr_err("%s must be outside\n", args[0].from);
1facf9fc 26720+ goto out;
26721+ }
26722+
26723+ err = 0;
26724+ xino->file = file;
26725+ xino->path = args[0].from;
26726+
4f0767ce 26727+out:
1facf9fc 26728+ return err;
26729+}
26730+
4a4d8108
AM
26731+static int noinline_for_stack
26732+au_opts_parse_xino_itrunc_path(struct super_block *sb,
26733+ struct au_opt_xino_itrunc *xino_itrunc,
26734+ substring_t args[])
1facf9fc 26735+{
26736+ int err;
5afbbe0d 26737+ aufs_bindex_t bbot, bindex;
1facf9fc 26738+ struct path path;
26739+ struct dentry *root;
26740+
26741+ err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
26742+ if (unlikely(err)) {
4a4d8108 26743+ pr_err("lookup failed %s (%d)\n", args[0].from, err);
1facf9fc 26744+ goto out;
26745+ }
26746+
26747+ xino_itrunc->bindex = -1;
26748+ root = sb->s_root;
26749+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d
AM
26750+ bbot = au_sbbot(sb);
26751+ for (bindex = 0; bindex <= bbot; bindex++) {
1facf9fc 26752+ if (au_h_dptr(root, bindex) == path.dentry) {
26753+ xino_itrunc->bindex = bindex;
26754+ break;
26755+ }
26756+ }
26757+ aufs_read_unlock(root, !AuLock_IR);
26758+ path_put(&path);
26759+
26760+ if (unlikely(xino_itrunc->bindex < 0)) {
4a4d8108 26761+ pr_err("no such branch %s\n", args[0].from);
1facf9fc 26762+ err = -EINVAL;
26763+ }
26764+
4f0767ce 26765+out:
1facf9fc 26766+ return err;
26767+}
26768+
26769+/* called without aufs lock */
26770+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
26771+{
26772+ int err, n, token;
26773+ aufs_bindex_t bindex;
26774+ unsigned char skipped;
26775+ struct dentry *root;
26776+ struct au_opt *opt, *opt_tail;
26777+ char *opt_str;
26778+ /* reduce the stack space */
26779+ union {
26780+ struct au_opt_xino_itrunc *xino_itrunc;
26781+ struct au_opt_wbr_create *create;
26782+ } u;
26783+ struct {
26784+ substring_t args[MAX_OPT_ARGS];
26785+ } *a;
26786+
26787+ err = -ENOMEM;
26788+ a = kmalloc(sizeof(*a), GFP_NOFS);
26789+ if (unlikely(!a))
26790+ goto out;
26791+
26792+ root = sb->s_root;
26793+ err = 0;
26794+ bindex = 0;
26795+ opt = opts->opt;
26796+ opt_tail = opt + opts->max_opt - 1;
26797+ opt->type = Opt_tail;
26798+ while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
26799+ err = -EINVAL;
26800+ skipped = 0;
26801+ token = match_token(opt_str, options, a->args);
26802+ switch (token) {
26803+ case Opt_br:
26804+ err = 0;
26805+ while (!err && (opt_str = strsep(&a->args[0].from, ":"))
26806+ && *opt_str) {
26807+ err = opt_add(opt, opt_str, opts->sb_flags,
26808+ bindex++);
26809+ if (unlikely(!err && ++opt > opt_tail)) {
26810+ err = -E2BIG;
26811+ break;
26812+ }
26813+ opt->type = Opt_tail;
26814+ skipped = 1;
26815+ }
26816+ break;
26817+ case Opt_add:
26818+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26819+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26820+ break;
26821+ }
26822+ bindex = n;
26823+ err = opt_add(opt, a->args[1].from, opts->sb_flags,
26824+ bindex);
26825+ if (!err)
26826+ opt->type = token;
26827+ break;
26828+ case Opt_append:
26829+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26830+ /*dummy bindex*/1);
26831+ if (!err)
26832+ opt->type = token;
26833+ break;
26834+ case Opt_prepend:
26835+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26836+ /*bindex*/0);
26837+ if (!err)
26838+ opt->type = token;
26839+ break;
26840+ case Opt_del:
26841+ err = au_opts_parse_del(&opt->del, a->args);
26842+ if (!err)
26843+ opt->type = token;
26844+ break;
26845+#if 0 /* reserved for future use */
26846+ case Opt_idel:
26847+ del->pathname = "(indexed)";
26848+ if (unlikely(match_int(&args[0], &n))) {
4a4d8108 26849+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26850+ break;
26851+ }
26852+ err = au_opts_parse_idel(sb, n, &opt->del, a->args);
26853+ if (!err)
26854+ opt->type = token;
26855+ break;
26856+#endif
26857+ case Opt_mod:
26858+ err = au_opts_parse_mod(&opt->mod, a->args);
26859+ if (!err)
26860+ opt->type = token;
26861+ break;
26862+#ifdef IMOD /* reserved for future use */
26863+ case Opt_imod:
26864+ u.mod->path = "(indexed)";
26865+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26866+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26867+ break;
26868+ }
26869+ err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
26870+ if (!err)
26871+ opt->type = token;
26872+ break;
26873+#endif
26874+ case Opt_xino:
26875+ err = au_opts_parse_xino(sb, &opt->xino, a->args);
26876+ if (!err)
26877+ opt->type = token;
26878+ break;
26879+
26880+ case Opt_trunc_xino_path:
26881+ err = au_opts_parse_xino_itrunc_path
26882+ (sb, &opt->xino_itrunc, a->args);
26883+ if (!err)
26884+ opt->type = token;
26885+ break;
26886+
26887+ case Opt_itrunc_xino:
26888+ u.xino_itrunc = &opt->xino_itrunc;
26889+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26890+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26891+ break;
26892+ }
26893+ u.xino_itrunc->bindex = n;
26894+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26895+ if (n < 0 || au_sbbot(sb) < n) {
4a4d8108 26896+ pr_err("out of bounds, %d\n", n);
1facf9fc 26897+ aufs_read_unlock(root, !AuLock_IR);
26898+ break;
26899+ }
26900+ aufs_read_unlock(root, !AuLock_IR);
26901+ err = 0;
26902+ opt->type = token;
26903+ break;
26904+
26905+ case Opt_dirwh:
26906+ if (unlikely(match_int(&a->args[0], &opt->dirwh)))
26907+ break;
26908+ err = 0;
26909+ opt->type = token;
26910+ break;
26911+
26912+ case Opt_rdcache:
027c5e7a
AM
26913+ if (unlikely(match_int(&a->args[0], &n))) {
26914+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26915+ break;
027c5e7a
AM
26916+ }
26917+ if (unlikely(n > AUFS_RDCACHE_MAX)) {
26918+ pr_err("rdcache must be smaller than %d\n",
26919+ AUFS_RDCACHE_MAX);
26920+ break;
26921+ }
26922+ opt->rdcache = n;
1facf9fc 26923+ err = 0;
26924+ opt->type = token;
26925+ break;
26926+ case Opt_rdblk:
26927+ if (unlikely(match_int(&a->args[0], &n)
1308ab2a 26928+ || n < 0
1facf9fc 26929+ || n > KMALLOC_MAX_SIZE)) {
4a4d8108 26930+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26931+ break;
26932+ }
1308ab2a 26933+ if (unlikely(n && n < NAME_MAX)) {
4a4d8108
AM
26934+ pr_err("rdblk must be larger than %d\n",
26935+ NAME_MAX);
1facf9fc 26936+ break;
26937+ }
26938+ opt->rdblk = n;
26939+ err = 0;
26940+ opt->type = token;
26941+ break;
26942+ case Opt_rdhash:
26943+ if (unlikely(match_int(&a->args[0], &n)
1308ab2a 26944+ || n < 0
1facf9fc 26945+ || n * sizeof(struct hlist_head)
26946+ > KMALLOC_MAX_SIZE)) {
4a4d8108 26947+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26948+ break;
26949+ }
26950+ opt->rdhash = n;
26951+ err = 0;
26952+ opt->type = token;
26953+ break;
26954+
26955+ case Opt_trunc_xino:
26956+ case Opt_notrunc_xino:
26957+ case Opt_noxino:
26958+ case Opt_trunc_xib:
26959+ case Opt_notrunc_xib:
dece6358
AM
26960+ case Opt_shwh:
26961+ case Opt_noshwh:
076b876e
AM
26962+ case Opt_dirperm1:
26963+ case Opt_nodirperm1:
1facf9fc 26964+ case Opt_plink:
26965+ case Opt_noplink:
26966+ case Opt_list_plink:
4a4d8108
AM
26967+ case Opt_dio:
26968+ case Opt_nodio:
1facf9fc 26969+ case Opt_diropq_a:
26970+ case Opt_diropq_w:
26971+ case Opt_warn_perm:
26972+ case Opt_nowarn_perm:
1facf9fc 26973+ case Opt_verbose:
26974+ case Opt_noverbose:
26975+ case Opt_sum:
26976+ case Opt_nosum:
26977+ case Opt_wsum:
dece6358
AM
26978+ case Opt_rdblk_def:
26979+ case Opt_rdhash_def:
8b6a4947
AM
26980+ case Opt_dirren:
26981+ case Opt_nodirren:
c1595e42
JR
26982+ case Opt_acl:
26983+ case Opt_noacl:
1facf9fc 26984+ err = 0;
26985+ opt->type = token;
26986+ break;
26987+
26988+ case Opt_udba:
26989+ opt->udba = udba_val(a->args[0].from);
26990+ if (opt->udba >= 0) {
26991+ err = 0;
26992+ opt->type = token;
26993+ } else
4a4d8108 26994+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26995+ break;
26996+
26997+ case Opt_wbr_create:
26998+ u.create = &opt->wbr_create;
26999+ u.create->wbr_create
27000+ = au_wbr_create_val(a->args[0].from, u.create);
27001+ if (u.create->wbr_create >= 0) {
27002+ err = 0;
27003+ opt->type = token;
27004+ } else
4a4d8108 27005+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 27006+ break;
27007+ case Opt_wbr_copyup:
27008+ opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
27009+ if (opt->wbr_copyup >= 0) {
27010+ err = 0;
27011+ opt->type = token;
27012+ } else
4a4d8108 27013+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 27014+ break;
27015+
076b876e
AM
27016+ case Opt_fhsm_sec:
27017+ if (unlikely(match_int(&a->args[0], &n)
27018+ || n < 0)) {
27019+ pr_err("bad integer in %s\n", opt_str);
27020+ break;
27021+ }
27022+ if (sysaufs_brs) {
27023+ opt->fhsm_second = n;
27024+ opt->type = token;
27025+ } else
27026+ pr_warn("ignored %s\n", opt_str);
27027+ err = 0;
27028+ break;
27029+
1facf9fc 27030+ case Opt_ignore:
0c3ec466 27031+ pr_warn("ignored %s\n", opt_str);
1facf9fc 27032+ /*FALLTHROUGH*/
27033+ case Opt_ignore_silent:
27034+ skipped = 1;
27035+ err = 0;
27036+ break;
27037+ case Opt_err:
4a4d8108 27038+ pr_err("unknown option %s\n", opt_str);
1facf9fc 27039+ break;
27040+ }
27041+
27042+ if (!err && !skipped) {
27043+ if (unlikely(++opt > opt_tail)) {
27044+ err = -E2BIG;
27045+ opt--;
27046+ opt->type = Opt_tail;
27047+ break;
27048+ }
27049+ opt->type = Opt_tail;
27050+ }
27051+ }
27052+
9f237c51 27053+ au_kfree_rcu(a);
1facf9fc 27054+ dump_opts(opts);
27055+ if (unlikely(err))
27056+ au_opts_free(opts);
27057+
4f0767ce 27058+out:
1facf9fc 27059+ return err;
27060+}
27061+
27062+static int au_opt_wbr_create(struct super_block *sb,
27063+ struct au_opt_wbr_create *create)
27064+{
27065+ int err;
27066+ struct au_sbinfo *sbinfo;
27067+
dece6358
AM
27068+ SiMustWriteLock(sb);
27069+
1facf9fc 27070+ err = 1; /* handled */
27071+ sbinfo = au_sbi(sb);
27072+ if (sbinfo->si_wbr_create_ops->fin) {
27073+ err = sbinfo->si_wbr_create_ops->fin(sb);
27074+ if (!err)
27075+ err = 1;
27076+ }
27077+
27078+ sbinfo->si_wbr_create = create->wbr_create;
27079+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
27080+ switch (create->wbr_create) {
27081+ case AuWbrCreate_MFSRRV:
27082+ case AuWbrCreate_MFSRR:
f2c43d5f
AM
27083+ case AuWbrCreate_TDMFS:
27084+ case AuWbrCreate_TDMFSV:
392086de
AM
27085+ case AuWbrCreate_PMFSRR:
27086+ case AuWbrCreate_PMFSRRV:
1facf9fc 27087+ sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
27088+ /*FALLTHROUGH*/
27089+ case AuWbrCreate_MFS:
27090+ case AuWbrCreate_MFSV:
27091+ case AuWbrCreate_PMFS:
27092+ case AuWbrCreate_PMFSV:
e49829fe
JR
27093+ sbinfo->si_wbr_mfs.mfs_expire
27094+ = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
1facf9fc 27095+ break;
27096+ }
27097+
27098+ if (sbinfo->si_wbr_create_ops->init)
27099+ sbinfo->si_wbr_create_ops->init(sb); /* ignore */
27100+
27101+ return err;
27102+}
27103+
27104+/*
27105+ * returns,
27106+ * plus: processed without an error
27107+ * zero: unprocessed
27108+ */
27109+static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
27110+ struct au_opts *opts)
27111+{
27112+ int err;
27113+ struct au_sbinfo *sbinfo;
27114+
dece6358
AM
27115+ SiMustWriteLock(sb);
27116+
1facf9fc 27117+ err = 1; /* handled */
27118+ sbinfo = au_sbi(sb);
27119+ switch (opt->type) {
27120+ case Opt_udba:
27121+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27122+ sbinfo->si_mntflags |= opt->udba;
27123+ opts->given_udba |= opt->udba;
27124+ break;
27125+
27126+ case Opt_plink:
27127+ au_opt_set(sbinfo->si_mntflags, PLINK);
27128+ break;
27129+ case Opt_noplink:
27130+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
e49829fe 27131+ au_plink_put(sb, /*verbose*/1);
1facf9fc 27132+ au_opt_clr(sbinfo->si_mntflags, PLINK);
27133+ break;
27134+ case Opt_list_plink:
27135+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
27136+ au_plink_list(sb);
27137+ break;
27138+
4a4d8108
AM
27139+ case Opt_dio:
27140+ au_opt_set(sbinfo->si_mntflags, DIO);
27141+ au_fset_opts(opts->flags, REFRESH_DYAOP);
27142+ break;
27143+ case Opt_nodio:
27144+ au_opt_clr(sbinfo->si_mntflags, DIO);
27145+ au_fset_opts(opts->flags, REFRESH_DYAOP);
27146+ break;
27147+
076b876e
AM
27148+ case Opt_fhsm_sec:
27149+ au_fhsm_set(sbinfo, opt->fhsm_second);
27150+ break;
27151+
1facf9fc 27152+ case Opt_diropq_a:
27153+ au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27154+ break;
27155+ case Opt_diropq_w:
27156+ au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27157+ break;
27158+
27159+ case Opt_warn_perm:
27160+ au_opt_set(sbinfo->si_mntflags, WARN_PERM);
27161+ break;
27162+ case Opt_nowarn_perm:
27163+ au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
27164+ break;
27165+
1facf9fc 27166+ case Opt_verbose:
27167+ au_opt_set(sbinfo->si_mntflags, VERBOSE);
27168+ break;
27169+ case Opt_noverbose:
27170+ au_opt_clr(sbinfo->si_mntflags, VERBOSE);
27171+ break;
27172+
27173+ case Opt_sum:
27174+ au_opt_set(sbinfo->si_mntflags, SUM);
27175+ break;
27176+ case Opt_wsum:
27177+ au_opt_clr(sbinfo->si_mntflags, SUM);
27178+ au_opt_set(sbinfo->si_mntflags, SUM_W);
27179+ case Opt_nosum:
27180+ au_opt_clr(sbinfo->si_mntflags, SUM);
27181+ au_opt_clr(sbinfo->si_mntflags, SUM_W);
27182+ break;
27183+
27184+ case Opt_wbr_create:
27185+ err = au_opt_wbr_create(sb, &opt->wbr_create);
27186+ break;
27187+ case Opt_wbr_copyup:
27188+ sbinfo->si_wbr_copyup = opt->wbr_copyup;
27189+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
27190+ break;
27191+
27192+ case Opt_dirwh:
27193+ sbinfo->si_dirwh = opt->dirwh;
27194+ break;
27195+
27196+ case Opt_rdcache:
e49829fe
JR
27197+ sbinfo->si_rdcache
27198+ = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
1facf9fc 27199+ break;
27200+ case Opt_rdblk:
27201+ sbinfo->si_rdblk = opt->rdblk;
27202+ break;
dece6358
AM
27203+ case Opt_rdblk_def:
27204+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
27205+ break;
1facf9fc 27206+ case Opt_rdhash:
27207+ sbinfo->si_rdhash = opt->rdhash;
27208+ break;
dece6358
AM
27209+ case Opt_rdhash_def:
27210+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
27211+ break;
27212+
27213+ case Opt_shwh:
27214+ au_opt_set(sbinfo->si_mntflags, SHWH);
27215+ break;
27216+ case Opt_noshwh:
27217+ au_opt_clr(sbinfo->si_mntflags, SHWH);
27218+ break;
1facf9fc 27219+
076b876e
AM
27220+ case Opt_dirperm1:
27221+ au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27222+ break;
27223+ case Opt_nodirperm1:
27224+ au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27225+ break;
27226+
1facf9fc 27227+ case Opt_trunc_xino:
27228+ au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27229+ break;
27230+ case Opt_notrunc_xino:
27231+ au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27232+ break;
27233+
27234+ case Opt_trunc_xino_path:
27235+ case Opt_itrunc_xino:
acd2b654
AM
27236+ err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27237+ /*idx_begin*/0);
1facf9fc 27238+ if (!err)
27239+ err = 1;
27240+ break;
27241+
27242+ case Opt_trunc_xib:
27243+ au_fset_opts(opts->flags, TRUNC_XIB);
27244+ break;
27245+ case Opt_notrunc_xib:
27246+ au_fclr_opts(opts->flags, TRUNC_XIB);
27247+ break;
27248+
8b6a4947
AM
27249+ case Opt_dirren:
27250+ err = 1;
27251+ if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27252+ err = au_dr_opt_set(sb);
27253+ if (!err)
27254+ err = 1;
27255+ }
27256+ if (err == 1)
27257+ au_opt_set(sbinfo->si_mntflags, DIRREN);
27258+ break;
27259+ case Opt_nodirren:
27260+ err = 1;
27261+ if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27262+ err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27263+ DR_FLUSHED));
27264+ if (!err)
27265+ err = 1;
27266+ }
27267+ if (err == 1)
27268+ au_opt_clr(sbinfo->si_mntflags, DIRREN);
27269+ break;
27270+
c1595e42 27271+ case Opt_acl:
2121bcd9 27272+ sb->s_flags |= SB_POSIXACL;
c1595e42
JR
27273+ break;
27274+ case Opt_noacl:
2121bcd9 27275+ sb->s_flags &= ~SB_POSIXACL;
c1595e42
JR
27276+ break;
27277+
1facf9fc 27278+ default:
27279+ err = 0;
27280+ break;
27281+ }
27282+
27283+ return err;
27284+}
27285+
27286+/*
27287+ * returns tri-state.
27288+ * plus: processed without an error
27289+ * zero: unprocessed
27290+ * minus: error
27291+ */
27292+static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27293+ struct au_opts *opts)
27294+{
27295+ int err, do_refresh;
27296+
27297+ err = 0;
27298+ switch (opt->type) {
27299+ case Opt_append:
5afbbe0d 27300+ opt->add.bindex = au_sbbot(sb) + 1;
1facf9fc 27301+ if (opt->add.bindex < 0)
27302+ opt->add.bindex = 0;
27303+ goto add;
27304+ case Opt_prepend:
27305+ opt->add.bindex = 0;
f6b6e03d 27306+ add: /* indented label */
1facf9fc 27307+ case Opt_add:
27308+ err = au_br_add(sb, &opt->add,
27309+ au_ftest_opts(opts->flags, REMOUNT));
27310+ if (!err) {
27311+ err = 1;
027c5e7a 27312+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27313+ }
27314+ break;
27315+
27316+ case Opt_del:
27317+ case Opt_idel:
27318+ err = au_br_del(sb, &opt->del,
27319+ au_ftest_opts(opts->flags, REMOUNT));
27320+ if (!err) {
27321+ err = 1;
27322+ au_fset_opts(opts->flags, TRUNC_XIB);
027c5e7a 27323+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27324+ }
27325+ break;
27326+
27327+ case Opt_mod:
27328+ case Opt_imod:
27329+ err = au_br_mod(sb, &opt->mod,
27330+ au_ftest_opts(opts->flags, REMOUNT),
27331+ &do_refresh);
27332+ if (!err) {
27333+ err = 1;
027c5e7a
AM
27334+ if (do_refresh)
27335+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27336+ }
27337+ break;
27338+ }
1facf9fc 27339+ return err;
27340+}
27341+
27342+static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27343+ struct au_opt_xino **opt_xino,
27344+ struct au_opts *opts)
27345+{
27346+ int err;
1facf9fc 27347+
27348+ err = 0;
27349+ switch (opt->type) {
27350+ case Opt_xino:
062440b3
AM
27351+ err = au_xino_set(sb, &opt->xino,
27352+ !!au_ftest_opts(opts->flags, REMOUNT));
27353+ if (unlikely(err))
27354+ break;
27355+
27356+ *opt_xino = &opt->xino;
1facf9fc 27357+ break;
27358+
27359+ case Opt_noxino:
27360+ au_xino_clr(sb);
1facf9fc 27361+ *opt_xino = (void *)-1;
27362+ break;
27363+ }
27364+
27365+ return err;
27366+}
27367+
27368+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27369+ unsigned int pending)
27370+{
076b876e 27371+ int err, fhsm;
5afbbe0d 27372+ aufs_bindex_t bindex, bbot;
79b8bda9 27373+ unsigned char do_plink, skip, do_free, can_no_dreval;
1facf9fc 27374+ struct au_branch *br;
27375+ struct au_wbr *wbr;
79b8bda9 27376+ struct dentry *root, *dentry;
1facf9fc 27377+ struct inode *dir, *h_dir;
27378+ struct au_sbinfo *sbinfo;
27379+ struct au_hinode *hdir;
27380+
dece6358
AM
27381+ SiMustAnyLock(sb);
27382+
1facf9fc 27383+ sbinfo = au_sbi(sb);
27384+ AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27385+
2121bcd9 27386+ if (!(sb_flags & SB_RDONLY)) {
dece6358 27387+ if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
0c3ec466 27388+ pr_warn("first branch should be rw\n");
dece6358 27389+ if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
febd17d6 27390+ pr_warn_once("shwh should be used with ro\n");
dece6358 27391+ }
1facf9fc 27392+
4a4d8108 27393+ if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
1facf9fc 27394+ && !au_opt_test(sbinfo->si_mntflags, XINO))
febd17d6 27395+ pr_warn_once("udba=*notify requires xino\n");
1facf9fc 27396+
076b876e 27397+ if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
febd17d6
JR
27398+ pr_warn_once("dirperm1 breaks the protection"
27399+ " by the permission bits on the lower branch\n");
076b876e 27400+
1facf9fc 27401+ err = 0;
076b876e 27402+ fhsm = 0;
1facf9fc 27403+ root = sb->s_root;
5527c038 27404+ dir = d_inode(root);
1facf9fc 27405+ do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
79b8bda9
AM
27406+ can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27407+ UDBA_NONE);
5afbbe0d
AM
27408+ bbot = au_sbbot(sb);
27409+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
1facf9fc 27410+ skip = 0;
27411+ h_dir = au_h_iptr(dir, bindex);
27412+ br = au_sbr(sb, bindex);
1facf9fc 27413+
c1595e42
JR
27414+ if ((br->br_perm & AuBrAttr_ICEX)
27415+ && !h_dir->i_op->listxattr)
27416+ br->br_perm &= ~AuBrAttr_ICEX;
27417+#if 0
27418+ if ((br->br_perm & AuBrAttr_ICEX_SEC)
2121bcd9 27419+ && (au_br_sb(br)->s_flags & SB_NOSEC))
c1595e42
JR
27420+ br->br_perm &= ~AuBrAttr_ICEX_SEC;
27421+#endif
27422+
27423+ do_free = 0;
1facf9fc 27424+ wbr = br->br_wbr;
27425+ if (wbr)
27426+ wbr_wh_read_lock(wbr);
27427+
1e00d052 27428+ if (!au_br_writable(br->br_perm)) {
1facf9fc 27429+ do_free = !!wbr;
27430+ skip = (!wbr
27431+ || (!wbr->wbr_whbase
27432+ && !wbr->wbr_plink
27433+ && !wbr->wbr_orph));
1e00d052 27434+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 27435+ /* skip = (!br->br_whbase && !br->br_orph); */
27436+ skip = (!wbr || !wbr->wbr_whbase);
27437+ if (skip && wbr) {
27438+ if (do_plink)
27439+ skip = !!wbr->wbr_plink;
27440+ else
27441+ skip = !wbr->wbr_plink;
27442+ }
1e00d052 27443+ } else {
1facf9fc 27444+ /* skip = (br->br_whbase && br->br_ohph); */
27445+ skip = (wbr && wbr->wbr_whbase);
27446+ if (skip) {
27447+ if (do_plink)
27448+ skip = !!wbr->wbr_plink;
27449+ else
27450+ skip = !wbr->wbr_plink;
27451+ }
1facf9fc 27452+ }
27453+ if (wbr)
27454+ wbr_wh_read_unlock(wbr);
27455+
79b8bda9
AM
27456+ if (can_no_dreval) {
27457+ dentry = br->br_path.dentry;
27458+ spin_lock(&dentry->d_lock);
27459+ if (dentry->d_flags &
27460+ (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27461+ can_no_dreval = 0;
27462+ spin_unlock(&dentry->d_lock);
27463+ }
27464+
076b876e
AM
27465+ if (au_br_fhsm(br->br_perm)) {
27466+ fhsm++;
27467+ AuDebugOn(!br->br_fhsm);
27468+ }
27469+
1facf9fc 27470+ if (skip)
27471+ continue;
27472+
27473+ hdir = au_hi(dir, bindex);
5afbbe0d 27474+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 27475+ if (wbr)
27476+ wbr_wh_write_lock(wbr);
86dc4139 27477+ err = au_wh_init(br, sb);
1facf9fc 27478+ if (wbr)
27479+ wbr_wh_write_unlock(wbr);
5afbbe0d 27480+ au_hn_inode_unlock(hdir);
1facf9fc 27481+
27482+ if (!err && do_free) {
9f237c51 27483+ au_kfree_rcu(wbr);
1facf9fc 27484+ br->br_wbr = NULL;
27485+ }
27486+ }
27487+
79b8bda9
AM
27488+ if (can_no_dreval)
27489+ au_fset_si(sbinfo, NO_DREVAL);
27490+ else
27491+ au_fclr_si(sbinfo, NO_DREVAL);
27492+
c1595e42 27493+ if (fhsm >= 2) {
076b876e 27494+ au_fset_si(sbinfo, FHSM);
5afbbe0d 27495+ for (bindex = bbot; bindex >= 0; bindex--) {
c1595e42
JR
27496+ br = au_sbr(sb, bindex);
27497+ if (au_br_fhsm(br->br_perm)) {
27498+ au_fhsm_set_bottom(sb, bindex);
27499+ break;
27500+ }
27501+ }
27502+ } else {
076b876e 27503+ au_fclr_si(sbinfo, FHSM);
c1595e42
JR
27504+ au_fhsm_set_bottom(sb, -1);
27505+ }
076b876e 27506+
1facf9fc 27507+ return err;
27508+}
27509+
27510+int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27511+{
27512+ int err;
27513+ unsigned int tmp;
5afbbe0d 27514+ aufs_bindex_t bindex, bbot;
1facf9fc 27515+ struct au_opt *opt;
27516+ struct au_opt_xino *opt_xino, xino;
27517+ struct au_sbinfo *sbinfo;
027c5e7a 27518+ struct au_branch *br;
076b876e 27519+ struct inode *dir;
1facf9fc 27520+
dece6358
AM
27521+ SiMustWriteLock(sb);
27522+
1facf9fc 27523+ err = 0;
27524+ opt_xino = NULL;
27525+ opt = opts->opt;
27526+ while (err >= 0 && opt->type != Opt_tail)
27527+ err = au_opt_simple(sb, opt++, opts);
27528+ if (err > 0)
27529+ err = 0;
27530+ else if (unlikely(err < 0))
27531+ goto out;
27532+
27533+ /* disable xino and udba temporary */
27534+ sbinfo = au_sbi(sb);
27535+ tmp = sbinfo->si_mntflags;
27536+ au_opt_clr(sbinfo->si_mntflags, XINO);
27537+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27538+
27539+ opt = opts->opt;
27540+ while (err >= 0 && opt->type != Opt_tail)
27541+ err = au_opt_br(sb, opt++, opts);
27542+ if (err > 0)
27543+ err = 0;
27544+ else if (unlikely(err < 0))
27545+ goto out;
27546+
5afbbe0d
AM
27547+ bbot = au_sbbot(sb);
27548+ if (unlikely(bbot < 0)) {
1facf9fc 27549+ err = -EINVAL;
4a4d8108 27550+ pr_err("no branches\n");
1facf9fc 27551+ goto out;
27552+ }
27553+
27554+ if (au_opt_test(tmp, XINO))
27555+ au_opt_set(sbinfo->si_mntflags, XINO);
27556+ opt = opts->opt;
27557+ while (!err && opt->type != Opt_tail)
27558+ err = au_opt_xino(sb, opt++, &opt_xino, opts);
27559+ if (unlikely(err))
27560+ goto out;
27561+
27562+ err = au_opts_verify(sb, sb->s_flags, tmp);
27563+ if (unlikely(err))
27564+ goto out;
27565+
27566+ /* restore xino */
27567+ if (au_opt_test(tmp, XINO) && !opt_xino) {
27568+ xino.file = au_xino_def(sb);
27569+ err = PTR_ERR(xino.file);
27570+ if (IS_ERR(xino.file))
27571+ goto out;
27572+
27573+ err = au_xino_set(sb, &xino, /*remount*/0);
27574+ fput(xino.file);
27575+ if (unlikely(err))
27576+ goto out;
27577+ }
27578+
27579+ /* restore udba */
027c5e7a 27580+ tmp &= AuOptMask_UDBA;
1facf9fc 27581+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
027c5e7a 27582+ sbinfo->si_mntflags |= tmp;
5afbbe0d
AM
27583+ bbot = au_sbbot(sb);
27584+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
27585+ br = au_sbr(sb, bindex);
27586+ err = au_hnotify_reset_br(tmp, br, br->br_perm);
27587+ if (unlikely(err))
27588+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
27589+ bindex, err);
27590+ /* go on even if err */
27591+ }
4a4d8108 27592+ if (au_opt_test(tmp, UDBA_HNOTIFY)) {
5527c038 27593+ dir = d_inode(sb->s_root);
4a4d8108 27594+ au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
1facf9fc 27595+ }
27596+
4f0767ce 27597+out:
1facf9fc 27598+ return err;
27599+}
27600+
27601+int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27602+{
27603+ int err, rerr;
79b8bda9 27604+ unsigned char no_dreval;
1facf9fc 27605+ struct inode *dir;
27606+ struct au_opt_xino *opt_xino;
27607+ struct au_opt *opt;
27608+ struct au_sbinfo *sbinfo;
27609+
dece6358
AM
27610+ SiMustWriteLock(sb);
27611+
8b6a4947
AM
27612+ err = au_dr_opt_flush(sb);
27613+ if (unlikely(err))
27614+ goto out;
27615+ au_fset_opts(opts->flags, DR_FLUSHED);
27616+
5527c038 27617+ dir = d_inode(sb->s_root);
1facf9fc 27618+ sbinfo = au_sbi(sb);
1facf9fc 27619+ opt_xino = NULL;
27620+ opt = opts->opt;
27621+ while (err >= 0 && opt->type != Opt_tail) {
27622+ err = au_opt_simple(sb, opt, opts);
27623+ if (!err)
27624+ err = au_opt_br(sb, opt, opts);
27625+ if (!err)
27626+ err = au_opt_xino(sb, opt, &opt_xino, opts);
27627+ opt++;
27628+ }
27629+ if (err > 0)
27630+ err = 0;
27631+ AuTraceErr(err);
27632+ /* go on even err */
27633+
79b8bda9 27634+ no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
1facf9fc 27635+ rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27636+ if (unlikely(rerr && !err))
27637+ err = rerr;
27638+
79b8bda9 27639+ if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
b95c5147 27640+ au_fset_opts(opts->flags, REFRESH_IDOP);
79b8bda9 27641+
1facf9fc 27642+ if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27643+ rerr = au_xib_trunc(sb);
27644+ if (unlikely(rerr && !err))
27645+ err = rerr;
27646+ }
27647+
27648+ /* will be handled by the caller */
027c5e7a 27649+ if (!au_ftest_opts(opts->flags, REFRESH)
79b8bda9
AM
27650+ && (opts->given_udba
27651+ || au_opt_test(sbinfo->si_mntflags, XINO)
b95c5147 27652+ || au_ftest_opts(opts->flags, REFRESH_IDOP)
79b8bda9 27653+ ))
027c5e7a 27654+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27655+
27656+ AuDbg("status 0x%x\n", opts->flags);
8b6a4947
AM
27657+
27658+out:
1facf9fc 27659+ return err;
27660+}
27661+
27662+/* ---------------------------------------------------------------------- */
27663+
27664+unsigned int au_opt_udba(struct super_block *sb)
27665+{
27666+ return au_mntflags(sb) & AuOptMask_UDBA;
27667+}
7f207e10
AM
27668diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27669--- /usr/share/empty/fs/aufs/opts.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 27670+++ linux/fs/aufs/opts.h 2018-08-12 23:43:05.460124736 +0200
062440b3
AM
27671@@ -0,0 +1,225 @@
27672+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 27673+/*
b00004a5 27674+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 27675+ *
27676+ * This program, aufs is free software; you can redistribute it and/or modify
27677+ * it under the terms of the GNU General Public License as published by
27678+ * the Free Software Foundation; either version 2 of the License, or
27679+ * (at your option) any later version.
dece6358
AM
27680+ *
27681+ * This program is distributed in the hope that it will be useful,
27682+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27683+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27684+ * GNU General Public License for more details.
27685+ *
27686+ * You should have received a copy of the GNU General Public License
523b37e3 27687+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 27688+ */
27689+
27690+/*
27691+ * mount options/flags
27692+ */
27693+
27694+#ifndef __AUFS_OPTS_H__
27695+#define __AUFS_OPTS_H__
27696+
27697+#ifdef __KERNEL__
27698+
dece6358 27699+#include <linux/path.h>
1facf9fc 27700+
dece6358 27701+struct file;
dece6358 27702+
1facf9fc 27703+/* ---------------------------------------------------------------------- */
27704+
27705+/* mount flags */
27706+#define AuOpt_XINO 1 /* external inode number bitmap
27707+ and translation table */
27708+#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
27709+#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
27710+#define AuOpt_UDBA_REVAL (1 << 3)
4a4d8108 27711+#define AuOpt_UDBA_HNOTIFY (1 << 4)
dece6358
AM
27712+#define AuOpt_SHWH (1 << 5) /* show whiteout */
27713+#define AuOpt_PLINK (1 << 6) /* pseudo-link */
076b876e
AM
27714+#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
27715+ bits */
dece6358
AM
27716+#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
27717+#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
27718+#define AuOpt_SUM_W (1 << 11) /* unimplemented */
27719+#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
27720+#define AuOpt_VERBOSE (1 << 13) /* busy inode when del-branch */
4a4d8108 27721+#define AuOpt_DIO (1 << 14) /* direct io */
8b6a4947 27722+#define AuOpt_DIRREN (1 << 15) /* directory rename */
1facf9fc 27723+
4a4d8108
AM
27724+#ifndef CONFIG_AUFS_HNOTIFY
27725+#undef AuOpt_UDBA_HNOTIFY
27726+#define AuOpt_UDBA_HNOTIFY 0
1facf9fc 27727+#endif
8b6a4947
AM
27728+#ifndef CONFIG_AUFS_DIRREN
27729+#undef AuOpt_DIRREN
27730+#define AuOpt_DIRREN 0
27731+#endif
dece6358
AM
27732+#ifndef CONFIG_AUFS_SHWH
27733+#undef AuOpt_SHWH
27734+#define AuOpt_SHWH 0
27735+#endif
1facf9fc 27736+
27737+#define AuOpt_Def (AuOpt_XINO \
27738+ | AuOpt_UDBA_REVAL \
27739+ | AuOpt_PLINK \
27740+ /* | AuOpt_DIRPERM1 */ \
27741+ | AuOpt_WARN_PERM)
27742+#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27743+ | AuOpt_UDBA_REVAL \
4a4d8108 27744+ | AuOpt_UDBA_HNOTIFY)
1facf9fc 27745+
27746+#define au_opt_test(flags, name) (flags & AuOpt_##name)
27747+#define au_opt_set(flags, name) do { \
27748+ BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27749+ ((flags) |= AuOpt_##name); \
27750+} while (0)
27751+#define au_opt_set_udba(flags, name) do { \
27752+ (flags) &= ~AuOptMask_UDBA; \
27753+ ((flags) |= AuOpt_##name); \
27754+} while (0)
7f207e10
AM
27755+#define au_opt_clr(flags, name) do { \
27756+ ((flags) &= ~AuOpt_##name); \
27757+} while (0)
1facf9fc 27758+
e49829fe
JR
27759+static inline unsigned int au_opts_plink(unsigned int mntflags)
27760+{
27761+#ifdef CONFIG_PROC_FS
27762+ return mntflags;
27763+#else
27764+ return mntflags & ~AuOpt_PLINK;
27765+#endif
27766+}
27767+
1facf9fc 27768+/* ---------------------------------------------------------------------- */
27769+
27770+/* policies to select one among multiple writable branches */
27771+enum {
27772+ AuWbrCreate_TDP, /* top down parent */
27773+ AuWbrCreate_RR, /* round robin */
27774+ AuWbrCreate_MFS, /* most free space */
27775+ AuWbrCreate_MFSV, /* mfs with seconds */
27776+ AuWbrCreate_MFSRR, /* mfs then rr */
27777+ AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
f2c43d5f
AM
27778+ AuWbrCreate_TDMFS, /* top down regardless parent and mfs */
27779+ AuWbrCreate_TDMFSV, /* top down regardless parent and mfs */
1facf9fc 27780+ AuWbrCreate_PMFS, /* parent and mfs */
27781+ AuWbrCreate_PMFSV, /* parent and mfs with seconds */
392086de
AM
27782+ AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
27783+ AuWbrCreate_PMFSRRV, /* plus seconds */
1facf9fc 27784+
27785+ AuWbrCreate_Def = AuWbrCreate_TDP
27786+};
27787+
27788+enum {
27789+ AuWbrCopyup_TDP, /* top down parent */
27790+ AuWbrCopyup_BUP, /* bottom up parent */
27791+ AuWbrCopyup_BU, /* bottom up */
27792+
27793+ AuWbrCopyup_Def = AuWbrCopyup_TDP
27794+};
27795+
27796+/* ---------------------------------------------------------------------- */
27797+
27798+struct au_opt_add {
27799+ aufs_bindex_t bindex;
27800+ char *pathname;
27801+ int perm;
27802+ struct path path;
27803+};
27804+
27805+struct au_opt_del {
27806+ char *pathname;
27807+ struct path h_path;
27808+};
27809+
27810+struct au_opt_mod {
27811+ char *path;
27812+ int perm;
27813+ struct dentry *h_root;
27814+};
27815+
27816+struct au_opt_xino {
27817+ char *path;
27818+ struct file *file;
27819+};
27820+
27821+struct au_opt_xino_itrunc {
27822+ aufs_bindex_t bindex;
27823+};
27824+
27825+struct au_opt_wbr_create {
27826+ int wbr_create;
27827+ int mfs_second;
27828+ unsigned long long mfsrr_watermark;
27829+};
27830+
27831+struct au_opt {
27832+ int type;
27833+ union {
27834+ struct au_opt_xino xino;
27835+ struct au_opt_xino_itrunc xino_itrunc;
27836+ struct au_opt_add add;
27837+ struct au_opt_del del;
27838+ struct au_opt_mod mod;
27839+ int dirwh;
27840+ int rdcache;
27841+ unsigned int rdblk;
27842+ unsigned int rdhash;
27843+ int udba;
27844+ struct au_opt_wbr_create wbr_create;
27845+ int wbr_copyup;
076b876e 27846+ unsigned int fhsm_second;
1facf9fc 27847+ };
27848+};
27849+
27850+/* opts flags */
27851+#define AuOpts_REMOUNT 1
027c5e7a
AM
27852+#define AuOpts_REFRESH (1 << 1)
27853+#define AuOpts_TRUNC_XIB (1 << 2)
27854+#define AuOpts_REFRESH_DYAOP (1 << 3)
b95c5147 27855+#define AuOpts_REFRESH_IDOP (1 << 4)
8b6a4947 27856+#define AuOpts_DR_FLUSHED (1 << 5)
1facf9fc 27857+#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
7f207e10
AM
27858+#define au_fset_opts(flags, name) \
27859+ do { (flags) |= AuOpts_##name; } while (0)
27860+#define au_fclr_opts(flags, name) \
27861+ do { (flags) &= ~AuOpts_##name; } while (0)
1facf9fc 27862+
8b6a4947
AM
27863+#ifndef CONFIG_AUFS_DIRREN
27864+#undef AuOpts_DR_FLUSHED
27865+#define AuOpts_DR_FLUSHED 0
27866+#endif
27867+
1facf9fc 27868+struct au_opts {
27869+ struct au_opt *opt;
27870+ int max_opt;
27871+
27872+ unsigned int given_udba;
27873+ unsigned int flags;
27874+ unsigned long sb_flags;
27875+};
27876+
27877+/* ---------------------------------------------------------------------- */
27878+
7e9cd9fe 27879+/* opts.c */
076b876e 27880+void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
1facf9fc 27881+const char *au_optstr_udba(int udba);
27882+const char *au_optstr_wbr_copyup(int wbr_copyup);
27883+const char *au_optstr_wbr_create(int wbr_create);
27884+
27885+void au_opts_free(struct au_opts *opts);
3c1bdaff 27886+struct super_block;
1facf9fc 27887+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
27888+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27889+ unsigned int pending);
27890+int au_opts_mount(struct super_block *sb, struct au_opts *opts);
27891+int au_opts_remount(struct super_block *sb, struct au_opts *opts);
27892+
27893+unsigned int au_opt_udba(struct super_block *sb);
27894+
1facf9fc 27895+#endif /* __KERNEL__ */
27896+#endif /* __AUFS_OPTS_H__ */
7f207e10
AM
27897diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
27898--- /usr/share/empty/fs/aufs/plink.c 1970-01-01 01:00:00.000000000 +0100
acd2b654 27899+++ linux/fs/aufs/plink.c 2018-10-23 12:33:35.599375796 +0200
062440b3 27900@@ -0,0 +1,516 @@
cd7a4cd9 27901+// SPDX-License-Identifier: GPL-2.0
1facf9fc 27902+/*
b00004a5 27903+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 27904+ *
27905+ * This program, aufs is free software; you can redistribute it and/or modify
27906+ * it under the terms of the GNU General Public License as published by
27907+ * the Free Software Foundation; either version 2 of the License, or
27908+ * (at your option) any later version.
dece6358
AM
27909+ *
27910+ * This program is distributed in the hope that it will be useful,
27911+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27912+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27913+ * GNU General Public License for more details.
27914+ *
27915+ * You should have received a copy of the GNU General Public License
523b37e3 27916+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 27917+ */
27918+
27919+/*
27920+ * pseudo-link
27921+ */
27922+
27923+#include "aufs.h"
27924+
27925+/*
e49829fe 27926+ * the pseudo-link maintenance mode.
1facf9fc 27927+ * during a user process maintains the pseudo-links,
27928+ * prohibit adding a new plink and branch manipulation.
e49829fe
JR
27929+ *
27930+ * Flags
27931+ * NOPLM:
27932+ * For entry functions which will handle plink, and i_mutex is already held
27933+ * in VFS.
27934+ * They cannot wait and should return an error at once.
27935+ * Callers has to check the error.
27936+ * NOPLMW:
27937+ * For entry functions which will handle plink, but i_mutex is not held
27938+ * in VFS.
27939+ * They can wait the plink maintenance mode to finish.
27940+ *
27941+ * They behave like F_SETLK and F_SETLKW.
27942+ * If the caller never handle plink, then both flags are unnecessary.
1facf9fc 27943+ */
e49829fe
JR
27944+
27945+int au_plink_maint(struct super_block *sb, int flags)
1facf9fc 27946+{
e49829fe
JR
27947+ int err;
27948+ pid_t pid, ppid;
f0c0a007 27949+ struct task_struct *parent, *prev;
e49829fe 27950+ struct au_sbinfo *sbi;
dece6358
AM
27951+
27952+ SiMustAnyLock(sb);
27953+
e49829fe
JR
27954+ err = 0;
27955+ if (!au_opt_test(au_mntflags(sb), PLINK))
27956+ goto out;
27957+
27958+ sbi = au_sbi(sb);
27959+ pid = sbi->si_plink_maint_pid;
27960+ if (!pid || pid == current->pid)
27961+ goto out;
27962+
27963+ /* todo: it highly depends upon /sbin/mount.aufs */
f0c0a007
AM
27964+ prev = NULL;
27965+ parent = current;
27966+ ppid = 0;
e49829fe 27967+ rcu_read_lock();
f0c0a007
AM
27968+ while (1) {
27969+ parent = rcu_dereference(parent->real_parent);
27970+ if (parent == prev)
27971+ break;
27972+ ppid = task_pid_vnr(parent);
27973+ if (pid == ppid) {
27974+ rcu_read_unlock();
27975+ goto out;
27976+ }
27977+ prev = parent;
27978+ }
e49829fe 27979+ rcu_read_unlock();
e49829fe
JR
27980+
27981+ if (au_ftest_lock(flags, NOPLMW)) {
027c5e7a
AM
27982+ /* if there is no i_mutex lock in VFS, we don't need to wait */
27983+ /* AuDebugOn(!lockdep_depth(current)); */
e49829fe
JR
27984+ while (sbi->si_plink_maint_pid) {
27985+ si_read_unlock(sb);
27986+ /* gave up wake_up_bit() */
27987+ wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
27988+
27989+ if (au_ftest_lock(flags, FLUSH))
27990+ au_nwt_flush(&sbi->si_nowait);
27991+ si_noflush_read_lock(sb);
27992+ }
27993+ } else if (au_ftest_lock(flags, NOPLM)) {
27994+ AuDbg("ppid %d, pid %d\n", ppid, pid);
27995+ err = -EAGAIN;
27996+ }
27997+
27998+out:
27999+ return err;
4a4d8108
AM
28000+}
28001+
e49829fe 28002+void au_plink_maint_leave(struct au_sbinfo *sbinfo)
4a4d8108 28003+{
4a4d8108 28004+ spin_lock(&sbinfo->si_plink_maint_lock);
027c5e7a 28005+ sbinfo->si_plink_maint_pid = 0;
4a4d8108 28006+ spin_unlock(&sbinfo->si_plink_maint_lock);
027c5e7a 28007+ wake_up_all(&sbinfo->si_plink_wq);
4a4d8108
AM
28008+}
28009+
e49829fe 28010+int au_plink_maint_enter(struct super_block *sb)
4a4d8108
AM
28011+{
28012+ int err;
4a4d8108
AM
28013+ struct au_sbinfo *sbinfo;
28014+
28015+ err = 0;
4a4d8108
AM
28016+ sbinfo = au_sbi(sb);
28017+ /* make sure i am the only one in this fs */
e49829fe
JR
28018+ si_write_lock(sb, AuLock_FLUSH);
28019+ if (au_opt_test(au_mntflags(sb), PLINK)) {
28020+ spin_lock(&sbinfo->si_plink_maint_lock);
28021+ if (!sbinfo->si_plink_maint_pid)
28022+ sbinfo->si_plink_maint_pid = current->pid;
28023+ else
28024+ err = -EBUSY;
28025+ spin_unlock(&sbinfo->si_plink_maint_lock);
28026+ }
4a4d8108
AM
28027+ si_write_unlock(sb);
28028+
28029+ return err;
1facf9fc 28030+}
28031+
28032+/* ---------------------------------------------------------------------- */
28033+
1facf9fc 28034+#ifdef CONFIG_AUFS_DEBUG
28035+void au_plink_list(struct super_block *sb)
28036+{
86dc4139 28037+ int i;
1facf9fc 28038+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28039+ struct hlist_bl_head *hbl;
28040+ struct hlist_bl_node *pos;
5afbbe0d 28041+ struct au_icntnr *icntnr;
1facf9fc 28042+
dece6358
AM
28043+ SiMustAnyLock(sb);
28044+
1facf9fc 28045+ sbinfo = au_sbi(sb);
28046+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28047+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28048+
86dc4139 28049+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
28050+ hbl = sbinfo->si_plink + i;
28051+ hlist_bl_lock(hbl);
28052+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
5afbbe0d 28053+ AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
8b6a4947 28054+ hlist_bl_unlock(hbl);
86dc4139 28055+ }
1facf9fc 28056+}
28057+#endif
28058+
28059+/* is the inode pseudo-linked? */
28060+int au_plink_test(struct inode *inode)
28061+{
86dc4139 28062+ int found, i;
1facf9fc 28063+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28064+ struct hlist_bl_head *hbl;
28065+ struct hlist_bl_node *pos;
5afbbe0d 28066+ struct au_icntnr *icntnr;
1facf9fc 28067+
28068+ sbinfo = au_sbi(inode->i_sb);
dece6358 28069+ AuRwMustAnyLock(&sbinfo->si_rwsem);
1facf9fc 28070+ AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
e49829fe 28071+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
1facf9fc 28072+
28073+ found = 0;
86dc4139 28074+ i = au_plink_hash(inode->i_ino);
8b6a4947
AM
28075+ hbl = sbinfo->si_plink + i;
28076+ hlist_bl_lock(hbl);
28077+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
5afbbe0d 28078+ if (&icntnr->vfs_inode == inode) {
1facf9fc 28079+ found = 1;
28080+ break;
28081+ }
8b6a4947 28082+ hlist_bl_unlock(hbl);
1facf9fc 28083+ return found;
28084+}
28085+
28086+/* ---------------------------------------------------------------------- */
28087+
28088+/*
28089+ * generate a name for plink.
28090+ * the file will be stored under AUFS_WH_PLINKDIR.
28091+ */
28092+/* 20 is max digits length of ulong 64 */
28093+#define PLINK_NAME_LEN ((20 + 1) * 2)
28094+
28095+static int plink_name(char *name, int len, struct inode *inode,
28096+ aufs_bindex_t bindex)
28097+{
28098+ int rlen;
28099+ struct inode *h_inode;
28100+
28101+ h_inode = au_h_iptr(inode, bindex);
28102+ rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
28103+ return rlen;
28104+}
28105+
7f207e10
AM
28106+struct au_do_plink_lkup_args {
28107+ struct dentry **errp;
28108+ struct qstr *tgtname;
28109+ struct dentry *h_parent;
28110+ struct au_branch *br;
28111+};
28112+
28113+static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
28114+ struct dentry *h_parent,
28115+ struct au_branch *br)
28116+{
28117+ struct dentry *h_dentry;
febd17d6 28118+ struct inode *h_inode;
7f207e10 28119+
febd17d6 28120+ h_inode = d_inode(h_parent);
be118d29 28121+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
b4510431 28122+ h_dentry = vfsub_lkup_one(tgtname, h_parent);
3c1bdaff 28123+ inode_unlock_shared(h_inode);
7f207e10
AM
28124+ return h_dentry;
28125+}
28126+
28127+static void au_call_do_plink_lkup(void *args)
28128+{
28129+ struct au_do_plink_lkup_args *a = args;
28130+ *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
28131+}
28132+
1facf9fc 28133+/* lookup the plink-ed @inode under the branch at @bindex */
28134+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
28135+{
28136+ struct dentry *h_dentry, *h_parent;
28137+ struct au_branch *br;
7f207e10 28138+ int wkq_err;
1facf9fc 28139+ char a[PLINK_NAME_LEN];
0c3ec466 28140+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 28141+
e49829fe
JR
28142+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28143+
1facf9fc 28144+ br = au_sbr(inode->i_sb, bindex);
28145+ h_parent = br->br_wbr->wbr_plink;
1facf9fc 28146+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28147+
2dfbb274 28148+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
7f207e10
AM
28149+ struct au_do_plink_lkup_args args = {
28150+ .errp = &h_dentry,
28151+ .tgtname = &tgtname,
28152+ .h_parent = h_parent,
28153+ .br = br
28154+ };
28155+
28156+ wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
28157+ if (unlikely(wkq_err))
28158+ h_dentry = ERR_PTR(wkq_err);
28159+ } else
28160+ h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
28161+
1facf9fc 28162+ return h_dentry;
28163+}
28164+
28165+/* create a pseudo-link */
28166+static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
28167+ struct dentry *h_dentry, struct au_branch *br)
28168+{
28169+ int err;
28170+ struct path h_path = {
86dc4139 28171+ .mnt = au_br_mnt(br)
1facf9fc 28172+ };
523b37e3 28173+ struct inode *h_dir, *delegated;
1facf9fc 28174+
5527c038 28175+ h_dir = d_inode(h_parent);
febd17d6 28176+ inode_lock_nested(h_dir, AuLsc_I_CHILD2);
4f0767ce 28177+again:
b4510431 28178+ h_path.dentry = vfsub_lkup_one(tgt, h_parent);
1facf9fc 28179+ err = PTR_ERR(h_path.dentry);
28180+ if (IS_ERR(h_path.dentry))
28181+ goto out;
28182+
28183+ err = 0;
28184+ /* wh.plink dir is not monitored */
7f207e10 28185+ /* todo: is it really safe? */
5527c038
JR
28186+ if (d_is_positive(h_path.dentry)
28187+ && d_inode(h_path.dentry) != d_inode(h_dentry)) {
523b37e3
AM
28188+ delegated = NULL;
28189+ err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28190+ if (unlikely(err == -EWOULDBLOCK)) {
28191+ pr_warn("cannot retry for NFSv4 delegation"
28192+ " for an internal unlink\n");
28193+ iput(delegated);
28194+ }
1facf9fc 28195+ dput(h_path.dentry);
28196+ h_path.dentry = NULL;
28197+ if (!err)
28198+ goto again;
28199+ }
5527c038 28200+ if (!err && d_is_negative(h_path.dentry)) {
523b37e3
AM
28201+ delegated = NULL;
28202+ err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28203+ if (unlikely(err == -EWOULDBLOCK)) {
28204+ pr_warn("cannot retry for NFSv4 delegation"
28205+ " for an internal link\n");
28206+ iput(delegated);
28207+ }
28208+ }
1facf9fc 28209+ dput(h_path.dentry);
28210+
4f0767ce 28211+out:
febd17d6 28212+ inode_unlock(h_dir);
1facf9fc 28213+ return err;
28214+}
28215+
28216+struct do_whplink_args {
28217+ int *errp;
28218+ struct qstr *tgt;
28219+ struct dentry *h_parent;
28220+ struct dentry *h_dentry;
28221+ struct au_branch *br;
28222+};
28223+
28224+static void call_do_whplink(void *args)
28225+{
28226+ struct do_whplink_args *a = args;
28227+ *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
28228+}
28229+
28230+static int whplink(struct dentry *h_dentry, struct inode *inode,
28231+ aufs_bindex_t bindex, struct au_branch *br)
28232+{
28233+ int err, wkq_err;
28234+ struct au_wbr *wbr;
28235+ struct dentry *h_parent;
1facf9fc 28236+ char a[PLINK_NAME_LEN];
0c3ec466 28237+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 28238+
28239+ wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
28240+ h_parent = wbr->wbr_plink;
1facf9fc 28241+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28242+
28243+ /* always superio. */
2dfbb274 28244+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
1facf9fc 28245+ struct do_whplink_args args = {
28246+ .errp = &err,
28247+ .tgt = &tgtname,
28248+ .h_parent = h_parent,
28249+ .h_dentry = h_dentry,
28250+ .br = br
28251+ };
28252+ wkq_err = au_wkq_wait(call_do_whplink, &args);
28253+ if (unlikely(wkq_err))
28254+ err = wkq_err;
28255+ } else
28256+ err = do_whplink(&tgtname, h_parent, h_dentry, br);
1facf9fc 28257+
28258+ return err;
28259+}
28260+
1facf9fc 28261+/*
28262+ * create a new pseudo-link for @h_dentry on @bindex.
28263+ * the linked inode is held in aufs @inode.
28264+ */
28265+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28266+ struct dentry *h_dentry)
28267+{
28268+ struct super_block *sb;
28269+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28270+ struct hlist_bl_head *hbl;
28271+ struct hlist_bl_node *pos;
5afbbe0d 28272+ struct au_icntnr *icntnr;
86dc4139 28273+ int found, err, cnt, i;
1facf9fc 28274+
28275+ sb = inode->i_sb;
28276+ sbinfo = au_sbi(sb);
28277+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28278+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28279+
86dc4139 28280+ found = au_plink_test(inode);
4a4d8108 28281+ if (found)
1facf9fc 28282+ return;
4a4d8108 28283+
86dc4139 28284+ i = au_plink_hash(inode->i_ino);
8b6a4947 28285+ hbl = sbinfo->si_plink + i;
5afbbe0d 28286+ au_igrab(inode);
1facf9fc 28287+
8b6a4947
AM
28288+ hlist_bl_lock(hbl);
28289+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
5afbbe0d 28290+ if (&icntnr->vfs_inode == inode) {
4a4d8108
AM
28291+ found = 1;
28292+ break;
28293+ }
1facf9fc 28294+ }
5afbbe0d
AM
28295+ if (!found) {
28296+ icntnr = container_of(inode, struct au_icntnr, vfs_inode);
8b6a4947 28297+ hlist_bl_add_head(&icntnr->plink, hbl);
5afbbe0d 28298+ }
8b6a4947 28299+ hlist_bl_unlock(hbl);
4a4d8108 28300+ if (!found) {
8b6a4947 28301+ cnt = au_hbl_count(hbl);
acd2b654 28302+#define msg "unexpectedly unbalanced or too many pseudo-links"
86dc4139
AM
28303+ if (cnt > AUFS_PLINK_WARN)
28304+ AuWarn1(msg ", %d\n", cnt);
28305+#undef msg
1facf9fc 28306+ err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
5afbbe0d
AM
28307+ if (unlikely(err)) {
28308+ pr_warn("err %d, damaged pseudo link.\n", err);
8b6a4947 28309+ au_hbl_del(&icntnr->plink, hbl);
5afbbe0d 28310+ iput(&icntnr->vfs_inode);
4a4d8108 28311+ }
5afbbe0d
AM
28312+ } else
28313+ iput(&icntnr->vfs_inode);
1facf9fc 28314+}
28315+
28316+/* free all plinks */
e49829fe 28317+void au_plink_put(struct super_block *sb, int verbose)
1facf9fc 28318+{
86dc4139 28319+ int i, warned;
1facf9fc 28320+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28321+ struct hlist_bl_head *hbl;
28322+ struct hlist_bl_node *pos, *tmp;
5afbbe0d 28323+ struct au_icntnr *icntnr;
1facf9fc 28324+
dece6358
AM
28325+ SiMustWriteLock(sb);
28326+
1facf9fc 28327+ sbinfo = au_sbi(sb);
28328+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28329+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28330+
1facf9fc 28331+ /* no spin_lock since sbinfo is write-locked */
86dc4139
AM
28332+ warned = 0;
28333+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
28334+ hbl = sbinfo->si_plink + i;
28335+ if (!warned && verbose && !hlist_bl_empty(hbl)) {
86dc4139
AM
28336+ pr_warn("pseudo-link is not flushed");
28337+ warned = 1;
28338+ }
8b6a4947 28339+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
5afbbe0d 28340+ iput(&icntnr->vfs_inode);
8b6a4947 28341+ INIT_HLIST_BL_HEAD(hbl);
86dc4139 28342+ }
1facf9fc 28343+}
28344+
e49829fe
JR
28345+void au_plink_clean(struct super_block *sb, int verbose)
28346+{
28347+ struct dentry *root;
28348+
28349+ root = sb->s_root;
28350+ aufs_write_lock(root);
28351+ if (au_opt_test(au_mntflags(sb), PLINK))
28352+ au_plink_put(sb, verbose);
28353+ aufs_write_unlock(root);
28354+}
28355+
86dc4139
AM
28356+static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28357+{
28358+ int do_put;
5afbbe0d 28359+ aufs_bindex_t btop, bbot, bindex;
86dc4139
AM
28360+
28361+ do_put = 0;
5afbbe0d
AM
28362+ btop = au_ibtop(inode);
28363+ bbot = au_ibbot(inode);
28364+ if (btop >= 0) {
28365+ for (bindex = btop; bindex <= bbot; bindex++) {
86dc4139
AM
28366+ if (!au_h_iptr(inode, bindex)
28367+ || au_ii_br_id(inode, bindex) != br_id)
28368+ continue;
28369+ au_set_h_iptr(inode, bindex, NULL, 0);
28370+ do_put = 1;
28371+ break;
28372+ }
28373+ if (do_put)
5afbbe0d 28374+ for (bindex = btop; bindex <= bbot; bindex++)
86dc4139
AM
28375+ if (au_h_iptr(inode, bindex)) {
28376+ do_put = 0;
28377+ break;
28378+ }
28379+ } else
28380+ do_put = 1;
28381+
28382+ return do_put;
28383+}
28384+
1facf9fc 28385+/* free the plinks on a branch specified by @br_id */
28386+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28387+{
28388+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28389+ struct hlist_bl_head *hbl;
28390+ struct hlist_bl_node *pos, *tmp;
5afbbe0d 28391+ struct au_icntnr *icntnr;
1facf9fc 28392+ struct inode *inode;
86dc4139 28393+ int i, do_put;
1facf9fc 28394+
dece6358
AM
28395+ SiMustWriteLock(sb);
28396+
1facf9fc 28397+ sbinfo = au_sbi(sb);
28398+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28399+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28400+
8b6a4947 28401+ /* no bit_lock since sbinfo is write-locked */
86dc4139 28402+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
28403+ hbl = sbinfo->si_plink + i;
28404+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
5afbbe0d 28405+ inode = au_igrab(&icntnr->vfs_inode);
86dc4139
AM
28406+ ii_write_lock_child(inode);
28407+ do_put = au_plink_do_half_refresh(inode, br_id);
5afbbe0d 28408+ if (do_put) {
8b6a4947 28409+ hlist_bl_del(&icntnr->plink);
5afbbe0d
AM
28410+ iput(inode);
28411+ }
86dc4139
AM
28412+ ii_write_unlock(inode);
28413+ iput(inode);
dece6358 28414+ }
dece6358
AM
28415+ }
28416+}
7f207e10
AM
28417diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28418--- /usr/share/empty/fs/aufs/poll.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9
AM
28419+++ linux/fs/aufs/poll.c 2018-08-12 23:43:05.460124736 +0200
28420@@ -0,0 +1,51 @@
28421+// SPDX-License-Identifier: GPL-2.0
dece6358 28422+/*
b00004a5 28423+ * Copyright (C) 2005-2018 Junjiro R. Okajima
dece6358
AM
28424+ *
28425+ * This program, aufs is free software; you can redistribute it and/or modify
28426+ * it under the terms of the GNU General Public License as published by
28427+ * the Free Software Foundation; either version 2 of the License, or
28428+ * (at your option) any later version.
28429+ *
28430+ * This program is distributed in the hope that it will be useful,
28431+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28432+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28433+ * GNU General Public License for more details.
28434+ *
28435+ * You should have received a copy of the GNU General Public License
523b37e3 28436+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358
AM
28437+ */
28438+
1308ab2a 28439+/*
28440+ * poll operation
28441+ * There is only one filesystem which implements ->poll operation, currently.
28442+ */
28443+
28444+#include "aufs.h"
28445+
cd7a4cd9 28446+__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
1308ab2a 28447+{
be118d29 28448+ __poll_t mask;
1308ab2a 28449+ struct file *h_file;
1308ab2a 28450+ struct super_block *sb;
28451+
28452+ /* We should pretend an error happened. */
be118d29 28453+ mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
b912730e 28454+ sb = file->f_path.dentry->d_sb;
e49829fe 28455+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 28456+
521ced18 28457+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
062440b3
AM
28458+ if (IS_ERR(h_file)) {
28459+ AuDbg("h_file %ld\n", PTR_ERR(h_file));
1308ab2a 28460+ goto out;
062440b3 28461+ }
1308ab2a 28462+
cd7a4cd9 28463+ mask = vfs_poll(h_file, pt);
b912730e 28464+ fput(h_file); /* instead of au_read_post() */
1308ab2a 28465+
4f0767ce 28466+out:
1308ab2a 28467+ si_read_unlock(sb);
062440b3 28468+ if (mask & EPOLLERR)
b00004a5 28469+ AuDbg("mask 0x%x\n", mask);
1308ab2a 28470+ return mask;
28471+}
c1595e42
JR
28472diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28473--- /usr/share/empty/fs/aufs/posix_acl.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 28474+++ linux/fs/aufs/posix_acl.c 2018-08-12 23:43:05.460124736 +0200
062440b3 28475@@ -0,0 +1,103 @@
cd7a4cd9 28476+// SPDX-License-Identifier: GPL-2.0
c1595e42 28477+/*
b00004a5 28478+ * Copyright (C) 2014-2018 Junjiro R. Okajima
c1595e42
JR
28479+ *
28480+ * This program, aufs is free software; you can redistribute it and/or modify
28481+ * it under the terms of the GNU General Public License as published by
28482+ * the Free Software Foundation; either version 2 of the License, or
28483+ * (at your option) any later version.
28484+ *
28485+ * This program is distributed in the hope that it will be useful,
28486+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28487+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28488+ * GNU General Public License for more details.
28489+ *
28490+ * You should have received a copy of the GNU General Public License
28491+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
28492+ */
28493+
28494+/*
28495+ * posix acl operations
28496+ */
28497+
28498+#include <linux/fs.h>
c1595e42
JR
28499+#include "aufs.h"
28500+
28501+struct posix_acl *aufs_get_acl(struct inode *inode, int type)
28502+{
28503+ struct posix_acl *acl;
28504+ int err;
28505+ aufs_bindex_t bindex;
28506+ struct inode *h_inode;
28507+ struct super_block *sb;
28508+
28509+ acl = NULL;
28510+ sb = inode->i_sb;
28511+ si_read_lock(sb, AuLock_FLUSH);
28512+ ii_read_lock_child(inode);
2121bcd9 28513+ if (!(sb->s_flags & SB_POSIXACL))
c1595e42
JR
28514+ goto out;
28515+
5afbbe0d 28516+ bindex = au_ibtop(inode);
c1595e42
JR
28517+ h_inode = au_h_iptr(inode, bindex);
28518+ if (unlikely(!h_inode
28519+ || ((h_inode->i_mode & S_IFMT)
28520+ != (inode->i_mode & S_IFMT)))) {
28521+ err = au_busy_or_stale();
28522+ acl = ERR_PTR(err);
28523+ goto out;
28524+ }
28525+
28526+ /* always topmost only */
28527+ acl = get_acl(h_inode, type);
a2654f78
AM
28528+ if (!IS_ERR_OR_NULL(acl))
28529+ set_cached_acl(inode, type, acl);
c1595e42
JR
28530+
28531+out:
28532+ ii_read_unlock(inode);
28533+ si_read_unlock(sb);
28534+
28535+ AuTraceErrPtr(acl);
28536+ return acl;
28537+}
28538+
28539+int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
28540+{
28541+ int err;
28542+ ssize_t ssz;
28543+ struct dentry *dentry;
f2c43d5f 28544+ struct au_sxattr arg = {
c1595e42
JR
28545+ .type = AU_ACL_SET,
28546+ .u.acl_set = {
28547+ .acl = acl,
28548+ .type = type
28549+ },
28550+ };
28551+
5afbbe0d
AM
28552+ IMustLock(inode);
28553+
c1595e42
JR
28554+ if (inode->i_ino == AUFS_ROOT_INO)
28555+ dentry = dget(inode->i_sb->s_root);
28556+ else {
28557+ dentry = d_find_alias(inode);
28558+ if (!dentry)
28559+ dentry = d_find_any_alias(inode);
28560+ if (!dentry) {
28561+ pr_warn("cannot handle this inode, "
28562+ "please report to aufs-users ML\n");
28563+ err = -ENOENT;
28564+ goto out;
28565+ }
28566+ }
28567+
f2c43d5f 28568+ ssz = au_sxattr(dentry, inode, &arg);
c1595e42
JR
28569+ dput(dentry);
28570+ err = ssz;
a2654f78 28571+ if (ssz >= 0) {
c1595e42 28572+ err = 0;
a2654f78
AM
28573+ set_cached_acl(inode, type, acl);
28574+ }
c1595e42
JR
28575+
28576+out:
c1595e42
JR
28577+ return err;
28578+}
7f207e10
AM
28579diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28580--- /usr/share/empty/fs/aufs/procfs.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 28581+++ linux/fs/aufs/procfs.c 2018-08-12 23:43:05.460124736 +0200
062440b3 28582@@ -0,0 +1,171 @@
cd7a4cd9 28583+// SPDX-License-Identifier: GPL-2.0
e49829fe 28584+/*
b00004a5 28585+ * Copyright (C) 2010-2018 Junjiro R. Okajima
e49829fe
JR
28586+ *
28587+ * This program, aufs is free software; you can redistribute it and/or modify
28588+ * it under the terms of the GNU General Public License as published by
28589+ * the Free Software Foundation; either version 2 of the License, or
28590+ * (at your option) any later version.
28591+ *
28592+ * This program is distributed in the hope that it will be useful,
28593+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28594+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28595+ * GNU General Public License for more details.
28596+ *
28597+ * You should have received a copy of the GNU General Public License
523b37e3 28598+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
28599+ */
28600+
28601+/*
28602+ * procfs interfaces
28603+ */
28604+
28605+#include <linux/proc_fs.h>
28606+#include "aufs.h"
28607+
28608+static int au_procfs_plm_release(struct inode *inode, struct file *file)
28609+{
28610+ struct au_sbinfo *sbinfo;
28611+
28612+ sbinfo = file->private_data;
28613+ if (sbinfo) {
28614+ au_plink_maint_leave(sbinfo);
28615+ kobject_put(&sbinfo->si_kobj);
28616+ }
28617+
28618+ return 0;
28619+}
28620+
28621+static void au_procfs_plm_write_clean(struct file *file)
28622+{
28623+ struct au_sbinfo *sbinfo;
28624+
28625+ sbinfo = file->private_data;
28626+ if (sbinfo)
28627+ au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28628+}
28629+
28630+static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28631+{
28632+ int err;
28633+ struct super_block *sb;
28634+ struct au_sbinfo *sbinfo;
8b6a4947 28635+ struct hlist_bl_node *pos;
e49829fe
JR
28636+
28637+ err = -EBUSY;
28638+ if (unlikely(file->private_data))
28639+ goto out;
28640+
28641+ sb = NULL;
53392da6 28642+ /* don't use au_sbilist_lock() here */
8b6a4947
AM
28643+ hlist_bl_lock(&au_sbilist);
28644+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
e49829fe
JR
28645+ if (id == sysaufs_si_id(sbinfo)) {
28646+ kobject_get(&sbinfo->si_kobj);
28647+ sb = sbinfo->si_sb;
28648+ break;
28649+ }
8b6a4947 28650+ hlist_bl_unlock(&au_sbilist);
e49829fe
JR
28651+
28652+ err = -EINVAL;
28653+ if (unlikely(!sb))
28654+ goto out;
28655+
28656+ err = au_plink_maint_enter(sb);
28657+ if (!err)
28658+ /* keep kobject_get() */
28659+ file->private_data = sbinfo;
28660+ else
28661+ kobject_put(&sbinfo->si_kobj);
28662+out:
28663+ return err;
28664+}
28665+
28666+/*
28667+ * Accept a valid "si=xxxx" only.
28668+ * Once it is accepted successfully, accept "clean" too.
28669+ */
28670+static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28671+ size_t count, loff_t *ppos)
28672+{
28673+ ssize_t err;
28674+ unsigned long id;
28675+ /* last newline is allowed */
28676+ char buf[3 + sizeof(unsigned long) * 2 + 1];
28677+
28678+ err = -EACCES;
28679+ if (unlikely(!capable(CAP_SYS_ADMIN)))
28680+ goto out;
28681+
28682+ err = -EINVAL;
28683+ if (unlikely(count > sizeof(buf)))
28684+ goto out;
28685+
28686+ err = copy_from_user(buf, ubuf, count);
28687+ if (unlikely(err)) {
28688+ err = -EFAULT;
28689+ goto out;
28690+ }
28691+ buf[count] = 0;
28692+
28693+ err = -EINVAL;
28694+ if (!strcmp("clean", buf)) {
28695+ au_procfs_plm_write_clean(file);
28696+ goto out_success;
28697+ } else if (unlikely(strncmp("si=", buf, 3)))
28698+ goto out;
28699+
9dbd164d 28700+ err = kstrtoul(buf + 3, 16, &id);
e49829fe
JR
28701+ if (unlikely(err))
28702+ goto out;
28703+
28704+ err = au_procfs_plm_write_si(file, id);
28705+ if (unlikely(err))
28706+ goto out;
28707+
28708+out_success:
28709+ err = count; /* success */
28710+out:
28711+ return err;
28712+}
28713+
28714+static const struct file_operations au_procfs_plm_fop = {
28715+ .write = au_procfs_plm_write,
28716+ .release = au_procfs_plm_release,
28717+ .owner = THIS_MODULE
28718+};
28719+
28720+/* ---------------------------------------------------------------------- */
28721+
28722+static struct proc_dir_entry *au_procfs_dir;
28723+
28724+void au_procfs_fin(void)
28725+{
28726+ remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28727+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28728+}
28729+
28730+int __init au_procfs_init(void)
28731+{
28732+ int err;
28733+ struct proc_dir_entry *entry;
28734+
28735+ err = -ENOMEM;
28736+ au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28737+ if (unlikely(!au_procfs_dir))
28738+ goto out;
28739+
cd7a4cd9 28740+ entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
e49829fe
JR
28741+ au_procfs_dir, &au_procfs_plm_fop);
28742+ if (unlikely(!entry))
28743+ goto out_dir;
28744+
28745+ err = 0;
28746+ goto out; /* success */
28747+
28748+
28749+out_dir:
28750+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28751+out:
28752+ return err;
28753+}
7f207e10
AM
28754diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
28755--- /usr/share/empty/fs/aufs/rdu.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 28756+++ linux/fs/aufs/rdu.c 2018-08-12 23:43:05.463458173 +0200
062440b3 28757@@ -0,0 +1,382 @@
cd7a4cd9 28758+// SPDX-License-Identifier: GPL-2.0
1308ab2a 28759+/*
b00004a5 28760+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1308ab2a 28761+ *
28762+ * This program, aufs is free software; you can redistribute it and/or modify
28763+ * it under the terms of the GNU General Public License as published by
28764+ * the Free Software Foundation; either version 2 of the License, or
28765+ * (at your option) any later version.
28766+ *
28767+ * This program is distributed in the hope that it will be useful,
28768+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28769+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28770+ * GNU General Public License for more details.
28771+ *
28772+ * You should have received a copy of the GNU General Public License
523b37e3 28773+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1308ab2a 28774+ */
28775+
28776+/*
28777+ * readdir in userspace.
28778+ */
28779+
b752ccd1 28780+#include <linux/compat.h>
4a4d8108 28781+#include <linux/fs_stack.h>
1308ab2a 28782+#include <linux/security.h>
1308ab2a 28783+#include "aufs.h"
28784+
28785+/* bits for struct aufs_rdu.flags */
28786+#define AuRdu_CALLED 1
28787+#define AuRdu_CONT (1 << 1)
28788+#define AuRdu_FULL (1 << 2)
28789+#define au_ftest_rdu(flags, name) ((flags) & AuRdu_##name)
7f207e10
AM
28790+#define au_fset_rdu(flags, name) \
28791+ do { (flags) |= AuRdu_##name; } while (0)
28792+#define au_fclr_rdu(flags, name) \
28793+ do { (flags) &= ~AuRdu_##name; } while (0)
1308ab2a 28794+
28795+struct au_rdu_arg {
392086de 28796+ struct dir_context ctx;
1308ab2a 28797+ struct aufs_rdu *rdu;
28798+ union au_rdu_ent_ul ent;
28799+ unsigned long end;
28800+
28801+ struct super_block *sb;
28802+ int err;
28803+};
28804+
392086de 28805+static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
1308ab2a 28806+ loff_t offset, u64 h_ino, unsigned int d_type)
28807+{
28808+ int err, len;
392086de 28809+ struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
1308ab2a 28810+ struct aufs_rdu *rdu = arg->rdu;
28811+ struct au_rdu_ent ent;
28812+
28813+ err = 0;
28814+ arg->err = 0;
28815+ au_fset_rdu(rdu->cookie.flags, CALLED);
28816+ len = au_rdu_len(nlen);
28817+ if (arg->ent.ul + len < arg->end) {
28818+ ent.ino = h_ino;
28819+ ent.bindex = rdu->cookie.bindex;
28820+ ent.type = d_type;
28821+ ent.nlen = nlen;
4a4d8108
AM
28822+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
28823+ ent.type = DT_UNKNOWN;
1308ab2a 28824+
9dbd164d 28825+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 28826+ err = -EFAULT;
28827+ if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
28828+ goto out;
28829+ if (copy_to_user(arg->ent.e->name, name, nlen))
28830+ goto out;
28831+ /* the terminating NULL */
28832+ if (__put_user(0, arg->ent.e->name + nlen))
28833+ goto out;
28834+ err = 0;
28835+ /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
28836+ arg->ent.ul += len;
28837+ rdu->rent++;
28838+ } else {
28839+ err = -EFAULT;
28840+ au_fset_rdu(rdu->cookie.flags, FULL);
28841+ rdu->full = 1;
28842+ rdu->tail = arg->ent;
28843+ }
28844+
4f0767ce 28845+out:
1308ab2a 28846+ /* AuTraceErr(err); */
28847+ return err;
28848+}
28849+
28850+static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
28851+{
28852+ int err;
28853+ loff_t offset;
28854+ struct au_rdu_cookie *cookie = &arg->rdu->cookie;
28855+
92d182d2 28856+ /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
1308ab2a 28857+ offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
28858+ err = offset;
28859+ if (unlikely(offset != cookie->h_pos))
28860+ goto out;
28861+
28862+ err = 0;
28863+ do {
28864+ arg->err = 0;
28865+ au_fclr_rdu(cookie->flags, CALLED);
28866+ /* smp_mb(); */
392086de 28867+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1308ab2a 28868+ if (err >= 0)
28869+ err = arg->err;
28870+ } while (!err
28871+ && au_ftest_rdu(cookie->flags, CALLED)
28872+ && !au_ftest_rdu(cookie->flags, FULL));
28873+ cookie->h_pos = h_file->f_pos;
28874+
4f0767ce 28875+out:
1308ab2a 28876+ AuTraceErr(err);
28877+ return err;
28878+}
28879+
28880+static int au_rdu(struct file *file, struct aufs_rdu *rdu)
28881+{
28882+ int err;
5afbbe0d 28883+ aufs_bindex_t bbot;
392086de
AM
28884+ struct au_rdu_arg arg = {
28885+ .ctx = {
2000de60 28886+ .actor = au_rdu_fill
392086de
AM
28887+ }
28888+ };
1308ab2a 28889+ struct dentry *dentry;
28890+ struct inode *inode;
28891+ struct file *h_file;
28892+ struct au_rdu_cookie *cookie = &rdu->cookie;
28893+
28894+ err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
28895+ if (unlikely(err)) {
28896+ err = -EFAULT;
28897+ AuTraceErr(err);
28898+ goto out;
28899+ }
28900+ rdu->rent = 0;
28901+ rdu->tail = rdu->ent;
28902+ rdu->full = 0;
28903+ arg.rdu = rdu;
28904+ arg.ent = rdu->ent;
28905+ arg.end = arg.ent.ul;
28906+ arg.end += rdu->sz;
28907+
28908+ err = -ENOTDIR;
5afbbe0d 28909+ if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
1308ab2a 28910+ goto out;
28911+
28912+ err = security_file_permission(file, MAY_READ);
28913+ AuTraceErr(err);
28914+ if (unlikely(err))
28915+ goto out;
28916+
2000de60 28917+ dentry = file->f_path.dentry;
5527c038 28918+ inode = d_inode(dentry);
5afbbe0d 28919+ inode_lock_shared(inode);
1308ab2a 28920+
28921+ arg.sb = inode->i_sb;
e49829fe
JR
28922+ err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
28923+ if (unlikely(err))
28924+ goto out_mtx;
027c5e7a
AM
28925+ err = au_alive_dir(dentry);
28926+ if (unlikely(err))
28927+ goto out_si;
e49829fe 28928+ /* todo: reval? */
1308ab2a 28929+ fi_read_lock(file);
28930+
28931+ err = -EAGAIN;
28932+ if (unlikely(au_ftest_rdu(cookie->flags, CONT)
28933+ && cookie->generation != au_figen(file)))
28934+ goto out_unlock;
28935+
28936+ err = 0;
28937+ if (!rdu->blk) {
28938+ rdu->blk = au_sbi(arg.sb)->si_rdblk;
28939+ if (!rdu->blk)
28940+ rdu->blk = au_dir_size(file, /*dentry*/NULL);
28941+ }
5afbbe0d
AM
28942+ bbot = au_fbtop(file);
28943+ if (cookie->bindex < bbot)
28944+ cookie->bindex = bbot;
28945+ bbot = au_fbbot_dir(file);
28946+ /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
28947+ for (; !err && cookie->bindex <= bbot;
1308ab2a 28948+ cookie->bindex++, cookie->h_pos = 0) {
4a4d8108 28949+ h_file = au_hf_dir(file, cookie->bindex);
1308ab2a 28950+ if (!h_file)
28951+ continue;
28952+
28953+ au_fclr_rdu(cookie->flags, FULL);
28954+ err = au_rdu_do(h_file, &arg);
28955+ AuTraceErr(err);
28956+ if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
28957+ break;
28958+ }
28959+ AuDbg("rent %llu\n", rdu->rent);
28960+
28961+ if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
28962+ rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
28963+ au_fset_rdu(cookie->flags, CONT);
28964+ cookie->generation = au_figen(file);
28965+ }
28966+
28967+ ii_read_lock_child(inode);
5afbbe0d 28968+ fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
1308ab2a 28969+ ii_read_unlock(inode);
28970+
4f0767ce 28971+out_unlock:
1308ab2a 28972+ fi_read_unlock(file);
027c5e7a 28973+out_si:
1308ab2a 28974+ si_read_unlock(arg.sb);
4f0767ce 28975+out_mtx:
5afbbe0d 28976+ inode_unlock_shared(inode);
4f0767ce 28977+out:
1308ab2a 28978+ AuTraceErr(err);
28979+ return err;
28980+}
28981+
28982+static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
28983+{
28984+ int err;
28985+ ino_t ino;
28986+ unsigned long long nent;
28987+ union au_rdu_ent_ul *u;
28988+ struct au_rdu_ent ent;
28989+ struct super_block *sb;
28990+
28991+ err = 0;
28992+ nent = rdu->nent;
28993+ u = &rdu->ent;
2000de60 28994+ sb = file->f_path.dentry->d_sb;
1308ab2a 28995+ si_read_lock(sb, AuLock_FLUSH);
28996+ while (nent-- > 0) {
9dbd164d 28997+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 28998+ err = copy_from_user(&ent, u->e, sizeof(ent));
4a4d8108
AM
28999+ if (!err)
29000+ err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
1308ab2a 29001+ if (unlikely(err)) {
29002+ err = -EFAULT;
29003+ AuTraceErr(err);
29004+ break;
29005+ }
29006+
29007+ /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
29008+ if (!ent.wh)
29009+ err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
29010+ else
29011+ err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
29012+ &ino);
29013+ if (unlikely(err)) {
29014+ AuTraceErr(err);
29015+ break;
29016+ }
29017+
29018+ err = __put_user(ino, &u->e->ino);
29019+ if (unlikely(err)) {
29020+ err = -EFAULT;
29021+ AuTraceErr(err);
29022+ break;
29023+ }
29024+ u->ul += au_rdu_len(ent.nlen);
29025+ }
29026+ si_read_unlock(sb);
29027+
29028+ return err;
29029+}
29030+
29031+/* ---------------------------------------------------------------------- */
29032+
29033+static int au_rdu_verify(struct aufs_rdu *rdu)
29034+{
b752ccd1 29035+ AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
1308ab2a 29036+ "%llu, b%d, 0x%x, g%u}\n",
b752ccd1 29037+ rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
1308ab2a 29038+ rdu->blk,
29039+ rdu->rent, rdu->shwh, rdu->full,
29040+ rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
29041+ rdu->cookie.generation);
dece6358 29042+
b752ccd1 29043+ if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
1308ab2a 29044+ return 0;
dece6358 29045+
b752ccd1
AM
29046+ AuDbg("%u:%u\n",
29047+ rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
1308ab2a 29048+ return -EINVAL;
29049+}
29050+
29051+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
dece6358 29052+{
1308ab2a 29053+ long err, e;
29054+ struct aufs_rdu rdu;
29055+ void __user *p = (void __user *)arg;
dece6358 29056+
1308ab2a 29057+ err = copy_from_user(&rdu, p, sizeof(rdu));
29058+ if (unlikely(err)) {
29059+ err = -EFAULT;
29060+ AuTraceErr(err);
29061+ goto out;
29062+ }
29063+ err = au_rdu_verify(&rdu);
dece6358
AM
29064+ if (unlikely(err))
29065+ goto out;
29066+
1308ab2a 29067+ switch (cmd) {
29068+ case AUFS_CTL_RDU:
29069+ err = au_rdu(file, &rdu);
29070+ if (unlikely(err))
29071+ break;
dece6358 29072+
1308ab2a 29073+ e = copy_to_user(p, &rdu, sizeof(rdu));
29074+ if (unlikely(e)) {
29075+ err = -EFAULT;
29076+ AuTraceErr(err);
29077+ }
29078+ break;
29079+ case AUFS_CTL_RDU_INO:
29080+ err = au_rdu_ino(file, &rdu);
29081+ break;
29082+
29083+ default:
4a4d8108 29084+ /* err = -ENOTTY; */
1308ab2a 29085+ err = -EINVAL;
29086+ }
dece6358 29087+
4f0767ce 29088+out:
1308ab2a 29089+ AuTraceErr(err);
29090+ return err;
1facf9fc 29091+}
b752ccd1
AM
29092+
29093+#ifdef CONFIG_COMPAT
29094+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29095+{
29096+ long err, e;
29097+ struct aufs_rdu rdu;
29098+ void __user *p = compat_ptr(arg);
29099+
29100+ /* todo: get_user()? */
29101+ err = copy_from_user(&rdu, p, sizeof(rdu));
29102+ if (unlikely(err)) {
29103+ err = -EFAULT;
29104+ AuTraceErr(err);
29105+ goto out;
29106+ }
29107+ rdu.ent.e = compat_ptr(rdu.ent.ul);
29108+ err = au_rdu_verify(&rdu);
29109+ if (unlikely(err))
29110+ goto out;
29111+
29112+ switch (cmd) {
29113+ case AUFS_CTL_RDU:
29114+ err = au_rdu(file, &rdu);
29115+ if (unlikely(err))
29116+ break;
29117+
29118+ rdu.ent.ul = ptr_to_compat(rdu.ent.e);
29119+ rdu.tail.ul = ptr_to_compat(rdu.tail.e);
29120+ e = copy_to_user(p, &rdu, sizeof(rdu));
29121+ if (unlikely(e)) {
29122+ err = -EFAULT;
29123+ AuTraceErr(err);
29124+ }
29125+ break;
29126+ case AUFS_CTL_RDU_INO:
29127+ err = au_rdu_ino(file, &rdu);
29128+ break;
29129+
29130+ default:
29131+ /* err = -ENOTTY; */
29132+ err = -EINVAL;
29133+ }
29134+
4f0767ce 29135+out:
b752ccd1
AM
29136+ AuTraceErr(err);
29137+ return err;
29138+}
29139+#endif
7f207e10
AM
29140diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
29141--- /usr/share/empty/fs/aufs/rwsem.h 1970-01-01 01:00:00.000000000 +0100
acd2b654 29142+++ linux/fs/aufs/rwsem.h 2018-10-23 12:33:35.599375796 +0200
062440b3
AM
29143@@ -0,0 +1,73 @@
29144+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 29145+/*
b00004a5 29146+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 29147+ *
29148+ * This program, aufs is free software; you can redistribute it and/or modify
29149+ * it under the terms of the GNU General Public License as published by
29150+ * the Free Software Foundation; either version 2 of the License, or
29151+ * (at your option) any later version.
dece6358
AM
29152+ *
29153+ * This program is distributed in the hope that it will be useful,
29154+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29155+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29156+ * GNU General Public License for more details.
29157+ *
29158+ * You should have received a copy of the GNU General Public License
523b37e3 29159+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29160+ */
29161+
29162+/*
29163+ * simple read-write semaphore wrappers
29164+ */
29165+
29166+#ifndef __AUFS_RWSEM_H__
29167+#define __AUFS_RWSEM_H__
29168+
29169+#ifdef __KERNEL__
29170+
4a4d8108 29171+#include "debug.h"
dece6358 29172+
acd2b654 29173+/* in the future, the name 'au_rwsem' will be totally gone */
8b6a4947 29174+#define au_rwsem rw_semaphore
dece6358
AM
29175+
29176+/* to debug easier, do not make them inlined functions */
8b6a4947 29177+#define AuRwMustNoWaiters(rw) AuDebugOn(rwsem_is_contended(rw))
dece6358 29178+/* rwsem_is_locked() is unusable */
8b6a4947
AM
29179+#define AuRwMustReadLock(rw) AuDebugOn(!lockdep_recursing(current) \
29180+ && debug_locks \
29181+ && !lockdep_is_held_type(rw, 1))
29182+#define AuRwMustWriteLock(rw) AuDebugOn(!lockdep_recursing(current) \
29183+ && debug_locks \
29184+ && !lockdep_is_held_type(rw, 0))
29185+#define AuRwMustAnyLock(rw) AuDebugOn(!lockdep_recursing(current) \
29186+ && debug_locks \
29187+ && !lockdep_is_held(rw))
29188+#define AuRwDestroy(rw) AuDebugOn(!lockdep_recursing(current) \
29189+ && debug_locks \
29190+ && lockdep_is_held(rw))
29191+
29192+#define au_rw_init(rw) init_rwsem(rw)
dece6358 29193+
5afbbe0d
AM
29194+#define au_rw_init_wlock(rw) do { \
29195+ au_rw_init(rw); \
8b6a4947 29196+ down_write(rw); \
5afbbe0d 29197+ } while (0)
dece6358 29198+
8b6a4947
AM
29199+#define au_rw_init_wlock_nested(rw, lsc) do { \
29200+ au_rw_init(rw); \
29201+ down_write_nested(rw, lsc); \
5afbbe0d 29202+ } while (0)
dece6358 29203+
8b6a4947
AM
29204+#define au_rw_read_lock(rw) down_read(rw)
29205+#define au_rw_read_lock_nested(rw, lsc) down_read_nested(rw, lsc)
29206+#define au_rw_read_unlock(rw) up_read(rw)
29207+#define au_rw_dgrade_lock(rw) downgrade_write(rw)
29208+#define au_rw_write_lock(rw) down_write(rw)
29209+#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29210+#define au_rw_write_unlock(rw) up_write(rw)
29211+/* why is not _nested version defined? */
29212+#define au_rw_read_trylock(rw) down_read_trylock(rw)
29213+#define au_rw_write_trylock(rw) down_write_trylock(rw)
1facf9fc 29214+
29215+#endif /* __KERNEL__ */
29216+#endif /* __AUFS_RWSEM_H__ */
7f207e10
AM
29217diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
29218--- /usr/share/empty/fs/aufs/sbinfo.c 1970-01-01 01:00:00.000000000 +0100
9f237c51 29219+++ linux/fs/aufs/sbinfo.c 2018-12-27 13:19:17.711749485 +0100
acd2b654 29220@@ -0,0 +1,313 @@
cd7a4cd9 29221+// SPDX-License-Identifier: GPL-2.0
1facf9fc 29222+/*
b00004a5 29223+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 29224+ *
29225+ * This program, aufs is free software; you can redistribute it and/or modify
29226+ * it under the terms of the GNU General Public License as published by
29227+ * the Free Software Foundation; either version 2 of the License, or
29228+ * (at your option) any later version.
dece6358
AM
29229+ *
29230+ * This program is distributed in the hope that it will be useful,
29231+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29232+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29233+ * GNU General Public License for more details.
29234+ *
29235+ * You should have received a copy of the GNU General Public License
523b37e3 29236+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29237+ */
29238+
29239+/*
29240+ * superblock private data
29241+ */
29242+
29243+#include "aufs.h"
29244+
29245+/*
29246+ * they are necessary regardless sysfs is disabled.
29247+ */
29248+void au_si_free(struct kobject *kobj)
29249+{
86dc4139 29250+ int i;
1facf9fc 29251+ struct au_sbinfo *sbinfo;
b752ccd1 29252+ char *locked __maybe_unused; /* debug only */
1facf9fc 29253+
29254+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
86dc4139 29255+ for (i = 0; i < AuPlink_NHASH; i++)
8b6a4947 29256+ AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
f0c0a007 29257+ AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
5afbbe0d 29258+
acd2b654
AM
29259+ AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29260+ au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29261+ AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29262+ au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
1facf9fc 29263+
062440b3 29264+ dbgaufs_si_fin(sbinfo);
e49829fe 29265+ au_rw_write_lock(&sbinfo->si_rwsem);
1facf9fc 29266+ au_br_free(sbinfo);
e49829fe 29267+ au_rw_write_unlock(&sbinfo->si_rwsem);
b752ccd1 29268+
9f237c51 29269+ au_kfree_try_rcu(sbinfo->si_branch);
1facf9fc 29270+ mutex_destroy(&sbinfo->si_xib_mtx);
dece6358 29271+ AuRwDestroy(&sbinfo->si_rwsem);
1facf9fc 29272+
acd2b654
AM
29273+ au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29274+ /* si_nfiles is waited too */
9f237c51 29275+ au_kfree_rcu(sbinfo);
1facf9fc 29276+}
29277+
29278+int au_si_alloc(struct super_block *sb)
29279+{
86dc4139 29280+ int err, i;
1facf9fc 29281+ struct au_sbinfo *sbinfo;
29282+
29283+ err = -ENOMEM;
4a4d8108 29284+ sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
1facf9fc 29285+ if (unlikely(!sbinfo))
29286+ goto out;
29287+
29288+ /* will be reallocated separately */
29289+ sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29290+ if (unlikely(!sbinfo->si_branch))
febd17d6 29291+ goto out_sbinfo;
1facf9fc 29292+
1facf9fc 29293+ err = sysaufs_si_init(sbinfo);
062440b3
AM
29294+ if (!err) {
29295+ dbgaufs_si_null(sbinfo);
29296+ err = dbgaufs_si_init(sbinfo);
29297+ if (unlikely(err))
29298+ kobject_put(&sbinfo->si_kobj);
29299+ }
1facf9fc 29300+ if (unlikely(err))
29301+ goto out_br;
29302+
29303+ au_nwt_init(&sbinfo->si_nowait);
dece6358 29304+ au_rw_init_wlock(&sbinfo->si_rwsem);
b752ccd1 29305+
acd2b654
AM
29306+ au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29307+ au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
7f207e10 29308+
5afbbe0d 29309+ sbinfo->si_bbot = -1;
392086de 29310+ sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
1facf9fc 29311+
29312+ sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29313+ sbinfo->si_wbr_create = AuWbrCreate_Def;
4a4d8108
AM
29314+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29315+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
1facf9fc 29316+
076b876e
AM
29317+ au_fhsm_init(sbinfo);
29318+
e49829fe 29319+ sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
1facf9fc 29320+
392086de
AM
29321+ sbinfo->si_xino_jiffy = jiffies;
29322+ sbinfo->si_xino_expire
29323+ = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
1facf9fc 29324+ mutex_init(&sbinfo->si_xib_mtx);
1facf9fc 29325+ /* leave si_xib_last_pindex and si_xib_next_bit */
29326+
8b6a4947 29327+ INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
b912730e 29328+
e49829fe 29329+ sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
1facf9fc 29330+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29331+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29332+ sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29333+
86dc4139 29334+ for (i = 0; i < AuPlink_NHASH; i++)
8b6a4947 29335+ INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
1facf9fc 29336+ init_waitqueue_head(&sbinfo->si_plink_wq);
4a4d8108 29337+ spin_lock_init(&sbinfo->si_plink_maint_lock);
1facf9fc 29338+
8b6a4947 29339+ INIT_HLIST_BL_HEAD(&sbinfo->si_files);
523b37e3 29340+
b95c5147
AM
29341+ /* with getattr by default */
29342+ sbinfo->si_iop_array = aufs_iop;
29343+
1facf9fc 29344+ /* leave other members for sysaufs and si_mnt. */
29345+ sbinfo->si_sb = sb;
29346+ sb->s_fs_info = sbinfo;
b752ccd1 29347+ si_pid_set(sb);
1facf9fc 29348+ return 0; /* success */
29349+
4f0767ce 29350+out_br:
9f237c51 29351+ au_kfree_try_rcu(sbinfo->si_branch);
4f0767ce 29352+out_sbinfo:
9f237c51 29353+ au_kfree_rcu(sbinfo);
4f0767ce 29354+out:
1facf9fc 29355+ return err;
29356+}
29357+
e2f27e51 29358+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
1facf9fc 29359+{
29360+ int err, sz;
29361+ struct au_branch **brp;
29362+
dece6358
AM
29363+ AuRwMustWriteLock(&sbinfo->si_rwsem);
29364+
1facf9fc 29365+ err = -ENOMEM;
5afbbe0d 29366+ sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
1facf9fc 29367+ if (unlikely(!sz))
29368+ sz = sizeof(*brp);
e2f27e51
AM
29369+ brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29370+ may_shrink);
1facf9fc 29371+ if (brp) {
29372+ sbinfo->si_branch = brp;
29373+ err = 0;
29374+ }
29375+
29376+ return err;
29377+}
29378+
29379+/* ---------------------------------------------------------------------- */
29380+
29381+unsigned int au_sigen_inc(struct super_block *sb)
29382+{
29383+ unsigned int gen;
5527c038 29384+ struct inode *inode;
1facf9fc 29385+
dece6358
AM
29386+ SiMustWriteLock(sb);
29387+
1facf9fc 29388+ gen = ++au_sbi(sb)->si_generation;
29389+ au_update_digen(sb->s_root);
5527c038
JR
29390+ inode = d_inode(sb->s_root);
29391+ au_update_iigen(inode, /*half*/0);
be118d29 29392+ inode_inc_iversion(inode);
1facf9fc 29393+ return gen;
29394+}
29395+
29396+aufs_bindex_t au_new_br_id(struct super_block *sb)
29397+{
29398+ aufs_bindex_t br_id;
29399+ int i;
29400+ struct au_sbinfo *sbinfo;
29401+
dece6358
AM
29402+ SiMustWriteLock(sb);
29403+
1facf9fc 29404+ sbinfo = au_sbi(sb);
29405+ for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29406+ br_id = ++sbinfo->si_last_br_id;
7f207e10 29407+ AuDebugOn(br_id < 0);
1facf9fc 29408+ if (br_id && au_br_index(sb, br_id) < 0)
29409+ return br_id;
29410+ }
29411+
29412+ return -1;
29413+}
29414+
29415+/* ---------------------------------------------------------------------- */
29416+
e49829fe
JR
29417+/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29418+int si_read_lock(struct super_block *sb, int flags)
29419+{
29420+ int err;
29421+
29422+ err = 0;
29423+ if (au_ftest_lock(flags, FLUSH))
29424+ au_nwt_flush(&au_sbi(sb)->si_nowait);
29425+
29426+ si_noflush_read_lock(sb);
29427+ err = au_plink_maint(sb, flags);
29428+ if (unlikely(err))
29429+ si_read_unlock(sb);
29430+
29431+ return err;
29432+}
29433+
29434+int si_write_lock(struct super_block *sb, int flags)
29435+{
29436+ int err;
29437+
29438+ if (au_ftest_lock(flags, FLUSH))
29439+ au_nwt_flush(&au_sbi(sb)->si_nowait);
29440+
29441+ si_noflush_write_lock(sb);
29442+ err = au_plink_maint(sb, flags);
29443+ if (unlikely(err))
29444+ si_write_unlock(sb);
29445+
29446+ return err;
29447+}
29448+
1facf9fc 29449+/* dentry and super_block lock. call at entry point */
e49829fe 29450+int aufs_read_lock(struct dentry *dentry, int flags)
1facf9fc 29451+{
e49829fe 29452+ int err;
027c5e7a 29453+ struct super_block *sb;
e49829fe 29454+
027c5e7a
AM
29455+ sb = dentry->d_sb;
29456+ err = si_read_lock(sb, flags);
29457+ if (unlikely(err))
29458+ goto out;
29459+
29460+ if (au_ftest_lock(flags, DW))
29461+ di_write_lock_child(dentry);
29462+ else
29463+ di_read_lock_child(dentry, flags);
29464+
29465+ if (au_ftest_lock(flags, GEN)) {
29466+ err = au_digen_test(dentry, au_sigen(sb));
79b8bda9
AM
29467+ if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29468+ AuDebugOn(!err && au_dbrange_test(dentry));
29469+ else if (!err)
29470+ err = au_dbrange_test(dentry);
027c5e7a
AM
29471+ if (unlikely(err))
29472+ aufs_read_unlock(dentry, flags);
e49829fe
JR
29473+ }
29474+
027c5e7a 29475+out:
e49829fe 29476+ return err;
1facf9fc 29477+}
29478+
29479+void aufs_read_unlock(struct dentry *dentry, int flags)
29480+{
29481+ if (au_ftest_lock(flags, DW))
29482+ di_write_unlock(dentry);
29483+ else
29484+ di_read_unlock(dentry, flags);
29485+ si_read_unlock(dentry->d_sb);
29486+}
29487+
29488+void aufs_write_lock(struct dentry *dentry)
29489+{
e49829fe 29490+ si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
1facf9fc 29491+ di_write_lock_child(dentry);
29492+}
29493+
29494+void aufs_write_unlock(struct dentry *dentry)
29495+{
29496+ di_write_unlock(dentry);
29497+ si_write_unlock(dentry->d_sb);
29498+}
29499+
e49829fe 29500+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
1facf9fc 29501+{
e49829fe 29502+ int err;
027c5e7a
AM
29503+ unsigned int sigen;
29504+ struct super_block *sb;
e49829fe 29505+
027c5e7a
AM
29506+ sb = d1->d_sb;
29507+ err = si_read_lock(sb, flags);
29508+ if (unlikely(err))
29509+ goto out;
29510+
b95c5147 29511+ di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
027c5e7a
AM
29512+
29513+ if (au_ftest_lock(flags, GEN)) {
29514+ sigen = au_sigen(sb);
29515+ err = au_digen_test(d1, sigen);
29516+ AuDebugOn(!err && au_dbrange_test(d1));
29517+ if (!err) {
29518+ err = au_digen_test(d2, sigen);
29519+ AuDebugOn(!err && au_dbrange_test(d2));
29520+ }
29521+ if (unlikely(err))
29522+ aufs_read_and_write_unlock2(d1, d2);
29523+ }
29524+
29525+out:
e49829fe 29526+ return err;
1facf9fc 29527+}
29528+
29529+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29530+{
29531+ di_write_unlock2(d1, d2);
29532+ si_read_unlock(d1->d_sb);
29533+}
7f207e10
AM
29534diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29535--- /usr/share/empty/fs/aufs/super.c 1970-01-01 01:00:00.000000000 +0100
acd2b654
AM
29536+++ linux/fs/aufs/super.c 2018-10-23 12:33:35.599375796 +0200
29537@@ -0,0 +1,1048 @@
cd7a4cd9 29538+// SPDX-License-Identifier: GPL-2.0
1facf9fc 29539+/*
b00004a5 29540+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 29541+ *
29542+ * This program, aufs is free software; you can redistribute it and/or modify
29543+ * it under the terms of the GNU General Public License as published by
29544+ * the Free Software Foundation; either version 2 of the License, or
29545+ * (at your option) any later version.
dece6358
AM
29546+ *
29547+ * This program is distributed in the hope that it will be useful,
29548+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29549+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29550+ * GNU General Public License for more details.
29551+ *
29552+ * You should have received a copy of the GNU General Public License
523b37e3 29553+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29554+ */
29555+
29556+/*
29557+ * mount and super_block operations
29558+ */
29559+
f6c5ef8b 29560+#include <linux/mm.h>
1facf9fc 29561+#include <linux/seq_file.h>
29562+#include <linux/statfs.h>
7f207e10 29563+#include <linux/vmalloc.h>
1facf9fc 29564+#include "aufs.h"
29565+
29566+/*
29567+ * super_operations
29568+ */
29569+static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29570+{
29571+ struct au_icntnr *c;
29572+
29573+ c = au_cache_alloc_icntnr();
29574+ if (c) {
027c5e7a 29575+ au_icntnr_init(c);
be118d29 29576+ inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
1facf9fc 29577+ c->iinfo.ii_hinode = NULL;
29578+ return &c->vfs_inode;
29579+ }
29580+ return NULL;
29581+}
29582+
027c5e7a
AM
29583+static void aufs_destroy_inode_cb(struct rcu_head *head)
29584+{
29585+ struct inode *inode = container_of(head, struct inode, i_rcu);
29586+
1c60b727 29587+ au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
027c5e7a
AM
29588+}
29589+
1facf9fc 29590+static void aufs_destroy_inode(struct inode *inode)
29591+{
5afbbe0d
AM
29592+ if (!au_is_bad_inode(inode))
29593+ au_iinfo_fin(inode);
027c5e7a 29594+ call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
1facf9fc 29595+}
29596+
29597+struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29598+{
29599+ struct inode *inode;
29600+ int err;
29601+
29602+ inode = iget_locked(sb, ino);
29603+ if (unlikely(!inode)) {
29604+ inode = ERR_PTR(-ENOMEM);
29605+ goto out;
29606+ }
29607+ if (!(inode->i_state & I_NEW))
29608+ goto out;
29609+
29610+ err = au_xigen_new(inode);
29611+ if (!err)
29612+ err = au_iinfo_init(inode);
29613+ if (!err)
be118d29 29614+ inode_inc_iversion(inode);
1facf9fc 29615+ else {
29616+ iget_failed(inode);
29617+ inode = ERR_PTR(err);
29618+ }
29619+
4f0767ce 29620+out:
1facf9fc 29621+ /* never return NULL */
29622+ AuDebugOn(!inode);
29623+ AuTraceErrPtr(inode);
29624+ return inode;
29625+}
29626+
29627+/* lock free root dinfo */
29628+static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29629+{
29630+ int err;
5afbbe0d 29631+ aufs_bindex_t bindex, bbot;
1facf9fc 29632+ struct path path;
4a4d8108 29633+ struct au_hdentry *hdp;
1facf9fc 29634+ struct au_branch *br;
076b876e 29635+ au_br_perm_str_t perm;
1facf9fc 29636+
29637+ err = 0;
5afbbe0d
AM
29638+ bbot = au_sbbot(sb);
29639+ bindex = 0;
29640+ hdp = au_hdentry(au_di(sb->s_root), bindex);
29641+ for (; !err && bindex <= bbot; bindex++, hdp++) {
1facf9fc 29642+ br = au_sbr(sb, bindex);
86dc4139 29643+ path.mnt = au_br_mnt(br);
5afbbe0d 29644+ path.dentry = hdp->hd_dentry;
1facf9fc 29645+ err = au_seq_path(seq, &path);
79b8bda9 29646+ if (!err) {
076b876e 29647+ au_optstr_br_perm(&perm, br->br_perm);
79b8bda9 29648+ seq_printf(seq, "=%s", perm.a);
5afbbe0d 29649+ if (bindex != bbot)
79b8bda9 29650+ seq_putc(seq, ':');
1e00d052 29651+ }
1facf9fc 29652+ }
79b8bda9
AM
29653+ if (unlikely(err || seq_has_overflowed(seq)))
29654+ err = -E2BIG;
1facf9fc 29655+
29656+ return err;
29657+}
29658+
f2c43d5f
AM
29659+static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29660+ const char *append)
29661+{
29662+ char *p;
29663+
29664+ p = fmt;
29665+ while (*pat != ':')
29666+ *p++ = *pat++;
29667+ *p++ = *pat++;
29668+ strcpy(p, append);
29669+ AuDebugOn(strlen(fmt) >= len);
29670+}
29671+
1facf9fc 29672+static void au_show_wbr_create(struct seq_file *m, int v,
29673+ struct au_sbinfo *sbinfo)
29674+{
29675+ const char *pat;
f2c43d5f
AM
29676+ char fmt[32];
29677+ struct au_wbr_mfs *mfs;
1facf9fc 29678+
dece6358
AM
29679+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29680+
c2b27bf2 29681+ seq_puts(m, ",create=");
1facf9fc 29682+ pat = au_optstr_wbr_create(v);
f2c43d5f 29683+ mfs = &sbinfo->si_wbr_mfs;
1facf9fc 29684+ switch (v) {
29685+ case AuWbrCreate_TDP:
29686+ case AuWbrCreate_RR:
29687+ case AuWbrCreate_MFS:
29688+ case AuWbrCreate_PMFS:
c2b27bf2 29689+ seq_puts(m, pat);
1facf9fc 29690+ break;
f2c43d5f
AM
29691+ case AuWbrCreate_MFSRR:
29692+ case AuWbrCreate_TDMFS:
29693+ case AuWbrCreate_PMFSRR:
29694+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29695+ seq_printf(m, fmt, mfs->mfsrr_watermark);
1facf9fc 29696+ break;
f2c43d5f 29697+ case AuWbrCreate_MFSV:
1facf9fc 29698+ case AuWbrCreate_PMFSV:
f2c43d5f
AM
29699+ au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29700+ seq_printf(m, fmt,
29701+ jiffies_to_msecs(mfs->mfs_expire)
e49829fe 29702+ / MSEC_PER_SEC);
1facf9fc 29703+ break;
1facf9fc 29704+ case AuWbrCreate_MFSRRV:
f2c43d5f 29705+ case AuWbrCreate_TDMFSV:
392086de 29706+ case AuWbrCreate_PMFSRRV:
f2c43d5f
AM
29707+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29708+ seq_printf(m, fmt, mfs->mfsrr_watermark,
29709+ jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
392086de 29710+ break;
f2c43d5f
AM
29711+ default:
29712+ BUG();
1facf9fc 29713+ }
29714+}
29715+
7eafdf33 29716+static int au_show_xino(struct seq_file *seq, struct super_block *sb)
1facf9fc 29717+{
29718+#ifdef CONFIG_SYSFS
29719+ return 0;
29720+#else
29721+ int err;
29722+ const int len = sizeof(AUFS_XINO_FNAME) - 1;
29723+ aufs_bindex_t bindex, brid;
1facf9fc 29724+ struct qstr *name;
29725+ struct file *f;
29726+ struct dentry *d, *h_root;
acd2b654 29727+ struct au_branch *br;
1facf9fc 29728+
dece6358
AM
29729+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29730+
1facf9fc 29731+ err = 0;
1facf9fc 29732+ f = au_sbi(sb)->si_xib;
29733+ if (!f)
29734+ goto out;
29735+
29736+ /* stop printing the default xino path on the first writable branch */
29737+ h_root = NULL;
acd2b654
AM
29738+ bindex = au_xi_root(sb, f->f_path.dentry);
29739+ if (bindex >= 0) {
29740+ br = au_sbr_sb(sb, bindex);
29741+ h_root = au_br_dentry(br);
1facf9fc 29742+ }
acd2b654 29743+
2000de60 29744+ d = f->f_path.dentry;
1facf9fc 29745+ name = &d->d_name;
29746+ /* safe ->d_parent because the file is unlinked */
29747+ if (d->d_parent == h_root
29748+ && name->len == len
29749+ && !memcmp(name->name, AUFS_XINO_FNAME, len))
29750+ goto out;
29751+
29752+ seq_puts(seq, ",xino=");
29753+ err = au_xino_path(seq, f);
29754+
4f0767ce 29755+out:
1facf9fc 29756+ return err;
29757+#endif
29758+}
29759+
29760+/* seq_file will re-call me in case of too long string */
7eafdf33 29761+static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
1facf9fc 29762+{
027c5e7a 29763+ int err;
1facf9fc 29764+ unsigned int mnt_flags, v;
29765+ struct super_block *sb;
29766+ struct au_sbinfo *sbinfo;
29767+
29768+#define AuBool(name, str) do { \
29769+ v = au_opt_test(mnt_flags, name); \
29770+ if (v != au_opt_test(AuOpt_Def, name)) \
29771+ seq_printf(m, ",%s" #str, v ? "" : "no"); \
29772+} while (0)
29773+
29774+#define AuStr(name, str) do { \
29775+ v = mnt_flags & AuOptMask_##name; \
29776+ if (v != (AuOpt_Def & AuOptMask_##name)) \
29777+ seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
29778+} while (0)
29779+
29780+#define AuUInt(name, str, val) do { \
29781+ if (val != AUFS_##name##_DEF) \
29782+ seq_printf(m, "," #str "=%u", val); \
29783+} while (0)
29784+
7eafdf33 29785+ sb = dentry->d_sb;
2121bcd9 29786+ if (sb->s_flags & SB_POSIXACL)
c1595e42 29787+ seq_puts(m, ",acl");
be118d29
JR
29788+#if 0
29789+ if (sb->s_flags & SB_I_VERSION)
29790+ seq_puts(m, ",i_version");
29791+#endif
c1595e42
JR
29792+
29793+ /* lock free root dinfo */
1facf9fc 29794+ si_noflush_read_lock(sb);
29795+ sbinfo = au_sbi(sb);
29796+ seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
29797+
29798+ mnt_flags = au_mntflags(sb);
29799+ if (au_opt_test(mnt_flags, XINO)) {
7eafdf33 29800+ err = au_show_xino(m, sb);
1facf9fc 29801+ if (unlikely(err))
29802+ goto out;
29803+ } else
29804+ seq_puts(m, ",noxino");
29805+
29806+ AuBool(TRUNC_XINO, trunc_xino);
29807+ AuStr(UDBA, udba);
dece6358 29808+ AuBool(SHWH, shwh);
1facf9fc 29809+ AuBool(PLINK, plink);
4a4d8108 29810+ AuBool(DIO, dio);
076b876e 29811+ AuBool(DIRPERM1, dirperm1);
1facf9fc 29812+
29813+ v = sbinfo->si_wbr_create;
29814+ if (v != AuWbrCreate_Def)
29815+ au_show_wbr_create(m, v, sbinfo);
29816+
29817+ v = sbinfo->si_wbr_copyup;
29818+ if (v != AuWbrCopyup_Def)
29819+ seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
29820+
29821+ v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
29822+ if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
29823+ seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
29824+
29825+ AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
29826+
027c5e7a
AM
29827+ v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
29828+ AuUInt(RDCACHE, rdcache, v);
1facf9fc 29829+
29830+ AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
29831+ AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
29832+
076b876e
AM
29833+ au_fhsm_show(m, sbinfo);
29834+
8b6a4947 29835+ AuBool(DIRREN, dirren);
1facf9fc 29836+ AuBool(SUM, sum);
29837+ /* AuBool(SUM_W, wsum); */
29838+ AuBool(WARN_PERM, warn_perm);
29839+ AuBool(VERBOSE, verbose);
29840+
4f0767ce 29841+out:
1facf9fc 29842+ /* be sure to print "br:" last */
29843+ if (!sysaufs_brs) {
29844+ seq_puts(m, ",br:");
29845+ au_show_brs(m, sb);
29846+ }
29847+ si_read_unlock(sb);
29848+ return 0;
29849+
1facf9fc 29850+#undef AuBool
29851+#undef AuStr
4a4d8108 29852+#undef AuUInt
1facf9fc 29853+}
29854+
29855+/* ---------------------------------------------------------------------- */
29856+
29857+/* sum mode which returns the summation for statfs(2) */
29858+
29859+static u64 au_add_till_max(u64 a, u64 b)
29860+{
29861+ u64 old;
29862+
29863+ old = a;
29864+ a += b;
92d182d2
AM
29865+ if (old <= a)
29866+ return a;
29867+ return ULLONG_MAX;
29868+}
29869+
29870+static u64 au_mul_till_max(u64 a, long mul)
29871+{
29872+ u64 old;
29873+
29874+ old = a;
29875+ a *= mul;
29876+ if (old <= a)
1facf9fc 29877+ return a;
29878+ return ULLONG_MAX;
29879+}
29880+
29881+static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
29882+{
29883+ int err;
92d182d2 29884+ long bsize, factor;
1facf9fc 29885+ u64 blocks, bfree, bavail, files, ffree;
5afbbe0d 29886+ aufs_bindex_t bbot, bindex, i;
1facf9fc 29887+ unsigned char shared;
7f207e10 29888+ struct path h_path;
1facf9fc 29889+ struct super_block *h_sb;
29890+
92d182d2
AM
29891+ err = 0;
29892+ bsize = LONG_MAX;
29893+ files = 0;
29894+ ffree = 0;
1facf9fc 29895+ blocks = 0;
29896+ bfree = 0;
29897+ bavail = 0;
5afbbe0d
AM
29898+ bbot = au_sbbot(sb);
29899+ for (bindex = 0; bindex <= bbot; bindex++) {
7f207e10
AM
29900+ h_path.mnt = au_sbr_mnt(sb, bindex);
29901+ h_sb = h_path.mnt->mnt_sb;
1facf9fc 29902+ shared = 0;
92d182d2 29903+ for (i = 0; !shared && i < bindex; i++)
1facf9fc 29904+ shared = (au_sbr_sb(sb, i) == h_sb);
29905+ if (shared)
29906+ continue;
29907+
29908+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
29909+ h_path.dentry = h_path.mnt->mnt_root;
29910+ err = vfs_statfs(&h_path, buf);
1facf9fc 29911+ if (unlikely(err))
29912+ goto out;
29913+
92d182d2
AM
29914+ if (bsize > buf->f_bsize) {
29915+ /*
29916+ * we will reduce bsize, so we have to expand blocks
29917+ * etc. to match them again
29918+ */
29919+ factor = (bsize / buf->f_bsize);
29920+ blocks = au_mul_till_max(blocks, factor);
29921+ bfree = au_mul_till_max(bfree, factor);
29922+ bavail = au_mul_till_max(bavail, factor);
29923+ bsize = buf->f_bsize;
29924+ }
29925+
29926+ factor = (buf->f_bsize / bsize);
29927+ blocks = au_add_till_max(blocks,
29928+ au_mul_till_max(buf->f_blocks, factor));
29929+ bfree = au_add_till_max(bfree,
29930+ au_mul_till_max(buf->f_bfree, factor));
29931+ bavail = au_add_till_max(bavail,
29932+ au_mul_till_max(buf->f_bavail, factor));
1facf9fc 29933+ files = au_add_till_max(files, buf->f_files);
29934+ ffree = au_add_till_max(ffree, buf->f_ffree);
29935+ }
29936+
92d182d2 29937+ buf->f_bsize = bsize;
1facf9fc 29938+ buf->f_blocks = blocks;
29939+ buf->f_bfree = bfree;
29940+ buf->f_bavail = bavail;
29941+ buf->f_files = files;
29942+ buf->f_ffree = ffree;
92d182d2 29943+ buf->f_frsize = 0;
1facf9fc 29944+
4f0767ce 29945+out:
1facf9fc 29946+ return err;
29947+}
29948+
29949+static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
29950+{
29951+ int err;
7f207e10 29952+ struct path h_path;
1facf9fc 29953+ struct super_block *sb;
29954+
29955+ /* lock free root dinfo */
29956+ sb = dentry->d_sb;
29957+ si_noflush_read_lock(sb);
7f207e10 29958+ if (!au_opt_test(au_mntflags(sb), SUM)) {
1facf9fc 29959+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
29960+ h_path.mnt = au_sbr_mnt(sb, 0);
29961+ h_path.dentry = h_path.mnt->mnt_root;
29962+ err = vfs_statfs(&h_path, buf);
29963+ } else
1facf9fc 29964+ err = au_statfs_sum(sb, buf);
29965+ si_read_unlock(sb);
29966+
29967+ if (!err) {
29968+ buf->f_type = AUFS_SUPER_MAGIC;
4a4d8108 29969+ buf->f_namelen = AUFS_MAX_NAMELEN;
1facf9fc 29970+ memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
29971+ }
29972+ /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
29973+
29974+ return err;
29975+}
29976+
29977+/* ---------------------------------------------------------------------- */
29978+
537831f9
AM
29979+static int aufs_sync_fs(struct super_block *sb, int wait)
29980+{
29981+ int err, e;
5afbbe0d 29982+ aufs_bindex_t bbot, bindex;
537831f9
AM
29983+ struct au_branch *br;
29984+ struct super_block *h_sb;
29985+
29986+ err = 0;
29987+ si_noflush_read_lock(sb);
5afbbe0d
AM
29988+ bbot = au_sbbot(sb);
29989+ for (bindex = 0; bindex <= bbot; bindex++) {
537831f9
AM
29990+ br = au_sbr(sb, bindex);
29991+ if (!au_br_writable(br->br_perm))
29992+ continue;
29993+
29994+ h_sb = au_sbr_sb(sb, bindex);
a2654f78
AM
29995+ e = vfsub_sync_filesystem(h_sb, wait);
29996+ if (unlikely(e && !err))
29997+ err = e;
29998+ /* go on even if an error happens */
537831f9
AM
29999+ }
30000+ si_read_unlock(sb);
30001+
30002+ return err;
30003+}
30004+
30005+/* ---------------------------------------------------------------------- */
30006+
1facf9fc 30007+/* final actions when unmounting a file system */
30008+static void aufs_put_super(struct super_block *sb)
30009+{
30010+ struct au_sbinfo *sbinfo;
30011+
30012+ sbinfo = au_sbi(sb);
062440b3
AM
30013+ if (sbinfo)
30014+ kobject_put(&sbinfo->si_kobj);
1facf9fc 30015+}
30016+
30017+/* ---------------------------------------------------------------------- */
30018+
79b8bda9
AM
30019+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30020+ struct super_block *sb, void *arg)
7f207e10
AM
30021+{
30022+ void *array;
076b876e 30023+ unsigned long long n, sz;
7f207e10
AM
30024+
30025+ array = NULL;
30026+ n = 0;
30027+ if (!*hint)
30028+ goto out;
30029+
30030+ if (*hint > ULLONG_MAX / sizeof(array)) {
30031+ array = ERR_PTR(-EMFILE);
30032+ pr_err("hint %llu\n", *hint);
30033+ goto out;
30034+ }
30035+
076b876e
AM
30036+ sz = sizeof(array) * *hint;
30037+ array = kzalloc(sz, GFP_NOFS);
7f207e10 30038+ if (unlikely(!array))
076b876e 30039+ array = vzalloc(sz);
7f207e10
AM
30040+ if (unlikely(!array)) {
30041+ array = ERR_PTR(-ENOMEM);
30042+ goto out;
30043+ }
30044+
79b8bda9 30045+ n = cb(sb, array, *hint, arg);
7f207e10
AM
30046+ AuDebugOn(n > *hint);
30047+
30048+out:
30049+ *hint = n;
30050+ return array;
30051+}
30052+
79b8bda9 30053+static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
7f207e10
AM
30054+ unsigned long long max __maybe_unused,
30055+ void *arg)
30056+{
30057+ unsigned long long n;
30058+ struct inode **p, *inode;
30059+ struct list_head *head;
30060+
30061+ n = 0;
30062+ p = a;
30063+ head = arg;
79b8bda9 30064+ spin_lock(&sb->s_inode_list_lock);
7f207e10 30065+ list_for_each_entry(inode, head, i_sb_list) {
5afbbe0d
AM
30066+ if (!au_is_bad_inode(inode)
30067+ && au_ii(inode)->ii_btop >= 0) {
2cbb1c4b
JR
30068+ spin_lock(&inode->i_lock);
30069+ if (atomic_read(&inode->i_count)) {
30070+ au_igrab(inode);
30071+ *p++ = inode;
30072+ n++;
30073+ AuDebugOn(n > max);
30074+ }
30075+ spin_unlock(&inode->i_lock);
7f207e10
AM
30076+ }
30077+ }
79b8bda9 30078+ spin_unlock(&sb->s_inode_list_lock);
7f207e10
AM
30079+
30080+ return n;
30081+}
30082+
30083+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
30084+{
acd2b654
AM
30085+ struct au_sbinfo *sbi;
30086+
30087+ sbi = au_sbi(sb);
30088+ *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
79b8bda9 30089+ return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
7f207e10
AM
30090+}
30091+
30092+void au_iarray_free(struct inode **a, unsigned long long max)
30093+{
30094+ unsigned long long ull;
30095+
30096+ for (ull = 0; ull < max; ull++)
30097+ iput(a[ull]);
be52b249 30098+ kvfree(a);
7f207e10
AM
30099+}
30100+
30101+/* ---------------------------------------------------------------------- */
30102+
1facf9fc 30103+/*
30104+ * refresh dentry and inode at remount time.
30105+ */
027c5e7a
AM
30106+/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
30107+static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
30108+ struct dentry *parent)
1facf9fc 30109+{
30110+ int err;
1facf9fc 30111+
30112+ di_write_lock_child(dentry);
1facf9fc 30113+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
30114+ err = au_refresh_dentry(dentry, parent);
30115+ if (!err && dir_flags)
5527c038 30116+ au_hn_reset(d_inode(dentry), dir_flags);
1facf9fc 30117+ di_read_unlock(parent, AuLock_IR);
1facf9fc 30118+ di_write_unlock(dentry);
30119+
30120+ return err;
30121+}
30122+
027c5e7a
AM
30123+static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
30124+ struct au_sbinfo *sbinfo,
b95c5147 30125+ const unsigned int dir_flags, unsigned int do_idop)
1facf9fc 30126+{
027c5e7a
AM
30127+ int err;
30128+ struct dentry *parent;
027c5e7a
AM
30129+
30130+ err = 0;
30131+ parent = dget_parent(dentry);
30132+ if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
5527c038
JR
30133+ if (d_really_is_positive(dentry)) {
30134+ if (!d_is_dir(dentry))
027c5e7a
AM
30135+ err = au_do_refresh(dentry, /*dir_flags*/0,
30136+ parent);
30137+ else {
30138+ err = au_do_refresh(dentry, dir_flags, parent);
30139+ if (unlikely(err))
30140+ au_fset_si(sbinfo, FAILED_REFRESH_DIR);
30141+ }
30142+ } else
30143+ err = au_do_refresh(dentry, /*dir_flags*/0, parent);
30144+ AuDbgDentry(dentry);
30145+ }
30146+ dput(parent);
30147+
79b8bda9 30148+ if (!err) {
b95c5147 30149+ if (do_idop)
79b8bda9
AM
30150+ au_refresh_dop(dentry, /*force_reval*/0);
30151+ } else
30152+ au_refresh_dop(dentry, /*force_reval*/1);
30153+
027c5e7a
AM
30154+ AuTraceErr(err);
30155+ return err;
1facf9fc 30156+}
30157+
b95c5147 30158+static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
1facf9fc 30159+{
30160+ int err, i, j, ndentry, e;
027c5e7a 30161+ unsigned int sigen;
1facf9fc 30162+ struct au_dcsub_pages dpages;
30163+ struct au_dpage *dpage;
027c5e7a
AM
30164+ struct dentry **dentries, *d;
30165+ struct au_sbinfo *sbinfo;
30166+ struct dentry *root = sb->s_root;
5527c038 30167+ const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
1facf9fc 30168+
b95c5147 30169+ if (do_idop)
79b8bda9
AM
30170+ au_refresh_dop(root, /*force_reval*/0);
30171+
027c5e7a
AM
30172+ err = au_dpages_init(&dpages, GFP_NOFS);
30173+ if (unlikely(err))
1facf9fc 30174+ goto out;
027c5e7a
AM
30175+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
30176+ if (unlikely(err))
1facf9fc 30177+ goto out_dpages;
1facf9fc 30178+
027c5e7a
AM
30179+ sigen = au_sigen(sb);
30180+ sbinfo = au_sbi(sb);
30181+ for (i = 0; i < dpages.ndpage; i++) {
1facf9fc 30182+ dpage = dpages.dpages + i;
30183+ dentries = dpage->dentries;
30184+ ndentry = dpage->ndentry;
027c5e7a 30185+ for (j = 0; j < ndentry; j++) {
1facf9fc 30186+ d = dentries[j];
79b8bda9 30187+ e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
b95c5147 30188+ do_idop);
027c5e7a
AM
30189+ if (unlikely(e && !err))
30190+ err = e;
30191+ /* go on even err */
1facf9fc 30192+ }
30193+ }
30194+
4f0767ce 30195+out_dpages:
1facf9fc 30196+ au_dpages_free(&dpages);
4f0767ce 30197+out:
1facf9fc 30198+ return err;
30199+}
30200+
b95c5147 30201+static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
1facf9fc 30202+{
027c5e7a
AM
30203+ int err, e;
30204+ unsigned int sigen;
30205+ unsigned long long max, ull;
30206+ struct inode *inode, **array;
1facf9fc 30207+
027c5e7a
AM
30208+ array = au_iarray_alloc(sb, &max);
30209+ err = PTR_ERR(array);
30210+ if (IS_ERR(array))
30211+ goto out;
1facf9fc 30212+
30213+ err = 0;
027c5e7a
AM
30214+ sigen = au_sigen(sb);
30215+ for (ull = 0; ull < max; ull++) {
30216+ inode = array[ull];
076b876e
AM
30217+ if (unlikely(!inode))
30218+ break;
b95c5147
AM
30219+
30220+ e = 0;
30221+ ii_write_lock_child(inode);
537831f9 30222+ if (au_iigen(inode, NULL) != sigen) {
027c5e7a 30223+ e = au_refresh_hinode_self(inode);
1facf9fc 30224+ if (unlikely(e)) {
b95c5147 30225+ au_refresh_iop(inode, /*force_getattr*/1);
027c5e7a 30226+ pr_err("error %d, i%lu\n", e, inode->i_ino);
1facf9fc 30227+ if (!err)
30228+ err = e;
30229+ /* go on even if err */
30230+ }
30231+ }
b95c5147
AM
30232+ if (!e && do_idop)
30233+ au_refresh_iop(inode, /*force_getattr*/0);
30234+ ii_write_unlock(inode);
1facf9fc 30235+ }
30236+
027c5e7a 30237+ au_iarray_free(array, max);
1facf9fc 30238+
4f0767ce 30239+out:
1facf9fc 30240+ return err;
30241+}
30242+
b95c5147 30243+static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
1facf9fc 30244+{
027c5e7a
AM
30245+ int err, e;
30246+ unsigned int udba;
5afbbe0d 30247+ aufs_bindex_t bindex, bbot;
1facf9fc 30248+ struct dentry *root;
30249+ struct inode *inode;
027c5e7a 30250+ struct au_branch *br;
79b8bda9 30251+ struct au_sbinfo *sbi;
1facf9fc 30252+
30253+ au_sigen_inc(sb);
79b8bda9
AM
30254+ sbi = au_sbi(sb);
30255+ au_fclr_si(sbi, FAILED_REFRESH_DIR);
1facf9fc 30256+
30257+ root = sb->s_root;
30258+ DiMustNoWaiters(root);
5527c038 30259+ inode = d_inode(root);
1facf9fc 30260+ IiMustNoWaiters(inode);
1facf9fc 30261+
027c5e7a 30262+ udba = au_opt_udba(sb);
5afbbe0d
AM
30263+ bbot = au_sbbot(sb);
30264+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
30265+ br = au_sbr(sb, bindex);
30266+ err = au_hnotify_reset_br(udba, br, br->br_perm);
1facf9fc 30267+ if (unlikely(err))
027c5e7a
AM
30268+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
30269+ bindex, err);
30270+ /* go on even if err */
1facf9fc 30271+ }
027c5e7a 30272+ au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
1facf9fc 30273+
b95c5147 30274+ if (do_idop) {
79b8bda9
AM
30275+ if (au_ftest_si(sbi, NO_DREVAL)) {
30276+ AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30277+ sb->s_d_op = &aufs_dop_noreval;
b95c5147
AM
30278+ AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30279+ sbi->si_iop_array = aufs_iop_nogetattr;
79b8bda9
AM
30280+ } else {
30281+ AuDebugOn(sb->s_d_op == &aufs_dop);
30282+ sb->s_d_op = &aufs_dop;
b95c5147
AM
30283+ AuDebugOn(sbi->si_iop_array == aufs_iop);
30284+ sbi->si_iop_array = aufs_iop;
79b8bda9 30285+ }
062440b3 30286+ pr_info("reset to %ps and %ps\n",
b95c5147 30287+ sb->s_d_op, sbi->si_iop_array);
79b8bda9
AM
30288+ }
30289+
027c5e7a 30290+ di_write_unlock(root);
b95c5147
AM
30291+ err = au_refresh_d(sb, do_idop);
30292+ e = au_refresh_i(sb, do_idop);
027c5e7a
AM
30293+ if (unlikely(e && !err))
30294+ err = e;
1facf9fc 30295+ /* aufs_write_lock() calls ..._child() */
30296+ di_write_lock_child(root);
027c5e7a
AM
30297+
30298+ au_cpup_attr_all(inode, /*force*/1);
30299+
30300+ if (unlikely(err))
30301+ AuIOErr("refresh failed, ignored, %d\n", err);
1facf9fc 30302+}
30303+
30304+/* stop extra interpretation of errno in mount(8), and strange error messages */
30305+static int cvt_err(int err)
30306+{
30307+ AuTraceErr(err);
30308+
30309+ switch (err) {
30310+ case -ENOENT:
30311+ case -ENOTDIR:
30312+ case -EEXIST:
30313+ case -EIO:
30314+ err = -EINVAL;
30315+ }
30316+ return err;
30317+}
30318+
30319+static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
30320+{
4a4d8108
AM
30321+ int err, do_dx;
30322+ unsigned int mntflags;
be52b249
AM
30323+ struct au_opts opts = {
30324+ .opt = NULL
30325+ };
1facf9fc 30326+ struct dentry *root;
30327+ struct inode *inode;
30328+ struct au_sbinfo *sbinfo;
30329+
30330+ err = 0;
30331+ root = sb->s_root;
30332+ if (!data || !*data) {
e49829fe
JR
30333+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30334+ if (!err) {
30335+ di_write_lock_child(root);
30336+ err = au_opts_verify(sb, *flags, /*pending*/0);
30337+ aufs_write_unlock(root);
30338+ }
1facf9fc 30339+ goto out;
30340+ }
30341+
30342+ err = -ENOMEM;
1facf9fc 30343+ opts.opt = (void *)__get_free_page(GFP_NOFS);
30344+ if (unlikely(!opts.opt))
30345+ goto out;
30346+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30347+ opts.flags = AuOpts_REMOUNT;
30348+ opts.sb_flags = *flags;
30349+
30350+ /* parse it before aufs lock */
30351+ err = au_opts_parse(sb, data, &opts);
30352+ if (unlikely(err))
30353+ goto out_opts;
30354+
30355+ sbinfo = au_sbi(sb);
5527c038 30356+ inode = d_inode(root);
febd17d6 30357+ inode_lock(inode);
e49829fe
JR
30358+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30359+ if (unlikely(err))
30360+ goto out_mtx;
30361+ di_write_lock_child(root);
1facf9fc 30362+
30363+ /* au_opts_remount() may return an error */
30364+ err = au_opts_remount(sb, &opts);
30365+ au_opts_free(&opts);
30366+
027c5e7a 30367+ if (au_ftest_opts(opts.flags, REFRESH))
b95c5147 30368+ au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
1facf9fc 30369+
4a4d8108
AM
30370+ if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
30371+ mntflags = au_mntflags(sb);
30372+ do_dx = !!au_opt_test(mntflags, DIO);
30373+ au_dy_arefresh(do_dx);
30374+ }
30375+
076b876e 30376+ au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
1facf9fc 30377+ aufs_write_unlock(root);
953406b4 30378+
e49829fe 30379+out_mtx:
febd17d6 30380+ inode_unlock(inode);
4f0767ce 30381+out_opts:
1c60b727 30382+ free_page((unsigned long)opts.opt);
4f0767ce 30383+out:
1facf9fc 30384+ err = cvt_err(err);
30385+ AuTraceErr(err);
30386+ return err;
30387+}
30388+
4a4d8108 30389+static const struct super_operations aufs_sop = {
1facf9fc 30390+ .alloc_inode = aufs_alloc_inode,
30391+ .destroy_inode = aufs_destroy_inode,
b752ccd1 30392+ /* always deleting, no clearing */
1facf9fc 30393+ .drop_inode = generic_delete_inode,
30394+ .show_options = aufs_show_options,
30395+ .statfs = aufs_statfs,
30396+ .put_super = aufs_put_super,
537831f9 30397+ .sync_fs = aufs_sync_fs,
1facf9fc 30398+ .remount_fs = aufs_remount_fs
30399+};
30400+
30401+/* ---------------------------------------------------------------------- */
30402+
30403+static int alloc_root(struct super_block *sb)
30404+{
30405+ int err;
30406+ struct inode *inode;
30407+ struct dentry *root;
30408+
30409+ err = -ENOMEM;
30410+ inode = au_iget_locked(sb, AUFS_ROOT_INO);
30411+ err = PTR_ERR(inode);
30412+ if (IS_ERR(inode))
30413+ goto out;
30414+
b95c5147 30415+ inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
1facf9fc 30416+ inode->i_fop = &aufs_dir_fop;
30417+ inode->i_mode = S_IFDIR;
9dbd164d 30418+ set_nlink(inode, 2);
1facf9fc 30419+ unlock_new_inode(inode);
30420+
92d182d2 30421+ root = d_make_root(inode);
1facf9fc 30422+ if (unlikely(!root))
92d182d2 30423+ goto out;
1facf9fc 30424+ err = PTR_ERR(root);
30425+ if (IS_ERR(root))
92d182d2 30426+ goto out;
1facf9fc 30427+
4a4d8108 30428+ err = au_di_init(root);
1facf9fc 30429+ if (!err) {
30430+ sb->s_root = root;
30431+ return 0; /* success */
30432+ }
30433+ dput(root);
1facf9fc 30434+
4f0767ce 30435+out:
1facf9fc 30436+ return err;
1facf9fc 30437+}
30438+
30439+static int aufs_fill_super(struct super_block *sb, void *raw_data,
30440+ int silent __maybe_unused)
30441+{
30442+ int err;
be52b249
AM
30443+ struct au_opts opts = {
30444+ .opt = NULL
30445+ };
79b8bda9 30446+ struct au_sbinfo *sbinfo;
1facf9fc 30447+ struct dentry *root;
30448+ struct inode *inode;
30449+ char *arg = raw_data;
30450+
30451+ if (unlikely(!arg || !*arg)) {
30452+ err = -EINVAL;
4a4d8108 30453+ pr_err("no arg\n");
1facf9fc 30454+ goto out;
30455+ }
30456+
30457+ err = -ENOMEM;
1facf9fc 30458+ opts.opt = (void *)__get_free_page(GFP_NOFS);
30459+ if (unlikely(!opts.opt))
30460+ goto out;
30461+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30462+ opts.sb_flags = sb->s_flags;
30463+
30464+ err = au_si_alloc(sb);
30465+ if (unlikely(err))
30466+ goto out_opts;
79b8bda9 30467+ sbinfo = au_sbi(sb);
1facf9fc 30468+
30469+ /* all timestamps always follow the ones on the branch */
2121bcd9 30470+ sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
be118d29 30471+ sb->s_flags |= SB_I_VERSION; /* do we really need this? */
1facf9fc 30472+ sb->s_op = &aufs_sop;
027c5e7a 30473+ sb->s_d_op = &aufs_dop;
1facf9fc 30474+ sb->s_magic = AUFS_SUPER_MAGIC;
30475+ sb->s_maxbytes = 0;
c1595e42 30476+ sb->s_stack_depth = 1;
1facf9fc 30477+ au_export_init(sb);
f2c43d5f 30478+ au_xattr_init(sb);
1facf9fc 30479+
30480+ err = alloc_root(sb);
30481+ if (unlikely(err)) {
30482+ si_write_unlock(sb);
30483+ goto out_info;
30484+ }
30485+ root = sb->s_root;
5527c038 30486+ inode = d_inode(root);
1facf9fc 30487+
30488+ /*
30489+ * actually we can parse options regardless aufs lock here.
30490+ * but at remount time, parsing must be done before aufs lock.
30491+ * so we follow the same rule.
30492+ */
30493+ ii_write_lock_parent(inode);
30494+ aufs_write_unlock(root);
30495+ err = au_opts_parse(sb, arg, &opts);
30496+ if (unlikely(err))
30497+ goto out_root;
30498+
30499+ /* lock vfs_inode first, then aufs. */
febd17d6 30500+ inode_lock(inode);
1facf9fc 30501+ aufs_write_lock(root);
30502+ err = au_opts_mount(sb, &opts);
30503+ au_opts_free(&opts);
79b8bda9
AM
30504+ if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
30505+ sb->s_d_op = &aufs_dop_noreval;
062440b3 30506+ pr_info("%ps\n", sb->s_d_op);
79b8bda9 30507+ au_refresh_dop(root, /*force_reval*/0);
b95c5147
AM
30508+ sbinfo->si_iop_array = aufs_iop_nogetattr;
30509+ au_refresh_iop(inode, /*force_getattr*/0);
79b8bda9 30510+ }
1facf9fc 30511+ aufs_write_unlock(root);
febd17d6 30512+ inode_unlock(inode);
4a4d8108
AM
30513+ if (!err)
30514+ goto out_opts; /* success */
1facf9fc 30515+
4f0767ce 30516+out_root:
1facf9fc 30517+ dput(root);
30518+ sb->s_root = NULL;
4f0767ce 30519+out_info:
79b8bda9 30520+ kobject_put(&sbinfo->si_kobj);
1facf9fc 30521+ sb->s_fs_info = NULL;
4f0767ce 30522+out_opts:
1c60b727 30523+ free_page((unsigned long)opts.opt);
4f0767ce 30524+out:
1facf9fc 30525+ AuTraceErr(err);
30526+ err = cvt_err(err);
30527+ AuTraceErr(err);
30528+ return err;
30529+}
30530+
30531+/* ---------------------------------------------------------------------- */
30532+
027c5e7a
AM
30533+static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
30534+ const char *dev_name __maybe_unused,
30535+ void *raw_data)
1facf9fc 30536+{
027c5e7a 30537+ struct dentry *root;
1facf9fc 30538+
30539+ /* all timestamps always follow the ones on the branch */
30540+ /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
027c5e7a
AM
30541+ root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
30542+ if (IS_ERR(root))
30543+ goto out;
30544+
062440b3 30545+ au_sbilist_add(root->d_sb);
027c5e7a
AM
30546+
30547+out:
30548+ return root;
1facf9fc 30549+}
30550+
e49829fe
JR
30551+static void aufs_kill_sb(struct super_block *sb)
30552+{
30553+ struct au_sbinfo *sbinfo;
30554+
30555+ sbinfo = au_sbi(sb);
30556+ if (sbinfo) {
30557+ au_sbilist_del(sb);
30558+ aufs_write_lock(sb->s_root);
076b876e 30559+ au_fhsm_fin(sb);
e49829fe
JR
30560+ if (sbinfo->si_wbr_create_ops->fin)
30561+ sbinfo->si_wbr_create_ops->fin(sb);
30562+ if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30563+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
b95c5147 30564+ au_remount_refresh(sb, /*do_idop*/0);
e49829fe
JR
30565+ }
30566+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
30567+ au_plink_put(sb, /*verbose*/1);
30568+ au_xino_clr(sb);
8b6a4947 30569+ au_dr_opt_flush(sb);
1e00d052 30570+ sbinfo->si_sb = NULL;
e49829fe 30571+ aufs_write_unlock(sb->s_root);
e49829fe
JR
30572+ au_nwt_flush(&sbinfo->si_nowait);
30573+ }
98d9a5b1 30574+ kill_anon_super(sb);
e49829fe
JR
30575+}
30576+
1facf9fc 30577+struct file_system_type aufs_fs_type = {
30578+ .name = AUFS_FSTYPE,
c06a8ce3
AM
30579+ /* a race between rename and others */
30580+ .fs_flags = FS_RENAME_DOES_D_MOVE,
027c5e7a 30581+ .mount = aufs_mount,
e49829fe 30582+ .kill_sb = aufs_kill_sb,
1facf9fc 30583+ /* no need to __module_get() and module_put(). */
30584+ .owner = THIS_MODULE,
30585+};
7f207e10
AM
30586diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30587--- /usr/share/empty/fs/aufs/super.h 1970-01-01 01:00:00.000000000 +0100
acd2b654
AM
30588+++ linux/fs/aufs/super.h 2018-10-23 12:33:35.599375796 +0200
30589@@ -0,0 +1,589 @@
062440b3 30590+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 30591+/*
b00004a5 30592+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 30593+ *
30594+ * This program, aufs is free software; you can redistribute it and/or modify
30595+ * it under the terms of the GNU General Public License as published by
30596+ * the Free Software Foundation; either version 2 of the License, or
30597+ * (at your option) any later version.
dece6358
AM
30598+ *
30599+ * This program is distributed in the hope that it will be useful,
30600+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30601+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30602+ * GNU General Public License for more details.
30603+ *
30604+ * You should have received a copy of the GNU General Public License
523b37e3 30605+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30606+ */
30607+
30608+/*
30609+ * super_block operations
30610+ */
30611+
30612+#ifndef __AUFS_SUPER_H__
30613+#define __AUFS_SUPER_H__
30614+
30615+#ifdef __KERNEL__
30616+
30617+#include <linux/fs.h>
5527c038 30618+#include <linux/kobject.h>
8b6a4947 30619+#include "hbl.h"
acd2b654 30620+#include "lcnt.h"
1facf9fc 30621+#include "rwsem.h"
1facf9fc 30622+#include "wkq.h"
30623+
1facf9fc 30624+/* policies to select one among multiple writable branches */
30625+struct au_wbr_copyup_operations {
30626+ int (*copyup)(struct dentry *dentry);
30627+};
30628+
392086de
AM
30629+#define AuWbr_DIR 1 /* target is a dir */
30630+#define AuWbr_PARENT (1 << 1) /* always require a parent */
30631+
30632+#define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
30633+#define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
30634+#define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
30635+
1facf9fc 30636+struct au_wbr_create_operations {
392086de 30637+ int (*create)(struct dentry *dentry, unsigned int flags);
1facf9fc 30638+ int (*init)(struct super_block *sb);
30639+ int (*fin)(struct super_block *sb);
30640+};
30641+
30642+struct au_wbr_mfs {
30643+ struct mutex mfs_lock; /* protect this structure */
30644+ unsigned long mfs_jiffy;
30645+ unsigned long mfs_expire;
30646+ aufs_bindex_t mfs_bindex;
30647+
30648+ unsigned long long mfsrr_bytes;
30649+ unsigned long long mfsrr_watermark;
30650+};
30651+
86dc4139
AM
30652+#define AuPlink_NHASH 100
30653+static inline int au_plink_hash(ino_t ino)
30654+{
30655+ return ino % AuPlink_NHASH;
30656+}
30657+
076b876e
AM
30658+/* File-based Hierarchical Storage Management */
30659+struct au_fhsm {
30660+#ifdef CONFIG_AUFS_FHSM
30661+ /* allow only one process who can receive the notification */
30662+ spinlock_t fhsm_spin;
30663+ pid_t fhsm_pid;
30664+ wait_queue_head_t fhsm_wqh;
30665+ atomic_t fhsm_readable;
30666+
c1595e42 30667+ /* these are protected by si_rwsem */
076b876e 30668+ unsigned long fhsm_expire;
c1595e42 30669+ aufs_bindex_t fhsm_bottom;
076b876e
AM
30670+#endif
30671+};
30672+
1facf9fc 30673+struct au_branch;
30674+struct au_sbinfo {
30675+ /* nowait tasks in the system-wide workqueue */
30676+ struct au_nowait_tasks si_nowait;
30677+
b752ccd1 30678+ /*
acd2b654 30679+ * tried sb->s_umount, but failed due to the dependency between i_mutex.
b752ccd1
AM
30680+ * rwsem for au_sbinfo is necessary.
30681+ */
dece6358 30682+ struct au_rwsem si_rwsem;
1facf9fc 30683+
7f207e10 30684+ /*
523b37e3
AM
30685+ * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30686+ * remount.
7f207e10 30687+ */
acd2b654 30688+ au_lcnt_t si_ninodes, si_nfiles;
7f207e10 30689+
1facf9fc 30690+ /* branch management */
30691+ unsigned int si_generation;
30692+
2000de60 30693+ /* see AuSi_ flags */
1facf9fc 30694+ unsigned char au_si_status;
30695+
5afbbe0d 30696+ aufs_bindex_t si_bbot;
7f207e10
AM
30697+
30698+ /* dirty trick to keep br_id plus */
30699+ unsigned int si_last_br_id :
30700+ sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
1facf9fc 30701+ struct au_branch **si_branch;
30702+
30703+ /* policy to select a writable branch */
30704+ unsigned char si_wbr_copyup;
30705+ unsigned char si_wbr_create;
30706+ struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30707+ struct au_wbr_create_operations *si_wbr_create_ops;
30708+
30709+ /* round robin */
30710+ atomic_t si_wbr_rr_next;
30711+
30712+ /* most free space */
30713+ struct au_wbr_mfs si_wbr_mfs;
30714+
076b876e
AM
30715+ /* File-based Hierarchical Storage Management */
30716+ struct au_fhsm si_fhsm;
30717+
1facf9fc 30718+ /* mount flags */
30719+ /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30720+ unsigned int si_mntflags;
30721+
30722+ /* external inode number (bitmap and translation table) */
5527c038
JR
30723+ vfs_readf_t si_xread;
30724+ vfs_writef_t si_xwrite;
acd2b654
AM
30725+ loff_t si_ximaxent; /* max entries in a xino */
30726+
1facf9fc 30727+ struct file *si_xib;
30728+ struct mutex si_xib_mtx; /* protect xib members */
30729+ unsigned long *si_xib_buf;
30730+ unsigned long si_xib_last_pindex;
30731+ int si_xib_next_bit;
acd2b654 30732+
392086de
AM
30733+ unsigned long si_xino_jiffy;
30734+ unsigned long si_xino_expire;
1facf9fc 30735+ /* reserved for future use */
30736+ /* unsigned long long si_xib_limit; */ /* Max xib file size */
30737+
30738+#ifdef CONFIG_AUFS_EXPORT
30739+ /* i_generation */
acd2b654 30740+ /* todo: make xigen file an array to support many inode numbers */
1facf9fc 30741+ struct file *si_xigen;
30742+ atomic_t si_xigen_next;
30743+#endif
30744+
acd2b654 30745+ /* dirty trick to support atomic_open */
8b6a4947 30746+ struct hlist_bl_head si_aopen;
b912730e 30747+
1facf9fc 30748+ /* vdir parameters */
e49829fe 30749+ unsigned long si_rdcache; /* max cache time in jiffies */
1facf9fc 30750+ unsigned int si_rdblk; /* deblk size */
30751+ unsigned int si_rdhash; /* hash size */
30752+
30753+ /*
30754+ * If the number of whiteouts are larger than si_dirwh, leave all of
30755+ * them after au_whtmp_ren to reduce the cost of rmdir(2).
30756+ * future fsck.aufs or kernel thread will remove them later.
30757+ * Otherwise, remove all whiteouts and the dir in rmdir(2).
30758+ */
30759+ unsigned int si_dirwh;
30760+
1facf9fc 30761+ /* pseudo_link list */
8b6a4947 30762+ struct hlist_bl_head si_plink[AuPlink_NHASH];
1facf9fc 30763+ wait_queue_head_t si_plink_wq;
4a4d8108 30764+ spinlock_t si_plink_maint_lock;
e49829fe 30765+ pid_t si_plink_maint_pid;
1facf9fc 30766+
523b37e3 30767+ /* file list */
8b6a4947 30768+ struct hlist_bl_head si_files;
523b37e3 30769+
b95c5147
AM
30770+ /* with/without getattr, brother of sb->s_d_op */
30771+ struct inode_operations *si_iop_array;
30772+
1facf9fc 30773+ /*
30774+ * sysfs and lifetime management.
30775+ * this is not a small structure and it may be a waste of memory in case
acd2b654 30776+ * of sysfs is disabled, particularly when many aufs-es are mounted.
1facf9fc 30777+ * but using sysfs is majority.
30778+ */
30779+ struct kobject si_kobj;
30780+#ifdef CONFIG_DEBUG_FS
86dc4139
AM
30781+ struct dentry *si_dbgaufs;
30782+ struct dentry *si_dbgaufs_plink;
30783+ struct dentry *si_dbgaufs_xib;
1facf9fc 30784+#ifdef CONFIG_AUFS_EXPORT
30785+ struct dentry *si_dbgaufs_xigen;
30786+#endif
30787+#endif
30788+
e49829fe 30789+#ifdef CONFIG_AUFS_SBILIST
8b6a4947 30790+ struct hlist_bl_node si_list;
e49829fe
JR
30791+#endif
30792+
1facf9fc 30793+ /* dirty, necessary for unmounting, sysfs and sysrq */
30794+ struct super_block *si_sb;
30795+};
30796+
dece6358
AM
30797+/* sbinfo status flags */
30798+/*
30799+ * set true when refresh_dirs() failed at remount time.
30800+ * then try refreshing dirs at access time again.
062440b3 30801+ * if it is false, refreshing dirs at access time is unnecessary
dece6358 30802+ */
027c5e7a 30803+#define AuSi_FAILED_REFRESH_DIR 1
076b876e 30804+#define AuSi_FHSM (1 << 1) /* fhsm is active now */
79b8bda9 30805+#define AuSi_NO_DREVAL (1 << 2) /* disable all d_revalidate */
076b876e
AM
30806+
30807+#ifndef CONFIG_AUFS_FHSM
30808+#undef AuSi_FHSM
30809+#define AuSi_FHSM 0
30810+#endif
30811+
dece6358
AM
30812+static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30813+ unsigned int flag)
30814+{
30815+ AuRwMustAnyLock(&sbi->si_rwsem);
30816+ return sbi->au_si_status & flag;
30817+}
30818+#define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
30819+#define au_fset_si(sbinfo, name) do { \
30820+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30821+ (sbinfo)->au_si_status |= AuSi_##name; \
30822+} while (0)
30823+#define au_fclr_si(sbinfo, name) do { \
30824+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30825+ (sbinfo)->au_si_status &= ~AuSi_##name; \
30826+} while (0)
30827+
1facf9fc 30828+/* ---------------------------------------------------------------------- */
30829+
30830+/* policy to select one among writable branches */
4a4d8108
AM
30831+#define AuWbrCopyup(sbinfo, ...) \
30832+ ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30833+#define AuWbrCreate(sbinfo, ...) \
30834+ ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
1facf9fc 30835+
30836+/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30837+#define AuLock_DW 1 /* write-lock dentry */
30838+#define AuLock_IR (1 << 1) /* read-lock inode */
30839+#define AuLock_IW (1 << 2) /* write-lock inode */
30840+#define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
b95c5147 30841+#define AuLock_DIRS (1 << 4) /* target is a pair of dirs */
f2c43d5f 30842+ /* except RENAME_EXCHANGE */
e49829fe
JR
30843+#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
30844+#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
027c5e7a 30845+#define AuLock_GEN (1 << 7) /* test digen/iigen */
1facf9fc 30846+#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
7f207e10
AM
30847+#define au_fset_lock(flags, name) \
30848+ do { (flags) |= AuLock_##name; } while (0)
30849+#define au_fclr_lock(flags, name) \
30850+ do { (flags) &= ~AuLock_##name; } while (0)
1facf9fc 30851+
30852+/* ---------------------------------------------------------------------- */
30853+
30854+/* super.c */
30855+extern struct file_system_type aufs_fs_type;
30856+struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
79b8bda9
AM
30857+typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30858+ unsigned long long max, void *arg);
79b8bda9
AM
30859+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30860+ struct super_block *sb, void *arg);
7f207e10
AM
30861+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30862+void au_iarray_free(struct inode **a, unsigned long long max);
1facf9fc 30863+
30864+/* sbinfo.c */
30865+void au_si_free(struct kobject *kobj);
30866+int au_si_alloc(struct super_block *sb);
e2f27e51 30867+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
1facf9fc 30868+
30869+unsigned int au_sigen_inc(struct super_block *sb);
30870+aufs_bindex_t au_new_br_id(struct super_block *sb);
30871+
e49829fe
JR
30872+int si_read_lock(struct super_block *sb, int flags);
30873+int si_write_lock(struct super_block *sb, int flags);
30874+int aufs_read_lock(struct dentry *dentry, int flags);
1facf9fc 30875+void aufs_read_unlock(struct dentry *dentry, int flags);
30876+void aufs_write_lock(struct dentry *dentry);
30877+void aufs_write_unlock(struct dentry *dentry);
e49829fe 30878+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
1facf9fc 30879+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30880+
30881+/* wbr_policy.c */
30882+extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30883+extern struct au_wbr_create_operations au_wbr_create_ops[];
30884+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
c2b27bf2 30885+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
5afbbe0d 30886+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
c2b27bf2
AM
30887+
30888+/* mvdown.c */
30889+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
1facf9fc 30890+
076b876e
AM
30891+#ifdef CONFIG_AUFS_FHSM
30892+/* fhsm.c */
30893+
30894+static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30895+{
30896+ pid_t pid;
30897+
30898+ spin_lock(&fhsm->fhsm_spin);
30899+ pid = fhsm->fhsm_pid;
30900+ spin_unlock(&fhsm->fhsm_spin);
30901+
30902+ return pid;
30903+}
30904+
30905+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
30906+void au_fhsm_wrote_all(struct super_block *sb, int force);
30907+int au_fhsm_fd(struct super_block *sb, int oflags);
30908+int au_fhsm_br_alloc(struct au_branch *br);
c1595e42 30909+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
076b876e
AM
30910+void au_fhsm_fin(struct super_block *sb);
30911+void au_fhsm_init(struct au_sbinfo *sbinfo);
30912+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
30913+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
30914+#else
30915+AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
30916+ int force)
30917+AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
30918+AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
c1595e42
JR
30919+AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
30920+AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
30921+AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
076b876e
AM
30922+AuStubVoid(au_fhsm_fin, struct super_block *sb)
30923+AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
30924+AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
30925+AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
30926+#endif
30927+
1facf9fc 30928+/* ---------------------------------------------------------------------- */
30929+
30930+static inline struct au_sbinfo *au_sbi(struct super_block *sb)
30931+{
30932+ return sb->s_fs_info;
30933+}
30934+
30935+/* ---------------------------------------------------------------------- */
30936+
30937+#ifdef CONFIG_AUFS_EXPORT
a2a7ad62 30938+int au_test_nfsd(void);
1facf9fc 30939+void au_export_init(struct super_block *sb);
b752ccd1 30940+void au_xigen_inc(struct inode *inode);
1facf9fc 30941+int au_xigen_new(struct inode *inode);
062440b3 30942+int au_xigen_set(struct super_block *sb, struct path *path);
1facf9fc 30943+void au_xigen_clr(struct super_block *sb);
30944+
30945+static inline int au_busy_or_stale(void)
30946+{
b752ccd1 30947+ if (!au_test_nfsd())
1facf9fc 30948+ return -EBUSY;
30949+ return -ESTALE;
30950+}
30951+#else
b752ccd1 30952+AuStubInt0(au_test_nfsd, void)
a2a7ad62 30953+AuStubVoid(au_export_init, struct super_block *sb)
b752ccd1 30954+AuStubVoid(au_xigen_inc, struct inode *inode)
4a4d8108 30955+AuStubInt0(au_xigen_new, struct inode *inode)
062440b3 30956+AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
4a4d8108 30957+AuStubVoid(au_xigen_clr, struct super_block *sb)
c1595e42 30958+AuStub(int, au_busy_or_stale, return -EBUSY, void)
1facf9fc 30959+#endif /* CONFIG_AUFS_EXPORT */
30960+
30961+/* ---------------------------------------------------------------------- */
30962+
e49829fe
JR
30963+#ifdef CONFIG_AUFS_SBILIST
30964+/* module.c */
8b6a4947 30965+extern struct hlist_bl_head au_sbilist;
e49829fe
JR
30966+
30967+static inline void au_sbilist_init(void)
30968+{
8b6a4947 30969+ INIT_HLIST_BL_HEAD(&au_sbilist);
e49829fe
JR
30970+}
30971+
30972+static inline void au_sbilist_add(struct super_block *sb)
30973+{
8b6a4947 30974+ au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe
JR
30975+}
30976+
30977+static inline void au_sbilist_del(struct super_block *sb)
30978+{
8b6a4947 30979+ au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe 30980+}
53392da6
AM
30981+
30982+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
30983+static inline void au_sbilist_lock(void)
30984+{
8b6a4947 30985+ hlist_bl_lock(&au_sbilist);
53392da6
AM
30986+}
30987+
30988+static inline void au_sbilist_unlock(void)
30989+{
8b6a4947 30990+ hlist_bl_unlock(&au_sbilist);
53392da6
AM
30991+}
30992+#define AuGFP_SBILIST GFP_ATOMIC
30993+#else
30994+AuStubVoid(au_sbilist_lock, void)
30995+AuStubVoid(au_sbilist_unlock, void)
30996+#define AuGFP_SBILIST GFP_NOFS
30997+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
e49829fe
JR
30998+#else
30999+AuStubVoid(au_sbilist_init, void)
c1595e42
JR
31000+AuStubVoid(au_sbilist_add, struct super_block *sb)
31001+AuStubVoid(au_sbilist_del, struct super_block *sb)
53392da6
AM
31002+AuStubVoid(au_sbilist_lock, void)
31003+AuStubVoid(au_sbilist_unlock, void)
31004+#define AuGFP_SBILIST GFP_NOFS
e49829fe
JR
31005+#endif
31006+
31007+/* ---------------------------------------------------------------------- */
31008+
1facf9fc 31009+static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
31010+{
dece6358 31011+ /*
c1595e42 31012+ * This function is a dynamic '__init' function actually,
dece6358
AM
31013+ * so the tiny check for si_rwsem is unnecessary.
31014+ */
31015+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
1facf9fc 31016+#ifdef CONFIG_DEBUG_FS
31017+ sbinfo->si_dbgaufs = NULL;
86dc4139 31018+ sbinfo->si_dbgaufs_plink = NULL;
1facf9fc 31019+ sbinfo->si_dbgaufs_xib = NULL;
31020+#ifdef CONFIG_AUFS_EXPORT
31021+ sbinfo->si_dbgaufs_xigen = NULL;
31022+#endif
31023+#endif
31024+}
31025+
31026+/* ---------------------------------------------------------------------- */
31027+
a2654f78
AM
31028+/* current->atomic_flags */
31029+/* this value should never corrupt the ones defined in linux/sched.h */
31030+#define PFA_AUFS 7
31031+
31032+TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
31033+TASK_PFA_SET(AUFS, aufs) /* task_set_aufs */
31034+TASK_PFA_CLEAR(AUFS, aufs) /* task_clear_aufs */
b752ccd1
AM
31035+
31036+static inline int si_pid_test(struct super_block *sb)
31037+{
a2654f78 31038+ return !!task_test_aufs(current);
b752ccd1
AM
31039+}
31040+
31041+static inline void si_pid_clr(struct super_block *sb)
31042+{
a2654f78
AM
31043+ AuDebugOn(!task_test_aufs(current));
31044+ task_clear_aufs(current);
b752ccd1
AM
31045+}
31046+
a2654f78
AM
31047+static inline void si_pid_set(struct super_block *sb)
31048+{
31049+ AuDebugOn(task_test_aufs(current));
31050+ task_set_aufs(current);
31051+}
febd17d6 31052+
b752ccd1
AM
31053+/* ---------------------------------------------------------------------- */
31054+
1facf9fc 31055+/* lock superblock. mainly for entry point functions */
8b6a4947
AM
31056+#define __si_read_lock(sb) au_rw_read_lock(&au_sbi(sb)->si_rwsem)
31057+#define __si_write_lock(sb) au_rw_write_lock(&au_sbi(sb)->si_rwsem)
31058+#define __si_read_trylock(sb) au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
31059+#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
31060+/*
31061+#define __si_read_trylock_nested(sb) \
31062+ au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
31063+#define __si_write_trylock_nested(sb) \
31064+ au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
31065+*/
31066+
31067+#define __si_read_unlock(sb) au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
31068+#define __si_write_unlock(sb) au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
31069+#define __si_downgrade_lock(sb) au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
1facf9fc 31070+
dece6358
AM
31071+#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
31072+#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
31073+#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
31074+
b752ccd1
AM
31075+static inline void si_noflush_read_lock(struct super_block *sb)
31076+{
31077+ __si_read_lock(sb);
31078+ si_pid_set(sb);
31079+}
31080+
31081+static inline int si_noflush_read_trylock(struct super_block *sb)
31082+{
076b876e
AM
31083+ int locked;
31084+
31085+ locked = __si_read_trylock(sb);
b752ccd1
AM
31086+ if (locked)
31087+ si_pid_set(sb);
31088+ return locked;
31089+}
31090+
31091+static inline void si_noflush_write_lock(struct super_block *sb)
31092+{
31093+ __si_write_lock(sb);
31094+ si_pid_set(sb);
31095+}
31096+
31097+static inline int si_noflush_write_trylock(struct super_block *sb)
31098+{
076b876e
AM
31099+ int locked;
31100+
31101+ locked = __si_write_trylock(sb);
b752ccd1
AM
31102+ if (locked)
31103+ si_pid_set(sb);
31104+ return locked;
31105+}
31106+
7e9cd9fe 31107+#if 0 /* reserved */
1facf9fc 31108+static inline int si_read_trylock(struct super_block *sb, int flags)
31109+{
31110+ if (au_ftest_lock(flags, FLUSH))
31111+ au_nwt_flush(&au_sbi(sb)->si_nowait);
31112+ return si_noflush_read_trylock(sb);
31113+}
e49829fe 31114+#endif
1facf9fc 31115+
b752ccd1
AM
31116+static inline void si_read_unlock(struct super_block *sb)
31117+{
31118+ si_pid_clr(sb);
31119+ __si_read_unlock(sb);
31120+}
31121+
7e9cd9fe 31122+#if 0 /* reserved */
1facf9fc 31123+static inline int si_write_trylock(struct super_block *sb, int flags)
31124+{
31125+ if (au_ftest_lock(flags, FLUSH))
31126+ au_nwt_flush(&au_sbi(sb)->si_nowait);
31127+ return si_noflush_write_trylock(sb);
31128+}
b752ccd1
AM
31129+#endif
31130+
31131+static inline void si_write_unlock(struct super_block *sb)
31132+{
31133+ si_pid_clr(sb);
31134+ __si_write_unlock(sb);
31135+}
31136+
7e9cd9fe 31137+#if 0 /* reserved */
b752ccd1
AM
31138+static inline void si_downgrade_lock(struct super_block *sb)
31139+{
31140+ __si_downgrade_lock(sb);
31141+}
31142+#endif
1facf9fc 31143+
31144+/* ---------------------------------------------------------------------- */
31145+
5afbbe0d 31146+static inline aufs_bindex_t au_sbbot(struct super_block *sb)
1facf9fc 31147+{
dece6358 31148+ SiMustAnyLock(sb);
5afbbe0d 31149+ return au_sbi(sb)->si_bbot;
1facf9fc 31150+}
31151+
31152+static inline unsigned int au_mntflags(struct super_block *sb)
31153+{
dece6358 31154+ SiMustAnyLock(sb);
1facf9fc 31155+ return au_sbi(sb)->si_mntflags;
31156+}
31157+
31158+static inline unsigned int au_sigen(struct super_block *sb)
31159+{
dece6358 31160+ SiMustAnyLock(sb);
1facf9fc 31161+ return au_sbi(sb)->si_generation;
31162+}
31163+
31164+static inline struct au_branch *au_sbr(struct super_block *sb,
31165+ aufs_bindex_t bindex)
31166+{
dece6358 31167+ SiMustAnyLock(sb);
1facf9fc 31168+ return au_sbi(sb)->si_branch[0 + bindex];
31169+}
31170+
acd2b654 31171+static inline loff_t au_xi_maxent(struct super_block *sb)
1facf9fc 31172+{
dece6358 31173+ SiMustAnyLock(sb);
acd2b654 31174+ return au_sbi(sb)->si_ximaxent;
1facf9fc 31175+}
31176+
31177+#endif /* __KERNEL__ */
31178+#endif /* __AUFS_SUPER_H__ */
7f207e10
AM
31179diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
31180--- /usr/share/empty/fs/aufs/sysaufs.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 31181+++ linux/fs/aufs/sysaufs.c 2018-08-12 23:43:05.463458173 +0200
062440b3 31182@@ -0,0 +1,93 @@
cd7a4cd9 31183+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31184+/*
b00004a5 31185+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 31186+ *
31187+ * This program, aufs is free software; you can redistribute it and/or modify
31188+ * it under the terms of the GNU General Public License as published by
31189+ * the Free Software Foundation; either version 2 of the License, or
31190+ * (at your option) any later version.
dece6358
AM
31191+ *
31192+ * This program is distributed in the hope that it will be useful,
31193+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31194+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31195+ * GNU General Public License for more details.
31196+ *
31197+ * You should have received a copy of the GNU General Public License
523b37e3 31198+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31199+ */
31200+
31201+/*
31202+ * sysfs interface and lifetime management
31203+ * they are necessary regardless sysfs is disabled.
31204+ */
31205+
1facf9fc 31206+#include <linux/random.h>
1facf9fc 31207+#include "aufs.h"
31208+
31209+unsigned long sysaufs_si_mask;
e49829fe 31210+struct kset *sysaufs_kset;
1facf9fc 31211+
31212+#define AuSiAttr(_name) { \
31213+ .attr = { .name = __stringify(_name), .mode = 0444 }, \
31214+ .show = sysaufs_si_##_name, \
31215+}
31216+
31217+static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
31218+struct attribute *sysaufs_si_attrs[] = {
31219+ &sysaufs_si_attr_xi_path.attr,
31220+ NULL,
31221+};
31222+
4a4d8108 31223+static const struct sysfs_ops au_sbi_ops = {
1facf9fc 31224+ .show = sysaufs_si_show
31225+};
31226+
31227+static struct kobj_type au_sbi_ktype = {
31228+ .release = au_si_free,
31229+ .sysfs_ops = &au_sbi_ops,
31230+ .default_attrs = sysaufs_si_attrs
31231+};
31232+
31233+/* ---------------------------------------------------------------------- */
31234+
31235+int sysaufs_si_init(struct au_sbinfo *sbinfo)
31236+{
31237+ int err;
31238+
e49829fe 31239+ sbinfo->si_kobj.kset = sysaufs_kset;
1facf9fc 31240+ /* cf. sysaufs_name() */
31241+ err = kobject_init_and_add
e49829fe 31242+ (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
1facf9fc 31243+ SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31244+
1facf9fc 31245+ return err;
31246+}
31247+
31248+void sysaufs_fin(void)
31249+{
e49829fe
JR
31250+ sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31251+ kset_unregister(sysaufs_kset);
1facf9fc 31252+}
31253+
31254+int __init sysaufs_init(void)
31255+{
31256+ int err;
31257+
31258+ do {
31259+ get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31260+ } while (!sysaufs_si_mask);
31261+
4a4d8108 31262+ err = -EINVAL;
e49829fe
JR
31263+ sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31264+ if (unlikely(!sysaufs_kset))
4a4d8108 31265+ goto out;
e49829fe
JR
31266+ err = PTR_ERR(sysaufs_kset);
31267+ if (IS_ERR(sysaufs_kset))
1facf9fc 31268+ goto out;
e49829fe 31269+ err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
062440b3 31270+ if (unlikely(err))
e49829fe 31271+ kset_unregister(sysaufs_kset);
1facf9fc 31272+
4f0767ce 31273+out:
1facf9fc 31274+ return err;
31275+}
7f207e10
AM
31276diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
31277--- /usr/share/empty/fs/aufs/sysaufs.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 31278+++ linux/fs/aufs/sysaufs.h 2018-08-12 23:43:05.463458173 +0200
062440b3
AM
31279@@ -0,0 +1,102 @@
31280+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 31281+/*
b00004a5 31282+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 31283+ *
31284+ * This program, aufs is free software; you can redistribute it and/or modify
31285+ * it under the terms of the GNU General Public License as published by
31286+ * the Free Software Foundation; either version 2 of the License, or
31287+ * (at your option) any later version.
dece6358
AM
31288+ *
31289+ * This program is distributed in the hope that it will be useful,
31290+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31291+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31292+ * GNU General Public License for more details.
31293+ *
31294+ * You should have received a copy of the GNU General Public License
523b37e3 31295+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31296+ */
31297+
31298+/*
31299+ * sysfs interface and mount lifetime management
31300+ */
31301+
31302+#ifndef __SYSAUFS_H__
31303+#define __SYSAUFS_H__
31304+
31305+#ifdef __KERNEL__
31306+
1facf9fc 31307+#include <linux/sysfs.h>
1facf9fc 31308+#include "module.h"
31309+
dece6358
AM
31310+struct super_block;
31311+struct au_sbinfo;
31312+
1facf9fc 31313+struct sysaufs_si_attr {
31314+ struct attribute attr;
31315+ int (*show)(struct seq_file *seq, struct super_block *sb);
31316+};
31317+
31318+/* ---------------------------------------------------------------------- */
31319+
31320+/* sysaufs.c */
31321+extern unsigned long sysaufs_si_mask;
e49829fe 31322+extern struct kset *sysaufs_kset;
1facf9fc 31323+extern struct attribute *sysaufs_si_attrs[];
31324+int sysaufs_si_init(struct au_sbinfo *sbinfo);
31325+int __init sysaufs_init(void);
31326+void sysaufs_fin(void);
31327+
31328+/* ---------------------------------------------------------------------- */
31329+
31330+/* some people doesn't like to show a pointer in kernel */
31331+static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31332+{
31333+ return sysaufs_si_mask ^ (unsigned long)sbinfo;
31334+}
31335+
31336+#define SysaufsSiNamePrefix "si_"
31337+#define SysaufsSiNameLen (sizeof(SysaufsSiNamePrefix) + 16)
31338+static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31339+{
31340+ snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31341+ sysaufs_si_id(sbinfo));
31342+}
31343+
31344+struct au_branch;
31345+#ifdef CONFIG_SYSFS
31346+/* sysfs.c */
31347+extern struct attribute_group *sysaufs_attr_group;
31348+
31349+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31350+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31351+ char *buf);
076b876e
AM
31352+long au_brinfo_ioctl(struct file *file, unsigned long arg);
31353+#ifdef CONFIG_COMPAT
31354+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31355+#endif
1facf9fc 31356+
31357+void sysaufs_br_init(struct au_branch *br);
31358+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31359+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31360+
31361+#define sysaufs_brs_init() do {} while (0)
31362+
31363+#else
31364+#define sysaufs_attr_group NULL
31365+
4a4d8108 31366+AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
c1595e42
JR
31367+AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31368+ struct attribute *attr, char *buf)
4a4d8108
AM
31369+AuStubVoid(sysaufs_br_init, struct au_branch *br)
31370+AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31371+AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
1facf9fc 31372+
31373+static inline void sysaufs_brs_init(void)
31374+{
31375+ sysaufs_brs = 0;
31376+}
31377+
31378+#endif /* CONFIG_SYSFS */
31379+
31380+#endif /* __KERNEL__ */
31381+#endif /* __SYSAUFS_H__ */
7f207e10
AM
31382diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31383--- /usr/share/empty/fs/aufs/sysfs.c 1970-01-01 01:00:00.000000000 +0100
9f237c51 31384+++ linux/fs/aufs/sysfs.c 2018-12-27 13:19:17.711749485 +0100
062440b3 31385@@ -0,0 +1,373 @@
cd7a4cd9 31386+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31387+/*
b00004a5 31388+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 31389+ *
31390+ * This program, aufs is free software; you can redistribute it and/or modify
31391+ * it under the terms of the GNU General Public License as published by
31392+ * the Free Software Foundation; either version 2 of the License, or
31393+ * (at your option) any later version.
dece6358
AM
31394+ *
31395+ * This program is distributed in the hope that it will be useful,
31396+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31397+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31398+ * GNU General Public License for more details.
31399+ *
31400+ * You should have received a copy of the GNU General Public License
523b37e3 31401+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31402+ */
31403+
31404+/*
31405+ * sysfs interface
31406+ */
31407+
076b876e 31408+#include <linux/compat.h>
1facf9fc 31409+#include <linux/seq_file.h>
1facf9fc 31410+#include "aufs.h"
31411+
4a4d8108
AM
31412+#ifdef CONFIG_AUFS_FS_MODULE
31413+/* this entry violates the "one line per file" policy of sysfs */
31414+static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31415+ char *buf)
31416+{
31417+ ssize_t err;
31418+ static char *conf =
31419+/* this file is generated at compiling */
31420+#include "conf.str"
31421+ ;
31422+
31423+ err = snprintf(buf, PAGE_SIZE, conf);
31424+ if (unlikely(err >= PAGE_SIZE))
31425+ err = -EFBIG;
31426+ return err;
31427+}
31428+
31429+static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31430+#endif
31431+
1facf9fc 31432+static struct attribute *au_attr[] = {
4a4d8108
AM
31433+#ifdef CONFIG_AUFS_FS_MODULE
31434+ &au_config_attr.attr,
31435+#endif
1facf9fc 31436+ NULL, /* need to NULL terminate the list of attributes */
31437+};
31438+
31439+static struct attribute_group sysaufs_attr_group_body = {
31440+ .attrs = au_attr
31441+};
31442+
31443+struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31444+
31445+/* ---------------------------------------------------------------------- */
31446+
31447+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31448+{
31449+ int err;
31450+
dece6358
AM
31451+ SiMustAnyLock(sb);
31452+
1facf9fc 31453+ err = 0;
31454+ if (au_opt_test(au_mntflags(sb), XINO)) {
31455+ err = au_xino_path(seq, au_sbi(sb)->si_xib);
31456+ seq_putc(seq, '\n');
31457+ }
31458+ return err;
31459+}
31460+
31461+/*
31462+ * the lifetime of branch is independent from the entry under sysfs.
31463+ * sysfs handles the lifetime of the entry, and never call ->show() after it is
31464+ * unlinked.
31465+ */
31466+static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
392086de 31467+ aufs_bindex_t bindex, int idx)
1facf9fc 31468+{
1e00d052 31469+ int err;
1facf9fc 31470+ struct path path;
31471+ struct dentry *root;
31472+ struct au_branch *br;
076b876e 31473+ au_br_perm_str_t perm;
1facf9fc 31474+
31475+ AuDbg("b%d\n", bindex);
31476+
1e00d052 31477+ err = 0;
1facf9fc 31478+ root = sb->s_root;
31479+ di_read_lock_parent(root, !AuLock_IR);
31480+ br = au_sbr(sb, bindex);
392086de
AM
31481+
31482+ switch (idx) {
31483+ case AuBrSysfs_BR:
31484+ path.mnt = au_br_mnt(br);
31485+ path.dentry = au_h_dptr(root, bindex);
79b8bda9
AM
31486+ err = au_seq_path(seq, &path);
31487+ if (!err) {
31488+ au_optstr_br_perm(&perm, br->br_perm);
31489+ seq_printf(seq, "=%s\n", perm.a);
31490+ }
392086de
AM
31491+ break;
31492+ case AuBrSysfs_BRID:
79b8bda9 31493+ seq_printf(seq, "%d\n", br->br_id);
392086de
AM
31494+ break;
31495+ }
076b876e 31496+ di_read_unlock(root, !AuLock_IR);
79b8bda9 31497+ if (unlikely(err || seq_has_overflowed(seq)))
076b876e 31498+ err = -E2BIG;
392086de 31499+
1e00d052 31500+ return err;
1facf9fc 31501+}
31502+
31503+/* ---------------------------------------------------------------------- */
31504+
31505+static struct seq_file *au_seq(char *p, ssize_t len)
31506+{
31507+ struct seq_file *seq;
31508+
31509+ seq = kzalloc(sizeof(*seq), GFP_NOFS);
31510+ if (seq) {
31511+ /* mutex_init(&seq.lock); */
31512+ seq->buf = p;
31513+ seq->size = len;
31514+ return seq; /* success */
31515+ }
31516+
31517+ seq = ERR_PTR(-ENOMEM);
31518+ return seq;
31519+}
31520+
392086de
AM
31521+#define SysaufsBr_PREFIX "br"
31522+#define SysaufsBrid_PREFIX "brid"
1facf9fc 31523+
31524+/* todo: file size may exceed PAGE_SIZE */
31525+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
1308ab2a 31526+ char *buf)
1facf9fc 31527+{
31528+ ssize_t err;
392086de 31529+ int idx;
1facf9fc 31530+ long l;
5afbbe0d 31531+ aufs_bindex_t bbot;
1facf9fc 31532+ struct au_sbinfo *sbinfo;
31533+ struct super_block *sb;
31534+ struct seq_file *seq;
31535+ char *name;
31536+ struct attribute **cattr;
31537+
31538+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31539+ sb = sbinfo->si_sb;
1308ab2a 31540+
31541+ /*
31542+ * prevent a race condition between sysfs and aufs.
31543+ * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31544+ * prohibits maintaining the sysfs entries.
31545+ * hew we acquire read lock after sysfs_get_active_two().
31546+ * on the other hand, the remount process may maintain the sysfs/aufs
31547+ * entries after acquiring write lock.
31548+ * it can cause a deadlock.
31549+ * simply we gave up processing read here.
31550+ */
31551+ err = -EBUSY;
31552+ if (unlikely(!si_noflush_read_trylock(sb)))
31553+ goto out;
1facf9fc 31554+
31555+ seq = au_seq(buf, PAGE_SIZE);
31556+ err = PTR_ERR(seq);
31557+ if (IS_ERR(seq))
1308ab2a 31558+ goto out_unlock;
1facf9fc 31559+
31560+ name = (void *)attr->name;
31561+ cattr = sysaufs_si_attrs;
31562+ while (*cattr) {
31563+ if (!strcmp(name, (*cattr)->name)) {
31564+ err = container_of(*cattr, struct sysaufs_si_attr, attr)
31565+ ->show(seq, sb);
31566+ goto out_seq;
31567+ }
31568+ cattr++;
31569+ }
31570+
392086de
AM
31571+ if (!strncmp(name, SysaufsBrid_PREFIX,
31572+ sizeof(SysaufsBrid_PREFIX) - 1)) {
31573+ idx = AuBrSysfs_BRID;
31574+ name += sizeof(SysaufsBrid_PREFIX) - 1;
31575+ } else if (!strncmp(name, SysaufsBr_PREFIX,
31576+ sizeof(SysaufsBr_PREFIX) - 1)) {
31577+ idx = AuBrSysfs_BR;
1facf9fc 31578+ name += sizeof(SysaufsBr_PREFIX) - 1;
392086de
AM
31579+ } else
31580+ BUG();
31581+
31582+ err = kstrtol(name, 10, &l);
31583+ if (!err) {
5afbbe0d
AM
31584+ bbot = au_sbbot(sb);
31585+ if (l <= bbot)
392086de
AM
31586+ err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31587+ else
31588+ err = -ENOENT;
1facf9fc 31589+ }
1facf9fc 31590+
4f0767ce 31591+out_seq:
1facf9fc 31592+ if (!err) {
31593+ err = seq->count;
31594+ /* sysfs limit */
31595+ if (unlikely(err == PAGE_SIZE))
31596+ err = -EFBIG;
31597+ }
9f237c51 31598+ au_kfree_rcu(seq);
4f0767ce 31599+out_unlock:
1facf9fc 31600+ si_read_unlock(sb);
4f0767ce 31601+out:
1facf9fc 31602+ return err;
31603+}
31604+
31605+/* ---------------------------------------------------------------------- */
31606+
076b876e
AM
31607+static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31608+{
31609+ int err;
31610+ int16_t brid;
5afbbe0d 31611+ aufs_bindex_t bindex, bbot;
076b876e
AM
31612+ size_t sz;
31613+ char *buf;
31614+ struct seq_file *seq;
31615+ struct au_branch *br;
31616+
31617+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d
AM
31618+ bbot = au_sbbot(sb);
31619+ err = bbot + 1;
076b876e
AM
31620+ if (!arg)
31621+ goto out;
31622+
31623+ err = -ENOMEM;
31624+ buf = (void *)__get_free_page(GFP_NOFS);
31625+ if (unlikely(!buf))
31626+ goto out;
31627+
31628+ seq = au_seq(buf, PAGE_SIZE);
31629+ err = PTR_ERR(seq);
31630+ if (IS_ERR(seq))
31631+ goto out_buf;
31632+
31633+ sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
5afbbe0d 31634+ for (bindex = 0; bindex <= bbot; bindex++, arg++) {
076b876e
AM
31635+ err = !access_ok(VERIFY_WRITE, arg, sizeof(*arg));
31636+ if (unlikely(err))
31637+ break;
31638+
31639+ br = au_sbr(sb, bindex);
31640+ brid = br->br_id;
31641+ BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31642+ err = __put_user(brid, &arg->id);
31643+ if (unlikely(err))
31644+ break;
31645+
31646+ BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31647+ err = __put_user(br->br_perm, &arg->perm);
31648+ if (unlikely(err))
31649+ break;
31650+
79b8bda9
AM
31651+ err = au_seq_path(seq, &br->br_path);
31652+ if (unlikely(err))
31653+ break;
31654+ seq_putc(seq, '\0');
31655+ if (!seq_has_overflowed(seq)) {
076b876e
AM
31656+ err = copy_to_user(arg->path, seq->buf, seq->count);
31657+ seq->count = 0;
31658+ if (unlikely(err))
31659+ break;
31660+ } else {
31661+ err = -E2BIG;
31662+ goto out_seq;
31663+ }
31664+ }
31665+ if (unlikely(err))
31666+ err = -EFAULT;
31667+
31668+out_seq:
9f237c51 31669+ au_kfree_rcu(seq);
076b876e 31670+out_buf:
1c60b727 31671+ free_page((unsigned long)buf);
076b876e
AM
31672+out:
31673+ si_read_unlock(sb);
31674+ return err;
31675+}
31676+
31677+long au_brinfo_ioctl(struct file *file, unsigned long arg)
31678+{
2000de60 31679+ return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
076b876e
AM
31680+}
31681+
31682+#ifdef CONFIG_COMPAT
31683+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31684+{
2000de60 31685+ return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
076b876e
AM
31686+}
31687+#endif
31688+
31689+/* ---------------------------------------------------------------------- */
31690+
1facf9fc 31691+void sysaufs_br_init(struct au_branch *br)
31692+{
392086de
AM
31693+ int i;
31694+ struct au_brsysfs *br_sysfs;
31695+ struct attribute *attr;
4a4d8108 31696+
392086de
AM
31697+ br_sysfs = br->br_sysfs;
31698+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31699+ attr = &br_sysfs->attr;
31700+ sysfs_attr_init(attr);
31701+ attr->name = br_sysfs->name;
cd7a4cd9 31702+ attr->mode = 0444;
392086de
AM
31703+ br_sysfs++;
31704+ }
1facf9fc 31705+}
31706+
31707+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31708+{
31709+ struct au_branch *br;
31710+ struct kobject *kobj;
392086de
AM
31711+ struct au_brsysfs *br_sysfs;
31712+ int i;
5afbbe0d 31713+ aufs_bindex_t bbot;
1facf9fc 31714+
1facf9fc 31715+ if (!sysaufs_brs)
31716+ return;
31717+
31718+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
31719+ bbot = au_sbbot(sb);
31720+ for (; bindex <= bbot; bindex++) {
1facf9fc 31721+ br = au_sbr(sb, bindex);
392086de
AM
31722+ br_sysfs = br->br_sysfs;
31723+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31724+ sysfs_remove_file(kobj, &br_sysfs->attr);
31725+ br_sysfs++;
31726+ }
1facf9fc 31727+ }
31728+}
31729+
31730+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31731+{
392086de 31732+ int err, i;
5afbbe0d 31733+ aufs_bindex_t bbot;
1facf9fc 31734+ struct kobject *kobj;
31735+ struct au_branch *br;
392086de 31736+ struct au_brsysfs *br_sysfs;
1facf9fc 31737+
1facf9fc 31738+ if (!sysaufs_brs)
31739+ return;
31740+
31741+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
31742+ bbot = au_sbbot(sb);
31743+ for (; bindex <= bbot; bindex++) {
1facf9fc 31744+ br = au_sbr(sb, bindex);
392086de
AM
31745+ br_sysfs = br->br_sysfs;
31746+ snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31747+ SysaufsBr_PREFIX "%d", bindex);
31748+ snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31749+ SysaufsBrid_PREFIX "%d", bindex);
31750+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31751+ err = sysfs_create_file(kobj, &br_sysfs->attr);
31752+ if (unlikely(err))
31753+ pr_warn("failed %s under sysfs(%d)\n",
31754+ br_sysfs->name, err);
31755+ br_sysfs++;
31756+ }
1facf9fc 31757+ }
31758+}
7f207e10
AM
31759diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31760--- /usr/share/empty/fs/aufs/sysrq.c 1970-01-01 01:00:00.000000000 +0100
acd2b654 31761+++ linux/fs/aufs/sysrq.c 2018-10-23 12:33:35.599375796 +0200
062440b3 31762@@ -0,0 +1,160 @@
cd7a4cd9 31763+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31764+/*
b00004a5 31765+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 31766+ *
31767+ * This program, aufs is free software; you can redistribute it and/or modify
31768+ * it under the terms of the GNU General Public License as published by
31769+ * the Free Software Foundation; either version 2 of the License, or
31770+ * (at your option) any later version.
dece6358
AM
31771+ *
31772+ * This program is distributed in the hope that it will be useful,
31773+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31774+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31775+ * GNU General Public License for more details.
31776+ *
31777+ * You should have received a copy of the GNU General Public License
523b37e3 31778+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31779+ */
31780+
31781+/*
acd2b654 31782+ * magic sysrq handler
1facf9fc 31783+ */
31784+
1facf9fc 31785+/* #include <linux/sysrq.h> */
027c5e7a 31786+#include <linux/writeback.h>
1facf9fc 31787+#include "aufs.h"
31788+
31789+/* ---------------------------------------------------------------------- */
31790+
31791+static void sysrq_sb(struct super_block *sb)
31792+{
31793+ char *plevel;
31794+ struct au_sbinfo *sbinfo;
31795+ struct file *file;
8b6a4947
AM
31796+ struct hlist_bl_head *files;
31797+ struct hlist_bl_node *pos;
523b37e3 31798+ struct au_finfo *finfo;
1facf9fc 31799+
31800+ plevel = au_plevel;
31801+ au_plevel = KERN_WARNING;
1facf9fc 31802+
4a4d8108 31803+ /* since we define pr_fmt, call printk directly */
c06a8ce3
AM
31804+#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31805+
31806+ sbinfo = au_sbi(sb);
4a4d8108 31807+ printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
c06a8ce3 31808+ pr("superblock\n");
1facf9fc 31809+ au_dpri_sb(sb);
027c5e7a
AM
31810+
31811+#if 0
c06a8ce3 31812+ pr("root dentry\n");
1facf9fc 31813+ au_dpri_dentry(sb->s_root);
c06a8ce3 31814+ pr("root inode\n");
5527c038 31815+ au_dpri_inode(d_inode(sb->s_root));
027c5e7a
AM
31816+#endif
31817+
1facf9fc 31818+#if 0
027c5e7a
AM
31819+ do {
31820+ int err, i, j, ndentry;
31821+ struct au_dcsub_pages dpages;
31822+ struct au_dpage *dpage;
31823+
31824+ err = au_dpages_init(&dpages, GFP_ATOMIC);
31825+ if (unlikely(err))
31826+ break;
31827+ err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31828+ if (!err)
31829+ for (i = 0; i < dpages.ndpage; i++) {
31830+ dpage = dpages.dpages + i;
31831+ ndentry = dpage->ndentry;
31832+ for (j = 0; j < ndentry; j++)
31833+ au_dpri_dentry(dpage->dentries[j]);
31834+ }
31835+ au_dpages_free(&dpages);
31836+ } while (0);
31837+#endif
31838+
31839+#if 1
31840+ {
31841+ struct inode *i;
076b876e 31842+
c06a8ce3 31843+ pr("isolated inode\n");
79b8bda9 31844+ spin_lock(&sb->s_inode_list_lock);
2cbb1c4b
JR
31845+ list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31846+ spin_lock(&i->i_lock);
b4510431 31847+ if (1 || hlist_empty(&i->i_dentry))
027c5e7a 31848+ au_dpri_inode(i);
2cbb1c4b
JR
31849+ spin_unlock(&i->i_lock);
31850+ }
79b8bda9 31851+ spin_unlock(&sb->s_inode_list_lock);
027c5e7a 31852+ }
1facf9fc 31853+#endif
c06a8ce3 31854+ pr("files\n");
523b37e3 31855+ files = &au_sbi(sb)->si_files;
8b6a4947
AM
31856+ hlist_bl_lock(files);
31857+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
4a4d8108 31858+ umode_t mode;
076b876e 31859+
523b37e3 31860+ file = finfo->fi_file;
c06a8ce3 31861+ mode = file_inode(file)->i_mode;
38d290e6 31862+ if (!special_file(mode))
1facf9fc 31863+ au_dpri_file(file);
523b37e3 31864+ }
8b6a4947 31865+ hlist_bl_unlock(files);
c06a8ce3 31866+ pr("done\n");
1facf9fc 31867+
c06a8ce3 31868+#undef pr
1facf9fc 31869+ au_plevel = plevel;
1facf9fc 31870+}
31871+
31872+/* ---------------------------------------------------------------------- */
31873+
31874+/* module parameter */
31875+static char *aufs_sysrq_key = "a";
cd7a4cd9 31876+module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
1facf9fc 31877+MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31878+
0c5527e5 31879+static void au_sysrq(int key __maybe_unused)
1facf9fc 31880+{
1facf9fc 31881+ struct au_sbinfo *sbinfo;
8b6a4947 31882+ struct hlist_bl_node *pos;
1facf9fc 31883+
027c5e7a 31884+ lockdep_off();
53392da6 31885+ au_sbilist_lock();
8b6a4947 31886+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
1facf9fc 31887+ sysrq_sb(sbinfo->si_sb);
53392da6 31888+ au_sbilist_unlock();
027c5e7a 31889+ lockdep_on();
1facf9fc 31890+}
31891+
31892+static struct sysrq_key_op au_sysrq_op = {
31893+ .handler = au_sysrq,
31894+ .help_msg = "Aufs",
31895+ .action_msg = "Aufs",
31896+ .enable_mask = SYSRQ_ENABLE_DUMP
31897+};
31898+
31899+/* ---------------------------------------------------------------------- */
31900+
31901+int __init au_sysrq_init(void)
31902+{
31903+ int err;
31904+ char key;
31905+
31906+ err = -1;
31907+ key = *aufs_sysrq_key;
31908+ if ('a' <= key && key <= 'z')
31909+ err = register_sysrq_key(key, &au_sysrq_op);
31910+ if (unlikely(err))
4a4d8108 31911+ pr_err("err %d, sysrq=%c\n", err, key);
1facf9fc 31912+ return err;
31913+}
31914+
31915+void au_sysrq_fin(void)
31916+{
31917+ int err;
076b876e 31918+
1facf9fc 31919+ err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31920+ if (unlikely(err))
4a4d8108 31921+ pr_err("err %d (ignored)\n", err);
1facf9fc 31922+}
7f207e10
AM
31923diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
31924--- /usr/share/empty/fs/aufs/vdir.c 1970-01-01 01:00:00.000000000 +0100
9f237c51 31925+++ linux/fs/aufs/vdir.c 2018-12-27 13:19:17.711749485 +0100
acd2b654 31926@@ -0,0 +1,895 @@
cd7a4cd9 31927+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31928+/*
b00004a5 31929+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 31930+ *
31931+ * This program, aufs is free software; you can redistribute it and/or modify
31932+ * it under the terms of the GNU General Public License as published by
31933+ * the Free Software Foundation; either version 2 of the License, or
31934+ * (at your option) any later version.
dece6358
AM
31935+ *
31936+ * This program is distributed in the hope that it will be useful,
31937+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31938+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31939+ * GNU General Public License for more details.
31940+ *
31941+ * You should have received a copy of the GNU General Public License
523b37e3 31942+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31943+ */
31944+
31945+/*
31946+ * virtual or vertical directory
31947+ */
31948+
31949+#include "aufs.h"
31950+
dece6358 31951+static unsigned int calc_size(int nlen)
1facf9fc 31952+{
dece6358 31953+ return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
1facf9fc 31954+}
31955+
31956+static int set_deblk_end(union au_vdir_deblk_p *p,
31957+ union au_vdir_deblk_p *deblk_end)
31958+{
31959+ if (calc_size(0) <= deblk_end->deblk - p->deblk) {
31960+ p->de->de_str.len = 0;
31961+ /* smp_mb(); */
31962+ return 0;
31963+ }
31964+ return -1; /* error */
31965+}
31966+
31967+/* returns true or false */
31968+static int is_deblk_end(union au_vdir_deblk_p *p,
31969+ union au_vdir_deblk_p *deblk_end)
31970+{
31971+ if (calc_size(0) <= deblk_end->deblk - p->deblk)
31972+ return !p->de->de_str.len;
31973+ return 1;
31974+}
31975+
31976+static unsigned char *last_deblk(struct au_vdir *vdir)
31977+{
31978+ return vdir->vd_deblk[vdir->vd_nblk - 1];
31979+}
31980+
31981+/* ---------------------------------------------------------------------- */
31982+
79b8bda9 31983+/* estimate the appropriate size for name hash table */
1308ab2a 31984+unsigned int au_rdhash_est(loff_t sz)
31985+{
31986+ unsigned int n;
31987+
31988+ n = UINT_MAX;
31989+ sz >>= 10;
31990+ if (sz < n)
31991+ n = sz;
31992+ if (sz < AUFS_RDHASH_DEF)
31993+ n = AUFS_RDHASH_DEF;
4a4d8108 31994+ /* pr_info("n %u\n", n); */
1308ab2a 31995+ return n;
31996+}
31997+
1facf9fc 31998+/*
31999+ * the allocated memory has to be freed by
dece6358 32000+ * au_nhash_wh_free() or au_nhash_de_free().
1facf9fc 32001+ */
dece6358 32002+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
1facf9fc 32003+{
1facf9fc 32004+ struct hlist_head *head;
dece6358 32005+ unsigned int u;
076b876e 32006+ size_t sz;
1facf9fc 32007+
076b876e
AM
32008+ sz = sizeof(*nhash->nh_head) * num_hash;
32009+ head = kmalloc(sz, gfp);
dece6358
AM
32010+ if (head) {
32011+ nhash->nh_num = num_hash;
32012+ nhash->nh_head = head;
32013+ for (u = 0; u < num_hash; u++)
1facf9fc 32014+ INIT_HLIST_HEAD(head++);
dece6358 32015+ return 0; /* success */
1facf9fc 32016+ }
1facf9fc 32017+
dece6358 32018+ return -ENOMEM;
1facf9fc 32019+}
32020+
dece6358
AM
32021+static void nhash_count(struct hlist_head *head)
32022+{
32023+#if 0
32024+ unsigned long n;
32025+ struct hlist_node *pos;
32026+
32027+ n = 0;
32028+ hlist_for_each(pos, head)
32029+ n++;
4a4d8108 32030+ pr_info("%lu\n", n);
dece6358
AM
32031+#endif
32032+}
32033+
32034+static void au_nhash_wh_do_free(struct hlist_head *head)
1facf9fc 32035+{
c06a8ce3
AM
32036+ struct au_vdir_wh *pos;
32037+ struct hlist_node *node;
1facf9fc 32038+
c06a8ce3 32039+ hlist_for_each_entry_safe(pos, node, head, wh_hash)
9f237c51 32040+ au_kfree_rcu(pos);
1facf9fc 32041+}
32042+
dece6358 32043+static void au_nhash_de_do_free(struct hlist_head *head)
1facf9fc 32044+{
c06a8ce3
AM
32045+ struct au_vdir_dehstr *pos;
32046+ struct hlist_node *node;
1facf9fc 32047+
c06a8ce3 32048+ hlist_for_each_entry_safe(pos, node, head, hash)
1c60b727 32049+ au_cache_free_vdir_dehstr(pos);
1facf9fc 32050+}
32051+
dece6358
AM
32052+static void au_nhash_do_free(struct au_nhash *nhash,
32053+ void (*free)(struct hlist_head *head))
1facf9fc 32054+{
1308ab2a 32055+ unsigned int n;
1facf9fc 32056+ struct hlist_head *head;
1facf9fc 32057+
dece6358 32058+ n = nhash->nh_num;
1308ab2a 32059+ if (!n)
32060+ return;
32061+
dece6358 32062+ head = nhash->nh_head;
1308ab2a 32063+ while (n-- > 0) {
dece6358
AM
32064+ nhash_count(head);
32065+ free(head++);
1facf9fc 32066+ }
9f237c51 32067+ au_kfree_try_rcu(nhash->nh_head);
1facf9fc 32068+}
32069+
dece6358 32070+void au_nhash_wh_free(struct au_nhash *whlist)
1facf9fc 32071+{
dece6358
AM
32072+ au_nhash_do_free(whlist, au_nhash_wh_do_free);
32073+}
1facf9fc 32074+
dece6358
AM
32075+static void au_nhash_de_free(struct au_nhash *delist)
32076+{
32077+ au_nhash_do_free(delist, au_nhash_de_do_free);
1facf9fc 32078+}
32079+
32080+/* ---------------------------------------------------------------------- */
32081+
32082+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
32083+ int limit)
32084+{
32085+ int num;
32086+ unsigned int u, n;
32087+ struct hlist_head *head;
c06a8ce3 32088+ struct au_vdir_wh *pos;
1facf9fc 32089+
32090+ num = 0;
32091+ n = whlist->nh_num;
32092+ head = whlist->nh_head;
1308ab2a 32093+ for (u = 0; u < n; u++, head++)
c06a8ce3
AM
32094+ hlist_for_each_entry(pos, head, wh_hash)
32095+ if (pos->wh_bindex == btgt && ++num > limit)
1facf9fc 32096+ return 1;
1facf9fc 32097+ return 0;
32098+}
32099+
32100+static struct hlist_head *au_name_hash(struct au_nhash *nhash,
dece6358 32101+ unsigned char *name,
1facf9fc 32102+ unsigned int len)
32103+{
dece6358
AM
32104+ unsigned int v;
32105+ /* const unsigned int magic_bit = 12; */
32106+
1308ab2a 32107+ AuDebugOn(!nhash->nh_num || !nhash->nh_head);
32108+
dece6358 32109+ v = 0;
f0c0a007
AM
32110+ if (len > 8)
32111+ len = 8;
dece6358
AM
32112+ while (len--)
32113+ v += *name++;
32114+ /* v = hash_long(v, magic_bit); */
32115+ v %= nhash->nh_num;
32116+ return nhash->nh_head + v;
32117+}
32118+
32119+static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
32120+ int nlen)
32121+{
32122+ return str->len == nlen && !memcmp(str->name, name, nlen);
1facf9fc 32123+}
32124+
32125+/* returns found or not */
dece6358 32126+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
1facf9fc 32127+{
32128+ struct hlist_head *head;
c06a8ce3 32129+ struct au_vdir_wh *pos;
1facf9fc 32130+ struct au_vdir_destr *str;
32131+
dece6358 32132+ head = au_name_hash(whlist, name, nlen);
c06a8ce3
AM
32133+ hlist_for_each_entry(pos, head, wh_hash) {
32134+ str = &pos->wh_str;
1facf9fc 32135+ AuDbg("%.*s\n", str->len, str->name);
dece6358
AM
32136+ if (au_nhash_test_name(str, name, nlen))
32137+ return 1;
32138+ }
32139+ return 0;
32140+}
32141+
32142+/* returns found(true) or not */
32143+static int test_known(struct au_nhash *delist, char *name, int nlen)
32144+{
32145+ struct hlist_head *head;
c06a8ce3 32146+ struct au_vdir_dehstr *pos;
dece6358
AM
32147+ struct au_vdir_destr *str;
32148+
32149+ head = au_name_hash(delist, name, nlen);
c06a8ce3
AM
32150+ hlist_for_each_entry(pos, head, hash) {
32151+ str = pos->str;
dece6358
AM
32152+ AuDbg("%.*s\n", str->len, str->name);
32153+ if (au_nhash_test_name(str, name, nlen))
1facf9fc 32154+ return 1;
32155+ }
32156+ return 0;
32157+}
32158+
dece6358
AM
32159+static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
32160+ unsigned char d_type)
32161+{
32162+#ifdef CONFIG_AUFS_SHWH
32163+ wh->wh_ino = ino;
32164+ wh->wh_type = d_type;
32165+#endif
32166+}
32167+
32168+/* ---------------------------------------------------------------------- */
32169+
32170+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
32171+ unsigned int d_type, aufs_bindex_t bindex,
32172+ unsigned char shwh)
1facf9fc 32173+{
32174+ int err;
32175+ struct au_vdir_destr *str;
32176+ struct au_vdir_wh *wh;
32177+
dece6358 32178+ AuDbg("%.*s\n", nlen, name);
1308ab2a 32179+ AuDebugOn(!whlist->nh_num || !whlist->nh_head);
32180+
1facf9fc 32181+ err = -ENOMEM;
dece6358 32182+ wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
1facf9fc 32183+ if (unlikely(!wh))
32184+ goto out;
32185+
32186+ err = 0;
32187+ wh->wh_bindex = bindex;
dece6358
AM
32188+ if (shwh)
32189+ au_shwh_init_wh(wh, ino, d_type);
1facf9fc 32190+ str = &wh->wh_str;
dece6358
AM
32191+ str->len = nlen;
32192+ memcpy(str->name, name, nlen);
32193+ hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
1facf9fc 32194+ /* smp_mb(); */
32195+
4f0767ce 32196+out:
1facf9fc 32197+ return err;
32198+}
32199+
1facf9fc 32200+static int append_deblk(struct au_vdir *vdir)
32201+{
32202+ int err;
dece6358 32203+ unsigned long ul;
1facf9fc 32204+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
32205+ union au_vdir_deblk_p p, deblk_end;
32206+ unsigned char **o;
32207+
32208+ err = -ENOMEM;
e2f27e51
AM
32209+ o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32210+ GFP_NOFS, /*may_shrink*/0);
1facf9fc 32211+ if (unlikely(!o))
32212+ goto out;
32213+
32214+ vdir->vd_deblk = o;
32215+ p.deblk = kmalloc(deblk_sz, GFP_NOFS);
32216+ if (p.deblk) {
32217+ ul = vdir->vd_nblk++;
32218+ vdir->vd_deblk[ul] = p.deblk;
32219+ vdir->vd_last.ul = ul;
32220+ vdir->vd_last.p.deblk = p.deblk;
32221+ deblk_end.deblk = p.deblk + deblk_sz;
32222+ err = set_deblk_end(&p, &deblk_end);
32223+ }
32224+
4f0767ce 32225+out:
1facf9fc 32226+ return err;
32227+}
32228+
dece6358
AM
32229+static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
32230+ unsigned int d_type, struct au_nhash *delist)
32231+{
32232+ int err;
32233+ unsigned int sz;
32234+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
32235+ union au_vdir_deblk_p p, *room, deblk_end;
32236+ struct au_vdir_dehstr *dehstr;
32237+
32238+ p.deblk = last_deblk(vdir);
32239+ deblk_end.deblk = p.deblk + deblk_sz;
32240+ room = &vdir->vd_last.p;
32241+ AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32242+ || !is_deblk_end(room, &deblk_end));
32243+
32244+ sz = calc_size(nlen);
32245+ if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32246+ err = append_deblk(vdir);
32247+ if (unlikely(err))
32248+ goto out;
32249+
32250+ p.deblk = last_deblk(vdir);
32251+ deblk_end.deblk = p.deblk + deblk_sz;
32252+ /* smp_mb(); */
32253+ AuDebugOn(room->deblk != p.deblk);
32254+ }
32255+
32256+ err = -ENOMEM;
4a4d8108 32257+ dehstr = au_cache_alloc_vdir_dehstr();
dece6358
AM
32258+ if (unlikely(!dehstr))
32259+ goto out;
32260+
32261+ dehstr->str = &room->de->de_str;
32262+ hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32263+ room->de->de_ino = ino;
32264+ room->de->de_type = d_type;
32265+ room->de->de_str.len = nlen;
32266+ memcpy(room->de->de_str.name, name, nlen);
32267+
32268+ err = 0;
32269+ room->deblk += sz;
32270+ if (unlikely(set_deblk_end(room, &deblk_end)))
32271+ err = append_deblk(vdir);
32272+ /* smp_mb(); */
32273+
4f0767ce 32274+out:
dece6358
AM
32275+ return err;
32276+}
32277+
32278+/* ---------------------------------------------------------------------- */
32279+
1c60b727 32280+void au_vdir_free(struct au_vdir *vdir)
dece6358
AM
32281+{
32282+ unsigned char **deblk;
32283+
32284+ deblk = vdir->vd_deblk;
1c60b727 32285+ while (vdir->vd_nblk--)
9f237c51
AM
32286+ au_kfree_try_rcu(*deblk++);
32287+ au_kfree_try_rcu(vdir->vd_deblk);
1c60b727 32288+ au_cache_free_vdir(vdir);
dece6358
AM
32289+}
32290+
1308ab2a 32291+static struct au_vdir *alloc_vdir(struct file *file)
1facf9fc 32292+{
32293+ struct au_vdir *vdir;
1308ab2a 32294+ struct super_block *sb;
1facf9fc 32295+ int err;
32296+
2000de60 32297+ sb = file->f_path.dentry->d_sb;
dece6358
AM
32298+ SiMustAnyLock(sb);
32299+
1facf9fc 32300+ err = -ENOMEM;
32301+ vdir = au_cache_alloc_vdir();
32302+ if (unlikely(!vdir))
32303+ goto out;
32304+
32305+ vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32306+ if (unlikely(!vdir->vd_deblk))
32307+ goto out_free;
32308+
32309+ vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
1308ab2a 32310+ if (!vdir->vd_deblk_sz) {
79b8bda9 32311+ /* estimate the appropriate size for deblk */
1308ab2a 32312+ vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
4a4d8108 32313+ /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
1308ab2a 32314+ }
1facf9fc 32315+ vdir->vd_nblk = 0;
32316+ vdir->vd_version = 0;
32317+ vdir->vd_jiffy = 0;
32318+ err = append_deblk(vdir);
32319+ if (!err)
32320+ return vdir; /* success */
32321+
9f237c51 32322+ au_kfree_try_rcu(vdir->vd_deblk);
1facf9fc 32323+
4f0767ce 32324+out_free:
1c60b727 32325+ au_cache_free_vdir(vdir);
4f0767ce 32326+out:
1facf9fc 32327+ vdir = ERR_PTR(err);
32328+ return vdir;
32329+}
32330+
32331+static int reinit_vdir(struct au_vdir *vdir)
32332+{
32333+ int err;
32334+ union au_vdir_deblk_p p, deblk_end;
32335+
32336+ while (vdir->vd_nblk > 1) {
9f237c51 32337+ au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
1facf9fc 32338+ /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32339+ vdir->vd_nblk--;
32340+ }
32341+ p.deblk = vdir->vd_deblk[0];
32342+ deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32343+ err = set_deblk_end(&p, &deblk_end);
32344+ /* keep vd_dblk_sz */
32345+ vdir->vd_last.ul = 0;
32346+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32347+ vdir->vd_version = 0;
32348+ vdir->vd_jiffy = 0;
32349+ /* smp_mb(); */
32350+ return err;
32351+}
32352+
32353+/* ---------------------------------------------------------------------- */
32354+
1facf9fc 32355+#define AuFillVdir_CALLED 1
32356+#define AuFillVdir_WHABLE (1 << 1)
dece6358 32357+#define AuFillVdir_SHWH (1 << 2)
1facf9fc 32358+#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
7f207e10
AM
32359+#define au_fset_fillvdir(flags, name) \
32360+ do { (flags) |= AuFillVdir_##name; } while (0)
32361+#define au_fclr_fillvdir(flags, name) \
32362+ do { (flags) &= ~AuFillVdir_##name; } while (0)
1facf9fc 32363+
dece6358
AM
32364+#ifndef CONFIG_AUFS_SHWH
32365+#undef AuFillVdir_SHWH
32366+#define AuFillVdir_SHWH 0
32367+#endif
32368+
1facf9fc 32369+struct fillvdir_arg {
392086de 32370+ struct dir_context ctx;
1facf9fc 32371+ struct file *file;
32372+ struct au_vdir *vdir;
dece6358
AM
32373+ struct au_nhash delist;
32374+ struct au_nhash whlist;
1facf9fc 32375+ aufs_bindex_t bindex;
32376+ unsigned int flags;
32377+ int err;
32378+};
32379+
392086de 32380+static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
1facf9fc 32381+ loff_t offset __maybe_unused, u64 h_ino,
32382+ unsigned int d_type)
32383+{
392086de 32384+ struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
1facf9fc 32385+ char *name = (void *)__name;
32386+ struct super_block *sb;
1facf9fc 32387+ ino_t ino;
dece6358 32388+ const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
1facf9fc 32389+
1facf9fc 32390+ arg->err = 0;
2000de60 32391+ sb = arg->file->f_path.dentry->d_sb;
1facf9fc 32392+ au_fset_fillvdir(arg->flags, CALLED);
32393+ /* smp_mb(); */
dece6358 32394+ if (nlen <= AUFS_WH_PFX_LEN
1facf9fc 32395+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
dece6358
AM
32396+ if (test_known(&arg->delist, name, nlen)
32397+ || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32398+ goto out; /* already exists or whiteouted */
1facf9fc 32399+
dece6358 32400+ arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
4a4d8108
AM
32401+ if (!arg->err) {
32402+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
32403+ d_type = DT_UNKNOWN;
dece6358
AM
32404+ arg->err = append_de(arg->vdir, name, nlen, ino,
32405+ d_type, &arg->delist);
4a4d8108 32406+ }
1facf9fc 32407+ } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32408+ name += AUFS_WH_PFX_LEN;
dece6358
AM
32409+ nlen -= AUFS_WH_PFX_LEN;
32410+ if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32411+ goto out; /* already whiteouted */
1facf9fc 32412+
acd2b654 32413+ ino = 0; /* just to suppress a warning */
dece6358
AM
32414+ if (shwh)
32415+ arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32416+ &ino);
4a4d8108
AM
32417+ if (!arg->err) {
32418+ if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32419+ d_type = DT_UNKNOWN;
1facf9fc 32420+ arg->err = au_nhash_append_wh
dece6358
AM
32421+ (&arg->whlist, name, nlen, ino, d_type,
32422+ arg->bindex, shwh);
4a4d8108 32423+ }
1facf9fc 32424+ }
32425+
4f0767ce 32426+out:
1facf9fc 32427+ if (!arg->err)
32428+ arg->vdir->vd_jiffy = jiffies;
32429+ /* smp_mb(); */
32430+ AuTraceErr(arg->err);
32431+ return arg->err;
32432+}
32433+
dece6358
AM
32434+static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32435+ struct au_nhash *whlist, struct au_nhash *delist)
32436+{
32437+#ifdef CONFIG_AUFS_SHWH
32438+ int err;
32439+ unsigned int nh, u;
32440+ struct hlist_head *head;
c06a8ce3
AM
32441+ struct au_vdir_wh *pos;
32442+ struct hlist_node *n;
dece6358
AM
32443+ char *p, *o;
32444+ struct au_vdir_destr *destr;
32445+
32446+ AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32447+
32448+ err = -ENOMEM;
537831f9 32449+ o = p = (void *)__get_free_page(GFP_NOFS);
dece6358
AM
32450+ if (unlikely(!p))
32451+ goto out;
32452+
32453+ err = 0;
32454+ nh = whlist->nh_num;
32455+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32456+ p += AUFS_WH_PFX_LEN;
32457+ for (u = 0; u < nh; u++) {
32458+ head = whlist->nh_head + u;
c06a8ce3
AM
32459+ hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32460+ destr = &pos->wh_str;
dece6358
AM
32461+ memcpy(p, destr->name, destr->len);
32462+ err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
c06a8ce3 32463+ pos->wh_ino, pos->wh_type, delist);
dece6358
AM
32464+ if (unlikely(err))
32465+ break;
32466+ }
32467+ }
32468+
1c60b727 32469+ free_page((unsigned long)o);
dece6358 32470+
4f0767ce 32471+out:
dece6358
AM
32472+ AuTraceErr(err);
32473+ return err;
32474+#else
32475+ return 0;
32476+#endif
32477+}
32478+
1facf9fc 32479+static int au_do_read_vdir(struct fillvdir_arg *arg)
32480+{
32481+ int err;
dece6358 32482+ unsigned int rdhash;
1facf9fc 32483+ loff_t offset;
5afbbe0d 32484+ aufs_bindex_t bbot, bindex, btop;
dece6358 32485+ unsigned char shwh;
1facf9fc 32486+ struct file *hf, *file;
32487+ struct super_block *sb;
32488+
1facf9fc 32489+ file = arg->file;
2000de60 32490+ sb = file->f_path.dentry->d_sb;
dece6358
AM
32491+ SiMustAnyLock(sb);
32492+
32493+ rdhash = au_sbi(sb)->si_rdhash;
1308ab2a 32494+ if (!rdhash)
32495+ rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
dece6358
AM
32496+ err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32497+ if (unlikely(err))
1facf9fc 32498+ goto out;
dece6358
AM
32499+ err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32500+ if (unlikely(err))
1facf9fc 32501+ goto out_delist;
32502+
32503+ err = 0;
32504+ arg->flags = 0;
dece6358
AM
32505+ shwh = 0;
32506+ if (au_opt_test(au_mntflags(sb), SHWH)) {
32507+ shwh = 1;
32508+ au_fset_fillvdir(arg->flags, SHWH);
32509+ }
5afbbe0d
AM
32510+ btop = au_fbtop(file);
32511+ bbot = au_fbbot_dir(file);
32512+ for (bindex = btop; !err && bindex <= bbot; bindex++) {
4a4d8108 32513+ hf = au_hf_dir(file, bindex);
1facf9fc 32514+ if (!hf)
32515+ continue;
32516+
32517+ offset = vfsub_llseek(hf, 0, SEEK_SET);
32518+ err = offset;
32519+ if (unlikely(offset))
32520+ break;
32521+
32522+ arg->bindex = bindex;
32523+ au_fclr_fillvdir(arg->flags, WHABLE);
dece6358 32524+ if (shwh
5afbbe0d 32525+ || (bindex != bbot
dece6358 32526+ && au_br_whable(au_sbr_perm(sb, bindex))))
1facf9fc 32527+ au_fset_fillvdir(arg->flags, WHABLE);
32528+ do {
32529+ arg->err = 0;
32530+ au_fclr_fillvdir(arg->flags, CALLED);
32531+ /* smp_mb(); */
392086de 32532+ err = vfsub_iterate_dir(hf, &arg->ctx);
1facf9fc 32533+ if (err >= 0)
32534+ err = arg->err;
32535+ } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
392086de
AM
32536+
32537+ /*
32538+ * dir_relax() may be good for concurrency, but aufs should not
32539+ * use it since it will cause a lockdep problem.
32540+ */
1facf9fc 32541+ }
dece6358
AM
32542+
32543+ if (!err && shwh)
32544+ err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32545+
32546+ au_nhash_wh_free(&arg->whlist);
1facf9fc 32547+
4f0767ce 32548+out_delist:
dece6358 32549+ au_nhash_de_free(&arg->delist);
4f0767ce 32550+out:
1facf9fc 32551+ return err;
32552+}
32553+
32554+static int read_vdir(struct file *file, int may_read)
32555+{
32556+ int err;
32557+ unsigned long expire;
32558+ unsigned char do_read;
392086de
AM
32559+ struct fillvdir_arg arg = {
32560+ .ctx = {
2000de60 32561+ .actor = fillvdir
392086de
AM
32562+ }
32563+ };
1facf9fc 32564+ struct inode *inode;
32565+ struct au_vdir *vdir, *allocated;
32566+
32567+ err = 0;
c06a8ce3 32568+ inode = file_inode(file);
1facf9fc 32569+ IMustLock(inode);
5afbbe0d 32570+ IiMustWriteLock(inode);
dece6358
AM
32571+ SiMustAnyLock(inode->i_sb);
32572+
1facf9fc 32573+ allocated = NULL;
32574+ do_read = 0;
32575+ expire = au_sbi(inode->i_sb)->si_rdcache;
32576+ vdir = au_ivdir(inode);
32577+ if (!vdir) {
32578+ do_read = 1;
1308ab2a 32579+ vdir = alloc_vdir(file);
1facf9fc 32580+ err = PTR_ERR(vdir);
32581+ if (IS_ERR(vdir))
32582+ goto out;
32583+ err = 0;
32584+ allocated = vdir;
32585+ } else if (may_read
be118d29 32586+ && (!inode_eq_iversion(inode, vdir->vd_version)
1facf9fc 32587+ || time_after(jiffies, vdir->vd_jiffy + expire))) {
32588+ do_read = 1;
32589+ err = reinit_vdir(vdir);
32590+ if (unlikely(err))
32591+ goto out;
32592+ }
32593+
32594+ if (!do_read)
32595+ return 0; /* success */
32596+
32597+ arg.file = file;
32598+ arg.vdir = vdir;
32599+ err = au_do_read_vdir(&arg);
32600+ if (!err) {
392086de 32601+ /* file->f_pos = 0; */ /* todo: ctx->pos? */
be118d29 32602+ vdir->vd_version = inode_query_iversion(inode);
1facf9fc 32603+ vdir->vd_last.ul = 0;
32604+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32605+ if (allocated)
32606+ au_set_ivdir(inode, allocated);
32607+ } else if (allocated)
1c60b727 32608+ au_vdir_free(allocated);
1facf9fc 32609+
4f0767ce 32610+out:
1facf9fc 32611+ return err;
32612+}
32613+
32614+static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32615+{
32616+ int err, rerr;
32617+ unsigned long ul, n;
32618+ const unsigned int deblk_sz = src->vd_deblk_sz;
32619+
32620+ AuDebugOn(tgt->vd_nblk != 1);
32621+
32622+ err = -ENOMEM;
32623+ if (tgt->vd_nblk < src->vd_nblk) {
32624+ unsigned char **p;
32625+
e2f27e51
AM
32626+ p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32627+ GFP_NOFS, /*may_shrink*/0);
1facf9fc 32628+ if (unlikely(!p))
32629+ goto out;
32630+ tgt->vd_deblk = p;
32631+ }
32632+
1308ab2a 32633+ if (tgt->vd_deblk_sz != deblk_sz) {
32634+ unsigned char *p;
32635+
32636+ tgt->vd_deblk_sz = deblk_sz;
e2f27e51
AM
32637+ p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32638+ /*may_shrink*/1);
1308ab2a 32639+ if (unlikely(!p))
32640+ goto out;
32641+ tgt->vd_deblk[0] = p;
32642+ }
1facf9fc 32643+ memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
1facf9fc 32644+ tgt->vd_version = src->vd_version;
32645+ tgt->vd_jiffy = src->vd_jiffy;
32646+
32647+ n = src->vd_nblk;
32648+ for (ul = 1; ul < n; ul++) {
dece6358
AM
32649+ tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32650+ GFP_NOFS);
32651+ if (unlikely(!tgt->vd_deblk[ul]))
1facf9fc 32652+ goto out;
1308ab2a 32653+ tgt->vd_nblk++;
1facf9fc 32654+ }
1308ab2a 32655+ tgt->vd_nblk = n;
32656+ tgt->vd_last.ul = tgt->vd_last.ul;
32657+ tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32658+ tgt->vd_last.p.deblk += src->vd_last.p.deblk
32659+ - src->vd_deblk[src->vd_last.ul];
1facf9fc 32660+ /* smp_mb(); */
32661+ return 0; /* success */
32662+
4f0767ce 32663+out:
1facf9fc 32664+ rerr = reinit_vdir(tgt);
32665+ BUG_ON(rerr);
32666+ return err;
32667+}
32668+
32669+int au_vdir_init(struct file *file)
32670+{
32671+ int err;
32672+ struct inode *inode;
32673+ struct au_vdir *vdir_cache, *allocated;
32674+
392086de 32675+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 32676+ err = read_vdir(file, !file->f_pos);
32677+ if (unlikely(err))
32678+ goto out;
32679+
32680+ allocated = NULL;
32681+ vdir_cache = au_fvdir_cache(file);
32682+ if (!vdir_cache) {
1308ab2a 32683+ vdir_cache = alloc_vdir(file);
1facf9fc 32684+ err = PTR_ERR(vdir_cache);
32685+ if (IS_ERR(vdir_cache))
32686+ goto out;
32687+ allocated = vdir_cache;
32688+ } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
392086de 32689+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 32690+ err = reinit_vdir(vdir_cache);
32691+ if (unlikely(err))
32692+ goto out;
32693+ } else
32694+ return 0; /* success */
32695+
c06a8ce3 32696+ inode = file_inode(file);
1facf9fc 32697+ err = copy_vdir(vdir_cache, au_ivdir(inode));
32698+ if (!err) {
be118d29 32699+ file->f_version = inode_query_iversion(inode);
1facf9fc 32700+ if (allocated)
32701+ au_set_fvdir_cache(file, allocated);
32702+ } else if (allocated)
1c60b727 32703+ au_vdir_free(allocated);
1facf9fc 32704+
4f0767ce 32705+out:
1facf9fc 32706+ return err;
32707+}
32708+
32709+static loff_t calc_offset(struct au_vdir *vdir)
32710+{
32711+ loff_t offset;
32712+ union au_vdir_deblk_p p;
32713+
32714+ p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32715+ offset = vdir->vd_last.p.deblk - p.deblk;
32716+ offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32717+ return offset;
32718+}
32719+
32720+/* returns true or false */
392086de 32721+static int seek_vdir(struct file *file, struct dir_context *ctx)
1facf9fc 32722+{
32723+ int valid;
32724+ unsigned int deblk_sz;
32725+ unsigned long ul, n;
32726+ loff_t offset;
32727+ union au_vdir_deblk_p p, deblk_end;
32728+ struct au_vdir *vdir_cache;
32729+
32730+ valid = 1;
32731+ vdir_cache = au_fvdir_cache(file);
32732+ offset = calc_offset(vdir_cache);
32733+ AuDbg("offset %lld\n", offset);
392086de 32734+ if (ctx->pos == offset)
1facf9fc 32735+ goto out;
32736+
32737+ vdir_cache->vd_last.ul = 0;
32738+ vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
392086de 32739+ if (!ctx->pos)
1facf9fc 32740+ goto out;
32741+
32742+ valid = 0;
32743+ deblk_sz = vdir_cache->vd_deblk_sz;
392086de 32744+ ul = div64_u64(ctx->pos, deblk_sz);
1facf9fc 32745+ AuDbg("ul %lu\n", ul);
32746+ if (ul >= vdir_cache->vd_nblk)
32747+ goto out;
32748+
32749+ n = vdir_cache->vd_nblk;
32750+ for (; ul < n; ul++) {
32751+ p.deblk = vdir_cache->vd_deblk[ul];
32752+ deblk_end.deblk = p.deblk + deblk_sz;
32753+ offset = ul;
32754+ offset *= deblk_sz;
392086de 32755+ while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
1facf9fc 32756+ unsigned int l;
32757+
32758+ l = calc_size(p.de->de_str.len);
32759+ offset += l;
32760+ p.deblk += l;
32761+ }
32762+ if (!is_deblk_end(&p, &deblk_end)) {
32763+ valid = 1;
32764+ vdir_cache->vd_last.ul = ul;
32765+ vdir_cache->vd_last.p = p;
32766+ break;
32767+ }
32768+ }
32769+
4f0767ce 32770+out:
1facf9fc 32771+ /* smp_mb(); */
b00004a5
AM
32772+ if (!valid)
32773+ AuDbg("valid %d\n", !valid);
1facf9fc 32774+ return valid;
32775+}
32776+
392086de 32777+int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
1facf9fc 32778+{
1facf9fc 32779+ unsigned int l, deblk_sz;
32780+ union au_vdir_deblk_p deblk_end;
32781+ struct au_vdir *vdir_cache;
32782+ struct au_vdir_de *de;
32783+
392086de 32784+ if (!seek_vdir(file, ctx))
1facf9fc 32785+ return 0;
32786+
acd2b654 32787+ vdir_cache = au_fvdir_cache(file);
1facf9fc 32788+ deblk_sz = vdir_cache->vd_deblk_sz;
32789+ while (1) {
32790+ deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32791+ deblk_end.deblk += deblk_sz;
32792+ while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32793+ de = vdir_cache->vd_last.p.de;
32794+ AuDbg("%.*s, off%lld, i%lu, dt%d\n",
392086de 32795+ de->de_str.len, de->de_str.name, ctx->pos,
1facf9fc 32796+ (unsigned long)de->de_ino, de->de_type);
392086de
AM
32797+ if (unlikely(!dir_emit(ctx, de->de_str.name,
32798+ de->de_str.len, de->de_ino,
32799+ de->de_type))) {
1facf9fc 32800+ /* todo: ignore the error caused by udba? */
32801+ /* return err; */
32802+ return 0;
32803+ }
32804+
32805+ l = calc_size(de->de_str.len);
32806+ vdir_cache->vd_last.p.deblk += l;
392086de 32807+ ctx->pos += l;
1facf9fc 32808+ }
32809+ if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32810+ vdir_cache->vd_last.ul++;
32811+ vdir_cache->vd_last.p.deblk
32812+ = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
392086de 32813+ ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
1facf9fc 32814+ continue;
32815+ }
32816+ break;
32817+ }
32818+
32819+ /* smp_mb(); */
32820+ return 0;
32821+}
7f207e10
AM
32822diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32823--- /usr/share/empty/fs/aufs/vfsub.c 1970-01-01 01:00:00.000000000 +0100
acd2b654
AM
32824+++ linux/fs/aufs/vfsub.c 2018-10-23 12:33:35.599375796 +0200
32825@@ -0,0 +1,902 @@
cd7a4cd9 32826+// SPDX-License-Identifier: GPL-2.0
1facf9fc 32827+/*
b00004a5 32828+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 32829+ *
32830+ * This program, aufs is free software; you can redistribute it and/or modify
32831+ * it under the terms of the GNU General Public License as published by
32832+ * the Free Software Foundation; either version 2 of the License, or
32833+ * (at your option) any later version.
dece6358
AM
32834+ *
32835+ * This program is distributed in the hope that it will be useful,
32836+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
32837+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32838+ * GNU General Public License for more details.
32839+ *
32840+ * You should have received a copy of the GNU General Public License
523b37e3 32841+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 32842+ */
32843+
32844+/*
32845+ * sub-routines for VFS
32846+ */
32847+
8b6a4947 32848+#include <linux/mnt_namespace.h>
dece6358 32849+#include <linux/namei.h>
8cdd5066 32850+#include <linux/nsproxy.h>
dece6358
AM
32851+#include <linux/security.h>
32852+#include <linux/splice.h>
1facf9fc 32853+#include "aufs.h"
32854+
8cdd5066
JR
32855+#ifdef CONFIG_AUFS_BR_FUSE
32856+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32857+{
8cdd5066
JR
32858+ if (!au_test_fuse(h_sb) || !au_userns)
32859+ return 0;
32860+
8b6a4947 32861+ return is_current_mnt_ns(mnt) ? 0 : -EACCES;
8cdd5066
JR
32862+}
32863+#endif
32864+
a2654f78
AM
32865+int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
32866+{
32867+ int err;
32868+
32869+ lockdep_off();
32870+ down_read(&h_sb->s_umount);
32871+ err = __sync_filesystem(h_sb, wait);
32872+ up_read(&h_sb->s_umount);
32873+ lockdep_on();
32874+
32875+ return err;
32876+}
32877+
8cdd5066
JR
32878+/* ---------------------------------------------------------------------- */
32879+
1facf9fc 32880+int vfsub_update_h_iattr(struct path *h_path, int *did)
32881+{
32882+ int err;
32883+ struct kstat st;
32884+ struct super_block *h_sb;
32885+
32886+ /* for remote fs, leave work for its getattr or d_revalidate */
32887+ /* for bad i_attr fs, handle them in aufs_getattr() */
32888+ /* still some fs may acquire i_mutex. we need to skip them */
32889+ err = 0;
32890+ if (!did)
32891+ did = &err;
32892+ h_sb = h_path->dentry->d_sb;
32893+ *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32894+ if (*did)
521ced18 32895+ err = vfsub_getattr(h_path, &st);
1facf9fc 32896+
32897+ return err;
32898+}
32899+
32900+/* ---------------------------------------------------------------------- */
32901+
4a4d8108 32902+struct file *vfsub_dentry_open(struct path *path, int flags)
1308ab2a 32903+{
32904+ struct file *file;
32905+
b4510431 32906+ file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
7f207e10 32907+ current_cred());
2cbb1c4b
JR
32908+ if (!IS_ERR_OR_NULL(file)
32909+ && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
5527c038 32910+ i_readcount_inc(d_inode(path->dentry));
4a4d8108 32911+
1308ab2a 32912+ return file;
32913+}
32914+
1facf9fc 32915+struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32916+{
32917+ struct file *file;
32918+
2cbb1c4b 32919+ lockdep_off();
7f207e10 32920+ file = filp_open(path,
2cbb1c4b 32921+ oflags /* | __FMODE_NONOTIFY */,
7f207e10 32922+ mode);
2cbb1c4b 32923+ lockdep_on();
1facf9fc 32924+ if (IS_ERR(file))
32925+ goto out;
32926+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32927+
4f0767ce 32928+out:
1facf9fc 32929+ return file;
32930+}
32931+
b912730e
AM
32932+/*
32933+ * Ideally this function should call VFS:do_last() in order to keep all its
32934+ * checkings. But it is very hard for aufs to regenerate several VFS internal
32935+ * structure such as nameidata. This is a second (or third) best approach.
32936+ * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32937+ */
32938+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
acd2b654 32939+ struct vfsub_aopen_args *args)
b912730e
AM
32940+{
32941+ int err;
acd2b654 32942+ struct au_branch *br = args->br;
b912730e
AM
32943+ struct file *file = args->file;
32944+ /* copied from linux/fs/namei.c:atomic_open() */
32945+ struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
32946+
32947+ IMustLock(dir);
32948+ AuDebugOn(!dir->i_op->atomic_open);
32949+
32950+ err = au_br_test_oflag(args->open_flag, br);
32951+ if (unlikely(err))
32952+ goto out;
32953+
acd2b654
AM
32954+ au_lcnt_inc(&br->br_nfiles);
32955+ file->f_path.dentry = DENTRY_NOT_SET;
32956+ file->f_path.mnt = au_br_mnt(br);
32957+ AuDbg("%ps\n", dir->i_op->atomic_open);
b912730e 32958+ err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
acd2b654
AM
32959+ args->create_mode);
32960+ if (unlikely(err < 0)) {
32961+ au_lcnt_dec(&br->br_nfiles);
b912730e 32962+ goto out;
acd2b654 32963+ }
b912730e 32964+
acd2b654
AM
32965+ /* temporary workaround for nfsv4 branch */
32966+ if (au_test_nfs(dir->i_sb))
32967+ nfs_mark_for_revalidate(dir);
b912730e 32968+
acd2b654
AM
32969+ if (file->f_mode & FMODE_CREATED)
32970+ fsnotify_create(dir, dentry);
32971+ if (!(file->f_mode & FMODE_OPENED)) {
32972+ au_lcnt_dec(&br->br_nfiles);
32973+ goto out;
b912730e
AM
32974+ }
32975+
acd2b654
AM
32976+ /* todo: call VFS:may_open() here */
32977+ /* todo: ima_file_check() too? */
32978+ if (!err && (args->open_flag & __FMODE_EXEC))
32979+ err = deny_write_access(file);
32980+ if (!err)
32981+ fsnotify_open(file);
32982+ else
32983+ au_lcnt_dec(&br->br_nfiles);
32984+ /* note that the file is created and still opened */
b912730e
AM
32985+
32986+out:
32987+ return err;
32988+}
32989+
1facf9fc 32990+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
32991+{
32992+ int err;
32993+
1facf9fc 32994+ err = kern_path(name, flags, path);
5527c038 32995+ if (!err && d_is_positive(path->dentry))
1facf9fc 32996+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
32997+ return err;
32998+}
32999+
febd17d6
JR
33000+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33001+ struct dentry *parent, int len)
33002+{
33003+ struct path path = {
33004+ .mnt = NULL
33005+ };
33006+
33007+ path.dentry = lookup_one_len_unlocked(name, parent, len);
33008+ if (IS_ERR(path.dentry))
33009+ goto out;
33010+ if (d_is_positive(path.dentry))
33011+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33012+
33013+out:
33014+ AuTraceErrPtr(path.dentry);
33015+ return path.dentry;
33016+}
33017+
1facf9fc 33018+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
33019+ int len)
33020+{
33021+ struct path path = {
33022+ .mnt = NULL
33023+ };
33024+
1308ab2a 33025+ /* VFS checks it too, but by WARN_ON_ONCE() */
5527c038 33026+ IMustLock(d_inode(parent));
1facf9fc 33027+
33028+ path.dentry = lookup_one_len(name, parent, len);
33029+ if (IS_ERR(path.dentry))
33030+ goto out;
5527c038 33031+ if (d_is_positive(path.dentry))
1facf9fc 33032+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33033+
4f0767ce 33034+out:
4a4d8108 33035+ AuTraceErrPtr(path.dentry);
1facf9fc 33036+ return path.dentry;
33037+}
33038+
b4510431 33039+void vfsub_call_lkup_one(void *args)
2cbb1c4b 33040+{
b4510431
AM
33041+ struct vfsub_lkup_one_args *a = args;
33042+ *a->errp = vfsub_lkup_one(a->name, a->parent);
2cbb1c4b
JR
33043+}
33044+
1facf9fc 33045+/* ---------------------------------------------------------------------- */
33046+
33047+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33048+ struct dentry *d2, struct au_hinode *hdir2)
33049+{
33050+ struct dentry *d;
33051+
2cbb1c4b 33052+ lockdep_off();
1facf9fc 33053+ d = lock_rename(d1, d2);
2cbb1c4b 33054+ lockdep_on();
4a4d8108 33055+ au_hn_suspend(hdir1);
1facf9fc 33056+ if (hdir1 != hdir2)
4a4d8108 33057+ au_hn_suspend(hdir2);
1facf9fc 33058+
33059+ return d;
33060+}
33061+
33062+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33063+ struct dentry *d2, struct au_hinode *hdir2)
33064+{
4a4d8108 33065+ au_hn_resume(hdir1);
1facf9fc 33066+ if (hdir1 != hdir2)
4a4d8108 33067+ au_hn_resume(hdir2);
2cbb1c4b 33068+ lockdep_off();
1facf9fc 33069+ unlock_rename(d1, d2);
2cbb1c4b 33070+ lockdep_on();
1facf9fc 33071+}
33072+
33073+/* ---------------------------------------------------------------------- */
33074+
b4510431 33075+int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
1facf9fc 33076+{
33077+ int err;
33078+ struct dentry *d;
33079+
33080+ IMustLock(dir);
33081+
33082+ d = path->dentry;
33083+ path->dentry = d->d_parent;
b752ccd1 33084+ err = security_path_mknod(path, d, mode, 0);
1facf9fc 33085+ path->dentry = d;
33086+ if (unlikely(err))
33087+ goto out;
33088+
c1595e42 33089+ lockdep_off();
b4510431 33090+ err = vfs_create(dir, path->dentry, mode, want_excl);
c1595e42 33091+ lockdep_on();
1facf9fc 33092+ if (!err) {
33093+ struct path tmp = *path;
33094+ int did;
33095+
33096+ vfsub_update_h_iattr(&tmp, &did);
33097+ if (did) {
33098+ tmp.dentry = path->dentry->d_parent;
33099+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33100+ }
33101+ /*ignore*/
33102+ }
33103+
4f0767ce 33104+out:
1facf9fc 33105+ return err;
33106+}
33107+
33108+int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
33109+{
33110+ int err;
33111+ struct dentry *d;
33112+
33113+ IMustLock(dir);
33114+
33115+ d = path->dentry;
33116+ path->dentry = d->d_parent;
b752ccd1 33117+ err = security_path_symlink(path, d, symname);
1facf9fc 33118+ path->dentry = d;
33119+ if (unlikely(err))
33120+ goto out;
33121+
c1595e42 33122+ lockdep_off();
1facf9fc 33123+ err = vfs_symlink(dir, path->dentry, symname);
c1595e42 33124+ lockdep_on();
1facf9fc 33125+ if (!err) {
33126+ struct path tmp = *path;
33127+ int did;
33128+
33129+ vfsub_update_h_iattr(&tmp, &did);
33130+ if (did) {
33131+ tmp.dentry = path->dentry->d_parent;
33132+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33133+ }
33134+ /*ignore*/
33135+ }
33136+
4f0767ce 33137+out:
1facf9fc 33138+ return err;
33139+}
33140+
33141+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
33142+{
33143+ int err;
33144+ struct dentry *d;
33145+
33146+ IMustLock(dir);
33147+
33148+ d = path->dentry;
33149+ path->dentry = d->d_parent;
027c5e7a 33150+ err = security_path_mknod(path, d, mode, new_encode_dev(dev));
1facf9fc 33151+ path->dentry = d;
33152+ if (unlikely(err))
33153+ goto out;
33154+
c1595e42 33155+ lockdep_off();
1facf9fc 33156+ err = vfs_mknod(dir, path->dentry, mode, dev);
c1595e42 33157+ lockdep_on();
1facf9fc 33158+ if (!err) {
33159+ struct path tmp = *path;
33160+ int did;
33161+
33162+ vfsub_update_h_iattr(&tmp, &did);
33163+ if (did) {
33164+ tmp.dentry = path->dentry->d_parent;
33165+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33166+ }
33167+ /*ignore*/
33168+ }
33169+
4f0767ce 33170+out:
1facf9fc 33171+ return err;
33172+}
33173+
33174+static int au_test_nlink(struct inode *inode)
33175+{
33176+ const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33177+
33178+ if (!au_test_fs_no_limit_nlink(inode->i_sb)
33179+ || inode->i_nlink < link_max)
33180+ return 0;
33181+ return -EMLINK;
33182+}
33183+
523b37e3
AM
33184+int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33185+ struct inode **delegated_inode)
1facf9fc 33186+{
33187+ int err;
33188+ struct dentry *d;
33189+
33190+ IMustLock(dir);
33191+
5527c038 33192+ err = au_test_nlink(d_inode(src_dentry));
1facf9fc 33193+ if (unlikely(err))
33194+ return err;
33195+
b4510431 33196+ /* we don't call may_linkat() */
1facf9fc 33197+ d = path->dentry;
33198+ path->dentry = d->d_parent;
b752ccd1 33199+ err = security_path_link(src_dentry, path, d);
1facf9fc 33200+ path->dentry = d;
33201+ if (unlikely(err))
33202+ goto out;
33203+
2cbb1c4b 33204+ lockdep_off();
523b37e3 33205+ err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
2cbb1c4b 33206+ lockdep_on();
1facf9fc 33207+ if (!err) {
33208+ struct path tmp = *path;
33209+ int did;
33210+
33211+ /* fuse has different memory inode for the same inumber */
33212+ vfsub_update_h_iattr(&tmp, &did);
33213+ if (did) {
33214+ tmp.dentry = path->dentry->d_parent;
33215+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33216+ tmp.dentry = src_dentry;
33217+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33218+ }
33219+ /*ignore*/
33220+ }
33221+
4f0767ce 33222+out:
1facf9fc 33223+ return err;
33224+}
33225+
33226+int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
523b37e3 33227+ struct inode *dir, struct path *path,
f2c43d5f 33228+ struct inode **delegated_inode, unsigned int flags)
1facf9fc 33229+{
33230+ int err;
33231+ struct path tmp = {
33232+ .mnt = path->mnt
33233+ };
33234+ struct dentry *d;
33235+
33236+ IMustLock(dir);
33237+ IMustLock(src_dir);
33238+
33239+ d = path->dentry;
33240+ path->dentry = d->d_parent;
33241+ tmp.dentry = src_dentry->d_parent;
38d290e6 33242+ err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
1facf9fc 33243+ path->dentry = d;
33244+ if (unlikely(err))
33245+ goto out;
33246+
2cbb1c4b 33247+ lockdep_off();
523b37e3 33248+ err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
f2c43d5f 33249+ delegated_inode, flags);
2cbb1c4b 33250+ lockdep_on();
1facf9fc 33251+ if (!err) {
33252+ int did;
33253+
33254+ tmp.dentry = d->d_parent;
33255+ vfsub_update_h_iattr(&tmp, &did);
33256+ if (did) {
33257+ tmp.dentry = src_dentry;
33258+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33259+ tmp.dentry = src_dentry->d_parent;
33260+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33261+ }
33262+ /*ignore*/
33263+ }
33264+
4f0767ce 33265+out:
1facf9fc 33266+ return err;
33267+}
33268+
33269+int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33270+{
33271+ int err;
33272+ struct dentry *d;
33273+
33274+ IMustLock(dir);
33275+
33276+ d = path->dentry;
33277+ path->dentry = d->d_parent;
b752ccd1 33278+ err = security_path_mkdir(path, d, mode);
1facf9fc 33279+ path->dentry = d;
33280+ if (unlikely(err))
33281+ goto out;
33282+
c1595e42 33283+ lockdep_off();
1facf9fc 33284+ err = vfs_mkdir(dir, path->dentry, mode);
c1595e42 33285+ lockdep_on();
1facf9fc 33286+ if (!err) {
33287+ struct path tmp = *path;
33288+ int did;
33289+
33290+ vfsub_update_h_iattr(&tmp, &did);
33291+ if (did) {
33292+ tmp.dentry = path->dentry->d_parent;
33293+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33294+ }
33295+ /*ignore*/
33296+ }
33297+
4f0767ce 33298+out:
1facf9fc 33299+ return err;
33300+}
33301+
33302+int vfsub_rmdir(struct inode *dir, struct path *path)
33303+{
33304+ int err;
33305+ struct dentry *d;
33306+
33307+ IMustLock(dir);
33308+
33309+ d = path->dentry;
33310+ path->dentry = d->d_parent;
b752ccd1 33311+ err = security_path_rmdir(path, d);
1facf9fc 33312+ path->dentry = d;
33313+ if (unlikely(err))
33314+ goto out;
33315+
2cbb1c4b 33316+ lockdep_off();
1facf9fc 33317+ err = vfs_rmdir(dir, path->dentry);
2cbb1c4b 33318+ lockdep_on();
1facf9fc 33319+ if (!err) {
33320+ struct path tmp = {
33321+ .dentry = path->dentry->d_parent,
33322+ .mnt = path->mnt
33323+ };
33324+
33325+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33326+ }
33327+
4f0767ce 33328+out:
1facf9fc 33329+ return err;
33330+}
33331+
33332+/* ---------------------------------------------------------------------- */
33333+
9dbd164d 33334+/* todo: support mmap_sem? */
1facf9fc 33335+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33336+ loff_t *ppos)
33337+{
33338+ ssize_t err;
33339+
2cbb1c4b 33340+ lockdep_off();
1facf9fc 33341+ err = vfs_read(file, ubuf, count, ppos);
2cbb1c4b 33342+ lockdep_on();
1facf9fc 33343+ if (err >= 0)
33344+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33345+ return err;
33346+}
33347+
33348+/* todo: kernel_read()? */
33349+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33350+ loff_t *ppos)
33351+{
33352+ ssize_t err;
33353+ mm_segment_t oldfs;
b752ccd1
AM
33354+ union {
33355+ void *k;
33356+ char __user *u;
33357+ } buf;
1facf9fc 33358+
b752ccd1 33359+ buf.k = kbuf;
1facf9fc 33360+ oldfs = get_fs();
33361+ set_fs(KERNEL_DS);
b752ccd1 33362+ err = vfsub_read_u(file, buf.u, count, ppos);
1facf9fc 33363+ set_fs(oldfs);
33364+ return err;
33365+}
33366+
33367+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33368+ loff_t *ppos)
33369+{
33370+ ssize_t err;
33371+
2cbb1c4b 33372+ lockdep_off();
1facf9fc 33373+ err = vfs_write(file, ubuf, count, ppos);
2cbb1c4b 33374+ lockdep_on();
1facf9fc 33375+ if (err >= 0)
33376+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33377+ return err;
33378+}
33379+
33380+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33381+{
33382+ ssize_t err;
33383+ mm_segment_t oldfs;
b752ccd1
AM
33384+ union {
33385+ void *k;
33386+ const char __user *u;
33387+ } buf;
1facf9fc 33388+
b752ccd1 33389+ buf.k = kbuf;
1facf9fc 33390+ oldfs = get_fs();
33391+ set_fs(KERNEL_DS);
b752ccd1 33392+ err = vfsub_write_u(file, buf.u, count, ppos);
1facf9fc 33393+ set_fs(oldfs);
33394+ return err;
33395+}
33396+
4a4d8108
AM
33397+int vfsub_flush(struct file *file, fl_owner_t id)
33398+{
33399+ int err;
33400+
33401+ err = 0;
523b37e3 33402+ if (file->f_op->flush) {
2000de60 33403+ if (!au_test_nfs(file->f_path.dentry->d_sb))
2cbb1c4b
JR
33404+ err = file->f_op->flush(file, id);
33405+ else {
33406+ lockdep_off();
33407+ err = file->f_op->flush(file, id);
33408+ lockdep_on();
33409+ }
4a4d8108
AM
33410+ if (!err)
33411+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33412+ /*ignore*/
33413+ }
33414+ return err;
33415+}
33416+
392086de 33417+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
1facf9fc 33418+{
33419+ int err;
33420+
062440b3 33421+ AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
392086de 33422+
2cbb1c4b 33423+ lockdep_off();
392086de 33424+ err = iterate_dir(file, ctx);
2cbb1c4b 33425+ lockdep_on();
1facf9fc 33426+ if (err >= 0)
33427+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
1c60b727 33428+
1facf9fc 33429+ return err;
33430+}
33431+
33432+long vfsub_splice_to(struct file *in, loff_t *ppos,
33433+ struct pipe_inode_info *pipe, size_t len,
33434+ unsigned int flags)
33435+{
33436+ long err;
33437+
2cbb1c4b 33438+ lockdep_off();
0fc653ad 33439+ err = do_splice_to(in, ppos, pipe, len, flags);
2cbb1c4b 33440+ lockdep_on();
4a4d8108 33441+ file_accessed(in);
1facf9fc 33442+ if (err >= 0)
33443+ vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33444+ return err;
33445+}
33446+
33447+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33448+ loff_t *ppos, size_t len, unsigned int flags)
33449+{
33450+ long err;
33451+
2cbb1c4b 33452+ lockdep_off();
0fc653ad 33453+ err = do_splice_from(pipe, out, ppos, len, flags);
2cbb1c4b 33454+ lockdep_on();
1facf9fc 33455+ if (err >= 0)
33456+ vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33457+ return err;
33458+}
33459+
53392da6
AM
33460+int vfsub_fsync(struct file *file, struct path *path, int datasync)
33461+{
33462+ int err;
33463+
33464+ /* file can be NULL */
33465+ lockdep_off();
33466+ err = vfs_fsync(file, datasync);
33467+ lockdep_on();
33468+ if (!err) {
33469+ if (!path) {
33470+ AuDebugOn(!file);
33471+ path = &file->f_path;
33472+ }
33473+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33474+ }
33475+ return err;
33476+}
33477+
1facf9fc 33478+/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33479+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33480+ struct file *h_file)
33481+{
33482+ int err;
33483+ struct inode *h_inode;
c06a8ce3 33484+ struct super_block *h_sb;
1facf9fc 33485+
1facf9fc 33486+ if (!h_file) {
c06a8ce3
AM
33487+ err = vfsub_truncate(h_path, length);
33488+ goto out;
1facf9fc 33489+ }
33490+
5527c038 33491+ h_inode = d_inode(h_path->dentry);
c06a8ce3
AM
33492+ h_sb = h_inode->i_sb;
33493+ lockdep_off();
33494+ sb_start_write(h_sb);
33495+ lockdep_on();
1facf9fc 33496+ err = locks_verify_truncate(h_inode, h_file, length);
33497+ if (!err)
953406b4 33498+ err = security_path_truncate(h_path);
2cbb1c4b
JR
33499+ if (!err) {
33500+ lockdep_off();
1facf9fc 33501+ err = do_truncate(h_path->dentry, length, attr, h_file);
2cbb1c4b
JR
33502+ lockdep_on();
33503+ }
c06a8ce3
AM
33504+ lockdep_off();
33505+ sb_end_write(h_sb);
33506+ lockdep_on();
1facf9fc 33507+
4f0767ce 33508+out:
1facf9fc 33509+ return err;
33510+}
33511+
33512+/* ---------------------------------------------------------------------- */
33513+
33514+struct au_vfsub_mkdir_args {
33515+ int *errp;
33516+ struct inode *dir;
33517+ struct path *path;
33518+ int mode;
33519+};
33520+
33521+static void au_call_vfsub_mkdir(void *args)
33522+{
33523+ struct au_vfsub_mkdir_args *a = args;
33524+ *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33525+}
33526+
33527+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33528+{
33529+ int err, do_sio, wkq_err;
33530+
33531+ do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
33532+ if (!do_sio) {
33533+ lockdep_off();
1facf9fc 33534+ err = vfsub_mkdir(dir, path, mode);
c1595e42
JR
33535+ lockdep_on();
33536+ } else {
1facf9fc 33537+ struct au_vfsub_mkdir_args args = {
33538+ .errp = &err,
33539+ .dir = dir,
33540+ .path = path,
33541+ .mode = mode
33542+ };
33543+ wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33544+ if (unlikely(wkq_err))
33545+ err = wkq_err;
33546+ }
33547+
33548+ return err;
33549+}
33550+
33551+struct au_vfsub_rmdir_args {
33552+ int *errp;
33553+ struct inode *dir;
33554+ struct path *path;
33555+};
33556+
33557+static void au_call_vfsub_rmdir(void *args)
33558+{
33559+ struct au_vfsub_rmdir_args *a = args;
33560+ *a->errp = vfsub_rmdir(a->dir, a->path);
33561+}
33562+
33563+int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33564+{
33565+ int err, do_sio, wkq_err;
33566+
33567+ do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
33568+ if (!do_sio) {
33569+ lockdep_off();
1facf9fc 33570+ err = vfsub_rmdir(dir, path);
c1595e42
JR
33571+ lockdep_on();
33572+ } else {
1facf9fc 33573+ struct au_vfsub_rmdir_args args = {
33574+ .errp = &err,
33575+ .dir = dir,
33576+ .path = path
33577+ };
33578+ wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33579+ if (unlikely(wkq_err))
33580+ err = wkq_err;
33581+ }
33582+
33583+ return err;
33584+}
33585+
33586+/* ---------------------------------------------------------------------- */
33587+
33588+struct notify_change_args {
33589+ int *errp;
33590+ struct path *path;
33591+ struct iattr *ia;
523b37e3 33592+ struct inode **delegated_inode;
1facf9fc 33593+};
33594+
33595+static void call_notify_change(void *args)
33596+{
33597+ struct notify_change_args *a = args;
33598+ struct inode *h_inode;
33599+
5527c038 33600+ h_inode = d_inode(a->path->dentry);
1facf9fc 33601+ IMustLock(h_inode);
33602+
33603+ *a->errp = -EPERM;
33604+ if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
c1595e42 33605+ lockdep_off();
523b37e3
AM
33606+ *a->errp = notify_change(a->path->dentry, a->ia,
33607+ a->delegated_inode);
c1595e42 33608+ lockdep_on();
1facf9fc 33609+ if (!*a->errp)
33610+ vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33611+ }
33612+ AuTraceErr(*a->errp);
33613+}
33614+
523b37e3
AM
33615+int vfsub_notify_change(struct path *path, struct iattr *ia,
33616+ struct inode **delegated_inode)
1facf9fc 33617+{
33618+ int err;
33619+ struct notify_change_args args = {
523b37e3
AM
33620+ .errp = &err,
33621+ .path = path,
33622+ .ia = ia,
33623+ .delegated_inode = delegated_inode
1facf9fc 33624+ };
33625+
33626+ call_notify_change(&args);
33627+
33628+ return err;
33629+}
33630+
523b37e3
AM
33631+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33632+ struct inode **delegated_inode)
1facf9fc 33633+{
33634+ int err, wkq_err;
33635+ struct notify_change_args args = {
523b37e3
AM
33636+ .errp = &err,
33637+ .path = path,
33638+ .ia = ia,
33639+ .delegated_inode = delegated_inode
1facf9fc 33640+ };
33641+
33642+ wkq_err = au_wkq_wait(call_notify_change, &args);
33643+ if (unlikely(wkq_err))
33644+ err = wkq_err;
33645+
33646+ return err;
33647+}
33648+
33649+/* ---------------------------------------------------------------------- */
33650+
33651+struct unlink_args {
33652+ int *errp;
33653+ struct inode *dir;
33654+ struct path *path;
523b37e3 33655+ struct inode **delegated_inode;
1facf9fc 33656+};
33657+
33658+static void call_unlink(void *args)
33659+{
33660+ struct unlink_args *a = args;
33661+ struct dentry *d = a->path->dentry;
33662+ struct inode *h_inode;
33663+ const int stop_sillyrename = (au_test_nfs(d->d_sb)
c1595e42 33664+ && au_dcount(d) == 1);
1facf9fc 33665+
33666+ IMustLock(a->dir);
33667+
33668+ a->path->dentry = d->d_parent;
33669+ *a->errp = security_path_unlink(a->path, d);
33670+ a->path->dentry = d;
33671+ if (unlikely(*a->errp))
33672+ return;
33673+
33674+ if (!stop_sillyrename)
33675+ dget(d);
5527c038
JR
33676+ h_inode = NULL;
33677+ if (d_is_positive(d)) {
33678+ h_inode = d_inode(d);
027c5e7a 33679+ ihold(h_inode);
5527c038 33680+ }
1facf9fc 33681+
2cbb1c4b 33682+ lockdep_off();
523b37e3 33683+ *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
2cbb1c4b 33684+ lockdep_on();
1facf9fc 33685+ if (!*a->errp) {
33686+ struct path tmp = {
33687+ .dentry = d->d_parent,
33688+ .mnt = a->path->mnt
33689+ };
33690+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33691+ }
33692+
33693+ if (!stop_sillyrename)
33694+ dput(d);
33695+ if (h_inode)
33696+ iput(h_inode);
33697+
33698+ AuTraceErr(*a->errp);
33699+}
33700+
33701+/*
33702+ * @dir: must be locked.
33703+ * @dentry: target dentry.
33704+ */
523b37e3
AM
33705+int vfsub_unlink(struct inode *dir, struct path *path,
33706+ struct inode **delegated_inode, int force)
1facf9fc 33707+{
33708+ int err;
33709+ struct unlink_args args = {
523b37e3
AM
33710+ .errp = &err,
33711+ .dir = dir,
33712+ .path = path,
33713+ .delegated_inode = delegated_inode
1facf9fc 33714+ };
33715+
33716+ if (!force)
33717+ call_unlink(&args);
33718+ else {
33719+ int wkq_err;
33720+
33721+ wkq_err = au_wkq_wait(call_unlink, &args);
33722+ if (unlikely(wkq_err))
33723+ err = wkq_err;
33724+ }
33725+
33726+ return err;
33727+}
7f207e10
AM
33728diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33729--- /usr/share/empty/fs/aufs/vfsub.h 1970-01-01 01:00:00.000000000 +0100
9f237c51 33730+++ linux/fs/aufs/vfsub.h 2018-12-27 13:19:17.711749485 +0100
062440b3
AM
33731@@ -0,0 +1,355 @@
33732+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 33733+/*
b00004a5 33734+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 33735+ *
33736+ * This program, aufs is free software; you can redistribute it and/or modify
33737+ * it under the terms of the GNU General Public License as published by
33738+ * the Free Software Foundation; either version 2 of the License, or
33739+ * (at your option) any later version.
dece6358
AM
33740+ *
33741+ * This program is distributed in the hope that it will be useful,
33742+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33743+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33744+ * GNU General Public License for more details.
33745+ *
33746+ * You should have received a copy of the GNU General Public License
523b37e3 33747+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 33748+ */
33749+
33750+/*
33751+ * sub-routines for VFS
33752+ */
33753+
33754+#ifndef __AUFS_VFSUB_H__
33755+#define __AUFS_VFSUB_H__
33756+
33757+#ifdef __KERNEL__
33758+
33759+#include <linux/fs.h>
be118d29 33760+#include <linux/iversion.h>
b4510431 33761+#include <linux/mount.h>
8cdd5066 33762+#include <linux/posix_acl.h>
c1595e42 33763+#include <linux/xattr.h>
7f207e10 33764+#include "debug.h"
1facf9fc 33765+
7f207e10 33766+/* copied from linux/fs/internal.h */
2cbb1c4b 33767+/* todo: BAD approach!! */
c06a8ce3 33768+extern void __mnt_drop_write(struct vfsmount *);
acd2b654 33769+extern struct file *alloc_empty_file(int, const struct cred *);
7f207e10
AM
33770+
33771+/* ---------------------------------------------------------------------- */
1facf9fc 33772+
33773+/* lock subclass for lower inode */
33774+/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33775+/* reduce? gave up. */
33776+enum {
c1595e42 33777+ AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
1facf9fc 33778+ AuLsc_I_PARENT, /* lower inode, parent first */
33779+ AuLsc_I_PARENT2, /* copyup dirs */
dece6358 33780+ AuLsc_I_PARENT3, /* copyup wh */
1facf9fc 33781+ AuLsc_I_CHILD,
33782+ AuLsc_I_CHILD2,
33783+ AuLsc_I_End
33784+};
33785+
33786+/* to debug easier, do not make them inlined functions */
33787+#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
febd17d6 33788+#define IMustLock(i) AuDebugOn(!inode_is_locked(i))
1facf9fc 33789+
33790+/* ---------------------------------------------------------------------- */
33791+
7f207e10
AM
33792+static inline void vfsub_drop_nlink(struct inode *inode)
33793+{
33794+ AuDebugOn(!inode->i_nlink);
33795+ drop_nlink(inode);
33796+}
33797+
027c5e7a
AM
33798+static inline void vfsub_dead_dir(struct inode *inode)
33799+{
33800+ AuDebugOn(!S_ISDIR(inode->i_mode));
33801+ inode->i_flags |= S_DEAD;
33802+ clear_nlink(inode);
33803+}
33804+
392086de
AM
33805+static inline int vfsub_native_ro(struct inode *inode)
33806+{
8b6a4947 33807+ return sb_rdonly(inode->i_sb)
392086de
AM
33808+ || IS_RDONLY(inode)
33809+ /* || IS_APPEND(inode) */
33810+ || IS_IMMUTABLE(inode);
33811+}
33812+
8cdd5066
JR
33813+#ifdef CONFIG_AUFS_BR_FUSE
33814+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33815+#else
33816+AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33817+#endif
33818+
a2654f78
AM
33819+int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
33820+
7f207e10
AM
33821+/* ---------------------------------------------------------------------- */
33822+
33823+int vfsub_update_h_iattr(struct path *h_path, int *did);
33824+struct file *vfsub_dentry_open(struct path *path, int flags);
33825+struct file *vfsub_filp_open(const char *path, int oflags, int mode);
acd2b654 33826+struct au_branch;
b912730e 33827+struct vfsub_aopen_args {
acd2b654
AM
33828+ struct file *file;
33829+ unsigned int open_flag;
33830+ umode_t create_mode;
33831+ struct au_branch *br;
b912730e 33832+};
b912730e 33833+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
acd2b654 33834+ struct vfsub_aopen_args *args);
1facf9fc 33835+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
b4510431 33836+
febd17d6
JR
33837+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33838+ struct dentry *parent, int len);
1facf9fc 33839+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
33840+ int len);
b4510431
AM
33841+
33842+struct vfsub_lkup_one_args {
33843+ struct dentry **errp;
33844+ struct qstr *name;
33845+ struct dentry *parent;
33846+};
33847+
33848+static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33849+ struct dentry *parent)
33850+{
33851+ return vfsub_lookup_one_len(name->name, parent, name->len);
33852+}
33853+
33854+void vfsub_call_lkup_one(void *args);
33855+
33856+/* ---------------------------------------------------------------------- */
33857+
33858+static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33859+{
33860+ int err;
076b876e 33861+
b4510431
AM
33862+ lockdep_off();
33863+ err = mnt_want_write(mnt);
33864+ lockdep_on();
33865+ return err;
33866+}
33867+
33868+static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33869+{
33870+ lockdep_off();
33871+ mnt_drop_write(mnt);
33872+ lockdep_on();
33873+}
1facf9fc 33874+
7e9cd9fe 33875+#if 0 /* reserved */
c06a8ce3
AM
33876+static inline void vfsub_mnt_drop_write_file(struct file *file)
33877+{
33878+ lockdep_off();
33879+ mnt_drop_write_file(file);
33880+ lockdep_on();
33881+}
7e9cd9fe 33882+#endif
c06a8ce3 33883+
1facf9fc 33884+/* ---------------------------------------------------------------------- */
33885+
33886+struct au_hinode;
33887+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33888+ struct dentry *d2, struct au_hinode *hdir2);
33889+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33890+ struct dentry *d2, struct au_hinode *hdir2);
33891+
537831f9
AM
33892+int vfsub_create(struct inode *dir, struct path *path, int mode,
33893+ bool want_excl);
1facf9fc 33894+int vfsub_symlink(struct inode *dir, struct path *path,
33895+ const char *symname);
33896+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33897+int vfsub_link(struct dentry *src_dentry, struct inode *dir,
523b37e3 33898+ struct path *path, struct inode **delegated_inode);
1facf9fc 33899+int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
523b37e3 33900+ struct inode *hdir, struct path *path,
f2c43d5f 33901+ struct inode **delegated_inode, unsigned int flags);
1facf9fc 33902+int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33903+int vfsub_rmdir(struct inode *dir, struct path *path);
33904+
33905+/* ---------------------------------------------------------------------- */
33906+
33907+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33908+ loff_t *ppos);
33909+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33910+ loff_t *ppos);
33911+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33912+ loff_t *ppos);
33913+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33914+ loff_t *ppos);
4a4d8108 33915+int vfsub_flush(struct file *file, fl_owner_t id);
392086de
AM
33916+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33917+
c06a8ce3
AM
33918+static inline loff_t vfsub_f_size_read(struct file *file)
33919+{
33920+ return i_size_read(file_inode(file));
33921+}
33922+
4a4d8108
AM
33923+static inline unsigned int vfsub_file_flags(struct file *file)
33924+{
33925+ unsigned int flags;
33926+
33927+ spin_lock(&file->f_lock);
33928+ flags = file->f_flags;
33929+ spin_unlock(&file->f_lock);
33930+
33931+ return flags;
33932+}
1308ab2a 33933+
f0c0a007
AM
33934+static inline int vfsub_file_execed(struct file *file)
33935+{
33936+ /* todo: direct access f_flags */
33937+ return !!(vfsub_file_flags(file) & __FMODE_EXEC);
33938+}
33939+
7e9cd9fe 33940+#if 0 /* reserved */
1facf9fc 33941+static inline void vfsub_file_accessed(struct file *h_file)
33942+{
33943+ file_accessed(h_file);
33944+ vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
33945+}
7e9cd9fe 33946+#endif
1facf9fc 33947+
79b8bda9 33948+#if 0 /* reserved */
1facf9fc 33949+static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
33950+ struct dentry *h_dentry)
33951+{
33952+ struct path h_path = {
33953+ .dentry = h_dentry,
33954+ .mnt = h_mnt
33955+ };
92d182d2 33956+ touch_atime(&h_path);
1facf9fc 33957+ vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
33958+}
79b8bda9 33959+#endif
1facf9fc 33960+
cd7a4cd9
AM
33961+static inline int vfsub_update_time(struct inode *h_inode,
33962+ struct timespec64 *ts, int flags)
0c3ec466 33963+{
5afbbe0d 33964+ return update_time(h_inode, ts, flags);
0c3ec466
AM
33965+ /* no vfsub_update_h_iattr() since we don't have struct path */
33966+}
33967+
8cdd5066
JR
33968+#ifdef CONFIG_FS_POSIX_ACL
33969+static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
33970+{
33971+ int err;
33972+
33973+ err = posix_acl_chmod(h_inode, h_mode);
33974+ if (err == -EOPNOTSUPP)
33975+ err = 0;
33976+ return err;
33977+}
33978+#else
33979+AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
33980+#endif
33981+
4a4d8108
AM
33982+long vfsub_splice_to(struct file *in, loff_t *ppos,
33983+ struct pipe_inode_info *pipe, size_t len,
33984+ unsigned int flags);
33985+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33986+ loff_t *ppos, size_t len, unsigned int flags);
c06a8ce3
AM
33987+
33988+static inline long vfsub_truncate(struct path *path, loff_t length)
33989+{
33990+ long err;
076b876e 33991+
c06a8ce3
AM
33992+ lockdep_off();
33993+ err = vfs_truncate(path, length);
33994+ lockdep_on();
33995+ return err;
33996+}
33997+
4a4d8108
AM
33998+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33999+ struct file *h_file);
53392da6 34000+int vfsub_fsync(struct file *file, struct path *path, int datasync);
4a4d8108 34001+
521ced18
JR
34002+/*
34003+ * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
34004+ * ioctl.
34005+ */
9f237c51
AM
34006+static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
34007+ loff_t len)
521ced18 34008+{
9f237c51 34009+ loff_t err;
521ced18
JR
34010+
34011+ lockdep_off();
9f237c51 34012+ err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
521ced18
JR
34013+ lockdep_on();
34014+
34015+ return err;
34016+}
34017+
34018+/* copy_file_range(2) is a systemcall */
34019+static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
34020+ struct file *dst, loff_t dst_pos,
34021+ size_t len, unsigned int flags)
34022+{
34023+ ssize_t ssz;
34024+
34025+ lockdep_off();
34026+ ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
34027+ lockdep_on();
34028+
34029+ return ssz;
34030+}
34031+
1facf9fc 34032+/* ---------------------------------------------------------------------- */
34033+
34034+static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
34035+{
34036+ loff_t err;
34037+
2cbb1c4b 34038+ lockdep_off();
1facf9fc 34039+ err = vfs_llseek(file, offset, origin);
2cbb1c4b 34040+ lockdep_on();
1facf9fc 34041+ return err;
34042+}
34043+
34044+/* ---------------------------------------------------------------------- */
34045+
4a4d8108
AM
34046+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
34047+int vfsub_sio_rmdir(struct inode *dir, struct path *path);
523b37e3
AM
34048+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
34049+ struct inode **delegated_inode);
34050+int vfsub_notify_change(struct path *path, struct iattr *ia,
34051+ struct inode **delegated_inode);
34052+int vfsub_unlink(struct inode *dir, struct path *path,
34053+ struct inode **delegated_inode, int force);
4a4d8108 34054+
521ced18
JR
34055+static inline int vfsub_getattr(const struct path *path, struct kstat *st)
34056+{
34057+ return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
34058+}
34059+
c1595e42
JR
34060+/* ---------------------------------------------------------------------- */
34061+
34062+static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
34063+ const void *value, size_t size, int flags)
34064+{
34065+ int err;
34066+
34067+ lockdep_off();
34068+ err = vfs_setxattr(dentry, name, value, size, flags);
34069+ lockdep_on();
34070+
34071+ return err;
34072+}
34073+
34074+static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
34075+{
34076+ int err;
34077+
34078+ lockdep_off();
34079+ err = vfs_removexattr(dentry, name);
34080+ lockdep_on();
34081+
34082+ return err;
34083+}
34084+
1facf9fc 34085+#endif /* __KERNEL__ */
34086+#endif /* __AUFS_VFSUB_H__ */
7f207e10
AM
34087diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
34088--- /usr/share/empty/fs/aufs/wbr_policy.c 1970-01-01 01:00:00.000000000 +0100
9f237c51 34089+++ linux/fs/aufs/wbr_policy.c 2018-12-27 13:19:17.711749485 +0100
cd7a4cd9
AM
34090@@ -0,0 +1,830 @@
34091+// SPDX-License-Identifier: GPL-2.0
1facf9fc 34092+/*
b00004a5 34093+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 34094+ *
34095+ * This program, aufs is free software; you can redistribute it and/or modify
34096+ * it under the terms of the GNU General Public License as published by
34097+ * the Free Software Foundation; either version 2 of the License, or
34098+ * (at your option) any later version.
dece6358
AM
34099+ *
34100+ * This program is distributed in the hope that it will be useful,
34101+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
34102+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34103+ * GNU General Public License for more details.
34104+ *
34105+ * You should have received a copy of the GNU General Public License
523b37e3 34106+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 34107+ */
34108+
34109+/*
34110+ * policies for selecting one among multiple writable branches
34111+ */
34112+
34113+#include <linux/statfs.h>
34114+#include "aufs.h"
34115+
34116+/* subset of cpup_attr() */
34117+static noinline_for_stack
34118+int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
34119+{
34120+ int err, sbits;
34121+ struct iattr ia;
34122+ struct inode *h_isrc;
34123+
5527c038 34124+ h_isrc = d_inode(h_src);
1facf9fc 34125+ ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
34126+ ia.ia_mode = h_isrc->i_mode;
34127+ ia.ia_uid = h_isrc->i_uid;
34128+ ia.ia_gid = h_isrc->i_gid;
34129+ sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
5527c038 34130+ au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
523b37e3
AM
34131+ /* no delegation since it is just created */
34132+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 34133+
34134+ /* is this nfs only? */
34135+ if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
34136+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
34137+ ia.ia_mode = h_isrc->i_mode;
523b37e3 34138+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 34139+ }
34140+
34141+ return err;
34142+}
34143+
34144+#define AuCpdown_PARENT_OPQ 1
34145+#define AuCpdown_WHED (1 << 1)
34146+#define AuCpdown_MADE_DIR (1 << 2)
34147+#define AuCpdown_DIROPQ (1 << 3)
34148+#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
7f207e10
AM
34149+#define au_fset_cpdown(flags, name) \
34150+ do { (flags) |= AuCpdown_##name; } while (0)
34151+#define au_fclr_cpdown(flags, name) \
34152+ do { (flags) &= ~AuCpdown_##name; } while (0)
1facf9fc 34153+
1facf9fc 34154+static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
c2b27bf2 34155+ unsigned int *flags)
1facf9fc 34156+{
34157+ int err;
34158+ struct dentry *opq_dentry;
34159+
34160+ opq_dentry = au_diropq_create(dentry, bdst);
34161+ err = PTR_ERR(opq_dentry);
34162+ if (IS_ERR(opq_dentry))
34163+ goto out;
34164+ dput(opq_dentry);
c2b27bf2 34165+ au_fset_cpdown(*flags, DIROPQ);
1facf9fc 34166+
4f0767ce 34167+out:
1facf9fc 34168+ return err;
34169+}
34170+
34171+static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34172+ struct inode *dir, aufs_bindex_t bdst)
34173+{
34174+ int err;
34175+ struct path h_path;
34176+ struct au_branch *br;
34177+
34178+ br = au_sbr(dentry->d_sb, bdst);
34179+ h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34180+ err = PTR_ERR(h_path.dentry);
34181+ if (IS_ERR(h_path.dentry))
34182+ goto out;
34183+
34184+ err = 0;
5527c038 34185+ if (d_is_positive(h_path.dentry)) {
86dc4139 34186+ h_path.mnt = au_br_mnt(br);
1facf9fc 34187+ err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34188+ dentry);
34189+ }
34190+ dput(h_path.dentry);
34191+
4f0767ce 34192+out:
1facf9fc 34193+ return err;
34194+}
34195+
34196+static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 34197+ struct au_pin *pin,
1facf9fc 34198+ struct dentry *h_parent, void *arg)
34199+{
34200+ int err, rerr;
5afbbe0d 34201+ aufs_bindex_t bopq, btop;
1facf9fc 34202+ struct path h_path;
34203+ struct dentry *parent;
34204+ struct inode *h_dir, *h_inode, *inode, *dir;
c2b27bf2 34205+ unsigned int *flags = arg;
1facf9fc 34206+
5afbbe0d 34207+ btop = au_dbtop(dentry);
1facf9fc 34208+ /* dentry is di-locked */
34209+ parent = dget_parent(dentry);
5527c038
JR
34210+ dir = d_inode(parent);
34211+ h_dir = d_inode(h_parent);
1facf9fc 34212+ AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34213+ IMustLock(h_dir);
34214+
86dc4139 34215+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
1facf9fc 34216+ if (unlikely(err < 0))
34217+ goto out;
34218+ h_path.dentry = au_h_dptr(dentry, bdst);
34219+ h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
cd7a4cd9 34220+ err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
1facf9fc 34221+ if (unlikely(err))
34222+ goto out_put;
c2b27bf2 34223+ au_fset_cpdown(*flags, MADE_DIR);
1facf9fc 34224+
1facf9fc 34225+ bopq = au_dbdiropq(dentry);
c2b27bf2
AM
34226+ au_fclr_cpdown(*flags, WHED);
34227+ au_fclr_cpdown(*flags, DIROPQ);
1facf9fc 34228+ if (au_dbwh(dentry) == bdst)
c2b27bf2
AM
34229+ au_fset_cpdown(*flags, WHED);
34230+ if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34231+ au_fset_cpdown(*flags, PARENT_OPQ);
5527c038 34232+ h_inode = d_inode(h_path.dentry);
febd17d6 34233+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
c2b27bf2
AM
34234+ if (au_ftest_cpdown(*flags, WHED)) {
34235+ err = au_cpdown_dir_opq(dentry, bdst, flags);
1facf9fc 34236+ if (unlikely(err)) {
febd17d6 34237+ inode_unlock(h_inode);
1facf9fc 34238+ goto out_dir;
34239+ }
34240+ }
34241+
5afbbe0d 34242+ err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
febd17d6 34243+ inode_unlock(h_inode);
1facf9fc 34244+ if (unlikely(err))
34245+ goto out_opq;
34246+
c2b27bf2 34247+ if (au_ftest_cpdown(*flags, WHED)) {
1facf9fc 34248+ err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34249+ if (unlikely(err))
34250+ goto out_opq;
34251+ }
34252+
5527c038 34253+ inode = d_inode(dentry);
5afbbe0d
AM
34254+ if (au_ibbot(inode) < bdst)
34255+ au_set_ibbot(inode, bdst);
1facf9fc 34256+ au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34257+ au_hi_flags(inode, /*isdir*/1));
076b876e 34258+ au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
1facf9fc 34259+ goto out; /* success */
34260+
34261+ /* revert */
4f0767ce 34262+out_opq:
c2b27bf2 34263+ if (au_ftest_cpdown(*flags, DIROPQ)) {
febd17d6 34264+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 34265+ rerr = au_diropq_remove(dentry, bdst);
febd17d6 34266+ inode_unlock(h_inode);
1facf9fc 34267+ if (unlikely(rerr)) {
523b37e3
AM
34268+ AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34269+ dentry, bdst, rerr);
1facf9fc 34270+ err = -EIO;
34271+ goto out;
34272+ }
34273+ }
4f0767ce 34274+out_dir:
c2b27bf2 34275+ if (au_ftest_cpdown(*flags, MADE_DIR)) {
1facf9fc 34276+ rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34277+ if (unlikely(rerr)) {
523b37e3
AM
34278+ AuIOErr("failed removing %pd b%d (%d)\n",
34279+ dentry, bdst, rerr);
1facf9fc 34280+ err = -EIO;
34281+ }
34282+ }
4f0767ce 34283+out_put:
1facf9fc 34284+ au_set_h_dptr(dentry, bdst, NULL);
5afbbe0d
AM
34285+ if (au_dbbot(dentry) == bdst)
34286+ au_update_dbbot(dentry);
4f0767ce 34287+out:
1facf9fc 34288+ dput(parent);
34289+ return err;
34290+}
34291+
34292+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34293+{
34294+ int err;
c2b27bf2 34295+ unsigned int flags;
1facf9fc 34296+
c2b27bf2
AM
34297+ flags = 0;
34298+ err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
1facf9fc 34299+
34300+ return err;
34301+}
34302+
34303+/* ---------------------------------------------------------------------- */
34304+
34305+/* policies for create */
34306+
c2b27bf2 34307+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
4a4d8108
AM
34308+{
34309+ int err, i, j, ndentry;
34310+ aufs_bindex_t bopq;
34311+ struct au_dcsub_pages dpages;
34312+ struct au_dpage *dpage;
34313+ struct dentry **dentries, *parent, *d;
34314+
34315+ err = au_dpages_init(&dpages, GFP_NOFS);
34316+ if (unlikely(err))
34317+ goto out;
34318+ parent = dget_parent(dentry);
027c5e7a 34319+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
4a4d8108
AM
34320+ if (unlikely(err))
34321+ goto out_free;
34322+
34323+ err = bindex;
34324+ for (i = 0; i < dpages.ndpage; i++) {
34325+ dpage = dpages.dpages + i;
34326+ dentries = dpage->dentries;
34327+ ndentry = dpage->ndentry;
34328+ for (j = 0; j < ndentry; j++) {
34329+ d = dentries[j];
34330+ di_read_lock_parent2(d, !AuLock_IR);
34331+ bopq = au_dbdiropq(d);
34332+ di_read_unlock(d, !AuLock_IR);
34333+ if (bopq >= 0 && bopq < err)
34334+ err = bopq;
34335+ }
34336+ }
34337+
34338+out_free:
34339+ dput(parent);
34340+ au_dpages_free(&dpages);
34341+out:
34342+ return err;
34343+}
34344+
1facf9fc 34345+static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34346+{
34347+ for (; bindex >= 0; bindex--)
34348+ if (!au_br_rdonly(au_sbr(sb, bindex)))
34349+ return bindex;
34350+ return -EROFS;
34351+}
34352+
34353+/* top down parent */
392086de
AM
34354+static int au_wbr_create_tdp(struct dentry *dentry,
34355+ unsigned int flags __maybe_unused)
1facf9fc 34356+{
34357+ int err;
5afbbe0d 34358+ aufs_bindex_t btop, bindex;
1facf9fc 34359+ struct super_block *sb;
34360+ struct dentry *parent, *h_parent;
34361+
34362+ sb = dentry->d_sb;
5afbbe0d
AM
34363+ btop = au_dbtop(dentry);
34364+ err = btop;
34365+ if (!au_br_rdonly(au_sbr(sb, btop)))
1facf9fc 34366+ goto out;
34367+
34368+ err = -EROFS;
34369+ parent = dget_parent(dentry);
5afbbe0d 34370+ for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
1facf9fc 34371+ h_parent = au_h_dptr(parent, bindex);
5527c038 34372+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34373+ continue;
34374+
34375+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34376+ err = bindex;
34377+ break;
34378+ }
34379+ }
34380+ dput(parent);
34381+
34382+ /* bottom up here */
4a4d8108 34383+ if (unlikely(err < 0)) {
5afbbe0d 34384+ err = au_wbr_bu(sb, btop - 1);
4a4d8108
AM
34385+ if (err >= 0)
34386+ err = au_wbr_nonopq(dentry, err);
34387+ }
1facf9fc 34388+
4f0767ce 34389+out:
1facf9fc 34390+ AuDbg("b%d\n", err);
34391+ return err;
34392+}
34393+
34394+/* ---------------------------------------------------------------------- */
34395+
34396+/* an exception for the policy other than tdp */
34397+static int au_wbr_create_exp(struct dentry *dentry)
34398+{
34399+ int err;
34400+ aufs_bindex_t bwh, bdiropq;
34401+ struct dentry *parent;
34402+
34403+ err = -1;
34404+ bwh = au_dbwh(dentry);
34405+ parent = dget_parent(dentry);
34406+ bdiropq = au_dbdiropq(parent);
34407+ if (bwh >= 0) {
34408+ if (bdiropq >= 0)
34409+ err = min(bdiropq, bwh);
34410+ else
34411+ err = bwh;
34412+ AuDbg("%d\n", err);
34413+ } else if (bdiropq >= 0) {
34414+ err = bdiropq;
34415+ AuDbg("%d\n", err);
34416+ }
34417+ dput(parent);
34418+
4a4d8108
AM
34419+ if (err >= 0)
34420+ err = au_wbr_nonopq(dentry, err);
34421+
1facf9fc 34422+ if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34423+ err = -1;
34424+
34425+ AuDbg("%d\n", err);
34426+ return err;
34427+}
34428+
34429+/* ---------------------------------------------------------------------- */
34430+
34431+/* round robin */
34432+static int au_wbr_create_init_rr(struct super_block *sb)
34433+{
34434+ int err;
34435+
5afbbe0d 34436+ err = au_wbr_bu(sb, au_sbbot(sb));
1facf9fc 34437+ atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
dece6358 34438+ /* smp_mb(); */
1facf9fc 34439+
34440+ AuDbg("b%d\n", err);
34441+ return err;
34442+}
34443+
392086de 34444+static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
1facf9fc 34445+{
34446+ int err, nbr;
34447+ unsigned int u;
5afbbe0d 34448+ aufs_bindex_t bindex, bbot;
1facf9fc 34449+ struct super_block *sb;
34450+ atomic_t *next;
34451+
34452+ err = au_wbr_create_exp(dentry);
34453+ if (err >= 0)
34454+ goto out;
34455+
34456+ sb = dentry->d_sb;
34457+ next = &au_sbi(sb)->si_wbr_rr_next;
5afbbe0d
AM
34458+ bbot = au_sbbot(sb);
34459+ nbr = bbot + 1;
34460+ for (bindex = 0; bindex <= bbot; bindex++) {
392086de 34461+ if (!au_ftest_wbr(flags, DIR)) {
1facf9fc 34462+ err = atomic_dec_return(next) + 1;
34463+ /* modulo for 0 is meaningless */
34464+ if (unlikely(!err))
34465+ err = atomic_dec_return(next) + 1;
34466+ } else
34467+ err = atomic_read(next);
34468+ AuDbg("%d\n", err);
34469+ u = err;
34470+ err = u % nbr;
34471+ AuDbg("%d\n", err);
34472+ if (!au_br_rdonly(au_sbr(sb, err)))
34473+ break;
34474+ err = -EROFS;
34475+ }
34476+
4a4d8108
AM
34477+ if (err >= 0)
34478+ err = au_wbr_nonopq(dentry, err);
34479+
4f0767ce 34480+out:
1facf9fc 34481+ AuDbg("%d\n", err);
34482+ return err;
34483+}
34484+
34485+/* ---------------------------------------------------------------------- */
34486+
34487+/* most free space */
392086de 34488+static void au_mfs(struct dentry *dentry, struct dentry *parent)
1facf9fc 34489+{
34490+ struct super_block *sb;
34491+ struct au_branch *br;
34492+ struct au_wbr_mfs *mfs;
392086de 34493+ struct dentry *h_parent;
5afbbe0d 34494+ aufs_bindex_t bindex, bbot;
1facf9fc 34495+ int err;
34496+ unsigned long long b, bavail;
7f207e10 34497+ struct path h_path;
1facf9fc 34498+ /* reduce the stack usage */
34499+ struct kstatfs *st;
34500+
34501+ st = kmalloc(sizeof(*st), GFP_NOFS);
34502+ if (unlikely(!st)) {
34503+ AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34504+ return;
34505+ }
34506+
34507+ bavail = 0;
34508+ sb = dentry->d_sb;
34509+ mfs = &au_sbi(sb)->si_wbr_mfs;
dece6358 34510+ MtxMustLock(&mfs->mfs_lock);
1facf9fc 34511+ mfs->mfs_bindex = -EROFS;
34512+ mfs->mfsrr_bytes = 0;
392086de
AM
34513+ if (!parent) {
34514+ bindex = 0;
5afbbe0d 34515+ bbot = au_sbbot(sb);
392086de 34516+ } else {
5afbbe0d
AM
34517+ bindex = au_dbtop(parent);
34518+ bbot = au_dbtaildir(parent);
392086de
AM
34519+ }
34520+
5afbbe0d 34521+ for (; bindex <= bbot; bindex++) {
392086de
AM
34522+ if (parent) {
34523+ h_parent = au_h_dptr(parent, bindex);
5527c038 34524+ if (!h_parent || d_is_negative(h_parent))
392086de
AM
34525+ continue;
34526+ }
1facf9fc 34527+ br = au_sbr(sb, bindex);
34528+ if (au_br_rdonly(br))
34529+ continue;
34530+
34531+ /* sb->s_root for NFS is unreliable */
86dc4139 34532+ h_path.mnt = au_br_mnt(br);
7f207e10
AM
34533+ h_path.dentry = h_path.mnt->mnt_root;
34534+ err = vfs_statfs(&h_path, st);
1facf9fc 34535+ if (unlikely(err)) {
34536+ AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34537+ continue;
34538+ }
34539+
34540+ /* when the available size is equal, select the lower one */
34541+ BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34542+ || sizeof(b) < sizeof(st->f_bsize));
34543+ b = st->f_bavail * st->f_bsize;
34544+ br->br_wbr->wbr_bytes = b;
34545+ if (b >= bavail) {
34546+ bavail = b;
34547+ mfs->mfs_bindex = bindex;
34548+ mfs->mfs_jiffy = jiffies;
34549+ }
34550+ }
34551+
34552+ mfs->mfsrr_bytes = bavail;
34553+ AuDbg("b%d\n", mfs->mfs_bindex);
9f237c51 34554+ au_kfree_rcu(st);
1facf9fc 34555+}
34556+
392086de 34557+static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
1facf9fc 34558+{
34559+ int err;
392086de 34560+ struct dentry *parent;
1facf9fc 34561+ struct super_block *sb;
34562+ struct au_wbr_mfs *mfs;
34563+
34564+ err = au_wbr_create_exp(dentry);
34565+ if (err >= 0)
34566+ goto out;
34567+
34568+ sb = dentry->d_sb;
392086de
AM
34569+ parent = NULL;
34570+ if (au_ftest_wbr(flags, PARENT))
34571+ parent = dget_parent(dentry);
1facf9fc 34572+ mfs = &au_sbi(sb)->si_wbr_mfs;
34573+ mutex_lock(&mfs->mfs_lock);
34574+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34575+ || mfs->mfs_bindex < 0
34576+ || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
392086de 34577+ au_mfs(dentry, parent);
1facf9fc 34578+ mutex_unlock(&mfs->mfs_lock);
34579+ err = mfs->mfs_bindex;
392086de 34580+ dput(parent);
1facf9fc 34581+
4a4d8108
AM
34582+ if (err >= 0)
34583+ err = au_wbr_nonopq(dentry, err);
34584+
4f0767ce 34585+out:
1facf9fc 34586+ AuDbg("b%d\n", err);
34587+ return err;
34588+}
34589+
34590+static int au_wbr_create_init_mfs(struct super_block *sb)
34591+{
34592+ struct au_wbr_mfs *mfs;
34593+
34594+ mfs = &au_sbi(sb)->si_wbr_mfs;
34595+ mutex_init(&mfs->mfs_lock);
34596+ mfs->mfs_jiffy = 0;
34597+ mfs->mfs_bindex = -EROFS;
34598+
34599+ return 0;
34600+}
34601+
34602+static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34603+{
34604+ mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34605+ return 0;
34606+}
34607+
34608+/* ---------------------------------------------------------------------- */
34609+
f2c43d5f
AM
34610+/* top down regardless parent, and then mfs */
34611+static int au_wbr_create_tdmfs(struct dentry *dentry,
34612+ unsigned int flags __maybe_unused)
34613+{
34614+ int err;
34615+ aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34616+ unsigned long long watermark;
34617+ struct super_block *sb;
34618+ struct au_wbr_mfs *mfs;
34619+ struct au_branch *br;
34620+ struct dentry *parent;
34621+
34622+ sb = dentry->d_sb;
34623+ mfs = &au_sbi(sb)->si_wbr_mfs;
34624+ mutex_lock(&mfs->mfs_lock);
34625+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34626+ || mfs->mfs_bindex < 0)
34627+ au_mfs(dentry, /*parent*/NULL);
34628+ watermark = mfs->mfsrr_watermark;
34629+ bmfs = mfs->mfs_bindex;
34630+ mutex_unlock(&mfs->mfs_lock);
34631+
34632+ /* another style of au_wbr_create_exp() */
34633+ bwh = au_dbwh(dentry);
34634+ parent = dget_parent(dentry);
34635+ btail = au_dbtaildir(parent);
34636+ if (bwh >= 0 && bwh < btail)
34637+ btail = bwh;
34638+
34639+ err = au_wbr_nonopq(dentry, btail);
34640+ if (unlikely(err < 0))
34641+ goto out;
34642+ btail = err;
34643+ bfound = -1;
34644+ for (bindex = 0; bindex <= btail; bindex++) {
34645+ br = au_sbr(sb, bindex);
34646+ if (au_br_rdonly(br))
34647+ continue;
34648+ if (br->br_wbr->wbr_bytes > watermark) {
34649+ bfound = bindex;
34650+ break;
34651+ }
34652+ }
34653+ err = bfound;
34654+ if (err < 0)
34655+ err = bmfs;
34656+
34657+out:
34658+ dput(parent);
34659+ AuDbg("b%d\n", err);
34660+ return err;
34661+}
34662+
34663+/* ---------------------------------------------------------------------- */
34664+
1facf9fc 34665+/* most free space and then round robin */
392086de 34666+static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
1facf9fc 34667+{
34668+ int err;
34669+ struct au_wbr_mfs *mfs;
34670+
392086de 34671+ err = au_wbr_create_mfs(dentry, flags);
1facf9fc 34672+ if (err >= 0) {
34673+ mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
dece6358 34674+ mutex_lock(&mfs->mfs_lock);
1facf9fc 34675+ if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
392086de 34676+ err = au_wbr_create_rr(dentry, flags);
dece6358 34677+ mutex_unlock(&mfs->mfs_lock);
1facf9fc 34678+ }
34679+
34680+ AuDbg("b%d\n", err);
34681+ return err;
34682+}
34683+
34684+static int au_wbr_create_init_mfsrr(struct super_block *sb)
34685+{
34686+ int err;
34687+
34688+ au_wbr_create_init_mfs(sb); /* ignore */
34689+ err = au_wbr_create_init_rr(sb);
34690+
34691+ return err;
34692+}
34693+
34694+/* ---------------------------------------------------------------------- */
34695+
34696+/* top down parent and most free space */
392086de 34697+static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
1facf9fc 34698+{
34699+ int err, e2;
34700+ unsigned long long b;
5afbbe0d 34701+ aufs_bindex_t bindex, btop, bbot;
1facf9fc 34702+ struct super_block *sb;
34703+ struct dentry *parent, *h_parent;
34704+ struct au_branch *br;
34705+
392086de 34706+ err = au_wbr_create_tdp(dentry, flags);
1facf9fc 34707+ if (unlikely(err < 0))
34708+ goto out;
34709+ parent = dget_parent(dentry);
5afbbe0d
AM
34710+ btop = au_dbtop(parent);
34711+ bbot = au_dbtaildir(parent);
34712+ if (btop == bbot)
1facf9fc 34713+ goto out_parent; /* success */
34714+
392086de 34715+ e2 = au_wbr_create_mfs(dentry, flags);
1facf9fc 34716+ if (e2 < 0)
34717+ goto out_parent; /* success */
34718+
34719+ /* when the available size is equal, select upper one */
34720+ sb = dentry->d_sb;
34721+ br = au_sbr(sb, err);
34722+ b = br->br_wbr->wbr_bytes;
34723+ AuDbg("b%d, %llu\n", err, b);
34724+
5afbbe0d 34725+ for (bindex = btop; bindex <= bbot; bindex++) {
1facf9fc 34726+ h_parent = au_h_dptr(parent, bindex);
5527c038 34727+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34728+ continue;
34729+
34730+ br = au_sbr(sb, bindex);
34731+ if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34732+ b = br->br_wbr->wbr_bytes;
34733+ err = bindex;
34734+ AuDbg("b%d, %llu\n", err, b);
34735+ }
34736+ }
34737+
4a4d8108
AM
34738+ if (err >= 0)
34739+ err = au_wbr_nonopq(dentry, err);
34740+
4f0767ce 34741+out_parent:
1facf9fc 34742+ dput(parent);
4f0767ce 34743+out:
1facf9fc 34744+ AuDbg("b%d\n", err);
34745+ return err;
34746+}
34747+
34748+/* ---------------------------------------------------------------------- */
34749+
392086de
AM
34750+/*
34751+ * - top down parent
34752+ * - most free space with parent
34753+ * - most free space round-robin regardless parent
34754+ */
34755+static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34756+{
34757+ int err;
34758+ unsigned long long watermark;
34759+ struct super_block *sb;
34760+ struct au_branch *br;
34761+ struct au_wbr_mfs *mfs;
34762+
34763+ err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34764+ if (unlikely(err < 0))
34765+ goto out;
34766+
34767+ sb = dentry->d_sb;
34768+ br = au_sbr(sb, err);
34769+ mfs = &au_sbi(sb)->si_wbr_mfs;
34770+ mutex_lock(&mfs->mfs_lock);
34771+ watermark = mfs->mfsrr_watermark;
34772+ mutex_unlock(&mfs->mfs_lock);
34773+ if (br->br_wbr->wbr_bytes < watermark)
34774+ /* regardless the parent dir */
34775+ err = au_wbr_create_mfsrr(dentry, flags);
34776+
34777+out:
34778+ AuDbg("b%d\n", err);
34779+ return err;
34780+}
34781+
34782+/* ---------------------------------------------------------------------- */
34783+
1facf9fc 34784+/* policies for copyup */
34785+
34786+/* top down parent */
34787+static int au_wbr_copyup_tdp(struct dentry *dentry)
34788+{
392086de 34789+ return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
1facf9fc 34790+}
34791+
34792+/* bottom up parent */
34793+static int au_wbr_copyup_bup(struct dentry *dentry)
34794+{
34795+ int err;
5afbbe0d 34796+ aufs_bindex_t bindex, btop;
1facf9fc 34797+ struct dentry *parent, *h_parent;
34798+ struct super_block *sb;
34799+
34800+ err = -EROFS;
34801+ sb = dentry->d_sb;
34802+ parent = dget_parent(dentry);
5afbbe0d
AM
34803+ btop = au_dbtop(parent);
34804+ for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
1facf9fc 34805+ h_parent = au_h_dptr(parent, bindex);
5527c038 34806+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34807+ continue;
34808+
34809+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34810+ err = bindex;
34811+ break;
34812+ }
34813+ }
34814+ dput(parent);
34815+
34816+ /* bottom up here */
34817+ if (unlikely(err < 0))
5afbbe0d 34818+ err = au_wbr_bu(sb, btop - 1);
1facf9fc 34819+
34820+ AuDbg("b%d\n", err);
34821+ return err;
34822+}
34823+
34824+/* bottom up */
5afbbe0d 34825+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
1facf9fc 34826+{
34827+ int err;
34828+
5afbbe0d 34829+ err = au_wbr_bu(dentry->d_sb, btop);
4a4d8108 34830+ AuDbg("b%d\n", err);
5afbbe0d 34831+ if (err > btop)
4a4d8108 34832+ err = au_wbr_nonopq(dentry, err);
1facf9fc 34833+
34834+ AuDbg("b%d\n", err);
34835+ return err;
34836+}
34837+
076b876e
AM
34838+static int au_wbr_copyup_bu(struct dentry *dentry)
34839+{
34840+ int err;
5afbbe0d 34841+ aufs_bindex_t btop;
076b876e 34842+
5afbbe0d
AM
34843+ btop = au_dbtop(dentry);
34844+ err = au_wbr_do_copyup_bu(dentry, btop);
076b876e
AM
34845+ return err;
34846+}
34847+
1facf9fc 34848+/* ---------------------------------------------------------------------- */
34849+
34850+struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34851+ [AuWbrCopyup_TDP] = {
34852+ .copyup = au_wbr_copyup_tdp
34853+ },
34854+ [AuWbrCopyup_BUP] = {
34855+ .copyup = au_wbr_copyup_bup
34856+ },
34857+ [AuWbrCopyup_BU] = {
34858+ .copyup = au_wbr_copyup_bu
34859+ }
34860+};
34861+
34862+struct au_wbr_create_operations au_wbr_create_ops[] = {
34863+ [AuWbrCreate_TDP] = {
34864+ .create = au_wbr_create_tdp
34865+ },
34866+ [AuWbrCreate_RR] = {
34867+ .create = au_wbr_create_rr,
34868+ .init = au_wbr_create_init_rr
34869+ },
34870+ [AuWbrCreate_MFS] = {
34871+ .create = au_wbr_create_mfs,
34872+ .init = au_wbr_create_init_mfs,
34873+ .fin = au_wbr_create_fin_mfs
34874+ },
34875+ [AuWbrCreate_MFSV] = {
34876+ .create = au_wbr_create_mfs,
34877+ .init = au_wbr_create_init_mfs,
34878+ .fin = au_wbr_create_fin_mfs
34879+ },
34880+ [AuWbrCreate_MFSRR] = {
34881+ .create = au_wbr_create_mfsrr,
34882+ .init = au_wbr_create_init_mfsrr,
34883+ .fin = au_wbr_create_fin_mfs
34884+ },
34885+ [AuWbrCreate_MFSRRV] = {
34886+ .create = au_wbr_create_mfsrr,
34887+ .init = au_wbr_create_init_mfsrr,
34888+ .fin = au_wbr_create_fin_mfs
34889+ },
f2c43d5f
AM
34890+ [AuWbrCreate_TDMFS] = {
34891+ .create = au_wbr_create_tdmfs,
34892+ .init = au_wbr_create_init_mfs,
34893+ .fin = au_wbr_create_fin_mfs
34894+ },
34895+ [AuWbrCreate_TDMFSV] = {
34896+ .create = au_wbr_create_tdmfs,
34897+ .init = au_wbr_create_init_mfs,
34898+ .fin = au_wbr_create_fin_mfs
34899+ },
1facf9fc 34900+ [AuWbrCreate_PMFS] = {
34901+ .create = au_wbr_create_pmfs,
34902+ .init = au_wbr_create_init_mfs,
34903+ .fin = au_wbr_create_fin_mfs
34904+ },
34905+ [AuWbrCreate_PMFSV] = {
34906+ .create = au_wbr_create_pmfs,
34907+ .init = au_wbr_create_init_mfs,
34908+ .fin = au_wbr_create_fin_mfs
392086de
AM
34909+ },
34910+ [AuWbrCreate_PMFSRR] = {
34911+ .create = au_wbr_create_pmfsrr,
34912+ .init = au_wbr_create_init_mfsrr,
34913+ .fin = au_wbr_create_fin_mfs
34914+ },
34915+ [AuWbrCreate_PMFSRRV] = {
34916+ .create = au_wbr_create_pmfsrr,
34917+ .init = au_wbr_create_init_mfsrr,
34918+ .fin = au_wbr_create_fin_mfs
1facf9fc 34919+ }
34920+};
7f207e10
AM
34921diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
34922--- /usr/share/empty/fs/aufs/whout.c 1970-01-01 01:00:00.000000000 +0100
9f237c51 34923+++ linux/fs/aufs/whout.c 2018-12-27 13:19:17.711749485 +0100
062440b3 34924@@ -0,0 +1,1062 @@
cd7a4cd9 34925+// SPDX-License-Identifier: GPL-2.0
1facf9fc 34926+/*
b00004a5 34927+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 34928+ *
34929+ * This program, aufs is free software; you can redistribute it and/or modify
34930+ * it under the terms of the GNU General Public License as published by
34931+ * the Free Software Foundation; either version 2 of the License, or
34932+ * (at your option) any later version.
dece6358
AM
34933+ *
34934+ * This program is distributed in the hope that it will be useful,
34935+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
34936+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34937+ * GNU General Public License for more details.
34938+ *
34939+ * You should have received a copy of the GNU General Public License
523b37e3 34940+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 34941+ */
34942+
34943+/*
34944+ * whiteout for logical deletion and opaque directory
34945+ */
34946+
1facf9fc 34947+#include "aufs.h"
34948+
cd7a4cd9 34949+#define WH_MASK 0444
1facf9fc 34950+
34951+/*
34952+ * If a directory contains this file, then it is opaque. We start with the
34953+ * .wh. flag so that it is blocked by lookup.
34954+ */
0c3ec466
AM
34955+static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
34956+ sizeof(AUFS_WH_DIROPQ) - 1);
1facf9fc 34957+
34958+/*
34959+ * generate whiteout name, which is NOT terminated by NULL.
34960+ * @name: original d_name.name
34961+ * @len: original d_name.len
34962+ * @wh: whiteout qstr
34963+ * returns zero when succeeds, otherwise error.
34964+ * succeeded value as wh->name should be freed by kfree().
34965+ */
34966+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
34967+{
34968+ char *p;
34969+
34970+ if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
34971+ return -ENAMETOOLONG;
34972+
34973+ wh->len = name->len + AUFS_WH_PFX_LEN;
34974+ p = kmalloc(wh->len, GFP_NOFS);
34975+ wh->name = p;
34976+ if (p) {
34977+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
34978+ memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
34979+ /* smp_mb(); */
34980+ return 0;
34981+ }
34982+ return -ENOMEM;
34983+}
34984+
34985+/* ---------------------------------------------------------------------- */
34986+
34987+/*
34988+ * test if the @wh_name exists under @h_parent.
34989+ * @try_sio specifies the necessary of super-io.
34990+ */
076b876e 34991+int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
1facf9fc 34992+{
34993+ int err;
34994+ struct dentry *wh_dentry;
1facf9fc 34995+
1facf9fc 34996+ if (!try_sio)
b4510431 34997+ wh_dentry = vfsub_lkup_one(wh_name, h_parent);
1facf9fc 34998+ else
076b876e 34999+ wh_dentry = au_sio_lkup_one(wh_name, h_parent);
1facf9fc 35000+ err = PTR_ERR(wh_dentry);
2000de60
JR
35001+ if (IS_ERR(wh_dentry)) {
35002+ if (err == -ENAMETOOLONG)
35003+ err = 0;
1facf9fc 35004+ goto out;
2000de60 35005+ }
1facf9fc 35006+
35007+ err = 0;
5527c038 35008+ if (d_is_negative(wh_dentry))
1facf9fc 35009+ goto out_wh; /* success */
35010+
35011+ err = 1;
7e9cd9fe 35012+ if (d_is_reg(wh_dentry))
1facf9fc 35013+ goto out_wh; /* success */
35014+
35015+ err = -EIO;
523b37e3 35016+ AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
5527c038 35017+ wh_dentry, d_inode(wh_dentry)->i_mode);
1facf9fc 35018+
4f0767ce 35019+out_wh:
1facf9fc 35020+ dput(wh_dentry);
4f0767ce 35021+out:
1facf9fc 35022+ return err;
35023+}
35024+
35025+/*
35026+ * test if the @h_dentry sets opaque or not.
35027+ */
076b876e 35028+int au_diropq_test(struct dentry *h_dentry)
1facf9fc 35029+{
35030+ int err;
35031+ struct inode *h_dir;
35032+
5527c038 35033+ h_dir = d_inode(h_dentry);
076b876e 35034+ err = au_wh_test(h_dentry, &diropq_name,
1facf9fc 35035+ au_test_h_perm_sio(h_dir, MAY_EXEC));
35036+ return err;
35037+}
35038+
35039+/*
35040+ * returns a negative dentry whose name is unique and temporary.
35041+ */
35042+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35043+ struct qstr *prefix)
35044+{
1facf9fc 35045+ struct dentry *dentry;
35046+ int i;
027c5e7a 35047+ char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
4a4d8108 35048+ *name, *p;
027c5e7a 35049+ /* strict atomic_t is unnecessary here */
1facf9fc 35050+ static unsigned short cnt;
35051+ struct qstr qs;
35052+
4a4d8108
AM
35053+ BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
35054+
1facf9fc 35055+ name = defname;
027c5e7a
AM
35056+ qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
35057+ if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
1facf9fc 35058+ dentry = ERR_PTR(-ENAMETOOLONG);
4a4d8108 35059+ if (unlikely(qs.len > NAME_MAX))
1facf9fc 35060+ goto out;
35061+ dentry = ERR_PTR(-ENOMEM);
35062+ name = kmalloc(qs.len + 1, GFP_NOFS);
35063+ if (unlikely(!name))
35064+ goto out;
35065+ }
35066+
35067+ /* doubly whiteout-ed */
35068+ memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
35069+ p = name + AUFS_WH_PFX_LEN * 2;
35070+ memcpy(p, prefix->name, prefix->len);
35071+ p += prefix->len;
35072+ *p++ = '.';
4a4d8108 35073+ AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
1facf9fc 35074+
35075+ qs.name = name;
35076+ for (i = 0; i < 3; i++) {
b752ccd1 35077+ sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
076b876e 35078+ dentry = au_sio_lkup_one(&qs, h_parent);
5527c038 35079+ if (IS_ERR(dentry) || d_is_negative(dentry))
1facf9fc 35080+ goto out_name;
35081+ dput(dentry);
35082+ }
0c3ec466 35083+ /* pr_warn("could not get random name\n"); */
1facf9fc 35084+ dentry = ERR_PTR(-EEXIST);
35085+ AuDbg("%.*s\n", AuLNPair(&qs));
35086+ BUG();
35087+
4f0767ce 35088+out_name:
1facf9fc 35089+ if (name != defname)
9f237c51 35090+ au_kfree_try_rcu(name);
4f0767ce 35091+out:
4a4d8108 35092+ AuTraceErrPtr(dentry);
1facf9fc 35093+ return dentry;
1facf9fc 35094+}
35095+
35096+/*
35097+ * rename the @h_dentry on @br to the whiteouted temporary name.
35098+ */
35099+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
35100+{
35101+ int err;
35102+ struct path h_path = {
86dc4139 35103+ .mnt = au_br_mnt(br)
1facf9fc 35104+ };
523b37e3 35105+ struct inode *h_dir, *delegated;
1facf9fc 35106+ struct dentry *h_parent;
35107+
35108+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 35109+ h_dir = d_inode(h_parent);
1facf9fc 35110+ IMustLock(h_dir);
35111+
35112+ h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
35113+ err = PTR_ERR(h_path.dentry);
35114+ if (IS_ERR(h_path.dentry))
35115+ goto out;
35116+
35117+ /* under the same dir, no need to lock_rename() */
523b37e3 35118+ delegated = NULL;
f2c43d5f
AM
35119+ err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
35120+ /*flags*/0);
1facf9fc 35121+ AuTraceErr(err);
523b37e3
AM
35122+ if (unlikely(err == -EWOULDBLOCK)) {
35123+ pr_warn("cannot retry for NFSv4 delegation"
35124+ " for an internal rename\n");
35125+ iput(delegated);
35126+ }
1facf9fc 35127+ dput(h_path.dentry);
35128+
4f0767ce 35129+out:
4a4d8108 35130+ AuTraceErr(err);
1facf9fc 35131+ return err;
35132+}
35133+
35134+/* ---------------------------------------------------------------------- */
35135+/*
35136+ * functions for removing a whiteout
35137+ */
35138+
35139+static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
35140+{
523b37e3
AM
35141+ int err, force;
35142+ struct inode *delegated;
1facf9fc 35143+
35144+ /*
35145+ * forces superio when the dir has a sticky bit.
35146+ * this may be a violation of unix fs semantics.
35147+ */
35148+ force = (h_dir->i_mode & S_ISVTX)
5527c038 35149+ && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
523b37e3
AM
35150+ delegated = NULL;
35151+ err = vfsub_unlink(h_dir, h_path, &delegated, force);
35152+ if (unlikely(err == -EWOULDBLOCK)) {
35153+ pr_warn("cannot retry for NFSv4 delegation"
35154+ " for an internal unlink\n");
35155+ iput(delegated);
35156+ }
35157+ return err;
1facf9fc 35158+}
35159+
35160+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35161+ struct dentry *dentry)
35162+{
35163+ int err;
35164+
35165+ err = do_unlink_wh(h_dir, h_path);
35166+ if (!err && dentry)
35167+ au_set_dbwh(dentry, -1);
35168+
35169+ return err;
35170+}
35171+
35172+static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
35173+ struct au_branch *br)
35174+{
35175+ int err;
35176+ struct path h_path = {
86dc4139 35177+ .mnt = au_br_mnt(br)
1facf9fc 35178+ };
35179+
35180+ err = 0;
b4510431 35181+ h_path.dentry = vfsub_lkup_one(wh, h_parent);
1facf9fc 35182+ if (IS_ERR(h_path.dentry))
35183+ err = PTR_ERR(h_path.dentry);
35184+ else {
5527c038
JR
35185+ if (d_is_reg(h_path.dentry))
35186+ err = do_unlink_wh(d_inode(h_parent), &h_path);
1facf9fc 35187+ dput(h_path.dentry);
35188+ }
35189+
35190+ return err;
35191+}
35192+
35193+/* ---------------------------------------------------------------------- */
35194+/*
35195+ * initialize/clean whiteout for a branch
35196+ */
35197+
35198+static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35199+ const int isdir)
35200+{
35201+ int err;
523b37e3 35202+ struct inode *delegated;
1facf9fc 35203+
5527c038 35204+ if (d_is_negative(whpath->dentry))
1facf9fc 35205+ return;
35206+
86dc4139
AM
35207+ if (isdir)
35208+ err = vfsub_rmdir(h_dir, whpath);
523b37e3
AM
35209+ else {
35210+ delegated = NULL;
35211+ err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35212+ if (unlikely(err == -EWOULDBLOCK)) {
35213+ pr_warn("cannot retry for NFSv4 delegation"
35214+ " for an internal unlink\n");
35215+ iput(delegated);
35216+ }
35217+ }
1facf9fc 35218+ if (unlikely(err))
523b37e3
AM
35219+ pr_warn("failed removing %pd (%d), ignored.\n",
35220+ whpath->dentry, err);
1facf9fc 35221+}
35222+
35223+static int test_linkable(struct dentry *h_root)
35224+{
5527c038 35225+ struct inode *h_dir = d_inode(h_root);
1facf9fc 35226+
35227+ if (h_dir->i_op->link)
35228+ return 0;
35229+
523b37e3
AM
35230+ pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35231+ h_root, au_sbtype(h_root->d_sb));
1facf9fc 35232+ return -ENOSYS;
35233+}
35234+
35235+/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35236+static int au_whdir(struct inode *h_dir, struct path *path)
35237+{
35238+ int err;
35239+
35240+ err = -EEXIST;
5527c038 35241+ if (d_is_negative(path->dentry)) {
cd7a4cd9 35242+ int mode = 0700;
1facf9fc 35243+
35244+ if (au_test_nfs(path->dentry->d_sb))
cd7a4cd9 35245+ mode |= 0111;
86dc4139 35246+ err = vfsub_mkdir(h_dir, path, mode);
2000de60 35247+ } else if (d_is_dir(path->dentry))
1facf9fc 35248+ err = 0;
35249+ else
523b37e3 35250+ pr_err("unknown %pd exists\n", path->dentry);
1facf9fc 35251+
35252+ return err;
35253+}
35254+
35255+struct au_wh_base {
35256+ const struct qstr *name;
35257+ struct dentry *dentry;
35258+};
35259+
35260+static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35261+ struct path *h_path)
35262+{
35263+ h_path->dentry = base[AuBrWh_BASE].dentry;
35264+ au_wh_clean(h_dir, h_path, /*isdir*/0);
35265+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35266+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35267+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35268+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35269+}
35270+
35271+/*
35272+ * returns tri-state,
c1595e42 35273+ * minus: error, caller should print the message
acd2b654 35274+ * zero: success
c1595e42 35275+ * plus: error, caller should NOT print the message
1facf9fc 35276+ */
35277+static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35278+ int do_plink, struct au_wh_base base[],
35279+ struct path *h_path)
35280+{
35281+ int err;
35282+ struct inode *h_dir;
35283+
5527c038 35284+ h_dir = d_inode(h_root);
1facf9fc 35285+ h_path->dentry = base[AuBrWh_BASE].dentry;
35286+ au_wh_clean(h_dir, h_path, /*isdir*/0);
35287+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35288+ if (do_plink) {
35289+ err = test_linkable(h_root);
35290+ if (unlikely(err)) {
35291+ err = 1;
35292+ goto out;
35293+ }
35294+
35295+ err = au_whdir(h_dir, h_path);
35296+ if (unlikely(err))
35297+ goto out;
35298+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35299+ } else
35300+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35301+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35302+ err = au_whdir(h_dir, h_path);
35303+ if (unlikely(err))
35304+ goto out;
35305+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35306+
4f0767ce 35307+out:
1facf9fc 35308+ return err;
35309+}
35310+
35311+/*
35312+ * for the moment, aufs supports the branch filesystem which does not support
35313+ * link(2). testing on FAT which does not support i_op->setattr() fully either,
35314+ * copyup failed. finally, such filesystem will not be used as the writable
35315+ * branch.
35316+ *
35317+ * returns tri-state, see above.
35318+ */
35319+static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35320+ int do_plink, struct au_wh_base base[],
35321+ struct path *h_path)
35322+{
35323+ int err;
35324+ struct inode *h_dir;
35325+
1308ab2a 35326+ WbrWhMustWriteLock(wbr);
35327+
1facf9fc 35328+ err = test_linkable(h_root);
35329+ if (unlikely(err)) {
35330+ err = 1;
35331+ goto out;
35332+ }
35333+
35334+ /*
35335+ * todo: should this create be done in /sbin/mount.aufs helper?
35336+ */
35337+ err = -EEXIST;
5527c038
JR
35338+ h_dir = d_inode(h_root);
35339+ if (d_is_negative(base[AuBrWh_BASE].dentry)) {
86dc4139
AM
35340+ h_path->dentry = base[AuBrWh_BASE].dentry;
35341+ err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
7e9cd9fe 35342+ } else if (d_is_reg(base[AuBrWh_BASE].dentry))
1facf9fc 35343+ err = 0;
35344+ else
523b37e3 35345+ pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
1facf9fc 35346+ if (unlikely(err))
35347+ goto out;
35348+
35349+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35350+ if (do_plink) {
35351+ err = au_whdir(h_dir, h_path);
35352+ if (unlikely(err))
35353+ goto out;
35354+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35355+ } else
35356+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35357+ wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35358+
35359+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35360+ err = au_whdir(h_dir, h_path);
35361+ if (unlikely(err))
35362+ goto out;
35363+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35364+
4f0767ce 35365+out:
1facf9fc 35366+ return err;
35367+}
35368+
35369+/*
35370+ * initialize the whiteout base file/dir for @br.
35371+ */
86dc4139 35372+int au_wh_init(struct au_branch *br, struct super_block *sb)
1facf9fc 35373+{
35374+ int err, i;
35375+ const unsigned char do_plink
35376+ = !!au_opt_test(au_mntflags(sb), PLINK);
1facf9fc 35377+ struct inode *h_dir;
86dc4139
AM
35378+ struct path path = br->br_path;
35379+ struct dentry *h_root = path.dentry;
1facf9fc 35380+ struct au_wbr *wbr = br->br_wbr;
35381+ static const struct qstr base_name[] = {
0c3ec466
AM
35382+ [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35383+ sizeof(AUFS_BASE_NAME) - 1),
35384+ [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35385+ sizeof(AUFS_PLINKDIR_NAME) - 1),
35386+ [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35387+ sizeof(AUFS_ORPHDIR_NAME) - 1)
1facf9fc 35388+ };
35389+ struct au_wh_base base[] = {
35390+ [AuBrWh_BASE] = {
35391+ .name = base_name + AuBrWh_BASE,
35392+ .dentry = NULL
35393+ },
35394+ [AuBrWh_PLINK] = {
35395+ .name = base_name + AuBrWh_PLINK,
35396+ .dentry = NULL
35397+ },
35398+ [AuBrWh_ORPH] = {
35399+ .name = base_name + AuBrWh_ORPH,
35400+ .dentry = NULL
35401+ }
35402+ };
35403+
1308ab2a 35404+ if (wbr)
35405+ WbrWhMustWriteLock(wbr);
1facf9fc 35406+
1facf9fc 35407+ for (i = 0; i < AuBrWh_Last; i++) {
35408+ /* doubly whiteouted */
35409+ struct dentry *d;
35410+
35411+ d = au_wh_lkup(h_root, (void *)base[i].name, br);
35412+ err = PTR_ERR(d);
35413+ if (IS_ERR(d))
35414+ goto out;
35415+
35416+ base[i].dentry = d;
35417+ AuDebugOn(wbr
35418+ && wbr->wbr_wh[i]
35419+ && wbr->wbr_wh[i] != base[i].dentry);
35420+ }
35421+
35422+ if (wbr)
35423+ for (i = 0; i < AuBrWh_Last; i++) {
35424+ dput(wbr->wbr_wh[i]);
35425+ wbr->wbr_wh[i] = NULL;
35426+ }
35427+
35428+ err = 0;
1e00d052 35429+ if (!au_br_writable(br->br_perm)) {
5527c038 35430+ h_dir = d_inode(h_root);
1facf9fc 35431+ au_wh_init_ro(h_dir, base, &path);
1e00d052 35432+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 35433+ err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35434+ if (err > 0)
35435+ goto out;
35436+ else if (err)
35437+ goto out_err;
1e00d052 35438+ } else {
1facf9fc 35439+ err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35440+ if (err > 0)
35441+ goto out;
35442+ else if (err)
35443+ goto out_err;
1facf9fc 35444+ }
35445+ goto out; /* success */
35446+
4f0767ce 35447+out_err:
523b37e3
AM
35448+ pr_err("an error(%d) on the writable branch %pd(%s)\n",
35449+ err, h_root, au_sbtype(h_root->d_sb));
4f0767ce 35450+out:
1facf9fc 35451+ for (i = 0; i < AuBrWh_Last; i++)
35452+ dput(base[i].dentry);
35453+ return err;
35454+}
35455+
35456+/* ---------------------------------------------------------------------- */
35457+/*
35458+ * whiteouts are all hard-linked usually.
35459+ * when its link count reaches a ceiling, we create a new whiteout base
35460+ * asynchronously.
35461+ */
35462+
35463+struct reinit_br_wh {
35464+ struct super_block *sb;
35465+ struct au_branch *br;
35466+};
35467+
35468+static void reinit_br_wh(void *arg)
35469+{
35470+ int err;
35471+ aufs_bindex_t bindex;
35472+ struct path h_path;
35473+ struct reinit_br_wh *a = arg;
35474+ struct au_wbr *wbr;
523b37e3 35475+ struct inode *dir, *delegated;
1facf9fc 35476+ struct dentry *h_root;
35477+ struct au_hinode *hdir;
35478+
35479+ err = 0;
35480+ wbr = a->br->br_wbr;
35481+ /* big aufs lock */
35482+ si_noflush_write_lock(a->sb);
35483+ if (!au_br_writable(a->br->br_perm))
35484+ goto out;
35485+ bindex = au_br_index(a->sb, a->br->br_id);
35486+ if (unlikely(bindex < 0))
35487+ goto out;
35488+
1308ab2a 35489+ di_read_lock_parent(a->sb->s_root, AuLock_IR);
5527c038 35490+ dir = d_inode(a->sb->s_root);
1facf9fc 35491+ hdir = au_hi(dir, bindex);
35492+ h_root = au_h_dptr(a->sb->s_root, bindex);
86dc4139 35493+ AuDebugOn(h_root != au_br_dentry(a->br));
1facf9fc 35494+
5afbbe0d 35495+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 35496+ wbr_wh_write_lock(wbr);
35497+ err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35498+ h_root, a->br);
35499+ if (!err) {
86dc4139
AM
35500+ h_path.dentry = wbr->wbr_whbase;
35501+ h_path.mnt = au_br_mnt(a->br);
523b37e3
AM
35502+ delegated = NULL;
35503+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35504+ /*force*/0);
35505+ if (unlikely(err == -EWOULDBLOCK)) {
35506+ pr_warn("cannot retry for NFSv4 delegation"
35507+ " for an internal unlink\n");
35508+ iput(delegated);
35509+ }
1facf9fc 35510+ } else {
523b37e3 35511+ pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
1facf9fc 35512+ err = 0;
35513+ }
35514+ dput(wbr->wbr_whbase);
35515+ wbr->wbr_whbase = NULL;
35516+ if (!err)
86dc4139 35517+ err = au_wh_init(a->br, a->sb);
1facf9fc 35518+ wbr_wh_write_unlock(wbr);
5afbbe0d 35519+ au_hn_inode_unlock(hdir);
1308ab2a 35520+ di_read_unlock(a->sb->s_root, AuLock_IR);
076b876e
AM
35521+ if (!err)
35522+ au_fhsm_wrote(a->sb, bindex, /*force*/0);
1facf9fc 35523+
4f0767ce 35524+out:
1facf9fc 35525+ if (wbr)
35526+ atomic_dec(&wbr->wbr_wh_running);
acd2b654 35527+ au_lcnt_dec(&a->br->br_count);
1facf9fc 35528+ si_write_unlock(a->sb);
027c5e7a 35529+ au_nwt_done(&au_sbi(a->sb)->si_nowait);
9f237c51 35530+ au_kfree_rcu(a);
1facf9fc 35531+ if (unlikely(err))
35532+ AuIOErr("err %d\n", err);
35533+}
35534+
35535+static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35536+{
35537+ int do_dec, wkq_err;
35538+ struct reinit_br_wh *arg;
35539+
35540+ do_dec = 1;
35541+ if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35542+ goto out;
35543+
35544+ /* ignore ENOMEM */
35545+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
35546+ if (arg) {
35547+ /*
35548+ * dec(wh_running), kfree(arg) and dec(br_count)
35549+ * in reinit function
35550+ */
35551+ arg->sb = sb;
35552+ arg->br = br;
acd2b654 35553+ au_lcnt_inc(&br->br_count);
53392da6 35554+ wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
1facf9fc 35555+ if (unlikely(wkq_err)) {
35556+ atomic_dec(&br->br_wbr->wbr_wh_running);
acd2b654 35557+ au_lcnt_dec(&br->br_count);
9f237c51 35558+ au_kfree_rcu(arg);
1facf9fc 35559+ }
35560+ do_dec = 0;
35561+ }
35562+
4f0767ce 35563+out:
1facf9fc 35564+ if (do_dec)
35565+ atomic_dec(&br->br_wbr->wbr_wh_running);
35566+}
35567+
35568+/* ---------------------------------------------------------------------- */
35569+
35570+/*
35571+ * create the whiteout @wh.
35572+ */
35573+static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35574+ struct dentry *wh)
35575+{
35576+ int err;
35577+ struct path h_path = {
35578+ .dentry = wh
35579+ };
35580+ struct au_branch *br;
35581+ struct au_wbr *wbr;
35582+ struct dentry *h_parent;
523b37e3 35583+ struct inode *h_dir, *delegated;
1facf9fc 35584+
35585+ h_parent = wh->d_parent; /* dir inode is locked */
5527c038 35586+ h_dir = d_inode(h_parent);
1facf9fc 35587+ IMustLock(h_dir);
35588+
35589+ br = au_sbr(sb, bindex);
86dc4139 35590+ h_path.mnt = au_br_mnt(br);
1facf9fc 35591+ wbr = br->br_wbr;
35592+ wbr_wh_read_lock(wbr);
35593+ if (wbr->wbr_whbase) {
523b37e3
AM
35594+ delegated = NULL;
35595+ err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35596+ if (unlikely(err == -EWOULDBLOCK)) {
35597+ pr_warn("cannot retry for NFSv4 delegation"
35598+ " for an internal link\n");
35599+ iput(delegated);
35600+ }
1facf9fc 35601+ if (!err || err != -EMLINK)
35602+ goto out;
35603+
35604+ /* link count full. re-initialize br_whbase. */
35605+ kick_reinit_br_wh(sb, br);
35606+ }
35607+
35608+ /* return this error in this context */
b4510431 35609+ err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
076b876e
AM
35610+ if (!err)
35611+ au_fhsm_wrote(sb, bindex, /*force*/0);
1facf9fc 35612+
4f0767ce 35613+out:
1facf9fc 35614+ wbr_wh_read_unlock(wbr);
35615+ return err;
35616+}
35617+
35618+/* ---------------------------------------------------------------------- */
35619+
35620+/*
35621+ * create or remove the diropq.
35622+ */
35623+static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35624+ unsigned int flags)
35625+{
35626+ struct dentry *opq_dentry, *h_dentry;
35627+ struct super_block *sb;
35628+ struct au_branch *br;
35629+ int err;
35630+
35631+ sb = dentry->d_sb;
35632+ br = au_sbr(sb, bindex);
35633+ h_dentry = au_h_dptr(dentry, bindex);
b4510431 35634+ opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
1facf9fc 35635+ if (IS_ERR(opq_dentry))
35636+ goto out;
35637+
35638+ if (au_ftest_diropq(flags, CREATE)) {
35639+ err = link_or_create_wh(sb, bindex, opq_dentry);
35640+ if (!err) {
35641+ au_set_dbdiropq(dentry, bindex);
35642+ goto out; /* success */
35643+ }
35644+ } else {
35645+ struct path tmp = {
35646+ .dentry = opq_dentry,
86dc4139 35647+ .mnt = au_br_mnt(br)
1facf9fc 35648+ };
5527c038 35649+ err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
1facf9fc 35650+ if (!err)
35651+ au_set_dbdiropq(dentry, -1);
35652+ }
35653+ dput(opq_dentry);
35654+ opq_dentry = ERR_PTR(err);
35655+
4f0767ce 35656+out:
1facf9fc 35657+ return opq_dentry;
35658+}
35659+
35660+struct do_diropq_args {
35661+ struct dentry **errp;
35662+ struct dentry *dentry;
35663+ aufs_bindex_t bindex;
35664+ unsigned int flags;
35665+};
35666+
35667+static void call_do_diropq(void *args)
35668+{
35669+ struct do_diropq_args *a = args;
35670+ *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35671+}
35672+
35673+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35674+ unsigned int flags)
35675+{
35676+ struct dentry *diropq, *h_dentry;
35677+
35678+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 35679+ if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
1facf9fc 35680+ diropq = do_diropq(dentry, bindex, flags);
35681+ else {
35682+ int wkq_err;
35683+ struct do_diropq_args args = {
35684+ .errp = &diropq,
35685+ .dentry = dentry,
35686+ .bindex = bindex,
35687+ .flags = flags
35688+ };
35689+
35690+ wkq_err = au_wkq_wait(call_do_diropq, &args);
35691+ if (unlikely(wkq_err))
35692+ diropq = ERR_PTR(wkq_err);
35693+ }
35694+
35695+ return diropq;
35696+}
35697+
35698+/* ---------------------------------------------------------------------- */
35699+
35700+/*
35701+ * lookup whiteout dentry.
35702+ * @h_parent: lower parent dentry which must exist and be locked
35703+ * @base_name: name of dentry which will be whiteouted
35704+ * returns dentry for whiteout.
35705+ */
35706+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35707+ struct au_branch *br)
35708+{
35709+ int err;
35710+ struct qstr wh_name;
35711+ struct dentry *wh_dentry;
35712+
35713+ err = au_wh_name_alloc(&wh_name, base_name);
35714+ wh_dentry = ERR_PTR(err);
35715+ if (!err) {
b4510431 35716+ wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
9f237c51 35717+ au_kfree_try_rcu(wh_name.name);
1facf9fc 35718+ }
35719+ return wh_dentry;
35720+}
35721+
35722+/*
35723+ * link/create a whiteout for @dentry on @bindex.
35724+ */
35725+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35726+ struct dentry *h_parent)
35727+{
35728+ struct dentry *wh_dentry;
35729+ struct super_block *sb;
35730+ int err;
35731+
35732+ sb = dentry->d_sb;
35733+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
5527c038 35734+ if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
1facf9fc 35735+ err = link_or_create_wh(sb, bindex, wh_dentry);
076b876e 35736+ if (!err) {
1facf9fc 35737+ au_set_dbwh(dentry, bindex);
076b876e
AM
35738+ au_fhsm_wrote(sb, bindex, /*force*/0);
35739+ } else {
1facf9fc 35740+ dput(wh_dentry);
35741+ wh_dentry = ERR_PTR(err);
35742+ }
35743+ }
35744+
35745+ return wh_dentry;
35746+}
35747+
35748+/* ---------------------------------------------------------------------- */
35749+
35750+/* Delete all whiteouts in this directory on branch bindex. */
35751+static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
35752+ aufs_bindex_t bindex, struct au_branch *br)
35753+{
35754+ int err;
35755+ unsigned long ul, n;
35756+ struct qstr wh_name;
35757+ char *p;
35758+ struct hlist_head *head;
c06a8ce3 35759+ struct au_vdir_wh *pos;
1facf9fc 35760+ struct au_vdir_destr *str;
35761+
35762+ err = -ENOMEM;
537831f9 35763+ p = (void *)__get_free_page(GFP_NOFS);
1facf9fc 35764+ wh_name.name = p;
35765+ if (unlikely(!wh_name.name))
35766+ goto out;
35767+
35768+ err = 0;
35769+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35770+ p += AUFS_WH_PFX_LEN;
35771+ n = whlist->nh_num;
35772+ head = whlist->nh_head;
35773+ for (ul = 0; !err && ul < n; ul++, head++) {
c06a8ce3
AM
35774+ hlist_for_each_entry(pos, head, wh_hash) {
35775+ if (pos->wh_bindex != bindex)
1facf9fc 35776+ continue;
35777+
c06a8ce3 35778+ str = &pos->wh_str;
1facf9fc 35779+ if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35780+ memcpy(p, str->name, str->len);
35781+ wh_name.len = AUFS_WH_PFX_LEN + str->len;
35782+ err = unlink_wh_name(h_dentry, &wh_name, br);
35783+ if (!err)
35784+ continue;
35785+ break;
35786+ }
35787+ AuIOErr("whiteout name too long %.*s\n",
35788+ str->len, str->name);
35789+ err = -EIO;
35790+ break;
35791+ }
35792+ }
1c60b727 35793+ free_page((unsigned long)wh_name.name);
1facf9fc 35794+
4f0767ce 35795+out:
1facf9fc 35796+ return err;
35797+}
35798+
35799+struct del_wh_children_args {
35800+ int *errp;
35801+ struct dentry *h_dentry;
1308ab2a 35802+ struct au_nhash *whlist;
1facf9fc 35803+ aufs_bindex_t bindex;
35804+ struct au_branch *br;
35805+};
35806+
35807+static void call_del_wh_children(void *args)
35808+{
35809+ struct del_wh_children_args *a = args;
1308ab2a 35810+ *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
1facf9fc 35811+}
35812+
35813+/* ---------------------------------------------------------------------- */
35814+
35815+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35816+{
35817+ struct au_whtmp_rmdir *whtmp;
dece6358 35818+ int err;
1308ab2a 35819+ unsigned int rdhash;
dece6358
AM
35820+
35821+ SiMustAnyLock(sb);
1facf9fc 35822+
be52b249 35823+ whtmp = kzalloc(sizeof(*whtmp), gfp);
dece6358
AM
35824+ if (unlikely(!whtmp)) {
35825+ whtmp = ERR_PTR(-ENOMEM);
1facf9fc 35826+ goto out;
dece6358 35827+ }
1facf9fc 35828+
1308ab2a 35829+ /* no estimation for dir size */
35830+ rdhash = au_sbi(sb)->si_rdhash;
35831+ if (!rdhash)
35832+ rdhash = AUFS_RDHASH_DEF;
35833+ err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35834+ if (unlikely(err)) {
9f237c51 35835+ au_kfree_rcu(whtmp);
1308ab2a 35836+ whtmp = ERR_PTR(err);
35837+ }
dece6358 35838+
4f0767ce 35839+out:
dece6358 35840+ return whtmp;
1facf9fc 35841+}
35842+
35843+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35844+{
027c5e7a 35845+ if (whtmp->br)
acd2b654 35846+ au_lcnt_dec(&whtmp->br->br_count);
1facf9fc 35847+ dput(whtmp->wh_dentry);
35848+ iput(whtmp->dir);
dece6358 35849+ au_nhash_wh_free(&whtmp->whlist);
9f237c51 35850+ au_kfree_rcu(whtmp);
1facf9fc 35851+}
35852+
35853+/*
35854+ * rmdir the whiteouted temporary named dir @h_dentry.
35855+ * @whlist: whiteouted children.
35856+ */
35857+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35858+ struct dentry *wh_dentry, struct au_nhash *whlist)
35859+{
35860+ int err;
2000de60 35861+ unsigned int h_nlink;
1facf9fc 35862+ struct path h_tmp;
35863+ struct inode *wh_inode, *h_dir;
35864+ struct au_branch *br;
35865+
5527c038 35866+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
1facf9fc 35867+ IMustLock(h_dir);
35868+
35869+ br = au_sbr(dir->i_sb, bindex);
5527c038 35870+ wh_inode = d_inode(wh_dentry);
febd17d6 35871+ inode_lock_nested(wh_inode, AuLsc_I_CHILD);
1facf9fc 35872+
35873+ /*
35874+ * someone else might change some whiteouts while we were sleeping.
35875+ * it means this whlist may have an obsoleted entry.
35876+ */
35877+ if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
35878+ err = del_wh_children(wh_dentry, whlist, bindex, br);
35879+ else {
35880+ int wkq_err;
35881+ struct del_wh_children_args args = {
35882+ .errp = &err,
35883+ .h_dentry = wh_dentry,
1308ab2a 35884+ .whlist = whlist,
1facf9fc 35885+ .bindex = bindex,
35886+ .br = br
35887+ };
35888+
35889+ wkq_err = au_wkq_wait(call_del_wh_children, &args);
35890+ if (unlikely(wkq_err))
35891+ err = wkq_err;
35892+ }
febd17d6 35893+ inode_unlock(wh_inode);
1facf9fc 35894+
35895+ if (!err) {
35896+ h_tmp.dentry = wh_dentry;
86dc4139 35897+ h_tmp.mnt = au_br_mnt(br);
2000de60 35898+ h_nlink = h_dir->i_nlink;
1facf9fc 35899+ err = vfsub_rmdir(h_dir, &h_tmp);
2000de60
JR
35900+ /* some fs doesn't change the parent nlink in some cases */
35901+ h_nlink -= h_dir->i_nlink;
1facf9fc 35902+ }
35903+
35904+ if (!err) {
5afbbe0d 35905+ if (au_ibtop(dir) == bindex) {
7f207e10 35906+ /* todo: dir->i_mutex is necessary */
1facf9fc 35907+ au_cpup_attr_timesizes(dir);
2000de60
JR
35908+ if (h_nlink)
35909+ vfsub_drop_nlink(dir);
1facf9fc 35910+ }
35911+ return 0; /* success */
35912+ }
35913+
523b37e3 35914+ pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
1facf9fc 35915+ return err;
35916+}
35917+
35918+static void call_rmdir_whtmp(void *args)
35919+{
35920+ int err;
e49829fe 35921+ aufs_bindex_t bindex;
1facf9fc 35922+ struct au_whtmp_rmdir *a = args;
35923+ struct super_block *sb;
35924+ struct dentry *h_parent;
35925+ struct inode *h_dir;
1facf9fc 35926+ struct au_hinode *hdir;
35927+
35928+ /* rmdir by nfsd may cause deadlock with this i_mutex */
febd17d6 35929+ /* inode_lock(a->dir); */
e49829fe 35930+ err = -EROFS;
1facf9fc 35931+ sb = a->dir->i_sb;
e49829fe
JR
35932+ si_read_lock(sb, !AuLock_FLUSH);
35933+ if (!au_br_writable(a->br->br_perm))
35934+ goto out;
35935+ bindex = au_br_index(sb, a->br->br_id);
35936+ if (unlikely(bindex < 0))
1facf9fc 35937+ goto out;
35938+
35939+ err = -EIO;
1facf9fc 35940+ ii_write_lock_parent(a->dir);
35941+ h_parent = dget_parent(a->wh_dentry);
5527c038 35942+ h_dir = d_inode(h_parent);
e49829fe 35943+ hdir = au_hi(a->dir, bindex);
86dc4139
AM
35944+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
35945+ if (unlikely(err))
35946+ goto out_mnt;
5afbbe0d 35947+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
e49829fe
JR
35948+ err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
35949+ a->br);
86dc4139
AM
35950+ if (!err)
35951+ err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
5afbbe0d 35952+ au_hn_inode_unlock(hdir);
86dc4139
AM
35953+ vfsub_mnt_drop_write(au_br_mnt(a->br));
35954+
35955+out_mnt:
1facf9fc 35956+ dput(h_parent);
35957+ ii_write_unlock(a->dir);
4f0767ce 35958+out:
febd17d6 35959+ /* inode_unlock(a->dir); */
1facf9fc 35960+ au_whtmp_rmdir_free(a);
027c5e7a
AM
35961+ si_read_unlock(sb);
35962+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 35963+ if (unlikely(err))
35964+ AuIOErr("err %d\n", err);
35965+}
35966+
35967+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35968+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
35969+{
35970+ int wkq_err;
e49829fe 35971+ struct super_block *sb;
1facf9fc 35972+
35973+ IMustLock(dir);
35974+
35975+ /* all post-process will be done in do_rmdir_whtmp(). */
e49829fe 35976+ sb = dir->i_sb;
1facf9fc 35977+ args->dir = au_igrab(dir);
e49829fe 35978+ args->br = au_sbr(sb, bindex);
acd2b654 35979+ au_lcnt_inc(&args->br->br_count);
1facf9fc 35980+ args->wh_dentry = dget(wh_dentry);
53392da6 35981+ wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
1facf9fc 35982+ if (unlikely(wkq_err)) {
523b37e3 35983+ pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
1facf9fc 35984+ au_whtmp_rmdir_free(args);
35985+ }
35986+}
7f207e10
AM
35987diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
35988--- /usr/share/empty/fs/aufs/whout.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 35989+++ linux/fs/aufs/whout.h 2018-08-12 23:43:05.463458173 +0200
062440b3
AM
35990@@ -0,0 +1,86 @@
35991+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 35992+/*
b00004a5 35993+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 35994+ *
35995+ * This program, aufs is free software; you can redistribute it and/or modify
35996+ * it under the terms of the GNU General Public License as published by
35997+ * the Free Software Foundation; either version 2 of the License, or
35998+ * (at your option) any later version.
dece6358
AM
35999+ *
36000+ * This program is distributed in the hope that it will be useful,
36001+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36002+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36003+ * GNU General Public License for more details.
36004+ *
36005+ * You should have received a copy of the GNU General Public License
523b37e3 36006+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36007+ */
36008+
36009+/*
36010+ * whiteout for logical deletion and opaque directory
36011+ */
36012+
36013+#ifndef __AUFS_WHOUT_H__
36014+#define __AUFS_WHOUT_H__
36015+
36016+#ifdef __KERNEL__
36017+
1facf9fc 36018+#include "dir.h"
36019+
36020+/* whout.c */
36021+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
076b876e
AM
36022+int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
36023+int au_diropq_test(struct dentry *h_dentry);
3c1bdaff 36024+struct au_branch;
1facf9fc 36025+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
36026+ struct qstr *prefix);
36027+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
36028+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
36029+ struct dentry *dentry);
86dc4139 36030+int au_wh_init(struct au_branch *br, struct super_block *sb);
1facf9fc 36031+
36032+/* diropq flags */
36033+#define AuDiropq_CREATE 1
36034+#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
7f207e10
AM
36035+#define au_fset_diropq(flags, name) \
36036+ do { (flags) |= AuDiropq_##name; } while (0)
36037+#define au_fclr_diropq(flags, name) \
36038+ do { (flags) &= ~AuDiropq_##name; } while (0)
1facf9fc 36039+
36040+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
36041+ unsigned int flags);
36042+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
36043+ struct au_branch *br);
36044+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
36045+ struct dentry *h_parent);
36046+
36047+/* real rmdir for the whiteout-ed dir */
36048+struct au_whtmp_rmdir {
36049+ struct inode *dir;
e49829fe 36050+ struct au_branch *br;
1facf9fc 36051+ struct dentry *wh_dentry;
dece6358 36052+ struct au_nhash whlist;
1facf9fc 36053+};
36054+
36055+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
36056+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
36057+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
36058+ struct dentry *wh_dentry, struct au_nhash *whlist);
36059+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36060+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
36061+
36062+/* ---------------------------------------------------------------------- */
36063+
36064+static inline struct dentry *au_diropq_create(struct dentry *dentry,
36065+ aufs_bindex_t bindex)
36066+{
36067+ return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
36068+}
36069+
36070+static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
36071+{
36072+ return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
36073+}
36074+
36075+#endif /* __KERNEL__ */
36076+#endif /* __AUFS_WHOUT_H__ */
7f207e10
AM
36077diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
36078--- /usr/share/empty/fs/aufs/wkq.c 1970-01-01 01:00:00.000000000 +0100
9f237c51 36079+++ linux/fs/aufs/wkq.c 2018-12-27 13:19:17.711749485 +0100
acd2b654 36080@@ -0,0 +1,392 @@
cd7a4cd9 36081+// SPDX-License-Identifier: GPL-2.0
1facf9fc 36082+/*
b00004a5 36083+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 36084+ *
36085+ * This program, aufs is free software; you can redistribute it and/or modify
36086+ * it under the terms of the GNU General Public License as published by
36087+ * the Free Software Foundation; either version 2 of the License, or
36088+ * (at your option) any later version.
dece6358
AM
36089+ *
36090+ * This program is distributed in the hope that it will be useful,
36091+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36092+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36093+ * GNU General Public License for more details.
36094+ *
36095+ * You should have received a copy of the GNU General Public License
523b37e3 36096+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36097+ */
36098+
36099+/*
36100+ * workqueue for asynchronous/super-io operations
acd2b654 36101+ * todo: try new credential scheme
1facf9fc 36102+ */
36103+
dece6358 36104+#include <linux/module.h>
1facf9fc 36105+#include "aufs.h"
36106+
9dbd164d 36107+/* internal workqueue named AUFS_WKQ_NAME */
b752ccd1 36108+
9dbd164d 36109+static struct workqueue_struct *au_wkq;
1facf9fc 36110+
36111+struct au_wkinfo {
36112+ struct work_struct wk;
7f207e10 36113+ struct kobject *kobj;
1facf9fc 36114+
36115+ unsigned int flags; /* see wkq.h */
36116+
36117+ au_wkq_func_t func;
36118+ void *args;
36119+
8b6a4947
AM
36120+#ifdef CONFIG_LOCKDEP
36121+ int dont_check;
36122+ struct held_lock **hlock;
36123+#endif
36124+
1facf9fc 36125+ struct completion *comp;
36126+};
36127+
36128+/* ---------------------------------------------------------------------- */
8b6a4947
AM
36129+/*
36130+ * Aufs passes some operations to the workqueue such as the internal copyup.
36131+ * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
36132+ * job run by workqueue depends upon the locks acquired in the other task.
36133+ * Delegating a small operation to the workqueue, aufs passes its lockdep
36134+ * information too. And the job in the workqueue restores the info in order to
36135+ * pretend as if it acquired those locks. This is just to make LOCKDEP work
36136+ * correctly and expectedly.
36137+ */
36138+
36139+#ifndef CONFIG_LOCKDEP
36140+AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
36141+AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
36142+AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
36143+AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
36144+AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
36145+#else
36146+static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
36147+{
36148+ wkinfo->hlock = NULL;
36149+ wkinfo->dont_check = 0;
36150+}
36151+
36152+/*
36153+ * 1: matched
36154+ * 0: unmatched
36155+ */
36156+static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
36157+{
36158+ static DEFINE_SPINLOCK(spin);
36159+ static struct {
36160+ char *name;
36161+ struct lock_class_key *key;
36162+ } a[] = {
36163+ { .name = "&sbinfo->si_rwsem" },
36164+ { .name = "&finfo->fi_rwsem" },
36165+ { .name = "&dinfo->di_rwsem" },
36166+ { .name = "&iinfo->ii_rwsem" }
36167+ };
36168+ static int set;
36169+ int i;
36170+
36171+ /* lockless read from 'set.' see below */
36172+ if (set == ARRAY_SIZE(a)) {
36173+ for (i = 0; i < ARRAY_SIZE(a); i++)
36174+ if (a[i].key == key)
36175+ goto match;
36176+ goto unmatch;
36177+ }
36178+
36179+ spin_lock(&spin);
36180+ if (set)
36181+ for (i = 0; i < ARRAY_SIZE(a); i++)
36182+ if (a[i].key == key) {
36183+ spin_unlock(&spin);
36184+ goto match;
36185+ }
36186+ for (i = 0; i < ARRAY_SIZE(a); i++) {
36187+ if (a[i].key) {
36188+ if (unlikely(a[i].key == key)) { /* rare but possible */
36189+ spin_unlock(&spin);
36190+ goto match;
36191+ } else
36192+ continue;
36193+ }
36194+ if (strstr(a[i].name, name)) {
36195+ /*
36196+ * the order of these three lines is important for the
36197+ * lockless read above.
36198+ */
36199+ a[i].key = key;
36200+ spin_unlock(&spin);
36201+ set++;
36202+ /* AuDbg("%d, %s\n", set, name); */
36203+ goto match;
36204+ }
36205+ }
36206+ spin_unlock(&spin);
36207+ goto unmatch;
36208+
36209+match:
36210+ return 1;
36211+unmatch:
36212+ return 0;
36213+}
36214+
36215+static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36216+{
36217+ int err, n;
36218+ struct task_struct *curr;
36219+ struct held_lock **hl, *held_locks, *p;
36220+
36221+ err = 0;
36222+ curr = current;
36223+ wkinfo->dont_check = lockdep_recursing(curr);
36224+ if (wkinfo->dont_check)
36225+ goto out;
36226+ n = curr->lockdep_depth;
36227+ if (!n)
36228+ goto out;
36229+
36230+ err = -ENOMEM;
36231+ wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36232+ if (unlikely(!wkinfo->hlock))
36233+ goto out;
36234+
36235+ err = 0;
36236+#if 0
36237+ if (0 && au_debug_test()) /* left for debugging */
36238+ lockdep_print_held_locks(curr);
36239+#endif
36240+ held_locks = curr->held_locks;
36241+ hl = wkinfo->hlock;
36242+ while (n--) {
36243+ p = held_locks++;
36244+ if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36245+ *hl++ = p;
36246+ }
36247+ *hl = NULL;
36248+
36249+out:
36250+ return err;
36251+}
36252+
36253+static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36254+{
9f237c51 36255+ au_kfree_try_rcu(wkinfo->hlock);
8b6a4947
AM
36256+}
36257+
36258+static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36259+{
36260+ struct held_lock *p, **hl = wkinfo->hlock;
36261+ int subclass;
36262+
36263+ if (wkinfo->dont_check)
36264+ lockdep_off();
36265+ if (!hl)
36266+ return;
36267+ while ((p = *hl++)) { /* assignment */
36268+ subclass = lockdep_hlock_class(p)->subclass;
36269+ /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36270+ if (p->read)
36271+ rwsem_acquire_read(p->instance, subclass, 0,
36272+ /*p->acquire_ip*/_RET_IP_);
36273+ else
36274+ rwsem_acquire(p->instance, subclass, 0,
36275+ /*p->acquire_ip*/_RET_IP_);
36276+ }
36277+}
36278+
36279+static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36280+{
36281+ struct held_lock *p, **hl = wkinfo->hlock;
36282+
36283+ if (wkinfo->dont_check)
36284+ lockdep_on();
36285+ if (!hl)
36286+ return;
36287+ while ((p = *hl++)) /* assignment */
36288+ rwsem_release(p->instance, 0, /*p->acquire_ip*/_RET_IP_);
36289+}
36290+#endif
1facf9fc 36291+
1facf9fc 36292+static void wkq_func(struct work_struct *wk)
36293+{
36294+ struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36295+
2dfbb274 36296+ AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
7f207e10
AM
36297+ AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36298+
8b6a4947 36299+ au_wkq_lockdep_pre(wkinfo);
1facf9fc 36300+ wkinfo->func(wkinfo->args);
8b6a4947 36301+ au_wkq_lockdep_post(wkinfo);
1facf9fc 36302+ if (au_ftest_wkq(wkinfo->flags, WAIT))
36303+ complete(wkinfo->comp);
36304+ else {
7f207e10 36305+ kobject_put(wkinfo->kobj);
9dbd164d 36306+ module_put(THIS_MODULE); /* todo: ?? */
9f237c51 36307+ au_kfree_rcu(wkinfo);
1facf9fc 36308+ }
36309+}
36310+
36311+/*
36312+ * Since struct completion is large, try allocating it dynamically.
36313+ */
c2b27bf2 36314+#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
1facf9fc 36315+#define AuWkqCompDeclare(name) struct completion *comp = NULL
36316+
36317+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36318+{
36319+ *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36320+ if (*comp) {
36321+ init_completion(*comp);
36322+ wkinfo->comp = *comp;
36323+ return 0;
36324+ }
36325+ return -ENOMEM;
36326+}
36327+
36328+static void au_wkq_comp_free(struct completion *comp)
36329+{
9f237c51 36330+ au_kfree_rcu(comp);
1facf9fc 36331+}
36332+
36333+#else
36334+
36335+/* no braces */
36336+#define AuWkqCompDeclare(name) \
36337+ DECLARE_COMPLETION_ONSTACK(_ ## name); \
36338+ struct completion *comp = &_ ## name
36339+
36340+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36341+{
36342+ wkinfo->comp = *comp;
36343+ return 0;
36344+}
36345+
36346+static void au_wkq_comp_free(struct completion *comp __maybe_unused)
36347+{
36348+ /* empty */
36349+}
36350+#endif /* 4KSTACKS */
36351+
53392da6 36352+static void au_wkq_run(struct au_wkinfo *wkinfo)
1facf9fc 36353+{
53392da6
AM
36354+ if (au_ftest_wkq(wkinfo->flags, NEST)) {
36355+ if (au_wkq_test()) {
38d290e6 36356+ AuWarn1("wkq from wkq, unless silly-rename on NFS,"
acd2b654
AM
36357+ " due to a dead dir by UDBA,"
36358+ " or async xino write?\n");
53392da6
AM
36359+ AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36360+ }
36361+ } else
36362+ au_dbg_verify_kthread();
36363+
36364+ if (au_ftest_wkq(wkinfo->flags, WAIT)) {
a1f66529 36365+ INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
9dbd164d 36366+ queue_work(au_wkq, &wkinfo->wk);
4a4d8108
AM
36367+ } else {
36368+ INIT_WORK(&wkinfo->wk, wkq_func);
36369+ schedule_work(&wkinfo->wk);
36370+ }
1facf9fc 36371+}
36372+
7f207e10
AM
36373+/*
36374+ * Be careful. It is easy to make deadlock happen.
36375+ * processA: lock, wkq and wait
36376+ * processB: wkq and wait, lock in wkq
36377+ * --> deadlock
36378+ */
b752ccd1 36379+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
1facf9fc 36380+{
36381+ int err;
36382+ AuWkqCompDeclare(comp);
36383+ struct au_wkinfo wkinfo = {
b752ccd1 36384+ .flags = flags,
1facf9fc 36385+ .func = func,
36386+ .args = args
36387+ };
36388+
36389+ err = au_wkq_comp_alloc(&wkinfo, &comp);
8b6a4947
AM
36390+ if (unlikely(err))
36391+ goto out;
36392+ err = au_wkq_lockdep_alloc(&wkinfo);
36393+ if (unlikely(err))
36394+ goto out_comp;
1facf9fc 36395+ if (!err) {
53392da6 36396+ au_wkq_run(&wkinfo);
1facf9fc 36397+ /* no timeout, no interrupt */
36398+ wait_for_completion(wkinfo.comp);
1facf9fc 36399+ }
8b6a4947 36400+ au_wkq_lockdep_free(&wkinfo);
1facf9fc 36401+
8b6a4947
AM
36402+out_comp:
36403+ au_wkq_comp_free(comp);
36404+out:
36405+ destroy_work_on_stack(&wkinfo.wk);
1facf9fc 36406+ return err;
1facf9fc 36407+}
36408+
027c5e7a
AM
36409+/*
36410+ * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36411+ * problem in a concurrent umounting.
36412+ */
53392da6
AM
36413+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36414+ unsigned int flags)
1facf9fc 36415+{
36416+ int err;
36417+ struct au_wkinfo *wkinfo;
36418+
f0c0a007 36419+ atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
1facf9fc 36420+
36421+ /*
36422+ * wkq_func() must free this wkinfo.
36423+ * it highly depends upon the implementation of workqueue.
36424+ */
36425+ err = 0;
36426+ wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36427+ if (wkinfo) {
7f207e10 36428+ wkinfo->kobj = &au_sbi(sb)->si_kobj;
53392da6 36429+ wkinfo->flags = flags & ~AuWkq_WAIT;
1facf9fc 36430+ wkinfo->func = func;
36431+ wkinfo->args = args;
36432+ wkinfo->comp = NULL;
8b6a4947 36433+ au_wkq_lockdep_init(wkinfo);
7f207e10 36434+ kobject_get(wkinfo->kobj);
9dbd164d 36435+ __module_get(THIS_MODULE); /* todo: ?? */
1facf9fc 36436+
53392da6 36437+ au_wkq_run(wkinfo);
1facf9fc 36438+ } else {
36439+ err = -ENOMEM;
e49829fe 36440+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 36441+ }
36442+
36443+ return err;
36444+}
36445+
36446+/* ---------------------------------------------------------------------- */
36447+
36448+void au_nwt_init(struct au_nowait_tasks *nwt)
36449+{
f0c0a007
AM
36450+ atomic_set(&nwt->nw_len, 0);
36451+ /* smp_mb(); */ /* atomic_set */
1facf9fc 36452+ init_waitqueue_head(&nwt->nw_wq);
36453+}
36454+
36455+void au_wkq_fin(void)
36456+{
9dbd164d 36457+ destroy_workqueue(au_wkq);
1facf9fc 36458+}
36459+
36460+int __init au_wkq_init(void)
36461+{
9dbd164d 36462+ int err;
b752ccd1
AM
36463+
36464+ err = 0;
86dc4139 36465+ au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
9dbd164d
AM
36466+ if (IS_ERR(au_wkq))
36467+ err = PTR_ERR(au_wkq);
36468+ else if (!au_wkq)
36469+ err = -ENOMEM;
b752ccd1
AM
36470+
36471+ return err;
1facf9fc 36472+}
7f207e10
AM
36473diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36474--- /usr/share/empty/fs/aufs/wkq.h 1970-01-01 01:00:00.000000000 +0100
acd2b654
AM
36475+++ linux/fs/aufs/wkq.h 2018-10-23 12:33:35.599375796 +0200
36476@@ -0,0 +1,89 @@
062440b3 36477+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 36478+/*
b00004a5 36479+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 36480+ *
36481+ * This program, aufs is free software; you can redistribute it and/or modify
36482+ * it under the terms of the GNU General Public License as published by
36483+ * the Free Software Foundation; either version 2 of the License, or
36484+ * (at your option) any later version.
dece6358
AM
36485+ *
36486+ * This program is distributed in the hope that it will be useful,
36487+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36488+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36489+ * GNU General Public License for more details.
36490+ *
36491+ * You should have received a copy of the GNU General Public License
523b37e3 36492+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36493+ */
36494+
36495+/*
36496+ * workqueue for asynchronous/super-io operations
36497+ * todo: try new credentials management scheme
36498+ */
36499+
36500+#ifndef __AUFS_WKQ_H__
36501+#define __AUFS_WKQ_H__
36502+
36503+#ifdef __KERNEL__
36504+
8b6a4947 36505+#include <linux/wait.h>
5afbbe0d 36506+
dece6358
AM
36507+struct super_block;
36508+
1facf9fc 36509+/* ---------------------------------------------------------------------- */
36510+
36511+/*
36512+ * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36513+ */
36514+struct au_nowait_tasks {
f0c0a007 36515+ atomic_t nw_len;
1facf9fc 36516+ wait_queue_head_t nw_wq;
36517+};
36518+
36519+/* ---------------------------------------------------------------------- */
36520+
36521+typedef void (*au_wkq_func_t)(void *args);
36522+
36523+/* wkq flags */
36524+#define AuWkq_WAIT 1
9dbd164d 36525+#define AuWkq_NEST (1 << 1)
1facf9fc 36526+#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
7f207e10
AM
36527+#define au_fset_wkq(flags, name) \
36528+ do { (flags) |= AuWkq_##name; } while (0)
36529+#define au_fclr_wkq(flags, name) \
36530+ do { (flags) &= ~AuWkq_##name; } while (0)
1facf9fc 36531+
36532+/* wkq.c */
b752ccd1 36533+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
53392da6
AM
36534+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36535+ unsigned int flags);
1facf9fc 36536+void au_nwt_init(struct au_nowait_tasks *nwt);
36537+int __init au_wkq_init(void);
36538+void au_wkq_fin(void);
36539+
36540+/* ---------------------------------------------------------------------- */
36541+
53392da6
AM
36542+static inline int au_wkq_test(void)
36543+{
36544+ return current->flags & PF_WQ_WORKER;
36545+}
36546+
b752ccd1 36547+static inline int au_wkq_wait(au_wkq_func_t func, void *args)
1facf9fc 36548+{
b752ccd1 36549+ return au_wkq_do_wait(AuWkq_WAIT, func, args);
1facf9fc 36550+}
36551+
36552+static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36553+{
f0c0a007 36554+ if (atomic_dec_and_test(&nwt->nw_len))
1facf9fc 36555+ wake_up_all(&nwt->nw_wq);
36556+}
36557+
36558+static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36559+{
f0c0a007 36560+ wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
1facf9fc 36561+ return 0;
36562+}
36563+
36564+#endif /* __KERNEL__ */
36565+#endif /* __AUFS_WKQ_H__ */
c1595e42
JR
36566diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36567--- /usr/share/empty/fs/aufs/xattr.c 1970-01-01 01:00:00.000000000 +0100
9f237c51 36568+++ linux/fs/aufs/xattr.c 2018-12-27 13:19:17.711749485 +0100
062440b3 36569@@ -0,0 +1,356 @@
cd7a4cd9 36570+// SPDX-License-Identifier: GPL-2.0
c1595e42 36571+/*
b00004a5 36572+ * Copyright (C) 2014-2018 Junjiro R. Okajima
c1595e42
JR
36573+ *
36574+ * This program, aufs is free software; you can redistribute it and/or modify
36575+ * it under the terms of the GNU General Public License as published by
36576+ * the Free Software Foundation; either version 2 of the License, or
36577+ * (at your option) any later version.
36578+ *
36579+ * This program is distributed in the hope that it will be useful,
36580+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36581+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36582+ * GNU General Public License for more details.
36583+ *
36584+ * You should have received a copy of the GNU General Public License
36585+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
36586+ */
36587+
36588+/*
36589+ * handling xattr functions
36590+ */
36591+
a2654f78
AM
36592+#include <linux/fs.h>
36593+#include <linux/posix_acl_xattr.h>
c1595e42
JR
36594+#include <linux/xattr.h>
36595+#include "aufs.h"
36596+
36597+static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36598+{
36599+ if (!ignore_flags)
36600+ goto out;
36601+ switch (err) {
36602+ case -ENOMEM:
36603+ case -EDQUOT:
36604+ goto out;
36605+ }
36606+
36607+ if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36608+ err = 0;
36609+ goto out;
36610+ }
36611+
36612+#define cmp(brattr, prefix) do { \
36613+ if (!strncmp(name, XATTR_##prefix##_PREFIX, \
36614+ XATTR_##prefix##_PREFIX_LEN)) { \
36615+ if (ignore_flags & AuBrAttr_ICEX_##brattr) \
36616+ err = 0; \
36617+ goto out; \
36618+ } \
36619+ } while (0)
36620+
36621+ cmp(SEC, SECURITY);
36622+ cmp(SYS, SYSTEM);
36623+ cmp(TR, TRUSTED);
36624+ cmp(USR, USER);
36625+#undef cmp
36626+
36627+ if (ignore_flags & AuBrAttr_ICEX_OTH)
36628+ err = 0;
36629+
36630+out:
36631+ return err;
36632+}
36633+
36634+static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36635+
36636+static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
7e9cd9fe
AM
36637+ char *name, char **buf, unsigned int ignore_flags,
36638+ unsigned int verbose)
c1595e42
JR
36639+{
36640+ int err;
36641+ ssize_t ssz;
36642+ struct inode *h_idst;
36643+
36644+ ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
36645+ err = ssz;
36646+ if (unlikely(err <= 0)) {
c1595e42
JR
36647+ if (err == -ENODATA
36648+ || (err == -EOPNOTSUPP
b912730e 36649+ && ((ignore_flags & au_xattr_out_of_list)
5527c038 36650+ || (au_test_nfs_noacl(d_inode(h_src))
b912730e
AM
36651+ && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36652+ || !strcmp(name,
36653+ XATTR_NAME_POSIX_ACL_DEFAULT))))
36654+ ))
c1595e42 36655+ err = 0;
b912730e
AM
36656+ if (err && (verbose || au_debug_test()))
36657+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
36658+ goto out;
36659+ }
36660+
36661+ /* unlock it temporary */
5527c038 36662+ h_idst = d_inode(h_dst);
febd17d6 36663+ inode_unlock(h_idst);
c1595e42 36664+ err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
febd17d6 36665+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42 36666+ if (unlikely(err)) {
7e9cd9fe
AM
36667+ if (verbose || au_debug_test())
36668+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
36669+ err = au_xattr_ignore(err, name, ignore_flags);
36670+ }
36671+
36672+out:
36673+ return err;
36674+}
36675+
7e9cd9fe
AM
36676+int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
36677+ unsigned int verbose)
c1595e42
JR
36678+{
36679+ int err, unlocked, acl_access, acl_default;
36680+ ssize_t ssz;
36681+ struct inode *h_isrc, *h_idst;
36682+ char *value, *p, *o, *e;
36683+
36684+ /* try stopping to update the source inode while we are referencing */
7e9cd9fe 36685+ /* there should not be the parent-child relationship between them */
5527c038
JR
36686+ h_isrc = d_inode(h_src);
36687+ h_idst = d_inode(h_dst);
febd17d6 36688+ inode_unlock(h_idst);
be118d29 36689+ inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
febd17d6 36690+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42
JR
36691+ unlocked = 0;
36692+
36693+ /* some filesystems don't list POSIX ACL, for example tmpfs */
36694+ ssz = vfs_listxattr(h_src, NULL, 0);
36695+ err = ssz;
36696+ if (unlikely(err < 0)) {
36697+ AuTraceErr(err);
36698+ if (err == -ENODATA
36699+ || err == -EOPNOTSUPP)
36700+ err = 0; /* ignore */
36701+ goto out;
36702+ }
36703+
36704+ err = 0;
36705+ p = NULL;
36706+ o = NULL;
36707+ if (ssz) {
36708+ err = -ENOMEM;
36709+ p = kmalloc(ssz, GFP_NOFS);
36710+ o = p;
36711+ if (unlikely(!p))
36712+ goto out;
36713+ err = vfs_listxattr(h_src, p, ssz);
36714+ }
3c1bdaff 36715+ inode_unlock_shared(h_isrc);
c1595e42
JR
36716+ unlocked = 1;
36717+ AuDbg("err %d, ssz %zd\n", err, ssz);
36718+ if (unlikely(err < 0))
36719+ goto out_free;
36720+
36721+ err = 0;
36722+ e = p + ssz;
36723+ value = NULL;
36724+ acl_access = 0;
36725+ acl_default = 0;
36726+ while (!err && p < e) {
36727+ acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36728+ sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36729+ acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36730+ sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36731+ - 1);
7e9cd9fe
AM
36732+ err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36733+ verbose);
c1595e42
JR
36734+ p += strlen(p) + 1;
36735+ }
36736+ AuTraceErr(err);
36737+ ignore_flags |= au_xattr_out_of_list;
36738+ if (!err && !acl_access) {
36739+ err = au_do_cpup_xattr(h_dst, h_src,
36740+ XATTR_NAME_POSIX_ACL_ACCESS, &value,
7e9cd9fe 36741+ ignore_flags, verbose);
c1595e42
JR
36742+ AuTraceErr(err);
36743+ }
36744+ if (!err && !acl_default) {
36745+ err = au_do_cpup_xattr(h_dst, h_src,
36746+ XATTR_NAME_POSIX_ACL_DEFAULT, &value,
7e9cd9fe 36747+ ignore_flags, verbose);
c1595e42
JR
36748+ AuTraceErr(err);
36749+ }
36750+
9f237c51 36751+ au_kfree_try_rcu(value);
c1595e42
JR
36752+
36753+out_free:
9f237c51 36754+ au_kfree_try_rcu(o);
c1595e42
JR
36755+out:
36756+ if (!unlocked)
3c1bdaff 36757+ inode_unlock_shared(h_isrc);
c1595e42
JR
36758+ AuTraceErr(err);
36759+ return err;
36760+}
36761+
36762+/* ---------------------------------------------------------------------- */
36763+
a2654f78
AM
36764+static int au_smack_reentering(struct super_block *sb)
36765+{
36766+#if IS_ENABLED(CONFIG_SECURITY_SMACK)
36767+ /*
36768+ * as a part of lookup, smack_d_instantiate() is called, and it calls
36769+ * i_op->getxattr(). ouch.
36770+ */
36771+ return si_pid_test(sb);
36772+#else
36773+ return 0;
36774+#endif
36775+}
36776+
c1595e42
JR
36777+enum {
36778+ AU_XATTR_LIST,
36779+ AU_XATTR_GET
36780+};
36781+
36782+struct au_lgxattr {
36783+ int type;
36784+ union {
36785+ struct {
36786+ char *list;
36787+ size_t size;
36788+ } list;
36789+ struct {
36790+ const char *name;
36791+ void *value;
36792+ size_t size;
36793+ } get;
36794+ } u;
36795+};
36796+
36797+static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
36798+{
36799+ ssize_t err;
a2654f78 36800+ int reenter;
c1595e42
JR
36801+ struct path h_path;
36802+ struct super_block *sb;
36803+
36804+ sb = dentry->d_sb;
a2654f78
AM
36805+ reenter = au_smack_reentering(sb);
36806+ if (!reenter) {
36807+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36808+ if (unlikely(err))
36809+ goto out;
36810+ }
36811+ err = au_h_path_getattr(dentry, /*force*/1, &h_path, reenter);
c1595e42
JR
36812+ if (unlikely(err))
36813+ goto out_si;
36814+ if (unlikely(!h_path.dentry))
36815+ /* illegally overlapped or something */
36816+ goto out_di; /* pretending success */
36817+
36818+ /* always topmost entry only */
36819+ switch (arg->type) {
36820+ case AU_XATTR_LIST:
36821+ err = vfs_listxattr(h_path.dentry,
36822+ arg->u.list.list, arg->u.list.size);
36823+ break;
36824+ case AU_XATTR_GET:
5afbbe0d 36825+ AuDebugOn(d_is_negative(h_path.dentry));
c1595e42
JR
36826+ err = vfs_getxattr(h_path.dentry,
36827+ arg->u.get.name, arg->u.get.value,
36828+ arg->u.get.size);
36829+ break;
36830+ }
36831+
36832+out_di:
a2654f78
AM
36833+ if (!reenter)
36834+ di_read_unlock(dentry, AuLock_IR);
c1595e42 36835+out_si:
a2654f78
AM
36836+ if (!reenter)
36837+ si_read_unlock(sb);
c1595e42
JR
36838+out:
36839+ AuTraceErr(err);
36840+ return err;
36841+}
36842+
36843+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36844+{
36845+ struct au_lgxattr arg = {
36846+ .type = AU_XATTR_LIST,
36847+ .u.list = {
36848+ .list = list,
36849+ .size = size
36850+ },
36851+ };
36852+
36853+ return au_lgxattr(dentry, &arg);
36854+}
36855+
f2c43d5f
AM
36856+static ssize_t au_getxattr(struct dentry *dentry,
36857+ struct inode *inode __maybe_unused,
36858+ const char *name, void *value, size_t size)
c1595e42
JR
36859+{
36860+ struct au_lgxattr arg = {
36861+ .type = AU_XATTR_GET,
36862+ .u.get = {
36863+ .name = name,
36864+ .value = value,
36865+ .size = size
36866+ },
36867+ };
36868+
36869+ return au_lgxattr(dentry, &arg);
36870+}
36871+
f2c43d5f
AM
36872+static int au_setxattr(struct dentry *dentry, struct inode *inode,
36873+ const char *name, const void *value, size_t size,
36874+ int flags)
c1595e42 36875+{
f2c43d5f 36876+ struct au_sxattr arg = {
c1595e42
JR
36877+ .type = AU_XATTR_SET,
36878+ .u.set = {
36879+ .name = name,
36880+ .value = value,
36881+ .size = size,
36882+ .flags = flags
36883+ },
36884+ };
36885+
f2c43d5f 36886+ return au_sxattr(dentry, inode, &arg);
c1595e42
JR
36887+}
36888+
36889+/* ---------------------------------------------------------------------- */
36890+
f2c43d5f
AM
36891+static int au_xattr_get(const struct xattr_handler *handler,
36892+ struct dentry *dentry, struct inode *inode,
36893+ const char *name, void *buffer, size_t size)
c1595e42 36894+{
f2c43d5f 36895+ return au_getxattr(dentry, inode, name, buffer, size);
c1595e42
JR
36896+}
36897+
f2c43d5f
AM
36898+static int au_xattr_set(const struct xattr_handler *handler,
36899+ struct dentry *dentry, struct inode *inode,
36900+ const char *name, const void *value, size_t size,
36901+ int flags)
c1595e42 36902+{
f2c43d5f 36903+ return au_setxattr(dentry, inode, name, value, size, flags);
c1595e42
JR
36904+}
36905+
36906+static const struct xattr_handler au_xattr_handler = {
f2c43d5f
AM
36907+ .name = "",
36908+ .prefix = "",
c1595e42
JR
36909+ .get = au_xattr_get,
36910+ .set = au_xattr_set
c1595e42
JR
36911+};
36912+
36913+static const struct xattr_handler *au_xattr_handlers[] = {
a2654f78
AM
36914+#ifdef CONFIG_FS_POSIX_ACL
36915+ &posix_acl_access_xattr_handler,
36916+ &posix_acl_default_xattr_handler,
36917+#endif
36918+ &au_xattr_handler, /* must be last */
f2c43d5f 36919+ NULL
c1595e42
JR
36920+};
36921+
36922+void au_xattr_init(struct super_block *sb)
36923+{
f2c43d5f 36924+ sb->s_xattr = au_xattr_handlers;
c1595e42 36925+}
7f207e10
AM
36926diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
36927--- /usr/share/empty/fs/aufs/xino.c 1970-01-01 01:00:00.000000000 +0100
9f237c51
AM
36928+++ linux/fs/aufs/xino.c 2018-12-27 13:19:17.715082917 +0100
36929@@ -0,0 +1,1956 @@
cd7a4cd9 36930+// SPDX-License-Identifier: GPL-2.0
1facf9fc 36931+/*
b00004a5 36932+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 36933+ *
36934+ * This program, aufs is free software; you can redistribute it and/or modify
36935+ * it under the terms of the GNU General Public License as published by
36936+ * the Free Software Foundation; either version 2 of the License, or
36937+ * (at your option) any later version.
dece6358
AM
36938+ *
36939+ * This program is distributed in the hope that it will be useful,
36940+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36941+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36942+ * GNU General Public License for more details.
36943+ *
36944+ * You should have received a copy of the GNU General Public License
523b37e3 36945+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36946+ */
36947+
36948+/*
36949+ * external inode number translation table and bitmap
062440b3
AM
36950+ *
36951+ * things to consider
36952+ * - the lifetime
36953+ * + au_xino object
36954+ * + XINO files (xino, xib, xigen)
36955+ * + dynamic debugfs entries (xiN)
36956+ * + static debugfs entries (xib, xigen)
36957+ * + static sysfs entry (xi_path)
36958+ * - several entry points to handle them.
36959+ * + mount(2) without xino option (default)
36960+ * + mount(2) with xino option
36961+ * + mount(2) with noxino option
36962+ * + umount(2)
36963+ * + remount with add/del branches
36964+ * + remount with xino/noxino options
1facf9fc 36965+ */
36966+
36967+#include <linux/seq_file.h>
392086de 36968+#include <linux/statfs.h>
1facf9fc 36969+#include "aufs.h"
36970+
062440b3
AM
36971+static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
36972+ aufs_bindex_t bbot,
36973+ struct super_block *h_sb)
1facf9fc 36974+{
062440b3
AM
36975+ /* todo: try binary-search if the branches are many */
36976+ for (; btop <= bbot; btop++)
36977+ if (h_sb == au_sbr_sb(sb, btop))
36978+ return btop;
36979+ return -1;
be52b249
AM
36980+}
36981+
062440b3
AM
36982+/*
36983+ * find another branch who is on the same filesystem of the specified
36984+ * branch{@btgt}. search until @bbot.
36985+ */
36986+static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
36987+ aufs_bindex_t bbot)
1facf9fc 36988+{
062440b3
AM
36989+ aufs_bindex_t bindex;
36990+ struct super_block *tgt_sb;
1facf9fc 36991+
062440b3
AM
36992+ tgt_sb = au_sbr_sb(sb, btgt);
36993+ bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
36994+ if (bindex < 0)
36995+ bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
1facf9fc 36996+
062440b3 36997+ return bindex;
1facf9fc 36998+}
36999+
37000+/* ---------------------------------------------------------------------- */
37001+
37002+/*
062440b3 37003+ * stop unnecessary notify events at creating xino files
1facf9fc 37004+ */
acd2b654
AM
37005+
37006+aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
37007+{
37008+ aufs_bindex_t bfound, bindex, bbot;
37009+ struct dentry *parent;
37010+ struct au_branch *br;
37011+
37012+ bfound = -1;
37013+ parent = dentry->d_parent; /* safe d_parent access */
37014+ bbot = au_sbbot(sb);
37015+ for (bindex = 0; bindex <= bbot; bindex++) {
37016+ br = au_sbr(sb, bindex);
37017+ if (au_br_dentry(br) == parent) {
37018+ bfound = bindex;
37019+ break;
37020+ }
37021+ }
37022+
37023+ AuDbg("bfound b%d\n", bfound);
37024+ return bfound;
37025+}
37026+
062440b3
AM
37027+struct au_xino_lock_dir {
37028+ struct au_hinode *hdir;
37029+ struct dentry *parent;
37030+ struct inode *dir;
37031+};
37032+
37033+static struct dentry *au_dget_parent_lock(struct dentry *dentry,
37034+ unsigned int lsc)
1facf9fc 37035+{
062440b3
AM
37036+ struct dentry *parent;
37037+ struct inode *dir;
1facf9fc 37038+
062440b3 37039+ parent = dget_parent(dentry);
5527c038 37040+ dir = d_inode(parent);
062440b3
AM
37041+ inode_lock_nested(dir, lsc);
37042+#if 0 /* it should not happen */
37043+ spin_lock(&dentry->d_lock);
37044+ if (unlikely(dentry->d_parent != parent)) {
37045+ spin_unlock(&dentry->d_lock);
37046+ inode_unlock(dir);
37047+ dput(parent);
37048+ parent = NULL;
1facf9fc 37049+ goto out;
37050+ }
062440b3 37051+ spin_unlock(&dentry->d_lock);
1facf9fc 37052+
4f0767ce 37053+out:
062440b3
AM
37054+#endif
37055+ return parent;
1facf9fc 37056+}
37057+
062440b3 37058+static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
1facf9fc 37059+ struct au_xino_lock_dir *ldir)
37060+{
acd2b654 37061+ aufs_bindex_t bindex;
1facf9fc 37062+
37063+ ldir->hdir = NULL;
acd2b654 37064+ bindex = au_xi_root(sb, xipath->dentry);
1facf9fc 37065+ if (bindex >= 0) {
062440b3 37066+ /* rw branch root */
5527c038 37067+ ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 37068+ au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
1facf9fc 37069+ } else {
062440b3
AM
37070+ /* other */
37071+ ldir->parent = au_dget_parent_lock(xipath->dentry,
37072+ AuLsc_I_PARENT);
febd17d6 37073+ ldir->dir = d_inode(ldir->parent);
1facf9fc 37074+ }
37075+}
37076+
37077+static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
37078+{
37079+ if (ldir->hdir)
5afbbe0d 37080+ au_hn_inode_unlock(ldir->hdir);
1facf9fc 37081+ else {
febd17d6 37082+ inode_unlock(ldir->dir);
1facf9fc 37083+ dput(ldir->parent);
37084+ }
37085+}
37086+
37087+/* ---------------------------------------------------------------------- */
37088+
062440b3
AM
37089+/*
37090+ * create and set a new xino file
37091+ */
37092+struct file *au_xino_create(struct super_block *sb, char *fpath, int silent)
37093+{
37094+ struct file *file;
37095+ struct dentry *h_parent, *d;
37096+ struct inode *h_dir, *inode;
37097+ int err;
37098+
37099+ /*
37100+ * at mount-time, and the xino file is the default path,
37101+ * hnotify is disabled so we have no notify events to ignore.
37102+ * when a user specified the xino, we cannot get au_hdir to be ignored.
37103+ */
37104+ file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37105+ /* | __FMODE_NONOTIFY */,
cd7a4cd9 37106+ 0666);
062440b3
AM
37107+ if (IS_ERR(file)) {
37108+ if (!silent)
37109+ pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
37110+ return file;
37111+ }
37112+
37113+ /* keep file count */
37114+ err = 0;
37115+ d = file->f_path.dentry;
37116+ h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
37117+ /* mnt_want_write() is unnecessary here */
37118+ h_dir = d_inode(h_parent);
37119+ inode = file_inode(file);
37120+ /* no delegation since it is just created */
37121+ if (inode->i_nlink)
37122+ err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
37123+ /*force*/0);
37124+ inode_unlock(h_dir);
37125+ dput(h_parent);
37126+ if (unlikely(err)) {
37127+ if (!silent)
37128+ pr_err("unlink %s(%d)\n", fpath, err);
37129+ goto out;
37130+ }
37131+
37132+ err = -EINVAL;
37133+ if (unlikely(sb == d->d_sb)) {
37134+ if (!silent)
37135+ pr_err("%s must be outside\n", fpath);
37136+ goto out;
37137+ }
37138+ if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
37139+ if (!silent)
37140+ pr_err("xino doesn't support %s(%s)\n",
37141+ fpath, au_sbtype(d->d_sb));
37142+ goto out;
37143+ }
37144+ return file; /* success */
37145+
37146+out:
37147+ fput(file);
37148+ file = ERR_PTR(err);
37149+ return file;
37150+}
37151+
37152+/*
37153+ * create a new xinofile at the same place/path as @base.
37154+ */
37155+struct file *au_xino_create2(struct super_block *sb, struct path *base,
37156+ struct file *copy_src)
37157+{
37158+ struct file *file;
37159+ struct dentry *dentry, *parent;
37160+ struct inode *dir, *delegated;
37161+ struct qstr *name;
37162+ struct path path;
37163+ int err, do_unlock;
37164+ struct au_xino_lock_dir ldir;
37165+
37166+ do_unlock = 1;
37167+ au_xino_lock_dir(sb, base, &ldir);
37168+ dentry = base->dentry;
37169+ parent = dentry->d_parent; /* dir inode is locked */
37170+ dir = d_inode(parent);
37171+ IMustLock(dir);
37172+
37173+ name = &dentry->d_name;
37174+ path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
37175+ if (IS_ERR(path.dentry)) {
37176+ file = (void *)path.dentry;
37177+ pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37178+ goto out;
37179+ }
37180+
37181+ /* no need to mnt_want_write() since we call dentry_open() later */
cd7a4cd9 37182+ err = vfs_create(dir, path.dentry, 0666, NULL);
062440b3
AM
37183+ if (unlikely(err)) {
37184+ file = ERR_PTR(err);
37185+ pr_err("%pd create err %d\n", dentry, err);
37186+ goto out_dput;
37187+ }
37188+
37189+ path.mnt = base->mnt;
37190+ file = vfsub_dentry_open(&path,
37191+ O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37192+ /* | __FMODE_NONOTIFY */);
37193+ if (IS_ERR(file)) {
37194+ pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37195+ goto out_dput;
37196+ }
37197+
37198+ delegated = NULL;
37199+ err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37200+ au_xino_unlock_dir(&ldir);
37201+ do_unlock = 0;
37202+ if (unlikely(err == -EWOULDBLOCK)) {
37203+ pr_warn("cannot retry for NFSv4 delegation"
37204+ " for an internal unlink\n");
37205+ iput(delegated);
37206+ }
37207+ if (unlikely(err)) {
37208+ pr_err("%pd unlink err %d\n", dentry, err);
37209+ goto out_fput;
37210+ }
37211+
37212+ if (copy_src) {
37213+ /* no one can touch copy_src xino */
37214+ err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37215+ if (unlikely(err)) {
37216+ pr_err("%pd copy err %d\n", dentry, err);
37217+ goto out_fput;
37218+ }
37219+ }
37220+ goto out_dput; /* success */
1facf9fc 37221+
062440b3
AM
37222+out_fput:
37223+ fput(file);
37224+ file = ERR_PTR(err);
37225+out_dput:
37226+ dput(path.dentry);
37227+out:
37228+ if (do_unlock)
37229+ au_xino_unlock_dir(&ldir);
37230+ return file;
37231+}
37232+
acd2b654
AM
37233+struct file *au_xino_file1(struct au_xino *xi)
37234+{
37235+ struct file *file;
37236+ unsigned int u, nfile;
37237+
37238+ file = NULL;
37239+ nfile = xi->xi_nfile;
37240+ for (u = 0; u < nfile; u++) {
37241+ file = xi->xi_file[u];
37242+ if (file)
37243+ break;
37244+ }
37245+
37246+ return file;
37247+}
37248+
37249+static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37250+{
37251+ int err;
37252+ struct file *f;
37253+ void *p;
37254+
37255+ if (file)
37256+ get_file(file);
37257+
37258+ err = 0;
37259+ f = NULL;
37260+ if (idx < xi->xi_nfile) {
37261+ f = xi->xi_file[idx];
37262+ if (f)
37263+ fput(f);
37264+ } else {
37265+ p = au_kzrealloc(xi->xi_file,
37266+ sizeof(*xi->xi_file) * xi->xi_nfile,
37267+ sizeof(*xi->xi_file) * (idx + 1),
37268+ GFP_NOFS, /*may_shrink*/0);
37269+ if (p) {
37270+ MtxMustLock(&xi->xi_mtx);
37271+ xi->xi_file = p;
37272+ xi->xi_nfile = idx + 1;
37273+ } else {
37274+ err = -ENOMEM;
37275+ if (file)
37276+ fput(file);
37277+ goto out;
37278+ }
37279+ }
37280+ xi->xi_file[idx] = file;
37281+
37282+out:
37283+ return err;
37284+}
37285+
37286+/*
37287+ * if @xinew->xi is not set, then create new xigen file.
37288+ */
37289+struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37290+{
37291+ struct file *file;
37292+ int err;
37293+
37294+ SiMustAnyLock(sb);
37295+
37296+ file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37297+ if (IS_ERR(file)) {
37298+ err = PTR_ERR(file);
37299+ pr_err("%s[%d], err %d\n",
37300+ xinew->xi ? "xino" : "xigen",
37301+ xinew->idx, err);
37302+ goto out;
37303+ }
37304+
37305+ if (xinew->xi)
37306+ err = au_xino_file_set(xinew->xi, xinew->idx, file);
37307+ else {
37308+ BUG();
37309+ /* todo: make xigen file an array */
37310+ /* err = au_xigen_file_set(sb, xinew->idx, file); */
37311+ }
37312+ fput(file);
37313+ if (unlikely(err))
37314+ file = ERR_PTR(err);
37315+
37316+out:
37317+ return file;
37318+}
37319+
062440b3
AM
37320+/* ---------------------------------------------------------------------- */
37321+
37322+/*
37323+ * truncate xino files
37324+ */
acd2b654
AM
37325+static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37326+ int idx, struct kstatfs *st)
1facf9fc 37327+{
37328+ int err;
392086de 37329+ blkcnt_t blocks;
acd2b654
AM
37330+ struct file *file, *new_xino;
37331+ struct au_xi_new xinew = {
37332+ .idx = idx
37333+ };
37334+
37335+ err = 0;
37336+ xinew.xi = au_sbr(sb, bindex)->br_xino;
37337+ file = au_xino_file(xinew.xi, idx);
37338+ if (!file)
37339+ goto out;
37340+
37341+ xinew.base = &file->f_path;
37342+ err = vfs_statfs(xinew.base, st);
37343+ if (unlikely(err)) {
37344+ AuErr1("statfs err %d, ignored\n", err);
37345+ err = 0;
37346+ goto out;
37347+ }
37348+
37349+ blocks = file_inode(file)->i_blocks;
37350+ pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37351+ bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37352+
37353+ xinew.copy_src = file;
37354+ new_xino = au_xi_new(sb, &xinew);
37355+ if (IS_ERR(new_xino)) {
37356+ err = PTR_ERR(new_xino);
37357+ pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37358+ goto out;
37359+ }
37360+
37361+ err = vfs_statfs(&new_xino->f_path, st);
37362+ if (!err)
37363+ pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37364+ bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37365+ st->f_bfree, st->f_blocks);
37366+ else {
37367+ AuErr1("statfs err %d, ignored\n", err);
37368+ err = 0;
37369+ }
37370+
37371+out:
37372+ return err;
37373+}
37374+
37375+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37376+{
37377+ int err, i;
37378+ unsigned long jiffy;
062440b3 37379+ aufs_bindex_t bbot;
392086de 37380+ struct kstatfs *st;
1facf9fc 37381+ struct au_branch *br;
acd2b654 37382+ struct au_xino *xi;
1facf9fc 37383+
392086de 37384+ err = -ENOMEM;
be52b249 37385+ st = kmalloc(sizeof(*st), GFP_NOFS);
392086de
AM
37386+ if (unlikely(!st))
37387+ goto out;
37388+
1facf9fc 37389+ err = -EINVAL;
5afbbe0d
AM
37390+ bbot = au_sbbot(sb);
37391+ if (unlikely(bindex < 0 || bbot < bindex))
392086de 37392+ goto out_st;
392086de 37393+
1facf9fc 37394+ err = 0;
acd2b654
AM
37395+ jiffy = jiffies;
37396+ br = au_sbr(sb, bindex);
37397+ xi = br->br_xino;
37398+ for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37399+ err = au_xino_do_trunc(sb, bindex, i, st);
37400+ if (!err)
37401+ au_sbi(sb)->si_xino_jiffy = jiffy;
392086de
AM
37402+
37403+out_st:
9f237c51 37404+ au_kfree_rcu(st);
4f0767ce 37405+out:
1facf9fc 37406+ return err;
37407+}
37408+
37409+struct xino_do_trunc_args {
37410+ struct super_block *sb;
37411+ struct au_branch *br;
acd2b654 37412+ int idx;
1facf9fc 37413+};
37414+
37415+static void xino_do_trunc(void *_args)
37416+{
37417+ struct xino_do_trunc_args *args = _args;
37418+ struct super_block *sb;
37419+ struct au_branch *br;
37420+ struct inode *dir;
acd2b654 37421+ int err, idx;
1facf9fc 37422+ aufs_bindex_t bindex;
37423+
37424+ err = 0;
37425+ sb = args->sb;
5527c038 37426+ dir = d_inode(sb->s_root);
1facf9fc 37427+ br = args->br;
acd2b654 37428+ idx = args->idx;
1facf9fc 37429+
37430+ si_noflush_write_lock(sb);
37431+ ii_read_lock_parent(dir);
37432+ bindex = au_br_index(sb, br->br_id);
acd2b654 37433+ err = au_xino_trunc(sb, bindex, idx);
1facf9fc 37434+ ii_read_unlock(dir);
37435+ if (unlikely(err))
392086de 37436+ pr_warn("err b%d, (%d)\n", bindex, err);
062440b3 37437+ atomic_dec(&br->br_xino->xi_truncating);
acd2b654 37438+ au_lcnt_dec(&br->br_count);
1facf9fc 37439+ si_write_unlock(sb);
027c5e7a 37440+ au_nwt_done(&au_sbi(sb)->si_nowait);
9f237c51 37441+ au_kfree_rcu(args);
1facf9fc 37442+}
37443+
acd2b654
AM
37444+/*
37445+ * returns the index in the xi_file array whose corresponding file is necessary
37446+ * to truncate, or -1 which means no need to truncate.
37447+ */
392086de
AM
37448+static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37449+{
37450+ int err;
acd2b654 37451+ unsigned int u;
392086de
AM
37452+ struct kstatfs st;
37453+ struct au_sbinfo *sbinfo;
acd2b654 37454+ struct au_xino *xi;
062440b3 37455+ struct file *file;
392086de
AM
37456+
37457+ /* todo: si_xino_expire and the ratio should be customizable */
37458+ sbinfo = au_sbi(sb);
37459+ if (time_before(jiffies,
37460+ sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
acd2b654 37461+ return -1;
392086de
AM
37462+
37463+ /* truncation border */
acd2b654
AM
37464+ xi = br->br_xino;
37465+ for (u = 0; u < xi->xi_nfile; u++) {
37466+ file = au_xino_file(xi, u);
37467+ if (!file)
37468+ continue;
37469+
37470+ err = vfs_statfs(&file->f_path, &st);
37471+ if (unlikely(err)) {
37472+ AuErr1("statfs err %d, ignored\n", err);
37473+ return -1;
37474+ }
37475+ if (div64_u64(st.f_bfree * 100, st.f_blocks)
37476+ >= AUFS_XINO_DEF_TRUNC)
37477+ return u;
392086de 37478+ }
392086de 37479+
acd2b654 37480+ return -1;
392086de
AM
37481+}
37482+
1facf9fc 37483+static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37484+{
acd2b654 37485+ int idx;
1facf9fc 37486+ struct xino_do_trunc_args *args;
37487+ int wkq_err;
37488+
acd2b654
AM
37489+ idx = xino_trunc_test(sb, br);
37490+ if (idx < 0)
1facf9fc 37491+ return;
37492+
062440b3 37493+ if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
1facf9fc 37494+ goto out;
37495+
37496+ /* lock and kfree() will be called in trunc_xino() */
37497+ args = kmalloc(sizeof(*args), GFP_NOFS);
37498+ if (unlikely(!args)) {
37499+ AuErr1("no memory\n");
f0c0a007 37500+ goto out;
1facf9fc 37501+ }
37502+
acd2b654 37503+ au_lcnt_inc(&br->br_count);
1facf9fc 37504+ args->sb = sb;
37505+ args->br = br;
acd2b654 37506+ args->idx = idx;
53392da6 37507+ wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
1facf9fc 37508+ if (!wkq_err)
37509+ return; /* success */
37510+
4a4d8108 37511+ pr_err("wkq %d\n", wkq_err);
acd2b654 37512+ au_lcnt_dec(&br->br_count);
9f237c51 37513+ au_kfree_rcu(args);
1facf9fc 37514+
4f0767ce 37515+out:
062440b3 37516+ atomic_dec(&br->br_xino->xi_truncating);
1facf9fc 37517+}
37518+
37519+/* ---------------------------------------------------------------------- */
37520+
acd2b654
AM
37521+struct au_xi_calc {
37522+ int idx;
37523+ loff_t pos;
37524+};
37525+
37526+static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37527+ struct au_xi_calc *calc)
37528+{
37529+ loff_t maxent;
37530+
37531+ maxent = au_xi_maxent(sb);
37532+ calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37533+ calc->pos *= sizeof(ino_t);
37534+}
37535+
37536+static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37537+ struct au_xi_calc *calc)
37538+{
37539+ int err;
37540+ struct file *file;
37541+ struct au_xino *xi = br->br_xino;
37542+ struct au_xi_new xinew = {
37543+ .xi = xi
37544+ };
37545+
37546+ SiMustAnyLock(sb);
37547+
37548+ err = 0;
37549+ if (!xi)
37550+ goto out;
37551+
37552+ mutex_lock(&xi->xi_mtx);
37553+ file = au_xino_file(xi, calc->idx);
37554+ if (file)
37555+ goto out_mtx;
37556+
37557+ file = au_xino_file(xi, /*idx*/-1);
37558+ AuDebugOn(!file);
37559+ xinew.idx = calc->idx;
37560+ xinew.base = &file->f_path;
37561+ /* xinew.copy_src = NULL; */
37562+ file = au_xi_new(sb, &xinew);
37563+ if (IS_ERR(file))
37564+ err = PTR_ERR(file);
37565+
37566+out_mtx:
37567+ mutex_unlock(&xi->xi_mtx);
37568+out:
37569+ return err;
37570+}
37571+
37572+struct au_xino_do_new_async_args {
37573+ struct super_block *sb;
37574+ struct au_branch *br;
37575+ struct au_xi_calc calc;
37576+ ino_t ino;
37577+};
37578+
9f237c51
AM
37579+struct au_xi_writing {
37580+ struct hlist_bl_node node;
37581+ ino_t h_ino, ino;
37582+};
37583+
acd2b654
AM
37584+static int au_xino_do_write(vfs_writef_t write, struct file *file,
37585+ struct au_xi_calc *calc, ino_t ino);
37586+
37587+static void au_xino_call_do_new_async(void *args)
37588+{
37589+ struct au_xino_do_new_async_args *a = args;
37590+ struct au_branch *br;
37591+ struct super_block *sb;
37592+ struct au_sbinfo *sbi;
37593+ struct inode *root;
37594+ struct file *file;
9f237c51
AM
37595+ struct au_xi_writing *del, *p;
37596+ struct hlist_bl_head *hbl;
37597+ struct hlist_bl_node *pos;
acd2b654
AM
37598+ int err;
37599+
37600+ br = a->br;
37601+ sb = a->sb;
37602+ sbi = au_sbi(sb);
37603+ si_noflush_read_lock(sb);
37604+ root = d_inode(sb->s_root);
37605+ ii_read_lock_child(root);
37606+ err = au_xino_do_new_async(sb, br, &a->calc);
9f237c51
AM
37607+ if (unlikely(err)) {
37608+ AuIOErr("err %d\n", err);
37609+ goto out;
37610+ }
37611+
37612+ file = au_xino_file(br->br_xino, a->calc.idx);
37613+ AuDebugOn(!file);
37614+ err = au_xino_do_write(sbi->si_xwrite, file, &a->calc, a->ino);
37615+ if (unlikely(err)) {
acd2b654 37616+ AuIOErr("err %d\n", err);
9f237c51
AM
37617+ goto out;
37618+ }
37619+
37620+ del = NULL;
37621+ hbl = &br->br_xino->xi_writing;
37622+ hlist_bl_lock(hbl);
37623+ au_hbl_for_each(pos, hbl) {
37624+ p = container_of(pos, struct au_xi_writing, node);
37625+ if (p->ino == a->ino) {
37626+ del = p;
37627+ hlist_bl_del(&p->node);
37628+ break;
37629+ }
37630+ }
37631+ hlist_bl_unlock(hbl);
37632+ au_kfree_rcu(del);
37633+
37634+out:
acd2b654
AM
37635+ au_lcnt_dec(&br->br_count);
37636+ ii_read_unlock(root);
37637+ si_read_unlock(sb);
37638+ au_nwt_done(&sbi->si_nowait);
9f237c51 37639+ au_kfree_rcu(a);
acd2b654
AM
37640+}
37641+
37642+/*
37643+ * create a new xino file asynchronously
37644+ */
37645+static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37646+ struct au_xi_calc *calc, ino_t ino)
37647+{
37648+ int err;
37649+ struct au_xino_do_new_async_args *arg;
37650+
37651+ err = -ENOMEM;
37652+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
37653+ if (unlikely(!arg))
37654+ goto out;
37655+
37656+ arg->sb = sb;
37657+ arg->br = br;
37658+ arg->calc = *calc;
37659+ arg->ino = ino;
37660+ au_lcnt_inc(&br->br_count);
37661+ err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37662+ if (unlikely(err)) {
37663+ pr_err("wkq %d\n", err);
37664+ au_lcnt_dec(&br->br_count);
9f237c51 37665+ au_kfree_rcu(arg);
acd2b654
AM
37666+ }
37667+
37668+out:
37669+ return err;
37670+}
37671+
062440b3
AM
37672+/*
37673+ * read @ino from xinofile for the specified branch{@sb, @bindex}
37674+ * at the position of @h_ino.
37675+ */
37676+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37677+ ino_t *ino)
37678+{
37679+ int err;
37680+ ssize_t sz;
acd2b654 37681+ struct au_xi_calc calc;
062440b3 37682+ struct au_sbinfo *sbinfo;
acd2b654 37683+ struct file *file;
9f237c51
AM
37684+ struct au_xino *xi;
37685+ struct hlist_bl_head *hbl;
37686+ struct hlist_bl_node *pos;
37687+ struct au_xi_writing *p;
062440b3
AM
37688+
37689+ *ino = 0;
37690+ if (!au_opt_test(au_mntflags(sb), XINO))
37691+ return 0; /* no xino */
37692+
37693+ err = 0;
acd2b654 37694+ au_xi_calc(sb, h_ino, &calc);
9f237c51
AM
37695+ xi = au_sbr(sb, bindex)->br_xino;
37696+ file = au_xino_file(xi, calc.idx);
37697+ if (!file) {
37698+ hbl = &xi->xi_writing;
37699+ hlist_bl_lock(hbl);
37700+ au_hbl_for_each(pos, hbl) {
37701+ p = container_of(pos, struct au_xi_writing, node);
37702+ if (p->h_ino == h_ino) {
37703+ AuDbg("hi%llu, i%llu, found\n",
37704+ (u64)p->h_ino, (u64)p->ino);
37705+ *ino = p->ino;
37706+ break;
37707+ }
37708+ }
37709+ hlist_bl_unlock(hbl);
37710+ return 0;
37711+ } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37712+ return 0; /* no xino */
062440b3 37713+
acd2b654
AM
37714+ sbinfo = au_sbi(sb);
37715+ sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &calc.pos);
062440b3
AM
37716+ if (sz == sizeof(*ino))
37717+ return 0; /* success */
37718+
37719+ err = sz;
37720+ if (unlikely(sz >= 0)) {
37721+ err = -EIO;
37722+ AuIOErr("xino read error (%zd)\n", sz);
37723+ }
062440b3
AM
37724+ return err;
37725+}
37726+
5527c038 37727+static int au_xino_do_write(vfs_writef_t write, struct file *file,
acd2b654 37728+ struct au_xi_calc *calc, ino_t ino)
1facf9fc 37729+{
1facf9fc 37730+ ssize_t sz;
37731+
acd2b654 37732+ sz = xino_fwrite(write, file, &ino, sizeof(ino), &calc->pos);
1facf9fc 37733+ if (sz == sizeof(ino))
37734+ return 0; /* success */
37735+
37736+ AuIOErr("write failed (%zd)\n", sz);
37737+ return -EIO;
37738+}
37739+
37740+/*
37741+ * write @ino to the xinofile for the specified branch{@sb, @bindex}
37742+ * at the position of @h_ino.
37743+ * even if @ino is zero, it is written to the xinofile and means no entry.
37744+ * if the size of the xino file on a specific filesystem exceeds the watermark,
37745+ * try truncating it.
37746+ */
37747+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37748+ ino_t ino)
37749+{
37750+ int err;
37751+ unsigned int mnt_flags;
acd2b654
AM
37752+ struct au_xi_calc calc;
37753+ struct file *file;
1facf9fc 37754+ struct au_branch *br;
acd2b654 37755+ struct au_xino *xi;
9f237c51 37756+ struct au_xi_writing *p;
1facf9fc 37757+
dece6358 37758+ SiMustAnyLock(sb);
1facf9fc 37759+
37760+ mnt_flags = au_mntflags(sb);
37761+ if (!au_opt_test(mnt_flags, XINO))
37762+ return 0;
37763+
acd2b654 37764+ au_xi_calc(sb, h_ino, &calc);
1facf9fc 37765+ br = au_sbr(sb, bindex);
acd2b654
AM
37766+ xi = br->br_xino;
37767+ file = au_xino_file(xi, calc.idx);
37768+ if (!file) {
9f237c51
AM
37769+ /* store the inum pair into the list */
37770+ p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37771+ p->h_ino = h_ino;
37772+ p->ino = ino;
37773+ au_hbl_add(&p->node, &xi->xi_writing);
37774+
acd2b654
AM
37775+ /* create and write a new xino file asynchronously */
37776+ err = au_xino_new_async(sb, br, &calc, ino);
37777+ if (!err)
37778+ return 0; /* success */
37779+ goto out;
37780+ }
37781+
37782+ err = au_xino_do_write(au_sbi(sb)->si_xwrite, file, &calc, ino);
1facf9fc 37783+ if (!err) {
acd2b654 37784+ br = au_sbr(sb, bindex);
1facf9fc 37785+ if (au_opt_test(mnt_flags, TRUNC_XINO)
86dc4139 37786+ && au_test_fs_trunc_xino(au_br_sb(br)))
1facf9fc 37787+ xino_try_trunc(sb, br);
37788+ return 0; /* success */
37789+ }
37790+
acd2b654 37791+out:
1facf9fc 37792+ AuIOErr("write failed (%d)\n", err);
37793+ return -EIO;
37794+}
37795+
062440b3
AM
37796+static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37797+ size_t size, loff_t *pos);
1facf9fc 37798+
062440b3
AM
37799+/* todo: unnecessary to support mmap_sem since kernel-space? */
37800+ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
37801+ loff_t *pos)
37802+{
37803+ ssize_t err;
37804+ mm_segment_t oldfs;
37805+ union {
37806+ void *k;
37807+ char __user *u;
37808+ } buf;
37809+ int i;
37810+ const int prevent_endless = 10;
1facf9fc 37811+
062440b3
AM
37812+ i = 0;
37813+ buf.k = kbuf;
37814+ oldfs = get_fs();
37815+ set_fs(KERNEL_DS);
37816+ do {
37817+ err = func(file, buf.u, size, pos);
37818+ if (err == -EINTR
37819+ && !au_wkq_test()
37820+ && fatal_signal_pending(current)) {
37821+ set_fs(oldfs);
37822+ err = xino_fread_wkq(func, file, kbuf, size, pos);
37823+ BUG_ON(err == -EINTR);
37824+ oldfs = get_fs();
37825+ set_fs(KERNEL_DS);
37826+ }
37827+ } while (i++ < prevent_endless
37828+ && (err == -EAGAIN || err == -EINTR));
37829+ set_fs(oldfs);
37830+
37831+#if 0 /* reserved for future use */
37832+ if (err > 0)
37833+ fsnotify_access(file->f_path.dentry);
37834+#endif
37835+
37836+ return err;
37837+}
37838+
37839+struct xino_fread_args {
37840+ ssize_t *errp;
37841+ vfs_readf_t func;
37842+ struct file *file;
37843+ void *buf;
37844+ size_t size;
37845+ loff_t *pos;
37846+};
37847+
37848+static void call_xino_fread(void *args)
37849+{
37850+ struct xino_fread_args *a = args;
37851+ *a->errp = xino_fread(a->func, a->file, a->buf, a->size, a->pos);
37852+}
37853+
37854+static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37855+ size_t size, loff_t *pos)
37856+{
37857+ ssize_t err;
37858+ int wkq_err;
37859+ struct xino_fread_args args = {
37860+ .errp = &err,
37861+ .func = func,
37862+ .file = file,
37863+ .buf = buf,
37864+ .size = size,
37865+ .pos = pos
37866+ };
37867+
37868+ wkq_err = au_wkq_wait(call_xino_fread, &args);
37869+ if (unlikely(wkq_err))
37870+ err = wkq_err;
37871+
37872+ return err;
37873+}
37874+
37875+static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37876+ size_t size, loff_t *pos);
37877+
37878+static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
37879+ size_t size, loff_t *pos)
37880+{
37881+ ssize_t err;
37882+ mm_segment_t oldfs;
37883+ union {
37884+ void *k;
37885+ const char __user *u;
37886+ } buf;
37887+ int i;
37888+ const int prevent_endless = 10;
37889+
37890+ i = 0;
37891+ buf.k = kbuf;
37892+ oldfs = get_fs();
37893+ set_fs(KERNEL_DS);
37894+ do {
37895+ err = func(file, buf.u, size, pos);
37896+ if (err == -EINTR
37897+ && !au_wkq_test()
37898+ && fatal_signal_pending(current)) {
37899+ set_fs(oldfs);
37900+ err = xino_fwrite_wkq(func, file, kbuf, size, pos);
37901+ BUG_ON(err == -EINTR);
37902+ oldfs = get_fs();
37903+ set_fs(KERNEL_DS);
37904+ }
37905+ } while (i++ < prevent_endless
37906+ && (err == -EAGAIN || err == -EINTR));
37907+ set_fs(oldfs);
37908+
37909+#if 0 /* reserved for future use */
37910+ if (err > 0)
37911+ fsnotify_modify(file->f_path.dentry);
37912+#endif
37913+
37914+ return err;
37915+}
37916+
37917+struct do_xino_fwrite_args {
37918+ ssize_t *errp;
37919+ vfs_writef_t func;
37920+ struct file *file;
37921+ void *buf;
37922+ size_t size;
37923+ loff_t *pos;
37924+};
37925+
37926+static void call_do_xino_fwrite(void *args)
37927+{
37928+ struct do_xino_fwrite_args *a = args;
37929+ *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
37930+}
37931+
37932+static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37933+ size_t size, loff_t *pos)
37934+{
37935+ ssize_t err;
37936+ int wkq_err;
37937+ struct do_xino_fwrite_args args = {
37938+ .errp = &err,
37939+ .func = func,
37940+ .file = file,
37941+ .buf = buf,
37942+ .size = size,
37943+ .pos = pos
37944+ };
37945+
37946+ /*
37947+ * it breaks RLIMIT_FSIZE and normal user's limit,
37948+ * users should care about quota and real 'filesystem full.'
37949+ */
37950+ wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
37951+ if (unlikely(wkq_err))
37952+ err = wkq_err;
37953+
37954+ return err;
37955+}
37956+
37957+ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
37958+ size_t size, loff_t *pos)
37959+{
37960+ ssize_t err;
37961+
37962+ if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
37963+ lockdep_off();
37964+ err = do_xino_fwrite(func, file, buf, size, pos);
37965+ lockdep_on();
37966+ } else {
37967+ lockdep_off();
37968+ err = xino_fwrite_wkq(func, file, buf, size, pos);
37969+ lockdep_on();
37970+ }
37971+
37972+ return err;
37973+}
37974+
37975+/* ---------------------------------------------------------------------- */
37976+
37977+/*
37978+ * inode number bitmap
37979+ */
1facf9fc 37980+static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
37981+static ino_t xib_calc_ino(unsigned long pindex, int bit)
37982+{
37983+ ino_t ino;
37984+
37985+ AuDebugOn(bit < 0 || page_bits <= bit);
37986+ ino = AUFS_FIRST_INO + pindex * page_bits + bit;
37987+ return ino;
37988+}
37989+
37990+static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
37991+{
37992+ AuDebugOn(ino < AUFS_FIRST_INO);
37993+ ino -= AUFS_FIRST_INO;
37994+ *pindex = ino / page_bits;
37995+ *bit = ino % page_bits;
37996+}
37997+
37998+static int xib_pindex(struct super_block *sb, unsigned long pindex)
37999+{
38000+ int err;
38001+ loff_t pos;
38002+ ssize_t sz;
38003+ struct au_sbinfo *sbinfo;
38004+ struct file *xib;
38005+ unsigned long *p;
38006+
38007+ sbinfo = au_sbi(sb);
38008+ MtxMustLock(&sbinfo->si_xib_mtx);
38009+ AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
38010+ || !au_opt_test(sbinfo->si_mntflags, XINO));
38011+
38012+ if (pindex == sbinfo->si_xib_last_pindex)
38013+ return 0;
38014+
38015+ xib = sbinfo->si_xib;
38016+ p = sbinfo->si_xib_buf;
38017+ pos = sbinfo->si_xib_last_pindex;
38018+ pos *= PAGE_SIZE;
38019+ sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
38020+ if (unlikely(sz != PAGE_SIZE))
38021+ goto out;
38022+
38023+ pos = pindex;
38024+ pos *= PAGE_SIZE;
c06a8ce3 38025+ if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
1facf9fc 38026+ sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
38027+ else {
38028+ memset(p, 0, PAGE_SIZE);
38029+ sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
38030+ }
38031+ if (sz == PAGE_SIZE) {
38032+ sbinfo->si_xib_last_pindex = pindex;
38033+ return 0; /* success */
38034+ }
38035+
4f0767ce 38036+out:
b752ccd1
AM
38037+ AuIOErr1("write failed (%zd)\n", sz);
38038+ err = sz;
38039+ if (sz >= 0)
38040+ err = -EIO;
38041+ return err;
38042+}
38043+
b752ccd1
AM
38044+static void au_xib_clear_bit(struct inode *inode)
38045+{
38046+ int err, bit;
38047+ unsigned long pindex;
38048+ struct super_block *sb;
38049+ struct au_sbinfo *sbinfo;
38050+
38051+ AuDebugOn(inode->i_nlink);
38052+
38053+ sb = inode->i_sb;
38054+ xib_calc_bit(inode->i_ino, &pindex, &bit);
38055+ AuDebugOn(page_bits <= bit);
38056+ sbinfo = au_sbi(sb);
38057+ mutex_lock(&sbinfo->si_xib_mtx);
38058+ err = xib_pindex(sb, pindex);
38059+ if (!err) {
38060+ clear_bit(bit, sbinfo->si_xib_buf);
38061+ sbinfo->si_xib_next_bit = bit;
38062+ }
38063+ mutex_unlock(&sbinfo->si_xib_mtx);
38064+}
38065+
1facf9fc 38066+/* ---------------------------------------------------------------------- */
38067+
1facf9fc 38068+/*
062440b3 38069+ * truncate a xino bitmap file
1facf9fc 38070+ */
1facf9fc 38071+
38072+/* todo: slow */
38073+static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
38074+{
38075+ int err, bit;
38076+ ssize_t sz;
38077+ unsigned long pindex;
38078+ loff_t pos, pend;
38079+ struct au_sbinfo *sbinfo;
5527c038 38080+ vfs_readf_t func;
1facf9fc 38081+ ino_t *ino;
38082+ unsigned long *p;
38083+
38084+ err = 0;
38085+ sbinfo = au_sbi(sb);
dece6358 38086+ MtxMustLock(&sbinfo->si_xib_mtx);
1facf9fc 38087+ p = sbinfo->si_xib_buf;
38088+ func = sbinfo->si_xread;
c06a8ce3 38089+ pend = vfsub_f_size_read(file);
1facf9fc 38090+ pos = 0;
38091+ while (pos < pend) {
38092+ sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
38093+ err = sz;
38094+ if (unlikely(sz <= 0))
38095+ goto out;
38096+
38097+ err = 0;
38098+ for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
38099+ if (unlikely(*ino < AUFS_FIRST_INO))
38100+ continue;
38101+
38102+ xib_calc_bit(*ino, &pindex, &bit);
38103+ AuDebugOn(page_bits <= bit);
38104+ err = xib_pindex(sb, pindex);
38105+ if (!err)
38106+ set_bit(bit, p);
38107+ else
38108+ goto out;
38109+ }
38110+ }
38111+
4f0767ce 38112+out:
1facf9fc 38113+ return err;
38114+}
38115+
38116+static int xib_restore(struct super_block *sb)
38117+{
acd2b654
AM
38118+ int err, i;
38119+ unsigned int nfile;
5afbbe0d 38120+ aufs_bindex_t bindex, bbot;
1facf9fc 38121+ void *page;
062440b3 38122+ struct au_branch *br;
acd2b654
AM
38123+ struct au_xino *xi;
38124+ struct file *file;
1facf9fc 38125+
38126+ err = -ENOMEM;
38127+ page = (void *)__get_free_page(GFP_NOFS);
38128+ if (unlikely(!page))
38129+ goto out;
38130+
38131+ err = 0;
5afbbe0d
AM
38132+ bbot = au_sbbot(sb);
38133+ for (bindex = 0; !err && bindex <= bbot; bindex++)
062440b3
AM
38134+ if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
38135+ br = au_sbr(sb, bindex);
acd2b654
AM
38136+ xi = br->br_xino;
38137+ nfile = xi->xi_nfile;
38138+ for (i = 0; i < nfile; i++) {
38139+ file = au_xino_file(xi, i);
38140+ if (file)
38141+ err = do_xib_restore(sb, file, page);
38142+ }
062440b3
AM
38143+ } else
38144+ AuDbg("skip shared b%d\n", bindex);
1c60b727 38145+ free_page((unsigned long)page);
1facf9fc 38146+
4f0767ce 38147+out:
1facf9fc 38148+ return err;
38149+}
38150+
38151+int au_xib_trunc(struct super_block *sb)
38152+{
38153+ int err;
38154+ ssize_t sz;
38155+ loff_t pos;
1facf9fc 38156+ struct au_sbinfo *sbinfo;
38157+ unsigned long *p;
38158+ struct file *file;
38159+
dece6358
AM
38160+ SiMustWriteLock(sb);
38161+
1facf9fc 38162+ err = 0;
38163+ sbinfo = au_sbi(sb);
38164+ if (!au_opt_test(sbinfo->si_mntflags, XINO))
38165+ goto out;
38166+
38167+ file = sbinfo->si_xib;
c06a8ce3 38168+ if (vfsub_f_size_read(file) <= PAGE_SIZE)
1facf9fc 38169+ goto out;
38170+
062440b3 38171+ file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
1facf9fc 38172+ err = PTR_ERR(file);
38173+ if (IS_ERR(file))
38174+ goto out;
38175+ fput(sbinfo->si_xib);
38176+ sbinfo->si_xib = file;
38177+
38178+ p = sbinfo->si_xib_buf;
38179+ memset(p, 0, PAGE_SIZE);
38180+ pos = 0;
38181+ sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
38182+ if (unlikely(sz != PAGE_SIZE)) {
38183+ err = sz;
38184+ AuIOErr("err %d\n", err);
38185+ if (sz >= 0)
38186+ err = -EIO;
38187+ goto out;
38188+ }
38189+
38190+ mutex_lock(&sbinfo->si_xib_mtx);
38191+ /* mnt_want_write() is unnecessary here */
38192+ err = xib_restore(sb);
38193+ mutex_unlock(&sbinfo->si_xib_mtx);
38194+
38195+out:
38196+ return err;
38197+}
38198+
38199+/* ---------------------------------------------------------------------- */
38200+
acd2b654 38201+struct au_xino *au_xino_alloc(unsigned int nfile)
062440b3
AM
38202+{
38203+ struct au_xino *xi;
38204+
38205+ xi = kzalloc(sizeof(*xi), GFP_NOFS);
38206+ if (unlikely(!xi))
38207+ goto out;
acd2b654
AM
38208+ xi->xi_nfile = nfile;
38209+ xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38210+ if (unlikely(!xi->xi_file))
38211+ goto out_free;
062440b3
AM
38212+
38213+ xi->xi_nondir.total = 8; /* initial size */
38214+ xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38215+ GFP_NOFS);
38216+ if (unlikely(!xi->xi_nondir.array))
acd2b654 38217+ goto out_file;
062440b3
AM
38218+
38219+ spin_lock_init(&xi->xi_nondir.spin);
38220+ init_waitqueue_head(&xi->xi_nondir.wqh);
acd2b654 38221+ mutex_init(&xi->xi_mtx);
9f237c51 38222+ INIT_HLIST_BL_HEAD(&xi->xi_writing);
062440b3
AM
38223+ atomic_set(&xi->xi_truncating, 0);
38224+ kref_init(&xi->xi_kref);
38225+ goto out; /* success */
38226+
acd2b654 38227+out_file:
9f237c51 38228+ au_kfree_try_rcu(xi->xi_file);
062440b3 38229+out_free:
9f237c51 38230+ au_kfree_rcu(xi);
062440b3
AM
38231+ xi = NULL;
38232+out:
38233+ return xi;
38234+}
38235+
acd2b654 38236+static int au_xino_init(struct au_branch *br, int idx, struct file *file)
062440b3
AM
38237+{
38238+ int err;
38239+ struct au_xino *xi;
38240+
38241+ err = 0;
acd2b654 38242+ xi = au_xino_alloc(idx + 1);
062440b3
AM
38243+ if (unlikely(!xi)) {
38244+ err = -ENOMEM;
38245+ goto out;
38246+ }
38247+
acd2b654
AM
38248+ if (file)
38249+ get_file(file);
38250+ xi->xi_file[idx] = file;
062440b3
AM
38251+ AuDebugOn(br->br_xino);
38252+ br->br_xino = xi;
38253+
38254+out:
38255+ return err;
38256+}
38257+
38258+static void au_xino_release(struct kref *kref)
38259+{
38260+ struct au_xino *xi;
38261+ int i;
9f237c51
AM
38262+ unsigned long ul;
38263+ struct hlist_bl_head *hbl;
38264+ struct hlist_bl_node *pos, *n;
38265+ struct au_xi_writing *p;
062440b3
AM
38266+
38267+ xi = container_of(kref, struct au_xino, xi_kref);
acd2b654
AM
38268+ for (i = 0; i < xi->xi_nfile; i++)
38269+ if (xi->xi_file[i])
38270+ fput(xi->xi_file[i]);
062440b3
AM
38271+ for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38272+ AuDebugOn(xi->xi_nondir.array[i]);
acd2b654 38273+ mutex_destroy(&xi->xi_mtx);
9f237c51
AM
38274+ hbl = &xi->xi_writing;
38275+ ul = au_hbl_count(hbl);
38276+ if (unlikely(ul)) {
38277+ pr_warn("xi_writing %lu\n", ul);
38278+ hlist_bl_lock(hbl);
38279+ hlist_bl_for_each_entry_safe (p, pos, n, hbl, node) {
38280+ hlist_bl_del(&p->node);
38281+ au_kfree_rcu(p);
38282+ }
38283+ hlist_bl_unlock(hbl);
38284+ }
38285+ au_kfree_try_rcu(xi->xi_file);
38286+ au_kfree_try_rcu(xi->xi_nondir.array);
38287+ au_kfree_rcu(xi);
062440b3
AM
38288+}
38289+
38290+int au_xino_put(struct au_branch *br)
38291+{
38292+ int ret;
38293+ struct au_xino *xi;
38294+
38295+ ret = 0;
38296+ xi = br->br_xino;
38297+ if (xi) {
38298+ br->br_xino = NULL;
38299+ ret = kref_put(&xi->xi_kref, au_xino_release);
38300+ }
38301+
38302+ return ret;
38303+}
38304+
062440b3
AM
38305+/* ---------------------------------------------------------------------- */
38306+
1facf9fc 38307+/*
38308+ * xino mount option handlers
38309+ */
1facf9fc 38310+
38311+/* xino bitmap */
38312+static void xino_clear_xib(struct super_block *sb)
38313+{
38314+ struct au_sbinfo *sbinfo;
38315+
dece6358
AM
38316+ SiMustWriteLock(sb);
38317+
1facf9fc 38318+ sbinfo = au_sbi(sb);
062440b3 38319+ /* unnecessary to clear sbinfo->si_xread and ->si_xwrite */
1facf9fc 38320+ if (sbinfo->si_xib)
38321+ fput(sbinfo->si_xib);
38322+ sbinfo->si_xib = NULL;
f0c0a007 38323+ if (sbinfo->si_xib_buf)
1c60b727 38324+ free_page((unsigned long)sbinfo->si_xib_buf);
1facf9fc 38325+ sbinfo->si_xib_buf = NULL;
38326+}
38327+
062440b3 38328+static int au_xino_set_xib(struct super_block *sb, struct path *path)
1facf9fc 38329+{
38330+ int err;
38331+ loff_t pos;
38332+ struct au_sbinfo *sbinfo;
38333+ struct file *file;
acd2b654 38334+ struct super_block *xi_sb;
1facf9fc 38335+
dece6358
AM
38336+ SiMustWriteLock(sb);
38337+
1facf9fc 38338+ sbinfo = au_sbi(sb);
062440b3 38339+ file = au_xino_create2(sb, path, sbinfo->si_xib);
1facf9fc 38340+ err = PTR_ERR(file);
38341+ if (IS_ERR(file))
38342+ goto out;
38343+ if (sbinfo->si_xib)
38344+ fput(sbinfo->si_xib);
38345+ sbinfo->si_xib = file;
5527c038
JR
38346+ sbinfo->si_xread = vfs_readf(file);
38347+ sbinfo->si_xwrite = vfs_writef(file);
acd2b654
AM
38348+ xi_sb = file_inode(file)->i_sb;
38349+ sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38350+ if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38351+ err = -EIO;
38352+ pr_err("s_maxbytes(%llu) on %s is too small\n",
38353+ (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38354+ goto out_unset;
38355+ }
38356+ sbinfo->si_ximaxent /= sizeof(ino_t);
1facf9fc 38357+
38358+ err = -ENOMEM;
38359+ if (!sbinfo->si_xib_buf)
38360+ sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38361+ if (unlikely(!sbinfo->si_xib_buf))
38362+ goto out_unset;
38363+
38364+ sbinfo->si_xib_last_pindex = 0;
38365+ sbinfo->si_xib_next_bit = 0;
c06a8ce3 38366+ if (vfsub_f_size_read(file) < PAGE_SIZE) {
1facf9fc 38367+ pos = 0;
38368+ err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
38369+ PAGE_SIZE, &pos);
38370+ if (unlikely(err != PAGE_SIZE))
38371+ goto out_free;
38372+ }
38373+ err = 0;
38374+ goto out; /* success */
38375+
4f0767ce 38376+out_free:
f0c0a007 38377+ if (sbinfo->si_xib_buf)
1c60b727 38378+ free_page((unsigned long)sbinfo->si_xib_buf);
b752ccd1
AM
38379+ sbinfo->si_xib_buf = NULL;
38380+ if (err >= 0)
38381+ err = -EIO;
4f0767ce 38382+out_unset:
b752ccd1
AM
38383+ fput(sbinfo->si_xib);
38384+ sbinfo->si_xib = NULL;
4f0767ce 38385+out:
062440b3 38386+ AuTraceErr(err);
b752ccd1 38387+ return err;
1facf9fc 38388+}
38389+
b752ccd1
AM
38390+/* xino for each branch */
38391+static void xino_clear_br(struct super_block *sb)
38392+{
5afbbe0d 38393+ aufs_bindex_t bindex, bbot;
b752ccd1 38394+ struct au_branch *br;
1facf9fc 38395+
5afbbe0d
AM
38396+ bbot = au_sbbot(sb);
38397+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1 38398+ br = au_sbr(sb, bindex);
062440b3
AM
38399+ AuDebugOn(!br);
38400+ au_xino_put(br);
38401+ }
38402+}
38403+
38404+static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38405+ aufs_bindex_t bshared)
38406+{
38407+ struct au_branch *brshared;
b752ccd1 38408+
062440b3
AM
38409+ brshared = au_sbr(sb, bshared);
38410+ AuDebugOn(!brshared->br_xino);
38411+ AuDebugOn(!brshared->br_xino->xi_file);
38412+ if (br->br_xino != brshared->br_xino) {
38413+ au_xino_get(brshared);
38414+ au_xino_put(br);
38415+ br->br_xino = brshared->br_xino;
b752ccd1
AM
38416+ }
38417+}
38418+
062440b3
AM
38419+struct au_xino_do_set_br {
38420+ vfs_writef_t writef;
38421+ struct au_branch *br;
38422+ ino_t h_ino;
38423+ aufs_bindex_t bshared;
38424+};
38425+
38426+static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38427+ struct au_xino_do_set_br *args)
1facf9fc 38428+{
38429+ int err;
acd2b654 38430+ struct au_xi_calc calc;
062440b3 38431+ struct file *file;
acd2b654
AM
38432+ struct au_branch *br;
38433+ struct au_xi_new xinew = {
38434+ .base = path
38435+ };
062440b3 38436+
acd2b654
AM
38437+ br = args->br;
38438+ xinew.xi = br->br_xino;
38439+ au_xi_calc(sb, args->h_ino, &calc);
38440+ xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38441+ if (args->bshared >= 0)
062440b3 38442+ /* shared xino */
acd2b654
AM
38443+ au_xino_set_br_shared(sb, br, args->bshared);
38444+ else if (!xinew.xi) {
38445+ /* new xino */
38446+ err = au_xino_init(br, calc.idx, xinew.copy_src);
38447+ if (unlikely(err))
38448+ goto out;
062440b3
AM
38449+ }
38450+
acd2b654
AM
38451+ /* force re-creating */
38452+ xinew.xi = br->br_xino;
38453+ xinew.idx = calc.idx;
38454+ mutex_lock(&xinew.xi->xi_mtx);
38455+ file = au_xi_new(sb, &xinew);
38456+ mutex_unlock(&xinew.xi->xi_mtx);
062440b3
AM
38457+ err = PTR_ERR(file);
38458+ if (IS_ERR(file))
38459+ goto out;
acd2b654
AM
38460+ AuDebugOn(!file);
38461+
38462+ err = au_xino_do_write(args->writef, file, &calc, AUFS_ROOT_INO);
38463+ if (unlikely(err))
38464+ au_xino_put(br);
062440b3 38465+
062440b3
AM
38466+out:
38467+ AuTraceErr(err);
38468+ return err;
38469+}
38470+
38471+static int au_xino_set_br(struct super_block *sb, struct path *path)
38472+{
38473+ int err;
38474+ aufs_bindex_t bindex, bbot;
38475+ struct au_xino_do_set_br args;
b752ccd1 38476+ struct inode *inode;
1facf9fc 38477+
b752ccd1
AM
38478+ SiMustWriteLock(sb);
38479+
5afbbe0d 38480+ bbot = au_sbbot(sb);
5527c038 38481+ inode = d_inode(sb->s_root);
062440b3
AM
38482+ args.writef = au_sbi(sb)->si_xwrite;
38483+ for (bindex = 0; bindex <= bbot; bindex++) {
38484+ args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38485+ args.br = au_sbr(sb, bindex);
38486+ args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38487+ err = au_xino_do_set_br(sb, path, &args);
b752ccd1 38488+ if (unlikely(err))
062440b3 38489+ break;
b752ccd1 38490+ }
1facf9fc 38491+
062440b3 38492+ AuTraceErr(err);
1facf9fc 38493+ return err;
38494+}
b752ccd1
AM
38495+
38496+void au_xino_clr(struct super_block *sb)
38497+{
38498+ struct au_sbinfo *sbinfo;
38499+
38500+ au_xigen_clr(sb);
38501+ xino_clear_xib(sb);
38502+ xino_clear_br(sb);
062440b3 38503+ dbgaufs_brs_del(sb, 0);
b752ccd1
AM
38504+ sbinfo = au_sbi(sb);
38505+ /* lvalue, do not call au_mntflags() */
38506+ au_opt_clr(sbinfo->si_mntflags, XINO);
38507+}
38508+
062440b3 38509+int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
b752ccd1
AM
38510+{
38511+ int err, skip;
062440b3 38512+ struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
b752ccd1
AM
38513+ struct qstr *dname, *cur_name;
38514+ struct file *cur_xino;
b752ccd1 38515+ struct au_sbinfo *sbinfo;
062440b3 38516+ struct path *path, *cur_path;
b752ccd1
AM
38517+
38518+ SiMustWriteLock(sb);
38519+
38520+ err = 0;
38521+ sbinfo = au_sbi(sb);
062440b3
AM
38522+ path = &xiopt->file->f_path;
38523+ dentry = path->dentry;
38524+ parent = dget_parent(dentry);
b752ccd1
AM
38525+ if (remount) {
38526+ skip = 0;
b752ccd1
AM
38527+ cur_xino = sbinfo->si_xib;
38528+ if (cur_xino) {
062440b3
AM
38529+ cur_path = &cur_xino->f_path;
38530+ cur_dentry = cur_path->dentry;
38531+ cur_parent = dget_parent(cur_dentry);
38532+ cur_name = &cur_dentry->d_name;
38533+ dname = &dentry->d_name;
b752ccd1 38534+ skip = (cur_parent == parent
38d290e6 38535+ && au_qstreq(dname, cur_name));
b752ccd1
AM
38536+ dput(cur_parent);
38537+ }
38538+ if (skip)
38539+ goto out;
38540+ }
38541+
38542+ au_opt_set(sbinfo->si_mntflags, XINO);
062440b3
AM
38543+ err = au_xino_set_xib(sb, path);
38544+ /* si_x{read,write} are set */
b752ccd1 38545+ if (!err)
062440b3 38546+ err = au_xigen_set(sb, path);
b752ccd1 38547+ if (!err)
062440b3
AM
38548+ err = au_xino_set_br(sb, path);
38549+ if (!err) {
38550+ dbgaufs_brs_add(sb, 0, /*topdown*/1);
b752ccd1 38551+ goto out; /* success */
062440b3 38552+ }
b752ccd1
AM
38553+
38554+ /* reset all */
062440b3
AM
38555+ AuIOErr("failed setting xino(%d).\n", err);
38556+ au_xino_clr(sb);
b752ccd1 38557+
4f0767ce 38558+out:
b752ccd1
AM
38559+ dput(parent);
38560+ return err;
38561+}
38562+
b752ccd1
AM
38563+/*
38564+ * create a xinofile at the default place/path.
38565+ */
38566+struct file *au_xino_def(struct super_block *sb)
38567+{
38568+ struct file *file;
38569+ char *page, *p;
38570+ struct au_branch *br;
38571+ struct super_block *h_sb;
38572+ struct path path;
5afbbe0d 38573+ aufs_bindex_t bbot, bindex, bwr;
b752ccd1
AM
38574+
38575+ br = NULL;
5afbbe0d 38576+ bbot = au_sbbot(sb);
b752ccd1 38577+ bwr = -1;
5afbbe0d 38578+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1
AM
38579+ br = au_sbr(sb, bindex);
38580+ if (au_br_writable(br->br_perm)
86dc4139 38581+ && !au_test_fs_bad_xino(au_br_sb(br))) {
b752ccd1
AM
38582+ bwr = bindex;
38583+ break;
38584+ }
38585+ }
38586+
7f207e10
AM
38587+ if (bwr >= 0) {
38588+ file = ERR_PTR(-ENOMEM);
537831f9 38589+ page = (void *)__get_free_page(GFP_NOFS);
7f207e10
AM
38590+ if (unlikely(!page))
38591+ goto out;
86dc4139 38592+ path.mnt = au_br_mnt(br);
7f207e10
AM
38593+ path.dentry = au_h_dptr(sb->s_root, bwr);
38594+ p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38595+ file = (void *)p;
38596+ if (!IS_ERR(p)) {
38597+ strcat(p, "/" AUFS_XINO_FNAME);
38598+ AuDbg("%s\n", p);
38599+ file = au_xino_create(sb, p, /*silent*/0);
7f207e10 38600+ }
1c60b727 38601+ free_page((unsigned long)page);
7f207e10
AM
38602+ } else {
38603+ file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
38604+ if (IS_ERR(file))
38605+ goto out;
2000de60 38606+ h_sb = file->f_path.dentry->d_sb;
7f207e10
AM
38607+ if (unlikely(au_test_fs_bad_xino(h_sb))) {
38608+ pr_err("xino doesn't support %s(%s)\n",
38609+ AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38610+ fput(file);
38611+ file = ERR_PTR(-EINVAL);
38612+ }
7f207e10 38613+ }
0c5527e5 38614+
7f207e10
AM
38615+out:
38616+ return file;
38617+}
38618+
38619+/* ---------------------------------------------------------------------- */
38620+
062440b3
AM
38621+/*
38622+ * initialize the xinofile for the specified branch @br
38623+ * at the place/path where @base_file indicates.
38624+ * test whether another branch is on the same filesystem or not,
38625+ * if found then share the xinofile with another branch.
38626+ */
38627+int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38628+ struct path *base)
7f207e10
AM
38629+{
38630+ int err;
062440b3
AM
38631+ struct au_xino_do_set_br args = {
38632+ .h_ino = h_ino,
38633+ .br = br
38634+ };
7f207e10 38635+
062440b3
AM
38636+ args.writef = au_sbi(sb)->si_xwrite;
38637+ args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38638+ au_br_sb(br));
38639+ err = au_xino_do_set_br(sb, base, &args);
79b8bda9 38640+ if (unlikely(err))
062440b3 38641+ au_xino_put(br);
7f207e10 38642+
7f207e10
AM
38643+ return err;
38644+}
521ced18
JR
38645+
38646+/* ---------------------------------------------------------------------- */
38647+
062440b3
AM
38648+/*
38649+ * get an unused inode number from bitmap
38650+ */
38651+ino_t au_xino_new_ino(struct super_block *sb)
38652+{
38653+ ino_t ino;
38654+ unsigned long *p, pindex, ul, pend;
38655+ struct au_sbinfo *sbinfo;
38656+ struct file *file;
38657+ int free_bit, err;
38658+
38659+ if (!au_opt_test(au_mntflags(sb), XINO))
38660+ return iunique(sb, AUFS_FIRST_INO);
38661+
38662+ sbinfo = au_sbi(sb);
38663+ mutex_lock(&sbinfo->si_xib_mtx);
38664+ p = sbinfo->si_xib_buf;
38665+ free_bit = sbinfo->si_xib_next_bit;
38666+ if (free_bit < page_bits && !test_bit(free_bit, p))
38667+ goto out; /* success */
38668+ free_bit = find_first_zero_bit(p, page_bits);
38669+ if (free_bit < page_bits)
38670+ goto out; /* success */
38671+
38672+ pindex = sbinfo->si_xib_last_pindex;
38673+ for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38674+ err = xib_pindex(sb, ul);
38675+ if (unlikely(err))
38676+ goto out_err;
38677+ free_bit = find_first_zero_bit(p, page_bits);
38678+ if (free_bit < page_bits)
38679+ goto out; /* success */
38680+ }
38681+
38682+ file = sbinfo->si_xib;
38683+ pend = vfsub_f_size_read(file) / PAGE_SIZE;
38684+ for (ul = pindex + 1; ul <= pend; ul++) {
38685+ err = xib_pindex(sb, ul);
38686+ if (unlikely(err))
38687+ goto out_err;
38688+ free_bit = find_first_zero_bit(p, page_bits);
38689+ if (free_bit < page_bits)
38690+ goto out; /* success */
38691+ }
38692+ BUG();
38693+
38694+out:
38695+ set_bit(free_bit, p);
38696+ sbinfo->si_xib_next_bit = free_bit + 1;
38697+ pindex = sbinfo->si_xib_last_pindex;
38698+ mutex_unlock(&sbinfo->si_xib_mtx);
38699+ ino = xib_calc_ino(pindex, free_bit);
38700+ AuDbg("i%lu\n", (unsigned long)ino);
38701+ return ino;
38702+out_err:
38703+ mutex_unlock(&sbinfo->si_xib_mtx);
38704+ AuDbg("i0\n");
38705+ return 0;
38706+}
38707+
38708+/* for s_op->delete_inode() */
38709+void au_xino_delete_inode(struct inode *inode, const int unlinked)
521ced18 38710+{
062440b3
AM
38711+ int err;
38712+ unsigned int mnt_flags;
38713+ aufs_bindex_t bindex, bbot, bi;
38714+ unsigned char try_trunc;
38715+ struct au_iinfo *iinfo;
38716+ struct super_block *sb;
38717+ struct au_hinode *hi;
38718+ struct inode *h_inode;
38719+ struct au_branch *br;
38720+ vfs_writef_t xwrite;
acd2b654
AM
38721+ struct au_xi_calc calc;
38722+ struct file *file;
521ced18 38723+
062440b3 38724+ AuDebugOn(au_is_bad_inode(inode));
521ced18 38725+
062440b3
AM
38726+ sb = inode->i_sb;
38727+ mnt_flags = au_mntflags(sb);
38728+ if (!au_opt_test(mnt_flags, XINO)
38729+ || inode->i_ino == AUFS_ROOT_INO)
38730+ return;
38731+
38732+ if (unlinked) {
38733+ au_xigen_inc(inode);
38734+ au_xib_clear_bit(inode);
38735+ }
38736+
38737+ iinfo = au_ii(inode);
38738+ bindex = iinfo->ii_btop;
38739+ if (bindex < 0)
38740+ return;
38741+
38742+ xwrite = au_sbi(sb)->si_xwrite;
38743+ try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38744+ hi = au_hinode(iinfo, bindex);
38745+ bbot = iinfo->ii_bbot;
38746+ for (; bindex <= bbot; bindex++, hi++) {
38747+ h_inode = hi->hi_inode;
38748+ if (!h_inode
38749+ || (!unlinked && h_inode->i_nlink))
38750+ continue;
38751+
38752+ /* inode may not be revalidated */
38753+ bi = au_br_index(sb, hi->hi_id);
38754+ if (bi < 0)
38755+ continue;
38756+
38757+ br = au_sbr(sb, bi);
acd2b654
AM
38758+ au_xi_calc(sb, h_inode->i_ino, &calc);
38759+ file = au_xino_file(br->br_xino, calc.idx);
38760+ if (IS_ERR_OR_NULL(file))
38761+ continue;
38762+
38763+ err = au_xino_do_write(xwrite, file, &calc, /*ino*/0);
062440b3
AM
38764+ if (!err && try_trunc
38765+ && au_test_fs_trunc_xino(au_br_sb(br)))
38766+ xino_try_trunc(sb, br);
38767+ }
521ced18
JR
38768+}
38769+
062440b3
AM
38770+/* ---------------------------------------------------------------------- */
38771+
38772+static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
521ced18
JR
38773+{
38774+ int found, total, i;
38775+
38776+ found = -1;
062440b3 38777+ total = xi->xi_nondir.total;
521ced18 38778+ for (i = 0; i < total; i++) {
062440b3 38779+ if (xi->xi_nondir.array[i] != h_ino)
521ced18
JR
38780+ continue;
38781+ found = i;
38782+ break;
38783+ }
38784+
38785+ return found;
38786+}
38787+
062440b3 38788+static int au_xinondir_expand(struct au_xino *xi)
521ced18
JR
38789+{
38790+ int err, sz;
38791+ ino_t *p;
38792+
38793+ BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38794+
38795+ err = -ENOMEM;
062440b3 38796+ sz = xi->xi_nondir.total * sizeof(ino_t);
521ced18
JR
38797+ if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38798+ goto out;
062440b3 38799+ p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
521ced18
JR
38800+ /*may_shrink*/0);
38801+ if (p) {
062440b3
AM
38802+ xi->xi_nondir.array = p;
38803+ xi->xi_nondir.total <<= 1;
38804+ AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
521ced18
JR
38805+ err = 0;
38806+ }
38807+
38808+out:
38809+ return err;
38810+}
38811+
062440b3
AM
38812+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38813+ ino_t h_ino, int idx)
38814+{
38815+ struct au_xino *xi;
38816+
38817+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38818+ xi = au_sbr(sb, bindex)->br_xino;
38819+ AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38820+
38821+ spin_lock(&xi->xi_nondir.spin);
38822+ AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38823+ xi->xi_nondir.array[idx] = 0;
38824+ spin_unlock(&xi->xi_nondir.spin);
38825+ wake_up_all(&xi->xi_nondir.wqh);
38826+}
38827+
521ced18
JR
38828+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38829+ int *idx)
38830+{
38831+ int err, found, empty;
062440b3 38832+ struct au_xino *xi;
521ced18
JR
38833+
38834+ err = 0;
38835+ *idx = -1;
38836+ if (!au_opt_test(au_mntflags(sb), XINO))
38837+ goto out; /* no xino */
38838+
062440b3 38839+ xi = au_sbr(sb, bindex)->br_xino;
521ced18
JR
38840+
38841+again:
062440b3
AM
38842+ spin_lock(&xi->xi_nondir.spin);
38843+ found = au_xinondir_find(xi, h_ino);
521ced18 38844+ if (found == -1) {
062440b3 38845+ empty = au_xinondir_find(xi, /*h_ino*/0);
521ced18 38846+ if (empty == -1) {
062440b3
AM
38847+ empty = xi->xi_nondir.total;
38848+ err = au_xinondir_expand(xi);
521ced18
JR
38849+ if (unlikely(err))
38850+ goto out_unlock;
38851+ }
062440b3 38852+ xi->xi_nondir.array[empty] = h_ino;
521ced18
JR
38853+ *idx = empty;
38854+ } else {
062440b3
AM
38855+ spin_unlock(&xi->xi_nondir.spin);
38856+ wait_event(xi->xi_nondir.wqh,
38857+ xi->xi_nondir.array[found] != h_ino);
521ced18
JR
38858+ goto again;
38859+ }
38860+
38861+out_unlock:
062440b3
AM
38862+ spin_unlock(&xi->xi_nondir.spin);
38863+out:
38864+ return err;
38865+}
38866+
38867+/* ---------------------------------------------------------------------- */
38868+
38869+int au_xino_path(struct seq_file *seq, struct file *file)
38870+{
38871+ int err;
38872+
38873+ err = au_seq_path(seq, &file->f_path);
38874+ if (unlikely(err))
38875+ goto out;
38876+
38877+#define Deleted "\\040(deleted)"
38878+ seq->count -= sizeof(Deleted) - 1;
38879+ AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38880+ sizeof(Deleted) - 1));
38881+#undef Deleted
38882+
521ced18
JR
38883+out:
38884+ return err;
38885+}
537831f9
AM
38886diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
38887--- /usr/share/empty/include/uapi/linux/aufs_type.h 1970-01-01 01:00:00.000000000 +0100
9f237c51 38888+++ linux/include/uapi/linux/aufs_type.h 2018-12-27 13:19:17.715082917 +0100
062440b3
AM
38889@@ -0,0 +1,448 @@
38890+/* SPDX-License-Identifier: GPL-2.0 */
7f207e10 38891+/*
b00004a5 38892+ * Copyright (C) 2005-2018 Junjiro R. Okajima
7f207e10
AM
38893+ *
38894+ * This program, aufs is free software; you can redistribute it and/or modify
38895+ * it under the terms of the GNU General Public License as published by
38896+ * the Free Software Foundation; either version 2 of the License, or
38897+ * (at your option) any later version.
38898+ *
38899+ * This program is distributed in the hope that it will be useful,
38900+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
38901+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38902+ * GNU General Public License for more details.
38903+ *
38904+ * You should have received a copy of the GNU General Public License
523b37e3 38905+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
38906+ */
38907+
38908+#ifndef __AUFS_TYPE_H__
38909+#define __AUFS_TYPE_H__
38910+
f6c5ef8b
AM
38911+#define AUFS_NAME "aufs"
38912+
9dbd164d 38913+#ifdef __KERNEL__
f6c5ef8b
AM
38914+/*
38915+ * define it before including all other headers.
38916+ * sched.h may use pr_* macros before defining "current", so define the
38917+ * no-current version first, and re-define later.
38918+ */
38919+#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
38920+#include <linux/sched.h>
38921+#undef pr_fmt
a2a7ad62
AM
38922+#define pr_fmt(fmt) \
38923+ AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
38924+ (int)sizeof(current->comm), current->comm, current->pid
9dbd164d
AM
38925+#else
38926+#include <stdint.h>
38927+#include <sys/types.h>
f6c5ef8b 38928+#endif /* __KERNEL__ */
7f207e10 38929+
f6c5ef8b
AM
38930+#include <linux/limits.h>
38931+
9f237c51 38932+#define AUFS_VERSION "4.x-rcN-20181217"
7f207e10
AM
38933+
38934+/* todo? move this to linux-2.6.19/include/magic.h */
38935+#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
38936+
38937+/* ---------------------------------------------------------------------- */
38938+
38939+#ifdef CONFIG_AUFS_BRANCH_MAX_127
9dbd164d 38940+typedef int8_t aufs_bindex_t;
7f207e10
AM
38941+#define AUFS_BRANCH_MAX 127
38942+#else
9dbd164d 38943+typedef int16_t aufs_bindex_t;
7f207e10
AM
38944+#ifdef CONFIG_AUFS_BRANCH_MAX_511
38945+#define AUFS_BRANCH_MAX 511
38946+#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
38947+#define AUFS_BRANCH_MAX 1023
38948+#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
38949+#define AUFS_BRANCH_MAX 32767
38950+#endif
38951+#endif
38952+
38953+#ifdef __KERNEL__
38954+#ifndef AUFS_BRANCH_MAX
38955+#error unknown CONFIG_AUFS_BRANCH_MAX value
38956+#endif
38957+#endif /* __KERNEL__ */
38958+
38959+/* ---------------------------------------------------------------------- */
38960+
7f207e10
AM
38961+#define AUFS_FSTYPE AUFS_NAME
38962+
38963+#define AUFS_ROOT_INO 2
38964+#define AUFS_FIRST_INO 11
38965+
38966+#define AUFS_WH_PFX ".wh."
38967+#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
38968+#define AUFS_WH_TMP_LEN 4
86dc4139 38969+/* a limit for rmdir/rename a dir and copyup */
7f207e10
AM
38970+#define AUFS_MAX_NAMELEN (NAME_MAX \
38971+ - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
38972+ - 1 /* dot */\
38973+ - AUFS_WH_TMP_LEN) /* hex */
38974+#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
38975+#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
392086de
AM
38976+#define AUFS_XINO_DEF_SEC 30 /* seconds */
38977+#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
7f207e10
AM
38978+#define AUFS_DIRWH_DEF 3
38979+#define AUFS_RDCACHE_DEF 10 /* seconds */
027c5e7a 38980+#define AUFS_RDCACHE_MAX 3600 /* seconds */
7f207e10
AM
38981+#define AUFS_RDBLK_DEF 512 /* bytes */
38982+#define AUFS_RDHASH_DEF 32
38983+#define AUFS_WKQ_NAME AUFS_NAME "d"
027c5e7a
AM
38984+#define AUFS_MFS_DEF_SEC 30 /* seconds */
38985+#define AUFS_MFS_MAX_SEC 3600 /* seconds */
076b876e 38986+#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
86dc4139 38987+#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
7f207e10
AM
38988+
38989+/* pseudo-link maintenace under /proc */
38990+#define AUFS_PLINK_MAINT_NAME "plink_maint"
38991+#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
38992+#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
38993+
8b6a4947
AM
38994+/* dirren, renamed dir */
38995+#define AUFS_DR_INFO_PFX AUFS_WH_PFX ".dr."
38996+#define AUFS_DR_BRHINO_NAME AUFS_WH_PFX "hino"
38997+/* whiteouted doubly */
38998+#define AUFS_WH_DR_INFO_PFX AUFS_WH_PFX AUFS_DR_INFO_PFX
38999+#define AUFS_WH_DR_BRHINO AUFS_WH_PFX AUFS_DR_BRHINO_NAME
39000+
7f207e10
AM
39001+#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
39002+#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
39003+
39004+#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
39005+#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
39006+#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
39007+
39008+/* doubly whiteouted */
39009+#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
39010+#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
39011+#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
39012+
1e00d052 39013+/* branch permissions and attributes */
7f207e10
AM
39014+#define AUFS_BRPERM_RW "rw"
39015+#define AUFS_BRPERM_RO "ro"
39016+#define AUFS_BRPERM_RR "rr"
076b876e
AM
39017+#define AUFS_BRATTR_COO_REG "coo_reg"
39018+#define AUFS_BRATTR_COO_ALL "coo_all"
39019+#define AUFS_BRATTR_FHSM "fhsm"
39020+#define AUFS_BRATTR_UNPIN "unpin"
c1595e42
JR
39021+#define AUFS_BRATTR_ICEX "icex"
39022+#define AUFS_BRATTR_ICEX_SEC "icexsec"
39023+#define AUFS_BRATTR_ICEX_SYS "icexsys"
39024+#define AUFS_BRATTR_ICEX_TR "icextr"
39025+#define AUFS_BRATTR_ICEX_USR "icexusr"
39026+#define AUFS_BRATTR_ICEX_OTH "icexoth"
1e00d052
AM
39027+#define AUFS_BRRATTR_WH "wh"
39028+#define AUFS_BRWATTR_NLWH "nolwh"
076b876e
AM
39029+#define AUFS_BRWATTR_MOO "moo"
39030+
39031+#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
39032+#define AuBrPerm_RO (1 << 1) /* readonly */
39033+#define AuBrPerm_RR (1 << 2) /* natively readonly */
39034+#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
39035+
39036+#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
39037+#define AuBrAttr_COO_ALL (1 << 4)
39038+#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
39039+
39040+#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
39041+#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
c1595e42
JR
39042+ branch. meaningless since
39043+ linux-3.18-rc1 */
39044+
39045+/* ignore error in copying XATTR */
39046+#define AuBrAttr_ICEX_SEC (1 << 7)
39047+#define AuBrAttr_ICEX_SYS (1 << 8)
39048+#define AuBrAttr_ICEX_TR (1 << 9)
39049+#define AuBrAttr_ICEX_USR (1 << 10)
39050+#define AuBrAttr_ICEX_OTH (1 << 11)
39051+#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
39052+ | AuBrAttr_ICEX_SYS \
39053+ | AuBrAttr_ICEX_TR \
39054+ | AuBrAttr_ICEX_USR \
39055+ | AuBrAttr_ICEX_OTH)
39056+
39057+#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
076b876e
AM
39058+#define AuBrRAttr_Mask AuBrRAttr_WH
39059+
c1595e42
JR
39060+#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
39061+#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
076b876e
AM
39062+#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
39063+
39064+#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
39065+
c1595e42 39066+/* #warning test userspace */
076b876e
AM
39067+#ifdef __KERNEL__
39068+#ifndef CONFIG_AUFS_FHSM
39069+#undef AuBrAttr_FHSM
39070+#define AuBrAttr_FHSM 0
39071+#endif
c1595e42
JR
39072+#ifndef CONFIG_AUFS_XATTR
39073+#undef AuBrAttr_ICEX
39074+#define AuBrAttr_ICEX 0
39075+#undef AuBrAttr_ICEX_SEC
39076+#define AuBrAttr_ICEX_SEC 0
39077+#undef AuBrAttr_ICEX_SYS
39078+#define AuBrAttr_ICEX_SYS 0
39079+#undef AuBrAttr_ICEX_TR
39080+#define AuBrAttr_ICEX_TR 0
39081+#undef AuBrAttr_ICEX_USR
39082+#define AuBrAttr_ICEX_USR 0
39083+#undef AuBrAttr_ICEX_OTH
39084+#define AuBrAttr_ICEX_OTH 0
39085+#endif
076b876e
AM
39086+#endif
39087+
39088+/* the longest combination */
c1595e42
JR
39089+/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
39090+#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
39091+ "+" AUFS_BRATTR_COO_REG \
39092+ "+" AUFS_BRATTR_FHSM \
39093+ "+" AUFS_BRATTR_UNPIN \
7e9cd9fe
AM
39094+ "+" AUFS_BRATTR_ICEX_SEC \
39095+ "+" AUFS_BRATTR_ICEX_SYS \
39096+ "+" AUFS_BRATTR_ICEX_USR \
39097+ "+" AUFS_BRATTR_ICEX_OTH \
076b876e
AM
39098+ "+" AUFS_BRWATTR_NLWH)
39099+
39100+typedef struct {
39101+ char a[AuBrPermStrSz];
39102+} au_br_perm_str_t;
39103+
39104+static inline int au_br_writable(int brperm)
39105+{
39106+ return brperm & AuBrPerm_RW;
39107+}
39108+
39109+static inline int au_br_whable(int brperm)
39110+{
39111+ return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
39112+}
39113+
39114+static inline int au_br_wh_linkable(int brperm)
39115+{
39116+ return !(brperm & AuBrWAttr_NoLinkWH);
39117+}
39118+
39119+static inline int au_br_cmoo(int brperm)
39120+{
39121+ return brperm & AuBrAttr_CMOO_Mask;
39122+}
39123+
39124+static inline int au_br_fhsm(int brperm)
39125+{
39126+ return brperm & AuBrAttr_FHSM;
39127+}
7f207e10
AM
39128+
39129+/* ---------------------------------------------------------------------- */
39130+
39131+/* ioctl */
39132+enum {
39133+ /* readdir in userspace */
39134+ AuCtl_RDU,
39135+ AuCtl_RDU_INO,
39136+
076b876e
AM
39137+ AuCtl_WBR_FD, /* pathconf wrapper */
39138+ AuCtl_IBUSY, /* busy inode */
39139+ AuCtl_MVDOWN, /* move-down */
39140+ AuCtl_BR, /* info about branches */
39141+ AuCtl_FHSM_FD /* connection for fhsm */
7f207e10
AM
39142+};
39143+
39144+/* borrowed from linux/include/linux/kernel.h */
39145+#ifndef ALIGN
39146+#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
39147+#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
39148+#endif
39149+
39150+/* borrowed from linux/include/linux/compiler-gcc3.h */
39151+#ifndef __aligned
39152+#define __aligned(x) __attribute__((aligned(x)))
53392da6
AM
39153+#endif
39154+
39155+#ifdef __KERNEL__
39156+#ifndef __packed
7f207e10
AM
39157+#define __packed __attribute__((packed))
39158+#endif
53392da6 39159+#endif
7f207e10
AM
39160+
39161+struct au_rdu_cookie {
9dbd164d
AM
39162+ uint64_t h_pos;
39163+ int16_t bindex;
39164+ uint8_t flags;
39165+ uint8_t pad;
39166+ uint32_t generation;
7f207e10
AM
39167+} __aligned(8);
39168+
39169+struct au_rdu_ent {
9dbd164d
AM
39170+ uint64_t ino;
39171+ int16_t bindex;
39172+ uint8_t type;
39173+ uint8_t nlen;
39174+ uint8_t wh;
7f207e10
AM
39175+ char name[0];
39176+} __aligned(8);
39177+
39178+static inline int au_rdu_len(int nlen)
39179+{
39180+ /* include the terminating NULL */
39181+ return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
9dbd164d 39182+ sizeof(uint64_t));
7f207e10
AM
39183+}
39184+
39185+union au_rdu_ent_ul {
39186+ struct au_rdu_ent __user *e;
9dbd164d 39187+ uint64_t ul;
7f207e10
AM
39188+};
39189+
39190+enum {
39191+ AufsCtlRduV_SZ,
39192+ AufsCtlRduV_End
39193+};
39194+
39195+struct aufs_rdu {
39196+ /* input */
39197+ union {
9dbd164d
AM
39198+ uint64_t sz; /* AuCtl_RDU */
39199+ uint64_t nent; /* AuCtl_RDU_INO */
7f207e10
AM
39200+ };
39201+ union au_rdu_ent_ul ent;
9dbd164d 39202+ uint16_t verify[AufsCtlRduV_End];
7f207e10
AM
39203+
39204+ /* input/output */
9dbd164d 39205+ uint32_t blk;
7f207e10
AM
39206+
39207+ /* output */
39208+ union au_rdu_ent_ul tail;
39209+ /* number of entries which were added in a single call */
9dbd164d
AM
39210+ uint64_t rent;
39211+ uint8_t full;
39212+ uint8_t shwh;
7f207e10
AM
39213+
39214+ struct au_rdu_cookie cookie;
39215+} __aligned(8);
39216+
1e00d052
AM
39217+/* ---------------------------------------------------------------------- */
39218+
8b6a4947
AM
39219+/* dirren. the branch is identified by the filename who contains this */
39220+struct au_drinfo {
39221+ uint64_t ino;
39222+ union {
39223+ uint8_t oldnamelen;
39224+ uint64_t _padding;
39225+ };
39226+ uint8_t oldname[0];
39227+} __aligned(8);
39228+
39229+struct au_drinfo_fdata {
39230+ uint32_t magic;
39231+ struct au_drinfo drinfo;
39232+} __aligned(8);
39233+
39234+#define AUFS_DRINFO_MAGIC_V1 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39235+/* future */
39236+#define AUFS_DRINFO_MAGIC_V2 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39237+
39238+/* ---------------------------------------------------------------------- */
39239+
1e00d052 39240+struct aufs_wbr_fd {
9dbd164d
AM
39241+ uint32_t oflags;
39242+ int16_t brid;
1e00d052
AM
39243+} __aligned(8);
39244+
39245+/* ---------------------------------------------------------------------- */
39246+
027c5e7a 39247+struct aufs_ibusy {
9dbd164d
AM
39248+ uint64_t ino, h_ino;
39249+ int16_t bindex;
027c5e7a
AM
39250+} __aligned(8);
39251+
1e00d052
AM
39252+/* ---------------------------------------------------------------------- */
39253+
392086de
AM
39254+/* error code for move-down */
39255+/* the actual message strings are implemented in aufs-util.git */
39256+enum {
39257+ EAU_MVDOWN_OPAQUE = 1,
39258+ EAU_MVDOWN_WHITEOUT,
39259+ EAU_MVDOWN_UPPER,
39260+ EAU_MVDOWN_BOTTOM,
39261+ EAU_MVDOWN_NOUPPER,
39262+ EAU_MVDOWN_NOLOWERBR,
39263+ EAU_Last
39264+};
39265+
c2b27bf2 39266+/* flags for move-down */
392086de
AM
39267+#define AUFS_MVDOWN_DMSG 1
39268+#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
39269+#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
39270+#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
39271+#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
39272+#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
39273+#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
39274+#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
39275+#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
076b876e
AM
39276+#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
39277+#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
39278+#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
39279+#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
c2b27bf2 39280+
076b876e 39281+/* index for move-down */
392086de
AM
39282+enum {
39283+ AUFS_MVDOWN_UPPER,
39284+ AUFS_MVDOWN_LOWER,
39285+ AUFS_MVDOWN_NARRAY
39286+};
39287+
076b876e
AM
39288+/*
39289+ * additional info of move-down
39290+ * number of free blocks and inodes.
39291+ * subset of struct kstatfs, but smaller and always 64bit.
39292+ */
39293+struct aufs_stfs {
39294+ uint64_t f_blocks;
39295+ uint64_t f_bavail;
39296+ uint64_t f_files;
39297+ uint64_t f_ffree;
39298+};
39299+
39300+struct aufs_stbr {
39301+ int16_t brid; /* optional input */
39302+ int16_t bindex; /* output */
39303+ struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
39304+} __aligned(8);
39305+
c2b27bf2 39306+struct aufs_mvdown {
076b876e
AM
39307+ uint32_t flags; /* input/output */
39308+ struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39309+ int8_t au_errno; /* output */
39310+} __aligned(8);
39311+
39312+/* ---------------------------------------------------------------------- */
39313+
39314+union aufs_brinfo {
39315+ /* PATH_MAX may differ between kernel-space and user-space */
39316+ char _spacer[4096];
392086de 39317+ struct {
076b876e
AM
39318+ int16_t id;
39319+ int perm;
39320+ char path[0];
39321+ };
c2b27bf2
AM
39322+} __aligned(8);
39323+
39324+/* ---------------------------------------------------------------------- */
39325+
7f207e10
AM
39326+#define AuCtlType 'A'
39327+#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39328+#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
1e00d052
AM
39329+#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
39330+ struct aufs_wbr_fd)
027c5e7a 39331+#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
392086de
AM
39332+#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
39333+ struct aufs_mvdown)
076b876e
AM
39334+#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39335+#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
7f207e10
AM
39336+
39337+#endif /* __AUFS_TYPE_H__ */
2121bcd9 39338SPDX-License-Identifier: GPL-2.0
cd7a4cd9 39339aufs4.x-rcN loopback patch
5527c038
JR
39340
39341diff --git a/drivers/block/loop.c b/drivers/block/loop.c
9f237c51 39342index 470dd02..20dc3ec 100644
5527c038
JR
39343--- a/drivers/block/loop.c
39344+++ b/drivers/block/loop.c
9f237c51 39345@@ -625,6 +625,15 @@ static inline void loop_update_dio(struct loop_device *lo)
8b6a4947 39346 lo->use_dio);
5527c038
JR
39347 }
39348
5527c038
JR
39349+static struct file *loop_real_file(struct file *file)
39350+{
39351+ struct file *f = NULL;
39352+
39353+ if (file->f_path.dentry->d_sb->s_op->real_loop)
39354+ f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39355+ return f;
8b6a4947
AM
39356+}
39357+
c2c0f25c 39358 static void loop_reread_partitions(struct loop_device *lo,
8b6a4947
AM
39359 struct block_device *bdev)
39360 {
9f237c51 39361@@ -689,6 +698,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
5527c038
JR
39362 unsigned int arg)
39363 {
39364 struct file *file, *old_file;
39365+ struct file *f, *virt_file = NULL, *old_virt_file;
5527c038
JR
39366 int error;
39367
acd2b654 39368 error = -ENXIO;
9f237c51 39369@@ -704,12 +714,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
cd7a4cd9
AM
39370 file = fget(arg);
39371 if (!file)
39372 goto out;
5527c038
JR
39373+ f = loop_real_file(file);
39374+ if (f) {
39375+ virt_file = file;
39376+ file = f;
39377+ get_file(file);
39378+ }
39379
cd7a4cd9
AM
39380 error = loop_validate_file(file, bdev);
39381 if (error)
acd2b654 39382 goto out_putf;
cd7a4cd9 39383
5527c038
JR
39384 old_file = lo->lo_backing_file;
39385+ old_virt_file = lo->lo_backing_virt_file;
39386
39387 error = -EINVAL;
39388
9f237c51 39389@@ -721,6 +738,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
8b6a4947
AM
39390 blk_mq_freeze_queue(lo->lo_queue);
39391 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39392 lo->lo_backing_file = file;
39393+ lo->lo_backing_virt_file = virt_file;
39394 lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39395 mapping_set_gfp_mask(file->f_mapping,
39396 lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
9f237c51 39397@@ -728,12 +746,16 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
8b6a4947 39398 blk_mq_unfreeze_queue(lo->lo_queue);
5527c038
JR
39399
39400 fput(old_file);
39401+ if (old_virt_file)
39402+ fput(old_virt_file);
39403 if (lo->lo_flags & LO_FLAGS_PARTSCAN)
c2c0f25c 39404 loop_reread_partitions(lo, bdev);
5527c038
JR
39405 return 0;
39406
39407 out_putf:
39408 fput(file);
39409+ if (virt_file)
39410+ fput(virt_file);
39411 out:
39412 return error;
39413 }
9f237c51 39414@@ -921,7 +943,7 @@ static int loop_prepare_queue(struct loop_device *lo)
5527c038
JR
39415 static int loop_set_fd(struct loop_device *lo, fmode_t mode,
39416 struct block_device *bdev, unsigned int arg)
39417 {
062440b3 39418- struct file *file;
5527c038
JR
39419+ struct file *file, *f, *virt_file = NULL;
39420 struct inode *inode;
39421 struct address_space *mapping;
8b6a4947 39422 int lo_flags = 0;
9f237c51 39423@@ -935,6 +957,12 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
39424 file = fget(arg);
39425 if (!file)
39426 goto out;
39427+ f = loop_real_file(file);
39428+ if (f) {
39429+ virt_file = file;
39430+ file = f;
39431+ get_file(file);
39432+ }
39433
39434 error = -EBUSY;
39435 if (lo->lo_state != Lo_unbound)
9f237c51 39436@@ -967,6 +995,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
39437 lo->lo_device = bdev;
39438 lo->lo_flags = lo_flags;
39439 lo->lo_backing_file = file;
39440+ lo->lo_backing_virt_file = virt_file;
39441 lo->transfer = NULL;
39442 lo->ioctl = NULL;
39443 lo->lo_sizelimit = 0;
9f237c51 39444@@ -1000,6 +1029,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
39445
39446 out_putf:
39447 fput(file);
39448+ if (virt_file)
39449+ fput(virt_file);
39450 out:
39451 /* This is safe: open() is still holding a reference. */
39452 module_put(THIS_MODULE);
9f237c51 39453@@ -1046,6 +1077,7 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
5527c038
JR
39454 static int loop_clr_fd(struct loop_device *lo)
39455 {
39456 struct file *filp = lo->lo_backing_file;
39457+ struct file *virt_filp = lo->lo_backing_virt_file;
39458 gfp_t gfp = lo->old_gfp_mask;
39459 struct block_device *bdev = lo->lo_device;
39460
9f237c51 39461@@ -1077,6 +1109,7 @@ static int loop_clr_fd(struct loop_device *lo)
5527c038
JR
39462 spin_lock_irq(&lo->lo_lock);
39463 lo->lo_state = Lo_rundown;
39464 lo->lo_backing_file = NULL;
39465+ lo->lo_backing_virt_file = NULL;
39466 spin_unlock_irq(&lo->lo_lock);
39467
39468 loop_release_xfer(lo);
9f237c51 39469@@ -1125,6 +1158,8 @@ static int loop_clr_fd(struct loop_device *lo)
5527c038
JR
39470 * bd_mutex which is usually taken before lo_ctl_mutex.
39471 */
39472 fput(filp);
39473+ if (virt_filp)
39474+ fput(virt_filp);
39475 return 0;
39476 }
39477
39478diff --git a/drivers/block/loop.h b/drivers/block/loop.h
cd7a4cd9 39479index 4d42c7a..a4974ee 100644
5527c038
JR
39480--- a/drivers/block/loop.h
39481+++ b/drivers/block/loop.h
39482@@ -46,7 +46,7 @@ struct loop_device {
39483 int (*ioctl)(struct loop_device *, int cmd,
39484 unsigned long arg);
39485
39486- struct file * lo_backing_file;
39487+ struct file * lo_backing_file, *lo_backing_virt_file;
39488 struct block_device *lo_device;
5527c038 39489 void *key_data;
8b6a4947 39490
5527c038 39491diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
acd2b654 39492index 5309874..1a334cf 100644
5527c038
JR
39493--- a/fs/aufs/f_op.c
39494+++ b/fs/aufs/f_op.c
acd2b654 39495@@ -359,7 +359,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
5527c038
JR
39496 if (IS_ERR(h_file))
39497 goto out;
39498
39499- if (au_test_loopback_kthread()) {
39500+ if (0 && au_test_loopback_kthread()) {
39501 au_warn_loopback(h_file->f_path.dentry->d_sb);
39502 if (file->f_mapping != h_file->f_mapping) {
39503 file->f_mapping = h_file->f_mapping;
39504diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
9f237c51 39505index 3f3577d..3b44158 100644
5527c038
JR
39506--- a/fs/aufs/loop.c
39507+++ b/fs/aufs/loop.c
2121bcd9 39508@@ -133,3 +133,19 @@ void au_loopback_fin(void)
79b8bda9 39509 symbol_put(loop_backing_file);
9f237c51 39510 au_kfree_try_rcu(au_warn_loopback_array);
5527c038
JR
39511 }
39512+
39513+/* ---------------------------------------------------------------------- */
39514+
39515+/* support the loopback block device insude aufs */
39516+
39517+struct file *aufs_real_loop(struct file *file)
39518+{
39519+ struct file *f;
39520+
39521+ BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39522+ fi_read_lock(file);
39523+ f = au_hf_top(file);
39524+ fi_read_unlock(file);
39525+ AuDebugOn(!f);
39526+ return f;
39527+}
39528diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
062440b3 39529index 05d703d..6bb23c8 100644
5527c038
JR
39530--- a/fs/aufs/loop.h
39531+++ b/fs/aufs/loop.h
2121bcd9 39532@@ -26,7 +26,11 @@ void au_warn_loopback(struct super_block *h_sb);
5527c038
JR
39533
39534 int au_loopback_init(void);
39535 void au_loopback_fin(void);
39536+
39537+struct file *aufs_real_loop(struct file *file);
39538 #else
39539+AuStub(struct file *, loop_backing_file, return NULL)
39540+
39541 AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
39542 struct dentry *h_adding)
39543 AuStubInt0(au_test_loopback_kthread, void)
2121bcd9 39544@@ -34,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
5527c038
JR
39545
39546 AuStubInt0(au_loopback_init, void)
39547 AuStubVoid(au_loopback_fin, void)
39548+
39549+AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39550 #endif /* BLK_DEV_LOOP */
39551
39552 #endif /* __KERNEL__ */
39553diff --git a/fs/aufs/super.c b/fs/aufs/super.c
acd2b654 39554index 777503e..7130061 100644
5527c038
JR
39555--- a/fs/aufs/super.c
39556+++ b/fs/aufs/super.c
acd2b654 39557@@ -845,7 +845,10 @@ static const struct super_operations aufs_sop = {
5527c038
JR
39558 .statfs = aufs_statfs,
39559 .put_super = aufs_put_super,
39560 .sync_fs = aufs_sync_fs,
39561- .remount_fs = aufs_remount_fs
39562+ .remount_fs = aufs_remount_fs,
39563+#ifdef CONFIG_AUFS_BDEV_LOOP
39564+ .real_loop = aufs_real_loop
39565+#endif
39566 };
39567
39568 /* ---------------------------------------------------------------------- */
39569diff --git a/include/linux/fs.h b/include/linux/fs.h
9f237c51 39570index 0e44705..59fb8ae 100644
5527c038
JR
39571--- a/include/linux/fs.h
39572+++ b/include/linux/fs.h
9f237c51 39573@@ -1931,6 +1931,10 @@ struct super_operations {
5527c038
JR
39574 struct shrink_control *);
39575 long (*free_cached_objects)(struct super_block *,
39576 struct shrink_control *);
39577+#if defined(CONFIG_BLK_DEV_LOOP) || defined(CONFIG_BLK_DEV_LOOP_MODULE)
39578+ /* and aufs */
39579+ struct file *(*real_loop)(struct file *);
39580+#endif
39581 };
39582
39583 /*
This page took 8.64297 seconds and 4 git commands to generate.