]> git.pld-linux.org Git - packages/kernel.git/blame - kernel-aufs4.patch
- 4.14.17
[packages/kernel.git] / kernel-aufs4.patch
CommitLineData
ffa93bbd 1aufs4.x-rcN kbuild patch
7f207e10
AM
2
3diff --git a/fs/Kconfig b/fs/Kconfig
3c1bdaff 4index 7aee6d6..ec92031 100644
7f207e10
AM
5--- a/fs/Kconfig
6+++ b/fs/Kconfig
3c1bdaff 7@@ -248,6 +248,7 @@ source "fs/pstore/Kconfig"
5527c038 8 source "fs/sysv/Kconfig"
7e9cd9fe 9 source "fs/ufs/Kconfig"
7f207e10
AM
10 source "fs/exofs/Kconfig"
11+source "fs/aufs/Kconfig"
12
13 endif # MISC_FILESYSTEMS
14
15diff --git a/fs/Makefile b/fs/Makefile
a2654f78 16index 7bbaca9..a026491 100644
7f207e10
AM
17--- a/fs/Makefile
18+++ b/fs/Makefile
a2654f78 19@@ -128,3 +128,4 @@ obj-y += exofs/ # Multiple modules
7f207e10 20 obj-$(CONFIG_CEPH_FS) += ceph/
bf0370f2 21 obj-$(CONFIG_PSTORE) += pstore/
c06a8ce3 22 obj-$(CONFIG_EFIVAR_FS) += efivarfs/
86dc4139 23+obj-$(CONFIG_AUFS_FS) += aufs/
ffa93bbd 24aufs4.x-rcN base patch
7f207e10 25
c1595e42 26diff --git a/MAINTAINERS b/MAINTAINERS
8b6a4947 27index af0cb69..d360d2e 100644
c1595e42
JR
28--- a/MAINTAINERS
29+++ b/MAINTAINERS
8b6a4947 30@@ -2465,6 +2465,19 @@ F: include/linux/audit.h
c1595e42
JR
31 F: include/uapi/linux/audit.h
32 F: kernel/audit*
33
34+AUFS (advanced multi layered unification filesystem) FILESYSTEM
35+M: "J. R. Okajima" <hooanon05g@gmail.com>
36+L: linux-unionfs@vger.kernel.org
37+L: aufs-users@lists.sourceforge.net (members only)
38+W: http://aufs.sourceforge.net
5527c038 39+T: git://github.com/sfjro/aufs4-linux.git
c1595e42
JR
40+S: Supported
41+F: Documentation/filesystems/aufs/
42+F: Documentation/ABI/testing/debugfs-aufs
43+F: Documentation/ABI/testing/sysfs-aufs
44+F: fs/aufs/
45+F: include/uapi/linux/aufs_type.h
46+
47 AUXILIARY DISPLAY DRIVERS
48 M: Miguel Ojeda Sandonis <miguel.ojeda.sandonis@gmail.com>
49 W: http://miguelojeda.es/auxdisplay.htm
392086de 50diff --git a/drivers/block/loop.c b/drivers/block/loop.c
8b6a4947 51index 85de673..d44de9d 100644
392086de
AM
52--- a/drivers/block/loop.c
53+++ b/drivers/block/loop.c
8b6a4947 54@@ -686,6 +686,24 @@ static inline int is_loop_device(struct file *file)
392086de
AM
55 return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
56 }
57
58+/*
59+ * for AUFS
60+ * no get/put for file.
61+ */
62+struct file *loop_backing_file(struct super_block *sb)
63+{
64+ struct file *ret;
65+ struct loop_device *l;
66+
67+ ret = NULL;
68+ if (MAJOR(sb->s_dev) == LOOP_MAJOR) {
69+ l = sb->s_bdev->bd_disk->private_data;
70+ ret = l->lo_backing_file;
71+ }
72+ return ret;
73+}
febd17d6 74+EXPORT_SYMBOL_GPL(loop_backing_file);
392086de
AM
75+
76 /* loop sysfs attributes */
77
78 static ssize_t loop_attr_show(struct device *dev, char *page,
c1595e42 79diff --git a/fs/dcache.c b/fs/dcache.c
3c1bdaff 80index f901413..e3719a5 100644
c1595e42
JR
81--- a/fs/dcache.c
82+++ b/fs/dcache.c
3c1bdaff 83@@ -1197,7 +1197,7 @@ enum d_walk_ret {
c1595e42
JR
84 *
85 * The @enter() and @finish() callbacks are called with d_lock held.
86 */
87-static void d_walk(struct dentry *parent, void *data,
88+void d_walk(struct dentry *parent, void *data,
89 enum d_walk_ret (*enter)(void *, struct dentry *),
90 void (*finish)(void *))
91 {
febd17d6 92diff --git a/fs/fcntl.c b/fs/fcntl.c
8b6a4947 93index 448a111..f51c2cf 100644
febd17d6
JR
94--- a/fs/fcntl.c
95+++ b/fs/fcntl.c
ffa93bbd 96@@ -31,7 +31,7 @@
febd17d6
JR
97
98 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
99
100-static int setfl(int fd, struct file * filp, unsigned long arg)
101+int setfl(int fd, struct file * filp, unsigned long arg)
102 {
103 struct inode * inode = file_inode(filp);
104 int error = 0;
ffa93bbd 105@@ -62,6 +62,8 @@ static int setfl(int fd, struct file * filp, unsigned long arg)
febd17d6
JR
106
107 if (filp->f_op->check_flags)
108 error = filp->f_op->check_flags(arg);
109+ if (!error && filp->f_op->setfl)
110+ error = filp->f_op->setfl(filp, arg);
111 if (error)
112 return error;
113
5afbbe0d 114diff --git a/fs/inode.c b/fs/inode.c
8b6a4947 115index d1e35b5..f7800d6 100644
5afbbe0d
AM
116--- a/fs/inode.c
117+++ b/fs/inode.c
8b6a4947 118@@ -1655,7 +1655,7 @@ EXPORT_SYMBOL(generic_update_time);
5afbbe0d
AM
119 * This does the actual work of updating an inodes time or version. Must have
120 * had called mnt_want_write() before calling this.
121 */
122-static int update_time(struct inode *inode, struct timespec *time, int flags)
123+int update_time(struct inode *inode, struct timespec *time, int flags)
124 {
125 int (*update_time)(struct inode *, struct timespec *, int);
126
8b6a4947
AM
127diff --git a/fs/namespace.c b/fs/namespace.c
128index d18deb4..e5a4a7f 100644
129--- a/fs/namespace.c
130+++ b/fs/namespace.c
131@@ -846,6 +846,12 @@ static inline int check_mnt(struct mount *mnt)
132 return mnt->mnt_ns == current->nsproxy->mnt_ns;
133 }
134
135+/* for aufs, CONFIG_AUFS_BR_FUSE */
136+int is_current_mnt_ns(struct vfsmount *mnt)
137+{
138+ return check_mnt(real_mount(mnt));
139+}
140+
141 /*
142 * vfsmount lock must be held for write
143 */
5527c038 144diff --git a/fs/read_write.c b/fs/read_write.c
8b6a4947 145index f0d4b16..6aa8c7a 100644
5527c038
JR
146--- a/fs/read_write.c
147+++ b/fs/read_write.c
8b6a4947
AM
148@@ -483,6 +483,28 @@ ssize_t __vfs_write(struct file *file, const char __user *p, size_t count,
149 return -EINVAL;
5527c038 150 }
5527c038
JR
151
152+vfs_readf_t vfs_readf(struct file *file)
153+{
154+ const struct file_operations *fop = file->f_op;
155+
156+ if (fop->read)
157+ return fop->read;
158+ if (fop->read_iter)
159+ return new_sync_read;
160+ return ERR_PTR(-ENOSYS);
161+}
162+
163+vfs_writef_t vfs_writef(struct file *file)
164+{
165+ const struct file_operations *fop = file->f_op;
166+
167+ if (fop->write)
168+ return fop->write;
169+ if (fop->write_iter)
170+ return new_sync_write;
171+ return ERR_PTR(-ENOSYS);
172+}
173+
8b6a4947 174 ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
5527c038
JR
175 {
176 mm_segment_t old_fs;
7f207e10 177diff --git a/fs/splice.c b/fs/splice.c
8b6a4947 178index f3084cc..eb888c6 100644
7f207e10
AM
179--- a/fs/splice.c
180+++ b/fs/splice.c
8b6a4947 181@@ -837,8 +837,8 @@ EXPORT_SYMBOL(generic_splice_sendpage);
7f207e10
AM
182 /*
183 * Attempt to initiate a splice from pipe to file.
184 */
185-static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
186- loff_t *ppos, size_t len, unsigned int flags)
187+long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
188+ loff_t *ppos, size_t len, unsigned int flags)
189 {
190 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
191 loff_t *, size_t, unsigned int);
8b6a4947 192@@ -854,9 +854,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
7f207e10
AM
193 /*
194 * Attempt to initiate a splice from a file to a pipe.
195 */
196-static long do_splice_to(struct file *in, loff_t *ppos,
197- struct pipe_inode_info *pipe, size_t len,
198- unsigned int flags)
199+long do_splice_to(struct file *in, loff_t *ppos,
200+ struct pipe_inode_info *pipe, size_t len,
201+ unsigned int flags)
202 {
203 ssize_t (*splice_read)(struct file *, loff_t *,
204 struct pipe_inode_info *, size_t, unsigned int);
a2654f78 205diff --git a/fs/sync.c b/fs/sync.c
8b6a4947 206index a576aa2..eb61780 100644
a2654f78
AM
207--- a/fs/sync.c
208+++ b/fs/sync.c
209@@ -27,7 +27,7 @@
210 * wait == 1 case since in that case write_inode() functions do
211 * sync_dirty_buffer() and thus effectively write one block at a time.
212 */
213-static int __sync_filesystem(struct super_block *sb, int wait)
214+int __sync_filesystem(struct super_block *sb, int wait)
215 {
216 if (wait)
217 sync_inodes_sb(sb);
b912730e 218diff --git a/include/linux/file.h b/include/linux/file.h
a2654f78 219index 61eb82c..e700888 100644
b912730e
AM
220--- a/include/linux/file.h
221+++ b/include/linux/file.h
a2654f78 222@@ -19,6 +19,7 @@ struct dentry;
b912730e 223 struct path;
a2654f78 224 extern struct file *alloc_file(const struct path *, fmode_t mode,
b912730e
AM
225 const struct file_operations *fop);
226+extern struct file *get_empty_filp(void);
227
228 static inline void fput_light(struct file *file, int fput_needed)
229 {
5527c038 230diff --git a/include/linux/fs.h b/include/linux/fs.h
8b6a4947 231index 13dab19..8ab6566 100644
5527c038
JR
232--- a/include/linux/fs.h
233+++ b/include/linux/fs.h
8b6a4947 234@@ -1264,6 +1264,7 @@ extern void fasync_free(struct fasync_struct *);
febd17d6
JR
235 /* can be called from interrupts */
236 extern void kill_fasync(struct fasync_struct **, int, int);
237
238+extern int setfl(int fd, struct file * filp, unsigned long arg);
239 extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
3c1bdaff 240 extern int f_setown(struct file *filp, unsigned long arg, int force);
febd17d6 241 extern void f_delown(struct file *filp);
8b6a4947 242@@ -1710,6 +1711,7 @@ struct file_operations {
febd17d6
JR
243 ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
244 unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
245 int (*check_flags)(int);
246+ int (*setfl)(struct file *, unsigned long);
247 int (*flock) (struct file *, int, struct file_lock *);
248 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
249 ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
8b6a4947 250@@ -1780,6 +1782,12 @@ ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
5527c038
JR
251 struct iovec *fast_pointer,
252 struct iovec **ret_pointer);
253
254+typedef ssize_t (*vfs_readf_t)(struct file *, char __user *, size_t, loff_t *);
255+typedef ssize_t (*vfs_writef_t)(struct file *, const char __user *, size_t,
256+ loff_t *);
257+vfs_readf_t vfs_readf(struct file *file);
258+vfs_writef_t vfs_writef(struct file *file);
259+
260 extern ssize_t __vfs_read(struct file *, char __user *, size_t, loff_t *);
5527c038 261 extern ssize_t vfs_read(struct file *, char __user *, size_t, loff_t *);
8b6a4947
AM
262 extern ssize_t vfs_write(struct file *, const char __user *, size_t, loff_t *);
263@@ -2182,6 +2190,7 @@ extern int current_umask(void);
5afbbe0d
AM
264 extern void ihold(struct inode * inode);
265 extern void iput(struct inode *);
266 extern int generic_update_time(struct inode *, struct timespec *, int);
267+extern int update_time(struct inode *, struct timespec *, int);
268
269 /* /sys/fs */
270 extern struct kobject *fs_kobj;
8b6a4947 271@@ -2462,6 +2471,7 @@ static inline bool sb_is_blkdev_sb(struct super_block *sb)
a2654f78
AM
272 return false;
273 }
274 #endif
275+extern int __sync_filesystem(struct super_block *, int);
276 extern int sync_filesystem(struct super_block *);
277 extern const struct file_operations def_blk_fops;
278 extern const struct file_operations def_chr_fops;
8b6a4947
AM
279diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
280index bfa8e0b..728d810 100644
281--- a/include/linux/lockdep.h
282+++ b/include/linux/lockdep.h
283@@ -405,6 +405,8 @@ static inline int lockdep_match_key(struct lockdep_map *lock,
284 return lock->key == key;
285 }
286
287+struct lock_class *lockdep_hlock_class(struct held_lock *hlock);
288+
289 /*
290 * Acquire a lock.
291 *
292@@ -529,6 +531,7 @@ struct lock_class_key { };
293
294 #define lockdep_depth(tsk) (0)
295
296+#define lockdep_is_held(lock) (1)
297 #define lockdep_is_held_type(l, r) (1)
298
299 #define lockdep_assert_held(l) do { (void)(l); } while (0)
300diff --git a/include/linux/mnt_namespace.h b/include/linux/mnt_namespace.h
301index 12b2ab5..8b810d1 100644
302--- a/include/linux/mnt_namespace.h
303+++ b/include/linux/mnt_namespace.h
304@@ -5,11 +5,14 @@
305 struct mnt_namespace;
306 struct fs_struct;
307 struct user_namespace;
308+struct vfsmount;
309
310 extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
311 struct user_namespace *, struct fs_struct *);
312 extern void put_mnt_ns(struct mnt_namespace *ns);
313
314+extern int is_current_mnt_ns(struct vfsmount *mnt);
315+
316 extern const struct file_operations proc_mounts_operations;
317 extern const struct file_operations proc_mountinfo_operations;
318 extern const struct file_operations proc_mountstats_operations;
1e00d052 319diff --git a/include/linux/splice.h b/include/linux/splice.h
ffa93bbd 320index db42746..12f3a5a 100644
1e00d052
AM
321--- a/include/linux/splice.h
322+++ b/include/linux/splice.h
ffa93bbd 323@@ -86,4 +86,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
4b3da204
AM
324
325 extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
106341ce 326 extern const struct pipe_buf_operations default_pipe_buf_ops;
1e00d052
AM
327+
328+extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
329+ loff_t *ppos, size_t len, unsigned int flags);
330+extern long do_splice_to(struct file *in, loff_t *ppos,
331+ struct pipe_inode_info *pipe, size_t len,
332+ unsigned int flags);
333 #endif
8b6a4947
AM
334diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
335index e36e652..bc97a97 100644
336--- a/kernel/locking/lockdep.c
337+++ b/kernel/locking/lockdep.c
338@@ -144,7 +144,7 @@ static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
339 unsigned long nr_lock_classes;
340 static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
341
342-static inline struct lock_class *hlock_class(struct held_lock *hlock)
343+inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
344 {
345 if (!hlock->class_idx) {
346 /*
347@@ -155,6 +155,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
348 }
349 return lock_classes + hlock->class_idx - 1;
350 }
351+#define hlock_class(hlock) lockdep_hlock_class(hlock)
352
353 #ifdef CONFIG_LOCK_STAT
354 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
ffa93bbd 355aufs4.x-rcN mmap patch
fb47a38f 356
c1595e42 357diff --git a/fs/proc/base.c b/fs/proc/base.c
8b6a4947 358index ad3b076..ad4a50d 100644
c1595e42
JR
359--- a/fs/proc/base.c
360+++ b/fs/proc/base.c
8b6a4947 361@@ -1987,7 +1987,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
c1595e42
JR
362 down_read(&mm->mmap_sem);
363 vma = find_exact_vma(mm, vm_start, vm_end);
364 if (vma && vma->vm_file) {
365- *path = vma->vm_file->f_path;
366+ *path = vma_pr_or_file(vma)->f_path;
367 path_get(path);
368 rc = 0;
369 }
fb47a38f 370diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
a2654f78 371index 7563437..7c0dc0f 100644
fb47a38f
JR
372--- a/fs/proc/nommu.c
373+++ b/fs/proc/nommu.c
076b876e 374@@ -45,7 +45,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
fb47a38f
JR
375 file = region->vm_file;
376
377 if (file) {
378- struct inode *inode = file_inode(region->vm_file);
379+ struct inode *inode;
076b876e 380+
fb47a38f
JR
381+ file = vmr_pr_or_file(region);
382+ inode = file_inode(file);
383 dev = inode->i_sb->s_dev;
384 ino = inode->i_ino;
385 }
386diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
8b6a4947 387index 5589b4b..f60aea2 100644
fb47a38f
JR
388--- a/fs/proc/task_mmu.c
389+++ b/fs/proc/task_mmu.c
8b6a4947 390@@ -309,7 +309,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
fb47a38f
JR
391 const char *name = NULL;
392
393 if (file) {
394- struct inode *inode = file_inode(vma->vm_file);
395+ struct inode *inode;
076b876e 396+
fb47a38f
JR
397+ file = vma_pr_or_file(vma);
398+ inode = file_inode(file);
399 dev = inode->i_sb->s_dev;
400 ino = inode->i_ino;
401 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
8b6a4947 402@@ -1734,7 +1737,7 @@ static int show_numa_map(struct seq_file *m, void *v, int is_pid)
076b876e
AM
403 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
404 struct vm_area_struct *vma = v;
405 struct numa_maps *md = &numa_priv->md;
406- struct file *file = vma->vm_file;
407+ struct file *file = vma_pr_or_file(vma);
076b876e 408 struct mm_struct *mm = vma->vm_mm;
7e9cd9fe
AM
409 struct mm_walk walk = {
410 .hugetlb_entry = gather_hugetlb_stats,
fb47a38f 411diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
8b6a4947 412index b00b7660..93e8a86 100644
fb47a38f
JR
413--- a/fs/proc/task_nommu.c
414+++ b/fs/proc/task_nommu.c
8b6a4947 415@@ -155,7 +155,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma,
fb47a38f
JR
416 file = vma->vm_file;
417
418 if (file) {
419- struct inode *inode = file_inode(vma->vm_file);
420+ struct inode *inode;
076b876e 421+
b912730e 422+ file = vma_pr_or_file(vma);
fb47a38f
JR
423+ inode = file_inode(file);
424 dev = inode->i_sb->s_dev;
425 ino = inode->i_ino;
426 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
427diff --git a/include/linux/mm.h b/include/linux/mm.h
8b6a4947 428index 065d99d..04486c3 100644
fb47a38f
JR
429--- a/include/linux/mm.h
430+++ b/include/linux/mm.h
8b6a4947 431@@ -1348,6 +1348,28 @@ static inline int fixup_user_fault(struct task_struct *tsk,
fb47a38f
JR
432 }
433 #endif
434
076b876e
AM
435+extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
436+extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
f2c43d5f 437+ int);
076b876e
AM
438+extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
439+extern void vma_do_fput(struct vm_area_struct *, const char[], int);
fb47a38f 440+
f2c43d5f
AM
441+#define vma_file_update_time(vma) vma_do_file_update_time(vma, __func__, \
442+ __LINE__)
443+#define vma_pr_or_file(vma) vma_do_pr_or_file(vma, __func__, \
444+ __LINE__)
445+#define vma_get_file(vma) vma_do_get_file(vma, __func__, __LINE__)
446+#define vma_fput(vma) vma_do_fput(vma, __func__, __LINE__)
b912730e
AM
447+
448+#ifndef CONFIG_MMU
076b876e
AM
449+extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
450+extern void vmr_do_fput(struct vm_region *, const char[], int);
451+
f2c43d5f
AM
452+#define vmr_pr_or_file(region) vmr_do_pr_or_file(region, __func__, \
453+ __LINE__)
454+#define vmr_fput(region) vmr_do_fput(region, __func__, __LINE__)
b912730e 455+#endif /* !CONFIG_MMU */
fb47a38f 456+
106341ce
AM
457 extern int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len,
458 unsigned int gup_flags);
fb47a38f 459 extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
fb47a38f 460diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
8b6a4947 461index 1861ea8..d85a914 100644
fb47a38f
JR
462--- a/include/linux/mm_types.h
463+++ b/include/linux/mm_types.h
8b6a4947 464@@ -260,6 +260,7 @@ struct vm_region {
fb47a38f
JR
465 unsigned long vm_top; /* region allocated to here */
466 unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */
467 struct file *vm_file; /* the backing file or NULL */
468+ struct file *vm_prfile; /* the virtual backing file or NULL */
469
470 int vm_usage; /* region usage count (access under nommu_region_sem) */
471 bool vm_icache_flushed : 1; /* true if the icache has been flushed for
8b6a4947 472@@ -334,6 +335,7 @@ struct vm_area_struct {
fb47a38f 473 unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
5afbbe0d 474 units */
fb47a38f
JR
475 struct file * vm_file; /* File we map to (can be NULL). */
476+ struct file *vm_prfile; /* shadow of vm_file */
477 void * vm_private_data; /* was vm_pte (shared mem) */
478
8b6a4947 479 atomic_long_t swap_readahead_info;
fb47a38f 480diff --git a/kernel/fork.c b/kernel/fork.c
8b6a4947 481index 07cc743..b1d2b43 100644
fb47a38f
JR
482--- a/kernel/fork.c
483+++ b/kernel/fork.c
8b6a4947 484@@ -676,7 +676,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
fb47a38f
JR
485 struct inode *inode = file_inode(file);
486 struct address_space *mapping = file->f_mapping;
487
488- get_file(file);
489+ vma_get_file(tmp);
490 if (tmp->vm_flags & VM_DENYWRITE)
491 atomic_dec(&inode->i_writecount);
2000de60 492 i_mmap_lock_write(mapping);
076b876e 493diff --git a/mm/Makefile b/mm/Makefile
8b6a4947 494index e3ac3ae..745b26c 100644
076b876e
AM
495--- a/mm/Makefile
496+++ b/mm/Makefile
521ced18 497@@ -39,7 +39,7 @@ obj-y := filemap.o mempool.o oom_kill.o \
076b876e 498 mm_init.o mmu_context.o percpu.o slab_common.o \
521ced18 499 compaction.o vmacache.o swap_slots.o \
076b876e 500 interval_tree.o list_lru.o workingset.o \
7e9cd9fe
AM
501- debug.o $(mmu-y)
502+ prfile.o debug.o $(mmu-y)
076b876e
AM
503
504 obj-y += init-mm.o
505
fb47a38f 506diff --git a/mm/filemap.c b/mm/filemap.c
8b6a4947 507index 594d73f..7183aef 100644
fb47a38f
JR
508--- a/mm/filemap.c
509+++ b/mm/filemap.c
8b6a4947 510@@ -2590,7 +2590,7 @@ int filemap_page_mkwrite(struct vm_fault *vmf)
fb47a38f
JR
511 int ret = VM_FAULT_LOCKED;
512
513 sb_start_pagefault(inode->i_sb);
521ced18
JR
514- file_update_time(vmf->vma->vm_file);
515+ vma_file_update_time(vmf->vma);
fb47a38f
JR
516 lock_page(page);
517 if (page->mapping != inode->i_mapping) {
518 unlock_page(page);
fb47a38f 519diff --git a/mm/mmap.c b/mm/mmap.c
8b6a4947 520index 680506f..081406a 100644
fb47a38f
JR
521--- a/mm/mmap.c
522+++ b/mm/mmap.c
8b6a4947 523@@ -171,7 +171,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
fb47a38f
JR
524 if (vma->vm_ops && vma->vm_ops->close)
525 vma->vm_ops->close(vma);
526 if (vma->vm_file)
527- fput(vma->vm_file);
528+ vma_fput(vma);
529 mpol_put(vma_policy(vma));
530 kmem_cache_free(vm_area_cachep, vma);
531 return next;
8b6a4947 532@@ -896,7 +896,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
fb47a38f
JR
533 if (remove_next) {
534 if (file) {
535 uprobe_munmap(next, next->vm_start, next->vm_end);
536- fput(file);
537+ vma_fput(vma);
538 }
539 if (next->anon_vma)
540 anon_vma_merge(vma, next);
8b6a4947 541@@ -1746,8 +1746,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
35939ee7
JR
542 return addr;
543
fb47a38f 544 unmap_and_free_vma:
fb47a38f
JR
545+ vma_fput(vma);
546 vma->vm_file = NULL;
547- fput(file);
548
549 /* Undo any partial mapping done by a device driver. */
550 unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
8b6a4947 551@@ -2569,7 +2569,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
fb47a38f
JR
552 goto out_free_mpol;
553
554 if (new->vm_file)
555- get_file(new->vm_file);
556+ vma_get_file(new);
557
558 if (new->vm_ops && new->vm_ops->open)
559 new->vm_ops->open(new);
8b6a4947 560@@ -2588,7 +2588,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
fb47a38f
JR
561 if (new->vm_ops && new->vm_ops->close)
562 new->vm_ops->close(new);
563 if (new->vm_file)
564- fput(new->vm_file);
565+ vma_fput(new);
566 unlink_anon_vmas(new);
567 out_free_mpol:
568 mpol_put(vma_policy(new));
8b6a4947 569@@ -2750,7 +2750,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
7e9cd9fe
AM
570 struct vm_area_struct *vma;
571 unsigned long populate = 0;
572 unsigned long ret = -EINVAL;
573- struct file *file;
5afbbe0d 574+ struct file *file, *prfile;
7e9cd9fe 575
5afbbe0d
AM
576 pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.txt.\n",
577 current->comm, current->pid);
8b6a4947 578@@ -2825,10 +2825,27 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
febd17d6 579 }
7e9cd9fe
AM
580 }
581
582- file = get_file(vma->vm_file);
583+ vma_get_file(vma);
5afbbe0d
AM
584+ file = vma->vm_file;
585+ prfile = vma->vm_prfile;
7e9cd9fe 586 ret = do_mmap_pgoff(vma->vm_file, start, size,
521ced18 587 prot, flags, pgoff, &populate, NULL);
5afbbe0d
AM
588+ if (!IS_ERR_VALUE(ret) && file && prfile) {
589+ struct vm_area_struct *new_vma;
590+
591+ new_vma = find_vma(mm, ret);
592+ if (!new_vma->vm_prfile)
593+ new_vma->vm_prfile = prfile;
594+ if (new_vma != vma)
595+ get_file(prfile);
596+ }
597+ /*
598+ * two fput()s instead of vma_fput(vma),
599+ * coz vma may not be available anymore.
600+ */
601 fput(file);
602+ if (prfile)
603+ fput(prfile);
7e9cd9fe
AM
604 out:
605 up_write(&mm->mmap_sem);
606 if (populate)
8b6a4947 607@@ -3136,7 +3153,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
79b8bda9
AM
608 if (anon_vma_clone(new_vma, vma))
609 goto out_free_mempol;
610 if (new_vma->vm_file)
611- get_file(new_vma->vm_file);
612+ vma_get_file(new_vma);
613 if (new_vma->vm_ops && new_vma->vm_ops->open)
614 new_vma->vm_ops->open(new_vma);
615 vma_link(mm, new_vma, prev, rb_link, rb_parent);
fb47a38f 616diff --git a/mm/nommu.c b/mm/nommu.c
8b6a4947 617index 17c00d9..4bcdf94 100644
fb47a38f
JR
618--- a/mm/nommu.c
619+++ b/mm/nommu.c
ffa93bbd 620@@ -641,7 +641,7 @@ static void __put_nommu_region(struct vm_region *region)
fb47a38f
JR
621 up_write(&nommu_region_sem);
622
623 if (region->vm_file)
624- fput(region->vm_file);
625+ vmr_fput(region);
626
627 /* IO memory and memory shared directly out of the pagecache
628 * from ramfs/tmpfs mustn't be released here */
ffa93bbd 629@@ -799,7 +799,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
fb47a38f
JR
630 if (vma->vm_ops && vma->vm_ops->close)
631 vma->vm_ops->close(vma);
632 if (vma->vm_file)
633- fput(vma->vm_file);
634+ vma_fput(vma);
635 put_nommu_region(vma->vm_region);
636 kmem_cache_free(vm_area_cachep, vma);
637 }
8b6a4947 638@@ -1321,7 +1321,7 @@ unsigned long do_mmap(struct file *file,
fb47a38f
JR
639 goto error_just_free;
640 }
641 }
642- fput(region->vm_file);
643+ vmr_fput(region);
644 kmem_cache_free(vm_region_jar, region);
645 region = pregion;
646 result = start;
8b6a4947 647@@ -1396,10 +1396,10 @@ unsigned long do_mmap(struct file *file,
fb47a38f
JR
648 up_write(&nommu_region_sem);
649 error:
650 if (region->vm_file)
651- fput(region->vm_file);
652+ vmr_fput(region);
653 kmem_cache_free(vm_region_jar, region);
654 if (vma->vm_file)
655- fput(vma->vm_file);
656+ vma_fput(vma);
657 kmem_cache_free(vm_area_cachep, vma);
fb47a38f 658 return ret;
c2c0f25c 659
076b876e
AM
660diff --git a/mm/prfile.c b/mm/prfile.c
661new file mode 100644
1c60b727 662index 0000000..1ef053b
076b876e
AM
663--- /dev/null
664+++ b/mm/prfile.c
1c60b727 665@@ -0,0 +1,85 @@
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+ *
1c60b727 673+ * Copyright (c) 2014-2017 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 */
ffa93bbd 751aufs4.x-rcN standalone patch
7f207e10 752
c1595e42 753diff --git a/fs/dcache.c b/fs/dcache.c
3c1bdaff 754index e3719a5..3203470 100644
c1595e42
JR
755--- a/fs/dcache.c
756+++ b/fs/dcache.c
3c1bdaff 757@@ -1305,6 +1305,7 @@ void d_walk(struct dentry *parent, void *data,
c1595e42
JR
758 seq = 1;
759 goto again;
760 }
febd17d6 761+EXPORT_SYMBOL_GPL(d_walk);
c1595e42 762
a2654f78
AM
763 struct check_mount {
764 struct vfsmount *mnt;
3c1bdaff 765@@ -2894,6 +2895,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
f2c43d5f
AM
766
767 write_sequnlock(&rename_lock);
768 }
769+EXPORT_SYMBOL_GPL(d_exchange);
770
771 /**
772 * d_ancestor - search for an ancestor
79b8bda9 773diff --git a/fs/exec.c b/fs/exec.c
8b6a4947 774index 3e14ba2..6818b01 100644
79b8bda9
AM
775--- a/fs/exec.c
776+++ b/fs/exec.c
521ced18 777@@ -109,6 +109,7 @@ bool path_noexec(const struct path *path)
79b8bda9
AM
778 return (path->mnt->mnt_flags & MNT_NOEXEC) ||
779 (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
780 }
febd17d6 781+EXPORT_SYMBOL_GPL(path_noexec);
79b8bda9
AM
782
783 #ifdef CONFIG_USELIB
784 /*
febd17d6 785diff --git a/fs/fcntl.c b/fs/fcntl.c
8b6a4947 786index f51c2cf..58bf222 100644
febd17d6
JR
787--- a/fs/fcntl.c
788+++ b/fs/fcntl.c
ffa93bbd 789@@ -84,6 +84,7 @@ int setfl(int fd, struct file * filp, unsigned long arg)
febd17d6
JR
790 out:
791 return error;
792 }
793+EXPORT_SYMBOL_GPL(setfl);
794
795 static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
796 int force)
b912730e 797diff --git a/fs/file_table.c b/fs/file_table.c
8b6a4947 798index 61517f5..c6bab39c 100644
b912730e
AM
799--- a/fs/file_table.c
800+++ b/fs/file_table.c
521ced18 801@@ -148,6 +148,7 @@ struct file *get_empty_filp(void)
b912730e
AM
802 }
803 return ERR_PTR(-ENFILE);
804 }
febd17d6 805+EXPORT_SYMBOL_GPL(get_empty_filp);
b912730e
AM
806
807 /**
808 * alloc_file - allocate and initialize a 'struct file'
8b6a4947 809@@ -258,6 +259,7 @@ void flush_delayed_fput(void)
8cdd5066
JR
810 {
811 delayed_fput(NULL);
812 }
febd17d6 813+EXPORT_SYMBOL_GPL(flush_delayed_fput);
8cdd5066
JR
814
815 static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput);
816
8b6a4947 817@@ -300,6 +302,7 @@ void __fput_sync(struct file *file)
8cdd5066
JR
818 }
819
820 EXPORT_SYMBOL(fput);
febd17d6 821+EXPORT_SYMBOL_GPL(__fput_sync);
8cdd5066
JR
822
823 void put_filp(struct file *file)
824 {
8b6a4947 825@@ -308,6 +311,7 @@ void put_filp(struct file *file)
b912730e
AM
826 file_free(file);
827 }
828 }
febd17d6 829+EXPORT_SYMBOL_GPL(put_filp);
b912730e 830
79b8bda9 831 void __init files_init(void)
8b6a4947 832 {
5afbbe0d 833diff --git a/fs/inode.c b/fs/inode.c
8b6a4947 834index f7800d6..f31a6c7 100644
5afbbe0d
AM
835--- a/fs/inode.c
836+++ b/fs/inode.c
8b6a4947 837@@ -1664,6 +1664,7 @@ int update_time(struct inode *inode, struct timespec *time, int flags)
5afbbe0d
AM
838
839 return update_time(inode, time, flags);
840 }
841+EXPORT_SYMBOL_GPL(update_time);
842
843 /**
844 * touch_atime - update the access time
7f207e10 845diff --git a/fs/namespace.c b/fs/namespace.c
8b6a4947 846index e5a4a7f..6d0c376 100644
7f207e10
AM
847--- a/fs/namespace.c
848+++ b/fs/namespace.c
8b6a4947 849@@ -517,6 +517,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
c06a8ce3
AM
850 mnt_dec_writers(real_mount(mnt));
851 preempt_enable();
852 }
853+EXPORT_SYMBOL_GPL(__mnt_drop_write);
854
855 /**
856 * mnt_drop_write - give up write access to a mount
8b6a4947
AM
857@@ -851,6 +852,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
858 {
859 return check_mnt(real_mount(mnt));
860 }
861+EXPORT_SYMBOL_GPL(is_current_mnt_ns);
862
863 /*
864 * vfsmount lock must be held for write
865@@ -1887,6 +1889,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
7f207e10
AM
866 }
867 return 0;
868 }
febd17d6 869+EXPORT_SYMBOL_GPL(iterate_mounts);
7f207e10 870
7eafdf33 871 static void cleanup_group_ids(struct mount *mnt, struct mount *end)
7f207e10
AM
872 {
873diff --git a/fs/notify/group.c b/fs/notify/group.c
ffa93bbd 874index 3235753..14a2d48 100644
7f207e10
AM
875--- a/fs/notify/group.c
876+++ b/fs/notify/group.c
877@@ -22,6 +22,7 @@
878 #include <linux/srcu.h>
879 #include <linux/rculist.h>
880 #include <linux/wait.h>
881+#include <linux/module.h>
882
883 #include <linux/fsnotify_backend.h>
884 #include "fsnotify.h"
ffa93bbd 885@@ -109,6 +110,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
1716fcea
AM
886 {
887 atomic_inc(&group->refcnt);
888 }
febd17d6 889+EXPORT_SYMBOL_GPL(fsnotify_get_group);
1716fcea
AM
890
891 /*
892 * Drop a reference to a group. Free it if it's through.
ffa93bbd 893@@ -118,6 +120,7 @@ void fsnotify_put_group(struct fsnotify_group *group)
7f207e10 894 if (atomic_dec_and_test(&group->refcnt))
1716fcea 895 fsnotify_final_destroy_group(group);
7f207e10 896 }
febd17d6 897+EXPORT_SYMBOL_GPL(fsnotify_put_group);
7f207e10
AM
898
899 /*
900 * Create a new fsnotify_group and hold a reference for the group returned.
ffa93bbd 901@@ -147,6 +150,7 @@ struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops)
7f207e10
AM
902
903 return group;
904 }
febd17d6 905+EXPORT_SYMBOL_GPL(fsnotify_alloc_group);
1716fcea
AM
906
907 int fsnotify_fasync(int fd, struct file *file, int on)
908 {
7f207e10 909diff --git a/fs/notify/mark.c b/fs/notify/mark.c
ffa93bbd 910index 9991f88..117042c 100644
7f207e10
AM
911--- a/fs/notify/mark.c
912+++ b/fs/notify/mark.c
ffa93bbd
AM
913@@ -118,6 +118,7 @@ static bool fsnotify_get_mark_safe(struct fsnotify_mark *mark)
914 {
915 return atomic_inc_not_zero(&mark->refcnt);
7f207e10 916 }
febd17d6 917+EXPORT_SYMBOL_GPL(fsnotify_put_mark);
7f207e10 918
ffa93bbd
AM
919 static void __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
920 {
921@@ -395,6 +396,7 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark,
1716fcea 922 mutex_unlock(&group->mark_mutex);
79b8bda9 923 fsnotify_free_mark(mark);
7f207e10 924 }
febd17d6 925+EXPORT_SYMBOL_GPL(fsnotify_destroy_mark);
7f207e10 926
ffa93bbd
AM
927 /*
928 * Sorting function for lists of fsnotify marks.
929@@ -607,6 +609,7 @@ int fsnotify_add_mark_locked(struct fsnotify_mark *mark, struct inode *inode,
930 fsnotify_put_mark(mark);
7f207e10
AM
931 return ret;
932 }
febd17d6 933+EXPORT_SYMBOL_GPL(fsnotify_add_mark);
7f207e10 934
ffa93bbd
AM
935 int fsnotify_add_mark(struct fsnotify_mark *mark, struct inode *inode,
936 struct vfsmount *mnt, int allow_dups)
937@@ -742,6 +745,7 @@ void fsnotify_init_mark(struct fsnotify_mark *mark,
938 fsnotify_get_group(group);
939 mark->group = group;
7f207e10 940 }
febd17d6 941+EXPORT_SYMBOL_GPL(fsnotify_init_mark);
7f207e10 942
5afbbe0d
AM
943 /*
944 * Destroy all marks in destroy_list, waits for SRCU period to finish before
7f207e10 945diff --git a/fs/open.c b/fs/open.c
8b6a4947 946index 7ea1184..6e2e241 100644
7f207e10
AM
947--- a/fs/open.c
948+++ b/fs/open.c
c2c0f25c 949@@ -64,6 +64,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
febd17d6 950 inode_unlock(dentry->d_inode);
7f207e10
AM
951 return ret;
952 }
febd17d6 953+EXPORT_SYMBOL_GPL(do_truncate);
7f207e10 954
5afbbe0d 955 long vfs_truncate(const struct path *path, loff_t length)
7f207e10 956 {
ffa93bbd 957@@ -691,6 +692,7 @@ int open_check_o_direct(struct file *f)
b912730e
AM
958 }
959 return 0;
960 }
febd17d6 961+EXPORT_SYMBOL_GPL(open_check_o_direct);
b912730e
AM
962
963 static int do_dentry_open(struct file *f,
c2c0f25c 964 struct inode *inode,
5527c038 965diff --git a/fs/read_write.c b/fs/read_write.c
8b6a4947 966index 6aa8c7a..b5d392e 100644
5527c038
JR
967--- a/fs/read_write.c
968+++ b/fs/read_write.c
8b6a4947
AM
969@@ -453,6 +453,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
970
971 return ret;
972 }
973+EXPORT_SYMBOL_GPL(vfs_read);
974
975 static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
976 {
977@@ -493,6 +494,7 @@ vfs_readf_t vfs_readf(struct file *file)
5527c038
JR
978 return new_sync_read;
979 return ERR_PTR(-ENOSYS);
980 }
febd17d6 981+EXPORT_SYMBOL_GPL(vfs_readf);
5527c038
JR
982
983 vfs_writef_t vfs_writef(struct file *file)
984 {
8b6a4947 985@@ -504,6 +506,7 @@ vfs_writef_t vfs_writef(struct file *file)
5527c038
JR
986 return new_sync_write;
987 return ERR_PTR(-ENOSYS);
988 }
febd17d6 989+EXPORT_SYMBOL_GPL(vfs_writef);
5527c038 990
8b6a4947
AM
991 ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
992 {
993@@ -573,6 +576,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
994
995 return ret;
996 }
997+EXPORT_SYMBOL_GPL(vfs_write);
998
999 static inline loff_t file_pos_read(struct file *file)
5527c038 1000 {
7f207e10 1001diff --git a/fs/splice.c b/fs/splice.c
8b6a4947 1002index eb888c6..7ab89d2 100644
7f207e10
AM
1003--- a/fs/splice.c
1004+++ b/fs/splice.c
8b6a4947 1005@@ -850,6 +850,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
392086de
AM
1006
1007 return splice_write(pipe, out, ppos, len, flags);
7f207e10 1008 }
febd17d6 1009+EXPORT_SYMBOL_GPL(do_splice_from);
7f207e10
AM
1010
1011 /*
1012 * Attempt to initiate a splice from a file to a pipe.
8b6a4947 1013@@ -879,6 +880,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
7f207e10
AM
1014
1015 return splice_read(in, ppos, pipe, len, flags);
1016 }
febd17d6 1017+EXPORT_SYMBOL_GPL(do_splice_to);
7f207e10
AM
1018
1019 /**
1020 * splice_direct_to_actor - splices data directly between two non-pipes
a2654f78 1021diff --git a/fs/sync.c b/fs/sync.c
8b6a4947 1022index eb61780..32c5a05 100644
a2654f78
AM
1023--- a/fs/sync.c
1024+++ b/fs/sync.c
1025@@ -38,6 +38,7 @@ int __sync_filesystem(struct super_block *sb, int wait)
1026 sb->s_op->sync_fs(sb, wait);
1027 return __sync_blockdev(sb->s_bdev, wait);
1028 }
1029+EXPORT_SYMBOL_GPL(__sync_filesystem);
1030
1031 /*
1032 * Write out and wait upon all dirty data associated with this
c1595e42 1033diff --git a/fs/xattr.c b/fs/xattr.c
8b6a4947 1034index 61cd28b..35570cd 100644
c1595e42
JR
1035--- a/fs/xattr.c
1036+++ b/fs/xattr.c
8b6a4947 1037@@ -297,6 +297,7 @@ vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
c1595e42
JR
1038 *xattr_value = value;
1039 return error;
1040 }
febd17d6 1041+EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
c1595e42 1042
febd17d6 1043 ssize_t
f2c43d5f 1044 __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
8b6a4947
AM
1045diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
1046index bc97a97..895a1ba 100644
1047--- a/kernel/locking/lockdep.c
1048+++ b/kernel/locking/lockdep.c
1049@@ -155,6 +155,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
1050 }
1051 return lock_classes + hlock->class_idx - 1;
1052 }
1053+EXPORT_SYMBOL_GPL(lockdep_hlock_class);
1054 #define hlock_class(hlock) lockdep_hlock_class(hlock)
1055
1056 #ifdef CONFIG_LOCK_STAT
8cdd5066 1057diff --git a/kernel/task_work.c b/kernel/task_work.c
8b6a4947 1058index 836a72a..aa00d49 100644
8cdd5066
JR
1059--- a/kernel/task_work.c
1060+++ b/kernel/task_work.c
8b6a4947 1061@@ -115,3 +115,4 @@ void task_work_run(void)
8cdd5066
JR
1062 } while (work);
1063 }
1064 }
febd17d6 1065+EXPORT_SYMBOL_GPL(task_work_run);
7f207e10 1066diff --git a/security/commoncap.c b/security/commoncap.c
8b6a4947 1067index fc46f5b..90543ef 100644
7f207e10
AM
1068--- a/security/commoncap.c
1069+++ b/security/commoncap.c
8b6a4947 1070@@ -1270,12 +1270,14 @@ int cap_mmap_addr(unsigned long addr)
94337f0d 1071 }
7f207e10
AM
1072 return ret;
1073 }
febd17d6 1074+EXPORT_SYMBOL_GPL(cap_mmap_addr);
0c3ec466
AM
1075
1076 int cap_mmap_file(struct file *file, unsigned long reqprot,
1077 unsigned long prot, unsigned long flags)
1078 {
1079 return 0;
1080 }
febd17d6 1081+EXPORT_SYMBOL_GPL(cap_mmap_file);
c2c0f25c
AM
1082
1083 #ifdef CONFIG_SECURITY
1084
7f207e10 1085diff --git a/security/device_cgroup.c b/security/device_cgroup.c
febd17d6 1086index 03c1652..f88c84b 100644
7f207e10
AM
1087--- a/security/device_cgroup.c
1088+++ b/security/device_cgroup.c
f6c5ef8b
AM
1089@@ -7,6 +7,7 @@
1090 #include <linux/device_cgroup.h>
1091 #include <linux/cgroup.h>
1092 #include <linux/ctype.h>
1093+#include <linux/export.h>
1094 #include <linux/list.h>
1095 #include <linux/uaccess.h>
1096 #include <linux/seq_file.h>
076b876e 1097@@ -849,6 +850,7 @@ int __devcgroup_inode_permission(struct inode *inode, int mask)
537831f9
AM
1098 return __devcgroup_check_permission(type, imajor(inode), iminor(inode),
1099 access);
7f207e10 1100 }
febd17d6 1101+EXPORT_SYMBOL_GPL(__devcgroup_inode_permission);
7f207e10
AM
1102
1103 int devcgroup_inode_mknod(int mode, dev_t dev)
1104 {
1105diff --git a/security/security.c b/security/security.c
8b6a4947 1106index 4bf0f57..b30d1e1 100644
7f207e10
AM
1107--- a/security/security.c
1108+++ b/security/security.c
8b6a4947 1109@@ -530,6 +530,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
7f207e10 1110 return 0;
c2c0f25c 1111 return call_int_hook(path_rmdir, 0, dir, dentry);
7f207e10 1112 }
febd17d6 1113+EXPORT_SYMBOL_GPL(security_path_rmdir);
7f207e10 1114
5afbbe0d 1115 int security_path_unlink(const struct path *dir, struct dentry *dentry)
7f207e10 1116 {
8b6a4947 1117@@ -546,6 +547,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
7f207e10 1118 return 0;
c2c0f25c 1119 return call_int_hook(path_symlink, 0, dir, dentry, old_name);
7f207e10 1120 }
febd17d6 1121+EXPORT_SYMBOL_GPL(security_path_symlink);
7f207e10 1122
5afbbe0d 1123 int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
7f207e10 1124 struct dentry *new_dentry)
8b6a4947 1125@@ -554,6 +556,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
7f207e10 1126 return 0;
c2c0f25c 1127 return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
7f207e10 1128 }
febd17d6 1129+EXPORT_SYMBOL_GPL(security_path_link);
7f207e10 1130
5afbbe0d
AM
1131 int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
1132 const struct path *new_dir, struct dentry *new_dentry,
8b6a4947 1133@@ -581,6 +584,7 @@ int security_path_truncate(const struct path *path)
7f207e10 1134 return 0;
c2c0f25c 1135 return call_int_hook(path_truncate, 0, path);
7f207e10 1136 }
febd17d6 1137+EXPORT_SYMBOL_GPL(security_path_truncate);
7f207e10 1138
5afbbe0d 1139 int security_path_chmod(const struct path *path, umode_t mode)
7eafdf33 1140 {
8b6a4947 1141@@ -588,6 +592,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
7f207e10 1142 return 0;
c2c0f25c 1143 return call_int_hook(path_chmod, 0, path, mode);
7f207e10 1144 }
febd17d6 1145+EXPORT_SYMBOL_GPL(security_path_chmod);
7f207e10 1146
5afbbe0d 1147 int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
7f207e10 1148 {
8b6a4947 1149@@ -595,6 +600,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
7f207e10 1150 return 0;
c2c0f25c 1151 return call_int_hook(path_chown, 0, path, uid, gid);
7f207e10 1152 }
febd17d6 1153+EXPORT_SYMBOL_GPL(security_path_chown);
7f207e10 1154
5afbbe0d 1155 int security_path_chroot(const struct path *path)
7f207e10 1156 {
8b6a4947 1157@@ -680,6 +686,7 @@ int security_inode_readlink(struct dentry *dentry)
7f207e10 1158 return 0;
c2c0f25c 1159 return call_int_hook(inode_readlink, 0, dentry);
7f207e10 1160 }
febd17d6 1161+EXPORT_SYMBOL_GPL(security_inode_readlink);
7f207e10 1162
c2c0f25c
AM
1163 int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
1164 bool rcu)
8b6a4947 1165@@ -695,6 +702,7 @@ int security_inode_permission(struct inode *inode, int mask)
7f207e10 1166 return 0;
c2c0f25c 1167 return call_int_hook(inode_permission, 0, inode, mask);
7f207e10 1168 }
febd17d6 1169+EXPORT_SYMBOL_GPL(security_inode_permission);
7f207e10 1170
1e00d052 1171 int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
7f207e10 1172 {
8b6a4947 1173@@ -866,6 +874,7 @@ int security_file_permission(struct file *file, int mask)
7f207e10
AM
1174
1175 return fsnotify_perm(file, mask);
1176 }
febd17d6 1177+EXPORT_SYMBOL_GPL(security_file_permission);
7f207e10
AM
1178
1179 int security_file_alloc(struct file *file)
1180 {
8b6a4947 1181@@ -925,6 +934,7 @@ int security_mmap_file(struct file *file, unsigned long prot,
7f207e10
AM
1182 return ret;
1183 return ima_file_mmap(file, prot);
1184 }
febd17d6 1185+EXPORT_SYMBOL_GPL(security_mmap_file);
7f207e10 1186
0c3ec466
AM
1187 int security_mmap_addr(unsigned long addr)
1188 {
7f207e10
AM
1189diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
1190--- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs 1970-01-01 01:00:00.000000000 +0100
1c60b727 1191+++ linux/Documentation/ABI/testing/debugfs-aufs 2017-07-29 12:14:25.893041746 +0200
86dc4139 1192@@ -0,0 +1,50 @@
7f207e10
AM
1193+What: /debug/aufs/si_<id>/
1194+Date: March 2009
f6b6e03d 1195+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1196+Description:
1197+ Under /debug/aufs, a directory named si_<id> is created
1198+ per aufs mount, where <id> is a unique id generated
1199+ internally.
1facf9fc 1200+
86dc4139
AM
1201+What: /debug/aufs/si_<id>/plink
1202+Date: Apr 2013
f6b6e03d 1203+Contact: J. R. Okajima <hooanon05g@gmail.com>
86dc4139
AM
1204+Description:
1205+ It has three lines and shows the information about the
1206+ pseudo-link. The first line is a single number
1207+ representing a number of buckets. The second line is a
1208+ number of pseudo-links per buckets (separated by a
1209+ blank). The last line is a single number representing a
1210+ total number of psedo-links.
1211+ When the aufs mount option 'noplink' is specified, it
1212+ will show "1\n0\n0\n".
1213+
7f207e10
AM
1214+What: /debug/aufs/si_<id>/xib
1215+Date: March 2009
f6b6e03d 1216+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1217+Description:
1218+ It shows the consumed blocks by xib (External Inode Number
1219+ Bitmap), its block size and file size.
1220+ When the aufs mount option 'noxino' is specified, it
1221+ will be empty. About XINO files, see the aufs manual.
1222+
1223+What: /debug/aufs/si_<id>/xino0, xino1 ... xinoN
1224+Date: March 2009
f6b6e03d 1225+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1226+Description:
1227+ It shows the consumed blocks by xino (External Inode Number
1228+ Translation Table), its link count, block size and file
1229+ size.
1230+ When the aufs mount option 'noxino' is specified, it
1231+ will be empty. About XINO files, see the aufs manual.
1232+
1233+What: /debug/aufs/si_<id>/xigen
1234+Date: March 2009
f6b6e03d 1235+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1236+Description:
1237+ It shows the consumed blocks by xigen (External Inode
1238+ Generation Table), its block size and file size.
1239+ If CONFIG_AUFS_EXPORT is disabled, this entry will not
1240+ be created.
1241+ When the aufs mount option 'noxino' is specified, it
1242+ will be empty. About XINO files, see the aufs manual.
1243diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
1244--- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs 1970-01-01 01:00:00.000000000 +0100
1c60b727 1245+++ linux/Documentation/ABI/testing/sysfs-aufs 2017-07-29 12:14:25.893041746 +0200
392086de 1246@@ -0,0 +1,31 @@
7f207e10
AM
1247+What: /sys/fs/aufs/si_<id>/
1248+Date: March 2009
f6b6e03d 1249+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1250+Description:
1251+ Under /sys/fs/aufs, a directory named si_<id> is created
1252+ per aufs mount, where <id> is a unique id generated
1253+ internally.
1254+
1255+What: /sys/fs/aufs/si_<id>/br0, br1 ... brN
1256+Date: March 2009
f6b6e03d 1257+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1258+Description:
1259+ It shows the abolute path of a member directory (which
1260+ is called branch) in aufs, and its permission.
1261+
392086de
AM
1262+What: /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
1263+Date: July 2013
f6b6e03d 1264+Contact: J. R. Okajima <hooanon05g@gmail.com>
392086de
AM
1265+Description:
1266+ It shows the id of a member directory (which is called
1267+ branch) in aufs.
1268+
7f207e10
AM
1269+What: /sys/fs/aufs/si_<id>/xi_path
1270+Date: March 2009
f6b6e03d 1271+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1272+Description:
1273+ It shows the abolute path of XINO (External Inode Number
1274+ Bitmap, Translation Table and Generation Table) file
1275+ even if it is the default path.
1276+ When the aufs mount option 'noxino' is specified, it
1277+ will be empty. About XINO files, see the aufs manual.
53392da6
AM
1278diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1279--- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt 1970-01-01 01:00:00.000000000 +0100
8b6a4947 1280+++ linux/Documentation/filesystems/aufs/design/01intro.txt 2017-11-12 22:24:42.257509799 +0100
1c60b727 1281@@ -0,0 +1,171 @@
53392da6 1282+
a2654f78 1283+# Copyright (C) 2005-2017 Junjiro R. Okajima
53392da6
AM
1284+#
1285+# This program is free software; you can redistribute it and/or modify
1286+# it under the terms of the GNU General Public License as published by
1287+# the Free Software Foundation; either version 2 of the License, or
1288+# (at your option) any later version.
1289+#
1290+# This program is distributed in the hope that it will be useful,
1291+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1292+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1293+# GNU General Public License for more details.
1294+#
1295+# You should have received a copy of the GNU General Public License
523b37e3 1296+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1297+
1298+Introduction
1299+----------------------------------------
1300+
3c1bdaff 1301+aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
53392da6
AM
1302+1. abbrev. for "advanced multi-layered unification filesystem".
1303+2. abbrev. for "another unionfs".
1304+3. abbrev. for "auf das" in German which means "on the" in English.
1305+ Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1306+ But "Filesystem aufs Filesystem" is hard to understand.
1c60b727 1307+4. abbrev. for "African Urban Fashion Show".
53392da6
AM
1308+
1309+AUFS is a filesystem with features:
1310+- multi layered stackable unification filesystem, the member directory
1311+ is called as a branch.
1312+- branch permission and attribute, 'readonly', 'real-readonly',
7e9cd9fe 1313+ 'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
53392da6
AM
1314+ combination.
1315+- internal "file copy-on-write".
1316+- logical deletion, whiteout.
1317+- dynamic branch manipulation, adding, deleting and changing permission.
1318+- allow bypassing aufs, user's direct branch access.
1319+- external inode number translation table and bitmap which maintains the
1320+ persistent aufs inode number.
1321+- seekable directory, including NFS readdir.
1322+- file mapping, mmap and sharing pages.
1323+- pseudo-link, hardlink over branches.
1324+- loopback mounted filesystem as a branch.
1325+- several policies to select one among multiple writable branches.
1326+- revert a single systemcall when an error occurs in aufs.
1327+- and more...
1328+
1329+
1330+Multi Layered Stackable Unification Filesystem
1331+----------------------------------------------------------------------
1332+Most people already knows what it is.
1333+It is a filesystem which unifies several directories and provides a
1334+merged single directory. When users access a file, the access will be
1335+passed/re-directed/converted (sorry, I am not sure which English word is
1336+correct) to the real file on the member filesystem. The member
1337+filesystem is called 'lower filesystem' or 'branch' and has a mode
1338+'readonly' and 'readwrite.' And the deletion for a file on the lower
1339+readonly branch is handled by creating 'whiteout' on the upper writable
1340+branch.
1341+
1342+On LKML, there have been discussions about UnionMount (Jan Blunck,
1343+Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1344+different approaches to implement the merged-view.
1345+The former tries putting it into VFS, and the latter implements as a
1346+separate filesystem.
1347+(If I misunderstand about these implementations, please let me know and
1348+I shall correct it. Because it is a long time ago when I read their
1349+source files last time).
1350+
1351+UnionMount's approach will be able to small, but may be hard to share
1352+branches between several UnionMount since the whiteout in it is
1353+implemented in the inode on branch filesystem and always
1354+shared. According to Bharata's post, readdir does not seems to be
1355+finished yet.
1356+There are several missing features known in this implementations such as
1357+- for users, the inode number may change silently. eg. copy-up.
1358+- link(2) may break by copy-up.
1359+- read(2) may get an obsoleted filedata (fstat(2) too).
1360+- fcntl(F_SETLK) may be broken by copy-up.
1361+- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1362+ open(O_RDWR).
1363+
7e9cd9fe
AM
1364+In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1365+merged into mainline. This is another implementation of UnionMount as a
1366+separated filesystem. All the limitations and known problems which
1367+UnionMount are equally inherited to "overlay" filesystem.
1368+
1369+Unionfs has a longer history. When I started implementing a stackable
1370+filesystem (Aug 2005), it already existed. It has virtual super_block,
1371+inode, dentry and file objects and they have an array pointing lower
1372+same kind objects. After contributing many patches for Unionfs, I
1373+re-started my project AUFS (Jun 2006).
53392da6
AM
1374+
1375+In AUFS, the structure of filesystem resembles to Unionfs, but I
1376+implemented my own ideas, approaches and enhancements and it became
1377+totally different one.
1378+
1379+Comparing DM snapshot and fs based implementation
1380+- the number of bytes to be copied between devices is much smaller.
1381+- the type of filesystem must be one and only.
1382+- the fs must be writable, no readonly fs, even for the lower original
1383+ device. so the compression fs will not be usable. but if we use
1384+ loopback mount, we may address this issue.
1385+ for instance,
1386+ mount /cdrom/squashfs.img /sq
1387+ losetup /sq/ext2.img
1388+ losetup /somewhere/cow
1389+ dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1390+- it will be difficult (or needs more operations) to extract the
1391+ difference between the original device and COW.
1392+- DM snapshot-merge may help a lot when users try merging. in the
1393+ fs-layer union, users will use rsync(1).
1394+
7e9cd9fe
AM
1395+You may want to read my old paper "Filesystems in LiveCD"
1396+(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
53392da6 1397+
7e9cd9fe
AM
1398+
1399+Several characters/aspects/persona of aufs
53392da6
AM
1400+----------------------------------------------------------------------
1401+
7e9cd9fe 1402+Aufs has several characters, aspects or persona.
53392da6
AM
1403+1. a filesystem, callee of VFS helper
1404+2. sub-VFS, caller of VFS helper for branches
1405+3. a virtual filesystem which maintains persistent inode number
1406+4. reader/writer of files on branches such like an application
1407+
1408+1. Callee of VFS Helper
1409+As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1410+unlink(2) from an application reaches sys_unlink() kernel function and
1411+then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1412+calls filesystem specific unlink operation. Actually aufs implements the
1413+unlink operation but it behaves like a redirector.
1414+
1415+2. Caller of VFS Helper for Branches
1416+aufs_unlink() passes the unlink request to the branch filesystem as if
1417+it were called from VFS. So the called unlink operation of the branch
1418+filesystem acts as usual. As a caller of VFS helper, aufs should handle
1419+every necessary pre/post operation for the branch filesystem.
1420+- acquire the lock for the parent dir on a branch
1421+- lookup in a branch
1422+- revalidate dentry on a branch
1423+- mnt_want_write() for a branch
1424+- vfs_unlink() for a branch
1425+- mnt_drop_write() for a branch
1426+- release the lock on a branch
1427+
1428+3. Persistent Inode Number
1429+One of the most important issue for a filesystem is to maintain inode
1430+numbers. This is particularly important to support exporting a
1431+filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1432+backend block device for its own. But some storage is necessary to
7e9cd9fe
AM
1433+keep and maintain the inode numbers. It may be a large space and may not
1434+suit to keep in memory. Aufs rents some space from its first writable
1435+branch filesystem (by default) and creates file(s) on it. These files
1436+are created by aufs internally and removed soon (currently) keeping
1437+opened.
53392da6
AM
1438+Note: Because these files are removed, they are totally gone after
1439+ unmounting aufs. It means the inode numbers are not persistent
1440+ across unmount or reboot. I have a plan to make them really
1441+ persistent which will be important for aufs on NFS server.
1442+
1443+4. Read/Write Files Internally (copy-on-write)
1444+Because a branch can be readonly, when you write a file on it, aufs will
1445+"copy-up" it to the upper writable branch internally. And then write the
1446+originally requested thing to the file. Generally kernel doesn't
1447+open/read/write file actively. In aufs, even a single write may cause a
1448+internal "file copy". This behaviour is very similar to cp(1) command.
1449+
1450+Some people may think it is better to pass such work to user space
1451+helper, instead of doing in kernel space. Actually I am still thinking
1452+about it. But currently I have implemented it in kernel space.
1453diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1454--- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1c60b727 1455+++ linux/Documentation/filesystems/aufs/design/02struct.txt 2017-07-29 12:14:25.893041746 +0200
7e9cd9fe 1456@@ -0,0 +1,258 @@
53392da6 1457+
a2654f78 1458+# Copyright (C) 2005-2017 Junjiro R. Okajima
53392da6
AM
1459+#
1460+# This program is free software; you can redistribute it and/or modify
1461+# it under the terms of the GNU General Public License as published by
1462+# the Free Software Foundation; either version 2 of the License, or
1463+# (at your option) any later version.
1464+#
1465+# This program is distributed in the hope that it will be useful,
1466+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1467+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1468+# GNU General Public License for more details.
1469+#
1470+# You should have received a copy of the GNU General Public License
523b37e3 1471+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1472+
1473+Basic Aufs Internal Structure
1474+
1475+Superblock/Inode/Dentry/File Objects
1476+----------------------------------------------------------------------
1477+As like an ordinary filesystem, aufs has its own
1478+superblock/inode/dentry/file objects. All these objects have a
1479+dynamically allocated array and store the same kind of pointers to the
1480+lower filesystem, branch.
1481+For example, when you build a union with one readwrite branch and one
1482+readonly, mounted /au, /rw and /ro respectively.
1483+- /au = /rw + /ro
1484+- /ro/fileA exists but /rw/fileA
1485+
1486+Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1487+pointers are stored in a aufs dentry. The array in aufs dentry will be,
7e9cd9fe 1488+- [0] = NULL (because /rw/fileA doesn't exist)
53392da6
AM
1489+- [1] = /ro/fileA
1490+
1491+This style of an array is essentially same to the aufs
1492+superblock/inode/dentry/file objects.
1493+
1494+Because aufs supports manipulating branches, ie. add/delete/change
7e9cd9fe
AM
1495+branches dynamically, these objects has its own generation. When
1496+branches are changed, the generation in aufs superblock is
1497+incremented. And a generation in other object are compared when it is
1498+accessed. When a generation in other objects are obsoleted, aufs
1499+refreshes the internal array.
53392da6
AM
1500+
1501+
1502+Superblock
1503+----------------------------------------------------------------------
1504+Additionally aufs superblock has some data for policies to select one
1505+among multiple writable branches, XIB files, pseudo-links and kobject.
1506+See below in detail.
7e9cd9fe
AM
1507+About the policies which supports copy-down a directory, see
1508+wbr_policy.txt too.
53392da6
AM
1509+
1510+
1511+Branch and XINO(External Inode Number Translation Table)
1512+----------------------------------------------------------------------
1513+Every branch has its own xino (external inode number translation table)
1514+file. The xino file is created and unlinked by aufs internally. When two
1515+members of a union exist on the same filesystem, they share the single
1516+xino file.
1517+The struct of a xino file is simple, just a sequence of aufs inode
1518+numbers which is indexed by the lower inode number.
1519+In the above sample, assume the inode number of /ro/fileA is i111 and
1520+aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1521+4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1522+
1523+When the inode numbers are not contiguous, the xino file will be sparse
1524+which has a hole in it and doesn't consume as much disk space as it
1525+might appear. If your branch filesystem consumes disk space for such
1526+holes, then you should specify 'xino=' option at mounting aufs.
1527+
7e9cd9fe
AM
1528+Aufs has a mount option to free the disk blocks for such holes in XINO
1529+files on tmpfs or ramdisk. But it is not so effective actually. If you
1530+meet a problem of disk shortage due to XINO files, then you should try
1531+"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1532+The patch localizes the assignment inumbers per tmpfs-mount and avoid
1533+the holes in XINO files.
1534+
53392da6 1535+Also a writable branch has three kinds of "whiteout bases". All these
7e9cd9fe 1536+are existed when the branch is joined to aufs, and their names are
53392da6
AM
1537+whiteout-ed doubly, so that users will never see their names in aufs
1538+hierarchy.
7e9cd9fe 1539+1. a regular file which will be hardlinked to all whiteouts.
53392da6 1540+2. a directory to store a pseudo-link.
7e9cd9fe 1541+3. a directory to store an "orphan"-ed file temporary.
53392da6
AM
1542+
1543+1. Whiteout Base
1544+ When you remove a file on a readonly branch, aufs handles it as a
1545+ logical deletion and creates a whiteout on the upper writable branch
1546+ as a hardlink of this file in order not to consume inode on the
1547+ writable branch.
1548+2. Pseudo-link Dir
1549+ See below, Pseudo-link.
1550+3. Step-Parent Dir
1551+ When "fileC" exists on the lower readonly branch only and it is
1552+ opened and removed with its parent dir, and then user writes
1553+ something into it, then aufs copies-up fileC to this
1554+ directory. Because there is no other dir to store fileC. After
1555+ creating a file under this dir, the file is unlinked.
1556+
1557+Because aufs supports manipulating branches, ie. add/delete/change
7e9cd9fe
AM
1558+dynamically, a branch has its own id. When the branch order changes,
1559+aufs finds the new index by searching the branch id.
53392da6
AM
1560+
1561+
1562+Pseudo-link
1563+----------------------------------------------------------------------
1564+Assume "fileA" exists on the lower readonly branch only and it is
1565+hardlinked to "fileB" on the branch. When you write something to fileA,
1566+aufs copies-up it to the upper writable branch. Additionally aufs
1567+creates a hardlink under the Pseudo-link Directory of the writable
1568+branch. The inode of a pseudo-link is kept in aufs super_block as a
1569+simple list. If fileB is read after unlinking fileA, aufs returns
1570+filedata from the pseudo-link instead of the lower readonly
1571+branch. Because the pseudo-link is based upon the inode, to keep the
7e9cd9fe 1572+inode number by xino (see above) is essentially necessary.
53392da6
AM
1573+
1574+All the hardlinks under the Pseudo-link Directory of the writable branch
1575+should be restored in a proper location later. Aufs provides a utility
1576+to do this. The userspace helpers executed at remounting and unmounting
1577+aufs by default.
1578+During this utility is running, it puts aufs into the pseudo-link
1579+maintenance mode. In this mode, only the process which began the
1580+maintenance mode (and its child processes) is allowed to operate in
1581+aufs. Some other processes which are not related to the pseudo-link will
1582+be allowed to run too, but the rest have to return an error or wait
1583+until the maintenance mode ends. If a process already acquires an inode
1584+mutex (in VFS), it has to return an error.
1585+
1586+
1587+XIB(external inode number bitmap)
1588+----------------------------------------------------------------------
1589+Addition to the xino file per a branch, aufs has an external inode number
7e9cd9fe
AM
1590+bitmap in a superblock object. It is also an internal file such like a
1591+xino file.
53392da6
AM
1592+It is a simple bitmap to mark whether the aufs inode number is in-use or
1593+not.
1594+To reduce the file I/O, aufs prepares a single memory page to cache xib.
1595+
7e9cd9fe 1596+As well as XINO files, aufs has a feature to truncate/refresh XIB to
53392da6
AM
1597+reduce the number of consumed disk blocks for these files.
1598+
1599+
1600+Virtual or Vertical Dir, and Readdir in Userspace
1601+----------------------------------------------------------------------
1602+In order to support multiple layers (branches), aufs readdir operation
1603+constructs a virtual dir block on memory. For readdir, aufs calls
1604+vfs_readdir() internally for each dir on branches, merges their entries
1605+with eliminating the whiteout-ed ones, and sets it to file (dir)
1606+object. So the file object has its entry list until it is closed. The
1607+entry list will be updated when the file position is zero and becomes
7e9cd9fe 1608+obsoleted. This decision is made in aufs automatically.
53392da6
AM
1609+
1610+The dynamically allocated memory block for the name of entries has a
1611+unit of 512 bytes (by default) and stores the names contiguously (no
1612+padding). Another block for each entry is handled by kmem_cache too.
1613+During building dir blocks, aufs creates hash list and judging whether
1614+the entry is whiteouted by its upper branch or already listed.
1615+The merged result is cached in the corresponding inode object and
1616+maintained by a customizable life-time option.
1617+
1618+Some people may call it can be a security hole or invite DoS attack
1619+since the opened and once readdir-ed dir (file object) holds its entry
1620+list and becomes a pressure for system memory. But I'd say it is similar
1621+to files under /proc or /sys. The virtual files in them also holds a
1622+memory page (generally) while they are opened. When an idea to reduce
1623+memory for them is introduced, it will be applied to aufs too.
1624+For those who really hate this situation, I've developed readdir(3)
1625+library which operates this merging in userspace. You just need to set
1626+LD_PRELOAD environment variable, and aufs will not consume no memory in
1627+kernel space for readdir(3).
1628+
1629+
1630+Workqueue
1631+----------------------------------------------------------------------
1632+Aufs sometimes requires privilege access to a branch. For instance,
1633+in copy-up/down operation. When a user process is going to make changes
1634+to a file which exists in the lower readonly branch only, and the mode
1635+of one of ancestor directories may not be writable by a user
1636+process. Here aufs copy-up the file with its ancestors and they may
1637+require privilege to set its owner/group/mode/etc.
1638+This is a typical case of a application character of aufs (see
1639+Introduction).
1640+
1641+Aufs uses workqueue synchronously for this case. It creates its own
1642+workqueue. The workqueue is a kernel thread and has privilege. Aufs
1643+passes the request to call mkdir or write (for example), and wait for
1644+its completion. This approach solves a problem of a signal handler
1645+simply.
1646+If aufs didn't adopt the workqueue and changed the privilege of the
7e9cd9fe
AM
1647+process, then the process may receive the unexpected SIGXFSZ or other
1648+signals.
53392da6
AM
1649+
1650+Also aufs uses the system global workqueue ("events" kernel thread) too
1651+for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1652+whiteout base and etc. This is unrelated to a privilege.
1653+Most of aufs operation tries acquiring a rw_semaphore for aufs
1654+superblock at the beginning, at the same time waits for the completion
1655+of all queued asynchronous tasks.
1656+
1657+
1658+Whiteout
1659+----------------------------------------------------------------------
1660+The whiteout in aufs is very similar to Unionfs's. That is represented
1661+by its filename. UnionMount takes an approach of a file mode, but I am
1662+afraid several utilities (find(1) or something) will have to support it.
1663+
1664+Basically the whiteout represents "logical deletion" which stops aufs to
1665+lookup further, but also it represents "dir is opaque" which also stop
7e9cd9fe 1666+further lookup.
53392da6
AM
1667+
1668+In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1669+In order to make several functions in a single systemcall to be
1670+revertible, aufs adopts an approach to rename a directory to a temporary
1671+unique whiteouted name.
1672+For example, in rename(2) dir where the target dir already existed, aufs
1673+renames the target dir to a temporary unique whiteouted name before the
7e9cd9fe 1674+actual rename on a branch, and then handles other actions (make it opaque,
53392da6
AM
1675+update the attributes, etc). If an error happens in these actions, aufs
1676+simply renames the whiteouted name back and returns an error. If all are
1677+succeeded, aufs registers a function to remove the whiteouted unique
1678+temporary name completely and asynchronously to the system global
1679+workqueue.
1680+
1681+
1682+Copy-up
1683+----------------------------------------------------------------------
1684+It is a well-known feature or concept.
1685+When user modifies a file on a readonly branch, aufs operate "copy-up"
1686+internally and makes change to the new file on the upper writable branch.
1687+When the trigger systemcall does not update the timestamps of the parent
1688+dir, aufs reverts it after copy-up.
c2b27bf2
AM
1689+
1690+
1691+Move-down (aufs3.9 and later)
1692+----------------------------------------------------------------------
1693+"Copy-up" is one of the essential feature in aufs. It copies a file from
1694+the lower readonly branch to the upper writable branch when a user
1695+changes something about the file.
1696+"Move-down" is an opposite action of copy-up. Basically this action is
1697+ran manually instead of automatically and internally.
076b876e
AM
1698+For desgin and implementation, aufs has to consider these issues.
1699+- whiteout for the file may exist on the lower branch.
1700+- ancestor directories may not exist on the lower branch.
1701+- diropq for the ancestor directories may exist on the upper branch.
1702+- free space on the lower branch will reduce.
1703+- another access to the file may happen during moving-down, including
7e9cd9fe 1704+ UDBA (see "Revalidate Dentry and UDBA").
076b876e
AM
1705+- the file should not be hard-linked nor pseudo-linked. they should be
1706+ handled by auplink utility later.
c2b27bf2
AM
1707+
1708+Sometimes users want to move-down a file from the upper writable branch
1709+to the lower readonly or writable branch. For instance,
1710+- the free space of the upper writable branch is going to run out.
1711+- create a new intermediate branch between the upper and lower branch.
1712+- etc.
1713+
1714+For this purpose, use "aumvdown" command in aufs-util.git.
b912730e
AM
1715diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1716--- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt 1970-01-01 01:00:00.000000000 +0100
1c60b727 1717+++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt 2017-07-29 12:14:25.893041746 +0200
b912730e
AM
1718@@ -0,0 +1,85 @@
1719+
a2654f78 1720+# Copyright (C) 2015-2017 Junjiro R. Okajima
b912730e
AM
1721+#
1722+# This program is free software; you can redistribute it and/or modify
1723+# it under the terms of the GNU General Public License as published by
1724+# the Free Software Foundation; either version 2 of the License, or
1725+# (at your option) any later version.
1726+#
1727+# This program is distributed in the hope that it will be useful,
1728+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1729+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1730+# GNU General Public License for more details.
1731+#
1732+# You should have received a copy of the GNU General Public License
1733+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1734+
1735+Support for a branch who has its ->atomic_open()
1736+----------------------------------------------------------------------
1737+The filesystems who implement its ->atomic_open() are not majority. For
1738+example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1739+particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1740+->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1741+sure whether all filesystems who have ->atomic_open() behave like this,
1742+but NFSv4 surely returns the error.
1743+
1744+In order to support ->atomic_open() for aufs, there are a few
1745+approaches.
1746+
1747+A. Introduce aufs_atomic_open()
1748+ - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1749+ branch fs.
1750+B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1751+ an aufs user Pip Cet's approach
1752+ - calls aufs_create(), VFS finish_open() and notify_change().
1753+ - pass fake-mode to finish_open(), and then correct the mode by
1754+ notify_change().
1755+C. Extend aufs_open() to call branch fs's ->atomic_open()
1756+ - no aufs_atomic_open().
1757+ - aufs_lookup() registers the TID to an aufs internal object.
1758+ - aufs_create() does nothing when the matching TID is registered, but
1759+ registers the mode.
1760+ - aufs_open() calls branch fs's ->atomic_open() when the matching
1761+ TID is registered.
1762+D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1763+ credential
1764+ - no aufs_atomic_open().
1765+ - aufs_create() registers the TID to an internal object. this info
1766+ represents "this process created this file just now."
1767+ - when aufs gets EACCES from branch fs's ->open(), then confirm the
1768+ registered TID and re-try open() with superuser's credential.
1769+
1770+Pros and cons for each approach.
1771+
1772+A.
1773+ - straightforward but highly depends upon VFS internal.
1774+ - the atomic behavaiour is kept.
1775+ - some of parameters such as nameidata are hard to reproduce for
1776+ branch fs.
1777+ - large overhead.
1778+B.
1779+ - easy to implement.
1780+ - the atomic behavaiour is lost.
1781+C.
1782+ - the atomic behavaiour is kept.
1783+ - dirty and tricky.
1784+ - VFS checks whether the file is created correctly after calling
1785+ ->create(), which means this approach doesn't work.
1786+D.
1787+ - easy to implement.
1788+ - the atomic behavaiour is lost.
1789+ - to open a file with superuser's credential and give it to a user
1790+ process is a bad idea, since the file object keeps the credential
1791+ in it. It may affect LSM or something. This approach doesn't work
1792+ either.
1793+
1794+The approach A is ideal, but it hard to implement. So here is a
1795+variation of A, which is to be implemented.
1796+
1797+A-1. Introduce aufs_atomic_open()
1798+ - calls branch fs ->atomic_open() if exists. otherwise calls
1799+ vfs_create() and finish_open().
1800+ - the demerit is that the several checks after branch fs
1801+ ->atomic_open() are lost. in the ordinary case, the checks are
1802+ done by VFS:do_last(), lookup_open() and atomic_open(). some can
1803+ be implemented in aufs, but not all I am afraid.
53392da6
AM
1804diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1805--- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1c60b727 1806+++ linux/Documentation/filesystems/aufs/design/03lookup.txt 2017-07-29 12:14:25.893041746 +0200
7e9cd9fe 1807@@ -0,0 +1,113 @@
53392da6 1808+
a2654f78 1809+# Copyright (C) 2005-2017 Junjiro R. Okajima
53392da6
AM
1810+#
1811+# This program is free software; you can redistribute it and/or modify
1812+# it under the terms of the GNU General Public License as published by
1813+# the Free Software Foundation; either version 2 of the License, or
1814+# (at your option) any later version.
1815+#
1816+# This program is distributed in the hope that it will be useful,
1817+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1818+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1819+# GNU General Public License for more details.
1820+#
1821+# You should have received a copy of the GNU General Public License
523b37e3 1822+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1823+
1824+Lookup in a Branch
1825+----------------------------------------------------------------------
1826+Since aufs has a character of sub-VFS (see Introduction), it operates
7e9cd9fe
AM
1827+lookup for branches as VFS does. It may be a heavy work. But almost all
1828+lookup operation in aufs is the simplest case, ie. lookup only an entry
1829+directly connected to its parent. Digging down the directory hierarchy
1830+is unnecessary. VFS has a function lookup_one_len() for that use, and
1831+aufs calls it.
1832+
1833+When a branch is a remote filesystem, aufs basically relies upon its
53392da6
AM
1834+->d_revalidate(), also aufs forces the hardest revalidate tests for
1835+them.
1836+For d_revalidate, aufs implements three levels of revalidate tests. See
1837+"Revalidate Dentry and UDBA" in detail.
1838+
1839+
076b876e
AM
1840+Test Only the Highest One for the Directory Permission (dirperm1 option)
1841+----------------------------------------------------------------------
1842+Let's try case study.
1843+- aufs has two branches, upper readwrite and lower readonly.
1844+ /au = /rw + /ro
1845+- "dirA" exists under /ro, but /rw. and its mode is 0700.
1846+- user invoked "chmod a+rx /au/dirA"
1847+- the internal copy-up is activated and "/rw/dirA" is created and its
7e9cd9fe 1848+ permission bits are set to world readable.
076b876e
AM
1849+- then "/au/dirA" becomes world readable?
1850+
1851+In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1852+or it may be a natively readonly filesystem. If aufs respects the lower
1853+branch, it should not respond readdir request from other users. But user
1854+allowed it by chmod. Should really aufs rejects showing the entries
1855+under /ro/dirA?
1856+
7e9cd9fe
AM
1857+To be honest, I don't have a good solution for this case. So aufs
1858+implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1859+users.
076b876e
AM
1860+When dirperm1 is specified, aufs checks only the highest one for the
1861+directory permission, and shows the entries. Otherwise, as usual, checks
1862+every dir existing on all branches and rejects the request.
1863+
1864+As a side effect, dirperm1 option improves the performance of aufs
1865+because the number of permission check is reduced when the number of
1866+branch is many.
1867+
1868+
53392da6
AM
1869+Revalidate Dentry and UDBA (User's Direct Branch Access)
1870+----------------------------------------------------------------------
1871+Generally VFS helpers re-validate a dentry as a part of lookup.
1872+0. digging down the directory hierarchy.
1873+1. lock the parent dir by its i_mutex.
1874+2. lookup the final (child) entry.
1875+3. revalidate it.
1876+4. call the actual operation (create, unlink, etc.)
1877+5. unlock the parent dir
1878+
1879+If the filesystem implements its ->d_revalidate() (step 3), then it is
1880+called. Actually aufs implements it and checks the dentry on a branch is
1881+still valid.
1882+But it is not enough. Because aufs has to release the lock for the
1883+parent dir on a branch at the end of ->lookup() (step 2) and
1884+->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1885+held by VFS.
1886+If the file on a branch is changed directly, eg. bypassing aufs, after
1887+aufs released the lock, then the subsequent operation may cause
1888+something unpleasant result.
1889+
1890+This situation is a result of VFS architecture, ->lookup() and
1891+->d_revalidate() is separated. But I never say it is wrong. It is a good
1892+design from VFS's point of view. It is just not suitable for sub-VFS
1893+character in aufs.
1894+
1895+Aufs supports such case by three level of revalidation which is
1896+selectable by user.
1897+1. Simple Revalidate
1898+ Addition to the native flow in VFS's, confirm the child-parent
1899+ relationship on the branch just after locking the parent dir on the
1900+ branch in the "actual operation" (step 4). When this validation
1901+ fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1902+ checks the validation of the dentry on branches.
1903+2. Monitor Changes Internally by Inotify/Fsnotify
1904+ Addition to above, in the "actual operation" (step 4) aufs re-lookup
1905+ the dentry on the branch, and returns EBUSY if it finds different
1906+ dentry.
1907+ Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1908+ during it is in cache. When the event is notified, aufs registers a
1909+ function to kernel 'events' thread by schedule_work(). And the
1910+ function sets some special status to the cached aufs dentry and inode
1911+ private data. If they are not cached, then aufs has nothing to
1912+ do. When the same file is accessed through aufs (step 0-3) later,
1913+ aufs will detect the status and refresh all necessary data.
1914+ In this mode, aufs has to ignore the event which is fired by aufs
1915+ itself.
1916+3. No Extra Validation
1917+ This is the simplest test and doesn't add any additional revalidation
7e9cd9fe 1918+ test, and skip the revalidation in step 4. It is useful and improves
53392da6
AM
1919+ aufs performance when system surely hide the aufs branches from user,
1920+ by over-mounting something (or another method).
1921diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1922--- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1c60b727 1923+++ linux/Documentation/filesystems/aufs/design/04branch.txt 2017-07-29 12:14:25.893041746 +0200
7e9cd9fe 1924@@ -0,0 +1,74 @@
53392da6 1925+
a2654f78 1926+# Copyright (C) 2005-2017 Junjiro R. Okajima
53392da6
AM
1927+#
1928+# This program is free software; you can redistribute it and/or modify
1929+# it under the terms of the GNU General Public License as published by
1930+# the Free Software Foundation; either version 2 of the License, or
1931+# (at your option) any later version.
1932+#
1933+# This program is distributed in the hope that it will be useful,
1934+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1935+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1936+# GNU General Public License for more details.
1937+#
1938+# You should have received a copy of the GNU General Public License
523b37e3 1939+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1940+
1941+Branch Manipulation
1942+
1943+Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1944+and changing its permission/attribute, there are a lot of works to do.
1945+
1946+
1947+Add a Branch
1948+----------------------------------------------------------------------
1949+o Confirm the adding dir exists outside of aufs, including loopback
7e9cd9fe 1950+ mount, and its various attributes.
53392da6
AM
1951+o Initialize the xino file and whiteout bases if necessary.
1952+ See struct.txt.
1953+
1954+o Check the owner/group/mode of the directory
1955+ When the owner/group/mode of the adding directory differs from the
1956+ existing branch, aufs issues a warning because it may impose a
1957+ security risk.
1958+ For example, when a upper writable branch has a world writable empty
1959+ top directory, a malicious user can create any files on the writable
1960+ branch directly, like copy-up and modify manually. If something like
1961+ /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1962+ writable branch, and the writable branch is world-writable, then a
1963+ malicious guy may create /etc/passwd on the writable branch directly
1964+ and the infected file will be valid in aufs.
7e9cd9fe 1965+ I am afraid it can be a security issue, but aufs can do nothing except
53392da6
AM
1966+ producing a warning.
1967+
1968+
1969+Delete a Branch
1970+----------------------------------------------------------------------
1971+o Confirm the deleting branch is not busy
1972+ To be general, there is one merit to adopt "remount" interface to
1973+ manipulate branches. It is to discard caches. At deleting a branch,
1974+ aufs checks the still cached (and connected) dentries and inodes. If
1975+ there are any, then they are all in-use. An inode without its
1976+ corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1977+
1978+ For the cached one, aufs checks whether the same named entry exists on
1979+ other branches.
1980+ If the cached one is a directory, because aufs provides a merged view
1981+ to users, as long as one dir is left on any branch aufs can show the
1982+ dir to users. In this case, the branch can be removed from aufs.
1983+ Otherwise aufs rejects deleting the branch.
1984+
1985+ If any file on the deleting branch is opened by aufs, then aufs
1986+ rejects deleting.
1987+
1988+
1989+Modify the Permission of a Branch
1990+----------------------------------------------------------------------
1991+o Re-initialize or remove the xino file and whiteout bases if necessary.
1992+ See struct.txt.
1993+
1994+o rw --> ro: Confirm the modifying branch is not busy
1995+ Aufs rejects the request if any of these conditions are true.
1996+ - a file on the branch is mmap-ed.
1997+ - a regular file on the branch is opened for write and there is no
1998+ same named entry on the upper branch.
1999diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
2000--- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt 1970-01-01 01:00:00.000000000 +0100
1c60b727 2001+++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt 2017-07-29 12:14:25.893041746 +0200
523b37e3 2002@@ -0,0 +1,64 @@
53392da6 2003+
a2654f78 2004+# Copyright (C) 2005-2017 Junjiro R. Okajima
53392da6
AM
2005+#
2006+# This program is free software; you can redistribute it and/or modify
2007+# it under the terms of the GNU General Public License as published by
2008+# the Free Software Foundation; either version 2 of the License, or
2009+# (at your option) any later version.
2010+#
2011+# This program is distributed in the hope that it will be useful,
2012+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2013+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2014+# GNU General Public License for more details.
2015+#
2016+# You should have received a copy of the GNU General Public License
523b37e3 2017+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2018+
2019+Policies to Select One among Multiple Writable Branches
2020+----------------------------------------------------------------------
2021+When the number of writable branch is more than one, aufs has to decide
2022+the target branch for file creation or copy-up. By default, the highest
2023+writable branch which has the parent (or ancestor) dir of the target
2024+file is chosen (top-down-parent policy).
2025+By user's request, aufs implements some other policies to select the
7e9cd9fe
AM
2026+writable branch, for file creation several policies, round-robin,
2027+most-free-space, and other policies. For copy-up, top-down-parent,
2028+bottom-up-parent, bottom-up and others.
53392da6
AM
2029+
2030+As expected, the round-robin policy selects the branch in circular. When
2031+you have two writable branches and creates 10 new files, 5 files will be
2032+created for each branch. mkdir(2) systemcall is an exception. When you
2033+create 10 new directories, all will be created on the same branch.
2034+And the most-free-space policy selects the one which has most free
2035+space among the writable branches. The amount of free space will be
2036+checked by aufs internally, and users can specify its time interval.
2037+
2038+The policies for copy-up is more simple,
2039+top-down-parent is equivalent to the same named on in create policy,
2040+bottom-up-parent selects the writable branch where the parent dir
2041+exists and the nearest upper one from the copyup-source,
2042+bottom-up selects the nearest upper writable branch from the
2043+copyup-source, regardless the existence of the parent dir.
2044+
2045+There are some rules or exceptions to apply these policies.
2046+- If there is a readonly branch above the policy-selected branch and
2047+ the parent dir is marked as opaque (a variation of whiteout), or the
2048+ target (creating) file is whiteout-ed on the upper readonly branch,
2049+ then the result of the policy is ignored and the target file will be
2050+ created on the nearest upper writable branch than the readonly branch.
2051+- If there is a writable branch above the policy-selected branch and
2052+ the parent dir is marked as opaque or the target file is whiteouted
2053+ on the branch, then the result of the policy is ignored and the target
2054+ file will be created on the highest one among the upper writable
2055+ branches who has diropq or whiteout. In case of whiteout, aufs removes
2056+ it as usual.
2057+- link(2) and rename(2) systemcalls are exceptions in every policy.
2058+ They try selecting the branch where the source exists as possible
2059+ since copyup a large file will take long time. If it can't be,
2060+ ie. the branch where the source exists is readonly, then they will
2061+ follow the copyup policy.
2062+- There is an exception for rename(2) when the target exists.
2063+ If the rename target exists, aufs compares the index of the branches
2064+ where the source and the target exists and selects the higher
2065+ one. If the selected branch is readonly, then aufs follows the
2066+ copyup policy.
8b6a4947
AM
2067diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
2068--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
2069+++ linux/Documentation/filesystems/aufs/design/06dirren.dot 2017-11-12 22:24:44.694244127 +0100
2070@@ -0,0 +1,31 @@
2071+
2072+// to view this graph, run dot(1) command in GRAPHVIZ.
2073+
2074+digraph G {
2075+node [shape=box];
2076+whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
2077+
2078+node [shape=oval];
2079+
2080+aufs_rename -> whinfo [label="store/remove"];
2081+
2082+node [shape=oval];
2083+inode_list [label="h_inum list in branch\ncache"];
2084+
2085+node [shape=box];
2086+whinode [label="h_inum list file"];
2087+
2088+node [shape=oval];
2089+brmgmt [label="br_add/del/mod/umount"];
2090+
2091+brmgmt -> inode_list [label="create/remove"];
2092+brmgmt -> whinode [label="load/store"];
2093+
2094+inode_list -> whinode [style=dashed,dir=both];
2095+
2096+aufs_rename -> inode_list [label="add/del"];
2097+
2098+aufs_lookup -> inode_list [label="search"];
2099+
2100+aufs_lookup -> whinfo [label="load/remove"];
2101+}
2102diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
2103--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
2104+++ linux/Documentation/filesystems/aufs/design/06dirren.txt 2017-11-12 22:24:44.694244127 +0100
2105@@ -0,0 +1,102 @@
2106+
2107+# Copyright (C) 2017 Junjiro R. Okajima
2108+#
2109+# This program is free software; you can redistribute it and/or modify
2110+# it under the terms of the GNU General Public License as published by
2111+# the Free Software Foundation; either version 2 of the License, or
2112+# (at your option) any later version.
2113+#
2114+# This program is distributed in the hope that it will be useful,
2115+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2116+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2117+# GNU General Public License for more details.
2118+#
2119+# You should have received a copy of the GNU General Public License
2120+# along with this program. If not, see <http://www.gnu.org/licenses/>.
2121+
2122+Special handling for renaming a directory (DIRREN)
2123+----------------------------------------------------------------------
2124+First, let's assume we have a simple usecase.
2125+
2126+- /u = /rw + /ro
2127+- /rw/dirA exists
2128+- /ro/dirA and /ro/dirA/file exist too
2129+- there is no dirB on both branches
2130+- a user issues rename("dirA", "dirB")
2131+
2132+Now, what should aufs behave against this rename(2)?
2133+There are a few possible cases.
2134+
2135+A. returns EROFS.
2136+ since dirA exists on a readonly branch which cannot be renamed.
2137+B. returns EXDEV.
2138+ it is possible to copy-up dirA (only the dir itself), but the child
2139+ entries ("file" in this case) should not be. it must be a bad
2140+ approach to copy-up recursively.
2141+C. returns a success.
2142+ even the branch /ro is readonly, aufs tries renaming it. Obviously it
2143+ is a violation of aufs' policy.
2144+D. construct an extra information which indicates that /ro/dirA should
2145+ be handled as the name of dirB.
2146+ overlayfs has a similar feature called REDIRECT.
2147+
2148+Until now, aufs implements the case B only which returns EXDEV, and
2149+expects the userspace application behaves like mv(1) which tries
2150+issueing rename(2) recursively.
2151+
2152+A new aufs feature called DIRREN is introduced which implements the case
2153+D. There are several "extra information" added.
2154+
2155+1. detailed info per renamed directory
2156+ path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
2157+2. the inode-number list of directories on a branch
2158+ path: /rw/dirB/$AUFS_WH_DR_BRHINO
2159+
2160+The filename of "detailed info per directory" represents the lower
2161+branch, and its format is
2162+- a type of the branch id
2163+ one of these.
2164+ + uuid (not implemented yet)
2165+ + fsid
2166+ + dev
2167+- the inode-number of the branch root dir
2168+
2169+And it contains these info in a single regular file.
2170+- magic number
2171+- branch's inode-number of the logically renamed dir
2172+- the name of the before-renamed dir
2173+
2174+The "detailed info per directory" file is created in aufs rename(2), and
2175+loaded in any lookup.
2176+The info is considered in lookup for the matching case only. Here
2177+"matching" means that the root of branch (in the info filename) is same
2178+to the current looking-up branch. After looking-up the before-renamed
2179+name, the inode-number is compared. And the matched dentry is used.
2180+
2181+The "inode-number list of directories" is a regular file which contains
2182+simply the inode-numbers on the branch. The file is created or updated
2183+in removing the branch, and loaded in adding the branch. Its lifetime is
2184+equal to the branch.
2185+The list is refered in lookup, and when the current target inode is
2186+found in the list, the aufs tries loading the "detailed info per
2187+directory" and get the changed and valid name of the dir.
2188+
2189+Theoretically these "extra informaiton" may be able to be put into XATTR
2190+in the dir inode. But aufs doesn't choose this way because
2191+1. XATTR may not be supported by the branch (or its configuration)
2192+2. XATTR may have its size limit.
2193+3. XATTR may be less easy to convert than a regular file, when the
2194+ format of the info is changed in the future.
2195+At the same time, I agree that the regular file approach is much slower
2196+than XATTR approach. So, in the future, aufs may take the XATTR or other
2197+better approach.
2198+
2199+This DIRREN feature is enabled by aufs configuration, and is activated
2200+by a new mount option.
2201+
2202+For the more complicated case, there is a work with UDBA option, which
2203+is to dected the direct access to the branches (by-passing aufs) and to
2204+maintain the cashes in aufs. Since a single cached aufs dentry may
2205+contains two names, before- and after-rename, the name comparision in
2206+UDBA handler may not work correctly. In this case, the behaviour will be
2207+equivalen to udba=reval case.
076b876e
AM
2208diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
2209--- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt 1970-01-01 01:00:00.000000000 +0100
1c60b727 2210+++ linux/Documentation/filesystems/aufs/design/06fhsm.txt 2017-07-29 12:14:25.896375188 +0200
076b876e
AM
2211@@ -0,0 +1,120 @@
2212+
a2654f78 2213+# Copyright (C) 2011-2017 Junjiro R. Okajima
076b876e
AM
2214+#
2215+# This program is free software; you can redistribute it and/or modify
2216+# it under the terms of the GNU General Public License as published by
2217+# the Free Software Foundation; either version 2 of the License, or
2218+# (at your option) any later version.
2219+#
2220+# This program is distributed in the hope that it will be useful,
2221+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2222+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2223+# GNU General Public License for more details.
2224+#
2225+# You should have received a copy of the GNU General Public License
2226+# along with this program; if not, write to the Free Software
2227+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2228+
2229+
2230+File-based Hierarchical Storage Management (FHSM)
2231+----------------------------------------------------------------------
2232+Hierarchical Storage Management (or HSM) is a well-known feature in the
2233+storage world. Aufs provides this feature as file-based with multiple
7e9cd9fe 2234+writable branches, based upon the principle of "Colder, the Lower".
076b876e 2235+Here the word "colder" means that the less used files, and "lower" means
7e9cd9fe 2236+that the position in the order of the stacked branches vertically.
076b876e
AM
2237+These multiple writable branches are prioritized, ie. the topmost one
2238+should be the fastest drive and be used heavily.
2239+
2240+o Characters in aufs FHSM story
2241+- aufs itself and a new branch attribute.
2242+- a new ioctl interface to move-down and to establish a connection with
2243+ the daemon ("move-down" is a converse of "copy-up").
2244+- userspace tool and daemon.
2245+
2246+The userspace daemon establishes a connection with aufs and waits for
2247+the notification. The notified information is very similar to struct
2248+statfs containing the number of consumed blocks and inodes.
2249+When the consumed blocks/inodes of a branch exceeds the user-specified
2250+upper watermark, the daemon activates its move-down process until the
2251+consumed blocks/inodes reaches the user-specified lower watermark.
2252+
2253+The actual move-down is done by aufs based upon the request from
2254+user-space since we need to maintain the inode number and the internal
2255+pointer arrays in aufs.
2256+
2257+Currently aufs FHSM handles the regular files only. Additionally they
2258+must not be hard-linked nor pseudo-linked.
2259+
2260+
2261+o Cowork of aufs and the user-space daemon
2262+ During the userspace daemon established the connection, aufs sends a
2263+ small notification to it whenever aufs writes something into the
2264+ writable branch. But it may cost high since aufs issues statfs(2)
2265+ internally. So user can specify a new option to cache the
2266+ info. Actually the notification is controlled by these factors.
2267+ + the specified cache time.
2268+ + classified as "force" by aufs internally.
2269+ Until the specified time expires, aufs doesn't send the info
2270+ except the forced cases. When aufs decide forcing, the info is always
2271+ notified to userspace.
2272+ For example, the number of free inodes is generally large enough and
2273+ the shortage of it happens rarely. So aufs doesn't force the
2274+ notification when creating a new file, directory and others. This is
2275+ the typical case which aufs doesn't force.
2276+ When aufs writes the actual filedata and the files consumes any of new
2277+ blocks, the aufs forces notifying.
2278+
2279+
2280+o Interfaces in aufs
2281+- New branch attribute.
2282+ + fhsm
2283+ Specifies that the branch is managed by FHSM feature. In other word,
2284+ participant in the FHSM.
2285+ When nofhsm is set to the branch, it will not be the source/target
2286+ branch of the move-down operation. This attribute is set
2287+ independently from coo and moo attributes, and if you want full
2288+ FHSM, you should specify them as well.
2289+- New mount option.
2290+ + fhsm_sec
2291+ Specifies a second to suppress many less important info to be
2292+ notified.
2293+- New ioctl.
2294+ + AUFS_CTL_FHSM_FD
2295+ create a new file descriptor which userspace can read the notification
2296+ (a subset of struct statfs) from aufs.
2297+- Module parameter 'brs'
2298+ It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2299+ be set.
2300+- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2301+ When there are two or more branches with fhsm attributes,
2302+ /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2303+ terminates it. As a result of remounting and branch-manipulation, the
2304+ number of branches with fhsm attribute can be one. In this case,
2305+ /sbin/mount.aufs will terminate the user-space daemon.
2306+
2307+
2308+Finally the operation is done as these steps in kernel-space.
2309+- make sure that,
2310+ + no one else is using the file.
2311+ + the file is not hard-linked.
2312+ + the file is not pseudo-linked.
2313+ + the file is a regular file.
2314+ + the parent dir is not opaqued.
2315+- find the target writable branch.
2316+- make sure the file is not whiteout-ed by the upper (than the target)
2317+ branch.
2318+- make the parent dir on the target branch.
2319+- mutex lock the inode on the branch.
2320+- unlink the whiteout on the target branch (if exists).
2321+- lookup and create the whiteout-ed temporary name on the target branch.
2322+- copy the file as the whiteout-ed temporary name on the target branch.
2323+- rename the whiteout-ed temporary name to the original name.
2324+- unlink the file on the source branch.
2325+- maintain the internal pointer array and the external inode number
2326+ table (XINO).
2327+- maintain the timestamps and other attributes of the parent dir and the
2328+ file.
2329+
2330+And of course, in every step, an error may happen. So the operation
2331+should restore the original file state after an error happens.
53392da6
AM
2332diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2333--- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt 1970-01-01 01:00:00.000000000 +0100
1c60b727 2334+++ linux/Documentation/filesystems/aufs/design/06mmap.txt 2017-07-29 12:14:25.896375188 +0200
b912730e 2335@@ -0,0 +1,72 @@
53392da6 2336+
a2654f78 2337+# Copyright (C) 2005-2017 Junjiro R. Okajima
53392da6
AM
2338+#
2339+# This program is free software; you can redistribute it and/or modify
2340+# it under the terms of the GNU General Public License as published by
2341+# the Free Software Foundation; either version 2 of the License, or
2342+# (at your option) any later version.
2343+#
2344+# This program is distributed in the hope that it will be useful,
2345+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2346+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2347+# GNU General Public License for more details.
2348+#
2349+# You should have received a copy of the GNU General Public License
523b37e3 2350+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2351+
2352+mmap(2) -- File Memory Mapping
2353+----------------------------------------------------------------------
2354+In aufs, the file-mapped pages are handled by a branch fs directly, no
2355+interaction with aufs. It means aufs_mmap() calls the branch fs's
2356+->mmap().
2357+This approach is simple and good, but there is one problem.
7e9cd9fe 2358+Under /proc, several entries show the mmapped files by its path (with
53392da6
AM
2359+device and inode number), and the printed path will be the path on the
2360+branch fs's instead of virtual aufs's.
2361+This is not a problem in most cases, but some utilities lsof(1) (and its
2362+user) may expect the path on aufs.
2363+
2364+To address this issue, aufs adds a new member called vm_prfile in struct
2365+vm_area_struct (and struct vm_region). The original vm_file points to
2366+the file on the branch fs in order to handle everything correctly as
2367+usual. The new vm_prfile points to a virtual file in aufs, and the
2368+show-functions in procfs refers to vm_prfile if it is set.
2369+Also we need to maintain several other places where touching vm_file
2370+such like
2371+- fork()/clone() copies vma and the reference count of vm_file is
2372+ incremented.
2373+- merging vma maintains the ref count too.
2374+
7e9cd9fe 2375+This is not a good approach. It just fakes the printed path. But it
53392da6
AM
2376+leaves all behaviour around f_mapping unchanged. This is surely an
2377+advantage.
2378+Actually aufs had adopted another complicated approach which calls
2379+generic_file_mmap() and handles struct vm_operations_struct. In this
2380+approach, aufs met a hard problem and I could not solve it without
2381+switching the approach.
b912730e
AM
2382+
2383+There may be one more another approach which is
2384+- bind-mount the branch-root onto the aufs-root internally
2385+- grab the new vfsmount (ie. struct mount)
2386+- lazy-umount the branch-root internally
2387+- in open(2) the aufs-file, open the branch-file with the hidden
2388+ vfsmount (instead of the original branch's vfsmount)
2389+- ideally this "bind-mount and lazy-umount" should be done atomically,
2390+ but it may be possible from userspace by the mount helper.
2391+
2392+Adding the internal hidden vfsmount and using it in opening a file, the
2393+file path under /proc will be printed correctly. This approach looks
2394+smarter, but is not possible I am afraid.
2395+- aufs-root may be bind-mount later. when it happens, another hidden
2396+ vfsmount will be required.
2397+- it is hard to get the chance to bind-mount and lazy-umount
2398+ + in kernel-space, FS can have vfsmount in open(2) via
2399+ file->f_path, and aufs can know its vfsmount. But several locks are
2400+ already acquired, and if aufs tries to bind-mount and lazy-umount
2401+ here, then it may cause a deadlock.
2402+ + in user-space, bind-mount doesn't invoke the mount helper.
2403+- since /proc shows dev and ino, aufs has to give vma these info. it
2404+ means a new member vm_prinode will be necessary. this is essentially
2405+ equivalent to vm_prfile described above.
2406+
2407+I have to give up this "looks-smater" approach.
c1595e42
JR
2408diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2409--- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt 1970-01-01 01:00:00.000000000 +0100
1c60b727 2410+++ linux/Documentation/filesystems/aufs/design/06xattr.txt 2017-07-29 12:14:25.896375188 +0200
c1595e42
JR
2411@@ -0,0 +1,96 @@
2412+
a2654f78 2413+# Copyright (C) 2014-2017 Junjiro R. Okajima
c1595e42
JR
2414+#
2415+# This program is free software; you can redistribute it and/or modify
2416+# it under the terms of the GNU General Public License as published by
2417+# the Free Software Foundation; either version 2 of the License, or
2418+# (at your option) any later version.
2419+#
2420+# This program is distributed in the hope that it will be useful,
2421+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2422+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2423+# GNU General Public License for more details.
2424+#
2425+# You should have received a copy of the GNU General Public License
2426+# along with this program; if not, write to the Free Software
2427+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2428+
2429+
2430+Listing XATTR/EA and getting the value
2431+----------------------------------------------------------------------
2432+For the inode standard attributes (owner, group, timestamps, etc.), aufs
2433+shows the values from the topmost existing file. This behaviour is good
7e9cd9fe 2434+for the non-dir entries since the bahaviour exactly matches the shown
c1595e42
JR
2435+information. But for the directories, aufs considers all the same named
2436+entries on the lower branches. Which means, if one of the lower entry
2437+rejects readdir call, then aufs returns an error even if the topmost
2438+entry allows it. This behaviour is necessary to respect the branch fs's
2439+security, but can make users confused since the user-visible standard
2440+attributes don't match the behaviour.
2441+To address this issue, aufs has a mount option called dirperm1 which
2442+checks the permission for the topmost entry only, and ignores the lower
2443+entry's permission.
2444+
2445+A similar issue can happen around XATTR.
2446+getxattr(2) and listxattr(2) families behave as if dirperm1 option is
7e9cd9fe
AM
2447+always set. Otherwise these very unpleasant situation would happen.
2448+- listxattr(2) may return the duplicated entries.
c1595e42
JR
2449+- users may not be able to remove or reset the XATTR forever,
2450+
2451+
2452+XATTR/EA support in the internal (copy,move)-(up,down)
2453+----------------------------------------------------------------------
7e9cd9fe 2454+Generally the extended attributes of inode are categorized as these.
c1595e42
JR
2455+- "security" for LSM and capability.
2456+- "system" for posix ACL, 'acl' mount option is required for the branch
2457+ fs generally.
2458+- "trusted" for userspace, CAP_SYS_ADMIN is required.
2459+- "user" for userspace, 'user_xattr' mount option is required for the
2460+ branch fs generally.
2461+
2462+Moreover there are some other categories. Aufs handles these rather
2463+unpopular categories as the ordinary ones, ie. there is no special
2464+condition nor exception.
2465+
2466+In copy-up, the support for XATTR on the dst branch may differ from the
2467+src branch. In this case, the copy-up operation will get an error and
7e9cd9fe
AM
2468+the original user operation which triggered the copy-up will fail. It
2469+can happen that even all copy-up will fail.
c1595e42
JR
2470+When both of src and dst branches support XATTR and if an error occurs
2471+during copying XATTR, then the copy-up should fail obviously. That is a
2472+good reason and aufs should return an error to userspace. But when only
7e9cd9fe 2473+the src branch support that XATTR, aufs should not return an error.
c1595e42
JR
2474+For example, the src branch supports ACL but the dst branch doesn't
2475+because the dst branch may natively un-support it or temporary
2476+un-support it due to "noacl" mount option. Of course, the dst branch fs
2477+may NOT return an error even if the XATTR is not supported. It is
2478+totally up to the branch fs.
2479+
2480+Anyway when the aufs internal copy-up gets an error from the dst branch
2481+fs, then aufs tries removing the just copied entry and returns the error
2482+to the userspace. The worst case of this situation will be all copy-up
2483+will fail.
2484+
2485+For the copy-up operation, there two basic approaches.
2486+- copy the specified XATTR only (by category above), and return the
7e9cd9fe 2487+ error unconditionally if it happens.
c1595e42
JR
2488+- copy all XATTR, and ignore the error on the specified category only.
2489+
2490+In order to support XATTR and to implement the correct behaviour, aufs
7e9cd9fe
AM
2491+chooses the latter approach and introduces some new branch attributes,
2492+"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
c1595e42 2493+They correspond to the XATTR namespaces (see above). Additionally, to be
7e9cd9fe
AM
2494+convenient, "icex" is also provided which means all "icex*" attributes
2495+are set (here the word "icex" stands for "ignore copy-error on XATTR").
c1595e42
JR
2496+
2497+The meaning of these attributes is to ignore the error from setting
2498+XATTR on that branch.
2499+Note that aufs tries copying all XATTR unconditionally, and ignores the
2500+error from the dst branch according to the specified attributes.
2501+
2502+Some XATTR may have its default value. The default value may come from
2503+the parent dir or the environment. If the default value is set at the
2504+file creating-time, it will be overwritten by copy-up.
2505+Some contradiction may happen I am afraid.
2506+Do we need another attribute to stop copying XATTR? I am unsure. For
2507+now, aufs implements the branch attributes to ignore the error.
53392da6
AM
2508diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2509--- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
1c60b727 2510+++ linux/Documentation/filesystems/aufs/design/07export.txt 2017-07-29 12:14:25.896375188 +0200
523b37e3 2511@@ -0,0 +1,58 @@
53392da6 2512+
a2654f78 2513+# Copyright (C) 2005-2017 Junjiro R. Okajima
53392da6
AM
2514+#
2515+# This program is free software; you can redistribute it and/or modify
2516+# it under the terms of the GNU General Public License as published by
2517+# the Free Software Foundation; either version 2 of the License, or
2518+# (at your option) any later version.
2519+#
2520+# This program is distributed in the hope that it will be useful,
2521+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2522+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2523+# GNU General Public License for more details.
2524+#
2525+# You should have received a copy of the GNU General Public License
523b37e3 2526+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2527+
2528+Export Aufs via NFS
2529+----------------------------------------------------------------------
2530+Here is an approach.
2531+- like xino/xib, add a new file 'xigen' which stores aufs inode
2532+ generation.
2533+- iget_locked(): initialize aufs inode generation for a new inode, and
2534+ store it in xigen file.
2535+- destroy_inode(): increment aufs inode generation and store it in xigen
2536+ file. it is necessary even if it is not unlinked, because any data of
2537+ inode may be changed by UDBA.
2538+- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2539+ build file handle by
2540+ + branch id (4 bytes)
2541+ + superblock generation (4 bytes)
2542+ + inode number (4 or 8 bytes)
2543+ + parent dir inode number (4 or 8 bytes)
2544+ + inode generation (4 bytes))
2545+ + return value of exportfs_encode_fh() for the parent on a branch (4
2546+ bytes)
2547+ + file handle for a branch (by exportfs_encode_fh())
2548+- fh_to_dentry():
2549+ + find the index of a branch from its id in handle, and check it is
2550+ still exist in aufs.
2551+ + 1st level: get the inode number from handle and search it in cache.
7e9cd9fe
AM
2552+ + 2nd level: if not found in cache, get the parent inode number from
2553+ the handle and search it in cache. and then open the found parent
2554+ dir, find the matching inode number by vfs_readdir() and get its
2555+ name, and call lookup_one_len() for the target dentry.
53392da6
AM
2556+ + 3rd level: if the parent dir is not cached, call
2557+ exportfs_decode_fh() for a branch and get the parent on a branch,
2558+ build a pathname of it, convert it a pathname in aufs, call
2559+ path_lookup(). now aufs gets a parent dir dentry, then handle it as
2560+ the 2nd level.
2561+ + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2562+ for every branch, but not itself. to get this, (currently) aufs
2563+ searches in current->nsproxy->mnt_ns list. it may not be a good
2564+ idea, but I didn't get other approach.
2565+ + test the generation of the gotten inode.
2566+- every inode operation: they may get EBUSY due to UDBA. in this case,
2567+ convert it into ESTALE for NFSD.
2568+- readdir(): call lockdep_on/off() because filldir in NFSD calls
2569+ lookup_one_len(), vfs_getattr(), encode_fh() and others.
2570diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2571--- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt 1970-01-01 01:00:00.000000000 +0100
1c60b727 2572+++ linux/Documentation/filesystems/aufs/design/08shwh.txt 2017-07-29 12:14:25.896375188 +0200
523b37e3 2573@@ -0,0 +1,52 @@
53392da6 2574+
a2654f78 2575+# Copyright (C) 2005-2017 Junjiro R. Okajima
53392da6
AM
2576+#
2577+# This program is free software; you can redistribute it and/or modify
2578+# it under the terms of the GNU General Public License as published by
2579+# the Free Software Foundation; either version 2 of the License, or
2580+# (at your option) any later version.
2581+#
2582+# This program is distributed in the hope that it will be useful,
2583+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2584+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2585+# GNU General Public License for more details.
2586+#
2587+# You should have received a copy of the GNU General Public License
523b37e3 2588+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2589+
2590+Show Whiteout Mode (shwh)
2591+----------------------------------------------------------------------
2592+Generally aufs hides the name of whiteouts. But in some cases, to show
2593+them is very useful for users. For instance, creating a new middle layer
2594+(branch) by merging existing layers.
2595+
2596+(borrowing aufs1 HOW-TO from a user, Michael Towers)
2597+When you have three branches,
2598+- Bottom: 'system', squashfs (underlying base system), read-only
2599+- Middle: 'mods', squashfs, read-only
2600+- Top: 'overlay', ram (tmpfs), read-write
2601+
2602+The top layer is loaded at boot time and saved at shutdown, to preserve
2603+the changes made to the system during the session.
2604+When larger changes have been made, or smaller changes have accumulated,
2605+the size of the saved top layer data grows. At this point, it would be
2606+nice to be able to merge the two overlay branches ('mods' and 'overlay')
2607+and rewrite the 'mods' squashfs, clearing the top layer and thus
2608+restoring save and load speed.
2609+
2610+This merging is simplified by the use of another aufs mount, of just the
2611+two overlay branches using the 'shwh' option.
2612+# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2613+ aufs /livesys/merge_union
2614+
2615+A merged view of these two branches is then available at
2616+/livesys/merge_union, and the new feature is that the whiteouts are
2617+visible!
2618+Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2619+writing to all branches. Also the default mode for all branches is 'ro'.
2620+It is now possible to save the combined contents of the two overlay
2621+branches to a new squashfs, e.g.:
2622+# mksquashfs /livesys/merge_union /path/to/newmods.squash
2623+
2624+This new squashfs archive can be stored on the boot device and the
2625+initramfs will use it to replace the old one at the next boot.
2626diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2627--- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt 1970-01-01 01:00:00.000000000 +0100
1c60b727 2628+++ linux/Documentation/filesystems/aufs/design/10dynop.txt 2017-07-29 12:14:25.896375188 +0200
7e9cd9fe 2629@@ -0,0 +1,47 @@
53392da6 2630+
a2654f78 2631+# Copyright (C) 2010-2017 Junjiro R. Okajima
53392da6
AM
2632+#
2633+# This program is free software; you can redistribute it and/or modify
2634+# it under the terms of the GNU General Public License as published by
2635+# the Free Software Foundation; either version 2 of the License, or
2636+# (at your option) any later version.
2637+#
2638+# This program is distributed in the hope that it will be useful,
2639+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2640+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2641+# GNU General Public License for more details.
2642+#
2643+# You should have received a copy of the GNU General Public License
523b37e3 2644+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2645+
2646+Dynamically customizable FS operations
2647+----------------------------------------------------------------------
2648+Generally FS operations (struct inode_operations, struct
2649+address_space_operations, struct file_operations, etc.) are defined as
2650+"static const", but it never means that FS have only one set of
2651+operation. Some FS have multiple sets of them. For instance, ext2 has
2652+three sets, one for XIP, for NOBH, and for normal.
2653+Since aufs overrides and redirects these operations, sometimes aufs has
7e9cd9fe 2654+to change its behaviour according to the branch FS type. More importantly
53392da6
AM
2655+VFS acts differently if a function (member in the struct) is set or
2656+not. It means aufs should have several sets of operations and select one
2657+among them according to the branch FS definition.
2658+
7e9cd9fe 2659+In order to solve this problem and not to affect the behaviour of VFS,
53392da6 2660+aufs defines these operations dynamically. For instance, aufs defines
7e9cd9fe
AM
2661+dummy direct_IO function for struct address_space_operations, but it may
2662+not be set to the address_space_operations actually. When the branch FS
2663+doesn't have it, aufs doesn't set it to its address_space_operations
2664+while the function definition itself is still alive. So the behaviour
2665+itself will not change, and it will return an error when direct_IO is
2666+not set.
53392da6
AM
2667+
2668+The lifetime of these dynamically generated operation object is
2669+maintained by aufs branch object. When the branch is removed from aufs,
2670+the reference counter of the object is decremented. When it reaches
2671+zero, the dynamically generated operation object will be freed.
2672+
7e9cd9fe
AM
2673+This approach is designed to support AIO (io_submit), Direct I/O and
2674+XIP (DAX) mainly.
2675+Currently this approach is applied to address_space_operations for
2676+regular files only.
53392da6
AM
2677diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2678--- /usr/share/empty/Documentation/filesystems/aufs/README 1970-01-01 01:00:00.000000000 +0100
1c60b727 2679+++ linux/Documentation/filesystems/aufs/README 2017-07-29 12:14:25.893041746 +0200
f2c43d5f 2680@@ -0,0 +1,393 @@
53392da6 2681+
5527c038 2682+Aufs4 -- advanced multi layered unification filesystem version 4.x
53392da6
AM
2683+http://aufs.sf.net
2684+Junjiro R. Okajima
2685+
2686+
2687+0. Introduction
2688+----------------------------------------
2689+In the early days, aufs was entirely re-designed and re-implemented
7e9cd9fe 2690+Unionfs Version 1.x series. Adding many original ideas, approaches,
53392da6
AM
2691+improvements and implementations, it becomes totally different from
2692+Unionfs while keeping the basic features.
2693+Recently, Unionfs Version 2.x series begin taking some of the same
2694+approaches to aufs1's.
2695+Unionfs is being developed by Professor Erez Zadok at Stony Brook
2696+University and his team.
2697+
5527c038 2698+Aufs4 supports linux-4.0 and later, and for linux-3.x series try aufs3.
53392da6
AM
2699+If you want older kernel version support, try aufs2-2.6.git or
2700+aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
2701+
2702+Note: it becomes clear that "Aufs was rejected. Let's give it up."
38d290e6
JR
2703+ According to Christoph Hellwig, linux rejects all union-type
2704+ filesystems but UnionMount.
53392da6
AM
2705+<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2706+
38d290e6
JR
2707+PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2708+ UnionMount, and he pointed out an issue around a directory mutex
2709+ lock and aufs addressed it. But it is still unsure whether aufs will
2710+ be merged (or any other union solution).
076b876e 2711+<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
38d290e6 2712+
53392da6
AM
2713+
2714+1. Features
2715+----------------------------------------
2716+- unite several directories into a single virtual filesystem. The member
2717+ directory is called as a branch.
2718+- you can specify the permission flags to the branch, which are 'readonly',
2719+ 'readwrite' and 'whiteout-able.'
2720+- by upper writable branch, internal copyup and whiteout, files/dirs on
2721+ readonly branch are modifiable logically.
2722+- dynamic branch manipulation, add, del.
2723+- etc...
2724+
7e9cd9fe
AM
2725+Also there are many enhancements in aufs, such as:
2726+- test only the highest one for the directory permission (dirperm1)
2727+- copyup on open (coo=)
2728+- 'move' policy for copy-up between two writable branches, after
2729+ checking free space.
2730+- xattr, acl
53392da6
AM
2731+- readdir(3) in userspace.
2732+- keep inode number by external inode number table
2733+- keep the timestamps of file/dir in internal copyup operation
2734+- seekable directory, supporting NFS readdir.
2735+- whiteout is hardlinked in order to reduce the consumption of inodes
2736+ on branch
2737+- do not copyup, nor create a whiteout when it is unnecessary
2738+- revert a single systemcall when an error occurs in aufs
2739+- remount interface instead of ioctl
2740+- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2741+- loopback mounted filesystem as a branch
2742+- kernel thread for removing the dir who has a plenty of whiteouts
2743+- support copyup sparse file (a file which has a 'hole' in it)
2744+- default permission flags for branches
2745+- selectable permission flags for ro branch, whether whiteout can
2746+ exist or not
2747+- export via NFS.
2748+- support <sysfs>/fs/aufs and <debugfs>/aufs.
2749+- support multiple writable branches, some policies to select one
2750+ among multiple writable branches.
2751+- a new semantics for link(2) and rename(2) to support multiple
2752+ writable branches.
2753+- no glibc changes are required.
2754+- pseudo hardlink (hardlink over branches)
2755+- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2756+ including NFS or remote filesystem branch.
2757+- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2758+- and more...
2759+
5527c038 2760+Currently these features are dropped temporary from aufs4.
53392da6 2761+See design/08plan.txt in detail.
53392da6
AM
2762+- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2763+ (robr)
2764+- statistics of aufs thread (/sys/fs/aufs/stat)
53392da6
AM
2765+
2766+Features or just an idea in the future (see also design/*.txt),
2767+- reorder the branch index without del/re-add.
2768+- permanent xino files for NFSD
2769+- an option for refreshing the opened files after add/del branches
53392da6
AM
2770+- light version, without branch manipulation. (unnecessary?)
2771+- copyup in userspace
2772+- inotify in userspace
2773+- readv/writev
53392da6
AM
2774+
2775+
2776+2. Download
2777+----------------------------------------
5527c038
JR
2778+There are three GIT trees for aufs4, aufs4-linux.git,
2779+aufs4-standalone.git, and aufs-util.git. Note that there is no "4" in
1e00d052 2780+"aufs-util.git."
5527c038
JR
2781+While the aufs-util is always necessary, you need either of aufs4-linux
2782+or aufs4-standalone.
1e00d052 2783+
5527c038 2784+The aufs4-linux tree includes the whole linux mainline GIT tree,
1e00d052
AM
2785+git://git.kernel.org/.../torvalds/linux.git.
2786+And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
5527c038 2787+build aufs4 as an external kernel module.
2000de60 2788+Several extra patches are not included in this tree. Only
be52b249 2789+aufs4-standalone tree contains them. They are described in the later
2000de60 2790+section "Configuration and Compilation."
1e00d052 2791+
5527c038 2792+On the other hand, the aufs4-standalone tree has only aufs source files
53392da6 2793+and necessary patches, and you can select CONFIG_AUFS_FS=m.
2000de60 2794+But you need to apply all aufs patches manually.
53392da6 2795+
5527c038
JR
2796+You will find GIT branches whose name is in form of "aufs4.x" where "x"
2797+represents the linux kernel version, "linux-4.x". For instance,
2798+"aufs4.0" is for linux-4.0. For latest "linux-4.x-rcN", use
2799+"aufs4.x-rcN" branch.
1e00d052 2800+
5527c038 2801+o aufs4-linux tree
1e00d052 2802+$ git clone --reference /your/linux/git/tree \
5527c038 2803+ git://github.com/sfjro/aufs4-linux.git aufs4-linux.git
1e00d052 2804+- if you don't have linux GIT tree, then remove "--reference ..."
5527c038
JR
2805+$ cd aufs4-linux.git
2806+$ git checkout origin/aufs4.0
53392da6 2807+
2000de60
JR
2808+Or You may want to directly git-pull aufs into your linux GIT tree, and
2809+leave the patch-work to GIT.
2810+$ cd /your/linux/git/tree
5527c038
JR
2811+$ git remote add aufs4 git://github.com/sfjro/aufs4-linux.git
2812+$ git fetch aufs4
2813+$ git checkout -b my4.0 v4.0
2814+$ (add your local change...)
2815+$ git pull aufs4 aufs4.0
2816+- now you have v4.0 + your_changes + aufs4.0 in you my4.0 branch.
2000de60 2817+- you may need to solve some conflicts between your_changes and
5527c038
JR
2818+ aufs4.0. in this case, git-rerere is recommended so that you can
2819+ solve the similar conflicts automatically when you upgrade to 4.1 or
2000de60
JR
2820+ later in the future.
2821+
5527c038
JR
2822+o aufs4-standalone tree
2823+$ git clone git://github.com/sfjro/aufs4-standalone.git aufs4-standalone.git
2824+$ cd aufs4-standalone.git
2825+$ git checkout origin/aufs4.0
53392da6
AM
2826+
2827+o aufs-util tree
5527c038
JR
2828+$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2829+- note that the public aufs-util.git is on SourceForge instead of
2830+ GitHUB.
53392da6 2831+$ cd aufs-util.git
5527c038 2832+$ git checkout origin/aufs4.0
53392da6 2833+
5527c038
JR
2834+Note: The 4.x-rcN branch is to be used with `rc' kernel versions ONLY.
2835+The minor version number, 'x' in '4.x', of aufs may not always
9dbd164d
AM
2836+follow the minor version number of the kernel.
2837+Because changes in the kernel that cause the use of a new
2838+minor version number do not always require changes to aufs-util.
2839+
2840+Since aufs-util has its own minor version number, you may not be
2841+able to find a GIT branch in aufs-util for your kernel's
2842+exact minor version number.
2843+In this case, you should git-checkout the branch for the
53392da6 2844+nearest lower number.
9dbd164d
AM
2845+
2846+For (an unreleased) example:
5527c038
JR
2847+If you are using "linux-4.10" and the "aufs4.10" branch
2848+does not exist in aufs-util repository, then "aufs4.9", "aufs4.8"
9dbd164d
AM
2849+or something numerically smaller is the branch for your kernel.
2850+
53392da6
AM
2851+Also you can view all branches by
2852+ $ git branch -a
2853+
2854+
2855+3. Configuration and Compilation
2856+----------------------------------------
2857+Make sure you have git-checkout'ed the correct branch.
2858+
5527c038 2859+For aufs4-linux tree,
c06a8ce3 2860+- enable CONFIG_AUFS_FS.
1e00d052
AM
2861+- set other aufs configurations if necessary.
2862+
5527c038 2863+For aufs4-standalone tree,
53392da6
AM
2864+There are several ways to build.
2865+
2866+1.
5527c038
JR
2867+- apply ./aufs4-kbuild.patch to your kernel source files.
2868+- apply ./aufs4-base.patch too.
2869+- apply ./aufs4-mmap.patch too.
2870+- apply ./aufs4-standalone.patch too, if you have a plan to set
2871+ CONFIG_AUFS_FS=m. otherwise you don't need ./aufs4-standalone.patch.
537831f9
AM
2872+- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2873+ kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
c06a8ce3 2874+- enable CONFIG_AUFS_FS, you can select either
53392da6
AM
2875+ =m or =y.
2876+- and build your kernel as usual.
2877+- install the built kernel.
c06a8ce3
AM
2878+ Note: Since linux-3.9, every filesystem module requires an alias
2879+ "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2880+ modules.aliases file if you set CONFIG_AUFS_FS=m.
7eafdf33
AM
2881+- install the header files too by "make headers_install" to the
2882+ directory where you specify. By default, it is $PWD/usr.
b4510431 2883+ "make help" shows a brief note for headers_install.
53392da6
AM
2884+- and reboot your system.
2885+
2886+2.
2887+- module only (CONFIG_AUFS_FS=m).
5527c038
JR
2888+- apply ./aufs4-base.patch to your kernel source files.
2889+- apply ./aufs4-mmap.patch too.
2890+- apply ./aufs4-standalone.patch too.
53392da6
AM
2891+- build your kernel, don't forget "make headers_install", and reboot.
2892+- edit ./config.mk and set other aufs configurations if necessary.
b4510431 2893+ Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
53392da6
AM
2894+ every aufs configurations.
2895+- build the module by simple "make".
c06a8ce3
AM
2896+ Note: Since linux-3.9, every filesystem module requires an alias
2897+ "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2898+ modules.aliases file.
53392da6
AM
2899+- you can specify ${KDIR} make variable which points to your kernel
2900+ source tree.
2901+- install the files
2902+ + run "make install" to install the aufs module, or copy the built
b4510431
AM
2903+ $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2904+ + run "make install_headers" (instead of headers_install) to install
2905+ the modified aufs header file (you can specify DESTDIR which is
2906+ available in aufs standalone version's Makefile only), or copy
2907+ $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2908+ you like manually. By default, the target directory is $PWD/usr.
5527c038 2909+- no need to apply aufs4-kbuild.patch, nor copying source files to your
53392da6
AM
2910+ kernel source tree.
2911+
b4510431 2912+Note: The header file aufs_type.h is necessary to build aufs-util
53392da6
AM
2913+ as well as "make headers_install" in the kernel source tree.
2914+ headers_install is subject to be forgotten, but it is essentially
2915+ necessary, not only for building aufs-util.
2916+ You may not meet problems without headers_install in some older
2917+ version though.
2918+
2919+And then,
2920+- read README in aufs-util, build and install it
9dbd164d
AM
2921+- note that your distribution may contain an obsoleted version of
2922+ aufs_type.h in /usr/include/linux or something. When you build aufs
2923+ utilities, make sure that your compiler refers the correct aufs header
2924+ file which is built by "make headers_install."
53392da6
AM
2925+- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2926+ then run "make install_ulib" too. And refer to the aufs manual in
2927+ detail.
2928+
5527c038 2929+There several other patches in aufs4-standalone.git. They are all
38d290e6 2930+optional. When you meet some problems, they will help you.
5527c038 2931+- aufs4-loopback.patch
38d290e6
JR
2932+ Supports a nested loopback mount in a branch-fs. This patch is
2933+ unnecessary until aufs produces a message like "you may want to try
2934+ another patch for loopback file".
2935+- vfs-ino.patch
2936+ Modifies a system global kernel internal function get_next_ino() in
2937+ order to stop assigning 0 for an inode-number. Not directly related to
2938+ aufs, but recommended generally.
2939+- tmpfs-idr.patch
2940+ Keeps the tmpfs inode number as the lowest value. Effective to reduce
2941+ the size of aufs XINO files for tmpfs branch. Also it prevents the
2942+ duplication of inode number, which is important for backup tools and
2943+ other utilities. When you find aufs XINO files for tmpfs branch
2944+ growing too much, try this patch.
be52b249
AM
2945+- lockdep-debug.patch
2946+ Because aufs is not only an ordinary filesystem (callee of VFS), but
2947+ also a caller of VFS functions for branch filesystems, subclassing of
2948+ the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2949+ feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2950+ need to apply this debug patch to expand several constant values.
2951+ If don't know what LOCKDEP, then you don't have apply this patch.
38d290e6 2952+
53392da6
AM
2953+
2954+4. Usage
2955+----------------------------------------
2956+At first, make sure aufs-util are installed, and please read the aufs
2957+manual, aufs.5 in aufs-util.git tree.
2958+$ man -l aufs.5
2959+
2960+And then,
2961+$ mkdir /tmp/rw /tmp/aufs
2962+# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2963+
2964+Here is another example. The result is equivalent.
2965+# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2966+ Or
2967+# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2968+# mount -o remount,append:${HOME} /tmp/aufs
2969+
2970+Then, you can see whole tree of your home dir through /tmp/aufs. If
2971+you modify a file under /tmp/aufs, the one on your home directory is
2972+not affected, instead the same named file will be newly created under
2973+/tmp/rw. And all of your modification to a file will be applied to
2974+the one under /tmp/rw. This is called the file based Copy on Write
2975+(COW) method.
2976+Aufs mount options are described in aufs.5.
2977+If you run chroot or something and make your aufs as a root directory,
2978+then you need to customize the shutdown script. See the aufs manual in
2979+detail.
2980+
2981+Additionally, there are some sample usages of aufs which are a
2982+diskless system with network booting, and LiveCD over NFS.
2983+See sample dir in CVS tree on SourceForge.
2984+
2985+
2986+5. Contact
2987+----------------------------------------
2988+When you have any problems or strange behaviour in aufs, please let me
2989+know with:
2990+- /proc/mounts (instead of the output of mount(8))
2991+- /sys/module/aufs/*
2992+- /sys/fs/aufs/* (if you have them)
2993+- /debug/aufs/* (if you have them)
2994+- linux kernel version
2995+ if your kernel is not plain, for example modified by distributor,
2996+ the url where i can download its source is necessary too.
2997+- aufs version which was printed at loading the module or booting the
2998+ system, instead of the date you downloaded.
2999+- configuration (define/undefine CONFIG_AUFS_xxx)
3000+- kernel configuration or /proc/config.gz (if you have it)
3001+- behaviour which you think to be incorrect
3002+- actual operation, reproducible one is better
3003+- mailto: aufs-users at lists.sourceforge.net
3004+
3005+Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
3006+and Feature Requests) on SourceForge. Please join and write to
3007+aufs-users ML.
3008+
3009+
3010+6. Acknowledgements
3011+----------------------------------------
3012+Thanks to everyone who have tried and are using aufs, whoever
3013+have reported a bug or any feedback.
3014+
3015+Especially donators:
3016+Tomas Matejicek(slax.org) made a donation (much more than once).
3017+ Since Apr 2010, Tomas M (the author of Slax and Linux Live
3018+ scripts) is making "doubling" donations.
3019+ Unfortunately I cannot list all of the donators, but I really
b4510431 3020+ appreciate.
53392da6
AM
3021+ It ends Aug 2010, but the ordinary donation URL is still available.
3022+ <http://sourceforge.net/donate/index.php?group_id=167503>
3023+Dai Itasaka made a donation (2007/8).
3024+Chuck Smith made a donation (2008/4, 10 and 12).
3025+Henk Schoneveld made a donation (2008/9).
3026+Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
3027+Francois Dupoux made a donation (2008/11).
3028+Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
3029+ aufs2 GIT tree (2009/2).
3030+William Grant made a donation (2009/3).
3031+Patrick Lane made a donation (2009/4).
3032+The Mail Archive (mail-archive.com) made donations (2009/5).
3033+Nippy Networks (Ed Wildgoose) made a donation (2009/7).
3034+New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
3035+Pavel Pronskiy made a donation (2011/2).
3036+Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
3037+ Networks (Ed Wildgoose) made a donation for hardware (2011/3).
537831f9
AM
3038+Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
3039+11).
1e00d052 3040+Sam Liddicott made a donation (2011/9).
86dc4139
AM
3041+Era Scarecrow made a donation (2013/4).
3042+Bor Ratajc made a donation (2013/4).
3043+Alessandro Gorreta made a donation (2013/4).
3044+POIRETTE Marc made a donation (2013/4).
3045+Alessandro Gorreta made a donation (2013/4).
3046+lauri kasvandik made a donation (2013/5).
392086de 3047+"pemasu from Finland" made a donation (2013/7).
523b37e3
AM
3048+The Parted Magic Project made a donation (2013/9 and 11).
3049+Pavel Barta made a donation (2013/10).
38d290e6 3050+Nikolay Pertsev made a donation (2014/5).
c2c0f25c 3051+James B made a donation (2014/7 and 2015/7).
076b876e 3052+Stefano Di Biase made a donation (2014/8).
2000de60 3053+Daniel Epellei made a donation (2015/1).
8cdd5066 3054+OmegaPhil made a donation (2016/1).
5afbbe0d 3055+Tomasz Szewczyk made a donation (2016/4).
f2c43d5f 3056+James Burry made a donation (2016/12).
53392da6
AM
3057+
3058+Thank you very much.
3059+Donations are always, including future donations, very important and
3060+helpful for me to keep on developing aufs.
3061+
3062+
3063+7.
3064+----------------------------------------
3065+If you are an experienced user, no explanation is needed. Aufs is
3066+just a linux filesystem.
3067+
3068+
3069+Enjoy!
3070+
3071+# Local variables: ;
3072+# mode: text;
3073+# End: ;
7f207e10
AM
3074diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
3075--- /usr/share/empty/fs/aufs/aufs.h 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
3076+++ linux/fs/aufs/aufs.h 2017-11-12 22:24:44.697577553 +0100
3077@@ -0,0 +1,60 @@
7f207e10 3078+/*
a2654f78 3079+ * Copyright (C) 2005-2017 Junjiro R. Okajima
7f207e10
AM
3080+ *
3081+ * This program, aufs is free software; you can redistribute it and/or modify
3082+ * it under the terms of the GNU General Public License as published by
3083+ * the Free Software Foundation; either version 2 of the License, or
3084+ * (at your option) any later version.
3085+ *
3086+ * This program is distributed in the hope that it will be useful,
3087+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
3088+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3089+ * GNU General Public License for more details.
3090+ *
3091+ * You should have received a copy of the GNU General Public License
523b37e3 3092+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
3093+ */
3094+
3095+/*
3096+ * all header files
3097+ */
3098+
3099+#ifndef __AUFS_H__
3100+#define __AUFS_H__
3101+
3102+#ifdef __KERNEL__
3103+
3104+#define AuStub(type, name, body, ...) \
3105+ static inline type name(__VA_ARGS__) { body; }
3106+
3107+#define AuStubVoid(name, ...) \
3108+ AuStub(void, name, , __VA_ARGS__)
3109+#define AuStubInt0(name, ...) \
3110+ AuStub(int, name, return 0, __VA_ARGS__)
3111+
3112+#include "debug.h"
3113+
3114+#include "branch.h"
3115+#include "cpup.h"
3116+#include "dcsub.h"
3117+#include "dbgaufs.h"
3118+#include "dentry.h"
3119+#include "dir.h"
8b6a4947 3120+#include "dirren.h"
7f207e10
AM
3121+#include "dynop.h"
3122+#include "file.h"
3123+#include "fstype.h"
8b6a4947 3124+#include "hbl.h"
7f207e10
AM
3125+#include "inode.h"
3126+#include "loop.h"
3127+#include "module.h"
7f207e10
AM
3128+#include "opts.h"
3129+#include "rwsem.h"
7f207e10
AM
3130+#include "super.h"
3131+#include "sysaufs.h"
3132+#include "vfsub.h"
3133+#include "whout.h"
3134+#include "wkq.h"
3135+
3136+#endif /* __KERNEL__ */
3137+#endif /* __AUFS_H__ */
3138diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
3139--- /usr/share/empty/fs/aufs/branch.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
3140+++ linux/fs/aufs/branch.c 2017-11-12 22:24:44.697577553 +0100
3141@@ -0,0 +1,1432 @@
7f207e10 3142+/*
a2654f78 3143+ * Copyright (C) 2005-2017 Junjiro R. Okajima
7f207e10
AM
3144+ *
3145+ * This program, aufs is free software; you can redistribute it and/or modify
3146+ * it under the terms of the GNU General Public License as published by
3147+ * the Free Software Foundation; either version 2 of the License, or
3148+ * (at your option) any later version.
3149+ *
3150+ * This program is distributed in the hope that it will be useful,
3151+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
3152+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3153+ * GNU General Public License for more details.
3154+ *
3155+ * You should have received a copy of the GNU General Public License
523b37e3 3156+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
3157+ */
3158+
3159+/*
3160+ * branch management
3161+ */
3162+
027c5e7a 3163+#include <linux/compat.h>
7f207e10
AM
3164+#include <linux/statfs.h>
3165+#include "aufs.h"
3166+
3167+/*
3168+ * free a single branch
1facf9fc 3169+ */
3170+static void au_br_do_free(struct au_branch *br)
3171+{
3172+ int i;
3173+ struct au_wbr *wbr;
4a4d8108 3174+ struct au_dykey **key;
1facf9fc 3175+
027c5e7a 3176+ au_hnotify_fin_br(br);
8b6a4947
AM
3177+ /* always, regardless the mount option */
3178+ au_dr_hino_free(&br->br_dirren);
027c5e7a 3179+
1facf9fc 3180+ if (br->br_xino.xi_file)
3181+ fput(br->br_xino.xi_file);
521ced18
JR
3182+ for (i = br->br_xino.xi_nondir.total - 1; i >= 0; i--)
3183+ AuDebugOn(br->br_xino.xi_nondir.array[i]);
1c60b727 3184+ kfree(br->br_xino.xi_nondir.array);
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;
521ced18
JR
3281+ add_branch->br_xino.xi_nondir.total = 8; /* initial size */
3282+ add_branch->br_xino.xi_nondir.array
8b6a4947 3283+ = kcalloc(add_branch->br_xino.xi_nondir.total, sizeof(ino_t),
521ced18
JR
3284+ GFP_NOFS);
3285+ if (unlikely(!add_branch->br_xino.xi_nondir.array))
3286+ goto out_br;
1facf9fc 3287+
027c5e7a
AM
3288+ err = au_hnotify_init_br(add_branch, perm);
3289+ if (unlikely(err))
521ced18 3290+ goto out_xinondir;
027c5e7a 3291+
1facf9fc 3292+ if (au_br_writable(perm)) {
3293+ /* may be freed separately at changing the branch permission */
be52b249 3294+ add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
1facf9fc 3295+ GFP_NOFS);
3296+ if (unlikely(!add_branch->br_wbr))
027c5e7a 3297+ goto out_hnotify;
1facf9fc 3298+ }
3299+
076b876e
AM
3300+ if (au_br_fhsm(perm)) {
3301+ err = au_fhsm_br_alloc(add_branch);
3302+ if (unlikely(err))
3303+ goto out_wbr;
3304+ }
3305+
521ced18 3306+ root = sb->s_root;
e2f27e51 3307+ err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
4a4d8108 3308+ if (!err)
e2f27e51 3309+ err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
5527c038
JR
3310+ if (!err) {
3311+ inode = d_inode(root);
1c60b727
AM
3312+ err = au_hinode_realloc(au_ii(inode), new_nbranch,
3313+ /*may_shrink*/0);
5527c038 3314+ }
4a4d8108
AM
3315+ if (!err)
3316+ return add_branch; /* success */
1facf9fc 3317+
076b876e 3318+out_wbr:
1c60b727 3319+ kfree(add_branch->br_wbr);
027c5e7a
AM
3320+out_hnotify:
3321+ au_hnotify_fin_br(add_branch);
521ced18 3322+out_xinondir:
1c60b727 3323+ kfree(add_branch->br_xino.xi_nondir.array);
4f0767ce 3324+out_br:
1c60b727 3325+ kfree(add_branch);
4f0767ce 3326+out:
4a4d8108 3327+ return ERR_PTR(err);
1facf9fc 3328+}
3329+
3330+/*
3331+ * test if the branch permission is legal or not.
3332+ */
3333+static int test_br(struct inode *inode, int brperm, char *path)
3334+{
3335+ int err;
3336+
4a4d8108
AM
3337+ err = (au_br_writable(brperm) && IS_RDONLY(inode));
3338+ if (!err)
3339+ goto out;
1facf9fc 3340+
4a4d8108
AM
3341+ err = -EINVAL;
3342+ pr_err("write permission for readonly mount or inode, %s\n", path);
3343+
4f0767ce 3344+out:
1facf9fc 3345+ return err;
3346+}
3347+
3348+/*
3349+ * returns:
3350+ * 0: success, the caller will add it
3351+ * plus: success, it is already unified, the caller should ignore it
3352+ * minus: error
3353+ */
3354+static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3355+{
3356+ int err;
5afbbe0d 3357+ aufs_bindex_t bbot, bindex;
5527c038 3358+ struct dentry *root, *h_dentry;
1facf9fc 3359+ struct inode *inode, *h_inode;
3360+
3361+ root = sb->s_root;
5afbbe0d
AM
3362+ bbot = au_sbbot(sb);
3363+ if (unlikely(bbot >= 0
1facf9fc 3364+ && au_find_dbindex(root, add->path.dentry) >= 0)) {
3365+ err = 1;
3366+ if (!remount) {
3367+ err = -EINVAL;
4a4d8108 3368+ pr_err("%s duplicated\n", add->pathname);
1facf9fc 3369+ }
3370+ goto out;
3371+ }
3372+
3373+ err = -ENOSPC; /* -E2BIG; */
3374+ if (unlikely(AUFS_BRANCH_MAX <= add->bindex
5afbbe0d 3375+ || AUFS_BRANCH_MAX - 1 <= bbot)) {
4a4d8108 3376+ pr_err("number of branches exceeded %s\n", add->pathname);
1facf9fc 3377+ goto out;
3378+ }
3379+
3380+ err = -EDOM;
5afbbe0d 3381+ if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
4a4d8108 3382+ pr_err("bad index %d\n", add->bindex);
1facf9fc 3383+ goto out;
3384+ }
3385+
5527c038 3386+ inode = d_inode(add->path.dentry);
1facf9fc 3387+ err = -ENOENT;
3388+ if (unlikely(!inode->i_nlink)) {
4a4d8108 3389+ pr_err("no existence %s\n", add->pathname);
1facf9fc 3390+ goto out;
3391+ }
3392+
3393+ err = -EINVAL;
3394+ if (unlikely(inode->i_sb == sb)) {
4a4d8108 3395+ pr_err("%s must be outside\n", add->pathname);
1facf9fc 3396+ goto out;
3397+ }
3398+
3399+ if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
4a4d8108
AM
3400+ pr_err("unsupported filesystem, %s (%s)\n",
3401+ add->pathname, au_sbtype(inode->i_sb));
1facf9fc 3402+ goto out;
3403+ }
3404+
c1595e42
JR
3405+ if (unlikely(inode->i_sb->s_stack_depth)) {
3406+ pr_err("already stacked, %s (%s)\n",
3407+ add->pathname, au_sbtype(inode->i_sb));
3408+ goto out;
3409+ }
3410+
5527c038 3411+ err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
1facf9fc 3412+ if (unlikely(err))
3413+ goto out;
3414+
5afbbe0d 3415+ if (bbot < 0)
1facf9fc 3416+ return 0; /* success */
3417+
3418+ err = -EINVAL;
5afbbe0d 3419+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 3420+ if (unlikely(test_overlap(sb, add->path.dentry,
3421+ au_h_dptr(root, bindex)))) {
4a4d8108 3422+ pr_err("%s is overlapped\n", add->pathname);
1facf9fc 3423+ goto out;
3424+ }
3425+
3426+ err = 0;
3427+ if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
5527c038
JR
3428+ h_dentry = au_h_dptr(root, 0);
3429+ h_inode = d_inode(h_dentry);
1facf9fc 3430+ if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
0c3ec466
AM
3431+ || !uid_eq(h_inode->i_uid, inode->i_uid)
3432+ || !gid_eq(h_inode->i_gid, inode->i_gid))
3433+ pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3434+ add->pathname,
3435+ i_uid_read(inode), i_gid_read(inode),
3436+ (inode->i_mode & S_IALLUGO),
3437+ i_uid_read(h_inode), i_gid_read(h_inode),
3438+ (h_inode->i_mode & S_IALLUGO));
1facf9fc 3439+ }
3440+
4f0767ce 3441+out:
1facf9fc 3442+ return err;
3443+}
3444+
3445+/*
3446+ * initialize or clean the whiteouts for an adding branch
3447+ */
3448+static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
86dc4139 3449+ int new_perm)
1facf9fc 3450+{
3451+ int err, old_perm;
3452+ aufs_bindex_t bindex;
febd17d6 3453+ struct inode *h_inode;
1facf9fc 3454+ struct au_wbr *wbr;
3455+ struct au_hinode *hdir;
5527c038 3456+ struct dentry *h_dentry;
1facf9fc 3457+
86dc4139
AM
3458+ err = vfsub_mnt_want_write(au_br_mnt(br));
3459+ if (unlikely(err))
3460+ goto out;
3461+
1facf9fc 3462+ wbr = br->br_wbr;
3463+ old_perm = br->br_perm;
3464+ br->br_perm = new_perm;
3465+ hdir = NULL;
febd17d6 3466+ h_inode = NULL;
1facf9fc 3467+ bindex = au_br_index(sb, br->br_id);
3468+ if (0 <= bindex) {
5527c038 3469+ hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 3470+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 3471+ } else {
5527c038 3472+ h_dentry = au_br_dentry(br);
febd17d6
JR
3473+ h_inode = d_inode(h_dentry);
3474+ inode_lock_nested(h_inode, AuLsc_I_PARENT);
1facf9fc 3475+ }
3476+ if (!wbr)
86dc4139 3477+ err = au_wh_init(br, sb);
1facf9fc 3478+ else {
3479+ wbr_wh_write_lock(wbr);
86dc4139 3480+ err = au_wh_init(br, sb);
1facf9fc 3481+ wbr_wh_write_unlock(wbr);
3482+ }
3483+ if (hdir)
5afbbe0d 3484+ au_hn_inode_unlock(hdir);
1facf9fc 3485+ else
febd17d6 3486+ inode_unlock(h_inode);
86dc4139 3487+ vfsub_mnt_drop_write(au_br_mnt(br));
1facf9fc 3488+ br->br_perm = old_perm;
3489+
3490+ if (!err && wbr && !au_br_writable(new_perm)) {
1c60b727 3491+ kfree(wbr);
1facf9fc 3492+ br->br_wbr = NULL;
3493+ }
3494+
86dc4139 3495+out:
1facf9fc 3496+ return err;
3497+}
3498+
3499+static int au_wbr_init(struct au_branch *br, struct super_block *sb,
86dc4139 3500+ int perm)
1facf9fc 3501+{
3502+ int err;
4a4d8108 3503+ struct kstatfs kst;
1facf9fc 3504+ struct au_wbr *wbr;
3505+
3506+ wbr = br->br_wbr;
dece6358 3507+ au_rw_init(&wbr->wbr_wh_rwsem);
1facf9fc 3508+ atomic_set(&wbr->wbr_wh_running, 0);
1facf9fc 3509+
4a4d8108
AM
3510+ /*
3511+ * a limit for rmdir/rename a dir
523b37e3 3512+ * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
4a4d8108 3513+ */
86dc4139 3514+ err = vfs_statfs(&br->br_path, &kst);
4a4d8108
AM
3515+ if (unlikely(err))
3516+ goto out;
3517+ err = -EINVAL;
3518+ if (kst.f_namelen >= NAME_MAX)
86dc4139 3519+ err = au_br_init_wh(sb, br, perm);
4a4d8108 3520+ else
523b37e3
AM
3521+ pr_err("%pd(%s), unsupported namelen %ld\n",
3522+ au_br_dentry(br),
86dc4139 3523+ au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
1facf9fc 3524+
4f0767ce 3525+out:
1facf9fc 3526+ return err;
3527+}
3528+
c1595e42 3529+/* initialize a new branch */
1facf9fc 3530+static int au_br_init(struct au_branch *br, struct super_block *sb,
3531+ struct au_opt_add *add)
3532+{
3533+ int err;
5527c038 3534+ struct inode *h_inode;
1facf9fc 3535+
3536+ err = 0;
521ced18
JR
3537+ spin_lock_init(&br->br_xino.xi_nondir.spin);
3538+ init_waitqueue_head(&br->br_xino.xi_nondir.wqh);
1facf9fc 3539+ br->br_perm = add->perm;
86dc4139 3540+ br->br_path = add->path; /* set first, path_get() later */
4a4d8108 3541+ spin_lock_init(&br->br_dykey_lock);
5afbbe0d 3542+ au_br_count_init(br);
1facf9fc 3543+ atomic_set(&br->br_xino_running, 0);
3544+ br->br_id = au_new_br_id(sb);
7f207e10 3545+ AuDebugOn(br->br_id < 0);
1facf9fc 3546+
8b6a4947
AM
3547+ /* always, regardless the given option */
3548+ err = au_dr_br_init(sb, br, &add->path);
3549+ if (unlikely(err))
3550+ goto out_err;
3551+
1facf9fc 3552+ if (au_br_writable(add->perm)) {
86dc4139 3553+ err = au_wbr_init(br, sb, add->perm);
1facf9fc 3554+ if (unlikely(err))
b752ccd1 3555+ goto out_err;
1facf9fc 3556+ }
3557+
3558+ if (au_opt_test(au_mntflags(sb), XINO)) {
5527c038
JR
3559+ h_inode = d_inode(add->path.dentry);
3560+ err = au_xino_br(sb, br, h_inode->i_ino,
1facf9fc 3561+ au_sbr(sb, 0)->br_xino.xi_file, /*do_test*/1);
3562+ if (unlikely(err)) {
3563+ AuDebugOn(br->br_xino.xi_file);
b752ccd1 3564+ goto out_err;
1facf9fc 3565+ }
3566+ }
3567+
3568+ sysaufs_br_init(br);
86dc4139 3569+ path_get(&br->br_path);
b752ccd1 3570+ goto out; /* success */
1facf9fc 3571+
4f0767ce 3572+out_err:
86dc4139 3573+ memset(&br->br_path, 0, sizeof(br->br_path));
4f0767ce 3574+out:
1facf9fc 3575+ return err;
3576+}
3577+
3578+static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
5afbbe0d 3579+ struct au_branch *br, aufs_bindex_t bbot,
1facf9fc 3580+ aufs_bindex_t amount)
3581+{
3582+ struct au_branch **brp;
3583+
dece6358
AM
3584+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3585+
1facf9fc 3586+ brp = sbinfo->si_branch + bindex;
3587+ memmove(brp + 1, brp, sizeof(*brp) * amount);
3588+ *brp = br;
5afbbe0d
AM
3589+ sbinfo->si_bbot++;
3590+ if (unlikely(bbot < 0))
3591+ sbinfo->si_bbot = 0;
1facf9fc 3592+}
3593+
3594+static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
5afbbe0d 3595+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3596+{
3597+ struct au_hdentry *hdp;
3598+
1308ab2a 3599+ AuRwMustWriteLock(&dinfo->di_rwsem);
3600+
5afbbe0d 3601+ hdp = au_hdentry(dinfo, bindex);
1facf9fc 3602+ memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3603+ au_h_dentry_init(hdp);
5afbbe0d
AM
3604+ dinfo->di_bbot++;
3605+ if (unlikely(bbot < 0))
3606+ dinfo->di_btop = 0;
1facf9fc 3607+}
3608+
3609+static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
5afbbe0d 3610+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3611+{
3612+ struct au_hinode *hip;
3613+
1308ab2a 3614+ AuRwMustWriteLock(&iinfo->ii_rwsem);
3615+
5afbbe0d 3616+ hip = au_hinode(iinfo, bindex);
1facf9fc 3617+ memmove(hip + 1, hip, sizeof(*hip) * amount);
5afbbe0d
AM
3618+ au_hinode_init(hip);
3619+ iinfo->ii_bbot++;
3620+ if (unlikely(bbot < 0))
3621+ iinfo->ii_btop = 0;
1facf9fc 3622+}
3623+
86dc4139
AM
3624+static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3625+ aufs_bindex_t bindex)
1facf9fc 3626+{
86dc4139 3627+ struct dentry *root, *h_dentry;
5527c038 3628+ struct inode *root_inode, *h_inode;
5afbbe0d 3629+ aufs_bindex_t bbot, amount;
1facf9fc 3630+
3631+ root = sb->s_root;
5527c038 3632+ root_inode = d_inode(root);
5afbbe0d
AM
3633+ bbot = au_sbbot(sb);
3634+ amount = bbot + 1 - bindex;
86dc4139 3635+ h_dentry = au_br_dentry(br);
53392da6 3636+ au_sbilist_lock();
5afbbe0d
AM
3637+ au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3638+ au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3639+ au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
1facf9fc 3640+ au_set_h_dptr(root, bindex, dget(h_dentry));
5527c038
JR
3641+ h_inode = d_inode(h_dentry);
3642+ au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
53392da6 3643+ au_sbilist_unlock();
1facf9fc 3644+}
3645+
3646+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3647+{
3648+ int err;
5afbbe0d 3649+ aufs_bindex_t bbot, add_bindex;
1facf9fc 3650+ struct dentry *root, *h_dentry;
3651+ struct inode *root_inode;
3652+ struct au_branch *add_branch;
3653+
3654+ root = sb->s_root;
5527c038 3655+ root_inode = d_inode(root);
1facf9fc 3656+ IMustLock(root_inode);
5afbbe0d 3657+ IiMustWriteLock(root_inode);
1facf9fc 3658+ err = test_add(sb, add, remount);
3659+ if (unlikely(err < 0))
3660+ goto out;
3661+ if (err) {
3662+ err = 0;
3663+ goto out; /* success */
3664+ }
3665+
5afbbe0d
AM
3666+ bbot = au_sbbot(sb);
3667+ add_branch = au_br_alloc(sb, bbot + 2, add->perm);
1facf9fc 3668+ err = PTR_ERR(add_branch);
3669+ if (IS_ERR(add_branch))
3670+ goto out;
3671+
3672+ err = au_br_init(add_branch, sb, add);
3673+ if (unlikely(err)) {
3674+ au_br_do_free(add_branch);
3675+ goto out;
3676+ }
3677+
3678+ add_bindex = add->bindex;
1facf9fc 3679+ if (!remount)
86dc4139 3680+ au_br_do_add(sb, add_branch, add_bindex);
1facf9fc 3681+ else {
3682+ sysaufs_brs_del(sb, add_bindex);
86dc4139 3683+ au_br_do_add(sb, add_branch, add_bindex);
1facf9fc 3684+ sysaufs_brs_add(sb, add_bindex);
3685+ }
3686+
86dc4139 3687+ h_dentry = add->path.dentry;
1308ab2a 3688+ if (!add_bindex) {
1facf9fc 3689+ au_cpup_attr_all(root_inode, /*force*/1);
1308ab2a 3690+ sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3691+ } else
5527c038 3692+ au_add_nlink(root_inode, d_inode(h_dentry));
1facf9fc 3693+
3694+ /*
4a4d8108 3695+ * this test/set prevents aufs from handling unnecesary notify events
027c5e7a 3696+ * of xino files, in case of re-adding a writable branch which was
1facf9fc 3697+ * once detached from aufs.
3698+ */
3699+ if (au_xino_brid(sb) < 0
3700+ && au_br_writable(add_branch->br_perm)
3701+ && !au_test_fs_bad_xino(h_dentry->d_sb)
3702+ && add_branch->br_xino.xi_file
2000de60 3703+ && add_branch->br_xino.xi_file->f_path.dentry->d_parent == h_dentry)
1facf9fc 3704+ au_xino_brid_set(sb, add_branch->br_id);
3705+
4f0767ce 3706+out:
1facf9fc 3707+ return err;
3708+}
3709+
3710+/* ---------------------------------------------------------------------- */
3711+
79b8bda9 3712+static unsigned long long au_farray_cb(struct super_block *sb, void *a,
076b876e
AM
3713+ unsigned long long max __maybe_unused,
3714+ void *arg)
3715+{
3716+ unsigned long long n;
3717+ struct file **p, *f;
8b6a4947
AM
3718+ struct hlist_bl_head *files;
3719+ struct hlist_bl_node *pos;
076b876e 3720+ struct au_finfo *finfo;
076b876e
AM
3721+
3722+ n = 0;
3723+ p = a;
3724+ files = &au_sbi(sb)->si_files;
8b6a4947
AM
3725+ hlist_bl_lock(files);
3726+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
076b876e
AM
3727+ f = finfo->fi_file;
3728+ if (file_count(f)
3729+ && !special_file(file_inode(f)->i_mode)) {
3730+ get_file(f);
3731+ *p++ = f;
3732+ n++;
3733+ AuDebugOn(n > max);
3734+ }
3735+ }
8b6a4947 3736+ hlist_bl_unlock(files);
076b876e
AM
3737+
3738+ return n;
3739+}
3740+
3741+static struct file **au_farray_alloc(struct super_block *sb,
3742+ unsigned long long *max)
3743+{
5afbbe0d 3744+ *max = au_nfiles(sb);
79b8bda9 3745+ return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
076b876e
AM
3746+}
3747+
3748+static void au_farray_free(struct file **a, unsigned long long max)
3749+{
3750+ unsigned long long ull;
3751+
3752+ for (ull = 0; ull < max; ull++)
3753+ if (a[ull])
3754+ fput(a[ull]);
be52b249 3755+ kvfree(a);
076b876e
AM
3756+}
3757+
3758+/* ---------------------------------------------------------------------- */
3759+
1facf9fc 3760+/*
3761+ * delete a branch
3762+ */
3763+
3764+/* to show the line number, do not make it inlined function */
4a4d8108 3765+#define AuVerbose(do_info, fmt, ...) do { \
1facf9fc 3766+ if (do_info) \
4a4d8108 3767+ pr_info(fmt, ##__VA_ARGS__); \
1facf9fc 3768+} while (0)
3769+
5afbbe0d
AM
3770+static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3771+ aufs_bindex_t bbot)
027c5e7a 3772+{
5afbbe0d 3773+ return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
027c5e7a
AM
3774+}
3775+
5afbbe0d
AM
3776+static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3777+ aufs_bindex_t bbot)
027c5e7a 3778+{
5afbbe0d 3779+ return au_test_ibusy(d_inode(dentry), btop, bbot);
027c5e7a
AM
3780+}
3781+
1facf9fc 3782+/*
3783+ * test if the branch is deletable or not.
3784+ */
3785+static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
b752ccd1 3786+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3787+{
3788+ int err, i, j, ndentry;
5afbbe0d 3789+ aufs_bindex_t btop, bbot;
1facf9fc 3790+ struct au_dcsub_pages dpages;
3791+ struct au_dpage *dpage;
3792+ struct dentry *d;
1facf9fc 3793+
3794+ err = au_dpages_init(&dpages, GFP_NOFS);
3795+ if (unlikely(err))
3796+ goto out;
3797+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
3798+ if (unlikely(err))
3799+ goto out_dpages;
3800+
1facf9fc 3801+ for (i = 0; !err && i < dpages.ndpage; i++) {
3802+ dpage = dpages.dpages + i;
3803+ ndentry = dpage->ndentry;
3804+ for (j = 0; !err && j < ndentry; j++) {
3805+ d = dpage->dentries[j];
c1595e42 3806+ AuDebugOn(au_dcount(d) <= 0);
027c5e7a 3807+ if (!au_digen_test(d, sigen)) {
1facf9fc 3808+ di_read_lock_child(d, AuLock_IR);
027c5e7a
AM
3809+ if (unlikely(au_dbrange_test(d))) {
3810+ di_read_unlock(d, AuLock_IR);
3811+ continue;
3812+ }
3813+ } else {
1facf9fc 3814+ di_write_lock_child(d);
027c5e7a
AM
3815+ if (unlikely(au_dbrange_test(d))) {
3816+ di_write_unlock(d);
3817+ continue;
3818+ }
1facf9fc 3819+ err = au_reval_dpath(d, sigen);
3820+ if (!err)
3821+ di_downgrade_lock(d, AuLock_IR);
3822+ else {
3823+ di_write_unlock(d);
3824+ break;
3825+ }
3826+ }
3827+
027c5e7a 3828+ /* AuDbgDentry(d); */
5afbbe0d
AM
3829+ btop = au_dbtop(d);
3830+ bbot = au_dbbot(d);
3831+ if (btop <= bindex
3832+ && bindex <= bbot
1facf9fc 3833+ && au_h_dptr(d, bindex)
5afbbe0d 3834+ && au_test_dbusy(d, btop, bbot)) {
1facf9fc 3835+ err = -EBUSY;
523b37e3 3836+ AuVerbose(verbose, "busy %pd\n", d);
027c5e7a 3837+ AuDbgDentry(d);
1facf9fc 3838+ }
3839+ di_read_unlock(d, AuLock_IR);
3840+ }
3841+ }
3842+
4f0767ce 3843+out_dpages:
1facf9fc 3844+ au_dpages_free(&dpages);
4f0767ce 3845+out:
1facf9fc 3846+ return err;
3847+}
3848+
3849+static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
b752ccd1 3850+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3851+{
3852+ int err;
7f207e10
AM
3853+ unsigned long long max, ull;
3854+ struct inode *i, **array;
5afbbe0d 3855+ aufs_bindex_t btop, bbot;
1facf9fc 3856+
7f207e10
AM
3857+ array = au_iarray_alloc(sb, &max);
3858+ err = PTR_ERR(array);
3859+ if (IS_ERR(array))
3860+ goto out;
3861+
1facf9fc 3862+ err = 0;
7f207e10
AM
3863+ AuDbg("b%d\n", bindex);
3864+ for (ull = 0; !err && ull < max; ull++) {
3865+ i = array[ull];
076b876e
AM
3866+ if (unlikely(!i))
3867+ break;
7f207e10 3868+ if (i->i_ino == AUFS_ROOT_INO)
1facf9fc 3869+ continue;
3870+
7f207e10 3871+ /* AuDbgInode(i); */
537831f9 3872+ if (au_iigen(i, NULL) == sigen)
1facf9fc 3873+ ii_read_lock_child(i);
3874+ else {
3875+ ii_write_lock_child(i);
027c5e7a
AM
3876+ err = au_refresh_hinode_self(i);
3877+ au_iigen_dec(i);
1facf9fc 3878+ if (!err)
3879+ ii_downgrade_lock(i);
3880+ else {
3881+ ii_write_unlock(i);
3882+ break;
3883+ }
3884+ }
3885+
5afbbe0d
AM
3886+ btop = au_ibtop(i);
3887+ bbot = au_ibbot(i);
3888+ if (btop <= bindex
3889+ && bindex <= bbot
1facf9fc 3890+ && au_h_iptr(i, bindex)
5afbbe0d 3891+ && au_test_ibusy(i, btop, bbot)) {
1facf9fc 3892+ err = -EBUSY;
3893+ AuVerbose(verbose, "busy i%lu\n", i->i_ino);
7f207e10 3894+ AuDbgInode(i);
1facf9fc 3895+ }
3896+ ii_read_unlock(i);
3897+ }
7f207e10 3898+ au_iarray_free(array, max);
1facf9fc 3899+
7f207e10 3900+out:
1facf9fc 3901+ return err;
3902+}
3903+
b752ccd1
AM
3904+static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3905+ const unsigned int verbose)
1facf9fc 3906+{
3907+ int err;
3908+ unsigned int sigen;
3909+
3910+ sigen = au_sigen(root->d_sb);
3911+ DiMustNoWaiters(root);
5527c038 3912+ IiMustNoWaiters(d_inode(root));
1facf9fc 3913+ di_write_unlock(root);
b752ccd1 3914+ err = test_dentry_busy(root, bindex, sigen, verbose);
1facf9fc 3915+ if (!err)
b752ccd1 3916+ err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
1facf9fc 3917+ di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3918+
3919+ return err;
3920+}
3921+
076b876e
AM
3922+static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3923+ struct file **to_free, int *idx)
3924+{
3925+ int err;
c1595e42 3926+ unsigned char matched, root;
5afbbe0d 3927+ aufs_bindex_t bindex, bbot;
076b876e
AM
3928+ struct au_fidir *fidir;
3929+ struct au_hfile *hfile;
3930+
3931+ err = 0;
2000de60 3932+ root = IS_ROOT(file->f_path.dentry);
c1595e42
JR
3933+ if (root) {
3934+ get_file(file);
3935+ to_free[*idx] = file;
3936+ (*idx)++;
3937+ goto out;
3938+ }
3939+
076b876e 3940+ matched = 0;
076b876e
AM
3941+ fidir = au_fi(file)->fi_hdir;
3942+ AuDebugOn(!fidir);
5afbbe0d
AM
3943+ bbot = au_fbbot_dir(file);
3944+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
076b876e
AM
3945+ hfile = fidir->fd_hfile + bindex;
3946+ if (!hfile->hf_file)
3947+ continue;
3948+
c1595e42 3949+ if (hfile->hf_br->br_id == br_id) {
076b876e 3950+ matched = 1;
076b876e 3951+ break;
c1595e42 3952+ }
076b876e 3953+ }
c1595e42 3954+ if (matched)
076b876e
AM
3955+ err = -EBUSY;
3956+
3957+out:
3958+ return err;
3959+}
3960+
3961+static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3962+ struct file **to_free, int opened)
3963+{
3964+ int err, idx;
3965+ unsigned long long ull, max;
5afbbe0d 3966+ aufs_bindex_t btop;
076b876e 3967+ struct file *file, **array;
076b876e
AM
3968+ struct dentry *root;
3969+ struct au_hfile *hfile;
3970+
3971+ array = au_farray_alloc(sb, &max);
3972+ err = PTR_ERR(array);
3973+ if (IS_ERR(array))
3974+ goto out;
3975+
3976+ err = 0;
3977+ idx = 0;
3978+ root = sb->s_root;
3979+ di_write_unlock(root);
3980+ for (ull = 0; ull < max; ull++) {
3981+ file = array[ull];
3982+ if (unlikely(!file))
3983+ break;
3984+
3985+ /* AuDbg("%pD\n", file); */
3986+ fi_read_lock(file);
5afbbe0d 3987+ btop = au_fbtop(file);
2000de60 3988+ if (!d_is_dir(file->f_path.dentry)) {
076b876e
AM
3989+ hfile = &au_fi(file)->fi_htop;
3990+ if (hfile->hf_br->br_id == br_id)
3991+ err = -EBUSY;
3992+ } else
3993+ err = test_dir_busy(file, br_id, to_free, &idx);
3994+ fi_read_unlock(file);
3995+ if (unlikely(err))
3996+ break;
3997+ }
3998+ di_write_lock_child(root);
3999+ au_farray_free(array, max);
4000+ AuDebugOn(idx > opened);
4001+
4002+out:
4003+ return err;
4004+}
4005+
4006+static void br_del_file(struct file **to_free, unsigned long long opened,
4007+ aufs_bindex_t br_id)
4008+{
4009+ unsigned long long ull;
5afbbe0d 4010+ aufs_bindex_t bindex, btop, bbot, bfound;
076b876e
AM
4011+ struct file *file;
4012+ struct au_fidir *fidir;
4013+ struct au_hfile *hfile;
4014+
4015+ for (ull = 0; ull < opened; ull++) {
4016+ file = to_free[ull];
4017+ if (unlikely(!file))
4018+ break;
4019+
4020+ /* AuDbg("%pD\n", file); */
2000de60 4021+ AuDebugOn(!d_is_dir(file->f_path.dentry));
076b876e
AM
4022+ bfound = -1;
4023+ fidir = au_fi(file)->fi_hdir;
4024+ AuDebugOn(!fidir);
4025+ fi_write_lock(file);
5afbbe0d
AM
4026+ btop = au_fbtop(file);
4027+ bbot = au_fbbot_dir(file);
4028+ for (bindex = btop; bindex <= bbot; bindex++) {
076b876e
AM
4029+ hfile = fidir->fd_hfile + bindex;
4030+ if (!hfile->hf_file)
4031+ continue;
4032+
4033+ if (hfile->hf_br->br_id == br_id) {
4034+ bfound = bindex;
4035+ break;
4036+ }
4037+ }
4038+ AuDebugOn(bfound < 0);
4039+ au_set_h_fptr(file, bfound, NULL);
5afbbe0d
AM
4040+ if (bfound == btop) {
4041+ for (btop++; btop <= bbot; btop++)
4042+ if (au_hf_dir(file, btop)) {
4043+ au_set_fbtop(file, btop);
076b876e
AM
4044+ break;
4045+ }
4046+ }
4047+ fi_write_unlock(file);
4048+ }
4049+}
4050+
1facf9fc 4051+static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
4052+ const aufs_bindex_t bindex,
5afbbe0d 4053+ const aufs_bindex_t bbot)
1facf9fc 4054+{
4055+ struct au_branch **brp, **p;
4056+
dece6358
AM
4057+ AuRwMustWriteLock(&sbinfo->si_rwsem);
4058+
1facf9fc 4059+ brp = sbinfo->si_branch + bindex;
5afbbe0d
AM
4060+ if (bindex < bbot)
4061+ memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
4062+ sbinfo->si_branch[0 + bbot] = NULL;
4063+ sbinfo->si_bbot--;
1facf9fc 4064+
e2f27e51
AM
4065+ p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
4066+ /*may_shrink*/1);
1facf9fc 4067+ if (p)
4068+ sbinfo->si_branch = p;
4a4d8108 4069+ /* harmless error */
1facf9fc 4070+}
4071+
4072+static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
5afbbe0d 4073+ const aufs_bindex_t bbot)
1facf9fc 4074+{
4075+ struct au_hdentry *hdp, *p;
4076+
1308ab2a 4077+ AuRwMustWriteLock(&dinfo->di_rwsem);
4078+
5afbbe0d
AM
4079+ hdp = au_hdentry(dinfo, bindex);
4080+ if (bindex < bbot)
4081+ memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
4082+ /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
4083+ dinfo->di_bbot--;
1facf9fc 4084+
e2f27e51
AM
4085+ p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
4086+ /*may_shrink*/1);
1facf9fc 4087+ if (p)
4088+ dinfo->di_hdentry = p;
4a4d8108 4089+ /* harmless error */
1facf9fc 4090+}
4091+
4092+static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
5afbbe0d 4093+ const aufs_bindex_t bbot)
1facf9fc 4094+{
4095+ struct au_hinode *hip, *p;
4096+
1308ab2a 4097+ AuRwMustWriteLock(&iinfo->ii_rwsem);
4098+
5afbbe0d
AM
4099+ hip = au_hinode(iinfo, bindex);
4100+ if (bindex < bbot)
4101+ memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
4102+ /* au_hinode_init(au_hinode(iinfo, bbot)); */
4103+ iinfo->ii_bbot--;
1facf9fc 4104+
e2f27e51
AM
4105+ p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
4106+ /*may_shrink*/1);
1facf9fc 4107+ if (p)
4108+ iinfo->ii_hinode = p;
4a4d8108 4109+ /* harmless error */
1facf9fc 4110+}
4111+
4112+static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
4113+ struct au_branch *br)
4114+{
5afbbe0d 4115+ aufs_bindex_t bbot;
1facf9fc 4116+ struct au_sbinfo *sbinfo;
53392da6
AM
4117+ struct dentry *root, *h_root;
4118+ struct inode *inode, *h_inode;
4119+ struct au_hinode *hinode;
1facf9fc 4120+
dece6358
AM
4121+ SiMustWriteLock(sb);
4122+
1facf9fc 4123+ root = sb->s_root;
5527c038 4124+ inode = d_inode(root);
1facf9fc 4125+ sbinfo = au_sbi(sb);
5afbbe0d 4126+ bbot = sbinfo->si_bbot;
1facf9fc 4127+
53392da6
AM
4128+ h_root = au_h_dptr(root, bindex);
4129+ hinode = au_hi(inode, bindex);
4130+ h_inode = au_igrab(hinode->hi_inode);
4131+ au_hiput(hinode);
1facf9fc 4132+
53392da6 4133+ au_sbilist_lock();
5afbbe0d
AM
4134+ au_br_do_del_brp(sbinfo, bindex, bbot);
4135+ au_br_do_del_hdp(au_di(root), bindex, bbot);
4136+ au_br_do_del_hip(au_ii(inode), bindex, bbot);
53392da6
AM
4137+ au_sbilist_unlock();
4138+
8b6a4947
AM
4139+ /* ignore an error */
4140+ au_dr_br_fin(sb, br); /* always, regardless the mount option */
4141+
53392da6
AM
4142+ dput(h_root);
4143+ iput(h_inode);
4144+ au_br_do_free(br);
1facf9fc 4145+}
4146+
79b8bda9
AM
4147+static unsigned long long empty_cb(struct super_block *sb, void *array,
4148+ unsigned long long max, void *arg)
076b876e
AM
4149+{
4150+ return max;
4151+}
4152+
1facf9fc 4153+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
4154+{
4155+ int err, rerr, i;
076b876e 4156+ unsigned long long opened;
1facf9fc 4157+ unsigned int mnt_flags;
5afbbe0d 4158+ aufs_bindex_t bindex, bbot, br_id;
1facf9fc 4159+ unsigned char do_wh, verbose;
4160+ struct au_branch *br;
4161+ struct au_wbr *wbr;
076b876e
AM
4162+ struct dentry *root;
4163+ struct file **to_free;
1facf9fc 4164+
4165+ err = 0;
076b876e
AM
4166+ opened = 0;
4167+ to_free = NULL;
4168+ root = sb->s_root;
4169+ bindex = au_find_dbindex(root, del->h_path.dentry);
1facf9fc 4170+ if (bindex < 0) {
4171+ if (remount)
4172+ goto out; /* success */
4173+ err = -ENOENT;
4a4d8108 4174+ pr_err("%s no such branch\n", del->pathname);
1facf9fc 4175+ goto out;
4176+ }
4177+ AuDbg("bindex b%d\n", bindex);
4178+
4179+ err = -EBUSY;
4180+ mnt_flags = au_mntflags(sb);
4181+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
5afbbe0d
AM
4182+ bbot = au_sbbot(sb);
4183+ if (unlikely(!bbot)) {
1facf9fc 4184+ AuVerbose(verbose, "no more branches left\n");
4185+ goto out;
4186+ }
4187+ br = au_sbr(sb, bindex);
86dc4139 4188+ AuDebugOn(!path_equal(&br->br_path, &del->h_path));
076b876e
AM
4189+
4190+ br_id = br->br_id;
5afbbe0d 4191+ opened = au_br_count(br);
076b876e 4192+ if (unlikely(opened)) {
79b8bda9 4193+ to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
076b876e
AM
4194+ err = PTR_ERR(to_free);
4195+ if (IS_ERR(to_free))
4196+ goto out;
4197+
4198+ err = test_file_busy(sb, br_id, to_free, opened);
4199+ if (unlikely(err)) {
4200+ AuVerbose(verbose, "%llu file(s) opened\n", opened);
4201+ goto out;
4202+ }
1facf9fc 4203+ }
4204+
4205+ wbr = br->br_wbr;
4206+ do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
4207+ if (do_wh) {
1308ab2a 4208+ /* instead of WbrWhMustWriteLock(wbr) */
4209+ SiMustWriteLock(sb);
1facf9fc 4210+ for (i = 0; i < AuBrWh_Last; i++) {
4211+ dput(wbr->wbr_wh[i]);
4212+ wbr->wbr_wh[i] = NULL;
4213+ }
4214+ }
4215+
076b876e 4216+ err = test_children_busy(root, bindex, verbose);
1facf9fc 4217+ if (unlikely(err)) {
4218+ if (do_wh)
4219+ goto out_wh;
4220+ goto out;
4221+ }
4222+
4223+ err = 0;
076b876e
AM
4224+ if (to_free) {
4225+ /*
4226+ * now we confirmed the branch is deletable.
4227+ * let's free the remaining opened dirs on the branch.
4228+ */
4229+ di_write_unlock(root);
4230+ br_del_file(to_free, opened, br_id);
4231+ di_write_lock_child(root);
4232+ }
4233+
1facf9fc 4234+ if (!remount)
4235+ au_br_do_del(sb, bindex, br);
4236+ else {
4237+ sysaufs_brs_del(sb, bindex);
4238+ au_br_do_del(sb, bindex, br);
4239+ sysaufs_brs_add(sb, bindex);
4240+ }
4241+
1308ab2a 4242+ if (!bindex) {
5527c038 4243+ au_cpup_attr_all(d_inode(root), /*force*/1);
1308ab2a 4244+ sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
4245+ } else
5527c038 4246+ au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
1facf9fc 4247+ if (au_opt_test(mnt_flags, PLINK))
4248+ au_plink_half_refresh(sb, br_id);
4249+
b752ccd1 4250+ if (au_xino_brid(sb) == br_id)
1facf9fc 4251+ au_xino_brid_set(sb, -1);
4252+ goto out; /* success */
4253+
4f0767ce 4254+out_wh:
1facf9fc 4255+ /* revert */
86dc4139 4256+ rerr = au_br_init_wh(sb, br, br->br_perm);
1facf9fc 4257+ if (rerr)
0c3ec466
AM
4258+ pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4259+ del->pathname, rerr);
4f0767ce 4260+out:
076b876e
AM
4261+ if (to_free)
4262+ au_farray_free(to_free, opened);
1facf9fc 4263+ return err;
4264+}
4265+
4266+/* ---------------------------------------------------------------------- */
4267+
027c5e7a
AM
4268+static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4269+{
4270+ int err;
5afbbe0d 4271+ aufs_bindex_t btop, bbot;
027c5e7a
AM
4272+ struct aufs_ibusy ibusy;
4273+ struct inode *inode, *h_inode;
4274+
4275+ err = -EPERM;
4276+ if (unlikely(!capable(CAP_SYS_ADMIN)))
4277+ goto out;
4278+
4279+ err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4280+ if (!err)
4281+ err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
4282+ if (unlikely(err)) {
4283+ err = -EFAULT;
4284+ AuTraceErr(err);
4285+ goto out;
4286+ }
4287+
4288+ err = -EINVAL;
4289+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d 4290+ if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
027c5e7a
AM
4291+ goto out_unlock;
4292+
4293+ err = 0;
4294+ ibusy.h_ino = 0; /* invalid */
4295+ inode = ilookup(sb, ibusy.ino);
4296+ if (!inode
4297+ || inode->i_ino == AUFS_ROOT_INO
5afbbe0d 4298+ || au_is_bad_inode(inode))
027c5e7a
AM
4299+ goto out_unlock;
4300+
4301+ ii_read_lock_child(inode);
5afbbe0d
AM
4302+ btop = au_ibtop(inode);
4303+ bbot = au_ibbot(inode);
4304+ if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
027c5e7a 4305+ h_inode = au_h_iptr(inode, ibusy.bindex);
5afbbe0d 4306+ if (h_inode && au_test_ibusy(inode, btop, bbot))
027c5e7a
AM
4307+ ibusy.h_ino = h_inode->i_ino;
4308+ }
4309+ ii_read_unlock(inode);
4310+ iput(inode);
4311+
4312+out_unlock:
4313+ si_read_unlock(sb);
4314+ if (!err) {
4315+ err = __put_user(ibusy.h_ino, &arg->h_ino);
4316+ if (unlikely(err)) {
4317+ err = -EFAULT;
4318+ AuTraceErr(err);
4319+ }
4320+ }
4321+out:
4322+ return err;
4323+}
4324+
4325+long au_ibusy_ioctl(struct file *file, unsigned long arg)
4326+{
2000de60 4327+ return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
027c5e7a
AM
4328+}
4329+
4330+#ifdef CONFIG_COMPAT
4331+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4332+{
2000de60 4333+ return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
027c5e7a
AM
4334+}
4335+#endif
4336+
4337+/* ---------------------------------------------------------------------- */
4338+
1facf9fc 4339+/*
4340+ * change a branch permission
4341+ */
4342+
dece6358
AM
4343+static void au_warn_ima(void)
4344+{
4345+#ifdef CONFIG_IMA
1308ab2a 4346+ /* since it doesn't support mark_files_ro() */
027c5e7a 4347+ AuWarn1("RW -> RO makes IMA to produce wrong message\n");
dece6358
AM
4348+#endif
4349+}
4350+
1facf9fc 4351+static int do_need_sigen_inc(int a, int b)
4352+{
4353+ return au_br_whable(a) && !au_br_whable(b);
4354+}
4355+
4356+static int need_sigen_inc(int old, int new)
4357+{
4358+ return do_need_sigen_inc(old, new)
4359+ || do_need_sigen_inc(new, old);
4360+}
4361+
4362+static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4363+{
7f207e10 4364+ int err, do_warn;
027c5e7a 4365+ unsigned int mnt_flags;
7f207e10 4366+ unsigned long long ull, max;
e49829fe 4367+ aufs_bindex_t br_id;
38d290e6 4368+ unsigned char verbose, writer;
7f207e10 4369+ struct file *file, *hf, **array;
e49829fe 4370+ struct au_hfile *hfile;
1facf9fc 4371+
027c5e7a
AM
4372+ mnt_flags = au_mntflags(sb);
4373+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
4374+
7f207e10
AM
4375+ array = au_farray_alloc(sb, &max);
4376+ err = PTR_ERR(array);
4377+ if (IS_ERR(array))
1facf9fc 4378+ goto out;
4379+
7f207e10 4380+ do_warn = 0;
e49829fe 4381+ br_id = au_sbr_id(sb, bindex);
7f207e10
AM
4382+ for (ull = 0; ull < max; ull++) {
4383+ file = array[ull];
076b876e
AM
4384+ if (unlikely(!file))
4385+ break;
1facf9fc 4386+
523b37e3 4387+ /* AuDbg("%pD\n", file); */
1facf9fc 4388+ fi_read_lock(file);
4389+ if (unlikely(au_test_mmapped(file))) {
4390+ err = -EBUSY;
523b37e3 4391+ AuVerbose(verbose, "mmapped %pD\n", file);
7f207e10 4392+ AuDbgFile(file);
1facf9fc 4393+ FiMustNoWaiters(file);
4394+ fi_read_unlock(file);
7f207e10 4395+ goto out_array;
1facf9fc 4396+ }
4397+
e49829fe
JR
4398+ hfile = &au_fi(file)->fi_htop;
4399+ hf = hfile->hf_file;
7e9cd9fe 4400+ if (!d_is_reg(file->f_path.dentry)
1facf9fc 4401+ || !(file->f_mode & FMODE_WRITE)
e49829fe 4402+ || hfile->hf_br->br_id != br_id
7f207e10
AM
4403+ || !(hf->f_mode & FMODE_WRITE))
4404+ array[ull] = NULL;
4405+ else {
4406+ do_warn = 1;
4407+ get_file(file);
1facf9fc 4408+ }
4409+
1facf9fc 4410+ FiMustNoWaiters(file);
4411+ fi_read_unlock(file);
7f207e10
AM
4412+ fput(file);
4413+ }
1facf9fc 4414+
4415+ err = 0;
7f207e10 4416+ if (do_warn)
dece6358 4417+ au_warn_ima();
7f207e10
AM
4418+
4419+ for (ull = 0; ull < max; ull++) {
4420+ file = array[ull];
4421+ if (!file)
4422+ continue;
4423+
1facf9fc 4424+ /* todo: already flushed? */
523b37e3
AM
4425+ /*
4426+ * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4427+ * approach which resets f_mode and calls mnt_drop_write() and
4428+ * file_release_write() for each file, because the branch
4429+ * attribute in aufs world is totally different from the native
4430+ * fs rw/ro mode.
4431+ */
7f207e10
AM
4432+ /* fi_read_lock(file); */
4433+ hfile = &au_fi(file)->fi_htop;
4434+ hf = hfile->hf_file;
4435+ /* fi_read_unlock(file); */
027c5e7a 4436+ spin_lock(&hf->f_lock);
38d290e6
JR
4437+ writer = !!(hf->f_mode & FMODE_WRITER);
4438+ hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
027c5e7a 4439+ spin_unlock(&hf->f_lock);
38d290e6
JR
4440+ if (writer) {
4441+ put_write_access(file_inode(hf));
c06a8ce3 4442+ __mnt_drop_write(hf->f_path.mnt);
1facf9fc 4443+ }
4444+ }
4445+
7f207e10
AM
4446+out_array:
4447+ au_farray_free(array, max);
4f0767ce 4448+out:
7f207e10 4449+ AuTraceErr(err);
1facf9fc 4450+ return err;
4451+}
4452+
4453+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4454+ int *do_refresh)
1facf9fc 4455+{
4456+ int err, rerr;
4457+ aufs_bindex_t bindex;
4458+ struct dentry *root;
4459+ struct au_branch *br;
076b876e 4460+ struct au_br_fhsm *bf;
1facf9fc 4461+
4462+ root = sb->s_root;
1facf9fc 4463+ bindex = au_find_dbindex(root, mod->h_root);
4464+ if (bindex < 0) {
4465+ if (remount)
4466+ return 0; /* success */
4467+ err = -ENOENT;
4a4d8108 4468+ pr_err("%s no such branch\n", mod->path);
1facf9fc 4469+ goto out;
4470+ }
4471+ AuDbg("bindex b%d\n", bindex);
4472+
5527c038 4473+ err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
1facf9fc 4474+ if (unlikely(err))
4475+ goto out;
4476+
4477+ br = au_sbr(sb, bindex);
86dc4139 4478+ AuDebugOn(mod->h_root != au_br_dentry(br));
1facf9fc 4479+ if (br->br_perm == mod->perm)
4480+ return 0; /* success */
4481+
076b876e
AM
4482+ /* pre-allocate for non-fhsm --> fhsm */
4483+ bf = NULL;
4484+ if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4485+ err = au_fhsm_br_alloc(br);
4486+ if (unlikely(err))
4487+ goto out;
4488+ bf = br->br_fhsm;
4489+ br->br_fhsm = NULL;
4490+ }
4491+
1facf9fc 4492+ if (au_br_writable(br->br_perm)) {
4493+ /* remove whiteout base */
86dc4139 4494+ err = au_br_init_wh(sb, br, mod->perm);
1facf9fc 4495+ if (unlikely(err))
076b876e 4496+ goto out_bf;
1facf9fc 4497+
4498+ if (!au_br_writable(mod->perm)) {
4499+ /* rw --> ro, file might be mmapped */
4500+ DiMustNoWaiters(root);
5527c038 4501+ IiMustNoWaiters(d_inode(root));
1facf9fc 4502+ di_write_unlock(root);
4503+ err = au_br_mod_files_ro(sb, bindex);
4504+ /* aufs_write_lock() calls ..._child() */
4505+ di_write_lock_child(root);
4506+
4507+ if (unlikely(err)) {
4508+ rerr = -ENOMEM;
be52b249 4509+ br->br_wbr = kzalloc(sizeof(*br->br_wbr),
1facf9fc 4510+ GFP_NOFS);
86dc4139
AM
4511+ if (br->br_wbr)
4512+ rerr = au_wbr_init(br, sb, br->br_perm);
1facf9fc 4513+ if (unlikely(rerr)) {
4514+ AuIOErr("nested error %d (%d)\n",
4515+ rerr, err);
4516+ br->br_perm = mod->perm;
4517+ }
4518+ }
4519+ }
4520+ } else if (au_br_writable(mod->perm)) {
4521+ /* ro --> rw */
4522+ err = -ENOMEM;
be52b249 4523+ br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
1facf9fc 4524+ if (br->br_wbr) {
86dc4139 4525+ err = au_wbr_init(br, sb, mod->perm);
1facf9fc 4526+ if (unlikely(err)) {
1c60b727 4527+ kfree(br->br_wbr);
1facf9fc 4528+ br->br_wbr = NULL;
4529+ }
4530+ }
4531+ }
076b876e
AM
4532+ if (unlikely(err))
4533+ goto out_bf;
4534+
4535+ if (au_br_fhsm(br->br_perm)) {
4536+ if (!au_br_fhsm(mod->perm)) {
4537+ /* fhsm --> non-fhsm */
4538+ au_br_fhsm_fin(br->br_fhsm);
1c60b727 4539+ kfree(br->br_fhsm);
076b876e
AM
4540+ br->br_fhsm = NULL;
4541+ }
4542+ } else if (au_br_fhsm(mod->perm))
4543+ /* non-fhsm --> fhsm */
4544+ br->br_fhsm = bf;
4545+
076b876e
AM
4546+ *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4547+ br->br_perm = mod->perm;
4548+ goto out; /* success */
1facf9fc 4549+
076b876e 4550+out_bf:
8b6a4947 4551+ kfree(bf);
076b876e
AM
4552+out:
4553+ AuTraceErr(err);
4554+ return err;
4555+}
4556+
4557+/* ---------------------------------------------------------------------- */
4558+
4559+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4560+{
4561+ int err;
4562+ struct kstatfs kstfs;
4563+
4564+ err = vfs_statfs(&br->br_path, &kstfs);
1facf9fc 4565+ if (!err) {
076b876e
AM
4566+ stfs->f_blocks = kstfs.f_blocks;
4567+ stfs->f_bavail = kstfs.f_bavail;
4568+ stfs->f_files = kstfs.f_files;
4569+ stfs->f_ffree = kstfs.f_ffree;
1facf9fc 4570+ }
4571+
1facf9fc 4572+ return err;
4573+}
7f207e10
AM
4574diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4575--- /usr/share/empty/fs/aufs/branch.h 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
4576+++ linux/fs/aufs/branch.h 2017-11-12 22:24:44.697577553 +0100
4577@@ -0,0 +1,333 @@
1facf9fc 4578+/*
a2654f78 4579+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 4580+ *
4581+ * This program, aufs is free software; you can redistribute it and/or modify
4582+ * it under the terms of the GNU General Public License as published by
4583+ * the Free Software Foundation; either version 2 of the License, or
4584+ * (at your option) any later version.
dece6358
AM
4585+ *
4586+ * This program is distributed in the hope that it will be useful,
4587+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4588+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4589+ * GNU General Public License for more details.
4590+ *
4591+ * You should have received a copy of the GNU General Public License
523b37e3 4592+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 4593+ */
4594+
4595+/*
4596+ * branch filesystems and xino for them
4597+ */
4598+
4599+#ifndef __AUFS_BRANCH_H__
4600+#define __AUFS_BRANCH_H__
4601+
4602+#ifdef __KERNEL__
4603+
1facf9fc 4604+#include <linux/mount.h>
8b6a4947 4605+#include "dirren.h"
4a4d8108 4606+#include "dynop.h"
1facf9fc 4607+#include "rwsem.h"
4608+#include "super.h"
4609+
4610+/* ---------------------------------------------------------------------- */
4611+
4612+/* a xino file */
4613+struct au_xino_file {
4614+ struct file *xi_file;
521ced18
JR
4615+ struct {
4616+ spinlock_t spin;
4617+ ino_t *array;
4618+ int total;
4619+ /* reserved for future use */
4620+ /* unsigned long *bitmap; */
4621+ wait_queue_head_t wqh;
4622+ } xi_nondir;
1facf9fc 4623+
4624+ /* todo: make xino files an array to support huge inode number */
4625+
4626+#ifdef CONFIG_DEBUG_FS
4627+ struct dentry *xi_dbgaufs;
4628+#endif
4629+};
4630+
076b876e
AM
4631+/* File-based Hierarchical Storage Management */
4632+struct au_br_fhsm {
4633+#ifdef CONFIG_AUFS_FHSM
4634+ struct mutex bf_lock;
4635+ unsigned long bf_jiffy;
4636+ struct aufs_stfs bf_stfs;
4637+ int bf_readable;
4638+#endif
4639+};
4640+
1facf9fc 4641+/* members for writable branch only */
4642+enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4643+struct au_wbr {
dece6358 4644+ struct au_rwsem wbr_wh_rwsem;
1facf9fc 4645+ struct dentry *wbr_wh[AuBrWh_Last];
4a4d8108 4646+ atomic_t wbr_wh_running;
1facf9fc 4647+#define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
4648+#define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
4649+#define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
4650+
4651+ /* mfs mode */
4652+ unsigned long long wbr_bytes;
4653+};
4654+
4a4d8108
AM
4655+/* ext2 has 3 types of operations at least, ext3 has 4 */
4656+#define AuBrDynOp (AuDyLast * 4)
4657+
1716fcea
AM
4658+#ifdef CONFIG_AUFS_HFSNOTIFY
4659+/* support for asynchronous destruction */
4660+struct au_br_hfsnotify {
4661+ struct fsnotify_group *hfsn_group;
4662+};
4663+#endif
4664+
392086de
AM
4665+/* sysfs entries */
4666+struct au_brsysfs {
4667+ char name[16];
4668+ struct attribute attr;
4669+};
4670+
4671+enum {
4672+ AuBrSysfs_BR,
4673+ AuBrSysfs_BRID,
4674+ AuBrSysfs_Last
4675+};
4676+
1facf9fc 4677+/* protected by superblock rwsem */
4678+struct au_branch {
4679+ struct au_xino_file br_xino;
4680+
4681+ aufs_bindex_t br_id;
4682+
4683+ int br_perm;
86dc4139 4684+ struct path br_path;
4a4d8108
AM
4685+ spinlock_t br_dykey_lock;
4686+ struct au_dykey *br_dykey[AuBrDynOp];
5afbbe0d 4687+ struct percpu_counter br_count;
1facf9fc 4688+
4689+ struct au_wbr *br_wbr;
076b876e 4690+ struct au_br_fhsm *br_fhsm;
1facf9fc 4691+
4692+ /* xino truncation */
1facf9fc 4693+ atomic_t br_xino_running;
4694+
027c5e7a 4695+#ifdef CONFIG_AUFS_HFSNOTIFY
1716fcea 4696+ struct au_br_hfsnotify *br_hfsn;
027c5e7a
AM
4697+#endif
4698+
1facf9fc 4699+#ifdef CONFIG_SYSFS
392086de
AM
4700+ /* entries under sysfs per mount-point */
4701+ struct au_brsysfs br_sysfs[AuBrSysfs_Last];
1facf9fc 4702+#endif
8b6a4947
AM
4703+
4704+ struct au_dr_br br_dirren;
1facf9fc 4705+};
4706+
4707+/* ---------------------------------------------------------------------- */
4708+
86dc4139
AM
4709+static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4710+{
4711+ return br->br_path.mnt;
4712+}
4713+
4714+static inline struct dentry *au_br_dentry(struct au_branch *br)
4715+{
4716+ return br->br_path.dentry;
4717+}
4718+
4719+static inline struct super_block *au_br_sb(struct au_branch *br)
4720+{
4721+ return au_br_mnt(br)->mnt_sb;
4722+}
4723+
5afbbe0d
AM
4724+static inline void au_br_get(struct au_branch *br)
4725+{
4726+ percpu_counter_inc(&br->br_count);
4727+}
4728+
4729+static inline void au_br_put(struct au_branch *br)
4730+{
4731+ percpu_counter_dec(&br->br_count);
4732+}
4733+
4734+static inline s64 au_br_count(struct au_branch *br)
4735+{
4736+ return percpu_counter_sum(&br->br_count);
4737+}
4738+
4739+static inline void au_br_count_init(struct au_branch *br)
4740+{
4741+ percpu_counter_init(&br->br_count, 0, GFP_NOFS);
4742+}
4743+
4744+static inline void au_br_count_fin(struct au_branch *br)
4745+{
4746+ percpu_counter_destroy(&br->br_count);
4747+}
4748+
1facf9fc 4749+static inline int au_br_rdonly(struct au_branch *br)
4750+{
8b6a4947 4751+ return (sb_rdonly(au_br_sb(br))
1facf9fc 4752+ || !au_br_writable(br->br_perm))
4753+ ? -EROFS : 0;
4754+}
4755+
4a4d8108 4756+static inline int au_br_hnotifyable(int brperm __maybe_unused)
1facf9fc 4757+{
4a4d8108 4758+#ifdef CONFIG_AUFS_HNOTIFY
1e00d052 4759+ return !(brperm & AuBrPerm_RR);
1facf9fc 4760+#else
4761+ return 0;
4762+#endif
4763+}
4764+
b912730e
AM
4765+static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4766+{
4767+ int err, exec_flag;
4768+
4769+ err = 0;
4770+ exec_flag = oflag & __FMODE_EXEC;
79b8bda9 4771+ if (unlikely(exec_flag && path_noexec(&br->br_path)))
b912730e
AM
4772+ err = -EACCES;
4773+
4774+ return err;
4775+}
4776+
1facf9fc 4777+/* ---------------------------------------------------------------------- */
4778+
4779+/* branch.c */
4780+struct au_sbinfo;
4781+void au_br_free(struct au_sbinfo *sinfo);
4782+int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4783+struct au_opt_add;
4784+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4785+struct au_opt_del;
4786+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
027c5e7a
AM
4787+long au_ibusy_ioctl(struct file *file, unsigned long arg);
4788+#ifdef CONFIG_COMPAT
4789+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4790+#endif
1facf9fc 4791+struct au_opt_mod;
4792+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4793+ int *do_refresh);
076b876e
AM
4794+struct aufs_stfs;
4795+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
1facf9fc 4796+
4797+/* xino.c */
4798+static const loff_t au_loff_max = LLONG_MAX;
4799+
4800+int au_xib_trunc(struct super_block *sb);
5527c038 4801+ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
1facf9fc 4802+ loff_t *pos);
5527c038
JR
4803+ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
4804+ size_t size, loff_t *pos);
1facf9fc 4805+struct file *au_xino_create2(struct file *base_file, struct file *copy_src);
4806+struct file *au_xino_create(struct super_block *sb, char *fname, int silent);
4807+ino_t au_xino_new_ino(struct super_block *sb);
b752ccd1 4808+void au_xino_delete_inode(struct inode *inode, const int unlinked);
1facf9fc 4809+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4810+ ino_t ino);
4811+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4812+ ino_t *ino);
4813+int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4814+ struct file *base_file, int do_test);
4815+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex);
4816+
4817+struct au_opt_xino;
4818+int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount);
4819+void au_xino_clr(struct super_block *sb);
4820+struct file *au_xino_def(struct super_block *sb);
4821+int au_xino_path(struct seq_file *seq, struct file *file);
4822+
521ced18
JR
4823+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4824+ ino_t h_ino, int idx);
4825+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4826+ int *idx);
4827+
1facf9fc 4828+/* ---------------------------------------------------------------------- */
4829+
4830+/* Superblock to branch */
4831+static inline
4832+aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4833+{
4834+ return au_sbr(sb, bindex)->br_id;
4835+}
4836+
4837+static inline
4838+struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4839+{
86dc4139 4840+ return au_br_mnt(au_sbr(sb, bindex));
1facf9fc 4841+}
4842+
4843+static inline
4844+struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4845+{
86dc4139 4846+ return au_br_sb(au_sbr(sb, bindex));
1facf9fc 4847+}
4848+
5afbbe0d
AM
4849+static inline void au_sbr_get(struct super_block *sb, aufs_bindex_t bindex)
4850+{
4851+ au_br_get(au_sbr(sb, bindex));
4852+}
4853+
1facf9fc 4854+static inline void au_sbr_put(struct super_block *sb, aufs_bindex_t bindex)
4855+{
5afbbe0d 4856+ au_br_put(au_sbr(sb, bindex));
1facf9fc 4857+}
4858+
4859+static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4860+{
4861+ return au_sbr(sb, bindex)->br_perm;
4862+}
4863+
4864+static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4865+{
4866+ return au_br_whable(au_sbr_perm(sb, bindex));
4867+}
4868+
4869+/* ---------------------------------------------------------------------- */
4870+
8b6a4947
AM
4871+#define wbr_wh_read_lock(wbr) au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4872+#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4873+#define wbr_wh_read_trylock(wbr) au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4874+#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
1facf9fc 4875+/*
8b6a4947
AM
4876+#define wbr_wh_read_trylock_nested(wbr) \
4877+ au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4878+#define wbr_wh_write_trylock_nested(wbr) \
4879+ au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4880+*/
1facf9fc 4881+
8b6a4947
AM
4882+#define wbr_wh_read_unlock(wbr) au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4883+#define wbr_wh_write_unlock(wbr) au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4884+#define wbr_wh_downgrade_lock(wbr) au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4885+
4886+#define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4887+#define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4888+#define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
dece6358 4889+
076b876e
AM
4890+/* ---------------------------------------------------------------------- */
4891+
4892+#ifdef CONFIG_AUFS_FHSM
4893+static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4894+{
4895+ mutex_init(&brfhsm->bf_lock);
4896+ brfhsm->bf_jiffy = 0;
4897+ brfhsm->bf_readable = 0;
4898+}
4899+
4900+static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4901+{
4902+ mutex_destroy(&brfhsm->bf_lock);
4903+}
4904+#else
4905+AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4906+AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4907+#endif
4908+
1facf9fc 4909+#endif /* __KERNEL__ */
4910+#endif /* __AUFS_BRANCH_H__ */
7f207e10
AM
4911diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4912--- /usr/share/empty/fs/aufs/conf.mk 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
4913+++ linux/fs/aufs/conf.mk 2017-11-12 22:24:44.704244405 +0100
4914@@ -0,0 +1,39 @@
4a4d8108
AM
4915+
4916+AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4917+
4918+define AuConf
4919+ifdef ${1}
4920+AuConfStr += ${1}=${${1}}
4921+endif
4922+endef
4923+
b752ccd1 4924+AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
e49829fe 4925+ SBILIST \
7f207e10 4926+ HNOTIFY HFSNOTIFY \
4a4d8108 4927+ EXPORT INO_T_64 \
c1595e42 4928+ XATTR \
076b876e 4929+ FHSM \
4a4d8108 4930+ RDU \
8b6a4947 4931+ DIRREN \
4a4d8108
AM
4932+ SHWH \
4933+ BR_RAMFS \
4934+ BR_FUSE POLL \
4935+ BR_HFSPLUS \
4936+ BDEV_LOOP \
b752ccd1
AM
4937+ DEBUG MAGIC_SYSRQ
4938+$(foreach i, ${AuConfAll}, \
4a4d8108
AM
4939+ $(eval $(call AuConf,CONFIG_AUFS_${i})))
4940+
4941+AuConfName = ${obj}/conf.str
4942+${AuConfName}.tmp: FORCE
4943+ @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4944+${AuConfName}: ${AuConfName}.tmp
4945+ @diff -q $< $@ > /dev/null 2>&1 || { \
4946+ echo ' GEN ' $@; \
4947+ cp -p $< $@; \
4948+ }
4949+FORCE:
4950+clean-files += ${AuConfName} ${AuConfName}.tmp
4951+${obj}/sysfs.o: ${AuConfName}
b752ccd1
AM
4952+
4953+-include ${srctree}/${src}/conf_priv.mk
7f207e10
AM
4954diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4955--- /usr/share/empty/fs/aufs/cpup.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
4956+++ linux/fs/aufs/cpup.c 2017-11-12 22:24:44.704244405 +0100
4957@@ -0,0 +1,1443 @@
1facf9fc 4958+/*
a2654f78 4959+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 4960+ *
4961+ * This program, aufs is free software; you can redistribute it and/or modify
4962+ * it under the terms of the GNU General Public License as published by
4963+ * the Free Software Foundation; either version 2 of the License, or
4964+ * (at your option) any later version.
dece6358
AM
4965+ *
4966+ * This program is distributed in the hope that it will be useful,
4967+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4968+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4969+ * GNU General Public License for more details.
4970+ *
4971+ * You should have received a copy of the GNU General Public License
523b37e3 4972+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 4973+ */
4974+
4975+/*
4976+ * copy-up functions, see wbr_policy.c for copy-down
4977+ */
4978+
4979+#include <linux/fs_stack.h>
dece6358 4980+#include <linux/mm.h>
8cdd5066 4981+#include <linux/task_work.h>
1facf9fc 4982+#include "aufs.h"
4983+
86dc4139 4984+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
1facf9fc 4985+{
4986+ const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
367653fa 4987+ | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
1facf9fc 4988+
86dc4139
AM
4989+ BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4990+
4991+ dst->i_flags |= iflags & ~mask;
1facf9fc 4992+ if (au_test_fs_notime(dst->i_sb))
4993+ dst->i_flags |= S_NOATIME | S_NOCMTIME;
4994+}
4995+
4996+void au_cpup_attr_timesizes(struct inode *inode)
4997+{
4998+ struct inode *h_inode;
4999+
5afbbe0d 5000+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 5001+ fsstack_copy_attr_times(inode, h_inode);
4a4d8108 5002+ fsstack_copy_inode_size(inode, h_inode);
1facf9fc 5003+}
5004+
5005+void au_cpup_attr_nlink(struct inode *inode, int force)
5006+{
5007+ struct inode *h_inode;
5008+ struct super_block *sb;
5afbbe0d 5009+ aufs_bindex_t bindex, bbot;
1facf9fc 5010+
5011+ sb = inode->i_sb;
5afbbe0d 5012+ bindex = au_ibtop(inode);
1facf9fc 5013+ h_inode = au_h_iptr(inode, bindex);
5014+ if (!force
5015+ && !S_ISDIR(h_inode->i_mode)
5016+ && au_opt_test(au_mntflags(sb), PLINK)
5017+ && au_plink_test(inode))
5018+ return;
5019+
7eafdf33
AM
5020+ /*
5021+ * 0 can happen in revalidating.
38d290e6
JR
5022+ * h_inode->i_mutex may not be held here, but it is harmless since once
5023+ * i_nlink reaches 0, it will never become positive except O_TMPFILE
5024+ * case.
5025+ * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
5026+ * the incorrect link count.
7eafdf33 5027+ */
92d182d2 5028+ set_nlink(inode, h_inode->i_nlink);
1facf9fc 5029+
5030+ /*
5031+ * fewer nlink makes find(1) noisy, but larger nlink doesn't.
5032+ * it may includes whplink directory.
5033+ */
5034+ if (S_ISDIR(h_inode->i_mode)) {
5afbbe0d
AM
5035+ bbot = au_ibbot(inode);
5036+ for (bindex++; bindex <= bbot; bindex++) {
1facf9fc 5037+ h_inode = au_h_iptr(inode, bindex);
5038+ if (h_inode)
5039+ au_add_nlink(inode, h_inode);
5040+ }
5041+ }
5042+}
5043+
5044+void au_cpup_attr_changeable(struct inode *inode)
5045+{
5046+ struct inode *h_inode;
5047+
5afbbe0d 5048+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 5049+ inode->i_mode = h_inode->i_mode;
5050+ inode->i_uid = h_inode->i_uid;
5051+ inode->i_gid = h_inode->i_gid;
5052+ au_cpup_attr_timesizes(inode);
86dc4139 5053+ au_cpup_attr_flags(inode, h_inode->i_flags);
1facf9fc 5054+}
5055+
5056+void au_cpup_igen(struct inode *inode, struct inode *h_inode)
5057+{
5058+ struct au_iinfo *iinfo = au_ii(inode);
5059+
1308ab2a 5060+ IiMustWriteLock(inode);
5061+
1facf9fc 5062+ iinfo->ii_higen = h_inode->i_generation;
5063+ iinfo->ii_hsb1 = h_inode->i_sb;
5064+}
5065+
5066+void au_cpup_attr_all(struct inode *inode, int force)
5067+{
5068+ struct inode *h_inode;
5069+
5afbbe0d 5070+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 5071+ au_cpup_attr_changeable(inode);
5072+ if (inode->i_nlink > 0)
5073+ au_cpup_attr_nlink(inode, force);
5074+ inode->i_rdev = h_inode->i_rdev;
5075+ inode->i_blkbits = h_inode->i_blkbits;
5076+ au_cpup_igen(inode, h_inode);
5077+}
5078+
5079+/* ---------------------------------------------------------------------- */
5080+
5081+/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
5082+
5083+/* keep the timestamps of the parent dir when cpup */
5084+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
5085+ struct path *h_path)
5086+{
5087+ struct inode *h_inode;
5088+
5089+ dt->dt_dentry = dentry;
5090+ dt->dt_h_path = *h_path;
5527c038 5091+ h_inode = d_inode(h_path->dentry);
1facf9fc 5092+ dt->dt_atime = h_inode->i_atime;
5093+ dt->dt_mtime = h_inode->i_mtime;
5094+ /* smp_mb(); */
5095+}
5096+
5097+void au_dtime_revert(struct au_dtime *dt)
5098+{
5099+ struct iattr attr;
5100+ int err;
5101+
5102+ attr.ia_atime = dt->dt_atime;
5103+ attr.ia_mtime = dt->dt_mtime;
5104+ attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
5105+ | ATTR_ATIME | ATTR_ATIME_SET;
5106+
523b37e3
AM
5107+ /* no delegation since this is a directory */
5108+ err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
1facf9fc 5109+ if (unlikely(err))
0c3ec466 5110+ pr_warn("restoring timestamps failed(%d). ignored\n", err);
1facf9fc 5111+}
5112+
5113+/* ---------------------------------------------------------------------- */
5114+
86dc4139
AM
5115+/* internal use only */
5116+struct au_cpup_reg_attr {
5117+ int valid;
5118+ struct kstat st;
5119+ unsigned int iflags; /* inode->i_flags */
5120+};
5121+
1facf9fc 5122+static noinline_for_stack
86dc4139
AM
5123+int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
5124+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5125+{
c1595e42 5126+ int err, sbits, icex;
7e9cd9fe
AM
5127+ unsigned int mnt_flags;
5128+ unsigned char verbose;
1facf9fc 5129+ struct iattr ia;
5130+ struct path h_path;
1308ab2a 5131+ struct inode *h_isrc, *h_idst;
86dc4139 5132+ struct kstat *h_st;
c1595e42 5133+ struct au_branch *br;
1facf9fc 5134+
5135+ h_path.dentry = au_h_dptr(dst, bindex);
5527c038 5136+ h_idst = d_inode(h_path.dentry);
c1595e42
JR
5137+ br = au_sbr(dst->d_sb, bindex);
5138+ h_path.mnt = au_br_mnt(br);
5527c038 5139+ h_isrc = d_inode(h_src);
1308ab2a 5140+ ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
1facf9fc 5141+ | ATTR_ATIME | ATTR_MTIME
5142+ | ATTR_ATIME_SET | ATTR_MTIME_SET;
86dc4139
AM
5143+ if (h_src_attr && h_src_attr->valid) {
5144+ h_st = &h_src_attr->st;
5145+ ia.ia_uid = h_st->uid;
5146+ ia.ia_gid = h_st->gid;
5147+ ia.ia_atime = h_st->atime;
5148+ ia.ia_mtime = h_st->mtime;
5149+ if (h_idst->i_mode != h_st->mode
5150+ && !S_ISLNK(h_idst->i_mode)) {
5151+ ia.ia_valid |= ATTR_MODE;
5152+ ia.ia_mode = h_st->mode;
5153+ }
5154+ sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
5155+ au_cpup_attr_flags(h_idst, h_src_attr->iflags);
5156+ } else {
5157+ ia.ia_uid = h_isrc->i_uid;
5158+ ia.ia_gid = h_isrc->i_gid;
5159+ ia.ia_atime = h_isrc->i_atime;
5160+ ia.ia_mtime = h_isrc->i_mtime;
5161+ if (h_idst->i_mode != h_isrc->i_mode
5162+ && !S_ISLNK(h_idst->i_mode)) {
5163+ ia.ia_valid |= ATTR_MODE;
5164+ ia.ia_mode = h_isrc->i_mode;
5165+ }
5166+ sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
5167+ au_cpup_attr_flags(h_idst, h_isrc->i_flags);
1308ab2a 5168+ }
523b37e3
AM
5169+ /* no delegation since it is just created */
5170+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 5171+
5172+ /* is this nfs only? */
5173+ if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
5174+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
5175+ ia.ia_mode = h_isrc->i_mode;
523b37e3 5176+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 5177+ }
5178+
c1595e42 5179+ icex = br->br_perm & AuBrAttr_ICEX;
7e9cd9fe
AM
5180+ if (!err) {
5181+ mnt_flags = au_mntflags(dst->d_sb);
5182+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
5183+ err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
5184+ }
c1595e42 5185+
1facf9fc 5186+ return err;
5187+}
5188+
5189+/* ---------------------------------------------------------------------- */
5190+
5191+static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
5192+ char *buf, unsigned long blksize)
5193+{
5194+ int err;
5195+ size_t sz, rbytes, wbytes;
5196+ unsigned char all_zero;
5197+ char *p, *zp;
febd17d6 5198+ struct inode *h_inode;
1facf9fc 5199+ /* reduce stack usage */
5200+ struct iattr *ia;
5201+
5202+ zp = page_address(ZERO_PAGE(0));
5203+ if (unlikely(!zp))
5204+ return -ENOMEM; /* possible? */
5205+
5206+ err = 0;
5207+ all_zero = 0;
5208+ while (len) {
5209+ AuDbg("len %lld\n", len);
5210+ sz = blksize;
5211+ if (len < blksize)
5212+ sz = len;
5213+
5214+ rbytes = 0;
5215+ /* todo: signal_pending? */
5216+ while (!rbytes || err == -EAGAIN || err == -EINTR) {
5217+ rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5218+ err = rbytes;
5219+ }
5220+ if (unlikely(err < 0))
5221+ break;
5222+
5223+ all_zero = 0;
5224+ if (len >= rbytes && rbytes == blksize)
5225+ all_zero = !memcmp(buf, zp, rbytes);
5226+ if (!all_zero) {
5227+ wbytes = rbytes;
5228+ p = buf;
5229+ while (wbytes) {
5230+ size_t b;
5231+
5232+ b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5233+ err = b;
5234+ /* todo: signal_pending? */
5235+ if (unlikely(err == -EAGAIN || err == -EINTR))
5236+ continue;
5237+ if (unlikely(err < 0))
5238+ break;
5239+ wbytes -= b;
5240+ p += b;
5241+ }
392086de
AM
5242+ if (unlikely(err < 0))
5243+ break;
1facf9fc 5244+ } else {
5245+ loff_t res;
5246+
5247+ AuLabel(hole);
5248+ res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5249+ err = res;
5250+ if (unlikely(res < 0))
5251+ break;
5252+ }
5253+ len -= rbytes;
5254+ err = 0;
5255+ }
5256+
5257+ /* the last block may be a hole */
5258+ if (!err && all_zero) {
5259+ AuLabel(last hole);
5260+
5261+ err = 1;
2000de60 5262+ if (au_test_nfs(dst->f_path.dentry->d_sb)) {
1facf9fc 5263+ /* nfs requires this step to make last hole */
5264+ /* is this only nfs? */
5265+ do {
5266+ /* todo: signal_pending? */
5267+ err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5268+ } while (err == -EAGAIN || err == -EINTR);
5269+ if (err == 1)
5270+ dst->f_pos--;
5271+ }
5272+
5273+ if (err == 1) {
5274+ ia = (void *)buf;
5275+ ia->ia_size = dst->f_pos;
5276+ ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5277+ ia->ia_file = dst;
febd17d6
JR
5278+ h_inode = file_inode(dst);
5279+ inode_lock_nested(h_inode, AuLsc_I_CHILD2);
523b37e3
AM
5280+ /* no delegation since it is just created */
5281+ err = vfsub_notify_change(&dst->f_path, ia,
5282+ /*delegated*/NULL);
febd17d6 5283+ inode_unlock(h_inode);
1facf9fc 5284+ }
5285+ }
5286+
5287+ return err;
5288+}
5289+
5290+int au_copy_file(struct file *dst, struct file *src, loff_t len)
5291+{
5292+ int err;
5293+ unsigned long blksize;
5294+ unsigned char do_kfree;
5295+ char *buf;
5296+
5297+ err = -ENOMEM;
2000de60 5298+ blksize = dst->f_path.dentry->d_sb->s_blocksize;
1facf9fc 5299+ if (!blksize || PAGE_SIZE < blksize)
5300+ blksize = PAGE_SIZE;
5301+ AuDbg("blksize %lu\n", blksize);
5302+ do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5303+ if (do_kfree)
5304+ buf = kmalloc(blksize, GFP_NOFS);
5305+ else
5306+ buf = (void *)__get_free_page(GFP_NOFS);
5307+ if (unlikely(!buf))
5308+ goto out;
5309+
5310+ if (len > (1 << 22))
5311+ AuDbg("copying a large file %lld\n", (long long)len);
5312+
5313+ src->f_pos = 0;
5314+ dst->f_pos = 0;
5315+ err = au_do_copy_file(dst, src, len, buf, blksize);
5316+ if (do_kfree)
1c60b727 5317+ kfree(buf);
1facf9fc 5318+ else
1c60b727 5319+ free_page((unsigned long)buf);
1facf9fc 5320+
4f0767ce 5321+out:
1facf9fc 5322+ return err;
5323+}
5324+
1c60b727
AM
5325+static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5326+{
5327+ int err;
5328+ struct super_block *h_src_sb;
5329+ struct inode *h_src_inode;
5330+
5331+ h_src_inode = file_inode(src);
5332+ h_src_sb = h_src_inode->i_sb;
5333+
5334+ /* XFS acquires inode_lock */
5335+ if (!au_test_xfs(h_src_sb))
5336+ err = au_copy_file(dst, src, len);
5337+ else {
3c1bdaff 5338+ inode_unlock_shared(h_src_inode);
1c60b727 5339+ err = au_copy_file(dst, src, len);
3c1bdaff 5340+ vfsub_inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
1c60b727
AM
5341+ }
5342+
5343+ return err;
5344+}
5345+
5346+static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5347+{
5348+ int err;
5349+ struct super_block *h_src_sb;
5350+ struct inode *h_src_inode;
5351+
5352+ h_src_inode = file_inode(src);
5353+ h_src_sb = h_src_inode->i_sb;
5354+ if (h_src_sb != file_inode(dst)->i_sb
5355+ || !dst->f_op->clone_file_range) {
5356+ err = au_do_copy(dst, src, len);
5357+ goto out;
5358+ }
5359+
5360+ if (!au_test_nfs(h_src_sb)) {
3c1bdaff 5361+ inode_unlock_shared(h_src_inode);
1c60b727 5362+ err = vfsub_clone_file_range(src, dst, len);
3c1bdaff 5363+ vfsub_inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
1c60b727
AM
5364+ } else
5365+ err = vfsub_clone_file_range(src, dst, len);
5366+ /* older XFS has a condition in cloning */
5367+ if (unlikely(err != -EOPNOTSUPP))
5368+ goto out;
5369+
5370+ /* the backend fs on NFS may not support cloning */
5371+ err = au_do_copy(dst, src, len);
5372+
5373+out:
5374+ AuTraceErr(err);
5375+ return err;
5376+}
5377+
1facf9fc 5378+/*
5379+ * to support a sparse file which is opened with O_APPEND,
5380+ * we need to close the file.
5381+ */
c2b27bf2 5382+static int au_cp_regular(struct au_cp_generic *cpg)
1facf9fc 5383+{
5384+ int err, i;
5385+ enum { SRC, DST };
5386+ struct {
5387+ aufs_bindex_t bindex;
5388+ unsigned int flags;
5389+ struct dentry *dentry;
392086de 5390+ int force_wr;
1facf9fc 5391+ struct file *file;
523b37e3 5392+ void *label;
1facf9fc 5393+ } *f, file[] = {
5394+ {
c2b27bf2 5395+ .bindex = cpg->bsrc,
1facf9fc 5396+ .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
523b37e3 5397+ .label = &&out
1facf9fc 5398+ },
5399+ {
c2b27bf2 5400+ .bindex = cpg->bdst,
1facf9fc 5401+ .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
392086de 5402+ .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
523b37e3 5403+ .label = &&out_src
1facf9fc 5404+ }
5405+ };
521ced18 5406+ struct super_block *sb, *h_src_sb;
e2f27e51 5407+ struct inode *h_src_inode;
8cdd5066 5408+ struct task_struct *tsk = current;
1facf9fc 5409+
5410+ /* bsrc branch can be ro/rw. */
c2b27bf2 5411+ sb = cpg->dentry->d_sb;
1facf9fc 5412+ f = file;
5413+ for (i = 0; i < 2; i++, f++) {
c2b27bf2
AM
5414+ f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5415+ f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
392086de 5416+ /*file*/NULL, f->force_wr);
1facf9fc 5417+ err = PTR_ERR(f->file);
5418+ if (IS_ERR(f->file))
5419+ goto *f->label;
1facf9fc 5420+ }
5421+
5422+ /* try stopping to update while we copyup */
e2f27e51 5423+ h_src_inode = d_inode(file[SRC].dentry);
521ced18
JR
5424+ h_src_sb = h_src_inode->i_sb;
5425+ if (!au_test_nfs(h_src_sb))
e2f27e51 5426+ IMustLock(h_src_inode);
1c60b727 5427+ err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
1facf9fc 5428+
8cdd5066
JR
5429+ /* i wonder if we had O_NO_DELAY_FPUT flag */
5430+ if (tsk->flags & PF_KTHREAD)
5431+ __fput_sync(file[DST].file);
5432+ else {
5433+ WARN(1, "%pD\nPlease report this warning to aufs-users ML",
5434+ file[DST].file);
5435+ fput(file[DST].file);
5436+ /*
5437+ * too bad.
5438+ * we have to call both since we don't know which place the file
5439+ * was added to.
5440+ */
5441+ task_work_run();
5442+ flush_delayed_fput();
5443+ }
1facf9fc 5444+ au_sbr_put(sb, file[DST].bindex);
523b37e3 5445+
4f0767ce 5446+out_src:
1facf9fc 5447+ fput(file[SRC].file);
5448+ au_sbr_put(sb, file[SRC].bindex);
4f0767ce 5449+out:
1facf9fc 5450+ return err;
5451+}
5452+
c2b27bf2 5453+static int au_do_cpup_regular(struct au_cp_generic *cpg,
86dc4139 5454+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5455+{
5456+ int err, rerr;
5457+ loff_t l;
86dc4139 5458+ struct path h_path;
38d290e6 5459+ struct inode *h_src_inode, *h_dst_inode;
1facf9fc 5460+
5461+ err = 0;
5527c038 5462+ h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
86dc4139 5463+ l = i_size_read(h_src_inode);
c2b27bf2
AM
5464+ if (cpg->len == -1 || l < cpg->len)
5465+ cpg->len = l;
5466+ if (cpg->len) {
86dc4139 5467+ /* try stopping to update while we are referencing */
3c1bdaff 5468+ vfsub_inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
c2b27bf2 5469+ au_pin_hdir_unlock(cpg->pin);
1facf9fc 5470+
c2b27bf2
AM
5471+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5472+ h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
86dc4139 5473+ h_src_attr->iflags = h_src_inode->i_flags;
5527c038 5474+ if (!au_test_nfs(h_src_inode->i_sb))
521ced18 5475+ err = vfsub_getattr(&h_path, &h_src_attr->st);
5527c038 5476+ else {
3c1bdaff 5477+ inode_unlock_shared(h_src_inode);
521ced18 5478+ err = vfsub_getattr(&h_path, &h_src_attr->st);
8b6a4947
AM
5479+ vfsub_inode_lock_shared_nested(h_src_inode,
5480+ AuLsc_I_CHILD);
5527c038 5481+ }
86dc4139 5482+ if (unlikely(err)) {
3c1bdaff 5483+ inode_unlock_shared(h_src_inode);
86dc4139
AM
5484+ goto out;
5485+ }
5486+ h_src_attr->valid = 1;
e2f27e51
AM
5487+ if (!au_test_nfs(h_src_inode->i_sb)) {
5488+ err = au_cp_regular(cpg);
3c1bdaff 5489+ inode_unlock_shared(h_src_inode);
e2f27e51 5490+ } else {
3c1bdaff 5491+ inode_unlock_shared(h_src_inode);
e2f27e51
AM
5492+ err = au_cp_regular(cpg);
5493+ }
c2b27bf2 5494+ rerr = au_pin_hdir_relock(cpg->pin);
86dc4139
AM
5495+ if (!err && rerr)
5496+ err = rerr;
1facf9fc 5497+ }
38d290e6
JR
5498+ if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5499+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5527c038 5500+ h_dst_inode = d_inode(h_path.dentry);
38d290e6
JR
5501+ spin_lock(&h_dst_inode->i_lock);
5502+ h_dst_inode->i_state |= I_LINKABLE;
5503+ spin_unlock(&h_dst_inode->i_lock);
5504+ }
1facf9fc 5505+
4f0767ce 5506+out:
1facf9fc 5507+ return err;
5508+}
5509+
5510+static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5511+ struct inode *h_dir)
5512+{
5513+ int err, symlen;
5514+ mm_segment_t old_fs;
b752ccd1
AM
5515+ union {
5516+ char *k;
5517+ char __user *u;
5518+ } sym;
1facf9fc 5519+
5520+ err = -ENOMEM;
537831f9 5521+ sym.k = (void *)__get_free_page(GFP_NOFS);
b752ccd1 5522+ if (unlikely(!sym.k))
1facf9fc 5523+ goto out;
5524+
9dbd164d 5525+ /* unnecessary to support mmap_sem since symlink is not mmap-able */
1facf9fc 5526+ old_fs = get_fs();
5527+ set_fs(KERNEL_DS);
a2654f78 5528+ symlen = vfs_readlink(h_src, sym.u, PATH_MAX);
1facf9fc 5529+ err = symlen;
5530+ set_fs(old_fs);
5531+
5532+ if (symlen > 0) {
b752ccd1
AM
5533+ sym.k[symlen] = 0;
5534+ err = vfsub_symlink(h_dir, h_path, sym.k);
1facf9fc 5535+ }
1c60b727 5536+ free_page((unsigned long)sym.k);
1facf9fc 5537+
4f0767ce 5538+out:
1facf9fc 5539+ return err;
5540+}
5541+
8cdd5066
JR
5542+/*
5543+ * regardless 'acl' option, reset all ACL.
5544+ * All ACL will be copied up later from the original entry on the lower branch.
5545+ */
5546+static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5547+{
5548+ int err;
5549+ struct dentry *h_dentry;
5550+ struct inode *h_inode;
5551+
5552+ h_dentry = h_path->dentry;
5553+ h_inode = d_inode(h_dentry);
5554+ /* forget_all_cached_acls(h_inode)); */
5555+ err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5556+ AuTraceErr(err);
5557+ if (err == -EOPNOTSUPP)
5558+ err = 0;
5559+ if (!err)
5560+ err = vfsub_acl_chmod(h_inode, mode);
5561+
5562+ AuTraceErr(err);
5563+ return err;
5564+}
5565+
5566+static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5567+ struct inode *h_dir, struct path *h_path)
5568+{
5569+ int err;
5570+ struct inode *dir, *inode;
5571+
5572+ err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
5573+ AuTraceErr(err);
5574+ if (err == -EOPNOTSUPP)
5575+ err = 0;
5576+ if (unlikely(err))
5577+ goto out;
5578+
5579+ /*
5580+ * strange behaviour from the users view,
5581+ * particularry setattr case
5582+ */
5583+ dir = d_inode(dst_parent);
5afbbe0d 5584+ if (au_ibtop(dir) == cpg->bdst)
8cdd5066
JR
5585+ au_cpup_attr_nlink(dir, /*force*/1);
5586+ inode = d_inode(cpg->dentry);
5587+ au_cpup_attr_nlink(inode, /*force*/1);
5588+
5589+out:
5590+ return err;
5591+}
5592+
1facf9fc 5593+static noinline_for_stack
c2b27bf2 5594+int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
86dc4139 5595+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5596+{
5597+ int err;
5598+ umode_t mode;
5599+ unsigned int mnt_flags;
076b876e 5600+ unsigned char isdir, isreg, force;
c2b27bf2 5601+ const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5602+ struct au_dtime dt;
5603+ struct path h_path;
5604+ struct dentry *h_src, *h_dst, *h_parent;
8cdd5066 5605+ struct inode *h_inode, *h_dir;
1facf9fc 5606+ struct super_block *sb;
5607+
5608+ /* bsrc branch can be ro/rw. */
c2b27bf2 5609+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038
JR
5610+ h_inode = d_inode(h_src);
5611+ AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
1facf9fc 5612+
5613+ /* try stopping to be referenced while we are creating */
c2b27bf2
AM
5614+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5615+ if (au_ftest_cpup(cpg->flags, RENAME))
86dc4139
AM
5616+ AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5617+ AUFS_WH_PFX_LEN));
1facf9fc 5618+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5619+ h_dir = d_inode(h_parent);
1facf9fc 5620+ IMustLock(h_dir);
5621+ AuDebugOn(h_parent != h_dst->d_parent);
5622+
c2b27bf2
AM
5623+ sb = cpg->dentry->d_sb;
5624+ h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
1facf9fc 5625+ if (do_dt) {
5626+ h_path.dentry = h_parent;
5627+ au_dtime_store(&dt, dst_parent, &h_path);
5628+ }
5629+ h_path.dentry = h_dst;
5630+
076b876e 5631+ isreg = 0;
1facf9fc 5632+ isdir = 0;
5633+ mode = h_inode->i_mode;
5634+ switch (mode & S_IFMT) {
5635+ case S_IFREG:
076b876e 5636+ isreg = 1;
8cdd5066 5637+ err = vfsub_create(h_dir, &h_path, S_IRUSR | S_IWUSR,
b4510431 5638+ /*want_excl*/true);
1facf9fc 5639+ if (!err)
c2b27bf2 5640+ err = au_do_cpup_regular(cpg, h_src_attr);
1facf9fc 5641+ break;
5642+ case S_IFDIR:
5643+ isdir = 1;
5644+ err = vfsub_mkdir(h_dir, &h_path, mode);
8cdd5066
JR
5645+ if (!err)
5646+ err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
1facf9fc 5647+ break;
5648+ case S_IFLNK:
5649+ err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5650+ break;
5651+ case S_IFCHR:
5652+ case S_IFBLK:
5653+ AuDebugOn(!capable(CAP_MKNOD));
5654+ /*FALLTHROUGH*/
5655+ case S_IFIFO:
5656+ case S_IFSOCK:
5657+ err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5658+ break;
5659+ default:
5660+ AuIOErr("Unknown inode type 0%o\n", mode);
5661+ err = -EIO;
5662+ }
8cdd5066
JR
5663+ if (!err)
5664+ err = au_reset_acl(h_dir, &h_path, mode);
1facf9fc 5665+
5666+ mnt_flags = au_mntflags(sb);
5667+ if (!au_opt_test(mnt_flags, UDBA_NONE)
5668+ && !isdir
5669+ && au_opt_test(mnt_flags, XINO)
38d290e6
JR
5670+ && (h_inode->i_nlink == 1
5671+ || (h_inode->i_state & I_LINKABLE))
1facf9fc 5672+ /* todo: unnecessary? */
5527c038 5673+ /* && d_inode(cpg->dentry)->i_nlink == 1 */
c2b27bf2
AM
5674+ && cpg->bdst < cpg->bsrc
5675+ && !au_ftest_cpup(cpg->flags, KEEPLINO))
5676+ au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
1facf9fc 5677+ /* ignore this error */
5678+
076b876e
AM
5679+ if (!err) {
5680+ force = 0;
5681+ if (isreg) {
5682+ force = !!cpg->len;
5683+ if (cpg->len == -1)
5684+ force = !!i_size_read(h_inode);
5685+ }
5686+ au_fhsm_wrote(sb, cpg->bdst, force);
5687+ }
5688+
1facf9fc 5689+ if (do_dt)
5690+ au_dtime_revert(&dt);
5691+ return err;
5692+}
5693+
392086de 5694+static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
86dc4139
AM
5695+{
5696+ int err;
392086de 5697+ struct dentry *dentry, *h_dentry, *h_parent, *parent;
86dc4139 5698+ struct inode *h_dir;
392086de 5699+ aufs_bindex_t bdst;
86dc4139 5700+
392086de
AM
5701+ dentry = cpg->dentry;
5702+ bdst = cpg->bdst;
5703+ h_dentry = au_h_dptr(dentry, bdst);
5704+ if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5705+ dget(h_dentry);
5706+ au_set_h_dptr(dentry, bdst, NULL);
5707+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
5708+ if (!err)
5709+ h_path->dentry = dget(au_h_dptr(dentry, bdst));
86dc4139 5710+ au_set_h_dptr(dentry, bdst, h_dentry);
392086de
AM
5711+ } else {
5712+ err = 0;
5713+ parent = dget_parent(dentry);
5714+ h_parent = au_h_dptr(parent, bdst);
5715+ dput(parent);
5716+ h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5717+ if (IS_ERR(h_path->dentry))
5718+ err = PTR_ERR(h_path->dentry);
86dc4139 5719+ }
392086de
AM
5720+ if (unlikely(err))
5721+ goto out;
86dc4139 5722+
86dc4139 5723+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 5724+ h_dir = d_inode(h_parent);
86dc4139 5725+ IMustLock(h_dir);
523b37e3
AM
5726+ AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5727+ /* no delegation since it is just created */
f2c43d5f
AM
5728+ err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5729+ /*flags*/0);
86dc4139
AM
5730+ dput(h_path->dentry);
5731+
5732+out:
5733+ return err;
5734+}
5735+
1facf9fc 5736+/*
5737+ * copyup the @dentry from @bsrc to @bdst.
5738+ * the caller must set the both of lower dentries.
5739+ * @len is for truncating when it is -1 copyup the entire file.
5740+ * in link/rename cases, @dst_parent may be different from the real one.
c2b27bf2 5741+ * basic->bsrc can be larger than basic->bdst.
f2c43d5f
AM
5742+ * aufs doesn't touch the credential so
5743+ * security_inode_copy_up{,_xattr}() are unnecrssary.
1facf9fc 5744+ */
c2b27bf2 5745+static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5746+{
5747+ int err, rerr;
5afbbe0d 5748+ aufs_bindex_t old_ibtop;
1facf9fc 5749+ unsigned char isdir, plink;
1facf9fc 5750+ struct dentry *h_src, *h_dst, *h_parent;
5527c038 5751+ struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
1facf9fc 5752+ struct super_block *sb;
86dc4139 5753+ struct au_branch *br;
c2b27bf2
AM
5754+ /* to reuduce stack size */
5755+ struct {
5756+ struct au_dtime dt;
5757+ struct path h_path;
5758+ struct au_cpup_reg_attr h_src_attr;
5759+ } *a;
1facf9fc 5760+
c2b27bf2
AM
5761+ err = -ENOMEM;
5762+ a = kmalloc(sizeof(*a), GFP_NOFS);
5763+ if (unlikely(!a))
5764+ goto out;
5765+ a->h_src_attr.valid = 0;
1facf9fc 5766+
c2b27bf2
AM
5767+ sb = cpg->dentry->d_sb;
5768+ br = au_sbr(sb, cpg->bdst);
5769+ a->h_path.mnt = au_br_mnt(br);
5770+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
1facf9fc 5771+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5772+ h_dir = d_inode(h_parent);
1facf9fc 5773+ IMustLock(h_dir);
5774+
c2b27bf2 5775+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5776+ inode = d_inode(cpg->dentry);
1facf9fc 5777+
5778+ if (!dst_parent)
c2b27bf2 5779+ dst_parent = dget_parent(cpg->dentry);
1facf9fc 5780+ else
5781+ dget(dst_parent);
5782+
5783+ plink = !!au_opt_test(au_mntflags(sb), PLINK);
c2b27bf2 5784+ dst_inode = au_h_iptr(inode, cpg->bdst);
1facf9fc 5785+ if (dst_inode) {
5786+ if (unlikely(!plink)) {
5787+ err = -EIO;
027c5e7a
AM
5788+ AuIOErr("hi%lu(i%lu) exists on b%d "
5789+ "but plink is disabled\n",
c2b27bf2
AM
5790+ dst_inode->i_ino, inode->i_ino, cpg->bdst);
5791+ goto out_parent;
1facf9fc 5792+ }
5793+
5794+ if (dst_inode->i_nlink) {
c2b27bf2 5795+ const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5796+
c2b27bf2 5797+ h_src = au_plink_lkup(inode, cpg->bdst);
1facf9fc 5798+ err = PTR_ERR(h_src);
5799+ if (IS_ERR(h_src))
c2b27bf2 5800+ goto out_parent;
5527c038 5801+ if (unlikely(d_is_negative(h_src))) {
1facf9fc 5802+ err = -EIO;
79b8bda9 5803+ AuIOErr("i%lu exists on b%d "
027c5e7a 5804+ "but not pseudo-linked\n",
79b8bda9 5805+ inode->i_ino, cpg->bdst);
1facf9fc 5806+ dput(h_src);
c2b27bf2 5807+ goto out_parent;
1facf9fc 5808+ }
5809+
5810+ if (do_dt) {
c2b27bf2
AM
5811+ a->h_path.dentry = h_parent;
5812+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
1facf9fc 5813+ }
86dc4139 5814+
c2b27bf2 5815+ a->h_path.dentry = h_dst;
523b37e3
AM
5816+ delegated = NULL;
5817+ err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
c2b27bf2 5818+ if (!err && au_ftest_cpup(cpg->flags, RENAME))
392086de 5819+ err = au_do_ren_after_cpup(cpg, &a->h_path);
1facf9fc 5820+ if (do_dt)
c2b27bf2 5821+ au_dtime_revert(&a->dt);
523b37e3
AM
5822+ if (unlikely(err == -EWOULDBLOCK)) {
5823+ pr_warn("cannot retry for NFSv4 delegation"
5824+ " for an internal link\n");
5825+ iput(delegated);
5826+ }
1facf9fc 5827+ dput(h_src);
c2b27bf2 5828+ goto out_parent;
1facf9fc 5829+ } else
5830+ /* todo: cpup_wh_file? */
5831+ /* udba work */
4a4d8108 5832+ au_update_ibrange(inode, /*do_put_zero*/1);
1facf9fc 5833+ }
5834+
86dc4139 5835+ isdir = S_ISDIR(inode->i_mode);
5afbbe0d 5836+ old_ibtop = au_ibtop(inode);
c2b27bf2 5837+ err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
1facf9fc 5838+ if (unlikely(err))
86dc4139 5839+ goto out_rev;
5527c038 5840+ dst_inode = d_inode(h_dst);
febd17d6 5841+ inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
86dc4139 5842+ /* todo: necessary? */
c2b27bf2 5843+ /* au_pin_hdir_unlock(cpg->pin); */
1facf9fc 5844+
c2b27bf2 5845+ err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
86dc4139
AM
5846+ if (unlikely(err)) {
5847+ /* todo: necessary? */
c2b27bf2 5848+ /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
febd17d6 5849+ inode_unlock(dst_inode);
86dc4139
AM
5850+ goto out_rev;
5851+ }
5852+
5afbbe0d 5853+ if (cpg->bdst < old_ibtop) {
86dc4139 5854+ if (S_ISREG(inode->i_mode)) {
c2b27bf2 5855+ err = au_dy_iaop(inode, cpg->bdst, dst_inode);
86dc4139 5856+ if (unlikely(err)) {
c2b27bf2
AM
5857+ /* ignore an error */
5858+ /* au_pin_hdir_relock(cpg->pin); */
febd17d6 5859+ inode_unlock(dst_inode);
86dc4139 5860+ goto out_rev;
4a4d8108 5861+ }
4a4d8108 5862+ }
5afbbe0d 5863+ au_set_ibtop(inode, cpg->bdst);
c2b27bf2 5864+ } else
5afbbe0d 5865+ au_set_ibbot(inode, cpg->bdst);
c2b27bf2 5866+ au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
86dc4139
AM
5867+ au_hi_flags(inode, isdir));
5868+
5869+ /* todo: necessary? */
c2b27bf2 5870+ /* err = au_pin_hdir_relock(cpg->pin); */
febd17d6 5871+ inode_unlock(dst_inode);
86dc4139
AM
5872+ if (unlikely(err))
5873+ goto out_rev;
5874+
5527c038 5875+ src_inode = d_inode(h_src);
86dc4139 5876+ if (!isdir
5527c038
JR
5877+ && (src_inode->i_nlink > 1
5878+ || src_inode->i_state & I_LINKABLE)
86dc4139 5879+ && plink)
c2b27bf2 5880+ au_plink_append(inode, cpg->bdst, h_dst);
86dc4139 5881+
c2b27bf2
AM
5882+ if (au_ftest_cpup(cpg->flags, RENAME)) {
5883+ a->h_path.dentry = h_dst;
392086de 5884+ err = au_do_ren_after_cpup(cpg, &a->h_path);
86dc4139
AM
5885+ }
5886+ if (!err)
c2b27bf2 5887+ goto out_parent; /* success */
1facf9fc 5888+
5889+ /* revert */
4a4d8108 5890+out_rev:
c2b27bf2
AM
5891+ a->h_path.dentry = h_parent;
5892+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
5893+ a->h_path.dentry = h_dst;
86dc4139 5894+ rerr = 0;
5527c038 5895+ if (d_is_positive(h_dst)) {
523b37e3
AM
5896+ if (!isdir) {
5897+ /* no delegation since it is just created */
5898+ rerr = vfsub_unlink(h_dir, &a->h_path,
5899+ /*delegated*/NULL, /*force*/0);
5900+ } else
c2b27bf2 5901+ rerr = vfsub_rmdir(h_dir, &a->h_path);
86dc4139 5902+ }
c2b27bf2 5903+ au_dtime_revert(&a->dt);
1facf9fc 5904+ if (rerr) {
5905+ AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5906+ err = -EIO;
5907+ }
c2b27bf2 5908+out_parent:
1facf9fc 5909+ dput(dst_parent);
1c60b727 5910+ kfree(a);
c2b27bf2 5911+out:
1facf9fc 5912+ return err;
5913+}
5914+
7e9cd9fe 5915+#if 0 /* reserved */
1facf9fc 5916+struct au_cpup_single_args {
5917+ int *errp;
c2b27bf2 5918+ struct au_cp_generic *cpg;
1facf9fc 5919+ struct dentry *dst_parent;
5920+};
5921+
5922+static void au_call_cpup_single(void *args)
5923+{
5924+ struct au_cpup_single_args *a = args;
86dc4139 5925+
c2b27bf2
AM
5926+ au_pin_hdir_acquire_nest(a->cpg->pin);
5927+ *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5928+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 5929+}
c2b27bf2 5930+#endif
1facf9fc 5931+
53392da6
AM
5932+/*
5933+ * prevent SIGXFSZ in copy-up.
5934+ * testing CAP_MKNOD is for generic fs,
5935+ * but CAP_FSETID is for xfs only, currently.
5936+ */
86dc4139 5937+static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
53392da6
AM
5938+{
5939+ int do_sio;
86dc4139
AM
5940+ struct super_block *sb;
5941+ struct inode *h_dir;
53392da6
AM
5942+
5943+ do_sio = 0;
86dc4139 5944+ sb = au_pinned_parent(pin)->d_sb;
53392da6
AM
5945+ if (!au_wkq_test()
5946+ && (!au_sbi(sb)->si_plink_maint_pid
5947+ || au_plink_maint(sb, AuLock_NOPLM))) {
5948+ switch (mode & S_IFMT) {
5949+ case S_IFREG:
5950+ /* no condition about RLIMIT_FSIZE and the file size */
5951+ do_sio = 1;
5952+ break;
5953+ case S_IFCHR:
5954+ case S_IFBLK:
5955+ do_sio = !capable(CAP_MKNOD);
5956+ break;
5957+ }
5958+ if (!do_sio)
5959+ do_sio = ((mode & (S_ISUID | S_ISGID))
5960+ && !capable(CAP_FSETID));
86dc4139
AM
5961+ /* this workaround may be removed in the future */
5962+ if (!do_sio) {
5963+ h_dir = au_pinned_h_dir(pin);
5964+ do_sio = h_dir->i_mode & S_ISVTX;
5965+ }
53392da6
AM
5966+ }
5967+
5968+ return do_sio;
5969+}
5970+
7e9cd9fe 5971+#if 0 /* reserved */
c2b27bf2 5972+int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5973+{
5974+ int err, wkq_err;
1facf9fc 5975+ struct dentry *h_dentry;
5976+
c2b27bf2 5977+ h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5978+ if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
c2b27bf2 5979+ err = au_cpup_single(cpg, dst_parent);
1facf9fc 5980+ else {
5981+ struct au_cpup_single_args args = {
5982+ .errp = &err,
c2b27bf2
AM
5983+ .cpg = cpg,
5984+ .dst_parent = dst_parent
1facf9fc 5985+ };
5986+ wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5987+ if (unlikely(wkq_err))
5988+ err = wkq_err;
5989+ }
5990+
5991+ return err;
5992+}
c2b27bf2 5993+#endif
1facf9fc 5994+
5995+/*
5996+ * copyup the @dentry from the first active lower branch to @bdst,
5997+ * using au_cpup_single().
5998+ */
c2b27bf2 5999+static int au_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 6000+{
6001+ int err;
c2b27bf2
AM
6002+ unsigned int flags_orig;
6003+ struct dentry *dentry;
6004+
6005+ AuDebugOn(cpg->bsrc < 0);
1facf9fc 6006+
c2b27bf2 6007+ dentry = cpg->dentry;
86dc4139 6008+ DiMustWriteLock(dentry);
1facf9fc 6009+
c2b27bf2 6010+ err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
1facf9fc 6011+ if (!err) {
c2b27bf2
AM
6012+ flags_orig = cpg->flags;
6013+ au_fset_cpup(cpg->flags, RENAME);
6014+ err = au_cpup_single(cpg, NULL);
6015+ cpg->flags = flags_orig;
1facf9fc 6016+ if (!err)
6017+ return 0; /* success */
6018+
6019+ /* revert */
c2b27bf2 6020+ au_set_h_dptr(dentry, cpg->bdst, NULL);
5afbbe0d 6021+ au_set_dbtop(dentry, cpg->bsrc);
1facf9fc 6022+ }
6023+
6024+ return err;
6025+}
6026+
6027+struct au_cpup_simple_args {
6028+ int *errp;
c2b27bf2 6029+ struct au_cp_generic *cpg;
1facf9fc 6030+};
6031+
6032+static void au_call_cpup_simple(void *args)
6033+{
6034+ struct au_cpup_simple_args *a = args;
86dc4139 6035+
c2b27bf2
AM
6036+ au_pin_hdir_acquire_nest(a->cpg->pin);
6037+ *a->errp = au_cpup_simple(a->cpg);
6038+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 6039+}
6040+
c2b27bf2 6041+static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 6042+{
6043+ int err, wkq_err;
c2b27bf2
AM
6044+ struct dentry *dentry, *parent;
6045+ struct file *h_file;
1facf9fc 6046+ struct inode *h_dir;
6047+
c2b27bf2
AM
6048+ dentry = cpg->dentry;
6049+ h_file = NULL;
6050+ if (au_ftest_cpup(cpg->flags, HOPEN)) {
6051+ AuDebugOn(cpg->bsrc < 0);
392086de 6052+ h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
c2b27bf2
AM
6053+ err = PTR_ERR(h_file);
6054+ if (IS_ERR(h_file))
6055+ goto out;
6056+ }
6057+
1facf9fc 6058+ parent = dget_parent(dentry);
5527c038 6059+ h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
53392da6 6060+ if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
5527c038 6061+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 6062+ err = au_cpup_simple(cpg);
1facf9fc 6063+ else {
6064+ struct au_cpup_simple_args args = {
6065+ .errp = &err,
c2b27bf2 6066+ .cpg = cpg
1facf9fc 6067+ };
6068+ wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
6069+ if (unlikely(wkq_err))
6070+ err = wkq_err;
6071+ }
6072+
6073+ dput(parent);
c2b27bf2
AM
6074+ if (h_file)
6075+ au_h_open_post(dentry, cpg->bsrc, h_file);
6076+
6077+out:
1facf9fc 6078+ return err;
6079+}
6080+
c2b27bf2 6081+int au_sio_cpup_simple(struct au_cp_generic *cpg)
367653fa 6082+{
5afbbe0d 6083+ aufs_bindex_t bsrc, bbot;
c2b27bf2 6084+ struct dentry *dentry, *h_dentry;
367653fa 6085+
c2b27bf2
AM
6086+ if (cpg->bsrc < 0) {
6087+ dentry = cpg->dentry;
5afbbe0d
AM
6088+ bbot = au_dbbot(dentry);
6089+ for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
c2b27bf2
AM
6090+ h_dentry = au_h_dptr(dentry, bsrc);
6091+ if (h_dentry) {
5527c038 6092+ AuDebugOn(d_is_negative(h_dentry));
c2b27bf2
AM
6093+ break;
6094+ }
6095+ }
5afbbe0d 6096+ AuDebugOn(bsrc > bbot);
c2b27bf2 6097+ cpg->bsrc = bsrc;
367653fa 6098+ }
c2b27bf2
AM
6099+ AuDebugOn(cpg->bsrc <= cpg->bdst);
6100+ return au_do_sio_cpup_simple(cpg);
6101+}
367653fa 6102+
c2b27bf2
AM
6103+int au_sio_cpdown_simple(struct au_cp_generic *cpg)
6104+{
6105+ AuDebugOn(cpg->bdst <= cpg->bsrc);
6106+ return au_do_sio_cpup_simple(cpg);
367653fa
AM
6107+}
6108+
1facf9fc 6109+/* ---------------------------------------------------------------------- */
6110+
6111+/*
6112+ * copyup the deleted file for writing.
6113+ */
c2b27bf2
AM
6114+static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
6115+ struct file *file)
1facf9fc 6116+{
6117+ int err;
c2b27bf2
AM
6118+ unsigned int flags_orig;
6119+ aufs_bindex_t bsrc_orig;
c2b27bf2 6120+ struct au_dinfo *dinfo;
5afbbe0d
AM
6121+ struct {
6122+ struct au_hdentry *hd;
6123+ struct dentry *h_dentry;
6124+ } hdst, hsrc;
1facf9fc 6125+
c2b27bf2 6126+ dinfo = au_di(cpg->dentry);
1308ab2a 6127+ AuRwMustWriteLock(&dinfo->di_rwsem);
6128+
c2b27bf2 6129+ bsrc_orig = cpg->bsrc;
5afbbe0d
AM
6130+ cpg->bsrc = dinfo->di_btop;
6131+ hdst.hd = au_hdentry(dinfo, cpg->bdst);
6132+ hdst.h_dentry = hdst.hd->hd_dentry;
6133+ hdst.hd->hd_dentry = wh_dentry;
6134+ dinfo->di_btop = cpg->bdst;
6135+
6136+ hsrc.h_dentry = NULL;
027c5e7a 6137+ if (file) {
5afbbe0d
AM
6138+ hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
6139+ hsrc.h_dentry = hsrc.hd->hd_dentry;
6140+ hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
027c5e7a 6141+ }
c2b27bf2
AM
6142+ flags_orig = cpg->flags;
6143+ cpg->flags = !AuCpup_DTIME;
6144+ err = au_cpup_single(cpg, /*h_parent*/NULL);
6145+ cpg->flags = flags_orig;
027c5e7a
AM
6146+ if (file) {
6147+ if (!err)
6148+ err = au_reopen_nondir(file);
5afbbe0d 6149+ hsrc.hd->hd_dentry = hsrc.h_dentry;
1facf9fc 6150+ }
5afbbe0d
AM
6151+ hdst.hd->hd_dentry = hdst.h_dentry;
6152+ dinfo->di_btop = cpg->bsrc;
c2b27bf2 6153+ cpg->bsrc = bsrc_orig;
1facf9fc 6154+
6155+ return err;
6156+}
6157+
c2b27bf2 6158+static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 6159+{
6160+ int err;
c2b27bf2 6161+ aufs_bindex_t bdst;
1facf9fc 6162+ struct au_dtime dt;
c2b27bf2 6163+ struct dentry *dentry, *parent, *h_parent, *wh_dentry;
1facf9fc 6164+ struct au_branch *br;
6165+ struct path h_path;
6166+
c2b27bf2
AM
6167+ dentry = cpg->dentry;
6168+ bdst = cpg->bdst;
1facf9fc 6169+ br = au_sbr(dentry->d_sb, bdst);
6170+ parent = dget_parent(dentry);
6171+ h_parent = au_h_dptr(parent, bdst);
6172+ wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
6173+ err = PTR_ERR(wh_dentry);
6174+ if (IS_ERR(wh_dentry))
6175+ goto out;
6176+
6177+ h_path.dentry = h_parent;
86dc4139 6178+ h_path.mnt = au_br_mnt(br);
1facf9fc 6179+ au_dtime_store(&dt, parent, &h_path);
c2b27bf2 6180+ err = au_do_cpup_wh(cpg, wh_dentry, file);
1facf9fc 6181+ if (unlikely(err))
6182+ goto out_wh;
6183+
6184+ dget(wh_dentry);
6185+ h_path.dentry = wh_dentry;
2000de60 6186+ if (!d_is_dir(wh_dentry)) {
523b37e3 6187+ /* no delegation since it is just created */
5527c038 6188+ err = vfsub_unlink(d_inode(h_parent), &h_path,
523b37e3
AM
6189+ /*delegated*/NULL, /*force*/0);
6190+ } else
5527c038 6191+ err = vfsub_rmdir(d_inode(h_parent), &h_path);
1facf9fc 6192+ if (unlikely(err)) {
523b37e3
AM
6193+ AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6194+ wh_dentry, err);
1facf9fc 6195+ err = -EIO;
6196+ }
6197+ au_dtime_revert(&dt);
5527c038 6198+ au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
1facf9fc 6199+
4f0767ce 6200+out_wh:
1facf9fc 6201+ dput(wh_dentry);
4f0767ce 6202+out:
1facf9fc 6203+ dput(parent);
6204+ return err;
6205+}
6206+
6207+struct au_cpup_wh_args {
6208+ int *errp;
c2b27bf2 6209+ struct au_cp_generic *cpg;
1facf9fc 6210+ struct file *file;
6211+};
6212+
6213+static void au_call_cpup_wh(void *args)
6214+{
6215+ struct au_cpup_wh_args *a = args;
86dc4139 6216+
c2b27bf2
AM
6217+ au_pin_hdir_acquire_nest(a->cpg->pin);
6218+ *a->errp = au_cpup_wh(a->cpg, a->file);
6219+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 6220+}
6221+
c2b27bf2 6222+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 6223+{
6224+ int err, wkq_err;
c2b27bf2 6225+ aufs_bindex_t bdst;
c1595e42 6226+ struct dentry *dentry, *parent, *h_orph, *h_parent;
86dc4139 6227+ struct inode *dir, *h_dir, *h_tmpdir;
1facf9fc 6228+ struct au_wbr *wbr;
c2b27bf2 6229+ struct au_pin wh_pin, *pin_orig;
1facf9fc 6230+
c2b27bf2
AM
6231+ dentry = cpg->dentry;
6232+ bdst = cpg->bdst;
1facf9fc 6233+ parent = dget_parent(dentry);
5527c038 6234+ dir = d_inode(parent);
1facf9fc 6235+ h_orph = NULL;
6236+ h_parent = NULL;
6237+ h_dir = au_igrab(au_h_iptr(dir, bdst));
6238+ h_tmpdir = h_dir;
c2b27bf2 6239+ pin_orig = NULL;
1facf9fc 6240+ if (!h_dir->i_nlink) {
6241+ wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6242+ h_orph = wbr->wbr_orph;
6243+
6244+ h_parent = dget(au_h_dptr(parent, bdst));
1facf9fc 6245+ au_set_h_dptr(parent, bdst, dget(h_orph));
5527c038 6246+ h_tmpdir = d_inode(h_orph);
1facf9fc 6247+ au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6248+
febd17d6 6249+ inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
4a4d8108 6250+ /* todo: au_h_open_pre()? */
86dc4139 6251+
c2b27bf2 6252+ pin_orig = cpg->pin;
86dc4139 6253+ au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
c2b27bf2
AM
6254+ AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6255+ cpg->pin = &wh_pin;
1facf9fc 6256+ }
6257+
53392da6 6258+ if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
5527c038 6259+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 6260+ err = au_cpup_wh(cpg, file);
1facf9fc 6261+ else {
6262+ struct au_cpup_wh_args args = {
6263+ .errp = &err,
c2b27bf2
AM
6264+ .cpg = cpg,
6265+ .file = file
1facf9fc 6266+ };
6267+ wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6268+ if (unlikely(wkq_err))
6269+ err = wkq_err;
6270+ }
6271+
6272+ if (h_orph) {
febd17d6 6273+ inode_unlock(h_tmpdir);
4a4d8108 6274+ /* todo: au_h_open_post()? */
1facf9fc 6275+ au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
1facf9fc 6276+ au_set_h_dptr(parent, bdst, h_parent);
c2b27bf2
AM
6277+ AuDebugOn(!pin_orig);
6278+ cpg->pin = pin_orig;
1facf9fc 6279+ }
6280+ iput(h_dir);
6281+ dput(parent);
6282+
6283+ return err;
6284+}
6285+
6286+/* ---------------------------------------------------------------------- */
6287+
6288+/*
6289+ * generic routine for both of copy-up and copy-down.
6290+ */
6291+/* cf. revalidate function in file.c */
6292+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6293+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6294+ struct au_pin *pin,
1facf9fc 6295+ struct dentry *h_parent, void *arg),
6296+ void *arg)
6297+{
6298+ int err;
6299+ struct au_pin pin;
5527c038 6300+ struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
1facf9fc 6301+
6302+ err = 0;
6303+ parent = dget_parent(dentry);
6304+ if (IS_ROOT(parent))
6305+ goto out;
6306+
6307+ au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6308+ au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6309+
6310+ /* do not use au_dpage */
6311+ real_parent = parent;
6312+ while (1) {
6313+ dput(parent);
6314+ parent = dget_parent(dentry);
6315+ h_parent = au_h_dptr(parent, bdst);
6316+ if (h_parent)
6317+ goto out; /* success */
6318+
6319+ /* find top dir which is necessary to cpup */
6320+ do {
6321+ d = parent;
6322+ dput(parent);
6323+ parent = dget_parent(d);
6324+ di_read_lock_parent3(parent, !AuLock_IR);
6325+ h_parent = au_h_dptr(parent, bdst);
6326+ di_read_unlock(parent, !AuLock_IR);
6327+ } while (!h_parent);
6328+
6329+ if (d != real_parent)
6330+ di_write_lock_child3(d);
6331+
6332+ /* somebody else might create while we were sleeping */
5527c038
JR
6333+ h_dentry = au_h_dptr(d, bdst);
6334+ if (!h_dentry || d_is_negative(h_dentry)) {
6335+ if (h_dentry)
5afbbe0d 6336+ au_update_dbtop(d);
1facf9fc 6337+
6338+ au_pin_set_dentry(&pin, d);
6339+ err = au_do_pin(&pin);
6340+ if (!err) {
86dc4139 6341+ err = cp(d, bdst, &pin, h_parent, arg);
1facf9fc 6342+ au_unpin(&pin);
6343+ }
6344+ }
6345+
6346+ if (d != real_parent)
6347+ di_write_unlock(d);
6348+ if (unlikely(err))
6349+ break;
6350+ }
6351+
4f0767ce 6352+out:
1facf9fc 6353+ dput(parent);
6354+ return err;
6355+}
6356+
6357+static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6358+ struct au_pin *pin,
2000de60 6359+ struct dentry *h_parent __maybe_unused,
1facf9fc 6360+ void *arg __maybe_unused)
6361+{
c2b27bf2
AM
6362+ struct au_cp_generic cpg = {
6363+ .dentry = dentry,
6364+ .bdst = bdst,
6365+ .bsrc = -1,
6366+ .len = 0,
6367+ .pin = pin,
6368+ .flags = AuCpup_DTIME
6369+ };
6370+ return au_sio_cpup_simple(&cpg);
1facf9fc 6371+}
6372+
6373+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6374+{
6375+ return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6376+}
6377+
6378+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6379+{
6380+ int err;
6381+ struct dentry *parent;
6382+ struct inode *dir;
6383+
6384+ parent = dget_parent(dentry);
5527c038 6385+ dir = d_inode(parent);
1facf9fc 6386+ err = 0;
6387+ if (au_h_iptr(dir, bdst))
6388+ goto out;
6389+
6390+ di_read_unlock(parent, AuLock_IR);
6391+ di_write_lock_parent(parent);
6392+ /* someone else might change our inode while we were sleeping */
6393+ if (!au_h_iptr(dir, bdst))
6394+ err = au_cpup_dirs(dentry, bdst);
6395+ di_downgrade_lock(parent, AuLock_IR);
6396+
4f0767ce 6397+out:
1facf9fc 6398+ dput(parent);
6399+ return err;
6400+}
7f207e10
AM
6401diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6402--- /usr/share/empty/fs/aufs/cpup.h 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
6403+++ linux/fs/aufs/cpup.h 2017-11-12 22:24:44.704244405 +0100
6404@@ -0,0 +1,99 @@
1facf9fc 6405+/*
a2654f78 6406+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 6407+ *
6408+ * This program, aufs is free software; you can redistribute it and/or modify
6409+ * it under the terms of the GNU General Public License as published by
6410+ * the Free Software Foundation; either version 2 of the License, or
6411+ * (at your option) any later version.
dece6358
AM
6412+ *
6413+ * This program is distributed in the hope that it will be useful,
6414+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6415+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6416+ * GNU General Public License for more details.
6417+ *
6418+ * You should have received a copy of the GNU General Public License
523b37e3 6419+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6420+ */
6421+
6422+/*
6423+ * copy-up/down functions
6424+ */
6425+
6426+#ifndef __AUFS_CPUP_H__
6427+#define __AUFS_CPUP_H__
6428+
6429+#ifdef __KERNEL__
6430+
dece6358 6431+#include <linux/path.h>
1facf9fc 6432+
dece6358
AM
6433+struct inode;
6434+struct file;
86dc4139 6435+struct au_pin;
dece6358 6436+
86dc4139 6437+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
1facf9fc 6438+void au_cpup_attr_timesizes(struct inode *inode);
6439+void au_cpup_attr_nlink(struct inode *inode, int force);
6440+void au_cpup_attr_changeable(struct inode *inode);
6441+void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6442+void au_cpup_attr_all(struct inode *inode, int force);
6443+
6444+/* ---------------------------------------------------------------------- */
6445+
c2b27bf2
AM
6446+struct au_cp_generic {
6447+ struct dentry *dentry;
6448+ aufs_bindex_t bdst, bsrc;
6449+ loff_t len;
6450+ struct au_pin *pin;
6451+ unsigned int flags;
6452+};
6453+
1facf9fc 6454+/* cpup flags */
392086de
AM
6455+#define AuCpup_DTIME 1 /* do dtime_store/revert */
6456+#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
6457+ for link(2) */
6458+#define AuCpup_RENAME (1 << 2) /* rename after cpup */
6459+#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
6460+ cpup */
6461+#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
6462+ existing entry */
6463+#define AuCpup_RWDST (1 << 5) /* force write target even if
6464+ the branch is marked as RO */
c2b27bf2 6465+
8b6a4947
AM
6466+#ifndef CONFIG_AUFS_BR_HFSPLUS
6467+#undef AuCpup_HOPEN
6468+#define AuCpup_HOPEN 0
6469+#endif
6470+
1facf9fc 6471+#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
7f207e10
AM
6472+#define au_fset_cpup(flags, name) \
6473+ do { (flags) |= AuCpup_##name; } while (0)
6474+#define au_fclr_cpup(flags, name) \
6475+ do { (flags) &= ~AuCpup_##name; } while (0)
1facf9fc 6476+
6477+int au_copy_file(struct file *dst, struct file *src, loff_t len);
c2b27bf2
AM
6478+int au_sio_cpup_simple(struct au_cp_generic *cpg);
6479+int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6480+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
1facf9fc 6481+
6482+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6483+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6484+ struct au_pin *pin,
1facf9fc 6485+ struct dentry *h_parent, void *arg),
6486+ void *arg);
6487+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6488+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6489+
6490+/* ---------------------------------------------------------------------- */
6491+
6492+/* keep timestamps when copyup */
6493+struct au_dtime {
6494+ struct dentry *dt_dentry;
6495+ struct path dt_h_path;
6496+ struct timespec dt_atime, dt_mtime;
6497+};
6498+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6499+ struct path *h_path);
6500+void au_dtime_revert(struct au_dtime *dt);
6501+
6502+#endif /* __KERNEL__ */
6503+#endif /* __AUFS_CPUP_H__ */
7f207e10
AM
6504diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6505--- /usr/share/empty/fs/aufs/dbgaufs.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
6506+++ linux/fs/aufs/dbgaufs.c 2017-11-12 22:24:44.704244405 +0100
6507@@ -0,0 +1,437 @@
1facf9fc 6508+/*
a2654f78 6509+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 6510+ *
6511+ * This program, aufs is free software; you can redistribute it and/or modify
6512+ * it under the terms of the GNU General Public License as published by
6513+ * the Free Software Foundation; either version 2 of the License, or
6514+ * (at your option) any later version.
dece6358
AM
6515+ *
6516+ * This program is distributed in the hope that it will be useful,
6517+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6518+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6519+ * GNU General Public License for more details.
6520+ *
6521+ * You should have received a copy of the GNU General Public License
523b37e3 6522+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6523+ */
6524+
6525+/*
6526+ * debugfs interface
6527+ */
6528+
6529+#include <linux/debugfs.h>
6530+#include "aufs.h"
6531+
6532+#ifndef CONFIG_SYSFS
6533+#error DEBUG_FS depends upon SYSFS
6534+#endif
6535+
6536+static struct dentry *dbgaufs;
6537+static const mode_t dbgaufs_mode = S_IRUSR | S_IRGRP | S_IROTH;
6538+
6539+/* 20 is max digits length of ulong 64 */
6540+struct dbgaufs_arg {
6541+ int n;
6542+ char a[20 * 4];
6543+};
6544+
6545+/*
6546+ * common function for all XINO files
6547+ */
6548+static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6549+ struct file *file)
6550+{
1c60b727 6551+ kfree(file->private_data);
1facf9fc 6552+ return 0;
6553+}
6554+
6555+static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt)
6556+{
6557+ int err;
6558+ struct kstat st;
6559+ struct dbgaufs_arg *p;
6560+
6561+ err = -ENOMEM;
6562+ p = kmalloc(sizeof(*p), GFP_NOFS);
6563+ if (unlikely(!p))
6564+ goto out;
6565+
6566+ err = 0;
6567+ p->n = 0;
6568+ file->private_data = p;
6569+ if (!xf)
6570+ goto out;
6571+
521ced18 6572+ err = vfsub_getattr(&xf->f_path, &st);
1facf9fc 6573+ if (!err) {
6574+ if (do_fcnt)
6575+ p->n = snprintf
521ced18 6576+ (p->a, sizeof(p->a), "%ld, %llux%u %lld\n",
1facf9fc 6577+ (long)file_count(xf), st.blocks, st.blksize,
6578+ (long long)st.size);
6579+ else
521ced18 6580+ p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
1facf9fc 6581+ st.blocks, st.blksize,
6582+ (long long)st.size);
6583+ AuDebugOn(p->n >= sizeof(p->a));
6584+ } else {
6585+ p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6586+ err = 0;
6587+ }
6588+
4f0767ce 6589+out:
1facf9fc 6590+ return err;
6591+
6592+}
6593+
6594+static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6595+ size_t count, loff_t *ppos)
6596+{
6597+ struct dbgaufs_arg *p;
6598+
6599+ p = file->private_data;
6600+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6601+}
6602+
6603+/* ---------------------------------------------------------------------- */
6604+
86dc4139
AM
6605+struct dbgaufs_plink_arg {
6606+ int n;
6607+ char a[];
6608+};
6609+
6610+static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6611+ struct file *file)
6612+{
1c60b727 6613+ free_page((unsigned long)file->private_data);
86dc4139
AM
6614+ return 0;
6615+}
6616+
6617+static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6618+{
6619+ int err, i, limit;
6620+ unsigned long n, sum;
6621+ struct dbgaufs_plink_arg *p;
6622+ struct au_sbinfo *sbinfo;
6623+ struct super_block *sb;
8b6a4947 6624+ struct hlist_bl_head *hbl;
86dc4139
AM
6625+
6626+ err = -ENOMEM;
6627+ p = (void *)get_zeroed_page(GFP_NOFS);
6628+ if (unlikely(!p))
6629+ goto out;
6630+
6631+ err = -EFBIG;
6632+ sbinfo = inode->i_private;
6633+ sb = sbinfo->si_sb;
6634+ si_noflush_read_lock(sb);
6635+ if (au_opt_test(au_mntflags(sb), PLINK)) {
6636+ limit = PAGE_SIZE - sizeof(p->n);
6637+
6638+ /* the number of buckets */
6639+ n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6640+ p->n += n;
6641+ limit -= n;
6642+
6643+ sum = 0;
8b6a4947
AM
6644+ for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6645+ i++, hbl++) {
6646+ n = au_hbl_count(hbl);
86dc4139
AM
6647+ sum += n;
6648+
6649+ n = snprintf(p->a + p->n, limit, "%lu ", n);
6650+ p->n += n;
6651+ limit -= n;
6652+ if (unlikely(limit <= 0))
6653+ goto out_free;
6654+ }
6655+ p->a[p->n - 1] = '\n';
6656+
6657+ /* the sum of plinks */
6658+ n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6659+ p->n += n;
6660+ limit -= n;
6661+ if (unlikely(limit <= 0))
6662+ goto out_free;
6663+ } else {
6664+#define str "1\n0\n0\n"
6665+ p->n = sizeof(str) - 1;
6666+ strcpy(p->a, str);
6667+#undef str
6668+ }
6669+ si_read_unlock(sb);
6670+
6671+ err = 0;
6672+ file->private_data = p;
6673+ goto out; /* success */
6674+
6675+out_free:
1c60b727 6676+ free_page((unsigned long)p);
86dc4139
AM
6677+out:
6678+ return err;
6679+}
6680+
6681+static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6682+ size_t count, loff_t *ppos)
6683+{
6684+ struct dbgaufs_plink_arg *p;
6685+
6686+ p = file->private_data;
6687+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6688+}
6689+
6690+static const struct file_operations dbgaufs_plink_fop = {
6691+ .owner = THIS_MODULE,
6692+ .open = dbgaufs_plink_open,
6693+ .release = dbgaufs_plink_release,
6694+ .read = dbgaufs_plink_read
6695+};
6696+
6697+/* ---------------------------------------------------------------------- */
6698+
1facf9fc 6699+static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6700+{
6701+ int err;
6702+ struct au_sbinfo *sbinfo;
6703+ struct super_block *sb;
6704+
6705+ sbinfo = inode->i_private;
6706+ sb = sbinfo->si_sb;
6707+ si_noflush_read_lock(sb);
6708+ err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0);
6709+ si_read_unlock(sb);
6710+ return err;
6711+}
6712+
6713+static const struct file_operations dbgaufs_xib_fop = {
4a4d8108 6714+ .owner = THIS_MODULE,
1facf9fc 6715+ .open = dbgaufs_xib_open,
6716+ .release = dbgaufs_xi_release,
6717+ .read = dbgaufs_xi_read
6718+};
6719+
6720+/* ---------------------------------------------------------------------- */
6721+
6722+#define DbgaufsXi_PREFIX "xi"
6723+
6724+static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6725+{
6726+ int err;
6727+ long l;
6728+ struct au_sbinfo *sbinfo;
6729+ struct super_block *sb;
6730+ struct file *xf;
6731+ struct qstr *name;
6732+
6733+ err = -ENOENT;
6734+ xf = NULL;
2000de60 6735+ name = &file->f_path.dentry->d_name;
1facf9fc 6736+ if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6737+ || memcmp(name->name, DbgaufsXi_PREFIX,
6738+ sizeof(DbgaufsXi_PREFIX) - 1)))
6739+ goto out;
9dbd164d 6740+ err = kstrtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
1facf9fc 6741+ if (unlikely(err))
6742+ goto out;
6743+
6744+ sbinfo = inode->i_private;
6745+ sb = sbinfo->si_sb;
6746+ si_noflush_read_lock(sb);
5afbbe0d 6747+ if (l <= au_sbbot(sb)) {
1facf9fc 6748+ xf = au_sbr(sb, (aufs_bindex_t)l)->br_xino.xi_file;
6749+ err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1);
6750+ } else
6751+ err = -ENOENT;
6752+ si_read_unlock(sb);
6753+
4f0767ce 6754+out:
1facf9fc 6755+ return err;
6756+}
6757+
6758+static const struct file_operations dbgaufs_xino_fop = {
4a4d8108 6759+ .owner = THIS_MODULE,
1facf9fc 6760+ .open = dbgaufs_xino_open,
6761+ .release = dbgaufs_xi_release,
6762+ .read = dbgaufs_xi_read
6763+};
6764+
6765+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6766+{
5afbbe0d 6767+ aufs_bindex_t bbot;
1facf9fc 6768+ struct au_branch *br;
6769+ struct au_xino_file *xi;
6770+
6771+ if (!au_sbi(sb)->si_dbgaufs)
6772+ return;
6773+
5afbbe0d
AM
6774+ bbot = au_sbbot(sb);
6775+ for (; bindex <= bbot; bindex++) {
1facf9fc 6776+ br = au_sbr(sb, bindex);
6777+ xi = &br->br_xino;
e2f27e51
AM
6778+ /* debugfs acquires the parent i_mutex */
6779+ lockdep_off();
c06a8ce3 6780+ debugfs_remove(xi->xi_dbgaufs);
e2f27e51 6781+ lockdep_on();
c06a8ce3 6782+ xi->xi_dbgaufs = NULL;
1facf9fc 6783+ }
6784+}
6785+
6786+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
6787+{
6788+ struct au_sbinfo *sbinfo;
6789+ struct dentry *parent;
6790+ struct au_branch *br;
6791+ struct au_xino_file *xi;
5afbbe0d 6792+ aufs_bindex_t bbot;
1facf9fc 6793+ char name[sizeof(DbgaufsXi_PREFIX) + 5]; /* "xi" bindex NULL */
6794+
6795+ sbinfo = au_sbi(sb);
6796+ parent = sbinfo->si_dbgaufs;
6797+ if (!parent)
6798+ return;
6799+
5afbbe0d
AM
6800+ bbot = au_sbbot(sb);
6801+ for (; bindex <= bbot; bindex++) {
1facf9fc 6802+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6803+ br = au_sbr(sb, bindex);
6804+ xi = &br->br_xino;
6805+ AuDebugOn(xi->xi_dbgaufs);
f0c0a007
AM
6806+ /* debugfs acquires the parent i_mutex */
6807+ lockdep_off();
1facf9fc 6808+ xi->xi_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6809+ sbinfo, &dbgaufs_xino_fop);
f0c0a007 6810+ lockdep_on();
1facf9fc 6811+ /* ignore an error */
6812+ if (unlikely(!xi->xi_dbgaufs))
6813+ AuWarn1("failed %s under debugfs\n", name);
6814+ }
6815+}
6816+
6817+/* ---------------------------------------------------------------------- */
6818+
6819+#ifdef CONFIG_AUFS_EXPORT
6820+static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6821+{
6822+ int err;
6823+ struct au_sbinfo *sbinfo;
6824+ struct super_block *sb;
6825+
6826+ sbinfo = inode->i_private;
6827+ sb = sbinfo->si_sb;
6828+ si_noflush_read_lock(sb);
6829+ err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0);
6830+ si_read_unlock(sb);
6831+ return err;
6832+}
6833+
6834+static const struct file_operations dbgaufs_xigen_fop = {
4a4d8108 6835+ .owner = THIS_MODULE,
1facf9fc 6836+ .open = dbgaufs_xigen_open,
6837+ .release = dbgaufs_xi_release,
6838+ .read = dbgaufs_xi_read
6839+};
6840+
6841+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6842+{
6843+ int err;
6844+
dece6358 6845+ /*
c1595e42 6846+ * This function is a dynamic '__init' function actually,
dece6358
AM
6847+ * so the tiny check for si_rwsem is unnecessary.
6848+ */
6849+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6850+
1facf9fc 6851+ err = -EIO;
6852+ sbinfo->si_dbgaufs_xigen = debugfs_create_file
6853+ ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6854+ &dbgaufs_xigen_fop);
6855+ if (sbinfo->si_dbgaufs_xigen)
6856+ err = 0;
6857+
6858+ return err;
6859+}
6860+#else
6861+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6862+{
6863+ return 0;
6864+}
6865+#endif /* CONFIG_AUFS_EXPORT */
6866+
6867+/* ---------------------------------------------------------------------- */
6868+
6869+void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6870+{
dece6358 6871+ /*
7e9cd9fe 6872+ * This function is a dynamic '__fin' function actually,
dece6358
AM
6873+ * so the tiny check for si_rwsem is unnecessary.
6874+ */
6875+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6876+
1facf9fc 6877+ debugfs_remove_recursive(sbinfo->si_dbgaufs);
6878+ sbinfo->si_dbgaufs = NULL;
6879+ kobject_put(&sbinfo->si_kobj);
6880+}
6881+
6882+int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6883+{
6884+ int err;
6885+ char name[SysaufsSiNameLen];
6886+
dece6358 6887+ /*
c1595e42 6888+ * This function is a dynamic '__init' function actually,
dece6358
AM
6889+ * so the tiny check for si_rwsem is unnecessary.
6890+ */
6891+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6892+
1facf9fc 6893+ err = -ENOENT;
6894+ if (!dbgaufs) {
6895+ AuErr1("/debug/aufs is uninitialized\n");
6896+ goto out;
6897+ }
6898+
6899+ err = -EIO;
6900+ sysaufs_name(sbinfo, name);
6901+ sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6902+ if (unlikely(!sbinfo->si_dbgaufs))
6903+ goto out;
6904+ kobject_get(&sbinfo->si_kobj);
6905+
6906+ sbinfo->si_dbgaufs_xib = debugfs_create_file
6907+ ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6908+ &dbgaufs_xib_fop);
6909+ if (unlikely(!sbinfo->si_dbgaufs_xib))
6910+ goto out_dir;
6911+
86dc4139
AM
6912+ sbinfo->si_dbgaufs_plink = debugfs_create_file
6913+ ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6914+ &dbgaufs_plink_fop);
6915+ if (unlikely(!sbinfo->si_dbgaufs_plink))
6916+ goto out_dir;
6917+
1facf9fc 6918+ err = dbgaufs_xigen_init(sbinfo);
6919+ if (!err)
6920+ goto out; /* success */
6921+
4f0767ce 6922+out_dir:
1facf9fc 6923+ dbgaufs_si_fin(sbinfo);
4f0767ce 6924+out:
1facf9fc 6925+ return err;
6926+}
6927+
6928+/* ---------------------------------------------------------------------- */
6929+
6930+void dbgaufs_fin(void)
6931+{
6932+ debugfs_remove(dbgaufs);
6933+}
6934+
6935+int __init dbgaufs_init(void)
6936+{
6937+ int err;
6938+
6939+ err = -EIO;
6940+ dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6941+ if (dbgaufs)
6942+ err = 0;
6943+ return err;
6944+}
7f207e10
AM
6945diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6946--- /usr/share/empty/fs/aufs/dbgaufs.h 1970-01-01 01:00:00.000000000 +0100
1c60b727 6947+++ linux/fs/aufs/dbgaufs.h 2017-07-29 12:14:25.899708630 +0200
523b37e3 6948@@ -0,0 +1,48 @@
1facf9fc 6949+/*
a2654f78 6950+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 6951+ *
6952+ * This program, aufs is free software; you can redistribute it and/or modify
6953+ * it under the terms of the GNU General Public License as published by
6954+ * the Free Software Foundation; either version 2 of the License, or
6955+ * (at your option) any later version.
dece6358
AM
6956+ *
6957+ * This program is distributed in the hope that it will be useful,
6958+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6959+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6960+ * GNU General Public License for more details.
6961+ *
6962+ * You should have received a copy of the GNU General Public License
523b37e3 6963+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6964+ */
6965+
6966+/*
6967+ * debugfs interface
6968+ */
6969+
6970+#ifndef __DBGAUFS_H__
6971+#define __DBGAUFS_H__
6972+
6973+#ifdef __KERNEL__
6974+
dece6358 6975+struct super_block;
1facf9fc 6976+struct au_sbinfo;
dece6358 6977+
1facf9fc 6978+#ifdef CONFIG_DEBUG_FS
6979+/* dbgaufs.c */
6980+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6981+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
6982+void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6983+int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6984+void dbgaufs_fin(void);
6985+int __init dbgaufs_init(void);
1facf9fc 6986+#else
4a4d8108
AM
6987+AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6988+AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
6989+AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6990+AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6991+AuStubVoid(dbgaufs_fin, void)
6992+AuStubInt0(__init dbgaufs_init, void)
1facf9fc 6993+#endif /* CONFIG_DEBUG_FS */
6994+
6995+#endif /* __KERNEL__ */
6996+#endif /* __DBGAUFS_H__ */
7f207e10
AM
6997diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6998--- /usr/share/empty/fs/aufs/dcsub.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947 6999+++ linux/fs/aufs/dcsub.c 2017-11-12 22:24:42.267510077 +0100
e2f27e51 7000@@ -0,0 +1,225 @@
1facf9fc 7001+/*
a2654f78 7002+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 7003+ *
7004+ * This program, aufs is free software; you can redistribute it and/or modify
7005+ * it under the terms of the GNU General Public License as published by
7006+ * the Free Software Foundation; either version 2 of the License, or
7007+ * (at your option) any later version.
dece6358
AM
7008+ *
7009+ * This program is distributed in the hope that it will be useful,
7010+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7011+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7012+ * GNU General Public License for more details.
7013+ *
7014+ * You should have received a copy of the GNU General Public License
523b37e3 7015+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7016+ */
7017+
7018+/*
7019+ * sub-routines for dentry cache
7020+ */
7021+
7022+#include "aufs.h"
7023+
7024+static void au_dpage_free(struct au_dpage *dpage)
7025+{
7026+ int i;
7027+ struct dentry **p;
7028+
7029+ p = dpage->dentries;
7030+ for (i = 0; i < dpage->ndentry; i++)
7031+ dput(*p++);
1c60b727 7032+ free_page((unsigned long)dpage->dentries);
1facf9fc 7033+}
7034+
7035+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
7036+{
7037+ int err;
7038+ void *p;
7039+
7040+ err = -ENOMEM;
7041+ dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
7042+ if (unlikely(!dpages->dpages))
7043+ goto out;
7044+
7045+ p = (void *)__get_free_page(gfp);
7046+ if (unlikely(!p))
7047+ goto out_dpages;
7048+
7049+ dpages->dpages[0].ndentry = 0;
7050+ dpages->dpages[0].dentries = p;
7051+ dpages->ndpage = 1;
7052+ return 0; /* success */
7053+
4f0767ce 7054+out_dpages:
1c60b727 7055+ kfree(dpages->dpages);
4f0767ce 7056+out:
1facf9fc 7057+ return err;
7058+}
7059+
7060+void au_dpages_free(struct au_dcsub_pages *dpages)
7061+{
7062+ int i;
7063+ struct au_dpage *p;
7064+
7065+ p = dpages->dpages;
7066+ for (i = 0; i < dpages->ndpage; i++)
7067+ au_dpage_free(p++);
1c60b727 7068+ kfree(dpages->dpages);
1facf9fc 7069+}
7070+
7071+static int au_dpages_append(struct au_dcsub_pages *dpages,
7072+ struct dentry *dentry, gfp_t gfp)
7073+{
7074+ int err, sz;
7075+ struct au_dpage *dpage;
7076+ void *p;
7077+
7078+ dpage = dpages->dpages + dpages->ndpage - 1;
7079+ sz = PAGE_SIZE / sizeof(dentry);
7080+ if (unlikely(dpage->ndentry >= sz)) {
7081+ AuLabel(new dpage);
7082+ err = -ENOMEM;
7083+ sz = dpages->ndpage * sizeof(*dpages->dpages);
7084+ p = au_kzrealloc(dpages->dpages, sz,
e2f27e51
AM
7085+ sz + sizeof(*dpages->dpages), gfp,
7086+ /*may_shrink*/0);
1facf9fc 7087+ if (unlikely(!p))
7088+ goto out;
7089+
7090+ dpages->dpages = p;
7091+ dpage = dpages->dpages + dpages->ndpage;
7092+ p = (void *)__get_free_page(gfp);
7093+ if (unlikely(!p))
7094+ goto out;
7095+
7096+ dpage->ndentry = 0;
7097+ dpage->dentries = p;
7098+ dpages->ndpage++;
7099+ }
7100+
c1595e42 7101+ AuDebugOn(au_dcount(dentry) <= 0);
027c5e7a 7102+ dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
1facf9fc 7103+ return 0; /* success */
7104+
4f0767ce 7105+out:
1facf9fc 7106+ return err;
7107+}
7108+
c1595e42
JR
7109+/* todo: BAD approach */
7110+/* copied from linux/fs/dcache.c */
7111+enum d_walk_ret {
7112+ D_WALK_CONTINUE,
7113+ D_WALK_QUIT,
7114+ D_WALK_NORETRY,
7115+ D_WALK_SKIP,
7116+};
7117+
7118+extern void d_walk(struct dentry *parent, void *data,
7119+ enum d_walk_ret (*enter)(void *, struct dentry *),
7120+ void (*finish)(void *));
7121+
7122+struct ac_dpages_arg {
1facf9fc 7123+ int err;
c1595e42
JR
7124+ struct au_dcsub_pages *dpages;
7125+ struct super_block *sb;
7126+ au_dpages_test test;
7127+ void *arg;
7128+};
1facf9fc 7129+
c1595e42
JR
7130+static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7131+{
7132+ enum d_walk_ret ret;
7133+ struct ac_dpages_arg *arg = _arg;
1facf9fc 7134+
c1595e42
JR
7135+ ret = D_WALK_CONTINUE;
7136+ if (dentry->d_sb == arg->sb
7137+ && !IS_ROOT(dentry)
7138+ && au_dcount(dentry) > 0
7139+ && au_di(dentry)
7140+ && (!arg->test || arg->test(dentry, arg->arg))) {
7141+ arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7142+ if (unlikely(arg->err))
7143+ ret = D_WALK_QUIT;
1facf9fc 7144+ }
7145+
c1595e42
JR
7146+ return ret;
7147+}
027c5e7a 7148+
c1595e42
JR
7149+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7150+ au_dpages_test test, void *arg)
7151+{
7152+ struct ac_dpages_arg args = {
7153+ .err = 0,
7154+ .dpages = dpages,
7155+ .sb = root->d_sb,
7156+ .test = test,
7157+ .arg = arg
7158+ };
027c5e7a 7159+
c1595e42
JR
7160+ d_walk(root, &args, au_call_dpages_append, NULL);
7161+
7162+ return args.err;
1facf9fc 7163+}
7164+
7165+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7166+ int do_include, au_dpages_test test, void *arg)
7167+{
7168+ int err;
7169+
7170+ err = 0;
027c5e7a
AM
7171+ write_seqlock(&rename_lock);
7172+ spin_lock(&dentry->d_lock);
7173+ if (do_include
c1595e42 7174+ && au_dcount(dentry) > 0
027c5e7a 7175+ && (!test || test(dentry, arg)))
1facf9fc 7176+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
7177+ spin_unlock(&dentry->d_lock);
7178+ if (unlikely(err))
7179+ goto out;
7180+
7181+ /*
523b37e3 7182+ * RCU for vfsmount is unnecessary since this is a traverse in a single
027c5e7a
AM
7183+ * mount
7184+ */
1facf9fc 7185+ while (!IS_ROOT(dentry)) {
027c5e7a
AM
7186+ dentry = dentry->d_parent; /* rename_lock is locked */
7187+ spin_lock(&dentry->d_lock);
c1595e42 7188+ if (au_dcount(dentry) > 0
027c5e7a 7189+ && (!test || test(dentry, arg)))
1facf9fc 7190+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
7191+ spin_unlock(&dentry->d_lock);
7192+ if (unlikely(err))
7193+ break;
1facf9fc 7194+ }
7195+
4f0767ce 7196+out:
027c5e7a 7197+ write_sequnlock(&rename_lock);
1facf9fc 7198+ return err;
7199+}
7200+
027c5e7a
AM
7201+static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7202+{
7203+ return au_di(dentry) && dentry->d_sb == arg;
7204+}
7205+
7206+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7207+ struct dentry *dentry, int do_include)
7208+{
7209+ return au_dcsub_pages_rev(dpages, dentry, do_include,
7210+ au_dcsub_dpages_aufs, dentry->d_sb);
7211+}
7212+
4a4d8108 7213+int au_test_subdir(struct dentry *d1, struct dentry *d2)
1facf9fc 7214+{
4a4d8108
AM
7215+ struct path path[2] = {
7216+ {
7217+ .dentry = d1
7218+ },
7219+ {
7220+ .dentry = d2
7221+ }
7222+ };
1facf9fc 7223+
4a4d8108 7224+ return path_is_under(path + 0, path + 1);
1facf9fc 7225+}
7f207e10
AM
7226diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7227--- /usr/share/empty/fs/aufs/dcsub.h 1970-01-01 01:00:00.000000000 +0100
1c60b727 7228+++ linux/fs/aufs/dcsub.h 2017-07-29 12:14:25.899708630 +0200
5527c038 7229@@ -0,0 +1,136 @@
1facf9fc 7230+/*
a2654f78 7231+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 7232+ *
7233+ * This program, aufs is free software; you can redistribute it and/or modify
7234+ * it under the terms of the GNU General Public License as published by
7235+ * the Free Software Foundation; either version 2 of the License, or
7236+ * (at your option) any later version.
dece6358
AM
7237+ *
7238+ * This program is distributed in the hope that it will be useful,
7239+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7240+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7241+ * GNU General Public License for more details.
7242+ *
7243+ * You should have received a copy of the GNU General Public License
523b37e3 7244+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7245+ */
7246+
7247+/*
7248+ * sub-routines for dentry cache
7249+ */
7250+
7251+#ifndef __AUFS_DCSUB_H__
7252+#define __AUFS_DCSUB_H__
7253+
7254+#ifdef __KERNEL__
7255+
7f207e10 7256+#include <linux/dcache.h>
027c5e7a 7257+#include <linux/fs.h>
dece6358 7258+
1facf9fc 7259+struct au_dpage {
7260+ int ndentry;
7261+ struct dentry **dentries;
7262+};
7263+
7264+struct au_dcsub_pages {
7265+ int ndpage;
7266+ struct au_dpage *dpages;
7267+};
7268+
7269+/* ---------------------------------------------------------------------- */
7270+
7f207e10 7271+/* dcsub.c */
1facf9fc 7272+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7273+void au_dpages_free(struct au_dcsub_pages *dpages);
7274+typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7275+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7276+ au_dpages_test test, void *arg);
7277+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7278+ int do_include, au_dpages_test test, void *arg);
027c5e7a
AM
7279+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7280+ struct dentry *dentry, int do_include);
4a4d8108 7281+int au_test_subdir(struct dentry *d1, struct dentry *d2);
1facf9fc 7282+
7f207e10
AM
7283+/* ---------------------------------------------------------------------- */
7284+
523b37e3
AM
7285+/*
7286+ * todo: in linux-3.13, several similar (but faster) helpers are added to
7287+ * include/linux/dcache.h. Try them (in the future).
7288+ */
7289+
027c5e7a
AM
7290+static inline int au_d_hashed_positive(struct dentry *d)
7291+{
7292+ int err;
5527c038 7293+ struct inode *inode = d_inode(d);
076b876e 7294+
027c5e7a 7295+ err = 0;
5527c038
JR
7296+ if (unlikely(d_unhashed(d)
7297+ || d_is_negative(d)
7298+ || !inode->i_nlink))
027c5e7a
AM
7299+ err = -ENOENT;
7300+ return err;
7301+}
7302+
38d290e6
JR
7303+static inline int au_d_linkable(struct dentry *d)
7304+{
7305+ int err;
5527c038 7306+ struct inode *inode = d_inode(d);
076b876e 7307+
38d290e6
JR
7308+ err = au_d_hashed_positive(d);
7309+ if (err
5527c038 7310+ && d_is_positive(d)
38d290e6
JR
7311+ && (inode->i_state & I_LINKABLE))
7312+ err = 0;
7313+ return err;
7314+}
7315+
027c5e7a
AM
7316+static inline int au_d_alive(struct dentry *d)
7317+{
7318+ int err;
7319+ struct inode *inode;
076b876e 7320+
027c5e7a
AM
7321+ err = 0;
7322+ if (!IS_ROOT(d))
7323+ err = au_d_hashed_positive(d);
7324+ else {
5527c038
JR
7325+ inode = d_inode(d);
7326+ if (unlikely(d_unlinked(d)
7327+ || d_is_negative(d)
7328+ || !inode->i_nlink))
027c5e7a
AM
7329+ err = -ENOENT;
7330+ }
7331+ return err;
7332+}
7333+
7334+static inline int au_alive_dir(struct dentry *d)
7f207e10 7335+{
027c5e7a 7336+ int err;
076b876e 7337+
027c5e7a 7338+ err = au_d_alive(d);
5527c038 7339+ if (unlikely(err || IS_DEADDIR(d_inode(d))))
027c5e7a
AM
7340+ err = -ENOENT;
7341+ return err;
7f207e10
AM
7342+}
7343+
38d290e6
JR
7344+static inline int au_qstreq(struct qstr *a, struct qstr *b)
7345+{
7346+ return a->len == b->len
7347+ && !memcmp(a->name, b->name, a->len);
7348+}
7349+
7e9cd9fe
AM
7350+/*
7351+ * by the commit
7352+ * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7353+ * taking d_lock
7354+ * the type of d_lockref.count became int, but the inlined function d_count()
7355+ * still returns unsigned int.
7356+ * I don't know why. Maybe it is for every d_count() users?
7357+ * Anyway au_dcount() lives on.
7358+ */
c1595e42
JR
7359+static inline int au_dcount(struct dentry *d)
7360+{
7361+ return (int)d_count(d);
7362+}
7363+
1facf9fc 7364+#endif /* __KERNEL__ */
7365+#endif /* __AUFS_DCSUB_H__ */
7f207e10
AM
7366diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7367--- /usr/share/empty/fs/aufs/debug.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947 7368+++ linux/fs/aufs/debug.c 2017-11-12 22:24:42.267510077 +0100
f0c0a007 7369@@ -0,0 +1,440 @@
1facf9fc 7370+/*
a2654f78 7371+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 7372+ *
7373+ * This program, aufs is free software; you can redistribute it and/or modify
7374+ * it under the terms of the GNU General Public License as published by
7375+ * the Free Software Foundation; either version 2 of the License, or
7376+ * (at your option) any later version.
dece6358
AM
7377+ *
7378+ * This program is distributed in the hope that it will be useful,
7379+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7380+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7381+ * GNU General Public License for more details.
7382+ *
7383+ * You should have received a copy of the GNU General Public License
523b37e3 7384+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7385+ */
7386+
7387+/*
7388+ * debug print functions
7389+ */
7390+
7391+#include "aufs.h"
7392+
392086de
AM
7393+/* Returns 0, or -errno. arg is in kp->arg. */
7394+static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7395+{
7396+ int err, n;
7397+
7398+ err = kstrtoint(val, 0, &n);
7399+ if (!err) {
7400+ if (n > 0)
7401+ au_debug_on();
7402+ else
7403+ au_debug_off();
7404+ }
7405+ return err;
7406+}
7407+
7408+/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
7409+static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7410+{
7411+ atomic_t *a;
7412+
7413+ a = kp->arg;
7414+ return sprintf(buffer, "%d", atomic_read(a));
7415+}
7416+
7417+static struct kernel_param_ops param_ops_atomic_t = {
7418+ .set = param_atomic_t_set,
7419+ .get = param_atomic_t_get
7420+ /* void (*free)(void *arg) */
7421+};
7422+
7423+atomic_t aufs_debug = ATOMIC_INIT(0);
1facf9fc 7424+MODULE_PARM_DESC(debug, "debug print");
392086de 7425+module_param_named(debug, aufs_debug, atomic_t, S_IRUGO | S_IWUSR | S_IWGRP);
1facf9fc 7426+
c1595e42 7427+DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
1facf9fc 7428+char *au_plevel = KERN_DEBUG;
e49829fe
JR
7429+#define dpri(fmt, ...) do { \
7430+ if ((au_plevel \
7431+ && strcmp(au_plevel, KERN_DEBUG)) \
7432+ || au_debug_test()) \
7433+ printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
1facf9fc 7434+} while (0)
7435+
7436+/* ---------------------------------------------------------------------- */
7437+
7438+void au_dpri_whlist(struct au_nhash *whlist)
7439+{
7440+ unsigned long ul, n;
7441+ struct hlist_head *head;
c06a8ce3 7442+ struct au_vdir_wh *pos;
1facf9fc 7443+
7444+ n = whlist->nh_num;
7445+ head = whlist->nh_head;
7446+ for (ul = 0; ul < n; ul++) {
c06a8ce3 7447+ hlist_for_each_entry(pos, head, wh_hash)
1facf9fc 7448+ dpri("b%d, %.*s, %d\n",
c06a8ce3
AM
7449+ pos->wh_bindex,
7450+ pos->wh_str.len, pos->wh_str.name,
7451+ pos->wh_str.len);
1facf9fc 7452+ head++;
7453+ }
7454+}
7455+
7456+void au_dpri_vdir(struct au_vdir *vdir)
7457+{
7458+ unsigned long ul;
7459+ union au_vdir_deblk_p p;
7460+ unsigned char *o;
7461+
7462+ if (!vdir || IS_ERR(vdir)) {
7463+ dpri("err %ld\n", PTR_ERR(vdir));
7464+ return;
7465+ }
7466+
7467+ dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %lu\n",
7468+ vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7469+ vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7470+ for (ul = 0; ul < vdir->vd_nblk; ul++) {
7471+ p.deblk = vdir->vd_deblk[ul];
7472+ o = p.deblk;
7473+ dpri("[%lu]: %p\n", ul, o);
7474+ }
7475+}
7476+
53392da6 7477+static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
1facf9fc 7478+ struct dentry *wh)
7479+{
7480+ char *n = NULL;
7481+ int l = 0;
7482+
7483+ if (!inode || IS_ERR(inode)) {
7484+ dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7485+ return -1;
7486+ }
7487+
c2b27bf2 7488+ /* the type of i_blocks depends upon CONFIG_LBDAF */
1facf9fc 7489+ BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7490+ && sizeof(inode->i_blocks) != sizeof(u64));
7491+ if (wh) {
7492+ n = (void *)wh->d_name.name;
7493+ l = wh->d_name.len;
7494+ }
7495+
53392da6
AM
7496+ dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7497+ " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7498+ bindex, inode,
1facf9fc 7499+ inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7500+ atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7501+ i_size_read(inode), (unsigned long long)inode->i_blocks,
53392da6 7502+ hn, (long long)timespec_to_ns(&inode->i_ctime) & 0x0ffff,
1facf9fc 7503+ inode->i_mapping ? inode->i_mapping->nrpages : 0,
b752ccd1
AM
7504+ inode->i_state, inode->i_flags, inode->i_version,
7505+ inode->i_generation,
1facf9fc 7506+ l ? ", wh " : "", l, n);
7507+ return 0;
7508+}
7509+
7510+void au_dpri_inode(struct inode *inode)
7511+{
7512+ struct au_iinfo *iinfo;
5afbbe0d 7513+ struct au_hinode *hi;
1facf9fc 7514+ aufs_bindex_t bindex;
53392da6 7515+ int err, hn;
1facf9fc 7516+
53392da6 7517+ err = do_pri_inode(-1, inode, -1, NULL);
5afbbe0d 7518+ if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
1facf9fc 7519+ return;
7520+
7521+ iinfo = au_ii(inode);
5afbbe0d
AM
7522+ dpri("i-1: btop %d, bbot %d, gen %d\n",
7523+ iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7524+ if (iinfo->ii_btop < 0)
1facf9fc 7525+ return;
53392da6 7526+ hn = 0;
5afbbe0d
AM
7527+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7528+ hi = au_hinode(iinfo, bindex);
7529+ hn = !!au_hn(hi);
7530+ do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
53392da6 7531+ }
1facf9fc 7532+}
7533+
2cbb1c4b
JR
7534+void au_dpri_dalias(struct inode *inode)
7535+{
7536+ struct dentry *d;
7537+
7538+ spin_lock(&inode->i_lock);
c1595e42 7539+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
2cbb1c4b
JR
7540+ au_dpri_dentry(d);
7541+ spin_unlock(&inode->i_lock);
7542+}
7543+
1facf9fc 7544+static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7545+{
7546+ struct dentry *wh = NULL;
53392da6 7547+ int hn;
5afbbe0d 7548+ struct inode *inode;
076b876e 7549+ struct au_iinfo *iinfo;
5afbbe0d 7550+ struct au_hinode *hi;
1facf9fc 7551+
7552+ if (!dentry || IS_ERR(dentry)) {
7553+ dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7554+ return -1;
7555+ }
7556+ /* do not call dget_parent() here */
027c5e7a 7557+ /* note: access d_xxx without d_lock */
523b37e3
AM
7558+ dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7559+ bindex, dentry, dentry,
1facf9fc 7560+ dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
c1595e42 7561+ au_dcount(dentry), dentry->d_flags,
523b37e3 7562+ d_unhashed(dentry) ? "un" : "");
53392da6 7563+ hn = -1;
5afbbe0d
AM
7564+ inode = NULL;
7565+ if (d_is_positive(dentry))
7566+ inode = d_inode(dentry);
7567+ if (inode
7568+ && au_test_aufs(dentry->d_sb)
7569+ && bindex >= 0
7570+ && !au_is_bad_inode(inode)) {
7571+ iinfo = au_ii(inode);
7572+ hi = au_hinode(iinfo, bindex);
7573+ hn = !!au_hn(hi);
7574+ wh = hi->hi_whdentry;
7575+ }
7576+ do_pri_inode(bindex, inode, hn, wh);
1facf9fc 7577+ return 0;
7578+}
7579+
7580+void au_dpri_dentry(struct dentry *dentry)
7581+{
7582+ struct au_dinfo *dinfo;
7583+ aufs_bindex_t bindex;
7584+ int err;
7585+
7586+ err = do_pri_dentry(-1, dentry);
7587+ if (err || !au_test_aufs(dentry->d_sb))
7588+ return;
7589+
7590+ dinfo = au_di(dentry);
7591+ if (!dinfo)
7592+ return;
5afbbe0d
AM
7593+ dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7594+ dinfo->di_btop, dinfo->di_bbot,
38d290e6
JR
7595+ dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7596+ dinfo->di_tmpfile);
5afbbe0d 7597+ if (dinfo->di_btop < 0)
1facf9fc 7598+ return;
5afbbe0d
AM
7599+ for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7600+ do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
1facf9fc 7601+}
7602+
7603+static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7604+{
7605+ char a[32];
7606+
7607+ if (!file || IS_ERR(file)) {
7608+ dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7609+ return -1;
7610+ }
7611+ a[0] = 0;
7612+ if (bindex < 0
b912730e 7613+ && !IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7614+ && au_test_aufs(file->f_path.dentry->d_sb)
1facf9fc 7615+ && au_fi(file))
e49829fe 7616+ snprintf(a, sizeof(a), ", gen %d, mmapped %d",
2cbb1c4b 7617+ au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
b752ccd1 7618+ dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
1facf9fc 7619+ bindex, file->f_mode, file->f_flags, (long)file_count(file),
b752ccd1 7620+ file->f_version, file->f_pos, a);
b912730e 7621+ if (!IS_ERR_OR_NULL(file->f_path.dentry))
2000de60 7622+ do_pri_dentry(bindex, file->f_path.dentry);
1facf9fc 7623+ return 0;
7624+}
7625+
7626+void au_dpri_file(struct file *file)
7627+{
7628+ struct au_finfo *finfo;
4a4d8108
AM
7629+ struct au_fidir *fidir;
7630+ struct au_hfile *hfile;
1facf9fc 7631+ aufs_bindex_t bindex;
7632+ int err;
7633+
7634+ err = do_pri_file(-1, file);
2000de60 7635+ if (err
b912730e 7636+ || IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7637+ || !au_test_aufs(file->f_path.dentry->d_sb))
1facf9fc 7638+ return;
7639+
7640+ finfo = au_fi(file);
7641+ if (!finfo)
7642+ return;
4a4d8108 7643+ if (finfo->fi_btop < 0)
1facf9fc 7644+ return;
4a4d8108
AM
7645+ fidir = finfo->fi_hdir;
7646+ if (!fidir)
7647+ do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7648+ else
e49829fe
JR
7649+ for (bindex = finfo->fi_btop;
7650+ bindex >= 0 && bindex <= fidir->fd_bbot;
4a4d8108
AM
7651+ bindex++) {
7652+ hfile = fidir->fd_hfile + bindex;
7653+ do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7654+ }
1facf9fc 7655+}
7656+
7657+static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7658+{
7659+ struct vfsmount *mnt;
7660+ struct super_block *sb;
7661+
7662+ if (!br || IS_ERR(br))
7663+ goto out;
86dc4139 7664+ mnt = au_br_mnt(br);
1facf9fc 7665+ if (!mnt || IS_ERR(mnt))
7666+ goto out;
7667+ sb = mnt->mnt_sb;
7668+ if (!sb || IS_ERR(sb))
7669+ goto out;
7670+
5afbbe0d 7671+ dpri("s%d: {perm 0x%x, id %d, cnt %lld, wbr %p}, "
b752ccd1 7672+ "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
1facf9fc 7673+ "xino %d\n",
5afbbe0d 7674+ bindex, br->br_perm, br->br_id, au_br_count(br),
1e00d052 7675+ br->br_wbr, au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
b752ccd1 7676+ sb->s_flags, sb->s_count,
1facf9fc 7677+ atomic_read(&sb->s_active), !!br->br_xino.xi_file);
7678+ return 0;
7679+
4f0767ce 7680+out:
1facf9fc 7681+ dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7682+ return -1;
7683+}
7684+
7685+void au_dpri_sb(struct super_block *sb)
7686+{
7687+ struct au_sbinfo *sbinfo;
7688+ aufs_bindex_t bindex;
7689+ int err;
7690+ /* to reuduce stack size */
7691+ struct {
7692+ struct vfsmount mnt;
7693+ struct au_branch fake;
7694+ } *a;
7695+
7696+ /* this function can be called from magic sysrq */
7697+ a = kzalloc(sizeof(*a), GFP_ATOMIC);
7698+ if (unlikely(!a)) {
7699+ dpri("no memory\n");
7700+ return;
7701+ }
7702+
7703+ a->mnt.mnt_sb = sb;
86dc4139 7704+ a->fake.br_path.mnt = &a->mnt;
5afbbe0d 7705+ au_br_count_init(&a->fake);
1facf9fc 7706+ err = do_pri_br(-1, &a->fake);
5afbbe0d 7707+ au_br_count_fin(&a->fake);
1c60b727 7708+ kfree(a);
1facf9fc 7709+ dpri("dev 0x%x\n", sb->s_dev);
7710+ if (err || !au_test_aufs(sb))
7711+ return;
7712+
7713+ sbinfo = au_sbi(sb);
7714+ if (!sbinfo)
7715+ return;
f0c0a007
AM
7716+ dpri("nw %d, gen %u, kobj %d\n",
7717+ atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
521ced18 7718+ kref_read(&sbinfo->si_kobj.kref));
5afbbe0d 7719+ for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
1facf9fc 7720+ do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7721+}
7722+
7723+/* ---------------------------------------------------------------------- */
7724+
027c5e7a
AM
7725+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7726+{
5527c038 7727+ struct inode *h_inode, *inode = d_inode(dentry);
027c5e7a 7728+ struct dentry *h_dentry;
5afbbe0d 7729+ aufs_bindex_t bindex, bbot, bi;
027c5e7a
AM
7730+
7731+ if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7732+ return;
7733+
5afbbe0d
AM
7734+ bbot = au_dbbot(dentry);
7735+ bi = au_ibbot(inode);
7736+ if (bi < bbot)
7737+ bbot = bi;
7738+ bindex = au_dbtop(dentry);
7739+ bi = au_ibtop(inode);
027c5e7a
AM
7740+ if (bi > bindex)
7741+ bindex = bi;
7742+
5afbbe0d 7743+ for (; bindex <= bbot; bindex++) {
027c5e7a
AM
7744+ h_dentry = au_h_dptr(dentry, bindex);
7745+ if (!h_dentry)
7746+ continue;
7747+ h_inode = au_h_iptr(inode, bindex);
5527c038 7748+ if (unlikely(h_inode != d_inode(h_dentry))) {
392086de 7749+ au_debug_on();
027c5e7a
AM
7750+ AuDbg("b%d, %s:%d\n", bindex, func, line);
7751+ AuDbgDentry(dentry);
7752+ AuDbgInode(inode);
392086de 7753+ au_debug_off();
027c5e7a
AM
7754+ BUG();
7755+ }
7756+ }
7757+}
7758+
1facf9fc 7759+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7760+{
7761+ int err, i, j;
7762+ struct au_dcsub_pages dpages;
7763+ struct au_dpage *dpage;
7764+ struct dentry **dentries;
7765+
7766+ err = au_dpages_init(&dpages, GFP_NOFS);
7767+ AuDebugOn(err);
027c5e7a 7768+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
1facf9fc 7769+ AuDebugOn(err);
7770+ for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7771+ dpage = dpages.dpages + i;
7772+ dentries = dpage->dentries;
7773+ for (j = dpage->ndentry - 1; !err && j >= 0; j--)
027c5e7a 7774+ AuDebugOn(au_digen_test(dentries[j], sigen));
1facf9fc 7775+ }
7776+ au_dpages_free(&dpages);
7777+}
7778+
1facf9fc 7779+void au_dbg_verify_kthread(void)
7780+{
53392da6 7781+ if (au_wkq_test()) {
1facf9fc 7782+ au_dbg_blocked();
1e00d052
AM
7783+ /*
7784+ * It may be recursive, but udba=notify between two aufs mounts,
7785+ * where a single ro branch is shared, is not a problem.
7786+ */
7787+ /* WARN_ON(1); */
1facf9fc 7788+ }
7789+}
7790+
7791+/* ---------------------------------------------------------------------- */
7792+
1facf9fc 7793+int __init au_debug_init(void)
7794+{
7795+ aufs_bindex_t bindex;
7796+ struct au_vdir_destr destr;
7797+
7798+ bindex = -1;
7799+ AuDebugOn(bindex >= 0);
7800+
7801+ destr.len = -1;
7802+ AuDebugOn(destr.len < NAME_MAX);
7803+
7804+#ifdef CONFIG_4KSTACKS
0c3ec466 7805+ pr_warn("CONFIG_4KSTACKS is defined.\n");
1facf9fc 7806+#endif
7807+
1facf9fc 7808+ return 0;
7809+}
7f207e10
AM
7810diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7811--- /usr/share/empty/fs/aufs/debug.h 1970-01-01 01:00:00.000000000 +0100
1c60b727 7812+++ linux/fs/aufs/debug.h 2017-07-29 12:14:25.899708630 +0200
5527c038 7813@@ -0,0 +1,225 @@
1facf9fc 7814+/*
a2654f78 7815+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 7816+ *
7817+ * This program, aufs is free software; you can redistribute it and/or modify
7818+ * it under the terms of the GNU General Public License as published by
7819+ * the Free Software Foundation; either version 2 of the License, or
7820+ * (at your option) any later version.
dece6358
AM
7821+ *
7822+ * This program is distributed in the hope that it will be useful,
7823+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7824+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7825+ * GNU General Public License for more details.
7826+ *
7827+ * You should have received a copy of the GNU General Public License
523b37e3 7828+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7829+ */
7830+
7831+/*
7832+ * debug print functions
7833+ */
7834+
7835+#ifndef __AUFS_DEBUG_H__
7836+#define __AUFS_DEBUG_H__
7837+
7838+#ifdef __KERNEL__
7839+
392086de 7840+#include <linux/atomic.h>
4a4d8108
AM
7841+#include <linux/module.h>
7842+#include <linux/kallsyms.h>
1facf9fc 7843+#include <linux/sysrq.h>
4a4d8108 7844+
1facf9fc 7845+#ifdef CONFIG_AUFS_DEBUG
7846+#define AuDebugOn(a) BUG_ON(a)
7847+
7848+/* module parameter */
392086de
AM
7849+extern atomic_t aufs_debug;
7850+static inline void au_debug_on(void)
1facf9fc 7851+{
392086de
AM
7852+ atomic_inc(&aufs_debug);
7853+}
7854+static inline void au_debug_off(void)
7855+{
7856+ atomic_dec_if_positive(&aufs_debug);
1facf9fc 7857+}
7858+
7859+static inline int au_debug_test(void)
7860+{
392086de 7861+ return atomic_read(&aufs_debug) > 0;
1facf9fc 7862+}
7863+#else
7864+#define AuDebugOn(a) do {} while (0)
392086de
AM
7865+AuStubVoid(au_debug_on, void)
7866+AuStubVoid(au_debug_off, void)
4a4d8108 7867+AuStubInt0(au_debug_test, void)
1facf9fc 7868+#endif /* CONFIG_AUFS_DEBUG */
7869+
392086de
AM
7870+#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7871+
1facf9fc 7872+/* ---------------------------------------------------------------------- */
7873+
7874+/* debug print */
7875+
4a4d8108 7876+#define AuDbg(fmt, ...) do { \
1facf9fc 7877+ if (au_debug_test()) \
4a4d8108 7878+ pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
1facf9fc 7879+} while (0)
4a4d8108
AM
7880+#define AuLabel(l) AuDbg(#l "\n")
7881+#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7882+#define AuWarn1(fmt, ...) do { \
1facf9fc 7883+ static unsigned char _c; \
7884+ if (!_c++) \
0c3ec466 7885+ pr_warn(fmt, ##__VA_ARGS__); \
1facf9fc 7886+} while (0)
7887+
4a4d8108 7888+#define AuErr1(fmt, ...) do { \
1facf9fc 7889+ static unsigned char _c; \
7890+ if (!_c++) \
4a4d8108 7891+ pr_err(fmt, ##__VA_ARGS__); \
1facf9fc 7892+} while (0)
7893+
4a4d8108 7894+#define AuIOErr1(fmt, ...) do { \
1facf9fc 7895+ static unsigned char _c; \
7896+ if (!_c++) \
4a4d8108 7897+ AuIOErr(fmt, ##__VA_ARGS__); \
1facf9fc 7898+} while (0)
7899+
7900+#define AuUnsupportMsg "This operation is not supported." \
7901+ " Please report this application to aufs-users ML."
4a4d8108
AM
7902+#define AuUnsupport(fmt, ...) do { \
7903+ pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
1facf9fc 7904+ dump_stack(); \
7905+} while (0)
7906+
7907+#define AuTraceErr(e) do { \
7908+ if (unlikely((e) < 0)) \
7909+ AuDbg("err %d\n", (int)(e)); \
7910+} while (0)
7911+
7912+#define AuTraceErrPtr(p) do { \
7913+ if (IS_ERR(p)) \
7914+ AuDbg("err %ld\n", PTR_ERR(p)); \
7915+} while (0)
7916+
7917+/* dirty macros for debug print, use with "%.*s" and caution */
7918+#define AuLNPair(qstr) (qstr)->len, (qstr)->name
1facf9fc 7919+
7920+/* ---------------------------------------------------------------------- */
7921+
dece6358 7922+struct dentry;
1facf9fc 7923+#ifdef CONFIG_AUFS_DEBUG
c1595e42 7924+extern struct mutex au_dbg_mtx;
1facf9fc 7925+extern char *au_plevel;
7926+struct au_nhash;
7927+void au_dpri_whlist(struct au_nhash *whlist);
7928+struct au_vdir;
7929+void au_dpri_vdir(struct au_vdir *vdir);
dece6358 7930+struct inode;
1facf9fc 7931+void au_dpri_inode(struct inode *inode);
2cbb1c4b 7932+void au_dpri_dalias(struct inode *inode);
1facf9fc 7933+void au_dpri_dentry(struct dentry *dentry);
dece6358 7934+struct file;
1facf9fc 7935+void au_dpri_file(struct file *filp);
dece6358 7936+struct super_block;
1facf9fc 7937+void au_dpri_sb(struct super_block *sb);
7938+
027c5e7a
AM
7939+#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7940+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
1facf9fc 7941+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
1facf9fc 7942+void au_dbg_verify_kthread(void);
7943+
7944+int __init au_debug_init(void);
7e9cd9fe 7945+
1facf9fc 7946+#define AuDbgWhlist(w) do { \
c1595e42 7947+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7948+ AuDbg(#w "\n"); \
7949+ au_dpri_whlist(w); \
c1595e42 7950+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7951+} while (0)
7952+
7953+#define AuDbgVdir(v) do { \
c1595e42 7954+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7955+ AuDbg(#v "\n"); \
7956+ au_dpri_vdir(v); \
c1595e42 7957+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7958+} while (0)
7959+
7960+#define AuDbgInode(i) do { \
c1595e42 7961+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7962+ AuDbg(#i "\n"); \
7963+ au_dpri_inode(i); \
c1595e42 7964+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7965+} while (0)
7966+
2cbb1c4b 7967+#define AuDbgDAlias(i) do { \
c1595e42 7968+ mutex_lock(&au_dbg_mtx); \
2cbb1c4b
JR
7969+ AuDbg(#i "\n"); \
7970+ au_dpri_dalias(i); \
c1595e42 7971+ mutex_unlock(&au_dbg_mtx); \
2cbb1c4b
JR
7972+} while (0)
7973+
1facf9fc 7974+#define AuDbgDentry(d) do { \
c1595e42 7975+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7976+ AuDbg(#d "\n"); \
7977+ au_dpri_dentry(d); \
c1595e42 7978+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7979+} while (0)
7980+
7981+#define AuDbgFile(f) do { \
c1595e42 7982+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7983+ AuDbg(#f "\n"); \
7984+ au_dpri_file(f); \
c1595e42 7985+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7986+} while (0)
7987+
7988+#define AuDbgSb(sb) do { \
c1595e42 7989+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7990+ AuDbg(#sb "\n"); \
7991+ au_dpri_sb(sb); \
c1595e42 7992+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7993+} while (0)
7994+
4a4d8108
AM
7995+#define AuDbgSym(addr) do { \
7996+ char sym[KSYM_SYMBOL_LEN]; \
7997+ sprint_symbol(sym, (unsigned long)addr); \
7998+ AuDbg("%s\n", sym); \
7999+} while (0)
1facf9fc 8000+#else
027c5e7a 8001+AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
4a4d8108
AM
8002+AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
8003+AuStubVoid(au_dbg_verify_kthread, void)
8004+AuStubInt0(__init au_debug_init, void)
1facf9fc 8005+
1facf9fc 8006+#define AuDbgWhlist(w) do {} while (0)
8007+#define AuDbgVdir(v) do {} while (0)
8008+#define AuDbgInode(i) do {} while (0)
2cbb1c4b 8009+#define AuDbgDAlias(i) do {} while (0)
1facf9fc 8010+#define AuDbgDentry(d) do {} while (0)
8011+#define AuDbgFile(f) do {} while (0)
8012+#define AuDbgSb(sb) do {} while (0)
4a4d8108 8013+#define AuDbgSym(addr) do {} while (0)
1facf9fc 8014+#endif /* CONFIG_AUFS_DEBUG */
8015+
8016+/* ---------------------------------------------------------------------- */
8017+
8018+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
8019+int __init au_sysrq_init(void);
8020+void au_sysrq_fin(void);
8021+
8022+#ifdef CONFIG_HW_CONSOLE
8023+#define au_dbg_blocked() do { \
8024+ WARN_ON(1); \
0c5527e5 8025+ handle_sysrq('w'); \
1facf9fc 8026+} while (0)
8027+#else
4a4d8108 8028+AuStubVoid(au_dbg_blocked, void)
1facf9fc 8029+#endif
8030+
8031+#else
4a4d8108
AM
8032+AuStubInt0(__init au_sysrq_init, void)
8033+AuStubVoid(au_sysrq_fin, void)
8034+AuStubVoid(au_dbg_blocked, void)
1facf9fc 8035+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
8036+
8037+#endif /* __KERNEL__ */
8038+#endif /* __AUFS_DEBUG_H__ */
7f207e10
AM
8039diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
8040--- /usr/share/empty/fs/aufs/dentry.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
8041+++ linux/fs/aufs/dentry.c 2017-11-12 22:24:44.704244405 +0100
8042@@ -0,0 +1,1152 @@
1facf9fc 8043+/*
a2654f78 8044+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 8045+ *
8046+ * This program, aufs is free software; you can redistribute it and/or modify
8047+ * it under the terms of the GNU General Public License as published by
8048+ * the Free Software Foundation; either version 2 of the License, or
8049+ * (at your option) any later version.
dece6358
AM
8050+ *
8051+ * This program is distributed in the hope that it will be useful,
8052+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
8053+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8054+ * GNU General Public License for more details.
8055+ *
8056+ * You should have received a copy of the GNU General Public License
523b37e3 8057+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 8058+ */
8059+
8060+/*
8061+ * lookup and dentry operations
8062+ */
8063+
dece6358 8064+#include <linux/namei.h>
1facf9fc 8065+#include "aufs.h"
8066+
1facf9fc 8067+/*
8068+ * returns positive/negative dentry, NULL or an error.
8069+ * NULL means whiteout-ed or not-found.
8070+ */
8071+static struct dentry*
8072+au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
8b6a4947 8073+ aufs_bindex_t bindex, struct au_do_lookup_args *args)
1facf9fc 8074+{
8075+ struct dentry *h_dentry;
2000de60 8076+ struct inode *h_inode;
1facf9fc 8077+ struct au_branch *br;
8078+ int wh_found, opq;
8079+ unsigned char wh_able;
8080+ const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
076b876e
AM
8081+ const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8082+ IGNORE_PERM);
1facf9fc 8083+
1facf9fc 8084+ wh_found = 0;
8085+ br = au_sbr(dentry->d_sb, bindex);
8086+ wh_able = !!au_br_whable(br->br_perm);
8087+ if (wh_able)
8b6a4947 8088+ wh_found = au_wh_test(h_parent, &args->whname, ignore_perm);
1facf9fc 8089+ h_dentry = ERR_PTR(wh_found);
8090+ if (!wh_found)
8091+ goto real_lookup;
8092+ if (unlikely(wh_found < 0))
8093+ goto out;
8094+
8095+ /* We found a whiteout */
5afbbe0d 8096+ /* au_set_dbbot(dentry, bindex); */
1facf9fc 8097+ au_set_dbwh(dentry, bindex);
8098+ if (!allow_neg)
8099+ return NULL; /* success */
8100+
4f0767ce 8101+real_lookup:
076b876e 8102+ if (!ignore_perm)
8b6a4947 8103+ h_dentry = vfsub_lkup_one(args->name, h_parent);
076b876e 8104+ else
8b6a4947 8105+ h_dentry = au_sio_lkup_one(args->name, h_parent);
2000de60
JR
8106+ if (IS_ERR(h_dentry)) {
8107+ if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8108+ && !allow_neg)
8109+ h_dentry = NULL;
1facf9fc 8110+ goto out;
2000de60 8111+ }
1facf9fc 8112+
5527c038
JR
8113+ h_inode = d_inode(h_dentry);
8114+ if (d_is_negative(h_dentry)) {
1facf9fc 8115+ if (!allow_neg)
8116+ goto out_neg;
8117+ } else if (wh_found
8118+ || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8119+ goto out_neg;
8b6a4947
AM
8120+ else if (au_ftest_lkup(args->flags, DIRREN)
8121+ /* && h_inode */
8122+ && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8123+ AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8124+ (unsigned long long)h_inode->i_ino);
8125+ goto out_neg;
8126+ }
1facf9fc 8127+
5afbbe0d
AM
8128+ if (au_dbbot(dentry) <= bindex)
8129+ au_set_dbbot(dentry, bindex);
8130+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8131+ au_set_dbtop(dentry, bindex);
1facf9fc 8132+ au_set_h_dptr(dentry, bindex, h_dentry);
8133+
2000de60
JR
8134+ if (!d_is_dir(h_dentry)
8135+ || !wh_able
5527c038 8136+ || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
1facf9fc 8137+ goto out; /* success */
8138+
3c1bdaff 8139+ vfsub_inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
076b876e 8140+ opq = au_diropq_test(h_dentry);
3c1bdaff 8141+ inode_unlock_shared(h_inode);
1facf9fc 8142+ if (opq > 0)
8143+ au_set_dbdiropq(dentry, bindex);
8144+ else if (unlikely(opq < 0)) {
8145+ au_set_h_dptr(dentry, bindex, NULL);
8146+ h_dentry = ERR_PTR(opq);
8147+ }
8148+ goto out;
8149+
4f0767ce 8150+out_neg:
1facf9fc 8151+ dput(h_dentry);
8152+ h_dentry = NULL;
4f0767ce 8153+out:
1facf9fc 8154+ return h_dentry;
8155+}
8156+
dece6358
AM
8157+static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8158+{
8159+ if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8160+ && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8161+ return -EPERM;
8162+ return 0;
8163+}
8164+
1facf9fc 8165+/*
8166+ * returns the number of lower positive dentries,
8167+ * otherwise an error.
8168+ * can be called at unlinking with @type is zero.
8169+ */
5afbbe0d
AM
8170+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8171+ unsigned int flags)
1facf9fc 8172+{
8173+ int npositive, err;
8174+ aufs_bindex_t bindex, btail, bdiropq;
8b6a4947 8175+ unsigned char isdir, dirperm1, dirren;
1facf9fc 8176+ struct au_do_lookup_args args = {
8b6a4947
AM
8177+ .flags = flags,
8178+ .name = &dentry->d_name
1facf9fc 8179+ };
1facf9fc 8180+ struct dentry *parent;
076b876e 8181+ struct super_block *sb;
1facf9fc 8182+
076b876e 8183+ sb = dentry->d_sb;
8b6a4947 8184+ err = au_test_shwh(sb, args.name);
dece6358 8185+ if (unlikely(err))
1facf9fc 8186+ goto out;
8187+
8b6a4947 8188+ err = au_wh_name_alloc(&args.whname, args.name);
1facf9fc 8189+ if (unlikely(err))
8190+ goto out;
8191+
2000de60 8192+ isdir = !!d_is_dir(dentry);
076b876e 8193+ dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8b6a4947
AM
8194+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8195+ if (dirren)
8196+ au_fset_lkup(args.flags, DIRREN);
1facf9fc 8197+
8198+ npositive = 0;
4a4d8108 8199+ parent = dget_parent(dentry);
1facf9fc 8200+ btail = au_dbtaildir(parent);
5afbbe0d 8201+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 8202+ struct dentry *h_parent, *h_dentry;
8203+ struct inode *h_inode, *h_dir;
8b6a4947 8204+ struct au_branch *br;
1facf9fc 8205+
8206+ h_dentry = au_h_dptr(dentry, bindex);
8207+ if (h_dentry) {
5527c038 8208+ if (d_is_positive(h_dentry))
1facf9fc 8209+ npositive++;
5afbbe0d 8210+ break;
1facf9fc 8211+ }
8212+ h_parent = au_h_dptr(parent, bindex);
2000de60 8213+ if (!h_parent || !d_is_dir(h_parent))
1facf9fc 8214+ continue;
8215+
8b6a4947
AM
8216+ if (dirren) {
8217+ /* if the inum matches, then use the prepared name */
8218+ err = au_dr_lkup_name(&args, bindex);
8219+ if (unlikely(err))
8220+ goto out_parent;
8221+ }
8222+
5527c038 8223+ h_dir = d_inode(h_parent);
3c1bdaff 8224+ vfsub_inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8b6a4947 8225+ h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
3c1bdaff 8226+ inode_unlock_shared(h_dir);
1facf9fc 8227+ err = PTR_ERR(h_dentry);
8228+ if (IS_ERR(h_dentry))
4a4d8108 8229+ goto out_parent;
2000de60
JR
8230+ if (h_dentry)
8231+ au_fclr_lkup(args.flags, ALLOW_NEG);
076b876e
AM
8232+ if (dirperm1)
8233+ au_fset_lkup(args.flags, IGNORE_PERM);
1facf9fc 8234+
79b8bda9 8235+ if (au_dbwh(dentry) == bindex)
1facf9fc 8236+ break;
8237+ if (!h_dentry)
8238+ continue;
5527c038 8239+ if (d_is_negative(h_dentry))
1facf9fc 8240+ continue;
5527c038 8241+ h_inode = d_inode(h_dentry);
1facf9fc 8242+ npositive++;
8243+ if (!args.type)
8244+ args.type = h_inode->i_mode & S_IFMT;
8245+ if (args.type != S_IFDIR)
8246+ break;
8247+ else if (isdir) {
8248+ /* the type of lower may be different */
8249+ bdiropq = au_dbdiropq(dentry);
8250+ if (bdiropq >= 0 && bdiropq <= bindex)
8251+ break;
8252+ }
8b6a4947
AM
8253+ br = au_sbr(sb, bindex);
8254+ if (dirren
8255+ && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8256+ /*add_ent*/NULL)) {
8257+ /* prepare next name to lookup */
8258+ err = au_dr_lkup(&args, dentry, bindex);
8259+ if (unlikely(err))
8260+ goto out_parent;
8261+ }
1facf9fc 8262+ }
8263+
8264+ if (npositive) {
8265+ AuLabel(positive);
5afbbe0d 8266+ au_update_dbtop(dentry);
1facf9fc 8267+ }
8268+ err = npositive;
076b876e 8269+ if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
5afbbe0d 8270+ && au_dbtop(dentry) < 0)) {
1facf9fc 8271+ err = -EIO;
523b37e3
AM
8272+ AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8273+ dentry, err);
027c5e7a 8274+ }
1facf9fc 8275+
4f0767ce 8276+out_parent:
4a4d8108 8277+ dput(parent);
8b6a4947
AM
8278+ kfree(args.whname.name);
8279+ if (dirren)
8280+ au_dr_lkup_fin(&args);
4f0767ce 8281+out:
1facf9fc 8282+ return err;
8283+}
8284+
076b876e 8285+struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
1facf9fc 8286+{
8287+ struct dentry *dentry;
8288+ int wkq_err;
8289+
5527c038 8290+ if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
b4510431 8291+ dentry = vfsub_lkup_one(name, parent);
1facf9fc 8292+ else {
b4510431
AM
8293+ struct vfsub_lkup_one_args args = {
8294+ .errp = &dentry,
8295+ .name = name,
8296+ .parent = parent
1facf9fc 8297+ };
8298+
b4510431 8299+ wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
1facf9fc 8300+ if (unlikely(wkq_err))
8301+ dentry = ERR_PTR(wkq_err);
8302+ }
8303+
8304+ return dentry;
8305+}
8306+
8307+/*
8308+ * lookup @dentry on @bindex which should be negative.
8309+ */
86dc4139 8310+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
1facf9fc 8311+{
8312+ int err;
8313+ struct dentry *parent, *h_parent, *h_dentry;
86dc4139 8314+ struct au_branch *br;
1facf9fc 8315+
1facf9fc 8316+ parent = dget_parent(dentry);
8317+ h_parent = au_h_dptr(parent, bindex);
86dc4139
AM
8318+ br = au_sbr(dentry->d_sb, bindex);
8319+ if (wh)
8320+ h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
8321+ else
076b876e 8322+ h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
1facf9fc 8323+ err = PTR_ERR(h_dentry);
8324+ if (IS_ERR(h_dentry))
8325+ goto out;
5527c038 8326+ if (unlikely(d_is_positive(h_dentry))) {
1facf9fc 8327+ err = -EIO;
523b37e3 8328+ AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
1facf9fc 8329+ dput(h_dentry);
8330+ goto out;
8331+ }
8332+
4a4d8108 8333+ err = 0;
5afbbe0d
AM
8334+ if (bindex < au_dbtop(dentry))
8335+ au_set_dbtop(dentry, bindex);
8336+ if (au_dbbot(dentry) < bindex)
8337+ au_set_dbbot(dentry, bindex);
1facf9fc 8338+ au_set_h_dptr(dentry, bindex, h_dentry);
1facf9fc 8339+
4f0767ce 8340+out:
1facf9fc 8341+ dput(parent);
8342+ return err;
8343+}
8344+
8345+/* ---------------------------------------------------------------------- */
8346+
8347+/* subset of struct inode */
8348+struct au_iattr {
8349+ unsigned long i_ino;
8350+ /* unsigned int i_nlink; */
0c3ec466
AM
8351+ kuid_t i_uid;
8352+ kgid_t i_gid;
1facf9fc 8353+ u64 i_version;
8354+/*
8355+ loff_t i_size;
8356+ blkcnt_t i_blocks;
8357+*/
8358+ umode_t i_mode;
8359+};
8360+
8361+static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8362+{
8363+ ia->i_ino = h_inode->i_ino;
8364+ /* ia->i_nlink = h_inode->i_nlink; */
8365+ ia->i_uid = h_inode->i_uid;
8366+ ia->i_gid = h_inode->i_gid;
8367+ ia->i_version = h_inode->i_version;
8368+/*
8369+ ia->i_size = h_inode->i_size;
8370+ ia->i_blocks = h_inode->i_blocks;
8371+*/
8372+ ia->i_mode = (h_inode->i_mode & S_IFMT);
8373+}
8374+
8375+static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8376+{
8377+ return ia->i_ino != h_inode->i_ino
8378+ /* || ia->i_nlink != h_inode->i_nlink */
0c3ec466 8379+ || !uid_eq(ia->i_uid, h_inode->i_uid)
2dfbb274 8380+ || !gid_eq(ia->i_gid, h_inode->i_gid)
1facf9fc 8381+ || ia->i_version != h_inode->i_version
8382+/*
8383+ || ia->i_size != h_inode->i_size
8384+ || ia->i_blocks != h_inode->i_blocks
8385+*/
8386+ || ia->i_mode != (h_inode->i_mode & S_IFMT);
8387+}
8388+
8389+static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8390+ struct au_branch *br)
8391+{
8392+ int err;
8393+ struct au_iattr ia;
8394+ struct inode *h_inode;
8395+ struct dentry *h_d;
8396+ struct super_block *h_sb;
8397+
8398+ err = 0;
8399+ memset(&ia, -1, sizeof(ia));
8400+ h_sb = h_dentry->d_sb;
5527c038
JR
8401+ h_inode = NULL;
8402+ if (d_is_positive(h_dentry)) {
8403+ h_inode = d_inode(h_dentry);
1facf9fc 8404+ au_iattr_save(&ia, h_inode);
5527c038 8405+ } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
1facf9fc 8406+ /* nfs d_revalidate may return 0 for negative dentry */
8407+ /* fuse d_revalidate always return 0 for negative dentry */
8408+ goto out;
8409+
8410+ /* main purpose is namei.c:cached_lookup() and d_revalidate */
b4510431 8411+ h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
1facf9fc 8412+ err = PTR_ERR(h_d);
8413+ if (IS_ERR(h_d))
8414+ goto out;
8415+
8416+ err = 0;
8417+ if (unlikely(h_d != h_dentry
5527c038 8418+ || d_inode(h_d) != h_inode
1facf9fc 8419+ || (h_inode && au_iattr_test(&ia, h_inode))))
8420+ err = au_busy_or_stale();
8421+ dput(h_d);
8422+
4f0767ce 8423+out:
1facf9fc 8424+ AuTraceErr(err);
8425+ return err;
8426+}
8427+
8428+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8429+ struct dentry *h_parent, struct au_branch *br)
8430+{
8431+ int err;
8432+
8433+ err = 0;
027c5e7a
AM
8434+ if (udba == AuOpt_UDBA_REVAL
8435+ && !au_test_fs_remote(h_dentry->d_sb)) {
1facf9fc 8436+ IMustLock(h_dir);
5527c038 8437+ err = (d_inode(h_dentry->d_parent) != h_dir);
027c5e7a 8438+ } else if (udba != AuOpt_UDBA_NONE)
1facf9fc 8439+ err = au_h_verify_dentry(h_dentry, h_parent, br);
8440+
8441+ return err;
8442+}
8443+
8444+/* ---------------------------------------------------------------------- */
8445+
027c5e7a 8446+static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
1facf9fc 8447+{
027c5e7a 8448+ int err;
5afbbe0d 8449+ aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
027c5e7a
AM
8450+ struct au_hdentry tmp, *p, *q;
8451+ struct au_dinfo *dinfo;
8452+ struct super_block *sb;
1facf9fc 8453+
027c5e7a 8454+ DiMustWriteLock(dentry);
1308ab2a 8455+
027c5e7a
AM
8456+ sb = dentry->d_sb;
8457+ dinfo = au_di(dentry);
5afbbe0d 8458+ bbot = dinfo->di_bbot;
1facf9fc 8459+ bwh = dinfo->di_bwh;
8460+ bdiropq = dinfo->di_bdiropq;
5afbbe0d
AM
8461+ bindex = dinfo->di_btop;
8462+ p = au_hdentry(dinfo, bindex);
8463+ for (; bindex <= bbot; bindex++, p++) {
027c5e7a 8464+ if (!p->hd_dentry)
1facf9fc 8465+ continue;
8466+
027c5e7a
AM
8467+ new_bindex = au_br_index(sb, p->hd_id);
8468+ if (new_bindex == bindex)
1facf9fc 8469+ continue;
1facf9fc 8470+
1facf9fc 8471+ if (dinfo->di_bwh == bindex)
8472+ bwh = new_bindex;
8473+ if (dinfo->di_bdiropq == bindex)
8474+ bdiropq = new_bindex;
8475+ if (new_bindex < 0) {
8476+ au_hdput(p);
8477+ p->hd_dentry = NULL;
8478+ continue;
8479+ }
8480+
8481+ /* swap two lower dentries, and loop again */
5afbbe0d 8482+ q = au_hdentry(dinfo, new_bindex);
1facf9fc 8483+ tmp = *q;
8484+ *q = *p;
8485+ *p = tmp;
8486+ if (tmp.hd_dentry) {
8487+ bindex--;
8488+ p--;
8489+ }
8490+ }
8491+
1facf9fc 8492+ dinfo->di_bwh = -1;
5afbbe0d 8493+ if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
1facf9fc 8494+ dinfo->di_bwh = bwh;
8495+
8496+ dinfo->di_bdiropq = -1;
8497+ if (bdiropq >= 0
5afbbe0d 8498+ && bdiropq <= au_sbbot(sb)
1facf9fc 8499+ && au_sbr_whable(sb, bdiropq))
8500+ dinfo->di_bdiropq = bdiropq;
8501+
027c5e7a 8502+ err = -EIO;
5afbbe0d
AM
8503+ dinfo->di_btop = -1;
8504+ dinfo->di_bbot = -1;
8505+ bbot = au_dbbot(parent);
8506+ bindex = 0;
8507+ p = au_hdentry(dinfo, bindex);
8508+ for (; bindex <= bbot; bindex++, p++)
1facf9fc 8509+ if (p->hd_dentry) {
5afbbe0d 8510+ dinfo->di_btop = bindex;
1facf9fc 8511+ break;
8512+ }
8513+
5afbbe0d
AM
8514+ if (dinfo->di_btop >= 0) {
8515+ bindex = bbot;
8516+ p = au_hdentry(dinfo, bindex);
8517+ for (; bindex >= 0; bindex--, p--)
027c5e7a 8518+ if (p->hd_dentry) {
5afbbe0d 8519+ dinfo->di_bbot = bindex;
027c5e7a
AM
8520+ err = 0;
8521+ break;
8522+ }
8523+ }
8524+
8525+ return err;
1facf9fc 8526+}
8527+
027c5e7a 8528+static void au_do_hide(struct dentry *dentry)
1facf9fc 8529+{
027c5e7a 8530+ struct inode *inode;
1facf9fc 8531+
5527c038
JR
8532+ if (d_really_is_positive(dentry)) {
8533+ inode = d_inode(dentry);
8534+ if (!d_is_dir(dentry)) {
027c5e7a
AM
8535+ if (inode->i_nlink && !d_unhashed(dentry))
8536+ drop_nlink(inode);
8537+ } else {
8538+ clear_nlink(inode);
8539+ /* stop next lookup */
8540+ inode->i_flags |= S_DEAD;
8541+ }
8542+ smp_mb(); /* necessary? */
8543+ }
8544+ d_drop(dentry);
8545+}
1308ab2a 8546+
027c5e7a
AM
8547+static int au_hide_children(struct dentry *parent)
8548+{
8549+ int err, i, j, ndentry;
8550+ struct au_dcsub_pages dpages;
8551+ struct au_dpage *dpage;
8552+ struct dentry *dentry;
1facf9fc 8553+
027c5e7a 8554+ err = au_dpages_init(&dpages, GFP_NOFS);
1facf9fc 8555+ if (unlikely(err))
8556+ goto out;
027c5e7a
AM
8557+ err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8558+ if (unlikely(err))
8559+ goto out_dpages;
1facf9fc 8560+
027c5e7a
AM
8561+ /* in reverse order */
8562+ for (i = dpages.ndpage - 1; i >= 0; i--) {
8563+ dpage = dpages.dpages + i;
8564+ ndentry = dpage->ndentry;
8565+ for (j = ndentry - 1; j >= 0; j--) {
8566+ dentry = dpage->dentries[j];
8567+ if (dentry != parent)
8568+ au_do_hide(dentry);
8569+ }
8570+ }
1facf9fc 8571+
027c5e7a
AM
8572+out_dpages:
8573+ au_dpages_free(&dpages);
4f0767ce 8574+out:
027c5e7a 8575+ return err;
1facf9fc 8576+}
8577+
027c5e7a 8578+static void au_hide(struct dentry *dentry)
1facf9fc 8579+{
027c5e7a 8580+ int err;
1facf9fc 8581+
027c5e7a 8582+ AuDbgDentry(dentry);
2000de60 8583+ if (d_is_dir(dentry)) {
027c5e7a
AM
8584+ /* shrink_dcache_parent(dentry); */
8585+ err = au_hide_children(dentry);
8586+ if (unlikely(err))
523b37e3
AM
8587+ AuIOErr("%pd, failed hiding children, ignored %d\n",
8588+ dentry, err);
027c5e7a
AM
8589+ }
8590+ au_do_hide(dentry);
8591+}
1facf9fc 8592+
027c5e7a
AM
8593+/*
8594+ * By adding a dirty branch, a cached dentry may be affected in various ways.
8595+ *
8596+ * a dirty branch is added
8597+ * - on the top of layers
8598+ * - in the middle of layers
8599+ * - to the bottom of layers
8600+ *
8601+ * on the added branch there exists
8602+ * - a whiteout
8603+ * - a diropq
8604+ * - a same named entry
8605+ * + exist
8606+ * * negative --> positive
8607+ * * positive --> positive
8608+ * - type is unchanged
8609+ * - type is changed
8610+ * + doesn't exist
8611+ * * negative --> negative
8612+ * * positive --> negative (rejected by au_br_del() for non-dir case)
8613+ * - none
8614+ */
8615+static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8616+ struct au_dinfo *tmp)
8617+{
8618+ int err;
5afbbe0d 8619+ aufs_bindex_t bindex, bbot;
027c5e7a
AM
8620+ struct {
8621+ struct dentry *dentry;
8622+ struct inode *inode;
8623+ mode_t mode;
be52b249
AM
8624+ } orig_h, tmp_h = {
8625+ .dentry = NULL
8626+ };
027c5e7a
AM
8627+ struct au_hdentry *hd;
8628+ struct inode *inode, *h_inode;
8629+ struct dentry *h_dentry;
8630+
8631+ err = 0;
5afbbe0d 8632+ AuDebugOn(dinfo->di_btop < 0);
027c5e7a 8633+ orig_h.mode = 0;
5afbbe0d 8634+ orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
5527c038
JR
8635+ orig_h.inode = NULL;
8636+ if (d_is_positive(orig_h.dentry)) {
8637+ orig_h.inode = d_inode(orig_h.dentry);
027c5e7a 8638+ orig_h.mode = orig_h.inode->i_mode & S_IFMT;
5527c038 8639+ }
5afbbe0d
AM
8640+ if (tmp->di_btop >= 0) {
8641+ tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
5527c038
JR
8642+ if (d_is_positive(tmp_h.dentry)) {
8643+ tmp_h.inode = d_inode(tmp_h.dentry);
027c5e7a 8644+ tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
5527c038 8645+ }
027c5e7a
AM
8646+ }
8647+
5527c038
JR
8648+ inode = NULL;
8649+ if (d_really_is_positive(dentry))
8650+ inode = d_inode(dentry);
027c5e7a
AM
8651+ if (!orig_h.inode) {
8652+ AuDbg("nagative originally\n");
8653+ if (inode) {
8654+ au_hide(dentry);
8655+ goto out;
8656+ }
8657+ AuDebugOn(inode);
5afbbe0d 8658+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
027c5e7a
AM
8659+ AuDebugOn(dinfo->di_bdiropq != -1);
8660+
8661+ if (!tmp_h.inode) {
8662+ AuDbg("negative --> negative\n");
8663+ /* should have only one negative lower */
5afbbe0d
AM
8664+ if (tmp->di_btop >= 0
8665+ && tmp->di_btop < dinfo->di_btop) {
8666+ AuDebugOn(tmp->di_btop != tmp->di_bbot);
8667+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8668+ au_set_h_dptr(dentry, dinfo->di_btop, NULL);
027c5e7a 8669+ au_di_cp(dinfo, tmp);
5afbbe0d
AM
8670+ hd = au_hdentry(tmp, tmp->di_btop);
8671+ au_set_h_dptr(dentry, tmp->di_btop,
027c5e7a
AM
8672+ dget(hd->hd_dentry));
8673+ }
8674+ au_dbg_verify_dinode(dentry);
8675+ } else {
8676+ AuDbg("negative --> positive\n");
8677+ /*
8678+ * similar to the behaviour of creating with bypassing
8679+ * aufs.
8680+ * unhash it in order to force an error in the
8681+ * succeeding create operation.
8682+ * we should not set S_DEAD here.
8683+ */
8684+ d_drop(dentry);
8685+ /* au_di_swap(tmp, dinfo); */
8686+ au_dbg_verify_dinode(dentry);
8687+ }
8688+ } else {
8689+ AuDbg("positive originally\n");
8690+ /* inode may be NULL */
8691+ AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8692+ if (!tmp_h.inode) {
8693+ AuDbg("positive --> negative\n");
8694+ /* or bypassing aufs */
8695+ au_hide(dentry);
5afbbe0d 8696+ if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
027c5e7a
AM
8697+ dinfo->di_bwh = tmp->di_bwh;
8698+ if (inode)
8699+ err = au_refresh_hinode_self(inode);
8700+ au_dbg_verify_dinode(dentry);
8701+ } else if (orig_h.mode == tmp_h.mode) {
8702+ AuDbg("positive --> positive, same type\n");
8703+ if (!S_ISDIR(orig_h.mode)
5afbbe0d 8704+ && dinfo->di_btop > tmp->di_btop) {
027c5e7a
AM
8705+ /*
8706+ * similar to the behaviour of removing and
8707+ * creating.
8708+ */
8709+ au_hide(dentry);
8710+ if (inode)
8711+ err = au_refresh_hinode_self(inode);
8712+ au_dbg_verify_dinode(dentry);
8713+ } else {
8714+ /* fill empty slots */
5afbbe0d
AM
8715+ if (dinfo->di_btop > tmp->di_btop)
8716+ dinfo->di_btop = tmp->di_btop;
8717+ if (dinfo->di_bbot < tmp->di_bbot)
8718+ dinfo->di_bbot = tmp->di_bbot;
027c5e7a
AM
8719+ dinfo->di_bwh = tmp->di_bwh;
8720+ dinfo->di_bdiropq = tmp->di_bdiropq;
5afbbe0d
AM
8721+ bbot = dinfo->di_bbot;
8722+ bindex = tmp->di_btop;
8723+ hd = au_hdentry(tmp, bindex);
8724+ for (; bindex <= bbot; bindex++, hd++) {
027c5e7a
AM
8725+ if (au_h_dptr(dentry, bindex))
8726+ continue;
5afbbe0d 8727+ h_dentry = hd->hd_dentry;
027c5e7a
AM
8728+ if (!h_dentry)
8729+ continue;
5527c038
JR
8730+ AuDebugOn(d_is_negative(h_dentry));
8731+ h_inode = d_inode(h_dentry);
027c5e7a
AM
8732+ AuDebugOn(orig_h.mode
8733+ != (h_inode->i_mode
8734+ & S_IFMT));
8735+ au_set_h_dptr(dentry, bindex,
8736+ dget(h_dentry));
8737+ }
5afbbe0d
AM
8738+ if (inode)
8739+ err = au_refresh_hinode(inode, dentry);
027c5e7a
AM
8740+ au_dbg_verify_dinode(dentry);
8741+ }
8742+ } else {
8743+ AuDbg("positive --> positive, different type\n");
8744+ /* similar to the behaviour of removing and creating */
8745+ au_hide(dentry);
8746+ if (inode)
8747+ err = au_refresh_hinode_self(inode);
8748+ au_dbg_verify_dinode(dentry);
8749+ }
8750+ }
8751+
8752+out:
8753+ return err;
8754+}
8755+
79b8bda9
AM
8756+void au_refresh_dop(struct dentry *dentry, int force_reval)
8757+{
8758+ const struct dentry_operations *dop
8759+ = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8760+ static const unsigned int mask
8761+ = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8762+
8763+ BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8764+
8765+ if (dentry->d_op == dop)
8766+ return;
8767+
8768+ AuDbg("%pd\n", dentry);
8769+ spin_lock(&dentry->d_lock);
8770+ if (dop == &aufs_dop)
8771+ dentry->d_flags |= mask;
8772+ else
8773+ dentry->d_flags &= ~mask;
8774+ dentry->d_op = dop;
8775+ spin_unlock(&dentry->d_lock);
8776+}
8777+
027c5e7a
AM
8778+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8779+{
e2f27e51 8780+ int err, ebrange, nbr;
027c5e7a
AM
8781+ unsigned int sigen;
8782+ struct au_dinfo *dinfo, *tmp;
8783+ struct super_block *sb;
8784+ struct inode *inode;
8785+
8786+ DiMustWriteLock(dentry);
8787+ AuDebugOn(IS_ROOT(dentry));
5527c038 8788+ AuDebugOn(d_really_is_negative(parent));
027c5e7a
AM
8789+
8790+ sb = dentry->d_sb;
027c5e7a
AM
8791+ sigen = au_sigen(sb);
8792+ err = au_digen_test(parent, sigen);
8793+ if (unlikely(err))
8794+ goto out;
8795+
e2f27e51 8796+ nbr = au_sbbot(sb) + 1;
027c5e7a 8797+ dinfo = au_di(dentry);
e2f27e51 8798+ err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
027c5e7a
AM
8799+ if (unlikely(err))
8800+ goto out;
8801+ ebrange = au_dbrange_test(dentry);
8802+ if (!ebrange)
8803+ ebrange = au_do_refresh_hdentry(dentry, parent);
8804+
38d290e6 8805+ if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
5afbbe0d 8806+ AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
5527c038
JR
8807+ if (d_really_is_positive(dentry)) {
8808+ inode = d_inode(dentry);
027c5e7a 8809+ err = au_refresh_hinode_self(inode);
5527c038 8810+ }
027c5e7a
AM
8811+ au_dbg_verify_dinode(dentry);
8812+ if (!err)
8813+ goto out_dgen; /* success */
8814+ goto out;
8815+ }
8816+
8817+ /* temporary dinfo */
8818+ AuDbgDentry(dentry);
8819+ err = -ENOMEM;
8820+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8821+ if (unlikely(!tmp))
8822+ goto out;
8823+ au_di_swap(tmp, dinfo);
8824+ /* returns the number of positive dentries */
8825+ /*
8826+ * if current working dir is removed, it returns an error.
8827+ * but the dentry is legal.
8828+ */
5afbbe0d 8829+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
027c5e7a
AM
8830+ AuDbgDentry(dentry);
8831+ au_di_swap(tmp, dinfo);
8832+ if (err == -ENOENT)
8833+ err = 0;
8834+ if (err >= 0) {
8835+ /* compare/refresh by dinfo */
8836+ AuDbgDentry(dentry);
8837+ err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8838+ au_dbg_verify_dinode(dentry);
8839+ AuTraceErr(err);
8840+ }
e2f27e51 8841+ au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
027c5e7a
AM
8842+ au_rw_write_unlock(&tmp->di_rwsem);
8843+ au_di_free(tmp);
8844+ if (unlikely(err))
8845+ goto out;
8846+
8847+out_dgen:
8848+ au_update_digen(dentry);
8849+out:
8850+ if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
523b37e3 8851+ AuIOErr("failed refreshing %pd, %d\n", dentry, err);
027c5e7a
AM
8852+ AuDbgDentry(dentry);
8853+ }
8854+ AuTraceErr(err);
8855+ return err;
8856+}
8857+
b4510431
AM
8858+static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8859+ struct dentry *dentry, aufs_bindex_t bindex)
027c5e7a
AM
8860+{
8861+ int err, valid;
027c5e7a
AM
8862+
8863+ err = 0;
8864+ if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8865+ goto out;
027c5e7a
AM
8866+
8867+ AuDbg("b%d\n", bindex);
b4510431
AM
8868+ /*
8869+ * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8870+ * due to whiteout and branch permission.
8871+ */
8872+ flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8873+ | LOOKUP_FOLLOW | LOOKUP_EXCL);
8874+ /* it may return tri-state */
8875+ valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
1facf9fc 8876+
8877+ if (unlikely(valid < 0))
8878+ err = valid;
8879+ else if (!valid)
8880+ err = -EINVAL;
8881+
4f0767ce 8882+out:
1facf9fc 8883+ AuTraceErr(err);
8884+ return err;
8885+}
8886+
8887+/* todo: remove this */
8888+static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8b6a4947 8889+ unsigned int flags, int do_udba, int dirren)
1facf9fc 8890+{
8891+ int err;
8892+ umode_t mode, h_mode;
5afbbe0d 8893+ aufs_bindex_t bindex, btail, btop, ibs, ibe;
38d290e6 8894+ unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
4a4d8108 8895+ struct inode *h_inode, *h_cached_inode;
1facf9fc 8896+ struct dentry *h_dentry;
8897+ struct qstr *name, *h_name;
8898+
8899+ err = 0;
8900+ plus = 0;
8901+ mode = 0;
1facf9fc 8902+ ibs = -1;
8903+ ibe = -1;
8904+ unhashed = !!d_unhashed(dentry);
8905+ is_root = !!IS_ROOT(dentry);
8906+ name = &dentry->d_name;
38d290e6 8907+ tmpfile = au_di(dentry)->di_tmpfile;
1facf9fc 8908+
8909+ /*
7f207e10
AM
8910+ * Theoretically, REVAL test should be unnecessary in case of
8911+ * {FS,I}NOTIFY.
8912+ * But {fs,i}notify doesn't fire some necessary events,
1facf9fc 8913+ * IN_ATTRIB for atime/nlink/pageio
1facf9fc 8914+ * Let's do REVAL test too.
8915+ */
8916+ if (do_udba && inode) {
8917+ mode = (inode->i_mode & S_IFMT);
8918+ plus = (inode->i_nlink > 0);
5afbbe0d
AM
8919+ ibs = au_ibtop(inode);
8920+ ibe = au_ibbot(inode);
1facf9fc 8921+ }
8922+
5afbbe0d
AM
8923+ btop = au_dbtop(dentry);
8924+ btail = btop;
1facf9fc 8925+ if (inode && S_ISDIR(inode->i_mode))
8926+ btail = au_dbtaildir(dentry);
5afbbe0d 8927+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 8928+ h_dentry = au_h_dptr(dentry, bindex);
8929+ if (!h_dentry)
8930+ continue;
8931+
523b37e3
AM
8932+ AuDbg("b%d, %pd\n", bindex, h_dentry);
8933+ h_nfs = !!au_test_nfs(h_dentry->d_sb);
027c5e7a 8934+ spin_lock(&h_dentry->d_lock);
1facf9fc 8935+ h_name = &h_dentry->d_name;
8936+ if (unlikely(do_udba
8937+ && !is_root
523b37e3
AM
8938+ && ((!h_nfs
8939+ && (unhashed != !!d_unhashed(h_dentry)
8b6a4947 8940+ || (!tmpfile && !dirren
38d290e6
JR
8941+ && !au_qstreq(name, h_name))
8942+ ))
523b37e3
AM
8943+ || (h_nfs
8944+ && !(flags & LOOKUP_OPEN)
8945+ && (h_dentry->d_flags
8946+ & DCACHE_NFSFS_RENAMED)))
1facf9fc 8947+ )) {
38d290e6
JR
8948+ int h_unhashed;
8949+
8950+ h_unhashed = d_unhashed(h_dentry);
027c5e7a 8951+ spin_unlock(&h_dentry->d_lock);
38d290e6
JR
8952+ AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8953+ unhashed, h_unhashed, dentry, h_dentry);
1facf9fc 8954+ goto err;
8955+ }
027c5e7a 8956+ spin_unlock(&h_dentry->d_lock);
1facf9fc 8957+
b4510431 8958+ err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
1facf9fc 8959+ if (unlikely(err))
8960+ /* do not goto err, to keep the errno */
8961+ break;
8962+
8963+ /* todo: plink too? */
8964+ if (!do_udba)
8965+ continue;
8966+
8967+ /* UDBA tests */
5527c038 8968+ if (unlikely(!!inode != d_is_positive(h_dentry)))
1facf9fc 8969+ goto err;
8970+
5527c038
JR
8971+ h_inode = NULL;
8972+ if (d_is_positive(h_dentry))
8973+ h_inode = d_inode(h_dentry);
1facf9fc 8974+ h_plus = plus;
8975+ h_mode = mode;
8976+ h_cached_inode = h_inode;
8977+ if (h_inode) {
8978+ h_mode = (h_inode->i_mode & S_IFMT);
8979+ h_plus = (h_inode->i_nlink > 0);
8980+ }
8981+ if (inode && ibs <= bindex && bindex <= ibe)
8982+ h_cached_inode = au_h_iptr(inode, bindex);
8983+
523b37e3 8984+ if (!h_nfs) {
38d290e6 8985+ if (unlikely(plus != h_plus && !tmpfile))
523b37e3
AM
8986+ goto err;
8987+ } else {
8988+ if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8989+ && !is_root
8990+ && !IS_ROOT(h_dentry)
8991+ && unhashed != d_unhashed(h_dentry)))
8992+ goto err;
8993+ }
8994+ if (unlikely(mode != h_mode
1facf9fc 8995+ || h_cached_inode != h_inode))
8996+ goto err;
8997+ continue;
8998+
f6b6e03d 8999+err:
1facf9fc 9000+ err = -EINVAL;
9001+ break;
9002+ }
9003+
523b37e3 9004+ AuTraceErr(err);
1facf9fc 9005+ return err;
9006+}
9007+
027c5e7a 9008+/* todo: consolidate with do_refresh() and au_reval_for_attr() */
1facf9fc 9009+static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
9010+{
9011+ int err;
9012+ struct dentry *parent;
1facf9fc 9013+
027c5e7a 9014+ if (!au_digen_test(dentry, sigen))
1facf9fc 9015+ return 0;
9016+
9017+ parent = dget_parent(dentry);
9018+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 9019+ AuDebugOn(au_digen_test(parent, sigen));
1facf9fc 9020+ au_dbg_verify_gen(parent, sigen);
027c5e7a 9021+ err = au_refresh_dentry(dentry, parent);
1facf9fc 9022+ di_read_unlock(parent, AuLock_IR);
9023+ dput(parent);
027c5e7a 9024+ AuTraceErr(err);
1facf9fc 9025+ return err;
9026+}
9027+
9028+int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
9029+{
9030+ int err;
9031+ struct dentry *d, *parent;
1facf9fc 9032+
027c5e7a 9033+ if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
1facf9fc 9034+ return simple_reval_dpath(dentry, sigen);
9035+
9036+ /* slow loop, keep it simple and stupid */
9037+ /* cf: au_cpup_dirs() */
9038+ err = 0;
9039+ parent = NULL;
027c5e7a 9040+ while (au_digen_test(dentry, sigen)) {
1facf9fc 9041+ d = dentry;
9042+ while (1) {
9043+ dput(parent);
9044+ parent = dget_parent(d);
027c5e7a 9045+ if (!au_digen_test(parent, sigen))
1facf9fc 9046+ break;
9047+ d = parent;
9048+ }
9049+
1facf9fc 9050+ if (d != dentry)
027c5e7a 9051+ di_write_lock_child2(d);
1facf9fc 9052+
9053+ /* someone might update our dentry while we were sleeping */
027c5e7a
AM
9054+ if (au_digen_test(d, sigen)) {
9055+ /*
9056+ * todo: consolidate with simple_reval_dpath(),
9057+ * do_refresh() and au_reval_for_attr().
9058+ */
1facf9fc 9059+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 9060+ err = au_refresh_dentry(d, parent);
1facf9fc 9061+ di_read_unlock(parent, AuLock_IR);
9062+ }
9063+
9064+ if (d != dentry)
9065+ di_write_unlock(d);
9066+ dput(parent);
9067+ if (unlikely(err))
9068+ break;
9069+ }
9070+
9071+ return err;
9072+}
9073+
9074+/*
9075+ * if valid returns 1, otherwise 0.
9076+ */
b4510431 9077+static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
1facf9fc 9078+{
9079+ int valid, err;
9080+ unsigned int sigen;
8b6a4947 9081+ unsigned char do_udba, dirren;
1facf9fc 9082+ struct super_block *sb;
9083+ struct inode *inode;
9084+
027c5e7a 9085+ /* todo: support rcu-walk? */
b4510431 9086+ if (flags & LOOKUP_RCU)
027c5e7a
AM
9087+ return -ECHILD;
9088+
9089+ valid = 0;
9090+ if (unlikely(!au_di(dentry)))
9091+ goto out;
9092+
e49829fe 9093+ valid = 1;
1facf9fc 9094+ sb = dentry->d_sb;
e49829fe
JR
9095+ /*
9096+ * todo: very ugly
9097+ * i_mutex of parent dir may be held,
9098+ * but we should not return 'invalid' due to busy.
9099+ */
9100+ err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9101+ if (unlikely(err)) {
9102+ valid = err;
027c5e7a 9103+ AuTraceErr(err);
e49829fe
JR
9104+ goto out;
9105+ }
5527c038
JR
9106+ inode = NULL;
9107+ if (d_really_is_positive(dentry))
9108+ inode = d_inode(dentry);
5afbbe0d 9109+ if (unlikely(inode && au_is_bad_inode(inode))) {
c1595e42
JR
9110+ err = -EINVAL;
9111+ AuTraceErr(err);
9112+ goto out_dgrade;
9113+ }
027c5e7a
AM
9114+ if (unlikely(au_dbrange_test(dentry))) {
9115+ err = -EINVAL;
9116+ AuTraceErr(err);
9117+ goto out_dgrade;
1facf9fc 9118+ }
027c5e7a
AM
9119+
9120+ sigen = au_sigen(sb);
9121+ if (au_digen_test(dentry, sigen)) {
1facf9fc 9122+ AuDebugOn(IS_ROOT(dentry));
027c5e7a
AM
9123+ err = au_reval_dpath(dentry, sigen);
9124+ if (unlikely(err)) {
9125+ AuTraceErr(err);
1facf9fc 9126+ goto out_dgrade;
027c5e7a 9127+ }
1facf9fc 9128+ }
9129+ di_downgrade_lock(dentry, AuLock_IR);
9130+
1facf9fc 9131+ err = -EINVAL;
c1595e42 9132+ if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
523b37e3 9133+ && inode
38d290e6 9134+ && !(inode->i_state && I_LINKABLE)
79b8bda9
AM
9135+ && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9136+ AuTraceErr(err);
027c5e7a 9137+ goto out_inval;
79b8bda9 9138+ }
027c5e7a 9139+
1facf9fc 9140+ do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9141+ if (do_udba && inode) {
5afbbe0d 9142+ aufs_bindex_t btop = au_ibtop(inode);
027c5e7a 9143+ struct inode *h_inode;
1facf9fc 9144+
5afbbe0d
AM
9145+ if (btop >= 0) {
9146+ h_inode = au_h_iptr(inode, btop);
79b8bda9
AM
9147+ if (h_inode && au_test_higen(inode, h_inode)) {
9148+ AuTraceErr(err);
027c5e7a 9149+ goto out_inval;
79b8bda9 9150+ }
027c5e7a 9151+ }
1facf9fc 9152+ }
9153+
8b6a4947
AM
9154+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9155+ err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
5afbbe0d 9156+ if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
1facf9fc 9157+ err = -EIO;
523b37e3
AM
9158+ AuDbg("both of real entry and whiteout found, %p, err %d\n",
9159+ dentry, err);
027c5e7a 9160+ }
e49829fe 9161+ goto out_inval;
1facf9fc 9162+
4f0767ce 9163+out_dgrade:
1facf9fc 9164+ di_downgrade_lock(dentry, AuLock_IR);
e49829fe 9165+out_inval:
1facf9fc 9166+ aufs_read_unlock(dentry, AuLock_IR);
9167+ AuTraceErr(err);
9168+ valid = !err;
e49829fe 9169+out:
027c5e7a 9170+ if (!valid) {
523b37e3 9171+ AuDbg("%pd invalid, %d\n", dentry, valid);
027c5e7a
AM
9172+ d_drop(dentry);
9173+ }
1facf9fc 9174+ return valid;
9175+}
9176+
9177+static void aufs_d_release(struct dentry *dentry)
9178+{
027c5e7a 9179+ if (au_di(dentry)) {
4a4d8108
AM
9180+ au_di_fin(dentry);
9181+ au_hn_di_reinit(dentry);
1facf9fc 9182+ }
1facf9fc 9183+}
9184+
4a4d8108 9185+const struct dentry_operations aufs_dop = {
c06a8ce3
AM
9186+ .d_revalidate = aufs_d_revalidate,
9187+ .d_weak_revalidate = aufs_d_revalidate,
9188+ .d_release = aufs_d_release
1facf9fc 9189+};
79b8bda9
AM
9190+
9191+/* aufs_dop without d_revalidate */
9192+const struct dentry_operations aufs_dop_noreval = {
9193+ .d_release = aufs_d_release
9194+};
7f207e10
AM
9195diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9196--- /usr/share/empty/fs/aufs/dentry.h 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
9197+++ linux/fs/aufs/dentry.h 2017-11-12 22:24:44.704244405 +0100
9198@@ -0,0 +1,266 @@
1facf9fc 9199+/*
a2654f78 9200+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 9201+ *
9202+ * This program, aufs is free software; you can redistribute it and/or modify
9203+ * it under the terms of the GNU General Public License as published by
9204+ * the Free Software Foundation; either version 2 of the License, or
9205+ * (at your option) any later version.
dece6358
AM
9206+ *
9207+ * This program is distributed in the hope that it will be useful,
9208+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9209+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9210+ * GNU General Public License for more details.
9211+ *
9212+ * You should have received a copy of the GNU General Public License
523b37e3 9213+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9214+ */
9215+
9216+/*
9217+ * lookup and dentry operations
9218+ */
9219+
9220+#ifndef __AUFS_DENTRY_H__
9221+#define __AUFS_DENTRY_H__
9222+
9223+#ifdef __KERNEL__
9224+
dece6358 9225+#include <linux/dcache.h>
8b6a4947 9226+#include "dirren.h"
1facf9fc 9227+#include "rwsem.h"
9228+
1facf9fc 9229+struct au_hdentry {
9230+ struct dentry *hd_dentry;
027c5e7a 9231+ aufs_bindex_t hd_id;
1facf9fc 9232+};
9233+
9234+struct au_dinfo {
9235+ atomic_t di_generation;
9236+
dece6358 9237+ struct au_rwsem di_rwsem;
5afbbe0d 9238+ aufs_bindex_t di_btop, di_bbot, di_bwh, di_bdiropq;
38d290e6 9239+ unsigned char di_tmpfile; /* to allow the different name */
1c60b727 9240+ struct au_hdentry *di_hdentry;
4a4d8108 9241+} ____cacheline_aligned_in_smp;
1facf9fc 9242+
9243+/* ---------------------------------------------------------------------- */
9244+
5afbbe0d
AM
9245+/* flags for au_lkup_dentry() */
9246+#define AuLkup_ALLOW_NEG 1
9247+#define AuLkup_IGNORE_PERM (1 << 1)
8b6a4947 9248+#define AuLkup_DIRREN (1 << 2)
5afbbe0d
AM
9249+#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
9250+#define au_fset_lkup(flags, name) \
9251+ do { (flags) |= AuLkup_##name; } while (0)
9252+#define au_fclr_lkup(flags, name) \
9253+ do { (flags) &= ~AuLkup_##name; } while (0)
9254+
8b6a4947
AM
9255+#ifndef CONFIG_AUFS_DIRREN
9256+#undef AuLkup_DIRREN
9257+#define AuLkup_DIRREN 0
9258+#endif
9259+
9260+struct au_do_lookup_args {
9261+ unsigned int flags;
9262+ mode_t type;
9263+ struct qstr whname, *name;
9264+ struct au_dr_lookup dirren;
9265+};
9266+
5afbbe0d
AM
9267+/* ---------------------------------------------------------------------- */
9268+
1facf9fc 9269+/* dentry.c */
79b8bda9 9270+extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
1facf9fc 9271+struct au_branch;
076b876e 9272+struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
1facf9fc 9273+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9274+ struct dentry *h_parent, struct au_branch *br);
9275+
5afbbe0d
AM
9276+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9277+ unsigned int flags);
86dc4139 9278+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
027c5e7a 9279+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
1facf9fc 9280+int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
79b8bda9 9281+void au_refresh_dop(struct dentry *dentry, int force_reval);
1facf9fc 9282+
9283+/* dinfo.c */
4a4d8108 9284+void au_di_init_once(void *_di);
027c5e7a
AM
9285+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9286+void au_di_free(struct au_dinfo *dinfo);
9287+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9288+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
4a4d8108
AM
9289+int au_di_init(struct dentry *dentry);
9290+void au_di_fin(struct dentry *dentry);
e2f27e51 9291+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
1facf9fc 9292+
9293+void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9294+void di_read_unlock(struct dentry *d, int flags);
9295+void di_downgrade_lock(struct dentry *d, int flags);
9296+void di_write_lock(struct dentry *d, unsigned int lsc);
9297+void di_write_unlock(struct dentry *d);
9298+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9299+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9300+void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9301+
9302+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
2cbb1c4b 9303+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
1facf9fc 9304+aufs_bindex_t au_dbtail(struct dentry *dentry);
9305+aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9306+
9307+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9308+ struct dentry *h_dentry);
027c5e7a
AM
9309+int au_digen_test(struct dentry *dentry, unsigned int sigen);
9310+int au_dbrange_test(struct dentry *dentry);
1facf9fc 9311+void au_update_digen(struct dentry *dentry);
9312+void au_update_dbrange(struct dentry *dentry, int do_put_zero);
5afbbe0d
AM
9313+void au_update_dbtop(struct dentry *dentry);
9314+void au_update_dbbot(struct dentry *dentry);
1facf9fc 9315+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9316+
9317+/* ---------------------------------------------------------------------- */
9318+
9319+static inline struct au_dinfo *au_di(struct dentry *dentry)
9320+{
9321+ return dentry->d_fsdata;
9322+}
9323+
9324+/* ---------------------------------------------------------------------- */
9325+
9326+/* lock subclass for dinfo */
9327+enum {
9328+ AuLsc_DI_CHILD, /* child first */
4a4d8108 9329+ AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
1facf9fc 9330+ AuLsc_DI_CHILD3, /* copyup dirs */
9331+ AuLsc_DI_PARENT,
9332+ AuLsc_DI_PARENT2,
027c5e7a
AM
9333+ AuLsc_DI_PARENT3,
9334+ AuLsc_DI_TMP /* temp for replacing dinfo */
1facf9fc 9335+};
9336+
9337+/*
9338+ * di_read_lock_child, di_write_lock_child,
9339+ * di_read_lock_child2, di_write_lock_child2,
9340+ * di_read_lock_child3, di_write_lock_child3,
9341+ * di_read_lock_parent, di_write_lock_parent,
9342+ * di_read_lock_parent2, di_write_lock_parent2,
9343+ * di_read_lock_parent3, di_write_lock_parent3,
9344+ */
9345+#define AuReadLockFunc(name, lsc) \
9346+static inline void di_read_lock_##name(struct dentry *d, int flags) \
9347+{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9348+
9349+#define AuWriteLockFunc(name, lsc) \
9350+static inline void di_write_lock_##name(struct dentry *d) \
9351+{ di_write_lock(d, AuLsc_DI_##lsc); }
9352+
9353+#define AuRWLockFuncs(name, lsc) \
9354+ AuReadLockFunc(name, lsc) \
9355+ AuWriteLockFunc(name, lsc)
9356+
9357+AuRWLockFuncs(child, CHILD);
9358+AuRWLockFuncs(child2, CHILD2);
9359+AuRWLockFuncs(child3, CHILD3);
9360+AuRWLockFuncs(parent, PARENT);
9361+AuRWLockFuncs(parent2, PARENT2);
9362+AuRWLockFuncs(parent3, PARENT3);
9363+
9364+#undef AuReadLockFunc
9365+#undef AuWriteLockFunc
9366+#undef AuRWLockFuncs
9367+
9368+#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
dece6358
AM
9369+#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
9370+#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
1facf9fc 9371+
9372+/* ---------------------------------------------------------------------- */
9373+
9374+/* todo: memory barrier? */
9375+static inline unsigned int au_digen(struct dentry *d)
9376+{
9377+ return atomic_read(&au_di(d)->di_generation);
9378+}
9379+
9380+static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9381+{
9382+ hdentry->hd_dentry = NULL;
9383+}
9384+
5afbbe0d
AM
9385+static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9386+ aufs_bindex_t bindex)
9387+{
9388+ return di->di_hdentry + bindex;
9389+}
9390+
1facf9fc 9391+static inline void au_hdput(struct au_hdentry *hd)
9392+{
4a4d8108
AM
9393+ if (hd)
9394+ dput(hd->hd_dentry);
1facf9fc 9395+}
9396+
5afbbe0d 9397+static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
1facf9fc 9398+{
1308ab2a 9399+ DiMustAnyLock(dentry);
5afbbe0d 9400+ return au_di(dentry)->di_btop;
1facf9fc 9401+}
9402+
5afbbe0d 9403+static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
1facf9fc 9404+{
1308ab2a 9405+ DiMustAnyLock(dentry);
5afbbe0d 9406+ return au_di(dentry)->di_bbot;
1facf9fc 9407+}
9408+
9409+static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9410+{
1308ab2a 9411+ DiMustAnyLock(dentry);
1facf9fc 9412+ return au_di(dentry)->di_bwh;
9413+}
9414+
9415+static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9416+{
1308ab2a 9417+ DiMustAnyLock(dentry);
1facf9fc 9418+ return au_di(dentry)->di_bdiropq;
9419+}
9420+
9421+/* todo: hard/soft set? */
5afbbe0d 9422+static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9423+{
1308ab2a 9424+ DiMustWriteLock(dentry);
5afbbe0d 9425+ au_di(dentry)->di_btop = bindex;
1facf9fc 9426+}
9427+
5afbbe0d 9428+static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9429+{
1308ab2a 9430+ DiMustWriteLock(dentry);
5afbbe0d 9431+ au_di(dentry)->di_bbot = bindex;
1facf9fc 9432+}
9433+
9434+static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9435+{
1308ab2a 9436+ DiMustWriteLock(dentry);
5afbbe0d 9437+ /* dbwh can be outside of btop - bbot range */
1facf9fc 9438+ au_di(dentry)->di_bwh = bindex;
9439+}
9440+
9441+static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9442+{
1308ab2a 9443+ DiMustWriteLock(dentry);
1facf9fc 9444+ au_di(dentry)->di_bdiropq = bindex;
9445+}
9446+
9447+/* ---------------------------------------------------------------------- */
9448+
4a4d8108 9449+#ifdef CONFIG_AUFS_HNOTIFY
1facf9fc 9450+static inline void au_digen_dec(struct dentry *d)
9451+{
e49829fe 9452+ atomic_dec(&au_di(d)->di_generation);
1facf9fc 9453+}
9454+
4a4d8108 9455+static inline void au_hn_di_reinit(struct dentry *dentry)
1facf9fc 9456+{
9457+ dentry->d_fsdata = NULL;
9458+}
9459+#else
4a4d8108
AM
9460+AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9461+#endif /* CONFIG_AUFS_HNOTIFY */
1facf9fc 9462+
9463+#endif /* __KERNEL__ */
9464+#endif /* __AUFS_DENTRY_H__ */
7f207e10
AM
9465diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9466--- /usr/share/empty/fs/aufs/dinfo.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947 9467+++ linux/fs/aufs/dinfo.c 2017-11-12 22:24:42.267510077 +0100
e2f27e51 9468@@ -0,0 +1,553 @@
1facf9fc 9469+/*
a2654f78 9470+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 9471+ *
9472+ * This program, aufs is free software; you can redistribute it and/or modify
9473+ * it under the terms of the GNU General Public License as published by
9474+ * the Free Software Foundation; either version 2 of the License, or
9475+ * (at your option) any later version.
dece6358
AM
9476+ *
9477+ * This program is distributed in the hope that it will be useful,
9478+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9479+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9480+ * GNU General Public License for more details.
9481+ *
9482+ * You should have received a copy of the GNU General Public License
523b37e3 9483+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9484+ */
9485+
9486+/*
9487+ * dentry private data
9488+ */
9489+
9490+#include "aufs.h"
9491+
e49829fe 9492+void au_di_init_once(void *_dinfo)
4a4d8108 9493+{
e49829fe 9494+ struct au_dinfo *dinfo = _dinfo;
4a4d8108 9495+
e49829fe 9496+ au_rw_init(&dinfo->di_rwsem);
4a4d8108
AM
9497+}
9498+
027c5e7a 9499+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
1facf9fc 9500+{
9501+ struct au_dinfo *dinfo;
027c5e7a 9502+ int nbr, i;
1facf9fc 9503+
9504+ dinfo = au_cache_alloc_dinfo();
9505+ if (unlikely(!dinfo))
9506+ goto out;
9507+
5afbbe0d 9508+ nbr = au_sbbot(sb) + 1;
1facf9fc 9509+ if (nbr <= 0)
9510+ nbr = 1;
9511+ dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
027c5e7a
AM
9512+ if (dinfo->di_hdentry) {
9513+ au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
5afbbe0d
AM
9514+ dinfo->di_btop = -1;
9515+ dinfo->di_bbot = -1;
027c5e7a
AM
9516+ dinfo->di_bwh = -1;
9517+ dinfo->di_bdiropq = -1;
38d290e6 9518+ dinfo->di_tmpfile = 0;
027c5e7a
AM
9519+ for (i = 0; i < nbr; i++)
9520+ dinfo->di_hdentry[i].hd_id = -1;
9521+ goto out;
9522+ }
1facf9fc 9523+
1c60b727 9524+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9525+ dinfo = NULL;
9526+
4f0767ce 9527+out:
027c5e7a 9528+ return dinfo;
1facf9fc 9529+}
9530+
027c5e7a 9531+void au_di_free(struct au_dinfo *dinfo)
4a4d8108 9532+{
4a4d8108 9533+ struct au_hdentry *p;
5afbbe0d 9534+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
9535+
9536+ /* dentry may not be revalidated */
5afbbe0d 9537+ bindex = dinfo->di_btop;
4a4d8108 9538+ if (bindex >= 0) {
5afbbe0d
AM
9539+ bbot = dinfo->di_bbot;
9540+ p = au_hdentry(dinfo, bindex);
9541+ while (bindex++ <= bbot)
4a4d8108
AM
9542+ au_hdput(p++);
9543+ }
1c60b727
AM
9544+ kfree(dinfo->di_hdentry);
9545+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9546+}
9547+
9548+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9549+{
9550+ struct au_hdentry *p;
9551+ aufs_bindex_t bi;
9552+
9553+ AuRwMustWriteLock(&a->di_rwsem);
9554+ AuRwMustWriteLock(&b->di_rwsem);
9555+
9556+#define DiSwap(v, name) \
9557+ do { \
9558+ v = a->di_##name; \
9559+ a->di_##name = b->di_##name; \
9560+ b->di_##name = v; \
9561+ } while (0)
9562+
9563+ DiSwap(p, hdentry);
5afbbe0d
AM
9564+ DiSwap(bi, btop);
9565+ DiSwap(bi, bbot);
027c5e7a
AM
9566+ DiSwap(bi, bwh);
9567+ DiSwap(bi, bdiropq);
9568+ /* smp_mb(); */
9569+
9570+#undef DiSwap
9571+}
9572+
9573+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9574+{
9575+ AuRwMustWriteLock(&dst->di_rwsem);
9576+ AuRwMustWriteLock(&src->di_rwsem);
9577+
5afbbe0d
AM
9578+ dst->di_btop = src->di_btop;
9579+ dst->di_bbot = src->di_bbot;
027c5e7a
AM
9580+ dst->di_bwh = src->di_bwh;
9581+ dst->di_bdiropq = src->di_bdiropq;
9582+ /* smp_mb(); */
9583+}
9584+
9585+int au_di_init(struct dentry *dentry)
9586+{
9587+ int err;
9588+ struct super_block *sb;
9589+ struct au_dinfo *dinfo;
9590+
9591+ err = 0;
9592+ sb = dentry->d_sb;
9593+ dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9594+ if (dinfo) {
9595+ atomic_set(&dinfo->di_generation, au_sigen(sb));
9596+ /* smp_mb(); */ /* atomic_set */
9597+ dentry->d_fsdata = dinfo;
9598+ } else
9599+ err = -ENOMEM;
9600+
9601+ return err;
9602+}
9603+
9604+void au_di_fin(struct dentry *dentry)
9605+{
9606+ struct au_dinfo *dinfo;
9607+
9608+ dinfo = au_di(dentry);
9609+ AuRwDestroy(&dinfo->di_rwsem);
9610+ au_di_free(dinfo);
4a4d8108
AM
9611+}
9612+
e2f27e51 9613+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
1facf9fc 9614+{
9615+ int err, sz;
9616+ struct au_hdentry *hdp;
9617+
1308ab2a 9618+ AuRwMustWriteLock(&dinfo->di_rwsem);
9619+
1facf9fc 9620+ err = -ENOMEM;
5afbbe0d 9621+ sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
1facf9fc 9622+ if (!sz)
9623+ sz = sizeof(*hdp);
e2f27e51
AM
9624+ hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9625+ may_shrink);
1facf9fc 9626+ if (hdp) {
9627+ dinfo->di_hdentry = hdp;
9628+ err = 0;
9629+ }
9630+
9631+ return err;
9632+}
9633+
9634+/* ---------------------------------------------------------------------- */
9635+
9636+static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9637+{
9638+ switch (lsc) {
9639+ case AuLsc_DI_CHILD:
9640+ ii_write_lock_child(inode);
9641+ break;
9642+ case AuLsc_DI_CHILD2:
9643+ ii_write_lock_child2(inode);
9644+ break;
9645+ case AuLsc_DI_CHILD3:
9646+ ii_write_lock_child3(inode);
9647+ break;
9648+ case AuLsc_DI_PARENT:
9649+ ii_write_lock_parent(inode);
9650+ break;
9651+ case AuLsc_DI_PARENT2:
9652+ ii_write_lock_parent2(inode);
9653+ break;
9654+ case AuLsc_DI_PARENT3:
9655+ ii_write_lock_parent3(inode);
9656+ break;
9657+ default:
9658+ BUG();
9659+ }
9660+}
9661+
9662+static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9663+{
9664+ switch (lsc) {
9665+ case AuLsc_DI_CHILD:
9666+ ii_read_lock_child(inode);
9667+ break;
9668+ case AuLsc_DI_CHILD2:
9669+ ii_read_lock_child2(inode);
9670+ break;
9671+ case AuLsc_DI_CHILD3:
9672+ ii_read_lock_child3(inode);
9673+ break;
9674+ case AuLsc_DI_PARENT:
9675+ ii_read_lock_parent(inode);
9676+ break;
9677+ case AuLsc_DI_PARENT2:
9678+ ii_read_lock_parent2(inode);
9679+ break;
9680+ case AuLsc_DI_PARENT3:
9681+ ii_read_lock_parent3(inode);
9682+ break;
9683+ default:
9684+ BUG();
9685+ }
9686+}
9687+
9688+void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9689+{
5527c038
JR
9690+ struct inode *inode;
9691+
dece6358 9692+ au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9693+ if (d_really_is_positive(d)) {
9694+ inode = d_inode(d);
1facf9fc 9695+ if (au_ftest_lock(flags, IW))
5527c038 9696+ do_ii_write_lock(inode, lsc);
1facf9fc 9697+ else if (au_ftest_lock(flags, IR))
5527c038 9698+ do_ii_read_lock(inode, lsc);
1facf9fc 9699+ }
9700+}
9701+
9702+void di_read_unlock(struct dentry *d, int flags)
9703+{
5527c038
JR
9704+ struct inode *inode;
9705+
9706+ if (d_really_is_positive(d)) {
9707+ inode = d_inode(d);
027c5e7a
AM
9708+ if (au_ftest_lock(flags, IW)) {
9709+ au_dbg_verify_dinode(d);
5527c038 9710+ ii_write_unlock(inode);
027c5e7a
AM
9711+ } else if (au_ftest_lock(flags, IR)) {
9712+ au_dbg_verify_dinode(d);
5527c038 9713+ ii_read_unlock(inode);
027c5e7a 9714+ }
1facf9fc 9715+ }
dece6358 9716+ au_rw_read_unlock(&au_di(d)->di_rwsem);
1facf9fc 9717+}
9718+
9719+void di_downgrade_lock(struct dentry *d, int flags)
9720+{
5527c038
JR
9721+ if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9722+ ii_downgrade_lock(d_inode(d));
dece6358 9723+ au_rw_dgrade_lock(&au_di(d)->di_rwsem);
1facf9fc 9724+}
9725+
9726+void di_write_lock(struct dentry *d, unsigned int lsc)
9727+{
dece6358 9728+ au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9729+ if (d_really_is_positive(d))
9730+ do_ii_write_lock(d_inode(d), lsc);
1facf9fc 9731+}
9732+
9733+void di_write_unlock(struct dentry *d)
9734+{
027c5e7a 9735+ au_dbg_verify_dinode(d);
5527c038
JR
9736+ if (d_really_is_positive(d))
9737+ ii_write_unlock(d_inode(d));
dece6358 9738+ au_rw_write_unlock(&au_di(d)->di_rwsem);
1facf9fc 9739+}
9740+
9741+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9742+{
9743+ AuDebugOn(d1 == d2
5527c038 9744+ || d_inode(d1) == d_inode(d2)
1facf9fc 9745+ || d1->d_sb != d2->d_sb);
9746+
521ced18
JR
9747+ if ((isdir && au_test_subdir(d1, d2))
9748+ || d1 < d2) {
1facf9fc 9749+ di_write_lock_child(d1);
9750+ di_write_lock_child2(d2);
9751+ } else {
1facf9fc 9752+ di_write_lock_child(d2);
9753+ di_write_lock_child2(d1);
9754+ }
9755+}
9756+
9757+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9758+{
9759+ AuDebugOn(d1 == d2
5527c038 9760+ || d_inode(d1) == d_inode(d2)
1facf9fc 9761+ || d1->d_sb != d2->d_sb);
9762+
521ced18
JR
9763+ if ((isdir && au_test_subdir(d1, d2))
9764+ || d1 < d2) {
1facf9fc 9765+ di_write_lock_parent(d1);
9766+ di_write_lock_parent2(d2);
9767+ } else {
1facf9fc 9768+ di_write_lock_parent(d2);
9769+ di_write_lock_parent2(d1);
9770+ }
9771+}
9772+
9773+void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9774+{
9775+ di_write_unlock(d1);
5527c038 9776+ if (d_inode(d1) == d_inode(d2))
dece6358 9777+ au_rw_write_unlock(&au_di(d2)->di_rwsem);
1facf9fc 9778+ else
9779+ di_write_unlock(d2);
9780+}
9781+
9782+/* ---------------------------------------------------------------------- */
9783+
9784+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9785+{
9786+ struct dentry *d;
9787+
1308ab2a 9788+ DiMustAnyLock(dentry);
9789+
5afbbe0d 9790+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
1facf9fc 9791+ return NULL;
9792+ AuDebugOn(bindex < 0);
5afbbe0d 9793+ d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
c1595e42 9794+ AuDebugOn(d && au_dcount(d) <= 0);
1facf9fc 9795+ return d;
9796+}
9797+
2cbb1c4b
JR
9798+/*
9799+ * extended version of au_h_dptr().
38d290e6
JR
9800+ * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9801+ * error.
2cbb1c4b
JR
9802+ */
9803+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9804+{
9805+ struct dentry *h_dentry;
9806+ struct inode *inode, *h_inode;
9807+
5527c038 9808+ AuDebugOn(d_really_is_negative(dentry));
2cbb1c4b
JR
9809+
9810+ h_dentry = NULL;
5afbbe0d
AM
9811+ if (au_dbtop(dentry) <= bindex
9812+ && bindex <= au_dbbot(dentry))
2cbb1c4b 9813+ h_dentry = au_h_dptr(dentry, bindex);
38d290e6 9814+ if (h_dentry && !au_d_linkable(h_dentry)) {
2cbb1c4b
JR
9815+ dget(h_dentry);
9816+ goto out; /* success */
9817+ }
9818+
5527c038 9819+ inode = d_inode(dentry);
5afbbe0d
AM
9820+ AuDebugOn(bindex < au_ibtop(inode));
9821+ AuDebugOn(au_ibbot(inode) < bindex);
2cbb1c4b
JR
9822+ h_inode = au_h_iptr(inode, bindex);
9823+ h_dentry = d_find_alias(h_inode);
9824+ if (h_dentry) {
9825+ if (!IS_ERR(h_dentry)) {
38d290e6 9826+ if (!au_d_linkable(h_dentry))
2cbb1c4b
JR
9827+ goto out; /* success */
9828+ dput(h_dentry);
9829+ } else
9830+ goto out;
9831+ }
9832+
9833+ if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9834+ h_dentry = au_plink_lkup(inode, bindex);
9835+ AuDebugOn(!h_dentry);
9836+ if (!IS_ERR(h_dentry)) {
9837+ if (!au_d_hashed_positive(h_dentry))
9838+ goto out; /* success */
9839+ dput(h_dentry);
9840+ h_dentry = NULL;
9841+ }
9842+ }
9843+
9844+out:
9845+ AuDbgDentry(h_dentry);
9846+ return h_dentry;
9847+}
9848+
1facf9fc 9849+aufs_bindex_t au_dbtail(struct dentry *dentry)
9850+{
5afbbe0d 9851+ aufs_bindex_t bbot, bwh;
1facf9fc 9852+
5afbbe0d
AM
9853+ bbot = au_dbbot(dentry);
9854+ if (0 <= bbot) {
1facf9fc 9855+ bwh = au_dbwh(dentry);
9856+ if (!bwh)
9857+ return bwh;
5afbbe0d 9858+ if (0 < bwh && bwh < bbot)
1facf9fc 9859+ return bwh - 1;
9860+ }
5afbbe0d 9861+ return bbot;
1facf9fc 9862+}
9863+
9864+aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9865+{
5afbbe0d 9866+ aufs_bindex_t bbot, bopq;
1facf9fc 9867+
5afbbe0d
AM
9868+ bbot = au_dbtail(dentry);
9869+ if (0 <= bbot) {
1facf9fc 9870+ bopq = au_dbdiropq(dentry);
5afbbe0d
AM
9871+ if (0 <= bopq && bopq < bbot)
9872+ bbot = bopq;
1facf9fc 9873+ }
5afbbe0d 9874+ return bbot;
1facf9fc 9875+}
9876+
9877+/* ---------------------------------------------------------------------- */
9878+
9879+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9880+ struct dentry *h_dentry)
9881+{
5afbbe0d
AM
9882+ struct au_dinfo *dinfo;
9883+ struct au_hdentry *hd;
027c5e7a 9884+ struct au_branch *br;
1facf9fc 9885+
1308ab2a 9886+ DiMustWriteLock(dentry);
9887+
5afbbe0d
AM
9888+ dinfo = au_di(dentry);
9889+ hd = au_hdentry(dinfo, bindex);
4a4d8108 9890+ au_hdput(hd);
1facf9fc 9891+ hd->hd_dentry = h_dentry;
027c5e7a
AM
9892+ if (h_dentry) {
9893+ br = au_sbr(dentry->d_sb, bindex);
9894+ hd->hd_id = br->br_id;
9895+ }
9896+}
9897+
9898+int au_dbrange_test(struct dentry *dentry)
9899+{
9900+ int err;
5afbbe0d 9901+ aufs_bindex_t btop, bbot;
027c5e7a
AM
9902+
9903+ err = 0;
5afbbe0d
AM
9904+ btop = au_dbtop(dentry);
9905+ bbot = au_dbbot(dentry);
9906+ if (btop >= 0)
9907+ AuDebugOn(bbot < 0 && btop > bbot);
027c5e7a
AM
9908+ else {
9909+ err = -EIO;
5afbbe0d 9910+ AuDebugOn(bbot >= 0);
027c5e7a
AM
9911+ }
9912+
9913+ return err;
9914+}
9915+
9916+int au_digen_test(struct dentry *dentry, unsigned int sigen)
9917+{
9918+ int err;
9919+
9920+ err = 0;
9921+ if (unlikely(au_digen(dentry) != sigen
5527c038 9922+ || au_iigen_test(d_inode(dentry), sigen)))
027c5e7a
AM
9923+ err = -EIO;
9924+
9925+ return err;
1facf9fc 9926+}
9927+
9928+void au_update_digen(struct dentry *dentry)
9929+{
9930+ atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9931+ /* smp_mb(); */ /* atomic_set */
9932+}
9933+
9934+void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9935+{
9936+ struct au_dinfo *dinfo;
9937+ struct dentry *h_d;
4a4d8108 9938+ struct au_hdentry *hdp;
5afbbe0d 9939+ aufs_bindex_t bindex, bbot;
1facf9fc 9940+
1308ab2a 9941+ DiMustWriteLock(dentry);
9942+
1facf9fc 9943+ dinfo = au_di(dentry);
5afbbe0d 9944+ if (!dinfo || dinfo->di_btop < 0)
1facf9fc 9945+ return;
9946+
9947+ if (do_put_zero) {
5afbbe0d
AM
9948+ bbot = dinfo->di_bbot;
9949+ bindex = dinfo->di_btop;
9950+ hdp = au_hdentry(dinfo, bindex);
9951+ for (; bindex <= bbot; bindex++, hdp++) {
9952+ h_d = hdp->hd_dentry;
5527c038 9953+ if (h_d && d_is_negative(h_d))
1facf9fc 9954+ au_set_h_dptr(dentry, bindex, NULL);
9955+ }
9956+ }
9957+
5afbbe0d
AM
9958+ dinfo->di_btop = 0;
9959+ hdp = au_hdentry(dinfo, dinfo->di_btop);
9960+ for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9961+ if (hdp->hd_dentry)
1facf9fc 9962+ break;
5afbbe0d
AM
9963+ if (dinfo->di_btop > dinfo->di_bbot) {
9964+ dinfo->di_btop = -1;
9965+ dinfo->di_bbot = -1;
1facf9fc 9966+ return;
9967+ }
9968+
5afbbe0d
AM
9969+ hdp = au_hdentry(dinfo, dinfo->di_bbot);
9970+ for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9971+ if (hdp->hd_dentry)
1facf9fc 9972+ break;
5afbbe0d 9973+ AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
1facf9fc 9974+}
9975+
5afbbe0d 9976+void au_update_dbtop(struct dentry *dentry)
1facf9fc 9977+{
5afbbe0d 9978+ aufs_bindex_t bindex, bbot;
1facf9fc 9979+ struct dentry *h_dentry;
9980+
5afbbe0d
AM
9981+ bbot = au_dbbot(dentry);
9982+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
1facf9fc 9983+ h_dentry = au_h_dptr(dentry, bindex);
9984+ if (!h_dentry)
9985+ continue;
5527c038 9986+ if (d_is_positive(h_dentry)) {
5afbbe0d 9987+ au_set_dbtop(dentry, bindex);
1facf9fc 9988+ return;
9989+ }
9990+ au_set_h_dptr(dentry, bindex, NULL);
9991+ }
9992+}
9993+
5afbbe0d 9994+void au_update_dbbot(struct dentry *dentry)
1facf9fc 9995+{
5afbbe0d 9996+ aufs_bindex_t bindex, btop;
1facf9fc 9997+ struct dentry *h_dentry;
9998+
5afbbe0d
AM
9999+ btop = au_dbtop(dentry);
10000+ for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
1facf9fc 10001+ h_dentry = au_h_dptr(dentry, bindex);
10002+ if (!h_dentry)
10003+ continue;
5527c038 10004+ if (d_is_positive(h_dentry)) {
5afbbe0d 10005+ au_set_dbbot(dentry, bindex);
1facf9fc 10006+ return;
10007+ }
10008+ au_set_h_dptr(dentry, bindex, NULL);
10009+ }
10010+}
10011+
10012+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
10013+{
5afbbe0d 10014+ aufs_bindex_t bindex, bbot;
1facf9fc 10015+
5afbbe0d
AM
10016+ bbot = au_dbbot(dentry);
10017+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
1facf9fc 10018+ if (au_h_dptr(dentry, bindex) == h_dentry)
10019+ return bindex;
10020+ return -1;
10021+}
7f207e10
AM
10022diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
10023--- /usr/share/empty/fs/aufs/dir.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947 10024+++ linux/fs/aufs/dir.c 2017-11-12 22:24:44.704244405 +0100
1c60b727 10025@@ -0,0 +1,759 @@
1facf9fc 10026+/*
a2654f78 10027+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 10028+ *
10029+ * This program, aufs is free software; you can redistribute it and/or modify
10030+ * it under the terms of the GNU General Public License as published by
10031+ * the Free Software Foundation; either version 2 of the License, or
10032+ * (at your option) any later version.
dece6358
AM
10033+ *
10034+ * This program is distributed in the hope that it will be useful,
10035+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10036+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10037+ * GNU General Public License for more details.
10038+ *
10039+ * You should have received a copy of the GNU General Public License
523b37e3 10040+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10041+ */
10042+
10043+/*
10044+ * directory operations
10045+ */
10046+
10047+#include <linux/fs_stack.h>
10048+#include "aufs.h"
10049+
10050+void au_add_nlink(struct inode *dir, struct inode *h_dir)
10051+{
9dbd164d
AM
10052+ unsigned int nlink;
10053+
1facf9fc 10054+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10055+
9dbd164d
AM
10056+ nlink = dir->i_nlink;
10057+ nlink += h_dir->i_nlink - 2;
1facf9fc 10058+ if (h_dir->i_nlink < 2)
9dbd164d 10059+ nlink += 2;
f6b6e03d 10060+ smp_mb(); /* for i_nlink */
7eafdf33 10061+ /* 0 can happen in revaliding */
92d182d2 10062+ set_nlink(dir, nlink);
1facf9fc 10063+}
10064+
10065+void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10066+{
9dbd164d
AM
10067+ unsigned int nlink;
10068+
1facf9fc 10069+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10070+
9dbd164d
AM
10071+ nlink = dir->i_nlink;
10072+ nlink -= h_dir->i_nlink - 2;
1facf9fc 10073+ if (h_dir->i_nlink < 2)
9dbd164d 10074+ nlink -= 2;
f6b6e03d 10075+ smp_mb(); /* for i_nlink */
92d182d2 10076+ /* nlink == 0 means the branch-fs is broken */
9dbd164d 10077+ set_nlink(dir, nlink);
1facf9fc 10078+}
10079+
1308ab2a 10080+loff_t au_dir_size(struct file *file, struct dentry *dentry)
10081+{
10082+ loff_t sz;
5afbbe0d 10083+ aufs_bindex_t bindex, bbot;
1308ab2a 10084+ struct file *h_file;
10085+ struct dentry *h_dentry;
10086+
10087+ sz = 0;
10088+ if (file) {
2000de60 10089+ AuDebugOn(!d_is_dir(file->f_path.dentry));
1308ab2a 10090+
5afbbe0d
AM
10091+ bbot = au_fbbot_dir(file);
10092+ for (bindex = au_fbtop(file);
10093+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 10094+ bindex++) {
4a4d8108 10095+ h_file = au_hf_dir(file, bindex);
c06a8ce3
AM
10096+ if (h_file && file_inode(h_file))
10097+ sz += vfsub_f_size_read(h_file);
1308ab2a 10098+ }
10099+ } else {
10100+ AuDebugOn(!dentry);
2000de60 10101+ AuDebugOn(!d_is_dir(dentry));
1308ab2a 10102+
5afbbe0d
AM
10103+ bbot = au_dbtaildir(dentry);
10104+ for (bindex = au_dbtop(dentry);
10105+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 10106+ bindex++) {
10107+ h_dentry = au_h_dptr(dentry, bindex);
5527c038
JR
10108+ if (h_dentry && d_is_positive(h_dentry))
10109+ sz += i_size_read(d_inode(h_dentry));
1308ab2a 10110+ }
10111+ }
10112+ if (sz < KMALLOC_MAX_SIZE)
10113+ sz = roundup_pow_of_two(sz);
10114+ if (sz > KMALLOC_MAX_SIZE)
10115+ sz = KMALLOC_MAX_SIZE;
10116+ else if (sz < NAME_MAX) {
10117+ BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10118+ sz = AUFS_RDBLK_DEF;
10119+ }
10120+ return sz;
10121+}
10122+
b912730e
AM
10123+struct au_dir_ts_arg {
10124+ struct dentry *dentry;
10125+ aufs_bindex_t brid;
10126+};
10127+
10128+static void au_do_dir_ts(void *arg)
10129+{
10130+ struct au_dir_ts_arg *a = arg;
10131+ struct au_dtime dt;
10132+ struct path h_path;
10133+ struct inode *dir, *h_dir;
10134+ struct super_block *sb;
10135+ struct au_branch *br;
10136+ struct au_hinode *hdir;
10137+ int err;
5afbbe0d 10138+ aufs_bindex_t btop, bindex;
b912730e
AM
10139+
10140+ sb = a->dentry->d_sb;
5527c038 10141+ if (d_really_is_negative(a->dentry))
b912730e 10142+ goto out;
5527c038 10143+ /* no dir->i_mutex lock */
b95c5147
AM
10144+ aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10145+
5527c038 10146+ dir = d_inode(a->dentry);
5afbbe0d 10147+ btop = au_ibtop(dir);
b912730e 10148+ bindex = au_br_index(sb, a->brid);
5afbbe0d 10149+ if (bindex < btop)
b912730e
AM
10150+ goto out_unlock;
10151+
10152+ br = au_sbr(sb, bindex);
10153+ h_path.dentry = au_h_dptr(a->dentry, bindex);
10154+ if (!h_path.dentry)
10155+ goto out_unlock;
10156+ h_path.mnt = au_br_mnt(br);
10157+ au_dtime_store(&dt, a->dentry, &h_path);
10158+
5afbbe0d 10159+ br = au_sbr(sb, btop);
b912730e
AM
10160+ if (!au_br_writable(br->br_perm))
10161+ goto out_unlock;
5afbbe0d 10162+ h_path.dentry = au_h_dptr(a->dentry, btop);
b912730e
AM
10163+ h_path.mnt = au_br_mnt(br);
10164+ err = vfsub_mnt_want_write(h_path.mnt);
10165+ if (err)
10166+ goto out_unlock;
5afbbe0d
AM
10167+ hdir = au_hi(dir, btop);
10168+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10169+ h_dir = au_h_iptr(dir, btop);
b912730e
AM
10170+ if (h_dir->i_nlink
10171+ && timespec_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
10172+ dt.dt_h_path = h_path;
10173+ au_dtime_revert(&dt);
10174+ }
5afbbe0d 10175+ au_hn_inode_unlock(hdir);
b912730e
AM
10176+ vfsub_mnt_drop_write(h_path.mnt);
10177+ au_cpup_attr_timesizes(dir);
10178+
10179+out_unlock:
10180+ aufs_read_unlock(a->dentry, AuLock_DW);
10181+out:
10182+ dput(a->dentry);
10183+ au_nwt_done(&au_sbi(sb)->si_nowait);
1c60b727 10184+ kfree(arg);
b912730e
AM
10185+}
10186+
10187+void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10188+{
10189+ int perm, wkq_err;
5afbbe0d 10190+ aufs_bindex_t btop;
b912730e
AM
10191+ struct au_dir_ts_arg *arg;
10192+ struct dentry *dentry;
10193+ struct super_block *sb;
10194+
10195+ IMustLock(dir);
10196+
10197+ dentry = d_find_any_alias(dir);
10198+ AuDebugOn(!dentry);
10199+ sb = dentry->d_sb;
5afbbe0d
AM
10200+ btop = au_ibtop(dir);
10201+ if (btop == bindex) {
b912730e
AM
10202+ au_cpup_attr_timesizes(dir);
10203+ goto out;
10204+ }
10205+
5afbbe0d 10206+ perm = au_sbr_perm(sb, btop);
b912730e
AM
10207+ if (!au_br_writable(perm))
10208+ goto out;
10209+
10210+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
10211+ if (!arg)
10212+ goto out;
10213+
10214+ arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10215+ arg->brid = au_sbr_id(sb, bindex);
10216+ wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10217+ if (unlikely(wkq_err)) {
10218+ pr_err("wkq %d\n", wkq_err);
10219+ dput(dentry);
1c60b727 10220+ kfree(arg);
b912730e
AM
10221+ }
10222+
10223+out:
10224+ dput(dentry);
10225+}
10226+
1facf9fc 10227+/* ---------------------------------------------------------------------- */
10228+
10229+static int reopen_dir(struct file *file)
10230+{
10231+ int err;
10232+ unsigned int flags;
5afbbe0d 10233+ aufs_bindex_t bindex, btail, btop;
1facf9fc 10234+ struct dentry *dentry, *h_dentry;
10235+ struct file *h_file;
10236+
10237+ /* open all lower dirs */
2000de60 10238+ dentry = file->f_path.dentry;
5afbbe0d
AM
10239+ btop = au_dbtop(dentry);
10240+ for (bindex = au_fbtop(file); bindex < btop; bindex++)
1facf9fc 10241+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 10242+ au_set_fbtop(file, btop);
1facf9fc 10243+
10244+ btail = au_dbtaildir(dentry);
5afbbe0d 10245+ for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
1facf9fc 10246+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 10247+ au_set_fbbot_dir(file, btail);
1facf9fc 10248+
4a4d8108 10249+ flags = vfsub_file_flags(file);
5afbbe0d 10250+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 10251+ h_dentry = au_h_dptr(dentry, bindex);
10252+ if (!h_dentry)
10253+ continue;
4a4d8108 10254+ h_file = au_hf_dir(file, bindex);
1facf9fc 10255+ if (h_file)
10256+ continue;
10257+
392086de 10258+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 10259+ err = PTR_ERR(h_file);
10260+ if (IS_ERR(h_file))
10261+ goto out; /* close all? */
10262+ au_set_h_fptr(file, bindex, h_file);
10263+ }
10264+ au_update_figen(file);
10265+ /* todo: necessary? */
10266+ /* file->f_ra = h_file->f_ra; */
10267+ err = 0;
10268+
4f0767ce 10269+out:
1facf9fc 10270+ return err;
10271+}
10272+
b912730e 10273+static int do_open_dir(struct file *file, int flags, struct file *h_file)
1facf9fc 10274+{
10275+ int err;
10276+ aufs_bindex_t bindex, btail;
10277+ struct dentry *dentry, *h_dentry;
8cdd5066 10278+ struct vfsmount *mnt;
1facf9fc 10279+
1308ab2a 10280+ FiMustWriteLock(file);
b912730e 10281+ AuDebugOn(h_file);
1308ab2a 10282+
523b37e3 10283+ err = 0;
8cdd5066 10284+ mnt = file->f_path.mnt;
2000de60 10285+ dentry = file->f_path.dentry;
5527c038 10286+ file->f_version = d_inode(dentry)->i_version;
5afbbe0d
AM
10287+ bindex = au_dbtop(dentry);
10288+ au_set_fbtop(file, bindex);
1facf9fc 10289+ btail = au_dbtaildir(dentry);
5afbbe0d 10290+ au_set_fbbot_dir(file, btail);
1facf9fc 10291+ for (; !err && bindex <= btail; bindex++) {
10292+ h_dentry = au_h_dptr(dentry, bindex);
10293+ if (!h_dentry)
10294+ continue;
10295+
8cdd5066
JR
10296+ err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10297+ if (unlikely(err))
10298+ break;
392086de 10299+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 10300+ if (IS_ERR(h_file)) {
10301+ err = PTR_ERR(h_file);
10302+ break;
10303+ }
10304+ au_set_h_fptr(file, bindex, h_file);
10305+ }
10306+ au_update_figen(file);
10307+ /* todo: necessary? */
10308+ /* file->f_ra = h_file->f_ra; */
10309+ if (!err)
10310+ return 0; /* success */
10311+
10312+ /* close all */
5afbbe0d 10313+ for (bindex = au_fbtop(file); bindex <= btail; bindex++)
1facf9fc 10314+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d
AM
10315+ au_set_fbtop(file, -1);
10316+ au_set_fbbot_dir(file, -1);
4a4d8108 10317+
1facf9fc 10318+ return err;
10319+}
10320+
10321+static int aufs_open_dir(struct inode *inode __maybe_unused,
10322+ struct file *file)
10323+{
4a4d8108
AM
10324+ int err;
10325+ struct super_block *sb;
10326+ struct au_fidir *fidir;
10327+
10328+ err = -ENOMEM;
2000de60 10329+ sb = file->f_path.dentry->d_sb;
4a4d8108 10330+ si_read_lock(sb, AuLock_FLUSH);
e49829fe 10331+ fidir = au_fidir_alloc(sb);
4a4d8108 10332+ if (fidir) {
b912730e
AM
10333+ struct au_do_open_args args = {
10334+ .open = do_open_dir,
10335+ .fidir = fidir
10336+ };
10337+ err = au_do_open(file, &args);
4a4d8108 10338+ if (unlikely(err))
1c60b727 10339+ kfree(fidir);
4a4d8108
AM
10340+ }
10341+ si_read_unlock(sb);
10342+ return err;
1facf9fc 10343+}
10344+
10345+static int aufs_release_dir(struct inode *inode __maybe_unused,
10346+ struct file *file)
10347+{
10348+ struct au_vdir *vdir_cache;
4a4d8108
AM
10349+ struct au_finfo *finfo;
10350+ struct au_fidir *fidir;
f0c0a007 10351+ struct au_hfile *hf;
5afbbe0d 10352+ aufs_bindex_t bindex, bbot;
1facf9fc 10353+
4a4d8108
AM
10354+ finfo = au_fi(file);
10355+ fidir = finfo->fi_hdir;
10356+ if (fidir) {
8b6a4947
AM
10357+ au_hbl_del(&finfo->fi_hlist,
10358+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108
AM
10359+ vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10360+ if (vdir_cache)
1c60b727 10361+ au_vdir_free(vdir_cache);
4a4d8108
AM
10362+
10363+ bindex = finfo->fi_btop;
10364+ if (bindex >= 0) {
f0c0a007 10365+ hf = fidir->fd_hfile + bindex;
4a4d8108
AM
10366+ /*
10367+ * calls fput() instead of filp_close(),
10368+ * since no dnotify or lock for the lower file.
10369+ */
5afbbe0d 10370+ bbot = fidir->fd_bbot;
f0c0a007
AM
10371+ for (; bindex <= bbot; bindex++, hf++)
10372+ if (hf->hf_file)
1c60b727 10373+ au_hfput(hf, /*execed*/0);
4a4d8108 10374+ }
1c60b727 10375+ kfree(fidir);
4a4d8108 10376+ finfo->fi_hdir = NULL;
1facf9fc 10377+ }
1c60b727 10378+ au_finfo_fin(file);
1facf9fc 10379+ return 0;
10380+}
10381+
10382+/* ---------------------------------------------------------------------- */
10383+
4a4d8108
AM
10384+static int au_do_flush_dir(struct file *file, fl_owner_t id)
10385+{
10386+ int err;
5afbbe0d 10387+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
10388+ struct file *h_file;
10389+
10390+ err = 0;
5afbbe0d
AM
10391+ bbot = au_fbbot_dir(file);
10392+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
10393+ h_file = au_hf_dir(file, bindex);
10394+ if (h_file)
10395+ err = vfsub_flush(h_file, id);
10396+ }
10397+ return err;
10398+}
10399+
10400+static int aufs_flush_dir(struct file *file, fl_owner_t id)
10401+{
10402+ return au_do_flush(file, id, au_do_flush_dir);
10403+}
10404+
10405+/* ---------------------------------------------------------------------- */
10406+
1facf9fc 10407+static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10408+{
10409+ int err;
5afbbe0d 10410+ aufs_bindex_t bbot, bindex;
1facf9fc 10411+ struct inode *inode;
10412+ struct super_block *sb;
10413+
10414+ err = 0;
10415+ sb = dentry->d_sb;
5527c038 10416+ inode = d_inode(dentry);
1facf9fc 10417+ IMustLock(inode);
5afbbe0d
AM
10418+ bbot = au_dbbot(dentry);
10419+ for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
1facf9fc 10420+ struct path h_path;
1facf9fc 10421+
10422+ if (au_test_ro(sb, bindex, inode))
10423+ continue;
10424+ h_path.dentry = au_h_dptr(dentry, bindex);
10425+ if (!h_path.dentry)
10426+ continue;
1facf9fc 10427+
1facf9fc 10428+ h_path.mnt = au_sbr_mnt(sb, bindex);
53392da6 10429+ err = vfsub_fsync(NULL, &h_path, datasync);
1facf9fc 10430+ }
10431+
10432+ return err;
10433+}
10434+
10435+static int au_do_fsync_dir(struct file *file, int datasync)
10436+{
10437+ int err;
5afbbe0d 10438+ aufs_bindex_t bbot, bindex;
1facf9fc 10439+ struct file *h_file;
10440+ struct super_block *sb;
10441+ struct inode *inode;
1facf9fc 10442+
521ced18 10443+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
1facf9fc 10444+ if (unlikely(err))
10445+ goto out;
10446+
c06a8ce3 10447+ inode = file_inode(file);
b912730e 10448+ sb = inode->i_sb;
5afbbe0d
AM
10449+ bbot = au_fbbot_dir(file);
10450+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108 10451+ h_file = au_hf_dir(file, bindex);
1facf9fc 10452+ if (!h_file || au_test_ro(sb, bindex, inode))
10453+ continue;
10454+
53392da6 10455+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
1facf9fc 10456+ }
10457+
4f0767ce 10458+out:
1facf9fc 10459+ return err;
10460+}
10461+
10462+/*
10463+ * @file may be NULL
10464+ */
1e00d052
AM
10465+static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10466+ int datasync)
1facf9fc 10467+{
10468+ int err;
b752ccd1 10469+ struct dentry *dentry;
5527c038 10470+ struct inode *inode;
1facf9fc 10471+ struct super_block *sb;
1facf9fc 10472+
10473+ err = 0;
2000de60 10474+ dentry = file->f_path.dentry;
5527c038 10475+ inode = d_inode(dentry);
febd17d6 10476+ inode_lock(inode);
1facf9fc 10477+ sb = dentry->d_sb;
10478+ si_noflush_read_lock(sb);
10479+ if (file)
10480+ err = au_do_fsync_dir(file, datasync);
10481+ else {
10482+ di_write_lock_child(dentry);
10483+ err = au_do_fsync_dir_no_file(dentry, datasync);
10484+ }
5527c038 10485+ au_cpup_attr_timesizes(inode);
1facf9fc 10486+ di_write_unlock(dentry);
10487+ if (file)
10488+ fi_write_unlock(file);
10489+
10490+ si_read_unlock(sb);
febd17d6 10491+ inode_unlock(inode);
1facf9fc 10492+ return err;
10493+}
10494+
10495+/* ---------------------------------------------------------------------- */
10496+
5afbbe0d 10497+static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
1facf9fc 10498+{
10499+ int err;
10500+ struct dentry *dentry;
9dbd164d 10501+ struct inode *inode, *h_inode;
1facf9fc 10502+ struct super_block *sb;
10503+
523b37e3 10504+ AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
392086de 10505+
2000de60 10506+ dentry = file->f_path.dentry;
5527c038 10507+ inode = d_inode(dentry);
1facf9fc 10508+ IMustLock(inode);
10509+
10510+ sb = dentry->d_sb;
10511+ si_read_lock(sb, AuLock_FLUSH);
521ced18 10512+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
1facf9fc 10513+ if (unlikely(err))
10514+ goto out;
027c5e7a
AM
10515+ err = au_alive_dir(dentry);
10516+ if (!err)
10517+ err = au_vdir_init(file);
1facf9fc 10518+ di_downgrade_lock(dentry, AuLock_IR);
10519+ if (unlikely(err))
10520+ goto out_unlock;
10521+
5afbbe0d 10522+ h_inode = au_h_iptr(inode, au_ibtop(inode));
b752ccd1 10523+ if (!au_test_nfsd()) {
392086de 10524+ err = au_vdir_fill_de(file, ctx);
9dbd164d 10525+ fsstack_copy_attr_atime(inode, h_inode);
1facf9fc 10526+ } else {
10527+ /*
10528+ * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10529+ * encode_fh() and others.
10530+ */
9dbd164d 10531+ atomic_inc(&h_inode->i_count);
1facf9fc 10532+ di_read_unlock(dentry, AuLock_IR);
10533+ si_read_unlock(sb);
392086de 10534+ err = au_vdir_fill_de(file, ctx);
1facf9fc 10535+ fsstack_copy_attr_atime(inode, h_inode);
10536+ fi_write_unlock(file);
9dbd164d 10537+ iput(h_inode);
1facf9fc 10538+
10539+ AuTraceErr(err);
10540+ return err;
10541+ }
10542+
4f0767ce 10543+out_unlock:
1facf9fc 10544+ di_read_unlock(dentry, AuLock_IR);
10545+ fi_write_unlock(file);
4f0767ce 10546+out:
1facf9fc 10547+ si_read_unlock(sb);
10548+ return err;
10549+}
10550+
10551+/* ---------------------------------------------------------------------- */
10552+
10553+#define AuTestEmpty_WHONLY 1
dece6358
AM
10554+#define AuTestEmpty_CALLED (1 << 1)
10555+#define AuTestEmpty_SHWH (1 << 2)
1facf9fc 10556+#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
7f207e10
AM
10557+#define au_fset_testempty(flags, name) \
10558+ do { (flags) |= AuTestEmpty_##name; } while (0)
10559+#define au_fclr_testempty(flags, name) \
10560+ do { (flags) &= ~AuTestEmpty_##name; } while (0)
1facf9fc 10561+
dece6358
AM
10562+#ifndef CONFIG_AUFS_SHWH
10563+#undef AuTestEmpty_SHWH
10564+#define AuTestEmpty_SHWH 0
10565+#endif
10566+
1facf9fc 10567+struct test_empty_arg {
392086de 10568+ struct dir_context ctx;
1308ab2a 10569+ struct au_nhash *whlist;
1facf9fc 10570+ unsigned int flags;
10571+ int err;
10572+ aufs_bindex_t bindex;
10573+};
10574+
392086de
AM
10575+static int test_empty_cb(struct dir_context *ctx, const char *__name,
10576+ int namelen, loff_t offset __maybe_unused, u64 ino,
dece6358 10577+ unsigned int d_type)
1facf9fc 10578+{
392086de
AM
10579+ struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10580+ ctx);
1facf9fc 10581+ char *name = (void *)__name;
10582+
10583+ arg->err = 0;
10584+ au_fset_testempty(arg->flags, CALLED);
10585+ /* smp_mb(); */
10586+ if (name[0] == '.'
10587+ && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10588+ goto out; /* success */
10589+
10590+ if (namelen <= AUFS_WH_PFX_LEN
10591+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10592+ if (au_ftest_testempty(arg->flags, WHONLY)
1308ab2a 10593+ && !au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10594+ arg->err = -ENOTEMPTY;
10595+ goto out;
10596+ }
10597+
10598+ name += AUFS_WH_PFX_LEN;
10599+ namelen -= AUFS_WH_PFX_LEN;
1308ab2a 10600+ if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10601+ arg->err = au_nhash_append_wh
1308ab2a 10602+ (arg->whlist, name, namelen, ino, d_type, arg->bindex,
dece6358 10603+ au_ftest_testempty(arg->flags, SHWH));
1facf9fc 10604+
4f0767ce 10605+out:
1facf9fc 10606+ /* smp_mb(); */
10607+ AuTraceErr(arg->err);
10608+ return arg->err;
10609+}
10610+
10611+static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10612+{
10613+ int err;
10614+ struct file *h_file;
10615+
10616+ h_file = au_h_open(dentry, arg->bindex,
10617+ O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
392086de 10618+ /*file*/NULL, /*force_wr*/0);
1facf9fc 10619+ err = PTR_ERR(h_file);
10620+ if (IS_ERR(h_file))
10621+ goto out;
10622+
10623+ err = 0;
10624+ if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
c06a8ce3 10625+ && !file_inode(h_file)->i_nlink)
1facf9fc 10626+ goto out_put;
10627+
10628+ do {
10629+ arg->err = 0;
10630+ au_fclr_testempty(arg->flags, CALLED);
10631+ /* smp_mb(); */
392086de 10632+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1facf9fc 10633+ if (err >= 0)
10634+ err = arg->err;
10635+ } while (!err && au_ftest_testempty(arg->flags, CALLED));
10636+
4f0767ce 10637+out_put:
1facf9fc 10638+ fput(h_file);
10639+ au_sbr_put(dentry->d_sb, arg->bindex);
4f0767ce 10640+out:
1facf9fc 10641+ return err;
10642+}
10643+
10644+struct do_test_empty_args {
10645+ int *errp;
10646+ struct dentry *dentry;
10647+ struct test_empty_arg *arg;
10648+};
10649+
10650+static void call_do_test_empty(void *args)
10651+{
10652+ struct do_test_empty_args *a = args;
10653+ *a->errp = do_test_empty(a->dentry, a->arg);
10654+}
10655+
10656+static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10657+{
10658+ int err, wkq_err;
10659+ struct dentry *h_dentry;
10660+ struct inode *h_inode;
10661+
10662+ h_dentry = au_h_dptr(dentry, arg->bindex);
5527c038 10663+ h_inode = d_inode(h_dentry);
53392da6 10664+ /* todo: i_mode changes anytime? */
3c1bdaff 10665+ vfsub_inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 10666+ err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
3c1bdaff 10667+ inode_unlock_shared(h_inode);
1facf9fc 10668+ if (!err)
10669+ err = do_test_empty(dentry, arg);
10670+ else {
10671+ struct do_test_empty_args args = {
10672+ .errp = &err,
10673+ .dentry = dentry,
10674+ .arg = arg
10675+ };
10676+ unsigned int flags = arg->flags;
10677+
10678+ wkq_err = au_wkq_wait(call_do_test_empty, &args);
10679+ if (unlikely(wkq_err))
10680+ err = wkq_err;
10681+ arg->flags = flags;
10682+ }
10683+
10684+ return err;
10685+}
10686+
10687+int au_test_empty_lower(struct dentry *dentry)
10688+{
10689+ int err;
1308ab2a 10690+ unsigned int rdhash;
5afbbe0d 10691+ aufs_bindex_t bindex, btop, btail;
1308ab2a 10692+ struct au_nhash whlist;
392086de
AM
10693+ struct test_empty_arg arg = {
10694+ .ctx = {
2000de60 10695+ .actor = test_empty_cb
392086de
AM
10696+ }
10697+ };
076b876e 10698+ int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
1facf9fc 10699+
dece6358
AM
10700+ SiMustAnyLock(dentry->d_sb);
10701+
1308ab2a 10702+ rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10703+ if (!rdhash)
10704+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10705+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
dece6358 10706+ if (unlikely(err))
1facf9fc 10707+ goto out;
10708+
1facf9fc 10709+ arg.flags = 0;
1308ab2a 10710+ arg.whlist = &whlist;
5afbbe0d 10711+ btop = au_dbtop(dentry);
dece6358
AM
10712+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10713+ au_fset_testempty(arg.flags, SHWH);
076b876e
AM
10714+ test_empty = do_test_empty;
10715+ if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10716+ test_empty = sio_test_empty;
5afbbe0d 10717+ arg.bindex = btop;
076b876e 10718+ err = test_empty(dentry, &arg);
1facf9fc 10719+ if (unlikely(err))
10720+ goto out_whlist;
10721+
10722+ au_fset_testempty(arg.flags, WHONLY);
10723+ btail = au_dbtaildir(dentry);
5afbbe0d 10724+ for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
1facf9fc 10725+ struct dentry *h_dentry;
10726+
10727+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10728+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10729+ arg.bindex = bindex;
076b876e 10730+ err = test_empty(dentry, &arg);
1facf9fc 10731+ }
10732+ }
10733+
4f0767ce 10734+out_whlist:
1308ab2a 10735+ au_nhash_wh_free(&whlist);
4f0767ce 10736+out:
1facf9fc 10737+ return err;
10738+}
10739+
10740+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10741+{
10742+ int err;
392086de
AM
10743+ struct test_empty_arg arg = {
10744+ .ctx = {
2000de60 10745+ .actor = test_empty_cb
392086de
AM
10746+ }
10747+ };
1facf9fc 10748+ aufs_bindex_t bindex, btail;
10749+
10750+ err = 0;
1308ab2a 10751+ arg.whlist = whlist;
1facf9fc 10752+ arg.flags = AuTestEmpty_WHONLY;
dece6358
AM
10753+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10754+ au_fset_testempty(arg.flags, SHWH);
1facf9fc 10755+ btail = au_dbtaildir(dentry);
5afbbe0d 10756+ for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
1facf9fc 10757+ struct dentry *h_dentry;
10758+
10759+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10760+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10761+ arg.bindex = bindex;
10762+ err = sio_test_empty(dentry, &arg);
10763+ }
10764+ }
10765+
10766+ return err;
10767+}
10768+
10769+/* ---------------------------------------------------------------------- */
10770+
10771+const struct file_operations aufs_dir_fop = {
4a4d8108 10772+ .owner = THIS_MODULE,
027c5e7a 10773+ .llseek = default_llseek,
1facf9fc 10774+ .read = generic_read_dir,
5afbbe0d 10775+ .iterate_shared = aufs_iterate_shared,
1facf9fc 10776+ .unlocked_ioctl = aufs_ioctl_dir,
b752ccd1
AM
10777+#ifdef CONFIG_COMPAT
10778+ .compat_ioctl = aufs_compat_ioctl_dir,
10779+#endif
1facf9fc 10780+ .open = aufs_open_dir,
10781+ .release = aufs_release_dir,
4a4d8108 10782+ .flush = aufs_flush_dir,
1facf9fc 10783+ .fsync = aufs_fsync_dir
10784+};
7f207e10
AM
10785diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10786--- /usr/share/empty/fs/aufs/dir.h 1970-01-01 01:00:00.000000000 +0100
8b6a4947 10787+++ linux/fs/aufs/dir.h 2017-11-12 22:24:42.267510077 +0100
1c60b727 10788@@ -0,0 +1,131 @@
1facf9fc 10789+/*
a2654f78 10790+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 10791+ *
10792+ * This program, aufs is free software; you can redistribute it and/or modify
10793+ * it under the terms of the GNU General Public License as published by
10794+ * the Free Software Foundation; either version 2 of the License, or
10795+ * (at your option) any later version.
dece6358
AM
10796+ *
10797+ * This program is distributed in the hope that it will be useful,
10798+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10799+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10800+ * GNU General Public License for more details.
10801+ *
10802+ * You should have received a copy of the GNU General Public License
523b37e3 10803+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10804+ */
10805+
10806+/*
10807+ * directory operations
10808+ */
10809+
10810+#ifndef __AUFS_DIR_H__
10811+#define __AUFS_DIR_H__
10812+
10813+#ifdef __KERNEL__
10814+
10815+#include <linux/fs.h>
1facf9fc 10816+
10817+/* ---------------------------------------------------------------------- */
10818+
10819+/* need to be faster and smaller */
10820+
10821+struct au_nhash {
dece6358
AM
10822+ unsigned int nh_num;
10823+ struct hlist_head *nh_head;
1facf9fc 10824+};
10825+
10826+struct au_vdir_destr {
10827+ unsigned char len;
10828+ unsigned char name[0];
10829+} __packed;
10830+
10831+struct au_vdir_dehstr {
10832+ struct hlist_node hash;
1c60b727 10833+ struct au_vdir_destr *str;
4a4d8108 10834+} ____cacheline_aligned_in_smp;
1facf9fc 10835+
10836+struct au_vdir_de {
10837+ ino_t de_ino;
10838+ unsigned char de_type;
10839+ /* caution: packed */
10840+ struct au_vdir_destr de_str;
10841+} __packed;
10842+
10843+struct au_vdir_wh {
10844+ struct hlist_node wh_hash;
dece6358
AM
10845+#ifdef CONFIG_AUFS_SHWH
10846+ ino_t wh_ino;
1facf9fc 10847+ aufs_bindex_t wh_bindex;
dece6358
AM
10848+ unsigned char wh_type;
10849+#else
10850+ aufs_bindex_t wh_bindex;
10851+#endif
10852+ /* caution: packed */
1facf9fc 10853+ struct au_vdir_destr wh_str;
10854+} __packed;
10855+
10856+union au_vdir_deblk_p {
10857+ unsigned char *deblk;
10858+ struct au_vdir_de *de;
10859+};
10860+
10861+struct au_vdir {
10862+ unsigned char **vd_deblk;
10863+ unsigned long vd_nblk;
1facf9fc 10864+ struct {
10865+ unsigned long ul;
10866+ union au_vdir_deblk_p p;
10867+ } vd_last;
10868+
10869+ unsigned long vd_version;
dece6358 10870+ unsigned int vd_deblk_sz;
1c60b727 10871+ unsigned long vd_jiffy;
4a4d8108 10872+} ____cacheline_aligned_in_smp;
1facf9fc 10873+
10874+/* ---------------------------------------------------------------------- */
10875+
10876+/* dir.c */
10877+extern const struct file_operations aufs_dir_fop;
10878+void au_add_nlink(struct inode *dir, struct inode *h_dir);
10879+void au_sub_nlink(struct inode *dir, struct inode *h_dir);
1308ab2a 10880+loff_t au_dir_size(struct file *file, struct dentry *dentry);
b912730e 10881+void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
1facf9fc 10882+int au_test_empty_lower(struct dentry *dentry);
10883+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10884+
10885+/* vdir.c */
1308ab2a 10886+unsigned int au_rdhash_est(loff_t sz);
dece6358
AM
10887+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10888+void au_nhash_wh_free(struct au_nhash *whlist);
1facf9fc 10889+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10890+ int limit);
dece6358
AM
10891+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10892+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10893+ unsigned int d_type, aufs_bindex_t bindex,
10894+ unsigned char shwh);
1c60b727 10895+void au_vdir_free(struct au_vdir *vdir);
1facf9fc 10896+int au_vdir_init(struct file *file);
392086de 10897+int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
1facf9fc 10898+
10899+/* ioctl.c */
10900+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10901+
1308ab2a 10902+#ifdef CONFIG_AUFS_RDU
10903+/* rdu.c */
10904+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
10905+#ifdef CONFIG_COMPAT
10906+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10907+ unsigned long arg);
10908+#endif
1308ab2a 10909+#else
c1595e42
JR
10910+AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10911+ unsigned int cmd, unsigned long arg)
b752ccd1 10912+#ifdef CONFIG_COMPAT
c1595e42
JR
10913+AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10914+ unsigned int cmd, unsigned long arg)
b752ccd1 10915+#endif
1308ab2a 10916+#endif
10917+
1facf9fc 10918+#endif /* __KERNEL__ */
10919+#endif /* __AUFS_DIR_H__ */
8b6a4947
AM
10920diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
10921--- /usr/share/empty/fs/aufs/dirren.c 1970-01-01 01:00:00.000000000 +0100
10922+++ linux/fs/aufs/dirren.c 2017-11-12 22:24:44.704244405 +0100
10923@@ -0,0 +1,1315 @@
10924+/*
10925+ * Copyright (C) 2017 Junjiro R. Okajima
10926+ *
10927+ * This program, aufs is free software; you can redistribute it and/or modify
10928+ * it under the terms of the GNU General Public License as published by
10929+ * the Free Software Foundation; either version 2 of the License, or
10930+ * (at your option) any later version.
10931+ *
10932+ * This program is distributed in the hope that it will be useful,
10933+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10934+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10935+ * GNU General Public License for more details.
10936+ *
10937+ * You should have received a copy of the GNU General Public License
10938+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
10939+ */
10940+
10941+/*
10942+ * special handling in renaming a directoy
10943+ * in order to support looking-up the before-renamed name on the lower readonly
10944+ * branches
10945+ */
10946+
10947+#include <linux/byteorder/generic.h>
10948+#include "aufs.h"
10949+
10950+static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10951+{
10952+ int idx;
10953+
10954+ idx = au_dr_ihash(ent->dr_h_ino);
10955+ au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10956+}
10957+
10958+static int au_dr_hino_test_empty(struct au_dr_br *dr)
10959+{
10960+ int ret, i;
10961+ struct hlist_bl_head *hbl;
10962+
10963+ ret = 1;
10964+ for (i = 0; ret && i < AuDirren_NHASH; i++) {
10965+ hbl = dr->dr_h_ino + i;
10966+ hlist_bl_lock(hbl);
10967+ ret &= hlist_bl_empty(hbl);
10968+ hlist_bl_unlock(hbl);
10969+ }
10970+
10971+ return ret;
10972+}
10973+
10974+static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
10975+{
10976+ struct au_dr_hino *found, *ent;
10977+ struct hlist_bl_head *hbl;
10978+ struct hlist_bl_node *pos;
10979+ int idx;
10980+
10981+ found = NULL;
10982+ idx = au_dr_ihash(ino);
10983+ hbl = dr->dr_h_ino + idx;
10984+ hlist_bl_lock(hbl);
10985+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10986+ if (ent->dr_h_ino == ino) {
10987+ found = ent;
10988+ break;
10989+ }
10990+ hlist_bl_unlock(hbl);
10991+
10992+ return found;
10993+}
10994+
10995+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
10996+ struct au_dr_hino *add_ent)
10997+{
10998+ int found, idx;
10999+ struct hlist_bl_head *hbl;
11000+ struct hlist_bl_node *pos;
11001+ struct au_dr_hino *ent;
11002+
11003+ found = 0;
11004+ idx = au_dr_ihash(ino);
11005+ hbl = dr->dr_h_ino + idx;
11006+#if 0
11007+ {
11008+ struct hlist_bl_node *tmp;
11009+
11010+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11011+ AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
11012+ }
11013+#endif
11014+ hlist_bl_lock(hbl);
11015+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11016+ if (ent->dr_h_ino == ino) {
11017+ found = 1;
11018+ break;
11019+ }
11020+ if (!found && add_ent)
11021+ hlist_bl_add_head(&add_ent->dr_hnode, hbl);
11022+ hlist_bl_unlock(hbl);
11023+
11024+ if (!found && add_ent)
11025+ AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
11026+
11027+ return found;
11028+}
11029+
11030+void au_dr_hino_free(struct au_dr_br *dr)
11031+{
11032+ int i;
11033+ struct hlist_bl_head *hbl;
11034+ struct hlist_bl_node *pos, *tmp;
11035+ struct au_dr_hino *ent;
11036+
11037+ /* SiMustWriteLock(sb); */
11038+
11039+ for (i = 0; i < AuDirren_NHASH; i++) {
11040+ hbl = dr->dr_h_ino + i;
11041+ /* no spinlock since sbinfo must be write-locked */
11042+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11043+ kfree(ent);
11044+ INIT_HLIST_BL_HEAD(hbl);
11045+ }
11046+}
11047+
11048+/* returns the number of inodes or an error */
11049+static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
11050+ struct file *hinofile)
11051+{
11052+ int err, i;
11053+ ssize_t ssz;
11054+ loff_t pos, oldsize;
11055+ __be64 u64;
11056+ struct inode *hinoinode;
11057+ struct hlist_bl_head *hbl;
11058+ struct hlist_bl_node *n1, *n2;
11059+ struct au_dr_hino *ent;
11060+
11061+ SiMustWriteLock(sb);
11062+ AuDebugOn(!au_br_writable(br->br_perm));
11063+
11064+ hinoinode = file_inode(hinofile);
11065+ oldsize = i_size_read(hinoinode);
11066+
11067+ err = 0;
11068+ pos = 0;
11069+ hbl = br->br_dirren.dr_h_ino;
11070+ for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11071+ /* no bit-lock since sbinfo must be write-locked */
11072+ hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11073+ AuDbg("hi%llu, %pD2\n",
11074+ (unsigned long long)ent->dr_h_ino, hinofile);
11075+ u64 = cpu_to_be64(ent->dr_h_ino);
11076+ ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11077+ if (ssz == sizeof(u64))
11078+ continue;
11079+
11080+ /* write error */
11081+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11082+ err = -ENOSPC;
11083+ if (ssz < 0)
11084+ err = ssz;
11085+ break;
11086+ }
11087+ }
11088+ /* regardless the error */
11089+ if (pos < oldsize) {
11090+ err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11091+ AuTraceErr(err);
11092+ }
11093+
11094+ AuTraceErr(err);
11095+ return err;
11096+}
11097+
11098+static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11099+{
11100+ int err, hidx;
11101+ ssize_t ssz;
11102+ size_t sz, n;
11103+ loff_t pos;
11104+ uint64_t u64;
11105+ struct au_dr_hino *ent;
11106+ struct inode *hinoinode;
11107+ struct hlist_bl_head *hbl;
11108+
11109+ err = 0;
11110+ pos = 0;
11111+ hbl = dr->dr_h_ino;
11112+ hinoinode = file_inode(hinofile);
11113+ sz = i_size_read(hinoinode);
11114+ AuDebugOn(sz % sizeof(u64));
11115+ n = sz / sizeof(u64);
11116+ while (n--) {
11117+ ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11118+ if (unlikely(ssz != sizeof(u64))) {
11119+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11120+ err = -EINVAL;
11121+ if (ssz < 0)
11122+ err = ssz;
11123+ goto out_free;
11124+ }
11125+
11126+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11127+ if (!ent) {
11128+ err = -ENOMEM;
11129+ AuTraceErr(err);
11130+ goto out_free;
11131+ }
11132+ ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11133+ AuDbg("hi%llu, %pD2\n",
11134+ (unsigned long long)ent->dr_h_ino, hinofile);
11135+ hidx = au_dr_ihash(ent->dr_h_ino);
11136+ au_hbl_add(&ent->dr_hnode, hbl + hidx);
11137+ }
11138+ goto out; /* success */
11139+
11140+out_free:
11141+ au_dr_hino_free(dr);
11142+out:
11143+ AuTraceErr(err);
11144+ return err;
11145+}
11146+
11147+/*
11148+ * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11149+ * @path is a switch to distinguish load and store.
11150+ */
11151+static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11152+ struct au_branch *br, const struct path *path)
11153+{
11154+ int err, flags;
11155+ unsigned char load, suspend;
11156+ struct file *hinofile;
11157+ struct au_hinode *hdir;
11158+ struct inode *dir, *delegated;
11159+ struct path hinopath;
11160+ struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11161+ sizeof(AUFS_WH_DR_BRHINO) - 1);
11162+
11163+ AuDebugOn(bindex < 0 && !br);
11164+ AuDebugOn(bindex >= 0 && br);
11165+
11166+ err = -EINVAL;
11167+ suspend = !br;
11168+ if (suspend)
11169+ br = au_sbr(sb, bindex);
11170+ load = !!path;
11171+ if (!load) {
11172+ path = &br->br_path;
11173+ AuDebugOn(!au_br_writable(br->br_perm));
11174+ if (unlikely(!au_br_writable(br->br_perm)))
11175+ goto out;
11176+ }
11177+
11178+ hdir = NULL;
11179+ if (suspend) {
11180+ dir = d_inode(sb->s_root);
11181+ hdir = au_hinode(au_ii(dir), bindex);
11182+ dir = hdir->hi_inode;
11183+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11184+ } else {
11185+ dir = d_inode(path->dentry);
11186+ inode_lock_nested(dir, AuLsc_I_CHILD);
11187+ }
11188+ hinopath.dentry = vfsub_lkup_one(&hinoname, path->dentry);
11189+ err = PTR_ERR(hinopath.dentry);
11190+ if (IS_ERR(hinopath.dentry))
11191+ goto out_unlock;
11192+
11193+ err = 0;
11194+ flags = O_RDONLY;
11195+ if (load) {
11196+ if (d_is_negative(hinopath.dentry))
11197+ goto out_dput; /* success */
11198+ } else {
11199+ if (au_dr_hino_test_empty(&br->br_dirren)) {
11200+ if (d_is_positive(hinopath.dentry)) {
11201+ delegated = NULL;
11202+ err = vfsub_unlink(dir, &hinopath, &delegated,
11203+ /*force*/0);
11204+ AuTraceErr(err);
11205+ if (unlikely(err))
11206+ pr_err("ignored err %d, %pd2\n",
11207+ err, hinopath.dentry);
11208+ if (unlikely(err == -EWOULDBLOCK))
11209+ iput(delegated);
11210+ err = 0;
11211+ }
11212+ goto out_dput;
11213+ } else if (!d_is_positive(hinopath.dentry)) {
11214+ err = vfsub_create(dir, &hinopath, 0600,
11215+ /*want_excl*/false);
11216+ AuTraceErr(err);
11217+ if (unlikely(err))
11218+ goto out_dput;
11219+ }
11220+ flags = O_WRONLY;
11221+ }
11222+ hinopath.mnt = path->mnt;
11223+ hinofile = vfsub_dentry_open(&hinopath, flags);
11224+ if (suspend)
11225+ au_hn_inode_unlock(hdir);
11226+ else
11227+ inode_unlock(dir);
11228+ dput(hinopath.dentry);
11229+ AuTraceErrPtr(hinofile);
11230+ if (IS_ERR(hinofile)) {
11231+ err = PTR_ERR(hinofile);
11232+ goto out;
11233+ }
11234+
11235+ if (load)
11236+ err = au_dr_hino_load(&br->br_dirren, hinofile);
11237+ else
11238+ err = au_dr_hino_store(sb, br, hinofile);
11239+ fput(hinofile);
11240+ goto out;
11241+
11242+out_dput:
11243+ dput(hinopath.dentry);
11244+out_unlock:
11245+ if (suspend)
11246+ au_hn_inode_unlock(hdir);
11247+ else
11248+ inode_unlock(dir);
11249+out:
11250+ AuTraceErr(err);
11251+ return err;
11252+}
11253+
11254+/* ---------------------------------------------------------------------- */
11255+
11256+static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11257+{
11258+ int err;
11259+ struct kstatfs kstfs;
11260+ dev_t dev;
11261+ struct dentry *dentry;
11262+ struct super_block *sb;
11263+
11264+ err = vfs_statfs((void *)path, &kstfs);
11265+ AuTraceErr(err);
11266+ if (unlikely(err))
11267+ goto out;
11268+
11269+ /* todo: support for UUID */
11270+
11271+ if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11272+ brid->type = AuBrid_FSID;
11273+ brid->fsid = kstfs.f_fsid;
11274+ } else {
11275+ dentry = path->dentry;
11276+ sb = dentry->d_sb;
11277+ dev = sb->s_dev;
11278+ if (dev) {
11279+ brid->type = AuBrid_DEV;
11280+ brid->dev = dev;
11281+ }
11282+ }
11283+
11284+out:
11285+ return err;
11286+}
11287+
11288+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11289+ const struct path *path)
11290+{
11291+ int err, i;
11292+ struct au_dr_br *dr;
11293+ struct hlist_bl_head *hbl;
11294+
11295+ dr = &br->br_dirren;
11296+ hbl = dr->dr_h_ino;
11297+ for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11298+ INIT_HLIST_BL_HEAD(hbl);
11299+
11300+ err = au_dr_brid_init(&dr->dr_brid, path);
11301+ if (unlikely(err))
11302+ goto out;
11303+
11304+ if (au_opt_test(au_mntflags(sb), DIRREN))
11305+ err = au_dr_hino(sb, /*bindex*/-1, br, path);
11306+
11307+out:
11308+ AuTraceErr(err);
11309+ return err;
11310+}
11311+
11312+int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11313+{
11314+ int err;
11315+
11316+ err = 0;
11317+ if (au_br_writable(br->br_perm))
11318+ err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11319+ if (!err)
11320+ au_dr_hino_free(&br->br_dirren);
11321+
11322+ return err;
11323+}
11324+
11325+/* ---------------------------------------------------------------------- */
11326+
11327+static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11328+ char *buf, size_t sz)
11329+{
11330+ int err;
11331+ unsigned int major, minor;
11332+ char *p;
11333+
11334+ p = buf;
11335+ err = snprintf(p, sz, "%d_", brid->type);
11336+ AuDebugOn(err > sz);
11337+ p += err;
11338+ sz -= err;
11339+ switch (brid->type) {
11340+ case AuBrid_Unset:
11341+ return -EINVAL;
11342+ case AuBrid_UUID:
11343+ err = snprintf(p, sz, "%pU", brid->uuid.b);
11344+ break;
11345+ case AuBrid_FSID:
11346+ err = snprintf(p, sz, "%08x-%08x",
11347+ brid->fsid.val[0], brid->fsid.val[1]);
11348+ break;
11349+ case AuBrid_DEV:
11350+ major = MAJOR(brid->dev);
11351+ minor = MINOR(brid->dev);
11352+ if (major <= 0xff && minor <= 0xff)
11353+ err = snprintf(p, sz, "%02x%02x", major, minor);
11354+ else
11355+ err = snprintf(p, sz, "%03x:%05x", major, minor);
11356+ break;
11357+ }
11358+ AuDebugOn(err > sz);
11359+ p += err;
11360+ sz -= err;
11361+ err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11362+ AuDebugOn(err > sz);
11363+ p += err;
11364+ sz -= err;
11365+
11366+ return p - buf;
11367+}
11368+
11369+static int au_drinfo_name(struct au_branch *br, char *name, int len)
11370+{
11371+ int rlen;
11372+ struct dentry *br_dentry;
11373+ struct inode *br_inode;
11374+
11375+ br_dentry = au_br_dentry(br);
11376+ br_inode = d_inode(br_dentry);
11377+ rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11378+ AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11379+ AuDebugOn(rlen > len);
11380+
11381+ return rlen;
11382+}
11383+
11384+/* ---------------------------------------------------------------------- */
11385+
11386+/*
11387+ * from the given @h_dentry, construct drinfo at @*fdata.
11388+ * when the size of @*fdata is not enough, reallocate and return new @fdata and
11389+ * @allocated.
11390+ */
11391+static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11392+ struct dentry *h_dentry,
11393+ unsigned char *allocated)
11394+{
11395+ int err, v;
11396+ struct au_drinfo_fdata *f, *p;
11397+ struct au_drinfo *drinfo;
11398+ struct inode *h_inode;
11399+ struct qstr *qname;
11400+
11401+ err = 0;
11402+ f = *fdata;
11403+ h_inode = d_inode(h_dentry);
11404+ qname = &h_dentry->d_name;
11405+ drinfo = &f->drinfo;
11406+ drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11407+ drinfo->oldnamelen = qname->len;
11408+ if (*allocated < sizeof(*f) + qname->len) {
11409+ v = roundup_pow_of_two(*allocated + qname->len);
11410+ p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11411+ if (unlikely(!p)) {
11412+ err = -ENOMEM;
11413+ AuTraceErr(err);
11414+ goto out;
11415+ }
11416+ f = p;
11417+ *fdata = f;
11418+ *allocated = v;
11419+ drinfo = &f->drinfo;
11420+ }
11421+ memcpy(drinfo->oldname, qname->name, qname->len);
11422+ AuDbg("i%llu, %.*s\n",
11423+ be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11424+ drinfo->oldname);
11425+
11426+out:
11427+ AuTraceErr(err);
11428+ return err;
11429+}
11430+
11431+/* callers have to free the return value */
11432+static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11433+{
11434+ struct au_drinfo *ret, *drinfo;
11435+ struct au_drinfo_fdata fdata;
11436+ int len;
11437+ loff_t pos;
11438+ ssize_t ssz;
11439+
11440+ ret = ERR_PTR(-EIO);
11441+ pos = 0;
11442+ ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11443+ if (unlikely(ssz != sizeof(fdata))) {
11444+ AuIOErr("ssz %zd, %u, %pD2\n",
11445+ ssz, (unsigned int)sizeof(fdata), file);
11446+ goto out;
11447+ }
11448+
11449+ fdata.magic = ntohl((__force __be32)fdata.magic);
11450+ switch (fdata.magic) {
11451+ case AUFS_DRINFO_MAGIC_V1:
11452+ break;
11453+ default:
11454+ AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11455+ fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11456+ goto out;
11457+ }
11458+
11459+ drinfo = &fdata.drinfo;
11460+ len = drinfo->oldnamelen;
11461+ if (!len) {
11462+ AuIOErr("broken drinfo %pD2\n", file);
11463+ goto out;
11464+ }
11465+
11466+ ret = NULL;
11467+ drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11468+ if (unlikely(h_ino && drinfo->ino != h_ino)) {
11469+ AuDbg("ignored i%llu, i%llu, %pD2\n",
11470+ (unsigned long long)drinfo->ino,
11471+ (unsigned long long)h_ino, file);
11472+ goto out; /* success */
11473+ }
11474+
11475+ ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11476+ if (unlikely(!ret)) {
11477+ ret = ERR_PTR(-ENOMEM);
11478+ AuTraceErrPtr(ret);
11479+ goto out;
11480+ }
11481+
11482+ *ret = *drinfo;
11483+ ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11484+ if (unlikely(ssz != len)) {
11485+ kfree(ret);
11486+ ret = ERR_PTR(-EIO);
11487+ AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11488+ goto out;
11489+ }
11490+
11491+ AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11492+
11493+out:
11494+ return ret;
11495+}
11496+
11497+/* ---------------------------------------------------------------------- */
11498+
11499+/* in order to be revertible */
11500+struct au_drinfo_rev_elm {
11501+ int created;
11502+ struct dentry *info_dentry;
11503+ struct au_drinfo *info_last;
11504+};
11505+
11506+struct au_drinfo_rev {
11507+ unsigned char already;
11508+ aufs_bindex_t nelm;
11509+ struct au_drinfo_rev_elm elm[0];
11510+};
11511+
11512+/* todo: isn't it too large? */
11513+struct au_drinfo_store {
11514+ struct path h_ppath;
11515+ struct dentry *h_dentry;
11516+ struct au_drinfo_fdata *fdata;
11517+ char *infoname; /* inside of whname, just after PFX */
11518+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11519+ aufs_bindex_t btgt, btail;
11520+ unsigned char no_sio,
11521+ allocated, /* current size of *fdata */
11522+ infonamelen, /* room size for p */
11523+ whnamelen, /* length of the genarated name */
11524+ renameback; /* renamed back */
11525+};
11526+
11527+/* on rename(2) error, the caller should revert it using @elm */
11528+static int au_drinfo_do_store(struct au_drinfo_store *w,
11529+ struct au_drinfo_rev_elm *elm)
11530+{
11531+ int err, len;
11532+ ssize_t ssz;
11533+ loff_t pos;
11534+ struct path infopath = {
11535+ .mnt = w->h_ppath.mnt
11536+ };
11537+ struct inode *h_dir, *h_inode, *delegated;
11538+ struct file *infofile;
11539+ struct qstr *qname;
11540+
11541+ AuDebugOn(elm
11542+ && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11543+
11544+ infopath.dentry = vfsub_lookup_one_len(w->whname, w->h_ppath.dentry,
11545+ w->whnamelen);
11546+ AuTraceErrPtr(infopath.dentry);
11547+ if (IS_ERR(infopath.dentry)) {
11548+ err = PTR_ERR(infopath.dentry);
11549+ goto out;
11550+ }
11551+
11552+ err = 0;
11553+ h_dir = d_inode(w->h_ppath.dentry);
11554+ if (elm && d_is_negative(infopath.dentry)) {
11555+ err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11556+ AuTraceErr(err);
11557+ if (unlikely(err))
11558+ goto out_dput;
11559+ elm->created = 1;
11560+ elm->info_dentry = dget(infopath.dentry);
11561+ }
11562+
11563+ infofile = vfsub_dentry_open(&infopath, O_RDWR);
11564+ AuTraceErrPtr(infofile);
11565+ if (IS_ERR(infofile)) {
11566+ err = PTR_ERR(infofile);
11567+ goto out_dput;
11568+ }
11569+
11570+ h_inode = d_inode(infopath.dentry);
11571+ if (elm && i_size_read(h_inode)) {
11572+ h_inode = d_inode(w->h_dentry);
11573+ elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11574+ AuTraceErrPtr(elm->info_last);
11575+ if (IS_ERR(elm->info_last)) {
11576+ err = PTR_ERR(elm->info_last);
11577+ elm->info_last = NULL;
11578+ AuDebugOn(elm->info_dentry);
11579+ goto out_fput;
11580+ }
11581+ }
11582+
11583+ if (elm && w->renameback) {
11584+ delegated = NULL;
11585+ err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11586+ AuTraceErr(err);
11587+ if (unlikely(err == -EWOULDBLOCK))
11588+ iput(delegated);
11589+ goto out_fput;
11590+ }
11591+
11592+ pos = 0;
11593+ qname = &w->h_dentry->d_name;
11594+ len = sizeof(*w->fdata) + qname->len;
11595+ if (!elm)
11596+ len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11597+ ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11598+ if (ssz == len) {
11599+ AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11600+ w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11601+ goto out_fput; /* success */
11602+ } else {
11603+ err = -EIO;
11604+ if (ssz < 0)
11605+ err = ssz;
11606+ /* the caller should revert it using @elm */
11607+ }
11608+
11609+out_fput:
11610+ fput(infofile);
11611+out_dput:
11612+ dput(infopath.dentry);
11613+out:
11614+ AuTraceErr(err);
11615+ return err;
11616+}
11617+
11618+struct au_call_drinfo_do_store_args {
11619+ int *errp;
11620+ struct au_drinfo_store *w;
11621+ struct au_drinfo_rev_elm *elm;
11622+};
11623+
11624+static void au_call_drinfo_do_store(void *args)
11625+{
11626+ struct au_call_drinfo_do_store_args *a = args;
11627+
11628+ *a->errp = au_drinfo_do_store(a->w, a->elm);
11629+}
11630+
11631+static int au_drinfo_store_sio(struct au_drinfo_store *w,
11632+ struct au_drinfo_rev_elm *elm)
11633+{
11634+ int err, wkq_err;
11635+
11636+ if (w->no_sio)
11637+ err = au_drinfo_do_store(w, elm);
11638+ else {
11639+ struct au_call_drinfo_do_store_args a = {
11640+ .errp = &err,
11641+ .w = w,
11642+ .elm = elm
11643+ };
11644+ wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11645+ if (unlikely(wkq_err))
11646+ err = wkq_err;
11647+ }
11648+ AuTraceErr(err);
11649+
11650+ return err;
11651+}
11652+
11653+static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11654+ aufs_bindex_t btgt)
11655+{
11656+ int err;
11657+
11658+ memset(w, 0, sizeof(*w));
11659+ w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11660+ strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11661+ w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11662+ w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11663+ w->btgt = btgt;
11664+ w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11665+
11666+ err = -ENOMEM;
11667+ w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11668+ if (unlikely(!w->fdata)) {
11669+ AuTraceErr(err);
11670+ goto out;
11671+ }
11672+ w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11673+ err = 0;
11674+
11675+out:
11676+ return err;
11677+}
11678+
11679+static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11680+{
11681+ kfree(w->fdata);
11682+}
11683+
11684+static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11685+ struct au_drinfo_store *w)
11686+{
11687+ struct au_drinfo_rev_elm *elm;
11688+ struct inode *h_dir, *delegated;
11689+ int err, nelm;
11690+ struct path infopath = {
11691+ .mnt = w->h_ppath.mnt
11692+ };
11693+
11694+ h_dir = d_inode(w->h_ppath.dentry);
11695+ IMustLock(h_dir);
11696+
11697+ err = 0;
11698+ elm = rev->elm;
11699+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11700+ AuDebugOn(elm->created && elm->info_last);
11701+ if (elm->created) {
11702+ AuDbg("here\n");
11703+ delegated = NULL;
11704+ infopath.dentry = elm->info_dentry;
11705+ err = vfsub_unlink(h_dir, &infopath, &delegated,
11706+ !w->no_sio);
11707+ AuTraceErr(err);
11708+ if (unlikely(err == -EWOULDBLOCK))
11709+ iput(delegated);
11710+ dput(elm->info_dentry);
11711+ } else if (elm->info_last) {
11712+ AuDbg("here\n");
11713+ w->fdata->drinfo = *elm->info_last;
11714+ memcpy(w->fdata->drinfo.oldname,
11715+ elm->info_last->oldname,
11716+ elm->info_last->oldnamelen);
11717+ err = au_drinfo_store_sio(w, /*elm*/NULL);
11718+ kfree(elm->info_last);
11719+ }
11720+ if (unlikely(err))
11721+ AuIOErr("%d, %s\n", err, w->whname);
11722+ /* go on even if err */
11723+ }
11724+}
11725+
11726+/* caller has to call au_dr_rename_fin() later */
11727+static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11728+ struct qstr *dst_name, void *_rev)
11729+{
11730+ int err, sz, nelm;
11731+ aufs_bindex_t bindex, btail;
11732+ struct au_drinfo_store work;
11733+ struct au_drinfo_rev *rev, **p;
11734+ struct au_drinfo_rev_elm *elm;
11735+ struct super_block *sb;
11736+ struct au_branch *br;
11737+ struct au_hinode *hdir;
11738+
11739+ err = au_drinfo_store_work_init(&work, btgt);
11740+ AuTraceErr(err);
11741+ if (unlikely(err))
11742+ goto out;
11743+
11744+ err = -ENOMEM;
11745+ btail = au_dbtaildir(dentry);
11746+ nelm = btail - btgt;
11747+ sz = sizeof(*rev) + sizeof(*elm) * nelm;
11748+ rev = kcalloc(1, sz, GFP_NOFS);
11749+ if (unlikely(!rev)) {
11750+ AuTraceErr(err);
11751+ goto out_args;
11752+ }
11753+ rev->nelm = nelm;
11754+ elm = rev->elm;
11755+ p = _rev;
11756+ *p = rev;
11757+
11758+ err = 0;
11759+ sb = dentry->d_sb;
11760+ work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11761+ work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11762+ hdir = au_hi(d_inode(dentry), btgt);
11763+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11764+ for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11765+ work.h_dentry = au_h_dptr(dentry, bindex);
11766+ if (!work.h_dentry)
11767+ continue;
11768+
11769+ err = au_drinfo_construct(&work.fdata, work.h_dentry,
11770+ &work.allocated);
11771+ AuTraceErr(err);
11772+ if (unlikely(err))
11773+ break;
11774+
11775+ work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11776+ br = au_sbr(sb, bindex);
11777+ work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11778+ work.whnamelen += au_drinfo_name(br, work.infoname,
11779+ work.infonamelen);
11780+ AuDbg("whname %.*s, i%llu, %.*s\n",
11781+ work.whnamelen, work.whname,
11782+ be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11783+ work.fdata->drinfo.oldnamelen,
11784+ work.fdata->drinfo.oldname);
11785+
11786+ err = au_drinfo_store_sio(&work, elm);
11787+ AuTraceErr(err);
11788+ if (unlikely(err))
11789+ break;
11790+ }
11791+ if (unlikely(err)) {
11792+ /* revert all drinfo */
11793+ au_drinfo_store_rev(rev, &work);
11794+ kfree(rev);
11795+ *p = NULL;
11796+ }
11797+ au_hn_inode_unlock(hdir);
11798+
11799+out_args:
11800+ au_drinfo_store_work_fin(&work);
11801+out:
11802+ return err;
11803+}
11804+
11805+/* ---------------------------------------------------------------------- */
11806+
11807+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11808+ struct qstr *dst_name, void *_rev)
11809+{
11810+ int err, already;
11811+ ino_t ino;
11812+ struct super_block *sb;
11813+ struct au_branch *br;
11814+ struct au_dr_br *dr;
11815+ struct dentry *h_dentry;
11816+ struct inode *h_inode;
11817+ struct au_dr_hino *ent;
11818+ struct au_drinfo_rev *rev, **p;
11819+
11820+ AuDbg("bindex %d\n", bindex);
11821+
11822+ err = -ENOMEM;
11823+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11824+ if (unlikely(!ent))
11825+ goto out;
11826+
11827+ sb = src->d_sb;
11828+ br = au_sbr(sb, bindex);
11829+ dr = &br->br_dirren;
11830+ h_dentry = au_h_dptr(src, bindex);
11831+ h_inode = d_inode(h_dentry);
11832+ ino = h_inode->i_ino;
11833+ ent->dr_h_ino = ino;
11834+ already = au_dr_hino_test_add(dr, ino, ent);
11835+ AuDbg("b%d, hi%llu, already %d\n",
11836+ bindex, (unsigned long long)ino, already);
11837+
11838+ err = au_drinfo_store(src, bindex, dst_name, _rev);
11839+ AuTraceErr(err);
11840+ if (!err) {
11841+ p = _rev;
11842+ rev = *p;
11843+ rev->already = already;
11844+ goto out; /* success */
11845+ }
11846+
11847+ /* revert */
11848+ if (!already)
11849+ au_dr_hino_del(dr, ent);
11850+ kfree(ent);
11851+
11852+out:
11853+ AuTraceErr(err);
11854+ return err;
11855+}
11856+
11857+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11858+{
11859+ struct au_drinfo_rev *rev;
11860+ struct au_drinfo_rev_elm *elm;
11861+ int nelm;
11862+
11863+ rev = _rev;
11864+ elm = rev->elm;
11865+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11866+ dput(elm->info_dentry);
11867+ kfree(elm->info_last);
11868+ }
11869+ kfree(rev);
11870+}
11871+
11872+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11873+{
11874+ int err;
11875+ struct au_drinfo_store work;
11876+ struct au_drinfo_rev *rev = _rev;
11877+ struct super_block *sb;
11878+ struct au_branch *br;
11879+ struct inode *h_inode;
11880+ struct au_dr_br *dr;
11881+ struct au_dr_hino *ent;
11882+
11883+ err = au_drinfo_store_work_init(&work, btgt);
11884+ if (unlikely(err))
11885+ goto out;
11886+
11887+ sb = src->d_sb;
11888+ br = au_sbr(sb, btgt);
11889+ work.h_ppath.dentry = au_h_dptr(src, btgt);
11890+ work.h_ppath.mnt = au_br_mnt(br);
11891+ au_drinfo_store_rev(rev, &work);
11892+ au_drinfo_store_work_fin(&work);
11893+ if (rev->already)
11894+ goto out;
11895+
11896+ dr = &br->br_dirren;
11897+ h_inode = d_inode(work.h_ppath.dentry);
11898+ ent = au_dr_hino_find(dr, h_inode->i_ino);
11899+ BUG_ON(!ent);
11900+ au_dr_hino_del(dr, ent);
11901+ kfree(ent);
11902+
11903+out:
11904+ kfree(rev);
11905+ if (unlikely(err))
11906+ pr_err("failed to remove dirren info\n");
11907+}
11908+
11909+/* ---------------------------------------------------------------------- */
11910+
11911+static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11912+ char *whname, int whnamelen,
11913+ struct dentry **info_dentry)
11914+{
11915+ struct au_drinfo *drinfo;
11916+ struct file *f;
11917+ struct inode *h_dir;
11918+ struct path infopath;
11919+ int unlocked;
11920+
11921+ AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11922+
11923+ *info_dentry = NULL;
11924+ drinfo = NULL;
11925+ unlocked = 0;
11926+ h_dir = d_inode(h_ppath->dentry);
11927+ vfsub_inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
11928+ infopath.dentry = vfsub_lookup_one_len(whname, h_ppath->dentry,
11929+ whnamelen);
11930+ if (IS_ERR(infopath.dentry)) {
11931+ drinfo = (void *)infopath.dentry;
11932+ goto out;
11933+ }
11934+
11935+ if (d_is_negative(infopath.dentry))
11936+ goto out_dput; /* success */
11937+
11938+ infopath.mnt = h_ppath->mnt;
11939+ f = vfsub_dentry_open(&infopath, O_RDONLY);
11940+ inode_unlock_shared(h_dir);
11941+ unlocked = 1;
11942+ if (IS_ERR(f)) {
11943+ drinfo = (void *)f;
11944+ goto out_dput;
11945+ }
11946+
11947+ drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11948+ if (IS_ERR_OR_NULL(drinfo))
11949+ goto out_fput;
11950+
11951+ AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11952+ *info_dentry = dget(infopath.dentry); /* keep it alive */
11953+
11954+out_fput:
11955+ fput(f);
11956+out_dput:
11957+ dput(infopath.dentry);
11958+out:
11959+ if (!unlocked)
11960+ inode_unlock_shared(h_dir);
11961+ AuTraceErrPtr(drinfo);
11962+ return drinfo;
11963+}
11964+
11965+struct au_drinfo_do_load_args {
11966+ struct au_drinfo **drinfop;
11967+ struct path *h_ppath;
11968+ char *whname;
11969+ int whnamelen;
11970+ struct dentry **info_dentry;
11971+};
11972+
11973+static void au_call_drinfo_do_load(void *args)
11974+{
11975+ struct au_drinfo_do_load_args *a = args;
11976+
11977+ *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
11978+ a->info_dentry);
11979+}
11980+
11981+struct au_drinfo_load {
11982+ struct path h_ppath;
11983+ struct qstr *qname;
11984+ unsigned char no_sio;
11985+
11986+ aufs_bindex_t ninfo;
11987+ struct au_drinfo **drinfo;
11988+};
11989+
11990+static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
11991+ struct au_branch *br)
11992+{
11993+ int err, wkq_err, whnamelen, e;
11994+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
11995+ = AUFS_WH_DR_INFO_PFX;
11996+ struct au_drinfo *drinfo;
11997+ struct qstr oldname;
11998+ struct inode *h_dir, *delegated;
11999+ struct dentry *info_dentry;
12000+ struct path infopath;
12001+
12002+ whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
12003+ whnamelen += au_drinfo_name(br, whname + whnamelen,
12004+ sizeof(whname) - whnamelen);
12005+ if (w->no_sio)
12006+ drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
12007+ &info_dentry);
12008+ else {
12009+ struct au_drinfo_do_load_args args = {
12010+ .drinfop = &drinfo,
12011+ .h_ppath = &w->h_ppath,
12012+ .whname = whname,
12013+ .whnamelen = whnamelen,
12014+ .info_dentry = &info_dentry
12015+ };
12016+ wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
12017+ if (unlikely(wkq_err))
12018+ drinfo = ERR_PTR(wkq_err);
12019+ }
12020+ err = PTR_ERR(drinfo);
12021+ if (IS_ERR_OR_NULL(drinfo))
12022+ goto out;
12023+
12024+ err = 0;
12025+ oldname.len = drinfo->oldnamelen;
12026+ oldname.name = drinfo->oldname;
12027+ if (au_qstreq(w->qname, &oldname)) {
12028+ /* the name is renamed back */
12029+ kfree(drinfo);
12030+ drinfo = NULL;
12031+
12032+ infopath.dentry = info_dentry;
12033+ infopath.mnt = w->h_ppath.mnt;
12034+ h_dir = d_inode(w->h_ppath.dentry);
12035+ delegated = NULL;
12036+ inode_lock_nested(h_dir, AuLsc_I_PARENT);
12037+ e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
12038+ inode_unlock(h_dir);
12039+ if (unlikely(e))
12040+ AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
12041+ if (unlikely(e == -EWOULDBLOCK))
12042+ iput(delegated);
12043+ }
12044+ kfree(w->drinfo[bindex]);
12045+ w->drinfo[bindex] = drinfo;
12046+ dput(info_dentry);
12047+
12048+out:
12049+ AuTraceErr(err);
12050+ return err;
12051+}
12052+
12053+/* ---------------------------------------------------------------------- */
12054+
12055+static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12056+{
12057+ struct au_drinfo **p = drinfo;
12058+
12059+ while (n-- > 0)
12060+ kfree(*drinfo++);
12061+ kfree(p);
12062+}
12063+
12064+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12065+ aufs_bindex_t btgt)
12066+{
12067+ int err, ninfo;
12068+ struct au_drinfo_load w;
12069+ aufs_bindex_t bindex, bbot;
12070+ struct au_branch *br;
12071+ struct inode *h_dir;
12072+ struct au_dr_hino *ent;
12073+ struct super_block *sb;
12074+
12075+ AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12076+ AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12077+ AuLNPair(&lkup->whname), btgt);
12078+
12079+ sb = dentry->d_sb;
12080+ bbot = au_sbbot(sb);
12081+ w.ninfo = bbot + 1;
12082+ if (!lkup->dirren.drinfo) {
12083+ lkup->dirren.drinfo = kcalloc(w.ninfo,
12084+ sizeof(*lkup->dirren.drinfo),
12085+ GFP_NOFS);
12086+ if (unlikely(!lkup->dirren.drinfo)) {
12087+ err = -ENOMEM;
12088+ goto out;
12089+ }
12090+ lkup->dirren.ninfo = w.ninfo;
12091+ }
12092+ w.drinfo = lkup->dirren.drinfo;
12093+ w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12094+ w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12095+ AuDebugOn(!w.h_ppath.dentry);
12096+ w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12097+ w.qname = &dentry->d_name;
12098+
12099+ ninfo = 0;
12100+ for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12101+ br = au_sbr(sb, bindex);
12102+ err = au_drinfo_load(&w, bindex, br);
12103+ if (unlikely(err))
12104+ goto out_free;
12105+ if (w.drinfo[bindex])
12106+ ninfo++;
12107+ }
12108+ if (!ninfo) {
12109+ br = au_sbr(sb, btgt);
12110+ h_dir = d_inode(w.h_ppath.dentry);
12111+ ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12112+ AuDebugOn(!ent);
12113+ au_dr_hino_del(&br->br_dirren, ent);
12114+ kfree(ent);
12115+ }
12116+ goto out; /* success */
12117+
12118+out_free:
12119+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12120+ lkup->dirren.ninfo = 0;
12121+ lkup->dirren.drinfo = NULL;
12122+out:
12123+ AuTraceErr(err);
12124+ return err;
12125+}
12126+
12127+void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12128+{
12129+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12130+}
12131+
12132+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12133+{
12134+ int err;
12135+ struct au_drinfo *drinfo;
12136+
12137+ err = 0;
12138+ if (!lkup->dirren.drinfo)
12139+ goto out;
12140+ AuDebugOn(lkup->dirren.ninfo < btgt + 1);
12141+ drinfo = lkup->dirren.drinfo[btgt + 1];
12142+ if (!drinfo)
12143+ goto out;
12144+
12145+ kfree(lkup->whname.name);
12146+ lkup->whname.name = NULL;
12147+ lkup->dirren.dr_name.len = drinfo->oldnamelen;
12148+ lkup->dirren.dr_name.name = drinfo->oldname;
12149+ lkup->name = &lkup->dirren.dr_name;
12150+ err = au_wh_name_alloc(&lkup->whname, lkup->name);
12151+ if (!err)
12152+ AuDbg("name %.*s, whname %.*s, b%d\n",
12153+ AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12154+ btgt);
12155+
12156+out:
12157+ AuTraceErr(err);
12158+ return err;
12159+}
12160+
12161+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12162+ ino_t h_ino)
12163+{
12164+ int match;
12165+ struct au_drinfo *drinfo;
12166+
12167+ match = 1;
12168+ if (!lkup->dirren.drinfo)
12169+ goto out;
12170+ AuDebugOn(lkup->dirren.ninfo < bindex + 1);
12171+ drinfo = lkup->dirren.drinfo[bindex + 1];
12172+ if (!drinfo)
12173+ goto out;
12174+
12175+ match = (drinfo->ino == h_ino);
12176+ AuDbg("match %d\n", match);
12177+
12178+out:
12179+ return match;
12180+}
12181+
12182+/* ---------------------------------------------------------------------- */
12183+
12184+int au_dr_opt_set(struct super_block *sb)
12185+{
12186+ int err;
12187+ aufs_bindex_t bindex, bbot;
12188+ struct au_branch *br;
12189+
12190+ err = 0;
12191+ bbot = au_sbbot(sb);
12192+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12193+ br = au_sbr(sb, bindex);
12194+ err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12195+ }
12196+
12197+ return err;
12198+}
12199+
12200+int au_dr_opt_flush(struct super_block *sb)
12201+{
12202+ int err;
12203+ aufs_bindex_t bindex, bbot;
12204+ struct au_branch *br;
12205+
12206+ err = 0;
12207+ bbot = au_sbbot(sb);
12208+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12209+ br = au_sbr(sb, bindex);
12210+ if (au_br_writable(br->br_perm))
12211+ err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12212+ }
12213+
12214+ return err;
12215+}
12216+
12217+int au_dr_opt_clr(struct super_block *sb, int no_flush)
12218+{
12219+ int err;
12220+ aufs_bindex_t bindex, bbot;
12221+ struct au_branch *br;
12222+
12223+ err = 0;
12224+ if (!no_flush) {
12225+ err = au_dr_opt_flush(sb);
12226+ if (unlikely(err))
12227+ goto out;
12228+ }
12229+
12230+ bbot = au_sbbot(sb);
12231+ for (bindex = 0; bindex <= bbot; bindex++) {
12232+ br = au_sbr(sb, bindex);
12233+ au_dr_hino_free(&br->br_dirren);
12234+ }
12235+
12236+out:
12237+ return err;
12238+}
12239diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12240--- /usr/share/empty/fs/aufs/dirren.h 1970-01-01 01:00:00.000000000 +0100
12241+++ linux/fs/aufs/dirren.h 2017-11-12 22:24:44.704244405 +0100
12242@@ -0,0 +1,139 @@
12243+/*
12244+ * Copyright (C) 2017 Junjiro R. Okajima
12245+ *
12246+ * This program, aufs is free software; you can redistribute it and/or modify
12247+ * it under the terms of the GNU General Public License as published by
12248+ * the Free Software Foundation; either version 2 of the License, or
12249+ * (at your option) any later version.
12250+ *
12251+ * This program is distributed in the hope that it will be useful,
12252+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12253+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12254+ * GNU General Public License for more details.
12255+ *
12256+ * You should have received a copy of the GNU General Public License
12257+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
12258+ */
12259+
12260+/*
12261+ * renamed dir info
12262+ */
12263+
12264+#ifndef __AUFS_DIRREN_H__
12265+#define __AUFS_DIRREN_H__
12266+
12267+#ifdef __KERNEL__
12268+
12269+#include <linux/dcache.h>
12270+#include <linux/statfs.h>
12271+#include <linux/uuid.h>
12272+#include "hbl.h"
12273+
12274+#define AuDirren_NHASH 100
12275+
12276+#ifdef CONFIG_AUFS_DIRREN
12277+enum au_brid_type {
12278+ AuBrid_Unset,
12279+ AuBrid_UUID,
12280+ AuBrid_FSID,
12281+ AuBrid_DEV
12282+};
12283+
12284+struct au_dr_brid {
12285+ enum au_brid_type type;
12286+ union {
12287+ uuid_t uuid; /* unimplemented yet */
12288+ fsid_t fsid;
12289+ dev_t dev;
12290+ };
12291+};
12292+
12293+/* 20 is the max digits length of ulong 64 */
12294+/* brid-type "_" uuid "_" inum */
12295+#define AUFS_DIRREN_FNAME_SZ (1 + 1 + UUID_STRING_LEN + 20)
12296+#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12297+
12298+struct au_dr_hino {
12299+ struct hlist_bl_node dr_hnode;
12300+ ino_t dr_h_ino;
12301+};
12302+
12303+struct au_dr_br {
12304+ struct hlist_bl_head dr_h_ino[AuDirren_NHASH];
12305+ struct au_dr_brid dr_brid;
12306+};
12307+
12308+struct au_dr_lookup {
12309+ /* dr_name is pointed by struct au_do_lookup_args.name */
12310+ struct qstr dr_name; /* subset of dr_info */
12311+ aufs_bindex_t ninfo;
12312+ struct au_drinfo **drinfo;
12313+};
12314+#else
12315+struct au_dr_hino;
12316+/* empty */
12317+struct au_dr_br { };
12318+struct au_dr_lookup { };
12319+#endif
12320+
12321+/* ---------------------------------------------------------------------- */
12322+
12323+struct au_branch;
12324+struct au_do_lookup_args;
12325+struct au_hinode;
12326+#ifdef CONFIG_AUFS_DIRREN
12327+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12328+ struct au_dr_hino *add_ent);
12329+void au_dr_hino_free(struct au_dr_br *dr);
12330+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12331+ const struct path *path);
12332+int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12333+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12334+ struct qstr *dst_name, void *_rev);
12335+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12336+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12337+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12338+ aufs_bindex_t bindex);
12339+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12340+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12341+ ino_t h_ino);
12342+void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12343+int au_dr_opt_set(struct super_block *sb);
12344+int au_dr_opt_flush(struct super_block *sb);
12345+int au_dr_opt_clr(struct super_block *sb, int no_flush);
12346+#else
12347+AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12348+ struct au_dr_hino *add_ent);
12349+AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12350+AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12351+ const struct path *path);
12352+AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12353+AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12354+ struct qstr *dst_name, void *_rev);
12355+AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12356+AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12357+ void *rev);
12358+AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12359+ aufs_bindex_t bindex);
12360+AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12361+AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12362+ aufs_bindex_t bindex, ino_t h_ino);
12363+AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12364+AuStubInt0(au_dr_opt_set, struct super_block *sb);
12365+AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12366+AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12367+#endif
12368+
12369+/* ---------------------------------------------------------------------- */
12370+
12371+#ifdef CONFIG_AUFS_DIRREN
12372+static inline int au_dr_ihash(ino_t h_ino)
12373+{
12374+ return h_ino % AuDirren_NHASH;
12375+}
12376+#else
12377+AuStubInt0(au_dr_ihash, ino_t h_ino);
12378+#endif
12379+
12380+#endif /* __KERNEL__ */
12381+#endif /* __AUFS_DIRREN_H__ */
7f207e10
AM
12382diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12383--- /usr/share/empty/fs/aufs/dynop.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
12384+++ linux/fs/aufs/dynop.c 2017-11-12 22:24:44.704244405 +0100
12385@@ -0,0 +1,369 @@
1facf9fc 12386+/*
a2654f78 12387+ * Copyright (C) 2010-2017 Junjiro R. Okajima
1facf9fc 12388+ *
12389+ * This program, aufs is free software; you can redistribute it and/or modify
12390+ * it under the terms of the GNU General Public License as published by
12391+ * the Free Software Foundation; either version 2 of the License, or
12392+ * (at your option) any later version.
dece6358
AM
12393+ *
12394+ * This program is distributed in the hope that it will be useful,
12395+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12396+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12397+ * GNU General Public License for more details.
12398+ *
12399+ * You should have received a copy of the GNU General Public License
523b37e3 12400+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 12401+ */
12402+
12403+/*
4a4d8108 12404+ * dynamically customizable operations for regular files
1facf9fc 12405+ */
12406+
1facf9fc 12407+#include "aufs.h"
12408+
4a4d8108 12409+#define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
1facf9fc 12410+
4a4d8108
AM
12411+/*
12412+ * How large will these lists be?
12413+ * Usually just a few elements, 20-30 at most for each, I guess.
12414+ */
8b6a4947 12415+static struct hlist_bl_head dynop[AuDyLast];
4a4d8108 12416+
8b6a4947
AM
12417+static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12418+ const void *h_op)
1facf9fc 12419+{
4a4d8108 12420+ struct au_dykey *key, *tmp;
8b6a4947 12421+ struct hlist_bl_node *pos;
1facf9fc 12422+
4a4d8108 12423+ key = NULL;
8b6a4947
AM
12424+ hlist_bl_lock(hbl);
12425+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
4a4d8108
AM
12426+ if (tmp->dk_op.dy_hop == h_op) {
12427+ key = tmp;
12428+ kref_get(&key->dk_kref);
12429+ break;
12430+ }
8b6a4947 12431+ hlist_bl_unlock(hbl);
4a4d8108
AM
12432+
12433+ return key;
1facf9fc 12434+}
12435+
4a4d8108 12436+static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
1facf9fc 12437+{
4a4d8108
AM
12438+ struct au_dykey **k, *found;
12439+ const void *h_op = key->dk_op.dy_hop;
12440+ int i;
1facf9fc 12441+
4a4d8108
AM
12442+ found = NULL;
12443+ k = br->br_dykey;
12444+ for (i = 0; i < AuBrDynOp; i++)
12445+ if (k[i]) {
12446+ if (k[i]->dk_op.dy_hop == h_op) {
12447+ found = k[i];
12448+ break;
12449+ }
12450+ } else
12451+ break;
12452+ if (!found) {
12453+ spin_lock(&br->br_dykey_lock);
12454+ for (; i < AuBrDynOp; i++)
12455+ if (k[i]) {
12456+ if (k[i]->dk_op.dy_hop == h_op) {
12457+ found = k[i];
12458+ break;
12459+ }
12460+ } else {
12461+ k[i] = key;
12462+ break;
12463+ }
12464+ spin_unlock(&br->br_dykey_lock);
12465+ BUG_ON(i == AuBrDynOp); /* expand the array */
12466+ }
12467+
12468+ return found;
1facf9fc 12469+}
12470+
4a4d8108 12471+/* kref_get() if @key is already added */
8b6a4947 12472+static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
4a4d8108
AM
12473+{
12474+ struct au_dykey *tmp, *found;
8b6a4947 12475+ struct hlist_bl_node *pos;
4a4d8108 12476+ const void *h_op = key->dk_op.dy_hop;
1facf9fc 12477+
4a4d8108 12478+ found = NULL;
8b6a4947
AM
12479+ hlist_bl_lock(hbl);
12480+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
4a4d8108
AM
12481+ if (tmp->dk_op.dy_hop == h_op) {
12482+ kref_get(&tmp->dk_kref);
12483+ found = tmp;
12484+ break;
12485+ }
12486+ if (!found)
8b6a4947
AM
12487+ hlist_bl_add_head(&key->dk_hnode, hbl);
12488+ hlist_bl_unlock(hbl);
1facf9fc 12489+
4a4d8108
AM
12490+ if (!found)
12491+ DyPrSym(key);
12492+ return found;
12493+}
12494+
12495+static void dy_free_rcu(struct rcu_head *rcu)
1facf9fc 12496+{
4a4d8108
AM
12497+ struct au_dykey *key;
12498+
12499+ key = container_of(rcu, struct au_dykey, dk_rcu);
12500+ DyPrSym(key);
1c60b727 12501+ kfree(key);
1facf9fc 12502+}
12503+
4a4d8108
AM
12504+static void dy_free(struct kref *kref)
12505+{
12506+ struct au_dykey *key;
8b6a4947 12507+ struct hlist_bl_head *hbl;
1facf9fc 12508+
4a4d8108 12509+ key = container_of(kref, struct au_dykey, dk_kref);
8b6a4947
AM
12510+ hbl = dynop + key->dk_op.dy_type;
12511+ au_hbl_del(&key->dk_hnode, hbl);
4a4d8108
AM
12512+ call_rcu(&key->dk_rcu, dy_free_rcu);
12513+}
12514+
12515+void au_dy_put(struct au_dykey *key)
1facf9fc 12516+{
4a4d8108
AM
12517+ kref_put(&key->dk_kref, dy_free);
12518+}
1facf9fc 12519+
4a4d8108
AM
12520+/* ---------------------------------------------------------------------- */
12521+
12522+#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12523+
12524+#ifdef CONFIG_AUFS_DEBUG
12525+#define DyDbgDeclare(cnt) unsigned int cnt = 0
4f0767ce 12526+#define DyDbgInc(cnt) do { cnt++; } while (0)
4a4d8108
AM
12527+#else
12528+#define DyDbgDeclare(cnt) do {} while (0)
12529+#define DyDbgInc(cnt) do {} while (0)
12530+#endif
12531+
12532+#define DySet(func, dst, src, h_op, h_sb) do { \
12533+ DyDbgInc(cnt); \
12534+ if (h_op->func) { \
12535+ if (src.func) \
12536+ dst.func = src.func; \
12537+ else \
12538+ AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
12539+ } \
12540+} while (0)
12541+
12542+#define DySetForce(func, dst, src) do { \
12543+ AuDebugOn(!src.func); \
12544+ DyDbgInc(cnt); \
12545+ dst.func = src.func; \
12546+} while (0)
12547+
12548+#define DySetAop(func) \
12549+ DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12550+#define DySetAopForce(func) \
12551+ DySetForce(func, dyaop->da_op, aufs_aop)
12552+
12553+static void dy_aop(struct au_dykey *key, const void *h_op,
12554+ struct super_block *h_sb __maybe_unused)
12555+{
12556+ struct au_dyaop *dyaop = (void *)key;
12557+ const struct address_space_operations *h_aop = h_op;
12558+ DyDbgDeclare(cnt);
12559+
12560+ AuDbg("%s\n", au_sbtype(h_sb));
12561+
12562+ DySetAop(writepage);
12563+ DySetAopForce(readpage); /* force */
4a4d8108
AM
12564+ DySetAop(writepages);
12565+ DySetAop(set_page_dirty);
12566+ DySetAop(readpages);
12567+ DySetAop(write_begin);
12568+ DySetAop(write_end);
12569+ DySetAop(bmap);
12570+ DySetAop(invalidatepage);
12571+ DySetAop(releasepage);
027c5e7a 12572+ DySetAop(freepage);
7e9cd9fe 12573+ /* this one will be changed according to an aufs mount option */
4a4d8108 12574+ DySetAop(direct_IO);
4a4d8108 12575+ DySetAop(migratepage);
e2f27e51
AM
12576+ DySetAop(isolate_page);
12577+ DySetAop(putback_page);
4a4d8108
AM
12578+ DySetAop(launder_page);
12579+ DySetAop(is_partially_uptodate);
392086de 12580+ DySetAop(is_dirty_writeback);
4a4d8108 12581+ DySetAop(error_remove_page);
b4510431
AM
12582+ DySetAop(swap_activate);
12583+ DySetAop(swap_deactivate);
4a4d8108
AM
12584+
12585+ DyDbgSize(cnt, *h_aop);
4a4d8108
AM
12586+}
12587+
4a4d8108
AM
12588+/* ---------------------------------------------------------------------- */
12589+
12590+static void dy_bug(struct kref *kref)
12591+{
12592+ BUG();
12593+}
12594+
12595+static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12596+{
12597+ struct au_dykey *key, *old;
8b6a4947 12598+ struct hlist_bl_head *hbl;
b752ccd1 12599+ struct op {
4a4d8108 12600+ unsigned int sz;
b752ccd1
AM
12601+ void (*set)(struct au_dykey *key, const void *h_op,
12602+ struct super_block *h_sb __maybe_unused);
12603+ };
12604+ static const struct op a[] = {
4a4d8108
AM
12605+ [AuDy_AOP] = {
12606+ .sz = sizeof(struct au_dyaop),
b752ccd1 12607+ .set = dy_aop
4a4d8108 12608+ }
b752ccd1
AM
12609+ };
12610+ const struct op *p;
4a4d8108 12611+
8b6a4947
AM
12612+ hbl = dynop + op->dy_type;
12613+ key = dy_gfind_get(hbl, op->dy_hop);
4a4d8108
AM
12614+ if (key)
12615+ goto out_add; /* success */
12616+
12617+ p = a + op->dy_type;
12618+ key = kzalloc(p->sz, GFP_NOFS);
12619+ if (unlikely(!key)) {
12620+ key = ERR_PTR(-ENOMEM);
12621+ goto out;
12622+ }
12623+
12624+ key->dk_op.dy_hop = op->dy_hop;
12625+ kref_init(&key->dk_kref);
86dc4139 12626+ p->set(key, op->dy_hop, au_br_sb(br));
8b6a4947 12627+ old = dy_gadd(hbl, key);
4a4d8108 12628+ if (old) {
1c60b727 12629+ kfree(key);
4a4d8108
AM
12630+ key = old;
12631+ }
12632+
12633+out_add:
12634+ old = dy_bradd(br, key);
12635+ if (old)
12636+ /* its ref-count should never be zero here */
12637+ kref_put(&key->dk_kref, dy_bug);
12638+out:
12639+ return key;
12640+}
12641+
12642+/* ---------------------------------------------------------------------- */
12643+/*
12644+ * Aufs prohibits O_DIRECT by defaut even if the branch supports it.
c1595e42 12645+ * This behaviour is necessary to return an error from open(O_DIRECT) instead
4a4d8108
AM
12646+ * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12647+ * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12648+ * See the aufs manual in detail.
4a4d8108
AM
12649+ */
12650+static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12651+{
7e9cd9fe 12652+ if (!do_dx)
4a4d8108 12653+ dyaop->da_op.direct_IO = NULL;
7e9cd9fe 12654+ else
4a4d8108 12655+ dyaop->da_op.direct_IO = aufs_aop.direct_IO;
4a4d8108
AM
12656+}
12657+
12658+static struct au_dyaop *dy_aget(struct au_branch *br,
12659+ const struct address_space_operations *h_aop,
12660+ int do_dx)
12661+{
12662+ struct au_dyaop *dyaop;
12663+ struct au_dynop op;
12664+
12665+ op.dy_type = AuDy_AOP;
12666+ op.dy_haop = h_aop;
12667+ dyaop = (void *)dy_get(&op, br);
12668+ if (IS_ERR(dyaop))
12669+ goto out;
12670+ dy_adx(dyaop, do_dx);
12671+
12672+out:
12673+ return dyaop;
12674+}
12675+
12676+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12677+ struct inode *h_inode)
12678+{
12679+ int err, do_dx;
12680+ struct super_block *sb;
12681+ struct au_branch *br;
12682+ struct au_dyaop *dyaop;
12683+
12684+ AuDebugOn(!S_ISREG(h_inode->i_mode));
12685+ IiMustWriteLock(inode);
12686+
12687+ sb = inode->i_sb;
12688+ br = au_sbr(sb, bindex);
12689+ do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12690+ dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12691+ err = PTR_ERR(dyaop);
12692+ if (IS_ERR(dyaop))
12693+ /* unnecessary to call dy_fput() */
12694+ goto out;
12695+
12696+ err = 0;
12697+ inode->i_mapping->a_ops = &dyaop->da_op;
12698+
12699+out:
12700+ return err;
12701+}
12702+
b752ccd1
AM
12703+/*
12704+ * Is it safe to replace a_ops during the inode/file is in operation?
12705+ * Yes, I hope so.
12706+ */
12707+int au_dy_irefresh(struct inode *inode)
12708+{
12709+ int err;
5afbbe0d 12710+ aufs_bindex_t btop;
b752ccd1
AM
12711+ struct inode *h_inode;
12712+
12713+ err = 0;
12714+ if (S_ISREG(inode->i_mode)) {
5afbbe0d
AM
12715+ btop = au_ibtop(inode);
12716+ h_inode = au_h_iptr(inode, btop);
12717+ err = au_dy_iaop(inode, btop, h_inode);
b752ccd1
AM
12718+ }
12719+ return err;
12720+}
12721+
4a4d8108
AM
12722+void au_dy_arefresh(int do_dx)
12723+{
8b6a4947
AM
12724+ struct hlist_bl_head *hbl;
12725+ struct hlist_bl_node *pos;
4a4d8108
AM
12726+ struct au_dykey *key;
12727+
8b6a4947
AM
12728+ hbl = dynop + AuDy_AOP;
12729+ hlist_bl_lock(hbl);
12730+ hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
4a4d8108 12731+ dy_adx((void *)key, do_dx);
8b6a4947 12732+ hlist_bl_unlock(hbl);
4a4d8108
AM
12733+}
12734+
4a4d8108
AM
12735+/* ---------------------------------------------------------------------- */
12736+
12737+void __init au_dy_init(void)
12738+{
12739+ int i;
12740+
12741+ /* make sure that 'struct au_dykey *' can be any type */
12742+ BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
4a4d8108
AM
12743+
12744+ for (i = 0; i < AuDyLast; i++)
8b6a4947 12745+ INIT_HLIST_BL_HEAD(dynop + i);
4a4d8108
AM
12746+}
12747+
12748+void au_dy_fin(void)
12749+{
12750+ int i;
12751+
12752+ for (i = 0; i < AuDyLast; i++)
8b6a4947 12753+ WARN_ON(!hlist_bl_empty(dynop + i));
4a4d8108 12754+}
7f207e10
AM
12755diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12756--- /usr/share/empty/fs/aufs/dynop.h 1970-01-01 01:00:00.000000000 +0100
8b6a4947 12757+++ linux/fs/aufs/dynop.h 2017-11-12 22:24:44.704244405 +0100
7e9cd9fe 12758@@ -0,0 +1,74 @@
4a4d8108 12759+/*
a2654f78 12760+ * Copyright (C) 2010-2017 Junjiro R. Okajima
4a4d8108
AM
12761+ *
12762+ * This program, aufs is free software; you can redistribute it and/or modify
12763+ * it under the terms of the GNU General Public License as published by
12764+ * the Free Software Foundation; either version 2 of the License, or
12765+ * (at your option) any later version.
12766+ *
12767+ * This program is distributed in the hope that it will be useful,
12768+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12769+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12770+ * GNU General Public License for more details.
12771+ *
12772+ * You should have received a copy of the GNU General Public License
523b37e3 12773+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
12774+ */
12775+
12776+/*
12777+ * dynamically customizable operations (for regular files only)
12778+ */
12779+
12780+#ifndef __AUFS_DYNOP_H__
12781+#define __AUFS_DYNOP_H__
12782+
12783+#ifdef __KERNEL__
12784+
7e9cd9fe
AM
12785+#include <linux/fs.h>
12786+#include <linux/kref.h>
4a4d8108 12787+
2cbb1c4b 12788+enum {AuDy_AOP, AuDyLast};
4a4d8108
AM
12789+
12790+struct au_dynop {
12791+ int dy_type;
12792+ union {
12793+ const void *dy_hop;
12794+ const struct address_space_operations *dy_haop;
4a4d8108
AM
12795+ };
12796+};
12797+
12798+struct au_dykey {
12799+ union {
8b6a4947 12800+ struct hlist_bl_node dk_hnode;
4a4d8108
AM
12801+ struct rcu_head dk_rcu;
12802+ };
12803+ struct au_dynop dk_op;
12804+
12805+ /*
12806+ * during I am in the branch local array, kref is gotten. when the
12807+ * branch is removed, kref is put.
12808+ */
12809+ struct kref dk_kref;
12810+};
12811+
12812+/* stop unioning since their sizes are very different from each other */
12813+struct au_dyaop {
12814+ struct au_dykey da_key;
12815+ struct address_space_operations da_op; /* not const */
4a4d8108
AM
12816+};
12817+
4a4d8108
AM
12818+/* ---------------------------------------------------------------------- */
12819+
12820+/* dynop.c */
12821+struct au_branch;
12822+void au_dy_put(struct au_dykey *key);
12823+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12824+ struct inode *h_inode);
b752ccd1 12825+int au_dy_irefresh(struct inode *inode);
4a4d8108 12826+void au_dy_arefresh(int do_dio);
4a4d8108
AM
12827+
12828+void __init au_dy_init(void);
12829+void au_dy_fin(void);
12830+
4a4d8108
AM
12831+#endif /* __KERNEL__ */
12832+#endif /* __AUFS_DYNOP_H__ */
7f207e10
AM
12833diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12834--- /usr/share/empty/fs/aufs/export.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947 12835+++ linux/fs/aufs/export.c 2017-11-12 22:24:42.267510077 +0100
f2c43d5f 12836@@ -0,0 +1,836 @@
4a4d8108 12837+/*
a2654f78 12838+ * Copyright (C) 2005-2017 Junjiro R. Okajima
4a4d8108
AM
12839+ *
12840+ * This program, aufs is free software; you can redistribute it and/or modify
12841+ * it under the terms of the GNU General Public License as published by
12842+ * the Free Software Foundation; either version 2 of the License, or
12843+ * (at your option) any later version.
12844+ *
12845+ * This program is distributed in the hope that it will be useful,
12846+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12847+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12848+ * GNU General Public License for more details.
12849+ *
12850+ * You should have received a copy of the GNU General Public License
523b37e3 12851+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
12852+ */
12853+
12854+/*
12855+ * export via nfs
12856+ */
12857+
12858+#include <linux/exportfs.h>
7eafdf33 12859+#include <linux/fs_struct.h>
4a4d8108
AM
12860+#include <linux/namei.h>
12861+#include <linux/nsproxy.h>
12862+#include <linux/random.h>
12863+#include <linux/writeback.h>
12864+#include "aufs.h"
12865+
12866+union conv {
12867+#ifdef CONFIG_AUFS_INO_T_64
12868+ __u32 a[2];
12869+#else
12870+ __u32 a[1];
12871+#endif
12872+ ino_t ino;
12873+};
12874+
12875+static ino_t decode_ino(__u32 *a)
12876+{
12877+ union conv u;
12878+
12879+ BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12880+ u.a[0] = a[0];
12881+#ifdef CONFIG_AUFS_INO_T_64
12882+ u.a[1] = a[1];
12883+#endif
12884+ return u.ino;
12885+}
12886+
12887+static void encode_ino(__u32 *a, ino_t ino)
12888+{
12889+ union conv u;
12890+
12891+ u.ino = ino;
12892+ a[0] = u.a[0];
12893+#ifdef CONFIG_AUFS_INO_T_64
12894+ a[1] = u.a[1];
12895+#endif
12896+}
12897+
12898+/* NFS file handle */
12899+enum {
12900+ Fh_br_id,
12901+ Fh_sigen,
12902+#ifdef CONFIG_AUFS_INO_T_64
12903+ /* support 64bit inode number */
12904+ Fh_ino1,
12905+ Fh_ino2,
12906+ Fh_dir_ino1,
12907+ Fh_dir_ino2,
12908+#else
12909+ Fh_ino1,
12910+ Fh_dir_ino1,
12911+#endif
12912+ Fh_igen,
12913+ Fh_h_type,
12914+ Fh_tail,
12915+
12916+ Fh_ino = Fh_ino1,
12917+ Fh_dir_ino = Fh_dir_ino1
12918+};
12919+
12920+static int au_test_anon(struct dentry *dentry)
12921+{
027c5e7a 12922+ /* note: read d_flags without d_lock */
4a4d8108
AM
12923+ return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12924+}
12925+
a2a7ad62
AM
12926+int au_test_nfsd(void)
12927+{
12928+ int ret;
12929+ struct task_struct *tsk = current;
12930+ char comm[sizeof(tsk->comm)];
12931+
12932+ ret = 0;
12933+ if (tsk->flags & PF_KTHREAD) {
12934+ get_task_comm(comm, tsk);
12935+ ret = !strcmp(comm, "nfsd");
12936+ }
12937+
12938+ return ret;
12939+}
12940+
4a4d8108
AM
12941+/* ---------------------------------------------------------------------- */
12942+/* inode generation external table */
12943+
b752ccd1 12944+void au_xigen_inc(struct inode *inode)
4a4d8108 12945+{
4a4d8108
AM
12946+ loff_t pos;
12947+ ssize_t sz;
12948+ __u32 igen;
12949+ struct super_block *sb;
12950+ struct au_sbinfo *sbinfo;
12951+
4a4d8108 12952+ sb = inode->i_sb;
b752ccd1 12953+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
1facf9fc 12954+
b752ccd1 12955+ sbinfo = au_sbi(sb);
1facf9fc 12956+ pos = inode->i_ino;
12957+ pos *= sizeof(igen);
12958+ igen = inode->i_generation + 1;
1facf9fc 12959+ sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
12960+ sizeof(igen), &pos);
12961+ if (sz == sizeof(igen))
b752ccd1 12962+ return; /* success */
1facf9fc 12963+
b752ccd1 12964+ if (unlikely(sz >= 0))
1facf9fc 12965+ AuIOErr("xigen error (%zd)\n", sz);
1facf9fc 12966+}
12967+
12968+int au_xigen_new(struct inode *inode)
12969+{
12970+ int err;
12971+ loff_t pos;
12972+ ssize_t sz;
12973+ struct super_block *sb;
12974+ struct au_sbinfo *sbinfo;
12975+ struct file *file;
12976+
12977+ err = 0;
12978+ /* todo: dirty, at mount time */
12979+ if (inode->i_ino == AUFS_ROOT_INO)
12980+ goto out;
12981+ sb = inode->i_sb;
dece6358 12982+ SiMustAnyLock(sb);
1facf9fc 12983+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
12984+ goto out;
12985+
12986+ err = -EFBIG;
12987+ pos = inode->i_ino;
12988+ if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
12989+ AuIOErr1("too large i%lld\n", pos);
12990+ goto out;
12991+ }
12992+ pos *= sizeof(inode->i_generation);
12993+
12994+ err = 0;
12995+ sbinfo = au_sbi(sb);
12996+ file = sbinfo->si_xigen;
12997+ BUG_ON(!file);
12998+
c06a8ce3 12999+ if (vfsub_f_size_read(file)
1facf9fc 13000+ < pos + sizeof(inode->i_generation)) {
13001+ inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
13002+ sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
13003+ sizeof(inode->i_generation), &pos);
13004+ } else
13005+ sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
13006+ sizeof(inode->i_generation), &pos);
13007+ if (sz == sizeof(inode->i_generation))
13008+ goto out; /* success */
13009+
13010+ err = sz;
13011+ if (unlikely(sz >= 0)) {
13012+ err = -EIO;
13013+ AuIOErr("xigen error (%zd)\n", sz);
13014+ }
13015+
4f0767ce 13016+out:
1facf9fc 13017+ return err;
13018+}
13019+
13020+int au_xigen_set(struct super_block *sb, struct file *base)
13021+{
13022+ int err;
13023+ struct au_sbinfo *sbinfo;
13024+ struct file *file;
13025+
dece6358
AM
13026+ SiMustWriteLock(sb);
13027+
1facf9fc 13028+ sbinfo = au_sbi(sb);
13029+ file = au_xino_create2(base, sbinfo->si_xigen);
13030+ err = PTR_ERR(file);
13031+ if (IS_ERR(file))
13032+ goto out;
13033+ err = 0;
13034+ if (sbinfo->si_xigen)
13035+ fput(sbinfo->si_xigen);
13036+ sbinfo->si_xigen = file;
13037+
4f0767ce 13038+out:
1facf9fc 13039+ return err;
13040+}
13041+
13042+void au_xigen_clr(struct super_block *sb)
13043+{
13044+ struct au_sbinfo *sbinfo;
13045+
dece6358
AM
13046+ SiMustWriteLock(sb);
13047+
1facf9fc 13048+ sbinfo = au_sbi(sb);
13049+ if (sbinfo->si_xigen) {
13050+ fput(sbinfo->si_xigen);
13051+ sbinfo->si_xigen = NULL;
13052+ }
13053+}
13054+
13055+/* ---------------------------------------------------------------------- */
13056+
13057+static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13058+ ino_t dir_ino)
13059+{
13060+ struct dentry *dentry, *d;
13061+ struct inode *inode;
13062+ unsigned int sigen;
13063+
13064+ dentry = NULL;
13065+ inode = ilookup(sb, ino);
13066+ if (!inode)
13067+ goto out;
13068+
13069+ dentry = ERR_PTR(-ESTALE);
13070+ sigen = au_sigen(sb);
5afbbe0d 13071+ if (unlikely(au_is_bad_inode(inode)
1facf9fc 13072+ || IS_DEADDIR(inode)
537831f9 13073+ || sigen != au_iigen(inode, NULL)))
1facf9fc 13074+ goto out_iput;
13075+
13076+ dentry = NULL;
13077+ if (!dir_ino || S_ISDIR(inode->i_mode))
13078+ dentry = d_find_alias(inode);
13079+ else {
027c5e7a 13080+ spin_lock(&inode->i_lock);
c1595e42 13081+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 13082+ spin_lock(&d->d_lock);
1facf9fc 13083+ if (!au_test_anon(d)
5527c038 13084+ && d_inode(d->d_parent)->i_ino == dir_ino) {
027c5e7a
AM
13085+ dentry = dget_dlock(d);
13086+ spin_unlock(&d->d_lock);
1facf9fc 13087+ break;
13088+ }
027c5e7a
AM
13089+ spin_unlock(&d->d_lock);
13090+ }
13091+ spin_unlock(&inode->i_lock);
1facf9fc 13092+ }
027c5e7a 13093+ if (unlikely(dentry && au_digen_test(dentry, sigen))) {
2cbb1c4b 13094+ /* need to refresh */
1facf9fc 13095+ dput(dentry);
2cbb1c4b 13096+ dentry = NULL;
1facf9fc 13097+ }
13098+
4f0767ce 13099+out_iput:
1facf9fc 13100+ iput(inode);
4f0767ce 13101+out:
2cbb1c4b 13102+ AuTraceErrPtr(dentry);
1facf9fc 13103+ return dentry;
13104+}
13105+
13106+/* ---------------------------------------------------------------------- */
13107+
13108+/* todo: dirty? */
13109+/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
4a4d8108
AM
13110+
13111+struct au_compare_mnt_args {
13112+ /* input */
13113+ struct super_block *sb;
13114+
13115+ /* output */
13116+ struct vfsmount *mnt;
13117+};
13118+
13119+static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13120+{
13121+ struct au_compare_mnt_args *a = arg;
13122+
13123+ if (mnt->mnt_sb != a->sb)
13124+ return 0;
13125+ a->mnt = mntget(mnt);
13126+ return 1;
13127+}
13128+
1facf9fc 13129+static struct vfsmount *au_mnt_get(struct super_block *sb)
13130+{
4a4d8108 13131+ int err;
7eafdf33 13132+ struct path root;
4a4d8108
AM
13133+ struct au_compare_mnt_args args = {
13134+ .sb = sb
13135+ };
1facf9fc 13136+
7eafdf33 13137+ get_fs_root(current->fs, &root);
523b37e3 13138+ rcu_read_lock();
7eafdf33 13139+ err = iterate_mounts(au_compare_mnt, &args, root.mnt);
523b37e3 13140+ rcu_read_unlock();
7eafdf33 13141+ path_put(&root);
4a4d8108
AM
13142+ AuDebugOn(!err);
13143+ AuDebugOn(!args.mnt);
13144+ return args.mnt;
1facf9fc 13145+}
13146+
13147+struct au_nfsd_si_lock {
4a4d8108 13148+ unsigned int sigen;
027c5e7a 13149+ aufs_bindex_t bindex, br_id;
1facf9fc 13150+ unsigned char force_lock;
13151+};
13152+
027c5e7a
AM
13153+static int si_nfsd_read_lock(struct super_block *sb,
13154+ struct au_nfsd_si_lock *nsi_lock)
1facf9fc 13155+{
027c5e7a 13156+ int err;
1facf9fc 13157+ aufs_bindex_t bindex;
13158+
13159+ si_read_lock(sb, AuLock_FLUSH);
13160+
13161+ /* branch id may be wrapped around */
027c5e7a 13162+ err = 0;
1facf9fc 13163+ bindex = au_br_index(sb, nsi_lock->br_id);
13164+ if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13165+ goto out; /* success */
13166+
027c5e7a
AM
13167+ err = -ESTALE;
13168+ bindex = -1;
1facf9fc 13169+ if (!nsi_lock->force_lock)
13170+ si_read_unlock(sb);
1facf9fc 13171+
4f0767ce 13172+out:
027c5e7a
AM
13173+ nsi_lock->bindex = bindex;
13174+ return err;
1facf9fc 13175+}
13176+
13177+struct find_name_by_ino {
392086de 13178+ struct dir_context ctx;
1facf9fc 13179+ int called, found;
13180+ ino_t ino;
13181+ char *name;
13182+ int namelen;
13183+};
13184+
13185+static int
392086de
AM
13186+find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13187+ loff_t offset, u64 ino, unsigned int d_type)
1facf9fc 13188+{
392086de
AM
13189+ struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13190+ ctx);
1facf9fc 13191+
13192+ a->called++;
13193+ if (a->ino != ino)
13194+ return 0;
13195+
13196+ memcpy(a->name, name, namelen);
13197+ a->namelen = namelen;
13198+ a->found = 1;
13199+ return 1;
13200+}
13201+
13202+static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13203+ struct au_nfsd_si_lock *nsi_lock)
13204+{
13205+ struct dentry *dentry, *parent;
13206+ struct file *file;
13207+ struct inode *dir;
392086de
AM
13208+ struct find_name_by_ino arg = {
13209+ .ctx = {
2000de60 13210+ .actor = find_name_by_ino
392086de
AM
13211+ }
13212+ };
1facf9fc 13213+ int err;
13214+
13215+ parent = path->dentry;
13216+ if (nsi_lock)
13217+ si_read_unlock(parent->d_sb);
4a4d8108 13218+ file = vfsub_dentry_open(path, au_dir_roflags);
1facf9fc 13219+ dentry = (void *)file;
13220+ if (IS_ERR(file))
13221+ goto out;
13222+
13223+ dentry = ERR_PTR(-ENOMEM);
537831f9 13224+ arg.name = (void *)__get_free_page(GFP_NOFS);
1facf9fc 13225+ if (unlikely(!arg.name))
13226+ goto out_file;
13227+ arg.ino = ino;
13228+ arg.found = 0;
13229+ do {
13230+ arg.called = 0;
13231+ /* smp_mb(); */
392086de 13232+ err = vfsub_iterate_dir(file, &arg.ctx);
1facf9fc 13233+ } while (!err && !arg.found && arg.called);
13234+ dentry = ERR_PTR(err);
13235+ if (unlikely(err))
13236+ goto out_name;
1716fcea
AM
13237+ /* instead of ENOENT */
13238+ dentry = ERR_PTR(-ESTALE);
1facf9fc 13239+ if (!arg.found)
13240+ goto out_name;
13241+
b4510431 13242+ /* do not call vfsub_lkup_one() */
5527c038 13243+ dir = d_inode(parent);
febd17d6 13244+ dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
1facf9fc 13245+ AuTraceErrPtr(dentry);
13246+ if (IS_ERR(dentry))
13247+ goto out_name;
13248+ AuDebugOn(au_test_anon(dentry));
5527c038 13249+ if (unlikely(d_really_is_negative(dentry))) {
1facf9fc 13250+ dput(dentry);
13251+ dentry = ERR_PTR(-ENOENT);
13252+ }
13253+
4f0767ce 13254+out_name:
1c60b727 13255+ free_page((unsigned long)arg.name);
4f0767ce 13256+out_file:
1facf9fc 13257+ fput(file);
4f0767ce 13258+out:
1facf9fc 13259+ if (unlikely(nsi_lock
13260+ && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13261+ if (!IS_ERR(dentry)) {
13262+ dput(dentry);
13263+ dentry = ERR_PTR(-ESTALE);
13264+ }
13265+ AuTraceErrPtr(dentry);
13266+ return dentry;
13267+}
13268+
13269+static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13270+ ino_t dir_ino,
13271+ struct au_nfsd_si_lock *nsi_lock)
13272+{
13273+ struct dentry *dentry;
13274+ struct path path;
13275+
13276+ if (dir_ino != AUFS_ROOT_INO) {
13277+ path.dentry = decode_by_ino(sb, dir_ino, 0);
13278+ dentry = path.dentry;
13279+ if (!path.dentry || IS_ERR(path.dentry))
13280+ goto out;
13281+ AuDebugOn(au_test_anon(path.dentry));
13282+ } else
13283+ path.dentry = dget(sb->s_root);
13284+
13285+ path.mnt = au_mnt_get(sb);
13286+ dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13287+ path_put(&path);
13288+
4f0767ce 13289+out:
1facf9fc 13290+ AuTraceErrPtr(dentry);
13291+ return dentry;
13292+}
13293+
13294+/* ---------------------------------------------------------------------- */
13295+
13296+static int h_acceptable(void *expv, struct dentry *dentry)
13297+{
13298+ return 1;
13299+}
13300+
13301+static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13302+ char *buf, int len, struct super_block *sb)
13303+{
13304+ char *p;
13305+ int n;
13306+ struct path path;
13307+
13308+ p = d_path(h_rootpath, buf, len);
13309+ if (IS_ERR(p))
13310+ goto out;
13311+ n = strlen(p);
13312+
13313+ path.mnt = h_rootpath->mnt;
13314+ path.dentry = h_parent;
13315+ p = d_path(&path, buf, len);
13316+ if (IS_ERR(p))
13317+ goto out;
13318+ if (n != 1)
13319+ p += n;
13320+
13321+ path.mnt = au_mnt_get(sb);
13322+ path.dentry = sb->s_root;
13323+ p = d_path(&path, buf, len - strlen(p));
13324+ mntput(path.mnt);
13325+ if (IS_ERR(p))
13326+ goto out;
13327+ if (n != 1)
13328+ p[strlen(p)] = '/';
13329+
4f0767ce 13330+out:
1facf9fc 13331+ AuTraceErrPtr(p);
13332+ return p;
13333+}
13334+
13335+static
027c5e7a
AM
13336+struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13337+ int fh_len, struct au_nfsd_si_lock *nsi_lock)
1facf9fc 13338+{
13339+ struct dentry *dentry, *h_parent, *root;
13340+ struct super_block *h_sb;
13341+ char *pathname, *p;
13342+ struct vfsmount *h_mnt;
13343+ struct au_branch *br;
13344+ int err;
13345+ struct path path;
13346+
027c5e7a 13347+ br = au_sbr(sb, nsi_lock->bindex);
86dc4139 13348+ h_mnt = au_br_mnt(br);
1facf9fc 13349+ h_sb = h_mnt->mnt_sb;
13350+ /* todo: call lower fh_to_dentry()? fh_to_parent()? */
5afbbe0d 13351+ lockdep_off();
1facf9fc 13352+ h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13353+ fh_len - Fh_tail, fh[Fh_h_type],
13354+ h_acceptable, /*context*/NULL);
5afbbe0d 13355+ lockdep_on();
1facf9fc 13356+ dentry = h_parent;
13357+ if (unlikely(!h_parent || IS_ERR(h_parent))) {
13358+ AuWarn1("%s decode_fh failed, %ld\n",
13359+ au_sbtype(h_sb), PTR_ERR(h_parent));
13360+ goto out;
13361+ }
13362+ dentry = NULL;
13363+ if (unlikely(au_test_anon(h_parent))) {
13364+ AuWarn1("%s decode_fh returned a disconnected dentry\n",
13365+ au_sbtype(h_sb));
13366+ goto out_h_parent;
13367+ }
13368+
13369+ dentry = ERR_PTR(-ENOMEM);
13370+ pathname = (void *)__get_free_page(GFP_NOFS);
13371+ if (unlikely(!pathname))
13372+ goto out_h_parent;
13373+
13374+ root = sb->s_root;
13375+ path.mnt = h_mnt;
13376+ di_read_lock_parent(root, !AuLock_IR);
027c5e7a 13377+ path.dentry = au_h_dptr(root, nsi_lock->bindex);
1facf9fc 13378+ di_read_unlock(root, !AuLock_IR);
13379+ p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13380+ dentry = (void *)p;
13381+ if (IS_ERR(p))
13382+ goto out_pathname;
13383+
13384+ si_read_unlock(sb);
13385+ err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13386+ dentry = ERR_PTR(err);
13387+ if (unlikely(err))
13388+ goto out_relock;
13389+
13390+ dentry = ERR_PTR(-ENOENT);
13391+ AuDebugOn(au_test_anon(path.dentry));
5527c038 13392+ if (unlikely(d_really_is_negative(path.dentry)))
1facf9fc 13393+ goto out_path;
13394+
5527c038 13395+ if (ino != d_inode(path.dentry)->i_ino)
1facf9fc 13396+ dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13397+ else
13398+ dentry = dget(path.dentry);
13399+
4f0767ce 13400+out_path:
1facf9fc 13401+ path_put(&path);
4f0767ce 13402+out_relock:
1facf9fc 13403+ if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13404+ if (!IS_ERR(dentry)) {
13405+ dput(dentry);
13406+ dentry = ERR_PTR(-ESTALE);
13407+ }
4f0767ce 13408+out_pathname:
1c60b727 13409+ free_page((unsigned long)pathname);
4f0767ce 13410+out_h_parent:
1facf9fc 13411+ dput(h_parent);
4f0767ce 13412+out:
1facf9fc 13413+ AuTraceErrPtr(dentry);
13414+ return dentry;
13415+}
13416+
13417+/* ---------------------------------------------------------------------- */
13418+
13419+static struct dentry *
13420+aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13421+ int fh_type)
13422+{
13423+ struct dentry *dentry;
13424+ __u32 *fh = fid->raw;
027c5e7a 13425+ struct au_branch *br;
1facf9fc 13426+ ino_t ino, dir_ino;
1facf9fc 13427+ struct au_nfsd_si_lock nsi_lock = {
1facf9fc 13428+ .force_lock = 0
13429+ };
13430+
1facf9fc 13431+ dentry = ERR_PTR(-ESTALE);
4a4d8108
AM
13432+ /* it should never happen, but the file handle is unreliable */
13433+ if (unlikely(fh_len < Fh_tail))
13434+ goto out;
13435+ nsi_lock.sigen = fh[Fh_sigen];
13436+ nsi_lock.br_id = fh[Fh_br_id];
13437+
1facf9fc 13438+ /* branch id may be wrapped around */
027c5e7a
AM
13439+ br = NULL;
13440+ if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
1facf9fc 13441+ goto out;
13442+ nsi_lock.force_lock = 1;
13443+
13444+ /* is this inode still cached? */
13445+ ino = decode_ino(fh + Fh_ino);
4a4d8108
AM
13446+ /* it should never happen */
13447+ if (unlikely(ino == AUFS_ROOT_INO))
8cdd5066 13448+ goto out_unlock;
4a4d8108 13449+
1facf9fc 13450+ dir_ino = decode_ino(fh + Fh_dir_ino);
13451+ dentry = decode_by_ino(sb, ino, dir_ino);
13452+ if (IS_ERR(dentry))
13453+ goto out_unlock;
13454+ if (dentry)
13455+ goto accept;
13456+
13457+ /* is the parent dir cached? */
027c5e7a 13458+ br = au_sbr(sb, nsi_lock.bindex);
5afbbe0d 13459+ au_br_get(br);
1facf9fc 13460+ dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13461+ if (IS_ERR(dentry))
13462+ goto out_unlock;
13463+ if (dentry)
13464+ goto accept;
13465+
13466+ /* lookup path */
027c5e7a 13467+ dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
1facf9fc 13468+ if (IS_ERR(dentry))
13469+ goto out_unlock;
13470+ if (unlikely(!dentry))
13471+ /* todo?: make it ESTALE */
13472+ goto out_unlock;
13473+
4f0767ce 13474+accept:
027c5e7a 13475+ if (!au_digen_test(dentry, au_sigen(sb))
5527c038 13476+ && d_inode(dentry)->i_generation == fh[Fh_igen])
1facf9fc 13477+ goto out_unlock; /* success */
13478+
13479+ dput(dentry);
13480+ dentry = ERR_PTR(-ESTALE);
4f0767ce 13481+out_unlock:
027c5e7a 13482+ if (br)
5afbbe0d 13483+ au_br_put(br);
1facf9fc 13484+ si_read_unlock(sb);
4f0767ce 13485+out:
1facf9fc 13486+ AuTraceErrPtr(dentry);
13487+ return dentry;
13488+}
13489+
13490+#if 0 /* reserved for future use */
13491+/* support subtreecheck option */
13492+static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13493+ int fh_len, int fh_type)
13494+{
13495+ struct dentry *parent;
13496+ __u32 *fh = fid->raw;
13497+ ino_t dir_ino;
13498+
13499+ dir_ino = decode_ino(fh + Fh_dir_ino);
13500+ parent = decode_by_ino(sb, dir_ino, 0);
13501+ if (IS_ERR(parent))
13502+ goto out;
13503+ if (!parent)
13504+ parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13505+ dir_ino, fh, fh_len);
13506+
4f0767ce 13507+out:
1facf9fc 13508+ AuTraceErrPtr(parent);
13509+ return parent;
13510+}
13511+#endif
13512+
13513+/* ---------------------------------------------------------------------- */
13514+
0c3ec466
AM
13515+static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13516+ struct inode *dir)
1facf9fc 13517+{
13518+ int err;
0c3ec466 13519+ aufs_bindex_t bindex;
1facf9fc 13520+ struct super_block *sb, *h_sb;
0c3ec466
AM
13521+ struct dentry *dentry, *parent, *h_parent;
13522+ struct inode *h_dir;
1facf9fc 13523+ struct au_branch *br;
13524+
1facf9fc 13525+ err = -ENOSPC;
13526+ if (unlikely(*max_len <= Fh_tail)) {
13527+ AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13528+ goto out;
13529+ }
13530+
13531+ err = FILEID_ROOT;
0c3ec466
AM
13532+ if (inode->i_ino == AUFS_ROOT_INO) {
13533+ AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
1facf9fc 13534+ goto out;
13535+ }
13536+
1facf9fc 13537+ h_parent = NULL;
0c3ec466
AM
13538+ sb = inode->i_sb;
13539+ err = si_read_lock(sb, AuLock_FLUSH);
027c5e7a
AM
13540+ if (unlikely(err))
13541+ goto out;
13542+
1facf9fc 13543+#ifdef CONFIG_AUFS_DEBUG
13544+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13545+ AuWarn1("NFS-exporting requires xino\n");
13546+#endif
027c5e7a 13547+ err = -EIO;
0c3ec466
AM
13548+ parent = NULL;
13549+ ii_read_lock_child(inode);
5afbbe0d 13550+ bindex = au_ibtop(inode);
0c3ec466 13551+ if (!dir) {
c1595e42 13552+ dentry = d_find_any_alias(inode);
0c3ec466
AM
13553+ if (unlikely(!dentry))
13554+ goto out_unlock;
13555+ AuDebugOn(au_test_anon(dentry));
13556+ parent = dget_parent(dentry);
13557+ dput(dentry);
13558+ if (unlikely(!parent))
13559+ goto out_unlock;
5527c038
JR
13560+ if (d_really_is_positive(parent))
13561+ dir = d_inode(parent);
1facf9fc 13562+ }
0c3ec466
AM
13563+
13564+ ii_read_lock_parent(dir);
13565+ h_dir = au_h_iptr(dir, bindex);
13566+ ii_read_unlock(dir);
13567+ if (unlikely(!h_dir))
13568+ goto out_parent;
c1595e42 13569+ h_parent = d_find_any_alias(h_dir);
1facf9fc 13570+ if (unlikely(!h_parent))
0c3ec466 13571+ goto out_hparent;
1facf9fc 13572+
13573+ err = -EPERM;
13574+ br = au_sbr(sb, bindex);
86dc4139 13575+ h_sb = au_br_sb(br);
1facf9fc 13576+ if (unlikely(!h_sb->s_export_op)) {
13577+ AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
0c3ec466 13578+ goto out_hparent;
1facf9fc 13579+ }
13580+
13581+ fh[Fh_br_id] = br->br_id;
13582+ fh[Fh_sigen] = au_sigen(sb);
13583+ encode_ino(fh + Fh_ino, inode->i_ino);
0c3ec466 13584+ encode_ino(fh + Fh_dir_ino, dir->i_ino);
1facf9fc 13585+ fh[Fh_igen] = inode->i_generation;
13586+
13587+ *max_len -= Fh_tail;
13588+ fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13589+ max_len,
13590+ /*connectable or subtreecheck*/0);
13591+ err = fh[Fh_h_type];
13592+ *max_len += Fh_tail;
13593+ /* todo: macros? */
1716fcea 13594+ if (err != FILEID_INVALID)
1facf9fc 13595+ err = 99;
13596+ else
13597+ AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13598+
0c3ec466 13599+out_hparent:
1facf9fc 13600+ dput(h_parent);
0c3ec466 13601+out_parent:
1facf9fc 13602+ dput(parent);
0c3ec466
AM
13603+out_unlock:
13604+ ii_read_unlock(inode);
13605+ si_read_unlock(sb);
4f0767ce 13606+out:
1facf9fc 13607+ if (unlikely(err < 0))
1716fcea 13608+ err = FILEID_INVALID;
1facf9fc 13609+ return err;
13610+}
13611+
13612+/* ---------------------------------------------------------------------- */
13613+
4a4d8108
AM
13614+static int aufs_commit_metadata(struct inode *inode)
13615+{
13616+ int err;
13617+ aufs_bindex_t bindex;
13618+ struct super_block *sb;
13619+ struct inode *h_inode;
13620+ int (*f)(struct inode *inode);
13621+
13622+ sb = inode->i_sb;
e49829fe 13623+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 13624+ ii_write_lock_child(inode);
5afbbe0d 13625+ bindex = au_ibtop(inode);
4a4d8108
AM
13626+ AuDebugOn(bindex < 0);
13627+ h_inode = au_h_iptr(inode, bindex);
13628+
13629+ f = h_inode->i_sb->s_export_op->commit_metadata;
13630+ if (f)
13631+ err = f(h_inode);
13632+ else {
13633+ struct writeback_control wbc = {
13634+ .sync_mode = WB_SYNC_ALL,
13635+ .nr_to_write = 0 /* metadata only */
13636+ };
13637+
13638+ err = sync_inode(h_inode, &wbc);
13639+ }
13640+
13641+ au_cpup_attr_timesizes(inode);
13642+ ii_write_unlock(inode);
13643+ si_read_unlock(sb);
13644+ return err;
13645+}
13646+
13647+/* ---------------------------------------------------------------------- */
13648+
1facf9fc 13649+static struct export_operations aufs_export_op = {
4a4d8108 13650+ .fh_to_dentry = aufs_fh_to_dentry,
1facf9fc 13651+ /* .fh_to_parent = aufs_fh_to_parent, */
4a4d8108
AM
13652+ .encode_fh = aufs_encode_fh,
13653+ .commit_metadata = aufs_commit_metadata
1facf9fc 13654+};
13655+
13656+void au_export_init(struct super_block *sb)
13657+{
13658+ struct au_sbinfo *sbinfo;
13659+ __u32 u;
13660+
5afbbe0d
AM
13661+ BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13662+ && IS_MODULE(CONFIG_EXPORTFS),
13663+ AUFS_NAME ": unsupported configuration "
13664+ "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13665+
1facf9fc 13666+ sb->s_export_op = &aufs_export_op;
13667+ sbinfo = au_sbi(sb);
13668+ sbinfo->si_xigen = NULL;
13669+ get_random_bytes(&u, sizeof(u));
13670+ BUILD_BUG_ON(sizeof(u) != sizeof(int));
13671+ atomic_set(&sbinfo->si_xigen_next, u);
13672+}
076b876e
AM
13673diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13674--- /usr/share/empty/fs/aufs/fhsm.c 1970-01-01 01:00:00.000000000 +0100
1c60b727 13675+++ linux/fs/aufs/fhsm.c 2017-07-29 12:14:25.903042072 +0200
c1595e42 13676@@ -0,0 +1,426 @@
076b876e 13677+/*
a2654f78 13678+ * Copyright (C) 2011-2017 Junjiro R. Okajima
076b876e
AM
13679+ *
13680+ * This program, aufs is free software; you can redistribute it and/or modify
13681+ * it under the terms of the GNU General Public License as published by
13682+ * the Free Software Foundation; either version 2 of the License, or
13683+ * (at your option) any later version.
13684+ *
13685+ * This program is distributed in the hope that it will be useful,
13686+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13687+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13688+ * GNU General Public License for more details.
13689+ *
13690+ * You should have received a copy of the GNU General Public License
13691+ * along with this program; if not, write to the Free Software
13692+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
13693+ */
13694+
13695+/*
13696+ * File-based Hierarchy Storage Management
13697+ */
13698+
13699+#include <linux/anon_inodes.h>
13700+#include <linux/poll.h>
13701+#include <linux/seq_file.h>
13702+#include <linux/statfs.h>
13703+#include "aufs.h"
13704+
c1595e42
JR
13705+static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13706+{
13707+ struct au_sbinfo *sbinfo;
13708+ struct au_fhsm *fhsm;
13709+
13710+ SiMustAnyLock(sb);
13711+
13712+ sbinfo = au_sbi(sb);
13713+ fhsm = &sbinfo->si_fhsm;
13714+ AuDebugOn(!fhsm);
13715+ return fhsm->fhsm_bottom;
13716+}
13717+
13718+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13719+{
13720+ struct au_sbinfo *sbinfo;
13721+ struct au_fhsm *fhsm;
13722+
13723+ SiMustWriteLock(sb);
13724+
13725+ sbinfo = au_sbi(sb);
13726+ fhsm = &sbinfo->si_fhsm;
13727+ AuDebugOn(!fhsm);
13728+ fhsm->fhsm_bottom = bindex;
13729+}
13730+
13731+/* ---------------------------------------------------------------------- */
13732+
076b876e
AM
13733+static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13734+{
13735+ struct au_br_fhsm *bf;
13736+
13737+ bf = br->br_fhsm;
13738+ MtxMustLock(&bf->bf_lock);
13739+
13740+ return !bf->bf_readable
13741+ || time_after(jiffies,
13742+ bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13743+}
13744+
13745+/* ---------------------------------------------------------------------- */
13746+
13747+static void au_fhsm_notify(struct super_block *sb, int val)
13748+{
13749+ struct au_sbinfo *sbinfo;
13750+ struct au_fhsm *fhsm;
13751+
13752+ SiMustAnyLock(sb);
13753+
13754+ sbinfo = au_sbi(sb);
13755+ fhsm = &sbinfo->si_fhsm;
13756+ if (au_fhsm_pid(fhsm)
13757+ && atomic_read(&fhsm->fhsm_readable) != -1) {
13758+ atomic_set(&fhsm->fhsm_readable, val);
13759+ if (val)
13760+ wake_up(&fhsm->fhsm_wqh);
13761+ }
13762+}
13763+
13764+static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13765+ struct aufs_stfs *rstfs, int do_lock, int do_notify)
13766+{
13767+ int err;
13768+ struct au_branch *br;
13769+ struct au_br_fhsm *bf;
13770+
13771+ br = au_sbr(sb, bindex);
13772+ AuDebugOn(au_br_rdonly(br));
13773+ bf = br->br_fhsm;
13774+ AuDebugOn(!bf);
13775+
13776+ if (do_lock)
13777+ mutex_lock(&bf->bf_lock);
13778+ else
13779+ MtxMustLock(&bf->bf_lock);
13780+
13781+ /* sb->s_root for NFS is unreliable */
13782+ err = au_br_stfs(br, &bf->bf_stfs);
13783+ if (unlikely(err)) {
13784+ AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13785+ goto out;
13786+ }
13787+
13788+ bf->bf_jiffy = jiffies;
13789+ bf->bf_readable = 1;
13790+ if (do_notify)
13791+ au_fhsm_notify(sb, /*val*/1);
13792+ if (rstfs)
13793+ *rstfs = bf->bf_stfs;
13794+
13795+out:
13796+ if (do_lock)
13797+ mutex_unlock(&bf->bf_lock);
13798+ au_fhsm_notify(sb, /*val*/1);
13799+
13800+ return err;
13801+}
13802+
13803+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13804+{
13805+ int err;
076b876e
AM
13806+ struct au_sbinfo *sbinfo;
13807+ struct au_fhsm *fhsm;
13808+ struct au_branch *br;
13809+ struct au_br_fhsm *bf;
13810+
13811+ AuDbg("b%d, force %d\n", bindex, force);
13812+ SiMustAnyLock(sb);
13813+
13814+ sbinfo = au_sbi(sb);
13815+ fhsm = &sbinfo->si_fhsm;
c1595e42
JR
13816+ if (!au_ftest_si(sbinfo, FHSM)
13817+ || fhsm->fhsm_bottom == bindex)
076b876e
AM
13818+ return;
13819+
13820+ br = au_sbr(sb, bindex);
13821+ bf = br->br_fhsm;
13822+ AuDebugOn(!bf);
13823+ mutex_lock(&bf->bf_lock);
13824+ if (force
13825+ || au_fhsm_pid(fhsm)
13826+ || au_fhsm_test_jiffy(sbinfo, br))
13827+ err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13828+ /*do_notify*/1);
13829+ mutex_unlock(&bf->bf_lock);
13830+}
13831+
13832+void au_fhsm_wrote_all(struct super_block *sb, int force)
13833+{
5afbbe0d 13834+ aufs_bindex_t bindex, bbot;
076b876e
AM
13835+ struct au_branch *br;
13836+
13837+ /* exclude the bottom */
5afbbe0d
AM
13838+ bbot = au_fhsm_bottom(sb);
13839+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
13840+ br = au_sbr(sb, bindex);
13841+ if (au_br_fhsm(br->br_perm))
13842+ au_fhsm_wrote(sb, bindex, force);
13843+ }
13844+}
13845+
13846+/* ---------------------------------------------------------------------- */
13847+
13848+static unsigned int au_fhsm_poll(struct file *file,
13849+ struct poll_table_struct *wait)
13850+{
13851+ unsigned int mask;
13852+ struct au_sbinfo *sbinfo;
13853+ struct au_fhsm *fhsm;
13854+
13855+ mask = 0;
13856+ sbinfo = file->private_data;
13857+ fhsm = &sbinfo->si_fhsm;
13858+ poll_wait(file, &fhsm->fhsm_wqh, wait);
13859+ if (atomic_read(&fhsm->fhsm_readable))
13860+ mask = POLLIN /* | POLLRDNORM */;
13861+
13862+ AuTraceErr((int)mask);
13863+ return mask;
13864+}
13865+
13866+static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13867+ struct aufs_stfs *stfs, __s16 brid)
13868+{
13869+ int err;
13870+
13871+ err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13872+ if (!err)
13873+ err = __put_user(brid, &stbr->brid);
13874+ if (unlikely(err))
13875+ err = -EFAULT;
13876+
13877+ return err;
13878+}
13879+
13880+static ssize_t au_fhsm_do_read(struct super_block *sb,
13881+ struct aufs_stbr __user *stbr, size_t count)
13882+{
13883+ ssize_t err;
13884+ int nstbr;
5afbbe0d 13885+ aufs_bindex_t bindex, bbot;
076b876e
AM
13886+ struct au_branch *br;
13887+ struct au_br_fhsm *bf;
13888+
13889+ /* except the bottom branch */
13890+ err = 0;
13891+ nstbr = 0;
5afbbe0d
AM
13892+ bbot = au_fhsm_bottom(sb);
13893+ for (bindex = 0; !err && bindex < bbot; bindex++) {
076b876e
AM
13894+ br = au_sbr(sb, bindex);
13895+ if (!au_br_fhsm(br->br_perm))
13896+ continue;
13897+
13898+ bf = br->br_fhsm;
13899+ mutex_lock(&bf->bf_lock);
13900+ if (bf->bf_readable) {
13901+ err = -EFAULT;
13902+ if (count >= sizeof(*stbr))
13903+ err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13904+ br->br_id);
13905+ if (!err) {
13906+ bf->bf_readable = 0;
13907+ count -= sizeof(*stbr);
13908+ nstbr++;
13909+ }
13910+ }
13911+ mutex_unlock(&bf->bf_lock);
13912+ }
13913+ if (!err)
13914+ err = sizeof(*stbr) * nstbr;
13915+
13916+ return err;
13917+}
13918+
13919+static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13920+ loff_t *pos)
13921+{
13922+ ssize_t err;
13923+ int readable;
5afbbe0d 13924+ aufs_bindex_t nfhsm, bindex, bbot;
076b876e
AM
13925+ struct au_sbinfo *sbinfo;
13926+ struct au_fhsm *fhsm;
13927+ struct au_branch *br;
13928+ struct super_block *sb;
13929+
13930+ err = 0;
13931+ sbinfo = file->private_data;
13932+ fhsm = &sbinfo->si_fhsm;
13933+need_data:
13934+ spin_lock_irq(&fhsm->fhsm_wqh.lock);
13935+ if (!atomic_read(&fhsm->fhsm_readable)) {
13936+ if (vfsub_file_flags(file) & O_NONBLOCK)
13937+ err = -EAGAIN;
13938+ else
13939+ err = wait_event_interruptible_locked_irq
13940+ (fhsm->fhsm_wqh,
13941+ atomic_read(&fhsm->fhsm_readable));
13942+ }
13943+ spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13944+ if (unlikely(err))
13945+ goto out;
13946+
13947+ /* sb may already be dead */
13948+ au_rw_read_lock(&sbinfo->si_rwsem);
13949+ readable = atomic_read(&fhsm->fhsm_readable);
13950+ if (readable > 0) {
13951+ sb = sbinfo->si_sb;
13952+ AuDebugOn(!sb);
13953+ /* exclude the bottom branch */
13954+ nfhsm = 0;
5afbbe0d
AM
13955+ bbot = au_fhsm_bottom(sb);
13956+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
13957+ br = au_sbr(sb, bindex);
13958+ if (au_br_fhsm(br->br_perm))
13959+ nfhsm++;
13960+ }
13961+ err = -EMSGSIZE;
13962+ if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13963+ atomic_set(&fhsm->fhsm_readable, 0);
13964+ err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13965+ count);
13966+ }
13967+ }
13968+ au_rw_read_unlock(&sbinfo->si_rwsem);
13969+ if (!readable)
13970+ goto need_data;
13971+
13972+out:
13973+ return err;
13974+}
13975+
13976+static int au_fhsm_release(struct inode *inode, struct file *file)
13977+{
13978+ struct au_sbinfo *sbinfo;
13979+ struct au_fhsm *fhsm;
13980+
13981+ /* sb may already be dead */
13982+ sbinfo = file->private_data;
13983+ fhsm = &sbinfo->si_fhsm;
13984+ spin_lock(&fhsm->fhsm_spin);
13985+ fhsm->fhsm_pid = 0;
13986+ spin_unlock(&fhsm->fhsm_spin);
13987+ kobject_put(&sbinfo->si_kobj);
13988+
13989+ return 0;
13990+}
13991+
13992+static const struct file_operations au_fhsm_fops = {
13993+ .owner = THIS_MODULE,
13994+ .llseek = noop_llseek,
13995+ .read = au_fhsm_read,
13996+ .poll = au_fhsm_poll,
13997+ .release = au_fhsm_release
13998+};
13999+
14000+int au_fhsm_fd(struct super_block *sb, int oflags)
14001+{
14002+ int err, fd;
14003+ struct au_sbinfo *sbinfo;
14004+ struct au_fhsm *fhsm;
14005+
14006+ err = -EPERM;
14007+ if (unlikely(!capable(CAP_SYS_ADMIN)))
14008+ goto out;
14009+
14010+ err = -EINVAL;
14011+ if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
14012+ goto out;
14013+
14014+ err = 0;
14015+ sbinfo = au_sbi(sb);
14016+ fhsm = &sbinfo->si_fhsm;
14017+ spin_lock(&fhsm->fhsm_spin);
14018+ if (!fhsm->fhsm_pid)
14019+ fhsm->fhsm_pid = current->pid;
14020+ else
14021+ err = -EBUSY;
14022+ spin_unlock(&fhsm->fhsm_spin);
14023+ if (unlikely(err))
14024+ goto out;
14025+
14026+ oflags |= O_RDONLY;
14027+ /* oflags |= FMODE_NONOTIFY; */
14028+ fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
14029+ err = fd;
14030+ if (unlikely(fd < 0))
14031+ goto out_pid;
14032+
14033+ /* succeed reglardless 'fhsm' status */
14034+ kobject_get(&sbinfo->si_kobj);
14035+ si_noflush_read_lock(sb);
14036+ if (au_ftest_si(sbinfo, FHSM))
14037+ au_fhsm_wrote_all(sb, /*force*/0);
14038+ si_read_unlock(sb);
14039+ goto out; /* success */
14040+
14041+out_pid:
14042+ spin_lock(&fhsm->fhsm_spin);
14043+ fhsm->fhsm_pid = 0;
14044+ spin_unlock(&fhsm->fhsm_spin);
14045+out:
14046+ AuTraceErr(err);
14047+ return err;
14048+}
14049+
14050+/* ---------------------------------------------------------------------- */
14051+
14052+int au_fhsm_br_alloc(struct au_branch *br)
14053+{
14054+ int err;
14055+
14056+ err = 0;
14057+ br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14058+ if (br->br_fhsm)
14059+ au_br_fhsm_init(br->br_fhsm);
14060+ else
14061+ err = -ENOMEM;
14062+
14063+ return err;
14064+}
14065+
14066+/* ---------------------------------------------------------------------- */
14067+
14068+void au_fhsm_fin(struct super_block *sb)
14069+{
14070+ au_fhsm_notify(sb, /*val*/-1);
14071+}
14072+
14073+void au_fhsm_init(struct au_sbinfo *sbinfo)
14074+{
14075+ struct au_fhsm *fhsm;
14076+
14077+ fhsm = &sbinfo->si_fhsm;
14078+ spin_lock_init(&fhsm->fhsm_spin);
14079+ init_waitqueue_head(&fhsm->fhsm_wqh);
14080+ atomic_set(&fhsm->fhsm_readable, 0);
14081+ fhsm->fhsm_expire
14082+ = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
c1595e42 14083+ fhsm->fhsm_bottom = -1;
076b876e
AM
14084+}
14085+
14086+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14087+{
14088+ sbinfo->si_fhsm.fhsm_expire
14089+ = msecs_to_jiffies(sec * MSEC_PER_SEC);
14090+}
14091+
14092+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14093+{
14094+ unsigned int u;
14095+
14096+ if (!au_ftest_si(sbinfo, FHSM))
14097+ return;
14098+
14099+ u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14100+ if (u != AUFS_FHSM_CACHE_DEF_SEC)
14101+ seq_printf(seq, ",fhsm_sec=%u", u);
14102+}
7f207e10
AM
14103diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14104--- /usr/share/empty/fs/aufs/file.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
14105+++ linux/fs/aufs/file.c 2017-11-12 22:24:44.704244405 +0100
14106@@ -0,0 +1,856 @@
1facf9fc 14107+/*
a2654f78 14108+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 14109+ *
14110+ * This program, aufs is free software; you can redistribute it and/or modify
14111+ * it under the terms of the GNU General Public License as published by
14112+ * the Free Software Foundation; either version 2 of the License, or
14113+ * (at your option) any later version.
dece6358
AM
14114+ *
14115+ * This program is distributed in the hope that it will be useful,
14116+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14117+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14118+ * GNU General Public License for more details.
14119+ *
14120+ * You should have received a copy of the GNU General Public License
523b37e3 14121+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 14122+ */
14123+
14124+/*
4a4d8108 14125+ * handling file/dir, and address_space operation
1facf9fc 14126+ */
14127+
7eafdf33
AM
14128+#ifdef CONFIG_AUFS_DEBUG
14129+#include <linux/migrate.h>
14130+#endif
4a4d8108 14131+#include <linux/pagemap.h>
1facf9fc 14132+#include "aufs.h"
14133+
4a4d8108
AM
14134+/* drop flags for writing */
14135+unsigned int au_file_roflags(unsigned int flags)
14136+{
14137+ flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14138+ flags |= O_RDONLY | O_NOATIME;
14139+ return flags;
14140+}
14141+
14142+/* common functions to regular file and dir */
14143+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 14144+ struct file *file, int force_wr)
1facf9fc 14145+{
1308ab2a 14146+ struct file *h_file;
4a4d8108
AM
14147+ struct dentry *h_dentry;
14148+ struct inode *h_inode;
14149+ struct super_block *sb;
14150+ struct au_branch *br;
14151+ struct path h_path;
b912730e 14152+ int err;
1facf9fc 14153+
4a4d8108
AM
14154+ /* a race condition can happen between open and unlink/rmdir */
14155+ h_file = ERR_PTR(-ENOENT);
14156+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 14157+ if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
4a4d8108 14158+ goto out;
5527c038 14159+ h_inode = d_inode(h_dentry);
027c5e7a
AM
14160+ spin_lock(&h_dentry->d_lock);
14161+ err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
5527c038 14162+ /* || !d_inode(dentry)->i_nlink */
027c5e7a
AM
14163+ ;
14164+ spin_unlock(&h_dentry->d_lock);
14165+ if (unlikely(err))
4a4d8108 14166+ goto out;
1facf9fc 14167+
4a4d8108
AM
14168+ sb = dentry->d_sb;
14169+ br = au_sbr(sb, bindex);
b912730e
AM
14170+ err = au_br_test_oflag(flags, br);
14171+ h_file = ERR_PTR(err);
14172+ if (unlikely(err))
027c5e7a 14173+ goto out;
1facf9fc 14174+
4a4d8108 14175+ /* drop flags for writing */
5527c038 14176+ if (au_test_ro(sb, bindex, d_inode(dentry))) {
392086de
AM
14177+ if (force_wr && !(flags & O_WRONLY))
14178+ force_wr = 0;
4a4d8108 14179+ flags = au_file_roflags(flags);
392086de
AM
14180+ if (force_wr) {
14181+ h_file = ERR_PTR(-EROFS);
14182+ flags = au_file_roflags(flags);
14183+ if (unlikely(vfsub_native_ro(h_inode)
14184+ || IS_APPEND(h_inode)))
14185+ goto out;
14186+ flags &= ~O_ACCMODE;
14187+ flags |= O_WRONLY;
14188+ }
14189+ }
4a4d8108 14190+ flags &= ~O_CREAT;
5afbbe0d 14191+ au_br_get(br);
4a4d8108 14192+ h_path.dentry = h_dentry;
86dc4139 14193+ h_path.mnt = au_br_mnt(br);
38d290e6 14194+ h_file = vfsub_dentry_open(&h_path, flags);
4a4d8108
AM
14195+ if (IS_ERR(h_file))
14196+ goto out_br;
dece6358 14197+
b912730e 14198+ if (flags & __FMODE_EXEC) {
4a4d8108
AM
14199+ err = deny_write_access(h_file);
14200+ if (unlikely(err)) {
14201+ fput(h_file);
14202+ h_file = ERR_PTR(err);
14203+ goto out_br;
14204+ }
14205+ }
953406b4 14206+ fsnotify_open(h_file);
4a4d8108 14207+ goto out; /* success */
1facf9fc 14208+
4f0767ce 14209+out_br:
5afbbe0d 14210+ au_br_put(br);
4f0767ce 14211+out:
4a4d8108
AM
14212+ return h_file;
14213+}
1308ab2a 14214+
076b876e
AM
14215+static int au_cmoo(struct dentry *dentry)
14216+{
8b6a4947 14217+ int err, cmoo, matched;
076b876e
AM
14218+ unsigned int udba;
14219+ struct path h_path;
14220+ struct au_pin pin;
14221+ struct au_cp_generic cpg = {
14222+ .dentry = dentry,
14223+ .bdst = -1,
14224+ .bsrc = -1,
14225+ .len = -1,
14226+ .pin = &pin,
14227+ .flags = AuCpup_DTIME | AuCpup_HOPEN
14228+ };
7e9cd9fe 14229+ struct inode *delegated;
076b876e
AM
14230+ struct super_block *sb;
14231+ struct au_sbinfo *sbinfo;
14232+ struct au_fhsm *fhsm;
14233+ pid_t pid;
14234+ struct au_branch *br;
14235+ struct dentry *parent;
14236+ struct au_hinode *hdir;
14237+
14238+ DiMustWriteLock(dentry);
5527c038 14239+ IiMustWriteLock(d_inode(dentry));
076b876e
AM
14240+
14241+ err = 0;
14242+ if (IS_ROOT(dentry))
14243+ goto out;
5afbbe0d 14244+ cpg.bsrc = au_dbtop(dentry);
076b876e
AM
14245+ if (!cpg.bsrc)
14246+ goto out;
14247+
14248+ sb = dentry->d_sb;
14249+ sbinfo = au_sbi(sb);
14250+ fhsm = &sbinfo->si_fhsm;
14251+ pid = au_fhsm_pid(fhsm);
8b6a4947
AM
14252+ rcu_read_lock();
14253+ matched = (pid
14254+ && (current->pid == pid
14255+ || rcu_dereference(current->real_parent)->pid == pid));
14256+ rcu_read_unlock();
14257+ if (matched)
076b876e
AM
14258+ goto out;
14259+
14260+ br = au_sbr(sb, cpg.bsrc);
14261+ cmoo = au_br_cmoo(br->br_perm);
14262+ if (!cmoo)
14263+ goto out;
7e9cd9fe 14264+ if (!d_is_reg(dentry))
076b876e
AM
14265+ cmoo &= AuBrAttr_COO_ALL;
14266+ if (!cmoo)
14267+ goto out;
14268+
14269+ parent = dget_parent(dentry);
14270+ di_write_lock_parent(parent);
14271+ err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14272+ cpg.bdst = err;
14273+ if (unlikely(err < 0)) {
14274+ err = 0; /* there is no upper writable branch */
14275+ goto out_dgrade;
14276+ }
14277+ AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14278+
14279+ /* do not respect the coo attrib for the target branch */
14280+ err = au_cpup_dirs(dentry, cpg.bdst);
14281+ if (unlikely(err))
14282+ goto out_dgrade;
14283+
14284+ di_downgrade_lock(parent, AuLock_IR);
14285+ udba = au_opt_udba(sb);
14286+ err = au_pin(&pin, dentry, cpg.bdst, udba,
14287+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14288+ if (unlikely(err))
14289+ goto out_parent;
14290+
14291+ err = au_sio_cpup_simple(&cpg);
14292+ au_unpin(&pin);
14293+ if (unlikely(err))
14294+ goto out_parent;
14295+ if (!(cmoo & AuBrWAttr_MOO))
14296+ goto out_parent; /* success */
14297+
14298+ err = au_pin(&pin, dentry, cpg.bsrc, udba,
14299+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14300+ if (unlikely(err))
14301+ goto out_parent;
14302+
14303+ h_path.mnt = au_br_mnt(br);
14304+ h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
5527c038 14305+ hdir = au_hi(d_inode(parent), cpg.bsrc);
076b876e
AM
14306+ delegated = NULL;
14307+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14308+ au_unpin(&pin);
14309+ /* todo: keep h_dentry or not? */
14310+ if (unlikely(err == -EWOULDBLOCK)) {
14311+ pr_warn("cannot retry for NFSv4 delegation"
14312+ " for an internal unlink\n");
14313+ iput(delegated);
14314+ }
14315+ if (unlikely(err)) {
14316+ pr_err("unlink %pd after coo failed (%d), ignored\n",
14317+ dentry, err);
14318+ err = 0;
14319+ }
14320+ goto out_parent; /* success */
14321+
14322+out_dgrade:
14323+ di_downgrade_lock(parent, AuLock_IR);
14324+out_parent:
14325+ di_read_unlock(parent, AuLock_IR);
14326+ dput(parent);
14327+out:
14328+ AuTraceErr(err);
14329+ return err;
14330+}
14331+
b912730e 14332+int au_do_open(struct file *file, struct au_do_open_args *args)
1facf9fc 14333+{
8b6a4947 14334+ int err, aopen = args->aopen;
1facf9fc 14335+ struct dentry *dentry;
076b876e 14336+ struct au_finfo *finfo;
1308ab2a 14337+
8b6a4947 14338+ if (!aopen)
b912730e
AM
14339+ err = au_finfo_init(file, args->fidir);
14340+ else {
14341+ lockdep_off();
14342+ err = au_finfo_init(file, args->fidir);
14343+ lockdep_on();
14344+ }
4a4d8108
AM
14345+ if (unlikely(err))
14346+ goto out;
1facf9fc 14347+
2000de60 14348+ dentry = file->f_path.dentry;
b912730e 14349+ AuDebugOn(IS_ERR_OR_NULL(dentry));
8b6a4947
AM
14350+ di_write_lock_child(dentry);
14351+ err = au_cmoo(dentry);
14352+ di_downgrade_lock(dentry, AuLock_IR);
14353+ if (!err) {
14354+ if (!aopen)
b912730e 14355+ err = args->open(file, vfsub_file_flags(file), NULL);
8b6a4947
AM
14356+ else {
14357+ lockdep_off();
14358+ err = args->open(file, vfsub_file_flags(file), NULL);
14359+ lockdep_on();
14360+ }
b912730e 14361+ }
8b6a4947 14362+ di_read_unlock(dentry, AuLock_IR);
1facf9fc 14363+
076b876e
AM
14364+ finfo = au_fi(file);
14365+ if (!err) {
14366+ finfo->fi_file = file;
8b6a4947
AM
14367+ au_hbl_add(&finfo->fi_hlist,
14368+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
076b876e 14369+ }
8b6a4947 14370+ if (!aopen)
b912730e
AM
14371+ fi_write_unlock(file);
14372+ else {
14373+ lockdep_off();
14374+ fi_write_unlock(file);
14375+ lockdep_on();
14376+ }
4a4d8108 14377+ if (unlikely(err)) {
076b876e 14378+ finfo->fi_hdir = NULL;
1c60b727 14379+ au_finfo_fin(file);
1308ab2a 14380+ }
4a4d8108 14381+
4f0767ce 14382+out:
8b6a4947 14383+ AuTraceErr(err);
1308ab2a 14384+ return err;
14385+}
dece6358 14386+
4a4d8108 14387+int au_reopen_nondir(struct file *file)
1308ab2a 14388+{
4a4d8108 14389+ int err;
5afbbe0d 14390+ aufs_bindex_t btop;
4a4d8108
AM
14391+ struct dentry *dentry;
14392+ struct file *h_file, *h_file_tmp;
1308ab2a 14393+
2000de60 14394+ dentry = file->f_path.dentry;
5afbbe0d 14395+ btop = au_dbtop(dentry);
4a4d8108 14396+ h_file_tmp = NULL;
5afbbe0d 14397+ if (au_fbtop(file) == btop) {
4a4d8108
AM
14398+ h_file = au_hf_top(file);
14399+ if (file->f_mode == h_file->f_mode)
14400+ return 0; /* success */
14401+ h_file_tmp = h_file;
14402+ get_file(h_file_tmp);
5afbbe0d 14403+ au_set_h_fptr(file, btop, NULL);
4a4d8108
AM
14404+ }
14405+ AuDebugOn(au_fi(file)->fi_hdir);
86dc4139
AM
14406+ /*
14407+ * it can happen
14408+ * file exists on both of rw and ro
5afbbe0d 14409+ * open --> dbtop and fbtop are both 0
86dc4139
AM
14410+ * prepend a branch as rw, "rw" become ro
14411+ * remove rw/file
14412+ * delete the top branch, "rw" becomes rw again
5afbbe0d
AM
14413+ * --> dbtop is 1, fbtop is still 0
14414+ * write --> fbtop is 0 but dbtop is 1
86dc4139 14415+ */
5afbbe0d 14416+ /* AuDebugOn(au_fbtop(file) < btop); */
1308ab2a 14417+
5afbbe0d 14418+ h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
392086de 14419+ file, /*force_wr*/0);
4a4d8108 14420+ err = PTR_ERR(h_file);
86dc4139
AM
14421+ if (IS_ERR(h_file)) {
14422+ if (h_file_tmp) {
5afbbe0d
AM
14423+ au_sbr_get(dentry->d_sb, btop);
14424+ au_set_h_fptr(file, btop, h_file_tmp);
86dc4139
AM
14425+ h_file_tmp = NULL;
14426+ }
4a4d8108 14427+ goto out; /* todo: close all? */
86dc4139 14428+ }
4a4d8108
AM
14429+
14430+ err = 0;
5afbbe0d
AM
14431+ au_set_fbtop(file, btop);
14432+ au_set_h_fptr(file, btop, h_file);
4a4d8108
AM
14433+ au_update_figen(file);
14434+ /* todo: necessary? */
14435+ /* file->f_ra = h_file->f_ra; */
14436+
4f0767ce 14437+out:
4a4d8108
AM
14438+ if (h_file_tmp)
14439+ fput(h_file_tmp);
14440+ return err;
1facf9fc 14441+}
14442+
1308ab2a 14443+/* ---------------------------------------------------------------------- */
14444+
4a4d8108
AM
14445+static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14446+ struct dentry *hi_wh)
1facf9fc 14447+{
4a4d8108 14448+ int err;
5afbbe0d 14449+ aufs_bindex_t btop;
4a4d8108
AM
14450+ struct au_dinfo *dinfo;
14451+ struct dentry *h_dentry;
14452+ struct au_hdentry *hdp;
1facf9fc 14453+
2000de60 14454+ dinfo = au_di(file->f_path.dentry);
4a4d8108 14455+ AuRwMustWriteLock(&dinfo->di_rwsem);
dece6358 14456+
5afbbe0d
AM
14457+ btop = dinfo->di_btop;
14458+ dinfo->di_btop = btgt;
14459+ hdp = au_hdentry(dinfo, btgt);
14460+ h_dentry = hdp->hd_dentry;
14461+ hdp->hd_dentry = hi_wh;
4a4d8108 14462+ err = au_reopen_nondir(file);
5afbbe0d
AM
14463+ hdp->hd_dentry = h_dentry;
14464+ dinfo->di_btop = btop;
1facf9fc 14465+
1facf9fc 14466+ return err;
14467+}
14468+
4a4d8108 14469+static int au_ready_to_write_wh(struct file *file, loff_t len,
86dc4139 14470+ aufs_bindex_t bcpup, struct au_pin *pin)
1facf9fc 14471+{
4a4d8108 14472+ int err;
027c5e7a 14473+ struct inode *inode, *h_inode;
c2b27bf2
AM
14474+ struct dentry *h_dentry, *hi_wh;
14475+ struct au_cp_generic cpg = {
2000de60 14476+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14477+ .bdst = bcpup,
14478+ .bsrc = -1,
14479+ .len = len,
14480+ .pin = pin
14481+ };
1facf9fc 14482+
5afbbe0d 14483+ au_update_dbtop(cpg.dentry);
5527c038 14484+ inode = d_inode(cpg.dentry);
027c5e7a 14485+ h_inode = NULL;
5afbbe0d
AM
14486+ if (au_dbtop(cpg.dentry) <= bcpup
14487+ && au_dbbot(cpg.dentry) >= bcpup) {
c2b27bf2 14488+ h_dentry = au_h_dptr(cpg.dentry, bcpup);
5527c038
JR
14489+ if (h_dentry && d_is_positive(h_dentry))
14490+ h_inode = d_inode(h_dentry);
027c5e7a 14491+ }
4a4d8108 14492+ hi_wh = au_hi_wh(inode, bcpup);
027c5e7a 14493+ if (!hi_wh && !h_inode)
c2b27bf2 14494+ err = au_sio_cpup_wh(&cpg, file);
4a4d8108
AM
14495+ else
14496+ /* already copied-up after unlink */
14497+ err = au_reopen_wh(file, bcpup, hi_wh);
1facf9fc 14498+
4a4d8108 14499+ if (!err
38d290e6
JR
14500+ && (inode->i_nlink > 1
14501+ || (inode->i_state & I_LINKABLE))
c2b27bf2
AM
14502+ && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14503+ au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
1308ab2a 14504+
dece6358 14505+ return err;
1facf9fc 14506+}
14507+
4a4d8108
AM
14508+/*
14509+ * prepare the @file for writing.
14510+ */
14511+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
1facf9fc 14512+{
4a4d8108 14513+ int err;
5afbbe0d 14514+ aufs_bindex_t dbtop;
c1595e42 14515+ struct dentry *parent;
86dc4139 14516+ struct inode *inode;
1facf9fc 14517+ struct super_block *sb;
4a4d8108 14518+ struct file *h_file;
c2b27bf2 14519+ struct au_cp_generic cpg = {
2000de60 14520+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14521+ .bdst = -1,
14522+ .bsrc = -1,
14523+ .len = len,
14524+ .pin = pin,
14525+ .flags = AuCpup_DTIME
14526+ };
1facf9fc 14527+
c2b27bf2 14528+ sb = cpg.dentry->d_sb;
5527c038 14529+ inode = d_inode(cpg.dentry);
5afbbe0d 14530+ cpg.bsrc = au_fbtop(file);
c2b27bf2 14531+ err = au_test_ro(sb, cpg.bsrc, inode);
4a4d8108 14532+ if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
c2b27bf2
AM
14533+ err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14534+ /*flags*/0);
1facf9fc 14535+ goto out;
4a4d8108 14536+ }
1facf9fc 14537+
027c5e7a 14538+ /* need to cpup or reopen */
c2b27bf2 14539+ parent = dget_parent(cpg.dentry);
4a4d8108 14540+ di_write_lock_parent(parent);
c2b27bf2
AM
14541+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14542+ cpg.bdst = err;
4a4d8108
AM
14543+ if (unlikely(err < 0))
14544+ goto out_dgrade;
14545+ err = 0;
14546+
c2b27bf2
AM
14547+ if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14548+ err = au_cpup_dirs(cpg.dentry, cpg.bdst);
1facf9fc 14549+ if (unlikely(err))
4a4d8108
AM
14550+ goto out_dgrade;
14551+ }
14552+
c2b27bf2 14553+ err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108
AM
14554+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14555+ if (unlikely(err))
14556+ goto out_dgrade;
14557+
5afbbe0d
AM
14558+ dbtop = au_dbtop(cpg.dentry);
14559+ if (dbtop <= cpg.bdst)
c2b27bf2 14560+ cpg.bsrc = cpg.bdst;
027c5e7a 14561+
5afbbe0d 14562+ if (dbtop <= cpg.bdst /* just reopen */
c2b27bf2 14563+ || !d_unhashed(cpg.dentry) /* copyup and reopen */
027c5e7a 14564+ ) {
392086de 14565+ h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
86dc4139 14566+ if (IS_ERR(h_file))
027c5e7a 14567+ err = PTR_ERR(h_file);
86dc4139 14568+ else {
027c5e7a 14569+ di_downgrade_lock(parent, AuLock_IR);
5afbbe0d 14570+ if (dbtop > cpg.bdst)
c2b27bf2 14571+ err = au_sio_cpup_simple(&cpg);
027c5e7a
AM
14572+ if (!err)
14573+ err = au_reopen_nondir(file);
c2b27bf2 14574+ au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
027c5e7a 14575+ }
027c5e7a
AM
14576+ } else { /* copyup as wh and reopen */
14577+ /*
14578+ * since writable hfsplus branch is not supported,
14579+ * h_open_pre/post() are unnecessary.
14580+ */
c2b27bf2 14581+ err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
4a4d8108 14582+ di_downgrade_lock(parent, AuLock_IR);
4a4d8108 14583+ }
4a4d8108
AM
14584+
14585+ if (!err) {
14586+ au_pin_set_parent_lflag(pin, /*lflag*/0);
14587+ goto out_dput; /* success */
14588+ }
14589+ au_unpin(pin);
14590+ goto out_unlock;
1facf9fc 14591+
4f0767ce 14592+out_dgrade:
4a4d8108 14593+ di_downgrade_lock(parent, AuLock_IR);
4f0767ce 14594+out_unlock:
4a4d8108 14595+ di_read_unlock(parent, AuLock_IR);
4f0767ce 14596+out_dput:
4a4d8108 14597+ dput(parent);
4f0767ce 14598+out:
1facf9fc 14599+ return err;
14600+}
14601+
4a4d8108
AM
14602+/* ---------------------------------------------------------------------- */
14603+
14604+int au_do_flush(struct file *file, fl_owner_t id,
14605+ int (*flush)(struct file *file, fl_owner_t id))
1facf9fc 14606+{
4a4d8108 14607+ int err;
1facf9fc 14608+ struct super_block *sb;
4a4d8108 14609+ struct inode *inode;
1facf9fc 14610+
c06a8ce3
AM
14611+ inode = file_inode(file);
14612+ sb = inode->i_sb;
4a4d8108
AM
14613+ si_noflush_read_lock(sb);
14614+ fi_read_lock(file);
b752ccd1 14615+ ii_read_lock_child(inode);
1facf9fc 14616+
4a4d8108
AM
14617+ err = flush(file, id);
14618+ au_cpup_attr_timesizes(inode);
1facf9fc 14619+
b752ccd1 14620+ ii_read_unlock(inode);
4a4d8108 14621+ fi_read_unlock(file);
1308ab2a 14622+ si_read_unlock(sb);
dece6358 14623+ return err;
1facf9fc 14624+}
14625+
4a4d8108
AM
14626+/* ---------------------------------------------------------------------- */
14627+
14628+static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
1facf9fc 14629+{
4a4d8108 14630+ int err;
4a4d8108
AM
14631+ struct au_pin pin;
14632+ struct au_finfo *finfo;
c2b27bf2 14633+ struct dentry *parent, *hi_wh;
4a4d8108 14634+ struct inode *inode;
1facf9fc 14635+ struct super_block *sb;
c2b27bf2 14636+ struct au_cp_generic cpg = {
2000de60 14637+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14638+ .bdst = -1,
14639+ .bsrc = -1,
14640+ .len = -1,
14641+ .pin = &pin,
14642+ .flags = AuCpup_DTIME
14643+ };
1facf9fc 14644+
4a4d8108
AM
14645+ FiMustWriteLock(file);
14646+
14647+ err = 0;
14648+ finfo = au_fi(file);
c2b27bf2 14649+ sb = cpg.dentry->d_sb;
5527c038 14650+ inode = d_inode(cpg.dentry);
5afbbe0d 14651+ cpg.bdst = au_ibtop(inode);
c2b27bf2 14652+ if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
1308ab2a 14653+ goto out;
dece6358 14654+
c2b27bf2
AM
14655+ parent = dget_parent(cpg.dentry);
14656+ if (au_test_ro(sb, cpg.bdst, inode)) {
4a4d8108 14657+ di_read_lock_parent(parent, !AuLock_IR);
c2b27bf2
AM
14658+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14659+ cpg.bdst = err;
4a4d8108
AM
14660+ di_read_unlock(parent, !AuLock_IR);
14661+ if (unlikely(err < 0))
14662+ goto out_parent;
14663+ err = 0;
1facf9fc 14664+ }
1facf9fc 14665+
4a4d8108 14666+ di_read_lock_parent(parent, AuLock_IR);
c2b27bf2 14667+ hi_wh = au_hi_wh(inode, cpg.bdst);
7f207e10
AM
14668+ if (!S_ISDIR(inode->i_mode)
14669+ && au_opt_test(au_mntflags(sb), PLINK)
4a4d8108 14670+ && au_plink_test(inode)
c2b27bf2 14671+ && !d_unhashed(cpg.dentry)
5afbbe0d 14672+ && cpg.bdst < au_dbtop(cpg.dentry)) {
c2b27bf2 14673+ err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
4a4d8108
AM
14674+ if (unlikely(err))
14675+ goto out_unlock;
14676+
14677+ /* always superio. */
c2b27bf2 14678+ err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108 14679+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 14680+ if (!err) {
c2b27bf2 14681+ err = au_sio_cpup_simple(&cpg);
367653fa
AM
14682+ au_unpin(&pin);
14683+ }
4a4d8108
AM
14684+ } else if (hi_wh) {
14685+ /* already copied-up after unlink */
c2b27bf2 14686+ err = au_reopen_wh(file, cpg.bdst, hi_wh);
4a4d8108
AM
14687+ *need_reopen = 0;
14688+ }
1facf9fc 14689+
4f0767ce 14690+out_unlock:
4a4d8108 14691+ di_read_unlock(parent, AuLock_IR);
4f0767ce 14692+out_parent:
4a4d8108 14693+ dput(parent);
4f0767ce 14694+out:
1308ab2a 14695+ return err;
dece6358 14696+}
1facf9fc 14697+
4a4d8108 14698+static void au_do_refresh_dir(struct file *file)
dece6358 14699+{
5afbbe0d 14700+ aufs_bindex_t bindex, bbot, new_bindex, brid;
4a4d8108
AM
14701+ struct au_hfile *p, tmp, *q;
14702+ struct au_finfo *finfo;
1308ab2a 14703+ struct super_block *sb;
4a4d8108 14704+ struct au_fidir *fidir;
1facf9fc 14705+
4a4d8108 14706+ FiMustWriteLock(file);
1facf9fc 14707+
2000de60 14708+ sb = file->f_path.dentry->d_sb;
4a4d8108
AM
14709+ finfo = au_fi(file);
14710+ fidir = finfo->fi_hdir;
14711+ AuDebugOn(!fidir);
14712+ p = fidir->fd_hfile + finfo->fi_btop;
14713+ brid = p->hf_br->br_id;
5afbbe0d
AM
14714+ bbot = fidir->fd_bbot;
14715+ for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
4a4d8108
AM
14716+ if (!p->hf_file)
14717+ continue;
1308ab2a 14718+
4a4d8108
AM
14719+ new_bindex = au_br_index(sb, p->hf_br->br_id);
14720+ if (new_bindex == bindex)
14721+ continue;
14722+ if (new_bindex < 0) {
14723+ au_set_h_fptr(file, bindex, NULL);
14724+ continue;
14725+ }
1308ab2a 14726+
4a4d8108
AM
14727+ /* swap two lower inode, and loop again */
14728+ q = fidir->fd_hfile + new_bindex;
14729+ tmp = *q;
14730+ *q = *p;
14731+ *p = tmp;
14732+ if (tmp.hf_file) {
14733+ bindex--;
14734+ p--;
14735+ }
14736+ }
1308ab2a 14737+
4a4d8108 14738+ p = fidir->fd_hfile;
2000de60 14739+ if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
5afbbe0d
AM
14740+ bbot = au_sbbot(sb);
14741+ for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
4a4d8108
AM
14742+ finfo->fi_btop++, p++)
14743+ if (p->hf_file) {
c06a8ce3 14744+ if (file_inode(p->hf_file))
4a4d8108 14745+ break;
1c60b727 14746+ au_hfput(p, /*execed*/0);
4a4d8108
AM
14747+ }
14748+ } else {
5afbbe0d
AM
14749+ bbot = au_br_index(sb, brid);
14750+ for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
4a4d8108
AM
14751+ finfo->fi_btop++, p++)
14752+ if (p->hf_file)
1c60b727 14753+ au_hfput(p, /*execed*/0);
5afbbe0d 14754+ bbot = au_sbbot(sb);
4a4d8108 14755+ }
1308ab2a 14756+
5afbbe0d
AM
14757+ p = fidir->fd_hfile + bbot;
14758+ for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
4a4d8108
AM
14759+ fidir->fd_bbot--, p--)
14760+ if (p->hf_file) {
c06a8ce3 14761+ if (file_inode(p->hf_file))
4a4d8108 14762+ break;
1c60b727 14763+ au_hfput(p, /*execed*/0);
4a4d8108
AM
14764+ }
14765+ AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
1308ab2a 14766+}
14767+
4a4d8108
AM
14768+/*
14769+ * after branch manipulating, refresh the file.
14770+ */
14771+static int refresh_file(struct file *file, int (*reopen)(struct file *file))
1facf9fc 14772+{
e2f27e51 14773+ int err, need_reopen, nbr;
5afbbe0d 14774+ aufs_bindex_t bbot, bindex;
4a4d8108 14775+ struct dentry *dentry;
e2f27e51 14776+ struct super_block *sb;
1308ab2a 14777+ struct au_finfo *finfo;
4a4d8108 14778+ struct au_hfile *hfile;
1facf9fc 14779+
2000de60 14780+ dentry = file->f_path.dentry;
e2f27e51
AM
14781+ sb = dentry->d_sb;
14782+ nbr = au_sbbot(sb) + 1;
1308ab2a 14783+ finfo = au_fi(file);
4a4d8108
AM
14784+ if (!finfo->fi_hdir) {
14785+ hfile = &finfo->fi_htop;
14786+ AuDebugOn(!hfile->hf_file);
e2f27e51 14787+ bindex = au_br_index(sb, hfile->hf_br->br_id);
4a4d8108
AM
14788+ AuDebugOn(bindex < 0);
14789+ if (bindex != finfo->fi_btop)
5afbbe0d 14790+ au_set_fbtop(file, bindex);
4a4d8108 14791+ } else {
e2f27e51 14792+ err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
4a4d8108
AM
14793+ if (unlikely(err))
14794+ goto out;
14795+ au_do_refresh_dir(file);
14796+ }
1facf9fc 14797+
4a4d8108
AM
14798+ err = 0;
14799+ need_reopen = 1;
14800+ if (!au_test_mmapped(file))
14801+ err = au_file_refresh_by_inode(file, &need_reopen);
e2f27e51
AM
14802+ if (finfo->fi_hdir)
14803+ /* harmless if err */
14804+ au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
027c5e7a 14805+ if (!err && need_reopen && !d_unlinked(dentry))
4a4d8108
AM
14806+ err = reopen(file);
14807+ if (!err) {
14808+ au_update_figen(file);
14809+ goto out; /* success */
14810+ }
14811+
14812+ /* error, close all lower files */
14813+ if (finfo->fi_hdir) {
5afbbe0d
AM
14814+ bbot = au_fbbot_dir(file);
14815+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
4a4d8108
AM
14816+ au_set_h_fptr(file, bindex, NULL);
14817+ }
1facf9fc 14818+
4f0767ce 14819+out:
1facf9fc 14820+ return err;
14821+}
14822+
4a4d8108
AM
14823+/* common function to regular file and dir */
14824+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
521ced18 14825+ int wlock, unsigned int fi_lsc)
dece6358 14826+{
1308ab2a 14827+ int err;
4a4d8108 14828+ unsigned int sigen, figen;
5afbbe0d 14829+ aufs_bindex_t btop;
4a4d8108
AM
14830+ unsigned char pseudo_link;
14831+ struct dentry *dentry;
14832+ struct inode *inode;
1facf9fc 14833+
4a4d8108 14834+ err = 0;
2000de60 14835+ dentry = file->f_path.dentry;
5527c038 14836+ inode = d_inode(dentry);
4a4d8108 14837+ sigen = au_sigen(dentry->d_sb);
521ced18 14838+ fi_write_lock_nested(file, fi_lsc);
4a4d8108 14839+ figen = au_figen(file);
521ced18
JR
14840+ if (!fi_lsc)
14841+ di_write_lock_child(dentry);
14842+ else
14843+ di_write_lock_child2(dentry);
5afbbe0d
AM
14844+ btop = au_dbtop(dentry);
14845+ pseudo_link = (btop != au_ibtop(inode));
14846+ if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
4a4d8108
AM
14847+ if (!wlock) {
14848+ di_downgrade_lock(dentry, AuLock_IR);
14849+ fi_downgrade_lock(file);
14850+ }
14851+ goto out; /* success */
14852+ }
dece6358 14853+
4a4d8108 14854+ AuDbg("sigen %d, figen %d\n", sigen, figen);
027c5e7a 14855+ if (au_digen_test(dentry, sigen)) {
4a4d8108 14856+ err = au_reval_dpath(dentry, sigen);
027c5e7a 14857+ AuDebugOn(!err && au_digen_test(dentry, sigen));
4a4d8108 14858+ }
dece6358 14859+
027c5e7a
AM
14860+ if (!err)
14861+ err = refresh_file(file, reopen);
4a4d8108
AM
14862+ if (!err) {
14863+ if (!wlock) {
14864+ di_downgrade_lock(dentry, AuLock_IR);
14865+ fi_downgrade_lock(file);
14866+ }
14867+ } else {
14868+ di_write_unlock(dentry);
14869+ fi_write_unlock(file);
14870+ }
1facf9fc 14871+
4f0767ce 14872+out:
1308ab2a 14873+ return err;
14874+}
1facf9fc 14875+
4a4d8108
AM
14876+/* ---------------------------------------------------------------------- */
14877+
14878+/* cf. aufs_nopage() */
14879+/* for madvise(2) */
14880+static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
1308ab2a 14881+{
4a4d8108
AM
14882+ unlock_page(page);
14883+ return 0;
14884+}
1facf9fc 14885+
4a4d8108 14886+/* it will never be called, but necessary to support O_DIRECT */
5afbbe0d 14887+static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
4a4d8108 14888+{ BUG(); return 0; }
1facf9fc 14889+
4a4d8108
AM
14890+/* they will never be called. */
14891+#ifdef CONFIG_AUFS_DEBUG
14892+static int aufs_write_begin(struct file *file, struct address_space *mapping,
14893+ loff_t pos, unsigned len, unsigned flags,
14894+ struct page **pagep, void **fsdata)
14895+{ AuUnsupport(); return 0; }
14896+static int aufs_write_end(struct file *file, struct address_space *mapping,
14897+ loff_t pos, unsigned len, unsigned copied,
14898+ struct page *page, void *fsdata)
14899+{ AuUnsupport(); return 0; }
14900+static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14901+{ AuUnsupport(); return 0; }
1308ab2a 14902+
4a4d8108
AM
14903+static int aufs_set_page_dirty(struct page *page)
14904+{ AuUnsupport(); return 0; }
392086de
AM
14905+static void aufs_invalidatepage(struct page *page, unsigned int offset,
14906+ unsigned int length)
4a4d8108
AM
14907+{ AuUnsupport(); }
14908+static int aufs_releasepage(struct page *page, gfp_t gfp)
14909+{ AuUnsupport(); return 0; }
79b8bda9 14910+#if 0 /* called by memory compaction regardless file */
4a4d8108 14911+static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
7eafdf33 14912+ struct page *page, enum migrate_mode mode)
4a4d8108 14913+{ AuUnsupport(); return 0; }
79b8bda9 14914+#endif
e2f27e51
AM
14915+static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
14916+{ AuUnsupport(); return true; }
14917+static void aufs_putback_page(struct page *page)
14918+{ AuUnsupport(); }
4a4d8108
AM
14919+static int aufs_launder_page(struct page *page)
14920+{ AuUnsupport(); return 0; }
14921+static int aufs_is_partially_uptodate(struct page *page,
38d290e6
JR
14922+ unsigned long from,
14923+ unsigned long count)
4a4d8108 14924+{ AuUnsupport(); return 0; }
392086de
AM
14925+static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
14926+ bool *writeback)
14927+{ AuUnsupport(); }
4a4d8108
AM
14928+static int aufs_error_remove_page(struct address_space *mapping,
14929+ struct page *page)
14930+{ AuUnsupport(); return 0; }
b4510431
AM
14931+static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14932+ sector_t *span)
14933+{ AuUnsupport(); return 0; }
14934+static void aufs_swap_deactivate(struct file *file)
14935+{ AuUnsupport(); }
4a4d8108
AM
14936+#endif /* CONFIG_AUFS_DEBUG */
14937+
14938+const struct address_space_operations aufs_aop = {
14939+ .readpage = aufs_readpage,
14940+ .direct_IO = aufs_direct_IO,
4a4d8108
AM
14941+#ifdef CONFIG_AUFS_DEBUG
14942+ .writepage = aufs_writepage,
4a4d8108
AM
14943+ /* no writepages, because of writepage */
14944+ .set_page_dirty = aufs_set_page_dirty,
14945+ /* no readpages, because of readpage */
14946+ .write_begin = aufs_write_begin,
14947+ .write_end = aufs_write_end,
14948+ /* no bmap, no block device */
14949+ .invalidatepage = aufs_invalidatepage,
14950+ .releasepage = aufs_releasepage,
79b8bda9
AM
14951+ /* is fallback_migrate_page ok? */
14952+ /* .migratepage = aufs_migratepage, */
e2f27e51
AM
14953+ .isolate_page = aufs_isolate_page,
14954+ .putback_page = aufs_putback_page,
4a4d8108
AM
14955+ .launder_page = aufs_launder_page,
14956+ .is_partially_uptodate = aufs_is_partially_uptodate,
392086de 14957+ .is_dirty_writeback = aufs_is_dirty_writeback,
b4510431
AM
14958+ .error_remove_page = aufs_error_remove_page,
14959+ .swap_activate = aufs_swap_activate,
14960+ .swap_deactivate = aufs_swap_deactivate
4a4d8108 14961+#endif /* CONFIG_AUFS_DEBUG */
dece6358 14962+};
7f207e10
AM
14963diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
14964--- /usr/share/empty/fs/aufs/file.h 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
14965+++ linux/fs/aufs/file.h 2017-11-12 22:24:44.704244405 +0100
14966@@ -0,0 +1,340 @@
4a4d8108 14967+/*
a2654f78 14968+ * Copyright (C) 2005-2017 Junjiro R. Okajima
4a4d8108
AM
14969+ *
14970+ * This program, aufs is free software; you can redistribute it and/or modify
14971+ * it under the terms of the GNU General Public License as published by
14972+ * the Free Software Foundation; either version 2 of the License, or
14973+ * (at your option) any later version.
14974+ *
14975+ * This program is distributed in the hope that it will be useful,
14976+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14977+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14978+ * GNU General Public License for more details.
14979+ *
14980+ * You should have received a copy of the GNU General Public License
523b37e3 14981+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 14982+ */
1facf9fc 14983+
4a4d8108
AM
14984+/*
14985+ * file operations
14986+ */
1facf9fc 14987+
4a4d8108
AM
14988+#ifndef __AUFS_FILE_H__
14989+#define __AUFS_FILE_H__
1facf9fc 14990+
4a4d8108 14991+#ifdef __KERNEL__
1facf9fc 14992+
2cbb1c4b 14993+#include <linux/file.h>
4a4d8108 14994+#include <linux/fs.h>
3c1bdaff 14995+#include <linux/mm_types.h>
4a4d8108 14996+#include <linux/poll.h>
4a4d8108 14997+#include "rwsem.h"
1facf9fc 14998+
4a4d8108
AM
14999+struct au_branch;
15000+struct au_hfile {
15001+ struct file *hf_file;
15002+ struct au_branch *hf_br;
15003+};
1facf9fc 15004+
4a4d8108
AM
15005+struct au_vdir;
15006+struct au_fidir {
15007+ aufs_bindex_t fd_bbot;
15008+ aufs_bindex_t fd_nent;
15009+ struct au_vdir *fd_vdir_cache;
15010+ struct au_hfile fd_hfile[];
15011+};
1facf9fc 15012+
4a4d8108 15013+static inline int au_fidir_sz(int nent)
dece6358 15014+{
4f0767ce
JR
15015+ AuDebugOn(nent < 0);
15016+ return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
4a4d8108 15017+}
1facf9fc 15018+
4a4d8108
AM
15019+struct au_finfo {
15020+ atomic_t fi_generation;
dece6358 15021+
4a4d8108
AM
15022+ struct au_rwsem fi_rwsem;
15023+ aufs_bindex_t fi_btop;
15024+
15025+ /* do not union them */
15026+ struct { /* for non-dir */
15027+ struct au_hfile fi_htop;
2cbb1c4b 15028+ atomic_t fi_mmapped;
4a4d8108
AM
15029+ };
15030+ struct au_fidir *fi_hdir; /* for dir only */
523b37e3 15031+
8b6a4947 15032+ struct hlist_bl_node fi_hlist;
1c60b727 15033+ struct file *fi_file; /* very ugly */
4a4d8108 15034+} ____cacheline_aligned_in_smp;
1facf9fc 15035+
4a4d8108 15036+/* ---------------------------------------------------------------------- */
1facf9fc 15037+
4a4d8108
AM
15038+/* file.c */
15039+extern const struct address_space_operations aufs_aop;
15040+unsigned int au_file_roflags(unsigned int flags);
15041+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 15042+ struct file *file, int force_wr);
b912730e 15043+struct au_do_open_args {
8b6a4947 15044+ int aopen;
b912730e
AM
15045+ int (*open)(struct file *file, int flags,
15046+ struct file *h_file);
15047+ struct au_fidir *fidir;
15048+ struct file *h_file;
15049+};
15050+int au_do_open(struct file *file, struct au_do_open_args *args);
4a4d8108
AM
15051+int au_reopen_nondir(struct file *file);
15052+struct au_pin;
15053+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15054+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
521ced18 15055+ int wlock, unsigned int fi_lsc);
4a4d8108
AM
15056+int au_do_flush(struct file *file, fl_owner_t id,
15057+ int (*flush)(struct file *file, fl_owner_t id));
1facf9fc 15058+
4a4d8108
AM
15059+/* poll.c */
15060+#ifdef CONFIG_AUFS_POLL
15061+unsigned int aufs_poll(struct file *file, poll_table *wait);
15062+#endif
1facf9fc 15063+
4a4d8108
AM
15064+#ifdef CONFIG_AUFS_BR_HFSPLUS
15065+/* hfsplus.c */
392086de
AM
15066+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15067+ int force_wr);
4a4d8108
AM
15068+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15069+ struct file *h_file);
15070+#else
c1595e42
JR
15071+AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15072+ aufs_bindex_t bindex, int force_wr)
4a4d8108
AM
15073+AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15074+ struct file *h_file);
15075+#endif
1facf9fc 15076+
4a4d8108
AM
15077+/* f_op.c */
15078+extern const struct file_operations aufs_file_fop;
b912730e 15079+int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
4a4d8108 15080+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
521ced18 15081+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
4a4d8108 15082+
4a4d8108 15083+/* finfo.c */
f0c0a007 15084+void au_hfput(struct au_hfile *hf, int execed);
4a4d8108
AM
15085+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15086+ struct file *h_file);
1facf9fc 15087+
4a4d8108 15088+void au_update_figen(struct file *file);
4a4d8108 15089+struct au_fidir *au_fidir_alloc(struct super_block *sb);
e2f27e51 15090+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
1facf9fc 15091+
4a4d8108 15092+void au_fi_init_once(void *_fi);
1c60b727 15093+void au_finfo_fin(struct file *file);
4a4d8108 15094+int au_finfo_init(struct file *file, struct au_fidir *fidir);
1facf9fc 15095+
4a4d8108
AM
15096+/* ioctl.c */
15097+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
15098+#ifdef CONFIG_COMPAT
15099+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15100+ unsigned long arg);
c2b27bf2
AM
15101+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15102+ unsigned long arg);
b752ccd1 15103+#endif
1facf9fc 15104+
4a4d8108 15105+/* ---------------------------------------------------------------------- */
1facf9fc 15106+
4a4d8108
AM
15107+static inline struct au_finfo *au_fi(struct file *file)
15108+{
38d290e6 15109+ return file->private_data;
4a4d8108 15110+}
1facf9fc 15111+
4a4d8108 15112+/* ---------------------------------------------------------------------- */
1facf9fc 15113+
8b6a4947
AM
15114+#define fi_read_lock(f) au_rw_read_lock(&au_fi(f)->fi_rwsem)
15115+#define fi_write_lock(f) au_rw_write_lock(&au_fi(f)->fi_rwsem)
15116+#define fi_read_trylock(f) au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15117+#define fi_write_trylock(f) au_rw_write_trylock(&au_fi(f)->fi_rwsem)
4a4d8108 15118+/*
8b6a4947
AM
15119+#define fi_read_trylock_nested(f) \
15120+ au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15121+#define fi_write_trylock_nested(f) \
15122+ au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15123+*/
15124+
15125+#define fi_read_unlock(f) au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15126+#define fi_write_unlock(f) au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15127+#define fi_downgrade_lock(f) au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
1308ab2a 15128+
521ced18
JR
15129+/* lock subclass for finfo */
15130+enum {
15131+ AuLsc_FI_1,
15132+ AuLsc_FI_2
15133+};
15134+
15135+static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15136+{
15137+ au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15138+}
15139+
15140+static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15141+{
15142+ au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15143+}
15144+
15145+/*
15146+ * fi_read_lock_1, fi_write_lock_1,
15147+ * fi_read_lock_2, fi_write_lock_2
15148+ */
15149+#define AuReadLockFunc(name) \
15150+static inline void fi_read_lock_##name(struct file *f) \
15151+{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15152+
15153+#define AuWriteLockFunc(name) \
15154+static inline void fi_write_lock_##name(struct file *f) \
15155+{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15156+
15157+#define AuRWLockFuncs(name) \
15158+ AuReadLockFunc(name) \
15159+ AuWriteLockFunc(name)
15160+
15161+AuRWLockFuncs(1);
15162+AuRWLockFuncs(2);
15163+
15164+#undef AuReadLockFunc
15165+#undef AuWriteLockFunc
15166+#undef AuRWLockFuncs
15167+
4a4d8108
AM
15168+#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15169+#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15170+#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
1facf9fc 15171+
1308ab2a 15172+/* ---------------------------------------------------------------------- */
15173+
4a4d8108 15174+/* todo: hard/soft set? */
5afbbe0d 15175+static inline aufs_bindex_t au_fbtop(struct file *file)
dece6358 15176+{
4a4d8108
AM
15177+ FiMustAnyLock(file);
15178+ return au_fi(file)->fi_btop;
15179+}
dece6358 15180+
5afbbe0d 15181+static inline aufs_bindex_t au_fbbot_dir(struct file *file)
4a4d8108
AM
15182+{
15183+ FiMustAnyLock(file);
15184+ AuDebugOn(!au_fi(file)->fi_hdir);
15185+ return au_fi(file)->fi_hdir->fd_bbot;
15186+}
1facf9fc 15187+
4a4d8108
AM
15188+static inline struct au_vdir *au_fvdir_cache(struct file *file)
15189+{
15190+ FiMustAnyLock(file);
15191+ AuDebugOn(!au_fi(file)->fi_hdir);
15192+ return au_fi(file)->fi_hdir->fd_vdir_cache;
15193+}
1facf9fc 15194+
5afbbe0d 15195+static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
15196+{
15197+ FiMustWriteLock(file);
15198+ au_fi(file)->fi_btop = bindex;
15199+}
1facf9fc 15200+
5afbbe0d 15201+static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
15202+{
15203+ FiMustWriteLock(file);
15204+ AuDebugOn(!au_fi(file)->fi_hdir);
15205+ au_fi(file)->fi_hdir->fd_bbot = bindex;
15206+}
1308ab2a 15207+
4a4d8108
AM
15208+static inline void au_set_fvdir_cache(struct file *file,
15209+ struct au_vdir *vdir_cache)
15210+{
15211+ FiMustWriteLock(file);
15212+ AuDebugOn(!au_fi(file)->fi_hdir);
15213+ au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15214+}
dece6358 15215+
4a4d8108
AM
15216+static inline struct file *au_hf_top(struct file *file)
15217+{
15218+ FiMustAnyLock(file);
15219+ AuDebugOn(au_fi(file)->fi_hdir);
15220+ return au_fi(file)->fi_htop.hf_file;
15221+}
1facf9fc 15222+
4a4d8108
AM
15223+static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15224+{
15225+ FiMustAnyLock(file);
15226+ AuDebugOn(!au_fi(file)->fi_hdir);
15227+ return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
dece6358
AM
15228+}
15229+
4a4d8108
AM
15230+/* todo: memory barrier? */
15231+static inline unsigned int au_figen(struct file *f)
dece6358 15232+{
4a4d8108
AM
15233+ return atomic_read(&au_fi(f)->fi_generation);
15234+}
dece6358 15235+
2cbb1c4b
JR
15236+static inline void au_set_mmapped(struct file *f)
15237+{
15238+ if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15239+ return;
0c3ec466 15240+ pr_warn("fi_mmapped wrapped around\n");
2cbb1c4b
JR
15241+ while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15242+ ;
15243+}
15244+
15245+static inline void au_unset_mmapped(struct file *f)
15246+{
15247+ atomic_dec(&au_fi(f)->fi_mmapped);
15248+}
15249+
4a4d8108
AM
15250+static inline int au_test_mmapped(struct file *f)
15251+{
2cbb1c4b
JR
15252+ return atomic_read(&au_fi(f)->fi_mmapped);
15253+}
15254+
15255+/* customize vma->vm_file */
15256+
15257+static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15258+ struct file *file)
15259+{
53392da6
AM
15260+ struct file *f;
15261+
15262+ f = vma->vm_file;
2cbb1c4b
JR
15263+ get_file(file);
15264+ vma->vm_file = file;
53392da6 15265+ fput(f);
2cbb1c4b
JR
15266+}
15267+
15268+#ifdef CONFIG_MMU
15269+#define AuDbgVmRegion(file, vma) do {} while (0)
15270+
15271+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15272+ struct file *file)
15273+{
15274+ au_do_vm_file_reset(vma, file);
15275+}
15276+#else
15277+#define AuDbgVmRegion(file, vma) \
15278+ AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15279+
15280+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15281+ struct file *file)
15282+{
53392da6
AM
15283+ struct file *f;
15284+
2cbb1c4b 15285+ au_do_vm_file_reset(vma, file);
53392da6 15286+ f = vma->vm_region->vm_file;
2cbb1c4b
JR
15287+ get_file(file);
15288+ vma->vm_region->vm_file = file;
53392da6 15289+ fput(f);
2cbb1c4b
JR
15290+}
15291+#endif /* CONFIG_MMU */
15292+
15293+/* handle vma->vm_prfile */
fb47a38f 15294+static inline void au_vm_prfile_set(struct vm_area_struct *vma,
2cbb1c4b
JR
15295+ struct file *file)
15296+{
2cbb1c4b
JR
15297+ get_file(file);
15298+ vma->vm_prfile = file;
15299+#ifndef CONFIG_MMU
15300+ get_file(file);
15301+ vma->vm_region->vm_prfile = file;
15302+#endif
fb47a38f 15303+}
1308ab2a 15304+
4a4d8108
AM
15305+#endif /* __KERNEL__ */
15306+#endif /* __AUFS_FILE_H__ */
7f207e10
AM
15307diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15308--- /usr/share/empty/fs/aufs/finfo.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947 15309+++ linux/fs/aufs/finfo.c 2017-11-12 22:24:42.270843503 +0100
1c60b727 15310@@ -0,0 +1,148 @@
4a4d8108 15311+/*
a2654f78 15312+ * Copyright (C) 2005-2017 Junjiro R. Okajima
4a4d8108
AM
15313+ *
15314+ * This program, aufs is free software; you can redistribute it and/or modify
15315+ * it under the terms of the GNU General Public License as published by
15316+ * the Free Software Foundation; either version 2 of the License, or
15317+ * (at your option) any later version.
15318+ *
15319+ * This program is distributed in the hope that it will be useful,
15320+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15321+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15322+ * GNU General Public License for more details.
15323+ *
15324+ * You should have received a copy of the GNU General Public License
523b37e3 15325+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 15326+ */
1308ab2a 15327+
4a4d8108
AM
15328+/*
15329+ * file private data
15330+ */
1facf9fc 15331+
4a4d8108 15332+#include "aufs.h"
1facf9fc 15333+
f0c0a007 15334+void au_hfput(struct au_hfile *hf, int execed)
4a4d8108 15335+{
f0c0a007 15336+ if (execed)
4a4d8108
AM
15337+ allow_write_access(hf->hf_file);
15338+ fput(hf->hf_file);
15339+ hf->hf_file = NULL;
5afbbe0d 15340+ au_br_put(hf->hf_br);
4a4d8108
AM
15341+ hf->hf_br = NULL;
15342+}
1facf9fc 15343+
4a4d8108
AM
15344+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15345+{
15346+ struct au_finfo *finfo = au_fi(file);
15347+ struct au_hfile *hf;
15348+ struct au_fidir *fidir;
15349+
15350+ fidir = finfo->fi_hdir;
15351+ if (!fidir) {
15352+ AuDebugOn(finfo->fi_btop != bindex);
15353+ hf = &finfo->fi_htop;
15354+ } else
15355+ hf = fidir->fd_hfile + bindex;
15356+
15357+ if (hf && hf->hf_file)
f0c0a007 15358+ au_hfput(hf, vfsub_file_execed(file));
4a4d8108
AM
15359+ if (val) {
15360+ FiMustWriteLock(file);
b912730e 15361+ AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
4a4d8108 15362+ hf->hf_file = val;
2000de60 15363+ hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
1308ab2a 15364+ }
4a4d8108 15365+}
1facf9fc 15366+
4a4d8108
AM
15367+void au_update_figen(struct file *file)
15368+{
2000de60 15369+ atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
4a4d8108 15370+ /* smp_mb(); */ /* atomic_set */
1facf9fc 15371+}
15372+
4a4d8108
AM
15373+/* ---------------------------------------------------------------------- */
15374+
4a4d8108
AM
15375+struct au_fidir *au_fidir_alloc(struct super_block *sb)
15376+{
15377+ struct au_fidir *fidir;
15378+ int nbr;
15379+
5afbbe0d 15380+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
15381+ if (nbr < 2)
15382+ nbr = 2; /* initial allocate for 2 branches */
15383+ fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15384+ if (fidir) {
15385+ fidir->fd_bbot = -1;
15386+ fidir->fd_nent = nbr;
4a4d8108
AM
15387+ }
15388+
15389+ return fidir;
15390+}
15391+
e2f27e51 15392+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
4a4d8108
AM
15393+{
15394+ int err;
15395+ struct au_fidir *fidir, *p;
15396+
15397+ AuRwMustWriteLock(&finfo->fi_rwsem);
15398+ fidir = finfo->fi_hdir;
15399+ AuDebugOn(!fidir);
15400+
15401+ err = -ENOMEM;
15402+ p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
e2f27e51 15403+ GFP_NOFS, may_shrink);
4a4d8108
AM
15404+ if (p) {
15405+ p->fd_nent = nbr;
15406+ finfo->fi_hdir = p;
15407+ err = 0;
15408+ }
1facf9fc 15409+
dece6358 15410+ return err;
1facf9fc 15411+}
1308ab2a 15412+
15413+/* ---------------------------------------------------------------------- */
15414+
1c60b727 15415+void au_finfo_fin(struct file *file)
1308ab2a 15416+{
4a4d8108
AM
15417+ struct au_finfo *finfo;
15418+
2000de60 15419+ au_nfiles_dec(file->f_path.dentry->d_sb);
7f207e10 15420+
4a4d8108
AM
15421+ finfo = au_fi(file);
15422+ AuDebugOn(finfo->fi_hdir);
15423+ AuRwDestroy(&finfo->fi_rwsem);
1c60b727 15424+ au_cache_free_finfo(finfo);
1308ab2a 15425+}
1308ab2a 15426+
e49829fe 15427+void au_fi_init_once(void *_finfo)
4a4d8108 15428+{
e49829fe 15429+ struct au_finfo *finfo = _finfo;
1308ab2a 15430+
e49829fe 15431+ au_rw_init(&finfo->fi_rwsem);
4a4d8108 15432+}
1308ab2a 15433+
4a4d8108
AM
15434+int au_finfo_init(struct file *file, struct au_fidir *fidir)
15435+{
1716fcea 15436+ int err;
4a4d8108
AM
15437+ struct au_finfo *finfo;
15438+ struct dentry *dentry;
15439+
15440+ err = -ENOMEM;
2000de60 15441+ dentry = file->f_path.dentry;
4a4d8108
AM
15442+ finfo = au_cache_alloc_finfo();
15443+ if (unlikely(!finfo))
15444+ goto out;
15445+
15446+ err = 0;
7f207e10 15447+ au_nfiles_inc(dentry->d_sb);
4a4d8108
AM
15448+ au_rw_write_lock(&finfo->fi_rwsem);
15449+ finfo->fi_btop = -1;
15450+ finfo->fi_hdir = fidir;
15451+ atomic_set(&finfo->fi_generation, au_digen(dentry));
15452+ /* smp_mb(); */ /* atomic_set */
15453+
15454+ file->private_data = finfo;
15455+
15456+out:
15457+ return err;
15458+}
7f207e10
AM
15459diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15460--- /usr/share/empty/fs/aufs/f_op.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947 15461+++ linux/fs/aufs/f_op.c 2017-11-12 22:24:44.704244405 +0100
1c60b727 15462@@ -0,0 +1,817 @@
dece6358 15463+/*
a2654f78 15464+ * Copyright (C) 2005-2017 Junjiro R. Okajima
dece6358
AM
15465+ *
15466+ * This program, aufs is free software; you can redistribute it and/or modify
15467+ * it under the terms of the GNU General Public License as published by
15468+ * the Free Software Foundation; either version 2 of the License, or
15469+ * (at your option) any later version.
15470+ *
15471+ * This program is distributed in the hope that it will be useful,
15472+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15473+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15474+ * GNU General Public License for more details.
15475+ *
15476+ * You should have received a copy of the GNU General Public License
523b37e3 15477+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 15478+ */
1facf9fc 15479+
15480+/*
4a4d8108 15481+ * file and vm operations
1facf9fc 15482+ */
dece6358 15483+
86dc4139 15484+#include <linux/aio.h>
4a4d8108
AM
15485+#include <linux/fs_stack.h>
15486+#include <linux/mman.h>
4a4d8108 15487+#include <linux/security.h>
dece6358
AM
15488+#include "aufs.h"
15489+
b912730e 15490+int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
1facf9fc 15491+{
4a4d8108
AM
15492+ int err;
15493+ aufs_bindex_t bindex;
8cdd5066 15494+ struct dentry *dentry, *h_dentry;
4a4d8108 15495+ struct au_finfo *finfo;
38d290e6 15496+ struct inode *h_inode;
4a4d8108
AM
15497+
15498+ FiMustWriteLock(file);
15499+
523b37e3 15500+ err = 0;
2000de60 15501+ dentry = file->f_path.dentry;
b912730e 15502+ AuDebugOn(IS_ERR_OR_NULL(dentry));
4a4d8108
AM
15503+ finfo = au_fi(file);
15504+ memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
2cbb1c4b 15505+ atomic_set(&finfo->fi_mmapped, 0);
5afbbe0d 15506+ bindex = au_dbtop(dentry);
8cdd5066
JR
15507+ if (!h_file) {
15508+ h_dentry = au_h_dptr(dentry, bindex);
15509+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15510+ if (unlikely(err))
15511+ goto out;
b912730e 15512+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
8cdd5066
JR
15513+ } else {
15514+ h_dentry = h_file->f_path.dentry;
15515+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15516+ if (unlikely(err))
15517+ goto out;
b912730e 15518+ get_file(h_file);
8cdd5066 15519+ }
4a4d8108
AM
15520+ if (IS_ERR(h_file))
15521+ err = PTR_ERR(h_file);
15522+ else {
38d290e6
JR
15523+ if ((flags & __O_TMPFILE)
15524+ && !(flags & O_EXCL)) {
15525+ h_inode = file_inode(h_file);
15526+ spin_lock(&h_inode->i_lock);
15527+ h_inode->i_state |= I_LINKABLE;
15528+ spin_unlock(&h_inode->i_lock);
15529+ }
5afbbe0d 15530+ au_set_fbtop(file, bindex);
4a4d8108
AM
15531+ au_set_h_fptr(file, bindex, h_file);
15532+ au_update_figen(file);
15533+ /* todo: necessary? */
15534+ /* file->f_ra = h_file->f_ra; */
15535+ }
027c5e7a 15536+
8cdd5066 15537+out:
4a4d8108 15538+ return err;
1facf9fc 15539+}
15540+
4a4d8108
AM
15541+static int aufs_open_nondir(struct inode *inode __maybe_unused,
15542+ struct file *file)
1facf9fc 15543+{
4a4d8108 15544+ int err;
1308ab2a 15545+ struct super_block *sb;
b912730e
AM
15546+ struct au_do_open_args args = {
15547+ .open = au_do_open_nondir
15548+ };
1facf9fc 15549+
523b37e3
AM
15550+ AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15551+ file, vfsub_file_flags(file), file->f_mode);
1facf9fc 15552+
2000de60 15553+ sb = file->f_path.dentry->d_sb;
4a4d8108 15554+ si_read_lock(sb, AuLock_FLUSH);
b912730e 15555+ err = au_do_open(file, &args);
4a4d8108
AM
15556+ si_read_unlock(sb);
15557+ return err;
15558+}
1facf9fc 15559+
4a4d8108
AM
15560+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15561+{
15562+ struct au_finfo *finfo;
15563+ aufs_bindex_t bindex;
1facf9fc 15564+
4a4d8108 15565+ finfo = au_fi(file);
8b6a4947
AM
15566+ au_hbl_del(&finfo->fi_hlist,
15567+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108 15568+ bindex = finfo->fi_btop;
b4510431 15569+ if (bindex >= 0)
4a4d8108 15570+ au_set_h_fptr(file, bindex, NULL);
7f207e10 15571+
1c60b727 15572+ au_finfo_fin(file);
4a4d8108 15573+ return 0;
1facf9fc 15574+}
15575+
4a4d8108
AM
15576+/* ---------------------------------------------------------------------- */
15577+
15578+static int au_do_flush_nondir(struct file *file, fl_owner_t id)
dece6358 15579+{
1308ab2a 15580+ int err;
4a4d8108
AM
15581+ struct file *h_file;
15582+
15583+ err = 0;
15584+ h_file = au_hf_top(file);
15585+ if (h_file)
15586+ err = vfsub_flush(h_file, id);
15587+ return err;
15588+}
15589+
15590+static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15591+{
15592+ return au_do_flush(file, id, au_do_flush_nondir);
15593+}
15594+
15595+/* ---------------------------------------------------------------------- */
9dbd164d
AM
15596+/*
15597+ * read and write functions acquire [fdi]_rwsem once, but release before
15598+ * mmap_sem. This is because to stop a race condition between mmap(2).
15599+ * Releasing these aufs-rwsem should be safe, no branch-mamagement (by keeping
15600+ * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15601+ * read functions after [fdi]_rwsem are released, but it should be harmless.
15602+ */
4a4d8108 15603+
b912730e 15604+/* Callers should call au_read_post() or fput() in the end */
521ced18 15605+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
4a4d8108 15606+{
4a4d8108 15607+ struct file *h_file;
b912730e 15608+ int err;
1facf9fc 15609+
521ced18 15610+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
b912730e
AM
15611+ if (!err) {
15612+ di_read_unlock(file->f_path.dentry, AuLock_IR);
15613+ h_file = au_hf_top(file);
15614+ get_file(h_file);
15615+ if (!keep_fi)
15616+ fi_read_unlock(file);
15617+ } else
15618+ h_file = ERR_PTR(err);
15619+
15620+ return h_file;
15621+}
15622+
15623+static void au_read_post(struct inode *inode, struct file *h_file)
15624+{
15625+ /* update without lock, I don't think it a problem */
15626+ fsstack_copy_attr_atime(inode, file_inode(h_file));
15627+ fput(h_file);
15628+}
15629+
15630+struct au_write_pre {
521ced18
JR
15631+ /* input */
15632+ unsigned int lsc;
15633+
15634+ /* output */
b912730e 15635+ blkcnt_t blks;
5afbbe0d 15636+ aufs_bindex_t btop;
b912730e
AM
15637+};
15638+
15639+/*
15640+ * return with iinfo is write-locked
15641+ * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15642+ * end
15643+ */
15644+static struct file *au_write_pre(struct file *file, int do_ready,
15645+ struct au_write_pre *wpre)
15646+{
15647+ struct file *h_file;
15648+ struct dentry *dentry;
15649+ int err;
521ced18 15650+ unsigned int lsc;
b912730e
AM
15651+ struct au_pin pin;
15652+
521ced18
JR
15653+ lsc = 0;
15654+ if (wpre)
15655+ lsc = wpre->lsc;
15656+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
b912730e 15657+ h_file = ERR_PTR(err);
dece6358
AM
15658+ if (unlikely(err))
15659+ goto out;
1facf9fc 15660+
b912730e
AM
15661+ dentry = file->f_path.dentry;
15662+ if (do_ready) {
15663+ err = au_ready_to_write(file, -1, &pin);
15664+ if (unlikely(err)) {
15665+ h_file = ERR_PTR(err);
15666+ di_write_unlock(dentry);
15667+ goto out_fi;
15668+ }
15669+ }
15670+
15671+ di_downgrade_lock(dentry, /*flags*/0);
15672+ if (wpre)
5afbbe0d 15673+ wpre->btop = au_fbtop(file);
4a4d8108 15674+ h_file = au_hf_top(file);
9dbd164d 15675+ get_file(h_file);
b912730e
AM
15676+ if (wpre)
15677+ wpre->blks = file_inode(h_file)->i_blocks;
15678+ if (do_ready)
15679+ au_unpin(&pin);
15680+ di_read_unlock(dentry, /*flags*/0);
15681+
15682+out_fi:
15683+ fi_write_unlock(file);
15684+out:
15685+ return h_file;
15686+}
15687+
15688+static void au_write_post(struct inode *inode, struct file *h_file,
15689+ struct au_write_pre *wpre, ssize_t written)
15690+{
15691+ struct inode *h_inode;
15692+
15693+ au_cpup_attr_timesizes(inode);
5afbbe0d 15694+ AuDebugOn(au_ibtop(inode) != wpre->btop);
b912730e
AM
15695+ h_inode = file_inode(h_file);
15696+ inode->i_mode = h_inode->i_mode;
15697+ ii_write_unlock(inode);
b912730e
AM
15698+ /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15699+ if (written > 0)
5afbbe0d 15700+ au_fhsm_wrote(inode->i_sb, wpre->btop,
b912730e 15701+ /*force*/h_inode->i_blocks > wpre->blks);
1c60b727 15702+ fput(h_file);
b912730e
AM
15703+}
15704+
15705+static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
15706+ loff_t *ppos)
15707+{
15708+ ssize_t err;
15709+ struct inode *inode;
15710+ struct file *h_file;
15711+ struct super_block *sb;
15712+
15713+ inode = file_inode(file);
15714+ sb = inode->i_sb;
15715+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15716+
521ced18 15717+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
15718+ err = PTR_ERR(h_file);
15719+ if (IS_ERR(h_file))
15720+ goto out;
9dbd164d
AM
15721+
15722+ /* filedata may be obsoleted by concurrent copyup, but no problem */
4a4d8108
AM
15723+ err = vfsub_read_u(h_file, buf, count, ppos);
15724+ /* todo: necessary? */
15725+ /* file->f_ra = h_file->f_ra; */
b912730e 15726+ au_read_post(inode, h_file);
1308ab2a 15727+
4f0767ce 15728+out:
dece6358
AM
15729+ si_read_unlock(sb);
15730+ return err;
15731+}
1facf9fc 15732+
e49829fe
JR
15733+/*
15734+ * todo: very ugly
15735+ * it locks both of i_mutex and si_rwsem for read in safe.
15736+ * if the plink maintenance mode continues forever (that is the problem),
15737+ * may loop forever.
15738+ */
15739+static void au_mtx_and_read_lock(struct inode *inode)
15740+{
15741+ int err;
15742+ struct super_block *sb = inode->i_sb;
15743+
15744+ while (1) {
febd17d6 15745+ inode_lock(inode);
e49829fe
JR
15746+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15747+ if (!err)
15748+ break;
febd17d6 15749+ inode_unlock(inode);
e49829fe
JR
15750+ si_read_lock(sb, AuLock_NOPLMW);
15751+ si_read_unlock(sb);
15752+ }
15753+}
15754+
4a4d8108
AM
15755+static ssize_t aufs_write(struct file *file, const char __user *ubuf,
15756+ size_t count, loff_t *ppos)
dece6358 15757+{
4a4d8108 15758+ ssize_t err;
b912730e
AM
15759+ struct au_write_pre wpre;
15760+ struct inode *inode;
4a4d8108
AM
15761+ struct file *h_file;
15762+ char __user *buf = (char __user *)ubuf;
1facf9fc 15763+
b912730e 15764+ inode = file_inode(file);
e49829fe 15765+ au_mtx_and_read_lock(inode);
1facf9fc 15766+
521ced18 15767+ wpre.lsc = 0;
b912730e
AM
15768+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15769+ err = PTR_ERR(h_file);
15770+ if (IS_ERR(h_file))
9dbd164d 15771+ goto out;
9dbd164d 15772+
4a4d8108 15773+ err = vfsub_write_u(h_file, buf, count, ppos);
b912730e 15774+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15775+
4f0767ce 15776+out:
b912730e 15777+ si_read_unlock(inode->i_sb);
febd17d6 15778+ inode_unlock(inode);
dece6358
AM
15779+ return err;
15780+}
1facf9fc 15781+
076b876e
AM
15782+static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15783+ struct iov_iter *iov_iter)
dece6358 15784+{
4a4d8108
AM
15785+ ssize_t err;
15786+ struct file *file;
076b876e 15787+ ssize_t (*iter)(struct kiocb *, struct iov_iter *);
1facf9fc 15788+
4a4d8108
AM
15789+ err = security_file_permission(h_file, rw);
15790+ if (unlikely(err))
15791+ goto out;
1facf9fc 15792+
4a4d8108 15793+ err = -ENOSYS;
076b876e 15794+ iter = NULL;
5527c038 15795+ if (rw == MAY_READ)
076b876e 15796+ iter = h_file->f_op->read_iter;
5527c038 15797+ else if (rw == MAY_WRITE)
076b876e 15798+ iter = h_file->f_op->write_iter;
076b876e
AM
15799+
15800+ file = kio->ki_filp;
15801+ kio->ki_filp = h_file;
15802+ if (iter) {
2cbb1c4b 15803+ lockdep_off();
076b876e
AM
15804+ err = iter(kio, iov_iter);
15805+ lockdep_on();
4a4d8108
AM
15806+ } else
15807+ /* currently there is no such fs */
15808+ WARN_ON_ONCE(1);
076b876e 15809+ kio->ki_filp = file;
1facf9fc 15810+
4f0767ce 15811+out:
dece6358
AM
15812+ return err;
15813+}
1facf9fc 15814+
076b876e 15815+static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1facf9fc 15816+{
4a4d8108
AM
15817+ ssize_t err;
15818+ struct file *file, *h_file;
b912730e 15819+ struct inode *inode;
dece6358 15820+ struct super_block *sb;
1facf9fc 15821+
4a4d8108 15822+ file = kio->ki_filp;
b912730e
AM
15823+ inode = file_inode(file);
15824+ sb = inode->i_sb;
e49829fe 15825+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 15826+
521ced18 15827+ h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
b912730e
AM
15828+ err = PTR_ERR(h_file);
15829+ if (IS_ERR(h_file))
15830+ goto out;
9dbd164d 15831+
5afbbe0d
AM
15832+ if (au_test_loopback_kthread()) {
15833+ au_warn_loopback(h_file->f_path.dentry->d_sb);
15834+ if (file->f_mapping != h_file->f_mapping) {
15835+ file->f_mapping = h_file->f_mapping;
15836+ smp_mb(); /* unnecessary? */
15837+ }
15838+ }
15839+ fi_read_unlock(file);
15840+
076b876e 15841+ err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
4a4d8108
AM
15842+ /* todo: necessary? */
15843+ /* file->f_ra = h_file->f_ra; */
b912730e 15844+ au_read_post(inode, h_file);
1facf9fc 15845+
4f0767ce 15846+out:
4a4d8108 15847+ si_read_unlock(sb);
1308ab2a 15848+ return err;
15849+}
1facf9fc 15850+
076b876e 15851+static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1308ab2a 15852+{
4a4d8108 15853+ ssize_t err;
b912730e
AM
15854+ struct au_write_pre wpre;
15855+ struct inode *inode;
4a4d8108 15856+ struct file *file, *h_file;
1308ab2a 15857+
4a4d8108 15858+ file = kio->ki_filp;
b912730e 15859+ inode = file_inode(file);
e49829fe
JR
15860+ au_mtx_and_read_lock(inode);
15861+
521ced18 15862+ wpre.lsc = 0;
b912730e
AM
15863+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15864+ err = PTR_ERR(h_file);
15865+ if (IS_ERR(h_file))
9dbd164d 15866+ goto out;
9dbd164d 15867+
076b876e 15868+ err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
b912730e 15869+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15870+
4f0767ce 15871+out:
b912730e 15872+ si_read_unlock(inode->i_sb);
febd17d6 15873+ inode_unlock(inode);
dece6358 15874+ return err;
1facf9fc 15875+}
15876+
4a4d8108
AM
15877+static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15878+ struct pipe_inode_info *pipe, size_t len,
15879+ unsigned int flags)
1facf9fc 15880+{
4a4d8108
AM
15881+ ssize_t err;
15882+ struct file *h_file;
b912730e 15883+ struct inode *inode;
dece6358 15884+ struct super_block *sb;
1facf9fc 15885+
b912730e
AM
15886+ inode = file_inode(file);
15887+ sb = inode->i_sb;
e49829fe 15888+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 15889+
521ced18 15890+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
15891+ err = PTR_ERR(h_file);
15892+ if (IS_ERR(h_file))
dece6358 15893+ goto out;
1facf9fc 15894+
4a4d8108
AM
15895+ err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
15896+ /* todo: necessasry? */
15897+ /* file->f_ra = h_file->f_ra; */
b912730e 15898+ au_read_post(inode, h_file);
1facf9fc 15899+
4f0767ce 15900+out:
4a4d8108 15901+ si_read_unlock(sb);
dece6358 15902+ return err;
1facf9fc 15903+}
15904+
4a4d8108
AM
15905+static ssize_t
15906+aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15907+ size_t len, unsigned int flags)
1facf9fc 15908+{
4a4d8108 15909+ ssize_t err;
b912730e
AM
15910+ struct au_write_pre wpre;
15911+ struct inode *inode;
076b876e 15912+ struct file *h_file;
1facf9fc 15913+
b912730e 15914+ inode = file_inode(file);
e49829fe 15915+ au_mtx_and_read_lock(inode);
9dbd164d 15916+
521ced18 15917+ wpre.lsc = 0;
b912730e
AM
15918+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15919+ err = PTR_ERR(h_file);
15920+ if (IS_ERR(h_file))
9dbd164d 15921+ goto out;
9dbd164d 15922+
4a4d8108 15923+ err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
b912730e 15924+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15925+
4f0767ce 15926+out:
b912730e 15927+ si_read_unlock(inode->i_sb);
febd17d6 15928+ inode_unlock(inode);
4a4d8108
AM
15929+ return err;
15930+}
1facf9fc 15931+
38d290e6
JR
15932+static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15933+ loff_t len)
15934+{
15935+ long err;
b912730e 15936+ struct au_write_pre wpre;
38d290e6
JR
15937+ struct inode *inode;
15938+ struct file *h_file;
15939+
b912730e 15940+ inode = file_inode(file);
38d290e6
JR
15941+ au_mtx_and_read_lock(inode);
15942+
521ced18 15943+ wpre.lsc = 0;
b912730e
AM
15944+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15945+ err = PTR_ERR(h_file);
15946+ if (IS_ERR(h_file))
38d290e6 15947+ goto out;
38d290e6
JR
15948+
15949+ lockdep_off();
03673fb0 15950+ err = vfs_fallocate(h_file, mode, offset, len);
38d290e6 15951+ lockdep_on();
b912730e 15952+ au_write_post(inode, h_file, &wpre, /*written*/1);
38d290e6
JR
15953+
15954+out:
b912730e 15955+ si_read_unlock(inode->i_sb);
febd17d6 15956+ inode_unlock(inode);
38d290e6
JR
15957+ return err;
15958+}
15959+
521ced18
JR
15960+static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
15961+ struct file *dst, loff_t dst_pos,
15962+ size_t len, unsigned int flags)
15963+{
15964+ ssize_t err;
15965+ struct au_write_pre wpre;
15966+ enum { SRC, DST };
15967+ struct {
15968+ struct inode *inode;
15969+ struct file *h_file;
15970+ struct super_block *h_sb;
15971+ } a[2];
15972+#define a_src a[SRC]
15973+#define a_dst a[DST]
15974+
15975+ err = -EINVAL;
15976+ a_src.inode = file_inode(src);
15977+ if (unlikely(!S_ISREG(a_src.inode->i_mode)))
15978+ goto out;
15979+ a_dst.inode = file_inode(dst);
15980+ if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
15981+ goto out;
15982+
15983+ au_mtx_and_read_lock(a_dst.inode);
15984+ /*
15985+ * in order to match the order in di_write_lock2_{child,parent}(),
15986+ * use f_path.dentry for this comparision.
15987+ */
15988+ if (src->f_path.dentry < dst->f_path.dentry) {
15989+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
15990+ err = PTR_ERR(a_src.h_file);
15991+ if (IS_ERR(a_src.h_file))
15992+ goto out_si;
15993+
15994+ wpre.lsc = AuLsc_FI_2;
15995+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15996+ err = PTR_ERR(a_dst.h_file);
15997+ if (IS_ERR(a_dst.h_file)) {
15998+ au_read_post(a_src.inode, a_src.h_file);
15999+ goto out_si;
16000+ }
16001+ } else {
16002+ wpre.lsc = AuLsc_FI_1;
16003+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16004+ err = PTR_ERR(a_dst.h_file);
16005+ if (IS_ERR(a_dst.h_file))
16006+ goto out_si;
16007+
16008+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
16009+ err = PTR_ERR(a_src.h_file);
16010+ if (IS_ERR(a_src.h_file)) {
16011+ au_write_post(a_dst.inode, a_dst.h_file, &wpre,
16012+ /*written*/0);
16013+ goto out_si;
16014+ }
16015+ }
16016+
16017+ err = -EXDEV;
16018+ a_src.h_sb = file_inode(a_src.h_file)->i_sb;
16019+ a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
16020+ if (unlikely(a_src.h_sb != a_dst.h_sb)) {
16021+ AuDbgFile(src);
16022+ AuDbgFile(dst);
16023+ goto out_file;
16024+ }
16025+
16026+ err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
16027+ dst_pos, len, flags);
16028+
16029+out_file:
16030+ au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
16031+ fi_read_unlock(src);
16032+ au_read_post(a_src.inode, a_src.h_file);
16033+out_si:
16034+ si_read_unlock(a_dst.inode->i_sb);
16035+ inode_unlock(a_dst.inode);
16036+out:
16037+ return err;
16038+#undef a_src
16039+#undef a_dst
16040+}
16041+
4a4d8108
AM
16042+/* ---------------------------------------------------------------------- */
16043+
9dbd164d
AM
16044+/*
16045+ * The locking order around current->mmap_sem.
16046+ * - in most and regular cases
16047+ * file I/O syscall -- aufs_read() or something
16048+ * -- si_rwsem for read -- mmap_sem
16049+ * (Note that [fdi]i_rwsem are released before mmap_sem).
16050+ * - in mmap case
16051+ * mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
16052+ * This AB-BA order is definitly bad, but is not a problem since "si_rwsem for
16053+ * read" allows muliple processes to acquire it and [fdi]i_rwsem are not held in
16054+ * file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
16055+ * It means that when aufs acquires si_rwsem for write, the process should never
16056+ * acquire mmap_sem.
16057+ *
392086de 16058+ * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
9dbd164d
AM
16059+ * problem either since any directory is not able to be mmap-ed.
16060+ * The similar scenario is applied to aufs_readlink() too.
16061+ */
16062+
38d290e6 16063+#if 0 /* stop calling security_file_mmap() */
2dfbb274
AM
16064+/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
16065+#define AuConv_VM_PROT(f, b) _calc_vm_trans(f, VM_##b, PROT_##b)
16066+
16067+static unsigned long au_arch_prot_conv(unsigned long flags)
16068+{
16069+ /* currently ppc64 only */
16070+#ifdef CONFIG_PPC64
16071+ /* cf. linux/arch/powerpc/include/asm/mman.h */
16072+ AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
16073+ return AuConv_VM_PROT(flags, SAO);
16074+#else
16075+ AuDebugOn(arch_calc_vm_prot_bits(-1));
16076+ return 0;
16077+#endif
16078+}
16079+
16080+static unsigned long au_prot_conv(unsigned long flags)
16081+{
16082+ return AuConv_VM_PROT(flags, READ)
16083+ | AuConv_VM_PROT(flags, WRITE)
16084+ | AuConv_VM_PROT(flags, EXEC)
16085+ | au_arch_prot_conv(flags);
16086+}
16087+
16088+/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16089+#define AuConv_VM_MAP(f, b) _calc_vm_trans(f, VM_##b, MAP_##b)
16090+
16091+static unsigned long au_flag_conv(unsigned long flags)
16092+{
16093+ return AuConv_VM_MAP(flags, GROWSDOWN)
16094+ | AuConv_VM_MAP(flags, DENYWRITE)
2dfbb274
AM
16095+ | AuConv_VM_MAP(flags, LOCKED);
16096+}
38d290e6 16097+#endif
2dfbb274 16098+
9dbd164d 16099+static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
dece6358 16100+{
4a4d8108 16101+ int err;
4a4d8108 16102+ const unsigned char wlock
9dbd164d 16103+ = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
4a4d8108 16104+ struct super_block *sb;
9dbd164d 16105+ struct file *h_file;
b912730e 16106+ struct inode *inode;
9dbd164d
AM
16107+
16108+ AuDbgVmRegion(file, vma);
1308ab2a 16109+
b912730e
AM
16110+ inode = file_inode(file);
16111+ sb = inode->i_sb;
9dbd164d 16112+ lockdep_off();
e49829fe 16113+ si_read_lock(sb, AuLock_NOPLMW);
4a4d8108 16114+
b912730e 16115+ h_file = au_write_pre(file, wlock, /*wpre*/NULL);
9dbd164d 16116+ lockdep_on();
b912730e
AM
16117+ err = PTR_ERR(h_file);
16118+ if (IS_ERR(h_file))
16119+ goto out;
1308ab2a 16120+
b912730e
AM
16121+ err = 0;
16122+ au_set_mmapped(file);
9dbd164d 16123+ au_vm_file_reset(vma, h_file);
38d290e6
JR
16124+ /*
16125+ * we cannot call security_mmap_file() here since it may acquire
16126+ * mmap_sem or i_mutex.
16127+ *
16128+ * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16129+ * au_flag_conv(vma->vm_flags));
16130+ */
9dbd164d 16131+ if (!err)
521ced18 16132+ err = call_mmap(h_file, vma);
b912730e
AM
16133+ if (!err) {
16134+ au_vm_prfile_set(vma, file);
16135+ fsstack_copy_attr_atime(inode, file_inode(h_file));
16136+ goto out_fput; /* success */
16137+ }
2cbb1c4b
JR
16138+ au_unset_mmapped(file);
16139+ au_vm_file_reset(vma, file);
b912730e 16140+
2cbb1c4b 16141+out_fput:
9dbd164d 16142+ lockdep_off();
b912730e
AM
16143+ ii_write_unlock(inode);
16144+ lockdep_on();
16145+ fput(h_file);
4f0767ce 16146+out:
b912730e 16147+ lockdep_off();
9dbd164d
AM
16148+ si_read_unlock(sb);
16149+ lockdep_on();
16150+ AuTraceErr(err);
4a4d8108
AM
16151+ return err;
16152+}
16153+
16154+/* ---------------------------------------------------------------------- */
16155+
1e00d052
AM
16156+static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16157+ int datasync)
4a4d8108
AM
16158+{
16159+ int err;
b912730e 16160+ struct au_write_pre wpre;
4a4d8108
AM
16161+ struct inode *inode;
16162+ struct file *h_file;
4a4d8108
AM
16163+
16164+ err = 0; /* -EBADF; */ /* posix? */
16165+ if (unlikely(!(file->f_mode & FMODE_WRITE)))
b912730e 16166+ goto out;
4a4d8108 16167+
b912730e
AM
16168+ inode = file_inode(file);
16169+ au_mtx_and_read_lock(inode);
16170+
521ced18 16171+ wpre.lsc = 0;
b912730e
AM
16172+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16173+ err = PTR_ERR(h_file);
16174+ if (IS_ERR(h_file))
4a4d8108 16175+ goto out_unlock;
4a4d8108 16176+
53392da6 16177+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
b912730e 16178+ au_write_post(inode, h_file, &wpre, /*written*/0);
4a4d8108 16179+
4f0767ce 16180+out_unlock:
b912730e 16181+ si_read_unlock(inode->i_sb);
febd17d6 16182+ inode_unlock(inode);
b912730e 16183+out:
4a4d8108 16184+ return err;
dece6358
AM
16185+}
16186+
4a4d8108 16187+static int aufs_fasync(int fd, struct file *file, int flag)
dece6358 16188+{
4a4d8108
AM
16189+ int err;
16190+ struct file *h_file;
4a4d8108 16191+ struct super_block *sb;
1308ab2a 16192+
b912730e 16193+ sb = file->f_path.dentry->d_sb;
e49829fe 16194+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 16195+
521ced18 16196+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
16197+ err = PTR_ERR(h_file);
16198+ if (IS_ERR(h_file))
4a4d8108
AM
16199+ goto out;
16200+
523b37e3 16201+ if (h_file->f_op->fasync)
4a4d8108 16202+ err = h_file->f_op->fasync(fd, h_file, flag);
b912730e 16203+ fput(h_file); /* instead of au_read_post() */
1308ab2a 16204+
4f0767ce 16205+out:
4a4d8108 16206+ si_read_unlock(sb);
1308ab2a 16207+ return err;
dece6358 16208+}
4a4d8108 16209+
febd17d6
JR
16210+static int aufs_setfl(struct file *file, unsigned long arg)
16211+{
16212+ int err;
16213+ struct file *h_file;
16214+ struct super_block *sb;
16215+
16216+ sb = file->f_path.dentry->d_sb;
16217+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16218+
521ced18 16219+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
febd17d6
JR
16220+ err = PTR_ERR(h_file);
16221+ if (IS_ERR(h_file))
16222+ goto out;
16223+
1c60b727
AM
16224+ /* stop calling h_file->fasync */
16225+ arg |= vfsub_file_flags(file) & FASYNC;
febd17d6
JR
16226+ err = setfl(/*unused fd*/-1, h_file, arg);
16227+ fput(h_file); /* instead of au_read_post() */
16228+
16229+out:
16230+ si_read_unlock(sb);
16231+ return err;
16232+}
16233+
4a4d8108
AM
16234+/* ---------------------------------------------------------------------- */
16235+
16236+/* no one supports this operation, currently */
16237+#if 0
16238+static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
2000de60 16239+ size_t len, loff_t *pos, int more)
4a4d8108
AM
16240+{
16241+}
16242+#endif
16243+
16244+/* ---------------------------------------------------------------------- */
16245+
16246+const struct file_operations aufs_file_fop = {
16247+ .owner = THIS_MODULE,
2cbb1c4b 16248+
027c5e7a 16249+ .llseek = default_llseek,
4a4d8108
AM
16250+
16251+ .read = aufs_read,
16252+ .write = aufs_write,
076b876e
AM
16253+ .read_iter = aufs_read_iter,
16254+ .write_iter = aufs_write_iter,
16255+
4a4d8108
AM
16256+#ifdef CONFIG_AUFS_POLL
16257+ .poll = aufs_poll,
16258+#endif
16259+ .unlocked_ioctl = aufs_ioctl_nondir,
b752ccd1 16260+#ifdef CONFIG_COMPAT
c2b27bf2 16261+ .compat_ioctl = aufs_compat_ioctl_nondir,
b752ccd1 16262+#endif
4a4d8108
AM
16263+ .mmap = aufs_mmap,
16264+ .open = aufs_open_nondir,
16265+ .flush = aufs_flush_nondir,
16266+ .release = aufs_release_nondir,
16267+ .fsync = aufs_fsync_nondir,
4a4d8108
AM
16268+ .fasync = aufs_fasync,
16269+ /* .sendpage = aufs_sendpage, */
febd17d6 16270+ .setfl = aufs_setfl,
4a4d8108
AM
16271+ .splice_write = aufs_splice_write,
16272+ .splice_read = aufs_splice_read,
16273+#if 0
16274+ .aio_splice_write = aufs_aio_splice_write,
38d290e6 16275+ .aio_splice_read = aufs_aio_splice_read,
4a4d8108 16276+#endif
521ced18
JR
16277+ .fallocate = aufs_fallocate,
16278+ .copy_file_range = aufs_copy_file_range
4a4d8108 16279+};
7f207e10
AM
16280diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
16281--- /usr/share/empty/fs/aufs/fstype.h 1970-01-01 01:00:00.000000000 +0100
1c60b727 16282+++ linux/fs/aufs/fstype.h 2017-07-29 12:14:25.903042072 +0200
b912730e 16283@@ -0,0 +1,400 @@
4a4d8108 16284+/*
a2654f78 16285+ * Copyright (C) 2005-2017 Junjiro R. Okajima
4a4d8108
AM
16286+ *
16287+ * This program, aufs is free software; you can redistribute it and/or modify
16288+ * it under the terms of the GNU General Public License as published by
16289+ * the Free Software Foundation; either version 2 of the License, or
16290+ * (at your option) any later version.
16291+ *
16292+ * This program is distributed in the hope that it will be useful,
16293+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16294+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16295+ * GNU General Public License for more details.
16296+ *
16297+ * You should have received a copy of the GNU General Public License
523b37e3 16298+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
16299+ */
16300+
16301+/*
16302+ * judging filesystem type
16303+ */
16304+
16305+#ifndef __AUFS_FSTYPE_H__
16306+#define __AUFS_FSTYPE_H__
16307+
16308+#ifdef __KERNEL__
16309+
16310+#include <linux/fs.h>
16311+#include <linux/magic.h>
b912730e 16312+#include <linux/nfs_fs.h>
b95c5147 16313+#include <linux/romfs_fs.h>
4a4d8108
AM
16314+
16315+static inline int au_test_aufs(struct super_block *sb)
16316+{
16317+ return sb->s_magic == AUFS_SUPER_MAGIC;
16318+}
16319+
16320+static inline const char *au_sbtype(struct super_block *sb)
16321+{
16322+ return sb->s_type->name;
16323+}
1308ab2a 16324+
16325+static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
16326+{
f0c0a007 16327+#if IS_ENABLED(CONFIG_ISO9660_FS)
2000de60 16328+ return sb->s_magic == ISOFS_SUPER_MAGIC;
dece6358
AM
16329+#else
16330+ return 0;
16331+#endif
16332+}
16333+
1308ab2a 16334+static inline int au_test_romfs(struct super_block *sb __maybe_unused)
dece6358 16335+{
f0c0a007 16336+#if IS_ENABLED(CONFIG_ROMFS_FS)
2000de60 16337+ return sb->s_magic == ROMFS_MAGIC;
dece6358
AM
16338+#else
16339+ return 0;
16340+#endif
16341+}
16342+
1308ab2a 16343+static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
dece6358 16344+{
f0c0a007 16345+#if IS_ENABLED(CONFIG_CRAMFS)
1308ab2a 16346+ return sb->s_magic == CRAMFS_MAGIC;
16347+#endif
16348+ return 0;
16349+}
16350+
16351+static inline int au_test_nfs(struct super_block *sb __maybe_unused)
16352+{
f0c0a007 16353+#if IS_ENABLED(CONFIG_NFS_FS)
1308ab2a 16354+ return sb->s_magic == NFS_SUPER_MAGIC;
dece6358
AM
16355+#else
16356+ return 0;
16357+#endif
16358+}
16359+
1308ab2a 16360+static inline int au_test_fuse(struct super_block *sb __maybe_unused)
dece6358 16361+{
f0c0a007 16362+#if IS_ENABLED(CONFIG_FUSE_FS)
1308ab2a 16363+ return sb->s_magic == FUSE_SUPER_MAGIC;
dece6358
AM
16364+#else
16365+ return 0;
16366+#endif
16367+}
16368+
1308ab2a 16369+static inline int au_test_xfs(struct super_block *sb __maybe_unused)
dece6358 16370+{
f0c0a007 16371+#if IS_ENABLED(CONFIG_XFS_FS)
1308ab2a 16372+ return sb->s_magic == XFS_SB_MAGIC;
dece6358
AM
16373+#else
16374+ return 0;
16375+#endif
16376+}
16377+
1308ab2a 16378+static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
dece6358 16379+{
1308ab2a 16380+#ifdef CONFIG_TMPFS
16381+ return sb->s_magic == TMPFS_MAGIC;
16382+#else
16383+ return 0;
dece6358 16384+#endif
dece6358
AM
16385+}
16386+
1308ab2a 16387+static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
1facf9fc 16388+{
f0c0a007 16389+#if IS_ENABLED(CONFIG_ECRYPT_FS)
1308ab2a 16390+ return !strcmp(au_sbtype(sb), "ecryptfs");
16391+#else
16392+ return 0;
16393+#endif
1facf9fc 16394+}
16395+
1308ab2a 16396+static inline int au_test_ramfs(struct super_block *sb)
16397+{
16398+ return sb->s_magic == RAMFS_MAGIC;
16399+}
16400+
16401+static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
16402+{
f0c0a007 16403+#if IS_ENABLED(CONFIG_UBIFS_FS)
1308ab2a 16404+ return sb->s_magic == UBIFS_SUPER_MAGIC;
16405+#else
16406+ return 0;
16407+#endif
16408+}
16409+
16410+static inline int au_test_procfs(struct super_block *sb __maybe_unused)
16411+{
16412+#ifdef CONFIG_PROC_FS
16413+ return sb->s_magic == PROC_SUPER_MAGIC;
16414+#else
16415+ return 0;
16416+#endif
16417+}
16418+
16419+static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
16420+{
16421+#ifdef CONFIG_SYSFS
16422+ return sb->s_magic == SYSFS_MAGIC;
16423+#else
16424+ return 0;
16425+#endif
16426+}
16427+
16428+static inline int au_test_configfs(struct super_block *sb __maybe_unused)
16429+{
f0c0a007 16430+#if IS_ENABLED(CONFIG_CONFIGFS_FS)
1308ab2a 16431+ return sb->s_magic == CONFIGFS_MAGIC;
16432+#else
16433+ return 0;
16434+#endif
16435+}
16436+
16437+static inline int au_test_minix(struct super_block *sb __maybe_unused)
16438+{
f0c0a007 16439+#if IS_ENABLED(CONFIG_MINIX_FS)
1308ab2a 16440+ return sb->s_magic == MINIX3_SUPER_MAGIC
16441+ || sb->s_magic == MINIX2_SUPER_MAGIC
16442+ || sb->s_magic == MINIX2_SUPER_MAGIC2
16443+ || sb->s_magic == MINIX_SUPER_MAGIC
16444+ || sb->s_magic == MINIX_SUPER_MAGIC2;
16445+#else
16446+ return 0;
16447+#endif
16448+}
16449+
1308ab2a 16450+static inline int au_test_fat(struct super_block *sb __maybe_unused)
16451+{
f0c0a007 16452+#if IS_ENABLED(CONFIG_FAT_FS)
1308ab2a 16453+ return sb->s_magic == MSDOS_SUPER_MAGIC;
16454+#else
16455+ return 0;
16456+#endif
16457+}
16458+
16459+static inline int au_test_msdos(struct super_block *sb)
16460+{
16461+ return au_test_fat(sb);
16462+}
16463+
16464+static inline int au_test_vfat(struct super_block *sb)
16465+{
16466+ return au_test_fat(sb);
16467+}
16468+
16469+static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
16470+{
16471+#ifdef CONFIG_SECURITYFS
16472+ return sb->s_magic == SECURITYFS_MAGIC;
16473+#else
16474+ return 0;
16475+#endif
16476+}
16477+
16478+static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
16479+{
f0c0a007 16480+#if IS_ENABLED(CONFIG_SQUASHFS)
1308ab2a 16481+ return sb->s_magic == SQUASHFS_MAGIC;
16482+#else
16483+ return 0;
16484+#endif
16485+}
16486+
16487+static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
16488+{
f0c0a007 16489+#if IS_ENABLED(CONFIG_BTRFS_FS)
1308ab2a 16490+ return sb->s_magic == BTRFS_SUPER_MAGIC;
16491+#else
16492+ return 0;
16493+#endif
16494+}
16495+
16496+static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
16497+{
f0c0a007 16498+#if IS_ENABLED(CONFIG_XENFS)
1308ab2a 16499+ return sb->s_magic == XENFS_SUPER_MAGIC;
16500+#else
16501+ return 0;
16502+#endif
16503+}
16504+
16505+static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
16506+{
16507+#ifdef CONFIG_DEBUG_FS
16508+ return sb->s_magic == DEBUGFS_MAGIC;
16509+#else
16510+ return 0;
16511+#endif
16512+}
16513+
16514+static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
16515+{
f0c0a007 16516+#if IS_ENABLED(CONFIG_NILFS)
1308ab2a 16517+ return sb->s_magic == NILFS_SUPER_MAGIC;
16518+#else
16519+ return 0;
16520+#endif
16521+}
16522+
4a4d8108
AM
16523+static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
16524+{
f0c0a007 16525+#if IS_ENABLED(CONFIG_HFSPLUS_FS)
4a4d8108
AM
16526+ return sb->s_magic == HFSPLUS_SUPER_MAGIC;
16527+#else
16528+ return 0;
16529+#endif
16530+}
16531+
1308ab2a 16532+/* ---------------------------------------------------------------------- */
16533+/*
16534+ * they can't be an aufs branch.
16535+ */
16536+static inline int au_test_fs_unsuppoted(struct super_block *sb)
16537+{
16538+ return
16539+#ifndef CONFIG_AUFS_BR_RAMFS
16540+ au_test_ramfs(sb) ||
16541+#endif
16542+ au_test_procfs(sb)
16543+ || au_test_sysfs(sb)
16544+ || au_test_configfs(sb)
16545+ || au_test_debugfs(sb)
16546+ || au_test_securityfs(sb)
16547+ || au_test_xenfs(sb)
16548+ || au_test_ecryptfs(sb)
16549+ /* || !strcmp(au_sbtype(sb), "unionfs") */
16550+ || au_test_aufs(sb); /* will be supported in next version */
16551+}
16552+
1308ab2a 16553+static inline int au_test_fs_remote(struct super_block *sb)
16554+{
16555+ return !au_test_tmpfs(sb)
16556+#ifdef CONFIG_AUFS_BR_RAMFS
16557+ && !au_test_ramfs(sb)
16558+#endif
16559+ && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
16560+}
16561+
16562+/* ---------------------------------------------------------------------- */
16563+
16564+/*
16565+ * Note: these functions (below) are created after reading ->getattr() in all
16566+ * filesystems under linux/fs. it means we have to do so in every update...
16567+ */
16568+
16569+/*
16570+ * some filesystems require getattr to refresh the inode attributes before
16571+ * referencing.
16572+ * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
16573+ * and leave the work for d_revalidate()
16574+ */
16575+static inline int au_test_fs_refresh_iattr(struct super_block *sb)
16576+{
16577+ return au_test_nfs(sb)
16578+ || au_test_fuse(sb)
1308ab2a 16579+ /* || au_test_btrfs(sb) */ /* untested */
1308ab2a 16580+ ;
16581+}
16582+
16583+/*
16584+ * filesystems which don't maintain i_size or i_blocks.
16585+ */
16586+static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
16587+{
16588+ return au_test_xfs(sb)
4a4d8108
AM
16589+ || au_test_btrfs(sb)
16590+ || au_test_ubifs(sb)
16591+ || au_test_hfsplus(sb) /* maintained, but incorrect */
1308ab2a 16592+ /* || au_test_minix(sb) */ /* untested */
16593+ ;
16594+}
16595+
16596+/*
16597+ * filesystems which don't store the correct value in some of their inode
16598+ * attributes.
16599+ */
16600+static inline int au_test_fs_bad_iattr(struct super_block *sb)
16601+{
16602+ return au_test_fs_bad_iattr_size(sb)
1308ab2a 16603+ || au_test_fat(sb)
16604+ || au_test_msdos(sb)
16605+ || au_test_vfat(sb);
1facf9fc 16606+}
16607+
16608+/* they don't check i_nlink in link(2) */
16609+static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
16610+{
16611+ return au_test_tmpfs(sb)
16612+#ifdef CONFIG_AUFS_BR_RAMFS
16613+ || au_test_ramfs(sb)
16614+#endif
4a4d8108 16615+ || au_test_ubifs(sb)
4a4d8108 16616+ || au_test_hfsplus(sb);
1facf9fc 16617+}
16618+
16619+/*
16620+ * filesystems which sets S_NOATIME and S_NOCMTIME.
16621+ */
16622+static inline int au_test_fs_notime(struct super_block *sb)
16623+{
16624+ return au_test_nfs(sb)
16625+ || au_test_fuse(sb)
dece6358 16626+ || au_test_ubifs(sb)
1facf9fc 16627+ ;
16628+}
16629+
1facf9fc 16630+/* temporary support for i#1 in cramfs */
16631+static inline int au_test_fs_unique_ino(struct inode *inode)
16632+{
16633+ if (au_test_cramfs(inode->i_sb))
16634+ return inode->i_ino != 1;
16635+ return 1;
16636+}
16637+
16638+/* ---------------------------------------------------------------------- */
16639+
16640+/*
16641+ * the filesystem where the xino files placed must support i/o after unlink and
16642+ * maintain i_size and i_blocks.
16643+ */
16644+static inline int au_test_fs_bad_xino(struct super_block *sb)
16645+{
16646+ return au_test_fs_remote(sb)
16647+ || au_test_fs_bad_iattr_size(sb)
1facf9fc 16648+ /* don't want unnecessary work for xino */
16649+ || au_test_aufs(sb)
1308ab2a 16650+ || au_test_ecryptfs(sb)
16651+ || au_test_nilfs(sb);
1facf9fc 16652+}
16653+
16654+static inline int au_test_fs_trunc_xino(struct super_block *sb)
16655+{
16656+ return au_test_tmpfs(sb)
16657+ || au_test_ramfs(sb);
16658+}
16659+
16660+/*
16661+ * test if the @sb is real-readonly.
16662+ */
16663+static inline int au_test_fs_rr(struct super_block *sb)
16664+{
16665+ return au_test_squashfs(sb)
16666+ || au_test_iso9660(sb)
16667+ || au_test_cramfs(sb)
16668+ || au_test_romfs(sb);
16669+}
16670+
b912730e
AM
16671+/*
16672+ * test if the @inode is nfs with 'noacl' option
16673+ * NFS always sets MS_POSIXACL regardless its mount option 'noacl.'
16674+ */
16675+static inline int au_test_nfs_noacl(struct inode *inode)
16676+{
16677+ return au_test_nfs(inode->i_sb)
16678+ /* && IS_POSIXACL(inode) */
16679+ && !nfs_server_capable(inode, NFS_CAP_ACLS);
16680+}
16681+
1facf9fc 16682+#endif /* __KERNEL__ */
16683+#endif /* __AUFS_FSTYPE_H__ */
8b6a4947
AM
16684diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
16685--- /usr/share/empty/fs/aufs/hbl.h 1970-01-01 01:00:00.000000000 +0100
16686+++ linux/fs/aufs/hbl.h 2017-11-12 22:24:44.707577830 +0100
16687@@ -0,0 +1,64 @@
16688+/*
16689+ * Copyright (C) 2017 Junjiro R. Okajima
16690+ *
16691+ * This program, aufs is free software; you can redistribute it and/or modify
16692+ * it under the terms of the GNU General Public License as published by
16693+ * the Free Software Foundation; either version 2 of the License, or
16694+ * (at your option) any later version.
16695+ *
16696+ * This program is distributed in the hope that it will be useful,
16697+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16698+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16699+ * GNU General Public License for more details.
16700+ *
16701+ * You should have received a copy of the GNU General Public License
16702+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16703+ */
16704+
16705+/*
16706+ * helpers for hlist_bl.h
16707+ */
16708+
16709+#ifndef __AUFS_HBL_H__
16710+#define __AUFS_HBL_H__
16711+
16712+#ifdef __KERNEL__
16713+
16714+#include <linux/list_bl.h>
16715+
16716+static inline void au_hbl_add(struct hlist_bl_node *node,
16717+ struct hlist_bl_head *hbl)
16718+{
16719+ hlist_bl_lock(hbl);
16720+ hlist_bl_add_head(node, hbl);
16721+ hlist_bl_unlock(hbl);
16722+}
16723+
16724+static inline void au_hbl_del(struct hlist_bl_node *node,
16725+ struct hlist_bl_head *hbl)
16726+{
16727+ hlist_bl_lock(hbl);
16728+ hlist_bl_del(node);
16729+ hlist_bl_unlock(hbl);
16730+}
16731+
16732+#define au_hbl_for_each(pos, head) \
16733+ for (pos = hlist_bl_first(head); \
16734+ pos; \
16735+ pos = pos->next)
16736+
16737+static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
16738+{
16739+ unsigned long cnt;
16740+ struct hlist_bl_node *pos;
16741+
16742+ cnt = 0;
16743+ hlist_bl_lock(hbl);
16744+ au_hbl_for_each(pos, hbl)
16745+ cnt++;
16746+ hlist_bl_unlock(hbl);
16747+ return cnt;
16748+}
16749+
16750+#endif /* __KERNEL__ */
16751+#endif /* __AUFS_HBL_H__ */
7f207e10
AM
16752diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
16753--- /usr/share/empty/fs/aufs/hfsnotify.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947 16754+++ linux/fs/aufs/hfsnotify.c 2017-11-12 22:24:44.707577830 +0100
ffa93bbd 16755@@ -0,0 +1,289 @@
1facf9fc 16756+/*
a2654f78 16757+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 16758+ *
16759+ * This program, aufs is free software; you can redistribute it and/or modify
16760+ * it under the terms of the GNU General Public License as published by
16761+ * the Free Software Foundation; either version 2 of the License, or
16762+ * (at your option) any later version.
dece6358
AM
16763+ *
16764+ * This program is distributed in the hope that it will be useful,
16765+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16766+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16767+ * GNU General Public License for more details.
16768+ *
16769+ * You should have received a copy of the GNU General Public License
523b37e3 16770+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 16771+ */
16772+
16773+/*
4a4d8108 16774+ * fsnotify for the lower directories
1facf9fc 16775+ */
16776+
16777+#include "aufs.h"
16778+
4a4d8108
AM
16779+/* FS_IN_IGNORED is unnecessary */
16780+static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
16781+ | FS_CREATE | FS_EVENT_ON_CHILD);
7f207e10 16782+static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
7eafdf33 16783+static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
1facf9fc 16784+
0c5527e5 16785+static void au_hfsn_free_mark(struct fsnotify_mark *mark)
1facf9fc 16786+{
0c5527e5
AM
16787+ struct au_hnotify *hn = container_of(mark, struct au_hnotify,
16788+ hn_mark);
5afbbe0d 16789+ /* AuDbg("here\n"); */
1c60b727 16790+ au_cache_free_hnotify(hn);
8b6a4947 16791+ smp_mb__before_atomic(); /* for atomic64_dec */
1716fcea
AM
16792+ if (atomic64_dec_and_test(&au_hfsn_ifree))
16793+ wake_up(&au_hfsn_wq);
4a4d8108 16794+}
1facf9fc 16795+
027c5e7a 16796+static int au_hfsn_alloc(struct au_hinode *hinode)
4a4d8108 16797+{
1716fcea 16798+ int err;
027c5e7a
AM
16799+ struct au_hnotify *hn;
16800+ struct super_block *sb;
16801+ struct au_branch *br;
0c5527e5 16802+ struct fsnotify_mark *mark;
027c5e7a 16803+ aufs_bindex_t bindex;
1facf9fc 16804+
027c5e7a
AM
16805+ hn = hinode->hi_notify;
16806+ sb = hn->hn_aufs_inode->i_sb;
16807+ bindex = au_br_index(sb, hinode->hi_id);
16808+ br = au_sbr(sb, bindex);
1716fcea
AM
16809+ AuDebugOn(!br->br_hfsn);
16810+
0c5527e5 16811+ mark = &hn->hn_mark;
ffa93bbd 16812+ fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
0c5527e5 16813+ mark->mask = AuHfsnMask;
7f207e10
AM
16814+ /*
16815+ * by udba rename or rmdir, aufs assign a new inode to the known
16816+ * h_inode, so specify 1 to allow dups.
16817+ */
c1595e42 16818+ lockdep_off();
ffa93bbd
AM
16819+ err = fsnotify_add_mark(mark, hinode->hi_inode, /*mnt*/NULL,
16820+ /*allow_dups*/1);
c1595e42 16821+ lockdep_on();
1716fcea
AM
16822+
16823+ return err;
1facf9fc 16824+}
16825+
7eafdf33 16826+static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
1facf9fc 16827+{
0c5527e5 16828+ struct fsnotify_mark *mark;
7eafdf33 16829+ unsigned long long ull;
1716fcea 16830+ struct fsnotify_group *group;
7eafdf33
AM
16831+
16832+ ull = atomic64_inc_return(&au_hfsn_ifree);
16833+ BUG_ON(!ull);
953406b4 16834+
0c5527e5 16835+ mark = &hn->hn_mark;
1716fcea
AM
16836+ spin_lock(&mark->lock);
16837+ group = mark->group;
16838+ fsnotify_get_group(group);
16839+ spin_unlock(&mark->lock);
c1595e42 16840+ lockdep_off();
1716fcea 16841+ fsnotify_destroy_mark(mark, group);
5afbbe0d 16842+ fsnotify_put_mark(mark);
1716fcea 16843+ fsnotify_put_group(group);
c1595e42 16844+ lockdep_on();
7f207e10 16845+
7eafdf33
AM
16846+ /* free hn by myself */
16847+ return 0;
1facf9fc 16848+}
16849+
16850+/* ---------------------------------------------------------------------- */
16851+
4a4d8108 16852+static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
1facf9fc 16853+{
0c5527e5 16854+ struct fsnotify_mark *mark;
1facf9fc 16855+
0c5527e5
AM
16856+ mark = &hinode->hi_notify->hn_mark;
16857+ spin_lock(&mark->lock);
1facf9fc 16858+ if (do_set) {
0c5527e5
AM
16859+ AuDebugOn(mark->mask & AuHfsnMask);
16860+ mark->mask |= AuHfsnMask;
1facf9fc 16861+ } else {
0c5527e5
AM
16862+ AuDebugOn(!(mark->mask & AuHfsnMask));
16863+ mark->mask &= ~AuHfsnMask;
1facf9fc 16864+ }
0c5527e5 16865+ spin_unlock(&mark->lock);
4a4d8108 16866+ /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
1facf9fc 16867+}
16868+
4a4d8108 16869+/* ---------------------------------------------------------------------- */
1facf9fc 16870+
4a4d8108
AM
16871+/* #define AuDbgHnotify */
16872+#ifdef AuDbgHnotify
16873+static char *au_hfsn_name(u32 mask)
16874+{
16875+#ifdef CONFIG_AUFS_DEBUG
c06a8ce3
AM
16876+#define test_ret(flag) \
16877+ do { \
16878+ if (mask & flag) \
16879+ return #flag; \
16880+ } while (0)
4a4d8108
AM
16881+ test_ret(FS_ACCESS);
16882+ test_ret(FS_MODIFY);
16883+ test_ret(FS_ATTRIB);
16884+ test_ret(FS_CLOSE_WRITE);
16885+ test_ret(FS_CLOSE_NOWRITE);
16886+ test_ret(FS_OPEN);
16887+ test_ret(FS_MOVED_FROM);
16888+ test_ret(FS_MOVED_TO);
16889+ test_ret(FS_CREATE);
16890+ test_ret(FS_DELETE);
16891+ test_ret(FS_DELETE_SELF);
16892+ test_ret(FS_MOVE_SELF);
16893+ test_ret(FS_UNMOUNT);
16894+ test_ret(FS_Q_OVERFLOW);
16895+ test_ret(FS_IN_IGNORED);
b912730e 16896+ test_ret(FS_ISDIR);
4a4d8108
AM
16897+ test_ret(FS_IN_ONESHOT);
16898+ test_ret(FS_EVENT_ON_CHILD);
16899+ return "";
16900+#undef test_ret
16901+#else
16902+ return "??";
16903+#endif
1facf9fc 16904+}
4a4d8108 16905+#endif
1facf9fc 16906+
16907+/* ---------------------------------------------------------------------- */
16908+
1716fcea
AM
16909+static void au_hfsn_free_group(struct fsnotify_group *group)
16910+{
16911+ struct au_br_hfsnotify *hfsn = group->private;
16912+
5afbbe0d 16913+ /* AuDbg("here\n"); */
1c60b727 16914+ kfree(hfsn);
1716fcea
AM
16915+}
16916+
4a4d8108 16917+static int au_hfsn_handle_event(struct fsnotify_group *group,
fb47a38f 16918+ struct inode *inode,
0c5527e5
AM
16919+ struct fsnotify_mark *inode_mark,
16920+ struct fsnotify_mark *vfsmount_mark,
a2654f78 16921+ u32 mask, const void *data, int data_type,
ffa93bbd
AM
16922+ const unsigned char *file_name, u32 cookie,
16923+ struct fsnotify_iter_info *iter_info)
1facf9fc 16924+{
16925+ int err;
4a4d8108
AM
16926+ struct au_hnotify *hnotify;
16927+ struct inode *h_dir, *h_inode;
fb47a38f 16928+ struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
4a4d8108 16929+
fb47a38f 16930+ AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
1facf9fc 16931+
16932+ err = 0;
0c5527e5 16933+ /* if FS_UNMOUNT happens, there must be another bug */
4a4d8108 16934+ AuDebugOn(mask & FS_UNMOUNT);
0c5527e5 16935+ if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
1facf9fc 16936+ goto out;
1facf9fc 16937+
fb47a38f
JR
16938+ h_dir = inode;
16939+ h_inode = NULL;
4a4d8108 16940+#ifdef AuDbgHnotify
392086de 16941+ au_debug_on();
4a4d8108
AM
16942+ if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
16943+ || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
16944+ AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
16945+ h_dir->i_ino, mask, au_hfsn_name(mask),
16946+ AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
16947+ /* WARN_ON(1); */
1facf9fc 16948+ }
392086de 16949+ au_debug_off();
1facf9fc 16950+#endif
4a4d8108 16951+
0c5527e5
AM
16952+ AuDebugOn(!inode_mark);
16953+ hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
16954+ err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
1facf9fc 16955+
4a4d8108
AM
16956+out:
16957+ return err;
16958+}
1facf9fc 16959+
4a4d8108 16960+static struct fsnotify_ops au_hfsn_ops = {
1716fcea 16961+ .handle_event = au_hfsn_handle_event,
ffa93bbd
AM
16962+ .free_group_priv = au_hfsn_free_group,
16963+ .free_mark = au_hfsn_free_mark
4a4d8108
AM
16964+};
16965+
16966+/* ---------------------------------------------------------------------- */
16967+
027c5e7a
AM
16968+static void au_hfsn_fin_br(struct au_branch *br)
16969+{
1716fcea 16970+ struct au_br_hfsnotify *hfsn;
027c5e7a 16971+
1716fcea 16972+ hfsn = br->br_hfsn;
c1595e42
JR
16973+ if (hfsn) {
16974+ lockdep_off();
1716fcea 16975+ fsnotify_put_group(hfsn->hfsn_group);
c1595e42
JR
16976+ lockdep_on();
16977+ }
027c5e7a
AM
16978+}
16979+
1716fcea 16980+static int au_hfsn_init_br(struct au_branch *br, int perm)
4a4d8108
AM
16981+{
16982+ int err;
1716fcea
AM
16983+ struct fsnotify_group *group;
16984+ struct au_br_hfsnotify *hfsn;
1facf9fc 16985+
4a4d8108 16986+ err = 0;
1716fcea
AM
16987+ br->br_hfsn = NULL;
16988+ if (!au_br_hnotifyable(perm))
027c5e7a 16989+ goto out;
027c5e7a 16990+
1716fcea
AM
16991+ err = -ENOMEM;
16992+ hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
16993+ if (unlikely(!hfsn))
027c5e7a
AM
16994+ goto out;
16995+
1716fcea
AM
16996+ err = 0;
16997+ group = fsnotify_alloc_group(&au_hfsn_ops);
16998+ if (IS_ERR(group)) {
16999+ err = PTR_ERR(group);
0c5527e5 17000+ pr_err("fsnotify_alloc_group() failed, %d\n", err);
1716fcea 17001+ goto out_hfsn;
4a4d8108 17002+ }
1facf9fc 17003+
1716fcea
AM
17004+ group->private = hfsn;
17005+ hfsn->hfsn_group = group;
17006+ br->br_hfsn = hfsn;
17007+ goto out; /* success */
17008+
17009+out_hfsn:
1c60b727 17010+ kfree(hfsn);
027c5e7a 17011+out:
1716fcea
AM
17012+ return err;
17013+}
17014+
17015+static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
17016+{
17017+ int err;
17018+
17019+ err = 0;
17020+ if (!br->br_hfsn)
17021+ err = au_hfsn_init_br(br, perm);
17022+
1facf9fc 17023+ return err;
17024+}
17025+
7eafdf33
AM
17026+/* ---------------------------------------------------------------------- */
17027+
17028+static void au_hfsn_fin(void)
17029+{
17030+ AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
17031+ wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
17032+}
17033+
4a4d8108
AM
17034+const struct au_hnotify_op au_hnotify_op = {
17035+ .ctl = au_hfsn_ctl,
17036+ .alloc = au_hfsn_alloc,
17037+ .free = au_hfsn_free,
1facf9fc 17038+
7eafdf33
AM
17039+ .fin = au_hfsn_fin,
17040+
027c5e7a
AM
17041+ .reset_br = au_hfsn_reset_br,
17042+ .fin_br = au_hfsn_fin_br,
17043+ .init_br = au_hfsn_init_br
4a4d8108 17044+};
7f207e10
AM
17045diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
17046--- /usr/share/empty/fs/aufs/hfsplus.c 1970-01-01 01:00:00.000000000 +0100
1c60b727 17047+++ linux/fs/aufs/hfsplus.c 2017-07-29 12:14:25.903042072 +0200
523b37e3 17048@@ -0,0 +1,56 @@
4a4d8108 17049+/*
a2654f78 17050+ * Copyright (C) 2010-2017 Junjiro R. Okajima
4a4d8108
AM
17051+ *
17052+ * This program, aufs is free software; you can redistribute it and/or modify
17053+ * it under the terms of the GNU General Public License as published by
17054+ * the Free Software Foundation; either version 2 of the License, or
17055+ * (at your option) any later version.
17056+ *
17057+ * This program is distributed in the hope that it will be useful,
17058+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17059+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17060+ * GNU General Public License for more details.
17061+ *
17062+ * You should have received a copy of the GNU General Public License
523b37e3 17063+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 17064+ */
1facf9fc 17065+
4a4d8108
AM
17066+/*
17067+ * special support for filesystems which aqucires an inode mutex
17068+ * at final closing a file, eg, hfsplus.
17069+ *
17070+ * This trick is very simple and stupid, just to open the file before really
17071+ * neceeary open to tell hfsplus that this is not the final closing.
17072+ * The caller should call au_h_open_pre() after acquiring the inode mutex,
17073+ * and au_h_open_post() after releasing it.
17074+ */
1facf9fc 17075+
4a4d8108 17076+#include "aufs.h"
1facf9fc 17077+
392086de
AM
17078+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
17079+ int force_wr)
4a4d8108
AM
17080+{
17081+ struct file *h_file;
17082+ struct dentry *h_dentry;
1facf9fc 17083+
4a4d8108
AM
17084+ h_dentry = au_h_dptr(dentry, bindex);
17085+ AuDebugOn(!h_dentry);
5527c038 17086+ AuDebugOn(d_is_negative(h_dentry));
4a4d8108
AM
17087+
17088+ h_file = NULL;
17089+ if (au_test_hfsplus(h_dentry->d_sb)
7e9cd9fe 17090+ && d_is_reg(h_dentry))
4a4d8108
AM
17091+ h_file = au_h_open(dentry, bindex,
17092+ O_RDONLY | O_NOATIME | O_LARGEFILE,
392086de 17093+ /*file*/NULL, force_wr);
4a4d8108 17094+ return h_file;
1facf9fc 17095+}
17096+
4a4d8108
AM
17097+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
17098+ struct file *h_file)
17099+{
17100+ if (h_file) {
17101+ fput(h_file);
17102+ au_sbr_put(dentry->d_sb, bindex);
17103+ }
17104+}
7f207e10
AM
17105diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
17106--- /usr/share/empty/fs/aufs/hnotify.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
17107+++ linux/fs/aufs/hnotify.c 2017-11-12 22:24:44.707577830 +0100
17108@@ -0,0 +1,719 @@
e49829fe 17109+/*
a2654f78 17110+ * Copyright (C) 2005-2017 Junjiro R. Okajima
e49829fe
JR
17111+ *
17112+ * This program, aufs is free software; you can redistribute it and/or modify
17113+ * it under the terms of the GNU General Public License as published by
17114+ * the Free Software Foundation; either version 2 of the License, or
17115+ * (at your option) any later version.
17116+ *
17117+ * This program is distributed in the hope that it will be useful,
17118+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17119+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17120+ * GNU General Public License for more details.
17121+ *
17122+ * You should have received a copy of the GNU General Public License
523b37e3 17123+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
17124+ */
17125+
17126+/*
7f207e10 17127+ * abstraction to notify the direct changes on lower directories
e49829fe
JR
17128+ */
17129+
17130+#include "aufs.h"
17131+
027c5e7a 17132+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
e49829fe
JR
17133+{
17134+ int err;
7f207e10 17135+ struct au_hnotify *hn;
1facf9fc 17136+
4a4d8108
AM
17137+ err = -ENOMEM;
17138+ hn = au_cache_alloc_hnotify();
17139+ if (hn) {
17140+ hn->hn_aufs_inode = inode;
027c5e7a
AM
17141+ hinode->hi_notify = hn;
17142+ err = au_hnotify_op.alloc(hinode);
17143+ AuTraceErr(err);
17144+ if (unlikely(err)) {
17145+ hinode->hi_notify = NULL;
1c60b727 17146+ au_cache_free_hnotify(hn);
4a4d8108
AM
17147+ /*
17148+ * The upper dir was removed by udba, but the same named
17149+ * dir left. In this case, aufs assignes a new inode
17150+ * number and set the monitor again.
17151+ * For the lower dir, the old monitnor is still left.
17152+ */
17153+ if (err == -EEXIST)
17154+ err = 0;
17155+ }
1308ab2a 17156+ }
1308ab2a 17157+
027c5e7a 17158+ AuTraceErr(err);
1308ab2a 17159+ return err;
dece6358 17160+}
1facf9fc 17161+
4a4d8108 17162+void au_hn_free(struct au_hinode *hinode)
dece6358 17163+{
4a4d8108 17164+ struct au_hnotify *hn;
1facf9fc 17165+
4a4d8108
AM
17166+ hn = hinode->hi_notify;
17167+ if (hn) {
4a4d8108 17168+ hinode->hi_notify = NULL;
7eafdf33 17169+ if (au_hnotify_op.free(hinode, hn))
1c60b727 17170+ au_cache_free_hnotify(hn);
4a4d8108
AM
17171+ }
17172+}
dece6358 17173+
4a4d8108 17174+/* ---------------------------------------------------------------------- */
dece6358 17175+
4a4d8108
AM
17176+void au_hn_ctl(struct au_hinode *hinode, int do_set)
17177+{
17178+ if (hinode->hi_notify)
17179+ au_hnotify_op.ctl(hinode, do_set);
17180+}
17181+
17182+void au_hn_reset(struct inode *inode, unsigned int flags)
17183+{
5afbbe0d 17184+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
17185+ struct inode *hi;
17186+ struct dentry *iwhdentry;
1facf9fc 17187+
5afbbe0d
AM
17188+ bbot = au_ibbot(inode);
17189+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108
AM
17190+ hi = au_h_iptr(inode, bindex);
17191+ if (!hi)
17192+ continue;
1308ab2a 17193+
febd17d6 17194+ /* inode_lock_nested(hi, AuLsc_I_CHILD); */
4a4d8108
AM
17195+ iwhdentry = au_hi_wh(inode, bindex);
17196+ if (iwhdentry)
17197+ dget(iwhdentry);
17198+ au_igrab(hi);
17199+ au_set_h_iptr(inode, bindex, NULL, 0);
17200+ au_set_h_iptr(inode, bindex, au_igrab(hi),
17201+ flags & ~AuHi_XINO);
17202+ iput(hi);
17203+ dput(iwhdentry);
febd17d6 17204+ /* inode_unlock(hi); */
1facf9fc 17205+ }
1facf9fc 17206+}
17207+
1308ab2a 17208+/* ---------------------------------------------------------------------- */
1facf9fc 17209+
4a4d8108 17210+static int hn_xino(struct inode *inode, struct inode *h_inode)
1facf9fc 17211+{
4a4d8108 17212+ int err;
5afbbe0d 17213+ aufs_bindex_t bindex, bbot, bfound, btop;
4a4d8108 17214+ struct inode *h_i;
1facf9fc 17215+
4a4d8108
AM
17216+ err = 0;
17217+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17218+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17219+ goto out;
17220+ }
1facf9fc 17221+
4a4d8108 17222+ bfound = -1;
5afbbe0d
AM
17223+ bbot = au_ibbot(inode);
17224+ btop = au_ibtop(inode);
4a4d8108 17225+#if 0 /* reserved for future use */
5afbbe0d 17226+ if (bindex == bbot) {
4a4d8108
AM
17227+ /* keep this ino in rename case */
17228+ goto out;
17229+ }
17230+#endif
5afbbe0d 17231+ for (bindex = btop; bindex <= bbot; bindex++)
4a4d8108
AM
17232+ if (au_h_iptr(inode, bindex) == h_inode) {
17233+ bfound = bindex;
17234+ break;
17235+ }
17236+ if (bfound < 0)
1308ab2a 17237+ goto out;
1facf9fc 17238+
5afbbe0d 17239+ for (bindex = btop; bindex <= bbot; bindex++) {
4a4d8108
AM
17240+ h_i = au_h_iptr(inode, bindex);
17241+ if (!h_i)
17242+ continue;
1facf9fc 17243+
4a4d8108
AM
17244+ err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
17245+ /* ignore this error */
17246+ /* bad action? */
1facf9fc 17247+ }
1facf9fc 17248+
4a4d8108 17249+ /* children inode number will be broken */
1facf9fc 17250+
4f0767ce 17251+out:
4a4d8108
AM
17252+ AuTraceErr(err);
17253+ return err;
1facf9fc 17254+}
17255+
4a4d8108 17256+static int hn_gen_tree(struct dentry *dentry)
1facf9fc 17257+{
4a4d8108
AM
17258+ int err, i, j, ndentry;
17259+ struct au_dcsub_pages dpages;
17260+ struct au_dpage *dpage;
17261+ struct dentry **dentries;
1facf9fc 17262+
4a4d8108
AM
17263+ err = au_dpages_init(&dpages, GFP_NOFS);
17264+ if (unlikely(err))
17265+ goto out;
17266+ err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
17267+ if (unlikely(err))
17268+ goto out_dpages;
1facf9fc 17269+
4a4d8108
AM
17270+ for (i = 0; i < dpages.ndpage; i++) {
17271+ dpage = dpages.dpages + i;
17272+ dentries = dpage->dentries;
17273+ ndentry = dpage->ndentry;
17274+ for (j = 0; j < ndentry; j++) {
17275+ struct dentry *d;
17276+
17277+ d = dentries[j];
17278+ if (IS_ROOT(d))
17279+ continue;
17280+
4a4d8108 17281+ au_digen_dec(d);
5527c038 17282+ if (d_really_is_positive(d))
4a4d8108
AM
17283+ /* todo: reset children xino?
17284+ cached children only? */
5527c038 17285+ au_iigen_dec(d_inode(d));
1308ab2a 17286+ }
dece6358 17287+ }
1facf9fc 17288+
4f0767ce 17289+out_dpages:
4a4d8108 17290+ au_dpages_free(&dpages);
dece6358 17291+
027c5e7a 17292+#if 0
4a4d8108
AM
17293+ /* discard children */
17294+ dentry_unhash(dentry);
17295+ dput(dentry);
027c5e7a 17296+#endif
4f0767ce 17297+out:
dece6358
AM
17298+ return err;
17299+}
17300+
1308ab2a 17301+/*
4a4d8108 17302+ * return 0 if processed.
1308ab2a 17303+ */
4a4d8108
AM
17304+static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
17305+ const unsigned int isdir)
dece6358 17306+{
1308ab2a 17307+ int err;
4a4d8108
AM
17308+ struct dentry *d;
17309+ struct qstr *dname;
1facf9fc 17310+
4a4d8108
AM
17311+ err = 1;
17312+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17313+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17314+ err = 0;
17315+ goto out;
17316+ }
dece6358 17317+
4a4d8108
AM
17318+ if (!isdir) {
17319+ AuDebugOn(!name);
17320+ au_iigen_dec(inode);
027c5e7a 17321+ spin_lock(&inode->i_lock);
c1595e42 17322+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 17323+ spin_lock(&d->d_lock);
4a4d8108
AM
17324+ dname = &d->d_name;
17325+ if (dname->len != nlen
027c5e7a
AM
17326+ && memcmp(dname->name, name, nlen)) {
17327+ spin_unlock(&d->d_lock);
4a4d8108 17328+ continue;
027c5e7a 17329+ }
4a4d8108 17330+ err = 0;
4a4d8108
AM
17331+ au_digen_dec(d);
17332+ spin_unlock(&d->d_lock);
17333+ break;
1facf9fc 17334+ }
027c5e7a 17335+ spin_unlock(&inode->i_lock);
1308ab2a 17336+ } else {
027c5e7a 17337+ au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
c1595e42 17338+ d = d_find_any_alias(inode);
4a4d8108
AM
17339+ if (!d) {
17340+ au_iigen_dec(inode);
17341+ goto out;
17342+ }
1facf9fc 17343+
027c5e7a 17344+ spin_lock(&d->d_lock);
4a4d8108 17345+ dname = &d->d_name;
027c5e7a
AM
17346+ if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
17347+ spin_unlock(&d->d_lock);
4a4d8108 17348+ err = hn_gen_tree(d);
027c5e7a
AM
17349+ spin_lock(&d->d_lock);
17350+ }
17351+ spin_unlock(&d->d_lock);
4a4d8108
AM
17352+ dput(d);
17353+ }
1facf9fc 17354+
4f0767ce 17355+out:
4a4d8108 17356+ AuTraceErr(err);
1308ab2a 17357+ return err;
17358+}
dece6358 17359+
4a4d8108 17360+static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
1facf9fc 17361+{
4a4d8108 17362+ int err;
1facf9fc 17363+
5527c038 17364+ if (IS_ROOT(dentry)) {
0c3ec466 17365+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17366+ return 0;
17367+ }
1308ab2a 17368+
4a4d8108
AM
17369+ err = 0;
17370+ if (!isdir) {
4a4d8108 17371+ au_digen_dec(dentry);
5527c038
JR
17372+ if (d_really_is_positive(dentry))
17373+ au_iigen_dec(d_inode(dentry));
4a4d8108 17374+ } else {
027c5e7a 17375+ au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
5527c038 17376+ if (d_really_is_positive(dentry))
4a4d8108
AM
17377+ err = hn_gen_tree(dentry);
17378+ }
17379+
17380+ AuTraceErr(err);
17381+ return err;
1facf9fc 17382+}
17383+
4a4d8108 17384+/* ---------------------------------------------------------------------- */
1facf9fc 17385+
4a4d8108
AM
17386+/* hnotify job flags */
17387+#define AuHnJob_XINO0 1
17388+#define AuHnJob_GEN (1 << 1)
17389+#define AuHnJob_DIRENT (1 << 2)
17390+#define AuHnJob_ISDIR (1 << 3)
17391+#define AuHnJob_TRYXINO0 (1 << 4)
17392+#define AuHnJob_MNTPNT (1 << 5)
17393+#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
7f207e10
AM
17394+#define au_fset_hnjob(flags, name) \
17395+ do { (flags) |= AuHnJob_##name; } while (0)
17396+#define au_fclr_hnjob(flags, name) \
17397+ do { (flags) &= ~AuHnJob_##name; } while (0)
1facf9fc 17398+
4a4d8108
AM
17399+enum {
17400+ AuHn_CHILD,
17401+ AuHn_PARENT,
17402+ AuHnLast
17403+};
1facf9fc 17404+
4a4d8108
AM
17405+struct au_hnotify_args {
17406+ struct inode *h_dir, *dir, *h_child_inode;
17407+ u32 mask;
17408+ unsigned int flags[AuHnLast];
17409+ unsigned int h_child_nlen;
17410+ char h_child_name[];
17411+};
1facf9fc 17412+
4a4d8108
AM
17413+struct hn_job_args {
17414+ unsigned int flags;
17415+ struct inode *inode, *h_inode, *dir, *h_dir;
17416+ struct dentry *dentry;
17417+ char *h_name;
17418+ int h_nlen;
17419+};
1308ab2a 17420+
4a4d8108
AM
17421+static int hn_job(struct hn_job_args *a)
17422+{
17423+ const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
076b876e 17424+ int e;
1308ab2a 17425+
4a4d8108
AM
17426+ /* reset xino */
17427+ if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
17428+ hn_xino(a->inode, a->h_inode); /* ignore this error */
1308ab2a 17429+
4a4d8108
AM
17430+ if (au_ftest_hnjob(a->flags, TRYXINO0)
17431+ && a->inode
17432+ && a->h_inode) {
3c1bdaff 17433+ vfsub_inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
38d290e6
JR
17434+ if (!a->h_inode->i_nlink
17435+ && !(a->h_inode->i_state & I_LINKABLE))
4a4d8108 17436+ hn_xino(a->inode, a->h_inode); /* ignore this error */
3c1bdaff 17437+ inode_unlock_shared(a->h_inode);
1308ab2a 17438+ }
1facf9fc 17439+
4a4d8108
AM
17440+ /* make the generation obsolete */
17441+ if (au_ftest_hnjob(a->flags, GEN)) {
076b876e 17442+ e = -1;
4a4d8108 17443+ if (a->inode)
076b876e 17444+ e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
4a4d8108 17445+ isdir);
076b876e 17446+ if (e && a->dentry)
4a4d8108
AM
17447+ hn_gen_by_name(a->dentry, isdir);
17448+ /* ignore this error */
1facf9fc 17449+ }
1facf9fc 17450+
4a4d8108
AM
17451+ /* make dir entries obsolete */
17452+ if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
17453+ struct au_vdir *vdir;
1facf9fc 17454+
4a4d8108
AM
17455+ vdir = au_ivdir(a->inode);
17456+ if (vdir)
17457+ vdir->vd_jiffy = 0;
17458+ /* IMustLock(a->inode); */
17459+ /* a->inode->i_version++; */
17460+ }
1facf9fc 17461+
4a4d8108
AM
17462+ /* can do nothing but warn */
17463+ if (au_ftest_hnjob(a->flags, MNTPNT)
17464+ && a->dentry
17465+ && d_mountpoint(a->dentry))
523b37e3 17466+ pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
1facf9fc 17467+
4a4d8108 17468+ return 0;
1308ab2a 17469+}
1facf9fc 17470+
1308ab2a 17471+/* ---------------------------------------------------------------------- */
1facf9fc 17472+
4a4d8108
AM
17473+static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
17474+ struct inode *dir)
1308ab2a 17475+{
4a4d8108
AM
17476+ struct dentry *dentry, *d, *parent;
17477+ struct qstr *dname;
1308ab2a 17478+
c1595e42 17479+ parent = d_find_any_alias(dir);
4a4d8108
AM
17480+ if (!parent)
17481+ return NULL;
1308ab2a 17482+
4a4d8108 17483+ dentry = NULL;
027c5e7a 17484+ spin_lock(&parent->d_lock);
c1595e42 17485+ list_for_each_entry(d, &parent->d_subdirs, d_child) {
523b37e3 17486+ /* AuDbg("%pd\n", d); */
027c5e7a 17487+ spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
4a4d8108
AM
17488+ dname = &d->d_name;
17489+ if (dname->len != nlen || memcmp(dname->name, name, nlen))
027c5e7a
AM
17490+ goto cont_unlock;
17491+ if (au_di(d))
17492+ au_digen_dec(d);
17493+ else
17494+ goto cont_unlock;
c1595e42 17495+ if (au_dcount(d) > 0) {
027c5e7a 17496+ dentry = dget_dlock(d);
4a4d8108 17497+ spin_unlock(&d->d_lock);
027c5e7a 17498+ break;
dece6358 17499+ }
1facf9fc 17500+
f6b6e03d 17501+cont_unlock:
027c5e7a 17502+ spin_unlock(&d->d_lock);
1308ab2a 17503+ }
027c5e7a 17504+ spin_unlock(&parent->d_lock);
4a4d8108 17505+ dput(parent);
1facf9fc 17506+
4a4d8108
AM
17507+ if (dentry)
17508+ di_write_lock_child(dentry);
1308ab2a 17509+
4a4d8108
AM
17510+ return dentry;
17511+}
dece6358 17512+
4a4d8108
AM
17513+static struct inode *lookup_wlock_by_ino(struct super_block *sb,
17514+ aufs_bindex_t bindex, ino_t h_ino)
17515+{
17516+ struct inode *inode;
17517+ ino_t ino;
17518+ int err;
17519+
17520+ inode = NULL;
17521+ err = au_xino_read(sb, bindex, h_ino, &ino);
17522+ if (!err && ino)
17523+ inode = ilookup(sb, ino);
17524+ if (!inode)
17525+ goto out;
17526+
17527+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17528+ pr_warn("wrong root branch\n");
4a4d8108
AM
17529+ iput(inode);
17530+ inode = NULL;
17531+ goto out;
1308ab2a 17532+ }
17533+
4a4d8108 17534+ ii_write_lock_child(inode);
1308ab2a 17535+
4f0767ce 17536+out:
4a4d8108 17537+ return inode;
dece6358
AM
17538+}
17539+
4a4d8108 17540+static void au_hn_bh(void *_args)
1facf9fc 17541+{
4a4d8108
AM
17542+ struct au_hnotify_args *a = _args;
17543+ struct super_block *sb;
5afbbe0d 17544+ aufs_bindex_t bindex, bbot, bfound;
4a4d8108 17545+ unsigned char xino, try_iput;
1facf9fc 17546+ int err;
1308ab2a 17547+ struct inode *inode;
4a4d8108
AM
17548+ ino_t h_ino;
17549+ struct hn_job_args args;
17550+ struct dentry *dentry;
17551+ struct au_sbinfo *sbinfo;
1facf9fc 17552+
4a4d8108
AM
17553+ AuDebugOn(!_args);
17554+ AuDebugOn(!a->h_dir);
17555+ AuDebugOn(!a->dir);
17556+ AuDebugOn(!a->mask);
17557+ AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
17558+ a->mask, a->dir->i_ino, a->h_dir->i_ino,
17559+ a->h_child_inode ? a->h_child_inode->i_ino : 0);
1facf9fc 17560+
4a4d8108
AM
17561+ inode = NULL;
17562+ dentry = NULL;
17563+ /*
17564+ * do not lock a->dir->i_mutex here
17565+ * because of d_revalidate() may cause a deadlock.
17566+ */
17567+ sb = a->dir->i_sb;
17568+ AuDebugOn(!sb);
17569+ sbinfo = au_sbi(sb);
17570+ AuDebugOn(!sbinfo);
7f207e10 17571+ si_write_lock(sb, AuLock_NOPLMW);
1facf9fc 17572+
8b6a4947
AM
17573+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
17574+ switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
17575+ case FS_MOVED_FROM:
17576+ case FS_MOVED_TO:
17577+ AuWarn1("DIRREN with UDBA may not work correctly "
17578+ "for the direct rename(2)\n");
17579+ }
17580+
4a4d8108
AM
17581+ ii_read_lock_parent(a->dir);
17582+ bfound = -1;
5afbbe0d
AM
17583+ bbot = au_ibbot(a->dir);
17584+ for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
4a4d8108
AM
17585+ if (au_h_iptr(a->dir, bindex) == a->h_dir) {
17586+ bfound = bindex;
17587+ break;
17588+ }
17589+ ii_read_unlock(a->dir);
17590+ if (unlikely(bfound < 0))
17591+ goto out;
1facf9fc 17592+
4a4d8108
AM
17593+ xino = !!au_opt_test(au_mntflags(sb), XINO);
17594+ h_ino = 0;
17595+ if (a->h_child_inode)
17596+ h_ino = a->h_child_inode->i_ino;
1facf9fc 17597+
4a4d8108
AM
17598+ if (a->h_child_nlen
17599+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
17600+ || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
17601+ dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
17602+ a->dir);
17603+ try_iput = 0;
5527c038
JR
17604+ if (dentry && d_really_is_positive(dentry))
17605+ inode = d_inode(dentry);
4a4d8108
AM
17606+ if (xino && !inode && h_ino
17607+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
17608+ || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
17609+ || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
17610+ inode = lookup_wlock_by_ino(sb, bfound, h_ino);
17611+ try_iput = 1;
f0c0a007 17612+ }
1facf9fc 17613+
4a4d8108
AM
17614+ args.flags = a->flags[AuHn_CHILD];
17615+ args.dentry = dentry;
17616+ args.inode = inode;
17617+ args.h_inode = a->h_child_inode;
17618+ args.dir = a->dir;
17619+ args.h_dir = a->h_dir;
17620+ args.h_name = a->h_child_name;
17621+ args.h_nlen = a->h_child_nlen;
17622+ err = hn_job(&args);
17623+ if (dentry) {
027c5e7a 17624+ if (au_di(dentry))
4a4d8108
AM
17625+ di_write_unlock(dentry);
17626+ dput(dentry);
17627+ }
17628+ if (inode && try_iput) {
17629+ ii_write_unlock(inode);
17630+ iput(inode);
17631+ }
1facf9fc 17632+
4a4d8108
AM
17633+ ii_write_lock_parent(a->dir);
17634+ args.flags = a->flags[AuHn_PARENT];
17635+ args.dentry = NULL;
17636+ args.inode = a->dir;
17637+ args.h_inode = a->h_dir;
17638+ args.dir = NULL;
17639+ args.h_dir = NULL;
17640+ args.h_name = NULL;
17641+ args.h_nlen = 0;
17642+ err = hn_job(&args);
17643+ ii_write_unlock(a->dir);
1facf9fc 17644+
4f0767ce 17645+out:
4a4d8108
AM
17646+ iput(a->h_child_inode);
17647+ iput(a->h_dir);
17648+ iput(a->dir);
027c5e7a
AM
17649+ si_write_unlock(sb);
17650+ au_nwt_done(&sbinfo->si_nowait);
1c60b727 17651+ kfree(a);
dece6358 17652+}
1facf9fc 17653+
4a4d8108
AM
17654+/* ---------------------------------------------------------------------- */
17655+
17656+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17657+ struct qstr *h_child_qstr, struct inode *h_child_inode)
dece6358 17658+{
4a4d8108 17659+ int err, len;
53392da6 17660+ unsigned int flags[AuHnLast], f;
4a4d8108
AM
17661+ unsigned char isdir, isroot, wh;
17662+ struct inode *dir;
17663+ struct au_hnotify_args *args;
17664+ char *p, *h_child_name;
dece6358 17665+
1308ab2a 17666+ err = 0;
4a4d8108
AM
17667+ AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
17668+ dir = igrab(hnotify->hn_aufs_inode);
17669+ if (!dir)
17670+ goto out;
1facf9fc 17671+
4a4d8108
AM
17672+ isroot = (dir->i_ino == AUFS_ROOT_INO);
17673+ wh = 0;
17674+ h_child_name = (void *)h_child_qstr->name;
17675+ len = h_child_qstr->len;
17676+ if (h_child_name) {
17677+ if (len > AUFS_WH_PFX_LEN
17678+ && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
17679+ h_child_name += AUFS_WH_PFX_LEN;
17680+ len -= AUFS_WH_PFX_LEN;
17681+ wh = 1;
17682+ }
1facf9fc 17683+ }
dece6358 17684+
4a4d8108
AM
17685+ isdir = 0;
17686+ if (h_child_inode)
17687+ isdir = !!S_ISDIR(h_child_inode->i_mode);
17688+ flags[AuHn_PARENT] = AuHnJob_ISDIR;
17689+ flags[AuHn_CHILD] = 0;
17690+ if (isdir)
17691+ flags[AuHn_CHILD] = AuHnJob_ISDIR;
17692+ au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
17693+ au_fset_hnjob(flags[AuHn_CHILD], GEN);
17694+ switch (mask & FS_EVENTS_POSS_ON_CHILD) {
17695+ case FS_MOVED_FROM:
17696+ case FS_MOVED_TO:
17697+ au_fset_hnjob(flags[AuHn_CHILD], XINO0);
17698+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17699+ /*FALLTHROUGH*/
17700+ case FS_CREATE:
fb47a38f 17701+ AuDebugOn(!h_child_name);
4a4d8108 17702+ break;
1facf9fc 17703+
4a4d8108
AM
17704+ case FS_DELETE:
17705+ /*
17706+ * aufs never be able to get this child inode.
17707+ * revalidation should be in d_revalidate()
17708+ * by checking i_nlink, i_generation or d_unhashed().
17709+ */
17710+ AuDebugOn(!h_child_name);
17711+ au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
17712+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17713+ break;
dece6358 17714+
4a4d8108
AM
17715+ default:
17716+ AuDebugOn(1);
17717+ }
1308ab2a 17718+
4a4d8108
AM
17719+ if (wh)
17720+ h_child_inode = NULL;
1308ab2a 17721+
4a4d8108
AM
17722+ err = -ENOMEM;
17723+ /* iput() and kfree() will be called in au_hnotify() */
4a4d8108 17724+ args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
4a4d8108
AM
17725+ if (unlikely(!args)) {
17726+ AuErr1("no memory\n");
17727+ iput(dir);
17728+ goto out;
17729+ }
17730+ args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
17731+ args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
17732+ args->mask = mask;
17733+ args->dir = dir;
17734+ args->h_dir = igrab(h_dir);
17735+ if (h_child_inode)
17736+ h_child_inode = igrab(h_child_inode); /* can be NULL */
17737+ args->h_child_inode = h_child_inode;
17738+ args->h_child_nlen = len;
17739+ if (len) {
17740+ p = (void *)args;
17741+ p += sizeof(*args);
17742+ memcpy(p, h_child_name, len);
17743+ p[len] = 0;
1308ab2a 17744+ }
1308ab2a 17745+
38d290e6 17746+ /* NFS fires the event for silly-renamed one from kworker */
53392da6 17747+ f = 0;
38d290e6
JR
17748+ if (!dir->i_nlink
17749+ || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
53392da6
AM
17750+ f = AuWkq_NEST;
17751+ err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
4a4d8108
AM
17752+ if (unlikely(err)) {
17753+ pr_err("wkq %d\n", err);
17754+ iput(args->h_child_inode);
17755+ iput(args->h_dir);
17756+ iput(args->dir);
1c60b727 17757+ kfree(args);
1facf9fc 17758+ }
1facf9fc 17759+
4a4d8108 17760+out:
1facf9fc 17761+ return err;
17762+}
17763+
027c5e7a
AM
17764+/* ---------------------------------------------------------------------- */
17765+
17766+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
17767+{
17768+ int err;
17769+
17770+ AuDebugOn(!(udba & AuOptMask_UDBA));
17771+
17772+ err = 0;
17773+ if (au_hnotify_op.reset_br)
17774+ err = au_hnotify_op.reset_br(udba, br, perm);
17775+
17776+ return err;
17777+}
17778+
17779+int au_hnotify_init_br(struct au_branch *br, int perm)
17780+{
17781+ int err;
17782+
17783+ err = 0;
17784+ if (au_hnotify_op.init_br)
17785+ err = au_hnotify_op.init_br(br, perm);
17786+
17787+ return err;
17788+}
17789+
17790+void au_hnotify_fin_br(struct au_branch *br)
17791+{
17792+ if (au_hnotify_op.fin_br)
17793+ au_hnotify_op.fin_br(br);
17794+}
17795+
4a4d8108
AM
17796+static void au_hn_destroy_cache(void)
17797+{
1c60b727
AM
17798+ kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
17799+ au_cache[AuCache_HNOTIFY] = NULL;
4a4d8108 17800+}
1308ab2a 17801+
4a4d8108 17802+int __init au_hnotify_init(void)
1facf9fc 17803+{
1308ab2a 17804+ int err;
1308ab2a 17805+
4a4d8108 17806+ err = -ENOMEM;
1c60b727
AM
17807+ au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
17808+ if (au_cache[AuCache_HNOTIFY]) {
027c5e7a
AM
17809+ err = 0;
17810+ if (au_hnotify_op.init)
17811+ err = au_hnotify_op.init();
4a4d8108
AM
17812+ if (unlikely(err))
17813+ au_hn_destroy_cache();
1308ab2a 17814+ }
1308ab2a 17815+ AuTraceErr(err);
4a4d8108 17816+ return err;
1308ab2a 17817+}
17818+
4a4d8108 17819+void au_hnotify_fin(void)
1308ab2a 17820+{
027c5e7a
AM
17821+ if (au_hnotify_op.fin)
17822+ au_hnotify_op.fin();
f0c0a007 17823+
4a4d8108 17824+ /* cf. au_cache_fin() */
1c60b727 17825+ if (au_cache[AuCache_HNOTIFY])
4a4d8108 17826+ au_hn_destroy_cache();
dece6358 17827+}
7f207e10
AM
17828diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
17829--- /usr/share/empty/fs/aufs/iinfo.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947 17830+++ linux/fs/aufs/iinfo.c 2017-11-12 22:24:42.270843503 +0100
e2f27e51 17831@@ -0,0 +1,285 @@
dece6358 17832+/*
a2654f78 17833+ * Copyright (C) 2005-2017 Junjiro R. Okajima
dece6358
AM
17834+ *
17835+ * This program, aufs is free software; you can redistribute it and/or modify
17836+ * it under the terms of the GNU General Public License as published by
17837+ * the Free Software Foundation; either version 2 of the License, or
17838+ * (at your option) any later version.
17839+ *
17840+ * This program is distributed in the hope that it will be useful,
17841+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17842+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17843+ * GNU General Public License for more details.
17844+ *
17845+ * You should have received a copy of the GNU General Public License
523b37e3 17846+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 17847+ */
1facf9fc 17848+
dece6358 17849+/*
4a4d8108 17850+ * inode private data
dece6358 17851+ */
1facf9fc 17852+
1308ab2a 17853+#include "aufs.h"
1facf9fc 17854+
4a4d8108 17855+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 17856+{
4a4d8108 17857+ struct inode *h_inode;
5afbbe0d 17858+ struct au_hinode *hinode;
1facf9fc 17859+
4a4d8108 17860+ IiMustAnyLock(inode);
1facf9fc 17861+
5afbbe0d
AM
17862+ hinode = au_hinode(au_ii(inode), bindex);
17863+ h_inode = hinode->hi_inode;
4a4d8108
AM
17864+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17865+ return h_inode;
17866+}
1facf9fc 17867+
4a4d8108
AM
17868+/* todo: hard/soft set? */
17869+void au_hiput(struct au_hinode *hinode)
17870+{
17871+ au_hn_free(hinode);
17872+ dput(hinode->hi_whdentry);
17873+ iput(hinode->hi_inode);
17874+}
1facf9fc 17875+
4a4d8108
AM
17876+unsigned int au_hi_flags(struct inode *inode, int isdir)
17877+{
17878+ unsigned int flags;
17879+ const unsigned int mnt_flags = au_mntflags(inode->i_sb);
1facf9fc 17880+
4a4d8108
AM
17881+ flags = 0;
17882+ if (au_opt_test(mnt_flags, XINO))
17883+ au_fset_hi(flags, XINO);
17884+ if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
17885+ au_fset_hi(flags, HNOTIFY);
17886+ return flags;
1facf9fc 17887+}
17888+
4a4d8108
AM
17889+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
17890+ struct inode *h_inode, unsigned int flags)
1308ab2a 17891+{
4a4d8108
AM
17892+ struct au_hinode *hinode;
17893+ struct inode *hi;
17894+ struct au_iinfo *iinfo = au_ii(inode);
1facf9fc 17895+
4a4d8108 17896+ IiMustWriteLock(inode);
dece6358 17897+
5afbbe0d 17898+ hinode = au_hinode(iinfo, bindex);
4a4d8108
AM
17899+ hi = hinode->hi_inode;
17900+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17901+
17902+ if (hi)
17903+ au_hiput(hinode);
17904+ hinode->hi_inode = h_inode;
17905+ if (h_inode) {
17906+ int err;
17907+ struct super_block *sb = inode->i_sb;
17908+ struct au_branch *br;
17909+
027c5e7a
AM
17910+ AuDebugOn(inode->i_mode
17911+ && (h_inode->i_mode & S_IFMT)
17912+ != (inode->i_mode & S_IFMT));
5afbbe0d 17913+ if (bindex == iinfo->ii_btop)
4a4d8108
AM
17914+ au_cpup_igen(inode, h_inode);
17915+ br = au_sbr(sb, bindex);
17916+ hinode->hi_id = br->br_id;
17917+ if (au_ftest_hi(flags, XINO)) {
17918+ err = au_xino_write(sb, bindex, h_inode->i_ino,
17919+ inode->i_ino);
17920+ if (unlikely(err))
17921+ AuIOErr1("failed au_xino_write() %d\n", err);
17922+ }
17923+
17924+ if (au_ftest_hi(flags, HNOTIFY)
17925+ && au_br_hnotifyable(br->br_perm)) {
027c5e7a 17926+ err = au_hn_alloc(hinode, inode);
4a4d8108
AM
17927+ if (unlikely(err))
17928+ AuIOErr1("au_hn_alloc() %d\n", err);
1308ab2a 17929+ }
17930+ }
4a4d8108 17931+}
dece6358 17932+
4a4d8108
AM
17933+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
17934+ struct dentry *h_wh)
17935+{
17936+ struct au_hinode *hinode;
dece6358 17937+
4a4d8108
AM
17938+ IiMustWriteLock(inode);
17939+
5afbbe0d 17940+ hinode = au_hinode(au_ii(inode), bindex);
4a4d8108
AM
17941+ AuDebugOn(hinode->hi_whdentry);
17942+ hinode->hi_whdentry = h_wh;
1facf9fc 17943+}
17944+
537831f9 17945+void au_update_iigen(struct inode *inode, int half)
1308ab2a 17946+{
537831f9
AM
17947+ struct au_iinfo *iinfo;
17948+ struct au_iigen *iigen;
17949+ unsigned int sigen;
17950+
17951+ sigen = au_sigen(inode->i_sb);
17952+ iinfo = au_ii(inode);
17953+ iigen = &iinfo->ii_generation;
be52b249 17954+ spin_lock(&iigen->ig_spin);
537831f9
AM
17955+ iigen->ig_generation = sigen;
17956+ if (half)
17957+ au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
17958+ else
17959+ au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
be52b249 17960+ spin_unlock(&iigen->ig_spin);
4a4d8108 17961+}
1facf9fc 17962+
4a4d8108
AM
17963+/* it may be called at remount time, too */
17964+void au_update_ibrange(struct inode *inode, int do_put_zero)
17965+{
17966+ struct au_iinfo *iinfo;
5afbbe0d 17967+ aufs_bindex_t bindex, bbot;
1facf9fc 17968+
5afbbe0d 17969+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 17970+ IiMustWriteLock(inode);
1facf9fc 17971+
5afbbe0d
AM
17972+ iinfo = au_ii(inode);
17973+ if (do_put_zero && iinfo->ii_btop >= 0) {
17974+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
17975+ bindex++) {
17976+ struct inode *h_i;
1facf9fc 17977+
5afbbe0d 17978+ h_i = au_hinode(iinfo, bindex)->hi_inode;
38d290e6
JR
17979+ if (h_i
17980+ && !h_i->i_nlink
17981+ && !(h_i->i_state & I_LINKABLE))
027c5e7a
AM
17982+ au_set_h_iptr(inode, bindex, NULL, 0);
17983+ }
4a4d8108
AM
17984+ }
17985+
5afbbe0d
AM
17986+ iinfo->ii_btop = -1;
17987+ iinfo->ii_bbot = -1;
17988+ bbot = au_sbbot(inode->i_sb);
17989+ for (bindex = 0; bindex <= bbot; bindex++)
17990+ if (au_hinode(iinfo, bindex)->hi_inode) {
17991+ iinfo->ii_btop = bindex;
4a4d8108 17992+ break;
027c5e7a 17993+ }
5afbbe0d
AM
17994+ if (iinfo->ii_btop >= 0)
17995+ for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
17996+ if (au_hinode(iinfo, bindex)->hi_inode) {
17997+ iinfo->ii_bbot = bindex;
027c5e7a
AM
17998+ break;
17999+ }
5afbbe0d 18000+ AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
1308ab2a 18001+}
1facf9fc 18002+
dece6358 18003+/* ---------------------------------------------------------------------- */
1facf9fc 18004+
4a4d8108 18005+void au_icntnr_init_once(void *_c)
dece6358 18006+{
4a4d8108
AM
18007+ struct au_icntnr *c = _c;
18008+ struct au_iinfo *iinfo = &c->iinfo;
1facf9fc 18009+
be52b249 18010+ spin_lock_init(&iinfo->ii_generation.ig_spin);
4a4d8108
AM
18011+ au_rw_init(&iinfo->ii_rwsem);
18012+ inode_init_once(&c->vfs_inode);
18013+}
1facf9fc 18014+
5afbbe0d
AM
18015+void au_hinode_init(struct au_hinode *hinode)
18016+{
18017+ hinode->hi_inode = NULL;
18018+ hinode->hi_id = -1;
18019+ au_hn_init(hinode);
18020+ hinode->hi_whdentry = NULL;
18021+}
18022+
4a4d8108
AM
18023+int au_iinfo_init(struct inode *inode)
18024+{
18025+ struct au_iinfo *iinfo;
18026+ struct super_block *sb;
5afbbe0d 18027+ struct au_hinode *hi;
4a4d8108 18028+ int nbr, i;
1facf9fc 18029+
4a4d8108
AM
18030+ sb = inode->i_sb;
18031+ iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
5afbbe0d 18032+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
18033+ if (unlikely(nbr <= 0))
18034+ nbr = 1;
5afbbe0d
AM
18035+ hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
18036+ if (hi) {
7f207e10 18037+ au_ninodes_inc(sb);
5afbbe0d
AM
18038+
18039+ iinfo->ii_hinode = hi;
18040+ for (i = 0; i < nbr; i++, hi++)
18041+ au_hinode_init(hi);
1facf9fc 18042+
537831f9 18043+ iinfo->ii_generation.ig_generation = au_sigen(sb);
5afbbe0d
AM
18044+ iinfo->ii_btop = -1;
18045+ iinfo->ii_bbot = -1;
4a4d8108
AM
18046+ iinfo->ii_vdir = NULL;
18047+ return 0;
1308ab2a 18048+ }
4a4d8108
AM
18049+ return -ENOMEM;
18050+}
1facf9fc 18051+
e2f27e51 18052+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
4a4d8108 18053+{
5afbbe0d 18054+ int err, i;
4a4d8108 18055+ struct au_hinode *hip;
1facf9fc 18056+
4a4d8108
AM
18057+ AuRwMustWriteLock(&iinfo->ii_rwsem);
18058+
18059+ err = -ENOMEM;
e2f27e51
AM
18060+ hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
18061+ may_shrink);
4a4d8108
AM
18062+ if (hip) {
18063+ iinfo->ii_hinode = hip;
5afbbe0d
AM
18064+ i = iinfo->ii_bbot + 1;
18065+ hip += i;
18066+ for (; i < nbr; i++, hip++)
18067+ au_hinode_init(hip);
4a4d8108 18068+ err = 0;
1308ab2a 18069+ }
4a4d8108 18070+
1308ab2a 18071+ return err;
1facf9fc 18072+}
18073+
4a4d8108 18074+void au_iinfo_fin(struct inode *inode)
1facf9fc 18075+{
4a4d8108
AM
18076+ struct au_iinfo *iinfo;
18077+ struct au_hinode *hi;
18078+ struct super_block *sb;
5afbbe0d 18079+ aufs_bindex_t bindex, bbot;
b752ccd1 18080+ const unsigned char unlinked = !inode->i_nlink;
1308ab2a 18081+
5afbbe0d 18082+ AuDebugOn(au_is_bad_inode(inode));
1308ab2a 18083+
b752ccd1 18084+ sb = inode->i_sb;
7f207e10 18085+ au_ninodes_dec(sb);
b752ccd1
AM
18086+ if (si_pid_test(sb))
18087+ au_xino_delete_inode(inode, unlinked);
18088+ else {
18089+ /*
18090+ * it is safe to hide the dependency between sbinfo and
18091+ * sb->s_umount.
18092+ */
18093+ lockdep_off();
18094+ si_noflush_read_lock(sb);
18095+ au_xino_delete_inode(inode, unlinked);
18096+ si_read_unlock(sb);
18097+ lockdep_on();
18098+ }
18099+
5afbbe0d 18100+ iinfo = au_ii(inode);
4a4d8108 18101+ if (iinfo->ii_vdir)
1c60b727 18102+ au_vdir_free(iinfo->ii_vdir);
1308ab2a 18103+
5afbbe0d 18104+ bindex = iinfo->ii_btop;
b752ccd1 18105+ if (bindex >= 0) {
5afbbe0d
AM
18106+ hi = au_hinode(iinfo, bindex);
18107+ bbot = iinfo->ii_bbot;
18108+ while (bindex++ <= bbot) {
b752ccd1 18109+ if (hi->hi_inode)
4a4d8108 18110+ au_hiput(hi);
4a4d8108
AM
18111+ hi++;
18112+ }
18113+ }
1c60b727 18114+ kfree(iinfo->ii_hinode);
4a4d8108 18115+ AuRwDestroy(&iinfo->ii_rwsem);
dece6358 18116+}
7f207e10
AM
18117diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
18118--- /usr/share/empty/fs/aufs/inode.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947 18119+++ linux/fs/aufs/inode.c 2017-11-12 22:24:42.270843503 +0100
521ced18 18120@@ -0,0 +1,527 @@
4a4d8108 18121+/*
a2654f78 18122+ * Copyright (C) 2005-2017 Junjiro R. Okajima
4a4d8108
AM
18123+ *
18124+ * This program, aufs is free software; you can redistribute it and/or modify
18125+ * it under the terms of the GNU General Public License as published by
18126+ * the Free Software Foundation; either version 2 of the License, or
18127+ * (at your option) any later version.
18128+ *
18129+ * This program is distributed in the hope that it will be useful,
18130+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18131+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18132+ * GNU General Public License for more details.
18133+ *
18134+ * You should have received a copy of the GNU General Public License
523b37e3 18135+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 18136+ */
1facf9fc 18137+
4a4d8108
AM
18138+/*
18139+ * inode functions
18140+ */
1facf9fc 18141+
4a4d8108 18142+#include "aufs.h"
1308ab2a 18143+
4a4d8108
AM
18144+struct inode *au_igrab(struct inode *inode)
18145+{
18146+ if (inode) {
18147+ AuDebugOn(!atomic_read(&inode->i_count));
027c5e7a 18148+ ihold(inode);
1facf9fc 18149+ }
4a4d8108
AM
18150+ return inode;
18151+}
1facf9fc 18152+
4a4d8108
AM
18153+static void au_refresh_hinode_attr(struct inode *inode, int do_version)
18154+{
18155+ au_cpup_attr_all(inode, /*force*/0);
537831f9 18156+ au_update_iigen(inode, /*half*/1);
4a4d8108
AM
18157+ if (do_version)
18158+ inode->i_version++;
dece6358 18159+}
1facf9fc 18160+
027c5e7a 18161+static int au_ii_refresh(struct inode *inode, int *update)
dece6358 18162+{
e2f27e51 18163+ int err, e, nbr;
027c5e7a 18164+ umode_t type;
4a4d8108 18165+ aufs_bindex_t bindex, new_bindex;
1308ab2a 18166+ struct super_block *sb;
4a4d8108 18167+ struct au_iinfo *iinfo;
027c5e7a 18168+ struct au_hinode *p, *q, tmp;
1facf9fc 18169+
5afbbe0d 18170+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 18171+ IiMustWriteLock(inode);
1facf9fc 18172+
027c5e7a 18173+ *update = 0;
4a4d8108 18174+ sb = inode->i_sb;
e2f27e51 18175+ nbr = au_sbbot(sb) + 1;
027c5e7a 18176+ type = inode->i_mode & S_IFMT;
4a4d8108 18177+ iinfo = au_ii(inode);
e2f27e51 18178+ err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
4a4d8108 18179+ if (unlikely(err))
1308ab2a 18180+ goto out;
1facf9fc 18181+
5afbbe0d
AM
18182+ AuDebugOn(iinfo->ii_btop < 0);
18183+ p = au_hinode(iinfo, iinfo->ii_btop);
18184+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
18185+ bindex++, p++) {
18186+ if (!p->hi_inode)
18187+ continue;
1facf9fc 18188+
027c5e7a 18189+ AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
4a4d8108
AM
18190+ new_bindex = au_br_index(sb, p->hi_id);
18191+ if (new_bindex == bindex)
18192+ continue;
1facf9fc 18193+
4a4d8108 18194+ if (new_bindex < 0) {
027c5e7a 18195+ *update = 1;
4a4d8108
AM
18196+ au_hiput(p);
18197+ p->hi_inode = NULL;
18198+ continue;
1308ab2a 18199+ }
4a4d8108 18200+
5afbbe0d
AM
18201+ if (new_bindex < iinfo->ii_btop)
18202+ iinfo->ii_btop = new_bindex;
18203+ if (iinfo->ii_bbot < new_bindex)
18204+ iinfo->ii_bbot = new_bindex;
4a4d8108 18205+ /* swap two lower inode, and loop again */
5afbbe0d 18206+ q = au_hinode(iinfo, new_bindex);
4a4d8108
AM
18207+ tmp = *q;
18208+ *q = *p;
18209+ *p = tmp;
18210+ if (tmp.hi_inode) {
18211+ bindex--;
18212+ p--;
1308ab2a 18213+ }
18214+ }
4a4d8108 18215+ au_update_ibrange(inode, /*do_put_zero*/0);
e2f27e51 18216+ au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
4a4d8108
AM
18217+ e = au_dy_irefresh(inode);
18218+ if (unlikely(e && !err))
18219+ err = e;
1facf9fc 18220+
4f0767ce 18221+out:
027c5e7a
AM
18222+ AuTraceErr(err);
18223+ return err;
18224+}
18225+
b95c5147
AM
18226+void au_refresh_iop(struct inode *inode, int force_getattr)
18227+{
18228+ int type;
18229+ struct au_sbinfo *sbi = au_sbi(inode->i_sb);
18230+ const struct inode_operations *iop
18231+ = force_getattr ? aufs_iop : sbi->si_iop_array;
18232+
18233+ if (inode->i_op == iop)
18234+ return;
18235+
18236+ switch (inode->i_mode & S_IFMT) {
18237+ case S_IFDIR:
18238+ type = AuIop_DIR;
18239+ break;
18240+ case S_IFLNK:
18241+ type = AuIop_SYMLINK;
18242+ break;
18243+ default:
18244+ type = AuIop_OTHER;
18245+ break;
18246+ }
18247+
18248+ inode->i_op = iop + type;
18249+ /* unnecessary smp_wmb() */
18250+}
18251+
027c5e7a
AM
18252+int au_refresh_hinode_self(struct inode *inode)
18253+{
18254+ int err, update;
18255+
18256+ err = au_ii_refresh(inode, &update);
18257+ if (!err)
18258+ au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
18259+
18260+ AuTraceErr(err);
4a4d8108
AM
18261+ return err;
18262+}
1facf9fc 18263+
4a4d8108
AM
18264+int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
18265+{
027c5e7a 18266+ int err, e, update;
4a4d8108 18267+ unsigned int flags;
027c5e7a 18268+ umode_t mode;
5afbbe0d 18269+ aufs_bindex_t bindex, bbot;
027c5e7a 18270+ unsigned char isdir;
4a4d8108
AM
18271+ struct au_hinode *p;
18272+ struct au_iinfo *iinfo;
1facf9fc 18273+
027c5e7a 18274+ err = au_ii_refresh(inode, &update);
4a4d8108
AM
18275+ if (unlikely(err))
18276+ goto out;
18277+
18278+ update = 0;
18279+ iinfo = au_ii(inode);
5afbbe0d 18280+ p = au_hinode(iinfo, iinfo->ii_btop);
027c5e7a
AM
18281+ mode = (inode->i_mode & S_IFMT);
18282+ isdir = S_ISDIR(mode);
4a4d8108 18283+ flags = au_hi_flags(inode, isdir);
5afbbe0d
AM
18284+ bbot = au_dbbot(dentry);
18285+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
5527c038 18286+ struct inode *h_i, *h_inode;
4a4d8108
AM
18287+ struct dentry *h_d;
18288+
18289+ h_d = au_h_dptr(dentry, bindex);
5527c038 18290+ if (!h_d || d_is_negative(h_d))
4a4d8108
AM
18291+ continue;
18292+
5527c038
JR
18293+ h_inode = d_inode(h_d);
18294+ AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
5afbbe0d 18295+ if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
4a4d8108
AM
18296+ h_i = au_h_iptr(inode, bindex);
18297+ if (h_i) {
5527c038 18298+ if (h_i == h_inode)
4a4d8108
AM
18299+ continue;
18300+ err = -EIO;
18301+ break;
18302+ }
18303+ }
5afbbe0d
AM
18304+ if (bindex < iinfo->ii_btop)
18305+ iinfo->ii_btop = bindex;
18306+ if (iinfo->ii_bbot < bindex)
18307+ iinfo->ii_bbot = bindex;
5527c038 18308+ au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
4a4d8108 18309+ update = 1;
1308ab2a 18310+ }
4a4d8108
AM
18311+ au_update_ibrange(inode, /*do_put_zero*/0);
18312+ e = au_dy_irefresh(inode);
18313+ if (unlikely(e && !err))
18314+ err = e;
027c5e7a
AM
18315+ if (!err)
18316+ au_refresh_hinode_attr(inode, update && isdir);
4a4d8108 18317+
4f0767ce 18318+out:
4a4d8108 18319+ AuTraceErr(err);
1308ab2a 18320+ return err;
dece6358
AM
18321+}
18322+
4a4d8108 18323+static int set_inode(struct inode *inode, struct dentry *dentry)
dece6358 18324+{
4a4d8108
AM
18325+ int err;
18326+ unsigned int flags;
18327+ umode_t mode;
5afbbe0d 18328+ aufs_bindex_t bindex, btop, btail;
4a4d8108
AM
18329+ unsigned char isdir;
18330+ struct dentry *h_dentry;
18331+ struct inode *h_inode;
18332+ struct au_iinfo *iinfo;
b95c5147 18333+ struct inode_operations *iop;
dece6358 18334+
4a4d8108 18335+ IiMustWriteLock(inode);
dece6358 18336+
4a4d8108
AM
18337+ err = 0;
18338+ isdir = 0;
b95c5147 18339+ iop = au_sbi(inode->i_sb)->si_iop_array;
5afbbe0d
AM
18340+ btop = au_dbtop(dentry);
18341+ h_dentry = au_h_dptr(dentry, btop);
5527c038 18342+ h_inode = d_inode(h_dentry);
4a4d8108
AM
18343+ mode = h_inode->i_mode;
18344+ switch (mode & S_IFMT) {
18345+ case S_IFREG:
18346+ btail = au_dbtail(dentry);
b95c5147 18347+ inode->i_op = iop + AuIop_OTHER;
4a4d8108 18348+ inode->i_fop = &aufs_file_fop;
5afbbe0d 18349+ err = au_dy_iaop(inode, btop, h_inode);
4a4d8108
AM
18350+ if (unlikely(err))
18351+ goto out;
18352+ break;
18353+ case S_IFDIR:
18354+ isdir = 1;
18355+ btail = au_dbtaildir(dentry);
b95c5147 18356+ inode->i_op = iop + AuIop_DIR;
4a4d8108
AM
18357+ inode->i_fop = &aufs_dir_fop;
18358+ break;
18359+ case S_IFLNK:
18360+ btail = au_dbtail(dentry);
b95c5147 18361+ inode->i_op = iop + AuIop_SYMLINK;
4a4d8108
AM
18362+ break;
18363+ case S_IFBLK:
18364+ case S_IFCHR:
18365+ case S_IFIFO:
18366+ case S_IFSOCK:
18367+ btail = au_dbtail(dentry);
b95c5147 18368+ inode->i_op = iop + AuIop_OTHER;
38d290e6 18369+ init_special_inode(inode, mode, h_inode->i_rdev);
4a4d8108
AM
18370+ break;
18371+ default:
18372+ AuIOErr("Unknown file type 0%o\n", mode);
18373+ err = -EIO;
1308ab2a 18374+ goto out;
4a4d8108 18375+ }
dece6358 18376+
4a4d8108
AM
18377+ /* do not set hnotify for whiteouted dirs (SHWH mode) */
18378+ flags = au_hi_flags(inode, isdir);
18379+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
18380+ && au_ftest_hi(flags, HNOTIFY)
18381+ && dentry->d_name.len > AUFS_WH_PFX_LEN
18382+ && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
18383+ au_fclr_hi(flags, HNOTIFY);
18384+ iinfo = au_ii(inode);
5afbbe0d
AM
18385+ iinfo->ii_btop = btop;
18386+ iinfo->ii_bbot = btail;
18387+ for (bindex = btop; bindex <= btail; bindex++) {
4a4d8108
AM
18388+ h_dentry = au_h_dptr(dentry, bindex);
18389+ if (h_dentry)
18390+ au_set_h_iptr(inode, bindex,
5527c038 18391+ au_igrab(d_inode(h_dentry)), flags);
4a4d8108
AM
18392+ }
18393+ au_cpup_attr_all(inode, /*force*/1);
c1595e42
JR
18394+ /*
18395+ * to force calling aufs_get_acl() every time,
18396+ * do not call cache_no_acl() for aufs inode.
18397+ */
dece6358 18398+
4f0767ce 18399+out:
4a4d8108
AM
18400+ return err;
18401+}
dece6358 18402+
027c5e7a
AM
18403+/*
18404+ * successful returns with iinfo write_locked
18405+ * minus: errno
18406+ * zero: success, matched
18407+ * plus: no error, but unmatched
18408+ */
18409+static int reval_inode(struct inode *inode, struct dentry *dentry)
4a4d8108
AM
18410+{
18411+ int err;
cfc41e69 18412+ unsigned int gen, igflags;
5afbbe0d 18413+ aufs_bindex_t bindex, bbot;
4a4d8108 18414+ struct inode *h_inode, *h_dinode;
5527c038 18415+ struct dentry *h_dentry;
dece6358 18416+
4a4d8108
AM
18417+ /*
18418+ * before this function, if aufs got any iinfo lock, it must be only
18419+ * one, the parent dir.
18420+ * it can happen by UDBA and the obsoleted inode number.
18421+ */
18422+ err = -EIO;
18423+ if (unlikely(inode->i_ino == parent_ino(dentry)))
18424+ goto out;
18425+
027c5e7a 18426+ err = 1;
4a4d8108 18427+ ii_write_lock_new_child(inode);
5afbbe0d 18428+ h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
5527c038 18429+ h_dinode = d_inode(h_dentry);
5afbbe0d
AM
18430+ bbot = au_ibbot(inode);
18431+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108 18432+ h_inode = au_h_iptr(inode, bindex);
537831f9
AM
18433+ if (!h_inode || h_inode != h_dinode)
18434+ continue;
18435+
18436+ err = 0;
cfc41e69 18437+ gen = au_iigen(inode, &igflags);
537831f9 18438+ if (gen == au_digen(dentry)
cfc41e69 18439+ && !au_ig_ftest(igflags, HALF_REFRESHED))
4a4d8108 18440+ break;
537831f9
AM
18441+
18442+ /* fully refresh inode using dentry */
18443+ err = au_refresh_hinode(inode, dentry);
18444+ if (!err)
18445+ au_update_iigen(inode, /*half*/0);
18446+ break;
1facf9fc 18447+ }
dece6358 18448+
4a4d8108
AM
18449+ if (unlikely(err))
18450+ ii_write_unlock(inode);
4f0767ce 18451+out:
1facf9fc 18452+ return err;
18453+}
1facf9fc 18454+
4a4d8108
AM
18455+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18456+ unsigned int d_type, ino_t *ino)
1facf9fc 18457+{
521ced18
JR
18458+ int err, idx;
18459+ const int isnondir = d_type != DT_DIR;
1facf9fc 18460+
b752ccd1 18461+ /* prevent hardlinked inode number from race condition */
521ced18
JR
18462+ if (isnondir) {
18463+ err = au_xinondir_enter(sb, bindex, h_ino, &idx);
18464+ if (unlikely(err))
18465+ goto out;
4a4d8108 18466+ }
521ced18 18467+
4a4d8108
AM
18468+ err = au_xino_read(sb, bindex, h_ino, ino);
18469+ if (unlikely(err))
521ced18 18470+ goto out_xinondir;
1308ab2a 18471+
4a4d8108
AM
18472+ if (!*ino) {
18473+ err = -EIO;
18474+ *ino = au_xino_new_ino(sb);
18475+ if (unlikely(!*ino))
521ced18 18476+ goto out_xinondir;
4a4d8108
AM
18477+ err = au_xino_write(sb, bindex, h_ino, *ino);
18478+ if (unlikely(err))
521ced18 18479+ goto out_xinondir;
1308ab2a 18480+ }
1facf9fc 18481+
521ced18
JR
18482+out_xinondir:
18483+ if (isnondir && idx >= 0)
18484+ au_xinondir_leave(sb, bindex, h_ino, idx);
4f0767ce 18485+out:
1facf9fc 18486+ return err;
18487+}
18488+
4a4d8108
AM
18489+/* successful returns with iinfo write_locked */
18490+/* todo: return with unlocked? */
18491+struct inode *au_new_inode(struct dentry *dentry, int must_new)
1facf9fc 18492+{
5527c038 18493+ struct inode *inode, *h_inode;
4a4d8108
AM
18494+ struct dentry *h_dentry;
18495+ struct super_block *sb;
18496+ ino_t h_ino, ino;
521ced18 18497+ int err, idx, hlinked;
5afbbe0d 18498+ aufs_bindex_t btop;
1facf9fc 18499+
4a4d8108 18500+ sb = dentry->d_sb;
5afbbe0d
AM
18501+ btop = au_dbtop(dentry);
18502+ h_dentry = au_h_dptr(dentry, btop);
5527c038
JR
18503+ h_inode = d_inode(h_dentry);
18504+ h_ino = h_inode->i_ino;
521ced18 18505+ hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
b752ccd1 18506+
521ced18 18507+new_ino:
b752ccd1
AM
18508+ /*
18509+ * stop 'race'-ing between hardlinks under different
18510+ * parents.
18511+ */
521ced18
JR
18512+ if (hlinked) {
18513+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18514+ inode = ERR_PTR(err);
18515+ if (unlikely(err))
18516+ goto out;
18517+ }
b752ccd1 18518+
5afbbe0d 18519+ err = au_xino_read(sb, btop, h_ino, &ino);
4a4d8108
AM
18520+ inode = ERR_PTR(err);
18521+ if (unlikely(err))
521ced18 18522+ goto out_xinondir;
b752ccd1 18523+
4a4d8108
AM
18524+ if (!ino) {
18525+ ino = au_xino_new_ino(sb);
18526+ if (unlikely(!ino)) {
18527+ inode = ERR_PTR(-EIO);
521ced18 18528+ goto out_xinondir;
dece6358
AM
18529+ }
18530+ }
1facf9fc 18531+
4a4d8108
AM
18532+ AuDbg("i%lu\n", (unsigned long)ino);
18533+ inode = au_iget_locked(sb, ino);
18534+ err = PTR_ERR(inode);
18535+ if (IS_ERR(inode))
521ced18 18536+ goto out_xinondir;
1facf9fc 18537+
4a4d8108
AM
18538+ AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
18539+ if (inode->i_state & I_NEW) {
18540+ ii_write_lock_new_child(inode);
18541+ err = set_inode(inode, dentry);
18542+ if (!err) {
18543+ unlock_new_inode(inode);
521ced18 18544+ goto out_xinondir; /* success */
4a4d8108 18545+ }
1308ab2a 18546+
027c5e7a
AM
18547+ /*
18548+ * iget_failed() calls iput(), but we need to call
18549+ * ii_write_unlock() after iget_failed(). so dirty hack for
18550+ * i_count.
18551+ */
18552+ atomic_inc(&inode->i_count);
4a4d8108 18553+ iget_failed(inode);
027c5e7a 18554+ ii_write_unlock(inode);
5afbbe0d 18555+ au_xino_write(sb, btop, h_ino, /*ino*/0);
027c5e7a
AM
18556+ /* ignore this error */
18557+ goto out_iput;
18558+ } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
b752ccd1
AM
18559+ /*
18560+ * horrible race condition between lookup, readdir and copyup
18561+ * (or something).
18562+ */
521ced18
JR
18563+ if (hlinked && idx >= 0)
18564+ au_xinondir_leave(sb, btop, h_ino, idx);
027c5e7a
AM
18565+ err = reval_inode(inode, dentry);
18566+ if (unlikely(err < 0)) {
521ced18 18567+ hlinked = 0;
027c5e7a
AM
18568+ goto out_iput;
18569+ }
521ced18 18570+ if (!err)
4a4d8108 18571+ goto out; /* success */
521ced18
JR
18572+ else if (hlinked && idx >= 0) {
18573+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18574+ if (unlikely(err)) {
18575+ iput(inode);
18576+ inode = ERR_PTR(err);
18577+ goto out;
18578+ }
18579+ }
4a4d8108
AM
18580+ }
18581+
5527c038 18582+ if (unlikely(au_test_fs_unique_ino(h_inode)))
4a4d8108 18583+ AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
523b37e3 18584+ " b%d, %s, %pd, hi%lu, i%lu.\n",
5afbbe0d 18585+ btop, au_sbtype(h_dentry->d_sb), dentry,
4a4d8108
AM
18586+ (unsigned long)h_ino, (unsigned long)ino);
18587+ ino = 0;
5afbbe0d 18588+ err = au_xino_write(sb, btop, h_ino, /*ino*/0);
4a4d8108
AM
18589+ if (!err) {
18590+ iput(inode);
521ced18
JR
18591+ if (hlinked && idx >= 0)
18592+ au_xinondir_leave(sb, btop, h_ino, idx);
4a4d8108
AM
18593+ goto new_ino;
18594+ }
1308ab2a 18595+
4f0767ce 18596+out_iput:
4a4d8108 18597+ iput(inode);
4a4d8108 18598+ inode = ERR_PTR(err);
521ced18
JR
18599+out_xinondir:
18600+ if (hlinked && idx >= 0)
18601+ au_xinondir_leave(sb, btop, h_ino, idx);
4f0767ce 18602+out:
4a4d8108 18603+ return inode;
1facf9fc 18604+}
18605+
4a4d8108 18606+/* ---------------------------------------------------------------------- */
1facf9fc 18607+
4a4d8108
AM
18608+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18609+ struct inode *inode)
18610+{
18611+ int err;
076b876e 18612+ struct inode *hi;
1facf9fc 18613+
4a4d8108 18614+ err = au_br_rdonly(au_sbr(sb, bindex));
1facf9fc 18615+
4a4d8108
AM
18616+ /* pseudo-link after flushed may happen out of bounds */
18617+ if (!err
18618+ && inode
5afbbe0d
AM
18619+ && au_ibtop(inode) <= bindex
18620+ && bindex <= au_ibbot(inode)) {
4a4d8108
AM
18621+ /*
18622+ * permission check is unnecessary since vfsub routine
18623+ * will be called later
18624+ */
076b876e 18625+ hi = au_h_iptr(inode, bindex);
4a4d8108
AM
18626+ if (hi)
18627+ err = IS_IMMUTABLE(hi) ? -EROFS : 0;
1facf9fc 18628+ }
18629+
4a4d8108
AM
18630+ return err;
18631+}
dece6358 18632+
4a4d8108
AM
18633+int au_test_h_perm(struct inode *h_inode, int mask)
18634+{
2dfbb274 18635+ if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
4a4d8108
AM
18636+ return 0;
18637+ return inode_permission(h_inode, mask);
18638+}
1facf9fc 18639+
4a4d8108
AM
18640+int au_test_h_perm_sio(struct inode *h_inode, int mask)
18641+{
18642+ if (au_test_nfs(h_inode->i_sb)
18643+ && (mask & MAY_WRITE)
18644+ && S_ISDIR(h_inode->i_mode))
18645+ mask |= MAY_READ; /* force permission check */
18646+ return au_test_h_perm(h_inode, mask);
1facf9fc 18647+}
7f207e10
AM
18648diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
18649--- /usr/share/empty/fs/aufs/inode.h 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
18650+++ linux/fs/aufs/inode.h 2017-11-12 22:24:44.707577830 +0100
18651@@ -0,0 +1,695 @@
4a4d8108 18652+/*
a2654f78 18653+ * Copyright (C) 2005-2017 Junjiro R. Okajima
4a4d8108
AM
18654+ *
18655+ * This program, aufs is free software; you can redistribute it and/or modify
18656+ * it under the terms of the GNU General Public License as published by
18657+ * the Free Software Foundation; either version 2 of the License, or
18658+ * (at your option) any later version.
18659+ *
18660+ * This program is distributed in the hope that it will be useful,
18661+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18662+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18663+ * GNU General Public License for more details.
18664+ *
18665+ * You should have received a copy of the GNU General Public License
523b37e3 18666+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 18667+ */
1facf9fc 18668+
1308ab2a 18669+/*
4a4d8108 18670+ * inode operations
1308ab2a 18671+ */
dece6358 18672+
4a4d8108
AM
18673+#ifndef __AUFS_INODE_H__
18674+#define __AUFS_INODE_H__
dece6358 18675+
4a4d8108 18676+#ifdef __KERNEL__
1308ab2a 18677+
4a4d8108 18678+#include <linux/fsnotify.h>
4a4d8108 18679+#include "rwsem.h"
1308ab2a 18680+
4a4d8108 18681+struct vfsmount;
1facf9fc 18682+
4a4d8108
AM
18683+struct au_hnotify {
18684+#ifdef CONFIG_AUFS_HNOTIFY
18685+#ifdef CONFIG_AUFS_HFSNOTIFY
7f207e10 18686+ /* never use fsnotify_add_vfsmount_mark() */
0c5527e5 18687+ struct fsnotify_mark hn_mark;
4a4d8108 18688+#endif
1c60b727 18689+ struct inode *hn_aufs_inode; /* no get/put */
4a4d8108
AM
18690+#endif
18691+} ____cacheline_aligned_in_smp;
1facf9fc 18692+
4a4d8108
AM
18693+struct au_hinode {
18694+ struct inode *hi_inode;
18695+ aufs_bindex_t hi_id;
18696+#ifdef CONFIG_AUFS_HNOTIFY
18697+ struct au_hnotify *hi_notify;
18698+#endif
dece6358 18699+
4a4d8108
AM
18700+ /* reference to the copied-up whiteout with get/put */
18701+ struct dentry *hi_whdentry;
18702+};
dece6358 18703+
537831f9
AM
18704+/* ig_flags */
18705+#define AuIG_HALF_REFRESHED 1
18706+#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
18707+#define au_ig_fset(flags, name) \
18708+ do { (flags) |= AuIG_##name; } while (0)
18709+#define au_ig_fclr(flags, name) \
18710+ do { (flags) &= ~AuIG_##name; } while (0)
18711+
18712+struct au_iigen {
be52b249 18713+ spinlock_t ig_spin;
537831f9
AM
18714+ __u32 ig_generation, ig_flags;
18715+};
18716+
4a4d8108
AM
18717+struct au_vdir;
18718+struct au_iinfo {
7a9e40b8 18719+ struct au_iigen ii_generation;
4a4d8108 18720+ struct super_block *ii_hsb1; /* no get/put */
1facf9fc 18721+
4a4d8108 18722+ struct au_rwsem ii_rwsem;
5afbbe0d 18723+ aufs_bindex_t ii_btop, ii_bbot;
4a4d8108
AM
18724+ __u32 ii_higen;
18725+ struct au_hinode *ii_hinode;
18726+ struct au_vdir *ii_vdir;
18727+};
1facf9fc 18728+
4a4d8108
AM
18729+struct au_icntnr {
18730+ struct au_iinfo iinfo;
18731+ struct inode vfs_inode;
8b6a4947 18732+ struct hlist_bl_node plink;
4a4d8108 18733+} ____cacheline_aligned_in_smp;
1308ab2a 18734+
4a4d8108
AM
18735+/* au_pin flags */
18736+#define AuPin_DI_LOCKED 1
18737+#define AuPin_MNT_WRITE (1 << 1)
18738+#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
7f207e10
AM
18739+#define au_fset_pin(flags, name) \
18740+ do { (flags) |= AuPin_##name; } while (0)
18741+#define au_fclr_pin(flags, name) \
18742+ do { (flags) &= ~AuPin_##name; } while (0)
4a4d8108
AM
18743+
18744+struct au_pin {
18745+ /* input */
18746+ struct dentry *dentry;
18747+ unsigned int udba;
18748+ unsigned char lsc_di, lsc_hi, flags;
18749+ aufs_bindex_t bindex;
18750+
18751+ /* output */
18752+ struct dentry *parent;
18753+ struct au_hinode *hdir;
18754+ struct vfsmount *h_mnt;
86dc4139
AM
18755+
18756+ /* temporary unlock/relock for copyup */
18757+ struct dentry *h_dentry, *h_parent;
18758+ struct au_branch *br;
18759+ struct task_struct *task;
4a4d8108 18760+};
1facf9fc 18761+
86dc4139 18762+void au_pin_hdir_unlock(struct au_pin *p);
c1595e42 18763+int au_pin_hdir_lock(struct au_pin *p);
86dc4139 18764+int au_pin_hdir_relock(struct au_pin *p);
86dc4139
AM
18765+void au_pin_hdir_acquire_nest(struct au_pin *p);
18766+void au_pin_hdir_release(struct au_pin *p);
18767+
1308ab2a 18768+/* ---------------------------------------------------------------------- */
18769+
4a4d8108 18770+static inline struct au_iinfo *au_ii(struct inode *inode)
1facf9fc 18771+{
5afbbe0d
AM
18772+ BUG_ON(is_bad_inode(inode));
18773+ return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
4a4d8108 18774+}
1facf9fc 18775+
4a4d8108 18776+/* ---------------------------------------------------------------------- */
1facf9fc 18777+
4a4d8108
AM
18778+/* inode.c */
18779+struct inode *au_igrab(struct inode *inode);
b95c5147 18780+void au_refresh_iop(struct inode *inode, int force_getattr);
027c5e7a 18781+int au_refresh_hinode_self(struct inode *inode);
4a4d8108
AM
18782+int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
18783+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18784+ unsigned int d_type, ino_t *ino);
18785+struct inode *au_new_inode(struct dentry *dentry, int must_new);
18786+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18787+ struct inode *inode);
18788+int au_test_h_perm(struct inode *h_inode, int mask);
18789+int au_test_h_perm_sio(struct inode *h_inode, int mask);
1facf9fc 18790+
4a4d8108
AM
18791+static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
18792+ ino_t h_ino, unsigned int d_type, ino_t *ino)
18793+{
18794+#ifdef CONFIG_AUFS_SHWH
18795+ return au_ino(sb, bindex, h_ino, d_type, ino);
18796+#else
18797+ return 0;
18798+#endif
18799+}
1facf9fc 18800+
4a4d8108 18801+/* i_op.c */
b95c5147
AM
18802+enum {
18803+ AuIop_SYMLINK,
18804+ AuIop_DIR,
18805+ AuIop_OTHER,
18806+ AuIop_Last
18807+};
18808+extern struct inode_operations aufs_iop[AuIop_Last],
18809+ aufs_iop_nogetattr[AuIop_Last];
1308ab2a 18810+
4a4d8108
AM
18811+/* au_wr_dir flags */
18812+#define AuWrDir_ADD_ENTRY 1
7e9cd9fe
AM
18813+#define AuWrDir_ISDIR (1 << 1)
18814+#define AuWrDir_TMPFILE (1 << 2)
4a4d8108 18815+#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
7f207e10
AM
18816+#define au_fset_wrdir(flags, name) \
18817+ do { (flags) |= AuWrDir_##name; } while (0)
18818+#define au_fclr_wrdir(flags, name) \
18819+ do { (flags) &= ~AuWrDir_##name; } while (0)
1facf9fc 18820+
4a4d8108
AM
18821+struct au_wr_dir_args {
18822+ aufs_bindex_t force_btgt;
18823+ unsigned char flags;
18824+};
18825+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18826+ struct au_wr_dir_args *args);
dece6358 18827+
4a4d8108
AM
18828+struct dentry *au_pinned_h_parent(struct au_pin *pin);
18829+void au_pin_init(struct au_pin *pin, struct dentry *dentry,
18830+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18831+ unsigned int udba, unsigned char flags);
18832+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18833+ unsigned int udba, unsigned char flags) __must_check;
18834+int au_do_pin(struct au_pin *pin) __must_check;
18835+void au_unpin(struct au_pin *pin);
c1595e42
JR
18836+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
18837+
18838+#define AuIcpup_DID_CPUP 1
18839+#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
18840+#define au_fset_icpup(flags, name) \
18841+ do { (flags) |= AuIcpup_##name; } while (0)
18842+#define au_fclr_icpup(flags, name) \
18843+ do { (flags) &= ~AuIcpup_##name; } while (0)
18844+
18845+struct au_icpup_args {
18846+ unsigned char flags;
18847+ unsigned char pin_flags;
18848+ aufs_bindex_t btgt;
18849+ unsigned int udba;
18850+ struct au_pin pin;
18851+ struct path h_path;
18852+ struct inode *h_inode;
18853+};
18854+
18855+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
18856+ struct au_icpup_args *a);
18857+
a2654f78
AM
18858+int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
18859+ int locked);
1facf9fc 18860+
4a4d8108
AM
18861+/* i_op_add.c */
18862+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
18863+ struct dentry *h_parent, int isdir);
7eafdf33
AM
18864+int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
18865+ dev_t dev);
4a4d8108 18866+int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
7eafdf33 18867+int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
b4510431 18868+ bool want_excl);
b912730e
AM
18869+struct vfsub_aopen_args;
18870+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
18871+ struct vfsub_aopen_args *args);
38d290e6 18872+int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
4a4d8108
AM
18873+int aufs_link(struct dentry *src_dentry, struct inode *dir,
18874+ struct dentry *dentry);
7eafdf33 18875+int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
1facf9fc 18876+
4a4d8108
AM
18877+/* i_op_del.c */
18878+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
18879+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
18880+ struct dentry *h_parent, int isdir);
18881+int aufs_unlink(struct inode *dir, struct dentry *dentry);
18882+int aufs_rmdir(struct inode *dir, struct dentry *dentry);
1308ab2a 18883+
4a4d8108
AM
18884+/* i_op_ren.c */
18885+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
18886+int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
f2c43d5f
AM
18887+ struct inode *dir, struct dentry *dentry,
18888+ unsigned int flags);
1facf9fc 18889+
4a4d8108
AM
18890+/* iinfo.c */
18891+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
18892+void au_hiput(struct au_hinode *hinode);
18893+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
18894+ struct dentry *h_wh);
18895+unsigned int au_hi_flags(struct inode *inode, int isdir);
1308ab2a 18896+
4a4d8108
AM
18897+/* hinode flags */
18898+#define AuHi_XINO 1
18899+#define AuHi_HNOTIFY (1 << 1)
18900+#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
7f207e10
AM
18901+#define au_fset_hi(flags, name) \
18902+ do { (flags) |= AuHi_##name; } while (0)
18903+#define au_fclr_hi(flags, name) \
18904+ do { (flags) &= ~AuHi_##name; } while (0)
1facf9fc 18905+
4a4d8108
AM
18906+#ifndef CONFIG_AUFS_HNOTIFY
18907+#undef AuHi_HNOTIFY
18908+#define AuHi_HNOTIFY 0
18909+#endif
1facf9fc 18910+
4a4d8108
AM
18911+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
18912+ struct inode *h_inode, unsigned int flags);
1facf9fc 18913+
537831f9 18914+void au_update_iigen(struct inode *inode, int half);
4a4d8108 18915+void au_update_ibrange(struct inode *inode, int do_put_zero);
1facf9fc 18916+
4a4d8108 18917+void au_icntnr_init_once(void *_c);
5afbbe0d 18918+void au_hinode_init(struct au_hinode *hinode);
4a4d8108
AM
18919+int au_iinfo_init(struct inode *inode);
18920+void au_iinfo_fin(struct inode *inode);
e2f27e51 18921+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
1308ab2a 18922+
e49829fe 18923+#ifdef CONFIG_PROC_FS
4a4d8108 18924+/* plink.c */
e49829fe 18925+int au_plink_maint(struct super_block *sb, int flags);
7e9cd9fe 18926+struct au_sbinfo;
e49829fe
JR
18927+void au_plink_maint_leave(struct au_sbinfo *sbinfo);
18928+int au_plink_maint_enter(struct super_block *sb);
4a4d8108
AM
18929+#ifdef CONFIG_AUFS_DEBUG
18930+void au_plink_list(struct super_block *sb);
18931+#else
18932+AuStubVoid(au_plink_list, struct super_block *sb)
18933+#endif
18934+int au_plink_test(struct inode *inode);
18935+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
18936+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
18937+ struct dentry *h_dentry);
e49829fe
JR
18938+void au_plink_put(struct super_block *sb, int verbose);
18939+void au_plink_clean(struct super_block *sb, int verbose);
4a4d8108 18940+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
e49829fe
JR
18941+#else
18942+AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
18943+AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
18944+AuStubInt0(au_plink_maint_enter, struct super_block *sb);
18945+AuStubVoid(au_plink_list, struct super_block *sb);
18946+AuStubInt0(au_plink_test, struct inode *inode);
18947+AuStub(struct dentry *, au_plink_lkup, return NULL,
18948+ struct inode *inode, aufs_bindex_t bindex);
18949+AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
18950+ struct dentry *h_dentry);
18951+AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
18952+AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
18953+AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
18954+#endif /* CONFIG_PROC_FS */
1facf9fc 18955+
c1595e42
JR
18956+#ifdef CONFIG_AUFS_XATTR
18957+/* xattr.c */
7e9cd9fe
AM
18958+int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
18959+ unsigned int verbose);
c1595e42 18960+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
f2c43d5f 18961+void au_xattr_init(struct super_block *sb);
c1595e42
JR
18962+#else
18963+AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
7e9cd9fe 18964+ int ignore_flags, unsigned int verbose);
f2c43d5f 18965+AuStubVoid(au_xattr_init, struct super_block *sb);
c1595e42
JR
18966+#endif
18967+
18968+#ifdef CONFIG_FS_POSIX_ACL
18969+struct posix_acl *aufs_get_acl(struct inode *inode, int type);
18970+int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
18971+#endif
18972+
18973+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
18974+enum {
18975+ AU_XATTR_SET,
c1595e42
JR
18976+ AU_ACL_SET
18977+};
18978+
f2c43d5f 18979+struct au_sxattr {
c1595e42
JR
18980+ int type;
18981+ union {
18982+ struct {
18983+ const char *name;
18984+ const void *value;
18985+ size_t size;
18986+ int flags;
18987+ } set;
18988+ struct {
c1595e42
JR
18989+ struct posix_acl *acl;
18990+ int type;
18991+ } acl_set;
18992+ } u;
18993+};
f2c43d5f
AM
18994+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
18995+ struct au_sxattr *arg);
c1595e42
JR
18996+#endif
18997+
4a4d8108 18998+/* ---------------------------------------------------------------------- */
1308ab2a 18999+
4a4d8108
AM
19000+/* lock subclass for iinfo */
19001+enum {
19002+ AuLsc_II_CHILD, /* child first */
19003+ AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
19004+ AuLsc_II_CHILD3, /* copyup dirs */
19005+ AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
19006+ AuLsc_II_PARENT2,
19007+ AuLsc_II_PARENT3, /* copyup dirs */
19008+ AuLsc_II_NEW_CHILD
19009+};
1308ab2a 19010+
1facf9fc 19011+/*
4a4d8108
AM
19012+ * ii_read_lock_child, ii_write_lock_child,
19013+ * ii_read_lock_child2, ii_write_lock_child2,
19014+ * ii_read_lock_child3, ii_write_lock_child3,
19015+ * ii_read_lock_parent, ii_write_lock_parent,
19016+ * ii_read_lock_parent2, ii_write_lock_parent2,
19017+ * ii_read_lock_parent3, ii_write_lock_parent3,
19018+ * ii_read_lock_new_child, ii_write_lock_new_child,
1facf9fc 19019+ */
4a4d8108
AM
19020+#define AuReadLockFunc(name, lsc) \
19021+static inline void ii_read_lock_##name(struct inode *i) \
19022+{ \
19023+ au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19024+}
19025+
19026+#define AuWriteLockFunc(name, lsc) \
19027+static inline void ii_write_lock_##name(struct inode *i) \
19028+{ \
19029+ au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19030+}
19031+
19032+#define AuRWLockFuncs(name, lsc) \
19033+ AuReadLockFunc(name, lsc) \
19034+ AuWriteLockFunc(name, lsc)
19035+
19036+AuRWLockFuncs(child, CHILD);
19037+AuRWLockFuncs(child2, CHILD2);
19038+AuRWLockFuncs(child3, CHILD3);
19039+AuRWLockFuncs(parent, PARENT);
19040+AuRWLockFuncs(parent2, PARENT2);
19041+AuRWLockFuncs(parent3, PARENT3);
19042+AuRWLockFuncs(new_child, NEW_CHILD);
19043+
19044+#undef AuReadLockFunc
19045+#undef AuWriteLockFunc
19046+#undef AuRWLockFuncs
1facf9fc 19047+
8b6a4947
AM
19048+#define ii_read_unlock(i) au_rw_read_unlock(&au_ii(i)->ii_rwsem)
19049+#define ii_write_unlock(i) au_rw_write_unlock(&au_ii(i)->ii_rwsem)
19050+#define ii_downgrade_lock(i) au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
1facf9fc 19051+
4a4d8108
AM
19052+#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
19053+#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
19054+#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
1facf9fc 19055+
4a4d8108 19056+/* ---------------------------------------------------------------------- */
1308ab2a 19057+
027c5e7a
AM
19058+static inline void au_icntnr_init(struct au_icntnr *c)
19059+{
19060+#ifdef CONFIG_AUFS_DEBUG
19061+ c->vfs_inode.i_mode = 0;
19062+#endif
19063+}
19064+
cfc41e69 19065+static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
4a4d8108 19066+{
537831f9
AM
19067+ unsigned int gen;
19068+ struct au_iinfo *iinfo;
be52b249 19069+ struct au_iigen *iigen;
537831f9
AM
19070+
19071+ iinfo = au_ii(inode);
be52b249
AM
19072+ iigen = &iinfo->ii_generation;
19073+ spin_lock(&iigen->ig_spin);
cfc41e69
AM
19074+ if (igflags)
19075+ *igflags = iigen->ig_flags;
be52b249
AM
19076+ gen = iigen->ig_generation;
19077+ spin_unlock(&iigen->ig_spin);
537831f9
AM
19078+
19079+ return gen;
4a4d8108 19080+}
1308ab2a 19081+
4a4d8108
AM
19082+/* tiny test for inode number */
19083+/* tmpfs generation is too rough */
19084+static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
19085+{
19086+ struct au_iinfo *iinfo;
1308ab2a 19087+
4a4d8108
AM
19088+ iinfo = au_ii(inode);
19089+ AuRwMustAnyLock(&iinfo->ii_rwsem);
19090+ return !(iinfo->ii_hsb1 == h_inode->i_sb
19091+ && iinfo->ii_higen == h_inode->i_generation);
19092+}
1308ab2a 19093+
4a4d8108
AM
19094+static inline void au_iigen_dec(struct inode *inode)
19095+{
537831f9 19096+ struct au_iinfo *iinfo;
be52b249 19097+ struct au_iigen *iigen;
537831f9
AM
19098+
19099+ iinfo = au_ii(inode);
be52b249
AM
19100+ iigen = &iinfo->ii_generation;
19101+ spin_lock(&iigen->ig_spin);
19102+ iigen->ig_generation--;
19103+ spin_unlock(&iigen->ig_spin);
027c5e7a
AM
19104+}
19105+
19106+static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
19107+{
19108+ int err;
19109+
19110+ err = 0;
537831f9 19111+ if (unlikely(inode && au_iigen(inode, NULL) != sigen))
027c5e7a
AM
19112+ err = -EIO;
19113+
19114+ return err;
4a4d8108 19115+}
1308ab2a 19116+
4a4d8108 19117+/* ---------------------------------------------------------------------- */
1308ab2a 19118+
5afbbe0d
AM
19119+static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
19120+ aufs_bindex_t bindex)
19121+{
19122+ return iinfo->ii_hinode + bindex;
19123+}
19124+
19125+static inline int au_is_bad_inode(struct inode *inode)
19126+{
19127+ return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
19128+}
19129+
4a4d8108
AM
19130+static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
19131+ aufs_bindex_t bindex)
19132+{
19133+ IiMustAnyLock(inode);
5afbbe0d 19134+ return au_hinode(au_ii(inode), bindex)->hi_id;
4a4d8108 19135+}
1308ab2a 19136+
5afbbe0d 19137+static inline aufs_bindex_t au_ibtop(struct inode *inode)
4a4d8108
AM
19138+{
19139+ IiMustAnyLock(inode);
5afbbe0d 19140+ return au_ii(inode)->ii_btop;
4a4d8108 19141+}
1308ab2a 19142+
5afbbe0d 19143+static inline aufs_bindex_t au_ibbot(struct inode *inode)
4a4d8108
AM
19144+{
19145+ IiMustAnyLock(inode);
5afbbe0d 19146+ return au_ii(inode)->ii_bbot;
4a4d8108 19147+}
1308ab2a 19148+
4a4d8108
AM
19149+static inline struct au_vdir *au_ivdir(struct inode *inode)
19150+{
19151+ IiMustAnyLock(inode);
19152+ return au_ii(inode)->ii_vdir;
19153+}
1308ab2a 19154+
4a4d8108
AM
19155+static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
19156+{
19157+ IiMustAnyLock(inode);
5afbbe0d 19158+ return au_hinode(au_ii(inode), bindex)->hi_whdentry;
4a4d8108 19159+}
1308ab2a 19160+
5afbbe0d 19161+static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 19162+{
4a4d8108 19163+ IiMustWriteLock(inode);
5afbbe0d 19164+ au_ii(inode)->ii_btop = bindex;
4a4d8108 19165+}
1308ab2a 19166+
5afbbe0d 19167+static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
4a4d8108
AM
19168+{
19169+ IiMustWriteLock(inode);
5afbbe0d 19170+ au_ii(inode)->ii_bbot = bindex;
1308ab2a 19171+}
19172+
4a4d8108
AM
19173+static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
19174+{
19175+ IiMustWriteLock(inode);
19176+ au_ii(inode)->ii_vdir = vdir;
19177+}
1facf9fc 19178+
4a4d8108 19179+static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 19180+{
4a4d8108 19181+ IiMustAnyLock(inode);
5afbbe0d 19182+ return au_hinode(au_ii(inode), bindex);
4a4d8108 19183+}
dece6358 19184+
4a4d8108 19185+/* ---------------------------------------------------------------------- */
1facf9fc 19186+
4a4d8108
AM
19187+static inline struct dentry *au_pinned_parent(struct au_pin *pin)
19188+{
19189+ if (pin)
19190+ return pin->parent;
19191+ return NULL;
1facf9fc 19192+}
19193+
4a4d8108 19194+static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
1facf9fc 19195+{
4a4d8108
AM
19196+ if (pin && pin->hdir)
19197+ return pin->hdir->hi_inode;
19198+ return NULL;
1308ab2a 19199+}
1facf9fc 19200+
4a4d8108
AM
19201+static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
19202+{
19203+ if (pin)
19204+ return pin->hdir;
19205+ return NULL;
19206+}
1facf9fc 19207+
4a4d8108 19208+static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
1308ab2a 19209+{
4a4d8108
AM
19210+ if (pin)
19211+ pin->dentry = dentry;
19212+}
1308ab2a 19213+
4a4d8108
AM
19214+static inline void au_pin_set_parent_lflag(struct au_pin *pin,
19215+ unsigned char lflag)
19216+{
19217+ if (pin) {
7f207e10 19218+ if (lflag)
4a4d8108 19219+ au_fset_pin(pin->flags, DI_LOCKED);
7f207e10 19220+ else
4a4d8108 19221+ au_fclr_pin(pin->flags, DI_LOCKED);
1308ab2a 19222+ }
4a4d8108
AM
19223+}
19224+
7e9cd9fe 19225+#if 0 /* reserved */
4a4d8108
AM
19226+static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
19227+{
19228+ if (pin) {
19229+ dput(pin->parent);
19230+ pin->parent = dget(parent);
1facf9fc 19231+ }
4a4d8108 19232+}
7e9cd9fe 19233+#endif
1facf9fc 19234+
4a4d8108
AM
19235+/* ---------------------------------------------------------------------- */
19236+
027c5e7a 19237+struct au_branch;
4a4d8108
AM
19238+#ifdef CONFIG_AUFS_HNOTIFY
19239+struct au_hnotify_op {
19240+ void (*ctl)(struct au_hinode *hinode, int do_set);
027c5e7a 19241+ int (*alloc)(struct au_hinode *hinode);
7eafdf33
AM
19242+
19243+ /*
19244+ * if it returns true, the the caller should free hinode->hi_notify,
19245+ * otherwise ->free() frees it.
19246+ */
19247+ int (*free)(struct au_hinode *hinode,
19248+ struct au_hnotify *hn) __must_check;
4a4d8108
AM
19249+
19250+ void (*fin)(void);
19251+ int (*init)(void);
027c5e7a
AM
19252+
19253+ int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
19254+ void (*fin_br)(struct au_branch *br);
19255+ int (*init_br)(struct au_branch *br, int perm);
4a4d8108
AM
19256+};
19257+
19258+/* hnotify.c */
027c5e7a 19259+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
4a4d8108
AM
19260+void au_hn_free(struct au_hinode *hinode);
19261+void au_hn_ctl(struct au_hinode *hinode, int do_set);
19262+void au_hn_reset(struct inode *inode, unsigned int flags);
19263+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
19264+ struct qstr *h_child_qstr, struct inode *h_child_inode);
027c5e7a
AM
19265+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
19266+int au_hnotify_init_br(struct au_branch *br, int perm);
19267+void au_hnotify_fin_br(struct au_branch *br);
4a4d8108
AM
19268+int __init au_hnotify_init(void);
19269+void au_hnotify_fin(void);
19270+
7f207e10 19271+/* hfsnotify.c */
4a4d8108
AM
19272+extern const struct au_hnotify_op au_hnotify_op;
19273+
19274+static inline
19275+void au_hn_init(struct au_hinode *hinode)
19276+{
19277+ hinode->hi_notify = NULL;
1308ab2a 19278+}
19279+
53392da6
AM
19280+static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
19281+{
19282+ return hinode->hi_notify;
19283+}
19284+
4a4d8108 19285+#else
c1595e42
JR
19286+AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
19287+ struct au_hinode *hinode __maybe_unused,
19288+ struct inode *inode __maybe_unused)
19289+AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
4a4d8108
AM
19290+AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
19291+AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
19292+ int do_set __maybe_unused)
19293+AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
19294+ unsigned int flags __maybe_unused)
027c5e7a
AM
19295+AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
19296+ struct au_branch *br __maybe_unused,
19297+ int perm __maybe_unused)
19298+AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
19299+ int perm __maybe_unused)
19300+AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
4a4d8108
AM
19301+AuStubInt0(__init au_hnotify_init, void)
19302+AuStubVoid(au_hnotify_fin, void)
19303+AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
19304+#endif /* CONFIG_AUFS_HNOTIFY */
19305+
19306+static inline void au_hn_suspend(struct au_hinode *hdir)
19307+{
19308+ au_hn_ctl(hdir, /*do_set*/0);
1308ab2a 19309+}
19310+
4a4d8108 19311+static inline void au_hn_resume(struct au_hinode *hdir)
1308ab2a 19312+{
4a4d8108
AM
19313+ au_hn_ctl(hdir, /*do_set*/1);
19314+}
1308ab2a 19315+
5afbbe0d 19316+static inline void au_hn_inode_lock(struct au_hinode *hdir)
4a4d8108 19317+{
febd17d6 19318+ inode_lock(hdir->hi_inode);
4a4d8108
AM
19319+ au_hn_suspend(hdir);
19320+}
dece6358 19321+
5afbbe0d 19322+static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
4a4d8108
AM
19323+ unsigned int sc __maybe_unused)
19324+{
febd17d6 19325+ inode_lock_nested(hdir->hi_inode, sc);
4a4d8108 19326+ au_hn_suspend(hdir);
1facf9fc 19327+}
1facf9fc 19328+
8b6a4947
AM
19329+#if 0 /* unused */
19330+#include "vfsub.h"
3c1bdaff
AM
19331+static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
19332+ unsigned int sc)
19333+{
19334+ vfsub_inode_lock_shared_nested(hdir->hi_inode, sc);
19335+ au_hn_suspend(hdir);
19336+}
8b6a4947 19337+#endif
3c1bdaff 19338+
5afbbe0d 19339+static inline void au_hn_inode_unlock(struct au_hinode *hdir)
4a4d8108
AM
19340+{
19341+ au_hn_resume(hdir);
febd17d6 19342+ inode_unlock(hdir->hi_inode);
4a4d8108
AM
19343+}
19344+
19345+#endif /* __KERNEL__ */
19346+#endif /* __AUFS_INODE_H__ */
7f207e10
AM
19347diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
19348--- /usr/share/empty/fs/aufs/ioctl.c 1970-01-01 01:00:00.000000000 +0100
1c60b727 19349+++ linux/fs/aufs/ioctl.c 2017-07-29 12:14:25.903042072 +0200
c1595e42 19350@@ -0,0 +1,219 @@
4a4d8108 19351+/*
a2654f78 19352+ * Copyright (C) 2005-2017 Junjiro R. Okajima
4a4d8108
AM
19353+ *
19354+ * This program, aufs is free software; you can redistribute it and/or modify
19355+ * it under the terms of the GNU General Public License as published by
19356+ * the Free Software Foundation; either version 2 of the License, or
19357+ * (at your option) any later version.
19358+ *
19359+ * This program is distributed in the hope that it will be useful,
19360+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19361+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19362+ * GNU General Public License for more details.
19363+ *
19364+ * You should have received a copy of the GNU General Public License
523b37e3 19365+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
19366+ */
19367+
19368+/*
19369+ * ioctl
19370+ * plink-management and readdir in userspace.
19371+ * assist the pathconf(3) wrapper library.
c2b27bf2 19372+ * move-down
076b876e 19373+ * File-based Hierarchical Storage Management.
4a4d8108
AM
19374+ */
19375+
c2b27bf2
AM
19376+#include <linux/compat.h>
19377+#include <linux/file.h>
4a4d8108
AM
19378+#include "aufs.h"
19379+
1e00d052 19380+static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
4a4d8108
AM
19381+{
19382+ int err, fd;
5afbbe0d 19383+ aufs_bindex_t wbi, bindex, bbot;
4a4d8108
AM
19384+ struct file *h_file;
19385+ struct super_block *sb;
19386+ struct dentry *root;
1e00d052
AM
19387+ struct au_branch *br;
19388+ struct aufs_wbr_fd wbrfd = {
19389+ .oflags = au_dir_roflags,
19390+ .brid = -1
19391+ };
19392+ const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
19393+ | O_NOATIME | O_CLOEXEC;
4a4d8108 19394+
1e00d052
AM
19395+ AuDebugOn(wbrfd.oflags & ~valid);
19396+
19397+ if (arg) {
19398+ err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
19399+ if (unlikely(err)) {
19400+ err = -EFAULT;
19401+ goto out;
19402+ }
19403+
19404+ err = -EINVAL;
19405+ AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
19406+ wbrfd.oflags |= au_dir_roflags;
19407+ AuDbg("0%o\n", wbrfd.oflags);
19408+ if (unlikely(wbrfd.oflags & ~valid))
19409+ goto out;
19410+ }
19411+
2000de60 19412+ fd = get_unused_fd_flags(0);
1e00d052
AM
19413+ err = fd;
19414+ if (unlikely(fd < 0))
4a4d8108 19415+ goto out;
4a4d8108 19416+
1e00d052 19417+ h_file = ERR_PTR(-EINVAL);
4a4d8108 19418+ wbi = 0;
1e00d052 19419+ br = NULL;
4a4d8108
AM
19420+ sb = path->dentry->d_sb;
19421+ root = sb->s_root;
19422+ aufs_read_lock(root, AuLock_IR);
5afbbe0d 19423+ bbot = au_sbbot(sb);
1e00d052
AM
19424+ if (wbrfd.brid >= 0) {
19425+ wbi = au_br_index(sb, wbrfd.brid);
5afbbe0d 19426+ if (unlikely(wbi < 0 || wbi > bbot))
1e00d052
AM
19427+ goto out_unlock;
19428+ }
19429+
19430+ h_file = ERR_PTR(-ENOENT);
19431+ br = au_sbr(sb, wbi);
19432+ if (!au_br_writable(br->br_perm)) {
19433+ if (arg)
19434+ goto out_unlock;
19435+
19436+ bindex = wbi + 1;
19437+ wbi = -1;
5afbbe0d 19438+ for (; bindex <= bbot; bindex++) {
1e00d052
AM
19439+ br = au_sbr(sb, bindex);
19440+ if (au_br_writable(br->br_perm)) {
4a4d8108 19441+ wbi = bindex;
1e00d052 19442+ br = au_sbr(sb, wbi);
4a4d8108
AM
19443+ break;
19444+ }
19445+ }
4a4d8108
AM
19446+ }
19447+ AuDbg("wbi %d\n", wbi);
1e00d052 19448+ if (wbi >= 0)
392086de
AM
19449+ h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
19450+ /*force_wr*/0);
1e00d052
AM
19451+
19452+out_unlock:
4a4d8108
AM
19453+ aufs_read_unlock(root, AuLock_IR);
19454+ err = PTR_ERR(h_file);
19455+ if (IS_ERR(h_file))
19456+ goto out_fd;
19457+
5afbbe0d 19458+ au_br_put(br); /* cf. au_h_open() */
4a4d8108
AM
19459+ fd_install(fd, h_file);
19460+ err = fd;
19461+ goto out; /* success */
19462+
4f0767ce 19463+out_fd:
4a4d8108 19464+ put_unused_fd(fd);
4f0767ce 19465+out:
1e00d052 19466+ AuTraceErr(err);
4a4d8108
AM
19467+ return err;
19468+}
19469+
19470+/* ---------------------------------------------------------------------- */
19471+
19472+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
19473+{
19474+ long err;
c1595e42 19475+ struct dentry *dentry;
4a4d8108
AM
19476+
19477+ switch (cmd) {
4a4d8108
AM
19478+ case AUFS_CTL_RDU:
19479+ case AUFS_CTL_RDU_INO:
19480+ err = au_rdu_ioctl(file, cmd, arg);
19481+ break;
19482+
19483+ case AUFS_CTL_WBR_FD:
1e00d052 19484+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
19485+ break;
19486+
027c5e7a
AM
19487+ case AUFS_CTL_IBUSY:
19488+ err = au_ibusy_ioctl(file, arg);
19489+ break;
19490+
076b876e
AM
19491+ case AUFS_CTL_BRINFO:
19492+ err = au_brinfo_ioctl(file, arg);
19493+ break;
19494+
19495+ case AUFS_CTL_FHSM_FD:
2000de60 19496+ dentry = file->f_path.dentry;
c1595e42
JR
19497+ if (IS_ROOT(dentry))
19498+ err = au_fhsm_fd(dentry->d_sb, arg);
19499+ else
19500+ err = -ENOTTY;
076b876e
AM
19501+ break;
19502+
4a4d8108
AM
19503+ default:
19504+ /* do not call the lower */
19505+ AuDbg("0x%x\n", cmd);
19506+ err = -ENOTTY;
19507+ }
19508+
19509+ AuTraceErr(err);
19510+ return err;
19511+}
19512+
19513+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
19514+{
19515+ long err;
19516+
19517+ switch (cmd) {
c2b27bf2 19518+ case AUFS_CTL_MVDOWN:
2000de60 19519+ err = au_mvdown(file->f_path.dentry, (void __user *)arg);
c2b27bf2
AM
19520+ break;
19521+
4a4d8108 19522+ case AUFS_CTL_WBR_FD:
1e00d052 19523+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
19524+ break;
19525+
19526+ default:
19527+ /* do not call the lower */
19528+ AuDbg("0x%x\n", cmd);
19529+ err = -ENOTTY;
19530+ }
19531+
19532+ AuTraceErr(err);
19533+ return err;
19534+}
b752ccd1
AM
19535+
19536+#ifdef CONFIG_COMPAT
19537+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
19538+ unsigned long arg)
19539+{
19540+ long err;
19541+
19542+ switch (cmd) {
19543+ case AUFS_CTL_RDU:
19544+ case AUFS_CTL_RDU_INO:
19545+ err = au_rdu_compat_ioctl(file, cmd, arg);
19546+ break;
19547+
027c5e7a
AM
19548+ case AUFS_CTL_IBUSY:
19549+ err = au_ibusy_compat_ioctl(file, arg);
19550+ break;
19551+
076b876e
AM
19552+ case AUFS_CTL_BRINFO:
19553+ err = au_brinfo_compat_ioctl(file, arg);
19554+ break;
19555+
b752ccd1
AM
19556+ default:
19557+ err = aufs_ioctl_dir(file, cmd, arg);
19558+ }
19559+
19560+ AuTraceErr(err);
19561+ return err;
19562+}
19563+
b752ccd1
AM
19564+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
19565+ unsigned long arg)
19566+{
19567+ return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
19568+}
19569+#endif
7f207e10
AM
19570diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
19571--- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947 19572+++ linux/fs/aufs/i_op_add.c 2017-11-12 22:24:42.270843503 +0100
521ced18 19573@@ -0,0 +1,920 @@
4a4d8108 19574+/*
a2654f78 19575+ * Copyright (C) 2005-2017 Junjiro R. Okajima
4a4d8108
AM
19576+ *
19577+ * This program, aufs is free software; you can redistribute it and/or modify
19578+ * it under the terms of the GNU General Public License as published by
19579+ * the Free Software Foundation; either version 2 of the License, or
19580+ * (at your option) any later version.
19581+ *
19582+ * This program is distributed in the hope that it will be useful,
19583+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19584+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19585+ * GNU General Public License for more details.
19586+ *
19587+ * You should have received a copy of the GNU General Public License
523b37e3 19588+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
19589+ */
19590+
19591+/*
19592+ * inode operations (add entry)
19593+ */
19594+
19595+#include "aufs.h"
19596+
19597+/*
19598+ * final procedure of adding a new entry, except link(2).
19599+ * remove whiteout, instantiate, copyup the parent dir's times and size
19600+ * and update version.
19601+ * if it failed, re-create the removed whiteout.
19602+ */
19603+static int epilog(struct inode *dir, aufs_bindex_t bindex,
19604+ struct dentry *wh_dentry, struct dentry *dentry)
19605+{
19606+ int err, rerr;
19607+ aufs_bindex_t bwh;
19608+ struct path h_path;
076b876e 19609+ struct super_block *sb;
4a4d8108
AM
19610+ struct inode *inode, *h_dir;
19611+ struct dentry *wh;
19612+
19613+ bwh = -1;
076b876e 19614+ sb = dir->i_sb;
4a4d8108 19615+ if (wh_dentry) {
5527c038 19616+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
4a4d8108
AM
19617+ IMustLock(h_dir);
19618+ AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
19619+ bwh = au_dbwh(dentry);
19620+ h_path.dentry = wh_dentry;
076b876e 19621+ h_path.mnt = au_sbr_mnt(sb, bindex);
4a4d8108
AM
19622+ err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
19623+ dentry);
19624+ if (unlikely(err))
19625+ goto out;
19626+ }
19627+
19628+ inode = au_new_inode(dentry, /*must_new*/1);
19629+ if (!IS_ERR(inode)) {
19630+ d_instantiate(dentry, inode);
5527c038 19631+ dir = d_inode(dentry->d_parent); /* dir inode is locked */
4a4d8108 19632+ IMustLock(dir);
b912730e 19633+ au_dir_ts(dir, bindex);
4a4d8108 19634+ dir->i_version++;
076b876e 19635+ au_fhsm_wrote(sb, bindex, /*force*/0);
4a4d8108
AM
19636+ return 0; /* success */
19637+ }
19638+
19639+ err = PTR_ERR(inode);
19640+ if (!wh_dentry)
19641+ goto out;
19642+
19643+ /* revert */
19644+ /* dir inode is locked */
19645+ wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
19646+ rerr = PTR_ERR(wh);
19647+ if (IS_ERR(wh)) {
523b37e3
AM
19648+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
19649+ dentry, err, rerr);
4a4d8108
AM
19650+ err = -EIO;
19651+ } else
19652+ dput(wh);
19653+
4f0767ce 19654+out:
4a4d8108
AM
19655+ return err;
19656+}
19657+
027c5e7a
AM
19658+static int au_d_may_add(struct dentry *dentry)
19659+{
19660+ int err;
19661+
19662+ err = 0;
19663+ if (unlikely(d_unhashed(dentry)))
19664+ err = -ENOENT;
5527c038 19665+ if (unlikely(d_really_is_positive(dentry)))
027c5e7a
AM
19666+ err = -EEXIST;
19667+ return err;
19668+}
19669+
4a4d8108
AM
19670+/*
19671+ * simple tests for the adding inode operations.
19672+ * following the checks in vfs, plus the parent-child relationship.
19673+ */
19674+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19675+ struct dentry *h_parent, int isdir)
19676+{
19677+ int err;
19678+ umode_t h_mode;
19679+ struct dentry *h_dentry;
19680+ struct inode *h_inode;
19681+
19682+ err = -ENAMETOOLONG;
19683+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19684+ goto out;
19685+
19686+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 19687+ if (d_really_is_negative(dentry)) {
4a4d8108 19688+ err = -EEXIST;
5527c038 19689+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
19690+ goto out;
19691+ } else {
19692+ /* rename(2) case */
19693+ err = -EIO;
5527c038
JR
19694+ if (unlikely(d_is_negative(h_dentry)))
19695+ goto out;
19696+ h_inode = d_inode(h_dentry);
19697+ if (unlikely(!h_inode->i_nlink))
4a4d8108
AM
19698+ goto out;
19699+
19700+ h_mode = h_inode->i_mode;
19701+ if (!isdir) {
19702+ err = -EISDIR;
19703+ if (unlikely(S_ISDIR(h_mode)))
19704+ goto out;
19705+ } else if (unlikely(!S_ISDIR(h_mode))) {
19706+ err = -ENOTDIR;
19707+ goto out;
19708+ }
19709+ }
19710+
19711+ err = 0;
19712+ /* expected parent dir is locked */
19713+ if (unlikely(h_parent != h_dentry->d_parent))
19714+ err = -EIO;
19715+
4f0767ce 19716+out:
4a4d8108
AM
19717+ AuTraceErr(err);
19718+ return err;
19719+}
19720+
19721+/*
19722+ * initial procedure of adding a new entry.
19723+ * prepare writable branch and the parent dir, lock it,
19724+ * and lookup whiteout for the new entry.
19725+ */
19726+static struct dentry*
19727+lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
19728+ struct dentry *src_dentry, struct au_pin *pin,
19729+ struct au_wr_dir_args *wr_dir_args)
19730+{
19731+ struct dentry *wh_dentry, *h_parent;
19732+ struct super_block *sb;
19733+ struct au_branch *br;
19734+ int err;
19735+ unsigned int udba;
19736+ aufs_bindex_t bcpup;
19737+
523b37e3 19738+ AuDbg("%pd\n", dentry);
4a4d8108
AM
19739+
19740+ err = au_wr_dir(dentry, src_dentry, wr_dir_args);
19741+ bcpup = err;
19742+ wh_dentry = ERR_PTR(err);
19743+ if (unlikely(err < 0))
19744+ goto out;
19745+
19746+ sb = dentry->d_sb;
19747+ udba = au_opt_udba(sb);
19748+ err = au_pin(pin, dentry, bcpup, udba,
19749+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19750+ wh_dentry = ERR_PTR(err);
19751+ if (unlikely(err))
19752+ goto out;
19753+
19754+ h_parent = au_pinned_h_parent(pin);
19755+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 19756+ && au_dbtop(dentry) == bcpup)
4a4d8108
AM
19757+ err = au_may_add(dentry, bcpup, h_parent,
19758+ au_ftest_wrdir(wr_dir_args->flags, ISDIR));
19759+ else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19760+ err = -ENAMETOOLONG;
19761+ wh_dentry = ERR_PTR(err);
19762+ if (unlikely(err))
19763+ goto out_unpin;
19764+
19765+ br = au_sbr(sb, bcpup);
19766+ if (dt) {
19767+ struct path tmp = {
19768+ .dentry = h_parent,
86dc4139 19769+ .mnt = au_br_mnt(br)
4a4d8108
AM
19770+ };
19771+ au_dtime_store(dt, au_pinned_parent(pin), &tmp);
19772+ }
19773+
19774+ wh_dentry = NULL;
19775+ if (bcpup != au_dbwh(dentry))
19776+ goto out; /* success */
19777+
2000de60
JR
19778+ /*
19779+ * ENAMETOOLONG here means that if we allowed create such name, then it
19780+ * would not be able to removed in the future. So we don't allow such
19781+ * name here and we don't handle ENAMETOOLONG differently here.
19782+ */
4a4d8108
AM
19783+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
19784+
4f0767ce 19785+out_unpin:
4a4d8108
AM
19786+ if (IS_ERR(wh_dentry))
19787+ au_unpin(pin);
4f0767ce 19788+out:
4a4d8108
AM
19789+ return wh_dentry;
19790+}
19791+
19792+/* ---------------------------------------------------------------------- */
19793+
19794+enum { Mknod, Symlink, Creat };
19795+struct simple_arg {
19796+ int type;
19797+ union {
19798+ struct {
b912730e
AM
19799+ umode_t mode;
19800+ bool want_excl;
19801+ bool try_aopen;
19802+ struct vfsub_aopen_args *aopen;
4a4d8108
AM
19803+ } c;
19804+ struct {
19805+ const char *symname;
19806+ } s;
19807+ struct {
7eafdf33 19808+ umode_t mode;
4a4d8108
AM
19809+ dev_t dev;
19810+ } m;
19811+ } u;
19812+};
19813+
19814+static int add_simple(struct inode *dir, struct dentry *dentry,
19815+ struct simple_arg *arg)
19816+{
076b876e 19817+ int err, rerr;
5afbbe0d 19818+ aufs_bindex_t btop;
4a4d8108 19819+ unsigned char created;
b912730e
AM
19820+ const unsigned char try_aopen
19821+ = (arg->type == Creat && arg->u.c.try_aopen);
4a4d8108
AM
19822+ struct dentry *wh_dentry, *parent;
19823+ struct inode *h_dir;
b912730e
AM
19824+ struct super_block *sb;
19825+ struct au_branch *br;
c2b27bf2
AM
19826+ /* to reuduce stack size */
19827+ struct {
19828+ struct au_dtime dt;
19829+ struct au_pin pin;
19830+ struct path h_path;
19831+ struct au_wr_dir_args wr_dir_args;
19832+ } *a;
4a4d8108 19833+
523b37e3 19834+ AuDbg("%pd\n", dentry);
4a4d8108
AM
19835+ IMustLock(dir);
19836+
c2b27bf2
AM
19837+ err = -ENOMEM;
19838+ a = kmalloc(sizeof(*a), GFP_NOFS);
19839+ if (unlikely(!a))
19840+ goto out;
19841+ a->wr_dir_args.force_btgt = -1;
19842+ a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
19843+
4a4d8108 19844+ parent = dentry->d_parent; /* dir inode is locked */
b912730e
AM
19845+ if (!try_aopen) {
19846+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
19847+ if (unlikely(err))
19848+ goto out_free;
19849+ }
027c5e7a
AM
19850+ err = au_d_may_add(dentry);
19851+ if (unlikely(err))
19852+ goto out_unlock;
b912730e
AM
19853+ if (!try_aopen)
19854+ di_write_lock_parent(parent);
c2b27bf2
AM
19855+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
19856+ &a->pin, &a->wr_dir_args);
4a4d8108
AM
19857+ err = PTR_ERR(wh_dentry);
19858+ if (IS_ERR(wh_dentry))
027c5e7a 19859+ goto out_parent;
4a4d8108 19860+
5afbbe0d 19861+ btop = au_dbtop(dentry);
b912730e 19862+ sb = dentry->d_sb;
5afbbe0d
AM
19863+ br = au_sbr(sb, btop);
19864+ a->h_path.dentry = au_h_dptr(dentry, btop);
b912730e 19865+ a->h_path.mnt = au_br_mnt(br);
c2b27bf2 19866+ h_dir = au_pinned_h_dir(&a->pin);
4a4d8108
AM
19867+ switch (arg->type) {
19868+ case Creat:
b912730e
AM
19869+ err = 0;
19870+ if (!try_aopen || !h_dir->i_op->atomic_open)
19871+ err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
19872+ arg->u.c.want_excl);
19873+ else
19874+ err = vfsub_atomic_open(h_dir, a->h_path.dentry,
19875+ arg->u.c.aopen, br);
4a4d8108
AM
19876+ break;
19877+ case Symlink:
c2b27bf2 19878+ err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
4a4d8108
AM
19879+ break;
19880+ case Mknod:
c2b27bf2
AM
19881+ err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
19882+ arg->u.m.dev);
4a4d8108
AM
19883+ break;
19884+ default:
19885+ BUG();
19886+ }
19887+ created = !err;
19888+ if (!err)
5afbbe0d 19889+ err = epilog(dir, btop, wh_dentry, dentry);
4a4d8108
AM
19890+
19891+ /* revert */
5527c038 19892+ if (unlikely(created && err && d_is_positive(a->h_path.dentry))) {
523b37e3
AM
19893+ /* no delegation since it is just created */
19894+ rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
19895+ /*force*/0);
4a4d8108 19896+ if (rerr) {
523b37e3
AM
19897+ AuIOErr("%pd revert failure(%d, %d)\n",
19898+ dentry, err, rerr);
4a4d8108
AM
19899+ err = -EIO;
19900+ }
c2b27bf2 19901+ au_dtime_revert(&a->dt);
4a4d8108
AM
19902+ }
19903+
b912730e
AM
19904+ if (!err && try_aopen && !h_dir->i_op->atomic_open)
19905+ *arg->u.c.aopen->opened |= FILE_CREATED;
19906+
c2b27bf2 19907+ au_unpin(&a->pin);
4a4d8108
AM
19908+ dput(wh_dentry);
19909+
027c5e7a 19910+out_parent:
b912730e
AM
19911+ if (!try_aopen)
19912+ di_write_unlock(parent);
027c5e7a 19913+out_unlock:
4a4d8108 19914+ if (unlikely(err)) {
5afbbe0d 19915+ au_update_dbtop(dentry);
4a4d8108
AM
19916+ d_drop(dentry);
19917+ }
b912730e
AM
19918+ if (!try_aopen)
19919+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 19920+out_free:
1c60b727 19921+ kfree(a);
027c5e7a 19922+out:
4a4d8108
AM
19923+ return err;
19924+}
19925+
7eafdf33
AM
19926+int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
19927+ dev_t dev)
4a4d8108
AM
19928+{
19929+ struct simple_arg arg = {
19930+ .type = Mknod,
19931+ .u.m = {
19932+ .mode = mode,
19933+ .dev = dev
19934+ }
19935+ };
19936+ return add_simple(dir, dentry, &arg);
19937+}
19938+
19939+int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
19940+{
19941+ struct simple_arg arg = {
19942+ .type = Symlink,
19943+ .u.s.symname = symname
19944+ };
19945+ return add_simple(dir, dentry, &arg);
19946+}
19947+
7eafdf33 19948+int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
b4510431 19949+ bool want_excl)
4a4d8108
AM
19950+{
19951+ struct simple_arg arg = {
19952+ .type = Creat,
19953+ .u.c = {
b4510431
AM
19954+ .mode = mode,
19955+ .want_excl = want_excl
4a4d8108
AM
19956+ }
19957+ };
19958+ return add_simple(dir, dentry, &arg);
19959+}
19960+
b912730e
AM
19961+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
19962+ struct vfsub_aopen_args *aopen_args)
19963+{
19964+ struct simple_arg arg = {
19965+ .type = Creat,
19966+ .u.c = {
19967+ .mode = aopen_args->create_mode,
19968+ .want_excl = aopen_args->open_flag & O_EXCL,
19969+ .try_aopen = true,
19970+ .aopen = aopen_args
19971+ }
19972+ };
19973+ return add_simple(dir, dentry, &arg);
19974+}
19975+
38d290e6
JR
19976+int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
19977+{
19978+ int err;
19979+ aufs_bindex_t bindex;
19980+ struct super_block *sb;
19981+ struct dentry *parent, *h_parent, *h_dentry;
19982+ struct inode *h_dir, *inode;
19983+ struct vfsmount *h_mnt;
19984+ struct au_wr_dir_args wr_dir_args = {
19985+ .force_btgt = -1,
19986+ .flags = AuWrDir_TMPFILE
19987+ };
19988+
19989+ /* copy-up may happen */
febd17d6 19990+ inode_lock(dir);
38d290e6
JR
19991+
19992+ sb = dir->i_sb;
19993+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19994+ if (unlikely(err))
19995+ goto out;
19996+
19997+ err = au_di_init(dentry);
19998+ if (unlikely(err))
19999+ goto out_si;
20000+
20001+ err = -EBUSY;
20002+ parent = d_find_any_alias(dir);
20003+ AuDebugOn(!parent);
20004+ di_write_lock_parent(parent);
5527c038 20005+ if (unlikely(d_inode(parent) != dir))
38d290e6
JR
20006+ goto out_parent;
20007+
20008+ err = au_digen_test(parent, au_sigen(sb));
20009+ if (unlikely(err))
20010+ goto out_parent;
20011+
5afbbe0d
AM
20012+ bindex = au_dbtop(parent);
20013+ au_set_dbtop(dentry, bindex);
20014+ au_set_dbbot(dentry, bindex);
38d290e6
JR
20015+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
20016+ bindex = err;
20017+ if (unlikely(err < 0))
20018+ goto out_parent;
20019+
20020+ err = -EOPNOTSUPP;
20021+ h_dir = au_h_iptr(dir, bindex);
20022+ if (unlikely(!h_dir->i_op->tmpfile))
20023+ goto out_parent;
20024+
20025+ h_mnt = au_sbr_mnt(sb, bindex);
20026+ err = vfsub_mnt_want_write(h_mnt);
20027+ if (unlikely(err))
20028+ goto out_parent;
20029+
20030+ h_parent = au_h_dptr(parent, bindex);
521ced18
JR
20031+ h_dentry = vfs_tmpfile(h_parent, mode, /*open_flag*/0);
20032+ if (IS_ERR(h_dentry)) {
20033+ err = PTR_ERR(h_dentry);
38d290e6 20034+ goto out_mnt;
521ced18 20035+ }
38d290e6 20036+
5afbbe0d
AM
20037+ au_set_dbtop(dentry, bindex);
20038+ au_set_dbbot(dentry, bindex);
38d290e6
JR
20039+ au_set_h_dptr(dentry, bindex, dget(h_dentry));
20040+ inode = au_new_inode(dentry, /*must_new*/1);
20041+ if (IS_ERR(inode)) {
20042+ err = PTR_ERR(inode);
20043+ au_set_h_dptr(dentry, bindex, NULL);
5afbbe0d
AM
20044+ au_set_dbtop(dentry, -1);
20045+ au_set_dbbot(dentry, -1);
38d290e6
JR
20046+ } else {
20047+ if (!inode->i_nlink)
20048+ set_nlink(inode, 1);
20049+ d_tmpfile(dentry, inode);
20050+ au_di(dentry)->di_tmpfile = 1;
20051+
20052+ /* update without i_mutex */
5afbbe0d 20053+ if (au_ibtop(dir) == au_dbtop(dentry))
38d290e6
JR
20054+ au_cpup_attr_timesizes(dir);
20055+ }
38d290e6 20056+ dput(h_dentry);
521ced18 20057+
38d290e6
JR
20058+out_mnt:
20059+ vfsub_mnt_drop_write(h_mnt);
20060+out_parent:
20061+ di_write_unlock(parent);
20062+ dput(parent);
20063+ di_write_unlock(dentry);
5afbbe0d 20064+ if (unlikely(err)) {
38d290e6
JR
20065+ au_di_fin(dentry);
20066+ dentry->d_fsdata = NULL;
20067+ }
20068+out_si:
20069+ si_read_unlock(sb);
20070+out:
febd17d6 20071+ inode_unlock(dir);
38d290e6
JR
20072+ return err;
20073+}
20074+
4a4d8108
AM
20075+/* ---------------------------------------------------------------------- */
20076+
20077+struct au_link_args {
20078+ aufs_bindex_t bdst, bsrc;
20079+ struct au_pin pin;
20080+ struct path h_path;
20081+ struct dentry *src_parent, *parent;
20082+};
20083+
20084+static int au_cpup_before_link(struct dentry *src_dentry,
20085+ struct au_link_args *a)
20086+{
20087+ int err;
20088+ struct dentry *h_src_dentry;
c2b27bf2
AM
20089+ struct au_cp_generic cpg = {
20090+ .dentry = src_dentry,
20091+ .bdst = a->bdst,
20092+ .bsrc = a->bsrc,
20093+ .len = -1,
20094+ .pin = &a->pin,
20095+ .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
20096+ };
4a4d8108
AM
20097+
20098+ di_read_lock_parent(a->src_parent, AuLock_IR);
20099+ err = au_test_and_cpup_dirs(src_dentry, a->bdst);
20100+ if (unlikely(err))
20101+ goto out;
20102+
20103+ h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
4a4d8108
AM
20104+ err = au_pin(&a->pin, src_dentry, a->bdst,
20105+ au_opt_udba(src_dentry->d_sb),
20106+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20107+ if (unlikely(err))
20108+ goto out;
367653fa 20109+
c2b27bf2 20110+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
20111+ au_unpin(&a->pin);
20112+
4f0767ce 20113+out:
4a4d8108
AM
20114+ di_read_unlock(a->src_parent, AuLock_IR);
20115+ return err;
20116+}
20117+
86dc4139
AM
20118+static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
20119+ struct au_link_args *a)
4a4d8108
AM
20120+{
20121+ int err;
20122+ unsigned char plink;
5afbbe0d 20123+ aufs_bindex_t bbot;
4a4d8108 20124+ struct dentry *h_src_dentry;
523b37e3 20125+ struct inode *h_inode, *inode, *delegated;
4a4d8108
AM
20126+ struct super_block *sb;
20127+ struct file *h_file;
20128+
20129+ plink = 0;
20130+ h_inode = NULL;
20131+ sb = src_dentry->d_sb;
5527c038 20132+ inode = d_inode(src_dentry);
5afbbe0d 20133+ if (au_ibtop(inode) <= a->bdst)
4a4d8108
AM
20134+ h_inode = au_h_iptr(inode, a->bdst);
20135+ if (!h_inode || !h_inode->i_nlink) {
20136+ /* copyup src_dentry as the name of dentry. */
5afbbe0d
AM
20137+ bbot = au_dbbot(dentry);
20138+ if (bbot < a->bsrc)
20139+ au_set_dbbot(dentry, a->bsrc);
86dc4139
AM
20140+ au_set_h_dptr(dentry, a->bsrc,
20141+ dget(au_h_dptr(src_dentry, a->bsrc)));
20142+ dget(a->h_path.dentry);
20143+ au_set_h_dptr(dentry, a->bdst, NULL);
c1595e42
JR
20144+ AuDbg("temporary d_inode...\n");
20145+ spin_lock(&dentry->d_lock);
5527c038 20146+ dentry->d_inode = d_inode(src_dentry); /* tmp */
c1595e42 20147+ spin_unlock(&dentry->d_lock);
392086de 20148+ h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
86dc4139 20149+ if (IS_ERR(h_file))
4a4d8108 20150+ err = PTR_ERR(h_file);
86dc4139 20151+ else {
c2b27bf2
AM
20152+ struct au_cp_generic cpg = {
20153+ .dentry = dentry,
20154+ .bdst = a->bdst,
20155+ .bsrc = -1,
20156+ .len = -1,
20157+ .pin = &a->pin,
20158+ .flags = AuCpup_KEEPLINO
20159+ };
20160+ err = au_sio_cpup_simple(&cpg);
86dc4139
AM
20161+ au_h_open_post(dentry, a->bsrc, h_file);
20162+ if (!err) {
20163+ dput(a->h_path.dentry);
20164+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20165+ } else
20166+ au_set_h_dptr(dentry, a->bdst,
20167+ a->h_path.dentry);
20168+ }
c1595e42 20169+ spin_lock(&dentry->d_lock);
86dc4139 20170+ dentry->d_inode = NULL; /* restore */
c1595e42
JR
20171+ spin_unlock(&dentry->d_lock);
20172+ AuDbg("temporary d_inode...done\n");
86dc4139 20173+ au_set_h_dptr(dentry, a->bsrc, NULL);
5afbbe0d 20174+ au_set_dbbot(dentry, bbot);
4a4d8108
AM
20175+ } else {
20176+ /* the inode of src_dentry already exists on a.bdst branch */
20177+ h_src_dentry = d_find_alias(h_inode);
20178+ if (!h_src_dentry && au_plink_test(inode)) {
20179+ plink = 1;
20180+ h_src_dentry = au_plink_lkup(inode, a->bdst);
20181+ err = PTR_ERR(h_src_dentry);
20182+ if (IS_ERR(h_src_dentry))
20183+ goto out;
20184+
5527c038 20185+ if (unlikely(d_is_negative(h_src_dentry))) {
4a4d8108
AM
20186+ dput(h_src_dentry);
20187+ h_src_dentry = NULL;
20188+ }
20189+
20190+ }
20191+ if (h_src_dentry) {
523b37e3 20192+ delegated = NULL;
4a4d8108 20193+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
20194+ &a->h_path, &delegated);
20195+ if (unlikely(err == -EWOULDBLOCK)) {
20196+ pr_warn("cannot retry for NFSv4 delegation"
20197+ " for an internal link\n");
20198+ iput(delegated);
20199+ }
4a4d8108
AM
20200+ dput(h_src_dentry);
20201+ } else {
20202+ AuIOErr("no dentry found for hi%lu on b%d\n",
20203+ h_inode->i_ino, a->bdst);
20204+ err = -EIO;
20205+ }
20206+ }
20207+
20208+ if (!err && !plink)
20209+ au_plink_append(inode, a->bdst, a->h_path.dentry);
20210+
20211+out:
2cbb1c4b 20212+ AuTraceErr(err);
4a4d8108
AM
20213+ return err;
20214+}
20215+
20216+int aufs_link(struct dentry *src_dentry, struct inode *dir,
20217+ struct dentry *dentry)
20218+{
20219+ int err, rerr;
20220+ struct au_dtime dt;
20221+ struct au_link_args *a;
20222+ struct dentry *wh_dentry, *h_src_dentry;
523b37e3 20223+ struct inode *inode, *delegated;
4a4d8108
AM
20224+ struct super_block *sb;
20225+ struct au_wr_dir_args wr_dir_args = {
20226+ /* .force_btgt = -1, */
20227+ .flags = AuWrDir_ADD_ENTRY
20228+ };
20229+
20230+ IMustLock(dir);
5527c038 20231+ inode = d_inode(src_dentry);
4a4d8108
AM
20232+ IMustLock(inode);
20233+
4a4d8108
AM
20234+ err = -ENOMEM;
20235+ a = kzalloc(sizeof(*a), GFP_NOFS);
20236+ if (unlikely(!a))
20237+ goto out;
20238+
20239+ a->parent = dentry->d_parent; /* dir inode is locked */
027c5e7a
AM
20240+ err = aufs_read_and_write_lock2(dentry, src_dentry,
20241+ AuLock_NOPLM | AuLock_GEN);
e49829fe
JR
20242+ if (unlikely(err))
20243+ goto out_kfree;
38d290e6 20244+ err = au_d_linkable(src_dentry);
027c5e7a
AM
20245+ if (unlikely(err))
20246+ goto out_unlock;
20247+ err = au_d_may_add(dentry);
20248+ if (unlikely(err))
20249+ goto out_unlock;
e49829fe 20250+
4a4d8108 20251+ a->src_parent = dget_parent(src_dentry);
5afbbe0d 20252+ wr_dir_args.force_btgt = au_ibtop(inode);
4a4d8108
AM
20253+
20254+ di_write_lock_parent(a->parent);
20255+ wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
20256+ wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
20257+ &wr_dir_args);
20258+ err = PTR_ERR(wh_dentry);
20259+ if (IS_ERR(wh_dentry))
027c5e7a 20260+ goto out_parent;
4a4d8108
AM
20261+
20262+ err = 0;
20263+ sb = dentry->d_sb;
5afbbe0d 20264+ a->bdst = au_dbtop(dentry);
4a4d8108
AM
20265+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20266+ a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
5afbbe0d 20267+ a->bsrc = au_ibtop(inode);
2cbb1c4b 20268+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
38d290e6
JR
20269+ if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
20270+ h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
2cbb1c4b 20271+ if (!h_src_dentry) {
5afbbe0d 20272+ a->bsrc = au_dbtop(src_dentry);
2cbb1c4b
JR
20273+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20274+ AuDebugOn(!h_src_dentry);
38d290e6
JR
20275+ } else if (IS_ERR(h_src_dentry)) {
20276+ err = PTR_ERR(h_src_dentry);
2cbb1c4b 20277+ goto out_parent;
38d290e6 20278+ }
2cbb1c4b 20279+
f2c43d5f
AM
20280+ /*
20281+ * aufs doesn't touch the credential so
20282+ * security_dentry_create_files_as() is unnecrssary.
20283+ */
4a4d8108
AM
20284+ if (au_opt_test(au_mntflags(sb), PLINK)) {
20285+ if (a->bdst < a->bsrc
20286+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
86dc4139 20287+ err = au_cpup_or_link(src_dentry, dentry, a);
523b37e3
AM
20288+ else {
20289+ delegated = NULL;
4a4d8108 20290+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
20291+ &a->h_path, &delegated);
20292+ if (unlikely(err == -EWOULDBLOCK)) {
20293+ pr_warn("cannot retry for NFSv4 delegation"
20294+ " for an internal link\n");
20295+ iput(delegated);
20296+ }
20297+ }
2cbb1c4b 20298+ dput(h_src_dentry);
4a4d8108
AM
20299+ } else {
20300+ /*
20301+ * copyup src_dentry to the branch we process,
20302+ * and then link(2) to it.
20303+ */
2cbb1c4b 20304+ dput(h_src_dentry);
4a4d8108
AM
20305+ if (a->bdst < a->bsrc
20306+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
20307+ au_unpin(&a->pin);
20308+ di_write_unlock(a->parent);
20309+ err = au_cpup_before_link(src_dentry, a);
20310+ di_write_lock_parent(a->parent);
20311+ if (!err)
20312+ err = au_pin(&a->pin, dentry, a->bdst,
20313+ au_opt_udba(sb),
20314+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20315+ if (unlikely(err))
20316+ goto out_wh;
20317+ }
20318+ if (!err) {
20319+ h_src_dentry = au_h_dptr(src_dentry, a->bdst);
20320+ err = -ENOENT;
5527c038 20321+ if (h_src_dentry && d_is_positive(h_src_dentry)) {
523b37e3 20322+ delegated = NULL;
4a4d8108
AM
20323+ err = vfsub_link(h_src_dentry,
20324+ au_pinned_h_dir(&a->pin),
523b37e3
AM
20325+ &a->h_path, &delegated);
20326+ if (unlikely(err == -EWOULDBLOCK)) {
20327+ pr_warn("cannot retry"
20328+ " for NFSv4 delegation"
20329+ " for an internal link\n");
20330+ iput(delegated);
20331+ }
20332+ }
4a4d8108
AM
20333+ }
20334+ }
20335+ if (unlikely(err))
20336+ goto out_unpin;
20337+
20338+ if (wh_dentry) {
20339+ a->h_path.dentry = wh_dentry;
20340+ err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
20341+ dentry);
20342+ if (unlikely(err))
20343+ goto out_revert;
20344+ }
20345+
b912730e 20346+ au_dir_ts(dir, a->bdst);
4a4d8108 20347+ dir->i_version++;
4a4d8108
AM
20348+ inc_nlink(inode);
20349+ inode->i_ctime = dir->i_ctime;
027c5e7a
AM
20350+ d_instantiate(dentry, au_igrab(inode));
20351+ if (d_unhashed(a->h_path.dentry))
4a4d8108
AM
20352+ /* some filesystem calls d_drop() */
20353+ d_drop(dentry);
076b876e
AM
20354+ /* some filesystems consume an inode even hardlink */
20355+ au_fhsm_wrote(sb, a->bdst, /*force*/0);
4a4d8108
AM
20356+ goto out_unpin; /* success */
20357+
4f0767ce 20358+out_revert:
523b37e3
AM
20359+ /* no delegation since it is just created */
20360+ rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
20361+ /*delegated*/NULL, /*force*/0);
027c5e7a 20362+ if (unlikely(rerr)) {
523b37e3 20363+ AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
027c5e7a
AM
20364+ err = -EIO;
20365+ }
4a4d8108 20366+ au_dtime_revert(&dt);
4f0767ce 20367+out_unpin:
4a4d8108 20368+ au_unpin(&a->pin);
4f0767ce 20369+out_wh:
4a4d8108 20370+ dput(wh_dentry);
027c5e7a
AM
20371+out_parent:
20372+ di_write_unlock(a->parent);
20373+ dput(a->src_parent);
4f0767ce 20374+out_unlock:
4a4d8108 20375+ if (unlikely(err)) {
5afbbe0d 20376+ au_update_dbtop(dentry);
4a4d8108
AM
20377+ d_drop(dentry);
20378+ }
4a4d8108 20379+ aufs_read_and_write_unlock2(dentry, src_dentry);
e49829fe 20380+out_kfree:
1c60b727 20381+ kfree(a);
4f0767ce 20382+out:
86dc4139 20383+ AuTraceErr(err);
4a4d8108
AM
20384+ return err;
20385+}
20386+
7eafdf33 20387+int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
4a4d8108
AM
20388+{
20389+ int err, rerr;
20390+ aufs_bindex_t bindex;
20391+ unsigned char diropq;
20392+ struct path h_path;
20393+ struct dentry *wh_dentry, *parent, *opq_dentry;
febd17d6 20394+ struct inode *h_inode;
4a4d8108
AM
20395+ struct super_block *sb;
20396+ struct {
20397+ struct au_pin pin;
20398+ struct au_dtime dt;
20399+ } *a; /* reduce the stack usage */
20400+ struct au_wr_dir_args wr_dir_args = {
20401+ .force_btgt = -1,
20402+ .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
20403+ };
20404+
20405+ IMustLock(dir);
20406+
20407+ err = -ENOMEM;
20408+ a = kmalloc(sizeof(*a), GFP_NOFS);
20409+ if (unlikely(!a))
20410+ goto out;
20411+
027c5e7a
AM
20412+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20413+ if (unlikely(err))
20414+ goto out_free;
20415+ err = au_d_may_add(dentry);
20416+ if (unlikely(err))
20417+ goto out_unlock;
20418+
4a4d8108
AM
20419+ parent = dentry->d_parent; /* dir inode is locked */
20420+ di_write_lock_parent(parent);
20421+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20422+ &a->pin, &wr_dir_args);
20423+ err = PTR_ERR(wh_dentry);
20424+ if (IS_ERR(wh_dentry))
027c5e7a 20425+ goto out_parent;
4a4d8108
AM
20426+
20427+ sb = dentry->d_sb;
5afbbe0d 20428+ bindex = au_dbtop(dentry);
4a4d8108
AM
20429+ h_path.dentry = au_h_dptr(dentry, bindex);
20430+ h_path.mnt = au_sbr_mnt(sb, bindex);
20431+ err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
20432+ if (unlikely(err))
027c5e7a 20433+ goto out_unpin;
4a4d8108
AM
20434+
20435+ /* make the dir opaque */
20436+ diropq = 0;
febd17d6 20437+ h_inode = d_inode(h_path.dentry);
4a4d8108
AM
20438+ if (wh_dentry
20439+ || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
febd17d6 20440+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 20441+ opq_dentry = au_diropq_create(dentry, bindex);
febd17d6 20442+ inode_unlock(h_inode);
4a4d8108
AM
20443+ err = PTR_ERR(opq_dentry);
20444+ if (IS_ERR(opq_dentry))
20445+ goto out_dir;
20446+ dput(opq_dentry);
20447+ diropq = 1;
20448+ }
20449+
20450+ err = epilog(dir, bindex, wh_dentry, dentry);
20451+ if (!err) {
20452+ inc_nlink(dir);
027c5e7a 20453+ goto out_unpin; /* success */
4a4d8108
AM
20454+ }
20455+
20456+ /* revert */
20457+ if (diropq) {
20458+ AuLabel(revert opq);
febd17d6 20459+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 20460+ rerr = au_diropq_remove(dentry, bindex);
febd17d6 20461+ inode_unlock(h_inode);
4a4d8108 20462+ if (rerr) {
523b37e3
AM
20463+ AuIOErr("%pd reverting diropq failed(%d, %d)\n",
20464+ dentry, err, rerr);
4a4d8108
AM
20465+ err = -EIO;
20466+ }
20467+ }
20468+
4f0767ce 20469+out_dir:
4a4d8108
AM
20470+ AuLabel(revert dir);
20471+ rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
20472+ if (rerr) {
523b37e3
AM
20473+ AuIOErr("%pd reverting dir failed(%d, %d)\n",
20474+ dentry, err, rerr);
4a4d8108
AM
20475+ err = -EIO;
20476+ }
4a4d8108 20477+ au_dtime_revert(&a->dt);
027c5e7a 20478+out_unpin:
4a4d8108
AM
20479+ au_unpin(&a->pin);
20480+ dput(wh_dentry);
027c5e7a
AM
20481+out_parent:
20482+ di_write_unlock(parent);
20483+out_unlock:
4a4d8108 20484+ if (unlikely(err)) {
5afbbe0d 20485+ au_update_dbtop(dentry);
4a4d8108
AM
20486+ d_drop(dentry);
20487+ }
4a4d8108 20488+ aufs_read_unlock(dentry, AuLock_DW);
027c5e7a 20489+out_free:
1c60b727 20490+ kfree(a);
4f0767ce 20491+out:
4a4d8108
AM
20492+ return err;
20493+}
7f207e10
AM
20494diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
20495--- /usr/share/empty/fs/aufs/i_op.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
20496+++ linux/fs/aufs/i_op.c 2017-11-12 22:24:44.707577830 +0100
20497@@ -0,0 +1,1459 @@
4a4d8108 20498+/*
a2654f78 20499+ * Copyright (C) 2005-2017 Junjiro R. Okajima
4a4d8108
AM
20500+ *
20501+ * This program, aufs is free software; you can redistribute it and/or modify
20502+ * it under the terms of the GNU General Public License as published by
20503+ * the Free Software Foundation; either version 2 of the License, or
20504+ * (at your option) any later version.
20505+ *
20506+ * This program is distributed in the hope that it will be useful,
20507+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20508+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20509+ * GNU General Public License for more details.
20510+ *
20511+ * You should have received a copy of the GNU General Public License
523b37e3 20512+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 20513+ */
1facf9fc 20514+
1308ab2a 20515+/*
4a4d8108 20516+ * inode operations (except add/del/rename)
1308ab2a 20517+ */
4a4d8108
AM
20518+
20519+#include <linux/device_cgroup.h>
20520+#include <linux/fs_stack.h>
4a4d8108
AM
20521+#include <linux/namei.h>
20522+#include <linux/security.h>
4a4d8108
AM
20523+#include "aufs.h"
20524+
1e00d052 20525+static int h_permission(struct inode *h_inode, int mask,
79b8bda9 20526+ struct path *h_path, int brperm)
1facf9fc 20527+{
1308ab2a 20528+ int err;
4a4d8108 20529+ const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
1facf9fc 20530+
e2f27e51
AM
20531+ err = -EPERM;
20532+ if (write_mask && IS_IMMUTABLE(h_inode))
20533+ goto out;
20534+
4a4d8108 20535+ err = -EACCES;
e2f27e51
AM
20536+ if (((mask & MAY_EXEC)
20537+ && S_ISREG(h_inode->i_mode)
20538+ && (path_noexec(h_path)
20539+ || !(h_inode->i_mode & S_IXUGO))))
4a4d8108
AM
20540+ goto out;
20541+
20542+ /*
20543+ * - skip the lower fs test in the case of write to ro branch.
20544+ * - nfs dir permission write check is optimized, but a policy for
20545+ * link/rename requires a real check.
b912730e
AM
20546+ * - nfs always sets MS_POSIXACL regardless its mount option 'noacl.'
20547+ * in this case, generic_permission() returns -EOPNOTSUPP.
4a4d8108
AM
20548+ */
20549+ if ((write_mask && !au_br_writable(brperm))
20550+ || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
20551+ && write_mask && !(mask & MAY_READ))
20552+ || !h_inode->i_op->permission) {
20553+ /* AuLabel(generic_permission); */
b912730e 20554+ /* AuDbg("get_acl %pf\n", h_inode->i_op->get_acl); */
1e00d052 20555+ err = generic_permission(h_inode, mask);
b912730e
AM
20556+ if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
20557+ err = h_inode->i_op->permission(h_inode, mask);
20558+ AuTraceErr(err);
1308ab2a 20559+ } else {
4a4d8108 20560+ /* AuLabel(h_inode->permission); */
1e00d052 20561+ err = h_inode->i_op->permission(h_inode, mask);
4a4d8108
AM
20562+ AuTraceErr(err);
20563+ }
1facf9fc 20564+
4a4d8108
AM
20565+ if (!err)
20566+ err = devcgroup_inode_permission(h_inode, mask);
7f207e10 20567+ if (!err)
4a4d8108 20568+ err = security_inode_permission(h_inode, mask);
4a4d8108
AM
20569+
20570+#if 0
20571+ if (!err) {
20572+ /* todo: do we need to call ima_path_check()? */
20573+ struct path h_path = {
20574+ .dentry =
20575+ .mnt = h_mnt
20576+ };
20577+ err = ima_path_check(&h_path,
20578+ mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
20579+ IMA_COUNT_LEAVE);
1308ab2a 20580+ }
4a4d8108 20581+#endif
dece6358 20582+
4f0767ce 20583+out:
1308ab2a 20584+ return err;
20585+}
dece6358 20586+
1e00d052 20587+static int aufs_permission(struct inode *inode, int mask)
1308ab2a 20588+{
20589+ int err;
5afbbe0d 20590+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
20591+ const unsigned char isdir = !!S_ISDIR(inode->i_mode),
20592+ write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20593+ struct inode *h_inode;
20594+ struct super_block *sb;
20595+ struct au_branch *br;
1facf9fc 20596+
027c5e7a 20597+ /* todo: support rcu-walk? */
1e00d052 20598+ if (mask & MAY_NOT_BLOCK)
027c5e7a
AM
20599+ return -ECHILD;
20600+
4a4d8108
AM
20601+ sb = inode->i_sb;
20602+ si_read_lock(sb, AuLock_FLUSH);
20603+ ii_read_lock_child(inode);
027c5e7a
AM
20604+#if 0
20605+ err = au_iigen_test(inode, au_sigen(sb));
20606+ if (unlikely(err))
20607+ goto out;
20608+#endif
dece6358 20609+
076b876e
AM
20610+ if (!isdir
20611+ || write_mask
20612+ || au_opt_test(au_mntflags(sb), DIRPERM1)) {
4a4d8108 20613+ err = au_busy_or_stale();
5afbbe0d 20614+ h_inode = au_h_iptr(inode, au_ibtop(inode));
4a4d8108
AM
20615+ if (unlikely(!h_inode
20616+ || (h_inode->i_mode & S_IFMT)
20617+ != (inode->i_mode & S_IFMT)))
20618+ goto out;
1facf9fc 20619+
4a4d8108 20620+ err = 0;
5afbbe0d 20621+ bindex = au_ibtop(inode);
4a4d8108 20622+ br = au_sbr(sb, bindex);
79b8bda9 20623+ err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
4a4d8108
AM
20624+ if (write_mask
20625+ && !err
20626+ && !special_file(h_inode->i_mode)) {
20627+ /* test whether the upper writable branch exists */
20628+ err = -EROFS;
20629+ for (; bindex >= 0; bindex--)
20630+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
20631+ err = 0;
20632+ break;
20633+ }
20634+ }
20635+ goto out;
20636+ }
dece6358 20637+
4a4d8108 20638+ /* non-write to dir */
1308ab2a 20639+ err = 0;
5afbbe0d
AM
20640+ bbot = au_ibbot(inode);
20641+ for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
20642+ h_inode = au_h_iptr(inode, bindex);
20643+ if (h_inode) {
20644+ err = au_busy_or_stale();
20645+ if (unlikely(!S_ISDIR(h_inode->i_mode)))
20646+ break;
20647+
20648+ br = au_sbr(sb, bindex);
79b8bda9 20649+ err = h_permission(h_inode, mask, &br->br_path,
4a4d8108
AM
20650+ br->br_perm);
20651+ }
20652+ }
1308ab2a 20653+
4f0767ce 20654+out:
4a4d8108
AM
20655+ ii_read_unlock(inode);
20656+ si_read_unlock(sb);
1308ab2a 20657+ return err;
20658+}
20659+
4a4d8108 20660+/* ---------------------------------------------------------------------- */
1facf9fc 20661+
4a4d8108 20662+static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
b4510431 20663+ unsigned int flags)
4a4d8108
AM
20664+{
20665+ struct dentry *ret, *parent;
b752ccd1 20666+ struct inode *inode;
4a4d8108 20667+ struct super_block *sb;
1716fcea 20668+ int err, npositive;
dece6358 20669+
4a4d8108 20670+ IMustLock(dir);
1308ab2a 20671+
537831f9
AM
20672+ /* todo: support rcu-walk? */
20673+ ret = ERR_PTR(-ECHILD);
20674+ if (flags & LOOKUP_RCU)
20675+ goto out;
20676+
20677+ ret = ERR_PTR(-ENAMETOOLONG);
20678+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20679+ goto out;
20680+
4a4d8108 20681+ sb = dir->i_sb;
7f207e10
AM
20682+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20683+ ret = ERR_PTR(err);
20684+ if (unlikely(err))
20685+ goto out;
20686+
4a4d8108
AM
20687+ err = au_di_init(dentry);
20688+ ret = ERR_PTR(err);
20689+ if (unlikely(err))
7f207e10 20690+ goto out_si;
1308ab2a 20691+
9dbd164d 20692+ inode = NULL;
027c5e7a 20693+ npositive = 0; /* suppress a warning */
4a4d8108
AM
20694+ parent = dentry->d_parent; /* dir inode is locked */
20695+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
20696+ err = au_alive_dir(parent);
20697+ if (!err)
20698+ err = au_digen_test(parent, au_sigen(sb));
20699+ if (!err) {
5afbbe0d
AM
20700+ /* regardless LOOKUP_CREATE, always ALLOW_NEG */
20701+ npositive = au_lkup_dentry(dentry, au_dbtop(parent),
20702+ AuLkup_ALLOW_NEG);
027c5e7a
AM
20703+ err = npositive;
20704+ }
4a4d8108 20705+ di_read_unlock(parent, AuLock_IR);
4a4d8108
AM
20706+ ret = ERR_PTR(err);
20707+ if (unlikely(err < 0))
20708+ goto out_unlock;
1308ab2a 20709+
4a4d8108 20710+ if (npositive) {
b752ccd1 20711+ inode = au_new_inode(dentry, /*must_new*/0);
c1595e42
JR
20712+ if (IS_ERR(inode)) {
20713+ ret = (void *)inode;
20714+ inode = NULL;
20715+ goto out_unlock;
20716+ }
9dbd164d 20717+ }
4a4d8108 20718+
c1595e42
JR
20719+ if (inode)
20720+ atomic_inc(&inode->i_count);
4a4d8108 20721+ ret = d_splice_alias(inode, dentry);
537831f9
AM
20722+#if 0
20723+ if (unlikely(d_need_lookup(dentry))) {
20724+ spin_lock(&dentry->d_lock);
20725+ dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
20726+ spin_unlock(&dentry->d_lock);
20727+ } else
20728+#endif
c1595e42 20729+ if (inode) {
2000de60 20730+ if (!IS_ERR(ret)) {
c1595e42 20731+ iput(inode);
2000de60
JR
20732+ if (ret && ret != dentry)
20733+ ii_write_unlock(inode);
20734+ } else {
c1595e42
JR
20735+ ii_write_unlock(inode);
20736+ iput(inode);
20737+ inode = NULL;
20738+ }
7f207e10 20739+ }
1facf9fc 20740+
4f0767ce 20741+out_unlock:
4a4d8108 20742+ di_write_unlock(dentry);
7f207e10 20743+out_si:
4a4d8108 20744+ si_read_unlock(sb);
7f207e10 20745+out:
4a4d8108
AM
20746+ return ret;
20747+}
1facf9fc 20748+
4a4d8108 20749+/* ---------------------------------------------------------------------- */
1facf9fc 20750+
b912730e 20751+struct aopen_node {
8b6a4947 20752+ struct hlist_bl_node hblist;
b912730e
AM
20753+ struct file *file, *h_file;
20754+};
20755+
20756+static int au_do_aopen(struct inode *inode, struct file *file)
20757+{
8b6a4947
AM
20758+ struct hlist_bl_head *aopen;
20759+ struct hlist_bl_node *pos;
b912730e
AM
20760+ struct aopen_node *node;
20761+ struct au_do_open_args args = {
8b6a4947
AM
20762+ .aopen = 1,
20763+ .open = au_do_open_nondir
b912730e
AM
20764+ };
20765+
20766+ aopen = &au_sbi(inode->i_sb)->si_aopen;
8b6a4947
AM
20767+ hlist_bl_lock(aopen);
20768+ hlist_bl_for_each_entry(node, pos, aopen, hblist)
b912730e
AM
20769+ if (node->file == file) {
20770+ args.h_file = node->h_file;
20771+ break;
20772+ }
8b6a4947 20773+ hlist_bl_unlock(aopen);
b912730e
AM
20774+ /* AuDebugOn(!args.h_file); */
20775+
20776+ return au_do_open(file, &args);
20777+}
20778+
20779+static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
20780+ struct file *file, unsigned int open_flag,
20781+ umode_t create_mode, int *opened)
20782+{
8b6a4947 20783+ int err, unlocked, h_opened = *opened;
5afbbe0d 20784+ unsigned int lkup_flags;
f0c0a007 20785+ struct dentry *parent, *d;
8b6a4947 20786+ struct hlist_bl_head *aopen;
b912730e
AM
20787+ struct vfsub_aopen_args args = {
20788+ .open_flag = open_flag,
20789+ .create_mode = create_mode,
20790+ .opened = &h_opened
20791+ };
20792+ struct aopen_node aopen_node = {
20793+ .file = file
20794+ };
20795+
20796+ IMustLock(dir);
5afbbe0d 20797+ AuDbg("open_flag 0%o\n", open_flag);
b912730e
AM
20798+ AuDbgDentry(dentry);
20799+
20800+ err = 0;
20801+ if (!au_di(dentry)) {
5afbbe0d
AM
20802+ lkup_flags = LOOKUP_OPEN;
20803+ if (open_flag & O_CREAT)
20804+ lkup_flags |= LOOKUP_CREATE;
20805+ d = aufs_lookup(dir, dentry, lkup_flags);
b912730e
AM
20806+ if (IS_ERR(d)) {
20807+ err = PTR_ERR(d);
5afbbe0d 20808+ AuTraceErr(err);
b912730e
AM
20809+ goto out;
20810+ } else if (d) {
20811+ /*
20812+ * obsoleted dentry found.
20813+ * another error will be returned later.
20814+ */
20815+ d_drop(d);
b912730e 20816+ AuDbgDentry(d);
5afbbe0d 20817+ dput(d);
b912730e
AM
20818+ }
20819+ AuDbgDentry(dentry);
20820+ }
20821+
20822+ if (d_is_positive(dentry)
20823+ || d_unhashed(dentry)
20824+ || d_unlinked(dentry)
20825+ || !(open_flag & O_CREAT))
20826+ goto out_no_open;
20827+
8b6a4947 20828+ unlocked = 0;
b912730e
AM
20829+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
20830+ if (unlikely(err))
20831+ goto out;
20832+
20833+ parent = dentry->d_parent; /* dir is locked */
20834+ di_write_lock_parent(parent);
5afbbe0d 20835+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
b912730e
AM
20836+ if (unlikely(err))
20837+ goto out_unlock;
20838+
20839+ AuDbgDentry(dentry);
20840+ if (d_is_positive(dentry))
20841+ goto out_unlock;
20842+
20843+ args.file = get_empty_filp();
20844+ err = PTR_ERR(args.file);
20845+ if (IS_ERR(args.file))
20846+ goto out_unlock;
20847+
20848+ args.file->f_flags = file->f_flags;
20849+ err = au_aopen_or_create(dir, dentry, &args);
20850+ AuTraceErr(err);
20851+ AuDbgFile(args.file);
20852+ if (unlikely(err < 0)) {
20853+ if (h_opened & FILE_OPENED)
20854+ fput(args.file);
20855+ else
20856+ put_filp(args.file);
20857+ goto out_unlock;
20858+ }
8b6a4947
AM
20859+ di_write_unlock(parent);
20860+ di_write_unlock(dentry);
20861+ unlocked = 1;
b912730e
AM
20862+
20863+ /* some filesystems don't set FILE_CREATED while succeeded? */
20864+ *opened |= FILE_CREATED;
20865+ if (h_opened & FILE_OPENED)
20866+ aopen_node.h_file = args.file;
20867+ else {
20868+ put_filp(args.file);
20869+ args.file = NULL;
20870+ }
20871+ aopen = &au_sbi(dir->i_sb)->si_aopen;
8b6a4947 20872+ au_hbl_add(&aopen_node.hblist, aopen);
b912730e 20873+ err = finish_open(file, dentry, au_do_aopen, opened);
8b6a4947 20874+ au_hbl_del(&aopen_node.hblist, aopen);
b912730e
AM
20875+ AuTraceErr(err);
20876+ AuDbgFile(file);
20877+ if (aopen_node.h_file)
20878+ fput(aopen_node.h_file);
20879+
20880+out_unlock:
8b6a4947
AM
20881+ if (unlocked)
20882+ si_read_unlock(dentry->d_sb);
20883+ else {
20884+ di_write_unlock(parent);
20885+ aufs_read_unlock(dentry, AuLock_DW);
20886+ }
b912730e 20887+ AuDbgDentry(dentry);
f0c0a007 20888+ if (unlikely(err < 0))
b912730e
AM
20889+ goto out;
20890+out_no_open:
f0c0a007 20891+ if (err >= 0 && !(*opened & FILE_CREATED)) {
b912730e
AM
20892+ AuLabel(out_no_open);
20893+ dget(dentry);
20894+ err = finish_no_open(file, dentry);
20895+ }
20896+out:
20897+ AuDbg("%pd%s%s\n", dentry,
20898+ (*opened & FILE_CREATED) ? " created" : "",
20899+ (*opened & FILE_OPENED) ? " opened" : "");
20900+ AuTraceErr(err);
20901+ return err;
20902+}
20903+
20904+
20905+/* ---------------------------------------------------------------------- */
20906+
4a4d8108
AM
20907+static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
20908+ const unsigned char add_entry, aufs_bindex_t bcpup,
5afbbe0d 20909+ aufs_bindex_t btop)
4a4d8108
AM
20910+{
20911+ int err;
20912+ struct dentry *h_parent;
20913+ struct inode *h_dir;
1facf9fc 20914+
027c5e7a 20915+ if (add_entry)
5527c038 20916+ IMustLock(d_inode(parent));
027c5e7a 20917+ else
4a4d8108
AM
20918+ di_write_lock_parent(parent);
20919+
20920+ err = 0;
20921+ if (!au_h_dptr(parent, bcpup)) {
5afbbe0d 20922+ if (btop > bcpup)
c2b27bf2 20923+ err = au_cpup_dirs(dentry, bcpup);
5afbbe0d 20924+ else if (btop < bcpup)
4a4d8108
AM
20925+ err = au_cpdown_dirs(dentry, bcpup);
20926+ else
c2b27bf2 20927+ BUG();
4a4d8108 20928+ }
38d290e6 20929+ if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
4a4d8108 20930+ h_parent = au_h_dptr(parent, bcpup);
5527c038 20931+ h_dir = d_inode(h_parent);
3c1bdaff 20932+ vfsub_inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
7e9cd9fe 20933+ err = au_lkup_neg(dentry, bcpup, /*wh*/0);
4a4d8108 20934+ /* todo: no unlock here */
3c1bdaff 20935+ inode_unlock_shared(h_dir);
027c5e7a
AM
20936+
20937+ AuDbg("bcpup %d\n", bcpup);
20938+ if (!err) {
5527c038 20939+ if (d_really_is_negative(dentry))
5afbbe0d 20940+ au_set_h_dptr(dentry, btop, NULL);
4a4d8108
AM
20941+ au_update_dbrange(dentry, /*do_put_zero*/0);
20942+ }
1308ab2a 20943+ }
1facf9fc 20944+
4a4d8108
AM
20945+ if (!add_entry)
20946+ di_write_unlock(parent);
20947+ if (!err)
20948+ err = bcpup; /* success */
1308ab2a 20949+
027c5e7a 20950+ AuTraceErr(err);
4a4d8108
AM
20951+ return err;
20952+}
1facf9fc 20953+
4a4d8108
AM
20954+/*
20955+ * decide the branch and the parent dir where we will create a new entry.
20956+ * returns new bindex or an error.
20957+ * copyup the parent dir if needed.
20958+ */
20959+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
20960+ struct au_wr_dir_args *args)
20961+{
20962+ int err;
392086de 20963+ unsigned int flags;
5afbbe0d 20964+ aufs_bindex_t bcpup, btop, src_btop;
86dc4139
AM
20965+ const unsigned char add_entry
20966+ = au_ftest_wrdir(args->flags, ADD_ENTRY)
38d290e6 20967+ | au_ftest_wrdir(args->flags, TMPFILE);
4a4d8108
AM
20968+ struct super_block *sb;
20969+ struct dentry *parent;
20970+ struct au_sbinfo *sbinfo;
1facf9fc 20971+
4a4d8108
AM
20972+ sb = dentry->d_sb;
20973+ sbinfo = au_sbi(sb);
20974+ parent = dget_parent(dentry);
5afbbe0d
AM
20975+ btop = au_dbtop(dentry);
20976+ bcpup = btop;
4a4d8108
AM
20977+ if (args->force_btgt < 0) {
20978+ if (src_dentry) {
5afbbe0d
AM
20979+ src_btop = au_dbtop(src_dentry);
20980+ if (src_btop < btop)
20981+ bcpup = src_btop;
4a4d8108 20982+ } else if (add_entry) {
392086de
AM
20983+ flags = 0;
20984+ if (au_ftest_wrdir(args->flags, ISDIR))
20985+ au_fset_wbr(flags, DIR);
20986+ err = AuWbrCreate(sbinfo, dentry, flags);
4a4d8108
AM
20987+ bcpup = err;
20988+ }
1facf9fc 20989+
5527c038 20990+ if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
4a4d8108
AM
20991+ if (add_entry)
20992+ err = AuWbrCopyup(sbinfo, dentry);
20993+ else {
20994+ if (!IS_ROOT(dentry)) {
20995+ di_read_lock_parent(parent, !AuLock_IR);
20996+ err = AuWbrCopyup(sbinfo, dentry);
20997+ di_read_unlock(parent, !AuLock_IR);
20998+ } else
20999+ err = AuWbrCopyup(sbinfo, dentry);
21000+ }
21001+ bcpup = err;
21002+ if (unlikely(err < 0))
21003+ goto out;
21004+ }
21005+ } else {
21006+ bcpup = args->force_btgt;
5527c038 21007+ AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
1308ab2a 21008+ }
027c5e7a 21009+
5afbbe0d 21010+ AuDbg("btop %d, bcpup %d\n", btop, bcpup);
4a4d8108 21011+ err = bcpup;
5afbbe0d 21012+ if (bcpup == btop)
4a4d8108 21013+ goto out; /* success */
4a4d8108
AM
21014+
21015+ /* copyup the new parent into the branch we process */
5afbbe0d 21016+ err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
027c5e7a 21017+ if (err >= 0) {
5527c038 21018+ if (d_really_is_negative(dentry)) {
5afbbe0d
AM
21019+ au_set_h_dptr(dentry, btop, NULL);
21020+ au_set_dbtop(dentry, bcpup);
21021+ au_set_dbbot(dentry, bcpup);
027c5e7a 21022+ }
38d290e6
JR
21023+ AuDebugOn(add_entry
21024+ && !au_ftest_wrdir(args->flags, TMPFILE)
21025+ && !au_h_dptr(dentry, bcpup));
027c5e7a 21026+ }
86dc4139
AM
21027+
21028+out:
21029+ dput(parent);
21030+ return err;
21031+}
21032+
21033+/* ---------------------------------------------------------------------- */
21034+
21035+void au_pin_hdir_unlock(struct au_pin *p)
21036+{
21037+ if (p->hdir)
5afbbe0d 21038+ au_hn_inode_unlock(p->hdir);
86dc4139
AM
21039+}
21040+
c1595e42 21041+int au_pin_hdir_lock(struct au_pin *p)
86dc4139
AM
21042+{
21043+ int err;
21044+
21045+ err = 0;
21046+ if (!p->hdir)
21047+ goto out;
21048+
21049+ /* even if an error happens later, keep this lock */
5afbbe0d 21050+ au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
86dc4139
AM
21051+
21052+ err = -EBUSY;
5527c038 21053+ if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
86dc4139
AM
21054+ goto out;
21055+
21056+ err = 0;
21057+ if (p->h_dentry)
21058+ err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
21059+ p->h_parent, p->br);
21060+
21061+out:
21062+ return err;
21063+}
21064+
21065+int au_pin_hdir_relock(struct au_pin *p)
21066+{
21067+ int err, i;
21068+ struct inode *h_i;
21069+ struct dentry *h_d[] = {
21070+ p->h_dentry,
21071+ p->h_parent
21072+ };
21073+
21074+ err = au_pin_hdir_lock(p);
21075+ if (unlikely(err))
21076+ goto out;
21077+
21078+ for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
21079+ if (!h_d[i])
21080+ continue;
5527c038
JR
21081+ if (d_is_positive(h_d[i])) {
21082+ h_i = d_inode(h_d[i]);
86dc4139 21083+ err = !h_i->i_nlink;
5527c038 21084+ }
86dc4139
AM
21085+ }
21086+
21087+out:
21088+ return err;
21089+}
21090+
5afbbe0d 21091+static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
86dc4139 21092+{
5afbbe0d
AM
21093+#if !defined(CONFIG_RWSEM_GENERIC_SPINLOCK) && defined(CONFIG_RWSEM_SPIN_ON_OWNER)
21094+ p->hdir->hi_inode->i_rwsem.owner = task;
86dc4139
AM
21095+#endif
21096+}
21097+
21098+void au_pin_hdir_acquire_nest(struct au_pin *p)
21099+{
21100+ if (p->hdir) {
5afbbe0d 21101+ rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
86dc4139
AM
21102+ p->lsc_hi, 0, NULL, _RET_IP_);
21103+ au_pin_hdir_set_owner(p, current);
21104+ }
dece6358 21105+}
1facf9fc 21106+
86dc4139
AM
21107+void au_pin_hdir_release(struct au_pin *p)
21108+{
21109+ if (p->hdir) {
21110+ au_pin_hdir_set_owner(p, p->task);
5afbbe0d 21111+ rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, 1, _RET_IP_);
86dc4139
AM
21112+ }
21113+}
1308ab2a 21114+
4a4d8108 21115+struct dentry *au_pinned_h_parent(struct au_pin *pin)
1308ab2a 21116+{
4a4d8108
AM
21117+ if (pin && pin->parent)
21118+ return au_h_dptr(pin->parent, pin->bindex);
21119+ return NULL;
dece6358 21120+}
1facf9fc 21121+
4a4d8108 21122+void au_unpin(struct au_pin *p)
dece6358 21123+{
86dc4139
AM
21124+ if (p->hdir)
21125+ au_pin_hdir_unlock(p);
e49829fe 21126+ if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
b4510431 21127+ vfsub_mnt_drop_write(p->h_mnt);
4a4d8108
AM
21128+ if (!p->hdir)
21129+ return;
1facf9fc 21130+
4a4d8108
AM
21131+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21132+ di_read_unlock(p->parent, AuLock_IR);
21133+ iput(p->hdir->hi_inode);
21134+ dput(p->parent);
21135+ p->parent = NULL;
21136+ p->hdir = NULL;
21137+ p->h_mnt = NULL;
86dc4139 21138+ /* do not clear p->task */
4a4d8108 21139+}
1308ab2a 21140+
4a4d8108
AM
21141+int au_do_pin(struct au_pin *p)
21142+{
21143+ int err;
21144+ struct super_block *sb;
4a4d8108
AM
21145+ struct inode *h_dir;
21146+
21147+ err = 0;
21148+ sb = p->dentry->d_sb;
86dc4139 21149+ p->br = au_sbr(sb, p->bindex);
4a4d8108
AM
21150+ if (IS_ROOT(p->dentry)) {
21151+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 21152+ p->h_mnt = au_br_mnt(p->br);
b4510431 21153+ err = vfsub_mnt_want_write(p->h_mnt);
4a4d8108
AM
21154+ if (unlikely(err)) {
21155+ au_fclr_pin(p->flags, MNT_WRITE);
21156+ goto out_err;
21157+ }
21158+ }
dece6358 21159+ goto out;
1facf9fc 21160+ }
21161+
86dc4139 21162+ p->h_dentry = NULL;
5afbbe0d 21163+ if (p->bindex <= au_dbbot(p->dentry))
86dc4139 21164+ p->h_dentry = au_h_dptr(p->dentry, p->bindex);
dece6358 21165+
4a4d8108
AM
21166+ p->parent = dget_parent(p->dentry);
21167+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21168+ di_read_lock(p->parent, AuLock_IR, p->lsc_di);
dece6358 21169+
4a4d8108 21170+ h_dir = NULL;
86dc4139 21171+ p->h_parent = au_h_dptr(p->parent, p->bindex);
5527c038 21172+ p->hdir = au_hi(d_inode(p->parent), p->bindex);
4a4d8108
AM
21173+ if (p->hdir)
21174+ h_dir = p->hdir->hi_inode;
dece6358 21175+
b752ccd1
AM
21176+ /*
21177+ * udba case, or
21178+ * if DI_LOCKED is not set, then p->parent may be different
21179+ * and h_parent can be NULL.
21180+ */
86dc4139 21181+ if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
e49829fe 21182+ err = -EBUSY;
4a4d8108
AM
21183+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21184+ di_read_unlock(p->parent, AuLock_IR);
21185+ dput(p->parent);
21186+ p->parent = NULL;
21187+ goto out_err;
21188+ }
1308ab2a 21189+
4a4d8108 21190+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 21191+ p->h_mnt = au_br_mnt(p->br);
b4510431 21192+ err = vfsub_mnt_want_write(p->h_mnt);
dece6358 21193+ if (unlikely(err)) {
4a4d8108 21194+ au_fclr_pin(p->flags, MNT_WRITE);
86dc4139
AM
21195+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21196+ di_read_unlock(p->parent, AuLock_IR);
21197+ dput(p->parent);
21198+ p->parent = NULL;
21199+ goto out_err;
dece6358
AM
21200+ }
21201+ }
4a4d8108 21202+
86dc4139
AM
21203+ au_igrab(h_dir);
21204+ err = au_pin_hdir_lock(p);
21205+ if (!err)
21206+ goto out; /* success */
21207+
076b876e
AM
21208+ au_unpin(p);
21209+
4f0767ce 21210+out_err:
4a4d8108
AM
21211+ pr_err("err %d\n", err);
21212+ err = au_busy_or_stale();
4f0767ce 21213+out:
1facf9fc 21214+ return err;
21215+}
21216+
4a4d8108
AM
21217+void au_pin_init(struct au_pin *p, struct dentry *dentry,
21218+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
21219+ unsigned int udba, unsigned char flags)
21220+{
21221+ p->dentry = dentry;
21222+ p->udba = udba;
21223+ p->lsc_di = lsc_di;
21224+ p->lsc_hi = lsc_hi;
21225+ p->flags = flags;
21226+ p->bindex = bindex;
21227+
21228+ p->parent = NULL;
21229+ p->hdir = NULL;
21230+ p->h_mnt = NULL;
86dc4139
AM
21231+
21232+ p->h_dentry = NULL;
21233+ p->h_parent = NULL;
21234+ p->br = NULL;
21235+ p->task = current;
4a4d8108
AM
21236+}
21237+
21238+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
21239+ unsigned int udba, unsigned char flags)
21240+{
21241+ au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
21242+ udba, flags);
21243+ return au_do_pin(pin);
21244+}
21245+
dece6358
AM
21246+/* ---------------------------------------------------------------------- */
21247+
1308ab2a 21248+/*
4a4d8108
AM
21249+ * ->setattr() and ->getattr() are called in various cases.
21250+ * chmod, stat: dentry is revalidated.
21251+ * fchmod, fstat: file and dentry are not revalidated, additionally they may be
21252+ * unhashed.
21253+ * for ->setattr(), ia->ia_file is passed from ftruncate only.
1308ab2a 21254+ */
027c5e7a 21255+/* todo: consolidate with do_refresh() and simple_reval_dpath() */
c1595e42 21256+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
1facf9fc 21257+{
4a4d8108 21258+ int err;
4a4d8108 21259+ struct dentry *parent;
1facf9fc 21260+
1308ab2a 21261+ err = 0;
027c5e7a 21262+ if (au_digen_test(dentry, sigen)) {
4a4d8108
AM
21263+ parent = dget_parent(dentry);
21264+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 21265+ err = au_refresh_dentry(dentry, parent);
4a4d8108
AM
21266+ di_read_unlock(parent, AuLock_IR);
21267+ dput(parent);
dece6358 21268+ }
1facf9fc 21269+
4a4d8108 21270+ AuTraceErr(err);
1308ab2a 21271+ return err;
21272+}
dece6358 21273+
c1595e42
JR
21274+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
21275+ struct au_icpup_args *a)
1308ab2a 21276+{
21277+ int err;
4a4d8108 21278+ loff_t sz;
5afbbe0d 21279+ aufs_bindex_t btop, ibtop;
4a4d8108
AM
21280+ struct dentry *hi_wh, *parent;
21281+ struct inode *inode;
4a4d8108
AM
21282+ struct au_wr_dir_args wr_dir_args = {
21283+ .force_btgt = -1,
21284+ .flags = 0
21285+ };
21286+
2000de60 21287+ if (d_is_dir(dentry))
4a4d8108
AM
21288+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
21289+ /* plink or hi_wh() case */
5afbbe0d 21290+ btop = au_dbtop(dentry);
5527c038 21291+ inode = d_inode(dentry);
5afbbe0d
AM
21292+ ibtop = au_ibtop(inode);
21293+ if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
21294+ wr_dir_args.force_btgt = ibtop;
4a4d8108
AM
21295+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21296+ if (unlikely(err < 0))
21297+ goto out;
21298+ a->btgt = err;
5afbbe0d 21299+ if (err != btop)
4a4d8108
AM
21300+ au_fset_icpup(a->flags, DID_CPUP);
21301+
21302+ err = 0;
21303+ a->pin_flags = AuPin_MNT_WRITE;
21304+ parent = NULL;
21305+ if (!IS_ROOT(dentry)) {
21306+ au_fset_pin(a->pin_flags, DI_LOCKED);
21307+ parent = dget_parent(dentry);
21308+ di_write_lock_parent(parent);
21309+ }
21310+
21311+ err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
21312+ if (unlikely(err))
21313+ goto out_parent;
21314+
4a4d8108 21315+ sz = -1;
5afbbe0d 21316+ a->h_path.dentry = au_h_dptr(dentry, btop);
5527c038 21317+ a->h_inode = d_inode(a->h_path.dentry);
c1595e42 21318+ if (ia && (ia->ia_valid & ATTR_SIZE)) {
3c1bdaff 21319+ vfsub_inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
c1595e42
JR
21320+ if (ia->ia_size < i_size_read(a->h_inode))
21321+ sz = ia->ia_size;
3c1bdaff 21322+ inode_unlock_shared(a->h_inode);
c1595e42 21323+ }
4a4d8108 21324+
4a4d8108 21325+ hi_wh = NULL;
027c5e7a 21326+ if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
4a4d8108
AM
21327+ hi_wh = au_hi_wh(inode, a->btgt);
21328+ if (!hi_wh) {
c2b27bf2
AM
21329+ struct au_cp_generic cpg = {
21330+ .dentry = dentry,
21331+ .bdst = a->btgt,
21332+ .bsrc = -1,
21333+ .len = sz,
21334+ .pin = &a->pin
21335+ };
21336+ err = au_sio_cpup_wh(&cpg, /*file*/NULL);
4a4d8108
AM
21337+ if (unlikely(err))
21338+ goto out_unlock;
21339+ hi_wh = au_hi_wh(inode, a->btgt);
21340+ /* todo: revalidate hi_wh? */
21341+ }
21342+ }
21343+
21344+ if (parent) {
21345+ au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
21346+ di_downgrade_lock(parent, AuLock_IR);
21347+ dput(parent);
21348+ parent = NULL;
21349+ }
21350+ if (!au_ftest_icpup(a->flags, DID_CPUP))
21351+ goto out; /* success */
21352+
21353+ if (!d_unhashed(dentry)) {
c2b27bf2
AM
21354+ struct au_cp_generic cpg = {
21355+ .dentry = dentry,
21356+ .bdst = a->btgt,
5afbbe0d 21357+ .bsrc = btop,
c2b27bf2
AM
21358+ .len = sz,
21359+ .pin = &a->pin,
21360+ .flags = AuCpup_DTIME | AuCpup_HOPEN
21361+ };
21362+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
21363+ if (!err)
21364+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21365+ } else if (!hi_wh)
21366+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21367+ else
21368+ a->h_path.dentry = hi_wh; /* do not dget here */
1308ab2a 21369+
4f0767ce 21370+out_unlock:
5527c038 21371+ a->h_inode = d_inode(a->h_path.dentry);
86dc4139 21372+ if (!err)
dece6358 21373+ goto out; /* success */
4a4d8108 21374+ au_unpin(&a->pin);
4f0767ce 21375+out_parent:
4a4d8108
AM
21376+ if (parent) {
21377+ di_write_unlock(parent);
21378+ dput(parent);
21379+ }
4f0767ce 21380+out:
86dc4139 21381+ if (!err)
febd17d6 21382+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
1facf9fc 21383+ return err;
21384+}
21385+
4a4d8108 21386+static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
1facf9fc 21387+{
4a4d8108 21388+ int err;
523b37e3 21389+ struct inode *inode, *delegated;
4a4d8108
AM
21390+ struct super_block *sb;
21391+ struct file *file;
21392+ struct au_icpup_args *a;
1facf9fc 21393+
5527c038 21394+ inode = d_inode(dentry);
4a4d8108 21395+ IMustLock(inode);
dece6358 21396+
f2c43d5f
AM
21397+ err = setattr_prepare(dentry, ia);
21398+ if (unlikely(err))
21399+ goto out;
21400+
4a4d8108
AM
21401+ err = -ENOMEM;
21402+ a = kzalloc(sizeof(*a), GFP_NOFS);
21403+ if (unlikely(!a))
21404+ goto out;
1facf9fc 21405+
4a4d8108
AM
21406+ if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
21407+ ia->ia_valid &= ~ATTR_MODE;
dece6358 21408+
4a4d8108
AM
21409+ file = NULL;
21410+ sb = dentry->d_sb;
e49829fe
JR
21411+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21412+ if (unlikely(err))
21413+ goto out_kfree;
21414+
4a4d8108
AM
21415+ if (ia->ia_valid & ATTR_FILE) {
21416+ /* currently ftruncate(2) only */
7e9cd9fe 21417+ AuDebugOn(!d_is_reg(dentry));
4a4d8108 21418+ file = ia->ia_file;
521ced18
JR
21419+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
21420+ /*fi_lsc*/0);
4a4d8108
AM
21421+ if (unlikely(err))
21422+ goto out_si;
21423+ ia->ia_file = au_hf_top(file);
21424+ a->udba = AuOpt_UDBA_NONE;
21425+ } else {
21426+ /* fchmod() doesn't pass ia_file */
21427+ a->udba = au_opt_udba(sb);
027c5e7a
AM
21428+ di_write_lock_child(dentry);
21429+ /* no d_unlinked(), to set UDBA_NONE for root */
4a4d8108
AM
21430+ if (d_unhashed(dentry))
21431+ a->udba = AuOpt_UDBA_NONE;
4a4d8108
AM
21432+ if (a->udba != AuOpt_UDBA_NONE) {
21433+ AuDebugOn(IS_ROOT(dentry));
21434+ err = au_reval_for_attr(dentry, au_sigen(sb));
21435+ if (unlikely(err))
21436+ goto out_dentry;
21437+ }
dece6358 21438+ }
dece6358 21439+
4a4d8108
AM
21440+ err = au_pin_and_icpup(dentry, ia, a);
21441+ if (unlikely(err < 0))
21442+ goto out_dentry;
21443+ if (au_ftest_icpup(a->flags, DID_CPUP)) {
21444+ ia->ia_file = NULL;
21445+ ia->ia_valid &= ~ATTR_FILE;
1308ab2a 21446+ }
dece6358 21447+
4a4d8108
AM
21448+ a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
21449+ if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
21450+ == (ATTR_MODE | ATTR_CTIME)) {
7eafdf33 21451+ err = security_path_chmod(&a->h_path, ia->ia_mode);
4a4d8108
AM
21452+ if (unlikely(err))
21453+ goto out_unlock;
21454+ } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
21455+ && (ia->ia_valid & ATTR_CTIME)) {
86dc4139 21456+ err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
4a4d8108
AM
21457+ if (unlikely(err))
21458+ goto out_unlock;
21459+ }
dece6358 21460+
4a4d8108
AM
21461+ if (ia->ia_valid & ATTR_SIZE) {
21462+ struct file *f;
1308ab2a 21463+
953406b4 21464+ if (ia->ia_size < i_size_read(inode))
4a4d8108 21465+ /* unmap only */
953406b4 21466+ truncate_setsize(inode, ia->ia_size);
1308ab2a 21467+
4a4d8108
AM
21468+ f = NULL;
21469+ if (ia->ia_valid & ATTR_FILE)
21470+ f = ia->ia_file;
febd17d6 21471+ inode_unlock(a->h_inode);
4a4d8108 21472+ err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
febd17d6 21473+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
523b37e3
AM
21474+ } else {
21475+ delegated = NULL;
21476+ while (1) {
21477+ err = vfsub_notify_change(&a->h_path, ia, &delegated);
21478+ if (delegated) {
21479+ err = break_deleg_wait(&delegated);
21480+ if (!err)
21481+ continue;
21482+ }
21483+ break;
21484+ }
21485+ }
8cdd5066
JR
21486+ /*
21487+ * regardless aufs 'acl' option setting.
21488+ * why don't all acl-aware fs call this func from their ->setattr()?
21489+ */
21490+ if (!err && (ia->ia_valid & ATTR_MODE))
21491+ err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
4a4d8108
AM
21492+ if (!err)
21493+ au_cpup_attr_changeable(inode);
1308ab2a 21494+
4f0767ce 21495+out_unlock:
febd17d6 21496+ inode_unlock(a->h_inode);
4a4d8108 21497+ au_unpin(&a->pin);
027c5e7a 21498+ if (unlikely(err))
5afbbe0d 21499+ au_update_dbtop(dentry);
4f0767ce 21500+out_dentry:
4a4d8108
AM
21501+ di_write_unlock(dentry);
21502+ if (file) {
21503+ fi_write_unlock(file);
21504+ ia->ia_file = file;
21505+ ia->ia_valid |= ATTR_FILE;
21506+ }
4f0767ce 21507+out_si:
4a4d8108 21508+ si_read_unlock(sb);
e49829fe 21509+out_kfree:
1c60b727 21510+ kfree(a);
4f0767ce 21511+out:
4a4d8108
AM
21512+ AuTraceErr(err);
21513+ return err;
1facf9fc 21514+}
21515+
c1595e42
JR
21516+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
21517+static int au_h_path_to_set_attr(struct dentry *dentry,
21518+ struct au_icpup_args *a, struct path *h_path)
21519+{
21520+ int err;
21521+ struct super_block *sb;
21522+
21523+ sb = dentry->d_sb;
21524+ a->udba = au_opt_udba(sb);
21525+ /* no d_unlinked(), to set UDBA_NONE for root */
21526+ if (d_unhashed(dentry))
21527+ a->udba = AuOpt_UDBA_NONE;
21528+ if (a->udba != AuOpt_UDBA_NONE) {
21529+ AuDebugOn(IS_ROOT(dentry));
21530+ err = au_reval_for_attr(dentry, au_sigen(sb));
21531+ if (unlikely(err))
21532+ goto out;
21533+ }
21534+ err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
21535+ if (unlikely(err < 0))
21536+ goto out;
21537+
21538+ h_path->dentry = a->h_path.dentry;
21539+ h_path->mnt = au_sbr_mnt(sb, a->btgt);
21540+
21541+out:
21542+ return err;
21543+}
21544+
f2c43d5f
AM
21545+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
21546+ struct au_sxattr *arg)
c1595e42
JR
21547+{
21548+ int err;
21549+ struct path h_path;
21550+ struct super_block *sb;
21551+ struct au_icpup_args *a;
5afbbe0d 21552+ struct inode *h_inode;
c1595e42 21553+
c1595e42
JR
21554+ IMustLock(inode);
21555+
21556+ err = -ENOMEM;
21557+ a = kzalloc(sizeof(*a), GFP_NOFS);
21558+ if (unlikely(!a))
21559+ goto out;
21560+
21561+ sb = dentry->d_sb;
21562+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21563+ if (unlikely(err))
21564+ goto out_kfree;
21565+
21566+ h_path.dentry = NULL; /* silence gcc */
21567+ di_write_lock_child(dentry);
21568+ err = au_h_path_to_set_attr(dentry, a, &h_path);
21569+ if (unlikely(err))
21570+ goto out_di;
21571+
febd17d6 21572+ inode_unlock(a->h_inode);
c1595e42
JR
21573+ switch (arg->type) {
21574+ case AU_XATTR_SET:
5afbbe0d 21575+ AuDebugOn(d_is_negative(h_path.dentry));
c1595e42
JR
21576+ err = vfsub_setxattr(h_path.dentry,
21577+ arg->u.set.name, arg->u.set.value,
21578+ arg->u.set.size, arg->u.set.flags);
21579+ break;
c1595e42
JR
21580+ case AU_ACL_SET:
21581+ err = -EOPNOTSUPP;
5527c038 21582+ h_inode = d_inode(h_path.dentry);
c1595e42 21583+ if (h_inode->i_op->set_acl)
f2c43d5f 21584+ /* this will call posix_acl_update_mode */
c1595e42
JR
21585+ err = h_inode->i_op->set_acl(h_inode,
21586+ arg->u.acl_set.acl,
21587+ arg->u.acl_set.type);
21588+ break;
21589+ }
21590+ if (!err)
21591+ au_cpup_attr_timesizes(inode);
21592+
21593+ au_unpin(&a->pin);
21594+ if (unlikely(err))
5afbbe0d 21595+ au_update_dbtop(dentry);
c1595e42
JR
21596+
21597+out_di:
21598+ di_write_unlock(dentry);
21599+ si_read_unlock(sb);
21600+out_kfree:
1c60b727 21601+ kfree(a);
c1595e42
JR
21602+out:
21603+ AuTraceErr(err);
21604+ return err;
21605+}
21606+#endif
21607+
4a4d8108
AM
21608+static void au_refresh_iattr(struct inode *inode, struct kstat *st,
21609+ unsigned int nlink)
1facf9fc 21610+{
9dbd164d
AM
21611+ unsigned int n;
21612+
4a4d8108 21613+ inode->i_mode = st->mode;
86dc4139
AM
21614+ /* don't i_[ug]id_write() here */
21615+ inode->i_uid = st->uid;
21616+ inode->i_gid = st->gid;
4a4d8108
AM
21617+ inode->i_atime = st->atime;
21618+ inode->i_mtime = st->mtime;
21619+ inode->i_ctime = st->ctime;
1facf9fc 21620+
4a4d8108
AM
21621+ au_cpup_attr_nlink(inode, /*force*/0);
21622+ if (S_ISDIR(inode->i_mode)) {
9dbd164d
AM
21623+ n = inode->i_nlink;
21624+ n -= nlink;
21625+ n += st->nlink;
f6b6e03d 21626+ smp_mb(); /* for i_nlink */
7eafdf33 21627+ /* 0 can happen */
92d182d2 21628+ set_nlink(inode, n);
4a4d8108 21629+ }
1facf9fc 21630+
4a4d8108
AM
21631+ spin_lock(&inode->i_lock);
21632+ inode->i_blocks = st->blocks;
21633+ i_size_write(inode, st->size);
21634+ spin_unlock(&inode->i_lock);
1facf9fc 21635+}
21636+
c1595e42 21637+/*
f2c43d5f 21638+ * common routine for aufs_getattr() and au_getxattr().
c1595e42
JR
21639+ * returns zero or negative (an error).
21640+ * @dentry will be read-locked in success.
21641+ */
a2654f78
AM
21642+int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
21643+ int locked)
1facf9fc 21644+{
4a4d8108 21645+ int err;
076b876e 21646+ unsigned int mnt_flags, sigen;
c1595e42 21647+ unsigned char udba_none;
4a4d8108 21648+ aufs_bindex_t bindex;
4a4d8108
AM
21649+ struct super_block *sb, *h_sb;
21650+ struct inode *inode;
1facf9fc 21651+
c1595e42
JR
21652+ h_path->mnt = NULL;
21653+ h_path->dentry = NULL;
21654+
21655+ err = 0;
4a4d8108 21656+ sb = dentry->d_sb;
4a4d8108
AM
21657+ mnt_flags = au_mntflags(sb);
21658+ udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
1facf9fc 21659+
a2654f78
AM
21660+ if (unlikely(locked))
21661+ goto body; /* skip locking dinfo */
21662+
4a4d8108 21663+ /* support fstat(2) */
027c5e7a 21664+ if (!d_unlinked(dentry) && !udba_none) {
076b876e 21665+ sigen = au_sigen(sb);
027c5e7a
AM
21666+ err = au_digen_test(dentry, sigen);
21667+ if (!err) {
4a4d8108 21668+ di_read_lock_child(dentry, AuLock_IR);
027c5e7a 21669+ err = au_dbrange_test(dentry);
c1595e42
JR
21670+ if (unlikely(err)) {
21671+ di_read_unlock(dentry, AuLock_IR);
21672+ goto out;
21673+ }
027c5e7a 21674+ } else {
4a4d8108
AM
21675+ AuDebugOn(IS_ROOT(dentry));
21676+ di_write_lock_child(dentry);
027c5e7a
AM
21677+ err = au_dbrange_test(dentry);
21678+ if (!err)
21679+ err = au_reval_for_attr(dentry, sigen);
c1595e42
JR
21680+ if (!err)
21681+ di_downgrade_lock(dentry, AuLock_IR);
21682+ else {
21683+ di_write_unlock(dentry);
21684+ goto out;
21685+ }
4a4d8108
AM
21686+ }
21687+ } else
21688+ di_read_lock_child(dentry, AuLock_IR);
1facf9fc 21689+
a2654f78 21690+body:
5527c038 21691+ inode = d_inode(dentry);
5afbbe0d 21692+ bindex = au_ibtop(inode);
c1595e42
JR
21693+ h_path->mnt = au_sbr_mnt(sb, bindex);
21694+ h_sb = h_path->mnt->mnt_sb;
21695+ if (!force
21696+ && !au_test_fs_bad_iattr(h_sb)
21697+ && udba_none)
21698+ goto out; /* success */
1facf9fc 21699+
5afbbe0d 21700+ if (au_dbtop(dentry) == bindex)
c1595e42 21701+ h_path->dentry = au_h_dptr(dentry, bindex);
4a4d8108 21702+ else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
c1595e42
JR
21703+ h_path->dentry = au_plink_lkup(inode, bindex);
21704+ if (IS_ERR(h_path->dentry))
21705+ /* pretending success */
21706+ h_path->dentry = NULL;
21707+ else
21708+ dput(h_path->dentry);
4a4d8108 21709+ }
c1595e42
JR
21710+
21711+out:
21712+ return err;
21713+}
21714+
521ced18
JR
21715+static int aufs_getattr(const struct path *path, struct kstat *st,
21716+ u32 request, unsigned int query)
c1595e42
JR
21717+{
21718+ int err;
21719+ unsigned char positive;
21720+ struct path h_path;
521ced18 21721+ struct dentry *dentry;
c1595e42
JR
21722+ struct inode *inode;
21723+ struct super_block *sb;
21724+
521ced18 21725+ dentry = path->dentry;
5527c038 21726+ inode = d_inode(dentry);
c1595e42
JR
21727+ sb = dentry->d_sb;
21728+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21729+ if (unlikely(err))
21730+ goto out;
a2654f78 21731+ err = au_h_path_getattr(dentry, /*force*/0, &h_path, /*locked*/0);
c1595e42
JR
21732+ if (unlikely(err))
21733+ goto out_si;
c06a8ce3 21734+ if (unlikely(!h_path.dentry))
c1595e42 21735+ /* illegally overlapped or something */
4a4d8108
AM
21736+ goto out_fill; /* pretending success */
21737+
5527c038 21738+ positive = d_is_positive(h_path.dentry);
4a4d8108 21739+ if (positive)
521ced18
JR
21740+ /* no vfsub version */
21741+ err = vfs_getattr(&h_path, st, request, query);
4a4d8108
AM
21742+ if (!err) {
21743+ if (positive)
c06a8ce3 21744+ au_refresh_iattr(inode, st,
5527c038 21745+ d_inode(h_path.dentry)->i_nlink);
4a4d8108 21746+ goto out_fill; /* success */
1facf9fc 21747+ }
7f207e10 21748+ AuTraceErr(err);
c1595e42 21749+ goto out_di;
4a4d8108 21750+
4f0767ce 21751+out_fill:
4a4d8108 21752+ generic_fillattr(inode, st);
c1595e42 21753+out_di:
4a4d8108 21754+ di_read_unlock(dentry, AuLock_IR);
c1595e42 21755+out_si:
4a4d8108 21756+ si_read_unlock(sb);
7f207e10
AM
21757+out:
21758+ AuTraceErr(err);
4a4d8108 21759+ return err;
1facf9fc 21760+}
21761+
21762+/* ---------------------------------------------------------------------- */
21763+
febd17d6
JR
21764+static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
21765+ struct delayed_call *done)
4a4d8108 21766+{
c2c0f25c 21767+ const char *ret;
c2c0f25c 21768+ struct dentry *h_dentry;
febd17d6 21769+ struct inode *h_inode;
4a4d8108 21770+ int err;
c2c0f25c 21771+ aufs_bindex_t bindex;
1facf9fc 21772+
79b8bda9 21773+ ret = NULL; /* suppress a warning */
febd17d6
JR
21774+ err = -ECHILD;
21775+ if (!dentry)
21776+ goto out;
21777+
027c5e7a
AM
21778+ err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
21779+ if (unlikely(err))
c2c0f25c 21780+ goto out;
027c5e7a
AM
21781+
21782+ err = au_d_hashed_positive(dentry);
c2c0f25c
AM
21783+ if (unlikely(err))
21784+ goto out_unlock;
21785+
21786+ err = -EINVAL;
21787+ inode = d_inode(dentry);
5afbbe0d 21788+ bindex = au_ibtop(inode);
c2c0f25c 21789+ h_inode = au_h_iptr(inode, bindex);
febd17d6 21790+ if (unlikely(!h_inode->i_op->get_link))
c2c0f25c
AM
21791+ goto out_unlock;
21792+
21793+ err = -EBUSY;
21794+ h_dentry = NULL;
5afbbe0d 21795+ if (au_dbtop(dentry) <= bindex) {
c2c0f25c
AM
21796+ h_dentry = au_h_dptr(dentry, bindex);
21797+ if (h_dentry)
21798+ dget(h_dentry);
027c5e7a 21799+ }
c2c0f25c
AM
21800+ if (!h_dentry) {
21801+ h_dentry = d_find_any_alias(h_inode);
21802+ if (IS_ERR(h_dentry)) {
21803+ err = PTR_ERR(h_dentry);
febd17d6 21804+ goto out_unlock;
c2c0f25c
AM
21805+ }
21806+ }
21807+ if (unlikely(!h_dentry))
febd17d6 21808+ goto out_unlock;
1facf9fc 21809+
c2c0f25c 21810+ err = 0;
febd17d6 21811+ AuDbg("%pf\n", h_inode->i_op->get_link);
c2c0f25c 21812+ AuDbgDentry(h_dentry);
f2c43d5f 21813+ ret = vfs_get_link(h_dentry, done);
c2c0f25c 21814+ dput(h_dentry);
febd17d6
JR
21815+ if (IS_ERR(ret))
21816+ err = PTR_ERR(ret);
c2c0f25c 21817+
c2c0f25c
AM
21818+out_unlock:
21819+ aufs_read_unlock(dentry, AuLock_IR);
4f0767ce 21820+out:
c2c0f25c
AM
21821+ if (unlikely(err))
21822+ ret = ERR_PTR(err);
21823+ AuTraceErrPtr(ret);
21824+ return ret;
4a4d8108 21825+}
1facf9fc 21826+
4a4d8108 21827+/* ---------------------------------------------------------------------- */
1facf9fc 21828+
e2f27e51
AM
21829+static int au_is_special(struct inode *inode)
21830+{
21831+ return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
21832+}
21833+
0c3ec466 21834+static int aufs_update_time(struct inode *inode, struct timespec *ts, int flags)
4a4d8108 21835+{
0c3ec466 21836+ int err;
e2f27e51 21837+ aufs_bindex_t bindex;
0c3ec466
AM
21838+ struct super_block *sb;
21839+ struct inode *h_inode;
e2f27e51 21840+ struct vfsmount *h_mnt;
0c3ec466
AM
21841+
21842+ sb = inode->i_sb;
e2f27e51
AM
21843+ WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
21844+ "unexpected s_flags 0x%lx", sb->s_flags);
21845+
0c3ec466
AM
21846+ /* mmap_sem might be acquired already, cf. aufs_mmap() */
21847+ lockdep_off();
21848+ si_read_lock(sb, AuLock_FLUSH);
21849+ ii_write_lock_child(inode);
e2f27e51
AM
21850+
21851+ err = 0;
21852+ bindex = au_ibtop(inode);
21853+ h_inode = au_h_iptr(inode, bindex);
21854+ if (!au_test_ro(sb, bindex, inode)) {
21855+ h_mnt = au_sbr_mnt(sb, bindex);
21856+ err = vfsub_mnt_want_write(h_mnt);
21857+ if (!err) {
21858+ err = vfsub_update_time(h_inode, ts, flags);
21859+ vfsub_mnt_drop_write(h_mnt);
21860+ }
21861+ } else if (au_is_special(h_inode)) {
21862+ /*
21863+ * Never copy-up here.
21864+ * These special files may already be opened and used for
21865+ * communicating. If we copied it up, then the communication
21866+ * would be corrupted.
21867+ */
21868+ AuWarn1("timestamps for i%lu are ignored "
21869+ "since it is on readonly branch (hi%lu).\n",
21870+ inode->i_ino, h_inode->i_ino);
21871+ } else if (flags & ~S_ATIME) {
21872+ err = -EIO;
21873+ AuIOErr1("unexpected flags 0x%x\n", flags);
21874+ AuDebugOn(1);
21875+ }
21876+
38d290e6
JR
21877+ if (!err)
21878+ au_cpup_attr_timesizes(inode);
0c3ec466
AM
21879+ ii_write_unlock(inode);
21880+ si_read_unlock(sb);
21881+ lockdep_on();
38d290e6
JR
21882+
21883+ if (!err && (flags & S_VERSION))
21884+ inode_inc_iversion(inode);
21885+
0c3ec466 21886+ return err;
4a4d8108 21887+}
1facf9fc 21888+
4a4d8108 21889+/* ---------------------------------------------------------------------- */
1308ab2a 21890+
b95c5147
AM
21891+/* no getattr version will be set by module.c:aufs_init() */
21892+struct inode_operations aufs_iop_nogetattr[AuIop_Last],
21893+ aufs_iop[] = {
21894+ [AuIop_SYMLINK] = {
21895+ .permission = aufs_permission,
c1595e42 21896+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
21897+ .get_acl = aufs_get_acl,
21898+ .set_acl = aufs_set_acl, /* unsupport for symlink? */
c1595e42
JR
21899+#endif
21900+
b95c5147
AM
21901+ .setattr = aufs_setattr,
21902+ .getattr = aufs_getattr,
0c3ec466 21903+
c1595e42 21904+#ifdef CONFIG_AUFS_XATTR
b95c5147 21905+ .listxattr = aufs_listxattr,
c1595e42
JR
21906+#endif
21907+
febd17d6 21908+ .get_link = aufs_get_link,
0c3ec466 21909+
b95c5147
AM
21910+ /* .update_time = aufs_update_time */
21911+ },
21912+ [AuIop_DIR] = {
21913+ .create = aufs_create,
21914+ .lookup = aufs_lookup,
21915+ .link = aufs_link,
21916+ .unlink = aufs_unlink,
21917+ .symlink = aufs_symlink,
21918+ .mkdir = aufs_mkdir,
21919+ .rmdir = aufs_rmdir,
21920+ .mknod = aufs_mknod,
21921+ .rename = aufs_rename,
21922+
21923+ .permission = aufs_permission,
c1595e42 21924+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
21925+ .get_acl = aufs_get_acl,
21926+ .set_acl = aufs_set_acl,
c1595e42
JR
21927+#endif
21928+
b95c5147
AM
21929+ .setattr = aufs_setattr,
21930+ .getattr = aufs_getattr,
0c3ec466 21931+
c1595e42 21932+#ifdef CONFIG_AUFS_XATTR
b95c5147 21933+ .listxattr = aufs_listxattr,
c1595e42
JR
21934+#endif
21935+
b95c5147
AM
21936+ .update_time = aufs_update_time,
21937+ .atomic_open = aufs_atomic_open,
21938+ .tmpfile = aufs_tmpfile
21939+ },
21940+ [AuIop_OTHER] = {
21941+ .permission = aufs_permission,
c1595e42 21942+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
21943+ .get_acl = aufs_get_acl,
21944+ .set_acl = aufs_set_acl,
c1595e42
JR
21945+#endif
21946+
b95c5147
AM
21947+ .setattr = aufs_setattr,
21948+ .getattr = aufs_getattr,
0c3ec466 21949+
c1595e42 21950+#ifdef CONFIG_AUFS_XATTR
b95c5147 21951+ .listxattr = aufs_listxattr,
c1595e42
JR
21952+#endif
21953+
b95c5147
AM
21954+ .update_time = aufs_update_time
21955+ }
4a4d8108 21956+};
7f207e10
AM
21957diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
21958--- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947 21959+++ linux/fs/aufs/i_op_del.c 2017-11-12 22:24:42.270843503 +0100
5afbbe0d 21960@@ -0,0 +1,511 @@
1facf9fc 21961+/*
a2654f78 21962+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 21963+ *
21964+ * This program, aufs is free software; you can redistribute it and/or modify
21965+ * it under the terms of the GNU General Public License as published by
21966+ * the Free Software Foundation; either version 2 of the License, or
21967+ * (at your option) any later version.
dece6358
AM
21968+ *
21969+ * This program is distributed in the hope that it will be useful,
21970+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21971+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21972+ * GNU General Public License for more details.
21973+ *
21974+ * You should have received a copy of the GNU General Public License
523b37e3 21975+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 21976+ */
21977+
21978+/*
4a4d8108 21979+ * inode operations (del entry)
1308ab2a 21980+ */
dece6358 21981+
1308ab2a 21982+#include "aufs.h"
dece6358 21983+
4a4d8108
AM
21984+/*
21985+ * decide if a new whiteout for @dentry is necessary or not.
21986+ * when it is necessary, prepare the parent dir for the upper branch whose
21987+ * branch index is @bcpup for creation. the actual creation of the whiteout will
21988+ * be done by caller.
21989+ * return value:
21990+ * 0: wh is unnecessary
21991+ * plus: wh is necessary
21992+ * minus: error
21993+ */
21994+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
1308ab2a 21995+{
4a4d8108 21996+ int need_wh, err;
5afbbe0d 21997+ aufs_bindex_t btop;
4a4d8108 21998+ struct super_block *sb;
dece6358 21999+
4a4d8108 22000+ sb = dentry->d_sb;
5afbbe0d 22001+ btop = au_dbtop(dentry);
4a4d8108 22002+ if (*bcpup < 0) {
5afbbe0d
AM
22003+ *bcpup = btop;
22004+ if (au_test_ro(sb, btop, d_inode(dentry))) {
4a4d8108
AM
22005+ err = AuWbrCopyup(au_sbi(sb), dentry);
22006+ *bcpup = err;
22007+ if (unlikely(err < 0))
22008+ goto out;
22009+ }
22010+ } else
5afbbe0d 22011+ AuDebugOn(btop < *bcpup
5527c038 22012+ || au_test_ro(sb, *bcpup, d_inode(dentry)));
5afbbe0d 22013+ AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
1308ab2a 22014+
5afbbe0d 22015+ if (*bcpup != btop) {
4a4d8108
AM
22016+ err = au_cpup_dirs(dentry, *bcpup);
22017+ if (unlikely(err))
22018+ goto out;
22019+ need_wh = 1;
22020+ } else {
027c5e7a 22021+ struct au_dinfo *dinfo, *tmp;
4a4d8108 22022+
027c5e7a
AM
22023+ need_wh = -ENOMEM;
22024+ dinfo = au_di(dentry);
22025+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
22026+ if (tmp) {
22027+ au_di_cp(tmp, dinfo);
22028+ au_di_swap(tmp, dinfo);
22029+ /* returns the number of positive dentries */
5afbbe0d
AM
22030+ need_wh = au_lkup_dentry(dentry, btop + 1,
22031+ /* AuLkup_IGNORE_PERM */ 0);
027c5e7a
AM
22032+ au_di_swap(tmp, dinfo);
22033+ au_rw_write_unlock(&tmp->di_rwsem);
22034+ au_di_free(tmp);
4a4d8108
AM
22035+ }
22036+ }
22037+ AuDbg("need_wh %d\n", need_wh);
22038+ err = need_wh;
22039+
4f0767ce 22040+out:
4a4d8108 22041+ return err;
1facf9fc 22042+}
22043+
4a4d8108
AM
22044+/*
22045+ * simple tests for the del-entry operations.
22046+ * following the checks in vfs, plus the parent-child relationship.
22047+ */
22048+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
22049+ struct dentry *h_parent, int isdir)
1facf9fc 22050+{
4a4d8108
AM
22051+ int err;
22052+ umode_t h_mode;
22053+ struct dentry *h_dentry, *h_latest;
1308ab2a 22054+ struct inode *h_inode;
1facf9fc 22055+
4a4d8108 22056+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 22057+ if (d_really_is_positive(dentry)) {
4a4d8108 22058+ err = -ENOENT;
5527c038
JR
22059+ if (unlikely(d_is_negative(h_dentry)))
22060+ goto out;
22061+ h_inode = d_inode(h_dentry);
22062+ if (unlikely(!h_inode->i_nlink))
4a4d8108 22063+ goto out;
1facf9fc 22064+
4a4d8108
AM
22065+ h_mode = h_inode->i_mode;
22066+ if (!isdir) {
22067+ err = -EISDIR;
22068+ if (unlikely(S_ISDIR(h_mode)))
22069+ goto out;
22070+ } else if (unlikely(!S_ISDIR(h_mode))) {
22071+ err = -ENOTDIR;
22072+ goto out;
22073+ }
22074+ } else {
22075+ /* rename(2) case */
22076+ err = -EIO;
5527c038 22077+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
22078+ goto out;
22079+ }
1facf9fc 22080+
4a4d8108
AM
22081+ err = -ENOENT;
22082+ /* expected parent dir is locked */
22083+ if (unlikely(h_parent != h_dentry->d_parent))
22084+ goto out;
22085+ err = 0;
22086+
22087+ /*
22088+ * rmdir a dir may break the consistency on some filesystem.
22089+ * let's try heavy test.
22090+ */
22091+ err = -EACCES;
076b876e 22092+ if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
5527c038 22093+ && au_test_h_perm(d_inode(h_parent),
076b876e 22094+ MAY_EXEC | MAY_WRITE)))
4a4d8108
AM
22095+ goto out;
22096+
076b876e 22097+ h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
4a4d8108
AM
22098+ err = -EIO;
22099+ if (IS_ERR(h_latest))
22100+ goto out;
22101+ if (h_latest == h_dentry)
22102+ err = 0;
22103+ dput(h_latest);
22104+
4f0767ce 22105+out:
4a4d8108 22106+ return err;
1308ab2a 22107+}
1facf9fc 22108+
4a4d8108
AM
22109+/*
22110+ * decide the branch where we operate for @dentry. the branch index will be set
22111+ * @rbcpup. after diciding it, 'pin' it and store the timestamps of the parent
22112+ * dir for reverting.
22113+ * when a new whiteout is necessary, create it.
22114+ */
22115+static struct dentry*
22116+lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
22117+ struct au_dtime *dt, struct au_pin *pin)
1308ab2a 22118+{
4a4d8108
AM
22119+ struct dentry *wh_dentry;
22120+ struct super_block *sb;
22121+ struct path h_path;
22122+ int err, need_wh;
22123+ unsigned int udba;
22124+ aufs_bindex_t bcpup;
dece6358 22125+
4a4d8108
AM
22126+ need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
22127+ wh_dentry = ERR_PTR(need_wh);
22128+ if (unlikely(need_wh < 0))
22129+ goto out;
22130+
22131+ sb = dentry->d_sb;
22132+ udba = au_opt_udba(sb);
22133+ bcpup = *rbcpup;
22134+ err = au_pin(pin, dentry, bcpup, udba,
22135+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22136+ wh_dentry = ERR_PTR(err);
22137+ if (unlikely(err))
22138+ goto out;
22139+
22140+ h_path.dentry = au_pinned_h_parent(pin);
22141+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 22142+ && au_dbtop(dentry) == bcpup) {
4a4d8108
AM
22143+ err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
22144+ wh_dentry = ERR_PTR(err);
22145+ if (unlikely(err))
22146+ goto out_unpin;
22147+ }
22148+
22149+ h_path.mnt = au_sbr_mnt(sb, bcpup);
22150+ au_dtime_store(dt, au_pinned_parent(pin), &h_path);
22151+ wh_dentry = NULL;
22152+ if (!need_wh)
22153+ goto out; /* success, no need to create whiteout */
22154+
22155+ wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
22156+ if (IS_ERR(wh_dentry))
22157+ goto out_unpin;
22158+
22159+ /* returns with the parent is locked and wh_dentry is dget-ed */
22160+ goto out; /* success */
22161+
4f0767ce 22162+out_unpin:
4a4d8108 22163+ au_unpin(pin);
4f0767ce 22164+out:
4a4d8108 22165+ return wh_dentry;
1facf9fc 22166+}
22167+
4a4d8108
AM
22168+/*
22169+ * when removing a dir, rename it to a unique temporary whiteout-ed name first
22170+ * in order to be revertible and save time for removing many child whiteouts
22171+ * under the dir.
22172+ * returns 1 when there are too many child whiteout and caller should remove
22173+ * them asynchronously. returns 0 when the number of children is enough small to
22174+ * remove now or the branch fs is a remote fs.
22175+ * otherwise return an error.
22176+ */
22177+static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
22178+ struct au_nhash *whlist, struct inode *dir)
1facf9fc 22179+{
4a4d8108
AM
22180+ int rmdir_later, err, dirwh;
22181+ struct dentry *h_dentry;
22182+ struct super_block *sb;
5527c038 22183+ struct inode *inode;
4a4d8108
AM
22184+
22185+ sb = dentry->d_sb;
22186+ SiMustAnyLock(sb);
22187+ h_dentry = au_h_dptr(dentry, bindex);
22188+ err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
22189+ if (unlikely(err))
22190+ goto out;
22191+
22192+ /* stop monitoring */
5527c038
JR
22193+ inode = d_inode(dentry);
22194+ au_hn_free(au_hi(inode, bindex));
4a4d8108
AM
22195+
22196+ if (!au_test_fs_remote(h_dentry->d_sb)) {
22197+ dirwh = au_sbi(sb)->si_dirwh;
22198+ rmdir_later = (dirwh <= 1);
22199+ if (!rmdir_later)
22200+ rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
22201+ dirwh);
22202+ if (rmdir_later)
22203+ return rmdir_later;
22204+ }
1facf9fc 22205+
4a4d8108
AM
22206+ err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
22207+ if (unlikely(err)) {
523b37e3
AM
22208+ AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
22209+ h_dentry, bindex, err);
4a4d8108
AM
22210+ err = 0;
22211+ }
dece6358 22212+
4f0767ce 22213+out:
4a4d8108
AM
22214+ AuTraceErr(err);
22215+ return err;
22216+}
1308ab2a 22217+
4a4d8108
AM
22218+/*
22219+ * final procedure for deleting a entry.
22220+ * maintain dentry and iattr.
22221+ */
22222+static void epilog(struct inode *dir, struct dentry *dentry,
22223+ aufs_bindex_t bindex)
22224+{
22225+ struct inode *inode;
1308ab2a 22226+
5527c038 22227+ inode = d_inode(dentry);
4a4d8108
AM
22228+ d_drop(dentry);
22229+ inode->i_ctime = dir->i_ctime;
1308ab2a 22230+
b912730e 22231+ au_dir_ts(dir, bindex);
4a4d8108 22232+ dir->i_version++;
1facf9fc 22233+}
22234+
4a4d8108
AM
22235+/*
22236+ * when an error happened, remove the created whiteout and revert everything.
22237+ */
7f207e10
AM
22238+static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
22239+ aufs_bindex_t bwh, struct dentry *wh_dentry,
22240+ struct dentry *dentry, struct au_dtime *dt)
1facf9fc 22241+{
4a4d8108
AM
22242+ int rerr;
22243+ struct path h_path = {
22244+ .dentry = wh_dentry,
7f207e10 22245+ .mnt = au_sbr_mnt(dir->i_sb, bindex)
4a4d8108 22246+ };
dece6358 22247+
7f207e10 22248+ rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
4a4d8108
AM
22249+ if (!rerr) {
22250+ au_set_dbwh(dentry, bwh);
22251+ au_dtime_revert(dt);
22252+ return 0;
22253+ }
dece6358 22254+
523b37e3 22255+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
4a4d8108 22256+ return -EIO;
1facf9fc 22257+}
22258+
4a4d8108 22259+/* ---------------------------------------------------------------------- */
1facf9fc 22260+
4a4d8108 22261+int aufs_unlink(struct inode *dir, struct dentry *dentry)
1308ab2a 22262+{
4a4d8108 22263+ int err;
5afbbe0d 22264+ aufs_bindex_t bwh, bindex, btop;
523b37e3 22265+ struct inode *inode, *h_dir, *delegated;
4a4d8108 22266+ struct dentry *parent, *wh_dentry;
c2b27bf2
AM
22267+ /* to reuduce stack size */
22268+ struct {
22269+ struct au_dtime dt;
22270+ struct au_pin pin;
22271+ struct path h_path;
22272+ } *a;
1facf9fc 22273+
4a4d8108 22274+ IMustLock(dir);
027c5e7a 22275+
c2b27bf2
AM
22276+ err = -ENOMEM;
22277+ a = kmalloc(sizeof(*a), GFP_NOFS);
22278+ if (unlikely(!a))
22279+ goto out;
22280+
027c5e7a
AM
22281+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
22282+ if (unlikely(err))
c2b27bf2 22283+ goto out_free;
027c5e7a
AM
22284+ err = au_d_hashed_positive(dentry);
22285+ if (unlikely(err))
22286+ goto out_unlock;
5527c038 22287+ inode = d_inode(dentry);
4a4d8108 22288+ IMustLock(inode);
027c5e7a 22289+ err = -EISDIR;
2000de60 22290+ if (unlikely(d_is_dir(dentry)))
027c5e7a 22291+ goto out_unlock; /* possible? */
1facf9fc 22292+
5afbbe0d 22293+ btop = au_dbtop(dentry);
4a4d8108
AM
22294+ bwh = au_dbwh(dentry);
22295+ bindex = -1;
027c5e7a
AM
22296+ parent = dentry->d_parent; /* dir inode is locked */
22297+ di_write_lock_parent(parent);
c2b27bf2
AM
22298+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
22299+ &a->pin);
4a4d8108
AM
22300+ err = PTR_ERR(wh_dentry);
22301+ if (IS_ERR(wh_dentry))
027c5e7a 22302+ goto out_parent;
1facf9fc 22303+
5afbbe0d
AM
22304+ a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
22305+ a->h_path.dentry = au_h_dptr(dentry, btop);
c2b27bf2 22306+ dget(a->h_path.dentry);
5afbbe0d 22307+ if (bindex == btop) {
c2b27bf2 22308+ h_dir = au_pinned_h_dir(&a->pin);
523b37e3
AM
22309+ delegated = NULL;
22310+ err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
22311+ if (unlikely(err == -EWOULDBLOCK)) {
22312+ pr_warn("cannot retry for NFSv4 delegation"
22313+ " for an internal unlink\n");
22314+ iput(delegated);
22315+ }
4a4d8108
AM
22316+ } else {
22317+ /* dir inode is locked */
5527c038 22318+ h_dir = d_inode(wh_dentry->d_parent);
4a4d8108
AM
22319+ IMustLock(h_dir);
22320+ err = 0;
22321+ }
dece6358 22322+
4a4d8108 22323+ if (!err) {
7f207e10 22324+ vfsub_drop_nlink(inode);
4a4d8108
AM
22325+ epilog(dir, dentry, bindex);
22326+
22327+ /* update target timestamps */
5afbbe0d 22328+ if (bindex == btop) {
c2b27bf2
AM
22329+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
22330+ /*ignore*/
5527c038 22331+ inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
4a4d8108
AM
22332+ } else
22333+ /* todo: this timestamp may be reverted later */
22334+ inode->i_ctime = h_dir->i_ctime;
027c5e7a 22335+ goto out_unpin; /* success */
1facf9fc 22336+ }
22337+
4a4d8108
AM
22338+ /* revert */
22339+ if (wh_dentry) {
22340+ int rerr;
22341+
c2b27bf2
AM
22342+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22343+ &a->dt);
4a4d8108
AM
22344+ if (rerr)
22345+ err = rerr;
dece6358 22346+ }
1facf9fc 22347+
027c5e7a 22348+out_unpin:
c2b27bf2 22349+ au_unpin(&a->pin);
4a4d8108 22350+ dput(wh_dentry);
c2b27bf2 22351+ dput(a->h_path.dentry);
027c5e7a 22352+out_parent:
4a4d8108 22353+ di_write_unlock(parent);
027c5e7a 22354+out_unlock:
4a4d8108 22355+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 22356+out_free:
1c60b727 22357+ kfree(a);
027c5e7a 22358+out:
4a4d8108 22359+ return err;
dece6358
AM
22360+}
22361+
4a4d8108 22362+int aufs_rmdir(struct inode *dir, struct dentry *dentry)
1308ab2a 22363+{
4a4d8108 22364+ int err, rmdir_later;
5afbbe0d 22365+ aufs_bindex_t bwh, bindex, btop;
4a4d8108
AM
22366+ struct inode *inode;
22367+ struct dentry *parent, *wh_dentry, *h_dentry;
22368+ struct au_whtmp_rmdir *args;
c2b27bf2
AM
22369+ /* to reuduce stack size */
22370+ struct {
22371+ struct au_dtime dt;
22372+ struct au_pin pin;
22373+ } *a;
1facf9fc 22374+
4a4d8108 22375+ IMustLock(dir);
027c5e7a 22376+
c2b27bf2
AM
22377+ err = -ENOMEM;
22378+ a = kmalloc(sizeof(*a), GFP_NOFS);
22379+ if (unlikely(!a))
22380+ goto out;
22381+
027c5e7a
AM
22382+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22383+ if (unlikely(err))
c2b27bf2 22384+ goto out_free;
53392da6
AM
22385+ err = au_alive_dir(dentry);
22386+ if (unlikely(err))
027c5e7a 22387+ goto out_unlock;
5527c038 22388+ inode = d_inode(dentry);
4a4d8108 22389+ IMustLock(inode);
027c5e7a 22390+ err = -ENOTDIR;
2000de60 22391+ if (unlikely(!d_is_dir(dentry)))
027c5e7a 22392+ goto out_unlock; /* possible? */
dece6358 22393+
4a4d8108
AM
22394+ err = -ENOMEM;
22395+ args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
22396+ if (unlikely(!args))
22397+ goto out_unlock;
dece6358 22398+
4a4d8108
AM
22399+ parent = dentry->d_parent; /* dir inode is locked */
22400+ di_write_lock_parent(parent);
22401+ err = au_test_empty(dentry, &args->whlist);
22402+ if (unlikely(err))
027c5e7a 22403+ goto out_parent;
1facf9fc 22404+
5afbbe0d 22405+ btop = au_dbtop(dentry);
4a4d8108
AM
22406+ bwh = au_dbwh(dentry);
22407+ bindex = -1;
c2b27bf2
AM
22408+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
22409+ &a->pin);
4a4d8108
AM
22410+ err = PTR_ERR(wh_dentry);
22411+ if (IS_ERR(wh_dentry))
027c5e7a 22412+ goto out_parent;
1facf9fc 22413+
5afbbe0d 22414+ h_dentry = au_h_dptr(dentry, btop);
4a4d8108
AM
22415+ dget(h_dentry);
22416+ rmdir_later = 0;
5afbbe0d
AM
22417+ if (bindex == btop) {
22418+ err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
4a4d8108
AM
22419+ if (err > 0) {
22420+ rmdir_later = err;
22421+ err = 0;
22422+ }
22423+ } else {
22424+ /* stop monitoring */
5afbbe0d 22425+ au_hn_free(au_hi(inode, btop));
4a4d8108
AM
22426+
22427+ /* dir inode is locked */
5527c038 22428+ IMustLock(d_inode(wh_dentry->d_parent));
1facf9fc 22429+ err = 0;
22430+ }
22431+
4a4d8108 22432+ if (!err) {
027c5e7a 22433+ vfsub_dead_dir(inode);
4a4d8108
AM
22434+ au_set_dbdiropq(dentry, -1);
22435+ epilog(dir, dentry, bindex);
1308ab2a 22436+
4a4d8108 22437+ if (rmdir_later) {
5afbbe0d 22438+ au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
4a4d8108
AM
22439+ args = NULL;
22440+ }
1308ab2a 22441+
4a4d8108 22442+ goto out_unpin; /* success */
1facf9fc 22443+ }
22444+
4a4d8108
AM
22445+ /* revert */
22446+ AuLabel(revert);
22447+ if (wh_dentry) {
22448+ int rerr;
1308ab2a 22449+
c2b27bf2
AM
22450+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22451+ &a->dt);
4a4d8108
AM
22452+ if (rerr)
22453+ err = rerr;
1facf9fc 22454+ }
22455+
4f0767ce 22456+out_unpin:
c2b27bf2 22457+ au_unpin(&a->pin);
4a4d8108
AM
22458+ dput(wh_dentry);
22459+ dput(h_dentry);
027c5e7a 22460+out_parent:
4a4d8108
AM
22461+ di_write_unlock(parent);
22462+ if (args)
22463+ au_whtmp_rmdir_free(args);
4f0767ce 22464+out_unlock:
4a4d8108 22465+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 22466+out_free:
1c60b727 22467+ kfree(a);
4f0767ce 22468+out:
4a4d8108
AM
22469+ AuTraceErr(err);
22470+ return err;
dece6358 22471+}
7f207e10
AM
22472diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
22473--- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
22474+++ linux/fs/aufs/i_op_ren.c 2017-11-12 22:24:44.707577830 +0100
22475@@ -0,0 +1,1246 @@
1facf9fc 22476+/*
a2654f78 22477+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 22478+ *
22479+ * This program, aufs is free software; you can redistribute it and/or modify
22480+ * it under the terms of the GNU General Public License as published by
22481+ * the Free Software Foundation; either version 2 of the License, or
22482+ * (at your option) any later version.
dece6358
AM
22483+ *
22484+ * This program is distributed in the hope that it will be useful,
22485+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22486+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22487+ * GNU General Public License for more details.
22488+ *
22489+ * You should have received a copy of the GNU General Public License
523b37e3 22490+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 22491+ */
22492+
22493+/*
4a4d8108
AM
22494+ * inode operation (rename entry)
22495+ * todo: this is crazy monster
1facf9fc 22496+ */
22497+
22498+#include "aufs.h"
22499+
4a4d8108
AM
22500+enum { AuSRC, AuDST, AuSrcDst };
22501+enum { AuPARENT, AuCHILD, AuParentChild };
1facf9fc 22502+
f2c43d5f
AM
22503+#define AuRen_ISDIR_SRC 1
22504+#define AuRen_ISDIR_DST (1 << 1)
22505+#define AuRen_ISSAMEDIR (1 << 2)
22506+#define AuRen_WHSRC (1 << 3)
22507+#define AuRen_WHDST (1 << 4)
22508+#define AuRen_MNT_WRITE (1 << 5)
22509+#define AuRen_DT_DSTDIR (1 << 6)
22510+#define AuRen_DIROPQ_SRC (1 << 7)
22511+#define AuRen_DIROPQ_DST (1 << 8)
8b6a4947
AM
22512+#define AuRen_DIRREN (1 << 9)
22513+#define AuRen_DROPPED_SRC (1 << 10)
22514+#define AuRen_DROPPED_DST (1 << 11)
4a4d8108 22515+#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
7f207e10
AM
22516+#define au_fset_ren(flags, name) \
22517+ do { (flags) |= AuRen_##name; } while (0)
22518+#define au_fclr_ren(flags, name) \
22519+ do { (flags) &= ~AuRen_##name; } while (0)
1facf9fc 22520+
8b6a4947
AM
22521+#ifndef CONFIG_AUFS_DIRREN
22522+#undef AuRen_DIRREN
22523+#define AuRen_DIRREN 0
22524+#endif
22525+
4a4d8108
AM
22526+struct au_ren_args {
22527+ struct {
22528+ struct dentry *dentry, *h_dentry, *parent, *h_parent,
22529+ *wh_dentry;
22530+ struct inode *dir, *inode;
f2c43d5f 22531+ struct au_hinode *hdir, *hinode;
4a4d8108 22532+ struct au_dtime dt[AuParentChild];
f2c43d5f 22533+ aufs_bindex_t btop, bdiropq;
4a4d8108 22534+ } sd[AuSrcDst];
1facf9fc 22535+
4a4d8108
AM
22536+#define src_dentry sd[AuSRC].dentry
22537+#define src_dir sd[AuSRC].dir
22538+#define src_inode sd[AuSRC].inode
22539+#define src_h_dentry sd[AuSRC].h_dentry
22540+#define src_parent sd[AuSRC].parent
22541+#define src_h_parent sd[AuSRC].h_parent
22542+#define src_wh_dentry sd[AuSRC].wh_dentry
22543+#define src_hdir sd[AuSRC].hdir
f2c43d5f 22544+#define src_hinode sd[AuSRC].hinode
4a4d8108
AM
22545+#define src_h_dir sd[AuSRC].hdir->hi_inode
22546+#define src_dt sd[AuSRC].dt
5afbbe0d 22547+#define src_btop sd[AuSRC].btop
f2c43d5f 22548+#define src_bdiropq sd[AuSRC].bdiropq
1facf9fc 22549+
4a4d8108
AM
22550+#define dst_dentry sd[AuDST].dentry
22551+#define dst_dir sd[AuDST].dir
22552+#define dst_inode sd[AuDST].inode
22553+#define dst_h_dentry sd[AuDST].h_dentry
22554+#define dst_parent sd[AuDST].parent
22555+#define dst_h_parent sd[AuDST].h_parent
22556+#define dst_wh_dentry sd[AuDST].wh_dentry
22557+#define dst_hdir sd[AuDST].hdir
f2c43d5f 22558+#define dst_hinode sd[AuDST].hinode
4a4d8108
AM
22559+#define dst_h_dir sd[AuDST].hdir->hi_inode
22560+#define dst_dt sd[AuDST].dt
5afbbe0d 22561+#define dst_btop sd[AuDST].btop
f2c43d5f 22562+#define dst_bdiropq sd[AuDST].bdiropq
4a4d8108
AM
22563+
22564+ struct dentry *h_trap;
22565+ struct au_branch *br;
4a4d8108
AM
22566+ struct path h_path;
22567+ struct au_nhash whlist;
f2c43d5f 22568+ aufs_bindex_t btgt, src_bwh;
1facf9fc 22569+
f2c43d5f
AM
22570+ struct {
22571+ unsigned short auren_flags;
22572+ unsigned char flags; /* syscall parameter */
22573+ unsigned char exchange;
22574+ } __packed;
1facf9fc 22575+
4a4d8108
AM
22576+ struct au_whtmp_rmdir *thargs;
22577+ struct dentry *h_dst;
8b6a4947 22578+ struct au_hinode *h_root;
4a4d8108 22579+};
1308ab2a 22580+
4a4d8108 22581+/* ---------------------------------------------------------------------- */
1308ab2a 22582+
4a4d8108
AM
22583+/*
22584+ * functions for reverting.
22585+ * when an error happened in a single rename systemcall, we should revert
79b8bda9 22586+ * everything as if nothing happened.
4a4d8108
AM
22587+ * we don't need to revert the copied-up/down the parent dir since they are
22588+ * harmless.
22589+ */
1facf9fc 22590+
4a4d8108
AM
22591+#define RevertFailure(fmt, ...) do { \
22592+ AuIOErr("revert failure: " fmt " (%d, %d)\n", \
22593+ ##__VA_ARGS__, err, rerr); \
22594+ err = -EIO; \
22595+} while (0)
1facf9fc 22596+
f2c43d5f 22597+static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
1facf9fc 22598+{
4a4d8108 22599+ int rerr;
f2c43d5f
AM
22600+ struct dentry *d;
22601+#define src_or_dst(member) a->sd[idx].member
1facf9fc 22602+
f2c43d5f
AM
22603+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22604+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22605+ rerr = au_diropq_remove(d, a->btgt);
22606+ au_hn_inode_unlock(src_or_dst(hinode));
22607+ au_set_dbdiropq(d, src_or_dst(bdiropq));
4a4d8108 22608+ if (rerr)
f2c43d5f
AM
22609+ RevertFailure("remove diropq %pd", d);
22610+
22611+#undef src_or_dst_
22612+}
22613+
22614+static void au_ren_rev_diropq(int err, struct au_ren_args *a)
22615+{
22616+ if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
22617+ au_ren_do_rev_diropq(err, a, AuSRC);
22618+ if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
22619+ au_ren_do_rev_diropq(err, a, AuDST);
4a4d8108 22620+}
1facf9fc 22621+
4a4d8108
AM
22622+static void au_ren_rev_rename(int err, struct au_ren_args *a)
22623+{
22624+ int rerr;
523b37e3 22625+ struct inode *delegated;
1facf9fc 22626+
b4510431
AM
22627+ a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
22628+ a->src_h_parent);
4a4d8108
AM
22629+ rerr = PTR_ERR(a->h_path.dentry);
22630+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 22631+ RevertFailure("lkup one %pd", a->src_dentry);
4a4d8108 22632+ return;
1facf9fc 22633+ }
22634+
523b37e3 22635+ delegated = NULL;
4a4d8108
AM
22636+ rerr = vfsub_rename(a->dst_h_dir,
22637+ au_h_dptr(a->src_dentry, a->btgt),
f2c43d5f 22638+ a->src_h_dir, &a->h_path, &delegated, a->flags);
523b37e3
AM
22639+ if (unlikely(rerr == -EWOULDBLOCK)) {
22640+ pr_warn("cannot retry for NFSv4 delegation"
22641+ " for an internal rename\n");
22642+ iput(delegated);
22643+ }
4a4d8108
AM
22644+ d_drop(a->h_path.dentry);
22645+ dput(a->h_path.dentry);
22646+ /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
22647+ if (rerr)
523b37e3 22648+ RevertFailure("rename %pd", a->src_dentry);
1facf9fc 22649+}
22650+
4a4d8108 22651+static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
1facf9fc 22652+{
4a4d8108 22653+ int rerr;
523b37e3 22654+ struct inode *delegated;
dece6358 22655+
b4510431
AM
22656+ a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
22657+ a->dst_h_parent);
4a4d8108
AM
22658+ rerr = PTR_ERR(a->h_path.dentry);
22659+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 22660+ RevertFailure("lkup one %pd", a->dst_dentry);
4a4d8108
AM
22661+ return;
22662+ }
5527c038 22663+ if (d_is_positive(a->h_path.dentry)) {
4a4d8108
AM
22664+ d_drop(a->h_path.dentry);
22665+ dput(a->h_path.dentry);
22666+ return;
dece6358
AM
22667+ }
22668+
523b37e3
AM
22669+ delegated = NULL;
22670+ rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
f2c43d5f 22671+ &delegated, a->flags);
523b37e3
AM
22672+ if (unlikely(rerr == -EWOULDBLOCK)) {
22673+ pr_warn("cannot retry for NFSv4 delegation"
22674+ " for an internal rename\n");
22675+ iput(delegated);
22676+ }
4a4d8108
AM
22677+ d_drop(a->h_path.dentry);
22678+ dput(a->h_path.dentry);
22679+ if (!rerr)
22680+ au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
22681+ else
523b37e3 22682+ RevertFailure("rename %pd", a->h_dst);
4a4d8108 22683+}
1308ab2a 22684+
4a4d8108
AM
22685+static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
22686+{
22687+ int rerr;
1308ab2a 22688+
4a4d8108
AM
22689+ a->h_path.dentry = a->src_wh_dentry;
22690+ rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
027c5e7a 22691+ au_set_dbwh(a->src_dentry, a->src_bwh);
4a4d8108 22692+ if (rerr)
523b37e3 22693+ RevertFailure("unlink %pd", a->src_wh_dentry);
4a4d8108 22694+}
4a4d8108 22695+#undef RevertFailure
1facf9fc 22696+
1308ab2a 22697+/* ---------------------------------------------------------------------- */
22698+
4a4d8108
AM
22699+/*
22700+ * when we have to copyup the renaming entry, do it with the rename-target name
22701+ * in order to minimize the cost (the later actual rename is unnecessary).
22702+ * otherwise rename it on the target branch.
22703+ */
22704+static int au_ren_or_cpup(struct au_ren_args *a)
1facf9fc 22705+{
dece6358 22706+ int err;
4a4d8108 22707+ struct dentry *d;
523b37e3 22708+ struct inode *delegated;
1facf9fc 22709+
4a4d8108 22710+ d = a->src_dentry;
5afbbe0d 22711+ if (au_dbtop(d) == a->btgt) {
4a4d8108 22712+ a->h_path.dentry = a->dst_h_dentry;
5afbbe0d 22713+ AuDebugOn(au_dbtop(d) != a->btgt);
523b37e3 22714+ delegated = NULL;
4a4d8108 22715+ err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
f2c43d5f
AM
22716+ a->dst_h_dir, &a->h_path, &delegated,
22717+ a->flags);
523b37e3
AM
22718+ if (unlikely(err == -EWOULDBLOCK)) {
22719+ pr_warn("cannot retry for NFSv4 delegation"
22720+ " for an internal rename\n");
22721+ iput(delegated);
22722+ }
c2b27bf2 22723+ } else
86dc4139 22724+ BUG();
1308ab2a 22725+
027c5e7a
AM
22726+ if (!err && a->h_dst)
22727+ /* it will be set to dinfo later */
22728+ dget(a->h_dst);
1facf9fc 22729+
dece6358
AM
22730+ return err;
22731+}
1facf9fc 22732+
4a4d8108
AM
22733+/* cf. aufs_rmdir() */
22734+static int au_ren_del_whtmp(struct au_ren_args *a)
dece6358 22735+{
4a4d8108
AM
22736+ int err;
22737+ struct inode *dir;
1facf9fc 22738+
4a4d8108
AM
22739+ dir = a->dst_dir;
22740+ SiMustAnyLock(dir->i_sb);
22741+ if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
22742+ au_sbi(dir->i_sb)->si_dirwh)
22743+ || au_test_fs_remote(a->h_dst->d_sb)) {
22744+ err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
22745+ if (unlikely(err))
523b37e3
AM
22746+ pr_warn("failed removing whtmp dir %pd (%d), "
22747+ "ignored.\n", a->h_dst, err);
4a4d8108
AM
22748+ } else {
22749+ au_nhash_wh_free(&a->thargs->whlist);
22750+ a->thargs->whlist = a->whlist;
22751+ a->whlist.nh_num = 0;
22752+ au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
22753+ dput(a->h_dst);
22754+ a->thargs = NULL;
22755+ }
22756+
22757+ return 0;
1308ab2a 22758+}
1facf9fc 22759+
4a4d8108 22760+/* make it 'opaque' dir. */
f2c43d5f 22761+static int au_ren_do_diropq(struct au_ren_args *a, int idx)
4a4d8108
AM
22762+{
22763+ int err;
f2c43d5f
AM
22764+ struct dentry *d, *diropq;
22765+#define src_or_dst(member) a->sd[idx].member
1facf9fc 22766+
4a4d8108 22767+ err = 0;
f2c43d5f
AM
22768+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22769+ src_or_dst(bdiropq) = au_dbdiropq(d);
22770+ src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
22771+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22772+ diropq = au_diropq_create(d, a->btgt);
22773+ au_hn_inode_unlock(src_or_dst(hinode));
4a4d8108
AM
22774+ if (IS_ERR(diropq))
22775+ err = PTR_ERR(diropq);
076b876e
AM
22776+ else
22777+ dput(diropq);
1facf9fc 22778+
f2c43d5f 22779+#undef src_or_dst_
4a4d8108
AM
22780+ return err;
22781+}
1facf9fc 22782+
f2c43d5f 22783+static int au_ren_diropq(struct au_ren_args *a)
4a4d8108
AM
22784+{
22785+ int err;
f2c43d5f
AM
22786+ unsigned char always;
22787+ struct dentry *d;
1facf9fc 22788+
f2c43d5f
AM
22789+ err = 0;
22790+ d = a->dst_dentry; /* already renamed on the branch */
22791+ always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
22792+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
8b6a4947 22793+ && !au_ftest_ren(a->auren_flags, DIRREN)
f2c43d5f
AM
22794+ && a->btgt != au_dbdiropq(a->src_dentry)
22795+ && (a->dst_wh_dentry
22796+ || a->btgt <= au_dbdiropq(d)
22797+ /* hide the lower to keep xino */
22798+ /* the lowers may not be a dir, but we hide them anyway */
22799+ || a->btgt < au_dbbot(d)
22800+ || always)) {
22801+ AuDbg("here\n");
22802+ err = au_ren_do_diropq(a, AuSRC);
22803+ if (unlikely(err))
4a4d8108 22804+ goto out;
f2c43d5f 22805+ au_fset_ren(a->auren_flags, DIROPQ_SRC);
4a4d8108 22806+ }
f2c43d5f
AM
22807+ if (!a->exchange)
22808+ goto out; /* success */
1facf9fc 22809+
f2c43d5f
AM
22810+ d = a->src_dentry; /* already renamed on the branch */
22811+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22812+ && a->btgt != au_dbdiropq(a->dst_dentry)
22813+ && (a->btgt < au_dbdiropq(d)
22814+ || a->btgt < au_dbbot(d)
22815+ || always)) {
22816+ AuDbgDentry(a->src_dentry);
22817+ AuDbgDentry(a->dst_dentry);
22818+ err = au_ren_do_diropq(a, AuDST);
4a4d8108 22819+ if (unlikely(err))
f2c43d5f
AM
22820+ goto out_rev_src;
22821+ au_fset_ren(a->auren_flags, DIROPQ_DST);
22822+ }
22823+ goto out; /* success */
dece6358 22824+
f2c43d5f
AM
22825+out_rev_src:
22826+ AuDbg("err %d, reverting src\n", err);
22827+ au_ren_rev_diropq(err, a);
22828+out:
22829+ return err;
22830+}
22831+
22832+static int do_rename(struct au_ren_args *a)
22833+{
22834+ int err;
22835+ struct dentry *d, *h_d;
22836+
22837+ if (!a->exchange) {
22838+ /* prepare workqueue args for asynchronous rmdir */
22839+ h_d = a->dst_h_dentry;
22840+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
8b6a4947 22841+ /* && !au_ftest_ren(a->auren_flags, DIRREN) */
f2c43d5f
AM
22842+ && d_is_positive(h_d)) {
22843+ err = -ENOMEM;
22844+ a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
22845+ GFP_NOFS);
22846+ if (unlikely(!a->thargs))
22847+ goto out;
22848+ a->h_dst = dget(h_d);
22849+ }
22850+
22851+ /* create whiteout for src_dentry */
22852+ if (au_ftest_ren(a->auren_flags, WHSRC)) {
22853+ a->src_bwh = au_dbwh(a->src_dentry);
22854+ AuDebugOn(a->src_bwh >= 0);
22855+ a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
22856+ a->src_h_parent);
22857+ err = PTR_ERR(a->src_wh_dentry);
22858+ if (IS_ERR(a->src_wh_dentry))
22859+ goto out_thargs;
22860+ }
22861+
22862+ /* lookup whiteout for dentry */
22863+ if (au_ftest_ren(a->auren_flags, WHDST)) {
22864+ h_d = au_wh_lkup(a->dst_h_parent,
22865+ &a->dst_dentry->d_name, a->br);
22866+ err = PTR_ERR(h_d);
22867+ if (IS_ERR(h_d))
22868+ goto out_whsrc;
22869+ if (d_is_negative(h_d))
22870+ dput(h_d);
22871+ else
22872+ a->dst_wh_dentry = h_d;
22873+ }
22874+
22875+ /* rename dentry to tmpwh */
22876+ if (a->thargs) {
22877+ err = au_whtmp_ren(a->dst_h_dentry, a->br);
22878+ if (unlikely(err))
22879+ goto out_whdst;
22880+
22881+ d = a->dst_dentry;
22882+ au_set_h_dptr(d, a->btgt, NULL);
22883+ err = au_lkup_neg(d, a->btgt, /*wh*/0);
22884+ if (unlikely(err))
22885+ goto out_whtmp;
22886+ a->dst_h_dentry = au_h_dptr(d, a->btgt);
22887+ }
4a4d8108 22888+ }
1facf9fc 22889+
5afbbe0d 22890+ BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
8b6a4947
AM
22891+#if 0
22892+ BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
22893+ && d_is_positive(a->dst_h_dentry)
22894+ && a->src_btop != a->btgt);
22895+#endif
1facf9fc 22896+
4a4d8108 22897+ /* rename by vfs_rename or cpup */
4a4d8108
AM
22898+ err = au_ren_or_cpup(a);
22899+ if (unlikely(err))
22900+ /* leave the copied-up one */
22901+ goto out_whtmp;
1308ab2a 22902+
4a4d8108 22903+ /* make dir opaque */
f2c43d5f
AM
22904+ err = au_ren_diropq(a);
22905+ if (unlikely(err))
22906+ goto out_rename;
1308ab2a 22907+
4a4d8108 22908+ /* update target timestamps */
f2c43d5f
AM
22909+ if (a->exchange) {
22910+ AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
22911+ a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
22912+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
22913+ a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22914+ }
5afbbe0d 22915+ AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
4a4d8108
AM
22916+ a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
22917+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
5527c038 22918+ a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
1facf9fc 22919+
f2c43d5f
AM
22920+ if (!a->exchange) {
22921+ /* remove whiteout for dentry */
22922+ if (a->dst_wh_dentry) {
22923+ a->h_path.dentry = a->dst_wh_dentry;
22924+ err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
22925+ a->dst_dentry);
22926+ if (unlikely(err))
22927+ goto out_diropq;
22928+ }
1facf9fc 22929+
f2c43d5f
AM
22930+ /* remove whtmp */
22931+ if (a->thargs)
22932+ au_ren_del_whtmp(a); /* ignore this error */
1308ab2a 22933+
f2c43d5f
AM
22934+ au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
22935+ }
4a4d8108
AM
22936+ err = 0;
22937+ goto out_success;
22938+
4f0767ce 22939+out_diropq:
f2c43d5f 22940+ au_ren_rev_diropq(err, a);
4f0767ce 22941+out_rename:
7e9cd9fe 22942+ au_ren_rev_rename(err, a);
027c5e7a 22943+ dput(a->h_dst);
4f0767ce 22944+out_whtmp:
4a4d8108
AM
22945+ if (a->thargs)
22946+ au_ren_rev_whtmp(err, a);
4f0767ce 22947+out_whdst:
4a4d8108
AM
22948+ dput(a->dst_wh_dentry);
22949+ a->dst_wh_dentry = NULL;
4f0767ce 22950+out_whsrc:
4a4d8108
AM
22951+ if (a->src_wh_dentry)
22952+ au_ren_rev_whsrc(err, a);
4f0767ce 22953+out_success:
4a4d8108
AM
22954+ dput(a->src_wh_dentry);
22955+ dput(a->dst_wh_dentry);
4f0767ce 22956+out_thargs:
4a4d8108
AM
22957+ if (a->thargs) {
22958+ dput(a->h_dst);
22959+ au_whtmp_rmdir_free(a->thargs);
22960+ a->thargs = NULL;
22961+ }
4f0767ce 22962+out:
4a4d8108 22963+ return err;
dece6358 22964+}
1facf9fc 22965+
1308ab2a 22966+/* ---------------------------------------------------------------------- */
1facf9fc 22967+
4a4d8108
AM
22968+/*
22969+ * test if @dentry dir can be rename destination or not.
22970+ * success means, it is a logically empty dir.
22971+ */
22972+static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
1308ab2a 22973+{
4a4d8108 22974+ return au_test_empty(dentry, whlist);
1308ab2a 22975+}
1facf9fc 22976+
4a4d8108 22977+/*
8b6a4947
AM
22978+ * test if @a->src_dentry dir can be rename source or not.
22979+ * if it can, return 0.
4a4d8108
AM
22980+ * success means,
22981+ * - it is a logically empty dir.
22982+ * - or, it exists on writable branch and has no children including whiteouts
8b6a4947 22983+ * on the lower branch unless DIRREN is on.
4a4d8108 22984+ */
8b6a4947 22985+static int may_rename_srcdir(struct au_ren_args *a)
4a4d8108
AM
22986+{
22987+ int err;
22988+ unsigned int rdhash;
8b6a4947
AM
22989+ aufs_bindex_t btop, btgt;
22990+ struct dentry *dentry;
22991+ struct super_block *sb;
22992+ struct au_sbinfo *sbinfo;
1facf9fc 22993+
8b6a4947
AM
22994+ dentry = a->src_dentry;
22995+ sb = dentry->d_sb;
22996+ sbinfo = au_sbi(sb);
22997+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
22998+ au_fset_ren(a->auren_flags, DIRREN);
22999+
23000+ btgt = a->btgt;
5afbbe0d
AM
23001+ btop = au_dbtop(dentry);
23002+ if (btop != btgt) {
4a4d8108 23003+ struct au_nhash whlist;
dece6358 23004+
8b6a4947
AM
23005+ SiMustAnyLock(sb);
23006+ rdhash = sbinfo->si_rdhash;
4a4d8108
AM
23007+ if (!rdhash)
23008+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
23009+ dentry));
23010+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
23011+ if (unlikely(err))
23012+ goto out;
23013+ err = au_test_empty(dentry, &whlist);
23014+ au_nhash_wh_free(&whlist);
23015+ goto out;
23016+ }
dece6358 23017+
5afbbe0d 23018+ if (btop == au_dbtaildir(dentry))
4a4d8108 23019+ return 0; /* success */
dece6358 23020+
4a4d8108 23021+ err = au_test_empty_lower(dentry);
1facf9fc 23022+
4f0767ce 23023+out:
4a4d8108 23024+ if (err == -ENOTEMPTY) {
8b6a4947
AM
23025+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23026+ err = 0;
23027+ } else {
23028+ AuWarn1("renaming dir who has child(ren) on multiple "
23029+ "branches, is not supported\n");
23030+ err = -EXDEV;
23031+ }
4a4d8108
AM
23032+ }
23033+ return err;
23034+}
1308ab2a 23035+
4a4d8108
AM
23036+/* side effect: sets whlist and h_dentry */
23037+static int au_ren_may_dir(struct au_ren_args *a)
1308ab2a 23038+{
4a4d8108
AM
23039+ int err;
23040+ unsigned int rdhash;
23041+ struct dentry *d;
1facf9fc 23042+
4a4d8108
AM
23043+ d = a->dst_dentry;
23044+ SiMustAnyLock(d->d_sb);
1facf9fc 23045+
4a4d8108 23046+ err = 0;
f2c43d5f 23047+ if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
4a4d8108
AM
23048+ rdhash = au_sbi(d->d_sb)->si_rdhash;
23049+ if (!rdhash)
23050+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
23051+ err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
23052+ if (unlikely(err))
23053+ goto out;
1308ab2a 23054+
f2c43d5f
AM
23055+ if (!a->exchange) {
23056+ au_set_dbtop(d, a->dst_btop);
23057+ err = may_rename_dstdir(d, &a->whlist);
23058+ au_set_dbtop(d, a->btgt);
23059+ } else
8b6a4947 23060+ err = may_rename_srcdir(a);
4a4d8108 23061+ }
5afbbe0d 23062+ a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
4a4d8108
AM
23063+ if (unlikely(err))
23064+ goto out;
23065+
23066+ d = a->src_dentry;
5afbbe0d 23067+ a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
f2c43d5f 23068+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
8b6a4947 23069+ err = may_rename_srcdir(a);
4a4d8108
AM
23070+ if (unlikely(err)) {
23071+ au_nhash_wh_free(&a->whlist);
23072+ a->whlist.nh_num = 0;
23073+ }
23074+ }
4f0767ce 23075+out:
4a4d8108 23076+ return err;
1facf9fc 23077+}
23078+
4a4d8108 23079+/* ---------------------------------------------------------------------- */
1facf9fc 23080+
4a4d8108
AM
23081+/*
23082+ * simple tests for rename.
23083+ * following the checks in vfs, plus the parent-child relationship.
23084+ */
23085+static int au_may_ren(struct au_ren_args *a)
23086+{
23087+ int err, isdir;
23088+ struct inode *h_inode;
1facf9fc 23089+
5afbbe0d 23090+ if (a->src_btop == a->btgt) {
4a4d8108 23091+ err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
f2c43d5f 23092+ au_ftest_ren(a->auren_flags, ISDIR_SRC));
4a4d8108
AM
23093+ if (unlikely(err))
23094+ goto out;
23095+ err = -EINVAL;
23096+ if (unlikely(a->src_h_dentry == a->h_trap))
23097+ goto out;
23098+ }
1facf9fc 23099+
4a4d8108 23100+ err = 0;
5afbbe0d 23101+ if (a->dst_btop != a->btgt)
4a4d8108 23102+ goto out;
1facf9fc 23103+
027c5e7a
AM
23104+ err = -ENOTEMPTY;
23105+ if (unlikely(a->dst_h_dentry == a->h_trap))
23106+ goto out;
23107+
4a4d8108 23108+ err = -EIO;
f2c43d5f 23109+ isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
5527c038
JR
23110+ if (d_really_is_negative(a->dst_dentry)) {
23111+ if (d_is_negative(a->dst_h_dentry))
23112+ err = au_may_add(a->dst_dentry, a->btgt,
23113+ a->dst_h_parent, isdir);
4a4d8108 23114+ } else {
5527c038 23115+ if (unlikely(d_is_negative(a->dst_h_dentry)))
4a4d8108 23116+ goto out;
5527c038
JR
23117+ h_inode = d_inode(a->dst_h_dentry);
23118+ if (h_inode->i_nlink)
23119+ err = au_may_del(a->dst_dentry, a->btgt,
23120+ a->dst_h_parent, isdir);
4a4d8108 23121+ }
1facf9fc 23122+
4f0767ce 23123+out:
4a4d8108
AM
23124+ if (unlikely(err == -ENOENT || err == -EEXIST))
23125+ err = -EIO;
23126+ AuTraceErr(err);
23127+ return err;
23128+}
1facf9fc 23129+
1308ab2a 23130+/* ---------------------------------------------------------------------- */
1facf9fc 23131+
4a4d8108
AM
23132+/*
23133+ * locking order
23134+ * (VFS)
23135+ * - src_dir and dir by lock_rename()
23136+ * - inode if exitsts
23137+ * (aufs)
23138+ * - lock all
23139+ * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
23140+ * + si_read_lock
23141+ * + di_write_lock2_child()
23142+ * + di_write_lock_child()
23143+ * + ii_write_lock_child()
23144+ * + di_write_lock_child2()
23145+ * + ii_write_lock_child2()
23146+ * + src_parent and parent
23147+ * + di_write_lock_parent()
23148+ * + ii_write_lock_parent()
23149+ * + di_write_lock_parent2()
23150+ * + ii_write_lock_parent2()
23151+ * + lower src_dir and dir by vfsub_lock_rename()
23152+ * + verify the every relationships between child and parent. if any
23153+ * of them failed, unlock all and return -EBUSY.
23154+ */
23155+static void au_ren_unlock(struct au_ren_args *a)
1308ab2a 23156+{
4a4d8108
AM
23157+ vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
23158+ a->dst_h_parent, a->dst_hdir);
8b6a4947
AM
23159+ if (au_ftest_ren(a->auren_flags, DIRREN)
23160+ && a->h_root)
23161+ au_hn_inode_unlock(a->h_root);
f2c43d5f 23162+ if (au_ftest_ren(a->auren_flags, MNT_WRITE))
86dc4139 23163+ vfsub_mnt_drop_write(au_br_mnt(a->br));
1308ab2a 23164+}
23165+
4a4d8108 23166+static int au_ren_lock(struct au_ren_args *a)
1308ab2a 23167+{
4a4d8108
AM
23168+ int err;
23169+ unsigned int udba;
1308ab2a 23170+
4a4d8108
AM
23171+ err = 0;
23172+ a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
23173+ a->src_hdir = au_hi(a->src_dir, a->btgt);
23174+ a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
23175+ a->dst_hdir = au_hi(a->dst_dir, a->btgt);
86dc4139
AM
23176+
23177+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
23178+ if (unlikely(err))
23179+ goto out;
f2c43d5f 23180+ au_fset_ren(a->auren_flags, MNT_WRITE);
8b6a4947
AM
23181+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23182+ struct dentry *root;
23183+ struct inode *dir;
23184+
23185+ /*
23186+ * sbinfo is already locked, so this ii_read_lock is
23187+ * unnecessary. but our debugging feature checks it.
23188+ */
23189+ root = a->src_inode->i_sb->s_root;
23190+ if (root != a->src_parent && root != a->dst_parent) {
23191+ dir = d_inode(root);
23192+ ii_read_lock_parent3(dir);
23193+ a->h_root = au_hi(dir, a->btgt);
23194+ ii_read_unlock(dir);
23195+ au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
23196+ }
23197+ }
4a4d8108
AM
23198+ a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
23199+ a->dst_h_parent, a->dst_hdir);
23200+ udba = au_opt_udba(a->src_dentry->d_sb);
5527c038
JR
23201+ if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
23202+ || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
4a4d8108 23203+ err = au_busy_or_stale();
5afbbe0d 23204+ if (!err && au_dbtop(a->src_dentry) == a->btgt)
4a4d8108 23205+ err = au_h_verify(a->src_h_dentry, udba,
5527c038 23206+ d_inode(a->src_h_parent), a->src_h_parent,
4a4d8108 23207+ a->br);
5afbbe0d 23208+ if (!err && au_dbtop(a->dst_dentry) == a->btgt)
4a4d8108 23209+ err = au_h_verify(a->dst_h_dentry, udba,
5527c038 23210+ d_inode(a->dst_h_parent), a->dst_h_parent,
4a4d8108 23211+ a->br);
86dc4139 23212+ if (!err)
4a4d8108 23213+ goto out; /* success */
4a4d8108
AM
23214+
23215+ err = au_busy_or_stale();
4a4d8108 23216+ au_ren_unlock(a);
86dc4139 23217+
4f0767ce 23218+out:
4a4d8108 23219+ return err;
1facf9fc 23220+}
23221+
23222+/* ---------------------------------------------------------------------- */
23223+
4a4d8108 23224+static void au_ren_refresh_dir(struct au_ren_args *a)
1facf9fc 23225+{
4a4d8108 23226+ struct inode *dir;
dece6358 23227+
4a4d8108
AM
23228+ dir = a->dst_dir;
23229+ dir->i_version++;
f2c43d5f 23230+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
4a4d8108
AM
23231+ /* is this updating defined in POSIX? */
23232+ au_cpup_attr_timesizes(a->src_inode);
23233+ au_cpup_attr_nlink(dir, /*force*/1);
4a4d8108 23234+ }
b912730e 23235+ au_dir_ts(dir, a->btgt);
dece6358 23236+
f2c43d5f
AM
23237+ if (a->exchange) {
23238+ dir = a->src_dir;
23239+ dir->i_version++;
23240+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23241+ /* is this updating defined in POSIX? */
23242+ au_cpup_attr_timesizes(a->dst_inode);
23243+ au_cpup_attr_nlink(dir, /*force*/1);
23244+ }
23245+ au_dir_ts(dir, a->btgt);
23246+ }
23247+
23248+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108 23249+ return;
dece6358 23250+
4a4d8108
AM
23251+ dir = a->src_dir;
23252+ dir->i_version++;
f2c43d5f 23253+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
4a4d8108 23254+ au_cpup_attr_nlink(dir, /*force*/1);
b912730e 23255+ au_dir_ts(dir, a->btgt);
1facf9fc 23256+}
23257+
4a4d8108 23258+static void au_ren_refresh(struct au_ren_args *a)
1facf9fc 23259+{
5afbbe0d 23260+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
23261+ struct dentry *d, *h_d;
23262+ struct inode *i, *h_i;
23263+ struct super_block *sb;
dece6358 23264+
027c5e7a
AM
23265+ d = a->dst_dentry;
23266+ d_drop(d);
23267+ if (a->h_dst)
23268+ /* already dget-ed by au_ren_or_cpup() */
23269+ au_set_h_dptr(d, a->btgt, a->h_dst);
23270+
23271+ i = a->dst_inode;
23272+ if (i) {
f2c43d5f
AM
23273+ if (!a->exchange) {
23274+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
23275+ vfsub_drop_nlink(i);
23276+ else {
23277+ vfsub_dead_dir(i);
23278+ au_cpup_attr_timesizes(i);
23279+ }
23280+ au_update_dbrange(d, /*do_put_zero*/1);
23281+ } else
23282+ au_cpup_attr_nlink(i, /*force*/1);
027c5e7a 23283+ } else {
5afbbe0d
AM
23284+ bbot = a->btgt;
23285+ for (bindex = au_dbtop(d); bindex < bbot; bindex++)
027c5e7a 23286+ au_set_h_dptr(d, bindex, NULL);
5afbbe0d
AM
23287+ bbot = au_dbbot(d);
23288+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
027c5e7a
AM
23289+ au_set_h_dptr(d, bindex, NULL);
23290+ au_update_dbrange(d, /*do_put_zero*/0);
23291+ }
23292+
8b6a4947
AM
23293+ if (a->exchange
23294+ || au_ftest_ren(a->auren_flags, DIRREN)) {
23295+ d_drop(a->src_dentry);
23296+ if (au_ftest_ren(a->auren_flags, DIRREN))
23297+ au_set_dbwh(a->src_dentry, -1);
23298+ return;
23299+ }
23300+
4a4d8108 23301+ d = a->src_dentry;
8b6a4947
AM
23302+ au_set_dbwh(d, -1);
23303+ bbot = au_dbbot(d);
23304+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23305+ h_d = au_h_dptr(d, bindex);
23306+ if (h_d)
23307+ au_set_h_dptr(d, bindex, NULL);
23308+ }
23309+ au_set_dbbot(d, a->btgt);
4a4d8108 23310+
8b6a4947
AM
23311+ sb = d->d_sb;
23312+ i = a->src_inode;
23313+ if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
23314+ return; /* success */
4a4d8108 23315+
8b6a4947
AM
23316+ bbot = au_ibbot(i);
23317+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23318+ h_i = au_h_iptr(i, bindex);
23319+ if (h_i) {
23320+ au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
23321+ /* ignore this error */
23322+ au_set_h_iptr(i, bindex, NULL, 0);
4a4d8108
AM
23323+ }
23324+ }
8b6a4947 23325+ au_set_ibbot(i, a->btgt);
1308ab2a 23326+}
dece6358 23327+
4a4d8108
AM
23328+/* ---------------------------------------------------------------------- */
23329+
23330+/* mainly for link(2) and rename(2) */
23331+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
1308ab2a 23332+{
4a4d8108
AM
23333+ aufs_bindex_t bdiropq, bwh;
23334+ struct dentry *parent;
23335+ struct au_branch *br;
23336+
23337+ parent = dentry->d_parent;
5527c038 23338+ IMustLock(d_inode(parent)); /* dir is locked */
4a4d8108
AM
23339+
23340+ bdiropq = au_dbdiropq(parent);
23341+ bwh = au_dbwh(dentry);
23342+ br = au_sbr(dentry->d_sb, btgt);
23343+ if (au_br_rdonly(br)
23344+ || (0 <= bdiropq && bdiropq < btgt)
23345+ || (0 <= bwh && bwh < btgt))
23346+ btgt = -1;
23347+
23348+ AuDbg("btgt %d\n", btgt);
23349+ return btgt;
1facf9fc 23350+}
23351+
5afbbe0d 23352+/* sets src_btop, dst_btop and btgt */
4a4d8108 23353+static int au_ren_wbr(struct au_ren_args *a)
1facf9fc 23354+{
4a4d8108
AM
23355+ int err;
23356+ struct au_wr_dir_args wr_dir_args = {
23357+ /* .force_btgt = -1, */
23358+ .flags = AuWrDir_ADD_ENTRY
23359+ };
dece6358 23360+
5afbbe0d
AM
23361+ a->src_btop = au_dbtop(a->src_dentry);
23362+ a->dst_btop = au_dbtop(a->dst_dentry);
f2c43d5f
AM
23363+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
23364+ || au_ftest_ren(a->auren_flags, ISDIR_DST))
4a4d8108 23365+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
5afbbe0d
AM
23366+ wr_dir_args.force_btgt = a->src_btop;
23367+ if (a->dst_inode && a->dst_btop < a->src_btop)
23368+ wr_dir_args.force_btgt = a->dst_btop;
4a4d8108
AM
23369+ wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
23370+ err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
23371+ a->btgt = err;
f2c43d5f
AM
23372+ if (a->exchange)
23373+ au_update_dbtop(a->dst_dentry);
dece6358 23374+
4a4d8108 23375+ return err;
1facf9fc 23376+}
23377+
4a4d8108 23378+static void au_ren_dt(struct au_ren_args *a)
1facf9fc 23379+{
4a4d8108
AM
23380+ a->h_path.dentry = a->src_h_parent;
23381+ au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
f2c43d5f 23382+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
4a4d8108
AM
23383+ a->h_path.dentry = a->dst_h_parent;
23384+ au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
23385+ }
1facf9fc 23386+
f2c43d5f
AM
23387+ au_fclr_ren(a->auren_flags, DT_DSTDIR);
23388+ if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
23389+ && !a->exchange)
4a4d8108 23390+ return;
dece6358 23391+
4a4d8108
AM
23392+ a->h_path.dentry = a->src_h_dentry;
23393+ au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
5527c038 23394+ if (d_is_positive(a->dst_h_dentry)) {
f2c43d5f 23395+ au_fset_ren(a->auren_flags, DT_DSTDIR);
4a4d8108
AM
23396+ a->h_path.dentry = a->dst_h_dentry;
23397+ au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
23398+ }
1308ab2a 23399+}
dece6358 23400+
4a4d8108 23401+static void au_ren_rev_dt(int err, struct au_ren_args *a)
1308ab2a 23402+{
4a4d8108 23403+ struct dentry *h_d;
febd17d6 23404+ struct inode *h_inode;
4a4d8108
AM
23405+
23406+ au_dtime_revert(a->src_dt + AuPARENT);
f2c43d5f 23407+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108
AM
23408+ au_dtime_revert(a->dst_dt + AuPARENT);
23409+
f2c43d5f 23410+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
4a4d8108 23411+ h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
23412+ h_inode = d_inode(h_d);
23413+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 23414+ au_dtime_revert(a->src_dt + AuCHILD);
febd17d6 23415+ inode_unlock(h_inode);
4a4d8108 23416+
f2c43d5f 23417+ if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
4a4d8108 23418+ h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
23419+ h_inode = d_inode(h_d);
23420+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 23421+ au_dtime_revert(a->dst_dt + AuCHILD);
febd17d6 23422+ inode_unlock(h_inode);
1facf9fc 23423+ }
23424+ }
23425+}
23426+
4a4d8108
AM
23427+/* ---------------------------------------------------------------------- */
23428+
23429+int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
f2c43d5f
AM
23430+ struct inode *_dst_dir, struct dentry *_dst_dentry,
23431+ unsigned int _flags)
1facf9fc 23432+{
f2c43d5f 23433+ int err, lock_flags;
8b6a4947 23434+ void *rev;
4a4d8108
AM
23435+ /* reduce stack space */
23436+ struct au_ren_args *a;
f2c43d5f 23437+ struct au_pin pin;
4a4d8108 23438+
f2c43d5f 23439+ AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
4a4d8108
AM
23440+ IMustLock(_src_dir);
23441+ IMustLock(_dst_dir);
23442+
f2c43d5f
AM
23443+ err = -EINVAL;
23444+ if (unlikely(_flags & RENAME_WHITEOUT))
23445+ goto out;
23446+
4a4d8108
AM
23447+ err = -ENOMEM;
23448+ BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
23449+ a = kzalloc(sizeof(*a), GFP_NOFS);
23450+ if (unlikely(!a))
23451+ goto out;
23452+
f2c43d5f
AM
23453+ a->flags = _flags;
23454+ a->exchange = _flags & RENAME_EXCHANGE;
4a4d8108
AM
23455+ a->src_dir = _src_dir;
23456+ a->src_dentry = _src_dentry;
5527c038
JR
23457+ a->src_inode = NULL;
23458+ if (d_really_is_positive(a->src_dentry))
23459+ a->src_inode = d_inode(a->src_dentry);
4a4d8108
AM
23460+ a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
23461+ a->dst_dir = _dst_dir;
23462+ a->dst_dentry = _dst_dentry;
5527c038
JR
23463+ a->dst_inode = NULL;
23464+ if (d_really_is_positive(a->dst_dentry))
23465+ a->dst_inode = d_inode(a->dst_dentry);
4a4d8108
AM
23466+ a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
23467+ if (a->dst_inode) {
f2c43d5f
AM
23468+ /*
23469+ * if EXCHANGE && src is non-dir && dst is dir,
23470+ * dst is not locked.
23471+ */
23472+ /* IMustLock(a->dst_inode); */
4a4d8108 23473+ au_igrab(a->dst_inode);
1facf9fc 23474+ }
1facf9fc 23475+
4a4d8108 23476+ err = -ENOTDIR;
f2c43d5f 23477+ lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
2000de60 23478+ if (d_is_dir(a->src_dentry)) {
f2c43d5f
AM
23479+ au_fset_ren(a->auren_flags, ISDIR_SRC);
23480+ if (unlikely(!a->exchange
23481+ && d_really_is_positive(a->dst_dentry)
2000de60 23482+ && !d_is_dir(a->dst_dentry)))
4a4d8108 23483+ goto out_free;
f2c43d5f
AM
23484+ lock_flags |= AuLock_DIRS;
23485+ }
23486+ if (a->dst_inode && d_is_dir(a->dst_dentry)) {
23487+ au_fset_ren(a->auren_flags, ISDIR_DST);
23488+ if (unlikely(!a->exchange
23489+ && d_really_is_positive(a->src_dentry)
23490+ && !d_is_dir(a->src_dentry)))
23491+ goto out_free;
23492+ lock_flags |= AuLock_DIRS;
b95c5147 23493+ }
8b6a4947
AM
23494+ err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
23495+ lock_flags);
e49829fe
JR
23496+ if (unlikely(err))
23497+ goto out_free;
1facf9fc 23498+
027c5e7a
AM
23499+ err = au_d_hashed_positive(a->src_dentry);
23500+ if (unlikely(err))
23501+ goto out_unlock;
23502+ err = -ENOENT;
23503+ if (a->dst_inode) {
23504+ /*
f2c43d5f 23505+ * If it is a dir, VFS unhash it before this
027c5e7a
AM
23506+ * function. It means we cannot rely upon d_unhashed().
23507+ */
23508+ if (unlikely(!a->dst_inode->i_nlink))
23509+ goto out_unlock;
f2c43d5f 23510+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
027c5e7a 23511+ err = au_d_hashed_positive(a->dst_dentry);
f2c43d5f 23512+ if (unlikely(err && !a->exchange))
027c5e7a
AM
23513+ goto out_unlock;
23514+ } else if (unlikely(IS_DEADDIR(a->dst_inode)))
23515+ goto out_unlock;
23516+ } else if (unlikely(d_unhashed(a->dst_dentry)))
23517+ goto out_unlock;
23518+
7eafdf33
AM
23519+ /*
23520+ * is it possible?
79b8bda9 23521+ * yes, it happened (in linux-3.3-rcN) but I don't know why.
7eafdf33
AM
23522+ * there may exist a problem somewhere else.
23523+ */
23524+ err = -EINVAL;
5527c038 23525+ if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
7eafdf33
AM
23526+ goto out_unlock;
23527+
f2c43d5f 23528+ au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
4a4d8108 23529+ di_write_lock_parent(a->dst_parent);
1facf9fc 23530+
4a4d8108
AM
23531+ /* which branch we process */
23532+ err = au_ren_wbr(a);
23533+ if (unlikely(err < 0))
027c5e7a 23534+ goto out_parent;
4a4d8108 23535+ a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
86dc4139 23536+ a->h_path.mnt = au_br_mnt(a->br);
1facf9fc 23537+
4a4d8108
AM
23538+ /* are they available to be renamed */
23539+ err = au_ren_may_dir(a);
23540+ if (unlikely(err))
23541+ goto out_children;
1facf9fc 23542+
4a4d8108 23543+ /* prepare the writable parent dir on the same branch */
5afbbe0d 23544+ if (a->dst_btop == a->btgt) {
f2c43d5f 23545+ au_fset_ren(a->auren_flags, WHDST);
4a4d8108
AM
23546+ } else {
23547+ err = au_cpup_dirs(a->dst_dentry, a->btgt);
23548+ if (unlikely(err))
23549+ goto out_children;
23550+ }
1facf9fc 23551+
f2c43d5f
AM
23552+ err = 0;
23553+ if (!a->exchange) {
23554+ if (a->src_dir != a->dst_dir) {
23555+ /*
23556+ * this temporary unlock is safe,
23557+ * because both dir->i_mutex are locked.
23558+ */
23559+ di_write_unlock(a->dst_parent);
23560+ di_write_lock_parent(a->src_parent);
23561+ err = au_wr_dir_need_wh(a->src_dentry,
23562+ au_ftest_ren(a->auren_flags,
23563+ ISDIR_SRC),
23564+ &a->btgt);
23565+ di_write_unlock(a->src_parent);
23566+ di_write_lock2_parent(a->src_parent, a->dst_parent,
23567+ /*isdir*/1);
23568+ au_fclr_ren(a->auren_flags, ISSAMEDIR);
23569+ } else
23570+ err = au_wr_dir_need_wh(a->src_dentry,
23571+ au_ftest_ren(a->auren_flags,
23572+ ISDIR_SRC),
23573+ &a->btgt);
23574+ }
4a4d8108
AM
23575+ if (unlikely(err < 0))
23576+ goto out_children;
23577+ if (err)
f2c43d5f 23578+ au_fset_ren(a->auren_flags, WHSRC);
1facf9fc 23579+
86dc4139 23580+ /* cpup src */
5afbbe0d 23581+ if (a->src_btop != a->btgt) {
86dc4139
AM
23582+ err = au_pin(&pin, a->src_dentry, a->btgt,
23583+ au_opt_udba(a->src_dentry->d_sb),
23584+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 23585+ if (!err) {
c2b27bf2
AM
23586+ struct au_cp_generic cpg = {
23587+ .dentry = a->src_dentry,
23588+ .bdst = a->btgt,
5afbbe0d 23589+ .bsrc = a->src_btop,
c2b27bf2
AM
23590+ .len = -1,
23591+ .pin = &pin,
23592+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23593+ };
5afbbe0d 23594+ AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
c2b27bf2 23595+ err = au_sio_cpup_simple(&cpg);
367653fa 23596+ au_unpin(&pin);
86dc4139 23597+ }
86dc4139
AM
23598+ if (unlikely(err))
23599+ goto out_children;
5afbbe0d 23600+ a->src_btop = a->btgt;
86dc4139 23601+ a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
f2c43d5f
AM
23602+ if (!a->exchange)
23603+ au_fset_ren(a->auren_flags, WHSRC);
23604+ }
23605+
23606+ /* cpup dst */
23607+ if (a->exchange && a->dst_inode
23608+ && a->dst_btop != a->btgt) {
23609+ err = au_pin(&pin, a->dst_dentry, a->btgt,
23610+ au_opt_udba(a->dst_dentry->d_sb),
23611+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23612+ if (!err) {
23613+ struct au_cp_generic cpg = {
23614+ .dentry = a->dst_dentry,
23615+ .bdst = a->btgt,
23616+ .bsrc = a->dst_btop,
23617+ .len = -1,
23618+ .pin = &pin,
23619+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23620+ };
23621+ err = au_sio_cpup_simple(&cpg);
23622+ au_unpin(&pin);
23623+ }
23624+ if (unlikely(err))
23625+ goto out_children;
23626+ a->dst_btop = a->btgt;
23627+ a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
86dc4139
AM
23628+ }
23629+
4a4d8108
AM
23630+ /* lock them all */
23631+ err = au_ren_lock(a);
23632+ if (unlikely(err))
86dc4139 23633+ /* leave the copied-up one */
4a4d8108 23634+ goto out_children;
1facf9fc 23635+
f2c43d5f
AM
23636+ if (!a->exchange) {
23637+ if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
23638+ err = au_may_ren(a);
23639+ else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
23640+ err = -ENAMETOOLONG;
23641+ if (unlikely(err))
23642+ goto out_hdir;
23643+ }
1facf9fc 23644+
4a4d8108
AM
23645+ /* store timestamps to be revertible */
23646+ au_ren_dt(a);
1facf9fc 23647+
8b6a4947
AM
23648+ /* store dirren info */
23649+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23650+ err = au_dr_rename(a->src_dentry, a->btgt,
23651+ &a->dst_dentry->d_name, &rev);
23652+ AuTraceErr(err);
23653+ if (unlikely(err))
23654+ goto out_dt;
23655+ }
23656+
4a4d8108
AM
23657+ /* here we go */
23658+ err = do_rename(a);
23659+ if (unlikely(err))
8b6a4947
AM
23660+ goto out_dirren;
23661+
23662+ if (au_ftest_ren(a->auren_flags, DIRREN))
23663+ au_dr_rename_fin(a->src_dentry, a->btgt, rev);
4a4d8108
AM
23664+
23665+ /* update dir attributes */
23666+ au_ren_refresh_dir(a);
23667+
23668+ /* dput/iput all lower dentries */
23669+ au_ren_refresh(a);
23670+
23671+ goto out_hdir; /* success */
23672+
8b6a4947
AM
23673+out_dirren:
23674+ if (au_ftest_ren(a->auren_flags, DIRREN))
23675+ au_dr_rename_rev(a->src_dentry, a->btgt, rev);
4f0767ce 23676+out_dt:
4a4d8108 23677+ au_ren_rev_dt(err, a);
4f0767ce 23678+out_hdir:
4a4d8108 23679+ au_ren_unlock(a);
4f0767ce 23680+out_children:
4a4d8108 23681+ au_nhash_wh_free(&a->whlist);
5afbbe0d
AM
23682+ if (err && a->dst_inode && a->dst_btop != a->btgt) {
23683+ AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
027c5e7a 23684+ au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
5afbbe0d 23685+ au_set_dbtop(a->dst_dentry, a->dst_btop);
4a4d8108 23686+ }
027c5e7a 23687+out_parent:
f2c43d5f 23688+ if (!err) {
8b6a4947
AM
23689+ if (d_unhashed(a->src_dentry))
23690+ au_fset_ren(a->auren_flags, DROPPED_SRC);
23691+ if (d_unhashed(a->dst_dentry))
23692+ au_fset_ren(a->auren_flags, DROPPED_DST);
f2c43d5f
AM
23693+ if (!a->exchange)
23694+ d_move(a->src_dentry, a->dst_dentry);
8b6a4947 23695+ else {
f2c43d5f 23696+ d_exchange(a->src_dentry, a->dst_dentry);
8b6a4947
AM
23697+ if (au_ftest_ren(a->auren_flags, DROPPED_DST))
23698+ d_drop(a->dst_dentry);
23699+ }
23700+ if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
23701+ d_drop(a->src_dentry);
f2c43d5f 23702+ } else {
5afbbe0d 23703+ au_update_dbtop(a->dst_dentry);
027c5e7a
AM
23704+ if (!a->dst_inode)
23705+ d_drop(a->dst_dentry);
23706+ }
f2c43d5f 23707+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108
AM
23708+ di_write_unlock(a->dst_parent);
23709+ else
23710+ di_write_unlock2(a->src_parent, a->dst_parent);
027c5e7a 23711+out_unlock:
4a4d8108 23712+ aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
4f0767ce 23713+out_free:
4a4d8108
AM
23714+ iput(a->dst_inode);
23715+ if (a->thargs)
23716+ au_whtmp_rmdir_free(a->thargs);
1c60b727 23717+ kfree(a);
4f0767ce 23718+out:
4a4d8108
AM
23719+ AuTraceErr(err);
23720+ return err;
1308ab2a 23721+}
7f207e10
AM
23722diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
23723--- /usr/share/empty/fs/aufs/Kconfig 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
23724+++ linux/fs/aufs/Kconfig 2017-11-12 22:24:44.697577553 +0100
23725@@ -0,0 +1,198 @@
4a4d8108
AM
23726+config AUFS_FS
23727+ tristate "Aufs (Advanced multi layered unification filesystem) support"
4a4d8108
AM
23728+ help
23729+ Aufs is a stackable unification filesystem such as Unionfs,
23730+ which unifies several directories and provides a merged single
23731+ directory.
23732+ In the early days, aufs was entirely re-designed and
23733+ re-implemented Unionfs Version 1.x series. Introducing many
23734+ original ideas, approaches and improvements, it becomes totally
23735+ different from Unionfs while keeping the basic features.
1facf9fc 23736+
4a4d8108
AM
23737+if AUFS_FS
23738+choice
23739+ prompt "Maximum number of branches"
23740+ default AUFS_BRANCH_MAX_127
23741+ help
23742+ Specifies the maximum number of branches (or member directories)
23743+ in a single aufs. The larger value consumes more system
23744+ resources and has a minor impact to performance.
23745+config AUFS_BRANCH_MAX_127
23746+ bool "127"
23747+ help
23748+ Specifies the maximum number of branches (or member directories)
23749+ in a single aufs. The larger value consumes more system
23750+ resources and has a minor impact to performance.
23751+config AUFS_BRANCH_MAX_511
23752+ bool "511"
23753+ help
23754+ Specifies the maximum number of branches (or member directories)
23755+ in a single aufs. The larger value consumes more system
23756+ resources and has a minor impact to performance.
23757+config AUFS_BRANCH_MAX_1023
23758+ bool "1023"
23759+ help
23760+ Specifies the maximum number of branches (or member directories)
23761+ in a single aufs. The larger value consumes more system
23762+ resources and has a minor impact to performance.
23763+config AUFS_BRANCH_MAX_32767
23764+ bool "32767"
23765+ help
23766+ Specifies the maximum number of branches (or member directories)
23767+ in a single aufs. The larger value consumes more system
23768+ resources and has a minor impact to performance.
23769+endchoice
1facf9fc 23770+
e49829fe
JR
23771+config AUFS_SBILIST
23772+ bool
23773+ depends on AUFS_MAGIC_SYSRQ || PROC_FS
23774+ default y
23775+ help
23776+ Automatic configuration for internal use.
23777+ When aufs supports Magic SysRq or /proc, enabled automatically.
23778+
4a4d8108
AM
23779+config AUFS_HNOTIFY
23780+ bool "Detect direct branch access (bypassing aufs)"
23781+ help
23782+ If you want to modify files on branches directly, eg. bypassing aufs,
23783+ and want aufs to detect the changes of them fully, then enable this
23784+ option and use 'udba=notify' mount option.
7f207e10 23785+ Currently there is only one available configuration, "fsnotify".
4a4d8108
AM
23786+ It will have a negative impact to the performance.
23787+ See detail in aufs.5.
dece6358 23788+
4a4d8108
AM
23789+choice
23790+ prompt "method" if AUFS_HNOTIFY
23791+ default AUFS_HFSNOTIFY
23792+config AUFS_HFSNOTIFY
23793+ bool "fsnotify"
23794+ select FSNOTIFY
4a4d8108 23795+endchoice
1facf9fc 23796+
4a4d8108
AM
23797+config AUFS_EXPORT
23798+ bool "NFS-exportable aufs"
2cbb1c4b 23799+ depends on EXPORTFS
4a4d8108
AM
23800+ help
23801+ If you want to export your mounted aufs via NFS, then enable this
23802+ option. There are several requirements for this configuration.
23803+ See detail in aufs.5.
1facf9fc 23804+
4a4d8108
AM
23805+config AUFS_INO_T_64
23806+ bool
23807+ depends on AUFS_EXPORT
23808+ depends on 64BIT && !(ALPHA || S390)
23809+ default y
23810+ help
23811+ Automatic configuration for internal use.
23812+ /* typedef unsigned long/int __kernel_ino_t */
23813+ /* alpha and s390x are int */
1facf9fc 23814+
c1595e42
JR
23815+config AUFS_XATTR
23816+ bool "support for XATTR/EA (including Security Labels)"
23817+ help
23818+ If your branch fs supports XATTR/EA and you want to make them
23819+ available in aufs too, then enable this opsion and specify the
23820+ branch attributes for EA.
23821+ See detail in aufs.5.
23822+
076b876e
AM
23823+config AUFS_FHSM
23824+ bool "File-based Hierarchical Storage Management"
23825+ help
23826+ Hierarchical Storage Management (or HSM) is a well-known feature
23827+ in the storage world. Aufs provides this feature as file-based.
23828+ with multiple branches.
23829+ These multiple branches are prioritized, ie. the topmost one
23830+ should be the fastest drive and be used heavily.
23831+
4a4d8108
AM
23832+config AUFS_RDU
23833+ bool "Readdir in userspace"
23834+ help
23835+ Aufs has two methods to provide a merged view for a directory,
23836+ by a user-space library and by kernel-space natively. The latter
23837+ is always enabled but sometimes large and slow.
23838+ If you enable this option, install the library in aufs2-util
23839+ package, and set some environment variables for your readdir(3),
23840+ then the work will be handled in user-space which generally
23841+ shows better performance in most cases.
23842+ See detail in aufs.5.
1facf9fc 23843+
8b6a4947
AM
23844+config AUFS_DIRREN
23845+ bool "Workaround for rename(2)-ing a directory"
23846+ help
23847+ By default, aufs returns EXDEV error in renameing a dir who has
23848+ his child on the lower branch, since it is a bad idea to issue
23849+ rename(2) internally for every lower branch. But user may not
23850+ accept this behaviour. So here is a workaround to allow such
23851+ rename(2) and store some extra infromation on the writable
23852+ branch. Obviously this costs high (and I don't like it).
23853+ To use this feature, you need to enable this configuration AND
23854+ to specify the mount option `dirren.'
23855+ See details in aufs.5 and the design documents.
23856+
4a4d8108
AM
23857+config AUFS_SHWH
23858+ bool "Show whiteouts"
23859+ help
23860+ If you want to make the whiteouts in aufs visible, then enable
23861+ this option and specify 'shwh' mount option. Although it may
23862+ sounds like philosophy or something, but in technically it
23863+ simply shows the name of whiteout with keeping its behaviour.
1facf9fc 23864+
4a4d8108
AM
23865+config AUFS_BR_RAMFS
23866+ bool "Ramfs (initramfs/rootfs) as an aufs branch"
23867+ help
23868+ If you want to use ramfs as an aufs branch fs, then enable this
23869+ option. Generally tmpfs is recommended.
23870+ Aufs prohibited them to be a branch fs by default, because
23871+ initramfs becomes unusable after switch_root or something
23872+ generally. If you sets initramfs as an aufs branch and boot your
23873+ system by switch_root, you will meet a problem easily since the
23874+ files in initramfs may be inaccessible.
23875+ Unless you are going to use ramfs as an aufs branch fs without
23876+ switch_root or something, leave it N.
1facf9fc 23877+
4a4d8108
AM
23878+config AUFS_BR_FUSE
23879+ bool "Fuse fs as an aufs branch"
23880+ depends on FUSE_FS
23881+ select AUFS_POLL
23882+ help
23883+ If you want to use fuse-based userspace filesystem as an aufs
23884+ branch fs, then enable this option.
23885+ It implements the internal poll(2) operation which is
23886+ implemented by fuse only (curretnly).
1facf9fc 23887+
4a4d8108
AM
23888+config AUFS_POLL
23889+ bool
23890+ help
23891+ Automatic configuration for internal use.
1facf9fc 23892+
4a4d8108
AM
23893+config AUFS_BR_HFSPLUS
23894+ bool "Hfsplus as an aufs branch"
23895+ depends on HFSPLUS_FS
23896+ default y
23897+ help
23898+ If you want to use hfsplus fs as an aufs branch fs, then enable
23899+ this option. This option introduces a small overhead at
23900+ copying-up a file on hfsplus.
1facf9fc 23901+
4a4d8108
AM
23902+config AUFS_BDEV_LOOP
23903+ bool
23904+ depends on BLK_DEV_LOOP
23905+ default y
23906+ help
23907+ Automatic configuration for internal use.
23908+ Convert =[ym] into =y.
1308ab2a 23909+
4a4d8108
AM
23910+config AUFS_DEBUG
23911+ bool "Debug aufs"
23912+ help
23913+ Enable this to compile aufs internal debug code.
23914+ It will have a negative impact to the performance.
23915+
23916+config AUFS_MAGIC_SYSRQ
23917+ bool
23918+ depends on AUFS_DEBUG && MAGIC_SYSRQ
23919+ default y
23920+ help
23921+ Automatic configuration for internal use.
23922+ When aufs supports Magic SysRq, enabled automatically.
23923+endif
7f207e10
AM
23924diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
23925--- /usr/share/empty/fs/aufs/loop.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947 23926+++ linux/fs/aufs/loop.c 2017-11-12 22:24:42.270843503 +0100
e2f27e51 23927@@ -0,0 +1,147 @@
1facf9fc 23928+/*
a2654f78 23929+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 23930+ *
23931+ * This program, aufs is free software; you can redistribute it and/or modify
23932+ * it under the terms of the GNU General Public License as published by
23933+ * the Free Software Foundation; either version 2 of the License, or
23934+ * (at your option) any later version.
dece6358
AM
23935+ *
23936+ * This program is distributed in the hope that it will be useful,
23937+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
23938+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23939+ * GNU General Public License for more details.
23940+ *
23941+ * You should have received a copy of the GNU General Public License
523b37e3 23942+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 23943+ */
23944+
23945+/*
23946+ * support for loopback block device as a branch
23947+ */
23948+
1facf9fc 23949+#include "aufs.h"
23950+
392086de
AM
23951+/* added into drivers/block/loop.c */
23952+static struct file *(*backing_file_func)(struct super_block *sb);
23953+
1facf9fc 23954+/*
23955+ * test if two lower dentries have overlapping branches.
23956+ */
b752ccd1 23957+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
1facf9fc 23958+{
b752ccd1 23959+ struct super_block *h_sb;
392086de
AM
23960+ struct file *backing_file;
23961+
23962+ if (unlikely(!backing_file_func)) {
23963+ /* don't load "loop" module here */
23964+ backing_file_func = symbol_get(loop_backing_file);
23965+ if (unlikely(!backing_file_func))
23966+ /* "loop" module is not loaded */
23967+ return 0;
23968+ }
1facf9fc 23969+
b752ccd1 23970+ h_sb = h_adding->d_sb;
392086de
AM
23971+ backing_file = backing_file_func(h_sb);
23972+ if (!backing_file)
1facf9fc 23973+ return 0;
23974+
2000de60 23975+ h_adding = backing_file->f_path.dentry;
b752ccd1
AM
23976+ /*
23977+ * h_adding can be local NFS.
23978+ * in this case aufs cannot detect the loop.
23979+ */
23980+ if (unlikely(h_adding->d_sb == sb))
1facf9fc 23981+ return 1;
b752ccd1 23982+ return !!au_test_subdir(h_adding, sb->s_root);
1facf9fc 23983+}
23984+
23985+/* true if a kernel thread named 'loop[0-9].*' accesses a file */
23986+int au_test_loopback_kthread(void)
23987+{
b752ccd1
AM
23988+ int ret;
23989+ struct task_struct *tsk = current;
a2a7ad62 23990+ char c, comm[sizeof(tsk->comm)];
b752ccd1
AM
23991+
23992+ ret = 0;
23993+ if (tsk->flags & PF_KTHREAD) {
a2a7ad62
AM
23994+ get_task_comm(comm, tsk);
23995+ c = comm[4];
b752ccd1 23996+ ret = ('0' <= c && c <= '9'
a2a7ad62 23997+ && !strncmp(comm, "loop", 4));
b752ccd1 23998+ }
1facf9fc 23999+
b752ccd1 24000+ return ret;
1facf9fc 24001+}
87a755f4
AM
24002+
24003+/* ---------------------------------------------------------------------- */
24004+
24005+#define au_warn_loopback_step 16
24006+static int au_warn_loopback_nelem = au_warn_loopback_step;
24007+static unsigned long *au_warn_loopback_array;
24008+
24009+void au_warn_loopback(struct super_block *h_sb)
24010+{
24011+ int i, new_nelem;
24012+ unsigned long *a, magic;
24013+ static DEFINE_SPINLOCK(spin);
24014+
24015+ magic = h_sb->s_magic;
24016+ spin_lock(&spin);
24017+ a = au_warn_loopback_array;
24018+ for (i = 0; i < au_warn_loopback_nelem && *a; i++)
24019+ if (a[i] == magic) {
24020+ spin_unlock(&spin);
24021+ return;
24022+ }
24023+
24024+ /* h_sb is new to us, print it */
24025+ if (i < au_warn_loopback_nelem) {
24026+ a[i] = magic;
24027+ goto pr;
24028+ }
24029+
24030+ /* expand the array */
24031+ new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
24032+ a = au_kzrealloc(au_warn_loopback_array,
24033+ au_warn_loopback_nelem * sizeof(unsigned long),
e2f27e51
AM
24034+ new_nelem * sizeof(unsigned long), GFP_ATOMIC,
24035+ /*may_shrink*/0);
87a755f4
AM
24036+ if (a) {
24037+ au_warn_loopback_nelem = new_nelem;
24038+ au_warn_loopback_array = a;
24039+ a[i] = magic;
24040+ goto pr;
24041+ }
24042+
24043+ spin_unlock(&spin);
24044+ AuWarn1("realloc failed, ignored\n");
24045+ return;
24046+
24047+pr:
24048+ spin_unlock(&spin);
0c3ec466
AM
24049+ pr_warn("you may want to try another patch for loopback file "
24050+ "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
87a755f4
AM
24051+}
24052+
24053+int au_loopback_init(void)
24054+{
24055+ int err;
24056+ struct super_block *sb __maybe_unused;
24057+
79b8bda9 24058+ BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(unsigned long));
87a755f4
AM
24059+
24060+ err = 0;
24061+ au_warn_loopback_array = kcalloc(au_warn_loopback_step,
24062+ sizeof(unsigned long), GFP_NOFS);
24063+ if (unlikely(!au_warn_loopback_array))
24064+ err = -ENOMEM;
24065+
24066+ return err;
24067+}
24068+
24069+void au_loopback_fin(void)
24070+{
79b8bda9
AM
24071+ if (backing_file_func)
24072+ symbol_put(loop_backing_file);
1c60b727 24073+ kfree(au_warn_loopback_array);
87a755f4 24074+}
7f207e10
AM
24075diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
24076--- /usr/share/empty/fs/aufs/loop.h 1970-01-01 01:00:00.000000000 +0100
1c60b727 24077+++ linux/fs/aufs/loop.h 2017-07-29 12:14:25.903042072 +0200
523b37e3 24078@@ -0,0 +1,52 @@
1facf9fc 24079+/*
a2654f78 24080+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 24081+ *
24082+ * This program, aufs is free software; you can redistribute it and/or modify
24083+ * it under the terms of the GNU General Public License as published by
24084+ * the Free Software Foundation; either version 2 of the License, or
24085+ * (at your option) any later version.
dece6358
AM
24086+ *
24087+ * This program is distributed in the hope that it will be useful,
24088+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24089+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24090+ * GNU General Public License for more details.
24091+ *
24092+ * You should have received a copy of the GNU General Public License
523b37e3 24093+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24094+ */
24095+
24096+/*
24097+ * support for loopback mount as a branch
24098+ */
24099+
24100+#ifndef __AUFS_LOOP_H__
24101+#define __AUFS_LOOP_H__
24102+
24103+#ifdef __KERNEL__
24104+
dece6358
AM
24105+struct dentry;
24106+struct super_block;
1facf9fc 24107+
24108+#ifdef CONFIG_AUFS_BDEV_LOOP
392086de
AM
24109+/* drivers/block/loop.c */
24110+struct file *loop_backing_file(struct super_block *sb);
24111+
1facf9fc 24112+/* loop.c */
b752ccd1 24113+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
1facf9fc 24114+int au_test_loopback_kthread(void);
87a755f4
AM
24115+void au_warn_loopback(struct super_block *h_sb);
24116+
24117+int au_loopback_init(void);
24118+void au_loopback_fin(void);
1facf9fc 24119+#else
4a4d8108 24120+AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
b752ccd1 24121+ struct dentry *h_adding)
4a4d8108 24122+AuStubInt0(au_test_loopback_kthread, void)
87a755f4
AM
24123+AuStubVoid(au_warn_loopback, struct super_block *h_sb)
24124+
24125+AuStubInt0(au_loopback_init, void)
24126+AuStubVoid(au_loopback_fin, void)
1facf9fc 24127+#endif /* BLK_DEV_LOOP */
24128+
24129+#endif /* __KERNEL__ */
24130+#endif /* __AUFS_LOOP_H__ */
7f207e10
AM
24131diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
24132--- /usr/share/empty/fs/aufs/magic.mk 1970-01-01 01:00:00.000000000 +0100
1c60b727 24133+++ linux/fs/aufs/magic.mk 2017-07-29 12:14:25.903042072 +0200
7e9cd9fe 24134@@ -0,0 +1,30 @@
1facf9fc 24135+
24136+# defined in ${srctree}/fs/fuse/inode.c
24137+# tristate
24138+ifdef CONFIG_FUSE_FS
24139+ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
24140+endif
24141+
1facf9fc 24142+# defined in ${srctree}/fs/xfs/xfs_sb.h
24143+# tristate
24144+ifdef CONFIG_XFS_FS
24145+ccflags-y += -DXFS_SB_MAGIC=0x58465342
24146+endif
24147+
24148+# defined in ${srctree}/fs/configfs/mount.c
24149+# tristate
24150+ifdef CONFIG_CONFIGFS_FS
24151+ccflags-y += -DCONFIGFS_MAGIC=0x62656570
24152+endif
24153+
1facf9fc 24154+# defined in ${srctree}/fs/ubifs/ubifs.h
24155+# tristate
24156+ifdef CONFIG_UBIFS_FS
24157+ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
24158+endif
4a4d8108
AM
24159+
24160+# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
24161+# tristate
24162+ifdef CONFIG_HFSPLUS_FS
24163+ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
24164+endif
7f207e10
AM
24165diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
24166--- /usr/share/empty/fs/aufs/Makefile 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
24167+++ linux/fs/aufs/Makefile 2017-11-12 22:24:44.697577553 +0100
24168@@ -0,0 +1,45 @@
4a4d8108
AM
24169+
24170+include ${src}/magic.mk
24171+ifeq (${CONFIG_AUFS_FS},m)
24172+include ${src}/conf.mk
24173+endif
24174+-include ${src}/priv_def.mk
24175+
24176+# cf. include/linux/kernel.h
24177+# enable pr_debug
24178+ccflags-y += -DDEBUG
f6c5ef8b
AM
24179+# sparse requires the full pathname
24180+ifdef M
523b37e3 24181+ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
f6c5ef8b 24182+else
523b37e3 24183+ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
f6c5ef8b 24184+endif
4a4d8108
AM
24185+
24186+obj-$(CONFIG_AUFS_FS) += aufs.o
24187+aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
24188+ wkq.o vfsub.o dcsub.o \
e49829fe 24189+ cpup.o whout.o wbr_policy.o \
4a4d8108
AM
24190+ dinfo.o dentry.o \
24191+ dynop.o \
24192+ finfo.o file.o f_op.o \
24193+ dir.o vdir.o \
24194+ iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
c2b27bf2 24195+ mvdown.o ioctl.o
4a4d8108
AM
24196+
24197+# all are boolean
e49829fe 24198+aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
4a4d8108
AM
24199+aufs-$(CONFIG_SYSFS) += sysfs.o
24200+aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
24201+aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
24202+aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
24203+aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
4a4d8108 24204+aufs-$(CONFIG_AUFS_EXPORT) += export.o
c1595e42
JR
24205+aufs-$(CONFIG_AUFS_XATTR) += xattr.o
24206+aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
8b6a4947 24207+aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
076b876e 24208+aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
4a4d8108
AM
24209+aufs-$(CONFIG_AUFS_POLL) += poll.o
24210+aufs-$(CONFIG_AUFS_RDU) += rdu.o
4a4d8108
AM
24211+aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
24212+aufs-$(CONFIG_AUFS_DEBUG) += debug.o
24213+aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
7f207e10
AM
24214diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
24215--- /usr/share/empty/fs/aufs/module.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947 24216+++ linux/fs/aufs/module.c 2017-11-12 22:24:44.707577830 +0100
1c60b727 24217@@ -0,0 +1,266 @@
1facf9fc 24218+/*
a2654f78 24219+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 24220+ *
24221+ * This program, aufs is free software; you can redistribute it and/or modify
24222+ * it under the terms of the GNU General Public License as published by
24223+ * the Free Software Foundation; either version 2 of the License, or
24224+ * (at your option) any later version.
dece6358
AM
24225+ *
24226+ * This program is distributed in the hope that it will be useful,
24227+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24228+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24229+ * GNU General Public License for more details.
24230+ *
24231+ * You should have received a copy of the GNU General Public License
523b37e3 24232+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24233+ */
24234+
24235+/*
24236+ * module global variables and operations
24237+ */
24238+
24239+#include <linux/module.h>
24240+#include <linux/seq_file.h>
24241+#include "aufs.h"
24242+
e2f27e51
AM
24243+/* shrinkable realloc */
24244+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
1facf9fc 24245+{
e2f27e51
AM
24246+ size_t sz;
24247+ int diff;
1facf9fc 24248+
e2f27e51
AM
24249+ sz = 0;
24250+ diff = -1;
24251+ if (p) {
24252+#if 0 /* unused */
24253+ if (!new_sz) {
1c60b727 24254+ kfree(p);
e2f27e51
AM
24255+ p = NULL;
24256+ goto out;
24257+ }
24258+#else
24259+ AuDebugOn(!new_sz);
24260+#endif
24261+ sz = ksize(p);
24262+ diff = au_kmidx_sub(sz, new_sz);
24263+ }
24264+ if (sz && !diff)
24265+ goto out;
24266+
24267+ if (sz < new_sz)
24268+ /* expand or SLOB */
24269+ p = krealloc(p, new_sz, gfp);
24270+ else if (new_sz < sz && may_shrink) {
24271+ /* shrink */
24272+ void *q;
24273+
24274+ q = kmalloc(new_sz, gfp);
24275+ if (q) {
24276+ if (p) {
24277+ memcpy(q, p, new_sz);
1c60b727 24278+ kfree(p);
e2f27e51
AM
24279+ }
24280+ p = q;
24281+ } else
24282+ p = NULL;
24283+ }
24284+
24285+out:
24286+ return p;
24287+}
24288+
24289+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24290+ int may_shrink)
24291+{
24292+ p = au_krealloc(p, new_sz, gfp, may_shrink);
24293+ if (p && new_sz > nused)
1facf9fc 24294+ memset(p + nused, 0, new_sz - nused);
24295+ return p;
24296+}
24297+
24298+/* ---------------------------------------------------------------------- */
1facf9fc 24299+/*
24300+ * aufs caches
24301+ */
1c60b727 24302+struct kmem_cache *au_cache[AuCache_Last];
5afbbe0d
AM
24303+
24304+static void au_cache_fin(void)
24305+{
24306+ int i;
24307+
24308+ /*
24309+ * Make sure all delayed rcu free inodes are flushed before we
24310+ * destroy cache.
24311+ */
24312+ rcu_barrier();
24313+
24314+ /* excluding AuCache_HNOTIFY */
24315+ BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
24316+ for (i = 0; i < AuCache_HNOTIFY; i++) {
1c60b727
AM
24317+ kmem_cache_destroy(au_cache[i]);
24318+ au_cache[i] = NULL;
5afbbe0d
AM
24319+ }
24320+}
24321+
1facf9fc 24322+static int __init au_cache_init(void)
24323+{
1c60b727
AM
24324+ au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
24325+ if (au_cache[AuCache_DINFO])
027c5e7a 24326+ /* SLAB_DESTROY_BY_RCU */
1c60b727 24327+ au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
f0c0a007 24328+ au_icntnr_init_once);
1c60b727
AM
24329+ if (au_cache[AuCache_ICNTNR])
24330+ au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
f0c0a007 24331+ au_fi_init_once);
1c60b727
AM
24332+ if (au_cache[AuCache_FINFO])
24333+ au_cache[AuCache_VDIR] = AuCache(au_vdir);
24334+ if (au_cache[AuCache_VDIR])
24335+ au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
24336+ if (au_cache[AuCache_DEHSTR])
1facf9fc 24337+ return 0;
24338+
5afbbe0d 24339+ au_cache_fin();
1facf9fc 24340+ return -ENOMEM;
24341+}
24342+
1facf9fc 24343+/* ---------------------------------------------------------------------- */
24344+
24345+int au_dir_roflags;
24346+
e49829fe 24347+#ifdef CONFIG_AUFS_SBILIST
1e00d052
AM
24348+/*
24349+ * iterate_supers_type() doesn't protect us from
24350+ * remounting (branch management)
24351+ */
8b6a4947 24352+struct hlist_bl_head au_sbilist;
e49829fe
JR
24353+#endif
24354+
1facf9fc 24355+/*
24356+ * functions for module interface.
24357+ */
24358+MODULE_LICENSE("GPL");
24359+/* MODULE_LICENSE("GPL v2"); */
dece6358 24360+MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
1facf9fc 24361+MODULE_DESCRIPTION(AUFS_NAME
24362+ " -- Advanced multi layered unification filesystem");
24363+MODULE_VERSION(AUFS_VERSION);
c06a8ce3 24364+MODULE_ALIAS_FS(AUFS_NAME);
1facf9fc 24365+
1facf9fc 24366+/* this module parameter has no meaning when SYSFS is disabled */
24367+int sysaufs_brs = 1;
24368+MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
24369+module_param_named(brs, sysaufs_brs, int, S_IRUGO);
24370+
076b876e 24371+/* this module parameter has no meaning when USER_NS is disabled */
8cdd5066 24372+bool au_userns;
076b876e
AM
24373+MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
24374+module_param_named(allow_userns, au_userns, bool, S_IRUGO);
24375+
1facf9fc 24376+/* ---------------------------------------------------------------------- */
24377+
24378+static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
24379+
24380+int au_seq_path(struct seq_file *seq, struct path *path)
24381+{
79b8bda9
AM
24382+ int err;
24383+
24384+ err = seq_path(seq, path, au_esc_chars);
1c60b727 24385+ if (err >= 0)
79b8bda9 24386+ err = 0;
1c60b727 24387+ else
79b8bda9
AM
24388+ err = -ENOMEM;
24389+
24390+ return err;
1facf9fc 24391+}
24392+
24393+/* ---------------------------------------------------------------------- */
24394+
24395+static int __init aufs_init(void)
24396+{
24397+ int err, i;
24398+ char *p;
24399+
24400+ p = au_esc_chars;
24401+ for (i = 1; i <= ' '; i++)
24402+ *p++ = i;
24403+ *p++ = '\\';
24404+ *p++ = '\x7f';
24405+ *p = 0;
24406+
24407+ au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
24408+
b95c5147
AM
24409+ memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
24410+ for (i = 0; i < AuIop_Last; i++)
24411+ aufs_iop_nogetattr[i].getattr = NULL;
24412+
1c60b727 24413+ memset(au_cache, 0, sizeof(au_cache)); /* including hnotify */
f0c0a007 24414+
e49829fe 24415+ au_sbilist_init();
1facf9fc 24416+ sysaufs_brs_init();
24417+ au_debug_init();
4a4d8108 24418+ au_dy_init();
1facf9fc 24419+ err = sysaufs_init();
24420+ if (unlikely(err))
24421+ goto out;
e49829fe 24422+ err = au_procfs_init();
4f0767ce 24423+ if (unlikely(err))
953406b4 24424+ goto out_sysaufs;
e49829fe
JR
24425+ err = au_wkq_init();
24426+ if (unlikely(err))
24427+ goto out_procfs;
87a755f4 24428+ err = au_loopback_init();
1facf9fc 24429+ if (unlikely(err))
24430+ goto out_wkq;
87a755f4
AM
24431+ err = au_hnotify_init();
24432+ if (unlikely(err))
24433+ goto out_loopback;
1facf9fc 24434+ err = au_sysrq_init();
24435+ if (unlikely(err))
24436+ goto out_hin;
24437+ err = au_cache_init();
24438+ if (unlikely(err))
24439+ goto out_sysrq;
076b876e
AM
24440+
24441+ aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
1facf9fc 24442+ err = register_filesystem(&aufs_fs_type);
24443+ if (unlikely(err))
24444+ goto out_cache;
076b876e 24445+
4a4d8108
AM
24446+ /* since we define pr_fmt, call printk directly */
24447+ printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
1facf9fc 24448+ goto out; /* success */
24449+
4f0767ce 24450+out_cache:
1facf9fc 24451+ au_cache_fin();
4f0767ce 24452+out_sysrq:
1facf9fc 24453+ au_sysrq_fin();
4f0767ce 24454+out_hin:
4a4d8108 24455+ au_hnotify_fin();
87a755f4
AM
24456+out_loopback:
24457+ au_loopback_fin();
4f0767ce 24458+out_wkq:
1facf9fc 24459+ au_wkq_fin();
e49829fe
JR
24460+out_procfs:
24461+ au_procfs_fin();
4f0767ce 24462+out_sysaufs:
1facf9fc 24463+ sysaufs_fin();
4a4d8108 24464+ au_dy_fin();
4f0767ce 24465+out:
1facf9fc 24466+ return err;
24467+}
24468+
24469+static void __exit aufs_exit(void)
24470+{
24471+ unregister_filesystem(&aufs_fs_type);
24472+ au_cache_fin();
24473+ au_sysrq_fin();
4a4d8108 24474+ au_hnotify_fin();
87a755f4 24475+ au_loopback_fin();
1facf9fc 24476+ au_wkq_fin();
e49829fe 24477+ au_procfs_fin();
1facf9fc 24478+ sysaufs_fin();
4a4d8108 24479+ au_dy_fin();
1facf9fc 24480+}
24481+
24482+module_init(aufs_init);
24483+module_exit(aufs_exit);
7f207e10
AM
24484diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
24485--- /usr/share/empty/fs/aufs/module.h 1970-01-01 01:00:00.000000000 +0100
8b6a4947 24486+++ linux/fs/aufs/module.h 2017-11-12 22:24:42.270843503 +0100
1c60b727 24487@@ -0,0 +1,101 @@
1facf9fc 24488+/*
a2654f78 24489+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 24490+ *
24491+ * This program, aufs is free software; you can redistribute it and/or modify
24492+ * it under the terms of the GNU General Public License as published by
24493+ * the Free Software Foundation; either version 2 of the License, or
24494+ * (at your option) any later version.
dece6358
AM
24495+ *
24496+ * This program is distributed in the hope that it will be useful,
24497+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24498+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24499+ * GNU General Public License for more details.
24500+ *
24501+ * You should have received a copy of the GNU General Public License
523b37e3 24502+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24503+ */
24504+
24505+/*
24506+ * module initialization and module-global
24507+ */
24508+
24509+#ifndef __AUFS_MODULE_H__
24510+#define __AUFS_MODULE_H__
24511+
24512+#ifdef __KERNEL__
24513+
24514+#include <linux/slab.h>
24515+
dece6358
AM
24516+struct path;
24517+struct seq_file;
24518+
1facf9fc 24519+/* module parameters */
1facf9fc 24520+extern int sysaufs_brs;
8cdd5066 24521+extern bool au_userns;
1facf9fc 24522+
24523+/* ---------------------------------------------------------------------- */
24524+
24525+extern int au_dir_roflags;
24526+
e2f27e51
AM
24527+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
24528+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24529+ int may_shrink);
24530+
24531+static inline int au_kmidx_sub(size_t sz, size_t new_sz)
24532+{
24533+#ifndef CONFIG_SLOB
24534+ return kmalloc_index(sz) - kmalloc_index(new_sz);
24535+#else
24536+ return -1; /* SLOB is untested */
24537+#endif
24538+}
24539+
1facf9fc 24540+int au_seq_path(struct seq_file *seq, struct path *path);
24541+
e49829fe
JR
24542+#ifdef CONFIG_PROC_FS
24543+/* procfs.c */
24544+int __init au_procfs_init(void);
24545+void au_procfs_fin(void);
24546+#else
24547+AuStubInt0(au_procfs_init, void);
24548+AuStubVoid(au_procfs_fin, void);
24549+#endif
24550+
4f0767ce
JR
24551+/* ---------------------------------------------------------------------- */
24552+
1c60b727 24553+/* kmem cache */
1facf9fc 24554+enum {
24555+ AuCache_DINFO,
24556+ AuCache_ICNTNR,
24557+ AuCache_FINFO,
24558+ AuCache_VDIR,
24559+ AuCache_DEHSTR,
7eafdf33 24560+ AuCache_HNOTIFY, /* must be last */
1facf9fc 24561+ AuCache_Last
24562+};
24563+
1c60b727 24564+extern struct kmem_cache *au_cache[AuCache_Last];
f0c0a007 24565+
4a4d8108
AM
24566+#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
24567+#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
24568+#define AuCacheCtor(type, ctor) \
24569+ kmem_cache_create(#type, sizeof(struct type), \
24570+ __alignof__(struct type), AuCacheFlags, ctor)
1facf9fc 24571+
1facf9fc 24572+#define AuCacheFuncs(name, index) \
4a4d8108 24573+static inline struct au_##name *au_cache_alloc_##name(void) \
1c60b727 24574+{ return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
4a4d8108 24575+static inline void au_cache_free_##name(struct au_##name *p) \
1c60b727 24576+{ kmem_cache_free(au_cache[AuCache_##index], p); }
1facf9fc 24577+
24578+AuCacheFuncs(dinfo, DINFO);
24579+AuCacheFuncs(icntnr, ICNTNR);
24580+AuCacheFuncs(finfo, FINFO);
24581+AuCacheFuncs(vdir, VDIR);
4a4d8108
AM
24582+AuCacheFuncs(vdir_dehstr, DEHSTR);
24583+#ifdef CONFIG_AUFS_HNOTIFY
24584+AuCacheFuncs(hnotify, HNOTIFY);
24585+#endif
1facf9fc 24586+
4a4d8108
AM
24587+#endif /* __KERNEL__ */
24588+#endif /* __AUFS_MODULE_H__ */
c2b27bf2
AM
24589diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
24590--- /usr/share/empty/fs/aufs/mvdown.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947 24591+++ linux/fs/aufs/mvdown.c 2017-11-12 22:24:44.707577830 +0100
5afbbe0d 24592@@ -0,0 +1,704 @@
c2b27bf2 24593+/*
a2654f78 24594+ * Copyright (C) 2011-2017 Junjiro R. Okajima
c2b27bf2
AM
24595+ *
24596+ * This program, aufs is free software; you can redistribute it and/or modify
24597+ * it under the terms of the GNU General Public License as published by
24598+ * the Free Software Foundation; either version 2 of the License, or
24599+ * (at your option) any later version.
24600+ *
24601+ * This program is distributed in the hope that it will be useful,
24602+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24603+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24604+ * GNU General Public License for more details.
24605+ *
24606+ * You should have received a copy of the GNU General Public License
523b37e3
AM
24607+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
24608+ */
24609+
24610+/*
24611+ * move-down, opposite of copy-up
c2b27bf2
AM
24612+ */
24613+
24614+#include "aufs.h"
24615+
c2b27bf2
AM
24616+struct au_mvd_args {
24617+ struct {
c2b27bf2
AM
24618+ struct super_block *h_sb;
24619+ struct dentry *h_parent;
24620+ struct au_hinode *hdir;
392086de 24621+ struct inode *h_dir, *h_inode;
c1595e42 24622+ struct au_pin pin;
c2b27bf2
AM
24623+ } info[AUFS_MVDOWN_NARRAY];
24624+
24625+ struct aufs_mvdown mvdown;
24626+ struct dentry *dentry, *parent;
24627+ struct inode *inode, *dir;
24628+ struct super_block *sb;
24629+ aufs_bindex_t bopq, bwh, bfound;
24630+ unsigned char rename_lock;
c2b27bf2
AM
24631+};
24632+
392086de 24633+#define mvd_errno mvdown.au_errno
076b876e
AM
24634+#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
24635+#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
24636+#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
24637+#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
c2b27bf2 24638+
392086de
AM
24639+#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
24640+#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
24641+#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
24642+#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
24643+#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
c1595e42 24644+#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
392086de
AM
24645+
24646+#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
24647+#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
24648+#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
24649+#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
24650+#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
c1595e42 24651+#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
c2b27bf2
AM
24652+
24653+#define AU_MVD_PR(flag, ...) do { \
24654+ if (flag) \
24655+ pr_err(__VA_ARGS__); \
24656+ } while (0)
24657+
076b876e
AM
24658+static int find_lower_writable(struct au_mvd_args *a)
24659+{
24660+ struct super_block *sb;
5afbbe0d 24661+ aufs_bindex_t bindex, bbot;
076b876e
AM
24662+ struct au_branch *br;
24663+
24664+ sb = a->sb;
24665+ bindex = a->mvd_bsrc;
5afbbe0d 24666+ bbot = au_sbbot(sb);
076b876e 24667+ if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
5afbbe0d 24668+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
24669+ br = au_sbr(sb, bindex);
24670+ if (au_br_fhsm(br->br_perm)
8b6a4947 24671+ && !sb_rdonly(au_br_sb(br)))
076b876e
AM
24672+ return bindex;
24673+ }
24674+ else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
5afbbe0d 24675+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
24676+ br = au_sbr(sb, bindex);
24677+ if (!au_br_rdonly(br))
24678+ return bindex;
24679+ }
24680+ else
5afbbe0d 24681+ for (bindex++; bindex <= bbot; bindex++) {
076b876e 24682+ br = au_sbr(sb, bindex);
8b6a4947 24683+ if (!sb_rdonly(au_br_sb(br))) {
076b876e
AM
24684+ if (au_br_rdonly(br))
24685+ a->mvdown.flags
24686+ |= AUFS_MVDOWN_ROLOWER_R;
24687+ return bindex;
24688+ }
24689+ }
24690+
24691+ return -1;
24692+}
24693+
c2b27bf2 24694+/* make the parent dir on bdst */
392086de 24695+static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24696+{
24697+ int err;
24698+
24699+ err = 0;
24700+ a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
24701+ a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
24702+ a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
24703+ a->mvd_h_dst_parent = NULL;
5afbbe0d 24704+ if (au_dbbot(a->parent) >= a->mvd_bdst)
c2b27bf2
AM
24705+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
24706+ if (!a->mvd_h_dst_parent) {
24707+ err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
24708+ if (unlikely(err)) {
392086de 24709+ AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
c2b27bf2
AM
24710+ goto out;
24711+ }
24712+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
24713+ }
24714+
24715+out:
24716+ AuTraceErr(err);
24717+ return err;
24718+}
24719+
24720+/* lock them all */
392086de 24721+static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24722+{
24723+ int err;
24724+ struct dentry *h_trap;
24725+
24726+ a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
24727+ a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
c1595e42
JR
24728+ err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
24729+ au_opt_udba(a->sb),
24730+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
24731+ AuTraceErr(err);
24732+ if (unlikely(err)) {
24733+ AU_MVD_PR(dmsg, "pin_dst failed\n");
24734+ goto out;
24735+ }
24736+
c2b27bf2
AM
24737+ if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
24738+ a->rename_lock = 0;
c1595e42
JR
24739+ au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
24740+ AuLsc_DI_PARENT, AuLsc_I_PARENT3,
24741+ au_opt_udba(a->sb),
24742+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
24743+ err = au_do_pin(&a->mvd_pin_src);
24744+ AuTraceErr(err);
5527c038 24745+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
24746+ if (unlikely(err)) {
24747+ AU_MVD_PR(dmsg, "pin_src failed\n");
24748+ goto out_dst;
24749+ }
24750+ goto out; /* success */
c2b27bf2
AM
24751+ }
24752+
c2b27bf2 24753+ a->rename_lock = 1;
c1595e42
JR
24754+ au_pin_hdir_unlock(&a->mvd_pin_dst);
24755+ err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
24756+ au_opt_udba(a->sb),
24757+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
24758+ AuTraceErr(err);
5527c038 24759+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
24760+ if (unlikely(err)) {
24761+ AU_MVD_PR(dmsg, "pin_src failed\n");
24762+ au_pin_hdir_lock(&a->mvd_pin_dst);
24763+ goto out_dst;
24764+ }
24765+ au_pin_hdir_unlock(&a->mvd_pin_src);
c2b27bf2
AM
24766+ h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
24767+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
24768+ if (h_trap) {
24769+ err = (h_trap != a->mvd_h_src_parent);
24770+ if (err)
24771+ err = (h_trap != a->mvd_h_dst_parent);
24772+ }
24773+ BUG_ON(err); /* it should never happen */
c1595e42
JR
24774+ if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
24775+ err = -EBUSY;
24776+ AuTraceErr(err);
24777+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
24778+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
24779+ au_pin_hdir_lock(&a->mvd_pin_src);
24780+ au_unpin(&a->mvd_pin_src);
24781+ au_pin_hdir_lock(&a->mvd_pin_dst);
24782+ goto out_dst;
24783+ }
24784+ goto out; /* success */
c2b27bf2 24785+
c1595e42
JR
24786+out_dst:
24787+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
24788+out:
24789+ AuTraceErr(err);
24790+ return err;
24791+}
24792+
392086de 24793+static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2 24794+{
c1595e42
JR
24795+ if (!a->rename_lock)
24796+ au_unpin(&a->mvd_pin_src);
24797+ else {
c2b27bf2
AM
24798+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
24799+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
c1595e42
JR
24800+ au_pin_hdir_lock(&a->mvd_pin_src);
24801+ au_unpin(&a->mvd_pin_src);
24802+ au_pin_hdir_lock(&a->mvd_pin_dst);
24803+ }
24804+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
24805+}
24806+
24807+/* copy-down the file */
392086de 24808+static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24809+{
24810+ int err;
24811+ struct au_cp_generic cpg = {
24812+ .dentry = a->dentry,
24813+ .bdst = a->mvd_bdst,
24814+ .bsrc = a->mvd_bsrc,
24815+ .len = -1,
c1595e42 24816+ .pin = &a->mvd_pin_dst,
c2b27bf2
AM
24817+ .flags = AuCpup_DTIME | AuCpup_HOPEN
24818+ };
24819+
24820+ AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
392086de
AM
24821+ if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
24822+ au_fset_cpup(cpg.flags, OVERWRITE);
24823+ if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
24824+ au_fset_cpup(cpg.flags, RWDST);
c2b27bf2
AM
24825+ err = au_sio_cpdown_simple(&cpg);
24826+ if (unlikely(err))
392086de 24827+ AU_MVD_PR(dmsg, "cpdown failed\n");
c2b27bf2
AM
24828+
24829+ AuTraceErr(err);
24830+ return err;
24831+}
24832+
24833+/*
24834+ * unlink the whiteout on bdst if exist which may be created by UDBA while we
24835+ * were sleeping
24836+ */
392086de 24837+static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24838+{
24839+ int err;
24840+ struct path h_path;
24841+ struct au_branch *br;
523b37e3 24842+ struct inode *delegated;
c2b27bf2
AM
24843+
24844+ br = au_sbr(a->sb, a->mvd_bdst);
24845+ h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
24846+ err = PTR_ERR(h_path.dentry);
24847+ if (IS_ERR(h_path.dentry)) {
392086de 24848+ AU_MVD_PR(dmsg, "wh_lkup failed\n");
c2b27bf2
AM
24849+ goto out;
24850+ }
24851+
24852+ err = 0;
5527c038 24853+ if (d_is_positive(h_path.dentry)) {
c2b27bf2 24854+ h_path.mnt = au_br_mnt(br);
523b37e3 24855+ delegated = NULL;
5527c038 24856+ err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
523b37e3
AM
24857+ &delegated, /*force*/0);
24858+ if (unlikely(err == -EWOULDBLOCK)) {
24859+ pr_warn("cannot retry for NFSv4 delegation"
24860+ " for an internal unlink\n");
24861+ iput(delegated);
24862+ }
c2b27bf2 24863+ if (unlikely(err))
392086de 24864+ AU_MVD_PR(dmsg, "wh_unlink failed\n");
c2b27bf2
AM
24865+ }
24866+ dput(h_path.dentry);
24867+
24868+out:
24869+ AuTraceErr(err);
24870+ return err;
24871+}
24872+
24873+/*
24874+ * unlink the topmost h_dentry
c2b27bf2 24875+ */
392086de 24876+static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24877+{
24878+ int err;
24879+ struct path h_path;
523b37e3 24880+ struct inode *delegated;
c2b27bf2
AM
24881+
24882+ h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
24883+ h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
523b37e3
AM
24884+ delegated = NULL;
24885+ err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
24886+ if (unlikely(err == -EWOULDBLOCK)) {
24887+ pr_warn("cannot retry for NFSv4 delegation"
24888+ " for an internal unlink\n");
24889+ iput(delegated);
24890+ }
c2b27bf2 24891+ if (unlikely(err))
392086de 24892+ AU_MVD_PR(dmsg, "unlink failed\n");
c2b27bf2
AM
24893+
24894+ AuTraceErr(err);
24895+ return err;
24896+}
24897+
076b876e
AM
24898+/* Since mvdown succeeded, we ignore an error of this function */
24899+static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
24900+{
24901+ int err;
24902+ struct au_branch *br;
24903+
24904+ a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
24905+ br = au_sbr(a->sb, a->mvd_bsrc);
24906+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
24907+ if (!err) {
24908+ br = au_sbr(a->sb, a->mvd_bdst);
24909+ a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
24910+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
24911+ }
24912+ if (!err)
24913+ a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
24914+ else
24915+ AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
24916+}
24917+
c2b27bf2
AM
24918+/*
24919+ * copy-down the file and unlink the bsrc file.
24920+ * - unlink the bdst whout if exist
24921+ * - copy-down the file (with whtmp name and rename)
24922+ * - unlink the bsrc file
24923+ */
392086de 24924+static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24925+{
24926+ int err;
24927+
392086de 24928+ err = au_do_mkdir(dmsg, a);
c2b27bf2 24929+ if (!err)
392086de 24930+ err = au_do_lock(dmsg, a);
c2b27bf2
AM
24931+ if (unlikely(err))
24932+ goto out;
24933+
24934+ /*
24935+ * do not revert the activities we made on bdst since they should be
24936+ * harmless in aufs.
24937+ */
24938+
392086de 24939+ err = au_do_cpdown(dmsg, a);
c2b27bf2 24940+ if (!err)
392086de
AM
24941+ err = au_do_unlink_wh(dmsg, a);
24942+ if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
24943+ err = au_do_unlink(dmsg, a);
c2b27bf2
AM
24944+ if (unlikely(err))
24945+ goto out_unlock;
24946+
c1595e42
JR
24947+ AuDbg("%pd2, 0x%x, %d --> %d\n",
24948+ a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
076b876e
AM
24949+ if (find_lower_writable(a) < 0)
24950+ a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
24951+
24952+ if (a->mvdown.flags & AUFS_MVDOWN_STFS)
24953+ au_do_stfs(dmsg, a);
24954+
c2b27bf2 24955+ /* maintain internal array */
392086de
AM
24956+ if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
24957+ au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
5afbbe0d 24958+ au_set_dbtop(a->dentry, a->mvd_bdst);
392086de 24959+ au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
5afbbe0d 24960+ au_set_ibtop(a->inode, a->mvd_bdst);
79b8bda9
AM
24961+ } else {
24962+ /* hide the lower */
24963+ au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
5afbbe0d 24964+ au_set_dbbot(a->dentry, a->mvd_bsrc);
79b8bda9 24965+ au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
5afbbe0d 24966+ au_set_ibbot(a->inode, a->mvd_bsrc);
392086de 24967+ }
5afbbe0d
AM
24968+ if (au_dbbot(a->dentry) < a->mvd_bdst)
24969+ au_set_dbbot(a->dentry, a->mvd_bdst);
24970+ if (au_ibbot(a->inode) < a->mvd_bdst)
24971+ au_set_ibbot(a->inode, a->mvd_bdst);
c2b27bf2
AM
24972+
24973+out_unlock:
392086de 24974+ au_do_unlock(dmsg, a);
c2b27bf2
AM
24975+out:
24976+ AuTraceErr(err);
24977+ return err;
24978+}
24979+
24980+/* ---------------------------------------------------------------------- */
24981+
c2b27bf2 24982+/* make sure the file is idle */
392086de 24983+static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24984+{
24985+ int err, plinked;
c2b27bf2
AM
24986+
24987+ err = 0;
c2b27bf2 24988+ plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
5afbbe0d 24989+ if (au_dbtop(a->dentry) == a->mvd_bsrc
c1595e42 24990+ && au_dcount(a->dentry) == 1
c2b27bf2 24991+ && atomic_read(&a->inode->i_count) == 1
392086de 24992+ /* && a->mvd_h_src_inode->i_nlink == 1 */
c2b27bf2
AM
24993+ && (!plinked || !au_plink_test(a->inode))
24994+ && a->inode->i_nlink == 1)
24995+ goto out;
24996+
24997+ err = -EBUSY;
392086de 24998+ AU_MVD_PR(dmsg,
c1595e42 24999+ "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
5afbbe0d 25000+ a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
c2b27bf2 25001+ atomic_read(&a->inode->i_count), a->inode->i_nlink,
392086de 25002+ a->mvd_h_src_inode->i_nlink,
c2b27bf2
AM
25003+ plinked, plinked ? au_plink_test(a->inode) : 0);
25004+
25005+out:
25006+ AuTraceErr(err);
25007+ return err;
25008+}
25009+
25010+/* make sure the parent dir is fine */
392086de 25011+static int au_mvd_args_parent(const unsigned char dmsg,
c2b27bf2
AM
25012+ struct au_mvd_args *a)
25013+{
25014+ int err;
25015+ aufs_bindex_t bindex;
25016+
25017+ err = 0;
25018+ if (unlikely(au_alive_dir(a->parent))) {
25019+ err = -ENOENT;
392086de 25020+ AU_MVD_PR(dmsg, "parent dir is dead\n");
c2b27bf2
AM
25021+ goto out;
25022+ }
25023+
25024+ a->bopq = au_dbdiropq(a->parent);
25025+ bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
25026+ AuDbg("b%d\n", bindex);
25027+ if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
25028+ || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
25029+ err = -EINVAL;
392086de
AM
25030+ a->mvd_errno = EAU_MVDOWN_OPAQUE;
25031+ AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
c2b27bf2
AM
25032+ a->bopq, a->mvd_bdst);
25033+ }
25034+
25035+out:
25036+ AuTraceErr(err);
25037+ return err;
25038+}
25039+
392086de 25040+static int au_mvd_args_intermediate(const unsigned char dmsg,
c2b27bf2
AM
25041+ struct au_mvd_args *a)
25042+{
25043+ int err;
25044+ struct au_dinfo *dinfo, *tmp;
25045+
25046+ /* lookup the next lower positive entry */
25047+ err = -ENOMEM;
25048+ tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
25049+ if (unlikely(!tmp))
25050+ goto out;
25051+
25052+ a->bfound = -1;
25053+ a->bwh = -1;
25054+ dinfo = au_di(a->dentry);
25055+ au_di_cp(tmp, dinfo);
25056+ au_di_swap(tmp, dinfo);
25057+
25058+ /* returns the number of positive dentries */
5afbbe0d
AM
25059+ err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
25060+ /* AuLkup_IGNORE_PERM */ 0);
c2b27bf2
AM
25061+ if (!err)
25062+ a->bwh = au_dbwh(a->dentry);
25063+ else if (err > 0)
5afbbe0d 25064+ a->bfound = au_dbtop(a->dentry);
c2b27bf2
AM
25065+
25066+ au_di_swap(tmp, dinfo);
25067+ au_rw_write_unlock(&tmp->di_rwsem);
25068+ au_di_free(tmp);
25069+ if (unlikely(err < 0))
392086de 25070+ AU_MVD_PR(dmsg, "failed look-up lower\n");
c2b27bf2
AM
25071+
25072+ /*
25073+ * here, we have these cases.
25074+ * bfound == -1
25075+ * no positive dentry under bsrc. there are more sub-cases.
25076+ * bwh < 0
25077+ * there no whiteout, we can safely move-down.
25078+ * bwh <= bsrc
25079+ * impossible
25080+ * bsrc < bwh && bwh < bdst
25081+ * there is a whiteout on RO branch. cannot proceed.
25082+ * bwh == bdst
25083+ * there is a whiteout on the RW target branch. it should
25084+ * be removed.
25085+ * bdst < bwh
25086+ * there is a whiteout somewhere unrelated branch.
25087+ * -1 < bfound && bfound <= bsrc
25088+ * impossible.
25089+ * bfound < bdst
25090+ * found, but it is on RO branch between bsrc and bdst. cannot
25091+ * proceed.
25092+ * bfound == bdst
25093+ * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
25094+ * error.
25095+ * bdst < bfound
25096+ * found, after we create the file on bdst, it will be hidden.
25097+ */
25098+
25099+ AuDebugOn(a->bfound == -1
25100+ && a->bwh != -1
25101+ && a->bwh <= a->mvd_bsrc);
25102+ AuDebugOn(-1 < a->bfound
25103+ && a->bfound <= a->mvd_bsrc);
25104+
25105+ err = -EINVAL;
25106+ if (a->bfound == -1
25107+ && a->mvd_bsrc < a->bwh
25108+ && a->bwh != -1
25109+ && a->bwh < a->mvd_bdst) {
392086de
AM
25110+ a->mvd_errno = EAU_MVDOWN_WHITEOUT;
25111+ AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
c2b27bf2
AM
25112+ a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
25113+ goto out;
25114+ } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
392086de
AM
25115+ a->mvd_errno = EAU_MVDOWN_UPPER;
25116+ AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
c2b27bf2
AM
25117+ a->mvd_bdst, a->bfound);
25118+ goto out;
25119+ }
25120+
25121+ err = 0; /* success */
25122+
25123+out:
25124+ AuTraceErr(err);
25125+ return err;
25126+}
25127+
392086de 25128+static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25129+{
25130+ int err;
25131+
392086de
AM
25132+ err = 0;
25133+ if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25134+ && a->bfound == a->mvd_bdst)
25135+ err = -EEXIST;
c2b27bf2
AM
25136+ AuTraceErr(err);
25137+ return err;
25138+}
25139+
392086de 25140+static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25141+{
25142+ int err;
25143+ struct au_branch *br;
25144+
25145+ err = -EISDIR;
25146+ if (unlikely(S_ISDIR(a->inode->i_mode)))
25147+ goto out;
25148+
25149+ err = -EINVAL;
392086de 25150+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
5afbbe0d 25151+ a->mvd_bsrc = au_ibtop(a->inode);
392086de
AM
25152+ else {
25153+ a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
25154+ if (unlikely(a->mvd_bsrc < 0
5afbbe0d
AM
25155+ || (a->mvd_bsrc < au_dbtop(a->dentry)
25156+ || au_dbbot(a->dentry) < a->mvd_bsrc
392086de 25157+ || !au_h_dptr(a->dentry, a->mvd_bsrc))
5afbbe0d
AM
25158+ || (a->mvd_bsrc < au_ibtop(a->inode)
25159+ || au_ibbot(a->inode) < a->mvd_bsrc
392086de
AM
25160+ || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
25161+ a->mvd_errno = EAU_MVDOWN_NOUPPER;
25162+ AU_MVD_PR(dmsg, "no upper\n");
25163+ goto out;
25164+ }
25165+ }
5afbbe0d 25166+ if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
392086de
AM
25167+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25168+ AU_MVD_PR(dmsg, "on the bottom\n");
c2b27bf2
AM
25169+ goto out;
25170+ }
392086de 25171+ a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
c2b27bf2
AM
25172+ br = au_sbr(a->sb, a->mvd_bsrc);
25173+ err = au_br_rdonly(br);
392086de
AM
25174+ if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
25175+ if (unlikely(err))
25176+ goto out;
25177+ } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
25178+ || IS_APPEND(a->mvd_h_src_inode))) {
25179+ if (err)
25180+ a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
25181+ /* go on */
25182+ } else
c2b27bf2
AM
25183+ goto out;
25184+
25185+ err = -EINVAL;
392086de
AM
25186+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
25187+ a->mvd_bdst = find_lower_writable(a);
25188+ if (unlikely(a->mvd_bdst < 0)) {
25189+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25190+ AU_MVD_PR(dmsg, "no writable lower branch\n");
25191+ goto out;
25192+ }
25193+ } else {
25194+ a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
25195+ if (unlikely(a->mvd_bdst < 0
5afbbe0d 25196+ || au_sbbot(a->sb) < a->mvd_bdst)) {
392086de
AM
25197+ a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
25198+ AU_MVD_PR(dmsg, "no lower brid\n");
25199+ goto out;
25200+ }
c2b27bf2
AM
25201+ }
25202+
392086de 25203+ err = au_mvd_args_busy(dmsg, a);
c2b27bf2 25204+ if (!err)
392086de 25205+ err = au_mvd_args_parent(dmsg, a);
c2b27bf2 25206+ if (!err)
392086de 25207+ err = au_mvd_args_intermediate(dmsg, a);
c2b27bf2 25208+ if (!err)
392086de 25209+ err = au_mvd_args_exist(dmsg, a);
c2b27bf2
AM
25210+ if (!err)
25211+ AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
25212+
25213+out:
25214+ AuTraceErr(err);
25215+ return err;
25216+}
25217+
25218+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
25219+{
392086de
AM
25220+ int err, e;
25221+ unsigned char dmsg;
25222+ struct au_mvd_args *args;
79b8bda9 25223+ struct inode *inode;
c2b27bf2 25224+
79b8bda9 25225+ inode = d_inode(dentry);
c2b27bf2
AM
25226+ err = -EPERM;
25227+ if (unlikely(!capable(CAP_SYS_ADMIN)))
25228+ goto out;
25229+
392086de
AM
25230+ err = -ENOMEM;
25231+ args = kmalloc(sizeof(*args), GFP_NOFS);
25232+ if (unlikely(!args))
25233+ goto out;
25234+
25235+ err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
25236+ if (!err)
25237+ err = !access_ok(VERIFY_WRITE, uarg, sizeof(*uarg));
c2b27bf2
AM
25238+ if (unlikely(err)) {
25239+ err = -EFAULT;
392086de
AM
25240+ AuTraceErr(err);
25241+ goto out_free;
c2b27bf2 25242+ }
392086de
AM
25243+ AuDbg("flags 0x%x\n", args->mvdown.flags);
25244+ args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
25245+ args->mvdown.au_errno = 0;
25246+ args->dentry = dentry;
79b8bda9 25247+ args->inode = inode;
392086de 25248+ args->sb = dentry->d_sb;
c2b27bf2 25249+
392086de
AM
25250+ err = -ENOENT;
25251+ dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
25252+ args->parent = dget_parent(dentry);
5527c038 25253+ args->dir = d_inode(args->parent);
febd17d6 25254+ inode_lock_nested(args->dir, I_MUTEX_PARENT);
392086de
AM
25255+ dput(args->parent);
25256+ if (unlikely(args->parent != dentry->d_parent)) {
25257+ AU_MVD_PR(dmsg, "parent dir is moved\n");
c2b27bf2
AM
25258+ goto out_dir;
25259+ }
25260+
febd17d6 25261+ inode_lock_nested(inode, I_MUTEX_CHILD);
b95c5147 25262+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
c2b27bf2
AM
25263+ if (unlikely(err))
25264+ goto out_inode;
25265+
392086de
AM
25266+ di_write_lock_parent(args->parent);
25267+ err = au_mvd_args(dmsg, args);
c2b27bf2
AM
25268+ if (unlikely(err))
25269+ goto out_parent;
25270+
392086de 25271+ err = au_do_mvdown(dmsg, args);
c2b27bf2
AM
25272+ if (unlikely(err))
25273+ goto out_parent;
c2b27bf2 25274+
392086de 25275+ au_cpup_attr_timesizes(args->dir);
79b8bda9
AM
25276+ au_cpup_attr_timesizes(inode);
25277+ if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
25278+ au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
c2b27bf2
AM
25279+ /* au_digen_dec(dentry); */
25280+
25281+out_parent:
392086de 25282+ di_write_unlock(args->parent);
c2b27bf2
AM
25283+ aufs_read_unlock(dentry, AuLock_DW);
25284+out_inode:
febd17d6 25285+ inode_unlock(inode);
c2b27bf2 25286+out_dir:
febd17d6 25287+ inode_unlock(args->dir);
392086de
AM
25288+out_free:
25289+ e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
25290+ if (unlikely(e))
25291+ err = -EFAULT;
1c60b727 25292+ kfree(args);
c2b27bf2
AM
25293+out:
25294+ AuTraceErr(err);
25295+ return err;
25296+}
25297diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
25298--- /usr/share/empty/fs/aufs/opts.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
25299+++ linux/fs/aufs/opts.c 2017-11-12 22:24:44.707577830 +0100
25300@@ -0,0 +1,1891 @@
1facf9fc 25301+/*
a2654f78 25302+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 25303+ *
25304+ * This program, aufs is free software; you can redistribute it and/or modify
25305+ * it under the terms of the GNU General Public License as published by
25306+ * the Free Software Foundation; either version 2 of the License, or
25307+ * (at your option) any later version.
dece6358
AM
25308+ *
25309+ * This program is distributed in the hope that it will be useful,
25310+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25311+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25312+ * GNU General Public License for more details.
25313+ *
25314+ * You should have received a copy of the GNU General Public License
523b37e3 25315+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 25316+ */
25317+
25318+/*
25319+ * mount options/flags
25320+ */
25321+
dece6358 25322+#include <linux/namei.h>
1facf9fc 25323+#include <linux/types.h> /* a distribution requires */
25324+#include <linux/parser.h>
25325+#include "aufs.h"
25326+
25327+/* ---------------------------------------------------------------------- */
25328+
25329+enum {
25330+ Opt_br,
7e9cd9fe
AM
25331+ Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
25332+ Opt_idel, Opt_imod,
25333+ Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
dece6358 25334+ Opt_rdblk_def, Opt_rdhash_def,
7e9cd9fe 25335+ Opt_xino, Opt_noxino,
1facf9fc 25336+ Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
25337+ Opt_trunc_xino_path, Opt_itrunc_xino,
25338+ Opt_trunc_xib, Opt_notrunc_xib,
dece6358 25339+ Opt_shwh, Opt_noshwh,
1facf9fc 25340+ Opt_plink, Opt_noplink, Opt_list_plink,
25341+ Opt_udba,
4a4d8108 25342+ Opt_dio, Opt_nodio,
1facf9fc 25343+ Opt_diropq_a, Opt_diropq_w,
25344+ Opt_warn_perm, Opt_nowarn_perm,
25345+ Opt_wbr_copyup, Opt_wbr_create,
076b876e 25346+ Opt_fhsm_sec,
1facf9fc 25347+ Opt_verbose, Opt_noverbose,
25348+ Opt_sum, Opt_nosum, Opt_wsum,
076b876e 25349+ Opt_dirperm1, Opt_nodirperm1,
8b6a4947 25350+ Opt_dirren, Opt_nodirren,
c1595e42 25351+ Opt_acl, Opt_noacl,
1facf9fc 25352+ Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
25353+};
25354+
25355+static match_table_t options = {
25356+ {Opt_br, "br=%s"},
25357+ {Opt_br, "br:%s"},
25358+
25359+ {Opt_add, "add=%d:%s"},
25360+ {Opt_add, "add:%d:%s"},
25361+ {Opt_add, "ins=%d:%s"},
25362+ {Opt_add, "ins:%d:%s"},
25363+ {Opt_append, "append=%s"},
25364+ {Opt_append, "append:%s"},
25365+ {Opt_prepend, "prepend=%s"},
25366+ {Opt_prepend, "prepend:%s"},
25367+
25368+ {Opt_del, "del=%s"},
25369+ {Opt_del, "del:%s"},
25370+ /* {Opt_idel, "idel:%d"}, */
25371+ {Opt_mod, "mod=%s"},
25372+ {Opt_mod, "mod:%s"},
25373+ /* {Opt_imod, "imod:%d:%s"}, */
25374+
25375+ {Opt_dirwh, "dirwh=%d"},
25376+
25377+ {Opt_xino, "xino=%s"},
25378+ {Opt_noxino, "noxino"},
25379+ {Opt_trunc_xino, "trunc_xino"},
25380+ {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
25381+ {Opt_notrunc_xino, "notrunc_xino"},
25382+ {Opt_trunc_xino_path, "trunc_xino=%s"},
25383+ {Opt_itrunc_xino, "itrunc_xino=%d"},
25384+ /* {Opt_zxino, "zxino=%s"}, */
25385+ {Opt_trunc_xib, "trunc_xib"},
25386+ {Opt_notrunc_xib, "notrunc_xib"},
25387+
e49829fe 25388+#ifdef CONFIG_PROC_FS
1facf9fc 25389+ {Opt_plink, "plink"},
e49829fe
JR
25390+#else
25391+ {Opt_ignore_silent, "plink"},
25392+#endif
25393+
1facf9fc 25394+ {Opt_noplink, "noplink"},
e49829fe 25395+
1facf9fc 25396+#ifdef CONFIG_AUFS_DEBUG
25397+ {Opt_list_plink, "list_plink"},
25398+#endif
25399+
25400+ {Opt_udba, "udba=%s"},
25401+
4a4d8108
AM
25402+ {Opt_dio, "dio"},
25403+ {Opt_nodio, "nodio"},
25404+
8b6a4947
AM
25405+#ifdef CONFIG_AUFS_DIRREN
25406+ {Opt_dirren, "dirren"},
25407+ {Opt_nodirren, "nodirren"},
25408+#else
25409+ {Opt_ignore, "dirren"},
25410+ {Opt_ignore_silent, "nodirren"},
25411+#endif
25412+
076b876e
AM
25413+#ifdef CONFIG_AUFS_FHSM
25414+ {Opt_fhsm_sec, "fhsm_sec=%d"},
25415+#else
8b6a4947 25416+ {Opt_ignore, "fhsm_sec=%d"},
076b876e
AM
25417+#endif
25418+
1facf9fc 25419+ {Opt_diropq_a, "diropq=always"},
25420+ {Opt_diropq_a, "diropq=a"},
25421+ {Opt_diropq_w, "diropq=whiteouted"},
25422+ {Opt_diropq_w, "diropq=w"},
25423+
25424+ {Opt_warn_perm, "warn_perm"},
25425+ {Opt_nowarn_perm, "nowarn_perm"},
25426+
25427+ /* keep them temporary */
1facf9fc 25428+ {Opt_ignore_silent, "nodlgt"},
8b6a4947 25429+ {Opt_ignore, "clean_plink"},
1facf9fc 25430+
dece6358
AM
25431+#ifdef CONFIG_AUFS_SHWH
25432+ {Opt_shwh, "shwh"},
25433+#endif
25434+ {Opt_noshwh, "noshwh"},
25435+
076b876e
AM
25436+ {Opt_dirperm1, "dirperm1"},
25437+ {Opt_nodirperm1, "nodirperm1"},
25438+
1facf9fc 25439+ {Opt_verbose, "verbose"},
25440+ {Opt_verbose, "v"},
25441+ {Opt_noverbose, "noverbose"},
25442+ {Opt_noverbose, "quiet"},
25443+ {Opt_noverbose, "q"},
25444+ {Opt_noverbose, "silent"},
25445+
25446+ {Opt_sum, "sum"},
25447+ {Opt_nosum, "nosum"},
25448+ {Opt_wsum, "wsum"},
25449+
25450+ {Opt_rdcache, "rdcache=%d"},
25451+ {Opt_rdblk, "rdblk=%d"},
dece6358 25452+ {Opt_rdblk_def, "rdblk=def"},
1facf9fc 25453+ {Opt_rdhash, "rdhash=%d"},
dece6358 25454+ {Opt_rdhash_def, "rdhash=def"},
1facf9fc 25455+
25456+ {Opt_wbr_create, "create=%s"},
25457+ {Opt_wbr_create, "create_policy=%s"},
25458+ {Opt_wbr_copyup, "cpup=%s"},
25459+ {Opt_wbr_copyup, "copyup=%s"},
25460+ {Opt_wbr_copyup, "copyup_policy=%s"},
25461+
c1595e42
JR
25462+ /* generic VFS flag */
25463+#ifdef CONFIG_FS_POSIX_ACL
25464+ {Opt_acl, "acl"},
25465+ {Opt_noacl, "noacl"},
25466+#else
8b6a4947 25467+ {Opt_ignore, "acl"},
c1595e42
JR
25468+ {Opt_ignore_silent, "noacl"},
25469+#endif
25470+
1facf9fc 25471+ /* internal use for the scripts */
25472+ {Opt_ignore_silent, "si=%s"},
25473+
25474+ {Opt_br, "dirs=%s"},
25475+ {Opt_ignore, "debug=%d"},
25476+ {Opt_ignore, "delete=whiteout"},
25477+ {Opt_ignore, "delete=all"},
25478+ {Opt_ignore, "imap=%s"},
25479+
1308ab2a 25480+ /* temporary workaround, due to old mount(8)? */
25481+ {Opt_ignore_silent, "relatime"},
25482+
1facf9fc 25483+ {Opt_err, NULL}
25484+};
25485+
25486+/* ---------------------------------------------------------------------- */
25487+
076b876e 25488+static const char *au_parser_pattern(int val, match_table_t tbl)
1facf9fc 25489+{
076b876e
AM
25490+ struct match_token *p;
25491+
25492+ p = tbl;
25493+ while (p->pattern) {
25494+ if (p->token == val)
25495+ return p->pattern;
25496+ p++;
1facf9fc 25497+ }
25498+ BUG();
25499+ return "??";
25500+}
25501+
076b876e
AM
25502+static const char *au_optstr(int *val, match_table_t tbl)
25503+{
25504+ struct match_token *p;
25505+ int v;
25506+
25507+ v = *val;
2000de60
JR
25508+ if (!v)
25509+ goto out;
076b876e 25510+ p = tbl;
2000de60
JR
25511+ while (p->pattern) {
25512+ if (p->token
25513+ && (v & p->token) == p->token) {
076b876e
AM
25514+ *val &= ~p->token;
25515+ return p->pattern;
25516+ }
25517+ p++;
25518+ }
2000de60
JR
25519+
25520+out:
076b876e
AM
25521+ return NULL;
25522+}
25523+
1facf9fc 25524+/* ---------------------------------------------------------------------- */
25525+
1e00d052 25526+static match_table_t brperm = {
1facf9fc 25527+ {AuBrPerm_RO, AUFS_BRPERM_RO},
25528+ {AuBrPerm_RR, AUFS_BRPERM_RR},
25529+ {AuBrPerm_RW, AUFS_BRPERM_RW},
1e00d052
AM
25530+ {0, NULL}
25531+};
1facf9fc 25532+
86dc4139 25533+static match_table_t brattr = {
076b876e
AM
25534+ /* general */
25535+ {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
25536+ {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
c1595e42 25537+ /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
86dc4139 25538+ {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
2000de60 25539+#ifdef CONFIG_AUFS_FHSM
076b876e 25540+ {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
2000de60
JR
25541+#endif
25542+#ifdef CONFIG_AUFS_XATTR
c1595e42
JR
25543+ {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
25544+ {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
25545+ {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
25546+ {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
25547+ {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
25548+ {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
2000de60 25549+#endif
076b876e
AM
25550+
25551+ /* ro/rr branch */
1e00d052 25552+ {AuBrRAttr_WH, AUFS_BRRATTR_WH},
076b876e
AM
25553+
25554+ /* rw branch */
25555+ {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
1e00d052 25556+ {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
076b876e 25557+
1e00d052 25558+ {0, NULL}
1facf9fc 25559+};
25560+
1e00d052
AM
25561+static int br_attr_val(char *str, match_table_t table, substring_t args[])
25562+{
25563+ int attr, v;
25564+ char *p;
25565+
25566+ attr = 0;
25567+ do {
25568+ p = strchr(str, '+');
25569+ if (p)
25570+ *p = 0;
25571+ v = match_token(str, table, args);
076b876e
AM
25572+ if (v) {
25573+ if (v & AuBrAttr_CMOO_Mask)
25574+ attr &= ~AuBrAttr_CMOO_Mask;
1e00d052 25575+ attr |= v;
076b876e 25576+ } else {
1e00d052
AM
25577+ if (p)
25578+ *p = '+';
0c3ec466 25579+ pr_warn("ignored branch attribute %s\n", str);
1e00d052
AM
25580+ break;
25581+ }
25582+ if (p)
25583+ str = p + 1;
25584+ } while (p);
25585+
25586+ return attr;
25587+}
25588+
076b876e
AM
25589+static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
25590+{
25591+ int sz;
25592+ const char *p;
25593+ char *q;
25594+
076b876e
AM
25595+ q = str->a;
25596+ *q = 0;
25597+ p = au_optstr(&perm, brattr);
25598+ if (p) {
25599+ sz = strlen(p);
25600+ memcpy(q, p, sz + 1);
25601+ q += sz;
25602+ } else
25603+ goto out;
25604+
25605+ do {
25606+ p = au_optstr(&perm, brattr);
25607+ if (p) {
25608+ *q++ = '+';
25609+ sz = strlen(p);
25610+ memcpy(q, p, sz + 1);
25611+ q += sz;
25612+ }
25613+ } while (p);
25614+
25615+out:
c1595e42 25616+ return q - str->a;
076b876e
AM
25617+}
25618+
4a4d8108 25619+static int noinline_for_stack br_perm_val(char *perm)
1facf9fc 25620+{
076b876e
AM
25621+ int val, bad, sz;
25622+ char *p;
1facf9fc 25623+ substring_t args[MAX_OPT_ARGS];
076b876e 25624+ au_br_perm_str_t attr;
1facf9fc 25625+
1e00d052
AM
25626+ p = strchr(perm, '+');
25627+ if (p)
25628+ *p = 0;
25629+ val = match_token(perm, brperm, args);
25630+ if (!val) {
25631+ if (p)
25632+ *p = '+';
0c3ec466 25633+ pr_warn("ignored branch permission %s\n", perm);
1e00d052
AM
25634+ val = AuBrPerm_RO;
25635+ goto out;
25636+ }
25637+ if (!p)
25638+ goto out;
25639+
076b876e
AM
25640+ val |= br_attr_val(p + 1, brattr, args);
25641+
25642+ bad = 0;
86dc4139 25643+ switch (val & AuBrPerm_Mask) {
1e00d052
AM
25644+ case AuBrPerm_RO:
25645+ case AuBrPerm_RR:
076b876e
AM
25646+ bad = val & AuBrWAttr_Mask;
25647+ val &= ~AuBrWAttr_Mask;
1e00d052
AM
25648+ break;
25649+ case AuBrPerm_RW:
076b876e
AM
25650+ bad = val & AuBrRAttr_Mask;
25651+ val &= ~AuBrRAttr_Mask;
1e00d052
AM
25652+ break;
25653+ }
c1595e42
JR
25654+
25655+ /*
25656+ * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
25657+ * does not treat it as an error, just warning.
25658+ * this is a tiny guard for the user operation.
25659+ */
25660+ if (val & AuBrAttr_UNPIN) {
25661+ bad |= AuBrAttr_UNPIN;
25662+ val &= ~AuBrAttr_UNPIN;
25663+ }
25664+
076b876e
AM
25665+ if (unlikely(bad)) {
25666+ sz = au_do_optstr_br_attr(&attr, bad);
25667+ AuDebugOn(!sz);
25668+ pr_warn("ignored branch attribute %s\n", attr.a);
25669+ }
1e00d052
AM
25670+
25671+out:
1facf9fc 25672+ return val;
25673+}
25674+
076b876e 25675+void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
1facf9fc 25676+{
076b876e
AM
25677+ au_br_perm_str_t attr;
25678+ const char *p;
25679+ char *q;
1e00d052
AM
25680+ int sz;
25681+
076b876e
AM
25682+ q = str->a;
25683+ p = au_optstr(&perm, brperm);
25684+ AuDebugOn(!p || !*p);
25685+ sz = strlen(p);
25686+ memcpy(q, p, sz + 1);
25687+ q += sz;
1e00d052 25688+
076b876e
AM
25689+ sz = au_do_optstr_br_attr(&attr, perm);
25690+ if (sz) {
25691+ *q++ = '+';
25692+ memcpy(q, attr.a, sz + 1);
1e00d052
AM
25693+ }
25694+
076b876e 25695+ AuDebugOn(strlen(str->a) >= sizeof(str->a));
1facf9fc 25696+}
25697+
25698+/* ---------------------------------------------------------------------- */
25699+
25700+static match_table_t udbalevel = {
25701+ {AuOpt_UDBA_REVAL, "reval"},
25702+ {AuOpt_UDBA_NONE, "none"},
4a4d8108
AM
25703+#ifdef CONFIG_AUFS_HNOTIFY
25704+ {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
25705+#ifdef CONFIG_AUFS_HFSNOTIFY
25706+ {AuOpt_UDBA_HNOTIFY, "fsnotify"},
4a4d8108 25707+#endif
1facf9fc 25708+#endif
25709+ {-1, NULL}
25710+};
25711+
4a4d8108 25712+static int noinline_for_stack udba_val(char *str)
1facf9fc 25713+{
25714+ substring_t args[MAX_OPT_ARGS];
25715+
7f207e10 25716+ return match_token(str, udbalevel, args);
1facf9fc 25717+}
25718+
25719+const char *au_optstr_udba(int udba)
25720+{
076b876e 25721+ return au_parser_pattern(udba, udbalevel);
1facf9fc 25722+}
25723+
25724+/* ---------------------------------------------------------------------- */
25725+
25726+static match_table_t au_wbr_create_policy = {
25727+ {AuWbrCreate_TDP, "tdp"},
25728+ {AuWbrCreate_TDP, "top-down-parent"},
25729+ {AuWbrCreate_RR, "rr"},
25730+ {AuWbrCreate_RR, "round-robin"},
25731+ {AuWbrCreate_MFS, "mfs"},
25732+ {AuWbrCreate_MFS, "most-free-space"},
25733+ {AuWbrCreate_MFSV, "mfs:%d"},
25734+ {AuWbrCreate_MFSV, "most-free-space:%d"},
25735+
f2c43d5f
AM
25736+ /* top-down regardless the parent, and then mfs */
25737+ {AuWbrCreate_TDMFS, "tdmfs:%d"},
25738+ {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
25739+
1facf9fc 25740+ {AuWbrCreate_MFSRR, "mfsrr:%d"},
25741+ {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
25742+ {AuWbrCreate_PMFS, "pmfs"},
25743+ {AuWbrCreate_PMFSV, "pmfs:%d"},
392086de
AM
25744+ {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
25745+ {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
1facf9fc 25746+
25747+ {-1, NULL}
25748+};
25749+
1facf9fc 25750+static int au_wbr_mfs_wmark(substring_t *arg, char *str,
25751+ struct au_opt_wbr_create *create)
25752+{
25753+ int err;
25754+ unsigned long long ull;
25755+
25756+ err = 0;
a2654f78 25757+ if (!match_u64(arg, &ull))
1facf9fc 25758+ create->mfsrr_watermark = ull;
25759+ else {
4a4d8108 25760+ pr_err("bad integer in %s\n", str);
1facf9fc 25761+ err = -EINVAL;
25762+ }
25763+
25764+ return err;
25765+}
25766+
25767+static int au_wbr_mfs_sec(substring_t *arg, char *str,
25768+ struct au_opt_wbr_create *create)
25769+{
25770+ int n, err;
25771+
25772+ err = 0;
027c5e7a 25773+ if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
1facf9fc 25774+ create->mfs_second = n;
25775+ else {
4a4d8108 25776+ pr_err("bad integer in %s\n", str);
1facf9fc 25777+ err = -EINVAL;
25778+ }
25779+
25780+ return err;
25781+}
25782+
4a4d8108
AM
25783+static int noinline_for_stack
25784+au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
1facf9fc 25785+{
25786+ int err, e;
25787+ substring_t args[MAX_OPT_ARGS];
25788+
25789+ err = match_token(str, au_wbr_create_policy, args);
25790+ create->wbr_create = err;
25791+ switch (err) {
25792+ case AuWbrCreate_MFSRRV:
f2c43d5f 25793+ case AuWbrCreate_TDMFSV:
392086de 25794+ case AuWbrCreate_PMFSRRV:
1facf9fc 25795+ e = au_wbr_mfs_wmark(&args[0], str, create);
25796+ if (!e)
25797+ e = au_wbr_mfs_sec(&args[1], str, create);
25798+ if (unlikely(e))
25799+ err = e;
25800+ break;
25801+ case AuWbrCreate_MFSRR:
f2c43d5f 25802+ case AuWbrCreate_TDMFS:
392086de 25803+ case AuWbrCreate_PMFSRR:
1facf9fc 25804+ e = au_wbr_mfs_wmark(&args[0], str, create);
25805+ if (unlikely(e)) {
25806+ err = e;
25807+ break;
25808+ }
25809+ /*FALLTHROUGH*/
25810+ case AuWbrCreate_MFS:
25811+ case AuWbrCreate_PMFS:
027c5e7a 25812+ create->mfs_second = AUFS_MFS_DEF_SEC;
1facf9fc 25813+ break;
25814+ case AuWbrCreate_MFSV:
25815+ case AuWbrCreate_PMFSV:
25816+ e = au_wbr_mfs_sec(&args[0], str, create);
25817+ if (unlikely(e))
25818+ err = e;
25819+ break;
25820+ }
25821+
25822+ return err;
25823+}
25824+
25825+const char *au_optstr_wbr_create(int wbr_create)
25826+{
076b876e 25827+ return au_parser_pattern(wbr_create, au_wbr_create_policy);
1facf9fc 25828+}
25829+
25830+static match_table_t au_wbr_copyup_policy = {
25831+ {AuWbrCopyup_TDP, "tdp"},
25832+ {AuWbrCopyup_TDP, "top-down-parent"},
25833+ {AuWbrCopyup_BUP, "bup"},
25834+ {AuWbrCopyup_BUP, "bottom-up-parent"},
25835+ {AuWbrCopyup_BU, "bu"},
25836+ {AuWbrCopyup_BU, "bottom-up"},
25837+ {-1, NULL}
25838+};
25839+
4a4d8108 25840+static int noinline_for_stack au_wbr_copyup_val(char *str)
1facf9fc 25841+{
25842+ substring_t args[MAX_OPT_ARGS];
25843+
25844+ return match_token(str, au_wbr_copyup_policy, args);
25845+}
25846+
25847+const char *au_optstr_wbr_copyup(int wbr_copyup)
25848+{
076b876e 25849+ return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
1facf9fc 25850+}
25851+
25852+/* ---------------------------------------------------------------------- */
25853+
25854+static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
25855+
25856+static void dump_opts(struct au_opts *opts)
25857+{
25858+#ifdef CONFIG_AUFS_DEBUG
25859+ /* reduce stack space */
25860+ union {
25861+ struct au_opt_add *add;
25862+ struct au_opt_del *del;
25863+ struct au_opt_mod *mod;
25864+ struct au_opt_xino *xino;
25865+ struct au_opt_xino_itrunc *xino_itrunc;
25866+ struct au_opt_wbr_create *create;
25867+ } u;
25868+ struct au_opt *opt;
25869+
25870+ opt = opts->opt;
25871+ while (opt->type != Opt_tail) {
25872+ switch (opt->type) {
25873+ case Opt_add:
25874+ u.add = &opt->add;
25875+ AuDbg("add {b%d, %s, 0x%x, %p}\n",
25876+ u.add->bindex, u.add->pathname, u.add->perm,
25877+ u.add->path.dentry);
25878+ break;
25879+ case Opt_del:
25880+ case Opt_idel:
25881+ u.del = &opt->del;
25882+ AuDbg("del {%s, %p}\n",
25883+ u.del->pathname, u.del->h_path.dentry);
25884+ break;
25885+ case Opt_mod:
25886+ case Opt_imod:
25887+ u.mod = &opt->mod;
25888+ AuDbg("mod {%s, 0x%x, %p}\n",
25889+ u.mod->path, u.mod->perm, u.mod->h_root);
25890+ break;
25891+ case Opt_append:
25892+ u.add = &opt->add;
25893+ AuDbg("append {b%d, %s, 0x%x, %p}\n",
25894+ u.add->bindex, u.add->pathname, u.add->perm,
25895+ u.add->path.dentry);
25896+ break;
25897+ case Opt_prepend:
25898+ u.add = &opt->add;
25899+ AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
25900+ u.add->bindex, u.add->pathname, u.add->perm,
25901+ u.add->path.dentry);
25902+ break;
25903+ case Opt_dirwh:
25904+ AuDbg("dirwh %d\n", opt->dirwh);
25905+ break;
25906+ case Opt_rdcache:
25907+ AuDbg("rdcache %d\n", opt->rdcache);
25908+ break;
25909+ case Opt_rdblk:
25910+ AuDbg("rdblk %u\n", opt->rdblk);
25911+ break;
dece6358
AM
25912+ case Opt_rdblk_def:
25913+ AuDbg("rdblk_def\n");
25914+ break;
1facf9fc 25915+ case Opt_rdhash:
25916+ AuDbg("rdhash %u\n", opt->rdhash);
25917+ break;
dece6358
AM
25918+ case Opt_rdhash_def:
25919+ AuDbg("rdhash_def\n");
25920+ break;
1facf9fc 25921+ case Opt_xino:
25922+ u.xino = &opt->xino;
523b37e3 25923+ AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
1facf9fc 25924+ break;
25925+ case Opt_trunc_xino:
25926+ AuLabel(trunc_xino);
25927+ break;
25928+ case Opt_notrunc_xino:
25929+ AuLabel(notrunc_xino);
25930+ break;
25931+ case Opt_trunc_xino_path:
25932+ case Opt_itrunc_xino:
25933+ u.xino_itrunc = &opt->xino_itrunc;
25934+ AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
25935+ break;
1facf9fc 25936+ case Opt_noxino:
25937+ AuLabel(noxino);
25938+ break;
25939+ case Opt_trunc_xib:
25940+ AuLabel(trunc_xib);
25941+ break;
25942+ case Opt_notrunc_xib:
25943+ AuLabel(notrunc_xib);
25944+ break;
dece6358
AM
25945+ case Opt_shwh:
25946+ AuLabel(shwh);
25947+ break;
25948+ case Opt_noshwh:
25949+ AuLabel(noshwh);
25950+ break;
076b876e
AM
25951+ case Opt_dirperm1:
25952+ AuLabel(dirperm1);
25953+ break;
25954+ case Opt_nodirperm1:
25955+ AuLabel(nodirperm1);
25956+ break;
1facf9fc 25957+ case Opt_plink:
25958+ AuLabel(plink);
25959+ break;
25960+ case Opt_noplink:
25961+ AuLabel(noplink);
25962+ break;
25963+ case Opt_list_plink:
25964+ AuLabel(list_plink);
25965+ break;
25966+ case Opt_udba:
25967+ AuDbg("udba %d, %s\n",
25968+ opt->udba, au_optstr_udba(opt->udba));
25969+ break;
4a4d8108
AM
25970+ case Opt_dio:
25971+ AuLabel(dio);
25972+ break;
25973+ case Opt_nodio:
25974+ AuLabel(nodio);
25975+ break;
1facf9fc 25976+ case Opt_diropq_a:
25977+ AuLabel(diropq_a);
25978+ break;
25979+ case Opt_diropq_w:
25980+ AuLabel(diropq_w);
25981+ break;
25982+ case Opt_warn_perm:
25983+ AuLabel(warn_perm);
25984+ break;
25985+ case Opt_nowarn_perm:
25986+ AuLabel(nowarn_perm);
25987+ break;
1facf9fc 25988+ case Opt_verbose:
25989+ AuLabel(verbose);
25990+ break;
25991+ case Opt_noverbose:
25992+ AuLabel(noverbose);
25993+ break;
25994+ case Opt_sum:
25995+ AuLabel(sum);
25996+ break;
25997+ case Opt_nosum:
25998+ AuLabel(nosum);
25999+ break;
26000+ case Opt_wsum:
26001+ AuLabel(wsum);
26002+ break;
26003+ case Opt_wbr_create:
26004+ u.create = &opt->wbr_create;
26005+ AuDbg("create %d, %s\n", u.create->wbr_create,
26006+ au_optstr_wbr_create(u.create->wbr_create));
26007+ switch (u.create->wbr_create) {
26008+ case AuWbrCreate_MFSV:
26009+ case AuWbrCreate_PMFSV:
26010+ AuDbg("%d sec\n", u.create->mfs_second);
26011+ break;
26012+ case AuWbrCreate_MFSRR:
f2c43d5f 26013+ case AuWbrCreate_TDMFS:
1facf9fc 26014+ AuDbg("%llu watermark\n",
26015+ u.create->mfsrr_watermark);
26016+ break;
26017+ case AuWbrCreate_MFSRRV:
f2c43d5f 26018+ case AuWbrCreate_TDMFSV:
392086de 26019+ case AuWbrCreate_PMFSRRV:
1facf9fc 26020+ AuDbg("%llu watermark, %d sec\n",
26021+ u.create->mfsrr_watermark,
26022+ u.create->mfs_second);
26023+ break;
26024+ }
26025+ break;
26026+ case Opt_wbr_copyup:
26027+ AuDbg("copyup %d, %s\n", opt->wbr_copyup,
26028+ au_optstr_wbr_copyup(opt->wbr_copyup));
26029+ break;
076b876e
AM
26030+ case Opt_fhsm_sec:
26031+ AuDbg("fhsm_sec %u\n", opt->fhsm_second);
26032+ break;
8b6a4947
AM
26033+ case Opt_dirren:
26034+ AuLabel(dirren);
26035+ break;
26036+ case Opt_nodirren:
26037+ AuLabel(nodirren);
26038+ break;
c1595e42
JR
26039+ case Opt_acl:
26040+ AuLabel(acl);
26041+ break;
26042+ case Opt_noacl:
26043+ AuLabel(noacl);
26044+ break;
1facf9fc 26045+ default:
26046+ BUG();
26047+ }
26048+ opt++;
26049+ }
26050+#endif
26051+}
26052+
26053+void au_opts_free(struct au_opts *opts)
26054+{
26055+ struct au_opt *opt;
26056+
26057+ opt = opts->opt;
26058+ while (opt->type != Opt_tail) {
26059+ switch (opt->type) {
26060+ case Opt_add:
26061+ case Opt_append:
26062+ case Opt_prepend:
26063+ path_put(&opt->add.path);
26064+ break;
26065+ case Opt_del:
26066+ case Opt_idel:
26067+ path_put(&opt->del.h_path);
26068+ break;
26069+ case Opt_mod:
26070+ case Opt_imod:
26071+ dput(opt->mod.h_root);
26072+ break;
26073+ case Opt_xino:
26074+ fput(opt->xino.file);
26075+ break;
26076+ }
26077+ opt++;
26078+ }
26079+}
26080+
26081+static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
26082+ aufs_bindex_t bindex)
26083+{
26084+ int err;
26085+ struct au_opt_add *add = &opt->add;
26086+ char *p;
26087+
26088+ add->bindex = bindex;
1e00d052 26089+ add->perm = AuBrPerm_RO;
1facf9fc 26090+ add->pathname = opt_str;
26091+ p = strchr(opt_str, '=');
26092+ if (p) {
26093+ *p++ = 0;
26094+ if (*p)
26095+ add->perm = br_perm_val(p);
26096+ }
26097+
26098+ err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
26099+ if (!err) {
26100+ if (!p) {
26101+ add->perm = AuBrPerm_RO;
26102+ if (au_test_fs_rr(add->path.dentry->d_sb))
26103+ add->perm = AuBrPerm_RR;
26104+ else if (!bindex && !(sb_flags & MS_RDONLY))
26105+ add->perm = AuBrPerm_RW;
26106+ }
26107+ opt->type = Opt_add;
26108+ goto out;
26109+ }
4a4d8108 26110+ pr_err("lookup failed %s (%d)\n", add->pathname, err);
1facf9fc 26111+ err = -EINVAL;
26112+
4f0767ce 26113+out:
1facf9fc 26114+ return err;
26115+}
26116+
26117+static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
26118+{
26119+ int err;
26120+
26121+ del->pathname = args[0].from;
26122+ AuDbg("del path %s\n", del->pathname);
26123+
26124+ err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
26125+ if (unlikely(err))
4a4d8108 26126+ pr_err("lookup failed %s (%d)\n", del->pathname, err);
1facf9fc 26127+
26128+ return err;
26129+}
26130+
26131+#if 0 /* reserved for future use */
26132+static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
26133+ struct au_opt_del *del, substring_t args[])
26134+{
26135+ int err;
26136+ struct dentry *root;
26137+
26138+ err = -EINVAL;
26139+ root = sb->s_root;
26140+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26141+ if (bindex < 0 || au_sbbot(sb) < bindex) {
4a4d8108 26142+ pr_err("out of bounds, %d\n", bindex);
1facf9fc 26143+ goto out;
26144+ }
26145+
26146+ err = 0;
26147+ del->h_path.dentry = dget(au_h_dptr(root, bindex));
26148+ del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
26149+
4f0767ce 26150+out:
1facf9fc 26151+ aufs_read_unlock(root, !AuLock_IR);
26152+ return err;
26153+}
26154+#endif
26155+
4a4d8108
AM
26156+static int noinline_for_stack
26157+au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
1facf9fc 26158+{
26159+ int err;
26160+ struct path path;
26161+ char *p;
26162+
26163+ err = -EINVAL;
26164+ mod->path = args[0].from;
26165+ p = strchr(mod->path, '=');
26166+ if (unlikely(!p)) {
4a4d8108 26167+ pr_err("no permssion %s\n", args[0].from);
1facf9fc 26168+ goto out;
26169+ }
26170+
26171+ *p++ = 0;
26172+ err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
26173+ if (unlikely(err)) {
4a4d8108 26174+ pr_err("lookup failed %s (%d)\n", mod->path, err);
1facf9fc 26175+ goto out;
26176+ }
26177+
26178+ mod->perm = br_perm_val(p);
26179+ AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
26180+ mod->h_root = dget(path.dentry);
26181+ path_put(&path);
26182+
4f0767ce 26183+out:
1facf9fc 26184+ return err;
26185+}
26186+
26187+#if 0 /* reserved for future use */
26188+static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
26189+ struct au_opt_mod *mod, substring_t args[])
26190+{
26191+ int err;
26192+ struct dentry *root;
26193+
26194+ err = -EINVAL;
26195+ root = sb->s_root;
26196+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26197+ if (bindex < 0 || au_sbbot(sb) < bindex) {
4a4d8108 26198+ pr_err("out of bounds, %d\n", bindex);
1facf9fc 26199+ goto out;
26200+ }
26201+
26202+ err = 0;
26203+ mod->perm = br_perm_val(args[1].from);
26204+ AuDbg("mod path %s, perm 0x%x, %s\n",
26205+ mod->path, mod->perm, args[1].from);
26206+ mod->h_root = dget(au_h_dptr(root, bindex));
26207+
4f0767ce 26208+out:
1facf9fc 26209+ aufs_read_unlock(root, !AuLock_IR);
26210+ return err;
26211+}
26212+#endif
26213+
26214+static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
26215+ substring_t args[])
26216+{
26217+ int err;
26218+ struct file *file;
26219+
26220+ file = au_xino_create(sb, args[0].from, /*silent*/0);
26221+ err = PTR_ERR(file);
26222+ if (IS_ERR(file))
26223+ goto out;
26224+
26225+ err = -EINVAL;
2000de60 26226+ if (unlikely(file->f_path.dentry->d_sb == sb)) {
1facf9fc 26227+ fput(file);
4a4d8108 26228+ pr_err("%s must be outside\n", args[0].from);
1facf9fc 26229+ goto out;
26230+ }
26231+
26232+ err = 0;
26233+ xino->file = file;
26234+ xino->path = args[0].from;
26235+
4f0767ce 26236+out:
1facf9fc 26237+ return err;
26238+}
26239+
4a4d8108
AM
26240+static int noinline_for_stack
26241+au_opts_parse_xino_itrunc_path(struct super_block *sb,
26242+ struct au_opt_xino_itrunc *xino_itrunc,
26243+ substring_t args[])
1facf9fc 26244+{
26245+ int err;
5afbbe0d 26246+ aufs_bindex_t bbot, bindex;
1facf9fc 26247+ struct path path;
26248+ struct dentry *root;
26249+
26250+ err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
26251+ if (unlikely(err)) {
4a4d8108 26252+ pr_err("lookup failed %s (%d)\n", args[0].from, err);
1facf9fc 26253+ goto out;
26254+ }
26255+
26256+ xino_itrunc->bindex = -1;
26257+ root = sb->s_root;
26258+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d
AM
26259+ bbot = au_sbbot(sb);
26260+ for (bindex = 0; bindex <= bbot; bindex++) {
1facf9fc 26261+ if (au_h_dptr(root, bindex) == path.dentry) {
26262+ xino_itrunc->bindex = bindex;
26263+ break;
26264+ }
26265+ }
26266+ aufs_read_unlock(root, !AuLock_IR);
26267+ path_put(&path);
26268+
26269+ if (unlikely(xino_itrunc->bindex < 0)) {
4a4d8108 26270+ pr_err("no such branch %s\n", args[0].from);
1facf9fc 26271+ err = -EINVAL;
26272+ }
26273+
4f0767ce 26274+out:
1facf9fc 26275+ return err;
26276+}
26277+
26278+/* called without aufs lock */
26279+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
26280+{
26281+ int err, n, token;
26282+ aufs_bindex_t bindex;
26283+ unsigned char skipped;
26284+ struct dentry *root;
26285+ struct au_opt *opt, *opt_tail;
26286+ char *opt_str;
26287+ /* reduce the stack space */
26288+ union {
26289+ struct au_opt_xino_itrunc *xino_itrunc;
26290+ struct au_opt_wbr_create *create;
26291+ } u;
26292+ struct {
26293+ substring_t args[MAX_OPT_ARGS];
26294+ } *a;
26295+
26296+ err = -ENOMEM;
26297+ a = kmalloc(sizeof(*a), GFP_NOFS);
26298+ if (unlikely(!a))
26299+ goto out;
26300+
26301+ root = sb->s_root;
26302+ err = 0;
26303+ bindex = 0;
26304+ opt = opts->opt;
26305+ opt_tail = opt + opts->max_opt - 1;
26306+ opt->type = Opt_tail;
26307+ while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
26308+ err = -EINVAL;
26309+ skipped = 0;
26310+ token = match_token(opt_str, options, a->args);
26311+ switch (token) {
26312+ case Opt_br:
26313+ err = 0;
26314+ while (!err && (opt_str = strsep(&a->args[0].from, ":"))
26315+ && *opt_str) {
26316+ err = opt_add(opt, opt_str, opts->sb_flags,
26317+ bindex++);
26318+ if (unlikely(!err && ++opt > opt_tail)) {
26319+ err = -E2BIG;
26320+ break;
26321+ }
26322+ opt->type = Opt_tail;
26323+ skipped = 1;
26324+ }
26325+ break;
26326+ case Opt_add:
26327+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26328+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26329+ break;
26330+ }
26331+ bindex = n;
26332+ err = opt_add(opt, a->args[1].from, opts->sb_flags,
26333+ bindex);
26334+ if (!err)
26335+ opt->type = token;
26336+ break;
26337+ case Opt_append:
26338+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26339+ /*dummy bindex*/1);
26340+ if (!err)
26341+ opt->type = token;
26342+ break;
26343+ case Opt_prepend:
26344+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26345+ /*bindex*/0);
26346+ if (!err)
26347+ opt->type = token;
26348+ break;
26349+ case Opt_del:
26350+ err = au_opts_parse_del(&opt->del, a->args);
26351+ if (!err)
26352+ opt->type = token;
26353+ break;
26354+#if 0 /* reserved for future use */
26355+ case Opt_idel:
26356+ del->pathname = "(indexed)";
26357+ if (unlikely(match_int(&args[0], &n))) {
4a4d8108 26358+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26359+ break;
26360+ }
26361+ err = au_opts_parse_idel(sb, n, &opt->del, a->args);
26362+ if (!err)
26363+ opt->type = token;
26364+ break;
26365+#endif
26366+ case Opt_mod:
26367+ err = au_opts_parse_mod(&opt->mod, a->args);
26368+ if (!err)
26369+ opt->type = token;
26370+ break;
26371+#ifdef IMOD /* reserved for future use */
26372+ case Opt_imod:
26373+ u.mod->path = "(indexed)";
26374+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26375+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26376+ break;
26377+ }
26378+ err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
26379+ if (!err)
26380+ opt->type = token;
26381+ break;
26382+#endif
26383+ case Opt_xino:
26384+ err = au_opts_parse_xino(sb, &opt->xino, a->args);
26385+ if (!err)
26386+ opt->type = token;
26387+ break;
26388+
26389+ case Opt_trunc_xino_path:
26390+ err = au_opts_parse_xino_itrunc_path
26391+ (sb, &opt->xino_itrunc, a->args);
26392+ if (!err)
26393+ opt->type = token;
26394+ break;
26395+
26396+ case Opt_itrunc_xino:
26397+ u.xino_itrunc = &opt->xino_itrunc;
26398+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26399+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26400+ break;
26401+ }
26402+ u.xino_itrunc->bindex = n;
26403+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26404+ if (n < 0 || au_sbbot(sb) < n) {
4a4d8108 26405+ pr_err("out of bounds, %d\n", n);
1facf9fc 26406+ aufs_read_unlock(root, !AuLock_IR);
26407+ break;
26408+ }
26409+ aufs_read_unlock(root, !AuLock_IR);
26410+ err = 0;
26411+ opt->type = token;
26412+ break;
26413+
26414+ case Opt_dirwh:
26415+ if (unlikely(match_int(&a->args[0], &opt->dirwh)))
26416+ break;
26417+ err = 0;
26418+ opt->type = token;
26419+ break;
26420+
26421+ case Opt_rdcache:
027c5e7a
AM
26422+ if (unlikely(match_int(&a->args[0], &n))) {
26423+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26424+ break;
027c5e7a
AM
26425+ }
26426+ if (unlikely(n > AUFS_RDCACHE_MAX)) {
26427+ pr_err("rdcache must be smaller than %d\n",
26428+ AUFS_RDCACHE_MAX);
26429+ break;
26430+ }
26431+ opt->rdcache = n;
1facf9fc 26432+ err = 0;
26433+ opt->type = token;
26434+ break;
26435+ case Opt_rdblk:
26436+ if (unlikely(match_int(&a->args[0], &n)
1308ab2a 26437+ || n < 0
1facf9fc 26438+ || n > KMALLOC_MAX_SIZE)) {
4a4d8108 26439+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26440+ break;
26441+ }
1308ab2a 26442+ if (unlikely(n && n < NAME_MAX)) {
4a4d8108
AM
26443+ pr_err("rdblk must be larger than %d\n",
26444+ NAME_MAX);
1facf9fc 26445+ break;
26446+ }
26447+ opt->rdblk = n;
26448+ err = 0;
26449+ opt->type = token;
26450+ break;
26451+ case Opt_rdhash:
26452+ if (unlikely(match_int(&a->args[0], &n)
1308ab2a 26453+ || n < 0
1facf9fc 26454+ || n * sizeof(struct hlist_head)
26455+ > KMALLOC_MAX_SIZE)) {
4a4d8108 26456+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26457+ break;
26458+ }
26459+ opt->rdhash = n;
26460+ err = 0;
26461+ opt->type = token;
26462+ break;
26463+
26464+ case Opt_trunc_xino:
26465+ case Opt_notrunc_xino:
26466+ case Opt_noxino:
26467+ case Opt_trunc_xib:
26468+ case Opt_notrunc_xib:
dece6358
AM
26469+ case Opt_shwh:
26470+ case Opt_noshwh:
076b876e
AM
26471+ case Opt_dirperm1:
26472+ case Opt_nodirperm1:
1facf9fc 26473+ case Opt_plink:
26474+ case Opt_noplink:
26475+ case Opt_list_plink:
4a4d8108
AM
26476+ case Opt_dio:
26477+ case Opt_nodio:
1facf9fc 26478+ case Opt_diropq_a:
26479+ case Opt_diropq_w:
26480+ case Opt_warn_perm:
26481+ case Opt_nowarn_perm:
1facf9fc 26482+ case Opt_verbose:
26483+ case Opt_noverbose:
26484+ case Opt_sum:
26485+ case Opt_nosum:
26486+ case Opt_wsum:
dece6358
AM
26487+ case Opt_rdblk_def:
26488+ case Opt_rdhash_def:
8b6a4947
AM
26489+ case Opt_dirren:
26490+ case Opt_nodirren:
c1595e42
JR
26491+ case Opt_acl:
26492+ case Opt_noacl:
1facf9fc 26493+ err = 0;
26494+ opt->type = token;
26495+ break;
26496+
26497+ case Opt_udba:
26498+ opt->udba = udba_val(a->args[0].from);
26499+ if (opt->udba >= 0) {
26500+ err = 0;
26501+ opt->type = token;
26502+ } else
4a4d8108 26503+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26504+ break;
26505+
26506+ case Opt_wbr_create:
26507+ u.create = &opt->wbr_create;
26508+ u.create->wbr_create
26509+ = au_wbr_create_val(a->args[0].from, u.create);
26510+ if (u.create->wbr_create >= 0) {
26511+ err = 0;
26512+ opt->type = token;
26513+ } else
4a4d8108 26514+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26515+ break;
26516+ case Opt_wbr_copyup:
26517+ opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
26518+ if (opt->wbr_copyup >= 0) {
26519+ err = 0;
26520+ opt->type = token;
26521+ } else
4a4d8108 26522+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26523+ break;
26524+
076b876e
AM
26525+ case Opt_fhsm_sec:
26526+ if (unlikely(match_int(&a->args[0], &n)
26527+ || n < 0)) {
26528+ pr_err("bad integer in %s\n", opt_str);
26529+ break;
26530+ }
26531+ if (sysaufs_brs) {
26532+ opt->fhsm_second = n;
26533+ opt->type = token;
26534+ } else
26535+ pr_warn("ignored %s\n", opt_str);
26536+ err = 0;
26537+ break;
26538+
1facf9fc 26539+ case Opt_ignore:
0c3ec466 26540+ pr_warn("ignored %s\n", opt_str);
1facf9fc 26541+ /*FALLTHROUGH*/
26542+ case Opt_ignore_silent:
26543+ skipped = 1;
26544+ err = 0;
26545+ break;
26546+ case Opt_err:
4a4d8108 26547+ pr_err("unknown option %s\n", opt_str);
1facf9fc 26548+ break;
26549+ }
26550+
26551+ if (!err && !skipped) {
26552+ if (unlikely(++opt > opt_tail)) {
26553+ err = -E2BIG;
26554+ opt--;
26555+ opt->type = Opt_tail;
26556+ break;
26557+ }
26558+ opt->type = Opt_tail;
26559+ }
26560+ }
26561+
1c60b727 26562+ kfree(a);
1facf9fc 26563+ dump_opts(opts);
26564+ if (unlikely(err))
26565+ au_opts_free(opts);
26566+
4f0767ce 26567+out:
1facf9fc 26568+ return err;
26569+}
26570+
26571+static int au_opt_wbr_create(struct super_block *sb,
26572+ struct au_opt_wbr_create *create)
26573+{
26574+ int err;
26575+ struct au_sbinfo *sbinfo;
26576+
dece6358
AM
26577+ SiMustWriteLock(sb);
26578+
1facf9fc 26579+ err = 1; /* handled */
26580+ sbinfo = au_sbi(sb);
26581+ if (sbinfo->si_wbr_create_ops->fin) {
26582+ err = sbinfo->si_wbr_create_ops->fin(sb);
26583+ if (!err)
26584+ err = 1;
26585+ }
26586+
26587+ sbinfo->si_wbr_create = create->wbr_create;
26588+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
26589+ switch (create->wbr_create) {
26590+ case AuWbrCreate_MFSRRV:
26591+ case AuWbrCreate_MFSRR:
f2c43d5f
AM
26592+ case AuWbrCreate_TDMFS:
26593+ case AuWbrCreate_TDMFSV:
392086de
AM
26594+ case AuWbrCreate_PMFSRR:
26595+ case AuWbrCreate_PMFSRRV:
1facf9fc 26596+ sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
26597+ /*FALLTHROUGH*/
26598+ case AuWbrCreate_MFS:
26599+ case AuWbrCreate_MFSV:
26600+ case AuWbrCreate_PMFS:
26601+ case AuWbrCreate_PMFSV:
e49829fe
JR
26602+ sbinfo->si_wbr_mfs.mfs_expire
26603+ = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
1facf9fc 26604+ break;
26605+ }
26606+
26607+ if (sbinfo->si_wbr_create_ops->init)
26608+ sbinfo->si_wbr_create_ops->init(sb); /* ignore */
26609+
26610+ return err;
26611+}
26612+
26613+/*
26614+ * returns,
26615+ * plus: processed without an error
26616+ * zero: unprocessed
26617+ */
26618+static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
26619+ struct au_opts *opts)
26620+{
26621+ int err;
26622+ struct au_sbinfo *sbinfo;
26623+
dece6358
AM
26624+ SiMustWriteLock(sb);
26625+
1facf9fc 26626+ err = 1; /* handled */
26627+ sbinfo = au_sbi(sb);
26628+ switch (opt->type) {
26629+ case Opt_udba:
26630+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
26631+ sbinfo->si_mntflags |= opt->udba;
26632+ opts->given_udba |= opt->udba;
26633+ break;
26634+
26635+ case Opt_plink:
26636+ au_opt_set(sbinfo->si_mntflags, PLINK);
26637+ break;
26638+ case Opt_noplink:
26639+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
e49829fe 26640+ au_plink_put(sb, /*verbose*/1);
1facf9fc 26641+ au_opt_clr(sbinfo->si_mntflags, PLINK);
26642+ break;
26643+ case Opt_list_plink:
26644+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
26645+ au_plink_list(sb);
26646+ break;
26647+
4a4d8108
AM
26648+ case Opt_dio:
26649+ au_opt_set(sbinfo->si_mntflags, DIO);
26650+ au_fset_opts(opts->flags, REFRESH_DYAOP);
26651+ break;
26652+ case Opt_nodio:
26653+ au_opt_clr(sbinfo->si_mntflags, DIO);
26654+ au_fset_opts(opts->flags, REFRESH_DYAOP);
26655+ break;
26656+
076b876e
AM
26657+ case Opt_fhsm_sec:
26658+ au_fhsm_set(sbinfo, opt->fhsm_second);
26659+ break;
26660+
1facf9fc 26661+ case Opt_diropq_a:
26662+ au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26663+ break;
26664+ case Opt_diropq_w:
26665+ au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26666+ break;
26667+
26668+ case Opt_warn_perm:
26669+ au_opt_set(sbinfo->si_mntflags, WARN_PERM);
26670+ break;
26671+ case Opt_nowarn_perm:
26672+ au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
26673+ break;
26674+
1facf9fc 26675+ case Opt_verbose:
26676+ au_opt_set(sbinfo->si_mntflags, VERBOSE);
26677+ break;
26678+ case Opt_noverbose:
26679+ au_opt_clr(sbinfo->si_mntflags, VERBOSE);
26680+ break;
26681+
26682+ case Opt_sum:
26683+ au_opt_set(sbinfo->si_mntflags, SUM);
26684+ break;
26685+ case Opt_wsum:
26686+ au_opt_clr(sbinfo->si_mntflags, SUM);
26687+ au_opt_set(sbinfo->si_mntflags, SUM_W);
26688+ case Opt_nosum:
26689+ au_opt_clr(sbinfo->si_mntflags, SUM);
26690+ au_opt_clr(sbinfo->si_mntflags, SUM_W);
26691+ break;
26692+
26693+ case Opt_wbr_create:
26694+ err = au_opt_wbr_create(sb, &opt->wbr_create);
26695+ break;
26696+ case Opt_wbr_copyup:
26697+ sbinfo->si_wbr_copyup = opt->wbr_copyup;
26698+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
26699+ break;
26700+
26701+ case Opt_dirwh:
26702+ sbinfo->si_dirwh = opt->dirwh;
26703+ break;
26704+
26705+ case Opt_rdcache:
e49829fe
JR
26706+ sbinfo->si_rdcache
26707+ = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
1facf9fc 26708+ break;
26709+ case Opt_rdblk:
26710+ sbinfo->si_rdblk = opt->rdblk;
26711+ break;
dece6358
AM
26712+ case Opt_rdblk_def:
26713+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
26714+ break;
1facf9fc 26715+ case Opt_rdhash:
26716+ sbinfo->si_rdhash = opt->rdhash;
26717+ break;
dece6358
AM
26718+ case Opt_rdhash_def:
26719+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
26720+ break;
26721+
26722+ case Opt_shwh:
26723+ au_opt_set(sbinfo->si_mntflags, SHWH);
26724+ break;
26725+ case Opt_noshwh:
26726+ au_opt_clr(sbinfo->si_mntflags, SHWH);
26727+ break;
1facf9fc 26728+
076b876e
AM
26729+ case Opt_dirperm1:
26730+ au_opt_set(sbinfo->si_mntflags, DIRPERM1);
26731+ break;
26732+ case Opt_nodirperm1:
26733+ au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
26734+ break;
26735+
1facf9fc 26736+ case Opt_trunc_xino:
26737+ au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
26738+ break;
26739+ case Opt_notrunc_xino:
26740+ au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
26741+ break;
26742+
26743+ case Opt_trunc_xino_path:
26744+ case Opt_itrunc_xino:
26745+ err = au_xino_trunc(sb, opt->xino_itrunc.bindex);
26746+ if (!err)
26747+ err = 1;
26748+ break;
26749+
26750+ case Opt_trunc_xib:
26751+ au_fset_opts(opts->flags, TRUNC_XIB);
26752+ break;
26753+ case Opt_notrunc_xib:
26754+ au_fclr_opts(opts->flags, TRUNC_XIB);
26755+ break;
26756+
8b6a4947
AM
26757+ case Opt_dirren:
26758+ err = 1;
26759+ if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
26760+ err = au_dr_opt_set(sb);
26761+ if (!err)
26762+ err = 1;
26763+ }
26764+ if (err == 1)
26765+ au_opt_set(sbinfo->si_mntflags, DIRREN);
26766+ break;
26767+ case Opt_nodirren:
26768+ err = 1;
26769+ if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
26770+ err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
26771+ DR_FLUSHED));
26772+ if (!err)
26773+ err = 1;
26774+ }
26775+ if (err == 1)
26776+ au_opt_clr(sbinfo->si_mntflags, DIRREN);
26777+ break;
26778+
c1595e42
JR
26779+ case Opt_acl:
26780+ sb->s_flags |= MS_POSIXACL;
26781+ break;
26782+ case Opt_noacl:
26783+ sb->s_flags &= ~MS_POSIXACL;
26784+ break;
26785+
1facf9fc 26786+ default:
26787+ err = 0;
26788+ break;
26789+ }
26790+
26791+ return err;
26792+}
26793+
26794+/*
26795+ * returns tri-state.
26796+ * plus: processed without an error
26797+ * zero: unprocessed
26798+ * minus: error
26799+ */
26800+static int au_opt_br(struct super_block *sb, struct au_opt *opt,
26801+ struct au_opts *opts)
26802+{
26803+ int err, do_refresh;
26804+
26805+ err = 0;
26806+ switch (opt->type) {
26807+ case Opt_append:
5afbbe0d 26808+ opt->add.bindex = au_sbbot(sb) + 1;
1facf9fc 26809+ if (opt->add.bindex < 0)
26810+ opt->add.bindex = 0;
26811+ goto add;
26812+ case Opt_prepend:
26813+ opt->add.bindex = 0;
f6b6e03d 26814+ add: /* indented label */
1facf9fc 26815+ case Opt_add:
26816+ err = au_br_add(sb, &opt->add,
26817+ au_ftest_opts(opts->flags, REMOUNT));
26818+ if (!err) {
26819+ err = 1;
027c5e7a 26820+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 26821+ }
26822+ break;
26823+
26824+ case Opt_del:
26825+ case Opt_idel:
26826+ err = au_br_del(sb, &opt->del,
26827+ au_ftest_opts(opts->flags, REMOUNT));
26828+ if (!err) {
26829+ err = 1;
26830+ au_fset_opts(opts->flags, TRUNC_XIB);
027c5e7a 26831+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 26832+ }
26833+ break;
26834+
26835+ case Opt_mod:
26836+ case Opt_imod:
26837+ err = au_br_mod(sb, &opt->mod,
26838+ au_ftest_opts(opts->flags, REMOUNT),
26839+ &do_refresh);
26840+ if (!err) {
26841+ err = 1;
027c5e7a
AM
26842+ if (do_refresh)
26843+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 26844+ }
26845+ break;
26846+ }
1facf9fc 26847+ return err;
26848+}
26849+
26850+static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
26851+ struct au_opt_xino **opt_xino,
26852+ struct au_opts *opts)
26853+{
26854+ int err;
5afbbe0d 26855+ aufs_bindex_t bbot, bindex;
1facf9fc 26856+ struct dentry *root, *parent, *h_root;
26857+
26858+ err = 0;
26859+ switch (opt->type) {
26860+ case Opt_xino:
26861+ err = au_xino_set(sb, &opt->xino,
26862+ !!au_ftest_opts(opts->flags, REMOUNT));
26863+ if (unlikely(err))
26864+ break;
26865+
26866+ *opt_xino = &opt->xino;
26867+ au_xino_brid_set(sb, -1);
26868+
26869+ /* safe d_parent access */
2000de60 26870+ parent = opt->xino.file->f_path.dentry->d_parent;
1facf9fc 26871+ root = sb->s_root;
5afbbe0d
AM
26872+ bbot = au_sbbot(sb);
26873+ for (bindex = 0; bindex <= bbot; bindex++) {
1facf9fc 26874+ h_root = au_h_dptr(root, bindex);
26875+ if (h_root == parent) {
26876+ au_xino_brid_set(sb, au_sbr_id(sb, bindex));
26877+ break;
26878+ }
26879+ }
26880+ break;
26881+
26882+ case Opt_noxino:
26883+ au_xino_clr(sb);
26884+ au_xino_brid_set(sb, -1);
26885+ *opt_xino = (void *)-1;
26886+ break;
26887+ }
26888+
26889+ return err;
26890+}
26891+
26892+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
26893+ unsigned int pending)
26894+{
076b876e 26895+ int err, fhsm;
5afbbe0d 26896+ aufs_bindex_t bindex, bbot;
79b8bda9 26897+ unsigned char do_plink, skip, do_free, can_no_dreval;
1facf9fc 26898+ struct au_branch *br;
26899+ struct au_wbr *wbr;
79b8bda9 26900+ struct dentry *root, *dentry;
1facf9fc 26901+ struct inode *dir, *h_dir;
26902+ struct au_sbinfo *sbinfo;
26903+ struct au_hinode *hdir;
26904+
dece6358
AM
26905+ SiMustAnyLock(sb);
26906+
1facf9fc 26907+ sbinfo = au_sbi(sb);
26908+ AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
26909+
dece6358
AM
26910+ if (!(sb_flags & MS_RDONLY)) {
26911+ if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
0c3ec466 26912+ pr_warn("first branch should be rw\n");
dece6358 26913+ if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
febd17d6 26914+ pr_warn_once("shwh should be used with ro\n");
dece6358 26915+ }
1facf9fc 26916+
4a4d8108 26917+ if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
1facf9fc 26918+ && !au_opt_test(sbinfo->si_mntflags, XINO))
febd17d6 26919+ pr_warn_once("udba=*notify requires xino\n");
1facf9fc 26920+
076b876e 26921+ if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
febd17d6
JR
26922+ pr_warn_once("dirperm1 breaks the protection"
26923+ " by the permission bits on the lower branch\n");
076b876e 26924+
1facf9fc 26925+ err = 0;
076b876e 26926+ fhsm = 0;
1facf9fc 26927+ root = sb->s_root;
5527c038 26928+ dir = d_inode(root);
1facf9fc 26929+ do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
79b8bda9
AM
26930+ can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
26931+ UDBA_NONE);
5afbbe0d
AM
26932+ bbot = au_sbbot(sb);
26933+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
1facf9fc 26934+ skip = 0;
26935+ h_dir = au_h_iptr(dir, bindex);
26936+ br = au_sbr(sb, bindex);
1facf9fc 26937+
c1595e42
JR
26938+ if ((br->br_perm & AuBrAttr_ICEX)
26939+ && !h_dir->i_op->listxattr)
26940+ br->br_perm &= ~AuBrAttr_ICEX;
26941+#if 0
26942+ if ((br->br_perm & AuBrAttr_ICEX_SEC)
26943+ && (au_br_sb(br)->s_flags & MS_NOSEC))
26944+ br->br_perm &= ~AuBrAttr_ICEX_SEC;
26945+#endif
26946+
26947+ do_free = 0;
1facf9fc 26948+ wbr = br->br_wbr;
26949+ if (wbr)
26950+ wbr_wh_read_lock(wbr);
26951+
1e00d052 26952+ if (!au_br_writable(br->br_perm)) {
1facf9fc 26953+ do_free = !!wbr;
26954+ skip = (!wbr
26955+ || (!wbr->wbr_whbase
26956+ && !wbr->wbr_plink
26957+ && !wbr->wbr_orph));
1e00d052 26958+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 26959+ /* skip = (!br->br_whbase && !br->br_orph); */
26960+ skip = (!wbr || !wbr->wbr_whbase);
26961+ if (skip && wbr) {
26962+ if (do_plink)
26963+ skip = !!wbr->wbr_plink;
26964+ else
26965+ skip = !wbr->wbr_plink;
26966+ }
1e00d052 26967+ } else {
1facf9fc 26968+ /* skip = (br->br_whbase && br->br_ohph); */
26969+ skip = (wbr && wbr->wbr_whbase);
26970+ if (skip) {
26971+ if (do_plink)
26972+ skip = !!wbr->wbr_plink;
26973+ else
26974+ skip = !wbr->wbr_plink;
26975+ }
1facf9fc 26976+ }
26977+ if (wbr)
26978+ wbr_wh_read_unlock(wbr);
26979+
79b8bda9
AM
26980+ if (can_no_dreval) {
26981+ dentry = br->br_path.dentry;
26982+ spin_lock(&dentry->d_lock);
26983+ if (dentry->d_flags &
26984+ (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
26985+ can_no_dreval = 0;
26986+ spin_unlock(&dentry->d_lock);
26987+ }
26988+
076b876e
AM
26989+ if (au_br_fhsm(br->br_perm)) {
26990+ fhsm++;
26991+ AuDebugOn(!br->br_fhsm);
26992+ }
26993+
1facf9fc 26994+ if (skip)
26995+ continue;
26996+
26997+ hdir = au_hi(dir, bindex);
5afbbe0d 26998+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 26999+ if (wbr)
27000+ wbr_wh_write_lock(wbr);
86dc4139 27001+ err = au_wh_init(br, sb);
1facf9fc 27002+ if (wbr)
27003+ wbr_wh_write_unlock(wbr);
5afbbe0d 27004+ au_hn_inode_unlock(hdir);
1facf9fc 27005+
27006+ if (!err && do_free) {
1c60b727 27007+ kfree(wbr);
1facf9fc 27008+ br->br_wbr = NULL;
27009+ }
27010+ }
27011+
79b8bda9
AM
27012+ if (can_no_dreval)
27013+ au_fset_si(sbinfo, NO_DREVAL);
27014+ else
27015+ au_fclr_si(sbinfo, NO_DREVAL);
27016+
c1595e42 27017+ if (fhsm >= 2) {
076b876e 27018+ au_fset_si(sbinfo, FHSM);
5afbbe0d 27019+ for (bindex = bbot; bindex >= 0; bindex--) {
c1595e42
JR
27020+ br = au_sbr(sb, bindex);
27021+ if (au_br_fhsm(br->br_perm)) {
27022+ au_fhsm_set_bottom(sb, bindex);
27023+ break;
27024+ }
27025+ }
27026+ } else {
076b876e 27027+ au_fclr_si(sbinfo, FHSM);
c1595e42
JR
27028+ au_fhsm_set_bottom(sb, -1);
27029+ }
076b876e 27030+
1facf9fc 27031+ return err;
27032+}
27033+
27034+int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27035+{
27036+ int err;
27037+ unsigned int tmp;
5afbbe0d 27038+ aufs_bindex_t bindex, bbot;
1facf9fc 27039+ struct au_opt *opt;
27040+ struct au_opt_xino *opt_xino, xino;
27041+ struct au_sbinfo *sbinfo;
027c5e7a 27042+ struct au_branch *br;
076b876e 27043+ struct inode *dir;
1facf9fc 27044+
dece6358
AM
27045+ SiMustWriteLock(sb);
27046+
1facf9fc 27047+ err = 0;
27048+ opt_xino = NULL;
27049+ opt = opts->opt;
27050+ while (err >= 0 && opt->type != Opt_tail)
27051+ err = au_opt_simple(sb, opt++, opts);
27052+ if (err > 0)
27053+ err = 0;
27054+ else if (unlikely(err < 0))
27055+ goto out;
27056+
27057+ /* disable xino and udba temporary */
27058+ sbinfo = au_sbi(sb);
27059+ tmp = sbinfo->si_mntflags;
27060+ au_opt_clr(sbinfo->si_mntflags, XINO);
27061+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27062+
27063+ opt = opts->opt;
27064+ while (err >= 0 && opt->type != Opt_tail)
27065+ err = au_opt_br(sb, opt++, opts);
27066+ if (err > 0)
27067+ err = 0;
27068+ else if (unlikely(err < 0))
27069+ goto out;
27070+
5afbbe0d
AM
27071+ bbot = au_sbbot(sb);
27072+ if (unlikely(bbot < 0)) {
1facf9fc 27073+ err = -EINVAL;
4a4d8108 27074+ pr_err("no branches\n");
1facf9fc 27075+ goto out;
27076+ }
27077+
27078+ if (au_opt_test(tmp, XINO))
27079+ au_opt_set(sbinfo->si_mntflags, XINO);
27080+ opt = opts->opt;
27081+ while (!err && opt->type != Opt_tail)
27082+ err = au_opt_xino(sb, opt++, &opt_xino, opts);
27083+ if (unlikely(err))
27084+ goto out;
27085+
27086+ err = au_opts_verify(sb, sb->s_flags, tmp);
27087+ if (unlikely(err))
27088+ goto out;
27089+
27090+ /* restore xino */
27091+ if (au_opt_test(tmp, XINO) && !opt_xino) {
27092+ xino.file = au_xino_def(sb);
27093+ err = PTR_ERR(xino.file);
27094+ if (IS_ERR(xino.file))
27095+ goto out;
27096+
27097+ err = au_xino_set(sb, &xino, /*remount*/0);
27098+ fput(xino.file);
27099+ if (unlikely(err))
27100+ goto out;
27101+ }
27102+
27103+ /* restore udba */
027c5e7a 27104+ tmp &= AuOptMask_UDBA;
1facf9fc 27105+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
027c5e7a 27106+ sbinfo->si_mntflags |= tmp;
5afbbe0d
AM
27107+ bbot = au_sbbot(sb);
27108+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
27109+ br = au_sbr(sb, bindex);
27110+ err = au_hnotify_reset_br(tmp, br, br->br_perm);
27111+ if (unlikely(err))
27112+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
27113+ bindex, err);
27114+ /* go on even if err */
27115+ }
4a4d8108 27116+ if (au_opt_test(tmp, UDBA_HNOTIFY)) {
5527c038 27117+ dir = d_inode(sb->s_root);
4a4d8108 27118+ au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
1facf9fc 27119+ }
27120+
4f0767ce 27121+out:
1facf9fc 27122+ return err;
27123+}
27124+
27125+int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27126+{
27127+ int err, rerr;
79b8bda9 27128+ unsigned char no_dreval;
1facf9fc 27129+ struct inode *dir;
27130+ struct au_opt_xino *opt_xino;
27131+ struct au_opt *opt;
27132+ struct au_sbinfo *sbinfo;
27133+
dece6358
AM
27134+ SiMustWriteLock(sb);
27135+
8b6a4947
AM
27136+ err = au_dr_opt_flush(sb);
27137+ if (unlikely(err))
27138+ goto out;
27139+ au_fset_opts(opts->flags, DR_FLUSHED);
27140+
5527c038 27141+ dir = d_inode(sb->s_root);
1facf9fc 27142+ sbinfo = au_sbi(sb);
1facf9fc 27143+ opt_xino = NULL;
27144+ opt = opts->opt;
27145+ while (err >= 0 && opt->type != Opt_tail) {
27146+ err = au_opt_simple(sb, opt, opts);
27147+ if (!err)
27148+ err = au_opt_br(sb, opt, opts);
27149+ if (!err)
27150+ err = au_opt_xino(sb, opt, &opt_xino, opts);
27151+ opt++;
27152+ }
27153+ if (err > 0)
27154+ err = 0;
27155+ AuTraceErr(err);
27156+ /* go on even err */
27157+
79b8bda9 27158+ no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
1facf9fc 27159+ rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27160+ if (unlikely(rerr && !err))
27161+ err = rerr;
27162+
79b8bda9 27163+ if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
b95c5147 27164+ au_fset_opts(opts->flags, REFRESH_IDOP);
79b8bda9 27165+
1facf9fc 27166+ if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27167+ rerr = au_xib_trunc(sb);
27168+ if (unlikely(rerr && !err))
27169+ err = rerr;
27170+ }
27171+
27172+ /* will be handled by the caller */
027c5e7a 27173+ if (!au_ftest_opts(opts->flags, REFRESH)
79b8bda9
AM
27174+ && (opts->given_udba
27175+ || au_opt_test(sbinfo->si_mntflags, XINO)
b95c5147 27176+ || au_ftest_opts(opts->flags, REFRESH_IDOP)
79b8bda9 27177+ ))
027c5e7a 27178+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27179+
27180+ AuDbg("status 0x%x\n", opts->flags);
8b6a4947
AM
27181+
27182+out:
1facf9fc 27183+ return err;
27184+}
27185+
27186+/* ---------------------------------------------------------------------- */
27187+
27188+unsigned int au_opt_udba(struct super_block *sb)
27189+{
27190+ return au_mntflags(sb) & AuOptMask_UDBA;
27191+}
7f207e10
AM
27192diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27193--- /usr/share/empty/fs/aufs/opts.h 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
27194+++ linux/fs/aufs/opts.h 2017-11-12 22:24:44.707577830 +0100
27195@@ -0,0 +1,224 @@
1facf9fc 27196+/*
a2654f78 27197+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 27198+ *
27199+ * This program, aufs is free software; you can redistribute it and/or modify
27200+ * it under the terms of the GNU General Public License as published by
27201+ * the Free Software Foundation; either version 2 of the License, or
27202+ * (at your option) any later version.
dece6358
AM
27203+ *
27204+ * This program is distributed in the hope that it will be useful,
27205+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27206+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27207+ * GNU General Public License for more details.
27208+ *
27209+ * You should have received a copy of the GNU General Public License
523b37e3 27210+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 27211+ */
27212+
27213+/*
27214+ * mount options/flags
27215+ */
27216+
27217+#ifndef __AUFS_OPTS_H__
27218+#define __AUFS_OPTS_H__
27219+
27220+#ifdef __KERNEL__
27221+
dece6358 27222+#include <linux/path.h>
1facf9fc 27223+
dece6358 27224+struct file;
dece6358 27225+
1facf9fc 27226+/* ---------------------------------------------------------------------- */
27227+
27228+/* mount flags */
27229+#define AuOpt_XINO 1 /* external inode number bitmap
27230+ and translation table */
27231+#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
27232+#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
27233+#define AuOpt_UDBA_REVAL (1 << 3)
4a4d8108 27234+#define AuOpt_UDBA_HNOTIFY (1 << 4)
dece6358
AM
27235+#define AuOpt_SHWH (1 << 5) /* show whiteout */
27236+#define AuOpt_PLINK (1 << 6) /* pseudo-link */
076b876e
AM
27237+#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
27238+ bits */
dece6358
AM
27239+#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
27240+#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
27241+#define AuOpt_SUM_W (1 << 11) /* unimplemented */
27242+#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
27243+#define AuOpt_VERBOSE (1 << 13) /* busy inode when del-branch */
4a4d8108 27244+#define AuOpt_DIO (1 << 14) /* direct io */
8b6a4947 27245+#define AuOpt_DIRREN (1 << 15) /* directory rename */
1facf9fc 27246+
4a4d8108
AM
27247+#ifndef CONFIG_AUFS_HNOTIFY
27248+#undef AuOpt_UDBA_HNOTIFY
27249+#define AuOpt_UDBA_HNOTIFY 0
1facf9fc 27250+#endif
8b6a4947
AM
27251+#ifndef CONFIG_AUFS_DIRREN
27252+#undef AuOpt_DIRREN
27253+#define AuOpt_DIRREN 0
27254+#endif
dece6358
AM
27255+#ifndef CONFIG_AUFS_SHWH
27256+#undef AuOpt_SHWH
27257+#define AuOpt_SHWH 0
27258+#endif
1facf9fc 27259+
27260+#define AuOpt_Def (AuOpt_XINO \
27261+ | AuOpt_UDBA_REVAL \
27262+ | AuOpt_PLINK \
27263+ /* | AuOpt_DIRPERM1 */ \
27264+ | AuOpt_WARN_PERM)
27265+#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27266+ | AuOpt_UDBA_REVAL \
4a4d8108 27267+ | AuOpt_UDBA_HNOTIFY)
1facf9fc 27268+
27269+#define au_opt_test(flags, name) (flags & AuOpt_##name)
27270+#define au_opt_set(flags, name) do { \
27271+ BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27272+ ((flags) |= AuOpt_##name); \
27273+} while (0)
27274+#define au_opt_set_udba(flags, name) do { \
27275+ (flags) &= ~AuOptMask_UDBA; \
27276+ ((flags) |= AuOpt_##name); \
27277+} while (0)
7f207e10
AM
27278+#define au_opt_clr(flags, name) do { \
27279+ ((flags) &= ~AuOpt_##name); \
27280+} while (0)
1facf9fc 27281+
e49829fe
JR
27282+static inline unsigned int au_opts_plink(unsigned int mntflags)
27283+{
27284+#ifdef CONFIG_PROC_FS
27285+ return mntflags;
27286+#else
27287+ return mntflags & ~AuOpt_PLINK;
27288+#endif
27289+}
27290+
1facf9fc 27291+/* ---------------------------------------------------------------------- */
27292+
27293+/* policies to select one among multiple writable branches */
27294+enum {
27295+ AuWbrCreate_TDP, /* top down parent */
27296+ AuWbrCreate_RR, /* round robin */
27297+ AuWbrCreate_MFS, /* most free space */
27298+ AuWbrCreate_MFSV, /* mfs with seconds */
27299+ AuWbrCreate_MFSRR, /* mfs then rr */
27300+ AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
f2c43d5f
AM
27301+ AuWbrCreate_TDMFS, /* top down regardless parent and mfs */
27302+ AuWbrCreate_TDMFSV, /* top down regardless parent and mfs */
1facf9fc 27303+ AuWbrCreate_PMFS, /* parent and mfs */
27304+ AuWbrCreate_PMFSV, /* parent and mfs with seconds */
392086de
AM
27305+ AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
27306+ AuWbrCreate_PMFSRRV, /* plus seconds */
1facf9fc 27307+
27308+ AuWbrCreate_Def = AuWbrCreate_TDP
27309+};
27310+
27311+enum {
27312+ AuWbrCopyup_TDP, /* top down parent */
27313+ AuWbrCopyup_BUP, /* bottom up parent */
27314+ AuWbrCopyup_BU, /* bottom up */
27315+
27316+ AuWbrCopyup_Def = AuWbrCopyup_TDP
27317+};
27318+
27319+/* ---------------------------------------------------------------------- */
27320+
27321+struct au_opt_add {
27322+ aufs_bindex_t bindex;
27323+ char *pathname;
27324+ int perm;
27325+ struct path path;
27326+};
27327+
27328+struct au_opt_del {
27329+ char *pathname;
27330+ struct path h_path;
27331+};
27332+
27333+struct au_opt_mod {
27334+ char *path;
27335+ int perm;
27336+ struct dentry *h_root;
27337+};
27338+
27339+struct au_opt_xino {
27340+ char *path;
27341+ struct file *file;
27342+};
27343+
27344+struct au_opt_xino_itrunc {
27345+ aufs_bindex_t bindex;
27346+};
27347+
27348+struct au_opt_wbr_create {
27349+ int wbr_create;
27350+ int mfs_second;
27351+ unsigned long long mfsrr_watermark;
27352+};
27353+
27354+struct au_opt {
27355+ int type;
27356+ union {
27357+ struct au_opt_xino xino;
27358+ struct au_opt_xino_itrunc xino_itrunc;
27359+ struct au_opt_add add;
27360+ struct au_opt_del del;
27361+ struct au_opt_mod mod;
27362+ int dirwh;
27363+ int rdcache;
27364+ unsigned int rdblk;
27365+ unsigned int rdhash;
27366+ int udba;
27367+ struct au_opt_wbr_create wbr_create;
27368+ int wbr_copyup;
076b876e 27369+ unsigned int fhsm_second;
1facf9fc 27370+ };
27371+};
27372+
27373+/* opts flags */
27374+#define AuOpts_REMOUNT 1
027c5e7a
AM
27375+#define AuOpts_REFRESH (1 << 1)
27376+#define AuOpts_TRUNC_XIB (1 << 2)
27377+#define AuOpts_REFRESH_DYAOP (1 << 3)
b95c5147 27378+#define AuOpts_REFRESH_IDOP (1 << 4)
8b6a4947 27379+#define AuOpts_DR_FLUSHED (1 << 5)
1facf9fc 27380+#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
7f207e10
AM
27381+#define au_fset_opts(flags, name) \
27382+ do { (flags) |= AuOpts_##name; } while (0)
27383+#define au_fclr_opts(flags, name) \
27384+ do { (flags) &= ~AuOpts_##name; } while (0)
1facf9fc 27385+
8b6a4947
AM
27386+#ifndef CONFIG_AUFS_DIRREN
27387+#undef AuOpts_DR_FLUSHED
27388+#define AuOpts_DR_FLUSHED 0
27389+#endif
27390+
1facf9fc 27391+struct au_opts {
27392+ struct au_opt *opt;
27393+ int max_opt;
27394+
27395+ unsigned int given_udba;
27396+ unsigned int flags;
27397+ unsigned long sb_flags;
27398+};
27399+
27400+/* ---------------------------------------------------------------------- */
27401+
7e9cd9fe 27402+/* opts.c */
076b876e 27403+void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
1facf9fc 27404+const char *au_optstr_udba(int udba);
27405+const char *au_optstr_wbr_copyup(int wbr_copyup);
27406+const char *au_optstr_wbr_create(int wbr_create);
27407+
27408+void au_opts_free(struct au_opts *opts);
3c1bdaff 27409+struct super_block;
1facf9fc 27410+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
27411+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27412+ unsigned int pending);
27413+int au_opts_mount(struct super_block *sb, struct au_opts *opts);
27414+int au_opts_remount(struct super_block *sb, struct au_opts *opts);
27415+
27416+unsigned int au_opt_udba(struct super_block *sb);
27417+
1facf9fc 27418+#endif /* __KERNEL__ */
27419+#endif /* __AUFS_OPTS_H__ */
7f207e10
AM
27420diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
27421--- /usr/share/empty/fs/aufs/plink.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
27422+++ linux/fs/aufs/plink.c 2017-11-12 22:24:44.707577830 +0100
27423@@ -0,0 +1,515 @@
1facf9fc 27424+/*
a2654f78 27425+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 27426+ *
27427+ * This program, aufs is free software; you can redistribute it and/or modify
27428+ * it under the terms of the GNU General Public License as published by
27429+ * the Free Software Foundation; either version 2 of the License, or
27430+ * (at your option) any later version.
dece6358
AM
27431+ *
27432+ * This program is distributed in the hope that it will be useful,
27433+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27434+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27435+ * GNU General Public License for more details.
27436+ *
27437+ * You should have received a copy of the GNU General Public License
523b37e3 27438+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 27439+ */
27440+
27441+/*
27442+ * pseudo-link
27443+ */
27444+
27445+#include "aufs.h"
27446+
27447+/*
e49829fe 27448+ * the pseudo-link maintenance mode.
1facf9fc 27449+ * during a user process maintains the pseudo-links,
27450+ * prohibit adding a new plink and branch manipulation.
e49829fe
JR
27451+ *
27452+ * Flags
27453+ * NOPLM:
27454+ * For entry functions which will handle plink, and i_mutex is already held
27455+ * in VFS.
27456+ * They cannot wait and should return an error at once.
27457+ * Callers has to check the error.
27458+ * NOPLMW:
27459+ * For entry functions which will handle plink, but i_mutex is not held
27460+ * in VFS.
27461+ * They can wait the plink maintenance mode to finish.
27462+ *
27463+ * They behave like F_SETLK and F_SETLKW.
27464+ * If the caller never handle plink, then both flags are unnecessary.
1facf9fc 27465+ */
e49829fe
JR
27466+
27467+int au_plink_maint(struct super_block *sb, int flags)
1facf9fc 27468+{
e49829fe
JR
27469+ int err;
27470+ pid_t pid, ppid;
f0c0a007 27471+ struct task_struct *parent, *prev;
e49829fe 27472+ struct au_sbinfo *sbi;
dece6358
AM
27473+
27474+ SiMustAnyLock(sb);
27475+
e49829fe
JR
27476+ err = 0;
27477+ if (!au_opt_test(au_mntflags(sb), PLINK))
27478+ goto out;
27479+
27480+ sbi = au_sbi(sb);
27481+ pid = sbi->si_plink_maint_pid;
27482+ if (!pid || pid == current->pid)
27483+ goto out;
27484+
27485+ /* todo: it highly depends upon /sbin/mount.aufs */
f0c0a007
AM
27486+ prev = NULL;
27487+ parent = current;
27488+ ppid = 0;
e49829fe 27489+ rcu_read_lock();
f0c0a007
AM
27490+ while (1) {
27491+ parent = rcu_dereference(parent->real_parent);
27492+ if (parent == prev)
27493+ break;
27494+ ppid = task_pid_vnr(parent);
27495+ if (pid == ppid) {
27496+ rcu_read_unlock();
27497+ goto out;
27498+ }
27499+ prev = parent;
27500+ }
e49829fe 27501+ rcu_read_unlock();
e49829fe
JR
27502+
27503+ if (au_ftest_lock(flags, NOPLMW)) {
027c5e7a
AM
27504+ /* if there is no i_mutex lock in VFS, we don't need to wait */
27505+ /* AuDebugOn(!lockdep_depth(current)); */
e49829fe
JR
27506+ while (sbi->si_plink_maint_pid) {
27507+ si_read_unlock(sb);
27508+ /* gave up wake_up_bit() */
27509+ wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
27510+
27511+ if (au_ftest_lock(flags, FLUSH))
27512+ au_nwt_flush(&sbi->si_nowait);
27513+ si_noflush_read_lock(sb);
27514+ }
27515+ } else if (au_ftest_lock(flags, NOPLM)) {
27516+ AuDbg("ppid %d, pid %d\n", ppid, pid);
27517+ err = -EAGAIN;
27518+ }
27519+
27520+out:
27521+ return err;
4a4d8108
AM
27522+}
27523+
e49829fe 27524+void au_plink_maint_leave(struct au_sbinfo *sbinfo)
4a4d8108 27525+{
4a4d8108 27526+ spin_lock(&sbinfo->si_plink_maint_lock);
027c5e7a 27527+ sbinfo->si_plink_maint_pid = 0;
4a4d8108 27528+ spin_unlock(&sbinfo->si_plink_maint_lock);
027c5e7a 27529+ wake_up_all(&sbinfo->si_plink_wq);
4a4d8108
AM
27530+}
27531+
e49829fe 27532+int au_plink_maint_enter(struct super_block *sb)
4a4d8108
AM
27533+{
27534+ int err;
4a4d8108
AM
27535+ struct au_sbinfo *sbinfo;
27536+
27537+ err = 0;
4a4d8108
AM
27538+ sbinfo = au_sbi(sb);
27539+ /* make sure i am the only one in this fs */
e49829fe
JR
27540+ si_write_lock(sb, AuLock_FLUSH);
27541+ if (au_opt_test(au_mntflags(sb), PLINK)) {
27542+ spin_lock(&sbinfo->si_plink_maint_lock);
27543+ if (!sbinfo->si_plink_maint_pid)
27544+ sbinfo->si_plink_maint_pid = current->pid;
27545+ else
27546+ err = -EBUSY;
27547+ spin_unlock(&sbinfo->si_plink_maint_lock);
27548+ }
4a4d8108
AM
27549+ si_write_unlock(sb);
27550+
27551+ return err;
1facf9fc 27552+}
27553+
27554+/* ---------------------------------------------------------------------- */
27555+
1facf9fc 27556+#ifdef CONFIG_AUFS_DEBUG
27557+void au_plink_list(struct super_block *sb)
27558+{
86dc4139 27559+ int i;
1facf9fc 27560+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27561+ struct hlist_bl_head *hbl;
27562+ struct hlist_bl_node *pos;
5afbbe0d 27563+ struct au_icntnr *icntnr;
1facf9fc 27564+
dece6358
AM
27565+ SiMustAnyLock(sb);
27566+
1facf9fc 27567+ sbinfo = au_sbi(sb);
27568+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 27569+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 27570+
86dc4139 27571+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
27572+ hbl = sbinfo->si_plink + i;
27573+ hlist_bl_lock(hbl);
27574+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
5afbbe0d 27575+ AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
8b6a4947 27576+ hlist_bl_unlock(hbl);
86dc4139 27577+ }
1facf9fc 27578+}
27579+#endif
27580+
27581+/* is the inode pseudo-linked? */
27582+int au_plink_test(struct inode *inode)
27583+{
86dc4139 27584+ int found, i;
1facf9fc 27585+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27586+ struct hlist_bl_head *hbl;
27587+ struct hlist_bl_node *pos;
5afbbe0d 27588+ struct au_icntnr *icntnr;
1facf9fc 27589+
27590+ sbinfo = au_sbi(inode->i_sb);
dece6358 27591+ AuRwMustAnyLock(&sbinfo->si_rwsem);
1facf9fc 27592+ AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
e49829fe 27593+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
1facf9fc 27594+
27595+ found = 0;
86dc4139 27596+ i = au_plink_hash(inode->i_ino);
8b6a4947
AM
27597+ hbl = sbinfo->si_plink + i;
27598+ hlist_bl_lock(hbl);
27599+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
5afbbe0d 27600+ if (&icntnr->vfs_inode == inode) {
1facf9fc 27601+ found = 1;
27602+ break;
27603+ }
8b6a4947 27604+ hlist_bl_unlock(hbl);
1facf9fc 27605+ return found;
27606+}
27607+
27608+/* ---------------------------------------------------------------------- */
27609+
27610+/*
27611+ * generate a name for plink.
27612+ * the file will be stored under AUFS_WH_PLINKDIR.
27613+ */
27614+/* 20 is max digits length of ulong 64 */
27615+#define PLINK_NAME_LEN ((20 + 1) * 2)
27616+
27617+static int plink_name(char *name, int len, struct inode *inode,
27618+ aufs_bindex_t bindex)
27619+{
27620+ int rlen;
27621+ struct inode *h_inode;
27622+
27623+ h_inode = au_h_iptr(inode, bindex);
27624+ rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
27625+ return rlen;
27626+}
27627+
7f207e10
AM
27628+struct au_do_plink_lkup_args {
27629+ struct dentry **errp;
27630+ struct qstr *tgtname;
27631+ struct dentry *h_parent;
27632+ struct au_branch *br;
27633+};
27634+
27635+static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
27636+ struct dentry *h_parent,
27637+ struct au_branch *br)
27638+{
27639+ struct dentry *h_dentry;
febd17d6 27640+ struct inode *h_inode;
7f207e10 27641+
febd17d6 27642+ h_inode = d_inode(h_parent);
3c1bdaff 27643+ vfsub_inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
b4510431 27644+ h_dentry = vfsub_lkup_one(tgtname, h_parent);
3c1bdaff 27645+ inode_unlock_shared(h_inode);
7f207e10
AM
27646+ return h_dentry;
27647+}
27648+
27649+static void au_call_do_plink_lkup(void *args)
27650+{
27651+ struct au_do_plink_lkup_args *a = args;
27652+ *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
27653+}
27654+
1facf9fc 27655+/* lookup the plink-ed @inode under the branch at @bindex */
27656+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
27657+{
27658+ struct dentry *h_dentry, *h_parent;
27659+ struct au_branch *br;
7f207e10 27660+ int wkq_err;
1facf9fc 27661+ char a[PLINK_NAME_LEN];
0c3ec466 27662+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 27663+
e49829fe
JR
27664+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27665+
1facf9fc 27666+ br = au_sbr(inode->i_sb, bindex);
27667+ h_parent = br->br_wbr->wbr_plink;
1facf9fc 27668+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
27669+
2dfbb274 27670+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
7f207e10
AM
27671+ struct au_do_plink_lkup_args args = {
27672+ .errp = &h_dentry,
27673+ .tgtname = &tgtname,
27674+ .h_parent = h_parent,
27675+ .br = br
27676+ };
27677+
27678+ wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
27679+ if (unlikely(wkq_err))
27680+ h_dentry = ERR_PTR(wkq_err);
27681+ } else
27682+ h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
27683+
1facf9fc 27684+ return h_dentry;
27685+}
27686+
27687+/* create a pseudo-link */
27688+static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
27689+ struct dentry *h_dentry, struct au_branch *br)
27690+{
27691+ int err;
27692+ struct path h_path = {
86dc4139 27693+ .mnt = au_br_mnt(br)
1facf9fc 27694+ };
523b37e3 27695+ struct inode *h_dir, *delegated;
1facf9fc 27696+
5527c038 27697+ h_dir = d_inode(h_parent);
febd17d6 27698+ inode_lock_nested(h_dir, AuLsc_I_CHILD2);
4f0767ce 27699+again:
b4510431 27700+ h_path.dentry = vfsub_lkup_one(tgt, h_parent);
1facf9fc 27701+ err = PTR_ERR(h_path.dentry);
27702+ if (IS_ERR(h_path.dentry))
27703+ goto out;
27704+
27705+ err = 0;
27706+ /* wh.plink dir is not monitored */
7f207e10 27707+ /* todo: is it really safe? */
5527c038
JR
27708+ if (d_is_positive(h_path.dentry)
27709+ && d_inode(h_path.dentry) != d_inode(h_dentry)) {
523b37e3
AM
27710+ delegated = NULL;
27711+ err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
27712+ if (unlikely(err == -EWOULDBLOCK)) {
27713+ pr_warn("cannot retry for NFSv4 delegation"
27714+ " for an internal unlink\n");
27715+ iput(delegated);
27716+ }
1facf9fc 27717+ dput(h_path.dentry);
27718+ h_path.dentry = NULL;
27719+ if (!err)
27720+ goto again;
27721+ }
5527c038 27722+ if (!err && d_is_negative(h_path.dentry)) {
523b37e3
AM
27723+ delegated = NULL;
27724+ err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
27725+ if (unlikely(err == -EWOULDBLOCK)) {
27726+ pr_warn("cannot retry for NFSv4 delegation"
27727+ " for an internal link\n");
27728+ iput(delegated);
27729+ }
27730+ }
1facf9fc 27731+ dput(h_path.dentry);
27732+
4f0767ce 27733+out:
febd17d6 27734+ inode_unlock(h_dir);
1facf9fc 27735+ return err;
27736+}
27737+
27738+struct do_whplink_args {
27739+ int *errp;
27740+ struct qstr *tgt;
27741+ struct dentry *h_parent;
27742+ struct dentry *h_dentry;
27743+ struct au_branch *br;
27744+};
27745+
27746+static void call_do_whplink(void *args)
27747+{
27748+ struct do_whplink_args *a = args;
27749+ *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
27750+}
27751+
27752+static int whplink(struct dentry *h_dentry, struct inode *inode,
27753+ aufs_bindex_t bindex, struct au_branch *br)
27754+{
27755+ int err, wkq_err;
27756+ struct au_wbr *wbr;
27757+ struct dentry *h_parent;
1facf9fc 27758+ char a[PLINK_NAME_LEN];
0c3ec466 27759+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 27760+
27761+ wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
27762+ h_parent = wbr->wbr_plink;
1facf9fc 27763+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
27764+
27765+ /* always superio. */
2dfbb274 27766+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
1facf9fc 27767+ struct do_whplink_args args = {
27768+ .errp = &err,
27769+ .tgt = &tgtname,
27770+ .h_parent = h_parent,
27771+ .h_dentry = h_dentry,
27772+ .br = br
27773+ };
27774+ wkq_err = au_wkq_wait(call_do_whplink, &args);
27775+ if (unlikely(wkq_err))
27776+ err = wkq_err;
27777+ } else
27778+ err = do_whplink(&tgtname, h_parent, h_dentry, br);
1facf9fc 27779+
27780+ return err;
27781+}
27782+
1facf9fc 27783+/*
27784+ * create a new pseudo-link for @h_dentry on @bindex.
27785+ * the linked inode is held in aufs @inode.
27786+ */
27787+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
27788+ struct dentry *h_dentry)
27789+{
27790+ struct super_block *sb;
27791+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27792+ struct hlist_bl_head *hbl;
27793+ struct hlist_bl_node *pos;
5afbbe0d 27794+ struct au_icntnr *icntnr;
86dc4139 27795+ int found, err, cnt, i;
1facf9fc 27796+
27797+ sb = inode->i_sb;
27798+ sbinfo = au_sbi(sb);
27799+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 27800+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 27801+
86dc4139 27802+ found = au_plink_test(inode);
4a4d8108 27803+ if (found)
1facf9fc 27804+ return;
4a4d8108 27805+
86dc4139 27806+ i = au_plink_hash(inode->i_ino);
8b6a4947 27807+ hbl = sbinfo->si_plink + i;
5afbbe0d 27808+ au_igrab(inode);
1facf9fc 27809+
8b6a4947
AM
27810+ hlist_bl_lock(hbl);
27811+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
5afbbe0d 27812+ if (&icntnr->vfs_inode == inode) {
4a4d8108
AM
27813+ found = 1;
27814+ break;
27815+ }
1facf9fc 27816+ }
5afbbe0d
AM
27817+ if (!found) {
27818+ icntnr = container_of(inode, struct au_icntnr, vfs_inode);
8b6a4947 27819+ hlist_bl_add_head(&icntnr->plink, hbl);
5afbbe0d 27820+ }
8b6a4947 27821+ hlist_bl_unlock(hbl);
4a4d8108 27822+ if (!found) {
8b6a4947 27823+ cnt = au_hbl_count(hbl);
86dc4139
AM
27824+#define msg "unexpectedly unblanced or too many pseudo-links"
27825+ if (cnt > AUFS_PLINK_WARN)
27826+ AuWarn1(msg ", %d\n", cnt);
27827+#undef msg
1facf9fc 27828+ err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
5afbbe0d
AM
27829+ if (unlikely(err)) {
27830+ pr_warn("err %d, damaged pseudo link.\n", err);
8b6a4947 27831+ au_hbl_del(&icntnr->plink, hbl);
5afbbe0d 27832+ iput(&icntnr->vfs_inode);
4a4d8108 27833+ }
5afbbe0d
AM
27834+ } else
27835+ iput(&icntnr->vfs_inode);
1facf9fc 27836+}
27837+
27838+/* free all plinks */
e49829fe 27839+void au_plink_put(struct super_block *sb, int verbose)
1facf9fc 27840+{
86dc4139 27841+ int i, warned;
1facf9fc 27842+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27843+ struct hlist_bl_head *hbl;
27844+ struct hlist_bl_node *pos, *tmp;
5afbbe0d 27845+ struct au_icntnr *icntnr;
1facf9fc 27846+
dece6358
AM
27847+ SiMustWriteLock(sb);
27848+
1facf9fc 27849+ sbinfo = au_sbi(sb);
27850+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 27851+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 27852+
1facf9fc 27853+ /* no spin_lock since sbinfo is write-locked */
86dc4139
AM
27854+ warned = 0;
27855+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
27856+ hbl = sbinfo->si_plink + i;
27857+ if (!warned && verbose && !hlist_bl_empty(hbl)) {
86dc4139
AM
27858+ pr_warn("pseudo-link is not flushed");
27859+ warned = 1;
27860+ }
8b6a4947 27861+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
5afbbe0d 27862+ iput(&icntnr->vfs_inode);
8b6a4947 27863+ INIT_HLIST_BL_HEAD(hbl);
86dc4139 27864+ }
1facf9fc 27865+}
27866+
e49829fe
JR
27867+void au_plink_clean(struct super_block *sb, int verbose)
27868+{
27869+ struct dentry *root;
27870+
27871+ root = sb->s_root;
27872+ aufs_write_lock(root);
27873+ if (au_opt_test(au_mntflags(sb), PLINK))
27874+ au_plink_put(sb, verbose);
27875+ aufs_write_unlock(root);
27876+}
27877+
86dc4139
AM
27878+static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
27879+{
27880+ int do_put;
5afbbe0d 27881+ aufs_bindex_t btop, bbot, bindex;
86dc4139
AM
27882+
27883+ do_put = 0;
5afbbe0d
AM
27884+ btop = au_ibtop(inode);
27885+ bbot = au_ibbot(inode);
27886+ if (btop >= 0) {
27887+ for (bindex = btop; bindex <= bbot; bindex++) {
86dc4139
AM
27888+ if (!au_h_iptr(inode, bindex)
27889+ || au_ii_br_id(inode, bindex) != br_id)
27890+ continue;
27891+ au_set_h_iptr(inode, bindex, NULL, 0);
27892+ do_put = 1;
27893+ break;
27894+ }
27895+ if (do_put)
5afbbe0d 27896+ for (bindex = btop; bindex <= bbot; bindex++)
86dc4139
AM
27897+ if (au_h_iptr(inode, bindex)) {
27898+ do_put = 0;
27899+ break;
27900+ }
27901+ } else
27902+ do_put = 1;
27903+
27904+ return do_put;
27905+}
27906+
1facf9fc 27907+/* free the plinks on a branch specified by @br_id */
27908+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
27909+{
27910+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27911+ struct hlist_bl_head *hbl;
27912+ struct hlist_bl_node *pos, *tmp;
5afbbe0d 27913+ struct au_icntnr *icntnr;
1facf9fc 27914+ struct inode *inode;
86dc4139 27915+ int i, do_put;
1facf9fc 27916+
dece6358
AM
27917+ SiMustWriteLock(sb);
27918+
1facf9fc 27919+ sbinfo = au_sbi(sb);
27920+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 27921+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 27922+
8b6a4947 27923+ /* no bit_lock since sbinfo is write-locked */
86dc4139 27924+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
27925+ hbl = sbinfo->si_plink + i;
27926+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
5afbbe0d 27927+ inode = au_igrab(&icntnr->vfs_inode);
86dc4139
AM
27928+ ii_write_lock_child(inode);
27929+ do_put = au_plink_do_half_refresh(inode, br_id);
5afbbe0d 27930+ if (do_put) {
8b6a4947 27931+ hlist_bl_del(&icntnr->plink);
5afbbe0d
AM
27932+ iput(inode);
27933+ }
86dc4139
AM
27934+ ii_write_unlock(inode);
27935+ iput(inode);
dece6358 27936+ }
dece6358
AM
27937+ }
27938+}
7f207e10
AM
27939diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
27940--- /usr/share/empty/fs/aufs/poll.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947 27941+++ linux/fs/aufs/poll.c 2017-11-12 22:24:42.274176929 +0100
b912730e 27942@@ -0,0 +1,52 @@
dece6358 27943+/*
a2654f78 27944+ * Copyright (C) 2005-2017 Junjiro R. Okajima
dece6358
AM
27945+ *
27946+ * This program, aufs is free software; you can redistribute it and/or modify
27947+ * it under the terms of the GNU General Public License as published by
27948+ * the Free Software Foundation; either version 2 of the License, or
27949+ * (at your option) any later version.
27950+ *
27951+ * This program is distributed in the hope that it will be useful,
27952+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27953+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27954+ * GNU General Public License for more details.
27955+ *
27956+ * You should have received a copy of the GNU General Public License
523b37e3 27957+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358
AM
27958+ */
27959+
1308ab2a 27960+/*
27961+ * poll operation
27962+ * There is only one filesystem which implements ->poll operation, currently.
27963+ */
27964+
27965+#include "aufs.h"
27966+
27967+unsigned int aufs_poll(struct file *file, poll_table *wait)
27968+{
27969+ unsigned int mask;
27970+ int err;
27971+ struct file *h_file;
1308ab2a 27972+ struct super_block *sb;
27973+
27974+ /* We should pretend an error happened. */
27975+ mask = POLLERR /* | POLLIN | POLLOUT */;
b912730e 27976+ sb = file->f_path.dentry->d_sb;
e49829fe 27977+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 27978+
521ced18 27979+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
27980+ err = PTR_ERR(h_file);
27981+ if (IS_ERR(h_file))
1308ab2a 27982+ goto out;
27983+
27984+ /* it is not an error if h_file has no operation */
27985+ mask = DEFAULT_POLLMASK;
523b37e3 27986+ if (h_file->f_op->poll)
1308ab2a 27987+ mask = h_file->f_op->poll(h_file, wait);
b912730e 27988+ fput(h_file); /* instead of au_read_post() */
1308ab2a 27989+
4f0767ce 27990+out:
1308ab2a 27991+ si_read_unlock(sb);
27992+ AuTraceErr((int)mask);
27993+ return mask;
27994+}
c1595e42
JR
27995diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
27996--- /usr/share/empty/fs/aufs/posix_acl.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947 27997+++ linux/fs/aufs/posix_acl.c 2017-11-12 22:24:42.274176929 +0100
a2654f78 27998@@ -0,0 +1,102 @@
c1595e42 27999+/*
a2654f78 28000+ * Copyright (C) 2014-2017 Junjiro R. Okajima
c1595e42
JR
28001+ *
28002+ * This program, aufs is free software; you can redistribute it and/or modify
28003+ * it under the terms of the GNU General Public License as published by
28004+ * the Free Software Foundation; either version 2 of the License, or
28005+ * (at your option) any later version.
28006+ *
28007+ * This program is distributed in the hope that it will be useful,
28008+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28009+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28010+ * GNU General Public License for more details.
28011+ *
28012+ * You should have received a copy of the GNU General Public License
28013+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
28014+ */
28015+
28016+/*
28017+ * posix acl operations
28018+ */
28019+
28020+#include <linux/fs.h>
c1595e42
JR
28021+#include "aufs.h"
28022+
28023+struct posix_acl *aufs_get_acl(struct inode *inode, int type)
28024+{
28025+ struct posix_acl *acl;
28026+ int err;
28027+ aufs_bindex_t bindex;
28028+ struct inode *h_inode;
28029+ struct super_block *sb;
28030+
28031+ acl = NULL;
28032+ sb = inode->i_sb;
28033+ si_read_lock(sb, AuLock_FLUSH);
28034+ ii_read_lock_child(inode);
28035+ if (!(sb->s_flags & MS_POSIXACL))
28036+ goto out;
28037+
5afbbe0d 28038+ bindex = au_ibtop(inode);
c1595e42
JR
28039+ h_inode = au_h_iptr(inode, bindex);
28040+ if (unlikely(!h_inode
28041+ || ((h_inode->i_mode & S_IFMT)
28042+ != (inode->i_mode & S_IFMT)))) {
28043+ err = au_busy_or_stale();
28044+ acl = ERR_PTR(err);
28045+ goto out;
28046+ }
28047+
28048+ /* always topmost only */
28049+ acl = get_acl(h_inode, type);
a2654f78
AM
28050+ if (!IS_ERR_OR_NULL(acl))
28051+ set_cached_acl(inode, type, acl);
c1595e42
JR
28052+
28053+out:
28054+ ii_read_unlock(inode);
28055+ si_read_unlock(sb);
28056+
28057+ AuTraceErrPtr(acl);
28058+ return acl;
28059+}
28060+
28061+int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
28062+{
28063+ int err;
28064+ ssize_t ssz;
28065+ struct dentry *dentry;
f2c43d5f 28066+ struct au_sxattr arg = {
c1595e42
JR
28067+ .type = AU_ACL_SET,
28068+ .u.acl_set = {
28069+ .acl = acl,
28070+ .type = type
28071+ },
28072+ };
28073+
5afbbe0d
AM
28074+ IMustLock(inode);
28075+
c1595e42
JR
28076+ if (inode->i_ino == AUFS_ROOT_INO)
28077+ dentry = dget(inode->i_sb->s_root);
28078+ else {
28079+ dentry = d_find_alias(inode);
28080+ if (!dentry)
28081+ dentry = d_find_any_alias(inode);
28082+ if (!dentry) {
28083+ pr_warn("cannot handle this inode, "
28084+ "please report to aufs-users ML\n");
28085+ err = -ENOENT;
28086+ goto out;
28087+ }
28088+ }
28089+
f2c43d5f 28090+ ssz = au_sxattr(dentry, inode, &arg);
c1595e42
JR
28091+ dput(dentry);
28092+ err = ssz;
a2654f78 28093+ if (ssz >= 0) {
c1595e42 28094+ err = 0;
a2654f78
AM
28095+ set_cached_acl(inode, type, acl);
28096+ }
c1595e42
JR
28097+
28098+out:
c1595e42
JR
28099+ return err;
28100+}
7f207e10
AM
28101diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28102--- /usr/share/empty/fs/aufs/procfs.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
28103+++ linux/fs/aufs/procfs.c 2017-11-12 22:24:44.707577830 +0100
28104@@ -0,0 +1,170 @@
e49829fe 28105+/*
a2654f78 28106+ * Copyright (C) 2010-2017 Junjiro R. Okajima
e49829fe
JR
28107+ *
28108+ * This program, aufs is free software; you can redistribute it and/or modify
28109+ * it under the terms of the GNU General Public License as published by
28110+ * the Free Software Foundation; either version 2 of the License, or
28111+ * (at your option) any later version.
28112+ *
28113+ * This program is distributed in the hope that it will be useful,
28114+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28115+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28116+ * GNU General Public License for more details.
28117+ *
28118+ * You should have received a copy of the GNU General Public License
523b37e3 28119+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
28120+ */
28121+
28122+/*
28123+ * procfs interfaces
28124+ */
28125+
28126+#include <linux/proc_fs.h>
28127+#include "aufs.h"
28128+
28129+static int au_procfs_plm_release(struct inode *inode, struct file *file)
28130+{
28131+ struct au_sbinfo *sbinfo;
28132+
28133+ sbinfo = file->private_data;
28134+ if (sbinfo) {
28135+ au_plink_maint_leave(sbinfo);
28136+ kobject_put(&sbinfo->si_kobj);
28137+ }
28138+
28139+ return 0;
28140+}
28141+
28142+static void au_procfs_plm_write_clean(struct file *file)
28143+{
28144+ struct au_sbinfo *sbinfo;
28145+
28146+ sbinfo = file->private_data;
28147+ if (sbinfo)
28148+ au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28149+}
28150+
28151+static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28152+{
28153+ int err;
28154+ struct super_block *sb;
28155+ struct au_sbinfo *sbinfo;
8b6a4947 28156+ struct hlist_bl_node *pos;
e49829fe
JR
28157+
28158+ err = -EBUSY;
28159+ if (unlikely(file->private_data))
28160+ goto out;
28161+
28162+ sb = NULL;
53392da6 28163+ /* don't use au_sbilist_lock() here */
8b6a4947
AM
28164+ hlist_bl_lock(&au_sbilist);
28165+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
e49829fe
JR
28166+ if (id == sysaufs_si_id(sbinfo)) {
28167+ kobject_get(&sbinfo->si_kobj);
28168+ sb = sbinfo->si_sb;
28169+ break;
28170+ }
8b6a4947 28171+ hlist_bl_unlock(&au_sbilist);
e49829fe
JR
28172+
28173+ err = -EINVAL;
28174+ if (unlikely(!sb))
28175+ goto out;
28176+
28177+ err = au_plink_maint_enter(sb);
28178+ if (!err)
28179+ /* keep kobject_get() */
28180+ file->private_data = sbinfo;
28181+ else
28182+ kobject_put(&sbinfo->si_kobj);
28183+out:
28184+ return err;
28185+}
28186+
28187+/*
28188+ * Accept a valid "si=xxxx" only.
28189+ * Once it is accepted successfully, accept "clean" too.
28190+ */
28191+static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28192+ size_t count, loff_t *ppos)
28193+{
28194+ ssize_t err;
28195+ unsigned long id;
28196+ /* last newline is allowed */
28197+ char buf[3 + sizeof(unsigned long) * 2 + 1];
28198+
28199+ err = -EACCES;
28200+ if (unlikely(!capable(CAP_SYS_ADMIN)))
28201+ goto out;
28202+
28203+ err = -EINVAL;
28204+ if (unlikely(count > sizeof(buf)))
28205+ goto out;
28206+
28207+ err = copy_from_user(buf, ubuf, count);
28208+ if (unlikely(err)) {
28209+ err = -EFAULT;
28210+ goto out;
28211+ }
28212+ buf[count] = 0;
28213+
28214+ err = -EINVAL;
28215+ if (!strcmp("clean", buf)) {
28216+ au_procfs_plm_write_clean(file);
28217+ goto out_success;
28218+ } else if (unlikely(strncmp("si=", buf, 3)))
28219+ goto out;
28220+
9dbd164d 28221+ err = kstrtoul(buf + 3, 16, &id);
e49829fe
JR
28222+ if (unlikely(err))
28223+ goto out;
28224+
28225+ err = au_procfs_plm_write_si(file, id);
28226+ if (unlikely(err))
28227+ goto out;
28228+
28229+out_success:
28230+ err = count; /* success */
28231+out:
28232+ return err;
28233+}
28234+
28235+static const struct file_operations au_procfs_plm_fop = {
28236+ .write = au_procfs_plm_write,
28237+ .release = au_procfs_plm_release,
28238+ .owner = THIS_MODULE
28239+};
28240+
28241+/* ---------------------------------------------------------------------- */
28242+
28243+static struct proc_dir_entry *au_procfs_dir;
28244+
28245+void au_procfs_fin(void)
28246+{
28247+ remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28248+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28249+}
28250+
28251+int __init au_procfs_init(void)
28252+{
28253+ int err;
28254+ struct proc_dir_entry *entry;
28255+
28256+ err = -ENOMEM;
28257+ au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28258+ if (unlikely(!au_procfs_dir))
28259+ goto out;
28260+
28261+ entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | S_IWUSR,
28262+ au_procfs_dir, &au_procfs_plm_fop);
28263+ if (unlikely(!entry))
28264+ goto out_dir;
28265+
28266+ err = 0;
28267+ goto out; /* success */
28268+
28269+
28270+out_dir:
28271+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28272+out:
28273+ return err;
28274+}
7f207e10
AM
28275diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
28276--- /usr/share/empty/fs/aufs/rdu.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947 28277+++ linux/fs/aufs/rdu.c 2017-11-12 22:24:42.274176929 +0100
5afbbe0d 28278@@ -0,0 +1,381 @@
1308ab2a 28279+/*
a2654f78 28280+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1308ab2a 28281+ *
28282+ * This program, aufs is free software; you can redistribute it and/or modify
28283+ * it under the terms of the GNU General Public License as published by
28284+ * the Free Software Foundation; either version 2 of the License, or
28285+ * (at your option) any later version.
28286+ *
28287+ * This program is distributed in the hope that it will be useful,
28288+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28289+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28290+ * GNU General Public License for more details.
28291+ *
28292+ * You should have received a copy of the GNU General Public License
523b37e3 28293+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1308ab2a 28294+ */
28295+
28296+/*
28297+ * readdir in userspace.
28298+ */
28299+
b752ccd1 28300+#include <linux/compat.h>
4a4d8108 28301+#include <linux/fs_stack.h>
1308ab2a 28302+#include <linux/security.h>
1308ab2a 28303+#include "aufs.h"
28304+
28305+/* bits for struct aufs_rdu.flags */
28306+#define AuRdu_CALLED 1
28307+#define AuRdu_CONT (1 << 1)
28308+#define AuRdu_FULL (1 << 2)
28309+#define au_ftest_rdu(flags, name) ((flags) & AuRdu_##name)
7f207e10
AM
28310+#define au_fset_rdu(flags, name) \
28311+ do { (flags) |= AuRdu_##name; } while (0)
28312+#define au_fclr_rdu(flags, name) \
28313+ do { (flags) &= ~AuRdu_##name; } while (0)
1308ab2a 28314+
28315+struct au_rdu_arg {
392086de 28316+ struct dir_context ctx;
1308ab2a 28317+ struct aufs_rdu *rdu;
28318+ union au_rdu_ent_ul ent;
28319+ unsigned long end;
28320+
28321+ struct super_block *sb;
28322+ int err;
28323+};
28324+
392086de 28325+static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
1308ab2a 28326+ loff_t offset, u64 h_ino, unsigned int d_type)
28327+{
28328+ int err, len;
392086de 28329+ struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
1308ab2a 28330+ struct aufs_rdu *rdu = arg->rdu;
28331+ struct au_rdu_ent ent;
28332+
28333+ err = 0;
28334+ arg->err = 0;
28335+ au_fset_rdu(rdu->cookie.flags, CALLED);
28336+ len = au_rdu_len(nlen);
28337+ if (arg->ent.ul + len < arg->end) {
28338+ ent.ino = h_ino;
28339+ ent.bindex = rdu->cookie.bindex;
28340+ ent.type = d_type;
28341+ ent.nlen = nlen;
4a4d8108
AM
28342+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
28343+ ent.type = DT_UNKNOWN;
1308ab2a 28344+
9dbd164d 28345+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 28346+ err = -EFAULT;
28347+ if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
28348+ goto out;
28349+ if (copy_to_user(arg->ent.e->name, name, nlen))
28350+ goto out;
28351+ /* the terminating NULL */
28352+ if (__put_user(0, arg->ent.e->name + nlen))
28353+ goto out;
28354+ err = 0;
28355+ /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
28356+ arg->ent.ul += len;
28357+ rdu->rent++;
28358+ } else {
28359+ err = -EFAULT;
28360+ au_fset_rdu(rdu->cookie.flags, FULL);
28361+ rdu->full = 1;
28362+ rdu->tail = arg->ent;
28363+ }
28364+
4f0767ce 28365+out:
1308ab2a 28366+ /* AuTraceErr(err); */
28367+ return err;
28368+}
28369+
28370+static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
28371+{
28372+ int err;
28373+ loff_t offset;
28374+ struct au_rdu_cookie *cookie = &arg->rdu->cookie;
28375+
92d182d2 28376+ /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
1308ab2a 28377+ offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
28378+ err = offset;
28379+ if (unlikely(offset != cookie->h_pos))
28380+ goto out;
28381+
28382+ err = 0;
28383+ do {
28384+ arg->err = 0;
28385+ au_fclr_rdu(cookie->flags, CALLED);
28386+ /* smp_mb(); */
392086de 28387+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1308ab2a 28388+ if (err >= 0)
28389+ err = arg->err;
28390+ } while (!err
28391+ && au_ftest_rdu(cookie->flags, CALLED)
28392+ && !au_ftest_rdu(cookie->flags, FULL));
28393+ cookie->h_pos = h_file->f_pos;
28394+
4f0767ce 28395+out:
1308ab2a 28396+ AuTraceErr(err);
28397+ return err;
28398+}
28399+
28400+static int au_rdu(struct file *file, struct aufs_rdu *rdu)
28401+{
28402+ int err;
5afbbe0d 28403+ aufs_bindex_t bbot;
392086de
AM
28404+ struct au_rdu_arg arg = {
28405+ .ctx = {
2000de60 28406+ .actor = au_rdu_fill
392086de
AM
28407+ }
28408+ };
1308ab2a 28409+ struct dentry *dentry;
28410+ struct inode *inode;
28411+ struct file *h_file;
28412+ struct au_rdu_cookie *cookie = &rdu->cookie;
28413+
28414+ err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
28415+ if (unlikely(err)) {
28416+ err = -EFAULT;
28417+ AuTraceErr(err);
28418+ goto out;
28419+ }
28420+ rdu->rent = 0;
28421+ rdu->tail = rdu->ent;
28422+ rdu->full = 0;
28423+ arg.rdu = rdu;
28424+ arg.ent = rdu->ent;
28425+ arg.end = arg.ent.ul;
28426+ arg.end += rdu->sz;
28427+
28428+ err = -ENOTDIR;
5afbbe0d 28429+ if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
1308ab2a 28430+ goto out;
28431+
28432+ err = security_file_permission(file, MAY_READ);
28433+ AuTraceErr(err);
28434+ if (unlikely(err))
28435+ goto out;
28436+
2000de60 28437+ dentry = file->f_path.dentry;
5527c038 28438+ inode = d_inode(dentry);
5afbbe0d 28439+ inode_lock_shared(inode);
1308ab2a 28440+
28441+ arg.sb = inode->i_sb;
e49829fe
JR
28442+ err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
28443+ if (unlikely(err))
28444+ goto out_mtx;
027c5e7a
AM
28445+ err = au_alive_dir(dentry);
28446+ if (unlikely(err))
28447+ goto out_si;
e49829fe 28448+ /* todo: reval? */
1308ab2a 28449+ fi_read_lock(file);
28450+
28451+ err = -EAGAIN;
28452+ if (unlikely(au_ftest_rdu(cookie->flags, CONT)
28453+ && cookie->generation != au_figen(file)))
28454+ goto out_unlock;
28455+
28456+ err = 0;
28457+ if (!rdu->blk) {
28458+ rdu->blk = au_sbi(arg.sb)->si_rdblk;
28459+ if (!rdu->blk)
28460+ rdu->blk = au_dir_size(file, /*dentry*/NULL);
28461+ }
5afbbe0d
AM
28462+ bbot = au_fbtop(file);
28463+ if (cookie->bindex < bbot)
28464+ cookie->bindex = bbot;
28465+ bbot = au_fbbot_dir(file);
28466+ /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
28467+ for (; !err && cookie->bindex <= bbot;
1308ab2a 28468+ cookie->bindex++, cookie->h_pos = 0) {
4a4d8108 28469+ h_file = au_hf_dir(file, cookie->bindex);
1308ab2a 28470+ if (!h_file)
28471+ continue;
28472+
28473+ au_fclr_rdu(cookie->flags, FULL);
28474+ err = au_rdu_do(h_file, &arg);
28475+ AuTraceErr(err);
28476+ if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
28477+ break;
28478+ }
28479+ AuDbg("rent %llu\n", rdu->rent);
28480+
28481+ if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
28482+ rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
28483+ au_fset_rdu(cookie->flags, CONT);
28484+ cookie->generation = au_figen(file);
28485+ }
28486+
28487+ ii_read_lock_child(inode);
5afbbe0d 28488+ fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
1308ab2a 28489+ ii_read_unlock(inode);
28490+
4f0767ce 28491+out_unlock:
1308ab2a 28492+ fi_read_unlock(file);
027c5e7a 28493+out_si:
1308ab2a 28494+ si_read_unlock(arg.sb);
4f0767ce 28495+out_mtx:
5afbbe0d 28496+ inode_unlock_shared(inode);
4f0767ce 28497+out:
1308ab2a 28498+ AuTraceErr(err);
28499+ return err;
28500+}
28501+
28502+static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
28503+{
28504+ int err;
28505+ ino_t ino;
28506+ unsigned long long nent;
28507+ union au_rdu_ent_ul *u;
28508+ struct au_rdu_ent ent;
28509+ struct super_block *sb;
28510+
28511+ err = 0;
28512+ nent = rdu->nent;
28513+ u = &rdu->ent;
2000de60 28514+ sb = file->f_path.dentry->d_sb;
1308ab2a 28515+ si_read_lock(sb, AuLock_FLUSH);
28516+ while (nent-- > 0) {
9dbd164d 28517+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 28518+ err = copy_from_user(&ent, u->e, sizeof(ent));
4a4d8108
AM
28519+ if (!err)
28520+ err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
1308ab2a 28521+ if (unlikely(err)) {
28522+ err = -EFAULT;
28523+ AuTraceErr(err);
28524+ break;
28525+ }
28526+
28527+ /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
28528+ if (!ent.wh)
28529+ err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
28530+ else
28531+ err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
28532+ &ino);
28533+ if (unlikely(err)) {
28534+ AuTraceErr(err);
28535+ break;
28536+ }
28537+
28538+ err = __put_user(ino, &u->e->ino);
28539+ if (unlikely(err)) {
28540+ err = -EFAULT;
28541+ AuTraceErr(err);
28542+ break;
28543+ }
28544+ u->ul += au_rdu_len(ent.nlen);
28545+ }
28546+ si_read_unlock(sb);
28547+
28548+ return err;
28549+}
28550+
28551+/* ---------------------------------------------------------------------- */
28552+
28553+static int au_rdu_verify(struct aufs_rdu *rdu)
28554+{
b752ccd1 28555+ AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
1308ab2a 28556+ "%llu, b%d, 0x%x, g%u}\n",
b752ccd1 28557+ rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
1308ab2a 28558+ rdu->blk,
28559+ rdu->rent, rdu->shwh, rdu->full,
28560+ rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
28561+ rdu->cookie.generation);
dece6358 28562+
b752ccd1 28563+ if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
1308ab2a 28564+ return 0;
dece6358 28565+
b752ccd1
AM
28566+ AuDbg("%u:%u\n",
28567+ rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
1308ab2a 28568+ return -EINVAL;
28569+}
28570+
28571+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
dece6358 28572+{
1308ab2a 28573+ long err, e;
28574+ struct aufs_rdu rdu;
28575+ void __user *p = (void __user *)arg;
dece6358 28576+
1308ab2a 28577+ err = copy_from_user(&rdu, p, sizeof(rdu));
28578+ if (unlikely(err)) {
28579+ err = -EFAULT;
28580+ AuTraceErr(err);
28581+ goto out;
28582+ }
28583+ err = au_rdu_verify(&rdu);
dece6358
AM
28584+ if (unlikely(err))
28585+ goto out;
28586+
1308ab2a 28587+ switch (cmd) {
28588+ case AUFS_CTL_RDU:
28589+ err = au_rdu(file, &rdu);
28590+ if (unlikely(err))
28591+ break;
dece6358 28592+
1308ab2a 28593+ e = copy_to_user(p, &rdu, sizeof(rdu));
28594+ if (unlikely(e)) {
28595+ err = -EFAULT;
28596+ AuTraceErr(err);
28597+ }
28598+ break;
28599+ case AUFS_CTL_RDU_INO:
28600+ err = au_rdu_ino(file, &rdu);
28601+ break;
28602+
28603+ default:
4a4d8108 28604+ /* err = -ENOTTY; */
1308ab2a 28605+ err = -EINVAL;
28606+ }
dece6358 28607+
4f0767ce 28608+out:
1308ab2a 28609+ AuTraceErr(err);
28610+ return err;
1facf9fc 28611+}
b752ccd1
AM
28612+
28613+#ifdef CONFIG_COMPAT
28614+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28615+{
28616+ long err, e;
28617+ struct aufs_rdu rdu;
28618+ void __user *p = compat_ptr(arg);
28619+
28620+ /* todo: get_user()? */
28621+ err = copy_from_user(&rdu, p, sizeof(rdu));
28622+ if (unlikely(err)) {
28623+ err = -EFAULT;
28624+ AuTraceErr(err);
28625+ goto out;
28626+ }
28627+ rdu.ent.e = compat_ptr(rdu.ent.ul);
28628+ err = au_rdu_verify(&rdu);
28629+ if (unlikely(err))
28630+ goto out;
28631+
28632+ switch (cmd) {
28633+ case AUFS_CTL_RDU:
28634+ err = au_rdu(file, &rdu);
28635+ if (unlikely(err))
28636+ break;
28637+
28638+ rdu.ent.ul = ptr_to_compat(rdu.ent.e);
28639+ rdu.tail.ul = ptr_to_compat(rdu.tail.e);
28640+ e = copy_to_user(p, &rdu, sizeof(rdu));
28641+ if (unlikely(e)) {
28642+ err = -EFAULT;
28643+ AuTraceErr(err);
28644+ }
28645+ break;
28646+ case AUFS_CTL_RDU_INO:
28647+ err = au_rdu_ino(file, &rdu);
28648+ break;
28649+
28650+ default:
28651+ /* err = -ENOTTY; */
28652+ err = -EINVAL;
28653+ }
28654+
4f0767ce 28655+out:
b752ccd1
AM
28656+ AuTraceErr(err);
28657+ return err;
28658+}
28659+#endif
7f207e10
AM
28660diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
28661--- /usr/share/empty/fs/aufs/rwsem.h 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
28662+++ linux/fs/aufs/rwsem.h 2017-11-12 22:24:44.707577830 +0100
28663@@ -0,0 +1,72 @@
1facf9fc 28664+/*
a2654f78 28665+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 28666+ *
28667+ * This program, aufs is free software; you can redistribute it and/or modify
28668+ * it under the terms of the GNU General Public License as published by
28669+ * the Free Software Foundation; either version 2 of the License, or
28670+ * (at your option) any later version.
dece6358
AM
28671+ *
28672+ * This program is distributed in the hope that it will be useful,
28673+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28674+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28675+ * GNU General Public License for more details.
28676+ *
28677+ * You should have received a copy of the GNU General Public License
523b37e3 28678+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 28679+ */
28680+
28681+/*
28682+ * simple read-write semaphore wrappers
28683+ */
28684+
28685+#ifndef __AUFS_RWSEM_H__
28686+#define __AUFS_RWSEM_H__
28687+
28688+#ifdef __KERNEL__
28689+
4a4d8108 28690+#include "debug.h"
dece6358 28691+
8b6a4947
AM
28692+/* in the futre, the name 'au_rwsem' will be totally gone */
28693+#define au_rwsem rw_semaphore
dece6358
AM
28694+
28695+/* to debug easier, do not make them inlined functions */
8b6a4947 28696+#define AuRwMustNoWaiters(rw) AuDebugOn(rwsem_is_contended(rw))
dece6358 28697+/* rwsem_is_locked() is unusable */
8b6a4947
AM
28698+#define AuRwMustReadLock(rw) AuDebugOn(!lockdep_recursing(current) \
28699+ && debug_locks \
28700+ && !lockdep_is_held_type(rw, 1))
28701+#define AuRwMustWriteLock(rw) AuDebugOn(!lockdep_recursing(current) \
28702+ && debug_locks \
28703+ && !lockdep_is_held_type(rw, 0))
28704+#define AuRwMustAnyLock(rw) AuDebugOn(!lockdep_recursing(current) \
28705+ && debug_locks \
28706+ && !lockdep_is_held(rw))
28707+#define AuRwDestroy(rw) AuDebugOn(!lockdep_recursing(current) \
28708+ && debug_locks \
28709+ && lockdep_is_held(rw))
28710+
28711+#define au_rw_init(rw) init_rwsem(rw)
dece6358 28712+
5afbbe0d
AM
28713+#define au_rw_init_wlock(rw) do { \
28714+ au_rw_init(rw); \
8b6a4947 28715+ down_write(rw); \
5afbbe0d 28716+ } while (0)
dece6358 28717+
8b6a4947
AM
28718+#define au_rw_init_wlock_nested(rw, lsc) do { \
28719+ au_rw_init(rw); \
28720+ down_write_nested(rw, lsc); \
5afbbe0d 28721+ } while (0)
dece6358 28722+
8b6a4947
AM
28723+#define au_rw_read_lock(rw) down_read(rw)
28724+#define au_rw_read_lock_nested(rw, lsc) down_read_nested(rw, lsc)
28725+#define au_rw_read_unlock(rw) up_read(rw)
28726+#define au_rw_dgrade_lock(rw) downgrade_write(rw)
28727+#define au_rw_write_lock(rw) down_write(rw)
28728+#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
28729+#define au_rw_write_unlock(rw) up_write(rw)
28730+/* why is not _nested version defined? */
28731+#define au_rw_read_trylock(rw) down_read_trylock(rw)
28732+#define au_rw_write_trylock(rw) down_write_trylock(rw)
1facf9fc 28733+
28734+#endif /* __KERNEL__ */
28735+#endif /* __AUFS_RWSEM_H__ */
7f207e10
AM
28736diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
28737--- /usr/share/empty/fs/aufs/sbinfo.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947 28738+++ linux/fs/aufs/sbinfo.c 2017-11-12 22:24:44.707577830 +0100
a2654f78 28739@@ -0,0 +1,304 @@
1facf9fc 28740+/*
a2654f78 28741+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 28742+ *
28743+ * This program, aufs is free software; you can redistribute it and/or modify
28744+ * it under the terms of the GNU General Public License as published by
28745+ * the Free Software Foundation; either version 2 of the License, or
28746+ * (at your option) any later version.
dece6358
AM
28747+ *
28748+ * This program is distributed in the hope that it will be useful,
28749+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28750+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28751+ * GNU General Public License for more details.
28752+ *
28753+ * You should have received a copy of the GNU General Public License
523b37e3 28754+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 28755+ */
28756+
28757+/*
28758+ * superblock private data
28759+ */
28760+
28761+#include "aufs.h"
28762+
28763+/*
28764+ * they are necessary regardless sysfs is disabled.
28765+ */
28766+void au_si_free(struct kobject *kobj)
28767+{
86dc4139 28768+ int i;
1facf9fc 28769+ struct au_sbinfo *sbinfo;
b752ccd1 28770+ char *locked __maybe_unused; /* debug only */
1facf9fc 28771+
28772+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
86dc4139 28773+ for (i = 0; i < AuPlink_NHASH; i++)
8b6a4947 28774+ AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
f0c0a007 28775+ AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
5afbbe0d
AM
28776+
28777+ AuDebugOn(percpu_counter_sum(&sbinfo->si_ninodes));
28778+ percpu_counter_destroy(&sbinfo->si_ninodes);
28779+ AuDebugOn(percpu_counter_sum(&sbinfo->si_nfiles));
28780+ percpu_counter_destroy(&sbinfo->si_nfiles);
1facf9fc 28781+
e49829fe 28782+ au_rw_write_lock(&sbinfo->si_rwsem);
1facf9fc 28783+ au_br_free(sbinfo);
e49829fe 28784+ au_rw_write_unlock(&sbinfo->si_rwsem);
b752ccd1 28785+
1c60b727 28786+ kfree(sbinfo->si_branch);
1facf9fc 28787+ mutex_destroy(&sbinfo->si_xib_mtx);
dece6358 28788+ AuRwDestroy(&sbinfo->si_rwsem);
1facf9fc 28789+
1c60b727 28790+ kfree(sbinfo);
1facf9fc 28791+}
28792+
28793+int au_si_alloc(struct super_block *sb)
28794+{
86dc4139 28795+ int err, i;
1facf9fc 28796+ struct au_sbinfo *sbinfo;
28797+
28798+ err = -ENOMEM;
4a4d8108 28799+ sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
1facf9fc 28800+ if (unlikely(!sbinfo))
28801+ goto out;
28802+
28803+ /* will be reallocated separately */
28804+ sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
28805+ if (unlikely(!sbinfo->si_branch))
febd17d6 28806+ goto out_sbinfo;
1facf9fc 28807+
1facf9fc 28808+ err = sysaufs_si_init(sbinfo);
28809+ if (unlikely(err))
28810+ goto out_br;
28811+
28812+ au_nwt_init(&sbinfo->si_nowait);
dece6358 28813+ au_rw_init_wlock(&sbinfo->si_rwsem);
b752ccd1 28814+
5afbbe0d
AM
28815+ percpu_counter_init(&sbinfo->si_ninodes, 0, GFP_NOFS);
28816+ percpu_counter_init(&sbinfo->si_nfiles, 0, GFP_NOFS);
7f207e10 28817+
5afbbe0d 28818+ sbinfo->si_bbot = -1;
392086de 28819+ sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
1facf9fc 28820+
28821+ sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
28822+ sbinfo->si_wbr_create = AuWbrCreate_Def;
4a4d8108
AM
28823+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
28824+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
1facf9fc 28825+
076b876e
AM
28826+ au_fhsm_init(sbinfo);
28827+
e49829fe 28828+ sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
1facf9fc 28829+
392086de
AM
28830+ sbinfo->si_xino_jiffy = jiffies;
28831+ sbinfo->si_xino_expire
28832+ = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
1facf9fc 28833+ mutex_init(&sbinfo->si_xib_mtx);
1facf9fc 28834+ sbinfo->si_xino_brid = -1;
28835+ /* leave si_xib_last_pindex and si_xib_next_bit */
28836+
8b6a4947 28837+ INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
b912730e 28838+
e49829fe 28839+ sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
1facf9fc 28840+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
28841+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
28842+ sbinfo->si_dirwh = AUFS_DIRWH_DEF;
28843+
86dc4139 28844+ for (i = 0; i < AuPlink_NHASH; i++)
8b6a4947 28845+ INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
1facf9fc 28846+ init_waitqueue_head(&sbinfo->si_plink_wq);
4a4d8108 28847+ spin_lock_init(&sbinfo->si_plink_maint_lock);
1facf9fc 28848+
8b6a4947 28849+ INIT_HLIST_BL_HEAD(&sbinfo->si_files);
523b37e3 28850+
b95c5147
AM
28851+ /* with getattr by default */
28852+ sbinfo->si_iop_array = aufs_iop;
28853+
1facf9fc 28854+ /* leave other members for sysaufs and si_mnt. */
28855+ sbinfo->si_sb = sb;
28856+ sb->s_fs_info = sbinfo;
b752ccd1 28857+ si_pid_set(sb);
1facf9fc 28858+ return 0; /* success */
28859+
4f0767ce 28860+out_br:
1c60b727 28861+ kfree(sbinfo->si_branch);
4f0767ce 28862+out_sbinfo:
1c60b727 28863+ kfree(sbinfo);
4f0767ce 28864+out:
1facf9fc 28865+ return err;
28866+}
28867+
e2f27e51 28868+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
1facf9fc 28869+{
28870+ int err, sz;
28871+ struct au_branch **brp;
28872+
dece6358
AM
28873+ AuRwMustWriteLock(&sbinfo->si_rwsem);
28874+
1facf9fc 28875+ err = -ENOMEM;
5afbbe0d 28876+ sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
1facf9fc 28877+ if (unlikely(!sz))
28878+ sz = sizeof(*brp);
e2f27e51
AM
28879+ brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
28880+ may_shrink);
1facf9fc 28881+ if (brp) {
28882+ sbinfo->si_branch = brp;
28883+ err = 0;
28884+ }
28885+
28886+ return err;
28887+}
28888+
28889+/* ---------------------------------------------------------------------- */
28890+
28891+unsigned int au_sigen_inc(struct super_block *sb)
28892+{
28893+ unsigned int gen;
5527c038 28894+ struct inode *inode;
1facf9fc 28895+
dece6358
AM
28896+ SiMustWriteLock(sb);
28897+
1facf9fc 28898+ gen = ++au_sbi(sb)->si_generation;
28899+ au_update_digen(sb->s_root);
5527c038
JR
28900+ inode = d_inode(sb->s_root);
28901+ au_update_iigen(inode, /*half*/0);
28902+ inode->i_version++;
1facf9fc 28903+ return gen;
28904+}
28905+
28906+aufs_bindex_t au_new_br_id(struct super_block *sb)
28907+{
28908+ aufs_bindex_t br_id;
28909+ int i;
28910+ struct au_sbinfo *sbinfo;
28911+
dece6358
AM
28912+ SiMustWriteLock(sb);
28913+
1facf9fc 28914+ sbinfo = au_sbi(sb);
28915+ for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
28916+ br_id = ++sbinfo->si_last_br_id;
7f207e10 28917+ AuDebugOn(br_id < 0);
1facf9fc 28918+ if (br_id && au_br_index(sb, br_id) < 0)
28919+ return br_id;
28920+ }
28921+
28922+ return -1;
28923+}
28924+
28925+/* ---------------------------------------------------------------------- */
28926+
e49829fe
JR
28927+/* it is ok that new 'nwt' tasks are appended while we are sleeping */
28928+int si_read_lock(struct super_block *sb, int flags)
28929+{
28930+ int err;
28931+
28932+ err = 0;
28933+ if (au_ftest_lock(flags, FLUSH))
28934+ au_nwt_flush(&au_sbi(sb)->si_nowait);
28935+
28936+ si_noflush_read_lock(sb);
28937+ err = au_plink_maint(sb, flags);
28938+ if (unlikely(err))
28939+ si_read_unlock(sb);
28940+
28941+ return err;
28942+}
28943+
28944+int si_write_lock(struct super_block *sb, int flags)
28945+{
28946+ int err;
28947+
28948+ if (au_ftest_lock(flags, FLUSH))
28949+ au_nwt_flush(&au_sbi(sb)->si_nowait);
28950+
28951+ si_noflush_write_lock(sb);
28952+ err = au_plink_maint(sb, flags);
28953+ if (unlikely(err))
28954+ si_write_unlock(sb);
28955+
28956+ return err;
28957+}
28958+
1facf9fc 28959+/* dentry and super_block lock. call at entry point */
e49829fe 28960+int aufs_read_lock(struct dentry *dentry, int flags)
1facf9fc 28961+{
e49829fe 28962+ int err;
027c5e7a 28963+ struct super_block *sb;
e49829fe 28964+
027c5e7a
AM
28965+ sb = dentry->d_sb;
28966+ err = si_read_lock(sb, flags);
28967+ if (unlikely(err))
28968+ goto out;
28969+
28970+ if (au_ftest_lock(flags, DW))
28971+ di_write_lock_child(dentry);
28972+ else
28973+ di_read_lock_child(dentry, flags);
28974+
28975+ if (au_ftest_lock(flags, GEN)) {
28976+ err = au_digen_test(dentry, au_sigen(sb));
79b8bda9
AM
28977+ if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
28978+ AuDebugOn(!err && au_dbrange_test(dentry));
28979+ else if (!err)
28980+ err = au_dbrange_test(dentry);
027c5e7a
AM
28981+ if (unlikely(err))
28982+ aufs_read_unlock(dentry, flags);
e49829fe
JR
28983+ }
28984+
027c5e7a 28985+out:
e49829fe 28986+ return err;
1facf9fc 28987+}
28988+
28989+void aufs_read_unlock(struct dentry *dentry, int flags)
28990+{
28991+ if (au_ftest_lock(flags, DW))
28992+ di_write_unlock(dentry);
28993+ else
28994+ di_read_unlock(dentry, flags);
28995+ si_read_unlock(dentry->d_sb);
28996+}
28997+
28998+void aufs_write_lock(struct dentry *dentry)
28999+{
e49829fe 29000+ si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
1facf9fc 29001+ di_write_lock_child(dentry);
29002+}
29003+
29004+void aufs_write_unlock(struct dentry *dentry)
29005+{
29006+ di_write_unlock(dentry);
29007+ si_write_unlock(dentry->d_sb);
29008+}
29009+
e49829fe 29010+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
1facf9fc 29011+{
e49829fe 29012+ int err;
027c5e7a
AM
29013+ unsigned int sigen;
29014+ struct super_block *sb;
e49829fe 29015+
027c5e7a
AM
29016+ sb = d1->d_sb;
29017+ err = si_read_lock(sb, flags);
29018+ if (unlikely(err))
29019+ goto out;
29020+
b95c5147 29021+ di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
027c5e7a
AM
29022+
29023+ if (au_ftest_lock(flags, GEN)) {
29024+ sigen = au_sigen(sb);
29025+ err = au_digen_test(d1, sigen);
29026+ AuDebugOn(!err && au_dbrange_test(d1));
29027+ if (!err) {
29028+ err = au_digen_test(d2, sigen);
29029+ AuDebugOn(!err && au_dbrange_test(d2));
29030+ }
29031+ if (unlikely(err))
29032+ aufs_read_and_write_unlock2(d1, d2);
29033+ }
29034+
29035+out:
e49829fe 29036+ return err;
1facf9fc 29037+}
29038+
29039+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29040+{
29041+ di_write_unlock2(d1, d2);
29042+ si_read_unlock(d1->d_sb);
29043+}
7f207e10
AM
29044diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29045--- /usr/share/empty/fs/aufs/super.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
29046+++ linux/fs/aufs/super.c 2017-11-12 22:24:44.707577830 +0100
29047@@ -0,0 +1,1046 @@
1facf9fc 29048+/*
a2654f78 29049+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 29050+ *
29051+ * This program, aufs is free software; you can redistribute it and/or modify
29052+ * it under the terms of the GNU General Public License as published by
29053+ * the Free Software Foundation; either version 2 of the License, or
29054+ * (at your option) any later version.
dece6358
AM
29055+ *
29056+ * This program is distributed in the hope that it will be useful,
29057+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29058+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29059+ * GNU General Public License for more details.
29060+ *
29061+ * You should have received a copy of the GNU General Public License
523b37e3 29062+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29063+ */
29064+
29065+/*
29066+ * mount and super_block operations
29067+ */
29068+
f6c5ef8b 29069+#include <linux/mm.h>
1facf9fc 29070+#include <linux/seq_file.h>
29071+#include <linux/statfs.h>
7f207e10 29072+#include <linux/vmalloc.h>
1facf9fc 29073+#include "aufs.h"
29074+
29075+/*
29076+ * super_operations
29077+ */
29078+static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29079+{
29080+ struct au_icntnr *c;
29081+
29082+ c = au_cache_alloc_icntnr();
29083+ if (c) {
027c5e7a 29084+ au_icntnr_init(c);
1facf9fc 29085+ c->vfs_inode.i_version = 1; /* sigen(sb); */
29086+ c->iinfo.ii_hinode = NULL;
29087+ return &c->vfs_inode;
29088+ }
29089+ return NULL;
29090+}
29091+
027c5e7a
AM
29092+static void aufs_destroy_inode_cb(struct rcu_head *head)
29093+{
29094+ struct inode *inode = container_of(head, struct inode, i_rcu);
29095+
1c60b727 29096+ au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
027c5e7a
AM
29097+}
29098+
1facf9fc 29099+static void aufs_destroy_inode(struct inode *inode)
29100+{
5afbbe0d
AM
29101+ if (!au_is_bad_inode(inode))
29102+ au_iinfo_fin(inode);
027c5e7a 29103+ call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
1facf9fc 29104+}
29105+
29106+struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29107+{
29108+ struct inode *inode;
29109+ int err;
29110+
29111+ inode = iget_locked(sb, ino);
29112+ if (unlikely(!inode)) {
29113+ inode = ERR_PTR(-ENOMEM);
29114+ goto out;
29115+ }
29116+ if (!(inode->i_state & I_NEW))
29117+ goto out;
29118+
29119+ err = au_xigen_new(inode);
29120+ if (!err)
29121+ err = au_iinfo_init(inode);
29122+ if (!err)
29123+ inode->i_version++;
29124+ else {
29125+ iget_failed(inode);
29126+ inode = ERR_PTR(err);
29127+ }
29128+
4f0767ce 29129+out:
1facf9fc 29130+ /* never return NULL */
29131+ AuDebugOn(!inode);
29132+ AuTraceErrPtr(inode);
29133+ return inode;
29134+}
29135+
29136+/* lock free root dinfo */
29137+static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29138+{
29139+ int err;
5afbbe0d 29140+ aufs_bindex_t bindex, bbot;
1facf9fc 29141+ struct path path;
4a4d8108 29142+ struct au_hdentry *hdp;
1facf9fc 29143+ struct au_branch *br;
076b876e 29144+ au_br_perm_str_t perm;
1facf9fc 29145+
29146+ err = 0;
5afbbe0d
AM
29147+ bbot = au_sbbot(sb);
29148+ bindex = 0;
29149+ hdp = au_hdentry(au_di(sb->s_root), bindex);
29150+ for (; !err && bindex <= bbot; bindex++, hdp++) {
1facf9fc 29151+ br = au_sbr(sb, bindex);
86dc4139 29152+ path.mnt = au_br_mnt(br);
5afbbe0d 29153+ path.dentry = hdp->hd_dentry;
1facf9fc 29154+ err = au_seq_path(seq, &path);
79b8bda9 29155+ if (!err) {
076b876e 29156+ au_optstr_br_perm(&perm, br->br_perm);
79b8bda9 29157+ seq_printf(seq, "=%s", perm.a);
5afbbe0d 29158+ if (bindex != bbot)
79b8bda9 29159+ seq_putc(seq, ':');
1e00d052 29160+ }
1facf9fc 29161+ }
79b8bda9
AM
29162+ if (unlikely(err || seq_has_overflowed(seq)))
29163+ err = -E2BIG;
1facf9fc 29164+
29165+ return err;
29166+}
29167+
f2c43d5f
AM
29168+static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29169+ const char *append)
29170+{
29171+ char *p;
29172+
29173+ p = fmt;
29174+ while (*pat != ':')
29175+ *p++ = *pat++;
29176+ *p++ = *pat++;
29177+ strcpy(p, append);
29178+ AuDebugOn(strlen(fmt) >= len);
29179+}
29180+
1facf9fc 29181+static void au_show_wbr_create(struct seq_file *m, int v,
29182+ struct au_sbinfo *sbinfo)
29183+{
29184+ const char *pat;
f2c43d5f
AM
29185+ char fmt[32];
29186+ struct au_wbr_mfs *mfs;
1facf9fc 29187+
dece6358
AM
29188+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29189+
c2b27bf2 29190+ seq_puts(m, ",create=");
1facf9fc 29191+ pat = au_optstr_wbr_create(v);
f2c43d5f 29192+ mfs = &sbinfo->si_wbr_mfs;
1facf9fc 29193+ switch (v) {
29194+ case AuWbrCreate_TDP:
29195+ case AuWbrCreate_RR:
29196+ case AuWbrCreate_MFS:
29197+ case AuWbrCreate_PMFS:
c2b27bf2 29198+ seq_puts(m, pat);
1facf9fc 29199+ break;
f2c43d5f
AM
29200+ case AuWbrCreate_MFSRR:
29201+ case AuWbrCreate_TDMFS:
29202+ case AuWbrCreate_PMFSRR:
29203+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29204+ seq_printf(m, fmt, mfs->mfsrr_watermark);
1facf9fc 29205+ break;
f2c43d5f 29206+ case AuWbrCreate_MFSV:
1facf9fc 29207+ case AuWbrCreate_PMFSV:
f2c43d5f
AM
29208+ au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29209+ seq_printf(m, fmt,
29210+ jiffies_to_msecs(mfs->mfs_expire)
e49829fe 29211+ / MSEC_PER_SEC);
1facf9fc 29212+ break;
1facf9fc 29213+ case AuWbrCreate_MFSRRV:
f2c43d5f 29214+ case AuWbrCreate_TDMFSV:
392086de 29215+ case AuWbrCreate_PMFSRRV:
f2c43d5f
AM
29216+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29217+ seq_printf(m, fmt, mfs->mfsrr_watermark,
29218+ jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
392086de 29219+ break;
f2c43d5f
AM
29220+ default:
29221+ BUG();
1facf9fc 29222+ }
29223+}
29224+
7eafdf33 29225+static int au_show_xino(struct seq_file *seq, struct super_block *sb)
1facf9fc 29226+{
29227+#ifdef CONFIG_SYSFS
29228+ return 0;
29229+#else
29230+ int err;
29231+ const int len = sizeof(AUFS_XINO_FNAME) - 1;
29232+ aufs_bindex_t bindex, brid;
1facf9fc 29233+ struct qstr *name;
29234+ struct file *f;
29235+ struct dentry *d, *h_root;
29236+
dece6358
AM
29237+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29238+
1facf9fc 29239+ err = 0;
1facf9fc 29240+ f = au_sbi(sb)->si_xib;
29241+ if (!f)
29242+ goto out;
29243+
29244+ /* stop printing the default xino path on the first writable branch */
29245+ h_root = NULL;
29246+ brid = au_xino_brid(sb);
29247+ if (brid >= 0) {
29248+ bindex = au_br_index(sb, brid);
5afbbe0d 29249+ h_root = au_hdentry(au_di(sb->s_root), bindex)->hd_dentry;
1facf9fc 29250+ }
2000de60 29251+ d = f->f_path.dentry;
1facf9fc 29252+ name = &d->d_name;
29253+ /* safe ->d_parent because the file is unlinked */
29254+ if (d->d_parent == h_root
29255+ && name->len == len
29256+ && !memcmp(name->name, AUFS_XINO_FNAME, len))
29257+ goto out;
29258+
29259+ seq_puts(seq, ",xino=");
29260+ err = au_xino_path(seq, f);
29261+
4f0767ce 29262+out:
1facf9fc 29263+ return err;
29264+#endif
29265+}
29266+
29267+/* seq_file will re-call me in case of too long string */
7eafdf33 29268+static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
1facf9fc 29269+{
027c5e7a 29270+ int err;
1facf9fc 29271+ unsigned int mnt_flags, v;
29272+ struct super_block *sb;
29273+ struct au_sbinfo *sbinfo;
29274+
29275+#define AuBool(name, str) do { \
29276+ v = au_opt_test(mnt_flags, name); \
29277+ if (v != au_opt_test(AuOpt_Def, name)) \
29278+ seq_printf(m, ",%s" #str, v ? "" : "no"); \
29279+} while (0)
29280+
29281+#define AuStr(name, str) do { \
29282+ v = mnt_flags & AuOptMask_##name; \
29283+ if (v != (AuOpt_Def & AuOptMask_##name)) \
29284+ seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
29285+} while (0)
29286+
29287+#define AuUInt(name, str, val) do { \
29288+ if (val != AUFS_##name##_DEF) \
29289+ seq_printf(m, "," #str "=%u", val); \
29290+} while (0)
29291+
7eafdf33 29292+ sb = dentry->d_sb;
c1595e42
JR
29293+ if (sb->s_flags & MS_POSIXACL)
29294+ seq_puts(m, ",acl");
29295+
29296+ /* lock free root dinfo */
1facf9fc 29297+ si_noflush_read_lock(sb);
29298+ sbinfo = au_sbi(sb);
29299+ seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
29300+
29301+ mnt_flags = au_mntflags(sb);
29302+ if (au_opt_test(mnt_flags, XINO)) {
7eafdf33 29303+ err = au_show_xino(m, sb);
1facf9fc 29304+ if (unlikely(err))
29305+ goto out;
29306+ } else
29307+ seq_puts(m, ",noxino");
29308+
29309+ AuBool(TRUNC_XINO, trunc_xino);
29310+ AuStr(UDBA, udba);
dece6358 29311+ AuBool(SHWH, shwh);
1facf9fc 29312+ AuBool(PLINK, plink);
4a4d8108 29313+ AuBool(DIO, dio);
076b876e 29314+ AuBool(DIRPERM1, dirperm1);
1facf9fc 29315+
29316+ v = sbinfo->si_wbr_create;
29317+ if (v != AuWbrCreate_Def)
29318+ au_show_wbr_create(m, v, sbinfo);
29319+
29320+ v = sbinfo->si_wbr_copyup;
29321+ if (v != AuWbrCopyup_Def)
29322+ seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
29323+
29324+ v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
29325+ if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
29326+ seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
29327+
29328+ AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
29329+
027c5e7a
AM
29330+ v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
29331+ AuUInt(RDCACHE, rdcache, v);
1facf9fc 29332+
29333+ AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
29334+ AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
29335+
076b876e
AM
29336+ au_fhsm_show(m, sbinfo);
29337+
8b6a4947 29338+ AuBool(DIRREN, dirren);
1facf9fc 29339+ AuBool(SUM, sum);
29340+ /* AuBool(SUM_W, wsum); */
29341+ AuBool(WARN_PERM, warn_perm);
29342+ AuBool(VERBOSE, verbose);
29343+
4f0767ce 29344+out:
1facf9fc 29345+ /* be sure to print "br:" last */
29346+ if (!sysaufs_brs) {
29347+ seq_puts(m, ",br:");
29348+ au_show_brs(m, sb);
29349+ }
29350+ si_read_unlock(sb);
29351+ return 0;
29352+
1facf9fc 29353+#undef AuBool
29354+#undef AuStr
4a4d8108 29355+#undef AuUInt
1facf9fc 29356+}
29357+
29358+/* ---------------------------------------------------------------------- */
29359+
29360+/* sum mode which returns the summation for statfs(2) */
29361+
29362+static u64 au_add_till_max(u64 a, u64 b)
29363+{
29364+ u64 old;
29365+
29366+ old = a;
29367+ a += b;
92d182d2
AM
29368+ if (old <= a)
29369+ return a;
29370+ return ULLONG_MAX;
29371+}
29372+
29373+static u64 au_mul_till_max(u64 a, long mul)
29374+{
29375+ u64 old;
29376+
29377+ old = a;
29378+ a *= mul;
29379+ if (old <= a)
1facf9fc 29380+ return a;
29381+ return ULLONG_MAX;
29382+}
29383+
29384+static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
29385+{
29386+ int err;
92d182d2 29387+ long bsize, factor;
1facf9fc 29388+ u64 blocks, bfree, bavail, files, ffree;
5afbbe0d 29389+ aufs_bindex_t bbot, bindex, i;
1facf9fc 29390+ unsigned char shared;
7f207e10 29391+ struct path h_path;
1facf9fc 29392+ struct super_block *h_sb;
29393+
92d182d2
AM
29394+ err = 0;
29395+ bsize = LONG_MAX;
29396+ files = 0;
29397+ ffree = 0;
1facf9fc 29398+ blocks = 0;
29399+ bfree = 0;
29400+ bavail = 0;
5afbbe0d
AM
29401+ bbot = au_sbbot(sb);
29402+ for (bindex = 0; bindex <= bbot; bindex++) {
7f207e10
AM
29403+ h_path.mnt = au_sbr_mnt(sb, bindex);
29404+ h_sb = h_path.mnt->mnt_sb;
1facf9fc 29405+ shared = 0;
92d182d2 29406+ for (i = 0; !shared && i < bindex; i++)
1facf9fc 29407+ shared = (au_sbr_sb(sb, i) == h_sb);
29408+ if (shared)
29409+ continue;
29410+
29411+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
29412+ h_path.dentry = h_path.mnt->mnt_root;
29413+ err = vfs_statfs(&h_path, buf);
1facf9fc 29414+ if (unlikely(err))
29415+ goto out;
29416+
92d182d2
AM
29417+ if (bsize > buf->f_bsize) {
29418+ /*
29419+ * we will reduce bsize, so we have to expand blocks
29420+ * etc. to match them again
29421+ */
29422+ factor = (bsize / buf->f_bsize);
29423+ blocks = au_mul_till_max(blocks, factor);
29424+ bfree = au_mul_till_max(bfree, factor);
29425+ bavail = au_mul_till_max(bavail, factor);
29426+ bsize = buf->f_bsize;
29427+ }
29428+
29429+ factor = (buf->f_bsize / bsize);
29430+ blocks = au_add_till_max(blocks,
29431+ au_mul_till_max(buf->f_blocks, factor));
29432+ bfree = au_add_till_max(bfree,
29433+ au_mul_till_max(buf->f_bfree, factor));
29434+ bavail = au_add_till_max(bavail,
29435+ au_mul_till_max(buf->f_bavail, factor));
1facf9fc 29436+ files = au_add_till_max(files, buf->f_files);
29437+ ffree = au_add_till_max(ffree, buf->f_ffree);
29438+ }
29439+
92d182d2 29440+ buf->f_bsize = bsize;
1facf9fc 29441+ buf->f_blocks = blocks;
29442+ buf->f_bfree = bfree;
29443+ buf->f_bavail = bavail;
29444+ buf->f_files = files;
29445+ buf->f_ffree = ffree;
92d182d2 29446+ buf->f_frsize = 0;
1facf9fc 29447+
4f0767ce 29448+out:
1facf9fc 29449+ return err;
29450+}
29451+
29452+static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
29453+{
29454+ int err;
7f207e10 29455+ struct path h_path;
1facf9fc 29456+ struct super_block *sb;
29457+
29458+ /* lock free root dinfo */
29459+ sb = dentry->d_sb;
29460+ si_noflush_read_lock(sb);
7f207e10 29461+ if (!au_opt_test(au_mntflags(sb), SUM)) {
1facf9fc 29462+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
29463+ h_path.mnt = au_sbr_mnt(sb, 0);
29464+ h_path.dentry = h_path.mnt->mnt_root;
29465+ err = vfs_statfs(&h_path, buf);
29466+ } else
1facf9fc 29467+ err = au_statfs_sum(sb, buf);
29468+ si_read_unlock(sb);
29469+
29470+ if (!err) {
29471+ buf->f_type = AUFS_SUPER_MAGIC;
4a4d8108 29472+ buf->f_namelen = AUFS_MAX_NAMELEN;
1facf9fc 29473+ memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
29474+ }
29475+ /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
29476+
29477+ return err;
29478+}
29479+
29480+/* ---------------------------------------------------------------------- */
29481+
537831f9
AM
29482+static int aufs_sync_fs(struct super_block *sb, int wait)
29483+{
29484+ int err, e;
5afbbe0d 29485+ aufs_bindex_t bbot, bindex;
537831f9
AM
29486+ struct au_branch *br;
29487+ struct super_block *h_sb;
29488+
29489+ err = 0;
29490+ si_noflush_read_lock(sb);
5afbbe0d
AM
29491+ bbot = au_sbbot(sb);
29492+ for (bindex = 0; bindex <= bbot; bindex++) {
537831f9
AM
29493+ br = au_sbr(sb, bindex);
29494+ if (!au_br_writable(br->br_perm))
29495+ continue;
29496+
29497+ h_sb = au_sbr_sb(sb, bindex);
a2654f78
AM
29498+ e = vfsub_sync_filesystem(h_sb, wait);
29499+ if (unlikely(e && !err))
29500+ err = e;
29501+ /* go on even if an error happens */
537831f9
AM
29502+ }
29503+ si_read_unlock(sb);
29504+
29505+ return err;
29506+}
29507+
29508+/* ---------------------------------------------------------------------- */
29509+
1facf9fc 29510+/* final actions when unmounting a file system */
29511+static void aufs_put_super(struct super_block *sb)
29512+{
29513+ struct au_sbinfo *sbinfo;
29514+
29515+ sbinfo = au_sbi(sb);
29516+ if (!sbinfo)
29517+ return;
29518+
1facf9fc 29519+ dbgaufs_si_fin(sbinfo);
29520+ kobject_put(&sbinfo->si_kobj);
29521+}
29522+
29523+/* ---------------------------------------------------------------------- */
29524+
79b8bda9
AM
29525+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
29526+ struct super_block *sb, void *arg)
7f207e10
AM
29527+{
29528+ void *array;
076b876e 29529+ unsigned long long n, sz;
7f207e10
AM
29530+
29531+ array = NULL;
29532+ n = 0;
29533+ if (!*hint)
29534+ goto out;
29535+
29536+ if (*hint > ULLONG_MAX / sizeof(array)) {
29537+ array = ERR_PTR(-EMFILE);
29538+ pr_err("hint %llu\n", *hint);
29539+ goto out;
29540+ }
29541+
076b876e
AM
29542+ sz = sizeof(array) * *hint;
29543+ array = kzalloc(sz, GFP_NOFS);
7f207e10 29544+ if (unlikely(!array))
076b876e 29545+ array = vzalloc(sz);
7f207e10
AM
29546+ if (unlikely(!array)) {
29547+ array = ERR_PTR(-ENOMEM);
29548+ goto out;
29549+ }
29550+
79b8bda9 29551+ n = cb(sb, array, *hint, arg);
7f207e10
AM
29552+ AuDebugOn(n > *hint);
29553+
29554+out:
29555+ *hint = n;
29556+ return array;
29557+}
29558+
79b8bda9 29559+static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
7f207e10
AM
29560+ unsigned long long max __maybe_unused,
29561+ void *arg)
29562+{
29563+ unsigned long long n;
29564+ struct inode **p, *inode;
29565+ struct list_head *head;
29566+
29567+ n = 0;
29568+ p = a;
29569+ head = arg;
79b8bda9 29570+ spin_lock(&sb->s_inode_list_lock);
7f207e10 29571+ list_for_each_entry(inode, head, i_sb_list) {
5afbbe0d
AM
29572+ if (!au_is_bad_inode(inode)
29573+ && au_ii(inode)->ii_btop >= 0) {
2cbb1c4b
JR
29574+ spin_lock(&inode->i_lock);
29575+ if (atomic_read(&inode->i_count)) {
29576+ au_igrab(inode);
29577+ *p++ = inode;
29578+ n++;
29579+ AuDebugOn(n > max);
29580+ }
29581+ spin_unlock(&inode->i_lock);
7f207e10
AM
29582+ }
29583+ }
79b8bda9 29584+ spin_unlock(&sb->s_inode_list_lock);
7f207e10
AM
29585+
29586+ return n;
29587+}
29588+
29589+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
29590+{
5afbbe0d 29591+ *max = au_ninodes(sb);
79b8bda9 29592+ return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
7f207e10
AM
29593+}
29594+
29595+void au_iarray_free(struct inode **a, unsigned long long max)
29596+{
29597+ unsigned long long ull;
29598+
29599+ for (ull = 0; ull < max; ull++)
29600+ iput(a[ull]);
be52b249 29601+ kvfree(a);
7f207e10
AM
29602+}
29603+
29604+/* ---------------------------------------------------------------------- */
29605+
1facf9fc 29606+/*
29607+ * refresh dentry and inode at remount time.
29608+ */
027c5e7a
AM
29609+/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
29610+static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
29611+ struct dentry *parent)
1facf9fc 29612+{
29613+ int err;
1facf9fc 29614+
29615+ di_write_lock_child(dentry);
1facf9fc 29616+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
29617+ err = au_refresh_dentry(dentry, parent);
29618+ if (!err && dir_flags)
5527c038 29619+ au_hn_reset(d_inode(dentry), dir_flags);
1facf9fc 29620+ di_read_unlock(parent, AuLock_IR);
1facf9fc 29621+ di_write_unlock(dentry);
29622+
29623+ return err;
29624+}
29625+
027c5e7a
AM
29626+static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
29627+ struct au_sbinfo *sbinfo,
b95c5147 29628+ const unsigned int dir_flags, unsigned int do_idop)
1facf9fc 29629+{
027c5e7a
AM
29630+ int err;
29631+ struct dentry *parent;
027c5e7a
AM
29632+
29633+ err = 0;
29634+ parent = dget_parent(dentry);
29635+ if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
5527c038
JR
29636+ if (d_really_is_positive(dentry)) {
29637+ if (!d_is_dir(dentry))
027c5e7a
AM
29638+ err = au_do_refresh(dentry, /*dir_flags*/0,
29639+ parent);
29640+ else {
29641+ err = au_do_refresh(dentry, dir_flags, parent);
29642+ if (unlikely(err))
29643+ au_fset_si(sbinfo, FAILED_REFRESH_DIR);
29644+ }
29645+ } else
29646+ err = au_do_refresh(dentry, /*dir_flags*/0, parent);
29647+ AuDbgDentry(dentry);
29648+ }
29649+ dput(parent);
29650+
79b8bda9 29651+ if (!err) {
b95c5147 29652+ if (do_idop)
79b8bda9
AM
29653+ au_refresh_dop(dentry, /*force_reval*/0);
29654+ } else
29655+ au_refresh_dop(dentry, /*force_reval*/1);
29656+
027c5e7a
AM
29657+ AuTraceErr(err);
29658+ return err;
1facf9fc 29659+}
29660+
b95c5147 29661+static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
1facf9fc 29662+{
29663+ int err, i, j, ndentry, e;
027c5e7a 29664+ unsigned int sigen;
1facf9fc 29665+ struct au_dcsub_pages dpages;
29666+ struct au_dpage *dpage;
027c5e7a
AM
29667+ struct dentry **dentries, *d;
29668+ struct au_sbinfo *sbinfo;
29669+ struct dentry *root = sb->s_root;
5527c038 29670+ const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
1facf9fc 29671+
b95c5147 29672+ if (do_idop)
79b8bda9
AM
29673+ au_refresh_dop(root, /*force_reval*/0);
29674+
027c5e7a
AM
29675+ err = au_dpages_init(&dpages, GFP_NOFS);
29676+ if (unlikely(err))
1facf9fc 29677+ goto out;
027c5e7a
AM
29678+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
29679+ if (unlikely(err))
1facf9fc 29680+ goto out_dpages;
1facf9fc 29681+
027c5e7a
AM
29682+ sigen = au_sigen(sb);
29683+ sbinfo = au_sbi(sb);
29684+ for (i = 0; i < dpages.ndpage; i++) {
1facf9fc 29685+ dpage = dpages.dpages + i;
29686+ dentries = dpage->dentries;
29687+ ndentry = dpage->ndentry;
027c5e7a 29688+ for (j = 0; j < ndentry; j++) {
1facf9fc 29689+ d = dentries[j];
79b8bda9 29690+ e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
b95c5147 29691+ do_idop);
027c5e7a
AM
29692+ if (unlikely(e && !err))
29693+ err = e;
29694+ /* go on even err */
1facf9fc 29695+ }
29696+ }
29697+
4f0767ce 29698+out_dpages:
1facf9fc 29699+ au_dpages_free(&dpages);
4f0767ce 29700+out:
1facf9fc 29701+ return err;
29702+}
29703+
b95c5147 29704+static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
1facf9fc 29705+{
027c5e7a
AM
29706+ int err, e;
29707+ unsigned int sigen;
29708+ unsigned long long max, ull;
29709+ struct inode *inode, **array;
1facf9fc 29710+
027c5e7a
AM
29711+ array = au_iarray_alloc(sb, &max);
29712+ err = PTR_ERR(array);
29713+ if (IS_ERR(array))
29714+ goto out;
1facf9fc 29715+
29716+ err = 0;
027c5e7a
AM
29717+ sigen = au_sigen(sb);
29718+ for (ull = 0; ull < max; ull++) {
29719+ inode = array[ull];
076b876e
AM
29720+ if (unlikely(!inode))
29721+ break;
b95c5147
AM
29722+
29723+ e = 0;
29724+ ii_write_lock_child(inode);
537831f9 29725+ if (au_iigen(inode, NULL) != sigen) {
027c5e7a 29726+ e = au_refresh_hinode_self(inode);
1facf9fc 29727+ if (unlikely(e)) {
b95c5147 29728+ au_refresh_iop(inode, /*force_getattr*/1);
027c5e7a 29729+ pr_err("error %d, i%lu\n", e, inode->i_ino);
1facf9fc 29730+ if (!err)
29731+ err = e;
29732+ /* go on even if err */
29733+ }
29734+ }
b95c5147
AM
29735+ if (!e && do_idop)
29736+ au_refresh_iop(inode, /*force_getattr*/0);
29737+ ii_write_unlock(inode);
1facf9fc 29738+ }
29739+
027c5e7a 29740+ au_iarray_free(array, max);
1facf9fc 29741+
4f0767ce 29742+out:
1facf9fc 29743+ return err;
29744+}
29745+
b95c5147 29746+static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
1facf9fc 29747+{
027c5e7a
AM
29748+ int err, e;
29749+ unsigned int udba;
5afbbe0d 29750+ aufs_bindex_t bindex, bbot;
1facf9fc 29751+ struct dentry *root;
29752+ struct inode *inode;
027c5e7a 29753+ struct au_branch *br;
79b8bda9 29754+ struct au_sbinfo *sbi;
1facf9fc 29755+
29756+ au_sigen_inc(sb);
79b8bda9
AM
29757+ sbi = au_sbi(sb);
29758+ au_fclr_si(sbi, FAILED_REFRESH_DIR);
1facf9fc 29759+
29760+ root = sb->s_root;
29761+ DiMustNoWaiters(root);
5527c038 29762+ inode = d_inode(root);
1facf9fc 29763+ IiMustNoWaiters(inode);
1facf9fc 29764+
027c5e7a 29765+ udba = au_opt_udba(sb);
5afbbe0d
AM
29766+ bbot = au_sbbot(sb);
29767+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
29768+ br = au_sbr(sb, bindex);
29769+ err = au_hnotify_reset_br(udba, br, br->br_perm);
1facf9fc 29770+ if (unlikely(err))
027c5e7a
AM
29771+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
29772+ bindex, err);
29773+ /* go on even if err */
1facf9fc 29774+ }
027c5e7a 29775+ au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
1facf9fc 29776+
b95c5147 29777+ if (do_idop) {
79b8bda9
AM
29778+ if (au_ftest_si(sbi, NO_DREVAL)) {
29779+ AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
29780+ sb->s_d_op = &aufs_dop_noreval;
b95c5147
AM
29781+ AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
29782+ sbi->si_iop_array = aufs_iop_nogetattr;
79b8bda9
AM
29783+ } else {
29784+ AuDebugOn(sb->s_d_op == &aufs_dop);
29785+ sb->s_d_op = &aufs_dop;
b95c5147
AM
29786+ AuDebugOn(sbi->si_iop_array == aufs_iop);
29787+ sbi->si_iop_array = aufs_iop;
79b8bda9 29788+ }
b95c5147
AM
29789+ pr_info("reset to %pf and %pf\n",
29790+ sb->s_d_op, sbi->si_iop_array);
79b8bda9
AM
29791+ }
29792+
027c5e7a 29793+ di_write_unlock(root);
b95c5147
AM
29794+ err = au_refresh_d(sb, do_idop);
29795+ e = au_refresh_i(sb, do_idop);
027c5e7a
AM
29796+ if (unlikely(e && !err))
29797+ err = e;
1facf9fc 29798+ /* aufs_write_lock() calls ..._child() */
29799+ di_write_lock_child(root);
027c5e7a
AM
29800+
29801+ au_cpup_attr_all(inode, /*force*/1);
29802+
29803+ if (unlikely(err))
29804+ AuIOErr("refresh failed, ignored, %d\n", err);
1facf9fc 29805+}
29806+
29807+/* stop extra interpretation of errno in mount(8), and strange error messages */
29808+static int cvt_err(int err)
29809+{
29810+ AuTraceErr(err);
29811+
29812+ switch (err) {
29813+ case -ENOENT:
29814+ case -ENOTDIR:
29815+ case -EEXIST:
29816+ case -EIO:
29817+ err = -EINVAL;
29818+ }
29819+ return err;
29820+}
29821+
29822+static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
29823+{
4a4d8108
AM
29824+ int err, do_dx;
29825+ unsigned int mntflags;
be52b249
AM
29826+ struct au_opts opts = {
29827+ .opt = NULL
29828+ };
1facf9fc 29829+ struct dentry *root;
29830+ struct inode *inode;
29831+ struct au_sbinfo *sbinfo;
29832+
29833+ err = 0;
29834+ root = sb->s_root;
29835+ if (!data || !*data) {
e49829fe
JR
29836+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
29837+ if (!err) {
29838+ di_write_lock_child(root);
29839+ err = au_opts_verify(sb, *flags, /*pending*/0);
29840+ aufs_write_unlock(root);
29841+ }
1facf9fc 29842+ goto out;
29843+ }
29844+
29845+ err = -ENOMEM;
1facf9fc 29846+ opts.opt = (void *)__get_free_page(GFP_NOFS);
29847+ if (unlikely(!opts.opt))
29848+ goto out;
29849+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
29850+ opts.flags = AuOpts_REMOUNT;
29851+ opts.sb_flags = *flags;
29852+
29853+ /* parse it before aufs lock */
29854+ err = au_opts_parse(sb, data, &opts);
29855+ if (unlikely(err))
29856+ goto out_opts;
29857+
29858+ sbinfo = au_sbi(sb);
5527c038 29859+ inode = d_inode(root);
febd17d6 29860+ inode_lock(inode);
e49829fe
JR
29861+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
29862+ if (unlikely(err))
29863+ goto out_mtx;
29864+ di_write_lock_child(root);
1facf9fc 29865+
29866+ /* au_opts_remount() may return an error */
29867+ err = au_opts_remount(sb, &opts);
29868+ au_opts_free(&opts);
29869+
027c5e7a 29870+ if (au_ftest_opts(opts.flags, REFRESH))
b95c5147 29871+ au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
1facf9fc 29872+
4a4d8108
AM
29873+ if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
29874+ mntflags = au_mntflags(sb);
29875+ do_dx = !!au_opt_test(mntflags, DIO);
29876+ au_dy_arefresh(do_dx);
29877+ }
29878+
076b876e 29879+ au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
1facf9fc 29880+ aufs_write_unlock(root);
953406b4 29881+
e49829fe 29882+out_mtx:
febd17d6 29883+ inode_unlock(inode);
4f0767ce 29884+out_opts:
1c60b727 29885+ free_page((unsigned long)opts.opt);
4f0767ce 29886+out:
1facf9fc 29887+ err = cvt_err(err);
29888+ AuTraceErr(err);
29889+ return err;
29890+}
29891+
4a4d8108 29892+static const struct super_operations aufs_sop = {
1facf9fc 29893+ .alloc_inode = aufs_alloc_inode,
29894+ .destroy_inode = aufs_destroy_inode,
b752ccd1 29895+ /* always deleting, no clearing */
1facf9fc 29896+ .drop_inode = generic_delete_inode,
29897+ .show_options = aufs_show_options,
29898+ .statfs = aufs_statfs,
29899+ .put_super = aufs_put_super,
537831f9 29900+ .sync_fs = aufs_sync_fs,
1facf9fc 29901+ .remount_fs = aufs_remount_fs
29902+};
29903+
29904+/* ---------------------------------------------------------------------- */
29905+
29906+static int alloc_root(struct super_block *sb)
29907+{
29908+ int err;
29909+ struct inode *inode;
29910+ struct dentry *root;
29911+
29912+ err = -ENOMEM;
29913+ inode = au_iget_locked(sb, AUFS_ROOT_INO);
29914+ err = PTR_ERR(inode);
29915+ if (IS_ERR(inode))
29916+ goto out;
29917+
b95c5147 29918+ inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
1facf9fc 29919+ inode->i_fop = &aufs_dir_fop;
29920+ inode->i_mode = S_IFDIR;
9dbd164d 29921+ set_nlink(inode, 2);
1facf9fc 29922+ unlock_new_inode(inode);
29923+
92d182d2 29924+ root = d_make_root(inode);
1facf9fc 29925+ if (unlikely(!root))
92d182d2 29926+ goto out;
1facf9fc 29927+ err = PTR_ERR(root);
29928+ if (IS_ERR(root))
92d182d2 29929+ goto out;
1facf9fc 29930+
4a4d8108 29931+ err = au_di_init(root);
1facf9fc 29932+ if (!err) {
29933+ sb->s_root = root;
29934+ return 0; /* success */
29935+ }
29936+ dput(root);
1facf9fc 29937+
4f0767ce 29938+out:
1facf9fc 29939+ return err;
1facf9fc 29940+}
29941+
29942+static int aufs_fill_super(struct super_block *sb, void *raw_data,
29943+ int silent __maybe_unused)
29944+{
29945+ int err;
be52b249
AM
29946+ struct au_opts opts = {
29947+ .opt = NULL
29948+ };
79b8bda9 29949+ struct au_sbinfo *sbinfo;
1facf9fc 29950+ struct dentry *root;
29951+ struct inode *inode;
29952+ char *arg = raw_data;
29953+
29954+ if (unlikely(!arg || !*arg)) {
29955+ err = -EINVAL;
4a4d8108 29956+ pr_err("no arg\n");
1facf9fc 29957+ goto out;
29958+ }
29959+
29960+ err = -ENOMEM;
1facf9fc 29961+ opts.opt = (void *)__get_free_page(GFP_NOFS);
29962+ if (unlikely(!opts.opt))
29963+ goto out;
29964+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
29965+ opts.sb_flags = sb->s_flags;
29966+
29967+ err = au_si_alloc(sb);
29968+ if (unlikely(err))
29969+ goto out_opts;
79b8bda9 29970+ sbinfo = au_sbi(sb);
1facf9fc 29971+
29972+ /* all timestamps always follow the ones on the branch */
29973+ sb->s_flags |= MS_NOATIME | MS_NODIRATIME;
29974+ sb->s_op = &aufs_sop;
027c5e7a 29975+ sb->s_d_op = &aufs_dop;
1facf9fc 29976+ sb->s_magic = AUFS_SUPER_MAGIC;
29977+ sb->s_maxbytes = 0;
c1595e42 29978+ sb->s_stack_depth = 1;
1facf9fc 29979+ au_export_init(sb);
f2c43d5f 29980+ au_xattr_init(sb);
1facf9fc 29981+
29982+ err = alloc_root(sb);
29983+ if (unlikely(err)) {
29984+ si_write_unlock(sb);
29985+ goto out_info;
29986+ }
29987+ root = sb->s_root;
5527c038 29988+ inode = d_inode(root);
1facf9fc 29989+
29990+ /*
29991+ * actually we can parse options regardless aufs lock here.
29992+ * but at remount time, parsing must be done before aufs lock.
29993+ * so we follow the same rule.
29994+ */
29995+ ii_write_lock_parent(inode);
29996+ aufs_write_unlock(root);
29997+ err = au_opts_parse(sb, arg, &opts);
29998+ if (unlikely(err))
29999+ goto out_root;
30000+
30001+ /* lock vfs_inode first, then aufs. */
febd17d6 30002+ inode_lock(inode);
1facf9fc 30003+ aufs_write_lock(root);
30004+ err = au_opts_mount(sb, &opts);
30005+ au_opts_free(&opts);
79b8bda9
AM
30006+ if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
30007+ sb->s_d_op = &aufs_dop_noreval;
30008+ pr_info("%pf\n", sb->s_d_op);
30009+ au_refresh_dop(root, /*force_reval*/0);
b95c5147
AM
30010+ sbinfo->si_iop_array = aufs_iop_nogetattr;
30011+ au_refresh_iop(inode, /*force_getattr*/0);
79b8bda9 30012+ }
1facf9fc 30013+ aufs_write_unlock(root);
febd17d6 30014+ inode_unlock(inode);
4a4d8108
AM
30015+ if (!err)
30016+ goto out_opts; /* success */
1facf9fc 30017+
4f0767ce 30018+out_root:
1facf9fc 30019+ dput(root);
30020+ sb->s_root = NULL;
4f0767ce 30021+out_info:
79b8bda9
AM
30022+ dbgaufs_si_fin(sbinfo);
30023+ kobject_put(&sbinfo->si_kobj);
1facf9fc 30024+ sb->s_fs_info = NULL;
4f0767ce 30025+out_opts:
1c60b727 30026+ free_page((unsigned long)opts.opt);
4f0767ce 30027+out:
1facf9fc 30028+ AuTraceErr(err);
30029+ err = cvt_err(err);
30030+ AuTraceErr(err);
30031+ return err;
30032+}
30033+
30034+/* ---------------------------------------------------------------------- */
30035+
027c5e7a
AM
30036+static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
30037+ const char *dev_name __maybe_unused,
30038+ void *raw_data)
1facf9fc 30039+{
027c5e7a 30040+ struct dentry *root;
1facf9fc 30041+ struct super_block *sb;
30042+
30043+ /* all timestamps always follow the ones on the branch */
30044+ /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
027c5e7a
AM
30045+ root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
30046+ if (IS_ERR(root))
30047+ goto out;
30048+
30049+ sb = root->d_sb;
30050+ si_write_lock(sb, !AuLock_FLUSH);
30051+ sysaufs_brs_add(sb, 0);
30052+ si_write_unlock(sb);
30053+ au_sbilist_add(sb);
30054+
30055+out:
30056+ return root;
1facf9fc 30057+}
30058+
e49829fe
JR
30059+static void aufs_kill_sb(struct super_block *sb)
30060+{
30061+ struct au_sbinfo *sbinfo;
30062+
30063+ sbinfo = au_sbi(sb);
30064+ if (sbinfo) {
30065+ au_sbilist_del(sb);
30066+ aufs_write_lock(sb->s_root);
076b876e 30067+ au_fhsm_fin(sb);
e49829fe
JR
30068+ if (sbinfo->si_wbr_create_ops->fin)
30069+ sbinfo->si_wbr_create_ops->fin(sb);
30070+ if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30071+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
b95c5147 30072+ au_remount_refresh(sb, /*do_idop*/0);
e49829fe
JR
30073+ }
30074+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
30075+ au_plink_put(sb, /*verbose*/1);
30076+ au_xino_clr(sb);
8b6a4947 30077+ au_dr_opt_flush(sb);
1e00d052 30078+ sbinfo->si_sb = NULL;
e49829fe 30079+ aufs_write_unlock(sb->s_root);
e49829fe
JR
30080+ au_nwt_flush(&sbinfo->si_nowait);
30081+ }
98d9a5b1 30082+ kill_anon_super(sb);
e49829fe
JR
30083+}
30084+
1facf9fc 30085+struct file_system_type aufs_fs_type = {
30086+ .name = AUFS_FSTYPE,
c06a8ce3
AM
30087+ /* a race between rename and others */
30088+ .fs_flags = FS_RENAME_DOES_D_MOVE,
027c5e7a 30089+ .mount = aufs_mount,
e49829fe 30090+ .kill_sb = aufs_kill_sb,
1facf9fc 30091+ /* no need to __module_get() and module_put(). */
30092+ .owner = THIS_MODULE,
30093+};
7f207e10
AM
30094diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30095--- /usr/share/empty/fs/aufs/super.h 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
30096+++ linux/fs/aufs/super.h 2017-11-12 22:24:44.707577830 +0100
30097@@ -0,0 +1,626 @@
1facf9fc 30098+/*
a2654f78 30099+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 30100+ *
30101+ * This program, aufs is free software; you can redistribute it and/or modify
30102+ * it under the terms of the GNU General Public License as published by
30103+ * the Free Software Foundation; either version 2 of the License, or
30104+ * (at your option) any later version.
dece6358
AM
30105+ *
30106+ * This program is distributed in the hope that it will be useful,
30107+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30108+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30109+ * GNU General Public License for more details.
30110+ *
30111+ * You should have received a copy of the GNU General Public License
523b37e3 30112+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30113+ */
30114+
30115+/*
30116+ * super_block operations
30117+ */
30118+
30119+#ifndef __AUFS_SUPER_H__
30120+#define __AUFS_SUPER_H__
30121+
30122+#ifdef __KERNEL__
30123+
30124+#include <linux/fs.h>
5527c038 30125+#include <linux/kobject.h>
8b6a4947 30126+#include "hbl.h"
1facf9fc 30127+#include "rwsem.h"
1facf9fc 30128+#include "wkq.h"
30129+
1facf9fc 30130+/* policies to select one among multiple writable branches */
30131+struct au_wbr_copyup_operations {
30132+ int (*copyup)(struct dentry *dentry);
30133+};
30134+
392086de
AM
30135+#define AuWbr_DIR 1 /* target is a dir */
30136+#define AuWbr_PARENT (1 << 1) /* always require a parent */
30137+
30138+#define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
30139+#define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
30140+#define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
30141+
1facf9fc 30142+struct au_wbr_create_operations {
392086de 30143+ int (*create)(struct dentry *dentry, unsigned int flags);
1facf9fc 30144+ int (*init)(struct super_block *sb);
30145+ int (*fin)(struct super_block *sb);
30146+};
30147+
30148+struct au_wbr_mfs {
30149+ struct mutex mfs_lock; /* protect this structure */
30150+ unsigned long mfs_jiffy;
30151+ unsigned long mfs_expire;
30152+ aufs_bindex_t mfs_bindex;
30153+
30154+ unsigned long long mfsrr_bytes;
30155+ unsigned long long mfsrr_watermark;
30156+};
30157+
86dc4139
AM
30158+#define AuPlink_NHASH 100
30159+static inline int au_plink_hash(ino_t ino)
30160+{
30161+ return ino % AuPlink_NHASH;
30162+}
30163+
076b876e
AM
30164+/* File-based Hierarchical Storage Management */
30165+struct au_fhsm {
30166+#ifdef CONFIG_AUFS_FHSM
30167+ /* allow only one process who can receive the notification */
30168+ spinlock_t fhsm_spin;
30169+ pid_t fhsm_pid;
30170+ wait_queue_head_t fhsm_wqh;
30171+ atomic_t fhsm_readable;
30172+
c1595e42 30173+ /* these are protected by si_rwsem */
076b876e 30174+ unsigned long fhsm_expire;
c1595e42 30175+ aufs_bindex_t fhsm_bottom;
076b876e
AM
30176+#endif
30177+};
30178+
1facf9fc 30179+struct au_branch;
30180+struct au_sbinfo {
30181+ /* nowait tasks in the system-wide workqueue */
30182+ struct au_nowait_tasks si_nowait;
30183+
b752ccd1
AM
30184+ /*
30185+ * tried sb->s_umount, but failed due to the dependecy between i_mutex.
30186+ * rwsem for au_sbinfo is necessary.
30187+ */
dece6358 30188+ struct au_rwsem si_rwsem;
1facf9fc 30189+
7f207e10 30190+ /*
523b37e3
AM
30191+ * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30192+ * remount.
7f207e10 30193+ */
5afbbe0d 30194+ struct percpu_counter si_ninodes, si_nfiles;
7f207e10 30195+
1facf9fc 30196+ /* branch management */
30197+ unsigned int si_generation;
30198+
2000de60 30199+ /* see AuSi_ flags */
1facf9fc 30200+ unsigned char au_si_status;
30201+
5afbbe0d 30202+ aufs_bindex_t si_bbot;
7f207e10
AM
30203+
30204+ /* dirty trick to keep br_id plus */
30205+ unsigned int si_last_br_id :
30206+ sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
1facf9fc 30207+ struct au_branch **si_branch;
30208+
30209+ /* policy to select a writable branch */
30210+ unsigned char si_wbr_copyup;
30211+ unsigned char si_wbr_create;
30212+ struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30213+ struct au_wbr_create_operations *si_wbr_create_ops;
30214+
30215+ /* round robin */
30216+ atomic_t si_wbr_rr_next;
30217+
30218+ /* most free space */
30219+ struct au_wbr_mfs si_wbr_mfs;
30220+
076b876e
AM
30221+ /* File-based Hierarchical Storage Management */
30222+ struct au_fhsm si_fhsm;
30223+
1facf9fc 30224+ /* mount flags */
30225+ /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30226+ unsigned int si_mntflags;
30227+
30228+ /* external inode number (bitmap and translation table) */
5527c038
JR
30229+ vfs_readf_t si_xread;
30230+ vfs_writef_t si_xwrite;
1facf9fc 30231+ struct file *si_xib;
30232+ struct mutex si_xib_mtx; /* protect xib members */
30233+ unsigned long *si_xib_buf;
30234+ unsigned long si_xib_last_pindex;
30235+ int si_xib_next_bit;
30236+ aufs_bindex_t si_xino_brid;
392086de
AM
30237+ unsigned long si_xino_jiffy;
30238+ unsigned long si_xino_expire;
1facf9fc 30239+ /* reserved for future use */
30240+ /* unsigned long long si_xib_limit; */ /* Max xib file size */
30241+
30242+#ifdef CONFIG_AUFS_EXPORT
30243+ /* i_generation */
30244+ struct file *si_xigen;
30245+ atomic_t si_xigen_next;
30246+#endif
30247+
b912730e 30248+ /* dirty trick to suppoer atomic_open */
8b6a4947 30249+ struct hlist_bl_head si_aopen;
b912730e 30250+
1facf9fc 30251+ /* vdir parameters */
e49829fe 30252+ unsigned long si_rdcache; /* max cache time in jiffies */
1facf9fc 30253+ unsigned int si_rdblk; /* deblk size */
30254+ unsigned int si_rdhash; /* hash size */
30255+
30256+ /*
30257+ * If the number of whiteouts are larger than si_dirwh, leave all of
30258+ * them after au_whtmp_ren to reduce the cost of rmdir(2).
30259+ * future fsck.aufs or kernel thread will remove them later.
30260+ * Otherwise, remove all whiteouts and the dir in rmdir(2).
30261+ */
30262+ unsigned int si_dirwh;
30263+
1facf9fc 30264+ /* pseudo_link list */
8b6a4947 30265+ struct hlist_bl_head si_plink[AuPlink_NHASH];
1facf9fc 30266+ wait_queue_head_t si_plink_wq;
4a4d8108 30267+ spinlock_t si_plink_maint_lock;
e49829fe 30268+ pid_t si_plink_maint_pid;
1facf9fc 30269+
523b37e3 30270+ /* file list */
8b6a4947 30271+ struct hlist_bl_head si_files;
523b37e3 30272+
b95c5147
AM
30273+ /* with/without getattr, brother of sb->s_d_op */
30274+ struct inode_operations *si_iop_array;
30275+
1facf9fc 30276+ /*
30277+ * sysfs and lifetime management.
30278+ * this is not a small structure and it may be a waste of memory in case
30279+ * of sysfs is disabled, particulary when many aufs-es are mounted.
30280+ * but using sysfs is majority.
30281+ */
30282+ struct kobject si_kobj;
30283+#ifdef CONFIG_DEBUG_FS
86dc4139
AM
30284+ struct dentry *si_dbgaufs;
30285+ struct dentry *si_dbgaufs_plink;
30286+ struct dentry *si_dbgaufs_xib;
1facf9fc 30287+#ifdef CONFIG_AUFS_EXPORT
30288+ struct dentry *si_dbgaufs_xigen;
30289+#endif
30290+#endif
30291+
e49829fe 30292+#ifdef CONFIG_AUFS_SBILIST
8b6a4947 30293+ struct hlist_bl_node si_list;
e49829fe
JR
30294+#endif
30295+
1facf9fc 30296+ /* dirty, necessary for unmounting, sysfs and sysrq */
30297+ struct super_block *si_sb;
30298+};
30299+
dece6358
AM
30300+/* sbinfo status flags */
30301+/*
30302+ * set true when refresh_dirs() failed at remount time.
30303+ * then try refreshing dirs at access time again.
30304+ * if it is false, refreshing dirs at access time is unnecesary
30305+ */
027c5e7a 30306+#define AuSi_FAILED_REFRESH_DIR 1
076b876e 30307+#define AuSi_FHSM (1 << 1) /* fhsm is active now */
79b8bda9 30308+#define AuSi_NO_DREVAL (1 << 2) /* disable all d_revalidate */
076b876e
AM
30309+
30310+#ifndef CONFIG_AUFS_FHSM
30311+#undef AuSi_FHSM
30312+#define AuSi_FHSM 0
30313+#endif
30314+
dece6358
AM
30315+static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30316+ unsigned int flag)
30317+{
30318+ AuRwMustAnyLock(&sbi->si_rwsem);
30319+ return sbi->au_si_status & flag;
30320+}
30321+#define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
30322+#define au_fset_si(sbinfo, name) do { \
30323+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30324+ (sbinfo)->au_si_status |= AuSi_##name; \
30325+} while (0)
30326+#define au_fclr_si(sbinfo, name) do { \
30327+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30328+ (sbinfo)->au_si_status &= ~AuSi_##name; \
30329+} while (0)
30330+
1facf9fc 30331+/* ---------------------------------------------------------------------- */
30332+
30333+/* policy to select one among writable branches */
4a4d8108
AM
30334+#define AuWbrCopyup(sbinfo, ...) \
30335+ ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30336+#define AuWbrCreate(sbinfo, ...) \
30337+ ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
1facf9fc 30338+
30339+/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30340+#define AuLock_DW 1 /* write-lock dentry */
30341+#define AuLock_IR (1 << 1) /* read-lock inode */
30342+#define AuLock_IW (1 << 2) /* write-lock inode */
30343+#define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
b95c5147 30344+#define AuLock_DIRS (1 << 4) /* target is a pair of dirs */
f2c43d5f 30345+ /* except RENAME_EXCHANGE */
e49829fe
JR
30346+#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
30347+#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
027c5e7a 30348+#define AuLock_GEN (1 << 7) /* test digen/iigen */
1facf9fc 30349+#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
7f207e10
AM
30350+#define au_fset_lock(flags, name) \
30351+ do { (flags) |= AuLock_##name; } while (0)
30352+#define au_fclr_lock(flags, name) \
30353+ do { (flags) &= ~AuLock_##name; } while (0)
1facf9fc 30354+
30355+/* ---------------------------------------------------------------------- */
30356+
30357+/* super.c */
30358+extern struct file_system_type aufs_fs_type;
30359+struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
79b8bda9
AM
30360+typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30361+ unsigned long long max, void *arg);
79b8bda9
AM
30362+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30363+ struct super_block *sb, void *arg);
7f207e10
AM
30364+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30365+void au_iarray_free(struct inode **a, unsigned long long max);
1facf9fc 30366+
30367+/* sbinfo.c */
30368+void au_si_free(struct kobject *kobj);
30369+int au_si_alloc(struct super_block *sb);
e2f27e51 30370+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
1facf9fc 30371+
30372+unsigned int au_sigen_inc(struct super_block *sb);
30373+aufs_bindex_t au_new_br_id(struct super_block *sb);
30374+
e49829fe
JR
30375+int si_read_lock(struct super_block *sb, int flags);
30376+int si_write_lock(struct super_block *sb, int flags);
30377+int aufs_read_lock(struct dentry *dentry, int flags);
1facf9fc 30378+void aufs_read_unlock(struct dentry *dentry, int flags);
30379+void aufs_write_lock(struct dentry *dentry);
30380+void aufs_write_unlock(struct dentry *dentry);
e49829fe 30381+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
1facf9fc 30382+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30383+
30384+/* wbr_policy.c */
30385+extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30386+extern struct au_wbr_create_operations au_wbr_create_ops[];
30387+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
c2b27bf2 30388+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
5afbbe0d 30389+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
c2b27bf2
AM
30390+
30391+/* mvdown.c */
30392+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
1facf9fc 30393+
076b876e
AM
30394+#ifdef CONFIG_AUFS_FHSM
30395+/* fhsm.c */
30396+
30397+static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30398+{
30399+ pid_t pid;
30400+
30401+ spin_lock(&fhsm->fhsm_spin);
30402+ pid = fhsm->fhsm_pid;
30403+ spin_unlock(&fhsm->fhsm_spin);
30404+
30405+ return pid;
30406+}
30407+
30408+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
30409+void au_fhsm_wrote_all(struct super_block *sb, int force);
30410+int au_fhsm_fd(struct super_block *sb, int oflags);
30411+int au_fhsm_br_alloc(struct au_branch *br);
c1595e42 30412+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
076b876e
AM
30413+void au_fhsm_fin(struct super_block *sb);
30414+void au_fhsm_init(struct au_sbinfo *sbinfo);
30415+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
30416+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
30417+#else
30418+AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
30419+ int force)
30420+AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
30421+AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
c1595e42
JR
30422+AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
30423+AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
30424+AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
076b876e
AM
30425+AuStubVoid(au_fhsm_fin, struct super_block *sb)
30426+AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
30427+AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
30428+AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
30429+#endif
30430+
1facf9fc 30431+/* ---------------------------------------------------------------------- */
30432+
30433+static inline struct au_sbinfo *au_sbi(struct super_block *sb)
30434+{
30435+ return sb->s_fs_info;
30436+}
30437+
30438+/* ---------------------------------------------------------------------- */
30439+
30440+#ifdef CONFIG_AUFS_EXPORT
a2a7ad62 30441+int au_test_nfsd(void);
1facf9fc 30442+void au_export_init(struct super_block *sb);
b752ccd1 30443+void au_xigen_inc(struct inode *inode);
1facf9fc 30444+int au_xigen_new(struct inode *inode);
30445+int au_xigen_set(struct super_block *sb, struct file *base);
30446+void au_xigen_clr(struct super_block *sb);
30447+
30448+static inline int au_busy_or_stale(void)
30449+{
b752ccd1 30450+ if (!au_test_nfsd())
1facf9fc 30451+ return -EBUSY;
30452+ return -ESTALE;
30453+}
30454+#else
b752ccd1 30455+AuStubInt0(au_test_nfsd, void)
a2a7ad62 30456+AuStubVoid(au_export_init, struct super_block *sb)
b752ccd1 30457+AuStubVoid(au_xigen_inc, struct inode *inode)
4a4d8108
AM
30458+AuStubInt0(au_xigen_new, struct inode *inode)
30459+AuStubInt0(au_xigen_set, struct super_block *sb, struct file *base)
30460+AuStubVoid(au_xigen_clr, struct super_block *sb)
c1595e42 30461+AuStub(int, au_busy_or_stale, return -EBUSY, void)
1facf9fc 30462+#endif /* CONFIG_AUFS_EXPORT */
30463+
30464+/* ---------------------------------------------------------------------- */
30465+
e49829fe
JR
30466+#ifdef CONFIG_AUFS_SBILIST
30467+/* module.c */
8b6a4947 30468+extern struct hlist_bl_head au_sbilist;
e49829fe
JR
30469+
30470+static inline void au_sbilist_init(void)
30471+{
8b6a4947 30472+ INIT_HLIST_BL_HEAD(&au_sbilist);
e49829fe
JR
30473+}
30474+
30475+static inline void au_sbilist_add(struct super_block *sb)
30476+{
8b6a4947 30477+ au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe
JR
30478+}
30479+
30480+static inline void au_sbilist_del(struct super_block *sb)
30481+{
8b6a4947 30482+ au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe 30483+}
53392da6
AM
30484+
30485+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
30486+static inline void au_sbilist_lock(void)
30487+{
8b6a4947 30488+ hlist_bl_lock(&au_sbilist);
53392da6
AM
30489+}
30490+
30491+static inline void au_sbilist_unlock(void)
30492+{
8b6a4947 30493+ hlist_bl_unlock(&au_sbilist);
53392da6
AM
30494+}
30495+#define AuGFP_SBILIST GFP_ATOMIC
30496+#else
30497+AuStubVoid(au_sbilist_lock, void)
30498+AuStubVoid(au_sbilist_unlock, void)
30499+#define AuGFP_SBILIST GFP_NOFS
30500+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
e49829fe
JR
30501+#else
30502+AuStubVoid(au_sbilist_init, void)
c1595e42
JR
30503+AuStubVoid(au_sbilist_add, struct super_block *sb)
30504+AuStubVoid(au_sbilist_del, struct super_block *sb)
53392da6
AM
30505+AuStubVoid(au_sbilist_lock, void)
30506+AuStubVoid(au_sbilist_unlock, void)
30507+#define AuGFP_SBILIST GFP_NOFS
e49829fe
JR
30508+#endif
30509+
30510+/* ---------------------------------------------------------------------- */
30511+
1facf9fc 30512+static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
30513+{
dece6358 30514+ /*
c1595e42 30515+ * This function is a dynamic '__init' function actually,
dece6358
AM
30516+ * so the tiny check for si_rwsem is unnecessary.
30517+ */
30518+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
1facf9fc 30519+#ifdef CONFIG_DEBUG_FS
30520+ sbinfo->si_dbgaufs = NULL;
86dc4139 30521+ sbinfo->si_dbgaufs_plink = NULL;
1facf9fc 30522+ sbinfo->si_dbgaufs_xib = NULL;
30523+#ifdef CONFIG_AUFS_EXPORT
30524+ sbinfo->si_dbgaufs_xigen = NULL;
30525+#endif
30526+#endif
30527+}
30528+
30529+/* ---------------------------------------------------------------------- */
30530+
a2654f78
AM
30531+/* current->atomic_flags */
30532+/* this value should never corrupt the ones defined in linux/sched.h */
30533+#define PFA_AUFS 7
30534+
30535+TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
30536+TASK_PFA_SET(AUFS, aufs) /* task_set_aufs */
30537+TASK_PFA_CLEAR(AUFS, aufs) /* task_clear_aufs */
b752ccd1
AM
30538+
30539+static inline int si_pid_test(struct super_block *sb)
30540+{
a2654f78 30541+ return !!task_test_aufs(current);
b752ccd1
AM
30542+}
30543+
30544+static inline void si_pid_clr(struct super_block *sb)
30545+{
a2654f78
AM
30546+ AuDebugOn(!task_test_aufs(current));
30547+ task_clear_aufs(current);
b752ccd1
AM
30548+}
30549+
a2654f78
AM
30550+static inline void si_pid_set(struct super_block *sb)
30551+{
30552+ AuDebugOn(task_test_aufs(current));
30553+ task_set_aufs(current);
30554+}
febd17d6 30555+
b752ccd1
AM
30556+/* ---------------------------------------------------------------------- */
30557+
1facf9fc 30558+/* lock superblock. mainly for entry point functions */
8b6a4947
AM
30559+#define __si_read_lock(sb) au_rw_read_lock(&au_sbi(sb)->si_rwsem)
30560+#define __si_write_lock(sb) au_rw_write_lock(&au_sbi(sb)->si_rwsem)
30561+#define __si_read_trylock(sb) au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
30562+#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
30563+/*
30564+#define __si_read_trylock_nested(sb) \
30565+ au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
30566+#define __si_write_trylock_nested(sb) \
30567+ au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
30568+*/
30569+
30570+#define __si_read_unlock(sb) au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
30571+#define __si_write_unlock(sb) au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
30572+#define __si_downgrade_lock(sb) au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
1facf9fc 30573+
dece6358
AM
30574+#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
30575+#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
30576+#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
30577+
b752ccd1
AM
30578+static inline void si_noflush_read_lock(struct super_block *sb)
30579+{
30580+ __si_read_lock(sb);
30581+ si_pid_set(sb);
30582+}
30583+
30584+static inline int si_noflush_read_trylock(struct super_block *sb)
30585+{
076b876e
AM
30586+ int locked;
30587+
30588+ locked = __si_read_trylock(sb);
b752ccd1
AM
30589+ if (locked)
30590+ si_pid_set(sb);
30591+ return locked;
30592+}
30593+
30594+static inline void si_noflush_write_lock(struct super_block *sb)
30595+{
30596+ __si_write_lock(sb);
30597+ si_pid_set(sb);
30598+}
30599+
30600+static inline int si_noflush_write_trylock(struct super_block *sb)
30601+{
076b876e
AM
30602+ int locked;
30603+
30604+ locked = __si_write_trylock(sb);
b752ccd1
AM
30605+ if (locked)
30606+ si_pid_set(sb);
30607+ return locked;
30608+}
30609+
7e9cd9fe 30610+#if 0 /* reserved */
1facf9fc 30611+static inline int si_read_trylock(struct super_block *sb, int flags)
30612+{
30613+ if (au_ftest_lock(flags, FLUSH))
30614+ au_nwt_flush(&au_sbi(sb)->si_nowait);
30615+ return si_noflush_read_trylock(sb);
30616+}
e49829fe 30617+#endif
1facf9fc 30618+
b752ccd1
AM
30619+static inline void si_read_unlock(struct super_block *sb)
30620+{
30621+ si_pid_clr(sb);
30622+ __si_read_unlock(sb);
30623+}
30624+
7e9cd9fe 30625+#if 0 /* reserved */
1facf9fc 30626+static inline int si_write_trylock(struct super_block *sb, int flags)
30627+{
30628+ if (au_ftest_lock(flags, FLUSH))
30629+ au_nwt_flush(&au_sbi(sb)->si_nowait);
30630+ return si_noflush_write_trylock(sb);
30631+}
b752ccd1
AM
30632+#endif
30633+
30634+static inline void si_write_unlock(struct super_block *sb)
30635+{
30636+ si_pid_clr(sb);
30637+ __si_write_unlock(sb);
30638+}
30639+
7e9cd9fe 30640+#if 0 /* reserved */
b752ccd1
AM
30641+static inline void si_downgrade_lock(struct super_block *sb)
30642+{
30643+ __si_downgrade_lock(sb);
30644+}
30645+#endif
1facf9fc 30646+
30647+/* ---------------------------------------------------------------------- */
30648+
5afbbe0d 30649+static inline aufs_bindex_t au_sbbot(struct super_block *sb)
1facf9fc 30650+{
dece6358 30651+ SiMustAnyLock(sb);
5afbbe0d 30652+ return au_sbi(sb)->si_bbot;
1facf9fc 30653+}
30654+
30655+static inline unsigned int au_mntflags(struct super_block *sb)
30656+{
dece6358 30657+ SiMustAnyLock(sb);
1facf9fc 30658+ return au_sbi(sb)->si_mntflags;
30659+}
30660+
30661+static inline unsigned int au_sigen(struct super_block *sb)
30662+{
dece6358 30663+ SiMustAnyLock(sb);
1facf9fc 30664+ return au_sbi(sb)->si_generation;
30665+}
30666+
5afbbe0d
AM
30667+static inline unsigned long long au_ninodes(struct super_block *sb)
30668+{
30669+ s64 n = percpu_counter_sum(&au_sbi(sb)->si_ninodes);
30670+
30671+ BUG_ON(n < 0);
30672+ return n;
30673+}
30674+
7f207e10
AM
30675+static inline void au_ninodes_inc(struct super_block *sb)
30676+{
5afbbe0d 30677+ percpu_counter_inc(&au_sbi(sb)->si_ninodes);
7f207e10
AM
30678+}
30679+
30680+static inline void au_ninodes_dec(struct super_block *sb)
30681+{
5afbbe0d
AM
30682+ percpu_counter_dec(&au_sbi(sb)->si_ninodes);
30683+}
30684+
30685+static inline unsigned long long au_nfiles(struct super_block *sb)
30686+{
30687+ s64 n = percpu_counter_sum(&au_sbi(sb)->si_nfiles);
30688+
30689+ BUG_ON(n < 0);
30690+ return n;
7f207e10
AM
30691+}
30692+
30693+static inline void au_nfiles_inc(struct super_block *sb)
30694+{
5afbbe0d 30695+ percpu_counter_inc(&au_sbi(sb)->si_nfiles);
7f207e10
AM
30696+}
30697+
30698+static inline void au_nfiles_dec(struct super_block *sb)
30699+{
5afbbe0d 30700+ percpu_counter_dec(&au_sbi(sb)->si_nfiles);
7f207e10
AM
30701+}
30702+
1facf9fc 30703+static inline struct au_branch *au_sbr(struct super_block *sb,
30704+ aufs_bindex_t bindex)
30705+{
dece6358 30706+ SiMustAnyLock(sb);
1facf9fc 30707+ return au_sbi(sb)->si_branch[0 + bindex];
30708+}
30709+
30710+static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
30711+{
dece6358 30712+ SiMustWriteLock(sb);
1facf9fc 30713+ au_sbi(sb)->si_xino_brid = brid;
30714+}
30715+
30716+static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
30717+{
dece6358 30718+ SiMustAnyLock(sb);
1facf9fc 30719+ return au_sbi(sb)->si_xino_brid;
30720+}
30721+
30722+#endif /* __KERNEL__ */
30723+#endif /* __AUFS_SUPER_H__ */
7f207e10
AM
30724diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
30725--- /usr/share/empty/fs/aufs/sysaufs.c 1970-01-01 01:00:00.000000000 +0100
1c60b727 30726+++ linux/fs/aufs/sysaufs.c 2017-07-29 12:14:25.906375514 +0200
523b37e3 30727@@ -0,0 +1,104 @@
1facf9fc 30728+/*
a2654f78 30729+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 30730+ *
30731+ * This program, aufs is free software; you can redistribute it and/or modify
30732+ * it under the terms of the GNU General Public License as published by
30733+ * the Free Software Foundation; either version 2 of the License, or
30734+ * (at your option) any later version.
dece6358
AM
30735+ *
30736+ * This program is distributed in the hope that it will be useful,
30737+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30738+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30739+ * GNU General Public License for more details.
30740+ *
30741+ * You should have received a copy of the GNU General Public License
523b37e3 30742+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30743+ */
30744+
30745+/*
30746+ * sysfs interface and lifetime management
30747+ * they are necessary regardless sysfs is disabled.
30748+ */
30749+
1facf9fc 30750+#include <linux/random.h>
1facf9fc 30751+#include "aufs.h"
30752+
30753+unsigned long sysaufs_si_mask;
e49829fe 30754+struct kset *sysaufs_kset;
1facf9fc 30755+
30756+#define AuSiAttr(_name) { \
30757+ .attr = { .name = __stringify(_name), .mode = 0444 }, \
30758+ .show = sysaufs_si_##_name, \
30759+}
30760+
30761+static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
30762+struct attribute *sysaufs_si_attrs[] = {
30763+ &sysaufs_si_attr_xi_path.attr,
30764+ NULL,
30765+};
30766+
4a4d8108 30767+static const struct sysfs_ops au_sbi_ops = {
1facf9fc 30768+ .show = sysaufs_si_show
30769+};
30770+
30771+static struct kobj_type au_sbi_ktype = {
30772+ .release = au_si_free,
30773+ .sysfs_ops = &au_sbi_ops,
30774+ .default_attrs = sysaufs_si_attrs
30775+};
30776+
30777+/* ---------------------------------------------------------------------- */
30778+
30779+int sysaufs_si_init(struct au_sbinfo *sbinfo)
30780+{
30781+ int err;
30782+
e49829fe 30783+ sbinfo->si_kobj.kset = sysaufs_kset;
1facf9fc 30784+ /* cf. sysaufs_name() */
30785+ err = kobject_init_and_add
e49829fe 30786+ (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
1facf9fc 30787+ SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
30788+
30789+ dbgaufs_si_null(sbinfo);
30790+ if (!err) {
30791+ err = dbgaufs_si_init(sbinfo);
30792+ if (unlikely(err))
30793+ kobject_put(&sbinfo->si_kobj);
30794+ }
30795+ return err;
30796+}
30797+
30798+void sysaufs_fin(void)
30799+{
30800+ dbgaufs_fin();
e49829fe
JR
30801+ sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
30802+ kset_unregister(sysaufs_kset);
1facf9fc 30803+}
30804+
30805+int __init sysaufs_init(void)
30806+{
30807+ int err;
30808+
30809+ do {
30810+ get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
30811+ } while (!sysaufs_si_mask);
30812+
4a4d8108 30813+ err = -EINVAL;
e49829fe
JR
30814+ sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
30815+ if (unlikely(!sysaufs_kset))
4a4d8108 30816+ goto out;
e49829fe
JR
30817+ err = PTR_ERR(sysaufs_kset);
30818+ if (IS_ERR(sysaufs_kset))
1facf9fc 30819+ goto out;
e49829fe 30820+ err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
1facf9fc 30821+ if (unlikely(err)) {
e49829fe 30822+ kset_unregister(sysaufs_kset);
1facf9fc 30823+ goto out;
30824+ }
30825+
30826+ err = dbgaufs_init();
30827+ if (unlikely(err))
30828+ sysaufs_fin();
4f0767ce 30829+out:
1facf9fc 30830+ return err;
30831+}
7f207e10
AM
30832diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
30833--- /usr/share/empty/fs/aufs/sysaufs.h 1970-01-01 01:00:00.000000000 +0100
1c60b727 30834+++ linux/fs/aufs/sysaufs.h 2017-07-29 12:14:25.906375514 +0200
c1595e42 30835@@ -0,0 +1,101 @@
1facf9fc 30836+/*
a2654f78 30837+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 30838+ *
30839+ * This program, aufs is free software; you can redistribute it and/or modify
30840+ * it under the terms of the GNU General Public License as published by
30841+ * the Free Software Foundation; either version 2 of the License, or
30842+ * (at your option) any later version.
dece6358
AM
30843+ *
30844+ * This program is distributed in the hope that it will be useful,
30845+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30846+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30847+ * GNU General Public License for more details.
30848+ *
30849+ * You should have received a copy of the GNU General Public License
523b37e3 30850+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30851+ */
30852+
30853+/*
30854+ * sysfs interface and mount lifetime management
30855+ */
30856+
30857+#ifndef __SYSAUFS_H__
30858+#define __SYSAUFS_H__
30859+
30860+#ifdef __KERNEL__
30861+
1facf9fc 30862+#include <linux/sysfs.h>
1facf9fc 30863+#include "module.h"
30864+
dece6358
AM
30865+struct super_block;
30866+struct au_sbinfo;
30867+
1facf9fc 30868+struct sysaufs_si_attr {
30869+ struct attribute attr;
30870+ int (*show)(struct seq_file *seq, struct super_block *sb);
30871+};
30872+
30873+/* ---------------------------------------------------------------------- */
30874+
30875+/* sysaufs.c */
30876+extern unsigned long sysaufs_si_mask;
e49829fe 30877+extern struct kset *sysaufs_kset;
1facf9fc 30878+extern struct attribute *sysaufs_si_attrs[];
30879+int sysaufs_si_init(struct au_sbinfo *sbinfo);
30880+int __init sysaufs_init(void);
30881+void sysaufs_fin(void);
30882+
30883+/* ---------------------------------------------------------------------- */
30884+
30885+/* some people doesn't like to show a pointer in kernel */
30886+static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
30887+{
30888+ return sysaufs_si_mask ^ (unsigned long)sbinfo;
30889+}
30890+
30891+#define SysaufsSiNamePrefix "si_"
30892+#define SysaufsSiNameLen (sizeof(SysaufsSiNamePrefix) + 16)
30893+static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
30894+{
30895+ snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
30896+ sysaufs_si_id(sbinfo));
30897+}
30898+
30899+struct au_branch;
30900+#ifdef CONFIG_SYSFS
30901+/* sysfs.c */
30902+extern struct attribute_group *sysaufs_attr_group;
30903+
30904+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
30905+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
30906+ char *buf);
076b876e
AM
30907+long au_brinfo_ioctl(struct file *file, unsigned long arg);
30908+#ifdef CONFIG_COMPAT
30909+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
30910+#endif
1facf9fc 30911+
30912+void sysaufs_br_init(struct au_branch *br);
30913+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
30914+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
30915+
30916+#define sysaufs_brs_init() do {} while (0)
30917+
30918+#else
30919+#define sysaufs_attr_group NULL
30920+
4a4d8108 30921+AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
c1595e42
JR
30922+AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
30923+ struct attribute *attr, char *buf)
4a4d8108
AM
30924+AuStubVoid(sysaufs_br_init, struct au_branch *br)
30925+AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
30926+AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
1facf9fc 30927+
30928+static inline void sysaufs_brs_init(void)
30929+{
30930+ sysaufs_brs = 0;
30931+}
30932+
30933+#endif /* CONFIG_SYSFS */
30934+
30935+#endif /* __KERNEL__ */
30936+#endif /* __SYSAUFS_H__ */
7f207e10
AM
30937diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
30938--- /usr/share/empty/fs/aufs/sysfs.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947 30939+++ linux/fs/aufs/sysfs.c 2017-11-12 22:24:42.274176929 +0100
79b8bda9 30940@@ -0,0 +1,376 @@
1facf9fc 30941+/*
a2654f78 30942+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 30943+ *
30944+ * This program, aufs is free software; you can redistribute it and/or modify
30945+ * it under the terms of the GNU General Public License as published by
30946+ * the Free Software Foundation; either version 2 of the License, or
30947+ * (at your option) any later version.
dece6358
AM
30948+ *
30949+ * This program is distributed in the hope that it will be useful,
30950+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30951+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30952+ * GNU General Public License for more details.
30953+ *
30954+ * You should have received a copy of the GNU General Public License
523b37e3 30955+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30956+ */
30957+
30958+/*
30959+ * sysfs interface
30960+ */
30961+
076b876e 30962+#include <linux/compat.h>
1facf9fc 30963+#include <linux/seq_file.h>
1facf9fc 30964+#include "aufs.h"
30965+
4a4d8108
AM
30966+#ifdef CONFIG_AUFS_FS_MODULE
30967+/* this entry violates the "one line per file" policy of sysfs */
30968+static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
30969+ char *buf)
30970+{
30971+ ssize_t err;
30972+ static char *conf =
30973+/* this file is generated at compiling */
30974+#include "conf.str"
30975+ ;
30976+
30977+ err = snprintf(buf, PAGE_SIZE, conf);
30978+ if (unlikely(err >= PAGE_SIZE))
30979+ err = -EFBIG;
30980+ return err;
30981+}
30982+
30983+static struct kobj_attribute au_config_attr = __ATTR_RO(config);
30984+#endif
30985+
1facf9fc 30986+static struct attribute *au_attr[] = {
4a4d8108
AM
30987+#ifdef CONFIG_AUFS_FS_MODULE
30988+ &au_config_attr.attr,
30989+#endif
1facf9fc 30990+ NULL, /* need to NULL terminate the list of attributes */
30991+};
30992+
30993+static struct attribute_group sysaufs_attr_group_body = {
30994+ .attrs = au_attr
30995+};
30996+
30997+struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
30998+
30999+/* ---------------------------------------------------------------------- */
31000+
31001+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31002+{
31003+ int err;
31004+
dece6358
AM
31005+ SiMustAnyLock(sb);
31006+
1facf9fc 31007+ err = 0;
31008+ if (au_opt_test(au_mntflags(sb), XINO)) {
31009+ err = au_xino_path(seq, au_sbi(sb)->si_xib);
31010+ seq_putc(seq, '\n');
31011+ }
31012+ return err;
31013+}
31014+
31015+/*
31016+ * the lifetime of branch is independent from the entry under sysfs.
31017+ * sysfs handles the lifetime of the entry, and never call ->show() after it is
31018+ * unlinked.
31019+ */
31020+static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
392086de 31021+ aufs_bindex_t bindex, int idx)
1facf9fc 31022+{
1e00d052 31023+ int err;
1facf9fc 31024+ struct path path;
31025+ struct dentry *root;
31026+ struct au_branch *br;
076b876e 31027+ au_br_perm_str_t perm;
1facf9fc 31028+
31029+ AuDbg("b%d\n", bindex);
31030+
1e00d052 31031+ err = 0;
1facf9fc 31032+ root = sb->s_root;
31033+ di_read_lock_parent(root, !AuLock_IR);
31034+ br = au_sbr(sb, bindex);
392086de
AM
31035+
31036+ switch (idx) {
31037+ case AuBrSysfs_BR:
31038+ path.mnt = au_br_mnt(br);
31039+ path.dentry = au_h_dptr(root, bindex);
79b8bda9
AM
31040+ err = au_seq_path(seq, &path);
31041+ if (!err) {
31042+ au_optstr_br_perm(&perm, br->br_perm);
31043+ seq_printf(seq, "=%s\n", perm.a);
31044+ }
392086de
AM
31045+ break;
31046+ case AuBrSysfs_BRID:
79b8bda9 31047+ seq_printf(seq, "%d\n", br->br_id);
392086de
AM
31048+ break;
31049+ }
076b876e 31050+ di_read_unlock(root, !AuLock_IR);
79b8bda9 31051+ if (unlikely(err || seq_has_overflowed(seq)))
076b876e 31052+ err = -E2BIG;
392086de 31053+
1e00d052 31054+ return err;
1facf9fc 31055+}
31056+
31057+/* ---------------------------------------------------------------------- */
31058+
31059+static struct seq_file *au_seq(char *p, ssize_t len)
31060+{
31061+ struct seq_file *seq;
31062+
31063+ seq = kzalloc(sizeof(*seq), GFP_NOFS);
31064+ if (seq) {
31065+ /* mutex_init(&seq.lock); */
31066+ seq->buf = p;
31067+ seq->size = len;
31068+ return seq; /* success */
31069+ }
31070+
31071+ seq = ERR_PTR(-ENOMEM);
31072+ return seq;
31073+}
31074+
392086de
AM
31075+#define SysaufsBr_PREFIX "br"
31076+#define SysaufsBrid_PREFIX "brid"
1facf9fc 31077+
31078+/* todo: file size may exceed PAGE_SIZE */
31079+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
1308ab2a 31080+ char *buf)
1facf9fc 31081+{
31082+ ssize_t err;
392086de 31083+ int idx;
1facf9fc 31084+ long l;
5afbbe0d 31085+ aufs_bindex_t bbot;
1facf9fc 31086+ struct au_sbinfo *sbinfo;
31087+ struct super_block *sb;
31088+ struct seq_file *seq;
31089+ char *name;
31090+ struct attribute **cattr;
31091+
31092+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31093+ sb = sbinfo->si_sb;
1308ab2a 31094+
31095+ /*
31096+ * prevent a race condition between sysfs and aufs.
31097+ * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31098+ * prohibits maintaining the sysfs entries.
31099+ * hew we acquire read lock after sysfs_get_active_two().
31100+ * on the other hand, the remount process may maintain the sysfs/aufs
31101+ * entries after acquiring write lock.
31102+ * it can cause a deadlock.
31103+ * simply we gave up processing read here.
31104+ */
31105+ err = -EBUSY;
31106+ if (unlikely(!si_noflush_read_trylock(sb)))
31107+ goto out;
1facf9fc 31108+
31109+ seq = au_seq(buf, PAGE_SIZE);
31110+ err = PTR_ERR(seq);
31111+ if (IS_ERR(seq))
1308ab2a 31112+ goto out_unlock;
1facf9fc 31113+
31114+ name = (void *)attr->name;
31115+ cattr = sysaufs_si_attrs;
31116+ while (*cattr) {
31117+ if (!strcmp(name, (*cattr)->name)) {
31118+ err = container_of(*cattr, struct sysaufs_si_attr, attr)
31119+ ->show(seq, sb);
31120+ goto out_seq;
31121+ }
31122+ cattr++;
31123+ }
31124+
392086de
AM
31125+ if (!strncmp(name, SysaufsBrid_PREFIX,
31126+ sizeof(SysaufsBrid_PREFIX) - 1)) {
31127+ idx = AuBrSysfs_BRID;
31128+ name += sizeof(SysaufsBrid_PREFIX) - 1;
31129+ } else if (!strncmp(name, SysaufsBr_PREFIX,
31130+ sizeof(SysaufsBr_PREFIX) - 1)) {
31131+ idx = AuBrSysfs_BR;
1facf9fc 31132+ name += sizeof(SysaufsBr_PREFIX) - 1;
392086de
AM
31133+ } else
31134+ BUG();
31135+
31136+ err = kstrtol(name, 10, &l);
31137+ if (!err) {
5afbbe0d
AM
31138+ bbot = au_sbbot(sb);
31139+ if (l <= bbot)
392086de
AM
31140+ err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31141+ else
31142+ err = -ENOENT;
1facf9fc 31143+ }
1facf9fc 31144+
4f0767ce 31145+out_seq:
1facf9fc 31146+ if (!err) {
31147+ err = seq->count;
31148+ /* sysfs limit */
31149+ if (unlikely(err == PAGE_SIZE))
31150+ err = -EFBIG;
31151+ }
1c60b727 31152+ kfree(seq);
4f0767ce 31153+out_unlock:
1facf9fc 31154+ si_read_unlock(sb);
4f0767ce 31155+out:
1facf9fc 31156+ return err;
31157+}
31158+
31159+/* ---------------------------------------------------------------------- */
31160+
076b876e
AM
31161+static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31162+{
31163+ int err;
31164+ int16_t brid;
5afbbe0d 31165+ aufs_bindex_t bindex, bbot;
076b876e
AM
31166+ size_t sz;
31167+ char *buf;
31168+ struct seq_file *seq;
31169+ struct au_branch *br;
31170+
31171+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d
AM
31172+ bbot = au_sbbot(sb);
31173+ err = bbot + 1;
076b876e
AM
31174+ if (!arg)
31175+ goto out;
31176+
31177+ err = -ENOMEM;
31178+ buf = (void *)__get_free_page(GFP_NOFS);
31179+ if (unlikely(!buf))
31180+ goto out;
31181+
31182+ seq = au_seq(buf, PAGE_SIZE);
31183+ err = PTR_ERR(seq);
31184+ if (IS_ERR(seq))
31185+ goto out_buf;
31186+
31187+ sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
5afbbe0d 31188+ for (bindex = 0; bindex <= bbot; bindex++, arg++) {
076b876e
AM
31189+ err = !access_ok(VERIFY_WRITE, arg, sizeof(*arg));
31190+ if (unlikely(err))
31191+ break;
31192+
31193+ br = au_sbr(sb, bindex);
31194+ brid = br->br_id;
31195+ BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31196+ err = __put_user(brid, &arg->id);
31197+ if (unlikely(err))
31198+ break;
31199+
31200+ BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31201+ err = __put_user(br->br_perm, &arg->perm);
31202+ if (unlikely(err))
31203+ break;
31204+
79b8bda9
AM
31205+ err = au_seq_path(seq, &br->br_path);
31206+ if (unlikely(err))
31207+ break;
31208+ seq_putc(seq, '\0');
31209+ if (!seq_has_overflowed(seq)) {
076b876e
AM
31210+ err = copy_to_user(arg->path, seq->buf, seq->count);
31211+ seq->count = 0;
31212+ if (unlikely(err))
31213+ break;
31214+ } else {
31215+ err = -E2BIG;
31216+ goto out_seq;
31217+ }
31218+ }
31219+ if (unlikely(err))
31220+ err = -EFAULT;
31221+
31222+out_seq:
1c60b727 31223+ kfree(seq);
076b876e 31224+out_buf:
1c60b727 31225+ free_page((unsigned long)buf);
076b876e
AM
31226+out:
31227+ si_read_unlock(sb);
31228+ return err;
31229+}
31230+
31231+long au_brinfo_ioctl(struct file *file, unsigned long arg)
31232+{
2000de60 31233+ return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
076b876e
AM
31234+}
31235+
31236+#ifdef CONFIG_COMPAT
31237+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31238+{
2000de60 31239+ return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
076b876e
AM
31240+}
31241+#endif
31242+
31243+/* ---------------------------------------------------------------------- */
31244+
1facf9fc 31245+void sysaufs_br_init(struct au_branch *br)
31246+{
392086de
AM
31247+ int i;
31248+ struct au_brsysfs *br_sysfs;
31249+ struct attribute *attr;
4a4d8108 31250+
392086de
AM
31251+ br_sysfs = br->br_sysfs;
31252+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31253+ attr = &br_sysfs->attr;
31254+ sysfs_attr_init(attr);
31255+ attr->name = br_sysfs->name;
31256+ attr->mode = S_IRUGO;
31257+ br_sysfs++;
31258+ }
1facf9fc 31259+}
31260+
31261+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31262+{
31263+ struct au_branch *br;
31264+ struct kobject *kobj;
392086de
AM
31265+ struct au_brsysfs *br_sysfs;
31266+ int i;
5afbbe0d 31267+ aufs_bindex_t bbot;
1facf9fc 31268+
31269+ dbgaufs_brs_del(sb, bindex);
31270+
31271+ if (!sysaufs_brs)
31272+ return;
31273+
31274+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
31275+ bbot = au_sbbot(sb);
31276+ for (; bindex <= bbot; bindex++) {
1facf9fc 31277+ br = au_sbr(sb, bindex);
392086de
AM
31278+ br_sysfs = br->br_sysfs;
31279+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31280+ sysfs_remove_file(kobj, &br_sysfs->attr);
31281+ br_sysfs++;
31282+ }
1facf9fc 31283+ }
31284+}
31285+
31286+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31287+{
392086de 31288+ int err, i;
5afbbe0d 31289+ aufs_bindex_t bbot;
1facf9fc 31290+ struct kobject *kobj;
31291+ struct au_branch *br;
392086de 31292+ struct au_brsysfs *br_sysfs;
1facf9fc 31293+
31294+ dbgaufs_brs_add(sb, bindex);
31295+
31296+ if (!sysaufs_brs)
31297+ return;
31298+
31299+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
31300+ bbot = au_sbbot(sb);
31301+ for (; bindex <= bbot; bindex++) {
1facf9fc 31302+ br = au_sbr(sb, bindex);
392086de
AM
31303+ br_sysfs = br->br_sysfs;
31304+ snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31305+ SysaufsBr_PREFIX "%d", bindex);
31306+ snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31307+ SysaufsBrid_PREFIX "%d", bindex);
31308+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31309+ err = sysfs_create_file(kobj, &br_sysfs->attr);
31310+ if (unlikely(err))
31311+ pr_warn("failed %s under sysfs(%d)\n",
31312+ br_sysfs->name, err);
31313+ br_sysfs++;
31314+ }
1facf9fc 31315+ }
31316+}
7f207e10
AM
31317diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31318--- /usr/share/empty/fs/aufs/sysrq.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
31319+++ linux/fs/aufs/sysrq.c 2017-11-12 22:24:44.707577830 +0100
31320@@ -0,0 +1,159 @@
1facf9fc 31321+/*
a2654f78 31322+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 31323+ *
31324+ * This program, aufs is free software; you can redistribute it and/or modify
31325+ * it under the terms of the GNU General Public License as published by
31326+ * the Free Software Foundation; either version 2 of the License, or
31327+ * (at your option) any later version.
dece6358
AM
31328+ *
31329+ * This program is distributed in the hope that it will be useful,
31330+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31331+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31332+ * GNU General Public License for more details.
31333+ *
31334+ * You should have received a copy of the GNU General Public License
523b37e3 31335+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31336+ */
31337+
31338+/*
31339+ * magic sysrq hanlder
31340+ */
31341+
1facf9fc 31342+/* #include <linux/sysrq.h> */
027c5e7a 31343+#include <linux/writeback.h>
1facf9fc 31344+#include "aufs.h"
31345+
31346+/* ---------------------------------------------------------------------- */
31347+
31348+static void sysrq_sb(struct super_block *sb)
31349+{
31350+ char *plevel;
31351+ struct au_sbinfo *sbinfo;
31352+ struct file *file;
8b6a4947
AM
31353+ struct hlist_bl_head *files;
31354+ struct hlist_bl_node *pos;
523b37e3 31355+ struct au_finfo *finfo;
1facf9fc 31356+
31357+ plevel = au_plevel;
31358+ au_plevel = KERN_WARNING;
1facf9fc 31359+
4a4d8108 31360+ /* since we define pr_fmt, call printk directly */
c06a8ce3
AM
31361+#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31362+
31363+ sbinfo = au_sbi(sb);
4a4d8108 31364+ printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
c06a8ce3 31365+ pr("superblock\n");
1facf9fc 31366+ au_dpri_sb(sb);
027c5e7a
AM
31367+
31368+#if 0
c06a8ce3 31369+ pr("root dentry\n");
1facf9fc 31370+ au_dpri_dentry(sb->s_root);
c06a8ce3 31371+ pr("root inode\n");
5527c038 31372+ au_dpri_inode(d_inode(sb->s_root));
027c5e7a
AM
31373+#endif
31374+
1facf9fc 31375+#if 0
027c5e7a
AM
31376+ do {
31377+ int err, i, j, ndentry;
31378+ struct au_dcsub_pages dpages;
31379+ struct au_dpage *dpage;
31380+
31381+ err = au_dpages_init(&dpages, GFP_ATOMIC);
31382+ if (unlikely(err))
31383+ break;
31384+ err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31385+ if (!err)
31386+ for (i = 0; i < dpages.ndpage; i++) {
31387+ dpage = dpages.dpages + i;
31388+ ndentry = dpage->ndentry;
31389+ for (j = 0; j < ndentry; j++)
31390+ au_dpri_dentry(dpage->dentries[j]);
31391+ }
31392+ au_dpages_free(&dpages);
31393+ } while (0);
31394+#endif
31395+
31396+#if 1
31397+ {
31398+ struct inode *i;
076b876e 31399+
c06a8ce3 31400+ pr("isolated inode\n");
79b8bda9 31401+ spin_lock(&sb->s_inode_list_lock);
2cbb1c4b
JR
31402+ list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31403+ spin_lock(&i->i_lock);
b4510431 31404+ if (1 || hlist_empty(&i->i_dentry))
027c5e7a 31405+ au_dpri_inode(i);
2cbb1c4b
JR
31406+ spin_unlock(&i->i_lock);
31407+ }
79b8bda9 31408+ spin_unlock(&sb->s_inode_list_lock);
027c5e7a 31409+ }
1facf9fc 31410+#endif
c06a8ce3 31411+ pr("files\n");
523b37e3 31412+ files = &au_sbi(sb)->si_files;
8b6a4947
AM
31413+ hlist_bl_lock(files);
31414+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
4a4d8108 31415+ umode_t mode;
076b876e 31416+
523b37e3 31417+ file = finfo->fi_file;
c06a8ce3 31418+ mode = file_inode(file)->i_mode;
38d290e6 31419+ if (!special_file(mode))
1facf9fc 31420+ au_dpri_file(file);
523b37e3 31421+ }
8b6a4947 31422+ hlist_bl_unlock(files);
c06a8ce3 31423+ pr("done\n");
1facf9fc 31424+
c06a8ce3 31425+#undef pr
1facf9fc 31426+ au_plevel = plevel;
1facf9fc 31427+}
31428+
31429+/* ---------------------------------------------------------------------- */
31430+
31431+/* module parameter */
31432+static char *aufs_sysrq_key = "a";
31433+module_param_named(sysrq, aufs_sysrq_key, charp, S_IRUGO);
31434+MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31435+
0c5527e5 31436+static void au_sysrq(int key __maybe_unused)
1facf9fc 31437+{
1facf9fc 31438+ struct au_sbinfo *sbinfo;
8b6a4947 31439+ struct hlist_bl_node *pos;
1facf9fc 31440+
027c5e7a 31441+ lockdep_off();
53392da6 31442+ au_sbilist_lock();
8b6a4947 31443+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
1facf9fc 31444+ sysrq_sb(sbinfo->si_sb);
53392da6 31445+ au_sbilist_unlock();
027c5e7a 31446+ lockdep_on();
1facf9fc 31447+}
31448+
31449+static struct sysrq_key_op au_sysrq_op = {
31450+ .handler = au_sysrq,
31451+ .help_msg = "Aufs",
31452+ .action_msg = "Aufs",
31453+ .enable_mask = SYSRQ_ENABLE_DUMP
31454+};
31455+
31456+/* ---------------------------------------------------------------------- */
31457+
31458+int __init au_sysrq_init(void)
31459+{
31460+ int err;
31461+ char key;
31462+
31463+ err = -1;
31464+ key = *aufs_sysrq_key;
31465+ if ('a' <= key && key <= 'z')
31466+ err = register_sysrq_key(key, &au_sysrq_op);
31467+ if (unlikely(err))
4a4d8108 31468+ pr_err("err %d, sysrq=%c\n", err, key);
1facf9fc 31469+ return err;
31470+}
31471+
31472+void au_sysrq_fin(void)
31473+{
31474+ int err;
076b876e 31475+
1facf9fc 31476+ err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31477+ if (unlikely(err))
4a4d8108 31478+ pr_err("err %d (ignored)\n", err);
1facf9fc 31479+}
7f207e10
AM
31480diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
31481--- /usr/share/empty/fs/aufs/vdir.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947 31482+++ linux/fs/aufs/vdir.c 2017-11-12 22:24:42.274176929 +0100
1c60b727 31483@@ -0,0 +1,892 @@
1facf9fc 31484+/*
a2654f78 31485+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 31486+ *
31487+ * This program, aufs is free software; you can redistribute it and/or modify
31488+ * it under the terms of the GNU General Public License as published by
31489+ * the Free Software Foundation; either version 2 of the License, or
31490+ * (at your option) any later version.
dece6358
AM
31491+ *
31492+ * This program is distributed in the hope that it will be useful,
31493+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31494+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31495+ * GNU General Public License for more details.
31496+ *
31497+ * You should have received a copy of the GNU General Public License
523b37e3 31498+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31499+ */
31500+
31501+/*
31502+ * virtual or vertical directory
31503+ */
31504+
31505+#include "aufs.h"
31506+
dece6358 31507+static unsigned int calc_size(int nlen)
1facf9fc 31508+{
dece6358 31509+ return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
1facf9fc 31510+}
31511+
31512+static int set_deblk_end(union au_vdir_deblk_p *p,
31513+ union au_vdir_deblk_p *deblk_end)
31514+{
31515+ if (calc_size(0) <= deblk_end->deblk - p->deblk) {
31516+ p->de->de_str.len = 0;
31517+ /* smp_mb(); */
31518+ return 0;
31519+ }
31520+ return -1; /* error */
31521+}
31522+
31523+/* returns true or false */
31524+static int is_deblk_end(union au_vdir_deblk_p *p,
31525+ union au_vdir_deblk_p *deblk_end)
31526+{
31527+ if (calc_size(0) <= deblk_end->deblk - p->deblk)
31528+ return !p->de->de_str.len;
31529+ return 1;
31530+}
31531+
31532+static unsigned char *last_deblk(struct au_vdir *vdir)
31533+{
31534+ return vdir->vd_deblk[vdir->vd_nblk - 1];
31535+}
31536+
31537+/* ---------------------------------------------------------------------- */
31538+
79b8bda9 31539+/* estimate the appropriate size for name hash table */
1308ab2a 31540+unsigned int au_rdhash_est(loff_t sz)
31541+{
31542+ unsigned int n;
31543+
31544+ n = UINT_MAX;
31545+ sz >>= 10;
31546+ if (sz < n)
31547+ n = sz;
31548+ if (sz < AUFS_RDHASH_DEF)
31549+ n = AUFS_RDHASH_DEF;
4a4d8108 31550+ /* pr_info("n %u\n", n); */
1308ab2a 31551+ return n;
31552+}
31553+
1facf9fc 31554+/*
31555+ * the allocated memory has to be freed by
dece6358 31556+ * au_nhash_wh_free() or au_nhash_de_free().
1facf9fc 31557+ */
dece6358 31558+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
1facf9fc 31559+{
1facf9fc 31560+ struct hlist_head *head;
dece6358 31561+ unsigned int u;
076b876e 31562+ size_t sz;
1facf9fc 31563+
076b876e
AM
31564+ sz = sizeof(*nhash->nh_head) * num_hash;
31565+ head = kmalloc(sz, gfp);
dece6358
AM
31566+ if (head) {
31567+ nhash->nh_num = num_hash;
31568+ nhash->nh_head = head;
31569+ for (u = 0; u < num_hash; u++)
1facf9fc 31570+ INIT_HLIST_HEAD(head++);
dece6358 31571+ return 0; /* success */
1facf9fc 31572+ }
1facf9fc 31573+
dece6358 31574+ return -ENOMEM;
1facf9fc 31575+}
31576+
dece6358
AM
31577+static void nhash_count(struct hlist_head *head)
31578+{
31579+#if 0
31580+ unsigned long n;
31581+ struct hlist_node *pos;
31582+
31583+ n = 0;
31584+ hlist_for_each(pos, head)
31585+ n++;
4a4d8108 31586+ pr_info("%lu\n", n);
dece6358
AM
31587+#endif
31588+}
31589+
31590+static void au_nhash_wh_do_free(struct hlist_head *head)
1facf9fc 31591+{
c06a8ce3
AM
31592+ struct au_vdir_wh *pos;
31593+ struct hlist_node *node;
1facf9fc 31594+
c06a8ce3 31595+ hlist_for_each_entry_safe(pos, node, head, wh_hash)
1c60b727 31596+ kfree(pos);
1facf9fc 31597+}
31598+
dece6358 31599+static void au_nhash_de_do_free(struct hlist_head *head)
1facf9fc 31600+{
c06a8ce3
AM
31601+ struct au_vdir_dehstr *pos;
31602+ struct hlist_node *node;
1facf9fc 31603+
c06a8ce3 31604+ hlist_for_each_entry_safe(pos, node, head, hash)
1c60b727 31605+ au_cache_free_vdir_dehstr(pos);
1facf9fc 31606+}
31607+
dece6358
AM
31608+static void au_nhash_do_free(struct au_nhash *nhash,
31609+ void (*free)(struct hlist_head *head))
1facf9fc 31610+{
1308ab2a 31611+ unsigned int n;
1facf9fc 31612+ struct hlist_head *head;
1facf9fc 31613+
dece6358 31614+ n = nhash->nh_num;
1308ab2a 31615+ if (!n)
31616+ return;
31617+
dece6358 31618+ head = nhash->nh_head;
1308ab2a 31619+ while (n-- > 0) {
dece6358
AM
31620+ nhash_count(head);
31621+ free(head++);
1facf9fc 31622+ }
1c60b727 31623+ kfree(nhash->nh_head);
1facf9fc 31624+}
31625+
dece6358 31626+void au_nhash_wh_free(struct au_nhash *whlist)
1facf9fc 31627+{
dece6358
AM
31628+ au_nhash_do_free(whlist, au_nhash_wh_do_free);
31629+}
1facf9fc 31630+
dece6358
AM
31631+static void au_nhash_de_free(struct au_nhash *delist)
31632+{
31633+ au_nhash_do_free(delist, au_nhash_de_do_free);
1facf9fc 31634+}
31635+
31636+/* ---------------------------------------------------------------------- */
31637+
31638+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
31639+ int limit)
31640+{
31641+ int num;
31642+ unsigned int u, n;
31643+ struct hlist_head *head;
c06a8ce3 31644+ struct au_vdir_wh *pos;
1facf9fc 31645+
31646+ num = 0;
31647+ n = whlist->nh_num;
31648+ head = whlist->nh_head;
1308ab2a 31649+ for (u = 0; u < n; u++, head++)
c06a8ce3
AM
31650+ hlist_for_each_entry(pos, head, wh_hash)
31651+ if (pos->wh_bindex == btgt && ++num > limit)
1facf9fc 31652+ return 1;
1facf9fc 31653+ return 0;
31654+}
31655+
31656+static struct hlist_head *au_name_hash(struct au_nhash *nhash,
dece6358 31657+ unsigned char *name,
1facf9fc 31658+ unsigned int len)
31659+{
dece6358
AM
31660+ unsigned int v;
31661+ /* const unsigned int magic_bit = 12; */
31662+
1308ab2a 31663+ AuDebugOn(!nhash->nh_num || !nhash->nh_head);
31664+
dece6358 31665+ v = 0;
f0c0a007
AM
31666+ if (len > 8)
31667+ len = 8;
dece6358
AM
31668+ while (len--)
31669+ v += *name++;
31670+ /* v = hash_long(v, magic_bit); */
31671+ v %= nhash->nh_num;
31672+ return nhash->nh_head + v;
31673+}
31674+
31675+static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
31676+ int nlen)
31677+{
31678+ return str->len == nlen && !memcmp(str->name, name, nlen);
1facf9fc 31679+}
31680+
31681+/* returns found or not */
dece6358 31682+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
1facf9fc 31683+{
31684+ struct hlist_head *head;
c06a8ce3 31685+ struct au_vdir_wh *pos;
1facf9fc 31686+ struct au_vdir_destr *str;
31687+
dece6358 31688+ head = au_name_hash(whlist, name, nlen);
c06a8ce3
AM
31689+ hlist_for_each_entry(pos, head, wh_hash) {
31690+ str = &pos->wh_str;
1facf9fc 31691+ AuDbg("%.*s\n", str->len, str->name);
dece6358
AM
31692+ if (au_nhash_test_name(str, name, nlen))
31693+ return 1;
31694+ }
31695+ return 0;
31696+}
31697+
31698+/* returns found(true) or not */
31699+static int test_known(struct au_nhash *delist, char *name, int nlen)
31700+{
31701+ struct hlist_head *head;
c06a8ce3 31702+ struct au_vdir_dehstr *pos;
dece6358
AM
31703+ struct au_vdir_destr *str;
31704+
31705+ head = au_name_hash(delist, name, nlen);
c06a8ce3
AM
31706+ hlist_for_each_entry(pos, head, hash) {
31707+ str = pos->str;
dece6358
AM
31708+ AuDbg("%.*s\n", str->len, str->name);
31709+ if (au_nhash_test_name(str, name, nlen))
1facf9fc 31710+ return 1;
31711+ }
31712+ return 0;
31713+}
31714+
dece6358
AM
31715+static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
31716+ unsigned char d_type)
31717+{
31718+#ifdef CONFIG_AUFS_SHWH
31719+ wh->wh_ino = ino;
31720+ wh->wh_type = d_type;
31721+#endif
31722+}
31723+
31724+/* ---------------------------------------------------------------------- */
31725+
31726+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
31727+ unsigned int d_type, aufs_bindex_t bindex,
31728+ unsigned char shwh)
1facf9fc 31729+{
31730+ int err;
31731+ struct au_vdir_destr *str;
31732+ struct au_vdir_wh *wh;
31733+
dece6358 31734+ AuDbg("%.*s\n", nlen, name);
1308ab2a 31735+ AuDebugOn(!whlist->nh_num || !whlist->nh_head);
31736+
1facf9fc 31737+ err = -ENOMEM;
dece6358 31738+ wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
1facf9fc 31739+ if (unlikely(!wh))
31740+ goto out;
31741+
31742+ err = 0;
31743+ wh->wh_bindex = bindex;
dece6358
AM
31744+ if (shwh)
31745+ au_shwh_init_wh(wh, ino, d_type);
1facf9fc 31746+ str = &wh->wh_str;
dece6358
AM
31747+ str->len = nlen;
31748+ memcpy(str->name, name, nlen);
31749+ hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
1facf9fc 31750+ /* smp_mb(); */
31751+
4f0767ce 31752+out:
1facf9fc 31753+ return err;
31754+}
31755+
1facf9fc 31756+static int append_deblk(struct au_vdir *vdir)
31757+{
31758+ int err;
dece6358 31759+ unsigned long ul;
1facf9fc 31760+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
31761+ union au_vdir_deblk_p p, deblk_end;
31762+ unsigned char **o;
31763+
31764+ err = -ENOMEM;
e2f27e51
AM
31765+ o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
31766+ GFP_NOFS, /*may_shrink*/0);
1facf9fc 31767+ if (unlikely(!o))
31768+ goto out;
31769+
31770+ vdir->vd_deblk = o;
31771+ p.deblk = kmalloc(deblk_sz, GFP_NOFS);
31772+ if (p.deblk) {
31773+ ul = vdir->vd_nblk++;
31774+ vdir->vd_deblk[ul] = p.deblk;
31775+ vdir->vd_last.ul = ul;
31776+ vdir->vd_last.p.deblk = p.deblk;
31777+ deblk_end.deblk = p.deblk + deblk_sz;
31778+ err = set_deblk_end(&p, &deblk_end);
31779+ }
31780+
4f0767ce 31781+out:
1facf9fc 31782+ return err;
31783+}
31784+
dece6358
AM
31785+static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
31786+ unsigned int d_type, struct au_nhash *delist)
31787+{
31788+ int err;
31789+ unsigned int sz;
31790+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
31791+ union au_vdir_deblk_p p, *room, deblk_end;
31792+ struct au_vdir_dehstr *dehstr;
31793+
31794+ p.deblk = last_deblk(vdir);
31795+ deblk_end.deblk = p.deblk + deblk_sz;
31796+ room = &vdir->vd_last.p;
31797+ AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
31798+ || !is_deblk_end(room, &deblk_end));
31799+
31800+ sz = calc_size(nlen);
31801+ if (unlikely(sz > deblk_end.deblk - room->deblk)) {
31802+ err = append_deblk(vdir);
31803+ if (unlikely(err))
31804+ goto out;
31805+
31806+ p.deblk = last_deblk(vdir);
31807+ deblk_end.deblk = p.deblk + deblk_sz;
31808+ /* smp_mb(); */
31809+ AuDebugOn(room->deblk != p.deblk);
31810+ }
31811+
31812+ err = -ENOMEM;
4a4d8108 31813+ dehstr = au_cache_alloc_vdir_dehstr();
dece6358
AM
31814+ if (unlikely(!dehstr))
31815+ goto out;
31816+
31817+ dehstr->str = &room->de->de_str;
31818+ hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
31819+ room->de->de_ino = ino;
31820+ room->de->de_type = d_type;
31821+ room->de->de_str.len = nlen;
31822+ memcpy(room->de->de_str.name, name, nlen);
31823+
31824+ err = 0;
31825+ room->deblk += sz;
31826+ if (unlikely(set_deblk_end(room, &deblk_end)))
31827+ err = append_deblk(vdir);
31828+ /* smp_mb(); */
31829+
4f0767ce 31830+out:
dece6358
AM
31831+ return err;
31832+}
31833+
31834+/* ---------------------------------------------------------------------- */
31835+
1c60b727 31836+void au_vdir_free(struct au_vdir *vdir)
dece6358
AM
31837+{
31838+ unsigned char **deblk;
31839+
31840+ deblk = vdir->vd_deblk;
1c60b727
AM
31841+ while (vdir->vd_nblk--)
31842+ kfree(*deblk++);
31843+ kfree(vdir->vd_deblk);
31844+ au_cache_free_vdir(vdir);
dece6358
AM
31845+}
31846+
1308ab2a 31847+static struct au_vdir *alloc_vdir(struct file *file)
1facf9fc 31848+{
31849+ struct au_vdir *vdir;
1308ab2a 31850+ struct super_block *sb;
1facf9fc 31851+ int err;
31852+
2000de60 31853+ sb = file->f_path.dentry->d_sb;
dece6358
AM
31854+ SiMustAnyLock(sb);
31855+
1facf9fc 31856+ err = -ENOMEM;
31857+ vdir = au_cache_alloc_vdir();
31858+ if (unlikely(!vdir))
31859+ goto out;
31860+
31861+ vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
31862+ if (unlikely(!vdir->vd_deblk))
31863+ goto out_free;
31864+
31865+ vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
1308ab2a 31866+ if (!vdir->vd_deblk_sz) {
79b8bda9 31867+ /* estimate the appropriate size for deblk */
1308ab2a 31868+ vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
4a4d8108 31869+ /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
1308ab2a 31870+ }
1facf9fc 31871+ vdir->vd_nblk = 0;
31872+ vdir->vd_version = 0;
31873+ vdir->vd_jiffy = 0;
31874+ err = append_deblk(vdir);
31875+ if (!err)
31876+ return vdir; /* success */
31877+
1c60b727 31878+ kfree(vdir->vd_deblk);
1facf9fc 31879+
4f0767ce 31880+out_free:
1c60b727 31881+ au_cache_free_vdir(vdir);
4f0767ce 31882+out:
1facf9fc 31883+ vdir = ERR_PTR(err);
31884+ return vdir;
31885+}
31886+
31887+static int reinit_vdir(struct au_vdir *vdir)
31888+{
31889+ int err;
31890+ union au_vdir_deblk_p p, deblk_end;
31891+
31892+ while (vdir->vd_nblk > 1) {
1c60b727 31893+ kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
1facf9fc 31894+ /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
31895+ vdir->vd_nblk--;
31896+ }
31897+ p.deblk = vdir->vd_deblk[0];
31898+ deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
31899+ err = set_deblk_end(&p, &deblk_end);
31900+ /* keep vd_dblk_sz */
31901+ vdir->vd_last.ul = 0;
31902+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
31903+ vdir->vd_version = 0;
31904+ vdir->vd_jiffy = 0;
31905+ /* smp_mb(); */
31906+ return err;
31907+}
31908+
31909+/* ---------------------------------------------------------------------- */
31910+
1facf9fc 31911+#define AuFillVdir_CALLED 1
31912+#define AuFillVdir_WHABLE (1 << 1)
dece6358 31913+#define AuFillVdir_SHWH (1 << 2)
1facf9fc 31914+#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
7f207e10
AM
31915+#define au_fset_fillvdir(flags, name) \
31916+ do { (flags) |= AuFillVdir_##name; } while (0)
31917+#define au_fclr_fillvdir(flags, name) \
31918+ do { (flags) &= ~AuFillVdir_##name; } while (0)
1facf9fc 31919+
dece6358
AM
31920+#ifndef CONFIG_AUFS_SHWH
31921+#undef AuFillVdir_SHWH
31922+#define AuFillVdir_SHWH 0
31923+#endif
31924+
1facf9fc 31925+struct fillvdir_arg {
392086de 31926+ struct dir_context ctx;
1facf9fc 31927+ struct file *file;
31928+ struct au_vdir *vdir;
dece6358
AM
31929+ struct au_nhash delist;
31930+ struct au_nhash whlist;
1facf9fc 31931+ aufs_bindex_t bindex;
31932+ unsigned int flags;
31933+ int err;
31934+};
31935+
392086de 31936+static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
1facf9fc 31937+ loff_t offset __maybe_unused, u64 h_ino,
31938+ unsigned int d_type)
31939+{
392086de 31940+ struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
1facf9fc 31941+ char *name = (void *)__name;
31942+ struct super_block *sb;
1facf9fc 31943+ ino_t ino;
dece6358 31944+ const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
1facf9fc 31945+
1facf9fc 31946+ arg->err = 0;
2000de60 31947+ sb = arg->file->f_path.dentry->d_sb;
1facf9fc 31948+ au_fset_fillvdir(arg->flags, CALLED);
31949+ /* smp_mb(); */
dece6358 31950+ if (nlen <= AUFS_WH_PFX_LEN
1facf9fc 31951+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
dece6358
AM
31952+ if (test_known(&arg->delist, name, nlen)
31953+ || au_nhash_test_known_wh(&arg->whlist, name, nlen))
31954+ goto out; /* already exists or whiteouted */
1facf9fc 31955+
dece6358 31956+ arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
4a4d8108
AM
31957+ if (!arg->err) {
31958+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
31959+ d_type = DT_UNKNOWN;
dece6358
AM
31960+ arg->err = append_de(arg->vdir, name, nlen, ino,
31961+ d_type, &arg->delist);
4a4d8108 31962+ }
1facf9fc 31963+ } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
31964+ name += AUFS_WH_PFX_LEN;
dece6358
AM
31965+ nlen -= AUFS_WH_PFX_LEN;
31966+ if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
31967+ goto out; /* already whiteouted */
1facf9fc 31968+
dece6358
AM
31969+ if (shwh)
31970+ arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
31971+ &ino);
4a4d8108
AM
31972+ if (!arg->err) {
31973+ if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
31974+ d_type = DT_UNKNOWN;
1facf9fc 31975+ arg->err = au_nhash_append_wh
dece6358
AM
31976+ (&arg->whlist, name, nlen, ino, d_type,
31977+ arg->bindex, shwh);
4a4d8108 31978+ }
1facf9fc 31979+ }
31980+
4f0767ce 31981+out:
1facf9fc 31982+ if (!arg->err)
31983+ arg->vdir->vd_jiffy = jiffies;
31984+ /* smp_mb(); */
31985+ AuTraceErr(arg->err);
31986+ return arg->err;
31987+}
31988+
dece6358
AM
31989+static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
31990+ struct au_nhash *whlist, struct au_nhash *delist)
31991+{
31992+#ifdef CONFIG_AUFS_SHWH
31993+ int err;
31994+ unsigned int nh, u;
31995+ struct hlist_head *head;
c06a8ce3
AM
31996+ struct au_vdir_wh *pos;
31997+ struct hlist_node *n;
dece6358
AM
31998+ char *p, *o;
31999+ struct au_vdir_destr *destr;
32000+
32001+ AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32002+
32003+ err = -ENOMEM;
537831f9 32004+ o = p = (void *)__get_free_page(GFP_NOFS);
dece6358
AM
32005+ if (unlikely(!p))
32006+ goto out;
32007+
32008+ err = 0;
32009+ nh = whlist->nh_num;
32010+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32011+ p += AUFS_WH_PFX_LEN;
32012+ for (u = 0; u < nh; u++) {
32013+ head = whlist->nh_head + u;
c06a8ce3
AM
32014+ hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32015+ destr = &pos->wh_str;
dece6358
AM
32016+ memcpy(p, destr->name, destr->len);
32017+ err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
c06a8ce3 32018+ pos->wh_ino, pos->wh_type, delist);
dece6358
AM
32019+ if (unlikely(err))
32020+ break;
32021+ }
32022+ }
32023+
1c60b727 32024+ free_page((unsigned long)o);
dece6358 32025+
4f0767ce 32026+out:
dece6358
AM
32027+ AuTraceErr(err);
32028+ return err;
32029+#else
32030+ return 0;
32031+#endif
32032+}
32033+
1facf9fc 32034+static int au_do_read_vdir(struct fillvdir_arg *arg)
32035+{
32036+ int err;
dece6358 32037+ unsigned int rdhash;
1facf9fc 32038+ loff_t offset;
5afbbe0d 32039+ aufs_bindex_t bbot, bindex, btop;
dece6358 32040+ unsigned char shwh;
1facf9fc 32041+ struct file *hf, *file;
32042+ struct super_block *sb;
32043+
1facf9fc 32044+ file = arg->file;
2000de60 32045+ sb = file->f_path.dentry->d_sb;
dece6358
AM
32046+ SiMustAnyLock(sb);
32047+
32048+ rdhash = au_sbi(sb)->si_rdhash;
1308ab2a 32049+ if (!rdhash)
32050+ rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
dece6358
AM
32051+ err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32052+ if (unlikely(err))
1facf9fc 32053+ goto out;
dece6358
AM
32054+ err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32055+ if (unlikely(err))
1facf9fc 32056+ goto out_delist;
32057+
32058+ err = 0;
32059+ arg->flags = 0;
dece6358
AM
32060+ shwh = 0;
32061+ if (au_opt_test(au_mntflags(sb), SHWH)) {
32062+ shwh = 1;
32063+ au_fset_fillvdir(arg->flags, SHWH);
32064+ }
5afbbe0d
AM
32065+ btop = au_fbtop(file);
32066+ bbot = au_fbbot_dir(file);
32067+ for (bindex = btop; !err && bindex <= bbot; bindex++) {
4a4d8108 32068+ hf = au_hf_dir(file, bindex);
1facf9fc 32069+ if (!hf)
32070+ continue;
32071+
32072+ offset = vfsub_llseek(hf, 0, SEEK_SET);
32073+ err = offset;
32074+ if (unlikely(offset))
32075+ break;
32076+
32077+ arg->bindex = bindex;
32078+ au_fclr_fillvdir(arg->flags, WHABLE);
dece6358 32079+ if (shwh
5afbbe0d 32080+ || (bindex != bbot
dece6358 32081+ && au_br_whable(au_sbr_perm(sb, bindex))))
1facf9fc 32082+ au_fset_fillvdir(arg->flags, WHABLE);
32083+ do {
32084+ arg->err = 0;
32085+ au_fclr_fillvdir(arg->flags, CALLED);
32086+ /* smp_mb(); */
392086de 32087+ err = vfsub_iterate_dir(hf, &arg->ctx);
1facf9fc 32088+ if (err >= 0)
32089+ err = arg->err;
32090+ } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
392086de
AM
32091+
32092+ /*
32093+ * dir_relax() may be good for concurrency, but aufs should not
32094+ * use it since it will cause a lockdep problem.
32095+ */
1facf9fc 32096+ }
dece6358
AM
32097+
32098+ if (!err && shwh)
32099+ err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32100+
32101+ au_nhash_wh_free(&arg->whlist);
1facf9fc 32102+
4f0767ce 32103+out_delist:
dece6358 32104+ au_nhash_de_free(&arg->delist);
4f0767ce 32105+out:
1facf9fc 32106+ return err;
32107+}
32108+
32109+static int read_vdir(struct file *file, int may_read)
32110+{
32111+ int err;
32112+ unsigned long expire;
32113+ unsigned char do_read;
392086de
AM
32114+ struct fillvdir_arg arg = {
32115+ .ctx = {
2000de60 32116+ .actor = fillvdir
392086de
AM
32117+ }
32118+ };
1facf9fc 32119+ struct inode *inode;
32120+ struct au_vdir *vdir, *allocated;
32121+
32122+ err = 0;
c06a8ce3 32123+ inode = file_inode(file);
1facf9fc 32124+ IMustLock(inode);
5afbbe0d 32125+ IiMustWriteLock(inode);
dece6358
AM
32126+ SiMustAnyLock(inode->i_sb);
32127+
1facf9fc 32128+ allocated = NULL;
32129+ do_read = 0;
32130+ expire = au_sbi(inode->i_sb)->si_rdcache;
32131+ vdir = au_ivdir(inode);
32132+ if (!vdir) {
32133+ do_read = 1;
1308ab2a 32134+ vdir = alloc_vdir(file);
1facf9fc 32135+ err = PTR_ERR(vdir);
32136+ if (IS_ERR(vdir))
32137+ goto out;
32138+ err = 0;
32139+ allocated = vdir;
32140+ } else if (may_read
32141+ && (inode->i_version != vdir->vd_version
32142+ || time_after(jiffies, vdir->vd_jiffy + expire))) {
32143+ do_read = 1;
32144+ err = reinit_vdir(vdir);
32145+ if (unlikely(err))
32146+ goto out;
32147+ }
32148+
32149+ if (!do_read)
32150+ return 0; /* success */
32151+
32152+ arg.file = file;
32153+ arg.vdir = vdir;
32154+ err = au_do_read_vdir(&arg);
32155+ if (!err) {
392086de 32156+ /* file->f_pos = 0; */ /* todo: ctx->pos? */
1facf9fc 32157+ vdir->vd_version = inode->i_version;
32158+ vdir->vd_last.ul = 0;
32159+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32160+ if (allocated)
32161+ au_set_ivdir(inode, allocated);
32162+ } else if (allocated)
1c60b727 32163+ au_vdir_free(allocated);
1facf9fc 32164+
4f0767ce 32165+out:
1facf9fc 32166+ return err;
32167+}
32168+
32169+static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32170+{
32171+ int err, rerr;
32172+ unsigned long ul, n;
32173+ const unsigned int deblk_sz = src->vd_deblk_sz;
32174+
32175+ AuDebugOn(tgt->vd_nblk != 1);
32176+
32177+ err = -ENOMEM;
32178+ if (tgt->vd_nblk < src->vd_nblk) {
32179+ unsigned char **p;
32180+
e2f27e51
AM
32181+ p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32182+ GFP_NOFS, /*may_shrink*/0);
1facf9fc 32183+ if (unlikely(!p))
32184+ goto out;
32185+ tgt->vd_deblk = p;
32186+ }
32187+
1308ab2a 32188+ if (tgt->vd_deblk_sz != deblk_sz) {
32189+ unsigned char *p;
32190+
32191+ tgt->vd_deblk_sz = deblk_sz;
e2f27e51
AM
32192+ p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32193+ /*may_shrink*/1);
1308ab2a 32194+ if (unlikely(!p))
32195+ goto out;
32196+ tgt->vd_deblk[0] = p;
32197+ }
1facf9fc 32198+ memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
1facf9fc 32199+ tgt->vd_version = src->vd_version;
32200+ tgt->vd_jiffy = src->vd_jiffy;
32201+
32202+ n = src->vd_nblk;
32203+ for (ul = 1; ul < n; ul++) {
dece6358
AM
32204+ tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32205+ GFP_NOFS);
32206+ if (unlikely(!tgt->vd_deblk[ul]))
1facf9fc 32207+ goto out;
1308ab2a 32208+ tgt->vd_nblk++;
1facf9fc 32209+ }
1308ab2a 32210+ tgt->vd_nblk = n;
32211+ tgt->vd_last.ul = tgt->vd_last.ul;
32212+ tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32213+ tgt->vd_last.p.deblk += src->vd_last.p.deblk
32214+ - src->vd_deblk[src->vd_last.ul];
1facf9fc 32215+ /* smp_mb(); */
32216+ return 0; /* success */
32217+
4f0767ce 32218+out:
1facf9fc 32219+ rerr = reinit_vdir(tgt);
32220+ BUG_ON(rerr);
32221+ return err;
32222+}
32223+
32224+int au_vdir_init(struct file *file)
32225+{
32226+ int err;
32227+ struct inode *inode;
32228+ struct au_vdir *vdir_cache, *allocated;
32229+
392086de 32230+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 32231+ err = read_vdir(file, !file->f_pos);
32232+ if (unlikely(err))
32233+ goto out;
32234+
32235+ allocated = NULL;
32236+ vdir_cache = au_fvdir_cache(file);
32237+ if (!vdir_cache) {
1308ab2a 32238+ vdir_cache = alloc_vdir(file);
1facf9fc 32239+ err = PTR_ERR(vdir_cache);
32240+ if (IS_ERR(vdir_cache))
32241+ goto out;
32242+ allocated = vdir_cache;
32243+ } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
392086de 32244+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 32245+ err = reinit_vdir(vdir_cache);
32246+ if (unlikely(err))
32247+ goto out;
32248+ } else
32249+ return 0; /* success */
32250+
c06a8ce3 32251+ inode = file_inode(file);
1facf9fc 32252+ err = copy_vdir(vdir_cache, au_ivdir(inode));
32253+ if (!err) {
32254+ file->f_version = inode->i_version;
32255+ if (allocated)
32256+ au_set_fvdir_cache(file, allocated);
32257+ } else if (allocated)
1c60b727 32258+ au_vdir_free(allocated);
1facf9fc 32259+
4f0767ce 32260+out:
1facf9fc 32261+ return err;
32262+}
32263+
32264+static loff_t calc_offset(struct au_vdir *vdir)
32265+{
32266+ loff_t offset;
32267+ union au_vdir_deblk_p p;
32268+
32269+ p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32270+ offset = vdir->vd_last.p.deblk - p.deblk;
32271+ offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32272+ return offset;
32273+}
32274+
32275+/* returns true or false */
392086de 32276+static int seek_vdir(struct file *file, struct dir_context *ctx)
1facf9fc 32277+{
32278+ int valid;
32279+ unsigned int deblk_sz;
32280+ unsigned long ul, n;
32281+ loff_t offset;
32282+ union au_vdir_deblk_p p, deblk_end;
32283+ struct au_vdir *vdir_cache;
32284+
32285+ valid = 1;
32286+ vdir_cache = au_fvdir_cache(file);
32287+ offset = calc_offset(vdir_cache);
32288+ AuDbg("offset %lld\n", offset);
392086de 32289+ if (ctx->pos == offset)
1facf9fc 32290+ goto out;
32291+
32292+ vdir_cache->vd_last.ul = 0;
32293+ vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
392086de 32294+ if (!ctx->pos)
1facf9fc 32295+ goto out;
32296+
32297+ valid = 0;
32298+ deblk_sz = vdir_cache->vd_deblk_sz;
392086de 32299+ ul = div64_u64(ctx->pos, deblk_sz);
1facf9fc 32300+ AuDbg("ul %lu\n", ul);
32301+ if (ul >= vdir_cache->vd_nblk)
32302+ goto out;
32303+
32304+ n = vdir_cache->vd_nblk;
32305+ for (; ul < n; ul++) {
32306+ p.deblk = vdir_cache->vd_deblk[ul];
32307+ deblk_end.deblk = p.deblk + deblk_sz;
32308+ offset = ul;
32309+ offset *= deblk_sz;
392086de 32310+ while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
1facf9fc 32311+ unsigned int l;
32312+
32313+ l = calc_size(p.de->de_str.len);
32314+ offset += l;
32315+ p.deblk += l;
32316+ }
32317+ if (!is_deblk_end(&p, &deblk_end)) {
32318+ valid = 1;
32319+ vdir_cache->vd_last.ul = ul;
32320+ vdir_cache->vd_last.p = p;
32321+ break;
32322+ }
32323+ }
32324+
4f0767ce 32325+out:
1facf9fc 32326+ /* smp_mb(); */
32327+ AuTraceErr(!valid);
32328+ return valid;
32329+}
32330+
392086de 32331+int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
1facf9fc 32332+{
1facf9fc 32333+ unsigned int l, deblk_sz;
32334+ union au_vdir_deblk_p deblk_end;
32335+ struct au_vdir *vdir_cache;
32336+ struct au_vdir_de *de;
32337+
32338+ vdir_cache = au_fvdir_cache(file);
392086de 32339+ if (!seek_vdir(file, ctx))
1facf9fc 32340+ return 0;
32341+
32342+ deblk_sz = vdir_cache->vd_deblk_sz;
32343+ while (1) {
32344+ deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32345+ deblk_end.deblk += deblk_sz;
32346+ while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32347+ de = vdir_cache->vd_last.p.de;
32348+ AuDbg("%.*s, off%lld, i%lu, dt%d\n",
392086de 32349+ de->de_str.len, de->de_str.name, ctx->pos,
1facf9fc 32350+ (unsigned long)de->de_ino, de->de_type);
392086de
AM
32351+ if (unlikely(!dir_emit(ctx, de->de_str.name,
32352+ de->de_str.len, de->de_ino,
32353+ de->de_type))) {
1facf9fc 32354+ /* todo: ignore the error caused by udba? */
32355+ /* return err; */
32356+ return 0;
32357+ }
32358+
32359+ l = calc_size(de->de_str.len);
32360+ vdir_cache->vd_last.p.deblk += l;
392086de 32361+ ctx->pos += l;
1facf9fc 32362+ }
32363+ if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32364+ vdir_cache->vd_last.ul++;
32365+ vdir_cache->vd_last.p.deblk
32366+ = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
392086de 32367+ ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
1facf9fc 32368+ continue;
32369+ }
32370+ break;
32371+ }
32372+
32373+ /* smp_mb(); */
32374+ return 0;
32375+}
7f207e10
AM
32376diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32377--- /usr/share/empty/fs/aufs/vfsub.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
32378+++ linux/fs/aufs/vfsub.c 2017-11-12 22:24:44.707577830 +0100
32379@@ -0,0 +1,894 @@
1facf9fc 32380+/*
a2654f78 32381+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 32382+ *
32383+ * This program, aufs is free software; you can redistribute it and/or modify
32384+ * it under the terms of the GNU General Public License as published by
32385+ * the Free Software Foundation; either version 2 of the License, or
32386+ * (at your option) any later version.
dece6358
AM
32387+ *
32388+ * This program is distributed in the hope that it will be useful,
32389+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
32390+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32391+ * GNU General Public License for more details.
32392+ *
32393+ * You should have received a copy of the GNU General Public License
523b37e3 32394+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 32395+ */
32396+
32397+/*
32398+ * sub-routines for VFS
32399+ */
32400+
8b6a4947 32401+#include <linux/mnt_namespace.h>
dece6358 32402+#include <linux/namei.h>
8cdd5066 32403+#include <linux/nsproxy.h>
dece6358
AM
32404+#include <linux/security.h>
32405+#include <linux/splice.h>
1facf9fc 32406+#include "aufs.h"
32407+
8cdd5066
JR
32408+#ifdef CONFIG_AUFS_BR_FUSE
32409+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32410+{
8cdd5066
JR
32411+ if (!au_test_fuse(h_sb) || !au_userns)
32412+ return 0;
32413+
8b6a4947 32414+ return is_current_mnt_ns(mnt) ? 0 : -EACCES;
8cdd5066
JR
32415+}
32416+#endif
32417+
a2654f78
AM
32418+int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
32419+{
32420+ int err;
32421+
32422+ lockdep_off();
32423+ down_read(&h_sb->s_umount);
32424+ err = __sync_filesystem(h_sb, wait);
32425+ up_read(&h_sb->s_umount);
32426+ lockdep_on();
32427+
32428+ return err;
32429+}
32430+
8cdd5066
JR
32431+/* ---------------------------------------------------------------------- */
32432+
1facf9fc 32433+int vfsub_update_h_iattr(struct path *h_path, int *did)
32434+{
32435+ int err;
32436+ struct kstat st;
32437+ struct super_block *h_sb;
32438+
32439+ /* for remote fs, leave work for its getattr or d_revalidate */
32440+ /* for bad i_attr fs, handle them in aufs_getattr() */
32441+ /* still some fs may acquire i_mutex. we need to skip them */
32442+ err = 0;
32443+ if (!did)
32444+ did = &err;
32445+ h_sb = h_path->dentry->d_sb;
32446+ *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32447+ if (*did)
521ced18 32448+ err = vfsub_getattr(h_path, &st);
1facf9fc 32449+
32450+ return err;
32451+}
32452+
32453+/* ---------------------------------------------------------------------- */
32454+
4a4d8108 32455+struct file *vfsub_dentry_open(struct path *path, int flags)
1308ab2a 32456+{
32457+ struct file *file;
32458+
b4510431 32459+ file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
7f207e10 32460+ current_cred());
2cbb1c4b
JR
32461+ if (!IS_ERR_OR_NULL(file)
32462+ && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
5527c038 32463+ i_readcount_inc(d_inode(path->dentry));
4a4d8108 32464+
1308ab2a 32465+ return file;
32466+}
32467+
1facf9fc 32468+struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32469+{
32470+ struct file *file;
32471+
2cbb1c4b 32472+ lockdep_off();
7f207e10 32473+ file = filp_open(path,
2cbb1c4b 32474+ oflags /* | __FMODE_NONOTIFY */,
7f207e10 32475+ mode);
2cbb1c4b 32476+ lockdep_on();
1facf9fc 32477+ if (IS_ERR(file))
32478+ goto out;
32479+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32480+
4f0767ce 32481+out:
1facf9fc 32482+ return file;
32483+}
32484+
b912730e
AM
32485+/*
32486+ * Ideally this function should call VFS:do_last() in order to keep all its
32487+ * checkings. But it is very hard for aufs to regenerate several VFS internal
32488+ * structure such as nameidata. This is a second (or third) best approach.
32489+ * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32490+ */
32491+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
32492+ struct vfsub_aopen_args *args, struct au_branch *br)
32493+{
32494+ int err;
32495+ struct file *file = args->file;
32496+ /* copied from linux/fs/namei.c:atomic_open() */
32497+ struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
32498+
32499+ IMustLock(dir);
32500+ AuDebugOn(!dir->i_op->atomic_open);
32501+
32502+ err = au_br_test_oflag(args->open_flag, br);
32503+ if (unlikely(err))
32504+ goto out;
32505+
32506+ args->file->f_path.dentry = DENTRY_NOT_SET;
32507+ args->file->f_path.mnt = au_br_mnt(br);
32508+ err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
32509+ args->create_mode, args->opened);
32510+ if (err >= 0) {
32511+ /* some filesystems don't set FILE_CREATED while succeeded? */
32512+ if (*args->opened & FILE_CREATED)
32513+ fsnotify_create(dir, dentry);
32514+ } else
32515+ goto out;
32516+
32517+
32518+ if (!err) {
32519+ /* todo: call VFS:may_open() here */
32520+ err = open_check_o_direct(file);
32521+ /* todo: ima_file_check() too? */
32522+ if (!err && (args->open_flag & __FMODE_EXEC))
32523+ err = deny_write_access(file);
32524+ if (unlikely(err))
32525+ /* note that the file is created and still opened */
32526+ goto out;
32527+ }
32528+
5afbbe0d 32529+ au_br_get(br);
b912730e
AM
32530+ fsnotify_open(file);
32531+
32532+out:
32533+ return err;
32534+}
32535+
1facf9fc 32536+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
32537+{
32538+ int err;
32539+
1facf9fc 32540+ err = kern_path(name, flags, path);
5527c038 32541+ if (!err && d_is_positive(path->dentry))
1facf9fc 32542+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
32543+ return err;
32544+}
32545+
febd17d6
JR
32546+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
32547+ struct dentry *parent, int len)
32548+{
32549+ struct path path = {
32550+ .mnt = NULL
32551+ };
32552+
32553+ path.dentry = lookup_one_len_unlocked(name, parent, len);
32554+ if (IS_ERR(path.dentry))
32555+ goto out;
32556+ if (d_is_positive(path.dentry))
32557+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32558+
32559+out:
32560+ AuTraceErrPtr(path.dentry);
32561+ return path.dentry;
32562+}
32563+
1facf9fc 32564+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
32565+ int len)
32566+{
32567+ struct path path = {
32568+ .mnt = NULL
32569+ };
32570+
1308ab2a 32571+ /* VFS checks it too, but by WARN_ON_ONCE() */
5527c038 32572+ IMustLock(d_inode(parent));
1facf9fc 32573+
32574+ path.dentry = lookup_one_len(name, parent, len);
32575+ if (IS_ERR(path.dentry))
32576+ goto out;
5527c038 32577+ if (d_is_positive(path.dentry))
1facf9fc 32578+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32579+
4f0767ce 32580+out:
4a4d8108 32581+ AuTraceErrPtr(path.dentry);
1facf9fc 32582+ return path.dentry;
32583+}
32584+
b4510431 32585+void vfsub_call_lkup_one(void *args)
2cbb1c4b 32586+{
b4510431
AM
32587+ struct vfsub_lkup_one_args *a = args;
32588+ *a->errp = vfsub_lkup_one(a->name, a->parent);
2cbb1c4b
JR
32589+}
32590+
1facf9fc 32591+/* ---------------------------------------------------------------------- */
32592+
32593+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
32594+ struct dentry *d2, struct au_hinode *hdir2)
32595+{
32596+ struct dentry *d;
32597+
2cbb1c4b 32598+ lockdep_off();
1facf9fc 32599+ d = lock_rename(d1, d2);
2cbb1c4b 32600+ lockdep_on();
4a4d8108 32601+ au_hn_suspend(hdir1);
1facf9fc 32602+ if (hdir1 != hdir2)
4a4d8108 32603+ au_hn_suspend(hdir2);
1facf9fc 32604+
32605+ return d;
32606+}
32607+
32608+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
32609+ struct dentry *d2, struct au_hinode *hdir2)
32610+{
4a4d8108 32611+ au_hn_resume(hdir1);
1facf9fc 32612+ if (hdir1 != hdir2)
4a4d8108 32613+ au_hn_resume(hdir2);
2cbb1c4b 32614+ lockdep_off();
1facf9fc 32615+ unlock_rename(d1, d2);
2cbb1c4b 32616+ lockdep_on();
1facf9fc 32617+}
32618+
32619+/* ---------------------------------------------------------------------- */
32620+
b4510431 32621+int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
1facf9fc 32622+{
32623+ int err;
32624+ struct dentry *d;
32625+
32626+ IMustLock(dir);
32627+
32628+ d = path->dentry;
32629+ path->dentry = d->d_parent;
b752ccd1 32630+ err = security_path_mknod(path, d, mode, 0);
1facf9fc 32631+ path->dentry = d;
32632+ if (unlikely(err))
32633+ goto out;
32634+
c1595e42 32635+ lockdep_off();
b4510431 32636+ err = vfs_create(dir, path->dentry, mode, want_excl);
c1595e42 32637+ lockdep_on();
1facf9fc 32638+ if (!err) {
32639+ struct path tmp = *path;
32640+ int did;
32641+
32642+ vfsub_update_h_iattr(&tmp, &did);
32643+ if (did) {
32644+ tmp.dentry = path->dentry->d_parent;
32645+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32646+ }
32647+ /*ignore*/
32648+ }
32649+
4f0767ce 32650+out:
1facf9fc 32651+ return err;
32652+}
32653+
32654+int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
32655+{
32656+ int err;
32657+ struct dentry *d;
32658+
32659+ IMustLock(dir);
32660+
32661+ d = path->dentry;
32662+ path->dentry = d->d_parent;
b752ccd1 32663+ err = security_path_symlink(path, d, symname);
1facf9fc 32664+ path->dentry = d;
32665+ if (unlikely(err))
32666+ goto out;
32667+
c1595e42 32668+ lockdep_off();
1facf9fc 32669+ err = vfs_symlink(dir, path->dentry, symname);
c1595e42 32670+ lockdep_on();
1facf9fc 32671+ if (!err) {
32672+ struct path tmp = *path;
32673+ int did;
32674+
32675+ vfsub_update_h_iattr(&tmp, &did);
32676+ if (did) {
32677+ tmp.dentry = path->dentry->d_parent;
32678+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32679+ }
32680+ /*ignore*/
32681+ }
32682+
4f0767ce 32683+out:
1facf9fc 32684+ return err;
32685+}
32686+
32687+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
32688+{
32689+ int err;
32690+ struct dentry *d;
32691+
32692+ IMustLock(dir);
32693+
32694+ d = path->dentry;
32695+ path->dentry = d->d_parent;
027c5e7a 32696+ err = security_path_mknod(path, d, mode, new_encode_dev(dev));
1facf9fc 32697+ path->dentry = d;
32698+ if (unlikely(err))
32699+ goto out;
32700+
c1595e42 32701+ lockdep_off();
1facf9fc 32702+ err = vfs_mknod(dir, path->dentry, mode, dev);
c1595e42 32703+ lockdep_on();
1facf9fc 32704+ if (!err) {
32705+ struct path tmp = *path;
32706+ int did;
32707+
32708+ vfsub_update_h_iattr(&tmp, &did);
32709+ if (did) {
32710+ tmp.dentry = path->dentry->d_parent;
32711+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32712+ }
32713+ /*ignore*/
32714+ }
32715+
4f0767ce 32716+out:
1facf9fc 32717+ return err;
32718+}
32719+
32720+static int au_test_nlink(struct inode *inode)
32721+{
32722+ const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
32723+
32724+ if (!au_test_fs_no_limit_nlink(inode->i_sb)
32725+ || inode->i_nlink < link_max)
32726+ return 0;
32727+ return -EMLINK;
32728+}
32729+
523b37e3
AM
32730+int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
32731+ struct inode **delegated_inode)
1facf9fc 32732+{
32733+ int err;
32734+ struct dentry *d;
32735+
32736+ IMustLock(dir);
32737+
5527c038 32738+ err = au_test_nlink(d_inode(src_dentry));
1facf9fc 32739+ if (unlikely(err))
32740+ return err;
32741+
b4510431 32742+ /* we don't call may_linkat() */
1facf9fc 32743+ d = path->dentry;
32744+ path->dentry = d->d_parent;
b752ccd1 32745+ err = security_path_link(src_dentry, path, d);
1facf9fc 32746+ path->dentry = d;
32747+ if (unlikely(err))
32748+ goto out;
32749+
2cbb1c4b 32750+ lockdep_off();
523b37e3 32751+ err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
2cbb1c4b 32752+ lockdep_on();
1facf9fc 32753+ if (!err) {
32754+ struct path tmp = *path;
32755+ int did;
32756+
32757+ /* fuse has different memory inode for the same inumber */
32758+ vfsub_update_h_iattr(&tmp, &did);
32759+ if (did) {
32760+ tmp.dentry = path->dentry->d_parent;
32761+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32762+ tmp.dentry = src_dentry;
32763+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32764+ }
32765+ /*ignore*/
32766+ }
32767+
4f0767ce 32768+out:
1facf9fc 32769+ return err;
32770+}
32771+
32772+int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
523b37e3 32773+ struct inode *dir, struct path *path,
f2c43d5f 32774+ struct inode **delegated_inode, unsigned int flags)
1facf9fc 32775+{
32776+ int err;
32777+ struct path tmp = {
32778+ .mnt = path->mnt
32779+ };
32780+ struct dentry *d;
32781+
32782+ IMustLock(dir);
32783+ IMustLock(src_dir);
32784+
32785+ d = path->dentry;
32786+ path->dentry = d->d_parent;
32787+ tmp.dentry = src_dentry->d_parent;
38d290e6 32788+ err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
1facf9fc 32789+ path->dentry = d;
32790+ if (unlikely(err))
32791+ goto out;
32792+
2cbb1c4b 32793+ lockdep_off();
523b37e3 32794+ err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
f2c43d5f 32795+ delegated_inode, flags);
2cbb1c4b 32796+ lockdep_on();
1facf9fc 32797+ if (!err) {
32798+ int did;
32799+
32800+ tmp.dentry = d->d_parent;
32801+ vfsub_update_h_iattr(&tmp, &did);
32802+ if (did) {
32803+ tmp.dentry = src_dentry;
32804+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32805+ tmp.dentry = src_dentry->d_parent;
32806+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32807+ }
32808+ /*ignore*/
32809+ }
32810+
4f0767ce 32811+out:
1facf9fc 32812+ return err;
32813+}
32814+
32815+int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
32816+{
32817+ int err;
32818+ struct dentry *d;
32819+
32820+ IMustLock(dir);
32821+
32822+ d = path->dentry;
32823+ path->dentry = d->d_parent;
b752ccd1 32824+ err = security_path_mkdir(path, d, mode);
1facf9fc 32825+ path->dentry = d;
32826+ if (unlikely(err))
32827+ goto out;
32828+
c1595e42 32829+ lockdep_off();
1facf9fc 32830+ err = vfs_mkdir(dir, path->dentry, mode);
c1595e42 32831+ lockdep_on();
1facf9fc 32832+ if (!err) {
32833+ struct path tmp = *path;
32834+ int did;
32835+
32836+ vfsub_update_h_iattr(&tmp, &did);
32837+ if (did) {
32838+ tmp.dentry = path->dentry->d_parent;
32839+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32840+ }
32841+ /*ignore*/
32842+ }
32843+
4f0767ce 32844+out:
1facf9fc 32845+ return err;
32846+}
32847+
32848+int vfsub_rmdir(struct inode *dir, struct path *path)
32849+{
32850+ int err;
32851+ struct dentry *d;
32852+
32853+ IMustLock(dir);
32854+
32855+ d = path->dentry;
32856+ path->dentry = d->d_parent;
b752ccd1 32857+ err = security_path_rmdir(path, d);
1facf9fc 32858+ path->dentry = d;
32859+ if (unlikely(err))
32860+ goto out;
32861+
2cbb1c4b 32862+ lockdep_off();
1facf9fc 32863+ err = vfs_rmdir(dir, path->dentry);
2cbb1c4b 32864+ lockdep_on();
1facf9fc 32865+ if (!err) {
32866+ struct path tmp = {
32867+ .dentry = path->dentry->d_parent,
32868+ .mnt = path->mnt
32869+ };
32870+
32871+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
32872+ }
32873+
4f0767ce 32874+out:
1facf9fc 32875+ return err;
32876+}
32877+
32878+/* ---------------------------------------------------------------------- */
32879+
9dbd164d 32880+/* todo: support mmap_sem? */
1facf9fc 32881+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
32882+ loff_t *ppos)
32883+{
32884+ ssize_t err;
32885+
2cbb1c4b 32886+ lockdep_off();
1facf9fc 32887+ err = vfs_read(file, ubuf, count, ppos);
2cbb1c4b 32888+ lockdep_on();
1facf9fc 32889+ if (err >= 0)
32890+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32891+ return err;
32892+}
32893+
32894+/* todo: kernel_read()? */
32895+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
32896+ loff_t *ppos)
32897+{
32898+ ssize_t err;
32899+ mm_segment_t oldfs;
b752ccd1
AM
32900+ union {
32901+ void *k;
32902+ char __user *u;
32903+ } buf;
1facf9fc 32904+
b752ccd1 32905+ buf.k = kbuf;
1facf9fc 32906+ oldfs = get_fs();
32907+ set_fs(KERNEL_DS);
b752ccd1 32908+ err = vfsub_read_u(file, buf.u, count, ppos);
1facf9fc 32909+ set_fs(oldfs);
32910+ return err;
32911+}
32912+
32913+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
32914+ loff_t *ppos)
32915+{
32916+ ssize_t err;
32917+
2cbb1c4b 32918+ lockdep_off();
1facf9fc 32919+ err = vfs_write(file, ubuf, count, ppos);
2cbb1c4b 32920+ lockdep_on();
1facf9fc 32921+ if (err >= 0)
32922+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32923+ return err;
32924+}
32925+
32926+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
32927+{
32928+ ssize_t err;
32929+ mm_segment_t oldfs;
b752ccd1
AM
32930+ union {
32931+ void *k;
32932+ const char __user *u;
32933+ } buf;
1facf9fc 32934+
b752ccd1 32935+ buf.k = kbuf;
1facf9fc 32936+ oldfs = get_fs();
32937+ set_fs(KERNEL_DS);
b752ccd1 32938+ err = vfsub_write_u(file, buf.u, count, ppos);
1facf9fc 32939+ set_fs(oldfs);
32940+ return err;
32941+}
32942+
4a4d8108
AM
32943+int vfsub_flush(struct file *file, fl_owner_t id)
32944+{
32945+ int err;
32946+
32947+ err = 0;
523b37e3 32948+ if (file->f_op->flush) {
2000de60 32949+ if (!au_test_nfs(file->f_path.dentry->d_sb))
2cbb1c4b
JR
32950+ err = file->f_op->flush(file, id);
32951+ else {
32952+ lockdep_off();
32953+ err = file->f_op->flush(file, id);
32954+ lockdep_on();
32955+ }
4a4d8108
AM
32956+ if (!err)
32957+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
32958+ /*ignore*/
32959+ }
32960+ return err;
32961+}
32962+
392086de 32963+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
1facf9fc 32964+{
32965+ int err;
32966+
523b37e3 32967+ AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
392086de 32968+
2cbb1c4b 32969+ lockdep_off();
392086de 32970+ err = iterate_dir(file, ctx);
2cbb1c4b 32971+ lockdep_on();
1facf9fc 32972+ if (err >= 0)
32973+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
1c60b727 32974+
1facf9fc 32975+ return err;
32976+}
32977+
32978+long vfsub_splice_to(struct file *in, loff_t *ppos,
32979+ struct pipe_inode_info *pipe, size_t len,
32980+ unsigned int flags)
32981+{
32982+ long err;
32983+
2cbb1c4b 32984+ lockdep_off();
0fc653ad 32985+ err = do_splice_to(in, ppos, pipe, len, flags);
2cbb1c4b 32986+ lockdep_on();
4a4d8108 32987+ file_accessed(in);
1facf9fc 32988+ if (err >= 0)
32989+ vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
32990+ return err;
32991+}
32992+
32993+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
32994+ loff_t *ppos, size_t len, unsigned int flags)
32995+{
32996+ long err;
32997+
2cbb1c4b 32998+ lockdep_off();
0fc653ad 32999+ err = do_splice_from(pipe, out, ppos, len, flags);
2cbb1c4b 33000+ lockdep_on();
1facf9fc 33001+ if (err >= 0)
33002+ vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33003+ return err;
33004+}
33005+
53392da6
AM
33006+int vfsub_fsync(struct file *file, struct path *path, int datasync)
33007+{
33008+ int err;
33009+
33010+ /* file can be NULL */
33011+ lockdep_off();
33012+ err = vfs_fsync(file, datasync);
33013+ lockdep_on();
33014+ if (!err) {
33015+ if (!path) {
33016+ AuDebugOn(!file);
33017+ path = &file->f_path;
33018+ }
33019+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33020+ }
33021+ return err;
33022+}
33023+
1facf9fc 33024+/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33025+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33026+ struct file *h_file)
33027+{
33028+ int err;
33029+ struct inode *h_inode;
c06a8ce3 33030+ struct super_block *h_sb;
1facf9fc 33031+
1facf9fc 33032+ if (!h_file) {
c06a8ce3
AM
33033+ err = vfsub_truncate(h_path, length);
33034+ goto out;
1facf9fc 33035+ }
33036+
5527c038 33037+ h_inode = d_inode(h_path->dentry);
c06a8ce3
AM
33038+ h_sb = h_inode->i_sb;
33039+ lockdep_off();
33040+ sb_start_write(h_sb);
33041+ lockdep_on();
1facf9fc 33042+ err = locks_verify_truncate(h_inode, h_file, length);
33043+ if (!err)
953406b4 33044+ err = security_path_truncate(h_path);
2cbb1c4b
JR
33045+ if (!err) {
33046+ lockdep_off();
1facf9fc 33047+ err = do_truncate(h_path->dentry, length, attr, h_file);
2cbb1c4b
JR
33048+ lockdep_on();
33049+ }
c06a8ce3
AM
33050+ lockdep_off();
33051+ sb_end_write(h_sb);
33052+ lockdep_on();
1facf9fc 33053+
4f0767ce 33054+out:
1facf9fc 33055+ return err;
33056+}
33057+
33058+/* ---------------------------------------------------------------------- */
33059+
33060+struct au_vfsub_mkdir_args {
33061+ int *errp;
33062+ struct inode *dir;
33063+ struct path *path;
33064+ int mode;
33065+};
33066+
33067+static void au_call_vfsub_mkdir(void *args)
33068+{
33069+ struct au_vfsub_mkdir_args *a = args;
33070+ *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33071+}
33072+
33073+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33074+{
33075+ int err, do_sio, wkq_err;
33076+
33077+ do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
33078+ if (!do_sio) {
33079+ lockdep_off();
1facf9fc 33080+ err = vfsub_mkdir(dir, path, mode);
c1595e42
JR
33081+ lockdep_on();
33082+ } else {
1facf9fc 33083+ struct au_vfsub_mkdir_args args = {
33084+ .errp = &err,
33085+ .dir = dir,
33086+ .path = path,
33087+ .mode = mode
33088+ };
33089+ wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33090+ if (unlikely(wkq_err))
33091+ err = wkq_err;
33092+ }
33093+
33094+ return err;
33095+}
33096+
33097+struct au_vfsub_rmdir_args {
33098+ int *errp;
33099+ struct inode *dir;
33100+ struct path *path;
33101+};
33102+
33103+static void au_call_vfsub_rmdir(void *args)
33104+{
33105+ struct au_vfsub_rmdir_args *a = args;
33106+ *a->errp = vfsub_rmdir(a->dir, a->path);
33107+}
33108+
33109+int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33110+{
33111+ int err, do_sio, wkq_err;
33112+
33113+ do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
33114+ if (!do_sio) {
33115+ lockdep_off();
1facf9fc 33116+ err = vfsub_rmdir(dir, path);
c1595e42
JR
33117+ lockdep_on();
33118+ } else {
1facf9fc 33119+ struct au_vfsub_rmdir_args args = {
33120+ .errp = &err,
33121+ .dir = dir,
33122+ .path = path
33123+ };
33124+ wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33125+ if (unlikely(wkq_err))
33126+ err = wkq_err;
33127+ }
33128+
33129+ return err;
33130+}
33131+
33132+/* ---------------------------------------------------------------------- */
33133+
33134+struct notify_change_args {
33135+ int *errp;
33136+ struct path *path;
33137+ struct iattr *ia;
523b37e3 33138+ struct inode **delegated_inode;
1facf9fc 33139+};
33140+
33141+static void call_notify_change(void *args)
33142+{
33143+ struct notify_change_args *a = args;
33144+ struct inode *h_inode;
33145+
5527c038 33146+ h_inode = d_inode(a->path->dentry);
1facf9fc 33147+ IMustLock(h_inode);
33148+
33149+ *a->errp = -EPERM;
33150+ if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
c1595e42 33151+ lockdep_off();
523b37e3
AM
33152+ *a->errp = notify_change(a->path->dentry, a->ia,
33153+ a->delegated_inode);
c1595e42 33154+ lockdep_on();
1facf9fc 33155+ if (!*a->errp)
33156+ vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33157+ }
33158+ AuTraceErr(*a->errp);
33159+}
33160+
523b37e3
AM
33161+int vfsub_notify_change(struct path *path, struct iattr *ia,
33162+ struct inode **delegated_inode)
1facf9fc 33163+{
33164+ int err;
33165+ struct notify_change_args args = {
523b37e3
AM
33166+ .errp = &err,
33167+ .path = path,
33168+ .ia = ia,
33169+ .delegated_inode = delegated_inode
1facf9fc 33170+ };
33171+
33172+ call_notify_change(&args);
33173+
33174+ return err;
33175+}
33176+
523b37e3
AM
33177+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33178+ struct inode **delegated_inode)
1facf9fc 33179+{
33180+ int err, wkq_err;
33181+ struct notify_change_args args = {
523b37e3
AM
33182+ .errp = &err,
33183+ .path = path,
33184+ .ia = ia,
33185+ .delegated_inode = delegated_inode
1facf9fc 33186+ };
33187+
33188+ wkq_err = au_wkq_wait(call_notify_change, &args);
33189+ if (unlikely(wkq_err))
33190+ err = wkq_err;
33191+
33192+ return err;
33193+}
33194+
33195+/* ---------------------------------------------------------------------- */
33196+
33197+struct unlink_args {
33198+ int *errp;
33199+ struct inode *dir;
33200+ struct path *path;
523b37e3 33201+ struct inode **delegated_inode;
1facf9fc 33202+};
33203+
33204+static void call_unlink(void *args)
33205+{
33206+ struct unlink_args *a = args;
33207+ struct dentry *d = a->path->dentry;
33208+ struct inode *h_inode;
33209+ const int stop_sillyrename = (au_test_nfs(d->d_sb)
c1595e42 33210+ && au_dcount(d) == 1);
1facf9fc 33211+
33212+ IMustLock(a->dir);
33213+
33214+ a->path->dentry = d->d_parent;
33215+ *a->errp = security_path_unlink(a->path, d);
33216+ a->path->dentry = d;
33217+ if (unlikely(*a->errp))
33218+ return;
33219+
33220+ if (!stop_sillyrename)
33221+ dget(d);
5527c038
JR
33222+ h_inode = NULL;
33223+ if (d_is_positive(d)) {
33224+ h_inode = d_inode(d);
027c5e7a 33225+ ihold(h_inode);
5527c038 33226+ }
1facf9fc 33227+
2cbb1c4b 33228+ lockdep_off();
523b37e3 33229+ *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
2cbb1c4b 33230+ lockdep_on();
1facf9fc 33231+ if (!*a->errp) {
33232+ struct path tmp = {
33233+ .dentry = d->d_parent,
33234+ .mnt = a->path->mnt
33235+ };
33236+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33237+ }
33238+
33239+ if (!stop_sillyrename)
33240+ dput(d);
33241+ if (h_inode)
33242+ iput(h_inode);
33243+
33244+ AuTraceErr(*a->errp);
33245+}
33246+
33247+/*
33248+ * @dir: must be locked.
33249+ * @dentry: target dentry.
33250+ */
523b37e3
AM
33251+int vfsub_unlink(struct inode *dir, struct path *path,
33252+ struct inode **delegated_inode, int force)
1facf9fc 33253+{
33254+ int err;
33255+ struct unlink_args args = {
523b37e3
AM
33256+ .errp = &err,
33257+ .dir = dir,
33258+ .path = path,
33259+ .delegated_inode = delegated_inode
1facf9fc 33260+ };
33261+
33262+ if (!force)
33263+ call_unlink(&args);
33264+ else {
33265+ int wkq_err;
33266+
33267+ wkq_err = au_wkq_wait(call_unlink, &args);
33268+ if (unlikely(wkq_err))
33269+ err = wkq_err;
33270+ }
33271+
33272+ return err;
33273+}
7f207e10
AM
33274diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33275--- /usr/share/empty/fs/aufs/vfsub.h 1970-01-01 01:00:00.000000000 +0100
8b6a4947 33276+++ linux/fs/aufs/vfsub.h 2017-11-12 22:24:44.707577830 +0100
3c1bdaff 33277@@ -0,0 +1,360 @@
1facf9fc 33278+/*
a2654f78 33279+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 33280+ *
33281+ * This program, aufs is free software; you can redistribute it and/or modify
33282+ * it under the terms of the GNU General Public License as published by
33283+ * the Free Software Foundation; either version 2 of the License, or
33284+ * (at your option) any later version.
dece6358
AM
33285+ *
33286+ * This program is distributed in the hope that it will be useful,
33287+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33288+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33289+ * GNU General Public License for more details.
33290+ *
33291+ * You should have received a copy of the GNU General Public License
523b37e3 33292+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 33293+ */
33294+
33295+/*
33296+ * sub-routines for VFS
33297+ */
33298+
33299+#ifndef __AUFS_VFSUB_H__
33300+#define __AUFS_VFSUB_H__
33301+
33302+#ifdef __KERNEL__
33303+
33304+#include <linux/fs.h>
b4510431 33305+#include <linux/mount.h>
8cdd5066 33306+#include <linux/posix_acl.h>
c1595e42 33307+#include <linux/xattr.h>
7f207e10 33308+#include "debug.h"
1facf9fc 33309+
7f207e10 33310+/* copied from linux/fs/internal.h */
2cbb1c4b 33311+/* todo: BAD approach!! */
c06a8ce3 33312+extern void __mnt_drop_write(struct vfsmount *);
b912730e 33313+extern int open_check_o_direct(struct file *f);
7f207e10
AM
33314+
33315+/* ---------------------------------------------------------------------- */
1facf9fc 33316+
33317+/* lock subclass for lower inode */
33318+/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33319+/* reduce? gave up. */
33320+enum {
c1595e42 33321+ AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
1facf9fc 33322+ AuLsc_I_PARENT, /* lower inode, parent first */
33323+ AuLsc_I_PARENT2, /* copyup dirs */
dece6358 33324+ AuLsc_I_PARENT3, /* copyup wh */
1facf9fc 33325+ AuLsc_I_CHILD,
33326+ AuLsc_I_CHILD2,
33327+ AuLsc_I_End
33328+};
33329+
33330+/* to debug easier, do not make them inlined functions */
33331+#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
febd17d6 33332+#define IMustLock(i) AuDebugOn(!inode_is_locked(i))
1facf9fc 33333+
3c1bdaff
AM
33334+/* why VFS doesn't define it? */
33335+static inline
33336+void vfsub_inode_lock_shared_nested(struct inode *inode, unsigned int sc)
33337+{
33338+ down_read_nested(&inode->i_rwsem, sc);
33339+}
33340+
1facf9fc 33341+/* ---------------------------------------------------------------------- */
33342+
7f207e10
AM
33343+static inline void vfsub_drop_nlink(struct inode *inode)
33344+{
33345+ AuDebugOn(!inode->i_nlink);
33346+ drop_nlink(inode);
33347+}
33348+
027c5e7a
AM
33349+static inline void vfsub_dead_dir(struct inode *inode)
33350+{
33351+ AuDebugOn(!S_ISDIR(inode->i_mode));
33352+ inode->i_flags |= S_DEAD;
33353+ clear_nlink(inode);
33354+}
33355+
392086de
AM
33356+static inline int vfsub_native_ro(struct inode *inode)
33357+{
8b6a4947 33358+ return sb_rdonly(inode->i_sb)
392086de
AM
33359+ || IS_RDONLY(inode)
33360+ /* || IS_APPEND(inode) */
33361+ || IS_IMMUTABLE(inode);
33362+}
33363+
8cdd5066
JR
33364+#ifdef CONFIG_AUFS_BR_FUSE
33365+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33366+#else
33367+AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33368+#endif
33369+
a2654f78
AM
33370+int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
33371+
7f207e10
AM
33372+/* ---------------------------------------------------------------------- */
33373+
33374+int vfsub_update_h_iattr(struct path *h_path, int *did);
33375+struct file *vfsub_dentry_open(struct path *path, int flags);
33376+struct file *vfsub_filp_open(const char *path, int oflags, int mode);
b912730e
AM
33377+struct vfsub_aopen_args {
33378+ struct file *file;
33379+ unsigned int open_flag;
33380+ umode_t create_mode;
33381+ int *opened;
33382+};
33383+struct au_branch;
33384+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33385+ struct vfsub_aopen_args *args, struct au_branch *br);
1facf9fc 33386+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
b4510431 33387+
febd17d6
JR
33388+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33389+ struct dentry *parent, int len);
1facf9fc 33390+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
33391+ int len);
b4510431
AM
33392+
33393+struct vfsub_lkup_one_args {
33394+ struct dentry **errp;
33395+ struct qstr *name;
33396+ struct dentry *parent;
33397+};
33398+
33399+static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33400+ struct dentry *parent)
33401+{
33402+ return vfsub_lookup_one_len(name->name, parent, name->len);
33403+}
33404+
33405+void vfsub_call_lkup_one(void *args);
33406+
33407+/* ---------------------------------------------------------------------- */
33408+
33409+static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33410+{
33411+ int err;
076b876e 33412+
b4510431
AM
33413+ lockdep_off();
33414+ err = mnt_want_write(mnt);
33415+ lockdep_on();
33416+ return err;
33417+}
33418+
33419+static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33420+{
33421+ lockdep_off();
33422+ mnt_drop_write(mnt);
33423+ lockdep_on();
33424+}
1facf9fc 33425+
7e9cd9fe 33426+#if 0 /* reserved */
c06a8ce3
AM
33427+static inline void vfsub_mnt_drop_write_file(struct file *file)
33428+{
33429+ lockdep_off();
33430+ mnt_drop_write_file(file);
33431+ lockdep_on();
33432+}
7e9cd9fe 33433+#endif
c06a8ce3 33434+
1facf9fc 33435+/* ---------------------------------------------------------------------- */
33436+
33437+struct au_hinode;
33438+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33439+ struct dentry *d2, struct au_hinode *hdir2);
33440+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33441+ struct dentry *d2, struct au_hinode *hdir2);
33442+
537831f9
AM
33443+int vfsub_create(struct inode *dir, struct path *path, int mode,
33444+ bool want_excl);
1facf9fc 33445+int vfsub_symlink(struct inode *dir, struct path *path,
33446+ const char *symname);
33447+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33448+int vfsub_link(struct dentry *src_dentry, struct inode *dir,
523b37e3 33449+ struct path *path, struct inode **delegated_inode);
1facf9fc 33450+int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
523b37e3 33451+ struct inode *hdir, struct path *path,
f2c43d5f 33452+ struct inode **delegated_inode, unsigned int flags);
1facf9fc 33453+int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33454+int vfsub_rmdir(struct inode *dir, struct path *path);
33455+
33456+/* ---------------------------------------------------------------------- */
33457+
33458+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33459+ loff_t *ppos);
33460+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33461+ loff_t *ppos);
33462+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33463+ loff_t *ppos);
33464+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33465+ loff_t *ppos);
4a4d8108 33466+int vfsub_flush(struct file *file, fl_owner_t id);
392086de
AM
33467+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33468+
c06a8ce3
AM
33469+static inline loff_t vfsub_f_size_read(struct file *file)
33470+{
33471+ return i_size_read(file_inode(file));
33472+}
33473+
4a4d8108
AM
33474+static inline unsigned int vfsub_file_flags(struct file *file)
33475+{
33476+ unsigned int flags;
33477+
33478+ spin_lock(&file->f_lock);
33479+ flags = file->f_flags;
33480+ spin_unlock(&file->f_lock);
33481+
33482+ return flags;
33483+}
1308ab2a 33484+
f0c0a007
AM
33485+static inline int vfsub_file_execed(struct file *file)
33486+{
33487+ /* todo: direct access f_flags */
33488+ return !!(vfsub_file_flags(file) & __FMODE_EXEC);
33489+}
33490+
7e9cd9fe 33491+#if 0 /* reserved */
1facf9fc 33492+static inline void vfsub_file_accessed(struct file *h_file)
33493+{
33494+ file_accessed(h_file);
33495+ vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
33496+}
7e9cd9fe 33497+#endif
1facf9fc 33498+
79b8bda9 33499+#if 0 /* reserved */
1facf9fc 33500+static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
33501+ struct dentry *h_dentry)
33502+{
33503+ struct path h_path = {
33504+ .dentry = h_dentry,
33505+ .mnt = h_mnt
33506+ };
92d182d2 33507+ touch_atime(&h_path);
1facf9fc 33508+ vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
33509+}
79b8bda9 33510+#endif
1facf9fc 33511+
0c3ec466
AM
33512+static inline int vfsub_update_time(struct inode *h_inode, struct timespec *ts,
33513+ int flags)
33514+{
5afbbe0d 33515+ return update_time(h_inode, ts, flags);
0c3ec466
AM
33516+ /* no vfsub_update_h_iattr() since we don't have struct path */
33517+}
33518+
8cdd5066
JR
33519+#ifdef CONFIG_FS_POSIX_ACL
33520+static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
33521+{
33522+ int err;
33523+
33524+ err = posix_acl_chmod(h_inode, h_mode);
33525+ if (err == -EOPNOTSUPP)
33526+ err = 0;
33527+ return err;
33528+}
33529+#else
33530+AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
33531+#endif
33532+
4a4d8108
AM
33533+long vfsub_splice_to(struct file *in, loff_t *ppos,
33534+ struct pipe_inode_info *pipe, size_t len,
33535+ unsigned int flags);
33536+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33537+ loff_t *ppos, size_t len, unsigned int flags);
c06a8ce3
AM
33538+
33539+static inline long vfsub_truncate(struct path *path, loff_t length)
33540+{
33541+ long err;
076b876e 33542+
c06a8ce3
AM
33543+ lockdep_off();
33544+ err = vfs_truncate(path, length);
33545+ lockdep_on();
33546+ return err;
33547+}
33548+
4a4d8108
AM
33549+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33550+ struct file *h_file);
53392da6 33551+int vfsub_fsync(struct file *file, struct path *path, int datasync);
4a4d8108 33552+
521ced18
JR
33553+/*
33554+ * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
33555+ * ioctl.
33556+ */
33557+static inline int vfsub_clone_file_range(struct file *src, struct file *dst,
33558+ u64 len)
33559+{
33560+ int err;
33561+
33562+ lockdep_off();
33563+ err = vfs_clone_file_range(src, 0, dst, 0, len);
33564+ lockdep_on();
33565+
33566+ return err;
33567+}
33568+
33569+/* copy_file_range(2) is a systemcall */
33570+static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
33571+ struct file *dst, loff_t dst_pos,
33572+ size_t len, unsigned int flags)
33573+{
33574+ ssize_t ssz;
33575+
33576+ lockdep_off();
33577+ ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
33578+ lockdep_on();
33579+
33580+ return ssz;
33581+}
33582+
1facf9fc 33583+/* ---------------------------------------------------------------------- */
33584+
33585+static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
33586+{
33587+ loff_t err;
33588+
2cbb1c4b 33589+ lockdep_off();
1facf9fc 33590+ err = vfs_llseek(file, offset, origin);
2cbb1c4b 33591+ lockdep_on();
1facf9fc 33592+ return err;
33593+}
33594+
33595+/* ---------------------------------------------------------------------- */
33596+
4a4d8108
AM
33597+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
33598+int vfsub_sio_rmdir(struct inode *dir, struct path *path);
523b37e3
AM
33599+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33600+ struct inode **delegated_inode);
33601+int vfsub_notify_change(struct path *path, struct iattr *ia,
33602+ struct inode **delegated_inode);
33603+int vfsub_unlink(struct inode *dir, struct path *path,
33604+ struct inode **delegated_inode, int force);
4a4d8108 33605+
521ced18
JR
33606+static inline int vfsub_getattr(const struct path *path, struct kstat *st)
33607+{
33608+ return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
33609+}
33610+
c1595e42
JR
33611+/* ---------------------------------------------------------------------- */
33612+
33613+static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
33614+ const void *value, size_t size, int flags)
33615+{
33616+ int err;
33617+
33618+ lockdep_off();
33619+ err = vfs_setxattr(dentry, name, value, size, flags);
33620+ lockdep_on();
33621+
33622+ return err;
33623+}
33624+
33625+static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
33626+{
33627+ int err;
33628+
33629+ lockdep_off();
33630+ err = vfs_removexattr(dentry, name);
33631+ lockdep_on();
33632+
33633+ return err;
33634+}
33635+
1facf9fc 33636+#endif /* __KERNEL__ */
33637+#endif /* __AUFS_VFSUB_H__ */
7f207e10
AM
33638diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
33639--- /usr/share/empty/fs/aufs/wbr_policy.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947 33640+++ linux/fs/aufs/wbr_policy.c 2017-11-12 22:24:42.274176929 +0100
f2c43d5f 33641@@ -0,0 +1,830 @@
1facf9fc 33642+/*
a2654f78 33643+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 33644+ *
33645+ * This program, aufs is free software; you can redistribute it and/or modify
33646+ * it under the terms of the GNU General Public License as published by
33647+ * the Free Software Foundation; either version 2 of the License, or
33648+ * (at your option) any later version.
dece6358
AM
33649+ *
33650+ * This program is distributed in the hope that it will be useful,
33651+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33652+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33653+ * GNU General Public License for more details.
33654+ *
33655+ * You should have received a copy of the GNU General Public License
523b37e3 33656+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 33657+ */
33658+
33659+/*
33660+ * policies for selecting one among multiple writable branches
33661+ */
33662+
33663+#include <linux/statfs.h>
33664+#include "aufs.h"
33665+
33666+/* subset of cpup_attr() */
33667+static noinline_for_stack
33668+int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
33669+{
33670+ int err, sbits;
33671+ struct iattr ia;
33672+ struct inode *h_isrc;
33673+
5527c038 33674+ h_isrc = d_inode(h_src);
1facf9fc 33675+ ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
33676+ ia.ia_mode = h_isrc->i_mode;
33677+ ia.ia_uid = h_isrc->i_uid;
33678+ ia.ia_gid = h_isrc->i_gid;
33679+ sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
5527c038 33680+ au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
523b37e3
AM
33681+ /* no delegation since it is just created */
33682+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 33683+
33684+ /* is this nfs only? */
33685+ if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
33686+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
33687+ ia.ia_mode = h_isrc->i_mode;
523b37e3 33688+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 33689+ }
33690+
33691+ return err;
33692+}
33693+
33694+#define AuCpdown_PARENT_OPQ 1
33695+#define AuCpdown_WHED (1 << 1)
33696+#define AuCpdown_MADE_DIR (1 << 2)
33697+#define AuCpdown_DIROPQ (1 << 3)
33698+#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
7f207e10
AM
33699+#define au_fset_cpdown(flags, name) \
33700+ do { (flags) |= AuCpdown_##name; } while (0)
33701+#define au_fclr_cpdown(flags, name) \
33702+ do { (flags) &= ~AuCpdown_##name; } while (0)
1facf9fc 33703+
1facf9fc 33704+static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
c2b27bf2 33705+ unsigned int *flags)
1facf9fc 33706+{
33707+ int err;
33708+ struct dentry *opq_dentry;
33709+
33710+ opq_dentry = au_diropq_create(dentry, bdst);
33711+ err = PTR_ERR(opq_dentry);
33712+ if (IS_ERR(opq_dentry))
33713+ goto out;
33714+ dput(opq_dentry);
c2b27bf2 33715+ au_fset_cpdown(*flags, DIROPQ);
1facf9fc 33716+
4f0767ce 33717+out:
1facf9fc 33718+ return err;
33719+}
33720+
33721+static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
33722+ struct inode *dir, aufs_bindex_t bdst)
33723+{
33724+ int err;
33725+ struct path h_path;
33726+ struct au_branch *br;
33727+
33728+ br = au_sbr(dentry->d_sb, bdst);
33729+ h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
33730+ err = PTR_ERR(h_path.dentry);
33731+ if (IS_ERR(h_path.dentry))
33732+ goto out;
33733+
33734+ err = 0;
5527c038 33735+ if (d_is_positive(h_path.dentry)) {
86dc4139 33736+ h_path.mnt = au_br_mnt(br);
1facf9fc 33737+ err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
33738+ dentry);
33739+ }
33740+ dput(h_path.dentry);
33741+
4f0767ce 33742+out:
1facf9fc 33743+ return err;
33744+}
33745+
33746+static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 33747+ struct au_pin *pin,
1facf9fc 33748+ struct dentry *h_parent, void *arg)
33749+{
33750+ int err, rerr;
5afbbe0d 33751+ aufs_bindex_t bopq, btop;
1facf9fc 33752+ struct path h_path;
33753+ struct dentry *parent;
33754+ struct inode *h_dir, *h_inode, *inode, *dir;
c2b27bf2 33755+ unsigned int *flags = arg;
1facf9fc 33756+
5afbbe0d 33757+ btop = au_dbtop(dentry);
1facf9fc 33758+ /* dentry is di-locked */
33759+ parent = dget_parent(dentry);
5527c038
JR
33760+ dir = d_inode(parent);
33761+ h_dir = d_inode(h_parent);
1facf9fc 33762+ AuDebugOn(h_dir != au_h_iptr(dir, bdst));
33763+ IMustLock(h_dir);
33764+
86dc4139 33765+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
1facf9fc 33766+ if (unlikely(err < 0))
33767+ goto out;
33768+ h_path.dentry = au_h_dptr(dentry, bdst);
33769+ h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
33770+ err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path,
33771+ S_IRWXU | S_IRUGO | S_IXUGO);
33772+ if (unlikely(err))
33773+ goto out_put;
c2b27bf2 33774+ au_fset_cpdown(*flags, MADE_DIR);
1facf9fc 33775+
1facf9fc 33776+ bopq = au_dbdiropq(dentry);
c2b27bf2
AM
33777+ au_fclr_cpdown(*flags, WHED);
33778+ au_fclr_cpdown(*flags, DIROPQ);
1facf9fc 33779+ if (au_dbwh(dentry) == bdst)
c2b27bf2
AM
33780+ au_fset_cpdown(*flags, WHED);
33781+ if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
33782+ au_fset_cpdown(*flags, PARENT_OPQ);
5527c038 33783+ h_inode = d_inode(h_path.dentry);
febd17d6 33784+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
c2b27bf2
AM
33785+ if (au_ftest_cpdown(*flags, WHED)) {
33786+ err = au_cpdown_dir_opq(dentry, bdst, flags);
1facf9fc 33787+ if (unlikely(err)) {
febd17d6 33788+ inode_unlock(h_inode);
1facf9fc 33789+ goto out_dir;
33790+ }
33791+ }
33792+
5afbbe0d 33793+ err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
febd17d6 33794+ inode_unlock(h_inode);
1facf9fc 33795+ if (unlikely(err))
33796+ goto out_opq;
33797+
c2b27bf2 33798+ if (au_ftest_cpdown(*flags, WHED)) {
1facf9fc 33799+ err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
33800+ if (unlikely(err))
33801+ goto out_opq;
33802+ }
33803+
5527c038 33804+ inode = d_inode(dentry);
5afbbe0d
AM
33805+ if (au_ibbot(inode) < bdst)
33806+ au_set_ibbot(inode, bdst);
1facf9fc 33807+ au_set_h_iptr(inode, bdst, au_igrab(h_inode),
33808+ au_hi_flags(inode, /*isdir*/1));
076b876e 33809+ au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
1facf9fc 33810+ goto out; /* success */
33811+
33812+ /* revert */
4f0767ce 33813+out_opq:
c2b27bf2 33814+ if (au_ftest_cpdown(*flags, DIROPQ)) {
febd17d6 33815+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 33816+ rerr = au_diropq_remove(dentry, bdst);
febd17d6 33817+ inode_unlock(h_inode);
1facf9fc 33818+ if (unlikely(rerr)) {
523b37e3
AM
33819+ AuIOErr("failed removing diropq for %pd b%d (%d)\n",
33820+ dentry, bdst, rerr);
1facf9fc 33821+ err = -EIO;
33822+ goto out;
33823+ }
33824+ }
4f0767ce 33825+out_dir:
c2b27bf2 33826+ if (au_ftest_cpdown(*flags, MADE_DIR)) {
1facf9fc 33827+ rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
33828+ if (unlikely(rerr)) {
523b37e3
AM
33829+ AuIOErr("failed removing %pd b%d (%d)\n",
33830+ dentry, bdst, rerr);
1facf9fc 33831+ err = -EIO;
33832+ }
33833+ }
4f0767ce 33834+out_put:
1facf9fc 33835+ au_set_h_dptr(dentry, bdst, NULL);
5afbbe0d
AM
33836+ if (au_dbbot(dentry) == bdst)
33837+ au_update_dbbot(dentry);
4f0767ce 33838+out:
1facf9fc 33839+ dput(parent);
33840+ return err;
33841+}
33842+
33843+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
33844+{
33845+ int err;
c2b27bf2 33846+ unsigned int flags;
1facf9fc 33847+
c2b27bf2
AM
33848+ flags = 0;
33849+ err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
1facf9fc 33850+
33851+ return err;
33852+}
33853+
33854+/* ---------------------------------------------------------------------- */
33855+
33856+/* policies for create */
33857+
c2b27bf2 33858+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
4a4d8108
AM
33859+{
33860+ int err, i, j, ndentry;
33861+ aufs_bindex_t bopq;
33862+ struct au_dcsub_pages dpages;
33863+ struct au_dpage *dpage;
33864+ struct dentry **dentries, *parent, *d;
33865+
33866+ err = au_dpages_init(&dpages, GFP_NOFS);
33867+ if (unlikely(err))
33868+ goto out;
33869+ parent = dget_parent(dentry);
027c5e7a 33870+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
4a4d8108
AM
33871+ if (unlikely(err))
33872+ goto out_free;
33873+
33874+ err = bindex;
33875+ for (i = 0; i < dpages.ndpage; i++) {
33876+ dpage = dpages.dpages + i;
33877+ dentries = dpage->dentries;
33878+ ndentry = dpage->ndentry;
33879+ for (j = 0; j < ndentry; j++) {
33880+ d = dentries[j];
33881+ di_read_lock_parent2(d, !AuLock_IR);
33882+ bopq = au_dbdiropq(d);
33883+ di_read_unlock(d, !AuLock_IR);
33884+ if (bopq >= 0 && bopq < err)
33885+ err = bopq;
33886+ }
33887+ }
33888+
33889+out_free:
33890+ dput(parent);
33891+ au_dpages_free(&dpages);
33892+out:
33893+ return err;
33894+}
33895+
1facf9fc 33896+static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
33897+{
33898+ for (; bindex >= 0; bindex--)
33899+ if (!au_br_rdonly(au_sbr(sb, bindex)))
33900+ return bindex;
33901+ return -EROFS;
33902+}
33903+
33904+/* top down parent */
392086de
AM
33905+static int au_wbr_create_tdp(struct dentry *dentry,
33906+ unsigned int flags __maybe_unused)
1facf9fc 33907+{
33908+ int err;
5afbbe0d 33909+ aufs_bindex_t btop, bindex;
1facf9fc 33910+ struct super_block *sb;
33911+ struct dentry *parent, *h_parent;
33912+
33913+ sb = dentry->d_sb;
5afbbe0d
AM
33914+ btop = au_dbtop(dentry);
33915+ err = btop;
33916+ if (!au_br_rdonly(au_sbr(sb, btop)))
1facf9fc 33917+ goto out;
33918+
33919+ err = -EROFS;
33920+ parent = dget_parent(dentry);
5afbbe0d 33921+ for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
1facf9fc 33922+ h_parent = au_h_dptr(parent, bindex);
5527c038 33923+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 33924+ continue;
33925+
33926+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
33927+ err = bindex;
33928+ break;
33929+ }
33930+ }
33931+ dput(parent);
33932+
33933+ /* bottom up here */
4a4d8108 33934+ if (unlikely(err < 0)) {
5afbbe0d 33935+ err = au_wbr_bu(sb, btop - 1);
4a4d8108
AM
33936+ if (err >= 0)
33937+ err = au_wbr_nonopq(dentry, err);
33938+ }
1facf9fc 33939+
4f0767ce 33940+out:
1facf9fc 33941+ AuDbg("b%d\n", err);
33942+ return err;
33943+}
33944+
33945+/* ---------------------------------------------------------------------- */
33946+
33947+/* an exception for the policy other than tdp */
33948+static int au_wbr_create_exp(struct dentry *dentry)
33949+{
33950+ int err;
33951+ aufs_bindex_t bwh, bdiropq;
33952+ struct dentry *parent;
33953+
33954+ err = -1;
33955+ bwh = au_dbwh(dentry);
33956+ parent = dget_parent(dentry);
33957+ bdiropq = au_dbdiropq(parent);
33958+ if (bwh >= 0) {
33959+ if (bdiropq >= 0)
33960+ err = min(bdiropq, bwh);
33961+ else
33962+ err = bwh;
33963+ AuDbg("%d\n", err);
33964+ } else if (bdiropq >= 0) {
33965+ err = bdiropq;
33966+ AuDbg("%d\n", err);
33967+ }
33968+ dput(parent);
33969+
4a4d8108
AM
33970+ if (err >= 0)
33971+ err = au_wbr_nonopq(dentry, err);
33972+
1facf9fc 33973+ if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
33974+ err = -1;
33975+
33976+ AuDbg("%d\n", err);
33977+ return err;
33978+}
33979+
33980+/* ---------------------------------------------------------------------- */
33981+
33982+/* round robin */
33983+static int au_wbr_create_init_rr(struct super_block *sb)
33984+{
33985+ int err;
33986+
5afbbe0d 33987+ err = au_wbr_bu(sb, au_sbbot(sb));
1facf9fc 33988+ atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
dece6358 33989+ /* smp_mb(); */
1facf9fc 33990+
33991+ AuDbg("b%d\n", err);
33992+ return err;
33993+}
33994+
392086de 33995+static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
1facf9fc 33996+{
33997+ int err, nbr;
33998+ unsigned int u;
5afbbe0d 33999+ aufs_bindex_t bindex, bbot;
1facf9fc 34000+ struct super_block *sb;
34001+ atomic_t *next;
34002+
34003+ err = au_wbr_create_exp(dentry);
34004+ if (err >= 0)
34005+ goto out;
34006+
34007+ sb = dentry->d_sb;
34008+ next = &au_sbi(sb)->si_wbr_rr_next;
5afbbe0d
AM
34009+ bbot = au_sbbot(sb);
34010+ nbr = bbot + 1;
34011+ for (bindex = 0; bindex <= bbot; bindex++) {
392086de 34012+ if (!au_ftest_wbr(flags, DIR)) {
1facf9fc 34013+ err = atomic_dec_return(next) + 1;
34014+ /* modulo for 0 is meaningless */
34015+ if (unlikely(!err))
34016+ err = atomic_dec_return(next) + 1;
34017+ } else
34018+ err = atomic_read(next);
34019+ AuDbg("%d\n", err);
34020+ u = err;
34021+ err = u % nbr;
34022+ AuDbg("%d\n", err);
34023+ if (!au_br_rdonly(au_sbr(sb, err)))
34024+ break;
34025+ err = -EROFS;
34026+ }
34027+
4a4d8108
AM
34028+ if (err >= 0)
34029+ err = au_wbr_nonopq(dentry, err);
34030+
4f0767ce 34031+out:
1facf9fc 34032+ AuDbg("%d\n", err);
34033+ return err;
34034+}
34035+
34036+/* ---------------------------------------------------------------------- */
34037+
34038+/* most free space */
392086de 34039+static void au_mfs(struct dentry *dentry, struct dentry *parent)
1facf9fc 34040+{
34041+ struct super_block *sb;
34042+ struct au_branch *br;
34043+ struct au_wbr_mfs *mfs;
392086de 34044+ struct dentry *h_parent;
5afbbe0d 34045+ aufs_bindex_t bindex, bbot;
1facf9fc 34046+ int err;
34047+ unsigned long long b, bavail;
7f207e10 34048+ struct path h_path;
1facf9fc 34049+ /* reduce the stack usage */
34050+ struct kstatfs *st;
34051+
34052+ st = kmalloc(sizeof(*st), GFP_NOFS);
34053+ if (unlikely(!st)) {
34054+ AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34055+ return;
34056+ }
34057+
34058+ bavail = 0;
34059+ sb = dentry->d_sb;
34060+ mfs = &au_sbi(sb)->si_wbr_mfs;
dece6358 34061+ MtxMustLock(&mfs->mfs_lock);
1facf9fc 34062+ mfs->mfs_bindex = -EROFS;
34063+ mfs->mfsrr_bytes = 0;
392086de
AM
34064+ if (!parent) {
34065+ bindex = 0;
5afbbe0d 34066+ bbot = au_sbbot(sb);
392086de 34067+ } else {
5afbbe0d
AM
34068+ bindex = au_dbtop(parent);
34069+ bbot = au_dbtaildir(parent);
392086de
AM
34070+ }
34071+
5afbbe0d 34072+ for (; bindex <= bbot; bindex++) {
392086de
AM
34073+ if (parent) {
34074+ h_parent = au_h_dptr(parent, bindex);
5527c038 34075+ if (!h_parent || d_is_negative(h_parent))
392086de
AM
34076+ continue;
34077+ }
1facf9fc 34078+ br = au_sbr(sb, bindex);
34079+ if (au_br_rdonly(br))
34080+ continue;
34081+
34082+ /* sb->s_root for NFS is unreliable */
86dc4139 34083+ h_path.mnt = au_br_mnt(br);
7f207e10
AM
34084+ h_path.dentry = h_path.mnt->mnt_root;
34085+ err = vfs_statfs(&h_path, st);
1facf9fc 34086+ if (unlikely(err)) {
34087+ AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34088+ continue;
34089+ }
34090+
34091+ /* when the available size is equal, select the lower one */
34092+ BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34093+ || sizeof(b) < sizeof(st->f_bsize));
34094+ b = st->f_bavail * st->f_bsize;
34095+ br->br_wbr->wbr_bytes = b;
34096+ if (b >= bavail) {
34097+ bavail = b;
34098+ mfs->mfs_bindex = bindex;
34099+ mfs->mfs_jiffy = jiffies;
34100+ }
34101+ }
34102+
34103+ mfs->mfsrr_bytes = bavail;
34104+ AuDbg("b%d\n", mfs->mfs_bindex);
1c60b727 34105+ kfree(st);
1facf9fc 34106+}
34107+
392086de 34108+static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
1facf9fc 34109+{
34110+ int err;
392086de 34111+ struct dentry *parent;
1facf9fc 34112+ struct super_block *sb;
34113+ struct au_wbr_mfs *mfs;
34114+
34115+ err = au_wbr_create_exp(dentry);
34116+ if (err >= 0)
34117+ goto out;
34118+
34119+ sb = dentry->d_sb;
392086de
AM
34120+ parent = NULL;
34121+ if (au_ftest_wbr(flags, PARENT))
34122+ parent = dget_parent(dentry);
1facf9fc 34123+ mfs = &au_sbi(sb)->si_wbr_mfs;
34124+ mutex_lock(&mfs->mfs_lock);
34125+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34126+ || mfs->mfs_bindex < 0
34127+ || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
392086de 34128+ au_mfs(dentry, parent);
1facf9fc 34129+ mutex_unlock(&mfs->mfs_lock);
34130+ err = mfs->mfs_bindex;
392086de 34131+ dput(parent);
1facf9fc 34132+
4a4d8108
AM
34133+ if (err >= 0)
34134+ err = au_wbr_nonopq(dentry, err);
34135+
4f0767ce 34136+out:
1facf9fc 34137+ AuDbg("b%d\n", err);
34138+ return err;
34139+}
34140+
34141+static int au_wbr_create_init_mfs(struct super_block *sb)
34142+{
34143+ struct au_wbr_mfs *mfs;
34144+
34145+ mfs = &au_sbi(sb)->si_wbr_mfs;
34146+ mutex_init(&mfs->mfs_lock);
34147+ mfs->mfs_jiffy = 0;
34148+ mfs->mfs_bindex = -EROFS;
34149+
34150+ return 0;
34151+}
34152+
34153+static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34154+{
34155+ mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34156+ return 0;
34157+}
34158+
34159+/* ---------------------------------------------------------------------- */
34160+
f2c43d5f
AM
34161+/* top down regardless parent, and then mfs */
34162+static int au_wbr_create_tdmfs(struct dentry *dentry,
34163+ unsigned int flags __maybe_unused)
34164+{
34165+ int err;
34166+ aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34167+ unsigned long long watermark;
34168+ struct super_block *sb;
34169+ struct au_wbr_mfs *mfs;
34170+ struct au_branch *br;
34171+ struct dentry *parent;
34172+
34173+ sb = dentry->d_sb;
34174+ mfs = &au_sbi(sb)->si_wbr_mfs;
34175+ mutex_lock(&mfs->mfs_lock);
34176+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34177+ || mfs->mfs_bindex < 0)
34178+ au_mfs(dentry, /*parent*/NULL);
34179+ watermark = mfs->mfsrr_watermark;
34180+ bmfs = mfs->mfs_bindex;
34181+ mutex_unlock(&mfs->mfs_lock);
34182+
34183+ /* another style of au_wbr_create_exp() */
34184+ bwh = au_dbwh(dentry);
34185+ parent = dget_parent(dentry);
34186+ btail = au_dbtaildir(parent);
34187+ if (bwh >= 0 && bwh < btail)
34188+ btail = bwh;
34189+
34190+ err = au_wbr_nonopq(dentry, btail);
34191+ if (unlikely(err < 0))
34192+ goto out;
34193+ btail = err;
34194+ bfound = -1;
34195+ for (bindex = 0; bindex <= btail; bindex++) {
34196+ br = au_sbr(sb, bindex);
34197+ if (au_br_rdonly(br))
34198+ continue;
34199+ if (br->br_wbr->wbr_bytes > watermark) {
34200+ bfound = bindex;
34201+ break;
34202+ }
34203+ }
34204+ err = bfound;
34205+ if (err < 0)
34206+ err = bmfs;
34207+
34208+out:
34209+ dput(parent);
34210+ AuDbg("b%d\n", err);
34211+ return err;
34212+}
34213+
34214+/* ---------------------------------------------------------------------- */
34215+
1facf9fc 34216+/* most free space and then round robin */
392086de 34217+static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
1facf9fc 34218+{
34219+ int err;
34220+ struct au_wbr_mfs *mfs;
34221+
392086de 34222+ err = au_wbr_create_mfs(dentry, flags);
1facf9fc 34223+ if (err >= 0) {
34224+ mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
dece6358 34225+ mutex_lock(&mfs->mfs_lock);
1facf9fc 34226+ if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
392086de 34227+ err = au_wbr_create_rr(dentry, flags);
dece6358 34228+ mutex_unlock(&mfs->mfs_lock);
1facf9fc 34229+ }
34230+
34231+ AuDbg("b%d\n", err);
34232+ return err;
34233+}
34234+
34235+static int au_wbr_create_init_mfsrr(struct super_block *sb)
34236+{
34237+ int err;
34238+
34239+ au_wbr_create_init_mfs(sb); /* ignore */
34240+ err = au_wbr_create_init_rr(sb);
34241+
34242+ return err;
34243+}
34244+
34245+/* ---------------------------------------------------------------------- */
34246+
34247+/* top down parent and most free space */
392086de 34248+static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
1facf9fc 34249+{
34250+ int err, e2;
34251+ unsigned long long b;
5afbbe0d 34252+ aufs_bindex_t bindex, btop, bbot;
1facf9fc 34253+ struct super_block *sb;
34254+ struct dentry *parent, *h_parent;
34255+ struct au_branch *br;
34256+
392086de 34257+ err = au_wbr_create_tdp(dentry, flags);
1facf9fc 34258+ if (unlikely(err < 0))
34259+ goto out;
34260+ parent = dget_parent(dentry);
5afbbe0d
AM
34261+ btop = au_dbtop(parent);
34262+ bbot = au_dbtaildir(parent);
34263+ if (btop == bbot)
1facf9fc 34264+ goto out_parent; /* success */
34265+
392086de 34266+ e2 = au_wbr_create_mfs(dentry, flags);
1facf9fc 34267+ if (e2 < 0)
34268+ goto out_parent; /* success */
34269+
34270+ /* when the available size is equal, select upper one */
34271+ sb = dentry->d_sb;
34272+ br = au_sbr(sb, err);
34273+ b = br->br_wbr->wbr_bytes;
34274+ AuDbg("b%d, %llu\n", err, b);
34275+
5afbbe0d 34276+ for (bindex = btop; bindex <= bbot; bindex++) {
1facf9fc 34277+ h_parent = au_h_dptr(parent, bindex);
5527c038 34278+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34279+ continue;
34280+
34281+ br = au_sbr(sb, bindex);
34282+ if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34283+ b = br->br_wbr->wbr_bytes;
34284+ err = bindex;
34285+ AuDbg("b%d, %llu\n", err, b);
34286+ }
34287+ }
34288+
4a4d8108
AM
34289+ if (err >= 0)
34290+ err = au_wbr_nonopq(dentry, err);
34291+
4f0767ce 34292+out_parent:
1facf9fc 34293+ dput(parent);
4f0767ce 34294+out:
1facf9fc 34295+ AuDbg("b%d\n", err);
34296+ return err;
34297+}
34298+
34299+/* ---------------------------------------------------------------------- */
34300+
392086de
AM
34301+/*
34302+ * - top down parent
34303+ * - most free space with parent
34304+ * - most free space round-robin regardless parent
34305+ */
34306+static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34307+{
34308+ int err;
34309+ unsigned long long watermark;
34310+ struct super_block *sb;
34311+ struct au_branch *br;
34312+ struct au_wbr_mfs *mfs;
34313+
34314+ err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34315+ if (unlikely(err < 0))
34316+ goto out;
34317+
34318+ sb = dentry->d_sb;
34319+ br = au_sbr(sb, err);
34320+ mfs = &au_sbi(sb)->si_wbr_mfs;
34321+ mutex_lock(&mfs->mfs_lock);
34322+ watermark = mfs->mfsrr_watermark;
34323+ mutex_unlock(&mfs->mfs_lock);
34324+ if (br->br_wbr->wbr_bytes < watermark)
34325+ /* regardless the parent dir */
34326+ err = au_wbr_create_mfsrr(dentry, flags);
34327+
34328+out:
34329+ AuDbg("b%d\n", err);
34330+ return err;
34331+}
34332+
34333+/* ---------------------------------------------------------------------- */
34334+
1facf9fc 34335+/* policies for copyup */
34336+
34337+/* top down parent */
34338+static int au_wbr_copyup_tdp(struct dentry *dentry)
34339+{
392086de 34340+ return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
1facf9fc 34341+}
34342+
34343+/* bottom up parent */
34344+static int au_wbr_copyup_bup(struct dentry *dentry)
34345+{
34346+ int err;
5afbbe0d 34347+ aufs_bindex_t bindex, btop;
1facf9fc 34348+ struct dentry *parent, *h_parent;
34349+ struct super_block *sb;
34350+
34351+ err = -EROFS;
34352+ sb = dentry->d_sb;
34353+ parent = dget_parent(dentry);
5afbbe0d
AM
34354+ btop = au_dbtop(parent);
34355+ for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
1facf9fc 34356+ h_parent = au_h_dptr(parent, bindex);
5527c038 34357+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34358+ continue;
34359+
34360+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34361+ err = bindex;
34362+ break;
34363+ }
34364+ }
34365+ dput(parent);
34366+
34367+ /* bottom up here */
34368+ if (unlikely(err < 0))
5afbbe0d 34369+ err = au_wbr_bu(sb, btop - 1);
1facf9fc 34370+
34371+ AuDbg("b%d\n", err);
34372+ return err;
34373+}
34374+
34375+/* bottom up */
5afbbe0d 34376+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
1facf9fc 34377+{
34378+ int err;
34379+
5afbbe0d 34380+ err = au_wbr_bu(dentry->d_sb, btop);
4a4d8108 34381+ AuDbg("b%d\n", err);
5afbbe0d 34382+ if (err > btop)
4a4d8108 34383+ err = au_wbr_nonopq(dentry, err);
1facf9fc 34384+
34385+ AuDbg("b%d\n", err);
34386+ return err;
34387+}
34388+
076b876e
AM
34389+static int au_wbr_copyup_bu(struct dentry *dentry)
34390+{
34391+ int err;
5afbbe0d 34392+ aufs_bindex_t btop;
076b876e 34393+
5afbbe0d
AM
34394+ btop = au_dbtop(dentry);
34395+ err = au_wbr_do_copyup_bu(dentry, btop);
076b876e
AM
34396+ return err;
34397+}
34398+
1facf9fc 34399+/* ---------------------------------------------------------------------- */
34400+
34401+struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34402+ [AuWbrCopyup_TDP] = {
34403+ .copyup = au_wbr_copyup_tdp
34404+ },
34405+ [AuWbrCopyup_BUP] = {
34406+ .copyup = au_wbr_copyup_bup
34407+ },
34408+ [AuWbrCopyup_BU] = {
34409+ .copyup = au_wbr_copyup_bu
34410+ }
34411+};
34412+
34413+struct au_wbr_create_operations au_wbr_create_ops[] = {
34414+ [AuWbrCreate_TDP] = {
34415+ .create = au_wbr_create_tdp
34416+ },
34417+ [AuWbrCreate_RR] = {
34418+ .create = au_wbr_create_rr,
34419+ .init = au_wbr_create_init_rr
34420+ },
34421+ [AuWbrCreate_MFS] = {
34422+ .create = au_wbr_create_mfs,
34423+ .init = au_wbr_create_init_mfs,
34424+ .fin = au_wbr_create_fin_mfs
34425+ },
34426+ [AuWbrCreate_MFSV] = {
34427+ .create = au_wbr_create_mfs,
34428+ .init = au_wbr_create_init_mfs,
34429+ .fin = au_wbr_create_fin_mfs
34430+ },
34431+ [AuWbrCreate_MFSRR] = {
34432+ .create = au_wbr_create_mfsrr,
34433+ .init = au_wbr_create_init_mfsrr,
34434+ .fin = au_wbr_create_fin_mfs
34435+ },
34436+ [AuWbrCreate_MFSRRV] = {
34437+ .create = au_wbr_create_mfsrr,
34438+ .init = au_wbr_create_init_mfsrr,
34439+ .fin = au_wbr_create_fin_mfs
34440+ },
f2c43d5f
AM
34441+ [AuWbrCreate_TDMFS] = {
34442+ .create = au_wbr_create_tdmfs,
34443+ .init = au_wbr_create_init_mfs,
34444+ .fin = au_wbr_create_fin_mfs
34445+ },
34446+ [AuWbrCreate_TDMFSV] = {
34447+ .create = au_wbr_create_tdmfs,
34448+ .init = au_wbr_create_init_mfs,
34449+ .fin = au_wbr_create_fin_mfs
34450+ },
1facf9fc 34451+ [AuWbrCreate_PMFS] = {
34452+ .create = au_wbr_create_pmfs,
34453+ .init = au_wbr_create_init_mfs,
34454+ .fin = au_wbr_create_fin_mfs
34455+ },
34456+ [AuWbrCreate_PMFSV] = {
34457+ .create = au_wbr_create_pmfs,
34458+ .init = au_wbr_create_init_mfs,
34459+ .fin = au_wbr_create_fin_mfs
392086de
AM
34460+ },
34461+ [AuWbrCreate_PMFSRR] = {
34462+ .create = au_wbr_create_pmfsrr,
34463+ .init = au_wbr_create_init_mfsrr,
34464+ .fin = au_wbr_create_fin_mfs
34465+ },
34466+ [AuWbrCreate_PMFSRRV] = {
34467+ .create = au_wbr_create_pmfsrr,
34468+ .init = au_wbr_create_init_mfsrr,
34469+ .fin = au_wbr_create_fin_mfs
1facf9fc 34470+ }
34471+};
7f207e10
AM
34472diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
34473--- /usr/share/empty/fs/aufs/whout.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947 34474+++ linux/fs/aufs/whout.c 2017-11-12 22:24:42.274176929 +0100
f2c43d5f 34475@@ -0,0 +1,1061 @@
1facf9fc 34476+/*
a2654f78 34477+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 34478+ *
34479+ * This program, aufs is free software; you can redistribute it and/or modify
34480+ * it under the terms of the GNU General Public License as published by
34481+ * the Free Software Foundation; either version 2 of the License, or
34482+ * (at your option) any later version.
dece6358
AM
34483+ *
34484+ * This program is distributed in the hope that it will be useful,
34485+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
34486+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34487+ * GNU General Public License for more details.
34488+ *
34489+ * You should have received a copy of the GNU General Public License
523b37e3 34490+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 34491+ */
34492+
34493+/*
34494+ * whiteout for logical deletion and opaque directory
34495+ */
34496+
1facf9fc 34497+#include "aufs.h"
34498+
34499+#define WH_MASK S_IRUGO
34500+
34501+/*
34502+ * If a directory contains this file, then it is opaque. We start with the
34503+ * .wh. flag so that it is blocked by lookup.
34504+ */
0c3ec466
AM
34505+static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
34506+ sizeof(AUFS_WH_DIROPQ) - 1);
1facf9fc 34507+
34508+/*
34509+ * generate whiteout name, which is NOT terminated by NULL.
34510+ * @name: original d_name.name
34511+ * @len: original d_name.len
34512+ * @wh: whiteout qstr
34513+ * returns zero when succeeds, otherwise error.
34514+ * succeeded value as wh->name should be freed by kfree().
34515+ */
34516+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
34517+{
34518+ char *p;
34519+
34520+ if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
34521+ return -ENAMETOOLONG;
34522+
34523+ wh->len = name->len + AUFS_WH_PFX_LEN;
34524+ p = kmalloc(wh->len, GFP_NOFS);
34525+ wh->name = p;
34526+ if (p) {
34527+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
34528+ memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
34529+ /* smp_mb(); */
34530+ return 0;
34531+ }
34532+ return -ENOMEM;
34533+}
34534+
34535+/* ---------------------------------------------------------------------- */
34536+
34537+/*
34538+ * test if the @wh_name exists under @h_parent.
34539+ * @try_sio specifies the necessary of super-io.
34540+ */
076b876e 34541+int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
1facf9fc 34542+{
34543+ int err;
34544+ struct dentry *wh_dentry;
1facf9fc 34545+
1facf9fc 34546+ if (!try_sio)
b4510431 34547+ wh_dentry = vfsub_lkup_one(wh_name, h_parent);
1facf9fc 34548+ else
076b876e 34549+ wh_dentry = au_sio_lkup_one(wh_name, h_parent);
1facf9fc 34550+ err = PTR_ERR(wh_dentry);
2000de60
JR
34551+ if (IS_ERR(wh_dentry)) {
34552+ if (err == -ENAMETOOLONG)
34553+ err = 0;
1facf9fc 34554+ goto out;
2000de60 34555+ }
1facf9fc 34556+
34557+ err = 0;
5527c038 34558+ if (d_is_negative(wh_dentry))
1facf9fc 34559+ goto out_wh; /* success */
34560+
34561+ err = 1;
7e9cd9fe 34562+ if (d_is_reg(wh_dentry))
1facf9fc 34563+ goto out_wh; /* success */
34564+
34565+ err = -EIO;
523b37e3 34566+ AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
5527c038 34567+ wh_dentry, d_inode(wh_dentry)->i_mode);
1facf9fc 34568+
4f0767ce 34569+out_wh:
1facf9fc 34570+ dput(wh_dentry);
4f0767ce 34571+out:
1facf9fc 34572+ return err;
34573+}
34574+
34575+/*
34576+ * test if the @h_dentry sets opaque or not.
34577+ */
076b876e 34578+int au_diropq_test(struct dentry *h_dentry)
1facf9fc 34579+{
34580+ int err;
34581+ struct inode *h_dir;
34582+
5527c038 34583+ h_dir = d_inode(h_dentry);
076b876e 34584+ err = au_wh_test(h_dentry, &diropq_name,
1facf9fc 34585+ au_test_h_perm_sio(h_dir, MAY_EXEC));
34586+ return err;
34587+}
34588+
34589+/*
34590+ * returns a negative dentry whose name is unique and temporary.
34591+ */
34592+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
34593+ struct qstr *prefix)
34594+{
1facf9fc 34595+ struct dentry *dentry;
34596+ int i;
027c5e7a 34597+ char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
4a4d8108 34598+ *name, *p;
027c5e7a 34599+ /* strict atomic_t is unnecessary here */
1facf9fc 34600+ static unsigned short cnt;
34601+ struct qstr qs;
34602+
4a4d8108
AM
34603+ BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
34604+
1facf9fc 34605+ name = defname;
027c5e7a
AM
34606+ qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
34607+ if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
1facf9fc 34608+ dentry = ERR_PTR(-ENAMETOOLONG);
4a4d8108 34609+ if (unlikely(qs.len > NAME_MAX))
1facf9fc 34610+ goto out;
34611+ dentry = ERR_PTR(-ENOMEM);
34612+ name = kmalloc(qs.len + 1, GFP_NOFS);
34613+ if (unlikely(!name))
34614+ goto out;
34615+ }
34616+
34617+ /* doubly whiteout-ed */
34618+ memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
34619+ p = name + AUFS_WH_PFX_LEN * 2;
34620+ memcpy(p, prefix->name, prefix->len);
34621+ p += prefix->len;
34622+ *p++ = '.';
4a4d8108 34623+ AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
1facf9fc 34624+
34625+ qs.name = name;
34626+ for (i = 0; i < 3; i++) {
b752ccd1 34627+ sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
076b876e 34628+ dentry = au_sio_lkup_one(&qs, h_parent);
5527c038 34629+ if (IS_ERR(dentry) || d_is_negative(dentry))
1facf9fc 34630+ goto out_name;
34631+ dput(dentry);
34632+ }
0c3ec466 34633+ /* pr_warn("could not get random name\n"); */
1facf9fc 34634+ dentry = ERR_PTR(-EEXIST);
34635+ AuDbg("%.*s\n", AuLNPair(&qs));
34636+ BUG();
34637+
4f0767ce 34638+out_name:
1facf9fc 34639+ if (name != defname)
1c60b727 34640+ kfree(name);
4f0767ce 34641+out:
4a4d8108 34642+ AuTraceErrPtr(dentry);
1facf9fc 34643+ return dentry;
1facf9fc 34644+}
34645+
34646+/*
34647+ * rename the @h_dentry on @br to the whiteouted temporary name.
34648+ */
34649+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
34650+{
34651+ int err;
34652+ struct path h_path = {
86dc4139 34653+ .mnt = au_br_mnt(br)
1facf9fc 34654+ };
523b37e3 34655+ struct inode *h_dir, *delegated;
1facf9fc 34656+ struct dentry *h_parent;
34657+
34658+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 34659+ h_dir = d_inode(h_parent);
1facf9fc 34660+ IMustLock(h_dir);
34661+
34662+ h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
34663+ err = PTR_ERR(h_path.dentry);
34664+ if (IS_ERR(h_path.dentry))
34665+ goto out;
34666+
34667+ /* under the same dir, no need to lock_rename() */
523b37e3 34668+ delegated = NULL;
f2c43d5f
AM
34669+ err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
34670+ /*flags*/0);
1facf9fc 34671+ AuTraceErr(err);
523b37e3
AM
34672+ if (unlikely(err == -EWOULDBLOCK)) {
34673+ pr_warn("cannot retry for NFSv4 delegation"
34674+ " for an internal rename\n");
34675+ iput(delegated);
34676+ }
1facf9fc 34677+ dput(h_path.dentry);
34678+
4f0767ce 34679+out:
4a4d8108 34680+ AuTraceErr(err);
1facf9fc 34681+ return err;
34682+}
34683+
34684+/* ---------------------------------------------------------------------- */
34685+/*
34686+ * functions for removing a whiteout
34687+ */
34688+
34689+static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
34690+{
523b37e3
AM
34691+ int err, force;
34692+ struct inode *delegated;
1facf9fc 34693+
34694+ /*
34695+ * forces superio when the dir has a sticky bit.
34696+ * this may be a violation of unix fs semantics.
34697+ */
34698+ force = (h_dir->i_mode & S_ISVTX)
5527c038 34699+ && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
523b37e3
AM
34700+ delegated = NULL;
34701+ err = vfsub_unlink(h_dir, h_path, &delegated, force);
34702+ if (unlikely(err == -EWOULDBLOCK)) {
34703+ pr_warn("cannot retry for NFSv4 delegation"
34704+ " for an internal unlink\n");
34705+ iput(delegated);
34706+ }
34707+ return err;
1facf9fc 34708+}
34709+
34710+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
34711+ struct dentry *dentry)
34712+{
34713+ int err;
34714+
34715+ err = do_unlink_wh(h_dir, h_path);
34716+ if (!err && dentry)
34717+ au_set_dbwh(dentry, -1);
34718+
34719+ return err;
34720+}
34721+
34722+static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
34723+ struct au_branch *br)
34724+{
34725+ int err;
34726+ struct path h_path = {
86dc4139 34727+ .mnt = au_br_mnt(br)
1facf9fc 34728+ };
34729+
34730+ err = 0;
b4510431 34731+ h_path.dentry = vfsub_lkup_one(wh, h_parent);
1facf9fc 34732+ if (IS_ERR(h_path.dentry))
34733+ err = PTR_ERR(h_path.dentry);
34734+ else {
5527c038
JR
34735+ if (d_is_reg(h_path.dentry))
34736+ err = do_unlink_wh(d_inode(h_parent), &h_path);
1facf9fc 34737+ dput(h_path.dentry);
34738+ }
34739+
34740+ return err;
34741+}
34742+
34743+/* ---------------------------------------------------------------------- */
34744+/*
34745+ * initialize/clean whiteout for a branch
34746+ */
34747+
34748+static void au_wh_clean(struct inode *h_dir, struct path *whpath,
34749+ const int isdir)
34750+{
34751+ int err;
523b37e3 34752+ struct inode *delegated;
1facf9fc 34753+
5527c038 34754+ if (d_is_negative(whpath->dentry))
1facf9fc 34755+ return;
34756+
86dc4139
AM
34757+ if (isdir)
34758+ err = vfsub_rmdir(h_dir, whpath);
523b37e3
AM
34759+ else {
34760+ delegated = NULL;
34761+ err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
34762+ if (unlikely(err == -EWOULDBLOCK)) {
34763+ pr_warn("cannot retry for NFSv4 delegation"
34764+ " for an internal unlink\n");
34765+ iput(delegated);
34766+ }
34767+ }
1facf9fc 34768+ if (unlikely(err))
523b37e3
AM
34769+ pr_warn("failed removing %pd (%d), ignored.\n",
34770+ whpath->dentry, err);
1facf9fc 34771+}
34772+
34773+static int test_linkable(struct dentry *h_root)
34774+{
5527c038 34775+ struct inode *h_dir = d_inode(h_root);
1facf9fc 34776+
34777+ if (h_dir->i_op->link)
34778+ return 0;
34779+
523b37e3
AM
34780+ pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
34781+ h_root, au_sbtype(h_root->d_sb));
1facf9fc 34782+ return -ENOSYS;
34783+}
34784+
34785+/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
34786+static int au_whdir(struct inode *h_dir, struct path *path)
34787+{
34788+ int err;
34789+
34790+ err = -EEXIST;
5527c038 34791+ if (d_is_negative(path->dentry)) {
1facf9fc 34792+ int mode = S_IRWXU;
34793+
34794+ if (au_test_nfs(path->dentry->d_sb))
34795+ mode |= S_IXUGO;
86dc4139 34796+ err = vfsub_mkdir(h_dir, path, mode);
2000de60 34797+ } else if (d_is_dir(path->dentry))
1facf9fc 34798+ err = 0;
34799+ else
523b37e3 34800+ pr_err("unknown %pd exists\n", path->dentry);
1facf9fc 34801+
34802+ return err;
34803+}
34804+
34805+struct au_wh_base {
34806+ const struct qstr *name;
34807+ struct dentry *dentry;
34808+};
34809+
34810+static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
34811+ struct path *h_path)
34812+{
34813+ h_path->dentry = base[AuBrWh_BASE].dentry;
34814+ au_wh_clean(h_dir, h_path, /*isdir*/0);
34815+ h_path->dentry = base[AuBrWh_PLINK].dentry;
34816+ au_wh_clean(h_dir, h_path, /*isdir*/1);
34817+ h_path->dentry = base[AuBrWh_ORPH].dentry;
34818+ au_wh_clean(h_dir, h_path, /*isdir*/1);
34819+}
34820+
34821+/*
34822+ * returns tri-state,
c1595e42 34823+ * minus: error, caller should print the message
1facf9fc 34824+ * zero: succuess
c1595e42 34825+ * plus: error, caller should NOT print the message
1facf9fc 34826+ */
34827+static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
34828+ int do_plink, struct au_wh_base base[],
34829+ struct path *h_path)
34830+{
34831+ int err;
34832+ struct inode *h_dir;
34833+
5527c038 34834+ h_dir = d_inode(h_root);
1facf9fc 34835+ h_path->dentry = base[AuBrWh_BASE].dentry;
34836+ au_wh_clean(h_dir, h_path, /*isdir*/0);
34837+ h_path->dentry = base[AuBrWh_PLINK].dentry;
34838+ if (do_plink) {
34839+ err = test_linkable(h_root);
34840+ if (unlikely(err)) {
34841+ err = 1;
34842+ goto out;
34843+ }
34844+
34845+ err = au_whdir(h_dir, h_path);
34846+ if (unlikely(err))
34847+ goto out;
34848+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
34849+ } else
34850+ au_wh_clean(h_dir, h_path, /*isdir*/1);
34851+ h_path->dentry = base[AuBrWh_ORPH].dentry;
34852+ err = au_whdir(h_dir, h_path);
34853+ if (unlikely(err))
34854+ goto out;
34855+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
34856+
4f0767ce 34857+out:
1facf9fc 34858+ return err;
34859+}
34860+
34861+/*
34862+ * for the moment, aufs supports the branch filesystem which does not support
34863+ * link(2). testing on FAT which does not support i_op->setattr() fully either,
34864+ * copyup failed. finally, such filesystem will not be used as the writable
34865+ * branch.
34866+ *
34867+ * returns tri-state, see above.
34868+ */
34869+static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
34870+ int do_plink, struct au_wh_base base[],
34871+ struct path *h_path)
34872+{
34873+ int err;
34874+ struct inode *h_dir;
34875+
1308ab2a 34876+ WbrWhMustWriteLock(wbr);
34877+
1facf9fc 34878+ err = test_linkable(h_root);
34879+ if (unlikely(err)) {
34880+ err = 1;
34881+ goto out;
34882+ }
34883+
34884+ /*
34885+ * todo: should this create be done in /sbin/mount.aufs helper?
34886+ */
34887+ err = -EEXIST;
5527c038
JR
34888+ h_dir = d_inode(h_root);
34889+ if (d_is_negative(base[AuBrWh_BASE].dentry)) {
86dc4139
AM
34890+ h_path->dentry = base[AuBrWh_BASE].dentry;
34891+ err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
7e9cd9fe 34892+ } else if (d_is_reg(base[AuBrWh_BASE].dentry))
1facf9fc 34893+ err = 0;
34894+ else
523b37e3 34895+ pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
1facf9fc 34896+ if (unlikely(err))
34897+ goto out;
34898+
34899+ h_path->dentry = base[AuBrWh_PLINK].dentry;
34900+ if (do_plink) {
34901+ err = au_whdir(h_dir, h_path);
34902+ if (unlikely(err))
34903+ goto out;
34904+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
34905+ } else
34906+ au_wh_clean(h_dir, h_path, /*isdir*/1);
34907+ wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
34908+
34909+ h_path->dentry = base[AuBrWh_ORPH].dentry;
34910+ err = au_whdir(h_dir, h_path);
34911+ if (unlikely(err))
34912+ goto out;
34913+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
34914+
4f0767ce 34915+out:
1facf9fc 34916+ return err;
34917+}
34918+
34919+/*
34920+ * initialize the whiteout base file/dir for @br.
34921+ */
86dc4139 34922+int au_wh_init(struct au_branch *br, struct super_block *sb)
1facf9fc 34923+{
34924+ int err, i;
34925+ const unsigned char do_plink
34926+ = !!au_opt_test(au_mntflags(sb), PLINK);
1facf9fc 34927+ struct inode *h_dir;
86dc4139
AM
34928+ struct path path = br->br_path;
34929+ struct dentry *h_root = path.dentry;
1facf9fc 34930+ struct au_wbr *wbr = br->br_wbr;
34931+ static const struct qstr base_name[] = {
0c3ec466
AM
34932+ [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
34933+ sizeof(AUFS_BASE_NAME) - 1),
34934+ [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
34935+ sizeof(AUFS_PLINKDIR_NAME) - 1),
34936+ [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
34937+ sizeof(AUFS_ORPHDIR_NAME) - 1)
1facf9fc 34938+ };
34939+ struct au_wh_base base[] = {
34940+ [AuBrWh_BASE] = {
34941+ .name = base_name + AuBrWh_BASE,
34942+ .dentry = NULL
34943+ },
34944+ [AuBrWh_PLINK] = {
34945+ .name = base_name + AuBrWh_PLINK,
34946+ .dentry = NULL
34947+ },
34948+ [AuBrWh_ORPH] = {
34949+ .name = base_name + AuBrWh_ORPH,
34950+ .dentry = NULL
34951+ }
34952+ };
34953+
1308ab2a 34954+ if (wbr)
34955+ WbrWhMustWriteLock(wbr);
1facf9fc 34956+
1facf9fc 34957+ for (i = 0; i < AuBrWh_Last; i++) {
34958+ /* doubly whiteouted */
34959+ struct dentry *d;
34960+
34961+ d = au_wh_lkup(h_root, (void *)base[i].name, br);
34962+ err = PTR_ERR(d);
34963+ if (IS_ERR(d))
34964+ goto out;
34965+
34966+ base[i].dentry = d;
34967+ AuDebugOn(wbr
34968+ && wbr->wbr_wh[i]
34969+ && wbr->wbr_wh[i] != base[i].dentry);
34970+ }
34971+
34972+ if (wbr)
34973+ for (i = 0; i < AuBrWh_Last; i++) {
34974+ dput(wbr->wbr_wh[i]);
34975+ wbr->wbr_wh[i] = NULL;
34976+ }
34977+
34978+ err = 0;
1e00d052 34979+ if (!au_br_writable(br->br_perm)) {
5527c038 34980+ h_dir = d_inode(h_root);
1facf9fc 34981+ au_wh_init_ro(h_dir, base, &path);
1e00d052 34982+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 34983+ err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
34984+ if (err > 0)
34985+ goto out;
34986+ else if (err)
34987+ goto out_err;
1e00d052 34988+ } else {
1facf9fc 34989+ err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
34990+ if (err > 0)
34991+ goto out;
34992+ else if (err)
34993+ goto out_err;
1facf9fc 34994+ }
34995+ goto out; /* success */
34996+
4f0767ce 34997+out_err:
523b37e3
AM
34998+ pr_err("an error(%d) on the writable branch %pd(%s)\n",
34999+ err, h_root, au_sbtype(h_root->d_sb));
4f0767ce 35000+out:
1facf9fc 35001+ for (i = 0; i < AuBrWh_Last; i++)
35002+ dput(base[i].dentry);
35003+ return err;
35004+}
35005+
35006+/* ---------------------------------------------------------------------- */
35007+/*
35008+ * whiteouts are all hard-linked usually.
35009+ * when its link count reaches a ceiling, we create a new whiteout base
35010+ * asynchronously.
35011+ */
35012+
35013+struct reinit_br_wh {
35014+ struct super_block *sb;
35015+ struct au_branch *br;
35016+};
35017+
35018+static void reinit_br_wh(void *arg)
35019+{
35020+ int err;
35021+ aufs_bindex_t bindex;
35022+ struct path h_path;
35023+ struct reinit_br_wh *a = arg;
35024+ struct au_wbr *wbr;
523b37e3 35025+ struct inode *dir, *delegated;
1facf9fc 35026+ struct dentry *h_root;
35027+ struct au_hinode *hdir;
35028+
35029+ err = 0;
35030+ wbr = a->br->br_wbr;
35031+ /* big aufs lock */
35032+ si_noflush_write_lock(a->sb);
35033+ if (!au_br_writable(a->br->br_perm))
35034+ goto out;
35035+ bindex = au_br_index(a->sb, a->br->br_id);
35036+ if (unlikely(bindex < 0))
35037+ goto out;
35038+
1308ab2a 35039+ di_read_lock_parent(a->sb->s_root, AuLock_IR);
5527c038 35040+ dir = d_inode(a->sb->s_root);
1facf9fc 35041+ hdir = au_hi(dir, bindex);
35042+ h_root = au_h_dptr(a->sb->s_root, bindex);
86dc4139 35043+ AuDebugOn(h_root != au_br_dentry(a->br));
1facf9fc 35044+
5afbbe0d 35045+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 35046+ wbr_wh_write_lock(wbr);
35047+ err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35048+ h_root, a->br);
35049+ if (!err) {
86dc4139
AM
35050+ h_path.dentry = wbr->wbr_whbase;
35051+ h_path.mnt = au_br_mnt(a->br);
523b37e3
AM
35052+ delegated = NULL;
35053+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35054+ /*force*/0);
35055+ if (unlikely(err == -EWOULDBLOCK)) {
35056+ pr_warn("cannot retry for NFSv4 delegation"
35057+ " for an internal unlink\n");
35058+ iput(delegated);
35059+ }
1facf9fc 35060+ } else {
523b37e3 35061+ pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
1facf9fc 35062+ err = 0;
35063+ }
35064+ dput(wbr->wbr_whbase);
35065+ wbr->wbr_whbase = NULL;
35066+ if (!err)
86dc4139 35067+ err = au_wh_init(a->br, a->sb);
1facf9fc 35068+ wbr_wh_write_unlock(wbr);
5afbbe0d 35069+ au_hn_inode_unlock(hdir);
1308ab2a 35070+ di_read_unlock(a->sb->s_root, AuLock_IR);
076b876e
AM
35071+ if (!err)
35072+ au_fhsm_wrote(a->sb, bindex, /*force*/0);
1facf9fc 35073+
4f0767ce 35074+out:
1facf9fc 35075+ if (wbr)
35076+ atomic_dec(&wbr->wbr_wh_running);
5afbbe0d 35077+ au_br_put(a->br);
1facf9fc 35078+ si_write_unlock(a->sb);
027c5e7a 35079+ au_nwt_done(&au_sbi(a->sb)->si_nowait);
1c60b727 35080+ kfree(arg);
1facf9fc 35081+ if (unlikely(err))
35082+ AuIOErr("err %d\n", err);
35083+}
35084+
35085+static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35086+{
35087+ int do_dec, wkq_err;
35088+ struct reinit_br_wh *arg;
35089+
35090+ do_dec = 1;
35091+ if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35092+ goto out;
35093+
35094+ /* ignore ENOMEM */
35095+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
35096+ if (arg) {
35097+ /*
35098+ * dec(wh_running), kfree(arg) and dec(br_count)
35099+ * in reinit function
35100+ */
35101+ arg->sb = sb;
35102+ arg->br = br;
5afbbe0d 35103+ au_br_get(br);
53392da6 35104+ wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
1facf9fc 35105+ if (unlikely(wkq_err)) {
35106+ atomic_dec(&br->br_wbr->wbr_wh_running);
5afbbe0d 35107+ au_br_put(br);
1c60b727 35108+ kfree(arg);
1facf9fc 35109+ }
35110+ do_dec = 0;
35111+ }
35112+
4f0767ce 35113+out:
1facf9fc 35114+ if (do_dec)
35115+ atomic_dec(&br->br_wbr->wbr_wh_running);
35116+}
35117+
35118+/* ---------------------------------------------------------------------- */
35119+
35120+/*
35121+ * create the whiteout @wh.
35122+ */
35123+static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35124+ struct dentry *wh)
35125+{
35126+ int err;
35127+ struct path h_path = {
35128+ .dentry = wh
35129+ };
35130+ struct au_branch *br;
35131+ struct au_wbr *wbr;
35132+ struct dentry *h_parent;
523b37e3 35133+ struct inode *h_dir, *delegated;
1facf9fc 35134+
35135+ h_parent = wh->d_parent; /* dir inode is locked */
5527c038 35136+ h_dir = d_inode(h_parent);
1facf9fc 35137+ IMustLock(h_dir);
35138+
35139+ br = au_sbr(sb, bindex);
86dc4139 35140+ h_path.mnt = au_br_mnt(br);
1facf9fc 35141+ wbr = br->br_wbr;
35142+ wbr_wh_read_lock(wbr);
35143+ if (wbr->wbr_whbase) {
523b37e3
AM
35144+ delegated = NULL;
35145+ err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35146+ if (unlikely(err == -EWOULDBLOCK)) {
35147+ pr_warn("cannot retry for NFSv4 delegation"
35148+ " for an internal link\n");
35149+ iput(delegated);
35150+ }
1facf9fc 35151+ if (!err || err != -EMLINK)
35152+ goto out;
35153+
35154+ /* link count full. re-initialize br_whbase. */
35155+ kick_reinit_br_wh(sb, br);
35156+ }
35157+
35158+ /* return this error in this context */
b4510431 35159+ err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
076b876e
AM
35160+ if (!err)
35161+ au_fhsm_wrote(sb, bindex, /*force*/0);
1facf9fc 35162+
4f0767ce 35163+out:
1facf9fc 35164+ wbr_wh_read_unlock(wbr);
35165+ return err;
35166+}
35167+
35168+/* ---------------------------------------------------------------------- */
35169+
35170+/*
35171+ * create or remove the diropq.
35172+ */
35173+static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35174+ unsigned int flags)
35175+{
35176+ struct dentry *opq_dentry, *h_dentry;
35177+ struct super_block *sb;
35178+ struct au_branch *br;
35179+ int err;
35180+
35181+ sb = dentry->d_sb;
35182+ br = au_sbr(sb, bindex);
35183+ h_dentry = au_h_dptr(dentry, bindex);
b4510431 35184+ opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
1facf9fc 35185+ if (IS_ERR(opq_dentry))
35186+ goto out;
35187+
35188+ if (au_ftest_diropq(flags, CREATE)) {
35189+ err = link_or_create_wh(sb, bindex, opq_dentry);
35190+ if (!err) {
35191+ au_set_dbdiropq(dentry, bindex);
35192+ goto out; /* success */
35193+ }
35194+ } else {
35195+ struct path tmp = {
35196+ .dentry = opq_dentry,
86dc4139 35197+ .mnt = au_br_mnt(br)
1facf9fc 35198+ };
5527c038 35199+ err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
1facf9fc 35200+ if (!err)
35201+ au_set_dbdiropq(dentry, -1);
35202+ }
35203+ dput(opq_dentry);
35204+ opq_dentry = ERR_PTR(err);
35205+
4f0767ce 35206+out:
1facf9fc 35207+ return opq_dentry;
35208+}
35209+
35210+struct do_diropq_args {
35211+ struct dentry **errp;
35212+ struct dentry *dentry;
35213+ aufs_bindex_t bindex;
35214+ unsigned int flags;
35215+};
35216+
35217+static void call_do_diropq(void *args)
35218+{
35219+ struct do_diropq_args *a = args;
35220+ *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35221+}
35222+
35223+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35224+ unsigned int flags)
35225+{
35226+ struct dentry *diropq, *h_dentry;
35227+
35228+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 35229+ if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
1facf9fc 35230+ diropq = do_diropq(dentry, bindex, flags);
35231+ else {
35232+ int wkq_err;
35233+ struct do_diropq_args args = {
35234+ .errp = &diropq,
35235+ .dentry = dentry,
35236+ .bindex = bindex,
35237+ .flags = flags
35238+ };
35239+
35240+ wkq_err = au_wkq_wait(call_do_diropq, &args);
35241+ if (unlikely(wkq_err))
35242+ diropq = ERR_PTR(wkq_err);
35243+ }
35244+
35245+ return diropq;
35246+}
35247+
35248+/* ---------------------------------------------------------------------- */
35249+
35250+/*
35251+ * lookup whiteout dentry.
35252+ * @h_parent: lower parent dentry which must exist and be locked
35253+ * @base_name: name of dentry which will be whiteouted
35254+ * returns dentry for whiteout.
35255+ */
35256+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35257+ struct au_branch *br)
35258+{
35259+ int err;
35260+ struct qstr wh_name;
35261+ struct dentry *wh_dentry;
35262+
35263+ err = au_wh_name_alloc(&wh_name, base_name);
35264+ wh_dentry = ERR_PTR(err);
35265+ if (!err) {
b4510431 35266+ wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
1c60b727 35267+ kfree(wh_name.name);
1facf9fc 35268+ }
35269+ return wh_dentry;
35270+}
35271+
35272+/*
35273+ * link/create a whiteout for @dentry on @bindex.
35274+ */
35275+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35276+ struct dentry *h_parent)
35277+{
35278+ struct dentry *wh_dentry;
35279+ struct super_block *sb;
35280+ int err;
35281+
35282+ sb = dentry->d_sb;
35283+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
5527c038 35284+ if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
1facf9fc 35285+ err = link_or_create_wh(sb, bindex, wh_dentry);
076b876e 35286+ if (!err) {
1facf9fc 35287+ au_set_dbwh(dentry, bindex);
076b876e
AM
35288+ au_fhsm_wrote(sb, bindex, /*force*/0);
35289+ } else {
1facf9fc 35290+ dput(wh_dentry);
35291+ wh_dentry = ERR_PTR(err);
35292+ }
35293+ }
35294+
35295+ return wh_dentry;
35296+}
35297+
35298+/* ---------------------------------------------------------------------- */
35299+
35300+/* Delete all whiteouts in this directory on branch bindex. */
35301+static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
35302+ aufs_bindex_t bindex, struct au_branch *br)
35303+{
35304+ int err;
35305+ unsigned long ul, n;
35306+ struct qstr wh_name;
35307+ char *p;
35308+ struct hlist_head *head;
c06a8ce3 35309+ struct au_vdir_wh *pos;
1facf9fc 35310+ struct au_vdir_destr *str;
35311+
35312+ err = -ENOMEM;
537831f9 35313+ p = (void *)__get_free_page(GFP_NOFS);
1facf9fc 35314+ wh_name.name = p;
35315+ if (unlikely(!wh_name.name))
35316+ goto out;
35317+
35318+ err = 0;
35319+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35320+ p += AUFS_WH_PFX_LEN;
35321+ n = whlist->nh_num;
35322+ head = whlist->nh_head;
35323+ for (ul = 0; !err && ul < n; ul++, head++) {
c06a8ce3
AM
35324+ hlist_for_each_entry(pos, head, wh_hash) {
35325+ if (pos->wh_bindex != bindex)
1facf9fc 35326+ continue;
35327+
c06a8ce3 35328+ str = &pos->wh_str;
1facf9fc 35329+ if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35330+ memcpy(p, str->name, str->len);
35331+ wh_name.len = AUFS_WH_PFX_LEN + str->len;
35332+ err = unlink_wh_name(h_dentry, &wh_name, br);
35333+ if (!err)
35334+ continue;
35335+ break;
35336+ }
35337+ AuIOErr("whiteout name too long %.*s\n",
35338+ str->len, str->name);
35339+ err = -EIO;
35340+ break;
35341+ }
35342+ }
1c60b727 35343+ free_page((unsigned long)wh_name.name);
1facf9fc 35344+
4f0767ce 35345+out:
1facf9fc 35346+ return err;
35347+}
35348+
35349+struct del_wh_children_args {
35350+ int *errp;
35351+ struct dentry *h_dentry;
1308ab2a 35352+ struct au_nhash *whlist;
1facf9fc 35353+ aufs_bindex_t bindex;
35354+ struct au_branch *br;
35355+};
35356+
35357+static void call_del_wh_children(void *args)
35358+{
35359+ struct del_wh_children_args *a = args;
1308ab2a 35360+ *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
1facf9fc 35361+}
35362+
35363+/* ---------------------------------------------------------------------- */
35364+
35365+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35366+{
35367+ struct au_whtmp_rmdir *whtmp;
dece6358 35368+ int err;
1308ab2a 35369+ unsigned int rdhash;
dece6358
AM
35370+
35371+ SiMustAnyLock(sb);
1facf9fc 35372+
be52b249 35373+ whtmp = kzalloc(sizeof(*whtmp), gfp);
dece6358
AM
35374+ if (unlikely(!whtmp)) {
35375+ whtmp = ERR_PTR(-ENOMEM);
1facf9fc 35376+ goto out;
dece6358 35377+ }
1facf9fc 35378+
1308ab2a 35379+ /* no estimation for dir size */
35380+ rdhash = au_sbi(sb)->si_rdhash;
35381+ if (!rdhash)
35382+ rdhash = AUFS_RDHASH_DEF;
35383+ err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35384+ if (unlikely(err)) {
1c60b727 35385+ kfree(whtmp);
1308ab2a 35386+ whtmp = ERR_PTR(err);
35387+ }
dece6358 35388+
4f0767ce 35389+out:
dece6358 35390+ return whtmp;
1facf9fc 35391+}
35392+
35393+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35394+{
027c5e7a 35395+ if (whtmp->br)
5afbbe0d 35396+ au_br_put(whtmp->br);
1facf9fc 35397+ dput(whtmp->wh_dentry);
35398+ iput(whtmp->dir);
dece6358 35399+ au_nhash_wh_free(&whtmp->whlist);
1c60b727 35400+ kfree(whtmp);
1facf9fc 35401+}
35402+
35403+/*
35404+ * rmdir the whiteouted temporary named dir @h_dentry.
35405+ * @whlist: whiteouted children.
35406+ */
35407+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35408+ struct dentry *wh_dentry, struct au_nhash *whlist)
35409+{
35410+ int err;
2000de60 35411+ unsigned int h_nlink;
1facf9fc 35412+ struct path h_tmp;
35413+ struct inode *wh_inode, *h_dir;
35414+ struct au_branch *br;
35415+
5527c038 35416+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
1facf9fc 35417+ IMustLock(h_dir);
35418+
35419+ br = au_sbr(dir->i_sb, bindex);
5527c038 35420+ wh_inode = d_inode(wh_dentry);
febd17d6 35421+ inode_lock_nested(wh_inode, AuLsc_I_CHILD);
1facf9fc 35422+
35423+ /*
35424+ * someone else might change some whiteouts while we were sleeping.
35425+ * it means this whlist may have an obsoleted entry.
35426+ */
35427+ if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
35428+ err = del_wh_children(wh_dentry, whlist, bindex, br);
35429+ else {
35430+ int wkq_err;
35431+ struct del_wh_children_args args = {
35432+ .errp = &err,
35433+ .h_dentry = wh_dentry,
1308ab2a 35434+ .whlist = whlist,
1facf9fc 35435+ .bindex = bindex,
35436+ .br = br
35437+ };
35438+
35439+ wkq_err = au_wkq_wait(call_del_wh_children, &args);
35440+ if (unlikely(wkq_err))
35441+ err = wkq_err;
35442+ }
febd17d6 35443+ inode_unlock(wh_inode);
1facf9fc 35444+
35445+ if (!err) {
35446+ h_tmp.dentry = wh_dentry;
86dc4139 35447+ h_tmp.mnt = au_br_mnt(br);
2000de60 35448+ h_nlink = h_dir->i_nlink;
1facf9fc 35449+ err = vfsub_rmdir(h_dir, &h_tmp);
2000de60
JR
35450+ /* some fs doesn't change the parent nlink in some cases */
35451+ h_nlink -= h_dir->i_nlink;
1facf9fc 35452+ }
35453+
35454+ if (!err) {
5afbbe0d 35455+ if (au_ibtop(dir) == bindex) {
7f207e10 35456+ /* todo: dir->i_mutex is necessary */
1facf9fc 35457+ au_cpup_attr_timesizes(dir);
2000de60
JR
35458+ if (h_nlink)
35459+ vfsub_drop_nlink(dir);
1facf9fc 35460+ }
35461+ return 0; /* success */
35462+ }
35463+
523b37e3 35464+ pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
1facf9fc 35465+ return err;
35466+}
35467+
35468+static void call_rmdir_whtmp(void *args)
35469+{
35470+ int err;
e49829fe 35471+ aufs_bindex_t bindex;
1facf9fc 35472+ struct au_whtmp_rmdir *a = args;
35473+ struct super_block *sb;
35474+ struct dentry *h_parent;
35475+ struct inode *h_dir;
1facf9fc 35476+ struct au_hinode *hdir;
35477+
35478+ /* rmdir by nfsd may cause deadlock with this i_mutex */
febd17d6 35479+ /* inode_lock(a->dir); */
e49829fe 35480+ err = -EROFS;
1facf9fc 35481+ sb = a->dir->i_sb;
e49829fe
JR
35482+ si_read_lock(sb, !AuLock_FLUSH);
35483+ if (!au_br_writable(a->br->br_perm))
35484+ goto out;
35485+ bindex = au_br_index(sb, a->br->br_id);
35486+ if (unlikely(bindex < 0))
1facf9fc 35487+ goto out;
35488+
35489+ err = -EIO;
1facf9fc 35490+ ii_write_lock_parent(a->dir);
35491+ h_parent = dget_parent(a->wh_dentry);
5527c038 35492+ h_dir = d_inode(h_parent);
e49829fe 35493+ hdir = au_hi(a->dir, bindex);
86dc4139
AM
35494+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
35495+ if (unlikely(err))
35496+ goto out_mnt;
5afbbe0d 35497+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
e49829fe
JR
35498+ err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
35499+ a->br);
86dc4139
AM
35500+ if (!err)
35501+ err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
5afbbe0d 35502+ au_hn_inode_unlock(hdir);
86dc4139
AM
35503+ vfsub_mnt_drop_write(au_br_mnt(a->br));
35504+
35505+out_mnt:
1facf9fc 35506+ dput(h_parent);
35507+ ii_write_unlock(a->dir);
4f0767ce 35508+out:
febd17d6 35509+ /* inode_unlock(a->dir); */
1facf9fc 35510+ au_whtmp_rmdir_free(a);
027c5e7a
AM
35511+ si_read_unlock(sb);
35512+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 35513+ if (unlikely(err))
35514+ AuIOErr("err %d\n", err);
35515+}
35516+
35517+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35518+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
35519+{
35520+ int wkq_err;
e49829fe 35521+ struct super_block *sb;
1facf9fc 35522+
35523+ IMustLock(dir);
35524+
35525+ /* all post-process will be done in do_rmdir_whtmp(). */
e49829fe 35526+ sb = dir->i_sb;
1facf9fc 35527+ args->dir = au_igrab(dir);
e49829fe 35528+ args->br = au_sbr(sb, bindex);
5afbbe0d 35529+ au_br_get(args->br);
1facf9fc 35530+ args->wh_dentry = dget(wh_dentry);
53392da6 35531+ wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
1facf9fc 35532+ if (unlikely(wkq_err)) {
523b37e3 35533+ pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
1facf9fc 35534+ au_whtmp_rmdir_free(args);
35535+ }
35536+}
7f207e10
AM
35537diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
35538--- /usr/share/empty/fs/aufs/whout.h 1970-01-01 01:00:00.000000000 +0100
3c1bdaff
AM
35539+++ linux/fs/aufs/whout.h 2017-09-05 10:42:11.058755349 +0200
35540@@ -0,0 +1,85 @@
1facf9fc 35541+/*
a2654f78 35542+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 35543+ *
35544+ * This program, aufs is free software; you can redistribute it and/or modify
35545+ * it under the terms of the GNU General Public License as published by
35546+ * the Free Software Foundation; either version 2 of the License, or
35547+ * (at your option) any later version.
dece6358
AM
35548+ *
35549+ * This program is distributed in the hope that it will be useful,
35550+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
35551+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35552+ * GNU General Public License for more details.
35553+ *
35554+ * You should have received a copy of the GNU General Public License
523b37e3 35555+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 35556+ */
35557+
35558+/*
35559+ * whiteout for logical deletion and opaque directory
35560+ */
35561+
35562+#ifndef __AUFS_WHOUT_H__
35563+#define __AUFS_WHOUT_H__
35564+
35565+#ifdef __KERNEL__
35566+
1facf9fc 35567+#include "dir.h"
35568+
35569+/* whout.c */
35570+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
076b876e
AM
35571+int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
35572+int au_diropq_test(struct dentry *h_dentry);
3c1bdaff 35573+struct au_branch;
1facf9fc 35574+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35575+ struct qstr *prefix);
35576+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
35577+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35578+ struct dentry *dentry);
86dc4139 35579+int au_wh_init(struct au_branch *br, struct super_block *sb);
1facf9fc 35580+
35581+/* diropq flags */
35582+#define AuDiropq_CREATE 1
35583+#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
7f207e10
AM
35584+#define au_fset_diropq(flags, name) \
35585+ do { (flags) |= AuDiropq_##name; } while (0)
35586+#define au_fclr_diropq(flags, name) \
35587+ do { (flags) &= ~AuDiropq_##name; } while (0)
1facf9fc 35588+
35589+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35590+ unsigned int flags);
35591+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35592+ struct au_branch *br);
35593+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35594+ struct dentry *h_parent);
35595+
35596+/* real rmdir for the whiteout-ed dir */
35597+struct au_whtmp_rmdir {
35598+ struct inode *dir;
e49829fe 35599+ struct au_branch *br;
1facf9fc 35600+ struct dentry *wh_dentry;
dece6358 35601+ struct au_nhash whlist;
1facf9fc 35602+};
35603+
35604+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
35605+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
35606+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35607+ struct dentry *wh_dentry, struct au_nhash *whlist);
35608+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35609+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
35610+
35611+/* ---------------------------------------------------------------------- */
35612+
35613+static inline struct dentry *au_diropq_create(struct dentry *dentry,
35614+ aufs_bindex_t bindex)
35615+{
35616+ return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
35617+}
35618+
35619+static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
35620+{
35621+ return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
35622+}
35623+
35624+#endif /* __KERNEL__ */
35625+#endif /* __AUFS_WHOUT_H__ */
7f207e10
AM
35626diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
35627--- /usr/share/empty/fs/aufs/wkq.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
35628+++ linux/fs/aufs/wkq.c 2017-11-12 22:24:44.707577830 +0100
35629@@ -0,0 +1,390 @@
1facf9fc 35630+/*
a2654f78 35631+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 35632+ *
35633+ * This program, aufs is free software; you can redistribute it and/or modify
35634+ * it under the terms of the GNU General Public License as published by
35635+ * the Free Software Foundation; either version 2 of the License, or
35636+ * (at your option) any later version.
dece6358
AM
35637+ *
35638+ * This program is distributed in the hope that it will be useful,
35639+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
35640+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35641+ * GNU General Public License for more details.
35642+ *
35643+ * You should have received a copy of the GNU General Public License
523b37e3 35644+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 35645+ */
35646+
35647+/*
35648+ * workqueue for asynchronous/super-io operations
35649+ * todo: try new dredential scheme
35650+ */
35651+
dece6358 35652+#include <linux/module.h>
1facf9fc 35653+#include "aufs.h"
35654+
9dbd164d 35655+/* internal workqueue named AUFS_WKQ_NAME */
b752ccd1 35656+
9dbd164d 35657+static struct workqueue_struct *au_wkq;
1facf9fc 35658+
35659+struct au_wkinfo {
35660+ struct work_struct wk;
7f207e10 35661+ struct kobject *kobj;
1facf9fc 35662+
35663+ unsigned int flags; /* see wkq.h */
35664+
35665+ au_wkq_func_t func;
35666+ void *args;
35667+
8b6a4947
AM
35668+#ifdef CONFIG_LOCKDEP
35669+ int dont_check;
35670+ struct held_lock **hlock;
35671+#endif
35672+
1facf9fc 35673+ struct completion *comp;
35674+};
35675+
35676+/* ---------------------------------------------------------------------- */
8b6a4947
AM
35677+/*
35678+ * Aufs passes some operations to the workqueue such as the internal copyup.
35679+ * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
35680+ * job run by workqueue depends upon the locks acquired in the other task.
35681+ * Delegating a small operation to the workqueue, aufs passes its lockdep
35682+ * information too. And the job in the workqueue restores the info in order to
35683+ * pretend as if it acquired those locks. This is just to make LOCKDEP work
35684+ * correctly and expectedly.
35685+ */
35686+
35687+#ifndef CONFIG_LOCKDEP
35688+AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
35689+AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
35690+AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
35691+AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
35692+AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
35693+#else
35694+static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
35695+{
35696+ wkinfo->hlock = NULL;
35697+ wkinfo->dont_check = 0;
35698+}
35699+
35700+/*
35701+ * 1: matched
35702+ * 0: unmatched
35703+ */
35704+static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
35705+{
35706+ static DEFINE_SPINLOCK(spin);
35707+ static struct {
35708+ char *name;
35709+ struct lock_class_key *key;
35710+ } a[] = {
35711+ { .name = "&sbinfo->si_rwsem" },
35712+ { .name = "&finfo->fi_rwsem" },
35713+ { .name = "&dinfo->di_rwsem" },
35714+ { .name = "&iinfo->ii_rwsem" }
35715+ };
35716+ static int set;
35717+ int i;
35718+
35719+ /* lockless read from 'set.' see below */
35720+ if (set == ARRAY_SIZE(a)) {
35721+ for (i = 0; i < ARRAY_SIZE(a); i++)
35722+ if (a[i].key == key)
35723+ goto match;
35724+ goto unmatch;
35725+ }
35726+
35727+ spin_lock(&spin);
35728+ if (set)
35729+ for (i = 0; i < ARRAY_SIZE(a); i++)
35730+ if (a[i].key == key) {
35731+ spin_unlock(&spin);
35732+ goto match;
35733+ }
35734+ for (i = 0; i < ARRAY_SIZE(a); i++) {
35735+ if (a[i].key) {
35736+ if (unlikely(a[i].key == key)) { /* rare but possible */
35737+ spin_unlock(&spin);
35738+ goto match;
35739+ } else
35740+ continue;
35741+ }
35742+ if (strstr(a[i].name, name)) {
35743+ /*
35744+ * the order of these three lines is important for the
35745+ * lockless read above.
35746+ */
35747+ a[i].key = key;
35748+ spin_unlock(&spin);
35749+ set++;
35750+ /* AuDbg("%d, %s\n", set, name); */
35751+ goto match;
35752+ }
35753+ }
35754+ spin_unlock(&spin);
35755+ goto unmatch;
35756+
35757+match:
35758+ return 1;
35759+unmatch:
35760+ return 0;
35761+}
35762+
35763+static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
35764+{
35765+ int err, n;
35766+ struct task_struct *curr;
35767+ struct held_lock **hl, *held_locks, *p;
35768+
35769+ err = 0;
35770+ curr = current;
35771+ wkinfo->dont_check = lockdep_recursing(curr);
35772+ if (wkinfo->dont_check)
35773+ goto out;
35774+ n = curr->lockdep_depth;
35775+ if (!n)
35776+ goto out;
35777+
35778+ err = -ENOMEM;
35779+ wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
35780+ if (unlikely(!wkinfo->hlock))
35781+ goto out;
35782+
35783+ err = 0;
35784+#if 0
35785+ if (0 && au_debug_test()) /* left for debugging */
35786+ lockdep_print_held_locks(curr);
35787+#endif
35788+ held_locks = curr->held_locks;
35789+ hl = wkinfo->hlock;
35790+ while (n--) {
35791+ p = held_locks++;
35792+ if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
35793+ *hl++ = p;
35794+ }
35795+ *hl = NULL;
35796+
35797+out:
35798+ return err;
35799+}
35800+
35801+static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
35802+{
35803+ kfree(wkinfo->hlock);
35804+}
35805+
35806+static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
35807+{
35808+ struct held_lock *p, **hl = wkinfo->hlock;
35809+ int subclass;
35810+
35811+ if (wkinfo->dont_check)
35812+ lockdep_off();
35813+ if (!hl)
35814+ return;
35815+ while ((p = *hl++)) { /* assignment */
35816+ subclass = lockdep_hlock_class(p)->subclass;
35817+ /* AuDbg("%s, %d\n", p->instance->name, subclass); */
35818+ if (p->read)
35819+ rwsem_acquire_read(p->instance, subclass, 0,
35820+ /*p->acquire_ip*/_RET_IP_);
35821+ else
35822+ rwsem_acquire(p->instance, subclass, 0,
35823+ /*p->acquire_ip*/_RET_IP_);
35824+ }
35825+}
35826+
35827+static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
35828+{
35829+ struct held_lock *p, **hl = wkinfo->hlock;
35830+
35831+ if (wkinfo->dont_check)
35832+ lockdep_on();
35833+ if (!hl)
35834+ return;
35835+ while ((p = *hl++)) /* assignment */
35836+ rwsem_release(p->instance, 0, /*p->acquire_ip*/_RET_IP_);
35837+}
35838+#endif
1facf9fc 35839+
1facf9fc 35840+static void wkq_func(struct work_struct *wk)
35841+{
35842+ struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
35843+
2dfbb274 35844+ AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
7f207e10
AM
35845+ AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
35846+
8b6a4947 35847+ au_wkq_lockdep_pre(wkinfo);
1facf9fc 35848+ wkinfo->func(wkinfo->args);
8b6a4947 35849+ au_wkq_lockdep_post(wkinfo);
1facf9fc 35850+ if (au_ftest_wkq(wkinfo->flags, WAIT))
35851+ complete(wkinfo->comp);
35852+ else {
7f207e10 35853+ kobject_put(wkinfo->kobj);
9dbd164d 35854+ module_put(THIS_MODULE); /* todo: ?? */
1c60b727 35855+ kfree(wkinfo);
1facf9fc 35856+ }
35857+}
35858+
35859+/*
35860+ * Since struct completion is large, try allocating it dynamically.
35861+ */
c2b27bf2 35862+#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
1facf9fc 35863+#define AuWkqCompDeclare(name) struct completion *comp = NULL
35864+
35865+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
35866+{
35867+ *comp = kmalloc(sizeof(**comp), GFP_NOFS);
35868+ if (*comp) {
35869+ init_completion(*comp);
35870+ wkinfo->comp = *comp;
35871+ return 0;
35872+ }
35873+ return -ENOMEM;
35874+}
35875+
35876+static void au_wkq_comp_free(struct completion *comp)
35877+{
1c60b727 35878+ kfree(comp);
1facf9fc 35879+}
35880+
35881+#else
35882+
35883+/* no braces */
35884+#define AuWkqCompDeclare(name) \
35885+ DECLARE_COMPLETION_ONSTACK(_ ## name); \
35886+ struct completion *comp = &_ ## name
35887+
35888+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
35889+{
35890+ wkinfo->comp = *comp;
35891+ return 0;
35892+}
35893+
35894+static void au_wkq_comp_free(struct completion *comp __maybe_unused)
35895+{
35896+ /* empty */
35897+}
35898+#endif /* 4KSTACKS */
35899+
53392da6 35900+static void au_wkq_run(struct au_wkinfo *wkinfo)
1facf9fc 35901+{
53392da6
AM
35902+ if (au_ftest_wkq(wkinfo->flags, NEST)) {
35903+ if (au_wkq_test()) {
38d290e6
JR
35904+ AuWarn1("wkq from wkq, unless silly-rename on NFS,"
35905+ " due to a dead dir by UDBA?\n");
53392da6
AM
35906+ AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
35907+ }
35908+ } else
35909+ au_dbg_verify_kthread();
35910+
35911+ if (au_ftest_wkq(wkinfo->flags, WAIT)) {
a1f66529 35912+ INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
9dbd164d 35913+ queue_work(au_wkq, &wkinfo->wk);
4a4d8108
AM
35914+ } else {
35915+ INIT_WORK(&wkinfo->wk, wkq_func);
35916+ schedule_work(&wkinfo->wk);
35917+ }
1facf9fc 35918+}
35919+
7f207e10
AM
35920+/*
35921+ * Be careful. It is easy to make deadlock happen.
35922+ * processA: lock, wkq and wait
35923+ * processB: wkq and wait, lock in wkq
35924+ * --> deadlock
35925+ */
b752ccd1 35926+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
1facf9fc 35927+{
35928+ int err;
35929+ AuWkqCompDeclare(comp);
35930+ struct au_wkinfo wkinfo = {
b752ccd1 35931+ .flags = flags,
1facf9fc 35932+ .func = func,
35933+ .args = args
35934+ };
35935+
35936+ err = au_wkq_comp_alloc(&wkinfo, &comp);
8b6a4947
AM
35937+ if (unlikely(err))
35938+ goto out;
35939+ err = au_wkq_lockdep_alloc(&wkinfo);
35940+ if (unlikely(err))
35941+ goto out_comp;
1facf9fc 35942+ if (!err) {
53392da6 35943+ au_wkq_run(&wkinfo);
1facf9fc 35944+ /* no timeout, no interrupt */
35945+ wait_for_completion(wkinfo.comp);
1facf9fc 35946+ }
8b6a4947 35947+ au_wkq_lockdep_free(&wkinfo);
1facf9fc 35948+
8b6a4947
AM
35949+out_comp:
35950+ au_wkq_comp_free(comp);
35951+out:
35952+ destroy_work_on_stack(&wkinfo.wk);
1facf9fc 35953+ return err;
1facf9fc 35954+}
35955+
027c5e7a
AM
35956+/*
35957+ * Note: dget/dput() in func for aufs dentries are not supported. It will be a
35958+ * problem in a concurrent umounting.
35959+ */
53392da6
AM
35960+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
35961+ unsigned int flags)
1facf9fc 35962+{
35963+ int err;
35964+ struct au_wkinfo *wkinfo;
35965+
f0c0a007 35966+ atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
1facf9fc 35967+
35968+ /*
35969+ * wkq_func() must free this wkinfo.
35970+ * it highly depends upon the implementation of workqueue.
35971+ */
35972+ err = 0;
35973+ wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
35974+ if (wkinfo) {
7f207e10 35975+ wkinfo->kobj = &au_sbi(sb)->si_kobj;
53392da6 35976+ wkinfo->flags = flags & ~AuWkq_WAIT;
1facf9fc 35977+ wkinfo->func = func;
35978+ wkinfo->args = args;
35979+ wkinfo->comp = NULL;
8b6a4947 35980+ au_wkq_lockdep_init(wkinfo);
7f207e10 35981+ kobject_get(wkinfo->kobj);
9dbd164d 35982+ __module_get(THIS_MODULE); /* todo: ?? */
1facf9fc 35983+
53392da6 35984+ au_wkq_run(wkinfo);
1facf9fc 35985+ } else {
35986+ err = -ENOMEM;
e49829fe 35987+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 35988+ }
35989+
35990+ return err;
35991+}
35992+
35993+/* ---------------------------------------------------------------------- */
35994+
35995+void au_nwt_init(struct au_nowait_tasks *nwt)
35996+{
f0c0a007
AM
35997+ atomic_set(&nwt->nw_len, 0);
35998+ /* smp_mb(); */ /* atomic_set */
1facf9fc 35999+ init_waitqueue_head(&nwt->nw_wq);
36000+}
36001+
36002+void au_wkq_fin(void)
36003+{
9dbd164d 36004+ destroy_workqueue(au_wkq);
1facf9fc 36005+}
36006+
36007+int __init au_wkq_init(void)
36008+{
9dbd164d 36009+ int err;
b752ccd1
AM
36010+
36011+ err = 0;
86dc4139 36012+ au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
9dbd164d
AM
36013+ if (IS_ERR(au_wkq))
36014+ err = PTR_ERR(au_wkq);
36015+ else if (!au_wkq)
36016+ err = -ENOMEM;
b752ccd1
AM
36017+
36018+ return err;
1facf9fc 36019+}
7f207e10
AM
36020diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36021--- /usr/share/empty/fs/aufs/wkq.h 1970-01-01 01:00:00.000000000 +0100
8b6a4947 36022+++ linux/fs/aufs/wkq.h 2017-11-12 22:24:44.707577830 +0100
f0c0a007 36023@@ -0,0 +1,93 @@
1facf9fc 36024+/*
a2654f78 36025+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 36026+ *
36027+ * This program, aufs is free software; you can redistribute it and/or modify
36028+ * it under the terms of the GNU General Public License as published by
36029+ * the Free Software Foundation; either version 2 of the License, or
36030+ * (at your option) any later version.
dece6358
AM
36031+ *
36032+ * This program is distributed in the hope that it will be useful,
36033+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36034+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36035+ * GNU General Public License for more details.
36036+ *
36037+ * You should have received a copy of the GNU General Public License
523b37e3 36038+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36039+ */
36040+
36041+/*
36042+ * workqueue for asynchronous/super-io operations
36043+ * todo: try new credentials management scheme
36044+ */
36045+
36046+#ifndef __AUFS_WKQ_H__
36047+#define __AUFS_WKQ_H__
36048+
36049+#ifdef __KERNEL__
36050+
8b6a4947 36051+#include <linux/wait.h>
5afbbe0d 36052+
dece6358
AM
36053+struct super_block;
36054+
1facf9fc 36055+/* ---------------------------------------------------------------------- */
36056+
36057+/*
36058+ * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36059+ */
36060+struct au_nowait_tasks {
f0c0a007 36061+ atomic_t nw_len;
1facf9fc 36062+ wait_queue_head_t nw_wq;
36063+};
36064+
36065+/* ---------------------------------------------------------------------- */
36066+
36067+typedef void (*au_wkq_func_t)(void *args);
36068+
36069+/* wkq flags */
36070+#define AuWkq_WAIT 1
9dbd164d 36071+#define AuWkq_NEST (1 << 1)
1facf9fc 36072+#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
7f207e10
AM
36073+#define au_fset_wkq(flags, name) \
36074+ do { (flags) |= AuWkq_##name; } while (0)
36075+#define au_fclr_wkq(flags, name) \
36076+ do { (flags) &= ~AuWkq_##name; } while (0)
1facf9fc 36077+
9dbd164d
AM
36078+#ifndef CONFIG_AUFS_HNOTIFY
36079+#undef AuWkq_NEST
36080+#define AuWkq_NEST 0
36081+#endif
36082+
1facf9fc 36083+/* wkq.c */
b752ccd1 36084+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
53392da6
AM
36085+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36086+ unsigned int flags);
1facf9fc 36087+void au_nwt_init(struct au_nowait_tasks *nwt);
36088+int __init au_wkq_init(void);
36089+void au_wkq_fin(void);
36090+
36091+/* ---------------------------------------------------------------------- */
36092+
53392da6
AM
36093+static inline int au_wkq_test(void)
36094+{
36095+ return current->flags & PF_WQ_WORKER;
36096+}
36097+
b752ccd1 36098+static inline int au_wkq_wait(au_wkq_func_t func, void *args)
1facf9fc 36099+{
b752ccd1 36100+ return au_wkq_do_wait(AuWkq_WAIT, func, args);
1facf9fc 36101+}
36102+
36103+static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36104+{
f0c0a007 36105+ if (atomic_dec_and_test(&nwt->nw_len))
1facf9fc 36106+ wake_up_all(&nwt->nw_wq);
36107+}
36108+
36109+static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36110+{
f0c0a007 36111+ wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
1facf9fc 36112+ return 0;
36113+}
36114+
36115+#endif /* __KERNEL__ */
36116+#endif /* __AUFS_WKQ_H__ */
c1595e42
JR
36117diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36118--- /usr/share/empty/fs/aufs/xattr.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
36119+++ linux/fs/aufs/xattr.c 2017-11-12 22:24:44.707577830 +0100
36120@@ -0,0 +1,355 @@
c1595e42 36121+/*
a2654f78 36122+ * Copyright (C) 2014-2017 Junjiro R. Okajima
c1595e42
JR
36123+ *
36124+ * This program, aufs is free software; you can redistribute it and/or modify
36125+ * it under the terms of the GNU General Public License as published by
36126+ * the Free Software Foundation; either version 2 of the License, or
36127+ * (at your option) any later version.
36128+ *
36129+ * This program is distributed in the hope that it will be useful,
36130+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36131+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36132+ * GNU General Public License for more details.
36133+ *
36134+ * You should have received a copy of the GNU General Public License
36135+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
36136+ */
36137+
36138+/*
36139+ * handling xattr functions
36140+ */
36141+
a2654f78
AM
36142+#include <linux/fs.h>
36143+#include <linux/posix_acl_xattr.h>
c1595e42
JR
36144+#include <linux/xattr.h>
36145+#include "aufs.h"
36146+
36147+static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36148+{
36149+ if (!ignore_flags)
36150+ goto out;
36151+ switch (err) {
36152+ case -ENOMEM:
36153+ case -EDQUOT:
36154+ goto out;
36155+ }
36156+
36157+ if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36158+ err = 0;
36159+ goto out;
36160+ }
36161+
36162+#define cmp(brattr, prefix) do { \
36163+ if (!strncmp(name, XATTR_##prefix##_PREFIX, \
36164+ XATTR_##prefix##_PREFIX_LEN)) { \
36165+ if (ignore_flags & AuBrAttr_ICEX_##brattr) \
36166+ err = 0; \
36167+ goto out; \
36168+ } \
36169+ } while (0)
36170+
36171+ cmp(SEC, SECURITY);
36172+ cmp(SYS, SYSTEM);
36173+ cmp(TR, TRUSTED);
36174+ cmp(USR, USER);
36175+#undef cmp
36176+
36177+ if (ignore_flags & AuBrAttr_ICEX_OTH)
36178+ err = 0;
36179+
36180+out:
36181+ return err;
36182+}
36183+
36184+static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36185+
36186+static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
7e9cd9fe
AM
36187+ char *name, char **buf, unsigned int ignore_flags,
36188+ unsigned int verbose)
c1595e42
JR
36189+{
36190+ int err;
36191+ ssize_t ssz;
36192+ struct inode *h_idst;
36193+
36194+ ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
36195+ err = ssz;
36196+ if (unlikely(err <= 0)) {
c1595e42
JR
36197+ if (err == -ENODATA
36198+ || (err == -EOPNOTSUPP
b912730e 36199+ && ((ignore_flags & au_xattr_out_of_list)
5527c038 36200+ || (au_test_nfs_noacl(d_inode(h_src))
b912730e
AM
36201+ && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36202+ || !strcmp(name,
36203+ XATTR_NAME_POSIX_ACL_DEFAULT))))
36204+ ))
c1595e42 36205+ err = 0;
b912730e
AM
36206+ if (err && (verbose || au_debug_test()))
36207+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
36208+ goto out;
36209+ }
36210+
36211+ /* unlock it temporary */
5527c038 36212+ h_idst = d_inode(h_dst);
febd17d6 36213+ inode_unlock(h_idst);
c1595e42 36214+ err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
febd17d6 36215+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42 36216+ if (unlikely(err)) {
7e9cd9fe
AM
36217+ if (verbose || au_debug_test())
36218+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
36219+ err = au_xattr_ignore(err, name, ignore_flags);
36220+ }
36221+
36222+out:
36223+ return err;
36224+}
36225+
7e9cd9fe
AM
36226+int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
36227+ unsigned int verbose)
c1595e42
JR
36228+{
36229+ int err, unlocked, acl_access, acl_default;
36230+ ssize_t ssz;
36231+ struct inode *h_isrc, *h_idst;
36232+ char *value, *p, *o, *e;
36233+
36234+ /* try stopping to update the source inode while we are referencing */
7e9cd9fe 36235+ /* there should not be the parent-child relationship between them */
5527c038
JR
36236+ h_isrc = d_inode(h_src);
36237+ h_idst = d_inode(h_dst);
febd17d6 36238+ inode_unlock(h_idst);
3c1bdaff 36239+ vfsub_inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
febd17d6 36240+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42
JR
36241+ unlocked = 0;
36242+
36243+ /* some filesystems don't list POSIX ACL, for example tmpfs */
36244+ ssz = vfs_listxattr(h_src, NULL, 0);
36245+ err = ssz;
36246+ if (unlikely(err < 0)) {
36247+ AuTraceErr(err);
36248+ if (err == -ENODATA
36249+ || err == -EOPNOTSUPP)
36250+ err = 0; /* ignore */
36251+ goto out;
36252+ }
36253+
36254+ err = 0;
36255+ p = NULL;
36256+ o = NULL;
36257+ if (ssz) {
36258+ err = -ENOMEM;
36259+ p = kmalloc(ssz, GFP_NOFS);
36260+ o = p;
36261+ if (unlikely(!p))
36262+ goto out;
36263+ err = vfs_listxattr(h_src, p, ssz);
36264+ }
3c1bdaff 36265+ inode_unlock_shared(h_isrc);
c1595e42
JR
36266+ unlocked = 1;
36267+ AuDbg("err %d, ssz %zd\n", err, ssz);
36268+ if (unlikely(err < 0))
36269+ goto out_free;
36270+
36271+ err = 0;
36272+ e = p + ssz;
36273+ value = NULL;
36274+ acl_access = 0;
36275+ acl_default = 0;
36276+ while (!err && p < e) {
36277+ acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36278+ sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36279+ acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36280+ sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36281+ - 1);
7e9cd9fe
AM
36282+ err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36283+ verbose);
c1595e42
JR
36284+ p += strlen(p) + 1;
36285+ }
36286+ AuTraceErr(err);
36287+ ignore_flags |= au_xattr_out_of_list;
36288+ if (!err && !acl_access) {
36289+ err = au_do_cpup_xattr(h_dst, h_src,
36290+ XATTR_NAME_POSIX_ACL_ACCESS, &value,
7e9cd9fe 36291+ ignore_flags, verbose);
c1595e42
JR
36292+ AuTraceErr(err);
36293+ }
36294+ if (!err && !acl_default) {
36295+ err = au_do_cpup_xattr(h_dst, h_src,
36296+ XATTR_NAME_POSIX_ACL_DEFAULT, &value,
7e9cd9fe 36297+ ignore_flags, verbose);
c1595e42
JR
36298+ AuTraceErr(err);
36299+ }
36300+
8b6a4947 36301+ kfree(value);
c1595e42
JR
36302+
36303+out_free:
8b6a4947 36304+ kfree(o);
c1595e42
JR
36305+out:
36306+ if (!unlocked)
3c1bdaff 36307+ inode_unlock_shared(h_isrc);
c1595e42
JR
36308+ AuTraceErr(err);
36309+ return err;
36310+}
36311+
36312+/* ---------------------------------------------------------------------- */
36313+
a2654f78
AM
36314+static int au_smack_reentering(struct super_block *sb)
36315+{
36316+#if IS_ENABLED(CONFIG_SECURITY_SMACK)
36317+ /*
36318+ * as a part of lookup, smack_d_instantiate() is called, and it calls
36319+ * i_op->getxattr(). ouch.
36320+ */
36321+ return si_pid_test(sb);
36322+#else
36323+ return 0;
36324+#endif
36325+}
36326+
c1595e42
JR
36327+enum {
36328+ AU_XATTR_LIST,
36329+ AU_XATTR_GET
36330+};
36331+
36332+struct au_lgxattr {
36333+ int type;
36334+ union {
36335+ struct {
36336+ char *list;
36337+ size_t size;
36338+ } list;
36339+ struct {
36340+ const char *name;
36341+ void *value;
36342+ size_t size;
36343+ } get;
36344+ } u;
36345+};
36346+
36347+static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
36348+{
36349+ ssize_t err;
a2654f78 36350+ int reenter;
c1595e42
JR
36351+ struct path h_path;
36352+ struct super_block *sb;
36353+
36354+ sb = dentry->d_sb;
a2654f78
AM
36355+ reenter = au_smack_reentering(sb);
36356+ if (!reenter) {
36357+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36358+ if (unlikely(err))
36359+ goto out;
36360+ }
36361+ err = au_h_path_getattr(dentry, /*force*/1, &h_path, reenter);
c1595e42
JR
36362+ if (unlikely(err))
36363+ goto out_si;
36364+ if (unlikely(!h_path.dentry))
36365+ /* illegally overlapped or something */
36366+ goto out_di; /* pretending success */
36367+
36368+ /* always topmost entry only */
36369+ switch (arg->type) {
36370+ case AU_XATTR_LIST:
36371+ err = vfs_listxattr(h_path.dentry,
36372+ arg->u.list.list, arg->u.list.size);
36373+ break;
36374+ case AU_XATTR_GET:
5afbbe0d 36375+ AuDebugOn(d_is_negative(h_path.dentry));
c1595e42
JR
36376+ err = vfs_getxattr(h_path.dentry,
36377+ arg->u.get.name, arg->u.get.value,
36378+ arg->u.get.size);
36379+ break;
36380+ }
36381+
36382+out_di:
a2654f78
AM
36383+ if (!reenter)
36384+ di_read_unlock(dentry, AuLock_IR);
c1595e42 36385+out_si:
a2654f78
AM
36386+ if (!reenter)
36387+ si_read_unlock(sb);
c1595e42
JR
36388+out:
36389+ AuTraceErr(err);
36390+ return err;
36391+}
36392+
36393+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36394+{
36395+ struct au_lgxattr arg = {
36396+ .type = AU_XATTR_LIST,
36397+ .u.list = {
36398+ .list = list,
36399+ .size = size
36400+ },
36401+ };
36402+
36403+ return au_lgxattr(dentry, &arg);
36404+}
36405+
f2c43d5f
AM
36406+static ssize_t au_getxattr(struct dentry *dentry,
36407+ struct inode *inode __maybe_unused,
36408+ const char *name, void *value, size_t size)
c1595e42
JR
36409+{
36410+ struct au_lgxattr arg = {
36411+ .type = AU_XATTR_GET,
36412+ .u.get = {
36413+ .name = name,
36414+ .value = value,
36415+ .size = size
36416+ },
36417+ };
36418+
36419+ return au_lgxattr(dentry, &arg);
36420+}
36421+
f2c43d5f
AM
36422+static int au_setxattr(struct dentry *dentry, struct inode *inode,
36423+ const char *name, const void *value, size_t size,
36424+ int flags)
c1595e42 36425+{
f2c43d5f 36426+ struct au_sxattr arg = {
c1595e42
JR
36427+ .type = AU_XATTR_SET,
36428+ .u.set = {
36429+ .name = name,
36430+ .value = value,
36431+ .size = size,
36432+ .flags = flags
36433+ },
36434+ };
36435+
f2c43d5f 36436+ return au_sxattr(dentry, inode, &arg);
c1595e42
JR
36437+}
36438+
36439+/* ---------------------------------------------------------------------- */
36440+
f2c43d5f
AM
36441+static int au_xattr_get(const struct xattr_handler *handler,
36442+ struct dentry *dentry, struct inode *inode,
36443+ const char *name, void *buffer, size_t size)
c1595e42 36444+{
f2c43d5f 36445+ return au_getxattr(dentry, inode, name, buffer, size);
c1595e42
JR
36446+}
36447+
f2c43d5f
AM
36448+static int au_xattr_set(const struct xattr_handler *handler,
36449+ struct dentry *dentry, struct inode *inode,
36450+ const char *name, const void *value, size_t size,
36451+ int flags)
c1595e42 36452+{
f2c43d5f 36453+ return au_setxattr(dentry, inode, name, value, size, flags);
c1595e42
JR
36454+}
36455+
36456+static const struct xattr_handler au_xattr_handler = {
f2c43d5f
AM
36457+ .name = "",
36458+ .prefix = "",
c1595e42
JR
36459+ .get = au_xattr_get,
36460+ .set = au_xattr_set
c1595e42
JR
36461+};
36462+
36463+static const struct xattr_handler *au_xattr_handlers[] = {
a2654f78
AM
36464+#ifdef CONFIG_FS_POSIX_ACL
36465+ &posix_acl_access_xattr_handler,
36466+ &posix_acl_default_xattr_handler,
36467+#endif
36468+ &au_xattr_handler, /* must be last */
f2c43d5f 36469+ NULL
c1595e42
JR
36470+};
36471+
36472+void au_xattr_init(struct super_block *sb)
36473+{
f2c43d5f 36474+ sb->s_xattr = au_xattr_handlers;
c1595e42 36475+}
7f207e10
AM
36476diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
36477--- /usr/share/empty/fs/aufs/xino.c 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
36478+++ linux/fs/aufs/xino.c 2017-11-12 22:24:44.710911257 +0100
36479@@ -0,0 +1,1418 @@
1facf9fc 36480+/*
a2654f78 36481+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 36482+ *
36483+ * This program, aufs is free software; you can redistribute it and/or modify
36484+ * it under the terms of the GNU General Public License as published by
36485+ * the Free Software Foundation; either version 2 of the License, or
36486+ * (at your option) any later version.
dece6358
AM
36487+ *
36488+ * This program is distributed in the hope that it will be useful,
36489+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36490+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36491+ * GNU General Public License for more details.
36492+ *
36493+ * You should have received a copy of the GNU General Public License
523b37e3 36494+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36495+ */
36496+
36497+/*
36498+ * external inode number translation table and bitmap
36499+ */
36500+
36501+#include <linux/seq_file.h>
392086de 36502+#include <linux/statfs.h>
1facf9fc 36503+#include "aufs.h"
36504+
9dbd164d 36505+/* todo: unnecessary to support mmap_sem since kernel-space? */
5527c038 36506+ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
1facf9fc 36507+ loff_t *pos)
36508+{
36509+ ssize_t err;
36510+ mm_segment_t oldfs;
b752ccd1
AM
36511+ union {
36512+ void *k;
36513+ char __user *u;
36514+ } buf;
1facf9fc 36515+
b752ccd1 36516+ buf.k = kbuf;
1facf9fc 36517+ oldfs = get_fs();
36518+ set_fs(KERNEL_DS);
36519+ do {
36520+ /* todo: signal_pending? */
b752ccd1 36521+ err = func(file, buf.u, size, pos);
1facf9fc 36522+ } while (err == -EAGAIN || err == -EINTR);
36523+ set_fs(oldfs);
36524+
36525+#if 0 /* reserved for future use */
36526+ if (err > 0)
2000de60 36527+ fsnotify_access(file->f_path.dentry);
1facf9fc 36528+#endif
36529+
36530+ return err;
36531+}
36532+
36533+/* ---------------------------------------------------------------------- */
36534+
be52b249
AM
36535+static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
36536+ size_t size, loff_t *pos);
36537+
5527c038 36538+static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
1facf9fc 36539+ size_t size, loff_t *pos)
36540+{
36541+ ssize_t err;
36542+ mm_segment_t oldfs;
b752ccd1
AM
36543+ union {
36544+ void *k;
36545+ const char __user *u;
36546+ } buf;
be52b249
AM
36547+ int i;
36548+ const int prevent_endless = 10;
1facf9fc 36549+
be52b249 36550+ i = 0;
b752ccd1 36551+ buf.k = kbuf;
1facf9fc 36552+ oldfs = get_fs();
36553+ set_fs(KERNEL_DS);
1facf9fc 36554+ do {
b752ccd1 36555+ err = func(file, buf.u, size, pos);
be52b249
AM
36556+ if (err == -EINTR
36557+ && !au_wkq_test()
36558+ && fatal_signal_pending(current)) {
36559+ set_fs(oldfs);
36560+ err = xino_fwrite_wkq(func, file, kbuf, size, pos);
36561+ BUG_ON(err == -EINTR);
36562+ oldfs = get_fs();
36563+ set_fs(KERNEL_DS);
36564+ }
36565+ } while (i++ < prevent_endless
36566+ && (err == -EAGAIN || err == -EINTR));
1facf9fc 36567+ set_fs(oldfs);
36568+
36569+#if 0 /* reserved for future use */
36570+ if (err > 0)
2000de60 36571+ fsnotify_modify(file->f_path.dentry);
1facf9fc 36572+#endif
36573+
36574+ return err;
36575+}
36576+
36577+struct do_xino_fwrite_args {
36578+ ssize_t *errp;
5527c038 36579+ vfs_writef_t func;
1facf9fc 36580+ struct file *file;
36581+ void *buf;
36582+ size_t size;
36583+ loff_t *pos;
36584+};
36585+
36586+static void call_do_xino_fwrite(void *args)
36587+{
36588+ struct do_xino_fwrite_args *a = args;
36589+ *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
36590+}
36591+
be52b249
AM
36592+static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
36593+ size_t size, loff_t *pos)
36594+{
36595+ ssize_t err;
36596+ int wkq_err;
36597+ struct do_xino_fwrite_args args = {
36598+ .errp = &err,
36599+ .func = func,
36600+ .file = file,
36601+ .buf = buf,
36602+ .size = size,
36603+ .pos = pos
36604+ };
36605+
36606+ /*
36607+ * it breaks RLIMIT_FSIZE and normal user's limit,
36608+ * users should care about quota and real 'filesystem full.'
36609+ */
36610+ wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
36611+ if (unlikely(wkq_err))
36612+ err = wkq_err;
36613+
36614+ return err;
36615+}
36616+
5527c038
JR
36617+ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
36618+ size_t size, loff_t *pos)
1facf9fc 36619+{
36620+ ssize_t err;
36621+
b752ccd1
AM
36622+ if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
36623+ lockdep_off();
36624+ err = do_xino_fwrite(func, file, buf, size, pos);
36625+ lockdep_on();
8b6a4947
AM
36626+ } else {
36627+ lockdep_off();
be52b249 36628+ err = xino_fwrite_wkq(func, file, buf, size, pos);
8b6a4947
AM
36629+ lockdep_on();
36630+ }
1facf9fc 36631+
36632+ return err;
36633+}
36634+
36635+/* ---------------------------------------------------------------------- */
36636+
36637+/*
36638+ * create a new xinofile at the same place/path as @base_file.
36639+ */
36640+struct file *au_xino_create2(struct file *base_file, struct file *copy_src)
36641+{
36642+ struct file *file;
4a4d8108 36643+ struct dentry *base, *parent;
523b37e3 36644+ struct inode *dir, *delegated;
1facf9fc 36645+ struct qstr *name;
1308ab2a 36646+ struct path path;
4a4d8108 36647+ int err;
1facf9fc 36648+
2000de60 36649+ base = base_file->f_path.dentry;
1facf9fc 36650+ parent = base->d_parent; /* dir inode is locked */
5527c038 36651+ dir = d_inode(parent);
1facf9fc 36652+ IMustLock(dir);
36653+
36654+ file = ERR_PTR(-EINVAL);
36655+ name = &base->d_name;
4a4d8108
AM
36656+ path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
36657+ if (IS_ERR(path.dentry)) {
36658+ file = (void *)path.dentry;
523b37e3
AM
36659+ pr_err("%pd lookup err %ld\n",
36660+ base, PTR_ERR(path.dentry));
1facf9fc 36661+ goto out;
36662+ }
36663+
36664+ /* no need to mnt_want_write() since we call dentry_open() later */
4a4d8108 36665+ err = vfs_create(dir, path.dentry, S_IRUGO | S_IWUGO, NULL);
1facf9fc 36666+ if (unlikely(err)) {
36667+ file = ERR_PTR(err);
523b37e3 36668+ pr_err("%pd create err %d\n", base, err);
1facf9fc 36669+ goto out_dput;
36670+ }
36671+
c06a8ce3 36672+ path.mnt = base_file->f_path.mnt;
4a4d8108 36673+ file = vfsub_dentry_open(&path,
7f207e10 36674+ O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
2cbb1c4b 36675+ /* | __FMODE_NONOTIFY */);
1facf9fc 36676+ if (IS_ERR(file)) {
523b37e3 36677+ pr_err("%pd open err %ld\n", base, PTR_ERR(file));
1facf9fc 36678+ goto out_dput;
36679+ }
36680+
523b37e3
AM
36681+ delegated = NULL;
36682+ err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
36683+ if (unlikely(err == -EWOULDBLOCK)) {
36684+ pr_warn("cannot retry for NFSv4 delegation"
36685+ " for an internal unlink\n");
36686+ iput(delegated);
36687+ }
1facf9fc 36688+ if (unlikely(err)) {
523b37e3 36689+ pr_err("%pd unlink err %d\n", base, err);
1facf9fc 36690+ goto out_fput;
36691+ }
36692+
36693+ if (copy_src) {
36694+ /* no one can touch copy_src xino */
c06a8ce3 36695+ err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
1facf9fc 36696+ if (unlikely(err)) {
523b37e3 36697+ pr_err("%pd copy err %d\n", base, err);
1facf9fc 36698+ goto out_fput;
36699+ }
36700+ }
36701+ goto out_dput; /* success */
36702+
4f0767ce 36703+out_fput:
1facf9fc 36704+ fput(file);
36705+ file = ERR_PTR(err);
4f0767ce 36706+out_dput:
4a4d8108 36707+ dput(path.dentry);
4f0767ce 36708+out:
1facf9fc 36709+ return file;
36710+}
36711+
36712+struct au_xino_lock_dir {
36713+ struct au_hinode *hdir;
36714+ struct dentry *parent;
febd17d6 36715+ struct inode *dir;
1facf9fc 36716+};
36717+
36718+static void au_xino_lock_dir(struct super_block *sb, struct file *xino,
36719+ struct au_xino_lock_dir *ldir)
36720+{
36721+ aufs_bindex_t brid, bindex;
36722+
36723+ ldir->hdir = NULL;
36724+ bindex = -1;
36725+ brid = au_xino_brid(sb);
36726+ if (brid >= 0)
36727+ bindex = au_br_index(sb, brid);
36728+ if (bindex >= 0) {
5527c038 36729+ ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 36730+ au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
1facf9fc 36731+ } else {
2000de60 36732+ ldir->parent = dget_parent(xino->f_path.dentry);
febd17d6
JR
36733+ ldir->dir = d_inode(ldir->parent);
36734+ inode_lock_nested(ldir->dir, AuLsc_I_PARENT);
1facf9fc 36735+ }
36736+}
36737+
36738+static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
36739+{
36740+ if (ldir->hdir)
5afbbe0d 36741+ au_hn_inode_unlock(ldir->hdir);
1facf9fc 36742+ else {
febd17d6 36743+ inode_unlock(ldir->dir);
1facf9fc 36744+ dput(ldir->parent);
36745+ }
36746+}
36747+
36748+/* ---------------------------------------------------------------------- */
36749+
36750+/* trucate xino files asynchronously */
36751+
36752+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
36753+{
36754+ int err;
392086de
AM
36755+ unsigned long jiffy;
36756+ blkcnt_t blocks;
5afbbe0d 36757+ aufs_bindex_t bi, bbot;
392086de 36758+ struct kstatfs *st;
1facf9fc 36759+ struct au_branch *br;
36760+ struct file *new_xino, *file;
36761+ struct super_block *h_sb;
36762+ struct au_xino_lock_dir ldir;
36763+
392086de 36764+ err = -ENOMEM;
be52b249 36765+ st = kmalloc(sizeof(*st), GFP_NOFS);
392086de
AM
36766+ if (unlikely(!st))
36767+ goto out;
36768+
1facf9fc 36769+ err = -EINVAL;
5afbbe0d
AM
36770+ bbot = au_sbbot(sb);
36771+ if (unlikely(bindex < 0 || bbot < bindex))
392086de 36772+ goto out_st;
1facf9fc 36773+ br = au_sbr(sb, bindex);
36774+ file = br->br_xino.xi_file;
36775+ if (!file)
392086de
AM
36776+ goto out_st;
36777+
36778+ err = vfs_statfs(&file->f_path, st);
36779+ if (unlikely(err))
36780+ AuErr1("statfs err %d, ignored\n", err);
36781+ jiffy = jiffies;
36782+ blocks = file_inode(file)->i_blocks;
36783+ pr_info("begin truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
36784+ bindex, (u64)blocks, st->f_bfree, st->f_blocks);
1facf9fc 36785+
36786+ au_xino_lock_dir(sb, file, &ldir);
36787+ /* mnt_want_write() is unnecessary here */
36788+ new_xino = au_xino_create2(file, file);
36789+ au_xino_unlock_dir(&ldir);
36790+ err = PTR_ERR(new_xino);
392086de
AM
36791+ if (IS_ERR(new_xino)) {
36792+ pr_err("err %d, ignored\n", err);
36793+ goto out_st;
36794+ }
1facf9fc 36795+ err = 0;
36796+ fput(file);
36797+ br->br_xino.xi_file = new_xino;
36798+
86dc4139 36799+ h_sb = au_br_sb(br);
5afbbe0d 36800+ for (bi = 0; bi <= bbot; bi++) {
1facf9fc 36801+ if (unlikely(bi == bindex))
36802+ continue;
36803+ br = au_sbr(sb, bi);
86dc4139 36804+ if (au_br_sb(br) != h_sb)
1facf9fc 36805+ continue;
36806+
36807+ fput(br->br_xino.xi_file);
36808+ br->br_xino.xi_file = new_xino;
36809+ get_file(new_xino);
36810+ }
36811+
392086de
AM
36812+ err = vfs_statfs(&new_xino->f_path, st);
36813+ if (!err) {
36814+ pr_info("end truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
36815+ bindex, (u64)file_inode(new_xino)->i_blocks,
36816+ st->f_bfree, st->f_blocks);
36817+ if (file_inode(new_xino)->i_blocks < blocks)
36818+ au_sbi(sb)->si_xino_jiffy = jiffy;
36819+ } else
36820+ AuErr1("statfs err %d, ignored\n", err);
36821+
36822+out_st:
1c60b727 36823+ kfree(st);
4f0767ce 36824+out:
1facf9fc 36825+ return err;
36826+}
36827+
36828+struct xino_do_trunc_args {
36829+ struct super_block *sb;
36830+ struct au_branch *br;
36831+};
36832+
36833+static void xino_do_trunc(void *_args)
36834+{
36835+ struct xino_do_trunc_args *args = _args;
36836+ struct super_block *sb;
36837+ struct au_branch *br;
36838+ struct inode *dir;
36839+ int err;
36840+ aufs_bindex_t bindex;
36841+
36842+ err = 0;
36843+ sb = args->sb;
5527c038 36844+ dir = d_inode(sb->s_root);
1facf9fc 36845+ br = args->br;
36846+
36847+ si_noflush_write_lock(sb);
36848+ ii_read_lock_parent(dir);
36849+ bindex = au_br_index(sb, br->br_id);
36850+ err = au_xino_trunc(sb, bindex);
1facf9fc 36851+ ii_read_unlock(dir);
36852+ if (unlikely(err))
392086de 36853+ pr_warn("err b%d, (%d)\n", bindex, err);
1facf9fc 36854+ atomic_dec(&br->br_xino_running);
5afbbe0d 36855+ au_br_put(br);
1facf9fc 36856+ si_write_unlock(sb);
027c5e7a 36857+ au_nwt_done(&au_sbi(sb)->si_nowait);
1c60b727 36858+ kfree(args);
1facf9fc 36859+}
36860+
392086de
AM
36861+static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
36862+{
36863+ int err;
36864+ struct kstatfs st;
36865+ struct au_sbinfo *sbinfo;
36866+
36867+ /* todo: si_xino_expire and the ratio should be customizable */
36868+ sbinfo = au_sbi(sb);
36869+ if (time_before(jiffies,
36870+ sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
36871+ return 0;
36872+
36873+ /* truncation border */
36874+ err = vfs_statfs(&br->br_xino.xi_file->f_path, &st);
36875+ if (unlikely(err)) {
36876+ AuErr1("statfs err %d, ignored\n", err);
36877+ return 0;
36878+ }
36879+ if (div64_u64(st.f_bfree * 100, st.f_blocks) >= AUFS_XINO_DEF_TRUNC)
36880+ return 0;
36881+
36882+ return 1;
36883+}
36884+
1facf9fc 36885+static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
36886+{
36887+ struct xino_do_trunc_args *args;
36888+ int wkq_err;
36889+
392086de 36890+ if (!xino_trunc_test(sb, br))
1facf9fc 36891+ return;
36892+
36893+ if (atomic_inc_return(&br->br_xino_running) > 1)
36894+ goto out;
36895+
36896+ /* lock and kfree() will be called in trunc_xino() */
36897+ args = kmalloc(sizeof(*args), GFP_NOFS);
36898+ if (unlikely(!args)) {
36899+ AuErr1("no memory\n");
f0c0a007 36900+ goto out;
1facf9fc 36901+ }
36902+
5afbbe0d 36903+ au_br_get(br);
1facf9fc 36904+ args->sb = sb;
36905+ args->br = br;
53392da6 36906+ wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
1facf9fc 36907+ if (!wkq_err)
36908+ return; /* success */
36909+
4a4d8108 36910+ pr_err("wkq %d\n", wkq_err);
5afbbe0d 36911+ au_br_put(br);
1c60b727 36912+ kfree(args);
1facf9fc 36913+
4f0767ce 36914+out:
e49829fe 36915+ atomic_dec(&br->br_xino_running);
1facf9fc 36916+}
36917+
36918+/* ---------------------------------------------------------------------- */
36919+
5527c038 36920+static int au_xino_do_write(vfs_writef_t write, struct file *file,
1facf9fc 36921+ ino_t h_ino, ino_t ino)
36922+{
36923+ loff_t pos;
36924+ ssize_t sz;
36925+
36926+ pos = h_ino;
36927+ if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
36928+ AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
36929+ return -EFBIG;
36930+ }
36931+ pos *= sizeof(ino);
36932+ sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
36933+ if (sz == sizeof(ino))
36934+ return 0; /* success */
36935+
36936+ AuIOErr("write failed (%zd)\n", sz);
36937+ return -EIO;
36938+}
36939+
36940+/*
36941+ * write @ino to the xinofile for the specified branch{@sb, @bindex}
36942+ * at the position of @h_ino.
36943+ * even if @ino is zero, it is written to the xinofile and means no entry.
36944+ * if the size of the xino file on a specific filesystem exceeds the watermark,
36945+ * try truncating it.
36946+ */
36947+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
36948+ ino_t ino)
36949+{
36950+ int err;
36951+ unsigned int mnt_flags;
36952+ struct au_branch *br;
36953+
36954+ BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
36955+ || ((loff_t)-1) > 0);
dece6358 36956+ SiMustAnyLock(sb);
1facf9fc 36957+
36958+ mnt_flags = au_mntflags(sb);
36959+ if (!au_opt_test(mnt_flags, XINO))
36960+ return 0;
36961+
36962+ br = au_sbr(sb, bindex);
36963+ err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
36964+ h_ino, ino);
36965+ if (!err) {
36966+ if (au_opt_test(mnt_flags, TRUNC_XINO)
86dc4139 36967+ && au_test_fs_trunc_xino(au_br_sb(br)))
1facf9fc 36968+ xino_try_trunc(sb, br);
36969+ return 0; /* success */
36970+ }
36971+
36972+ AuIOErr("write failed (%d)\n", err);
36973+ return -EIO;
36974+}
36975+
36976+/* ---------------------------------------------------------------------- */
36977+
36978+/* aufs inode number bitmap */
36979+
36980+static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
36981+static ino_t xib_calc_ino(unsigned long pindex, int bit)
36982+{
36983+ ino_t ino;
36984+
36985+ AuDebugOn(bit < 0 || page_bits <= bit);
36986+ ino = AUFS_FIRST_INO + pindex * page_bits + bit;
36987+ return ino;
36988+}
36989+
36990+static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
36991+{
36992+ AuDebugOn(ino < AUFS_FIRST_INO);
36993+ ino -= AUFS_FIRST_INO;
36994+ *pindex = ino / page_bits;
36995+ *bit = ino % page_bits;
36996+}
36997+
36998+static int xib_pindex(struct super_block *sb, unsigned long pindex)
36999+{
37000+ int err;
37001+ loff_t pos;
37002+ ssize_t sz;
37003+ struct au_sbinfo *sbinfo;
37004+ struct file *xib;
37005+ unsigned long *p;
37006+
37007+ sbinfo = au_sbi(sb);
37008+ MtxMustLock(&sbinfo->si_xib_mtx);
37009+ AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
37010+ || !au_opt_test(sbinfo->si_mntflags, XINO));
37011+
37012+ if (pindex == sbinfo->si_xib_last_pindex)
37013+ return 0;
37014+
37015+ xib = sbinfo->si_xib;
37016+ p = sbinfo->si_xib_buf;
37017+ pos = sbinfo->si_xib_last_pindex;
37018+ pos *= PAGE_SIZE;
37019+ sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
37020+ if (unlikely(sz != PAGE_SIZE))
37021+ goto out;
37022+
37023+ pos = pindex;
37024+ pos *= PAGE_SIZE;
c06a8ce3 37025+ if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
1facf9fc 37026+ sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
37027+ else {
37028+ memset(p, 0, PAGE_SIZE);
37029+ sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
37030+ }
37031+ if (sz == PAGE_SIZE) {
37032+ sbinfo->si_xib_last_pindex = pindex;
37033+ return 0; /* success */
37034+ }
37035+
4f0767ce 37036+out:
b752ccd1
AM
37037+ AuIOErr1("write failed (%zd)\n", sz);
37038+ err = sz;
37039+ if (sz >= 0)
37040+ err = -EIO;
37041+ return err;
37042+}
37043+
37044+/* ---------------------------------------------------------------------- */
37045+
37046+static void au_xib_clear_bit(struct inode *inode)
37047+{
37048+ int err, bit;
37049+ unsigned long pindex;
37050+ struct super_block *sb;
37051+ struct au_sbinfo *sbinfo;
37052+
37053+ AuDebugOn(inode->i_nlink);
37054+
37055+ sb = inode->i_sb;
37056+ xib_calc_bit(inode->i_ino, &pindex, &bit);
37057+ AuDebugOn(page_bits <= bit);
37058+ sbinfo = au_sbi(sb);
37059+ mutex_lock(&sbinfo->si_xib_mtx);
37060+ err = xib_pindex(sb, pindex);
37061+ if (!err) {
37062+ clear_bit(bit, sbinfo->si_xib_buf);
37063+ sbinfo->si_xib_next_bit = bit;
37064+ }
37065+ mutex_unlock(&sbinfo->si_xib_mtx);
37066+}
37067+
37068+/* for s_op->delete_inode() */
37069+void au_xino_delete_inode(struct inode *inode, const int unlinked)
37070+{
37071+ int err;
37072+ unsigned int mnt_flags;
5afbbe0d 37073+ aufs_bindex_t bindex, bbot, bi;
b752ccd1
AM
37074+ unsigned char try_trunc;
37075+ struct au_iinfo *iinfo;
37076+ struct super_block *sb;
37077+ struct au_hinode *hi;
37078+ struct inode *h_inode;
37079+ struct au_branch *br;
5527c038 37080+ vfs_writef_t xwrite;
b752ccd1 37081+
5afbbe0d
AM
37082+ AuDebugOn(au_is_bad_inode(inode));
37083+
b752ccd1
AM
37084+ sb = inode->i_sb;
37085+ mnt_flags = au_mntflags(sb);
37086+ if (!au_opt_test(mnt_flags, XINO)
37087+ || inode->i_ino == AUFS_ROOT_INO)
37088+ return;
37089+
37090+ if (unlinked) {
37091+ au_xigen_inc(inode);
37092+ au_xib_clear_bit(inode);
37093+ }
37094+
37095+ iinfo = au_ii(inode);
5afbbe0d 37096+ bindex = iinfo->ii_btop;
b752ccd1
AM
37097+ if (bindex < 0)
37098+ return;
1facf9fc 37099+
b752ccd1
AM
37100+ xwrite = au_sbi(sb)->si_xwrite;
37101+ try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
5afbbe0d
AM
37102+ hi = au_hinode(iinfo, bindex);
37103+ bbot = iinfo->ii_bbot;
37104+ for (; bindex <= bbot; bindex++, hi++) {
b752ccd1
AM
37105+ h_inode = hi->hi_inode;
37106+ if (!h_inode
37107+ || (!unlinked && h_inode->i_nlink))
37108+ continue;
1facf9fc 37109+
b752ccd1
AM
37110+ /* inode may not be revalidated */
37111+ bi = au_br_index(sb, hi->hi_id);
37112+ if (bi < 0)
37113+ continue;
1facf9fc 37114+
b752ccd1
AM
37115+ br = au_sbr(sb, bi);
37116+ err = au_xino_do_write(xwrite, br->br_xino.xi_file,
37117+ h_inode->i_ino, /*ino*/0);
37118+ if (!err && try_trunc
86dc4139 37119+ && au_test_fs_trunc_xino(au_br_sb(br)))
b752ccd1 37120+ xino_try_trunc(sb, br);
1facf9fc 37121+ }
1facf9fc 37122+}
37123+
37124+/* get an unused inode number from bitmap */
37125+ino_t au_xino_new_ino(struct super_block *sb)
37126+{
37127+ ino_t ino;
37128+ unsigned long *p, pindex, ul, pend;
37129+ struct au_sbinfo *sbinfo;
37130+ struct file *file;
37131+ int free_bit, err;
37132+
37133+ if (!au_opt_test(au_mntflags(sb), XINO))
37134+ return iunique(sb, AUFS_FIRST_INO);
37135+
37136+ sbinfo = au_sbi(sb);
37137+ mutex_lock(&sbinfo->si_xib_mtx);
37138+ p = sbinfo->si_xib_buf;
37139+ free_bit = sbinfo->si_xib_next_bit;
37140+ if (free_bit < page_bits && !test_bit(free_bit, p))
37141+ goto out; /* success */
37142+ free_bit = find_first_zero_bit(p, page_bits);
37143+ if (free_bit < page_bits)
37144+ goto out; /* success */
37145+
37146+ pindex = sbinfo->si_xib_last_pindex;
37147+ for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
37148+ err = xib_pindex(sb, ul);
37149+ if (unlikely(err))
37150+ goto out_err;
37151+ free_bit = find_first_zero_bit(p, page_bits);
37152+ if (free_bit < page_bits)
37153+ goto out; /* success */
37154+ }
37155+
37156+ file = sbinfo->si_xib;
c06a8ce3 37157+ pend = vfsub_f_size_read(file) / PAGE_SIZE;
1facf9fc 37158+ for (ul = pindex + 1; ul <= pend; ul++) {
37159+ err = xib_pindex(sb, ul);
37160+ if (unlikely(err))
37161+ goto out_err;
37162+ free_bit = find_first_zero_bit(p, page_bits);
37163+ if (free_bit < page_bits)
37164+ goto out; /* success */
37165+ }
37166+ BUG();
37167+
4f0767ce 37168+out:
1facf9fc 37169+ set_bit(free_bit, p);
7f207e10 37170+ sbinfo->si_xib_next_bit = free_bit + 1;
1facf9fc 37171+ pindex = sbinfo->si_xib_last_pindex;
37172+ mutex_unlock(&sbinfo->si_xib_mtx);
37173+ ino = xib_calc_ino(pindex, free_bit);
37174+ AuDbg("i%lu\n", (unsigned long)ino);
37175+ return ino;
4f0767ce 37176+out_err:
1facf9fc 37177+ mutex_unlock(&sbinfo->si_xib_mtx);
37178+ AuDbg("i0\n");
37179+ return 0;
37180+}
37181+
37182+/*
37183+ * read @ino from xinofile for the specified branch{@sb, @bindex}
37184+ * at the position of @h_ino.
37185+ * if @ino does not exist and @do_new is true, get new one.
37186+ */
37187+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37188+ ino_t *ino)
37189+{
37190+ int err;
37191+ ssize_t sz;
37192+ loff_t pos;
37193+ struct file *file;
37194+ struct au_sbinfo *sbinfo;
37195+
37196+ *ino = 0;
37197+ if (!au_opt_test(au_mntflags(sb), XINO))
37198+ return 0; /* no xino */
37199+
37200+ err = 0;
37201+ sbinfo = au_sbi(sb);
37202+ pos = h_ino;
37203+ if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
37204+ AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
37205+ return -EFBIG;
37206+ }
37207+ pos *= sizeof(*ino);
37208+
37209+ file = au_sbr(sb, bindex)->br_xino.xi_file;
c06a8ce3 37210+ if (vfsub_f_size_read(file) < pos + sizeof(*ino))
1facf9fc 37211+ return 0; /* no ino */
37212+
37213+ sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
37214+ if (sz == sizeof(*ino))
37215+ return 0; /* success */
37216+
37217+ err = sz;
37218+ if (unlikely(sz >= 0)) {
37219+ err = -EIO;
37220+ AuIOErr("xino read error (%zd)\n", sz);
37221+ }
37222+
37223+ return err;
37224+}
37225+
37226+/* ---------------------------------------------------------------------- */
37227+
37228+/* create and set a new xino file */
37229+
37230+struct file *au_xino_create(struct super_block *sb, char *fname, int silent)
37231+{
37232+ struct file *file;
37233+ struct dentry *h_parent, *d;
b912730e 37234+ struct inode *h_dir, *inode;
1facf9fc 37235+ int err;
37236+
37237+ /*
37238+ * at mount-time, and the xino file is the default path,
4a4d8108 37239+ * hnotify is disabled so we have no notify events to ignore.
1facf9fc 37240+ * when a user specified the xino, we cannot get au_hdir to be ignored.
37241+ */
7f207e10 37242+ file = vfsub_filp_open(fname, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
2cbb1c4b 37243+ /* | __FMODE_NONOTIFY */,
1facf9fc 37244+ S_IRUGO | S_IWUGO);
37245+ if (IS_ERR(file)) {
37246+ if (!silent)
4a4d8108 37247+ pr_err("open %s(%ld)\n", fname, PTR_ERR(file));
1facf9fc 37248+ return file;
37249+ }
37250+
37251+ /* keep file count */
b912730e
AM
37252+ err = 0;
37253+ inode = file_inode(file);
2000de60 37254+ h_parent = dget_parent(file->f_path.dentry);
5527c038 37255+ h_dir = d_inode(h_parent);
febd17d6 37256+ inode_lock_nested(h_dir, AuLsc_I_PARENT);
1facf9fc 37257+ /* mnt_want_write() is unnecessary here */
523b37e3 37258+ /* no delegation since it is just created */
b912730e
AM
37259+ if (inode->i_nlink)
37260+ err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
37261+ /*force*/0);
febd17d6 37262+ inode_unlock(h_dir);
1facf9fc 37263+ dput(h_parent);
37264+ if (unlikely(err)) {
37265+ if (!silent)
4a4d8108 37266+ pr_err("unlink %s(%d)\n", fname, err);
1facf9fc 37267+ goto out;
37268+ }
37269+
37270+ err = -EINVAL;
2000de60 37271+ d = file->f_path.dentry;
1facf9fc 37272+ if (unlikely(sb == d->d_sb)) {
37273+ if (!silent)
4a4d8108 37274+ pr_err("%s must be outside\n", fname);
1facf9fc 37275+ goto out;
37276+ }
37277+ if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
37278+ if (!silent)
4a4d8108
AM
37279+ pr_err("xino doesn't support %s(%s)\n",
37280+ fname, au_sbtype(d->d_sb));
1facf9fc 37281+ goto out;
37282+ }
37283+ return file; /* success */
37284+
4f0767ce 37285+out:
1facf9fc 37286+ fput(file);
37287+ file = ERR_PTR(err);
37288+ return file;
37289+}
37290+
37291+/*
37292+ * find another branch who is on the same filesystem of the specified
5afbbe0d 37293+ * branch{@btgt}. search until @bbot.
1facf9fc 37294+ */
37295+static int is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
5afbbe0d 37296+ aufs_bindex_t bbot)
1facf9fc 37297+{
37298+ aufs_bindex_t bindex;
37299+ struct super_block *tgt_sb = au_sbr_sb(sb, btgt);
37300+
37301+ for (bindex = 0; bindex < btgt; bindex++)
37302+ if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
37303+ return bindex;
5afbbe0d 37304+ for (bindex++; bindex <= bbot; bindex++)
1facf9fc 37305+ if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
37306+ return bindex;
37307+ return -1;
37308+}
37309+
37310+/* ---------------------------------------------------------------------- */
37311+
37312+/*
37313+ * initialize the xinofile for the specified branch @br
37314+ * at the place/path where @base_file indicates.
37315+ * test whether another branch is on the same filesystem or not,
37316+ * if @do_test is true.
37317+ */
37318+int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
37319+ struct file *base_file, int do_test)
37320+{
37321+ int err;
37322+ ino_t ino;
5afbbe0d 37323+ aufs_bindex_t bbot, bindex;
1facf9fc 37324+ struct au_branch *shared_br, *b;
37325+ struct file *file;
37326+ struct super_block *tgt_sb;
37327+
37328+ shared_br = NULL;
5afbbe0d 37329+ bbot = au_sbbot(sb);
1facf9fc 37330+ if (do_test) {
86dc4139 37331+ tgt_sb = au_br_sb(br);
5afbbe0d 37332+ for (bindex = 0; bindex <= bbot; bindex++) {
1facf9fc 37333+ b = au_sbr(sb, bindex);
86dc4139 37334+ if (tgt_sb == au_br_sb(b)) {
1facf9fc 37335+ shared_br = b;
37336+ break;
37337+ }
37338+ }
37339+ }
37340+
37341+ if (!shared_br || !shared_br->br_xino.xi_file) {
37342+ struct au_xino_lock_dir ldir;
37343+
37344+ au_xino_lock_dir(sb, base_file, &ldir);
37345+ /* mnt_want_write() is unnecessary here */
37346+ file = au_xino_create2(base_file, NULL);
37347+ au_xino_unlock_dir(&ldir);
37348+ err = PTR_ERR(file);
37349+ if (IS_ERR(file))
37350+ goto out;
37351+ br->br_xino.xi_file = file;
37352+ } else {
37353+ br->br_xino.xi_file = shared_br->br_xino.xi_file;
37354+ get_file(br->br_xino.xi_file);
37355+ }
37356+
37357+ ino = AUFS_ROOT_INO;
37358+ err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
37359+ h_ino, ino);
b752ccd1
AM
37360+ if (unlikely(err)) {
37361+ fput(br->br_xino.xi_file);
37362+ br->br_xino.xi_file = NULL;
37363+ }
1facf9fc 37364+
4f0767ce 37365+out:
1facf9fc 37366+ return err;
37367+}
37368+
37369+/* ---------------------------------------------------------------------- */
37370+
37371+/* trucate a xino bitmap file */
37372+
37373+/* todo: slow */
37374+static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
37375+{
37376+ int err, bit;
37377+ ssize_t sz;
37378+ unsigned long pindex;
37379+ loff_t pos, pend;
37380+ struct au_sbinfo *sbinfo;
5527c038 37381+ vfs_readf_t func;
1facf9fc 37382+ ino_t *ino;
37383+ unsigned long *p;
37384+
37385+ err = 0;
37386+ sbinfo = au_sbi(sb);
dece6358 37387+ MtxMustLock(&sbinfo->si_xib_mtx);
1facf9fc 37388+ p = sbinfo->si_xib_buf;
37389+ func = sbinfo->si_xread;
c06a8ce3 37390+ pend = vfsub_f_size_read(file);
1facf9fc 37391+ pos = 0;
37392+ while (pos < pend) {
37393+ sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
37394+ err = sz;
37395+ if (unlikely(sz <= 0))
37396+ goto out;
37397+
37398+ err = 0;
37399+ for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
37400+ if (unlikely(*ino < AUFS_FIRST_INO))
37401+ continue;
37402+
37403+ xib_calc_bit(*ino, &pindex, &bit);
37404+ AuDebugOn(page_bits <= bit);
37405+ err = xib_pindex(sb, pindex);
37406+ if (!err)
37407+ set_bit(bit, p);
37408+ else
37409+ goto out;
37410+ }
37411+ }
37412+
4f0767ce 37413+out:
1facf9fc 37414+ return err;
37415+}
37416+
37417+static int xib_restore(struct super_block *sb)
37418+{
37419+ int err;
5afbbe0d 37420+ aufs_bindex_t bindex, bbot;
1facf9fc 37421+ void *page;
37422+
37423+ err = -ENOMEM;
37424+ page = (void *)__get_free_page(GFP_NOFS);
37425+ if (unlikely(!page))
37426+ goto out;
37427+
37428+ err = 0;
5afbbe0d
AM
37429+ bbot = au_sbbot(sb);
37430+ for (bindex = 0; !err && bindex <= bbot; bindex++)
1facf9fc 37431+ if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0)
37432+ err = do_xib_restore
37433+ (sb, au_sbr(sb, bindex)->br_xino.xi_file, page);
37434+ else
37435+ AuDbg("b%d\n", bindex);
1c60b727 37436+ free_page((unsigned long)page);
1facf9fc 37437+
4f0767ce 37438+out:
1facf9fc 37439+ return err;
37440+}
37441+
37442+int au_xib_trunc(struct super_block *sb)
37443+{
37444+ int err;
37445+ ssize_t sz;
37446+ loff_t pos;
37447+ struct au_xino_lock_dir ldir;
37448+ struct au_sbinfo *sbinfo;
37449+ unsigned long *p;
37450+ struct file *file;
37451+
dece6358
AM
37452+ SiMustWriteLock(sb);
37453+
1facf9fc 37454+ err = 0;
37455+ sbinfo = au_sbi(sb);
37456+ if (!au_opt_test(sbinfo->si_mntflags, XINO))
37457+ goto out;
37458+
37459+ file = sbinfo->si_xib;
c06a8ce3 37460+ if (vfsub_f_size_read(file) <= PAGE_SIZE)
1facf9fc 37461+ goto out;
37462+
37463+ au_xino_lock_dir(sb, file, &ldir);
37464+ /* mnt_want_write() is unnecessary here */
37465+ file = au_xino_create2(sbinfo->si_xib, NULL);
37466+ au_xino_unlock_dir(&ldir);
37467+ err = PTR_ERR(file);
37468+ if (IS_ERR(file))
37469+ goto out;
37470+ fput(sbinfo->si_xib);
37471+ sbinfo->si_xib = file;
37472+
37473+ p = sbinfo->si_xib_buf;
37474+ memset(p, 0, PAGE_SIZE);
37475+ pos = 0;
37476+ sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
37477+ if (unlikely(sz != PAGE_SIZE)) {
37478+ err = sz;
37479+ AuIOErr("err %d\n", err);
37480+ if (sz >= 0)
37481+ err = -EIO;
37482+ goto out;
37483+ }
37484+
37485+ mutex_lock(&sbinfo->si_xib_mtx);
37486+ /* mnt_want_write() is unnecessary here */
37487+ err = xib_restore(sb);
37488+ mutex_unlock(&sbinfo->si_xib_mtx);
37489+
37490+out:
37491+ return err;
37492+}
37493+
37494+/* ---------------------------------------------------------------------- */
37495+
37496+/*
37497+ * xino mount option handlers
37498+ */
1facf9fc 37499+
37500+/* xino bitmap */
37501+static void xino_clear_xib(struct super_block *sb)
37502+{
37503+ struct au_sbinfo *sbinfo;
37504+
dece6358
AM
37505+ SiMustWriteLock(sb);
37506+
1facf9fc 37507+ sbinfo = au_sbi(sb);
37508+ sbinfo->si_xread = NULL;
37509+ sbinfo->si_xwrite = NULL;
37510+ if (sbinfo->si_xib)
37511+ fput(sbinfo->si_xib);
37512+ sbinfo->si_xib = NULL;
f0c0a007 37513+ if (sbinfo->si_xib_buf)
1c60b727 37514+ free_page((unsigned long)sbinfo->si_xib_buf);
1facf9fc 37515+ sbinfo->si_xib_buf = NULL;
37516+}
37517+
37518+static int au_xino_set_xib(struct super_block *sb, struct file *base)
37519+{
37520+ int err;
37521+ loff_t pos;
37522+ struct au_sbinfo *sbinfo;
37523+ struct file *file;
37524+
dece6358
AM
37525+ SiMustWriteLock(sb);
37526+
1facf9fc 37527+ sbinfo = au_sbi(sb);
37528+ file = au_xino_create2(base, sbinfo->si_xib);
37529+ err = PTR_ERR(file);
37530+ if (IS_ERR(file))
37531+ goto out;
37532+ if (sbinfo->si_xib)
37533+ fput(sbinfo->si_xib);
37534+ sbinfo->si_xib = file;
5527c038
JR
37535+ sbinfo->si_xread = vfs_readf(file);
37536+ sbinfo->si_xwrite = vfs_writef(file);
1facf9fc 37537+
37538+ err = -ENOMEM;
37539+ if (!sbinfo->si_xib_buf)
37540+ sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
37541+ if (unlikely(!sbinfo->si_xib_buf))
37542+ goto out_unset;
37543+
37544+ sbinfo->si_xib_last_pindex = 0;
37545+ sbinfo->si_xib_next_bit = 0;
c06a8ce3 37546+ if (vfsub_f_size_read(file) < PAGE_SIZE) {
1facf9fc 37547+ pos = 0;
37548+ err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
37549+ PAGE_SIZE, &pos);
37550+ if (unlikely(err != PAGE_SIZE))
37551+ goto out_free;
37552+ }
37553+ err = 0;
37554+ goto out; /* success */
37555+
4f0767ce 37556+out_free:
f0c0a007 37557+ if (sbinfo->si_xib_buf)
1c60b727 37558+ free_page((unsigned long)sbinfo->si_xib_buf);
b752ccd1
AM
37559+ sbinfo->si_xib_buf = NULL;
37560+ if (err >= 0)
37561+ err = -EIO;
4f0767ce 37562+out_unset:
b752ccd1
AM
37563+ fput(sbinfo->si_xib);
37564+ sbinfo->si_xib = NULL;
37565+ sbinfo->si_xread = NULL;
37566+ sbinfo->si_xwrite = NULL;
4f0767ce 37567+out:
b752ccd1 37568+ return err;
1facf9fc 37569+}
37570+
b752ccd1
AM
37571+/* xino for each branch */
37572+static void xino_clear_br(struct super_block *sb)
37573+{
5afbbe0d 37574+ aufs_bindex_t bindex, bbot;
b752ccd1 37575+ struct au_branch *br;
1facf9fc 37576+
5afbbe0d
AM
37577+ bbot = au_sbbot(sb);
37578+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1
AM
37579+ br = au_sbr(sb, bindex);
37580+ if (!br || !br->br_xino.xi_file)
37581+ continue;
37582+
37583+ fput(br->br_xino.xi_file);
37584+ br->br_xino.xi_file = NULL;
37585+ }
37586+}
37587+
37588+static int au_xino_set_br(struct super_block *sb, struct file *base)
1facf9fc 37589+{
37590+ int err;
b752ccd1 37591+ ino_t ino;
5afbbe0d 37592+ aufs_bindex_t bindex, bbot, bshared;
b752ccd1
AM
37593+ struct {
37594+ struct file *old, *new;
37595+ } *fpair, *p;
37596+ struct au_branch *br;
37597+ struct inode *inode;
5527c038 37598+ vfs_writef_t writef;
1facf9fc 37599+
b752ccd1
AM
37600+ SiMustWriteLock(sb);
37601+
37602+ err = -ENOMEM;
5afbbe0d
AM
37603+ bbot = au_sbbot(sb);
37604+ fpair = kcalloc(bbot + 1, sizeof(*fpair), GFP_NOFS);
b752ccd1 37605+ if (unlikely(!fpair))
1facf9fc 37606+ goto out;
37607+
5527c038 37608+ inode = d_inode(sb->s_root);
b752ccd1
AM
37609+ ino = AUFS_ROOT_INO;
37610+ writef = au_sbi(sb)->si_xwrite;
5afbbe0d 37611+ for (bindex = 0, p = fpair; bindex <= bbot; bindex++, p++) {
b752ccd1
AM
37612+ bshared = is_sb_shared(sb, bindex, bindex - 1);
37613+ if (bshared >= 0) {
37614+ /* shared xino */
37615+ *p = fpair[bshared];
37616+ get_file(p->new);
37617+ }
37618+
37619+ if (!p->new) {
37620+ /* new xino */
5afbbe0d 37621+ br = au_sbr(sb, bindex);
b752ccd1
AM
37622+ p->old = br->br_xino.xi_file;
37623+ p->new = au_xino_create2(base, br->br_xino.xi_file);
37624+ err = PTR_ERR(p->new);
37625+ if (IS_ERR(p->new)) {
37626+ p->new = NULL;
37627+ goto out_pair;
37628+ }
37629+ }
37630+
37631+ err = au_xino_do_write(writef, p->new,
37632+ au_h_iptr(inode, bindex)->i_ino, ino);
37633+ if (unlikely(err))
37634+ goto out_pair;
37635+ }
37636+
5afbbe0d 37637+ for (bindex = 0, p = fpair; bindex <= bbot; bindex++, p++) {
b752ccd1
AM
37638+ br = au_sbr(sb, bindex);
37639+ if (br->br_xino.xi_file)
37640+ fput(br->br_xino.xi_file);
37641+ get_file(p->new);
37642+ br->br_xino.xi_file = p->new;
37643+ }
1facf9fc 37644+
4f0767ce 37645+out_pair:
5afbbe0d 37646+ for (bindex = 0, p = fpair; bindex <= bbot; bindex++, p++)
b752ccd1
AM
37647+ if (p->new)
37648+ fput(p->new);
37649+ else
37650+ break;
1c60b727 37651+ kfree(fpair);
4f0767ce 37652+out:
1facf9fc 37653+ return err;
37654+}
b752ccd1
AM
37655+
37656+void au_xino_clr(struct super_block *sb)
37657+{
37658+ struct au_sbinfo *sbinfo;
37659+
37660+ au_xigen_clr(sb);
37661+ xino_clear_xib(sb);
37662+ xino_clear_br(sb);
37663+ sbinfo = au_sbi(sb);
37664+ /* lvalue, do not call au_mntflags() */
37665+ au_opt_clr(sbinfo->si_mntflags, XINO);
37666+}
37667+
37668+int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount)
37669+{
37670+ int err, skip;
37671+ struct dentry *parent, *cur_parent;
37672+ struct qstr *dname, *cur_name;
37673+ struct file *cur_xino;
37674+ struct inode *dir;
37675+ struct au_sbinfo *sbinfo;
37676+
37677+ SiMustWriteLock(sb);
37678+
37679+ err = 0;
37680+ sbinfo = au_sbi(sb);
2000de60 37681+ parent = dget_parent(xino->file->f_path.dentry);
b752ccd1
AM
37682+ if (remount) {
37683+ skip = 0;
2000de60 37684+ dname = &xino->file->f_path.dentry->d_name;
b752ccd1
AM
37685+ cur_xino = sbinfo->si_xib;
37686+ if (cur_xino) {
2000de60
JR
37687+ cur_parent = dget_parent(cur_xino->f_path.dentry);
37688+ cur_name = &cur_xino->f_path.dentry->d_name;
b752ccd1 37689+ skip = (cur_parent == parent
38d290e6 37690+ && au_qstreq(dname, cur_name));
b752ccd1
AM
37691+ dput(cur_parent);
37692+ }
37693+ if (skip)
37694+ goto out;
37695+ }
37696+
37697+ au_opt_set(sbinfo->si_mntflags, XINO);
5527c038 37698+ dir = d_inode(parent);
febd17d6 37699+ inode_lock_nested(dir, AuLsc_I_PARENT);
b752ccd1
AM
37700+ /* mnt_want_write() is unnecessary here */
37701+ err = au_xino_set_xib(sb, xino->file);
37702+ if (!err)
37703+ err = au_xigen_set(sb, xino->file);
37704+ if (!err)
37705+ err = au_xino_set_br(sb, xino->file);
febd17d6 37706+ inode_unlock(dir);
b752ccd1
AM
37707+ if (!err)
37708+ goto out; /* success */
37709+
37710+ /* reset all */
37711+ AuIOErr("failed creating xino(%d).\n", err);
c1595e42
JR
37712+ au_xigen_clr(sb);
37713+ xino_clear_xib(sb);
b752ccd1 37714+
4f0767ce 37715+out:
b752ccd1
AM
37716+ dput(parent);
37717+ return err;
37718+}
37719+
37720+/* ---------------------------------------------------------------------- */
37721+
37722+/*
37723+ * create a xinofile at the default place/path.
37724+ */
37725+struct file *au_xino_def(struct super_block *sb)
37726+{
37727+ struct file *file;
37728+ char *page, *p;
37729+ struct au_branch *br;
37730+ struct super_block *h_sb;
37731+ struct path path;
5afbbe0d 37732+ aufs_bindex_t bbot, bindex, bwr;
b752ccd1
AM
37733+
37734+ br = NULL;
5afbbe0d 37735+ bbot = au_sbbot(sb);
b752ccd1 37736+ bwr = -1;
5afbbe0d 37737+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1
AM
37738+ br = au_sbr(sb, bindex);
37739+ if (au_br_writable(br->br_perm)
86dc4139 37740+ && !au_test_fs_bad_xino(au_br_sb(br))) {
b752ccd1
AM
37741+ bwr = bindex;
37742+ break;
37743+ }
37744+ }
37745+
7f207e10
AM
37746+ if (bwr >= 0) {
37747+ file = ERR_PTR(-ENOMEM);
537831f9 37748+ page = (void *)__get_free_page(GFP_NOFS);
7f207e10
AM
37749+ if (unlikely(!page))
37750+ goto out;
86dc4139 37751+ path.mnt = au_br_mnt(br);
7f207e10
AM
37752+ path.dentry = au_h_dptr(sb->s_root, bwr);
37753+ p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
37754+ file = (void *)p;
37755+ if (!IS_ERR(p)) {
37756+ strcat(p, "/" AUFS_XINO_FNAME);
37757+ AuDbg("%s\n", p);
37758+ file = au_xino_create(sb, p, /*silent*/0);
37759+ if (!IS_ERR(file))
37760+ au_xino_brid_set(sb, br->br_id);
37761+ }
1c60b727 37762+ free_page((unsigned long)page);
7f207e10
AM
37763+ } else {
37764+ file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
37765+ if (IS_ERR(file))
37766+ goto out;
2000de60 37767+ h_sb = file->f_path.dentry->d_sb;
7f207e10
AM
37768+ if (unlikely(au_test_fs_bad_xino(h_sb))) {
37769+ pr_err("xino doesn't support %s(%s)\n",
37770+ AUFS_XINO_DEFPATH, au_sbtype(h_sb));
37771+ fput(file);
37772+ file = ERR_PTR(-EINVAL);
37773+ }
37774+ if (!IS_ERR(file))
37775+ au_xino_brid_set(sb, -1);
37776+ }
0c5527e5 37777+
7f207e10
AM
37778+out:
37779+ return file;
37780+}
37781+
37782+/* ---------------------------------------------------------------------- */
37783+
37784+int au_xino_path(struct seq_file *seq, struct file *file)
37785+{
37786+ int err;
37787+
37788+ err = au_seq_path(seq, &file->f_path);
79b8bda9 37789+ if (unlikely(err))
7f207e10
AM
37790+ goto out;
37791+
7f207e10
AM
37792+#define Deleted "\\040(deleted)"
37793+ seq->count -= sizeof(Deleted) - 1;
37794+ AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
37795+ sizeof(Deleted) - 1));
37796+#undef Deleted
37797+
37798+out:
37799+ return err;
37800+}
521ced18
JR
37801+
37802+/* ---------------------------------------------------------------------- */
37803+
37804+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
37805+ ino_t h_ino, int idx)
37806+{
37807+ struct au_xino_file *xino;
37808+
37809+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
37810+ xino = &au_sbr(sb, bindex)->br_xino;
37811+ AuDebugOn(idx < 0 || xino->xi_nondir.total <= idx);
37812+
37813+ spin_lock(&xino->xi_nondir.spin);
37814+ AuDebugOn(xino->xi_nondir.array[idx] != h_ino);
37815+ xino->xi_nondir.array[idx] = 0;
37816+ spin_unlock(&xino->xi_nondir.spin);
37817+ wake_up_all(&xino->xi_nondir.wqh);
37818+}
37819+
37820+static int au_xinondir_find(struct au_xino_file *xino, ino_t h_ino)
37821+{
37822+ int found, total, i;
37823+
37824+ found = -1;
37825+ total = xino->xi_nondir.total;
37826+ for (i = 0; i < total; i++) {
37827+ if (xino->xi_nondir.array[i] != h_ino)
37828+ continue;
37829+ found = i;
37830+ break;
37831+ }
37832+
37833+ return found;
37834+}
37835+
37836+static int au_xinondir_expand(struct au_xino_file *xino)
37837+{
37838+ int err, sz;
37839+ ino_t *p;
37840+
37841+ BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
37842+
37843+ err = -ENOMEM;
37844+ sz = xino->xi_nondir.total * sizeof(ino_t);
37845+ if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
37846+ goto out;
37847+ p = au_kzrealloc(xino->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
37848+ /*may_shrink*/0);
37849+ if (p) {
37850+ xino->xi_nondir.array = p;
37851+ xino->xi_nondir.total <<= 1;
37852+ AuDbg("xi_nondir.total %d\n", xino->xi_nondir.total);
37853+ err = 0;
37854+ }
37855+
37856+out:
37857+ return err;
37858+}
37859+
37860+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37861+ int *idx)
37862+{
37863+ int err, found, empty;
37864+ struct au_xino_file *xino;
37865+
37866+ err = 0;
37867+ *idx = -1;
37868+ if (!au_opt_test(au_mntflags(sb), XINO))
37869+ goto out; /* no xino */
37870+
37871+ xino = &au_sbr(sb, bindex)->br_xino;
37872+
37873+again:
37874+ spin_lock(&xino->xi_nondir.spin);
37875+ found = au_xinondir_find(xino, h_ino);
37876+ if (found == -1) {
37877+ empty = au_xinondir_find(xino, /*h_ino*/0);
37878+ if (empty == -1) {
37879+ empty = xino->xi_nondir.total;
37880+ err = au_xinondir_expand(xino);
37881+ if (unlikely(err))
37882+ goto out_unlock;
37883+ }
37884+ xino->xi_nondir.array[empty] = h_ino;
37885+ *idx = empty;
37886+ } else {
37887+ spin_unlock(&xino->xi_nondir.spin);
37888+ wait_event(xino->xi_nondir.wqh,
37889+ xino->xi_nondir.array[found] != h_ino);
37890+ goto again;
37891+ }
37892+
37893+out_unlock:
37894+ spin_unlock(&xino->xi_nondir.spin);
37895+out:
37896+ return err;
37897+}
537831f9
AM
37898diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
37899--- /usr/share/empty/include/uapi/linux/aufs_type.h 1970-01-01 01:00:00.000000000 +0100
8b6a4947
AM
37900+++ linux/include/uapi/linux/aufs_type.h 2017-11-12 22:24:44.710911257 +0100
37901@@ -0,0 +1,447 @@
7f207e10 37902+/*
a2654f78 37903+ * Copyright (C) 2005-2017 Junjiro R. Okajima
7f207e10
AM
37904+ *
37905+ * This program, aufs is free software; you can redistribute it and/or modify
37906+ * it under the terms of the GNU General Public License as published by
37907+ * the Free Software Foundation; either version 2 of the License, or
37908+ * (at your option) any later version.
37909+ *
37910+ * This program is distributed in the hope that it will be useful,
37911+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
37912+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37913+ * GNU General Public License for more details.
37914+ *
37915+ * You should have received a copy of the GNU General Public License
523b37e3 37916+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
37917+ */
37918+
37919+#ifndef __AUFS_TYPE_H__
37920+#define __AUFS_TYPE_H__
37921+
f6c5ef8b
AM
37922+#define AUFS_NAME "aufs"
37923+
9dbd164d 37924+#ifdef __KERNEL__
f6c5ef8b
AM
37925+/*
37926+ * define it before including all other headers.
37927+ * sched.h may use pr_* macros before defining "current", so define the
37928+ * no-current version first, and re-define later.
37929+ */
37930+#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
37931+#include <linux/sched.h>
37932+#undef pr_fmt
a2a7ad62
AM
37933+#define pr_fmt(fmt) \
37934+ AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
37935+ (int)sizeof(current->comm), current->comm, current->pid
9dbd164d
AM
37936+#else
37937+#include <stdint.h>
37938+#include <sys/types.h>
f6c5ef8b 37939+#endif /* __KERNEL__ */
7f207e10 37940+
f6c5ef8b
AM
37941+#include <linux/limits.h>
37942+
8b6a4947 37943+#define AUFS_VERSION "4.x-rcN-20171106"
7f207e10
AM
37944+
37945+/* todo? move this to linux-2.6.19/include/magic.h */
37946+#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
37947+
37948+/* ---------------------------------------------------------------------- */
37949+
37950+#ifdef CONFIG_AUFS_BRANCH_MAX_127
9dbd164d 37951+typedef int8_t aufs_bindex_t;
7f207e10
AM
37952+#define AUFS_BRANCH_MAX 127
37953+#else
9dbd164d 37954+typedef int16_t aufs_bindex_t;
7f207e10
AM
37955+#ifdef CONFIG_AUFS_BRANCH_MAX_511
37956+#define AUFS_BRANCH_MAX 511
37957+#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
37958+#define AUFS_BRANCH_MAX 1023
37959+#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
37960+#define AUFS_BRANCH_MAX 32767
37961+#endif
37962+#endif
37963+
37964+#ifdef __KERNEL__
37965+#ifndef AUFS_BRANCH_MAX
37966+#error unknown CONFIG_AUFS_BRANCH_MAX value
37967+#endif
37968+#endif /* __KERNEL__ */
37969+
37970+/* ---------------------------------------------------------------------- */
37971+
7f207e10
AM
37972+#define AUFS_FSTYPE AUFS_NAME
37973+
37974+#define AUFS_ROOT_INO 2
37975+#define AUFS_FIRST_INO 11
37976+
37977+#define AUFS_WH_PFX ".wh."
37978+#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
37979+#define AUFS_WH_TMP_LEN 4
86dc4139 37980+/* a limit for rmdir/rename a dir and copyup */
7f207e10
AM
37981+#define AUFS_MAX_NAMELEN (NAME_MAX \
37982+ - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
37983+ - 1 /* dot */\
37984+ - AUFS_WH_TMP_LEN) /* hex */
37985+#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
37986+#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
392086de
AM
37987+#define AUFS_XINO_DEF_SEC 30 /* seconds */
37988+#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
7f207e10
AM
37989+#define AUFS_DIRWH_DEF 3
37990+#define AUFS_RDCACHE_DEF 10 /* seconds */
027c5e7a 37991+#define AUFS_RDCACHE_MAX 3600 /* seconds */
7f207e10
AM
37992+#define AUFS_RDBLK_DEF 512 /* bytes */
37993+#define AUFS_RDHASH_DEF 32
37994+#define AUFS_WKQ_NAME AUFS_NAME "d"
027c5e7a
AM
37995+#define AUFS_MFS_DEF_SEC 30 /* seconds */
37996+#define AUFS_MFS_MAX_SEC 3600 /* seconds */
076b876e 37997+#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
86dc4139 37998+#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
7f207e10
AM
37999+
38000+/* pseudo-link maintenace under /proc */
38001+#define AUFS_PLINK_MAINT_NAME "plink_maint"
38002+#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
38003+#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
38004+
8b6a4947
AM
38005+/* dirren, renamed dir */
38006+#define AUFS_DR_INFO_PFX AUFS_WH_PFX ".dr."
38007+#define AUFS_DR_BRHINO_NAME AUFS_WH_PFX "hino"
38008+/* whiteouted doubly */
38009+#define AUFS_WH_DR_INFO_PFX AUFS_WH_PFX AUFS_DR_INFO_PFX
38010+#define AUFS_WH_DR_BRHINO AUFS_WH_PFX AUFS_DR_BRHINO_NAME
38011+
7f207e10
AM
38012+#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
38013+#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
38014+
38015+#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
38016+#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
38017+#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
38018+
38019+/* doubly whiteouted */
38020+#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
38021+#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
38022+#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
38023+
1e00d052 38024+/* branch permissions and attributes */
7f207e10
AM
38025+#define AUFS_BRPERM_RW "rw"
38026+#define AUFS_BRPERM_RO "ro"
38027+#define AUFS_BRPERM_RR "rr"
076b876e
AM
38028+#define AUFS_BRATTR_COO_REG "coo_reg"
38029+#define AUFS_BRATTR_COO_ALL "coo_all"
38030+#define AUFS_BRATTR_FHSM "fhsm"
38031+#define AUFS_BRATTR_UNPIN "unpin"
c1595e42
JR
38032+#define AUFS_BRATTR_ICEX "icex"
38033+#define AUFS_BRATTR_ICEX_SEC "icexsec"
38034+#define AUFS_BRATTR_ICEX_SYS "icexsys"
38035+#define AUFS_BRATTR_ICEX_TR "icextr"
38036+#define AUFS_BRATTR_ICEX_USR "icexusr"
38037+#define AUFS_BRATTR_ICEX_OTH "icexoth"
1e00d052
AM
38038+#define AUFS_BRRATTR_WH "wh"
38039+#define AUFS_BRWATTR_NLWH "nolwh"
076b876e
AM
38040+#define AUFS_BRWATTR_MOO "moo"
38041+
38042+#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
38043+#define AuBrPerm_RO (1 << 1) /* readonly */
38044+#define AuBrPerm_RR (1 << 2) /* natively readonly */
38045+#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
38046+
38047+#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
38048+#define AuBrAttr_COO_ALL (1 << 4)
38049+#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
38050+
38051+#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
38052+#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
c1595e42
JR
38053+ branch. meaningless since
38054+ linux-3.18-rc1 */
38055+
38056+/* ignore error in copying XATTR */
38057+#define AuBrAttr_ICEX_SEC (1 << 7)
38058+#define AuBrAttr_ICEX_SYS (1 << 8)
38059+#define AuBrAttr_ICEX_TR (1 << 9)
38060+#define AuBrAttr_ICEX_USR (1 << 10)
38061+#define AuBrAttr_ICEX_OTH (1 << 11)
38062+#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
38063+ | AuBrAttr_ICEX_SYS \
38064+ | AuBrAttr_ICEX_TR \
38065+ | AuBrAttr_ICEX_USR \
38066+ | AuBrAttr_ICEX_OTH)
38067+
38068+#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
076b876e
AM
38069+#define AuBrRAttr_Mask AuBrRAttr_WH
38070+
c1595e42
JR
38071+#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
38072+#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
076b876e
AM
38073+#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
38074+
38075+#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
38076+
c1595e42 38077+/* #warning test userspace */
076b876e
AM
38078+#ifdef __KERNEL__
38079+#ifndef CONFIG_AUFS_FHSM
38080+#undef AuBrAttr_FHSM
38081+#define AuBrAttr_FHSM 0
38082+#endif
c1595e42
JR
38083+#ifndef CONFIG_AUFS_XATTR
38084+#undef AuBrAttr_ICEX
38085+#define AuBrAttr_ICEX 0
38086+#undef AuBrAttr_ICEX_SEC
38087+#define AuBrAttr_ICEX_SEC 0
38088+#undef AuBrAttr_ICEX_SYS
38089+#define AuBrAttr_ICEX_SYS 0
38090+#undef AuBrAttr_ICEX_TR
38091+#define AuBrAttr_ICEX_TR 0
38092+#undef AuBrAttr_ICEX_USR
38093+#define AuBrAttr_ICEX_USR 0
38094+#undef AuBrAttr_ICEX_OTH
38095+#define AuBrAttr_ICEX_OTH 0
38096+#endif
076b876e
AM
38097+#endif
38098+
38099+/* the longest combination */
c1595e42
JR
38100+/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
38101+#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
38102+ "+" AUFS_BRATTR_COO_REG \
38103+ "+" AUFS_BRATTR_FHSM \
38104+ "+" AUFS_BRATTR_UNPIN \
7e9cd9fe
AM
38105+ "+" AUFS_BRATTR_ICEX_SEC \
38106+ "+" AUFS_BRATTR_ICEX_SYS \
38107+ "+" AUFS_BRATTR_ICEX_USR \
38108+ "+" AUFS_BRATTR_ICEX_OTH \
076b876e
AM
38109+ "+" AUFS_BRWATTR_NLWH)
38110+
38111+typedef struct {
38112+ char a[AuBrPermStrSz];
38113+} au_br_perm_str_t;
38114+
38115+static inline int au_br_writable(int brperm)
38116+{
38117+ return brperm & AuBrPerm_RW;
38118+}
38119+
38120+static inline int au_br_whable(int brperm)
38121+{
38122+ return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
38123+}
38124+
38125+static inline int au_br_wh_linkable(int brperm)
38126+{
38127+ return !(brperm & AuBrWAttr_NoLinkWH);
38128+}
38129+
38130+static inline int au_br_cmoo(int brperm)
38131+{
38132+ return brperm & AuBrAttr_CMOO_Mask;
38133+}
38134+
38135+static inline int au_br_fhsm(int brperm)
38136+{
38137+ return brperm & AuBrAttr_FHSM;
38138+}
7f207e10
AM
38139+
38140+/* ---------------------------------------------------------------------- */
38141+
38142+/* ioctl */
38143+enum {
38144+ /* readdir in userspace */
38145+ AuCtl_RDU,
38146+ AuCtl_RDU_INO,
38147+
076b876e
AM
38148+ AuCtl_WBR_FD, /* pathconf wrapper */
38149+ AuCtl_IBUSY, /* busy inode */
38150+ AuCtl_MVDOWN, /* move-down */
38151+ AuCtl_BR, /* info about branches */
38152+ AuCtl_FHSM_FD /* connection for fhsm */
7f207e10
AM
38153+};
38154+
38155+/* borrowed from linux/include/linux/kernel.h */
38156+#ifndef ALIGN
38157+#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
38158+#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
38159+#endif
38160+
38161+/* borrowed from linux/include/linux/compiler-gcc3.h */
38162+#ifndef __aligned
38163+#define __aligned(x) __attribute__((aligned(x)))
53392da6
AM
38164+#endif
38165+
38166+#ifdef __KERNEL__
38167+#ifndef __packed
7f207e10
AM
38168+#define __packed __attribute__((packed))
38169+#endif
53392da6 38170+#endif
7f207e10
AM
38171+
38172+struct au_rdu_cookie {
9dbd164d
AM
38173+ uint64_t h_pos;
38174+ int16_t bindex;
38175+ uint8_t flags;
38176+ uint8_t pad;
38177+ uint32_t generation;
7f207e10
AM
38178+} __aligned(8);
38179+
38180+struct au_rdu_ent {
9dbd164d
AM
38181+ uint64_t ino;
38182+ int16_t bindex;
38183+ uint8_t type;
38184+ uint8_t nlen;
38185+ uint8_t wh;
7f207e10
AM
38186+ char name[0];
38187+} __aligned(8);
38188+
38189+static inline int au_rdu_len(int nlen)
38190+{
38191+ /* include the terminating NULL */
38192+ return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
9dbd164d 38193+ sizeof(uint64_t));
7f207e10
AM
38194+}
38195+
38196+union au_rdu_ent_ul {
38197+ struct au_rdu_ent __user *e;
9dbd164d 38198+ uint64_t ul;
7f207e10
AM
38199+};
38200+
38201+enum {
38202+ AufsCtlRduV_SZ,
38203+ AufsCtlRduV_End
38204+};
38205+
38206+struct aufs_rdu {
38207+ /* input */
38208+ union {
9dbd164d
AM
38209+ uint64_t sz; /* AuCtl_RDU */
38210+ uint64_t nent; /* AuCtl_RDU_INO */
7f207e10
AM
38211+ };
38212+ union au_rdu_ent_ul ent;
9dbd164d 38213+ uint16_t verify[AufsCtlRduV_End];
7f207e10
AM
38214+
38215+ /* input/output */
9dbd164d 38216+ uint32_t blk;
7f207e10
AM
38217+
38218+ /* output */
38219+ union au_rdu_ent_ul tail;
38220+ /* number of entries which were added in a single call */
9dbd164d
AM
38221+ uint64_t rent;
38222+ uint8_t full;
38223+ uint8_t shwh;
7f207e10
AM
38224+
38225+ struct au_rdu_cookie cookie;
38226+} __aligned(8);
38227+
1e00d052
AM
38228+/* ---------------------------------------------------------------------- */
38229+
8b6a4947
AM
38230+/* dirren. the branch is identified by the filename who contains this */
38231+struct au_drinfo {
38232+ uint64_t ino;
38233+ union {
38234+ uint8_t oldnamelen;
38235+ uint64_t _padding;
38236+ };
38237+ uint8_t oldname[0];
38238+} __aligned(8);
38239+
38240+struct au_drinfo_fdata {
38241+ uint32_t magic;
38242+ struct au_drinfo drinfo;
38243+} __aligned(8);
38244+
38245+#define AUFS_DRINFO_MAGIC_V1 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
38246+/* future */
38247+#define AUFS_DRINFO_MAGIC_V2 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
38248+
38249+/* ---------------------------------------------------------------------- */
38250+
1e00d052 38251+struct aufs_wbr_fd {
9dbd164d
AM
38252+ uint32_t oflags;
38253+ int16_t brid;
1e00d052
AM
38254+} __aligned(8);
38255+
38256+/* ---------------------------------------------------------------------- */
38257+
027c5e7a 38258+struct aufs_ibusy {
9dbd164d
AM
38259+ uint64_t ino, h_ino;
38260+ int16_t bindex;
027c5e7a
AM
38261+} __aligned(8);
38262+
1e00d052
AM
38263+/* ---------------------------------------------------------------------- */
38264+
392086de
AM
38265+/* error code for move-down */
38266+/* the actual message strings are implemented in aufs-util.git */
38267+enum {
38268+ EAU_MVDOWN_OPAQUE = 1,
38269+ EAU_MVDOWN_WHITEOUT,
38270+ EAU_MVDOWN_UPPER,
38271+ EAU_MVDOWN_BOTTOM,
38272+ EAU_MVDOWN_NOUPPER,
38273+ EAU_MVDOWN_NOLOWERBR,
38274+ EAU_Last
38275+};
38276+
c2b27bf2 38277+/* flags for move-down */
392086de
AM
38278+#define AUFS_MVDOWN_DMSG 1
38279+#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
38280+#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
38281+#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
38282+#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
38283+#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
38284+#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
38285+#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
38286+#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
076b876e
AM
38287+#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
38288+#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
38289+#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
38290+#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
c2b27bf2 38291+
076b876e 38292+/* index for move-down */
392086de
AM
38293+enum {
38294+ AUFS_MVDOWN_UPPER,
38295+ AUFS_MVDOWN_LOWER,
38296+ AUFS_MVDOWN_NARRAY
38297+};
38298+
076b876e
AM
38299+/*
38300+ * additional info of move-down
38301+ * number of free blocks and inodes.
38302+ * subset of struct kstatfs, but smaller and always 64bit.
38303+ */
38304+struct aufs_stfs {
38305+ uint64_t f_blocks;
38306+ uint64_t f_bavail;
38307+ uint64_t f_files;
38308+ uint64_t f_ffree;
38309+};
38310+
38311+struct aufs_stbr {
38312+ int16_t brid; /* optional input */
38313+ int16_t bindex; /* output */
38314+ struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
38315+} __aligned(8);
38316+
c2b27bf2 38317+struct aufs_mvdown {
076b876e
AM
38318+ uint32_t flags; /* input/output */
38319+ struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
38320+ int8_t au_errno; /* output */
38321+} __aligned(8);
38322+
38323+/* ---------------------------------------------------------------------- */
38324+
38325+union aufs_brinfo {
38326+ /* PATH_MAX may differ between kernel-space and user-space */
38327+ char _spacer[4096];
392086de 38328+ struct {
076b876e
AM
38329+ int16_t id;
38330+ int perm;
38331+ char path[0];
38332+ };
c2b27bf2
AM
38333+} __aligned(8);
38334+
38335+/* ---------------------------------------------------------------------- */
38336+
7f207e10
AM
38337+#define AuCtlType 'A'
38338+#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
38339+#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
1e00d052
AM
38340+#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
38341+ struct aufs_wbr_fd)
027c5e7a 38342+#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
392086de
AM
38343+#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
38344+ struct aufs_mvdown)
076b876e
AM
38345+#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
38346+#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
7f207e10
AM
38347+
38348+#endif /* __AUFS_TYPE_H__ */
ffa93bbd 38349aufs4.x-rcN loopback patch
5527c038
JR
38350
38351diff --git a/drivers/block/loop.c b/drivers/block/loop.c
8b6a4947 38352index d44de9d..095672b 100644
5527c038
JR
38353--- a/drivers/block/loop.c
38354+++ b/drivers/block/loop.c
8b6a4947
AM
38355@@ -595,6 +595,15 @@ static inline void loop_update_dio(struct loop_device *lo)
38356 lo->use_dio);
5527c038
JR
38357 }
38358
5527c038
JR
38359+static struct file *loop_real_file(struct file *file)
38360+{
38361+ struct file *f = NULL;
38362+
38363+ if (file->f_path.dentry->d_sb->s_op->real_loop)
38364+ f = file->f_path.dentry->d_sb->s_op->real_loop(file);
38365+ return f;
8b6a4947
AM
38366+}
38367+
c2c0f25c 38368 static void loop_reread_partitions(struct loop_device *lo,
8b6a4947
AM
38369 struct block_device *bdev)
38370 {
38371@@ -629,6 +638,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
5527c038
JR
38372 unsigned int arg)
38373 {
38374 struct file *file, *old_file;
38375+ struct file *f, *virt_file = NULL, *old_virt_file;
38376 struct inode *inode;
38377 int error;
38378
8b6a4947 38379@@ -645,9 +655,16 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
5527c038
JR
38380 file = fget(arg);
38381 if (!file)
38382 goto out;
38383+ f = loop_real_file(file);
38384+ if (f) {
38385+ virt_file = file;
38386+ file = f;
38387+ get_file(file);
38388+ }
38389
38390 inode = file->f_mapping->host;
38391 old_file = lo->lo_backing_file;
38392+ old_virt_file = lo->lo_backing_virt_file;
38393
38394 error = -EINVAL;
38395
8b6a4947
AM
38396@@ -662,6 +679,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
38397 blk_mq_freeze_queue(lo->lo_queue);
38398 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
38399 lo->lo_backing_file = file;
38400+ lo->lo_backing_virt_file = virt_file;
38401 lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
38402 mapping_set_gfp_mask(file->f_mapping,
38403 lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
38404@@ -669,12 +687,16 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
38405 blk_mq_unfreeze_queue(lo->lo_queue);
5527c038
JR
38406
38407 fput(old_file);
38408+ if (old_virt_file)
38409+ fput(old_virt_file);
38410 if (lo->lo_flags & LO_FLAGS_PARTSCAN)
c2c0f25c 38411 loop_reread_partitions(lo, bdev);
5527c038
JR
38412 return 0;
38413
38414 out_putf:
38415 fput(file);
38416+ if (virt_file)
38417+ fput(virt_file);
38418 out:
38419 return error;
38420 }
8b6a4947 38421@@ -868,7 +890,7 @@ static int loop_prepare_queue(struct loop_device *lo)
5527c038
JR
38422 static int loop_set_fd(struct loop_device *lo, fmode_t mode,
38423 struct block_device *bdev, unsigned int arg)
38424 {
38425- struct file *file, *f;
38426+ struct file *file, *f, *virt_file = NULL;
38427 struct inode *inode;
38428 struct address_space *mapping;
8b6a4947
AM
38429 int lo_flags = 0;
38430@@ -882,6 +904,12 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
38431 file = fget(arg);
38432 if (!file)
38433 goto out;
38434+ f = loop_real_file(file);
38435+ if (f) {
38436+ virt_file = file;
38437+ file = f;
38438+ get_file(file);
38439+ }
38440
38441 error = -EBUSY;
38442 if (lo->lo_state != Lo_unbound)
8b6a4947 38443@@ -930,6 +958,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
38444 lo->lo_device = bdev;
38445 lo->lo_flags = lo_flags;
38446 lo->lo_backing_file = file;
38447+ lo->lo_backing_virt_file = virt_file;
38448 lo->transfer = NULL;
38449 lo->ioctl = NULL;
38450 lo->lo_sizelimit = 0;
8b6a4947 38451@@ -963,6 +992,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
38452
38453 out_putf:
38454 fput(file);
38455+ if (virt_file)
38456+ fput(virt_file);
38457 out:
38458 /* This is safe: open() is still holding a reference. */
38459 module_put(THIS_MODULE);
8b6a4947 38460@@ -1009,6 +1040,7 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
5527c038
JR
38461 static int loop_clr_fd(struct loop_device *lo)
38462 {
38463 struct file *filp = lo->lo_backing_file;
38464+ struct file *virt_filp = lo->lo_backing_virt_file;
38465 gfp_t gfp = lo->old_gfp_mask;
38466 struct block_device *bdev = lo->lo_device;
38467
8b6a4947 38468@@ -1040,6 +1072,7 @@ static int loop_clr_fd(struct loop_device *lo)
5527c038
JR
38469 spin_lock_irq(&lo->lo_lock);
38470 lo->lo_state = Lo_rundown;
38471 lo->lo_backing_file = NULL;
38472+ lo->lo_backing_virt_file = NULL;
38473 spin_unlock_irq(&lo->lo_lock);
38474
38475 loop_release_xfer(lo);
8b6a4947 38476@@ -1087,6 +1120,8 @@ static int loop_clr_fd(struct loop_device *lo)
5527c038
JR
38477 * bd_mutex which is usually taken before lo_ctl_mutex.
38478 */
38479 fput(filp);
38480+ if (virt_filp)
38481+ fput(virt_filp);
38482 return 0;
38483 }
38484
38485diff --git a/drivers/block/loop.h b/drivers/block/loop.h
8b6a4947 38486index 1f39567..128b137 100644
5527c038
JR
38487--- a/drivers/block/loop.h
38488+++ b/drivers/block/loop.h
38489@@ -46,7 +46,7 @@ struct loop_device {
38490 int (*ioctl)(struct loop_device *, int cmd,
38491 unsigned long arg);
38492
38493- struct file * lo_backing_file;
38494+ struct file * lo_backing_file, *lo_backing_virt_file;
38495 struct block_device *lo_device;
5527c038 38496 void *key_data;
8b6a4947 38497
5527c038 38498diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
8b6a4947 38499index 8aff060..e853272 100644
5527c038
JR
38500--- a/fs/aufs/f_op.c
38501+++ b/fs/aufs/f_op.c
1c60b727 38502@@ -357,7 +357,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
5527c038
JR
38503 if (IS_ERR(h_file))
38504 goto out;
38505
38506- if (au_test_loopback_kthread()) {
38507+ if (0 && au_test_loopback_kthread()) {
38508 au_warn_loopback(h_file->f_path.dentry->d_sb);
38509 if (file->f_mapping != h_file->f_mapping) {
38510 file->f_mapping = h_file->f_mapping;
38511diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
1c60b727 38512index e35f015..b37f1ae 100644
5527c038
JR
38513--- a/fs/aufs/loop.c
38514+++ b/fs/aufs/loop.c
e2f27e51 38515@@ -132,3 +132,19 @@ void au_loopback_fin(void)
79b8bda9 38516 symbol_put(loop_backing_file);
1c60b727 38517 kfree(au_warn_loopback_array);
5527c038
JR
38518 }
38519+
38520+/* ---------------------------------------------------------------------- */
38521+
38522+/* support the loopback block device insude aufs */
38523+
38524+struct file *aufs_real_loop(struct file *file)
38525+{
38526+ struct file *f;
38527+
38528+ BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
38529+ fi_read_lock(file);
38530+ f = au_hf_top(file);
38531+ fi_read_unlock(file);
38532+ AuDebugOn(!f);
38533+ return f;
38534+}
38535diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
a2654f78 38536index e2df495..36e5052 100644
5527c038
JR
38537--- a/fs/aufs/loop.h
38538+++ b/fs/aufs/loop.h
a2654f78 38539@@ -25,7 +25,11 @@ void au_warn_loopback(struct super_block *h_sb);
5527c038
JR
38540
38541 int au_loopback_init(void);
38542 void au_loopback_fin(void);
38543+
38544+struct file *aufs_real_loop(struct file *file);
38545 #else
38546+AuStub(struct file *, loop_backing_file, return NULL)
38547+
38548 AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
38549 struct dentry *h_adding)
38550 AuStubInt0(au_test_loopback_kthread, void)
a2654f78 38551@@ -33,6 +37,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
5527c038
JR
38552
38553 AuStubInt0(au_loopback_init, void)
38554 AuStubVoid(au_loopback_fin, void)
38555+
38556+AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
38557 #endif /* BLK_DEV_LOOP */
38558
38559 #endif /* __KERNEL__ */
38560diff --git a/fs/aufs/super.c b/fs/aufs/super.c
8b6a4947 38561index 3c300125..128d790 100644
5527c038
JR
38562--- a/fs/aufs/super.c
38563+++ b/fs/aufs/super.c
8b6a4947 38564@@ -838,7 +838,10 @@ static const struct super_operations aufs_sop = {
5527c038
JR
38565 .statfs = aufs_statfs,
38566 .put_super = aufs_put_super,
38567 .sync_fs = aufs_sync_fs,
38568- .remount_fs = aufs_remount_fs
38569+ .remount_fs = aufs_remount_fs,
38570+#ifdef CONFIG_AUFS_BDEV_LOOP
38571+ .real_loop = aufs_real_loop
38572+#endif
38573 };
38574
38575 /* ---------------------------------------------------------------------- */
38576diff --git a/include/linux/fs.h b/include/linux/fs.h
8b6a4947 38577index 8ab6566..8dbaa52 100644
5527c038
JR
38578--- a/include/linux/fs.h
38579+++ b/include/linux/fs.h
8b6a4947 38580@@ -1838,6 +1838,10 @@ struct super_operations {
5527c038
JR
38581 struct shrink_control *);
38582 long (*free_cached_objects)(struct super_block *,
38583 struct shrink_control *);
38584+#if defined(CONFIG_BLK_DEV_LOOP) || defined(CONFIG_BLK_DEV_LOOP_MODULE)
38585+ /* and aufs */
38586+ struct file *(*real_loop)(struct file *);
38587+#endif
38588 };
38589
38590 /*
This page took 6.939794 seconds and 4 git commands to generate.