]> git.pld-linux.org Git - packages/kernel.git/blame - kernel-aufs4.patch
- up to 4.18.13
[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
cd7a4cd9 29index 0fe4228..52cedab 100644
c1595e42
JR
30--- a/MAINTAINERS
31+++ b/MAINTAINERS
cd7a4cd9 32@@ -2541,6 +2541,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
cd7a4cd9 53index 4cb1d1b..5678bf8 100644
392086de
AM
54--- a/drivers/block/loop.c
55+++ b/drivers/block/loop.c
cd7a4cd9
AM
56@@ -741,6 +741,24 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
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
cd7a4cd9 82index 0e8e5de..88de3b6 100644
c1595e42
JR
83--- a/fs/dcache.c
84+++ b/fs/dcache.c
cd7a4cd9 85@@ -1240,7 +1240,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
cd7a4cd9 95index 12273b6..545e686 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
cd7a4cd9 117index 8c86c80..af894e7 100644
5afbbe0d
AM
118--- a/fs/inode.c
119+++ b/fs/inode.c
cd7a4cd9 120@@ -1649,7 +1649,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
cd7a4cd9 130index 8ddd148..ee649e0 100644
8b6a4947
AM
131--- a/fs/namespace.c
132+++ b/fs/namespace.c
133@@ -846,6 +846,12 @@ static inline int check_mnt(struct mount *mnt)
134 return mnt->mnt_ns == current->nsproxy->mnt_ns;
135 }
136
137+/* for aufs, CONFIG_AUFS_BR_FUSE */
138+int is_current_mnt_ns(struct vfsmount *mnt)
139+{
140+ return check_mnt(real_mount(mnt));
141+}
142+
143 /*
144 * vfsmount lock must be held for write
145 */
5527c038 146diff --git a/fs/read_write.c b/fs/read_write.c
cd7a4cd9 147index 153f8f6..ccc5bc8 100644
5527c038
JR
148--- a/fs/read_write.c
149+++ b/fs/read_write.c
b00004a5 150@@ -489,6 +489,28 @@ ssize_t __vfs_write(struct file *file, const char __user *p, size_t count,
8b6a4947 151 return -EINVAL;
5527c038 152 }
5527c038
JR
153
154+vfs_readf_t vfs_readf(struct file *file)
155+{
156+ const struct file_operations *fop = file->f_op;
157+
158+ if (fop->read)
159+ return fop->read;
160+ if (fop->read_iter)
161+ return new_sync_read;
162+ return ERR_PTR(-ENOSYS);
163+}
164+
165+vfs_writef_t vfs_writef(struct file *file)
166+{
167+ const struct file_operations *fop = file->f_op;
168+
169+ if (fop->write)
170+ return fop->write;
171+ if (fop->write_iter)
172+ return new_sync_write;
173+ return ERR_PTR(-ENOSYS);
174+}
175+
8b6a4947 176 ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
5527c038
JR
177 {
178 mm_segment_t old_fs;
7f207e10 179diff --git a/fs/splice.c b/fs/splice.c
cd7a4cd9 180index b3daa97..1dd7f96 100644
7f207e10
AM
181--- a/fs/splice.c
182+++ b/fs/splice.c
cd7a4cd9 183@@ -838,8 +838,8 @@ EXPORT_SYMBOL(generic_splice_sendpage);
7f207e10
AM
184 /*
185 * Attempt to initiate a splice from pipe to file.
186 */
187-static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
188- loff_t *ppos, size_t len, unsigned int flags)
189+long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
190+ loff_t *ppos, size_t len, unsigned int flags)
191 {
192 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
193 loff_t *, size_t, unsigned int);
cd7a4cd9 194@@ -855,9 +855,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
7f207e10
AM
195 /*
196 * Attempt to initiate a splice from a file to a pipe.
197 */
198-static long do_splice_to(struct file *in, loff_t *ppos,
199- struct pipe_inode_info *pipe, size_t len,
200- unsigned int flags)
201+long do_splice_to(struct file *in, loff_t *ppos,
202+ struct pipe_inode_info *pipe, size_t len,
203+ unsigned int flags)
204 {
205 ssize_t (*splice_read)(struct file *, loff_t *,
206 struct pipe_inode_info *, size_t, unsigned int);
a2654f78 207diff --git a/fs/sync.c b/fs/sync.c
b00004a5 208index b54e054..2860782 100644
a2654f78
AM
209--- a/fs/sync.c
210+++ b/fs/sync.c
2121bcd9 211@@ -28,7 +28,7 @@
a2654f78
AM
212 * wait == 1 case since in that case write_inode() functions do
213 * sync_dirty_buffer() and thus effectively write one block at a time.
214 */
215-static int __sync_filesystem(struct super_block *sb, int wait)
216+int __sync_filesystem(struct super_block *sb, int wait)
217 {
218 if (wait)
219 sync_inodes_sb(sb);
b912730e 220diff --git a/include/linux/file.h b/include/linux/file.h
2121bcd9 221index 279720d..76e38ea 100644
b912730e
AM
222--- a/include/linux/file.h
223+++ b/include/linux/file.h
2121bcd9 224@@ -20,6 +20,7 @@ struct dentry;
b912730e 225 struct path;
a2654f78 226 extern struct file *alloc_file(const struct path *, fmode_t mode,
b912730e
AM
227 const struct file_operations *fop);
228+extern struct file *get_empty_filp(void);
229
230 static inline void fput_light(struct file *file, int fput_needed)
231 {
5527c038 232diff --git a/include/linux/fs.h b/include/linux/fs.h
cd7a4cd9 233index 805bf22..0d0d247f 100644
5527c038
JR
234--- a/include/linux/fs.h
235+++ b/include/linux/fs.h
cd7a4cd9 236@@ -1273,6 +1273,7 @@ extern void fasync_free(struct fasync_struct *);
febd17d6
JR
237 /* can be called from interrupts */
238 extern void kill_fasync(struct fasync_struct **, int, int);
239
240+extern int setfl(int fd, struct file * filp, unsigned long arg);
241 extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
3c1bdaff 242 extern int f_setown(struct file *filp, unsigned long arg, int force);
febd17d6 243 extern void f_delown(struct file *filp);
cd7a4cd9 244@@ -1733,6 +1734,7 @@ struct file_operations {
febd17d6
JR
245 ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
246 unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
247 int (*check_flags)(int);
248+ int (*setfl)(struct file *, unsigned long);
249 int (*flock) (struct file *, int, struct file_lock *);
250 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
251 ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
cd7a4cd9 252@@ -1803,6 +1805,12 @@ ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
5527c038
JR
253 struct iovec *fast_pointer,
254 struct iovec **ret_pointer);
255
256+typedef ssize_t (*vfs_readf_t)(struct file *, char __user *, size_t, loff_t *);
257+typedef ssize_t (*vfs_writef_t)(struct file *, const char __user *, size_t,
258+ loff_t *);
259+vfs_readf_t vfs_readf(struct file *file);
260+vfs_writef_t vfs_writef(struct file *file);
261+
262 extern ssize_t __vfs_read(struct file *, char __user *, size_t, loff_t *);
5527c038 263 extern ssize_t vfs_read(struct file *, char __user *, size_t, loff_t *);
8b6a4947 264 extern ssize_t vfs_write(struct file *, const char __user *, size_t, loff_t *);
cd7a4cd9 265@@ -2218,6 +2226,7 @@ extern int current_umask(void);
5afbbe0d
AM
266 extern void ihold(struct inode * inode);
267 extern void iput(struct inode *);
cd7a4cd9
AM
268 extern int generic_update_time(struct inode *, struct timespec64 *, int);
269+extern int update_time(struct inode *, struct timespec64 *, int);
5afbbe0d
AM
270
271 /* /sys/fs */
272 extern struct kobject *fs_kobj;
cd7a4cd9 273@@ -2505,6 +2514,7 @@ static inline bool sb_is_blkdev_sb(struct super_block *sb)
a2654f78
AM
274 return false;
275 }
276 #endif
277+extern int __sync_filesystem(struct super_block *, int);
278 extern int sync_filesystem(struct super_block *);
279 extern const struct file_operations def_blk_fops;
280 extern const struct file_operations def_chr_fops;
8b6a4947 281diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
be118d29 282index 6fc77d4..27e76f0 100644
8b6a4947
AM
283--- a/include/linux/lockdep.h
284+++ b/include/linux/lockdep.h
be118d29 285@@ -313,6 +313,8 @@ static inline int lockdep_match_key(struct lockdep_map *lock,
8b6a4947
AM
286 return lock->key == key;
287 }
288
289+struct lock_class *lockdep_hlock_class(struct held_lock *hlock);
290+
291 /*
292 * Acquire a lock.
293 *
be118d29 294@@ -439,6 +441,7 @@ struct lockdep_map { };
8b6a4947
AM
295
296 #define lockdep_depth(tsk) (0)
297
298+#define lockdep_is_held(lock) (1)
299 #define lockdep_is_held_type(l, r) (1)
300
301 #define lockdep_assert_held(l) do { (void)(l); } while (0)
302diff --git a/include/linux/mnt_namespace.h b/include/linux/mnt_namespace.h
2121bcd9 303index 3594208..24f5fd1 100644
8b6a4947
AM
304--- a/include/linux/mnt_namespace.h
305+++ b/include/linux/mnt_namespace.h
2121bcd9 306@@ -6,11 +6,14 @@
8b6a4947
AM
307 struct mnt_namespace;
308 struct fs_struct;
309 struct user_namespace;
310+struct vfsmount;
311
312 extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
313 struct user_namespace *, struct fs_struct *);
314 extern void put_mnt_ns(struct mnt_namespace *ns);
315
316+extern int is_current_mnt_ns(struct vfsmount *mnt);
317+
318 extern const struct file_operations proc_mounts_operations;
319 extern const struct file_operations proc_mountinfo_operations;
320 extern const struct file_operations proc_mountstats_operations;
1e00d052 321diff --git a/include/linux/splice.h b/include/linux/splice.h
2121bcd9 322index 74b4911..19789fb 100644
1e00d052
AM
323--- a/include/linux/splice.h
324+++ b/include/linux/splice.h
2121bcd9 325@@ -87,4 +87,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
4b3da204
AM
326
327 extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
106341ce 328 extern const struct pipe_buf_operations default_pipe_buf_ops;
1e00d052
AM
329+
330+extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
331+ loff_t *ppos, size_t len, unsigned int flags);
332+extern long do_splice_to(struct file *in, loff_t *ppos,
333+ struct pipe_inode_info *pipe, size_t len,
334+ unsigned int flags);
335 #endif
8b6a4947 336diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
cd7a4cd9 337index 5fa4d31..720f070 100644
8b6a4947
AM
338--- a/kernel/locking/lockdep.c
339+++ b/kernel/locking/lockdep.c
be118d29 340@@ -140,7 +140,7 @@ static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
8b6a4947
AM
341 unsigned long nr_lock_classes;
342 static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
343
344-static inline struct lock_class *hlock_class(struct held_lock *hlock)
345+inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
346 {
347 if (!hlock->class_idx) {
348 /*
be118d29 349@@ -151,6 +151,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
8b6a4947
AM
350 }
351 return lock_classes + hlock->class_idx - 1;
352 }
353+#define hlock_class(hlock) lockdep_hlock_class(hlock)
354
355 #ifdef CONFIG_LOCK_STAT
356 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
2121bcd9 357SPDX-License-Identifier: GPL-2.0
cd7a4cd9 358aufs4.x-rcN mmap patch
fb47a38f 359
c1595e42 360diff --git a/fs/proc/base.c b/fs/proc/base.c
cd7a4cd9 361index aaffc0c..ab72941 100644
c1595e42
JR
362--- a/fs/proc/base.c
363+++ b/fs/proc/base.c
cd7a4cd9 364@@ -2004,7 +2004,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
c1595e42
JR
365 down_read(&mm->mmap_sem);
366 vma = find_exact_vma(mm, vm_start, vm_end);
367 if (vma && vma->vm_file) {
368- *path = vma->vm_file->f_path;
369+ *path = vma_pr_or_file(vma)->f_path;
370 path_get(path);
371 rc = 0;
372 }
fb47a38f 373diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
cd7a4cd9 374index 3b63be6..fb9913b 100644
fb47a38f
JR
375--- a/fs/proc/nommu.c
376+++ b/fs/proc/nommu.c
076b876e 377@@ -45,7 +45,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
fb47a38f
JR
378 file = region->vm_file;
379
380 if (file) {
381- struct inode *inode = file_inode(region->vm_file);
382+ struct inode *inode;
076b876e 383+
fb47a38f
JR
384+ file = vmr_pr_or_file(region);
385+ inode = file_inode(file);
386 dev = inode->i_sb->s_dev;
387 ino = inode->i_ino;
388 }
389diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
cd7a4cd9 390index dfd73a4..ed2ce10 100644
fb47a38f
JR
391--- a/fs/proc/task_mmu.c
392+++ b/fs/proc/task_mmu.c
cd7a4cd9 393@@ -306,7 +306,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
fb47a38f
JR
394 const char *name = NULL;
395
396 if (file) {
397- struct inode *inode = file_inode(vma->vm_file);
398+ struct inode *inode;
076b876e 399+
fb47a38f
JR
400+ file = vma_pr_or_file(vma);
401+ inode = file_inode(file);
402 dev = inode->i_sb->s_dev;
403 ino = inode->i_ino;
404 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
cd7a4cd9 405@@ -1734,7 +1737,7 @@ static int show_numa_map(struct seq_file *m, void *v, int is_pid)
076b876e
AM
406 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
407 struct vm_area_struct *vma = v;
408 struct numa_maps *md = &numa_priv->md;
409- struct file *file = vma->vm_file;
410+ struct file *file = vma_pr_or_file(vma);
076b876e 411 struct mm_struct *mm = vma->vm_mm;
7e9cd9fe
AM
412 struct mm_walk walk = {
413 .hugetlb_entry = gather_hugetlb_stats,
fb47a38f 414diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
2121bcd9 415index 5b62f57..dfb4a3b 100644
fb47a38f
JR
416--- a/fs/proc/task_nommu.c
417+++ b/fs/proc/task_nommu.c
2121bcd9 418@@ -156,7 +156,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma,
fb47a38f
JR
419 file = vma->vm_file;
420
421 if (file) {
422- struct inode *inode = file_inode(vma->vm_file);
423+ struct inode *inode;
076b876e 424+
b912730e 425+ file = vma_pr_or_file(vma);
fb47a38f
JR
426+ inode = file_inode(file);
427 dev = inode->i_sb->s_dev;
428 ino = inode->i_ino;
429 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
430diff --git a/include/linux/mm.h b/include/linux/mm.h
cd7a4cd9 431index d3a3842..c4664f2 100644
fb47a38f
JR
432--- a/include/linux/mm.h
433+++ b/include/linux/mm.h
cd7a4cd9 434@@ -1431,6 +1431,28 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
a3232e84 435 unmap_mapping_range(mapping, holebegin, holelen, 0);
fb47a38f 436 }
fb47a38f 437
076b876e
AM
438+extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
439+extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
f2c43d5f 440+ int);
076b876e
AM
441+extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
442+extern void vma_do_fput(struct vm_area_struct *, const char[], int);
fb47a38f 443+
f2c43d5f
AM
444+#define vma_file_update_time(vma) vma_do_file_update_time(vma, __func__, \
445+ __LINE__)
446+#define vma_pr_or_file(vma) vma_do_pr_or_file(vma, __func__, \
447+ __LINE__)
448+#define vma_get_file(vma) vma_do_get_file(vma, __func__, __LINE__)
449+#define vma_fput(vma) vma_do_fput(vma, __func__, __LINE__)
b912730e
AM
450+
451+#ifndef CONFIG_MMU
076b876e
AM
452+extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
453+extern void vmr_do_fput(struct vm_region *, const char[], int);
454+
f2c43d5f
AM
455+#define vmr_pr_or_file(region) vmr_do_pr_or_file(region, __func__, \
456+ __LINE__)
457+#define vmr_fput(region) vmr_do_fput(region, __func__, __LINE__)
b912730e 458+#endif /* !CONFIG_MMU */
fb47a38f 459+
a3232e84
JR
460 extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
461 void *buf, int len, unsigned int gup_flags);
fb47a38f 462 extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
fb47a38f 463diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
cd7a4cd9 464index 99ce070..8babe35 100644
fb47a38f
JR
465--- a/include/linux/mm_types.h
466+++ b/include/linux/mm_types.h
cd7a4cd9 467@@ -236,6 +236,7 @@ struct vm_region {
fb47a38f
JR
468 unsigned long vm_top; /* region allocated to here */
469 unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */
470 struct file *vm_file; /* the backing file or NULL */
471+ struct file *vm_prfile; /* the virtual backing file or NULL */
472
473 int vm_usage; /* region usage count (access under nommu_region_sem) */
474 bool vm_icache_flushed : 1; /* true if the icache has been flushed for
cd7a4cd9 475@@ -310,6 +311,7 @@ struct vm_area_struct {
fb47a38f 476 unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
5afbbe0d 477 units */
fb47a38f
JR
478 struct file * vm_file; /* File we map to (can be NULL). */
479+ struct file *vm_prfile; /* shadow of vm_file */
480 void * vm_private_data; /* was vm_pte (shared mem) */
481
8b6a4947 482 atomic_long_t swap_readahead_info;
fb47a38f 483diff --git a/kernel/fork.c b/kernel/fork.c
cd7a4cd9 484index a191c05..d88ee5a 100644
fb47a38f
JR
485--- a/kernel/fork.c
486+++ b/kernel/fork.c
cd7a4cd9 487@@ -506,7 +506,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
fb47a38f
JR
488 struct inode *inode = file_inode(file);
489 struct address_space *mapping = file->f_mapping;
490
491- get_file(file);
492+ vma_get_file(tmp);
493 if (tmp->vm_flags & VM_DENYWRITE)
494 atomic_dec(&inode->i_writecount);
2000de60 495 i_mmap_lock_write(mapping);
076b876e 496diff --git a/mm/Makefile b/mm/Makefile
cd7a4cd9 497index 8716bda..68afd6d 100644
076b876e
AM
498--- a/mm/Makefile
499+++ b/mm/Makefile
521ced18 500@@ -39,7 +39,7 @@ obj-y := filemap.o mempool.o oom_kill.o \
076b876e 501 mm_init.o mmu_context.o percpu.o slab_common.o \
b00004a5 502 compaction.o vmacache.o \
076b876e 503 interval_tree.o list_lru.o workingset.o \
7e9cd9fe
AM
504- debug.o $(mmu-y)
505+ prfile.o debug.o $(mmu-y)
076b876e
AM
506
507 obj-y += init-mm.o
508
fb47a38f 509diff --git a/mm/filemap.c b/mm/filemap.c
cd7a4cd9 510index 52517f2..250f675 100644
fb47a38f
JR
511--- a/mm/filemap.c
512+++ b/mm/filemap.c
cd7a4cd9
AM
513@@ -2700,7 +2700,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
514 vm_fault_t ret = VM_FAULT_LOCKED;
fb47a38f
JR
515
516 sb_start_pagefault(inode->i_sb);
521ced18
JR
517- file_update_time(vmf->vma->vm_file);
518+ vma_file_update_time(vmf->vma);
fb47a38f
JR
519 lock_page(page);
520 if (page->mapping != inode->i_mapping) {
521 unlock_page(page);
fb47a38f 522diff --git a/mm/mmap.c b/mm/mmap.c
cd7a4cd9 523index ff1944d..88abe2d 100644
fb47a38f
JR
524--- a/mm/mmap.c
525+++ b/mm/mmap.c
c4adf169 526@@ -180,7 +180,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
fb47a38f
JR
527 if (vma->vm_ops && vma->vm_ops->close)
528 vma->vm_ops->close(vma);
529 if (vma->vm_file)
530- fput(vma->vm_file);
531+ vma_fput(vma);
532 mpol_put(vma_policy(vma));
cd7a4cd9 533 vm_area_free(vma);
fb47a38f 534 return next;
c4adf169 535@@ -905,7 +905,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
fb47a38f
JR
536 if (remove_next) {
537 if (file) {
538 uprobe_munmap(next, next->vm_start, next->vm_end);
539- fput(file);
540+ vma_fput(vma);
541 }
542 if (next->anon_vma)
543 anon_vma_merge(vma, next);
cd7a4cd9 544@@ -1818,8 +1818,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
35939ee7
JR
545 return addr;
546
fb47a38f 547 unmap_and_free_vma:
fb47a38f
JR
548+ vma_fput(vma);
549 vma->vm_file = NULL;
550- fput(file);
551
552 /* Undo any partial mapping done by a device driver. */
553 unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
cd7a4cd9 554@@ -2638,7 +2638,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
fb47a38f
JR
555 goto out_free_mpol;
556
557 if (new->vm_file)
558- get_file(new->vm_file);
559+ vma_get_file(new);
560
561 if (new->vm_ops && new->vm_ops->open)
562 new->vm_ops->open(new);
cd7a4cd9 563@@ -2657,7 +2657,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
fb47a38f
JR
564 if (new->vm_ops && new->vm_ops->close)
565 new->vm_ops->close(new);
566 if (new->vm_file)
567- fput(new->vm_file);
568+ vma_fput(new);
569 unlink_anon_vmas(new);
570 out_free_mpol:
571 mpol_put(vma_policy(new));
cd7a4cd9 572@@ -2819,7 +2819,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
7e9cd9fe
AM
573 struct vm_area_struct *vma;
574 unsigned long populate = 0;
575 unsigned long ret = -EINVAL;
576- struct file *file;
5afbbe0d 577+ struct file *file, *prfile;
7e9cd9fe 578
cd7a4cd9 579 pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.rst.\n",
5afbbe0d 580 current->comm, current->pid);
cd7a4cd9 581@@ -2894,10 +2894,27 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
febd17d6 582 }
7e9cd9fe
AM
583 }
584
585- file = get_file(vma->vm_file);
586+ vma_get_file(vma);
5afbbe0d
AM
587+ file = vma->vm_file;
588+ prfile = vma->vm_prfile;
7e9cd9fe 589 ret = do_mmap_pgoff(vma->vm_file, start, size,
521ced18 590 prot, flags, pgoff, &populate, NULL);
5afbbe0d
AM
591+ if (!IS_ERR_VALUE(ret) && file && prfile) {
592+ struct vm_area_struct *new_vma;
593+
594+ new_vma = find_vma(mm, ret);
595+ if (!new_vma->vm_prfile)
596+ new_vma->vm_prfile = prfile;
597+ if (new_vma != vma)
598+ get_file(prfile);
599+ }
600+ /*
601+ * two fput()s instead of vma_fput(vma),
602+ * coz vma may not be available anymore.
603+ */
604 fput(file);
605+ if (prfile)
606+ fput(prfile);
7e9cd9fe
AM
607 out:
608 up_write(&mm->mmap_sem);
609 if (populate)
cd7a4cd9 610@@ -3204,7 +3221,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
79b8bda9
AM
611 if (anon_vma_clone(new_vma, vma))
612 goto out_free_mempol;
613 if (new_vma->vm_file)
614- get_file(new_vma->vm_file);
615+ vma_get_file(new_vma);
616 if (new_vma->vm_ops && new_vma->vm_ops->open)
617 new_vma->vm_ops->open(new_vma);
618 vma_link(mm, new_vma, prev, rb_link, rb_parent);
fb47a38f 619diff --git a/mm/nommu.c b/mm/nommu.c
cd7a4cd9 620index 1d22fdb..2b608be 100644
fb47a38f
JR
621--- a/mm/nommu.c
622+++ b/mm/nommu.c
b00004a5 623@@ -629,7 +629,7 @@ static void __put_nommu_region(struct vm_region *region)
fb47a38f
JR
624 up_write(&nommu_region_sem);
625
626 if (region->vm_file)
627- fput(region->vm_file);
628+ vmr_fput(region);
629
630 /* IO memory and memory shared directly out of the pagecache
631 * from ramfs/tmpfs mustn't be released here */
b00004a5 632@@ -767,7 +767,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
fb47a38f
JR
633 if (vma->vm_ops && vma->vm_ops->close)
634 vma->vm_ops->close(vma);
635 if (vma->vm_file)
636- fput(vma->vm_file);
637+ vma_fput(vma);
638 put_nommu_region(vma->vm_region);
cd7a4cd9 639 vm_area_free(vma);
fb47a38f 640 }
cd7a4cd9 641@@ -1288,7 +1288,7 @@ unsigned long do_mmap(struct file *file,
fb47a38f
JR
642 goto error_just_free;
643 }
644 }
645- fput(region->vm_file);
646+ vmr_fput(region);
647 kmem_cache_free(vm_region_jar, region);
648 region = pregion;
649 result = start;
cd7a4cd9 650@@ -1363,7 +1363,7 @@ unsigned long do_mmap(struct file *file,
fb47a38f
JR
651 up_write(&nommu_region_sem);
652 error:
653 if (region->vm_file)
654- fput(region->vm_file);
655+ vmr_fput(region);
656 kmem_cache_free(vm_region_jar, region);
657 if (vma->vm_file)
cd7a4cd9 658 fput(vma->vm_file);
076b876e
AM
659diff --git a/mm/prfile.c b/mm/prfile.c
660new file mode 100644
cd7a4cd9 661index 0000000..a27ac36
076b876e
AM
662--- /dev/null
663+++ b/mm/prfile.c
2121bcd9 664@@ -0,0 +1,86 @@
cd7a4cd9 665+// SPDX-License-Identifier: GPL-2.0
076b876e 666+/*
1c60b727
AM
667+ * Mainly for aufs which mmap(2) different file and wants to print different
668+ * path in /proc/PID/maps.
076b876e
AM
669+ * Call these functions via macros defined in linux/mm.h.
670+ *
671+ * See Documentation/filesystems/aufs/design/06mmap.txt
672+ *
b00004a5 673+ * Copyright (c) 2014-2018 Junjro R. Okajima
076b876e
AM
674+ * Copyright (c) 2014 Ian Campbell
675+ */
676+
677+#include <linux/mm.h>
678+#include <linux/file.h>
679+#include <linux/fs.h>
680+
681+/* #define PRFILE_TRACE */
682+static inline void prfile_trace(struct file *f, struct file *pr,
683+ const char func[], int line, const char func2[])
684+{
685+#ifdef PRFILE_TRACE
686+ if (pr)
1c60b727 687+ pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
076b876e
AM
688+#endif
689+}
690+
076b876e
AM
691+void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
692+ int line)
693+{
694+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
695+
696+ prfile_trace(f, pr, func, line, __func__);
697+ file_update_time(f);
698+ if (f && pr)
699+ file_update_time(pr);
700+}
701+
702+struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
703+ int line)
704+{
705+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
706+
707+ prfile_trace(f, pr, func, line, __func__);
708+ return (f && pr) ? pr : f;
709+}
710+
711+void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
712+{
713+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
714+
715+ prfile_trace(f, pr, func, line, __func__);
716+ get_file(f);
717+ if (f && pr)
718+ get_file(pr);
719+}
720+
721+void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
722+{
723+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
724+
725+ prfile_trace(f, pr, func, line, __func__);
726+ fput(f);
727+ if (f && pr)
728+ fput(pr);
729+}
b912730e
AM
730+
731+#ifndef CONFIG_MMU
076b876e
AM
732+struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
733+ int line)
734+{
735+ struct file *f = region->vm_file, *pr = region->vm_prfile;
736+
737+ prfile_trace(f, pr, func, line, __func__);
738+ return (f && pr) ? pr : f;
739+}
740+
741+void vmr_do_fput(struct vm_region *region, const char func[], int line)
742+{
743+ struct file *f = region->vm_file, *pr = region->vm_prfile;
744+
745+ prfile_trace(f, pr, func, line, __func__);
746+ fput(f);
747+ if (f && pr)
748+ fput(pr);
749+}
b912730e 750+#endif /* !CONFIG_MMU */
2121bcd9 751SPDX-License-Identifier: GPL-2.0
cd7a4cd9 752aufs4.x-rcN standalone patch
7f207e10 753
c1595e42 754diff --git a/fs/dcache.c b/fs/dcache.c
cd7a4cd9 755index 88de3b6..de0091a 100644
c1595e42
JR
756--- a/fs/dcache.c
757+++ b/fs/dcache.c
cd7a4cd9 758@@ -1345,6 +1345,7 @@ void d_walk(struct dentry *parent, void *data,
c1595e42
JR
759 seq = 1;
760 goto again;
761 }
febd17d6 762+EXPORT_SYMBOL_GPL(d_walk);
c1595e42 763
a2654f78
AM
764 struct check_mount {
765 struct vfsmount *mnt;
cd7a4cd9 766@@ -2891,6 +2892,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
f2c43d5f
AM
767
768 write_sequnlock(&rename_lock);
769 }
770+EXPORT_SYMBOL_GPL(d_exchange);
771
772 /**
773 * d_ancestor - search for an ancestor
79b8bda9 774diff --git a/fs/exec.c b/fs/exec.c
cd7a4cd9 775index 72e961a..adc7329 100644
79b8bda9
AM
776--- a/fs/exec.c
777+++ b/fs/exec.c
521ced18 778@@ -109,6 +109,7 @@ bool path_noexec(const struct path *path)
79b8bda9
AM
779 return (path->mnt->mnt_flags & MNT_NOEXEC) ||
780 (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
781 }
febd17d6 782+EXPORT_SYMBOL_GPL(path_noexec);
79b8bda9
AM
783
784 #ifdef CONFIG_USELIB
785 /*
febd17d6 786diff --git a/fs/fcntl.c b/fs/fcntl.c
cd7a4cd9 787index 545e686..0237ad7 100644
febd17d6
JR
788--- a/fs/fcntl.c
789+++ b/fs/fcntl.c
2121bcd9 790@@ -85,6 +85,7 @@ int setfl(int fd, struct file * filp, unsigned long arg)
febd17d6
JR
791 out:
792 return error;
793 }
794+EXPORT_SYMBOL_GPL(setfl);
795
796 static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
797 int force)
b912730e 798diff --git a/fs/file_table.c b/fs/file_table.c
be118d29 799index 7ec0b3e..819ee07 100644
b912730e
AM
800--- a/fs/file_table.c
801+++ b/fs/file_table.c
be118d29 802@@ -147,6 +147,7 @@ struct file *get_empty_filp(void)
b912730e
AM
803 }
804 return ERR_PTR(-ENFILE);
805 }
febd17d6 806+EXPORT_SYMBOL_GPL(get_empty_filp);
b912730e
AM
807
808 /**
809 * alloc_file - allocate and initialize a 'struct file'
be118d29 810@@ -257,6 +258,7 @@ void flush_delayed_fput(void)
8cdd5066
JR
811 {
812 delayed_fput(NULL);
813 }
febd17d6 814+EXPORT_SYMBOL_GPL(flush_delayed_fput);
8cdd5066
JR
815
816 static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput);
817
be118d29 818@@ -299,6 +301,7 @@ void __fput_sync(struct file *file)
8cdd5066
JR
819 }
820
821 EXPORT_SYMBOL(fput);
febd17d6 822+EXPORT_SYMBOL_GPL(__fput_sync);
8cdd5066
JR
823
824 void put_filp(struct file *file)
825 {
be118d29 826@@ -307,6 +310,7 @@ void put_filp(struct file *file)
b912730e
AM
827 file_free(file);
828 }
829 }
febd17d6 830+EXPORT_SYMBOL_GPL(put_filp);
b912730e 831
79b8bda9 832 void __init files_init(void)
8b6a4947 833 {
5afbbe0d 834diff --git a/fs/inode.c b/fs/inode.c
cd7a4cd9 835index af894e7..169811b 100644
5afbbe0d
AM
836--- a/fs/inode.c
837+++ b/fs/inode.c
cd7a4cd9 838@@ -1658,6 +1658,7 @@ int update_time(struct inode *inode, struct timespec64 *time, int flags)
5afbbe0d
AM
839
840 return update_time(inode, time, flags);
841 }
842+EXPORT_SYMBOL_GPL(update_time);
843
844 /**
845 * touch_atime - update the access time
7f207e10 846diff --git a/fs/namespace.c b/fs/namespace.c
cd7a4cd9 847index ee649e0..10923cb 100644
7f207e10
AM
848--- a/fs/namespace.c
849+++ b/fs/namespace.c
8b6a4947 850@@ -517,6 +517,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
c06a8ce3
AM
851 mnt_dec_writers(real_mount(mnt));
852 preempt_enable();
853 }
854+EXPORT_SYMBOL_GPL(__mnt_drop_write);
855
856 /**
857 * mnt_drop_write - give up write access to a mount
8b6a4947
AM
858@@ -851,6 +852,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
859 {
860 return check_mnt(real_mount(mnt));
861 }
862+EXPORT_SYMBOL_GPL(is_current_mnt_ns);
863
864 /*
865 * vfsmount lock must be held for write
b00004a5 866@@ -1893,6 +1895,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
7f207e10
AM
867 }
868 return 0;
869 }
febd17d6 870+EXPORT_SYMBOL_GPL(iterate_mounts);
7f207e10 871
7eafdf33 872 static void cleanup_group_ids(struct mount *mnt, struct mount *end)
7f207e10
AM
873 {
874diff --git a/fs/notify/group.c b/fs/notify/group.c
cd7a4cd9 875index aa5468f2..b38d224 100644
7f207e10
AM
876--- a/fs/notify/group.c
877+++ b/fs/notify/group.c
878@@ -22,6 +22,7 @@
879 #include <linux/srcu.h>
880 #include <linux/rculist.h>
881 #include <linux/wait.h>
882+#include <linux/module.h>
883
884 #include <linux/fsnotify_backend.h>
885 #include "fsnotify.h"
ffa93bbd 886@@ -109,6 +110,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
1716fcea 887 {
2121bcd9 888 refcount_inc(&group->refcnt);
1716fcea 889 }
febd17d6 890+EXPORT_SYMBOL_GPL(fsnotify_get_group);
1716fcea
AM
891
892 /*
893 * Drop a reference to a group. Free it if it's through.
ffa93bbd 894@@ -118,6 +120,7 @@ void fsnotify_put_group(struct fsnotify_group *group)
2121bcd9 895 if (refcount_dec_and_test(&group->refcnt))
1716fcea 896 fsnotify_final_destroy_group(group);
7f207e10 897 }
febd17d6 898+EXPORT_SYMBOL_GPL(fsnotify_put_group);
7f207e10
AM
899
900 /*
901 * Create a new fsnotify_group and hold a reference for the group returned.
ffa93bbd 902@@ -147,6 +150,7 @@ struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops)
7f207e10
AM
903
904 return group;
905 }
febd17d6 906+EXPORT_SYMBOL_GPL(fsnotify_alloc_group);
1716fcea
AM
907
908 int fsnotify_fasync(int fd, struct file *file, int on)
909 {
7f207e10 910diff --git a/fs/notify/mark.c b/fs/notify/mark.c
cd7a4cd9 911index 61f4c5f..855c655 100644
7f207e10
AM
912--- a/fs/notify/mark.c
913+++ b/fs/notify/mark.c
cd7a4cd9
AM
914@@ -245,6 +245,7 @@ void fsnotify_put_mark(struct fsnotify_mark *mark)
915 queue_delayed_work(system_unbound_wq, &reaper_work,
916 FSNOTIFY_REAPER_DELAY);
7f207e10 917 }
febd17d6 918+EXPORT_SYMBOL_GPL(fsnotify_put_mark);
7f207e10 919
cd7a4cd9
AM
920 /*
921 * Get mark reference when we found the mark via lockless traversal of object
922@@ -399,6 +400,7 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark,
1716fcea 923 mutex_unlock(&group->mark_mutex);
79b8bda9 924 fsnotify_free_mark(mark);
7f207e10 925 }
febd17d6 926+EXPORT_SYMBOL_GPL(fsnotify_destroy_mark);
7f207e10 927
ffa93bbd
AM
928 /*
929 * Sorting function for lists of fsnotify marks.
cd7a4cd9
AM
930@@ -624,6 +626,7 @@ int fsnotify_add_mark(struct fsnotify_mark *mark, struct inode *inode,
931 mutex_unlock(&group->mark_mutex);
7f207e10
AM
932 return ret;
933 }
febd17d6 934+EXPORT_SYMBOL_GPL(fsnotify_add_mark);
7f207e10 935
cd7a4cd9
AM
936 /*
937 * Given a list of marks, find the mark associated with given group. If found
938@@ -747,6 +750,7 @@ void fsnotify_init_mark(struct fsnotify_mark *mark,
ffa93bbd
AM
939 fsnotify_get_group(group);
940 mark->group = group;
7f207e10 941 }
febd17d6 942+EXPORT_SYMBOL_GPL(fsnotify_init_mark);
7f207e10 943
5afbbe0d
AM
944 /*
945 * Destroy all marks in destroy_list, waits for SRCU period to finish before
7f207e10 946diff --git a/fs/open.c b/fs/open.c
c4adf169 947index d0e955b..527bc1a 100644
7f207e10
AM
948--- a/fs/open.c
949+++ b/fs/open.c
c2c0f25c 950@@ -64,6 +64,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
febd17d6 951 inode_unlock(dentry->d_inode);
7f207e10
AM
952 return ret;
953 }
febd17d6 954+EXPORT_SYMBOL_GPL(do_truncate);
7f207e10 955
5afbbe0d 956 long vfs_truncate(const struct path *path, loff_t length)
7f207e10 957 {
c4adf169
AM
958@@ -723,6 +724,7 @@ SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
959 {
960 return ksys_fchown(fd, user, group);
961 }
962+EXPORT_SYMBOL_GPL(open_check_o_direct);
963
964 int open_check_o_direct(struct file *f)
965 {
5527c038 966diff --git a/fs/read_write.c b/fs/read_write.c
cd7a4cd9 967index ccc5bc8..36f52f4 100644
5527c038
JR
968--- a/fs/read_write.c
969+++ b/fs/read_write.c
b00004a5 970@@ -459,6 +459,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
8b6a4947
AM
971
972 return ret;
973 }
974+EXPORT_SYMBOL_GPL(vfs_read);
975
976 static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
977 {
b00004a5 978@@ -499,6 +500,7 @@ vfs_readf_t vfs_readf(struct file *file)
5527c038
JR
979 return new_sync_read;
980 return ERR_PTR(-ENOSYS);
981 }
febd17d6 982+EXPORT_SYMBOL_GPL(vfs_readf);
5527c038
JR
983
984 vfs_writef_t vfs_writef(struct file *file)
985 {
b00004a5 986@@ -510,6 +512,7 @@ vfs_writef_t vfs_writef(struct file *file)
5527c038
JR
987 return new_sync_write;
988 return ERR_PTR(-ENOSYS);
989 }
febd17d6 990+EXPORT_SYMBOL_GPL(vfs_writef);
5527c038 991
8b6a4947
AM
992 ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
993 {
b00004a5 994@@ -579,6 +582,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
8b6a4947
AM
995
996 return ret;
997 }
998+EXPORT_SYMBOL_GPL(vfs_write);
999
1000 static inline loff_t file_pos_read(struct file *file)
5527c038 1001 {
7f207e10 1002diff --git a/fs/splice.c b/fs/splice.c
cd7a4cd9 1003index 1dd7f96..a5e3bcb 100644
7f207e10
AM
1004--- a/fs/splice.c
1005+++ b/fs/splice.c
cd7a4cd9 1006@@ -851,6 +851,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
392086de
AM
1007
1008 return splice_write(pipe, out, ppos, len, flags);
7f207e10 1009 }
febd17d6 1010+EXPORT_SYMBOL_GPL(do_splice_from);
7f207e10
AM
1011
1012 /*
1013 * Attempt to initiate a splice from a file to a pipe.
cd7a4cd9 1014@@ -880,6 +881,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
7f207e10
AM
1015
1016 return splice_read(in, ppos, pipe, len, flags);
1017 }
febd17d6 1018+EXPORT_SYMBOL_GPL(do_splice_to);
7f207e10
AM
1019
1020 /**
1021 * splice_direct_to_actor - splices data directly between two non-pipes
a2654f78 1022diff --git a/fs/sync.c b/fs/sync.c
b00004a5 1023index 2860782..ffd7ea4 100644
a2654f78
AM
1024--- a/fs/sync.c
1025+++ b/fs/sync.c
2121bcd9 1026@@ -39,6 +39,7 @@ int __sync_filesystem(struct super_block *sb, int wait)
a2654f78
AM
1027 sb->s_op->sync_fs(sb, wait);
1028 return __sync_blockdev(sb->s_bdev, wait);
1029 }
1030+EXPORT_SYMBOL_GPL(__sync_filesystem);
1031
1032 /*
1033 * Write out and wait upon all dirty data associated with this
c1595e42 1034diff --git a/fs/xattr.c b/fs/xattr.c
cd7a4cd9 1035index f9cb1db..000b62b 100644
c1595e42
JR
1036--- a/fs/xattr.c
1037+++ b/fs/xattr.c
cd7a4cd9 1038@@ -296,6 +296,7 @@ vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
c1595e42
JR
1039 *xattr_value = value;
1040 return error;
1041 }
febd17d6 1042+EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
c1595e42 1043
febd17d6 1044 ssize_t
f2c43d5f 1045 __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
8b6a4947 1046diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
cd7a4cd9 1047index 720f070..4f58215 100644
8b6a4947
AM
1048--- a/kernel/locking/lockdep.c
1049+++ b/kernel/locking/lockdep.c
be118d29 1050@@ -151,6 +151,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
8b6a4947
AM
1051 }
1052 return lock_classes + hlock->class_idx - 1;
1053 }
1054+EXPORT_SYMBOL_GPL(lockdep_hlock_class);
1055 #define hlock_class(hlock) lockdep_hlock_class(hlock)
1056
1057 #ifdef CONFIG_LOCK_STAT
8cdd5066 1058diff --git a/kernel/task_work.c b/kernel/task_work.c
2121bcd9 1059index 0fef395..83fb1ec 100644
8cdd5066
JR
1060--- a/kernel/task_work.c
1061+++ b/kernel/task_work.c
2121bcd9 1062@@ -116,3 +116,4 @@ void task_work_run(void)
8cdd5066
JR
1063 } while (work);
1064 }
1065 }
febd17d6 1066+EXPORT_SYMBOL_GPL(task_work_run);
7f207e10 1067diff --git a/security/commoncap.c b/security/commoncap.c
cd7a4cd9 1068index f4c33ab..70563f1 100644
7f207e10
AM
1069--- a/security/commoncap.c
1070+++ b/security/commoncap.c
cd7a4cd9 1071@@ -1336,12 +1336,14 @@ int cap_mmap_addr(unsigned long addr)
94337f0d 1072 }
7f207e10
AM
1073 return ret;
1074 }
febd17d6 1075+EXPORT_SYMBOL_GPL(cap_mmap_addr);
0c3ec466
AM
1076
1077 int cap_mmap_file(struct file *file, unsigned long reqprot,
1078 unsigned long prot, unsigned long flags)
1079 {
1080 return 0;
1081 }
febd17d6 1082+EXPORT_SYMBOL_GPL(cap_mmap_file);
c2c0f25c
AM
1083
1084 #ifdef CONFIG_SECURITY
1085
7f207e10 1086diff --git a/security/device_cgroup.c b/security/device_cgroup.c
cd7a4cd9 1087index cd97929..424fd23 100644
7f207e10
AM
1088--- a/security/device_cgroup.c
1089+++ b/security/device_cgroup.c
2121bcd9 1090@@ -8,6 +8,7 @@
f6c5ef8b
AM
1091 #include <linux/device_cgroup.h>
1092 #include <linux/cgroup.h>
1093 #include <linux/ctype.h>
1094+#include <linux/export.h>
1095 #include <linux/list.h>
1096 #include <linux/uaccess.h>
1097 #include <linux/seq_file.h>
2121bcd9 1098@@ -824,3 +825,4 @@ int __devcgroup_check_permission(short type, u32 major, u32 minor,
7f207e10 1099
2121bcd9
AM
1100 return 0;
1101 }
1102+EXPORT_SYMBOL_GPL(__devcgroup_check_permission);
7f207e10 1103diff --git a/security/security.c b/security/security.c
cd7a4cd9 1104index 68f46d8..3bc1348 100644
7f207e10
AM
1105--- a/security/security.c
1106+++ b/security/security.c
b00004a5 1107@@ -537,6 +537,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
7f207e10 1108 return 0;
c2c0f25c 1109 return call_int_hook(path_rmdir, 0, dir, dentry);
7f207e10 1110 }
febd17d6 1111+EXPORT_SYMBOL_GPL(security_path_rmdir);
7f207e10 1112
5afbbe0d 1113 int security_path_unlink(const struct path *dir, struct dentry *dentry)
7f207e10 1114 {
b00004a5 1115@@ -553,6 +554,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
7f207e10 1116 return 0;
c2c0f25c 1117 return call_int_hook(path_symlink, 0, dir, dentry, old_name);
7f207e10 1118 }
febd17d6 1119+EXPORT_SYMBOL_GPL(security_path_symlink);
7f207e10 1120
5afbbe0d 1121 int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
7f207e10 1122 struct dentry *new_dentry)
b00004a5 1123@@ -561,6 +563,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
7f207e10 1124 return 0;
c2c0f25c 1125 return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
7f207e10 1126 }
febd17d6 1127+EXPORT_SYMBOL_GPL(security_path_link);
7f207e10 1128
5afbbe0d
AM
1129 int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
1130 const struct path *new_dir, struct dentry *new_dentry,
b00004a5 1131@@ -588,6 +591,7 @@ int security_path_truncate(const struct path *path)
7f207e10 1132 return 0;
c2c0f25c 1133 return call_int_hook(path_truncate, 0, path);
7f207e10 1134 }
febd17d6 1135+EXPORT_SYMBOL_GPL(security_path_truncate);
7f207e10 1136
5afbbe0d 1137 int security_path_chmod(const struct path *path, umode_t mode)
7eafdf33 1138 {
b00004a5 1139@@ -595,6 +599,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
7f207e10 1140 return 0;
c2c0f25c 1141 return call_int_hook(path_chmod, 0, path, mode);
7f207e10 1142 }
febd17d6 1143+EXPORT_SYMBOL_GPL(security_path_chmod);
7f207e10 1144
5afbbe0d 1145 int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
7f207e10 1146 {
b00004a5 1147@@ -602,6 +607,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
7f207e10 1148 return 0;
c2c0f25c 1149 return call_int_hook(path_chown, 0, path, uid, gid);
7f207e10 1150 }
febd17d6 1151+EXPORT_SYMBOL_GPL(security_path_chown);
7f207e10 1152
5afbbe0d 1153 int security_path_chroot(const struct path *path)
7f207e10 1154 {
b00004a5 1155@@ -687,6 +693,7 @@ int security_inode_readlink(struct dentry *dentry)
7f207e10 1156 return 0;
c2c0f25c 1157 return call_int_hook(inode_readlink, 0, dentry);
7f207e10 1158 }
febd17d6 1159+EXPORT_SYMBOL_GPL(security_inode_readlink);
7f207e10 1160
c2c0f25c
AM
1161 int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
1162 bool rcu)
b00004a5 1163@@ -702,6 +709,7 @@ int security_inode_permission(struct inode *inode, int mask)
7f207e10 1164 return 0;
c2c0f25c 1165 return call_int_hook(inode_permission, 0, inode, mask);
7f207e10 1166 }
febd17d6 1167+EXPORT_SYMBOL_GPL(security_inode_permission);
7f207e10 1168
1e00d052 1169 int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
7f207e10 1170 {
b00004a5 1171@@ -873,6 +881,7 @@ int security_file_permission(struct file *file, int mask)
7f207e10
AM
1172
1173 return fsnotify_perm(file, mask);
1174 }
febd17d6 1175+EXPORT_SYMBOL_GPL(security_file_permission);
7f207e10
AM
1176
1177 int security_file_alloc(struct file *file)
1178 {
b00004a5 1179@@ -932,6 +941,7 @@ int security_mmap_file(struct file *file, unsigned long prot,
7f207e10
AM
1180 return ret;
1181 return ima_file_mmap(file, prot);
1182 }
febd17d6 1183+EXPORT_SYMBOL_GPL(security_mmap_file);
7f207e10 1184
0c3ec466
AM
1185 int security_mmap_addr(unsigned long addr)
1186 {
7f207e10
AM
1187diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
1188--- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 1189+++ linux/Documentation/ABI/testing/debugfs-aufs 2018-08-12 23:43:05.450124426 +0200
062440b3 1190@@ -0,0 +1,55 @@
7f207e10
AM
1191+What: /debug/aufs/si_<id>/
1192+Date: March 2009
f6b6e03d 1193+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1194+Description:
1195+ Under /debug/aufs, a directory named si_<id> is created
1196+ per aufs mount, where <id> is a unique id generated
1197+ internally.
1facf9fc 1198+
86dc4139
AM
1199+What: /debug/aufs/si_<id>/plink
1200+Date: Apr 2013
f6b6e03d 1201+Contact: J. R. Okajima <hooanon05g@gmail.com>
86dc4139
AM
1202+Description:
1203+ It has three lines and shows the information about the
1204+ pseudo-link. The first line is a single number
1205+ representing a number of buckets. The second line is a
1206+ number of pseudo-links per buckets (separated by a
1207+ blank). The last line is a single number representing a
1208+ total number of psedo-links.
1209+ When the aufs mount option 'noplink' is specified, it
1210+ will show "1\n0\n0\n".
1211+
7f207e10
AM
1212+What: /debug/aufs/si_<id>/xib
1213+Date: March 2009
f6b6e03d 1214+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1215+Description:
1216+ It shows the consumed blocks by xib (External Inode Number
1217+ Bitmap), its block size and file size.
1218+ When the aufs mount option 'noxino' is specified, it
1219+ will be empty. About XINO files, see the aufs manual.
1220+
062440b3 1221+What: /debug/aufs/si_<id>/xi0, xi1 ... xiN and xiN-N
7f207e10 1222+Date: March 2009
f6b6e03d 1223+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1224+Description:
1225+ It shows the consumed blocks by xino (External Inode Number
1226+ Translation Table), its link count, block size and file
1227+ size.
062440b3
AM
1228+ Due to the file size limit, there may exist multiple
1229+ xino files per branch. In this case, "-N" is added to
1230+ the filename and it corresponds to the index of the
1231+ internal xino array. "-0" is omitted.
1232+ When the aufs mount option 'noxino' is specified, Those
1233+ entries won't exist. About XINO files, see the aufs
1234+ manual.
7f207e10
AM
1235+
1236+What: /debug/aufs/si_<id>/xigen
1237+Date: March 2009
f6b6e03d 1238+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1239+Description:
1240+ It shows the consumed blocks by xigen (External Inode
1241+ Generation Table), its block size and file size.
1242+ If CONFIG_AUFS_EXPORT is disabled, this entry will not
1243+ be created.
1244+ When the aufs mount option 'noxino' is specified, it
1245+ will be empty. About XINO files, see the aufs manual.
1246diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
1247--- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs 1970-01-01 01:00:00.000000000 +0100
b00004a5 1248+++ linux/Documentation/ABI/testing/sysfs-aufs 2017-07-29 12:14:25.893041746 +0200
392086de 1249@@ -0,0 +1,31 @@
7f207e10
AM
1250+What: /sys/fs/aufs/si_<id>/
1251+Date: March 2009
f6b6e03d 1252+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1253+Description:
1254+ Under /sys/fs/aufs, a directory named si_<id> is created
1255+ per aufs mount, where <id> is a unique id generated
1256+ internally.
1257+
1258+What: /sys/fs/aufs/si_<id>/br0, br1 ... brN
1259+Date: March 2009
f6b6e03d 1260+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1261+Description:
1262+ It shows the abolute path of a member directory (which
1263+ is called branch) in aufs, and its permission.
1264+
392086de
AM
1265+What: /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
1266+Date: July 2013
f6b6e03d 1267+Contact: J. R. Okajima <hooanon05g@gmail.com>
392086de
AM
1268+Description:
1269+ It shows the id of a member directory (which is called
1270+ branch) in aufs.
1271+
7f207e10
AM
1272+What: /sys/fs/aufs/si_<id>/xi_path
1273+Date: March 2009
f6b6e03d 1274+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1275+Description:
1276+ It shows the abolute path of XINO (External Inode Number
1277+ Bitmap, Translation Table and Generation Table) file
1278+ even if it is the default path.
1279+ When the aufs mount option 'noxino' is specified, it
1280+ will be empty. About XINO files, see the aufs manual.
53392da6
AM
1281diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1282--- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 1283+++ linux/Documentation/filesystems/aufs/design/01intro.txt 2018-04-15 08:49:13.394483860 +0200
1c60b727 1284@@ -0,0 +1,171 @@
53392da6 1285+
b00004a5 1286+# Copyright (C) 2005-2018 Junjiro R. Okajima
53392da6
AM
1287+#
1288+# This program is free software; you can redistribute it and/or modify
1289+# it under the terms of the GNU General Public License as published by
1290+# the Free Software Foundation; either version 2 of the License, or
1291+# (at your option) any later version.
1292+#
1293+# This program is distributed in the hope that it will be useful,
1294+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1295+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1296+# GNU General Public License for more details.
1297+#
1298+# You should have received a copy of the GNU General Public License
523b37e3 1299+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1300+
1301+Introduction
1302+----------------------------------------
1303+
3c1bdaff 1304+aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
53392da6
AM
1305+1. abbrev. for "advanced multi-layered unification filesystem".
1306+2. abbrev. for "another unionfs".
1307+3. abbrev. for "auf das" in German which means "on the" in English.
1308+ Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1309+ But "Filesystem aufs Filesystem" is hard to understand.
1c60b727 1310+4. abbrev. for "African Urban Fashion Show".
53392da6
AM
1311+
1312+AUFS is a filesystem with features:
1313+- multi layered stackable unification filesystem, the member directory
1314+ is called as a branch.
1315+- branch permission and attribute, 'readonly', 'real-readonly',
7e9cd9fe 1316+ 'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
53392da6
AM
1317+ combination.
1318+- internal "file copy-on-write".
1319+- logical deletion, whiteout.
1320+- dynamic branch manipulation, adding, deleting and changing permission.
1321+- allow bypassing aufs, user's direct branch access.
1322+- external inode number translation table and bitmap which maintains the
1323+ persistent aufs inode number.
1324+- seekable directory, including NFS readdir.
1325+- file mapping, mmap and sharing pages.
1326+- pseudo-link, hardlink over branches.
1327+- loopback mounted filesystem as a branch.
1328+- several policies to select one among multiple writable branches.
1329+- revert a single systemcall when an error occurs in aufs.
1330+- and more...
1331+
1332+
1333+Multi Layered Stackable Unification Filesystem
1334+----------------------------------------------------------------------
1335+Most people already knows what it is.
1336+It is a filesystem which unifies several directories and provides a
1337+merged single directory. When users access a file, the access will be
1338+passed/re-directed/converted (sorry, I am not sure which English word is
1339+correct) to the real file on the member filesystem. The member
1340+filesystem is called 'lower filesystem' or 'branch' and has a mode
1341+'readonly' and 'readwrite.' And the deletion for a file on the lower
1342+readonly branch is handled by creating 'whiteout' on the upper writable
1343+branch.
1344+
1345+On LKML, there have been discussions about UnionMount (Jan Blunck,
1346+Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1347+different approaches to implement the merged-view.
1348+The former tries putting it into VFS, and the latter implements as a
1349+separate filesystem.
1350+(If I misunderstand about these implementations, please let me know and
1351+I shall correct it. Because it is a long time ago when I read their
1352+source files last time).
1353+
1354+UnionMount's approach will be able to small, but may be hard to share
1355+branches between several UnionMount since the whiteout in it is
1356+implemented in the inode on branch filesystem and always
1357+shared. According to Bharata's post, readdir does not seems to be
1358+finished yet.
1359+There are several missing features known in this implementations such as
1360+- for users, the inode number may change silently. eg. copy-up.
1361+- link(2) may break by copy-up.
1362+- read(2) may get an obsoleted filedata (fstat(2) too).
1363+- fcntl(F_SETLK) may be broken by copy-up.
1364+- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1365+ open(O_RDWR).
1366+
7e9cd9fe
AM
1367+In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1368+merged into mainline. This is another implementation of UnionMount as a
1369+separated filesystem. All the limitations and known problems which
1370+UnionMount are equally inherited to "overlay" filesystem.
1371+
1372+Unionfs has a longer history. When I started implementing a stackable
1373+filesystem (Aug 2005), it already existed. It has virtual super_block,
1374+inode, dentry and file objects and they have an array pointing lower
1375+same kind objects. After contributing many patches for Unionfs, I
1376+re-started my project AUFS (Jun 2006).
53392da6
AM
1377+
1378+In AUFS, the structure of filesystem resembles to Unionfs, but I
1379+implemented my own ideas, approaches and enhancements and it became
1380+totally different one.
1381+
1382+Comparing DM snapshot and fs based implementation
1383+- the number of bytes to be copied between devices is much smaller.
1384+- the type of filesystem must be one and only.
1385+- the fs must be writable, no readonly fs, even for the lower original
1386+ device. so the compression fs will not be usable. but if we use
1387+ loopback mount, we may address this issue.
1388+ for instance,
1389+ mount /cdrom/squashfs.img /sq
1390+ losetup /sq/ext2.img
1391+ losetup /somewhere/cow
1392+ dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1393+- it will be difficult (or needs more operations) to extract the
1394+ difference between the original device and COW.
1395+- DM snapshot-merge may help a lot when users try merging. in the
1396+ fs-layer union, users will use rsync(1).
1397+
7e9cd9fe
AM
1398+You may want to read my old paper "Filesystems in LiveCD"
1399+(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
53392da6 1400+
7e9cd9fe
AM
1401+
1402+Several characters/aspects/persona of aufs
53392da6
AM
1403+----------------------------------------------------------------------
1404+
7e9cd9fe 1405+Aufs has several characters, aspects or persona.
53392da6
AM
1406+1. a filesystem, callee of VFS helper
1407+2. sub-VFS, caller of VFS helper for branches
1408+3. a virtual filesystem which maintains persistent inode number
1409+4. reader/writer of files on branches such like an application
1410+
1411+1. Callee of VFS Helper
1412+As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1413+unlink(2) from an application reaches sys_unlink() kernel function and
1414+then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1415+calls filesystem specific unlink operation. Actually aufs implements the
1416+unlink operation but it behaves like a redirector.
1417+
1418+2. Caller of VFS Helper for Branches
1419+aufs_unlink() passes the unlink request to the branch filesystem as if
1420+it were called from VFS. So the called unlink operation of the branch
1421+filesystem acts as usual. As a caller of VFS helper, aufs should handle
1422+every necessary pre/post operation for the branch filesystem.
1423+- acquire the lock for the parent dir on a branch
1424+- lookup in a branch
1425+- revalidate dentry on a branch
1426+- mnt_want_write() for a branch
1427+- vfs_unlink() for a branch
1428+- mnt_drop_write() for a branch
1429+- release the lock on a branch
1430+
1431+3. Persistent Inode Number
1432+One of the most important issue for a filesystem is to maintain inode
1433+numbers. This is particularly important to support exporting a
1434+filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1435+backend block device for its own. But some storage is necessary to
7e9cd9fe
AM
1436+keep and maintain the inode numbers. It may be a large space and may not
1437+suit to keep in memory. Aufs rents some space from its first writable
1438+branch filesystem (by default) and creates file(s) on it. These files
1439+are created by aufs internally and removed soon (currently) keeping
1440+opened.
53392da6
AM
1441+Note: Because these files are removed, they are totally gone after
1442+ unmounting aufs. It means the inode numbers are not persistent
1443+ across unmount or reboot. I have a plan to make them really
1444+ persistent which will be important for aufs on NFS server.
1445+
1446+4. Read/Write Files Internally (copy-on-write)
1447+Because a branch can be readonly, when you write a file on it, aufs will
1448+"copy-up" it to the upper writable branch internally. And then write the
1449+originally requested thing to the file. Generally kernel doesn't
1450+open/read/write file actively. In aufs, even a single write may cause a
1451+internal "file copy". This behaviour is very similar to cp(1) command.
1452+
1453+Some people may think it is better to pass such work to user space
1454+helper, instead of doing in kernel space. Actually I am still thinking
1455+about it. But currently I have implemented it in kernel space.
1456diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1457--- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 1458+++ linux/Documentation/filesystems/aufs/design/02struct.txt 2018-04-15 08:49:13.394483860 +0200
7e9cd9fe 1459@@ -0,0 +1,258 @@
53392da6 1460+
b00004a5 1461+# Copyright (C) 2005-2018 Junjiro R. Okajima
53392da6
AM
1462+#
1463+# This program is free software; you can redistribute it and/or modify
1464+# it under the terms of the GNU General Public License as published by
1465+# the Free Software Foundation; either version 2 of the License, or
1466+# (at your option) any later version.
1467+#
1468+# This program is distributed in the hope that it will be useful,
1469+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1470+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1471+# GNU General Public License for more details.
1472+#
1473+# You should have received a copy of the GNU General Public License
523b37e3 1474+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1475+
1476+Basic Aufs Internal Structure
1477+
1478+Superblock/Inode/Dentry/File Objects
1479+----------------------------------------------------------------------
1480+As like an ordinary filesystem, aufs has its own
1481+superblock/inode/dentry/file objects. All these objects have a
1482+dynamically allocated array and store the same kind of pointers to the
1483+lower filesystem, branch.
1484+For example, when you build a union with one readwrite branch and one
1485+readonly, mounted /au, /rw and /ro respectively.
1486+- /au = /rw + /ro
1487+- /ro/fileA exists but /rw/fileA
1488+
1489+Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1490+pointers are stored in a aufs dentry. The array in aufs dentry will be,
7e9cd9fe 1491+- [0] = NULL (because /rw/fileA doesn't exist)
53392da6
AM
1492+- [1] = /ro/fileA
1493+
1494+This style of an array is essentially same to the aufs
1495+superblock/inode/dentry/file objects.
1496+
1497+Because aufs supports manipulating branches, ie. add/delete/change
7e9cd9fe
AM
1498+branches dynamically, these objects has its own generation. When
1499+branches are changed, the generation in aufs superblock is
1500+incremented. And a generation in other object are compared when it is
1501+accessed. When a generation in other objects are obsoleted, aufs
1502+refreshes the internal array.
53392da6
AM
1503+
1504+
1505+Superblock
1506+----------------------------------------------------------------------
1507+Additionally aufs superblock has some data for policies to select one
1508+among multiple writable branches, XIB files, pseudo-links and kobject.
1509+See below in detail.
7e9cd9fe
AM
1510+About the policies which supports copy-down a directory, see
1511+wbr_policy.txt too.
53392da6
AM
1512+
1513+
1514+Branch and XINO(External Inode Number Translation Table)
1515+----------------------------------------------------------------------
1516+Every branch has its own xino (external inode number translation table)
1517+file. The xino file is created and unlinked by aufs internally. When two
1518+members of a union exist on the same filesystem, they share the single
1519+xino file.
1520+The struct of a xino file is simple, just a sequence of aufs inode
1521+numbers which is indexed by the lower inode number.
1522+In the above sample, assume the inode number of /ro/fileA is i111 and
1523+aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1524+4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1525+
1526+When the inode numbers are not contiguous, the xino file will be sparse
1527+which has a hole in it and doesn't consume as much disk space as it
1528+might appear. If your branch filesystem consumes disk space for such
1529+holes, then you should specify 'xino=' option at mounting aufs.
1530+
7e9cd9fe
AM
1531+Aufs has a mount option to free the disk blocks for such holes in XINO
1532+files on tmpfs or ramdisk. But it is not so effective actually. If you
1533+meet a problem of disk shortage due to XINO files, then you should try
1534+"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1535+The patch localizes the assignment inumbers per tmpfs-mount and avoid
1536+the holes in XINO files.
1537+
53392da6 1538+Also a writable branch has three kinds of "whiteout bases". All these
7e9cd9fe 1539+are existed when the branch is joined to aufs, and their names are
53392da6
AM
1540+whiteout-ed doubly, so that users will never see their names in aufs
1541+hierarchy.
7e9cd9fe 1542+1. a regular file which will be hardlinked to all whiteouts.
53392da6 1543+2. a directory to store a pseudo-link.
7e9cd9fe 1544+3. a directory to store an "orphan"-ed file temporary.
53392da6
AM
1545+
1546+1. Whiteout Base
1547+ When you remove a file on a readonly branch, aufs handles it as a
1548+ logical deletion and creates a whiteout on the upper writable branch
1549+ as a hardlink of this file in order not to consume inode on the
1550+ writable branch.
1551+2. Pseudo-link Dir
1552+ See below, Pseudo-link.
1553+3. Step-Parent Dir
1554+ When "fileC" exists on the lower readonly branch only and it is
1555+ opened and removed with its parent dir, and then user writes
1556+ something into it, then aufs copies-up fileC to this
1557+ directory. Because there is no other dir to store fileC. After
1558+ creating a file under this dir, the file is unlinked.
1559+
1560+Because aufs supports manipulating branches, ie. add/delete/change
7e9cd9fe
AM
1561+dynamically, a branch has its own id. When the branch order changes,
1562+aufs finds the new index by searching the branch id.
53392da6
AM
1563+
1564+
1565+Pseudo-link
1566+----------------------------------------------------------------------
1567+Assume "fileA" exists on the lower readonly branch only and it is
1568+hardlinked to "fileB" on the branch. When you write something to fileA,
1569+aufs copies-up it to the upper writable branch. Additionally aufs
1570+creates a hardlink under the Pseudo-link Directory of the writable
1571+branch. The inode of a pseudo-link is kept in aufs super_block as a
1572+simple list. If fileB is read after unlinking fileA, aufs returns
1573+filedata from the pseudo-link instead of the lower readonly
1574+branch. Because the pseudo-link is based upon the inode, to keep the
7e9cd9fe 1575+inode number by xino (see above) is essentially necessary.
53392da6
AM
1576+
1577+All the hardlinks under the Pseudo-link Directory of the writable branch
1578+should be restored in a proper location later. Aufs provides a utility
1579+to do this. The userspace helpers executed at remounting and unmounting
1580+aufs by default.
1581+During this utility is running, it puts aufs into the pseudo-link
1582+maintenance mode. In this mode, only the process which began the
1583+maintenance mode (and its child processes) is allowed to operate in
1584+aufs. Some other processes which are not related to the pseudo-link will
1585+be allowed to run too, but the rest have to return an error or wait
1586+until the maintenance mode ends. If a process already acquires an inode
1587+mutex (in VFS), it has to return an error.
1588+
1589+
1590+XIB(external inode number bitmap)
1591+----------------------------------------------------------------------
1592+Addition to the xino file per a branch, aufs has an external inode number
7e9cd9fe
AM
1593+bitmap in a superblock object. It is also an internal file such like a
1594+xino file.
53392da6
AM
1595+It is a simple bitmap to mark whether the aufs inode number is in-use or
1596+not.
1597+To reduce the file I/O, aufs prepares a single memory page to cache xib.
1598+
7e9cd9fe 1599+As well as XINO files, aufs has a feature to truncate/refresh XIB to
53392da6
AM
1600+reduce the number of consumed disk blocks for these files.
1601+
1602+
1603+Virtual or Vertical Dir, and Readdir in Userspace
1604+----------------------------------------------------------------------
1605+In order to support multiple layers (branches), aufs readdir operation
1606+constructs a virtual dir block on memory. For readdir, aufs calls
1607+vfs_readdir() internally for each dir on branches, merges their entries
1608+with eliminating the whiteout-ed ones, and sets it to file (dir)
1609+object. So the file object has its entry list until it is closed. The
1610+entry list will be updated when the file position is zero and becomes
7e9cd9fe 1611+obsoleted. This decision is made in aufs automatically.
53392da6
AM
1612+
1613+The dynamically allocated memory block for the name of entries has a
1614+unit of 512 bytes (by default) and stores the names contiguously (no
1615+padding). Another block for each entry is handled by kmem_cache too.
1616+During building dir blocks, aufs creates hash list and judging whether
1617+the entry is whiteouted by its upper branch or already listed.
1618+The merged result is cached in the corresponding inode object and
1619+maintained by a customizable life-time option.
1620+
1621+Some people may call it can be a security hole or invite DoS attack
1622+since the opened and once readdir-ed dir (file object) holds its entry
1623+list and becomes a pressure for system memory. But I'd say it is similar
1624+to files under /proc or /sys. The virtual files in them also holds a
1625+memory page (generally) while they are opened. When an idea to reduce
1626+memory for them is introduced, it will be applied to aufs too.
1627+For those who really hate this situation, I've developed readdir(3)
1628+library which operates this merging in userspace. You just need to set
1629+LD_PRELOAD environment variable, and aufs will not consume no memory in
1630+kernel space for readdir(3).
1631+
1632+
1633+Workqueue
1634+----------------------------------------------------------------------
1635+Aufs sometimes requires privilege access to a branch. For instance,
1636+in copy-up/down operation. When a user process is going to make changes
1637+to a file which exists in the lower readonly branch only, and the mode
1638+of one of ancestor directories may not be writable by a user
1639+process. Here aufs copy-up the file with its ancestors and they may
1640+require privilege to set its owner/group/mode/etc.
1641+This is a typical case of a application character of aufs (see
1642+Introduction).
1643+
1644+Aufs uses workqueue synchronously for this case. It creates its own
1645+workqueue. The workqueue is a kernel thread and has privilege. Aufs
1646+passes the request to call mkdir or write (for example), and wait for
1647+its completion. This approach solves a problem of a signal handler
1648+simply.
1649+If aufs didn't adopt the workqueue and changed the privilege of the
7e9cd9fe
AM
1650+process, then the process may receive the unexpected SIGXFSZ or other
1651+signals.
53392da6
AM
1652+
1653+Also aufs uses the system global workqueue ("events" kernel thread) too
1654+for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1655+whiteout base and etc. This is unrelated to a privilege.
1656+Most of aufs operation tries acquiring a rw_semaphore for aufs
1657+superblock at the beginning, at the same time waits for the completion
1658+of all queued asynchronous tasks.
1659+
1660+
1661+Whiteout
1662+----------------------------------------------------------------------
1663+The whiteout in aufs is very similar to Unionfs's. That is represented
1664+by its filename. UnionMount takes an approach of a file mode, but I am
1665+afraid several utilities (find(1) or something) will have to support it.
1666+
1667+Basically the whiteout represents "logical deletion" which stops aufs to
1668+lookup further, but also it represents "dir is opaque" which also stop
7e9cd9fe 1669+further lookup.
53392da6
AM
1670+
1671+In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1672+In order to make several functions in a single systemcall to be
1673+revertible, aufs adopts an approach to rename a directory to a temporary
1674+unique whiteouted name.
1675+For example, in rename(2) dir where the target dir already existed, aufs
1676+renames the target dir to a temporary unique whiteouted name before the
7e9cd9fe 1677+actual rename on a branch, and then handles other actions (make it opaque,
53392da6
AM
1678+update the attributes, etc). If an error happens in these actions, aufs
1679+simply renames the whiteouted name back and returns an error. If all are
1680+succeeded, aufs registers a function to remove the whiteouted unique
1681+temporary name completely and asynchronously to the system global
1682+workqueue.
1683+
1684+
1685+Copy-up
1686+----------------------------------------------------------------------
1687+It is a well-known feature or concept.
1688+When user modifies a file on a readonly branch, aufs operate "copy-up"
1689+internally and makes change to the new file on the upper writable branch.
1690+When the trigger systemcall does not update the timestamps of the parent
1691+dir, aufs reverts it after copy-up.
c2b27bf2
AM
1692+
1693+
1694+Move-down (aufs3.9 and later)
1695+----------------------------------------------------------------------
1696+"Copy-up" is one of the essential feature in aufs. It copies a file from
1697+the lower readonly branch to the upper writable branch when a user
1698+changes something about the file.
1699+"Move-down" is an opposite action of copy-up. Basically this action is
1700+ran manually instead of automatically and internally.
076b876e
AM
1701+For desgin and implementation, aufs has to consider these issues.
1702+- whiteout for the file may exist on the lower branch.
1703+- ancestor directories may not exist on the lower branch.
1704+- diropq for the ancestor directories may exist on the upper branch.
1705+- free space on the lower branch will reduce.
1706+- another access to the file may happen during moving-down, including
7e9cd9fe 1707+ UDBA (see "Revalidate Dentry and UDBA").
076b876e
AM
1708+- the file should not be hard-linked nor pseudo-linked. they should be
1709+ handled by auplink utility later.
c2b27bf2
AM
1710+
1711+Sometimes users want to move-down a file from the upper writable branch
1712+to the lower readonly or writable branch. For instance,
1713+- the free space of the upper writable branch is going to run out.
1714+- create a new intermediate branch between the upper and lower branch.
1715+- etc.
1716+
1717+For this purpose, use "aumvdown" command in aufs-util.git.
b912730e
AM
1718diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1719--- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 1720+++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt 2018-04-15 08:49:13.394483860 +0200
b912730e
AM
1721@@ -0,0 +1,85 @@
1722+
b00004a5 1723+# Copyright (C) 2015-2018 Junjiro R. Okajima
b912730e
AM
1724+#
1725+# This program is free software; you can redistribute it and/or modify
1726+# it under the terms of the GNU General Public License as published by
1727+# the Free Software Foundation; either version 2 of the License, or
1728+# (at your option) any later version.
1729+#
1730+# This program is distributed in the hope that it will be useful,
1731+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1732+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1733+# GNU General Public License for more details.
1734+#
1735+# You should have received a copy of the GNU General Public License
1736+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1737+
1738+Support for a branch who has its ->atomic_open()
1739+----------------------------------------------------------------------
1740+The filesystems who implement its ->atomic_open() are not majority. For
1741+example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1742+particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1743+->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1744+sure whether all filesystems who have ->atomic_open() behave like this,
1745+but NFSv4 surely returns the error.
1746+
1747+In order to support ->atomic_open() for aufs, there are a few
1748+approaches.
1749+
1750+A. Introduce aufs_atomic_open()
1751+ - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1752+ branch fs.
1753+B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1754+ an aufs user Pip Cet's approach
1755+ - calls aufs_create(), VFS finish_open() and notify_change().
1756+ - pass fake-mode to finish_open(), and then correct the mode by
1757+ notify_change().
1758+C. Extend aufs_open() to call branch fs's ->atomic_open()
1759+ - no aufs_atomic_open().
1760+ - aufs_lookup() registers the TID to an aufs internal object.
1761+ - aufs_create() does nothing when the matching TID is registered, but
1762+ registers the mode.
1763+ - aufs_open() calls branch fs's ->atomic_open() when the matching
1764+ TID is registered.
1765+D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1766+ credential
1767+ - no aufs_atomic_open().
1768+ - aufs_create() registers the TID to an internal object. this info
1769+ represents "this process created this file just now."
1770+ - when aufs gets EACCES from branch fs's ->open(), then confirm the
1771+ registered TID and re-try open() with superuser's credential.
1772+
1773+Pros and cons for each approach.
1774+
1775+A.
1776+ - straightforward but highly depends upon VFS internal.
1777+ - the atomic behavaiour is kept.
1778+ - some of parameters such as nameidata are hard to reproduce for
1779+ branch fs.
1780+ - large overhead.
1781+B.
1782+ - easy to implement.
1783+ - the atomic behavaiour is lost.
1784+C.
1785+ - the atomic behavaiour is kept.
1786+ - dirty and tricky.
1787+ - VFS checks whether the file is created correctly after calling
1788+ ->create(), which means this approach doesn't work.
1789+D.
1790+ - easy to implement.
1791+ - the atomic behavaiour is lost.
1792+ - to open a file with superuser's credential and give it to a user
1793+ process is a bad idea, since the file object keeps the credential
1794+ in it. It may affect LSM or something. This approach doesn't work
1795+ either.
1796+
1797+The approach A is ideal, but it hard to implement. So here is a
1798+variation of A, which is to be implemented.
1799+
1800+A-1. Introduce aufs_atomic_open()
1801+ - calls branch fs ->atomic_open() if exists. otherwise calls
1802+ vfs_create() and finish_open().
1803+ - the demerit is that the several checks after branch fs
1804+ ->atomic_open() are lost. in the ordinary case, the checks are
1805+ done by VFS:do_last(), lookup_open() and atomic_open(). some can
1806+ be implemented in aufs, but not all I am afraid.
53392da6
AM
1807diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1808--- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 1809+++ linux/Documentation/filesystems/aufs/design/03lookup.txt 2018-04-15 08:49:13.394483860 +0200
7e9cd9fe 1810@@ -0,0 +1,113 @@
53392da6 1811+
b00004a5 1812+# Copyright (C) 2005-2018 Junjiro R. Okajima
53392da6
AM
1813+#
1814+# This program is free software; you can redistribute it and/or modify
1815+# it under the terms of the GNU General Public License as published by
1816+# the Free Software Foundation; either version 2 of the License, or
1817+# (at your option) any later version.
1818+#
1819+# This program is distributed in the hope that it will be useful,
1820+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1821+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1822+# GNU General Public License for more details.
1823+#
1824+# You should have received a copy of the GNU General Public License
523b37e3 1825+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1826+
1827+Lookup in a Branch
1828+----------------------------------------------------------------------
1829+Since aufs has a character of sub-VFS (see Introduction), it operates
7e9cd9fe
AM
1830+lookup for branches as VFS does. It may be a heavy work. But almost all
1831+lookup operation in aufs is the simplest case, ie. lookup only an entry
1832+directly connected to its parent. Digging down the directory hierarchy
1833+is unnecessary. VFS has a function lookup_one_len() for that use, and
1834+aufs calls it.
1835+
1836+When a branch is a remote filesystem, aufs basically relies upon its
53392da6
AM
1837+->d_revalidate(), also aufs forces the hardest revalidate tests for
1838+them.
1839+For d_revalidate, aufs implements three levels of revalidate tests. See
1840+"Revalidate Dentry and UDBA" in detail.
1841+
1842+
076b876e
AM
1843+Test Only the Highest One for the Directory Permission (dirperm1 option)
1844+----------------------------------------------------------------------
1845+Let's try case study.
1846+- aufs has two branches, upper readwrite and lower readonly.
1847+ /au = /rw + /ro
1848+- "dirA" exists under /ro, but /rw. and its mode is 0700.
1849+- user invoked "chmod a+rx /au/dirA"
1850+- the internal copy-up is activated and "/rw/dirA" is created and its
7e9cd9fe 1851+ permission bits are set to world readable.
076b876e
AM
1852+- then "/au/dirA" becomes world readable?
1853+
1854+In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1855+or it may be a natively readonly filesystem. If aufs respects the lower
1856+branch, it should not respond readdir request from other users. But user
1857+allowed it by chmod. Should really aufs rejects showing the entries
1858+under /ro/dirA?
1859+
7e9cd9fe
AM
1860+To be honest, I don't have a good solution for this case. So aufs
1861+implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1862+users.
076b876e
AM
1863+When dirperm1 is specified, aufs checks only the highest one for the
1864+directory permission, and shows the entries. Otherwise, as usual, checks
1865+every dir existing on all branches and rejects the request.
1866+
1867+As a side effect, dirperm1 option improves the performance of aufs
1868+because the number of permission check is reduced when the number of
1869+branch is many.
1870+
1871+
53392da6
AM
1872+Revalidate Dentry and UDBA (User's Direct Branch Access)
1873+----------------------------------------------------------------------
1874+Generally VFS helpers re-validate a dentry as a part of lookup.
1875+0. digging down the directory hierarchy.
1876+1. lock the parent dir by its i_mutex.
1877+2. lookup the final (child) entry.
1878+3. revalidate it.
1879+4. call the actual operation (create, unlink, etc.)
1880+5. unlock the parent dir
1881+
1882+If the filesystem implements its ->d_revalidate() (step 3), then it is
1883+called. Actually aufs implements it and checks the dentry on a branch is
1884+still valid.
1885+But it is not enough. Because aufs has to release the lock for the
1886+parent dir on a branch at the end of ->lookup() (step 2) and
1887+->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1888+held by VFS.
1889+If the file on a branch is changed directly, eg. bypassing aufs, after
1890+aufs released the lock, then the subsequent operation may cause
1891+something unpleasant result.
1892+
1893+This situation is a result of VFS architecture, ->lookup() and
1894+->d_revalidate() is separated. But I never say it is wrong. It is a good
1895+design from VFS's point of view. It is just not suitable for sub-VFS
1896+character in aufs.
1897+
1898+Aufs supports such case by three level of revalidation which is
1899+selectable by user.
1900+1. Simple Revalidate
1901+ Addition to the native flow in VFS's, confirm the child-parent
1902+ relationship on the branch just after locking the parent dir on the
1903+ branch in the "actual operation" (step 4). When this validation
1904+ fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1905+ checks the validation of the dentry on branches.
1906+2. Monitor Changes Internally by Inotify/Fsnotify
1907+ Addition to above, in the "actual operation" (step 4) aufs re-lookup
1908+ the dentry on the branch, and returns EBUSY if it finds different
1909+ dentry.
1910+ Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1911+ during it is in cache. When the event is notified, aufs registers a
1912+ function to kernel 'events' thread by schedule_work(). And the
1913+ function sets some special status to the cached aufs dentry and inode
1914+ private data. If they are not cached, then aufs has nothing to
1915+ do. When the same file is accessed through aufs (step 0-3) later,
1916+ aufs will detect the status and refresh all necessary data.
1917+ In this mode, aufs has to ignore the event which is fired by aufs
1918+ itself.
1919+3. No Extra Validation
1920+ This is the simplest test and doesn't add any additional revalidation
7e9cd9fe 1921+ test, and skip the revalidation in step 4. It is useful and improves
53392da6
AM
1922+ aufs performance when system surely hide the aufs branches from user,
1923+ by over-mounting something (or another method).
1924diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1925--- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 1926+++ linux/Documentation/filesystems/aufs/design/04branch.txt 2018-04-15 08:49:13.394483860 +0200
7e9cd9fe 1927@@ -0,0 +1,74 @@
53392da6 1928+
b00004a5 1929+# Copyright (C) 2005-2018 Junjiro R. Okajima
53392da6
AM
1930+#
1931+# This program is free software; you can redistribute it and/or modify
1932+# it under the terms of the GNU General Public License as published by
1933+# the Free Software Foundation; either version 2 of the License, or
1934+# (at your option) any later version.
1935+#
1936+# This program is distributed in the hope that it will be useful,
1937+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1938+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1939+# GNU General Public License for more details.
1940+#
1941+# You should have received a copy of the GNU General Public License
523b37e3 1942+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1943+
1944+Branch Manipulation
1945+
1946+Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1947+and changing its permission/attribute, there are a lot of works to do.
1948+
1949+
1950+Add a Branch
1951+----------------------------------------------------------------------
1952+o Confirm the adding dir exists outside of aufs, including loopback
7e9cd9fe 1953+ mount, and its various attributes.
53392da6
AM
1954+o Initialize the xino file and whiteout bases if necessary.
1955+ See struct.txt.
1956+
1957+o Check the owner/group/mode of the directory
1958+ When the owner/group/mode of the adding directory differs from the
1959+ existing branch, aufs issues a warning because it may impose a
1960+ security risk.
1961+ For example, when a upper writable branch has a world writable empty
1962+ top directory, a malicious user can create any files on the writable
1963+ branch directly, like copy-up and modify manually. If something like
1964+ /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1965+ writable branch, and the writable branch is world-writable, then a
1966+ malicious guy may create /etc/passwd on the writable branch directly
1967+ and the infected file will be valid in aufs.
7e9cd9fe 1968+ I am afraid it can be a security issue, but aufs can do nothing except
53392da6
AM
1969+ producing a warning.
1970+
1971+
1972+Delete a Branch
1973+----------------------------------------------------------------------
1974+o Confirm the deleting branch is not busy
1975+ To be general, there is one merit to adopt "remount" interface to
1976+ manipulate branches. It is to discard caches. At deleting a branch,
1977+ aufs checks the still cached (and connected) dentries and inodes. If
1978+ there are any, then they are all in-use. An inode without its
1979+ corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1980+
1981+ For the cached one, aufs checks whether the same named entry exists on
1982+ other branches.
1983+ If the cached one is a directory, because aufs provides a merged view
1984+ to users, as long as one dir is left on any branch aufs can show the
1985+ dir to users. In this case, the branch can be removed from aufs.
1986+ Otherwise aufs rejects deleting the branch.
1987+
1988+ If any file on the deleting branch is opened by aufs, then aufs
1989+ rejects deleting.
1990+
1991+
1992+Modify the Permission of a Branch
1993+----------------------------------------------------------------------
1994+o Re-initialize or remove the xino file and whiteout bases if necessary.
1995+ See struct.txt.
1996+
1997+o rw --> ro: Confirm the modifying branch is not busy
1998+ Aufs rejects the request if any of these conditions are true.
1999+ - a file on the branch is mmap-ed.
2000+ - a regular file on the branch is opened for write and there is no
2001+ same named entry on the upper branch.
2002diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
2003--- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 2004+++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt 2018-04-15 08:49:13.394483860 +0200
523b37e3 2005@@ -0,0 +1,64 @@
53392da6 2006+
b00004a5 2007+# Copyright (C) 2005-2018 Junjiro R. Okajima
53392da6
AM
2008+#
2009+# This program is free software; you can redistribute it and/or modify
2010+# it under the terms of the GNU General Public License as published by
2011+# the Free Software Foundation; either version 2 of the License, or
2012+# (at your option) any later version.
2013+#
2014+# This program is distributed in the hope that it will be useful,
2015+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2016+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2017+# GNU General Public License for more details.
2018+#
2019+# You should have received a copy of the GNU General Public License
523b37e3 2020+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2021+
2022+Policies to Select One among Multiple Writable Branches
2023+----------------------------------------------------------------------
2024+When the number of writable branch is more than one, aufs has to decide
2025+the target branch for file creation or copy-up. By default, the highest
2026+writable branch which has the parent (or ancestor) dir of the target
2027+file is chosen (top-down-parent policy).
2028+By user's request, aufs implements some other policies to select the
7e9cd9fe
AM
2029+writable branch, for file creation several policies, round-robin,
2030+most-free-space, and other policies. For copy-up, top-down-parent,
2031+bottom-up-parent, bottom-up and others.
53392da6
AM
2032+
2033+As expected, the round-robin policy selects the branch in circular. When
2034+you have two writable branches and creates 10 new files, 5 files will be
2035+created for each branch. mkdir(2) systemcall is an exception. When you
2036+create 10 new directories, all will be created on the same branch.
2037+And the most-free-space policy selects the one which has most free
2038+space among the writable branches. The amount of free space will be
2039+checked by aufs internally, and users can specify its time interval.
2040+
2041+The policies for copy-up is more simple,
2042+top-down-parent is equivalent to the same named on in create policy,
2043+bottom-up-parent selects the writable branch where the parent dir
2044+exists and the nearest upper one from the copyup-source,
2045+bottom-up selects the nearest upper writable branch from the
2046+copyup-source, regardless the existence of the parent dir.
2047+
2048+There are some rules or exceptions to apply these policies.
2049+- If there is a readonly branch above the policy-selected branch and
2050+ the parent dir is marked as opaque (a variation of whiteout), or the
2051+ target (creating) file is whiteout-ed on the upper readonly branch,
2052+ then the result of the policy is ignored and the target file will be
2053+ created on the nearest upper writable branch than the readonly branch.
2054+- If there is a writable branch above the policy-selected branch and
2055+ the parent dir is marked as opaque or the target file is whiteouted
2056+ on the branch, then the result of the policy is ignored and the target
2057+ file will be created on the highest one among the upper writable
2058+ branches who has diropq or whiteout. In case of whiteout, aufs removes
2059+ it as usual.
2060+- link(2) and rename(2) systemcalls are exceptions in every policy.
2061+ They try selecting the branch where the source exists as possible
2062+ since copyup a large file will take long time. If it can't be,
2063+ ie. the branch where the source exists is readonly, then they will
2064+ follow the copyup policy.
2065+- There is an exception for rename(2) when the target exists.
2066+ If the rename target exists, aufs compares the index of the branches
2067+ where the source and the target exists and selects the higher
2068+ one. If the selected branch is readonly, then aufs follows the
2069+ copyup policy.
8b6a4947
AM
2070diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
2071--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
b00004a5 2072+++ linux/Documentation/filesystems/aufs/design/06dirren.dot 2018-04-15 08:49:13.394483860 +0200
8b6a4947
AM
2073@@ -0,0 +1,31 @@
2074+
2075+// to view this graph, run dot(1) command in GRAPHVIZ.
2076+
2077+digraph G {
2078+node [shape=box];
2079+whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
2080+
2081+node [shape=oval];
2082+
2083+aufs_rename -> whinfo [label="store/remove"];
2084+
2085+node [shape=oval];
2086+inode_list [label="h_inum list in branch\ncache"];
2087+
2088+node [shape=box];
2089+whinode [label="h_inum list file"];
2090+
2091+node [shape=oval];
2092+brmgmt [label="br_add/del/mod/umount"];
2093+
2094+brmgmt -> inode_list [label="create/remove"];
2095+brmgmt -> whinode [label="load/store"];
2096+
2097+inode_list -> whinode [style=dashed,dir=both];
2098+
2099+aufs_rename -> inode_list [label="add/del"];
2100+
2101+aufs_lookup -> inode_list [label="search"];
2102+
2103+aufs_lookup -> whinfo [label="load/remove"];
2104+}
2105diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
2106--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 2107+++ linux/Documentation/filesystems/aufs/design/06dirren.txt 2018-04-15 08:49:13.394483860 +0200
8b6a4947
AM
2108@@ -0,0 +1,102 @@
2109+
b00004a5 2110+# Copyright (C) 2017-2018 Junjiro R. Okajima
8b6a4947
AM
2111+#
2112+# This program is free software; you can redistribute it and/or modify
2113+# it under the terms of the GNU General Public License as published by
2114+# the Free Software Foundation; either version 2 of the License, or
2115+# (at your option) any later version.
2116+#
2117+# This program is distributed in the hope that it will be useful,
2118+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2119+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2120+# GNU General Public License for more details.
2121+#
2122+# You should have received a copy of the GNU General Public License
2123+# along with this program. If not, see <http://www.gnu.org/licenses/>.
2124+
2125+Special handling for renaming a directory (DIRREN)
2126+----------------------------------------------------------------------
2127+First, let's assume we have a simple usecase.
2128+
2129+- /u = /rw + /ro
2130+- /rw/dirA exists
2131+- /ro/dirA and /ro/dirA/file exist too
2132+- there is no dirB on both branches
2133+- a user issues rename("dirA", "dirB")
2134+
2135+Now, what should aufs behave against this rename(2)?
2136+There are a few possible cases.
2137+
2138+A. returns EROFS.
2139+ since dirA exists on a readonly branch which cannot be renamed.
2140+B. returns EXDEV.
2141+ it is possible to copy-up dirA (only the dir itself), but the child
2142+ entries ("file" in this case) should not be. it must be a bad
2143+ approach to copy-up recursively.
2144+C. returns a success.
2145+ even the branch /ro is readonly, aufs tries renaming it. Obviously it
2146+ is a violation of aufs' policy.
2147+D. construct an extra information which indicates that /ro/dirA should
2148+ be handled as the name of dirB.
2149+ overlayfs has a similar feature called REDIRECT.
2150+
2151+Until now, aufs implements the case B only which returns EXDEV, and
2152+expects the userspace application behaves like mv(1) which tries
2153+issueing rename(2) recursively.
2154+
2155+A new aufs feature called DIRREN is introduced which implements the case
2156+D. There are several "extra information" added.
2157+
2158+1. detailed info per renamed directory
2159+ path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
2160+2. the inode-number list of directories on a branch
2161+ path: /rw/dirB/$AUFS_WH_DR_BRHINO
2162+
2163+The filename of "detailed info per directory" represents the lower
2164+branch, and its format is
2165+- a type of the branch id
2166+ one of these.
2167+ + uuid (not implemented yet)
2168+ + fsid
2169+ + dev
2170+- the inode-number of the branch root dir
2171+
2172+And it contains these info in a single regular file.
2173+- magic number
2174+- branch's inode-number of the logically renamed dir
2175+- the name of the before-renamed dir
2176+
2177+The "detailed info per directory" file is created in aufs rename(2), and
2178+loaded in any lookup.
2179+The info is considered in lookup for the matching case only. Here
2180+"matching" means that the root of branch (in the info filename) is same
2181+to the current looking-up branch. After looking-up the before-renamed
2182+name, the inode-number is compared. And the matched dentry is used.
2183+
2184+The "inode-number list of directories" is a regular file which contains
2185+simply the inode-numbers on the branch. The file is created or updated
2186+in removing the branch, and loaded in adding the branch. Its lifetime is
2187+equal to the branch.
2188+The list is refered in lookup, and when the current target inode is
2189+found in the list, the aufs tries loading the "detailed info per
2190+directory" and get the changed and valid name of the dir.
2191+
2192+Theoretically these "extra informaiton" may be able to be put into XATTR
2193+in the dir inode. But aufs doesn't choose this way because
2194+1. XATTR may not be supported by the branch (or its configuration)
2195+2. XATTR may have its size limit.
2196+3. XATTR may be less easy to convert than a regular file, when the
2197+ format of the info is changed in the future.
2198+At the same time, I agree that the regular file approach is much slower
2199+than XATTR approach. So, in the future, aufs may take the XATTR or other
2200+better approach.
2201+
2202+This DIRREN feature is enabled by aufs configuration, and is activated
2203+by a new mount option.
2204+
2205+For the more complicated case, there is a work with UDBA option, which
2206+is to dected the direct access to the branches (by-passing aufs) and to
2207+maintain the cashes in aufs. Since a single cached aufs dentry may
2208+contains two names, before- and after-rename, the name comparision in
2209+UDBA handler may not work correctly. In this case, the behaviour will be
2210+equivalen to udba=reval case.
076b876e
AM
2211diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
2212--- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 2213+++ linux/Documentation/filesystems/aufs/design/06fhsm.txt 2018-04-15 08:49:13.394483860 +0200
076b876e
AM
2214@@ -0,0 +1,120 @@
2215+
b00004a5 2216+# Copyright (C) 2011-2018 Junjiro R. Okajima
076b876e
AM
2217+#
2218+# This program is free software; you can redistribute it and/or modify
2219+# it under the terms of the GNU General Public License as published by
2220+# the Free Software Foundation; either version 2 of the License, or
2221+# (at your option) any later version.
2222+#
2223+# This program is distributed in the hope that it will be useful,
2224+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2225+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2226+# GNU General Public License for more details.
2227+#
2228+# You should have received a copy of the GNU General Public License
2229+# along with this program; if not, write to the Free Software
2230+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2231+
2232+
2233+File-based Hierarchical Storage Management (FHSM)
2234+----------------------------------------------------------------------
2235+Hierarchical Storage Management (or HSM) is a well-known feature in the
2236+storage world. Aufs provides this feature as file-based with multiple
7e9cd9fe 2237+writable branches, based upon the principle of "Colder, the Lower".
076b876e 2238+Here the word "colder" means that the less used files, and "lower" means
7e9cd9fe 2239+that the position in the order of the stacked branches vertically.
076b876e
AM
2240+These multiple writable branches are prioritized, ie. the topmost one
2241+should be the fastest drive and be used heavily.
2242+
2243+o Characters in aufs FHSM story
2244+- aufs itself and a new branch attribute.
2245+- a new ioctl interface to move-down and to establish a connection with
2246+ the daemon ("move-down" is a converse of "copy-up").
2247+- userspace tool and daemon.
2248+
2249+The userspace daemon establishes a connection with aufs and waits for
2250+the notification. The notified information is very similar to struct
2251+statfs containing the number of consumed blocks and inodes.
2252+When the consumed blocks/inodes of a branch exceeds the user-specified
2253+upper watermark, the daemon activates its move-down process until the
2254+consumed blocks/inodes reaches the user-specified lower watermark.
2255+
2256+The actual move-down is done by aufs based upon the request from
2257+user-space since we need to maintain the inode number and the internal
2258+pointer arrays in aufs.
2259+
2260+Currently aufs FHSM handles the regular files only. Additionally they
2261+must not be hard-linked nor pseudo-linked.
2262+
2263+
2264+o Cowork of aufs and the user-space daemon
2265+ During the userspace daemon established the connection, aufs sends a
2266+ small notification to it whenever aufs writes something into the
2267+ writable branch. But it may cost high since aufs issues statfs(2)
2268+ internally. So user can specify a new option to cache the
2269+ info. Actually the notification is controlled by these factors.
2270+ + the specified cache time.
2271+ + classified as "force" by aufs internally.
2272+ Until the specified time expires, aufs doesn't send the info
2273+ except the forced cases. When aufs decide forcing, the info is always
2274+ notified to userspace.
2275+ For example, the number of free inodes is generally large enough and
2276+ the shortage of it happens rarely. So aufs doesn't force the
2277+ notification when creating a new file, directory and others. This is
2278+ the typical case which aufs doesn't force.
2279+ When aufs writes the actual filedata and the files consumes any of new
2280+ blocks, the aufs forces notifying.
2281+
2282+
2283+o Interfaces in aufs
2284+- New branch attribute.
2285+ + fhsm
2286+ Specifies that the branch is managed by FHSM feature. In other word,
2287+ participant in the FHSM.
2288+ When nofhsm is set to the branch, it will not be the source/target
2289+ branch of the move-down operation. This attribute is set
2290+ independently from coo and moo attributes, and if you want full
2291+ FHSM, you should specify them as well.
2292+- New mount option.
2293+ + fhsm_sec
2294+ Specifies a second to suppress many less important info to be
2295+ notified.
2296+- New ioctl.
2297+ + AUFS_CTL_FHSM_FD
2298+ create a new file descriptor which userspace can read the notification
2299+ (a subset of struct statfs) from aufs.
2300+- Module parameter 'brs'
2301+ It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2302+ be set.
2303+- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2304+ When there are two or more branches with fhsm attributes,
2305+ /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2306+ terminates it. As a result of remounting and branch-manipulation, the
2307+ number of branches with fhsm attribute can be one. In this case,
2308+ /sbin/mount.aufs will terminate the user-space daemon.
2309+
2310+
2311+Finally the operation is done as these steps in kernel-space.
2312+- make sure that,
2313+ + no one else is using the file.
2314+ + the file is not hard-linked.
2315+ + the file is not pseudo-linked.
2316+ + the file is a regular file.
2317+ + the parent dir is not opaqued.
2318+- find the target writable branch.
2319+- make sure the file is not whiteout-ed by the upper (than the target)
2320+ branch.
2321+- make the parent dir on the target branch.
2322+- mutex lock the inode on the branch.
2323+- unlink the whiteout on the target branch (if exists).
2324+- lookup and create the whiteout-ed temporary name on the target branch.
2325+- copy the file as the whiteout-ed temporary name on the target branch.
2326+- rename the whiteout-ed temporary name to the original name.
2327+- unlink the file on the source branch.
2328+- maintain the internal pointer array and the external inode number
2329+ table (XINO).
2330+- maintain the timestamps and other attributes of the parent dir and the
2331+ file.
2332+
2333+And of course, in every step, an error may happen. So the operation
2334+should restore the original file state after an error happens.
53392da6
AM
2335diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2336--- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 2337+++ linux/Documentation/filesystems/aufs/design/06mmap.txt 2018-04-15 08:49:13.394483860 +0200
b912730e 2338@@ -0,0 +1,72 @@
53392da6 2339+
b00004a5 2340+# Copyright (C) 2005-2018 Junjiro R. Okajima
53392da6
AM
2341+#
2342+# This program is free software; you can redistribute it and/or modify
2343+# it under the terms of the GNU General Public License as published by
2344+# the Free Software Foundation; either version 2 of the License, or
2345+# (at your option) any later version.
2346+#
2347+# This program is distributed in the hope that it will be useful,
2348+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2349+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2350+# GNU General Public License for more details.
2351+#
2352+# You should have received a copy of the GNU General Public License
523b37e3 2353+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2354+
2355+mmap(2) -- File Memory Mapping
2356+----------------------------------------------------------------------
2357+In aufs, the file-mapped pages are handled by a branch fs directly, no
2358+interaction with aufs. It means aufs_mmap() calls the branch fs's
2359+->mmap().
2360+This approach is simple and good, but there is one problem.
7e9cd9fe 2361+Under /proc, several entries show the mmapped files by its path (with
53392da6
AM
2362+device and inode number), and the printed path will be the path on the
2363+branch fs's instead of virtual aufs's.
2364+This is not a problem in most cases, but some utilities lsof(1) (and its
2365+user) may expect the path on aufs.
2366+
2367+To address this issue, aufs adds a new member called vm_prfile in struct
2368+vm_area_struct (and struct vm_region). The original vm_file points to
2369+the file on the branch fs in order to handle everything correctly as
2370+usual. The new vm_prfile points to a virtual file in aufs, and the
2371+show-functions in procfs refers to vm_prfile if it is set.
2372+Also we need to maintain several other places where touching vm_file
2373+such like
2374+- fork()/clone() copies vma and the reference count of vm_file is
2375+ incremented.
2376+- merging vma maintains the ref count too.
2377+
7e9cd9fe 2378+This is not a good approach. It just fakes the printed path. But it
53392da6
AM
2379+leaves all behaviour around f_mapping unchanged. This is surely an
2380+advantage.
2381+Actually aufs had adopted another complicated approach which calls
2382+generic_file_mmap() and handles struct vm_operations_struct. In this
2383+approach, aufs met a hard problem and I could not solve it without
2384+switching the approach.
b912730e
AM
2385+
2386+There may be one more another approach which is
2387+- bind-mount the branch-root onto the aufs-root internally
2388+- grab the new vfsmount (ie. struct mount)
2389+- lazy-umount the branch-root internally
2390+- in open(2) the aufs-file, open the branch-file with the hidden
2391+ vfsmount (instead of the original branch's vfsmount)
2392+- ideally this "bind-mount and lazy-umount" should be done atomically,
2393+ but it may be possible from userspace by the mount helper.
2394+
2395+Adding the internal hidden vfsmount and using it in opening a file, the
2396+file path under /proc will be printed correctly. This approach looks
2397+smarter, but is not possible I am afraid.
2398+- aufs-root may be bind-mount later. when it happens, another hidden
2399+ vfsmount will be required.
2400+- it is hard to get the chance to bind-mount and lazy-umount
2401+ + in kernel-space, FS can have vfsmount in open(2) via
2402+ file->f_path, and aufs can know its vfsmount. But several locks are
2403+ already acquired, and if aufs tries to bind-mount and lazy-umount
2404+ here, then it may cause a deadlock.
2405+ + in user-space, bind-mount doesn't invoke the mount helper.
2406+- since /proc shows dev and ino, aufs has to give vma these info. it
2407+ means a new member vm_prinode will be necessary. this is essentially
2408+ equivalent to vm_prfile described above.
2409+
2410+I have to give up this "looks-smater" approach.
c1595e42
JR
2411diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2412--- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 2413+++ linux/Documentation/filesystems/aufs/design/06xattr.txt 2018-04-15 08:49:13.394483860 +0200
c1595e42
JR
2414@@ -0,0 +1,96 @@
2415+
b00004a5 2416+# Copyright (C) 2014-2018 Junjiro R. Okajima
c1595e42
JR
2417+#
2418+# This program is free software; you can redistribute it and/or modify
2419+# it under the terms of the GNU General Public License as published by
2420+# the Free Software Foundation; either version 2 of the License, or
2421+# (at your option) any later version.
2422+#
2423+# This program is distributed in the hope that it will be useful,
2424+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2425+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2426+# GNU General Public License for more details.
2427+#
2428+# You should have received a copy of the GNU General Public License
2429+# along with this program; if not, write to the Free Software
2430+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2431+
2432+
2433+Listing XATTR/EA and getting the value
2434+----------------------------------------------------------------------
2435+For the inode standard attributes (owner, group, timestamps, etc.), aufs
2436+shows the values from the topmost existing file. This behaviour is good
7e9cd9fe 2437+for the non-dir entries since the bahaviour exactly matches the shown
c1595e42
JR
2438+information. But for the directories, aufs considers all the same named
2439+entries on the lower branches. Which means, if one of the lower entry
2440+rejects readdir call, then aufs returns an error even if the topmost
2441+entry allows it. This behaviour is necessary to respect the branch fs's
2442+security, but can make users confused since the user-visible standard
2443+attributes don't match the behaviour.
2444+To address this issue, aufs has a mount option called dirperm1 which
2445+checks the permission for the topmost entry only, and ignores the lower
2446+entry's permission.
2447+
2448+A similar issue can happen around XATTR.
2449+getxattr(2) and listxattr(2) families behave as if dirperm1 option is
7e9cd9fe
AM
2450+always set. Otherwise these very unpleasant situation would happen.
2451+- listxattr(2) may return the duplicated entries.
c1595e42
JR
2452+- users may not be able to remove or reset the XATTR forever,
2453+
2454+
2455+XATTR/EA support in the internal (copy,move)-(up,down)
2456+----------------------------------------------------------------------
7e9cd9fe 2457+Generally the extended attributes of inode are categorized as these.
c1595e42
JR
2458+- "security" for LSM and capability.
2459+- "system" for posix ACL, 'acl' mount option is required for the branch
2460+ fs generally.
2461+- "trusted" for userspace, CAP_SYS_ADMIN is required.
2462+- "user" for userspace, 'user_xattr' mount option is required for the
2463+ branch fs generally.
2464+
2465+Moreover there are some other categories. Aufs handles these rather
2466+unpopular categories as the ordinary ones, ie. there is no special
2467+condition nor exception.
2468+
2469+In copy-up, the support for XATTR on the dst branch may differ from the
2470+src branch. In this case, the copy-up operation will get an error and
7e9cd9fe
AM
2471+the original user operation which triggered the copy-up will fail. It
2472+can happen that even all copy-up will fail.
c1595e42
JR
2473+When both of src and dst branches support XATTR and if an error occurs
2474+during copying XATTR, then the copy-up should fail obviously. That is a
2475+good reason and aufs should return an error to userspace. But when only
7e9cd9fe 2476+the src branch support that XATTR, aufs should not return an error.
c1595e42
JR
2477+For example, the src branch supports ACL but the dst branch doesn't
2478+because the dst branch may natively un-support it or temporary
2479+un-support it due to "noacl" mount option. Of course, the dst branch fs
2480+may NOT return an error even if the XATTR is not supported. It is
2481+totally up to the branch fs.
2482+
2483+Anyway when the aufs internal copy-up gets an error from the dst branch
2484+fs, then aufs tries removing the just copied entry and returns the error
2485+to the userspace. The worst case of this situation will be all copy-up
2486+will fail.
2487+
2488+For the copy-up operation, there two basic approaches.
2489+- copy the specified XATTR only (by category above), and return the
7e9cd9fe 2490+ error unconditionally if it happens.
c1595e42
JR
2491+- copy all XATTR, and ignore the error on the specified category only.
2492+
2493+In order to support XATTR and to implement the correct behaviour, aufs
7e9cd9fe
AM
2494+chooses the latter approach and introduces some new branch attributes,
2495+"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
c1595e42 2496+They correspond to the XATTR namespaces (see above). Additionally, to be
7e9cd9fe
AM
2497+convenient, "icex" is also provided which means all "icex*" attributes
2498+are set (here the word "icex" stands for "ignore copy-error on XATTR").
c1595e42
JR
2499+
2500+The meaning of these attributes is to ignore the error from setting
2501+XATTR on that branch.
2502+Note that aufs tries copying all XATTR unconditionally, and ignores the
2503+error from the dst branch according to the specified attributes.
2504+
2505+Some XATTR may have its default value. The default value may come from
2506+the parent dir or the environment. If the default value is set at the
2507+file creating-time, it will be overwritten by copy-up.
2508+Some contradiction may happen I am afraid.
2509+Do we need another attribute to stop copying XATTR? I am unsure. For
2510+now, aufs implements the branch attributes to ignore the error.
53392da6
AM
2511diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2512--- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 2513+++ linux/Documentation/filesystems/aufs/design/07export.txt 2018-04-15 08:49:13.394483860 +0200
523b37e3 2514@@ -0,0 +1,58 @@
53392da6 2515+
b00004a5 2516+# Copyright (C) 2005-2018 Junjiro R. Okajima
53392da6
AM
2517+#
2518+# This program is free software; you can redistribute it and/or modify
2519+# it under the terms of the GNU General Public License as published by
2520+# the Free Software Foundation; either version 2 of the License, or
2521+# (at your option) any later version.
2522+#
2523+# This program is distributed in the hope that it will be useful,
2524+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2525+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2526+# GNU General Public License for more details.
2527+#
2528+# You should have received a copy of the GNU General Public License
523b37e3 2529+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2530+
2531+Export Aufs via NFS
2532+----------------------------------------------------------------------
2533+Here is an approach.
2534+- like xino/xib, add a new file 'xigen' which stores aufs inode
2535+ generation.
2536+- iget_locked(): initialize aufs inode generation for a new inode, and
2537+ store it in xigen file.
2538+- destroy_inode(): increment aufs inode generation and store it in xigen
2539+ file. it is necessary even if it is not unlinked, because any data of
2540+ inode may be changed by UDBA.
2541+- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2542+ build file handle by
2543+ + branch id (4 bytes)
2544+ + superblock generation (4 bytes)
2545+ + inode number (4 or 8 bytes)
2546+ + parent dir inode number (4 or 8 bytes)
2547+ + inode generation (4 bytes))
2548+ + return value of exportfs_encode_fh() for the parent on a branch (4
2549+ bytes)
2550+ + file handle for a branch (by exportfs_encode_fh())
2551+- fh_to_dentry():
2552+ + find the index of a branch from its id in handle, and check it is
2553+ still exist in aufs.
2554+ + 1st level: get the inode number from handle and search it in cache.
7e9cd9fe
AM
2555+ + 2nd level: if not found in cache, get the parent inode number from
2556+ the handle and search it in cache. and then open the found parent
2557+ dir, find the matching inode number by vfs_readdir() and get its
2558+ name, and call lookup_one_len() for the target dentry.
53392da6
AM
2559+ + 3rd level: if the parent dir is not cached, call
2560+ exportfs_decode_fh() for a branch and get the parent on a branch,
2561+ build a pathname of it, convert it a pathname in aufs, call
2562+ path_lookup(). now aufs gets a parent dir dentry, then handle it as
2563+ the 2nd level.
2564+ + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2565+ for every branch, but not itself. to get this, (currently) aufs
2566+ searches in current->nsproxy->mnt_ns list. it may not be a good
2567+ idea, but I didn't get other approach.
2568+ + test the generation of the gotten inode.
2569+- every inode operation: they may get EBUSY due to UDBA. in this case,
2570+ convert it into ESTALE for NFSD.
2571+- readdir(): call lockdep_on/off() because filldir in NFSD calls
2572+ lookup_one_len(), vfs_getattr(), encode_fh() and others.
2573diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2574--- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 2575+++ linux/Documentation/filesystems/aufs/design/08shwh.txt 2018-04-15 08:49:13.394483860 +0200
523b37e3 2576@@ -0,0 +1,52 @@
53392da6 2577+
b00004a5 2578+# Copyright (C) 2005-2018 Junjiro R. Okajima
53392da6
AM
2579+#
2580+# This program is free software; you can redistribute it and/or modify
2581+# it under the terms of the GNU General Public License as published by
2582+# the Free Software Foundation; either version 2 of the License, or
2583+# (at your option) any later version.
2584+#
2585+# This program is distributed in the hope that it will be useful,
2586+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2587+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2588+# GNU General Public License for more details.
2589+#
2590+# You should have received a copy of the GNU General Public License
523b37e3 2591+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2592+
2593+Show Whiteout Mode (shwh)
2594+----------------------------------------------------------------------
2595+Generally aufs hides the name of whiteouts. But in some cases, to show
2596+them is very useful for users. For instance, creating a new middle layer
2597+(branch) by merging existing layers.
2598+
2599+(borrowing aufs1 HOW-TO from a user, Michael Towers)
2600+When you have three branches,
2601+- Bottom: 'system', squashfs (underlying base system), read-only
2602+- Middle: 'mods', squashfs, read-only
2603+- Top: 'overlay', ram (tmpfs), read-write
2604+
2605+The top layer is loaded at boot time and saved at shutdown, to preserve
2606+the changes made to the system during the session.
2607+When larger changes have been made, or smaller changes have accumulated,
2608+the size of the saved top layer data grows. At this point, it would be
2609+nice to be able to merge the two overlay branches ('mods' and 'overlay')
2610+and rewrite the 'mods' squashfs, clearing the top layer and thus
2611+restoring save and load speed.
2612+
2613+This merging is simplified by the use of another aufs mount, of just the
2614+two overlay branches using the 'shwh' option.
2615+# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2616+ aufs /livesys/merge_union
2617+
2618+A merged view of these two branches is then available at
2619+/livesys/merge_union, and the new feature is that the whiteouts are
2620+visible!
2621+Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2622+writing to all branches. Also the default mode for all branches is 'ro'.
2623+It is now possible to save the combined contents of the two overlay
2624+branches to a new squashfs, e.g.:
2625+# mksquashfs /livesys/merge_union /path/to/newmods.squash
2626+
2627+This new squashfs archive can be stored on the boot device and the
2628+initramfs will use it to replace the old one at the next boot.
2629diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2630--- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 2631+++ linux/Documentation/filesystems/aufs/design/10dynop.txt 2018-04-15 08:49:13.394483860 +0200
7e9cd9fe 2632@@ -0,0 +1,47 @@
53392da6 2633+
b00004a5 2634+# Copyright (C) 2010-2018 Junjiro R. Okajima
53392da6
AM
2635+#
2636+# This program is free software; you can redistribute it and/or modify
2637+# it under the terms of the GNU General Public License as published by
2638+# the Free Software Foundation; either version 2 of the License, or
2639+# (at your option) any later version.
2640+#
2641+# This program is distributed in the hope that it will be useful,
2642+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2643+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2644+# GNU General Public License for more details.
2645+#
2646+# You should have received a copy of the GNU General Public License
523b37e3 2647+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2648+
2649+Dynamically customizable FS operations
2650+----------------------------------------------------------------------
2651+Generally FS operations (struct inode_operations, struct
2652+address_space_operations, struct file_operations, etc.) are defined as
2653+"static const", but it never means that FS have only one set of
2654+operation. Some FS have multiple sets of them. For instance, ext2 has
2655+three sets, one for XIP, for NOBH, and for normal.
2656+Since aufs overrides and redirects these operations, sometimes aufs has
7e9cd9fe 2657+to change its behaviour according to the branch FS type. More importantly
53392da6
AM
2658+VFS acts differently if a function (member in the struct) is set or
2659+not. It means aufs should have several sets of operations and select one
2660+among them according to the branch FS definition.
2661+
7e9cd9fe 2662+In order to solve this problem and not to affect the behaviour of VFS,
53392da6 2663+aufs defines these operations dynamically. For instance, aufs defines
7e9cd9fe
AM
2664+dummy direct_IO function for struct address_space_operations, but it may
2665+not be set to the address_space_operations actually. When the branch FS
2666+doesn't have it, aufs doesn't set it to its address_space_operations
2667+while the function definition itself is still alive. So the behaviour
2668+itself will not change, and it will return an error when direct_IO is
2669+not set.
53392da6
AM
2670+
2671+The lifetime of these dynamically generated operation object is
2672+maintained by aufs branch object. When the branch is removed from aufs,
2673+the reference counter of the object is decremented. When it reaches
2674+zero, the dynamically generated operation object will be freed.
2675+
7e9cd9fe
AM
2676+This approach is designed to support AIO (io_submit), Direct I/O and
2677+XIP (DAX) mainly.
2678+Currently this approach is applied to address_space_operations for
2679+regular files only.
53392da6
AM
2680diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2681--- /usr/share/empty/Documentation/filesystems/aufs/README 1970-01-01 01:00:00.000000000 +0100
c4adf169 2682+++ linux/Documentation/filesystems/aufs/README 2018-06-15 11:15:15.400449109 +0200
f2c43d5f 2683@@ -0,0 +1,393 @@
53392da6 2684+
5527c038 2685+Aufs4 -- advanced multi layered unification filesystem version 4.x
53392da6
AM
2686+http://aufs.sf.net
2687+Junjiro R. Okajima
2688+
2689+
2690+0. Introduction
2691+----------------------------------------
2692+In the early days, aufs was entirely re-designed and re-implemented
7e9cd9fe 2693+Unionfs Version 1.x series. Adding many original ideas, approaches,
53392da6
AM
2694+improvements and implementations, it becomes totally different from
2695+Unionfs while keeping the basic features.
2696+Recently, Unionfs Version 2.x series begin taking some of the same
2697+approaches to aufs1's.
2698+Unionfs is being developed by Professor Erez Zadok at Stony Brook
2699+University and his team.
2700+
5527c038 2701+Aufs4 supports linux-4.0 and later, and for linux-3.x series try aufs3.
53392da6
AM
2702+If you want older kernel version support, try aufs2-2.6.git or
2703+aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
2704+
2705+Note: it becomes clear that "Aufs was rejected. Let's give it up."
38d290e6
JR
2706+ According to Christoph Hellwig, linux rejects all union-type
2707+ filesystems but UnionMount.
53392da6
AM
2708+<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2709+
38d290e6
JR
2710+PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2711+ UnionMount, and he pointed out an issue around a directory mutex
2712+ lock and aufs addressed it. But it is still unsure whether aufs will
2713+ be merged (or any other union solution).
076b876e 2714+<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
38d290e6 2715+
53392da6
AM
2716+
2717+1. Features
2718+----------------------------------------
2719+- unite several directories into a single virtual filesystem. The member
2720+ directory is called as a branch.
2721+- you can specify the permission flags to the branch, which are 'readonly',
2722+ 'readwrite' and 'whiteout-able.'
2723+- by upper writable branch, internal copyup and whiteout, files/dirs on
2724+ readonly branch are modifiable logically.
2725+- dynamic branch manipulation, add, del.
2726+- etc...
2727+
7e9cd9fe
AM
2728+Also there are many enhancements in aufs, such as:
2729+- test only the highest one for the directory permission (dirperm1)
2730+- copyup on open (coo=)
2731+- 'move' policy for copy-up between two writable branches, after
2732+ checking free space.
2733+- xattr, acl
53392da6
AM
2734+- readdir(3) in userspace.
2735+- keep inode number by external inode number table
2736+- keep the timestamps of file/dir in internal copyup operation
2737+- seekable directory, supporting NFS readdir.
2738+- whiteout is hardlinked in order to reduce the consumption of inodes
2739+ on branch
2740+- do not copyup, nor create a whiteout when it is unnecessary
2741+- revert a single systemcall when an error occurs in aufs
2742+- remount interface instead of ioctl
2743+- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2744+- loopback mounted filesystem as a branch
2745+- kernel thread for removing the dir who has a plenty of whiteouts
2746+- support copyup sparse file (a file which has a 'hole' in it)
2747+- default permission flags for branches
2748+- selectable permission flags for ro branch, whether whiteout can
2749+ exist or not
2750+- export via NFS.
2751+- support <sysfs>/fs/aufs and <debugfs>/aufs.
2752+- support multiple writable branches, some policies to select one
2753+ among multiple writable branches.
2754+- a new semantics for link(2) and rename(2) to support multiple
2755+ writable branches.
2756+- no glibc changes are required.
2757+- pseudo hardlink (hardlink over branches)
2758+- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2759+ including NFS or remote filesystem branch.
2760+- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2761+- and more...
2762+
5527c038 2763+Currently these features are dropped temporary from aufs4.
53392da6 2764+See design/08plan.txt in detail.
53392da6
AM
2765+- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2766+ (robr)
2767+- statistics of aufs thread (/sys/fs/aufs/stat)
53392da6
AM
2768+
2769+Features or just an idea in the future (see also design/*.txt),
2770+- reorder the branch index without del/re-add.
2771+- permanent xino files for NFSD
2772+- an option for refreshing the opened files after add/del branches
53392da6
AM
2773+- light version, without branch manipulation. (unnecessary?)
2774+- copyup in userspace
2775+- inotify in userspace
2776+- readv/writev
53392da6
AM
2777+
2778+
2779+2. Download
2780+----------------------------------------
5527c038
JR
2781+There are three GIT trees for aufs4, aufs4-linux.git,
2782+aufs4-standalone.git, and aufs-util.git. Note that there is no "4" in
1e00d052 2783+"aufs-util.git."
5527c038
JR
2784+While the aufs-util is always necessary, you need either of aufs4-linux
2785+or aufs4-standalone.
1e00d052 2786+
5527c038 2787+The aufs4-linux tree includes the whole linux mainline GIT tree,
1e00d052
AM
2788+git://git.kernel.org/.../torvalds/linux.git.
2789+And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
5527c038 2790+build aufs4 as an external kernel module.
2000de60 2791+Several extra patches are not included in this tree. Only
be52b249 2792+aufs4-standalone tree contains them. They are described in the later
2000de60 2793+section "Configuration and Compilation."
1e00d052 2794+
5527c038 2795+On the other hand, the aufs4-standalone tree has only aufs source files
53392da6 2796+and necessary patches, and you can select CONFIG_AUFS_FS=m.
2000de60 2797+But you need to apply all aufs patches manually.
53392da6 2798+
5527c038
JR
2799+You will find GIT branches whose name is in form of "aufs4.x" where "x"
2800+represents the linux kernel version, "linux-4.x". For instance,
2801+"aufs4.0" is for linux-4.0. For latest "linux-4.x-rcN", use
2802+"aufs4.x-rcN" branch.
1e00d052 2803+
5527c038 2804+o aufs4-linux tree
1e00d052 2805+$ git clone --reference /your/linux/git/tree \
5527c038 2806+ git://github.com/sfjro/aufs4-linux.git aufs4-linux.git
1e00d052 2807+- if you don't have linux GIT tree, then remove "--reference ..."
5527c038
JR
2808+$ cd aufs4-linux.git
2809+$ git checkout origin/aufs4.0
53392da6 2810+
2000de60
JR
2811+Or You may want to directly git-pull aufs into your linux GIT tree, and
2812+leave the patch-work to GIT.
2813+$ cd /your/linux/git/tree
5527c038
JR
2814+$ git remote add aufs4 git://github.com/sfjro/aufs4-linux.git
2815+$ git fetch aufs4
2816+$ git checkout -b my4.0 v4.0
2817+$ (add your local change...)
2818+$ git pull aufs4 aufs4.0
2819+- now you have v4.0 + your_changes + aufs4.0 in you my4.0 branch.
2000de60 2820+- you may need to solve some conflicts between your_changes and
5527c038
JR
2821+ aufs4.0. in this case, git-rerere is recommended so that you can
2822+ solve the similar conflicts automatically when you upgrade to 4.1 or
2000de60
JR
2823+ later in the future.
2824+
5527c038
JR
2825+o aufs4-standalone tree
2826+$ git clone git://github.com/sfjro/aufs4-standalone.git aufs4-standalone.git
2827+$ cd aufs4-standalone.git
2828+$ git checkout origin/aufs4.0
53392da6
AM
2829+
2830+o aufs-util tree
5527c038
JR
2831+$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2832+- note that the public aufs-util.git is on SourceForge instead of
2833+ GitHUB.
53392da6 2834+$ cd aufs-util.git
5527c038 2835+$ git checkout origin/aufs4.0
53392da6 2836+
5527c038
JR
2837+Note: The 4.x-rcN branch is to be used with `rc' kernel versions ONLY.
2838+The minor version number, 'x' in '4.x', of aufs may not always
9dbd164d
AM
2839+follow the minor version number of the kernel.
2840+Because changes in the kernel that cause the use of a new
2841+minor version number do not always require changes to aufs-util.
2842+
2843+Since aufs-util has its own minor version number, you may not be
2844+able to find a GIT branch in aufs-util for your kernel's
2845+exact minor version number.
2846+In this case, you should git-checkout the branch for the
53392da6 2847+nearest lower number.
9dbd164d
AM
2848+
2849+For (an unreleased) example:
5527c038
JR
2850+If you are using "linux-4.10" and the "aufs4.10" branch
2851+does not exist in aufs-util repository, then "aufs4.9", "aufs4.8"
9dbd164d
AM
2852+or something numerically smaller is the branch for your kernel.
2853+
53392da6
AM
2854+Also you can view all branches by
2855+ $ git branch -a
2856+
2857+
2858+3. Configuration and Compilation
2859+----------------------------------------
2860+Make sure you have git-checkout'ed the correct branch.
2861+
5527c038 2862+For aufs4-linux tree,
c06a8ce3 2863+- enable CONFIG_AUFS_FS.
1e00d052
AM
2864+- set other aufs configurations if necessary.
2865+
5527c038 2866+For aufs4-standalone tree,
53392da6
AM
2867+There are several ways to build.
2868+
2869+1.
5527c038
JR
2870+- apply ./aufs4-kbuild.patch to your kernel source files.
2871+- apply ./aufs4-base.patch too.
2872+- apply ./aufs4-mmap.patch too.
2873+- apply ./aufs4-standalone.patch too, if you have a plan to set
2874+ CONFIG_AUFS_FS=m. otherwise you don't need ./aufs4-standalone.patch.
537831f9
AM
2875+- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2876+ kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
c06a8ce3 2877+- enable CONFIG_AUFS_FS, you can select either
53392da6
AM
2878+ =m or =y.
2879+- and build your kernel as usual.
2880+- install the built kernel.
c06a8ce3
AM
2881+ Note: Since linux-3.9, every filesystem module requires an alias
2882+ "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2883+ modules.aliases file if you set CONFIG_AUFS_FS=m.
7eafdf33
AM
2884+- install the header files too by "make headers_install" to the
2885+ directory where you specify. By default, it is $PWD/usr.
b4510431 2886+ "make help" shows a brief note for headers_install.
53392da6
AM
2887+- and reboot your system.
2888+
2889+2.
2890+- module only (CONFIG_AUFS_FS=m).
5527c038
JR
2891+- apply ./aufs4-base.patch to your kernel source files.
2892+- apply ./aufs4-mmap.patch too.
2893+- apply ./aufs4-standalone.patch too.
53392da6
AM
2894+- build your kernel, don't forget "make headers_install", and reboot.
2895+- edit ./config.mk and set other aufs configurations if necessary.
b4510431 2896+ Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
53392da6
AM
2897+ every aufs configurations.
2898+- build the module by simple "make".
c06a8ce3
AM
2899+ Note: Since linux-3.9, every filesystem module requires an alias
2900+ "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2901+ modules.aliases file.
53392da6
AM
2902+- you can specify ${KDIR} make variable which points to your kernel
2903+ source tree.
2904+- install the files
2905+ + run "make install" to install the aufs module, or copy the built
b4510431
AM
2906+ $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2907+ + run "make install_headers" (instead of headers_install) to install
2908+ the modified aufs header file (you can specify DESTDIR which is
2909+ available in aufs standalone version's Makefile only), or copy
2910+ $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2911+ you like manually. By default, the target directory is $PWD/usr.
5527c038 2912+- no need to apply aufs4-kbuild.patch, nor copying source files to your
53392da6
AM
2913+ kernel source tree.
2914+
b4510431 2915+Note: The header file aufs_type.h is necessary to build aufs-util
53392da6
AM
2916+ as well as "make headers_install" in the kernel source tree.
2917+ headers_install is subject to be forgotten, but it is essentially
2918+ necessary, not only for building aufs-util.
2919+ You may not meet problems without headers_install in some older
2920+ version though.
2921+
2922+And then,
2923+- read README in aufs-util, build and install it
9dbd164d
AM
2924+- note that your distribution may contain an obsoleted version of
2925+ aufs_type.h in /usr/include/linux or something. When you build aufs
2926+ utilities, make sure that your compiler refers the correct aufs header
2927+ file which is built by "make headers_install."
53392da6
AM
2928+- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2929+ then run "make install_ulib" too. And refer to the aufs manual in
2930+ detail.
2931+
5527c038 2932+There several other patches in aufs4-standalone.git. They are all
38d290e6 2933+optional. When you meet some problems, they will help you.
5527c038 2934+- aufs4-loopback.patch
38d290e6
JR
2935+ Supports a nested loopback mount in a branch-fs. This patch is
2936+ unnecessary until aufs produces a message like "you may want to try
2937+ another patch for loopback file".
2938+- vfs-ino.patch
2939+ Modifies a system global kernel internal function get_next_ino() in
2940+ order to stop assigning 0 for an inode-number. Not directly related to
2941+ aufs, but recommended generally.
2942+- tmpfs-idr.patch
2943+ Keeps the tmpfs inode number as the lowest value. Effective to reduce
2944+ the size of aufs XINO files for tmpfs branch. Also it prevents the
2945+ duplication of inode number, which is important for backup tools and
2946+ other utilities. When you find aufs XINO files for tmpfs branch
2947+ growing too much, try this patch.
be52b249
AM
2948+- lockdep-debug.patch
2949+ Because aufs is not only an ordinary filesystem (callee of VFS), but
2950+ also a caller of VFS functions for branch filesystems, subclassing of
2951+ the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2952+ feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2953+ need to apply this debug patch to expand several constant values.
2954+ If don't know what LOCKDEP, then you don't have apply this patch.
38d290e6 2955+
53392da6
AM
2956+
2957+4. Usage
2958+----------------------------------------
2959+At first, make sure aufs-util are installed, and please read the aufs
2960+manual, aufs.5 in aufs-util.git tree.
2961+$ man -l aufs.5
2962+
2963+And then,
2964+$ mkdir /tmp/rw /tmp/aufs
2965+# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2966+
2967+Here is another example. The result is equivalent.
2968+# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2969+ Or
2970+# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2971+# mount -o remount,append:${HOME} /tmp/aufs
2972+
2973+Then, you can see whole tree of your home dir through /tmp/aufs. If
2974+you modify a file under /tmp/aufs, the one on your home directory is
2975+not affected, instead the same named file will be newly created under
2976+/tmp/rw. And all of your modification to a file will be applied to
2977+the one under /tmp/rw. This is called the file based Copy on Write
2978+(COW) method.
2979+Aufs mount options are described in aufs.5.
2980+If you run chroot or something and make your aufs as a root directory,
2981+then you need to customize the shutdown script. See the aufs manual in
2982+detail.
2983+
2984+Additionally, there are some sample usages of aufs which are a
2985+diskless system with network booting, and LiveCD over NFS.
2986+See sample dir in CVS tree on SourceForge.
2987+
2988+
2989+5. Contact
2990+----------------------------------------
2991+When you have any problems or strange behaviour in aufs, please let me
2992+know with:
2993+- /proc/mounts (instead of the output of mount(8))
2994+- /sys/module/aufs/*
2995+- /sys/fs/aufs/* (if you have them)
2996+- /debug/aufs/* (if you have them)
2997+- linux kernel version
2998+ if your kernel is not plain, for example modified by distributor,
2999+ the url where i can download its source is necessary too.
3000+- aufs version which was printed at loading the module or booting the
3001+ system, instead of the date you downloaded.
3002+- configuration (define/undefine CONFIG_AUFS_xxx)
3003+- kernel configuration or /proc/config.gz (if you have it)
3004+- behaviour which you think to be incorrect
3005+- actual operation, reproducible one is better
3006+- mailto: aufs-users at lists.sourceforge.net
3007+
3008+Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
3009+and Feature Requests) on SourceForge. Please join and write to
3010+aufs-users ML.
3011+
3012+
3013+6. Acknowledgements
3014+----------------------------------------
3015+Thanks to everyone who have tried and are using aufs, whoever
3016+have reported a bug or any feedback.
3017+
3018+Especially donators:
3019+Tomas Matejicek(slax.org) made a donation (much more than once).
3020+ Since Apr 2010, Tomas M (the author of Slax and Linux Live
3021+ scripts) is making "doubling" donations.
3022+ Unfortunately I cannot list all of the donators, but I really
b4510431 3023+ appreciate.
53392da6
AM
3024+ It ends Aug 2010, but the ordinary donation URL is still available.
3025+ <http://sourceforge.net/donate/index.php?group_id=167503>
3026+Dai Itasaka made a donation (2007/8).
3027+Chuck Smith made a donation (2008/4, 10 and 12).
3028+Henk Schoneveld made a donation (2008/9).
3029+Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
3030+Francois Dupoux made a donation (2008/11).
3031+Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
3032+ aufs2 GIT tree (2009/2).
3033+William Grant made a donation (2009/3).
3034+Patrick Lane made a donation (2009/4).
3035+The Mail Archive (mail-archive.com) made donations (2009/5).
3036+Nippy Networks (Ed Wildgoose) made a donation (2009/7).
3037+New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
3038+Pavel Pronskiy made a donation (2011/2).
3039+Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
3040+ Networks (Ed Wildgoose) made a donation for hardware (2011/3).
537831f9
AM
3041+Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
3042+11).
1e00d052 3043+Sam Liddicott made a donation (2011/9).
86dc4139
AM
3044+Era Scarecrow made a donation (2013/4).
3045+Bor Ratajc made a donation (2013/4).
3046+Alessandro Gorreta made a donation (2013/4).
3047+POIRETTE Marc made a donation (2013/4).
3048+Alessandro Gorreta made a donation (2013/4).
3049+lauri kasvandik made a donation (2013/5).
392086de 3050+"pemasu from Finland" made a donation (2013/7).
523b37e3
AM
3051+The Parted Magic Project made a donation (2013/9 and 11).
3052+Pavel Barta made a donation (2013/10).
38d290e6 3053+Nikolay Pertsev made a donation (2014/5).
c2c0f25c 3054+James B made a donation (2014/7 and 2015/7).
076b876e 3055+Stefano Di Biase made a donation (2014/8).
2000de60 3056+Daniel Epellei made a donation (2015/1).
c4adf169 3057+OmegaPhil made a donation (2016/1, 2018/4).
5afbbe0d 3058+Tomasz Szewczyk made a donation (2016/4).
f2c43d5f 3059+James Burry made a donation (2016/12).
53392da6
AM
3060+
3061+Thank you very much.
3062+Donations are always, including future donations, very important and
3063+helpful for me to keep on developing aufs.
3064+
3065+
3066+7.
3067+----------------------------------------
3068+If you are an experienced user, no explanation is needed. Aufs is
3069+just a linux filesystem.
3070+
3071+
3072+Enjoy!
3073+
3074+# Local variables: ;
3075+# mode: text;
3076+# End: ;
7f207e10
AM
3077diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
3078--- /usr/share/empty/fs/aufs/aufs.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 3079+++ linux/fs/aufs/aufs.h 2018-08-12 23:43:05.450124426 +0200
062440b3
AM
3080@@ -0,0 +1,61 @@
3081+/* SPDX-License-Identifier: GPL-2.0 */
7f207e10 3082+/*
b00004a5 3083+ * Copyright (C) 2005-2018 Junjiro R. Okajima
7f207e10
AM
3084+ *
3085+ * This program, aufs is free software; you can redistribute it and/or modify
3086+ * it under the terms of the GNU General Public License as published by
3087+ * the Free Software Foundation; either version 2 of the License, or
3088+ * (at your option) any later version.
3089+ *
3090+ * This program is distributed in the hope that it will be useful,
3091+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
3092+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3093+ * GNU General Public License for more details.
3094+ *
3095+ * You should have received a copy of the GNU General Public License
523b37e3 3096+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
3097+ */
3098+
3099+/*
3100+ * all header files
3101+ */
3102+
3103+#ifndef __AUFS_H__
3104+#define __AUFS_H__
3105+
3106+#ifdef __KERNEL__
3107+
3108+#define AuStub(type, name, body, ...) \
3109+ static inline type name(__VA_ARGS__) { body; }
3110+
3111+#define AuStubVoid(name, ...) \
3112+ AuStub(void, name, , __VA_ARGS__)
3113+#define AuStubInt0(name, ...) \
3114+ AuStub(int, name, return 0, __VA_ARGS__)
3115+
3116+#include "debug.h"
3117+
3118+#include "branch.h"
3119+#include "cpup.h"
3120+#include "dcsub.h"
3121+#include "dbgaufs.h"
3122+#include "dentry.h"
3123+#include "dir.h"
8b6a4947 3124+#include "dirren.h"
7f207e10
AM
3125+#include "dynop.h"
3126+#include "file.h"
3127+#include "fstype.h"
8b6a4947 3128+#include "hbl.h"
7f207e10
AM
3129+#include "inode.h"
3130+#include "loop.h"
3131+#include "module.h"
7f207e10
AM
3132+#include "opts.h"
3133+#include "rwsem.h"
7f207e10
AM
3134+#include "super.h"
3135+#include "sysaufs.h"
3136+#include "vfsub.h"
3137+#include "whout.h"
3138+#include "wkq.h"
3139+
3140+#endif /* __KERNEL__ */
3141+#endif /* __AUFS_H__ */
3142diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
3143--- /usr/share/empty/fs/aufs/branch.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 3144+++ linux/fs/aufs/branch.c 2018-08-12 23:43:05.450124426 +0200
062440b3 3145@@ -0,0 +1,1422 @@
cd7a4cd9 3146+// SPDX-License-Identifier: GPL-2.0
7f207e10 3147+/*
b00004a5 3148+ * Copyright (C) 2005-2018 Junjiro R. Okajima
7f207e10
AM
3149+ *
3150+ * This program, aufs is free software; you can redistribute it and/or modify
3151+ * it under the terms of the GNU General Public License as published by
3152+ * the Free Software Foundation; either version 2 of the License, or
3153+ * (at your option) any later version.
3154+ *
3155+ * This program is distributed in the hope that it will be useful,
3156+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
3157+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3158+ * GNU General Public License for more details.
3159+ *
3160+ * You should have received a copy of the GNU General Public License
523b37e3 3161+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
3162+ */
3163+
3164+/*
3165+ * branch management
3166+ */
3167+
027c5e7a 3168+#include <linux/compat.h>
7f207e10
AM
3169+#include <linux/statfs.h>
3170+#include "aufs.h"
3171+
3172+/*
3173+ * free a single branch
1facf9fc 3174+ */
3175+static void au_br_do_free(struct au_branch *br)
3176+{
3177+ int i;
3178+ struct au_wbr *wbr;
4a4d8108 3179+ struct au_dykey **key;
1facf9fc 3180+
027c5e7a 3181+ au_hnotify_fin_br(br);
8b6a4947
AM
3182+ /* always, regardless the mount option */
3183+ au_dr_hino_free(&br->br_dirren);
062440b3 3184+ au_xino_put(br);
1facf9fc 3185+
5afbbe0d
AM
3186+ AuDebugOn(au_br_count(br));
3187+ au_br_count_fin(br);
1facf9fc 3188+
3189+ wbr = br->br_wbr;
3190+ if (wbr) {
3191+ for (i = 0; i < AuBrWh_Last; i++)
3192+ dput(wbr->wbr_wh[i]);
3193+ AuDebugOn(atomic_read(&wbr->wbr_wh_running));
dece6358 3194+ AuRwDestroy(&wbr->wbr_wh_rwsem);
1facf9fc 3195+ }
3196+
076b876e
AM
3197+ if (br->br_fhsm) {
3198+ au_br_fhsm_fin(br->br_fhsm);
1c60b727 3199+ kfree(br->br_fhsm);
076b876e
AM
3200+ }
3201+
4a4d8108
AM
3202+ key = br->br_dykey;
3203+ for (i = 0; i < AuBrDynOp; i++, key++)
3204+ if (*key)
3205+ au_dy_put(*key);
3206+ else
3207+ break;
3208+
537831f9
AM
3209+ /* recursive lock, s_umount of branch's */
3210+ lockdep_off();
86dc4139 3211+ path_put(&br->br_path);
537831f9 3212+ lockdep_on();
1c60b727
AM
3213+ kfree(wbr);
3214+ kfree(br);
1facf9fc 3215+}
3216+
3217+/*
3218+ * frees all branches
3219+ */
3220+void au_br_free(struct au_sbinfo *sbinfo)
3221+{
3222+ aufs_bindex_t bmax;
3223+ struct au_branch **br;
3224+
dece6358
AM
3225+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3226+
5afbbe0d 3227+ bmax = sbinfo->si_bbot + 1;
1facf9fc 3228+ br = sbinfo->si_branch;
3229+ while (bmax--)
3230+ au_br_do_free(*br++);
3231+}
3232+
3233+/*
3234+ * find the index of a branch which is specified by @br_id.
3235+ */
3236+int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3237+{
5afbbe0d 3238+ aufs_bindex_t bindex, bbot;
1facf9fc 3239+
5afbbe0d
AM
3240+ bbot = au_sbbot(sb);
3241+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 3242+ if (au_sbr_id(sb, bindex) == br_id)
3243+ return bindex;
3244+ return -1;
3245+}
3246+
3247+/* ---------------------------------------------------------------------- */
3248+
3249+/*
3250+ * add a branch
3251+ */
3252+
b752ccd1
AM
3253+static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3254+ struct dentry *h_root)
1facf9fc 3255+{
b752ccd1
AM
3256+ if (unlikely(h_adding == h_root
3257+ || au_test_loopback_overlap(sb, h_adding)))
1facf9fc 3258+ return 1;
b752ccd1
AM
3259+ if (h_adding->d_sb != h_root->d_sb)
3260+ return 0;
3261+ return au_test_subdir(h_adding, h_root)
3262+ || au_test_subdir(h_root, h_adding);
1facf9fc 3263+}
3264+
3265+/*
3266+ * returns a newly allocated branch. @new_nbranch is a number of branches
3267+ * after adding a branch.
3268+ */
3269+static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3270+ int perm)
3271+{
3272+ struct au_branch *add_branch;
3273+ struct dentry *root;
5527c038 3274+ struct inode *inode;
4a4d8108 3275+ int err;
1facf9fc 3276+
4a4d8108 3277+ err = -ENOMEM;
be52b249 3278+ add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
1facf9fc 3279+ if (unlikely(!add_branch))
3280+ goto out;
062440b3
AM
3281+ add_branch->br_xino = au_xino_alloc();
3282+ if (unlikely(!add_branch->br_xino))
521ced18 3283+ goto out_br;
027c5e7a
AM
3284+ err = au_hnotify_init_br(add_branch, perm);
3285+ if (unlikely(err))
062440b3 3286+ goto out_xino;
027c5e7a 3287+
1facf9fc 3288+ if (au_br_writable(perm)) {
3289+ /* may be freed separately at changing the branch permission */
be52b249 3290+ add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
1facf9fc 3291+ GFP_NOFS);
3292+ if (unlikely(!add_branch->br_wbr))
027c5e7a 3293+ goto out_hnotify;
1facf9fc 3294+ }
3295+
076b876e
AM
3296+ if (au_br_fhsm(perm)) {
3297+ err = au_fhsm_br_alloc(add_branch);
3298+ if (unlikely(err))
3299+ goto out_wbr;
3300+ }
3301+
521ced18 3302+ root = sb->s_root;
e2f27e51 3303+ err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
4a4d8108 3304+ if (!err)
e2f27e51 3305+ err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
5527c038
JR
3306+ if (!err) {
3307+ inode = d_inode(root);
1c60b727
AM
3308+ err = au_hinode_realloc(au_ii(inode), new_nbranch,
3309+ /*may_shrink*/0);
5527c038 3310+ }
4a4d8108
AM
3311+ if (!err)
3312+ return add_branch; /* success */
1facf9fc 3313+
076b876e 3314+out_wbr:
1c60b727 3315+ kfree(add_branch->br_wbr);
027c5e7a
AM
3316+out_hnotify:
3317+ au_hnotify_fin_br(add_branch);
062440b3
AM
3318+out_xino:
3319+ au_xino_put(add_branch);
4f0767ce 3320+out_br:
1c60b727 3321+ kfree(add_branch);
4f0767ce 3322+out:
4a4d8108 3323+ return ERR_PTR(err);
1facf9fc 3324+}
3325+
3326+/*
3327+ * test if the branch permission is legal or not.
3328+ */
3329+static int test_br(struct inode *inode, int brperm, char *path)
3330+{
3331+ int err;
3332+
4a4d8108
AM
3333+ err = (au_br_writable(brperm) && IS_RDONLY(inode));
3334+ if (!err)
3335+ goto out;
1facf9fc 3336+
4a4d8108
AM
3337+ err = -EINVAL;
3338+ pr_err("write permission for readonly mount or inode, %s\n", path);
3339+
4f0767ce 3340+out:
1facf9fc 3341+ return err;
3342+}
3343+
3344+/*
3345+ * returns:
3346+ * 0: success, the caller will add it
3347+ * plus: success, it is already unified, the caller should ignore it
3348+ * minus: error
3349+ */
3350+static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3351+{
3352+ int err;
5afbbe0d 3353+ aufs_bindex_t bbot, bindex;
5527c038 3354+ struct dentry *root, *h_dentry;
1facf9fc 3355+ struct inode *inode, *h_inode;
3356+
3357+ root = sb->s_root;
5afbbe0d
AM
3358+ bbot = au_sbbot(sb);
3359+ if (unlikely(bbot >= 0
1facf9fc 3360+ && au_find_dbindex(root, add->path.dentry) >= 0)) {
3361+ err = 1;
3362+ if (!remount) {
3363+ err = -EINVAL;
4a4d8108 3364+ pr_err("%s duplicated\n", add->pathname);
1facf9fc 3365+ }
3366+ goto out;
3367+ }
3368+
3369+ err = -ENOSPC; /* -E2BIG; */
3370+ if (unlikely(AUFS_BRANCH_MAX <= add->bindex
5afbbe0d 3371+ || AUFS_BRANCH_MAX - 1 <= bbot)) {
4a4d8108 3372+ pr_err("number of branches exceeded %s\n", add->pathname);
1facf9fc 3373+ goto out;
3374+ }
3375+
3376+ err = -EDOM;
5afbbe0d 3377+ if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
4a4d8108 3378+ pr_err("bad index %d\n", add->bindex);
1facf9fc 3379+ goto out;
3380+ }
3381+
5527c038 3382+ inode = d_inode(add->path.dentry);
1facf9fc 3383+ err = -ENOENT;
3384+ if (unlikely(!inode->i_nlink)) {
4a4d8108 3385+ pr_err("no existence %s\n", add->pathname);
1facf9fc 3386+ goto out;
3387+ }
3388+
3389+ err = -EINVAL;
3390+ if (unlikely(inode->i_sb == sb)) {
4a4d8108 3391+ pr_err("%s must be outside\n", add->pathname);
1facf9fc 3392+ goto out;
3393+ }
3394+
3395+ if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
4a4d8108
AM
3396+ pr_err("unsupported filesystem, %s (%s)\n",
3397+ add->pathname, au_sbtype(inode->i_sb));
1facf9fc 3398+ goto out;
3399+ }
3400+
c1595e42
JR
3401+ if (unlikely(inode->i_sb->s_stack_depth)) {
3402+ pr_err("already stacked, %s (%s)\n",
3403+ add->pathname, au_sbtype(inode->i_sb));
3404+ goto out;
3405+ }
3406+
5527c038 3407+ err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
1facf9fc 3408+ if (unlikely(err))
3409+ goto out;
3410+
5afbbe0d 3411+ if (bbot < 0)
1facf9fc 3412+ return 0; /* success */
3413+
3414+ err = -EINVAL;
5afbbe0d 3415+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 3416+ if (unlikely(test_overlap(sb, add->path.dentry,
3417+ au_h_dptr(root, bindex)))) {
4a4d8108 3418+ pr_err("%s is overlapped\n", add->pathname);
1facf9fc 3419+ goto out;
3420+ }
3421+
3422+ err = 0;
3423+ if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
5527c038
JR
3424+ h_dentry = au_h_dptr(root, 0);
3425+ h_inode = d_inode(h_dentry);
1facf9fc 3426+ if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
0c3ec466
AM
3427+ || !uid_eq(h_inode->i_uid, inode->i_uid)
3428+ || !gid_eq(h_inode->i_gid, inode->i_gid))
3429+ pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3430+ add->pathname,
3431+ i_uid_read(inode), i_gid_read(inode),
3432+ (inode->i_mode & S_IALLUGO),
3433+ i_uid_read(h_inode), i_gid_read(h_inode),
3434+ (h_inode->i_mode & S_IALLUGO));
1facf9fc 3435+ }
3436+
4f0767ce 3437+out:
1facf9fc 3438+ return err;
3439+}
3440+
3441+/*
3442+ * initialize or clean the whiteouts for an adding branch
3443+ */
3444+static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
86dc4139 3445+ int new_perm)
1facf9fc 3446+{
3447+ int err, old_perm;
3448+ aufs_bindex_t bindex;
febd17d6 3449+ struct inode *h_inode;
1facf9fc 3450+ struct au_wbr *wbr;
3451+ struct au_hinode *hdir;
5527c038 3452+ struct dentry *h_dentry;
1facf9fc 3453+
86dc4139
AM
3454+ err = vfsub_mnt_want_write(au_br_mnt(br));
3455+ if (unlikely(err))
3456+ goto out;
3457+
1facf9fc 3458+ wbr = br->br_wbr;
3459+ old_perm = br->br_perm;
3460+ br->br_perm = new_perm;
3461+ hdir = NULL;
febd17d6 3462+ h_inode = NULL;
1facf9fc 3463+ bindex = au_br_index(sb, br->br_id);
3464+ if (0 <= bindex) {
5527c038 3465+ hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 3466+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 3467+ } else {
5527c038 3468+ h_dentry = au_br_dentry(br);
febd17d6
JR
3469+ h_inode = d_inode(h_dentry);
3470+ inode_lock_nested(h_inode, AuLsc_I_PARENT);
1facf9fc 3471+ }
3472+ if (!wbr)
86dc4139 3473+ err = au_wh_init(br, sb);
1facf9fc 3474+ else {
3475+ wbr_wh_write_lock(wbr);
86dc4139 3476+ err = au_wh_init(br, sb);
1facf9fc 3477+ wbr_wh_write_unlock(wbr);
3478+ }
3479+ if (hdir)
5afbbe0d 3480+ au_hn_inode_unlock(hdir);
1facf9fc 3481+ else
febd17d6 3482+ inode_unlock(h_inode);
86dc4139 3483+ vfsub_mnt_drop_write(au_br_mnt(br));
1facf9fc 3484+ br->br_perm = old_perm;
3485+
3486+ if (!err && wbr && !au_br_writable(new_perm)) {
1c60b727 3487+ kfree(wbr);
1facf9fc 3488+ br->br_wbr = NULL;
3489+ }
3490+
86dc4139 3491+out:
1facf9fc 3492+ return err;
3493+}
3494+
3495+static int au_wbr_init(struct au_branch *br, struct super_block *sb,
86dc4139 3496+ int perm)
1facf9fc 3497+{
3498+ int err;
4a4d8108 3499+ struct kstatfs kst;
1facf9fc 3500+ struct au_wbr *wbr;
3501+
3502+ wbr = br->br_wbr;
dece6358 3503+ au_rw_init(&wbr->wbr_wh_rwsem);
1facf9fc 3504+ atomic_set(&wbr->wbr_wh_running, 0);
1facf9fc 3505+
4a4d8108
AM
3506+ /*
3507+ * a limit for rmdir/rename a dir
523b37e3 3508+ * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
4a4d8108 3509+ */
86dc4139 3510+ err = vfs_statfs(&br->br_path, &kst);
4a4d8108
AM
3511+ if (unlikely(err))
3512+ goto out;
3513+ err = -EINVAL;
3514+ if (kst.f_namelen >= NAME_MAX)
86dc4139 3515+ err = au_br_init_wh(sb, br, perm);
4a4d8108 3516+ else
523b37e3
AM
3517+ pr_err("%pd(%s), unsupported namelen %ld\n",
3518+ au_br_dentry(br),
86dc4139 3519+ au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
1facf9fc 3520+
4f0767ce 3521+out:
1facf9fc 3522+ return err;
3523+}
3524+
c1595e42 3525+/* initialize a new branch */
1facf9fc 3526+static int au_br_init(struct au_branch *br, struct super_block *sb,
3527+ struct au_opt_add *add)
3528+{
3529+ int err;
062440b3
AM
3530+ struct au_branch *brbase;
3531+ struct file *xf;
5527c038 3532+ struct inode *h_inode;
1facf9fc 3533+
3534+ err = 0;
1facf9fc 3535+ br->br_perm = add->perm;
86dc4139 3536+ br->br_path = add->path; /* set first, path_get() later */
4a4d8108 3537+ spin_lock_init(&br->br_dykey_lock);
5afbbe0d 3538+ au_br_count_init(br);
1facf9fc 3539+ br->br_id = au_new_br_id(sb);
7f207e10 3540+ AuDebugOn(br->br_id < 0);
1facf9fc 3541+
8b6a4947
AM
3542+ /* always, regardless the given option */
3543+ err = au_dr_br_init(sb, br, &add->path);
3544+ if (unlikely(err))
3545+ goto out_err;
3546+
1facf9fc 3547+ if (au_br_writable(add->perm)) {
86dc4139 3548+ err = au_wbr_init(br, sb, add->perm);
1facf9fc 3549+ if (unlikely(err))
b752ccd1 3550+ goto out_err;
1facf9fc 3551+ }
3552+
3553+ if (au_opt_test(au_mntflags(sb), XINO)) {
062440b3
AM
3554+ brbase = au_sbr(sb, 0);
3555+ xf = au_xino_file(brbase);
3556+ AuDebugOn(!xf);
5527c038 3557+ h_inode = d_inode(add->path.dentry);
062440b3 3558+ err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
1facf9fc 3559+ if (unlikely(err)) {
062440b3 3560+ AuDebugOn(au_xino_file(br));
b752ccd1 3561+ goto out_err;
1facf9fc 3562+ }
3563+ }
3564+
3565+ sysaufs_br_init(br);
86dc4139 3566+ path_get(&br->br_path);
b752ccd1 3567+ goto out; /* success */
1facf9fc 3568+
4f0767ce 3569+out_err:
86dc4139 3570+ memset(&br->br_path, 0, sizeof(br->br_path));
4f0767ce 3571+out:
1facf9fc 3572+ return err;
3573+}
3574+
3575+static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
5afbbe0d 3576+ struct au_branch *br, aufs_bindex_t bbot,
1facf9fc 3577+ aufs_bindex_t amount)
3578+{
3579+ struct au_branch **brp;
3580+
dece6358
AM
3581+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3582+
1facf9fc 3583+ brp = sbinfo->si_branch + bindex;
3584+ memmove(brp + 1, brp, sizeof(*brp) * amount);
3585+ *brp = br;
5afbbe0d
AM
3586+ sbinfo->si_bbot++;
3587+ if (unlikely(bbot < 0))
3588+ sbinfo->si_bbot = 0;
1facf9fc 3589+}
3590+
3591+static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
5afbbe0d 3592+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3593+{
3594+ struct au_hdentry *hdp;
3595+
1308ab2a 3596+ AuRwMustWriteLock(&dinfo->di_rwsem);
3597+
5afbbe0d 3598+ hdp = au_hdentry(dinfo, bindex);
1facf9fc 3599+ memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3600+ au_h_dentry_init(hdp);
5afbbe0d
AM
3601+ dinfo->di_bbot++;
3602+ if (unlikely(bbot < 0))
3603+ dinfo->di_btop = 0;
1facf9fc 3604+}
3605+
3606+static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
5afbbe0d 3607+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3608+{
3609+ struct au_hinode *hip;
3610+
1308ab2a 3611+ AuRwMustWriteLock(&iinfo->ii_rwsem);
3612+
5afbbe0d 3613+ hip = au_hinode(iinfo, bindex);
1facf9fc 3614+ memmove(hip + 1, hip, sizeof(*hip) * amount);
5afbbe0d
AM
3615+ au_hinode_init(hip);
3616+ iinfo->ii_bbot++;
3617+ if (unlikely(bbot < 0))
3618+ iinfo->ii_btop = 0;
1facf9fc 3619+}
3620+
86dc4139
AM
3621+static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3622+ aufs_bindex_t bindex)
1facf9fc 3623+{
86dc4139 3624+ struct dentry *root, *h_dentry;
5527c038 3625+ struct inode *root_inode, *h_inode;
5afbbe0d 3626+ aufs_bindex_t bbot, amount;
1facf9fc 3627+
3628+ root = sb->s_root;
5527c038 3629+ root_inode = d_inode(root);
5afbbe0d
AM
3630+ bbot = au_sbbot(sb);
3631+ amount = bbot + 1 - bindex;
86dc4139 3632+ h_dentry = au_br_dentry(br);
53392da6 3633+ au_sbilist_lock();
5afbbe0d
AM
3634+ au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3635+ au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3636+ au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
1facf9fc 3637+ au_set_h_dptr(root, bindex, dget(h_dentry));
5527c038
JR
3638+ h_inode = d_inode(h_dentry);
3639+ au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
53392da6 3640+ au_sbilist_unlock();
1facf9fc 3641+}
3642+
3643+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3644+{
3645+ int err;
5afbbe0d 3646+ aufs_bindex_t bbot, add_bindex;
1facf9fc 3647+ struct dentry *root, *h_dentry;
3648+ struct inode *root_inode;
3649+ struct au_branch *add_branch;
062440b3 3650+ struct file *xf;
1facf9fc 3651+
3652+ root = sb->s_root;
5527c038 3653+ root_inode = d_inode(root);
1facf9fc 3654+ IMustLock(root_inode);
5afbbe0d 3655+ IiMustWriteLock(root_inode);
1facf9fc 3656+ err = test_add(sb, add, remount);
3657+ if (unlikely(err < 0))
3658+ goto out;
3659+ if (err) {
3660+ err = 0;
3661+ goto out; /* success */
3662+ }
3663+
5afbbe0d
AM
3664+ bbot = au_sbbot(sb);
3665+ add_branch = au_br_alloc(sb, bbot + 2, add->perm);
1facf9fc 3666+ err = PTR_ERR(add_branch);
3667+ if (IS_ERR(add_branch))
3668+ goto out;
3669+
3670+ err = au_br_init(add_branch, sb, add);
3671+ if (unlikely(err)) {
3672+ au_br_do_free(add_branch);
3673+ goto out;
3674+ }
3675+
3676+ add_bindex = add->bindex;
062440b3
AM
3677+ sysaufs_brs_del(sb, add_bindex); /* remove successors */
3678+ au_br_do_add(sb, add_branch, add_bindex);
3679+ sysaufs_brs_add(sb, add_bindex); /* append successors */
3680+ dbgaufs_brs_add(sb, add_bindex, /*topdown*/0); /* rename successors */
1facf9fc 3681+
86dc4139 3682+ h_dentry = add->path.dentry;
1308ab2a 3683+ if (!add_bindex) {
1facf9fc 3684+ au_cpup_attr_all(root_inode, /*force*/1);
1308ab2a 3685+ sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3686+ } else
5527c038 3687+ au_add_nlink(root_inode, d_inode(h_dentry));
1facf9fc 3688+
3689+ /*
062440b3 3690+ * this test/set prevents aufs from handling unnecessary notify events
027c5e7a 3691+ * of xino files, in case of re-adding a writable branch which was
1facf9fc 3692+ * once detached from aufs.
3693+ */
3694+ if (au_xino_brid(sb) < 0
3695+ && au_br_writable(add_branch->br_perm)
062440b3
AM
3696+ && !au_test_fs_bad_xino(h_dentry->d_sb)) {
3697+ xf = au_xino_file(add_branch);
3698+ if (xf && xf->f_path.dentry->d_parent == h_dentry)
3699+ au_xino_brid_set(sb, add_branch->br_id);
3700+ }
1facf9fc 3701+
4f0767ce 3702+out:
1facf9fc 3703+ return err;
3704+}
3705+
3706+/* ---------------------------------------------------------------------- */
3707+
79b8bda9 3708+static unsigned long long au_farray_cb(struct super_block *sb, void *a,
076b876e
AM
3709+ unsigned long long max __maybe_unused,
3710+ void *arg)
3711+{
3712+ unsigned long long n;
3713+ struct file **p, *f;
8b6a4947
AM
3714+ struct hlist_bl_head *files;
3715+ struct hlist_bl_node *pos;
076b876e 3716+ struct au_finfo *finfo;
076b876e
AM
3717+
3718+ n = 0;
3719+ p = a;
3720+ files = &au_sbi(sb)->si_files;
8b6a4947
AM
3721+ hlist_bl_lock(files);
3722+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
076b876e
AM
3723+ f = finfo->fi_file;
3724+ if (file_count(f)
3725+ && !special_file(file_inode(f)->i_mode)) {
3726+ get_file(f);
3727+ *p++ = f;
3728+ n++;
3729+ AuDebugOn(n > max);
3730+ }
3731+ }
8b6a4947 3732+ hlist_bl_unlock(files);
076b876e
AM
3733+
3734+ return n;
3735+}
3736+
3737+static struct file **au_farray_alloc(struct super_block *sb,
3738+ unsigned long long *max)
3739+{
5afbbe0d 3740+ *max = au_nfiles(sb);
79b8bda9 3741+ return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
076b876e
AM
3742+}
3743+
3744+static void au_farray_free(struct file **a, unsigned long long max)
3745+{
3746+ unsigned long long ull;
3747+
3748+ for (ull = 0; ull < max; ull++)
3749+ if (a[ull])
3750+ fput(a[ull]);
be52b249 3751+ kvfree(a);
076b876e
AM
3752+}
3753+
3754+/* ---------------------------------------------------------------------- */
3755+
1facf9fc 3756+/*
3757+ * delete a branch
3758+ */
3759+
3760+/* to show the line number, do not make it inlined function */
4a4d8108 3761+#define AuVerbose(do_info, fmt, ...) do { \
1facf9fc 3762+ if (do_info) \
4a4d8108 3763+ pr_info(fmt, ##__VA_ARGS__); \
1facf9fc 3764+} while (0)
3765+
5afbbe0d
AM
3766+static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3767+ aufs_bindex_t bbot)
027c5e7a 3768+{
5afbbe0d 3769+ return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
027c5e7a
AM
3770+}
3771+
5afbbe0d
AM
3772+static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3773+ aufs_bindex_t bbot)
027c5e7a 3774+{
5afbbe0d 3775+ return au_test_ibusy(d_inode(dentry), btop, bbot);
027c5e7a
AM
3776+}
3777+
1facf9fc 3778+/*
3779+ * test if the branch is deletable or not.
3780+ */
3781+static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
b752ccd1 3782+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3783+{
3784+ int err, i, j, ndentry;
5afbbe0d 3785+ aufs_bindex_t btop, bbot;
1facf9fc 3786+ struct au_dcsub_pages dpages;
3787+ struct au_dpage *dpage;
3788+ struct dentry *d;
1facf9fc 3789+
3790+ err = au_dpages_init(&dpages, GFP_NOFS);
3791+ if (unlikely(err))
3792+ goto out;
3793+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
3794+ if (unlikely(err))
3795+ goto out_dpages;
3796+
1facf9fc 3797+ for (i = 0; !err && i < dpages.ndpage; i++) {
3798+ dpage = dpages.dpages + i;
3799+ ndentry = dpage->ndentry;
3800+ for (j = 0; !err && j < ndentry; j++) {
3801+ d = dpage->dentries[j];
c1595e42 3802+ AuDebugOn(au_dcount(d) <= 0);
027c5e7a 3803+ if (!au_digen_test(d, sigen)) {
1facf9fc 3804+ di_read_lock_child(d, AuLock_IR);
027c5e7a
AM
3805+ if (unlikely(au_dbrange_test(d))) {
3806+ di_read_unlock(d, AuLock_IR);
3807+ continue;
3808+ }
3809+ } else {
1facf9fc 3810+ di_write_lock_child(d);
027c5e7a
AM
3811+ if (unlikely(au_dbrange_test(d))) {
3812+ di_write_unlock(d);
3813+ continue;
3814+ }
1facf9fc 3815+ err = au_reval_dpath(d, sigen);
3816+ if (!err)
3817+ di_downgrade_lock(d, AuLock_IR);
3818+ else {
3819+ di_write_unlock(d);
3820+ break;
3821+ }
3822+ }
3823+
027c5e7a 3824+ /* AuDbgDentry(d); */
5afbbe0d
AM
3825+ btop = au_dbtop(d);
3826+ bbot = au_dbbot(d);
3827+ if (btop <= bindex
3828+ && bindex <= bbot
1facf9fc 3829+ && au_h_dptr(d, bindex)
5afbbe0d 3830+ && au_test_dbusy(d, btop, bbot)) {
1facf9fc 3831+ err = -EBUSY;
523b37e3 3832+ AuVerbose(verbose, "busy %pd\n", d);
027c5e7a 3833+ AuDbgDentry(d);
1facf9fc 3834+ }
3835+ di_read_unlock(d, AuLock_IR);
3836+ }
3837+ }
3838+
4f0767ce 3839+out_dpages:
1facf9fc 3840+ au_dpages_free(&dpages);
4f0767ce 3841+out:
1facf9fc 3842+ return err;
3843+}
3844+
3845+static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
b752ccd1 3846+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3847+{
3848+ int err;
7f207e10
AM
3849+ unsigned long long max, ull;
3850+ struct inode *i, **array;
5afbbe0d 3851+ aufs_bindex_t btop, bbot;
1facf9fc 3852+
7f207e10
AM
3853+ array = au_iarray_alloc(sb, &max);
3854+ err = PTR_ERR(array);
3855+ if (IS_ERR(array))
3856+ goto out;
3857+
1facf9fc 3858+ err = 0;
7f207e10
AM
3859+ AuDbg("b%d\n", bindex);
3860+ for (ull = 0; !err && ull < max; ull++) {
3861+ i = array[ull];
076b876e
AM
3862+ if (unlikely(!i))
3863+ break;
7f207e10 3864+ if (i->i_ino == AUFS_ROOT_INO)
1facf9fc 3865+ continue;
3866+
7f207e10 3867+ /* AuDbgInode(i); */
537831f9 3868+ if (au_iigen(i, NULL) == sigen)
1facf9fc 3869+ ii_read_lock_child(i);
3870+ else {
3871+ ii_write_lock_child(i);
027c5e7a
AM
3872+ err = au_refresh_hinode_self(i);
3873+ au_iigen_dec(i);
1facf9fc 3874+ if (!err)
3875+ ii_downgrade_lock(i);
3876+ else {
3877+ ii_write_unlock(i);
3878+ break;
3879+ }
3880+ }
3881+
5afbbe0d
AM
3882+ btop = au_ibtop(i);
3883+ bbot = au_ibbot(i);
3884+ if (btop <= bindex
3885+ && bindex <= bbot
1facf9fc 3886+ && au_h_iptr(i, bindex)
5afbbe0d 3887+ && au_test_ibusy(i, btop, bbot)) {
1facf9fc 3888+ err = -EBUSY;
3889+ AuVerbose(verbose, "busy i%lu\n", i->i_ino);
7f207e10 3890+ AuDbgInode(i);
1facf9fc 3891+ }
3892+ ii_read_unlock(i);
3893+ }
7f207e10 3894+ au_iarray_free(array, max);
1facf9fc 3895+
7f207e10 3896+out:
1facf9fc 3897+ return err;
3898+}
3899+
b752ccd1
AM
3900+static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3901+ const unsigned int verbose)
1facf9fc 3902+{
3903+ int err;
3904+ unsigned int sigen;
3905+
3906+ sigen = au_sigen(root->d_sb);
3907+ DiMustNoWaiters(root);
5527c038 3908+ IiMustNoWaiters(d_inode(root));
1facf9fc 3909+ di_write_unlock(root);
b752ccd1 3910+ err = test_dentry_busy(root, bindex, sigen, verbose);
1facf9fc 3911+ if (!err)
b752ccd1 3912+ err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
1facf9fc 3913+ di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3914+
3915+ return err;
3916+}
3917+
076b876e
AM
3918+static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3919+ struct file **to_free, int *idx)
3920+{
3921+ int err;
c1595e42 3922+ unsigned char matched, root;
5afbbe0d 3923+ aufs_bindex_t bindex, bbot;
076b876e
AM
3924+ struct au_fidir *fidir;
3925+ struct au_hfile *hfile;
3926+
3927+ err = 0;
2000de60 3928+ root = IS_ROOT(file->f_path.dentry);
c1595e42
JR
3929+ if (root) {
3930+ get_file(file);
3931+ to_free[*idx] = file;
3932+ (*idx)++;
3933+ goto out;
3934+ }
3935+
076b876e 3936+ matched = 0;
076b876e
AM
3937+ fidir = au_fi(file)->fi_hdir;
3938+ AuDebugOn(!fidir);
5afbbe0d
AM
3939+ bbot = au_fbbot_dir(file);
3940+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
076b876e
AM
3941+ hfile = fidir->fd_hfile + bindex;
3942+ if (!hfile->hf_file)
3943+ continue;
3944+
c1595e42 3945+ if (hfile->hf_br->br_id == br_id) {
076b876e 3946+ matched = 1;
076b876e 3947+ break;
c1595e42 3948+ }
076b876e 3949+ }
c1595e42 3950+ if (matched)
076b876e
AM
3951+ err = -EBUSY;
3952+
3953+out:
3954+ return err;
3955+}
3956+
3957+static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3958+ struct file **to_free, int opened)
3959+{
3960+ int err, idx;
3961+ unsigned long long ull, max;
5afbbe0d 3962+ aufs_bindex_t btop;
076b876e 3963+ struct file *file, **array;
076b876e
AM
3964+ struct dentry *root;
3965+ struct au_hfile *hfile;
3966+
3967+ array = au_farray_alloc(sb, &max);
3968+ err = PTR_ERR(array);
3969+ if (IS_ERR(array))
3970+ goto out;
3971+
3972+ err = 0;
3973+ idx = 0;
3974+ root = sb->s_root;
3975+ di_write_unlock(root);
3976+ for (ull = 0; ull < max; ull++) {
3977+ file = array[ull];
3978+ if (unlikely(!file))
3979+ break;
3980+
3981+ /* AuDbg("%pD\n", file); */
3982+ fi_read_lock(file);
5afbbe0d 3983+ btop = au_fbtop(file);
2000de60 3984+ if (!d_is_dir(file->f_path.dentry)) {
076b876e
AM
3985+ hfile = &au_fi(file)->fi_htop;
3986+ if (hfile->hf_br->br_id == br_id)
3987+ err = -EBUSY;
3988+ } else
3989+ err = test_dir_busy(file, br_id, to_free, &idx);
3990+ fi_read_unlock(file);
3991+ if (unlikely(err))
3992+ break;
3993+ }
3994+ di_write_lock_child(root);
3995+ au_farray_free(array, max);
3996+ AuDebugOn(idx > opened);
3997+
3998+out:
3999+ return err;
4000+}
4001+
4002+static void br_del_file(struct file **to_free, unsigned long long opened,
062440b3 4003+ aufs_bindex_t br_id)
076b876e
AM
4004+{
4005+ unsigned long long ull;
5afbbe0d 4006+ aufs_bindex_t bindex, btop, bbot, bfound;
076b876e
AM
4007+ struct file *file;
4008+ struct au_fidir *fidir;
4009+ struct au_hfile *hfile;
4010+
4011+ for (ull = 0; ull < opened; ull++) {
4012+ file = to_free[ull];
4013+ if (unlikely(!file))
4014+ break;
4015+
4016+ /* AuDbg("%pD\n", file); */
2000de60 4017+ AuDebugOn(!d_is_dir(file->f_path.dentry));
076b876e
AM
4018+ bfound = -1;
4019+ fidir = au_fi(file)->fi_hdir;
4020+ AuDebugOn(!fidir);
4021+ fi_write_lock(file);
5afbbe0d
AM
4022+ btop = au_fbtop(file);
4023+ bbot = au_fbbot_dir(file);
4024+ for (bindex = btop; bindex <= bbot; bindex++) {
076b876e
AM
4025+ hfile = fidir->fd_hfile + bindex;
4026+ if (!hfile->hf_file)
4027+ continue;
4028+
4029+ if (hfile->hf_br->br_id == br_id) {
4030+ bfound = bindex;
4031+ break;
4032+ }
4033+ }
4034+ AuDebugOn(bfound < 0);
4035+ au_set_h_fptr(file, bfound, NULL);
5afbbe0d
AM
4036+ if (bfound == btop) {
4037+ for (btop++; btop <= bbot; btop++)
4038+ if (au_hf_dir(file, btop)) {
4039+ au_set_fbtop(file, btop);
076b876e
AM
4040+ break;
4041+ }
4042+ }
4043+ fi_write_unlock(file);
4044+ }
4045+}
4046+
1facf9fc 4047+static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
4048+ const aufs_bindex_t bindex,
5afbbe0d 4049+ const aufs_bindex_t bbot)
1facf9fc 4050+{
4051+ struct au_branch **brp, **p;
4052+
dece6358
AM
4053+ AuRwMustWriteLock(&sbinfo->si_rwsem);
4054+
1facf9fc 4055+ brp = sbinfo->si_branch + bindex;
5afbbe0d
AM
4056+ if (bindex < bbot)
4057+ memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
4058+ sbinfo->si_branch[0 + bbot] = NULL;
4059+ sbinfo->si_bbot--;
1facf9fc 4060+
e2f27e51
AM
4061+ p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
4062+ /*may_shrink*/1);
1facf9fc 4063+ if (p)
4064+ sbinfo->si_branch = p;
4a4d8108 4065+ /* harmless error */
1facf9fc 4066+}
4067+
4068+static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
5afbbe0d 4069+ const aufs_bindex_t bbot)
1facf9fc 4070+{
4071+ struct au_hdentry *hdp, *p;
4072+
1308ab2a 4073+ AuRwMustWriteLock(&dinfo->di_rwsem);
4074+
5afbbe0d
AM
4075+ hdp = au_hdentry(dinfo, bindex);
4076+ if (bindex < bbot)
4077+ memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
4078+ /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
4079+ dinfo->di_bbot--;
1facf9fc 4080+
e2f27e51
AM
4081+ p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
4082+ /*may_shrink*/1);
1facf9fc 4083+ if (p)
4084+ dinfo->di_hdentry = p;
4a4d8108 4085+ /* harmless error */
1facf9fc 4086+}
4087+
4088+static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
5afbbe0d 4089+ const aufs_bindex_t bbot)
1facf9fc 4090+{
4091+ struct au_hinode *hip, *p;
4092+
1308ab2a 4093+ AuRwMustWriteLock(&iinfo->ii_rwsem);
4094+
5afbbe0d
AM
4095+ hip = au_hinode(iinfo, bindex);
4096+ if (bindex < bbot)
4097+ memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
4098+ /* au_hinode_init(au_hinode(iinfo, bbot)); */
4099+ iinfo->ii_bbot--;
1facf9fc 4100+
e2f27e51
AM
4101+ p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
4102+ /*may_shrink*/1);
1facf9fc 4103+ if (p)
4104+ iinfo->ii_hinode = p;
4a4d8108 4105+ /* harmless error */
1facf9fc 4106+}
4107+
4108+static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
4109+ struct au_branch *br)
4110+{
5afbbe0d 4111+ aufs_bindex_t bbot;
1facf9fc 4112+ struct au_sbinfo *sbinfo;
53392da6
AM
4113+ struct dentry *root, *h_root;
4114+ struct inode *inode, *h_inode;
4115+ struct au_hinode *hinode;
1facf9fc 4116+
dece6358
AM
4117+ SiMustWriteLock(sb);
4118+
1facf9fc 4119+ root = sb->s_root;
5527c038 4120+ inode = d_inode(root);
1facf9fc 4121+ sbinfo = au_sbi(sb);
5afbbe0d 4122+ bbot = sbinfo->si_bbot;
1facf9fc 4123+
53392da6
AM
4124+ h_root = au_h_dptr(root, bindex);
4125+ hinode = au_hi(inode, bindex);
4126+ h_inode = au_igrab(hinode->hi_inode);
4127+ au_hiput(hinode);
1facf9fc 4128+
53392da6 4129+ au_sbilist_lock();
5afbbe0d
AM
4130+ au_br_do_del_brp(sbinfo, bindex, bbot);
4131+ au_br_do_del_hdp(au_di(root), bindex, bbot);
4132+ au_br_do_del_hip(au_ii(inode), bindex, bbot);
53392da6
AM
4133+ au_sbilist_unlock();
4134+
8b6a4947
AM
4135+ /* ignore an error */
4136+ au_dr_br_fin(sb, br); /* always, regardless the mount option */
4137+
53392da6
AM
4138+ dput(h_root);
4139+ iput(h_inode);
4140+ au_br_do_free(br);
1facf9fc 4141+}
4142+
79b8bda9
AM
4143+static unsigned long long empty_cb(struct super_block *sb, void *array,
4144+ unsigned long long max, void *arg)
076b876e
AM
4145+{
4146+ return max;
4147+}
4148+
1facf9fc 4149+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
4150+{
4151+ int err, rerr, i;
076b876e 4152+ unsigned long long opened;
1facf9fc 4153+ unsigned int mnt_flags;
5afbbe0d 4154+ aufs_bindex_t bindex, bbot, br_id;
1facf9fc 4155+ unsigned char do_wh, verbose;
4156+ struct au_branch *br;
4157+ struct au_wbr *wbr;
076b876e
AM
4158+ struct dentry *root;
4159+ struct file **to_free;
1facf9fc 4160+
4161+ err = 0;
076b876e
AM
4162+ opened = 0;
4163+ to_free = NULL;
4164+ root = sb->s_root;
4165+ bindex = au_find_dbindex(root, del->h_path.dentry);
1facf9fc 4166+ if (bindex < 0) {
4167+ if (remount)
4168+ goto out; /* success */
4169+ err = -ENOENT;
4a4d8108 4170+ pr_err("%s no such branch\n", del->pathname);
1facf9fc 4171+ goto out;
4172+ }
4173+ AuDbg("bindex b%d\n", bindex);
4174+
4175+ err = -EBUSY;
4176+ mnt_flags = au_mntflags(sb);
4177+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
5afbbe0d
AM
4178+ bbot = au_sbbot(sb);
4179+ if (unlikely(!bbot)) {
1facf9fc 4180+ AuVerbose(verbose, "no more branches left\n");
4181+ goto out;
4182+ }
4183+ br = au_sbr(sb, bindex);
86dc4139 4184+ AuDebugOn(!path_equal(&br->br_path, &del->h_path));
076b876e
AM
4185+
4186+ br_id = br->br_id;
5afbbe0d 4187+ opened = au_br_count(br);
076b876e 4188+ if (unlikely(opened)) {
79b8bda9 4189+ to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
076b876e
AM
4190+ err = PTR_ERR(to_free);
4191+ if (IS_ERR(to_free))
4192+ goto out;
4193+
4194+ err = test_file_busy(sb, br_id, to_free, opened);
4195+ if (unlikely(err)) {
4196+ AuVerbose(verbose, "%llu file(s) opened\n", opened);
4197+ goto out;
4198+ }
1facf9fc 4199+ }
4200+
4201+ wbr = br->br_wbr;
4202+ do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
4203+ if (do_wh) {
1308ab2a 4204+ /* instead of WbrWhMustWriteLock(wbr) */
4205+ SiMustWriteLock(sb);
1facf9fc 4206+ for (i = 0; i < AuBrWh_Last; i++) {
4207+ dput(wbr->wbr_wh[i]);
4208+ wbr->wbr_wh[i] = NULL;
4209+ }
4210+ }
4211+
076b876e 4212+ err = test_children_busy(root, bindex, verbose);
1facf9fc 4213+ if (unlikely(err)) {
4214+ if (do_wh)
4215+ goto out_wh;
4216+ goto out;
4217+ }
4218+
4219+ err = 0;
076b876e
AM
4220+ if (to_free) {
4221+ /*
4222+ * now we confirmed the branch is deletable.
4223+ * let's free the remaining opened dirs on the branch.
4224+ */
4225+ di_write_unlock(root);
4226+ br_del_file(to_free, opened, br_id);
4227+ di_write_lock_child(root);
4228+ }
4229+
062440b3
AM
4230+ sysaufs_brs_del(sb, bindex); /* remove successors */
4231+ dbgaufs_xino_del(br); /* remove one */
4232+ au_br_do_del(sb, bindex, br);
4233+ sysaufs_brs_add(sb, bindex); /* append successors */
4234+ dbgaufs_brs_add(sb, bindex, /*topdown*/1); /* rename successors */
1facf9fc 4235+
1308ab2a 4236+ if (!bindex) {
5527c038 4237+ au_cpup_attr_all(d_inode(root), /*force*/1);
1308ab2a 4238+ sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
4239+ } else
5527c038 4240+ au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
1facf9fc 4241+ if (au_opt_test(mnt_flags, PLINK))
4242+ au_plink_half_refresh(sb, br_id);
4243+
b752ccd1 4244+ if (au_xino_brid(sb) == br_id)
1facf9fc 4245+ au_xino_brid_set(sb, -1);
4246+ goto out; /* success */
4247+
4f0767ce 4248+out_wh:
1facf9fc 4249+ /* revert */
86dc4139 4250+ rerr = au_br_init_wh(sb, br, br->br_perm);
1facf9fc 4251+ if (rerr)
0c3ec466
AM
4252+ pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4253+ del->pathname, rerr);
4f0767ce 4254+out:
076b876e
AM
4255+ if (to_free)
4256+ au_farray_free(to_free, opened);
1facf9fc 4257+ return err;
4258+}
4259+
4260+/* ---------------------------------------------------------------------- */
4261+
027c5e7a
AM
4262+static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4263+{
4264+ int err;
5afbbe0d 4265+ aufs_bindex_t btop, bbot;
027c5e7a
AM
4266+ struct aufs_ibusy ibusy;
4267+ struct inode *inode, *h_inode;
4268+
4269+ err = -EPERM;
4270+ if (unlikely(!capable(CAP_SYS_ADMIN)))
4271+ goto out;
4272+
4273+ err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4274+ if (!err)
4275+ err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
4276+ if (unlikely(err)) {
4277+ err = -EFAULT;
4278+ AuTraceErr(err);
4279+ goto out;
4280+ }
4281+
4282+ err = -EINVAL;
4283+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d 4284+ if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
027c5e7a
AM
4285+ goto out_unlock;
4286+
4287+ err = 0;
4288+ ibusy.h_ino = 0; /* invalid */
4289+ inode = ilookup(sb, ibusy.ino);
4290+ if (!inode
4291+ || inode->i_ino == AUFS_ROOT_INO
5afbbe0d 4292+ || au_is_bad_inode(inode))
027c5e7a
AM
4293+ goto out_unlock;
4294+
4295+ ii_read_lock_child(inode);
5afbbe0d
AM
4296+ btop = au_ibtop(inode);
4297+ bbot = au_ibbot(inode);
4298+ if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
027c5e7a 4299+ h_inode = au_h_iptr(inode, ibusy.bindex);
5afbbe0d 4300+ if (h_inode && au_test_ibusy(inode, btop, bbot))
027c5e7a
AM
4301+ ibusy.h_ino = h_inode->i_ino;
4302+ }
4303+ ii_read_unlock(inode);
4304+ iput(inode);
4305+
4306+out_unlock:
4307+ si_read_unlock(sb);
4308+ if (!err) {
4309+ err = __put_user(ibusy.h_ino, &arg->h_ino);
4310+ if (unlikely(err)) {
4311+ err = -EFAULT;
4312+ AuTraceErr(err);
4313+ }
4314+ }
4315+out:
4316+ return err;
4317+}
4318+
4319+long au_ibusy_ioctl(struct file *file, unsigned long arg)
4320+{
2000de60 4321+ return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
027c5e7a
AM
4322+}
4323+
4324+#ifdef CONFIG_COMPAT
4325+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4326+{
2000de60 4327+ return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
027c5e7a
AM
4328+}
4329+#endif
4330+
4331+/* ---------------------------------------------------------------------- */
4332+
1facf9fc 4333+/*
4334+ * change a branch permission
4335+ */
4336+
dece6358
AM
4337+static void au_warn_ima(void)
4338+{
4339+#ifdef CONFIG_IMA
1308ab2a 4340+ /* since it doesn't support mark_files_ro() */
027c5e7a 4341+ AuWarn1("RW -> RO makes IMA to produce wrong message\n");
dece6358
AM
4342+#endif
4343+}
4344+
1facf9fc 4345+static int do_need_sigen_inc(int a, int b)
4346+{
4347+ return au_br_whable(a) && !au_br_whable(b);
4348+}
4349+
4350+static int need_sigen_inc(int old, int new)
4351+{
4352+ return do_need_sigen_inc(old, new)
4353+ || do_need_sigen_inc(new, old);
4354+}
4355+
4356+static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4357+{
7f207e10 4358+ int err, do_warn;
027c5e7a 4359+ unsigned int mnt_flags;
7f207e10 4360+ unsigned long long ull, max;
e49829fe 4361+ aufs_bindex_t br_id;
38d290e6 4362+ unsigned char verbose, writer;
7f207e10 4363+ struct file *file, *hf, **array;
e49829fe 4364+ struct au_hfile *hfile;
1facf9fc 4365+
027c5e7a
AM
4366+ mnt_flags = au_mntflags(sb);
4367+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
4368+
7f207e10
AM
4369+ array = au_farray_alloc(sb, &max);
4370+ err = PTR_ERR(array);
4371+ if (IS_ERR(array))
1facf9fc 4372+ goto out;
4373+
7f207e10 4374+ do_warn = 0;
e49829fe 4375+ br_id = au_sbr_id(sb, bindex);
7f207e10
AM
4376+ for (ull = 0; ull < max; ull++) {
4377+ file = array[ull];
076b876e
AM
4378+ if (unlikely(!file))
4379+ break;
1facf9fc 4380+
523b37e3 4381+ /* AuDbg("%pD\n", file); */
1facf9fc 4382+ fi_read_lock(file);
4383+ if (unlikely(au_test_mmapped(file))) {
4384+ err = -EBUSY;
523b37e3 4385+ AuVerbose(verbose, "mmapped %pD\n", file);
7f207e10 4386+ AuDbgFile(file);
1facf9fc 4387+ FiMustNoWaiters(file);
4388+ fi_read_unlock(file);
7f207e10 4389+ goto out_array;
1facf9fc 4390+ }
4391+
e49829fe
JR
4392+ hfile = &au_fi(file)->fi_htop;
4393+ hf = hfile->hf_file;
7e9cd9fe 4394+ if (!d_is_reg(file->f_path.dentry)
1facf9fc 4395+ || !(file->f_mode & FMODE_WRITE)
e49829fe 4396+ || hfile->hf_br->br_id != br_id
7f207e10
AM
4397+ || !(hf->f_mode & FMODE_WRITE))
4398+ array[ull] = NULL;
4399+ else {
4400+ do_warn = 1;
4401+ get_file(file);
1facf9fc 4402+ }
4403+
1facf9fc 4404+ FiMustNoWaiters(file);
4405+ fi_read_unlock(file);
7f207e10
AM
4406+ fput(file);
4407+ }
1facf9fc 4408+
4409+ err = 0;
7f207e10 4410+ if (do_warn)
dece6358 4411+ au_warn_ima();
7f207e10
AM
4412+
4413+ for (ull = 0; ull < max; ull++) {
4414+ file = array[ull];
4415+ if (!file)
4416+ continue;
4417+
1facf9fc 4418+ /* todo: already flushed? */
523b37e3
AM
4419+ /*
4420+ * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4421+ * approach which resets f_mode and calls mnt_drop_write() and
4422+ * file_release_write() for each file, because the branch
4423+ * attribute in aufs world is totally different from the native
4424+ * fs rw/ro mode.
4425+ */
7f207e10
AM
4426+ /* fi_read_lock(file); */
4427+ hfile = &au_fi(file)->fi_htop;
4428+ hf = hfile->hf_file;
4429+ /* fi_read_unlock(file); */
027c5e7a 4430+ spin_lock(&hf->f_lock);
38d290e6
JR
4431+ writer = !!(hf->f_mode & FMODE_WRITER);
4432+ hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
027c5e7a 4433+ spin_unlock(&hf->f_lock);
38d290e6
JR
4434+ if (writer) {
4435+ put_write_access(file_inode(hf));
c06a8ce3 4436+ __mnt_drop_write(hf->f_path.mnt);
1facf9fc 4437+ }
4438+ }
4439+
7f207e10
AM
4440+out_array:
4441+ au_farray_free(array, max);
4f0767ce 4442+out:
7f207e10 4443+ AuTraceErr(err);
1facf9fc 4444+ return err;
4445+}
4446+
4447+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4448+ int *do_refresh)
1facf9fc 4449+{
4450+ int err, rerr;
4451+ aufs_bindex_t bindex;
4452+ struct dentry *root;
4453+ struct au_branch *br;
076b876e 4454+ struct au_br_fhsm *bf;
1facf9fc 4455+
4456+ root = sb->s_root;
1facf9fc 4457+ bindex = au_find_dbindex(root, mod->h_root);
4458+ if (bindex < 0) {
4459+ if (remount)
4460+ return 0; /* success */
4461+ err = -ENOENT;
4a4d8108 4462+ pr_err("%s no such branch\n", mod->path);
1facf9fc 4463+ goto out;
4464+ }
4465+ AuDbg("bindex b%d\n", bindex);
4466+
5527c038 4467+ err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
1facf9fc 4468+ if (unlikely(err))
4469+ goto out;
4470+
4471+ br = au_sbr(sb, bindex);
86dc4139 4472+ AuDebugOn(mod->h_root != au_br_dentry(br));
1facf9fc 4473+ if (br->br_perm == mod->perm)
4474+ return 0; /* success */
4475+
076b876e
AM
4476+ /* pre-allocate for non-fhsm --> fhsm */
4477+ bf = NULL;
4478+ if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4479+ err = au_fhsm_br_alloc(br);
4480+ if (unlikely(err))
4481+ goto out;
4482+ bf = br->br_fhsm;
4483+ br->br_fhsm = NULL;
4484+ }
4485+
1facf9fc 4486+ if (au_br_writable(br->br_perm)) {
4487+ /* remove whiteout base */
86dc4139 4488+ err = au_br_init_wh(sb, br, mod->perm);
1facf9fc 4489+ if (unlikely(err))
076b876e 4490+ goto out_bf;
1facf9fc 4491+
4492+ if (!au_br_writable(mod->perm)) {
4493+ /* rw --> ro, file might be mmapped */
4494+ DiMustNoWaiters(root);
5527c038 4495+ IiMustNoWaiters(d_inode(root));
1facf9fc 4496+ di_write_unlock(root);
4497+ err = au_br_mod_files_ro(sb, bindex);
4498+ /* aufs_write_lock() calls ..._child() */
4499+ di_write_lock_child(root);
4500+
4501+ if (unlikely(err)) {
4502+ rerr = -ENOMEM;
be52b249 4503+ br->br_wbr = kzalloc(sizeof(*br->br_wbr),
1facf9fc 4504+ GFP_NOFS);
86dc4139
AM
4505+ if (br->br_wbr)
4506+ rerr = au_wbr_init(br, sb, br->br_perm);
1facf9fc 4507+ if (unlikely(rerr)) {
4508+ AuIOErr("nested error %d (%d)\n",
4509+ rerr, err);
4510+ br->br_perm = mod->perm;
4511+ }
4512+ }
4513+ }
4514+ } else if (au_br_writable(mod->perm)) {
4515+ /* ro --> rw */
4516+ err = -ENOMEM;
be52b249 4517+ br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
1facf9fc 4518+ if (br->br_wbr) {
86dc4139 4519+ err = au_wbr_init(br, sb, mod->perm);
1facf9fc 4520+ if (unlikely(err)) {
1c60b727 4521+ kfree(br->br_wbr);
1facf9fc 4522+ br->br_wbr = NULL;
4523+ }
4524+ }
4525+ }
076b876e
AM
4526+ if (unlikely(err))
4527+ goto out_bf;
4528+
4529+ if (au_br_fhsm(br->br_perm)) {
4530+ if (!au_br_fhsm(mod->perm)) {
4531+ /* fhsm --> non-fhsm */
4532+ au_br_fhsm_fin(br->br_fhsm);
1c60b727 4533+ kfree(br->br_fhsm);
076b876e
AM
4534+ br->br_fhsm = NULL;
4535+ }
4536+ } else if (au_br_fhsm(mod->perm))
4537+ /* non-fhsm --> fhsm */
4538+ br->br_fhsm = bf;
4539+
076b876e
AM
4540+ *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4541+ br->br_perm = mod->perm;
4542+ goto out; /* success */
1facf9fc 4543+
076b876e 4544+out_bf:
8b6a4947 4545+ kfree(bf);
076b876e
AM
4546+out:
4547+ AuTraceErr(err);
4548+ return err;
4549+}
4550+
4551+/* ---------------------------------------------------------------------- */
4552+
4553+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4554+{
4555+ int err;
4556+ struct kstatfs kstfs;
4557+
4558+ err = vfs_statfs(&br->br_path, &kstfs);
1facf9fc 4559+ if (!err) {
076b876e
AM
4560+ stfs->f_blocks = kstfs.f_blocks;
4561+ stfs->f_bavail = kstfs.f_bavail;
4562+ stfs->f_files = kstfs.f_files;
4563+ stfs->f_ffree = kstfs.f_ffree;
1facf9fc 4564+ }
4565+
1facf9fc 4566+ return err;
4567+}
7f207e10
AM
4568diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4569--- /usr/share/empty/fs/aufs/branch.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 4570+++ linux/fs/aufs/branch.h 2018-08-12 23:43:05.453457863 +0200
062440b3
AM
4571@@ -0,0 +1,374 @@
4572+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 4573+/*
b00004a5 4574+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 4575+ *
4576+ * This program, aufs is free software; you can redistribute it and/or modify
4577+ * it under the terms of the GNU General Public License as published by
4578+ * the Free Software Foundation; either version 2 of the License, or
4579+ * (at your option) any later version.
dece6358
AM
4580+ *
4581+ * This program is distributed in the hope that it will be useful,
4582+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4583+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4584+ * GNU General Public License for more details.
4585+ *
4586+ * You should have received a copy of the GNU General Public License
523b37e3 4587+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 4588+ */
4589+
4590+/*
4591+ * branch filesystems and xino for them
4592+ */
4593+
4594+#ifndef __AUFS_BRANCH_H__
4595+#define __AUFS_BRANCH_H__
4596+
4597+#ifdef __KERNEL__
4598+
1facf9fc 4599+#include <linux/mount.h>
8b6a4947 4600+#include "dirren.h"
4a4d8108 4601+#include "dynop.h"
1facf9fc 4602+#include "rwsem.h"
4603+#include "super.h"
4604+
4605+/* ---------------------------------------------------------------------- */
4606+
4607+/* a xino file */
062440b3 4608+struct au_xino {
1facf9fc 4609+ struct file *xi_file;
521ced18
JR
4610+ struct {
4611+ spinlock_t spin;
4612+ ino_t *array;
4613+ int total;
4614+ /* reserved for future use */
4615+ /* unsigned long *bitmap; */
4616+ wait_queue_head_t wqh;
4617+ } xi_nondir;
1facf9fc 4618+
062440b3 4619+ atomic_t xi_truncating;
1facf9fc 4620+
062440b3
AM
4621+ struct kref xi_kref;
4622+
4623+ /* todo: make xino files an array to support huge inode number */
1facf9fc 4624+};
4625+
076b876e
AM
4626+/* File-based Hierarchical Storage Management */
4627+struct au_br_fhsm {
4628+#ifdef CONFIG_AUFS_FHSM
4629+ struct mutex bf_lock;
4630+ unsigned long bf_jiffy;
4631+ struct aufs_stfs bf_stfs;
4632+ int bf_readable;
4633+#endif
4634+};
4635+
1facf9fc 4636+/* members for writable branch only */
4637+enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4638+struct au_wbr {
dece6358 4639+ struct au_rwsem wbr_wh_rwsem;
1facf9fc 4640+ struct dentry *wbr_wh[AuBrWh_Last];
4a4d8108 4641+ atomic_t wbr_wh_running;
1facf9fc 4642+#define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
4643+#define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
4644+#define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
4645+
4646+ /* mfs mode */
4647+ unsigned long long wbr_bytes;
4648+};
4649+
4a4d8108
AM
4650+/* ext2 has 3 types of operations at least, ext3 has 4 */
4651+#define AuBrDynOp (AuDyLast * 4)
4652+
1716fcea
AM
4653+#ifdef CONFIG_AUFS_HFSNOTIFY
4654+/* support for asynchronous destruction */
4655+struct au_br_hfsnotify {
4656+ struct fsnotify_group *hfsn_group;
4657+};
4658+#endif
4659+
392086de
AM
4660+/* sysfs entries */
4661+struct au_brsysfs {
4662+ char name[16];
4663+ struct attribute attr;
4664+};
4665+
4666+enum {
4667+ AuBrSysfs_BR,
4668+ AuBrSysfs_BRID,
4669+ AuBrSysfs_Last
4670+};
4671+
1facf9fc 4672+/* protected by superblock rwsem */
4673+struct au_branch {
062440b3 4674+ struct au_xino *br_xino;
1facf9fc 4675+
4676+ aufs_bindex_t br_id;
4677+
4678+ int br_perm;
86dc4139 4679+ struct path br_path;
4a4d8108
AM
4680+ spinlock_t br_dykey_lock;
4681+ struct au_dykey *br_dykey[AuBrDynOp];
5afbbe0d 4682+ struct percpu_counter br_count;
1facf9fc 4683+
4684+ struct au_wbr *br_wbr;
076b876e 4685+ struct au_br_fhsm *br_fhsm;
1facf9fc 4686+
027c5e7a 4687+#ifdef CONFIG_AUFS_HFSNOTIFY
1716fcea 4688+ struct au_br_hfsnotify *br_hfsn;
027c5e7a
AM
4689+#endif
4690+
1facf9fc 4691+#ifdef CONFIG_SYSFS
392086de
AM
4692+ /* entries under sysfs per mount-point */
4693+ struct au_brsysfs br_sysfs[AuBrSysfs_Last];
1facf9fc 4694+#endif
8b6a4947 4695+
062440b3
AM
4696+#ifdef CONFIG_DEBUG_FS
4697+ struct dentry *br_dbgaufs; /* xino */
4698+#endif
4699+
8b6a4947 4700+ struct au_dr_br br_dirren;
1facf9fc 4701+};
4702+
4703+/* ---------------------------------------------------------------------- */
4704+
86dc4139
AM
4705+static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4706+{
4707+ return br->br_path.mnt;
4708+}
4709+
4710+static inline struct dentry *au_br_dentry(struct au_branch *br)
4711+{
4712+ return br->br_path.dentry;
4713+}
4714+
4715+static inline struct super_block *au_br_sb(struct au_branch *br)
4716+{
4717+ return au_br_mnt(br)->mnt_sb;
4718+}
4719+
5afbbe0d
AM
4720+static inline void au_br_get(struct au_branch *br)
4721+{
4722+ percpu_counter_inc(&br->br_count);
4723+}
4724+
4725+static inline void au_br_put(struct au_branch *br)
4726+{
4727+ percpu_counter_dec(&br->br_count);
4728+}
4729+
4730+static inline s64 au_br_count(struct au_branch *br)
4731+{
4732+ return percpu_counter_sum(&br->br_count);
4733+}
4734+
4735+static inline void au_br_count_init(struct au_branch *br)
4736+{
4737+ percpu_counter_init(&br->br_count, 0, GFP_NOFS);
4738+}
4739+
4740+static inline void au_br_count_fin(struct au_branch *br)
4741+{
4742+ percpu_counter_destroy(&br->br_count);
4743+}
4744+
1facf9fc 4745+static inline int au_br_rdonly(struct au_branch *br)
4746+{
8b6a4947 4747+ return (sb_rdonly(au_br_sb(br))
1facf9fc 4748+ || !au_br_writable(br->br_perm))
4749+ ? -EROFS : 0;
4750+}
4751+
4a4d8108 4752+static inline int au_br_hnotifyable(int brperm __maybe_unused)
1facf9fc 4753+{
4a4d8108 4754+#ifdef CONFIG_AUFS_HNOTIFY
1e00d052 4755+ return !(brperm & AuBrPerm_RR);
1facf9fc 4756+#else
4757+ return 0;
4758+#endif
4759+}
4760+
b912730e
AM
4761+static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4762+{
4763+ int err, exec_flag;
4764+
4765+ err = 0;
4766+ exec_flag = oflag & __FMODE_EXEC;
79b8bda9 4767+ if (unlikely(exec_flag && path_noexec(&br->br_path)))
b912730e
AM
4768+ err = -EACCES;
4769+
4770+ return err;
4771+}
4772+
062440b3
AM
4773+static inline void au_xino_get(struct au_branch *br)
4774+{
4775+ struct au_xino *xi;
4776+
4777+ xi = br->br_xino;
4778+ if (xi)
4779+ kref_get(&xi->xi_kref);
4780+}
4781+
4782+static inline int au_xino_count(struct au_branch *br)
4783+{
4784+ int v;
4785+ struct au_xino *xi;
4786+
4787+ v = 0;
4788+ xi = br->br_xino;
4789+ if (xi)
4790+ v = kref_read(&xi->xi_kref);
4791+
4792+ return v;
4793+}
4794+
4795+static inline struct file *au_xino_file(struct au_branch *br)
4796+{
4797+ struct au_xino *xi;
4798+
4799+ xi = br->br_xino;
4800+ return xi ? xi->xi_file : NULL;
4801+}
4802+
1facf9fc 4803+/* ---------------------------------------------------------------------- */
4804+
4805+/* branch.c */
4806+struct au_sbinfo;
4807+void au_br_free(struct au_sbinfo *sinfo);
4808+int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4809+struct au_opt_add;
4810+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4811+struct au_opt_del;
4812+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
027c5e7a
AM
4813+long au_ibusy_ioctl(struct file *file, unsigned long arg);
4814+#ifdef CONFIG_COMPAT
4815+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4816+#endif
1facf9fc 4817+struct au_opt_mod;
4818+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4819+ int *do_refresh);
076b876e
AM
4820+struct aufs_stfs;
4821+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
1facf9fc 4822+
4823+/* xino.c */
4824+static const loff_t au_loff_max = LLONG_MAX;
4825+
062440b3
AM
4826+struct file *au_xino_create(struct super_block *sb, char *fpath, int silent);
4827+struct file *au_xino_create2(struct super_block *sb, struct path *base,
4828+ struct file *copy_src);
4829+
4830+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4831+ ino_t *ino);
4832+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4833+ ino_t ino);
5527c038 4834+ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
1facf9fc 4835+ loff_t *pos);
5527c038
JR
4836+ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
4837+ size_t size, loff_t *pos);
062440b3
AM
4838+
4839+int au_xib_trunc(struct super_block *sb);
1facf9fc 4840+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex);
4841+
062440b3
AM
4842+struct au_xino *au_xino_alloc(void);
4843+int au_xino_put(struct au_branch *br);
4844+void au_xino_file_set(struct au_branch *br, struct file *file);
4845+
1facf9fc 4846+struct au_opt_xino;
1facf9fc 4847+void au_xino_clr(struct super_block *sb);
062440b3 4848+int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
1facf9fc 4849+struct file *au_xino_def(struct super_block *sb);
062440b3
AM
4850+int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4851+ struct path *base);
4852+
4853+ino_t au_xino_new_ino(struct super_block *sb);
4854+void au_xino_delete_inode(struct inode *inode, const int unlinked);
1facf9fc 4855+
521ced18
JR
4856+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4857+ ino_t h_ino, int idx);
4858+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4859+ int *idx);
4860+
062440b3
AM
4861+int au_xino_path(struct seq_file *seq, struct file *file);
4862+
1facf9fc 4863+/* ---------------------------------------------------------------------- */
4864+
4865+/* Superblock to branch */
4866+static inline
4867+aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4868+{
4869+ return au_sbr(sb, bindex)->br_id;
4870+}
4871+
4872+static inline
4873+struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4874+{
86dc4139 4875+ return au_br_mnt(au_sbr(sb, bindex));
1facf9fc 4876+}
4877+
4878+static inline
4879+struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4880+{
86dc4139 4881+ return au_br_sb(au_sbr(sb, bindex));
1facf9fc 4882+}
4883+
5afbbe0d
AM
4884+static inline void au_sbr_get(struct super_block *sb, aufs_bindex_t bindex)
4885+{
4886+ au_br_get(au_sbr(sb, bindex));
4887+}
4888+
1facf9fc 4889+static inline void au_sbr_put(struct super_block *sb, aufs_bindex_t bindex)
4890+{
5afbbe0d 4891+ au_br_put(au_sbr(sb, bindex));
1facf9fc 4892+}
4893+
4894+static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4895+{
4896+ return au_sbr(sb, bindex)->br_perm;
4897+}
4898+
4899+static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4900+{
4901+ return au_br_whable(au_sbr_perm(sb, bindex));
4902+}
4903+
4904+/* ---------------------------------------------------------------------- */
4905+
8b6a4947
AM
4906+#define wbr_wh_read_lock(wbr) au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4907+#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4908+#define wbr_wh_read_trylock(wbr) au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4909+#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
1facf9fc 4910+/*
8b6a4947
AM
4911+#define wbr_wh_read_trylock_nested(wbr) \
4912+ au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4913+#define wbr_wh_write_trylock_nested(wbr) \
4914+ au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4915+*/
1facf9fc 4916+
8b6a4947
AM
4917+#define wbr_wh_read_unlock(wbr) au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4918+#define wbr_wh_write_unlock(wbr) au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4919+#define wbr_wh_downgrade_lock(wbr) au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4920+
4921+#define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4922+#define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4923+#define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
dece6358 4924+
076b876e
AM
4925+/* ---------------------------------------------------------------------- */
4926+
4927+#ifdef CONFIG_AUFS_FHSM
4928+static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4929+{
4930+ mutex_init(&brfhsm->bf_lock);
4931+ brfhsm->bf_jiffy = 0;
4932+ brfhsm->bf_readable = 0;
4933+}
4934+
4935+static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4936+{
4937+ mutex_destroy(&brfhsm->bf_lock);
4938+}
4939+#else
4940+AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4941+AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4942+#endif
4943+
1facf9fc 4944+#endif /* __KERNEL__ */
4945+#endif /* __AUFS_BRANCH_H__ */
7f207e10
AM
4946diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4947--- /usr/share/empty/fs/aufs/conf.mk 1970-01-01 01:00:00.000000000 +0100
b00004a5 4948+++ linux/fs/aufs/conf.mk 2018-06-04 09:08:09.181412645 +0200
2121bcd9
AM
4949@@ -0,0 +1,40 @@
4950+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
4951+
4952+AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4953+
4954+define AuConf
4955+ifdef ${1}
4956+AuConfStr += ${1}=${${1}}
4957+endif
4958+endef
4959+
b752ccd1 4960+AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
e49829fe 4961+ SBILIST \
7f207e10 4962+ HNOTIFY HFSNOTIFY \
4a4d8108 4963+ EXPORT INO_T_64 \
c1595e42 4964+ XATTR \
076b876e 4965+ FHSM \
4a4d8108 4966+ RDU \
8b6a4947 4967+ DIRREN \
4a4d8108
AM
4968+ SHWH \
4969+ BR_RAMFS \
4970+ BR_FUSE POLL \
4971+ BR_HFSPLUS \
4972+ BDEV_LOOP \
b752ccd1
AM
4973+ DEBUG MAGIC_SYSRQ
4974+$(foreach i, ${AuConfAll}, \
4a4d8108
AM
4975+ $(eval $(call AuConf,CONFIG_AUFS_${i})))
4976+
4977+AuConfName = ${obj}/conf.str
4978+${AuConfName}.tmp: FORCE
4979+ @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4980+${AuConfName}: ${AuConfName}.tmp
4981+ @diff -q $< $@ > /dev/null 2>&1 || { \
4982+ echo ' GEN ' $@; \
4983+ cp -p $< $@; \
4984+ }
4985+FORCE:
4986+clean-files += ${AuConfName} ${AuConfName}.tmp
4987+${obj}/sysfs.o: ${AuConfName}
b752ccd1
AM
4988+
4989+-include ${srctree}/${src}/conf_priv.mk
7f207e10
AM
4990diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4991--- /usr/share/empty/fs/aufs/cpup.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9
AM
4992+++ linux/fs/aufs/cpup.c 2018-08-12 23:43:05.453457863 +0200
4993@@ -0,0 +1,1441 @@
4994+// SPDX-License-Identifier: GPL-2.0
1facf9fc 4995+/*
b00004a5 4996+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 4997+ *
4998+ * This program, aufs is free software; you can redistribute it and/or modify
4999+ * it under the terms of the GNU General Public License as published by
5000+ * the Free Software Foundation; either version 2 of the License, or
5001+ * (at your option) any later version.
dece6358
AM
5002+ *
5003+ * This program is distributed in the hope that it will be useful,
5004+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
5005+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5006+ * GNU General Public License for more details.
5007+ *
5008+ * You should have received a copy of the GNU General Public License
523b37e3 5009+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 5010+ */
5011+
5012+/*
5013+ * copy-up functions, see wbr_policy.c for copy-down
5014+ */
5015+
5016+#include <linux/fs_stack.h>
dece6358 5017+#include <linux/mm.h>
8cdd5066 5018+#include <linux/task_work.h>
1facf9fc 5019+#include "aufs.h"
5020+
86dc4139 5021+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
1facf9fc 5022+{
5023+ const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
367653fa 5024+ | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
1facf9fc 5025+
86dc4139
AM
5026+ BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
5027+
5028+ dst->i_flags |= iflags & ~mask;
1facf9fc 5029+ if (au_test_fs_notime(dst->i_sb))
5030+ dst->i_flags |= S_NOATIME | S_NOCMTIME;
5031+}
5032+
5033+void au_cpup_attr_timesizes(struct inode *inode)
5034+{
5035+ struct inode *h_inode;
5036+
5afbbe0d 5037+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 5038+ fsstack_copy_attr_times(inode, h_inode);
4a4d8108 5039+ fsstack_copy_inode_size(inode, h_inode);
1facf9fc 5040+}
5041+
5042+void au_cpup_attr_nlink(struct inode *inode, int force)
5043+{
5044+ struct inode *h_inode;
5045+ struct super_block *sb;
5afbbe0d 5046+ aufs_bindex_t bindex, bbot;
1facf9fc 5047+
5048+ sb = inode->i_sb;
5afbbe0d 5049+ bindex = au_ibtop(inode);
1facf9fc 5050+ h_inode = au_h_iptr(inode, bindex);
5051+ if (!force
5052+ && !S_ISDIR(h_inode->i_mode)
5053+ && au_opt_test(au_mntflags(sb), PLINK)
5054+ && au_plink_test(inode))
5055+ return;
5056+
7eafdf33
AM
5057+ /*
5058+ * 0 can happen in revalidating.
38d290e6
JR
5059+ * h_inode->i_mutex may not be held here, but it is harmless since once
5060+ * i_nlink reaches 0, it will never become positive except O_TMPFILE
5061+ * case.
5062+ * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
5063+ * the incorrect link count.
7eafdf33 5064+ */
92d182d2 5065+ set_nlink(inode, h_inode->i_nlink);
1facf9fc 5066+
5067+ /*
5068+ * fewer nlink makes find(1) noisy, but larger nlink doesn't.
5069+ * it may includes whplink directory.
5070+ */
5071+ if (S_ISDIR(h_inode->i_mode)) {
5afbbe0d
AM
5072+ bbot = au_ibbot(inode);
5073+ for (bindex++; bindex <= bbot; bindex++) {
1facf9fc 5074+ h_inode = au_h_iptr(inode, bindex);
5075+ if (h_inode)
5076+ au_add_nlink(inode, h_inode);
5077+ }
5078+ }
5079+}
5080+
5081+void au_cpup_attr_changeable(struct inode *inode)
5082+{
5083+ struct inode *h_inode;
5084+
5afbbe0d 5085+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 5086+ inode->i_mode = h_inode->i_mode;
5087+ inode->i_uid = h_inode->i_uid;
5088+ inode->i_gid = h_inode->i_gid;
5089+ au_cpup_attr_timesizes(inode);
86dc4139 5090+ au_cpup_attr_flags(inode, h_inode->i_flags);
1facf9fc 5091+}
5092+
5093+void au_cpup_igen(struct inode *inode, struct inode *h_inode)
5094+{
5095+ struct au_iinfo *iinfo = au_ii(inode);
5096+
1308ab2a 5097+ IiMustWriteLock(inode);
5098+
1facf9fc 5099+ iinfo->ii_higen = h_inode->i_generation;
5100+ iinfo->ii_hsb1 = h_inode->i_sb;
5101+}
5102+
5103+void au_cpup_attr_all(struct inode *inode, int force)
5104+{
5105+ struct inode *h_inode;
5106+
5afbbe0d 5107+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 5108+ au_cpup_attr_changeable(inode);
5109+ if (inode->i_nlink > 0)
5110+ au_cpup_attr_nlink(inode, force);
5111+ inode->i_rdev = h_inode->i_rdev;
5112+ inode->i_blkbits = h_inode->i_blkbits;
5113+ au_cpup_igen(inode, h_inode);
5114+}
5115+
5116+/* ---------------------------------------------------------------------- */
5117+
5118+/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
5119+
5120+/* keep the timestamps of the parent dir when cpup */
5121+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
5122+ struct path *h_path)
5123+{
5124+ struct inode *h_inode;
5125+
5126+ dt->dt_dentry = dentry;
5127+ dt->dt_h_path = *h_path;
5527c038 5128+ h_inode = d_inode(h_path->dentry);
1facf9fc 5129+ dt->dt_atime = h_inode->i_atime;
5130+ dt->dt_mtime = h_inode->i_mtime;
5131+ /* smp_mb(); */
5132+}
5133+
5134+void au_dtime_revert(struct au_dtime *dt)
5135+{
5136+ struct iattr attr;
5137+ int err;
5138+
5139+ attr.ia_atime = dt->dt_atime;
5140+ attr.ia_mtime = dt->dt_mtime;
5141+ attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
5142+ | ATTR_ATIME | ATTR_ATIME_SET;
5143+
523b37e3
AM
5144+ /* no delegation since this is a directory */
5145+ err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
1facf9fc 5146+ if (unlikely(err))
0c3ec466 5147+ pr_warn("restoring timestamps failed(%d). ignored\n", err);
1facf9fc 5148+}
5149+
5150+/* ---------------------------------------------------------------------- */
5151+
86dc4139
AM
5152+/* internal use only */
5153+struct au_cpup_reg_attr {
5154+ int valid;
5155+ struct kstat st;
5156+ unsigned int iflags; /* inode->i_flags */
5157+};
5158+
1facf9fc 5159+static noinline_for_stack
86dc4139
AM
5160+int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
5161+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5162+{
c1595e42 5163+ int err, sbits, icex;
7e9cd9fe
AM
5164+ unsigned int mnt_flags;
5165+ unsigned char verbose;
1facf9fc 5166+ struct iattr ia;
5167+ struct path h_path;
1308ab2a 5168+ struct inode *h_isrc, *h_idst;
86dc4139 5169+ struct kstat *h_st;
c1595e42 5170+ struct au_branch *br;
1facf9fc 5171+
5172+ h_path.dentry = au_h_dptr(dst, bindex);
5527c038 5173+ h_idst = d_inode(h_path.dentry);
c1595e42
JR
5174+ br = au_sbr(dst->d_sb, bindex);
5175+ h_path.mnt = au_br_mnt(br);
5527c038 5176+ h_isrc = d_inode(h_src);
1308ab2a 5177+ ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
1facf9fc 5178+ | ATTR_ATIME | ATTR_MTIME
5179+ | ATTR_ATIME_SET | ATTR_MTIME_SET;
86dc4139
AM
5180+ if (h_src_attr && h_src_attr->valid) {
5181+ h_st = &h_src_attr->st;
5182+ ia.ia_uid = h_st->uid;
5183+ ia.ia_gid = h_st->gid;
5184+ ia.ia_atime = h_st->atime;
5185+ ia.ia_mtime = h_st->mtime;
5186+ if (h_idst->i_mode != h_st->mode
5187+ && !S_ISLNK(h_idst->i_mode)) {
5188+ ia.ia_valid |= ATTR_MODE;
5189+ ia.ia_mode = h_st->mode;
5190+ }
5191+ sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
5192+ au_cpup_attr_flags(h_idst, h_src_attr->iflags);
5193+ } else {
5194+ ia.ia_uid = h_isrc->i_uid;
5195+ ia.ia_gid = h_isrc->i_gid;
5196+ ia.ia_atime = h_isrc->i_atime;
5197+ ia.ia_mtime = h_isrc->i_mtime;
5198+ if (h_idst->i_mode != h_isrc->i_mode
5199+ && !S_ISLNK(h_idst->i_mode)) {
5200+ ia.ia_valid |= ATTR_MODE;
5201+ ia.ia_mode = h_isrc->i_mode;
5202+ }
5203+ sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
5204+ au_cpup_attr_flags(h_idst, h_isrc->i_flags);
1308ab2a 5205+ }
523b37e3
AM
5206+ /* no delegation since it is just created */
5207+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 5208+
5209+ /* is this nfs only? */
5210+ if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
5211+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
5212+ ia.ia_mode = h_isrc->i_mode;
523b37e3 5213+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 5214+ }
5215+
c1595e42 5216+ icex = br->br_perm & AuBrAttr_ICEX;
7e9cd9fe
AM
5217+ if (!err) {
5218+ mnt_flags = au_mntflags(dst->d_sb);
5219+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
5220+ err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
5221+ }
c1595e42 5222+
1facf9fc 5223+ return err;
5224+}
5225+
5226+/* ---------------------------------------------------------------------- */
5227+
5228+static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
5229+ char *buf, unsigned long blksize)
5230+{
5231+ int err;
5232+ size_t sz, rbytes, wbytes;
5233+ unsigned char all_zero;
5234+ char *p, *zp;
febd17d6 5235+ struct inode *h_inode;
1facf9fc 5236+ /* reduce stack usage */
5237+ struct iattr *ia;
5238+
5239+ zp = page_address(ZERO_PAGE(0));
5240+ if (unlikely(!zp))
5241+ return -ENOMEM; /* possible? */
5242+
5243+ err = 0;
5244+ all_zero = 0;
5245+ while (len) {
5246+ AuDbg("len %lld\n", len);
5247+ sz = blksize;
5248+ if (len < blksize)
5249+ sz = len;
5250+
5251+ rbytes = 0;
5252+ /* todo: signal_pending? */
5253+ while (!rbytes || err == -EAGAIN || err == -EINTR) {
5254+ rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5255+ err = rbytes;
5256+ }
5257+ if (unlikely(err < 0))
5258+ break;
5259+
5260+ all_zero = 0;
5261+ if (len >= rbytes && rbytes == blksize)
5262+ all_zero = !memcmp(buf, zp, rbytes);
5263+ if (!all_zero) {
5264+ wbytes = rbytes;
5265+ p = buf;
5266+ while (wbytes) {
5267+ size_t b;
5268+
5269+ b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5270+ err = b;
5271+ /* todo: signal_pending? */
5272+ if (unlikely(err == -EAGAIN || err == -EINTR))
5273+ continue;
5274+ if (unlikely(err < 0))
5275+ break;
5276+ wbytes -= b;
5277+ p += b;
5278+ }
392086de
AM
5279+ if (unlikely(err < 0))
5280+ break;
1facf9fc 5281+ } else {
5282+ loff_t res;
5283+
5284+ AuLabel(hole);
5285+ res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5286+ err = res;
5287+ if (unlikely(res < 0))
5288+ break;
5289+ }
5290+ len -= rbytes;
5291+ err = 0;
5292+ }
5293+
5294+ /* the last block may be a hole */
5295+ if (!err && all_zero) {
5296+ AuLabel(last hole);
5297+
5298+ err = 1;
2000de60 5299+ if (au_test_nfs(dst->f_path.dentry->d_sb)) {
1facf9fc 5300+ /* nfs requires this step to make last hole */
5301+ /* is this only nfs? */
5302+ do {
5303+ /* todo: signal_pending? */
5304+ err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5305+ } while (err == -EAGAIN || err == -EINTR);
5306+ if (err == 1)
5307+ dst->f_pos--;
5308+ }
5309+
5310+ if (err == 1) {
5311+ ia = (void *)buf;
5312+ ia->ia_size = dst->f_pos;
5313+ ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5314+ ia->ia_file = dst;
febd17d6
JR
5315+ h_inode = file_inode(dst);
5316+ inode_lock_nested(h_inode, AuLsc_I_CHILD2);
523b37e3
AM
5317+ /* no delegation since it is just created */
5318+ err = vfsub_notify_change(&dst->f_path, ia,
5319+ /*delegated*/NULL);
febd17d6 5320+ inode_unlock(h_inode);
1facf9fc 5321+ }
5322+ }
5323+
5324+ return err;
5325+}
5326+
5327+int au_copy_file(struct file *dst, struct file *src, loff_t len)
5328+{
5329+ int err;
5330+ unsigned long blksize;
5331+ unsigned char do_kfree;
5332+ char *buf;
5333+
5334+ err = -ENOMEM;
2000de60 5335+ blksize = dst->f_path.dentry->d_sb->s_blocksize;
1facf9fc 5336+ if (!blksize || PAGE_SIZE < blksize)
5337+ blksize = PAGE_SIZE;
5338+ AuDbg("blksize %lu\n", blksize);
5339+ do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5340+ if (do_kfree)
5341+ buf = kmalloc(blksize, GFP_NOFS);
5342+ else
5343+ buf = (void *)__get_free_page(GFP_NOFS);
5344+ if (unlikely(!buf))
5345+ goto out;
5346+
5347+ if (len > (1 << 22))
5348+ AuDbg("copying a large file %lld\n", (long long)len);
5349+
5350+ src->f_pos = 0;
5351+ dst->f_pos = 0;
5352+ err = au_do_copy_file(dst, src, len, buf, blksize);
5353+ if (do_kfree)
1c60b727 5354+ kfree(buf);
1facf9fc 5355+ else
1c60b727 5356+ free_page((unsigned long)buf);
1facf9fc 5357+
4f0767ce 5358+out:
1facf9fc 5359+ return err;
5360+}
5361+
1c60b727
AM
5362+static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5363+{
5364+ int err;
5365+ struct super_block *h_src_sb;
5366+ struct inode *h_src_inode;
5367+
5368+ h_src_inode = file_inode(src);
5369+ h_src_sb = h_src_inode->i_sb;
5370+
5371+ /* XFS acquires inode_lock */
5372+ if (!au_test_xfs(h_src_sb))
5373+ err = au_copy_file(dst, src, len);
5374+ else {
3c1bdaff 5375+ inode_unlock_shared(h_src_inode);
1c60b727 5376+ err = au_copy_file(dst, src, len);
be118d29 5377+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
1c60b727
AM
5378+ }
5379+
5380+ return err;
5381+}
5382+
5383+static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5384+{
5385+ int err;
5386+ struct super_block *h_src_sb;
5387+ struct inode *h_src_inode;
5388+
5389+ h_src_inode = file_inode(src);
5390+ h_src_sb = h_src_inode->i_sb;
5391+ if (h_src_sb != file_inode(dst)->i_sb
5392+ || !dst->f_op->clone_file_range) {
5393+ err = au_do_copy(dst, src, len);
5394+ goto out;
5395+ }
5396+
5397+ if (!au_test_nfs(h_src_sb)) {
3c1bdaff 5398+ inode_unlock_shared(h_src_inode);
1c60b727 5399+ err = vfsub_clone_file_range(src, dst, len);
be118d29 5400+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
1c60b727
AM
5401+ } else
5402+ err = vfsub_clone_file_range(src, dst, len);
5403+ /* older XFS has a condition in cloning */
5404+ if (unlikely(err != -EOPNOTSUPP))
5405+ goto out;
5406+
5407+ /* the backend fs on NFS may not support cloning */
5408+ err = au_do_copy(dst, src, len);
5409+
5410+out:
5411+ AuTraceErr(err);
5412+ return err;
5413+}
5414+
1facf9fc 5415+/*
5416+ * to support a sparse file which is opened with O_APPEND,
5417+ * we need to close the file.
5418+ */
c2b27bf2 5419+static int au_cp_regular(struct au_cp_generic *cpg)
1facf9fc 5420+{
5421+ int err, i;
5422+ enum { SRC, DST };
5423+ struct {
5424+ aufs_bindex_t bindex;
5425+ unsigned int flags;
5426+ struct dentry *dentry;
392086de 5427+ int force_wr;
1facf9fc 5428+ struct file *file;
523b37e3 5429+ void *label;
1facf9fc 5430+ } *f, file[] = {
5431+ {
c2b27bf2 5432+ .bindex = cpg->bsrc,
1facf9fc 5433+ .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
523b37e3 5434+ .label = &&out
1facf9fc 5435+ },
5436+ {
c2b27bf2 5437+ .bindex = cpg->bdst,
1facf9fc 5438+ .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
392086de 5439+ .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
523b37e3 5440+ .label = &&out_src
1facf9fc 5441+ }
5442+ };
521ced18 5443+ struct super_block *sb, *h_src_sb;
e2f27e51 5444+ struct inode *h_src_inode;
8cdd5066 5445+ struct task_struct *tsk = current;
1facf9fc 5446+
5447+ /* bsrc branch can be ro/rw. */
c2b27bf2 5448+ sb = cpg->dentry->d_sb;
1facf9fc 5449+ f = file;
5450+ for (i = 0; i < 2; i++, f++) {
c2b27bf2
AM
5451+ f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5452+ f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
392086de 5453+ /*file*/NULL, f->force_wr);
1facf9fc 5454+ err = PTR_ERR(f->file);
5455+ if (IS_ERR(f->file))
5456+ goto *f->label;
1facf9fc 5457+ }
5458+
5459+ /* try stopping to update while we copyup */
e2f27e51 5460+ h_src_inode = d_inode(file[SRC].dentry);
521ced18
JR
5461+ h_src_sb = h_src_inode->i_sb;
5462+ if (!au_test_nfs(h_src_sb))
e2f27e51 5463+ IMustLock(h_src_inode);
1c60b727 5464+ err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
1facf9fc 5465+
8cdd5066
JR
5466+ /* i wonder if we had O_NO_DELAY_FPUT flag */
5467+ if (tsk->flags & PF_KTHREAD)
5468+ __fput_sync(file[DST].file);
5469+ else {
062440b3 5470+ /* it happened actually */
8cdd5066
JR
5471+ fput(file[DST].file);
5472+ /*
5473+ * too bad.
5474+ * we have to call both since we don't know which place the file
5475+ * was added to.
5476+ */
5477+ task_work_run();
5478+ flush_delayed_fput();
5479+ }
1facf9fc 5480+ au_sbr_put(sb, file[DST].bindex);
523b37e3 5481+
4f0767ce 5482+out_src:
1facf9fc 5483+ fput(file[SRC].file);
5484+ au_sbr_put(sb, file[SRC].bindex);
4f0767ce 5485+out:
1facf9fc 5486+ return err;
5487+}
5488+
c2b27bf2 5489+static int au_do_cpup_regular(struct au_cp_generic *cpg,
86dc4139 5490+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5491+{
5492+ int err, rerr;
5493+ loff_t l;
86dc4139 5494+ struct path h_path;
38d290e6 5495+ struct inode *h_src_inode, *h_dst_inode;
1facf9fc 5496+
5497+ err = 0;
5527c038 5498+ h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
86dc4139 5499+ l = i_size_read(h_src_inode);
c2b27bf2
AM
5500+ if (cpg->len == -1 || l < cpg->len)
5501+ cpg->len = l;
5502+ if (cpg->len) {
86dc4139 5503+ /* try stopping to update while we are referencing */
be118d29 5504+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
c2b27bf2 5505+ au_pin_hdir_unlock(cpg->pin);
1facf9fc 5506+
c2b27bf2
AM
5507+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5508+ h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
86dc4139 5509+ h_src_attr->iflags = h_src_inode->i_flags;
5527c038 5510+ if (!au_test_nfs(h_src_inode->i_sb))
521ced18 5511+ err = vfsub_getattr(&h_path, &h_src_attr->st);
5527c038 5512+ else {
3c1bdaff 5513+ inode_unlock_shared(h_src_inode);
521ced18 5514+ err = vfsub_getattr(&h_path, &h_src_attr->st);
be118d29 5515+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5527c038 5516+ }
86dc4139 5517+ if (unlikely(err)) {
3c1bdaff 5518+ inode_unlock_shared(h_src_inode);
86dc4139
AM
5519+ goto out;
5520+ }
5521+ h_src_attr->valid = 1;
e2f27e51
AM
5522+ if (!au_test_nfs(h_src_inode->i_sb)) {
5523+ err = au_cp_regular(cpg);
3c1bdaff 5524+ inode_unlock_shared(h_src_inode);
e2f27e51 5525+ } else {
3c1bdaff 5526+ inode_unlock_shared(h_src_inode);
e2f27e51
AM
5527+ err = au_cp_regular(cpg);
5528+ }
c2b27bf2 5529+ rerr = au_pin_hdir_relock(cpg->pin);
86dc4139
AM
5530+ if (!err && rerr)
5531+ err = rerr;
1facf9fc 5532+ }
38d290e6
JR
5533+ if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5534+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5527c038 5535+ h_dst_inode = d_inode(h_path.dentry);
38d290e6
JR
5536+ spin_lock(&h_dst_inode->i_lock);
5537+ h_dst_inode->i_state |= I_LINKABLE;
5538+ spin_unlock(&h_dst_inode->i_lock);
5539+ }
1facf9fc 5540+
4f0767ce 5541+out:
1facf9fc 5542+ return err;
5543+}
5544+
5545+static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5546+ struct inode *h_dir)
5547+{
5548+ int err, symlen;
5549+ mm_segment_t old_fs;
b752ccd1
AM
5550+ union {
5551+ char *k;
5552+ char __user *u;
5553+ } sym;
1facf9fc 5554+
5555+ err = -ENOMEM;
537831f9 5556+ sym.k = (void *)__get_free_page(GFP_NOFS);
b752ccd1 5557+ if (unlikely(!sym.k))
1facf9fc 5558+ goto out;
5559+
9dbd164d 5560+ /* unnecessary to support mmap_sem since symlink is not mmap-able */
1facf9fc 5561+ old_fs = get_fs();
5562+ set_fs(KERNEL_DS);
a2654f78 5563+ symlen = vfs_readlink(h_src, sym.u, PATH_MAX);
1facf9fc 5564+ err = symlen;
5565+ set_fs(old_fs);
5566+
5567+ if (symlen > 0) {
b752ccd1
AM
5568+ sym.k[symlen] = 0;
5569+ err = vfsub_symlink(h_dir, h_path, sym.k);
1facf9fc 5570+ }
1c60b727 5571+ free_page((unsigned long)sym.k);
1facf9fc 5572+
4f0767ce 5573+out:
1facf9fc 5574+ return err;
5575+}
5576+
8cdd5066
JR
5577+/*
5578+ * regardless 'acl' option, reset all ACL.
5579+ * All ACL will be copied up later from the original entry on the lower branch.
5580+ */
5581+static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5582+{
5583+ int err;
5584+ struct dentry *h_dentry;
5585+ struct inode *h_inode;
5586+
5587+ h_dentry = h_path->dentry;
5588+ h_inode = d_inode(h_dentry);
5589+ /* forget_all_cached_acls(h_inode)); */
5590+ err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5591+ AuTraceErr(err);
5592+ if (err == -EOPNOTSUPP)
5593+ err = 0;
5594+ if (!err)
5595+ err = vfsub_acl_chmod(h_inode, mode);
5596+
5597+ AuTraceErr(err);
5598+ return err;
5599+}
5600+
5601+static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5602+ struct inode *h_dir, struct path *h_path)
5603+{
5604+ int err;
5605+ struct inode *dir, *inode;
5606+
5607+ err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
5608+ AuTraceErr(err);
5609+ if (err == -EOPNOTSUPP)
5610+ err = 0;
5611+ if (unlikely(err))
5612+ goto out;
5613+
5614+ /*
5615+ * strange behaviour from the users view,
5616+ * particularry setattr case
5617+ */
5618+ dir = d_inode(dst_parent);
5afbbe0d 5619+ if (au_ibtop(dir) == cpg->bdst)
8cdd5066
JR
5620+ au_cpup_attr_nlink(dir, /*force*/1);
5621+ inode = d_inode(cpg->dentry);
5622+ au_cpup_attr_nlink(inode, /*force*/1);
5623+
5624+out:
5625+ return err;
5626+}
5627+
1facf9fc 5628+static noinline_for_stack
c2b27bf2 5629+int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
86dc4139 5630+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5631+{
5632+ int err;
5633+ umode_t mode;
5634+ unsigned int mnt_flags;
076b876e 5635+ unsigned char isdir, isreg, force;
c2b27bf2 5636+ const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5637+ struct au_dtime dt;
5638+ struct path h_path;
5639+ struct dentry *h_src, *h_dst, *h_parent;
8cdd5066 5640+ struct inode *h_inode, *h_dir;
1facf9fc 5641+ struct super_block *sb;
5642+
5643+ /* bsrc branch can be ro/rw. */
c2b27bf2 5644+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038
JR
5645+ h_inode = d_inode(h_src);
5646+ AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
1facf9fc 5647+
5648+ /* try stopping to be referenced while we are creating */
c2b27bf2
AM
5649+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5650+ if (au_ftest_cpup(cpg->flags, RENAME))
86dc4139
AM
5651+ AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5652+ AUFS_WH_PFX_LEN));
1facf9fc 5653+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5654+ h_dir = d_inode(h_parent);
1facf9fc 5655+ IMustLock(h_dir);
5656+ AuDebugOn(h_parent != h_dst->d_parent);
5657+
c2b27bf2
AM
5658+ sb = cpg->dentry->d_sb;
5659+ h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
1facf9fc 5660+ if (do_dt) {
5661+ h_path.dentry = h_parent;
5662+ au_dtime_store(&dt, dst_parent, &h_path);
5663+ }
5664+ h_path.dentry = h_dst;
5665+
076b876e 5666+ isreg = 0;
1facf9fc 5667+ isdir = 0;
5668+ mode = h_inode->i_mode;
5669+ switch (mode & S_IFMT) {
5670+ case S_IFREG:
076b876e 5671+ isreg = 1;
cd7a4cd9 5672+ err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
1facf9fc 5673+ if (!err)
c2b27bf2 5674+ err = au_do_cpup_regular(cpg, h_src_attr);
1facf9fc 5675+ break;
5676+ case S_IFDIR:
5677+ isdir = 1;
5678+ err = vfsub_mkdir(h_dir, &h_path, mode);
8cdd5066
JR
5679+ if (!err)
5680+ err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
1facf9fc 5681+ break;
5682+ case S_IFLNK:
5683+ err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5684+ break;
5685+ case S_IFCHR:
5686+ case S_IFBLK:
5687+ AuDebugOn(!capable(CAP_MKNOD));
5688+ /*FALLTHROUGH*/
5689+ case S_IFIFO:
5690+ case S_IFSOCK:
5691+ err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5692+ break;
5693+ default:
5694+ AuIOErr("Unknown inode type 0%o\n", mode);
5695+ err = -EIO;
5696+ }
8cdd5066
JR
5697+ if (!err)
5698+ err = au_reset_acl(h_dir, &h_path, mode);
1facf9fc 5699+
5700+ mnt_flags = au_mntflags(sb);
5701+ if (!au_opt_test(mnt_flags, UDBA_NONE)
5702+ && !isdir
5703+ && au_opt_test(mnt_flags, XINO)
38d290e6
JR
5704+ && (h_inode->i_nlink == 1
5705+ || (h_inode->i_state & I_LINKABLE))
1facf9fc 5706+ /* todo: unnecessary? */
5527c038 5707+ /* && d_inode(cpg->dentry)->i_nlink == 1 */
c2b27bf2
AM
5708+ && cpg->bdst < cpg->bsrc
5709+ && !au_ftest_cpup(cpg->flags, KEEPLINO))
5710+ au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
1facf9fc 5711+ /* ignore this error */
5712+
076b876e
AM
5713+ if (!err) {
5714+ force = 0;
5715+ if (isreg) {
5716+ force = !!cpg->len;
5717+ if (cpg->len == -1)
5718+ force = !!i_size_read(h_inode);
5719+ }
5720+ au_fhsm_wrote(sb, cpg->bdst, force);
5721+ }
5722+
1facf9fc 5723+ if (do_dt)
5724+ au_dtime_revert(&dt);
5725+ return err;
5726+}
5727+
392086de 5728+static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
86dc4139
AM
5729+{
5730+ int err;
392086de 5731+ struct dentry *dentry, *h_dentry, *h_parent, *parent;
86dc4139 5732+ struct inode *h_dir;
392086de 5733+ aufs_bindex_t bdst;
86dc4139 5734+
392086de
AM
5735+ dentry = cpg->dentry;
5736+ bdst = cpg->bdst;
5737+ h_dentry = au_h_dptr(dentry, bdst);
5738+ if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5739+ dget(h_dentry);
5740+ au_set_h_dptr(dentry, bdst, NULL);
5741+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
5742+ if (!err)
5743+ h_path->dentry = dget(au_h_dptr(dentry, bdst));
86dc4139 5744+ au_set_h_dptr(dentry, bdst, h_dentry);
392086de
AM
5745+ } else {
5746+ err = 0;
5747+ parent = dget_parent(dentry);
5748+ h_parent = au_h_dptr(parent, bdst);
5749+ dput(parent);
5750+ h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5751+ if (IS_ERR(h_path->dentry))
5752+ err = PTR_ERR(h_path->dentry);
86dc4139 5753+ }
392086de
AM
5754+ if (unlikely(err))
5755+ goto out;
86dc4139 5756+
86dc4139 5757+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 5758+ h_dir = d_inode(h_parent);
86dc4139 5759+ IMustLock(h_dir);
523b37e3
AM
5760+ AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5761+ /* no delegation since it is just created */
f2c43d5f
AM
5762+ err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5763+ /*flags*/0);
86dc4139
AM
5764+ dput(h_path->dentry);
5765+
5766+out:
5767+ return err;
5768+}
5769+
1facf9fc 5770+/*
5771+ * copyup the @dentry from @bsrc to @bdst.
5772+ * the caller must set the both of lower dentries.
5773+ * @len is for truncating when it is -1 copyup the entire file.
5774+ * in link/rename cases, @dst_parent may be different from the real one.
c2b27bf2 5775+ * basic->bsrc can be larger than basic->bdst.
f2c43d5f
AM
5776+ * aufs doesn't touch the credential so
5777+ * security_inode_copy_up{,_xattr}() are unnecrssary.
1facf9fc 5778+ */
c2b27bf2 5779+static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5780+{
5781+ int err, rerr;
5afbbe0d 5782+ aufs_bindex_t old_ibtop;
1facf9fc 5783+ unsigned char isdir, plink;
1facf9fc 5784+ struct dentry *h_src, *h_dst, *h_parent;
5527c038 5785+ struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
1facf9fc 5786+ struct super_block *sb;
86dc4139 5787+ struct au_branch *br;
c2b27bf2
AM
5788+ /* to reuduce stack size */
5789+ struct {
5790+ struct au_dtime dt;
5791+ struct path h_path;
5792+ struct au_cpup_reg_attr h_src_attr;
5793+ } *a;
1facf9fc 5794+
c2b27bf2
AM
5795+ err = -ENOMEM;
5796+ a = kmalloc(sizeof(*a), GFP_NOFS);
5797+ if (unlikely(!a))
5798+ goto out;
5799+ a->h_src_attr.valid = 0;
1facf9fc 5800+
c2b27bf2
AM
5801+ sb = cpg->dentry->d_sb;
5802+ br = au_sbr(sb, cpg->bdst);
5803+ a->h_path.mnt = au_br_mnt(br);
5804+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
1facf9fc 5805+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5806+ h_dir = d_inode(h_parent);
1facf9fc 5807+ IMustLock(h_dir);
5808+
c2b27bf2 5809+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5810+ inode = d_inode(cpg->dentry);
1facf9fc 5811+
5812+ if (!dst_parent)
c2b27bf2 5813+ dst_parent = dget_parent(cpg->dentry);
1facf9fc 5814+ else
5815+ dget(dst_parent);
5816+
5817+ plink = !!au_opt_test(au_mntflags(sb), PLINK);
c2b27bf2 5818+ dst_inode = au_h_iptr(inode, cpg->bdst);
1facf9fc 5819+ if (dst_inode) {
5820+ if (unlikely(!plink)) {
5821+ err = -EIO;
027c5e7a
AM
5822+ AuIOErr("hi%lu(i%lu) exists on b%d "
5823+ "but plink is disabled\n",
c2b27bf2
AM
5824+ dst_inode->i_ino, inode->i_ino, cpg->bdst);
5825+ goto out_parent;
1facf9fc 5826+ }
5827+
5828+ if (dst_inode->i_nlink) {
c2b27bf2 5829+ const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5830+
c2b27bf2 5831+ h_src = au_plink_lkup(inode, cpg->bdst);
1facf9fc 5832+ err = PTR_ERR(h_src);
5833+ if (IS_ERR(h_src))
c2b27bf2 5834+ goto out_parent;
5527c038 5835+ if (unlikely(d_is_negative(h_src))) {
1facf9fc 5836+ err = -EIO;
79b8bda9 5837+ AuIOErr("i%lu exists on b%d "
027c5e7a 5838+ "but not pseudo-linked\n",
79b8bda9 5839+ inode->i_ino, cpg->bdst);
1facf9fc 5840+ dput(h_src);
c2b27bf2 5841+ goto out_parent;
1facf9fc 5842+ }
5843+
5844+ if (do_dt) {
c2b27bf2
AM
5845+ a->h_path.dentry = h_parent;
5846+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
1facf9fc 5847+ }
86dc4139 5848+
c2b27bf2 5849+ a->h_path.dentry = h_dst;
523b37e3
AM
5850+ delegated = NULL;
5851+ err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
c2b27bf2 5852+ if (!err && au_ftest_cpup(cpg->flags, RENAME))
392086de 5853+ err = au_do_ren_after_cpup(cpg, &a->h_path);
1facf9fc 5854+ if (do_dt)
c2b27bf2 5855+ au_dtime_revert(&a->dt);
523b37e3
AM
5856+ if (unlikely(err == -EWOULDBLOCK)) {
5857+ pr_warn("cannot retry for NFSv4 delegation"
5858+ " for an internal link\n");
5859+ iput(delegated);
5860+ }
1facf9fc 5861+ dput(h_src);
c2b27bf2 5862+ goto out_parent;
1facf9fc 5863+ } else
5864+ /* todo: cpup_wh_file? */
5865+ /* udba work */
4a4d8108 5866+ au_update_ibrange(inode, /*do_put_zero*/1);
1facf9fc 5867+ }
5868+
86dc4139 5869+ isdir = S_ISDIR(inode->i_mode);
5afbbe0d 5870+ old_ibtop = au_ibtop(inode);
c2b27bf2 5871+ err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
1facf9fc 5872+ if (unlikely(err))
86dc4139 5873+ goto out_rev;
5527c038 5874+ dst_inode = d_inode(h_dst);
febd17d6 5875+ inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
86dc4139 5876+ /* todo: necessary? */
c2b27bf2 5877+ /* au_pin_hdir_unlock(cpg->pin); */
1facf9fc 5878+
c2b27bf2 5879+ err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
86dc4139
AM
5880+ if (unlikely(err)) {
5881+ /* todo: necessary? */
c2b27bf2 5882+ /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
febd17d6 5883+ inode_unlock(dst_inode);
86dc4139
AM
5884+ goto out_rev;
5885+ }
5886+
5afbbe0d 5887+ if (cpg->bdst < old_ibtop) {
86dc4139 5888+ if (S_ISREG(inode->i_mode)) {
c2b27bf2 5889+ err = au_dy_iaop(inode, cpg->bdst, dst_inode);
86dc4139 5890+ if (unlikely(err)) {
c2b27bf2
AM
5891+ /* ignore an error */
5892+ /* au_pin_hdir_relock(cpg->pin); */
febd17d6 5893+ inode_unlock(dst_inode);
86dc4139 5894+ goto out_rev;
4a4d8108 5895+ }
4a4d8108 5896+ }
5afbbe0d 5897+ au_set_ibtop(inode, cpg->bdst);
c2b27bf2 5898+ } else
5afbbe0d 5899+ au_set_ibbot(inode, cpg->bdst);
c2b27bf2 5900+ au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
86dc4139
AM
5901+ au_hi_flags(inode, isdir));
5902+
5903+ /* todo: necessary? */
c2b27bf2 5904+ /* err = au_pin_hdir_relock(cpg->pin); */
febd17d6 5905+ inode_unlock(dst_inode);
86dc4139
AM
5906+ if (unlikely(err))
5907+ goto out_rev;
5908+
5527c038 5909+ src_inode = d_inode(h_src);
86dc4139 5910+ if (!isdir
5527c038
JR
5911+ && (src_inode->i_nlink > 1
5912+ || src_inode->i_state & I_LINKABLE)
86dc4139 5913+ && plink)
c2b27bf2 5914+ au_plink_append(inode, cpg->bdst, h_dst);
86dc4139 5915+
c2b27bf2
AM
5916+ if (au_ftest_cpup(cpg->flags, RENAME)) {
5917+ a->h_path.dentry = h_dst;
392086de 5918+ err = au_do_ren_after_cpup(cpg, &a->h_path);
86dc4139
AM
5919+ }
5920+ if (!err)
c2b27bf2 5921+ goto out_parent; /* success */
1facf9fc 5922+
5923+ /* revert */
4a4d8108 5924+out_rev:
c2b27bf2
AM
5925+ a->h_path.dentry = h_parent;
5926+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
5927+ a->h_path.dentry = h_dst;
86dc4139 5928+ rerr = 0;
5527c038 5929+ if (d_is_positive(h_dst)) {
523b37e3
AM
5930+ if (!isdir) {
5931+ /* no delegation since it is just created */
5932+ rerr = vfsub_unlink(h_dir, &a->h_path,
5933+ /*delegated*/NULL, /*force*/0);
5934+ } else
c2b27bf2 5935+ rerr = vfsub_rmdir(h_dir, &a->h_path);
86dc4139 5936+ }
c2b27bf2 5937+ au_dtime_revert(&a->dt);
1facf9fc 5938+ if (rerr) {
5939+ AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5940+ err = -EIO;
5941+ }
c2b27bf2 5942+out_parent:
1facf9fc 5943+ dput(dst_parent);
1c60b727 5944+ kfree(a);
c2b27bf2 5945+out:
1facf9fc 5946+ return err;
5947+}
5948+
7e9cd9fe 5949+#if 0 /* reserved */
1facf9fc 5950+struct au_cpup_single_args {
5951+ int *errp;
c2b27bf2 5952+ struct au_cp_generic *cpg;
1facf9fc 5953+ struct dentry *dst_parent;
5954+};
5955+
5956+static void au_call_cpup_single(void *args)
5957+{
5958+ struct au_cpup_single_args *a = args;
86dc4139 5959+
c2b27bf2
AM
5960+ au_pin_hdir_acquire_nest(a->cpg->pin);
5961+ *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5962+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 5963+}
c2b27bf2 5964+#endif
1facf9fc 5965+
53392da6
AM
5966+/*
5967+ * prevent SIGXFSZ in copy-up.
5968+ * testing CAP_MKNOD is for generic fs,
5969+ * but CAP_FSETID is for xfs only, currently.
5970+ */
86dc4139 5971+static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
53392da6
AM
5972+{
5973+ int do_sio;
86dc4139
AM
5974+ struct super_block *sb;
5975+ struct inode *h_dir;
53392da6
AM
5976+
5977+ do_sio = 0;
86dc4139 5978+ sb = au_pinned_parent(pin)->d_sb;
53392da6
AM
5979+ if (!au_wkq_test()
5980+ && (!au_sbi(sb)->si_plink_maint_pid
5981+ || au_plink_maint(sb, AuLock_NOPLM))) {
5982+ switch (mode & S_IFMT) {
5983+ case S_IFREG:
5984+ /* no condition about RLIMIT_FSIZE and the file size */
5985+ do_sio = 1;
5986+ break;
5987+ case S_IFCHR:
5988+ case S_IFBLK:
5989+ do_sio = !capable(CAP_MKNOD);
5990+ break;
5991+ }
5992+ if (!do_sio)
5993+ do_sio = ((mode & (S_ISUID | S_ISGID))
5994+ && !capable(CAP_FSETID));
86dc4139
AM
5995+ /* this workaround may be removed in the future */
5996+ if (!do_sio) {
5997+ h_dir = au_pinned_h_dir(pin);
5998+ do_sio = h_dir->i_mode & S_ISVTX;
5999+ }
53392da6
AM
6000+ }
6001+
6002+ return do_sio;
6003+}
6004+
7e9cd9fe 6005+#if 0 /* reserved */
c2b27bf2 6006+int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 6007+{
6008+ int err, wkq_err;
1facf9fc 6009+ struct dentry *h_dentry;
6010+
c2b27bf2 6011+ h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 6012+ if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
c2b27bf2 6013+ err = au_cpup_single(cpg, dst_parent);
1facf9fc 6014+ else {
6015+ struct au_cpup_single_args args = {
6016+ .errp = &err,
c2b27bf2
AM
6017+ .cpg = cpg,
6018+ .dst_parent = dst_parent
1facf9fc 6019+ };
6020+ wkq_err = au_wkq_wait(au_call_cpup_single, &args);
6021+ if (unlikely(wkq_err))
6022+ err = wkq_err;
6023+ }
6024+
6025+ return err;
6026+}
c2b27bf2 6027+#endif
1facf9fc 6028+
6029+/*
6030+ * copyup the @dentry from the first active lower branch to @bdst,
6031+ * using au_cpup_single().
6032+ */
c2b27bf2 6033+static int au_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 6034+{
6035+ int err;
c2b27bf2
AM
6036+ unsigned int flags_orig;
6037+ struct dentry *dentry;
6038+
6039+ AuDebugOn(cpg->bsrc < 0);
1facf9fc 6040+
c2b27bf2 6041+ dentry = cpg->dentry;
86dc4139 6042+ DiMustWriteLock(dentry);
1facf9fc 6043+
c2b27bf2 6044+ err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
1facf9fc 6045+ if (!err) {
c2b27bf2
AM
6046+ flags_orig = cpg->flags;
6047+ au_fset_cpup(cpg->flags, RENAME);
6048+ err = au_cpup_single(cpg, NULL);
6049+ cpg->flags = flags_orig;
1facf9fc 6050+ if (!err)
6051+ return 0; /* success */
6052+
6053+ /* revert */
c2b27bf2 6054+ au_set_h_dptr(dentry, cpg->bdst, NULL);
5afbbe0d 6055+ au_set_dbtop(dentry, cpg->bsrc);
1facf9fc 6056+ }
6057+
6058+ return err;
6059+}
6060+
6061+struct au_cpup_simple_args {
6062+ int *errp;
c2b27bf2 6063+ struct au_cp_generic *cpg;
1facf9fc 6064+};
6065+
6066+static void au_call_cpup_simple(void *args)
6067+{
6068+ struct au_cpup_simple_args *a = args;
86dc4139 6069+
c2b27bf2
AM
6070+ au_pin_hdir_acquire_nest(a->cpg->pin);
6071+ *a->errp = au_cpup_simple(a->cpg);
6072+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 6073+}
6074+
c2b27bf2 6075+static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 6076+{
6077+ int err, wkq_err;
c2b27bf2
AM
6078+ struct dentry *dentry, *parent;
6079+ struct file *h_file;
1facf9fc 6080+ struct inode *h_dir;
6081+
c2b27bf2
AM
6082+ dentry = cpg->dentry;
6083+ h_file = NULL;
6084+ if (au_ftest_cpup(cpg->flags, HOPEN)) {
6085+ AuDebugOn(cpg->bsrc < 0);
392086de 6086+ h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
c2b27bf2
AM
6087+ err = PTR_ERR(h_file);
6088+ if (IS_ERR(h_file))
6089+ goto out;
6090+ }
6091+
1facf9fc 6092+ parent = dget_parent(dentry);
5527c038 6093+ h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
53392da6 6094+ if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
5527c038 6095+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 6096+ err = au_cpup_simple(cpg);
1facf9fc 6097+ else {
6098+ struct au_cpup_simple_args args = {
6099+ .errp = &err,
c2b27bf2 6100+ .cpg = cpg
1facf9fc 6101+ };
6102+ wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
6103+ if (unlikely(wkq_err))
6104+ err = wkq_err;
6105+ }
6106+
6107+ dput(parent);
c2b27bf2
AM
6108+ if (h_file)
6109+ au_h_open_post(dentry, cpg->bsrc, h_file);
6110+
6111+out:
1facf9fc 6112+ return err;
6113+}
6114+
c2b27bf2 6115+int au_sio_cpup_simple(struct au_cp_generic *cpg)
367653fa 6116+{
5afbbe0d 6117+ aufs_bindex_t bsrc, bbot;
c2b27bf2 6118+ struct dentry *dentry, *h_dentry;
367653fa 6119+
c2b27bf2
AM
6120+ if (cpg->bsrc < 0) {
6121+ dentry = cpg->dentry;
5afbbe0d
AM
6122+ bbot = au_dbbot(dentry);
6123+ for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
c2b27bf2
AM
6124+ h_dentry = au_h_dptr(dentry, bsrc);
6125+ if (h_dentry) {
5527c038 6126+ AuDebugOn(d_is_negative(h_dentry));
c2b27bf2
AM
6127+ break;
6128+ }
6129+ }
5afbbe0d 6130+ AuDebugOn(bsrc > bbot);
c2b27bf2 6131+ cpg->bsrc = bsrc;
367653fa 6132+ }
c2b27bf2
AM
6133+ AuDebugOn(cpg->bsrc <= cpg->bdst);
6134+ return au_do_sio_cpup_simple(cpg);
6135+}
367653fa 6136+
c2b27bf2
AM
6137+int au_sio_cpdown_simple(struct au_cp_generic *cpg)
6138+{
6139+ AuDebugOn(cpg->bdst <= cpg->bsrc);
6140+ return au_do_sio_cpup_simple(cpg);
367653fa
AM
6141+}
6142+
1facf9fc 6143+/* ---------------------------------------------------------------------- */
6144+
6145+/*
6146+ * copyup the deleted file for writing.
6147+ */
c2b27bf2
AM
6148+static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
6149+ struct file *file)
1facf9fc 6150+{
6151+ int err;
c2b27bf2
AM
6152+ unsigned int flags_orig;
6153+ aufs_bindex_t bsrc_orig;
c2b27bf2 6154+ struct au_dinfo *dinfo;
5afbbe0d
AM
6155+ struct {
6156+ struct au_hdentry *hd;
6157+ struct dentry *h_dentry;
6158+ } hdst, hsrc;
1facf9fc 6159+
c2b27bf2 6160+ dinfo = au_di(cpg->dentry);
1308ab2a 6161+ AuRwMustWriteLock(&dinfo->di_rwsem);
6162+
c2b27bf2 6163+ bsrc_orig = cpg->bsrc;
5afbbe0d
AM
6164+ cpg->bsrc = dinfo->di_btop;
6165+ hdst.hd = au_hdentry(dinfo, cpg->bdst);
6166+ hdst.h_dentry = hdst.hd->hd_dentry;
6167+ hdst.hd->hd_dentry = wh_dentry;
6168+ dinfo->di_btop = cpg->bdst;
6169+
6170+ hsrc.h_dentry = NULL;
027c5e7a 6171+ if (file) {
5afbbe0d
AM
6172+ hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
6173+ hsrc.h_dentry = hsrc.hd->hd_dentry;
6174+ hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
027c5e7a 6175+ }
c2b27bf2
AM
6176+ flags_orig = cpg->flags;
6177+ cpg->flags = !AuCpup_DTIME;
6178+ err = au_cpup_single(cpg, /*h_parent*/NULL);
6179+ cpg->flags = flags_orig;
027c5e7a
AM
6180+ if (file) {
6181+ if (!err)
6182+ err = au_reopen_nondir(file);
5afbbe0d 6183+ hsrc.hd->hd_dentry = hsrc.h_dentry;
1facf9fc 6184+ }
5afbbe0d
AM
6185+ hdst.hd->hd_dentry = hdst.h_dentry;
6186+ dinfo->di_btop = cpg->bsrc;
c2b27bf2 6187+ cpg->bsrc = bsrc_orig;
1facf9fc 6188+
6189+ return err;
6190+}
6191+
c2b27bf2 6192+static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 6193+{
6194+ int err;
c2b27bf2 6195+ aufs_bindex_t bdst;
1facf9fc 6196+ struct au_dtime dt;
c2b27bf2 6197+ struct dentry *dentry, *parent, *h_parent, *wh_dentry;
1facf9fc 6198+ struct au_branch *br;
6199+ struct path h_path;
6200+
c2b27bf2
AM
6201+ dentry = cpg->dentry;
6202+ bdst = cpg->bdst;
1facf9fc 6203+ br = au_sbr(dentry->d_sb, bdst);
6204+ parent = dget_parent(dentry);
6205+ h_parent = au_h_dptr(parent, bdst);
6206+ wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
6207+ err = PTR_ERR(wh_dentry);
6208+ if (IS_ERR(wh_dentry))
6209+ goto out;
6210+
6211+ h_path.dentry = h_parent;
86dc4139 6212+ h_path.mnt = au_br_mnt(br);
1facf9fc 6213+ au_dtime_store(&dt, parent, &h_path);
c2b27bf2 6214+ err = au_do_cpup_wh(cpg, wh_dentry, file);
1facf9fc 6215+ if (unlikely(err))
6216+ goto out_wh;
6217+
6218+ dget(wh_dentry);
6219+ h_path.dentry = wh_dentry;
2000de60 6220+ if (!d_is_dir(wh_dentry)) {
523b37e3 6221+ /* no delegation since it is just created */
5527c038 6222+ err = vfsub_unlink(d_inode(h_parent), &h_path,
523b37e3
AM
6223+ /*delegated*/NULL, /*force*/0);
6224+ } else
5527c038 6225+ err = vfsub_rmdir(d_inode(h_parent), &h_path);
1facf9fc 6226+ if (unlikely(err)) {
523b37e3
AM
6227+ AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6228+ wh_dentry, err);
1facf9fc 6229+ err = -EIO;
6230+ }
6231+ au_dtime_revert(&dt);
5527c038 6232+ au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
1facf9fc 6233+
4f0767ce 6234+out_wh:
1facf9fc 6235+ dput(wh_dentry);
4f0767ce 6236+out:
1facf9fc 6237+ dput(parent);
6238+ return err;
6239+}
6240+
6241+struct au_cpup_wh_args {
6242+ int *errp;
c2b27bf2 6243+ struct au_cp_generic *cpg;
1facf9fc 6244+ struct file *file;
6245+};
6246+
6247+static void au_call_cpup_wh(void *args)
6248+{
6249+ struct au_cpup_wh_args *a = args;
86dc4139 6250+
c2b27bf2
AM
6251+ au_pin_hdir_acquire_nest(a->cpg->pin);
6252+ *a->errp = au_cpup_wh(a->cpg, a->file);
6253+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 6254+}
6255+
c2b27bf2 6256+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 6257+{
6258+ int err, wkq_err;
c2b27bf2 6259+ aufs_bindex_t bdst;
c1595e42 6260+ struct dentry *dentry, *parent, *h_orph, *h_parent;
86dc4139 6261+ struct inode *dir, *h_dir, *h_tmpdir;
1facf9fc 6262+ struct au_wbr *wbr;
c2b27bf2 6263+ struct au_pin wh_pin, *pin_orig;
1facf9fc 6264+
c2b27bf2
AM
6265+ dentry = cpg->dentry;
6266+ bdst = cpg->bdst;
1facf9fc 6267+ parent = dget_parent(dentry);
5527c038 6268+ dir = d_inode(parent);
1facf9fc 6269+ h_orph = NULL;
6270+ h_parent = NULL;
6271+ h_dir = au_igrab(au_h_iptr(dir, bdst));
6272+ h_tmpdir = h_dir;
c2b27bf2 6273+ pin_orig = NULL;
1facf9fc 6274+ if (!h_dir->i_nlink) {
6275+ wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6276+ h_orph = wbr->wbr_orph;
6277+
6278+ h_parent = dget(au_h_dptr(parent, bdst));
1facf9fc 6279+ au_set_h_dptr(parent, bdst, dget(h_orph));
5527c038 6280+ h_tmpdir = d_inode(h_orph);
1facf9fc 6281+ au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6282+
febd17d6 6283+ inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
4a4d8108 6284+ /* todo: au_h_open_pre()? */
86dc4139 6285+
c2b27bf2 6286+ pin_orig = cpg->pin;
86dc4139 6287+ au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
c2b27bf2
AM
6288+ AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6289+ cpg->pin = &wh_pin;
1facf9fc 6290+ }
6291+
53392da6 6292+ if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
5527c038 6293+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 6294+ err = au_cpup_wh(cpg, file);
1facf9fc 6295+ else {
6296+ struct au_cpup_wh_args args = {
6297+ .errp = &err,
c2b27bf2
AM
6298+ .cpg = cpg,
6299+ .file = file
1facf9fc 6300+ };
6301+ wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6302+ if (unlikely(wkq_err))
6303+ err = wkq_err;
6304+ }
6305+
6306+ if (h_orph) {
febd17d6 6307+ inode_unlock(h_tmpdir);
4a4d8108 6308+ /* todo: au_h_open_post()? */
1facf9fc 6309+ au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
1facf9fc 6310+ au_set_h_dptr(parent, bdst, h_parent);
c2b27bf2
AM
6311+ AuDebugOn(!pin_orig);
6312+ cpg->pin = pin_orig;
1facf9fc 6313+ }
6314+ iput(h_dir);
6315+ dput(parent);
6316+
6317+ return err;
6318+}
6319+
6320+/* ---------------------------------------------------------------------- */
6321+
6322+/*
6323+ * generic routine for both of copy-up and copy-down.
6324+ */
6325+/* cf. revalidate function in file.c */
6326+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6327+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6328+ struct au_pin *pin,
1facf9fc 6329+ struct dentry *h_parent, void *arg),
6330+ void *arg)
6331+{
6332+ int err;
6333+ struct au_pin pin;
5527c038 6334+ struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
1facf9fc 6335+
6336+ err = 0;
6337+ parent = dget_parent(dentry);
6338+ if (IS_ROOT(parent))
6339+ goto out;
6340+
6341+ au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6342+ au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6343+
6344+ /* do not use au_dpage */
6345+ real_parent = parent;
6346+ while (1) {
6347+ dput(parent);
6348+ parent = dget_parent(dentry);
6349+ h_parent = au_h_dptr(parent, bdst);
6350+ if (h_parent)
6351+ goto out; /* success */
6352+
6353+ /* find top dir which is necessary to cpup */
6354+ do {
6355+ d = parent;
6356+ dput(parent);
6357+ parent = dget_parent(d);
6358+ di_read_lock_parent3(parent, !AuLock_IR);
6359+ h_parent = au_h_dptr(parent, bdst);
6360+ di_read_unlock(parent, !AuLock_IR);
6361+ } while (!h_parent);
6362+
6363+ if (d != real_parent)
6364+ di_write_lock_child3(d);
6365+
6366+ /* somebody else might create while we were sleeping */
5527c038
JR
6367+ h_dentry = au_h_dptr(d, bdst);
6368+ if (!h_dentry || d_is_negative(h_dentry)) {
6369+ if (h_dentry)
5afbbe0d 6370+ au_update_dbtop(d);
1facf9fc 6371+
6372+ au_pin_set_dentry(&pin, d);
6373+ err = au_do_pin(&pin);
6374+ if (!err) {
86dc4139 6375+ err = cp(d, bdst, &pin, h_parent, arg);
1facf9fc 6376+ au_unpin(&pin);
6377+ }
6378+ }
6379+
6380+ if (d != real_parent)
6381+ di_write_unlock(d);
6382+ if (unlikely(err))
6383+ break;
6384+ }
6385+
4f0767ce 6386+out:
1facf9fc 6387+ dput(parent);
6388+ return err;
6389+}
6390+
6391+static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6392+ struct au_pin *pin,
2000de60 6393+ struct dentry *h_parent __maybe_unused,
1facf9fc 6394+ void *arg __maybe_unused)
6395+{
c2b27bf2
AM
6396+ struct au_cp_generic cpg = {
6397+ .dentry = dentry,
6398+ .bdst = bdst,
6399+ .bsrc = -1,
6400+ .len = 0,
6401+ .pin = pin,
6402+ .flags = AuCpup_DTIME
6403+ };
6404+ return au_sio_cpup_simple(&cpg);
1facf9fc 6405+}
6406+
6407+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6408+{
6409+ return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6410+}
6411+
6412+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6413+{
6414+ int err;
6415+ struct dentry *parent;
6416+ struct inode *dir;
6417+
6418+ parent = dget_parent(dentry);
5527c038 6419+ dir = d_inode(parent);
1facf9fc 6420+ err = 0;
6421+ if (au_h_iptr(dir, bdst))
6422+ goto out;
6423+
6424+ di_read_unlock(parent, AuLock_IR);
6425+ di_write_lock_parent(parent);
6426+ /* someone else might change our inode while we were sleeping */
6427+ if (!au_h_iptr(dir, bdst))
6428+ err = au_cpup_dirs(dentry, bdst);
6429+ di_downgrade_lock(parent, AuLock_IR);
6430+
4f0767ce 6431+out:
1facf9fc 6432+ dput(parent);
6433+ return err;
6434+}
7f207e10
AM
6435diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6436--- /usr/share/empty/fs/aufs/cpup.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 6437+++ linux/fs/aufs/cpup.h 2018-08-12 23:43:05.453457863 +0200
062440b3
AM
6438@@ -0,0 +1,100 @@
6439+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 6440+/*
b00004a5 6441+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 6442+ *
6443+ * This program, aufs is free software; you can redistribute it and/or modify
6444+ * it under the terms of the GNU General Public License as published by
6445+ * the Free Software Foundation; either version 2 of the License, or
6446+ * (at your option) any later version.
dece6358
AM
6447+ *
6448+ * This program is distributed in the hope that it will be useful,
6449+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6450+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6451+ * GNU General Public License for more details.
6452+ *
6453+ * You should have received a copy of the GNU General Public License
523b37e3 6454+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6455+ */
6456+
6457+/*
6458+ * copy-up/down functions
6459+ */
6460+
6461+#ifndef __AUFS_CPUP_H__
6462+#define __AUFS_CPUP_H__
6463+
6464+#ifdef __KERNEL__
6465+
dece6358 6466+#include <linux/path.h>
1facf9fc 6467+
dece6358
AM
6468+struct inode;
6469+struct file;
86dc4139 6470+struct au_pin;
dece6358 6471+
86dc4139 6472+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
1facf9fc 6473+void au_cpup_attr_timesizes(struct inode *inode);
6474+void au_cpup_attr_nlink(struct inode *inode, int force);
6475+void au_cpup_attr_changeable(struct inode *inode);
6476+void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6477+void au_cpup_attr_all(struct inode *inode, int force);
6478+
6479+/* ---------------------------------------------------------------------- */
6480+
c2b27bf2
AM
6481+struct au_cp_generic {
6482+ struct dentry *dentry;
6483+ aufs_bindex_t bdst, bsrc;
6484+ loff_t len;
6485+ struct au_pin *pin;
6486+ unsigned int flags;
6487+};
6488+
1facf9fc 6489+/* cpup flags */
392086de
AM
6490+#define AuCpup_DTIME 1 /* do dtime_store/revert */
6491+#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
6492+ for link(2) */
6493+#define AuCpup_RENAME (1 << 2) /* rename after cpup */
6494+#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
6495+ cpup */
6496+#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
6497+ existing entry */
6498+#define AuCpup_RWDST (1 << 5) /* force write target even if
6499+ the branch is marked as RO */
c2b27bf2 6500+
8b6a4947
AM
6501+#ifndef CONFIG_AUFS_BR_HFSPLUS
6502+#undef AuCpup_HOPEN
6503+#define AuCpup_HOPEN 0
6504+#endif
6505+
1facf9fc 6506+#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
7f207e10
AM
6507+#define au_fset_cpup(flags, name) \
6508+ do { (flags) |= AuCpup_##name; } while (0)
6509+#define au_fclr_cpup(flags, name) \
6510+ do { (flags) &= ~AuCpup_##name; } while (0)
1facf9fc 6511+
6512+int au_copy_file(struct file *dst, struct file *src, loff_t len);
c2b27bf2
AM
6513+int au_sio_cpup_simple(struct au_cp_generic *cpg);
6514+int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6515+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
1facf9fc 6516+
6517+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6518+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6519+ struct au_pin *pin,
1facf9fc 6520+ struct dentry *h_parent, void *arg),
6521+ void *arg);
6522+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6523+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6524+
6525+/* ---------------------------------------------------------------------- */
6526+
6527+/* keep timestamps when copyup */
6528+struct au_dtime {
6529+ struct dentry *dt_dentry;
6530+ struct path dt_h_path;
cd7a4cd9 6531+ struct timespec64 dt_atime, dt_mtime;
1facf9fc 6532+};
6533+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6534+ struct path *h_path);
6535+void au_dtime_revert(struct au_dtime *dt);
6536+
6537+#endif /* __KERNEL__ */
6538+#endif /* __AUFS_CPUP_H__ */
7f207e10
AM
6539diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6540--- /usr/share/empty/fs/aufs/dbgaufs.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 6541+++ linux/fs/aufs/dbgaufs.c 2018-08-12 23:43:05.453457863 +0200
062440b3 6542@@ -0,0 +1,478 @@
cd7a4cd9 6543+// SPDX-License-Identifier: GPL-2.0
1facf9fc 6544+/*
b00004a5 6545+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 6546+ *
6547+ * This program, aufs is free software; you can redistribute it and/or modify
6548+ * it under the terms of the GNU General Public License as published by
6549+ * the Free Software Foundation; either version 2 of the License, or
6550+ * (at your option) any later version.
dece6358
AM
6551+ *
6552+ * This program is distributed in the hope that it will be useful,
6553+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6554+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6555+ * GNU General Public License for more details.
6556+ *
6557+ * You should have received a copy of the GNU General Public License
523b37e3 6558+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6559+ */
6560+
6561+/*
6562+ * debugfs interface
6563+ */
6564+
6565+#include <linux/debugfs.h>
6566+#include "aufs.h"
6567+
6568+#ifndef CONFIG_SYSFS
6569+#error DEBUG_FS depends upon SYSFS
6570+#endif
6571+
6572+static struct dentry *dbgaufs;
cd7a4cd9 6573+static const mode_t dbgaufs_mode = 0444;
1facf9fc 6574+
6575+/* 20 is max digits length of ulong 64 */
6576+struct dbgaufs_arg {
6577+ int n;
6578+ char a[20 * 4];
6579+};
6580+
6581+/*
6582+ * common function for all XINO files
6583+ */
6584+static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6585+ struct file *file)
6586+{
1c60b727 6587+ kfree(file->private_data);
1facf9fc 6588+ return 0;
6589+}
6590+
062440b3
AM
6591+static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6592+ int cnt)
1facf9fc 6593+{
6594+ int err;
6595+ struct kstat st;
6596+ struct dbgaufs_arg *p;
6597+
6598+ err = -ENOMEM;
6599+ p = kmalloc(sizeof(*p), GFP_NOFS);
6600+ if (unlikely(!p))
6601+ goto out;
6602+
6603+ err = 0;
6604+ p->n = 0;
6605+ file->private_data = p;
6606+ if (!xf)
6607+ goto out;
6608+
521ced18 6609+ err = vfsub_getattr(&xf->f_path, &st);
1facf9fc 6610+ if (!err) {
6611+ if (do_fcnt)
6612+ p->n = snprintf
062440b3
AM
6613+ (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6614+ cnt, st.blocks, st.blksize,
1facf9fc 6615+ (long long)st.size);
6616+ else
521ced18 6617+ p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
1facf9fc 6618+ st.blocks, st.blksize,
6619+ (long long)st.size);
6620+ AuDebugOn(p->n >= sizeof(p->a));
6621+ } else {
6622+ p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6623+ err = 0;
6624+ }
6625+
4f0767ce 6626+out:
1facf9fc 6627+ return err;
6628+
6629+}
6630+
6631+static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6632+ size_t count, loff_t *ppos)
6633+{
6634+ struct dbgaufs_arg *p;
6635+
6636+ p = file->private_data;
6637+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6638+}
6639+
6640+/* ---------------------------------------------------------------------- */
6641+
86dc4139
AM
6642+struct dbgaufs_plink_arg {
6643+ int n;
6644+ char a[];
6645+};
6646+
6647+static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6648+ struct file *file)
6649+{
1c60b727 6650+ free_page((unsigned long)file->private_data);
86dc4139
AM
6651+ return 0;
6652+}
6653+
6654+static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6655+{
6656+ int err, i, limit;
6657+ unsigned long n, sum;
6658+ struct dbgaufs_plink_arg *p;
6659+ struct au_sbinfo *sbinfo;
6660+ struct super_block *sb;
8b6a4947 6661+ struct hlist_bl_head *hbl;
86dc4139
AM
6662+
6663+ err = -ENOMEM;
6664+ p = (void *)get_zeroed_page(GFP_NOFS);
6665+ if (unlikely(!p))
6666+ goto out;
6667+
6668+ err = -EFBIG;
6669+ sbinfo = inode->i_private;
6670+ sb = sbinfo->si_sb;
6671+ si_noflush_read_lock(sb);
6672+ if (au_opt_test(au_mntflags(sb), PLINK)) {
6673+ limit = PAGE_SIZE - sizeof(p->n);
6674+
6675+ /* the number of buckets */
6676+ n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6677+ p->n += n;
6678+ limit -= n;
6679+
6680+ sum = 0;
8b6a4947
AM
6681+ for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6682+ i++, hbl++) {
6683+ n = au_hbl_count(hbl);
86dc4139
AM
6684+ sum += n;
6685+
6686+ n = snprintf(p->a + p->n, limit, "%lu ", n);
6687+ p->n += n;
6688+ limit -= n;
6689+ if (unlikely(limit <= 0))
6690+ goto out_free;
6691+ }
6692+ p->a[p->n - 1] = '\n';
6693+
6694+ /* the sum of plinks */
6695+ n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6696+ p->n += n;
6697+ limit -= n;
6698+ if (unlikely(limit <= 0))
6699+ goto out_free;
6700+ } else {
6701+#define str "1\n0\n0\n"
6702+ p->n = sizeof(str) - 1;
6703+ strcpy(p->a, str);
6704+#undef str
6705+ }
6706+ si_read_unlock(sb);
6707+
6708+ err = 0;
6709+ file->private_data = p;
6710+ goto out; /* success */
6711+
6712+out_free:
1c60b727 6713+ free_page((unsigned long)p);
86dc4139
AM
6714+out:
6715+ return err;
6716+}
6717+
6718+static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6719+ size_t count, loff_t *ppos)
6720+{
6721+ struct dbgaufs_plink_arg *p;
6722+
6723+ p = file->private_data;
6724+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6725+}
6726+
6727+static const struct file_operations dbgaufs_plink_fop = {
6728+ .owner = THIS_MODULE,
6729+ .open = dbgaufs_plink_open,
6730+ .release = dbgaufs_plink_release,
6731+ .read = dbgaufs_plink_read
6732+};
6733+
6734+/* ---------------------------------------------------------------------- */
6735+
1facf9fc 6736+static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6737+{
6738+ int err;
6739+ struct au_sbinfo *sbinfo;
6740+ struct super_block *sb;
6741+
6742+ sbinfo = inode->i_private;
6743+ sb = sbinfo->si_sb;
6744+ si_noflush_read_lock(sb);
062440b3 6745+ err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
1facf9fc 6746+ si_read_unlock(sb);
6747+ return err;
6748+}
6749+
6750+static const struct file_operations dbgaufs_xib_fop = {
4a4d8108 6751+ .owner = THIS_MODULE,
1facf9fc 6752+ .open = dbgaufs_xib_open,
6753+ .release = dbgaufs_xi_release,
6754+ .read = dbgaufs_xi_read
6755+};
6756+
6757+/* ---------------------------------------------------------------------- */
6758+
6759+#define DbgaufsXi_PREFIX "xi"
6760+
6761+static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6762+{
6763+ int err;
6764+ long l;
6765+ struct au_sbinfo *sbinfo;
6766+ struct super_block *sb;
6767+ struct file *xf;
6768+ struct qstr *name;
062440b3 6769+ struct au_branch *br;
1facf9fc 6770+
6771+ err = -ENOENT;
6772+ xf = NULL;
2000de60 6773+ name = &file->f_path.dentry->d_name;
1facf9fc 6774+ if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6775+ || memcmp(name->name, DbgaufsXi_PREFIX,
6776+ sizeof(DbgaufsXi_PREFIX) - 1)))
6777+ goto out;
9dbd164d 6778+ err = kstrtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
1facf9fc 6779+ if (unlikely(err))
6780+ goto out;
6781+
6782+ sbinfo = inode->i_private;
6783+ sb = sbinfo->si_sb;
6784+ si_noflush_read_lock(sb);
5afbbe0d 6785+ if (l <= au_sbbot(sb)) {
062440b3
AM
6786+ br = au_sbr(sb, (aufs_bindex_t)l);
6787+ xf = au_xino_file(br);
6788+ err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6789+ au_xino_count(br));
1facf9fc 6790+ } else
6791+ err = -ENOENT;
6792+ si_read_unlock(sb);
6793+
4f0767ce 6794+out:
1facf9fc 6795+ return err;
6796+}
6797+
6798+static const struct file_operations dbgaufs_xino_fop = {
4a4d8108 6799+ .owner = THIS_MODULE,
1facf9fc 6800+ .open = dbgaufs_xino_open,
6801+ .release = dbgaufs_xi_release,
6802+ .read = dbgaufs_xi_read
6803+};
6804+
062440b3
AM
6805+void dbgaufs_xino_del(struct au_branch *br)
6806+{
6807+ struct dentry *dbgaufs;
6808+
6809+ dbgaufs = br->br_dbgaufs;
6810+ if (!dbgaufs)
6811+ return;
6812+
6813+ br->br_dbgaufs = NULL;
6814+ /* debugfs acquires the parent i_mutex */
6815+ lockdep_off();
6816+ debugfs_remove(dbgaufs);
6817+ lockdep_on();
6818+}
6819+
1facf9fc 6820+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6821+{
5afbbe0d 6822+ aufs_bindex_t bbot;
1facf9fc 6823+ struct au_branch *br;
1facf9fc 6824+
6825+ if (!au_sbi(sb)->si_dbgaufs)
6826+ return;
6827+
5afbbe0d
AM
6828+ bbot = au_sbbot(sb);
6829+ for (; bindex <= bbot; bindex++) {
1facf9fc 6830+ br = au_sbr(sb, bindex);
062440b3
AM
6831+ dbgaufs_xino_del(br);
6832+ }
6833+}
6834+
6835+static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6836+ struct dentry *parent, struct au_sbinfo *sbinfo)
6837+{
6838+ struct au_branch *br;
6839+ struct dentry *d;
6840+ char name[sizeof(DbgaufsXi_PREFIX) + 5]; /* "xi" bindex NULL */
6841+
6842+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6843+ br = au_sbr(sb, bindex);
6844+ if (br->br_dbgaufs) {
6845+ struct qstr qstr = QSTR_INIT(name, strlen(name));
6846+
6847+ if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6848+ /* debugfs acquires the parent i_mutex */
6849+ lockdep_off();
6850+ d = debugfs_rename(parent, br->br_dbgaufs, parent,
6851+ name);
6852+ lockdep_on();
6853+ if (unlikely(!d))
6854+ pr_warn("failed renaming %pd/%s, ignored.\n",
6855+ parent, name);
6856+ }
6857+ } else {
e2f27e51 6858+ lockdep_off();
062440b3
AM
6859+ br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6860+ sbinfo, &dbgaufs_xino_fop);
e2f27e51 6861+ lockdep_on();
062440b3
AM
6862+ if (unlikely(!br->br_dbgaufs))
6863+ pr_warn("failed creaiting %pd/%s, ignored.\n",
6864+ parent, name);
1facf9fc 6865+ }
6866+}
6867+
062440b3 6868+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
1facf9fc 6869+{
6870+ struct au_sbinfo *sbinfo;
6871+ struct dentry *parent;
5afbbe0d 6872+ aufs_bindex_t bbot;
062440b3
AM
6873+
6874+ if (!au_opt_test(au_mntflags(sb), XINO))
6875+ return;
1facf9fc 6876+
6877+ sbinfo = au_sbi(sb);
6878+ parent = sbinfo->si_dbgaufs;
6879+ if (!parent)
6880+ return;
6881+
5afbbe0d 6882+ bbot = au_sbbot(sb);
062440b3
AM
6883+ if (topdown)
6884+ for (; bindex <= bbot; bindex++)
6885+ dbgaufs_br_add(sb, bindex, parent, sbinfo);
6886+ else
6887+ for (; bbot >= bindex; bbot--)
6888+ dbgaufs_br_add(sb, bbot, parent, sbinfo);
1facf9fc 6889+}
6890+
6891+/* ---------------------------------------------------------------------- */
6892+
6893+#ifdef CONFIG_AUFS_EXPORT
6894+static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6895+{
6896+ int err;
6897+ struct au_sbinfo *sbinfo;
6898+ struct super_block *sb;
6899+
6900+ sbinfo = inode->i_private;
6901+ sb = sbinfo->si_sb;
6902+ si_noflush_read_lock(sb);
062440b3 6903+ err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
1facf9fc 6904+ si_read_unlock(sb);
6905+ return err;
6906+}
6907+
6908+static const struct file_operations dbgaufs_xigen_fop = {
4a4d8108 6909+ .owner = THIS_MODULE,
1facf9fc 6910+ .open = dbgaufs_xigen_open,
6911+ .release = dbgaufs_xi_release,
6912+ .read = dbgaufs_xi_read
6913+};
6914+
6915+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6916+{
6917+ int err;
6918+
dece6358 6919+ /*
c1595e42 6920+ * This function is a dynamic '__init' function actually,
dece6358
AM
6921+ * so the tiny check for si_rwsem is unnecessary.
6922+ */
6923+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6924+
1facf9fc 6925+ err = -EIO;
6926+ sbinfo->si_dbgaufs_xigen = debugfs_create_file
6927+ ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6928+ &dbgaufs_xigen_fop);
6929+ if (sbinfo->si_dbgaufs_xigen)
6930+ err = 0;
6931+
6932+ return err;
6933+}
6934+#else
6935+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6936+{
6937+ return 0;
6938+}
6939+#endif /* CONFIG_AUFS_EXPORT */
6940+
6941+/* ---------------------------------------------------------------------- */
6942+
6943+void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6944+{
dece6358 6945+ /*
7e9cd9fe 6946+ * This function is a dynamic '__fin' function actually,
dece6358
AM
6947+ * so the tiny check for si_rwsem is unnecessary.
6948+ */
6949+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6950+
1facf9fc 6951+ debugfs_remove_recursive(sbinfo->si_dbgaufs);
6952+ sbinfo->si_dbgaufs = NULL;
1facf9fc 6953+}
6954+
6955+int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6956+{
6957+ int err;
6958+ char name[SysaufsSiNameLen];
6959+
dece6358 6960+ /*
c1595e42 6961+ * This function is a dynamic '__init' function actually,
dece6358
AM
6962+ * so the tiny check for si_rwsem is unnecessary.
6963+ */
6964+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6965+
1facf9fc 6966+ err = -ENOENT;
6967+ if (!dbgaufs) {
6968+ AuErr1("/debug/aufs is uninitialized\n");
6969+ goto out;
6970+ }
6971+
6972+ err = -EIO;
6973+ sysaufs_name(sbinfo, name);
6974+ sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6975+ if (unlikely(!sbinfo->si_dbgaufs))
6976+ goto out;
1facf9fc 6977+
062440b3 6978+ /* regardless plink/noplink option */
86dc4139
AM
6979+ sbinfo->si_dbgaufs_plink = debugfs_create_file
6980+ ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6981+ &dbgaufs_plink_fop);
6982+ if (unlikely(!sbinfo->si_dbgaufs_plink))
6983+ goto out_dir;
6984+
062440b3
AM
6985+ /* regardless xino/noxino option */
6986+ sbinfo->si_dbgaufs_xib = debugfs_create_file
6987+ ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6988+ &dbgaufs_xib_fop);
6989+ if (unlikely(!sbinfo->si_dbgaufs_xib))
6990+ goto out_dir;
6991+
1facf9fc 6992+ err = dbgaufs_xigen_init(sbinfo);
6993+ if (!err)
6994+ goto out; /* success */
6995+
4f0767ce 6996+out_dir:
1facf9fc 6997+ dbgaufs_si_fin(sbinfo);
4f0767ce 6998+out:
062440b3
AM
6999+ if (unlikely(err))
7000+ pr_err("debugfs/aufs failed\n");
1facf9fc 7001+ return err;
7002+}
7003+
7004+/* ---------------------------------------------------------------------- */
7005+
7006+void dbgaufs_fin(void)
7007+{
7008+ debugfs_remove(dbgaufs);
7009+}
7010+
7011+int __init dbgaufs_init(void)
7012+{
7013+ int err;
7014+
7015+ err = -EIO;
7016+ dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
7017+ if (dbgaufs)
7018+ err = 0;
7019+ return err;
7020+}
7f207e10
AM
7021diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
7022--- /usr/share/empty/fs/aufs/dbgaufs.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 7023+++ linux/fs/aufs/dbgaufs.h 2018-08-12 23:43:05.453457863 +0200
062440b3
AM
7024@@ -0,0 +1,53 @@
7025+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7026+/*
b00004a5 7027+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 7028+ *
7029+ * This program, aufs is free software; you can redistribute it and/or modify
7030+ * it under the terms of the GNU General Public License as published by
7031+ * the Free Software Foundation; either version 2 of the License, or
7032+ * (at your option) any later version.
dece6358
AM
7033+ *
7034+ * This program is distributed in the hope that it will be useful,
7035+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7036+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7037+ * GNU General Public License for more details.
7038+ *
7039+ * You should have received a copy of the GNU General Public License
523b37e3 7040+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7041+ */
7042+
7043+/*
7044+ * debugfs interface
7045+ */
7046+
7047+#ifndef __DBGAUFS_H__
7048+#define __DBGAUFS_H__
7049+
7050+#ifdef __KERNEL__
7051+
dece6358 7052+struct super_block;
1facf9fc 7053+struct au_sbinfo;
062440b3 7054+struct au_branch;
dece6358 7055+
1facf9fc 7056+#ifdef CONFIG_DEBUG_FS
7057+/* dbgaufs.c */
062440b3 7058+void dbgaufs_xino_del(struct au_branch *br);
1facf9fc 7059+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
062440b3 7060+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
1facf9fc 7061+void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
7062+int dbgaufs_si_init(struct au_sbinfo *sbinfo);
7063+void dbgaufs_fin(void);
7064+int __init dbgaufs_init(void);
1facf9fc 7065+#else
062440b3 7066+AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
4a4d8108 7067+AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
062440b3
AM
7068+AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
7069+ int topdown)
4a4d8108
AM
7070+AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
7071+AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
7072+AuStubVoid(dbgaufs_fin, void)
7073+AuStubInt0(__init dbgaufs_init, void)
1facf9fc 7074+#endif /* CONFIG_DEBUG_FS */
7075+
7076+#endif /* __KERNEL__ */
7077+#endif /* __DBGAUFS_H__ */
7f207e10
AM
7078diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
7079--- /usr/share/empty/fs/aufs/dcsub.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9
AM
7080+++ linux/fs/aufs/dcsub.c 2018-08-12 23:43:05.453457863 +0200
7081@@ -0,0 +1,225 @@
7082+// SPDX-License-Identifier: GPL-2.0
1facf9fc 7083+/*
b00004a5 7084+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 7085+ *
7086+ * This program, aufs is free software; you can redistribute it and/or modify
7087+ * it under the terms of the GNU General Public License as published by
7088+ * the Free Software Foundation; either version 2 of the License, or
7089+ * (at your option) any later version.
dece6358
AM
7090+ *
7091+ * This program is distributed in the hope that it will be useful,
7092+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7093+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7094+ * GNU General Public License for more details.
7095+ *
7096+ * You should have received a copy of the GNU General Public License
523b37e3 7097+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7098+ */
7099+
7100+/*
7101+ * sub-routines for dentry cache
7102+ */
7103+
7104+#include "aufs.h"
7105+
7106+static void au_dpage_free(struct au_dpage *dpage)
7107+{
7108+ int i;
7109+ struct dentry **p;
7110+
7111+ p = dpage->dentries;
7112+ for (i = 0; i < dpage->ndentry; i++)
7113+ dput(*p++);
1c60b727 7114+ free_page((unsigned long)dpage->dentries);
1facf9fc 7115+}
7116+
7117+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
7118+{
7119+ int err;
7120+ void *p;
7121+
7122+ err = -ENOMEM;
7123+ dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
7124+ if (unlikely(!dpages->dpages))
7125+ goto out;
7126+
7127+ p = (void *)__get_free_page(gfp);
7128+ if (unlikely(!p))
7129+ goto out_dpages;
7130+
7131+ dpages->dpages[0].ndentry = 0;
7132+ dpages->dpages[0].dentries = p;
7133+ dpages->ndpage = 1;
7134+ return 0; /* success */
7135+
4f0767ce 7136+out_dpages:
1c60b727 7137+ kfree(dpages->dpages);
4f0767ce 7138+out:
1facf9fc 7139+ return err;
7140+}
7141+
7142+void au_dpages_free(struct au_dcsub_pages *dpages)
7143+{
7144+ int i;
7145+ struct au_dpage *p;
7146+
7147+ p = dpages->dpages;
7148+ for (i = 0; i < dpages->ndpage; i++)
7149+ au_dpage_free(p++);
1c60b727 7150+ kfree(dpages->dpages);
1facf9fc 7151+}
7152+
7153+static int au_dpages_append(struct au_dcsub_pages *dpages,
7154+ struct dentry *dentry, gfp_t gfp)
7155+{
7156+ int err, sz;
7157+ struct au_dpage *dpage;
7158+ void *p;
7159+
7160+ dpage = dpages->dpages + dpages->ndpage - 1;
7161+ sz = PAGE_SIZE / sizeof(dentry);
7162+ if (unlikely(dpage->ndentry >= sz)) {
7163+ AuLabel(new dpage);
7164+ err = -ENOMEM;
7165+ sz = dpages->ndpage * sizeof(*dpages->dpages);
7166+ p = au_kzrealloc(dpages->dpages, sz,
e2f27e51
AM
7167+ sz + sizeof(*dpages->dpages), gfp,
7168+ /*may_shrink*/0);
1facf9fc 7169+ if (unlikely(!p))
7170+ goto out;
7171+
7172+ dpages->dpages = p;
7173+ dpage = dpages->dpages + dpages->ndpage;
7174+ p = (void *)__get_free_page(gfp);
7175+ if (unlikely(!p))
7176+ goto out;
7177+
7178+ dpage->ndentry = 0;
7179+ dpage->dentries = p;
7180+ dpages->ndpage++;
7181+ }
7182+
c1595e42 7183+ AuDebugOn(au_dcount(dentry) <= 0);
027c5e7a 7184+ dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
1facf9fc 7185+ return 0; /* success */
7186+
4f0767ce 7187+out:
1facf9fc 7188+ return err;
7189+}
7190+
c1595e42
JR
7191+/* todo: BAD approach */
7192+/* copied from linux/fs/dcache.c */
7193+enum d_walk_ret {
7194+ D_WALK_CONTINUE,
7195+ D_WALK_QUIT,
7196+ D_WALK_NORETRY,
7197+ D_WALK_SKIP,
7198+};
7199+
7200+extern void d_walk(struct dentry *parent, void *data,
cd7a4cd9 7201+ enum d_walk_ret (*enter)(void *, struct dentry *));
c1595e42
JR
7202+
7203+struct ac_dpages_arg {
1facf9fc 7204+ int err;
c1595e42
JR
7205+ struct au_dcsub_pages *dpages;
7206+ struct super_block *sb;
7207+ au_dpages_test test;
7208+ void *arg;
7209+};
1facf9fc 7210+
c1595e42
JR
7211+static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7212+{
7213+ enum d_walk_ret ret;
7214+ struct ac_dpages_arg *arg = _arg;
1facf9fc 7215+
c1595e42
JR
7216+ ret = D_WALK_CONTINUE;
7217+ if (dentry->d_sb == arg->sb
7218+ && !IS_ROOT(dentry)
7219+ && au_dcount(dentry) > 0
7220+ && au_di(dentry)
7221+ && (!arg->test || arg->test(dentry, arg->arg))) {
7222+ arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7223+ if (unlikely(arg->err))
7224+ ret = D_WALK_QUIT;
1facf9fc 7225+ }
7226+
c1595e42
JR
7227+ return ret;
7228+}
027c5e7a 7229+
c1595e42
JR
7230+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7231+ au_dpages_test test, void *arg)
7232+{
7233+ struct ac_dpages_arg args = {
7234+ .err = 0,
7235+ .dpages = dpages,
7236+ .sb = root->d_sb,
7237+ .test = test,
7238+ .arg = arg
7239+ };
027c5e7a 7240+
cd7a4cd9 7241+ d_walk(root, &args, au_call_dpages_append);
c1595e42
JR
7242+
7243+ return args.err;
1facf9fc 7244+}
7245+
7246+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7247+ int do_include, au_dpages_test test, void *arg)
7248+{
7249+ int err;
7250+
7251+ err = 0;
027c5e7a
AM
7252+ write_seqlock(&rename_lock);
7253+ spin_lock(&dentry->d_lock);
7254+ if (do_include
c1595e42 7255+ && au_dcount(dentry) > 0
027c5e7a 7256+ && (!test || test(dentry, arg)))
1facf9fc 7257+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
7258+ spin_unlock(&dentry->d_lock);
7259+ if (unlikely(err))
7260+ goto out;
7261+
7262+ /*
523b37e3 7263+ * RCU for vfsmount is unnecessary since this is a traverse in a single
027c5e7a
AM
7264+ * mount
7265+ */
1facf9fc 7266+ while (!IS_ROOT(dentry)) {
027c5e7a
AM
7267+ dentry = dentry->d_parent; /* rename_lock is locked */
7268+ spin_lock(&dentry->d_lock);
c1595e42 7269+ if (au_dcount(dentry) > 0
027c5e7a 7270+ && (!test || test(dentry, arg)))
1facf9fc 7271+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
7272+ spin_unlock(&dentry->d_lock);
7273+ if (unlikely(err))
7274+ break;
1facf9fc 7275+ }
7276+
4f0767ce 7277+out:
027c5e7a 7278+ write_sequnlock(&rename_lock);
1facf9fc 7279+ return err;
7280+}
7281+
027c5e7a
AM
7282+static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7283+{
7284+ return au_di(dentry) && dentry->d_sb == arg;
7285+}
7286+
7287+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7288+ struct dentry *dentry, int do_include)
7289+{
7290+ return au_dcsub_pages_rev(dpages, dentry, do_include,
7291+ au_dcsub_dpages_aufs, dentry->d_sb);
7292+}
7293+
4a4d8108 7294+int au_test_subdir(struct dentry *d1, struct dentry *d2)
1facf9fc 7295+{
4a4d8108
AM
7296+ struct path path[2] = {
7297+ {
7298+ .dentry = d1
7299+ },
7300+ {
7301+ .dentry = d2
7302+ }
7303+ };
1facf9fc 7304+
4a4d8108 7305+ return path_is_under(path + 0, path + 1);
1facf9fc 7306+}
7f207e10
AM
7307diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7308--- /usr/share/empty/fs/aufs/dcsub.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 7309+++ linux/fs/aufs/dcsub.h 2018-08-12 23:43:05.453457863 +0200
062440b3
AM
7310@@ -0,0 +1,137 @@
7311+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7312+/*
b00004a5 7313+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 7314+ *
7315+ * This program, aufs is free software; you can redistribute it and/or modify
7316+ * it under the terms of the GNU General Public License as published by
7317+ * the Free Software Foundation; either version 2 of the License, or
7318+ * (at your option) any later version.
dece6358
AM
7319+ *
7320+ * This program is distributed in the hope that it will be useful,
7321+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7322+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7323+ * GNU General Public License for more details.
7324+ *
7325+ * You should have received a copy of the GNU General Public License
523b37e3 7326+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7327+ */
7328+
7329+/*
7330+ * sub-routines for dentry cache
7331+ */
7332+
7333+#ifndef __AUFS_DCSUB_H__
7334+#define __AUFS_DCSUB_H__
7335+
7336+#ifdef __KERNEL__
7337+
7f207e10 7338+#include <linux/dcache.h>
027c5e7a 7339+#include <linux/fs.h>
dece6358 7340+
1facf9fc 7341+struct au_dpage {
7342+ int ndentry;
7343+ struct dentry **dentries;
7344+};
7345+
7346+struct au_dcsub_pages {
7347+ int ndpage;
7348+ struct au_dpage *dpages;
7349+};
7350+
7351+/* ---------------------------------------------------------------------- */
7352+
7f207e10 7353+/* dcsub.c */
1facf9fc 7354+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7355+void au_dpages_free(struct au_dcsub_pages *dpages);
7356+typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7357+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7358+ au_dpages_test test, void *arg);
7359+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7360+ int do_include, au_dpages_test test, void *arg);
027c5e7a
AM
7361+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7362+ struct dentry *dentry, int do_include);
4a4d8108 7363+int au_test_subdir(struct dentry *d1, struct dentry *d2);
1facf9fc 7364+
7f207e10
AM
7365+/* ---------------------------------------------------------------------- */
7366+
523b37e3
AM
7367+/*
7368+ * todo: in linux-3.13, several similar (but faster) helpers are added to
7369+ * include/linux/dcache.h. Try them (in the future).
7370+ */
7371+
027c5e7a
AM
7372+static inline int au_d_hashed_positive(struct dentry *d)
7373+{
7374+ int err;
5527c038 7375+ struct inode *inode = d_inode(d);
076b876e 7376+
027c5e7a 7377+ err = 0;
5527c038
JR
7378+ if (unlikely(d_unhashed(d)
7379+ || d_is_negative(d)
7380+ || !inode->i_nlink))
027c5e7a
AM
7381+ err = -ENOENT;
7382+ return err;
7383+}
7384+
38d290e6
JR
7385+static inline int au_d_linkable(struct dentry *d)
7386+{
7387+ int err;
5527c038 7388+ struct inode *inode = d_inode(d);
076b876e 7389+
38d290e6
JR
7390+ err = au_d_hashed_positive(d);
7391+ if (err
5527c038 7392+ && d_is_positive(d)
38d290e6
JR
7393+ && (inode->i_state & I_LINKABLE))
7394+ err = 0;
7395+ return err;
7396+}
7397+
027c5e7a
AM
7398+static inline int au_d_alive(struct dentry *d)
7399+{
7400+ int err;
7401+ struct inode *inode;
076b876e 7402+
027c5e7a
AM
7403+ err = 0;
7404+ if (!IS_ROOT(d))
7405+ err = au_d_hashed_positive(d);
7406+ else {
5527c038
JR
7407+ inode = d_inode(d);
7408+ if (unlikely(d_unlinked(d)
7409+ || d_is_negative(d)
7410+ || !inode->i_nlink))
027c5e7a
AM
7411+ err = -ENOENT;
7412+ }
7413+ return err;
7414+}
7415+
7416+static inline int au_alive_dir(struct dentry *d)
7f207e10 7417+{
027c5e7a 7418+ int err;
076b876e 7419+
027c5e7a 7420+ err = au_d_alive(d);
5527c038 7421+ if (unlikely(err || IS_DEADDIR(d_inode(d))))
027c5e7a
AM
7422+ err = -ENOENT;
7423+ return err;
7f207e10
AM
7424+}
7425+
38d290e6
JR
7426+static inline int au_qstreq(struct qstr *a, struct qstr *b)
7427+{
7428+ return a->len == b->len
7429+ && !memcmp(a->name, b->name, a->len);
7430+}
7431+
7e9cd9fe
AM
7432+/*
7433+ * by the commit
7434+ * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7435+ * taking d_lock
7436+ * the type of d_lockref.count became int, but the inlined function d_count()
7437+ * still returns unsigned int.
7438+ * I don't know why. Maybe it is for every d_count() users?
7439+ * Anyway au_dcount() lives on.
7440+ */
c1595e42
JR
7441+static inline int au_dcount(struct dentry *d)
7442+{
7443+ return (int)d_count(d);
7444+}
7445+
1facf9fc 7446+#endif /* __KERNEL__ */
7447+#endif /* __AUFS_DCSUB_H__ */
7f207e10
AM
7448diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7449--- /usr/share/empty/fs/aufs/debug.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 7450+++ linux/fs/aufs/debug.c 2018-08-12 23:43:05.453457863 +0200
062440b3 7451@@ -0,0 +1,441 @@
cd7a4cd9 7452+// SPDX-License-Identifier: GPL-2.0
1facf9fc 7453+/*
b00004a5 7454+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 7455+ *
7456+ * This program, aufs is free software; you can redistribute it and/or modify
7457+ * it under the terms of the GNU General Public License as published by
7458+ * the Free Software Foundation; either version 2 of the License, or
7459+ * (at your option) any later version.
dece6358
AM
7460+ *
7461+ * This program is distributed in the hope that it will be useful,
7462+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7463+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7464+ * GNU General Public License for more details.
7465+ *
7466+ * You should have received a copy of the GNU General Public License
523b37e3 7467+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7468+ */
7469+
7470+/*
7471+ * debug print functions
7472+ */
7473+
7474+#include "aufs.h"
7475+
392086de
AM
7476+/* Returns 0, or -errno. arg is in kp->arg. */
7477+static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7478+{
7479+ int err, n;
7480+
7481+ err = kstrtoint(val, 0, &n);
7482+ if (!err) {
7483+ if (n > 0)
7484+ au_debug_on();
7485+ else
7486+ au_debug_off();
7487+ }
7488+ return err;
7489+}
7490+
7491+/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
7492+static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7493+{
7494+ atomic_t *a;
7495+
7496+ a = kp->arg;
7497+ return sprintf(buffer, "%d", atomic_read(a));
7498+}
7499+
7500+static struct kernel_param_ops param_ops_atomic_t = {
7501+ .set = param_atomic_t_set,
7502+ .get = param_atomic_t_get
7503+ /* void (*free)(void *arg) */
7504+};
7505+
7506+atomic_t aufs_debug = ATOMIC_INIT(0);
1facf9fc 7507+MODULE_PARM_DESC(debug, "debug print");
cd7a4cd9 7508+module_param_named(debug, aufs_debug, atomic_t, 0664);
1facf9fc 7509+
c1595e42 7510+DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
1facf9fc 7511+char *au_plevel = KERN_DEBUG;
e49829fe
JR
7512+#define dpri(fmt, ...) do { \
7513+ if ((au_plevel \
7514+ && strcmp(au_plevel, KERN_DEBUG)) \
7515+ || au_debug_test()) \
7516+ printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
1facf9fc 7517+} while (0)
7518+
7519+/* ---------------------------------------------------------------------- */
7520+
7521+void au_dpri_whlist(struct au_nhash *whlist)
7522+{
7523+ unsigned long ul, n;
7524+ struct hlist_head *head;
c06a8ce3 7525+ struct au_vdir_wh *pos;
1facf9fc 7526+
7527+ n = whlist->nh_num;
7528+ head = whlist->nh_head;
7529+ for (ul = 0; ul < n; ul++) {
c06a8ce3 7530+ hlist_for_each_entry(pos, head, wh_hash)
1facf9fc 7531+ dpri("b%d, %.*s, %d\n",
c06a8ce3
AM
7532+ pos->wh_bindex,
7533+ pos->wh_str.len, pos->wh_str.name,
7534+ pos->wh_str.len);
1facf9fc 7535+ head++;
7536+ }
7537+}
7538+
7539+void au_dpri_vdir(struct au_vdir *vdir)
7540+{
7541+ unsigned long ul;
7542+ union au_vdir_deblk_p p;
7543+ unsigned char *o;
7544+
7545+ if (!vdir || IS_ERR(vdir)) {
7546+ dpri("err %ld\n", PTR_ERR(vdir));
7547+ return;
7548+ }
7549+
be118d29 7550+ dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
1facf9fc 7551+ vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7552+ vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7553+ for (ul = 0; ul < vdir->vd_nblk; ul++) {
7554+ p.deblk = vdir->vd_deblk[ul];
7555+ o = p.deblk;
7556+ dpri("[%lu]: %p\n", ul, o);
7557+ }
7558+}
7559+
53392da6 7560+static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
1facf9fc 7561+ struct dentry *wh)
7562+{
7563+ char *n = NULL;
7564+ int l = 0;
7565+
7566+ if (!inode || IS_ERR(inode)) {
7567+ dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7568+ return -1;
7569+ }
7570+
c2b27bf2 7571+ /* the type of i_blocks depends upon CONFIG_LBDAF */
1facf9fc 7572+ BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7573+ && sizeof(inode->i_blocks) != sizeof(u64));
7574+ if (wh) {
7575+ n = (void *)wh->d_name.name;
7576+ l = wh->d_name.len;
7577+ }
7578+
53392da6
AM
7579+ dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7580+ " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7581+ bindex, inode,
1facf9fc 7582+ inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7583+ atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7584+ i_size_read(inode), (unsigned long long)inode->i_blocks,
cd7a4cd9 7585+ hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
1facf9fc 7586+ inode->i_mapping ? inode->i_mapping->nrpages : 0,
be118d29 7587+ inode->i_state, inode->i_flags, inode_peek_iversion(inode),
b752ccd1 7588+ inode->i_generation,
1facf9fc 7589+ l ? ", wh " : "", l, n);
7590+ return 0;
7591+}
7592+
7593+void au_dpri_inode(struct inode *inode)
7594+{
7595+ struct au_iinfo *iinfo;
5afbbe0d 7596+ struct au_hinode *hi;
1facf9fc 7597+ aufs_bindex_t bindex;
53392da6 7598+ int err, hn;
1facf9fc 7599+
53392da6 7600+ err = do_pri_inode(-1, inode, -1, NULL);
5afbbe0d 7601+ if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
1facf9fc 7602+ return;
7603+
7604+ iinfo = au_ii(inode);
5afbbe0d
AM
7605+ dpri("i-1: btop %d, bbot %d, gen %d\n",
7606+ iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7607+ if (iinfo->ii_btop < 0)
1facf9fc 7608+ return;
53392da6 7609+ hn = 0;
5afbbe0d
AM
7610+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7611+ hi = au_hinode(iinfo, bindex);
7612+ hn = !!au_hn(hi);
7613+ do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
53392da6 7614+ }
1facf9fc 7615+}
7616+
2cbb1c4b
JR
7617+void au_dpri_dalias(struct inode *inode)
7618+{
7619+ struct dentry *d;
7620+
7621+ spin_lock(&inode->i_lock);
c1595e42 7622+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
2cbb1c4b
JR
7623+ au_dpri_dentry(d);
7624+ spin_unlock(&inode->i_lock);
7625+}
7626+
1facf9fc 7627+static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7628+{
7629+ struct dentry *wh = NULL;
53392da6 7630+ int hn;
5afbbe0d 7631+ struct inode *inode;
076b876e 7632+ struct au_iinfo *iinfo;
5afbbe0d 7633+ struct au_hinode *hi;
1facf9fc 7634+
7635+ if (!dentry || IS_ERR(dentry)) {
7636+ dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7637+ return -1;
7638+ }
7639+ /* do not call dget_parent() here */
027c5e7a 7640+ /* note: access d_xxx without d_lock */
523b37e3
AM
7641+ dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7642+ bindex, dentry, dentry,
1facf9fc 7643+ dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
c1595e42 7644+ au_dcount(dentry), dentry->d_flags,
523b37e3 7645+ d_unhashed(dentry) ? "un" : "");
53392da6 7646+ hn = -1;
5afbbe0d
AM
7647+ inode = NULL;
7648+ if (d_is_positive(dentry))
7649+ inode = d_inode(dentry);
7650+ if (inode
7651+ && au_test_aufs(dentry->d_sb)
7652+ && bindex >= 0
7653+ && !au_is_bad_inode(inode)) {
7654+ iinfo = au_ii(inode);
7655+ hi = au_hinode(iinfo, bindex);
7656+ hn = !!au_hn(hi);
7657+ wh = hi->hi_whdentry;
7658+ }
7659+ do_pri_inode(bindex, inode, hn, wh);
1facf9fc 7660+ return 0;
7661+}
7662+
7663+void au_dpri_dentry(struct dentry *dentry)
7664+{
7665+ struct au_dinfo *dinfo;
7666+ aufs_bindex_t bindex;
7667+ int err;
7668+
7669+ err = do_pri_dentry(-1, dentry);
7670+ if (err || !au_test_aufs(dentry->d_sb))
7671+ return;
7672+
7673+ dinfo = au_di(dentry);
7674+ if (!dinfo)
7675+ return;
5afbbe0d
AM
7676+ dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7677+ dinfo->di_btop, dinfo->di_bbot,
38d290e6
JR
7678+ dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7679+ dinfo->di_tmpfile);
5afbbe0d 7680+ if (dinfo->di_btop < 0)
1facf9fc 7681+ return;
5afbbe0d
AM
7682+ for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7683+ do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
1facf9fc 7684+}
7685+
7686+static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7687+{
7688+ char a[32];
7689+
7690+ if (!file || IS_ERR(file)) {
7691+ dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7692+ return -1;
7693+ }
7694+ a[0] = 0;
7695+ if (bindex < 0
b912730e 7696+ && !IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7697+ && au_test_aufs(file->f_path.dentry->d_sb)
1facf9fc 7698+ && au_fi(file))
e49829fe 7699+ snprintf(a, sizeof(a), ", gen %d, mmapped %d",
2cbb1c4b 7700+ au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
b752ccd1 7701+ dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
1facf9fc 7702+ bindex, file->f_mode, file->f_flags, (long)file_count(file),
b752ccd1 7703+ file->f_version, file->f_pos, a);
b912730e 7704+ if (!IS_ERR_OR_NULL(file->f_path.dentry))
2000de60 7705+ do_pri_dentry(bindex, file->f_path.dentry);
1facf9fc 7706+ return 0;
7707+}
7708+
7709+void au_dpri_file(struct file *file)
7710+{
7711+ struct au_finfo *finfo;
4a4d8108
AM
7712+ struct au_fidir *fidir;
7713+ struct au_hfile *hfile;
1facf9fc 7714+ aufs_bindex_t bindex;
7715+ int err;
7716+
7717+ err = do_pri_file(-1, file);
2000de60 7718+ if (err
b912730e 7719+ || IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7720+ || !au_test_aufs(file->f_path.dentry->d_sb))
1facf9fc 7721+ return;
7722+
7723+ finfo = au_fi(file);
7724+ if (!finfo)
7725+ return;
4a4d8108 7726+ if (finfo->fi_btop < 0)
1facf9fc 7727+ return;
4a4d8108
AM
7728+ fidir = finfo->fi_hdir;
7729+ if (!fidir)
7730+ do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7731+ else
e49829fe
JR
7732+ for (bindex = finfo->fi_btop;
7733+ bindex >= 0 && bindex <= fidir->fd_bbot;
4a4d8108
AM
7734+ bindex++) {
7735+ hfile = fidir->fd_hfile + bindex;
7736+ do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7737+ }
1facf9fc 7738+}
7739+
7740+static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7741+{
7742+ struct vfsmount *mnt;
7743+ struct super_block *sb;
7744+
7745+ if (!br || IS_ERR(br))
7746+ goto out;
86dc4139 7747+ mnt = au_br_mnt(br);
1facf9fc 7748+ if (!mnt || IS_ERR(mnt))
7749+ goto out;
7750+ sb = mnt->mnt_sb;
7751+ if (!sb || IS_ERR(sb))
7752+ goto out;
7753+
5afbbe0d 7754+ dpri("s%d: {perm 0x%x, id %d, cnt %lld, wbr %p}, "
b752ccd1 7755+ "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
1facf9fc 7756+ "xino %d\n",
5afbbe0d 7757+ bindex, br->br_perm, br->br_id, au_br_count(br),
1e00d052 7758+ br->br_wbr, au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
b752ccd1 7759+ sb->s_flags, sb->s_count,
062440b3 7760+ atomic_read(&sb->s_active), !!au_xino_file(br));
1facf9fc 7761+ return 0;
7762+
4f0767ce 7763+out:
1facf9fc 7764+ dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7765+ return -1;
7766+}
7767+
7768+void au_dpri_sb(struct super_block *sb)
7769+{
7770+ struct au_sbinfo *sbinfo;
7771+ aufs_bindex_t bindex;
7772+ int err;
7773+ /* to reuduce stack size */
7774+ struct {
7775+ struct vfsmount mnt;
7776+ struct au_branch fake;
7777+ } *a;
7778+
7779+ /* this function can be called from magic sysrq */
7780+ a = kzalloc(sizeof(*a), GFP_ATOMIC);
7781+ if (unlikely(!a)) {
7782+ dpri("no memory\n");
7783+ return;
7784+ }
7785+
7786+ a->mnt.mnt_sb = sb;
86dc4139 7787+ a->fake.br_path.mnt = &a->mnt;
5afbbe0d 7788+ au_br_count_init(&a->fake);
1facf9fc 7789+ err = do_pri_br(-1, &a->fake);
5afbbe0d 7790+ au_br_count_fin(&a->fake);
1c60b727 7791+ kfree(a);
1facf9fc 7792+ dpri("dev 0x%x\n", sb->s_dev);
7793+ if (err || !au_test_aufs(sb))
7794+ return;
7795+
7796+ sbinfo = au_sbi(sb);
7797+ if (!sbinfo)
7798+ return;
f0c0a007
AM
7799+ dpri("nw %d, gen %u, kobj %d\n",
7800+ atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
521ced18 7801+ kref_read(&sbinfo->si_kobj.kref));
5afbbe0d 7802+ for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
1facf9fc 7803+ do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7804+}
7805+
7806+/* ---------------------------------------------------------------------- */
7807+
027c5e7a
AM
7808+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7809+{
5527c038 7810+ struct inode *h_inode, *inode = d_inode(dentry);
027c5e7a 7811+ struct dentry *h_dentry;
5afbbe0d 7812+ aufs_bindex_t bindex, bbot, bi;
027c5e7a
AM
7813+
7814+ if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7815+ return;
7816+
5afbbe0d
AM
7817+ bbot = au_dbbot(dentry);
7818+ bi = au_ibbot(inode);
7819+ if (bi < bbot)
7820+ bbot = bi;
7821+ bindex = au_dbtop(dentry);
7822+ bi = au_ibtop(inode);
027c5e7a
AM
7823+ if (bi > bindex)
7824+ bindex = bi;
7825+
5afbbe0d 7826+ for (; bindex <= bbot; bindex++) {
027c5e7a
AM
7827+ h_dentry = au_h_dptr(dentry, bindex);
7828+ if (!h_dentry)
7829+ continue;
7830+ h_inode = au_h_iptr(inode, bindex);
5527c038 7831+ if (unlikely(h_inode != d_inode(h_dentry))) {
392086de 7832+ au_debug_on();
027c5e7a
AM
7833+ AuDbg("b%d, %s:%d\n", bindex, func, line);
7834+ AuDbgDentry(dentry);
7835+ AuDbgInode(inode);
392086de 7836+ au_debug_off();
027c5e7a
AM
7837+ BUG();
7838+ }
7839+ }
7840+}
7841+
1facf9fc 7842+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7843+{
7844+ int err, i, j;
7845+ struct au_dcsub_pages dpages;
7846+ struct au_dpage *dpage;
7847+ struct dentry **dentries;
7848+
7849+ err = au_dpages_init(&dpages, GFP_NOFS);
7850+ AuDebugOn(err);
027c5e7a 7851+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
1facf9fc 7852+ AuDebugOn(err);
7853+ for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7854+ dpage = dpages.dpages + i;
7855+ dentries = dpage->dentries;
7856+ for (j = dpage->ndentry - 1; !err && j >= 0; j--)
027c5e7a 7857+ AuDebugOn(au_digen_test(dentries[j], sigen));
1facf9fc 7858+ }
7859+ au_dpages_free(&dpages);
7860+}
7861+
1facf9fc 7862+void au_dbg_verify_kthread(void)
7863+{
53392da6 7864+ if (au_wkq_test()) {
1facf9fc 7865+ au_dbg_blocked();
1e00d052
AM
7866+ /*
7867+ * It may be recursive, but udba=notify between two aufs mounts,
7868+ * where a single ro branch is shared, is not a problem.
7869+ */
7870+ /* WARN_ON(1); */
1facf9fc 7871+ }
7872+}
7873+
7874+/* ---------------------------------------------------------------------- */
7875+
1facf9fc 7876+int __init au_debug_init(void)
7877+{
7878+ aufs_bindex_t bindex;
7879+ struct au_vdir_destr destr;
7880+
7881+ bindex = -1;
7882+ AuDebugOn(bindex >= 0);
7883+
7884+ destr.len = -1;
7885+ AuDebugOn(destr.len < NAME_MAX);
7886+
7887+#ifdef CONFIG_4KSTACKS
0c3ec466 7888+ pr_warn("CONFIG_4KSTACKS is defined.\n");
1facf9fc 7889+#endif
7890+
1facf9fc 7891+ return 0;
7892+}
7f207e10
AM
7893diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7894--- /usr/share/empty/fs/aufs/debug.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 7895+++ linux/fs/aufs/debug.h 2018-08-12 23:43:05.453457863 +0200
062440b3
AM
7896@@ -0,0 +1,226 @@
7897+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7898+/*
b00004a5 7899+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 7900+ *
7901+ * This program, aufs is free software; you can redistribute it and/or modify
7902+ * it under the terms of the GNU General Public License as published by
7903+ * the Free Software Foundation; either version 2 of the License, or
7904+ * (at your option) any later version.
dece6358
AM
7905+ *
7906+ * This program is distributed in the hope that it will be useful,
7907+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7908+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7909+ * GNU General Public License for more details.
7910+ *
7911+ * You should have received a copy of the GNU General Public License
523b37e3 7912+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7913+ */
7914+
7915+/*
7916+ * debug print functions
7917+ */
7918+
7919+#ifndef __AUFS_DEBUG_H__
7920+#define __AUFS_DEBUG_H__
7921+
7922+#ifdef __KERNEL__
7923+
392086de 7924+#include <linux/atomic.h>
4a4d8108
AM
7925+#include <linux/module.h>
7926+#include <linux/kallsyms.h>
1facf9fc 7927+#include <linux/sysrq.h>
4a4d8108 7928+
1facf9fc 7929+#ifdef CONFIG_AUFS_DEBUG
7930+#define AuDebugOn(a) BUG_ON(a)
7931+
7932+/* module parameter */
392086de
AM
7933+extern atomic_t aufs_debug;
7934+static inline void au_debug_on(void)
1facf9fc 7935+{
392086de
AM
7936+ atomic_inc(&aufs_debug);
7937+}
7938+static inline void au_debug_off(void)
7939+{
7940+ atomic_dec_if_positive(&aufs_debug);
1facf9fc 7941+}
7942+
7943+static inline int au_debug_test(void)
7944+{
392086de 7945+ return atomic_read(&aufs_debug) > 0;
1facf9fc 7946+}
7947+#else
7948+#define AuDebugOn(a) do {} while (0)
392086de
AM
7949+AuStubVoid(au_debug_on, void)
7950+AuStubVoid(au_debug_off, void)
4a4d8108 7951+AuStubInt0(au_debug_test, void)
1facf9fc 7952+#endif /* CONFIG_AUFS_DEBUG */
7953+
392086de
AM
7954+#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7955+
1facf9fc 7956+/* ---------------------------------------------------------------------- */
7957+
7958+/* debug print */
7959+
4a4d8108 7960+#define AuDbg(fmt, ...) do { \
1facf9fc 7961+ if (au_debug_test()) \
4a4d8108 7962+ pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
1facf9fc 7963+} while (0)
4a4d8108
AM
7964+#define AuLabel(l) AuDbg(#l "\n")
7965+#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7966+#define AuWarn1(fmt, ...) do { \
1facf9fc 7967+ static unsigned char _c; \
7968+ if (!_c++) \
0c3ec466 7969+ pr_warn(fmt, ##__VA_ARGS__); \
1facf9fc 7970+} while (0)
7971+
4a4d8108 7972+#define AuErr1(fmt, ...) do { \
1facf9fc 7973+ static unsigned char _c; \
7974+ if (!_c++) \
4a4d8108 7975+ pr_err(fmt, ##__VA_ARGS__); \
1facf9fc 7976+} while (0)
7977+
4a4d8108 7978+#define AuIOErr1(fmt, ...) do { \
1facf9fc 7979+ static unsigned char _c; \
7980+ if (!_c++) \
4a4d8108 7981+ AuIOErr(fmt, ##__VA_ARGS__); \
1facf9fc 7982+} while (0)
7983+
7984+#define AuUnsupportMsg "This operation is not supported." \
7985+ " Please report this application to aufs-users ML."
4a4d8108
AM
7986+#define AuUnsupport(fmt, ...) do { \
7987+ pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
1facf9fc 7988+ dump_stack(); \
7989+} while (0)
7990+
7991+#define AuTraceErr(e) do { \
7992+ if (unlikely((e) < 0)) \
7993+ AuDbg("err %d\n", (int)(e)); \
7994+} while (0)
7995+
7996+#define AuTraceErrPtr(p) do { \
7997+ if (IS_ERR(p)) \
7998+ AuDbg("err %ld\n", PTR_ERR(p)); \
7999+} while (0)
8000+
8001+/* dirty macros for debug print, use with "%.*s" and caution */
8002+#define AuLNPair(qstr) (qstr)->len, (qstr)->name
1facf9fc 8003+
8004+/* ---------------------------------------------------------------------- */
8005+
dece6358 8006+struct dentry;
1facf9fc 8007+#ifdef CONFIG_AUFS_DEBUG
c1595e42 8008+extern struct mutex au_dbg_mtx;
1facf9fc 8009+extern char *au_plevel;
8010+struct au_nhash;
8011+void au_dpri_whlist(struct au_nhash *whlist);
8012+struct au_vdir;
8013+void au_dpri_vdir(struct au_vdir *vdir);
dece6358 8014+struct inode;
1facf9fc 8015+void au_dpri_inode(struct inode *inode);
2cbb1c4b 8016+void au_dpri_dalias(struct inode *inode);
1facf9fc 8017+void au_dpri_dentry(struct dentry *dentry);
dece6358 8018+struct file;
1facf9fc 8019+void au_dpri_file(struct file *filp);
dece6358 8020+struct super_block;
1facf9fc 8021+void au_dpri_sb(struct super_block *sb);
8022+
027c5e7a
AM
8023+#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
8024+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
1facf9fc 8025+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
1facf9fc 8026+void au_dbg_verify_kthread(void);
8027+
8028+int __init au_debug_init(void);
7e9cd9fe 8029+
1facf9fc 8030+#define AuDbgWhlist(w) do { \
c1595e42 8031+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8032+ AuDbg(#w "\n"); \
8033+ au_dpri_whlist(w); \
c1595e42 8034+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8035+} while (0)
8036+
8037+#define AuDbgVdir(v) do { \
c1595e42 8038+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8039+ AuDbg(#v "\n"); \
8040+ au_dpri_vdir(v); \
c1595e42 8041+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8042+} while (0)
8043+
8044+#define AuDbgInode(i) do { \
c1595e42 8045+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8046+ AuDbg(#i "\n"); \
8047+ au_dpri_inode(i); \
c1595e42 8048+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8049+} while (0)
8050+
2cbb1c4b 8051+#define AuDbgDAlias(i) do { \
c1595e42 8052+ mutex_lock(&au_dbg_mtx); \
2cbb1c4b
JR
8053+ AuDbg(#i "\n"); \
8054+ au_dpri_dalias(i); \
c1595e42 8055+ mutex_unlock(&au_dbg_mtx); \
2cbb1c4b
JR
8056+} while (0)
8057+
1facf9fc 8058+#define AuDbgDentry(d) do { \
c1595e42 8059+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8060+ AuDbg(#d "\n"); \
8061+ au_dpri_dentry(d); \
c1595e42 8062+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8063+} while (0)
8064+
8065+#define AuDbgFile(f) do { \
c1595e42 8066+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8067+ AuDbg(#f "\n"); \
8068+ au_dpri_file(f); \
c1595e42 8069+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8070+} while (0)
8071+
8072+#define AuDbgSb(sb) do { \
c1595e42 8073+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8074+ AuDbg(#sb "\n"); \
8075+ au_dpri_sb(sb); \
c1595e42 8076+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8077+} while (0)
8078+
4a4d8108
AM
8079+#define AuDbgSym(addr) do { \
8080+ char sym[KSYM_SYMBOL_LEN]; \
8081+ sprint_symbol(sym, (unsigned long)addr); \
8082+ AuDbg("%s\n", sym); \
8083+} while (0)
1facf9fc 8084+#else
027c5e7a 8085+AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
4a4d8108
AM
8086+AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
8087+AuStubVoid(au_dbg_verify_kthread, void)
8088+AuStubInt0(__init au_debug_init, void)
1facf9fc 8089+
1facf9fc 8090+#define AuDbgWhlist(w) do {} while (0)
8091+#define AuDbgVdir(v) do {} while (0)
8092+#define AuDbgInode(i) do {} while (0)
2cbb1c4b 8093+#define AuDbgDAlias(i) do {} while (0)
1facf9fc 8094+#define AuDbgDentry(d) do {} while (0)
8095+#define AuDbgFile(f) do {} while (0)
8096+#define AuDbgSb(sb) do {} while (0)
4a4d8108 8097+#define AuDbgSym(addr) do {} while (0)
1facf9fc 8098+#endif /* CONFIG_AUFS_DEBUG */
8099+
8100+/* ---------------------------------------------------------------------- */
8101+
8102+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
8103+int __init au_sysrq_init(void);
8104+void au_sysrq_fin(void);
8105+
8106+#ifdef CONFIG_HW_CONSOLE
8107+#define au_dbg_blocked() do { \
8108+ WARN_ON(1); \
0c5527e5 8109+ handle_sysrq('w'); \
1facf9fc 8110+} while (0)
8111+#else
4a4d8108 8112+AuStubVoid(au_dbg_blocked, void)
1facf9fc 8113+#endif
8114+
8115+#else
4a4d8108
AM
8116+AuStubInt0(__init au_sysrq_init, void)
8117+AuStubVoid(au_sysrq_fin, void)
8118+AuStubVoid(au_dbg_blocked, void)
1facf9fc 8119+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
8120+
8121+#endif /* __KERNEL__ */
8122+#endif /* __AUFS_DEBUG_H__ */
7f207e10
AM
8123diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
8124--- /usr/share/empty/fs/aufs/dentry.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 8125+++ linux/fs/aufs/dentry.c 2018-08-12 23:43:05.453457863 +0200
062440b3 8126@@ -0,0 +1,1153 @@
cd7a4cd9 8127+// SPDX-License-Identifier: GPL-2.0
1facf9fc 8128+/*
b00004a5 8129+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 8130+ *
8131+ * This program, aufs is free software; you can redistribute it and/or modify
8132+ * it under the terms of the GNU General Public License as published by
8133+ * the Free Software Foundation; either version 2 of the License, or
8134+ * (at your option) any later version.
dece6358
AM
8135+ *
8136+ * This program is distributed in the hope that it will be useful,
8137+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
8138+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8139+ * GNU General Public License for more details.
8140+ *
8141+ * You should have received a copy of the GNU General Public License
523b37e3 8142+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 8143+ */
8144+
8145+/*
8146+ * lookup and dentry operations
8147+ */
8148+
dece6358 8149+#include <linux/namei.h>
1facf9fc 8150+#include "aufs.h"
8151+
1facf9fc 8152+/*
8153+ * returns positive/negative dentry, NULL or an error.
8154+ * NULL means whiteout-ed or not-found.
8155+ */
8156+static struct dentry*
8157+au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
8b6a4947 8158+ aufs_bindex_t bindex, struct au_do_lookup_args *args)
1facf9fc 8159+{
8160+ struct dentry *h_dentry;
2000de60 8161+ struct inode *h_inode;
1facf9fc 8162+ struct au_branch *br;
8163+ int wh_found, opq;
8164+ unsigned char wh_able;
8165+ const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
076b876e
AM
8166+ const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8167+ IGNORE_PERM);
1facf9fc 8168+
1facf9fc 8169+ wh_found = 0;
8170+ br = au_sbr(dentry->d_sb, bindex);
8171+ wh_able = !!au_br_whable(br->br_perm);
8172+ if (wh_able)
8b6a4947 8173+ wh_found = au_wh_test(h_parent, &args->whname, ignore_perm);
1facf9fc 8174+ h_dentry = ERR_PTR(wh_found);
8175+ if (!wh_found)
8176+ goto real_lookup;
8177+ if (unlikely(wh_found < 0))
8178+ goto out;
8179+
8180+ /* We found a whiteout */
5afbbe0d 8181+ /* au_set_dbbot(dentry, bindex); */
1facf9fc 8182+ au_set_dbwh(dentry, bindex);
8183+ if (!allow_neg)
8184+ return NULL; /* success */
8185+
4f0767ce 8186+real_lookup:
076b876e 8187+ if (!ignore_perm)
8b6a4947 8188+ h_dentry = vfsub_lkup_one(args->name, h_parent);
076b876e 8189+ else
8b6a4947 8190+ h_dentry = au_sio_lkup_one(args->name, h_parent);
2000de60
JR
8191+ if (IS_ERR(h_dentry)) {
8192+ if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8193+ && !allow_neg)
8194+ h_dentry = NULL;
1facf9fc 8195+ goto out;
2000de60 8196+ }
1facf9fc 8197+
5527c038
JR
8198+ h_inode = d_inode(h_dentry);
8199+ if (d_is_negative(h_dentry)) {
1facf9fc 8200+ if (!allow_neg)
8201+ goto out_neg;
8202+ } else if (wh_found
8203+ || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8204+ goto out_neg;
8b6a4947
AM
8205+ else if (au_ftest_lkup(args->flags, DIRREN)
8206+ /* && h_inode */
8207+ && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8208+ AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8209+ (unsigned long long)h_inode->i_ino);
8210+ goto out_neg;
8211+ }
1facf9fc 8212+
5afbbe0d
AM
8213+ if (au_dbbot(dentry) <= bindex)
8214+ au_set_dbbot(dentry, bindex);
8215+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8216+ au_set_dbtop(dentry, bindex);
1facf9fc 8217+ au_set_h_dptr(dentry, bindex, h_dentry);
8218+
2000de60
JR
8219+ if (!d_is_dir(h_dentry)
8220+ || !wh_able
5527c038 8221+ || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
1facf9fc 8222+ goto out; /* success */
8223+
be118d29 8224+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
076b876e 8225+ opq = au_diropq_test(h_dentry);
3c1bdaff 8226+ inode_unlock_shared(h_inode);
1facf9fc 8227+ if (opq > 0)
8228+ au_set_dbdiropq(dentry, bindex);
8229+ else if (unlikely(opq < 0)) {
8230+ au_set_h_dptr(dentry, bindex, NULL);
8231+ h_dentry = ERR_PTR(opq);
8232+ }
8233+ goto out;
8234+
4f0767ce 8235+out_neg:
1facf9fc 8236+ dput(h_dentry);
8237+ h_dentry = NULL;
4f0767ce 8238+out:
1facf9fc 8239+ return h_dentry;
8240+}
8241+
dece6358
AM
8242+static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8243+{
8244+ if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8245+ && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8246+ return -EPERM;
8247+ return 0;
8248+}
8249+
1facf9fc 8250+/*
8251+ * returns the number of lower positive dentries,
8252+ * otherwise an error.
8253+ * can be called at unlinking with @type is zero.
8254+ */
5afbbe0d
AM
8255+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8256+ unsigned int flags)
1facf9fc 8257+{
8258+ int npositive, err;
8259+ aufs_bindex_t bindex, btail, bdiropq;
8b6a4947 8260+ unsigned char isdir, dirperm1, dirren;
1facf9fc 8261+ struct au_do_lookup_args args = {
8b6a4947
AM
8262+ .flags = flags,
8263+ .name = &dentry->d_name
1facf9fc 8264+ };
1facf9fc 8265+ struct dentry *parent;
076b876e 8266+ struct super_block *sb;
1facf9fc 8267+
076b876e 8268+ sb = dentry->d_sb;
8b6a4947 8269+ err = au_test_shwh(sb, args.name);
dece6358 8270+ if (unlikely(err))
1facf9fc 8271+ goto out;
8272+
8b6a4947 8273+ err = au_wh_name_alloc(&args.whname, args.name);
1facf9fc 8274+ if (unlikely(err))
8275+ goto out;
8276+
2000de60 8277+ isdir = !!d_is_dir(dentry);
076b876e 8278+ dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8b6a4947
AM
8279+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8280+ if (dirren)
8281+ au_fset_lkup(args.flags, DIRREN);
1facf9fc 8282+
8283+ npositive = 0;
4a4d8108 8284+ parent = dget_parent(dentry);
1facf9fc 8285+ btail = au_dbtaildir(parent);
5afbbe0d 8286+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 8287+ struct dentry *h_parent, *h_dentry;
8288+ struct inode *h_inode, *h_dir;
8b6a4947 8289+ struct au_branch *br;
1facf9fc 8290+
8291+ h_dentry = au_h_dptr(dentry, bindex);
8292+ if (h_dentry) {
5527c038 8293+ if (d_is_positive(h_dentry))
1facf9fc 8294+ npositive++;
5afbbe0d 8295+ break;
1facf9fc 8296+ }
8297+ h_parent = au_h_dptr(parent, bindex);
2000de60 8298+ if (!h_parent || !d_is_dir(h_parent))
1facf9fc 8299+ continue;
8300+
8b6a4947
AM
8301+ if (dirren) {
8302+ /* if the inum matches, then use the prepared name */
8303+ err = au_dr_lkup_name(&args, bindex);
8304+ if (unlikely(err))
8305+ goto out_parent;
8306+ }
8307+
5527c038 8308+ h_dir = d_inode(h_parent);
be118d29 8309+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8b6a4947 8310+ h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
3c1bdaff 8311+ inode_unlock_shared(h_dir);
1facf9fc 8312+ err = PTR_ERR(h_dentry);
8313+ if (IS_ERR(h_dentry))
4a4d8108 8314+ goto out_parent;
2000de60
JR
8315+ if (h_dentry)
8316+ au_fclr_lkup(args.flags, ALLOW_NEG);
076b876e
AM
8317+ if (dirperm1)
8318+ au_fset_lkup(args.flags, IGNORE_PERM);
1facf9fc 8319+
79b8bda9 8320+ if (au_dbwh(dentry) == bindex)
1facf9fc 8321+ break;
8322+ if (!h_dentry)
8323+ continue;
5527c038 8324+ if (d_is_negative(h_dentry))
1facf9fc 8325+ continue;
5527c038 8326+ h_inode = d_inode(h_dentry);
1facf9fc 8327+ npositive++;
8328+ if (!args.type)
8329+ args.type = h_inode->i_mode & S_IFMT;
8330+ if (args.type != S_IFDIR)
8331+ break;
8332+ else if (isdir) {
8333+ /* the type of lower may be different */
8334+ bdiropq = au_dbdiropq(dentry);
8335+ if (bdiropq >= 0 && bdiropq <= bindex)
8336+ break;
8337+ }
8b6a4947
AM
8338+ br = au_sbr(sb, bindex);
8339+ if (dirren
8340+ && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8341+ /*add_ent*/NULL)) {
8342+ /* prepare next name to lookup */
8343+ err = au_dr_lkup(&args, dentry, bindex);
8344+ if (unlikely(err))
8345+ goto out_parent;
8346+ }
1facf9fc 8347+ }
8348+
8349+ if (npositive) {
8350+ AuLabel(positive);
5afbbe0d 8351+ au_update_dbtop(dentry);
1facf9fc 8352+ }
8353+ err = npositive;
076b876e 8354+ if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
5afbbe0d 8355+ && au_dbtop(dentry) < 0)) {
1facf9fc 8356+ err = -EIO;
523b37e3
AM
8357+ AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8358+ dentry, err);
027c5e7a 8359+ }
1facf9fc 8360+
4f0767ce 8361+out_parent:
4a4d8108 8362+ dput(parent);
8b6a4947
AM
8363+ kfree(args.whname.name);
8364+ if (dirren)
8365+ au_dr_lkup_fin(&args);
4f0767ce 8366+out:
1facf9fc 8367+ return err;
8368+}
8369+
076b876e 8370+struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
1facf9fc 8371+{
8372+ struct dentry *dentry;
8373+ int wkq_err;
8374+
5527c038 8375+ if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
b4510431 8376+ dentry = vfsub_lkup_one(name, parent);
1facf9fc 8377+ else {
b4510431
AM
8378+ struct vfsub_lkup_one_args args = {
8379+ .errp = &dentry,
8380+ .name = name,
8381+ .parent = parent
1facf9fc 8382+ };
8383+
b4510431 8384+ wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
1facf9fc 8385+ if (unlikely(wkq_err))
8386+ dentry = ERR_PTR(wkq_err);
8387+ }
8388+
8389+ return dentry;
8390+}
8391+
8392+/*
8393+ * lookup @dentry on @bindex which should be negative.
8394+ */
86dc4139 8395+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
1facf9fc 8396+{
8397+ int err;
8398+ struct dentry *parent, *h_parent, *h_dentry;
86dc4139 8399+ struct au_branch *br;
1facf9fc 8400+
1facf9fc 8401+ parent = dget_parent(dentry);
8402+ h_parent = au_h_dptr(parent, bindex);
86dc4139
AM
8403+ br = au_sbr(dentry->d_sb, bindex);
8404+ if (wh)
8405+ h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
8406+ else
076b876e 8407+ h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
1facf9fc 8408+ err = PTR_ERR(h_dentry);
8409+ if (IS_ERR(h_dentry))
8410+ goto out;
5527c038 8411+ if (unlikely(d_is_positive(h_dentry))) {
1facf9fc 8412+ err = -EIO;
523b37e3 8413+ AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
1facf9fc 8414+ dput(h_dentry);
8415+ goto out;
8416+ }
8417+
4a4d8108 8418+ err = 0;
5afbbe0d
AM
8419+ if (bindex < au_dbtop(dentry))
8420+ au_set_dbtop(dentry, bindex);
8421+ if (au_dbbot(dentry) < bindex)
8422+ au_set_dbbot(dentry, bindex);
1facf9fc 8423+ au_set_h_dptr(dentry, bindex, h_dentry);
1facf9fc 8424+
4f0767ce 8425+out:
1facf9fc 8426+ dput(parent);
8427+ return err;
8428+}
8429+
8430+/* ---------------------------------------------------------------------- */
8431+
8432+/* subset of struct inode */
8433+struct au_iattr {
8434+ unsigned long i_ino;
8435+ /* unsigned int i_nlink; */
0c3ec466
AM
8436+ kuid_t i_uid;
8437+ kgid_t i_gid;
1facf9fc 8438+ u64 i_version;
8439+/*
8440+ loff_t i_size;
8441+ blkcnt_t i_blocks;
8442+*/
8443+ umode_t i_mode;
8444+};
8445+
8446+static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8447+{
8448+ ia->i_ino = h_inode->i_ino;
8449+ /* ia->i_nlink = h_inode->i_nlink; */
8450+ ia->i_uid = h_inode->i_uid;
8451+ ia->i_gid = h_inode->i_gid;
be118d29 8452+ ia->i_version = inode_query_iversion(h_inode);
1facf9fc 8453+/*
8454+ ia->i_size = h_inode->i_size;
8455+ ia->i_blocks = h_inode->i_blocks;
8456+*/
8457+ ia->i_mode = (h_inode->i_mode & S_IFMT);
8458+}
8459+
8460+static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8461+{
8462+ return ia->i_ino != h_inode->i_ino
8463+ /* || ia->i_nlink != h_inode->i_nlink */
0c3ec466 8464+ || !uid_eq(ia->i_uid, h_inode->i_uid)
2dfbb274 8465+ || !gid_eq(ia->i_gid, h_inode->i_gid)
be118d29 8466+ || !inode_eq_iversion(h_inode, ia->i_version)
1facf9fc 8467+/*
8468+ || ia->i_size != h_inode->i_size
8469+ || ia->i_blocks != h_inode->i_blocks
8470+*/
8471+ || ia->i_mode != (h_inode->i_mode & S_IFMT);
8472+}
8473+
8474+static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8475+ struct au_branch *br)
8476+{
8477+ int err;
8478+ struct au_iattr ia;
8479+ struct inode *h_inode;
8480+ struct dentry *h_d;
8481+ struct super_block *h_sb;
8482+
8483+ err = 0;
8484+ memset(&ia, -1, sizeof(ia));
8485+ h_sb = h_dentry->d_sb;
5527c038
JR
8486+ h_inode = NULL;
8487+ if (d_is_positive(h_dentry)) {
8488+ h_inode = d_inode(h_dentry);
1facf9fc 8489+ au_iattr_save(&ia, h_inode);
5527c038 8490+ } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
1facf9fc 8491+ /* nfs d_revalidate may return 0 for negative dentry */
8492+ /* fuse d_revalidate always return 0 for negative dentry */
8493+ goto out;
8494+
8495+ /* main purpose is namei.c:cached_lookup() and d_revalidate */
b4510431 8496+ h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
1facf9fc 8497+ err = PTR_ERR(h_d);
8498+ if (IS_ERR(h_d))
8499+ goto out;
8500+
8501+ err = 0;
8502+ if (unlikely(h_d != h_dentry
5527c038 8503+ || d_inode(h_d) != h_inode
1facf9fc 8504+ || (h_inode && au_iattr_test(&ia, h_inode))))
8505+ err = au_busy_or_stale();
8506+ dput(h_d);
8507+
4f0767ce 8508+out:
1facf9fc 8509+ AuTraceErr(err);
8510+ return err;
8511+}
8512+
8513+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8514+ struct dentry *h_parent, struct au_branch *br)
8515+{
8516+ int err;
8517+
8518+ err = 0;
027c5e7a
AM
8519+ if (udba == AuOpt_UDBA_REVAL
8520+ && !au_test_fs_remote(h_dentry->d_sb)) {
1facf9fc 8521+ IMustLock(h_dir);
5527c038 8522+ err = (d_inode(h_dentry->d_parent) != h_dir);
027c5e7a 8523+ } else if (udba != AuOpt_UDBA_NONE)
1facf9fc 8524+ err = au_h_verify_dentry(h_dentry, h_parent, br);
8525+
8526+ return err;
8527+}
8528+
8529+/* ---------------------------------------------------------------------- */
8530+
027c5e7a 8531+static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
1facf9fc 8532+{
027c5e7a 8533+ int err;
5afbbe0d 8534+ aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
027c5e7a
AM
8535+ struct au_hdentry tmp, *p, *q;
8536+ struct au_dinfo *dinfo;
8537+ struct super_block *sb;
1facf9fc 8538+
027c5e7a 8539+ DiMustWriteLock(dentry);
1308ab2a 8540+
027c5e7a
AM
8541+ sb = dentry->d_sb;
8542+ dinfo = au_di(dentry);
5afbbe0d 8543+ bbot = dinfo->di_bbot;
1facf9fc 8544+ bwh = dinfo->di_bwh;
8545+ bdiropq = dinfo->di_bdiropq;
5afbbe0d
AM
8546+ bindex = dinfo->di_btop;
8547+ p = au_hdentry(dinfo, bindex);
8548+ for (; bindex <= bbot; bindex++, p++) {
027c5e7a 8549+ if (!p->hd_dentry)
1facf9fc 8550+ continue;
8551+
027c5e7a
AM
8552+ new_bindex = au_br_index(sb, p->hd_id);
8553+ if (new_bindex == bindex)
1facf9fc 8554+ continue;
1facf9fc 8555+
1facf9fc 8556+ if (dinfo->di_bwh == bindex)
8557+ bwh = new_bindex;
8558+ if (dinfo->di_bdiropq == bindex)
8559+ bdiropq = new_bindex;
8560+ if (new_bindex < 0) {
8561+ au_hdput(p);
8562+ p->hd_dentry = NULL;
8563+ continue;
8564+ }
8565+
8566+ /* swap two lower dentries, and loop again */
5afbbe0d 8567+ q = au_hdentry(dinfo, new_bindex);
1facf9fc 8568+ tmp = *q;
8569+ *q = *p;
8570+ *p = tmp;
8571+ if (tmp.hd_dentry) {
8572+ bindex--;
8573+ p--;
8574+ }
8575+ }
8576+
1facf9fc 8577+ dinfo->di_bwh = -1;
5afbbe0d 8578+ if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
1facf9fc 8579+ dinfo->di_bwh = bwh;
8580+
8581+ dinfo->di_bdiropq = -1;
8582+ if (bdiropq >= 0
5afbbe0d 8583+ && bdiropq <= au_sbbot(sb)
1facf9fc 8584+ && au_sbr_whable(sb, bdiropq))
8585+ dinfo->di_bdiropq = bdiropq;
8586+
027c5e7a 8587+ err = -EIO;
5afbbe0d
AM
8588+ dinfo->di_btop = -1;
8589+ dinfo->di_bbot = -1;
8590+ bbot = au_dbbot(parent);
8591+ bindex = 0;
8592+ p = au_hdentry(dinfo, bindex);
8593+ for (; bindex <= bbot; bindex++, p++)
1facf9fc 8594+ if (p->hd_dentry) {
5afbbe0d 8595+ dinfo->di_btop = bindex;
1facf9fc 8596+ break;
8597+ }
8598+
5afbbe0d
AM
8599+ if (dinfo->di_btop >= 0) {
8600+ bindex = bbot;
8601+ p = au_hdentry(dinfo, bindex);
8602+ for (; bindex >= 0; bindex--, p--)
027c5e7a 8603+ if (p->hd_dentry) {
5afbbe0d 8604+ dinfo->di_bbot = bindex;
027c5e7a
AM
8605+ err = 0;
8606+ break;
8607+ }
8608+ }
8609+
8610+ return err;
1facf9fc 8611+}
8612+
027c5e7a 8613+static void au_do_hide(struct dentry *dentry)
1facf9fc 8614+{
027c5e7a 8615+ struct inode *inode;
1facf9fc 8616+
5527c038
JR
8617+ if (d_really_is_positive(dentry)) {
8618+ inode = d_inode(dentry);
8619+ if (!d_is_dir(dentry)) {
027c5e7a
AM
8620+ if (inode->i_nlink && !d_unhashed(dentry))
8621+ drop_nlink(inode);
8622+ } else {
8623+ clear_nlink(inode);
8624+ /* stop next lookup */
8625+ inode->i_flags |= S_DEAD;
8626+ }
8627+ smp_mb(); /* necessary? */
8628+ }
8629+ d_drop(dentry);
8630+}
1308ab2a 8631+
027c5e7a
AM
8632+static int au_hide_children(struct dentry *parent)
8633+{
8634+ int err, i, j, ndentry;
8635+ struct au_dcsub_pages dpages;
8636+ struct au_dpage *dpage;
8637+ struct dentry *dentry;
1facf9fc 8638+
027c5e7a 8639+ err = au_dpages_init(&dpages, GFP_NOFS);
1facf9fc 8640+ if (unlikely(err))
8641+ goto out;
027c5e7a
AM
8642+ err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8643+ if (unlikely(err))
8644+ goto out_dpages;
1facf9fc 8645+
027c5e7a
AM
8646+ /* in reverse order */
8647+ for (i = dpages.ndpage - 1; i >= 0; i--) {
8648+ dpage = dpages.dpages + i;
8649+ ndentry = dpage->ndentry;
8650+ for (j = ndentry - 1; j >= 0; j--) {
8651+ dentry = dpage->dentries[j];
8652+ if (dentry != parent)
8653+ au_do_hide(dentry);
8654+ }
8655+ }
1facf9fc 8656+
027c5e7a
AM
8657+out_dpages:
8658+ au_dpages_free(&dpages);
4f0767ce 8659+out:
027c5e7a 8660+ return err;
1facf9fc 8661+}
8662+
027c5e7a 8663+static void au_hide(struct dentry *dentry)
1facf9fc 8664+{
027c5e7a 8665+ int err;
1facf9fc 8666+
027c5e7a 8667+ AuDbgDentry(dentry);
2000de60 8668+ if (d_is_dir(dentry)) {
027c5e7a
AM
8669+ /* shrink_dcache_parent(dentry); */
8670+ err = au_hide_children(dentry);
8671+ if (unlikely(err))
523b37e3
AM
8672+ AuIOErr("%pd, failed hiding children, ignored %d\n",
8673+ dentry, err);
027c5e7a
AM
8674+ }
8675+ au_do_hide(dentry);
8676+}
1facf9fc 8677+
027c5e7a
AM
8678+/*
8679+ * By adding a dirty branch, a cached dentry may be affected in various ways.
8680+ *
8681+ * a dirty branch is added
8682+ * - on the top of layers
8683+ * - in the middle of layers
8684+ * - to the bottom of layers
8685+ *
8686+ * on the added branch there exists
8687+ * - a whiteout
8688+ * - a diropq
8689+ * - a same named entry
8690+ * + exist
8691+ * * negative --> positive
8692+ * * positive --> positive
8693+ * - type is unchanged
8694+ * - type is changed
8695+ * + doesn't exist
8696+ * * negative --> negative
8697+ * * positive --> negative (rejected by au_br_del() for non-dir case)
8698+ * - none
8699+ */
8700+static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8701+ struct au_dinfo *tmp)
8702+{
8703+ int err;
5afbbe0d 8704+ aufs_bindex_t bindex, bbot;
027c5e7a
AM
8705+ struct {
8706+ struct dentry *dentry;
8707+ struct inode *inode;
8708+ mode_t mode;
be52b249
AM
8709+ } orig_h, tmp_h = {
8710+ .dentry = NULL
8711+ };
027c5e7a
AM
8712+ struct au_hdentry *hd;
8713+ struct inode *inode, *h_inode;
8714+ struct dentry *h_dentry;
8715+
8716+ err = 0;
5afbbe0d 8717+ AuDebugOn(dinfo->di_btop < 0);
027c5e7a 8718+ orig_h.mode = 0;
5afbbe0d 8719+ orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
5527c038
JR
8720+ orig_h.inode = NULL;
8721+ if (d_is_positive(orig_h.dentry)) {
8722+ orig_h.inode = d_inode(orig_h.dentry);
027c5e7a 8723+ orig_h.mode = orig_h.inode->i_mode & S_IFMT;
5527c038 8724+ }
5afbbe0d
AM
8725+ if (tmp->di_btop >= 0) {
8726+ tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
5527c038
JR
8727+ if (d_is_positive(tmp_h.dentry)) {
8728+ tmp_h.inode = d_inode(tmp_h.dentry);
027c5e7a 8729+ tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
5527c038 8730+ }
027c5e7a
AM
8731+ }
8732+
5527c038
JR
8733+ inode = NULL;
8734+ if (d_really_is_positive(dentry))
8735+ inode = d_inode(dentry);
027c5e7a
AM
8736+ if (!orig_h.inode) {
8737+ AuDbg("nagative originally\n");
8738+ if (inode) {
8739+ au_hide(dentry);
8740+ goto out;
8741+ }
8742+ AuDebugOn(inode);
5afbbe0d 8743+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
027c5e7a
AM
8744+ AuDebugOn(dinfo->di_bdiropq != -1);
8745+
8746+ if (!tmp_h.inode) {
8747+ AuDbg("negative --> negative\n");
8748+ /* should have only one negative lower */
5afbbe0d
AM
8749+ if (tmp->di_btop >= 0
8750+ && tmp->di_btop < dinfo->di_btop) {
8751+ AuDebugOn(tmp->di_btop != tmp->di_bbot);
8752+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8753+ au_set_h_dptr(dentry, dinfo->di_btop, NULL);
027c5e7a 8754+ au_di_cp(dinfo, tmp);
5afbbe0d
AM
8755+ hd = au_hdentry(tmp, tmp->di_btop);
8756+ au_set_h_dptr(dentry, tmp->di_btop,
027c5e7a
AM
8757+ dget(hd->hd_dentry));
8758+ }
8759+ au_dbg_verify_dinode(dentry);
8760+ } else {
8761+ AuDbg("negative --> positive\n");
8762+ /*
8763+ * similar to the behaviour of creating with bypassing
8764+ * aufs.
8765+ * unhash it in order to force an error in the
8766+ * succeeding create operation.
8767+ * we should not set S_DEAD here.
8768+ */
8769+ d_drop(dentry);
8770+ /* au_di_swap(tmp, dinfo); */
8771+ au_dbg_verify_dinode(dentry);
8772+ }
8773+ } else {
8774+ AuDbg("positive originally\n");
8775+ /* inode may be NULL */
8776+ AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8777+ if (!tmp_h.inode) {
8778+ AuDbg("positive --> negative\n");
8779+ /* or bypassing aufs */
8780+ au_hide(dentry);
5afbbe0d 8781+ if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
027c5e7a
AM
8782+ dinfo->di_bwh = tmp->di_bwh;
8783+ if (inode)
8784+ err = au_refresh_hinode_self(inode);
8785+ au_dbg_verify_dinode(dentry);
8786+ } else if (orig_h.mode == tmp_h.mode) {
8787+ AuDbg("positive --> positive, same type\n");
8788+ if (!S_ISDIR(orig_h.mode)
5afbbe0d 8789+ && dinfo->di_btop > tmp->di_btop) {
027c5e7a
AM
8790+ /*
8791+ * similar to the behaviour of removing and
8792+ * creating.
8793+ */
8794+ au_hide(dentry);
8795+ if (inode)
8796+ err = au_refresh_hinode_self(inode);
8797+ au_dbg_verify_dinode(dentry);
8798+ } else {
8799+ /* fill empty slots */
5afbbe0d
AM
8800+ if (dinfo->di_btop > tmp->di_btop)
8801+ dinfo->di_btop = tmp->di_btop;
8802+ if (dinfo->di_bbot < tmp->di_bbot)
8803+ dinfo->di_bbot = tmp->di_bbot;
027c5e7a
AM
8804+ dinfo->di_bwh = tmp->di_bwh;
8805+ dinfo->di_bdiropq = tmp->di_bdiropq;
5afbbe0d
AM
8806+ bbot = dinfo->di_bbot;
8807+ bindex = tmp->di_btop;
8808+ hd = au_hdentry(tmp, bindex);
8809+ for (; bindex <= bbot; bindex++, hd++) {
027c5e7a
AM
8810+ if (au_h_dptr(dentry, bindex))
8811+ continue;
5afbbe0d 8812+ h_dentry = hd->hd_dentry;
027c5e7a
AM
8813+ if (!h_dentry)
8814+ continue;
5527c038
JR
8815+ AuDebugOn(d_is_negative(h_dentry));
8816+ h_inode = d_inode(h_dentry);
027c5e7a
AM
8817+ AuDebugOn(orig_h.mode
8818+ != (h_inode->i_mode
8819+ & S_IFMT));
8820+ au_set_h_dptr(dentry, bindex,
8821+ dget(h_dentry));
8822+ }
5afbbe0d
AM
8823+ if (inode)
8824+ err = au_refresh_hinode(inode, dentry);
027c5e7a
AM
8825+ au_dbg_verify_dinode(dentry);
8826+ }
8827+ } else {
8828+ AuDbg("positive --> positive, different type\n");
8829+ /* similar to the behaviour of removing and creating */
8830+ au_hide(dentry);
8831+ if (inode)
8832+ err = au_refresh_hinode_self(inode);
8833+ au_dbg_verify_dinode(dentry);
8834+ }
8835+ }
8836+
8837+out:
8838+ return err;
8839+}
8840+
79b8bda9
AM
8841+void au_refresh_dop(struct dentry *dentry, int force_reval)
8842+{
8843+ const struct dentry_operations *dop
8844+ = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8845+ static const unsigned int mask
8846+ = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8847+
8848+ BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8849+
8850+ if (dentry->d_op == dop)
8851+ return;
8852+
8853+ AuDbg("%pd\n", dentry);
8854+ spin_lock(&dentry->d_lock);
8855+ if (dop == &aufs_dop)
8856+ dentry->d_flags |= mask;
8857+ else
8858+ dentry->d_flags &= ~mask;
8859+ dentry->d_op = dop;
8860+ spin_unlock(&dentry->d_lock);
8861+}
8862+
027c5e7a
AM
8863+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8864+{
e2f27e51 8865+ int err, ebrange, nbr;
027c5e7a
AM
8866+ unsigned int sigen;
8867+ struct au_dinfo *dinfo, *tmp;
8868+ struct super_block *sb;
8869+ struct inode *inode;
8870+
8871+ DiMustWriteLock(dentry);
8872+ AuDebugOn(IS_ROOT(dentry));
5527c038 8873+ AuDebugOn(d_really_is_negative(parent));
027c5e7a
AM
8874+
8875+ sb = dentry->d_sb;
027c5e7a
AM
8876+ sigen = au_sigen(sb);
8877+ err = au_digen_test(parent, sigen);
8878+ if (unlikely(err))
8879+ goto out;
8880+
e2f27e51 8881+ nbr = au_sbbot(sb) + 1;
027c5e7a 8882+ dinfo = au_di(dentry);
e2f27e51 8883+ err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
027c5e7a
AM
8884+ if (unlikely(err))
8885+ goto out;
8886+ ebrange = au_dbrange_test(dentry);
8887+ if (!ebrange)
8888+ ebrange = au_do_refresh_hdentry(dentry, parent);
8889+
38d290e6 8890+ if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
5afbbe0d 8891+ AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
5527c038
JR
8892+ if (d_really_is_positive(dentry)) {
8893+ inode = d_inode(dentry);
027c5e7a 8894+ err = au_refresh_hinode_self(inode);
5527c038 8895+ }
027c5e7a
AM
8896+ au_dbg_verify_dinode(dentry);
8897+ if (!err)
8898+ goto out_dgen; /* success */
8899+ goto out;
8900+ }
8901+
8902+ /* temporary dinfo */
8903+ AuDbgDentry(dentry);
8904+ err = -ENOMEM;
8905+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8906+ if (unlikely(!tmp))
8907+ goto out;
8908+ au_di_swap(tmp, dinfo);
8909+ /* returns the number of positive dentries */
8910+ /*
8911+ * if current working dir is removed, it returns an error.
8912+ * but the dentry is legal.
8913+ */
5afbbe0d 8914+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
027c5e7a
AM
8915+ AuDbgDentry(dentry);
8916+ au_di_swap(tmp, dinfo);
8917+ if (err == -ENOENT)
8918+ err = 0;
8919+ if (err >= 0) {
8920+ /* compare/refresh by dinfo */
8921+ AuDbgDentry(dentry);
8922+ err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8923+ au_dbg_verify_dinode(dentry);
8924+ AuTraceErr(err);
8925+ }
e2f27e51 8926+ au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
027c5e7a
AM
8927+ au_rw_write_unlock(&tmp->di_rwsem);
8928+ au_di_free(tmp);
8929+ if (unlikely(err))
8930+ goto out;
8931+
8932+out_dgen:
8933+ au_update_digen(dentry);
8934+out:
8935+ if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
523b37e3 8936+ AuIOErr("failed refreshing %pd, %d\n", dentry, err);
027c5e7a
AM
8937+ AuDbgDentry(dentry);
8938+ }
8939+ AuTraceErr(err);
8940+ return err;
8941+}
8942+
b4510431
AM
8943+static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8944+ struct dentry *dentry, aufs_bindex_t bindex)
027c5e7a
AM
8945+{
8946+ int err, valid;
027c5e7a
AM
8947+
8948+ err = 0;
8949+ if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8950+ goto out;
027c5e7a
AM
8951+
8952+ AuDbg("b%d\n", bindex);
b4510431
AM
8953+ /*
8954+ * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8955+ * due to whiteout and branch permission.
8956+ */
8957+ flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8958+ | LOOKUP_FOLLOW | LOOKUP_EXCL);
8959+ /* it may return tri-state */
8960+ valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
1facf9fc 8961+
8962+ if (unlikely(valid < 0))
8963+ err = valid;
8964+ else if (!valid)
8965+ err = -EINVAL;
8966+
4f0767ce 8967+out:
1facf9fc 8968+ AuTraceErr(err);
8969+ return err;
8970+}
8971+
8972+/* todo: remove this */
8973+static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8b6a4947 8974+ unsigned int flags, int do_udba, int dirren)
1facf9fc 8975+{
8976+ int err;
8977+ umode_t mode, h_mode;
5afbbe0d 8978+ aufs_bindex_t bindex, btail, btop, ibs, ibe;
38d290e6 8979+ unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
4a4d8108 8980+ struct inode *h_inode, *h_cached_inode;
1facf9fc 8981+ struct dentry *h_dentry;
8982+ struct qstr *name, *h_name;
8983+
8984+ err = 0;
8985+ plus = 0;
8986+ mode = 0;
1facf9fc 8987+ ibs = -1;
8988+ ibe = -1;
8989+ unhashed = !!d_unhashed(dentry);
8990+ is_root = !!IS_ROOT(dentry);
8991+ name = &dentry->d_name;
38d290e6 8992+ tmpfile = au_di(dentry)->di_tmpfile;
1facf9fc 8993+
8994+ /*
7f207e10
AM
8995+ * Theoretically, REVAL test should be unnecessary in case of
8996+ * {FS,I}NOTIFY.
8997+ * But {fs,i}notify doesn't fire some necessary events,
1facf9fc 8998+ * IN_ATTRIB for atime/nlink/pageio
1facf9fc 8999+ * Let's do REVAL test too.
9000+ */
9001+ if (do_udba && inode) {
9002+ mode = (inode->i_mode & S_IFMT);
9003+ plus = (inode->i_nlink > 0);
5afbbe0d
AM
9004+ ibs = au_ibtop(inode);
9005+ ibe = au_ibbot(inode);
1facf9fc 9006+ }
9007+
5afbbe0d
AM
9008+ btop = au_dbtop(dentry);
9009+ btail = btop;
1facf9fc 9010+ if (inode && S_ISDIR(inode->i_mode))
9011+ btail = au_dbtaildir(dentry);
5afbbe0d 9012+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 9013+ h_dentry = au_h_dptr(dentry, bindex);
9014+ if (!h_dentry)
9015+ continue;
9016+
523b37e3
AM
9017+ AuDbg("b%d, %pd\n", bindex, h_dentry);
9018+ h_nfs = !!au_test_nfs(h_dentry->d_sb);
027c5e7a 9019+ spin_lock(&h_dentry->d_lock);
1facf9fc 9020+ h_name = &h_dentry->d_name;
9021+ if (unlikely(do_udba
9022+ && !is_root
523b37e3
AM
9023+ && ((!h_nfs
9024+ && (unhashed != !!d_unhashed(h_dentry)
8b6a4947 9025+ || (!tmpfile && !dirren
38d290e6
JR
9026+ && !au_qstreq(name, h_name))
9027+ ))
523b37e3
AM
9028+ || (h_nfs
9029+ && !(flags & LOOKUP_OPEN)
9030+ && (h_dentry->d_flags
9031+ & DCACHE_NFSFS_RENAMED)))
1facf9fc 9032+ )) {
38d290e6
JR
9033+ int h_unhashed;
9034+
9035+ h_unhashed = d_unhashed(h_dentry);
027c5e7a 9036+ spin_unlock(&h_dentry->d_lock);
38d290e6
JR
9037+ AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
9038+ unhashed, h_unhashed, dentry, h_dentry);
1facf9fc 9039+ goto err;
9040+ }
027c5e7a 9041+ spin_unlock(&h_dentry->d_lock);
1facf9fc 9042+
b4510431 9043+ err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
1facf9fc 9044+ if (unlikely(err))
9045+ /* do not goto err, to keep the errno */
9046+ break;
9047+
9048+ /* todo: plink too? */
9049+ if (!do_udba)
9050+ continue;
9051+
9052+ /* UDBA tests */
5527c038 9053+ if (unlikely(!!inode != d_is_positive(h_dentry)))
1facf9fc 9054+ goto err;
9055+
5527c038
JR
9056+ h_inode = NULL;
9057+ if (d_is_positive(h_dentry))
9058+ h_inode = d_inode(h_dentry);
1facf9fc 9059+ h_plus = plus;
9060+ h_mode = mode;
9061+ h_cached_inode = h_inode;
9062+ if (h_inode) {
9063+ h_mode = (h_inode->i_mode & S_IFMT);
9064+ h_plus = (h_inode->i_nlink > 0);
9065+ }
9066+ if (inode && ibs <= bindex && bindex <= ibe)
9067+ h_cached_inode = au_h_iptr(inode, bindex);
9068+
523b37e3 9069+ if (!h_nfs) {
38d290e6 9070+ if (unlikely(plus != h_plus && !tmpfile))
523b37e3
AM
9071+ goto err;
9072+ } else {
9073+ if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
9074+ && !is_root
9075+ && !IS_ROOT(h_dentry)
9076+ && unhashed != d_unhashed(h_dentry)))
9077+ goto err;
9078+ }
9079+ if (unlikely(mode != h_mode
1facf9fc 9080+ || h_cached_inode != h_inode))
9081+ goto err;
9082+ continue;
9083+
f6b6e03d 9084+err:
1facf9fc 9085+ err = -EINVAL;
9086+ break;
9087+ }
9088+
523b37e3 9089+ AuTraceErr(err);
1facf9fc 9090+ return err;
9091+}
9092+
027c5e7a 9093+/* todo: consolidate with do_refresh() and au_reval_for_attr() */
1facf9fc 9094+static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
9095+{
9096+ int err;
9097+ struct dentry *parent;
1facf9fc 9098+
027c5e7a 9099+ if (!au_digen_test(dentry, sigen))
1facf9fc 9100+ return 0;
9101+
9102+ parent = dget_parent(dentry);
9103+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 9104+ AuDebugOn(au_digen_test(parent, sigen));
1facf9fc 9105+ au_dbg_verify_gen(parent, sigen);
027c5e7a 9106+ err = au_refresh_dentry(dentry, parent);
1facf9fc 9107+ di_read_unlock(parent, AuLock_IR);
9108+ dput(parent);
027c5e7a 9109+ AuTraceErr(err);
1facf9fc 9110+ return err;
9111+}
9112+
9113+int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
9114+{
9115+ int err;
9116+ struct dentry *d, *parent;
1facf9fc 9117+
027c5e7a 9118+ if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
1facf9fc 9119+ return simple_reval_dpath(dentry, sigen);
9120+
9121+ /* slow loop, keep it simple and stupid */
9122+ /* cf: au_cpup_dirs() */
9123+ err = 0;
9124+ parent = NULL;
027c5e7a 9125+ while (au_digen_test(dentry, sigen)) {
1facf9fc 9126+ d = dentry;
9127+ while (1) {
9128+ dput(parent);
9129+ parent = dget_parent(d);
027c5e7a 9130+ if (!au_digen_test(parent, sigen))
1facf9fc 9131+ break;
9132+ d = parent;
9133+ }
9134+
1facf9fc 9135+ if (d != dentry)
027c5e7a 9136+ di_write_lock_child2(d);
1facf9fc 9137+
9138+ /* someone might update our dentry while we were sleeping */
027c5e7a
AM
9139+ if (au_digen_test(d, sigen)) {
9140+ /*
9141+ * todo: consolidate with simple_reval_dpath(),
9142+ * do_refresh() and au_reval_for_attr().
9143+ */
1facf9fc 9144+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 9145+ err = au_refresh_dentry(d, parent);
1facf9fc 9146+ di_read_unlock(parent, AuLock_IR);
9147+ }
9148+
9149+ if (d != dentry)
9150+ di_write_unlock(d);
9151+ dput(parent);
9152+ if (unlikely(err))
9153+ break;
9154+ }
9155+
9156+ return err;
9157+}
9158+
9159+/*
9160+ * if valid returns 1, otherwise 0.
9161+ */
b4510431 9162+static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
1facf9fc 9163+{
9164+ int valid, err;
9165+ unsigned int sigen;
8b6a4947 9166+ unsigned char do_udba, dirren;
1facf9fc 9167+ struct super_block *sb;
9168+ struct inode *inode;
9169+
027c5e7a 9170+ /* todo: support rcu-walk? */
b4510431 9171+ if (flags & LOOKUP_RCU)
027c5e7a
AM
9172+ return -ECHILD;
9173+
9174+ valid = 0;
9175+ if (unlikely(!au_di(dentry)))
9176+ goto out;
9177+
e49829fe 9178+ valid = 1;
1facf9fc 9179+ sb = dentry->d_sb;
e49829fe
JR
9180+ /*
9181+ * todo: very ugly
9182+ * i_mutex of parent dir may be held,
9183+ * but we should not return 'invalid' due to busy.
9184+ */
9185+ err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9186+ if (unlikely(err)) {
9187+ valid = err;
027c5e7a 9188+ AuTraceErr(err);
e49829fe
JR
9189+ goto out;
9190+ }
5527c038
JR
9191+ inode = NULL;
9192+ if (d_really_is_positive(dentry))
9193+ inode = d_inode(dentry);
5afbbe0d 9194+ if (unlikely(inode && au_is_bad_inode(inode))) {
c1595e42
JR
9195+ err = -EINVAL;
9196+ AuTraceErr(err);
9197+ goto out_dgrade;
9198+ }
027c5e7a
AM
9199+ if (unlikely(au_dbrange_test(dentry))) {
9200+ err = -EINVAL;
9201+ AuTraceErr(err);
9202+ goto out_dgrade;
1facf9fc 9203+ }
027c5e7a
AM
9204+
9205+ sigen = au_sigen(sb);
9206+ if (au_digen_test(dentry, sigen)) {
1facf9fc 9207+ AuDebugOn(IS_ROOT(dentry));
027c5e7a
AM
9208+ err = au_reval_dpath(dentry, sigen);
9209+ if (unlikely(err)) {
9210+ AuTraceErr(err);
1facf9fc 9211+ goto out_dgrade;
027c5e7a 9212+ }
1facf9fc 9213+ }
9214+ di_downgrade_lock(dentry, AuLock_IR);
9215+
1facf9fc 9216+ err = -EINVAL;
c1595e42 9217+ if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
523b37e3 9218+ && inode
38d290e6 9219+ && !(inode->i_state && I_LINKABLE)
79b8bda9
AM
9220+ && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9221+ AuTraceErr(err);
027c5e7a 9222+ goto out_inval;
79b8bda9 9223+ }
027c5e7a 9224+
1facf9fc 9225+ do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9226+ if (do_udba && inode) {
5afbbe0d 9227+ aufs_bindex_t btop = au_ibtop(inode);
027c5e7a 9228+ struct inode *h_inode;
1facf9fc 9229+
5afbbe0d
AM
9230+ if (btop >= 0) {
9231+ h_inode = au_h_iptr(inode, btop);
79b8bda9
AM
9232+ if (h_inode && au_test_higen(inode, h_inode)) {
9233+ AuTraceErr(err);
027c5e7a 9234+ goto out_inval;
79b8bda9 9235+ }
027c5e7a 9236+ }
1facf9fc 9237+ }
9238+
8b6a4947
AM
9239+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9240+ err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
5afbbe0d 9241+ if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
1facf9fc 9242+ err = -EIO;
523b37e3
AM
9243+ AuDbg("both of real entry and whiteout found, %p, err %d\n",
9244+ dentry, err);
027c5e7a 9245+ }
e49829fe 9246+ goto out_inval;
1facf9fc 9247+
4f0767ce 9248+out_dgrade:
1facf9fc 9249+ di_downgrade_lock(dentry, AuLock_IR);
e49829fe 9250+out_inval:
1facf9fc 9251+ aufs_read_unlock(dentry, AuLock_IR);
9252+ AuTraceErr(err);
9253+ valid = !err;
e49829fe 9254+out:
027c5e7a 9255+ if (!valid) {
523b37e3 9256+ AuDbg("%pd invalid, %d\n", dentry, valid);
027c5e7a
AM
9257+ d_drop(dentry);
9258+ }
1facf9fc 9259+ return valid;
9260+}
9261+
9262+static void aufs_d_release(struct dentry *dentry)
9263+{
027c5e7a 9264+ if (au_di(dentry)) {
4a4d8108
AM
9265+ au_di_fin(dentry);
9266+ au_hn_di_reinit(dentry);
1facf9fc 9267+ }
1facf9fc 9268+}
9269+
4a4d8108 9270+const struct dentry_operations aufs_dop = {
c06a8ce3
AM
9271+ .d_revalidate = aufs_d_revalidate,
9272+ .d_weak_revalidate = aufs_d_revalidate,
9273+ .d_release = aufs_d_release
1facf9fc 9274+};
79b8bda9
AM
9275+
9276+/* aufs_dop without d_revalidate */
9277+const struct dentry_operations aufs_dop_noreval = {
9278+ .d_release = aufs_d_release
9279+};
7f207e10
AM
9280diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9281--- /usr/share/empty/fs/aufs/dentry.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 9282+++ linux/fs/aufs/dentry.h 2018-08-12 23:43:05.453457863 +0200
062440b3
AM
9283@@ -0,0 +1,267 @@
9284+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 9285+/*
b00004a5 9286+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 9287+ *
9288+ * This program, aufs is free software; you can redistribute it and/or modify
9289+ * it under the terms of the GNU General Public License as published by
9290+ * the Free Software Foundation; either version 2 of the License, or
9291+ * (at your option) any later version.
dece6358
AM
9292+ *
9293+ * This program is distributed in the hope that it will be useful,
9294+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9295+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9296+ * GNU General Public License for more details.
9297+ *
9298+ * You should have received a copy of the GNU General Public License
523b37e3 9299+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9300+ */
9301+
9302+/*
9303+ * lookup and dentry operations
9304+ */
9305+
9306+#ifndef __AUFS_DENTRY_H__
9307+#define __AUFS_DENTRY_H__
9308+
9309+#ifdef __KERNEL__
9310+
dece6358 9311+#include <linux/dcache.h>
8b6a4947 9312+#include "dirren.h"
1facf9fc 9313+#include "rwsem.h"
9314+
1facf9fc 9315+struct au_hdentry {
9316+ struct dentry *hd_dentry;
027c5e7a 9317+ aufs_bindex_t hd_id;
1facf9fc 9318+};
9319+
9320+struct au_dinfo {
9321+ atomic_t di_generation;
9322+
dece6358 9323+ struct au_rwsem di_rwsem;
5afbbe0d 9324+ aufs_bindex_t di_btop, di_bbot, di_bwh, di_bdiropq;
38d290e6 9325+ unsigned char di_tmpfile; /* to allow the different name */
1c60b727 9326+ struct au_hdentry *di_hdentry;
4a4d8108 9327+} ____cacheline_aligned_in_smp;
1facf9fc 9328+
9329+/* ---------------------------------------------------------------------- */
9330+
5afbbe0d
AM
9331+/* flags for au_lkup_dentry() */
9332+#define AuLkup_ALLOW_NEG 1
9333+#define AuLkup_IGNORE_PERM (1 << 1)
8b6a4947 9334+#define AuLkup_DIRREN (1 << 2)
5afbbe0d
AM
9335+#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
9336+#define au_fset_lkup(flags, name) \
9337+ do { (flags) |= AuLkup_##name; } while (0)
9338+#define au_fclr_lkup(flags, name) \
9339+ do { (flags) &= ~AuLkup_##name; } while (0)
9340+
8b6a4947
AM
9341+#ifndef CONFIG_AUFS_DIRREN
9342+#undef AuLkup_DIRREN
9343+#define AuLkup_DIRREN 0
9344+#endif
9345+
9346+struct au_do_lookup_args {
9347+ unsigned int flags;
9348+ mode_t type;
9349+ struct qstr whname, *name;
9350+ struct au_dr_lookup dirren;
9351+};
9352+
5afbbe0d
AM
9353+/* ---------------------------------------------------------------------- */
9354+
1facf9fc 9355+/* dentry.c */
79b8bda9 9356+extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
1facf9fc 9357+struct au_branch;
076b876e 9358+struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
1facf9fc 9359+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9360+ struct dentry *h_parent, struct au_branch *br);
9361+
5afbbe0d
AM
9362+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9363+ unsigned int flags);
86dc4139 9364+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
027c5e7a 9365+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
1facf9fc 9366+int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
79b8bda9 9367+void au_refresh_dop(struct dentry *dentry, int force_reval);
1facf9fc 9368+
9369+/* dinfo.c */
4a4d8108 9370+void au_di_init_once(void *_di);
027c5e7a
AM
9371+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9372+void au_di_free(struct au_dinfo *dinfo);
9373+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9374+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
4a4d8108
AM
9375+int au_di_init(struct dentry *dentry);
9376+void au_di_fin(struct dentry *dentry);
e2f27e51 9377+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
1facf9fc 9378+
9379+void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9380+void di_read_unlock(struct dentry *d, int flags);
9381+void di_downgrade_lock(struct dentry *d, int flags);
9382+void di_write_lock(struct dentry *d, unsigned int lsc);
9383+void di_write_unlock(struct dentry *d);
9384+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9385+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9386+void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9387+
9388+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
2cbb1c4b 9389+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
1facf9fc 9390+aufs_bindex_t au_dbtail(struct dentry *dentry);
9391+aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9392+
9393+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9394+ struct dentry *h_dentry);
027c5e7a
AM
9395+int au_digen_test(struct dentry *dentry, unsigned int sigen);
9396+int au_dbrange_test(struct dentry *dentry);
1facf9fc 9397+void au_update_digen(struct dentry *dentry);
9398+void au_update_dbrange(struct dentry *dentry, int do_put_zero);
5afbbe0d
AM
9399+void au_update_dbtop(struct dentry *dentry);
9400+void au_update_dbbot(struct dentry *dentry);
1facf9fc 9401+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9402+
9403+/* ---------------------------------------------------------------------- */
9404+
9405+static inline struct au_dinfo *au_di(struct dentry *dentry)
9406+{
9407+ return dentry->d_fsdata;
9408+}
9409+
9410+/* ---------------------------------------------------------------------- */
9411+
9412+/* lock subclass for dinfo */
9413+enum {
9414+ AuLsc_DI_CHILD, /* child first */
4a4d8108 9415+ AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
1facf9fc 9416+ AuLsc_DI_CHILD3, /* copyup dirs */
9417+ AuLsc_DI_PARENT,
9418+ AuLsc_DI_PARENT2,
027c5e7a
AM
9419+ AuLsc_DI_PARENT3,
9420+ AuLsc_DI_TMP /* temp for replacing dinfo */
1facf9fc 9421+};
9422+
9423+/*
9424+ * di_read_lock_child, di_write_lock_child,
9425+ * di_read_lock_child2, di_write_lock_child2,
9426+ * di_read_lock_child3, di_write_lock_child3,
9427+ * di_read_lock_parent, di_write_lock_parent,
9428+ * di_read_lock_parent2, di_write_lock_parent2,
9429+ * di_read_lock_parent3, di_write_lock_parent3,
9430+ */
9431+#define AuReadLockFunc(name, lsc) \
9432+static inline void di_read_lock_##name(struct dentry *d, int flags) \
9433+{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9434+
9435+#define AuWriteLockFunc(name, lsc) \
9436+static inline void di_write_lock_##name(struct dentry *d) \
9437+{ di_write_lock(d, AuLsc_DI_##lsc); }
9438+
9439+#define AuRWLockFuncs(name, lsc) \
9440+ AuReadLockFunc(name, lsc) \
9441+ AuWriteLockFunc(name, lsc)
9442+
9443+AuRWLockFuncs(child, CHILD);
9444+AuRWLockFuncs(child2, CHILD2);
9445+AuRWLockFuncs(child3, CHILD3);
9446+AuRWLockFuncs(parent, PARENT);
9447+AuRWLockFuncs(parent2, PARENT2);
9448+AuRWLockFuncs(parent3, PARENT3);
9449+
9450+#undef AuReadLockFunc
9451+#undef AuWriteLockFunc
9452+#undef AuRWLockFuncs
9453+
9454+#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
dece6358
AM
9455+#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
9456+#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
1facf9fc 9457+
9458+/* ---------------------------------------------------------------------- */
9459+
9460+/* todo: memory barrier? */
9461+static inline unsigned int au_digen(struct dentry *d)
9462+{
9463+ return atomic_read(&au_di(d)->di_generation);
9464+}
9465+
9466+static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9467+{
9468+ hdentry->hd_dentry = NULL;
9469+}
9470+
5afbbe0d
AM
9471+static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9472+ aufs_bindex_t bindex)
9473+{
9474+ return di->di_hdentry + bindex;
9475+}
9476+
1facf9fc 9477+static inline void au_hdput(struct au_hdentry *hd)
9478+{
4a4d8108
AM
9479+ if (hd)
9480+ dput(hd->hd_dentry);
1facf9fc 9481+}
9482+
5afbbe0d 9483+static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
1facf9fc 9484+{
1308ab2a 9485+ DiMustAnyLock(dentry);
5afbbe0d 9486+ return au_di(dentry)->di_btop;
1facf9fc 9487+}
9488+
5afbbe0d 9489+static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
1facf9fc 9490+{
1308ab2a 9491+ DiMustAnyLock(dentry);
5afbbe0d 9492+ return au_di(dentry)->di_bbot;
1facf9fc 9493+}
9494+
9495+static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9496+{
1308ab2a 9497+ DiMustAnyLock(dentry);
1facf9fc 9498+ return au_di(dentry)->di_bwh;
9499+}
9500+
9501+static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9502+{
1308ab2a 9503+ DiMustAnyLock(dentry);
1facf9fc 9504+ return au_di(dentry)->di_bdiropq;
9505+}
9506+
9507+/* todo: hard/soft set? */
5afbbe0d 9508+static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9509+{
1308ab2a 9510+ DiMustWriteLock(dentry);
5afbbe0d 9511+ au_di(dentry)->di_btop = bindex;
1facf9fc 9512+}
9513+
5afbbe0d 9514+static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9515+{
1308ab2a 9516+ DiMustWriteLock(dentry);
5afbbe0d 9517+ au_di(dentry)->di_bbot = bindex;
1facf9fc 9518+}
9519+
9520+static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9521+{
1308ab2a 9522+ DiMustWriteLock(dentry);
5afbbe0d 9523+ /* dbwh can be outside of btop - bbot range */
1facf9fc 9524+ au_di(dentry)->di_bwh = bindex;
9525+}
9526+
9527+static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9528+{
1308ab2a 9529+ DiMustWriteLock(dentry);
1facf9fc 9530+ au_di(dentry)->di_bdiropq = bindex;
9531+}
9532+
9533+/* ---------------------------------------------------------------------- */
9534+
4a4d8108 9535+#ifdef CONFIG_AUFS_HNOTIFY
1facf9fc 9536+static inline void au_digen_dec(struct dentry *d)
9537+{
e49829fe 9538+ atomic_dec(&au_di(d)->di_generation);
1facf9fc 9539+}
9540+
4a4d8108 9541+static inline void au_hn_di_reinit(struct dentry *dentry)
1facf9fc 9542+{
9543+ dentry->d_fsdata = NULL;
9544+}
9545+#else
4a4d8108
AM
9546+AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9547+#endif /* CONFIG_AUFS_HNOTIFY */
1facf9fc 9548+
9549+#endif /* __KERNEL__ */
9550+#endif /* __AUFS_DENTRY_H__ */
7f207e10
AM
9551diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9552--- /usr/share/empty/fs/aufs/dinfo.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 9553+++ linux/fs/aufs/dinfo.c 2018-08-12 23:43:05.453457863 +0200
062440b3 9554@@ -0,0 +1,554 @@
cd7a4cd9 9555+// SPDX-License-Identifier: GPL-2.0
1facf9fc 9556+/*
b00004a5 9557+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 9558+ *
9559+ * This program, aufs is free software; you can redistribute it and/or modify
9560+ * it under the terms of the GNU General Public License as published by
9561+ * the Free Software Foundation; either version 2 of the License, or
9562+ * (at your option) any later version.
dece6358
AM
9563+ *
9564+ * This program is distributed in the hope that it will be useful,
9565+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9566+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9567+ * GNU General Public License for more details.
9568+ *
9569+ * You should have received a copy of the GNU General Public License
523b37e3 9570+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9571+ */
9572+
9573+/*
9574+ * dentry private data
9575+ */
9576+
9577+#include "aufs.h"
9578+
e49829fe 9579+void au_di_init_once(void *_dinfo)
4a4d8108 9580+{
e49829fe 9581+ struct au_dinfo *dinfo = _dinfo;
4a4d8108 9582+
e49829fe 9583+ au_rw_init(&dinfo->di_rwsem);
4a4d8108
AM
9584+}
9585+
027c5e7a 9586+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
1facf9fc 9587+{
9588+ struct au_dinfo *dinfo;
027c5e7a 9589+ int nbr, i;
1facf9fc 9590+
9591+ dinfo = au_cache_alloc_dinfo();
9592+ if (unlikely(!dinfo))
9593+ goto out;
9594+
5afbbe0d 9595+ nbr = au_sbbot(sb) + 1;
1facf9fc 9596+ if (nbr <= 0)
9597+ nbr = 1;
9598+ dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
027c5e7a
AM
9599+ if (dinfo->di_hdentry) {
9600+ au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
5afbbe0d
AM
9601+ dinfo->di_btop = -1;
9602+ dinfo->di_bbot = -1;
027c5e7a
AM
9603+ dinfo->di_bwh = -1;
9604+ dinfo->di_bdiropq = -1;
38d290e6 9605+ dinfo->di_tmpfile = 0;
027c5e7a
AM
9606+ for (i = 0; i < nbr; i++)
9607+ dinfo->di_hdentry[i].hd_id = -1;
9608+ goto out;
9609+ }
1facf9fc 9610+
1c60b727 9611+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9612+ dinfo = NULL;
9613+
4f0767ce 9614+out:
027c5e7a 9615+ return dinfo;
1facf9fc 9616+}
9617+
027c5e7a 9618+void au_di_free(struct au_dinfo *dinfo)
4a4d8108 9619+{
4a4d8108 9620+ struct au_hdentry *p;
5afbbe0d 9621+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
9622+
9623+ /* dentry may not be revalidated */
5afbbe0d 9624+ bindex = dinfo->di_btop;
4a4d8108 9625+ if (bindex >= 0) {
5afbbe0d
AM
9626+ bbot = dinfo->di_bbot;
9627+ p = au_hdentry(dinfo, bindex);
9628+ while (bindex++ <= bbot)
4a4d8108
AM
9629+ au_hdput(p++);
9630+ }
1c60b727
AM
9631+ kfree(dinfo->di_hdentry);
9632+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9633+}
9634+
9635+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9636+{
9637+ struct au_hdentry *p;
9638+ aufs_bindex_t bi;
9639+
9640+ AuRwMustWriteLock(&a->di_rwsem);
9641+ AuRwMustWriteLock(&b->di_rwsem);
9642+
9643+#define DiSwap(v, name) \
9644+ do { \
9645+ v = a->di_##name; \
9646+ a->di_##name = b->di_##name; \
9647+ b->di_##name = v; \
9648+ } while (0)
9649+
9650+ DiSwap(p, hdentry);
5afbbe0d
AM
9651+ DiSwap(bi, btop);
9652+ DiSwap(bi, bbot);
027c5e7a
AM
9653+ DiSwap(bi, bwh);
9654+ DiSwap(bi, bdiropq);
9655+ /* smp_mb(); */
9656+
9657+#undef DiSwap
9658+}
9659+
9660+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9661+{
9662+ AuRwMustWriteLock(&dst->di_rwsem);
9663+ AuRwMustWriteLock(&src->di_rwsem);
9664+
5afbbe0d
AM
9665+ dst->di_btop = src->di_btop;
9666+ dst->di_bbot = src->di_bbot;
027c5e7a
AM
9667+ dst->di_bwh = src->di_bwh;
9668+ dst->di_bdiropq = src->di_bdiropq;
9669+ /* smp_mb(); */
9670+}
9671+
9672+int au_di_init(struct dentry *dentry)
9673+{
9674+ int err;
9675+ struct super_block *sb;
9676+ struct au_dinfo *dinfo;
9677+
9678+ err = 0;
9679+ sb = dentry->d_sb;
9680+ dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9681+ if (dinfo) {
9682+ atomic_set(&dinfo->di_generation, au_sigen(sb));
9683+ /* smp_mb(); */ /* atomic_set */
9684+ dentry->d_fsdata = dinfo;
9685+ } else
9686+ err = -ENOMEM;
9687+
9688+ return err;
9689+}
9690+
9691+void au_di_fin(struct dentry *dentry)
9692+{
9693+ struct au_dinfo *dinfo;
9694+
9695+ dinfo = au_di(dentry);
9696+ AuRwDestroy(&dinfo->di_rwsem);
9697+ au_di_free(dinfo);
4a4d8108
AM
9698+}
9699+
e2f27e51 9700+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
1facf9fc 9701+{
9702+ int err, sz;
9703+ struct au_hdentry *hdp;
9704+
1308ab2a 9705+ AuRwMustWriteLock(&dinfo->di_rwsem);
9706+
1facf9fc 9707+ err = -ENOMEM;
5afbbe0d 9708+ sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
1facf9fc 9709+ if (!sz)
9710+ sz = sizeof(*hdp);
e2f27e51
AM
9711+ hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9712+ may_shrink);
1facf9fc 9713+ if (hdp) {
9714+ dinfo->di_hdentry = hdp;
9715+ err = 0;
9716+ }
9717+
9718+ return err;
9719+}
9720+
9721+/* ---------------------------------------------------------------------- */
9722+
9723+static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9724+{
9725+ switch (lsc) {
9726+ case AuLsc_DI_CHILD:
9727+ ii_write_lock_child(inode);
9728+ break;
9729+ case AuLsc_DI_CHILD2:
9730+ ii_write_lock_child2(inode);
9731+ break;
9732+ case AuLsc_DI_CHILD3:
9733+ ii_write_lock_child3(inode);
9734+ break;
9735+ case AuLsc_DI_PARENT:
9736+ ii_write_lock_parent(inode);
9737+ break;
9738+ case AuLsc_DI_PARENT2:
9739+ ii_write_lock_parent2(inode);
9740+ break;
9741+ case AuLsc_DI_PARENT3:
9742+ ii_write_lock_parent3(inode);
9743+ break;
9744+ default:
9745+ BUG();
9746+ }
9747+}
9748+
9749+static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9750+{
9751+ switch (lsc) {
9752+ case AuLsc_DI_CHILD:
9753+ ii_read_lock_child(inode);
9754+ break;
9755+ case AuLsc_DI_CHILD2:
9756+ ii_read_lock_child2(inode);
9757+ break;
9758+ case AuLsc_DI_CHILD3:
9759+ ii_read_lock_child3(inode);
9760+ break;
9761+ case AuLsc_DI_PARENT:
9762+ ii_read_lock_parent(inode);
9763+ break;
9764+ case AuLsc_DI_PARENT2:
9765+ ii_read_lock_parent2(inode);
9766+ break;
9767+ case AuLsc_DI_PARENT3:
9768+ ii_read_lock_parent3(inode);
9769+ break;
9770+ default:
9771+ BUG();
9772+ }
9773+}
9774+
9775+void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9776+{
5527c038
JR
9777+ struct inode *inode;
9778+
dece6358 9779+ au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9780+ if (d_really_is_positive(d)) {
9781+ inode = d_inode(d);
1facf9fc 9782+ if (au_ftest_lock(flags, IW))
5527c038 9783+ do_ii_write_lock(inode, lsc);
1facf9fc 9784+ else if (au_ftest_lock(flags, IR))
5527c038 9785+ do_ii_read_lock(inode, lsc);
1facf9fc 9786+ }
9787+}
9788+
9789+void di_read_unlock(struct dentry *d, int flags)
9790+{
5527c038
JR
9791+ struct inode *inode;
9792+
9793+ if (d_really_is_positive(d)) {
9794+ inode = d_inode(d);
027c5e7a
AM
9795+ if (au_ftest_lock(flags, IW)) {
9796+ au_dbg_verify_dinode(d);
5527c038 9797+ ii_write_unlock(inode);
027c5e7a
AM
9798+ } else if (au_ftest_lock(flags, IR)) {
9799+ au_dbg_verify_dinode(d);
5527c038 9800+ ii_read_unlock(inode);
027c5e7a 9801+ }
1facf9fc 9802+ }
dece6358 9803+ au_rw_read_unlock(&au_di(d)->di_rwsem);
1facf9fc 9804+}
9805+
9806+void di_downgrade_lock(struct dentry *d, int flags)
9807+{
5527c038
JR
9808+ if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9809+ ii_downgrade_lock(d_inode(d));
dece6358 9810+ au_rw_dgrade_lock(&au_di(d)->di_rwsem);
1facf9fc 9811+}
9812+
9813+void di_write_lock(struct dentry *d, unsigned int lsc)
9814+{
dece6358 9815+ au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9816+ if (d_really_is_positive(d))
9817+ do_ii_write_lock(d_inode(d), lsc);
1facf9fc 9818+}
9819+
9820+void di_write_unlock(struct dentry *d)
9821+{
027c5e7a 9822+ au_dbg_verify_dinode(d);
5527c038
JR
9823+ if (d_really_is_positive(d))
9824+ ii_write_unlock(d_inode(d));
dece6358 9825+ au_rw_write_unlock(&au_di(d)->di_rwsem);
1facf9fc 9826+}
9827+
9828+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9829+{
9830+ AuDebugOn(d1 == d2
5527c038 9831+ || d_inode(d1) == d_inode(d2)
1facf9fc 9832+ || d1->d_sb != d2->d_sb);
9833+
521ced18
JR
9834+ if ((isdir && au_test_subdir(d1, d2))
9835+ || d1 < d2) {
1facf9fc 9836+ di_write_lock_child(d1);
9837+ di_write_lock_child2(d2);
9838+ } else {
1facf9fc 9839+ di_write_lock_child(d2);
9840+ di_write_lock_child2(d1);
9841+ }
9842+}
9843+
9844+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9845+{
9846+ AuDebugOn(d1 == d2
5527c038 9847+ || d_inode(d1) == d_inode(d2)
1facf9fc 9848+ || d1->d_sb != d2->d_sb);
9849+
521ced18
JR
9850+ if ((isdir && au_test_subdir(d1, d2))
9851+ || d1 < d2) {
1facf9fc 9852+ di_write_lock_parent(d1);
9853+ di_write_lock_parent2(d2);
9854+ } else {
1facf9fc 9855+ di_write_lock_parent(d2);
9856+ di_write_lock_parent2(d1);
9857+ }
9858+}
9859+
9860+void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9861+{
9862+ di_write_unlock(d1);
5527c038 9863+ if (d_inode(d1) == d_inode(d2))
dece6358 9864+ au_rw_write_unlock(&au_di(d2)->di_rwsem);
1facf9fc 9865+ else
9866+ di_write_unlock(d2);
9867+}
9868+
9869+/* ---------------------------------------------------------------------- */
9870+
9871+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9872+{
9873+ struct dentry *d;
9874+
1308ab2a 9875+ DiMustAnyLock(dentry);
9876+
5afbbe0d 9877+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
1facf9fc 9878+ return NULL;
9879+ AuDebugOn(bindex < 0);
5afbbe0d 9880+ d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
c1595e42 9881+ AuDebugOn(d && au_dcount(d) <= 0);
1facf9fc 9882+ return d;
9883+}
9884+
2cbb1c4b
JR
9885+/*
9886+ * extended version of au_h_dptr().
38d290e6
JR
9887+ * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9888+ * error.
2cbb1c4b
JR
9889+ */
9890+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9891+{
9892+ struct dentry *h_dentry;
9893+ struct inode *inode, *h_inode;
9894+
5527c038 9895+ AuDebugOn(d_really_is_negative(dentry));
2cbb1c4b
JR
9896+
9897+ h_dentry = NULL;
5afbbe0d
AM
9898+ if (au_dbtop(dentry) <= bindex
9899+ && bindex <= au_dbbot(dentry))
2cbb1c4b 9900+ h_dentry = au_h_dptr(dentry, bindex);
38d290e6 9901+ if (h_dentry && !au_d_linkable(h_dentry)) {
2cbb1c4b
JR
9902+ dget(h_dentry);
9903+ goto out; /* success */
9904+ }
9905+
5527c038 9906+ inode = d_inode(dentry);
5afbbe0d
AM
9907+ AuDebugOn(bindex < au_ibtop(inode));
9908+ AuDebugOn(au_ibbot(inode) < bindex);
2cbb1c4b
JR
9909+ h_inode = au_h_iptr(inode, bindex);
9910+ h_dentry = d_find_alias(h_inode);
9911+ if (h_dentry) {
9912+ if (!IS_ERR(h_dentry)) {
38d290e6 9913+ if (!au_d_linkable(h_dentry))
2cbb1c4b
JR
9914+ goto out; /* success */
9915+ dput(h_dentry);
9916+ } else
9917+ goto out;
9918+ }
9919+
9920+ if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9921+ h_dentry = au_plink_lkup(inode, bindex);
9922+ AuDebugOn(!h_dentry);
9923+ if (!IS_ERR(h_dentry)) {
9924+ if (!au_d_hashed_positive(h_dentry))
9925+ goto out; /* success */
9926+ dput(h_dentry);
9927+ h_dentry = NULL;
9928+ }
9929+ }
9930+
9931+out:
9932+ AuDbgDentry(h_dentry);
9933+ return h_dentry;
9934+}
9935+
1facf9fc 9936+aufs_bindex_t au_dbtail(struct dentry *dentry)
9937+{
5afbbe0d 9938+ aufs_bindex_t bbot, bwh;
1facf9fc 9939+
5afbbe0d
AM
9940+ bbot = au_dbbot(dentry);
9941+ if (0 <= bbot) {
1facf9fc 9942+ bwh = au_dbwh(dentry);
9943+ if (!bwh)
9944+ return bwh;
5afbbe0d 9945+ if (0 < bwh && bwh < bbot)
1facf9fc 9946+ return bwh - 1;
9947+ }
5afbbe0d 9948+ return bbot;
1facf9fc 9949+}
9950+
9951+aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9952+{
5afbbe0d 9953+ aufs_bindex_t bbot, bopq;
1facf9fc 9954+
5afbbe0d
AM
9955+ bbot = au_dbtail(dentry);
9956+ if (0 <= bbot) {
1facf9fc 9957+ bopq = au_dbdiropq(dentry);
5afbbe0d
AM
9958+ if (0 <= bopq && bopq < bbot)
9959+ bbot = bopq;
1facf9fc 9960+ }
5afbbe0d 9961+ return bbot;
1facf9fc 9962+}
9963+
9964+/* ---------------------------------------------------------------------- */
9965+
9966+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9967+ struct dentry *h_dentry)
9968+{
5afbbe0d
AM
9969+ struct au_dinfo *dinfo;
9970+ struct au_hdentry *hd;
027c5e7a 9971+ struct au_branch *br;
1facf9fc 9972+
1308ab2a 9973+ DiMustWriteLock(dentry);
9974+
5afbbe0d
AM
9975+ dinfo = au_di(dentry);
9976+ hd = au_hdentry(dinfo, bindex);
4a4d8108 9977+ au_hdput(hd);
1facf9fc 9978+ hd->hd_dentry = h_dentry;
027c5e7a
AM
9979+ if (h_dentry) {
9980+ br = au_sbr(dentry->d_sb, bindex);
9981+ hd->hd_id = br->br_id;
9982+ }
9983+}
9984+
9985+int au_dbrange_test(struct dentry *dentry)
9986+{
9987+ int err;
5afbbe0d 9988+ aufs_bindex_t btop, bbot;
027c5e7a
AM
9989+
9990+ err = 0;
5afbbe0d
AM
9991+ btop = au_dbtop(dentry);
9992+ bbot = au_dbbot(dentry);
9993+ if (btop >= 0)
9994+ AuDebugOn(bbot < 0 && btop > bbot);
027c5e7a
AM
9995+ else {
9996+ err = -EIO;
5afbbe0d 9997+ AuDebugOn(bbot >= 0);
027c5e7a
AM
9998+ }
9999+
10000+ return err;
10001+}
10002+
10003+int au_digen_test(struct dentry *dentry, unsigned int sigen)
10004+{
10005+ int err;
10006+
10007+ err = 0;
10008+ if (unlikely(au_digen(dentry) != sigen
5527c038 10009+ || au_iigen_test(d_inode(dentry), sigen)))
027c5e7a
AM
10010+ err = -EIO;
10011+
10012+ return err;
1facf9fc 10013+}
10014+
10015+void au_update_digen(struct dentry *dentry)
10016+{
10017+ atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
10018+ /* smp_mb(); */ /* atomic_set */
10019+}
10020+
10021+void au_update_dbrange(struct dentry *dentry, int do_put_zero)
10022+{
10023+ struct au_dinfo *dinfo;
10024+ struct dentry *h_d;
4a4d8108 10025+ struct au_hdentry *hdp;
5afbbe0d 10026+ aufs_bindex_t bindex, bbot;
1facf9fc 10027+
1308ab2a 10028+ DiMustWriteLock(dentry);
10029+
1facf9fc 10030+ dinfo = au_di(dentry);
5afbbe0d 10031+ if (!dinfo || dinfo->di_btop < 0)
1facf9fc 10032+ return;
10033+
10034+ if (do_put_zero) {
5afbbe0d
AM
10035+ bbot = dinfo->di_bbot;
10036+ bindex = dinfo->di_btop;
10037+ hdp = au_hdentry(dinfo, bindex);
10038+ for (; bindex <= bbot; bindex++, hdp++) {
10039+ h_d = hdp->hd_dentry;
5527c038 10040+ if (h_d && d_is_negative(h_d))
1facf9fc 10041+ au_set_h_dptr(dentry, bindex, NULL);
10042+ }
10043+ }
10044+
5afbbe0d
AM
10045+ dinfo->di_btop = 0;
10046+ hdp = au_hdentry(dinfo, dinfo->di_btop);
10047+ for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
10048+ if (hdp->hd_dentry)
1facf9fc 10049+ break;
5afbbe0d
AM
10050+ if (dinfo->di_btop > dinfo->di_bbot) {
10051+ dinfo->di_btop = -1;
10052+ dinfo->di_bbot = -1;
1facf9fc 10053+ return;
10054+ }
10055+
5afbbe0d
AM
10056+ hdp = au_hdentry(dinfo, dinfo->di_bbot);
10057+ for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
10058+ if (hdp->hd_dentry)
1facf9fc 10059+ break;
5afbbe0d 10060+ AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
1facf9fc 10061+}
10062+
5afbbe0d 10063+void au_update_dbtop(struct dentry *dentry)
1facf9fc 10064+{
5afbbe0d 10065+ aufs_bindex_t bindex, bbot;
1facf9fc 10066+ struct dentry *h_dentry;
10067+
5afbbe0d
AM
10068+ bbot = au_dbbot(dentry);
10069+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
1facf9fc 10070+ h_dentry = au_h_dptr(dentry, bindex);
10071+ if (!h_dentry)
10072+ continue;
5527c038 10073+ if (d_is_positive(h_dentry)) {
5afbbe0d 10074+ au_set_dbtop(dentry, bindex);
1facf9fc 10075+ return;
10076+ }
10077+ au_set_h_dptr(dentry, bindex, NULL);
10078+ }
10079+}
10080+
5afbbe0d 10081+void au_update_dbbot(struct dentry *dentry)
1facf9fc 10082+{
5afbbe0d 10083+ aufs_bindex_t bindex, btop;
1facf9fc 10084+ struct dentry *h_dentry;
10085+
5afbbe0d
AM
10086+ btop = au_dbtop(dentry);
10087+ for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
1facf9fc 10088+ h_dentry = au_h_dptr(dentry, bindex);
10089+ if (!h_dentry)
10090+ continue;
5527c038 10091+ if (d_is_positive(h_dentry)) {
5afbbe0d 10092+ au_set_dbbot(dentry, bindex);
1facf9fc 10093+ return;
10094+ }
10095+ au_set_h_dptr(dentry, bindex, NULL);
10096+ }
10097+}
10098+
10099+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
10100+{
5afbbe0d 10101+ aufs_bindex_t bindex, bbot;
1facf9fc 10102+
5afbbe0d
AM
10103+ bbot = au_dbbot(dentry);
10104+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
1facf9fc 10105+ if (au_h_dptr(dentry, bindex) == h_dentry)
10106+ return bindex;
10107+ return -1;
10108+}
7f207e10
AM
10109diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
10110--- /usr/share/empty/fs/aufs/dir.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 10111+++ linux/fs/aufs/dir.c 2018-08-12 23:43:05.453457863 +0200
062440b3 10112@@ -0,0 +1,760 @@
cd7a4cd9 10113+// SPDX-License-Identifier: GPL-2.0
1facf9fc 10114+/*
b00004a5 10115+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 10116+ *
10117+ * This program, aufs is free software; you can redistribute it and/or modify
10118+ * it under the terms of the GNU General Public License as published by
10119+ * the Free Software Foundation; either version 2 of the License, or
10120+ * (at your option) any later version.
dece6358
AM
10121+ *
10122+ * This program is distributed in the hope that it will be useful,
10123+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10124+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10125+ * GNU General Public License for more details.
10126+ *
10127+ * You should have received a copy of the GNU General Public License
523b37e3 10128+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10129+ */
10130+
10131+/*
10132+ * directory operations
10133+ */
10134+
10135+#include <linux/fs_stack.h>
10136+#include "aufs.h"
10137+
10138+void au_add_nlink(struct inode *dir, struct inode *h_dir)
10139+{
9dbd164d
AM
10140+ unsigned int nlink;
10141+
1facf9fc 10142+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10143+
9dbd164d
AM
10144+ nlink = dir->i_nlink;
10145+ nlink += h_dir->i_nlink - 2;
1facf9fc 10146+ if (h_dir->i_nlink < 2)
9dbd164d 10147+ nlink += 2;
f6b6e03d 10148+ smp_mb(); /* for i_nlink */
7eafdf33 10149+ /* 0 can happen in revaliding */
92d182d2 10150+ set_nlink(dir, nlink);
1facf9fc 10151+}
10152+
10153+void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10154+{
9dbd164d
AM
10155+ unsigned int nlink;
10156+
1facf9fc 10157+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10158+
9dbd164d
AM
10159+ nlink = dir->i_nlink;
10160+ nlink -= h_dir->i_nlink - 2;
1facf9fc 10161+ if (h_dir->i_nlink < 2)
9dbd164d 10162+ nlink -= 2;
f6b6e03d 10163+ smp_mb(); /* for i_nlink */
92d182d2 10164+ /* nlink == 0 means the branch-fs is broken */
9dbd164d 10165+ set_nlink(dir, nlink);
1facf9fc 10166+}
10167+
1308ab2a 10168+loff_t au_dir_size(struct file *file, struct dentry *dentry)
10169+{
10170+ loff_t sz;
5afbbe0d 10171+ aufs_bindex_t bindex, bbot;
1308ab2a 10172+ struct file *h_file;
10173+ struct dentry *h_dentry;
10174+
10175+ sz = 0;
10176+ if (file) {
2000de60 10177+ AuDebugOn(!d_is_dir(file->f_path.dentry));
1308ab2a 10178+
5afbbe0d
AM
10179+ bbot = au_fbbot_dir(file);
10180+ for (bindex = au_fbtop(file);
10181+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 10182+ bindex++) {
4a4d8108 10183+ h_file = au_hf_dir(file, bindex);
c06a8ce3
AM
10184+ if (h_file && file_inode(h_file))
10185+ sz += vfsub_f_size_read(h_file);
1308ab2a 10186+ }
10187+ } else {
10188+ AuDebugOn(!dentry);
2000de60 10189+ AuDebugOn(!d_is_dir(dentry));
1308ab2a 10190+
5afbbe0d
AM
10191+ bbot = au_dbtaildir(dentry);
10192+ for (bindex = au_dbtop(dentry);
10193+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 10194+ bindex++) {
10195+ h_dentry = au_h_dptr(dentry, bindex);
5527c038
JR
10196+ if (h_dentry && d_is_positive(h_dentry))
10197+ sz += i_size_read(d_inode(h_dentry));
1308ab2a 10198+ }
10199+ }
10200+ if (sz < KMALLOC_MAX_SIZE)
10201+ sz = roundup_pow_of_two(sz);
10202+ if (sz > KMALLOC_MAX_SIZE)
10203+ sz = KMALLOC_MAX_SIZE;
10204+ else if (sz < NAME_MAX) {
10205+ BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10206+ sz = AUFS_RDBLK_DEF;
10207+ }
10208+ return sz;
10209+}
10210+
b912730e
AM
10211+struct au_dir_ts_arg {
10212+ struct dentry *dentry;
10213+ aufs_bindex_t brid;
10214+};
10215+
10216+static void au_do_dir_ts(void *arg)
10217+{
10218+ struct au_dir_ts_arg *a = arg;
10219+ struct au_dtime dt;
10220+ struct path h_path;
10221+ struct inode *dir, *h_dir;
10222+ struct super_block *sb;
10223+ struct au_branch *br;
10224+ struct au_hinode *hdir;
10225+ int err;
5afbbe0d 10226+ aufs_bindex_t btop, bindex;
b912730e
AM
10227+
10228+ sb = a->dentry->d_sb;
5527c038 10229+ if (d_really_is_negative(a->dentry))
b912730e 10230+ goto out;
5527c038 10231+ /* no dir->i_mutex lock */
b95c5147
AM
10232+ aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10233+
5527c038 10234+ dir = d_inode(a->dentry);
5afbbe0d 10235+ btop = au_ibtop(dir);
b912730e 10236+ bindex = au_br_index(sb, a->brid);
5afbbe0d 10237+ if (bindex < btop)
b912730e
AM
10238+ goto out_unlock;
10239+
10240+ br = au_sbr(sb, bindex);
10241+ h_path.dentry = au_h_dptr(a->dentry, bindex);
10242+ if (!h_path.dentry)
10243+ goto out_unlock;
10244+ h_path.mnt = au_br_mnt(br);
10245+ au_dtime_store(&dt, a->dentry, &h_path);
10246+
5afbbe0d 10247+ br = au_sbr(sb, btop);
b912730e
AM
10248+ if (!au_br_writable(br->br_perm))
10249+ goto out_unlock;
5afbbe0d 10250+ h_path.dentry = au_h_dptr(a->dentry, btop);
b912730e
AM
10251+ h_path.mnt = au_br_mnt(br);
10252+ err = vfsub_mnt_want_write(h_path.mnt);
10253+ if (err)
10254+ goto out_unlock;
5afbbe0d
AM
10255+ hdir = au_hi(dir, btop);
10256+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10257+ h_dir = au_h_iptr(dir, btop);
b912730e 10258+ if (h_dir->i_nlink
cd7a4cd9 10259+ && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
b912730e
AM
10260+ dt.dt_h_path = h_path;
10261+ au_dtime_revert(&dt);
10262+ }
5afbbe0d 10263+ au_hn_inode_unlock(hdir);
b912730e
AM
10264+ vfsub_mnt_drop_write(h_path.mnt);
10265+ au_cpup_attr_timesizes(dir);
10266+
10267+out_unlock:
10268+ aufs_read_unlock(a->dentry, AuLock_DW);
10269+out:
10270+ dput(a->dentry);
10271+ au_nwt_done(&au_sbi(sb)->si_nowait);
1c60b727 10272+ kfree(arg);
b912730e
AM
10273+}
10274+
10275+void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10276+{
10277+ int perm, wkq_err;
5afbbe0d 10278+ aufs_bindex_t btop;
b912730e
AM
10279+ struct au_dir_ts_arg *arg;
10280+ struct dentry *dentry;
10281+ struct super_block *sb;
10282+
10283+ IMustLock(dir);
10284+
10285+ dentry = d_find_any_alias(dir);
10286+ AuDebugOn(!dentry);
10287+ sb = dentry->d_sb;
5afbbe0d
AM
10288+ btop = au_ibtop(dir);
10289+ if (btop == bindex) {
b912730e
AM
10290+ au_cpup_attr_timesizes(dir);
10291+ goto out;
10292+ }
10293+
5afbbe0d 10294+ perm = au_sbr_perm(sb, btop);
b912730e
AM
10295+ if (!au_br_writable(perm))
10296+ goto out;
10297+
10298+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
10299+ if (!arg)
10300+ goto out;
10301+
10302+ arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10303+ arg->brid = au_sbr_id(sb, bindex);
10304+ wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10305+ if (unlikely(wkq_err)) {
10306+ pr_err("wkq %d\n", wkq_err);
10307+ dput(dentry);
1c60b727 10308+ kfree(arg);
b912730e
AM
10309+ }
10310+
10311+out:
10312+ dput(dentry);
10313+}
10314+
1facf9fc 10315+/* ---------------------------------------------------------------------- */
10316+
10317+static int reopen_dir(struct file *file)
10318+{
10319+ int err;
10320+ unsigned int flags;
5afbbe0d 10321+ aufs_bindex_t bindex, btail, btop;
1facf9fc 10322+ struct dentry *dentry, *h_dentry;
10323+ struct file *h_file;
10324+
10325+ /* open all lower dirs */
2000de60 10326+ dentry = file->f_path.dentry;
5afbbe0d
AM
10327+ btop = au_dbtop(dentry);
10328+ for (bindex = au_fbtop(file); bindex < btop; bindex++)
1facf9fc 10329+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 10330+ au_set_fbtop(file, btop);
1facf9fc 10331+
10332+ btail = au_dbtaildir(dentry);
5afbbe0d 10333+ for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
1facf9fc 10334+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 10335+ au_set_fbbot_dir(file, btail);
1facf9fc 10336+
4a4d8108 10337+ flags = vfsub_file_flags(file);
5afbbe0d 10338+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 10339+ h_dentry = au_h_dptr(dentry, bindex);
10340+ if (!h_dentry)
10341+ continue;
4a4d8108 10342+ h_file = au_hf_dir(file, bindex);
1facf9fc 10343+ if (h_file)
10344+ continue;
10345+
392086de 10346+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 10347+ err = PTR_ERR(h_file);
10348+ if (IS_ERR(h_file))
10349+ goto out; /* close all? */
10350+ au_set_h_fptr(file, bindex, h_file);
10351+ }
10352+ au_update_figen(file);
10353+ /* todo: necessary? */
10354+ /* file->f_ra = h_file->f_ra; */
10355+ err = 0;
10356+
4f0767ce 10357+out:
1facf9fc 10358+ return err;
10359+}
10360+
b912730e 10361+static int do_open_dir(struct file *file, int flags, struct file *h_file)
1facf9fc 10362+{
10363+ int err;
10364+ aufs_bindex_t bindex, btail;
10365+ struct dentry *dentry, *h_dentry;
8cdd5066 10366+ struct vfsmount *mnt;
1facf9fc 10367+
1308ab2a 10368+ FiMustWriteLock(file);
b912730e 10369+ AuDebugOn(h_file);
1308ab2a 10370+
523b37e3 10371+ err = 0;
8cdd5066 10372+ mnt = file->f_path.mnt;
2000de60 10373+ dentry = file->f_path.dentry;
be118d29 10374+ file->f_version = inode_query_iversion(d_inode(dentry));
5afbbe0d
AM
10375+ bindex = au_dbtop(dentry);
10376+ au_set_fbtop(file, bindex);
1facf9fc 10377+ btail = au_dbtaildir(dentry);
5afbbe0d 10378+ au_set_fbbot_dir(file, btail);
1facf9fc 10379+ for (; !err && bindex <= btail; bindex++) {
10380+ h_dentry = au_h_dptr(dentry, bindex);
10381+ if (!h_dentry)
10382+ continue;
10383+
8cdd5066
JR
10384+ err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10385+ if (unlikely(err))
10386+ break;
392086de 10387+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 10388+ if (IS_ERR(h_file)) {
10389+ err = PTR_ERR(h_file);
10390+ break;
10391+ }
10392+ au_set_h_fptr(file, bindex, h_file);
10393+ }
10394+ au_update_figen(file);
10395+ /* todo: necessary? */
10396+ /* file->f_ra = h_file->f_ra; */
10397+ if (!err)
10398+ return 0; /* success */
10399+
10400+ /* close all */
5afbbe0d 10401+ for (bindex = au_fbtop(file); bindex <= btail; bindex++)
1facf9fc 10402+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d
AM
10403+ au_set_fbtop(file, -1);
10404+ au_set_fbbot_dir(file, -1);
4a4d8108 10405+
1facf9fc 10406+ return err;
10407+}
10408+
10409+static int aufs_open_dir(struct inode *inode __maybe_unused,
10410+ struct file *file)
10411+{
4a4d8108
AM
10412+ int err;
10413+ struct super_block *sb;
10414+ struct au_fidir *fidir;
10415+
10416+ err = -ENOMEM;
2000de60 10417+ sb = file->f_path.dentry->d_sb;
4a4d8108 10418+ si_read_lock(sb, AuLock_FLUSH);
e49829fe 10419+ fidir = au_fidir_alloc(sb);
4a4d8108 10420+ if (fidir) {
b912730e
AM
10421+ struct au_do_open_args args = {
10422+ .open = do_open_dir,
10423+ .fidir = fidir
10424+ };
10425+ err = au_do_open(file, &args);
4a4d8108 10426+ if (unlikely(err))
1c60b727 10427+ kfree(fidir);
4a4d8108
AM
10428+ }
10429+ si_read_unlock(sb);
10430+ return err;
1facf9fc 10431+}
10432+
10433+static int aufs_release_dir(struct inode *inode __maybe_unused,
10434+ struct file *file)
10435+{
10436+ struct au_vdir *vdir_cache;
4a4d8108
AM
10437+ struct au_finfo *finfo;
10438+ struct au_fidir *fidir;
f0c0a007 10439+ struct au_hfile *hf;
5afbbe0d 10440+ aufs_bindex_t bindex, bbot;
1facf9fc 10441+
4a4d8108
AM
10442+ finfo = au_fi(file);
10443+ fidir = finfo->fi_hdir;
10444+ if (fidir) {
8b6a4947
AM
10445+ au_hbl_del(&finfo->fi_hlist,
10446+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108
AM
10447+ vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10448+ if (vdir_cache)
1c60b727 10449+ au_vdir_free(vdir_cache);
4a4d8108
AM
10450+
10451+ bindex = finfo->fi_btop;
10452+ if (bindex >= 0) {
f0c0a007 10453+ hf = fidir->fd_hfile + bindex;
4a4d8108
AM
10454+ /*
10455+ * calls fput() instead of filp_close(),
10456+ * since no dnotify or lock for the lower file.
10457+ */
5afbbe0d 10458+ bbot = fidir->fd_bbot;
f0c0a007
AM
10459+ for (; bindex <= bbot; bindex++, hf++)
10460+ if (hf->hf_file)
1c60b727 10461+ au_hfput(hf, /*execed*/0);
4a4d8108 10462+ }
1c60b727 10463+ kfree(fidir);
4a4d8108 10464+ finfo->fi_hdir = NULL;
1facf9fc 10465+ }
1c60b727 10466+ au_finfo_fin(file);
1facf9fc 10467+ return 0;
10468+}
10469+
10470+/* ---------------------------------------------------------------------- */
10471+
4a4d8108
AM
10472+static int au_do_flush_dir(struct file *file, fl_owner_t id)
10473+{
10474+ int err;
5afbbe0d 10475+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
10476+ struct file *h_file;
10477+
10478+ err = 0;
5afbbe0d
AM
10479+ bbot = au_fbbot_dir(file);
10480+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
10481+ h_file = au_hf_dir(file, bindex);
10482+ if (h_file)
10483+ err = vfsub_flush(h_file, id);
10484+ }
10485+ return err;
10486+}
10487+
10488+static int aufs_flush_dir(struct file *file, fl_owner_t id)
10489+{
10490+ return au_do_flush(file, id, au_do_flush_dir);
10491+}
10492+
10493+/* ---------------------------------------------------------------------- */
10494+
1facf9fc 10495+static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10496+{
10497+ int err;
5afbbe0d 10498+ aufs_bindex_t bbot, bindex;
1facf9fc 10499+ struct inode *inode;
10500+ struct super_block *sb;
10501+
10502+ err = 0;
10503+ sb = dentry->d_sb;
5527c038 10504+ inode = d_inode(dentry);
1facf9fc 10505+ IMustLock(inode);
5afbbe0d
AM
10506+ bbot = au_dbbot(dentry);
10507+ for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
1facf9fc 10508+ struct path h_path;
1facf9fc 10509+
10510+ if (au_test_ro(sb, bindex, inode))
10511+ continue;
10512+ h_path.dentry = au_h_dptr(dentry, bindex);
10513+ if (!h_path.dentry)
10514+ continue;
1facf9fc 10515+
1facf9fc 10516+ h_path.mnt = au_sbr_mnt(sb, bindex);
53392da6 10517+ err = vfsub_fsync(NULL, &h_path, datasync);
1facf9fc 10518+ }
10519+
10520+ return err;
10521+}
10522+
10523+static int au_do_fsync_dir(struct file *file, int datasync)
10524+{
10525+ int err;
5afbbe0d 10526+ aufs_bindex_t bbot, bindex;
1facf9fc 10527+ struct file *h_file;
10528+ struct super_block *sb;
10529+ struct inode *inode;
1facf9fc 10530+
521ced18 10531+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
1facf9fc 10532+ if (unlikely(err))
10533+ goto out;
10534+
c06a8ce3 10535+ inode = file_inode(file);
b912730e 10536+ sb = inode->i_sb;
5afbbe0d
AM
10537+ bbot = au_fbbot_dir(file);
10538+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108 10539+ h_file = au_hf_dir(file, bindex);
1facf9fc 10540+ if (!h_file || au_test_ro(sb, bindex, inode))
10541+ continue;
10542+
53392da6 10543+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
1facf9fc 10544+ }
10545+
4f0767ce 10546+out:
1facf9fc 10547+ return err;
10548+}
10549+
10550+/*
10551+ * @file may be NULL
10552+ */
1e00d052
AM
10553+static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10554+ int datasync)
1facf9fc 10555+{
10556+ int err;
b752ccd1 10557+ struct dentry *dentry;
5527c038 10558+ struct inode *inode;
1facf9fc 10559+ struct super_block *sb;
1facf9fc 10560+
10561+ err = 0;
2000de60 10562+ dentry = file->f_path.dentry;
5527c038 10563+ inode = d_inode(dentry);
febd17d6 10564+ inode_lock(inode);
1facf9fc 10565+ sb = dentry->d_sb;
10566+ si_noflush_read_lock(sb);
10567+ if (file)
10568+ err = au_do_fsync_dir(file, datasync);
10569+ else {
10570+ di_write_lock_child(dentry);
10571+ err = au_do_fsync_dir_no_file(dentry, datasync);
10572+ }
5527c038 10573+ au_cpup_attr_timesizes(inode);
1facf9fc 10574+ di_write_unlock(dentry);
10575+ if (file)
10576+ fi_write_unlock(file);
10577+
10578+ si_read_unlock(sb);
febd17d6 10579+ inode_unlock(inode);
1facf9fc 10580+ return err;
10581+}
10582+
10583+/* ---------------------------------------------------------------------- */
10584+
5afbbe0d 10585+static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
1facf9fc 10586+{
10587+ int err;
10588+ struct dentry *dentry;
9dbd164d 10589+ struct inode *inode, *h_inode;
1facf9fc 10590+ struct super_block *sb;
10591+
062440b3 10592+ AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
392086de 10593+
2000de60 10594+ dentry = file->f_path.dentry;
5527c038 10595+ inode = d_inode(dentry);
1facf9fc 10596+ IMustLock(inode);
10597+
10598+ sb = dentry->d_sb;
10599+ si_read_lock(sb, AuLock_FLUSH);
521ced18 10600+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
1facf9fc 10601+ if (unlikely(err))
10602+ goto out;
027c5e7a
AM
10603+ err = au_alive_dir(dentry);
10604+ if (!err)
10605+ err = au_vdir_init(file);
1facf9fc 10606+ di_downgrade_lock(dentry, AuLock_IR);
10607+ if (unlikely(err))
10608+ goto out_unlock;
10609+
5afbbe0d 10610+ h_inode = au_h_iptr(inode, au_ibtop(inode));
b752ccd1 10611+ if (!au_test_nfsd()) {
392086de 10612+ err = au_vdir_fill_de(file, ctx);
9dbd164d 10613+ fsstack_copy_attr_atime(inode, h_inode);
1facf9fc 10614+ } else {
10615+ /*
10616+ * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10617+ * encode_fh() and others.
10618+ */
9dbd164d 10619+ atomic_inc(&h_inode->i_count);
1facf9fc 10620+ di_read_unlock(dentry, AuLock_IR);
10621+ si_read_unlock(sb);
392086de 10622+ err = au_vdir_fill_de(file, ctx);
1facf9fc 10623+ fsstack_copy_attr_atime(inode, h_inode);
10624+ fi_write_unlock(file);
9dbd164d 10625+ iput(h_inode);
1facf9fc 10626+
10627+ AuTraceErr(err);
10628+ return err;
10629+ }
10630+
4f0767ce 10631+out_unlock:
1facf9fc 10632+ di_read_unlock(dentry, AuLock_IR);
10633+ fi_write_unlock(file);
4f0767ce 10634+out:
1facf9fc 10635+ si_read_unlock(sb);
10636+ return err;
10637+}
10638+
10639+/* ---------------------------------------------------------------------- */
10640+
10641+#define AuTestEmpty_WHONLY 1
dece6358
AM
10642+#define AuTestEmpty_CALLED (1 << 1)
10643+#define AuTestEmpty_SHWH (1 << 2)
1facf9fc 10644+#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
7f207e10
AM
10645+#define au_fset_testempty(flags, name) \
10646+ do { (flags) |= AuTestEmpty_##name; } while (0)
10647+#define au_fclr_testempty(flags, name) \
10648+ do { (flags) &= ~AuTestEmpty_##name; } while (0)
1facf9fc 10649+
dece6358
AM
10650+#ifndef CONFIG_AUFS_SHWH
10651+#undef AuTestEmpty_SHWH
10652+#define AuTestEmpty_SHWH 0
10653+#endif
10654+
1facf9fc 10655+struct test_empty_arg {
392086de 10656+ struct dir_context ctx;
1308ab2a 10657+ struct au_nhash *whlist;
1facf9fc 10658+ unsigned int flags;
10659+ int err;
10660+ aufs_bindex_t bindex;
10661+};
10662+
392086de
AM
10663+static int test_empty_cb(struct dir_context *ctx, const char *__name,
10664+ int namelen, loff_t offset __maybe_unused, u64 ino,
dece6358 10665+ unsigned int d_type)
1facf9fc 10666+{
392086de
AM
10667+ struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10668+ ctx);
1facf9fc 10669+ char *name = (void *)__name;
10670+
10671+ arg->err = 0;
10672+ au_fset_testempty(arg->flags, CALLED);
10673+ /* smp_mb(); */
10674+ if (name[0] == '.'
10675+ && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10676+ goto out; /* success */
10677+
10678+ if (namelen <= AUFS_WH_PFX_LEN
10679+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10680+ if (au_ftest_testempty(arg->flags, WHONLY)
1308ab2a 10681+ && !au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10682+ arg->err = -ENOTEMPTY;
10683+ goto out;
10684+ }
10685+
10686+ name += AUFS_WH_PFX_LEN;
10687+ namelen -= AUFS_WH_PFX_LEN;
1308ab2a 10688+ if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10689+ arg->err = au_nhash_append_wh
1308ab2a 10690+ (arg->whlist, name, namelen, ino, d_type, arg->bindex,
dece6358 10691+ au_ftest_testempty(arg->flags, SHWH));
1facf9fc 10692+
4f0767ce 10693+out:
1facf9fc 10694+ /* smp_mb(); */
10695+ AuTraceErr(arg->err);
10696+ return arg->err;
10697+}
10698+
10699+static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10700+{
10701+ int err;
10702+ struct file *h_file;
10703+
10704+ h_file = au_h_open(dentry, arg->bindex,
10705+ O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
392086de 10706+ /*file*/NULL, /*force_wr*/0);
1facf9fc 10707+ err = PTR_ERR(h_file);
10708+ if (IS_ERR(h_file))
10709+ goto out;
10710+
10711+ err = 0;
10712+ if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
c06a8ce3 10713+ && !file_inode(h_file)->i_nlink)
1facf9fc 10714+ goto out_put;
10715+
10716+ do {
10717+ arg->err = 0;
10718+ au_fclr_testempty(arg->flags, CALLED);
10719+ /* smp_mb(); */
392086de 10720+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1facf9fc 10721+ if (err >= 0)
10722+ err = arg->err;
10723+ } while (!err && au_ftest_testempty(arg->flags, CALLED));
10724+
4f0767ce 10725+out_put:
1facf9fc 10726+ fput(h_file);
10727+ au_sbr_put(dentry->d_sb, arg->bindex);
4f0767ce 10728+out:
1facf9fc 10729+ return err;
10730+}
10731+
10732+struct do_test_empty_args {
10733+ int *errp;
10734+ struct dentry *dentry;
10735+ struct test_empty_arg *arg;
10736+};
10737+
10738+static void call_do_test_empty(void *args)
10739+{
10740+ struct do_test_empty_args *a = args;
10741+ *a->errp = do_test_empty(a->dentry, a->arg);
10742+}
10743+
10744+static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10745+{
10746+ int err, wkq_err;
10747+ struct dentry *h_dentry;
10748+ struct inode *h_inode;
10749+
10750+ h_dentry = au_h_dptr(dentry, arg->bindex);
5527c038 10751+ h_inode = d_inode(h_dentry);
53392da6 10752+ /* todo: i_mode changes anytime? */
be118d29 10753+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 10754+ err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
3c1bdaff 10755+ inode_unlock_shared(h_inode);
1facf9fc 10756+ if (!err)
10757+ err = do_test_empty(dentry, arg);
10758+ else {
10759+ struct do_test_empty_args args = {
10760+ .errp = &err,
10761+ .dentry = dentry,
10762+ .arg = arg
10763+ };
10764+ unsigned int flags = arg->flags;
10765+
10766+ wkq_err = au_wkq_wait(call_do_test_empty, &args);
10767+ if (unlikely(wkq_err))
10768+ err = wkq_err;
10769+ arg->flags = flags;
10770+ }
10771+
10772+ return err;
10773+}
10774+
10775+int au_test_empty_lower(struct dentry *dentry)
10776+{
10777+ int err;
1308ab2a 10778+ unsigned int rdhash;
5afbbe0d 10779+ aufs_bindex_t bindex, btop, btail;
1308ab2a 10780+ struct au_nhash whlist;
392086de
AM
10781+ struct test_empty_arg arg = {
10782+ .ctx = {
2000de60 10783+ .actor = test_empty_cb
392086de
AM
10784+ }
10785+ };
076b876e 10786+ int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
1facf9fc 10787+
dece6358
AM
10788+ SiMustAnyLock(dentry->d_sb);
10789+
1308ab2a 10790+ rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10791+ if (!rdhash)
10792+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10793+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
dece6358 10794+ if (unlikely(err))
1facf9fc 10795+ goto out;
10796+
1facf9fc 10797+ arg.flags = 0;
1308ab2a 10798+ arg.whlist = &whlist;
5afbbe0d 10799+ btop = au_dbtop(dentry);
dece6358
AM
10800+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10801+ au_fset_testempty(arg.flags, SHWH);
076b876e
AM
10802+ test_empty = do_test_empty;
10803+ if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10804+ test_empty = sio_test_empty;
5afbbe0d 10805+ arg.bindex = btop;
076b876e 10806+ err = test_empty(dentry, &arg);
1facf9fc 10807+ if (unlikely(err))
10808+ goto out_whlist;
10809+
10810+ au_fset_testempty(arg.flags, WHONLY);
10811+ btail = au_dbtaildir(dentry);
5afbbe0d 10812+ for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
1facf9fc 10813+ struct dentry *h_dentry;
10814+
10815+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10816+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10817+ arg.bindex = bindex;
076b876e 10818+ err = test_empty(dentry, &arg);
1facf9fc 10819+ }
10820+ }
10821+
4f0767ce 10822+out_whlist:
1308ab2a 10823+ au_nhash_wh_free(&whlist);
4f0767ce 10824+out:
1facf9fc 10825+ return err;
10826+}
10827+
10828+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10829+{
10830+ int err;
392086de
AM
10831+ struct test_empty_arg arg = {
10832+ .ctx = {
2000de60 10833+ .actor = test_empty_cb
392086de
AM
10834+ }
10835+ };
1facf9fc 10836+ aufs_bindex_t bindex, btail;
10837+
10838+ err = 0;
1308ab2a 10839+ arg.whlist = whlist;
1facf9fc 10840+ arg.flags = AuTestEmpty_WHONLY;
dece6358
AM
10841+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10842+ au_fset_testempty(arg.flags, SHWH);
1facf9fc 10843+ btail = au_dbtaildir(dentry);
5afbbe0d 10844+ for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
1facf9fc 10845+ struct dentry *h_dentry;
10846+
10847+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10848+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10849+ arg.bindex = bindex;
10850+ err = sio_test_empty(dentry, &arg);
10851+ }
10852+ }
10853+
10854+ return err;
10855+}
10856+
10857+/* ---------------------------------------------------------------------- */
10858+
10859+const struct file_operations aufs_dir_fop = {
4a4d8108 10860+ .owner = THIS_MODULE,
027c5e7a 10861+ .llseek = default_llseek,
1facf9fc 10862+ .read = generic_read_dir,
5afbbe0d 10863+ .iterate_shared = aufs_iterate_shared,
1facf9fc 10864+ .unlocked_ioctl = aufs_ioctl_dir,
b752ccd1
AM
10865+#ifdef CONFIG_COMPAT
10866+ .compat_ioctl = aufs_compat_ioctl_dir,
10867+#endif
1facf9fc 10868+ .open = aufs_open_dir,
10869+ .release = aufs_release_dir,
4a4d8108 10870+ .flush = aufs_flush_dir,
1facf9fc 10871+ .fsync = aufs_fsync_dir
10872+};
7f207e10
AM
10873diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10874--- /usr/share/empty/fs/aufs/dir.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 10875+++ linux/fs/aufs/dir.h 2018-08-12 23:43:05.453457863 +0200
062440b3
AM
10876@@ -0,0 +1,132 @@
10877+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 10878+/*
b00004a5 10879+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 10880+ *
10881+ * This program, aufs is free software; you can redistribute it and/or modify
10882+ * it under the terms of the GNU General Public License as published by
10883+ * the Free Software Foundation; either version 2 of the License, or
10884+ * (at your option) any later version.
dece6358
AM
10885+ *
10886+ * This program is distributed in the hope that it will be useful,
10887+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10888+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10889+ * GNU General Public License for more details.
10890+ *
10891+ * You should have received a copy of the GNU General Public License
523b37e3 10892+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10893+ */
10894+
10895+/*
10896+ * directory operations
10897+ */
10898+
10899+#ifndef __AUFS_DIR_H__
10900+#define __AUFS_DIR_H__
10901+
10902+#ifdef __KERNEL__
10903+
10904+#include <linux/fs.h>
1facf9fc 10905+
10906+/* ---------------------------------------------------------------------- */
10907+
10908+/* need to be faster and smaller */
10909+
10910+struct au_nhash {
dece6358
AM
10911+ unsigned int nh_num;
10912+ struct hlist_head *nh_head;
1facf9fc 10913+};
10914+
10915+struct au_vdir_destr {
10916+ unsigned char len;
10917+ unsigned char name[0];
10918+} __packed;
10919+
10920+struct au_vdir_dehstr {
10921+ struct hlist_node hash;
1c60b727 10922+ struct au_vdir_destr *str;
4a4d8108 10923+} ____cacheline_aligned_in_smp;
1facf9fc 10924+
10925+struct au_vdir_de {
10926+ ino_t de_ino;
10927+ unsigned char de_type;
10928+ /* caution: packed */
10929+ struct au_vdir_destr de_str;
10930+} __packed;
10931+
10932+struct au_vdir_wh {
10933+ struct hlist_node wh_hash;
dece6358
AM
10934+#ifdef CONFIG_AUFS_SHWH
10935+ ino_t wh_ino;
1facf9fc 10936+ aufs_bindex_t wh_bindex;
dece6358
AM
10937+ unsigned char wh_type;
10938+#else
10939+ aufs_bindex_t wh_bindex;
10940+#endif
10941+ /* caution: packed */
1facf9fc 10942+ struct au_vdir_destr wh_str;
10943+} __packed;
10944+
10945+union au_vdir_deblk_p {
10946+ unsigned char *deblk;
10947+ struct au_vdir_de *de;
10948+};
10949+
10950+struct au_vdir {
10951+ unsigned char **vd_deblk;
10952+ unsigned long vd_nblk;
1facf9fc 10953+ struct {
10954+ unsigned long ul;
10955+ union au_vdir_deblk_p p;
10956+ } vd_last;
10957+
be118d29 10958+ u64 vd_version;
dece6358 10959+ unsigned int vd_deblk_sz;
1c60b727 10960+ unsigned long vd_jiffy;
4a4d8108 10961+} ____cacheline_aligned_in_smp;
1facf9fc 10962+
10963+/* ---------------------------------------------------------------------- */
10964+
10965+/* dir.c */
10966+extern const struct file_operations aufs_dir_fop;
10967+void au_add_nlink(struct inode *dir, struct inode *h_dir);
10968+void au_sub_nlink(struct inode *dir, struct inode *h_dir);
1308ab2a 10969+loff_t au_dir_size(struct file *file, struct dentry *dentry);
b912730e 10970+void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
1facf9fc 10971+int au_test_empty_lower(struct dentry *dentry);
10972+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10973+
10974+/* vdir.c */
1308ab2a 10975+unsigned int au_rdhash_est(loff_t sz);
dece6358
AM
10976+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10977+void au_nhash_wh_free(struct au_nhash *whlist);
1facf9fc 10978+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10979+ int limit);
dece6358
AM
10980+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10981+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10982+ unsigned int d_type, aufs_bindex_t bindex,
10983+ unsigned char shwh);
1c60b727 10984+void au_vdir_free(struct au_vdir *vdir);
1facf9fc 10985+int au_vdir_init(struct file *file);
392086de 10986+int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
1facf9fc 10987+
10988+/* ioctl.c */
10989+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10990+
1308ab2a 10991+#ifdef CONFIG_AUFS_RDU
10992+/* rdu.c */
10993+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
10994+#ifdef CONFIG_COMPAT
10995+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10996+ unsigned long arg);
10997+#endif
1308ab2a 10998+#else
c1595e42
JR
10999+AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
11000+ unsigned int cmd, unsigned long arg)
b752ccd1 11001+#ifdef CONFIG_COMPAT
c1595e42
JR
11002+AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
11003+ unsigned int cmd, unsigned long arg)
b752ccd1 11004+#endif
1308ab2a 11005+#endif
11006+
1facf9fc 11007+#endif /* __KERNEL__ */
11008+#endif /* __AUFS_DIR_H__ */
8b6a4947
AM
11009diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
11010--- /usr/share/empty/fs/aufs/dirren.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 11011+++ linux/fs/aufs/dirren.c 2018-08-12 23:43:05.453457863 +0200
062440b3 11012@@ -0,0 +1,1316 @@
cd7a4cd9 11013+// SPDX-License-Identifier: GPL-2.0
8b6a4947 11014+/*
b00004a5 11015+ * Copyright (C) 2017-2018 Junjiro R. Okajima
8b6a4947
AM
11016+ *
11017+ * This program, aufs is free software; you can redistribute it and/or modify
11018+ * it under the terms of the GNU General Public License as published by
11019+ * the Free Software Foundation; either version 2 of the License, or
11020+ * (at your option) any later version.
11021+ *
11022+ * This program is distributed in the hope that it will be useful,
11023+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11024+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11025+ * GNU General Public License for more details.
11026+ *
11027+ * You should have received a copy of the GNU General Public License
11028+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
11029+ */
11030+
11031+/*
11032+ * special handling in renaming a directoy
11033+ * in order to support looking-up the before-renamed name on the lower readonly
11034+ * branches
11035+ */
11036+
11037+#include <linux/byteorder/generic.h>
11038+#include "aufs.h"
11039+
11040+static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
11041+{
11042+ int idx;
11043+
11044+ idx = au_dr_ihash(ent->dr_h_ino);
11045+ au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
11046+}
11047+
11048+static int au_dr_hino_test_empty(struct au_dr_br *dr)
11049+{
11050+ int ret, i;
11051+ struct hlist_bl_head *hbl;
11052+
11053+ ret = 1;
11054+ for (i = 0; ret && i < AuDirren_NHASH; i++) {
11055+ hbl = dr->dr_h_ino + i;
11056+ hlist_bl_lock(hbl);
11057+ ret &= hlist_bl_empty(hbl);
11058+ hlist_bl_unlock(hbl);
11059+ }
11060+
11061+ return ret;
11062+}
11063+
11064+static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
11065+{
11066+ struct au_dr_hino *found, *ent;
11067+ struct hlist_bl_head *hbl;
11068+ struct hlist_bl_node *pos;
11069+ int idx;
11070+
11071+ found = NULL;
11072+ idx = au_dr_ihash(ino);
11073+ hbl = dr->dr_h_ino + idx;
11074+ hlist_bl_lock(hbl);
11075+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11076+ if (ent->dr_h_ino == ino) {
11077+ found = ent;
11078+ break;
11079+ }
11080+ hlist_bl_unlock(hbl);
11081+
11082+ return found;
11083+}
11084+
11085+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
11086+ struct au_dr_hino *add_ent)
11087+{
11088+ int found, idx;
11089+ struct hlist_bl_head *hbl;
11090+ struct hlist_bl_node *pos;
11091+ struct au_dr_hino *ent;
11092+
11093+ found = 0;
11094+ idx = au_dr_ihash(ino);
11095+ hbl = dr->dr_h_ino + idx;
11096+#if 0
11097+ {
11098+ struct hlist_bl_node *tmp;
11099+
11100+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11101+ AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
11102+ }
11103+#endif
11104+ hlist_bl_lock(hbl);
11105+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11106+ if (ent->dr_h_ino == ino) {
11107+ found = 1;
11108+ break;
11109+ }
11110+ if (!found && add_ent)
11111+ hlist_bl_add_head(&add_ent->dr_hnode, hbl);
11112+ hlist_bl_unlock(hbl);
11113+
11114+ if (!found && add_ent)
11115+ AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
11116+
11117+ return found;
11118+}
11119+
11120+void au_dr_hino_free(struct au_dr_br *dr)
11121+{
11122+ int i;
11123+ struct hlist_bl_head *hbl;
11124+ struct hlist_bl_node *pos, *tmp;
11125+ struct au_dr_hino *ent;
11126+
11127+ /* SiMustWriteLock(sb); */
11128+
11129+ for (i = 0; i < AuDirren_NHASH; i++) {
11130+ hbl = dr->dr_h_ino + i;
11131+ /* no spinlock since sbinfo must be write-locked */
11132+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11133+ kfree(ent);
11134+ INIT_HLIST_BL_HEAD(hbl);
11135+ }
11136+}
11137+
11138+/* returns the number of inodes or an error */
11139+static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
11140+ struct file *hinofile)
11141+{
11142+ int err, i;
11143+ ssize_t ssz;
11144+ loff_t pos, oldsize;
11145+ __be64 u64;
11146+ struct inode *hinoinode;
11147+ struct hlist_bl_head *hbl;
11148+ struct hlist_bl_node *n1, *n2;
11149+ struct au_dr_hino *ent;
11150+
11151+ SiMustWriteLock(sb);
11152+ AuDebugOn(!au_br_writable(br->br_perm));
11153+
11154+ hinoinode = file_inode(hinofile);
11155+ oldsize = i_size_read(hinoinode);
11156+
11157+ err = 0;
11158+ pos = 0;
11159+ hbl = br->br_dirren.dr_h_ino;
11160+ for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11161+ /* no bit-lock since sbinfo must be write-locked */
11162+ hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11163+ AuDbg("hi%llu, %pD2\n",
11164+ (unsigned long long)ent->dr_h_ino, hinofile);
11165+ u64 = cpu_to_be64(ent->dr_h_ino);
11166+ ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11167+ if (ssz == sizeof(u64))
11168+ continue;
11169+
11170+ /* write error */
11171+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11172+ err = -ENOSPC;
11173+ if (ssz < 0)
11174+ err = ssz;
11175+ break;
11176+ }
11177+ }
11178+ /* regardless the error */
11179+ if (pos < oldsize) {
11180+ err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11181+ AuTraceErr(err);
11182+ }
11183+
11184+ AuTraceErr(err);
11185+ return err;
11186+}
11187+
11188+static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11189+{
11190+ int err, hidx;
11191+ ssize_t ssz;
11192+ size_t sz, n;
11193+ loff_t pos;
11194+ uint64_t u64;
11195+ struct au_dr_hino *ent;
11196+ struct inode *hinoinode;
11197+ struct hlist_bl_head *hbl;
11198+
11199+ err = 0;
11200+ pos = 0;
11201+ hbl = dr->dr_h_ino;
11202+ hinoinode = file_inode(hinofile);
11203+ sz = i_size_read(hinoinode);
11204+ AuDebugOn(sz % sizeof(u64));
11205+ n = sz / sizeof(u64);
11206+ while (n--) {
11207+ ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11208+ if (unlikely(ssz != sizeof(u64))) {
11209+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11210+ err = -EINVAL;
11211+ if (ssz < 0)
11212+ err = ssz;
11213+ goto out_free;
11214+ }
11215+
11216+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11217+ if (!ent) {
11218+ err = -ENOMEM;
11219+ AuTraceErr(err);
11220+ goto out_free;
11221+ }
11222+ ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11223+ AuDbg("hi%llu, %pD2\n",
11224+ (unsigned long long)ent->dr_h_ino, hinofile);
11225+ hidx = au_dr_ihash(ent->dr_h_ino);
11226+ au_hbl_add(&ent->dr_hnode, hbl + hidx);
11227+ }
11228+ goto out; /* success */
11229+
11230+out_free:
11231+ au_dr_hino_free(dr);
11232+out:
11233+ AuTraceErr(err);
11234+ return err;
11235+}
11236+
11237+/*
11238+ * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11239+ * @path is a switch to distinguish load and store.
11240+ */
11241+static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11242+ struct au_branch *br, const struct path *path)
11243+{
11244+ int err, flags;
11245+ unsigned char load, suspend;
11246+ struct file *hinofile;
11247+ struct au_hinode *hdir;
11248+ struct inode *dir, *delegated;
11249+ struct path hinopath;
11250+ struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11251+ sizeof(AUFS_WH_DR_BRHINO) - 1);
11252+
11253+ AuDebugOn(bindex < 0 && !br);
11254+ AuDebugOn(bindex >= 0 && br);
11255+
11256+ err = -EINVAL;
11257+ suspend = !br;
11258+ if (suspend)
11259+ br = au_sbr(sb, bindex);
11260+ load = !!path;
11261+ if (!load) {
11262+ path = &br->br_path;
11263+ AuDebugOn(!au_br_writable(br->br_perm));
11264+ if (unlikely(!au_br_writable(br->br_perm)))
11265+ goto out;
11266+ }
11267+
11268+ hdir = NULL;
11269+ if (suspend) {
11270+ dir = d_inode(sb->s_root);
11271+ hdir = au_hinode(au_ii(dir), bindex);
11272+ dir = hdir->hi_inode;
11273+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11274+ } else {
11275+ dir = d_inode(path->dentry);
11276+ inode_lock_nested(dir, AuLsc_I_CHILD);
11277+ }
11278+ hinopath.dentry = vfsub_lkup_one(&hinoname, path->dentry);
11279+ err = PTR_ERR(hinopath.dentry);
11280+ if (IS_ERR(hinopath.dentry))
11281+ goto out_unlock;
11282+
11283+ err = 0;
11284+ flags = O_RDONLY;
11285+ if (load) {
11286+ if (d_is_negative(hinopath.dentry))
11287+ goto out_dput; /* success */
11288+ } else {
11289+ if (au_dr_hino_test_empty(&br->br_dirren)) {
11290+ if (d_is_positive(hinopath.dentry)) {
11291+ delegated = NULL;
11292+ err = vfsub_unlink(dir, &hinopath, &delegated,
11293+ /*force*/0);
11294+ AuTraceErr(err);
11295+ if (unlikely(err))
11296+ pr_err("ignored err %d, %pd2\n",
11297+ err, hinopath.dentry);
11298+ if (unlikely(err == -EWOULDBLOCK))
11299+ iput(delegated);
11300+ err = 0;
11301+ }
11302+ goto out_dput;
11303+ } else if (!d_is_positive(hinopath.dentry)) {
11304+ err = vfsub_create(dir, &hinopath, 0600,
11305+ /*want_excl*/false);
11306+ AuTraceErr(err);
11307+ if (unlikely(err))
11308+ goto out_dput;
11309+ }
11310+ flags = O_WRONLY;
11311+ }
11312+ hinopath.mnt = path->mnt;
11313+ hinofile = vfsub_dentry_open(&hinopath, flags);
11314+ if (suspend)
11315+ au_hn_inode_unlock(hdir);
11316+ else
11317+ inode_unlock(dir);
11318+ dput(hinopath.dentry);
11319+ AuTraceErrPtr(hinofile);
11320+ if (IS_ERR(hinofile)) {
11321+ err = PTR_ERR(hinofile);
11322+ goto out;
11323+ }
11324+
11325+ if (load)
11326+ err = au_dr_hino_load(&br->br_dirren, hinofile);
11327+ else
11328+ err = au_dr_hino_store(sb, br, hinofile);
11329+ fput(hinofile);
11330+ goto out;
11331+
11332+out_dput:
11333+ dput(hinopath.dentry);
11334+out_unlock:
11335+ if (suspend)
11336+ au_hn_inode_unlock(hdir);
11337+ else
11338+ inode_unlock(dir);
11339+out:
11340+ AuTraceErr(err);
11341+ return err;
11342+}
11343+
11344+/* ---------------------------------------------------------------------- */
11345+
11346+static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11347+{
11348+ int err;
11349+ struct kstatfs kstfs;
11350+ dev_t dev;
11351+ struct dentry *dentry;
11352+ struct super_block *sb;
11353+
11354+ err = vfs_statfs((void *)path, &kstfs);
11355+ AuTraceErr(err);
11356+ if (unlikely(err))
11357+ goto out;
11358+
11359+ /* todo: support for UUID */
11360+
11361+ if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11362+ brid->type = AuBrid_FSID;
11363+ brid->fsid = kstfs.f_fsid;
11364+ } else {
11365+ dentry = path->dentry;
11366+ sb = dentry->d_sb;
11367+ dev = sb->s_dev;
11368+ if (dev) {
11369+ brid->type = AuBrid_DEV;
11370+ brid->dev = dev;
11371+ }
11372+ }
11373+
11374+out:
11375+ return err;
11376+}
11377+
11378+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11379+ const struct path *path)
11380+{
11381+ int err, i;
11382+ struct au_dr_br *dr;
11383+ struct hlist_bl_head *hbl;
11384+
11385+ dr = &br->br_dirren;
11386+ hbl = dr->dr_h_ino;
11387+ for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11388+ INIT_HLIST_BL_HEAD(hbl);
11389+
11390+ err = au_dr_brid_init(&dr->dr_brid, path);
11391+ if (unlikely(err))
11392+ goto out;
11393+
11394+ if (au_opt_test(au_mntflags(sb), DIRREN))
11395+ err = au_dr_hino(sb, /*bindex*/-1, br, path);
11396+
11397+out:
11398+ AuTraceErr(err);
11399+ return err;
11400+}
11401+
11402+int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11403+{
11404+ int err;
11405+
11406+ err = 0;
11407+ if (au_br_writable(br->br_perm))
11408+ err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11409+ if (!err)
11410+ au_dr_hino_free(&br->br_dirren);
11411+
11412+ return err;
11413+}
11414+
11415+/* ---------------------------------------------------------------------- */
11416+
11417+static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11418+ char *buf, size_t sz)
11419+{
11420+ int err;
11421+ unsigned int major, minor;
11422+ char *p;
11423+
11424+ p = buf;
11425+ err = snprintf(p, sz, "%d_", brid->type);
11426+ AuDebugOn(err > sz);
11427+ p += err;
11428+ sz -= err;
11429+ switch (brid->type) {
11430+ case AuBrid_Unset:
11431+ return -EINVAL;
11432+ case AuBrid_UUID:
11433+ err = snprintf(p, sz, "%pU", brid->uuid.b);
11434+ break;
11435+ case AuBrid_FSID:
11436+ err = snprintf(p, sz, "%08x-%08x",
11437+ brid->fsid.val[0], brid->fsid.val[1]);
11438+ break;
11439+ case AuBrid_DEV:
11440+ major = MAJOR(brid->dev);
11441+ minor = MINOR(brid->dev);
11442+ if (major <= 0xff && minor <= 0xff)
11443+ err = snprintf(p, sz, "%02x%02x", major, minor);
11444+ else
11445+ err = snprintf(p, sz, "%03x:%05x", major, minor);
11446+ break;
11447+ }
11448+ AuDebugOn(err > sz);
11449+ p += err;
11450+ sz -= err;
11451+ err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11452+ AuDebugOn(err > sz);
11453+ p += err;
11454+ sz -= err;
11455+
11456+ return p - buf;
11457+}
11458+
11459+static int au_drinfo_name(struct au_branch *br, char *name, int len)
11460+{
11461+ int rlen;
11462+ struct dentry *br_dentry;
11463+ struct inode *br_inode;
11464+
11465+ br_dentry = au_br_dentry(br);
11466+ br_inode = d_inode(br_dentry);
11467+ rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11468+ AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11469+ AuDebugOn(rlen > len);
11470+
11471+ return rlen;
11472+}
11473+
11474+/* ---------------------------------------------------------------------- */
11475+
11476+/*
11477+ * from the given @h_dentry, construct drinfo at @*fdata.
11478+ * when the size of @*fdata is not enough, reallocate and return new @fdata and
11479+ * @allocated.
11480+ */
11481+static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11482+ struct dentry *h_dentry,
11483+ unsigned char *allocated)
11484+{
11485+ int err, v;
11486+ struct au_drinfo_fdata *f, *p;
11487+ struct au_drinfo *drinfo;
11488+ struct inode *h_inode;
11489+ struct qstr *qname;
11490+
11491+ err = 0;
11492+ f = *fdata;
11493+ h_inode = d_inode(h_dentry);
11494+ qname = &h_dentry->d_name;
11495+ drinfo = &f->drinfo;
11496+ drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11497+ drinfo->oldnamelen = qname->len;
11498+ if (*allocated < sizeof(*f) + qname->len) {
11499+ v = roundup_pow_of_two(*allocated + qname->len);
11500+ p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11501+ if (unlikely(!p)) {
11502+ err = -ENOMEM;
11503+ AuTraceErr(err);
11504+ goto out;
11505+ }
11506+ f = p;
11507+ *fdata = f;
11508+ *allocated = v;
11509+ drinfo = &f->drinfo;
11510+ }
11511+ memcpy(drinfo->oldname, qname->name, qname->len);
11512+ AuDbg("i%llu, %.*s\n",
11513+ be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11514+ drinfo->oldname);
11515+
11516+out:
11517+ AuTraceErr(err);
11518+ return err;
11519+}
11520+
11521+/* callers have to free the return value */
11522+static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11523+{
11524+ struct au_drinfo *ret, *drinfo;
11525+ struct au_drinfo_fdata fdata;
11526+ int len;
11527+ loff_t pos;
11528+ ssize_t ssz;
11529+
11530+ ret = ERR_PTR(-EIO);
11531+ pos = 0;
11532+ ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11533+ if (unlikely(ssz != sizeof(fdata))) {
11534+ AuIOErr("ssz %zd, %u, %pD2\n",
11535+ ssz, (unsigned int)sizeof(fdata), file);
11536+ goto out;
11537+ }
11538+
11539+ fdata.magic = ntohl((__force __be32)fdata.magic);
11540+ switch (fdata.magic) {
11541+ case AUFS_DRINFO_MAGIC_V1:
11542+ break;
11543+ default:
11544+ AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11545+ fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11546+ goto out;
11547+ }
11548+
11549+ drinfo = &fdata.drinfo;
11550+ len = drinfo->oldnamelen;
11551+ if (!len) {
11552+ AuIOErr("broken drinfo %pD2\n", file);
11553+ goto out;
11554+ }
11555+
11556+ ret = NULL;
11557+ drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11558+ if (unlikely(h_ino && drinfo->ino != h_ino)) {
11559+ AuDbg("ignored i%llu, i%llu, %pD2\n",
11560+ (unsigned long long)drinfo->ino,
11561+ (unsigned long long)h_ino, file);
11562+ goto out; /* success */
11563+ }
11564+
11565+ ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11566+ if (unlikely(!ret)) {
11567+ ret = ERR_PTR(-ENOMEM);
11568+ AuTraceErrPtr(ret);
11569+ goto out;
11570+ }
11571+
11572+ *ret = *drinfo;
11573+ ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11574+ if (unlikely(ssz != len)) {
11575+ kfree(ret);
11576+ ret = ERR_PTR(-EIO);
11577+ AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11578+ goto out;
11579+ }
11580+
11581+ AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11582+
11583+out:
11584+ return ret;
11585+}
11586+
11587+/* ---------------------------------------------------------------------- */
11588+
11589+/* in order to be revertible */
11590+struct au_drinfo_rev_elm {
11591+ int created;
11592+ struct dentry *info_dentry;
11593+ struct au_drinfo *info_last;
11594+};
11595+
11596+struct au_drinfo_rev {
11597+ unsigned char already;
11598+ aufs_bindex_t nelm;
11599+ struct au_drinfo_rev_elm elm[0];
11600+};
11601+
11602+/* todo: isn't it too large? */
11603+struct au_drinfo_store {
11604+ struct path h_ppath;
11605+ struct dentry *h_dentry;
11606+ struct au_drinfo_fdata *fdata;
11607+ char *infoname; /* inside of whname, just after PFX */
11608+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11609+ aufs_bindex_t btgt, btail;
11610+ unsigned char no_sio,
11611+ allocated, /* current size of *fdata */
11612+ infonamelen, /* room size for p */
11613+ whnamelen, /* length of the genarated name */
11614+ renameback; /* renamed back */
11615+};
11616+
11617+/* on rename(2) error, the caller should revert it using @elm */
11618+static int au_drinfo_do_store(struct au_drinfo_store *w,
11619+ struct au_drinfo_rev_elm *elm)
11620+{
11621+ int err, len;
11622+ ssize_t ssz;
11623+ loff_t pos;
11624+ struct path infopath = {
11625+ .mnt = w->h_ppath.mnt
11626+ };
11627+ struct inode *h_dir, *h_inode, *delegated;
11628+ struct file *infofile;
11629+ struct qstr *qname;
11630+
11631+ AuDebugOn(elm
11632+ && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11633+
11634+ infopath.dentry = vfsub_lookup_one_len(w->whname, w->h_ppath.dentry,
11635+ w->whnamelen);
11636+ AuTraceErrPtr(infopath.dentry);
11637+ if (IS_ERR(infopath.dentry)) {
11638+ err = PTR_ERR(infopath.dentry);
11639+ goto out;
11640+ }
11641+
11642+ err = 0;
11643+ h_dir = d_inode(w->h_ppath.dentry);
11644+ if (elm && d_is_negative(infopath.dentry)) {
11645+ err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11646+ AuTraceErr(err);
11647+ if (unlikely(err))
11648+ goto out_dput;
11649+ elm->created = 1;
11650+ elm->info_dentry = dget(infopath.dentry);
11651+ }
11652+
11653+ infofile = vfsub_dentry_open(&infopath, O_RDWR);
11654+ AuTraceErrPtr(infofile);
11655+ if (IS_ERR(infofile)) {
11656+ err = PTR_ERR(infofile);
11657+ goto out_dput;
11658+ }
11659+
11660+ h_inode = d_inode(infopath.dentry);
11661+ if (elm && i_size_read(h_inode)) {
11662+ h_inode = d_inode(w->h_dentry);
11663+ elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11664+ AuTraceErrPtr(elm->info_last);
11665+ if (IS_ERR(elm->info_last)) {
11666+ err = PTR_ERR(elm->info_last);
11667+ elm->info_last = NULL;
11668+ AuDebugOn(elm->info_dentry);
11669+ goto out_fput;
11670+ }
11671+ }
11672+
11673+ if (elm && w->renameback) {
11674+ delegated = NULL;
11675+ err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11676+ AuTraceErr(err);
11677+ if (unlikely(err == -EWOULDBLOCK))
11678+ iput(delegated);
11679+ goto out_fput;
11680+ }
11681+
11682+ pos = 0;
11683+ qname = &w->h_dentry->d_name;
11684+ len = sizeof(*w->fdata) + qname->len;
11685+ if (!elm)
11686+ len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11687+ ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11688+ if (ssz == len) {
11689+ AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11690+ w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11691+ goto out_fput; /* success */
11692+ } else {
11693+ err = -EIO;
11694+ if (ssz < 0)
11695+ err = ssz;
11696+ /* the caller should revert it using @elm */
11697+ }
11698+
11699+out_fput:
11700+ fput(infofile);
11701+out_dput:
11702+ dput(infopath.dentry);
11703+out:
11704+ AuTraceErr(err);
11705+ return err;
11706+}
11707+
11708+struct au_call_drinfo_do_store_args {
11709+ int *errp;
11710+ struct au_drinfo_store *w;
11711+ struct au_drinfo_rev_elm *elm;
11712+};
11713+
11714+static void au_call_drinfo_do_store(void *args)
11715+{
11716+ struct au_call_drinfo_do_store_args *a = args;
11717+
11718+ *a->errp = au_drinfo_do_store(a->w, a->elm);
11719+}
11720+
11721+static int au_drinfo_store_sio(struct au_drinfo_store *w,
11722+ struct au_drinfo_rev_elm *elm)
11723+{
11724+ int err, wkq_err;
11725+
11726+ if (w->no_sio)
11727+ err = au_drinfo_do_store(w, elm);
11728+ else {
11729+ struct au_call_drinfo_do_store_args a = {
11730+ .errp = &err,
11731+ .w = w,
11732+ .elm = elm
11733+ };
11734+ wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11735+ if (unlikely(wkq_err))
11736+ err = wkq_err;
11737+ }
11738+ AuTraceErr(err);
11739+
11740+ return err;
11741+}
11742+
11743+static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11744+ aufs_bindex_t btgt)
11745+{
11746+ int err;
11747+
11748+ memset(w, 0, sizeof(*w));
11749+ w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11750+ strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11751+ w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11752+ w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11753+ w->btgt = btgt;
11754+ w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11755+
11756+ err = -ENOMEM;
11757+ w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11758+ if (unlikely(!w->fdata)) {
11759+ AuTraceErr(err);
11760+ goto out;
11761+ }
11762+ w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11763+ err = 0;
11764+
11765+out:
11766+ return err;
11767+}
11768+
11769+static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11770+{
11771+ kfree(w->fdata);
11772+}
11773+
11774+static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11775+ struct au_drinfo_store *w)
11776+{
11777+ struct au_drinfo_rev_elm *elm;
11778+ struct inode *h_dir, *delegated;
11779+ int err, nelm;
11780+ struct path infopath = {
11781+ .mnt = w->h_ppath.mnt
11782+ };
11783+
11784+ h_dir = d_inode(w->h_ppath.dentry);
11785+ IMustLock(h_dir);
11786+
11787+ err = 0;
11788+ elm = rev->elm;
11789+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11790+ AuDebugOn(elm->created && elm->info_last);
11791+ if (elm->created) {
11792+ AuDbg("here\n");
11793+ delegated = NULL;
11794+ infopath.dentry = elm->info_dentry;
11795+ err = vfsub_unlink(h_dir, &infopath, &delegated,
11796+ !w->no_sio);
11797+ AuTraceErr(err);
11798+ if (unlikely(err == -EWOULDBLOCK))
11799+ iput(delegated);
11800+ dput(elm->info_dentry);
11801+ } else if (elm->info_last) {
11802+ AuDbg("here\n");
11803+ w->fdata->drinfo = *elm->info_last;
11804+ memcpy(w->fdata->drinfo.oldname,
11805+ elm->info_last->oldname,
11806+ elm->info_last->oldnamelen);
11807+ err = au_drinfo_store_sio(w, /*elm*/NULL);
11808+ kfree(elm->info_last);
11809+ }
11810+ if (unlikely(err))
11811+ AuIOErr("%d, %s\n", err, w->whname);
11812+ /* go on even if err */
11813+ }
11814+}
11815+
11816+/* caller has to call au_dr_rename_fin() later */
11817+static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11818+ struct qstr *dst_name, void *_rev)
11819+{
11820+ int err, sz, nelm;
11821+ aufs_bindex_t bindex, btail;
11822+ struct au_drinfo_store work;
11823+ struct au_drinfo_rev *rev, **p;
11824+ struct au_drinfo_rev_elm *elm;
11825+ struct super_block *sb;
11826+ struct au_branch *br;
11827+ struct au_hinode *hdir;
11828+
11829+ err = au_drinfo_store_work_init(&work, btgt);
11830+ AuTraceErr(err);
11831+ if (unlikely(err))
11832+ goto out;
11833+
11834+ err = -ENOMEM;
11835+ btail = au_dbtaildir(dentry);
11836+ nelm = btail - btgt;
11837+ sz = sizeof(*rev) + sizeof(*elm) * nelm;
11838+ rev = kcalloc(1, sz, GFP_NOFS);
11839+ if (unlikely(!rev)) {
11840+ AuTraceErr(err);
11841+ goto out_args;
11842+ }
11843+ rev->nelm = nelm;
11844+ elm = rev->elm;
11845+ p = _rev;
11846+ *p = rev;
11847+
11848+ err = 0;
11849+ sb = dentry->d_sb;
11850+ work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11851+ work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11852+ hdir = au_hi(d_inode(dentry), btgt);
11853+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11854+ for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11855+ work.h_dentry = au_h_dptr(dentry, bindex);
11856+ if (!work.h_dentry)
11857+ continue;
11858+
11859+ err = au_drinfo_construct(&work.fdata, work.h_dentry,
11860+ &work.allocated);
11861+ AuTraceErr(err);
11862+ if (unlikely(err))
11863+ break;
11864+
11865+ work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11866+ br = au_sbr(sb, bindex);
11867+ work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11868+ work.whnamelen += au_drinfo_name(br, work.infoname,
11869+ work.infonamelen);
11870+ AuDbg("whname %.*s, i%llu, %.*s\n",
11871+ work.whnamelen, work.whname,
11872+ be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11873+ work.fdata->drinfo.oldnamelen,
11874+ work.fdata->drinfo.oldname);
11875+
11876+ err = au_drinfo_store_sio(&work, elm);
11877+ AuTraceErr(err);
11878+ if (unlikely(err))
11879+ break;
11880+ }
11881+ if (unlikely(err)) {
11882+ /* revert all drinfo */
11883+ au_drinfo_store_rev(rev, &work);
11884+ kfree(rev);
11885+ *p = NULL;
11886+ }
11887+ au_hn_inode_unlock(hdir);
11888+
11889+out_args:
11890+ au_drinfo_store_work_fin(&work);
11891+out:
11892+ return err;
11893+}
11894+
11895+/* ---------------------------------------------------------------------- */
11896+
11897+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11898+ struct qstr *dst_name, void *_rev)
11899+{
11900+ int err, already;
11901+ ino_t ino;
11902+ struct super_block *sb;
11903+ struct au_branch *br;
11904+ struct au_dr_br *dr;
11905+ struct dentry *h_dentry;
11906+ struct inode *h_inode;
11907+ struct au_dr_hino *ent;
11908+ struct au_drinfo_rev *rev, **p;
11909+
11910+ AuDbg("bindex %d\n", bindex);
11911+
11912+ err = -ENOMEM;
11913+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11914+ if (unlikely(!ent))
11915+ goto out;
11916+
11917+ sb = src->d_sb;
11918+ br = au_sbr(sb, bindex);
11919+ dr = &br->br_dirren;
11920+ h_dentry = au_h_dptr(src, bindex);
11921+ h_inode = d_inode(h_dentry);
11922+ ino = h_inode->i_ino;
11923+ ent->dr_h_ino = ino;
11924+ already = au_dr_hino_test_add(dr, ino, ent);
11925+ AuDbg("b%d, hi%llu, already %d\n",
11926+ bindex, (unsigned long long)ino, already);
11927+
11928+ err = au_drinfo_store(src, bindex, dst_name, _rev);
11929+ AuTraceErr(err);
11930+ if (!err) {
11931+ p = _rev;
11932+ rev = *p;
11933+ rev->already = already;
11934+ goto out; /* success */
11935+ }
11936+
11937+ /* revert */
11938+ if (!already)
11939+ au_dr_hino_del(dr, ent);
11940+ kfree(ent);
11941+
11942+out:
11943+ AuTraceErr(err);
11944+ return err;
11945+}
11946+
11947+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11948+{
11949+ struct au_drinfo_rev *rev;
11950+ struct au_drinfo_rev_elm *elm;
11951+ int nelm;
11952+
11953+ rev = _rev;
11954+ elm = rev->elm;
11955+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11956+ dput(elm->info_dentry);
11957+ kfree(elm->info_last);
11958+ }
11959+ kfree(rev);
11960+}
11961+
11962+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11963+{
11964+ int err;
11965+ struct au_drinfo_store work;
11966+ struct au_drinfo_rev *rev = _rev;
11967+ struct super_block *sb;
11968+ struct au_branch *br;
11969+ struct inode *h_inode;
11970+ struct au_dr_br *dr;
11971+ struct au_dr_hino *ent;
11972+
11973+ err = au_drinfo_store_work_init(&work, btgt);
11974+ if (unlikely(err))
11975+ goto out;
11976+
11977+ sb = src->d_sb;
11978+ br = au_sbr(sb, btgt);
11979+ work.h_ppath.dentry = au_h_dptr(src, btgt);
11980+ work.h_ppath.mnt = au_br_mnt(br);
11981+ au_drinfo_store_rev(rev, &work);
11982+ au_drinfo_store_work_fin(&work);
11983+ if (rev->already)
11984+ goto out;
11985+
11986+ dr = &br->br_dirren;
11987+ h_inode = d_inode(work.h_ppath.dentry);
11988+ ent = au_dr_hino_find(dr, h_inode->i_ino);
11989+ BUG_ON(!ent);
11990+ au_dr_hino_del(dr, ent);
11991+ kfree(ent);
11992+
11993+out:
11994+ kfree(rev);
11995+ if (unlikely(err))
11996+ pr_err("failed to remove dirren info\n");
11997+}
11998+
11999+/* ---------------------------------------------------------------------- */
12000+
12001+static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
12002+ char *whname, int whnamelen,
12003+ struct dentry **info_dentry)
12004+{
12005+ struct au_drinfo *drinfo;
12006+ struct file *f;
12007+ struct inode *h_dir;
12008+ struct path infopath;
12009+ int unlocked;
12010+
12011+ AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
12012+
12013+ *info_dentry = NULL;
12014+ drinfo = NULL;
12015+ unlocked = 0;
12016+ h_dir = d_inode(h_ppath->dentry);
be118d29 12017+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8b6a4947
AM
12018+ infopath.dentry = vfsub_lookup_one_len(whname, h_ppath->dentry,
12019+ whnamelen);
12020+ if (IS_ERR(infopath.dentry)) {
12021+ drinfo = (void *)infopath.dentry;
12022+ goto out;
12023+ }
12024+
12025+ if (d_is_negative(infopath.dentry))
12026+ goto out_dput; /* success */
12027+
12028+ infopath.mnt = h_ppath->mnt;
12029+ f = vfsub_dentry_open(&infopath, O_RDONLY);
12030+ inode_unlock_shared(h_dir);
12031+ unlocked = 1;
12032+ if (IS_ERR(f)) {
12033+ drinfo = (void *)f;
12034+ goto out_dput;
12035+ }
12036+
12037+ drinfo = au_drinfo_read_k(f, /*h_ino*/0);
12038+ if (IS_ERR_OR_NULL(drinfo))
12039+ goto out_fput;
12040+
12041+ AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
12042+ *info_dentry = dget(infopath.dentry); /* keep it alive */
12043+
12044+out_fput:
12045+ fput(f);
12046+out_dput:
12047+ dput(infopath.dentry);
12048+out:
12049+ if (!unlocked)
12050+ inode_unlock_shared(h_dir);
12051+ AuTraceErrPtr(drinfo);
12052+ return drinfo;
12053+}
12054+
12055+struct au_drinfo_do_load_args {
12056+ struct au_drinfo **drinfop;
12057+ struct path *h_ppath;
12058+ char *whname;
12059+ int whnamelen;
12060+ struct dentry **info_dentry;
12061+};
12062+
12063+static void au_call_drinfo_do_load(void *args)
12064+{
12065+ struct au_drinfo_do_load_args *a = args;
12066+
12067+ *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
12068+ a->info_dentry);
12069+}
12070+
12071+struct au_drinfo_load {
12072+ struct path h_ppath;
12073+ struct qstr *qname;
12074+ unsigned char no_sio;
12075+
12076+ aufs_bindex_t ninfo;
12077+ struct au_drinfo **drinfo;
12078+};
12079+
12080+static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
12081+ struct au_branch *br)
12082+{
12083+ int err, wkq_err, whnamelen, e;
12084+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
12085+ = AUFS_WH_DR_INFO_PFX;
12086+ struct au_drinfo *drinfo;
12087+ struct qstr oldname;
12088+ struct inode *h_dir, *delegated;
12089+ struct dentry *info_dentry;
12090+ struct path infopath;
12091+
12092+ whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
12093+ whnamelen += au_drinfo_name(br, whname + whnamelen,
12094+ sizeof(whname) - whnamelen);
12095+ if (w->no_sio)
12096+ drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
12097+ &info_dentry);
12098+ else {
12099+ struct au_drinfo_do_load_args args = {
12100+ .drinfop = &drinfo,
12101+ .h_ppath = &w->h_ppath,
12102+ .whname = whname,
12103+ .whnamelen = whnamelen,
12104+ .info_dentry = &info_dentry
12105+ };
12106+ wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
12107+ if (unlikely(wkq_err))
12108+ drinfo = ERR_PTR(wkq_err);
12109+ }
12110+ err = PTR_ERR(drinfo);
12111+ if (IS_ERR_OR_NULL(drinfo))
12112+ goto out;
12113+
12114+ err = 0;
12115+ oldname.len = drinfo->oldnamelen;
12116+ oldname.name = drinfo->oldname;
12117+ if (au_qstreq(w->qname, &oldname)) {
12118+ /* the name is renamed back */
12119+ kfree(drinfo);
12120+ drinfo = NULL;
12121+
12122+ infopath.dentry = info_dentry;
12123+ infopath.mnt = w->h_ppath.mnt;
12124+ h_dir = d_inode(w->h_ppath.dentry);
12125+ delegated = NULL;
12126+ inode_lock_nested(h_dir, AuLsc_I_PARENT);
12127+ e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
12128+ inode_unlock(h_dir);
12129+ if (unlikely(e))
12130+ AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
12131+ if (unlikely(e == -EWOULDBLOCK))
12132+ iput(delegated);
12133+ }
12134+ kfree(w->drinfo[bindex]);
12135+ w->drinfo[bindex] = drinfo;
12136+ dput(info_dentry);
12137+
12138+out:
12139+ AuTraceErr(err);
12140+ return err;
12141+}
12142+
12143+/* ---------------------------------------------------------------------- */
12144+
12145+static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12146+{
12147+ struct au_drinfo **p = drinfo;
12148+
12149+ while (n-- > 0)
12150+ kfree(*drinfo++);
12151+ kfree(p);
12152+}
12153+
12154+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12155+ aufs_bindex_t btgt)
12156+{
12157+ int err, ninfo;
12158+ struct au_drinfo_load w;
12159+ aufs_bindex_t bindex, bbot;
12160+ struct au_branch *br;
12161+ struct inode *h_dir;
12162+ struct au_dr_hino *ent;
12163+ struct super_block *sb;
12164+
12165+ AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12166+ AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12167+ AuLNPair(&lkup->whname), btgt);
12168+
12169+ sb = dentry->d_sb;
12170+ bbot = au_sbbot(sb);
12171+ w.ninfo = bbot + 1;
12172+ if (!lkup->dirren.drinfo) {
12173+ lkup->dirren.drinfo = kcalloc(w.ninfo,
12174+ sizeof(*lkup->dirren.drinfo),
12175+ GFP_NOFS);
12176+ if (unlikely(!lkup->dirren.drinfo)) {
12177+ err = -ENOMEM;
12178+ goto out;
12179+ }
12180+ lkup->dirren.ninfo = w.ninfo;
12181+ }
12182+ w.drinfo = lkup->dirren.drinfo;
12183+ w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12184+ w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12185+ AuDebugOn(!w.h_ppath.dentry);
12186+ w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12187+ w.qname = &dentry->d_name;
12188+
12189+ ninfo = 0;
12190+ for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12191+ br = au_sbr(sb, bindex);
12192+ err = au_drinfo_load(&w, bindex, br);
12193+ if (unlikely(err))
12194+ goto out_free;
12195+ if (w.drinfo[bindex])
12196+ ninfo++;
12197+ }
12198+ if (!ninfo) {
12199+ br = au_sbr(sb, btgt);
12200+ h_dir = d_inode(w.h_ppath.dentry);
12201+ ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12202+ AuDebugOn(!ent);
12203+ au_dr_hino_del(&br->br_dirren, ent);
12204+ kfree(ent);
12205+ }
12206+ goto out; /* success */
12207+
12208+out_free:
12209+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12210+ lkup->dirren.ninfo = 0;
12211+ lkup->dirren.drinfo = NULL;
12212+out:
12213+ AuTraceErr(err);
12214+ return err;
12215+}
12216+
12217+void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12218+{
12219+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12220+}
12221+
12222+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12223+{
12224+ int err;
12225+ struct au_drinfo *drinfo;
12226+
12227+ err = 0;
12228+ if (!lkup->dirren.drinfo)
12229+ goto out;
12230+ AuDebugOn(lkup->dirren.ninfo < btgt + 1);
12231+ drinfo = lkup->dirren.drinfo[btgt + 1];
12232+ if (!drinfo)
12233+ goto out;
12234+
12235+ kfree(lkup->whname.name);
12236+ lkup->whname.name = NULL;
12237+ lkup->dirren.dr_name.len = drinfo->oldnamelen;
12238+ lkup->dirren.dr_name.name = drinfo->oldname;
12239+ lkup->name = &lkup->dirren.dr_name;
12240+ err = au_wh_name_alloc(&lkup->whname, lkup->name);
12241+ if (!err)
12242+ AuDbg("name %.*s, whname %.*s, b%d\n",
12243+ AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12244+ btgt);
12245+
12246+out:
12247+ AuTraceErr(err);
12248+ return err;
12249+}
12250+
12251+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12252+ ino_t h_ino)
12253+{
12254+ int match;
12255+ struct au_drinfo *drinfo;
12256+
12257+ match = 1;
12258+ if (!lkup->dirren.drinfo)
12259+ goto out;
12260+ AuDebugOn(lkup->dirren.ninfo < bindex + 1);
12261+ drinfo = lkup->dirren.drinfo[bindex + 1];
12262+ if (!drinfo)
12263+ goto out;
12264+
12265+ match = (drinfo->ino == h_ino);
12266+ AuDbg("match %d\n", match);
12267+
12268+out:
12269+ return match;
12270+}
12271+
12272+/* ---------------------------------------------------------------------- */
12273+
12274+int au_dr_opt_set(struct super_block *sb)
12275+{
12276+ int err;
12277+ aufs_bindex_t bindex, bbot;
12278+ struct au_branch *br;
12279+
12280+ err = 0;
12281+ bbot = au_sbbot(sb);
12282+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12283+ br = au_sbr(sb, bindex);
12284+ err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12285+ }
12286+
12287+ return err;
12288+}
12289+
12290+int au_dr_opt_flush(struct super_block *sb)
12291+{
12292+ int err;
12293+ aufs_bindex_t bindex, bbot;
12294+ struct au_branch *br;
12295+
12296+ err = 0;
12297+ bbot = au_sbbot(sb);
12298+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12299+ br = au_sbr(sb, bindex);
12300+ if (au_br_writable(br->br_perm))
12301+ err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12302+ }
12303+
12304+ return err;
12305+}
12306+
12307+int au_dr_opt_clr(struct super_block *sb, int no_flush)
12308+{
12309+ int err;
12310+ aufs_bindex_t bindex, bbot;
12311+ struct au_branch *br;
12312+
12313+ err = 0;
12314+ if (!no_flush) {
12315+ err = au_dr_opt_flush(sb);
12316+ if (unlikely(err))
12317+ goto out;
12318+ }
12319+
12320+ bbot = au_sbbot(sb);
12321+ for (bindex = 0; bindex <= bbot; bindex++) {
12322+ br = au_sbr(sb, bindex);
12323+ au_dr_hino_free(&br->br_dirren);
12324+ }
12325+
12326+out:
12327+ return err;
12328+}
12329diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12330--- /usr/share/empty/fs/aufs/dirren.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 12331+++ linux/fs/aufs/dirren.h 2018-08-12 23:43:05.453457863 +0200
062440b3
AM
12332@@ -0,0 +1,140 @@
12333+/* SPDX-License-Identifier: GPL-2.0 */
8b6a4947 12334+/*
b00004a5 12335+ * Copyright (C) 2017-2018 Junjiro R. Okajima
8b6a4947
AM
12336+ *
12337+ * This program, aufs is free software; you can redistribute it and/or modify
12338+ * it under the terms of the GNU General Public License as published by
12339+ * the Free Software Foundation; either version 2 of the License, or
12340+ * (at your option) any later version.
12341+ *
12342+ * This program is distributed in the hope that it will be useful,
12343+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12344+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12345+ * GNU General Public License for more details.
12346+ *
12347+ * You should have received a copy of the GNU General Public License
12348+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
12349+ */
12350+
12351+/*
12352+ * renamed dir info
12353+ */
12354+
12355+#ifndef __AUFS_DIRREN_H__
12356+#define __AUFS_DIRREN_H__
12357+
12358+#ifdef __KERNEL__
12359+
12360+#include <linux/dcache.h>
12361+#include <linux/statfs.h>
12362+#include <linux/uuid.h>
12363+#include "hbl.h"
12364+
12365+#define AuDirren_NHASH 100
12366+
12367+#ifdef CONFIG_AUFS_DIRREN
12368+enum au_brid_type {
12369+ AuBrid_Unset,
12370+ AuBrid_UUID,
12371+ AuBrid_FSID,
12372+ AuBrid_DEV
12373+};
12374+
12375+struct au_dr_brid {
12376+ enum au_brid_type type;
12377+ union {
12378+ uuid_t uuid; /* unimplemented yet */
12379+ fsid_t fsid;
12380+ dev_t dev;
12381+ };
12382+};
12383+
12384+/* 20 is the max digits length of ulong 64 */
12385+/* brid-type "_" uuid "_" inum */
12386+#define AUFS_DIRREN_FNAME_SZ (1 + 1 + UUID_STRING_LEN + 20)
12387+#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12388+
12389+struct au_dr_hino {
12390+ struct hlist_bl_node dr_hnode;
12391+ ino_t dr_h_ino;
12392+};
12393+
12394+struct au_dr_br {
12395+ struct hlist_bl_head dr_h_ino[AuDirren_NHASH];
12396+ struct au_dr_brid dr_brid;
12397+};
12398+
12399+struct au_dr_lookup {
12400+ /* dr_name is pointed by struct au_do_lookup_args.name */
12401+ struct qstr dr_name; /* subset of dr_info */
12402+ aufs_bindex_t ninfo;
12403+ struct au_drinfo **drinfo;
12404+};
12405+#else
12406+struct au_dr_hino;
12407+/* empty */
12408+struct au_dr_br { };
12409+struct au_dr_lookup { };
12410+#endif
12411+
12412+/* ---------------------------------------------------------------------- */
12413+
12414+struct au_branch;
12415+struct au_do_lookup_args;
12416+struct au_hinode;
12417+#ifdef CONFIG_AUFS_DIRREN
12418+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12419+ struct au_dr_hino *add_ent);
12420+void au_dr_hino_free(struct au_dr_br *dr);
12421+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12422+ const struct path *path);
12423+int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12424+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12425+ struct qstr *dst_name, void *_rev);
12426+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12427+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12428+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12429+ aufs_bindex_t bindex);
12430+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12431+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12432+ ino_t h_ino);
12433+void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12434+int au_dr_opt_set(struct super_block *sb);
12435+int au_dr_opt_flush(struct super_block *sb);
12436+int au_dr_opt_clr(struct super_block *sb, int no_flush);
12437+#else
12438+AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12439+ struct au_dr_hino *add_ent);
12440+AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12441+AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12442+ const struct path *path);
12443+AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12444+AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12445+ struct qstr *dst_name, void *_rev);
12446+AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12447+AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12448+ void *rev);
12449+AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12450+ aufs_bindex_t bindex);
12451+AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12452+AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12453+ aufs_bindex_t bindex, ino_t h_ino);
12454+AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12455+AuStubInt0(au_dr_opt_set, struct super_block *sb);
12456+AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12457+AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12458+#endif
12459+
12460+/* ---------------------------------------------------------------------- */
12461+
12462+#ifdef CONFIG_AUFS_DIRREN
12463+static inline int au_dr_ihash(ino_t h_ino)
12464+{
12465+ return h_ino % AuDirren_NHASH;
12466+}
12467+#else
12468+AuStubInt0(au_dr_ihash, ino_t h_ino);
12469+#endif
12470+
12471+#endif /* __KERNEL__ */
12472+#endif /* __AUFS_DIRREN_H__ */
7f207e10
AM
12473diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12474--- /usr/share/empty/fs/aufs/dynop.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 12475+++ linux/fs/aufs/dynop.c 2018-08-12 23:43:05.456791299 +0200
062440b3 12476@@ -0,0 +1,370 @@
cd7a4cd9 12477+// SPDX-License-Identifier: GPL-2.0
1facf9fc 12478+/*
b00004a5 12479+ * Copyright (C) 2010-2018 Junjiro R. Okajima
1facf9fc 12480+ *
12481+ * This program, aufs is free software; you can redistribute it and/or modify
12482+ * it under the terms of the GNU General Public License as published by
12483+ * the Free Software Foundation; either version 2 of the License, or
12484+ * (at your option) any later version.
dece6358
AM
12485+ *
12486+ * This program is distributed in the hope that it will be useful,
12487+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12488+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12489+ * GNU General Public License for more details.
12490+ *
12491+ * You should have received a copy of the GNU General Public License
523b37e3 12492+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 12493+ */
12494+
12495+/*
4a4d8108 12496+ * dynamically customizable operations for regular files
1facf9fc 12497+ */
12498+
1facf9fc 12499+#include "aufs.h"
12500+
4a4d8108 12501+#define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
1facf9fc 12502+
4a4d8108
AM
12503+/*
12504+ * How large will these lists be?
12505+ * Usually just a few elements, 20-30 at most for each, I guess.
12506+ */
8b6a4947 12507+static struct hlist_bl_head dynop[AuDyLast];
4a4d8108 12508+
8b6a4947
AM
12509+static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12510+ const void *h_op)
1facf9fc 12511+{
4a4d8108 12512+ struct au_dykey *key, *tmp;
8b6a4947 12513+ struct hlist_bl_node *pos;
1facf9fc 12514+
4a4d8108 12515+ key = NULL;
8b6a4947
AM
12516+ hlist_bl_lock(hbl);
12517+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
4a4d8108
AM
12518+ if (tmp->dk_op.dy_hop == h_op) {
12519+ key = tmp;
12520+ kref_get(&key->dk_kref);
12521+ break;
12522+ }
8b6a4947 12523+ hlist_bl_unlock(hbl);
4a4d8108
AM
12524+
12525+ return key;
1facf9fc 12526+}
12527+
4a4d8108 12528+static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
1facf9fc 12529+{
4a4d8108
AM
12530+ struct au_dykey **k, *found;
12531+ const void *h_op = key->dk_op.dy_hop;
12532+ int i;
1facf9fc 12533+
4a4d8108
AM
12534+ found = NULL;
12535+ k = br->br_dykey;
12536+ for (i = 0; i < AuBrDynOp; i++)
12537+ if (k[i]) {
12538+ if (k[i]->dk_op.dy_hop == h_op) {
12539+ found = k[i];
12540+ break;
12541+ }
12542+ } else
12543+ break;
12544+ if (!found) {
12545+ spin_lock(&br->br_dykey_lock);
12546+ for (; i < AuBrDynOp; i++)
12547+ if (k[i]) {
12548+ if (k[i]->dk_op.dy_hop == h_op) {
12549+ found = k[i];
12550+ break;
12551+ }
12552+ } else {
12553+ k[i] = key;
12554+ break;
12555+ }
12556+ spin_unlock(&br->br_dykey_lock);
12557+ BUG_ON(i == AuBrDynOp); /* expand the array */
12558+ }
12559+
12560+ return found;
1facf9fc 12561+}
12562+
4a4d8108 12563+/* kref_get() if @key is already added */
8b6a4947 12564+static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
4a4d8108
AM
12565+{
12566+ struct au_dykey *tmp, *found;
8b6a4947 12567+ struct hlist_bl_node *pos;
4a4d8108 12568+ const void *h_op = key->dk_op.dy_hop;
1facf9fc 12569+
4a4d8108 12570+ found = NULL;
8b6a4947
AM
12571+ hlist_bl_lock(hbl);
12572+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
4a4d8108
AM
12573+ if (tmp->dk_op.dy_hop == h_op) {
12574+ kref_get(&tmp->dk_kref);
12575+ found = tmp;
12576+ break;
12577+ }
12578+ if (!found)
8b6a4947
AM
12579+ hlist_bl_add_head(&key->dk_hnode, hbl);
12580+ hlist_bl_unlock(hbl);
1facf9fc 12581+
4a4d8108
AM
12582+ if (!found)
12583+ DyPrSym(key);
12584+ return found;
12585+}
12586+
12587+static void dy_free_rcu(struct rcu_head *rcu)
1facf9fc 12588+{
4a4d8108
AM
12589+ struct au_dykey *key;
12590+
12591+ key = container_of(rcu, struct au_dykey, dk_rcu);
12592+ DyPrSym(key);
1c60b727 12593+ kfree(key);
1facf9fc 12594+}
12595+
4a4d8108
AM
12596+static void dy_free(struct kref *kref)
12597+{
12598+ struct au_dykey *key;
8b6a4947 12599+ struct hlist_bl_head *hbl;
1facf9fc 12600+
4a4d8108 12601+ key = container_of(kref, struct au_dykey, dk_kref);
8b6a4947
AM
12602+ hbl = dynop + key->dk_op.dy_type;
12603+ au_hbl_del(&key->dk_hnode, hbl);
4a4d8108
AM
12604+ call_rcu(&key->dk_rcu, dy_free_rcu);
12605+}
12606+
12607+void au_dy_put(struct au_dykey *key)
1facf9fc 12608+{
4a4d8108
AM
12609+ kref_put(&key->dk_kref, dy_free);
12610+}
1facf9fc 12611+
4a4d8108
AM
12612+/* ---------------------------------------------------------------------- */
12613+
12614+#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12615+
12616+#ifdef CONFIG_AUFS_DEBUG
12617+#define DyDbgDeclare(cnt) unsigned int cnt = 0
4f0767ce 12618+#define DyDbgInc(cnt) do { cnt++; } while (0)
4a4d8108
AM
12619+#else
12620+#define DyDbgDeclare(cnt) do {} while (0)
12621+#define DyDbgInc(cnt) do {} while (0)
12622+#endif
12623+
12624+#define DySet(func, dst, src, h_op, h_sb) do { \
12625+ DyDbgInc(cnt); \
12626+ if (h_op->func) { \
12627+ if (src.func) \
12628+ dst.func = src.func; \
12629+ else \
12630+ AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
12631+ } \
12632+} while (0)
12633+
12634+#define DySetForce(func, dst, src) do { \
12635+ AuDebugOn(!src.func); \
12636+ DyDbgInc(cnt); \
12637+ dst.func = src.func; \
12638+} while (0)
12639+
12640+#define DySetAop(func) \
12641+ DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12642+#define DySetAopForce(func) \
12643+ DySetForce(func, dyaop->da_op, aufs_aop)
12644+
12645+static void dy_aop(struct au_dykey *key, const void *h_op,
12646+ struct super_block *h_sb __maybe_unused)
12647+{
12648+ struct au_dyaop *dyaop = (void *)key;
12649+ const struct address_space_operations *h_aop = h_op;
12650+ DyDbgDeclare(cnt);
12651+
12652+ AuDbg("%s\n", au_sbtype(h_sb));
12653+
12654+ DySetAop(writepage);
12655+ DySetAopForce(readpage); /* force */
4a4d8108
AM
12656+ DySetAop(writepages);
12657+ DySetAop(set_page_dirty);
12658+ DySetAop(readpages);
12659+ DySetAop(write_begin);
12660+ DySetAop(write_end);
12661+ DySetAop(bmap);
12662+ DySetAop(invalidatepage);
12663+ DySetAop(releasepage);
027c5e7a 12664+ DySetAop(freepage);
7e9cd9fe 12665+ /* this one will be changed according to an aufs mount option */
4a4d8108 12666+ DySetAop(direct_IO);
4a4d8108 12667+ DySetAop(migratepage);
e2f27e51
AM
12668+ DySetAop(isolate_page);
12669+ DySetAop(putback_page);
4a4d8108
AM
12670+ DySetAop(launder_page);
12671+ DySetAop(is_partially_uptodate);
392086de 12672+ DySetAop(is_dirty_writeback);
4a4d8108 12673+ DySetAop(error_remove_page);
b4510431
AM
12674+ DySetAop(swap_activate);
12675+ DySetAop(swap_deactivate);
4a4d8108
AM
12676+
12677+ DyDbgSize(cnt, *h_aop);
4a4d8108
AM
12678+}
12679+
4a4d8108
AM
12680+/* ---------------------------------------------------------------------- */
12681+
12682+static void dy_bug(struct kref *kref)
12683+{
12684+ BUG();
12685+}
12686+
12687+static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12688+{
12689+ struct au_dykey *key, *old;
8b6a4947 12690+ struct hlist_bl_head *hbl;
b752ccd1 12691+ struct op {
4a4d8108 12692+ unsigned int sz;
b752ccd1
AM
12693+ void (*set)(struct au_dykey *key, const void *h_op,
12694+ struct super_block *h_sb __maybe_unused);
12695+ };
12696+ static const struct op a[] = {
4a4d8108
AM
12697+ [AuDy_AOP] = {
12698+ .sz = sizeof(struct au_dyaop),
b752ccd1 12699+ .set = dy_aop
4a4d8108 12700+ }
b752ccd1
AM
12701+ };
12702+ const struct op *p;
4a4d8108 12703+
8b6a4947
AM
12704+ hbl = dynop + op->dy_type;
12705+ key = dy_gfind_get(hbl, op->dy_hop);
4a4d8108
AM
12706+ if (key)
12707+ goto out_add; /* success */
12708+
12709+ p = a + op->dy_type;
12710+ key = kzalloc(p->sz, GFP_NOFS);
12711+ if (unlikely(!key)) {
12712+ key = ERR_PTR(-ENOMEM);
12713+ goto out;
12714+ }
12715+
12716+ key->dk_op.dy_hop = op->dy_hop;
12717+ kref_init(&key->dk_kref);
86dc4139 12718+ p->set(key, op->dy_hop, au_br_sb(br));
8b6a4947 12719+ old = dy_gadd(hbl, key);
4a4d8108 12720+ if (old) {
1c60b727 12721+ kfree(key);
4a4d8108
AM
12722+ key = old;
12723+ }
12724+
12725+out_add:
12726+ old = dy_bradd(br, key);
12727+ if (old)
12728+ /* its ref-count should never be zero here */
12729+ kref_put(&key->dk_kref, dy_bug);
12730+out:
12731+ return key;
12732+}
12733+
12734+/* ---------------------------------------------------------------------- */
12735+/*
12736+ * Aufs prohibits O_DIRECT by defaut even if the branch supports it.
c1595e42 12737+ * This behaviour is necessary to return an error from open(O_DIRECT) instead
4a4d8108
AM
12738+ * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12739+ * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12740+ * See the aufs manual in detail.
4a4d8108
AM
12741+ */
12742+static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12743+{
7e9cd9fe 12744+ if (!do_dx)
4a4d8108 12745+ dyaop->da_op.direct_IO = NULL;
7e9cd9fe 12746+ else
4a4d8108 12747+ dyaop->da_op.direct_IO = aufs_aop.direct_IO;
4a4d8108
AM
12748+}
12749+
12750+static struct au_dyaop *dy_aget(struct au_branch *br,
12751+ const struct address_space_operations *h_aop,
12752+ int do_dx)
12753+{
12754+ struct au_dyaop *dyaop;
12755+ struct au_dynop op;
12756+
12757+ op.dy_type = AuDy_AOP;
12758+ op.dy_haop = h_aop;
12759+ dyaop = (void *)dy_get(&op, br);
12760+ if (IS_ERR(dyaop))
12761+ goto out;
12762+ dy_adx(dyaop, do_dx);
12763+
12764+out:
12765+ return dyaop;
12766+}
12767+
12768+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12769+ struct inode *h_inode)
12770+{
12771+ int err, do_dx;
12772+ struct super_block *sb;
12773+ struct au_branch *br;
12774+ struct au_dyaop *dyaop;
12775+
12776+ AuDebugOn(!S_ISREG(h_inode->i_mode));
12777+ IiMustWriteLock(inode);
12778+
12779+ sb = inode->i_sb;
12780+ br = au_sbr(sb, bindex);
12781+ do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12782+ dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12783+ err = PTR_ERR(dyaop);
12784+ if (IS_ERR(dyaop))
12785+ /* unnecessary to call dy_fput() */
12786+ goto out;
12787+
12788+ err = 0;
12789+ inode->i_mapping->a_ops = &dyaop->da_op;
12790+
12791+out:
12792+ return err;
12793+}
12794+
b752ccd1
AM
12795+/*
12796+ * Is it safe to replace a_ops during the inode/file is in operation?
12797+ * Yes, I hope so.
12798+ */
12799+int au_dy_irefresh(struct inode *inode)
12800+{
12801+ int err;
5afbbe0d 12802+ aufs_bindex_t btop;
b752ccd1
AM
12803+ struct inode *h_inode;
12804+
12805+ err = 0;
12806+ if (S_ISREG(inode->i_mode)) {
5afbbe0d
AM
12807+ btop = au_ibtop(inode);
12808+ h_inode = au_h_iptr(inode, btop);
12809+ err = au_dy_iaop(inode, btop, h_inode);
b752ccd1
AM
12810+ }
12811+ return err;
12812+}
12813+
4a4d8108
AM
12814+void au_dy_arefresh(int do_dx)
12815+{
8b6a4947
AM
12816+ struct hlist_bl_head *hbl;
12817+ struct hlist_bl_node *pos;
4a4d8108
AM
12818+ struct au_dykey *key;
12819+
8b6a4947
AM
12820+ hbl = dynop + AuDy_AOP;
12821+ hlist_bl_lock(hbl);
12822+ hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
4a4d8108 12823+ dy_adx((void *)key, do_dx);
8b6a4947 12824+ hlist_bl_unlock(hbl);
4a4d8108
AM
12825+}
12826+
4a4d8108
AM
12827+/* ---------------------------------------------------------------------- */
12828+
12829+void __init au_dy_init(void)
12830+{
12831+ int i;
12832+
12833+ /* make sure that 'struct au_dykey *' can be any type */
12834+ BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
4a4d8108
AM
12835+
12836+ for (i = 0; i < AuDyLast; i++)
8b6a4947 12837+ INIT_HLIST_BL_HEAD(dynop + i);
4a4d8108
AM
12838+}
12839+
12840+void au_dy_fin(void)
12841+{
12842+ int i;
12843+
12844+ for (i = 0; i < AuDyLast; i++)
8b6a4947 12845+ WARN_ON(!hlist_bl_empty(dynop + i));
4a4d8108 12846+}
7f207e10
AM
12847diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12848--- /usr/share/empty/fs/aufs/dynop.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 12849+++ linux/fs/aufs/dynop.h 2018-08-12 23:43:05.456791299 +0200
062440b3
AM
12850@@ -0,0 +1,75 @@
12851+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 12852+/*
b00004a5 12853+ * Copyright (C) 2010-2018 Junjiro R. Okajima
4a4d8108
AM
12854+ *
12855+ * This program, aufs is free software; you can redistribute it and/or modify
12856+ * it under the terms of the GNU General Public License as published by
12857+ * the Free Software Foundation; either version 2 of the License, or
12858+ * (at your option) any later version.
12859+ *
12860+ * This program is distributed in the hope that it will be useful,
12861+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12862+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12863+ * GNU General Public License for more details.
12864+ *
12865+ * You should have received a copy of the GNU General Public License
523b37e3 12866+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
12867+ */
12868+
12869+/*
12870+ * dynamically customizable operations (for regular files only)
12871+ */
12872+
12873+#ifndef __AUFS_DYNOP_H__
12874+#define __AUFS_DYNOP_H__
12875+
12876+#ifdef __KERNEL__
12877+
7e9cd9fe
AM
12878+#include <linux/fs.h>
12879+#include <linux/kref.h>
4a4d8108 12880+
2cbb1c4b 12881+enum {AuDy_AOP, AuDyLast};
4a4d8108
AM
12882+
12883+struct au_dynop {
12884+ int dy_type;
12885+ union {
12886+ const void *dy_hop;
12887+ const struct address_space_operations *dy_haop;
4a4d8108
AM
12888+ };
12889+};
12890+
12891+struct au_dykey {
12892+ union {
8b6a4947 12893+ struct hlist_bl_node dk_hnode;
4a4d8108
AM
12894+ struct rcu_head dk_rcu;
12895+ };
12896+ struct au_dynop dk_op;
12897+
12898+ /*
12899+ * during I am in the branch local array, kref is gotten. when the
12900+ * branch is removed, kref is put.
12901+ */
12902+ struct kref dk_kref;
12903+};
12904+
12905+/* stop unioning since their sizes are very different from each other */
12906+struct au_dyaop {
12907+ struct au_dykey da_key;
12908+ struct address_space_operations da_op; /* not const */
4a4d8108
AM
12909+};
12910+
4a4d8108
AM
12911+/* ---------------------------------------------------------------------- */
12912+
12913+/* dynop.c */
12914+struct au_branch;
12915+void au_dy_put(struct au_dykey *key);
12916+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12917+ struct inode *h_inode);
b752ccd1 12918+int au_dy_irefresh(struct inode *inode);
4a4d8108 12919+void au_dy_arefresh(int do_dio);
4a4d8108
AM
12920+
12921+void __init au_dy_init(void);
12922+void au_dy_fin(void);
12923+
4a4d8108
AM
12924+#endif /* __KERNEL__ */
12925+#endif /* __AUFS_DYNOP_H__ */
7f207e10
AM
12926diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12927--- /usr/share/empty/fs/aufs/export.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 12928+++ linux/fs/aufs/export.c 2018-08-12 23:43:05.456791299 +0200
062440b3 12929@@ -0,0 +1,838 @@
cd7a4cd9 12930+// SPDX-License-Identifier: GPL-2.0
4a4d8108 12931+/*
b00004a5 12932+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
12933+ *
12934+ * This program, aufs is free software; you can redistribute it and/or modify
12935+ * it under the terms of the GNU General Public License as published by
12936+ * the Free Software Foundation; either version 2 of the License, or
12937+ * (at your option) any later version.
12938+ *
12939+ * This program is distributed in the hope that it will be useful,
12940+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12941+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12942+ * GNU General Public License for more details.
12943+ *
12944+ * You should have received a copy of the GNU General Public License
523b37e3 12945+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
12946+ */
12947+
12948+/*
12949+ * export via nfs
12950+ */
12951+
12952+#include <linux/exportfs.h>
7eafdf33 12953+#include <linux/fs_struct.h>
4a4d8108
AM
12954+#include <linux/namei.h>
12955+#include <linux/nsproxy.h>
12956+#include <linux/random.h>
12957+#include <linux/writeback.h>
12958+#include "aufs.h"
12959+
12960+union conv {
12961+#ifdef CONFIG_AUFS_INO_T_64
12962+ __u32 a[2];
12963+#else
12964+ __u32 a[1];
12965+#endif
12966+ ino_t ino;
12967+};
12968+
12969+static ino_t decode_ino(__u32 *a)
12970+{
12971+ union conv u;
12972+
12973+ BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12974+ u.a[0] = a[0];
12975+#ifdef CONFIG_AUFS_INO_T_64
12976+ u.a[1] = a[1];
12977+#endif
12978+ return u.ino;
12979+}
12980+
12981+static void encode_ino(__u32 *a, ino_t ino)
12982+{
12983+ union conv u;
12984+
12985+ u.ino = ino;
12986+ a[0] = u.a[0];
12987+#ifdef CONFIG_AUFS_INO_T_64
12988+ a[1] = u.a[1];
12989+#endif
12990+}
12991+
12992+/* NFS file handle */
12993+enum {
12994+ Fh_br_id,
12995+ Fh_sigen,
12996+#ifdef CONFIG_AUFS_INO_T_64
12997+ /* support 64bit inode number */
12998+ Fh_ino1,
12999+ Fh_ino2,
13000+ Fh_dir_ino1,
13001+ Fh_dir_ino2,
13002+#else
13003+ Fh_ino1,
13004+ Fh_dir_ino1,
13005+#endif
13006+ Fh_igen,
13007+ Fh_h_type,
13008+ Fh_tail,
13009+
13010+ Fh_ino = Fh_ino1,
13011+ Fh_dir_ino = Fh_dir_ino1
13012+};
13013+
13014+static int au_test_anon(struct dentry *dentry)
13015+{
027c5e7a 13016+ /* note: read d_flags without d_lock */
4a4d8108
AM
13017+ return !!(dentry->d_flags & DCACHE_DISCONNECTED);
13018+}
13019+
a2a7ad62
AM
13020+int au_test_nfsd(void)
13021+{
13022+ int ret;
13023+ struct task_struct *tsk = current;
13024+ char comm[sizeof(tsk->comm)];
13025+
13026+ ret = 0;
13027+ if (tsk->flags & PF_KTHREAD) {
13028+ get_task_comm(comm, tsk);
13029+ ret = !strcmp(comm, "nfsd");
13030+ }
13031+
13032+ return ret;
13033+}
13034+
4a4d8108
AM
13035+/* ---------------------------------------------------------------------- */
13036+/* inode generation external table */
13037+
b752ccd1 13038+void au_xigen_inc(struct inode *inode)
4a4d8108 13039+{
4a4d8108
AM
13040+ loff_t pos;
13041+ ssize_t sz;
13042+ __u32 igen;
13043+ struct super_block *sb;
13044+ struct au_sbinfo *sbinfo;
13045+
4a4d8108 13046+ sb = inode->i_sb;
b752ccd1 13047+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
1facf9fc 13048+
b752ccd1 13049+ sbinfo = au_sbi(sb);
1facf9fc 13050+ pos = inode->i_ino;
13051+ pos *= sizeof(igen);
13052+ igen = inode->i_generation + 1;
1facf9fc 13053+ sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
13054+ sizeof(igen), &pos);
13055+ if (sz == sizeof(igen))
b752ccd1 13056+ return; /* success */
1facf9fc 13057+
b752ccd1 13058+ if (unlikely(sz >= 0))
1facf9fc 13059+ AuIOErr("xigen error (%zd)\n", sz);
1facf9fc 13060+}
13061+
13062+int au_xigen_new(struct inode *inode)
13063+{
13064+ int err;
13065+ loff_t pos;
13066+ ssize_t sz;
13067+ struct super_block *sb;
13068+ struct au_sbinfo *sbinfo;
13069+ struct file *file;
13070+
13071+ err = 0;
13072+ /* todo: dirty, at mount time */
13073+ if (inode->i_ino == AUFS_ROOT_INO)
13074+ goto out;
13075+ sb = inode->i_sb;
dece6358 13076+ SiMustAnyLock(sb);
1facf9fc 13077+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13078+ goto out;
13079+
13080+ err = -EFBIG;
13081+ pos = inode->i_ino;
13082+ if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
13083+ AuIOErr1("too large i%lld\n", pos);
13084+ goto out;
13085+ }
13086+ pos *= sizeof(inode->i_generation);
13087+
13088+ err = 0;
13089+ sbinfo = au_sbi(sb);
13090+ file = sbinfo->si_xigen;
13091+ BUG_ON(!file);
13092+
c06a8ce3 13093+ if (vfsub_f_size_read(file)
1facf9fc 13094+ < pos + sizeof(inode->i_generation)) {
13095+ inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
13096+ sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
13097+ sizeof(inode->i_generation), &pos);
13098+ } else
13099+ sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
13100+ sizeof(inode->i_generation), &pos);
13101+ if (sz == sizeof(inode->i_generation))
13102+ goto out; /* success */
13103+
13104+ err = sz;
13105+ if (unlikely(sz >= 0)) {
13106+ err = -EIO;
13107+ AuIOErr("xigen error (%zd)\n", sz);
13108+ }
13109+
4f0767ce 13110+out:
1facf9fc 13111+ return err;
13112+}
13113+
062440b3 13114+int au_xigen_set(struct super_block *sb, struct path *path)
1facf9fc 13115+{
13116+ int err;
13117+ struct au_sbinfo *sbinfo;
13118+ struct file *file;
13119+
dece6358
AM
13120+ SiMustWriteLock(sb);
13121+
1facf9fc 13122+ sbinfo = au_sbi(sb);
062440b3 13123+ file = au_xino_create2(sb, path, sbinfo->si_xigen);
1facf9fc 13124+ err = PTR_ERR(file);
13125+ if (IS_ERR(file))
13126+ goto out;
13127+ err = 0;
13128+ if (sbinfo->si_xigen)
13129+ fput(sbinfo->si_xigen);
13130+ sbinfo->si_xigen = file;
13131+
4f0767ce 13132+out:
062440b3 13133+ AuTraceErr(err);
1facf9fc 13134+ return err;
13135+}
13136+
13137+void au_xigen_clr(struct super_block *sb)
13138+{
13139+ struct au_sbinfo *sbinfo;
13140+
dece6358
AM
13141+ SiMustWriteLock(sb);
13142+
1facf9fc 13143+ sbinfo = au_sbi(sb);
13144+ if (sbinfo->si_xigen) {
13145+ fput(sbinfo->si_xigen);
13146+ sbinfo->si_xigen = NULL;
13147+ }
13148+}
13149+
13150+/* ---------------------------------------------------------------------- */
13151+
13152+static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13153+ ino_t dir_ino)
13154+{
13155+ struct dentry *dentry, *d;
13156+ struct inode *inode;
13157+ unsigned int sigen;
13158+
13159+ dentry = NULL;
13160+ inode = ilookup(sb, ino);
13161+ if (!inode)
13162+ goto out;
13163+
13164+ dentry = ERR_PTR(-ESTALE);
13165+ sigen = au_sigen(sb);
5afbbe0d 13166+ if (unlikely(au_is_bad_inode(inode)
1facf9fc 13167+ || IS_DEADDIR(inode)
537831f9 13168+ || sigen != au_iigen(inode, NULL)))
1facf9fc 13169+ goto out_iput;
13170+
13171+ dentry = NULL;
13172+ if (!dir_ino || S_ISDIR(inode->i_mode))
13173+ dentry = d_find_alias(inode);
13174+ else {
027c5e7a 13175+ spin_lock(&inode->i_lock);
c1595e42 13176+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 13177+ spin_lock(&d->d_lock);
1facf9fc 13178+ if (!au_test_anon(d)
5527c038 13179+ && d_inode(d->d_parent)->i_ino == dir_ino) {
027c5e7a
AM
13180+ dentry = dget_dlock(d);
13181+ spin_unlock(&d->d_lock);
1facf9fc 13182+ break;
13183+ }
027c5e7a
AM
13184+ spin_unlock(&d->d_lock);
13185+ }
13186+ spin_unlock(&inode->i_lock);
1facf9fc 13187+ }
027c5e7a 13188+ if (unlikely(dentry && au_digen_test(dentry, sigen))) {
2cbb1c4b 13189+ /* need to refresh */
1facf9fc 13190+ dput(dentry);
2cbb1c4b 13191+ dentry = NULL;
1facf9fc 13192+ }
13193+
4f0767ce 13194+out_iput:
1facf9fc 13195+ iput(inode);
4f0767ce 13196+out:
2cbb1c4b 13197+ AuTraceErrPtr(dentry);
1facf9fc 13198+ return dentry;
13199+}
13200+
13201+/* ---------------------------------------------------------------------- */
13202+
13203+/* todo: dirty? */
13204+/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
4a4d8108
AM
13205+
13206+struct au_compare_mnt_args {
13207+ /* input */
13208+ struct super_block *sb;
13209+
13210+ /* output */
13211+ struct vfsmount *mnt;
13212+};
13213+
13214+static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13215+{
13216+ struct au_compare_mnt_args *a = arg;
13217+
13218+ if (mnt->mnt_sb != a->sb)
13219+ return 0;
13220+ a->mnt = mntget(mnt);
13221+ return 1;
13222+}
13223+
1facf9fc 13224+static struct vfsmount *au_mnt_get(struct super_block *sb)
13225+{
4a4d8108 13226+ int err;
7eafdf33 13227+ struct path root;
4a4d8108
AM
13228+ struct au_compare_mnt_args args = {
13229+ .sb = sb
13230+ };
1facf9fc 13231+
7eafdf33 13232+ get_fs_root(current->fs, &root);
523b37e3 13233+ rcu_read_lock();
7eafdf33 13234+ err = iterate_mounts(au_compare_mnt, &args, root.mnt);
523b37e3 13235+ rcu_read_unlock();
7eafdf33 13236+ path_put(&root);
4a4d8108
AM
13237+ AuDebugOn(!err);
13238+ AuDebugOn(!args.mnt);
13239+ return args.mnt;
1facf9fc 13240+}
13241+
13242+struct au_nfsd_si_lock {
4a4d8108 13243+ unsigned int sigen;
027c5e7a 13244+ aufs_bindex_t bindex, br_id;
1facf9fc 13245+ unsigned char force_lock;
13246+};
13247+
027c5e7a
AM
13248+static int si_nfsd_read_lock(struct super_block *sb,
13249+ struct au_nfsd_si_lock *nsi_lock)
1facf9fc 13250+{
027c5e7a 13251+ int err;
1facf9fc 13252+ aufs_bindex_t bindex;
13253+
13254+ si_read_lock(sb, AuLock_FLUSH);
13255+
13256+ /* branch id may be wrapped around */
027c5e7a 13257+ err = 0;
1facf9fc 13258+ bindex = au_br_index(sb, nsi_lock->br_id);
13259+ if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13260+ goto out; /* success */
13261+
027c5e7a
AM
13262+ err = -ESTALE;
13263+ bindex = -1;
1facf9fc 13264+ if (!nsi_lock->force_lock)
13265+ si_read_unlock(sb);
1facf9fc 13266+
4f0767ce 13267+out:
027c5e7a
AM
13268+ nsi_lock->bindex = bindex;
13269+ return err;
1facf9fc 13270+}
13271+
13272+struct find_name_by_ino {
392086de 13273+ struct dir_context ctx;
1facf9fc 13274+ int called, found;
13275+ ino_t ino;
13276+ char *name;
13277+ int namelen;
13278+};
13279+
13280+static int
392086de
AM
13281+find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13282+ loff_t offset, u64 ino, unsigned int d_type)
1facf9fc 13283+{
392086de
AM
13284+ struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13285+ ctx);
1facf9fc 13286+
13287+ a->called++;
13288+ if (a->ino != ino)
13289+ return 0;
13290+
13291+ memcpy(a->name, name, namelen);
13292+ a->namelen = namelen;
13293+ a->found = 1;
13294+ return 1;
13295+}
13296+
13297+static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13298+ struct au_nfsd_si_lock *nsi_lock)
13299+{
13300+ struct dentry *dentry, *parent;
13301+ struct file *file;
13302+ struct inode *dir;
392086de
AM
13303+ struct find_name_by_ino arg = {
13304+ .ctx = {
2000de60 13305+ .actor = find_name_by_ino
392086de
AM
13306+ }
13307+ };
1facf9fc 13308+ int err;
13309+
13310+ parent = path->dentry;
13311+ if (nsi_lock)
13312+ si_read_unlock(parent->d_sb);
4a4d8108 13313+ file = vfsub_dentry_open(path, au_dir_roflags);
1facf9fc 13314+ dentry = (void *)file;
13315+ if (IS_ERR(file))
13316+ goto out;
13317+
13318+ dentry = ERR_PTR(-ENOMEM);
537831f9 13319+ arg.name = (void *)__get_free_page(GFP_NOFS);
1facf9fc 13320+ if (unlikely(!arg.name))
13321+ goto out_file;
13322+ arg.ino = ino;
13323+ arg.found = 0;
13324+ do {
13325+ arg.called = 0;
13326+ /* smp_mb(); */
392086de 13327+ err = vfsub_iterate_dir(file, &arg.ctx);
1facf9fc 13328+ } while (!err && !arg.found && arg.called);
13329+ dentry = ERR_PTR(err);
13330+ if (unlikely(err))
13331+ goto out_name;
1716fcea
AM
13332+ /* instead of ENOENT */
13333+ dentry = ERR_PTR(-ESTALE);
1facf9fc 13334+ if (!arg.found)
13335+ goto out_name;
13336+
b4510431 13337+ /* do not call vfsub_lkup_one() */
5527c038 13338+ dir = d_inode(parent);
febd17d6 13339+ dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
1facf9fc 13340+ AuTraceErrPtr(dentry);
13341+ if (IS_ERR(dentry))
13342+ goto out_name;
13343+ AuDebugOn(au_test_anon(dentry));
5527c038 13344+ if (unlikely(d_really_is_negative(dentry))) {
1facf9fc 13345+ dput(dentry);
13346+ dentry = ERR_PTR(-ENOENT);
13347+ }
13348+
4f0767ce 13349+out_name:
1c60b727 13350+ free_page((unsigned long)arg.name);
4f0767ce 13351+out_file:
1facf9fc 13352+ fput(file);
4f0767ce 13353+out:
1facf9fc 13354+ if (unlikely(nsi_lock
13355+ && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13356+ if (!IS_ERR(dentry)) {
13357+ dput(dentry);
13358+ dentry = ERR_PTR(-ESTALE);
13359+ }
13360+ AuTraceErrPtr(dentry);
13361+ return dentry;
13362+}
13363+
13364+static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13365+ ino_t dir_ino,
13366+ struct au_nfsd_si_lock *nsi_lock)
13367+{
13368+ struct dentry *dentry;
13369+ struct path path;
13370+
13371+ if (dir_ino != AUFS_ROOT_INO) {
13372+ path.dentry = decode_by_ino(sb, dir_ino, 0);
13373+ dentry = path.dentry;
13374+ if (!path.dentry || IS_ERR(path.dentry))
13375+ goto out;
13376+ AuDebugOn(au_test_anon(path.dentry));
13377+ } else
13378+ path.dentry = dget(sb->s_root);
13379+
13380+ path.mnt = au_mnt_get(sb);
13381+ dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13382+ path_put(&path);
13383+
4f0767ce 13384+out:
1facf9fc 13385+ AuTraceErrPtr(dentry);
13386+ return dentry;
13387+}
13388+
13389+/* ---------------------------------------------------------------------- */
13390+
13391+static int h_acceptable(void *expv, struct dentry *dentry)
13392+{
13393+ return 1;
13394+}
13395+
13396+static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13397+ char *buf, int len, struct super_block *sb)
13398+{
13399+ char *p;
13400+ int n;
13401+ struct path path;
13402+
13403+ p = d_path(h_rootpath, buf, len);
13404+ if (IS_ERR(p))
13405+ goto out;
13406+ n = strlen(p);
13407+
13408+ path.mnt = h_rootpath->mnt;
13409+ path.dentry = h_parent;
13410+ p = d_path(&path, buf, len);
13411+ if (IS_ERR(p))
13412+ goto out;
13413+ if (n != 1)
13414+ p += n;
13415+
13416+ path.mnt = au_mnt_get(sb);
13417+ path.dentry = sb->s_root;
13418+ p = d_path(&path, buf, len - strlen(p));
13419+ mntput(path.mnt);
13420+ if (IS_ERR(p))
13421+ goto out;
13422+ if (n != 1)
13423+ p[strlen(p)] = '/';
13424+
4f0767ce 13425+out:
1facf9fc 13426+ AuTraceErrPtr(p);
13427+ return p;
13428+}
13429+
13430+static
027c5e7a
AM
13431+struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13432+ int fh_len, struct au_nfsd_si_lock *nsi_lock)
1facf9fc 13433+{
13434+ struct dentry *dentry, *h_parent, *root;
13435+ struct super_block *h_sb;
13436+ char *pathname, *p;
13437+ struct vfsmount *h_mnt;
13438+ struct au_branch *br;
13439+ int err;
13440+ struct path path;
13441+
027c5e7a 13442+ br = au_sbr(sb, nsi_lock->bindex);
86dc4139 13443+ h_mnt = au_br_mnt(br);
1facf9fc 13444+ h_sb = h_mnt->mnt_sb;
13445+ /* todo: call lower fh_to_dentry()? fh_to_parent()? */
5afbbe0d 13446+ lockdep_off();
1facf9fc 13447+ h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13448+ fh_len - Fh_tail, fh[Fh_h_type],
13449+ h_acceptable, /*context*/NULL);
5afbbe0d 13450+ lockdep_on();
1facf9fc 13451+ dentry = h_parent;
13452+ if (unlikely(!h_parent || IS_ERR(h_parent))) {
13453+ AuWarn1("%s decode_fh failed, %ld\n",
13454+ au_sbtype(h_sb), PTR_ERR(h_parent));
13455+ goto out;
13456+ }
13457+ dentry = NULL;
13458+ if (unlikely(au_test_anon(h_parent))) {
13459+ AuWarn1("%s decode_fh returned a disconnected dentry\n",
13460+ au_sbtype(h_sb));
13461+ goto out_h_parent;
13462+ }
13463+
13464+ dentry = ERR_PTR(-ENOMEM);
13465+ pathname = (void *)__get_free_page(GFP_NOFS);
13466+ if (unlikely(!pathname))
13467+ goto out_h_parent;
13468+
13469+ root = sb->s_root;
13470+ path.mnt = h_mnt;
13471+ di_read_lock_parent(root, !AuLock_IR);
027c5e7a 13472+ path.dentry = au_h_dptr(root, nsi_lock->bindex);
1facf9fc 13473+ di_read_unlock(root, !AuLock_IR);
13474+ p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13475+ dentry = (void *)p;
13476+ if (IS_ERR(p))
13477+ goto out_pathname;
13478+
13479+ si_read_unlock(sb);
13480+ err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13481+ dentry = ERR_PTR(err);
13482+ if (unlikely(err))
13483+ goto out_relock;
13484+
13485+ dentry = ERR_PTR(-ENOENT);
13486+ AuDebugOn(au_test_anon(path.dentry));
5527c038 13487+ if (unlikely(d_really_is_negative(path.dentry)))
1facf9fc 13488+ goto out_path;
13489+
5527c038 13490+ if (ino != d_inode(path.dentry)->i_ino)
1facf9fc 13491+ dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13492+ else
13493+ dentry = dget(path.dentry);
13494+
4f0767ce 13495+out_path:
1facf9fc 13496+ path_put(&path);
4f0767ce 13497+out_relock:
1facf9fc 13498+ if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13499+ if (!IS_ERR(dentry)) {
13500+ dput(dentry);
13501+ dentry = ERR_PTR(-ESTALE);
13502+ }
4f0767ce 13503+out_pathname:
1c60b727 13504+ free_page((unsigned long)pathname);
4f0767ce 13505+out_h_parent:
1facf9fc 13506+ dput(h_parent);
4f0767ce 13507+out:
1facf9fc 13508+ AuTraceErrPtr(dentry);
13509+ return dentry;
13510+}
13511+
13512+/* ---------------------------------------------------------------------- */
13513+
13514+static struct dentry *
13515+aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13516+ int fh_type)
13517+{
13518+ struct dentry *dentry;
13519+ __u32 *fh = fid->raw;
027c5e7a 13520+ struct au_branch *br;
1facf9fc 13521+ ino_t ino, dir_ino;
1facf9fc 13522+ struct au_nfsd_si_lock nsi_lock = {
1facf9fc 13523+ .force_lock = 0
13524+ };
13525+
1facf9fc 13526+ dentry = ERR_PTR(-ESTALE);
4a4d8108
AM
13527+ /* it should never happen, but the file handle is unreliable */
13528+ if (unlikely(fh_len < Fh_tail))
13529+ goto out;
13530+ nsi_lock.sigen = fh[Fh_sigen];
13531+ nsi_lock.br_id = fh[Fh_br_id];
13532+
1facf9fc 13533+ /* branch id may be wrapped around */
027c5e7a
AM
13534+ br = NULL;
13535+ if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
1facf9fc 13536+ goto out;
13537+ nsi_lock.force_lock = 1;
13538+
13539+ /* is this inode still cached? */
13540+ ino = decode_ino(fh + Fh_ino);
4a4d8108
AM
13541+ /* it should never happen */
13542+ if (unlikely(ino == AUFS_ROOT_INO))
8cdd5066 13543+ goto out_unlock;
4a4d8108 13544+
1facf9fc 13545+ dir_ino = decode_ino(fh + Fh_dir_ino);
13546+ dentry = decode_by_ino(sb, ino, dir_ino);
13547+ if (IS_ERR(dentry))
13548+ goto out_unlock;
13549+ if (dentry)
13550+ goto accept;
13551+
13552+ /* is the parent dir cached? */
027c5e7a 13553+ br = au_sbr(sb, nsi_lock.bindex);
5afbbe0d 13554+ au_br_get(br);
1facf9fc 13555+ dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13556+ if (IS_ERR(dentry))
13557+ goto out_unlock;
13558+ if (dentry)
13559+ goto accept;
13560+
13561+ /* lookup path */
027c5e7a 13562+ dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
1facf9fc 13563+ if (IS_ERR(dentry))
13564+ goto out_unlock;
13565+ if (unlikely(!dentry))
13566+ /* todo?: make it ESTALE */
13567+ goto out_unlock;
13568+
4f0767ce 13569+accept:
027c5e7a 13570+ if (!au_digen_test(dentry, au_sigen(sb))
5527c038 13571+ && d_inode(dentry)->i_generation == fh[Fh_igen])
1facf9fc 13572+ goto out_unlock; /* success */
13573+
13574+ dput(dentry);
13575+ dentry = ERR_PTR(-ESTALE);
4f0767ce 13576+out_unlock:
027c5e7a 13577+ if (br)
5afbbe0d 13578+ au_br_put(br);
1facf9fc 13579+ si_read_unlock(sb);
4f0767ce 13580+out:
1facf9fc 13581+ AuTraceErrPtr(dentry);
13582+ return dentry;
13583+}
13584+
13585+#if 0 /* reserved for future use */
13586+/* support subtreecheck option */
13587+static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13588+ int fh_len, int fh_type)
13589+{
13590+ struct dentry *parent;
13591+ __u32 *fh = fid->raw;
13592+ ino_t dir_ino;
13593+
13594+ dir_ino = decode_ino(fh + Fh_dir_ino);
13595+ parent = decode_by_ino(sb, dir_ino, 0);
13596+ if (IS_ERR(parent))
13597+ goto out;
13598+ if (!parent)
13599+ parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13600+ dir_ino, fh, fh_len);
13601+
4f0767ce 13602+out:
1facf9fc 13603+ AuTraceErrPtr(parent);
13604+ return parent;
13605+}
13606+#endif
13607+
13608+/* ---------------------------------------------------------------------- */
13609+
0c3ec466
AM
13610+static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13611+ struct inode *dir)
1facf9fc 13612+{
13613+ int err;
0c3ec466 13614+ aufs_bindex_t bindex;
1facf9fc 13615+ struct super_block *sb, *h_sb;
0c3ec466
AM
13616+ struct dentry *dentry, *parent, *h_parent;
13617+ struct inode *h_dir;
1facf9fc 13618+ struct au_branch *br;
13619+
1facf9fc 13620+ err = -ENOSPC;
13621+ if (unlikely(*max_len <= Fh_tail)) {
13622+ AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13623+ goto out;
13624+ }
13625+
13626+ err = FILEID_ROOT;
0c3ec466
AM
13627+ if (inode->i_ino == AUFS_ROOT_INO) {
13628+ AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
1facf9fc 13629+ goto out;
13630+ }
13631+
1facf9fc 13632+ h_parent = NULL;
0c3ec466
AM
13633+ sb = inode->i_sb;
13634+ err = si_read_lock(sb, AuLock_FLUSH);
027c5e7a
AM
13635+ if (unlikely(err))
13636+ goto out;
13637+
1facf9fc 13638+#ifdef CONFIG_AUFS_DEBUG
13639+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13640+ AuWarn1("NFS-exporting requires xino\n");
13641+#endif
027c5e7a 13642+ err = -EIO;
0c3ec466
AM
13643+ parent = NULL;
13644+ ii_read_lock_child(inode);
5afbbe0d 13645+ bindex = au_ibtop(inode);
0c3ec466 13646+ if (!dir) {
c1595e42 13647+ dentry = d_find_any_alias(inode);
0c3ec466
AM
13648+ if (unlikely(!dentry))
13649+ goto out_unlock;
13650+ AuDebugOn(au_test_anon(dentry));
13651+ parent = dget_parent(dentry);
13652+ dput(dentry);
13653+ if (unlikely(!parent))
13654+ goto out_unlock;
5527c038
JR
13655+ if (d_really_is_positive(parent))
13656+ dir = d_inode(parent);
1facf9fc 13657+ }
0c3ec466
AM
13658+
13659+ ii_read_lock_parent(dir);
13660+ h_dir = au_h_iptr(dir, bindex);
13661+ ii_read_unlock(dir);
13662+ if (unlikely(!h_dir))
13663+ goto out_parent;
c1595e42 13664+ h_parent = d_find_any_alias(h_dir);
1facf9fc 13665+ if (unlikely(!h_parent))
0c3ec466 13666+ goto out_hparent;
1facf9fc 13667+
13668+ err = -EPERM;
13669+ br = au_sbr(sb, bindex);
86dc4139 13670+ h_sb = au_br_sb(br);
1facf9fc 13671+ if (unlikely(!h_sb->s_export_op)) {
13672+ AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
0c3ec466 13673+ goto out_hparent;
1facf9fc 13674+ }
13675+
13676+ fh[Fh_br_id] = br->br_id;
13677+ fh[Fh_sigen] = au_sigen(sb);
13678+ encode_ino(fh + Fh_ino, inode->i_ino);
0c3ec466 13679+ encode_ino(fh + Fh_dir_ino, dir->i_ino);
1facf9fc 13680+ fh[Fh_igen] = inode->i_generation;
13681+
13682+ *max_len -= Fh_tail;
13683+ fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13684+ max_len,
13685+ /*connectable or subtreecheck*/0);
13686+ err = fh[Fh_h_type];
13687+ *max_len += Fh_tail;
13688+ /* todo: macros? */
1716fcea 13689+ if (err != FILEID_INVALID)
1facf9fc 13690+ err = 99;
13691+ else
13692+ AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13693+
0c3ec466 13694+out_hparent:
1facf9fc 13695+ dput(h_parent);
0c3ec466 13696+out_parent:
1facf9fc 13697+ dput(parent);
0c3ec466
AM
13698+out_unlock:
13699+ ii_read_unlock(inode);
13700+ si_read_unlock(sb);
4f0767ce 13701+out:
1facf9fc 13702+ if (unlikely(err < 0))
1716fcea 13703+ err = FILEID_INVALID;
1facf9fc 13704+ return err;
13705+}
13706+
13707+/* ---------------------------------------------------------------------- */
13708+
4a4d8108
AM
13709+static int aufs_commit_metadata(struct inode *inode)
13710+{
13711+ int err;
13712+ aufs_bindex_t bindex;
13713+ struct super_block *sb;
13714+ struct inode *h_inode;
13715+ int (*f)(struct inode *inode);
13716+
13717+ sb = inode->i_sb;
e49829fe 13718+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 13719+ ii_write_lock_child(inode);
5afbbe0d 13720+ bindex = au_ibtop(inode);
4a4d8108
AM
13721+ AuDebugOn(bindex < 0);
13722+ h_inode = au_h_iptr(inode, bindex);
13723+
13724+ f = h_inode->i_sb->s_export_op->commit_metadata;
13725+ if (f)
13726+ err = f(h_inode);
13727+ else {
13728+ struct writeback_control wbc = {
13729+ .sync_mode = WB_SYNC_ALL,
13730+ .nr_to_write = 0 /* metadata only */
13731+ };
13732+
13733+ err = sync_inode(h_inode, &wbc);
13734+ }
13735+
13736+ au_cpup_attr_timesizes(inode);
13737+ ii_write_unlock(inode);
13738+ si_read_unlock(sb);
13739+ return err;
13740+}
13741+
13742+/* ---------------------------------------------------------------------- */
13743+
1facf9fc 13744+static struct export_operations aufs_export_op = {
4a4d8108 13745+ .fh_to_dentry = aufs_fh_to_dentry,
1facf9fc 13746+ /* .fh_to_parent = aufs_fh_to_parent, */
4a4d8108
AM
13747+ .encode_fh = aufs_encode_fh,
13748+ .commit_metadata = aufs_commit_metadata
1facf9fc 13749+};
13750+
13751+void au_export_init(struct super_block *sb)
13752+{
13753+ struct au_sbinfo *sbinfo;
13754+ __u32 u;
13755+
5afbbe0d
AM
13756+ BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13757+ && IS_MODULE(CONFIG_EXPORTFS),
13758+ AUFS_NAME ": unsupported configuration "
13759+ "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13760+
1facf9fc 13761+ sb->s_export_op = &aufs_export_op;
13762+ sbinfo = au_sbi(sb);
13763+ sbinfo->si_xigen = NULL;
13764+ get_random_bytes(&u, sizeof(u));
13765+ BUILD_BUG_ON(sizeof(u) != sizeof(int));
13766+ atomic_set(&sbinfo->si_xigen_next, u);
13767+}
076b876e
AM
13768diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13769--- /usr/share/empty/fs/aufs/fhsm.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 13770+++ linux/fs/aufs/fhsm.c 2018-08-12 23:43:05.456791299 +0200
062440b3 13771@@ -0,0 +1,427 @@
cd7a4cd9 13772+// SPDX-License-Identifier: GPL-2.0
076b876e 13773+/*
b00004a5 13774+ * Copyright (C) 2011-2018 Junjiro R. Okajima
076b876e
AM
13775+ *
13776+ * This program, aufs is free software; you can redistribute it and/or modify
13777+ * it under the terms of the GNU General Public License as published by
13778+ * the Free Software Foundation; either version 2 of the License, or
13779+ * (at your option) any later version.
13780+ *
13781+ * This program is distributed in the hope that it will be useful,
13782+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13783+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13784+ * GNU General Public License for more details.
13785+ *
13786+ * You should have received a copy of the GNU General Public License
13787+ * along with this program; if not, write to the Free Software
13788+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
13789+ */
13790+
13791+/*
13792+ * File-based Hierarchy Storage Management
13793+ */
13794+
13795+#include <linux/anon_inodes.h>
13796+#include <linux/poll.h>
13797+#include <linux/seq_file.h>
13798+#include <linux/statfs.h>
13799+#include "aufs.h"
13800+
c1595e42
JR
13801+static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13802+{
13803+ struct au_sbinfo *sbinfo;
13804+ struct au_fhsm *fhsm;
13805+
13806+ SiMustAnyLock(sb);
13807+
13808+ sbinfo = au_sbi(sb);
13809+ fhsm = &sbinfo->si_fhsm;
13810+ AuDebugOn(!fhsm);
13811+ return fhsm->fhsm_bottom;
13812+}
13813+
13814+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13815+{
13816+ struct au_sbinfo *sbinfo;
13817+ struct au_fhsm *fhsm;
13818+
13819+ SiMustWriteLock(sb);
13820+
13821+ sbinfo = au_sbi(sb);
13822+ fhsm = &sbinfo->si_fhsm;
13823+ AuDebugOn(!fhsm);
13824+ fhsm->fhsm_bottom = bindex;
13825+}
13826+
13827+/* ---------------------------------------------------------------------- */
13828+
076b876e
AM
13829+static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13830+{
13831+ struct au_br_fhsm *bf;
13832+
13833+ bf = br->br_fhsm;
13834+ MtxMustLock(&bf->bf_lock);
13835+
13836+ return !bf->bf_readable
13837+ || time_after(jiffies,
13838+ bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13839+}
13840+
13841+/* ---------------------------------------------------------------------- */
13842+
13843+static void au_fhsm_notify(struct super_block *sb, int val)
13844+{
13845+ struct au_sbinfo *sbinfo;
13846+ struct au_fhsm *fhsm;
13847+
13848+ SiMustAnyLock(sb);
13849+
13850+ sbinfo = au_sbi(sb);
13851+ fhsm = &sbinfo->si_fhsm;
13852+ if (au_fhsm_pid(fhsm)
13853+ && atomic_read(&fhsm->fhsm_readable) != -1) {
13854+ atomic_set(&fhsm->fhsm_readable, val);
13855+ if (val)
13856+ wake_up(&fhsm->fhsm_wqh);
13857+ }
13858+}
13859+
13860+static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13861+ struct aufs_stfs *rstfs, int do_lock, int do_notify)
13862+{
13863+ int err;
13864+ struct au_branch *br;
13865+ struct au_br_fhsm *bf;
13866+
13867+ br = au_sbr(sb, bindex);
13868+ AuDebugOn(au_br_rdonly(br));
13869+ bf = br->br_fhsm;
13870+ AuDebugOn(!bf);
13871+
13872+ if (do_lock)
13873+ mutex_lock(&bf->bf_lock);
13874+ else
13875+ MtxMustLock(&bf->bf_lock);
13876+
13877+ /* sb->s_root for NFS is unreliable */
13878+ err = au_br_stfs(br, &bf->bf_stfs);
13879+ if (unlikely(err)) {
13880+ AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13881+ goto out;
13882+ }
13883+
13884+ bf->bf_jiffy = jiffies;
13885+ bf->bf_readable = 1;
13886+ if (do_notify)
13887+ au_fhsm_notify(sb, /*val*/1);
13888+ if (rstfs)
13889+ *rstfs = bf->bf_stfs;
13890+
13891+out:
13892+ if (do_lock)
13893+ mutex_unlock(&bf->bf_lock);
13894+ au_fhsm_notify(sb, /*val*/1);
13895+
13896+ return err;
13897+}
13898+
13899+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13900+{
13901+ int err;
076b876e
AM
13902+ struct au_sbinfo *sbinfo;
13903+ struct au_fhsm *fhsm;
13904+ struct au_branch *br;
13905+ struct au_br_fhsm *bf;
13906+
13907+ AuDbg("b%d, force %d\n", bindex, force);
13908+ SiMustAnyLock(sb);
13909+
13910+ sbinfo = au_sbi(sb);
13911+ fhsm = &sbinfo->si_fhsm;
c1595e42
JR
13912+ if (!au_ftest_si(sbinfo, FHSM)
13913+ || fhsm->fhsm_bottom == bindex)
076b876e
AM
13914+ return;
13915+
13916+ br = au_sbr(sb, bindex);
13917+ bf = br->br_fhsm;
13918+ AuDebugOn(!bf);
13919+ mutex_lock(&bf->bf_lock);
13920+ if (force
13921+ || au_fhsm_pid(fhsm)
13922+ || au_fhsm_test_jiffy(sbinfo, br))
13923+ err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13924+ /*do_notify*/1);
13925+ mutex_unlock(&bf->bf_lock);
13926+}
13927+
13928+void au_fhsm_wrote_all(struct super_block *sb, int force)
13929+{
5afbbe0d 13930+ aufs_bindex_t bindex, bbot;
076b876e
AM
13931+ struct au_branch *br;
13932+
13933+ /* exclude the bottom */
5afbbe0d
AM
13934+ bbot = au_fhsm_bottom(sb);
13935+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
13936+ br = au_sbr(sb, bindex);
13937+ if (au_br_fhsm(br->br_perm))
13938+ au_fhsm_wrote(sb, bindex, force);
13939+ }
13940+}
13941+
13942+/* ---------------------------------------------------------------------- */
13943+
be118d29 13944+static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
076b876e 13945+{
be118d29 13946+ __poll_t mask;
076b876e
AM
13947+ struct au_sbinfo *sbinfo;
13948+ struct au_fhsm *fhsm;
13949+
13950+ mask = 0;
13951+ sbinfo = file->private_data;
13952+ fhsm = &sbinfo->si_fhsm;
13953+ poll_wait(file, &fhsm->fhsm_wqh, wait);
13954+ if (atomic_read(&fhsm->fhsm_readable))
be118d29 13955+ mask = EPOLLIN /* | EPOLLRDNORM */;
076b876e 13956+
b00004a5
AM
13957+ if (!mask)
13958+ AuDbg("mask 0x%x\n", mask);
076b876e
AM
13959+ return mask;
13960+}
13961+
13962+static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13963+ struct aufs_stfs *stfs, __s16 brid)
13964+{
13965+ int err;
13966+
13967+ err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13968+ if (!err)
13969+ err = __put_user(brid, &stbr->brid);
13970+ if (unlikely(err))
13971+ err = -EFAULT;
13972+
13973+ return err;
13974+}
13975+
13976+static ssize_t au_fhsm_do_read(struct super_block *sb,
13977+ struct aufs_stbr __user *stbr, size_t count)
13978+{
13979+ ssize_t err;
13980+ int nstbr;
5afbbe0d 13981+ aufs_bindex_t bindex, bbot;
076b876e
AM
13982+ struct au_branch *br;
13983+ struct au_br_fhsm *bf;
13984+
13985+ /* except the bottom branch */
13986+ err = 0;
13987+ nstbr = 0;
5afbbe0d
AM
13988+ bbot = au_fhsm_bottom(sb);
13989+ for (bindex = 0; !err && bindex < bbot; bindex++) {
076b876e
AM
13990+ br = au_sbr(sb, bindex);
13991+ if (!au_br_fhsm(br->br_perm))
13992+ continue;
13993+
13994+ bf = br->br_fhsm;
13995+ mutex_lock(&bf->bf_lock);
13996+ if (bf->bf_readable) {
13997+ err = -EFAULT;
13998+ if (count >= sizeof(*stbr))
13999+ err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
14000+ br->br_id);
14001+ if (!err) {
14002+ bf->bf_readable = 0;
14003+ count -= sizeof(*stbr);
14004+ nstbr++;
14005+ }
14006+ }
14007+ mutex_unlock(&bf->bf_lock);
14008+ }
14009+ if (!err)
14010+ err = sizeof(*stbr) * nstbr;
14011+
14012+ return err;
14013+}
14014+
14015+static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
14016+ loff_t *pos)
14017+{
14018+ ssize_t err;
14019+ int readable;
5afbbe0d 14020+ aufs_bindex_t nfhsm, bindex, bbot;
076b876e
AM
14021+ struct au_sbinfo *sbinfo;
14022+ struct au_fhsm *fhsm;
14023+ struct au_branch *br;
14024+ struct super_block *sb;
14025+
14026+ err = 0;
14027+ sbinfo = file->private_data;
14028+ fhsm = &sbinfo->si_fhsm;
14029+need_data:
14030+ spin_lock_irq(&fhsm->fhsm_wqh.lock);
14031+ if (!atomic_read(&fhsm->fhsm_readable)) {
14032+ if (vfsub_file_flags(file) & O_NONBLOCK)
14033+ err = -EAGAIN;
14034+ else
14035+ err = wait_event_interruptible_locked_irq
14036+ (fhsm->fhsm_wqh,
14037+ atomic_read(&fhsm->fhsm_readable));
14038+ }
14039+ spin_unlock_irq(&fhsm->fhsm_wqh.lock);
14040+ if (unlikely(err))
14041+ goto out;
14042+
14043+ /* sb may already be dead */
14044+ au_rw_read_lock(&sbinfo->si_rwsem);
14045+ readable = atomic_read(&fhsm->fhsm_readable);
14046+ if (readable > 0) {
14047+ sb = sbinfo->si_sb;
14048+ AuDebugOn(!sb);
14049+ /* exclude the bottom branch */
14050+ nfhsm = 0;
5afbbe0d
AM
14051+ bbot = au_fhsm_bottom(sb);
14052+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
14053+ br = au_sbr(sb, bindex);
14054+ if (au_br_fhsm(br->br_perm))
14055+ nfhsm++;
14056+ }
14057+ err = -EMSGSIZE;
14058+ if (nfhsm * sizeof(struct aufs_stbr) <= count) {
14059+ atomic_set(&fhsm->fhsm_readable, 0);
14060+ err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
14061+ count);
14062+ }
14063+ }
14064+ au_rw_read_unlock(&sbinfo->si_rwsem);
14065+ if (!readable)
14066+ goto need_data;
14067+
14068+out:
14069+ return err;
14070+}
14071+
14072+static int au_fhsm_release(struct inode *inode, struct file *file)
14073+{
14074+ struct au_sbinfo *sbinfo;
14075+ struct au_fhsm *fhsm;
14076+
14077+ /* sb may already be dead */
14078+ sbinfo = file->private_data;
14079+ fhsm = &sbinfo->si_fhsm;
14080+ spin_lock(&fhsm->fhsm_spin);
14081+ fhsm->fhsm_pid = 0;
14082+ spin_unlock(&fhsm->fhsm_spin);
14083+ kobject_put(&sbinfo->si_kobj);
14084+
14085+ return 0;
14086+}
14087+
14088+static const struct file_operations au_fhsm_fops = {
14089+ .owner = THIS_MODULE,
14090+ .llseek = noop_llseek,
14091+ .read = au_fhsm_read,
14092+ .poll = au_fhsm_poll,
14093+ .release = au_fhsm_release
14094+};
14095+
14096+int au_fhsm_fd(struct super_block *sb, int oflags)
14097+{
14098+ int err, fd;
14099+ struct au_sbinfo *sbinfo;
14100+ struct au_fhsm *fhsm;
14101+
14102+ err = -EPERM;
14103+ if (unlikely(!capable(CAP_SYS_ADMIN)))
14104+ goto out;
14105+
14106+ err = -EINVAL;
14107+ if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
14108+ goto out;
14109+
14110+ err = 0;
14111+ sbinfo = au_sbi(sb);
14112+ fhsm = &sbinfo->si_fhsm;
14113+ spin_lock(&fhsm->fhsm_spin);
14114+ if (!fhsm->fhsm_pid)
14115+ fhsm->fhsm_pid = current->pid;
14116+ else
14117+ err = -EBUSY;
14118+ spin_unlock(&fhsm->fhsm_spin);
14119+ if (unlikely(err))
14120+ goto out;
14121+
14122+ oflags |= O_RDONLY;
14123+ /* oflags |= FMODE_NONOTIFY; */
14124+ fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
14125+ err = fd;
14126+ if (unlikely(fd < 0))
14127+ goto out_pid;
14128+
14129+ /* succeed reglardless 'fhsm' status */
14130+ kobject_get(&sbinfo->si_kobj);
14131+ si_noflush_read_lock(sb);
14132+ if (au_ftest_si(sbinfo, FHSM))
14133+ au_fhsm_wrote_all(sb, /*force*/0);
14134+ si_read_unlock(sb);
14135+ goto out; /* success */
14136+
14137+out_pid:
14138+ spin_lock(&fhsm->fhsm_spin);
14139+ fhsm->fhsm_pid = 0;
14140+ spin_unlock(&fhsm->fhsm_spin);
14141+out:
14142+ AuTraceErr(err);
14143+ return err;
14144+}
14145+
14146+/* ---------------------------------------------------------------------- */
14147+
14148+int au_fhsm_br_alloc(struct au_branch *br)
14149+{
14150+ int err;
14151+
14152+ err = 0;
14153+ br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14154+ if (br->br_fhsm)
14155+ au_br_fhsm_init(br->br_fhsm);
14156+ else
14157+ err = -ENOMEM;
14158+
14159+ return err;
14160+}
14161+
14162+/* ---------------------------------------------------------------------- */
14163+
14164+void au_fhsm_fin(struct super_block *sb)
14165+{
14166+ au_fhsm_notify(sb, /*val*/-1);
14167+}
14168+
14169+void au_fhsm_init(struct au_sbinfo *sbinfo)
14170+{
14171+ struct au_fhsm *fhsm;
14172+
14173+ fhsm = &sbinfo->si_fhsm;
14174+ spin_lock_init(&fhsm->fhsm_spin);
14175+ init_waitqueue_head(&fhsm->fhsm_wqh);
14176+ atomic_set(&fhsm->fhsm_readable, 0);
14177+ fhsm->fhsm_expire
14178+ = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
c1595e42 14179+ fhsm->fhsm_bottom = -1;
076b876e
AM
14180+}
14181+
14182+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14183+{
14184+ sbinfo->si_fhsm.fhsm_expire
14185+ = msecs_to_jiffies(sec * MSEC_PER_SEC);
14186+}
14187+
14188+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14189+{
14190+ unsigned int u;
14191+
14192+ if (!au_ftest_si(sbinfo, FHSM))
14193+ return;
14194+
14195+ u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14196+ if (u != AUFS_FHSM_CACHE_DEF_SEC)
14197+ seq_printf(seq, ",fhsm_sec=%u", u);
14198+}
7f207e10
AM
14199diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14200--- /usr/share/empty/fs/aufs/file.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 14201+++ linux/fs/aufs/file.c 2018-08-12 23:43:05.456791299 +0200
062440b3 14202@@ -0,0 +1,857 @@
cd7a4cd9 14203+// SPDX-License-Identifier: GPL-2.0
1facf9fc 14204+/*
b00004a5 14205+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 14206+ *
14207+ * This program, aufs is free software; you can redistribute it and/or modify
14208+ * it under the terms of the GNU General Public License as published by
14209+ * the Free Software Foundation; either version 2 of the License, or
14210+ * (at your option) any later version.
dece6358
AM
14211+ *
14212+ * This program is distributed in the hope that it will be useful,
14213+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14214+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14215+ * GNU General Public License for more details.
14216+ *
14217+ * You should have received a copy of the GNU General Public License
523b37e3 14218+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 14219+ */
14220+
14221+/*
4a4d8108 14222+ * handling file/dir, and address_space operation
1facf9fc 14223+ */
14224+
7eafdf33
AM
14225+#ifdef CONFIG_AUFS_DEBUG
14226+#include <linux/migrate.h>
14227+#endif
4a4d8108 14228+#include <linux/pagemap.h>
1facf9fc 14229+#include "aufs.h"
14230+
4a4d8108
AM
14231+/* drop flags for writing */
14232+unsigned int au_file_roflags(unsigned int flags)
14233+{
14234+ flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14235+ flags |= O_RDONLY | O_NOATIME;
14236+ return flags;
14237+}
14238+
14239+/* common functions to regular file and dir */
14240+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 14241+ struct file *file, int force_wr)
1facf9fc 14242+{
1308ab2a 14243+ struct file *h_file;
4a4d8108
AM
14244+ struct dentry *h_dentry;
14245+ struct inode *h_inode;
14246+ struct super_block *sb;
14247+ struct au_branch *br;
14248+ struct path h_path;
b912730e 14249+ int err;
1facf9fc 14250+
4a4d8108
AM
14251+ /* a race condition can happen between open and unlink/rmdir */
14252+ h_file = ERR_PTR(-ENOENT);
14253+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 14254+ if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
4a4d8108 14255+ goto out;
5527c038 14256+ h_inode = d_inode(h_dentry);
027c5e7a
AM
14257+ spin_lock(&h_dentry->d_lock);
14258+ err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
5527c038 14259+ /* || !d_inode(dentry)->i_nlink */
027c5e7a
AM
14260+ ;
14261+ spin_unlock(&h_dentry->d_lock);
14262+ if (unlikely(err))
4a4d8108 14263+ goto out;
1facf9fc 14264+
4a4d8108
AM
14265+ sb = dentry->d_sb;
14266+ br = au_sbr(sb, bindex);
b912730e
AM
14267+ err = au_br_test_oflag(flags, br);
14268+ h_file = ERR_PTR(err);
14269+ if (unlikely(err))
027c5e7a 14270+ goto out;
1facf9fc 14271+
4a4d8108 14272+ /* drop flags for writing */
5527c038 14273+ if (au_test_ro(sb, bindex, d_inode(dentry))) {
392086de
AM
14274+ if (force_wr && !(flags & O_WRONLY))
14275+ force_wr = 0;
4a4d8108 14276+ flags = au_file_roflags(flags);
392086de
AM
14277+ if (force_wr) {
14278+ h_file = ERR_PTR(-EROFS);
14279+ flags = au_file_roflags(flags);
14280+ if (unlikely(vfsub_native_ro(h_inode)
14281+ || IS_APPEND(h_inode)))
14282+ goto out;
14283+ flags &= ~O_ACCMODE;
14284+ flags |= O_WRONLY;
14285+ }
14286+ }
4a4d8108 14287+ flags &= ~O_CREAT;
5afbbe0d 14288+ au_br_get(br);
4a4d8108 14289+ h_path.dentry = h_dentry;
86dc4139 14290+ h_path.mnt = au_br_mnt(br);
38d290e6 14291+ h_file = vfsub_dentry_open(&h_path, flags);
4a4d8108
AM
14292+ if (IS_ERR(h_file))
14293+ goto out_br;
dece6358 14294+
b912730e 14295+ if (flags & __FMODE_EXEC) {
4a4d8108
AM
14296+ err = deny_write_access(h_file);
14297+ if (unlikely(err)) {
14298+ fput(h_file);
14299+ h_file = ERR_PTR(err);
14300+ goto out_br;
14301+ }
14302+ }
953406b4 14303+ fsnotify_open(h_file);
4a4d8108 14304+ goto out; /* success */
1facf9fc 14305+
4f0767ce 14306+out_br:
5afbbe0d 14307+ au_br_put(br);
4f0767ce 14308+out:
4a4d8108
AM
14309+ return h_file;
14310+}
1308ab2a 14311+
076b876e
AM
14312+static int au_cmoo(struct dentry *dentry)
14313+{
8b6a4947 14314+ int err, cmoo, matched;
076b876e
AM
14315+ unsigned int udba;
14316+ struct path h_path;
14317+ struct au_pin pin;
14318+ struct au_cp_generic cpg = {
14319+ .dentry = dentry,
14320+ .bdst = -1,
14321+ .bsrc = -1,
14322+ .len = -1,
14323+ .pin = &pin,
14324+ .flags = AuCpup_DTIME | AuCpup_HOPEN
14325+ };
7e9cd9fe 14326+ struct inode *delegated;
076b876e
AM
14327+ struct super_block *sb;
14328+ struct au_sbinfo *sbinfo;
14329+ struct au_fhsm *fhsm;
14330+ pid_t pid;
14331+ struct au_branch *br;
14332+ struct dentry *parent;
14333+ struct au_hinode *hdir;
14334+
14335+ DiMustWriteLock(dentry);
5527c038 14336+ IiMustWriteLock(d_inode(dentry));
076b876e
AM
14337+
14338+ err = 0;
14339+ if (IS_ROOT(dentry))
14340+ goto out;
5afbbe0d 14341+ cpg.bsrc = au_dbtop(dentry);
076b876e
AM
14342+ if (!cpg.bsrc)
14343+ goto out;
14344+
14345+ sb = dentry->d_sb;
14346+ sbinfo = au_sbi(sb);
14347+ fhsm = &sbinfo->si_fhsm;
14348+ pid = au_fhsm_pid(fhsm);
8b6a4947
AM
14349+ rcu_read_lock();
14350+ matched = (pid
14351+ && (current->pid == pid
14352+ || rcu_dereference(current->real_parent)->pid == pid));
14353+ rcu_read_unlock();
14354+ if (matched)
076b876e
AM
14355+ goto out;
14356+
14357+ br = au_sbr(sb, cpg.bsrc);
14358+ cmoo = au_br_cmoo(br->br_perm);
14359+ if (!cmoo)
14360+ goto out;
7e9cd9fe 14361+ if (!d_is_reg(dentry))
076b876e
AM
14362+ cmoo &= AuBrAttr_COO_ALL;
14363+ if (!cmoo)
14364+ goto out;
14365+
14366+ parent = dget_parent(dentry);
14367+ di_write_lock_parent(parent);
14368+ err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14369+ cpg.bdst = err;
14370+ if (unlikely(err < 0)) {
14371+ err = 0; /* there is no upper writable branch */
14372+ goto out_dgrade;
14373+ }
14374+ AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14375+
14376+ /* do not respect the coo attrib for the target branch */
14377+ err = au_cpup_dirs(dentry, cpg.bdst);
14378+ if (unlikely(err))
14379+ goto out_dgrade;
14380+
14381+ di_downgrade_lock(parent, AuLock_IR);
14382+ udba = au_opt_udba(sb);
14383+ err = au_pin(&pin, dentry, cpg.bdst, udba,
14384+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14385+ if (unlikely(err))
14386+ goto out_parent;
14387+
14388+ err = au_sio_cpup_simple(&cpg);
14389+ au_unpin(&pin);
14390+ if (unlikely(err))
14391+ goto out_parent;
14392+ if (!(cmoo & AuBrWAttr_MOO))
14393+ goto out_parent; /* success */
14394+
14395+ err = au_pin(&pin, dentry, cpg.bsrc, udba,
14396+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14397+ if (unlikely(err))
14398+ goto out_parent;
14399+
14400+ h_path.mnt = au_br_mnt(br);
14401+ h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
5527c038 14402+ hdir = au_hi(d_inode(parent), cpg.bsrc);
076b876e
AM
14403+ delegated = NULL;
14404+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14405+ au_unpin(&pin);
14406+ /* todo: keep h_dentry or not? */
14407+ if (unlikely(err == -EWOULDBLOCK)) {
14408+ pr_warn("cannot retry for NFSv4 delegation"
14409+ " for an internal unlink\n");
14410+ iput(delegated);
14411+ }
14412+ if (unlikely(err)) {
14413+ pr_err("unlink %pd after coo failed (%d), ignored\n",
14414+ dentry, err);
14415+ err = 0;
14416+ }
14417+ goto out_parent; /* success */
14418+
14419+out_dgrade:
14420+ di_downgrade_lock(parent, AuLock_IR);
14421+out_parent:
14422+ di_read_unlock(parent, AuLock_IR);
14423+ dput(parent);
14424+out:
14425+ AuTraceErr(err);
14426+ return err;
14427+}
14428+
b912730e 14429+int au_do_open(struct file *file, struct au_do_open_args *args)
1facf9fc 14430+{
8b6a4947 14431+ int err, aopen = args->aopen;
1facf9fc 14432+ struct dentry *dentry;
076b876e 14433+ struct au_finfo *finfo;
1308ab2a 14434+
8b6a4947 14435+ if (!aopen)
b912730e
AM
14436+ err = au_finfo_init(file, args->fidir);
14437+ else {
14438+ lockdep_off();
14439+ err = au_finfo_init(file, args->fidir);
14440+ lockdep_on();
14441+ }
4a4d8108
AM
14442+ if (unlikely(err))
14443+ goto out;
1facf9fc 14444+
2000de60 14445+ dentry = file->f_path.dentry;
b912730e 14446+ AuDebugOn(IS_ERR_OR_NULL(dentry));
8b6a4947
AM
14447+ di_write_lock_child(dentry);
14448+ err = au_cmoo(dentry);
14449+ di_downgrade_lock(dentry, AuLock_IR);
14450+ if (!err) {
14451+ if (!aopen)
b912730e 14452+ err = args->open(file, vfsub_file_flags(file), NULL);
8b6a4947
AM
14453+ else {
14454+ lockdep_off();
14455+ err = args->open(file, vfsub_file_flags(file), NULL);
14456+ lockdep_on();
14457+ }
b912730e 14458+ }
8b6a4947 14459+ di_read_unlock(dentry, AuLock_IR);
1facf9fc 14460+
076b876e
AM
14461+ finfo = au_fi(file);
14462+ if (!err) {
14463+ finfo->fi_file = file;
8b6a4947
AM
14464+ au_hbl_add(&finfo->fi_hlist,
14465+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
076b876e 14466+ }
8b6a4947 14467+ if (!aopen)
b912730e
AM
14468+ fi_write_unlock(file);
14469+ else {
14470+ lockdep_off();
14471+ fi_write_unlock(file);
14472+ lockdep_on();
14473+ }
4a4d8108 14474+ if (unlikely(err)) {
076b876e 14475+ finfo->fi_hdir = NULL;
1c60b727 14476+ au_finfo_fin(file);
1308ab2a 14477+ }
4a4d8108 14478+
4f0767ce 14479+out:
8b6a4947 14480+ AuTraceErr(err);
1308ab2a 14481+ return err;
14482+}
dece6358 14483+
4a4d8108 14484+int au_reopen_nondir(struct file *file)
1308ab2a 14485+{
4a4d8108 14486+ int err;
5afbbe0d 14487+ aufs_bindex_t btop;
4a4d8108
AM
14488+ struct dentry *dentry;
14489+ struct file *h_file, *h_file_tmp;
1308ab2a 14490+
2000de60 14491+ dentry = file->f_path.dentry;
5afbbe0d 14492+ btop = au_dbtop(dentry);
4a4d8108 14493+ h_file_tmp = NULL;
5afbbe0d 14494+ if (au_fbtop(file) == btop) {
4a4d8108
AM
14495+ h_file = au_hf_top(file);
14496+ if (file->f_mode == h_file->f_mode)
14497+ return 0; /* success */
14498+ h_file_tmp = h_file;
14499+ get_file(h_file_tmp);
5afbbe0d 14500+ au_set_h_fptr(file, btop, NULL);
4a4d8108
AM
14501+ }
14502+ AuDebugOn(au_fi(file)->fi_hdir);
86dc4139
AM
14503+ /*
14504+ * it can happen
14505+ * file exists on both of rw and ro
5afbbe0d 14506+ * open --> dbtop and fbtop are both 0
86dc4139
AM
14507+ * prepend a branch as rw, "rw" become ro
14508+ * remove rw/file
14509+ * delete the top branch, "rw" becomes rw again
5afbbe0d
AM
14510+ * --> dbtop is 1, fbtop is still 0
14511+ * write --> fbtop is 0 but dbtop is 1
86dc4139 14512+ */
5afbbe0d 14513+ /* AuDebugOn(au_fbtop(file) < btop); */
1308ab2a 14514+
5afbbe0d 14515+ h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
392086de 14516+ file, /*force_wr*/0);
4a4d8108 14517+ err = PTR_ERR(h_file);
86dc4139
AM
14518+ if (IS_ERR(h_file)) {
14519+ if (h_file_tmp) {
5afbbe0d
AM
14520+ au_sbr_get(dentry->d_sb, btop);
14521+ au_set_h_fptr(file, btop, h_file_tmp);
86dc4139
AM
14522+ h_file_tmp = NULL;
14523+ }
4a4d8108 14524+ goto out; /* todo: close all? */
86dc4139 14525+ }
4a4d8108
AM
14526+
14527+ err = 0;
5afbbe0d
AM
14528+ au_set_fbtop(file, btop);
14529+ au_set_h_fptr(file, btop, h_file);
4a4d8108
AM
14530+ au_update_figen(file);
14531+ /* todo: necessary? */
14532+ /* file->f_ra = h_file->f_ra; */
14533+
4f0767ce 14534+out:
4a4d8108
AM
14535+ if (h_file_tmp)
14536+ fput(h_file_tmp);
14537+ return err;
1facf9fc 14538+}
14539+
1308ab2a 14540+/* ---------------------------------------------------------------------- */
14541+
4a4d8108
AM
14542+static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14543+ struct dentry *hi_wh)
1facf9fc 14544+{
4a4d8108 14545+ int err;
5afbbe0d 14546+ aufs_bindex_t btop;
4a4d8108
AM
14547+ struct au_dinfo *dinfo;
14548+ struct dentry *h_dentry;
14549+ struct au_hdentry *hdp;
1facf9fc 14550+
2000de60 14551+ dinfo = au_di(file->f_path.dentry);
4a4d8108 14552+ AuRwMustWriteLock(&dinfo->di_rwsem);
dece6358 14553+
5afbbe0d
AM
14554+ btop = dinfo->di_btop;
14555+ dinfo->di_btop = btgt;
14556+ hdp = au_hdentry(dinfo, btgt);
14557+ h_dentry = hdp->hd_dentry;
14558+ hdp->hd_dentry = hi_wh;
4a4d8108 14559+ err = au_reopen_nondir(file);
5afbbe0d
AM
14560+ hdp->hd_dentry = h_dentry;
14561+ dinfo->di_btop = btop;
1facf9fc 14562+
1facf9fc 14563+ return err;
14564+}
14565+
4a4d8108 14566+static int au_ready_to_write_wh(struct file *file, loff_t len,
86dc4139 14567+ aufs_bindex_t bcpup, struct au_pin *pin)
1facf9fc 14568+{
4a4d8108 14569+ int err;
027c5e7a 14570+ struct inode *inode, *h_inode;
c2b27bf2
AM
14571+ struct dentry *h_dentry, *hi_wh;
14572+ struct au_cp_generic cpg = {
2000de60 14573+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14574+ .bdst = bcpup,
14575+ .bsrc = -1,
14576+ .len = len,
14577+ .pin = pin
14578+ };
1facf9fc 14579+
5afbbe0d 14580+ au_update_dbtop(cpg.dentry);
5527c038 14581+ inode = d_inode(cpg.dentry);
027c5e7a 14582+ h_inode = NULL;
5afbbe0d
AM
14583+ if (au_dbtop(cpg.dentry) <= bcpup
14584+ && au_dbbot(cpg.dentry) >= bcpup) {
c2b27bf2 14585+ h_dentry = au_h_dptr(cpg.dentry, bcpup);
5527c038
JR
14586+ if (h_dentry && d_is_positive(h_dentry))
14587+ h_inode = d_inode(h_dentry);
027c5e7a 14588+ }
4a4d8108 14589+ hi_wh = au_hi_wh(inode, bcpup);
027c5e7a 14590+ if (!hi_wh && !h_inode)
c2b27bf2 14591+ err = au_sio_cpup_wh(&cpg, file);
4a4d8108
AM
14592+ else
14593+ /* already copied-up after unlink */
14594+ err = au_reopen_wh(file, bcpup, hi_wh);
1facf9fc 14595+
4a4d8108 14596+ if (!err
38d290e6
JR
14597+ && (inode->i_nlink > 1
14598+ || (inode->i_state & I_LINKABLE))
c2b27bf2
AM
14599+ && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14600+ au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
1308ab2a 14601+
dece6358 14602+ return err;
1facf9fc 14603+}
14604+
4a4d8108
AM
14605+/*
14606+ * prepare the @file for writing.
14607+ */
14608+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
1facf9fc 14609+{
4a4d8108 14610+ int err;
5afbbe0d 14611+ aufs_bindex_t dbtop;
c1595e42 14612+ struct dentry *parent;
86dc4139 14613+ struct inode *inode;
1facf9fc 14614+ struct super_block *sb;
4a4d8108 14615+ struct file *h_file;
c2b27bf2 14616+ struct au_cp_generic cpg = {
2000de60 14617+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14618+ .bdst = -1,
14619+ .bsrc = -1,
14620+ .len = len,
14621+ .pin = pin,
14622+ .flags = AuCpup_DTIME
14623+ };
1facf9fc 14624+
c2b27bf2 14625+ sb = cpg.dentry->d_sb;
5527c038 14626+ inode = d_inode(cpg.dentry);
5afbbe0d 14627+ cpg.bsrc = au_fbtop(file);
c2b27bf2 14628+ err = au_test_ro(sb, cpg.bsrc, inode);
4a4d8108 14629+ if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
c2b27bf2
AM
14630+ err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14631+ /*flags*/0);
1facf9fc 14632+ goto out;
4a4d8108 14633+ }
1facf9fc 14634+
027c5e7a 14635+ /* need to cpup or reopen */
c2b27bf2 14636+ parent = dget_parent(cpg.dentry);
4a4d8108 14637+ di_write_lock_parent(parent);
c2b27bf2
AM
14638+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14639+ cpg.bdst = err;
4a4d8108
AM
14640+ if (unlikely(err < 0))
14641+ goto out_dgrade;
14642+ err = 0;
14643+
c2b27bf2
AM
14644+ if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14645+ err = au_cpup_dirs(cpg.dentry, cpg.bdst);
1facf9fc 14646+ if (unlikely(err))
4a4d8108
AM
14647+ goto out_dgrade;
14648+ }
14649+
c2b27bf2 14650+ err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108
AM
14651+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14652+ if (unlikely(err))
14653+ goto out_dgrade;
14654+
5afbbe0d
AM
14655+ dbtop = au_dbtop(cpg.dentry);
14656+ if (dbtop <= cpg.bdst)
c2b27bf2 14657+ cpg.bsrc = cpg.bdst;
027c5e7a 14658+
5afbbe0d 14659+ if (dbtop <= cpg.bdst /* just reopen */
c2b27bf2 14660+ || !d_unhashed(cpg.dentry) /* copyup and reopen */
027c5e7a 14661+ ) {
392086de 14662+ h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
86dc4139 14663+ if (IS_ERR(h_file))
027c5e7a 14664+ err = PTR_ERR(h_file);
86dc4139 14665+ else {
027c5e7a 14666+ di_downgrade_lock(parent, AuLock_IR);
5afbbe0d 14667+ if (dbtop > cpg.bdst)
c2b27bf2 14668+ err = au_sio_cpup_simple(&cpg);
027c5e7a
AM
14669+ if (!err)
14670+ err = au_reopen_nondir(file);
c2b27bf2 14671+ au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
027c5e7a 14672+ }
027c5e7a
AM
14673+ } else { /* copyup as wh and reopen */
14674+ /*
14675+ * since writable hfsplus branch is not supported,
14676+ * h_open_pre/post() are unnecessary.
14677+ */
c2b27bf2 14678+ err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
4a4d8108 14679+ di_downgrade_lock(parent, AuLock_IR);
4a4d8108 14680+ }
4a4d8108
AM
14681+
14682+ if (!err) {
14683+ au_pin_set_parent_lflag(pin, /*lflag*/0);
14684+ goto out_dput; /* success */
14685+ }
14686+ au_unpin(pin);
14687+ goto out_unlock;
1facf9fc 14688+
4f0767ce 14689+out_dgrade:
4a4d8108 14690+ di_downgrade_lock(parent, AuLock_IR);
4f0767ce 14691+out_unlock:
4a4d8108 14692+ di_read_unlock(parent, AuLock_IR);
4f0767ce 14693+out_dput:
4a4d8108 14694+ dput(parent);
4f0767ce 14695+out:
1facf9fc 14696+ return err;
14697+}
14698+
4a4d8108
AM
14699+/* ---------------------------------------------------------------------- */
14700+
14701+int au_do_flush(struct file *file, fl_owner_t id,
14702+ int (*flush)(struct file *file, fl_owner_t id))
1facf9fc 14703+{
4a4d8108 14704+ int err;
1facf9fc 14705+ struct super_block *sb;
4a4d8108 14706+ struct inode *inode;
1facf9fc 14707+
c06a8ce3
AM
14708+ inode = file_inode(file);
14709+ sb = inode->i_sb;
4a4d8108
AM
14710+ si_noflush_read_lock(sb);
14711+ fi_read_lock(file);
b752ccd1 14712+ ii_read_lock_child(inode);
1facf9fc 14713+
4a4d8108
AM
14714+ err = flush(file, id);
14715+ au_cpup_attr_timesizes(inode);
1facf9fc 14716+
b752ccd1 14717+ ii_read_unlock(inode);
4a4d8108 14718+ fi_read_unlock(file);
1308ab2a 14719+ si_read_unlock(sb);
dece6358 14720+ return err;
1facf9fc 14721+}
14722+
4a4d8108
AM
14723+/* ---------------------------------------------------------------------- */
14724+
14725+static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
1facf9fc 14726+{
4a4d8108 14727+ int err;
4a4d8108
AM
14728+ struct au_pin pin;
14729+ struct au_finfo *finfo;
c2b27bf2 14730+ struct dentry *parent, *hi_wh;
4a4d8108 14731+ struct inode *inode;
1facf9fc 14732+ struct super_block *sb;
c2b27bf2 14733+ struct au_cp_generic cpg = {
2000de60 14734+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14735+ .bdst = -1,
14736+ .bsrc = -1,
14737+ .len = -1,
14738+ .pin = &pin,
14739+ .flags = AuCpup_DTIME
14740+ };
1facf9fc 14741+
4a4d8108
AM
14742+ FiMustWriteLock(file);
14743+
14744+ err = 0;
14745+ finfo = au_fi(file);
c2b27bf2 14746+ sb = cpg.dentry->d_sb;
5527c038 14747+ inode = d_inode(cpg.dentry);
5afbbe0d 14748+ cpg.bdst = au_ibtop(inode);
c2b27bf2 14749+ if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
1308ab2a 14750+ goto out;
dece6358 14751+
c2b27bf2
AM
14752+ parent = dget_parent(cpg.dentry);
14753+ if (au_test_ro(sb, cpg.bdst, inode)) {
4a4d8108 14754+ di_read_lock_parent(parent, !AuLock_IR);
c2b27bf2
AM
14755+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14756+ cpg.bdst = err;
4a4d8108
AM
14757+ di_read_unlock(parent, !AuLock_IR);
14758+ if (unlikely(err < 0))
14759+ goto out_parent;
14760+ err = 0;
1facf9fc 14761+ }
1facf9fc 14762+
4a4d8108 14763+ di_read_lock_parent(parent, AuLock_IR);
c2b27bf2 14764+ hi_wh = au_hi_wh(inode, cpg.bdst);
7f207e10
AM
14765+ if (!S_ISDIR(inode->i_mode)
14766+ && au_opt_test(au_mntflags(sb), PLINK)
4a4d8108 14767+ && au_plink_test(inode)
c2b27bf2 14768+ && !d_unhashed(cpg.dentry)
5afbbe0d 14769+ && cpg.bdst < au_dbtop(cpg.dentry)) {
c2b27bf2 14770+ err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
4a4d8108
AM
14771+ if (unlikely(err))
14772+ goto out_unlock;
14773+
14774+ /* always superio. */
c2b27bf2 14775+ err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108 14776+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 14777+ if (!err) {
c2b27bf2 14778+ err = au_sio_cpup_simple(&cpg);
367653fa
AM
14779+ au_unpin(&pin);
14780+ }
4a4d8108
AM
14781+ } else if (hi_wh) {
14782+ /* already copied-up after unlink */
c2b27bf2 14783+ err = au_reopen_wh(file, cpg.bdst, hi_wh);
4a4d8108
AM
14784+ *need_reopen = 0;
14785+ }
1facf9fc 14786+
4f0767ce 14787+out_unlock:
4a4d8108 14788+ di_read_unlock(parent, AuLock_IR);
4f0767ce 14789+out_parent:
4a4d8108 14790+ dput(parent);
4f0767ce 14791+out:
1308ab2a 14792+ return err;
dece6358 14793+}
1facf9fc 14794+
4a4d8108 14795+static void au_do_refresh_dir(struct file *file)
dece6358 14796+{
5afbbe0d 14797+ aufs_bindex_t bindex, bbot, new_bindex, brid;
4a4d8108
AM
14798+ struct au_hfile *p, tmp, *q;
14799+ struct au_finfo *finfo;
1308ab2a 14800+ struct super_block *sb;
4a4d8108 14801+ struct au_fidir *fidir;
1facf9fc 14802+
4a4d8108 14803+ FiMustWriteLock(file);
1facf9fc 14804+
2000de60 14805+ sb = file->f_path.dentry->d_sb;
4a4d8108
AM
14806+ finfo = au_fi(file);
14807+ fidir = finfo->fi_hdir;
14808+ AuDebugOn(!fidir);
14809+ p = fidir->fd_hfile + finfo->fi_btop;
14810+ brid = p->hf_br->br_id;
5afbbe0d
AM
14811+ bbot = fidir->fd_bbot;
14812+ for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
4a4d8108
AM
14813+ if (!p->hf_file)
14814+ continue;
1308ab2a 14815+
4a4d8108
AM
14816+ new_bindex = au_br_index(sb, p->hf_br->br_id);
14817+ if (new_bindex == bindex)
14818+ continue;
14819+ if (new_bindex < 0) {
14820+ au_set_h_fptr(file, bindex, NULL);
14821+ continue;
14822+ }
1308ab2a 14823+
4a4d8108
AM
14824+ /* swap two lower inode, and loop again */
14825+ q = fidir->fd_hfile + new_bindex;
14826+ tmp = *q;
14827+ *q = *p;
14828+ *p = tmp;
14829+ if (tmp.hf_file) {
14830+ bindex--;
14831+ p--;
14832+ }
14833+ }
1308ab2a 14834+
4a4d8108 14835+ p = fidir->fd_hfile;
2000de60 14836+ if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
5afbbe0d
AM
14837+ bbot = au_sbbot(sb);
14838+ for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
4a4d8108
AM
14839+ finfo->fi_btop++, p++)
14840+ if (p->hf_file) {
c06a8ce3 14841+ if (file_inode(p->hf_file))
4a4d8108 14842+ break;
1c60b727 14843+ au_hfput(p, /*execed*/0);
4a4d8108
AM
14844+ }
14845+ } else {
5afbbe0d
AM
14846+ bbot = au_br_index(sb, brid);
14847+ for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
4a4d8108
AM
14848+ finfo->fi_btop++, p++)
14849+ if (p->hf_file)
1c60b727 14850+ au_hfput(p, /*execed*/0);
5afbbe0d 14851+ bbot = au_sbbot(sb);
4a4d8108 14852+ }
1308ab2a 14853+
5afbbe0d
AM
14854+ p = fidir->fd_hfile + bbot;
14855+ for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
4a4d8108
AM
14856+ fidir->fd_bbot--, p--)
14857+ if (p->hf_file) {
c06a8ce3 14858+ if (file_inode(p->hf_file))
4a4d8108 14859+ break;
1c60b727 14860+ au_hfput(p, /*execed*/0);
4a4d8108
AM
14861+ }
14862+ AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
1308ab2a 14863+}
14864+
4a4d8108
AM
14865+/*
14866+ * after branch manipulating, refresh the file.
14867+ */
14868+static int refresh_file(struct file *file, int (*reopen)(struct file *file))
1facf9fc 14869+{
e2f27e51 14870+ int err, need_reopen, nbr;
5afbbe0d 14871+ aufs_bindex_t bbot, bindex;
4a4d8108 14872+ struct dentry *dentry;
e2f27e51 14873+ struct super_block *sb;
1308ab2a 14874+ struct au_finfo *finfo;
4a4d8108 14875+ struct au_hfile *hfile;
1facf9fc 14876+
2000de60 14877+ dentry = file->f_path.dentry;
e2f27e51
AM
14878+ sb = dentry->d_sb;
14879+ nbr = au_sbbot(sb) + 1;
1308ab2a 14880+ finfo = au_fi(file);
4a4d8108
AM
14881+ if (!finfo->fi_hdir) {
14882+ hfile = &finfo->fi_htop;
14883+ AuDebugOn(!hfile->hf_file);
e2f27e51 14884+ bindex = au_br_index(sb, hfile->hf_br->br_id);
4a4d8108
AM
14885+ AuDebugOn(bindex < 0);
14886+ if (bindex != finfo->fi_btop)
5afbbe0d 14887+ au_set_fbtop(file, bindex);
4a4d8108 14888+ } else {
e2f27e51 14889+ err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
4a4d8108
AM
14890+ if (unlikely(err))
14891+ goto out;
14892+ au_do_refresh_dir(file);
14893+ }
1facf9fc 14894+
4a4d8108
AM
14895+ err = 0;
14896+ need_reopen = 1;
14897+ if (!au_test_mmapped(file))
14898+ err = au_file_refresh_by_inode(file, &need_reopen);
e2f27e51
AM
14899+ if (finfo->fi_hdir)
14900+ /* harmless if err */
14901+ au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
027c5e7a 14902+ if (!err && need_reopen && !d_unlinked(dentry))
4a4d8108
AM
14903+ err = reopen(file);
14904+ if (!err) {
14905+ au_update_figen(file);
14906+ goto out; /* success */
14907+ }
14908+
14909+ /* error, close all lower files */
14910+ if (finfo->fi_hdir) {
5afbbe0d
AM
14911+ bbot = au_fbbot_dir(file);
14912+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
4a4d8108
AM
14913+ au_set_h_fptr(file, bindex, NULL);
14914+ }
1facf9fc 14915+
4f0767ce 14916+out:
1facf9fc 14917+ return err;
14918+}
14919+
4a4d8108
AM
14920+/* common function to regular file and dir */
14921+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
521ced18 14922+ int wlock, unsigned int fi_lsc)
dece6358 14923+{
1308ab2a 14924+ int err;
4a4d8108 14925+ unsigned int sigen, figen;
5afbbe0d 14926+ aufs_bindex_t btop;
4a4d8108
AM
14927+ unsigned char pseudo_link;
14928+ struct dentry *dentry;
14929+ struct inode *inode;
1facf9fc 14930+
4a4d8108 14931+ err = 0;
2000de60 14932+ dentry = file->f_path.dentry;
5527c038 14933+ inode = d_inode(dentry);
4a4d8108 14934+ sigen = au_sigen(dentry->d_sb);
521ced18 14935+ fi_write_lock_nested(file, fi_lsc);
4a4d8108 14936+ figen = au_figen(file);
521ced18
JR
14937+ if (!fi_lsc)
14938+ di_write_lock_child(dentry);
14939+ else
14940+ di_write_lock_child2(dentry);
5afbbe0d
AM
14941+ btop = au_dbtop(dentry);
14942+ pseudo_link = (btop != au_ibtop(inode));
14943+ if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
4a4d8108
AM
14944+ if (!wlock) {
14945+ di_downgrade_lock(dentry, AuLock_IR);
14946+ fi_downgrade_lock(file);
14947+ }
14948+ goto out; /* success */
14949+ }
dece6358 14950+
4a4d8108 14951+ AuDbg("sigen %d, figen %d\n", sigen, figen);
027c5e7a 14952+ if (au_digen_test(dentry, sigen)) {
4a4d8108 14953+ err = au_reval_dpath(dentry, sigen);
027c5e7a 14954+ AuDebugOn(!err && au_digen_test(dentry, sigen));
4a4d8108 14955+ }
dece6358 14956+
027c5e7a
AM
14957+ if (!err)
14958+ err = refresh_file(file, reopen);
4a4d8108
AM
14959+ if (!err) {
14960+ if (!wlock) {
14961+ di_downgrade_lock(dentry, AuLock_IR);
14962+ fi_downgrade_lock(file);
14963+ }
14964+ } else {
14965+ di_write_unlock(dentry);
14966+ fi_write_unlock(file);
14967+ }
1facf9fc 14968+
4f0767ce 14969+out:
1308ab2a 14970+ return err;
14971+}
1facf9fc 14972+
4a4d8108
AM
14973+/* ---------------------------------------------------------------------- */
14974+
14975+/* cf. aufs_nopage() */
14976+/* for madvise(2) */
14977+static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
1308ab2a 14978+{
4a4d8108
AM
14979+ unlock_page(page);
14980+ return 0;
14981+}
1facf9fc 14982+
4a4d8108 14983+/* it will never be called, but necessary to support O_DIRECT */
5afbbe0d 14984+static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
4a4d8108 14985+{ BUG(); return 0; }
1facf9fc 14986+
4a4d8108
AM
14987+/* they will never be called. */
14988+#ifdef CONFIG_AUFS_DEBUG
14989+static int aufs_write_begin(struct file *file, struct address_space *mapping,
14990+ loff_t pos, unsigned len, unsigned flags,
14991+ struct page **pagep, void **fsdata)
14992+{ AuUnsupport(); return 0; }
14993+static int aufs_write_end(struct file *file, struct address_space *mapping,
14994+ loff_t pos, unsigned len, unsigned copied,
14995+ struct page *page, void *fsdata)
14996+{ AuUnsupport(); return 0; }
14997+static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14998+{ AuUnsupport(); return 0; }
1308ab2a 14999+
4a4d8108
AM
15000+static int aufs_set_page_dirty(struct page *page)
15001+{ AuUnsupport(); return 0; }
392086de
AM
15002+static void aufs_invalidatepage(struct page *page, unsigned int offset,
15003+ unsigned int length)
4a4d8108
AM
15004+{ AuUnsupport(); }
15005+static int aufs_releasepage(struct page *page, gfp_t gfp)
15006+{ AuUnsupport(); return 0; }
79b8bda9 15007+#if 0 /* called by memory compaction regardless file */
4a4d8108 15008+static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
7eafdf33 15009+ struct page *page, enum migrate_mode mode)
4a4d8108 15010+{ AuUnsupport(); return 0; }
79b8bda9 15011+#endif
e2f27e51
AM
15012+static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
15013+{ AuUnsupport(); return true; }
15014+static void aufs_putback_page(struct page *page)
15015+{ AuUnsupport(); }
4a4d8108
AM
15016+static int aufs_launder_page(struct page *page)
15017+{ AuUnsupport(); return 0; }
15018+static int aufs_is_partially_uptodate(struct page *page,
38d290e6
JR
15019+ unsigned long from,
15020+ unsigned long count)
4a4d8108 15021+{ AuUnsupport(); return 0; }
392086de
AM
15022+static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
15023+ bool *writeback)
15024+{ AuUnsupport(); }
4a4d8108
AM
15025+static int aufs_error_remove_page(struct address_space *mapping,
15026+ struct page *page)
15027+{ AuUnsupport(); return 0; }
b4510431
AM
15028+static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
15029+ sector_t *span)
15030+{ AuUnsupport(); return 0; }
15031+static void aufs_swap_deactivate(struct file *file)
15032+{ AuUnsupport(); }
4a4d8108
AM
15033+#endif /* CONFIG_AUFS_DEBUG */
15034+
15035+const struct address_space_operations aufs_aop = {
15036+ .readpage = aufs_readpage,
15037+ .direct_IO = aufs_direct_IO,
4a4d8108
AM
15038+#ifdef CONFIG_AUFS_DEBUG
15039+ .writepage = aufs_writepage,
4a4d8108
AM
15040+ /* no writepages, because of writepage */
15041+ .set_page_dirty = aufs_set_page_dirty,
15042+ /* no readpages, because of readpage */
15043+ .write_begin = aufs_write_begin,
15044+ .write_end = aufs_write_end,
15045+ /* no bmap, no block device */
15046+ .invalidatepage = aufs_invalidatepage,
15047+ .releasepage = aufs_releasepage,
79b8bda9
AM
15048+ /* is fallback_migrate_page ok? */
15049+ /* .migratepage = aufs_migratepage, */
e2f27e51
AM
15050+ .isolate_page = aufs_isolate_page,
15051+ .putback_page = aufs_putback_page,
4a4d8108
AM
15052+ .launder_page = aufs_launder_page,
15053+ .is_partially_uptodate = aufs_is_partially_uptodate,
392086de 15054+ .is_dirty_writeback = aufs_is_dirty_writeback,
b4510431
AM
15055+ .error_remove_page = aufs_error_remove_page,
15056+ .swap_activate = aufs_swap_activate,
15057+ .swap_deactivate = aufs_swap_deactivate
4a4d8108 15058+#endif /* CONFIG_AUFS_DEBUG */
dece6358 15059+};
7f207e10
AM
15060diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
15061--- /usr/share/empty/fs/aufs/file.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 15062+++ linux/fs/aufs/file.h 2018-08-12 23:43:05.456791299 +0200
062440b3
AM
15063@@ -0,0 +1,341 @@
15064+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 15065+/*
b00004a5 15066+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
15067+ *
15068+ * This program, aufs is free software; you can redistribute it and/or modify
15069+ * it under the terms of the GNU General Public License as published by
15070+ * the Free Software Foundation; either version 2 of the License, or
15071+ * (at your option) any later version.
15072+ *
15073+ * This program is distributed in the hope that it will be useful,
15074+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15075+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15076+ * GNU General Public License for more details.
15077+ *
15078+ * You should have received a copy of the GNU General Public License
523b37e3 15079+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 15080+ */
1facf9fc 15081+
4a4d8108
AM
15082+/*
15083+ * file operations
15084+ */
1facf9fc 15085+
4a4d8108
AM
15086+#ifndef __AUFS_FILE_H__
15087+#define __AUFS_FILE_H__
1facf9fc 15088+
4a4d8108 15089+#ifdef __KERNEL__
1facf9fc 15090+
2cbb1c4b 15091+#include <linux/file.h>
4a4d8108 15092+#include <linux/fs.h>
3c1bdaff 15093+#include <linux/mm_types.h>
4a4d8108 15094+#include <linux/poll.h>
4a4d8108 15095+#include "rwsem.h"
1facf9fc 15096+
4a4d8108
AM
15097+struct au_branch;
15098+struct au_hfile {
15099+ struct file *hf_file;
15100+ struct au_branch *hf_br;
15101+};
1facf9fc 15102+
4a4d8108
AM
15103+struct au_vdir;
15104+struct au_fidir {
15105+ aufs_bindex_t fd_bbot;
15106+ aufs_bindex_t fd_nent;
15107+ struct au_vdir *fd_vdir_cache;
15108+ struct au_hfile fd_hfile[];
15109+};
1facf9fc 15110+
4a4d8108 15111+static inline int au_fidir_sz(int nent)
dece6358 15112+{
4f0767ce
JR
15113+ AuDebugOn(nent < 0);
15114+ return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
4a4d8108 15115+}
1facf9fc 15116+
4a4d8108
AM
15117+struct au_finfo {
15118+ atomic_t fi_generation;
dece6358 15119+
4a4d8108
AM
15120+ struct au_rwsem fi_rwsem;
15121+ aufs_bindex_t fi_btop;
15122+
15123+ /* do not union them */
15124+ struct { /* for non-dir */
15125+ struct au_hfile fi_htop;
2cbb1c4b 15126+ atomic_t fi_mmapped;
4a4d8108
AM
15127+ };
15128+ struct au_fidir *fi_hdir; /* for dir only */
523b37e3 15129+
8b6a4947 15130+ struct hlist_bl_node fi_hlist;
1c60b727 15131+ struct file *fi_file; /* very ugly */
4a4d8108 15132+} ____cacheline_aligned_in_smp;
1facf9fc 15133+
4a4d8108 15134+/* ---------------------------------------------------------------------- */
1facf9fc 15135+
4a4d8108
AM
15136+/* file.c */
15137+extern const struct address_space_operations aufs_aop;
15138+unsigned int au_file_roflags(unsigned int flags);
15139+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 15140+ struct file *file, int force_wr);
b912730e 15141+struct au_do_open_args {
8b6a4947 15142+ int aopen;
b912730e
AM
15143+ int (*open)(struct file *file, int flags,
15144+ struct file *h_file);
15145+ struct au_fidir *fidir;
15146+ struct file *h_file;
15147+};
15148+int au_do_open(struct file *file, struct au_do_open_args *args);
4a4d8108
AM
15149+int au_reopen_nondir(struct file *file);
15150+struct au_pin;
15151+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15152+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
521ced18 15153+ int wlock, unsigned int fi_lsc);
4a4d8108
AM
15154+int au_do_flush(struct file *file, fl_owner_t id,
15155+ int (*flush)(struct file *file, fl_owner_t id));
1facf9fc 15156+
4a4d8108
AM
15157+/* poll.c */
15158+#ifdef CONFIG_AUFS_POLL
cd7a4cd9 15159+__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
4a4d8108 15160+#endif
1facf9fc 15161+
4a4d8108
AM
15162+#ifdef CONFIG_AUFS_BR_HFSPLUS
15163+/* hfsplus.c */
392086de
AM
15164+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15165+ int force_wr);
4a4d8108
AM
15166+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15167+ struct file *h_file);
15168+#else
c1595e42
JR
15169+AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15170+ aufs_bindex_t bindex, int force_wr)
4a4d8108
AM
15171+AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15172+ struct file *h_file);
15173+#endif
1facf9fc 15174+
4a4d8108
AM
15175+/* f_op.c */
15176+extern const struct file_operations aufs_file_fop;
b912730e 15177+int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
4a4d8108 15178+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
521ced18 15179+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
4a4d8108 15180+
4a4d8108 15181+/* finfo.c */
f0c0a007 15182+void au_hfput(struct au_hfile *hf, int execed);
4a4d8108
AM
15183+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15184+ struct file *h_file);
1facf9fc 15185+
4a4d8108 15186+void au_update_figen(struct file *file);
4a4d8108 15187+struct au_fidir *au_fidir_alloc(struct super_block *sb);
e2f27e51 15188+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
1facf9fc 15189+
4a4d8108 15190+void au_fi_init_once(void *_fi);
1c60b727 15191+void au_finfo_fin(struct file *file);
4a4d8108 15192+int au_finfo_init(struct file *file, struct au_fidir *fidir);
1facf9fc 15193+
4a4d8108
AM
15194+/* ioctl.c */
15195+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
15196+#ifdef CONFIG_COMPAT
15197+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15198+ unsigned long arg);
c2b27bf2
AM
15199+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15200+ unsigned long arg);
b752ccd1 15201+#endif
1facf9fc 15202+
4a4d8108 15203+/* ---------------------------------------------------------------------- */
1facf9fc 15204+
4a4d8108
AM
15205+static inline struct au_finfo *au_fi(struct file *file)
15206+{
38d290e6 15207+ return file->private_data;
4a4d8108 15208+}
1facf9fc 15209+
4a4d8108 15210+/* ---------------------------------------------------------------------- */
1facf9fc 15211+
8b6a4947
AM
15212+#define fi_read_lock(f) au_rw_read_lock(&au_fi(f)->fi_rwsem)
15213+#define fi_write_lock(f) au_rw_write_lock(&au_fi(f)->fi_rwsem)
15214+#define fi_read_trylock(f) au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15215+#define fi_write_trylock(f) au_rw_write_trylock(&au_fi(f)->fi_rwsem)
4a4d8108 15216+/*
8b6a4947
AM
15217+#define fi_read_trylock_nested(f) \
15218+ au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15219+#define fi_write_trylock_nested(f) \
15220+ au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15221+*/
15222+
15223+#define fi_read_unlock(f) au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15224+#define fi_write_unlock(f) au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15225+#define fi_downgrade_lock(f) au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
1308ab2a 15226+
521ced18
JR
15227+/* lock subclass for finfo */
15228+enum {
15229+ AuLsc_FI_1,
15230+ AuLsc_FI_2
15231+};
15232+
15233+static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15234+{
15235+ au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15236+}
15237+
15238+static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15239+{
15240+ au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15241+}
15242+
15243+/*
15244+ * fi_read_lock_1, fi_write_lock_1,
15245+ * fi_read_lock_2, fi_write_lock_2
15246+ */
15247+#define AuReadLockFunc(name) \
15248+static inline void fi_read_lock_##name(struct file *f) \
15249+{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15250+
15251+#define AuWriteLockFunc(name) \
15252+static inline void fi_write_lock_##name(struct file *f) \
15253+{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15254+
15255+#define AuRWLockFuncs(name) \
15256+ AuReadLockFunc(name) \
15257+ AuWriteLockFunc(name)
15258+
15259+AuRWLockFuncs(1);
15260+AuRWLockFuncs(2);
15261+
15262+#undef AuReadLockFunc
15263+#undef AuWriteLockFunc
15264+#undef AuRWLockFuncs
15265+
4a4d8108
AM
15266+#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15267+#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15268+#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
1facf9fc 15269+
1308ab2a 15270+/* ---------------------------------------------------------------------- */
15271+
4a4d8108 15272+/* todo: hard/soft set? */
5afbbe0d 15273+static inline aufs_bindex_t au_fbtop(struct file *file)
dece6358 15274+{
4a4d8108
AM
15275+ FiMustAnyLock(file);
15276+ return au_fi(file)->fi_btop;
15277+}
dece6358 15278+
5afbbe0d 15279+static inline aufs_bindex_t au_fbbot_dir(struct file *file)
4a4d8108
AM
15280+{
15281+ FiMustAnyLock(file);
15282+ AuDebugOn(!au_fi(file)->fi_hdir);
15283+ return au_fi(file)->fi_hdir->fd_bbot;
15284+}
1facf9fc 15285+
4a4d8108
AM
15286+static inline struct au_vdir *au_fvdir_cache(struct file *file)
15287+{
15288+ FiMustAnyLock(file);
15289+ AuDebugOn(!au_fi(file)->fi_hdir);
15290+ return au_fi(file)->fi_hdir->fd_vdir_cache;
15291+}
1facf9fc 15292+
5afbbe0d 15293+static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
15294+{
15295+ FiMustWriteLock(file);
15296+ au_fi(file)->fi_btop = bindex;
15297+}
1facf9fc 15298+
5afbbe0d 15299+static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
15300+{
15301+ FiMustWriteLock(file);
15302+ AuDebugOn(!au_fi(file)->fi_hdir);
15303+ au_fi(file)->fi_hdir->fd_bbot = bindex;
15304+}
1308ab2a 15305+
4a4d8108
AM
15306+static inline void au_set_fvdir_cache(struct file *file,
15307+ struct au_vdir *vdir_cache)
15308+{
15309+ FiMustWriteLock(file);
15310+ AuDebugOn(!au_fi(file)->fi_hdir);
15311+ au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15312+}
dece6358 15313+
4a4d8108
AM
15314+static inline struct file *au_hf_top(struct file *file)
15315+{
15316+ FiMustAnyLock(file);
15317+ AuDebugOn(au_fi(file)->fi_hdir);
15318+ return au_fi(file)->fi_htop.hf_file;
15319+}
1facf9fc 15320+
4a4d8108
AM
15321+static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15322+{
15323+ FiMustAnyLock(file);
15324+ AuDebugOn(!au_fi(file)->fi_hdir);
15325+ return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
dece6358
AM
15326+}
15327+
4a4d8108
AM
15328+/* todo: memory barrier? */
15329+static inline unsigned int au_figen(struct file *f)
dece6358 15330+{
4a4d8108
AM
15331+ return atomic_read(&au_fi(f)->fi_generation);
15332+}
dece6358 15333+
2cbb1c4b
JR
15334+static inline void au_set_mmapped(struct file *f)
15335+{
15336+ if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15337+ return;
0c3ec466 15338+ pr_warn("fi_mmapped wrapped around\n");
2cbb1c4b
JR
15339+ while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15340+ ;
15341+}
15342+
15343+static inline void au_unset_mmapped(struct file *f)
15344+{
15345+ atomic_dec(&au_fi(f)->fi_mmapped);
15346+}
15347+
4a4d8108
AM
15348+static inline int au_test_mmapped(struct file *f)
15349+{
2cbb1c4b
JR
15350+ return atomic_read(&au_fi(f)->fi_mmapped);
15351+}
15352+
15353+/* customize vma->vm_file */
15354+
15355+static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15356+ struct file *file)
15357+{
53392da6
AM
15358+ struct file *f;
15359+
15360+ f = vma->vm_file;
2cbb1c4b
JR
15361+ get_file(file);
15362+ vma->vm_file = file;
53392da6 15363+ fput(f);
2cbb1c4b
JR
15364+}
15365+
15366+#ifdef CONFIG_MMU
15367+#define AuDbgVmRegion(file, vma) do {} while (0)
15368+
15369+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15370+ struct file *file)
15371+{
15372+ au_do_vm_file_reset(vma, file);
15373+}
15374+#else
15375+#define AuDbgVmRegion(file, vma) \
15376+ AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15377+
15378+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15379+ struct file *file)
15380+{
53392da6
AM
15381+ struct file *f;
15382+
2cbb1c4b 15383+ au_do_vm_file_reset(vma, file);
53392da6 15384+ f = vma->vm_region->vm_file;
2cbb1c4b
JR
15385+ get_file(file);
15386+ vma->vm_region->vm_file = file;
53392da6 15387+ fput(f);
2cbb1c4b
JR
15388+}
15389+#endif /* CONFIG_MMU */
15390+
15391+/* handle vma->vm_prfile */
fb47a38f 15392+static inline void au_vm_prfile_set(struct vm_area_struct *vma,
2cbb1c4b
JR
15393+ struct file *file)
15394+{
2cbb1c4b
JR
15395+ get_file(file);
15396+ vma->vm_prfile = file;
15397+#ifndef CONFIG_MMU
15398+ get_file(file);
15399+ vma->vm_region->vm_prfile = file;
15400+#endif
fb47a38f 15401+}
1308ab2a 15402+
4a4d8108
AM
15403+#endif /* __KERNEL__ */
15404+#endif /* __AUFS_FILE_H__ */
7f207e10
AM
15405diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15406--- /usr/share/empty/fs/aufs/finfo.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 15407+++ linux/fs/aufs/finfo.c 2018-08-12 23:43:05.456791299 +0200
062440b3 15408@@ -0,0 +1,149 @@
cd7a4cd9 15409+// SPDX-License-Identifier: GPL-2.0
4a4d8108 15410+/*
b00004a5 15411+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
15412+ *
15413+ * This program, aufs is free software; you can redistribute it and/or modify
15414+ * it under the terms of the GNU General Public License as published by
15415+ * the Free Software Foundation; either version 2 of the License, or
15416+ * (at your option) any later version.
15417+ *
15418+ * This program is distributed in the hope that it will be useful,
15419+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15420+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15421+ * GNU General Public License for more details.
15422+ *
15423+ * You should have received a copy of the GNU General Public License
523b37e3 15424+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 15425+ */
1308ab2a 15426+
4a4d8108
AM
15427+/*
15428+ * file private data
15429+ */
1facf9fc 15430+
4a4d8108 15431+#include "aufs.h"
1facf9fc 15432+
f0c0a007 15433+void au_hfput(struct au_hfile *hf, int execed)
4a4d8108 15434+{
f0c0a007 15435+ if (execed)
4a4d8108
AM
15436+ allow_write_access(hf->hf_file);
15437+ fput(hf->hf_file);
15438+ hf->hf_file = NULL;
5afbbe0d 15439+ au_br_put(hf->hf_br);
4a4d8108
AM
15440+ hf->hf_br = NULL;
15441+}
1facf9fc 15442+
4a4d8108
AM
15443+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15444+{
15445+ struct au_finfo *finfo = au_fi(file);
15446+ struct au_hfile *hf;
15447+ struct au_fidir *fidir;
15448+
15449+ fidir = finfo->fi_hdir;
15450+ if (!fidir) {
15451+ AuDebugOn(finfo->fi_btop != bindex);
15452+ hf = &finfo->fi_htop;
15453+ } else
15454+ hf = fidir->fd_hfile + bindex;
15455+
15456+ if (hf && hf->hf_file)
f0c0a007 15457+ au_hfput(hf, vfsub_file_execed(file));
4a4d8108
AM
15458+ if (val) {
15459+ FiMustWriteLock(file);
b912730e 15460+ AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
4a4d8108 15461+ hf->hf_file = val;
2000de60 15462+ hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
1308ab2a 15463+ }
4a4d8108 15464+}
1facf9fc 15465+
4a4d8108
AM
15466+void au_update_figen(struct file *file)
15467+{
2000de60 15468+ atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
4a4d8108 15469+ /* smp_mb(); */ /* atomic_set */
1facf9fc 15470+}
15471+
4a4d8108
AM
15472+/* ---------------------------------------------------------------------- */
15473+
4a4d8108
AM
15474+struct au_fidir *au_fidir_alloc(struct super_block *sb)
15475+{
15476+ struct au_fidir *fidir;
15477+ int nbr;
15478+
5afbbe0d 15479+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
15480+ if (nbr < 2)
15481+ nbr = 2; /* initial allocate for 2 branches */
15482+ fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15483+ if (fidir) {
15484+ fidir->fd_bbot = -1;
15485+ fidir->fd_nent = nbr;
4a4d8108
AM
15486+ }
15487+
15488+ return fidir;
15489+}
15490+
e2f27e51 15491+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
4a4d8108
AM
15492+{
15493+ int err;
15494+ struct au_fidir *fidir, *p;
15495+
15496+ AuRwMustWriteLock(&finfo->fi_rwsem);
15497+ fidir = finfo->fi_hdir;
15498+ AuDebugOn(!fidir);
15499+
15500+ err = -ENOMEM;
15501+ p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
e2f27e51 15502+ GFP_NOFS, may_shrink);
4a4d8108
AM
15503+ if (p) {
15504+ p->fd_nent = nbr;
15505+ finfo->fi_hdir = p;
15506+ err = 0;
15507+ }
1facf9fc 15508+
dece6358 15509+ return err;
1facf9fc 15510+}
1308ab2a 15511+
15512+/* ---------------------------------------------------------------------- */
15513+
1c60b727 15514+void au_finfo_fin(struct file *file)
1308ab2a 15515+{
4a4d8108
AM
15516+ struct au_finfo *finfo;
15517+
2000de60 15518+ au_nfiles_dec(file->f_path.dentry->d_sb);
7f207e10 15519+
4a4d8108
AM
15520+ finfo = au_fi(file);
15521+ AuDebugOn(finfo->fi_hdir);
15522+ AuRwDestroy(&finfo->fi_rwsem);
1c60b727 15523+ au_cache_free_finfo(finfo);
1308ab2a 15524+}
1308ab2a 15525+
e49829fe 15526+void au_fi_init_once(void *_finfo)
4a4d8108 15527+{
e49829fe 15528+ struct au_finfo *finfo = _finfo;
1308ab2a 15529+
e49829fe 15530+ au_rw_init(&finfo->fi_rwsem);
4a4d8108 15531+}
1308ab2a 15532+
4a4d8108
AM
15533+int au_finfo_init(struct file *file, struct au_fidir *fidir)
15534+{
1716fcea 15535+ int err;
4a4d8108
AM
15536+ struct au_finfo *finfo;
15537+ struct dentry *dentry;
15538+
15539+ err = -ENOMEM;
2000de60 15540+ dentry = file->f_path.dentry;
4a4d8108
AM
15541+ finfo = au_cache_alloc_finfo();
15542+ if (unlikely(!finfo))
15543+ goto out;
15544+
15545+ err = 0;
7f207e10 15546+ au_nfiles_inc(dentry->d_sb);
4a4d8108
AM
15547+ au_rw_write_lock(&finfo->fi_rwsem);
15548+ finfo->fi_btop = -1;
15549+ finfo->fi_hdir = fidir;
15550+ atomic_set(&finfo->fi_generation, au_digen(dentry));
15551+ /* smp_mb(); */ /* atomic_set */
15552+
15553+ file->private_data = finfo;
15554+
15555+out:
15556+ return err;
15557+}
7f207e10
AM
15558diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15559--- /usr/share/empty/fs/aufs/f_op.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 15560+++ linux/fs/aufs/f_op.c 2018-08-12 23:43:05.456791299 +0200
062440b3 15561@@ -0,0 +1,818 @@
cd7a4cd9 15562+// SPDX-License-Identifier: GPL-2.0
dece6358 15563+/*
b00004a5 15564+ * Copyright (C) 2005-2018 Junjiro R. Okajima
dece6358
AM
15565+ *
15566+ * This program, aufs is free software; you can redistribute it and/or modify
15567+ * it under the terms of the GNU General Public License as published by
15568+ * the Free Software Foundation; either version 2 of the License, or
15569+ * (at your option) any later version.
15570+ *
15571+ * This program is distributed in the hope that it will be useful,
15572+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15573+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15574+ * GNU General Public License for more details.
15575+ *
15576+ * You should have received a copy of the GNU General Public License
523b37e3 15577+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 15578+ */
1facf9fc 15579+
15580+/*
4a4d8108 15581+ * file and vm operations
1facf9fc 15582+ */
dece6358 15583+
86dc4139 15584+#include <linux/aio.h>
4a4d8108
AM
15585+#include <linux/fs_stack.h>
15586+#include <linux/mman.h>
4a4d8108 15587+#include <linux/security.h>
dece6358
AM
15588+#include "aufs.h"
15589+
b912730e 15590+int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
1facf9fc 15591+{
4a4d8108
AM
15592+ int err;
15593+ aufs_bindex_t bindex;
8cdd5066 15594+ struct dentry *dentry, *h_dentry;
4a4d8108 15595+ struct au_finfo *finfo;
38d290e6 15596+ struct inode *h_inode;
4a4d8108
AM
15597+
15598+ FiMustWriteLock(file);
15599+
523b37e3 15600+ err = 0;
2000de60 15601+ dentry = file->f_path.dentry;
b912730e 15602+ AuDebugOn(IS_ERR_OR_NULL(dentry));
4a4d8108
AM
15603+ finfo = au_fi(file);
15604+ memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
2cbb1c4b 15605+ atomic_set(&finfo->fi_mmapped, 0);
5afbbe0d 15606+ bindex = au_dbtop(dentry);
8cdd5066
JR
15607+ if (!h_file) {
15608+ h_dentry = au_h_dptr(dentry, bindex);
15609+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15610+ if (unlikely(err))
15611+ goto out;
b912730e 15612+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
8cdd5066
JR
15613+ } else {
15614+ h_dentry = h_file->f_path.dentry;
15615+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15616+ if (unlikely(err))
15617+ goto out;
b912730e 15618+ get_file(h_file);
8cdd5066 15619+ }
4a4d8108
AM
15620+ if (IS_ERR(h_file))
15621+ err = PTR_ERR(h_file);
15622+ else {
38d290e6
JR
15623+ if ((flags & __O_TMPFILE)
15624+ && !(flags & O_EXCL)) {
15625+ h_inode = file_inode(h_file);
15626+ spin_lock(&h_inode->i_lock);
15627+ h_inode->i_state |= I_LINKABLE;
15628+ spin_unlock(&h_inode->i_lock);
15629+ }
5afbbe0d 15630+ au_set_fbtop(file, bindex);
4a4d8108
AM
15631+ au_set_h_fptr(file, bindex, h_file);
15632+ au_update_figen(file);
15633+ /* todo: necessary? */
15634+ /* file->f_ra = h_file->f_ra; */
15635+ }
027c5e7a 15636+
8cdd5066 15637+out:
4a4d8108 15638+ return err;
1facf9fc 15639+}
15640+
4a4d8108
AM
15641+static int aufs_open_nondir(struct inode *inode __maybe_unused,
15642+ struct file *file)
1facf9fc 15643+{
4a4d8108 15644+ int err;
1308ab2a 15645+ struct super_block *sb;
b912730e
AM
15646+ struct au_do_open_args args = {
15647+ .open = au_do_open_nondir
15648+ };
1facf9fc 15649+
523b37e3
AM
15650+ AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15651+ file, vfsub_file_flags(file), file->f_mode);
1facf9fc 15652+
2000de60 15653+ sb = file->f_path.dentry->d_sb;
4a4d8108 15654+ si_read_lock(sb, AuLock_FLUSH);
b912730e 15655+ err = au_do_open(file, &args);
4a4d8108
AM
15656+ si_read_unlock(sb);
15657+ return err;
15658+}
1facf9fc 15659+
4a4d8108
AM
15660+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15661+{
15662+ struct au_finfo *finfo;
15663+ aufs_bindex_t bindex;
1facf9fc 15664+
4a4d8108 15665+ finfo = au_fi(file);
8b6a4947
AM
15666+ au_hbl_del(&finfo->fi_hlist,
15667+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108 15668+ bindex = finfo->fi_btop;
b4510431 15669+ if (bindex >= 0)
4a4d8108 15670+ au_set_h_fptr(file, bindex, NULL);
7f207e10 15671+
1c60b727 15672+ au_finfo_fin(file);
4a4d8108 15673+ return 0;
1facf9fc 15674+}
15675+
4a4d8108
AM
15676+/* ---------------------------------------------------------------------- */
15677+
15678+static int au_do_flush_nondir(struct file *file, fl_owner_t id)
dece6358 15679+{
1308ab2a 15680+ int err;
4a4d8108
AM
15681+ struct file *h_file;
15682+
15683+ err = 0;
15684+ h_file = au_hf_top(file);
15685+ if (h_file)
15686+ err = vfsub_flush(h_file, id);
15687+ return err;
15688+}
15689+
15690+static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15691+{
15692+ return au_do_flush(file, id, au_do_flush_nondir);
15693+}
15694+
15695+/* ---------------------------------------------------------------------- */
9dbd164d
AM
15696+/*
15697+ * read and write functions acquire [fdi]_rwsem once, but release before
15698+ * mmap_sem. This is because to stop a race condition between mmap(2).
15699+ * Releasing these aufs-rwsem should be safe, no branch-mamagement (by keeping
15700+ * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15701+ * read functions after [fdi]_rwsem are released, but it should be harmless.
15702+ */
4a4d8108 15703+
b912730e 15704+/* Callers should call au_read_post() or fput() in the end */
521ced18 15705+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
4a4d8108 15706+{
4a4d8108 15707+ struct file *h_file;
b912730e 15708+ int err;
1facf9fc 15709+
521ced18 15710+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
b912730e
AM
15711+ if (!err) {
15712+ di_read_unlock(file->f_path.dentry, AuLock_IR);
15713+ h_file = au_hf_top(file);
15714+ get_file(h_file);
15715+ if (!keep_fi)
15716+ fi_read_unlock(file);
15717+ } else
15718+ h_file = ERR_PTR(err);
15719+
15720+ return h_file;
15721+}
15722+
15723+static void au_read_post(struct inode *inode, struct file *h_file)
15724+{
15725+ /* update without lock, I don't think it a problem */
15726+ fsstack_copy_attr_atime(inode, file_inode(h_file));
15727+ fput(h_file);
15728+}
15729+
15730+struct au_write_pre {
521ced18
JR
15731+ /* input */
15732+ unsigned int lsc;
15733+
15734+ /* output */
b912730e 15735+ blkcnt_t blks;
5afbbe0d 15736+ aufs_bindex_t btop;
b912730e
AM
15737+};
15738+
15739+/*
15740+ * return with iinfo is write-locked
15741+ * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15742+ * end
15743+ */
15744+static struct file *au_write_pre(struct file *file, int do_ready,
15745+ struct au_write_pre *wpre)
15746+{
15747+ struct file *h_file;
15748+ struct dentry *dentry;
15749+ int err;
521ced18 15750+ unsigned int lsc;
b912730e
AM
15751+ struct au_pin pin;
15752+
521ced18
JR
15753+ lsc = 0;
15754+ if (wpre)
15755+ lsc = wpre->lsc;
15756+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
b912730e 15757+ h_file = ERR_PTR(err);
dece6358
AM
15758+ if (unlikely(err))
15759+ goto out;
1facf9fc 15760+
b912730e
AM
15761+ dentry = file->f_path.dentry;
15762+ if (do_ready) {
15763+ err = au_ready_to_write(file, -1, &pin);
15764+ if (unlikely(err)) {
15765+ h_file = ERR_PTR(err);
15766+ di_write_unlock(dentry);
15767+ goto out_fi;
15768+ }
15769+ }
15770+
15771+ di_downgrade_lock(dentry, /*flags*/0);
15772+ if (wpre)
5afbbe0d 15773+ wpre->btop = au_fbtop(file);
4a4d8108 15774+ h_file = au_hf_top(file);
9dbd164d 15775+ get_file(h_file);
b912730e
AM
15776+ if (wpre)
15777+ wpre->blks = file_inode(h_file)->i_blocks;
15778+ if (do_ready)
15779+ au_unpin(&pin);
15780+ di_read_unlock(dentry, /*flags*/0);
15781+
15782+out_fi:
15783+ fi_write_unlock(file);
15784+out:
15785+ return h_file;
15786+}
15787+
15788+static void au_write_post(struct inode *inode, struct file *h_file,
15789+ struct au_write_pre *wpre, ssize_t written)
15790+{
15791+ struct inode *h_inode;
15792+
15793+ au_cpup_attr_timesizes(inode);
5afbbe0d 15794+ AuDebugOn(au_ibtop(inode) != wpre->btop);
b912730e
AM
15795+ h_inode = file_inode(h_file);
15796+ inode->i_mode = h_inode->i_mode;
15797+ ii_write_unlock(inode);
b912730e
AM
15798+ /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15799+ if (written > 0)
5afbbe0d 15800+ au_fhsm_wrote(inode->i_sb, wpre->btop,
b912730e 15801+ /*force*/h_inode->i_blocks > wpre->blks);
1c60b727 15802+ fput(h_file);
b912730e
AM
15803+}
15804+
15805+static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
15806+ loff_t *ppos)
15807+{
15808+ ssize_t err;
15809+ struct inode *inode;
15810+ struct file *h_file;
15811+ struct super_block *sb;
15812+
15813+ inode = file_inode(file);
15814+ sb = inode->i_sb;
15815+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15816+
521ced18 15817+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
15818+ err = PTR_ERR(h_file);
15819+ if (IS_ERR(h_file))
15820+ goto out;
9dbd164d
AM
15821+
15822+ /* filedata may be obsoleted by concurrent copyup, but no problem */
4a4d8108
AM
15823+ err = vfsub_read_u(h_file, buf, count, ppos);
15824+ /* todo: necessary? */
15825+ /* file->f_ra = h_file->f_ra; */
b912730e 15826+ au_read_post(inode, h_file);
1308ab2a 15827+
4f0767ce 15828+out:
dece6358
AM
15829+ si_read_unlock(sb);
15830+ return err;
15831+}
1facf9fc 15832+
e49829fe
JR
15833+/*
15834+ * todo: very ugly
15835+ * it locks both of i_mutex and si_rwsem for read in safe.
15836+ * if the plink maintenance mode continues forever (that is the problem),
15837+ * may loop forever.
15838+ */
15839+static void au_mtx_and_read_lock(struct inode *inode)
15840+{
15841+ int err;
15842+ struct super_block *sb = inode->i_sb;
15843+
15844+ while (1) {
febd17d6 15845+ inode_lock(inode);
e49829fe
JR
15846+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15847+ if (!err)
15848+ break;
febd17d6 15849+ inode_unlock(inode);
e49829fe
JR
15850+ si_read_lock(sb, AuLock_NOPLMW);
15851+ si_read_unlock(sb);
15852+ }
15853+}
15854+
4a4d8108
AM
15855+static ssize_t aufs_write(struct file *file, const char __user *ubuf,
15856+ size_t count, loff_t *ppos)
dece6358 15857+{
4a4d8108 15858+ ssize_t err;
b912730e
AM
15859+ struct au_write_pre wpre;
15860+ struct inode *inode;
4a4d8108
AM
15861+ struct file *h_file;
15862+ char __user *buf = (char __user *)ubuf;
1facf9fc 15863+
b912730e 15864+ inode = file_inode(file);
e49829fe 15865+ au_mtx_and_read_lock(inode);
1facf9fc 15866+
521ced18 15867+ wpre.lsc = 0;
b912730e
AM
15868+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15869+ err = PTR_ERR(h_file);
15870+ if (IS_ERR(h_file))
9dbd164d 15871+ goto out;
9dbd164d 15872+
4a4d8108 15873+ err = vfsub_write_u(h_file, buf, count, ppos);
b912730e 15874+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15875+
4f0767ce 15876+out:
b912730e 15877+ si_read_unlock(inode->i_sb);
febd17d6 15878+ inode_unlock(inode);
dece6358
AM
15879+ return err;
15880+}
1facf9fc 15881+
076b876e
AM
15882+static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15883+ struct iov_iter *iov_iter)
dece6358 15884+{
4a4d8108
AM
15885+ ssize_t err;
15886+ struct file *file;
076b876e 15887+ ssize_t (*iter)(struct kiocb *, struct iov_iter *);
1facf9fc 15888+
4a4d8108
AM
15889+ err = security_file_permission(h_file, rw);
15890+ if (unlikely(err))
15891+ goto out;
1facf9fc 15892+
4a4d8108 15893+ err = -ENOSYS;
076b876e 15894+ iter = NULL;
5527c038 15895+ if (rw == MAY_READ)
076b876e 15896+ iter = h_file->f_op->read_iter;
5527c038 15897+ else if (rw == MAY_WRITE)
076b876e 15898+ iter = h_file->f_op->write_iter;
076b876e
AM
15899+
15900+ file = kio->ki_filp;
15901+ kio->ki_filp = h_file;
15902+ if (iter) {
2cbb1c4b 15903+ lockdep_off();
076b876e
AM
15904+ err = iter(kio, iov_iter);
15905+ lockdep_on();
4a4d8108
AM
15906+ } else
15907+ /* currently there is no such fs */
15908+ WARN_ON_ONCE(1);
076b876e 15909+ kio->ki_filp = file;
1facf9fc 15910+
4f0767ce 15911+out:
dece6358
AM
15912+ return err;
15913+}
1facf9fc 15914+
076b876e 15915+static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1facf9fc 15916+{
4a4d8108
AM
15917+ ssize_t err;
15918+ struct file *file, *h_file;
b912730e 15919+ struct inode *inode;
dece6358 15920+ struct super_block *sb;
1facf9fc 15921+
4a4d8108 15922+ file = kio->ki_filp;
b912730e
AM
15923+ inode = file_inode(file);
15924+ sb = inode->i_sb;
e49829fe 15925+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 15926+
521ced18 15927+ h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
b912730e
AM
15928+ err = PTR_ERR(h_file);
15929+ if (IS_ERR(h_file))
15930+ goto out;
9dbd164d 15931+
5afbbe0d
AM
15932+ if (au_test_loopback_kthread()) {
15933+ au_warn_loopback(h_file->f_path.dentry->d_sb);
15934+ if (file->f_mapping != h_file->f_mapping) {
15935+ file->f_mapping = h_file->f_mapping;
15936+ smp_mb(); /* unnecessary? */
15937+ }
15938+ }
15939+ fi_read_unlock(file);
15940+
076b876e 15941+ err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
4a4d8108
AM
15942+ /* todo: necessary? */
15943+ /* file->f_ra = h_file->f_ra; */
b912730e 15944+ au_read_post(inode, h_file);
1facf9fc 15945+
4f0767ce 15946+out:
4a4d8108 15947+ si_read_unlock(sb);
1308ab2a 15948+ return err;
15949+}
1facf9fc 15950+
076b876e 15951+static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1308ab2a 15952+{
4a4d8108 15953+ ssize_t err;
b912730e
AM
15954+ struct au_write_pre wpre;
15955+ struct inode *inode;
4a4d8108 15956+ struct file *file, *h_file;
1308ab2a 15957+
4a4d8108 15958+ file = kio->ki_filp;
b912730e 15959+ inode = file_inode(file);
e49829fe
JR
15960+ au_mtx_and_read_lock(inode);
15961+
521ced18 15962+ wpre.lsc = 0;
b912730e
AM
15963+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15964+ err = PTR_ERR(h_file);
15965+ if (IS_ERR(h_file))
9dbd164d 15966+ goto out;
9dbd164d 15967+
076b876e 15968+ err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
b912730e 15969+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15970+
4f0767ce 15971+out:
b912730e 15972+ si_read_unlock(inode->i_sb);
febd17d6 15973+ inode_unlock(inode);
dece6358 15974+ return err;
1facf9fc 15975+}
15976+
4a4d8108
AM
15977+static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15978+ struct pipe_inode_info *pipe, size_t len,
15979+ unsigned int flags)
1facf9fc 15980+{
4a4d8108
AM
15981+ ssize_t err;
15982+ struct file *h_file;
b912730e 15983+ struct inode *inode;
dece6358 15984+ struct super_block *sb;
1facf9fc 15985+
b912730e
AM
15986+ inode = file_inode(file);
15987+ sb = inode->i_sb;
e49829fe 15988+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 15989+
521ced18 15990+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
15991+ err = PTR_ERR(h_file);
15992+ if (IS_ERR(h_file))
dece6358 15993+ goto out;
1facf9fc 15994+
4a4d8108
AM
15995+ err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
15996+ /* todo: necessasry? */
15997+ /* file->f_ra = h_file->f_ra; */
b912730e 15998+ au_read_post(inode, h_file);
1facf9fc 15999+
4f0767ce 16000+out:
4a4d8108 16001+ si_read_unlock(sb);
dece6358 16002+ return err;
1facf9fc 16003+}
16004+
4a4d8108
AM
16005+static ssize_t
16006+aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
16007+ size_t len, unsigned int flags)
1facf9fc 16008+{
4a4d8108 16009+ ssize_t err;
b912730e
AM
16010+ struct au_write_pre wpre;
16011+ struct inode *inode;
076b876e 16012+ struct file *h_file;
1facf9fc 16013+
b912730e 16014+ inode = file_inode(file);
e49829fe 16015+ au_mtx_and_read_lock(inode);
9dbd164d 16016+
521ced18 16017+ wpre.lsc = 0;
b912730e
AM
16018+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16019+ err = PTR_ERR(h_file);
16020+ if (IS_ERR(h_file))
9dbd164d 16021+ goto out;
9dbd164d 16022+
4a4d8108 16023+ err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
b912730e 16024+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 16025+
4f0767ce 16026+out:
b912730e 16027+ si_read_unlock(inode->i_sb);
febd17d6 16028+ inode_unlock(inode);
4a4d8108
AM
16029+ return err;
16030+}
1facf9fc 16031+
38d290e6
JR
16032+static long aufs_fallocate(struct file *file, int mode, loff_t offset,
16033+ loff_t len)
16034+{
16035+ long err;
b912730e 16036+ struct au_write_pre wpre;
38d290e6
JR
16037+ struct inode *inode;
16038+ struct file *h_file;
16039+
b912730e 16040+ inode = file_inode(file);
38d290e6
JR
16041+ au_mtx_and_read_lock(inode);
16042+
521ced18 16043+ wpre.lsc = 0;
b912730e
AM
16044+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16045+ err = PTR_ERR(h_file);
16046+ if (IS_ERR(h_file))
38d290e6 16047+ goto out;
38d290e6
JR
16048+
16049+ lockdep_off();
03673fb0 16050+ err = vfs_fallocate(h_file, mode, offset, len);
38d290e6 16051+ lockdep_on();
b912730e 16052+ au_write_post(inode, h_file, &wpre, /*written*/1);
38d290e6
JR
16053+
16054+out:
b912730e 16055+ si_read_unlock(inode->i_sb);
febd17d6 16056+ inode_unlock(inode);
38d290e6
JR
16057+ return err;
16058+}
16059+
521ced18
JR
16060+static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
16061+ struct file *dst, loff_t dst_pos,
16062+ size_t len, unsigned int flags)
16063+{
16064+ ssize_t err;
16065+ struct au_write_pre wpre;
16066+ enum { SRC, DST };
16067+ struct {
16068+ struct inode *inode;
16069+ struct file *h_file;
16070+ struct super_block *h_sb;
16071+ } a[2];
16072+#define a_src a[SRC]
16073+#define a_dst a[DST]
16074+
16075+ err = -EINVAL;
16076+ a_src.inode = file_inode(src);
16077+ if (unlikely(!S_ISREG(a_src.inode->i_mode)))
16078+ goto out;
16079+ a_dst.inode = file_inode(dst);
16080+ if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
16081+ goto out;
16082+
16083+ au_mtx_and_read_lock(a_dst.inode);
16084+ /*
16085+ * in order to match the order in di_write_lock2_{child,parent}(),
16086+ * use f_path.dentry for this comparision.
16087+ */
16088+ if (src->f_path.dentry < dst->f_path.dentry) {
16089+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
16090+ err = PTR_ERR(a_src.h_file);
16091+ if (IS_ERR(a_src.h_file))
16092+ goto out_si;
16093+
16094+ wpre.lsc = AuLsc_FI_2;
16095+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16096+ err = PTR_ERR(a_dst.h_file);
16097+ if (IS_ERR(a_dst.h_file)) {
16098+ au_read_post(a_src.inode, a_src.h_file);
16099+ goto out_si;
16100+ }
16101+ } else {
16102+ wpre.lsc = AuLsc_FI_1;
16103+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16104+ err = PTR_ERR(a_dst.h_file);
16105+ if (IS_ERR(a_dst.h_file))
16106+ goto out_si;
16107+
16108+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
16109+ err = PTR_ERR(a_src.h_file);
16110+ if (IS_ERR(a_src.h_file)) {
16111+ au_write_post(a_dst.inode, a_dst.h_file, &wpre,
16112+ /*written*/0);
16113+ goto out_si;
16114+ }
16115+ }
16116+
16117+ err = -EXDEV;
16118+ a_src.h_sb = file_inode(a_src.h_file)->i_sb;
16119+ a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
16120+ if (unlikely(a_src.h_sb != a_dst.h_sb)) {
16121+ AuDbgFile(src);
16122+ AuDbgFile(dst);
16123+ goto out_file;
16124+ }
16125+
16126+ err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
16127+ dst_pos, len, flags);
16128+
16129+out_file:
16130+ au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
16131+ fi_read_unlock(src);
16132+ au_read_post(a_src.inode, a_src.h_file);
16133+out_si:
16134+ si_read_unlock(a_dst.inode->i_sb);
16135+ inode_unlock(a_dst.inode);
16136+out:
16137+ return err;
16138+#undef a_src
16139+#undef a_dst
16140+}
16141+
4a4d8108
AM
16142+/* ---------------------------------------------------------------------- */
16143+
9dbd164d
AM
16144+/*
16145+ * The locking order around current->mmap_sem.
16146+ * - in most and regular cases
16147+ * file I/O syscall -- aufs_read() or something
16148+ * -- si_rwsem for read -- mmap_sem
16149+ * (Note that [fdi]i_rwsem are released before mmap_sem).
16150+ * - in mmap case
16151+ * mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
16152+ * This AB-BA order is definitly bad, but is not a problem since "si_rwsem for
16153+ * read" allows muliple processes to acquire it and [fdi]i_rwsem are not held in
16154+ * file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
16155+ * It means that when aufs acquires si_rwsem for write, the process should never
16156+ * acquire mmap_sem.
16157+ *
392086de 16158+ * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
9dbd164d
AM
16159+ * problem either since any directory is not able to be mmap-ed.
16160+ * The similar scenario is applied to aufs_readlink() too.
16161+ */
16162+
38d290e6 16163+#if 0 /* stop calling security_file_mmap() */
2dfbb274
AM
16164+/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
16165+#define AuConv_VM_PROT(f, b) _calc_vm_trans(f, VM_##b, PROT_##b)
16166+
16167+static unsigned long au_arch_prot_conv(unsigned long flags)
16168+{
16169+ /* currently ppc64 only */
16170+#ifdef CONFIG_PPC64
16171+ /* cf. linux/arch/powerpc/include/asm/mman.h */
16172+ AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
16173+ return AuConv_VM_PROT(flags, SAO);
16174+#else
16175+ AuDebugOn(arch_calc_vm_prot_bits(-1));
16176+ return 0;
16177+#endif
16178+}
16179+
16180+static unsigned long au_prot_conv(unsigned long flags)
16181+{
16182+ return AuConv_VM_PROT(flags, READ)
16183+ | AuConv_VM_PROT(flags, WRITE)
16184+ | AuConv_VM_PROT(flags, EXEC)
16185+ | au_arch_prot_conv(flags);
16186+}
16187+
16188+/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16189+#define AuConv_VM_MAP(f, b) _calc_vm_trans(f, VM_##b, MAP_##b)
16190+
16191+static unsigned long au_flag_conv(unsigned long flags)
16192+{
16193+ return AuConv_VM_MAP(flags, GROWSDOWN)
16194+ | AuConv_VM_MAP(flags, DENYWRITE)
2dfbb274
AM
16195+ | AuConv_VM_MAP(flags, LOCKED);
16196+}
38d290e6 16197+#endif
2dfbb274 16198+
9dbd164d 16199+static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
dece6358 16200+{
4a4d8108 16201+ int err;
4a4d8108 16202+ const unsigned char wlock
9dbd164d 16203+ = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
4a4d8108 16204+ struct super_block *sb;
9dbd164d 16205+ struct file *h_file;
b912730e 16206+ struct inode *inode;
9dbd164d
AM
16207+
16208+ AuDbgVmRegion(file, vma);
1308ab2a 16209+
b912730e
AM
16210+ inode = file_inode(file);
16211+ sb = inode->i_sb;
9dbd164d 16212+ lockdep_off();
e49829fe 16213+ si_read_lock(sb, AuLock_NOPLMW);
4a4d8108 16214+
b912730e 16215+ h_file = au_write_pre(file, wlock, /*wpre*/NULL);
9dbd164d 16216+ lockdep_on();
b912730e
AM
16217+ err = PTR_ERR(h_file);
16218+ if (IS_ERR(h_file))
16219+ goto out;
1308ab2a 16220+
b912730e
AM
16221+ err = 0;
16222+ au_set_mmapped(file);
9dbd164d 16223+ au_vm_file_reset(vma, h_file);
38d290e6
JR
16224+ /*
16225+ * we cannot call security_mmap_file() here since it may acquire
16226+ * mmap_sem or i_mutex.
16227+ *
16228+ * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16229+ * au_flag_conv(vma->vm_flags));
16230+ */
9dbd164d 16231+ if (!err)
521ced18 16232+ err = call_mmap(h_file, vma);
b912730e
AM
16233+ if (!err) {
16234+ au_vm_prfile_set(vma, file);
16235+ fsstack_copy_attr_atime(inode, file_inode(h_file));
16236+ goto out_fput; /* success */
16237+ }
2cbb1c4b
JR
16238+ au_unset_mmapped(file);
16239+ au_vm_file_reset(vma, file);
b912730e 16240+
2cbb1c4b 16241+out_fput:
9dbd164d 16242+ lockdep_off();
b912730e
AM
16243+ ii_write_unlock(inode);
16244+ lockdep_on();
16245+ fput(h_file);
4f0767ce 16246+out:
b912730e 16247+ lockdep_off();
9dbd164d
AM
16248+ si_read_unlock(sb);
16249+ lockdep_on();
16250+ AuTraceErr(err);
4a4d8108
AM
16251+ return err;
16252+}
16253+
16254+/* ---------------------------------------------------------------------- */
16255+
1e00d052
AM
16256+static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16257+ int datasync)
4a4d8108
AM
16258+{
16259+ int err;
b912730e 16260+ struct au_write_pre wpre;
4a4d8108
AM
16261+ struct inode *inode;
16262+ struct file *h_file;
4a4d8108
AM
16263+
16264+ err = 0; /* -EBADF; */ /* posix? */
16265+ if (unlikely(!(file->f_mode & FMODE_WRITE)))
b912730e 16266+ goto out;
4a4d8108 16267+
b912730e
AM
16268+ inode = file_inode(file);
16269+ au_mtx_and_read_lock(inode);
16270+
521ced18 16271+ wpre.lsc = 0;
b912730e
AM
16272+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16273+ err = PTR_ERR(h_file);
16274+ if (IS_ERR(h_file))
4a4d8108 16275+ goto out_unlock;
4a4d8108 16276+
53392da6 16277+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
b912730e 16278+ au_write_post(inode, h_file, &wpre, /*written*/0);
4a4d8108 16279+
4f0767ce 16280+out_unlock:
b912730e 16281+ si_read_unlock(inode->i_sb);
febd17d6 16282+ inode_unlock(inode);
b912730e 16283+out:
4a4d8108 16284+ return err;
dece6358
AM
16285+}
16286+
4a4d8108 16287+static int aufs_fasync(int fd, struct file *file, int flag)
dece6358 16288+{
4a4d8108
AM
16289+ int err;
16290+ struct file *h_file;
4a4d8108 16291+ struct super_block *sb;
1308ab2a 16292+
b912730e 16293+ sb = file->f_path.dentry->d_sb;
e49829fe 16294+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 16295+
521ced18 16296+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
16297+ err = PTR_ERR(h_file);
16298+ if (IS_ERR(h_file))
4a4d8108
AM
16299+ goto out;
16300+
523b37e3 16301+ if (h_file->f_op->fasync)
4a4d8108 16302+ err = h_file->f_op->fasync(fd, h_file, flag);
b912730e 16303+ fput(h_file); /* instead of au_read_post() */
1308ab2a 16304+
4f0767ce 16305+out:
4a4d8108 16306+ si_read_unlock(sb);
1308ab2a 16307+ return err;
dece6358 16308+}
4a4d8108 16309+
febd17d6
JR
16310+static int aufs_setfl(struct file *file, unsigned long arg)
16311+{
16312+ int err;
16313+ struct file *h_file;
16314+ struct super_block *sb;
16315+
16316+ sb = file->f_path.dentry->d_sb;
16317+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16318+
521ced18 16319+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
febd17d6
JR
16320+ err = PTR_ERR(h_file);
16321+ if (IS_ERR(h_file))
16322+ goto out;
16323+
1c60b727
AM
16324+ /* stop calling h_file->fasync */
16325+ arg |= vfsub_file_flags(file) & FASYNC;
febd17d6
JR
16326+ err = setfl(/*unused fd*/-1, h_file, arg);
16327+ fput(h_file); /* instead of au_read_post() */
16328+
16329+out:
16330+ si_read_unlock(sb);
16331+ return err;
16332+}
16333+
4a4d8108
AM
16334+/* ---------------------------------------------------------------------- */
16335+
16336+/* no one supports this operation, currently */
16337+#if 0
16338+static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
2000de60 16339+ size_t len, loff_t *pos, int more)
4a4d8108
AM
16340+{
16341+}
16342+#endif
16343+
16344+/* ---------------------------------------------------------------------- */
16345+
16346+const struct file_operations aufs_file_fop = {
16347+ .owner = THIS_MODULE,
2cbb1c4b 16348+
027c5e7a 16349+ .llseek = default_llseek,
4a4d8108
AM
16350+
16351+ .read = aufs_read,
16352+ .write = aufs_write,
076b876e
AM
16353+ .read_iter = aufs_read_iter,
16354+ .write_iter = aufs_write_iter,
16355+
4a4d8108
AM
16356+#ifdef CONFIG_AUFS_POLL
16357+ .poll = aufs_poll,
16358+#endif
16359+ .unlocked_ioctl = aufs_ioctl_nondir,
b752ccd1 16360+#ifdef CONFIG_COMPAT
c2b27bf2 16361+ .compat_ioctl = aufs_compat_ioctl_nondir,
b752ccd1 16362+#endif
4a4d8108
AM
16363+ .mmap = aufs_mmap,
16364+ .open = aufs_open_nondir,
16365+ .flush = aufs_flush_nondir,
16366+ .release = aufs_release_nondir,
16367+ .fsync = aufs_fsync_nondir,
4a4d8108
AM
16368+ .fasync = aufs_fasync,
16369+ /* .sendpage = aufs_sendpage, */
febd17d6 16370+ .setfl = aufs_setfl,
4a4d8108
AM
16371+ .splice_write = aufs_splice_write,
16372+ .splice_read = aufs_splice_read,
16373+#if 0
16374+ .aio_splice_write = aufs_aio_splice_write,
38d290e6 16375+ .aio_splice_read = aufs_aio_splice_read,
4a4d8108 16376+#endif
521ced18
JR
16377+ .fallocate = aufs_fallocate,
16378+ .copy_file_range = aufs_copy_file_range
4a4d8108 16379+};
7f207e10
AM
16380diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
16381--- /usr/share/empty/fs/aufs/fstype.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 16382+++ linux/fs/aufs/fstype.h 2018-08-12 23:43:05.456791299 +0200
062440b3
AM
16383@@ -0,0 +1,401 @@
16384+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 16385+/*
b00004a5 16386+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
16387+ *
16388+ * This program, aufs is free software; you can redistribute it and/or modify
16389+ * it under the terms of the GNU General Public License as published by
16390+ * the Free Software Foundation; either version 2 of the License, or
16391+ * (at your option) any later version.
16392+ *
16393+ * This program is distributed in the hope that it will be useful,
16394+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16395+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16396+ * GNU General Public License for more details.
16397+ *
16398+ * You should have received a copy of the GNU General Public License
523b37e3 16399+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
16400+ */
16401+
16402+/*
16403+ * judging filesystem type
16404+ */
16405+
16406+#ifndef __AUFS_FSTYPE_H__
16407+#define __AUFS_FSTYPE_H__
16408+
16409+#ifdef __KERNEL__
16410+
16411+#include <linux/fs.h>
16412+#include <linux/magic.h>
b912730e 16413+#include <linux/nfs_fs.h>
b95c5147 16414+#include <linux/romfs_fs.h>
4a4d8108
AM
16415+
16416+static inline int au_test_aufs(struct super_block *sb)
16417+{
16418+ return sb->s_magic == AUFS_SUPER_MAGIC;
16419+}
16420+
16421+static inline const char *au_sbtype(struct super_block *sb)
16422+{
16423+ return sb->s_type->name;
16424+}
1308ab2a 16425+
16426+static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
16427+{
f0c0a007 16428+#if IS_ENABLED(CONFIG_ISO9660_FS)
2000de60 16429+ return sb->s_magic == ISOFS_SUPER_MAGIC;
dece6358
AM
16430+#else
16431+ return 0;
16432+#endif
16433+}
16434+
1308ab2a 16435+static inline int au_test_romfs(struct super_block *sb __maybe_unused)
dece6358 16436+{
f0c0a007 16437+#if IS_ENABLED(CONFIG_ROMFS_FS)
2000de60 16438+ return sb->s_magic == ROMFS_MAGIC;
dece6358
AM
16439+#else
16440+ return 0;
16441+#endif
16442+}
16443+
1308ab2a 16444+static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
dece6358 16445+{
f0c0a007 16446+#if IS_ENABLED(CONFIG_CRAMFS)
1308ab2a 16447+ return sb->s_magic == CRAMFS_MAGIC;
16448+#endif
16449+ return 0;
16450+}
16451+
16452+static inline int au_test_nfs(struct super_block *sb __maybe_unused)
16453+{
f0c0a007 16454+#if IS_ENABLED(CONFIG_NFS_FS)
1308ab2a 16455+ return sb->s_magic == NFS_SUPER_MAGIC;
dece6358
AM
16456+#else
16457+ return 0;
16458+#endif
16459+}
16460+
1308ab2a 16461+static inline int au_test_fuse(struct super_block *sb __maybe_unused)
dece6358 16462+{
f0c0a007 16463+#if IS_ENABLED(CONFIG_FUSE_FS)
1308ab2a 16464+ return sb->s_magic == FUSE_SUPER_MAGIC;
dece6358
AM
16465+#else
16466+ return 0;
16467+#endif
16468+}
16469+
1308ab2a 16470+static inline int au_test_xfs(struct super_block *sb __maybe_unused)
dece6358 16471+{
f0c0a007 16472+#if IS_ENABLED(CONFIG_XFS_FS)
1308ab2a 16473+ return sb->s_magic == XFS_SB_MAGIC;
dece6358
AM
16474+#else
16475+ return 0;
16476+#endif
16477+}
16478+
1308ab2a 16479+static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
dece6358 16480+{
1308ab2a 16481+#ifdef CONFIG_TMPFS
16482+ return sb->s_magic == TMPFS_MAGIC;
16483+#else
16484+ return 0;
dece6358 16485+#endif
dece6358
AM
16486+}
16487+
1308ab2a 16488+static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
1facf9fc 16489+{
f0c0a007 16490+#if IS_ENABLED(CONFIG_ECRYPT_FS)
1308ab2a 16491+ return !strcmp(au_sbtype(sb), "ecryptfs");
16492+#else
16493+ return 0;
16494+#endif
1facf9fc 16495+}
16496+
1308ab2a 16497+static inline int au_test_ramfs(struct super_block *sb)
16498+{
16499+ return sb->s_magic == RAMFS_MAGIC;
16500+}
16501+
16502+static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
16503+{
f0c0a007 16504+#if IS_ENABLED(CONFIG_UBIFS_FS)
1308ab2a 16505+ return sb->s_magic == UBIFS_SUPER_MAGIC;
16506+#else
16507+ return 0;
16508+#endif
16509+}
16510+
16511+static inline int au_test_procfs(struct super_block *sb __maybe_unused)
16512+{
16513+#ifdef CONFIG_PROC_FS
16514+ return sb->s_magic == PROC_SUPER_MAGIC;
16515+#else
16516+ return 0;
16517+#endif
16518+}
16519+
16520+static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
16521+{
16522+#ifdef CONFIG_SYSFS
16523+ return sb->s_magic == SYSFS_MAGIC;
16524+#else
16525+ return 0;
16526+#endif
16527+}
16528+
16529+static inline int au_test_configfs(struct super_block *sb __maybe_unused)
16530+{
f0c0a007 16531+#if IS_ENABLED(CONFIG_CONFIGFS_FS)
1308ab2a 16532+ return sb->s_magic == CONFIGFS_MAGIC;
16533+#else
16534+ return 0;
16535+#endif
16536+}
16537+
16538+static inline int au_test_minix(struct super_block *sb __maybe_unused)
16539+{
f0c0a007 16540+#if IS_ENABLED(CONFIG_MINIX_FS)
1308ab2a 16541+ return sb->s_magic == MINIX3_SUPER_MAGIC
16542+ || sb->s_magic == MINIX2_SUPER_MAGIC
16543+ || sb->s_magic == MINIX2_SUPER_MAGIC2
16544+ || sb->s_magic == MINIX_SUPER_MAGIC
16545+ || sb->s_magic == MINIX_SUPER_MAGIC2;
16546+#else
16547+ return 0;
16548+#endif
16549+}
16550+
1308ab2a 16551+static inline int au_test_fat(struct super_block *sb __maybe_unused)
16552+{
f0c0a007 16553+#if IS_ENABLED(CONFIG_FAT_FS)
1308ab2a 16554+ return sb->s_magic == MSDOS_SUPER_MAGIC;
16555+#else
16556+ return 0;
16557+#endif
16558+}
16559+
16560+static inline int au_test_msdos(struct super_block *sb)
16561+{
16562+ return au_test_fat(sb);
16563+}
16564+
16565+static inline int au_test_vfat(struct super_block *sb)
16566+{
16567+ return au_test_fat(sb);
16568+}
16569+
16570+static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
16571+{
16572+#ifdef CONFIG_SECURITYFS
16573+ return sb->s_magic == SECURITYFS_MAGIC;
16574+#else
16575+ return 0;
16576+#endif
16577+}
16578+
16579+static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
16580+{
f0c0a007 16581+#if IS_ENABLED(CONFIG_SQUASHFS)
1308ab2a 16582+ return sb->s_magic == SQUASHFS_MAGIC;
16583+#else
16584+ return 0;
16585+#endif
16586+}
16587+
16588+static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
16589+{
f0c0a007 16590+#if IS_ENABLED(CONFIG_BTRFS_FS)
1308ab2a 16591+ return sb->s_magic == BTRFS_SUPER_MAGIC;
16592+#else
16593+ return 0;
16594+#endif
16595+}
16596+
16597+static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
16598+{
f0c0a007 16599+#if IS_ENABLED(CONFIG_XENFS)
1308ab2a 16600+ return sb->s_magic == XENFS_SUPER_MAGIC;
16601+#else
16602+ return 0;
16603+#endif
16604+}
16605+
16606+static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
16607+{
16608+#ifdef CONFIG_DEBUG_FS
16609+ return sb->s_magic == DEBUGFS_MAGIC;
16610+#else
16611+ return 0;
16612+#endif
16613+}
16614+
16615+static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
16616+{
f0c0a007 16617+#if IS_ENABLED(CONFIG_NILFS)
1308ab2a 16618+ return sb->s_magic == NILFS_SUPER_MAGIC;
16619+#else
16620+ return 0;
16621+#endif
16622+}
16623+
4a4d8108
AM
16624+static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
16625+{
f0c0a007 16626+#if IS_ENABLED(CONFIG_HFSPLUS_FS)
4a4d8108
AM
16627+ return sb->s_magic == HFSPLUS_SUPER_MAGIC;
16628+#else
16629+ return 0;
16630+#endif
16631+}
16632+
1308ab2a 16633+/* ---------------------------------------------------------------------- */
16634+/*
16635+ * they can't be an aufs branch.
16636+ */
16637+static inline int au_test_fs_unsuppoted(struct super_block *sb)
16638+{
16639+ return
16640+#ifndef CONFIG_AUFS_BR_RAMFS
16641+ au_test_ramfs(sb) ||
16642+#endif
16643+ au_test_procfs(sb)
16644+ || au_test_sysfs(sb)
16645+ || au_test_configfs(sb)
16646+ || au_test_debugfs(sb)
16647+ || au_test_securityfs(sb)
16648+ || au_test_xenfs(sb)
16649+ || au_test_ecryptfs(sb)
16650+ /* || !strcmp(au_sbtype(sb), "unionfs") */
16651+ || au_test_aufs(sb); /* will be supported in next version */
16652+}
16653+
1308ab2a 16654+static inline int au_test_fs_remote(struct super_block *sb)
16655+{
16656+ return !au_test_tmpfs(sb)
16657+#ifdef CONFIG_AUFS_BR_RAMFS
16658+ && !au_test_ramfs(sb)
16659+#endif
16660+ && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
16661+}
16662+
16663+/* ---------------------------------------------------------------------- */
16664+
16665+/*
16666+ * Note: these functions (below) are created after reading ->getattr() in all
16667+ * filesystems under linux/fs. it means we have to do so in every update...
16668+ */
16669+
16670+/*
16671+ * some filesystems require getattr to refresh the inode attributes before
16672+ * referencing.
16673+ * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
16674+ * and leave the work for d_revalidate()
16675+ */
16676+static inline int au_test_fs_refresh_iattr(struct super_block *sb)
16677+{
16678+ return au_test_nfs(sb)
16679+ || au_test_fuse(sb)
1308ab2a 16680+ /* || au_test_btrfs(sb) */ /* untested */
1308ab2a 16681+ ;
16682+}
16683+
16684+/*
16685+ * filesystems which don't maintain i_size or i_blocks.
16686+ */
16687+static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
16688+{
16689+ return au_test_xfs(sb)
4a4d8108
AM
16690+ || au_test_btrfs(sb)
16691+ || au_test_ubifs(sb)
16692+ || au_test_hfsplus(sb) /* maintained, but incorrect */
1308ab2a 16693+ /* || au_test_minix(sb) */ /* untested */
16694+ ;
16695+}
16696+
16697+/*
16698+ * filesystems which don't store the correct value in some of their inode
16699+ * attributes.
16700+ */
16701+static inline int au_test_fs_bad_iattr(struct super_block *sb)
16702+{
16703+ return au_test_fs_bad_iattr_size(sb)
1308ab2a 16704+ || au_test_fat(sb)
16705+ || au_test_msdos(sb)
16706+ || au_test_vfat(sb);
1facf9fc 16707+}
16708+
16709+/* they don't check i_nlink in link(2) */
16710+static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
16711+{
16712+ return au_test_tmpfs(sb)
16713+#ifdef CONFIG_AUFS_BR_RAMFS
16714+ || au_test_ramfs(sb)
16715+#endif
4a4d8108 16716+ || au_test_ubifs(sb)
4a4d8108 16717+ || au_test_hfsplus(sb);
1facf9fc 16718+}
16719+
16720+/*
16721+ * filesystems which sets S_NOATIME and S_NOCMTIME.
16722+ */
16723+static inline int au_test_fs_notime(struct super_block *sb)
16724+{
16725+ return au_test_nfs(sb)
16726+ || au_test_fuse(sb)
dece6358 16727+ || au_test_ubifs(sb)
1facf9fc 16728+ ;
16729+}
16730+
1facf9fc 16731+/* temporary support for i#1 in cramfs */
16732+static inline int au_test_fs_unique_ino(struct inode *inode)
16733+{
16734+ if (au_test_cramfs(inode->i_sb))
16735+ return inode->i_ino != 1;
16736+ return 1;
16737+}
16738+
16739+/* ---------------------------------------------------------------------- */
16740+
16741+/*
16742+ * the filesystem where the xino files placed must support i/o after unlink and
16743+ * maintain i_size and i_blocks.
16744+ */
16745+static inline int au_test_fs_bad_xino(struct super_block *sb)
16746+{
16747+ return au_test_fs_remote(sb)
16748+ || au_test_fs_bad_iattr_size(sb)
1facf9fc 16749+ /* don't want unnecessary work for xino */
16750+ || au_test_aufs(sb)
1308ab2a 16751+ || au_test_ecryptfs(sb)
16752+ || au_test_nilfs(sb);
1facf9fc 16753+}
16754+
16755+static inline int au_test_fs_trunc_xino(struct super_block *sb)
16756+{
16757+ return au_test_tmpfs(sb)
16758+ || au_test_ramfs(sb);
16759+}
16760+
16761+/*
16762+ * test if the @sb is real-readonly.
16763+ */
16764+static inline int au_test_fs_rr(struct super_block *sb)
16765+{
16766+ return au_test_squashfs(sb)
16767+ || au_test_iso9660(sb)
16768+ || au_test_cramfs(sb)
16769+ || au_test_romfs(sb);
16770+}
16771+
b912730e
AM
16772+/*
16773+ * test if the @inode is nfs with 'noacl' option
2121bcd9 16774+ * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
b912730e
AM
16775+ */
16776+static inline int au_test_nfs_noacl(struct inode *inode)
16777+{
16778+ return au_test_nfs(inode->i_sb)
16779+ /* && IS_POSIXACL(inode) */
16780+ && !nfs_server_capable(inode, NFS_CAP_ACLS);
16781+}
16782+
1facf9fc 16783+#endif /* __KERNEL__ */
16784+#endif /* __AUFS_FSTYPE_H__ */
8b6a4947
AM
16785diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
16786--- /usr/share/empty/fs/aufs/hbl.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 16787+++ linux/fs/aufs/hbl.h 2018-08-12 23:43:05.456791299 +0200
062440b3
AM
16788@@ -0,0 +1,65 @@
16789+/* SPDX-License-Identifier: GPL-2.0 */
8b6a4947 16790+/*
b00004a5 16791+ * Copyright (C) 2017-2018 Junjiro R. Okajima
8b6a4947
AM
16792+ *
16793+ * This program, aufs is free software; you can redistribute it and/or modify
16794+ * it under the terms of the GNU General Public License as published by
16795+ * the Free Software Foundation; either version 2 of the License, or
16796+ * (at your option) any later version.
16797+ *
16798+ * This program is distributed in the hope that it will be useful,
16799+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16800+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16801+ * GNU General Public License for more details.
16802+ *
16803+ * You should have received a copy of the GNU General Public License
16804+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16805+ */
16806+
16807+/*
16808+ * helpers for hlist_bl.h
16809+ */
16810+
16811+#ifndef __AUFS_HBL_H__
16812+#define __AUFS_HBL_H__
16813+
16814+#ifdef __KERNEL__
16815+
16816+#include <linux/list_bl.h>
16817+
16818+static inline void au_hbl_add(struct hlist_bl_node *node,
16819+ struct hlist_bl_head *hbl)
16820+{
16821+ hlist_bl_lock(hbl);
16822+ hlist_bl_add_head(node, hbl);
16823+ hlist_bl_unlock(hbl);
16824+}
16825+
16826+static inline void au_hbl_del(struct hlist_bl_node *node,
16827+ struct hlist_bl_head *hbl)
16828+{
16829+ hlist_bl_lock(hbl);
16830+ hlist_bl_del(node);
16831+ hlist_bl_unlock(hbl);
16832+}
16833+
16834+#define au_hbl_for_each(pos, head) \
16835+ for (pos = hlist_bl_first(head); \
16836+ pos; \
16837+ pos = pos->next)
16838+
16839+static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
16840+{
16841+ unsigned long cnt;
16842+ struct hlist_bl_node *pos;
16843+
16844+ cnt = 0;
16845+ hlist_bl_lock(hbl);
16846+ au_hbl_for_each(pos, hbl)
16847+ cnt++;
16848+ hlist_bl_unlock(hbl);
16849+ return cnt;
16850+}
16851+
16852+#endif /* __KERNEL__ */
16853+#endif /* __AUFS_HBL_H__ */
7f207e10
AM
16854diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
16855--- /usr/share/empty/fs/aufs/hfsnotify.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 16856+++ linux/fs/aufs/hfsnotify.c 2018-08-12 23:43:05.456791299 +0200
062440b3 16857@@ -0,0 +1,290 @@
cd7a4cd9 16858+// SPDX-License-Identifier: GPL-2.0
1facf9fc 16859+/*
b00004a5 16860+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 16861+ *
16862+ * This program, aufs is free software; you can redistribute it and/or modify
16863+ * it under the terms of the GNU General Public License as published by
16864+ * the Free Software Foundation; either version 2 of the License, or
16865+ * (at your option) any later version.
dece6358
AM
16866+ *
16867+ * This program is distributed in the hope that it will be useful,
16868+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16869+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16870+ * GNU General Public License for more details.
16871+ *
16872+ * You should have received a copy of the GNU General Public License
523b37e3 16873+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 16874+ */
16875+
16876+/*
4a4d8108 16877+ * fsnotify for the lower directories
1facf9fc 16878+ */
16879+
16880+#include "aufs.h"
16881+
4a4d8108
AM
16882+/* FS_IN_IGNORED is unnecessary */
16883+static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
16884+ | FS_CREATE | FS_EVENT_ON_CHILD);
7f207e10 16885+static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
7eafdf33 16886+static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
1facf9fc 16887+
0c5527e5 16888+static void au_hfsn_free_mark(struct fsnotify_mark *mark)
1facf9fc 16889+{
0c5527e5
AM
16890+ struct au_hnotify *hn = container_of(mark, struct au_hnotify,
16891+ hn_mark);
5afbbe0d 16892+ /* AuDbg("here\n"); */
1c60b727 16893+ au_cache_free_hnotify(hn);
8b6a4947 16894+ smp_mb__before_atomic(); /* for atomic64_dec */
1716fcea
AM
16895+ if (atomic64_dec_and_test(&au_hfsn_ifree))
16896+ wake_up(&au_hfsn_wq);
4a4d8108 16897+}
1facf9fc 16898+
027c5e7a 16899+static int au_hfsn_alloc(struct au_hinode *hinode)
4a4d8108 16900+{
1716fcea 16901+ int err;
027c5e7a
AM
16902+ struct au_hnotify *hn;
16903+ struct super_block *sb;
16904+ struct au_branch *br;
0c5527e5 16905+ struct fsnotify_mark *mark;
027c5e7a 16906+ aufs_bindex_t bindex;
1facf9fc 16907+
027c5e7a
AM
16908+ hn = hinode->hi_notify;
16909+ sb = hn->hn_aufs_inode->i_sb;
16910+ bindex = au_br_index(sb, hinode->hi_id);
16911+ br = au_sbr(sb, bindex);
1716fcea
AM
16912+ AuDebugOn(!br->br_hfsn);
16913+
0c5527e5 16914+ mark = &hn->hn_mark;
ffa93bbd 16915+ fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
0c5527e5 16916+ mark->mask = AuHfsnMask;
7f207e10
AM
16917+ /*
16918+ * by udba rename or rmdir, aufs assign a new inode to the known
16919+ * h_inode, so specify 1 to allow dups.
16920+ */
c1595e42 16921+ lockdep_off();
ffa93bbd
AM
16922+ err = fsnotify_add_mark(mark, hinode->hi_inode, /*mnt*/NULL,
16923+ /*allow_dups*/1);
c1595e42 16924+ lockdep_on();
1716fcea
AM
16925+
16926+ return err;
1facf9fc 16927+}
16928+
7eafdf33 16929+static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
1facf9fc 16930+{
0c5527e5 16931+ struct fsnotify_mark *mark;
7eafdf33 16932+ unsigned long long ull;
1716fcea 16933+ struct fsnotify_group *group;
7eafdf33
AM
16934+
16935+ ull = atomic64_inc_return(&au_hfsn_ifree);
16936+ BUG_ON(!ull);
953406b4 16937+
0c5527e5 16938+ mark = &hn->hn_mark;
1716fcea
AM
16939+ spin_lock(&mark->lock);
16940+ group = mark->group;
16941+ fsnotify_get_group(group);
16942+ spin_unlock(&mark->lock);
c1595e42 16943+ lockdep_off();
1716fcea 16944+ fsnotify_destroy_mark(mark, group);
5afbbe0d 16945+ fsnotify_put_mark(mark);
1716fcea 16946+ fsnotify_put_group(group);
c1595e42 16947+ lockdep_on();
7f207e10 16948+
7eafdf33
AM
16949+ /* free hn by myself */
16950+ return 0;
1facf9fc 16951+}
16952+
16953+/* ---------------------------------------------------------------------- */
16954+
4a4d8108 16955+static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
1facf9fc 16956+{
0c5527e5 16957+ struct fsnotify_mark *mark;
1facf9fc 16958+
0c5527e5
AM
16959+ mark = &hinode->hi_notify->hn_mark;
16960+ spin_lock(&mark->lock);
1facf9fc 16961+ if (do_set) {
0c5527e5
AM
16962+ AuDebugOn(mark->mask & AuHfsnMask);
16963+ mark->mask |= AuHfsnMask;
1facf9fc 16964+ } else {
0c5527e5
AM
16965+ AuDebugOn(!(mark->mask & AuHfsnMask));
16966+ mark->mask &= ~AuHfsnMask;
1facf9fc 16967+ }
0c5527e5 16968+ spin_unlock(&mark->lock);
4a4d8108 16969+ /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
1facf9fc 16970+}
16971+
4a4d8108 16972+/* ---------------------------------------------------------------------- */
1facf9fc 16973+
4a4d8108
AM
16974+/* #define AuDbgHnotify */
16975+#ifdef AuDbgHnotify
16976+static char *au_hfsn_name(u32 mask)
16977+{
16978+#ifdef CONFIG_AUFS_DEBUG
c06a8ce3
AM
16979+#define test_ret(flag) \
16980+ do { \
16981+ if (mask & flag) \
16982+ return #flag; \
16983+ } while (0)
4a4d8108
AM
16984+ test_ret(FS_ACCESS);
16985+ test_ret(FS_MODIFY);
16986+ test_ret(FS_ATTRIB);
16987+ test_ret(FS_CLOSE_WRITE);
16988+ test_ret(FS_CLOSE_NOWRITE);
16989+ test_ret(FS_OPEN);
16990+ test_ret(FS_MOVED_FROM);
16991+ test_ret(FS_MOVED_TO);
16992+ test_ret(FS_CREATE);
16993+ test_ret(FS_DELETE);
16994+ test_ret(FS_DELETE_SELF);
16995+ test_ret(FS_MOVE_SELF);
16996+ test_ret(FS_UNMOUNT);
16997+ test_ret(FS_Q_OVERFLOW);
16998+ test_ret(FS_IN_IGNORED);
b912730e 16999+ test_ret(FS_ISDIR);
4a4d8108
AM
17000+ test_ret(FS_IN_ONESHOT);
17001+ test_ret(FS_EVENT_ON_CHILD);
17002+ return "";
17003+#undef test_ret
17004+#else
17005+ return "??";
17006+#endif
1facf9fc 17007+}
4a4d8108 17008+#endif
1facf9fc 17009+
17010+/* ---------------------------------------------------------------------- */
17011+
1716fcea
AM
17012+static void au_hfsn_free_group(struct fsnotify_group *group)
17013+{
17014+ struct au_br_hfsnotify *hfsn = group->private;
17015+
5afbbe0d 17016+ /* AuDbg("here\n"); */
1c60b727 17017+ kfree(hfsn);
1716fcea
AM
17018+}
17019+
4a4d8108 17020+static int au_hfsn_handle_event(struct fsnotify_group *group,
fb47a38f 17021+ struct inode *inode,
a2654f78 17022+ u32 mask, const void *data, int data_type,
ffa93bbd
AM
17023+ const unsigned char *file_name, u32 cookie,
17024+ struct fsnotify_iter_info *iter_info)
1facf9fc 17025+{
17026+ int err;
4a4d8108
AM
17027+ struct au_hnotify *hnotify;
17028+ struct inode *h_dir, *h_inode;
fb47a38f 17029+ struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
cd7a4cd9 17030+ struct fsnotify_mark *inode_mark;
4a4d8108 17031+
fb47a38f 17032+ AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
1facf9fc 17033+
17034+ err = 0;
0c5527e5 17035+ /* if FS_UNMOUNT happens, there must be another bug */
4a4d8108 17036+ AuDebugOn(mask & FS_UNMOUNT);
0c5527e5 17037+ if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
1facf9fc 17038+ goto out;
1facf9fc 17039+
fb47a38f
JR
17040+ h_dir = inode;
17041+ h_inode = NULL;
4a4d8108 17042+#ifdef AuDbgHnotify
392086de 17043+ au_debug_on();
4a4d8108
AM
17044+ if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
17045+ || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
17046+ AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
17047+ h_dir->i_ino, mask, au_hfsn_name(mask),
17048+ AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
17049+ /* WARN_ON(1); */
1facf9fc 17050+ }
392086de 17051+ au_debug_off();
1facf9fc 17052+#endif
4a4d8108 17053+
cd7a4cd9 17054+ inode_mark = fsnotify_iter_inode_mark(iter_info);
0c5527e5
AM
17055+ AuDebugOn(!inode_mark);
17056+ hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
17057+ err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
1facf9fc 17058+
4a4d8108
AM
17059+out:
17060+ return err;
17061+}
1facf9fc 17062+
4a4d8108 17063+static struct fsnotify_ops au_hfsn_ops = {
1716fcea 17064+ .handle_event = au_hfsn_handle_event,
ffa93bbd
AM
17065+ .free_group_priv = au_hfsn_free_group,
17066+ .free_mark = au_hfsn_free_mark
4a4d8108
AM
17067+};
17068+
17069+/* ---------------------------------------------------------------------- */
17070+
027c5e7a
AM
17071+static void au_hfsn_fin_br(struct au_branch *br)
17072+{
1716fcea 17073+ struct au_br_hfsnotify *hfsn;
027c5e7a 17074+
1716fcea 17075+ hfsn = br->br_hfsn;
c1595e42
JR
17076+ if (hfsn) {
17077+ lockdep_off();
1716fcea 17078+ fsnotify_put_group(hfsn->hfsn_group);
c1595e42
JR
17079+ lockdep_on();
17080+ }
027c5e7a
AM
17081+}
17082+
1716fcea 17083+static int au_hfsn_init_br(struct au_branch *br, int perm)
4a4d8108
AM
17084+{
17085+ int err;
1716fcea
AM
17086+ struct fsnotify_group *group;
17087+ struct au_br_hfsnotify *hfsn;
1facf9fc 17088+
4a4d8108 17089+ err = 0;
1716fcea
AM
17090+ br->br_hfsn = NULL;
17091+ if (!au_br_hnotifyable(perm))
027c5e7a 17092+ goto out;
027c5e7a 17093+
1716fcea
AM
17094+ err = -ENOMEM;
17095+ hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
17096+ if (unlikely(!hfsn))
027c5e7a
AM
17097+ goto out;
17098+
1716fcea
AM
17099+ err = 0;
17100+ group = fsnotify_alloc_group(&au_hfsn_ops);
17101+ if (IS_ERR(group)) {
17102+ err = PTR_ERR(group);
0c5527e5 17103+ pr_err("fsnotify_alloc_group() failed, %d\n", err);
1716fcea 17104+ goto out_hfsn;
4a4d8108 17105+ }
1facf9fc 17106+
1716fcea
AM
17107+ group->private = hfsn;
17108+ hfsn->hfsn_group = group;
17109+ br->br_hfsn = hfsn;
17110+ goto out; /* success */
17111+
17112+out_hfsn:
1c60b727 17113+ kfree(hfsn);
027c5e7a 17114+out:
1716fcea
AM
17115+ return err;
17116+}
17117+
17118+static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
17119+{
17120+ int err;
17121+
17122+ err = 0;
17123+ if (!br->br_hfsn)
17124+ err = au_hfsn_init_br(br, perm);
17125+
1facf9fc 17126+ return err;
17127+}
17128+
7eafdf33
AM
17129+/* ---------------------------------------------------------------------- */
17130+
17131+static void au_hfsn_fin(void)
17132+{
17133+ AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
17134+ wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
17135+}
17136+
4a4d8108
AM
17137+const struct au_hnotify_op au_hnotify_op = {
17138+ .ctl = au_hfsn_ctl,
17139+ .alloc = au_hfsn_alloc,
17140+ .free = au_hfsn_free,
1facf9fc 17141+
7eafdf33
AM
17142+ .fin = au_hfsn_fin,
17143+
027c5e7a
AM
17144+ .reset_br = au_hfsn_reset_br,
17145+ .fin_br = au_hfsn_fin_br,
17146+ .init_br = au_hfsn_init_br
4a4d8108 17147+};
7f207e10
AM
17148diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
17149--- /usr/share/empty/fs/aufs/hfsplus.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 17150+++ linux/fs/aufs/hfsplus.c 2018-08-12 23:43:05.456791299 +0200
062440b3 17151@@ -0,0 +1,57 @@
cd7a4cd9 17152+// SPDX-License-Identifier: GPL-2.0
4a4d8108 17153+/*
b00004a5 17154+ * Copyright (C) 2010-2018 Junjiro R. Okajima
4a4d8108
AM
17155+ *
17156+ * This program, aufs is free software; you can redistribute it and/or modify
17157+ * it under the terms of the GNU General Public License as published by
17158+ * the Free Software Foundation; either version 2 of the License, or
17159+ * (at your option) any later version.
17160+ *
17161+ * This program is distributed in the hope that it will be useful,
17162+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17163+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17164+ * GNU General Public License for more details.
17165+ *
17166+ * You should have received a copy of the GNU General Public License
523b37e3 17167+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 17168+ */
1facf9fc 17169+
4a4d8108
AM
17170+/*
17171+ * special support for filesystems which aqucires an inode mutex
17172+ * at final closing a file, eg, hfsplus.
17173+ *
17174+ * This trick is very simple and stupid, just to open the file before really
17175+ * neceeary open to tell hfsplus that this is not the final closing.
17176+ * The caller should call au_h_open_pre() after acquiring the inode mutex,
17177+ * and au_h_open_post() after releasing it.
17178+ */
1facf9fc 17179+
4a4d8108 17180+#include "aufs.h"
1facf9fc 17181+
392086de
AM
17182+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
17183+ int force_wr)
4a4d8108
AM
17184+{
17185+ struct file *h_file;
17186+ struct dentry *h_dentry;
1facf9fc 17187+
4a4d8108
AM
17188+ h_dentry = au_h_dptr(dentry, bindex);
17189+ AuDebugOn(!h_dentry);
5527c038 17190+ AuDebugOn(d_is_negative(h_dentry));
4a4d8108
AM
17191+
17192+ h_file = NULL;
17193+ if (au_test_hfsplus(h_dentry->d_sb)
7e9cd9fe 17194+ && d_is_reg(h_dentry))
4a4d8108
AM
17195+ h_file = au_h_open(dentry, bindex,
17196+ O_RDONLY | O_NOATIME | O_LARGEFILE,
392086de 17197+ /*file*/NULL, force_wr);
4a4d8108 17198+ return h_file;
1facf9fc 17199+}
17200+
4a4d8108
AM
17201+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
17202+ struct file *h_file)
17203+{
17204+ if (h_file) {
17205+ fput(h_file);
17206+ au_sbr_put(dentry->d_sb, bindex);
17207+ }
17208+}
7f207e10
AM
17209diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
17210--- /usr/share/empty/fs/aufs/hnotify.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 17211+++ linux/fs/aufs/hnotify.c 2018-08-12 23:43:05.456791299 +0200
062440b3 17212@@ -0,0 +1,720 @@
cd7a4cd9 17213+// SPDX-License-Identifier: GPL-2.0
e49829fe 17214+/*
b00004a5 17215+ * Copyright (C) 2005-2018 Junjiro R. Okajima
e49829fe
JR
17216+ *
17217+ * This program, aufs is free software; you can redistribute it and/or modify
17218+ * it under the terms of the GNU General Public License as published by
17219+ * the Free Software Foundation; either version 2 of the License, or
17220+ * (at your option) any later version.
17221+ *
17222+ * This program is distributed in the hope that it will be useful,
17223+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17224+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17225+ * GNU General Public License for more details.
17226+ *
17227+ * You should have received a copy of the GNU General Public License
523b37e3 17228+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
17229+ */
17230+
17231+/*
7f207e10 17232+ * abstraction to notify the direct changes on lower directories
e49829fe
JR
17233+ */
17234+
17235+#include "aufs.h"
17236+
027c5e7a 17237+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
e49829fe
JR
17238+{
17239+ int err;
7f207e10 17240+ struct au_hnotify *hn;
1facf9fc 17241+
4a4d8108
AM
17242+ err = -ENOMEM;
17243+ hn = au_cache_alloc_hnotify();
17244+ if (hn) {
17245+ hn->hn_aufs_inode = inode;
027c5e7a
AM
17246+ hinode->hi_notify = hn;
17247+ err = au_hnotify_op.alloc(hinode);
17248+ AuTraceErr(err);
17249+ if (unlikely(err)) {
17250+ hinode->hi_notify = NULL;
1c60b727 17251+ au_cache_free_hnotify(hn);
4a4d8108
AM
17252+ /*
17253+ * The upper dir was removed by udba, but the same named
17254+ * dir left. In this case, aufs assignes a new inode
17255+ * number and set the monitor again.
17256+ * For the lower dir, the old monitnor is still left.
17257+ */
17258+ if (err == -EEXIST)
17259+ err = 0;
17260+ }
1308ab2a 17261+ }
1308ab2a 17262+
027c5e7a 17263+ AuTraceErr(err);
1308ab2a 17264+ return err;
dece6358 17265+}
1facf9fc 17266+
4a4d8108 17267+void au_hn_free(struct au_hinode *hinode)
dece6358 17268+{
4a4d8108 17269+ struct au_hnotify *hn;
1facf9fc 17270+
4a4d8108
AM
17271+ hn = hinode->hi_notify;
17272+ if (hn) {
4a4d8108 17273+ hinode->hi_notify = NULL;
7eafdf33 17274+ if (au_hnotify_op.free(hinode, hn))
1c60b727 17275+ au_cache_free_hnotify(hn);
4a4d8108
AM
17276+ }
17277+}
dece6358 17278+
4a4d8108 17279+/* ---------------------------------------------------------------------- */
dece6358 17280+
4a4d8108
AM
17281+void au_hn_ctl(struct au_hinode *hinode, int do_set)
17282+{
17283+ if (hinode->hi_notify)
17284+ au_hnotify_op.ctl(hinode, do_set);
17285+}
17286+
17287+void au_hn_reset(struct inode *inode, unsigned int flags)
17288+{
5afbbe0d 17289+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
17290+ struct inode *hi;
17291+ struct dentry *iwhdentry;
1facf9fc 17292+
5afbbe0d
AM
17293+ bbot = au_ibbot(inode);
17294+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108
AM
17295+ hi = au_h_iptr(inode, bindex);
17296+ if (!hi)
17297+ continue;
1308ab2a 17298+
febd17d6 17299+ /* inode_lock_nested(hi, AuLsc_I_CHILD); */
4a4d8108
AM
17300+ iwhdentry = au_hi_wh(inode, bindex);
17301+ if (iwhdentry)
17302+ dget(iwhdentry);
17303+ au_igrab(hi);
17304+ au_set_h_iptr(inode, bindex, NULL, 0);
17305+ au_set_h_iptr(inode, bindex, au_igrab(hi),
17306+ flags & ~AuHi_XINO);
17307+ iput(hi);
17308+ dput(iwhdentry);
febd17d6 17309+ /* inode_unlock(hi); */
1facf9fc 17310+ }
1facf9fc 17311+}
17312+
1308ab2a 17313+/* ---------------------------------------------------------------------- */
1facf9fc 17314+
4a4d8108 17315+static int hn_xino(struct inode *inode, struct inode *h_inode)
1facf9fc 17316+{
4a4d8108 17317+ int err;
5afbbe0d 17318+ aufs_bindex_t bindex, bbot, bfound, btop;
4a4d8108 17319+ struct inode *h_i;
1facf9fc 17320+
4a4d8108
AM
17321+ err = 0;
17322+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17323+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17324+ goto out;
17325+ }
1facf9fc 17326+
4a4d8108 17327+ bfound = -1;
5afbbe0d
AM
17328+ bbot = au_ibbot(inode);
17329+ btop = au_ibtop(inode);
4a4d8108 17330+#if 0 /* reserved for future use */
5afbbe0d 17331+ if (bindex == bbot) {
4a4d8108
AM
17332+ /* keep this ino in rename case */
17333+ goto out;
17334+ }
17335+#endif
5afbbe0d 17336+ for (bindex = btop; bindex <= bbot; bindex++)
4a4d8108
AM
17337+ if (au_h_iptr(inode, bindex) == h_inode) {
17338+ bfound = bindex;
17339+ break;
17340+ }
17341+ if (bfound < 0)
1308ab2a 17342+ goto out;
1facf9fc 17343+
5afbbe0d 17344+ for (bindex = btop; bindex <= bbot; bindex++) {
4a4d8108
AM
17345+ h_i = au_h_iptr(inode, bindex);
17346+ if (!h_i)
17347+ continue;
1facf9fc 17348+
4a4d8108
AM
17349+ err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
17350+ /* ignore this error */
17351+ /* bad action? */
1facf9fc 17352+ }
1facf9fc 17353+
4a4d8108 17354+ /* children inode number will be broken */
1facf9fc 17355+
4f0767ce 17356+out:
4a4d8108
AM
17357+ AuTraceErr(err);
17358+ return err;
1facf9fc 17359+}
17360+
4a4d8108 17361+static int hn_gen_tree(struct dentry *dentry)
1facf9fc 17362+{
4a4d8108
AM
17363+ int err, i, j, ndentry;
17364+ struct au_dcsub_pages dpages;
17365+ struct au_dpage *dpage;
17366+ struct dentry **dentries;
1facf9fc 17367+
4a4d8108
AM
17368+ err = au_dpages_init(&dpages, GFP_NOFS);
17369+ if (unlikely(err))
17370+ goto out;
17371+ err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
17372+ if (unlikely(err))
17373+ goto out_dpages;
1facf9fc 17374+
4a4d8108
AM
17375+ for (i = 0; i < dpages.ndpage; i++) {
17376+ dpage = dpages.dpages + i;
17377+ dentries = dpage->dentries;
17378+ ndentry = dpage->ndentry;
17379+ for (j = 0; j < ndentry; j++) {
17380+ struct dentry *d;
17381+
17382+ d = dentries[j];
17383+ if (IS_ROOT(d))
17384+ continue;
17385+
4a4d8108 17386+ au_digen_dec(d);
5527c038 17387+ if (d_really_is_positive(d))
4a4d8108
AM
17388+ /* todo: reset children xino?
17389+ cached children only? */
5527c038 17390+ au_iigen_dec(d_inode(d));
1308ab2a 17391+ }
dece6358 17392+ }
1facf9fc 17393+
4f0767ce 17394+out_dpages:
4a4d8108 17395+ au_dpages_free(&dpages);
dece6358 17396+
027c5e7a 17397+#if 0
4a4d8108
AM
17398+ /* discard children */
17399+ dentry_unhash(dentry);
17400+ dput(dentry);
027c5e7a 17401+#endif
4f0767ce 17402+out:
dece6358
AM
17403+ return err;
17404+}
17405+
1308ab2a 17406+/*
4a4d8108 17407+ * return 0 if processed.
1308ab2a 17408+ */
4a4d8108
AM
17409+static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
17410+ const unsigned int isdir)
dece6358 17411+{
1308ab2a 17412+ int err;
4a4d8108
AM
17413+ struct dentry *d;
17414+ struct qstr *dname;
1facf9fc 17415+
4a4d8108
AM
17416+ err = 1;
17417+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17418+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17419+ err = 0;
17420+ goto out;
17421+ }
dece6358 17422+
4a4d8108
AM
17423+ if (!isdir) {
17424+ AuDebugOn(!name);
17425+ au_iigen_dec(inode);
027c5e7a 17426+ spin_lock(&inode->i_lock);
c1595e42 17427+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 17428+ spin_lock(&d->d_lock);
4a4d8108
AM
17429+ dname = &d->d_name;
17430+ if (dname->len != nlen
027c5e7a
AM
17431+ && memcmp(dname->name, name, nlen)) {
17432+ spin_unlock(&d->d_lock);
4a4d8108 17433+ continue;
027c5e7a 17434+ }
4a4d8108 17435+ err = 0;
4a4d8108
AM
17436+ au_digen_dec(d);
17437+ spin_unlock(&d->d_lock);
17438+ break;
1facf9fc 17439+ }
027c5e7a 17440+ spin_unlock(&inode->i_lock);
1308ab2a 17441+ } else {
027c5e7a 17442+ au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
c1595e42 17443+ d = d_find_any_alias(inode);
4a4d8108
AM
17444+ if (!d) {
17445+ au_iigen_dec(inode);
17446+ goto out;
17447+ }
1facf9fc 17448+
027c5e7a 17449+ spin_lock(&d->d_lock);
4a4d8108 17450+ dname = &d->d_name;
027c5e7a
AM
17451+ if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
17452+ spin_unlock(&d->d_lock);
4a4d8108 17453+ err = hn_gen_tree(d);
027c5e7a
AM
17454+ spin_lock(&d->d_lock);
17455+ }
17456+ spin_unlock(&d->d_lock);
4a4d8108
AM
17457+ dput(d);
17458+ }
1facf9fc 17459+
4f0767ce 17460+out:
4a4d8108 17461+ AuTraceErr(err);
1308ab2a 17462+ return err;
17463+}
dece6358 17464+
4a4d8108 17465+static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
1facf9fc 17466+{
4a4d8108 17467+ int err;
1facf9fc 17468+
5527c038 17469+ if (IS_ROOT(dentry)) {
0c3ec466 17470+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17471+ return 0;
17472+ }
1308ab2a 17473+
4a4d8108
AM
17474+ err = 0;
17475+ if (!isdir) {
4a4d8108 17476+ au_digen_dec(dentry);
5527c038
JR
17477+ if (d_really_is_positive(dentry))
17478+ au_iigen_dec(d_inode(dentry));
4a4d8108 17479+ } else {
027c5e7a 17480+ au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
5527c038 17481+ if (d_really_is_positive(dentry))
4a4d8108
AM
17482+ err = hn_gen_tree(dentry);
17483+ }
17484+
17485+ AuTraceErr(err);
17486+ return err;
1facf9fc 17487+}
17488+
4a4d8108 17489+/* ---------------------------------------------------------------------- */
1facf9fc 17490+
4a4d8108
AM
17491+/* hnotify job flags */
17492+#define AuHnJob_XINO0 1
17493+#define AuHnJob_GEN (1 << 1)
17494+#define AuHnJob_DIRENT (1 << 2)
17495+#define AuHnJob_ISDIR (1 << 3)
17496+#define AuHnJob_TRYXINO0 (1 << 4)
17497+#define AuHnJob_MNTPNT (1 << 5)
17498+#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
7f207e10
AM
17499+#define au_fset_hnjob(flags, name) \
17500+ do { (flags) |= AuHnJob_##name; } while (0)
17501+#define au_fclr_hnjob(flags, name) \
17502+ do { (flags) &= ~AuHnJob_##name; } while (0)
1facf9fc 17503+
4a4d8108
AM
17504+enum {
17505+ AuHn_CHILD,
17506+ AuHn_PARENT,
17507+ AuHnLast
17508+};
1facf9fc 17509+
4a4d8108
AM
17510+struct au_hnotify_args {
17511+ struct inode *h_dir, *dir, *h_child_inode;
17512+ u32 mask;
17513+ unsigned int flags[AuHnLast];
17514+ unsigned int h_child_nlen;
17515+ char h_child_name[];
17516+};
1facf9fc 17517+
4a4d8108
AM
17518+struct hn_job_args {
17519+ unsigned int flags;
17520+ struct inode *inode, *h_inode, *dir, *h_dir;
17521+ struct dentry *dentry;
17522+ char *h_name;
17523+ int h_nlen;
17524+};
1308ab2a 17525+
4a4d8108
AM
17526+static int hn_job(struct hn_job_args *a)
17527+{
17528+ const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
076b876e 17529+ int e;
1308ab2a 17530+
4a4d8108
AM
17531+ /* reset xino */
17532+ if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
17533+ hn_xino(a->inode, a->h_inode); /* ignore this error */
1308ab2a 17534+
4a4d8108
AM
17535+ if (au_ftest_hnjob(a->flags, TRYXINO0)
17536+ && a->inode
17537+ && a->h_inode) {
be118d29 17538+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
38d290e6
JR
17539+ if (!a->h_inode->i_nlink
17540+ && !(a->h_inode->i_state & I_LINKABLE))
4a4d8108 17541+ hn_xino(a->inode, a->h_inode); /* ignore this error */
3c1bdaff 17542+ inode_unlock_shared(a->h_inode);
1308ab2a 17543+ }
1facf9fc 17544+
4a4d8108
AM
17545+ /* make the generation obsolete */
17546+ if (au_ftest_hnjob(a->flags, GEN)) {
076b876e 17547+ e = -1;
4a4d8108 17548+ if (a->inode)
076b876e 17549+ e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
4a4d8108 17550+ isdir);
076b876e 17551+ if (e && a->dentry)
4a4d8108
AM
17552+ hn_gen_by_name(a->dentry, isdir);
17553+ /* ignore this error */
1facf9fc 17554+ }
1facf9fc 17555+
4a4d8108
AM
17556+ /* make dir entries obsolete */
17557+ if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
17558+ struct au_vdir *vdir;
1facf9fc 17559+
4a4d8108
AM
17560+ vdir = au_ivdir(a->inode);
17561+ if (vdir)
17562+ vdir->vd_jiffy = 0;
17563+ /* IMustLock(a->inode); */
be118d29 17564+ /* inode_inc_iversion(a->inode); */
4a4d8108 17565+ }
1facf9fc 17566+
4a4d8108
AM
17567+ /* can do nothing but warn */
17568+ if (au_ftest_hnjob(a->flags, MNTPNT)
17569+ && a->dentry
17570+ && d_mountpoint(a->dentry))
523b37e3 17571+ pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
1facf9fc 17572+
4a4d8108 17573+ return 0;
1308ab2a 17574+}
1facf9fc 17575+
1308ab2a 17576+/* ---------------------------------------------------------------------- */
1facf9fc 17577+
4a4d8108
AM
17578+static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
17579+ struct inode *dir)
1308ab2a 17580+{
4a4d8108
AM
17581+ struct dentry *dentry, *d, *parent;
17582+ struct qstr *dname;
1308ab2a 17583+
c1595e42 17584+ parent = d_find_any_alias(dir);
4a4d8108
AM
17585+ if (!parent)
17586+ return NULL;
1308ab2a 17587+
4a4d8108 17588+ dentry = NULL;
027c5e7a 17589+ spin_lock(&parent->d_lock);
c1595e42 17590+ list_for_each_entry(d, &parent->d_subdirs, d_child) {
523b37e3 17591+ /* AuDbg("%pd\n", d); */
027c5e7a 17592+ spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
4a4d8108
AM
17593+ dname = &d->d_name;
17594+ if (dname->len != nlen || memcmp(dname->name, name, nlen))
027c5e7a
AM
17595+ goto cont_unlock;
17596+ if (au_di(d))
17597+ au_digen_dec(d);
17598+ else
17599+ goto cont_unlock;
c1595e42 17600+ if (au_dcount(d) > 0) {
027c5e7a 17601+ dentry = dget_dlock(d);
4a4d8108 17602+ spin_unlock(&d->d_lock);
027c5e7a 17603+ break;
dece6358 17604+ }
1facf9fc 17605+
f6b6e03d 17606+cont_unlock:
027c5e7a 17607+ spin_unlock(&d->d_lock);
1308ab2a 17608+ }
027c5e7a 17609+ spin_unlock(&parent->d_lock);
4a4d8108 17610+ dput(parent);
1facf9fc 17611+
4a4d8108
AM
17612+ if (dentry)
17613+ di_write_lock_child(dentry);
1308ab2a 17614+
4a4d8108
AM
17615+ return dentry;
17616+}
dece6358 17617+
4a4d8108
AM
17618+static struct inode *lookup_wlock_by_ino(struct super_block *sb,
17619+ aufs_bindex_t bindex, ino_t h_ino)
17620+{
17621+ struct inode *inode;
17622+ ino_t ino;
17623+ int err;
17624+
17625+ inode = NULL;
17626+ err = au_xino_read(sb, bindex, h_ino, &ino);
17627+ if (!err && ino)
17628+ inode = ilookup(sb, ino);
17629+ if (!inode)
17630+ goto out;
17631+
17632+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17633+ pr_warn("wrong root branch\n");
4a4d8108
AM
17634+ iput(inode);
17635+ inode = NULL;
17636+ goto out;
1308ab2a 17637+ }
17638+
4a4d8108 17639+ ii_write_lock_child(inode);
1308ab2a 17640+
4f0767ce 17641+out:
4a4d8108 17642+ return inode;
dece6358
AM
17643+}
17644+
4a4d8108 17645+static void au_hn_bh(void *_args)
1facf9fc 17646+{
4a4d8108
AM
17647+ struct au_hnotify_args *a = _args;
17648+ struct super_block *sb;
5afbbe0d 17649+ aufs_bindex_t bindex, bbot, bfound;
4a4d8108 17650+ unsigned char xino, try_iput;
1facf9fc 17651+ int err;
1308ab2a 17652+ struct inode *inode;
4a4d8108
AM
17653+ ino_t h_ino;
17654+ struct hn_job_args args;
17655+ struct dentry *dentry;
17656+ struct au_sbinfo *sbinfo;
1facf9fc 17657+
4a4d8108
AM
17658+ AuDebugOn(!_args);
17659+ AuDebugOn(!a->h_dir);
17660+ AuDebugOn(!a->dir);
17661+ AuDebugOn(!a->mask);
17662+ AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
17663+ a->mask, a->dir->i_ino, a->h_dir->i_ino,
17664+ a->h_child_inode ? a->h_child_inode->i_ino : 0);
1facf9fc 17665+
4a4d8108
AM
17666+ inode = NULL;
17667+ dentry = NULL;
17668+ /*
17669+ * do not lock a->dir->i_mutex here
17670+ * because of d_revalidate() may cause a deadlock.
17671+ */
17672+ sb = a->dir->i_sb;
17673+ AuDebugOn(!sb);
17674+ sbinfo = au_sbi(sb);
17675+ AuDebugOn(!sbinfo);
7f207e10 17676+ si_write_lock(sb, AuLock_NOPLMW);
1facf9fc 17677+
8b6a4947
AM
17678+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
17679+ switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
17680+ case FS_MOVED_FROM:
17681+ case FS_MOVED_TO:
17682+ AuWarn1("DIRREN with UDBA may not work correctly "
17683+ "for the direct rename(2)\n");
17684+ }
17685+
4a4d8108
AM
17686+ ii_read_lock_parent(a->dir);
17687+ bfound = -1;
5afbbe0d
AM
17688+ bbot = au_ibbot(a->dir);
17689+ for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
4a4d8108
AM
17690+ if (au_h_iptr(a->dir, bindex) == a->h_dir) {
17691+ bfound = bindex;
17692+ break;
17693+ }
17694+ ii_read_unlock(a->dir);
17695+ if (unlikely(bfound < 0))
17696+ goto out;
1facf9fc 17697+
4a4d8108
AM
17698+ xino = !!au_opt_test(au_mntflags(sb), XINO);
17699+ h_ino = 0;
17700+ if (a->h_child_inode)
17701+ h_ino = a->h_child_inode->i_ino;
1facf9fc 17702+
4a4d8108
AM
17703+ if (a->h_child_nlen
17704+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
17705+ || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
17706+ dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
17707+ a->dir);
17708+ try_iput = 0;
5527c038
JR
17709+ if (dentry && d_really_is_positive(dentry))
17710+ inode = d_inode(dentry);
4a4d8108
AM
17711+ if (xino && !inode && h_ino
17712+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
17713+ || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
17714+ || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
17715+ inode = lookup_wlock_by_ino(sb, bfound, h_ino);
17716+ try_iput = 1;
f0c0a007 17717+ }
1facf9fc 17718+
4a4d8108
AM
17719+ args.flags = a->flags[AuHn_CHILD];
17720+ args.dentry = dentry;
17721+ args.inode = inode;
17722+ args.h_inode = a->h_child_inode;
17723+ args.dir = a->dir;
17724+ args.h_dir = a->h_dir;
17725+ args.h_name = a->h_child_name;
17726+ args.h_nlen = a->h_child_nlen;
17727+ err = hn_job(&args);
17728+ if (dentry) {
027c5e7a 17729+ if (au_di(dentry))
4a4d8108
AM
17730+ di_write_unlock(dentry);
17731+ dput(dentry);
17732+ }
17733+ if (inode && try_iput) {
17734+ ii_write_unlock(inode);
17735+ iput(inode);
17736+ }
1facf9fc 17737+
4a4d8108
AM
17738+ ii_write_lock_parent(a->dir);
17739+ args.flags = a->flags[AuHn_PARENT];
17740+ args.dentry = NULL;
17741+ args.inode = a->dir;
17742+ args.h_inode = a->h_dir;
17743+ args.dir = NULL;
17744+ args.h_dir = NULL;
17745+ args.h_name = NULL;
17746+ args.h_nlen = 0;
17747+ err = hn_job(&args);
17748+ ii_write_unlock(a->dir);
1facf9fc 17749+
4f0767ce 17750+out:
4a4d8108
AM
17751+ iput(a->h_child_inode);
17752+ iput(a->h_dir);
17753+ iput(a->dir);
027c5e7a
AM
17754+ si_write_unlock(sb);
17755+ au_nwt_done(&sbinfo->si_nowait);
1c60b727 17756+ kfree(a);
dece6358 17757+}
1facf9fc 17758+
4a4d8108
AM
17759+/* ---------------------------------------------------------------------- */
17760+
17761+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17762+ struct qstr *h_child_qstr, struct inode *h_child_inode)
dece6358 17763+{
4a4d8108 17764+ int err, len;
53392da6 17765+ unsigned int flags[AuHnLast], f;
4a4d8108
AM
17766+ unsigned char isdir, isroot, wh;
17767+ struct inode *dir;
17768+ struct au_hnotify_args *args;
17769+ char *p, *h_child_name;
dece6358 17770+
1308ab2a 17771+ err = 0;
4a4d8108
AM
17772+ AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
17773+ dir = igrab(hnotify->hn_aufs_inode);
17774+ if (!dir)
17775+ goto out;
1facf9fc 17776+
4a4d8108
AM
17777+ isroot = (dir->i_ino == AUFS_ROOT_INO);
17778+ wh = 0;
17779+ h_child_name = (void *)h_child_qstr->name;
17780+ len = h_child_qstr->len;
17781+ if (h_child_name) {
17782+ if (len > AUFS_WH_PFX_LEN
17783+ && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
17784+ h_child_name += AUFS_WH_PFX_LEN;
17785+ len -= AUFS_WH_PFX_LEN;
17786+ wh = 1;
17787+ }
1facf9fc 17788+ }
dece6358 17789+
4a4d8108
AM
17790+ isdir = 0;
17791+ if (h_child_inode)
17792+ isdir = !!S_ISDIR(h_child_inode->i_mode);
17793+ flags[AuHn_PARENT] = AuHnJob_ISDIR;
17794+ flags[AuHn_CHILD] = 0;
17795+ if (isdir)
17796+ flags[AuHn_CHILD] = AuHnJob_ISDIR;
17797+ au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
17798+ au_fset_hnjob(flags[AuHn_CHILD], GEN);
17799+ switch (mask & FS_EVENTS_POSS_ON_CHILD) {
17800+ case FS_MOVED_FROM:
17801+ case FS_MOVED_TO:
17802+ au_fset_hnjob(flags[AuHn_CHILD], XINO0);
17803+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17804+ /*FALLTHROUGH*/
17805+ case FS_CREATE:
fb47a38f 17806+ AuDebugOn(!h_child_name);
4a4d8108 17807+ break;
1facf9fc 17808+
4a4d8108
AM
17809+ case FS_DELETE:
17810+ /*
17811+ * aufs never be able to get this child inode.
17812+ * revalidation should be in d_revalidate()
17813+ * by checking i_nlink, i_generation or d_unhashed().
17814+ */
17815+ AuDebugOn(!h_child_name);
17816+ au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
17817+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17818+ break;
dece6358 17819+
4a4d8108
AM
17820+ default:
17821+ AuDebugOn(1);
17822+ }
1308ab2a 17823+
4a4d8108
AM
17824+ if (wh)
17825+ h_child_inode = NULL;
1308ab2a 17826+
4a4d8108
AM
17827+ err = -ENOMEM;
17828+ /* iput() and kfree() will be called in au_hnotify() */
4a4d8108 17829+ args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
4a4d8108
AM
17830+ if (unlikely(!args)) {
17831+ AuErr1("no memory\n");
17832+ iput(dir);
17833+ goto out;
17834+ }
17835+ args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
17836+ args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
17837+ args->mask = mask;
17838+ args->dir = dir;
17839+ args->h_dir = igrab(h_dir);
17840+ if (h_child_inode)
17841+ h_child_inode = igrab(h_child_inode); /* can be NULL */
17842+ args->h_child_inode = h_child_inode;
17843+ args->h_child_nlen = len;
17844+ if (len) {
17845+ p = (void *)args;
17846+ p += sizeof(*args);
17847+ memcpy(p, h_child_name, len);
17848+ p[len] = 0;
1308ab2a 17849+ }
1308ab2a 17850+
38d290e6 17851+ /* NFS fires the event for silly-renamed one from kworker */
53392da6 17852+ f = 0;
38d290e6
JR
17853+ if (!dir->i_nlink
17854+ || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
53392da6
AM
17855+ f = AuWkq_NEST;
17856+ err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
4a4d8108
AM
17857+ if (unlikely(err)) {
17858+ pr_err("wkq %d\n", err);
17859+ iput(args->h_child_inode);
17860+ iput(args->h_dir);
17861+ iput(args->dir);
1c60b727 17862+ kfree(args);
1facf9fc 17863+ }
1facf9fc 17864+
4a4d8108 17865+out:
1facf9fc 17866+ return err;
17867+}
17868+
027c5e7a
AM
17869+/* ---------------------------------------------------------------------- */
17870+
17871+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
17872+{
17873+ int err;
17874+
17875+ AuDebugOn(!(udba & AuOptMask_UDBA));
17876+
17877+ err = 0;
17878+ if (au_hnotify_op.reset_br)
17879+ err = au_hnotify_op.reset_br(udba, br, perm);
17880+
17881+ return err;
17882+}
17883+
17884+int au_hnotify_init_br(struct au_branch *br, int perm)
17885+{
17886+ int err;
17887+
17888+ err = 0;
17889+ if (au_hnotify_op.init_br)
17890+ err = au_hnotify_op.init_br(br, perm);
17891+
17892+ return err;
17893+}
17894+
17895+void au_hnotify_fin_br(struct au_branch *br)
17896+{
17897+ if (au_hnotify_op.fin_br)
17898+ au_hnotify_op.fin_br(br);
17899+}
17900+
4a4d8108
AM
17901+static void au_hn_destroy_cache(void)
17902+{
1c60b727
AM
17903+ kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
17904+ au_cache[AuCache_HNOTIFY] = NULL;
4a4d8108 17905+}
1308ab2a 17906+
4a4d8108 17907+int __init au_hnotify_init(void)
1facf9fc 17908+{
1308ab2a 17909+ int err;
1308ab2a 17910+
4a4d8108 17911+ err = -ENOMEM;
1c60b727
AM
17912+ au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
17913+ if (au_cache[AuCache_HNOTIFY]) {
027c5e7a
AM
17914+ err = 0;
17915+ if (au_hnotify_op.init)
17916+ err = au_hnotify_op.init();
4a4d8108
AM
17917+ if (unlikely(err))
17918+ au_hn_destroy_cache();
1308ab2a 17919+ }
1308ab2a 17920+ AuTraceErr(err);
4a4d8108 17921+ return err;
1308ab2a 17922+}
17923+
4a4d8108 17924+void au_hnotify_fin(void)
1308ab2a 17925+{
027c5e7a
AM
17926+ if (au_hnotify_op.fin)
17927+ au_hnotify_op.fin();
f0c0a007 17928+
4a4d8108 17929+ /* cf. au_cache_fin() */
1c60b727 17930+ if (au_cache[AuCache_HNOTIFY])
4a4d8108 17931+ au_hn_destroy_cache();
dece6358 17932+}
7f207e10
AM
17933diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
17934--- /usr/share/empty/fs/aufs/iinfo.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 17935+++ linux/fs/aufs/iinfo.c 2018-08-12 23:43:05.460124736 +0200
062440b3 17936@@ -0,0 +1,286 @@
cd7a4cd9 17937+// SPDX-License-Identifier: GPL-2.0
dece6358 17938+/*
b00004a5 17939+ * Copyright (C) 2005-2018 Junjiro R. Okajima
dece6358
AM
17940+ *
17941+ * This program, aufs is free software; you can redistribute it and/or modify
17942+ * it under the terms of the GNU General Public License as published by
17943+ * the Free Software Foundation; either version 2 of the License, or
17944+ * (at your option) any later version.
17945+ *
17946+ * This program is distributed in the hope that it will be useful,
17947+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17948+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17949+ * GNU General Public License for more details.
17950+ *
17951+ * You should have received a copy of the GNU General Public License
523b37e3 17952+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 17953+ */
1facf9fc 17954+
dece6358 17955+/*
4a4d8108 17956+ * inode private data
dece6358 17957+ */
1facf9fc 17958+
1308ab2a 17959+#include "aufs.h"
1facf9fc 17960+
4a4d8108 17961+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 17962+{
4a4d8108 17963+ struct inode *h_inode;
5afbbe0d 17964+ struct au_hinode *hinode;
1facf9fc 17965+
4a4d8108 17966+ IiMustAnyLock(inode);
1facf9fc 17967+
5afbbe0d
AM
17968+ hinode = au_hinode(au_ii(inode), bindex);
17969+ h_inode = hinode->hi_inode;
4a4d8108
AM
17970+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17971+ return h_inode;
17972+}
1facf9fc 17973+
4a4d8108
AM
17974+/* todo: hard/soft set? */
17975+void au_hiput(struct au_hinode *hinode)
17976+{
17977+ au_hn_free(hinode);
17978+ dput(hinode->hi_whdentry);
17979+ iput(hinode->hi_inode);
17980+}
1facf9fc 17981+
4a4d8108
AM
17982+unsigned int au_hi_flags(struct inode *inode, int isdir)
17983+{
17984+ unsigned int flags;
17985+ const unsigned int mnt_flags = au_mntflags(inode->i_sb);
1facf9fc 17986+
4a4d8108
AM
17987+ flags = 0;
17988+ if (au_opt_test(mnt_flags, XINO))
17989+ au_fset_hi(flags, XINO);
17990+ if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
17991+ au_fset_hi(flags, HNOTIFY);
17992+ return flags;
1facf9fc 17993+}
17994+
4a4d8108
AM
17995+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
17996+ struct inode *h_inode, unsigned int flags)
1308ab2a 17997+{
4a4d8108
AM
17998+ struct au_hinode *hinode;
17999+ struct inode *hi;
18000+ struct au_iinfo *iinfo = au_ii(inode);
1facf9fc 18001+
4a4d8108 18002+ IiMustWriteLock(inode);
dece6358 18003+
5afbbe0d 18004+ hinode = au_hinode(iinfo, bindex);
4a4d8108
AM
18005+ hi = hinode->hi_inode;
18006+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
18007+
18008+ if (hi)
18009+ au_hiput(hinode);
18010+ hinode->hi_inode = h_inode;
18011+ if (h_inode) {
18012+ int err;
18013+ struct super_block *sb = inode->i_sb;
18014+ struct au_branch *br;
18015+
027c5e7a
AM
18016+ AuDebugOn(inode->i_mode
18017+ && (h_inode->i_mode & S_IFMT)
18018+ != (inode->i_mode & S_IFMT));
5afbbe0d 18019+ if (bindex == iinfo->ii_btop)
4a4d8108
AM
18020+ au_cpup_igen(inode, h_inode);
18021+ br = au_sbr(sb, bindex);
18022+ hinode->hi_id = br->br_id;
18023+ if (au_ftest_hi(flags, XINO)) {
18024+ err = au_xino_write(sb, bindex, h_inode->i_ino,
18025+ inode->i_ino);
18026+ if (unlikely(err))
18027+ AuIOErr1("failed au_xino_write() %d\n", err);
18028+ }
18029+
18030+ if (au_ftest_hi(flags, HNOTIFY)
18031+ && au_br_hnotifyable(br->br_perm)) {
027c5e7a 18032+ err = au_hn_alloc(hinode, inode);
4a4d8108
AM
18033+ if (unlikely(err))
18034+ AuIOErr1("au_hn_alloc() %d\n", err);
1308ab2a 18035+ }
18036+ }
4a4d8108 18037+}
dece6358 18038+
4a4d8108
AM
18039+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
18040+ struct dentry *h_wh)
18041+{
18042+ struct au_hinode *hinode;
dece6358 18043+
4a4d8108
AM
18044+ IiMustWriteLock(inode);
18045+
5afbbe0d 18046+ hinode = au_hinode(au_ii(inode), bindex);
4a4d8108
AM
18047+ AuDebugOn(hinode->hi_whdentry);
18048+ hinode->hi_whdentry = h_wh;
1facf9fc 18049+}
18050+
537831f9 18051+void au_update_iigen(struct inode *inode, int half)
1308ab2a 18052+{
537831f9
AM
18053+ struct au_iinfo *iinfo;
18054+ struct au_iigen *iigen;
18055+ unsigned int sigen;
18056+
18057+ sigen = au_sigen(inode->i_sb);
18058+ iinfo = au_ii(inode);
18059+ iigen = &iinfo->ii_generation;
be52b249 18060+ spin_lock(&iigen->ig_spin);
537831f9
AM
18061+ iigen->ig_generation = sigen;
18062+ if (half)
18063+ au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
18064+ else
18065+ au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
be52b249 18066+ spin_unlock(&iigen->ig_spin);
4a4d8108 18067+}
1facf9fc 18068+
4a4d8108
AM
18069+/* it may be called at remount time, too */
18070+void au_update_ibrange(struct inode *inode, int do_put_zero)
18071+{
18072+ struct au_iinfo *iinfo;
5afbbe0d 18073+ aufs_bindex_t bindex, bbot;
1facf9fc 18074+
5afbbe0d 18075+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 18076+ IiMustWriteLock(inode);
1facf9fc 18077+
5afbbe0d
AM
18078+ iinfo = au_ii(inode);
18079+ if (do_put_zero && iinfo->ii_btop >= 0) {
18080+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
18081+ bindex++) {
18082+ struct inode *h_i;
1facf9fc 18083+
5afbbe0d 18084+ h_i = au_hinode(iinfo, bindex)->hi_inode;
38d290e6
JR
18085+ if (h_i
18086+ && !h_i->i_nlink
18087+ && !(h_i->i_state & I_LINKABLE))
027c5e7a
AM
18088+ au_set_h_iptr(inode, bindex, NULL, 0);
18089+ }
4a4d8108
AM
18090+ }
18091+
5afbbe0d
AM
18092+ iinfo->ii_btop = -1;
18093+ iinfo->ii_bbot = -1;
18094+ bbot = au_sbbot(inode->i_sb);
18095+ for (bindex = 0; bindex <= bbot; bindex++)
18096+ if (au_hinode(iinfo, bindex)->hi_inode) {
18097+ iinfo->ii_btop = bindex;
4a4d8108 18098+ break;
027c5e7a 18099+ }
5afbbe0d
AM
18100+ if (iinfo->ii_btop >= 0)
18101+ for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
18102+ if (au_hinode(iinfo, bindex)->hi_inode) {
18103+ iinfo->ii_bbot = bindex;
027c5e7a
AM
18104+ break;
18105+ }
5afbbe0d 18106+ AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
1308ab2a 18107+}
1facf9fc 18108+
dece6358 18109+/* ---------------------------------------------------------------------- */
1facf9fc 18110+
4a4d8108 18111+void au_icntnr_init_once(void *_c)
dece6358 18112+{
4a4d8108
AM
18113+ struct au_icntnr *c = _c;
18114+ struct au_iinfo *iinfo = &c->iinfo;
1facf9fc 18115+
be52b249 18116+ spin_lock_init(&iinfo->ii_generation.ig_spin);
4a4d8108
AM
18117+ au_rw_init(&iinfo->ii_rwsem);
18118+ inode_init_once(&c->vfs_inode);
18119+}
1facf9fc 18120+
5afbbe0d
AM
18121+void au_hinode_init(struct au_hinode *hinode)
18122+{
18123+ hinode->hi_inode = NULL;
18124+ hinode->hi_id = -1;
18125+ au_hn_init(hinode);
18126+ hinode->hi_whdentry = NULL;
18127+}
18128+
4a4d8108
AM
18129+int au_iinfo_init(struct inode *inode)
18130+{
18131+ struct au_iinfo *iinfo;
18132+ struct super_block *sb;
5afbbe0d 18133+ struct au_hinode *hi;
4a4d8108 18134+ int nbr, i;
1facf9fc 18135+
4a4d8108
AM
18136+ sb = inode->i_sb;
18137+ iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
5afbbe0d 18138+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
18139+ if (unlikely(nbr <= 0))
18140+ nbr = 1;
5afbbe0d
AM
18141+ hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
18142+ if (hi) {
7f207e10 18143+ au_ninodes_inc(sb);
5afbbe0d
AM
18144+
18145+ iinfo->ii_hinode = hi;
18146+ for (i = 0; i < nbr; i++, hi++)
18147+ au_hinode_init(hi);
1facf9fc 18148+
537831f9 18149+ iinfo->ii_generation.ig_generation = au_sigen(sb);
5afbbe0d
AM
18150+ iinfo->ii_btop = -1;
18151+ iinfo->ii_bbot = -1;
4a4d8108
AM
18152+ iinfo->ii_vdir = NULL;
18153+ return 0;
1308ab2a 18154+ }
4a4d8108
AM
18155+ return -ENOMEM;
18156+}
1facf9fc 18157+
e2f27e51 18158+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
4a4d8108 18159+{
5afbbe0d 18160+ int err, i;
4a4d8108 18161+ struct au_hinode *hip;
1facf9fc 18162+
4a4d8108
AM
18163+ AuRwMustWriteLock(&iinfo->ii_rwsem);
18164+
18165+ err = -ENOMEM;
e2f27e51
AM
18166+ hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
18167+ may_shrink);
4a4d8108
AM
18168+ if (hip) {
18169+ iinfo->ii_hinode = hip;
5afbbe0d
AM
18170+ i = iinfo->ii_bbot + 1;
18171+ hip += i;
18172+ for (; i < nbr; i++, hip++)
18173+ au_hinode_init(hip);
4a4d8108 18174+ err = 0;
1308ab2a 18175+ }
4a4d8108 18176+
1308ab2a 18177+ return err;
1facf9fc 18178+}
18179+
4a4d8108 18180+void au_iinfo_fin(struct inode *inode)
1facf9fc 18181+{
4a4d8108
AM
18182+ struct au_iinfo *iinfo;
18183+ struct au_hinode *hi;
18184+ struct super_block *sb;
5afbbe0d 18185+ aufs_bindex_t bindex, bbot;
b752ccd1 18186+ const unsigned char unlinked = !inode->i_nlink;
1308ab2a 18187+
5afbbe0d 18188+ AuDebugOn(au_is_bad_inode(inode));
1308ab2a 18189+
b752ccd1 18190+ sb = inode->i_sb;
7f207e10 18191+ au_ninodes_dec(sb);
b752ccd1
AM
18192+ if (si_pid_test(sb))
18193+ au_xino_delete_inode(inode, unlinked);
18194+ else {
18195+ /*
18196+ * it is safe to hide the dependency between sbinfo and
18197+ * sb->s_umount.
18198+ */
18199+ lockdep_off();
18200+ si_noflush_read_lock(sb);
18201+ au_xino_delete_inode(inode, unlinked);
18202+ si_read_unlock(sb);
18203+ lockdep_on();
18204+ }
18205+
5afbbe0d 18206+ iinfo = au_ii(inode);
4a4d8108 18207+ if (iinfo->ii_vdir)
1c60b727 18208+ au_vdir_free(iinfo->ii_vdir);
1308ab2a 18209+
5afbbe0d 18210+ bindex = iinfo->ii_btop;
b752ccd1 18211+ if (bindex >= 0) {
5afbbe0d
AM
18212+ hi = au_hinode(iinfo, bindex);
18213+ bbot = iinfo->ii_bbot;
18214+ while (bindex++ <= bbot) {
b752ccd1 18215+ if (hi->hi_inode)
4a4d8108 18216+ au_hiput(hi);
4a4d8108
AM
18217+ hi++;
18218+ }
18219+ }
1c60b727 18220+ kfree(iinfo->ii_hinode);
4a4d8108 18221+ AuRwDestroy(&iinfo->ii_rwsem);
dece6358 18222+}
7f207e10
AM
18223diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
18224--- /usr/share/empty/fs/aufs/inode.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 18225+++ linux/fs/aufs/inode.c 2018-08-12 23:43:05.460124736 +0200
062440b3 18226@@ -0,0 +1,528 @@
cd7a4cd9 18227+// SPDX-License-Identifier: GPL-2.0
4a4d8108 18228+/*
b00004a5 18229+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
18230+ *
18231+ * This program, aufs is free software; you can redistribute it and/or modify
18232+ * it under the terms of the GNU General Public License as published by
18233+ * the Free Software Foundation; either version 2 of the License, or
18234+ * (at your option) any later version.
18235+ *
18236+ * This program is distributed in the hope that it will be useful,
18237+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18238+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18239+ * GNU General Public License for more details.
18240+ *
18241+ * You should have received a copy of the GNU General Public License
523b37e3 18242+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 18243+ */
1facf9fc 18244+
4a4d8108
AM
18245+/*
18246+ * inode functions
18247+ */
1facf9fc 18248+
4a4d8108 18249+#include "aufs.h"
1308ab2a 18250+
4a4d8108
AM
18251+struct inode *au_igrab(struct inode *inode)
18252+{
18253+ if (inode) {
18254+ AuDebugOn(!atomic_read(&inode->i_count));
027c5e7a 18255+ ihold(inode);
1facf9fc 18256+ }
4a4d8108
AM
18257+ return inode;
18258+}
1facf9fc 18259+
4a4d8108
AM
18260+static void au_refresh_hinode_attr(struct inode *inode, int do_version)
18261+{
18262+ au_cpup_attr_all(inode, /*force*/0);
537831f9 18263+ au_update_iigen(inode, /*half*/1);
4a4d8108 18264+ if (do_version)
be118d29 18265+ inode_inc_iversion(inode);
dece6358 18266+}
1facf9fc 18267+
027c5e7a 18268+static int au_ii_refresh(struct inode *inode, int *update)
dece6358 18269+{
e2f27e51 18270+ int err, e, nbr;
027c5e7a 18271+ umode_t type;
4a4d8108 18272+ aufs_bindex_t bindex, new_bindex;
1308ab2a 18273+ struct super_block *sb;
4a4d8108 18274+ struct au_iinfo *iinfo;
027c5e7a 18275+ struct au_hinode *p, *q, tmp;
1facf9fc 18276+
5afbbe0d 18277+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 18278+ IiMustWriteLock(inode);
1facf9fc 18279+
027c5e7a 18280+ *update = 0;
4a4d8108 18281+ sb = inode->i_sb;
e2f27e51 18282+ nbr = au_sbbot(sb) + 1;
027c5e7a 18283+ type = inode->i_mode & S_IFMT;
4a4d8108 18284+ iinfo = au_ii(inode);
e2f27e51 18285+ err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
4a4d8108 18286+ if (unlikely(err))
1308ab2a 18287+ goto out;
1facf9fc 18288+
5afbbe0d
AM
18289+ AuDebugOn(iinfo->ii_btop < 0);
18290+ p = au_hinode(iinfo, iinfo->ii_btop);
18291+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
18292+ bindex++, p++) {
18293+ if (!p->hi_inode)
18294+ continue;
1facf9fc 18295+
027c5e7a 18296+ AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
4a4d8108
AM
18297+ new_bindex = au_br_index(sb, p->hi_id);
18298+ if (new_bindex == bindex)
18299+ continue;
1facf9fc 18300+
4a4d8108 18301+ if (new_bindex < 0) {
027c5e7a 18302+ *update = 1;
4a4d8108
AM
18303+ au_hiput(p);
18304+ p->hi_inode = NULL;
18305+ continue;
1308ab2a 18306+ }
4a4d8108 18307+
5afbbe0d
AM
18308+ if (new_bindex < iinfo->ii_btop)
18309+ iinfo->ii_btop = new_bindex;
18310+ if (iinfo->ii_bbot < new_bindex)
18311+ iinfo->ii_bbot = new_bindex;
4a4d8108 18312+ /* swap two lower inode, and loop again */
5afbbe0d 18313+ q = au_hinode(iinfo, new_bindex);
4a4d8108
AM
18314+ tmp = *q;
18315+ *q = *p;
18316+ *p = tmp;
18317+ if (tmp.hi_inode) {
18318+ bindex--;
18319+ p--;
1308ab2a 18320+ }
18321+ }
4a4d8108 18322+ au_update_ibrange(inode, /*do_put_zero*/0);
e2f27e51 18323+ au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
4a4d8108
AM
18324+ e = au_dy_irefresh(inode);
18325+ if (unlikely(e && !err))
18326+ err = e;
1facf9fc 18327+
4f0767ce 18328+out:
027c5e7a
AM
18329+ AuTraceErr(err);
18330+ return err;
18331+}
18332+
b95c5147
AM
18333+void au_refresh_iop(struct inode *inode, int force_getattr)
18334+{
18335+ int type;
18336+ struct au_sbinfo *sbi = au_sbi(inode->i_sb);
18337+ const struct inode_operations *iop
18338+ = force_getattr ? aufs_iop : sbi->si_iop_array;
18339+
18340+ if (inode->i_op == iop)
18341+ return;
18342+
18343+ switch (inode->i_mode & S_IFMT) {
18344+ case S_IFDIR:
18345+ type = AuIop_DIR;
18346+ break;
18347+ case S_IFLNK:
18348+ type = AuIop_SYMLINK;
18349+ break;
18350+ default:
18351+ type = AuIop_OTHER;
18352+ break;
18353+ }
18354+
18355+ inode->i_op = iop + type;
18356+ /* unnecessary smp_wmb() */
18357+}
18358+
027c5e7a
AM
18359+int au_refresh_hinode_self(struct inode *inode)
18360+{
18361+ int err, update;
18362+
18363+ err = au_ii_refresh(inode, &update);
18364+ if (!err)
18365+ au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
18366+
18367+ AuTraceErr(err);
4a4d8108
AM
18368+ return err;
18369+}
1facf9fc 18370+
4a4d8108
AM
18371+int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
18372+{
027c5e7a 18373+ int err, e, update;
4a4d8108 18374+ unsigned int flags;
027c5e7a 18375+ umode_t mode;
5afbbe0d 18376+ aufs_bindex_t bindex, bbot;
027c5e7a 18377+ unsigned char isdir;
4a4d8108
AM
18378+ struct au_hinode *p;
18379+ struct au_iinfo *iinfo;
1facf9fc 18380+
027c5e7a 18381+ err = au_ii_refresh(inode, &update);
4a4d8108
AM
18382+ if (unlikely(err))
18383+ goto out;
18384+
18385+ update = 0;
18386+ iinfo = au_ii(inode);
5afbbe0d 18387+ p = au_hinode(iinfo, iinfo->ii_btop);
027c5e7a
AM
18388+ mode = (inode->i_mode & S_IFMT);
18389+ isdir = S_ISDIR(mode);
4a4d8108 18390+ flags = au_hi_flags(inode, isdir);
5afbbe0d
AM
18391+ bbot = au_dbbot(dentry);
18392+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
5527c038 18393+ struct inode *h_i, *h_inode;
4a4d8108
AM
18394+ struct dentry *h_d;
18395+
18396+ h_d = au_h_dptr(dentry, bindex);
5527c038 18397+ if (!h_d || d_is_negative(h_d))
4a4d8108
AM
18398+ continue;
18399+
5527c038
JR
18400+ h_inode = d_inode(h_d);
18401+ AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
5afbbe0d 18402+ if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
4a4d8108
AM
18403+ h_i = au_h_iptr(inode, bindex);
18404+ if (h_i) {
5527c038 18405+ if (h_i == h_inode)
4a4d8108
AM
18406+ continue;
18407+ err = -EIO;
18408+ break;
18409+ }
18410+ }
5afbbe0d
AM
18411+ if (bindex < iinfo->ii_btop)
18412+ iinfo->ii_btop = bindex;
18413+ if (iinfo->ii_bbot < bindex)
18414+ iinfo->ii_bbot = bindex;
5527c038 18415+ au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
4a4d8108 18416+ update = 1;
1308ab2a 18417+ }
4a4d8108
AM
18418+ au_update_ibrange(inode, /*do_put_zero*/0);
18419+ e = au_dy_irefresh(inode);
18420+ if (unlikely(e && !err))
18421+ err = e;
027c5e7a
AM
18422+ if (!err)
18423+ au_refresh_hinode_attr(inode, update && isdir);
4a4d8108 18424+
4f0767ce 18425+out:
4a4d8108 18426+ AuTraceErr(err);
1308ab2a 18427+ return err;
dece6358
AM
18428+}
18429+
4a4d8108 18430+static int set_inode(struct inode *inode, struct dentry *dentry)
dece6358 18431+{
4a4d8108
AM
18432+ int err;
18433+ unsigned int flags;
18434+ umode_t mode;
5afbbe0d 18435+ aufs_bindex_t bindex, btop, btail;
4a4d8108
AM
18436+ unsigned char isdir;
18437+ struct dentry *h_dentry;
18438+ struct inode *h_inode;
18439+ struct au_iinfo *iinfo;
b95c5147 18440+ struct inode_operations *iop;
dece6358 18441+
4a4d8108 18442+ IiMustWriteLock(inode);
dece6358 18443+
4a4d8108
AM
18444+ err = 0;
18445+ isdir = 0;
b95c5147 18446+ iop = au_sbi(inode->i_sb)->si_iop_array;
5afbbe0d
AM
18447+ btop = au_dbtop(dentry);
18448+ h_dentry = au_h_dptr(dentry, btop);
5527c038 18449+ h_inode = d_inode(h_dentry);
4a4d8108
AM
18450+ mode = h_inode->i_mode;
18451+ switch (mode & S_IFMT) {
18452+ case S_IFREG:
18453+ btail = au_dbtail(dentry);
b95c5147 18454+ inode->i_op = iop + AuIop_OTHER;
4a4d8108 18455+ inode->i_fop = &aufs_file_fop;
5afbbe0d 18456+ err = au_dy_iaop(inode, btop, h_inode);
4a4d8108
AM
18457+ if (unlikely(err))
18458+ goto out;
18459+ break;
18460+ case S_IFDIR:
18461+ isdir = 1;
18462+ btail = au_dbtaildir(dentry);
b95c5147 18463+ inode->i_op = iop + AuIop_DIR;
4a4d8108
AM
18464+ inode->i_fop = &aufs_dir_fop;
18465+ break;
18466+ case S_IFLNK:
18467+ btail = au_dbtail(dentry);
b95c5147 18468+ inode->i_op = iop + AuIop_SYMLINK;
4a4d8108
AM
18469+ break;
18470+ case S_IFBLK:
18471+ case S_IFCHR:
18472+ case S_IFIFO:
18473+ case S_IFSOCK:
18474+ btail = au_dbtail(dentry);
b95c5147 18475+ inode->i_op = iop + AuIop_OTHER;
38d290e6 18476+ init_special_inode(inode, mode, h_inode->i_rdev);
4a4d8108
AM
18477+ break;
18478+ default:
18479+ AuIOErr("Unknown file type 0%o\n", mode);
18480+ err = -EIO;
1308ab2a 18481+ goto out;
4a4d8108 18482+ }
dece6358 18483+
4a4d8108
AM
18484+ /* do not set hnotify for whiteouted dirs (SHWH mode) */
18485+ flags = au_hi_flags(inode, isdir);
18486+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
18487+ && au_ftest_hi(flags, HNOTIFY)
18488+ && dentry->d_name.len > AUFS_WH_PFX_LEN
18489+ && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
18490+ au_fclr_hi(flags, HNOTIFY);
18491+ iinfo = au_ii(inode);
5afbbe0d
AM
18492+ iinfo->ii_btop = btop;
18493+ iinfo->ii_bbot = btail;
18494+ for (bindex = btop; bindex <= btail; bindex++) {
4a4d8108
AM
18495+ h_dentry = au_h_dptr(dentry, bindex);
18496+ if (h_dentry)
18497+ au_set_h_iptr(inode, bindex,
5527c038 18498+ au_igrab(d_inode(h_dentry)), flags);
4a4d8108
AM
18499+ }
18500+ au_cpup_attr_all(inode, /*force*/1);
c1595e42
JR
18501+ /*
18502+ * to force calling aufs_get_acl() every time,
18503+ * do not call cache_no_acl() for aufs inode.
18504+ */
dece6358 18505+
4f0767ce 18506+out:
4a4d8108
AM
18507+ return err;
18508+}
dece6358 18509+
027c5e7a
AM
18510+/*
18511+ * successful returns with iinfo write_locked
18512+ * minus: errno
18513+ * zero: success, matched
18514+ * plus: no error, but unmatched
18515+ */
18516+static int reval_inode(struct inode *inode, struct dentry *dentry)
4a4d8108
AM
18517+{
18518+ int err;
cfc41e69 18519+ unsigned int gen, igflags;
5afbbe0d 18520+ aufs_bindex_t bindex, bbot;
4a4d8108 18521+ struct inode *h_inode, *h_dinode;
5527c038 18522+ struct dentry *h_dentry;
dece6358 18523+
4a4d8108
AM
18524+ /*
18525+ * before this function, if aufs got any iinfo lock, it must be only
18526+ * one, the parent dir.
18527+ * it can happen by UDBA and the obsoleted inode number.
18528+ */
18529+ err = -EIO;
18530+ if (unlikely(inode->i_ino == parent_ino(dentry)))
18531+ goto out;
18532+
027c5e7a 18533+ err = 1;
4a4d8108 18534+ ii_write_lock_new_child(inode);
5afbbe0d 18535+ h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
5527c038 18536+ h_dinode = d_inode(h_dentry);
5afbbe0d
AM
18537+ bbot = au_ibbot(inode);
18538+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108 18539+ h_inode = au_h_iptr(inode, bindex);
537831f9
AM
18540+ if (!h_inode || h_inode != h_dinode)
18541+ continue;
18542+
18543+ err = 0;
cfc41e69 18544+ gen = au_iigen(inode, &igflags);
537831f9 18545+ if (gen == au_digen(dentry)
cfc41e69 18546+ && !au_ig_ftest(igflags, HALF_REFRESHED))
4a4d8108 18547+ break;
537831f9
AM
18548+
18549+ /* fully refresh inode using dentry */
18550+ err = au_refresh_hinode(inode, dentry);
18551+ if (!err)
18552+ au_update_iigen(inode, /*half*/0);
18553+ break;
1facf9fc 18554+ }
dece6358 18555+
4a4d8108
AM
18556+ if (unlikely(err))
18557+ ii_write_unlock(inode);
4f0767ce 18558+out:
1facf9fc 18559+ return err;
18560+}
1facf9fc 18561+
4a4d8108
AM
18562+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18563+ unsigned int d_type, ino_t *ino)
1facf9fc 18564+{
521ced18
JR
18565+ int err, idx;
18566+ const int isnondir = d_type != DT_DIR;
1facf9fc 18567+
b752ccd1 18568+ /* prevent hardlinked inode number from race condition */
521ced18
JR
18569+ if (isnondir) {
18570+ err = au_xinondir_enter(sb, bindex, h_ino, &idx);
18571+ if (unlikely(err))
18572+ goto out;
4a4d8108 18573+ }
521ced18 18574+
4a4d8108
AM
18575+ err = au_xino_read(sb, bindex, h_ino, ino);
18576+ if (unlikely(err))
521ced18 18577+ goto out_xinondir;
1308ab2a 18578+
4a4d8108
AM
18579+ if (!*ino) {
18580+ err = -EIO;
18581+ *ino = au_xino_new_ino(sb);
18582+ if (unlikely(!*ino))
521ced18 18583+ goto out_xinondir;
4a4d8108
AM
18584+ err = au_xino_write(sb, bindex, h_ino, *ino);
18585+ if (unlikely(err))
521ced18 18586+ goto out_xinondir;
1308ab2a 18587+ }
1facf9fc 18588+
521ced18
JR
18589+out_xinondir:
18590+ if (isnondir && idx >= 0)
18591+ au_xinondir_leave(sb, bindex, h_ino, idx);
4f0767ce 18592+out:
1facf9fc 18593+ return err;
18594+}
18595+
4a4d8108
AM
18596+/* successful returns with iinfo write_locked */
18597+/* todo: return with unlocked? */
18598+struct inode *au_new_inode(struct dentry *dentry, int must_new)
1facf9fc 18599+{
5527c038 18600+ struct inode *inode, *h_inode;
4a4d8108
AM
18601+ struct dentry *h_dentry;
18602+ struct super_block *sb;
18603+ ino_t h_ino, ino;
521ced18 18604+ int err, idx, hlinked;
5afbbe0d 18605+ aufs_bindex_t btop;
1facf9fc 18606+
4a4d8108 18607+ sb = dentry->d_sb;
5afbbe0d
AM
18608+ btop = au_dbtop(dentry);
18609+ h_dentry = au_h_dptr(dentry, btop);
5527c038
JR
18610+ h_inode = d_inode(h_dentry);
18611+ h_ino = h_inode->i_ino;
521ced18 18612+ hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
b752ccd1 18613+
521ced18 18614+new_ino:
b752ccd1
AM
18615+ /*
18616+ * stop 'race'-ing between hardlinks under different
18617+ * parents.
18618+ */
521ced18
JR
18619+ if (hlinked) {
18620+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18621+ inode = ERR_PTR(err);
18622+ if (unlikely(err))
18623+ goto out;
18624+ }
b752ccd1 18625+
5afbbe0d 18626+ err = au_xino_read(sb, btop, h_ino, &ino);
4a4d8108
AM
18627+ inode = ERR_PTR(err);
18628+ if (unlikely(err))
521ced18 18629+ goto out_xinondir;
b752ccd1 18630+
4a4d8108
AM
18631+ if (!ino) {
18632+ ino = au_xino_new_ino(sb);
18633+ if (unlikely(!ino)) {
18634+ inode = ERR_PTR(-EIO);
521ced18 18635+ goto out_xinondir;
dece6358
AM
18636+ }
18637+ }
1facf9fc 18638+
4a4d8108
AM
18639+ AuDbg("i%lu\n", (unsigned long)ino);
18640+ inode = au_iget_locked(sb, ino);
18641+ err = PTR_ERR(inode);
18642+ if (IS_ERR(inode))
521ced18 18643+ goto out_xinondir;
1facf9fc 18644+
4a4d8108
AM
18645+ AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
18646+ if (inode->i_state & I_NEW) {
18647+ ii_write_lock_new_child(inode);
18648+ err = set_inode(inode, dentry);
18649+ if (!err) {
18650+ unlock_new_inode(inode);
521ced18 18651+ goto out_xinondir; /* success */
4a4d8108 18652+ }
1308ab2a 18653+
027c5e7a
AM
18654+ /*
18655+ * iget_failed() calls iput(), but we need to call
18656+ * ii_write_unlock() after iget_failed(). so dirty hack for
18657+ * i_count.
18658+ */
18659+ atomic_inc(&inode->i_count);
4a4d8108 18660+ iget_failed(inode);
027c5e7a 18661+ ii_write_unlock(inode);
5afbbe0d 18662+ au_xino_write(sb, btop, h_ino, /*ino*/0);
027c5e7a
AM
18663+ /* ignore this error */
18664+ goto out_iput;
18665+ } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
b752ccd1
AM
18666+ /*
18667+ * horrible race condition between lookup, readdir and copyup
18668+ * (or something).
18669+ */
521ced18
JR
18670+ if (hlinked && idx >= 0)
18671+ au_xinondir_leave(sb, btop, h_ino, idx);
027c5e7a
AM
18672+ err = reval_inode(inode, dentry);
18673+ if (unlikely(err < 0)) {
521ced18 18674+ hlinked = 0;
027c5e7a
AM
18675+ goto out_iput;
18676+ }
521ced18 18677+ if (!err)
4a4d8108 18678+ goto out; /* success */
521ced18
JR
18679+ else if (hlinked && idx >= 0) {
18680+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18681+ if (unlikely(err)) {
18682+ iput(inode);
18683+ inode = ERR_PTR(err);
18684+ goto out;
18685+ }
18686+ }
4a4d8108
AM
18687+ }
18688+
5527c038 18689+ if (unlikely(au_test_fs_unique_ino(h_inode)))
4a4d8108 18690+ AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
523b37e3 18691+ " b%d, %s, %pd, hi%lu, i%lu.\n",
5afbbe0d 18692+ btop, au_sbtype(h_dentry->d_sb), dentry,
4a4d8108
AM
18693+ (unsigned long)h_ino, (unsigned long)ino);
18694+ ino = 0;
5afbbe0d 18695+ err = au_xino_write(sb, btop, h_ino, /*ino*/0);
4a4d8108
AM
18696+ if (!err) {
18697+ iput(inode);
521ced18
JR
18698+ if (hlinked && idx >= 0)
18699+ au_xinondir_leave(sb, btop, h_ino, idx);
4a4d8108
AM
18700+ goto new_ino;
18701+ }
1308ab2a 18702+
4f0767ce 18703+out_iput:
4a4d8108 18704+ iput(inode);
4a4d8108 18705+ inode = ERR_PTR(err);
521ced18
JR
18706+out_xinondir:
18707+ if (hlinked && idx >= 0)
18708+ au_xinondir_leave(sb, btop, h_ino, idx);
4f0767ce 18709+out:
4a4d8108 18710+ return inode;
1facf9fc 18711+}
18712+
4a4d8108 18713+/* ---------------------------------------------------------------------- */
1facf9fc 18714+
4a4d8108
AM
18715+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18716+ struct inode *inode)
18717+{
18718+ int err;
076b876e 18719+ struct inode *hi;
1facf9fc 18720+
4a4d8108 18721+ err = au_br_rdonly(au_sbr(sb, bindex));
1facf9fc 18722+
4a4d8108
AM
18723+ /* pseudo-link after flushed may happen out of bounds */
18724+ if (!err
18725+ && inode
5afbbe0d
AM
18726+ && au_ibtop(inode) <= bindex
18727+ && bindex <= au_ibbot(inode)) {
4a4d8108
AM
18728+ /*
18729+ * permission check is unnecessary since vfsub routine
18730+ * will be called later
18731+ */
076b876e 18732+ hi = au_h_iptr(inode, bindex);
4a4d8108
AM
18733+ if (hi)
18734+ err = IS_IMMUTABLE(hi) ? -EROFS : 0;
1facf9fc 18735+ }
18736+
4a4d8108
AM
18737+ return err;
18738+}
dece6358 18739+
4a4d8108
AM
18740+int au_test_h_perm(struct inode *h_inode, int mask)
18741+{
2dfbb274 18742+ if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
4a4d8108
AM
18743+ return 0;
18744+ return inode_permission(h_inode, mask);
18745+}
1facf9fc 18746+
4a4d8108
AM
18747+int au_test_h_perm_sio(struct inode *h_inode, int mask)
18748+{
18749+ if (au_test_nfs(h_inode->i_sb)
18750+ && (mask & MAY_WRITE)
18751+ && S_ISDIR(h_inode->i_mode))
18752+ mask |= MAY_READ; /* force permission check */
18753+ return au_test_h_perm(h_inode, mask);
1facf9fc 18754+}
7f207e10
AM
18755diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
18756--- /usr/share/empty/fs/aufs/inode.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 18757+++ linux/fs/aufs/inode.h 2018-08-12 23:43:05.460124736 +0200
062440b3
AM
18758@@ -0,0 +1,696 @@
18759+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 18760+/*
b00004a5 18761+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
18762+ *
18763+ * This program, aufs is free software; you can redistribute it and/or modify
18764+ * it under the terms of the GNU General Public License as published by
18765+ * the Free Software Foundation; either version 2 of the License, or
18766+ * (at your option) any later version.
18767+ *
18768+ * This program is distributed in the hope that it will be useful,
18769+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18770+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18771+ * GNU General Public License for more details.
18772+ *
18773+ * You should have received a copy of the GNU General Public License
523b37e3 18774+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 18775+ */
1facf9fc 18776+
1308ab2a 18777+/*
4a4d8108 18778+ * inode operations
1308ab2a 18779+ */
dece6358 18780+
4a4d8108
AM
18781+#ifndef __AUFS_INODE_H__
18782+#define __AUFS_INODE_H__
dece6358 18783+
4a4d8108 18784+#ifdef __KERNEL__
1308ab2a 18785+
4a4d8108 18786+#include <linux/fsnotify.h>
4a4d8108 18787+#include "rwsem.h"
1308ab2a 18788+
4a4d8108 18789+struct vfsmount;
1facf9fc 18790+
4a4d8108
AM
18791+struct au_hnotify {
18792+#ifdef CONFIG_AUFS_HNOTIFY
18793+#ifdef CONFIG_AUFS_HFSNOTIFY
7f207e10 18794+ /* never use fsnotify_add_vfsmount_mark() */
0c5527e5 18795+ struct fsnotify_mark hn_mark;
4a4d8108 18796+#endif
1c60b727 18797+ struct inode *hn_aufs_inode; /* no get/put */
4a4d8108
AM
18798+#endif
18799+} ____cacheline_aligned_in_smp;
1facf9fc 18800+
4a4d8108
AM
18801+struct au_hinode {
18802+ struct inode *hi_inode;
18803+ aufs_bindex_t hi_id;
18804+#ifdef CONFIG_AUFS_HNOTIFY
18805+ struct au_hnotify *hi_notify;
18806+#endif
dece6358 18807+
4a4d8108
AM
18808+ /* reference to the copied-up whiteout with get/put */
18809+ struct dentry *hi_whdentry;
18810+};
dece6358 18811+
537831f9
AM
18812+/* ig_flags */
18813+#define AuIG_HALF_REFRESHED 1
18814+#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
18815+#define au_ig_fset(flags, name) \
18816+ do { (flags) |= AuIG_##name; } while (0)
18817+#define au_ig_fclr(flags, name) \
18818+ do { (flags) &= ~AuIG_##name; } while (0)
18819+
18820+struct au_iigen {
be52b249 18821+ spinlock_t ig_spin;
537831f9
AM
18822+ __u32 ig_generation, ig_flags;
18823+};
18824+
4a4d8108
AM
18825+struct au_vdir;
18826+struct au_iinfo {
7a9e40b8 18827+ struct au_iigen ii_generation;
4a4d8108 18828+ struct super_block *ii_hsb1; /* no get/put */
1facf9fc 18829+
4a4d8108 18830+ struct au_rwsem ii_rwsem;
5afbbe0d 18831+ aufs_bindex_t ii_btop, ii_bbot;
4a4d8108
AM
18832+ __u32 ii_higen;
18833+ struct au_hinode *ii_hinode;
18834+ struct au_vdir *ii_vdir;
18835+};
1facf9fc 18836+
4a4d8108
AM
18837+struct au_icntnr {
18838+ struct au_iinfo iinfo;
18839+ struct inode vfs_inode;
8b6a4947 18840+ struct hlist_bl_node plink;
4a4d8108 18841+} ____cacheline_aligned_in_smp;
1308ab2a 18842+
4a4d8108
AM
18843+/* au_pin flags */
18844+#define AuPin_DI_LOCKED 1
18845+#define AuPin_MNT_WRITE (1 << 1)
18846+#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
7f207e10
AM
18847+#define au_fset_pin(flags, name) \
18848+ do { (flags) |= AuPin_##name; } while (0)
18849+#define au_fclr_pin(flags, name) \
18850+ do { (flags) &= ~AuPin_##name; } while (0)
4a4d8108
AM
18851+
18852+struct au_pin {
18853+ /* input */
18854+ struct dentry *dentry;
18855+ unsigned int udba;
18856+ unsigned char lsc_di, lsc_hi, flags;
18857+ aufs_bindex_t bindex;
18858+
18859+ /* output */
18860+ struct dentry *parent;
18861+ struct au_hinode *hdir;
18862+ struct vfsmount *h_mnt;
86dc4139
AM
18863+
18864+ /* temporary unlock/relock for copyup */
18865+ struct dentry *h_dentry, *h_parent;
18866+ struct au_branch *br;
18867+ struct task_struct *task;
4a4d8108 18868+};
1facf9fc 18869+
86dc4139 18870+void au_pin_hdir_unlock(struct au_pin *p);
c1595e42 18871+int au_pin_hdir_lock(struct au_pin *p);
86dc4139 18872+int au_pin_hdir_relock(struct au_pin *p);
86dc4139
AM
18873+void au_pin_hdir_acquire_nest(struct au_pin *p);
18874+void au_pin_hdir_release(struct au_pin *p);
18875+
1308ab2a 18876+/* ---------------------------------------------------------------------- */
18877+
4a4d8108 18878+static inline struct au_iinfo *au_ii(struct inode *inode)
1facf9fc 18879+{
5afbbe0d
AM
18880+ BUG_ON(is_bad_inode(inode));
18881+ return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
4a4d8108 18882+}
1facf9fc 18883+
4a4d8108 18884+/* ---------------------------------------------------------------------- */
1facf9fc 18885+
4a4d8108
AM
18886+/* inode.c */
18887+struct inode *au_igrab(struct inode *inode);
b95c5147 18888+void au_refresh_iop(struct inode *inode, int force_getattr);
027c5e7a 18889+int au_refresh_hinode_self(struct inode *inode);
4a4d8108
AM
18890+int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
18891+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18892+ unsigned int d_type, ino_t *ino);
18893+struct inode *au_new_inode(struct dentry *dentry, int must_new);
18894+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18895+ struct inode *inode);
18896+int au_test_h_perm(struct inode *h_inode, int mask);
18897+int au_test_h_perm_sio(struct inode *h_inode, int mask);
1facf9fc 18898+
4a4d8108
AM
18899+static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
18900+ ino_t h_ino, unsigned int d_type, ino_t *ino)
18901+{
18902+#ifdef CONFIG_AUFS_SHWH
18903+ return au_ino(sb, bindex, h_ino, d_type, ino);
18904+#else
18905+ return 0;
18906+#endif
18907+}
1facf9fc 18908+
4a4d8108 18909+/* i_op.c */
b95c5147
AM
18910+enum {
18911+ AuIop_SYMLINK,
18912+ AuIop_DIR,
18913+ AuIop_OTHER,
18914+ AuIop_Last
18915+};
18916+extern struct inode_operations aufs_iop[AuIop_Last],
18917+ aufs_iop_nogetattr[AuIop_Last];
1308ab2a 18918+
4a4d8108
AM
18919+/* au_wr_dir flags */
18920+#define AuWrDir_ADD_ENTRY 1
7e9cd9fe
AM
18921+#define AuWrDir_ISDIR (1 << 1)
18922+#define AuWrDir_TMPFILE (1 << 2)
4a4d8108 18923+#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
7f207e10
AM
18924+#define au_fset_wrdir(flags, name) \
18925+ do { (flags) |= AuWrDir_##name; } while (0)
18926+#define au_fclr_wrdir(flags, name) \
18927+ do { (flags) &= ~AuWrDir_##name; } while (0)
1facf9fc 18928+
4a4d8108
AM
18929+struct au_wr_dir_args {
18930+ aufs_bindex_t force_btgt;
18931+ unsigned char flags;
18932+};
18933+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18934+ struct au_wr_dir_args *args);
dece6358 18935+
4a4d8108
AM
18936+struct dentry *au_pinned_h_parent(struct au_pin *pin);
18937+void au_pin_init(struct au_pin *pin, struct dentry *dentry,
18938+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18939+ unsigned int udba, unsigned char flags);
18940+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18941+ unsigned int udba, unsigned char flags) __must_check;
18942+int au_do_pin(struct au_pin *pin) __must_check;
18943+void au_unpin(struct au_pin *pin);
c1595e42
JR
18944+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
18945+
18946+#define AuIcpup_DID_CPUP 1
18947+#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
18948+#define au_fset_icpup(flags, name) \
18949+ do { (flags) |= AuIcpup_##name; } while (0)
18950+#define au_fclr_icpup(flags, name) \
18951+ do { (flags) &= ~AuIcpup_##name; } while (0)
18952+
18953+struct au_icpup_args {
18954+ unsigned char flags;
18955+ unsigned char pin_flags;
18956+ aufs_bindex_t btgt;
18957+ unsigned int udba;
18958+ struct au_pin pin;
18959+ struct path h_path;
18960+ struct inode *h_inode;
18961+};
18962+
18963+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
18964+ struct au_icpup_args *a);
18965+
a2654f78
AM
18966+int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
18967+ int locked);
1facf9fc 18968+
4a4d8108
AM
18969+/* i_op_add.c */
18970+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
18971+ struct dentry *h_parent, int isdir);
7eafdf33
AM
18972+int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
18973+ dev_t dev);
4a4d8108 18974+int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
7eafdf33 18975+int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
b4510431 18976+ bool want_excl);
b912730e
AM
18977+struct vfsub_aopen_args;
18978+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
18979+ struct vfsub_aopen_args *args);
38d290e6 18980+int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
4a4d8108
AM
18981+int aufs_link(struct dentry *src_dentry, struct inode *dir,
18982+ struct dentry *dentry);
7eafdf33 18983+int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
1facf9fc 18984+
4a4d8108
AM
18985+/* i_op_del.c */
18986+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
18987+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
18988+ struct dentry *h_parent, int isdir);
18989+int aufs_unlink(struct inode *dir, struct dentry *dentry);
18990+int aufs_rmdir(struct inode *dir, struct dentry *dentry);
1308ab2a 18991+
4a4d8108
AM
18992+/* i_op_ren.c */
18993+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
18994+int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
f2c43d5f
AM
18995+ struct inode *dir, struct dentry *dentry,
18996+ unsigned int flags);
1facf9fc 18997+
4a4d8108
AM
18998+/* iinfo.c */
18999+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
19000+void au_hiput(struct au_hinode *hinode);
19001+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
19002+ struct dentry *h_wh);
19003+unsigned int au_hi_flags(struct inode *inode, int isdir);
1308ab2a 19004+
4a4d8108
AM
19005+/* hinode flags */
19006+#define AuHi_XINO 1
19007+#define AuHi_HNOTIFY (1 << 1)
19008+#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
7f207e10
AM
19009+#define au_fset_hi(flags, name) \
19010+ do { (flags) |= AuHi_##name; } while (0)
19011+#define au_fclr_hi(flags, name) \
19012+ do { (flags) &= ~AuHi_##name; } while (0)
1facf9fc 19013+
4a4d8108
AM
19014+#ifndef CONFIG_AUFS_HNOTIFY
19015+#undef AuHi_HNOTIFY
19016+#define AuHi_HNOTIFY 0
19017+#endif
1facf9fc 19018+
4a4d8108
AM
19019+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
19020+ struct inode *h_inode, unsigned int flags);
1facf9fc 19021+
537831f9 19022+void au_update_iigen(struct inode *inode, int half);
4a4d8108 19023+void au_update_ibrange(struct inode *inode, int do_put_zero);
1facf9fc 19024+
4a4d8108 19025+void au_icntnr_init_once(void *_c);
5afbbe0d 19026+void au_hinode_init(struct au_hinode *hinode);
4a4d8108
AM
19027+int au_iinfo_init(struct inode *inode);
19028+void au_iinfo_fin(struct inode *inode);
e2f27e51 19029+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
1308ab2a 19030+
e49829fe 19031+#ifdef CONFIG_PROC_FS
4a4d8108 19032+/* plink.c */
e49829fe 19033+int au_plink_maint(struct super_block *sb, int flags);
7e9cd9fe 19034+struct au_sbinfo;
e49829fe
JR
19035+void au_plink_maint_leave(struct au_sbinfo *sbinfo);
19036+int au_plink_maint_enter(struct super_block *sb);
4a4d8108
AM
19037+#ifdef CONFIG_AUFS_DEBUG
19038+void au_plink_list(struct super_block *sb);
19039+#else
19040+AuStubVoid(au_plink_list, struct super_block *sb)
19041+#endif
19042+int au_plink_test(struct inode *inode);
19043+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
19044+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
19045+ struct dentry *h_dentry);
e49829fe
JR
19046+void au_plink_put(struct super_block *sb, int verbose);
19047+void au_plink_clean(struct super_block *sb, int verbose);
4a4d8108 19048+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
e49829fe
JR
19049+#else
19050+AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
19051+AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
19052+AuStubInt0(au_plink_maint_enter, struct super_block *sb);
19053+AuStubVoid(au_plink_list, struct super_block *sb);
19054+AuStubInt0(au_plink_test, struct inode *inode);
19055+AuStub(struct dentry *, au_plink_lkup, return NULL,
19056+ struct inode *inode, aufs_bindex_t bindex);
19057+AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
19058+ struct dentry *h_dentry);
19059+AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
19060+AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
19061+AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
19062+#endif /* CONFIG_PROC_FS */
1facf9fc 19063+
c1595e42
JR
19064+#ifdef CONFIG_AUFS_XATTR
19065+/* xattr.c */
7e9cd9fe
AM
19066+int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
19067+ unsigned int verbose);
c1595e42 19068+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
f2c43d5f 19069+void au_xattr_init(struct super_block *sb);
c1595e42
JR
19070+#else
19071+AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
7e9cd9fe 19072+ int ignore_flags, unsigned int verbose);
f2c43d5f 19073+AuStubVoid(au_xattr_init, struct super_block *sb);
c1595e42
JR
19074+#endif
19075+
19076+#ifdef CONFIG_FS_POSIX_ACL
19077+struct posix_acl *aufs_get_acl(struct inode *inode, int type);
19078+int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
19079+#endif
19080+
19081+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
19082+enum {
19083+ AU_XATTR_SET,
c1595e42
JR
19084+ AU_ACL_SET
19085+};
19086+
f2c43d5f 19087+struct au_sxattr {
c1595e42
JR
19088+ int type;
19089+ union {
19090+ struct {
19091+ const char *name;
19092+ const void *value;
19093+ size_t size;
19094+ int flags;
19095+ } set;
19096+ struct {
c1595e42
JR
19097+ struct posix_acl *acl;
19098+ int type;
19099+ } acl_set;
19100+ } u;
19101+};
f2c43d5f
AM
19102+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
19103+ struct au_sxattr *arg);
c1595e42
JR
19104+#endif
19105+
4a4d8108 19106+/* ---------------------------------------------------------------------- */
1308ab2a 19107+
4a4d8108
AM
19108+/* lock subclass for iinfo */
19109+enum {
19110+ AuLsc_II_CHILD, /* child first */
19111+ AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
19112+ AuLsc_II_CHILD3, /* copyup dirs */
19113+ AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
19114+ AuLsc_II_PARENT2,
19115+ AuLsc_II_PARENT3, /* copyup dirs */
19116+ AuLsc_II_NEW_CHILD
19117+};
1308ab2a 19118+
1facf9fc 19119+/*
4a4d8108
AM
19120+ * ii_read_lock_child, ii_write_lock_child,
19121+ * ii_read_lock_child2, ii_write_lock_child2,
19122+ * ii_read_lock_child3, ii_write_lock_child3,
19123+ * ii_read_lock_parent, ii_write_lock_parent,
19124+ * ii_read_lock_parent2, ii_write_lock_parent2,
19125+ * ii_read_lock_parent3, ii_write_lock_parent3,
19126+ * ii_read_lock_new_child, ii_write_lock_new_child,
1facf9fc 19127+ */
4a4d8108
AM
19128+#define AuReadLockFunc(name, lsc) \
19129+static inline void ii_read_lock_##name(struct inode *i) \
19130+{ \
19131+ au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19132+}
19133+
19134+#define AuWriteLockFunc(name, lsc) \
19135+static inline void ii_write_lock_##name(struct inode *i) \
19136+{ \
19137+ au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19138+}
19139+
19140+#define AuRWLockFuncs(name, lsc) \
19141+ AuReadLockFunc(name, lsc) \
19142+ AuWriteLockFunc(name, lsc)
19143+
19144+AuRWLockFuncs(child, CHILD);
19145+AuRWLockFuncs(child2, CHILD2);
19146+AuRWLockFuncs(child3, CHILD3);
19147+AuRWLockFuncs(parent, PARENT);
19148+AuRWLockFuncs(parent2, PARENT2);
19149+AuRWLockFuncs(parent3, PARENT3);
19150+AuRWLockFuncs(new_child, NEW_CHILD);
19151+
19152+#undef AuReadLockFunc
19153+#undef AuWriteLockFunc
19154+#undef AuRWLockFuncs
1facf9fc 19155+
8b6a4947
AM
19156+#define ii_read_unlock(i) au_rw_read_unlock(&au_ii(i)->ii_rwsem)
19157+#define ii_write_unlock(i) au_rw_write_unlock(&au_ii(i)->ii_rwsem)
19158+#define ii_downgrade_lock(i) au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
1facf9fc 19159+
4a4d8108
AM
19160+#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
19161+#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
19162+#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
1facf9fc 19163+
4a4d8108 19164+/* ---------------------------------------------------------------------- */
1308ab2a 19165+
027c5e7a
AM
19166+static inline void au_icntnr_init(struct au_icntnr *c)
19167+{
19168+#ifdef CONFIG_AUFS_DEBUG
19169+ c->vfs_inode.i_mode = 0;
19170+#endif
19171+}
19172+
cfc41e69 19173+static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
4a4d8108 19174+{
537831f9
AM
19175+ unsigned int gen;
19176+ struct au_iinfo *iinfo;
be52b249 19177+ struct au_iigen *iigen;
537831f9
AM
19178+
19179+ iinfo = au_ii(inode);
be52b249
AM
19180+ iigen = &iinfo->ii_generation;
19181+ spin_lock(&iigen->ig_spin);
cfc41e69
AM
19182+ if (igflags)
19183+ *igflags = iigen->ig_flags;
be52b249
AM
19184+ gen = iigen->ig_generation;
19185+ spin_unlock(&iigen->ig_spin);
537831f9
AM
19186+
19187+ return gen;
4a4d8108 19188+}
1308ab2a 19189+
4a4d8108
AM
19190+/* tiny test for inode number */
19191+/* tmpfs generation is too rough */
19192+static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
19193+{
19194+ struct au_iinfo *iinfo;
1308ab2a 19195+
4a4d8108
AM
19196+ iinfo = au_ii(inode);
19197+ AuRwMustAnyLock(&iinfo->ii_rwsem);
19198+ return !(iinfo->ii_hsb1 == h_inode->i_sb
19199+ && iinfo->ii_higen == h_inode->i_generation);
19200+}
1308ab2a 19201+
4a4d8108
AM
19202+static inline void au_iigen_dec(struct inode *inode)
19203+{
537831f9 19204+ struct au_iinfo *iinfo;
be52b249 19205+ struct au_iigen *iigen;
537831f9
AM
19206+
19207+ iinfo = au_ii(inode);
be52b249
AM
19208+ iigen = &iinfo->ii_generation;
19209+ spin_lock(&iigen->ig_spin);
19210+ iigen->ig_generation--;
19211+ spin_unlock(&iigen->ig_spin);
027c5e7a
AM
19212+}
19213+
19214+static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
19215+{
19216+ int err;
19217+
19218+ err = 0;
537831f9 19219+ if (unlikely(inode && au_iigen(inode, NULL) != sigen))
027c5e7a
AM
19220+ err = -EIO;
19221+
19222+ return err;
4a4d8108 19223+}
1308ab2a 19224+
4a4d8108 19225+/* ---------------------------------------------------------------------- */
1308ab2a 19226+
5afbbe0d
AM
19227+static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
19228+ aufs_bindex_t bindex)
19229+{
19230+ return iinfo->ii_hinode + bindex;
19231+}
19232+
19233+static inline int au_is_bad_inode(struct inode *inode)
19234+{
19235+ return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
19236+}
19237+
4a4d8108
AM
19238+static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
19239+ aufs_bindex_t bindex)
19240+{
19241+ IiMustAnyLock(inode);
5afbbe0d 19242+ return au_hinode(au_ii(inode), bindex)->hi_id;
4a4d8108 19243+}
1308ab2a 19244+
5afbbe0d 19245+static inline aufs_bindex_t au_ibtop(struct inode *inode)
4a4d8108
AM
19246+{
19247+ IiMustAnyLock(inode);
5afbbe0d 19248+ return au_ii(inode)->ii_btop;
4a4d8108 19249+}
1308ab2a 19250+
5afbbe0d 19251+static inline aufs_bindex_t au_ibbot(struct inode *inode)
4a4d8108
AM
19252+{
19253+ IiMustAnyLock(inode);
5afbbe0d 19254+ return au_ii(inode)->ii_bbot;
4a4d8108 19255+}
1308ab2a 19256+
4a4d8108
AM
19257+static inline struct au_vdir *au_ivdir(struct inode *inode)
19258+{
19259+ IiMustAnyLock(inode);
19260+ return au_ii(inode)->ii_vdir;
19261+}
1308ab2a 19262+
4a4d8108
AM
19263+static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
19264+{
19265+ IiMustAnyLock(inode);
5afbbe0d 19266+ return au_hinode(au_ii(inode), bindex)->hi_whdentry;
4a4d8108 19267+}
1308ab2a 19268+
5afbbe0d 19269+static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 19270+{
4a4d8108 19271+ IiMustWriteLock(inode);
5afbbe0d 19272+ au_ii(inode)->ii_btop = bindex;
4a4d8108 19273+}
1308ab2a 19274+
5afbbe0d 19275+static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
4a4d8108
AM
19276+{
19277+ IiMustWriteLock(inode);
5afbbe0d 19278+ au_ii(inode)->ii_bbot = bindex;
1308ab2a 19279+}
19280+
4a4d8108
AM
19281+static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
19282+{
19283+ IiMustWriteLock(inode);
19284+ au_ii(inode)->ii_vdir = vdir;
19285+}
1facf9fc 19286+
4a4d8108 19287+static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 19288+{
4a4d8108 19289+ IiMustAnyLock(inode);
5afbbe0d 19290+ return au_hinode(au_ii(inode), bindex);
4a4d8108 19291+}
dece6358 19292+
4a4d8108 19293+/* ---------------------------------------------------------------------- */
1facf9fc 19294+
4a4d8108
AM
19295+static inline struct dentry *au_pinned_parent(struct au_pin *pin)
19296+{
19297+ if (pin)
19298+ return pin->parent;
19299+ return NULL;
1facf9fc 19300+}
19301+
4a4d8108 19302+static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
1facf9fc 19303+{
4a4d8108
AM
19304+ if (pin && pin->hdir)
19305+ return pin->hdir->hi_inode;
19306+ return NULL;
1308ab2a 19307+}
1facf9fc 19308+
4a4d8108
AM
19309+static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
19310+{
19311+ if (pin)
19312+ return pin->hdir;
19313+ return NULL;
19314+}
1facf9fc 19315+
4a4d8108 19316+static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
1308ab2a 19317+{
4a4d8108
AM
19318+ if (pin)
19319+ pin->dentry = dentry;
19320+}
1308ab2a 19321+
4a4d8108
AM
19322+static inline void au_pin_set_parent_lflag(struct au_pin *pin,
19323+ unsigned char lflag)
19324+{
19325+ if (pin) {
7f207e10 19326+ if (lflag)
4a4d8108 19327+ au_fset_pin(pin->flags, DI_LOCKED);
7f207e10 19328+ else
4a4d8108 19329+ au_fclr_pin(pin->flags, DI_LOCKED);
1308ab2a 19330+ }
4a4d8108
AM
19331+}
19332+
7e9cd9fe 19333+#if 0 /* reserved */
4a4d8108
AM
19334+static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
19335+{
19336+ if (pin) {
19337+ dput(pin->parent);
19338+ pin->parent = dget(parent);
1facf9fc 19339+ }
4a4d8108 19340+}
7e9cd9fe 19341+#endif
1facf9fc 19342+
4a4d8108
AM
19343+/* ---------------------------------------------------------------------- */
19344+
027c5e7a 19345+struct au_branch;
4a4d8108
AM
19346+#ifdef CONFIG_AUFS_HNOTIFY
19347+struct au_hnotify_op {
19348+ void (*ctl)(struct au_hinode *hinode, int do_set);
027c5e7a 19349+ int (*alloc)(struct au_hinode *hinode);
7eafdf33
AM
19350+
19351+ /*
19352+ * if it returns true, the the caller should free hinode->hi_notify,
19353+ * otherwise ->free() frees it.
19354+ */
19355+ int (*free)(struct au_hinode *hinode,
19356+ struct au_hnotify *hn) __must_check;
4a4d8108
AM
19357+
19358+ void (*fin)(void);
19359+ int (*init)(void);
027c5e7a
AM
19360+
19361+ int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
19362+ void (*fin_br)(struct au_branch *br);
19363+ int (*init_br)(struct au_branch *br, int perm);
4a4d8108
AM
19364+};
19365+
19366+/* hnotify.c */
027c5e7a 19367+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
4a4d8108
AM
19368+void au_hn_free(struct au_hinode *hinode);
19369+void au_hn_ctl(struct au_hinode *hinode, int do_set);
19370+void au_hn_reset(struct inode *inode, unsigned int flags);
19371+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
19372+ struct qstr *h_child_qstr, struct inode *h_child_inode);
027c5e7a
AM
19373+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
19374+int au_hnotify_init_br(struct au_branch *br, int perm);
19375+void au_hnotify_fin_br(struct au_branch *br);
4a4d8108
AM
19376+int __init au_hnotify_init(void);
19377+void au_hnotify_fin(void);
19378+
7f207e10 19379+/* hfsnotify.c */
4a4d8108
AM
19380+extern const struct au_hnotify_op au_hnotify_op;
19381+
19382+static inline
19383+void au_hn_init(struct au_hinode *hinode)
19384+{
19385+ hinode->hi_notify = NULL;
1308ab2a 19386+}
19387+
53392da6
AM
19388+static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
19389+{
19390+ return hinode->hi_notify;
19391+}
19392+
4a4d8108 19393+#else
c1595e42
JR
19394+AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
19395+ struct au_hinode *hinode __maybe_unused,
19396+ struct inode *inode __maybe_unused)
19397+AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
4a4d8108
AM
19398+AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
19399+AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
19400+ int do_set __maybe_unused)
19401+AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
19402+ unsigned int flags __maybe_unused)
027c5e7a
AM
19403+AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
19404+ struct au_branch *br __maybe_unused,
19405+ int perm __maybe_unused)
19406+AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
19407+ int perm __maybe_unused)
19408+AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
4a4d8108
AM
19409+AuStubInt0(__init au_hnotify_init, void)
19410+AuStubVoid(au_hnotify_fin, void)
19411+AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
19412+#endif /* CONFIG_AUFS_HNOTIFY */
19413+
19414+static inline void au_hn_suspend(struct au_hinode *hdir)
19415+{
19416+ au_hn_ctl(hdir, /*do_set*/0);
1308ab2a 19417+}
19418+
4a4d8108 19419+static inline void au_hn_resume(struct au_hinode *hdir)
1308ab2a 19420+{
4a4d8108
AM
19421+ au_hn_ctl(hdir, /*do_set*/1);
19422+}
1308ab2a 19423+
5afbbe0d 19424+static inline void au_hn_inode_lock(struct au_hinode *hdir)
4a4d8108 19425+{
febd17d6 19426+ inode_lock(hdir->hi_inode);
4a4d8108
AM
19427+ au_hn_suspend(hdir);
19428+}
dece6358 19429+
5afbbe0d 19430+static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
4a4d8108
AM
19431+ unsigned int sc __maybe_unused)
19432+{
febd17d6 19433+ inode_lock_nested(hdir->hi_inode, sc);
4a4d8108 19434+ au_hn_suspend(hdir);
1facf9fc 19435+}
1facf9fc 19436+
8b6a4947
AM
19437+#if 0 /* unused */
19438+#include "vfsub.h"
3c1bdaff
AM
19439+static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
19440+ unsigned int sc)
19441+{
be118d29 19442+ inode_lock_shared_nested(hdir->hi_inode, sc);
3c1bdaff
AM
19443+ au_hn_suspend(hdir);
19444+}
8b6a4947 19445+#endif
3c1bdaff 19446+
5afbbe0d 19447+static inline void au_hn_inode_unlock(struct au_hinode *hdir)
4a4d8108
AM
19448+{
19449+ au_hn_resume(hdir);
febd17d6 19450+ inode_unlock(hdir->hi_inode);
4a4d8108
AM
19451+}
19452+
19453+#endif /* __KERNEL__ */
19454+#endif /* __AUFS_INODE_H__ */
7f207e10
AM
19455diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
19456--- /usr/share/empty/fs/aufs/ioctl.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 19457+++ linux/fs/aufs/ioctl.c 2018-08-12 23:43:05.460124736 +0200
062440b3 19458@@ -0,0 +1,220 @@
cd7a4cd9 19459+// SPDX-License-Identifier: GPL-2.0
4a4d8108 19460+/*
b00004a5 19461+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
19462+ *
19463+ * This program, aufs is free software; you can redistribute it and/or modify
19464+ * it under the terms of the GNU General Public License as published by
19465+ * the Free Software Foundation; either version 2 of the License, or
19466+ * (at your option) any later version.
19467+ *
19468+ * This program is distributed in the hope that it will be useful,
19469+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19470+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19471+ * GNU General Public License for more details.
19472+ *
19473+ * You should have received a copy of the GNU General Public License
523b37e3 19474+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
19475+ */
19476+
19477+/*
19478+ * ioctl
19479+ * plink-management and readdir in userspace.
19480+ * assist the pathconf(3) wrapper library.
c2b27bf2 19481+ * move-down
076b876e 19482+ * File-based Hierarchical Storage Management.
4a4d8108
AM
19483+ */
19484+
c2b27bf2
AM
19485+#include <linux/compat.h>
19486+#include <linux/file.h>
4a4d8108
AM
19487+#include "aufs.h"
19488+
1e00d052 19489+static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
4a4d8108
AM
19490+{
19491+ int err, fd;
5afbbe0d 19492+ aufs_bindex_t wbi, bindex, bbot;
4a4d8108
AM
19493+ struct file *h_file;
19494+ struct super_block *sb;
19495+ struct dentry *root;
1e00d052
AM
19496+ struct au_branch *br;
19497+ struct aufs_wbr_fd wbrfd = {
19498+ .oflags = au_dir_roflags,
19499+ .brid = -1
19500+ };
19501+ const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
19502+ | O_NOATIME | O_CLOEXEC;
4a4d8108 19503+
1e00d052
AM
19504+ AuDebugOn(wbrfd.oflags & ~valid);
19505+
19506+ if (arg) {
19507+ err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
19508+ if (unlikely(err)) {
19509+ err = -EFAULT;
19510+ goto out;
19511+ }
19512+
19513+ err = -EINVAL;
19514+ AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
19515+ wbrfd.oflags |= au_dir_roflags;
19516+ AuDbg("0%o\n", wbrfd.oflags);
19517+ if (unlikely(wbrfd.oflags & ~valid))
19518+ goto out;
19519+ }
19520+
2000de60 19521+ fd = get_unused_fd_flags(0);
1e00d052
AM
19522+ err = fd;
19523+ if (unlikely(fd < 0))
4a4d8108 19524+ goto out;
4a4d8108 19525+
1e00d052 19526+ h_file = ERR_PTR(-EINVAL);
4a4d8108 19527+ wbi = 0;
1e00d052 19528+ br = NULL;
4a4d8108
AM
19529+ sb = path->dentry->d_sb;
19530+ root = sb->s_root;
19531+ aufs_read_lock(root, AuLock_IR);
5afbbe0d 19532+ bbot = au_sbbot(sb);
1e00d052
AM
19533+ if (wbrfd.brid >= 0) {
19534+ wbi = au_br_index(sb, wbrfd.brid);
5afbbe0d 19535+ if (unlikely(wbi < 0 || wbi > bbot))
1e00d052
AM
19536+ goto out_unlock;
19537+ }
19538+
19539+ h_file = ERR_PTR(-ENOENT);
19540+ br = au_sbr(sb, wbi);
19541+ if (!au_br_writable(br->br_perm)) {
19542+ if (arg)
19543+ goto out_unlock;
19544+
19545+ bindex = wbi + 1;
19546+ wbi = -1;
5afbbe0d 19547+ for (; bindex <= bbot; bindex++) {
1e00d052
AM
19548+ br = au_sbr(sb, bindex);
19549+ if (au_br_writable(br->br_perm)) {
4a4d8108 19550+ wbi = bindex;
1e00d052 19551+ br = au_sbr(sb, wbi);
4a4d8108
AM
19552+ break;
19553+ }
19554+ }
4a4d8108
AM
19555+ }
19556+ AuDbg("wbi %d\n", wbi);
1e00d052 19557+ if (wbi >= 0)
392086de
AM
19558+ h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
19559+ /*force_wr*/0);
1e00d052
AM
19560+
19561+out_unlock:
4a4d8108
AM
19562+ aufs_read_unlock(root, AuLock_IR);
19563+ err = PTR_ERR(h_file);
19564+ if (IS_ERR(h_file))
19565+ goto out_fd;
19566+
5afbbe0d 19567+ au_br_put(br); /* cf. au_h_open() */
4a4d8108
AM
19568+ fd_install(fd, h_file);
19569+ err = fd;
19570+ goto out; /* success */
19571+
4f0767ce 19572+out_fd:
4a4d8108 19573+ put_unused_fd(fd);
4f0767ce 19574+out:
1e00d052 19575+ AuTraceErr(err);
4a4d8108
AM
19576+ return err;
19577+}
19578+
19579+/* ---------------------------------------------------------------------- */
19580+
19581+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
19582+{
19583+ long err;
c1595e42 19584+ struct dentry *dentry;
4a4d8108
AM
19585+
19586+ switch (cmd) {
4a4d8108
AM
19587+ case AUFS_CTL_RDU:
19588+ case AUFS_CTL_RDU_INO:
19589+ err = au_rdu_ioctl(file, cmd, arg);
19590+ break;
19591+
19592+ case AUFS_CTL_WBR_FD:
1e00d052 19593+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
19594+ break;
19595+
027c5e7a
AM
19596+ case AUFS_CTL_IBUSY:
19597+ err = au_ibusy_ioctl(file, arg);
19598+ break;
19599+
076b876e
AM
19600+ case AUFS_CTL_BRINFO:
19601+ err = au_brinfo_ioctl(file, arg);
19602+ break;
19603+
19604+ case AUFS_CTL_FHSM_FD:
2000de60 19605+ dentry = file->f_path.dentry;
c1595e42
JR
19606+ if (IS_ROOT(dentry))
19607+ err = au_fhsm_fd(dentry->d_sb, arg);
19608+ else
19609+ err = -ENOTTY;
076b876e
AM
19610+ break;
19611+
4a4d8108
AM
19612+ default:
19613+ /* do not call the lower */
19614+ AuDbg("0x%x\n", cmd);
19615+ err = -ENOTTY;
19616+ }
19617+
19618+ AuTraceErr(err);
19619+ return err;
19620+}
19621+
19622+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
19623+{
19624+ long err;
19625+
19626+ switch (cmd) {
c2b27bf2 19627+ case AUFS_CTL_MVDOWN:
2000de60 19628+ err = au_mvdown(file->f_path.dentry, (void __user *)arg);
c2b27bf2
AM
19629+ break;
19630+
4a4d8108 19631+ case AUFS_CTL_WBR_FD:
1e00d052 19632+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
19633+ break;
19634+
19635+ default:
19636+ /* do not call the lower */
19637+ AuDbg("0x%x\n", cmd);
19638+ err = -ENOTTY;
19639+ }
19640+
19641+ AuTraceErr(err);
19642+ return err;
19643+}
b752ccd1
AM
19644+
19645+#ifdef CONFIG_COMPAT
19646+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
19647+ unsigned long arg)
19648+{
19649+ long err;
19650+
19651+ switch (cmd) {
19652+ case AUFS_CTL_RDU:
19653+ case AUFS_CTL_RDU_INO:
19654+ err = au_rdu_compat_ioctl(file, cmd, arg);
19655+ break;
19656+
027c5e7a
AM
19657+ case AUFS_CTL_IBUSY:
19658+ err = au_ibusy_compat_ioctl(file, arg);
19659+ break;
19660+
076b876e
AM
19661+ case AUFS_CTL_BRINFO:
19662+ err = au_brinfo_compat_ioctl(file, arg);
19663+ break;
19664+
b752ccd1
AM
19665+ default:
19666+ err = aufs_ioctl_dir(file, cmd, arg);
19667+ }
19668+
19669+ AuTraceErr(err);
19670+ return err;
19671+}
19672+
b752ccd1
AM
19673+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
19674+ unsigned long arg)
19675+{
19676+ return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
19677+}
19678+#endif
7f207e10
AM
19679diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
19680--- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 19681+++ linux/fs/aufs/i_op_add.c 2018-08-12 23:43:05.456791299 +0200
062440b3 19682@@ -0,0 +1,921 @@
cd7a4cd9 19683+// SPDX-License-Identifier: GPL-2.0
4a4d8108 19684+/*
b00004a5 19685+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
19686+ *
19687+ * This program, aufs is free software; you can redistribute it and/or modify
19688+ * it under the terms of the GNU General Public License as published by
19689+ * the Free Software Foundation; either version 2 of the License, or
19690+ * (at your option) any later version.
19691+ *
19692+ * This program is distributed in the hope that it will be useful,
19693+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19694+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19695+ * GNU General Public License for more details.
19696+ *
19697+ * You should have received a copy of the GNU General Public License
523b37e3 19698+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
19699+ */
19700+
19701+/*
19702+ * inode operations (add entry)
19703+ */
19704+
19705+#include "aufs.h"
19706+
19707+/*
19708+ * final procedure of adding a new entry, except link(2).
19709+ * remove whiteout, instantiate, copyup the parent dir's times and size
19710+ * and update version.
19711+ * if it failed, re-create the removed whiteout.
19712+ */
19713+static int epilog(struct inode *dir, aufs_bindex_t bindex,
19714+ struct dentry *wh_dentry, struct dentry *dentry)
19715+{
19716+ int err, rerr;
19717+ aufs_bindex_t bwh;
19718+ struct path h_path;
076b876e 19719+ struct super_block *sb;
4a4d8108
AM
19720+ struct inode *inode, *h_dir;
19721+ struct dentry *wh;
19722+
19723+ bwh = -1;
076b876e 19724+ sb = dir->i_sb;
4a4d8108 19725+ if (wh_dentry) {
5527c038 19726+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
4a4d8108
AM
19727+ IMustLock(h_dir);
19728+ AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
19729+ bwh = au_dbwh(dentry);
19730+ h_path.dentry = wh_dentry;
076b876e 19731+ h_path.mnt = au_sbr_mnt(sb, bindex);
4a4d8108
AM
19732+ err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
19733+ dentry);
19734+ if (unlikely(err))
19735+ goto out;
19736+ }
19737+
19738+ inode = au_new_inode(dentry, /*must_new*/1);
19739+ if (!IS_ERR(inode)) {
19740+ d_instantiate(dentry, inode);
5527c038 19741+ dir = d_inode(dentry->d_parent); /* dir inode is locked */
4a4d8108 19742+ IMustLock(dir);
b912730e 19743+ au_dir_ts(dir, bindex);
be118d29 19744+ inode_inc_iversion(dir);
076b876e 19745+ au_fhsm_wrote(sb, bindex, /*force*/0);
4a4d8108
AM
19746+ return 0; /* success */
19747+ }
19748+
19749+ err = PTR_ERR(inode);
19750+ if (!wh_dentry)
19751+ goto out;
19752+
19753+ /* revert */
19754+ /* dir inode is locked */
19755+ wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
19756+ rerr = PTR_ERR(wh);
19757+ if (IS_ERR(wh)) {
523b37e3
AM
19758+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
19759+ dentry, err, rerr);
4a4d8108
AM
19760+ err = -EIO;
19761+ } else
19762+ dput(wh);
19763+
4f0767ce 19764+out:
4a4d8108
AM
19765+ return err;
19766+}
19767+
027c5e7a
AM
19768+static int au_d_may_add(struct dentry *dentry)
19769+{
19770+ int err;
19771+
19772+ err = 0;
19773+ if (unlikely(d_unhashed(dentry)))
19774+ err = -ENOENT;
5527c038 19775+ if (unlikely(d_really_is_positive(dentry)))
027c5e7a
AM
19776+ err = -EEXIST;
19777+ return err;
19778+}
19779+
4a4d8108
AM
19780+/*
19781+ * simple tests for the adding inode operations.
19782+ * following the checks in vfs, plus the parent-child relationship.
19783+ */
19784+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19785+ struct dentry *h_parent, int isdir)
19786+{
19787+ int err;
19788+ umode_t h_mode;
19789+ struct dentry *h_dentry;
19790+ struct inode *h_inode;
19791+
19792+ err = -ENAMETOOLONG;
19793+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19794+ goto out;
19795+
19796+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 19797+ if (d_really_is_negative(dentry)) {
4a4d8108 19798+ err = -EEXIST;
5527c038 19799+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
19800+ goto out;
19801+ } else {
19802+ /* rename(2) case */
19803+ err = -EIO;
5527c038
JR
19804+ if (unlikely(d_is_negative(h_dentry)))
19805+ goto out;
19806+ h_inode = d_inode(h_dentry);
19807+ if (unlikely(!h_inode->i_nlink))
4a4d8108
AM
19808+ goto out;
19809+
19810+ h_mode = h_inode->i_mode;
19811+ if (!isdir) {
19812+ err = -EISDIR;
19813+ if (unlikely(S_ISDIR(h_mode)))
19814+ goto out;
19815+ } else if (unlikely(!S_ISDIR(h_mode))) {
19816+ err = -ENOTDIR;
19817+ goto out;
19818+ }
19819+ }
19820+
19821+ err = 0;
19822+ /* expected parent dir is locked */
19823+ if (unlikely(h_parent != h_dentry->d_parent))
19824+ err = -EIO;
19825+
4f0767ce 19826+out:
4a4d8108
AM
19827+ AuTraceErr(err);
19828+ return err;
19829+}
19830+
19831+/*
19832+ * initial procedure of adding a new entry.
19833+ * prepare writable branch and the parent dir, lock it,
19834+ * and lookup whiteout for the new entry.
19835+ */
19836+static struct dentry*
19837+lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
19838+ struct dentry *src_dentry, struct au_pin *pin,
19839+ struct au_wr_dir_args *wr_dir_args)
19840+{
19841+ struct dentry *wh_dentry, *h_parent;
19842+ struct super_block *sb;
19843+ struct au_branch *br;
19844+ int err;
19845+ unsigned int udba;
19846+ aufs_bindex_t bcpup;
19847+
523b37e3 19848+ AuDbg("%pd\n", dentry);
4a4d8108
AM
19849+
19850+ err = au_wr_dir(dentry, src_dentry, wr_dir_args);
19851+ bcpup = err;
19852+ wh_dentry = ERR_PTR(err);
19853+ if (unlikely(err < 0))
19854+ goto out;
19855+
19856+ sb = dentry->d_sb;
19857+ udba = au_opt_udba(sb);
19858+ err = au_pin(pin, dentry, bcpup, udba,
19859+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19860+ wh_dentry = ERR_PTR(err);
19861+ if (unlikely(err))
19862+ goto out;
19863+
19864+ h_parent = au_pinned_h_parent(pin);
19865+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 19866+ && au_dbtop(dentry) == bcpup)
4a4d8108
AM
19867+ err = au_may_add(dentry, bcpup, h_parent,
19868+ au_ftest_wrdir(wr_dir_args->flags, ISDIR));
19869+ else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19870+ err = -ENAMETOOLONG;
19871+ wh_dentry = ERR_PTR(err);
19872+ if (unlikely(err))
19873+ goto out_unpin;
19874+
19875+ br = au_sbr(sb, bcpup);
19876+ if (dt) {
19877+ struct path tmp = {
19878+ .dentry = h_parent,
86dc4139 19879+ .mnt = au_br_mnt(br)
4a4d8108
AM
19880+ };
19881+ au_dtime_store(dt, au_pinned_parent(pin), &tmp);
19882+ }
19883+
19884+ wh_dentry = NULL;
19885+ if (bcpup != au_dbwh(dentry))
19886+ goto out; /* success */
19887+
2000de60
JR
19888+ /*
19889+ * ENAMETOOLONG here means that if we allowed create such name, then it
19890+ * would not be able to removed in the future. So we don't allow such
19891+ * name here and we don't handle ENAMETOOLONG differently here.
19892+ */
4a4d8108
AM
19893+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
19894+
4f0767ce 19895+out_unpin:
4a4d8108
AM
19896+ if (IS_ERR(wh_dentry))
19897+ au_unpin(pin);
4f0767ce 19898+out:
4a4d8108
AM
19899+ return wh_dentry;
19900+}
19901+
19902+/* ---------------------------------------------------------------------- */
19903+
19904+enum { Mknod, Symlink, Creat };
19905+struct simple_arg {
19906+ int type;
19907+ union {
19908+ struct {
b912730e
AM
19909+ umode_t mode;
19910+ bool want_excl;
19911+ bool try_aopen;
19912+ struct vfsub_aopen_args *aopen;
4a4d8108
AM
19913+ } c;
19914+ struct {
19915+ const char *symname;
19916+ } s;
19917+ struct {
7eafdf33 19918+ umode_t mode;
4a4d8108
AM
19919+ dev_t dev;
19920+ } m;
19921+ } u;
19922+};
19923+
19924+static int add_simple(struct inode *dir, struct dentry *dentry,
19925+ struct simple_arg *arg)
19926+{
076b876e 19927+ int err, rerr;
5afbbe0d 19928+ aufs_bindex_t btop;
4a4d8108 19929+ unsigned char created;
b912730e
AM
19930+ const unsigned char try_aopen
19931+ = (arg->type == Creat && arg->u.c.try_aopen);
4a4d8108
AM
19932+ struct dentry *wh_dentry, *parent;
19933+ struct inode *h_dir;
b912730e
AM
19934+ struct super_block *sb;
19935+ struct au_branch *br;
c2b27bf2
AM
19936+ /* to reuduce stack size */
19937+ struct {
19938+ struct au_dtime dt;
19939+ struct au_pin pin;
19940+ struct path h_path;
19941+ struct au_wr_dir_args wr_dir_args;
19942+ } *a;
4a4d8108 19943+
523b37e3 19944+ AuDbg("%pd\n", dentry);
4a4d8108
AM
19945+ IMustLock(dir);
19946+
c2b27bf2
AM
19947+ err = -ENOMEM;
19948+ a = kmalloc(sizeof(*a), GFP_NOFS);
19949+ if (unlikely(!a))
19950+ goto out;
19951+ a->wr_dir_args.force_btgt = -1;
19952+ a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
19953+
4a4d8108 19954+ parent = dentry->d_parent; /* dir inode is locked */
b912730e
AM
19955+ if (!try_aopen) {
19956+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
19957+ if (unlikely(err))
19958+ goto out_free;
19959+ }
027c5e7a
AM
19960+ err = au_d_may_add(dentry);
19961+ if (unlikely(err))
19962+ goto out_unlock;
b912730e
AM
19963+ if (!try_aopen)
19964+ di_write_lock_parent(parent);
c2b27bf2
AM
19965+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
19966+ &a->pin, &a->wr_dir_args);
4a4d8108
AM
19967+ err = PTR_ERR(wh_dentry);
19968+ if (IS_ERR(wh_dentry))
027c5e7a 19969+ goto out_parent;
4a4d8108 19970+
5afbbe0d 19971+ btop = au_dbtop(dentry);
b912730e 19972+ sb = dentry->d_sb;
5afbbe0d
AM
19973+ br = au_sbr(sb, btop);
19974+ a->h_path.dentry = au_h_dptr(dentry, btop);
b912730e 19975+ a->h_path.mnt = au_br_mnt(br);
c2b27bf2 19976+ h_dir = au_pinned_h_dir(&a->pin);
4a4d8108
AM
19977+ switch (arg->type) {
19978+ case Creat:
b912730e
AM
19979+ err = 0;
19980+ if (!try_aopen || !h_dir->i_op->atomic_open)
19981+ err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
19982+ arg->u.c.want_excl);
19983+ else
19984+ err = vfsub_atomic_open(h_dir, a->h_path.dentry,
19985+ arg->u.c.aopen, br);
4a4d8108
AM
19986+ break;
19987+ case Symlink:
c2b27bf2 19988+ err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
4a4d8108
AM
19989+ break;
19990+ case Mknod:
c2b27bf2
AM
19991+ err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
19992+ arg->u.m.dev);
4a4d8108
AM
19993+ break;
19994+ default:
19995+ BUG();
19996+ }
19997+ created = !err;
19998+ if (!err)
5afbbe0d 19999+ err = epilog(dir, btop, wh_dentry, dentry);
4a4d8108
AM
20000+
20001+ /* revert */
5527c038 20002+ if (unlikely(created && err && d_is_positive(a->h_path.dentry))) {
523b37e3
AM
20003+ /* no delegation since it is just created */
20004+ rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
20005+ /*force*/0);
4a4d8108 20006+ if (rerr) {
523b37e3
AM
20007+ AuIOErr("%pd revert failure(%d, %d)\n",
20008+ dentry, err, rerr);
4a4d8108
AM
20009+ err = -EIO;
20010+ }
c2b27bf2 20011+ au_dtime_revert(&a->dt);
4a4d8108
AM
20012+ }
20013+
b912730e
AM
20014+ if (!err && try_aopen && !h_dir->i_op->atomic_open)
20015+ *arg->u.c.aopen->opened |= FILE_CREATED;
20016+
c2b27bf2 20017+ au_unpin(&a->pin);
4a4d8108
AM
20018+ dput(wh_dentry);
20019+
027c5e7a 20020+out_parent:
b912730e
AM
20021+ if (!try_aopen)
20022+ di_write_unlock(parent);
027c5e7a 20023+out_unlock:
4a4d8108 20024+ if (unlikely(err)) {
5afbbe0d 20025+ au_update_dbtop(dentry);
4a4d8108
AM
20026+ d_drop(dentry);
20027+ }
b912730e
AM
20028+ if (!try_aopen)
20029+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 20030+out_free:
1c60b727 20031+ kfree(a);
027c5e7a 20032+out:
4a4d8108
AM
20033+ return err;
20034+}
20035+
7eafdf33
AM
20036+int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
20037+ dev_t dev)
4a4d8108
AM
20038+{
20039+ struct simple_arg arg = {
20040+ .type = Mknod,
20041+ .u.m = {
20042+ .mode = mode,
20043+ .dev = dev
20044+ }
20045+ };
20046+ return add_simple(dir, dentry, &arg);
20047+}
20048+
20049+int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
20050+{
20051+ struct simple_arg arg = {
20052+ .type = Symlink,
20053+ .u.s.symname = symname
20054+ };
20055+ return add_simple(dir, dentry, &arg);
20056+}
20057+
7eafdf33 20058+int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
b4510431 20059+ bool want_excl)
4a4d8108
AM
20060+{
20061+ struct simple_arg arg = {
20062+ .type = Creat,
20063+ .u.c = {
b4510431
AM
20064+ .mode = mode,
20065+ .want_excl = want_excl
4a4d8108
AM
20066+ }
20067+ };
20068+ return add_simple(dir, dentry, &arg);
20069+}
20070+
b912730e
AM
20071+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20072+ struct vfsub_aopen_args *aopen_args)
20073+{
20074+ struct simple_arg arg = {
20075+ .type = Creat,
20076+ .u.c = {
20077+ .mode = aopen_args->create_mode,
20078+ .want_excl = aopen_args->open_flag & O_EXCL,
20079+ .try_aopen = true,
20080+ .aopen = aopen_args
20081+ }
20082+ };
20083+ return add_simple(dir, dentry, &arg);
20084+}
20085+
38d290e6
JR
20086+int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
20087+{
20088+ int err;
20089+ aufs_bindex_t bindex;
20090+ struct super_block *sb;
20091+ struct dentry *parent, *h_parent, *h_dentry;
20092+ struct inode *h_dir, *inode;
20093+ struct vfsmount *h_mnt;
20094+ struct au_wr_dir_args wr_dir_args = {
20095+ .force_btgt = -1,
20096+ .flags = AuWrDir_TMPFILE
20097+ };
20098+
20099+ /* copy-up may happen */
febd17d6 20100+ inode_lock(dir);
38d290e6
JR
20101+
20102+ sb = dir->i_sb;
20103+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20104+ if (unlikely(err))
20105+ goto out;
20106+
20107+ err = au_di_init(dentry);
20108+ if (unlikely(err))
20109+ goto out_si;
20110+
20111+ err = -EBUSY;
20112+ parent = d_find_any_alias(dir);
20113+ AuDebugOn(!parent);
20114+ di_write_lock_parent(parent);
5527c038 20115+ if (unlikely(d_inode(parent) != dir))
38d290e6
JR
20116+ goto out_parent;
20117+
20118+ err = au_digen_test(parent, au_sigen(sb));
20119+ if (unlikely(err))
20120+ goto out_parent;
20121+
5afbbe0d
AM
20122+ bindex = au_dbtop(parent);
20123+ au_set_dbtop(dentry, bindex);
20124+ au_set_dbbot(dentry, bindex);
38d290e6
JR
20125+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
20126+ bindex = err;
20127+ if (unlikely(err < 0))
20128+ goto out_parent;
20129+
20130+ err = -EOPNOTSUPP;
20131+ h_dir = au_h_iptr(dir, bindex);
20132+ if (unlikely(!h_dir->i_op->tmpfile))
20133+ goto out_parent;
20134+
20135+ h_mnt = au_sbr_mnt(sb, bindex);
20136+ err = vfsub_mnt_want_write(h_mnt);
20137+ if (unlikely(err))
20138+ goto out_parent;
20139+
20140+ h_parent = au_h_dptr(parent, bindex);
521ced18
JR
20141+ h_dentry = vfs_tmpfile(h_parent, mode, /*open_flag*/0);
20142+ if (IS_ERR(h_dentry)) {
20143+ err = PTR_ERR(h_dentry);
38d290e6 20144+ goto out_mnt;
521ced18 20145+ }
38d290e6 20146+
5afbbe0d
AM
20147+ au_set_dbtop(dentry, bindex);
20148+ au_set_dbbot(dentry, bindex);
38d290e6
JR
20149+ au_set_h_dptr(dentry, bindex, dget(h_dentry));
20150+ inode = au_new_inode(dentry, /*must_new*/1);
20151+ if (IS_ERR(inode)) {
20152+ err = PTR_ERR(inode);
20153+ au_set_h_dptr(dentry, bindex, NULL);
5afbbe0d
AM
20154+ au_set_dbtop(dentry, -1);
20155+ au_set_dbbot(dentry, -1);
38d290e6
JR
20156+ } else {
20157+ if (!inode->i_nlink)
20158+ set_nlink(inode, 1);
20159+ d_tmpfile(dentry, inode);
20160+ au_di(dentry)->di_tmpfile = 1;
20161+
20162+ /* update without i_mutex */
5afbbe0d 20163+ if (au_ibtop(dir) == au_dbtop(dentry))
38d290e6
JR
20164+ au_cpup_attr_timesizes(dir);
20165+ }
38d290e6 20166+ dput(h_dentry);
521ced18 20167+
38d290e6
JR
20168+out_mnt:
20169+ vfsub_mnt_drop_write(h_mnt);
20170+out_parent:
20171+ di_write_unlock(parent);
20172+ dput(parent);
20173+ di_write_unlock(dentry);
5afbbe0d 20174+ if (unlikely(err)) {
38d290e6
JR
20175+ au_di_fin(dentry);
20176+ dentry->d_fsdata = NULL;
20177+ }
20178+out_si:
20179+ si_read_unlock(sb);
20180+out:
febd17d6 20181+ inode_unlock(dir);
38d290e6
JR
20182+ return err;
20183+}
20184+
4a4d8108
AM
20185+/* ---------------------------------------------------------------------- */
20186+
20187+struct au_link_args {
20188+ aufs_bindex_t bdst, bsrc;
20189+ struct au_pin pin;
20190+ struct path h_path;
20191+ struct dentry *src_parent, *parent;
20192+};
20193+
20194+static int au_cpup_before_link(struct dentry *src_dentry,
20195+ struct au_link_args *a)
20196+{
20197+ int err;
20198+ struct dentry *h_src_dentry;
c2b27bf2
AM
20199+ struct au_cp_generic cpg = {
20200+ .dentry = src_dentry,
20201+ .bdst = a->bdst,
20202+ .bsrc = a->bsrc,
20203+ .len = -1,
20204+ .pin = &a->pin,
20205+ .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
20206+ };
4a4d8108
AM
20207+
20208+ di_read_lock_parent(a->src_parent, AuLock_IR);
20209+ err = au_test_and_cpup_dirs(src_dentry, a->bdst);
20210+ if (unlikely(err))
20211+ goto out;
20212+
20213+ h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
4a4d8108
AM
20214+ err = au_pin(&a->pin, src_dentry, a->bdst,
20215+ au_opt_udba(src_dentry->d_sb),
20216+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20217+ if (unlikely(err))
20218+ goto out;
367653fa 20219+
c2b27bf2 20220+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
20221+ au_unpin(&a->pin);
20222+
4f0767ce 20223+out:
4a4d8108
AM
20224+ di_read_unlock(a->src_parent, AuLock_IR);
20225+ return err;
20226+}
20227+
86dc4139
AM
20228+static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
20229+ struct au_link_args *a)
4a4d8108
AM
20230+{
20231+ int err;
20232+ unsigned char plink;
5afbbe0d 20233+ aufs_bindex_t bbot;
4a4d8108 20234+ struct dentry *h_src_dentry;
523b37e3 20235+ struct inode *h_inode, *inode, *delegated;
4a4d8108
AM
20236+ struct super_block *sb;
20237+ struct file *h_file;
20238+
20239+ plink = 0;
20240+ h_inode = NULL;
20241+ sb = src_dentry->d_sb;
5527c038 20242+ inode = d_inode(src_dentry);
5afbbe0d 20243+ if (au_ibtop(inode) <= a->bdst)
4a4d8108
AM
20244+ h_inode = au_h_iptr(inode, a->bdst);
20245+ if (!h_inode || !h_inode->i_nlink) {
20246+ /* copyup src_dentry as the name of dentry. */
5afbbe0d
AM
20247+ bbot = au_dbbot(dentry);
20248+ if (bbot < a->bsrc)
20249+ au_set_dbbot(dentry, a->bsrc);
86dc4139
AM
20250+ au_set_h_dptr(dentry, a->bsrc,
20251+ dget(au_h_dptr(src_dentry, a->bsrc)));
20252+ dget(a->h_path.dentry);
20253+ au_set_h_dptr(dentry, a->bdst, NULL);
c1595e42
JR
20254+ AuDbg("temporary d_inode...\n");
20255+ spin_lock(&dentry->d_lock);
5527c038 20256+ dentry->d_inode = d_inode(src_dentry); /* tmp */
c1595e42 20257+ spin_unlock(&dentry->d_lock);
392086de 20258+ h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
86dc4139 20259+ if (IS_ERR(h_file))
4a4d8108 20260+ err = PTR_ERR(h_file);
86dc4139 20261+ else {
c2b27bf2
AM
20262+ struct au_cp_generic cpg = {
20263+ .dentry = dentry,
20264+ .bdst = a->bdst,
20265+ .bsrc = -1,
20266+ .len = -1,
20267+ .pin = &a->pin,
20268+ .flags = AuCpup_KEEPLINO
20269+ };
20270+ err = au_sio_cpup_simple(&cpg);
86dc4139
AM
20271+ au_h_open_post(dentry, a->bsrc, h_file);
20272+ if (!err) {
20273+ dput(a->h_path.dentry);
20274+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20275+ } else
20276+ au_set_h_dptr(dentry, a->bdst,
20277+ a->h_path.dentry);
20278+ }
c1595e42 20279+ spin_lock(&dentry->d_lock);
86dc4139 20280+ dentry->d_inode = NULL; /* restore */
c1595e42
JR
20281+ spin_unlock(&dentry->d_lock);
20282+ AuDbg("temporary d_inode...done\n");
86dc4139 20283+ au_set_h_dptr(dentry, a->bsrc, NULL);
5afbbe0d 20284+ au_set_dbbot(dentry, bbot);
4a4d8108
AM
20285+ } else {
20286+ /* the inode of src_dentry already exists on a.bdst branch */
20287+ h_src_dentry = d_find_alias(h_inode);
20288+ if (!h_src_dentry && au_plink_test(inode)) {
20289+ plink = 1;
20290+ h_src_dentry = au_plink_lkup(inode, a->bdst);
20291+ err = PTR_ERR(h_src_dentry);
20292+ if (IS_ERR(h_src_dentry))
20293+ goto out;
20294+
5527c038 20295+ if (unlikely(d_is_negative(h_src_dentry))) {
4a4d8108
AM
20296+ dput(h_src_dentry);
20297+ h_src_dentry = NULL;
20298+ }
20299+
20300+ }
20301+ if (h_src_dentry) {
523b37e3 20302+ delegated = NULL;
4a4d8108 20303+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
20304+ &a->h_path, &delegated);
20305+ if (unlikely(err == -EWOULDBLOCK)) {
20306+ pr_warn("cannot retry for NFSv4 delegation"
20307+ " for an internal link\n");
20308+ iput(delegated);
20309+ }
4a4d8108
AM
20310+ dput(h_src_dentry);
20311+ } else {
20312+ AuIOErr("no dentry found for hi%lu on b%d\n",
20313+ h_inode->i_ino, a->bdst);
20314+ err = -EIO;
20315+ }
20316+ }
20317+
20318+ if (!err && !plink)
20319+ au_plink_append(inode, a->bdst, a->h_path.dentry);
20320+
20321+out:
2cbb1c4b 20322+ AuTraceErr(err);
4a4d8108
AM
20323+ return err;
20324+}
20325+
20326+int aufs_link(struct dentry *src_dentry, struct inode *dir,
20327+ struct dentry *dentry)
20328+{
20329+ int err, rerr;
20330+ struct au_dtime dt;
20331+ struct au_link_args *a;
20332+ struct dentry *wh_dentry, *h_src_dentry;
523b37e3 20333+ struct inode *inode, *delegated;
4a4d8108
AM
20334+ struct super_block *sb;
20335+ struct au_wr_dir_args wr_dir_args = {
20336+ /* .force_btgt = -1, */
20337+ .flags = AuWrDir_ADD_ENTRY
20338+ };
20339+
20340+ IMustLock(dir);
5527c038 20341+ inode = d_inode(src_dentry);
4a4d8108
AM
20342+ IMustLock(inode);
20343+
4a4d8108
AM
20344+ err = -ENOMEM;
20345+ a = kzalloc(sizeof(*a), GFP_NOFS);
20346+ if (unlikely(!a))
20347+ goto out;
20348+
20349+ a->parent = dentry->d_parent; /* dir inode is locked */
027c5e7a
AM
20350+ err = aufs_read_and_write_lock2(dentry, src_dentry,
20351+ AuLock_NOPLM | AuLock_GEN);
e49829fe
JR
20352+ if (unlikely(err))
20353+ goto out_kfree;
38d290e6 20354+ err = au_d_linkable(src_dentry);
027c5e7a
AM
20355+ if (unlikely(err))
20356+ goto out_unlock;
20357+ err = au_d_may_add(dentry);
20358+ if (unlikely(err))
20359+ goto out_unlock;
e49829fe 20360+
4a4d8108 20361+ a->src_parent = dget_parent(src_dentry);
5afbbe0d 20362+ wr_dir_args.force_btgt = au_ibtop(inode);
4a4d8108
AM
20363+
20364+ di_write_lock_parent(a->parent);
20365+ wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
20366+ wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
20367+ &wr_dir_args);
20368+ err = PTR_ERR(wh_dentry);
20369+ if (IS_ERR(wh_dentry))
027c5e7a 20370+ goto out_parent;
4a4d8108
AM
20371+
20372+ err = 0;
20373+ sb = dentry->d_sb;
5afbbe0d 20374+ a->bdst = au_dbtop(dentry);
4a4d8108
AM
20375+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20376+ a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
5afbbe0d 20377+ a->bsrc = au_ibtop(inode);
2cbb1c4b 20378+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
38d290e6
JR
20379+ if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
20380+ h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
2cbb1c4b 20381+ if (!h_src_dentry) {
5afbbe0d 20382+ a->bsrc = au_dbtop(src_dentry);
2cbb1c4b
JR
20383+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20384+ AuDebugOn(!h_src_dentry);
38d290e6
JR
20385+ } else if (IS_ERR(h_src_dentry)) {
20386+ err = PTR_ERR(h_src_dentry);
2cbb1c4b 20387+ goto out_parent;
38d290e6 20388+ }
2cbb1c4b 20389+
f2c43d5f
AM
20390+ /*
20391+ * aufs doesn't touch the credential so
20392+ * security_dentry_create_files_as() is unnecrssary.
20393+ */
4a4d8108
AM
20394+ if (au_opt_test(au_mntflags(sb), PLINK)) {
20395+ if (a->bdst < a->bsrc
20396+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
86dc4139 20397+ err = au_cpup_or_link(src_dentry, dentry, a);
523b37e3
AM
20398+ else {
20399+ delegated = NULL;
4a4d8108 20400+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
20401+ &a->h_path, &delegated);
20402+ if (unlikely(err == -EWOULDBLOCK)) {
20403+ pr_warn("cannot retry for NFSv4 delegation"
20404+ " for an internal link\n");
20405+ iput(delegated);
20406+ }
20407+ }
2cbb1c4b 20408+ dput(h_src_dentry);
4a4d8108
AM
20409+ } else {
20410+ /*
20411+ * copyup src_dentry to the branch we process,
20412+ * and then link(2) to it.
20413+ */
2cbb1c4b 20414+ dput(h_src_dentry);
4a4d8108
AM
20415+ if (a->bdst < a->bsrc
20416+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
20417+ au_unpin(&a->pin);
20418+ di_write_unlock(a->parent);
20419+ err = au_cpup_before_link(src_dentry, a);
20420+ di_write_lock_parent(a->parent);
20421+ if (!err)
20422+ err = au_pin(&a->pin, dentry, a->bdst,
20423+ au_opt_udba(sb),
20424+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20425+ if (unlikely(err))
20426+ goto out_wh;
20427+ }
20428+ if (!err) {
20429+ h_src_dentry = au_h_dptr(src_dentry, a->bdst);
20430+ err = -ENOENT;
5527c038 20431+ if (h_src_dentry && d_is_positive(h_src_dentry)) {
523b37e3 20432+ delegated = NULL;
4a4d8108
AM
20433+ err = vfsub_link(h_src_dentry,
20434+ au_pinned_h_dir(&a->pin),
523b37e3
AM
20435+ &a->h_path, &delegated);
20436+ if (unlikely(err == -EWOULDBLOCK)) {
20437+ pr_warn("cannot retry"
20438+ " for NFSv4 delegation"
20439+ " for an internal link\n");
20440+ iput(delegated);
20441+ }
20442+ }
4a4d8108
AM
20443+ }
20444+ }
20445+ if (unlikely(err))
20446+ goto out_unpin;
20447+
20448+ if (wh_dentry) {
20449+ a->h_path.dentry = wh_dentry;
20450+ err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
20451+ dentry);
20452+ if (unlikely(err))
20453+ goto out_revert;
20454+ }
20455+
b912730e 20456+ au_dir_ts(dir, a->bdst);
be118d29 20457+ inode_inc_iversion(dir);
4a4d8108
AM
20458+ inc_nlink(inode);
20459+ inode->i_ctime = dir->i_ctime;
027c5e7a
AM
20460+ d_instantiate(dentry, au_igrab(inode));
20461+ if (d_unhashed(a->h_path.dentry))
4a4d8108
AM
20462+ /* some filesystem calls d_drop() */
20463+ d_drop(dentry);
076b876e
AM
20464+ /* some filesystems consume an inode even hardlink */
20465+ au_fhsm_wrote(sb, a->bdst, /*force*/0);
4a4d8108
AM
20466+ goto out_unpin; /* success */
20467+
4f0767ce 20468+out_revert:
523b37e3
AM
20469+ /* no delegation since it is just created */
20470+ rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
20471+ /*delegated*/NULL, /*force*/0);
027c5e7a 20472+ if (unlikely(rerr)) {
523b37e3 20473+ AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
027c5e7a
AM
20474+ err = -EIO;
20475+ }
4a4d8108 20476+ au_dtime_revert(&dt);
4f0767ce 20477+out_unpin:
4a4d8108 20478+ au_unpin(&a->pin);
4f0767ce 20479+out_wh:
4a4d8108 20480+ dput(wh_dentry);
027c5e7a
AM
20481+out_parent:
20482+ di_write_unlock(a->parent);
20483+ dput(a->src_parent);
4f0767ce 20484+out_unlock:
4a4d8108 20485+ if (unlikely(err)) {
5afbbe0d 20486+ au_update_dbtop(dentry);
4a4d8108
AM
20487+ d_drop(dentry);
20488+ }
4a4d8108 20489+ aufs_read_and_write_unlock2(dentry, src_dentry);
e49829fe 20490+out_kfree:
1c60b727 20491+ kfree(a);
4f0767ce 20492+out:
86dc4139 20493+ AuTraceErr(err);
4a4d8108
AM
20494+ return err;
20495+}
20496+
7eafdf33 20497+int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
4a4d8108
AM
20498+{
20499+ int err, rerr;
20500+ aufs_bindex_t bindex;
20501+ unsigned char diropq;
20502+ struct path h_path;
20503+ struct dentry *wh_dentry, *parent, *opq_dentry;
febd17d6 20504+ struct inode *h_inode;
4a4d8108
AM
20505+ struct super_block *sb;
20506+ struct {
20507+ struct au_pin pin;
20508+ struct au_dtime dt;
20509+ } *a; /* reduce the stack usage */
20510+ struct au_wr_dir_args wr_dir_args = {
20511+ .force_btgt = -1,
20512+ .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
20513+ };
20514+
20515+ IMustLock(dir);
20516+
20517+ err = -ENOMEM;
20518+ a = kmalloc(sizeof(*a), GFP_NOFS);
20519+ if (unlikely(!a))
20520+ goto out;
20521+
027c5e7a
AM
20522+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20523+ if (unlikely(err))
20524+ goto out_free;
20525+ err = au_d_may_add(dentry);
20526+ if (unlikely(err))
20527+ goto out_unlock;
20528+
4a4d8108
AM
20529+ parent = dentry->d_parent; /* dir inode is locked */
20530+ di_write_lock_parent(parent);
20531+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20532+ &a->pin, &wr_dir_args);
20533+ err = PTR_ERR(wh_dentry);
20534+ if (IS_ERR(wh_dentry))
027c5e7a 20535+ goto out_parent;
4a4d8108
AM
20536+
20537+ sb = dentry->d_sb;
5afbbe0d 20538+ bindex = au_dbtop(dentry);
4a4d8108
AM
20539+ h_path.dentry = au_h_dptr(dentry, bindex);
20540+ h_path.mnt = au_sbr_mnt(sb, bindex);
20541+ err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
20542+ if (unlikely(err))
027c5e7a 20543+ goto out_unpin;
4a4d8108
AM
20544+
20545+ /* make the dir opaque */
20546+ diropq = 0;
febd17d6 20547+ h_inode = d_inode(h_path.dentry);
4a4d8108
AM
20548+ if (wh_dentry
20549+ || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
febd17d6 20550+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 20551+ opq_dentry = au_diropq_create(dentry, bindex);
febd17d6 20552+ inode_unlock(h_inode);
4a4d8108
AM
20553+ err = PTR_ERR(opq_dentry);
20554+ if (IS_ERR(opq_dentry))
20555+ goto out_dir;
20556+ dput(opq_dentry);
20557+ diropq = 1;
20558+ }
20559+
20560+ err = epilog(dir, bindex, wh_dentry, dentry);
20561+ if (!err) {
20562+ inc_nlink(dir);
027c5e7a 20563+ goto out_unpin; /* success */
4a4d8108
AM
20564+ }
20565+
20566+ /* revert */
20567+ if (diropq) {
20568+ AuLabel(revert opq);
febd17d6 20569+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 20570+ rerr = au_diropq_remove(dentry, bindex);
febd17d6 20571+ inode_unlock(h_inode);
4a4d8108 20572+ if (rerr) {
523b37e3
AM
20573+ AuIOErr("%pd reverting diropq failed(%d, %d)\n",
20574+ dentry, err, rerr);
4a4d8108
AM
20575+ err = -EIO;
20576+ }
20577+ }
20578+
4f0767ce 20579+out_dir:
4a4d8108
AM
20580+ AuLabel(revert dir);
20581+ rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
20582+ if (rerr) {
523b37e3
AM
20583+ AuIOErr("%pd reverting dir failed(%d, %d)\n",
20584+ dentry, err, rerr);
4a4d8108
AM
20585+ err = -EIO;
20586+ }
4a4d8108 20587+ au_dtime_revert(&a->dt);
027c5e7a 20588+out_unpin:
4a4d8108
AM
20589+ au_unpin(&a->pin);
20590+ dput(wh_dentry);
027c5e7a
AM
20591+out_parent:
20592+ di_write_unlock(parent);
20593+out_unlock:
4a4d8108 20594+ if (unlikely(err)) {
5afbbe0d 20595+ au_update_dbtop(dentry);
4a4d8108
AM
20596+ d_drop(dentry);
20597+ }
4a4d8108 20598+ aufs_read_unlock(dentry, AuLock_DW);
027c5e7a 20599+out_free:
1c60b727 20600+ kfree(a);
4f0767ce 20601+out:
4a4d8108
AM
20602+ return err;
20603+}
7f207e10
AM
20604diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
20605--- /usr/share/empty/fs/aufs/i_op.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 20606+++ linux/fs/aufs/i_op.c 2018-08-12 23:43:05.456791299 +0200
062440b3 20607@@ -0,0 +1,1460 @@
cd7a4cd9 20608+// SPDX-License-Identifier: GPL-2.0
4a4d8108 20609+/*
b00004a5 20610+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
20611+ *
20612+ * This program, aufs is free software; you can redistribute it and/or modify
20613+ * it under the terms of the GNU General Public License as published by
20614+ * the Free Software Foundation; either version 2 of the License, or
20615+ * (at your option) any later version.
20616+ *
20617+ * This program is distributed in the hope that it will be useful,
20618+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20619+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20620+ * GNU General Public License for more details.
20621+ *
20622+ * You should have received a copy of the GNU General Public License
523b37e3 20623+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 20624+ */
1facf9fc 20625+
1308ab2a 20626+/*
4a4d8108 20627+ * inode operations (except add/del/rename)
1308ab2a 20628+ */
4a4d8108
AM
20629+
20630+#include <linux/device_cgroup.h>
20631+#include <linux/fs_stack.h>
4a4d8108
AM
20632+#include <linux/namei.h>
20633+#include <linux/security.h>
4a4d8108
AM
20634+#include "aufs.h"
20635+
1e00d052 20636+static int h_permission(struct inode *h_inode, int mask,
79b8bda9 20637+ struct path *h_path, int brperm)
1facf9fc 20638+{
1308ab2a 20639+ int err;
4a4d8108 20640+ const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
1facf9fc 20641+
e2f27e51
AM
20642+ err = -EPERM;
20643+ if (write_mask && IS_IMMUTABLE(h_inode))
20644+ goto out;
20645+
4a4d8108 20646+ err = -EACCES;
e2f27e51
AM
20647+ if (((mask & MAY_EXEC)
20648+ && S_ISREG(h_inode->i_mode)
20649+ && (path_noexec(h_path)
cd7a4cd9 20650+ || !(h_inode->i_mode & 0111))))
4a4d8108
AM
20651+ goto out;
20652+
20653+ /*
20654+ * - skip the lower fs test in the case of write to ro branch.
20655+ * - nfs dir permission write check is optimized, but a policy for
20656+ * link/rename requires a real check.
2121bcd9 20657+ * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
b912730e 20658+ * in this case, generic_permission() returns -EOPNOTSUPP.
4a4d8108
AM
20659+ */
20660+ if ((write_mask && !au_br_writable(brperm))
20661+ || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
20662+ && write_mask && !(mask & MAY_READ))
20663+ || !h_inode->i_op->permission) {
20664+ /* AuLabel(generic_permission); */
062440b3 20665+ /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
1e00d052 20666+ err = generic_permission(h_inode, mask);
b912730e
AM
20667+ if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
20668+ err = h_inode->i_op->permission(h_inode, mask);
20669+ AuTraceErr(err);
1308ab2a 20670+ } else {
4a4d8108 20671+ /* AuLabel(h_inode->permission); */
1e00d052 20672+ err = h_inode->i_op->permission(h_inode, mask);
4a4d8108
AM
20673+ AuTraceErr(err);
20674+ }
1facf9fc 20675+
4a4d8108
AM
20676+ if (!err)
20677+ err = devcgroup_inode_permission(h_inode, mask);
7f207e10 20678+ if (!err)
4a4d8108 20679+ err = security_inode_permission(h_inode, mask);
4a4d8108
AM
20680+
20681+#if 0
20682+ if (!err) {
20683+ /* todo: do we need to call ima_path_check()? */
20684+ struct path h_path = {
20685+ .dentry =
20686+ .mnt = h_mnt
20687+ };
20688+ err = ima_path_check(&h_path,
20689+ mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
20690+ IMA_COUNT_LEAVE);
1308ab2a 20691+ }
4a4d8108 20692+#endif
dece6358 20693+
4f0767ce 20694+out:
1308ab2a 20695+ return err;
20696+}
dece6358 20697+
1e00d052 20698+static int aufs_permission(struct inode *inode, int mask)
1308ab2a 20699+{
20700+ int err;
5afbbe0d 20701+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
20702+ const unsigned char isdir = !!S_ISDIR(inode->i_mode),
20703+ write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20704+ struct inode *h_inode;
20705+ struct super_block *sb;
20706+ struct au_branch *br;
1facf9fc 20707+
027c5e7a 20708+ /* todo: support rcu-walk? */
1e00d052 20709+ if (mask & MAY_NOT_BLOCK)
027c5e7a
AM
20710+ return -ECHILD;
20711+
4a4d8108
AM
20712+ sb = inode->i_sb;
20713+ si_read_lock(sb, AuLock_FLUSH);
20714+ ii_read_lock_child(inode);
027c5e7a
AM
20715+#if 0
20716+ err = au_iigen_test(inode, au_sigen(sb));
20717+ if (unlikely(err))
20718+ goto out;
20719+#endif
dece6358 20720+
076b876e
AM
20721+ if (!isdir
20722+ || write_mask
20723+ || au_opt_test(au_mntflags(sb), DIRPERM1)) {
4a4d8108 20724+ err = au_busy_or_stale();
5afbbe0d 20725+ h_inode = au_h_iptr(inode, au_ibtop(inode));
4a4d8108
AM
20726+ if (unlikely(!h_inode
20727+ || (h_inode->i_mode & S_IFMT)
20728+ != (inode->i_mode & S_IFMT)))
20729+ goto out;
1facf9fc 20730+
4a4d8108 20731+ err = 0;
5afbbe0d 20732+ bindex = au_ibtop(inode);
4a4d8108 20733+ br = au_sbr(sb, bindex);
79b8bda9 20734+ err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
4a4d8108
AM
20735+ if (write_mask
20736+ && !err
20737+ && !special_file(h_inode->i_mode)) {
20738+ /* test whether the upper writable branch exists */
20739+ err = -EROFS;
20740+ for (; bindex >= 0; bindex--)
20741+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
20742+ err = 0;
20743+ break;
20744+ }
20745+ }
20746+ goto out;
20747+ }
dece6358 20748+
4a4d8108 20749+ /* non-write to dir */
1308ab2a 20750+ err = 0;
5afbbe0d
AM
20751+ bbot = au_ibbot(inode);
20752+ for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
20753+ h_inode = au_h_iptr(inode, bindex);
20754+ if (h_inode) {
20755+ err = au_busy_or_stale();
20756+ if (unlikely(!S_ISDIR(h_inode->i_mode)))
20757+ break;
20758+
20759+ br = au_sbr(sb, bindex);
79b8bda9 20760+ err = h_permission(h_inode, mask, &br->br_path,
4a4d8108
AM
20761+ br->br_perm);
20762+ }
20763+ }
1308ab2a 20764+
4f0767ce 20765+out:
4a4d8108
AM
20766+ ii_read_unlock(inode);
20767+ si_read_unlock(sb);
1308ab2a 20768+ return err;
20769+}
20770+
4a4d8108 20771+/* ---------------------------------------------------------------------- */
1facf9fc 20772+
4a4d8108 20773+static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
b4510431 20774+ unsigned int flags)
4a4d8108
AM
20775+{
20776+ struct dentry *ret, *parent;
b752ccd1 20777+ struct inode *inode;
4a4d8108 20778+ struct super_block *sb;
1716fcea 20779+ int err, npositive;
dece6358 20780+
4a4d8108 20781+ IMustLock(dir);
1308ab2a 20782+
537831f9
AM
20783+ /* todo: support rcu-walk? */
20784+ ret = ERR_PTR(-ECHILD);
20785+ if (flags & LOOKUP_RCU)
20786+ goto out;
20787+
20788+ ret = ERR_PTR(-ENAMETOOLONG);
20789+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20790+ goto out;
20791+
4a4d8108 20792+ sb = dir->i_sb;
7f207e10
AM
20793+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20794+ ret = ERR_PTR(err);
20795+ if (unlikely(err))
20796+ goto out;
20797+
4a4d8108
AM
20798+ err = au_di_init(dentry);
20799+ ret = ERR_PTR(err);
20800+ if (unlikely(err))
7f207e10 20801+ goto out_si;
1308ab2a 20802+
9dbd164d 20803+ inode = NULL;
027c5e7a 20804+ npositive = 0; /* suppress a warning */
4a4d8108
AM
20805+ parent = dentry->d_parent; /* dir inode is locked */
20806+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
20807+ err = au_alive_dir(parent);
20808+ if (!err)
20809+ err = au_digen_test(parent, au_sigen(sb));
20810+ if (!err) {
5afbbe0d
AM
20811+ /* regardless LOOKUP_CREATE, always ALLOW_NEG */
20812+ npositive = au_lkup_dentry(dentry, au_dbtop(parent),
20813+ AuLkup_ALLOW_NEG);
027c5e7a
AM
20814+ err = npositive;
20815+ }
4a4d8108 20816+ di_read_unlock(parent, AuLock_IR);
4a4d8108
AM
20817+ ret = ERR_PTR(err);
20818+ if (unlikely(err < 0))
20819+ goto out_unlock;
1308ab2a 20820+
4a4d8108 20821+ if (npositive) {
b752ccd1 20822+ inode = au_new_inode(dentry, /*must_new*/0);
c1595e42
JR
20823+ if (IS_ERR(inode)) {
20824+ ret = (void *)inode;
20825+ inode = NULL;
20826+ goto out_unlock;
20827+ }
9dbd164d 20828+ }
4a4d8108 20829+
c1595e42
JR
20830+ if (inode)
20831+ atomic_inc(&inode->i_count);
4a4d8108 20832+ ret = d_splice_alias(inode, dentry);
537831f9
AM
20833+#if 0
20834+ if (unlikely(d_need_lookup(dentry))) {
20835+ spin_lock(&dentry->d_lock);
20836+ dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
20837+ spin_unlock(&dentry->d_lock);
20838+ } else
20839+#endif
c1595e42 20840+ if (inode) {
2000de60 20841+ if (!IS_ERR(ret)) {
c1595e42 20842+ iput(inode);
2000de60
JR
20843+ if (ret && ret != dentry)
20844+ ii_write_unlock(inode);
20845+ } else {
c1595e42
JR
20846+ ii_write_unlock(inode);
20847+ iput(inode);
20848+ inode = NULL;
20849+ }
7f207e10 20850+ }
1facf9fc 20851+
4f0767ce 20852+out_unlock:
4a4d8108 20853+ di_write_unlock(dentry);
7f207e10 20854+out_si:
4a4d8108 20855+ si_read_unlock(sb);
7f207e10 20856+out:
4a4d8108
AM
20857+ return ret;
20858+}
1facf9fc 20859+
4a4d8108 20860+/* ---------------------------------------------------------------------- */
1facf9fc 20861+
b912730e 20862+struct aopen_node {
8b6a4947 20863+ struct hlist_bl_node hblist;
b912730e
AM
20864+ struct file *file, *h_file;
20865+};
20866+
20867+static int au_do_aopen(struct inode *inode, struct file *file)
20868+{
8b6a4947
AM
20869+ struct hlist_bl_head *aopen;
20870+ struct hlist_bl_node *pos;
b912730e
AM
20871+ struct aopen_node *node;
20872+ struct au_do_open_args args = {
8b6a4947
AM
20873+ .aopen = 1,
20874+ .open = au_do_open_nondir
b912730e
AM
20875+ };
20876+
20877+ aopen = &au_sbi(inode->i_sb)->si_aopen;
8b6a4947
AM
20878+ hlist_bl_lock(aopen);
20879+ hlist_bl_for_each_entry(node, pos, aopen, hblist)
b912730e
AM
20880+ if (node->file == file) {
20881+ args.h_file = node->h_file;
20882+ break;
20883+ }
8b6a4947 20884+ hlist_bl_unlock(aopen);
b912730e
AM
20885+ /* AuDebugOn(!args.h_file); */
20886+
20887+ return au_do_open(file, &args);
20888+}
20889+
20890+static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
20891+ struct file *file, unsigned int open_flag,
20892+ umode_t create_mode, int *opened)
20893+{
8b6a4947 20894+ int err, unlocked, h_opened = *opened;
5afbbe0d 20895+ unsigned int lkup_flags;
f0c0a007 20896+ struct dentry *parent, *d;
8b6a4947 20897+ struct hlist_bl_head *aopen;
b912730e
AM
20898+ struct vfsub_aopen_args args = {
20899+ .open_flag = open_flag,
20900+ .create_mode = create_mode,
20901+ .opened = &h_opened
20902+ };
20903+ struct aopen_node aopen_node = {
20904+ .file = file
20905+ };
20906+
20907+ IMustLock(dir);
5afbbe0d 20908+ AuDbg("open_flag 0%o\n", open_flag);
b912730e
AM
20909+ AuDbgDentry(dentry);
20910+
20911+ err = 0;
20912+ if (!au_di(dentry)) {
5afbbe0d
AM
20913+ lkup_flags = LOOKUP_OPEN;
20914+ if (open_flag & O_CREAT)
20915+ lkup_flags |= LOOKUP_CREATE;
20916+ d = aufs_lookup(dir, dentry, lkup_flags);
b912730e
AM
20917+ if (IS_ERR(d)) {
20918+ err = PTR_ERR(d);
5afbbe0d 20919+ AuTraceErr(err);
b912730e
AM
20920+ goto out;
20921+ } else if (d) {
20922+ /*
20923+ * obsoleted dentry found.
20924+ * another error will be returned later.
20925+ */
20926+ d_drop(d);
b912730e 20927+ AuDbgDentry(d);
5afbbe0d 20928+ dput(d);
b912730e
AM
20929+ }
20930+ AuDbgDentry(dentry);
20931+ }
20932+
20933+ if (d_is_positive(dentry)
20934+ || d_unhashed(dentry)
20935+ || d_unlinked(dentry)
20936+ || !(open_flag & O_CREAT))
20937+ goto out_no_open;
20938+
8b6a4947 20939+ unlocked = 0;
b912730e
AM
20940+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
20941+ if (unlikely(err))
20942+ goto out;
20943+
20944+ parent = dentry->d_parent; /* dir is locked */
20945+ di_write_lock_parent(parent);
5afbbe0d 20946+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
b912730e
AM
20947+ if (unlikely(err))
20948+ goto out_unlock;
20949+
20950+ AuDbgDentry(dentry);
20951+ if (d_is_positive(dentry))
20952+ goto out_unlock;
20953+
20954+ args.file = get_empty_filp();
20955+ err = PTR_ERR(args.file);
20956+ if (IS_ERR(args.file))
20957+ goto out_unlock;
20958+
20959+ args.file->f_flags = file->f_flags;
20960+ err = au_aopen_or_create(dir, dentry, &args);
20961+ AuTraceErr(err);
20962+ AuDbgFile(args.file);
20963+ if (unlikely(err < 0)) {
20964+ if (h_opened & FILE_OPENED)
20965+ fput(args.file);
20966+ else
20967+ put_filp(args.file);
20968+ goto out_unlock;
20969+ }
8b6a4947
AM
20970+ di_write_unlock(parent);
20971+ di_write_unlock(dentry);
20972+ unlocked = 1;
b912730e
AM
20973+
20974+ /* some filesystems don't set FILE_CREATED while succeeded? */
20975+ *opened |= FILE_CREATED;
20976+ if (h_opened & FILE_OPENED)
20977+ aopen_node.h_file = args.file;
20978+ else {
20979+ put_filp(args.file);
20980+ args.file = NULL;
20981+ }
20982+ aopen = &au_sbi(dir->i_sb)->si_aopen;
8b6a4947 20983+ au_hbl_add(&aopen_node.hblist, aopen);
b912730e 20984+ err = finish_open(file, dentry, au_do_aopen, opened);
8b6a4947 20985+ au_hbl_del(&aopen_node.hblist, aopen);
b912730e
AM
20986+ AuTraceErr(err);
20987+ AuDbgFile(file);
20988+ if (aopen_node.h_file)
20989+ fput(aopen_node.h_file);
20990+
20991+out_unlock:
8b6a4947
AM
20992+ if (unlocked)
20993+ si_read_unlock(dentry->d_sb);
20994+ else {
20995+ di_write_unlock(parent);
20996+ aufs_read_unlock(dentry, AuLock_DW);
20997+ }
b912730e 20998+ AuDbgDentry(dentry);
f0c0a007 20999+ if (unlikely(err < 0))
b912730e
AM
21000+ goto out;
21001+out_no_open:
f0c0a007 21002+ if (err >= 0 && !(*opened & FILE_CREATED)) {
b912730e
AM
21003+ AuLabel(out_no_open);
21004+ dget(dentry);
21005+ err = finish_no_open(file, dentry);
21006+ }
21007+out:
21008+ AuDbg("%pd%s%s\n", dentry,
21009+ (*opened & FILE_CREATED) ? " created" : "",
21010+ (*opened & FILE_OPENED) ? " opened" : "");
21011+ AuTraceErr(err);
21012+ return err;
21013+}
21014+
21015+
21016+/* ---------------------------------------------------------------------- */
21017+
4a4d8108
AM
21018+static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
21019+ const unsigned char add_entry, aufs_bindex_t bcpup,
5afbbe0d 21020+ aufs_bindex_t btop)
4a4d8108
AM
21021+{
21022+ int err;
21023+ struct dentry *h_parent;
21024+ struct inode *h_dir;
1facf9fc 21025+
027c5e7a 21026+ if (add_entry)
5527c038 21027+ IMustLock(d_inode(parent));
027c5e7a 21028+ else
4a4d8108
AM
21029+ di_write_lock_parent(parent);
21030+
21031+ err = 0;
21032+ if (!au_h_dptr(parent, bcpup)) {
5afbbe0d 21033+ if (btop > bcpup)
c2b27bf2 21034+ err = au_cpup_dirs(dentry, bcpup);
5afbbe0d 21035+ else if (btop < bcpup)
4a4d8108
AM
21036+ err = au_cpdown_dirs(dentry, bcpup);
21037+ else
c2b27bf2 21038+ BUG();
4a4d8108 21039+ }
38d290e6 21040+ if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
4a4d8108 21041+ h_parent = au_h_dptr(parent, bcpup);
5527c038 21042+ h_dir = d_inode(h_parent);
be118d29 21043+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
7e9cd9fe 21044+ err = au_lkup_neg(dentry, bcpup, /*wh*/0);
4a4d8108 21045+ /* todo: no unlock here */
3c1bdaff 21046+ inode_unlock_shared(h_dir);
027c5e7a
AM
21047+
21048+ AuDbg("bcpup %d\n", bcpup);
21049+ if (!err) {
5527c038 21050+ if (d_really_is_negative(dentry))
5afbbe0d 21051+ au_set_h_dptr(dentry, btop, NULL);
4a4d8108
AM
21052+ au_update_dbrange(dentry, /*do_put_zero*/0);
21053+ }
1308ab2a 21054+ }
1facf9fc 21055+
4a4d8108
AM
21056+ if (!add_entry)
21057+ di_write_unlock(parent);
21058+ if (!err)
21059+ err = bcpup; /* success */
1308ab2a 21060+
027c5e7a 21061+ AuTraceErr(err);
4a4d8108
AM
21062+ return err;
21063+}
1facf9fc 21064+
4a4d8108
AM
21065+/*
21066+ * decide the branch and the parent dir where we will create a new entry.
21067+ * returns new bindex or an error.
21068+ * copyup the parent dir if needed.
21069+ */
21070+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
21071+ struct au_wr_dir_args *args)
21072+{
21073+ int err;
392086de 21074+ unsigned int flags;
5afbbe0d 21075+ aufs_bindex_t bcpup, btop, src_btop;
86dc4139
AM
21076+ const unsigned char add_entry
21077+ = au_ftest_wrdir(args->flags, ADD_ENTRY)
38d290e6 21078+ | au_ftest_wrdir(args->flags, TMPFILE);
4a4d8108
AM
21079+ struct super_block *sb;
21080+ struct dentry *parent;
21081+ struct au_sbinfo *sbinfo;
1facf9fc 21082+
4a4d8108
AM
21083+ sb = dentry->d_sb;
21084+ sbinfo = au_sbi(sb);
21085+ parent = dget_parent(dentry);
5afbbe0d
AM
21086+ btop = au_dbtop(dentry);
21087+ bcpup = btop;
4a4d8108
AM
21088+ if (args->force_btgt < 0) {
21089+ if (src_dentry) {
5afbbe0d
AM
21090+ src_btop = au_dbtop(src_dentry);
21091+ if (src_btop < btop)
21092+ bcpup = src_btop;
4a4d8108 21093+ } else if (add_entry) {
392086de
AM
21094+ flags = 0;
21095+ if (au_ftest_wrdir(args->flags, ISDIR))
21096+ au_fset_wbr(flags, DIR);
21097+ err = AuWbrCreate(sbinfo, dentry, flags);
4a4d8108
AM
21098+ bcpup = err;
21099+ }
1facf9fc 21100+
5527c038 21101+ if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
4a4d8108
AM
21102+ if (add_entry)
21103+ err = AuWbrCopyup(sbinfo, dentry);
21104+ else {
21105+ if (!IS_ROOT(dentry)) {
21106+ di_read_lock_parent(parent, !AuLock_IR);
21107+ err = AuWbrCopyup(sbinfo, dentry);
21108+ di_read_unlock(parent, !AuLock_IR);
21109+ } else
21110+ err = AuWbrCopyup(sbinfo, dentry);
21111+ }
21112+ bcpup = err;
21113+ if (unlikely(err < 0))
21114+ goto out;
21115+ }
21116+ } else {
21117+ bcpup = args->force_btgt;
5527c038 21118+ AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
1308ab2a 21119+ }
027c5e7a 21120+
5afbbe0d 21121+ AuDbg("btop %d, bcpup %d\n", btop, bcpup);
4a4d8108 21122+ err = bcpup;
5afbbe0d 21123+ if (bcpup == btop)
4a4d8108 21124+ goto out; /* success */
4a4d8108
AM
21125+
21126+ /* copyup the new parent into the branch we process */
5afbbe0d 21127+ err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
027c5e7a 21128+ if (err >= 0) {
5527c038 21129+ if (d_really_is_negative(dentry)) {
5afbbe0d
AM
21130+ au_set_h_dptr(dentry, btop, NULL);
21131+ au_set_dbtop(dentry, bcpup);
21132+ au_set_dbbot(dentry, bcpup);
027c5e7a 21133+ }
38d290e6
JR
21134+ AuDebugOn(add_entry
21135+ && !au_ftest_wrdir(args->flags, TMPFILE)
21136+ && !au_h_dptr(dentry, bcpup));
027c5e7a 21137+ }
86dc4139
AM
21138+
21139+out:
21140+ dput(parent);
21141+ return err;
21142+}
21143+
21144+/* ---------------------------------------------------------------------- */
21145+
21146+void au_pin_hdir_unlock(struct au_pin *p)
21147+{
21148+ if (p->hdir)
5afbbe0d 21149+ au_hn_inode_unlock(p->hdir);
86dc4139
AM
21150+}
21151+
c1595e42 21152+int au_pin_hdir_lock(struct au_pin *p)
86dc4139
AM
21153+{
21154+ int err;
21155+
21156+ err = 0;
21157+ if (!p->hdir)
21158+ goto out;
21159+
21160+ /* even if an error happens later, keep this lock */
5afbbe0d 21161+ au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
86dc4139
AM
21162+
21163+ err = -EBUSY;
5527c038 21164+ if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
86dc4139
AM
21165+ goto out;
21166+
21167+ err = 0;
21168+ if (p->h_dentry)
21169+ err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
21170+ p->h_parent, p->br);
21171+
21172+out:
21173+ return err;
21174+}
21175+
21176+int au_pin_hdir_relock(struct au_pin *p)
21177+{
21178+ int err, i;
21179+ struct inode *h_i;
21180+ struct dentry *h_d[] = {
21181+ p->h_dentry,
21182+ p->h_parent
21183+ };
21184+
21185+ err = au_pin_hdir_lock(p);
21186+ if (unlikely(err))
21187+ goto out;
21188+
21189+ for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
21190+ if (!h_d[i])
21191+ continue;
5527c038
JR
21192+ if (d_is_positive(h_d[i])) {
21193+ h_i = d_inode(h_d[i]);
86dc4139 21194+ err = !h_i->i_nlink;
5527c038 21195+ }
86dc4139
AM
21196+ }
21197+
21198+out:
21199+ return err;
21200+}
21201+
5afbbe0d 21202+static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
86dc4139 21203+{
5afbbe0d
AM
21204+#if !defined(CONFIG_RWSEM_GENERIC_SPINLOCK) && defined(CONFIG_RWSEM_SPIN_ON_OWNER)
21205+ p->hdir->hi_inode->i_rwsem.owner = task;
86dc4139
AM
21206+#endif
21207+}
21208+
21209+void au_pin_hdir_acquire_nest(struct au_pin *p)
21210+{
21211+ if (p->hdir) {
5afbbe0d 21212+ rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
86dc4139
AM
21213+ p->lsc_hi, 0, NULL, _RET_IP_);
21214+ au_pin_hdir_set_owner(p, current);
21215+ }
dece6358 21216+}
1facf9fc 21217+
86dc4139
AM
21218+void au_pin_hdir_release(struct au_pin *p)
21219+{
21220+ if (p->hdir) {
21221+ au_pin_hdir_set_owner(p, p->task);
5afbbe0d 21222+ rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, 1, _RET_IP_);
86dc4139
AM
21223+ }
21224+}
1308ab2a 21225+
4a4d8108 21226+struct dentry *au_pinned_h_parent(struct au_pin *pin)
1308ab2a 21227+{
4a4d8108
AM
21228+ if (pin && pin->parent)
21229+ return au_h_dptr(pin->parent, pin->bindex);
21230+ return NULL;
dece6358 21231+}
1facf9fc 21232+
4a4d8108 21233+void au_unpin(struct au_pin *p)
dece6358 21234+{
86dc4139
AM
21235+ if (p->hdir)
21236+ au_pin_hdir_unlock(p);
e49829fe 21237+ if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
b4510431 21238+ vfsub_mnt_drop_write(p->h_mnt);
4a4d8108
AM
21239+ if (!p->hdir)
21240+ return;
1facf9fc 21241+
4a4d8108
AM
21242+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21243+ di_read_unlock(p->parent, AuLock_IR);
21244+ iput(p->hdir->hi_inode);
21245+ dput(p->parent);
21246+ p->parent = NULL;
21247+ p->hdir = NULL;
21248+ p->h_mnt = NULL;
86dc4139 21249+ /* do not clear p->task */
4a4d8108 21250+}
1308ab2a 21251+
4a4d8108
AM
21252+int au_do_pin(struct au_pin *p)
21253+{
21254+ int err;
21255+ struct super_block *sb;
4a4d8108
AM
21256+ struct inode *h_dir;
21257+
21258+ err = 0;
21259+ sb = p->dentry->d_sb;
86dc4139 21260+ p->br = au_sbr(sb, p->bindex);
4a4d8108
AM
21261+ if (IS_ROOT(p->dentry)) {
21262+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 21263+ p->h_mnt = au_br_mnt(p->br);
b4510431 21264+ err = vfsub_mnt_want_write(p->h_mnt);
4a4d8108
AM
21265+ if (unlikely(err)) {
21266+ au_fclr_pin(p->flags, MNT_WRITE);
21267+ goto out_err;
21268+ }
21269+ }
dece6358 21270+ goto out;
1facf9fc 21271+ }
21272+
86dc4139 21273+ p->h_dentry = NULL;
5afbbe0d 21274+ if (p->bindex <= au_dbbot(p->dentry))
86dc4139 21275+ p->h_dentry = au_h_dptr(p->dentry, p->bindex);
dece6358 21276+
4a4d8108
AM
21277+ p->parent = dget_parent(p->dentry);
21278+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21279+ di_read_lock(p->parent, AuLock_IR, p->lsc_di);
dece6358 21280+
4a4d8108 21281+ h_dir = NULL;
86dc4139 21282+ p->h_parent = au_h_dptr(p->parent, p->bindex);
5527c038 21283+ p->hdir = au_hi(d_inode(p->parent), p->bindex);
4a4d8108
AM
21284+ if (p->hdir)
21285+ h_dir = p->hdir->hi_inode;
dece6358 21286+
b752ccd1
AM
21287+ /*
21288+ * udba case, or
21289+ * if DI_LOCKED is not set, then p->parent may be different
21290+ * and h_parent can be NULL.
21291+ */
86dc4139 21292+ if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
e49829fe 21293+ err = -EBUSY;
4a4d8108
AM
21294+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21295+ di_read_unlock(p->parent, AuLock_IR);
21296+ dput(p->parent);
21297+ p->parent = NULL;
21298+ goto out_err;
21299+ }
1308ab2a 21300+
4a4d8108 21301+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 21302+ p->h_mnt = au_br_mnt(p->br);
b4510431 21303+ err = vfsub_mnt_want_write(p->h_mnt);
dece6358 21304+ if (unlikely(err)) {
4a4d8108 21305+ au_fclr_pin(p->flags, MNT_WRITE);
86dc4139
AM
21306+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21307+ di_read_unlock(p->parent, AuLock_IR);
21308+ dput(p->parent);
21309+ p->parent = NULL;
21310+ goto out_err;
dece6358
AM
21311+ }
21312+ }
4a4d8108 21313+
86dc4139
AM
21314+ au_igrab(h_dir);
21315+ err = au_pin_hdir_lock(p);
21316+ if (!err)
21317+ goto out; /* success */
21318+
076b876e
AM
21319+ au_unpin(p);
21320+
4f0767ce 21321+out_err:
4a4d8108
AM
21322+ pr_err("err %d\n", err);
21323+ err = au_busy_or_stale();
4f0767ce 21324+out:
1facf9fc 21325+ return err;
21326+}
21327+
4a4d8108
AM
21328+void au_pin_init(struct au_pin *p, struct dentry *dentry,
21329+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
21330+ unsigned int udba, unsigned char flags)
21331+{
21332+ p->dentry = dentry;
21333+ p->udba = udba;
21334+ p->lsc_di = lsc_di;
21335+ p->lsc_hi = lsc_hi;
21336+ p->flags = flags;
21337+ p->bindex = bindex;
21338+
21339+ p->parent = NULL;
21340+ p->hdir = NULL;
21341+ p->h_mnt = NULL;
86dc4139
AM
21342+
21343+ p->h_dentry = NULL;
21344+ p->h_parent = NULL;
21345+ p->br = NULL;
21346+ p->task = current;
4a4d8108
AM
21347+}
21348+
21349+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
21350+ unsigned int udba, unsigned char flags)
21351+{
21352+ au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
21353+ udba, flags);
21354+ return au_do_pin(pin);
21355+}
21356+
dece6358
AM
21357+/* ---------------------------------------------------------------------- */
21358+
1308ab2a 21359+/*
4a4d8108
AM
21360+ * ->setattr() and ->getattr() are called in various cases.
21361+ * chmod, stat: dentry is revalidated.
21362+ * fchmod, fstat: file and dentry are not revalidated, additionally they may be
21363+ * unhashed.
21364+ * for ->setattr(), ia->ia_file is passed from ftruncate only.
1308ab2a 21365+ */
027c5e7a 21366+/* todo: consolidate with do_refresh() and simple_reval_dpath() */
c1595e42 21367+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
1facf9fc 21368+{
4a4d8108 21369+ int err;
4a4d8108 21370+ struct dentry *parent;
1facf9fc 21371+
1308ab2a 21372+ err = 0;
027c5e7a 21373+ if (au_digen_test(dentry, sigen)) {
4a4d8108
AM
21374+ parent = dget_parent(dentry);
21375+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 21376+ err = au_refresh_dentry(dentry, parent);
4a4d8108
AM
21377+ di_read_unlock(parent, AuLock_IR);
21378+ dput(parent);
dece6358 21379+ }
1facf9fc 21380+
4a4d8108 21381+ AuTraceErr(err);
1308ab2a 21382+ return err;
21383+}
dece6358 21384+
c1595e42
JR
21385+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
21386+ struct au_icpup_args *a)
1308ab2a 21387+{
21388+ int err;
4a4d8108 21389+ loff_t sz;
5afbbe0d 21390+ aufs_bindex_t btop, ibtop;
4a4d8108
AM
21391+ struct dentry *hi_wh, *parent;
21392+ struct inode *inode;
4a4d8108
AM
21393+ struct au_wr_dir_args wr_dir_args = {
21394+ .force_btgt = -1,
21395+ .flags = 0
21396+ };
21397+
2000de60 21398+ if (d_is_dir(dentry))
4a4d8108
AM
21399+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
21400+ /* plink or hi_wh() case */
5afbbe0d 21401+ btop = au_dbtop(dentry);
5527c038 21402+ inode = d_inode(dentry);
5afbbe0d
AM
21403+ ibtop = au_ibtop(inode);
21404+ if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
21405+ wr_dir_args.force_btgt = ibtop;
4a4d8108
AM
21406+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21407+ if (unlikely(err < 0))
21408+ goto out;
21409+ a->btgt = err;
5afbbe0d 21410+ if (err != btop)
4a4d8108
AM
21411+ au_fset_icpup(a->flags, DID_CPUP);
21412+
21413+ err = 0;
21414+ a->pin_flags = AuPin_MNT_WRITE;
21415+ parent = NULL;
21416+ if (!IS_ROOT(dentry)) {
21417+ au_fset_pin(a->pin_flags, DI_LOCKED);
21418+ parent = dget_parent(dentry);
21419+ di_write_lock_parent(parent);
21420+ }
21421+
21422+ err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
21423+ if (unlikely(err))
21424+ goto out_parent;
21425+
4a4d8108 21426+ sz = -1;
5afbbe0d 21427+ a->h_path.dentry = au_h_dptr(dentry, btop);
5527c038 21428+ a->h_inode = d_inode(a->h_path.dentry);
c1595e42 21429+ if (ia && (ia->ia_valid & ATTR_SIZE)) {
be118d29 21430+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
c1595e42
JR
21431+ if (ia->ia_size < i_size_read(a->h_inode))
21432+ sz = ia->ia_size;
3c1bdaff 21433+ inode_unlock_shared(a->h_inode);
c1595e42 21434+ }
4a4d8108 21435+
4a4d8108 21436+ hi_wh = NULL;
027c5e7a 21437+ if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
4a4d8108
AM
21438+ hi_wh = au_hi_wh(inode, a->btgt);
21439+ if (!hi_wh) {
c2b27bf2
AM
21440+ struct au_cp_generic cpg = {
21441+ .dentry = dentry,
21442+ .bdst = a->btgt,
21443+ .bsrc = -1,
21444+ .len = sz,
21445+ .pin = &a->pin
21446+ };
21447+ err = au_sio_cpup_wh(&cpg, /*file*/NULL);
4a4d8108
AM
21448+ if (unlikely(err))
21449+ goto out_unlock;
21450+ hi_wh = au_hi_wh(inode, a->btgt);
21451+ /* todo: revalidate hi_wh? */
21452+ }
21453+ }
21454+
21455+ if (parent) {
21456+ au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
21457+ di_downgrade_lock(parent, AuLock_IR);
21458+ dput(parent);
21459+ parent = NULL;
21460+ }
21461+ if (!au_ftest_icpup(a->flags, DID_CPUP))
21462+ goto out; /* success */
21463+
21464+ if (!d_unhashed(dentry)) {
c2b27bf2
AM
21465+ struct au_cp_generic cpg = {
21466+ .dentry = dentry,
21467+ .bdst = a->btgt,
5afbbe0d 21468+ .bsrc = btop,
c2b27bf2
AM
21469+ .len = sz,
21470+ .pin = &a->pin,
21471+ .flags = AuCpup_DTIME | AuCpup_HOPEN
21472+ };
21473+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
21474+ if (!err)
21475+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21476+ } else if (!hi_wh)
21477+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21478+ else
21479+ a->h_path.dentry = hi_wh; /* do not dget here */
1308ab2a 21480+
4f0767ce 21481+out_unlock:
5527c038 21482+ a->h_inode = d_inode(a->h_path.dentry);
86dc4139 21483+ if (!err)
dece6358 21484+ goto out; /* success */
4a4d8108 21485+ au_unpin(&a->pin);
4f0767ce 21486+out_parent:
4a4d8108
AM
21487+ if (parent) {
21488+ di_write_unlock(parent);
21489+ dput(parent);
21490+ }
4f0767ce 21491+out:
86dc4139 21492+ if (!err)
febd17d6 21493+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
1facf9fc 21494+ return err;
21495+}
21496+
4a4d8108 21497+static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
1facf9fc 21498+{
4a4d8108 21499+ int err;
523b37e3 21500+ struct inode *inode, *delegated;
4a4d8108
AM
21501+ struct super_block *sb;
21502+ struct file *file;
21503+ struct au_icpup_args *a;
1facf9fc 21504+
5527c038 21505+ inode = d_inode(dentry);
4a4d8108 21506+ IMustLock(inode);
dece6358 21507+
f2c43d5f
AM
21508+ err = setattr_prepare(dentry, ia);
21509+ if (unlikely(err))
21510+ goto out;
21511+
4a4d8108
AM
21512+ err = -ENOMEM;
21513+ a = kzalloc(sizeof(*a), GFP_NOFS);
21514+ if (unlikely(!a))
21515+ goto out;
1facf9fc 21516+
4a4d8108
AM
21517+ if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
21518+ ia->ia_valid &= ~ATTR_MODE;
dece6358 21519+
4a4d8108
AM
21520+ file = NULL;
21521+ sb = dentry->d_sb;
e49829fe
JR
21522+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21523+ if (unlikely(err))
21524+ goto out_kfree;
21525+
4a4d8108
AM
21526+ if (ia->ia_valid & ATTR_FILE) {
21527+ /* currently ftruncate(2) only */
7e9cd9fe 21528+ AuDebugOn(!d_is_reg(dentry));
4a4d8108 21529+ file = ia->ia_file;
521ced18
JR
21530+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
21531+ /*fi_lsc*/0);
4a4d8108
AM
21532+ if (unlikely(err))
21533+ goto out_si;
21534+ ia->ia_file = au_hf_top(file);
21535+ a->udba = AuOpt_UDBA_NONE;
21536+ } else {
21537+ /* fchmod() doesn't pass ia_file */
21538+ a->udba = au_opt_udba(sb);
027c5e7a
AM
21539+ di_write_lock_child(dentry);
21540+ /* no d_unlinked(), to set UDBA_NONE for root */
4a4d8108
AM
21541+ if (d_unhashed(dentry))
21542+ a->udba = AuOpt_UDBA_NONE;
4a4d8108
AM
21543+ if (a->udba != AuOpt_UDBA_NONE) {
21544+ AuDebugOn(IS_ROOT(dentry));
21545+ err = au_reval_for_attr(dentry, au_sigen(sb));
21546+ if (unlikely(err))
21547+ goto out_dentry;
21548+ }
dece6358 21549+ }
dece6358 21550+
4a4d8108
AM
21551+ err = au_pin_and_icpup(dentry, ia, a);
21552+ if (unlikely(err < 0))
21553+ goto out_dentry;
21554+ if (au_ftest_icpup(a->flags, DID_CPUP)) {
21555+ ia->ia_file = NULL;
21556+ ia->ia_valid &= ~ATTR_FILE;
1308ab2a 21557+ }
dece6358 21558+
4a4d8108
AM
21559+ a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
21560+ if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
21561+ == (ATTR_MODE | ATTR_CTIME)) {
7eafdf33 21562+ err = security_path_chmod(&a->h_path, ia->ia_mode);
4a4d8108
AM
21563+ if (unlikely(err))
21564+ goto out_unlock;
21565+ } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
21566+ && (ia->ia_valid & ATTR_CTIME)) {
86dc4139 21567+ err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
4a4d8108
AM
21568+ if (unlikely(err))
21569+ goto out_unlock;
21570+ }
dece6358 21571+
4a4d8108
AM
21572+ if (ia->ia_valid & ATTR_SIZE) {
21573+ struct file *f;
1308ab2a 21574+
953406b4 21575+ if (ia->ia_size < i_size_read(inode))
4a4d8108 21576+ /* unmap only */
953406b4 21577+ truncate_setsize(inode, ia->ia_size);
1308ab2a 21578+
4a4d8108
AM
21579+ f = NULL;
21580+ if (ia->ia_valid & ATTR_FILE)
21581+ f = ia->ia_file;
febd17d6 21582+ inode_unlock(a->h_inode);
4a4d8108 21583+ err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
febd17d6 21584+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
523b37e3
AM
21585+ } else {
21586+ delegated = NULL;
21587+ while (1) {
21588+ err = vfsub_notify_change(&a->h_path, ia, &delegated);
21589+ if (delegated) {
21590+ err = break_deleg_wait(&delegated);
21591+ if (!err)
21592+ continue;
21593+ }
21594+ break;
21595+ }
21596+ }
8cdd5066
JR
21597+ /*
21598+ * regardless aufs 'acl' option setting.
21599+ * why don't all acl-aware fs call this func from their ->setattr()?
21600+ */
21601+ if (!err && (ia->ia_valid & ATTR_MODE))
21602+ err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
4a4d8108
AM
21603+ if (!err)
21604+ au_cpup_attr_changeable(inode);
1308ab2a 21605+
4f0767ce 21606+out_unlock:
febd17d6 21607+ inode_unlock(a->h_inode);
4a4d8108 21608+ au_unpin(&a->pin);
027c5e7a 21609+ if (unlikely(err))
5afbbe0d 21610+ au_update_dbtop(dentry);
4f0767ce 21611+out_dentry:
4a4d8108
AM
21612+ di_write_unlock(dentry);
21613+ if (file) {
21614+ fi_write_unlock(file);
21615+ ia->ia_file = file;
21616+ ia->ia_valid |= ATTR_FILE;
21617+ }
4f0767ce 21618+out_si:
4a4d8108 21619+ si_read_unlock(sb);
e49829fe 21620+out_kfree:
1c60b727 21621+ kfree(a);
4f0767ce 21622+out:
4a4d8108
AM
21623+ AuTraceErr(err);
21624+ return err;
1facf9fc 21625+}
21626+
c1595e42
JR
21627+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
21628+static int au_h_path_to_set_attr(struct dentry *dentry,
21629+ struct au_icpup_args *a, struct path *h_path)
21630+{
21631+ int err;
21632+ struct super_block *sb;
21633+
21634+ sb = dentry->d_sb;
21635+ a->udba = au_opt_udba(sb);
21636+ /* no d_unlinked(), to set UDBA_NONE for root */
21637+ if (d_unhashed(dentry))
21638+ a->udba = AuOpt_UDBA_NONE;
21639+ if (a->udba != AuOpt_UDBA_NONE) {
21640+ AuDebugOn(IS_ROOT(dentry));
21641+ err = au_reval_for_attr(dentry, au_sigen(sb));
21642+ if (unlikely(err))
21643+ goto out;
21644+ }
21645+ err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
21646+ if (unlikely(err < 0))
21647+ goto out;
21648+
21649+ h_path->dentry = a->h_path.dentry;
21650+ h_path->mnt = au_sbr_mnt(sb, a->btgt);
21651+
21652+out:
21653+ return err;
21654+}
21655+
f2c43d5f
AM
21656+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
21657+ struct au_sxattr *arg)
c1595e42
JR
21658+{
21659+ int err;
21660+ struct path h_path;
21661+ struct super_block *sb;
21662+ struct au_icpup_args *a;
5afbbe0d 21663+ struct inode *h_inode;
c1595e42 21664+
c1595e42
JR
21665+ IMustLock(inode);
21666+
21667+ err = -ENOMEM;
21668+ a = kzalloc(sizeof(*a), GFP_NOFS);
21669+ if (unlikely(!a))
21670+ goto out;
21671+
21672+ sb = dentry->d_sb;
21673+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21674+ if (unlikely(err))
21675+ goto out_kfree;
21676+
21677+ h_path.dentry = NULL; /* silence gcc */
21678+ di_write_lock_child(dentry);
21679+ err = au_h_path_to_set_attr(dentry, a, &h_path);
21680+ if (unlikely(err))
21681+ goto out_di;
21682+
febd17d6 21683+ inode_unlock(a->h_inode);
c1595e42
JR
21684+ switch (arg->type) {
21685+ case AU_XATTR_SET:
5afbbe0d 21686+ AuDebugOn(d_is_negative(h_path.dentry));
c1595e42
JR
21687+ err = vfsub_setxattr(h_path.dentry,
21688+ arg->u.set.name, arg->u.set.value,
21689+ arg->u.set.size, arg->u.set.flags);
21690+ break;
c1595e42
JR
21691+ case AU_ACL_SET:
21692+ err = -EOPNOTSUPP;
5527c038 21693+ h_inode = d_inode(h_path.dentry);
c1595e42 21694+ if (h_inode->i_op->set_acl)
f2c43d5f 21695+ /* this will call posix_acl_update_mode */
c1595e42
JR
21696+ err = h_inode->i_op->set_acl(h_inode,
21697+ arg->u.acl_set.acl,
21698+ arg->u.acl_set.type);
21699+ break;
21700+ }
21701+ if (!err)
21702+ au_cpup_attr_timesizes(inode);
21703+
21704+ au_unpin(&a->pin);
21705+ if (unlikely(err))
5afbbe0d 21706+ au_update_dbtop(dentry);
c1595e42
JR
21707+
21708+out_di:
21709+ di_write_unlock(dentry);
21710+ si_read_unlock(sb);
21711+out_kfree:
1c60b727 21712+ kfree(a);
c1595e42
JR
21713+out:
21714+ AuTraceErr(err);
21715+ return err;
21716+}
21717+#endif
21718+
4a4d8108
AM
21719+static void au_refresh_iattr(struct inode *inode, struct kstat *st,
21720+ unsigned int nlink)
1facf9fc 21721+{
9dbd164d
AM
21722+ unsigned int n;
21723+
4a4d8108 21724+ inode->i_mode = st->mode;
86dc4139
AM
21725+ /* don't i_[ug]id_write() here */
21726+ inode->i_uid = st->uid;
21727+ inode->i_gid = st->gid;
4a4d8108
AM
21728+ inode->i_atime = st->atime;
21729+ inode->i_mtime = st->mtime;
21730+ inode->i_ctime = st->ctime;
1facf9fc 21731+
4a4d8108
AM
21732+ au_cpup_attr_nlink(inode, /*force*/0);
21733+ if (S_ISDIR(inode->i_mode)) {
9dbd164d
AM
21734+ n = inode->i_nlink;
21735+ n -= nlink;
21736+ n += st->nlink;
f6b6e03d 21737+ smp_mb(); /* for i_nlink */
7eafdf33 21738+ /* 0 can happen */
92d182d2 21739+ set_nlink(inode, n);
4a4d8108 21740+ }
1facf9fc 21741+
4a4d8108
AM
21742+ spin_lock(&inode->i_lock);
21743+ inode->i_blocks = st->blocks;
21744+ i_size_write(inode, st->size);
21745+ spin_unlock(&inode->i_lock);
1facf9fc 21746+}
21747+
c1595e42 21748+/*
f2c43d5f 21749+ * common routine for aufs_getattr() and au_getxattr().
c1595e42
JR
21750+ * returns zero or negative (an error).
21751+ * @dentry will be read-locked in success.
21752+ */
a2654f78
AM
21753+int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
21754+ int locked)
1facf9fc 21755+{
4a4d8108 21756+ int err;
076b876e 21757+ unsigned int mnt_flags, sigen;
c1595e42 21758+ unsigned char udba_none;
4a4d8108 21759+ aufs_bindex_t bindex;
4a4d8108
AM
21760+ struct super_block *sb, *h_sb;
21761+ struct inode *inode;
1facf9fc 21762+
c1595e42
JR
21763+ h_path->mnt = NULL;
21764+ h_path->dentry = NULL;
21765+
21766+ err = 0;
4a4d8108 21767+ sb = dentry->d_sb;
4a4d8108
AM
21768+ mnt_flags = au_mntflags(sb);
21769+ udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
1facf9fc 21770+
a2654f78
AM
21771+ if (unlikely(locked))
21772+ goto body; /* skip locking dinfo */
21773+
4a4d8108 21774+ /* support fstat(2) */
027c5e7a 21775+ if (!d_unlinked(dentry) && !udba_none) {
076b876e 21776+ sigen = au_sigen(sb);
027c5e7a
AM
21777+ err = au_digen_test(dentry, sigen);
21778+ if (!err) {
4a4d8108 21779+ di_read_lock_child(dentry, AuLock_IR);
027c5e7a 21780+ err = au_dbrange_test(dentry);
c1595e42
JR
21781+ if (unlikely(err)) {
21782+ di_read_unlock(dentry, AuLock_IR);
21783+ goto out;
21784+ }
027c5e7a 21785+ } else {
4a4d8108
AM
21786+ AuDebugOn(IS_ROOT(dentry));
21787+ di_write_lock_child(dentry);
027c5e7a
AM
21788+ err = au_dbrange_test(dentry);
21789+ if (!err)
21790+ err = au_reval_for_attr(dentry, sigen);
c1595e42
JR
21791+ if (!err)
21792+ di_downgrade_lock(dentry, AuLock_IR);
21793+ else {
21794+ di_write_unlock(dentry);
21795+ goto out;
21796+ }
4a4d8108
AM
21797+ }
21798+ } else
21799+ di_read_lock_child(dentry, AuLock_IR);
1facf9fc 21800+
a2654f78 21801+body:
5527c038 21802+ inode = d_inode(dentry);
5afbbe0d 21803+ bindex = au_ibtop(inode);
c1595e42
JR
21804+ h_path->mnt = au_sbr_mnt(sb, bindex);
21805+ h_sb = h_path->mnt->mnt_sb;
21806+ if (!force
21807+ && !au_test_fs_bad_iattr(h_sb)
21808+ && udba_none)
21809+ goto out; /* success */
1facf9fc 21810+
5afbbe0d 21811+ if (au_dbtop(dentry) == bindex)
c1595e42 21812+ h_path->dentry = au_h_dptr(dentry, bindex);
4a4d8108 21813+ else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
c1595e42
JR
21814+ h_path->dentry = au_plink_lkup(inode, bindex);
21815+ if (IS_ERR(h_path->dentry))
21816+ /* pretending success */
21817+ h_path->dentry = NULL;
21818+ else
21819+ dput(h_path->dentry);
4a4d8108 21820+ }
c1595e42
JR
21821+
21822+out:
21823+ return err;
21824+}
21825+
521ced18
JR
21826+static int aufs_getattr(const struct path *path, struct kstat *st,
21827+ u32 request, unsigned int query)
c1595e42
JR
21828+{
21829+ int err;
21830+ unsigned char positive;
21831+ struct path h_path;
521ced18 21832+ struct dentry *dentry;
c1595e42
JR
21833+ struct inode *inode;
21834+ struct super_block *sb;
21835+
521ced18 21836+ dentry = path->dentry;
5527c038 21837+ inode = d_inode(dentry);
c1595e42
JR
21838+ sb = dentry->d_sb;
21839+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21840+ if (unlikely(err))
21841+ goto out;
a2654f78 21842+ err = au_h_path_getattr(dentry, /*force*/0, &h_path, /*locked*/0);
c1595e42
JR
21843+ if (unlikely(err))
21844+ goto out_si;
c06a8ce3 21845+ if (unlikely(!h_path.dentry))
c1595e42 21846+ /* illegally overlapped or something */
4a4d8108
AM
21847+ goto out_fill; /* pretending success */
21848+
5527c038 21849+ positive = d_is_positive(h_path.dentry);
4a4d8108 21850+ if (positive)
521ced18
JR
21851+ /* no vfsub version */
21852+ err = vfs_getattr(&h_path, st, request, query);
4a4d8108
AM
21853+ if (!err) {
21854+ if (positive)
c06a8ce3 21855+ au_refresh_iattr(inode, st,
5527c038 21856+ d_inode(h_path.dentry)->i_nlink);
4a4d8108 21857+ goto out_fill; /* success */
1facf9fc 21858+ }
7f207e10 21859+ AuTraceErr(err);
c1595e42 21860+ goto out_di;
4a4d8108 21861+
4f0767ce 21862+out_fill:
4a4d8108 21863+ generic_fillattr(inode, st);
c1595e42 21864+out_di:
4a4d8108 21865+ di_read_unlock(dentry, AuLock_IR);
c1595e42 21866+out_si:
4a4d8108 21867+ si_read_unlock(sb);
7f207e10
AM
21868+out:
21869+ AuTraceErr(err);
4a4d8108 21870+ return err;
1facf9fc 21871+}
21872+
21873+/* ---------------------------------------------------------------------- */
21874+
febd17d6
JR
21875+static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
21876+ struct delayed_call *done)
4a4d8108 21877+{
c2c0f25c 21878+ const char *ret;
c2c0f25c 21879+ struct dentry *h_dentry;
febd17d6 21880+ struct inode *h_inode;
4a4d8108 21881+ int err;
c2c0f25c 21882+ aufs_bindex_t bindex;
1facf9fc 21883+
79b8bda9 21884+ ret = NULL; /* suppress a warning */
febd17d6
JR
21885+ err = -ECHILD;
21886+ if (!dentry)
21887+ goto out;
21888+
027c5e7a
AM
21889+ err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
21890+ if (unlikely(err))
c2c0f25c 21891+ goto out;
027c5e7a
AM
21892+
21893+ err = au_d_hashed_positive(dentry);
c2c0f25c
AM
21894+ if (unlikely(err))
21895+ goto out_unlock;
21896+
21897+ err = -EINVAL;
21898+ inode = d_inode(dentry);
5afbbe0d 21899+ bindex = au_ibtop(inode);
c2c0f25c 21900+ h_inode = au_h_iptr(inode, bindex);
febd17d6 21901+ if (unlikely(!h_inode->i_op->get_link))
c2c0f25c
AM
21902+ goto out_unlock;
21903+
21904+ err = -EBUSY;
21905+ h_dentry = NULL;
5afbbe0d 21906+ if (au_dbtop(dentry) <= bindex) {
c2c0f25c
AM
21907+ h_dentry = au_h_dptr(dentry, bindex);
21908+ if (h_dentry)
21909+ dget(h_dentry);
027c5e7a 21910+ }
c2c0f25c
AM
21911+ if (!h_dentry) {
21912+ h_dentry = d_find_any_alias(h_inode);
21913+ if (IS_ERR(h_dentry)) {
21914+ err = PTR_ERR(h_dentry);
febd17d6 21915+ goto out_unlock;
c2c0f25c
AM
21916+ }
21917+ }
21918+ if (unlikely(!h_dentry))
febd17d6 21919+ goto out_unlock;
1facf9fc 21920+
c2c0f25c 21921+ err = 0;
062440b3 21922+ AuDbg("%ps\n", h_inode->i_op->get_link);
c2c0f25c 21923+ AuDbgDentry(h_dentry);
f2c43d5f 21924+ ret = vfs_get_link(h_dentry, done);
c2c0f25c 21925+ dput(h_dentry);
febd17d6
JR
21926+ if (IS_ERR(ret))
21927+ err = PTR_ERR(ret);
c2c0f25c 21928+
c2c0f25c
AM
21929+out_unlock:
21930+ aufs_read_unlock(dentry, AuLock_IR);
4f0767ce 21931+out:
c2c0f25c
AM
21932+ if (unlikely(err))
21933+ ret = ERR_PTR(err);
21934+ AuTraceErrPtr(ret);
21935+ return ret;
4a4d8108 21936+}
1facf9fc 21937+
4a4d8108 21938+/* ---------------------------------------------------------------------- */
1facf9fc 21939+
e2f27e51
AM
21940+static int au_is_special(struct inode *inode)
21941+{
21942+ return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
21943+}
21944+
cd7a4cd9 21945+static int aufs_update_time(struct inode *inode, struct timespec64 *ts, int flags)
4a4d8108 21946+{
0c3ec466 21947+ int err;
e2f27e51 21948+ aufs_bindex_t bindex;
0c3ec466
AM
21949+ struct super_block *sb;
21950+ struct inode *h_inode;
e2f27e51 21951+ struct vfsmount *h_mnt;
0c3ec466
AM
21952+
21953+ sb = inode->i_sb;
e2f27e51
AM
21954+ WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
21955+ "unexpected s_flags 0x%lx", sb->s_flags);
21956+
0c3ec466
AM
21957+ /* mmap_sem might be acquired already, cf. aufs_mmap() */
21958+ lockdep_off();
21959+ si_read_lock(sb, AuLock_FLUSH);
21960+ ii_write_lock_child(inode);
e2f27e51
AM
21961+
21962+ err = 0;
21963+ bindex = au_ibtop(inode);
21964+ h_inode = au_h_iptr(inode, bindex);
21965+ if (!au_test_ro(sb, bindex, inode)) {
21966+ h_mnt = au_sbr_mnt(sb, bindex);
21967+ err = vfsub_mnt_want_write(h_mnt);
21968+ if (!err) {
21969+ err = vfsub_update_time(h_inode, ts, flags);
21970+ vfsub_mnt_drop_write(h_mnt);
21971+ }
21972+ } else if (au_is_special(h_inode)) {
21973+ /*
21974+ * Never copy-up here.
21975+ * These special files may already be opened and used for
21976+ * communicating. If we copied it up, then the communication
21977+ * would be corrupted.
21978+ */
21979+ AuWarn1("timestamps for i%lu are ignored "
21980+ "since it is on readonly branch (hi%lu).\n",
21981+ inode->i_ino, h_inode->i_ino);
21982+ } else if (flags & ~S_ATIME) {
21983+ err = -EIO;
21984+ AuIOErr1("unexpected flags 0x%x\n", flags);
21985+ AuDebugOn(1);
21986+ }
21987+
38d290e6
JR
21988+ if (!err)
21989+ au_cpup_attr_timesizes(inode);
0c3ec466
AM
21990+ ii_write_unlock(inode);
21991+ si_read_unlock(sb);
21992+ lockdep_on();
38d290e6
JR
21993+
21994+ if (!err && (flags & S_VERSION))
21995+ inode_inc_iversion(inode);
21996+
0c3ec466 21997+ return err;
4a4d8108 21998+}
1facf9fc 21999+
4a4d8108 22000+/* ---------------------------------------------------------------------- */
1308ab2a 22001+
b95c5147
AM
22002+/* no getattr version will be set by module.c:aufs_init() */
22003+struct inode_operations aufs_iop_nogetattr[AuIop_Last],
22004+ aufs_iop[] = {
22005+ [AuIop_SYMLINK] = {
22006+ .permission = aufs_permission,
c1595e42 22007+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
22008+ .get_acl = aufs_get_acl,
22009+ .set_acl = aufs_set_acl, /* unsupport for symlink? */
c1595e42
JR
22010+#endif
22011+
b95c5147
AM
22012+ .setattr = aufs_setattr,
22013+ .getattr = aufs_getattr,
0c3ec466 22014+
c1595e42 22015+#ifdef CONFIG_AUFS_XATTR
b95c5147 22016+ .listxattr = aufs_listxattr,
c1595e42
JR
22017+#endif
22018+
febd17d6 22019+ .get_link = aufs_get_link,
0c3ec466 22020+
b95c5147
AM
22021+ /* .update_time = aufs_update_time */
22022+ },
22023+ [AuIop_DIR] = {
22024+ .create = aufs_create,
22025+ .lookup = aufs_lookup,
22026+ .link = aufs_link,
22027+ .unlink = aufs_unlink,
22028+ .symlink = aufs_symlink,
22029+ .mkdir = aufs_mkdir,
22030+ .rmdir = aufs_rmdir,
22031+ .mknod = aufs_mknod,
22032+ .rename = aufs_rename,
22033+
22034+ .permission = aufs_permission,
c1595e42 22035+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
22036+ .get_acl = aufs_get_acl,
22037+ .set_acl = aufs_set_acl,
c1595e42
JR
22038+#endif
22039+
b95c5147
AM
22040+ .setattr = aufs_setattr,
22041+ .getattr = aufs_getattr,
0c3ec466 22042+
c1595e42 22043+#ifdef CONFIG_AUFS_XATTR
b95c5147 22044+ .listxattr = aufs_listxattr,
c1595e42
JR
22045+#endif
22046+
b95c5147
AM
22047+ .update_time = aufs_update_time,
22048+ .atomic_open = aufs_atomic_open,
22049+ .tmpfile = aufs_tmpfile
22050+ },
22051+ [AuIop_OTHER] = {
22052+ .permission = aufs_permission,
c1595e42 22053+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
22054+ .get_acl = aufs_get_acl,
22055+ .set_acl = aufs_set_acl,
c1595e42
JR
22056+#endif
22057+
b95c5147
AM
22058+ .setattr = aufs_setattr,
22059+ .getattr = aufs_getattr,
0c3ec466 22060+
c1595e42 22061+#ifdef CONFIG_AUFS_XATTR
b95c5147 22062+ .listxattr = aufs_listxattr,
c1595e42
JR
22063+#endif
22064+
b95c5147
AM
22065+ .update_time = aufs_update_time
22066+ }
4a4d8108 22067+};
7f207e10
AM
22068diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
22069--- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 22070+++ linux/fs/aufs/i_op_del.c 2018-08-12 23:43:05.456791299 +0200
062440b3 22071@@ -0,0 +1,512 @@
cd7a4cd9 22072+// SPDX-License-Identifier: GPL-2.0
1facf9fc 22073+/*
b00004a5 22074+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 22075+ *
22076+ * This program, aufs is free software; you can redistribute it and/or modify
22077+ * it under the terms of the GNU General Public License as published by
22078+ * the Free Software Foundation; either version 2 of the License, or
22079+ * (at your option) any later version.
dece6358
AM
22080+ *
22081+ * This program is distributed in the hope that it will be useful,
22082+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22083+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22084+ * GNU General Public License for more details.
22085+ *
22086+ * You should have received a copy of the GNU General Public License
523b37e3 22087+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 22088+ */
22089+
22090+/*
4a4d8108 22091+ * inode operations (del entry)
1308ab2a 22092+ */
dece6358 22093+
1308ab2a 22094+#include "aufs.h"
dece6358 22095+
4a4d8108
AM
22096+/*
22097+ * decide if a new whiteout for @dentry is necessary or not.
22098+ * when it is necessary, prepare the parent dir for the upper branch whose
22099+ * branch index is @bcpup for creation. the actual creation of the whiteout will
22100+ * be done by caller.
22101+ * return value:
22102+ * 0: wh is unnecessary
22103+ * plus: wh is necessary
22104+ * minus: error
22105+ */
22106+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
1308ab2a 22107+{
4a4d8108 22108+ int need_wh, err;
5afbbe0d 22109+ aufs_bindex_t btop;
4a4d8108 22110+ struct super_block *sb;
dece6358 22111+
4a4d8108 22112+ sb = dentry->d_sb;
5afbbe0d 22113+ btop = au_dbtop(dentry);
4a4d8108 22114+ if (*bcpup < 0) {
5afbbe0d
AM
22115+ *bcpup = btop;
22116+ if (au_test_ro(sb, btop, d_inode(dentry))) {
4a4d8108
AM
22117+ err = AuWbrCopyup(au_sbi(sb), dentry);
22118+ *bcpup = err;
22119+ if (unlikely(err < 0))
22120+ goto out;
22121+ }
22122+ } else
5afbbe0d 22123+ AuDebugOn(btop < *bcpup
5527c038 22124+ || au_test_ro(sb, *bcpup, d_inode(dentry)));
5afbbe0d 22125+ AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
1308ab2a 22126+
5afbbe0d 22127+ if (*bcpup != btop) {
4a4d8108
AM
22128+ err = au_cpup_dirs(dentry, *bcpup);
22129+ if (unlikely(err))
22130+ goto out;
22131+ need_wh = 1;
22132+ } else {
027c5e7a 22133+ struct au_dinfo *dinfo, *tmp;
4a4d8108 22134+
027c5e7a
AM
22135+ need_wh = -ENOMEM;
22136+ dinfo = au_di(dentry);
22137+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
22138+ if (tmp) {
22139+ au_di_cp(tmp, dinfo);
22140+ au_di_swap(tmp, dinfo);
22141+ /* returns the number of positive dentries */
5afbbe0d
AM
22142+ need_wh = au_lkup_dentry(dentry, btop + 1,
22143+ /* AuLkup_IGNORE_PERM */ 0);
027c5e7a
AM
22144+ au_di_swap(tmp, dinfo);
22145+ au_rw_write_unlock(&tmp->di_rwsem);
22146+ au_di_free(tmp);
4a4d8108
AM
22147+ }
22148+ }
22149+ AuDbg("need_wh %d\n", need_wh);
22150+ err = need_wh;
22151+
4f0767ce 22152+out:
4a4d8108 22153+ return err;
1facf9fc 22154+}
22155+
4a4d8108
AM
22156+/*
22157+ * simple tests for the del-entry operations.
22158+ * following the checks in vfs, plus the parent-child relationship.
22159+ */
22160+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
22161+ struct dentry *h_parent, int isdir)
1facf9fc 22162+{
4a4d8108
AM
22163+ int err;
22164+ umode_t h_mode;
22165+ struct dentry *h_dentry, *h_latest;
1308ab2a 22166+ struct inode *h_inode;
1facf9fc 22167+
4a4d8108 22168+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 22169+ if (d_really_is_positive(dentry)) {
4a4d8108 22170+ err = -ENOENT;
5527c038
JR
22171+ if (unlikely(d_is_negative(h_dentry)))
22172+ goto out;
22173+ h_inode = d_inode(h_dentry);
22174+ if (unlikely(!h_inode->i_nlink))
4a4d8108 22175+ goto out;
1facf9fc 22176+
4a4d8108
AM
22177+ h_mode = h_inode->i_mode;
22178+ if (!isdir) {
22179+ err = -EISDIR;
22180+ if (unlikely(S_ISDIR(h_mode)))
22181+ goto out;
22182+ } else if (unlikely(!S_ISDIR(h_mode))) {
22183+ err = -ENOTDIR;
22184+ goto out;
22185+ }
22186+ } else {
22187+ /* rename(2) case */
22188+ err = -EIO;
5527c038 22189+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
22190+ goto out;
22191+ }
1facf9fc 22192+
4a4d8108
AM
22193+ err = -ENOENT;
22194+ /* expected parent dir is locked */
22195+ if (unlikely(h_parent != h_dentry->d_parent))
22196+ goto out;
22197+ err = 0;
22198+
22199+ /*
22200+ * rmdir a dir may break the consistency on some filesystem.
22201+ * let's try heavy test.
22202+ */
22203+ err = -EACCES;
076b876e 22204+ if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
5527c038 22205+ && au_test_h_perm(d_inode(h_parent),
076b876e 22206+ MAY_EXEC | MAY_WRITE)))
4a4d8108
AM
22207+ goto out;
22208+
076b876e 22209+ h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
4a4d8108
AM
22210+ err = -EIO;
22211+ if (IS_ERR(h_latest))
22212+ goto out;
22213+ if (h_latest == h_dentry)
22214+ err = 0;
22215+ dput(h_latest);
22216+
4f0767ce 22217+out:
4a4d8108 22218+ return err;
1308ab2a 22219+}
1facf9fc 22220+
4a4d8108
AM
22221+/*
22222+ * decide the branch where we operate for @dentry. the branch index will be set
22223+ * @rbcpup. after diciding it, 'pin' it and store the timestamps of the parent
22224+ * dir for reverting.
22225+ * when a new whiteout is necessary, create it.
22226+ */
22227+static struct dentry*
22228+lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
22229+ struct au_dtime *dt, struct au_pin *pin)
1308ab2a 22230+{
4a4d8108
AM
22231+ struct dentry *wh_dentry;
22232+ struct super_block *sb;
22233+ struct path h_path;
22234+ int err, need_wh;
22235+ unsigned int udba;
22236+ aufs_bindex_t bcpup;
dece6358 22237+
4a4d8108
AM
22238+ need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
22239+ wh_dentry = ERR_PTR(need_wh);
22240+ if (unlikely(need_wh < 0))
22241+ goto out;
22242+
22243+ sb = dentry->d_sb;
22244+ udba = au_opt_udba(sb);
22245+ bcpup = *rbcpup;
22246+ err = au_pin(pin, dentry, bcpup, udba,
22247+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22248+ wh_dentry = ERR_PTR(err);
22249+ if (unlikely(err))
22250+ goto out;
22251+
22252+ h_path.dentry = au_pinned_h_parent(pin);
22253+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 22254+ && au_dbtop(dentry) == bcpup) {
4a4d8108
AM
22255+ err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
22256+ wh_dentry = ERR_PTR(err);
22257+ if (unlikely(err))
22258+ goto out_unpin;
22259+ }
22260+
22261+ h_path.mnt = au_sbr_mnt(sb, bcpup);
22262+ au_dtime_store(dt, au_pinned_parent(pin), &h_path);
22263+ wh_dentry = NULL;
22264+ if (!need_wh)
22265+ goto out; /* success, no need to create whiteout */
22266+
22267+ wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
22268+ if (IS_ERR(wh_dentry))
22269+ goto out_unpin;
22270+
22271+ /* returns with the parent is locked and wh_dentry is dget-ed */
22272+ goto out; /* success */
22273+
4f0767ce 22274+out_unpin:
4a4d8108 22275+ au_unpin(pin);
4f0767ce 22276+out:
4a4d8108 22277+ return wh_dentry;
1facf9fc 22278+}
22279+
4a4d8108
AM
22280+/*
22281+ * when removing a dir, rename it to a unique temporary whiteout-ed name first
22282+ * in order to be revertible and save time for removing many child whiteouts
22283+ * under the dir.
22284+ * returns 1 when there are too many child whiteout and caller should remove
22285+ * them asynchronously. returns 0 when the number of children is enough small to
22286+ * remove now or the branch fs is a remote fs.
22287+ * otherwise return an error.
22288+ */
22289+static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
22290+ struct au_nhash *whlist, struct inode *dir)
1facf9fc 22291+{
4a4d8108
AM
22292+ int rmdir_later, err, dirwh;
22293+ struct dentry *h_dentry;
22294+ struct super_block *sb;
5527c038 22295+ struct inode *inode;
4a4d8108
AM
22296+
22297+ sb = dentry->d_sb;
22298+ SiMustAnyLock(sb);
22299+ h_dentry = au_h_dptr(dentry, bindex);
22300+ err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
22301+ if (unlikely(err))
22302+ goto out;
22303+
22304+ /* stop monitoring */
5527c038
JR
22305+ inode = d_inode(dentry);
22306+ au_hn_free(au_hi(inode, bindex));
4a4d8108
AM
22307+
22308+ if (!au_test_fs_remote(h_dentry->d_sb)) {
22309+ dirwh = au_sbi(sb)->si_dirwh;
22310+ rmdir_later = (dirwh <= 1);
22311+ if (!rmdir_later)
22312+ rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
22313+ dirwh);
22314+ if (rmdir_later)
22315+ return rmdir_later;
22316+ }
1facf9fc 22317+
4a4d8108
AM
22318+ err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
22319+ if (unlikely(err)) {
523b37e3
AM
22320+ AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
22321+ h_dentry, bindex, err);
4a4d8108
AM
22322+ err = 0;
22323+ }
dece6358 22324+
4f0767ce 22325+out:
4a4d8108
AM
22326+ AuTraceErr(err);
22327+ return err;
22328+}
1308ab2a 22329+
4a4d8108
AM
22330+/*
22331+ * final procedure for deleting a entry.
22332+ * maintain dentry and iattr.
22333+ */
22334+static void epilog(struct inode *dir, struct dentry *dentry,
22335+ aufs_bindex_t bindex)
22336+{
22337+ struct inode *inode;
1308ab2a 22338+
5527c038 22339+ inode = d_inode(dentry);
4a4d8108
AM
22340+ d_drop(dentry);
22341+ inode->i_ctime = dir->i_ctime;
1308ab2a 22342+
b912730e 22343+ au_dir_ts(dir, bindex);
be118d29 22344+ inode_inc_iversion(dir);
1facf9fc 22345+}
22346+
4a4d8108
AM
22347+/*
22348+ * when an error happened, remove the created whiteout and revert everything.
22349+ */
7f207e10
AM
22350+static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
22351+ aufs_bindex_t bwh, struct dentry *wh_dentry,
22352+ struct dentry *dentry, struct au_dtime *dt)
1facf9fc 22353+{
4a4d8108
AM
22354+ int rerr;
22355+ struct path h_path = {
22356+ .dentry = wh_dentry,
7f207e10 22357+ .mnt = au_sbr_mnt(dir->i_sb, bindex)
4a4d8108 22358+ };
dece6358 22359+
7f207e10 22360+ rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
4a4d8108
AM
22361+ if (!rerr) {
22362+ au_set_dbwh(dentry, bwh);
22363+ au_dtime_revert(dt);
22364+ return 0;
22365+ }
dece6358 22366+
523b37e3 22367+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
4a4d8108 22368+ return -EIO;
1facf9fc 22369+}
22370+
4a4d8108 22371+/* ---------------------------------------------------------------------- */
1facf9fc 22372+
4a4d8108 22373+int aufs_unlink(struct inode *dir, struct dentry *dentry)
1308ab2a 22374+{
4a4d8108 22375+ int err;
5afbbe0d 22376+ aufs_bindex_t bwh, bindex, btop;
523b37e3 22377+ struct inode *inode, *h_dir, *delegated;
4a4d8108 22378+ struct dentry *parent, *wh_dentry;
c2b27bf2
AM
22379+ /* to reuduce stack size */
22380+ struct {
22381+ struct au_dtime dt;
22382+ struct au_pin pin;
22383+ struct path h_path;
22384+ } *a;
1facf9fc 22385+
4a4d8108 22386+ IMustLock(dir);
027c5e7a 22387+
c2b27bf2
AM
22388+ err = -ENOMEM;
22389+ a = kmalloc(sizeof(*a), GFP_NOFS);
22390+ if (unlikely(!a))
22391+ goto out;
22392+
027c5e7a
AM
22393+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
22394+ if (unlikely(err))
c2b27bf2 22395+ goto out_free;
027c5e7a
AM
22396+ err = au_d_hashed_positive(dentry);
22397+ if (unlikely(err))
22398+ goto out_unlock;
5527c038 22399+ inode = d_inode(dentry);
4a4d8108 22400+ IMustLock(inode);
027c5e7a 22401+ err = -EISDIR;
2000de60 22402+ if (unlikely(d_is_dir(dentry)))
027c5e7a 22403+ goto out_unlock; /* possible? */
1facf9fc 22404+
5afbbe0d 22405+ btop = au_dbtop(dentry);
4a4d8108
AM
22406+ bwh = au_dbwh(dentry);
22407+ bindex = -1;
027c5e7a
AM
22408+ parent = dentry->d_parent; /* dir inode is locked */
22409+ di_write_lock_parent(parent);
c2b27bf2
AM
22410+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
22411+ &a->pin);
4a4d8108
AM
22412+ err = PTR_ERR(wh_dentry);
22413+ if (IS_ERR(wh_dentry))
027c5e7a 22414+ goto out_parent;
1facf9fc 22415+
5afbbe0d
AM
22416+ a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
22417+ a->h_path.dentry = au_h_dptr(dentry, btop);
c2b27bf2 22418+ dget(a->h_path.dentry);
5afbbe0d 22419+ if (bindex == btop) {
c2b27bf2 22420+ h_dir = au_pinned_h_dir(&a->pin);
523b37e3
AM
22421+ delegated = NULL;
22422+ err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
22423+ if (unlikely(err == -EWOULDBLOCK)) {
22424+ pr_warn("cannot retry for NFSv4 delegation"
22425+ " for an internal unlink\n");
22426+ iput(delegated);
22427+ }
4a4d8108
AM
22428+ } else {
22429+ /* dir inode is locked */
5527c038 22430+ h_dir = d_inode(wh_dentry->d_parent);
4a4d8108
AM
22431+ IMustLock(h_dir);
22432+ err = 0;
22433+ }
dece6358 22434+
4a4d8108 22435+ if (!err) {
7f207e10 22436+ vfsub_drop_nlink(inode);
4a4d8108
AM
22437+ epilog(dir, dentry, bindex);
22438+
22439+ /* update target timestamps */
5afbbe0d 22440+ if (bindex == btop) {
c2b27bf2
AM
22441+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
22442+ /*ignore*/
5527c038 22443+ inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
4a4d8108
AM
22444+ } else
22445+ /* todo: this timestamp may be reverted later */
22446+ inode->i_ctime = h_dir->i_ctime;
027c5e7a 22447+ goto out_unpin; /* success */
1facf9fc 22448+ }
22449+
4a4d8108
AM
22450+ /* revert */
22451+ if (wh_dentry) {
22452+ int rerr;
22453+
c2b27bf2
AM
22454+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22455+ &a->dt);
4a4d8108
AM
22456+ if (rerr)
22457+ err = rerr;
dece6358 22458+ }
1facf9fc 22459+
027c5e7a 22460+out_unpin:
c2b27bf2 22461+ au_unpin(&a->pin);
4a4d8108 22462+ dput(wh_dentry);
c2b27bf2 22463+ dput(a->h_path.dentry);
027c5e7a 22464+out_parent:
4a4d8108 22465+ di_write_unlock(parent);
027c5e7a 22466+out_unlock:
4a4d8108 22467+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 22468+out_free:
1c60b727 22469+ kfree(a);
027c5e7a 22470+out:
4a4d8108 22471+ return err;
dece6358
AM
22472+}
22473+
4a4d8108 22474+int aufs_rmdir(struct inode *dir, struct dentry *dentry)
1308ab2a 22475+{
4a4d8108 22476+ int err, rmdir_later;
5afbbe0d 22477+ aufs_bindex_t bwh, bindex, btop;
4a4d8108
AM
22478+ struct inode *inode;
22479+ struct dentry *parent, *wh_dentry, *h_dentry;
22480+ struct au_whtmp_rmdir *args;
c2b27bf2
AM
22481+ /* to reuduce stack size */
22482+ struct {
22483+ struct au_dtime dt;
22484+ struct au_pin pin;
22485+ } *a;
1facf9fc 22486+
4a4d8108 22487+ IMustLock(dir);
027c5e7a 22488+
c2b27bf2
AM
22489+ err = -ENOMEM;
22490+ a = kmalloc(sizeof(*a), GFP_NOFS);
22491+ if (unlikely(!a))
22492+ goto out;
22493+
027c5e7a
AM
22494+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22495+ if (unlikely(err))
c2b27bf2 22496+ goto out_free;
53392da6
AM
22497+ err = au_alive_dir(dentry);
22498+ if (unlikely(err))
027c5e7a 22499+ goto out_unlock;
5527c038 22500+ inode = d_inode(dentry);
4a4d8108 22501+ IMustLock(inode);
027c5e7a 22502+ err = -ENOTDIR;
2000de60 22503+ if (unlikely(!d_is_dir(dentry)))
027c5e7a 22504+ goto out_unlock; /* possible? */
dece6358 22505+
4a4d8108
AM
22506+ err = -ENOMEM;
22507+ args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
22508+ if (unlikely(!args))
22509+ goto out_unlock;
dece6358 22510+
4a4d8108
AM
22511+ parent = dentry->d_parent; /* dir inode is locked */
22512+ di_write_lock_parent(parent);
22513+ err = au_test_empty(dentry, &args->whlist);
22514+ if (unlikely(err))
027c5e7a 22515+ goto out_parent;
1facf9fc 22516+
5afbbe0d 22517+ btop = au_dbtop(dentry);
4a4d8108
AM
22518+ bwh = au_dbwh(dentry);
22519+ bindex = -1;
c2b27bf2
AM
22520+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
22521+ &a->pin);
4a4d8108
AM
22522+ err = PTR_ERR(wh_dentry);
22523+ if (IS_ERR(wh_dentry))
027c5e7a 22524+ goto out_parent;
1facf9fc 22525+
5afbbe0d 22526+ h_dentry = au_h_dptr(dentry, btop);
4a4d8108
AM
22527+ dget(h_dentry);
22528+ rmdir_later = 0;
5afbbe0d
AM
22529+ if (bindex == btop) {
22530+ err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
4a4d8108
AM
22531+ if (err > 0) {
22532+ rmdir_later = err;
22533+ err = 0;
22534+ }
22535+ } else {
22536+ /* stop monitoring */
5afbbe0d 22537+ au_hn_free(au_hi(inode, btop));
4a4d8108
AM
22538+
22539+ /* dir inode is locked */
5527c038 22540+ IMustLock(d_inode(wh_dentry->d_parent));
1facf9fc 22541+ err = 0;
22542+ }
22543+
4a4d8108 22544+ if (!err) {
027c5e7a 22545+ vfsub_dead_dir(inode);
4a4d8108
AM
22546+ au_set_dbdiropq(dentry, -1);
22547+ epilog(dir, dentry, bindex);
1308ab2a 22548+
4a4d8108 22549+ if (rmdir_later) {
5afbbe0d 22550+ au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
4a4d8108
AM
22551+ args = NULL;
22552+ }
1308ab2a 22553+
4a4d8108 22554+ goto out_unpin; /* success */
1facf9fc 22555+ }
22556+
4a4d8108
AM
22557+ /* revert */
22558+ AuLabel(revert);
22559+ if (wh_dentry) {
22560+ int rerr;
1308ab2a 22561+
c2b27bf2
AM
22562+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22563+ &a->dt);
4a4d8108
AM
22564+ if (rerr)
22565+ err = rerr;
1facf9fc 22566+ }
22567+
4f0767ce 22568+out_unpin:
c2b27bf2 22569+ au_unpin(&a->pin);
4a4d8108
AM
22570+ dput(wh_dentry);
22571+ dput(h_dentry);
027c5e7a 22572+out_parent:
4a4d8108
AM
22573+ di_write_unlock(parent);
22574+ if (args)
22575+ au_whtmp_rmdir_free(args);
4f0767ce 22576+out_unlock:
4a4d8108 22577+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 22578+out_free:
1c60b727 22579+ kfree(a);
4f0767ce 22580+out:
4a4d8108
AM
22581+ AuTraceErr(err);
22582+ return err;
dece6358 22583+}
7f207e10
AM
22584diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
22585--- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 22586+++ linux/fs/aufs/i_op_ren.c 2018-08-12 23:43:05.460124736 +0200
062440b3 22587@@ -0,0 +1,1247 @@
cd7a4cd9 22588+// SPDX-License-Identifier: GPL-2.0
1facf9fc 22589+/*
b00004a5 22590+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 22591+ *
22592+ * This program, aufs is free software; you can redistribute it and/or modify
22593+ * it under the terms of the GNU General Public License as published by
22594+ * the Free Software Foundation; either version 2 of the License, or
22595+ * (at your option) any later version.
dece6358
AM
22596+ *
22597+ * This program is distributed in the hope that it will be useful,
22598+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22599+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22600+ * GNU General Public License for more details.
22601+ *
22602+ * You should have received a copy of the GNU General Public License
523b37e3 22603+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 22604+ */
22605+
22606+/*
4a4d8108
AM
22607+ * inode operation (rename entry)
22608+ * todo: this is crazy monster
1facf9fc 22609+ */
22610+
22611+#include "aufs.h"
22612+
4a4d8108
AM
22613+enum { AuSRC, AuDST, AuSrcDst };
22614+enum { AuPARENT, AuCHILD, AuParentChild };
1facf9fc 22615+
f2c43d5f
AM
22616+#define AuRen_ISDIR_SRC 1
22617+#define AuRen_ISDIR_DST (1 << 1)
22618+#define AuRen_ISSAMEDIR (1 << 2)
22619+#define AuRen_WHSRC (1 << 3)
22620+#define AuRen_WHDST (1 << 4)
22621+#define AuRen_MNT_WRITE (1 << 5)
22622+#define AuRen_DT_DSTDIR (1 << 6)
22623+#define AuRen_DIROPQ_SRC (1 << 7)
22624+#define AuRen_DIROPQ_DST (1 << 8)
8b6a4947
AM
22625+#define AuRen_DIRREN (1 << 9)
22626+#define AuRen_DROPPED_SRC (1 << 10)
22627+#define AuRen_DROPPED_DST (1 << 11)
4a4d8108 22628+#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
7f207e10
AM
22629+#define au_fset_ren(flags, name) \
22630+ do { (flags) |= AuRen_##name; } while (0)
22631+#define au_fclr_ren(flags, name) \
22632+ do { (flags) &= ~AuRen_##name; } while (0)
1facf9fc 22633+
8b6a4947
AM
22634+#ifndef CONFIG_AUFS_DIRREN
22635+#undef AuRen_DIRREN
22636+#define AuRen_DIRREN 0
22637+#endif
22638+
4a4d8108
AM
22639+struct au_ren_args {
22640+ struct {
22641+ struct dentry *dentry, *h_dentry, *parent, *h_parent,
22642+ *wh_dentry;
22643+ struct inode *dir, *inode;
f2c43d5f 22644+ struct au_hinode *hdir, *hinode;
4a4d8108 22645+ struct au_dtime dt[AuParentChild];
f2c43d5f 22646+ aufs_bindex_t btop, bdiropq;
4a4d8108 22647+ } sd[AuSrcDst];
1facf9fc 22648+
4a4d8108
AM
22649+#define src_dentry sd[AuSRC].dentry
22650+#define src_dir sd[AuSRC].dir
22651+#define src_inode sd[AuSRC].inode
22652+#define src_h_dentry sd[AuSRC].h_dentry
22653+#define src_parent sd[AuSRC].parent
22654+#define src_h_parent sd[AuSRC].h_parent
22655+#define src_wh_dentry sd[AuSRC].wh_dentry
22656+#define src_hdir sd[AuSRC].hdir
f2c43d5f 22657+#define src_hinode sd[AuSRC].hinode
4a4d8108
AM
22658+#define src_h_dir sd[AuSRC].hdir->hi_inode
22659+#define src_dt sd[AuSRC].dt
5afbbe0d 22660+#define src_btop sd[AuSRC].btop
f2c43d5f 22661+#define src_bdiropq sd[AuSRC].bdiropq
1facf9fc 22662+
4a4d8108
AM
22663+#define dst_dentry sd[AuDST].dentry
22664+#define dst_dir sd[AuDST].dir
22665+#define dst_inode sd[AuDST].inode
22666+#define dst_h_dentry sd[AuDST].h_dentry
22667+#define dst_parent sd[AuDST].parent
22668+#define dst_h_parent sd[AuDST].h_parent
22669+#define dst_wh_dentry sd[AuDST].wh_dentry
22670+#define dst_hdir sd[AuDST].hdir
f2c43d5f 22671+#define dst_hinode sd[AuDST].hinode
4a4d8108
AM
22672+#define dst_h_dir sd[AuDST].hdir->hi_inode
22673+#define dst_dt sd[AuDST].dt
5afbbe0d 22674+#define dst_btop sd[AuDST].btop
f2c43d5f 22675+#define dst_bdiropq sd[AuDST].bdiropq
4a4d8108
AM
22676+
22677+ struct dentry *h_trap;
22678+ struct au_branch *br;
4a4d8108
AM
22679+ struct path h_path;
22680+ struct au_nhash whlist;
f2c43d5f 22681+ aufs_bindex_t btgt, src_bwh;
1facf9fc 22682+
f2c43d5f
AM
22683+ struct {
22684+ unsigned short auren_flags;
22685+ unsigned char flags; /* syscall parameter */
22686+ unsigned char exchange;
22687+ } __packed;
1facf9fc 22688+
4a4d8108
AM
22689+ struct au_whtmp_rmdir *thargs;
22690+ struct dentry *h_dst;
8b6a4947 22691+ struct au_hinode *h_root;
4a4d8108 22692+};
1308ab2a 22693+
4a4d8108 22694+/* ---------------------------------------------------------------------- */
1308ab2a 22695+
4a4d8108
AM
22696+/*
22697+ * functions for reverting.
22698+ * when an error happened in a single rename systemcall, we should revert
79b8bda9 22699+ * everything as if nothing happened.
4a4d8108
AM
22700+ * we don't need to revert the copied-up/down the parent dir since they are
22701+ * harmless.
22702+ */
1facf9fc 22703+
4a4d8108
AM
22704+#define RevertFailure(fmt, ...) do { \
22705+ AuIOErr("revert failure: " fmt " (%d, %d)\n", \
22706+ ##__VA_ARGS__, err, rerr); \
22707+ err = -EIO; \
22708+} while (0)
1facf9fc 22709+
f2c43d5f 22710+static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
1facf9fc 22711+{
4a4d8108 22712+ int rerr;
f2c43d5f
AM
22713+ struct dentry *d;
22714+#define src_or_dst(member) a->sd[idx].member
1facf9fc 22715+
f2c43d5f
AM
22716+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22717+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22718+ rerr = au_diropq_remove(d, a->btgt);
22719+ au_hn_inode_unlock(src_or_dst(hinode));
22720+ au_set_dbdiropq(d, src_or_dst(bdiropq));
4a4d8108 22721+ if (rerr)
f2c43d5f
AM
22722+ RevertFailure("remove diropq %pd", d);
22723+
22724+#undef src_or_dst_
22725+}
22726+
22727+static void au_ren_rev_diropq(int err, struct au_ren_args *a)
22728+{
22729+ if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
22730+ au_ren_do_rev_diropq(err, a, AuSRC);
22731+ if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
22732+ au_ren_do_rev_diropq(err, a, AuDST);
4a4d8108 22733+}
1facf9fc 22734+
4a4d8108
AM
22735+static void au_ren_rev_rename(int err, struct au_ren_args *a)
22736+{
22737+ int rerr;
523b37e3 22738+ struct inode *delegated;
1facf9fc 22739+
b4510431
AM
22740+ a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
22741+ a->src_h_parent);
4a4d8108
AM
22742+ rerr = PTR_ERR(a->h_path.dentry);
22743+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 22744+ RevertFailure("lkup one %pd", a->src_dentry);
4a4d8108 22745+ return;
1facf9fc 22746+ }
22747+
523b37e3 22748+ delegated = NULL;
4a4d8108
AM
22749+ rerr = vfsub_rename(a->dst_h_dir,
22750+ au_h_dptr(a->src_dentry, a->btgt),
f2c43d5f 22751+ a->src_h_dir, &a->h_path, &delegated, a->flags);
523b37e3
AM
22752+ if (unlikely(rerr == -EWOULDBLOCK)) {
22753+ pr_warn("cannot retry for NFSv4 delegation"
22754+ " for an internal rename\n");
22755+ iput(delegated);
22756+ }
4a4d8108
AM
22757+ d_drop(a->h_path.dentry);
22758+ dput(a->h_path.dentry);
22759+ /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
22760+ if (rerr)
523b37e3 22761+ RevertFailure("rename %pd", a->src_dentry);
1facf9fc 22762+}
22763+
4a4d8108 22764+static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
1facf9fc 22765+{
4a4d8108 22766+ int rerr;
523b37e3 22767+ struct inode *delegated;
dece6358 22768+
b4510431
AM
22769+ a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
22770+ a->dst_h_parent);
4a4d8108
AM
22771+ rerr = PTR_ERR(a->h_path.dentry);
22772+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 22773+ RevertFailure("lkup one %pd", a->dst_dentry);
4a4d8108
AM
22774+ return;
22775+ }
5527c038 22776+ if (d_is_positive(a->h_path.dentry)) {
4a4d8108
AM
22777+ d_drop(a->h_path.dentry);
22778+ dput(a->h_path.dentry);
22779+ return;
dece6358
AM
22780+ }
22781+
523b37e3
AM
22782+ delegated = NULL;
22783+ rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
f2c43d5f 22784+ &delegated, a->flags);
523b37e3
AM
22785+ if (unlikely(rerr == -EWOULDBLOCK)) {
22786+ pr_warn("cannot retry for NFSv4 delegation"
22787+ " for an internal rename\n");
22788+ iput(delegated);
22789+ }
4a4d8108
AM
22790+ d_drop(a->h_path.dentry);
22791+ dput(a->h_path.dentry);
22792+ if (!rerr)
22793+ au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
22794+ else
523b37e3 22795+ RevertFailure("rename %pd", a->h_dst);
4a4d8108 22796+}
1308ab2a 22797+
4a4d8108
AM
22798+static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
22799+{
22800+ int rerr;
1308ab2a 22801+
4a4d8108
AM
22802+ a->h_path.dentry = a->src_wh_dentry;
22803+ rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
027c5e7a 22804+ au_set_dbwh(a->src_dentry, a->src_bwh);
4a4d8108 22805+ if (rerr)
523b37e3 22806+ RevertFailure("unlink %pd", a->src_wh_dentry);
4a4d8108 22807+}
4a4d8108 22808+#undef RevertFailure
1facf9fc 22809+
1308ab2a 22810+/* ---------------------------------------------------------------------- */
22811+
4a4d8108
AM
22812+/*
22813+ * when we have to copyup the renaming entry, do it with the rename-target name
22814+ * in order to minimize the cost (the later actual rename is unnecessary).
22815+ * otherwise rename it on the target branch.
22816+ */
22817+static int au_ren_or_cpup(struct au_ren_args *a)
1facf9fc 22818+{
dece6358 22819+ int err;
4a4d8108 22820+ struct dentry *d;
523b37e3 22821+ struct inode *delegated;
1facf9fc 22822+
4a4d8108 22823+ d = a->src_dentry;
5afbbe0d 22824+ if (au_dbtop(d) == a->btgt) {
4a4d8108 22825+ a->h_path.dentry = a->dst_h_dentry;
5afbbe0d 22826+ AuDebugOn(au_dbtop(d) != a->btgt);
523b37e3 22827+ delegated = NULL;
4a4d8108 22828+ err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
f2c43d5f
AM
22829+ a->dst_h_dir, &a->h_path, &delegated,
22830+ a->flags);
523b37e3
AM
22831+ if (unlikely(err == -EWOULDBLOCK)) {
22832+ pr_warn("cannot retry for NFSv4 delegation"
22833+ " for an internal rename\n");
22834+ iput(delegated);
22835+ }
c2b27bf2 22836+ } else
86dc4139 22837+ BUG();
1308ab2a 22838+
027c5e7a
AM
22839+ if (!err && a->h_dst)
22840+ /* it will be set to dinfo later */
22841+ dget(a->h_dst);
1facf9fc 22842+
dece6358
AM
22843+ return err;
22844+}
1facf9fc 22845+
4a4d8108
AM
22846+/* cf. aufs_rmdir() */
22847+static int au_ren_del_whtmp(struct au_ren_args *a)
dece6358 22848+{
4a4d8108
AM
22849+ int err;
22850+ struct inode *dir;
1facf9fc 22851+
4a4d8108
AM
22852+ dir = a->dst_dir;
22853+ SiMustAnyLock(dir->i_sb);
22854+ if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
22855+ au_sbi(dir->i_sb)->si_dirwh)
22856+ || au_test_fs_remote(a->h_dst->d_sb)) {
22857+ err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
22858+ if (unlikely(err))
523b37e3
AM
22859+ pr_warn("failed removing whtmp dir %pd (%d), "
22860+ "ignored.\n", a->h_dst, err);
4a4d8108
AM
22861+ } else {
22862+ au_nhash_wh_free(&a->thargs->whlist);
22863+ a->thargs->whlist = a->whlist;
22864+ a->whlist.nh_num = 0;
22865+ au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
22866+ dput(a->h_dst);
22867+ a->thargs = NULL;
22868+ }
22869+
22870+ return 0;
1308ab2a 22871+}
1facf9fc 22872+
4a4d8108 22873+/* make it 'opaque' dir. */
f2c43d5f 22874+static int au_ren_do_diropq(struct au_ren_args *a, int idx)
4a4d8108
AM
22875+{
22876+ int err;
f2c43d5f
AM
22877+ struct dentry *d, *diropq;
22878+#define src_or_dst(member) a->sd[idx].member
1facf9fc 22879+
4a4d8108 22880+ err = 0;
f2c43d5f
AM
22881+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22882+ src_or_dst(bdiropq) = au_dbdiropq(d);
22883+ src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
22884+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22885+ diropq = au_diropq_create(d, a->btgt);
22886+ au_hn_inode_unlock(src_or_dst(hinode));
4a4d8108
AM
22887+ if (IS_ERR(diropq))
22888+ err = PTR_ERR(diropq);
076b876e
AM
22889+ else
22890+ dput(diropq);
1facf9fc 22891+
f2c43d5f 22892+#undef src_or_dst_
4a4d8108
AM
22893+ return err;
22894+}
1facf9fc 22895+
f2c43d5f 22896+static int au_ren_diropq(struct au_ren_args *a)
4a4d8108
AM
22897+{
22898+ int err;
f2c43d5f
AM
22899+ unsigned char always;
22900+ struct dentry *d;
1facf9fc 22901+
f2c43d5f
AM
22902+ err = 0;
22903+ d = a->dst_dentry; /* already renamed on the branch */
22904+ always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
22905+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
8b6a4947 22906+ && !au_ftest_ren(a->auren_flags, DIRREN)
f2c43d5f
AM
22907+ && a->btgt != au_dbdiropq(a->src_dentry)
22908+ && (a->dst_wh_dentry
22909+ || a->btgt <= au_dbdiropq(d)
22910+ /* hide the lower to keep xino */
22911+ /* the lowers may not be a dir, but we hide them anyway */
22912+ || a->btgt < au_dbbot(d)
22913+ || always)) {
22914+ AuDbg("here\n");
22915+ err = au_ren_do_diropq(a, AuSRC);
22916+ if (unlikely(err))
4a4d8108 22917+ goto out;
f2c43d5f 22918+ au_fset_ren(a->auren_flags, DIROPQ_SRC);
4a4d8108 22919+ }
f2c43d5f
AM
22920+ if (!a->exchange)
22921+ goto out; /* success */
1facf9fc 22922+
f2c43d5f
AM
22923+ d = a->src_dentry; /* already renamed on the branch */
22924+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22925+ && a->btgt != au_dbdiropq(a->dst_dentry)
22926+ && (a->btgt < au_dbdiropq(d)
22927+ || a->btgt < au_dbbot(d)
22928+ || always)) {
22929+ AuDbgDentry(a->src_dentry);
22930+ AuDbgDentry(a->dst_dentry);
22931+ err = au_ren_do_diropq(a, AuDST);
4a4d8108 22932+ if (unlikely(err))
f2c43d5f
AM
22933+ goto out_rev_src;
22934+ au_fset_ren(a->auren_flags, DIROPQ_DST);
22935+ }
22936+ goto out; /* success */
dece6358 22937+
f2c43d5f
AM
22938+out_rev_src:
22939+ AuDbg("err %d, reverting src\n", err);
22940+ au_ren_rev_diropq(err, a);
22941+out:
22942+ return err;
22943+}
22944+
22945+static int do_rename(struct au_ren_args *a)
22946+{
22947+ int err;
22948+ struct dentry *d, *h_d;
22949+
22950+ if (!a->exchange) {
22951+ /* prepare workqueue args for asynchronous rmdir */
22952+ h_d = a->dst_h_dentry;
22953+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
8b6a4947 22954+ /* && !au_ftest_ren(a->auren_flags, DIRREN) */
f2c43d5f
AM
22955+ && d_is_positive(h_d)) {
22956+ err = -ENOMEM;
22957+ a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
22958+ GFP_NOFS);
22959+ if (unlikely(!a->thargs))
22960+ goto out;
22961+ a->h_dst = dget(h_d);
22962+ }
22963+
22964+ /* create whiteout for src_dentry */
22965+ if (au_ftest_ren(a->auren_flags, WHSRC)) {
22966+ a->src_bwh = au_dbwh(a->src_dentry);
22967+ AuDebugOn(a->src_bwh >= 0);
22968+ a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
22969+ a->src_h_parent);
22970+ err = PTR_ERR(a->src_wh_dentry);
22971+ if (IS_ERR(a->src_wh_dentry))
22972+ goto out_thargs;
22973+ }
22974+
22975+ /* lookup whiteout for dentry */
22976+ if (au_ftest_ren(a->auren_flags, WHDST)) {
22977+ h_d = au_wh_lkup(a->dst_h_parent,
22978+ &a->dst_dentry->d_name, a->br);
22979+ err = PTR_ERR(h_d);
22980+ if (IS_ERR(h_d))
22981+ goto out_whsrc;
22982+ if (d_is_negative(h_d))
22983+ dput(h_d);
22984+ else
22985+ a->dst_wh_dentry = h_d;
22986+ }
22987+
22988+ /* rename dentry to tmpwh */
22989+ if (a->thargs) {
22990+ err = au_whtmp_ren(a->dst_h_dentry, a->br);
22991+ if (unlikely(err))
22992+ goto out_whdst;
22993+
22994+ d = a->dst_dentry;
22995+ au_set_h_dptr(d, a->btgt, NULL);
22996+ err = au_lkup_neg(d, a->btgt, /*wh*/0);
22997+ if (unlikely(err))
22998+ goto out_whtmp;
22999+ a->dst_h_dentry = au_h_dptr(d, a->btgt);
23000+ }
4a4d8108 23001+ }
1facf9fc 23002+
5afbbe0d 23003+ BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
8b6a4947
AM
23004+#if 0
23005+ BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
23006+ && d_is_positive(a->dst_h_dentry)
23007+ && a->src_btop != a->btgt);
23008+#endif
1facf9fc 23009+
4a4d8108 23010+ /* rename by vfs_rename or cpup */
4a4d8108
AM
23011+ err = au_ren_or_cpup(a);
23012+ if (unlikely(err))
23013+ /* leave the copied-up one */
23014+ goto out_whtmp;
1308ab2a 23015+
4a4d8108 23016+ /* make dir opaque */
f2c43d5f
AM
23017+ err = au_ren_diropq(a);
23018+ if (unlikely(err))
23019+ goto out_rename;
1308ab2a 23020+
4a4d8108 23021+ /* update target timestamps */
f2c43d5f
AM
23022+ if (a->exchange) {
23023+ AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
23024+ a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
23025+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
23026+ a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
23027+ }
5afbbe0d 23028+ AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
4a4d8108
AM
23029+ a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
23030+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
5527c038 23031+ a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
1facf9fc 23032+
f2c43d5f
AM
23033+ if (!a->exchange) {
23034+ /* remove whiteout for dentry */
23035+ if (a->dst_wh_dentry) {
23036+ a->h_path.dentry = a->dst_wh_dentry;
23037+ err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
23038+ a->dst_dentry);
23039+ if (unlikely(err))
23040+ goto out_diropq;
23041+ }
1facf9fc 23042+
f2c43d5f
AM
23043+ /* remove whtmp */
23044+ if (a->thargs)
23045+ au_ren_del_whtmp(a); /* ignore this error */
1308ab2a 23046+
f2c43d5f
AM
23047+ au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
23048+ }
4a4d8108
AM
23049+ err = 0;
23050+ goto out_success;
23051+
4f0767ce 23052+out_diropq:
f2c43d5f 23053+ au_ren_rev_diropq(err, a);
4f0767ce 23054+out_rename:
7e9cd9fe 23055+ au_ren_rev_rename(err, a);
027c5e7a 23056+ dput(a->h_dst);
4f0767ce 23057+out_whtmp:
4a4d8108
AM
23058+ if (a->thargs)
23059+ au_ren_rev_whtmp(err, a);
4f0767ce 23060+out_whdst:
4a4d8108
AM
23061+ dput(a->dst_wh_dentry);
23062+ a->dst_wh_dentry = NULL;
4f0767ce 23063+out_whsrc:
4a4d8108
AM
23064+ if (a->src_wh_dentry)
23065+ au_ren_rev_whsrc(err, a);
4f0767ce 23066+out_success:
4a4d8108
AM
23067+ dput(a->src_wh_dentry);
23068+ dput(a->dst_wh_dentry);
4f0767ce 23069+out_thargs:
4a4d8108
AM
23070+ if (a->thargs) {
23071+ dput(a->h_dst);
23072+ au_whtmp_rmdir_free(a->thargs);
23073+ a->thargs = NULL;
23074+ }
4f0767ce 23075+out:
4a4d8108 23076+ return err;
dece6358 23077+}
1facf9fc 23078+
1308ab2a 23079+/* ---------------------------------------------------------------------- */
1facf9fc 23080+
4a4d8108
AM
23081+/*
23082+ * test if @dentry dir can be rename destination or not.
23083+ * success means, it is a logically empty dir.
23084+ */
23085+static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
1308ab2a 23086+{
4a4d8108 23087+ return au_test_empty(dentry, whlist);
1308ab2a 23088+}
1facf9fc 23089+
4a4d8108 23090+/*
8b6a4947
AM
23091+ * test if @a->src_dentry dir can be rename source or not.
23092+ * if it can, return 0.
4a4d8108
AM
23093+ * success means,
23094+ * - it is a logically empty dir.
23095+ * - or, it exists on writable branch and has no children including whiteouts
8b6a4947 23096+ * on the lower branch unless DIRREN is on.
4a4d8108 23097+ */
8b6a4947 23098+static int may_rename_srcdir(struct au_ren_args *a)
4a4d8108
AM
23099+{
23100+ int err;
23101+ unsigned int rdhash;
8b6a4947
AM
23102+ aufs_bindex_t btop, btgt;
23103+ struct dentry *dentry;
23104+ struct super_block *sb;
23105+ struct au_sbinfo *sbinfo;
1facf9fc 23106+
8b6a4947
AM
23107+ dentry = a->src_dentry;
23108+ sb = dentry->d_sb;
23109+ sbinfo = au_sbi(sb);
23110+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
23111+ au_fset_ren(a->auren_flags, DIRREN);
23112+
23113+ btgt = a->btgt;
5afbbe0d
AM
23114+ btop = au_dbtop(dentry);
23115+ if (btop != btgt) {
4a4d8108 23116+ struct au_nhash whlist;
dece6358 23117+
8b6a4947
AM
23118+ SiMustAnyLock(sb);
23119+ rdhash = sbinfo->si_rdhash;
4a4d8108
AM
23120+ if (!rdhash)
23121+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
23122+ dentry));
23123+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
23124+ if (unlikely(err))
23125+ goto out;
23126+ err = au_test_empty(dentry, &whlist);
23127+ au_nhash_wh_free(&whlist);
23128+ goto out;
23129+ }
dece6358 23130+
5afbbe0d 23131+ if (btop == au_dbtaildir(dentry))
4a4d8108 23132+ return 0; /* success */
dece6358 23133+
4a4d8108 23134+ err = au_test_empty_lower(dentry);
1facf9fc 23135+
4f0767ce 23136+out:
4a4d8108 23137+ if (err == -ENOTEMPTY) {
8b6a4947
AM
23138+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23139+ err = 0;
23140+ } else {
23141+ AuWarn1("renaming dir who has child(ren) on multiple "
23142+ "branches, is not supported\n");
23143+ err = -EXDEV;
23144+ }
4a4d8108
AM
23145+ }
23146+ return err;
23147+}
1308ab2a 23148+
4a4d8108
AM
23149+/* side effect: sets whlist and h_dentry */
23150+static int au_ren_may_dir(struct au_ren_args *a)
1308ab2a 23151+{
4a4d8108
AM
23152+ int err;
23153+ unsigned int rdhash;
23154+ struct dentry *d;
1facf9fc 23155+
4a4d8108
AM
23156+ d = a->dst_dentry;
23157+ SiMustAnyLock(d->d_sb);
1facf9fc 23158+
4a4d8108 23159+ err = 0;
f2c43d5f 23160+ if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
4a4d8108
AM
23161+ rdhash = au_sbi(d->d_sb)->si_rdhash;
23162+ if (!rdhash)
23163+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
23164+ err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
23165+ if (unlikely(err))
23166+ goto out;
1308ab2a 23167+
f2c43d5f
AM
23168+ if (!a->exchange) {
23169+ au_set_dbtop(d, a->dst_btop);
23170+ err = may_rename_dstdir(d, &a->whlist);
23171+ au_set_dbtop(d, a->btgt);
23172+ } else
8b6a4947 23173+ err = may_rename_srcdir(a);
4a4d8108 23174+ }
5afbbe0d 23175+ a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
4a4d8108
AM
23176+ if (unlikely(err))
23177+ goto out;
23178+
23179+ d = a->src_dentry;
5afbbe0d 23180+ a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
f2c43d5f 23181+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
8b6a4947 23182+ err = may_rename_srcdir(a);
4a4d8108
AM
23183+ if (unlikely(err)) {
23184+ au_nhash_wh_free(&a->whlist);
23185+ a->whlist.nh_num = 0;
23186+ }
23187+ }
4f0767ce 23188+out:
4a4d8108 23189+ return err;
1facf9fc 23190+}
23191+
4a4d8108 23192+/* ---------------------------------------------------------------------- */
1facf9fc 23193+
4a4d8108
AM
23194+/*
23195+ * simple tests for rename.
23196+ * following the checks in vfs, plus the parent-child relationship.
23197+ */
23198+static int au_may_ren(struct au_ren_args *a)
23199+{
23200+ int err, isdir;
23201+ struct inode *h_inode;
1facf9fc 23202+
5afbbe0d 23203+ if (a->src_btop == a->btgt) {
4a4d8108 23204+ err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
f2c43d5f 23205+ au_ftest_ren(a->auren_flags, ISDIR_SRC));
4a4d8108
AM
23206+ if (unlikely(err))
23207+ goto out;
23208+ err = -EINVAL;
23209+ if (unlikely(a->src_h_dentry == a->h_trap))
23210+ goto out;
23211+ }
1facf9fc 23212+
4a4d8108 23213+ err = 0;
5afbbe0d 23214+ if (a->dst_btop != a->btgt)
4a4d8108 23215+ goto out;
1facf9fc 23216+
027c5e7a
AM
23217+ err = -ENOTEMPTY;
23218+ if (unlikely(a->dst_h_dentry == a->h_trap))
23219+ goto out;
23220+
4a4d8108 23221+ err = -EIO;
f2c43d5f 23222+ isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
5527c038
JR
23223+ if (d_really_is_negative(a->dst_dentry)) {
23224+ if (d_is_negative(a->dst_h_dentry))
23225+ err = au_may_add(a->dst_dentry, a->btgt,
23226+ a->dst_h_parent, isdir);
4a4d8108 23227+ } else {
5527c038 23228+ if (unlikely(d_is_negative(a->dst_h_dentry)))
4a4d8108 23229+ goto out;
5527c038
JR
23230+ h_inode = d_inode(a->dst_h_dentry);
23231+ if (h_inode->i_nlink)
23232+ err = au_may_del(a->dst_dentry, a->btgt,
23233+ a->dst_h_parent, isdir);
4a4d8108 23234+ }
1facf9fc 23235+
4f0767ce 23236+out:
4a4d8108
AM
23237+ if (unlikely(err == -ENOENT || err == -EEXIST))
23238+ err = -EIO;
23239+ AuTraceErr(err);
23240+ return err;
23241+}
1facf9fc 23242+
1308ab2a 23243+/* ---------------------------------------------------------------------- */
1facf9fc 23244+
4a4d8108
AM
23245+/*
23246+ * locking order
23247+ * (VFS)
23248+ * - src_dir and dir by lock_rename()
23249+ * - inode if exitsts
23250+ * (aufs)
23251+ * - lock all
23252+ * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
23253+ * + si_read_lock
23254+ * + di_write_lock2_child()
23255+ * + di_write_lock_child()
23256+ * + ii_write_lock_child()
23257+ * + di_write_lock_child2()
23258+ * + ii_write_lock_child2()
23259+ * + src_parent and parent
23260+ * + di_write_lock_parent()
23261+ * + ii_write_lock_parent()
23262+ * + di_write_lock_parent2()
23263+ * + ii_write_lock_parent2()
23264+ * + lower src_dir and dir by vfsub_lock_rename()
23265+ * + verify the every relationships between child and parent. if any
23266+ * of them failed, unlock all and return -EBUSY.
23267+ */
23268+static void au_ren_unlock(struct au_ren_args *a)
1308ab2a 23269+{
4a4d8108
AM
23270+ vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
23271+ a->dst_h_parent, a->dst_hdir);
8b6a4947
AM
23272+ if (au_ftest_ren(a->auren_flags, DIRREN)
23273+ && a->h_root)
23274+ au_hn_inode_unlock(a->h_root);
f2c43d5f 23275+ if (au_ftest_ren(a->auren_flags, MNT_WRITE))
86dc4139 23276+ vfsub_mnt_drop_write(au_br_mnt(a->br));
1308ab2a 23277+}
23278+
4a4d8108 23279+static int au_ren_lock(struct au_ren_args *a)
1308ab2a 23280+{
4a4d8108
AM
23281+ int err;
23282+ unsigned int udba;
1308ab2a 23283+
4a4d8108
AM
23284+ err = 0;
23285+ a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
23286+ a->src_hdir = au_hi(a->src_dir, a->btgt);
23287+ a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
23288+ a->dst_hdir = au_hi(a->dst_dir, a->btgt);
86dc4139
AM
23289+
23290+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
23291+ if (unlikely(err))
23292+ goto out;
f2c43d5f 23293+ au_fset_ren(a->auren_flags, MNT_WRITE);
8b6a4947
AM
23294+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23295+ struct dentry *root;
23296+ struct inode *dir;
23297+
23298+ /*
23299+ * sbinfo is already locked, so this ii_read_lock is
23300+ * unnecessary. but our debugging feature checks it.
23301+ */
23302+ root = a->src_inode->i_sb->s_root;
23303+ if (root != a->src_parent && root != a->dst_parent) {
23304+ dir = d_inode(root);
23305+ ii_read_lock_parent3(dir);
23306+ a->h_root = au_hi(dir, a->btgt);
23307+ ii_read_unlock(dir);
23308+ au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
23309+ }
23310+ }
4a4d8108
AM
23311+ a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
23312+ a->dst_h_parent, a->dst_hdir);
23313+ udba = au_opt_udba(a->src_dentry->d_sb);
5527c038
JR
23314+ if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
23315+ || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
4a4d8108 23316+ err = au_busy_or_stale();
5afbbe0d 23317+ if (!err && au_dbtop(a->src_dentry) == a->btgt)
4a4d8108 23318+ err = au_h_verify(a->src_h_dentry, udba,
5527c038 23319+ d_inode(a->src_h_parent), a->src_h_parent,
4a4d8108 23320+ a->br);
5afbbe0d 23321+ if (!err && au_dbtop(a->dst_dentry) == a->btgt)
4a4d8108 23322+ err = au_h_verify(a->dst_h_dentry, udba,
5527c038 23323+ d_inode(a->dst_h_parent), a->dst_h_parent,
4a4d8108 23324+ a->br);
86dc4139 23325+ if (!err)
4a4d8108 23326+ goto out; /* success */
4a4d8108
AM
23327+
23328+ err = au_busy_or_stale();
4a4d8108 23329+ au_ren_unlock(a);
86dc4139 23330+
4f0767ce 23331+out:
4a4d8108 23332+ return err;
1facf9fc 23333+}
23334+
23335+/* ---------------------------------------------------------------------- */
23336+
4a4d8108 23337+static void au_ren_refresh_dir(struct au_ren_args *a)
1facf9fc 23338+{
4a4d8108 23339+ struct inode *dir;
dece6358 23340+
4a4d8108 23341+ dir = a->dst_dir;
be118d29 23342+ inode_inc_iversion(dir);
f2c43d5f 23343+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
4a4d8108
AM
23344+ /* is this updating defined in POSIX? */
23345+ au_cpup_attr_timesizes(a->src_inode);
23346+ au_cpup_attr_nlink(dir, /*force*/1);
4a4d8108 23347+ }
b912730e 23348+ au_dir_ts(dir, a->btgt);
dece6358 23349+
f2c43d5f
AM
23350+ if (a->exchange) {
23351+ dir = a->src_dir;
be118d29 23352+ inode_inc_iversion(dir);
f2c43d5f
AM
23353+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23354+ /* is this updating defined in POSIX? */
23355+ au_cpup_attr_timesizes(a->dst_inode);
23356+ au_cpup_attr_nlink(dir, /*force*/1);
23357+ }
23358+ au_dir_ts(dir, a->btgt);
23359+ }
23360+
23361+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108 23362+ return;
dece6358 23363+
4a4d8108 23364+ dir = a->src_dir;
be118d29 23365+ inode_inc_iversion(dir);
f2c43d5f 23366+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
4a4d8108 23367+ au_cpup_attr_nlink(dir, /*force*/1);
b912730e 23368+ au_dir_ts(dir, a->btgt);
1facf9fc 23369+}
23370+
4a4d8108 23371+static void au_ren_refresh(struct au_ren_args *a)
1facf9fc 23372+{
5afbbe0d 23373+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
23374+ struct dentry *d, *h_d;
23375+ struct inode *i, *h_i;
23376+ struct super_block *sb;
dece6358 23377+
027c5e7a
AM
23378+ d = a->dst_dentry;
23379+ d_drop(d);
23380+ if (a->h_dst)
23381+ /* already dget-ed by au_ren_or_cpup() */
23382+ au_set_h_dptr(d, a->btgt, a->h_dst);
23383+
23384+ i = a->dst_inode;
23385+ if (i) {
f2c43d5f
AM
23386+ if (!a->exchange) {
23387+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
23388+ vfsub_drop_nlink(i);
23389+ else {
23390+ vfsub_dead_dir(i);
23391+ au_cpup_attr_timesizes(i);
23392+ }
23393+ au_update_dbrange(d, /*do_put_zero*/1);
23394+ } else
23395+ au_cpup_attr_nlink(i, /*force*/1);
027c5e7a 23396+ } else {
5afbbe0d
AM
23397+ bbot = a->btgt;
23398+ for (bindex = au_dbtop(d); bindex < bbot; bindex++)
027c5e7a 23399+ au_set_h_dptr(d, bindex, NULL);
5afbbe0d
AM
23400+ bbot = au_dbbot(d);
23401+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
027c5e7a
AM
23402+ au_set_h_dptr(d, bindex, NULL);
23403+ au_update_dbrange(d, /*do_put_zero*/0);
23404+ }
23405+
8b6a4947
AM
23406+ if (a->exchange
23407+ || au_ftest_ren(a->auren_flags, DIRREN)) {
23408+ d_drop(a->src_dentry);
23409+ if (au_ftest_ren(a->auren_flags, DIRREN))
23410+ au_set_dbwh(a->src_dentry, -1);
23411+ return;
23412+ }
23413+
4a4d8108 23414+ d = a->src_dentry;
8b6a4947
AM
23415+ au_set_dbwh(d, -1);
23416+ bbot = au_dbbot(d);
23417+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23418+ h_d = au_h_dptr(d, bindex);
23419+ if (h_d)
23420+ au_set_h_dptr(d, bindex, NULL);
23421+ }
23422+ au_set_dbbot(d, a->btgt);
4a4d8108 23423+
8b6a4947
AM
23424+ sb = d->d_sb;
23425+ i = a->src_inode;
23426+ if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
23427+ return; /* success */
4a4d8108 23428+
8b6a4947
AM
23429+ bbot = au_ibbot(i);
23430+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23431+ h_i = au_h_iptr(i, bindex);
23432+ if (h_i) {
23433+ au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
23434+ /* ignore this error */
23435+ au_set_h_iptr(i, bindex, NULL, 0);
4a4d8108
AM
23436+ }
23437+ }
8b6a4947 23438+ au_set_ibbot(i, a->btgt);
1308ab2a 23439+}
dece6358 23440+
4a4d8108
AM
23441+/* ---------------------------------------------------------------------- */
23442+
23443+/* mainly for link(2) and rename(2) */
23444+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
1308ab2a 23445+{
4a4d8108
AM
23446+ aufs_bindex_t bdiropq, bwh;
23447+ struct dentry *parent;
23448+ struct au_branch *br;
23449+
23450+ parent = dentry->d_parent;
5527c038 23451+ IMustLock(d_inode(parent)); /* dir is locked */
4a4d8108
AM
23452+
23453+ bdiropq = au_dbdiropq(parent);
23454+ bwh = au_dbwh(dentry);
23455+ br = au_sbr(dentry->d_sb, btgt);
23456+ if (au_br_rdonly(br)
23457+ || (0 <= bdiropq && bdiropq < btgt)
23458+ || (0 <= bwh && bwh < btgt))
23459+ btgt = -1;
23460+
23461+ AuDbg("btgt %d\n", btgt);
23462+ return btgt;
1facf9fc 23463+}
23464+
5afbbe0d 23465+/* sets src_btop, dst_btop and btgt */
4a4d8108 23466+static int au_ren_wbr(struct au_ren_args *a)
1facf9fc 23467+{
4a4d8108
AM
23468+ int err;
23469+ struct au_wr_dir_args wr_dir_args = {
23470+ /* .force_btgt = -1, */
23471+ .flags = AuWrDir_ADD_ENTRY
23472+ };
dece6358 23473+
5afbbe0d
AM
23474+ a->src_btop = au_dbtop(a->src_dentry);
23475+ a->dst_btop = au_dbtop(a->dst_dentry);
f2c43d5f
AM
23476+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
23477+ || au_ftest_ren(a->auren_flags, ISDIR_DST))
4a4d8108 23478+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
5afbbe0d
AM
23479+ wr_dir_args.force_btgt = a->src_btop;
23480+ if (a->dst_inode && a->dst_btop < a->src_btop)
23481+ wr_dir_args.force_btgt = a->dst_btop;
4a4d8108
AM
23482+ wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
23483+ err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
23484+ a->btgt = err;
f2c43d5f
AM
23485+ if (a->exchange)
23486+ au_update_dbtop(a->dst_dentry);
dece6358 23487+
4a4d8108 23488+ return err;
1facf9fc 23489+}
23490+
4a4d8108 23491+static void au_ren_dt(struct au_ren_args *a)
1facf9fc 23492+{
4a4d8108
AM
23493+ a->h_path.dentry = a->src_h_parent;
23494+ au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
f2c43d5f 23495+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
4a4d8108
AM
23496+ a->h_path.dentry = a->dst_h_parent;
23497+ au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
23498+ }
1facf9fc 23499+
f2c43d5f
AM
23500+ au_fclr_ren(a->auren_flags, DT_DSTDIR);
23501+ if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
23502+ && !a->exchange)
4a4d8108 23503+ return;
dece6358 23504+
4a4d8108
AM
23505+ a->h_path.dentry = a->src_h_dentry;
23506+ au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
5527c038 23507+ if (d_is_positive(a->dst_h_dentry)) {
f2c43d5f 23508+ au_fset_ren(a->auren_flags, DT_DSTDIR);
4a4d8108
AM
23509+ a->h_path.dentry = a->dst_h_dentry;
23510+ au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
23511+ }
1308ab2a 23512+}
dece6358 23513+
4a4d8108 23514+static void au_ren_rev_dt(int err, struct au_ren_args *a)
1308ab2a 23515+{
4a4d8108 23516+ struct dentry *h_d;
febd17d6 23517+ struct inode *h_inode;
4a4d8108
AM
23518+
23519+ au_dtime_revert(a->src_dt + AuPARENT);
f2c43d5f 23520+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108
AM
23521+ au_dtime_revert(a->dst_dt + AuPARENT);
23522+
f2c43d5f 23523+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
4a4d8108 23524+ h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
23525+ h_inode = d_inode(h_d);
23526+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 23527+ au_dtime_revert(a->src_dt + AuCHILD);
febd17d6 23528+ inode_unlock(h_inode);
4a4d8108 23529+
f2c43d5f 23530+ if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
4a4d8108 23531+ h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
23532+ h_inode = d_inode(h_d);
23533+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 23534+ au_dtime_revert(a->dst_dt + AuCHILD);
febd17d6 23535+ inode_unlock(h_inode);
1facf9fc 23536+ }
23537+ }
23538+}
23539+
4a4d8108
AM
23540+/* ---------------------------------------------------------------------- */
23541+
23542+int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
f2c43d5f
AM
23543+ struct inode *_dst_dir, struct dentry *_dst_dentry,
23544+ unsigned int _flags)
1facf9fc 23545+{
f2c43d5f 23546+ int err, lock_flags;
8b6a4947 23547+ void *rev;
4a4d8108
AM
23548+ /* reduce stack space */
23549+ struct au_ren_args *a;
f2c43d5f 23550+ struct au_pin pin;
4a4d8108 23551+
f2c43d5f 23552+ AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
4a4d8108
AM
23553+ IMustLock(_src_dir);
23554+ IMustLock(_dst_dir);
23555+
f2c43d5f
AM
23556+ err = -EINVAL;
23557+ if (unlikely(_flags & RENAME_WHITEOUT))
23558+ goto out;
23559+
4a4d8108
AM
23560+ err = -ENOMEM;
23561+ BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
23562+ a = kzalloc(sizeof(*a), GFP_NOFS);
23563+ if (unlikely(!a))
23564+ goto out;
23565+
f2c43d5f
AM
23566+ a->flags = _flags;
23567+ a->exchange = _flags & RENAME_EXCHANGE;
4a4d8108
AM
23568+ a->src_dir = _src_dir;
23569+ a->src_dentry = _src_dentry;
5527c038
JR
23570+ a->src_inode = NULL;
23571+ if (d_really_is_positive(a->src_dentry))
23572+ a->src_inode = d_inode(a->src_dentry);
4a4d8108
AM
23573+ a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
23574+ a->dst_dir = _dst_dir;
23575+ a->dst_dentry = _dst_dentry;
5527c038
JR
23576+ a->dst_inode = NULL;
23577+ if (d_really_is_positive(a->dst_dentry))
23578+ a->dst_inode = d_inode(a->dst_dentry);
4a4d8108
AM
23579+ a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
23580+ if (a->dst_inode) {
f2c43d5f
AM
23581+ /*
23582+ * if EXCHANGE && src is non-dir && dst is dir,
23583+ * dst is not locked.
23584+ */
23585+ /* IMustLock(a->dst_inode); */
4a4d8108 23586+ au_igrab(a->dst_inode);
1facf9fc 23587+ }
1facf9fc 23588+
4a4d8108 23589+ err = -ENOTDIR;
f2c43d5f 23590+ lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
2000de60 23591+ if (d_is_dir(a->src_dentry)) {
f2c43d5f
AM
23592+ au_fset_ren(a->auren_flags, ISDIR_SRC);
23593+ if (unlikely(!a->exchange
23594+ && d_really_is_positive(a->dst_dentry)
2000de60 23595+ && !d_is_dir(a->dst_dentry)))
4a4d8108 23596+ goto out_free;
f2c43d5f
AM
23597+ lock_flags |= AuLock_DIRS;
23598+ }
23599+ if (a->dst_inode && d_is_dir(a->dst_dentry)) {
23600+ au_fset_ren(a->auren_flags, ISDIR_DST);
23601+ if (unlikely(!a->exchange
23602+ && d_really_is_positive(a->src_dentry)
23603+ && !d_is_dir(a->src_dentry)))
23604+ goto out_free;
23605+ lock_flags |= AuLock_DIRS;
b95c5147 23606+ }
8b6a4947
AM
23607+ err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
23608+ lock_flags);
e49829fe
JR
23609+ if (unlikely(err))
23610+ goto out_free;
1facf9fc 23611+
027c5e7a
AM
23612+ err = au_d_hashed_positive(a->src_dentry);
23613+ if (unlikely(err))
23614+ goto out_unlock;
23615+ err = -ENOENT;
23616+ if (a->dst_inode) {
23617+ /*
f2c43d5f 23618+ * If it is a dir, VFS unhash it before this
027c5e7a
AM
23619+ * function. It means we cannot rely upon d_unhashed().
23620+ */
23621+ if (unlikely(!a->dst_inode->i_nlink))
23622+ goto out_unlock;
f2c43d5f 23623+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
027c5e7a 23624+ err = au_d_hashed_positive(a->dst_dentry);
f2c43d5f 23625+ if (unlikely(err && !a->exchange))
027c5e7a
AM
23626+ goto out_unlock;
23627+ } else if (unlikely(IS_DEADDIR(a->dst_inode)))
23628+ goto out_unlock;
23629+ } else if (unlikely(d_unhashed(a->dst_dentry)))
23630+ goto out_unlock;
23631+
7eafdf33
AM
23632+ /*
23633+ * is it possible?
79b8bda9 23634+ * yes, it happened (in linux-3.3-rcN) but I don't know why.
7eafdf33
AM
23635+ * there may exist a problem somewhere else.
23636+ */
23637+ err = -EINVAL;
5527c038 23638+ if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
7eafdf33
AM
23639+ goto out_unlock;
23640+
f2c43d5f 23641+ au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
4a4d8108 23642+ di_write_lock_parent(a->dst_parent);
1facf9fc 23643+
4a4d8108
AM
23644+ /* which branch we process */
23645+ err = au_ren_wbr(a);
23646+ if (unlikely(err < 0))
027c5e7a 23647+ goto out_parent;
4a4d8108 23648+ a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
86dc4139 23649+ a->h_path.mnt = au_br_mnt(a->br);
1facf9fc 23650+
4a4d8108
AM
23651+ /* are they available to be renamed */
23652+ err = au_ren_may_dir(a);
23653+ if (unlikely(err))
23654+ goto out_children;
1facf9fc 23655+
4a4d8108 23656+ /* prepare the writable parent dir on the same branch */
5afbbe0d 23657+ if (a->dst_btop == a->btgt) {
f2c43d5f 23658+ au_fset_ren(a->auren_flags, WHDST);
4a4d8108
AM
23659+ } else {
23660+ err = au_cpup_dirs(a->dst_dentry, a->btgt);
23661+ if (unlikely(err))
23662+ goto out_children;
23663+ }
1facf9fc 23664+
f2c43d5f
AM
23665+ err = 0;
23666+ if (!a->exchange) {
23667+ if (a->src_dir != a->dst_dir) {
23668+ /*
23669+ * this temporary unlock is safe,
23670+ * because both dir->i_mutex are locked.
23671+ */
23672+ di_write_unlock(a->dst_parent);
23673+ di_write_lock_parent(a->src_parent);
23674+ err = au_wr_dir_need_wh(a->src_dentry,
23675+ au_ftest_ren(a->auren_flags,
23676+ ISDIR_SRC),
23677+ &a->btgt);
23678+ di_write_unlock(a->src_parent);
23679+ di_write_lock2_parent(a->src_parent, a->dst_parent,
23680+ /*isdir*/1);
23681+ au_fclr_ren(a->auren_flags, ISSAMEDIR);
23682+ } else
23683+ err = au_wr_dir_need_wh(a->src_dentry,
23684+ au_ftest_ren(a->auren_flags,
23685+ ISDIR_SRC),
23686+ &a->btgt);
23687+ }
4a4d8108
AM
23688+ if (unlikely(err < 0))
23689+ goto out_children;
23690+ if (err)
f2c43d5f 23691+ au_fset_ren(a->auren_flags, WHSRC);
1facf9fc 23692+
86dc4139 23693+ /* cpup src */
5afbbe0d 23694+ if (a->src_btop != a->btgt) {
86dc4139
AM
23695+ err = au_pin(&pin, a->src_dentry, a->btgt,
23696+ au_opt_udba(a->src_dentry->d_sb),
23697+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 23698+ if (!err) {
c2b27bf2
AM
23699+ struct au_cp_generic cpg = {
23700+ .dentry = a->src_dentry,
23701+ .bdst = a->btgt,
5afbbe0d 23702+ .bsrc = a->src_btop,
c2b27bf2
AM
23703+ .len = -1,
23704+ .pin = &pin,
23705+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23706+ };
5afbbe0d 23707+ AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
c2b27bf2 23708+ err = au_sio_cpup_simple(&cpg);
367653fa 23709+ au_unpin(&pin);
86dc4139 23710+ }
86dc4139
AM
23711+ if (unlikely(err))
23712+ goto out_children;
5afbbe0d 23713+ a->src_btop = a->btgt;
86dc4139 23714+ a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
f2c43d5f
AM
23715+ if (!a->exchange)
23716+ au_fset_ren(a->auren_flags, WHSRC);
23717+ }
23718+
23719+ /* cpup dst */
23720+ if (a->exchange && a->dst_inode
23721+ && a->dst_btop != a->btgt) {
23722+ err = au_pin(&pin, a->dst_dentry, a->btgt,
23723+ au_opt_udba(a->dst_dentry->d_sb),
23724+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23725+ if (!err) {
23726+ struct au_cp_generic cpg = {
23727+ .dentry = a->dst_dentry,
23728+ .bdst = a->btgt,
23729+ .bsrc = a->dst_btop,
23730+ .len = -1,
23731+ .pin = &pin,
23732+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23733+ };
23734+ err = au_sio_cpup_simple(&cpg);
23735+ au_unpin(&pin);
23736+ }
23737+ if (unlikely(err))
23738+ goto out_children;
23739+ a->dst_btop = a->btgt;
23740+ a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
86dc4139
AM
23741+ }
23742+
4a4d8108
AM
23743+ /* lock them all */
23744+ err = au_ren_lock(a);
23745+ if (unlikely(err))
86dc4139 23746+ /* leave the copied-up one */
4a4d8108 23747+ goto out_children;
1facf9fc 23748+
f2c43d5f
AM
23749+ if (!a->exchange) {
23750+ if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
23751+ err = au_may_ren(a);
23752+ else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
23753+ err = -ENAMETOOLONG;
23754+ if (unlikely(err))
23755+ goto out_hdir;
23756+ }
1facf9fc 23757+
4a4d8108
AM
23758+ /* store timestamps to be revertible */
23759+ au_ren_dt(a);
1facf9fc 23760+
8b6a4947
AM
23761+ /* store dirren info */
23762+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23763+ err = au_dr_rename(a->src_dentry, a->btgt,
23764+ &a->dst_dentry->d_name, &rev);
23765+ AuTraceErr(err);
23766+ if (unlikely(err))
23767+ goto out_dt;
23768+ }
23769+
4a4d8108
AM
23770+ /* here we go */
23771+ err = do_rename(a);
23772+ if (unlikely(err))
8b6a4947
AM
23773+ goto out_dirren;
23774+
23775+ if (au_ftest_ren(a->auren_flags, DIRREN))
23776+ au_dr_rename_fin(a->src_dentry, a->btgt, rev);
4a4d8108
AM
23777+
23778+ /* update dir attributes */
23779+ au_ren_refresh_dir(a);
23780+
23781+ /* dput/iput all lower dentries */
23782+ au_ren_refresh(a);
23783+
23784+ goto out_hdir; /* success */
23785+
8b6a4947
AM
23786+out_dirren:
23787+ if (au_ftest_ren(a->auren_flags, DIRREN))
23788+ au_dr_rename_rev(a->src_dentry, a->btgt, rev);
4f0767ce 23789+out_dt:
4a4d8108 23790+ au_ren_rev_dt(err, a);
4f0767ce 23791+out_hdir:
4a4d8108 23792+ au_ren_unlock(a);
4f0767ce 23793+out_children:
4a4d8108 23794+ au_nhash_wh_free(&a->whlist);
5afbbe0d
AM
23795+ if (err && a->dst_inode && a->dst_btop != a->btgt) {
23796+ AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
027c5e7a 23797+ au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
5afbbe0d 23798+ au_set_dbtop(a->dst_dentry, a->dst_btop);
4a4d8108 23799+ }
027c5e7a 23800+out_parent:
f2c43d5f 23801+ if (!err) {
8b6a4947
AM
23802+ if (d_unhashed(a->src_dentry))
23803+ au_fset_ren(a->auren_flags, DROPPED_SRC);
23804+ if (d_unhashed(a->dst_dentry))
23805+ au_fset_ren(a->auren_flags, DROPPED_DST);
f2c43d5f
AM
23806+ if (!a->exchange)
23807+ d_move(a->src_dentry, a->dst_dentry);
8b6a4947 23808+ else {
f2c43d5f 23809+ d_exchange(a->src_dentry, a->dst_dentry);
8b6a4947
AM
23810+ if (au_ftest_ren(a->auren_flags, DROPPED_DST))
23811+ d_drop(a->dst_dentry);
23812+ }
23813+ if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
23814+ d_drop(a->src_dentry);
f2c43d5f 23815+ } else {
5afbbe0d 23816+ au_update_dbtop(a->dst_dentry);
027c5e7a
AM
23817+ if (!a->dst_inode)
23818+ d_drop(a->dst_dentry);
23819+ }
f2c43d5f 23820+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108
AM
23821+ di_write_unlock(a->dst_parent);
23822+ else
23823+ di_write_unlock2(a->src_parent, a->dst_parent);
027c5e7a 23824+out_unlock:
4a4d8108 23825+ aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
4f0767ce 23826+out_free:
4a4d8108
AM
23827+ iput(a->dst_inode);
23828+ if (a->thargs)
23829+ au_whtmp_rmdir_free(a->thargs);
1c60b727 23830+ kfree(a);
4f0767ce 23831+out:
4a4d8108
AM
23832+ AuTraceErr(err);
23833+ return err;
1308ab2a 23834+}
7f207e10
AM
23835diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
23836--- /usr/share/empty/fs/aufs/Kconfig 1970-01-01 01:00:00.000000000 +0100
b00004a5 23837+++ linux/fs/aufs/Kconfig 2018-06-04 09:08:09.181412645 +0200
2121bcd9
AM
23838@@ -0,0 +1,199 @@
23839+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
23840+config AUFS_FS
23841+ tristate "Aufs (Advanced multi layered unification filesystem) support"
4a4d8108
AM
23842+ help
23843+ Aufs is a stackable unification filesystem such as Unionfs,
23844+ which unifies several directories and provides a merged single
23845+ directory.
23846+ In the early days, aufs was entirely re-designed and
23847+ re-implemented Unionfs Version 1.x series. Introducing many
23848+ original ideas, approaches and improvements, it becomes totally
23849+ different from Unionfs while keeping the basic features.
1facf9fc 23850+
4a4d8108
AM
23851+if AUFS_FS
23852+choice
23853+ prompt "Maximum number of branches"
23854+ default AUFS_BRANCH_MAX_127
23855+ help
23856+ Specifies the maximum number of branches (or member directories)
23857+ in a single aufs. The larger value consumes more system
23858+ resources and has a minor impact to performance.
23859+config AUFS_BRANCH_MAX_127
23860+ bool "127"
23861+ help
23862+ Specifies the maximum number of branches (or member directories)
23863+ in a single aufs. The larger value consumes more system
23864+ resources and has a minor impact to performance.
23865+config AUFS_BRANCH_MAX_511
23866+ bool "511"
23867+ help
23868+ Specifies the maximum number of branches (or member directories)
23869+ in a single aufs. The larger value consumes more system
23870+ resources and has a minor impact to performance.
23871+config AUFS_BRANCH_MAX_1023
23872+ bool "1023"
23873+ help
23874+ Specifies the maximum number of branches (or member directories)
23875+ in a single aufs. The larger value consumes more system
23876+ resources and has a minor impact to performance.
23877+config AUFS_BRANCH_MAX_32767
23878+ bool "32767"
23879+ help
23880+ Specifies the maximum number of branches (or member directories)
23881+ in a single aufs. The larger value consumes more system
23882+ resources and has a minor impact to performance.
23883+endchoice
1facf9fc 23884+
e49829fe
JR
23885+config AUFS_SBILIST
23886+ bool
23887+ depends on AUFS_MAGIC_SYSRQ || PROC_FS
23888+ default y
23889+ help
23890+ Automatic configuration for internal use.
23891+ When aufs supports Magic SysRq or /proc, enabled automatically.
23892+
4a4d8108
AM
23893+config AUFS_HNOTIFY
23894+ bool "Detect direct branch access (bypassing aufs)"
23895+ help
23896+ If you want to modify files on branches directly, eg. bypassing aufs,
23897+ and want aufs to detect the changes of them fully, then enable this
23898+ option and use 'udba=notify' mount option.
7f207e10 23899+ Currently there is only one available configuration, "fsnotify".
4a4d8108
AM
23900+ It will have a negative impact to the performance.
23901+ See detail in aufs.5.
dece6358 23902+
4a4d8108
AM
23903+choice
23904+ prompt "method" if AUFS_HNOTIFY
23905+ default AUFS_HFSNOTIFY
23906+config AUFS_HFSNOTIFY
23907+ bool "fsnotify"
23908+ select FSNOTIFY
4a4d8108 23909+endchoice
1facf9fc 23910+
4a4d8108
AM
23911+config AUFS_EXPORT
23912+ bool "NFS-exportable aufs"
2cbb1c4b 23913+ depends on EXPORTFS
4a4d8108
AM
23914+ help
23915+ If you want to export your mounted aufs via NFS, then enable this
23916+ option. There are several requirements for this configuration.
23917+ See detail in aufs.5.
1facf9fc 23918+
4a4d8108
AM
23919+config AUFS_INO_T_64
23920+ bool
23921+ depends on AUFS_EXPORT
23922+ depends on 64BIT && !(ALPHA || S390)
23923+ default y
23924+ help
23925+ Automatic configuration for internal use.
23926+ /* typedef unsigned long/int __kernel_ino_t */
23927+ /* alpha and s390x are int */
1facf9fc 23928+
c1595e42
JR
23929+config AUFS_XATTR
23930+ bool "support for XATTR/EA (including Security Labels)"
23931+ help
23932+ If your branch fs supports XATTR/EA and you want to make them
23933+ available in aufs too, then enable this opsion and specify the
23934+ branch attributes for EA.
23935+ See detail in aufs.5.
23936+
076b876e
AM
23937+config AUFS_FHSM
23938+ bool "File-based Hierarchical Storage Management"
23939+ help
23940+ Hierarchical Storage Management (or HSM) is a well-known feature
23941+ in the storage world. Aufs provides this feature as file-based.
23942+ with multiple branches.
23943+ These multiple branches are prioritized, ie. the topmost one
23944+ should be the fastest drive and be used heavily.
23945+
4a4d8108
AM
23946+config AUFS_RDU
23947+ bool "Readdir in userspace"
23948+ help
23949+ Aufs has two methods to provide a merged view for a directory,
23950+ by a user-space library and by kernel-space natively. The latter
23951+ is always enabled but sometimes large and slow.
23952+ If you enable this option, install the library in aufs2-util
23953+ package, and set some environment variables for your readdir(3),
23954+ then the work will be handled in user-space which generally
23955+ shows better performance in most cases.
23956+ See detail in aufs.5.
1facf9fc 23957+
8b6a4947
AM
23958+config AUFS_DIRREN
23959+ bool "Workaround for rename(2)-ing a directory"
23960+ help
23961+ By default, aufs returns EXDEV error in renameing a dir who has
23962+ his child on the lower branch, since it is a bad idea to issue
23963+ rename(2) internally for every lower branch. But user may not
23964+ accept this behaviour. So here is a workaround to allow such
23965+ rename(2) and store some extra infromation on the writable
23966+ branch. Obviously this costs high (and I don't like it).
23967+ To use this feature, you need to enable this configuration AND
23968+ to specify the mount option `dirren.'
23969+ See details in aufs.5 and the design documents.
23970+
4a4d8108
AM
23971+config AUFS_SHWH
23972+ bool "Show whiteouts"
23973+ help
23974+ If you want to make the whiteouts in aufs visible, then enable
23975+ this option and specify 'shwh' mount option. Although it may
23976+ sounds like philosophy or something, but in technically it
23977+ simply shows the name of whiteout with keeping its behaviour.
1facf9fc 23978+
4a4d8108
AM
23979+config AUFS_BR_RAMFS
23980+ bool "Ramfs (initramfs/rootfs) as an aufs branch"
23981+ help
23982+ If you want to use ramfs as an aufs branch fs, then enable this
23983+ option. Generally tmpfs is recommended.
23984+ Aufs prohibited them to be a branch fs by default, because
23985+ initramfs becomes unusable after switch_root or something
23986+ generally. If you sets initramfs as an aufs branch and boot your
23987+ system by switch_root, you will meet a problem easily since the
23988+ files in initramfs may be inaccessible.
23989+ Unless you are going to use ramfs as an aufs branch fs without
23990+ switch_root or something, leave it N.
1facf9fc 23991+
4a4d8108
AM
23992+config AUFS_BR_FUSE
23993+ bool "Fuse fs as an aufs branch"
23994+ depends on FUSE_FS
23995+ select AUFS_POLL
23996+ help
23997+ If you want to use fuse-based userspace filesystem as an aufs
23998+ branch fs, then enable this option.
23999+ It implements the internal poll(2) operation which is
24000+ implemented by fuse only (curretnly).
1facf9fc 24001+
4a4d8108
AM
24002+config AUFS_POLL
24003+ bool
24004+ help
24005+ Automatic configuration for internal use.
1facf9fc 24006+
4a4d8108
AM
24007+config AUFS_BR_HFSPLUS
24008+ bool "Hfsplus as an aufs branch"
24009+ depends on HFSPLUS_FS
24010+ default y
24011+ help
24012+ If you want to use hfsplus fs as an aufs branch fs, then enable
24013+ this option. This option introduces a small overhead at
24014+ copying-up a file on hfsplus.
1facf9fc 24015+
4a4d8108
AM
24016+config AUFS_BDEV_LOOP
24017+ bool
24018+ depends on BLK_DEV_LOOP
24019+ default y
24020+ help
24021+ Automatic configuration for internal use.
24022+ Convert =[ym] into =y.
1308ab2a 24023+
4a4d8108
AM
24024+config AUFS_DEBUG
24025+ bool "Debug aufs"
24026+ help
24027+ Enable this to compile aufs internal debug code.
24028+ It will have a negative impact to the performance.
24029+
24030+config AUFS_MAGIC_SYSRQ
24031+ bool
24032+ depends on AUFS_DEBUG && MAGIC_SYSRQ
24033+ default y
24034+ help
24035+ Automatic configuration for internal use.
24036+ When aufs supports Magic SysRq, enabled automatically.
24037+endif
7f207e10
AM
24038diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
24039--- /usr/share/empty/fs/aufs/loop.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 24040+++ linux/fs/aufs/loop.c 2018-08-12 23:43:05.460124736 +0200
062440b3 24041@@ -0,0 +1,148 @@
cd7a4cd9 24042+// SPDX-License-Identifier: GPL-2.0
1facf9fc 24043+/*
b00004a5 24044+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 24045+ *
24046+ * This program, aufs is free software; you can redistribute it and/or modify
24047+ * it under the terms of the GNU General Public License as published by
24048+ * the Free Software Foundation; either version 2 of the License, or
24049+ * (at your option) any later version.
dece6358
AM
24050+ *
24051+ * This program is distributed in the hope that it will be useful,
24052+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24053+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24054+ * GNU General Public License for more details.
24055+ *
24056+ * You should have received a copy of the GNU General Public License
523b37e3 24057+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24058+ */
24059+
24060+/*
24061+ * support for loopback block device as a branch
24062+ */
24063+
1facf9fc 24064+#include "aufs.h"
24065+
392086de
AM
24066+/* added into drivers/block/loop.c */
24067+static struct file *(*backing_file_func)(struct super_block *sb);
24068+
1facf9fc 24069+/*
24070+ * test if two lower dentries have overlapping branches.
24071+ */
b752ccd1 24072+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
1facf9fc 24073+{
b752ccd1 24074+ struct super_block *h_sb;
392086de
AM
24075+ struct file *backing_file;
24076+
24077+ if (unlikely(!backing_file_func)) {
24078+ /* don't load "loop" module here */
24079+ backing_file_func = symbol_get(loop_backing_file);
24080+ if (unlikely(!backing_file_func))
24081+ /* "loop" module is not loaded */
24082+ return 0;
24083+ }
1facf9fc 24084+
b752ccd1 24085+ h_sb = h_adding->d_sb;
392086de
AM
24086+ backing_file = backing_file_func(h_sb);
24087+ if (!backing_file)
1facf9fc 24088+ return 0;
24089+
2000de60 24090+ h_adding = backing_file->f_path.dentry;
b752ccd1
AM
24091+ /*
24092+ * h_adding can be local NFS.
24093+ * in this case aufs cannot detect the loop.
24094+ */
24095+ if (unlikely(h_adding->d_sb == sb))
1facf9fc 24096+ return 1;
b752ccd1 24097+ return !!au_test_subdir(h_adding, sb->s_root);
1facf9fc 24098+}
24099+
24100+/* true if a kernel thread named 'loop[0-9].*' accesses a file */
24101+int au_test_loopback_kthread(void)
24102+{
b752ccd1
AM
24103+ int ret;
24104+ struct task_struct *tsk = current;
a2a7ad62 24105+ char c, comm[sizeof(tsk->comm)];
b752ccd1
AM
24106+
24107+ ret = 0;
24108+ if (tsk->flags & PF_KTHREAD) {
a2a7ad62
AM
24109+ get_task_comm(comm, tsk);
24110+ c = comm[4];
b752ccd1 24111+ ret = ('0' <= c && c <= '9'
a2a7ad62 24112+ && !strncmp(comm, "loop", 4));
b752ccd1 24113+ }
1facf9fc 24114+
b752ccd1 24115+ return ret;
1facf9fc 24116+}
87a755f4
AM
24117+
24118+/* ---------------------------------------------------------------------- */
24119+
24120+#define au_warn_loopback_step 16
24121+static int au_warn_loopback_nelem = au_warn_loopback_step;
24122+static unsigned long *au_warn_loopback_array;
24123+
24124+void au_warn_loopback(struct super_block *h_sb)
24125+{
24126+ int i, new_nelem;
24127+ unsigned long *a, magic;
24128+ static DEFINE_SPINLOCK(spin);
24129+
24130+ magic = h_sb->s_magic;
24131+ spin_lock(&spin);
24132+ a = au_warn_loopback_array;
24133+ for (i = 0; i < au_warn_loopback_nelem && *a; i++)
24134+ if (a[i] == magic) {
24135+ spin_unlock(&spin);
24136+ return;
24137+ }
24138+
24139+ /* h_sb is new to us, print it */
24140+ if (i < au_warn_loopback_nelem) {
24141+ a[i] = magic;
24142+ goto pr;
24143+ }
24144+
24145+ /* expand the array */
24146+ new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
24147+ a = au_kzrealloc(au_warn_loopback_array,
24148+ au_warn_loopback_nelem * sizeof(unsigned long),
e2f27e51
AM
24149+ new_nelem * sizeof(unsigned long), GFP_ATOMIC,
24150+ /*may_shrink*/0);
87a755f4
AM
24151+ if (a) {
24152+ au_warn_loopback_nelem = new_nelem;
24153+ au_warn_loopback_array = a;
24154+ a[i] = magic;
24155+ goto pr;
24156+ }
24157+
24158+ spin_unlock(&spin);
24159+ AuWarn1("realloc failed, ignored\n");
24160+ return;
24161+
24162+pr:
24163+ spin_unlock(&spin);
0c3ec466
AM
24164+ pr_warn("you may want to try another patch for loopback file "
24165+ "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
87a755f4
AM
24166+}
24167+
24168+int au_loopback_init(void)
24169+{
24170+ int err;
24171+ struct super_block *sb __maybe_unused;
24172+
79b8bda9 24173+ BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(unsigned long));
87a755f4
AM
24174+
24175+ err = 0;
24176+ au_warn_loopback_array = kcalloc(au_warn_loopback_step,
24177+ sizeof(unsigned long), GFP_NOFS);
24178+ if (unlikely(!au_warn_loopback_array))
24179+ err = -ENOMEM;
24180+
24181+ return err;
24182+}
24183+
24184+void au_loopback_fin(void)
24185+{
79b8bda9
AM
24186+ if (backing_file_func)
24187+ symbol_put(loop_backing_file);
1c60b727 24188+ kfree(au_warn_loopback_array);
87a755f4 24189+}
7f207e10
AM
24190diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
24191--- /usr/share/empty/fs/aufs/loop.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 24192+++ linux/fs/aufs/loop.h 2018-08-12 23:43:05.460124736 +0200
062440b3
AM
24193@@ -0,0 +1,53 @@
24194+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 24195+/*
b00004a5 24196+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 24197+ *
24198+ * This program, aufs is free software; you can redistribute it and/or modify
24199+ * it under the terms of the GNU General Public License as published by
24200+ * the Free Software Foundation; either version 2 of the License, or
24201+ * (at your option) any later version.
dece6358
AM
24202+ *
24203+ * This program is distributed in the hope that it will be useful,
24204+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24205+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24206+ * GNU General Public License for more details.
24207+ *
24208+ * You should have received a copy of the GNU General Public License
523b37e3 24209+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24210+ */
24211+
24212+/*
24213+ * support for loopback mount as a branch
24214+ */
24215+
24216+#ifndef __AUFS_LOOP_H__
24217+#define __AUFS_LOOP_H__
24218+
24219+#ifdef __KERNEL__
24220+
dece6358
AM
24221+struct dentry;
24222+struct super_block;
1facf9fc 24223+
24224+#ifdef CONFIG_AUFS_BDEV_LOOP
392086de
AM
24225+/* drivers/block/loop.c */
24226+struct file *loop_backing_file(struct super_block *sb);
24227+
1facf9fc 24228+/* loop.c */
b752ccd1 24229+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
1facf9fc 24230+int au_test_loopback_kthread(void);
87a755f4
AM
24231+void au_warn_loopback(struct super_block *h_sb);
24232+
24233+int au_loopback_init(void);
24234+void au_loopback_fin(void);
1facf9fc 24235+#else
4a4d8108 24236+AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
b752ccd1 24237+ struct dentry *h_adding)
4a4d8108 24238+AuStubInt0(au_test_loopback_kthread, void)
87a755f4
AM
24239+AuStubVoid(au_warn_loopback, struct super_block *h_sb)
24240+
24241+AuStubInt0(au_loopback_init, void)
24242+AuStubVoid(au_loopback_fin, void)
1facf9fc 24243+#endif /* BLK_DEV_LOOP */
24244+
24245+#endif /* __KERNEL__ */
24246+#endif /* __AUFS_LOOP_H__ */
7f207e10
AM
24247diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
24248--- /usr/share/empty/fs/aufs/magic.mk 1970-01-01 01:00:00.000000000 +0100
b00004a5 24249+++ linux/fs/aufs/magic.mk 2018-06-04 09:08:09.188079511 +0200
2121bcd9
AM
24250@@ -0,0 +1,31 @@
24251+# SPDX-License-Identifier: GPL-2.0
1facf9fc 24252+
24253+# defined in ${srctree}/fs/fuse/inode.c
24254+# tristate
24255+ifdef CONFIG_FUSE_FS
24256+ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
24257+endif
24258+
1facf9fc 24259+# defined in ${srctree}/fs/xfs/xfs_sb.h
24260+# tristate
24261+ifdef CONFIG_XFS_FS
24262+ccflags-y += -DXFS_SB_MAGIC=0x58465342
24263+endif
24264+
24265+# defined in ${srctree}/fs/configfs/mount.c
24266+# tristate
24267+ifdef CONFIG_CONFIGFS_FS
24268+ccflags-y += -DCONFIGFS_MAGIC=0x62656570
24269+endif
24270+
1facf9fc 24271+# defined in ${srctree}/fs/ubifs/ubifs.h
24272+# tristate
24273+ifdef CONFIG_UBIFS_FS
24274+ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
24275+endif
4a4d8108
AM
24276+
24277+# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
24278+# tristate
24279+ifdef CONFIG_HFSPLUS_FS
24280+ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
24281+endif
7f207e10
AM
24282diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
24283--- /usr/share/empty/fs/aufs/Makefile 1970-01-01 01:00:00.000000000 +0100
b00004a5 24284+++ linux/fs/aufs/Makefile 2018-06-04 09:08:09.181412645 +0200
2121bcd9
AM
24285@@ -0,0 +1,46 @@
24286+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
24287+
24288+include ${src}/magic.mk
24289+ifeq (${CONFIG_AUFS_FS},m)
24290+include ${src}/conf.mk
24291+endif
24292+-include ${src}/priv_def.mk
24293+
24294+# cf. include/linux/kernel.h
24295+# enable pr_debug
24296+ccflags-y += -DDEBUG
f6c5ef8b
AM
24297+# sparse requires the full pathname
24298+ifdef M
523b37e3 24299+ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
f6c5ef8b 24300+else
523b37e3 24301+ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
f6c5ef8b 24302+endif
4a4d8108
AM
24303+
24304+obj-$(CONFIG_AUFS_FS) += aufs.o
24305+aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
24306+ wkq.o vfsub.o dcsub.o \
e49829fe 24307+ cpup.o whout.o wbr_policy.o \
4a4d8108
AM
24308+ dinfo.o dentry.o \
24309+ dynop.o \
24310+ finfo.o file.o f_op.o \
24311+ dir.o vdir.o \
24312+ iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
c2b27bf2 24313+ mvdown.o ioctl.o
4a4d8108
AM
24314+
24315+# all are boolean
e49829fe 24316+aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
4a4d8108
AM
24317+aufs-$(CONFIG_SYSFS) += sysfs.o
24318+aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
24319+aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
24320+aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
24321+aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
4a4d8108 24322+aufs-$(CONFIG_AUFS_EXPORT) += export.o
c1595e42
JR
24323+aufs-$(CONFIG_AUFS_XATTR) += xattr.o
24324+aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
8b6a4947 24325+aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
076b876e 24326+aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
4a4d8108
AM
24327+aufs-$(CONFIG_AUFS_POLL) += poll.o
24328+aufs-$(CONFIG_AUFS_RDU) += rdu.o
4a4d8108
AM
24329+aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
24330+aufs-$(CONFIG_AUFS_DEBUG) += debug.o
24331+aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
7f207e10
AM
24332diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
24333--- /usr/share/empty/fs/aufs/module.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 24334+++ linux/fs/aufs/module.c 2018-08-12 23:43:05.460124736 +0200
062440b3 24335@@ -0,0 +1,273 @@
cd7a4cd9 24336+// SPDX-License-Identifier: GPL-2.0
1facf9fc 24337+/*
b00004a5 24338+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 24339+ *
24340+ * This program, aufs is free software; you can redistribute it and/or modify
24341+ * it under the terms of the GNU General Public License as published by
24342+ * the Free Software Foundation; either version 2 of the License, or
24343+ * (at your option) any later version.
dece6358
AM
24344+ *
24345+ * This program is distributed in the hope that it will be useful,
24346+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24347+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24348+ * GNU General Public License for more details.
24349+ *
24350+ * You should have received a copy of the GNU General Public License
523b37e3 24351+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24352+ */
24353+
24354+/*
24355+ * module global variables and operations
24356+ */
24357+
24358+#include <linux/module.h>
24359+#include <linux/seq_file.h>
24360+#include "aufs.h"
24361+
e2f27e51
AM
24362+/* shrinkable realloc */
24363+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
1facf9fc 24364+{
e2f27e51
AM
24365+ size_t sz;
24366+ int diff;
1facf9fc 24367+
e2f27e51
AM
24368+ sz = 0;
24369+ diff = -1;
24370+ if (p) {
24371+#if 0 /* unused */
24372+ if (!new_sz) {
1c60b727 24373+ kfree(p);
e2f27e51
AM
24374+ p = NULL;
24375+ goto out;
24376+ }
24377+#else
24378+ AuDebugOn(!new_sz);
24379+#endif
24380+ sz = ksize(p);
24381+ diff = au_kmidx_sub(sz, new_sz);
24382+ }
24383+ if (sz && !diff)
24384+ goto out;
24385+
24386+ if (sz < new_sz)
24387+ /* expand or SLOB */
24388+ p = krealloc(p, new_sz, gfp);
24389+ else if (new_sz < sz && may_shrink) {
24390+ /* shrink */
24391+ void *q;
24392+
24393+ q = kmalloc(new_sz, gfp);
24394+ if (q) {
24395+ if (p) {
24396+ memcpy(q, p, new_sz);
1c60b727 24397+ kfree(p);
e2f27e51
AM
24398+ }
24399+ p = q;
24400+ } else
24401+ p = NULL;
24402+ }
24403+
24404+out:
24405+ return p;
24406+}
24407+
24408+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24409+ int may_shrink)
24410+{
24411+ p = au_krealloc(p, new_sz, gfp, may_shrink);
24412+ if (p && new_sz > nused)
1facf9fc 24413+ memset(p + nused, 0, new_sz - nused);
24414+ return p;
24415+}
24416+
24417+/* ---------------------------------------------------------------------- */
1facf9fc 24418+/*
24419+ * aufs caches
24420+ */
1c60b727 24421+struct kmem_cache *au_cache[AuCache_Last];
5afbbe0d
AM
24422+
24423+static void au_cache_fin(void)
24424+{
24425+ int i;
24426+
24427+ /*
24428+ * Make sure all delayed rcu free inodes are flushed before we
24429+ * destroy cache.
24430+ */
24431+ rcu_barrier();
24432+
24433+ /* excluding AuCache_HNOTIFY */
24434+ BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
24435+ for (i = 0; i < AuCache_HNOTIFY; i++) {
1c60b727
AM
24436+ kmem_cache_destroy(au_cache[i]);
24437+ au_cache[i] = NULL;
5afbbe0d
AM
24438+ }
24439+}
24440+
1facf9fc 24441+static int __init au_cache_init(void)
24442+{
1c60b727
AM
24443+ au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
24444+ if (au_cache[AuCache_DINFO])
027c5e7a 24445+ /* SLAB_DESTROY_BY_RCU */
1c60b727 24446+ au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
f0c0a007 24447+ au_icntnr_init_once);
1c60b727
AM
24448+ if (au_cache[AuCache_ICNTNR])
24449+ au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
f0c0a007 24450+ au_fi_init_once);
1c60b727
AM
24451+ if (au_cache[AuCache_FINFO])
24452+ au_cache[AuCache_VDIR] = AuCache(au_vdir);
24453+ if (au_cache[AuCache_VDIR])
24454+ au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
24455+ if (au_cache[AuCache_DEHSTR])
1facf9fc 24456+ return 0;
24457+
5afbbe0d 24458+ au_cache_fin();
1facf9fc 24459+ return -ENOMEM;
24460+}
24461+
1facf9fc 24462+/* ---------------------------------------------------------------------- */
24463+
24464+int au_dir_roflags;
24465+
e49829fe 24466+#ifdef CONFIG_AUFS_SBILIST
1e00d052
AM
24467+/*
24468+ * iterate_supers_type() doesn't protect us from
24469+ * remounting (branch management)
24470+ */
8b6a4947 24471+struct hlist_bl_head au_sbilist;
e49829fe
JR
24472+#endif
24473+
1facf9fc 24474+/*
24475+ * functions for module interface.
24476+ */
24477+MODULE_LICENSE("GPL");
24478+/* MODULE_LICENSE("GPL v2"); */
dece6358 24479+MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
1facf9fc 24480+MODULE_DESCRIPTION(AUFS_NAME
24481+ " -- Advanced multi layered unification filesystem");
24482+MODULE_VERSION(AUFS_VERSION);
c06a8ce3 24483+MODULE_ALIAS_FS(AUFS_NAME);
1facf9fc 24484+
1facf9fc 24485+/* this module parameter has no meaning when SYSFS is disabled */
24486+int sysaufs_brs = 1;
24487+MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
cd7a4cd9 24488+module_param_named(brs, sysaufs_brs, int, 0444);
1facf9fc 24489+
076b876e 24490+/* this module parameter has no meaning when USER_NS is disabled */
8cdd5066 24491+bool au_userns;
076b876e 24492+MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
cd7a4cd9 24493+module_param_named(allow_userns, au_userns, bool, 0444);
076b876e 24494+
1facf9fc 24495+/* ---------------------------------------------------------------------- */
24496+
24497+static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
24498+
24499+int au_seq_path(struct seq_file *seq, struct path *path)
24500+{
79b8bda9
AM
24501+ int err;
24502+
24503+ err = seq_path(seq, path, au_esc_chars);
1c60b727 24504+ if (err >= 0)
79b8bda9 24505+ err = 0;
1c60b727 24506+ else
79b8bda9
AM
24507+ err = -ENOMEM;
24508+
24509+ return err;
1facf9fc 24510+}
24511+
24512+/* ---------------------------------------------------------------------- */
24513+
24514+static int __init aufs_init(void)
24515+{
24516+ int err, i;
24517+ char *p;
24518+
24519+ p = au_esc_chars;
24520+ for (i = 1; i <= ' '; i++)
24521+ *p++ = i;
24522+ *p++ = '\\';
24523+ *p++ = '\x7f';
24524+ *p = 0;
24525+
24526+ au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
24527+
b95c5147
AM
24528+ memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
24529+ for (i = 0; i < AuIop_Last; i++)
24530+ aufs_iop_nogetattr[i].getattr = NULL;
24531+
1c60b727 24532+ memset(au_cache, 0, sizeof(au_cache)); /* including hnotify */
f0c0a007 24533+
e49829fe 24534+ au_sbilist_init();
1facf9fc 24535+ sysaufs_brs_init();
24536+ au_debug_init();
4a4d8108 24537+ au_dy_init();
1facf9fc 24538+ err = sysaufs_init();
24539+ if (unlikely(err))
24540+ goto out;
062440b3 24541+ err = dbgaufs_init();
4f0767ce 24542+ if (unlikely(err))
953406b4 24543+ goto out_sysaufs;
062440b3
AM
24544+ err = au_procfs_init();
24545+ if (unlikely(err))
24546+ goto out_dbgaufs;
e49829fe
JR
24547+ err = au_wkq_init();
24548+ if (unlikely(err))
24549+ goto out_procfs;
87a755f4 24550+ err = au_loopback_init();
1facf9fc 24551+ if (unlikely(err))
24552+ goto out_wkq;
87a755f4
AM
24553+ err = au_hnotify_init();
24554+ if (unlikely(err))
24555+ goto out_loopback;
1facf9fc 24556+ err = au_sysrq_init();
24557+ if (unlikely(err))
24558+ goto out_hin;
24559+ err = au_cache_init();
24560+ if (unlikely(err))
24561+ goto out_sysrq;
076b876e
AM
24562+
24563+ aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
1facf9fc 24564+ err = register_filesystem(&aufs_fs_type);
24565+ if (unlikely(err))
24566+ goto out_cache;
076b876e 24567+
4a4d8108
AM
24568+ /* since we define pr_fmt, call printk directly */
24569+ printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
1facf9fc 24570+ goto out; /* success */
24571+
4f0767ce 24572+out_cache:
1facf9fc 24573+ au_cache_fin();
4f0767ce 24574+out_sysrq:
1facf9fc 24575+ au_sysrq_fin();
4f0767ce 24576+out_hin:
4a4d8108 24577+ au_hnotify_fin();
87a755f4
AM
24578+out_loopback:
24579+ au_loopback_fin();
4f0767ce 24580+out_wkq:
1facf9fc 24581+ au_wkq_fin();
e49829fe
JR
24582+out_procfs:
24583+ au_procfs_fin();
062440b3
AM
24584+out_dbgaufs:
24585+ dbgaufs_fin();
4f0767ce 24586+out_sysaufs:
1facf9fc 24587+ sysaufs_fin();
4a4d8108 24588+ au_dy_fin();
4f0767ce 24589+out:
1facf9fc 24590+ return err;
24591+}
24592+
24593+static void __exit aufs_exit(void)
24594+{
24595+ unregister_filesystem(&aufs_fs_type);
24596+ au_cache_fin();
24597+ au_sysrq_fin();
4a4d8108 24598+ au_hnotify_fin();
87a755f4 24599+ au_loopback_fin();
1facf9fc 24600+ au_wkq_fin();
e49829fe 24601+ au_procfs_fin();
062440b3 24602+ dbgaufs_fin();
1facf9fc 24603+ sysaufs_fin();
4a4d8108 24604+ au_dy_fin();
1facf9fc 24605+}
24606+
24607+module_init(aufs_init);
24608+module_exit(aufs_exit);
7f207e10
AM
24609diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
24610--- /usr/share/empty/fs/aufs/module.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 24611+++ linux/fs/aufs/module.h 2018-08-12 23:43:05.460124736 +0200
062440b3
AM
24612@@ -0,0 +1,102 @@
24613+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 24614+/*
b00004a5 24615+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 24616+ *
24617+ * This program, aufs is free software; you can redistribute it and/or modify
24618+ * it under the terms of the GNU General Public License as published by
24619+ * the Free Software Foundation; either version 2 of the License, or
24620+ * (at your option) any later version.
dece6358
AM
24621+ *
24622+ * This program is distributed in the hope that it will be useful,
24623+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24624+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24625+ * GNU General Public License for more details.
24626+ *
24627+ * You should have received a copy of the GNU General Public License
523b37e3 24628+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24629+ */
24630+
24631+/*
24632+ * module initialization and module-global
24633+ */
24634+
24635+#ifndef __AUFS_MODULE_H__
24636+#define __AUFS_MODULE_H__
24637+
24638+#ifdef __KERNEL__
24639+
24640+#include <linux/slab.h>
24641+
dece6358
AM
24642+struct path;
24643+struct seq_file;
24644+
1facf9fc 24645+/* module parameters */
1facf9fc 24646+extern int sysaufs_brs;
8cdd5066 24647+extern bool au_userns;
1facf9fc 24648+
24649+/* ---------------------------------------------------------------------- */
24650+
24651+extern int au_dir_roflags;
24652+
e2f27e51
AM
24653+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
24654+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24655+ int may_shrink);
24656+
24657+static inline int au_kmidx_sub(size_t sz, size_t new_sz)
24658+{
24659+#ifndef CONFIG_SLOB
24660+ return kmalloc_index(sz) - kmalloc_index(new_sz);
24661+#else
24662+ return -1; /* SLOB is untested */
24663+#endif
24664+}
24665+
1facf9fc 24666+int au_seq_path(struct seq_file *seq, struct path *path);
24667+
e49829fe
JR
24668+#ifdef CONFIG_PROC_FS
24669+/* procfs.c */
24670+int __init au_procfs_init(void);
24671+void au_procfs_fin(void);
24672+#else
24673+AuStubInt0(au_procfs_init, void);
24674+AuStubVoid(au_procfs_fin, void);
24675+#endif
24676+
4f0767ce
JR
24677+/* ---------------------------------------------------------------------- */
24678+
1c60b727 24679+/* kmem cache */
1facf9fc 24680+enum {
24681+ AuCache_DINFO,
24682+ AuCache_ICNTNR,
24683+ AuCache_FINFO,
24684+ AuCache_VDIR,
24685+ AuCache_DEHSTR,
7eafdf33 24686+ AuCache_HNOTIFY, /* must be last */
1facf9fc 24687+ AuCache_Last
24688+};
24689+
1c60b727 24690+extern struct kmem_cache *au_cache[AuCache_Last];
f0c0a007 24691+
4a4d8108
AM
24692+#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
24693+#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
24694+#define AuCacheCtor(type, ctor) \
24695+ kmem_cache_create(#type, sizeof(struct type), \
24696+ __alignof__(struct type), AuCacheFlags, ctor)
1facf9fc 24697+
1facf9fc 24698+#define AuCacheFuncs(name, index) \
4a4d8108 24699+static inline struct au_##name *au_cache_alloc_##name(void) \
1c60b727 24700+{ return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
4a4d8108 24701+static inline void au_cache_free_##name(struct au_##name *p) \
1c60b727 24702+{ kmem_cache_free(au_cache[AuCache_##index], p); }
1facf9fc 24703+
24704+AuCacheFuncs(dinfo, DINFO);
24705+AuCacheFuncs(icntnr, ICNTNR);
24706+AuCacheFuncs(finfo, FINFO);
24707+AuCacheFuncs(vdir, VDIR);
4a4d8108
AM
24708+AuCacheFuncs(vdir_dehstr, DEHSTR);
24709+#ifdef CONFIG_AUFS_HNOTIFY
24710+AuCacheFuncs(hnotify, HNOTIFY);
24711+#endif
1facf9fc 24712+
4a4d8108
AM
24713+#endif /* __KERNEL__ */
24714+#endif /* __AUFS_MODULE_H__ */
c2b27bf2
AM
24715diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
24716--- /usr/share/empty/fs/aufs/mvdown.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 24717+++ linux/fs/aufs/mvdown.c 2018-08-12 23:43:05.460124736 +0200
062440b3 24718@@ -0,0 +1,705 @@
cd7a4cd9 24719+// SPDX-License-Identifier: GPL-2.0
c2b27bf2 24720+/*
b00004a5 24721+ * Copyright (C) 2011-2018 Junjiro R. Okajima
c2b27bf2
AM
24722+ *
24723+ * This program, aufs is free software; you can redistribute it and/or modify
24724+ * it under the terms of the GNU General Public License as published by
24725+ * the Free Software Foundation; either version 2 of the License, or
24726+ * (at your option) any later version.
24727+ *
24728+ * This program is distributed in the hope that it will be useful,
24729+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24730+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24731+ * GNU General Public License for more details.
24732+ *
24733+ * You should have received a copy of the GNU General Public License
523b37e3
AM
24734+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
24735+ */
24736+
24737+/*
24738+ * move-down, opposite of copy-up
c2b27bf2
AM
24739+ */
24740+
24741+#include "aufs.h"
24742+
c2b27bf2
AM
24743+struct au_mvd_args {
24744+ struct {
c2b27bf2
AM
24745+ struct super_block *h_sb;
24746+ struct dentry *h_parent;
24747+ struct au_hinode *hdir;
392086de 24748+ struct inode *h_dir, *h_inode;
c1595e42 24749+ struct au_pin pin;
c2b27bf2
AM
24750+ } info[AUFS_MVDOWN_NARRAY];
24751+
24752+ struct aufs_mvdown mvdown;
24753+ struct dentry *dentry, *parent;
24754+ struct inode *inode, *dir;
24755+ struct super_block *sb;
24756+ aufs_bindex_t bopq, bwh, bfound;
24757+ unsigned char rename_lock;
c2b27bf2
AM
24758+};
24759+
392086de 24760+#define mvd_errno mvdown.au_errno
076b876e
AM
24761+#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
24762+#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
24763+#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
24764+#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
c2b27bf2 24765+
392086de
AM
24766+#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
24767+#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
24768+#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
24769+#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
24770+#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
c1595e42 24771+#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
392086de
AM
24772+
24773+#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
24774+#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
24775+#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
24776+#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
24777+#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
c1595e42 24778+#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
c2b27bf2
AM
24779+
24780+#define AU_MVD_PR(flag, ...) do { \
24781+ if (flag) \
24782+ pr_err(__VA_ARGS__); \
24783+ } while (0)
24784+
076b876e
AM
24785+static int find_lower_writable(struct au_mvd_args *a)
24786+{
24787+ struct super_block *sb;
5afbbe0d 24788+ aufs_bindex_t bindex, bbot;
076b876e
AM
24789+ struct au_branch *br;
24790+
24791+ sb = a->sb;
24792+ bindex = a->mvd_bsrc;
5afbbe0d 24793+ bbot = au_sbbot(sb);
076b876e 24794+ if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
5afbbe0d 24795+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
24796+ br = au_sbr(sb, bindex);
24797+ if (au_br_fhsm(br->br_perm)
8b6a4947 24798+ && !sb_rdonly(au_br_sb(br)))
076b876e
AM
24799+ return bindex;
24800+ }
24801+ else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
5afbbe0d 24802+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
24803+ br = au_sbr(sb, bindex);
24804+ if (!au_br_rdonly(br))
24805+ return bindex;
24806+ }
24807+ else
5afbbe0d 24808+ for (bindex++; bindex <= bbot; bindex++) {
076b876e 24809+ br = au_sbr(sb, bindex);
8b6a4947 24810+ if (!sb_rdonly(au_br_sb(br))) {
076b876e
AM
24811+ if (au_br_rdonly(br))
24812+ a->mvdown.flags
24813+ |= AUFS_MVDOWN_ROLOWER_R;
24814+ return bindex;
24815+ }
24816+ }
24817+
24818+ return -1;
24819+}
24820+
c2b27bf2 24821+/* make the parent dir on bdst */
392086de 24822+static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24823+{
24824+ int err;
24825+
24826+ err = 0;
24827+ a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
24828+ a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
24829+ a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
24830+ a->mvd_h_dst_parent = NULL;
5afbbe0d 24831+ if (au_dbbot(a->parent) >= a->mvd_bdst)
c2b27bf2
AM
24832+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
24833+ if (!a->mvd_h_dst_parent) {
24834+ err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
24835+ if (unlikely(err)) {
392086de 24836+ AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
c2b27bf2
AM
24837+ goto out;
24838+ }
24839+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
24840+ }
24841+
24842+out:
24843+ AuTraceErr(err);
24844+ return err;
24845+}
24846+
24847+/* lock them all */
392086de 24848+static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24849+{
24850+ int err;
24851+ struct dentry *h_trap;
24852+
24853+ a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
24854+ a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
c1595e42
JR
24855+ err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
24856+ au_opt_udba(a->sb),
24857+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
24858+ AuTraceErr(err);
24859+ if (unlikely(err)) {
24860+ AU_MVD_PR(dmsg, "pin_dst failed\n");
24861+ goto out;
24862+ }
24863+
c2b27bf2
AM
24864+ if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
24865+ a->rename_lock = 0;
c1595e42
JR
24866+ au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
24867+ AuLsc_DI_PARENT, AuLsc_I_PARENT3,
24868+ au_opt_udba(a->sb),
24869+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
24870+ err = au_do_pin(&a->mvd_pin_src);
24871+ AuTraceErr(err);
5527c038 24872+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
24873+ if (unlikely(err)) {
24874+ AU_MVD_PR(dmsg, "pin_src failed\n");
24875+ goto out_dst;
24876+ }
24877+ goto out; /* success */
c2b27bf2
AM
24878+ }
24879+
c2b27bf2 24880+ a->rename_lock = 1;
c1595e42
JR
24881+ au_pin_hdir_unlock(&a->mvd_pin_dst);
24882+ err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
24883+ au_opt_udba(a->sb),
24884+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
24885+ AuTraceErr(err);
5527c038 24886+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
24887+ if (unlikely(err)) {
24888+ AU_MVD_PR(dmsg, "pin_src failed\n");
24889+ au_pin_hdir_lock(&a->mvd_pin_dst);
24890+ goto out_dst;
24891+ }
24892+ au_pin_hdir_unlock(&a->mvd_pin_src);
c2b27bf2
AM
24893+ h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
24894+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
24895+ if (h_trap) {
24896+ err = (h_trap != a->mvd_h_src_parent);
24897+ if (err)
24898+ err = (h_trap != a->mvd_h_dst_parent);
24899+ }
24900+ BUG_ON(err); /* it should never happen */
c1595e42
JR
24901+ if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
24902+ err = -EBUSY;
24903+ AuTraceErr(err);
24904+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
24905+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
24906+ au_pin_hdir_lock(&a->mvd_pin_src);
24907+ au_unpin(&a->mvd_pin_src);
24908+ au_pin_hdir_lock(&a->mvd_pin_dst);
24909+ goto out_dst;
24910+ }
24911+ goto out; /* success */
c2b27bf2 24912+
c1595e42
JR
24913+out_dst:
24914+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
24915+out:
24916+ AuTraceErr(err);
24917+ return err;
24918+}
24919+
392086de 24920+static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2 24921+{
c1595e42
JR
24922+ if (!a->rename_lock)
24923+ au_unpin(&a->mvd_pin_src);
24924+ else {
c2b27bf2
AM
24925+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
24926+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
c1595e42
JR
24927+ au_pin_hdir_lock(&a->mvd_pin_src);
24928+ au_unpin(&a->mvd_pin_src);
24929+ au_pin_hdir_lock(&a->mvd_pin_dst);
24930+ }
24931+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
24932+}
24933+
24934+/* copy-down the file */
392086de 24935+static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24936+{
24937+ int err;
24938+ struct au_cp_generic cpg = {
24939+ .dentry = a->dentry,
24940+ .bdst = a->mvd_bdst,
24941+ .bsrc = a->mvd_bsrc,
24942+ .len = -1,
c1595e42 24943+ .pin = &a->mvd_pin_dst,
c2b27bf2
AM
24944+ .flags = AuCpup_DTIME | AuCpup_HOPEN
24945+ };
24946+
24947+ AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
392086de
AM
24948+ if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
24949+ au_fset_cpup(cpg.flags, OVERWRITE);
24950+ if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
24951+ au_fset_cpup(cpg.flags, RWDST);
c2b27bf2
AM
24952+ err = au_sio_cpdown_simple(&cpg);
24953+ if (unlikely(err))
392086de 24954+ AU_MVD_PR(dmsg, "cpdown failed\n");
c2b27bf2
AM
24955+
24956+ AuTraceErr(err);
24957+ return err;
24958+}
24959+
24960+/*
24961+ * unlink the whiteout on bdst if exist which may be created by UDBA while we
24962+ * were sleeping
24963+ */
392086de 24964+static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24965+{
24966+ int err;
24967+ struct path h_path;
24968+ struct au_branch *br;
523b37e3 24969+ struct inode *delegated;
c2b27bf2
AM
24970+
24971+ br = au_sbr(a->sb, a->mvd_bdst);
24972+ h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
24973+ err = PTR_ERR(h_path.dentry);
24974+ if (IS_ERR(h_path.dentry)) {
392086de 24975+ AU_MVD_PR(dmsg, "wh_lkup failed\n");
c2b27bf2
AM
24976+ goto out;
24977+ }
24978+
24979+ err = 0;
5527c038 24980+ if (d_is_positive(h_path.dentry)) {
c2b27bf2 24981+ h_path.mnt = au_br_mnt(br);
523b37e3 24982+ delegated = NULL;
5527c038 24983+ err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
523b37e3
AM
24984+ &delegated, /*force*/0);
24985+ if (unlikely(err == -EWOULDBLOCK)) {
24986+ pr_warn("cannot retry for NFSv4 delegation"
24987+ " for an internal unlink\n");
24988+ iput(delegated);
24989+ }
c2b27bf2 24990+ if (unlikely(err))
392086de 24991+ AU_MVD_PR(dmsg, "wh_unlink failed\n");
c2b27bf2
AM
24992+ }
24993+ dput(h_path.dentry);
24994+
24995+out:
24996+ AuTraceErr(err);
24997+ return err;
24998+}
24999+
25000+/*
25001+ * unlink the topmost h_dentry
c2b27bf2 25002+ */
392086de 25003+static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25004+{
25005+ int err;
25006+ struct path h_path;
523b37e3 25007+ struct inode *delegated;
c2b27bf2
AM
25008+
25009+ h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
25010+ h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
523b37e3
AM
25011+ delegated = NULL;
25012+ err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
25013+ if (unlikely(err == -EWOULDBLOCK)) {
25014+ pr_warn("cannot retry for NFSv4 delegation"
25015+ " for an internal unlink\n");
25016+ iput(delegated);
25017+ }
c2b27bf2 25018+ if (unlikely(err))
392086de 25019+ AU_MVD_PR(dmsg, "unlink failed\n");
c2b27bf2
AM
25020+
25021+ AuTraceErr(err);
25022+ return err;
25023+}
25024+
076b876e
AM
25025+/* Since mvdown succeeded, we ignore an error of this function */
25026+static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
25027+{
25028+ int err;
25029+ struct au_branch *br;
25030+
25031+ a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
25032+ br = au_sbr(a->sb, a->mvd_bsrc);
25033+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
25034+ if (!err) {
25035+ br = au_sbr(a->sb, a->mvd_bdst);
25036+ a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
25037+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
25038+ }
25039+ if (!err)
25040+ a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
25041+ else
25042+ AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
25043+}
25044+
c2b27bf2
AM
25045+/*
25046+ * copy-down the file and unlink the bsrc file.
25047+ * - unlink the bdst whout if exist
25048+ * - copy-down the file (with whtmp name and rename)
25049+ * - unlink the bsrc file
25050+ */
392086de 25051+static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25052+{
25053+ int err;
25054+
392086de 25055+ err = au_do_mkdir(dmsg, a);
c2b27bf2 25056+ if (!err)
392086de 25057+ err = au_do_lock(dmsg, a);
c2b27bf2
AM
25058+ if (unlikely(err))
25059+ goto out;
25060+
25061+ /*
25062+ * do not revert the activities we made on bdst since they should be
25063+ * harmless in aufs.
25064+ */
25065+
392086de 25066+ err = au_do_cpdown(dmsg, a);
c2b27bf2 25067+ if (!err)
392086de
AM
25068+ err = au_do_unlink_wh(dmsg, a);
25069+ if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
25070+ err = au_do_unlink(dmsg, a);
c2b27bf2
AM
25071+ if (unlikely(err))
25072+ goto out_unlock;
25073+
c1595e42
JR
25074+ AuDbg("%pd2, 0x%x, %d --> %d\n",
25075+ a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
076b876e
AM
25076+ if (find_lower_writable(a) < 0)
25077+ a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
25078+
25079+ if (a->mvdown.flags & AUFS_MVDOWN_STFS)
25080+ au_do_stfs(dmsg, a);
25081+
c2b27bf2 25082+ /* maintain internal array */
392086de
AM
25083+ if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
25084+ au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
5afbbe0d 25085+ au_set_dbtop(a->dentry, a->mvd_bdst);
392086de 25086+ au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
5afbbe0d 25087+ au_set_ibtop(a->inode, a->mvd_bdst);
79b8bda9
AM
25088+ } else {
25089+ /* hide the lower */
25090+ au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
5afbbe0d 25091+ au_set_dbbot(a->dentry, a->mvd_bsrc);
79b8bda9 25092+ au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
5afbbe0d 25093+ au_set_ibbot(a->inode, a->mvd_bsrc);
392086de 25094+ }
5afbbe0d
AM
25095+ if (au_dbbot(a->dentry) < a->mvd_bdst)
25096+ au_set_dbbot(a->dentry, a->mvd_bdst);
25097+ if (au_ibbot(a->inode) < a->mvd_bdst)
25098+ au_set_ibbot(a->inode, a->mvd_bdst);
c2b27bf2
AM
25099+
25100+out_unlock:
392086de 25101+ au_do_unlock(dmsg, a);
c2b27bf2
AM
25102+out:
25103+ AuTraceErr(err);
25104+ return err;
25105+}
25106+
25107+/* ---------------------------------------------------------------------- */
25108+
c2b27bf2 25109+/* make sure the file is idle */
392086de 25110+static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25111+{
25112+ int err, plinked;
c2b27bf2
AM
25113+
25114+ err = 0;
c2b27bf2 25115+ plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
5afbbe0d 25116+ if (au_dbtop(a->dentry) == a->mvd_bsrc
c1595e42 25117+ && au_dcount(a->dentry) == 1
c2b27bf2 25118+ && atomic_read(&a->inode->i_count) == 1
392086de 25119+ /* && a->mvd_h_src_inode->i_nlink == 1 */
c2b27bf2
AM
25120+ && (!plinked || !au_plink_test(a->inode))
25121+ && a->inode->i_nlink == 1)
25122+ goto out;
25123+
25124+ err = -EBUSY;
392086de 25125+ AU_MVD_PR(dmsg,
c1595e42 25126+ "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
5afbbe0d 25127+ a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
c2b27bf2 25128+ atomic_read(&a->inode->i_count), a->inode->i_nlink,
392086de 25129+ a->mvd_h_src_inode->i_nlink,
c2b27bf2
AM
25130+ plinked, plinked ? au_plink_test(a->inode) : 0);
25131+
25132+out:
25133+ AuTraceErr(err);
25134+ return err;
25135+}
25136+
25137+/* make sure the parent dir is fine */
392086de 25138+static int au_mvd_args_parent(const unsigned char dmsg,
c2b27bf2
AM
25139+ struct au_mvd_args *a)
25140+{
25141+ int err;
25142+ aufs_bindex_t bindex;
25143+
25144+ err = 0;
25145+ if (unlikely(au_alive_dir(a->parent))) {
25146+ err = -ENOENT;
392086de 25147+ AU_MVD_PR(dmsg, "parent dir is dead\n");
c2b27bf2
AM
25148+ goto out;
25149+ }
25150+
25151+ a->bopq = au_dbdiropq(a->parent);
25152+ bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
25153+ AuDbg("b%d\n", bindex);
25154+ if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
25155+ || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
25156+ err = -EINVAL;
392086de
AM
25157+ a->mvd_errno = EAU_MVDOWN_OPAQUE;
25158+ AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
c2b27bf2
AM
25159+ a->bopq, a->mvd_bdst);
25160+ }
25161+
25162+out:
25163+ AuTraceErr(err);
25164+ return err;
25165+}
25166+
392086de 25167+static int au_mvd_args_intermediate(const unsigned char dmsg,
c2b27bf2
AM
25168+ struct au_mvd_args *a)
25169+{
25170+ int err;
25171+ struct au_dinfo *dinfo, *tmp;
25172+
25173+ /* lookup the next lower positive entry */
25174+ err = -ENOMEM;
25175+ tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
25176+ if (unlikely(!tmp))
25177+ goto out;
25178+
25179+ a->bfound = -1;
25180+ a->bwh = -1;
25181+ dinfo = au_di(a->dentry);
25182+ au_di_cp(tmp, dinfo);
25183+ au_di_swap(tmp, dinfo);
25184+
25185+ /* returns the number of positive dentries */
5afbbe0d
AM
25186+ err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
25187+ /* AuLkup_IGNORE_PERM */ 0);
c2b27bf2
AM
25188+ if (!err)
25189+ a->bwh = au_dbwh(a->dentry);
25190+ else if (err > 0)
5afbbe0d 25191+ a->bfound = au_dbtop(a->dentry);
c2b27bf2
AM
25192+
25193+ au_di_swap(tmp, dinfo);
25194+ au_rw_write_unlock(&tmp->di_rwsem);
25195+ au_di_free(tmp);
25196+ if (unlikely(err < 0))
392086de 25197+ AU_MVD_PR(dmsg, "failed look-up lower\n");
c2b27bf2
AM
25198+
25199+ /*
25200+ * here, we have these cases.
25201+ * bfound == -1
25202+ * no positive dentry under bsrc. there are more sub-cases.
25203+ * bwh < 0
25204+ * there no whiteout, we can safely move-down.
25205+ * bwh <= bsrc
25206+ * impossible
25207+ * bsrc < bwh && bwh < bdst
25208+ * there is a whiteout on RO branch. cannot proceed.
25209+ * bwh == bdst
25210+ * there is a whiteout on the RW target branch. it should
25211+ * be removed.
25212+ * bdst < bwh
25213+ * there is a whiteout somewhere unrelated branch.
25214+ * -1 < bfound && bfound <= bsrc
25215+ * impossible.
25216+ * bfound < bdst
25217+ * found, but it is on RO branch between bsrc and bdst. cannot
25218+ * proceed.
25219+ * bfound == bdst
25220+ * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
25221+ * error.
25222+ * bdst < bfound
25223+ * found, after we create the file on bdst, it will be hidden.
25224+ */
25225+
25226+ AuDebugOn(a->bfound == -1
25227+ && a->bwh != -1
25228+ && a->bwh <= a->mvd_bsrc);
25229+ AuDebugOn(-1 < a->bfound
25230+ && a->bfound <= a->mvd_bsrc);
25231+
25232+ err = -EINVAL;
25233+ if (a->bfound == -1
25234+ && a->mvd_bsrc < a->bwh
25235+ && a->bwh != -1
25236+ && a->bwh < a->mvd_bdst) {
392086de
AM
25237+ a->mvd_errno = EAU_MVDOWN_WHITEOUT;
25238+ AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
c2b27bf2
AM
25239+ a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
25240+ goto out;
25241+ } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
392086de
AM
25242+ a->mvd_errno = EAU_MVDOWN_UPPER;
25243+ AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
c2b27bf2
AM
25244+ a->mvd_bdst, a->bfound);
25245+ goto out;
25246+ }
25247+
25248+ err = 0; /* success */
25249+
25250+out:
25251+ AuTraceErr(err);
25252+ return err;
25253+}
25254+
392086de 25255+static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25256+{
25257+ int err;
25258+
392086de
AM
25259+ err = 0;
25260+ if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25261+ && a->bfound == a->mvd_bdst)
25262+ err = -EEXIST;
c2b27bf2
AM
25263+ AuTraceErr(err);
25264+ return err;
25265+}
25266+
392086de 25267+static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25268+{
25269+ int err;
25270+ struct au_branch *br;
25271+
25272+ err = -EISDIR;
25273+ if (unlikely(S_ISDIR(a->inode->i_mode)))
25274+ goto out;
25275+
25276+ err = -EINVAL;
392086de 25277+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
5afbbe0d 25278+ a->mvd_bsrc = au_ibtop(a->inode);
392086de
AM
25279+ else {
25280+ a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
25281+ if (unlikely(a->mvd_bsrc < 0
5afbbe0d
AM
25282+ || (a->mvd_bsrc < au_dbtop(a->dentry)
25283+ || au_dbbot(a->dentry) < a->mvd_bsrc
392086de 25284+ || !au_h_dptr(a->dentry, a->mvd_bsrc))
5afbbe0d
AM
25285+ || (a->mvd_bsrc < au_ibtop(a->inode)
25286+ || au_ibbot(a->inode) < a->mvd_bsrc
392086de
AM
25287+ || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
25288+ a->mvd_errno = EAU_MVDOWN_NOUPPER;
25289+ AU_MVD_PR(dmsg, "no upper\n");
25290+ goto out;
25291+ }
25292+ }
5afbbe0d 25293+ if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
392086de
AM
25294+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25295+ AU_MVD_PR(dmsg, "on the bottom\n");
c2b27bf2
AM
25296+ goto out;
25297+ }
392086de 25298+ a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
c2b27bf2
AM
25299+ br = au_sbr(a->sb, a->mvd_bsrc);
25300+ err = au_br_rdonly(br);
392086de
AM
25301+ if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
25302+ if (unlikely(err))
25303+ goto out;
25304+ } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
25305+ || IS_APPEND(a->mvd_h_src_inode))) {
25306+ if (err)
25307+ a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
25308+ /* go on */
25309+ } else
c2b27bf2
AM
25310+ goto out;
25311+
25312+ err = -EINVAL;
392086de
AM
25313+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
25314+ a->mvd_bdst = find_lower_writable(a);
25315+ if (unlikely(a->mvd_bdst < 0)) {
25316+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25317+ AU_MVD_PR(dmsg, "no writable lower branch\n");
25318+ goto out;
25319+ }
25320+ } else {
25321+ a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
25322+ if (unlikely(a->mvd_bdst < 0
5afbbe0d 25323+ || au_sbbot(a->sb) < a->mvd_bdst)) {
392086de
AM
25324+ a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
25325+ AU_MVD_PR(dmsg, "no lower brid\n");
25326+ goto out;
25327+ }
c2b27bf2
AM
25328+ }
25329+
392086de 25330+ err = au_mvd_args_busy(dmsg, a);
c2b27bf2 25331+ if (!err)
392086de 25332+ err = au_mvd_args_parent(dmsg, a);
c2b27bf2 25333+ if (!err)
392086de 25334+ err = au_mvd_args_intermediate(dmsg, a);
c2b27bf2 25335+ if (!err)
392086de 25336+ err = au_mvd_args_exist(dmsg, a);
c2b27bf2
AM
25337+ if (!err)
25338+ AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
25339+
25340+out:
25341+ AuTraceErr(err);
25342+ return err;
25343+}
25344+
25345+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
25346+{
392086de
AM
25347+ int err, e;
25348+ unsigned char dmsg;
25349+ struct au_mvd_args *args;
79b8bda9 25350+ struct inode *inode;
c2b27bf2 25351+
79b8bda9 25352+ inode = d_inode(dentry);
c2b27bf2
AM
25353+ err = -EPERM;
25354+ if (unlikely(!capable(CAP_SYS_ADMIN)))
25355+ goto out;
25356+
392086de
AM
25357+ err = -ENOMEM;
25358+ args = kmalloc(sizeof(*args), GFP_NOFS);
25359+ if (unlikely(!args))
25360+ goto out;
25361+
25362+ err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
25363+ if (!err)
25364+ err = !access_ok(VERIFY_WRITE, uarg, sizeof(*uarg));
c2b27bf2
AM
25365+ if (unlikely(err)) {
25366+ err = -EFAULT;
392086de
AM
25367+ AuTraceErr(err);
25368+ goto out_free;
c2b27bf2 25369+ }
392086de
AM
25370+ AuDbg("flags 0x%x\n", args->mvdown.flags);
25371+ args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
25372+ args->mvdown.au_errno = 0;
25373+ args->dentry = dentry;
79b8bda9 25374+ args->inode = inode;
392086de 25375+ args->sb = dentry->d_sb;
c2b27bf2 25376+
392086de
AM
25377+ err = -ENOENT;
25378+ dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
25379+ args->parent = dget_parent(dentry);
5527c038 25380+ args->dir = d_inode(args->parent);
febd17d6 25381+ inode_lock_nested(args->dir, I_MUTEX_PARENT);
392086de
AM
25382+ dput(args->parent);
25383+ if (unlikely(args->parent != dentry->d_parent)) {
25384+ AU_MVD_PR(dmsg, "parent dir is moved\n");
c2b27bf2
AM
25385+ goto out_dir;
25386+ }
25387+
febd17d6 25388+ inode_lock_nested(inode, I_MUTEX_CHILD);
b95c5147 25389+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
c2b27bf2
AM
25390+ if (unlikely(err))
25391+ goto out_inode;
25392+
392086de
AM
25393+ di_write_lock_parent(args->parent);
25394+ err = au_mvd_args(dmsg, args);
c2b27bf2
AM
25395+ if (unlikely(err))
25396+ goto out_parent;
25397+
392086de 25398+ err = au_do_mvdown(dmsg, args);
c2b27bf2
AM
25399+ if (unlikely(err))
25400+ goto out_parent;
c2b27bf2 25401+
392086de 25402+ au_cpup_attr_timesizes(args->dir);
79b8bda9
AM
25403+ au_cpup_attr_timesizes(inode);
25404+ if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
25405+ au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
c2b27bf2
AM
25406+ /* au_digen_dec(dentry); */
25407+
25408+out_parent:
392086de 25409+ di_write_unlock(args->parent);
c2b27bf2
AM
25410+ aufs_read_unlock(dentry, AuLock_DW);
25411+out_inode:
febd17d6 25412+ inode_unlock(inode);
c2b27bf2 25413+out_dir:
febd17d6 25414+ inode_unlock(args->dir);
392086de
AM
25415+out_free:
25416+ e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
25417+ if (unlikely(e))
25418+ err = -EFAULT;
1c60b727 25419+ kfree(args);
c2b27bf2
AM
25420+out:
25421+ AuTraceErr(err);
25422+ return err;
25423+}
25424diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
25425--- /usr/share/empty/fs/aufs/opts.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 25426+++ linux/fs/aufs/opts.c 2018-08-12 23:43:05.460124736 +0200
8b6a4947 25427@@ -0,0 +1,1891 @@
cd7a4cd9 25428+// SPDX-License-Identifier: GPL-2.0
1facf9fc 25429+/*
b00004a5 25430+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 25431+ *
25432+ * This program, aufs is free software; you can redistribute it and/or modify
25433+ * it under the terms of the GNU General Public License as published by
25434+ * the Free Software Foundation; either version 2 of the License, or
25435+ * (at your option) any later version.
dece6358
AM
25436+ *
25437+ * This program is distributed in the hope that it will be useful,
25438+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25439+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25440+ * GNU General Public License for more details.
25441+ *
25442+ * You should have received a copy of the GNU General Public License
523b37e3 25443+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 25444+ */
25445+
25446+/*
25447+ * mount options/flags
25448+ */
25449+
dece6358 25450+#include <linux/namei.h>
1facf9fc 25451+#include <linux/types.h> /* a distribution requires */
25452+#include <linux/parser.h>
25453+#include "aufs.h"
25454+
25455+/* ---------------------------------------------------------------------- */
25456+
25457+enum {
25458+ Opt_br,
7e9cd9fe
AM
25459+ Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
25460+ Opt_idel, Opt_imod,
25461+ Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
dece6358 25462+ Opt_rdblk_def, Opt_rdhash_def,
7e9cd9fe 25463+ Opt_xino, Opt_noxino,
1facf9fc 25464+ Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
25465+ Opt_trunc_xino_path, Opt_itrunc_xino,
25466+ Opt_trunc_xib, Opt_notrunc_xib,
dece6358 25467+ Opt_shwh, Opt_noshwh,
1facf9fc 25468+ Opt_plink, Opt_noplink, Opt_list_plink,
25469+ Opt_udba,
4a4d8108 25470+ Opt_dio, Opt_nodio,
1facf9fc 25471+ Opt_diropq_a, Opt_diropq_w,
25472+ Opt_warn_perm, Opt_nowarn_perm,
25473+ Opt_wbr_copyup, Opt_wbr_create,
076b876e 25474+ Opt_fhsm_sec,
1facf9fc 25475+ Opt_verbose, Opt_noverbose,
25476+ Opt_sum, Opt_nosum, Opt_wsum,
076b876e 25477+ Opt_dirperm1, Opt_nodirperm1,
8b6a4947 25478+ Opt_dirren, Opt_nodirren,
c1595e42 25479+ Opt_acl, Opt_noacl,
1facf9fc 25480+ Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
25481+};
25482+
25483+static match_table_t options = {
25484+ {Opt_br, "br=%s"},
25485+ {Opt_br, "br:%s"},
25486+
25487+ {Opt_add, "add=%d:%s"},
25488+ {Opt_add, "add:%d:%s"},
25489+ {Opt_add, "ins=%d:%s"},
25490+ {Opt_add, "ins:%d:%s"},
25491+ {Opt_append, "append=%s"},
25492+ {Opt_append, "append:%s"},
25493+ {Opt_prepend, "prepend=%s"},
25494+ {Opt_prepend, "prepend:%s"},
25495+
25496+ {Opt_del, "del=%s"},
25497+ {Opt_del, "del:%s"},
25498+ /* {Opt_idel, "idel:%d"}, */
25499+ {Opt_mod, "mod=%s"},
25500+ {Opt_mod, "mod:%s"},
25501+ /* {Opt_imod, "imod:%d:%s"}, */
25502+
25503+ {Opt_dirwh, "dirwh=%d"},
25504+
25505+ {Opt_xino, "xino=%s"},
25506+ {Opt_noxino, "noxino"},
25507+ {Opt_trunc_xino, "trunc_xino"},
25508+ {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
25509+ {Opt_notrunc_xino, "notrunc_xino"},
25510+ {Opt_trunc_xino_path, "trunc_xino=%s"},
25511+ {Opt_itrunc_xino, "itrunc_xino=%d"},
25512+ /* {Opt_zxino, "zxino=%s"}, */
25513+ {Opt_trunc_xib, "trunc_xib"},
25514+ {Opt_notrunc_xib, "notrunc_xib"},
25515+
e49829fe 25516+#ifdef CONFIG_PROC_FS
1facf9fc 25517+ {Opt_plink, "plink"},
e49829fe
JR
25518+#else
25519+ {Opt_ignore_silent, "plink"},
25520+#endif
25521+
1facf9fc 25522+ {Opt_noplink, "noplink"},
e49829fe 25523+
1facf9fc 25524+#ifdef CONFIG_AUFS_DEBUG
25525+ {Opt_list_plink, "list_plink"},
25526+#endif
25527+
25528+ {Opt_udba, "udba=%s"},
25529+
4a4d8108
AM
25530+ {Opt_dio, "dio"},
25531+ {Opt_nodio, "nodio"},
25532+
8b6a4947
AM
25533+#ifdef CONFIG_AUFS_DIRREN
25534+ {Opt_dirren, "dirren"},
25535+ {Opt_nodirren, "nodirren"},
25536+#else
25537+ {Opt_ignore, "dirren"},
25538+ {Opt_ignore_silent, "nodirren"},
25539+#endif
25540+
076b876e
AM
25541+#ifdef CONFIG_AUFS_FHSM
25542+ {Opt_fhsm_sec, "fhsm_sec=%d"},
25543+#else
8b6a4947 25544+ {Opt_ignore, "fhsm_sec=%d"},
076b876e
AM
25545+#endif
25546+
1facf9fc 25547+ {Opt_diropq_a, "diropq=always"},
25548+ {Opt_diropq_a, "diropq=a"},
25549+ {Opt_diropq_w, "diropq=whiteouted"},
25550+ {Opt_diropq_w, "diropq=w"},
25551+
25552+ {Opt_warn_perm, "warn_perm"},
25553+ {Opt_nowarn_perm, "nowarn_perm"},
25554+
25555+ /* keep them temporary */
1facf9fc 25556+ {Opt_ignore_silent, "nodlgt"},
8b6a4947 25557+ {Opt_ignore, "clean_plink"},
1facf9fc 25558+
dece6358
AM
25559+#ifdef CONFIG_AUFS_SHWH
25560+ {Opt_shwh, "shwh"},
25561+#endif
25562+ {Opt_noshwh, "noshwh"},
25563+
076b876e
AM
25564+ {Opt_dirperm1, "dirperm1"},
25565+ {Opt_nodirperm1, "nodirperm1"},
25566+
1facf9fc 25567+ {Opt_verbose, "verbose"},
25568+ {Opt_verbose, "v"},
25569+ {Opt_noverbose, "noverbose"},
25570+ {Opt_noverbose, "quiet"},
25571+ {Opt_noverbose, "q"},
25572+ {Opt_noverbose, "silent"},
25573+
25574+ {Opt_sum, "sum"},
25575+ {Opt_nosum, "nosum"},
25576+ {Opt_wsum, "wsum"},
25577+
25578+ {Opt_rdcache, "rdcache=%d"},
25579+ {Opt_rdblk, "rdblk=%d"},
dece6358 25580+ {Opt_rdblk_def, "rdblk=def"},
1facf9fc 25581+ {Opt_rdhash, "rdhash=%d"},
dece6358 25582+ {Opt_rdhash_def, "rdhash=def"},
1facf9fc 25583+
25584+ {Opt_wbr_create, "create=%s"},
25585+ {Opt_wbr_create, "create_policy=%s"},
25586+ {Opt_wbr_copyup, "cpup=%s"},
25587+ {Opt_wbr_copyup, "copyup=%s"},
25588+ {Opt_wbr_copyup, "copyup_policy=%s"},
25589+
c1595e42
JR
25590+ /* generic VFS flag */
25591+#ifdef CONFIG_FS_POSIX_ACL
25592+ {Opt_acl, "acl"},
25593+ {Opt_noacl, "noacl"},
25594+#else
8b6a4947 25595+ {Opt_ignore, "acl"},
c1595e42
JR
25596+ {Opt_ignore_silent, "noacl"},
25597+#endif
25598+
1facf9fc 25599+ /* internal use for the scripts */
25600+ {Opt_ignore_silent, "si=%s"},
25601+
25602+ {Opt_br, "dirs=%s"},
25603+ {Opt_ignore, "debug=%d"},
25604+ {Opt_ignore, "delete=whiteout"},
25605+ {Opt_ignore, "delete=all"},
25606+ {Opt_ignore, "imap=%s"},
25607+
1308ab2a 25608+ /* temporary workaround, due to old mount(8)? */
25609+ {Opt_ignore_silent, "relatime"},
25610+
1facf9fc 25611+ {Opt_err, NULL}
25612+};
25613+
25614+/* ---------------------------------------------------------------------- */
25615+
076b876e 25616+static const char *au_parser_pattern(int val, match_table_t tbl)
1facf9fc 25617+{
076b876e
AM
25618+ struct match_token *p;
25619+
25620+ p = tbl;
25621+ while (p->pattern) {
25622+ if (p->token == val)
25623+ return p->pattern;
25624+ p++;
1facf9fc 25625+ }
25626+ BUG();
25627+ return "??";
25628+}
25629+
076b876e
AM
25630+static const char *au_optstr(int *val, match_table_t tbl)
25631+{
25632+ struct match_token *p;
25633+ int v;
25634+
25635+ v = *val;
2000de60
JR
25636+ if (!v)
25637+ goto out;
076b876e 25638+ p = tbl;
2000de60
JR
25639+ while (p->pattern) {
25640+ if (p->token
25641+ && (v & p->token) == p->token) {
076b876e
AM
25642+ *val &= ~p->token;
25643+ return p->pattern;
25644+ }
25645+ p++;
25646+ }
2000de60
JR
25647+
25648+out:
076b876e
AM
25649+ return NULL;
25650+}
25651+
1facf9fc 25652+/* ---------------------------------------------------------------------- */
25653+
1e00d052 25654+static match_table_t brperm = {
1facf9fc 25655+ {AuBrPerm_RO, AUFS_BRPERM_RO},
25656+ {AuBrPerm_RR, AUFS_BRPERM_RR},
25657+ {AuBrPerm_RW, AUFS_BRPERM_RW},
1e00d052
AM
25658+ {0, NULL}
25659+};
1facf9fc 25660+
86dc4139 25661+static match_table_t brattr = {
076b876e
AM
25662+ /* general */
25663+ {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
25664+ {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
c1595e42 25665+ /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
86dc4139 25666+ {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
2000de60 25667+#ifdef CONFIG_AUFS_FHSM
076b876e 25668+ {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
2000de60
JR
25669+#endif
25670+#ifdef CONFIG_AUFS_XATTR
c1595e42
JR
25671+ {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
25672+ {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
25673+ {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
25674+ {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
25675+ {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
25676+ {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
2000de60 25677+#endif
076b876e
AM
25678+
25679+ /* ro/rr branch */
1e00d052 25680+ {AuBrRAttr_WH, AUFS_BRRATTR_WH},
076b876e
AM
25681+
25682+ /* rw branch */
25683+ {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
1e00d052 25684+ {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
076b876e 25685+
1e00d052 25686+ {0, NULL}
1facf9fc 25687+};
25688+
1e00d052
AM
25689+static int br_attr_val(char *str, match_table_t table, substring_t args[])
25690+{
25691+ int attr, v;
25692+ char *p;
25693+
25694+ attr = 0;
25695+ do {
25696+ p = strchr(str, '+');
25697+ if (p)
25698+ *p = 0;
25699+ v = match_token(str, table, args);
076b876e
AM
25700+ if (v) {
25701+ if (v & AuBrAttr_CMOO_Mask)
25702+ attr &= ~AuBrAttr_CMOO_Mask;
1e00d052 25703+ attr |= v;
076b876e 25704+ } else {
1e00d052
AM
25705+ if (p)
25706+ *p = '+';
0c3ec466 25707+ pr_warn("ignored branch attribute %s\n", str);
1e00d052
AM
25708+ break;
25709+ }
25710+ if (p)
25711+ str = p + 1;
25712+ } while (p);
25713+
25714+ return attr;
25715+}
25716+
076b876e
AM
25717+static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
25718+{
25719+ int sz;
25720+ const char *p;
25721+ char *q;
25722+
076b876e
AM
25723+ q = str->a;
25724+ *q = 0;
25725+ p = au_optstr(&perm, brattr);
25726+ if (p) {
25727+ sz = strlen(p);
25728+ memcpy(q, p, sz + 1);
25729+ q += sz;
25730+ } else
25731+ goto out;
25732+
25733+ do {
25734+ p = au_optstr(&perm, brattr);
25735+ if (p) {
25736+ *q++ = '+';
25737+ sz = strlen(p);
25738+ memcpy(q, p, sz + 1);
25739+ q += sz;
25740+ }
25741+ } while (p);
25742+
25743+out:
c1595e42 25744+ return q - str->a;
076b876e
AM
25745+}
25746+
4a4d8108 25747+static int noinline_for_stack br_perm_val(char *perm)
1facf9fc 25748+{
076b876e
AM
25749+ int val, bad, sz;
25750+ char *p;
1facf9fc 25751+ substring_t args[MAX_OPT_ARGS];
076b876e 25752+ au_br_perm_str_t attr;
1facf9fc 25753+
1e00d052
AM
25754+ p = strchr(perm, '+');
25755+ if (p)
25756+ *p = 0;
25757+ val = match_token(perm, brperm, args);
25758+ if (!val) {
25759+ if (p)
25760+ *p = '+';
0c3ec466 25761+ pr_warn("ignored branch permission %s\n", perm);
1e00d052
AM
25762+ val = AuBrPerm_RO;
25763+ goto out;
25764+ }
25765+ if (!p)
25766+ goto out;
25767+
076b876e
AM
25768+ val |= br_attr_val(p + 1, brattr, args);
25769+
25770+ bad = 0;
86dc4139 25771+ switch (val & AuBrPerm_Mask) {
1e00d052
AM
25772+ case AuBrPerm_RO:
25773+ case AuBrPerm_RR:
076b876e
AM
25774+ bad = val & AuBrWAttr_Mask;
25775+ val &= ~AuBrWAttr_Mask;
1e00d052
AM
25776+ break;
25777+ case AuBrPerm_RW:
076b876e
AM
25778+ bad = val & AuBrRAttr_Mask;
25779+ val &= ~AuBrRAttr_Mask;
1e00d052
AM
25780+ break;
25781+ }
c1595e42
JR
25782+
25783+ /*
25784+ * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
25785+ * does not treat it as an error, just warning.
25786+ * this is a tiny guard for the user operation.
25787+ */
25788+ if (val & AuBrAttr_UNPIN) {
25789+ bad |= AuBrAttr_UNPIN;
25790+ val &= ~AuBrAttr_UNPIN;
25791+ }
25792+
076b876e
AM
25793+ if (unlikely(bad)) {
25794+ sz = au_do_optstr_br_attr(&attr, bad);
25795+ AuDebugOn(!sz);
25796+ pr_warn("ignored branch attribute %s\n", attr.a);
25797+ }
1e00d052
AM
25798+
25799+out:
1facf9fc 25800+ return val;
25801+}
25802+
076b876e 25803+void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
1facf9fc 25804+{
076b876e
AM
25805+ au_br_perm_str_t attr;
25806+ const char *p;
25807+ char *q;
1e00d052
AM
25808+ int sz;
25809+
076b876e
AM
25810+ q = str->a;
25811+ p = au_optstr(&perm, brperm);
25812+ AuDebugOn(!p || !*p);
25813+ sz = strlen(p);
25814+ memcpy(q, p, sz + 1);
25815+ q += sz;
1e00d052 25816+
076b876e
AM
25817+ sz = au_do_optstr_br_attr(&attr, perm);
25818+ if (sz) {
25819+ *q++ = '+';
25820+ memcpy(q, attr.a, sz + 1);
1e00d052
AM
25821+ }
25822+
076b876e 25823+ AuDebugOn(strlen(str->a) >= sizeof(str->a));
1facf9fc 25824+}
25825+
25826+/* ---------------------------------------------------------------------- */
25827+
25828+static match_table_t udbalevel = {
25829+ {AuOpt_UDBA_REVAL, "reval"},
25830+ {AuOpt_UDBA_NONE, "none"},
4a4d8108
AM
25831+#ifdef CONFIG_AUFS_HNOTIFY
25832+ {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
25833+#ifdef CONFIG_AUFS_HFSNOTIFY
25834+ {AuOpt_UDBA_HNOTIFY, "fsnotify"},
4a4d8108 25835+#endif
1facf9fc 25836+#endif
25837+ {-1, NULL}
25838+};
25839+
4a4d8108 25840+static int noinline_for_stack udba_val(char *str)
1facf9fc 25841+{
25842+ substring_t args[MAX_OPT_ARGS];
25843+
7f207e10 25844+ return match_token(str, udbalevel, args);
1facf9fc 25845+}
25846+
25847+const char *au_optstr_udba(int udba)
25848+{
076b876e 25849+ return au_parser_pattern(udba, udbalevel);
1facf9fc 25850+}
25851+
25852+/* ---------------------------------------------------------------------- */
25853+
25854+static match_table_t au_wbr_create_policy = {
25855+ {AuWbrCreate_TDP, "tdp"},
25856+ {AuWbrCreate_TDP, "top-down-parent"},
25857+ {AuWbrCreate_RR, "rr"},
25858+ {AuWbrCreate_RR, "round-robin"},
25859+ {AuWbrCreate_MFS, "mfs"},
25860+ {AuWbrCreate_MFS, "most-free-space"},
25861+ {AuWbrCreate_MFSV, "mfs:%d"},
25862+ {AuWbrCreate_MFSV, "most-free-space:%d"},
25863+
f2c43d5f
AM
25864+ /* top-down regardless the parent, and then mfs */
25865+ {AuWbrCreate_TDMFS, "tdmfs:%d"},
25866+ {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
25867+
1facf9fc 25868+ {AuWbrCreate_MFSRR, "mfsrr:%d"},
25869+ {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
25870+ {AuWbrCreate_PMFS, "pmfs"},
25871+ {AuWbrCreate_PMFSV, "pmfs:%d"},
392086de
AM
25872+ {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
25873+ {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
1facf9fc 25874+
25875+ {-1, NULL}
25876+};
25877+
1facf9fc 25878+static int au_wbr_mfs_wmark(substring_t *arg, char *str,
25879+ struct au_opt_wbr_create *create)
25880+{
25881+ int err;
25882+ unsigned long long ull;
25883+
25884+ err = 0;
a2654f78 25885+ if (!match_u64(arg, &ull))
1facf9fc 25886+ create->mfsrr_watermark = ull;
25887+ else {
4a4d8108 25888+ pr_err("bad integer in %s\n", str);
1facf9fc 25889+ err = -EINVAL;
25890+ }
25891+
25892+ return err;
25893+}
25894+
25895+static int au_wbr_mfs_sec(substring_t *arg, char *str,
25896+ struct au_opt_wbr_create *create)
25897+{
25898+ int n, err;
25899+
25900+ err = 0;
027c5e7a 25901+ if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
1facf9fc 25902+ create->mfs_second = n;
25903+ else {
4a4d8108 25904+ pr_err("bad integer in %s\n", str);
1facf9fc 25905+ err = -EINVAL;
25906+ }
25907+
25908+ return err;
25909+}
25910+
4a4d8108
AM
25911+static int noinline_for_stack
25912+au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
1facf9fc 25913+{
25914+ int err, e;
25915+ substring_t args[MAX_OPT_ARGS];
25916+
25917+ err = match_token(str, au_wbr_create_policy, args);
25918+ create->wbr_create = err;
25919+ switch (err) {
25920+ case AuWbrCreate_MFSRRV:
f2c43d5f 25921+ case AuWbrCreate_TDMFSV:
392086de 25922+ case AuWbrCreate_PMFSRRV:
1facf9fc 25923+ e = au_wbr_mfs_wmark(&args[0], str, create);
25924+ if (!e)
25925+ e = au_wbr_mfs_sec(&args[1], str, create);
25926+ if (unlikely(e))
25927+ err = e;
25928+ break;
25929+ case AuWbrCreate_MFSRR:
f2c43d5f 25930+ case AuWbrCreate_TDMFS:
392086de 25931+ case AuWbrCreate_PMFSRR:
1facf9fc 25932+ e = au_wbr_mfs_wmark(&args[0], str, create);
25933+ if (unlikely(e)) {
25934+ err = e;
25935+ break;
25936+ }
25937+ /*FALLTHROUGH*/
25938+ case AuWbrCreate_MFS:
25939+ case AuWbrCreate_PMFS:
027c5e7a 25940+ create->mfs_second = AUFS_MFS_DEF_SEC;
1facf9fc 25941+ break;
25942+ case AuWbrCreate_MFSV:
25943+ case AuWbrCreate_PMFSV:
25944+ e = au_wbr_mfs_sec(&args[0], str, create);
25945+ if (unlikely(e))
25946+ err = e;
25947+ break;
25948+ }
25949+
25950+ return err;
25951+}
25952+
25953+const char *au_optstr_wbr_create(int wbr_create)
25954+{
076b876e 25955+ return au_parser_pattern(wbr_create, au_wbr_create_policy);
1facf9fc 25956+}
25957+
25958+static match_table_t au_wbr_copyup_policy = {
25959+ {AuWbrCopyup_TDP, "tdp"},
25960+ {AuWbrCopyup_TDP, "top-down-parent"},
25961+ {AuWbrCopyup_BUP, "bup"},
25962+ {AuWbrCopyup_BUP, "bottom-up-parent"},
25963+ {AuWbrCopyup_BU, "bu"},
25964+ {AuWbrCopyup_BU, "bottom-up"},
25965+ {-1, NULL}
25966+};
25967+
4a4d8108 25968+static int noinline_for_stack au_wbr_copyup_val(char *str)
1facf9fc 25969+{
25970+ substring_t args[MAX_OPT_ARGS];
25971+
25972+ return match_token(str, au_wbr_copyup_policy, args);
25973+}
25974+
25975+const char *au_optstr_wbr_copyup(int wbr_copyup)
25976+{
076b876e 25977+ return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
1facf9fc 25978+}
25979+
25980+/* ---------------------------------------------------------------------- */
25981+
25982+static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
25983+
25984+static void dump_opts(struct au_opts *opts)
25985+{
25986+#ifdef CONFIG_AUFS_DEBUG
25987+ /* reduce stack space */
25988+ union {
25989+ struct au_opt_add *add;
25990+ struct au_opt_del *del;
25991+ struct au_opt_mod *mod;
25992+ struct au_opt_xino *xino;
25993+ struct au_opt_xino_itrunc *xino_itrunc;
25994+ struct au_opt_wbr_create *create;
25995+ } u;
25996+ struct au_opt *opt;
25997+
25998+ opt = opts->opt;
25999+ while (opt->type != Opt_tail) {
26000+ switch (opt->type) {
26001+ case Opt_add:
26002+ u.add = &opt->add;
26003+ AuDbg("add {b%d, %s, 0x%x, %p}\n",
26004+ u.add->bindex, u.add->pathname, u.add->perm,
26005+ u.add->path.dentry);
26006+ break;
26007+ case Opt_del:
26008+ case Opt_idel:
26009+ u.del = &opt->del;
26010+ AuDbg("del {%s, %p}\n",
26011+ u.del->pathname, u.del->h_path.dentry);
26012+ break;
26013+ case Opt_mod:
26014+ case Opt_imod:
26015+ u.mod = &opt->mod;
26016+ AuDbg("mod {%s, 0x%x, %p}\n",
26017+ u.mod->path, u.mod->perm, u.mod->h_root);
26018+ break;
26019+ case Opt_append:
26020+ u.add = &opt->add;
26021+ AuDbg("append {b%d, %s, 0x%x, %p}\n",
26022+ u.add->bindex, u.add->pathname, u.add->perm,
26023+ u.add->path.dentry);
26024+ break;
26025+ case Opt_prepend:
26026+ u.add = &opt->add;
26027+ AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
26028+ u.add->bindex, u.add->pathname, u.add->perm,
26029+ u.add->path.dentry);
26030+ break;
26031+ case Opt_dirwh:
26032+ AuDbg("dirwh %d\n", opt->dirwh);
26033+ break;
26034+ case Opt_rdcache:
26035+ AuDbg("rdcache %d\n", opt->rdcache);
26036+ break;
26037+ case Opt_rdblk:
26038+ AuDbg("rdblk %u\n", opt->rdblk);
26039+ break;
dece6358
AM
26040+ case Opt_rdblk_def:
26041+ AuDbg("rdblk_def\n");
26042+ break;
1facf9fc 26043+ case Opt_rdhash:
26044+ AuDbg("rdhash %u\n", opt->rdhash);
26045+ break;
dece6358
AM
26046+ case Opt_rdhash_def:
26047+ AuDbg("rdhash_def\n");
26048+ break;
1facf9fc 26049+ case Opt_xino:
26050+ u.xino = &opt->xino;
523b37e3 26051+ AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
1facf9fc 26052+ break;
26053+ case Opt_trunc_xino:
26054+ AuLabel(trunc_xino);
26055+ break;
26056+ case Opt_notrunc_xino:
26057+ AuLabel(notrunc_xino);
26058+ break;
26059+ case Opt_trunc_xino_path:
26060+ case Opt_itrunc_xino:
26061+ u.xino_itrunc = &opt->xino_itrunc;
26062+ AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
26063+ break;
1facf9fc 26064+ case Opt_noxino:
26065+ AuLabel(noxino);
26066+ break;
26067+ case Opt_trunc_xib:
26068+ AuLabel(trunc_xib);
26069+ break;
26070+ case Opt_notrunc_xib:
26071+ AuLabel(notrunc_xib);
26072+ break;
dece6358
AM
26073+ case Opt_shwh:
26074+ AuLabel(shwh);
26075+ break;
26076+ case Opt_noshwh:
26077+ AuLabel(noshwh);
26078+ break;
076b876e
AM
26079+ case Opt_dirperm1:
26080+ AuLabel(dirperm1);
26081+ break;
26082+ case Opt_nodirperm1:
26083+ AuLabel(nodirperm1);
26084+ break;
1facf9fc 26085+ case Opt_plink:
26086+ AuLabel(plink);
26087+ break;
26088+ case Opt_noplink:
26089+ AuLabel(noplink);
26090+ break;
26091+ case Opt_list_plink:
26092+ AuLabel(list_plink);
26093+ break;
26094+ case Opt_udba:
26095+ AuDbg("udba %d, %s\n",
26096+ opt->udba, au_optstr_udba(opt->udba));
26097+ break;
4a4d8108
AM
26098+ case Opt_dio:
26099+ AuLabel(dio);
26100+ break;
26101+ case Opt_nodio:
26102+ AuLabel(nodio);
26103+ break;
1facf9fc 26104+ case Opt_diropq_a:
26105+ AuLabel(diropq_a);
26106+ break;
26107+ case Opt_diropq_w:
26108+ AuLabel(diropq_w);
26109+ break;
26110+ case Opt_warn_perm:
26111+ AuLabel(warn_perm);
26112+ break;
26113+ case Opt_nowarn_perm:
26114+ AuLabel(nowarn_perm);
26115+ break;
1facf9fc 26116+ case Opt_verbose:
26117+ AuLabel(verbose);
26118+ break;
26119+ case Opt_noverbose:
26120+ AuLabel(noverbose);
26121+ break;
26122+ case Opt_sum:
26123+ AuLabel(sum);
26124+ break;
26125+ case Opt_nosum:
26126+ AuLabel(nosum);
26127+ break;
26128+ case Opt_wsum:
26129+ AuLabel(wsum);
26130+ break;
26131+ case Opt_wbr_create:
26132+ u.create = &opt->wbr_create;
26133+ AuDbg("create %d, %s\n", u.create->wbr_create,
26134+ au_optstr_wbr_create(u.create->wbr_create));
26135+ switch (u.create->wbr_create) {
26136+ case AuWbrCreate_MFSV:
26137+ case AuWbrCreate_PMFSV:
26138+ AuDbg("%d sec\n", u.create->mfs_second);
26139+ break;
26140+ case AuWbrCreate_MFSRR:
f2c43d5f 26141+ case AuWbrCreate_TDMFS:
1facf9fc 26142+ AuDbg("%llu watermark\n",
26143+ u.create->mfsrr_watermark);
26144+ break;
26145+ case AuWbrCreate_MFSRRV:
f2c43d5f 26146+ case AuWbrCreate_TDMFSV:
392086de 26147+ case AuWbrCreate_PMFSRRV:
1facf9fc 26148+ AuDbg("%llu watermark, %d sec\n",
26149+ u.create->mfsrr_watermark,
26150+ u.create->mfs_second);
26151+ break;
26152+ }
26153+ break;
26154+ case Opt_wbr_copyup:
26155+ AuDbg("copyup %d, %s\n", opt->wbr_copyup,
26156+ au_optstr_wbr_copyup(opt->wbr_copyup));
26157+ break;
076b876e
AM
26158+ case Opt_fhsm_sec:
26159+ AuDbg("fhsm_sec %u\n", opt->fhsm_second);
26160+ break;
8b6a4947
AM
26161+ case Opt_dirren:
26162+ AuLabel(dirren);
26163+ break;
26164+ case Opt_nodirren:
26165+ AuLabel(nodirren);
26166+ break;
c1595e42
JR
26167+ case Opt_acl:
26168+ AuLabel(acl);
26169+ break;
26170+ case Opt_noacl:
26171+ AuLabel(noacl);
26172+ break;
1facf9fc 26173+ default:
26174+ BUG();
26175+ }
26176+ opt++;
26177+ }
26178+#endif
26179+}
26180+
26181+void au_opts_free(struct au_opts *opts)
26182+{
26183+ struct au_opt *opt;
26184+
26185+ opt = opts->opt;
26186+ while (opt->type != Opt_tail) {
26187+ switch (opt->type) {
26188+ case Opt_add:
26189+ case Opt_append:
26190+ case Opt_prepend:
26191+ path_put(&opt->add.path);
26192+ break;
26193+ case Opt_del:
26194+ case Opt_idel:
26195+ path_put(&opt->del.h_path);
26196+ break;
26197+ case Opt_mod:
26198+ case Opt_imod:
26199+ dput(opt->mod.h_root);
26200+ break;
26201+ case Opt_xino:
26202+ fput(opt->xino.file);
26203+ break;
26204+ }
26205+ opt++;
26206+ }
26207+}
26208+
26209+static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
26210+ aufs_bindex_t bindex)
26211+{
26212+ int err;
26213+ struct au_opt_add *add = &opt->add;
26214+ char *p;
26215+
26216+ add->bindex = bindex;
1e00d052 26217+ add->perm = AuBrPerm_RO;
1facf9fc 26218+ add->pathname = opt_str;
26219+ p = strchr(opt_str, '=');
26220+ if (p) {
26221+ *p++ = 0;
26222+ if (*p)
26223+ add->perm = br_perm_val(p);
26224+ }
26225+
26226+ err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
26227+ if (!err) {
26228+ if (!p) {
26229+ add->perm = AuBrPerm_RO;
26230+ if (au_test_fs_rr(add->path.dentry->d_sb))
26231+ add->perm = AuBrPerm_RR;
2121bcd9 26232+ else if (!bindex && !(sb_flags & SB_RDONLY))
1facf9fc 26233+ add->perm = AuBrPerm_RW;
26234+ }
26235+ opt->type = Opt_add;
26236+ goto out;
26237+ }
4a4d8108 26238+ pr_err("lookup failed %s (%d)\n", add->pathname, err);
1facf9fc 26239+ err = -EINVAL;
26240+
4f0767ce 26241+out:
1facf9fc 26242+ return err;
26243+}
26244+
26245+static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
26246+{
26247+ int err;
26248+
26249+ del->pathname = args[0].from;
26250+ AuDbg("del path %s\n", del->pathname);
26251+
26252+ err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
26253+ if (unlikely(err))
4a4d8108 26254+ pr_err("lookup failed %s (%d)\n", del->pathname, err);
1facf9fc 26255+
26256+ return err;
26257+}
26258+
26259+#if 0 /* reserved for future use */
26260+static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
26261+ struct au_opt_del *del, substring_t args[])
26262+{
26263+ int err;
26264+ struct dentry *root;
26265+
26266+ err = -EINVAL;
26267+ root = sb->s_root;
26268+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26269+ if (bindex < 0 || au_sbbot(sb) < bindex) {
4a4d8108 26270+ pr_err("out of bounds, %d\n", bindex);
1facf9fc 26271+ goto out;
26272+ }
26273+
26274+ err = 0;
26275+ del->h_path.dentry = dget(au_h_dptr(root, bindex));
26276+ del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
26277+
4f0767ce 26278+out:
1facf9fc 26279+ aufs_read_unlock(root, !AuLock_IR);
26280+ return err;
26281+}
26282+#endif
26283+
4a4d8108
AM
26284+static int noinline_for_stack
26285+au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
1facf9fc 26286+{
26287+ int err;
26288+ struct path path;
26289+ char *p;
26290+
26291+ err = -EINVAL;
26292+ mod->path = args[0].from;
26293+ p = strchr(mod->path, '=');
26294+ if (unlikely(!p)) {
4a4d8108 26295+ pr_err("no permssion %s\n", args[0].from);
1facf9fc 26296+ goto out;
26297+ }
26298+
26299+ *p++ = 0;
26300+ err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
26301+ if (unlikely(err)) {
4a4d8108 26302+ pr_err("lookup failed %s (%d)\n", mod->path, err);
1facf9fc 26303+ goto out;
26304+ }
26305+
26306+ mod->perm = br_perm_val(p);
26307+ AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
26308+ mod->h_root = dget(path.dentry);
26309+ path_put(&path);
26310+
4f0767ce 26311+out:
1facf9fc 26312+ return err;
26313+}
26314+
26315+#if 0 /* reserved for future use */
26316+static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
26317+ struct au_opt_mod *mod, substring_t args[])
26318+{
26319+ int err;
26320+ struct dentry *root;
26321+
26322+ err = -EINVAL;
26323+ root = sb->s_root;
26324+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26325+ if (bindex < 0 || au_sbbot(sb) < bindex) {
4a4d8108 26326+ pr_err("out of bounds, %d\n", bindex);
1facf9fc 26327+ goto out;
26328+ }
26329+
26330+ err = 0;
26331+ mod->perm = br_perm_val(args[1].from);
26332+ AuDbg("mod path %s, perm 0x%x, %s\n",
26333+ mod->path, mod->perm, args[1].from);
26334+ mod->h_root = dget(au_h_dptr(root, bindex));
26335+
4f0767ce 26336+out:
1facf9fc 26337+ aufs_read_unlock(root, !AuLock_IR);
26338+ return err;
26339+}
26340+#endif
26341+
26342+static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
26343+ substring_t args[])
26344+{
26345+ int err;
26346+ struct file *file;
26347+
26348+ file = au_xino_create(sb, args[0].from, /*silent*/0);
26349+ err = PTR_ERR(file);
26350+ if (IS_ERR(file))
26351+ goto out;
26352+
26353+ err = -EINVAL;
2000de60 26354+ if (unlikely(file->f_path.dentry->d_sb == sb)) {
1facf9fc 26355+ fput(file);
4a4d8108 26356+ pr_err("%s must be outside\n", args[0].from);
1facf9fc 26357+ goto out;
26358+ }
26359+
26360+ err = 0;
26361+ xino->file = file;
26362+ xino->path = args[0].from;
26363+
4f0767ce 26364+out:
1facf9fc 26365+ return err;
26366+}
26367+
4a4d8108
AM
26368+static int noinline_for_stack
26369+au_opts_parse_xino_itrunc_path(struct super_block *sb,
26370+ struct au_opt_xino_itrunc *xino_itrunc,
26371+ substring_t args[])
1facf9fc 26372+{
26373+ int err;
5afbbe0d 26374+ aufs_bindex_t bbot, bindex;
1facf9fc 26375+ struct path path;
26376+ struct dentry *root;
26377+
26378+ err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
26379+ if (unlikely(err)) {
4a4d8108 26380+ pr_err("lookup failed %s (%d)\n", args[0].from, err);
1facf9fc 26381+ goto out;
26382+ }
26383+
26384+ xino_itrunc->bindex = -1;
26385+ root = sb->s_root;
26386+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d
AM
26387+ bbot = au_sbbot(sb);
26388+ for (bindex = 0; bindex <= bbot; bindex++) {
1facf9fc 26389+ if (au_h_dptr(root, bindex) == path.dentry) {
26390+ xino_itrunc->bindex = bindex;
26391+ break;
26392+ }
26393+ }
26394+ aufs_read_unlock(root, !AuLock_IR);
26395+ path_put(&path);
26396+
26397+ if (unlikely(xino_itrunc->bindex < 0)) {
4a4d8108 26398+ pr_err("no such branch %s\n", args[0].from);
1facf9fc 26399+ err = -EINVAL;
26400+ }
26401+
4f0767ce 26402+out:
1facf9fc 26403+ return err;
26404+}
26405+
26406+/* called without aufs lock */
26407+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
26408+{
26409+ int err, n, token;
26410+ aufs_bindex_t bindex;
26411+ unsigned char skipped;
26412+ struct dentry *root;
26413+ struct au_opt *opt, *opt_tail;
26414+ char *opt_str;
26415+ /* reduce the stack space */
26416+ union {
26417+ struct au_opt_xino_itrunc *xino_itrunc;
26418+ struct au_opt_wbr_create *create;
26419+ } u;
26420+ struct {
26421+ substring_t args[MAX_OPT_ARGS];
26422+ } *a;
26423+
26424+ err = -ENOMEM;
26425+ a = kmalloc(sizeof(*a), GFP_NOFS);
26426+ if (unlikely(!a))
26427+ goto out;
26428+
26429+ root = sb->s_root;
26430+ err = 0;
26431+ bindex = 0;
26432+ opt = opts->opt;
26433+ opt_tail = opt + opts->max_opt - 1;
26434+ opt->type = Opt_tail;
26435+ while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
26436+ err = -EINVAL;
26437+ skipped = 0;
26438+ token = match_token(opt_str, options, a->args);
26439+ switch (token) {
26440+ case Opt_br:
26441+ err = 0;
26442+ while (!err && (opt_str = strsep(&a->args[0].from, ":"))
26443+ && *opt_str) {
26444+ err = opt_add(opt, opt_str, opts->sb_flags,
26445+ bindex++);
26446+ if (unlikely(!err && ++opt > opt_tail)) {
26447+ err = -E2BIG;
26448+ break;
26449+ }
26450+ opt->type = Opt_tail;
26451+ skipped = 1;
26452+ }
26453+ break;
26454+ case Opt_add:
26455+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26456+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26457+ break;
26458+ }
26459+ bindex = n;
26460+ err = opt_add(opt, a->args[1].from, opts->sb_flags,
26461+ bindex);
26462+ if (!err)
26463+ opt->type = token;
26464+ break;
26465+ case Opt_append:
26466+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26467+ /*dummy bindex*/1);
26468+ if (!err)
26469+ opt->type = token;
26470+ break;
26471+ case Opt_prepend:
26472+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26473+ /*bindex*/0);
26474+ if (!err)
26475+ opt->type = token;
26476+ break;
26477+ case Opt_del:
26478+ err = au_opts_parse_del(&opt->del, a->args);
26479+ if (!err)
26480+ opt->type = token;
26481+ break;
26482+#if 0 /* reserved for future use */
26483+ case Opt_idel:
26484+ del->pathname = "(indexed)";
26485+ if (unlikely(match_int(&args[0], &n))) {
4a4d8108 26486+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26487+ break;
26488+ }
26489+ err = au_opts_parse_idel(sb, n, &opt->del, a->args);
26490+ if (!err)
26491+ opt->type = token;
26492+ break;
26493+#endif
26494+ case Opt_mod:
26495+ err = au_opts_parse_mod(&opt->mod, a->args);
26496+ if (!err)
26497+ opt->type = token;
26498+ break;
26499+#ifdef IMOD /* reserved for future use */
26500+ case Opt_imod:
26501+ u.mod->path = "(indexed)";
26502+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26503+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26504+ break;
26505+ }
26506+ err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
26507+ if (!err)
26508+ opt->type = token;
26509+ break;
26510+#endif
26511+ case Opt_xino:
26512+ err = au_opts_parse_xino(sb, &opt->xino, a->args);
26513+ if (!err)
26514+ opt->type = token;
26515+ break;
26516+
26517+ case Opt_trunc_xino_path:
26518+ err = au_opts_parse_xino_itrunc_path
26519+ (sb, &opt->xino_itrunc, a->args);
26520+ if (!err)
26521+ opt->type = token;
26522+ break;
26523+
26524+ case Opt_itrunc_xino:
26525+ u.xino_itrunc = &opt->xino_itrunc;
26526+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26527+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26528+ break;
26529+ }
26530+ u.xino_itrunc->bindex = n;
26531+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26532+ if (n < 0 || au_sbbot(sb) < n) {
4a4d8108 26533+ pr_err("out of bounds, %d\n", n);
1facf9fc 26534+ aufs_read_unlock(root, !AuLock_IR);
26535+ break;
26536+ }
26537+ aufs_read_unlock(root, !AuLock_IR);
26538+ err = 0;
26539+ opt->type = token;
26540+ break;
26541+
26542+ case Opt_dirwh:
26543+ if (unlikely(match_int(&a->args[0], &opt->dirwh)))
26544+ break;
26545+ err = 0;
26546+ opt->type = token;
26547+ break;
26548+
26549+ case Opt_rdcache:
027c5e7a
AM
26550+ if (unlikely(match_int(&a->args[0], &n))) {
26551+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26552+ break;
027c5e7a
AM
26553+ }
26554+ if (unlikely(n > AUFS_RDCACHE_MAX)) {
26555+ pr_err("rdcache must be smaller than %d\n",
26556+ AUFS_RDCACHE_MAX);
26557+ break;
26558+ }
26559+ opt->rdcache = n;
1facf9fc 26560+ err = 0;
26561+ opt->type = token;
26562+ break;
26563+ case Opt_rdblk:
26564+ if (unlikely(match_int(&a->args[0], &n)
1308ab2a 26565+ || n < 0
1facf9fc 26566+ || n > KMALLOC_MAX_SIZE)) {
4a4d8108 26567+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26568+ break;
26569+ }
1308ab2a 26570+ if (unlikely(n && n < NAME_MAX)) {
4a4d8108
AM
26571+ pr_err("rdblk must be larger than %d\n",
26572+ NAME_MAX);
1facf9fc 26573+ break;
26574+ }
26575+ opt->rdblk = n;
26576+ err = 0;
26577+ opt->type = token;
26578+ break;
26579+ case Opt_rdhash:
26580+ if (unlikely(match_int(&a->args[0], &n)
1308ab2a 26581+ || n < 0
1facf9fc 26582+ || n * sizeof(struct hlist_head)
26583+ > KMALLOC_MAX_SIZE)) {
4a4d8108 26584+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26585+ break;
26586+ }
26587+ opt->rdhash = n;
26588+ err = 0;
26589+ opt->type = token;
26590+ break;
26591+
26592+ case Opt_trunc_xino:
26593+ case Opt_notrunc_xino:
26594+ case Opt_noxino:
26595+ case Opt_trunc_xib:
26596+ case Opt_notrunc_xib:
dece6358
AM
26597+ case Opt_shwh:
26598+ case Opt_noshwh:
076b876e
AM
26599+ case Opt_dirperm1:
26600+ case Opt_nodirperm1:
1facf9fc 26601+ case Opt_plink:
26602+ case Opt_noplink:
26603+ case Opt_list_plink:
4a4d8108
AM
26604+ case Opt_dio:
26605+ case Opt_nodio:
1facf9fc 26606+ case Opt_diropq_a:
26607+ case Opt_diropq_w:
26608+ case Opt_warn_perm:
26609+ case Opt_nowarn_perm:
1facf9fc 26610+ case Opt_verbose:
26611+ case Opt_noverbose:
26612+ case Opt_sum:
26613+ case Opt_nosum:
26614+ case Opt_wsum:
dece6358
AM
26615+ case Opt_rdblk_def:
26616+ case Opt_rdhash_def:
8b6a4947
AM
26617+ case Opt_dirren:
26618+ case Opt_nodirren:
c1595e42
JR
26619+ case Opt_acl:
26620+ case Opt_noacl:
1facf9fc 26621+ err = 0;
26622+ opt->type = token;
26623+ break;
26624+
26625+ case Opt_udba:
26626+ opt->udba = udba_val(a->args[0].from);
26627+ if (opt->udba >= 0) {
26628+ err = 0;
26629+ opt->type = token;
26630+ } else
4a4d8108 26631+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26632+ break;
26633+
26634+ case Opt_wbr_create:
26635+ u.create = &opt->wbr_create;
26636+ u.create->wbr_create
26637+ = au_wbr_create_val(a->args[0].from, u.create);
26638+ if (u.create->wbr_create >= 0) {
26639+ err = 0;
26640+ opt->type = token;
26641+ } else
4a4d8108 26642+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26643+ break;
26644+ case Opt_wbr_copyup:
26645+ opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
26646+ if (opt->wbr_copyup >= 0) {
26647+ err = 0;
26648+ opt->type = token;
26649+ } else
4a4d8108 26650+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26651+ break;
26652+
076b876e
AM
26653+ case Opt_fhsm_sec:
26654+ if (unlikely(match_int(&a->args[0], &n)
26655+ || n < 0)) {
26656+ pr_err("bad integer in %s\n", opt_str);
26657+ break;
26658+ }
26659+ if (sysaufs_brs) {
26660+ opt->fhsm_second = n;
26661+ opt->type = token;
26662+ } else
26663+ pr_warn("ignored %s\n", opt_str);
26664+ err = 0;
26665+ break;
26666+
1facf9fc 26667+ case Opt_ignore:
0c3ec466 26668+ pr_warn("ignored %s\n", opt_str);
1facf9fc 26669+ /*FALLTHROUGH*/
26670+ case Opt_ignore_silent:
26671+ skipped = 1;
26672+ err = 0;
26673+ break;
26674+ case Opt_err:
4a4d8108 26675+ pr_err("unknown option %s\n", opt_str);
1facf9fc 26676+ break;
26677+ }
26678+
26679+ if (!err && !skipped) {
26680+ if (unlikely(++opt > opt_tail)) {
26681+ err = -E2BIG;
26682+ opt--;
26683+ opt->type = Opt_tail;
26684+ break;
26685+ }
26686+ opt->type = Opt_tail;
26687+ }
26688+ }
26689+
1c60b727 26690+ kfree(a);
1facf9fc 26691+ dump_opts(opts);
26692+ if (unlikely(err))
26693+ au_opts_free(opts);
26694+
4f0767ce 26695+out:
1facf9fc 26696+ return err;
26697+}
26698+
26699+static int au_opt_wbr_create(struct super_block *sb,
26700+ struct au_opt_wbr_create *create)
26701+{
26702+ int err;
26703+ struct au_sbinfo *sbinfo;
26704+
dece6358
AM
26705+ SiMustWriteLock(sb);
26706+
1facf9fc 26707+ err = 1; /* handled */
26708+ sbinfo = au_sbi(sb);
26709+ if (sbinfo->si_wbr_create_ops->fin) {
26710+ err = sbinfo->si_wbr_create_ops->fin(sb);
26711+ if (!err)
26712+ err = 1;
26713+ }
26714+
26715+ sbinfo->si_wbr_create = create->wbr_create;
26716+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
26717+ switch (create->wbr_create) {
26718+ case AuWbrCreate_MFSRRV:
26719+ case AuWbrCreate_MFSRR:
f2c43d5f
AM
26720+ case AuWbrCreate_TDMFS:
26721+ case AuWbrCreate_TDMFSV:
392086de
AM
26722+ case AuWbrCreate_PMFSRR:
26723+ case AuWbrCreate_PMFSRRV:
1facf9fc 26724+ sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
26725+ /*FALLTHROUGH*/
26726+ case AuWbrCreate_MFS:
26727+ case AuWbrCreate_MFSV:
26728+ case AuWbrCreate_PMFS:
26729+ case AuWbrCreate_PMFSV:
e49829fe
JR
26730+ sbinfo->si_wbr_mfs.mfs_expire
26731+ = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
1facf9fc 26732+ break;
26733+ }
26734+
26735+ if (sbinfo->si_wbr_create_ops->init)
26736+ sbinfo->si_wbr_create_ops->init(sb); /* ignore */
26737+
26738+ return err;
26739+}
26740+
26741+/*
26742+ * returns,
26743+ * plus: processed without an error
26744+ * zero: unprocessed
26745+ */
26746+static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
26747+ struct au_opts *opts)
26748+{
26749+ int err;
26750+ struct au_sbinfo *sbinfo;
26751+
dece6358
AM
26752+ SiMustWriteLock(sb);
26753+
1facf9fc 26754+ err = 1; /* handled */
26755+ sbinfo = au_sbi(sb);
26756+ switch (opt->type) {
26757+ case Opt_udba:
26758+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
26759+ sbinfo->si_mntflags |= opt->udba;
26760+ opts->given_udba |= opt->udba;
26761+ break;
26762+
26763+ case Opt_plink:
26764+ au_opt_set(sbinfo->si_mntflags, PLINK);
26765+ break;
26766+ case Opt_noplink:
26767+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
e49829fe 26768+ au_plink_put(sb, /*verbose*/1);
1facf9fc 26769+ au_opt_clr(sbinfo->si_mntflags, PLINK);
26770+ break;
26771+ case Opt_list_plink:
26772+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
26773+ au_plink_list(sb);
26774+ break;
26775+
4a4d8108
AM
26776+ case Opt_dio:
26777+ au_opt_set(sbinfo->si_mntflags, DIO);
26778+ au_fset_opts(opts->flags, REFRESH_DYAOP);
26779+ break;
26780+ case Opt_nodio:
26781+ au_opt_clr(sbinfo->si_mntflags, DIO);
26782+ au_fset_opts(opts->flags, REFRESH_DYAOP);
26783+ break;
26784+
076b876e
AM
26785+ case Opt_fhsm_sec:
26786+ au_fhsm_set(sbinfo, opt->fhsm_second);
26787+ break;
26788+
1facf9fc 26789+ case Opt_diropq_a:
26790+ au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26791+ break;
26792+ case Opt_diropq_w:
26793+ au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26794+ break;
26795+
26796+ case Opt_warn_perm:
26797+ au_opt_set(sbinfo->si_mntflags, WARN_PERM);
26798+ break;
26799+ case Opt_nowarn_perm:
26800+ au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
26801+ break;
26802+
1facf9fc 26803+ case Opt_verbose:
26804+ au_opt_set(sbinfo->si_mntflags, VERBOSE);
26805+ break;
26806+ case Opt_noverbose:
26807+ au_opt_clr(sbinfo->si_mntflags, VERBOSE);
26808+ break;
26809+
26810+ case Opt_sum:
26811+ au_opt_set(sbinfo->si_mntflags, SUM);
26812+ break;
26813+ case Opt_wsum:
26814+ au_opt_clr(sbinfo->si_mntflags, SUM);
26815+ au_opt_set(sbinfo->si_mntflags, SUM_W);
26816+ case Opt_nosum:
26817+ au_opt_clr(sbinfo->si_mntflags, SUM);
26818+ au_opt_clr(sbinfo->si_mntflags, SUM_W);
26819+ break;
26820+
26821+ case Opt_wbr_create:
26822+ err = au_opt_wbr_create(sb, &opt->wbr_create);
26823+ break;
26824+ case Opt_wbr_copyup:
26825+ sbinfo->si_wbr_copyup = opt->wbr_copyup;
26826+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
26827+ break;
26828+
26829+ case Opt_dirwh:
26830+ sbinfo->si_dirwh = opt->dirwh;
26831+ break;
26832+
26833+ case Opt_rdcache:
e49829fe
JR
26834+ sbinfo->si_rdcache
26835+ = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
1facf9fc 26836+ break;
26837+ case Opt_rdblk:
26838+ sbinfo->si_rdblk = opt->rdblk;
26839+ break;
dece6358
AM
26840+ case Opt_rdblk_def:
26841+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
26842+ break;
1facf9fc 26843+ case Opt_rdhash:
26844+ sbinfo->si_rdhash = opt->rdhash;
26845+ break;
dece6358
AM
26846+ case Opt_rdhash_def:
26847+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
26848+ break;
26849+
26850+ case Opt_shwh:
26851+ au_opt_set(sbinfo->si_mntflags, SHWH);
26852+ break;
26853+ case Opt_noshwh:
26854+ au_opt_clr(sbinfo->si_mntflags, SHWH);
26855+ break;
1facf9fc 26856+
076b876e
AM
26857+ case Opt_dirperm1:
26858+ au_opt_set(sbinfo->si_mntflags, DIRPERM1);
26859+ break;
26860+ case Opt_nodirperm1:
26861+ au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
26862+ break;
26863+
1facf9fc 26864+ case Opt_trunc_xino:
26865+ au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
26866+ break;
26867+ case Opt_notrunc_xino:
26868+ au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
26869+ break;
26870+
26871+ case Opt_trunc_xino_path:
26872+ case Opt_itrunc_xino:
26873+ err = au_xino_trunc(sb, opt->xino_itrunc.bindex);
26874+ if (!err)
26875+ err = 1;
26876+ break;
26877+
26878+ case Opt_trunc_xib:
26879+ au_fset_opts(opts->flags, TRUNC_XIB);
26880+ break;
26881+ case Opt_notrunc_xib:
26882+ au_fclr_opts(opts->flags, TRUNC_XIB);
26883+ break;
26884+
8b6a4947
AM
26885+ case Opt_dirren:
26886+ err = 1;
26887+ if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
26888+ err = au_dr_opt_set(sb);
26889+ if (!err)
26890+ err = 1;
26891+ }
26892+ if (err == 1)
26893+ au_opt_set(sbinfo->si_mntflags, DIRREN);
26894+ break;
26895+ case Opt_nodirren:
26896+ err = 1;
26897+ if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
26898+ err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
26899+ DR_FLUSHED));
26900+ if (!err)
26901+ err = 1;
26902+ }
26903+ if (err == 1)
26904+ au_opt_clr(sbinfo->si_mntflags, DIRREN);
26905+ break;
26906+
c1595e42 26907+ case Opt_acl:
2121bcd9 26908+ sb->s_flags |= SB_POSIXACL;
c1595e42
JR
26909+ break;
26910+ case Opt_noacl:
2121bcd9 26911+ sb->s_flags &= ~SB_POSIXACL;
c1595e42
JR
26912+ break;
26913+
1facf9fc 26914+ default:
26915+ err = 0;
26916+ break;
26917+ }
26918+
26919+ return err;
26920+}
26921+
26922+/*
26923+ * returns tri-state.
26924+ * plus: processed without an error
26925+ * zero: unprocessed
26926+ * minus: error
26927+ */
26928+static int au_opt_br(struct super_block *sb, struct au_opt *opt,
26929+ struct au_opts *opts)
26930+{
26931+ int err, do_refresh;
26932+
26933+ err = 0;
26934+ switch (opt->type) {
26935+ case Opt_append:
5afbbe0d 26936+ opt->add.bindex = au_sbbot(sb) + 1;
1facf9fc 26937+ if (opt->add.bindex < 0)
26938+ opt->add.bindex = 0;
26939+ goto add;
26940+ case Opt_prepend:
26941+ opt->add.bindex = 0;
f6b6e03d 26942+ add: /* indented label */
1facf9fc 26943+ case Opt_add:
26944+ err = au_br_add(sb, &opt->add,
26945+ au_ftest_opts(opts->flags, REMOUNT));
26946+ if (!err) {
26947+ err = 1;
027c5e7a 26948+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 26949+ }
26950+ break;
26951+
26952+ case Opt_del:
26953+ case Opt_idel:
26954+ err = au_br_del(sb, &opt->del,
26955+ au_ftest_opts(opts->flags, REMOUNT));
26956+ if (!err) {
26957+ err = 1;
26958+ au_fset_opts(opts->flags, TRUNC_XIB);
027c5e7a 26959+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 26960+ }
26961+ break;
26962+
26963+ case Opt_mod:
26964+ case Opt_imod:
26965+ err = au_br_mod(sb, &opt->mod,
26966+ au_ftest_opts(opts->flags, REMOUNT),
26967+ &do_refresh);
26968+ if (!err) {
26969+ err = 1;
027c5e7a
AM
26970+ if (do_refresh)
26971+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 26972+ }
26973+ break;
26974+ }
1facf9fc 26975+ return err;
26976+}
26977+
26978+static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
26979+ struct au_opt_xino **opt_xino,
26980+ struct au_opts *opts)
26981+{
26982+ int err;
5afbbe0d 26983+ aufs_bindex_t bbot, bindex;
1facf9fc 26984+ struct dentry *root, *parent, *h_root;
26985+
26986+ err = 0;
26987+ switch (opt->type) {
26988+ case Opt_xino:
1facf9fc 26989+ au_xino_brid_set(sb, -1);
1facf9fc 26990+ /* safe d_parent access */
2000de60 26991+ parent = opt->xino.file->f_path.dentry->d_parent;
1facf9fc 26992+ root = sb->s_root;
5afbbe0d
AM
26993+ bbot = au_sbbot(sb);
26994+ for (bindex = 0; bindex <= bbot; bindex++) {
1facf9fc 26995+ h_root = au_h_dptr(root, bindex);
26996+ if (h_root == parent) {
26997+ au_xino_brid_set(sb, au_sbr_id(sb, bindex));
26998+ break;
26999+ }
27000+ }
062440b3
AM
27001+
27002+ err = au_xino_set(sb, &opt->xino,
27003+ !!au_ftest_opts(opts->flags, REMOUNT));
27004+ if (unlikely(err))
27005+ break;
27006+
27007+ *opt_xino = &opt->xino;
1facf9fc 27008+ break;
27009+
27010+ case Opt_noxino:
27011+ au_xino_clr(sb);
1facf9fc 27012+ *opt_xino = (void *)-1;
27013+ break;
27014+ }
27015+
27016+ return err;
27017+}
27018+
27019+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27020+ unsigned int pending)
27021+{
076b876e 27022+ int err, fhsm;
5afbbe0d 27023+ aufs_bindex_t bindex, bbot;
79b8bda9 27024+ unsigned char do_plink, skip, do_free, can_no_dreval;
1facf9fc 27025+ struct au_branch *br;
27026+ struct au_wbr *wbr;
79b8bda9 27027+ struct dentry *root, *dentry;
1facf9fc 27028+ struct inode *dir, *h_dir;
27029+ struct au_sbinfo *sbinfo;
27030+ struct au_hinode *hdir;
27031+
dece6358
AM
27032+ SiMustAnyLock(sb);
27033+
1facf9fc 27034+ sbinfo = au_sbi(sb);
27035+ AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27036+
2121bcd9 27037+ if (!(sb_flags & SB_RDONLY)) {
dece6358 27038+ if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
0c3ec466 27039+ pr_warn("first branch should be rw\n");
dece6358 27040+ if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
febd17d6 27041+ pr_warn_once("shwh should be used with ro\n");
dece6358 27042+ }
1facf9fc 27043+
4a4d8108 27044+ if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
1facf9fc 27045+ && !au_opt_test(sbinfo->si_mntflags, XINO))
febd17d6 27046+ pr_warn_once("udba=*notify requires xino\n");
1facf9fc 27047+
076b876e 27048+ if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
febd17d6
JR
27049+ pr_warn_once("dirperm1 breaks the protection"
27050+ " by the permission bits on the lower branch\n");
076b876e 27051+
1facf9fc 27052+ err = 0;
076b876e 27053+ fhsm = 0;
1facf9fc 27054+ root = sb->s_root;
5527c038 27055+ dir = d_inode(root);
1facf9fc 27056+ do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
79b8bda9
AM
27057+ can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27058+ UDBA_NONE);
5afbbe0d
AM
27059+ bbot = au_sbbot(sb);
27060+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
1facf9fc 27061+ skip = 0;
27062+ h_dir = au_h_iptr(dir, bindex);
27063+ br = au_sbr(sb, bindex);
1facf9fc 27064+
c1595e42
JR
27065+ if ((br->br_perm & AuBrAttr_ICEX)
27066+ && !h_dir->i_op->listxattr)
27067+ br->br_perm &= ~AuBrAttr_ICEX;
27068+#if 0
27069+ if ((br->br_perm & AuBrAttr_ICEX_SEC)
2121bcd9 27070+ && (au_br_sb(br)->s_flags & SB_NOSEC))
c1595e42
JR
27071+ br->br_perm &= ~AuBrAttr_ICEX_SEC;
27072+#endif
27073+
27074+ do_free = 0;
1facf9fc 27075+ wbr = br->br_wbr;
27076+ if (wbr)
27077+ wbr_wh_read_lock(wbr);
27078+
1e00d052 27079+ if (!au_br_writable(br->br_perm)) {
1facf9fc 27080+ do_free = !!wbr;
27081+ skip = (!wbr
27082+ || (!wbr->wbr_whbase
27083+ && !wbr->wbr_plink
27084+ && !wbr->wbr_orph));
1e00d052 27085+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 27086+ /* skip = (!br->br_whbase && !br->br_orph); */
27087+ skip = (!wbr || !wbr->wbr_whbase);
27088+ if (skip && wbr) {
27089+ if (do_plink)
27090+ skip = !!wbr->wbr_plink;
27091+ else
27092+ skip = !wbr->wbr_plink;
27093+ }
1e00d052 27094+ } else {
1facf9fc 27095+ /* skip = (br->br_whbase && br->br_ohph); */
27096+ skip = (wbr && wbr->wbr_whbase);
27097+ if (skip) {
27098+ if (do_plink)
27099+ skip = !!wbr->wbr_plink;
27100+ else
27101+ skip = !wbr->wbr_plink;
27102+ }
1facf9fc 27103+ }
27104+ if (wbr)
27105+ wbr_wh_read_unlock(wbr);
27106+
79b8bda9
AM
27107+ if (can_no_dreval) {
27108+ dentry = br->br_path.dentry;
27109+ spin_lock(&dentry->d_lock);
27110+ if (dentry->d_flags &
27111+ (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27112+ can_no_dreval = 0;
27113+ spin_unlock(&dentry->d_lock);
27114+ }
27115+
076b876e
AM
27116+ if (au_br_fhsm(br->br_perm)) {
27117+ fhsm++;
27118+ AuDebugOn(!br->br_fhsm);
27119+ }
27120+
1facf9fc 27121+ if (skip)
27122+ continue;
27123+
27124+ hdir = au_hi(dir, bindex);
5afbbe0d 27125+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 27126+ if (wbr)
27127+ wbr_wh_write_lock(wbr);
86dc4139 27128+ err = au_wh_init(br, sb);
1facf9fc 27129+ if (wbr)
27130+ wbr_wh_write_unlock(wbr);
5afbbe0d 27131+ au_hn_inode_unlock(hdir);
1facf9fc 27132+
27133+ if (!err && do_free) {
1c60b727 27134+ kfree(wbr);
1facf9fc 27135+ br->br_wbr = NULL;
27136+ }
27137+ }
27138+
79b8bda9
AM
27139+ if (can_no_dreval)
27140+ au_fset_si(sbinfo, NO_DREVAL);
27141+ else
27142+ au_fclr_si(sbinfo, NO_DREVAL);
27143+
c1595e42 27144+ if (fhsm >= 2) {
076b876e 27145+ au_fset_si(sbinfo, FHSM);
5afbbe0d 27146+ for (bindex = bbot; bindex >= 0; bindex--) {
c1595e42
JR
27147+ br = au_sbr(sb, bindex);
27148+ if (au_br_fhsm(br->br_perm)) {
27149+ au_fhsm_set_bottom(sb, bindex);
27150+ break;
27151+ }
27152+ }
27153+ } else {
076b876e 27154+ au_fclr_si(sbinfo, FHSM);
c1595e42
JR
27155+ au_fhsm_set_bottom(sb, -1);
27156+ }
076b876e 27157+
1facf9fc 27158+ return err;
27159+}
27160+
27161+int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27162+{
27163+ int err;
27164+ unsigned int tmp;
5afbbe0d 27165+ aufs_bindex_t bindex, bbot;
1facf9fc 27166+ struct au_opt *opt;
27167+ struct au_opt_xino *opt_xino, xino;
27168+ struct au_sbinfo *sbinfo;
027c5e7a 27169+ struct au_branch *br;
076b876e 27170+ struct inode *dir;
1facf9fc 27171+
dece6358
AM
27172+ SiMustWriteLock(sb);
27173+
1facf9fc 27174+ err = 0;
27175+ opt_xino = NULL;
27176+ opt = opts->opt;
27177+ while (err >= 0 && opt->type != Opt_tail)
27178+ err = au_opt_simple(sb, opt++, opts);
27179+ if (err > 0)
27180+ err = 0;
27181+ else if (unlikely(err < 0))
27182+ goto out;
27183+
27184+ /* disable xino and udba temporary */
27185+ sbinfo = au_sbi(sb);
27186+ tmp = sbinfo->si_mntflags;
27187+ au_opt_clr(sbinfo->si_mntflags, XINO);
27188+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27189+
27190+ opt = opts->opt;
27191+ while (err >= 0 && opt->type != Opt_tail)
27192+ err = au_opt_br(sb, opt++, opts);
27193+ if (err > 0)
27194+ err = 0;
27195+ else if (unlikely(err < 0))
27196+ goto out;
27197+
5afbbe0d
AM
27198+ bbot = au_sbbot(sb);
27199+ if (unlikely(bbot < 0)) {
1facf9fc 27200+ err = -EINVAL;
4a4d8108 27201+ pr_err("no branches\n");
1facf9fc 27202+ goto out;
27203+ }
27204+
27205+ if (au_opt_test(tmp, XINO))
27206+ au_opt_set(sbinfo->si_mntflags, XINO);
27207+ opt = opts->opt;
27208+ while (!err && opt->type != Opt_tail)
27209+ err = au_opt_xino(sb, opt++, &opt_xino, opts);
27210+ if (unlikely(err))
27211+ goto out;
27212+
27213+ err = au_opts_verify(sb, sb->s_flags, tmp);
27214+ if (unlikely(err))
27215+ goto out;
27216+
27217+ /* restore xino */
27218+ if (au_opt_test(tmp, XINO) && !opt_xino) {
27219+ xino.file = au_xino_def(sb);
27220+ err = PTR_ERR(xino.file);
27221+ if (IS_ERR(xino.file))
27222+ goto out;
27223+
27224+ err = au_xino_set(sb, &xino, /*remount*/0);
27225+ fput(xino.file);
27226+ if (unlikely(err))
27227+ goto out;
27228+ }
27229+
27230+ /* restore udba */
027c5e7a 27231+ tmp &= AuOptMask_UDBA;
1facf9fc 27232+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
027c5e7a 27233+ sbinfo->si_mntflags |= tmp;
5afbbe0d
AM
27234+ bbot = au_sbbot(sb);
27235+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
27236+ br = au_sbr(sb, bindex);
27237+ err = au_hnotify_reset_br(tmp, br, br->br_perm);
27238+ if (unlikely(err))
27239+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
27240+ bindex, err);
27241+ /* go on even if err */
27242+ }
4a4d8108 27243+ if (au_opt_test(tmp, UDBA_HNOTIFY)) {
5527c038 27244+ dir = d_inode(sb->s_root);
4a4d8108 27245+ au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
1facf9fc 27246+ }
27247+
4f0767ce 27248+out:
1facf9fc 27249+ return err;
27250+}
27251+
27252+int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27253+{
27254+ int err, rerr;
79b8bda9 27255+ unsigned char no_dreval;
1facf9fc 27256+ struct inode *dir;
27257+ struct au_opt_xino *opt_xino;
27258+ struct au_opt *opt;
27259+ struct au_sbinfo *sbinfo;
27260+
dece6358
AM
27261+ SiMustWriteLock(sb);
27262+
8b6a4947
AM
27263+ err = au_dr_opt_flush(sb);
27264+ if (unlikely(err))
27265+ goto out;
27266+ au_fset_opts(opts->flags, DR_FLUSHED);
27267+
5527c038 27268+ dir = d_inode(sb->s_root);
1facf9fc 27269+ sbinfo = au_sbi(sb);
1facf9fc 27270+ opt_xino = NULL;
27271+ opt = opts->opt;
27272+ while (err >= 0 && opt->type != Opt_tail) {
27273+ err = au_opt_simple(sb, opt, opts);
27274+ if (!err)
27275+ err = au_opt_br(sb, opt, opts);
27276+ if (!err)
27277+ err = au_opt_xino(sb, opt, &opt_xino, opts);
27278+ opt++;
27279+ }
27280+ if (err > 0)
27281+ err = 0;
27282+ AuTraceErr(err);
27283+ /* go on even err */
27284+
79b8bda9 27285+ no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
1facf9fc 27286+ rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27287+ if (unlikely(rerr && !err))
27288+ err = rerr;
27289+
79b8bda9 27290+ if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
b95c5147 27291+ au_fset_opts(opts->flags, REFRESH_IDOP);
79b8bda9 27292+
1facf9fc 27293+ if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27294+ rerr = au_xib_trunc(sb);
27295+ if (unlikely(rerr && !err))
27296+ err = rerr;
27297+ }
27298+
27299+ /* will be handled by the caller */
027c5e7a 27300+ if (!au_ftest_opts(opts->flags, REFRESH)
79b8bda9
AM
27301+ && (opts->given_udba
27302+ || au_opt_test(sbinfo->si_mntflags, XINO)
b95c5147 27303+ || au_ftest_opts(opts->flags, REFRESH_IDOP)
79b8bda9 27304+ ))
027c5e7a 27305+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27306+
27307+ AuDbg("status 0x%x\n", opts->flags);
8b6a4947
AM
27308+
27309+out:
1facf9fc 27310+ return err;
27311+}
27312+
27313+/* ---------------------------------------------------------------------- */
27314+
27315+unsigned int au_opt_udba(struct super_block *sb)
27316+{
27317+ return au_mntflags(sb) & AuOptMask_UDBA;
27318+}
7f207e10
AM
27319diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27320--- /usr/share/empty/fs/aufs/opts.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 27321+++ linux/fs/aufs/opts.h 2018-08-12 23:43:05.460124736 +0200
062440b3
AM
27322@@ -0,0 +1,225 @@
27323+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 27324+/*
b00004a5 27325+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 27326+ *
27327+ * This program, aufs is free software; you can redistribute it and/or modify
27328+ * it under the terms of the GNU General Public License as published by
27329+ * the Free Software Foundation; either version 2 of the License, or
27330+ * (at your option) any later version.
dece6358
AM
27331+ *
27332+ * This program is distributed in the hope that it will be useful,
27333+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27334+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27335+ * GNU General Public License for more details.
27336+ *
27337+ * You should have received a copy of the GNU General Public License
523b37e3 27338+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 27339+ */
27340+
27341+/*
27342+ * mount options/flags
27343+ */
27344+
27345+#ifndef __AUFS_OPTS_H__
27346+#define __AUFS_OPTS_H__
27347+
27348+#ifdef __KERNEL__
27349+
dece6358 27350+#include <linux/path.h>
1facf9fc 27351+
dece6358 27352+struct file;
dece6358 27353+
1facf9fc 27354+/* ---------------------------------------------------------------------- */
27355+
27356+/* mount flags */
27357+#define AuOpt_XINO 1 /* external inode number bitmap
27358+ and translation table */
27359+#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
27360+#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
27361+#define AuOpt_UDBA_REVAL (1 << 3)
4a4d8108 27362+#define AuOpt_UDBA_HNOTIFY (1 << 4)
dece6358
AM
27363+#define AuOpt_SHWH (1 << 5) /* show whiteout */
27364+#define AuOpt_PLINK (1 << 6) /* pseudo-link */
076b876e
AM
27365+#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
27366+ bits */
dece6358
AM
27367+#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
27368+#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
27369+#define AuOpt_SUM_W (1 << 11) /* unimplemented */
27370+#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
27371+#define AuOpt_VERBOSE (1 << 13) /* busy inode when del-branch */
4a4d8108 27372+#define AuOpt_DIO (1 << 14) /* direct io */
8b6a4947 27373+#define AuOpt_DIRREN (1 << 15) /* directory rename */
1facf9fc 27374+
4a4d8108
AM
27375+#ifndef CONFIG_AUFS_HNOTIFY
27376+#undef AuOpt_UDBA_HNOTIFY
27377+#define AuOpt_UDBA_HNOTIFY 0
1facf9fc 27378+#endif
8b6a4947
AM
27379+#ifndef CONFIG_AUFS_DIRREN
27380+#undef AuOpt_DIRREN
27381+#define AuOpt_DIRREN 0
27382+#endif
dece6358
AM
27383+#ifndef CONFIG_AUFS_SHWH
27384+#undef AuOpt_SHWH
27385+#define AuOpt_SHWH 0
27386+#endif
1facf9fc 27387+
27388+#define AuOpt_Def (AuOpt_XINO \
27389+ | AuOpt_UDBA_REVAL \
27390+ | AuOpt_PLINK \
27391+ /* | AuOpt_DIRPERM1 */ \
27392+ | AuOpt_WARN_PERM)
27393+#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27394+ | AuOpt_UDBA_REVAL \
4a4d8108 27395+ | AuOpt_UDBA_HNOTIFY)
1facf9fc 27396+
27397+#define au_opt_test(flags, name) (flags & AuOpt_##name)
27398+#define au_opt_set(flags, name) do { \
27399+ BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27400+ ((flags) |= AuOpt_##name); \
27401+} while (0)
27402+#define au_opt_set_udba(flags, name) do { \
27403+ (flags) &= ~AuOptMask_UDBA; \
27404+ ((flags) |= AuOpt_##name); \
27405+} while (0)
7f207e10
AM
27406+#define au_opt_clr(flags, name) do { \
27407+ ((flags) &= ~AuOpt_##name); \
27408+} while (0)
1facf9fc 27409+
e49829fe
JR
27410+static inline unsigned int au_opts_plink(unsigned int mntflags)
27411+{
27412+#ifdef CONFIG_PROC_FS
27413+ return mntflags;
27414+#else
27415+ return mntflags & ~AuOpt_PLINK;
27416+#endif
27417+}
27418+
1facf9fc 27419+/* ---------------------------------------------------------------------- */
27420+
27421+/* policies to select one among multiple writable branches */
27422+enum {
27423+ AuWbrCreate_TDP, /* top down parent */
27424+ AuWbrCreate_RR, /* round robin */
27425+ AuWbrCreate_MFS, /* most free space */
27426+ AuWbrCreate_MFSV, /* mfs with seconds */
27427+ AuWbrCreate_MFSRR, /* mfs then rr */
27428+ AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
f2c43d5f
AM
27429+ AuWbrCreate_TDMFS, /* top down regardless parent and mfs */
27430+ AuWbrCreate_TDMFSV, /* top down regardless parent and mfs */
1facf9fc 27431+ AuWbrCreate_PMFS, /* parent and mfs */
27432+ AuWbrCreate_PMFSV, /* parent and mfs with seconds */
392086de
AM
27433+ AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
27434+ AuWbrCreate_PMFSRRV, /* plus seconds */
1facf9fc 27435+
27436+ AuWbrCreate_Def = AuWbrCreate_TDP
27437+};
27438+
27439+enum {
27440+ AuWbrCopyup_TDP, /* top down parent */
27441+ AuWbrCopyup_BUP, /* bottom up parent */
27442+ AuWbrCopyup_BU, /* bottom up */
27443+
27444+ AuWbrCopyup_Def = AuWbrCopyup_TDP
27445+};
27446+
27447+/* ---------------------------------------------------------------------- */
27448+
27449+struct au_opt_add {
27450+ aufs_bindex_t bindex;
27451+ char *pathname;
27452+ int perm;
27453+ struct path path;
27454+};
27455+
27456+struct au_opt_del {
27457+ char *pathname;
27458+ struct path h_path;
27459+};
27460+
27461+struct au_opt_mod {
27462+ char *path;
27463+ int perm;
27464+ struct dentry *h_root;
27465+};
27466+
27467+struct au_opt_xino {
27468+ char *path;
27469+ struct file *file;
27470+};
27471+
27472+struct au_opt_xino_itrunc {
27473+ aufs_bindex_t bindex;
27474+};
27475+
27476+struct au_opt_wbr_create {
27477+ int wbr_create;
27478+ int mfs_second;
27479+ unsigned long long mfsrr_watermark;
27480+};
27481+
27482+struct au_opt {
27483+ int type;
27484+ union {
27485+ struct au_opt_xino xino;
27486+ struct au_opt_xino_itrunc xino_itrunc;
27487+ struct au_opt_add add;
27488+ struct au_opt_del del;
27489+ struct au_opt_mod mod;
27490+ int dirwh;
27491+ int rdcache;
27492+ unsigned int rdblk;
27493+ unsigned int rdhash;
27494+ int udba;
27495+ struct au_opt_wbr_create wbr_create;
27496+ int wbr_copyup;
076b876e 27497+ unsigned int fhsm_second;
1facf9fc 27498+ };
27499+};
27500+
27501+/* opts flags */
27502+#define AuOpts_REMOUNT 1
027c5e7a
AM
27503+#define AuOpts_REFRESH (1 << 1)
27504+#define AuOpts_TRUNC_XIB (1 << 2)
27505+#define AuOpts_REFRESH_DYAOP (1 << 3)
b95c5147 27506+#define AuOpts_REFRESH_IDOP (1 << 4)
8b6a4947 27507+#define AuOpts_DR_FLUSHED (1 << 5)
1facf9fc 27508+#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
7f207e10
AM
27509+#define au_fset_opts(flags, name) \
27510+ do { (flags) |= AuOpts_##name; } while (0)
27511+#define au_fclr_opts(flags, name) \
27512+ do { (flags) &= ~AuOpts_##name; } while (0)
1facf9fc 27513+
8b6a4947
AM
27514+#ifndef CONFIG_AUFS_DIRREN
27515+#undef AuOpts_DR_FLUSHED
27516+#define AuOpts_DR_FLUSHED 0
27517+#endif
27518+
1facf9fc 27519+struct au_opts {
27520+ struct au_opt *opt;
27521+ int max_opt;
27522+
27523+ unsigned int given_udba;
27524+ unsigned int flags;
27525+ unsigned long sb_flags;
27526+};
27527+
27528+/* ---------------------------------------------------------------------- */
27529+
7e9cd9fe 27530+/* opts.c */
076b876e 27531+void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
1facf9fc 27532+const char *au_optstr_udba(int udba);
27533+const char *au_optstr_wbr_copyup(int wbr_copyup);
27534+const char *au_optstr_wbr_create(int wbr_create);
27535+
27536+void au_opts_free(struct au_opts *opts);
3c1bdaff 27537+struct super_block;
1facf9fc 27538+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
27539+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27540+ unsigned int pending);
27541+int au_opts_mount(struct super_block *sb, struct au_opts *opts);
27542+int au_opts_remount(struct super_block *sb, struct au_opts *opts);
27543+
27544+unsigned int au_opt_udba(struct super_block *sb);
27545+
1facf9fc 27546+#endif /* __KERNEL__ */
27547+#endif /* __AUFS_OPTS_H__ */
7f207e10
AM
27548diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
27549--- /usr/share/empty/fs/aufs/plink.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 27550+++ linux/fs/aufs/plink.c 2018-08-12 23:43:05.460124736 +0200
062440b3 27551@@ -0,0 +1,516 @@
cd7a4cd9 27552+// SPDX-License-Identifier: GPL-2.0
1facf9fc 27553+/*
b00004a5 27554+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 27555+ *
27556+ * This program, aufs is free software; you can redistribute it and/or modify
27557+ * it under the terms of the GNU General Public License as published by
27558+ * the Free Software Foundation; either version 2 of the License, or
27559+ * (at your option) any later version.
dece6358
AM
27560+ *
27561+ * This program is distributed in the hope that it will be useful,
27562+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27563+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27564+ * GNU General Public License for more details.
27565+ *
27566+ * You should have received a copy of the GNU General Public License
523b37e3 27567+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 27568+ */
27569+
27570+/*
27571+ * pseudo-link
27572+ */
27573+
27574+#include "aufs.h"
27575+
27576+/*
e49829fe 27577+ * the pseudo-link maintenance mode.
1facf9fc 27578+ * during a user process maintains the pseudo-links,
27579+ * prohibit adding a new plink and branch manipulation.
e49829fe
JR
27580+ *
27581+ * Flags
27582+ * NOPLM:
27583+ * For entry functions which will handle plink, and i_mutex is already held
27584+ * in VFS.
27585+ * They cannot wait and should return an error at once.
27586+ * Callers has to check the error.
27587+ * NOPLMW:
27588+ * For entry functions which will handle plink, but i_mutex is not held
27589+ * in VFS.
27590+ * They can wait the plink maintenance mode to finish.
27591+ *
27592+ * They behave like F_SETLK and F_SETLKW.
27593+ * If the caller never handle plink, then both flags are unnecessary.
1facf9fc 27594+ */
e49829fe
JR
27595+
27596+int au_plink_maint(struct super_block *sb, int flags)
1facf9fc 27597+{
e49829fe
JR
27598+ int err;
27599+ pid_t pid, ppid;
f0c0a007 27600+ struct task_struct *parent, *prev;
e49829fe 27601+ struct au_sbinfo *sbi;
dece6358
AM
27602+
27603+ SiMustAnyLock(sb);
27604+
e49829fe
JR
27605+ err = 0;
27606+ if (!au_opt_test(au_mntflags(sb), PLINK))
27607+ goto out;
27608+
27609+ sbi = au_sbi(sb);
27610+ pid = sbi->si_plink_maint_pid;
27611+ if (!pid || pid == current->pid)
27612+ goto out;
27613+
27614+ /* todo: it highly depends upon /sbin/mount.aufs */
f0c0a007
AM
27615+ prev = NULL;
27616+ parent = current;
27617+ ppid = 0;
e49829fe 27618+ rcu_read_lock();
f0c0a007
AM
27619+ while (1) {
27620+ parent = rcu_dereference(parent->real_parent);
27621+ if (parent == prev)
27622+ break;
27623+ ppid = task_pid_vnr(parent);
27624+ if (pid == ppid) {
27625+ rcu_read_unlock();
27626+ goto out;
27627+ }
27628+ prev = parent;
27629+ }
e49829fe 27630+ rcu_read_unlock();
e49829fe
JR
27631+
27632+ if (au_ftest_lock(flags, NOPLMW)) {
027c5e7a
AM
27633+ /* if there is no i_mutex lock in VFS, we don't need to wait */
27634+ /* AuDebugOn(!lockdep_depth(current)); */
e49829fe
JR
27635+ while (sbi->si_plink_maint_pid) {
27636+ si_read_unlock(sb);
27637+ /* gave up wake_up_bit() */
27638+ wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
27639+
27640+ if (au_ftest_lock(flags, FLUSH))
27641+ au_nwt_flush(&sbi->si_nowait);
27642+ si_noflush_read_lock(sb);
27643+ }
27644+ } else if (au_ftest_lock(flags, NOPLM)) {
27645+ AuDbg("ppid %d, pid %d\n", ppid, pid);
27646+ err = -EAGAIN;
27647+ }
27648+
27649+out:
27650+ return err;
4a4d8108
AM
27651+}
27652+
e49829fe 27653+void au_plink_maint_leave(struct au_sbinfo *sbinfo)
4a4d8108 27654+{
4a4d8108 27655+ spin_lock(&sbinfo->si_plink_maint_lock);
027c5e7a 27656+ sbinfo->si_plink_maint_pid = 0;
4a4d8108 27657+ spin_unlock(&sbinfo->si_plink_maint_lock);
027c5e7a 27658+ wake_up_all(&sbinfo->si_plink_wq);
4a4d8108
AM
27659+}
27660+
e49829fe 27661+int au_plink_maint_enter(struct super_block *sb)
4a4d8108
AM
27662+{
27663+ int err;
4a4d8108
AM
27664+ struct au_sbinfo *sbinfo;
27665+
27666+ err = 0;
4a4d8108
AM
27667+ sbinfo = au_sbi(sb);
27668+ /* make sure i am the only one in this fs */
e49829fe
JR
27669+ si_write_lock(sb, AuLock_FLUSH);
27670+ if (au_opt_test(au_mntflags(sb), PLINK)) {
27671+ spin_lock(&sbinfo->si_plink_maint_lock);
27672+ if (!sbinfo->si_plink_maint_pid)
27673+ sbinfo->si_plink_maint_pid = current->pid;
27674+ else
27675+ err = -EBUSY;
27676+ spin_unlock(&sbinfo->si_plink_maint_lock);
27677+ }
4a4d8108
AM
27678+ si_write_unlock(sb);
27679+
27680+ return err;
1facf9fc 27681+}
27682+
27683+/* ---------------------------------------------------------------------- */
27684+
1facf9fc 27685+#ifdef CONFIG_AUFS_DEBUG
27686+void au_plink_list(struct super_block *sb)
27687+{
86dc4139 27688+ int i;
1facf9fc 27689+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27690+ struct hlist_bl_head *hbl;
27691+ struct hlist_bl_node *pos;
5afbbe0d 27692+ struct au_icntnr *icntnr;
1facf9fc 27693+
dece6358
AM
27694+ SiMustAnyLock(sb);
27695+
1facf9fc 27696+ sbinfo = au_sbi(sb);
27697+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 27698+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 27699+
86dc4139 27700+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
27701+ hbl = sbinfo->si_plink + i;
27702+ hlist_bl_lock(hbl);
27703+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
5afbbe0d 27704+ AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
8b6a4947 27705+ hlist_bl_unlock(hbl);
86dc4139 27706+ }
1facf9fc 27707+}
27708+#endif
27709+
27710+/* is the inode pseudo-linked? */
27711+int au_plink_test(struct inode *inode)
27712+{
86dc4139 27713+ int found, i;
1facf9fc 27714+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27715+ struct hlist_bl_head *hbl;
27716+ struct hlist_bl_node *pos;
5afbbe0d 27717+ struct au_icntnr *icntnr;
1facf9fc 27718+
27719+ sbinfo = au_sbi(inode->i_sb);
dece6358 27720+ AuRwMustAnyLock(&sbinfo->si_rwsem);
1facf9fc 27721+ AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
e49829fe 27722+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
1facf9fc 27723+
27724+ found = 0;
86dc4139 27725+ i = au_plink_hash(inode->i_ino);
8b6a4947
AM
27726+ hbl = sbinfo->si_plink + i;
27727+ hlist_bl_lock(hbl);
27728+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
5afbbe0d 27729+ if (&icntnr->vfs_inode == inode) {
1facf9fc 27730+ found = 1;
27731+ break;
27732+ }
8b6a4947 27733+ hlist_bl_unlock(hbl);
1facf9fc 27734+ return found;
27735+}
27736+
27737+/* ---------------------------------------------------------------------- */
27738+
27739+/*
27740+ * generate a name for plink.
27741+ * the file will be stored under AUFS_WH_PLINKDIR.
27742+ */
27743+/* 20 is max digits length of ulong 64 */
27744+#define PLINK_NAME_LEN ((20 + 1) * 2)
27745+
27746+static int plink_name(char *name, int len, struct inode *inode,
27747+ aufs_bindex_t bindex)
27748+{
27749+ int rlen;
27750+ struct inode *h_inode;
27751+
27752+ h_inode = au_h_iptr(inode, bindex);
27753+ rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
27754+ return rlen;
27755+}
27756+
7f207e10
AM
27757+struct au_do_plink_lkup_args {
27758+ struct dentry **errp;
27759+ struct qstr *tgtname;
27760+ struct dentry *h_parent;
27761+ struct au_branch *br;
27762+};
27763+
27764+static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
27765+ struct dentry *h_parent,
27766+ struct au_branch *br)
27767+{
27768+ struct dentry *h_dentry;
febd17d6 27769+ struct inode *h_inode;
7f207e10 27770+
febd17d6 27771+ h_inode = d_inode(h_parent);
be118d29 27772+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
b4510431 27773+ h_dentry = vfsub_lkup_one(tgtname, h_parent);
3c1bdaff 27774+ inode_unlock_shared(h_inode);
7f207e10
AM
27775+ return h_dentry;
27776+}
27777+
27778+static void au_call_do_plink_lkup(void *args)
27779+{
27780+ struct au_do_plink_lkup_args *a = args;
27781+ *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
27782+}
27783+
1facf9fc 27784+/* lookup the plink-ed @inode under the branch at @bindex */
27785+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
27786+{
27787+ struct dentry *h_dentry, *h_parent;
27788+ struct au_branch *br;
7f207e10 27789+ int wkq_err;
1facf9fc 27790+ char a[PLINK_NAME_LEN];
0c3ec466 27791+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 27792+
e49829fe
JR
27793+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27794+
1facf9fc 27795+ br = au_sbr(inode->i_sb, bindex);
27796+ h_parent = br->br_wbr->wbr_plink;
1facf9fc 27797+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
27798+
2dfbb274 27799+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
7f207e10
AM
27800+ struct au_do_plink_lkup_args args = {
27801+ .errp = &h_dentry,
27802+ .tgtname = &tgtname,
27803+ .h_parent = h_parent,
27804+ .br = br
27805+ };
27806+
27807+ wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
27808+ if (unlikely(wkq_err))
27809+ h_dentry = ERR_PTR(wkq_err);
27810+ } else
27811+ h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
27812+
1facf9fc 27813+ return h_dentry;
27814+}
27815+
27816+/* create a pseudo-link */
27817+static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
27818+ struct dentry *h_dentry, struct au_branch *br)
27819+{
27820+ int err;
27821+ struct path h_path = {
86dc4139 27822+ .mnt = au_br_mnt(br)
1facf9fc 27823+ };
523b37e3 27824+ struct inode *h_dir, *delegated;
1facf9fc 27825+
5527c038 27826+ h_dir = d_inode(h_parent);
febd17d6 27827+ inode_lock_nested(h_dir, AuLsc_I_CHILD2);
4f0767ce 27828+again:
b4510431 27829+ h_path.dentry = vfsub_lkup_one(tgt, h_parent);
1facf9fc 27830+ err = PTR_ERR(h_path.dentry);
27831+ if (IS_ERR(h_path.dentry))
27832+ goto out;
27833+
27834+ err = 0;
27835+ /* wh.plink dir is not monitored */
7f207e10 27836+ /* todo: is it really safe? */
5527c038
JR
27837+ if (d_is_positive(h_path.dentry)
27838+ && d_inode(h_path.dentry) != d_inode(h_dentry)) {
523b37e3
AM
27839+ delegated = NULL;
27840+ err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
27841+ if (unlikely(err == -EWOULDBLOCK)) {
27842+ pr_warn("cannot retry for NFSv4 delegation"
27843+ " for an internal unlink\n");
27844+ iput(delegated);
27845+ }
1facf9fc 27846+ dput(h_path.dentry);
27847+ h_path.dentry = NULL;
27848+ if (!err)
27849+ goto again;
27850+ }
5527c038 27851+ if (!err && d_is_negative(h_path.dentry)) {
523b37e3
AM
27852+ delegated = NULL;
27853+ err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
27854+ if (unlikely(err == -EWOULDBLOCK)) {
27855+ pr_warn("cannot retry for NFSv4 delegation"
27856+ " for an internal link\n");
27857+ iput(delegated);
27858+ }
27859+ }
1facf9fc 27860+ dput(h_path.dentry);
27861+
4f0767ce 27862+out:
febd17d6 27863+ inode_unlock(h_dir);
1facf9fc 27864+ return err;
27865+}
27866+
27867+struct do_whplink_args {
27868+ int *errp;
27869+ struct qstr *tgt;
27870+ struct dentry *h_parent;
27871+ struct dentry *h_dentry;
27872+ struct au_branch *br;
27873+};
27874+
27875+static void call_do_whplink(void *args)
27876+{
27877+ struct do_whplink_args *a = args;
27878+ *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
27879+}
27880+
27881+static int whplink(struct dentry *h_dentry, struct inode *inode,
27882+ aufs_bindex_t bindex, struct au_branch *br)
27883+{
27884+ int err, wkq_err;
27885+ struct au_wbr *wbr;
27886+ struct dentry *h_parent;
1facf9fc 27887+ char a[PLINK_NAME_LEN];
0c3ec466 27888+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 27889+
27890+ wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
27891+ h_parent = wbr->wbr_plink;
1facf9fc 27892+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
27893+
27894+ /* always superio. */
2dfbb274 27895+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
1facf9fc 27896+ struct do_whplink_args args = {
27897+ .errp = &err,
27898+ .tgt = &tgtname,
27899+ .h_parent = h_parent,
27900+ .h_dentry = h_dentry,
27901+ .br = br
27902+ };
27903+ wkq_err = au_wkq_wait(call_do_whplink, &args);
27904+ if (unlikely(wkq_err))
27905+ err = wkq_err;
27906+ } else
27907+ err = do_whplink(&tgtname, h_parent, h_dentry, br);
1facf9fc 27908+
27909+ return err;
27910+}
27911+
1facf9fc 27912+/*
27913+ * create a new pseudo-link for @h_dentry on @bindex.
27914+ * the linked inode is held in aufs @inode.
27915+ */
27916+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
27917+ struct dentry *h_dentry)
27918+{
27919+ struct super_block *sb;
27920+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27921+ struct hlist_bl_head *hbl;
27922+ struct hlist_bl_node *pos;
5afbbe0d 27923+ struct au_icntnr *icntnr;
86dc4139 27924+ int found, err, cnt, i;
1facf9fc 27925+
27926+ sb = inode->i_sb;
27927+ sbinfo = au_sbi(sb);
27928+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 27929+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 27930+
86dc4139 27931+ found = au_plink_test(inode);
4a4d8108 27932+ if (found)
1facf9fc 27933+ return;
4a4d8108 27934+
86dc4139 27935+ i = au_plink_hash(inode->i_ino);
8b6a4947 27936+ hbl = sbinfo->si_plink + i;
5afbbe0d 27937+ au_igrab(inode);
1facf9fc 27938+
8b6a4947
AM
27939+ hlist_bl_lock(hbl);
27940+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
5afbbe0d 27941+ if (&icntnr->vfs_inode == inode) {
4a4d8108
AM
27942+ found = 1;
27943+ break;
27944+ }
1facf9fc 27945+ }
5afbbe0d
AM
27946+ if (!found) {
27947+ icntnr = container_of(inode, struct au_icntnr, vfs_inode);
8b6a4947 27948+ hlist_bl_add_head(&icntnr->plink, hbl);
5afbbe0d 27949+ }
8b6a4947 27950+ hlist_bl_unlock(hbl);
4a4d8108 27951+ if (!found) {
8b6a4947 27952+ cnt = au_hbl_count(hbl);
86dc4139
AM
27953+#define msg "unexpectedly unblanced or too many pseudo-links"
27954+ if (cnt > AUFS_PLINK_WARN)
27955+ AuWarn1(msg ", %d\n", cnt);
27956+#undef msg
1facf9fc 27957+ err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
5afbbe0d
AM
27958+ if (unlikely(err)) {
27959+ pr_warn("err %d, damaged pseudo link.\n", err);
8b6a4947 27960+ au_hbl_del(&icntnr->plink, hbl);
5afbbe0d 27961+ iput(&icntnr->vfs_inode);
4a4d8108 27962+ }
5afbbe0d
AM
27963+ } else
27964+ iput(&icntnr->vfs_inode);
1facf9fc 27965+}
27966+
27967+/* free all plinks */
e49829fe 27968+void au_plink_put(struct super_block *sb, int verbose)
1facf9fc 27969+{
86dc4139 27970+ int i, warned;
1facf9fc 27971+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27972+ struct hlist_bl_head *hbl;
27973+ struct hlist_bl_node *pos, *tmp;
5afbbe0d 27974+ struct au_icntnr *icntnr;
1facf9fc 27975+
dece6358
AM
27976+ SiMustWriteLock(sb);
27977+
1facf9fc 27978+ sbinfo = au_sbi(sb);
27979+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 27980+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 27981+
1facf9fc 27982+ /* no spin_lock since sbinfo is write-locked */
86dc4139
AM
27983+ warned = 0;
27984+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
27985+ hbl = sbinfo->si_plink + i;
27986+ if (!warned && verbose && !hlist_bl_empty(hbl)) {
86dc4139
AM
27987+ pr_warn("pseudo-link is not flushed");
27988+ warned = 1;
27989+ }
8b6a4947 27990+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
5afbbe0d 27991+ iput(&icntnr->vfs_inode);
8b6a4947 27992+ INIT_HLIST_BL_HEAD(hbl);
86dc4139 27993+ }
1facf9fc 27994+}
27995+
e49829fe
JR
27996+void au_plink_clean(struct super_block *sb, int verbose)
27997+{
27998+ struct dentry *root;
27999+
28000+ root = sb->s_root;
28001+ aufs_write_lock(root);
28002+ if (au_opt_test(au_mntflags(sb), PLINK))
28003+ au_plink_put(sb, verbose);
28004+ aufs_write_unlock(root);
28005+}
28006+
86dc4139
AM
28007+static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28008+{
28009+ int do_put;
5afbbe0d 28010+ aufs_bindex_t btop, bbot, bindex;
86dc4139
AM
28011+
28012+ do_put = 0;
5afbbe0d
AM
28013+ btop = au_ibtop(inode);
28014+ bbot = au_ibbot(inode);
28015+ if (btop >= 0) {
28016+ for (bindex = btop; bindex <= bbot; bindex++) {
86dc4139
AM
28017+ if (!au_h_iptr(inode, bindex)
28018+ || au_ii_br_id(inode, bindex) != br_id)
28019+ continue;
28020+ au_set_h_iptr(inode, bindex, NULL, 0);
28021+ do_put = 1;
28022+ break;
28023+ }
28024+ if (do_put)
5afbbe0d 28025+ for (bindex = btop; bindex <= bbot; bindex++)
86dc4139
AM
28026+ if (au_h_iptr(inode, bindex)) {
28027+ do_put = 0;
28028+ break;
28029+ }
28030+ } else
28031+ do_put = 1;
28032+
28033+ return do_put;
28034+}
28035+
1facf9fc 28036+/* free the plinks on a branch specified by @br_id */
28037+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28038+{
28039+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28040+ struct hlist_bl_head *hbl;
28041+ struct hlist_bl_node *pos, *tmp;
5afbbe0d 28042+ struct au_icntnr *icntnr;
1facf9fc 28043+ struct inode *inode;
86dc4139 28044+ int i, do_put;
1facf9fc 28045+
dece6358
AM
28046+ SiMustWriteLock(sb);
28047+
1facf9fc 28048+ sbinfo = au_sbi(sb);
28049+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28050+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28051+
8b6a4947 28052+ /* no bit_lock since sbinfo is write-locked */
86dc4139 28053+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
28054+ hbl = sbinfo->si_plink + i;
28055+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
5afbbe0d 28056+ inode = au_igrab(&icntnr->vfs_inode);
86dc4139
AM
28057+ ii_write_lock_child(inode);
28058+ do_put = au_plink_do_half_refresh(inode, br_id);
5afbbe0d 28059+ if (do_put) {
8b6a4947 28060+ hlist_bl_del(&icntnr->plink);
5afbbe0d
AM
28061+ iput(inode);
28062+ }
86dc4139
AM
28063+ ii_write_unlock(inode);
28064+ iput(inode);
dece6358 28065+ }
dece6358
AM
28066+ }
28067+}
7f207e10
AM
28068diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28069--- /usr/share/empty/fs/aufs/poll.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9
AM
28070+++ linux/fs/aufs/poll.c 2018-08-12 23:43:05.460124736 +0200
28071@@ -0,0 +1,51 @@
28072+// SPDX-License-Identifier: GPL-2.0
dece6358 28073+/*
b00004a5 28074+ * Copyright (C) 2005-2018 Junjiro R. Okajima
dece6358
AM
28075+ *
28076+ * This program, aufs is free software; you can redistribute it and/or modify
28077+ * it under the terms of the GNU General Public License as published by
28078+ * the Free Software Foundation; either version 2 of the License, or
28079+ * (at your option) any later version.
28080+ *
28081+ * This program is distributed in the hope that it will be useful,
28082+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28083+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28084+ * GNU General Public License for more details.
28085+ *
28086+ * You should have received a copy of the GNU General Public License
523b37e3 28087+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358
AM
28088+ */
28089+
1308ab2a 28090+/*
28091+ * poll operation
28092+ * There is only one filesystem which implements ->poll operation, currently.
28093+ */
28094+
28095+#include "aufs.h"
28096+
cd7a4cd9 28097+__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
1308ab2a 28098+{
be118d29 28099+ __poll_t mask;
1308ab2a 28100+ struct file *h_file;
1308ab2a 28101+ struct super_block *sb;
28102+
28103+ /* We should pretend an error happened. */
be118d29 28104+ mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
b912730e 28105+ sb = file->f_path.dentry->d_sb;
e49829fe 28106+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 28107+
521ced18 28108+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
062440b3
AM
28109+ if (IS_ERR(h_file)) {
28110+ AuDbg("h_file %ld\n", PTR_ERR(h_file));
1308ab2a 28111+ goto out;
062440b3 28112+ }
1308ab2a 28113+
cd7a4cd9 28114+ mask = vfs_poll(h_file, pt);
b912730e 28115+ fput(h_file); /* instead of au_read_post() */
1308ab2a 28116+
4f0767ce 28117+out:
1308ab2a 28118+ si_read_unlock(sb);
062440b3 28119+ if (mask & EPOLLERR)
b00004a5 28120+ AuDbg("mask 0x%x\n", mask);
1308ab2a 28121+ return mask;
28122+}
c1595e42
JR
28123diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28124--- /usr/share/empty/fs/aufs/posix_acl.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 28125+++ linux/fs/aufs/posix_acl.c 2018-08-12 23:43:05.460124736 +0200
062440b3 28126@@ -0,0 +1,103 @@
cd7a4cd9 28127+// SPDX-License-Identifier: GPL-2.0
c1595e42 28128+/*
b00004a5 28129+ * Copyright (C) 2014-2018 Junjiro R. Okajima
c1595e42
JR
28130+ *
28131+ * This program, aufs is free software; you can redistribute it and/or modify
28132+ * it under the terms of the GNU General Public License as published by
28133+ * the Free Software Foundation; either version 2 of the License, or
28134+ * (at your option) any later version.
28135+ *
28136+ * This program is distributed in the hope that it will be useful,
28137+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28138+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28139+ * GNU General Public License for more details.
28140+ *
28141+ * You should have received a copy of the GNU General Public License
28142+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
28143+ */
28144+
28145+/*
28146+ * posix acl operations
28147+ */
28148+
28149+#include <linux/fs.h>
c1595e42
JR
28150+#include "aufs.h"
28151+
28152+struct posix_acl *aufs_get_acl(struct inode *inode, int type)
28153+{
28154+ struct posix_acl *acl;
28155+ int err;
28156+ aufs_bindex_t bindex;
28157+ struct inode *h_inode;
28158+ struct super_block *sb;
28159+
28160+ acl = NULL;
28161+ sb = inode->i_sb;
28162+ si_read_lock(sb, AuLock_FLUSH);
28163+ ii_read_lock_child(inode);
2121bcd9 28164+ if (!(sb->s_flags & SB_POSIXACL))
c1595e42
JR
28165+ goto out;
28166+
5afbbe0d 28167+ bindex = au_ibtop(inode);
c1595e42
JR
28168+ h_inode = au_h_iptr(inode, bindex);
28169+ if (unlikely(!h_inode
28170+ || ((h_inode->i_mode & S_IFMT)
28171+ != (inode->i_mode & S_IFMT)))) {
28172+ err = au_busy_or_stale();
28173+ acl = ERR_PTR(err);
28174+ goto out;
28175+ }
28176+
28177+ /* always topmost only */
28178+ acl = get_acl(h_inode, type);
a2654f78
AM
28179+ if (!IS_ERR_OR_NULL(acl))
28180+ set_cached_acl(inode, type, acl);
c1595e42
JR
28181+
28182+out:
28183+ ii_read_unlock(inode);
28184+ si_read_unlock(sb);
28185+
28186+ AuTraceErrPtr(acl);
28187+ return acl;
28188+}
28189+
28190+int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
28191+{
28192+ int err;
28193+ ssize_t ssz;
28194+ struct dentry *dentry;
f2c43d5f 28195+ struct au_sxattr arg = {
c1595e42
JR
28196+ .type = AU_ACL_SET,
28197+ .u.acl_set = {
28198+ .acl = acl,
28199+ .type = type
28200+ },
28201+ };
28202+
5afbbe0d
AM
28203+ IMustLock(inode);
28204+
c1595e42
JR
28205+ if (inode->i_ino == AUFS_ROOT_INO)
28206+ dentry = dget(inode->i_sb->s_root);
28207+ else {
28208+ dentry = d_find_alias(inode);
28209+ if (!dentry)
28210+ dentry = d_find_any_alias(inode);
28211+ if (!dentry) {
28212+ pr_warn("cannot handle this inode, "
28213+ "please report to aufs-users ML\n");
28214+ err = -ENOENT;
28215+ goto out;
28216+ }
28217+ }
28218+
f2c43d5f 28219+ ssz = au_sxattr(dentry, inode, &arg);
c1595e42
JR
28220+ dput(dentry);
28221+ err = ssz;
a2654f78 28222+ if (ssz >= 0) {
c1595e42 28223+ err = 0;
a2654f78
AM
28224+ set_cached_acl(inode, type, acl);
28225+ }
c1595e42
JR
28226+
28227+out:
c1595e42
JR
28228+ return err;
28229+}
7f207e10
AM
28230diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28231--- /usr/share/empty/fs/aufs/procfs.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 28232+++ linux/fs/aufs/procfs.c 2018-08-12 23:43:05.460124736 +0200
062440b3 28233@@ -0,0 +1,171 @@
cd7a4cd9 28234+// SPDX-License-Identifier: GPL-2.0
e49829fe 28235+/*
b00004a5 28236+ * Copyright (C) 2010-2018 Junjiro R. Okajima
e49829fe
JR
28237+ *
28238+ * This program, aufs is free software; you can redistribute it and/or modify
28239+ * it under the terms of the GNU General Public License as published by
28240+ * the Free Software Foundation; either version 2 of the License, or
28241+ * (at your option) any later version.
28242+ *
28243+ * This program is distributed in the hope that it will be useful,
28244+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28245+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28246+ * GNU General Public License for more details.
28247+ *
28248+ * You should have received a copy of the GNU General Public License
523b37e3 28249+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
28250+ */
28251+
28252+/*
28253+ * procfs interfaces
28254+ */
28255+
28256+#include <linux/proc_fs.h>
28257+#include "aufs.h"
28258+
28259+static int au_procfs_plm_release(struct inode *inode, struct file *file)
28260+{
28261+ struct au_sbinfo *sbinfo;
28262+
28263+ sbinfo = file->private_data;
28264+ if (sbinfo) {
28265+ au_plink_maint_leave(sbinfo);
28266+ kobject_put(&sbinfo->si_kobj);
28267+ }
28268+
28269+ return 0;
28270+}
28271+
28272+static void au_procfs_plm_write_clean(struct file *file)
28273+{
28274+ struct au_sbinfo *sbinfo;
28275+
28276+ sbinfo = file->private_data;
28277+ if (sbinfo)
28278+ au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28279+}
28280+
28281+static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28282+{
28283+ int err;
28284+ struct super_block *sb;
28285+ struct au_sbinfo *sbinfo;
8b6a4947 28286+ struct hlist_bl_node *pos;
e49829fe
JR
28287+
28288+ err = -EBUSY;
28289+ if (unlikely(file->private_data))
28290+ goto out;
28291+
28292+ sb = NULL;
53392da6 28293+ /* don't use au_sbilist_lock() here */
8b6a4947
AM
28294+ hlist_bl_lock(&au_sbilist);
28295+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
e49829fe
JR
28296+ if (id == sysaufs_si_id(sbinfo)) {
28297+ kobject_get(&sbinfo->si_kobj);
28298+ sb = sbinfo->si_sb;
28299+ break;
28300+ }
8b6a4947 28301+ hlist_bl_unlock(&au_sbilist);
e49829fe
JR
28302+
28303+ err = -EINVAL;
28304+ if (unlikely(!sb))
28305+ goto out;
28306+
28307+ err = au_plink_maint_enter(sb);
28308+ if (!err)
28309+ /* keep kobject_get() */
28310+ file->private_data = sbinfo;
28311+ else
28312+ kobject_put(&sbinfo->si_kobj);
28313+out:
28314+ return err;
28315+}
28316+
28317+/*
28318+ * Accept a valid "si=xxxx" only.
28319+ * Once it is accepted successfully, accept "clean" too.
28320+ */
28321+static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28322+ size_t count, loff_t *ppos)
28323+{
28324+ ssize_t err;
28325+ unsigned long id;
28326+ /* last newline is allowed */
28327+ char buf[3 + sizeof(unsigned long) * 2 + 1];
28328+
28329+ err = -EACCES;
28330+ if (unlikely(!capable(CAP_SYS_ADMIN)))
28331+ goto out;
28332+
28333+ err = -EINVAL;
28334+ if (unlikely(count > sizeof(buf)))
28335+ goto out;
28336+
28337+ err = copy_from_user(buf, ubuf, count);
28338+ if (unlikely(err)) {
28339+ err = -EFAULT;
28340+ goto out;
28341+ }
28342+ buf[count] = 0;
28343+
28344+ err = -EINVAL;
28345+ if (!strcmp("clean", buf)) {
28346+ au_procfs_plm_write_clean(file);
28347+ goto out_success;
28348+ } else if (unlikely(strncmp("si=", buf, 3)))
28349+ goto out;
28350+
9dbd164d 28351+ err = kstrtoul(buf + 3, 16, &id);
e49829fe
JR
28352+ if (unlikely(err))
28353+ goto out;
28354+
28355+ err = au_procfs_plm_write_si(file, id);
28356+ if (unlikely(err))
28357+ goto out;
28358+
28359+out_success:
28360+ err = count; /* success */
28361+out:
28362+ return err;
28363+}
28364+
28365+static const struct file_operations au_procfs_plm_fop = {
28366+ .write = au_procfs_plm_write,
28367+ .release = au_procfs_plm_release,
28368+ .owner = THIS_MODULE
28369+};
28370+
28371+/* ---------------------------------------------------------------------- */
28372+
28373+static struct proc_dir_entry *au_procfs_dir;
28374+
28375+void au_procfs_fin(void)
28376+{
28377+ remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28378+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28379+}
28380+
28381+int __init au_procfs_init(void)
28382+{
28383+ int err;
28384+ struct proc_dir_entry *entry;
28385+
28386+ err = -ENOMEM;
28387+ au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28388+ if (unlikely(!au_procfs_dir))
28389+ goto out;
28390+
cd7a4cd9 28391+ entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
e49829fe
JR
28392+ au_procfs_dir, &au_procfs_plm_fop);
28393+ if (unlikely(!entry))
28394+ goto out_dir;
28395+
28396+ err = 0;
28397+ goto out; /* success */
28398+
28399+
28400+out_dir:
28401+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28402+out:
28403+ return err;
28404+}
7f207e10
AM
28405diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
28406--- /usr/share/empty/fs/aufs/rdu.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 28407+++ linux/fs/aufs/rdu.c 2018-08-12 23:43:05.463458173 +0200
062440b3 28408@@ -0,0 +1,382 @@
cd7a4cd9 28409+// SPDX-License-Identifier: GPL-2.0
1308ab2a 28410+/*
b00004a5 28411+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1308ab2a 28412+ *
28413+ * This program, aufs is free software; you can redistribute it and/or modify
28414+ * it under the terms of the GNU General Public License as published by
28415+ * the Free Software Foundation; either version 2 of the License, or
28416+ * (at your option) any later version.
28417+ *
28418+ * This program is distributed in the hope that it will be useful,
28419+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28420+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28421+ * GNU General Public License for more details.
28422+ *
28423+ * You should have received a copy of the GNU General Public License
523b37e3 28424+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1308ab2a 28425+ */
28426+
28427+/*
28428+ * readdir in userspace.
28429+ */
28430+
b752ccd1 28431+#include <linux/compat.h>
4a4d8108 28432+#include <linux/fs_stack.h>
1308ab2a 28433+#include <linux/security.h>
1308ab2a 28434+#include "aufs.h"
28435+
28436+/* bits for struct aufs_rdu.flags */
28437+#define AuRdu_CALLED 1
28438+#define AuRdu_CONT (1 << 1)
28439+#define AuRdu_FULL (1 << 2)
28440+#define au_ftest_rdu(flags, name) ((flags) & AuRdu_##name)
7f207e10
AM
28441+#define au_fset_rdu(flags, name) \
28442+ do { (flags) |= AuRdu_##name; } while (0)
28443+#define au_fclr_rdu(flags, name) \
28444+ do { (flags) &= ~AuRdu_##name; } while (0)
1308ab2a 28445+
28446+struct au_rdu_arg {
392086de 28447+ struct dir_context ctx;
1308ab2a 28448+ struct aufs_rdu *rdu;
28449+ union au_rdu_ent_ul ent;
28450+ unsigned long end;
28451+
28452+ struct super_block *sb;
28453+ int err;
28454+};
28455+
392086de 28456+static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
1308ab2a 28457+ loff_t offset, u64 h_ino, unsigned int d_type)
28458+{
28459+ int err, len;
392086de 28460+ struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
1308ab2a 28461+ struct aufs_rdu *rdu = arg->rdu;
28462+ struct au_rdu_ent ent;
28463+
28464+ err = 0;
28465+ arg->err = 0;
28466+ au_fset_rdu(rdu->cookie.flags, CALLED);
28467+ len = au_rdu_len(nlen);
28468+ if (arg->ent.ul + len < arg->end) {
28469+ ent.ino = h_ino;
28470+ ent.bindex = rdu->cookie.bindex;
28471+ ent.type = d_type;
28472+ ent.nlen = nlen;
4a4d8108
AM
28473+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
28474+ ent.type = DT_UNKNOWN;
1308ab2a 28475+
9dbd164d 28476+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 28477+ err = -EFAULT;
28478+ if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
28479+ goto out;
28480+ if (copy_to_user(arg->ent.e->name, name, nlen))
28481+ goto out;
28482+ /* the terminating NULL */
28483+ if (__put_user(0, arg->ent.e->name + nlen))
28484+ goto out;
28485+ err = 0;
28486+ /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
28487+ arg->ent.ul += len;
28488+ rdu->rent++;
28489+ } else {
28490+ err = -EFAULT;
28491+ au_fset_rdu(rdu->cookie.flags, FULL);
28492+ rdu->full = 1;
28493+ rdu->tail = arg->ent;
28494+ }
28495+
4f0767ce 28496+out:
1308ab2a 28497+ /* AuTraceErr(err); */
28498+ return err;
28499+}
28500+
28501+static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
28502+{
28503+ int err;
28504+ loff_t offset;
28505+ struct au_rdu_cookie *cookie = &arg->rdu->cookie;
28506+
92d182d2 28507+ /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
1308ab2a 28508+ offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
28509+ err = offset;
28510+ if (unlikely(offset != cookie->h_pos))
28511+ goto out;
28512+
28513+ err = 0;
28514+ do {
28515+ arg->err = 0;
28516+ au_fclr_rdu(cookie->flags, CALLED);
28517+ /* smp_mb(); */
392086de 28518+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1308ab2a 28519+ if (err >= 0)
28520+ err = arg->err;
28521+ } while (!err
28522+ && au_ftest_rdu(cookie->flags, CALLED)
28523+ && !au_ftest_rdu(cookie->flags, FULL));
28524+ cookie->h_pos = h_file->f_pos;
28525+
4f0767ce 28526+out:
1308ab2a 28527+ AuTraceErr(err);
28528+ return err;
28529+}
28530+
28531+static int au_rdu(struct file *file, struct aufs_rdu *rdu)
28532+{
28533+ int err;
5afbbe0d 28534+ aufs_bindex_t bbot;
392086de
AM
28535+ struct au_rdu_arg arg = {
28536+ .ctx = {
2000de60 28537+ .actor = au_rdu_fill
392086de
AM
28538+ }
28539+ };
1308ab2a 28540+ struct dentry *dentry;
28541+ struct inode *inode;
28542+ struct file *h_file;
28543+ struct au_rdu_cookie *cookie = &rdu->cookie;
28544+
28545+ err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
28546+ if (unlikely(err)) {
28547+ err = -EFAULT;
28548+ AuTraceErr(err);
28549+ goto out;
28550+ }
28551+ rdu->rent = 0;
28552+ rdu->tail = rdu->ent;
28553+ rdu->full = 0;
28554+ arg.rdu = rdu;
28555+ arg.ent = rdu->ent;
28556+ arg.end = arg.ent.ul;
28557+ arg.end += rdu->sz;
28558+
28559+ err = -ENOTDIR;
5afbbe0d 28560+ if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
1308ab2a 28561+ goto out;
28562+
28563+ err = security_file_permission(file, MAY_READ);
28564+ AuTraceErr(err);
28565+ if (unlikely(err))
28566+ goto out;
28567+
2000de60 28568+ dentry = file->f_path.dentry;
5527c038 28569+ inode = d_inode(dentry);
5afbbe0d 28570+ inode_lock_shared(inode);
1308ab2a 28571+
28572+ arg.sb = inode->i_sb;
e49829fe
JR
28573+ err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
28574+ if (unlikely(err))
28575+ goto out_mtx;
027c5e7a
AM
28576+ err = au_alive_dir(dentry);
28577+ if (unlikely(err))
28578+ goto out_si;
e49829fe 28579+ /* todo: reval? */
1308ab2a 28580+ fi_read_lock(file);
28581+
28582+ err = -EAGAIN;
28583+ if (unlikely(au_ftest_rdu(cookie->flags, CONT)
28584+ && cookie->generation != au_figen(file)))
28585+ goto out_unlock;
28586+
28587+ err = 0;
28588+ if (!rdu->blk) {
28589+ rdu->blk = au_sbi(arg.sb)->si_rdblk;
28590+ if (!rdu->blk)
28591+ rdu->blk = au_dir_size(file, /*dentry*/NULL);
28592+ }
5afbbe0d
AM
28593+ bbot = au_fbtop(file);
28594+ if (cookie->bindex < bbot)
28595+ cookie->bindex = bbot;
28596+ bbot = au_fbbot_dir(file);
28597+ /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
28598+ for (; !err && cookie->bindex <= bbot;
1308ab2a 28599+ cookie->bindex++, cookie->h_pos = 0) {
4a4d8108 28600+ h_file = au_hf_dir(file, cookie->bindex);
1308ab2a 28601+ if (!h_file)
28602+ continue;
28603+
28604+ au_fclr_rdu(cookie->flags, FULL);
28605+ err = au_rdu_do(h_file, &arg);
28606+ AuTraceErr(err);
28607+ if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
28608+ break;
28609+ }
28610+ AuDbg("rent %llu\n", rdu->rent);
28611+
28612+ if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
28613+ rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
28614+ au_fset_rdu(cookie->flags, CONT);
28615+ cookie->generation = au_figen(file);
28616+ }
28617+
28618+ ii_read_lock_child(inode);
5afbbe0d 28619+ fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
1308ab2a 28620+ ii_read_unlock(inode);
28621+
4f0767ce 28622+out_unlock:
1308ab2a 28623+ fi_read_unlock(file);
027c5e7a 28624+out_si:
1308ab2a 28625+ si_read_unlock(arg.sb);
4f0767ce 28626+out_mtx:
5afbbe0d 28627+ inode_unlock_shared(inode);
4f0767ce 28628+out:
1308ab2a 28629+ AuTraceErr(err);
28630+ return err;
28631+}
28632+
28633+static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
28634+{
28635+ int err;
28636+ ino_t ino;
28637+ unsigned long long nent;
28638+ union au_rdu_ent_ul *u;
28639+ struct au_rdu_ent ent;
28640+ struct super_block *sb;
28641+
28642+ err = 0;
28643+ nent = rdu->nent;
28644+ u = &rdu->ent;
2000de60 28645+ sb = file->f_path.dentry->d_sb;
1308ab2a 28646+ si_read_lock(sb, AuLock_FLUSH);
28647+ while (nent-- > 0) {
9dbd164d 28648+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 28649+ err = copy_from_user(&ent, u->e, sizeof(ent));
4a4d8108
AM
28650+ if (!err)
28651+ err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
1308ab2a 28652+ if (unlikely(err)) {
28653+ err = -EFAULT;
28654+ AuTraceErr(err);
28655+ break;
28656+ }
28657+
28658+ /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
28659+ if (!ent.wh)
28660+ err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
28661+ else
28662+ err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
28663+ &ino);
28664+ if (unlikely(err)) {
28665+ AuTraceErr(err);
28666+ break;
28667+ }
28668+
28669+ err = __put_user(ino, &u->e->ino);
28670+ if (unlikely(err)) {
28671+ err = -EFAULT;
28672+ AuTraceErr(err);
28673+ break;
28674+ }
28675+ u->ul += au_rdu_len(ent.nlen);
28676+ }
28677+ si_read_unlock(sb);
28678+
28679+ return err;
28680+}
28681+
28682+/* ---------------------------------------------------------------------- */
28683+
28684+static int au_rdu_verify(struct aufs_rdu *rdu)
28685+{
b752ccd1 28686+ AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
1308ab2a 28687+ "%llu, b%d, 0x%x, g%u}\n",
b752ccd1 28688+ rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
1308ab2a 28689+ rdu->blk,
28690+ rdu->rent, rdu->shwh, rdu->full,
28691+ rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
28692+ rdu->cookie.generation);
dece6358 28693+
b752ccd1 28694+ if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
1308ab2a 28695+ return 0;
dece6358 28696+
b752ccd1
AM
28697+ AuDbg("%u:%u\n",
28698+ rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
1308ab2a 28699+ return -EINVAL;
28700+}
28701+
28702+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
dece6358 28703+{
1308ab2a 28704+ long err, e;
28705+ struct aufs_rdu rdu;
28706+ void __user *p = (void __user *)arg;
dece6358 28707+
1308ab2a 28708+ err = copy_from_user(&rdu, p, sizeof(rdu));
28709+ if (unlikely(err)) {
28710+ err = -EFAULT;
28711+ AuTraceErr(err);
28712+ goto out;
28713+ }
28714+ err = au_rdu_verify(&rdu);
dece6358
AM
28715+ if (unlikely(err))
28716+ goto out;
28717+
1308ab2a 28718+ switch (cmd) {
28719+ case AUFS_CTL_RDU:
28720+ err = au_rdu(file, &rdu);
28721+ if (unlikely(err))
28722+ break;
dece6358 28723+
1308ab2a 28724+ e = copy_to_user(p, &rdu, sizeof(rdu));
28725+ if (unlikely(e)) {
28726+ err = -EFAULT;
28727+ AuTraceErr(err);
28728+ }
28729+ break;
28730+ case AUFS_CTL_RDU_INO:
28731+ err = au_rdu_ino(file, &rdu);
28732+ break;
28733+
28734+ default:
4a4d8108 28735+ /* err = -ENOTTY; */
1308ab2a 28736+ err = -EINVAL;
28737+ }
dece6358 28738+
4f0767ce 28739+out:
1308ab2a 28740+ AuTraceErr(err);
28741+ return err;
1facf9fc 28742+}
b752ccd1
AM
28743+
28744+#ifdef CONFIG_COMPAT
28745+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28746+{
28747+ long err, e;
28748+ struct aufs_rdu rdu;
28749+ void __user *p = compat_ptr(arg);
28750+
28751+ /* todo: get_user()? */
28752+ err = copy_from_user(&rdu, p, sizeof(rdu));
28753+ if (unlikely(err)) {
28754+ err = -EFAULT;
28755+ AuTraceErr(err);
28756+ goto out;
28757+ }
28758+ rdu.ent.e = compat_ptr(rdu.ent.ul);
28759+ err = au_rdu_verify(&rdu);
28760+ if (unlikely(err))
28761+ goto out;
28762+
28763+ switch (cmd) {
28764+ case AUFS_CTL_RDU:
28765+ err = au_rdu(file, &rdu);
28766+ if (unlikely(err))
28767+ break;
28768+
28769+ rdu.ent.ul = ptr_to_compat(rdu.ent.e);
28770+ rdu.tail.ul = ptr_to_compat(rdu.tail.e);
28771+ e = copy_to_user(p, &rdu, sizeof(rdu));
28772+ if (unlikely(e)) {
28773+ err = -EFAULT;
28774+ AuTraceErr(err);
28775+ }
28776+ break;
28777+ case AUFS_CTL_RDU_INO:
28778+ err = au_rdu_ino(file, &rdu);
28779+ break;
28780+
28781+ default:
28782+ /* err = -ENOTTY; */
28783+ err = -EINVAL;
28784+ }
28785+
4f0767ce 28786+out:
b752ccd1
AM
28787+ AuTraceErr(err);
28788+ return err;
28789+}
28790+#endif
7f207e10
AM
28791diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
28792--- /usr/share/empty/fs/aufs/rwsem.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 28793+++ linux/fs/aufs/rwsem.h 2018-08-12 23:43:05.463458173 +0200
062440b3
AM
28794@@ -0,0 +1,73 @@
28795+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 28796+/*
b00004a5 28797+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 28798+ *
28799+ * This program, aufs is free software; you can redistribute it and/or modify
28800+ * it under the terms of the GNU General Public License as published by
28801+ * the Free Software Foundation; either version 2 of the License, or
28802+ * (at your option) any later version.
dece6358
AM
28803+ *
28804+ * This program is distributed in the hope that it will be useful,
28805+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28806+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28807+ * GNU General Public License for more details.
28808+ *
28809+ * You should have received a copy of the GNU General Public License
523b37e3 28810+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 28811+ */
28812+
28813+/*
28814+ * simple read-write semaphore wrappers
28815+ */
28816+
28817+#ifndef __AUFS_RWSEM_H__
28818+#define __AUFS_RWSEM_H__
28819+
28820+#ifdef __KERNEL__
28821+
4a4d8108 28822+#include "debug.h"
dece6358 28823+
8b6a4947
AM
28824+/* in the futre, the name 'au_rwsem' will be totally gone */
28825+#define au_rwsem rw_semaphore
dece6358
AM
28826+
28827+/* to debug easier, do not make them inlined functions */
8b6a4947 28828+#define AuRwMustNoWaiters(rw) AuDebugOn(rwsem_is_contended(rw))
dece6358 28829+/* rwsem_is_locked() is unusable */
8b6a4947
AM
28830+#define AuRwMustReadLock(rw) AuDebugOn(!lockdep_recursing(current) \
28831+ && debug_locks \
28832+ && !lockdep_is_held_type(rw, 1))
28833+#define AuRwMustWriteLock(rw) AuDebugOn(!lockdep_recursing(current) \
28834+ && debug_locks \
28835+ && !lockdep_is_held_type(rw, 0))
28836+#define AuRwMustAnyLock(rw) AuDebugOn(!lockdep_recursing(current) \
28837+ && debug_locks \
28838+ && !lockdep_is_held(rw))
28839+#define AuRwDestroy(rw) AuDebugOn(!lockdep_recursing(current) \
28840+ && debug_locks \
28841+ && lockdep_is_held(rw))
28842+
28843+#define au_rw_init(rw) init_rwsem(rw)
dece6358 28844+
5afbbe0d
AM
28845+#define au_rw_init_wlock(rw) do { \
28846+ au_rw_init(rw); \
8b6a4947 28847+ down_write(rw); \
5afbbe0d 28848+ } while (0)
dece6358 28849+
8b6a4947
AM
28850+#define au_rw_init_wlock_nested(rw, lsc) do { \
28851+ au_rw_init(rw); \
28852+ down_write_nested(rw, lsc); \
5afbbe0d 28853+ } while (0)
dece6358 28854+
8b6a4947
AM
28855+#define au_rw_read_lock(rw) down_read(rw)
28856+#define au_rw_read_lock_nested(rw, lsc) down_read_nested(rw, lsc)
28857+#define au_rw_read_unlock(rw) up_read(rw)
28858+#define au_rw_dgrade_lock(rw) downgrade_write(rw)
28859+#define au_rw_write_lock(rw) down_write(rw)
28860+#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
28861+#define au_rw_write_unlock(rw) up_write(rw)
28862+/* why is not _nested version defined? */
28863+#define au_rw_read_trylock(rw) down_read_trylock(rw)
28864+#define au_rw_write_trylock(rw) down_write_trylock(rw)
1facf9fc 28865+
28866+#endif /* __KERNEL__ */
28867+#endif /* __AUFS_RWSEM_H__ */
7f207e10
AM
28868diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
28869--- /usr/share/empty/fs/aufs/sbinfo.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 28870+++ linux/fs/aufs/sbinfo.c 2018-08-12 23:43:05.463458173 +0200
062440b3 28871@@ -0,0 +1,312 @@
cd7a4cd9 28872+// SPDX-License-Identifier: GPL-2.0
1facf9fc 28873+/*
b00004a5 28874+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 28875+ *
28876+ * This program, aufs is free software; you can redistribute it and/or modify
28877+ * it under the terms of the GNU General Public License as published by
28878+ * the Free Software Foundation; either version 2 of the License, or
28879+ * (at your option) any later version.
dece6358
AM
28880+ *
28881+ * This program is distributed in the hope that it will be useful,
28882+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28883+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28884+ * GNU General Public License for more details.
28885+ *
28886+ * You should have received a copy of the GNU General Public License
523b37e3 28887+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 28888+ */
28889+
28890+/*
28891+ * superblock private data
28892+ */
28893+
28894+#include "aufs.h"
28895+
28896+/*
28897+ * they are necessary regardless sysfs is disabled.
28898+ */
28899+void au_si_free(struct kobject *kobj)
28900+{
86dc4139 28901+ int i;
1facf9fc 28902+ struct au_sbinfo *sbinfo;
b752ccd1 28903+ char *locked __maybe_unused; /* debug only */
1facf9fc 28904+
28905+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
86dc4139 28906+ for (i = 0; i < AuPlink_NHASH; i++)
8b6a4947 28907+ AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
f0c0a007 28908+ AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
5afbbe0d
AM
28909+
28910+ AuDebugOn(percpu_counter_sum(&sbinfo->si_ninodes));
28911+ percpu_counter_destroy(&sbinfo->si_ninodes);
28912+ AuDebugOn(percpu_counter_sum(&sbinfo->si_nfiles));
28913+ percpu_counter_destroy(&sbinfo->si_nfiles);
1facf9fc 28914+
062440b3 28915+ dbgaufs_si_fin(sbinfo);
e49829fe 28916+ au_rw_write_lock(&sbinfo->si_rwsem);
1facf9fc 28917+ au_br_free(sbinfo);
e49829fe 28918+ au_rw_write_unlock(&sbinfo->si_rwsem);
b752ccd1 28919+
1c60b727 28920+ kfree(sbinfo->si_branch);
1facf9fc 28921+ mutex_destroy(&sbinfo->si_xib_mtx);
dece6358 28922+ AuRwDestroy(&sbinfo->si_rwsem);
1facf9fc 28923+
1c60b727 28924+ kfree(sbinfo);
1facf9fc 28925+}
28926+
28927+int au_si_alloc(struct super_block *sb)
28928+{
86dc4139 28929+ int err, i;
1facf9fc 28930+ struct au_sbinfo *sbinfo;
28931+
28932+ err = -ENOMEM;
4a4d8108 28933+ sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
1facf9fc 28934+ if (unlikely(!sbinfo))
28935+ goto out;
28936+
28937+ /* will be reallocated separately */
28938+ sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
28939+ if (unlikely(!sbinfo->si_branch))
febd17d6 28940+ goto out_sbinfo;
1facf9fc 28941+
1facf9fc 28942+ err = sysaufs_si_init(sbinfo);
062440b3
AM
28943+ if (!err) {
28944+ dbgaufs_si_null(sbinfo);
28945+ err = dbgaufs_si_init(sbinfo);
28946+ if (unlikely(err))
28947+ kobject_put(&sbinfo->si_kobj);
28948+ }
1facf9fc 28949+ if (unlikely(err))
28950+ goto out_br;
28951+
28952+ au_nwt_init(&sbinfo->si_nowait);
dece6358 28953+ au_rw_init_wlock(&sbinfo->si_rwsem);
b752ccd1 28954+
5afbbe0d
AM
28955+ percpu_counter_init(&sbinfo->si_ninodes, 0, GFP_NOFS);
28956+ percpu_counter_init(&sbinfo->si_nfiles, 0, GFP_NOFS);
7f207e10 28957+
5afbbe0d 28958+ sbinfo->si_bbot = -1;
392086de 28959+ sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
1facf9fc 28960+
28961+ sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
28962+ sbinfo->si_wbr_create = AuWbrCreate_Def;
4a4d8108
AM
28963+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
28964+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
1facf9fc 28965+
076b876e
AM
28966+ au_fhsm_init(sbinfo);
28967+
e49829fe 28968+ sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
1facf9fc 28969+
392086de
AM
28970+ sbinfo->si_xino_jiffy = jiffies;
28971+ sbinfo->si_xino_expire
28972+ = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
1facf9fc 28973+ mutex_init(&sbinfo->si_xib_mtx);
1facf9fc 28974+ sbinfo->si_xino_brid = -1;
28975+ /* leave si_xib_last_pindex and si_xib_next_bit */
28976+
8b6a4947 28977+ INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
b912730e 28978+
e49829fe 28979+ sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
1facf9fc 28980+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
28981+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
28982+ sbinfo->si_dirwh = AUFS_DIRWH_DEF;
28983+
86dc4139 28984+ for (i = 0; i < AuPlink_NHASH; i++)
8b6a4947 28985+ INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
1facf9fc 28986+ init_waitqueue_head(&sbinfo->si_plink_wq);
4a4d8108 28987+ spin_lock_init(&sbinfo->si_plink_maint_lock);
1facf9fc 28988+
8b6a4947 28989+ INIT_HLIST_BL_HEAD(&sbinfo->si_files);
523b37e3 28990+
b95c5147
AM
28991+ /* with getattr by default */
28992+ sbinfo->si_iop_array = aufs_iop;
28993+
1facf9fc 28994+ /* leave other members for sysaufs and si_mnt. */
28995+ sbinfo->si_sb = sb;
28996+ sb->s_fs_info = sbinfo;
b752ccd1 28997+ si_pid_set(sb);
1facf9fc 28998+ return 0; /* success */
28999+
4f0767ce 29000+out_br:
1c60b727 29001+ kfree(sbinfo->si_branch);
4f0767ce 29002+out_sbinfo:
1c60b727 29003+ kfree(sbinfo);
4f0767ce 29004+out:
1facf9fc 29005+ return err;
29006+}
29007+
e2f27e51 29008+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
1facf9fc 29009+{
29010+ int err, sz;
29011+ struct au_branch **brp;
29012+
dece6358
AM
29013+ AuRwMustWriteLock(&sbinfo->si_rwsem);
29014+
1facf9fc 29015+ err = -ENOMEM;
5afbbe0d 29016+ sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
1facf9fc 29017+ if (unlikely(!sz))
29018+ sz = sizeof(*brp);
e2f27e51
AM
29019+ brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29020+ may_shrink);
1facf9fc 29021+ if (brp) {
29022+ sbinfo->si_branch = brp;
29023+ err = 0;
29024+ }
29025+
29026+ return err;
29027+}
29028+
29029+/* ---------------------------------------------------------------------- */
29030+
29031+unsigned int au_sigen_inc(struct super_block *sb)
29032+{
29033+ unsigned int gen;
5527c038 29034+ struct inode *inode;
1facf9fc 29035+
dece6358
AM
29036+ SiMustWriteLock(sb);
29037+
1facf9fc 29038+ gen = ++au_sbi(sb)->si_generation;
29039+ au_update_digen(sb->s_root);
5527c038
JR
29040+ inode = d_inode(sb->s_root);
29041+ au_update_iigen(inode, /*half*/0);
be118d29 29042+ inode_inc_iversion(inode);
1facf9fc 29043+ return gen;
29044+}
29045+
29046+aufs_bindex_t au_new_br_id(struct super_block *sb)
29047+{
29048+ aufs_bindex_t br_id;
29049+ int i;
29050+ struct au_sbinfo *sbinfo;
29051+
dece6358
AM
29052+ SiMustWriteLock(sb);
29053+
1facf9fc 29054+ sbinfo = au_sbi(sb);
29055+ for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29056+ br_id = ++sbinfo->si_last_br_id;
7f207e10 29057+ AuDebugOn(br_id < 0);
1facf9fc 29058+ if (br_id && au_br_index(sb, br_id) < 0)
29059+ return br_id;
29060+ }
29061+
29062+ return -1;
29063+}
29064+
29065+/* ---------------------------------------------------------------------- */
29066+
e49829fe
JR
29067+/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29068+int si_read_lock(struct super_block *sb, int flags)
29069+{
29070+ int err;
29071+
29072+ err = 0;
29073+ if (au_ftest_lock(flags, FLUSH))
29074+ au_nwt_flush(&au_sbi(sb)->si_nowait);
29075+
29076+ si_noflush_read_lock(sb);
29077+ err = au_plink_maint(sb, flags);
29078+ if (unlikely(err))
29079+ si_read_unlock(sb);
29080+
29081+ return err;
29082+}
29083+
29084+int si_write_lock(struct super_block *sb, int flags)
29085+{
29086+ int err;
29087+
29088+ if (au_ftest_lock(flags, FLUSH))
29089+ au_nwt_flush(&au_sbi(sb)->si_nowait);
29090+
29091+ si_noflush_write_lock(sb);
29092+ err = au_plink_maint(sb, flags);
29093+ if (unlikely(err))
29094+ si_write_unlock(sb);
29095+
29096+ return err;
29097+}
29098+
1facf9fc 29099+/* dentry and super_block lock. call at entry point */
e49829fe 29100+int aufs_read_lock(struct dentry *dentry, int flags)
1facf9fc 29101+{
e49829fe 29102+ int err;
027c5e7a 29103+ struct super_block *sb;
e49829fe 29104+
027c5e7a
AM
29105+ sb = dentry->d_sb;
29106+ err = si_read_lock(sb, flags);
29107+ if (unlikely(err))
29108+ goto out;
29109+
29110+ if (au_ftest_lock(flags, DW))
29111+ di_write_lock_child(dentry);
29112+ else
29113+ di_read_lock_child(dentry, flags);
29114+
29115+ if (au_ftest_lock(flags, GEN)) {
29116+ err = au_digen_test(dentry, au_sigen(sb));
79b8bda9
AM
29117+ if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29118+ AuDebugOn(!err && au_dbrange_test(dentry));
29119+ else if (!err)
29120+ err = au_dbrange_test(dentry);
027c5e7a
AM
29121+ if (unlikely(err))
29122+ aufs_read_unlock(dentry, flags);
e49829fe
JR
29123+ }
29124+
027c5e7a 29125+out:
e49829fe 29126+ return err;
1facf9fc 29127+}
29128+
29129+void aufs_read_unlock(struct dentry *dentry, int flags)
29130+{
29131+ if (au_ftest_lock(flags, DW))
29132+ di_write_unlock(dentry);
29133+ else
29134+ di_read_unlock(dentry, flags);
29135+ si_read_unlock(dentry->d_sb);
29136+}
29137+
29138+void aufs_write_lock(struct dentry *dentry)
29139+{
e49829fe 29140+ si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
1facf9fc 29141+ di_write_lock_child(dentry);
29142+}
29143+
29144+void aufs_write_unlock(struct dentry *dentry)
29145+{
29146+ di_write_unlock(dentry);
29147+ si_write_unlock(dentry->d_sb);
29148+}
29149+
e49829fe 29150+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
1facf9fc 29151+{
e49829fe 29152+ int err;
027c5e7a
AM
29153+ unsigned int sigen;
29154+ struct super_block *sb;
e49829fe 29155+
027c5e7a
AM
29156+ sb = d1->d_sb;
29157+ err = si_read_lock(sb, flags);
29158+ if (unlikely(err))
29159+ goto out;
29160+
b95c5147 29161+ di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
027c5e7a
AM
29162+
29163+ if (au_ftest_lock(flags, GEN)) {
29164+ sigen = au_sigen(sb);
29165+ err = au_digen_test(d1, sigen);
29166+ AuDebugOn(!err && au_dbrange_test(d1));
29167+ if (!err) {
29168+ err = au_digen_test(d2, sigen);
29169+ AuDebugOn(!err && au_dbrange_test(d2));
29170+ }
29171+ if (unlikely(err))
29172+ aufs_read_and_write_unlock2(d1, d2);
29173+ }
29174+
29175+out:
e49829fe 29176+ return err;
1facf9fc 29177+}
29178+
29179+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29180+{
29181+ di_write_unlock2(d1, d2);
29182+ si_read_unlock(d1->d_sb);
29183+}
7f207e10
AM
29184diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29185--- /usr/share/empty/fs/aufs/super.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 29186+++ linux/fs/aufs/super.c 2018-08-12 23:43:05.463458173 +0200
062440b3 29187@@ -0,0 +1,1043 @@
cd7a4cd9 29188+// SPDX-License-Identifier: GPL-2.0
1facf9fc 29189+/*
b00004a5 29190+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 29191+ *
29192+ * This program, aufs is free software; you can redistribute it and/or modify
29193+ * it under the terms of the GNU General Public License as published by
29194+ * the Free Software Foundation; either version 2 of the License, or
29195+ * (at your option) any later version.
dece6358
AM
29196+ *
29197+ * This program is distributed in the hope that it will be useful,
29198+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29199+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29200+ * GNU General Public License for more details.
29201+ *
29202+ * You should have received a copy of the GNU General Public License
523b37e3 29203+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29204+ */
29205+
29206+/*
29207+ * mount and super_block operations
29208+ */
29209+
f6c5ef8b 29210+#include <linux/mm.h>
1facf9fc 29211+#include <linux/seq_file.h>
29212+#include <linux/statfs.h>
7f207e10 29213+#include <linux/vmalloc.h>
1facf9fc 29214+#include "aufs.h"
29215+
29216+/*
29217+ * super_operations
29218+ */
29219+static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29220+{
29221+ struct au_icntnr *c;
29222+
29223+ c = au_cache_alloc_icntnr();
29224+ if (c) {
027c5e7a 29225+ au_icntnr_init(c);
be118d29 29226+ inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
1facf9fc 29227+ c->iinfo.ii_hinode = NULL;
29228+ return &c->vfs_inode;
29229+ }
29230+ return NULL;
29231+}
29232+
027c5e7a
AM
29233+static void aufs_destroy_inode_cb(struct rcu_head *head)
29234+{
29235+ struct inode *inode = container_of(head, struct inode, i_rcu);
29236+
1c60b727 29237+ au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
027c5e7a
AM
29238+}
29239+
1facf9fc 29240+static void aufs_destroy_inode(struct inode *inode)
29241+{
5afbbe0d
AM
29242+ if (!au_is_bad_inode(inode))
29243+ au_iinfo_fin(inode);
027c5e7a 29244+ call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
1facf9fc 29245+}
29246+
29247+struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29248+{
29249+ struct inode *inode;
29250+ int err;
29251+
29252+ inode = iget_locked(sb, ino);
29253+ if (unlikely(!inode)) {
29254+ inode = ERR_PTR(-ENOMEM);
29255+ goto out;
29256+ }
29257+ if (!(inode->i_state & I_NEW))
29258+ goto out;
29259+
29260+ err = au_xigen_new(inode);
29261+ if (!err)
29262+ err = au_iinfo_init(inode);
29263+ if (!err)
be118d29 29264+ inode_inc_iversion(inode);
1facf9fc 29265+ else {
29266+ iget_failed(inode);
29267+ inode = ERR_PTR(err);
29268+ }
29269+
4f0767ce 29270+out:
1facf9fc 29271+ /* never return NULL */
29272+ AuDebugOn(!inode);
29273+ AuTraceErrPtr(inode);
29274+ return inode;
29275+}
29276+
29277+/* lock free root dinfo */
29278+static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29279+{
29280+ int err;
5afbbe0d 29281+ aufs_bindex_t bindex, bbot;
1facf9fc 29282+ struct path path;
4a4d8108 29283+ struct au_hdentry *hdp;
1facf9fc 29284+ struct au_branch *br;
076b876e 29285+ au_br_perm_str_t perm;
1facf9fc 29286+
29287+ err = 0;
5afbbe0d
AM
29288+ bbot = au_sbbot(sb);
29289+ bindex = 0;
29290+ hdp = au_hdentry(au_di(sb->s_root), bindex);
29291+ for (; !err && bindex <= bbot; bindex++, hdp++) {
1facf9fc 29292+ br = au_sbr(sb, bindex);
86dc4139 29293+ path.mnt = au_br_mnt(br);
5afbbe0d 29294+ path.dentry = hdp->hd_dentry;
1facf9fc 29295+ err = au_seq_path(seq, &path);
79b8bda9 29296+ if (!err) {
076b876e 29297+ au_optstr_br_perm(&perm, br->br_perm);
79b8bda9 29298+ seq_printf(seq, "=%s", perm.a);
5afbbe0d 29299+ if (bindex != bbot)
79b8bda9 29300+ seq_putc(seq, ':');
1e00d052 29301+ }
1facf9fc 29302+ }
79b8bda9
AM
29303+ if (unlikely(err || seq_has_overflowed(seq)))
29304+ err = -E2BIG;
1facf9fc 29305+
29306+ return err;
29307+}
29308+
f2c43d5f
AM
29309+static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29310+ const char *append)
29311+{
29312+ char *p;
29313+
29314+ p = fmt;
29315+ while (*pat != ':')
29316+ *p++ = *pat++;
29317+ *p++ = *pat++;
29318+ strcpy(p, append);
29319+ AuDebugOn(strlen(fmt) >= len);
29320+}
29321+
1facf9fc 29322+static void au_show_wbr_create(struct seq_file *m, int v,
29323+ struct au_sbinfo *sbinfo)
29324+{
29325+ const char *pat;
f2c43d5f
AM
29326+ char fmt[32];
29327+ struct au_wbr_mfs *mfs;
1facf9fc 29328+
dece6358
AM
29329+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29330+
c2b27bf2 29331+ seq_puts(m, ",create=");
1facf9fc 29332+ pat = au_optstr_wbr_create(v);
f2c43d5f 29333+ mfs = &sbinfo->si_wbr_mfs;
1facf9fc 29334+ switch (v) {
29335+ case AuWbrCreate_TDP:
29336+ case AuWbrCreate_RR:
29337+ case AuWbrCreate_MFS:
29338+ case AuWbrCreate_PMFS:
c2b27bf2 29339+ seq_puts(m, pat);
1facf9fc 29340+ break;
f2c43d5f
AM
29341+ case AuWbrCreate_MFSRR:
29342+ case AuWbrCreate_TDMFS:
29343+ case AuWbrCreate_PMFSRR:
29344+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29345+ seq_printf(m, fmt, mfs->mfsrr_watermark);
1facf9fc 29346+ break;
f2c43d5f 29347+ case AuWbrCreate_MFSV:
1facf9fc 29348+ case AuWbrCreate_PMFSV:
f2c43d5f
AM
29349+ au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29350+ seq_printf(m, fmt,
29351+ jiffies_to_msecs(mfs->mfs_expire)
e49829fe 29352+ / MSEC_PER_SEC);
1facf9fc 29353+ break;
1facf9fc 29354+ case AuWbrCreate_MFSRRV:
f2c43d5f 29355+ case AuWbrCreate_TDMFSV:
392086de 29356+ case AuWbrCreate_PMFSRRV:
f2c43d5f
AM
29357+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29358+ seq_printf(m, fmt, mfs->mfsrr_watermark,
29359+ jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
392086de 29360+ break;
f2c43d5f
AM
29361+ default:
29362+ BUG();
1facf9fc 29363+ }
29364+}
29365+
7eafdf33 29366+static int au_show_xino(struct seq_file *seq, struct super_block *sb)
1facf9fc 29367+{
29368+#ifdef CONFIG_SYSFS
29369+ return 0;
29370+#else
29371+ int err;
29372+ const int len = sizeof(AUFS_XINO_FNAME) - 1;
29373+ aufs_bindex_t bindex, brid;
1facf9fc 29374+ struct qstr *name;
29375+ struct file *f;
29376+ struct dentry *d, *h_root;
29377+
dece6358
AM
29378+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29379+
1facf9fc 29380+ err = 0;
1facf9fc 29381+ f = au_sbi(sb)->si_xib;
29382+ if (!f)
29383+ goto out;
29384+
29385+ /* stop printing the default xino path on the first writable branch */
29386+ h_root = NULL;
29387+ brid = au_xino_brid(sb);
29388+ if (brid >= 0) {
29389+ bindex = au_br_index(sb, brid);
5afbbe0d 29390+ h_root = au_hdentry(au_di(sb->s_root), bindex)->hd_dentry;
1facf9fc 29391+ }
2000de60 29392+ d = f->f_path.dentry;
1facf9fc 29393+ name = &d->d_name;
29394+ /* safe ->d_parent because the file is unlinked */
29395+ if (d->d_parent == h_root
29396+ && name->len == len
29397+ && !memcmp(name->name, AUFS_XINO_FNAME, len))
29398+ goto out;
29399+
29400+ seq_puts(seq, ",xino=");
29401+ err = au_xino_path(seq, f);
29402+
4f0767ce 29403+out:
1facf9fc 29404+ return err;
29405+#endif
29406+}
29407+
29408+/* seq_file will re-call me in case of too long string */
7eafdf33 29409+static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
1facf9fc 29410+{
027c5e7a 29411+ int err;
1facf9fc 29412+ unsigned int mnt_flags, v;
29413+ struct super_block *sb;
29414+ struct au_sbinfo *sbinfo;
29415+
29416+#define AuBool(name, str) do { \
29417+ v = au_opt_test(mnt_flags, name); \
29418+ if (v != au_opt_test(AuOpt_Def, name)) \
29419+ seq_printf(m, ",%s" #str, v ? "" : "no"); \
29420+} while (0)
29421+
29422+#define AuStr(name, str) do { \
29423+ v = mnt_flags & AuOptMask_##name; \
29424+ if (v != (AuOpt_Def & AuOptMask_##name)) \
29425+ seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
29426+} while (0)
29427+
29428+#define AuUInt(name, str, val) do { \
29429+ if (val != AUFS_##name##_DEF) \
29430+ seq_printf(m, "," #str "=%u", val); \
29431+} while (0)
29432+
7eafdf33 29433+ sb = dentry->d_sb;
2121bcd9 29434+ if (sb->s_flags & SB_POSIXACL)
c1595e42 29435+ seq_puts(m, ",acl");
be118d29
JR
29436+#if 0
29437+ if (sb->s_flags & SB_I_VERSION)
29438+ seq_puts(m, ",i_version");
29439+#endif
c1595e42
JR
29440+
29441+ /* lock free root dinfo */
1facf9fc 29442+ si_noflush_read_lock(sb);
29443+ sbinfo = au_sbi(sb);
29444+ seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
29445+
29446+ mnt_flags = au_mntflags(sb);
29447+ if (au_opt_test(mnt_flags, XINO)) {
7eafdf33 29448+ err = au_show_xino(m, sb);
1facf9fc 29449+ if (unlikely(err))
29450+ goto out;
29451+ } else
29452+ seq_puts(m, ",noxino");
29453+
29454+ AuBool(TRUNC_XINO, trunc_xino);
29455+ AuStr(UDBA, udba);
dece6358 29456+ AuBool(SHWH, shwh);
1facf9fc 29457+ AuBool(PLINK, plink);
4a4d8108 29458+ AuBool(DIO, dio);
076b876e 29459+ AuBool(DIRPERM1, dirperm1);
1facf9fc 29460+
29461+ v = sbinfo->si_wbr_create;
29462+ if (v != AuWbrCreate_Def)
29463+ au_show_wbr_create(m, v, sbinfo);
29464+
29465+ v = sbinfo->si_wbr_copyup;
29466+ if (v != AuWbrCopyup_Def)
29467+ seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
29468+
29469+ v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
29470+ if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
29471+ seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
29472+
29473+ AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
29474+
027c5e7a
AM
29475+ v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
29476+ AuUInt(RDCACHE, rdcache, v);
1facf9fc 29477+
29478+ AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
29479+ AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
29480+
076b876e
AM
29481+ au_fhsm_show(m, sbinfo);
29482+
8b6a4947 29483+ AuBool(DIRREN, dirren);
1facf9fc 29484+ AuBool(SUM, sum);
29485+ /* AuBool(SUM_W, wsum); */
29486+ AuBool(WARN_PERM, warn_perm);
29487+ AuBool(VERBOSE, verbose);
29488+
4f0767ce 29489+out:
1facf9fc 29490+ /* be sure to print "br:" last */
29491+ if (!sysaufs_brs) {
29492+ seq_puts(m, ",br:");
29493+ au_show_brs(m, sb);
29494+ }
29495+ si_read_unlock(sb);
29496+ return 0;
29497+
1facf9fc 29498+#undef AuBool
29499+#undef AuStr
4a4d8108 29500+#undef AuUInt
1facf9fc 29501+}
29502+
29503+/* ---------------------------------------------------------------------- */
29504+
29505+/* sum mode which returns the summation for statfs(2) */
29506+
29507+static u64 au_add_till_max(u64 a, u64 b)
29508+{
29509+ u64 old;
29510+
29511+ old = a;
29512+ a += b;
92d182d2
AM
29513+ if (old <= a)
29514+ return a;
29515+ return ULLONG_MAX;
29516+}
29517+
29518+static u64 au_mul_till_max(u64 a, long mul)
29519+{
29520+ u64 old;
29521+
29522+ old = a;
29523+ a *= mul;
29524+ if (old <= a)
1facf9fc 29525+ return a;
29526+ return ULLONG_MAX;
29527+}
29528+
29529+static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
29530+{
29531+ int err;
92d182d2 29532+ long bsize, factor;
1facf9fc 29533+ u64 blocks, bfree, bavail, files, ffree;
5afbbe0d 29534+ aufs_bindex_t bbot, bindex, i;
1facf9fc 29535+ unsigned char shared;
7f207e10 29536+ struct path h_path;
1facf9fc 29537+ struct super_block *h_sb;
29538+
92d182d2
AM
29539+ err = 0;
29540+ bsize = LONG_MAX;
29541+ files = 0;
29542+ ffree = 0;
1facf9fc 29543+ blocks = 0;
29544+ bfree = 0;
29545+ bavail = 0;
5afbbe0d
AM
29546+ bbot = au_sbbot(sb);
29547+ for (bindex = 0; bindex <= bbot; bindex++) {
7f207e10
AM
29548+ h_path.mnt = au_sbr_mnt(sb, bindex);
29549+ h_sb = h_path.mnt->mnt_sb;
1facf9fc 29550+ shared = 0;
92d182d2 29551+ for (i = 0; !shared && i < bindex; i++)
1facf9fc 29552+ shared = (au_sbr_sb(sb, i) == h_sb);
29553+ if (shared)
29554+ continue;
29555+
29556+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
29557+ h_path.dentry = h_path.mnt->mnt_root;
29558+ err = vfs_statfs(&h_path, buf);
1facf9fc 29559+ if (unlikely(err))
29560+ goto out;
29561+
92d182d2
AM
29562+ if (bsize > buf->f_bsize) {
29563+ /*
29564+ * we will reduce bsize, so we have to expand blocks
29565+ * etc. to match them again
29566+ */
29567+ factor = (bsize / buf->f_bsize);
29568+ blocks = au_mul_till_max(blocks, factor);
29569+ bfree = au_mul_till_max(bfree, factor);
29570+ bavail = au_mul_till_max(bavail, factor);
29571+ bsize = buf->f_bsize;
29572+ }
29573+
29574+ factor = (buf->f_bsize / bsize);
29575+ blocks = au_add_till_max(blocks,
29576+ au_mul_till_max(buf->f_blocks, factor));
29577+ bfree = au_add_till_max(bfree,
29578+ au_mul_till_max(buf->f_bfree, factor));
29579+ bavail = au_add_till_max(bavail,
29580+ au_mul_till_max(buf->f_bavail, factor));
1facf9fc 29581+ files = au_add_till_max(files, buf->f_files);
29582+ ffree = au_add_till_max(ffree, buf->f_ffree);
29583+ }
29584+
92d182d2 29585+ buf->f_bsize = bsize;
1facf9fc 29586+ buf->f_blocks = blocks;
29587+ buf->f_bfree = bfree;
29588+ buf->f_bavail = bavail;
29589+ buf->f_files = files;
29590+ buf->f_ffree = ffree;
92d182d2 29591+ buf->f_frsize = 0;
1facf9fc 29592+
4f0767ce 29593+out:
1facf9fc 29594+ return err;
29595+}
29596+
29597+static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
29598+{
29599+ int err;
7f207e10 29600+ struct path h_path;
1facf9fc 29601+ struct super_block *sb;
29602+
29603+ /* lock free root dinfo */
29604+ sb = dentry->d_sb;
29605+ si_noflush_read_lock(sb);
7f207e10 29606+ if (!au_opt_test(au_mntflags(sb), SUM)) {
1facf9fc 29607+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
29608+ h_path.mnt = au_sbr_mnt(sb, 0);
29609+ h_path.dentry = h_path.mnt->mnt_root;
29610+ err = vfs_statfs(&h_path, buf);
29611+ } else
1facf9fc 29612+ err = au_statfs_sum(sb, buf);
29613+ si_read_unlock(sb);
29614+
29615+ if (!err) {
29616+ buf->f_type = AUFS_SUPER_MAGIC;
4a4d8108 29617+ buf->f_namelen = AUFS_MAX_NAMELEN;
1facf9fc 29618+ memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
29619+ }
29620+ /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
29621+
29622+ return err;
29623+}
29624+
29625+/* ---------------------------------------------------------------------- */
29626+
537831f9
AM
29627+static int aufs_sync_fs(struct super_block *sb, int wait)
29628+{
29629+ int err, e;
5afbbe0d 29630+ aufs_bindex_t bbot, bindex;
537831f9
AM
29631+ struct au_branch *br;
29632+ struct super_block *h_sb;
29633+
29634+ err = 0;
29635+ si_noflush_read_lock(sb);
5afbbe0d
AM
29636+ bbot = au_sbbot(sb);
29637+ for (bindex = 0; bindex <= bbot; bindex++) {
537831f9
AM
29638+ br = au_sbr(sb, bindex);
29639+ if (!au_br_writable(br->br_perm))
29640+ continue;
29641+
29642+ h_sb = au_sbr_sb(sb, bindex);
a2654f78
AM
29643+ e = vfsub_sync_filesystem(h_sb, wait);
29644+ if (unlikely(e && !err))
29645+ err = e;
29646+ /* go on even if an error happens */
537831f9
AM
29647+ }
29648+ si_read_unlock(sb);
29649+
29650+ return err;
29651+}
29652+
29653+/* ---------------------------------------------------------------------- */
29654+
1facf9fc 29655+/* final actions when unmounting a file system */
29656+static void aufs_put_super(struct super_block *sb)
29657+{
29658+ struct au_sbinfo *sbinfo;
29659+
29660+ sbinfo = au_sbi(sb);
062440b3
AM
29661+ if (sbinfo)
29662+ kobject_put(&sbinfo->si_kobj);
1facf9fc 29663+}
29664+
29665+/* ---------------------------------------------------------------------- */
29666+
79b8bda9
AM
29667+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
29668+ struct super_block *sb, void *arg)
7f207e10
AM
29669+{
29670+ void *array;
076b876e 29671+ unsigned long long n, sz;
7f207e10
AM
29672+
29673+ array = NULL;
29674+ n = 0;
29675+ if (!*hint)
29676+ goto out;
29677+
29678+ if (*hint > ULLONG_MAX / sizeof(array)) {
29679+ array = ERR_PTR(-EMFILE);
29680+ pr_err("hint %llu\n", *hint);
29681+ goto out;
29682+ }
29683+
076b876e
AM
29684+ sz = sizeof(array) * *hint;
29685+ array = kzalloc(sz, GFP_NOFS);
7f207e10 29686+ if (unlikely(!array))
076b876e 29687+ array = vzalloc(sz);
7f207e10
AM
29688+ if (unlikely(!array)) {
29689+ array = ERR_PTR(-ENOMEM);
29690+ goto out;
29691+ }
29692+
79b8bda9 29693+ n = cb(sb, array, *hint, arg);
7f207e10
AM
29694+ AuDebugOn(n > *hint);
29695+
29696+out:
29697+ *hint = n;
29698+ return array;
29699+}
29700+
79b8bda9 29701+static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
7f207e10
AM
29702+ unsigned long long max __maybe_unused,
29703+ void *arg)
29704+{
29705+ unsigned long long n;
29706+ struct inode **p, *inode;
29707+ struct list_head *head;
29708+
29709+ n = 0;
29710+ p = a;
29711+ head = arg;
79b8bda9 29712+ spin_lock(&sb->s_inode_list_lock);
7f207e10 29713+ list_for_each_entry(inode, head, i_sb_list) {
5afbbe0d
AM
29714+ if (!au_is_bad_inode(inode)
29715+ && au_ii(inode)->ii_btop >= 0) {
2cbb1c4b
JR
29716+ spin_lock(&inode->i_lock);
29717+ if (atomic_read(&inode->i_count)) {
29718+ au_igrab(inode);
29719+ *p++ = inode;
29720+ n++;
29721+ AuDebugOn(n > max);
29722+ }
29723+ spin_unlock(&inode->i_lock);
7f207e10
AM
29724+ }
29725+ }
79b8bda9 29726+ spin_unlock(&sb->s_inode_list_lock);
7f207e10
AM
29727+
29728+ return n;
29729+}
29730+
29731+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
29732+{
5afbbe0d 29733+ *max = au_ninodes(sb);
79b8bda9 29734+ return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
7f207e10
AM
29735+}
29736+
29737+void au_iarray_free(struct inode **a, unsigned long long max)
29738+{
29739+ unsigned long long ull;
29740+
29741+ for (ull = 0; ull < max; ull++)
29742+ iput(a[ull]);
be52b249 29743+ kvfree(a);
7f207e10
AM
29744+}
29745+
29746+/* ---------------------------------------------------------------------- */
29747+
1facf9fc 29748+/*
29749+ * refresh dentry and inode at remount time.
29750+ */
027c5e7a
AM
29751+/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
29752+static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
29753+ struct dentry *parent)
1facf9fc 29754+{
29755+ int err;
1facf9fc 29756+
29757+ di_write_lock_child(dentry);
1facf9fc 29758+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
29759+ err = au_refresh_dentry(dentry, parent);
29760+ if (!err && dir_flags)
5527c038 29761+ au_hn_reset(d_inode(dentry), dir_flags);
1facf9fc 29762+ di_read_unlock(parent, AuLock_IR);
1facf9fc 29763+ di_write_unlock(dentry);
29764+
29765+ return err;
29766+}
29767+
027c5e7a
AM
29768+static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
29769+ struct au_sbinfo *sbinfo,
b95c5147 29770+ const unsigned int dir_flags, unsigned int do_idop)
1facf9fc 29771+{
027c5e7a
AM
29772+ int err;
29773+ struct dentry *parent;
027c5e7a
AM
29774+
29775+ err = 0;
29776+ parent = dget_parent(dentry);
29777+ if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
5527c038
JR
29778+ if (d_really_is_positive(dentry)) {
29779+ if (!d_is_dir(dentry))
027c5e7a
AM
29780+ err = au_do_refresh(dentry, /*dir_flags*/0,
29781+ parent);
29782+ else {
29783+ err = au_do_refresh(dentry, dir_flags, parent);
29784+ if (unlikely(err))
29785+ au_fset_si(sbinfo, FAILED_REFRESH_DIR);
29786+ }
29787+ } else
29788+ err = au_do_refresh(dentry, /*dir_flags*/0, parent);
29789+ AuDbgDentry(dentry);
29790+ }
29791+ dput(parent);
29792+
79b8bda9 29793+ if (!err) {
b95c5147 29794+ if (do_idop)
79b8bda9
AM
29795+ au_refresh_dop(dentry, /*force_reval*/0);
29796+ } else
29797+ au_refresh_dop(dentry, /*force_reval*/1);
29798+
027c5e7a
AM
29799+ AuTraceErr(err);
29800+ return err;
1facf9fc 29801+}
29802+
b95c5147 29803+static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
1facf9fc 29804+{
29805+ int err, i, j, ndentry, e;
027c5e7a 29806+ unsigned int sigen;
1facf9fc 29807+ struct au_dcsub_pages dpages;
29808+ struct au_dpage *dpage;
027c5e7a
AM
29809+ struct dentry **dentries, *d;
29810+ struct au_sbinfo *sbinfo;
29811+ struct dentry *root = sb->s_root;
5527c038 29812+ const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
1facf9fc 29813+
b95c5147 29814+ if (do_idop)
79b8bda9
AM
29815+ au_refresh_dop(root, /*force_reval*/0);
29816+
027c5e7a
AM
29817+ err = au_dpages_init(&dpages, GFP_NOFS);
29818+ if (unlikely(err))
1facf9fc 29819+ goto out;
027c5e7a
AM
29820+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
29821+ if (unlikely(err))
1facf9fc 29822+ goto out_dpages;
1facf9fc 29823+
027c5e7a
AM
29824+ sigen = au_sigen(sb);
29825+ sbinfo = au_sbi(sb);
29826+ for (i = 0; i < dpages.ndpage; i++) {
1facf9fc 29827+ dpage = dpages.dpages + i;
29828+ dentries = dpage->dentries;
29829+ ndentry = dpage->ndentry;
027c5e7a 29830+ for (j = 0; j < ndentry; j++) {
1facf9fc 29831+ d = dentries[j];
79b8bda9 29832+ e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
b95c5147 29833+ do_idop);
027c5e7a
AM
29834+ if (unlikely(e && !err))
29835+ err = e;
29836+ /* go on even err */
1facf9fc 29837+ }
29838+ }
29839+
4f0767ce 29840+out_dpages:
1facf9fc 29841+ au_dpages_free(&dpages);
4f0767ce 29842+out:
1facf9fc 29843+ return err;
29844+}
29845+
b95c5147 29846+static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
1facf9fc 29847+{
027c5e7a
AM
29848+ int err, e;
29849+ unsigned int sigen;
29850+ unsigned long long max, ull;
29851+ struct inode *inode, **array;
1facf9fc 29852+
027c5e7a
AM
29853+ array = au_iarray_alloc(sb, &max);
29854+ err = PTR_ERR(array);
29855+ if (IS_ERR(array))
29856+ goto out;
1facf9fc 29857+
29858+ err = 0;
027c5e7a
AM
29859+ sigen = au_sigen(sb);
29860+ for (ull = 0; ull < max; ull++) {
29861+ inode = array[ull];
076b876e
AM
29862+ if (unlikely(!inode))
29863+ break;
b95c5147
AM
29864+
29865+ e = 0;
29866+ ii_write_lock_child(inode);
537831f9 29867+ if (au_iigen(inode, NULL) != sigen) {
027c5e7a 29868+ e = au_refresh_hinode_self(inode);
1facf9fc 29869+ if (unlikely(e)) {
b95c5147 29870+ au_refresh_iop(inode, /*force_getattr*/1);
027c5e7a 29871+ pr_err("error %d, i%lu\n", e, inode->i_ino);
1facf9fc 29872+ if (!err)
29873+ err = e;
29874+ /* go on even if err */
29875+ }
29876+ }
b95c5147
AM
29877+ if (!e && do_idop)
29878+ au_refresh_iop(inode, /*force_getattr*/0);
29879+ ii_write_unlock(inode);
1facf9fc 29880+ }
29881+
027c5e7a 29882+ au_iarray_free(array, max);
1facf9fc 29883+
4f0767ce 29884+out:
1facf9fc 29885+ return err;
29886+}
29887+
b95c5147 29888+static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
1facf9fc 29889+{
027c5e7a
AM
29890+ int err, e;
29891+ unsigned int udba;
5afbbe0d 29892+ aufs_bindex_t bindex, bbot;
1facf9fc 29893+ struct dentry *root;
29894+ struct inode *inode;
027c5e7a 29895+ struct au_branch *br;
79b8bda9 29896+ struct au_sbinfo *sbi;
1facf9fc 29897+
29898+ au_sigen_inc(sb);
79b8bda9
AM
29899+ sbi = au_sbi(sb);
29900+ au_fclr_si(sbi, FAILED_REFRESH_DIR);
1facf9fc 29901+
29902+ root = sb->s_root;
29903+ DiMustNoWaiters(root);
5527c038 29904+ inode = d_inode(root);
1facf9fc 29905+ IiMustNoWaiters(inode);
1facf9fc 29906+
027c5e7a 29907+ udba = au_opt_udba(sb);
5afbbe0d
AM
29908+ bbot = au_sbbot(sb);
29909+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
29910+ br = au_sbr(sb, bindex);
29911+ err = au_hnotify_reset_br(udba, br, br->br_perm);
1facf9fc 29912+ if (unlikely(err))
027c5e7a
AM
29913+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
29914+ bindex, err);
29915+ /* go on even if err */
1facf9fc 29916+ }
027c5e7a 29917+ au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
1facf9fc 29918+
b95c5147 29919+ if (do_idop) {
79b8bda9
AM
29920+ if (au_ftest_si(sbi, NO_DREVAL)) {
29921+ AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
29922+ sb->s_d_op = &aufs_dop_noreval;
b95c5147
AM
29923+ AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
29924+ sbi->si_iop_array = aufs_iop_nogetattr;
79b8bda9
AM
29925+ } else {
29926+ AuDebugOn(sb->s_d_op == &aufs_dop);
29927+ sb->s_d_op = &aufs_dop;
b95c5147
AM
29928+ AuDebugOn(sbi->si_iop_array == aufs_iop);
29929+ sbi->si_iop_array = aufs_iop;
79b8bda9 29930+ }
062440b3 29931+ pr_info("reset to %ps and %ps\n",
b95c5147 29932+ sb->s_d_op, sbi->si_iop_array);
79b8bda9
AM
29933+ }
29934+
027c5e7a 29935+ di_write_unlock(root);
b95c5147
AM
29936+ err = au_refresh_d(sb, do_idop);
29937+ e = au_refresh_i(sb, do_idop);
027c5e7a
AM
29938+ if (unlikely(e && !err))
29939+ err = e;
1facf9fc 29940+ /* aufs_write_lock() calls ..._child() */
29941+ di_write_lock_child(root);
027c5e7a
AM
29942+
29943+ au_cpup_attr_all(inode, /*force*/1);
29944+
29945+ if (unlikely(err))
29946+ AuIOErr("refresh failed, ignored, %d\n", err);
1facf9fc 29947+}
29948+
29949+/* stop extra interpretation of errno in mount(8), and strange error messages */
29950+static int cvt_err(int err)
29951+{
29952+ AuTraceErr(err);
29953+
29954+ switch (err) {
29955+ case -ENOENT:
29956+ case -ENOTDIR:
29957+ case -EEXIST:
29958+ case -EIO:
29959+ err = -EINVAL;
29960+ }
29961+ return err;
29962+}
29963+
29964+static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
29965+{
4a4d8108
AM
29966+ int err, do_dx;
29967+ unsigned int mntflags;
be52b249
AM
29968+ struct au_opts opts = {
29969+ .opt = NULL
29970+ };
1facf9fc 29971+ struct dentry *root;
29972+ struct inode *inode;
29973+ struct au_sbinfo *sbinfo;
29974+
29975+ err = 0;
29976+ root = sb->s_root;
29977+ if (!data || !*data) {
e49829fe
JR
29978+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
29979+ if (!err) {
29980+ di_write_lock_child(root);
29981+ err = au_opts_verify(sb, *flags, /*pending*/0);
29982+ aufs_write_unlock(root);
29983+ }
1facf9fc 29984+ goto out;
29985+ }
29986+
29987+ err = -ENOMEM;
1facf9fc 29988+ opts.opt = (void *)__get_free_page(GFP_NOFS);
29989+ if (unlikely(!opts.opt))
29990+ goto out;
29991+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
29992+ opts.flags = AuOpts_REMOUNT;
29993+ opts.sb_flags = *flags;
29994+
29995+ /* parse it before aufs lock */
29996+ err = au_opts_parse(sb, data, &opts);
29997+ if (unlikely(err))
29998+ goto out_opts;
29999+
30000+ sbinfo = au_sbi(sb);
5527c038 30001+ inode = d_inode(root);
febd17d6 30002+ inode_lock(inode);
e49829fe
JR
30003+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30004+ if (unlikely(err))
30005+ goto out_mtx;
30006+ di_write_lock_child(root);
1facf9fc 30007+
30008+ /* au_opts_remount() may return an error */
30009+ err = au_opts_remount(sb, &opts);
30010+ au_opts_free(&opts);
30011+
027c5e7a 30012+ if (au_ftest_opts(opts.flags, REFRESH))
b95c5147 30013+ au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
1facf9fc 30014+
4a4d8108
AM
30015+ if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
30016+ mntflags = au_mntflags(sb);
30017+ do_dx = !!au_opt_test(mntflags, DIO);
30018+ au_dy_arefresh(do_dx);
30019+ }
30020+
076b876e 30021+ au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
1facf9fc 30022+ aufs_write_unlock(root);
953406b4 30023+
e49829fe 30024+out_mtx:
febd17d6 30025+ inode_unlock(inode);
4f0767ce 30026+out_opts:
1c60b727 30027+ free_page((unsigned long)opts.opt);
4f0767ce 30028+out:
1facf9fc 30029+ err = cvt_err(err);
30030+ AuTraceErr(err);
30031+ return err;
30032+}
30033+
4a4d8108 30034+static const struct super_operations aufs_sop = {
1facf9fc 30035+ .alloc_inode = aufs_alloc_inode,
30036+ .destroy_inode = aufs_destroy_inode,
b752ccd1 30037+ /* always deleting, no clearing */
1facf9fc 30038+ .drop_inode = generic_delete_inode,
30039+ .show_options = aufs_show_options,
30040+ .statfs = aufs_statfs,
30041+ .put_super = aufs_put_super,
537831f9 30042+ .sync_fs = aufs_sync_fs,
1facf9fc 30043+ .remount_fs = aufs_remount_fs
30044+};
30045+
30046+/* ---------------------------------------------------------------------- */
30047+
30048+static int alloc_root(struct super_block *sb)
30049+{
30050+ int err;
30051+ struct inode *inode;
30052+ struct dentry *root;
30053+
30054+ err = -ENOMEM;
30055+ inode = au_iget_locked(sb, AUFS_ROOT_INO);
30056+ err = PTR_ERR(inode);
30057+ if (IS_ERR(inode))
30058+ goto out;
30059+
b95c5147 30060+ inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
1facf9fc 30061+ inode->i_fop = &aufs_dir_fop;
30062+ inode->i_mode = S_IFDIR;
9dbd164d 30063+ set_nlink(inode, 2);
1facf9fc 30064+ unlock_new_inode(inode);
30065+
92d182d2 30066+ root = d_make_root(inode);
1facf9fc 30067+ if (unlikely(!root))
92d182d2 30068+ goto out;
1facf9fc 30069+ err = PTR_ERR(root);
30070+ if (IS_ERR(root))
92d182d2 30071+ goto out;
1facf9fc 30072+
4a4d8108 30073+ err = au_di_init(root);
1facf9fc 30074+ if (!err) {
30075+ sb->s_root = root;
30076+ return 0; /* success */
30077+ }
30078+ dput(root);
1facf9fc 30079+
4f0767ce 30080+out:
1facf9fc 30081+ return err;
1facf9fc 30082+}
30083+
30084+static int aufs_fill_super(struct super_block *sb, void *raw_data,
30085+ int silent __maybe_unused)
30086+{
30087+ int err;
be52b249
AM
30088+ struct au_opts opts = {
30089+ .opt = NULL
30090+ };
79b8bda9 30091+ struct au_sbinfo *sbinfo;
1facf9fc 30092+ struct dentry *root;
30093+ struct inode *inode;
30094+ char *arg = raw_data;
30095+
30096+ if (unlikely(!arg || !*arg)) {
30097+ err = -EINVAL;
4a4d8108 30098+ pr_err("no arg\n");
1facf9fc 30099+ goto out;
30100+ }
30101+
30102+ err = -ENOMEM;
1facf9fc 30103+ opts.opt = (void *)__get_free_page(GFP_NOFS);
30104+ if (unlikely(!opts.opt))
30105+ goto out;
30106+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30107+ opts.sb_flags = sb->s_flags;
30108+
30109+ err = au_si_alloc(sb);
30110+ if (unlikely(err))
30111+ goto out_opts;
79b8bda9 30112+ sbinfo = au_sbi(sb);
1facf9fc 30113+
30114+ /* all timestamps always follow the ones on the branch */
2121bcd9 30115+ sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
be118d29 30116+ sb->s_flags |= SB_I_VERSION; /* do we really need this? */
1facf9fc 30117+ sb->s_op = &aufs_sop;
027c5e7a 30118+ sb->s_d_op = &aufs_dop;
1facf9fc 30119+ sb->s_magic = AUFS_SUPER_MAGIC;
30120+ sb->s_maxbytes = 0;
c1595e42 30121+ sb->s_stack_depth = 1;
1facf9fc 30122+ au_export_init(sb);
f2c43d5f 30123+ au_xattr_init(sb);
1facf9fc 30124+
30125+ err = alloc_root(sb);
30126+ if (unlikely(err)) {
30127+ si_write_unlock(sb);
30128+ goto out_info;
30129+ }
30130+ root = sb->s_root;
5527c038 30131+ inode = d_inode(root);
1facf9fc 30132+
30133+ /*
30134+ * actually we can parse options regardless aufs lock here.
30135+ * but at remount time, parsing must be done before aufs lock.
30136+ * so we follow the same rule.
30137+ */
30138+ ii_write_lock_parent(inode);
30139+ aufs_write_unlock(root);
30140+ err = au_opts_parse(sb, arg, &opts);
30141+ if (unlikely(err))
30142+ goto out_root;
30143+
30144+ /* lock vfs_inode first, then aufs. */
febd17d6 30145+ inode_lock(inode);
1facf9fc 30146+ aufs_write_lock(root);
30147+ err = au_opts_mount(sb, &opts);
30148+ au_opts_free(&opts);
79b8bda9
AM
30149+ if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
30150+ sb->s_d_op = &aufs_dop_noreval;
062440b3 30151+ pr_info("%ps\n", sb->s_d_op);
79b8bda9 30152+ au_refresh_dop(root, /*force_reval*/0);
b95c5147
AM
30153+ sbinfo->si_iop_array = aufs_iop_nogetattr;
30154+ au_refresh_iop(inode, /*force_getattr*/0);
79b8bda9 30155+ }
1facf9fc 30156+ aufs_write_unlock(root);
febd17d6 30157+ inode_unlock(inode);
4a4d8108
AM
30158+ if (!err)
30159+ goto out_opts; /* success */
1facf9fc 30160+
4f0767ce 30161+out_root:
1facf9fc 30162+ dput(root);
30163+ sb->s_root = NULL;
4f0767ce 30164+out_info:
79b8bda9 30165+ kobject_put(&sbinfo->si_kobj);
1facf9fc 30166+ sb->s_fs_info = NULL;
4f0767ce 30167+out_opts:
1c60b727 30168+ free_page((unsigned long)opts.opt);
4f0767ce 30169+out:
1facf9fc 30170+ AuTraceErr(err);
30171+ err = cvt_err(err);
30172+ AuTraceErr(err);
30173+ return err;
30174+}
30175+
30176+/* ---------------------------------------------------------------------- */
30177+
027c5e7a
AM
30178+static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
30179+ const char *dev_name __maybe_unused,
30180+ void *raw_data)
1facf9fc 30181+{
027c5e7a 30182+ struct dentry *root;
1facf9fc 30183+
30184+ /* all timestamps always follow the ones on the branch */
30185+ /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
027c5e7a
AM
30186+ root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
30187+ if (IS_ERR(root))
30188+ goto out;
30189+
062440b3 30190+ au_sbilist_add(root->d_sb);
027c5e7a
AM
30191+
30192+out:
30193+ return root;
1facf9fc 30194+}
30195+
e49829fe
JR
30196+static void aufs_kill_sb(struct super_block *sb)
30197+{
30198+ struct au_sbinfo *sbinfo;
30199+
30200+ sbinfo = au_sbi(sb);
30201+ if (sbinfo) {
30202+ au_sbilist_del(sb);
30203+ aufs_write_lock(sb->s_root);
076b876e 30204+ au_fhsm_fin(sb);
e49829fe
JR
30205+ if (sbinfo->si_wbr_create_ops->fin)
30206+ sbinfo->si_wbr_create_ops->fin(sb);
30207+ if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30208+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
b95c5147 30209+ au_remount_refresh(sb, /*do_idop*/0);
e49829fe
JR
30210+ }
30211+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
30212+ au_plink_put(sb, /*verbose*/1);
30213+ au_xino_clr(sb);
8b6a4947 30214+ au_dr_opt_flush(sb);
1e00d052 30215+ sbinfo->si_sb = NULL;
e49829fe 30216+ aufs_write_unlock(sb->s_root);
e49829fe
JR
30217+ au_nwt_flush(&sbinfo->si_nowait);
30218+ }
98d9a5b1 30219+ kill_anon_super(sb);
e49829fe
JR
30220+}
30221+
1facf9fc 30222+struct file_system_type aufs_fs_type = {
30223+ .name = AUFS_FSTYPE,
c06a8ce3
AM
30224+ /* a race between rename and others */
30225+ .fs_flags = FS_RENAME_DOES_D_MOVE,
027c5e7a 30226+ .mount = aufs_mount,
e49829fe 30227+ .kill_sb = aufs_kill_sb,
1facf9fc 30228+ /* no need to __module_get() and module_put(). */
30229+ .owner = THIS_MODULE,
30230+};
7f207e10
AM
30231diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30232--- /usr/share/empty/fs/aufs/super.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 30233+++ linux/fs/aufs/super.h 2018-08-12 23:43:05.463458173 +0200
062440b3
AM
30234@@ -0,0 +1,627 @@
30235+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 30236+/*
b00004a5 30237+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 30238+ *
30239+ * This program, aufs is free software; you can redistribute it and/or modify
30240+ * it under the terms of the GNU General Public License as published by
30241+ * the Free Software Foundation; either version 2 of the License, or
30242+ * (at your option) any later version.
dece6358
AM
30243+ *
30244+ * This program is distributed in the hope that it will be useful,
30245+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30246+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30247+ * GNU General Public License for more details.
30248+ *
30249+ * You should have received a copy of the GNU General Public License
523b37e3 30250+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30251+ */
30252+
30253+/*
30254+ * super_block operations
30255+ */
30256+
30257+#ifndef __AUFS_SUPER_H__
30258+#define __AUFS_SUPER_H__
30259+
30260+#ifdef __KERNEL__
30261+
30262+#include <linux/fs.h>
5527c038 30263+#include <linux/kobject.h>
8b6a4947 30264+#include "hbl.h"
1facf9fc 30265+#include "rwsem.h"
1facf9fc 30266+#include "wkq.h"
30267+
1facf9fc 30268+/* policies to select one among multiple writable branches */
30269+struct au_wbr_copyup_operations {
30270+ int (*copyup)(struct dentry *dentry);
30271+};
30272+
392086de
AM
30273+#define AuWbr_DIR 1 /* target is a dir */
30274+#define AuWbr_PARENT (1 << 1) /* always require a parent */
30275+
30276+#define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
30277+#define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
30278+#define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
30279+
1facf9fc 30280+struct au_wbr_create_operations {
392086de 30281+ int (*create)(struct dentry *dentry, unsigned int flags);
1facf9fc 30282+ int (*init)(struct super_block *sb);
30283+ int (*fin)(struct super_block *sb);
30284+};
30285+
30286+struct au_wbr_mfs {
30287+ struct mutex mfs_lock; /* protect this structure */
30288+ unsigned long mfs_jiffy;
30289+ unsigned long mfs_expire;
30290+ aufs_bindex_t mfs_bindex;
30291+
30292+ unsigned long long mfsrr_bytes;
30293+ unsigned long long mfsrr_watermark;
30294+};
30295+
86dc4139
AM
30296+#define AuPlink_NHASH 100
30297+static inline int au_plink_hash(ino_t ino)
30298+{
30299+ return ino % AuPlink_NHASH;
30300+}
30301+
076b876e
AM
30302+/* File-based Hierarchical Storage Management */
30303+struct au_fhsm {
30304+#ifdef CONFIG_AUFS_FHSM
30305+ /* allow only one process who can receive the notification */
30306+ spinlock_t fhsm_spin;
30307+ pid_t fhsm_pid;
30308+ wait_queue_head_t fhsm_wqh;
30309+ atomic_t fhsm_readable;
30310+
c1595e42 30311+ /* these are protected by si_rwsem */
076b876e 30312+ unsigned long fhsm_expire;
c1595e42 30313+ aufs_bindex_t fhsm_bottom;
076b876e
AM
30314+#endif
30315+};
30316+
1facf9fc 30317+struct au_branch;
30318+struct au_sbinfo {
30319+ /* nowait tasks in the system-wide workqueue */
30320+ struct au_nowait_tasks si_nowait;
30321+
b752ccd1
AM
30322+ /*
30323+ * tried sb->s_umount, but failed due to the dependecy between i_mutex.
30324+ * rwsem for au_sbinfo is necessary.
30325+ */
dece6358 30326+ struct au_rwsem si_rwsem;
1facf9fc 30327+
7f207e10 30328+ /*
523b37e3
AM
30329+ * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30330+ * remount.
7f207e10 30331+ */
5afbbe0d 30332+ struct percpu_counter si_ninodes, si_nfiles;
7f207e10 30333+
1facf9fc 30334+ /* branch management */
30335+ unsigned int si_generation;
30336+
2000de60 30337+ /* see AuSi_ flags */
1facf9fc 30338+ unsigned char au_si_status;
30339+
5afbbe0d 30340+ aufs_bindex_t si_bbot;
7f207e10
AM
30341+
30342+ /* dirty trick to keep br_id plus */
30343+ unsigned int si_last_br_id :
30344+ sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
1facf9fc 30345+ struct au_branch **si_branch;
30346+
30347+ /* policy to select a writable branch */
30348+ unsigned char si_wbr_copyup;
30349+ unsigned char si_wbr_create;
30350+ struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30351+ struct au_wbr_create_operations *si_wbr_create_ops;
30352+
30353+ /* round robin */
30354+ atomic_t si_wbr_rr_next;
30355+
30356+ /* most free space */
30357+ struct au_wbr_mfs si_wbr_mfs;
30358+
076b876e
AM
30359+ /* File-based Hierarchical Storage Management */
30360+ struct au_fhsm si_fhsm;
30361+
1facf9fc 30362+ /* mount flags */
30363+ /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30364+ unsigned int si_mntflags;
30365+
30366+ /* external inode number (bitmap and translation table) */
5527c038
JR
30367+ vfs_readf_t si_xread;
30368+ vfs_writef_t si_xwrite;
1facf9fc 30369+ struct file *si_xib;
30370+ struct mutex si_xib_mtx; /* protect xib members */
30371+ unsigned long *si_xib_buf;
30372+ unsigned long si_xib_last_pindex;
30373+ int si_xib_next_bit;
30374+ aufs_bindex_t si_xino_brid;
392086de
AM
30375+ unsigned long si_xino_jiffy;
30376+ unsigned long si_xino_expire;
1facf9fc 30377+ /* reserved for future use */
30378+ /* unsigned long long si_xib_limit; */ /* Max xib file size */
30379+
30380+#ifdef CONFIG_AUFS_EXPORT
30381+ /* i_generation */
30382+ struct file *si_xigen;
30383+ atomic_t si_xigen_next;
30384+#endif
30385+
b912730e 30386+ /* dirty trick to suppoer atomic_open */
8b6a4947 30387+ struct hlist_bl_head si_aopen;
b912730e 30388+
1facf9fc 30389+ /* vdir parameters */
e49829fe 30390+ unsigned long si_rdcache; /* max cache time in jiffies */
1facf9fc 30391+ unsigned int si_rdblk; /* deblk size */
30392+ unsigned int si_rdhash; /* hash size */
30393+
30394+ /*
30395+ * If the number of whiteouts are larger than si_dirwh, leave all of
30396+ * them after au_whtmp_ren to reduce the cost of rmdir(2).
30397+ * future fsck.aufs or kernel thread will remove them later.
30398+ * Otherwise, remove all whiteouts and the dir in rmdir(2).
30399+ */
30400+ unsigned int si_dirwh;
30401+
1facf9fc 30402+ /* pseudo_link list */
8b6a4947 30403+ struct hlist_bl_head si_plink[AuPlink_NHASH];
1facf9fc 30404+ wait_queue_head_t si_plink_wq;
4a4d8108 30405+ spinlock_t si_plink_maint_lock;
e49829fe 30406+ pid_t si_plink_maint_pid;
1facf9fc 30407+
523b37e3 30408+ /* file list */
8b6a4947 30409+ struct hlist_bl_head si_files;
523b37e3 30410+
b95c5147
AM
30411+ /* with/without getattr, brother of sb->s_d_op */
30412+ struct inode_operations *si_iop_array;
30413+
1facf9fc 30414+ /*
30415+ * sysfs and lifetime management.
30416+ * this is not a small structure and it may be a waste of memory in case
30417+ * of sysfs is disabled, particulary when many aufs-es are mounted.
30418+ * but using sysfs is majority.
30419+ */
30420+ struct kobject si_kobj;
30421+#ifdef CONFIG_DEBUG_FS
86dc4139
AM
30422+ struct dentry *si_dbgaufs;
30423+ struct dentry *si_dbgaufs_plink;
30424+ struct dentry *si_dbgaufs_xib;
1facf9fc 30425+#ifdef CONFIG_AUFS_EXPORT
30426+ struct dentry *si_dbgaufs_xigen;
30427+#endif
30428+#endif
30429+
e49829fe 30430+#ifdef CONFIG_AUFS_SBILIST
8b6a4947 30431+ struct hlist_bl_node si_list;
e49829fe
JR
30432+#endif
30433+
1facf9fc 30434+ /* dirty, necessary for unmounting, sysfs and sysrq */
30435+ struct super_block *si_sb;
30436+};
30437+
dece6358
AM
30438+/* sbinfo status flags */
30439+/*
30440+ * set true when refresh_dirs() failed at remount time.
30441+ * then try refreshing dirs at access time again.
062440b3 30442+ * if it is false, refreshing dirs at access time is unnecessary
dece6358 30443+ */
027c5e7a 30444+#define AuSi_FAILED_REFRESH_DIR 1
076b876e 30445+#define AuSi_FHSM (1 << 1) /* fhsm is active now */
79b8bda9 30446+#define AuSi_NO_DREVAL (1 << 2) /* disable all d_revalidate */
076b876e
AM
30447+
30448+#ifndef CONFIG_AUFS_FHSM
30449+#undef AuSi_FHSM
30450+#define AuSi_FHSM 0
30451+#endif
30452+
dece6358
AM
30453+static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30454+ unsigned int flag)
30455+{
30456+ AuRwMustAnyLock(&sbi->si_rwsem);
30457+ return sbi->au_si_status & flag;
30458+}
30459+#define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
30460+#define au_fset_si(sbinfo, name) do { \
30461+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30462+ (sbinfo)->au_si_status |= AuSi_##name; \
30463+} while (0)
30464+#define au_fclr_si(sbinfo, name) do { \
30465+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30466+ (sbinfo)->au_si_status &= ~AuSi_##name; \
30467+} while (0)
30468+
1facf9fc 30469+/* ---------------------------------------------------------------------- */
30470+
30471+/* policy to select one among writable branches */
4a4d8108
AM
30472+#define AuWbrCopyup(sbinfo, ...) \
30473+ ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30474+#define AuWbrCreate(sbinfo, ...) \
30475+ ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
1facf9fc 30476+
30477+/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30478+#define AuLock_DW 1 /* write-lock dentry */
30479+#define AuLock_IR (1 << 1) /* read-lock inode */
30480+#define AuLock_IW (1 << 2) /* write-lock inode */
30481+#define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
b95c5147 30482+#define AuLock_DIRS (1 << 4) /* target is a pair of dirs */
f2c43d5f 30483+ /* except RENAME_EXCHANGE */
e49829fe
JR
30484+#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
30485+#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
027c5e7a 30486+#define AuLock_GEN (1 << 7) /* test digen/iigen */
1facf9fc 30487+#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
7f207e10
AM
30488+#define au_fset_lock(flags, name) \
30489+ do { (flags) |= AuLock_##name; } while (0)
30490+#define au_fclr_lock(flags, name) \
30491+ do { (flags) &= ~AuLock_##name; } while (0)
1facf9fc 30492+
30493+/* ---------------------------------------------------------------------- */
30494+
30495+/* super.c */
30496+extern struct file_system_type aufs_fs_type;
30497+struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
79b8bda9
AM
30498+typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30499+ unsigned long long max, void *arg);
79b8bda9
AM
30500+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30501+ struct super_block *sb, void *arg);
7f207e10
AM
30502+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30503+void au_iarray_free(struct inode **a, unsigned long long max);
1facf9fc 30504+
30505+/* sbinfo.c */
30506+void au_si_free(struct kobject *kobj);
30507+int au_si_alloc(struct super_block *sb);
e2f27e51 30508+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
1facf9fc 30509+
30510+unsigned int au_sigen_inc(struct super_block *sb);
30511+aufs_bindex_t au_new_br_id(struct super_block *sb);
30512+
e49829fe
JR
30513+int si_read_lock(struct super_block *sb, int flags);
30514+int si_write_lock(struct super_block *sb, int flags);
30515+int aufs_read_lock(struct dentry *dentry, int flags);
1facf9fc 30516+void aufs_read_unlock(struct dentry *dentry, int flags);
30517+void aufs_write_lock(struct dentry *dentry);
30518+void aufs_write_unlock(struct dentry *dentry);
e49829fe 30519+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
1facf9fc 30520+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30521+
30522+/* wbr_policy.c */
30523+extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30524+extern struct au_wbr_create_operations au_wbr_create_ops[];
30525+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
c2b27bf2 30526+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
5afbbe0d 30527+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
c2b27bf2
AM
30528+
30529+/* mvdown.c */
30530+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
1facf9fc 30531+
076b876e
AM
30532+#ifdef CONFIG_AUFS_FHSM
30533+/* fhsm.c */
30534+
30535+static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30536+{
30537+ pid_t pid;
30538+
30539+ spin_lock(&fhsm->fhsm_spin);
30540+ pid = fhsm->fhsm_pid;
30541+ spin_unlock(&fhsm->fhsm_spin);
30542+
30543+ return pid;
30544+}
30545+
30546+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
30547+void au_fhsm_wrote_all(struct super_block *sb, int force);
30548+int au_fhsm_fd(struct super_block *sb, int oflags);
30549+int au_fhsm_br_alloc(struct au_branch *br);
c1595e42 30550+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
076b876e
AM
30551+void au_fhsm_fin(struct super_block *sb);
30552+void au_fhsm_init(struct au_sbinfo *sbinfo);
30553+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
30554+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
30555+#else
30556+AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
30557+ int force)
30558+AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
30559+AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
c1595e42
JR
30560+AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
30561+AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
30562+AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
076b876e
AM
30563+AuStubVoid(au_fhsm_fin, struct super_block *sb)
30564+AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
30565+AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
30566+AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
30567+#endif
30568+
1facf9fc 30569+/* ---------------------------------------------------------------------- */
30570+
30571+static inline struct au_sbinfo *au_sbi(struct super_block *sb)
30572+{
30573+ return sb->s_fs_info;
30574+}
30575+
30576+/* ---------------------------------------------------------------------- */
30577+
30578+#ifdef CONFIG_AUFS_EXPORT
a2a7ad62 30579+int au_test_nfsd(void);
1facf9fc 30580+void au_export_init(struct super_block *sb);
b752ccd1 30581+void au_xigen_inc(struct inode *inode);
1facf9fc 30582+int au_xigen_new(struct inode *inode);
062440b3 30583+int au_xigen_set(struct super_block *sb, struct path *path);
1facf9fc 30584+void au_xigen_clr(struct super_block *sb);
30585+
30586+static inline int au_busy_or_stale(void)
30587+{
b752ccd1 30588+ if (!au_test_nfsd())
1facf9fc 30589+ return -EBUSY;
30590+ return -ESTALE;
30591+}
30592+#else
b752ccd1 30593+AuStubInt0(au_test_nfsd, void)
a2a7ad62 30594+AuStubVoid(au_export_init, struct super_block *sb)
b752ccd1 30595+AuStubVoid(au_xigen_inc, struct inode *inode)
4a4d8108 30596+AuStubInt0(au_xigen_new, struct inode *inode)
062440b3 30597+AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
4a4d8108 30598+AuStubVoid(au_xigen_clr, struct super_block *sb)
c1595e42 30599+AuStub(int, au_busy_or_stale, return -EBUSY, void)
1facf9fc 30600+#endif /* CONFIG_AUFS_EXPORT */
30601+
30602+/* ---------------------------------------------------------------------- */
30603+
e49829fe
JR
30604+#ifdef CONFIG_AUFS_SBILIST
30605+/* module.c */
8b6a4947 30606+extern struct hlist_bl_head au_sbilist;
e49829fe
JR
30607+
30608+static inline void au_sbilist_init(void)
30609+{
8b6a4947 30610+ INIT_HLIST_BL_HEAD(&au_sbilist);
e49829fe
JR
30611+}
30612+
30613+static inline void au_sbilist_add(struct super_block *sb)
30614+{
8b6a4947 30615+ au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe
JR
30616+}
30617+
30618+static inline void au_sbilist_del(struct super_block *sb)
30619+{
8b6a4947 30620+ au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe 30621+}
53392da6
AM
30622+
30623+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
30624+static inline void au_sbilist_lock(void)
30625+{
8b6a4947 30626+ hlist_bl_lock(&au_sbilist);
53392da6
AM
30627+}
30628+
30629+static inline void au_sbilist_unlock(void)
30630+{
8b6a4947 30631+ hlist_bl_unlock(&au_sbilist);
53392da6
AM
30632+}
30633+#define AuGFP_SBILIST GFP_ATOMIC
30634+#else
30635+AuStubVoid(au_sbilist_lock, void)
30636+AuStubVoid(au_sbilist_unlock, void)
30637+#define AuGFP_SBILIST GFP_NOFS
30638+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
e49829fe
JR
30639+#else
30640+AuStubVoid(au_sbilist_init, void)
c1595e42
JR
30641+AuStubVoid(au_sbilist_add, struct super_block *sb)
30642+AuStubVoid(au_sbilist_del, struct super_block *sb)
53392da6
AM
30643+AuStubVoid(au_sbilist_lock, void)
30644+AuStubVoid(au_sbilist_unlock, void)
30645+#define AuGFP_SBILIST GFP_NOFS
e49829fe
JR
30646+#endif
30647+
30648+/* ---------------------------------------------------------------------- */
30649+
1facf9fc 30650+static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
30651+{
dece6358 30652+ /*
c1595e42 30653+ * This function is a dynamic '__init' function actually,
dece6358
AM
30654+ * so the tiny check for si_rwsem is unnecessary.
30655+ */
30656+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
1facf9fc 30657+#ifdef CONFIG_DEBUG_FS
30658+ sbinfo->si_dbgaufs = NULL;
86dc4139 30659+ sbinfo->si_dbgaufs_plink = NULL;
1facf9fc 30660+ sbinfo->si_dbgaufs_xib = NULL;
30661+#ifdef CONFIG_AUFS_EXPORT
30662+ sbinfo->si_dbgaufs_xigen = NULL;
30663+#endif
30664+#endif
30665+}
30666+
30667+/* ---------------------------------------------------------------------- */
30668+
a2654f78
AM
30669+/* current->atomic_flags */
30670+/* this value should never corrupt the ones defined in linux/sched.h */
30671+#define PFA_AUFS 7
30672+
30673+TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
30674+TASK_PFA_SET(AUFS, aufs) /* task_set_aufs */
30675+TASK_PFA_CLEAR(AUFS, aufs) /* task_clear_aufs */
b752ccd1
AM
30676+
30677+static inline int si_pid_test(struct super_block *sb)
30678+{
a2654f78 30679+ return !!task_test_aufs(current);
b752ccd1
AM
30680+}
30681+
30682+static inline void si_pid_clr(struct super_block *sb)
30683+{
a2654f78
AM
30684+ AuDebugOn(!task_test_aufs(current));
30685+ task_clear_aufs(current);
b752ccd1
AM
30686+}
30687+
a2654f78
AM
30688+static inline void si_pid_set(struct super_block *sb)
30689+{
30690+ AuDebugOn(task_test_aufs(current));
30691+ task_set_aufs(current);
30692+}
febd17d6 30693+
b752ccd1
AM
30694+/* ---------------------------------------------------------------------- */
30695+
1facf9fc 30696+/* lock superblock. mainly for entry point functions */
8b6a4947
AM
30697+#define __si_read_lock(sb) au_rw_read_lock(&au_sbi(sb)->si_rwsem)
30698+#define __si_write_lock(sb) au_rw_write_lock(&au_sbi(sb)->si_rwsem)
30699+#define __si_read_trylock(sb) au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
30700+#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
30701+/*
30702+#define __si_read_trylock_nested(sb) \
30703+ au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
30704+#define __si_write_trylock_nested(sb) \
30705+ au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
30706+*/
30707+
30708+#define __si_read_unlock(sb) au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
30709+#define __si_write_unlock(sb) au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
30710+#define __si_downgrade_lock(sb) au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
1facf9fc 30711+
dece6358
AM
30712+#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
30713+#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
30714+#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
30715+
b752ccd1
AM
30716+static inline void si_noflush_read_lock(struct super_block *sb)
30717+{
30718+ __si_read_lock(sb);
30719+ si_pid_set(sb);
30720+}
30721+
30722+static inline int si_noflush_read_trylock(struct super_block *sb)
30723+{
076b876e
AM
30724+ int locked;
30725+
30726+ locked = __si_read_trylock(sb);
b752ccd1
AM
30727+ if (locked)
30728+ si_pid_set(sb);
30729+ return locked;
30730+}
30731+
30732+static inline void si_noflush_write_lock(struct super_block *sb)
30733+{
30734+ __si_write_lock(sb);
30735+ si_pid_set(sb);
30736+}
30737+
30738+static inline int si_noflush_write_trylock(struct super_block *sb)
30739+{
076b876e
AM
30740+ int locked;
30741+
30742+ locked = __si_write_trylock(sb);
b752ccd1
AM
30743+ if (locked)
30744+ si_pid_set(sb);
30745+ return locked;
30746+}
30747+
7e9cd9fe 30748+#if 0 /* reserved */
1facf9fc 30749+static inline int si_read_trylock(struct super_block *sb, int flags)
30750+{
30751+ if (au_ftest_lock(flags, FLUSH))
30752+ au_nwt_flush(&au_sbi(sb)->si_nowait);
30753+ return si_noflush_read_trylock(sb);
30754+}
e49829fe 30755+#endif
1facf9fc 30756+
b752ccd1
AM
30757+static inline void si_read_unlock(struct super_block *sb)
30758+{
30759+ si_pid_clr(sb);
30760+ __si_read_unlock(sb);
30761+}
30762+
7e9cd9fe 30763+#if 0 /* reserved */
1facf9fc 30764+static inline int si_write_trylock(struct super_block *sb, int flags)
30765+{
30766+ if (au_ftest_lock(flags, FLUSH))
30767+ au_nwt_flush(&au_sbi(sb)->si_nowait);
30768+ return si_noflush_write_trylock(sb);
30769+}
b752ccd1
AM
30770+#endif
30771+
30772+static inline void si_write_unlock(struct super_block *sb)
30773+{
30774+ si_pid_clr(sb);
30775+ __si_write_unlock(sb);
30776+}
30777+
7e9cd9fe 30778+#if 0 /* reserved */
b752ccd1
AM
30779+static inline void si_downgrade_lock(struct super_block *sb)
30780+{
30781+ __si_downgrade_lock(sb);
30782+}
30783+#endif
1facf9fc 30784+
30785+/* ---------------------------------------------------------------------- */
30786+
5afbbe0d 30787+static inline aufs_bindex_t au_sbbot(struct super_block *sb)
1facf9fc 30788+{
dece6358 30789+ SiMustAnyLock(sb);
5afbbe0d 30790+ return au_sbi(sb)->si_bbot;
1facf9fc 30791+}
30792+
30793+static inline unsigned int au_mntflags(struct super_block *sb)
30794+{
dece6358 30795+ SiMustAnyLock(sb);
1facf9fc 30796+ return au_sbi(sb)->si_mntflags;
30797+}
30798+
30799+static inline unsigned int au_sigen(struct super_block *sb)
30800+{
dece6358 30801+ SiMustAnyLock(sb);
1facf9fc 30802+ return au_sbi(sb)->si_generation;
30803+}
30804+
5afbbe0d
AM
30805+static inline unsigned long long au_ninodes(struct super_block *sb)
30806+{
30807+ s64 n = percpu_counter_sum(&au_sbi(sb)->si_ninodes);
30808+
30809+ BUG_ON(n < 0);
30810+ return n;
30811+}
30812+
7f207e10
AM
30813+static inline void au_ninodes_inc(struct super_block *sb)
30814+{
5afbbe0d 30815+ percpu_counter_inc(&au_sbi(sb)->si_ninodes);
7f207e10
AM
30816+}
30817+
30818+static inline void au_ninodes_dec(struct super_block *sb)
30819+{
5afbbe0d
AM
30820+ percpu_counter_dec(&au_sbi(sb)->si_ninodes);
30821+}
30822+
30823+static inline unsigned long long au_nfiles(struct super_block *sb)
30824+{
30825+ s64 n = percpu_counter_sum(&au_sbi(sb)->si_nfiles);
30826+
30827+ BUG_ON(n < 0);
30828+ return n;
7f207e10
AM
30829+}
30830+
30831+static inline void au_nfiles_inc(struct super_block *sb)
30832+{
5afbbe0d 30833+ percpu_counter_inc(&au_sbi(sb)->si_nfiles);
7f207e10
AM
30834+}
30835+
30836+static inline void au_nfiles_dec(struct super_block *sb)
30837+{
5afbbe0d 30838+ percpu_counter_dec(&au_sbi(sb)->si_nfiles);
7f207e10
AM
30839+}
30840+
1facf9fc 30841+static inline struct au_branch *au_sbr(struct super_block *sb,
30842+ aufs_bindex_t bindex)
30843+{
dece6358 30844+ SiMustAnyLock(sb);
1facf9fc 30845+ return au_sbi(sb)->si_branch[0 + bindex];
30846+}
30847+
30848+static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
30849+{
dece6358 30850+ SiMustWriteLock(sb);
1facf9fc 30851+ au_sbi(sb)->si_xino_brid = brid;
30852+}
30853+
30854+static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
30855+{
dece6358 30856+ SiMustAnyLock(sb);
1facf9fc 30857+ return au_sbi(sb)->si_xino_brid;
30858+}
30859+
30860+#endif /* __KERNEL__ */
30861+#endif /* __AUFS_SUPER_H__ */
7f207e10
AM
30862diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
30863--- /usr/share/empty/fs/aufs/sysaufs.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 30864+++ linux/fs/aufs/sysaufs.c 2018-08-12 23:43:05.463458173 +0200
062440b3 30865@@ -0,0 +1,93 @@
cd7a4cd9 30866+// SPDX-License-Identifier: GPL-2.0
1facf9fc 30867+/*
b00004a5 30868+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 30869+ *
30870+ * This program, aufs is free software; you can redistribute it and/or modify
30871+ * it under the terms of the GNU General Public License as published by
30872+ * the Free Software Foundation; either version 2 of the License, or
30873+ * (at your option) any later version.
dece6358
AM
30874+ *
30875+ * This program is distributed in the hope that it will be useful,
30876+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30877+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30878+ * GNU General Public License for more details.
30879+ *
30880+ * You should have received a copy of the GNU General Public License
523b37e3 30881+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30882+ */
30883+
30884+/*
30885+ * sysfs interface and lifetime management
30886+ * they are necessary regardless sysfs is disabled.
30887+ */
30888+
1facf9fc 30889+#include <linux/random.h>
1facf9fc 30890+#include "aufs.h"
30891+
30892+unsigned long sysaufs_si_mask;
e49829fe 30893+struct kset *sysaufs_kset;
1facf9fc 30894+
30895+#define AuSiAttr(_name) { \
30896+ .attr = { .name = __stringify(_name), .mode = 0444 }, \
30897+ .show = sysaufs_si_##_name, \
30898+}
30899+
30900+static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
30901+struct attribute *sysaufs_si_attrs[] = {
30902+ &sysaufs_si_attr_xi_path.attr,
30903+ NULL,
30904+};
30905+
4a4d8108 30906+static const struct sysfs_ops au_sbi_ops = {
1facf9fc 30907+ .show = sysaufs_si_show
30908+};
30909+
30910+static struct kobj_type au_sbi_ktype = {
30911+ .release = au_si_free,
30912+ .sysfs_ops = &au_sbi_ops,
30913+ .default_attrs = sysaufs_si_attrs
30914+};
30915+
30916+/* ---------------------------------------------------------------------- */
30917+
30918+int sysaufs_si_init(struct au_sbinfo *sbinfo)
30919+{
30920+ int err;
30921+
e49829fe 30922+ sbinfo->si_kobj.kset = sysaufs_kset;
1facf9fc 30923+ /* cf. sysaufs_name() */
30924+ err = kobject_init_and_add
e49829fe 30925+ (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
1facf9fc 30926+ SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
30927+
1facf9fc 30928+ return err;
30929+}
30930+
30931+void sysaufs_fin(void)
30932+{
e49829fe
JR
30933+ sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
30934+ kset_unregister(sysaufs_kset);
1facf9fc 30935+}
30936+
30937+int __init sysaufs_init(void)
30938+{
30939+ int err;
30940+
30941+ do {
30942+ get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
30943+ } while (!sysaufs_si_mask);
30944+
4a4d8108 30945+ err = -EINVAL;
e49829fe
JR
30946+ sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
30947+ if (unlikely(!sysaufs_kset))
4a4d8108 30948+ goto out;
e49829fe
JR
30949+ err = PTR_ERR(sysaufs_kset);
30950+ if (IS_ERR(sysaufs_kset))
1facf9fc 30951+ goto out;
e49829fe 30952+ err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
062440b3 30953+ if (unlikely(err))
e49829fe 30954+ kset_unregister(sysaufs_kset);
1facf9fc 30955+
4f0767ce 30956+out:
1facf9fc 30957+ return err;
30958+}
7f207e10
AM
30959diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
30960--- /usr/share/empty/fs/aufs/sysaufs.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 30961+++ linux/fs/aufs/sysaufs.h 2018-08-12 23:43:05.463458173 +0200
062440b3
AM
30962@@ -0,0 +1,102 @@
30963+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 30964+/*
b00004a5 30965+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 30966+ *
30967+ * This program, aufs is free software; you can redistribute it and/or modify
30968+ * it under the terms of the GNU General Public License as published by
30969+ * the Free Software Foundation; either version 2 of the License, or
30970+ * (at your option) any later version.
dece6358
AM
30971+ *
30972+ * This program is distributed in the hope that it will be useful,
30973+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30974+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30975+ * GNU General Public License for more details.
30976+ *
30977+ * You should have received a copy of the GNU General Public License
523b37e3 30978+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30979+ */
30980+
30981+/*
30982+ * sysfs interface and mount lifetime management
30983+ */
30984+
30985+#ifndef __SYSAUFS_H__
30986+#define __SYSAUFS_H__
30987+
30988+#ifdef __KERNEL__
30989+
1facf9fc 30990+#include <linux/sysfs.h>
1facf9fc 30991+#include "module.h"
30992+
dece6358
AM
30993+struct super_block;
30994+struct au_sbinfo;
30995+
1facf9fc 30996+struct sysaufs_si_attr {
30997+ struct attribute attr;
30998+ int (*show)(struct seq_file *seq, struct super_block *sb);
30999+};
31000+
31001+/* ---------------------------------------------------------------------- */
31002+
31003+/* sysaufs.c */
31004+extern unsigned long sysaufs_si_mask;
e49829fe 31005+extern struct kset *sysaufs_kset;
1facf9fc 31006+extern struct attribute *sysaufs_si_attrs[];
31007+int sysaufs_si_init(struct au_sbinfo *sbinfo);
31008+int __init sysaufs_init(void);
31009+void sysaufs_fin(void);
31010+
31011+/* ---------------------------------------------------------------------- */
31012+
31013+/* some people doesn't like to show a pointer in kernel */
31014+static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31015+{
31016+ return sysaufs_si_mask ^ (unsigned long)sbinfo;
31017+}
31018+
31019+#define SysaufsSiNamePrefix "si_"
31020+#define SysaufsSiNameLen (sizeof(SysaufsSiNamePrefix) + 16)
31021+static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31022+{
31023+ snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31024+ sysaufs_si_id(sbinfo));
31025+}
31026+
31027+struct au_branch;
31028+#ifdef CONFIG_SYSFS
31029+/* sysfs.c */
31030+extern struct attribute_group *sysaufs_attr_group;
31031+
31032+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31033+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31034+ char *buf);
076b876e
AM
31035+long au_brinfo_ioctl(struct file *file, unsigned long arg);
31036+#ifdef CONFIG_COMPAT
31037+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31038+#endif
1facf9fc 31039+
31040+void sysaufs_br_init(struct au_branch *br);
31041+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31042+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31043+
31044+#define sysaufs_brs_init() do {} while (0)
31045+
31046+#else
31047+#define sysaufs_attr_group NULL
31048+
4a4d8108 31049+AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
c1595e42
JR
31050+AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31051+ struct attribute *attr, char *buf)
4a4d8108
AM
31052+AuStubVoid(sysaufs_br_init, struct au_branch *br)
31053+AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31054+AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
1facf9fc 31055+
31056+static inline void sysaufs_brs_init(void)
31057+{
31058+ sysaufs_brs = 0;
31059+}
31060+
31061+#endif /* CONFIG_SYSFS */
31062+
31063+#endif /* __KERNEL__ */
31064+#endif /* __SYSAUFS_H__ */
7f207e10
AM
31065diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31066--- /usr/share/empty/fs/aufs/sysfs.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 31067+++ linux/fs/aufs/sysfs.c 2018-08-12 23:43:05.463458173 +0200
062440b3 31068@@ -0,0 +1,373 @@
cd7a4cd9 31069+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31070+/*
b00004a5 31071+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 31072+ *
31073+ * This program, aufs is free software; you can redistribute it and/or modify
31074+ * it under the terms of the GNU General Public License as published by
31075+ * the Free Software Foundation; either version 2 of the License, or
31076+ * (at your option) any later version.
dece6358
AM
31077+ *
31078+ * This program is distributed in the hope that it will be useful,
31079+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31080+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31081+ * GNU General Public License for more details.
31082+ *
31083+ * You should have received a copy of the GNU General Public License
523b37e3 31084+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31085+ */
31086+
31087+/*
31088+ * sysfs interface
31089+ */
31090+
076b876e 31091+#include <linux/compat.h>
1facf9fc 31092+#include <linux/seq_file.h>
1facf9fc 31093+#include "aufs.h"
31094+
4a4d8108
AM
31095+#ifdef CONFIG_AUFS_FS_MODULE
31096+/* this entry violates the "one line per file" policy of sysfs */
31097+static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31098+ char *buf)
31099+{
31100+ ssize_t err;
31101+ static char *conf =
31102+/* this file is generated at compiling */
31103+#include "conf.str"
31104+ ;
31105+
31106+ err = snprintf(buf, PAGE_SIZE, conf);
31107+ if (unlikely(err >= PAGE_SIZE))
31108+ err = -EFBIG;
31109+ return err;
31110+}
31111+
31112+static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31113+#endif
31114+
1facf9fc 31115+static struct attribute *au_attr[] = {
4a4d8108
AM
31116+#ifdef CONFIG_AUFS_FS_MODULE
31117+ &au_config_attr.attr,
31118+#endif
1facf9fc 31119+ NULL, /* need to NULL terminate the list of attributes */
31120+};
31121+
31122+static struct attribute_group sysaufs_attr_group_body = {
31123+ .attrs = au_attr
31124+};
31125+
31126+struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31127+
31128+/* ---------------------------------------------------------------------- */
31129+
31130+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31131+{
31132+ int err;
31133+
dece6358
AM
31134+ SiMustAnyLock(sb);
31135+
1facf9fc 31136+ err = 0;
31137+ if (au_opt_test(au_mntflags(sb), XINO)) {
31138+ err = au_xino_path(seq, au_sbi(sb)->si_xib);
31139+ seq_putc(seq, '\n');
31140+ }
31141+ return err;
31142+}
31143+
31144+/*
31145+ * the lifetime of branch is independent from the entry under sysfs.
31146+ * sysfs handles the lifetime of the entry, and never call ->show() after it is
31147+ * unlinked.
31148+ */
31149+static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
392086de 31150+ aufs_bindex_t bindex, int idx)
1facf9fc 31151+{
1e00d052 31152+ int err;
1facf9fc 31153+ struct path path;
31154+ struct dentry *root;
31155+ struct au_branch *br;
076b876e 31156+ au_br_perm_str_t perm;
1facf9fc 31157+
31158+ AuDbg("b%d\n", bindex);
31159+
1e00d052 31160+ err = 0;
1facf9fc 31161+ root = sb->s_root;
31162+ di_read_lock_parent(root, !AuLock_IR);
31163+ br = au_sbr(sb, bindex);
392086de
AM
31164+
31165+ switch (idx) {
31166+ case AuBrSysfs_BR:
31167+ path.mnt = au_br_mnt(br);
31168+ path.dentry = au_h_dptr(root, bindex);
79b8bda9
AM
31169+ err = au_seq_path(seq, &path);
31170+ if (!err) {
31171+ au_optstr_br_perm(&perm, br->br_perm);
31172+ seq_printf(seq, "=%s\n", perm.a);
31173+ }
392086de
AM
31174+ break;
31175+ case AuBrSysfs_BRID:
79b8bda9 31176+ seq_printf(seq, "%d\n", br->br_id);
392086de
AM
31177+ break;
31178+ }
076b876e 31179+ di_read_unlock(root, !AuLock_IR);
79b8bda9 31180+ if (unlikely(err || seq_has_overflowed(seq)))
076b876e 31181+ err = -E2BIG;
392086de 31182+
1e00d052 31183+ return err;
1facf9fc 31184+}
31185+
31186+/* ---------------------------------------------------------------------- */
31187+
31188+static struct seq_file *au_seq(char *p, ssize_t len)
31189+{
31190+ struct seq_file *seq;
31191+
31192+ seq = kzalloc(sizeof(*seq), GFP_NOFS);
31193+ if (seq) {
31194+ /* mutex_init(&seq.lock); */
31195+ seq->buf = p;
31196+ seq->size = len;
31197+ return seq; /* success */
31198+ }
31199+
31200+ seq = ERR_PTR(-ENOMEM);
31201+ return seq;
31202+}
31203+
392086de
AM
31204+#define SysaufsBr_PREFIX "br"
31205+#define SysaufsBrid_PREFIX "brid"
1facf9fc 31206+
31207+/* todo: file size may exceed PAGE_SIZE */
31208+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
1308ab2a 31209+ char *buf)
1facf9fc 31210+{
31211+ ssize_t err;
392086de 31212+ int idx;
1facf9fc 31213+ long l;
5afbbe0d 31214+ aufs_bindex_t bbot;
1facf9fc 31215+ struct au_sbinfo *sbinfo;
31216+ struct super_block *sb;
31217+ struct seq_file *seq;
31218+ char *name;
31219+ struct attribute **cattr;
31220+
31221+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31222+ sb = sbinfo->si_sb;
1308ab2a 31223+
31224+ /*
31225+ * prevent a race condition between sysfs and aufs.
31226+ * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31227+ * prohibits maintaining the sysfs entries.
31228+ * hew we acquire read lock after sysfs_get_active_two().
31229+ * on the other hand, the remount process may maintain the sysfs/aufs
31230+ * entries after acquiring write lock.
31231+ * it can cause a deadlock.
31232+ * simply we gave up processing read here.
31233+ */
31234+ err = -EBUSY;
31235+ if (unlikely(!si_noflush_read_trylock(sb)))
31236+ goto out;
1facf9fc 31237+
31238+ seq = au_seq(buf, PAGE_SIZE);
31239+ err = PTR_ERR(seq);
31240+ if (IS_ERR(seq))
1308ab2a 31241+ goto out_unlock;
1facf9fc 31242+
31243+ name = (void *)attr->name;
31244+ cattr = sysaufs_si_attrs;
31245+ while (*cattr) {
31246+ if (!strcmp(name, (*cattr)->name)) {
31247+ err = container_of(*cattr, struct sysaufs_si_attr, attr)
31248+ ->show(seq, sb);
31249+ goto out_seq;
31250+ }
31251+ cattr++;
31252+ }
31253+
392086de
AM
31254+ if (!strncmp(name, SysaufsBrid_PREFIX,
31255+ sizeof(SysaufsBrid_PREFIX) - 1)) {
31256+ idx = AuBrSysfs_BRID;
31257+ name += sizeof(SysaufsBrid_PREFIX) - 1;
31258+ } else if (!strncmp(name, SysaufsBr_PREFIX,
31259+ sizeof(SysaufsBr_PREFIX) - 1)) {
31260+ idx = AuBrSysfs_BR;
1facf9fc 31261+ name += sizeof(SysaufsBr_PREFIX) - 1;
392086de
AM
31262+ } else
31263+ BUG();
31264+
31265+ err = kstrtol(name, 10, &l);
31266+ if (!err) {
5afbbe0d
AM
31267+ bbot = au_sbbot(sb);
31268+ if (l <= bbot)
392086de
AM
31269+ err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31270+ else
31271+ err = -ENOENT;
1facf9fc 31272+ }
1facf9fc 31273+
4f0767ce 31274+out_seq:
1facf9fc 31275+ if (!err) {
31276+ err = seq->count;
31277+ /* sysfs limit */
31278+ if (unlikely(err == PAGE_SIZE))
31279+ err = -EFBIG;
31280+ }
1c60b727 31281+ kfree(seq);
4f0767ce 31282+out_unlock:
1facf9fc 31283+ si_read_unlock(sb);
4f0767ce 31284+out:
1facf9fc 31285+ return err;
31286+}
31287+
31288+/* ---------------------------------------------------------------------- */
31289+
076b876e
AM
31290+static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31291+{
31292+ int err;
31293+ int16_t brid;
5afbbe0d 31294+ aufs_bindex_t bindex, bbot;
076b876e
AM
31295+ size_t sz;
31296+ char *buf;
31297+ struct seq_file *seq;
31298+ struct au_branch *br;
31299+
31300+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d
AM
31301+ bbot = au_sbbot(sb);
31302+ err = bbot + 1;
076b876e
AM
31303+ if (!arg)
31304+ goto out;
31305+
31306+ err = -ENOMEM;
31307+ buf = (void *)__get_free_page(GFP_NOFS);
31308+ if (unlikely(!buf))
31309+ goto out;
31310+
31311+ seq = au_seq(buf, PAGE_SIZE);
31312+ err = PTR_ERR(seq);
31313+ if (IS_ERR(seq))
31314+ goto out_buf;
31315+
31316+ sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
5afbbe0d 31317+ for (bindex = 0; bindex <= bbot; bindex++, arg++) {
076b876e
AM
31318+ err = !access_ok(VERIFY_WRITE, arg, sizeof(*arg));
31319+ if (unlikely(err))
31320+ break;
31321+
31322+ br = au_sbr(sb, bindex);
31323+ brid = br->br_id;
31324+ BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31325+ err = __put_user(brid, &arg->id);
31326+ if (unlikely(err))
31327+ break;
31328+
31329+ BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31330+ err = __put_user(br->br_perm, &arg->perm);
31331+ if (unlikely(err))
31332+ break;
31333+
79b8bda9
AM
31334+ err = au_seq_path(seq, &br->br_path);
31335+ if (unlikely(err))
31336+ break;
31337+ seq_putc(seq, '\0');
31338+ if (!seq_has_overflowed(seq)) {
076b876e
AM
31339+ err = copy_to_user(arg->path, seq->buf, seq->count);
31340+ seq->count = 0;
31341+ if (unlikely(err))
31342+ break;
31343+ } else {
31344+ err = -E2BIG;
31345+ goto out_seq;
31346+ }
31347+ }
31348+ if (unlikely(err))
31349+ err = -EFAULT;
31350+
31351+out_seq:
1c60b727 31352+ kfree(seq);
076b876e 31353+out_buf:
1c60b727 31354+ free_page((unsigned long)buf);
076b876e
AM
31355+out:
31356+ si_read_unlock(sb);
31357+ return err;
31358+}
31359+
31360+long au_brinfo_ioctl(struct file *file, unsigned long arg)
31361+{
2000de60 31362+ return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
076b876e
AM
31363+}
31364+
31365+#ifdef CONFIG_COMPAT
31366+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31367+{
2000de60 31368+ return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
076b876e
AM
31369+}
31370+#endif
31371+
31372+/* ---------------------------------------------------------------------- */
31373+
1facf9fc 31374+void sysaufs_br_init(struct au_branch *br)
31375+{
392086de
AM
31376+ int i;
31377+ struct au_brsysfs *br_sysfs;
31378+ struct attribute *attr;
4a4d8108 31379+
392086de
AM
31380+ br_sysfs = br->br_sysfs;
31381+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31382+ attr = &br_sysfs->attr;
31383+ sysfs_attr_init(attr);
31384+ attr->name = br_sysfs->name;
cd7a4cd9 31385+ attr->mode = 0444;
392086de
AM
31386+ br_sysfs++;
31387+ }
1facf9fc 31388+}
31389+
31390+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31391+{
31392+ struct au_branch *br;
31393+ struct kobject *kobj;
392086de
AM
31394+ struct au_brsysfs *br_sysfs;
31395+ int i;
5afbbe0d 31396+ aufs_bindex_t bbot;
1facf9fc 31397+
1facf9fc 31398+ if (!sysaufs_brs)
31399+ return;
31400+
31401+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
31402+ bbot = au_sbbot(sb);
31403+ for (; bindex <= bbot; bindex++) {
1facf9fc 31404+ br = au_sbr(sb, bindex);
392086de
AM
31405+ br_sysfs = br->br_sysfs;
31406+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31407+ sysfs_remove_file(kobj, &br_sysfs->attr);
31408+ br_sysfs++;
31409+ }
1facf9fc 31410+ }
31411+}
31412+
31413+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31414+{
392086de 31415+ int err, i;
5afbbe0d 31416+ aufs_bindex_t bbot;
1facf9fc 31417+ struct kobject *kobj;
31418+ struct au_branch *br;
392086de 31419+ struct au_brsysfs *br_sysfs;
1facf9fc 31420+
1facf9fc 31421+ if (!sysaufs_brs)
31422+ return;
31423+
31424+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
31425+ bbot = au_sbbot(sb);
31426+ for (; bindex <= bbot; bindex++) {
1facf9fc 31427+ br = au_sbr(sb, bindex);
392086de
AM
31428+ br_sysfs = br->br_sysfs;
31429+ snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31430+ SysaufsBr_PREFIX "%d", bindex);
31431+ snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31432+ SysaufsBrid_PREFIX "%d", bindex);
31433+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31434+ err = sysfs_create_file(kobj, &br_sysfs->attr);
31435+ if (unlikely(err))
31436+ pr_warn("failed %s under sysfs(%d)\n",
31437+ br_sysfs->name, err);
31438+ br_sysfs++;
31439+ }
1facf9fc 31440+ }
31441+}
7f207e10
AM
31442diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31443--- /usr/share/empty/fs/aufs/sysrq.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 31444+++ linux/fs/aufs/sysrq.c 2018-08-12 23:43:05.463458173 +0200
062440b3 31445@@ -0,0 +1,160 @@
cd7a4cd9 31446+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31447+/*
b00004a5 31448+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 31449+ *
31450+ * This program, aufs is free software; you can redistribute it and/or modify
31451+ * it under the terms of the GNU General Public License as published by
31452+ * the Free Software Foundation; either version 2 of the License, or
31453+ * (at your option) any later version.
dece6358
AM
31454+ *
31455+ * This program is distributed in the hope that it will be useful,
31456+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31457+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31458+ * GNU General Public License for more details.
31459+ *
31460+ * You should have received a copy of the GNU General Public License
523b37e3 31461+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31462+ */
31463+
31464+/*
31465+ * magic sysrq hanlder
31466+ */
31467+
1facf9fc 31468+/* #include <linux/sysrq.h> */
027c5e7a 31469+#include <linux/writeback.h>
1facf9fc 31470+#include "aufs.h"
31471+
31472+/* ---------------------------------------------------------------------- */
31473+
31474+static void sysrq_sb(struct super_block *sb)
31475+{
31476+ char *plevel;
31477+ struct au_sbinfo *sbinfo;
31478+ struct file *file;
8b6a4947
AM
31479+ struct hlist_bl_head *files;
31480+ struct hlist_bl_node *pos;
523b37e3 31481+ struct au_finfo *finfo;
1facf9fc 31482+
31483+ plevel = au_plevel;
31484+ au_plevel = KERN_WARNING;
1facf9fc 31485+
4a4d8108 31486+ /* since we define pr_fmt, call printk directly */
c06a8ce3
AM
31487+#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31488+
31489+ sbinfo = au_sbi(sb);
4a4d8108 31490+ printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
c06a8ce3 31491+ pr("superblock\n");
1facf9fc 31492+ au_dpri_sb(sb);
027c5e7a
AM
31493+
31494+#if 0
c06a8ce3 31495+ pr("root dentry\n");
1facf9fc 31496+ au_dpri_dentry(sb->s_root);
c06a8ce3 31497+ pr("root inode\n");
5527c038 31498+ au_dpri_inode(d_inode(sb->s_root));
027c5e7a
AM
31499+#endif
31500+
1facf9fc 31501+#if 0
027c5e7a
AM
31502+ do {
31503+ int err, i, j, ndentry;
31504+ struct au_dcsub_pages dpages;
31505+ struct au_dpage *dpage;
31506+
31507+ err = au_dpages_init(&dpages, GFP_ATOMIC);
31508+ if (unlikely(err))
31509+ break;
31510+ err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31511+ if (!err)
31512+ for (i = 0; i < dpages.ndpage; i++) {
31513+ dpage = dpages.dpages + i;
31514+ ndentry = dpage->ndentry;
31515+ for (j = 0; j < ndentry; j++)
31516+ au_dpri_dentry(dpage->dentries[j]);
31517+ }
31518+ au_dpages_free(&dpages);
31519+ } while (0);
31520+#endif
31521+
31522+#if 1
31523+ {
31524+ struct inode *i;
076b876e 31525+
c06a8ce3 31526+ pr("isolated inode\n");
79b8bda9 31527+ spin_lock(&sb->s_inode_list_lock);
2cbb1c4b
JR
31528+ list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31529+ spin_lock(&i->i_lock);
b4510431 31530+ if (1 || hlist_empty(&i->i_dentry))
027c5e7a 31531+ au_dpri_inode(i);
2cbb1c4b
JR
31532+ spin_unlock(&i->i_lock);
31533+ }
79b8bda9 31534+ spin_unlock(&sb->s_inode_list_lock);
027c5e7a 31535+ }
1facf9fc 31536+#endif
c06a8ce3 31537+ pr("files\n");
523b37e3 31538+ files = &au_sbi(sb)->si_files;
8b6a4947
AM
31539+ hlist_bl_lock(files);
31540+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
4a4d8108 31541+ umode_t mode;
076b876e 31542+
523b37e3 31543+ file = finfo->fi_file;
c06a8ce3 31544+ mode = file_inode(file)->i_mode;
38d290e6 31545+ if (!special_file(mode))
1facf9fc 31546+ au_dpri_file(file);
523b37e3 31547+ }
8b6a4947 31548+ hlist_bl_unlock(files);
c06a8ce3 31549+ pr("done\n");
1facf9fc 31550+
c06a8ce3 31551+#undef pr
1facf9fc 31552+ au_plevel = plevel;
1facf9fc 31553+}
31554+
31555+/* ---------------------------------------------------------------------- */
31556+
31557+/* module parameter */
31558+static char *aufs_sysrq_key = "a";
cd7a4cd9 31559+module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
1facf9fc 31560+MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31561+
0c5527e5 31562+static void au_sysrq(int key __maybe_unused)
1facf9fc 31563+{
1facf9fc 31564+ struct au_sbinfo *sbinfo;
8b6a4947 31565+ struct hlist_bl_node *pos;
1facf9fc 31566+
027c5e7a 31567+ lockdep_off();
53392da6 31568+ au_sbilist_lock();
8b6a4947 31569+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
1facf9fc 31570+ sysrq_sb(sbinfo->si_sb);
53392da6 31571+ au_sbilist_unlock();
027c5e7a 31572+ lockdep_on();
1facf9fc 31573+}
31574+
31575+static struct sysrq_key_op au_sysrq_op = {
31576+ .handler = au_sysrq,
31577+ .help_msg = "Aufs",
31578+ .action_msg = "Aufs",
31579+ .enable_mask = SYSRQ_ENABLE_DUMP
31580+};
31581+
31582+/* ---------------------------------------------------------------------- */
31583+
31584+int __init au_sysrq_init(void)
31585+{
31586+ int err;
31587+ char key;
31588+
31589+ err = -1;
31590+ key = *aufs_sysrq_key;
31591+ if ('a' <= key && key <= 'z')
31592+ err = register_sysrq_key(key, &au_sysrq_op);
31593+ if (unlikely(err))
4a4d8108 31594+ pr_err("err %d, sysrq=%c\n", err, key);
1facf9fc 31595+ return err;
31596+}
31597+
31598+void au_sysrq_fin(void)
31599+{
31600+ int err;
076b876e 31601+
1facf9fc 31602+ err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31603+ if (unlikely(err))
4a4d8108 31604+ pr_err("err %d (ignored)\n", err);
1facf9fc 31605+}
7f207e10
AM
31606diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
31607--- /usr/share/empty/fs/aufs/vdir.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 31608+++ linux/fs/aufs/vdir.c 2018-08-12 23:43:05.463458173 +0200
062440b3 31609@@ -0,0 +1,894 @@
cd7a4cd9 31610+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31611+/*
b00004a5 31612+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 31613+ *
31614+ * This program, aufs is free software; you can redistribute it and/or modify
31615+ * it under the terms of the GNU General Public License as published by
31616+ * the Free Software Foundation; either version 2 of the License, or
31617+ * (at your option) any later version.
dece6358
AM
31618+ *
31619+ * This program is distributed in the hope that it will be useful,
31620+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31621+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31622+ * GNU General Public License for more details.
31623+ *
31624+ * You should have received a copy of the GNU General Public License
523b37e3 31625+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31626+ */
31627+
31628+/*
31629+ * virtual or vertical directory
31630+ */
31631+
31632+#include "aufs.h"
31633+
dece6358 31634+static unsigned int calc_size(int nlen)
1facf9fc 31635+{
dece6358 31636+ return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
1facf9fc 31637+}
31638+
31639+static int set_deblk_end(union au_vdir_deblk_p *p,
31640+ union au_vdir_deblk_p *deblk_end)
31641+{
31642+ if (calc_size(0) <= deblk_end->deblk - p->deblk) {
31643+ p->de->de_str.len = 0;
31644+ /* smp_mb(); */
31645+ return 0;
31646+ }
31647+ return -1; /* error */
31648+}
31649+
31650+/* returns true or false */
31651+static int is_deblk_end(union au_vdir_deblk_p *p,
31652+ union au_vdir_deblk_p *deblk_end)
31653+{
31654+ if (calc_size(0) <= deblk_end->deblk - p->deblk)
31655+ return !p->de->de_str.len;
31656+ return 1;
31657+}
31658+
31659+static unsigned char *last_deblk(struct au_vdir *vdir)
31660+{
31661+ return vdir->vd_deblk[vdir->vd_nblk - 1];
31662+}
31663+
31664+/* ---------------------------------------------------------------------- */
31665+
79b8bda9 31666+/* estimate the appropriate size for name hash table */
1308ab2a 31667+unsigned int au_rdhash_est(loff_t sz)
31668+{
31669+ unsigned int n;
31670+
31671+ n = UINT_MAX;
31672+ sz >>= 10;
31673+ if (sz < n)
31674+ n = sz;
31675+ if (sz < AUFS_RDHASH_DEF)
31676+ n = AUFS_RDHASH_DEF;
4a4d8108 31677+ /* pr_info("n %u\n", n); */
1308ab2a 31678+ return n;
31679+}
31680+
1facf9fc 31681+/*
31682+ * the allocated memory has to be freed by
dece6358 31683+ * au_nhash_wh_free() or au_nhash_de_free().
1facf9fc 31684+ */
dece6358 31685+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
1facf9fc 31686+{
1facf9fc 31687+ struct hlist_head *head;
dece6358 31688+ unsigned int u;
076b876e 31689+ size_t sz;
1facf9fc 31690+
076b876e
AM
31691+ sz = sizeof(*nhash->nh_head) * num_hash;
31692+ head = kmalloc(sz, gfp);
dece6358
AM
31693+ if (head) {
31694+ nhash->nh_num = num_hash;
31695+ nhash->nh_head = head;
31696+ for (u = 0; u < num_hash; u++)
1facf9fc 31697+ INIT_HLIST_HEAD(head++);
dece6358 31698+ return 0; /* success */
1facf9fc 31699+ }
1facf9fc 31700+
dece6358 31701+ return -ENOMEM;
1facf9fc 31702+}
31703+
dece6358
AM
31704+static void nhash_count(struct hlist_head *head)
31705+{
31706+#if 0
31707+ unsigned long n;
31708+ struct hlist_node *pos;
31709+
31710+ n = 0;
31711+ hlist_for_each(pos, head)
31712+ n++;
4a4d8108 31713+ pr_info("%lu\n", n);
dece6358
AM
31714+#endif
31715+}
31716+
31717+static void au_nhash_wh_do_free(struct hlist_head *head)
1facf9fc 31718+{
c06a8ce3
AM
31719+ struct au_vdir_wh *pos;
31720+ struct hlist_node *node;
1facf9fc 31721+
c06a8ce3 31722+ hlist_for_each_entry_safe(pos, node, head, wh_hash)
1c60b727 31723+ kfree(pos);
1facf9fc 31724+}
31725+
dece6358 31726+static void au_nhash_de_do_free(struct hlist_head *head)
1facf9fc 31727+{
c06a8ce3
AM
31728+ struct au_vdir_dehstr *pos;
31729+ struct hlist_node *node;
1facf9fc 31730+
c06a8ce3 31731+ hlist_for_each_entry_safe(pos, node, head, hash)
1c60b727 31732+ au_cache_free_vdir_dehstr(pos);
1facf9fc 31733+}
31734+
dece6358
AM
31735+static void au_nhash_do_free(struct au_nhash *nhash,
31736+ void (*free)(struct hlist_head *head))
1facf9fc 31737+{
1308ab2a 31738+ unsigned int n;
1facf9fc 31739+ struct hlist_head *head;
1facf9fc 31740+
dece6358 31741+ n = nhash->nh_num;
1308ab2a 31742+ if (!n)
31743+ return;
31744+
dece6358 31745+ head = nhash->nh_head;
1308ab2a 31746+ while (n-- > 0) {
dece6358
AM
31747+ nhash_count(head);
31748+ free(head++);
1facf9fc 31749+ }
1c60b727 31750+ kfree(nhash->nh_head);
1facf9fc 31751+}
31752+
dece6358 31753+void au_nhash_wh_free(struct au_nhash *whlist)
1facf9fc 31754+{
dece6358
AM
31755+ au_nhash_do_free(whlist, au_nhash_wh_do_free);
31756+}
1facf9fc 31757+
dece6358
AM
31758+static void au_nhash_de_free(struct au_nhash *delist)
31759+{
31760+ au_nhash_do_free(delist, au_nhash_de_do_free);
1facf9fc 31761+}
31762+
31763+/* ---------------------------------------------------------------------- */
31764+
31765+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
31766+ int limit)
31767+{
31768+ int num;
31769+ unsigned int u, n;
31770+ struct hlist_head *head;
c06a8ce3 31771+ struct au_vdir_wh *pos;
1facf9fc 31772+
31773+ num = 0;
31774+ n = whlist->nh_num;
31775+ head = whlist->nh_head;
1308ab2a 31776+ for (u = 0; u < n; u++, head++)
c06a8ce3
AM
31777+ hlist_for_each_entry(pos, head, wh_hash)
31778+ if (pos->wh_bindex == btgt && ++num > limit)
1facf9fc 31779+ return 1;
1facf9fc 31780+ return 0;
31781+}
31782+
31783+static struct hlist_head *au_name_hash(struct au_nhash *nhash,
dece6358 31784+ unsigned char *name,
1facf9fc 31785+ unsigned int len)
31786+{
dece6358
AM
31787+ unsigned int v;
31788+ /* const unsigned int magic_bit = 12; */
31789+
1308ab2a 31790+ AuDebugOn(!nhash->nh_num || !nhash->nh_head);
31791+
dece6358 31792+ v = 0;
f0c0a007
AM
31793+ if (len > 8)
31794+ len = 8;
dece6358
AM
31795+ while (len--)
31796+ v += *name++;
31797+ /* v = hash_long(v, magic_bit); */
31798+ v %= nhash->nh_num;
31799+ return nhash->nh_head + v;
31800+}
31801+
31802+static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
31803+ int nlen)
31804+{
31805+ return str->len == nlen && !memcmp(str->name, name, nlen);
1facf9fc 31806+}
31807+
31808+/* returns found or not */
dece6358 31809+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
1facf9fc 31810+{
31811+ struct hlist_head *head;
c06a8ce3 31812+ struct au_vdir_wh *pos;
1facf9fc 31813+ struct au_vdir_destr *str;
31814+
dece6358 31815+ head = au_name_hash(whlist, name, nlen);
c06a8ce3
AM
31816+ hlist_for_each_entry(pos, head, wh_hash) {
31817+ str = &pos->wh_str;
1facf9fc 31818+ AuDbg("%.*s\n", str->len, str->name);
dece6358
AM
31819+ if (au_nhash_test_name(str, name, nlen))
31820+ return 1;
31821+ }
31822+ return 0;
31823+}
31824+
31825+/* returns found(true) or not */
31826+static int test_known(struct au_nhash *delist, char *name, int nlen)
31827+{
31828+ struct hlist_head *head;
c06a8ce3 31829+ struct au_vdir_dehstr *pos;
dece6358
AM
31830+ struct au_vdir_destr *str;
31831+
31832+ head = au_name_hash(delist, name, nlen);
c06a8ce3
AM
31833+ hlist_for_each_entry(pos, head, hash) {
31834+ str = pos->str;
dece6358
AM
31835+ AuDbg("%.*s\n", str->len, str->name);
31836+ if (au_nhash_test_name(str, name, nlen))
1facf9fc 31837+ return 1;
31838+ }
31839+ return 0;
31840+}
31841+
dece6358
AM
31842+static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
31843+ unsigned char d_type)
31844+{
31845+#ifdef CONFIG_AUFS_SHWH
31846+ wh->wh_ino = ino;
31847+ wh->wh_type = d_type;
31848+#endif
31849+}
31850+
31851+/* ---------------------------------------------------------------------- */
31852+
31853+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
31854+ unsigned int d_type, aufs_bindex_t bindex,
31855+ unsigned char shwh)
1facf9fc 31856+{
31857+ int err;
31858+ struct au_vdir_destr *str;
31859+ struct au_vdir_wh *wh;
31860+
dece6358 31861+ AuDbg("%.*s\n", nlen, name);
1308ab2a 31862+ AuDebugOn(!whlist->nh_num || !whlist->nh_head);
31863+
1facf9fc 31864+ err = -ENOMEM;
dece6358 31865+ wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
1facf9fc 31866+ if (unlikely(!wh))
31867+ goto out;
31868+
31869+ err = 0;
31870+ wh->wh_bindex = bindex;
dece6358
AM
31871+ if (shwh)
31872+ au_shwh_init_wh(wh, ino, d_type);
1facf9fc 31873+ str = &wh->wh_str;
dece6358
AM
31874+ str->len = nlen;
31875+ memcpy(str->name, name, nlen);
31876+ hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
1facf9fc 31877+ /* smp_mb(); */
31878+
4f0767ce 31879+out:
1facf9fc 31880+ return err;
31881+}
31882+
1facf9fc 31883+static int append_deblk(struct au_vdir *vdir)
31884+{
31885+ int err;
dece6358 31886+ unsigned long ul;
1facf9fc 31887+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
31888+ union au_vdir_deblk_p p, deblk_end;
31889+ unsigned char **o;
31890+
31891+ err = -ENOMEM;
e2f27e51
AM
31892+ o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
31893+ GFP_NOFS, /*may_shrink*/0);
1facf9fc 31894+ if (unlikely(!o))
31895+ goto out;
31896+
31897+ vdir->vd_deblk = o;
31898+ p.deblk = kmalloc(deblk_sz, GFP_NOFS);
31899+ if (p.deblk) {
31900+ ul = vdir->vd_nblk++;
31901+ vdir->vd_deblk[ul] = p.deblk;
31902+ vdir->vd_last.ul = ul;
31903+ vdir->vd_last.p.deblk = p.deblk;
31904+ deblk_end.deblk = p.deblk + deblk_sz;
31905+ err = set_deblk_end(&p, &deblk_end);
31906+ }
31907+
4f0767ce 31908+out:
1facf9fc 31909+ return err;
31910+}
31911+
dece6358
AM
31912+static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
31913+ unsigned int d_type, struct au_nhash *delist)
31914+{
31915+ int err;
31916+ unsigned int sz;
31917+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
31918+ union au_vdir_deblk_p p, *room, deblk_end;
31919+ struct au_vdir_dehstr *dehstr;
31920+
31921+ p.deblk = last_deblk(vdir);
31922+ deblk_end.deblk = p.deblk + deblk_sz;
31923+ room = &vdir->vd_last.p;
31924+ AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
31925+ || !is_deblk_end(room, &deblk_end));
31926+
31927+ sz = calc_size(nlen);
31928+ if (unlikely(sz > deblk_end.deblk - room->deblk)) {
31929+ err = append_deblk(vdir);
31930+ if (unlikely(err))
31931+ goto out;
31932+
31933+ p.deblk = last_deblk(vdir);
31934+ deblk_end.deblk = p.deblk + deblk_sz;
31935+ /* smp_mb(); */
31936+ AuDebugOn(room->deblk != p.deblk);
31937+ }
31938+
31939+ err = -ENOMEM;
4a4d8108 31940+ dehstr = au_cache_alloc_vdir_dehstr();
dece6358
AM
31941+ if (unlikely(!dehstr))
31942+ goto out;
31943+
31944+ dehstr->str = &room->de->de_str;
31945+ hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
31946+ room->de->de_ino = ino;
31947+ room->de->de_type = d_type;
31948+ room->de->de_str.len = nlen;
31949+ memcpy(room->de->de_str.name, name, nlen);
31950+
31951+ err = 0;
31952+ room->deblk += sz;
31953+ if (unlikely(set_deblk_end(room, &deblk_end)))
31954+ err = append_deblk(vdir);
31955+ /* smp_mb(); */
31956+
4f0767ce 31957+out:
dece6358
AM
31958+ return err;
31959+}
31960+
31961+/* ---------------------------------------------------------------------- */
31962+
1c60b727 31963+void au_vdir_free(struct au_vdir *vdir)
dece6358
AM
31964+{
31965+ unsigned char **deblk;
31966+
31967+ deblk = vdir->vd_deblk;
1c60b727
AM
31968+ while (vdir->vd_nblk--)
31969+ kfree(*deblk++);
31970+ kfree(vdir->vd_deblk);
31971+ au_cache_free_vdir(vdir);
dece6358
AM
31972+}
31973+
1308ab2a 31974+static struct au_vdir *alloc_vdir(struct file *file)
1facf9fc 31975+{
31976+ struct au_vdir *vdir;
1308ab2a 31977+ struct super_block *sb;
1facf9fc 31978+ int err;
31979+
2000de60 31980+ sb = file->f_path.dentry->d_sb;
dece6358
AM
31981+ SiMustAnyLock(sb);
31982+
1facf9fc 31983+ err = -ENOMEM;
31984+ vdir = au_cache_alloc_vdir();
31985+ if (unlikely(!vdir))
31986+ goto out;
31987+
31988+ vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
31989+ if (unlikely(!vdir->vd_deblk))
31990+ goto out_free;
31991+
31992+ vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
1308ab2a 31993+ if (!vdir->vd_deblk_sz) {
79b8bda9 31994+ /* estimate the appropriate size for deblk */
1308ab2a 31995+ vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
4a4d8108 31996+ /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
1308ab2a 31997+ }
1facf9fc 31998+ vdir->vd_nblk = 0;
31999+ vdir->vd_version = 0;
32000+ vdir->vd_jiffy = 0;
32001+ err = append_deblk(vdir);
32002+ if (!err)
32003+ return vdir; /* success */
32004+
1c60b727 32005+ kfree(vdir->vd_deblk);
1facf9fc 32006+
4f0767ce 32007+out_free:
1c60b727 32008+ au_cache_free_vdir(vdir);
4f0767ce 32009+out:
1facf9fc 32010+ vdir = ERR_PTR(err);
32011+ return vdir;
32012+}
32013+
32014+static int reinit_vdir(struct au_vdir *vdir)
32015+{
32016+ int err;
32017+ union au_vdir_deblk_p p, deblk_end;
32018+
32019+ while (vdir->vd_nblk > 1) {
1c60b727 32020+ kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
1facf9fc 32021+ /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32022+ vdir->vd_nblk--;
32023+ }
32024+ p.deblk = vdir->vd_deblk[0];
32025+ deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32026+ err = set_deblk_end(&p, &deblk_end);
32027+ /* keep vd_dblk_sz */
32028+ vdir->vd_last.ul = 0;
32029+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32030+ vdir->vd_version = 0;
32031+ vdir->vd_jiffy = 0;
32032+ /* smp_mb(); */
32033+ return err;
32034+}
32035+
32036+/* ---------------------------------------------------------------------- */
32037+
1facf9fc 32038+#define AuFillVdir_CALLED 1
32039+#define AuFillVdir_WHABLE (1 << 1)
dece6358 32040+#define AuFillVdir_SHWH (1 << 2)
1facf9fc 32041+#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
7f207e10
AM
32042+#define au_fset_fillvdir(flags, name) \
32043+ do { (flags) |= AuFillVdir_##name; } while (0)
32044+#define au_fclr_fillvdir(flags, name) \
32045+ do { (flags) &= ~AuFillVdir_##name; } while (0)
1facf9fc 32046+
dece6358
AM
32047+#ifndef CONFIG_AUFS_SHWH
32048+#undef AuFillVdir_SHWH
32049+#define AuFillVdir_SHWH 0
32050+#endif
32051+
1facf9fc 32052+struct fillvdir_arg {
392086de 32053+ struct dir_context ctx;
1facf9fc 32054+ struct file *file;
32055+ struct au_vdir *vdir;
dece6358
AM
32056+ struct au_nhash delist;
32057+ struct au_nhash whlist;
1facf9fc 32058+ aufs_bindex_t bindex;
32059+ unsigned int flags;
32060+ int err;
32061+};
32062+
392086de 32063+static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
1facf9fc 32064+ loff_t offset __maybe_unused, u64 h_ino,
32065+ unsigned int d_type)
32066+{
392086de 32067+ struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
1facf9fc 32068+ char *name = (void *)__name;
32069+ struct super_block *sb;
1facf9fc 32070+ ino_t ino;
dece6358 32071+ const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
1facf9fc 32072+
1facf9fc 32073+ arg->err = 0;
2000de60 32074+ sb = arg->file->f_path.dentry->d_sb;
1facf9fc 32075+ au_fset_fillvdir(arg->flags, CALLED);
32076+ /* smp_mb(); */
dece6358 32077+ if (nlen <= AUFS_WH_PFX_LEN
1facf9fc 32078+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
dece6358
AM
32079+ if (test_known(&arg->delist, name, nlen)
32080+ || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32081+ goto out; /* already exists or whiteouted */
1facf9fc 32082+
dece6358 32083+ arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
4a4d8108
AM
32084+ if (!arg->err) {
32085+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
32086+ d_type = DT_UNKNOWN;
dece6358
AM
32087+ arg->err = append_de(arg->vdir, name, nlen, ino,
32088+ d_type, &arg->delist);
4a4d8108 32089+ }
1facf9fc 32090+ } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32091+ name += AUFS_WH_PFX_LEN;
dece6358
AM
32092+ nlen -= AUFS_WH_PFX_LEN;
32093+ if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32094+ goto out; /* already whiteouted */
1facf9fc 32095+
dece6358
AM
32096+ if (shwh)
32097+ arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32098+ &ino);
4a4d8108
AM
32099+ if (!arg->err) {
32100+ if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32101+ d_type = DT_UNKNOWN;
1facf9fc 32102+ arg->err = au_nhash_append_wh
dece6358
AM
32103+ (&arg->whlist, name, nlen, ino, d_type,
32104+ arg->bindex, shwh);
4a4d8108 32105+ }
1facf9fc 32106+ }
32107+
4f0767ce 32108+out:
1facf9fc 32109+ if (!arg->err)
32110+ arg->vdir->vd_jiffy = jiffies;
32111+ /* smp_mb(); */
32112+ AuTraceErr(arg->err);
32113+ return arg->err;
32114+}
32115+
dece6358
AM
32116+static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32117+ struct au_nhash *whlist, struct au_nhash *delist)
32118+{
32119+#ifdef CONFIG_AUFS_SHWH
32120+ int err;
32121+ unsigned int nh, u;
32122+ struct hlist_head *head;
c06a8ce3
AM
32123+ struct au_vdir_wh *pos;
32124+ struct hlist_node *n;
dece6358
AM
32125+ char *p, *o;
32126+ struct au_vdir_destr *destr;
32127+
32128+ AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32129+
32130+ err = -ENOMEM;
537831f9 32131+ o = p = (void *)__get_free_page(GFP_NOFS);
dece6358
AM
32132+ if (unlikely(!p))
32133+ goto out;
32134+
32135+ err = 0;
32136+ nh = whlist->nh_num;
32137+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32138+ p += AUFS_WH_PFX_LEN;
32139+ for (u = 0; u < nh; u++) {
32140+ head = whlist->nh_head + u;
c06a8ce3
AM
32141+ hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32142+ destr = &pos->wh_str;
dece6358
AM
32143+ memcpy(p, destr->name, destr->len);
32144+ err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
c06a8ce3 32145+ pos->wh_ino, pos->wh_type, delist);
dece6358
AM
32146+ if (unlikely(err))
32147+ break;
32148+ }
32149+ }
32150+
1c60b727 32151+ free_page((unsigned long)o);
dece6358 32152+
4f0767ce 32153+out:
dece6358
AM
32154+ AuTraceErr(err);
32155+ return err;
32156+#else
32157+ return 0;
32158+#endif
32159+}
32160+
1facf9fc 32161+static int au_do_read_vdir(struct fillvdir_arg *arg)
32162+{
32163+ int err;
dece6358 32164+ unsigned int rdhash;
1facf9fc 32165+ loff_t offset;
5afbbe0d 32166+ aufs_bindex_t bbot, bindex, btop;
dece6358 32167+ unsigned char shwh;
1facf9fc 32168+ struct file *hf, *file;
32169+ struct super_block *sb;
32170+
1facf9fc 32171+ file = arg->file;
2000de60 32172+ sb = file->f_path.dentry->d_sb;
dece6358
AM
32173+ SiMustAnyLock(sb);
32174+
32175+ rdhash = au_sbi(sb)->si_rdhash;
1308ab2a 32176+ if (!rdhash)
32177+ rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
dece6358
AM
32178+ err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32179+ if (unlikely(err))
1facf9fc 32180+ goto out;
dece6358
AM
32181+ err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32182+ if (unlikely(err))
1facf9fc 32183+ goto out_delist;
32184+
32185+ err = 0;
32186+ arg->flags = 0;
dece6358
AM
32187+ shwh = 0;
32188+ if (au_opt_test(au_mntflags(sb), SHWH)) {
32189+ shwh = 1;
32190+ au_fset_fillvdir(arg->flags, SHWH);
32191+ }
5afbbe0d
AM
32192+ btop = au_fbtop(file);
32193+ bbot = au_fbbot_dir(file);
32194+ for (bindex = btop; !err && bindex <= bbot; bindex++) {
4a4d8108 32195+ hf = au_hf_dir(file, bindex);
1facf9fc 32196+ if (!hf)
32197+ continue;
32198+
32199+ offset = vfsub_llseek(hf, 0, SEEK_SET);
32200+ err = offset;
32201+ if (unlikely(offset))
32202+ break;
32203+
32204+ arg->bindex = bindex;
32205+ au_fclr_fillvdir(arg->flags, WHABLE);
dece6358 32206+ if (shwh
5afbbe0d 32207+ || (bindex != bbot
dece6358 32208+ && au_br_whable(au_sbr_perm(sb, bindex))))
1facf9fc 32209+ au_fset_fillvdir(arg->flags, WHABLE);
32210+ do {
32211+ arg->err = 0;
32212+ au_fclr_fillvdir(arg->flags, CALLED);
32213+ /* smp_mb(); */
392086de 32214+ err = vfsub_iterate_dir(hf, &arg->ctx);
1facf9fc 32215+ if (err >= 0)
32216+ err = arg->err;
32217+ } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
392086de
AM
32218+
32219+ /*
32220+ * dir_relax() may be good for concurrency, but aufs should not
32221+ * use it since it will cause a lockdep problem.
32222+ */
1facf9fc 32223+ }
dece6358
AM
32224+
32225+ if (!err && shwh)
32226+ err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32227+
32228+ au_nhash_wh_free(&arg->whlist);
1facf9fc 32229+
4f0767ce 32230+out_delist:
dece6358 32231+ au_nhash_de_free(&arg->delist);
4f0767ce 32232+out:
1facf9fc 32233+ return err;
32234+}
32235+
32236+static int read_vdir(struct file *file, int may_read)
32237+{
32238+ int err;
32239+ unsigned long expire;
32240+ unsigned char do_read;
392086de
AM
32241+ struct fillvdir_arg arg = {
32242+ .ctx = {
2000de60 32243+ .actor = fillvdir
392086de
AM
32244+ }
32245+ };
1facf9fc 32246+ struct inode *inode;
32247+ struct au_vdir *vdir, *allocated;
32248+
32249+ err = 0;
c06a8ce3 32250+ inode = file_inode(file);
1facf9fc 32251+ IMustLock(inode);
5afbbe0d 32252+ IiMustWriteLock(inode);
dece6358
AM
32253+ SiMustAnyLock(inode->i_sb);
32254+
1facf9fc 32255+ allocated = NULL;
32256+ do_read = 0;
32257+ expire = au_sbi(inode->i_sb)->si_rdcache;
32258+ vdir = au_ivdir(inode);
32259+ if (!vdir) {
32260+ do_read = 1;
1308ab2a 32261+ vdir = alloc_vdir(file);
1facf9fc 32262+ err = PTR_ERR(vdir);
32263+ if (IS_ERR(vdir))
32264+ goto out;
32265+ err = 0;
32266+ allocated = vdir;
32267+ } else if (may_read
be118d29 32268+ && (!inode_eq_iversion(inode, vdir->vd_version)
1facf9fc 32269+ || time_after(jiffies, vdir->vd_jiffy + expire))) {
32270+ do_read = 1;
32271+ err = reinit_vdir(vdir);
32272+ if (unlikely(err))
32273+ goto out;
32274+ }
32275+
32276+ if (!do_read)
32277+ return 0; /* success */
32278+
32279+ arg.file = file;
32280+ arg.vdir = vdir;
32281+ err = au_do_read_vdir(&arg);
32282+ if (!err) {
392086de 32283+ /* file->f_pos = 0; */ /* todo: ctx->pos? */
be118d29 32284+ vdir->vd_version = inode_query_iversion(inode);
1facf9fc 32285+ vdir->vd_last.ul = 0;
32286+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32287+ if (allocated)
32288+ au_set_ivdir(inode, allocated);
32289+ } else if (allocated)
1c60b727 32290+ au_vdir_free(allocated);
1facf9fc 32291+
4f0767ce 32292+out:
1facf9fc 32293+ return err;
32294+}
32295+
32296+static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32297+{
32298+ int err, rerr;
32299+ unsigned long ul, n;
32300+ const unsigned int deblk_sz = src->vd_deblk_sz;
32301+
32302+ AuDebugOn(tgt->vd_nblk != 1);
32303+
32304+ err = -ENOMEM;
32305+ if (tgt->vd_nblk < src->vd_nblk) {
32306+ unsigned char **p;
32307+
e2f27e51
AM
32308+ p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32309+ GFP_NOFS, /*may_shrink*/0);
1facf9fc 32310+ if (unlikely(!p))
32311+ goto out;
32312+ tgt->vd_deblk = p;
32313+ }
32314+
1308ab2a 32315+ if (tgt->vd_deblk_sz != deblk_sz) {
32316+ unsigned char *p;
32317+
32318+ tgt->vd_deblk_sz = deblk_sz;
e2f27e51
AM
32319+ p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32320+ /*may_shrink*/1);
1308ab2a 32321+ if (unlikely(!p))
32322+ goto out;
32323+ tgt->vd_deblk[0] = p;
32324+ }
1facf9fc 32325+ memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
1facf9fc 32326+ tgt->vd_version = src->vd_version;
32327+ tgt->vd_jiffy = src->vd_jiffy;
32328+
32329+ n = src->vd_nblk;
32330+ for (ul = 1; ul < n; ul++) {
dece6358
AM
32331+ tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32332+ GFP_NOFS);
32333+ if (unlikely(!tgt->vd_deblk[ul]))
1facf9fc 32334+ goto out;
1308ab2a 32335+ tgt->vd_nblk++;
1facf9fc 32336+ }
1308ab2a 32337+ tgt->vd_nblk = n;
32338+ tgt->vd_last.ul = tgt->vd_last.ul;
32339+ tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32340+ tgt->vd_last.p.deblk += src->vd_last.p.deblk
32341+ - src->vd_deblk[src->vd_last.ul];
1facf9fc 32342+ /* smp_mb(); */
32343+ return 0; /* success */
32344+
4f0767ce 32345+out:
1facf9fc 32346+ rerr = reinit_vdir(tgt);
32347+ BUG_ON(rerr);
32348+ return err;
32349+}
32350+
32351+int au_vdir_init(struct file *file)
32352+{
32353+ int err;
32354+ struct inode *inode;
32355+ struct au_vdir *vdir_cache, *allocated;
32356+
392086de 32357+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 32358+ err = read_vdir(file, !file->f_pos);
32359+ if (unlikely(err))
32360+ goto out;
32361+
32362+ allocated = NULL;
32363+ vdir_cache = au_fvdir_cache(file);
32364+ if (!vdir_cache) {
1308ab2a 32365+ vdir_cache = alloc_vdir(file);
1facf9fc 32366+ err = PTR_ERR(vdir_cache);
32367+ if (IS_ERR(vdir_cache))
32368+ goto out;
32369+ allocated = vdir_cache;
32370+ } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
392086de 32371+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 32372+ err = reinit_vdir(vdir_cache);
32373+ if (unlikely(err))
32374+ goto out;
32375+ } else
32376+ return 0; /* success */
32377+
c06a8ce3 32378+ inode = file_inode(file);
1facf9fc 32379+ err = copy_vdir(vdir_cache, au_ivdir(inode));
32380+ if (!err) {
be118d29 32381+ file->f_version = inode_query_iversion(inode);
1facf9fc 32382+ if (allocated)
32383+ au_set_fvdir_cache(file, allocated);
32384+ } else if (allocated)
1c60b727 32385+ au_vdir_free(allocated);
1facf9fc 32386+
4f0767ce 32387+out:
1facf9fc 32388+ return err;
32389+}
32390+
32391+static loff_t calc_offset(struct au_vdir *vdir)
32392+{
32393+ loff_t offset;
32394+ union au_vdir_deblk_p p;
32395+
32396+ p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32397+ offset = vdir->vd_last.p.deblk - p.deblk;
32398+ offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32399+ return offset;
32400+}
32401+
32402+/* returns true or false */
392086de 32403+static int seek_vdir(struct file *file, struct dir_context *ctx)
1facf9fc 32404+{
32405+ int valid;
32406+ unsigned int deblk_sz;
32407+ unsigned long ul, n;
32408+ loff_t offset;
32409+ union au_vdir_deblk_p p, deblk_end;
32410+ struct au_vdir *vdir_cache;
32411+
32412+ valid = 1;
32413+ vdir_cache = au_fvdir_cache(file);
32414+ offset = calc_offset(vdir_cache);
32415+ AuDbg("offset %lld\n", offset);
392086de 32416+ if (ctx->pos == offset)
1facf9fc 32417+ goto out;
32418+
32419+ vdir_cache->vd_last.ul = 0;
32420+ vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
392086de 32421+ if (!ctx->pos)
1facf9fc 32422+ goto out;
32423+
32424+ valid = 0;
32425+ deblk_sz = vdir_cache->vd_deblk_sz;
392086de 32426+ ul = div64_u64(ctx->pos, deblk_sz);
1facf9fc 32427+ AuDbg("ul %lu\n", ul);
32428+ if (ul >= vdir_cache->vd_nblk)
32429+ goto out;
32430+
32431+ n = vdir_cache->vd_nblk;
32432+ for (; ul < n; ul++) {
32433+ p.deblk = vdir_cache->vd_deblk[ul];
32434+ deblk_end.deblk = p.deblk + deblk_sz;
32435+ offset = ul;
32436+ offset *= deblk_sz;
392086de 32437+ while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
1facf9fc 32438+ unsigned int l;
32439+
32440+ l = calc_size(p.de->de_str.len);
32441+ offset += l;
32442+ p.deblk += l;
32443+ }
32444+ if (!is_deblk_end(&p, &deblk_end)) {
32445+ valid = 1;
32446+ vdir_cache->vd_last.ul = ul;
32447+ vdir_cache->vd_last.p = p;
32448+ break;
32449+ }
32450+ }
32451+
4f0767ce 32452+out:
1facf9fc 32453+ /* smp_mb(); */
b00004a5
AM
32454+ if (!valid)
32455+ AuDbg("valid %d\n", !valid);
1facf9fc 32456+ return valid;
32457+}
32458+
392086de 32459+int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
1facf9fc 32460+{
1facf9fc 32461+ unsigned int l, deblk_sz;
32462+ union au_vdir_deblk_p deblk_end;
32463+ struct au_vdir *vdir_cache;
32464+ struct au_vdir_de *de;
32465+
32466+ vdir_cache = au_fvdir_cache(file);
392086de 32467+ if (!seek_vdir(file, ctx))
1facf9fc 32468+ return 0;
32469+
32470+ deblk_sz = vdir_cache->vd_deblk_sz;
32471+ while (1) {
32472+ deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32473+ deblk_end.deblk += deblk_sz;
32474+ while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32475+ de = vdir_cache->vd_last.p.de;
32476+ AuDbg("%.*s, off%lld, i%lu, dt%d\n",
392086de 32477+ de->de_str.len, de->de_str.name, ctx->pos,
1facf9fc 32478+ (unsigned long)de->de_ino, de->de_type);
392086de
AM
32479+ if (unlikely(!dir_emit(ctx, de->de_str.name,
32480+ de->de_str.len, de->de_ino,
32481+ de->de_type))) {
1facf9fc 32482+ /* todo: ignore the error caused by udba? */
32483+ /* return err; */
32484+ return 0;
32485+ }
32486+
32487+ l = calc_size(de->de_str.len);
32488+ vdir_cache->vd_last.p.deblk += l;
392086de 32489+ ctx->pos += l;
1facf9fc 32490+ }
32491+ if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32492+ vdir_cache->vd_last.ul++;
32493+ vdir_cache->vd_last.p.deblk
32494+ = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
392086de 32495+ ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
1facf9fc 32496+ continue;
32497+ }
32498+ break;
32499+ }
32500+
32501+ /* smp_mb(); */
32502+ return 0;
32503+}
7f207e10
AM
32504diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32505--- /usr/share/empty/fs/aufs/vfsub.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 32506+++ linux/fs/aufs/vfsub.c 2018-08-12 23:43:05.463458173 +0200
062440b3 32507@@ -0,0 +1,895 @@
cd7a4cd9 32508+// SPDX-License-Identifier: GPL-2.0
1facf9fc 32509+/*
b00004a5 32510+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 32511+ *
32512+ * This program, aufs is free software; you can redistribute it and/or modify
32513+ * it under the terms of the GNU General Public License as published by
32514+ * the Free Software Foundation; either version 2 of the License, or
32515+ * (at your option) any later version.
dece6358
AM
32516+ *
32517+ * This program is distributed in the hope that it will be useful,
32518+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
32519+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32520+ * GNU General Public License for more details.
32521+ *
32522+ * You should have received a copy of the GNU General Public License
523b37e3 32523+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 32524+ */
32525+
32526+/*
32527+ * sub-routines for VFS
32528+ */
32529+
8b6a4947 32530+#include <linux/mnt_namespace.h>
dece6358 32531+#include <linux/namei.h>
8cdd5066 32532+#include <linux/nsproxy.h>
dece6358
AM
32533+#include <linux/security.h>
32534+#include <linux/splice.h>
1facf9fc 32535+#include "aufs.h"
32536+
8cdd5066
JR
32537+#ifdef CONFIG_AUFS_BR_FUSE
32538+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32539+{
8cdd5066
JR
32540+ if (!au_test_fuse(h_sb) || !au_userns)
32541+ return 0;
32542+
8b6a4947 32543+ return is_current_mnt_ns(mnt) ? 0 : -EACCES;
8cdd5066
JR
32544+}
32545+#endif
32546+
a2654f78
AM
32547+int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
32548+{
32549+ int err;
32550+
32551+ lockdep_off();
32552+ down_read(&h_sb->s_umount);
32553+ err = __sync_filesystem(h_sb, wait);
32554+ up_read(&h_sb->s_umount);
32555+ lockdep_on();
32556+
32557+ return err;
32558+}
32559+
8cdd5066
JR
32560+/* ---------------------------------------------------------------------- */
32561+
1facf9fc 32562+int vfsub_update_h_iattr(struct path *h_path, int *did)
32563+{
32564+ int err;
32565+ struct kstat st;
32566+ struct super_block *h_sb;
32567+
32568+ /* for remote fs, leave work for its getattr or d_revalidate */
32569+ /* for bad i_attr fs, handle them in aufs_getattr() */
32570+ /* still some fs may acquire i_mutex. we need to skip them */
32571+ err = 0;
32572+ if (!did)
32573+ did = &err;
32574+ h_sb = h_path->dentry->d_sb;
32575+ *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32576+ if (*did)
521ced18 32577+ err = vfsub_getattr(h_path, &st);
1facf9fc 32578+
32579+ return err;
32580+}
32581+
32582+/* ---------------------------------------------------------------------- */
32583+
4a4d8108 32584+struct file *vfsub_dentry_open(struct path *path, int flags)
1308ab2a 32585+{
32586+ struct file *file;
32587+
b4510431 32588+ file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
7f207e10 32589+ current_cred());
2cbb1c4b
JR
32590+ if (!IS_ERR_OR_NULL(file)
32591+ && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
5527c038 32592+ i_readcount_inc(d_inode(path->dentry));
4a4d8108 32593+
1308ab2a 32594+ return file;
32595+}
32596+
1facf9fc 32597+struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32598+{
32599+ struct file *file;
32600+
2cbb1c4b 32601+ lockdep_off();
7f207e10 32602+ file = filp_open(path,
2cbb1c4b 32603+ oflags /* | __FMODE_NONOTIFY */,
7f207e10 32604+ mode);
2cbb1c4b 32605+ lockdep_on();
1facf9fc 32606+ if (IS_ERR(file))
32607+ goto out;
32608+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32609+
4f0767ce 32610+out:
1facf9fc 32611+ return file;
32612+}
32613+
b912730e
AM
32614+/*
32615+ * Ideally this function should call VFS:do_last() in order to keep all its
32616+ * checkings. But it is very hard for aufs to regenerate several VFS internal
32617+ * structure such as nameidata. This is a second (or third) best approach.
32618+ * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32619+ */
32620+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
32621+ struct vfsub_aopen_args *args, struct au_branch *br)
32622+{
32623+ int err;
32624+ struct file *file = args->file;
32625+ /* copied from linux/fs/namei.c:atomic_open() */
32626+ struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
32627+
32628+ IMustLock(dir);
32629+ AuDebugOn(!dir->i_op->atomic_open);
32630+
32631+ err = au_br_test_oflag(args->open_flag, br);
32632+ if (unlikely(err))
32633+ goto out;
32634+
32635+ args->file->f_path.dentry = DENTRY_NOT_SET;
32636+ args->file->f_path.mnt = au_br_mnt(br);
32637+ err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
32638+ args->create_mode, args->opened);
32639+ if (err >= 0) {
32640+ /* some filesystems don't set FILE_CREATED while succeeded? */
32641+ if (*args->opened & FILE_CREATED)
32642+ fsnotify_create(dir, dentry);
32643+ } else
32644+ goto out;
32645+
32646+
32647+ if (!err) {
32648+ /* todo: call VFS:may_open() here */
c4adf169 32649+ err = open_check_o_direct(file);
b912730e
AM
32650+ /* todo: ima_file_check() too? */
32651+ if (!err && (args->open_flag & __FMODE_EXEC))
32652+ err = deny_write_access(file);
32653+ if (unlikely(err))
32654+ /* note that the file is created and still opened */
32655+ goto out;
32656+ }
32657+
5afbbe0d 32658+ au_br_get(br);
b912730e
AM
32659+ fsnotify_open(file);
32660+
32661+out:
32662+ return err;
32663+}
32664+
1facf9fc 32665+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
32666+{
32667+ int err;
32668+
1facf9fc 32669+ err = kern_path(name, flags, path);
5527c038 32670+ if (!err && d_is_positive(path->dentry))
1facf9fc 32671+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
32672+ return err;
32673+}
32674+
febd17d6
JR
32675+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
32676+ struct dentry *parent, int len)
32677+{
32678+ struct path path = {
32679+ .mnt = NULL
32680+ };
32681+
32682+ path.dentry = lookup_one_len_unlocked(name, parent, len);
32683+ if (IS_ERR(path.dentry))
32684+ goto out;
32685+ if (d_is_positive(path.dentry))
32686+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32687+
32688+out:
32689+ AuTraceErrPtr(path.dentry);
32690+ return path.dentry;
32691+}
32692+
1facf9fc 32693+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
32694+ int len)
32695+{
32696+ struct path path = {
32697+ .mnt = NULL
32698+ };
32699+
1308ab2a 32700+ /* VFS checks it too, but by WARN_ON_ONCE() */
5527c038 32701+ IMustLock(d_inode(parent));
1facf9fc 32702+
32703+ path.dentry = lookup_one_len(name, parent, len);
32704+ if (IS_ERR(path.dentry))
32705+ goto out;
5527c038 32706+ if (d_is_positive(path.dentry))
1facf9fc 32707+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32708+
4f0767ce 32709+out:
4a4d8108 32710+ AuTraceErrPtr(path.dentry);
1facf9fc 32711+ return path.dentry;
32712+}
32713+
b4510431 32714+void vfsub_call_lkup_one(void *args)
2cbb1c4b 32715+{
b4510431
AM
32716+ struct vfsub_lkup_one_args *a = args;
32717+ *a->errp = vfsub_lkup_one(a->name, a->parent);
2cbb1c4b
JR
32718+}
32719+
1facf9fc 32720+/* ---------------------------------------------------------------------- */
32721+
32722+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
32723+ struct dentry *d2, struct au_hinode *hdir2)
32724+{
32725+ struct dentry *d;
32726+
2cbb1c4b 32727+ lockdep_off();
1facf9fc 32728+ d = lock_rename(d1, d2);
2cbb1c4b 32729+ lockdep_on();
4a4d8108 32730+ au_hn_suspend(hdir1);
1facf9fc 32731+ if (hdir1 != hdir2)
4a4d8108 32732+ au_hn_suspend(hdir2);
1facf9fc 32733+
32734+ return d;
32735+}
32736+
32737+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
32738+ struct dentry *d2, struct au_hinode *hdir2)
32739+{
4a4d8108 32740+ au_hn_resume(hdir1);
1facf9fc 32741+ if (hdir1 != hdir2)
4a4d8108 32742+ au_hn_resume(hdir2);
2cbb1c4b 32743+ lockdep_off();
1facf9fc 32744+ unlock_rename(d1, d2);
2cbb1c4b 32745+ lockdep_on();
1facf9fc 32746+}
32747+
32748+/* ---------------------------------------------------------------------- */
32749+
b4510431 32750+int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
1facf9fc 32751+{
32752+ int err;
32753+ struct dentry *d;
32754+
32755+ IMustLock(dir);
32756+
32757+ d = path->dentry;
32758+ path->dentry = d->d_parent;
b752ccd1 32759+ err = security_path_mknod(path, d, mode, 0);
1facf9fc 32760+ path->dentry = d;
32761+ if (unlikely(err))
32762+ goto out;
32763+
c1595e42 32764+ lockdep_off();
b4510431 32765+ err = vfs_create(dir, path->dentry, mode, want_excl);
c1595e42 32766+ lockdep_on();
1facf9fc 32767+ if (!err) {
32768+ struct path tmp = *path;
32769+ int did;
32770+
32771+ vfsub_update_h_iattr(&tmp, &did);
32772+ if (did) {
32773+ tmp.dentry = path->dentry->d_parent;
32774+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32775+ }
32776+ /*ignore*/
32777+ }
32778+
4f0767ce 32779+out:
1facf9fc 32780+ return err;
32781+}
32782+
32783+int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
32784+{
32785+ int err;
32786+ struct dentry *d;
32787+
32788+ IMustLock(dir);
32789+
32790+ d = path->dentry;
32791+ path->dentry = d->d_parent;
b752ccd1 32792+ err = security_path_symlink(path, d, symname);
1facf9fc 32793+ path->dentry = d;
32794+ if (unlikely(err))
32795+ goto out;
32796+
c1595e42 32797+ lockdep_off();
1facf9fc 32798+ err = vfs_symlink(dir, path->dentry, symname);
c1595e42 32799+ lockdep_on();
1facf9fc 32800+ if (!err) {
32801+ struct path tmp = *path;
32802+ int did;
32803+
32804+ vfsub_update_h_iattr(&tmp, &did);
32805+ if (did) {
32806+ tmp.dentry = path->dentry->d_parent;
32807+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32808+ }
32809+ /*ignore*/
32810+ }
32811+
4f0767ce 32812+out:
1facf9fc 32813+ return err;
32814+}
32815+
32816+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
32817+{
32818+ int err;
32819+ struct dentry *d;
32820+
32821+ IMustLock(dir);
32822+
32823+ d = path->dentry;
32824+ path->dentry = d->d_parent;
027c5e7a 32825+ err = security_path_mknod(path, d, mode, new_encode_dev(dev));
1facf9fc 32826+ path->dentry = d;
32827+ if (unlikely(err))
32828+ goto out;
32829+
c1595e42 32830+ lockdep_off();
1facf9fc 32831+ err = vfs_mknod(dir, path->dentry, mode, dev);
c1595e42 32832+ lockdep_on();
1facf9fc 32833+ if (!err) {
32834+ struct path tmp = *path;
32835+ int did;
32836+
32837+ vfsub_update_h_iattr(&tmp, &did);
32838+ if (did) {
32839+ tmp.dentry = path->dentry->d_parent;
32840+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32841+ }
32842+ /*ignore*/
32843+ }
32844+
4f0767ce 32845+out:
1facf9fc 32846+ return err;
32847+}
32848+
32849+static int au_test_nlink(struct inode *inode)
32850+{
32851+ const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
32852+
32853+ if (!au_test_fs_no_limit_nlink(inode->i_sb)
32854+ || inode->i_nlink < link_max)
32855+ return 0;
32856+ return -EMLINK;
32857+}
32858+
523b37e3
AM
32859+int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
32860+ struct inode **delegated_inode)
1facf9fc 32861+{
32862+ int err;
32863+ struct dentry *d;
32864+
32865+ IMustLock(dir);
32866+
5527c038 32867+ err = au_test_nlink(d_inode(src_dentry));
1facf9fc 32868+ if (unlikely(err))
32869+ return err;
32870+
b4510431 32871+ /* we don't call may_linkat() */
1facf9fc 32872+ d = path->dentry;
32873+ path->dentry = d->d_parent;
b752ccd1 32874+ err = security_path_link(src_dentry, path, d);
1facf9fc 32875+ path->dentry = d;
32876+ if (unlikely(err))
32877+ goto out;
32878+
2cbb1c4b 32879+ lockdep_off();
523b37e3 32880+ err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
2cbb1c4b 32881+ lockdep_on();
1facf9fc 32882+ if (!err) {
32883+ struct path tmp = *path;
32884+ int did;
32885+
32886+ /* fuse has different memory inode for the same inumber */
32887+ vfsub_update_h_iattr(&tmp, &did);
32888+ if (did) {
32889+ tmp.dentry = path->dentry->d_parent;
32890+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32891+ tmp.dentry = src_dentry;
32892+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32893+ }
32894+ /*ignore*/
32895+ }
32896+
4f0767ce 32897+out:
1facf9fc 32898+ return err;
32899+}
32900+
32901+int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
523b37e3 32902+ struct inode *dir, struct path *path,
f2c43d5f 32903+ struct inode **delegated_inode, unsigned int flags)
1facf9fc 32904+{
32905+ int err;
32906+ struct path tmp = {
32907+ .mnt = path->mnt
32908+ };
32909+ struct dentry *d;
32910+
32911+ IMustLock(dir);
32912+ IMustLock(src_dir);
32913+
32914+ d = path->dentry;
32915+ path->dentry = d->d_parent;
32916+ tmp.dentry = src_dentry->d_parent;
38d290e6 32917+ err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
1facf9fc 32918+ path->dentry = d;
32919+ if (unlikely(err))
32920+ goto out;
32921+
2cbb1c4b 32922+ lockdep_off();
523b37e3 32923+ err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
f2c43d5f 32924+ delegated_inode, flags);
2cbb1c4b 32925+ lockdep_on();
1facf9fc 32926+ if (!err) {
32927+ int did;
32928+
32929+ tmp.dentry = d->d_parent;
32930+ vfsub_update_h_iattr(&tmp, &did);
32931+ if (did) {
32932+ tmp.dentry = src_dentry;
32933+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32934+ tmp.dentry = src_dentry->d_parent;
32935+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32936+ }
32937+ /*ignore*/
32938+ }
32939+
4f0767ce 32940+out:
1facf9fc 32941+ return err;
32942+}
32943+
32944+int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
32945+{
32946+ int err;
32947+ struct dentry *d;
32948+
32949+ IMustLock(dir);
32950+
32951+ d = path->dentry;
32952+ path->dentry = d->d_parent;
b752ccd1 32953+ err = security_path_mkdir(path, d, mode);
1facf9fc 32954+ path->dentry = d;
32955+ if (unlikely(err))
32956+ goto out;
32957+
c1595e42 32958+ lockdep_off();
1facf9fc 32959+ err = vfs_mkdir(dir, path->dentry, mode);
c1595e42 32960+ lockdep_on();
1facf9fc 32961+ if (!err) {
32962+ struct path tmp = *path;
32963+ int did;
32964+
32965+ vfsub_update_h_iattr(&tmp, &did);
32966+ if (did) {
32967+ tmp.dentry = path->dentry->d_parent;
32968+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32969+ }
32970+ /*ignore*/
32971+ }
32972+
4f0767ce 32973+out:
1facf9fc 32974+ return err;
32975+}
32976+
32977+int vfsub_rmdir(struct inode *dir, struct path *path)
32978+{
32979+ int err;
32980+ struct dentry *d;
32981+
32982+ IMustLock(dir);
32983+
32984+ d = path->dentry;
32985+ path->dentry = d->d_parent;
b752ccd1 32986+ err = security_path_rmdir(path, d);
1facf9fc 32987+ path->dentry = d;
32988+ if (unlikely(err))
32989+ goto out;
32990+
2cbb1c4b 32991+ lockdep_off();
1facf9fc 32992+ err = vfs_rmdir(dir, path->dentry);
2cbb1c4b 32993+ lockdep_on();
1facf9fc 32994+ if (!err) {
32995+ struct path tmp = {
32996+ .dentry = path->dentry->d_parent,
32997+ .mnt = path->mnt
32998+ };
32999+
33000+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33001+ }
33002+
4f0767ce 33003+out:
1facf9fc 33004+ return err;
33005+}
33006+
33007+/* ---------------------------------------------------------------------- */
33008+
9dbd164d 33009+/* todo: support mmap_sem? */
1facf9fc 33010+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33011+ loff_t *ppos)
33012+{
33013+ ssize_t err;
33014+
2cbb1c4b 33015+ lockdep_off();
1facf9fc 33016+ err = vfs_read(file, ubuf, count, ppos);
2cbb1c4b 33017+ lockdep_on();
1facf9fc 33018+ if (err >= 0)
33019+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33020+ return err;
33021+}
33022+
33023+/* todo: kernel_read()? */
33024+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33025+ loff_t *ppos)
33026+{
33027+ ssize_t err;
33028+ mm_segment_t oldfs;
b752ccd1
AM
33029+ union {
33030+ void *k;
33031+ char __user *u;
33032+ } buf;
1facf9fc 33033+
b752ccd1 33034+ buf.k = kbuf;
1facf9fc 33035+ oldfs = get_fs();
33036+ set_fs(KERNEL_DS);
b752ccd1 33037+ err = vfsub_read_u(file, buf.u, count, ppos);
1facf9fc 33038+ set_fs(oldfs);
33039+ return err;
33040+}
33041+
33042+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33043+ loff_t *ppos)
33044+{
33045+ ssize_t err;
33046+
2cbb1c4b 33047+ lockdep_off();
1facf9fc 33048+ err = vfs_write(file, ubuf, count, ppos);
2cbb1c4b 33049+ lockdep_on();
1facf9fc 33050+ if (err >= 0)
33051+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33052+ return err;
33053+}
33054+
33055+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33056+{
33057+ ssize_t err;
33058+ mm_segment_t oldfs;
b752ccd1
AM
33059+ union {
33060+ void *k;
33061+ const char __user *u;
33062+ } buf;
1facf9fc 33063+
b752ccd1 33064+ buf.k = kbuf;
1facf9fc 33065+ oldfs = get_fs();
33066+ set_fs(KERNEL_DS);
b752ccd1 33067+ err = vfsub_write_u(file, buf.u, count, ppos);
1facf9fc 33068+ set_fs(oldfs);
33069+ return err;
33070+}
33071+
4a4d8108
AM
33072+int vfsub_flush(struct file *file, fl_owner_t id)
33073+{
33074+ int err;
33075+
33076+ err = 0;
523b37e3 33077+ if (file->f_op->flush) {
2000de60 33078+ if (!au_test_nfs(file->f_path.dentry->d_sb))
2cbb1c4b
JR
33079+ err = file->f_op->flush(file, id);
33080+ else {
33081+ lockdep_off();
33082+ err = file->f_op->flush(file, id);
33083+ lockdep_on();
33084+ }
4a4d8108
AM
33085+ if (!err)
33086+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33087+ /*ignore*/
33088+ }
33089+ return err;
33090+}
33091+
392086de 33092+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
1facf9fc 33093+{
33094+ int err;
33095+
062440b3 33096+ AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
392086de 33097+
2cbb1c4b 33098+ lockdep_off();
392086de 33099+ err = iterate_dir(file, ctx);
2cbb1c4b 33100+ lockdep_on();
1facf9fc 33101+ if (err >= 0)
33102+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
1c60b727 33103+
1facf9fc 33104+ return err;
33105+}
33106+
33107+long vfsub_splice_to(struct file *in, loff_t *ppos,
33108+ struct pipe_inode_info *pipe, size_t len,
33109+ unsigned int flags)
33110+{
33111+ long err;
33112+
2cbb1c4b 33113+ lockdep_off();
0fc653ad 33114+ err = do_splice_to(in, ppos, pipe, len, flags);
2cbb1c4b 33115+ lockdep_on();
4a4d8108 33116+ file_accessed(in);
1facf9fc 33117+ if (err >= 0)
33118+ vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33119+ return err;
33120+}
33121+
33122+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33123+ loff_t *ppos, size_t len, unsigned int flags)
33124+{
33125+ long err;
33126+
2cbb1c4b 33127+ lockdep_off();
0fc653ad 33128+ err = do_splice_from(pipe, out, ppos, len, flags);
2cbb1c4b 33129+ lockdep_on();
1facf9fc 33130+ if (err >= 0)
33131+ vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33132+ return err;
33133+}
33134+
53392da6
AM
33135+int vfsub_fsync(struct file *file, struct path *path, int datasync)
33136+{
33137+ int err;
33138+
33139+ /* file can be NULL */
33140+ lockdep_off();
33141+ err = vfs_fsync(file, datasync);
33142+ lockdep_on();
33143+ if (!err) {
33144+ if (!path) {
33145+ AuDebugOn(!file);
33146+ path = &file->f_path;
33147+ }
33148+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33149+ }
33150+ return err;
33151+}
33152+
1facf9fc 33153+/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33154+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33155+ struct file *h_file)
33156+{
33157+ int err;
33158+ struct inode *h_inode;
c06a8ce3 33159+ struct super_block *h_sb;
1facf9fc 33160+
1facf9fc 33161+ if (!h_file) {
c06a8ce3
AM
33162+ err = vfsub_truncate(h_path, length);
33163+ goto out;
1facf9fc 33164+ }
33165+
5527c038 33166+ h_inode = d_inode(h_path->dentry);
c06a8ce3
AM
33167+ h_sb = h_inode->i_sb;
33168+ lockdep_off();
33169+ sb_start_write(h_sb);
33170+ lockdep_on();
1facf9fc 33171+ err = locks_verify_truncate(h_inode, h_file, length);
33172+ if (!err)
953406b4 33173+ err = security_path_truncate(h_path);
2cbb1c4b
JR
33174+ if (!err) {
33175+ lockdep_off();
1facf9fc 33176+ err = do_truncate(h_path->dentry, length, attr, h_file);
2cbb1c4b
JR
33177+ lockdep_on();
33178+ }
c06a8ce3
AM
33179+ lockdep_off();
33180+ sb_end_write(h_sb);
33181+ lockdep_on();
1facf9fc 33182+
4f0767ce 33183+out:
1facf9fc 33184+ return err;
33185+}
33186+
33187+/* ---------------------------------------------------------------------- */
33188+
33189+struct au_vfsub_mkdir_args {
33190+ int *errp;
33191+ struct inode *dir;
33192+ struct path *path;
33193+ int mode;
33194+};
33195+
33196+static void au_call_vfsub_mkdir(void *args)
33197+{
33198+ struct au_vfsub_mkdir_args *a = args;
33199+ *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33200+}
33201+
33202+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33203+{
33204+ int err, do_sio, wkq_err;
33205+
33206+ do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
33207+ if (!do_sio) {
33208+ lockdep_off();
1facf9fc 33209+ err = vfsub_mkdir(dir, path, mode);
c1595e42
JR
33210+ lockdep_on();
33211+ } else {
1facf9fc 33212+ struct au_vfsub_mkdir_args args = {
33213+ .errp = &err,
33214+ .dir = dir,
33215+ .path = path,
33216+ .mode = mode
33217+ };
33218+ wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33219+ if (unlikely(wkq_err))
33220+ err = wkq_err;
33221+ }
33222+
33223+ return err;
33224+}
33225+
33226+struct au_vfsub_rmdir_args {
33227+ int *errp;
33228+ struct inode *dir;
33229+ struct path *path;
33230+};
33231+
33232+static void au_call_vfsub_rmdir(void *args)
33233+{
33234+ struct au_vfsub_rmdir_args *a = args;
33235+ *a->errp = vfsub_rmdir(a->dir, a->path);
33236+}
33237+
33238+int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33239+{
33240+ int err, do_sio, wkq_err;
33241+
33242+ do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
33243+ if (!do_sio) {
33244+ lockdep_off();
1facf9fc 33245+ err = vfsub_rmdir(dir, path);
c1595e42
JR
33246+ lockdep_on();
33247+ } else {
1facf9fc 33248+ struct au_vfsub_rmdir_args args = {
33249+ .errp = &err,
33250+ .dir = dir,
33251+ .path = path
33252+ };
33253+ wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33254+ if (unlikely(wkq_err))
33255+ err = wkq_err;
33256+ }
33257+
33258+ return err;
33259+}
33260+
33261+/* ---------------------------------------------------------------------- */
33262+
33263+struct notify_change_args {
33264+ int *errp;
33265+ struct path *path;
33266+ struct iattr *ia;
523b37e3 33267+ struct inode **delegated_inode;
1facf9fc 33268+};
33269+
33270+static void call_notify_change(void *args)
33271+{
33272+ struct notify_change_args *a = args;
33273+ struct inode *h_inode;
33274+
5527c038 33275+ h_inode = d_inode(a->path->dentry);
1facf9fc 33276+ IMustLock(h_inode);
33277+
33278+ *a->errp = -EPERM;
33279+ if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
c1595e42 33280+ lockdep_off();
523b37e3
AM
33281+ *a->errp = notify_change(a->path->dentry, a->ia,
33282+ a->delegated_inode);
c1595e42 33283+ lockdep_on();
1facf9fc 33284+ if (!*a->errp)
33285+ vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33286+ }
33287+ AuTraceErr(*a->errp);
33288+}
33289+
523b37e3
AM
33290+int vfsub_notify_change(struct path *path, struct iattr *ia,
33291+ struct inode **delegated_inode)
1facf9fc 33292+{
33293+ int err;
33294+ struct notify_change_args args = {
523b37e3
AM
33295+ .errp = &err,
33296+ .path = path,
33297+ .ia = ia,
33298+ .delegated_inode = delegated_inode
1facf9fc 33299+ };
33300+
33301+ call_notify_change(&args);
33302+
33303+ return err;
33304+}
33305+
523b37e3
AM
33306+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33307+ struct inode **delegated_inode)
1facf9fc 33308+{
33309+ int err, wkq_err;
33310+ struct notify_change_args args = {
523b37e3
AM
33311+ .errp = &err,
33312+ .path = path,
33313+ .ia = ia,
33314+ .delegated_inode = delegated_inode
1facf9fc 33315+ };
33316+
33317+ wkq_err = au_wkq_wait(call_notify_change, &args);
33318+ if (unlikely(wkq_err))
33319+ err = wkq_err;
33320+
33321+ return err;
33322+}
33323+
33324+/* ---------------------------------------------------------------------- */
33325+
33326+struct unlink_args {
33327+ int *errp;
33328+ struct inode *dir;
33329+ struct path *path;
523b37e3 33330+ struct inode **delegated_inode;
1facf9fc 33331+};
33332+
33333+static void call_unlink(void *args)
33334+{
33335+ struct unlink_args *a = args;
33336+ struct dentry *d = a->path->dentry;
33337+ struct inode *h_inode;
33338+ const int stop_sillyrename = (au_test_nfs(d->d_sb)
c1595e42 33339+ && au_dcount(d) == 1);
1facf9fc 33340+
33341+ IMustLock(a->dir);
33342+
33343+ a->path->dentry = d->d_parent;
33344+ *a->errp = security_path_unlink(a->path, d);
33345+ a->path->dentry = d;
33346+ if (unlikely(*a->errp))
33347+ return;
33348+
33349+ if (!stop_sillyrename)
33350+ dget(d);
5527c038
JR
33351+ h_inode = NULL;
33352+ if (d_is_positive(d)) {
33353+ h_inode = d_inode(d);
027c5e7a 33354+ ihold(h_inode);
5527c038 33355+ }
1facf9fc 33356+
2cbb1c4b 33357+ lockdep_off();
523b37e3 33358+ *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
2cbb1c4b 33359+ lockdep_on();
1facf9fc 33360+ if (!*a->errp) {
33361+ struct path tmp = {
33362+ .dentry = d->d_parent,
33363+ .mnt = a->path->mnt
33364+ };
33365+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33366+ }
33367+
33368+ if (!stop_sillyrename)
33369+ dput(d);
33370+ if (h_inode)
33371+ iput(h_inode);
33372+
33373+ AuTraceErr(*a->errp);
33374+}
33375+
33376+/*
33377+ * @dir: must be locked.
33378+ * @dentry: target dentry.
33379+ */
523b37e3
AM
33380+int vfsub_unlink(struct inode *dir, struct path *path,
33381+ struct inode **delegated_inode, int force)
1facf9fc 33382+{
33383+ int err;
33384+ struct unlink_args args = {
523b37e3
AM
33385+ .errp = &err,
33386+ .dir = dir,
33387+ .path = path,
33388+ .delegated_inode = delegated_inode
1facf9fc 33389+ };
33390+
33391+ if (!force)
33392+ call_unlink(&args);
33393+ else {
33394+ int wkq_err;
33395+
33396+ wkq_err = au_wkq_wait(call_unlink, &args);
33397+ if (unlikely(wkq_err))
33398+ err = wkq_err;
33399+ }
33400+
33401+ return err;
33402+}
7f207e10
AM
33403diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33404--- /usr/share/empty/fs/aufs/vfsub.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 33405+++ linux/fs/aufs/vfsub.h 2018-08-12 23:43:05.463458173 +0200
062440b3
AM
33406@@ -0,0 +1,355 @@
33407+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 33408+/*
b00004a5 33409+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 33410+ *
33411+ * This program, aufs is free software; you can redistribute it and/or modify
33412+ * it under the terms of the GNU General Public License as published by
33413+ * the Free Software Foundation; either version 2 of the License, or
33414+ * (at your option) any later version.
dece6358
AM
33415+ *
33416+ * This program is distributed in the hope that it will be useful,
33417+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33418+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33419+ * GNU General Public License for more details.
33420+ *
33421+ * You should have received a copy of the GNU General Public License
523b37e3 33422+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 33423+ */
33424+
33425+/*
33426+ * sub-routines for VFS
33427+ */
33428+
33429+#ifndef __AUFS_VFSUB_H__
33430+#define __AUFS_VFSUB_H__
33431+
33432+#ifdef __KERNEL__
33433+
33434+#include <linux/fs.h>
be118d29 33435+#include <linux/iversion.h>
b4510431 33436+#include <linux/mount.h>
8cdd5066 33437+#include <linux/posix_acl.h>
c1595e42 33438+#include <linux/xattr.h>
7f207e10 33439+#include "debug.h"
1facf9fc 33440+
7f207e10 33441+/* copied from linux/fs/internal.h */
2cbb1c4b 33442+/* todo: BAD approach!! */
c06a8ce3 33443+extern void __mnt_drop_write(struct vfsmount *);
c4adf169 33444+extern int open_check_o_direct(struct file *f);
7f207e10
AM
33445+
33446+/* ---------------------------------------------------------------------- */
1facf9fc 33447+
33448+/* lock subclass for lower inode */
33449+/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33450+/* reduce? gave up. */
33451+enum {
c1595e42 33452+ AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
1facf9fc 33453+ AuLsc_I_PARENT, /* lower inode, parent first */
33454+ AuLsc_I_PARENT2, /* copyup dirs */
dece6358 33455+ AuLsc_I_PARENT3, /* copyup wh */
1facf9fc 33456+ AuLsc_I_CHILD,
33457+ AuLsc_I_CHILD2,
33458+ AuLsc_I_End
33459+};
33460+
33461+/* to debug easier, do not make them inlined functions */
33462+#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
febd17d6 33463+#define IMustLock(i) AuDebugOn(!inode_is_locked(i))
1facf9fc 33464+
33465+/* ---------------------------------------------------------------------- */
33466+
7f207e10
AM
33467+static inline void vfsub_drop_nlink(struct inode *inode)
33468+{
33469+ AuDebugOn(!inode->i_nlink);
33470+ drop_nlink(inode);
33471+}
33472+
027c5e7a
AM
33473+static inline void vfsub_dead_dir(struct inode *inode)
33474+{
33475+ AuDebugOn(!S_ISDIR(inode->i_mode));
33476+ inode->i_flags |= S_DEAD;
33477+ clear_nlink(inode);
33478+}
33479+
392086de
AM
33480+static inline int vfsub_native_ro(struct inode *inode)
33481+{
8b6a4947 33482+ return sb_rdonly(inode->i_sb)
392086de
AM
33483+ || IS_RDONLY(inode)
33484+ /* || IS_APPEND(inode) */
33485+ || IS_IMMUTABLE(inode);
33486+}
33487+
8cdd5066
JR
33488+#ifdef CONFIG_AUFS_BR_FUSE
33489+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33490+#else
33491+AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33492+#endif
33493+
a2654f78
AM
33494+int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
33495+
7f207e10
AM
33496+/* ---------------------------------------------------------------------- */
33497+
33498+int vfsub_update_h_iattr(struct path *h_path, int *did);
33499+struct file *vfsub_dentry_open(struct path *path, int flags);
33500+struct file *vfsub_filp_open(const char *path, int oflags, int mode);
b912730e
AM
33501+struct vfsub_aopen_args {
33502+ struct file *file;
33503+ unsigned int open_flag;
33504+ umode_t create_mode;
33505+ int *opened;
33506+};
33507+struct au_branch;
33508+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33509+ struct vfsub_aopen_args *args, struct au_branch *br);
1facf9fc 33510+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
b4510431 33511+
febd17d6
JR
33512+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33513+ struct dentry *parent, int len);
1facf9fc 33514+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
33515+ int len);
b4510431
AM
33516+
33517+struct vfsub_lkup_one_args {
33518+ struct dentry **errp;
33519+ struct qstr *name;
33520+ struct dentry *parent;
33521+};
33522+
33523+static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33524+ struct dentry *parent)
33525+{
33526+ return vfsub_lookup_one_len(name->name, parent, name->len);
33527+}
33528+
33529+void vfsub_call_lkup_one(void *args);
33530+
33531+/* ---------------------------------------------------------------------- */
33532+
33533+static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33534+{
33535+ int err;
076b876e 33536+
b4510431
AM
33537+ lockdep_off();
33538+ err = mnt_want_write(mnt);
33539+ lockdep_on();
33540+ return err;
33541+}
33542+
33543+static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33544+{
33545+ lockdep_off();
33546+ mnt_drop_write(mnt);
33547+ lockdep_on();
33548+}
1facf9fc 33549+
7e9cd9fe 33550+#if 0 /* reserved */
c06a8ce3
AM
33551+static inline void vfsub_mnt_drop_write_file(struct file *file)
33552+{
33553+ lockdep_off();
33554+ mnt_drop_write_file(file);
33555+ lockdep_on();
33556+}
7e9cd9fe 33557+#endif
c06a8ce3 33558+
1facf9fc 33559+/* ---------------------------------------------------------------------- */
33560+
33561+struct au_hinode;
33562+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33563+ struct dentry *d2, struct au_hinode *hdir2);
33564+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33565+ struct dentry *d2, struct au_hinode *hdir2);
33566+
537831f9
AM
33567+int vfsub_create(struct inode *dir, struct path *path, int mode,
33568+ bool want_excl);
1facf9fc 33569+int vfsub_symlink(struct inode *dir, struct path *path,
33570+ const char *symname);
33571+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33572+int vfsub_link(struct dentry *src_dentry, struct inode *dir,
523b37e3 33573+ struct path *path, struct inode **delegated_inode);
1facf9fc 33574+int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
523b37e3 33575+ struct inode *hdir, struct path *path,
f2c43d5f 33576+ struct inode **delegated_inode, unsigned int flags);
1facf9fc 33577+int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33578+int vfsub_rmdir(struct inode *dir, struct path *path);
33579+
33580+/* ---------------------------------------------------------------------- */
33581+
33582+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33583+ loff_t *ppos);
33584+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33585+ loff_t *ppos);
33586+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33587+ loff_t *ppos);
33588+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33589+ loff_t *ppos);
4a4d8108 33590+int vfsub_flush(struct file *file, fl_owner_t id);
392086de
AM
33591+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33592+
c06a8ce3
AM
33593+static inline loff_t vfsub_f_size_read(struct file *file)
33594+{
33595+ return i_size_read(file_inode(file));
33596+}
33597+
4a4d8108
AM
33598+static inline unsigned int vfsub_file_flags(struct file *file)
33599+{
33600+ unsigned int flags;
33601+
33602+ spin_lock(&file->f_lock);
33603+ flags = file->f_flags;
33604+ spin_unlock(&file->f_lock);
33605+
33606+ return flags;
33607+}
1308ab2a 33608+
f0c0a007
AM
33609+static inline int vfsub_file_execed(struct file *file)
33610+{
33611+ /* todo: direct access f_flags */
33612+ return !!(vfsub_file_flags(file) & __FMODE_EXEC);
33613+}
33614+
7e9cd9fe 33615+#if 0 /* reserved */
1facf9fc 33616+static inline void vfsub_file_accessed(struct file *h_file)
33617+{
33618+ file_accessed(h_file);
33619+ vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
33620+}
7e9cd9fe 33621+#endif
1facf9fc 33622+
79b8bda9 33623+#if 0 /* reserved */
1facf9fc 33624+static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
33625+ struct dentry *h_dentry)
33626+{
33627+ struct path h_path = {
33628+ .dentry = h_dentry,
33629+ .mnt = h_mnt
33630+ };
92d182d2 33631+ touch_atime(&h_path);
1facf9fc 33632+ vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
33633+}
79b8bda9 33634+#endif
1facf9fc 33635+
cd7a4cd9
AM
33636+static inline int vfsub_update_time(struct inode *h_inode,
33637+ struct timespec64 *ts, int flags)
0c3ec466 33638+{
5afbbe0d 33639+ return update_time(h_inode, ts, flags);
0c3ec466
AM
33640+ /* no vfsub_update_h_iattr() since we don't have struct path */
33641+}
33642+
8cdd5066
JR
33643+#ifdef CONFIG_FS_POSIX_ACL
33644+static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
33645+{
33646+ int err;
33647+
33648+ err = posix_acl_chmod(h_inode, h_mode);
33649+ if (err == -EOPNOTSUPP)
33650+ err = 0;
33651+ return err;
33652+}
33653+#else
33654+AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
33655+#endif
33656+
4a4d8108
AM
33657+long vfsub_splice_to(struct file *in, loff_t *ppos,
33658+ struct pipe_inode_info *pipe, size_t len,
33659+ unsigned int flags);
33660+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33661+ loff_t *ppos, size_t len, unsigned int flags);
c06a8ce3
AM
33662+
33663+static inline long vfsub_truncate(struct path *path, loff_t length)
33664+{
33665+ long err;
076b876e 33666+
c06a8ce3
AM
33667+ lockdep_off();
33668+ err = vfs_truncate(path, length);
33669+ lockdep_on();
33670+ return err;
33671+}
33672+
4a4d8108
AM
33673+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33674+ struct file *h_file);
53392da6 33675+int vfsub_fsync(struct file *file, struct path *path, int datasync);
4a4d8108 33676+
521ced18
JR
33677+/*
33678+ * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
33679+ * ioctl.
33680+ */
33681+static inline int vfsub_clone_file_range(struct file *src, struct file *dst,
33682+ u64 len)
33683+{
33684+ int err;
33685+
33686+ lockdep_off();
33687+ err = vfs_clone_file_range(src, 0, dst, 0, len);
33688+ lockdep_on();
33689+
33690+ return err;
33691+}
33692+
33693+/* copy_file_range(2) is a systemcall */
33694+static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
33695+ struct file *dst, loff_t dst_pos,
33696+ size_t len, unsigned int flags)
33697+{
33698+ ssize_t ssz;
33699+
33700+ lockdep_off();
33701+ ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
33702+ lockdep_on();
33703+
33704+ return ssz;
33705+}
33706+
1facf9fc 33707+/* ---------------------------------------------------------------------- */
33708+
33709+static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
33710+{
33711+ loff_t err;
33712+
2cbb1c4b 33713+ lockdep_off();
1facf9fc 33714+ err = vfs_llseek(file, offset, origin);
2cbb1c4b 33715+ lockdep_on();
1facf9fc 33716+ return err;
33717+}
33718+
33719+/* ---------------------------------------------------------------------- */
33720+
4a4d8108
AM
33721+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
33722+int vfsub_sio_rmdir(struct inode *dir, struct path *path);
523b37e3
AM
33723+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33724+ struct inode **delegated_inode);
33725+int vfsub_notify_change(struct path *path, struct iattr *ia,
33726+ struct inode **delegated_inode);
33727+int vfsub_unlink(struct inode *dir, struct path *path,
33728+ struct inode **delegated_inode, int force);
4a4d8108 33729+
521ced18
JR
33730+static inline int vfsub_getattr(const struct path *path, struct kstat *st)
33731+{
33732+ return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
33733+}
33734+
c1595e42
JR
33735+/* ---------------------------------------------------------------------- */
33736+
33737+static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
33738+ const void *value, size_t size, int flags)
33739+{
33740+ int err;
33741+
33742+ lockdep_off();
33743+ err = vfs_setxattr(dentry, name, value, size, flags);
33744+ lockdep_on();
33745+
33746+ return err;
33747+}
33748+
33749+static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
33750+{
33751+ int err;
33752+
33753+ lockdep_off();
33754+ err = vfs_removexattr(dentry, name);
33755+ lockdep_on();
33756+
33757+ return err;
33758+}
33759+
1facf9fc 33760+#endif /* __KERNEL__ */
33761+#endif /* __AUFS_VFSUB_H__ */
7f207e10
AM
33762diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
33763--- /usr/share/empty/fs/aufs/wbr_policy.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9
AM
33764+++ linux/fs/aufs/wbr_policy.c 2018-08-12 23:43:05.463458173 +0200
33765@@ -0,0 +1,830 @@
33766+// SPDX-License-Identifier: GPL-2.0
1facf9fc 33767+/*
b00004a5 33768+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 33769+ *
33770+ * This program, aufs is free software; you can redistribute it and/or modify
33771+ * it under the terms of the GNU General Public License as published by
33772+ * the Free Software Foundation; either version 2 of the License, or
33773+ * (at your option) any later version.
dece6358
AM
33774+ *
33775+ * This program is distributed in the hope that it will be useful,
33776+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33777+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33778+ * GNU General Public License for more details.
33779+ *
33780+ * You should have received a copy of the GNU General Public License
523b37e3 33781+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 33782+ */
33783+
33784+/*
33785+ * policies for selecting one among multiple writable branches
33786+ */
33787+
33788+#include <linux/statfs.h>
33789+#include "aufs.h"
33790+
33791+/* subset of cpup_attr() */
33792+static noinline_for_stack
33793+int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
33794+{
33795+ int err, sbits;
33796+ struct iattr ia;
33797+ struct inode *h_isrc;
33798+
5527c038 33799+ h_isrc = d_inode(h_src);
1facf9fc 33800+ ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
33801+ ia.ia_mode = h_isrc->i_mode;
33802+ ia.ia_uid = h_isrc->i_uid;
33803+ ia.ia_gid = h_isrc->i_gid;
33804+ sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
5527c038 33805+ au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
523b37e3
AM
33806+ /* no delegation since it is just created */
33807+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 33808+
33809+ /* is this nfs only? */
33810+ if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
33811+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
33812+ ia.ia_mode = h_isrc->i_mode;
523b37e3 33813+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 33814+ }
33815+
33816+ return err;
33817+}
33818+
33819+#define AuCpdown_PARENT_OPQ 1
33820+#define AuCpdown_WHED (1 << 1)
33821+#define AuCpdown_MADE_DIR (1 << 2)
33822+#define AuCpdown_DIROPQ (1 << 3)
33823+#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
7f207e10
AM
33824+#define au_fset_cpdown(flags, name) \
33825+ do { (flags) |= AuCpdown_##name; } while (0)
33826+#define au_fclr_cpdown(flags, name) \
33827+ do { (flags) &= ~AuCpdown_##name; } while (0)
1facf9fc 33828+
1facf9fc 33829+static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
c2b27bf2 33830+ unsigned int *flags)
1facf9fc 33831+{
33832+ int err;
33833+ struct dentry *opq_dentry;
33834+
33835+ opq_dentry = au_diropq_create(dentry, bdst);
33836+ err = PTR_ERR(opq_dentry);
33837+ if (IS_ERR(opq_dentry))
33838+ goto out;
33839+ dput(opq_dentry);
c2b27bf2 33840+ au_fset_cpdown(*flags, DIROPQ);
1facf9fc 33841+
4f0767ce 33842+out:
1facf9fc 33843+ return err;
33844+}
33845+
33846+static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
33847+ struct inode *dir, aufs_bindex_t bdst)
33848+{
33849+ int err;
33850+ struct path h_path;
33851+ struct au_branch *br;
33852+
33853+ br = au_sbr(dentry->d_sb, bdst);
33854+ h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
33855+ err = PTR_ERR(h_path.dentry);
33856+ if (IS_ERR(h_path.dentry))
33857+ goto out;
33858+
33859+ err = 0;
5527c038 33860+ if (d_is_positive(h_path.dentry)) {
86dc4139 33861+ h_path.mnt = au_br_mnt(br);
1facf9fc 33862+ err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
33863+ dentry);
33864+ }
33865+ dput(h_path.dentry);
33866+
4f0767ce 33867+out:
1facf9fc 33868+ return err;
33869+}
33870+
33871+static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 33872+ struct au_pin *pin,
1facf9fc 33873+ struct dentry *h_parent, void *arg)
33874+{
33875+ int err, rerr;
5afbbe0d 33876+ aufs_bindex_t bopq, btop;
1facf9fc 33877+ struct path h_path;
33878+ struct dentry *parent;
33879+ struct inode *h_dir, *h_inode, *inode, *dir;
c2b27bf2 33880+ unsigned int *flags = arg;
1facf9fc 33881+
5afbbe0d 33882+ btop = au_dbtop(dentry);
1facf9fc 33883+ /* dentry is di-locked */
33884+ parent = dget_parent(dentry);
5527c038
JR
33885+ dir = d_inode(parent);
33886+ h_dir = d_inode(h_parent);
1facf9fc 33887+ AuDebugOn(h_dir != au_h_iptr(dir, bdst));
33888+ IMustLock(h_dir);
33889+
86dc4139 33890+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
1facf9fc 33891+ if (unlikely(err < 0))
33892+ goto out;
33893+ h_path.dentry = au_h_dptr(dentry, bdst);
33894+ h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
cd7a4cd9 33895+ err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
1facf9fc 33896+ if (unlikely(err))
33897+ goto out_put;
c2b27bf2 33898+ au_fset_cpdown(*flags, MADE_DIR);
1facf9fc 33899+
1facf9fc 33900+ bopq = au_dbdiropq(dentry);
c2b27bf2
AM
33901+ au_fclr_cpdown(*flags, WHED);
33902+ au_fclr_cpdown(*flags, DIROPQ);
1facf9fc 33903+ if (au_dbwh(dentry) == bdst)
c2b27bf2
AM
33904+ au_fset_cpdown(*flags, WHED);
33905+ if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
33906+ au_fset_cpdown(*flags, PARENT_OPQ);
5527c038 33907+ h_inode = d_inode(h_path.dentry);
febd17d6 33908+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
c2b27bf2
AM
33909+ if (au_ftest_cpdown(*flags, WHED)) {
33910+ err = au_cpdown_dir_opq(dentry, bdst, flags);
1facf9fc 33911+ if (unlikely(err)) {
febd17d6 33912+ inode_unlock(h_inode);
1facf9fc 33913+ goto out_dir;
33914+ }
33915+ }
33916+
5afbbe0d 33917+ err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
febd17d6 33918+ inode_unlock(h_inode);
1facf9fc 33919+ if (unlikely(err))
33920+ goto out_opq;
33921+
c2b27bf2 33922+ if (au_ftest_cpdown(*flags, WHED)) {
1facf9fc 33923+ err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
33924+ if (unlikely(err))
33925+ goto out_opq;
33926+ }
33927+
5527c038 33928+ inode = d_inode(dentry);
5afbbe0d
AM
33929+ if (au_ibbot(inode) < bdst)
33930+ au_set_ibbot(inode, bdst);
1facf9fc 33931+ au_set_h_iptr(inode, bdst, au_igrab(h_inode),
33932+ au_hi_flags(inode, /*isdir*/1));
076b876e 33933+ au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
1facf9fc 33934+ goto out; /* success */
33935+
33936+ /* revert */
4f0767ce 33937+out_opq:
c2b27bf2 33938+ if (au_ftest_cpdown(*flags, DIROPQ)) {
febd17d6 33939+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 33940+ rerr = au_diropq_remove(dentry, bdst);
febd17d6 33941+ inode_unlock(h_inode);
1facf9fc 33942+ if (unlikely(rerr)) {
523b37e3
AM
33943+ AuIOErr("failed removing diropq for %pd b%d (%d)\n",
33944+ dentry, bdst, rerr);
1facf9fc 33945+ err = -EIO;
33946+ goto out;
33947+ }
33948+ }
4f0767ce 33949+out_dir:
c2b27bf2 33950+ if (au_ftest_cpdown(*flags, MADE_DIR)) {
1facf9fc 33951+ rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
33952+ if (unlikely(rerr)) {
523b37e3
AM
33953+ AuIOErr("failed removing %pd b%d (%d)\n",
33954+ dentry, bdst, rerr);
1facf9fc 33955+ err = -EIO;
33956+ }
33957+ }
4f0767ce 33958+out_put:
1facf9fc 33959+ au_set_h_dptr(dentry, bdst, NULL);
5afbbe0d
AM
33960+ if (au_dbbot(dentry) == bdst)
33961+ au_update_dbbot(dentry);
4f0767ce 33962+out:
1facf9fc 33963+ dput(parent);
33964+ return err;
33965+}
33966+
33967+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
33968+{
33969+ int err;
c2b27bf2 33970+ unsigned int flags;
1facf9fc 33971+
c2b27bf2
AM
33972+ flags = 0;
33973+ err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
1facf9fc 33974+
33975+ return err;
33976+}
33977+
33978+/* ---------------------------------------------------------------------- */
33979+
33980+/* policies for create */
33981+
c2b27bf2 33982+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
4a4d8108
AM
33983+{
33984+ int err, i, j, ndentry;
33985+ aufs_bindex_t bopq;
33986+ struct au_dcsub_pages dpages;
33987+ struct au_dpage *dpage;
33988+ struct dentry **dentries, *parent, *d;
33989+
33990+ err = au_dpages_init(&dpages, GFP_NOFS);
33991+ if (unlikely(err))
33992+ goto out;
33993+ parent = dget_parent(dentry);
027c5e7a 33994+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
4a4d8108
AM
33995+ if (unlikely(err))
33996+ goto out_free;
33997+
33998+ err = bindex;
33999+ for (i = 0; i < dpages.ndpage; i++) {
34000+ dpage = dpages.dpages + i;
34001+ dentries = dpage->dentries;
34002+ ndentry = dpage->ndentry;
34003+ for (j = 0; j < ndentry; j++) {
34004+ d = dentries[j];
34005+ di_read_lock_parent2(d, !AuLock_IR);
34006+ bopq = au_dbdiropq(d);
34007+ di_read_unlock(d, !AuLock_IR);
34008+ if (bopq >= 0 && bopq < err)
34009+ err = bopq;
34010+ }
34011+ }
34012+
34013+out_free:
34014+ dput(parent);
34015+ au_dpages_free(&dpages);
34016+out:
34017+ return err;
34018+}
34019+
1facf9fc 34020+static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34021+{
34022+ for (; bindex >= 0; bindex--)
34023+ if (!au_br_rdonly(au_sbr(sb, bindex)))
34024+ return bindex;
34025+ return -EROFS;
34026+}
34027+
34028+/* top down parent */
392086de
AM
34029+static int au_wbr_create_tdp(struct dentry *dentry,
34030+ unsigned int flags __maybe_unused)
1facf9fc 34031+{
34032+ int err;
5afbbe0d 34033+ aufs_bindex_t btop, bindex;
1facf9fc 34034+ struct super_block *sb;
34035+ struct dentry *parent, *h_parent;
34036+
34037+ sb = dentry->d_sb;
5afbbe0d
AM
34038+ btop = au_dbtop(dentry);
34039+ err = btop;
34040+ if (!au_br_rdonly(au_sbr(sb, btop)))
1facf9fc 34041+ goto out;
34042+
34043+ err = -EROFS;
34044+ parent = dget_parent(dentry);
5afbbe0d 34045+ for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
1facf9fc 34046+ h_parent = au_h_dptr(parent, bindex);
5527c038 34047+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34048+ continue;
34049+
34050+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34051+ err = bindex;
34052+ break;
34053+ }
34054+ }
34055+ dput(parent);
34056+
34057+ /* bottom up here */
4a4d8108 34058+ if (unlikely(err < 0)) {
5afbbe0d 34059+ err = au_wbr_bu(sb, btop - 1);
4a4d8108
AM
34060+ if (err >= 0)
34061+ err = au_wbr_nonopq(dentry, err);
34062+ }
1facf9fc 34063+
4f0767ce 34064+out:
1facf9fc 34065+ AuDbg("b%d\n", err);
34066+ return err;
34067+}
34068+
34069+/* ---------------------------------------------------------------------- */
34070+
34071+/* an exception for the policy other than tdp */
34072+static int au_wbr_create_exp(struct dentry *dentry)
34073+{
34074+ int err;
34075+ aufs_bindex_t bwh, bdiropq;
34076+ struct dentry *parent;
34077+
34078+ err = -1;
34079+ bwh = au_dbwh(dentry);
34080+ parent = dget_parent(dentry);
34081+ bdiropq = au_dbdiropq(parent);
34082+ if (bwh >= 0) {
34083+ if (bdiropq >= 0)
34084+ err = min(bdiropq, bwh);
34085+ else
34086+ err = bwh;
34087+ AuDbg("%d\n", err);
34088+ } else if (bdiropq >= 0) {
34089+ err = bdiropq;
34090+ AuDbg("%d\n", err);
34091+ }
34092+ dput(parent);
34093+
4a4d8108
AM
34094+ if (err >= 0)
34095+ err = au_wbr_nonopq(dentry, err);
34096+
1facf9fc 34097+ if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34098+ err = -1;
34099+
34100+ AuDbg("%d\n", err);
34101+ return err;
34102+}
34103+
34104+/* ---------------------------------------------------------------------- */
34105+
34106+/* round robin */
34107+static int au_wbr_create_init_rr(struct super_block *sb)
34108+{
34109+ int err;
34110+
5afbbe0d 34111+ err = au_wbr_bu(sb, au_sbbot(sb));
1facf9fc 34112+ atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
dece6358 34113+ /* smp_mb(); */
1facf9fc 34114+
34115+ AuDbg("b%d\n", err);
34116+ return err;
34117+}
34118+
392086de 34119+static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
1facf9fc 34120+{
34121+ int err, nbr;
34122+ unsigned int u;
5afbbe0d 34123+ aufs_bindex_t bindex, bbot;
1facf9fc 34124+ struct super_block *sb;
34125+ atomic_t *next;
34126+
34127+ err = au_wbr_create_exp(dentry);
34128+ if (err >= 0)
34129+ goto out;
34130+
34131+ sb = dentry->d_sb;
34132+ next = &au_sbi(sb)->si_wbr_rr_next;
5afbbe0d
AM
34133+ bbot = au_sbbot(sb);
34134+ nbr = bbot + 1;
34135+ for (bindex = 0; bindex <= bbot; bindex++) {
392086de 34136+ if (!au_ftest_wbr(flags, DIR)) {
1facf9fc 34137+ err = atomic_dec_return(next) + 1;
34138+ /* modulo for 0 is meaningless */
34139+ if (unlikely(!err))
34140+ err = atomic_dec_return(next) + 1;
34141+ } else
34142+ err = atomic_read(next);
34143+ AuDbg("%d\n", err);
34144+ u = err;
34145+ err = u % nbr;
34146+ AuDbg("%d\n", err);
34147+ if (!au_br_rdonly(au_sbr(sb, err)))
34148+ break;
34149+ err = -EROFS;
34150+ }
34151+
4a4d8108
AM
34152+ if (err >= 0)
34153+ err = au_wbr_nonopq(dentry, err);
34154+
4f0767ce 34155+out:
1facf9fc 34156+ AuDbg("%d\n", err);
34157+ return err;
34158+}
34159+
34160+/* ---------------------------------------------------------------------- */
34161+
34162+/* most free space */
392086de 34163+static void au_mfs(struct dentry *dentry, struct dentry *parent)
1facf9fc 34164+{
34165+ struct super_block *sb;
34166+ struct au_branch *br;
34167+ struct au_wbr_mfs *mfs;
392086de 34168+ struct dentry *h_parent;
5afbbe0d 34169+ aufs_bindex_t bindex, bbot;
1facf9fc 34170+ int err;
34171+ unsigned long long b, bavail;
7f207e10 34172+ struct path h_path;
1facf9fc 34173+ /* reduce the stack usage */
34174+ struct kstatfs *st;
34175+
34176+ st = kmalloc(sizeof(*st), GFP_NOFS);
34177+ if (unlikely(!st)) {
34178+ AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34179+ return;
34180+ }
34181+
34182+ bavail = 0;
34183+ sb = dentry->d_sb;
34184+ mfs = &au_sbi(sb)->si_wbr_mfs;
dece6358 34185+ MtxMustLock(&mfs->mfs_lock);
1facf9fc 34186+ mfs->mfs_bindex = -EROFS;
34187+ mfs->mfsrr_bytes = 0;
392086de
AM
34188+ if (!parent) {
34189+ bindex = 0;
5afbbe0d 34190+ bbot = au_sbbot(sb);
392086de 34191+ } else {
5afbbe0d
AM
34192+ bindex = au_dbtop(parent);
34193+ bbot = au_dbtaildir(parent);
392086de
AM
34194+ }
34195+
5afbbe0d 34196+ for (; bindex <= bbot; bindex++) {
392086de
AM
34197+ if (parent) {
34198+ h_parent = au_h_dptr(parent, bindex);
5527c038 34199+ if (!h_parent || d_is_negative(h_parent))
392086de
AM
34200+ continue;
34201+ }
1facf9fc 34202+ br = au_sbr(sb, bindex);
34203+ if (au_br_rdonly(br))
34204+ continue;
34205+
34206+ /* sb->s_root for NFS is unreliable */
86dc4139 34207+ h_path.mnt = au_br_mnt(br);
7f207e10
AM
34208+ h_path.dentry = h_path.mnt->mnt_root;
34209+ err = vfs_statfs(&h_path, st);
1facf9fc 34210+ if (unlikely(err)) {
34211+ AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34212+ continue;
34213+ }
34214+
34215+ /* when the available size is equal, select the lower one */
34216+ BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34217+ || sizeof(b) < sizeof(st->f_bsize));
34218+ b = st->f_bavail * st->f_bsize;
34219+ br->br_wbr->wbr_bytes = b;
34220+ if (b >= bavail) {
34221+ bavail = b;
34222+ mfs->mfs_bindex = bindex;
34223+ mfs->mfs_jiffy = jiffies;
34224+ }
34225+ }
34226+
34227+ mfs->mfsrr_bytes = bavail;
34228+ AuDbg("b%d\n", mfs->mfs_bindex);
1c60b727 34229+ kfree(st);
1facf9fc 34230+}
34231+
392086de 34232+static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
1facf9fc 34233+{
34234+ int err;
392086de 34235+ struct dentry *parent;
1facf9fc 34236+ struct super_block *sb;
34237+ struct au_wbr_mfs *mfs;
34238+
34239+ err = au_wbr_create_exp(dentry);
34240+ if (err >= 0)
34241+ goto out;
34242+
34243+ sb = dentry->d_sb;
392086de
AM
34244+ parent = NULL;
34245+ if (au_ftest_wbr(flags, PARENT))
34246+ parent = dget_parent(dentry);
1facf9fc 34247+ mfs = &au_sbi(sb)->si_wbr_mfs;
34248+ mutex_lock(&mfs->mfs_lock);
34249+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34250+ || mfs->mfs_bindex < 0
34251+ || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
392086de 34252+ au_mfs(dentry, parent);
1facf9fc 34253+ mutex_unlock(&mfs->mfs_lock);
34254+ err = mfs->mfs_bindex;
392086de 34255+ dput(parent);
1facf9fc 34256+
4a4d8108
AM
34257+ if (err >= 0)
34258+ err = au_wbr_nonopq(dentry, err);
34259+
4f0767ce 34260+out:
1facf9fc 34261+ AuDbg("b%d\n", err);
34262+ return err;
34263+}
34264+
34265+static int au_wbr_create_init_mfs(struct super_block *sb)
34266+{
34267+ struct au_wbr_mfs *mfs;
34268+
34269+ mfs = &au_sbi(sb)->si_wbr_mfs;
34270+ mutex_init(&mfs->mfs_lock);
34271+ mfs->mfs_jiffy = 0;
34272+ mfs->mfs_bindex = -EROFS;
34273+
34274+ return 0;
34275+}
34276+
34277+static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34278+{
34279+ mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34280+ return 0;
34281+}
34282+
34283+/* ---------------------------------------------------------------------- */
34284+
f2c43d5f
AM
34285+/* top down regardless parent, and then mfs */
34286+static int au_wbr_create_tdmfs(struct dentry *dentry,
34287+ unsigned int flags __maybe_unused)
34288+{
34289+ int err;
34290+ aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34291+ unsigned long long watermark;
34292+ struct super_block *sb;
34293+ struct au_wbr_mfs *mfs;
34294+ struct au_branch *br;
34295+ struct dentry *parent;
34296+
34297+ sb = dentry->d_sb;
34298+ mfs = &au_sbi(sb)->si_wbr_mfs;
34299+ mutex_lock(&mfs->mfs_lock);
34300+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34301+ || mfs->mfs_bindex < 0)
34302+ au_mfs(dentry, /*parent*/NULL);
34303+ watermark = mfs->mfsrr_watermark;
34304+ bmfs = mfs->mfs_bindex;
34305+ mutex_unlock(&mfs->mfs_lock);
34306+
34307+ /* another style of au_wbr_create_exp() */
34308+ bwh = au_dbwh(dentry);
34309+ parent = dget_parent(dentry);
34310+ btail = au_dbtaildir(parent);
34311+ if (bwh >= 0 && bwh < btail)
34312+ btail = bwh;
34313+
34314+ err = au_wbr_nonopq(dentry, btail);
34315+ if (unlikely(err < 0))
34316+ goto out;
34317+ btail = err;
34318+ bfound = -1;
34319+ for (bindex = 0; bindex <= btail; bindex++) {
34320+ br = au_sbr(sb, bindex);
34321+ if (au_br_rdonly(br))
34322+ continue;
34323+ if (br->br_wbr->wbr_bytes > watermark) {
34324+ bfound = bindex;
34325+ break;
34326+ }
34327+ }
34328+ err = bfound;
34329+ if (err < 0)
34330+ err = bmfs;
34331+
34332+out:
34333+ dput(parent);
34334+ AuDbg("b%d\n", err);
34335+ return err;
34336+}
34337+
34338+/* ---------------------------------------------------------------------- */
34339+
1facf9fc 34340+/* most free space and then round robin */
392086de 34341+static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
1facf9fc 34342+{
34343+ int err;
34344+ struct au_wbr_mfs *mfs;
34345+
392086de 34346+ err = au_wbr_create_mfs(dentry, flags);
1facf9fc 34347+ if (err >= 0) {
34348+ mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
dece6358 34349+ mutex_lock(&mfs->mfs_lock);
1facf9fc 34350+ if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
392086de 34351+ err = au_wbr_create_rr(dentry, flags);
dece6358 34352+ mutex_unlock(&mfs->mfs_lock);
1facf9fc 34353+ }
34354+
34355+ AuDbg("b%d\n", err);
34356+ return err;
34357+}
34358+
34359+static int au_wbr_create_init_mfsrr(struct super_block *sb)
34360+{
34361+ int err;
34362+
34363+ au_wbr_create_init_mfs(sb); /* ignore */
34364+ err = au_wbr_create_init_rr(sb);
34365+
34366+ return err;
34367+}
34368+
34369+/* ---------------------------------------------------------------------- */
34370+
34371+/* top down parent and most free space */
392086de 34372+static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
1facf9fc 34373+{
34374+ int err, e2;
34375+ unsigned long long b;
5afbbe0d 34376+ aufs_bindex_t bindex, btop, bbot;
1facf9fc 34377+ struct super_block *sb;
34378+ struct dentry *parent, *h_parent;
34379+ struct au_branch *br;
34380+
392086de 34381+ err = au_wbr_create_tdp(dentry, flags);
1facf9fc 34382+ if (unlikely(err < 0))
34383+ goto out;
34384+ parent = dget_parent(dentry);
5afbbe0d
AM
34385+ btop = au_dbtop(parent);
34386+ bbot = au_dbtaildir(parent);
34387+ if (btop == bbot)
1facf9fc 34388+ goto out_parent; /* success */
34389+
392086de 34390+ e2 = au_wbr_create_mfs(dentry, flags);
1facf9fc 34391+ if (e2 < 0)
34392+ goto out_parent; /* success */
34393+
34394+ /* when the available size is equal, select upper one */
34395+ sb = dentry->d_sb;
34396+ br = au_sbr(sb, err);
34397+ b = br->br_wbr->wbr_bytes;
34398+ AuDbg("b%d, %llu\n", err, b);
34399+
5afbbe0d 34400+ for (bindex = btop; bindex <= bbot; bindex++) {
1facf9fc 34401+ h_parent = au_h_dptr(parent, bindex);
5527c038 34402+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34403+ continue;
34404+
34405+ br = au_sbr(sb, bindex);
34406+ if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34407+ b = br->br_wbr->wbr_bytes;
34408+ err = bindex;
34409+ AuDbg("b%d, %llu\n", err, b);
34410+ }
34411+ }
34412+
4a4d8108
AM
34413+ if (err >= 0)
34414+ err = au_wbr_nonopq(dentry, err);
34415+
4f0767ce 34416+out_parent:
1facf9fc 34417+ dput(parent);
4f0767ce 34418+out:
1facf9fc 34419+ AuDbg("b%d\n", err);
34420+ return err;
34421+}
34422+
34423+/* ---------------------------------------------------------------------- */
34424+
392086de
AM
34425+/*
34426+ * - top down parent
34427+ * - most free space with parent
34428+ * - most free space round-robin regardless parent
34429+ */
34430+static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34431+{
34432+ int err;
34433+ unsigned long long watermark;
34434+ struct super_block *sb;
34435+ struct au_branch *br;
34436+ struct au_wbr_mfs *mfs;
34437+
34438+ err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34439+ if (unlikely(err < 0))
34440+ goto out;
34441+
34442+ sb = dentry->d_sb;
34443+ br = au_sbr(sb, err);
34444+ mfs = &au_sbi(sb)->si_wbr_mfs;
34445+ mutex_lock(&mfs->mfs_lock);
34446+ watermark = mfs->mfsrr_watermark;
34447+ mutex_unlock(&mfs->mfs_lock);
34448+ if (br->br_wbr->wbr_bytes < watermark)
34449+ /* regardless the parent dir */
34450+ err = au_wbr_create_mfsrr(dentry, flags);
34451+
34452+out:
34453+ AuDbg("b%d\n", err);
34454+ return err;
34455+}
34456+
34457+/* ---------------------------------------------------------------------- */
34458+
1facf9fc 34459+/* policies for copyup */
34460+
34461+/* top down parent */
34462+static int au_wbr_copyup_tdp(struct dentry *dentry)
34463+{
392086de 34464+ return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
1facf9fc 34465+}
34466+
34467+/* bottom up parent */
34468+static int au_wbr_copyup_bup(struct dentry *dentry)
34469+{
34470+ int err;
5afbbe0d 34471+ aufs_bindex_t bindex, btop;
1facf9fc 34472+ struct dentry *parent, *h_parent;
34473+ struct super_block *sb;
34474+
34475+ err = -EROFS;
34476+ sb = dentry->d_sb;
34477+ parent = dget_parent(dentry);
5afbbe0d
AM
34478+ btop = au_dbtop(parent);
34479+ for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
1facf9fc 34480+ h_parent = au_h_dptr(parent, bindex);
5527c038 34481+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34482+ continue;
34483+
34484+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34485+ err = bindex;
34486+ break;
34487+ }
34488+ }
34489+ dput(parent);
34490+
34491+ /* bottom up here */
34492+ if (unlikely(err < 0))
5afbbe0d 34493+ err = au_wbr_bu(sb, btop - 1);
1facf9fc 34494+
34495+ AuDbg("b%d\n", err);
34496+ return err;
34497+}
34498+
34499+/* bottom up */
5afbbe0d 34500+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
1facf9fc 34501+{
34502+ int err;
34503+
5afbbe0d 34504+ err = au_wbr_bu(dentry->d_sb, btop);
4a4d8108 34505+ AuDbg("b%d\n", err);
5afbbe0d 34506+ if (err > btop)
4a4d8108 34507+ err = au_wbr_nonopq(dentry, err);
1facf9fc 34508+
34509+ AuDbg("b%d\n", err);
34510+ return err;
34511+}
34512+
076b876e
AM
34513+static int au_wbr_copyup_bu(struct dentry *dentry)
34514+{
34515+ int err;
5afbbe0d 34516+ aufs_bindex_t btop;
076b876e 34517+
5afbbe0d
AM
34518+ btop = au_dbtop(dentry);
34519+ err = au_wbr_do_copyup_bu(dentry, btop);
076b876e
AM
34520+ return err;
34521+}
34522+
1facf9fc 34523+/* ---------------------------------------------------------------------- */
34524+
34525+struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34526+ [AuWbrCopyup_TDP] = {
34527+ .copyup = au_wbr_copyup_tdp
34528+ },
34529+ [AuWbrCopyup_BUP] = {
34530+ .copyup = au_wbr_copyup_bup
34531+ },
34532+ [AuWbrCopyup_BU] = {
34533+ .copyup = au_wbr_copyup_bu
34534+ }
34535+};
34536+
34537+struct au_wbr_create_operations au_wbr_create_ops[] = {
34538+ [AuWbrCreate_TDP] = {
34539+ .create = au_wbr_create_tdp
34540+ },
34541+ [AuWbrCreate_RR] = {
34542+ .create = au_wbr_create_rr,
34543+ .init = au_wbr_create_init_rr
34544+ },
34545+ [AuWbrCreate_MFS] = {
34546+ .create = au_wbr_create_mfs,
34547+ .init = au_wbr_create_init_mfs,
34548+ .fin = au_wbr_create_fin_mfs
34549+ },
34550+ [AuWbrCreate_MFSV] = {
34551+ .create = au_wbr_create_mfs,
34552+ .init = au_wbr_create_init_mfs,
34553+ .fin = au_wbr_create_fin_mfs
34554+ },
34555+ [AuWbrCreate_MFSRR] = {
34556+ .create = au_wbr_create_mfsrr,
34557+ .init = au_wbr_create_init_mfsrr,
34558+ .fin = au_wbr_create_fin_mfs
34559+ },
34560+ [AuWbrCreate_MFSRRV] = {
34561+ .create = au_wbr_create_mfsrr,
34562+ .init = au_wbr_create_init_mfsrr,
34563+ .fin = au_wbr_create_fin_mfs
34564+ },
f2c43d5f
AM
34565+ [AuWbrCreate_TDMFS] = {
34566+ .create = au_wbr_create_tdmfs,
34567+ .init = au_wbr_create_init_mfs,
34568+ .fin = au_wbr_create_fin_mfs
34569+ },
34570+ [AuWbrCreate_TDMFSV] = {
34571+ .create = au_wbr_create_tdmfs,
34572+ .init = au_wbr_create_init_mfs,
34573+ .fin = au_wbr_create_fin_mfs
34574+ },
1facf9fc 34575+ [AuWbrCreate_PMFS] = {
34576+ .create = au_wbr_create_pmfs,
34577+ .init = au_wbr_create_init_mfs,
34578+ .fin = au_wbr_create_fin_mfs
34579+ },
34580+ [AuWbrCreate_PMFSV] = {
34581+ .create = au_wbr_create_pmfs,
34582+ .init = au_wbr_create_init_mfs,
34583+ .fin = au_wbr_create_fin_mfs
392086de
AM
34584+ },
34585+ [AuWbrCreate_PMFSRR] = {
34586+ .create = au_wbr_create_pmfsrr,
34587+ .init = au_wbr_create_init_mfsrr,
34588+ .fin = au_wbr_create_fin_mfs
34589+ },
34590+ [AuWbrCreate_PMFSRRV] = {
34591+ .create = au_wbr_create_pmfsrr,
34592+ .init = au_wbr_create_init_mfsrr,
34593+ .fin = au_wbr_create_fin_mfs
1facf9fc 34594+ }
34595+};
7f207e10
AM
34596diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
34597--- /usr/share/empty/fs/aufs/whout.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 34598+++ linux/fs/aufs/whout.c 2018-08-12 23:43:05.463458173 +0200
062440b3 34599@@ -0,0 +1,1062 @@
cd7a4cd9 34600+// SPDX-License-Identifier: GPL-2.0
1facf9fc 34601+/*
b00004a5 34602+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 34603+ *
34604+ * This program, aufs is free software; you can redistribute it and/or modify
34605+ * it under the terms of the GNU General Public License as published by
34606+ * the Free Software Foundation; either version 2 of the License, or
34607+ * (at your option) any later version.
dece6358
AM
34608+ *
34609+ * This program is distributed in the hope that it will be useful,
34610+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
34611+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34612+ * GNU General Public License for more details.
34613+ *
34614+ * You should have received a copy of the GNU General Public License
523b37e3 34615+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 34616+ */
34617+
34618+/*
34619+ * whiteout for logical deletion and opaque directory
34620+ */
34621+
1facf9fc 34622+#include "aufs.h"
34623+
cd7a4cd9 34624+#define WH_MASK 0444
1facf9fc 34625+
34626+/*
34627+ * If a directory contains this file, then it is opaque. We start with the
34628+ * .wh. flag so that it is blocked by lookup.
34629+ */
0c3ec466
AM
34630+static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
34631+ sizeof(AUFS_WH_DIROPQ) - 1);
1facf9fc 34632+
34633+/*
34634+ * generate whiteout name, which is NOT terminated by NULL.
34635+ * @name: original d_name.name
34636+ * @len: original d_name.len
34637+ * @wh: whiteout qstr
34638+ * returns zero when succeeds, otherwise error.
34639+ * succeeded value as wh->name should be freed by kfree().
34640+ */
34641+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
34642+{
34643+ char *p;
34644+
34645+ if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
34646+ return -ENAMETOOLONG;
34647+
34648+ wh->len = name->len + AUFS_WH_PFX_LEN;
34649+ p = kmalloc(wh->len, GFP_NOFS);
34650+ wh->name = p;
34651+ if (p) {
34652+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
34653+ memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
34654+ /* smp_mb(); */
34655+ return 0;
34656+ }
34657+ return -ENOMEM;
34658+}
34659+
34660+/* ---------------------------------------------------------------------- */
34661+
34662+/*
34663+ * test if the @wh_name exists under @h_parent.
34664+ * @try_sio specifies the necessary of super-io.
34665+ */
076b876e 34666+int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
1facf9fc 34667+{
34668+ int err;
34669+ struct dentry *wh_dentry;
1facf9fc 34670+
1facf9fc 34671+ if (!try_sio)
b4510431 34672+ wh_dentry = vfsub_lkup_one(wh_name, h_parent);
1facf9fc 34673+ else
076b876e 34674+ wh_dentry = au_sio_lkup_one(wh_name, h_parent);
1facf9fc 34675+ err = PTR_ERR(wh_dentry);
2000de60
JR
34676+ if (IS_ERR(wh_dentry)) {
34677+ if (err == -ENAMETOOLONG)
34678+ err = 0;
1facf9fc 34679+ goto out;
2000de60 34680+ }
1facf9fc 34681+
34682+ err = 0;
5527c038 34683+ if (d_is_negative(wh_dentry))
1facf9fc 34684+ goto out_wh; /* success */
34685+
34686+ err = 1;
7e9cd9fe 34687+ if (d_is_reg(wh_dentry))
1facf9fc 34688+ goto out_wh; /* success */
34689+
34690+ err = -EIO;
523b37e3 34691+ AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
5527c038 34692+ wh_dentry, d_inode(wh_dentry)->i_mode);
1facf9fc 34693+
4f0767ce 34694+out_wh:
1facf9fc 34695+ dput(wh_dentry);
4f0767ce 34696+out:
1facf9fc 34697+ return err;
34698+}
34699+
34700+/*
34701+ * test if the @h_dentry sets opaque or not.
34702+ */
076b876e 34703+int au_diropq_test(struct dentry *h_dentry)
1facf9fc 34704+{
34705+ int err;
34706+ struct inode *h_dir;
34707+
5527c038 34708+ h_dir = d_inode(h_dentry);
076b876e 34709+ err = au_wh_test(h_dentry, &diropq_name,
1facf9fc 34710+ au_test_h_perm_sio(h_dir, MAY_EXEC));
34711+ return err;
34712+}
34713+
34714+/*
34715+ * returns a negative dentry whose name is unique and temporary.
34716+ */
34717+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
34718+ struct qstr *prefix)
34719+{
1facf9fc 34720+ struct dentry *dentry;
34721+ int i;
027c5e7a 34722+ char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
4a4d8108 34723+ *name, *p;
027c5e7a 34724+ /* strict atomic_t is unnecessary here */
1facf9fc 34725+ static unsigned short cnt;
34726+ struct qstr qs;
34727+
4a4d8108
AM
34728+ BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
34729+
1facf9fc 34730+ name = defname;
027c5e7a
AM
34731+ qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
34732+ if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
1facf9fc 34733+ dentry = ERR_PTR(-ENAMETOOLONG);
4a4d8108 34734+ if (unlikely(qs.len > NAME_MAX))
1facf9fc 34735+ goto out;
34736+ dentry = ERR_PTR(-ENOMEM);
34737+ name = kmalloc(qs.len + 1, GFP_NOFS);
34738+ if (unlikely(!name))
34739+ goto out;
34740+ }
34741+
34742+ /* doubly whiteout-ed */
34743+ memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
34744+ p = name + AUFS_WH_PFX_LEN * 2;
34745+ memcpy(p, prefix->name, prefix->len);
34746+ p += prefix->len;
34747+ *p++ = '.';
4a4d8108 34748+ AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
1facf9fc 34749+
34750+ qs.name = name;
34751+ for (i = 0; i < 3; i++) {
b752ccd1 34752+ sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
076b876e 34753+ dentry = au_sio_lkup_one(&qs, h_parent);
5527c038 34754+ if (IS_ERR(dentry) || d_is_negative(dentry))
1facf9fc 34755+ goto out_name;
34756+ dput(dentry);
34757+ }
0c3ec466 34758+ /* pr_warn("could not get random name\n"); */
1facf9fc 34759+ dentry = ERR_PTR(-EEXIST);
34760+ AuDbg("%.*s\n", AuLNPair(&qs));
34761+ BUG();
34762+
4f0767ce 34763+out_name:
1facf9fc 34764+ if (name != defname)
1c60b727 34765+ kfree(name);
4f0767ce 34766+out:
4a4d8108 34767+ AuTraceErrPtr(dentry);
1facf9fc 34768+ return dentry;
1facf9fc 34769+}
34770+
34771+/*
34772+ * rename the @h_dentry on @br to the whiteouted temporary name.
34773+ */
34774+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
34775+{
34776+ int err;
34777+ struct path h_path = {
86dc4139 34778+ .mnt = au_br_mnt(br)
1facf9fc 34779+ };
523b37e3 34780+ struct inode *h_dir, *delegated;
1facf9fc 34781+ struct dentry *h_parent;
34782+
34783+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 34784+ h_dir = d_inode(h_parent);
1facf9fc 34785+ IMustLock(h_dir);
34786+
34787+ h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
34788+ err = PTR_ERR(h_path.dentry);
34789+ if (IS_ERR(h_path.dentry))
34790+ goto out;
34791+
34792+ /* under the same dir, no need to lock_rename() */
523b37e3 34793+ delegated = NULL;
f2c43d5f
AM
34794+ err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
34795+ /*flags*/0);
1facf9fc 34796+ AuTraceErr(err);
523b37e3
AM
34797+ if (unlikely(err == -EWOULDBLOCK)) {
34798+ pr_warn("cannot retry for NFSv4 delegation"
34799+ " for an internal rename\n");
34800+ iput(delegated);
34801+ }
1facf9fc 34802+ dput(h_path.dentry);
34803+
4f0767ce 34804+out:
4a4d8108 34805+ AuTraceErr(err);
1facf9fc 34806+ return err;
34807+}
34808+
34809+/* ---------------------------------------------------------------------- */
34810+/*
34811+ * functions for removing a whiteout
34812+ */
34813+
34814+static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
34815+{
523b37e3
AM
34816+ int err, force;
34817+ struct inode *delegated;
1facf9fc 34818+
34819+ /*
34820+ * forces superio when the dir has a sticky bit.
34821+ * this may be a violation of unix fs semantics.
34822+ */
34823+ force = (h_dir->i_mode & S_ISVTX)
5527c038 34824+ && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
523b37e3
AM
34825+ delegated = NULL;
34826+ err = vfsub_unlink(h_dir, h_path, &delegated, force);
34827+ if (unlikely(err == -EWOULDBLOCK)) {
34828+ pr_warn("cannot retry for NFSv4 delegation"
34829+ " for an internal unlink\n");
34830+ iput(delegated);
34831+ }
34832+ return err;
1facf9fc 34833+}
34834+
34835+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
34836+ struct dentry *dentry)
34837+{
34838+ int err;
34839+
34840+ err = do_unlink_wh(h_dir, h_path);
34841+ if (!err && dentry)
34842+ au_set_dbwh(dentry, -1);
34843+
34844+ return err;
34845+}
34846+
34847+static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
34848+ struct au_branch *br)
34849+{
34850+ int err;
34851+ struct path h_path = {
86dc4139 34852+ .mnt = au_br_mnt(br)
1facf9fc 34853+ };
34854+
34855+ err = 0;
b4510431 34856+ h_path.dentry = vfsub_lkup_one(wh, h_parent);
1facf9fc 34857+ if (IS_ERR(h_path.dentry))
34858+ err = PTR_ERR(h_path.dentry);
34859+ else {
5527c038
JR
34860+ if (d_is_reg(h_path.dentry))
34861+ err = do_unlink_wh(d_inode(h_parent), &h_path);
1facf9fc 34862+ dput(h_path.dentry);
34863+ }
34864+
34865+ return err;
34866+}
34867+
34868+/* ---------------------------------------------------------------------- */
34869+/*
34870+ * initialize/clean whiteout for a branch
34871+ */
34872+
34873+static void au_wh_clean(struct inode *h_dir, struct path *whpath,
34874+ const int isdir)
34875+{
34876+ int err;
523b37e3 34877+ struct inode *delegated;
1facf9fc 34878+
5527c038 34879+ if (d_is_negative(whpath->dentry))
1facf9fc 34880+ return;
34881+
86dc4139
AM
34882+ if (isdir)
34883+ err = vfsub_rmdir(h_dir, whpath);
523b37e3
AM
34884+ else {
34885+ delegated = NULL;
34886+ err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
34887+ if (unlikely(err == -EWOULDBLOCK)) {
34888+ pr_warn("cannot retry for NFSv4 delegation"
34889+ " for an internal unlink\n");
34890+ iput(delegated);
34891+ }
34892+ }
1facf9fc 34893+ if (unlikely(err))
523b37e3
AM
34894+ pr_warn("failed removing %pd (%d), ignored.\n",
34895+ whpath->dentry, err);
1facf9fc 34896+}
34897+
34898+static int test_linkable(struct dentry *h_root)
34899+{
5527c038 34900+ struct inode *h_dir = d_inode(h_root);
1facf9fc 34901+
34902+ if (h_dir->i_op->link)
34903+ return 0;
34904+
523b37e3
AM
34905+ pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
34906+ h_root, au_sbtype(h_root->d_sb));
1facf9fc 34907+ return -ENOSYS;
34908+}
34909+
34910+/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
34911+static int au_whdir(struct inode *h_dir, struct path *path)
34912+{
34913+ int err;
34914+
34915+ err = -EEXIST;
5527c038 34916+ if (d_is_negative(path->dentry)) {
cd7a4cd9 34917+ int mode = 0700;
1facf9fc 34918+
34919+ if (au_test_nfs(path->dentry->d_sb))
cd7a4cd9 34920+ mode |= 0111;
86dc4139 34921+ err = vfsub_mkdir(h_dir, path, mode);
2000de60 34922+ } else if (d_is_dir(path->dentry))
1facf9fc 34923+ err = 0;
34924+ else
523b37e3 34925+ pr_err("unknown %pd exists\n", path->dentry);
1facf9fc 34926+
34927+ return err;
34928+}
34929+
34930+struct au_wh_base {
34931+ const struct qstr *name;
34932+ struct dentry *dentry;
34933+};
34934+
34935+static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
34936+ struct path *h_path)
34937+{
34938+ h_path->dentry = base[AuBrWh_BASE].dentry;
34939+ au_wh_clean(h_dir, h_path, /*isdir*/0);
34940+ h_path->dentry = base[AuBrWh_PLINK].dentry;
34941+ au_wh_clean(h_dir, h_path, /*isdir*/1);
34942+ h_path->dentry = base[AuBrWh_ORPH].dentry;
34943+ au_wh_clean(h_dir, h_path, /*isdir*/1);
34944+}
34945+
34946+/*
34947+ * returns tri-state,
c1595e42 34948+ * minus: error, caller should print the message
1facf9fc 34949+ * zero: succuess
c1595e42 34950+ * plus: error, caller should NOT print the message
1facf9fc 34951+ */
34952+static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
34953+ int do_plink, struct au_wh_base base[],
34954+ struct path *h_path)
34955+{
34956+ int err;
34957+ struct inode *h_dir;
34958+
5527c038 34959+ h_dir = d_inode(h_root);
1facf9fc 34960+ h_path->dentry = base[AuBrWh_BASE].dentry;
34961+ au_wh_clean(h_dir, h_path, /*isdir*/0);
34962+ h_path->dentry = base[AuBrWh_PLINK].dentry;
34963+ if (do_plink) {
34964+ err = test_linkable(h_root);
34965+ if (unlikely(err)) {
34966+ err = 1;
34967+ goto out;
34968+ }
34969+
34970+ err = au_whdir(h_dir, h_path);
34971+ if (unlikely(err))
34972+ goto out;
34973+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
34974+ } else
34975+ au_wh_clean(h_dir, h_path, /*isdir*/1);
34976+ h_path->dentry = base[AuBrWh_ORPH].dentry;
34977+ err = au_whdir(h_dir, h_path);
34978+ if (unlikely(err))
34979+ goto out;
34980+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
34981+
4f0767ce 34982+out:
1facf9fc 34983+ return err;
34984+}
34985+
34986+/*
34987+ * for the moment, aufs supports the branch filesystem which does not support
34988+ * link(2). testing on FAT which does not support i_op->setattr() fully either,
34989+ * copyup failed. finally, such filesystem will not be used as the writable
34990+ * branch.
34991+ *
34992+ * returns tri-state, see above.
34993+ */
34994+static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
34995+ int do_plink, struct au_wh_base base[],
34996+ struct path *h_path)
34997+{
34998+ int err;
34999+ struct inode *h_dir;
35000+
1308ab2a 35001+ WbrWhMustWriteLock(wbr);
35002+
1facf9fc 35003+ err = test_linkable(h_root);
35004+ if (unlikely(err)) {
35005+ err = 1;
35006+ goto out;
35007+ }
35008+
35009+ /*
35010+ * todo: should this create be done in /sbin/mount.aufs helper?
35011+ */
35012+ err = -EEXIST;
5527c038
JR
35013+ h_dir = d_inode(h_root);
35014+ if (d_is_negative(base[AuBrWh_BASE].dentry)) {
86dc4139
AM
35015+ h_path->dentry = base[AuBrWh_BASE].dentry;
35016+ err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
7e9cd9fe 35017+ } else if (d_is_reg(base[AuBrWh_BASE].dentry))
1facf9fc 35018+ err = 0;
35019+ else
523b37e3 35020+ pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
1facf9fc 35021+ if (unlikely(err))
35022+ goto out;
35023+
35024+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35025+ if (do_plink) {
35026+ err = au_whdir(h_dir, h_path);
35027+ if (unlikely(err))
35028+ goto out;
35029+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35030+ } else
35031+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35032+ wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35033+
35034+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35035+ err = au_whdir(h_dir, h_path);
35036+ if (unlikely(err))
35037+ goto out;
35038+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35039+
4f0767ce 35040+out:
1facf9fc 35041+ return err;
35042+}
35043+
35044+/*
35045+ * initialize the whiteout base file/dir for @br.
35046+ */
86dc4139 35047+int au_wh_init(struct au_branch *br, struct super_block *sb)
1facf9fc 35048+{
35049+ int err, i;
35050+ const unsigned char do_plink
35051+ = !!au_opt_test(au_mntflags(sb), PLINK);
1facf9fc 35052+ struct inode *h_dir;
86dc4139
AM
35053+ struct path path = br->br_path;
35054+ struct dentry *h_root = path.dentry;
1facf9fc 35055+ struct au_wbr *wbr = br->br_wbr;
35056+ static const struct qstr base_name[] = {
0c3ec466
AM
35057+ [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35058+ sizeof(AUFS_BASE_NAME) - 1),
35059+ [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35060+ sizeof(AUFS_PLINKDIR_NAME) - 1),
35061+ [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35062+ sizeof(AUFS_ORPHDIR_NAME) - 1)
1facf9fc 35063+ };
35064+ struct au_wh_base base[] = {
35065+ [AuBrWh_BASE] = {
35066+ .name = base_name + AuBrWh_BASE,
35067+ .dentry = NULL
35068+ },
35069+ [AuBrWh_PLINK] = {
35070+ .name = base_name + AuBrWh_PLINK,
35071+ .dentry = NULL
35072+ },
35073+ [AuBrWh_ORPH] = {
35074+ .name = base_name + AuBrWh_ORPH,
35075+ .dentry = NULL
35076+ }
35077+ };
35078+
1308ab2a 35079+ if (wbr)
35080+ WbrWhMustWriteLock(wbr);
1facf9fc 35081+
1facf9fc 35082+ for (i = 0; i < AuBrWh_Last; i++) {
35083+ /* doubly whiteouted */
35084+ struct dentry *d;
35085+
35086+ d = au_wh_lkup(h_root, (void *)base[i].name, br);
35087+ err = PTR_ERR(d);
35088+ if (IS_ERR(d))
35089+ goto out;
35090+
35091+ base[i].dentry = d;
35092+ AuDebugOn(wbr
35093+ && wbr->wbr_wh[i]
35094+ && wbr->wbr_wh[i] != base[i].dentry);
35095+ }
35096+
35097+ if (wbr)
35098+ for (i = 0; i < AuBrWh_Last; i++) {
35099+ dput(wbr->wbr_wh[i]);
35100+ wbr->wbr_wh[i] = NULL;
35101+ }
35102+
35103+ err = 0;
1e00d052 35104+ if (!au_br_writable(br->br_perm)) {
5527c038 35105+ h_dir = d_inode(h_root);
1facf9fc 35106+ au_wh_init_ro(h_dir, base, &path);
1e00d052 35107+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 35108+ err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35109+ if (err > 0)
35110+ goto out;
35111+ else if (err)
35112+ goto out_err;
1e00d052 35113+ } else {
1facf9fc 35114+ err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35115+ if (err > 0)
35116+ goto out;
35117+ else if (err)
35118+ goto out_err;
1facf9fc 35119+ }
35120+ goto out; /* success */
35121+
4f0767ce 35122+out_err:
523b37e3
AM
35123+ pr_err("an error(%d) on the writable branch %pd(%s)\n",
35124+ err, h_root, au_sbtype(h_root->d_sb));
4f0767ce 35125+out:
1facf9fc 35126+ for (i = 0; i < AuBrWh_Last; i++)
35127+ dput(base[i].dentry);
35128+ return err;
35129+}
35130+
35131+/* ---------------------------------------------------------------------- */
35132+/*
35133+ * whiteouts are all hard-linked usually.
35134+ * when its link count reaches a ceiling, we create a new whiteout base
35135+ * asynchronously.
35136+ */
35137+
35138+struct reinit_br_wh {
35139+ struct super_block *sb;
35140+ struct au_branch *br;
35141+};
35142+
35143+static void reinit_br_wh(void *arg)
35144+{
35145+ int err;
35146+ aufs_bindex_t bindex;
35147+ struct path h_path;
35148+ struct reinit_br_wh *a = arg;
35149+ struct au_wbr *wbr;
523b37e3 35150+ struct inode *dir, *delegated;
1facf9fc 35151+ struct dentry *h_root;
35152+ struct au_hinode *hdir;
35153+
35154+ err = 0;
35155+ wbr = a->br->br_wbr;
35156+ /* big aufs lock */
35157+ si_noflush_write_lock(a->sb);
35158+ if (!au_br_writable(a->br->br_perm))
35159+ goto out;
35160+ bindex = au_br_index(a->sb, a->br->br_id);
35161+ if (unlikely(bindex < 0))
35162+ goto out;
35163+
1308ab2a 35164+ di_read_lock_parent(a->sb->s_root, AuLock_IR);
5527c038 35165+ dir = d_inode(a->sb->s_root);
1facf9fc 35166+ hdir = au_hi(dir, bindex);
35167+ h_root = au_h_dptr(a->sb->s_root, bindex);
86dc4139 35168+ AuDebugOn(h_root != au_br_dentry(a->br));
1facf9fc 35169+
5afbbe0d 35170+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 35171+ wbr_wh_write_lock(wbr);
35172+ err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35173+ h_root, a->br);
35174+ if (!err) {
86dc4139
AM
35175+ h_path.dentry = wbr->wbr_whbase;
35176+ h_path.mnt = au_br_mnt(a->br);
523b37e3
AM
35177+ delegated = NULL;
35178+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35179+ /*force*/0);
35180+ if (unlikely(err == -EWOULDBLOCK)) {
35181+ pr_warn("cannot retry for NFSv4 delegation"
35182+ " for an internal unlink\n");
35183+ iput(delegated);
35184+ }
1facf9fc 35185+ } else {
523b37e3 35186+ pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
1facf9fc 35187+ err = 0;
35188+ }
35189+ dput(wbr->wbr_whbase);
35190+ wbr->wbr_whbase = NULL;
35191+ if (!err)
86dc4139 35192+ err = au_wh_init(a->br, a->sb);
1facf9fc 35193+ wbr_wh_write_unlock(wbr);
5afbbe0d 35194+ au_hn_inode_unlock(hdir);
1308ab2a 35195+ di_read_unlock(a->sb->s_root, AuLock_IR);
076b876e
AM
35196+ if (!err)
35197+ au_fhsm_wrote(a->sb, bindex, /*force*/0);
1facf9fc 35198+
4f0767ce 35199+out:
1facf9fc 35200+ if (wbr)
35201+ atomic_dec(&wbr->wbr_wh_running);
5afbbe0d 35202+ au_br_put(a->br);
1facf9fc 35203+ si_write_unlock(a->sb);
027c5e7a 35204+ au_nwt_done(&au_sbi(a->sb)->si_nowait);
1c60b727 35205+ kfree(arg);
1facf9fc 35206+ if (unlikely(err))
35207+ AuIOErr("err %d\n", err);
35208+}
35209+
35210+static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35211+{
35212+ int do_dec, wkq_err;
35213+ struct reinit_br_wh *arg;
35214+
35215+ do_dec = 1;
35216+ if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35217+ goto out;
35218+
35219+ /* ignore ENOMEM */
35220+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
35221+ if (arg) {
35222+ /*
35223+ * dec(wh_running), kfree(arg) and dec(br_count)
35224+ * in reinit function
35225+ */
35226+ arg->sb = sb;
35227+ arg->br = br;
5afbbe0d 35228+ au_br_get(br);
53392da6 35229+ wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
1facf9fc 35230+ if (unlikely(wkq_err)) {
35231+ atomic_dec(&br->br_wbr->wbr_wh_running);
5afbbe0d 35232+ au_br_put(br);
1c60b727 35233+ kfree(arg);
1facf9fc 35234+ }
35235+ do_dec = 0;
35236+ }
35237+
4f0767ce 35238+out:
1facf9fc 35239+ if (do_dec)
35240+ atomic_dec(&br->br_wbr->wbr_wh_running);
35241+}
35242+
35243+/* ---------------------------------------------------------------------- */
35244+
35245+/*
35246+ * create the whiteout @wh.
35247+ */
35248+static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35249+ struct dentry *wh)
35250+{
35251+ int err;
35252+ struct path h_path = {
35253+ .dentry = wh
35254+ };
35255+ struct au_branch *br;
35256+ struct au_wbr *wbr;
35257+ struct dentry *h_parent;
523b37e3 35258+ struct inode *h_dir, *delegated;
1facf9fc 35259+
35260+ h_parent = wh->d_parent; /* dir inode is locked */
5527c038 35261+ h_dir = d_inode(h_parent);
1facf9fc 35262+ IMustLock(h_dir);
35263+
35264+ br = au_sbr(sb, bindex);
86dc4139 35265+ h_path.mnt = au_br_mnt(br);
1facf9fc 35266+ wbr = br->br_wbr;
35267+ wbr_wh_read_lock(wbr);
35268+ if (wbr->wbr_whbase) {
523b37e3
AM
35269+ delegated = NULL;
35270+ err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35271+ if (unlikely(err == -EWOULDBLOCK)) {
35272+ pr_warn("cannot retry for NFSv4 delegation"
35273+ " for an internal link\n");
35274+ iput(delegated);
35275+ }
1facf9fc 35276+ if (!err || err != -EMLINK)
35277+ goto out;
35278+
35279+ /* link count full. re-initialize br_whbase. */
35280+ kick_reinit_br_wh(sb, br);
35281+ }
35282+
35283+ /* return this error in this context */
b4510431 35284+ err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
076b876e
AM
35285+ if (!err)
35286+ au_fhsm_wrote(sb, bindex, /*force*/0);
1facf9fc 35287+
4f0767ce 35288+out:
1facf9fc 35289+ wbr_wh_read_unlock(wbr);
35290+ return err;
35291+}
35292+
35293+/* ---------------------------------------------------------------------- */
35294+
35295+/*
35296+ * create or remove the diropq.
35297+ */
35298+static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35299+ unsigned int flags)
35300+{
35301+ struct dentry *opq_dentry, *h_dentry;
35302+ struct super_block *sb;
35303+ struct au_branch *br;
35304+ int err;
35305+
35306+ sb = dentry->d_sb;
35307+ br = au_sbr(sb, bindex);
35308+ h_dentry = au_h_dptr(dentry, bindex);
b4510431 35309+ opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
1facf9fc 35310+ if (IS_ERR(opq_dentry))
35311+ goto out;
35312+
35313+ if (au_ftest_diropq(flags, CREATE)) {
35314+ err = link_or_create_wh(sb, bindex, opq_dentry);
35315+ if (!err) {
35316+ au_set_dbdiropq(dentry, bindex);
35317+ goto out; /* success */
35318+ }
35319+ } else {
35320+ struct path tmp = {
35321+ .dentry = opq_dentry,
86dc4139 35322+ .mnt = au_br_mnt(br)
1facf9fc 35323+ };
5527c038 35324+ err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
1facf9fc 35325+ if (!err)
35326+ au_set_dbdiropq(dentry, -1);
35327+ }
35328+ dput(opq_dentry);
35329+ opq_dentry = ERR_PTR(err);
35330+
4f0767ce 35331+out:
1facf9fc 35332+ return opq_dentry;
35333+}
35334+
35335+struct do_diropq_args {
35336+ struct dentry **errp;
35337+ struct dentry *dentry;
35338+ aufs_bindex_t bindex;
35339+ unsigned int flags;
35340+};
35341+
35342+static void call_do_diropq(void *args)
35343+{
35344+ struct do_diropq_args *a = args;
35345+ *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35346+}
35347+
35348+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35349+ unsigned int flags)
35350+{
35351+ struct dentry *diropq, *h_dentry;
35352+
35353+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 35354+ if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
1facf9fc 35355+ diropq = do_diropq(dentry, bindex, flags);
35356+ else {
35357+ int wkq_err;
35358+ struct do_diropq_args args = {
35359+ .errp = &diropq,
35360+ .dentry = dentry,
35361+ .bindex = bindex,
35362+ .flags = flags
35363+ };
35364+
35365+ wkq_err = au_wkq_wait(call_do_diropq, &args);
35366+ if (unlikely(wkq_err))
35367+ diropq = ERR_PTR(wkq_err);
35368+ }
35369+
35370+ return diropq;
35371+}
35372+
35373+/* ---------------------------------------------------------------------- */
35374+
35375+/*
35376+ * lookup whiteout dentry.
35377+ * @h_parent: lower parent dentry which must exist and be locked
35378+ * @base_name: name of dentry which will be whiteouted
35379+ * returns dentry for whiteout.
35380+ */
35381+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35382+ struct au_branch *br)
35383+{
35384+ int err;
35385+ struct qstr wh_name;
35386+ struct dentry *wh_dentry;
35387+
35388+ err = au_wh_name_alloc(&wh_name, base_name);
35389+ wh_dentry = ERR_PTR(err);
35390+ if (!err) {
b4510431 35391+ wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
1c60b727 35392+ kfree(wh_name.name);
1facf9fc 35393+ }
35394+ return wh_dentry;
35395+}
35396+
35397+/*
35398+ * link/create a whiteout for @dentry on @bindex.
35399+ */
35400+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35401+ struct dentry *h_parent)
35402+{
35403+ struct dentry *wh_dentry;
35404+ struct super_block *sb;
35405+ int err;
35406+
35407+ sb = dentry->d_sb;
35408+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
5527c038 35409+ if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
1facf9fc 35410+ err = link_or_create_wh(sb, bindex, wh_dentry);
076b876e 35411+ if (!err) {
1facf9fc 35412+ au_set_dbwh(dentry, bindex);
076b876e
AM
35413+ au_fhsm_wrote(sb, bindex, /*force*/0);
35414+ } else {
1facf9fc 35415+ dput(wh_dentry);
35416+ wh_dentry = ERR_PTR(err);
35417+ }
35418+ }
35419+
35420+ return wh_dentry;
35421+}
35422+
35423+/* ---------------------------------------------------------------------- */
35424+
35425+/* Delete all whiteouts in this directory on branch bindex. */
35426+static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
35427+ aufs_bindex_t bindex, struct au_branch *br)
35428+{
35429+ int err;
35430+ unsigned long ul, n;
35431+ struct qstr wh_name;
35432+ char *p;
35433+ struct hlist_head *head;
c06a8ce3 35434+ struct au_vdir_wh *pos;
1facf9fc 35435+ struct au_vdir_destr *str;
35436+
35437+ err = -ENOMEM;
537831f9 35438+ p = (void *)__get_free_page(GFP_NOFS);
1facf9fc 35439+ wh_name.name = p;
35440+ if (unlikely(!wh_name.name))
35441+ goto out;
35442+
35443+ err = 0;
35444+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35445+ p += AUFS_WH_PFX_LEN;
35446+ n = whlist->nh_num;
35447+ head = whlist->nh_head;
35448+ for (ul = 0; !err && ul < n; ul++, head++) {
c06a8ce3
AM
35449+ hlist_for_each_entry(pos, head, wh_hash) {
35450+ if (pos->wh_bindex != bindex)
1facf9fc 35451+ continue;
35452+
c06a8ce3 35453+ str = &pos->wh_str;
1facf9fc 35454+ if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35455+ memcpy(p, str->name, str->len);
35456+ wh_name.len = AUFS_WH_PFX_LEN + str->len;
35457+ err = unlink_wh_name(h_dentry, &wh_name, br);
35458+ if (!err)
35459+ continue;
35460+ break;
35461+ }
35462+ AuIOErr("whiteout name too long %.*s\n",
35463+ str->len, str->name);
35464+ err = -EIO;
35465+ break;
35466+ }
35467+ }
1c60b727 35468+ free_page((unsigned long)wh_name.name);
1facf9fc 35469+
4f0767ce 35470+out:
1facf9fc 35471+ return err;
35472+}
35473+
35474+struct del_wh_children_args {
35475+ int *errp;
35476+ struct dentry *h_dentry;
1308ab2a 35477+ struct au_nhash *whlist;
1facf9fc 35478+ aufs_bindex_t bindex;
35479+ struct au_branch *br;
35480+};
35481+
35482+static void call_del_wh_children(void *args)
35483+{
35484+ struct del_wh_children_args *a = args;
1308ab2a 35485+ *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
1facf9fc 35486+}
35487+
35488+/* ---------------------------------------------------------------------- */
35489+
35490+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35491+{
35492+ struct au_whtmp_rmdir *whtmp;
dece6358 35493+ int err;
1308ab2a 35494+ unsigned int rdhash;
dece6358
AM
35495+
35496+ SiMustAnyLock(sb);
1facf9fc 35497+
be52b249 35498+ whtmp = kzalloc(sizeof(*whtmp), gfp);
dece6358
AM
35499+ if (unlikely(!whtmp)) {
35500+ whtmp = ERR_PTR(-ENOMEM);
1facf9fc 35501+ goto out;
dece6358 35502+ }
1facf9fc 35503+
1308ab2a 35504+ /* no estimation for dir size */
35505+ rdhash = au_sbi(sb)->si_rdhash;
35506+ if (!rdhash)
35507+ rdhash = AUFS_RDHASH_DEF;
35508+ err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35509+ if (unlikely(err)) {
1c60b727 35510+ kfree(whtmp);
1308ab2a 35511+ whtmp = ERR_PTR(err);
35512+ }
dece6358 35513+
4f0767ce 35514+out:
dece6358 35515+ return whtmp;
1facf9fc 35516+}
35517+
35518+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35519+{
027c5e7a 35520+ if (whtmp->br)
5afbbe0d 35521+ au_br_put(whtmp->br);
1facf9fc 35522+ dput(whtmp->wh_dentry);
35523+ iput(whtmp->dir);
dece6358 35524+ au_nhash_wh_free(&whtmp->whlist);
1c60b727 35525+ kfree(whtmp);
1facf9fc 35526+}
35527+
35528+/*
35529+ * rmdir the whiteouted temporary named dir @h_dentry.
35530+ * @whlist: whiteouted children.
35531+ */
35532+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35533+ struct dentry *wh_dentry, struct au_nhash *whlist)
35534+{
35535+ int err;
2000de60 35536+ unsigned int h_nlink;
1facf9fc 35537+ struct path h_tmp;
35538+ struct inode *wh_inode, *h_dir;
35539+ struct au_branch *br;
35540+
5527c038 35541+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
1facf9fc 35542+ IMustLock(h_dir);
35543+
35544+ br = au_sbr(dir->i_sb, bindex);
5527c038 35545+ wh_inode = d_inode(wh_dentry);
febd17d6 35546+ inode_lock_nested(wh_inode, AuLsc_I_CHILD);
1facf9fc 35547+
35548+ /*
35549+ * someone else might change some whiteouts while we were sleeping.
35550+ * it means this whlist may have an obsoleted entry.
35551+ */
35552+ if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
35553+ err = del_wh_children(wh_dentry, whlist, bindex, br);
35554+ else {
35555+ int wkq_err;
35556+ struct del_wh_children_args args = {
35557+ .errp = &err,
35558+ .h_dentry = wh_dentry,
1308ab2a 35559+ .whlist = whlist,
1facf9fc 35560+ .bindex = bindex,
35561+ .br = br
35562+ };
35563+
35564+ wkq_err = au_wkq_wait(call_del_wh_children, &args);
35565+ if (unlikely(wkq_err))
35566+ err = wkq_err;
35567+ }
febd17d6 35568+ inode_unlock(wh_inode);
1facf9fc 35569+
35570+ if (!err) {
35571+ h_tmp.dentry = wh_dentry;
86dc4139 35572+ h_tmp.mnt = au_br_mnt(br);
2000de60 35573+ h_nlink = h_dir->i_nlink;
1facf9fc 35574+ err = vfsub_rmdir(h_dir, &h_tmp);
2000de60
JR
35575+ /* some fs doesn't change the parent nlink in some cases */
35576+ h_nlink -= h_dir->i_nlink;
1facf9fc 35577+ }
35578+
35579+ if (!err) {
5afbbe0d 35580+ if (au_ibtop(dir) == bindex) {
7f207e10 35581+ /* todo: dir->i_mutex is necessary */
1facf9fc 35582+ au_cpup_attr_timesizes(dir);
2000de60
JR
35583+ if (h_nlink)
35584+ vfsub_drop_nlink(dir);
1facf9fc 35585+ }
35586+ return 0; /* success */
35587+ }
35588+
523b37e3 35589+ pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
1facf9fc 35590+ return err;
35591+}
35592+
35593+static void call_rmdir_whtmp(void *args)
35594+{
35595+ int err;
e49829fe 35596+ aufs_bindex_t bindex;
1facf9fc 35597+ struct au_whtmp_rmdir *a = args;
35598+ struct super_block *sb;
35599+ struct dentry *h_parent;
35600+ struct inode *h_dir;
1facf9fc 35601+ struct au_hinode *hdir;
35602+
35603+ /* rmdir by nfsd may cause deadlock with this i_mutex */
febd17d6 35604+ /* inode_lock(a->dir); */
e49829fe 35605+ err = -EROFS;
1facf9fc 35606+ sb = a->dir->i_sb;
e49829fe
JR
35607+ si_read_lock(sb, !AuLock_FLUSH);
35608+ if (!au_br_writable(a->br->br_perm))
35609+ goto out;
35610+ bindex = au_br_index(sb, a->br->br_id);
35611+ if (unlikely(bindex < 0))
1facf9fc 35612+ goto out;
35613+
35614+ err = -EIO;
1facf9fc 35615+ ii_write_lock_parent(a->dir);
35616+ h_parent = dget_parent(a->wh_dentry);
5527c038 35617+ h_dir = d_inode(h_parent);
e49829fe 35618+ hdir = au_hi(a->dir, bindex);
86dc4139
AM
35619+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
35620+ if (unlikely(err))
35621+ goto out_mnt;
5afbbe0d 35622+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
e49829fe
JR
35623+ err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
35624+ a->br);
86dc4139
AM
35625+ if (!err)
35626+ err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
5afbbe0d 35627+ au_hn_inode_unlock(hdir);
86dc4139
AM
35628+ vfsub_mnt_drop_write(au_br_mnt(a->br));
35629+
35630+out_mnt:
1facf9fc 35631+ dput(h_parent);
35632+ ii_write_unlock(a->dir);
4f0767ce 35633+out:
febd17d6 35634+ /* inode_unlock(a->dir); */
1facf9fc 35635+ au_whtmp_rmdir_free(a);
027c5e7a
AM
35636+ si_read_unlock(sb);
35637+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 35638+ if (unlikely(err))
35639+ AuIOErr("err %d\n", err);
35640+}
35641+
35642+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35643+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
35644+{
35645+ int wkq_err;
e49829fe 35646+ struct super_block *sb;
1facf9fc 35647+
35648+ IMustLock(dir);
35649+
35650+ /* all post-process will be done in do_rmdir_whtmp(). */
e49829fe 35651+ sb = dir->i_sb;
1facf9fc 35652+ args->dir = au_igrab(dir);
e49829fe 35653+ args->br = au_sbr(sb, bindex);
5afbbe0d 35654+ au_br_get(args->br);
1facf9fc 35655+ args->wh_dentry = dget(wh_dentry);
53392da6 35656+ wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
1facf9fc 35657+ if (unlikely(wkq_err)) {
523b37e3 35658+ pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
1facf9fc 35659+ au_whtmp_rmdir_free(args);
35660+ }
35661+}
7f207e10
AM
35662diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
35663--- /usr/share/empty/fs/aufs/whout.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 35664+++ linux/fs/aufs/whout.h 2018-08-12 23:43:05.463458173 +0200
062440b3
AM
35665@@ -0,0 +1,86 @@
35666+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 35667+/*
b00004a5 35668+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 35669+ *
35670+ * This program, aufs is free software; you can redistribute it and/or modify
35671+ * it under the terms of the GNU General Public License as published by
35672+ * the Free Software Foundation; either version 2 of the License, or
35673+ * (at your option) any later version.
dece6358
AM
35674+ *
35675+ * This program is distributed in the hope that it will be useful,
35676+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
35677+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35678+ * GNU General Public License for more details.
35679+ *
35680+ * You should have received a copy of the GNU General Public License
523b37e3 35681+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 35682+ */
35683+
35684+/*
35685+ * whiteout for logical deletion and opaque directory
35686+ */
35687+
35688+#ifndef __AUFS_WHOUT_H__
35689+#define __AUFS_WHOUT_H__
35690+
35691+#ifdef __KERNEL__
35692+
1facf9fc 35693+#include "dir.h"
35694+
35695+/* whout.c */
35696+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
076b876e
AM
35697+int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
35698+int au_diropq_test(struct dentry *h_dentry);
3c1bdaff 35699+struct au_branch;
1facf9fc 35700+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35701+ struct qstr *prefix);
35702+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
35703+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35704+ struct dentry *dentry);
86dc4139 35705+int au_wh_init(struct au_branch *br, struct super_block *sb);
1facf9fc 35706+
35707+/* diropq flags */
35708+#define AuDiropq_CREATE 1
35709+#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
7f207e10
AM
35710+#define au_fset_diropq(flags, name) \
35711+ do { (flags) |= AuDiropq_##name; } while (0)
35712+#define au_fclr_diropq(flags, name) \
35713+ do { (flags) &= ~AuDiropq_##name; } while (0)
1facf9fc 35714+
35715+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35716+ unsigned int flags);
35717+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35718+ struct au_branch *br);
35719+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35720+ struct dentry *h_parent);
35721+
35722+/* real rmdir for the whiteout-ed dir */
35723+struct au_whtmp_rmdir {
35724+ struct inode *dir;
e49829fe 35725+ struct au_branch *br;
1facf9fc 35726+ struct dentry *wh_dentry;
dece6358 35727+ struct au_nhash whlist;
1facf9fc 35728+};
35729+
35730+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
35731+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
35732+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35733+ struct dentry *wh_dentry, struct au_nhash *whlist);
35734+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35735+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
35736+
35737+/* ---------------------------------------------------------------------- */
35738+
35739+static inline struct dentry *au_diropq_create(struct dentry *dentry,
35740+ aufs_bindex_t bindex)
35741+{
35742+ return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
35743+}
35744+
35745+static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
35746+{
35747+ return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
35748+}
35749+
35750+#endif /* __KERNEL__ */
35751+#endif /* __AUFS_WHOUT_H__ */
7f207e10
AM
35752diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
35753--- /usr/share/empty/fs/aufs/wkq.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 35754+++ linux/fs/aufs/wkq.c 2018-08-12 23:43:05.463458173 +0200
062440b3 35755@@ -0,0 +1,391 @@
cd7a4cd9 35756+// SPDX-License-Identifier: GPL-2.0
1facf9fc 35757+/*
b00004a5 35758+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 35759+ *
35760+ * This program, aufs is free software; you can redistribute it and/or modify
35761+ * it under the terms of the GNU General Public License as published by
35762+ * the Free Software Foundation; either version 2 of the License, or
35763+ * (at your option) any later version.
dece6358
AM
35764+ *
35765+ * This program is distributed in the hope that it will be useful,
35766+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
35767+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35768+ * GNU General Public License for more details.
35769+ *
35770+ * You should have received a copy of the GNU General Public License
523b37e3 35771+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 35772+ */
35773+
35774+/*
35775+ * workqueue for asynchronous/super-io operations
35776+ * todo: try new dredential scheme
35777+ */
35778+
dece6358 35779+#include <linux/module.h>
1facf9fc 35780+#include "aufs.h"
35781+
9dbd164d 35782+/* internal workqueue named AUFS_WKQ_NAME */
b752ccd1 35783+
9dbd164d 35784+static struct workqueue_struct *au_wkq;
1facf9fc 35785+
35786+struct au_wkinfo {
35787+ struct work_struct wk;
7f207e10 35788+ struct kobject *kobj;
1facf9fc 35789+
35790+ unsigned int flags; /* see wkq.h */
35791+
35792+ au_wkq_func_t func;
35793+ void *args;
35794+
8b6a4947
AM
35795+#ifdef CONFIG_LOCKDEP
35796+ int dont_check;
35797+ struct held_lock **hlock;
35798+#endif
35799+
1facf9fc 35800+ struct completion *comp;
35801+};
35802+
35803+/* ---------------------------------------------------------------------- */
8b6a4947
AM
35804+/*
35805+ * Aufs passes some operations to the workqueue such as the internal copyup.
35806+ * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
35807+ * job run by workqueue depends upon the locks acquired in the other task.
35808+ * Delegating a small operation to the workqueue, aufs passes its lockdep
35809+ * information too. And the job in the workqueue restores the info in order to
35810+ * pretend as if it acquired those locks. This is just to make LOCKDEP work
35811+ * correctly and expectedly.
35812+ */
35813+
35814+#ifndef CONFIG_LOCKDEP
35815+AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
35816+AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
35817+AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
35818+AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
35819+AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
35820+#else
35821+static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
35822+{
35823+ wkinfo->hlock = NULL;
35824+ wkinfo->dont_check = 0;
35825+}
35826+
35827+/*
35828+ * 1: matched
35829+ * 0: unmatched
35830+ */
35831+static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
35832+{
35833+ static DEFINE_SPINLOCK(spin);
35834+ static struct {
35835+ char *name;
35836+ struct lock_class_key *key;
35837+ } a[] = {
35838+ { .name = "&sbinfo->si_rwsem" },
35839+ { .name = "&finfo->fi_rwsem" },
35840+ { .name = "&dinfo->di_rwsem" },
35841+ { .name = "&iinfo->ii_rwsem" }
35842+ };
35843+ static int set;
35844+ int i;
35845+
35846+ /* lockless read from 'set.' see below */
35847+ if (set == ARRAY_SIZE(a)) {
35848+ for (i = 0; i < ARRAY_SIZE(a); i++)
35849+ if (a[i].key == key)
35850+ goto match;
35851+ goto unmatch;
35852+ }
35853+
35854+ spin_lock(&spin);
35855+ if (set)
35856+ for (i = 0; i < ARRAY_SIZE(a); i++)
35857+ if (a[i].key == key) {
35858+ spin_unlock(&spin);
35859+ goto match;
35860+ }
35861+ for (i = 0; i < ARRAY_SIZE(a); i++) {
35862+ if (a[i].key) {
35863+ if (unlikely(a[i].key == key)) { /* rare but possible */
35864+ spin_unlock(&spin);
35865+ goto match;
35866+ } else
35867+ continue;
35868+ }
35869+ if (strstr(a[i].name, name)) {
35870+ /*
35871+ * the order of these three lines is important for the
35872+ * lockless read above.
35873+ */
35874+ a[i].key = key;
35875+ spin_unlock(&spin);
35876+ set++;
35877+ /* AuDbg("%d, %s\n", set, name); */
35878+ goto match;
35879+ }
35880+ }
35881+ spin_unlock(&spin);
35882+ goto unmatch;
35883+
35884+match:
35885+ return 1;
35886+unmatch:
35887+ return 0;
35888+}
35889+
35890+static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
35891+{
35892+ int err, n;
35893+ struct task_struct *curr;
35894+ struct held_lock **hl, *held_locks, *p;
35895+
35896+ err = 0;
35897+ curr = current;
35898+ wkinfo->dont_check = lockdep_recursing(curr);
35899+ if (wkinfo->dont_check)
35900+ goto out;
35901+ n = curr->lockdep_depth;
35902+ if (!n)
35903+ goto out;
35904+
35905+ err = -ENOMEM;
35906+ wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
35907+ if (unlikely(!wkinfo->hlock))
35908+ goto out;
35909+
35910+ err = 0;
35911+#if 0
35912+ if (0 && au_debug_test()) /* left for debugging */
35913+ lockdep_print_held_locks(curr);
35914+#endif
35915+ held_locks = curr->held_locks;
35916+ hl = wkinfo->hlock;
35917+ while (n--) {
35918+ p = held_locks++;
35919+ if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
35920+ *hl++ = p;
35921+ }
35922+ *hl = NULL;
35923+
35924+out:
35925+ return err;
35926+}
35927+
35928+static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
35929+{
35930+ kfree(wkinfo->hlock);
35931+}
35932+
35933+static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
35934+{
35935+ struct held_lock *p, **hl = wkinfo->hlock;
35936+ int subclass;
35937+
35938+ if (wkinfo->dont_check)
35939+ lockdep_off();
35940+ if (!hl)
35941+ return;
35942+ while ((p = *hl++)) { /* assignment */
35943+ subclass = lockdep_hlock_class(p)->subclass;
35944+ /* AuDbg("%s, %d\n", p->instance->name, subclass); */
35945+ if (p->read)
35946+ rwsem_acquire_read(p->instance, subclass, 0,
35947+ /*p->acquire_ip*/_RET_IP_);
35948+ else
35949+ rwsem_acquire(p->instance, subclass, 0,
35950+ /*p->acquire_ip*/_RET_IP_);
35951+ }
35952+}
35953+
35954+static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
35955+{
35956+ struct held_lock *p, **hl = wkinfo->hlock;
35957+
35958+ if (wkinfo->dont_check)
35959+ lockdep_on();
35960+ if (!hl)
35961+ return;
35962+ while ((p = *hl++)) /* assignment */
35963+ rwsem_release(p->instance, 0, /*p->acquire_ip*/_RET_IP_);
35964+}
35965+#endif
1facf9fc 35966+
1facf9fc 35967+static void wkq_func(struct work_struct *wk)
35968+{
35969+ struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
35970+
2dfbb274 35971+ AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
7f207e10
AM
35972+ AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
35973+
8b6a4947 35974+ au_wkq_lockdep_pre(wkinfo);
1facf9fc 35975+ wkinfo->func(wkinfo->args);
8b6a4947 35976+ au_wkq_lockdep_post(wkinfo);
1facf9fc 35977+ if (au_ftest_wkq(wkinfo->flags, WAIT))
35978+ complete(wkinfo->comp);
35979+ else {
7f207e10 35980+ kobject_put(wkinfo->kobj);
9dbd164d 35981+ module_put(THIS_MODULE); /* todo: ?? */
1c60b727 35982+ kfree(wkinfo);
1facf9fc 35983+ }
35984+}
35985+
35986+/*
35987+ * Since struct completion is large, try allocating it dynamically.
35988+ */
c2b27bf2 35989+#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
1facf9fc 35990+#define AuWkqCompDeclare(name) struct completion *comp = NULL
35991+
35992+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
35993+{
35994+ *comp = kmalloc(sizeof(**comp), GFP_NOFS);
35995+ if (*comp) {
35996+ init_completion(*comp);
35997+ wkinfo->comp = *comp;
35998+ return 0;
35999+ }
36000+ return -ENOMEM;
36001+}
36002+
36003+static void au_wkq_comp_free(struct completion *comp)
36004+{
1c60b727 36005+ kfree(comp);
1facf9fc 36006+}
36007+
36008+#else
36009+
36010+/* no braces */
36011+#define AuWkqCompDeclare(name) \
36012+ DECLARE_COMPLETION_ONSTACK(_ ## name); \
36013+ struct completion *comp = &_ ## name
36014+
36015+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36016+{
36017+ wkinfo->comp = *comp;
36018+ return 0;
36019+}
36020+
36021+static void au_wkq_comp_free(struct completion *comp __maybe_unused)
36022+{
36023+ /* empty */
36024+}
36025+#endif /* 4KSTACKS */
36026+
53392da6 36027+static void au_wkq_run(struct au_wkinfo *wkinfo)
1facf9fc 36028+{
53392da6
AM
36029+ if (au_ftest_wkq(wkinfo->flags, NEST)) {
36030+ if (au_wkq_test()) {
38d290e6
JR
36031+ AuWarn1("wkq from wkq, unless silly-rename on NFS,"
36032+ " due to a dead dir by UDBA?\n");
53392da6
AM
36033+ AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36034+ }
36035+ } else
36036+ au_dbg_verify_kthread();
36037+
36038+ if (au_ftest_wkq(wkinfo->flags, WAIT)) {
a1f66529 36039+ INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
9dbd164d 36040+ queue_work(au_wkq, &wkinfo->wk);
4a4d8108
AM
36041+ } else {
36042+ INIT_WORK(&wkinfo->wk, wkq_func);
36043+ schedule_work(&wkinfo->wk);
36044+ }
1facf9fc 36045+}
36046+
7f207e10
AM
36047+/*
36048+ * Be careful. It is easy to make deadlock happen.
36049+ * processA: lock, wkq and wait
36050+ * processB: wkq and wait, lock in wkq
36051+ * --> deadlock
36052+ */
b752ccd1 36053+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
1facf9fc 36054+{
36055+ int err;
36056+ AuWkqCompDeclare(comp);
36057+ struct au_wkinfo wkinfo = {
b752ccd1 36058+ .flags = flags,
1facf9fc 36059+ .func = func,
36060+ .args = args
36061+ };
36062+
36063+ err = au_wkq_comp_alloc(&wkinfo, &comp);
8b6a4947
AM
36064+ if (unlikely(err))
36065+ goto out;
36066+ err = au_wkq_lockdep_alloc(&wkinfo);
36067+ if (unlikely(err))
36068+ goto out_comp;
1facf9fc 36069+ if (!err) {
53392da6 36070+ au_wkq_run(&wkinfo);
1facf9fc 36071+ /* no timeout, no interrupt */
36072+ wait_for_completion(wkinfo.comp);
1facf9fc 36073+ }
8b6a4947 36074+ au_wkq_lockdep_free(&wkinfo);
1facf9fc 36075+
8b6a4947
AM
36076+out_comp:
36077+ au_wkq_comp_free(comp);
36078+out:
36079+ destroy_work_on_stack(&wkinfo.wk);
1facf9fc 36080+ return err;
1facf9fc 36081+}
36082+
027c5e7a
AM
36083+/*
36084+ * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36085+ * problem in a concurrent umounting.
36086+ */
53392da6
AM
36087+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36088+ unsigned int flags)
1facf9fc 36089+{
36090+ int err;
36091+ struct au_wkinfo *wkinfo;
36092+
f0c0a007 36093+ atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
1facf9fc 36094+
36095+ /*
36096+ * wkq_func() must free this wkinfo.
36097+ * it highly depends upon the implementation of workqueue.
36098+ */
36099+ err = 0;
36100+ wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36101+ if (wkinfo) {
7f207e10 36102+ wkinfo->kobj = &au_sbi(sb)->si_kobj;
53392da6 36103+ wkinfo->flags = flags & ~AuWkq_WAIT;
1facf9fc 36104+ wkinfo->func = func;
36105+ wkinfo->args = args;
36106+ wkinfo->comp = NULL;
8b6a4947 36107+ au_wkq_lockdep_init(wkinfo);
7f207e10 36108+ kobject_get(wkinfo->kobj);
9dbd164d 36109+ __module_get(THIS_MODULE); /* todo: ?? */
1facf9fc 36110+
53392da6 36111+ au_wkq_run(wkinfo);
1facf9fc 36112+ } else {
36113+ err = -ENOMEM;
e49829fe 36114+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 36115+ }
36116+
36117+ return err;
36118+}
36119+
36120+/* ---------------------------------------------------------------------- */
36121+
36122+void au_nwt_init(struct au_nowait_tasks *nwt)
36123+{
f0c0a007
AM
36124+ atomic_set(&nwt->nw_len, 0);
36125+ /* smp_mb(); */ /* atomic_set */
1facf9fc 36126+ init_waitqueue_head(&nwt->nw_wq);
36127+}
36128+
36129+void au_wkq_fin(void)
36130+{
9dbd164d 36131+ destroy_workqueue(au_wkq);
1facf9fc 36132+}
36133+
36134+int __init au_wkq_init(void)
36135+{
9dbd164d 36136+ int err;
b752ccd1
AM
36137+
36138+ err = 0;
86dc4139 36139+ au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
9dbd164d
AM
36140+ if (IS_ERR(au_wkq))
36141+ err = PTR_ERR(au_wkq);
36142+ else if (!au_wkq)
36143+ err = -ENOMEM;
b752ccd1
AM
36144+
36145+ return err;
1facf9fc 36146+}
7f207e10
AM
36147diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36148--- /usr/share/empty/fs/aufs/wkq.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 36149+++ linux/fs/aufs/wkq.h 2018-08-12 23:43:05.463458173 +0200
062440b3
AM
36150@@ -0,0 +1,94 @@
36151+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 36152+/*
b00004a5 36153+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 36154+ *
36155+ * This program, aufs is free software; you can redistribute it and/or modify
36156+ * it under the terms of the GNU General Public License as published by
36157+ * the Free Software Foundation; either version 2 of the License, or
36158+ * (at your option) any later version.
dece6358
AM
36159+ *
36160+ * This program is distributed in the hope that it will be useful,
36161+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36162+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36163+ * GNU General Public License for more details.
36164+ *
36165+ * You should have received a copy of the GNU General Public License
523b37e3 36166+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36167+ */
36168+
36169+/*
36170+ * workqueue for asynchronous/super-io operations
36171+ * todo: try new credentials management scheme
36172+ */
36173+
36174+#ifndef __AUFS_WKQ_H__
36175+#define __AUFS_WKQ_H__
36176+
36177+#ifdef __KERNEL__
36178+
8b6a4947 36179+#include <linux/wait.h>
5afbbe0d 36180+
dece6358
AM
36181+struct super_block;
36182+
1facf9fc 36183+/* ---------------------------------------------------------------------- */
36184+
36185+/*
36186+ * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36187+ */
36188+struct au_nowait_tasks {
f0c0a007 36189+ atomic_t nw_len;
1facf9fc 36190+ wait_queue_head_t nw_wq;
36191+};
36192+
36193+/* ---------------------------------------------------------------------- */
36194+
36195+typedef void (*au_wkq_func_t)(void *args);
36196+
36197+/* wkq flags */
36198+#define AuWkq_WAIT 1
9dbd164d 36199+#define AuWkq_NEST (1 << 1)
1facf9fc 36200+#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
7f207e10
AM
36201+#define au_fset_wkq(flags, name) \
36202+ do { (flags) |= AuWkq_##name; } while (0)
36203+#define au_fclr_wkq(flags, name) \
36204+ do { (flags) &= ~AuWkq_##name; } while (0)
1facf9fc 36205+
9dbd164d
AM
36206+#ifndef CONFIG_AUFS_HNOTIFY
36207+#undef AuWkq_NEST
36208+#define AuWkq_NEST 0
36209+#endif
36210+
1facf9fc 36211+/* wkq.c */
b752ccd1 36212+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
53392da6
AM
36213+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36214+ unsigned int flags);
1facf9fc 36215+void au_nwt_init(struct au_nowait_tasks *nwt);
36216+int __init au_wkq_init(void);
36217+void au_wkq_fin(void);
36218+
36219+/* ---------------------------------------------------------------------- */
36220+
53392da6
AM
36221+static inline int au_wkq_test(void)
36222+{
36223+ return current->flags & PF_WQ_WORKER;
36224+}
36225+
b752ccd1 36226+static inline int au_wkq_wait(au_wkq_func_t func, void *args)
1facf9fc 36227+{
b752ccd1 36228+ return au_wkq_do_wait(AuWkq_WAIT, func, args);
1facf9fc 36229+}
36230+
36231+static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36232+{
f0c0a007 36233+ if (atomic_dec_and_test(&nwt->nw_len))
1facf9fc 36234+ wake_up_all(&nwt->nw_wq);
36235+}
36236+
36237+static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36238+{
f0c0a007 36239+ wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
1facf9fc 36240+ return 0;
36241+}
36242+
36243+#endif /* __KERNEL__ */
36244+#endif /* __AUFS_WKQ_H__ */
c1595e42
JR
36245diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36246--- /usr/share/empty/fs/aufs/xattr.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 36247+++ linux/fs/aufs/xattr.c 2018-08-12 23:43:05.466791610 +0200
062440b3 36248@@ -0,0 +1,356 @@
cd7a4cd9 36249+// SPDX-License-Identifier: GPL-2.0
c1595e42 36250+/*
b00004a5 36251+ * Copyright (C) 2014-2018 Junjiro R. Okajima
c1595e42
JR
36252+ *
36253+ * This program, aufs is free software; you can redistribute it and/or modify
36254+ * it under the terms of the GNU General Public License as published by
36255+ * the Free Software Foundation; either version 2 of the License, or
36256+ * (at your option) any later version.
36257+ *
36258+ * This program is distributed in the hope that it will be useful,
36259+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36260+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36261+ * GNU General Public License for more details.
36262+ *
36263+ * You should have received a copy of the GNU General Public License
36264+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
36265+ */
36266+
36267+/*
36268+ * handling xattr functions
36269+ */
36270+
a2654f78
AM
36271+#include <linux/fs.h>
36272+#include <linux/posix_acl_xattr.h>
c1595e42
JR
36273+#include <linux/xattr.h>
36274+#include "aufs.h"
36275+
36276+static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36277+{
36278+ if (!ignore_flags)
36279+ goto out;
36280+ switch (err) {
36281+ case -ENOMEM:
36282+ case -EDQUOT:
36283+ goto out;
36284+ }
36285+
36286+ if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36287+ err = 0;
36288+ goto out;
36289+ }
36290+
36291+#define cmp(brattr, prefix) do { \
36292+ if (!strncmp(name, XATTR_##prefix##_PREFIX, \
36293+ XATTR_##prefix##_PREFIX_LEN)) { \
36294+ if (ignore_flags & AuBrAttr_ICEX_##brattr) \
36295+ err = 0; \
36296+ goto out; \
36297+ } \
36298+ } while (0)
36299+
36300+ cmp(SEC, SECURITY);
36301+ cmp(SYS, SYSTEM);
36302+ cmp(TR, TRUSTED);
36303+ cmp(USR, USER);
36304+#undef cmp
36305+
36306+ if (ignore_flags & AuBrAttr_ICEX_OTH)
36307+ err = 0;
36308+
36309+out:
36310+ return err;
36311+}
36312+
36313+static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36314+
36315+static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
7e9cd9fe
AM
36316+ char *name, char **buf, unsigned int ignore_flags,
36317+ unsigned int verbose)
c1595e42
JR
36318+{
36319+ int err;
36320+ ssize_t ssz;
36321+ struct inode *h_idst;
36322+
36323+ ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
36324+ err = ssz;
36325+ if (unlikely(err <= 0)) {
c1595e42
JR
36326+ if (err == -ENODATA
36327+ || (err == -EOPNOTSUPP
b912730e 36328+ && ((ignore_flags & au_xattr_out_of_list)
5527c038 36329+ || (au_test_nfs_noacl(d_inode(h_src))
b912730e
AM
36330+ && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36331+ || !strcmp(name,
36332+ XATTR_NAME_POSIX_ACL_DEFAULT))))
36333+ ))
c1595e42 36334+ err = 0;
b912730e
AM
36335+ if (err && (verbose || au_debug_test()))
36336+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
36337+ goto out;
36338+ }
36339+
36340+ /* unlock it temporary */
5527c038 36341+ h_idst = d_inode(h_dst);
febd17d6 36342+ inode_unlock(h_idst);
c1595e42 36343+ err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
febd17d6 36344+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42 36345+ if (unlikely(err)) {
7e9cd9fe
AM
36346+ if (verbose || au_debug_test())
36347+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
36348+ err = au_xattr_ignore(err, name, ignore_flags);
36349+ }
36350+
36351+out:
36352+ return err;
36353+}
36354+
7e9cd9fe
AM
36355+int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
36356+ unsigned int verbose)
c1595e42
JR
36357+{
36358+ int err, unlocked, acl_access, acl_default;
36359+ ssize_t ssz;
36360+ struct inode *h_isrc, *h_idst;
36361+ char *value, *p, *o, *e;
36362+
36363+ /* try stopping to update the source inode while we are referencing */
7e9cd9fe 36364+ /* there should not be the parent-child relationship between them */
5527c038
JR
36365+ h_isrc = d_inode(h_src);
36366+ h_idst = d_inode(h_dst);
febd17d6 36367+ inode_unlock(h_idst);
be118d29 36368+ inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
febd17d6 36369+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42
JR
36370+ unlocked = 0;
36371+
36372+ /* some filesystems don't list POSIX ACL, for example tmpfs */
36373+ ssz = vfs_listxattr(h_src, NULL, 0);
36374+ err = ssz;
36375+ if (unlikely(err < 0)) {
36376+ AuTraceErr(err);
36377+ if (err == -ENODATA
36378+ || err == -EOPNOTSUPP)
36379+ err = 0; /* ignore */
36380+ goto out;
36381+ }
36382+
36383+ err = 0;
36384+ p = NULL;
36385+ o = NULL;
36386+ if (ssz) {
36387+ err = -ENOMEM;
36388+ p = kmalloc(ssz, GFP_NOFS);
36389+ o = p;
36390+ if (unlikely(!p))
36391+ goto out;
36392+ err = vfs_listxattr(h_src, p, ssz);
36393+ }
3c1bdaff 36394+ inode_unlock_shared(h_isrc);
c1595e42
JR
36395+ unlocked = 1;
36396+ AuDbg("err %d, ssz %zd\n", err, ssz);
36397+ if (unlikely(err < 0))
36398+ goto out_free;
36399+
36400+ err = 0;
36401+ e = p + ssz;
36402+ value = NULL;
36403+ acl_access = 0;
36404+ acl_default = 0;
36405+ while (!err && p < e) {
36406+ acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36407+ sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36408+ acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36409+ sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36410+ - 1);
7e9cd9fe
AM
36411+ err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36412+ verbose);
c1595e42
JR
36413+ p += strlen(p) + 1;
36414+ }
36415+ AuTraceErr(err);
36416+ ignore_flags |= au_xattr_out_of_list;
36417+ if (!err && !acl_access) {
36418+ err = au_do_cpup_xattr(h_dst, h_src,
36419+ XATTR_NAME_POSIX_ACL_ACCESS, &value,
7e9cd9fe 36420+ ignore_flags, verbose);
c1595e42
JR
36421+ AuTraceErr(err);
36422+ }
36423+ if (!err && !acl_default) {
36424+ err = au_do_cpup_xattr(h_dst, h_src,
36425+ XATTR_NAME_POSIX_ACL_DEFAULT, &value,
7e9cd9fe 36426+ ignore_flags, verbose);
c1595e42
JR
36427+ AuTraceErr(err);
36428+ }
36429+
8b6a4947 36430+ kfree(value);
c1595e42
JR
36431+
36432+out_free:
8b6a4947 36433+ kfree(o);
c1595e42
JR
36434+out:
36435+ if (!unlocked)
3c1bdaff 36436+ inode_unlock_shared(h_isrc);
c1595e42
JR
36437+ AuTraceErr(err);
36438+ return err;
36439+}
36440+
36441+/* ---------------------------------------------------------------------- */
36442+
a2654f78
AM
36443+static int au_smack_reentering(struct super_block *sb)
36444+{
36445+#if IS_ENABLED(CONFIG_SECURITY_SMACK)
36446+ /*
36447+ * as a part of lookup, smack_d_instantiate() is called, and it calls
36448+ * i_op->getxattr(). ouch.
36449+ */
36450+ return si_pid_test(sb);
36451+#else
36452+ return 0;
36453+#endif
36454+}
36455+
c1595e42
JR
36456+enum {
36457+ AU_XATTR_LIST,
36458+ AU_XATTR_GET
36459+};
36460+
36461+struct au_lgxattr {
36462+ int type;
36463+ union {
36464+ struct {
36465+ char *list;
36466+ size_t size;
36467+ } list;
36468+ struct {
36469+ const char *name;
36470+ void *value;
36471+ size_t size;
36472+ } get;
36473+ } u;
36474+};
36475+
36476+static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
36477+{
36478+ ssize_t err;
a2654f78 36479+ int reenter;
c1595e42
JR
36480+ struct path h_path;
36481+ struct super_block *sb;
36482+
36483+ sb = dentry->d_sb;
a2654f78
AM
36484+ reenter = au_smack_reentering(sb);
36485+ if (!reenter) {
36486+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36487+ if (unlikely(err))
36488+ goto out;
36489+ }
36490+ err = au_h_path_getattr(dentry, /*force*/1, &h_path, reenter);
c1595e42
JR
36491+ if (unlikely(err))
36492+ goto out_si;
36493+ if (unlikely(!h_path.dentry))
36494+ /* illegally overlapped or something */
36495+ goto out_di; /* pretending success */
36496+
36497+ /* always topmost entry only */
36498+ switch (arg->type) {
36499+ case AU_XATTR_LIST:
36500+ err = vfs_listxattr(h_path.dentry,
36501+ arg->u.list.list, arg->u.list.size);
36502+ break;
36503+ case AU_XATTR_GET:
5afbbe0d 36504+ AuDebugOn(d_is_negative(h_path.dentry));
c1595e42
JR
36505+ err = vfs_getxattr(h_path.dentry,
36506+ arg->u.get.name, arg->u.get.value,
36507+ arg->u.get.size);
36508+ break;
36509+ }
36510+
36511+out_di:
a2654f78
AM
36512+ if (!reenter)
36513+ di_read_unlock(dentry, AuLock_IR);
c1595e42 36514+out_si:
a2654f78
AM
36515+ if (!reenter)
36516+ si_read_unlock(sb);
c1595e42
JR
36517+out:
36518+ AuTraceErr(err);
36519+ return err;
36520+}
36521+
36522+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36523+{
36524+ struct au_lgxattr arg = {
36525+ .type = AU_XATTR_LIST,
36526+ .u.list = {
36527+ .list = list,
36528+ .size = size
36529+ },
36530+ };
36531+
36532+ return au_lgxattr(dentry, &arg);
36533+}
36534+
f2c43d5f
AM
36535+static ssize_t au_getxattr(struct dentry *dentry,
36536+ struct inode *inode __maybe_unused,
36537+ const char *name, void *value, size_t size)
c1595e42
JR
36538+{
36539+ struct au_lgxattr arg = {
36540+ .type = AU_XATTR_GET,
36541+ .u.get = {
36542+ .name = name,
36543+ .value = value,
36544+ .size = size
36545+ },
36546+ };
36547+
36548+ return au_lgxattr(dentry, &arg);
36549+}
36550+
f2c43d5f
AM
36551+static int au_setxattr(struct dentry *dentry, struct inode *inode,
36552+ const char *name, const void *value, size_t size,
36553+ int flags)
c1595e42 36554+{
f2c43d5f 36555+ struct au_sxattr arg = {
c1595e42
JR
36556+ .type = AU_XATTR_SET,
36557+ .u.set = {
36558+ .name = name,
36559+ .value = value,
36560+ .size = size,
36561+ .flags = flags
36562+ },
36563+ };
36564+
f2c43d5f 36565+ return au_sxattr(dentry, inode, &arg);
c1595e42
JR
36566+}
36567+
36568+/* ---------------------------------------------------------------------- */
36569+
f2c43d5f
AM
36570+static int au_xattr_get(const struct xattr_handler *handler,
36571+ struct dentry *dentry, struct inode *inode,
36572+ const char *name, void *buffer, size_t size)
c1595e42 36573+{
f2c43d5f 36574+ return au_getxattr(dentry, inode, name, buffer, size);
c1595e42
JR
36575+}
36576+
f2c43d5f
AM
36577+static int au_xattr_set(const struct xattr_handler *handler,
36578+ struct dentry *dentry, struct inode *inode,
36579+ const char *name, const void *value, size_t size,
36580+ int flags)
c1595e42 36581+{
f2c43d5f 36582+ return au_setxattr(dentry, inode, name, value, size, flags);
c1595e42
JR
36583+}
36584+
36585+static const struct xattr_handler au_xattr_handler = {
f2c43d5f
AM
36586+ .name = "",
36587+ .prefix = "",
c1595e42
JR
36588+ .get = au_xattr_get,
36589+ .set = au_xattr_set
c1595e42
JR
36590+};
36591+
36592+static const struct xattr_handler *au_xattr_handlers[] = {
a2654f78
AM
36593+#ifdef CONFIG_FS_POSIX_ACL
36594+ &posix_acl_access_xattr_handler,
36595+ &posix_acl_default_xattr_handler,
36596+#endif
36597+ &au_xattr_handler, /* must be last */
f2c43d5f 36598+ NULL
c1595e42
JR
36599+};
36600+
36601+void au_xattr_init(struct super_block *sb)
36602+{
f2c43d5f 36603+ sb->s_xattr = au_xattr_handlers;
c1595e42 36604+}
7f207e10
AM
36605diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
36606--- /usr/share/empty/fs/aufs/xino.c 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 36607+++ linux/fs/aufs/xino.c 2018-08-12 23:43:05.466791610 +0200
062440b3 36608@@ -0,0 +1,1593 @@
cd7a4cd9 36609+// SPDX-License-Identifier: GPL-2.0
1facf9fc 36610+/*
b00004a5 36611+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 36612+ *
36613+ * This program, aufs is free software; you can redistribute it and/or modify
36614+ * it under the terms of the GNU General Public License as published by
36615+ * the Free Software Foundation; either version 2 of the License, or
36616+ * (at your option) any later version.
dece6358
AM
36617+ *
36618+ * This program is distributed in the hope that it will be useful,
36619+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36620+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36621+ * GNU General Public License for more details.
36622+ *
36623+ * You should have received a copy of the GNU General Public License
523b37e3 36624+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36625+ */
36626+
36627+/*
36628+ * external inode number translation table and bitmap
062440b3
AM
36629+ *
36630+ * things to consider
36631+ * - the lifetime
36632+ * + au_xino object
36633+ * + XINO files (xino, xib, xigen)
36634+ * + dynamic debugfs entries (xiN)
36635+ * + static debugfs entries (xib, xigen)
36636+ * + static sysfs entry (xi_path)
36637+ * - several entry points to handle them.
36638+ * + mount(2) without xino option (default)
36639+ * + mount(2) with xino option
36640+ * + mount(2) with noxino option
36641+ * + umount(2)
36642+ * + remount with add/del branches
36643+ * + remount with xino/noxino options
1facf9fc 36644+ */
36645+
36646+#include <linux/seq_file.h>
392086de 36647+#include <linux/statfs.h>
1facf9fc 36648+#include "aufs.h"
36649+
062440b3
AM
36650+static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
36651+ aufs_bindex_t bbot,
36652+ struct super_block *h_sb)
1facf9fc 36653+{
062440b3
AM
36654+ /* todo: try binary-search if the branches are many */
36655+ for (; btop <= bbot; btop++)
36656+ if (h_sb == au_sbr_sb(sb, btop))
36657+ return btop;
36658+ return -1;
be52b249
AM
36659+}
36660+
062440b3
AM
36661+/*
36662+ * find another branch who is on the same filesystem of the specified
36663+ * branch{@btgt}. search until @bbot.
36664+ */
36665+static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
36666+ aufs_bindex_t bbot)
1facf9fc 36667+{
062440b3
AM
36668+ aufs_bindex_t bindex;
36669+ struct super_block *tgt_sb;
1facf9fc 36670+
062440b3
AM
36671+ tgt_sb = au_sbr_sb(sb, btgt);
36672+ bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
36673+ if (bindex < 0)
36674+ bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
1facf9fc 36675+
062440b3 36676+ return bindex;
1facf9fc 36677+}
36678+
36679+/* ---------------------------------------------------------------------- */
36680+
36681+/*
062440b3 36682+ * stop unnecessary notify events at creating xino files
1facf9fc 36683+ */
062440b3
AM
36684+struct au_xino_lock_dir {
36685+ struct au_hinode *hdir;
36686+ struct dentry *parent;
36687+ struct inode *dir;
36688+};
36689+
36690+static struct dentry *au_dget_parent_lock(struct dentry *dentry,
36691+ unsigned int lsc)
1facf9fc 36692+{
062440b3
AM
36693+ struct dentry *parent;
36694+ struct inode *dir;
1facf9fc 36695+
062440b3 36696+ parent = dget_parent(dentry);
5527c038 36697+ dir = d_inode(parent);
062440b3
AM
36698+ inode_lock_nested(dir, lsc);
36699+#if 0 /* it should not happen */
36700+ spin_lock(&dentry->d_lock);
36701+ if (unlikely(dentry->d_parent != parent)) {
36702+ spin_unlock(&dentry->d_lock);
36703+ inode_unlock(dir);
36704+ dput(parent);
36705+ parent = NULL;
1facf9fc 36706+ goto out;
36707+ }
062440b3 36708+ spin_unlock(&dentry->d_lock);
1facf9fc 36709+
4f0767ce 36710+out:
062440b3
AM
36711+#endif
36712+ return parent;
1facf9fc 36713+}
36714+
062440b3 36715+static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
1facf9fc 36716+ struct au_xino_lock_dir *ldir)
36717+{
36718+ aufs_bindex_t brid, bindex;
36719+
36720+ ldir->hdir = NULL;
36721+ bindex = -1;
36722+ brid = au_xino_brid(sb);
36723+ if (brid >= 0)
36724+ bindex = au_br_index(sb, brid);
36725+ if (bindex >= 0) {
062440b3 36726+ /* rw branch root */
5527c038 36727+ ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 36728+ au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
1facf9fc 36729+ } else {
062440b3
AM
36730+ /* other */
36731+ ldir->parent = au_dget_parent_lock(xipath->dentry,
36732+ AuLsc_I_PARENT);
febd17d6 36733+ ldir->dir = d_inode(ldir->parent);
1facf9fc 36734+ }
36735+}
36736+
36737+static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
36738+{
36739+ if (ldir->hdir)
5afbbe0d 36740+ au_hn_inode_unlock(ldir->hdir);
1facf9fc 36741+ else {
febd17d6 36742+ inode_unlock(ldir->dir);
1facf9fc 36743+ dput(ldir->parent);
36744+ }
36745+}
36746+
36747+/* ---------------------------------------------------------------------- */
36748+
062440b3
AM
36749+/*
36750+ * create and set a new xino file
36751+ */
36752+struct file *au_xino_create(struct super_block *sb, char *fpath, int silent)
36753+{
36754+ struct file *file;
36755+ struct dentry *h_parent, *d;
36756+ struct inode *h_dir, *inode;
36757+ int err;
36758+
36759+ /*
36760+ * at mount-time, and the xino file is the default path,
36761+ * hnotify is disabled so we have no notify events to ignore.
36762+ * when a user specified the xino, we cannot get au_hdir to be ignored.
36763+ */
36764+ file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
36765+ /* | __FMODE_NONOTIFY */,
cd7a4cd9 36766+ 0666);
062440b3
AM
36767+ if (IS_ERR(file)) {
36768+ if (!silent)
36769+ pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
36770+ return file;
36771+ }
36772+
36773+ /* keep file count */
36774+ err = 0;
36775+ d = file->f_path.dentry;
36776+ h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
36777+ /* mnt_want_write() is unnecessary here */
36778+ h_dir = d_inode(h_parent);
36779+ inode = file_inode(file);
36780+ /* no delegation since it is just created */
36781+ if (inode->i_nlink)
36782+ err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
36783+ /*force*/0);
36784+ inode_unlock(h_dir);
36785+ dput(h_parent);
36786+ if (unlikely(err)) {
36787+ if (!silent)
36788+ pr_err("unlink %s(%d)\n", fpath, err);
36789+ goto out;
36790+ }
36791+
36792+ err = -EINVAL;
36793+ if (unlikely(sb == d->d_sb)) {
36794+ if (!silent)
36795+ pr_err("%s must be outside\n", fpath);
36796+ goto out;
36797+ }
36798+ if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
36799+ if (!silent)
36800+ pr_err("xino doesn't support %s(%s)\n",
36801+ fpath, au_sbtype(d->d_sb));
36802+ goto out;
36803+ }
36804+ return file; /* success */
36805+
36806+out:
36807+ fput(file);
36808+ file = ERR_PTR(err);
36809+ return file;
36810+}
36811+
36812+/*
36813+ * create a new xinofile at the same place/path as @base.
36814+ */
36815+struct file *au_xino_create2(struct super_block *sb, struct path *base,
36816+ struct file *copy_src)
36817+{
36818+ struct file *file;
36819+ struct dentry *dentry, *parent;
36820+ struct inode *dir, *delegated;
36821+ struct qstr *name;
36822+ struct path path;
36823+ int err, do_unlock;
36824+ struct au_xino_lock_dir ldir;
36825+
36826+ do_unlock = 1;
36827+ au_xino_lock_dir(sb, base, &ldir);
36828+ dentry = base->dentry;
36829+ parent = dentry->d_parent; /* dir inode is locked */
36830+ dir = d_inode(parent);
36831+ IMustLock(dir);
36832+
36833+ name = &dentry->d_name;
36834+ path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
36835+ if (IS_ERR(path.dentry)) {
36836+ file = (void *)path.dentry;
36837+ pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
36838+ goto out;
36839+ }
36840+
36841+ /* no need to mnt_want_write() since we call dentry_open() later */
cd7a4cd9 36842+ err = vfs_create(dir, path.dentry, 0666, NULL);
062440b3
AM
36843+ if (unlikely(err)) {
36844+ file = ERR_PTR(err);
36845+ pr_err("%pd create err %d\n", dentry, err);
36846+ goto out_dput;
36847+ }
36848+
36849+ path.mnt = base->mnt;
36850+ file = vfsub_dentry_open(&path,
36851+ O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
36852+ /* | __FMODE_NONOTIFY */);
36853+ if (IS_ERR(file)) {
36854+ pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
36855+ goto out_dput;
36856+ }
36857+
36858+ delegated = NULL;
36859+ err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
36860+ au_xino_unlock_dir(&ldir);
36861+ do_unlock = 0;
36862+ if (unlikely(err == -EWOULDBLOCK)) {
36863+ pr_warn("cannot retry for NFSv4 delegation"
36864+ " for an internal unlink\n");
36865+ iput(delegated);
36866+ }
36867+ if (unlikely(err)) {
36868+ pr_err("%pd unlink err %d\n", dentry, err);
36869+ goto out_fput;
36870+ }
36871+
36872+ if (copy_src) {
36873+ /* no one can touch copy_src xino */
36874+ err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
36875+ if (unlikely(err)) {
36876+ pr_err("%pd copy err %d\n", dentry, err);
36877+ goto out_fput;
36878+ }
36879+ }
36880+ goto out_dput; /* success */
1facf9fc 36881+
062440b3
AM
36882+out_fput:
36883+ fput(file);
36884+ file = ERR_PTR(err);
36885+out_dput:
36886+ dput(path.dentry);
36887+out:
36888+ if (do_unlock)
36889+ au_xino_unlock_dir(&ldir);
36890+ return file;
36891+}
36892+
36893+/* ---------------------------------------------------------------------- */
36894+
36895+/*
36896+ * truncate xino files
36897+ */
1facf9fc 36898+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
36899+{
36900+ int err;
392086de
AM
36901+ unsigned long jiffy;
36902+ blkcnt_t blocks;
062440b3 36903+ aufs_bindex_t bbot;
392086de 36904+ struct kstatfs *st;
1facf9fc 36905+ struct au_branch *br;
36906+ struct file *new_xino, *file;
062440b3 36907+ struct path *path;
1facf9fc 36908+
392086de 36909+ err = -ENOMEM;
be52b249 36910+ st = kmalloc(sizeof(*st), GFP_NOFS);
392086de
AM
36911+ if (unlikely(!st))
36912+ goto out;
36913+
1facf9fc 36914+ err = -EINVAL;
5afbbe0d
AM
36915+ bbot = au_sbbot(sb);
36916+ if (unlikely(bindex < 0 || bbot < bindex))
392086de 36917+ goto out_st;
1facf9fc 36918+ br = au_sbr(sb, bindex);
062440b3 36919+ file = au_xino_file(br);
1facf9fc 36920+ if (!file)
392086de
AM
36921+ goto out_st;
36922+
062440b3
AM
36923+ path = &file->f_path;
36924+ err = vfs_statfs(path, st);
392086de
AM
36925+ if (unlikely(err))
36926+ AuErr1("statfs err %d, ignored\n", err);
36927+ jiffy = jiffies;
36928+ blocks = file_inode(file)->i_blocks;
36929+ pr_info("begin truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
36930+ bindex, (u64)blocks, st->f_bfree, st->f_blocks);
1facf9fc 36931+
062440b3 36932+ new_xino = au_xino_create2(sb, path, file);
1facf9fc 36933+ err = PTR_ERR(new_xino);
392086de
AM
36934+ if (IS_ERR(new_xino)) {
36935+ pr_err("err %d, ignored\n", err);
36936+ goto out_st;
36937+ }
1facf9fc 36938+ err = 0;
062440b3 36939+ au_xino_file_set(br, new_xino);
1facf9fc 36940+
392086de
AM
36941+ err = vfs_statfs(&new_xino->f_path, st);
36942+ if (!err) {
36943+ pr_info("end truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
36944+ bindex, (u64)file_inode(new_xino)->i_blocks,
36945+ st->f_bfree, st->f_blocks);
36946+ if (file_inode(new_xino)->i_blocks < blocks)
36947+ au_sbi(sb)->si_xino_jiffy = jiffy;
36948+ } else
36949+ AuErr1("statfs err %d, ignored\n", err);
062440b3 36950+ fput(new_xino);
392086de
AM
36951+
36952+out_st:
1c60b727 36953+ kfree(st);
4f0767ce 36954+out:
1facf9fc 36955+ return err;
36956+}
36957+
36958+struct xino_do_trunc_args {
36959+ struct super_block *sb;
36960+ struct au_branch *br;
36961+};
36962+
36963+static void xino_do_trunc(void *_args)
36964+{
36965+ struct xino_do_trunc_args *args = _args;
36966+ struct super_block *sb;
36967+ struct au_branch *br;
36968+ struct inode *dir;
36969+ int err;
36970+ aufs_bindex_t bindex;
36971+
36972+ err = 0;
36973+ sb = args->sb;
5527c038 36974+ dir = d_inode(sb->s_root);
1facf9fc 36975+ br = args->br;
36976+
36977+ si_noflush_write_lock(sb);
36978+ ii_read_lock_parent(dir);
36979+ bindex = au_br_index(sb, br->br_id);
36980+ err = au_xino_trunc(sb, bindex);
1facf9fc 36981+ ii_read_unlock(dir);
36982+ if (unlikely(err))
392086de 36983+ pr_warn("err b%d, (%d)\n", bindex, err);
062440b3 36984+ atomic_dec(&br->br_xino->xi_truncating);
5afbbe0d 36985+ au_br_put(br);
1facf9fc 36986+ si_write_unlock(sb);
027c5e7a 36987+ au_nwt_done(&au_sbi(sb)->si_nowait);
1c60b727 36988+ kfree(args);
1facf9fc 36989+}
36990+
392086de
AM
36991+static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
36992+{
36993+ int err;
36994+ struct kstatfs st;
36995+ struct au_sbinfo *sbinfo;
062440b3 36996+ struct file *file;
392086de
AM
36997+
36998+ /* todo: si_xino_expire and the ratio should be customizable */
36999+ sbinfo = au_sbi(sb);
37000+ if (time_before(jiffies,
37001+ sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37002+ return 0;
37003+
37004+ /* truncation border */
062440b3
AM
37005+ file = au_xino_file(br);
37006+ AuDebugOn(!file);
37007+ err = vfs_statfs(&file->f_path, &st);
392086de
AM
37008+ if (unlikely(err)) {
37009+ AuErr1("statfs err %d, ignored\n", err);
37010+ return 0;
37011+ }
37012+ if (div64_u64(st.f_bfree * 100, st.f_blocks) >= AUFS_XINO_DEF_TRUNC)
37013+ return 0;
37014+
37015+ return 1;
37016+}
37017+
1facf9fc 37018+static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37019+{
37020+ struct xino_do_trunc_args *args;
37021+ int wkq_err;
37022+
392086de 37023+ if (!xino_trunc_test(sb, br))
1facf9fc 37024+ return;
37025+
062440b3 37026+ if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
1facf9fc 37027+ goto out;
37028+
37029+ /* lock and kfree() will be called in trunc_xino() */
37030+ args = kmalloc(sizeof(*args), GFP_NOFS);
37031+ if (unlikely(!args)) {
37032+ AuErr1("no memory\n");
f0c0a007 37033+ goto out;
1facf9fc 37034+ }
37035+
5afbbe0d 37036+ au_br_get(br);
1facf9fc 37037+ args->sb = sb;
37038+ args->br = br;
53392da6 37039+ wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
1facf9fc 37040+ if (!wkq_err)
37041+ return; /* success */
37042+
4a4d8108 37043+ pr_err("wkq %d\n", wkq_err);
5afbbe0d 37044+ au_br_put(br);
1c60b727 37045+ kfree(args);
1facf9fc 37046+
4f0767ce 37047+out:
062440b3 37048+ atomic_dec(&br->br_xino->xi_truncating);
1facf9fc 37049+}
37050+
37051+/* ---------------------------------------------------------------------- */
37052+
062440b3
AM
37053+/*
37054+ * read @ino from xinofile for the specified branch{@sb, @bindex}
37055+ * at the position of @h_ino.
37056+ */
37057+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37058+ ino_t *ino)
37059+{
37060+ int err;
37061+ ssize_t sz;
37062+ loff_t pos;
37063+ struct au_branch *br;
37064+ struct file *file;
37065+ struct au_sbinfo *sbinfo;
37066+
37067+ *ino = 0;
37068+ if (!au_opt_test(au_mntflags(sb), XINO))
37069+ return 0; /* no xino */
37070+
37071+ err = 0;
37072+ sbinfo = au_sbi(sb);
37073+ pos = h_ino;
37074+ if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
37075+ AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
37076+ return -EFBIG;
37077+ }
37078+ pos *= sizeof(*ino);
37079+
37080+ br = au_sbr(sb, bindex);
37081+ file = au_xino_file(br);
37082+ if (vfsub_f_size_read(file) < pos + sizeof(*ino))
37083+ return 0; /* no ino */
37084+
37085+ sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
37086+ if (sz == sizeof(*ino))
37087+ return 0; /* success */
37088+
37089+ err = sz;
37090+ if (unlikely(sz >= 0)) {
37091+ err = -EIO;
37092+ AuIOErr("xino read error (%zd)\n", sz);
37093+ }
37094+
37095+ return err;
37096+}
37097+
5527c038 37098+static int au_xino_do_write(vfs_writef_t write, struct file *file,
1facf9fc 37099+ ino_t h_ino, ino_t ino)
37100+{
37101+ loff_t pos;
37102+ ssize_t sz;
37103+
37104+ pos = h_ino;
37105+ if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
37106+ AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
37107+ return -EFBIG;
37108+ }
37109+ pos *= sizeof(ino);
37110+ sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
37111+ if (sz == sizeof(ino))
37112+ return 0; /* success */
37113+
37114+ AuIOErr("write failed (%zd)\n", sz);
37115+ return -EIO;
37116+}
37117+
37118+/*
37119+ * write @ino to the xinofile for the specified branch{@sb, @bindex}
37120+ * at the position of @h_ino.
37121+ * even if @ino is zero, it is written to the xinofile and means no entry.
37122+ * if the size of the xino file on a specific filesystem exceeds the watermark,
37123+ * try truncating it.
37124+ */
37125+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37126+ ino_t ino)
37127+{
37128+ int err;
37129+ unsigned int mnt_flags;
37130+ struct au_branch *br;
37131+
37132+ BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
37133+ || ((loff_t)-1) > 0);
dece6358 37134+ SiMustAnyLock(sb);
1facf9fc 37135+
37136+ mnt_flags = au_mntflags(sb);
37137+ if (!au_opt_test(mnt_flags, XINO))
37138+ return 0;
37139+
37140+ br = au_sbr(sb, bindex);
062440b3
AM
37141+ err = au_xino_do_write(au_sbi(sb)->si_xwrite, au_xino_file(br), h_ino,
37142+ ino);
1facf9fc 37143+ if (!err) {
37144+ if (au_opt_test(mnt_flags, TRUNC_XINO)
86dc4139 37145+ && au_test_fs_trunc_xino(au_br_sb(br)))
1facf9fc 37146+ xino_try_trunc(sb, br);
37147+ return 0; /* success */
37148+ }
37149+
37150+ AuIOErr("write failed (%d)\n", err);
37151+ return -EIO;
37152+}
37153+
062440b3
AM
37154+static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37155+ size_t size, loff_t *pos);
1facf9fc 37156+
062440b3
AM
37157+/* todo: unnecessary to support mmap_sem since kernel-space? */
37158+ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
37159+ loff_t *pos)
37160+{
37161+ ssize_t err;
37162+ mm_segment_t oldfs;
37163+ union {
37164+ void *k;
37165+ char __user *u;
37166+ } buf;
37167+ int i;
37168+ const int prevent_endless = 10;
1facf9fc 37169+
062440b3
AM
37170+ i = 0;
37171+ buf.k = kbuf;
37172+ oldfs = get_fs();
37173+ set_fs(KERNEL_DS);
37174+ do {
37175+ err = func(file, buf.u, size, pos);
37176+ if (err == -EINTR
37177+ && !au_wkq_test()
37178+ && fatal_signal_pending(current)) {
37179+ set_fs(oldfs);
37180+ err = xino_fread_wkq(func, file, kbuf, size, pos);
37181+ BUG_ON(err == -EINTR);
37182+ oldfs = get_fs();
37183+ set_fs(KERNEL_DS);
37184+ }
37185+ } while (i++ < prevent_endless
37186+ && (err == -EAGAIN || err == -EINTR));
37187+ set_fs(oldfs);
37188+
37189+#if 0 /* reserved for future use */
37190+ if (err > 0)
37191+ fsnotify_access(file->f_path.dentry);
37192+#endif
37193+
37194+ return err;
37195+}
37196+
37197+struct xino_fread_args {
37198+ ssize_t *errp;
37199+ vfs_readf_t func;
37200+ struct file *file;
37201+ void *buf;
37202+ size_t size;
37203+ loff_t *pos;
37204+};
37205+
37206+static void call_xino_fread(void *args)
37207+{
37208+ struct xino_fread_args *a = args;
37209+ *a->errp = xino_fread(a->func, a->file, a->buf, a->size, a->pos);
37210+}
37211+
37212+static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37213+ size_t size, loff_t *pos)
37214+{
37215+ ssize_t err;
37216+ int wkq_err;
37217+ struct xino_fread_args args = {
37218+ .errp = &err,
37219+ .func = func,
37220+ .file = file,
37221+ .buf = buf,
37222+ .size = size,
37223+ .pos = pos
37224+ };
37225+
37226+ wkq_err = au_wkq_wait(call_xino_fread, &args);
37227+ if (unlikely(wkq_err))
37228+ err = wkq_err;
37229+
37230+ return err;
37231+}
37232+
37233+static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37234+ size_t size, loff_t *pos);
37235+
37236+static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
37237+ size_t size, loff_t *pos)
37238+{
37239+ ssize_t err;
37240+ mm_segment_t oldfs;
37241+ union {
37242+ void *k;
37243+ const char __user *u;
37244+ } buf;
37245+ int i;
37246+ const int prevent_endless = 10;
37247+
37248+ i = 0;
37249+ buf.k = kbuf;
37250+ oldfs = get_fs();
37251+ set_fs(KERNEL_DS);
37252+ do {
37253+ err = func(file, buf.u, size, pos);
37254+ if (err == -EINTR
37255+ && !au_wkq_test()
37256+ && fatal_signal_pending(current)) {
37257+ set_fs(oldfs);
37258+ err = xino_fwrite_wkq(func, file, kbuf, size, pos);
37259+ BUG_ON(err == -EINTR);
37260+ oldfs = get_fs();
37261+ set_fs(KERNEL_DS);
37262+ }
37263+ } while (i++ < prevent_endless
37264+ && (err == -EAGAIN || err == -EINTR));
37265+ set_fs(oldfs);
37266+
37267+#if 0 /* reserved for future use */
37268+ if (err > 0)
37269+ fsnotify_modify(file->f_path.dentry);
37270+#endif
37271+
37272+ return err;
37273+}
37274+
37275+struct do_xino_fwrite_args {
37276+ ssize_t *errp;
37277+ vfs_writef_t func;
37278+ struct file *file;
37279+ void *buf;
37280+ size_t size;
37281+ loff_t *pos;
37282+};
37283+
37284+static void call_do_xino_fwrite(void *args)
37285+{
37286+ struct do_xino_fwrite_args *a = args;
37287+ *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
37288+}
37289+
37290+static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37291+ size_t size, loff_t *pos)
37292+{
37293+ ssize_t err;
37294+ int wkq_err;
37295+ struct do_xino_fwrite_args args = {
37296+ .errp = &err,
37297+ .func = func,
37298+ .file = file,
37299+ .buf = buf,
37300+ .size = size,
37301+ .pos = pos
37302+ };
37303+
37304+ /*
37305+ * it breaks RLIMIT_FSIZE and normal user's limit,
37306+ * users should care about quota and real 'filesystem full.'
37307+ */
37308+ wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
37309+ if (unlikely(wkq_err))
37310+ err = wkq_err;
37311+
37312+ return err;
37313+}
37314+
37315+ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
37316+ size_t size, loff_t *pos)
37317+{
37318+ ssize_t err;
37319+
37320+ if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
37321+ lockdep_off();
37322+ err = do_xino_fwrite(func, file, buf, size, pos);
37323+ lockdep_on();
37324+ } else {
37325+ lockdep_off();
37326+ err = xino_fwrite_wkq(func, file, buf, size, pos);
37327+ lockdep_on();
37328+ }
37329+
37330+ return err;
37331+}
37332+
37333+/* ---------------------------------------------------------------------- */
37334+
37335+/*
37336+ * inode number bitmap
37337+ */
1facf9fc 37338+static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
37339+static ino_t xib_calc_ino(unsigned long pindex, int bit)
37340+{
37341+ ino_t ino;
37342+
37343+ AuDebugOn(bit < 0 || page_bits <= bit);
37344+ ino = AUFS_FIRST_INO + pindex * page_bits + bit;
37345+ return ino;
37346+}
37347+
37348+static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
37349+{
37350+ AuDebugOn(ino < AUFS_FIRST_INO);
37351+ ino -= AUFS_FIRST_INO;
37352+ *pindex = ino / page_bits;
37353+ *bit = ino % page_bits;
37354+}
37355+
37356+static int xib_pindex(struct super_block *sb, unsigned long pindex)
37357+{
37358+ int err;
37359+ loff_t pos;
37360+ ssize_t sz;
37361+ struct au_sbinfo *sbinfo;
37362+ struct file *xib;
37363+ unsigned long *p;
37364+
37365+ sbinfo = au_sbi(sb);
37366+ MtxMustLock(&sbinfo->si_xib_mtx);
37367+ AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
37368+ || !au_opt_test(sbinfo->si_mntflags, XINO));
37369+
37370+ if (pindex == sbinfo->si_xib_last_pindex)
37371+ return 0;
37372+
37373+ xib = sbinfo->si_xib;
37374+ p = sbinfo->si_xib_buf;
37375+ pos = sbinfo->si_xib_last_pindex;
37376+ pos *= PAGE_SIZE;
37377+ sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
37378+ if (unlikely(sz != PAGE_SIZE))
37379+ goto out;
37380+
37381+ pos = pindex;
37382+ pos *= PAGE_SIZE;
c06a8ce3 37383+ if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
1facf9fc 37384+ sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
37385+ else {
37386+ memset(p, 0, PAGE_SIZE);
37387+ sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
37388+ }
37389+ if (sz == PAGE_SIZE) {
37390+ sbinfo->si_xib_last_pindex = pindex;
37391+ return 0; /* success */
37392+ }
37393+
4f0767ce 37394+out:
b752ccd1
AM
37395+ AuIOErr1("write failed (%zd)\n", sz);
37396+ err = sz;
37397+ if (sz >= 0)
37398+ err = -EIO;
37399+ return err;
37400+}
37401+
b752ccd1
AM
37402+static void au_xib_clear_bit(struct inode *inode)
37403+{
37404+ int err, bit;
37405+ unsigned long pindex;
37406+ struct super_block *sb;
37407+ struct au_sbinfo *sbinfo;
37408+
37409+ AuDebugOn(inode->i_nlink);
37410+
37411+ sb = inode->i_sb;
37412+ xib_calc_bit(inode->i_ino, &pindex, &bit);
37413+ AuDebugOn(page_bits <= bit);
37414+ sbinfo = au_sbi(sb);
37415+ mutex_lock(&sbinfo->si_xib_mtx);
37416+ err = xib_pindex(sb, pindex);
37417+ if (!err) {
37418+ clear_bit(bit, sbinfo->si_xib_buf);
37419+ sbinfo->si_xib_next_bit = bit;
37420+ }
37421+ mutex_unlock(&sbinfo->si_xib_mtx);
37422+}
37423+
1facf9fc 37424+/* ---------------------------------------------------------------------- */
37425+
1facf9fc 37426+/*
062440b3 37427+ * truncate a xino bitmap file
1facf9fc 37428+ */
1facf9fc 37429+
37430+/* todo: slow */
37431+static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
37432+{
37433+ int err, bit;
37434+ ssize_t sz;
37435+ unsigned long pindex;
37436+ loff_t pos, pend;
37437+ struct au_sbinfo *sbinfo;
5527c038 37438+ vfs_readf_t func;
1facf9fc 37439+ ino_t *ino;
37440+ unsigned long *p;
37441+
37442+ err = 0;
37443+ sbinfo = au_sbi(sb);
dece6358 37444+ MtxMustLock(&sbinfo->si_xib_mtx);
1facf9fc 37445+ p = sbinfo->si_xib_buf;
37446+ func = sbinfo->si_xread;
c06a8ce3 37447+ pend = vfsub_f_size_read(file);
1facf9fc 37448+ pos = 0;
37449+ while (pos < pend) {
37450+ sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
37451+ err = sz;
37452+ if (unlikely(sz <= 0))
37453+ goto out;
37454+
37455+ err = 0;
37456+ for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
37457+ if (unlikely(*ino < AUFS_FIRST_INO))
37458+ continue;
37459+
37460+ xib_calc_bit(*ino, &pindex, &bit);
37461+ AuDebugOn(page_bits <= bit);
37462+ err = xib_pindex(sb, pindex);
37463+ if (!err)
37464+ set_bit(bit, p);
37465+ else
37466+ goto out;
37467+ }
37468+ }
37469+
4f0767ce 37470+out:
1facf9fc 37471+ return err;
37472+}
37473+
37474+static int xib_restore(struct super_block *sb)
37475+{
37476+ int err;
5afbbe0d 37477+ aufs_bindex_t bindex, bbot;
1facf9fc 37478+ void *page;
062440b3 37479+ struct au_branch *br;
1facf9fc 37480+
37481+ err = -ENOMEM;
37482+ page = (void *)__get_free_page(GFP_NOFS);
37483+ if (unlikely(!page))
37484+ goto out;
37485+
37486+ err = 0;
5afbbe0d
AM
37487+ bbot = au_sbbot(sb);
37488+ for (bindex = 0; !err && bindex <= bbot; bindex++)
062440b3
AM
37489+ if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
37490+ br = au_sbr(sb, bindex);
37491+ err = do_xib_restore(sb, au_xino_file(br), page);
37492+ } else
37493+ AuDbg("skip shared b%d\n", bindex);
1c60b727 37494+ free_page((unsigned long)page);
1facf9fc 37495+
4f0767ce 37496+out:
1facf9fc 37497+ return err;
37498+}
37499+
37500+int au_xib_trunc(struct super_block *sb)
37501+{
37502+ int err;
37503+ ssize_t sz;
37504+ loff_t pos;
1facf9fc 37505+ struct au_sbinfo *sbinfo;
37506+ unsigned long *p;
37507+ struct file *file;
37508+
dece6358
AM
37509+ SiMustWriteLock(sb);
37510+
1facf9fc 37511+ err = 0;
37512+ sbinfo = au_sbi(sb);
37513+ if (!au_opt_test(sbinfo->si_mntflags, XINO))
37514+ goto out;
37515+
37516+ file = sbinfo->si_xib;
c06a8ce3 37517+ if (vfsub_f_size_read(file) <= PAGE_SIZE)
1facf9fc 37518+ goto out;
37519+
062440b3 37520+ file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
1facf9fc 37521+ err = PTR_ERR(file);
37522+ if (IS_ERR(file))
37523+ goto out;
37524+ fput(sbinfo->si_xib);
37525+ sbinfo->si_xib = file;
37526+
37527+ p = sbinfo->si_xib_buf;
37528+ memset(p, 0, PAGE_SIZE);
37529+ pos = 0;
37530+ sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
37531+ if (unlikely(sz != PAGE_SIZE)) {
37532+ err = sz;
37533+ AuIOErr("err %d\n", err);
37534+ if (sz >= 0)
37535+ err = -EIO;
37536+ goto out;
37537+ }
37538+
37539+ mutex_lock(&sbinfo->si_xib_mtx);
37540+ /* mnt_want_write() is unnecessary here */
37541+ err = xib_restore(sb);
37542+ mutex_unlock(&sbinfo->si_xib_mtx);
37543+
37544+out:
37545+ return err;
37546+}
37547+
37548+/* ---------------------------------------------------------------------- */
37549+
062440b3
AM
37550+struct au_xino *au_xino_alloc(void)
37551+{
37552+ struct au_xino *xi;
37553+
37554+ xi = kzalloc(sizeof(*xi), GFP_NOFS);
37555+ if (unlikely(!xi))
37556+ goto out;
37557+
37558+ xi->xi_nondir.total = 8; /* initial size */
37559+ xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
37560+ GFP_NOFS);
37561+ if (unlikely(!xi->xi_nondir.array))
37562+ goto out_free;
37563+
37564+ spin_lock_init(&xi->xi_nondir.spin);
37565+ init_waitqueue_head(&xi->xi_nondir.wqh);
37566+ atomic_set(&xi->xi_truncating, 0);
37567+ kref_init(&xi->xi_kref);
37568+ goto out; /* success */
37569+
37570+out_free:
37571+ kfree(xi);
37572+ xi = NULL;
37573+out:
37574+ return xi;
37575+}
37576+
37577+static int au_xino_init(struct au_branch *br, struct file *file)
37578+{
37579+ int err;
37580+ struct au_xino *xi;
37581+
37582+ err = 0;
37583+ xi = au_xino_alloc();
37584+ if (unlikely(!xi)) {
37585+ err = -ENOMEM;
37586+ goto out;
37587+ }
37588+
37589+ get_file(file);
37590+ xi->xi_file = file;
37591+ AuDebugOn(br->br_xino);
37592+ br->br_xino = xi;
37593+
37594+out:
37595+ return err;
37596+}
37597+
37598+static void au_xino_release(struct kref *kref)
37599+{
37600+ struct au_xino *xi;
37601+ int i;
37602+
37603+ xi = container_of(kref, struct au_xino, xi_kref);
37604+ if (xi->xi_file)
37605+ fput(xi->xi_file);
37606+ for (i = xi->xi_nondir.total - 1; i >= 0; i--)
37607+ AuDebugOn(xi->xi_nondir.array[i]);
37608+ kfree(xi->xi_nondir.array);
37609+ kfree(xi);
37610+}
37611+
37612+int au_xino_put(struct au_branch *br)
37613+{
37614+ int ret;
37615+ struct au_xino *xi;
37616+
37617+ ret = 0;
37618+ xi = br->br_xino;
37619+ if (xi) {
37620+ br->br_xino = NULL;
37621+ ret = kref_put(&xi->xi_kref, au_xino_release);
37622+ }
37623+
37624+ return ret;
37625+}
37626+
37627+void au_xino_file_set(struct au_branch *br, struct file *file)
37628+{
37629+ struct au_xino *xi;
37630+ struct file *f;
37631+
37632+ if (file)
37633+ get_file(file);
37634+ xi = br->br_xino;
37635+ AuDebugOn(!xi);
37636+ f = xi->xi_file;
37637+ if (f)
37638+ fput(f);
37639+ xi->xi_file = file;
37640+}
37641+
37642+/* ---------------------------------------------------------------------- */
37643+
1facf9fc 37644+/*
37645+ * xino mount option handlers
37646+ */
1facf9fc 37647+
37648+/* xino bitmap */
37649+static void xino_clear_xib(struct super_block *sb)
37650+{
37651+ struct au_sbinfo *sbinfo;
37652+
dece6358
AM
37653+ SiMustWriteLock(sb);
37654+
1facf9fc 37655+ sbinfo = au_sbi(sb);
062440b3 37656+ /* unnecessary to clear sbinfo->si_xread and ->si_xwrite */
1facf9fc 37657+ if (sbinfo->si_xib)
37658+ fput(sbinfo->si_xib);
37659+ sbinfo->si_xib = NULL;
f0c0a007 37660+ if (sbinfo->si_xib_buf)
1c60b727 37661+ free_page((unsigned long)sbinfo->si_xib_buf);
1facf9fc 37662+ sbinfo->si_xib_buf = NULL;
37663+}
37664+
062440b3 37665+static int au_xino_set_xib(struct super_block *sb, struct path *path)
1facf9fc 37666+{
37667+ int err;
37668+ loff_t pos;
37669+ struct au_sbinfo *sbinfo;
37670+ struct file *file;
37671+
dece6358
AM
37672+ SiMustWriteLock(sb);
37673+
1facf9fc 37674+ sbinfo = au_sbi(sb);
062440b3 37675+ file = au_xino_create2(sb, path, sbinfo->si_xib);
1facf9fc 37676+ err = PTR_ERR(file);
37677+ if (IS_ERR(file))
37678+ goto out;
37679+ if (sbinfo->si_xib)
37680+ fput(sbinfo->si_xib);
37681+ sbinfo->si_xib = file;
5527c038
JR
37682+ sbinfo->si_xread = vfs_readf(file);
37683+ sbinfo->si_xwrite = vfs_writef(file);
1facf9fc 37684+
37685+ err = -ENOMEM;
37686+ if (!sbinfo->si_xib_buf)
37687+ sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
37688+ if (unlikely(!sbinfo->si_xib_buf))
37689+ goto out_unset;
37690+
37691+ sbinfo->si_xib_last_pindex = 0;
37692+ sbinfo->si_xib_next_bit = 0;
c06a8ce3 37693+ if (vfsub_f_size_read(file) < PAGE_SIZE) {
1facf9fc 37694+ pos = 0;
37695+ err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
37696+ PAGE_SIZE, &pos);
37697+ if (unlikely(err != PAGE_SIZE))
37698+ goto out_free;
37699+ }
37700+ err = 0;
37701+ goto out; /* success */
37702+
4f0767ce 37703+out_free:
f0c0a007 37704+ if (sbinfo->si_xib_buf)
1c60b727 37705+ free_page((unsigned long)sbinfo->si_xib_buf);
b752ccd1
AM
37706+ sbinfo->si_xib_buf = NULL;
37707+ if (err >= 0)
37708+ err = -EIO;
4f0767ce 37709+out_unset:
b752ccd1
AM
37710+ fput(sbinfo->si_xib);
37711+ sbinfo->si_xib = NULL;
4f0767ce 37712+out:
062440b3 37713+ AuTraceErr(err);
b752ccd1 37714+ return err;
1facf9fc 37715+}
37716+
b752ccd1
AM
37717+/* xino for each branch */
37718+static void xino_clear_br(struct super_block *sb)
37719+{
5afbbe0d 37720+ aufs_bindex_t bindex, bbot;
b752ccd1 37721+ struct au_branch *br;
1facf9fc 37722+
5afbbe0d
AM
37723+ bbot = au_sbbot(sb);
37724+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1 37725+ br = au_sbr(sb, bindex);
062440b3
AM
37726+ AuDebugOn(!br);
37727+ au_xino_put(br);
37728+ }
37729+}
37730+
37731+static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
37732+ aufs_bindex_t bshared)
37733+{
37734+ struct au_branch *brshared;
b752ccd1 37735+
062440b3
AM
37736+ brshared = au_sbr(sb, bshared);
37737+ AuDebugOn(!brshared->br_xino);
37738+ AuDebugOn(!brshared->br_xino->xi_file);
37739+ if (br->br_xino != brshared->br_xino) {
37740+ au_xino_get(brshared);
37741+ au_xino_put(br);
37742+ br->br_xino = brshared->br_xino;
b752ccd1
AM
37743+ }
37744+}
37745+
062440b3
AM
37746+struct au_xino_do_set_br {
37747+ vfs_writef_t writef;
37748+ struct au_branch *br;
37749+ ino_t h_ino;
37750+ aufs_bindex_t bshared;
37751+};
37752+
37753+static int au_xino_do_set_br(struct super_block *sb, struct path *path,
37754+ struct au_xino_do_set_br *args)
1facf9fc 37755+{
37756+ int err;
062440b3
AM
37757+ struct file *file;
37758+
37759+ if (args->bshared >= 0) {
37760+ /* shared xino */
37761+ au_xino_set_br_shared(sb, args->br, args->bshared);
37762+ file = au_xino_file(args->br);
37763+ goto out_ino; /* success */
37764+ }
37765+
37766+ /* new xino */
37767+ file = au_xino_create2(sb, path, au_xino_file(args->br));
37768+ err = PTR_ERR(file);
37769+ if (IS_ERR(file))
37770+ goto out;
37771+ if (!args->br->br_xino) {
37772+ err = au_xino_init(args->br, file);
37773+ fput(file);
37774+ if (unlikely(err))
37775+ goto out;
37776+ } else {
37777+ au_xino_file_set(args->br, file);
37778+ fput(file);
37779+ }
37780+
37781+out_ino:
37782+ err = au_xino_do_write(args->writef, file, args->h_ino, AUFS_ROOT_INO);
37783+out:
37784+ AuTraceErr(err);
37785+ return err;
37786+}
37787+
37788+static int au_xino_set_br(struct super_block *sb, struct path *path)
37789+{
37790+ int err;
37791+ aufs_bindex_t bindex, bbot;
37792+ struct au_xino_do_set_br args;
b752ccd1 37793+ struct inode *inode;
1facf9fc 37794+
b752ccd1
AM
37795+ SiMustWriteLock(sb);
37796+
5afbbe0d 37797+ bbot = au_sbbot(sb);
5527c038 37798+ inode = d_inode(sb->s_root);
062440b3
AM
37799+ args.writef = au_sbi(sb)->si_xwrite;
37800+ for (bindex = 0; bindex <= bbot; bindex++) {
37801+ args.h_ino = au_h_iptr(inode, bindex)->i_ino;
37802+ args.br = au_sbr(sb, bindex);
37803+ args.bshared = is_sb_shared(sb, bindex, bindex - 1);
37804+ err = au_xino_do_set_br(sb, path, &args);
b752ccd1 37805+ if (unlikely(err))
062440b3 37806+ break;
b752ccd1 37807+ }
1facf9fc 37808+
062440b3 37809+ AuTraceErr(err);
1facf9fc 37810+ return err;
37811+}
b752ccd1
AM
37812+
37813+void au_xino_clr(struct super_block *sb)
37814+{
37815+ struct au_sbinfo *sbinfo;
37816+
37817+ au_xigen_clr(sb);
37818+ xino_clear_xib(sb);
37819+ xino_clear_br(sb);
062440b3
AM
37820+ dbgaufs_brs_del(sb, 0);
37821+ au_xino_brid_set(sb, -1);
b752ccd1
AM
37822+ sbinfo = au_sbi(sb);
37823+ /* lvalue, do not call au_mntflags() */
37824+ au_opt_clr(sbinfo->si_mntflags, XINO);
37825+}
37826+
062440b3 37827+int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
b752ccd1
AM
37828+{
37829+ int err, skip;
062440b3 37830+ struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
b752ccd1
AM
37831+ struct qstr *dname, *cur_name;
37832+ struct file *cur_xino;
b752ccd1 37833+ struct au_sbinfo *sbinfo;
062440b3 37834+ struct path *path, *cur_path;
b752ccd1
AM
37835+
37836+ SiMustWriteLock(sb);
37837+
37838+ err = 0;
37839+ sbinfo = au_sbi(sb);
062440b3
AM
37840+ path = &xiopt->file->f_path;
37841+ dentry = path->dentry;
37842+ parent = dget_parent(dentry);
b752ccd1
AM
37843+ if (remount) {
37844+ skip = 0;
b752ccd1
AM
37845+ cur_xino = sbinfo->si_xib;
37846+ if (cur_xino) {
062440b3
AM
37847+ cur_path = &cur_xino->f_path;
37848+ cur_dentry = cur_path->dentry;
37849+ cur_parent = dget_parent(cur_dentry);
37850+ cur_name = &cur_dentry->d_name;
37851+ dname = &dentry->d_name;
b752ccd1 37852+ skip = (cur_parent == parent
38d290e6 37853+ && au_qstreq(dname, cur_name));
b752ccd1
AM
37854+ dput(cur_parent);
37855+ }
37856+ if (skip)
37857+ goto out;
37858+ }
37859+
37860+ au_opt_set(sbinfo->si_mntflags, XINO);
062440b3
AM
37861+ err = au_xino_set_xib(sb, path);
37862+ /* si_x{read,write} are set */
b752ccd1 37863+ if (!err)
062440b3 37864+ err = au_xigen_set(sb, path);
b752ccd1 37865+ if (!err)
062440b3
AM
37866+ err = au_xino_set_br(sb, path);
37867+ if (!err) {
37868+ dbgaufs_brs_add(sb, 0, /*topdown*/1);
b752ccd1 37869+ goto out; /* success */
062440b3 37870+ }
b752ccd1
AM
37871+
37872+ /* reset all */
062440b3
AM
37873+ AuIOErr("failed setting xino(%d).\n", err);
37874+ au_xino_clr(sb);
b752ccd1 37875+
4f0767ce 37876+out:
b752ccd1
AM
37877+ dput(parent);
37878+ return err;
37879+}
37880+
b752ccd1
AM
37881+/*
37882+ * create a xinofile at the default place/path.
37883+ */
37884+struct file *au_xino_def(struct super_block *sb)
37885+{
37886+ struct file *file;
37887+ char *page, *p;
37888+ struct au_branch *br;
37889+ struct super_block *h_sb;
37890+ struct path path;
5afbbe0d 37891+ aufs_bindex_t bbot, bindex, bwr;
b752ccd1
AM
37892+
37893+ br = NULL;
5afbbe0d 37894+ bbot = au_sbbot(sb);
b752ccd1 37895+ bwr = -1;
5afbbe0d 37896+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1
AM
37897+ br = au_sbr(sb, bindex);
37898+ if (au_br_writable(br->br_perm)
86dc4139 37899+ && !au_test_fs_bad_xino(au_br_sb(br))) {
b752ccd1
AM
37900+ bwr = bindex;
37901+ break;
37902+ }
37903+ }
37904+
7f207e10
AM
37905+ if (bwr >= 0) {
37906+ file = ERR_PTR(-ENOMEM);
537831f9 37907+ page = (void *)__get_free_page(GFP_NOFS);
7f207e10
AM
37908+ if (unlikely(!page))
37909+ goto out;
86dc4139 37910+ path.mnt = au_br_mnt(br);
7f207e10
AM
37911+ path.dentry = au_h_dptr(sb->s_root, bwr);
37912+ p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
37913+ file = (void *)p;
37914+ if (!IS_ERR(p)) {
37915+ strcat(p, "/" AUFS_XINO_FNAME);
37916+ AuDbg("%s\n", p);
37917+ file = au_xino_create(sb, p, /*silent*/0);
37918+ if (!IS_ERR(file))
37919+ au_xino_brid_set(sb, br->br_id);
37920+ }
1c60b727 37921+ free_page((unsigned long)page);
7f207e10
AM
37922+ } else {
37923+ file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
37924+ if (IS_ERR(file))
37925+ goto out;
2000de60 37926+ h_sb = file->f_path.dentry->d_sb;
7f207e10
AM
37927+ if (unlikely(au_test_fs_bad_xino(h_sb))) {
37928+ pr_err("xino doesn't support %s(%s)\n",
37929+ AUFS_XINO_DEFPATH, au_sbtype(h_sb));
37930+ fput(file);
37931+ file = ERR_PTR(-EINVAL);
37932+ }
37933+ if (!IS_ERR(file))
37934+ au_xino_brid_set(sb, -1);
37935+ }
0c5527e5 37936+
7f207e10
AM
37937+out:
37938+ return file;
37939+}
37940+
37941+/* ---------------------------------------------------------------------- */
37942+
062440b3
AM
37943+/*
37944+ * initialize the xinofile for the specified branch @br
37945+ * at the place/path where @base_file indicates.
37946+ * test whether another branch is on the same filesystem or not,
37947+ * if found then share the xinofile with another branch.
37948+ */
37949+int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
37950+ struct path *base)
7f207e10
AM
37951+{
37952+ int err;
062440b3
AM
37953+ struct au_xino_do_set_br args = {
37954+ .h_ino = h_ino,
37955+ .br = br
37956+ };
7f207e10 37957+
062440b3
AM
37958+ args.writef = au_sbi(sb)->si_xwrite;
37959+ args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
37960+ au_br_sb(br));
37961+ err = au_xino_do_set_br(sb, base, &args);
79b8bda9 37962+ if (unlikely(err))
062440b3 37963+ au_xino_put(br);
7f207e10 37964+
7f207e10
AM
37965+ return err;
37966+}
521ced18
JR
37967+
37968+/* ---------------------------------------------------------------------- */
37969+
062440b3
AM
37970+/*
37971+ * get an unused inode number from bitmap
37972+ */
37973+ino_t au_xino_new_ino(struct super_block *sb)
37974+{
37975+ ino_t ino;
37976+ unsigned long *p, pindex, ul, pend;
37977+ struct au_sbinfo *sbinfo;
37978+ struct file *file;
37979+ int free_bit, err;
37980+
37981+ if (!au_opt_test(au_mntflags(sb), XINO))
37982+ return iunique(sb, AUFS_FIRST_INO);
37983+
37984+ sbinfo = au_sbi(sb);
37985+ mutex_lock(&sbinfo->si_xib_mtx);
37986+ p = sbinfo->si_xib_buf;
37987+ free_bit = sbinfo->si_xib_next_bit;
37988+ if (free_bit < page_bits && !test_bit(free_bit, p))
37989+ goto out; /* success */
37990+ free_bit = find_first_zero_bit(p, page_bits);
37991+ if (free_bit < page_bits)
37992+ goto out; /* success */
37993+
37994+ pindex = sbinfo->si_xib_last_pindex;
37995+ for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
37996+ err = xib_pindex(sb, ul);
37997+ if (unlikely(err))
37998+ goto out_err;
37999+ free_bit = find_first_zero_bit(p, page_bits);
38000+ if (free_bit < page_bits)
38001+ goto out; /* success */
38002+ }
38003+
38004+ file = sbinfo->si_xib;
38005+ pend = vfsub_f_size_read(file) / PAGE_SIZE;
38006+ for (ul = pindex + 1; ul <= pend; ul++) {
38007+ err = xib_pindex(sb, ul);
38008+ if (unlikely(err))
38009+ goto out_err;
38010+ free_bit = find_first_zero_bit(p, page_bits);
38011+ if (free_bit < page_bits)
38012+ goto out; /* success */
38013+ }
38014+ BUG();
38015+
38016+out:
38017+ set_bit(free_bit, p);
38018+ sbinfo->si_xib_next_bit = free_bit + 1;
38019+ pindex = sbinfo->si_xib_last_pindex;
38020+ mutex_unlock(&sbinfo->si_xib_mtx);
38021+ ino = xib_calc_ino(pindex, free_bit);
38022+ AuDbg("i%lu\n", (unsigned long)ino);
38023+ return ino;
38024+out_err:
38025+ mutex_unlock(&sbinfo->si_xib_mtx);
38026+ AuDbg("i0\n");
38027+ return 0;
38028+}
38029+
38030+/* for s_op->delete_inode() */
38031+void au_xino_delete_inode(struct inode *inode, const int unlinked)
521ced18 38032+{
062440b3
AM
38033+ int err;
38034+ unsigned int mnt_flags;
38035+ aufs_bindex_t bindex, bbot, bi;
38036+ unsigned char try_trunc;
38037+ struct au_iinfo *iinfo;
38038+ struct super_block *sb;
38039+ struct au_hinode *hi;
38040+ struct inode *h_inode;
38041+ struct au_branch *br;
38042+ vfs_writef_t xwrite;
521ced18 38043+
062440b3 38044+ AuDebugOn(au_is_bad_inode(inode));
521ced18 38045+
062440b3
AM
38046+ sb = inode->i_sb;
38047+ mnt_flags = au_mntflags(sb);
38048+ if (!au_opt_test(mnt_flags, XINO)
38049+ || inode->i_ino == AUFS_ROOT_INO)
38050+ return;
38051+
38052+ if (unlinked) {
38053+ au_xigen_inc(inode);
38054+ au_xib_clear_bit(inode);
38055+ }
38056+
38057+ iinfo = au_ii(inode);
38058+ bindex = iinfo->ii_btop;
38059+ if (bindex < 0)
38060+ return;
38061+
38062+ xwrite = au_sbi(sb)->si_xwrite;
38063+ try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38064+ hi = au_hinode(iinfo, bindex);
38065+ bbot = iinfo->ii_bbot;
38066+ for (; bindex <= bbot; bindex++, hi++) {
38067+ h_inode = hi->hi_inode;
38068+ if (!h_inode
38069+ || (!unlinked && h_inode->i_nlink))
38070+ continue;
38071+
38072+ /* inode may not be revalidated */
38073+ bi = au_br_index(sb, hi->hi_id);
38074+ if (bi < 0)
38075+ continue;
38076+
38077+ br = au_sbr(sb, bi);
38078+ err = au_xino_do_write(xwrite, au_xino_file(br),
38079+ h_inode->i_ino, /*ino*/0);
38080+ if (!err && try_trunc
38081+ && au_test_fs_trunc_xino(au_br_sb(br)))
38082+ xino_try_trunc(sb, br);
38083+ }
521ced18
JR
38084+}
38085+
062440b3
AM
38086+/* ---------------------------------------------------------------------- */
38087+
38088+static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
521ced18
JR
38089+{
38090+ int found, total, i;
38091+
38092+ found = -1;
062440b3 38093+ total = xi->xi_nondir.total;
521ced18 38094+ for (i = 0; i < total; i++) {
062440b3 38095+ if (xi->xi_nondir.array[i] != h_ino)
521ced18
JR
38096+ continue;
38097+ found = i;
38098+ break;
38099+ }
38100+
38101+ return found;
38102+}
38103+
062440b3 38104+static int au_xinondir_expand(struct au_xino *xi)
521ced18
JR
38105+{
38106+ int err, sz;
38107+ ino_t *p;
38108+
38109+ BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38110+
38111+ err = -ENOMEM;
062440b3 38112+ sz = xi->xi_nondir.total * sizeof(ino_t);
521ced18
JR
38113+ if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38114+ goto out;
062440b3 38115+ p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
521ced18
JR
38116+ /*may_shrink*/0);
38117+ if (p) {
062440b3
AM
38118+ xi->xi_nondir.array = p;
38119+ xi->xi_nondir.total <<= 1;
38120+ AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
521ced18
JR
38121+ err = 0;
38122+ }
38123+
38124+out:
38125+ return err;
38126+}
38127+
062440b3
AM
38128+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38129+ ino_t h_ino, int idx)
38130+{
38131+ struct au_xino *xi;
38132+
38133+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38134+ xi = au_sbr(sb, bindex)->br_xino;
38135+ AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38136+
38137+ spin_lock(&xi->xi_nondir.spin);
38138+ AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38139+ xi->xi_nondir.array[idx] = 0;
38140+ spin_unlock(&xi->xi_nondir.spin);
38141+ wake_up_all(&xi->xi_nondir.wqh);
38142+}
38143+
521ced18
JR
38144+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38145+ int *idx)
38146+{
38147+ int err, found, empty;
062440b3 38148+ struct au_xino *xi;
521ced18
JR
38149+
38150+ err = 0;
38151+ *idx = -1;
38152+ if (!au_opt_test(au_mntflags(sb), XINO))
38153+ goto out; /* no xino */
38154+
062440b3 38155+ xi = au_sbr(sb, bindex)->br_xino;
521ced18
JR
38156+
38157+again:
062440b3
AM
38158+ spin_lock(&xi->xi_nondir.spin);
38159+ found = au_xinondir_find(xi, h_ino);
521ced18 38160+ if (found == -1) {
062440b3 38161+ empty = au_xinondir_find(xi, /*h_ino*/0);
521ced18 38162+ if (empty == -1) {
062440b3
AM
38163+ empty = xi->xi_nondir.total;
38164+ err = au_xinondir_expand(xi);
521ced18
JR
38165+ if (unlikely(err))
38166+ goto out_unlock;
38167+ }
062440b3 38168+ xi->xi_nondir.array[empty] = h_ino;
521ced18
JR
38169+ *idx = empty;
38170+ } else {
062440b3
AM
38171+ spin_unlock(&xi->xi_nondir.spin);
38172+ wait_event(xi->xi_nondir.wqh,
38173+ xi->xi_nondir.array[found] != h_ino);
521ced18
JR
38174+ goto again;
38175+ }
38176+
38177+out_unlock:
062440b3
AM
38178+ spin_unlock(&xi->xi_nondir.spin);
38179+out:
38180+ return err;
38181+}
38182+
38183+/* ---------------------------------------------------------------------- */
38184+
38185+int au_xino_path(struct seq_file *seq, struct file *file)
38186+{
38187+ int err;
38188+
38189+ err = au_seq_path(seq, &file->f_path);
38190+ if (unlikely(err))
38191+ goto out;
38192+
38193+#define Deleted "\\040(deleted)"
38194+ seq->count -= sizeof(Deleted) - 1;
38195+ AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38196+ sizeof(Deleted) - 1));
38197+#undef Deleted
38198+
521ced18
JR
38199+out:
38200+ return err;
38201+}
537831f9
AM
38202diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
38203--- /usr/share/empty/include/uapi/linux/aufs_type.h 1970-01-01 01:00:00.000000000 +0100
cd7a4cd9 38204+++ linux/include/uapi/linux/aufs_type.h 2018-08-12 23:43:05.466791610 +0200
062440b3
AM
38205@@ -0,0 +1,448 @@
38206+/* SPDX-License-Identifier: GPL-2.0 */
7f207e10 38207+/*
b00004a5 38208+ * Copyright (C) 2005-2018 Junjiro R. Okajima
7f207e10
AM
38209+ *
38210+ * This program, aufs is free software; you can redistribute it and/or modify
38211+ * it under the terms of the GNU General Public License as published by
38212+ * the Free Software Foundation; either version 2 of the License, or
38213+ * (at your option) any later version.
38214+ *
38215+ * This program is distributed in the hope that it will be useful,
38216+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
38217+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38218+ * GNU General Public License for more details.
38219+ *
38220+ * You should have received a copy of the GNU General Public License
523b37e3 38221+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
38222+ */
38223+
38224+#ifndef __AUFS_TYPE_H__
38225+#define __AUFS_TYPE_H__
38226+
f6c5ef8b
AM
38227+#define AUFS_NAME "aufs"
38228+
9dbd164d 38229+#ifdef __KERNEL__
f6c5ef8b
AM
38230+/*
38231+ * define it before including all other headers.
38232+ * sched.h may use pr_* macros before defining "current", so define the
38233+ * no-current version first, and re-define later.
38234+ */
38235+#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
38236+#include <linux/sched.h>
38237+#undef pr_fmt
a2a7ad62
AM
38238+#define pr_fmt(fmt) \
38239+ AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
38240+ (int)sizeof(current->comm), current->comm, current->pid
9dbd164d
AM
38241+#else
38242+#include <stdint.h>
38243+#include <sys/types.h>
f6c5ef8b 38244+#endif /* __KERNEL__ */
7f207e10 38245+
f6c5ef8b
AM
38246+#include <linux/limits.h>
38247+
cd7a4cd9 38248+#define AUFS_VERSION "4.x-rcN-20180730"
7f207e10
AM
38249+
38250+/* todo? move this to linux-2.6.19/include/magic.h */
38251+#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
38252+
38253+/* ---------------------------------------------------------------------- */
38254+
38255+#ifdef CONFIG_AUFS_BRANCH_MAX_127
9dbd164d 38256+typedef int8_t aufs_bindex_t;
7f207e10
AM
38257+#define AUFS_BRANCH_MAX 127
38258+#else
9dbd164d 38259+typedef int16_t aufs_bindex_t;
7f207e10
AM
38260+#ifdef CONFIG_AUFS_BRANCH_MAX_511
38261+#define AUFS_BRANCH_MAX 511
38262+#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
38263+#define AUFS_BRANCH_MAX 1023
38264+#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
38265+#define AUFS_BRANCH_MAX 32767
38266+#endif
38267+#endif
38268+
38269+#ifdef __KERNEL__
38270+#ifndef AUFS_BRANCH_MAX
38271+#error unknown CONFIG_AUFS_BRANCH_MAX value
38272+#endif
38273+#endif /* __KERNEL__ */
38274+
38275+/* ---------------------------------------------------------------------- */
38276+
7f207e10
AM
38277+#define AUFS_FSTYPE AUFS_NAME
38278+
38279+#define AUFS_ROOT_INO 2
38280+#define AUFS_FIRST_INO 11
38281+
38282+#define AUFS_WH_PFX ".wh."
38283+#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
38284+#define AUFS_WH_TMP_LEN 4
86dc4139 38285+/* a limit for rmdir/rename a dir and copyup */
7f207e10
AM
38286+#define AUFS_MAX_NAMELEN (NAME_MAX \
38287+ - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
38288+ - 1 /* dot */\
38289+ - AUFS_WH_TMP_LEN) /* hex */
38290+#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
38291+#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
392086de
AM
38292+#define AUFS_XINO_DEF_SEC 30 /* seconds */
38293+#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
7f207e10
AM
38294+#define AUFS_DIRWH_DEF 3
38295+#define AUFS_RDCACHE_DEF 10 /* seconds */
027c5e7a 38296+#define AUFS_RDCACHE_MAX 3600 /* seconds */
7f207e10
AM
38297+#define AUFS_RDBLK_DEF 512 /* bytes */
38298+#define AUFS_RDHASH_DEF 32
38299+#define AUFS_WKQ_NAME AUFS_NAME "d"
027c5e7a
AM
38300+#define AUFS_MFS_DEF_SEC 30 /* seconds */
38301+#define AUFS_MFS_MAX_SEC 3600 /* seconds */
076b876e 38302+#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
86dc4139 38303+#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
7f207e10
AM
38304+
38305+/* pseudo-link maintenace under /proc */
38306+#define AUFS_PLINK_MAINT_NAME "plink_maint"
38307+#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
38308+#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
38309+
8b6a4947
AM
38310+/* dirren, renamed dir */
38311+#define AUFS_DR_INFO_PFX AUFS_WH_PFX ".dr."
38312+#define AUFS_DR_BRHINO_NAME AUFS_WH_PFX "hino"
38313+/* whiteouted doubly */
38314+#define AUFS_WH_DR_INFO_PFX AUFS_WH_PFX AUFS_DR_INFO_PFX
38315+#define AUFS_WH_DR_BRHINO AUFS_WH_PFX AUFS_DR_BRHINO_NAME
38316+
7f207e10
AM
38317+#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
38318+#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
38319+
38320+#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
38321+#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
38322+#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
38323+
38324+/* doubly whiteouted */
38325+#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
38326+#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
38327+#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
38328+
1e00d052 38329+/* branch permissions and attributes */
7f207e10
AM
38330+#define AUFS_BRPERM_RW "rw"
38331+#define AUFS_BRPERM_RO "ro"
38332+#define AUFS_BRPERM_RR "rr"
076b876e
AM
38333+#define AUFS_BRATTR_COO_REG "coo_reg"
38334+#define AUFS_BRATTR_COO_ALL "coo_all"
38335+#define AUFS_BRATTR_FHSM "fhsm"
38336+#define AUFS_BRATTR_UNPIN "unpin"
c1595e42
JR
38337+#define AUFS_BRATTR_ICEX "icex"
38338+#define AUFS_BRATTR_ICEX_SEC "icexsec"
38339+#define AUFS_BRATTR_ICEX_SYS "icexsys"
38340+#define AUFS_BRATTR_ICEX_TR "icextr"
38341+#define AUFS_BRATTR_ICEX_USR "icexusr"
38342+#define AUFS_BRATTR_ICEX_OTH "icexoth"
1e00d052
AM
38343+#define AUFS_BRRATTR_WH "wh"
38344+#define AUFS_BRWATTR_NLWH "nolwh"
076b876e
AM
38345+#define AUFS_BRWATTR_MOO "moo"
38346+
38347+#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
38348+#define AuBrPerm_RO (1 << 1) /* readonly */
38349+#define AuBrPerm_RR (1 << 2) /* natively readonly */
38350+#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
38351+
38352+#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
38353+#define AuBrAttr_COO_ALL (1 << 4)
38354+#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
38355+
38356+#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
38357+#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
c1595e42
JR
38358+ branch. meaningless since
38359+ linux-3.18-rc1 */
38360+
38361+/* ignore error in copying XATTR */
38362+#define AuBrAttr_ICEX_SEC (1 << 7)
38363+#define AuBrAttr_ICEX_SYS (1 << 8)
38364+#define AuBrAttr_ICEX_TR (1 << 9)
38365+#define AuBrAttr_ICEX_USR (1 << 10)
38366+#define AuBrAttr_ICEX_OTH (1 << 11)
38367+#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
38368+ | AuBrAttr_ICEX_SYS \
38369+ | AuBrAttr_ICEX_TR \
38370+ | AuBrAttr_ICEX_USR \
38371+ | AuBrAttr_ICEX_OTH)
38372+
38373+#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
076b876e
AM
38374+#define AuBrRAttr_Mask AuBrRAttr_WH
38375+
c1595e42
JR
38376+#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
38377+#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
076b876e
AM
38378+#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
38379+
38380+#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
38381+
c1595e42 38382+/* #warning test userspace */
076b876e
AM
38383+#ifdef __KERNEL__
38384+#ifndef CONFIG_AUFS_FHSM
38385+#undef AuBrAttr_FHSM
38386+#define AuBrAttr_FHSM 0
38387+#endif
c1595e42
JR
38388+#ifndef CONFIG_AUFS_XATTR
38389+#undef AuBrAttr_ICEX
38390+#define AuBrAttr_ICEX 0
38391+#undef AuBrAttr_ICEX_SEC
38392+#define AuBrAttr_ICEX_SEC 0
38393+#undef AuBrAttr_ICEX_SYS
38394+#define AuBrAttr_ICEX_SYS 0
38395+#undef AuBrAttr_ICEX_TR
38396+#define AuBrAttr_ICEX_TR 0
38397+#undef AuBrAttr_ICEX_USR
38398+#define AuBrAttr_ICEX_USR 0
38399+#undef AuBrAttr_ICEX_OTH
38400+#define AuBrAttr_ICEX_OTH 0
38401+#endif
076b876e
AM
38402+#endif
38403+
38404+/* the longest combination */
c1595e42
JR
38405+/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
38406+#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
38407+ "+" AUFS_BRATTR_COO_REG \
38408+ "+" AUFS_BRATTR_FHSM \
38409+ "+" AUFS_BRATTR_UNPIN \
7e9cd9fe
AM
38410+ "+" AUFS_BRATTR_ICEX_SEC \
38411+ "+" AUFS_BRATTR_ICEX_SYS \
38412+ "+" AUFS_BRATTR_ICEX_USR \
38413+ "+" AUFS_BRATTR_ICEX_OTH \
076b876e
AM
38414+ "+" AUFS_BRWATTR_NLWH)
38415+
38416+typedef struct {
38417+ char a[AuBrPermStrSz];
38418+} au_br_perm_str_t;
38419+
38420+static inline int au_br_writable(int brperm)
38421+{
38422+ return brperm & AuBrPerm_RW;
38423+}
38424+
38425+static inline int au_br_whable(int brperm)
38426+{
38427+ return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
38428+}
38429+
38430+static inline int au_br_wh_linkable(int brperm)
38431+{
38432+ return !(brperm & AuBrWAttr_NoLinkWH);
38433+}
38434+
38435+static inline int au_br_cmoo(int brperm)
38436+{
38437+ return brperm & AuBrAttr_CMOO_Mask;
38438+}
38439+
38440+static inline int au_br_fhsm(int brperm)
38441+{
38442+ return brperm & AuBrAttr_FHSM;
38443+}
7f207e10
AM
38444+
38445+/* ---------------------------------------------------------------------- */
38446+
38447+/* ioctl */
38448+enum {
38449+ /* readdir in userspace */
38450+ AuCtl_RDU,
38451+ AuCtl_RDU_INO,
38452+
076b876e
AM
38453+ AuCtl_WBR_FD, /* pathconf wrapper */
38454+ AuCtl_IBUSY, /* busy inode */
38455+ AuCtl_MVDOWN, /* move-down */
38456+ AuCtl_BR, /* info about branches */
38457+ AuCtl_FHSM_FD /* connection for fhsm */
7f207e10
AM
38458+};
38459+
38460+/* borrowed from linux/include/linux/kernel.h */
38461+#ifndef ALIGN
38462+#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
38463+#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
38464+#endif
38465+
38466+/* borrowed from linux/include/linux/compiler-gcc3.h */
38467+#ifndef __aligned
38468+#define __aligned(x) __attribute__((aligned(x)))
53392da6
AM
38469+#endif
38470+
38471+#ifdef __KERNEL__
38472+#ifndef __packed
7f207e10
AM
38473+#define __packed __attribute__((packed))
38474+#endif
53392da6 38475+#endif
7f207e10
AM
38476+
38477+struct au_rdu_cookie {
9dbd164d
AM
38478+ uint64_t h_pos;
38479+ int16_t bindex;
38480+ uint8_t flags;
38481+ uint8_t pad;
38482+ uint32_t generation;
7f207e10
AM
38483+} __aligned(8);
38484+
38485+struct au_rdu_ent {
9dbd164d
AM
38486+ uint64_t ino;
38487+ int16_t bindex;
38488+ uint8_t type;
38489+ uint8_t nlen;
38490+ uint8_t wh;
7f207e10
AM
38491+ char name[0];
38492+} __aligned(8);
38493+
38494+static inline int au_rdu_len(int nlen)
38495+{
38496+ /* include the terminating NULL */
38497+ return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
9dbd164d 38498+ sizeof(uint64_t));
7f207e10
AM
38499+}
38500+
38501+union au_rdu_ent_ul {
38502+ struct au_rdu_ent __user *e;
9dbd164d 38503+ uint64_t ul;
7f207e10
AM
38504+};
38505+
38506+enum {
38507+ AufsCtlRduV_SZ,
38508+ AufsCtlRduV_End
38509+};
38510+
38511+struct aufs_rdu {
38512+ /* input */
38513+ union {
9dbd164d
AM
38514+ uint64_t sz; /* AuCtl_RDU */
38515+ uint64_t nent; /* AuCtl_RDU_INO */
7f207e10
AM
38516+ };
38517+ union au_rdu_ent_ul ent;
9dbd164d 38518+ uint16_t verify[AufsCtlRduV_End];
7f207e10
AM
38519+
38520+ /* input/output */
9dbd164d 38521+ uint32_t blk;
7f207e10
AM
38522+
38523+ /* output */
38524+ union au_rdu_ent_ul tail;
38525+ /* number of entries which were added in a single call */
9dbd164d
AM
38526+ uint64_t rent;
38527+ uint8_t full;
38528+ uint8_t shwh;
7f207e10
AM
38529+
38530+ struct au_rdu_cookie cookie;
38531+} __aligned(8);
38532+
1e00d052
AM
38533+/* ---------------------------------------------------------------------- */
38534+
8b6a4947
AM
38535+/* dirren. the branch is identified by the filename who contains this */
38536+struct au_drinfo {
38537+ uint64_t ino;
38538+ union {
38539+ uint8_t oldnamelen;
38540+ uint64_t _padding;
38541+ };
38542+ uint8_t oldname[0];
38543+} __aligned(8);
38544+
38545+struct au_drinfo_fdata {
38546+ uint32_t magic;
38547+ struct au_drinfo drinfo;
38548+} __aligned(8);
38549+
38550+#define AUFS_DRINFO_MAGIC_V1 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
38551+/* future */
38552+#define AUFS_DRINFO_MAGIC_V2 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
38553+
38554+/* ---------------------------------------------------------------------- */
38555+
1e00d052 38556+struct aufs_wbr_fd {
9dbd164d
AM
38557+ uint32_t oflags;
38558+ int16_t brid;
1e00d052
AM
38559+} __aligned(8);
38560+
38561+/* ---------------------------------------------------------------------- */
38562+
027c5e7a 38563+struct aufs_ibusy {
9dbd164d
AM
38564+ uint64_t ino, h_ino;
38565+ int16_t bindex;
027c5e7a
AM
38566+} __aligned(8);
38567+
1e00d052
AM
38568+/* ---------------------------------------------------------------------- */
38569+
392086de
AM
38570+/* error code for move-down */
38571+/* the actual message strings are implemented in aufs-util.git */
38572+enum {
38573+ EAU_MVDOWN_OPAQUE = 1,
38574+ EAU_MVDOWN_WHITEOUT,
38575+ EAU_MVDOWN_UPPER,
38576+ EAU_MVDOWN_BOTTOM,
38577+ EAU_MVDOWN_NOUPPER,
38578+ EAU_MVDOWN_NOLOWERBR,
38579+ EAU_Last
38580+};
38581+
c2b27bf2 38582+/* flags for move-down */
392086de
AM
38583+#define AUFS_MVDOWN_DMSG 1
38584+#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
38585+#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
38586+#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
38587+#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
38588+#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
38589+#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
38590+#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
38591+#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
076b876e
AM
38592+#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
38593+#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
38594+#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
38595+#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
c2b27bf2 38596+
076b876e 38597+/* index for move-down */
392086de
AM
38598+enum {
38599+ AUFS_MVDOWN_UPPER,
38600+ AUFS_MVDOWN_LOWER,
38601+ AUFS_MVDOWN_NARRAY
38602+};
38603+
076b876e
AM
38604+/*
38605+ * additional info of move-down
38606+ * number of free blocks and inodes.
38607+ * subset of struct kstatfs, but smaller and always 64bit.
38608+ */
38609+struct aufs_stfs {
38610+ uint64_t f_blocks;
38611+ uint64_t f_bavail;
38612+ uint64_t f_files;
38613+ uint64_t f_ffree;
38614+};
38615+
38616+struct aufs_stbr {
38617+ int16_t brid; /* optional input */
38618+ int16_t bindex; /* output */
38619+ struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
38620+} __aligned(8);
38621+
c2b27bf2 38622+struct aufs_mvdown {
076b876e
AM
38623+ uint32_t flags; /* input/output */
38624+ struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
38625+ int8_t au_errno; /* output */
38626+} __aligned(8);
38627+
38628+/* ---------------------------------------------------------------------- */
38629+
38630+union aufs_brinfo {
38631+ /* PATH_MAX may differ between kernel-space and user-space */
38632+ char _spacer[4096];
392086de 38633+ struct {
076b876e
AM
38634+ int16_t id;
38635+ int perm;
38636+ char path[0];
38637+ };
c2b27bf2
AM
38638+} __aligned(8);
38639+
38640+/* ---------------------------------------------------------------------- */
38641+
7f207e10
AM
38642+#define AuCtlType 'A'
38643+#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
38644+#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
1e00d052
AM
38645+#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
38646+ struct aufs_wbr_fd)
027c5e7a 38647+#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
392086de
AM
38648+#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
38649+ struct aufs_mvdown)
076b876e
AM
38650+#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
38651+#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
7f207e10
AM
38652+
38653+#endif /* __AUFS_TYPE_H__ */
2121bcd9 38654SPDX-License-Identifier: GPL-2.0
cd7a4cd9 38655aufs4.x-rcN loopback patch
5527c038
JR
38656
38657diff --git a/drivers/block/loop.c b/drivers/block/loop.c
cd7a4cd9 38658index 5678bf8..ec9c4fb 100644
5527c038
JR
38659--- a/drivers/block/loop.c
38660+++ b/drivers/block/loop.c
cd7a4cd9 38661@@ -626,6 +626,15 @@ static inline void loop_update_dio(struct loop_device *lo)
8b6a4947 38662 lo->use_dio);
5527c038
JR
38663 }
38664
5527c038
JR
38665+static struct file *loop_real_file(struct file *file)
38666+{
38667+ struct file *f = NULL;
38668+
38669+ if (file->f_path.dentry->d_sb->s_op->real_loop)
38670+ f = file->f_path.dentry->d_sb->s_op->real_loop(file);
38671+ return f;
8b6a4947
AM
38672+}
38673+
c2c0f25c 38674 static void loop_reread_partitions(struct loop_device *lo,
8b6a4947
AM
38675 struct block_device *bdev)
38676 {
cd7a4cd9 38677@@ -690,6 +699,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
5527c038
JR
38678 unsigned int arg)
38679 {
38680 struct file *file, *old_file;
38681+ struct file *f, *virt_file = NULL, *old_virt_file;
38682 struct inode *inode;
38683 int error;
38684
cd7a4cd9
AM
38685@@ -706,6 +716,12 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
38686 file = fget(arg);
38687 if (!file)
38688 goto out;
5527c038
JR
38689+ f = loop_real_file(file);
38690+ if (f) {
38691+ virt_file = file;
38692+ file = f;
38693+ get_file(file);
38694+ }
38695
cd7a4cd9
AM
38696 error = loop_validate_file(file, bdev);
38697 if (error)
38698@@ -713,6 +729,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
38699
5527c038
JR
38700 inode = file->f_mapping->host;
38701 old_file = lo->lo_backing_file;
38702+ old_virt_file = lo->lo_backing_virt_file;
38703
38704 error = -EINVAL;
38705
cd7a4cd9 38706@@ -724,6 +741,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
8b6a4947
AM
38707 blk_mq_freeze_queue(lo->lo_queue);
38708 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
38709 lo->lo_backing_file = file;
38710+ lo->lo_backing_virt_file = virt_file;
38711 lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
38712 mapping_set_gfp_mask(file->f_mapping,
38713 lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
cd7a4cd9 38714@@ -731,12 +749,16 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
8b6a4947 38715 blk_mq_unfreeze_queue(lo->lo_queue);
5527c038
JR
38716
38717 fput(old_file);
38718+ if (old_virt_file)
38719+ fput(old_virt_file);
38720 if (lo->lo_flags & LO_FLAGS_PARTSCAN)
c2c0f25c 38721 loop_reread_partitions(lo, bdev);
5527c038
JR
38722 return 0;
38723
38724 out_putf:
38725 fput(file);
38726+ if (virt_file)
38727+ fput(virt_file);
38728 out:
38729 return error;
38730 }
cd7a4cd9 38731@@ -924,7 +946,7 @@ static int loop_prepare_queue(struct loop_device *lo)
5527c038
JR
38732 static int loop_set_fd(struct loop_device *lo, fmode_t mode,
38733 struct block_device *bdev, unsigned int arg)
38734 {
062440b3 38735- struct file *file;
5527c038
JR
38736+ struct file *file, *f, *virt_file = NULL;
38737 struct inode *inode;
38738 struct address_space *mapping;
8b6a4947 38739 int lo_flags = 0;
cd7a4cd9 38740@@ -938,6 +960,12 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
38741 file = fget(arg);
38742 if (!file)
38743 goto out;
38744+ f = loop_real_file(file);
38745+ if (f) {
38746+ virt_file = file;
38747+ file = f;
38748+ get_file(file);
38749+ }
38750
38751 error = -EBUSY;
38752 if (lo->lo_state != Lo_unbound)
cd7a4cd9 38753@@ -970,6 +998,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
38754 lo->lo_device = bdev;
38755 lo->lo_flags = lo_flags;
38756 lo->lo_backing_file = file;
38757+ lo->lo_backing_virt_file = virt_file;
38758 lo->transfer = NULL;
38759 lo->ioctl = NULL;
38760 lo->lo_sizelimit = 0;
cd7a4cd9 38761@@ -1003,6 +1032,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
38762
38763 out_putf:
38764 fput(file);
38765+ if (virt_file)
38766+ fput(virt_file);
38767 out:
38768 /* This is safe: open() is still holding a reference. */
38769 module_put(THIS_MODULE);
cd7a4cd9 38770@@ -1049,6 +1080,7 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
5527c038
JR
38771 static int loop_clr_fd(struct loop_device *lo)
38772 {
38773 struct file *filp = lo->lo_backing_file;
38774+ struct file *virt_filp = lo->lo_backing_virt_file;
38775 gfp_t gfp = lo->old_gfp_mask;
38776 struct block_device *bdev = lo->lo_device;
38777
cd7a4cd9 38778@@ -1080,6 +1112,7 @@ static int loop_clr_fd(struct loop_device *lo)
5527c038
JR
38779 spin_lock_irq(&lo->lo_lock);
38780 lo->lo_state = Lo_rundown;
38781 lo->lo_backing_file = NULL;
38782+ lo->lo_backing_virt_file = NULL;
38783 spin_unlock_irq(&lo->lo_lock);
38784
38785 loop_release_xfer(lo);
cd7a4cd9 38786@@ -1128,6 +1161,8 @@ static int loop_clr_fd(struct loop_device *lo)
5527c038
JR
38787 * bd_mutex which is usually taken before lo_ctl_mutex.
38788 */
38789 fput(filp);
38790+ if (virt_filp)
38791+ fput(virt_filp);
38792 return 0;
38793 }
38794
38795diff --git a/drivers/block/loop.h b/drivers/block/loop.h
cd7a4cd9 38796index 4d42c7a..a4974ee 100644
5527c038
JR
38797--- a/drivers/block/loop.h
38798+++ b/drivers/block/loop.h
38799@@ -46,7 +46,7 @@ struct loop_device {
38800 int (*ioctl)(struct loop_device *, int cmd,
38801 unsigned long arg);
38802
38803- struct file * lo_backing_file;
38804+ struct file * lo_backing_file, *lo_backing_virt_file;
38805 struct block_device *lo_device;
5527c038 38806 void *key_data;
8b6a4947 38807
5527c038 38808diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
cd7a4cd9 38809index 937a20d..1355656 100644
5527c038
JR
38810--- a/fs/aufs/f_op.c
38811+++ b/fs/aufs/f_op.c
2121bcd9 38812@@ -358,7 +358,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
5527c038
JR
38813 if (IS_ERR(h_file))
38814 goto out;
38815
38816- if (au_test_loopback_kthread()) {
38817+ if (0 && au_test_loopback_kthread()) {
38818 au_warn_loopback(h_file->f_path.dentry->d_sb);
38819 if (file->f_mapping != h_file->f_mapping) {
38820 file->f_mapping = h_file->f_mapping;
38821diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
cd7a4cd9 38822index 3d84349..a642de5 100644
5527c038
JR
38823--- a/fs/aufs/loop.c
38824+++ b/fs/aufs/loop.c
2121bcd9 38825@@ -133,3 +133,19 @@ void au_loopback_fin(void)
79b8bda9 38826 symbol_put(loop_backing_file);
1c60b727 38827 kfree(au_warn_loopback_array);
5527c038
JR
38828 }
38829+
38830+/* ---------------------------------------------------------------------- */
38831+
38832+/* support the loopback block device insude aufs */
38833+
38834+struct file *aufs_real_loop(struct file *file)
38835+{
38836+ struct file *f;
38837+
38838+ BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
38839+ fi_read_lock(file);
38840+ f = au_hf_top(file);
38841+ fi_read_unlock(file);
38842+ AuDebugOn(!f);
38843+ return f;
38844+}
38845diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
062440b3 38846index 05d703d..6bb23c8 100644
5527c038
JR
38847--- a/fs/aufs/loop.h
38848+++ b/fs/aufs/loop.h
2121bcd9 38849@@ -26,7 +26,11 @@ void au_warn_loopback(struct super_block *h_sb);
5527c038
JR
38850
38851 int au_loopback_init(void);
38852 void au_loopback_fin(void);
38853+
38854+struct file *aufs_real_loop(struct file *file);
38855 #else
38856+AuStub(struct file *, loop_backing_file, return NULL)
38857+
38858 AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
38859 struct dentry *h_adding)
38860 AuStubInt0(au_test_loopback_kthread, void)
2121bcd9 38861@@ -34,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
5527c038
JR
38862
38863 AuStubInt0(au_loopback_init, void)
38864 AuStubVoid(au_loopback_fin, void)
38865+
38866+AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
38867 #endif /* BLK_DEV_LOOP */
38868
38869 #endif /* __KERNEL__ */
38870diff --git a/fs/aufs/super.c b/fs/aufs/super.c
cd7a4cd9 38871index 4020639..13e315f 100644
5527c038
JR
38872--- a/fs/aufs/super.c
38873+++ b/fs/aufs/super.c
062440b3 38874@@ -840,7 +840,10 @@ static const struct super_operations aufs_sop = {
5527c038
JR
38875 .statfs = aufs_statfs,
38876 .put_super = aufs_put_super,
38877 .sync_fs = aufs_sync_fs,
38878- .remount_fs = aufs_remount_fs
38879+ .remount_fs = aufs_remount_fs,
38880+#ifdef CONFIG_AUFS_BDEV_LOOP
38881+ .real_loop = aufs_real_loop
38882+#endif
38883 };
38884
38885 /* ---------------------------------------------------------------------- */
38886diff --git a/include/linux/fs.h b/include/linux/fs.h
cd7a4cd9 38887index 0d0d247f..50d77f8 100644
5527c038
JR
38888--- a/include/linux/fs.h
38889+++ b/include/linux/fs.h
cd7a4cd9 38890@@ -1861,6 +1861,10 @@ struct super_operations {
5527c038
JR
38891 struct shrink_control *);
38892 long (*free_cached_objects)(struct super_block *,
38893 struct shrink_control *);
38894+#if defined(CONFIG_BLK_DEV_LOOP) || defined(CONFIG_BLK_DEV_LOOP_MODULE)
38895+ /* and aufs */
38896+ struct file *(*real_loop)(struct file *);
38897+#endif
38898 };
38899
38900 /*
This page took 7.457256 seconds and 4 git commands to generate.