xref: /OK3568_Linux_fs/kernel/fs/overlayfs/super.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (C) 2011 Novell Inc.
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <uapi/linux/magic.h>
8*4882a593Smuzhiyun #include <linux/fs.h>
9*4882a593Smuzhiyun #include <linux/namei.h>
10*4882a593Smuzhiyun #include <linux/xattr.h>
11*4882a593Smuzhiyun #include <linux/mount.h>
12*4882a593Smuzhiyun #include <linux/parser.h>
13*4882a593Smuzhiyun #include <linux/module.h>
14*4882a593Smuzhiyun #include <linux/statfs.h>
15*4882a593Smuzhiyun #include <linux/seq_file.h>
16*4882a593Smuzhiyun #include <linux/posix_acl_xattr.h>
17*4882a593Smuzhiyun #include <linux/exportfs.h>
18*4882a593Smuzhiyun #include "overlayfs.h"
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
21*4882a593Smuzhiyun MODULE_DESCRIPTION("Overlay filesystem");
22*4882a593Smuzhiyun MODULE_LICENSE("GPL");
23*4882a593Smuzhiyun MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun struct ovl_dir_cache;
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #define OVL_MAX_STACK 500
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR);
31*4882a593Smuzhiyun module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644);
32*4882a593Smuzhiyun MODULE_PARM_DESC(redirect_dir,
33*4882a593Smuzhiyun 		 "Default to on or off for the redirect_dir feature");
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun static bool ovl_redirect_always_follow =
36*4882a593Smuzhiyun 	IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW);
37*4882a593Smuzhiyun module_param_named(redirect_always_follow, ovl_redirect_always_follow,
38*4882a593Smuzhiyun 		   bool, 0644);
39*4882a593Smuzhiyun MODULE_PARM_DESC(redirect_always_follow,
40*4882a593Smuzhiyun 		 "Follow redirects even if redirect_dir feature is turned off");
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
43*4882a593Smuzhiyun module_param_named(index, ovl_index_def, bool, 0644);
44*4882a593Smuzhiyun MODULE_PARM_DESC(index,
45*4882a593Smuzhiyun 		 "Default to on or off for the inodes index feature");
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun static bool ovl_nfs_export_def = IS_ENABLED(CONFIG_OVERLAY_FS_NFS_EXPORT);
48*4882a593Smuzhiyun module_param_named(nfs_export, ovl_nfs_export_def, bool, 0644);
49*4882a593Smuzhiyun MODULE_PARM_DESC(nfs_export,
50*4882a593Smuzhiyun 		 "Default to on or off for the NFS export feature");
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun static bool ovl_xino_auto_def = IS_ENABLED(CONFIG_OVERLAY_FS_XINO_AUTO);
53*4882a593Smuzhiyun module_param_named(xino_auto, ovl_xino_auto_def, bool, 0644);
54*4882a593Smuzhiyun MODULE_PARM_DESC(xino_auto,
55*4882a593Smuzhiyun 		 "Auto enable xino feature");
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun static bool __read_mostly ovl_override_creds_def = true;
58*4882a593Smuzhiyun module_param_named(override_creds, ovl_override_creds_def, bool, 0644);
59*4882a593Smuzhiyun MODULE_PARM_DESC(ovl_override_creds_def,
60*4882a593Smuzhiyun 		 "Use mounter's credentials for accesses");
61*4882a593Smuzhiyun 
ovl_entry_stack_free(struct ovl_entry * oe)62*4882a593Smuzhiyun static void ovl_entry_stack_free(struct ovl_entry *oe)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun 	unsigned int i;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	for (i = 0; i < oe->numlower; i++)
67*4882a593Smuzhiyun 		dput(oe->lowerstack[i].dentry);
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun static bool ovl_metacopy_def = IS_ENABLED(CONFIG_OVERLAY_FS_METACOPY);
71*4882a593Smuzhiyun module_param_named(metacopy, ovl_metacopy_def, bool, 0644);
72*4882a593Smuzhiyun MODULE_PARM_DESC(metacopy,
73*4882a593Smuzhiyun 		 "Default to on or off for the metadata only copy up feature");
74*4882a593Smuzhiyun 
ovl_dentry_release(struct dentry * dentry)75*4882a593Smuzhiyun static void ovl_dentry_release(struct dentry *dentry)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun 	struct ovl_entry *oe = dentry->d_fsdata;
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	if (oe) {
80*4882a593Smuzhiyun 		ovl_entry_stack_free(oe);
81*4882a593Smuzhiyun 		kfree_rcu(oe, rcu);
82*4882a593Smuzhiyun 	}
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun 
ovl_d_real(struct dentry * dentry,const struct inode * inode)85*4882a593Smuzhiyun static struct dentry *ovl_d_real(struct dentry *dentry,
86*4882a593Smuzhiyun 				 const struct inode *inode)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun 	struct dentry *real = NULL, *lower;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	/* It's an overlay file */
91*4882a593Smuzhiyun 	if (inode && d_inode(dentry) == inode)
92*4882a593Smuzhiyun 		return dentry;
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	if (!d_is_reg(dentry)) {
95*4882a593Smuzhiyun 		if (!inode || inode == d_inode(dentry))
96*4882a593Smuzhiyun 			return dentry;
97*4882a593Smuzhiyun 		goto bug;
98*4882a593Smuzhiyun 	}
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	real = ovl_dentry_upper(dentry);
101*4882a593Smuzhiyun 	if (real && (inode == d_inode(real)))
102*4882a593Smuzhiyun 		return real;
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	if (real && !inode && ovl_has_upperdata(d_inode(dentry)))
105*4882a593Smuzhiyun 		return real;
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	lower = ovl_dentry_lowerdata(dentry);
108*4882a593Smuzhiyun 	if (!lower)
109*4882a593Smuzhiyun 		goto bug;
110*4882a593Smuzhiyun 	real = lower;
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	/* Handle recursion */
113*4882a593Smuzhiyun 	real = d_real(real, inode);
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	if (!inode || inode == d_inode(real))
116*4882a593Smuzhiyun 		return real;
117*4882a593Smuzhiyun bug:
118*4882a593Smuzhiyun 	WARN(1, "%s(%pd4, %s:%lu): real dentry (%p/%lu) not found\n",
119*4882a593Smuzhiyun 	     __func__, dentry, inode ? inode->i_sb->s_id : "NULL",
120*4882a593Smuzhiyun 	     inode ? inode->i_ino : 0, real,
121*4882a593Smuzhiyun 	     real && d_inode(real) ? d_inode(real)->i_ino : 0);
122*4882a593Smuzhiyun 	return dentry;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun 
ovl_revalidate_real(struct dentry * d,unsigned int flags,bool weak)125*4882a593Smuzhiyun static int ovl_revalidate_real(struct dentry *d, unsigned int flags, bool weak)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun 	int ret = 1;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	if (weak) {
130*4882a593Smuzhiyun 		if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE)
131*4882a593Smuzhiyun 			ret =  d->d_op->d_weak_revalidate(d, flags);
132*4882a593Smuzhiyun 	} else if (d->d_flags & DCACHE_OP_REVALIDATE) {
133*4882a593Smuzhiyun 		ret = d->d_op->d_revalidate(d, flags);
134*4882a593Smuzhiyun 		if (!ret) {
135*4882a593Smuzhiyun 			if (!(flags & LOOKUP_RCU))
136*4882a593Smuzhiyun 				d_invalidate(d);
137*4882a593Smuzhiyun 			ret = -ESTALE;
138*4882a593Smuzhiyun 		}
139*4882a593Smuzhiyun 	}
140*4882a593Smuzhiyun 	return ret;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun 
ovl_dentry_revalidate_common(struct dentry * dentry,unsigned int flags,bool weak)143*4882a593Smuzhiyun static int ovl_dentry_revalidate_common(struct dentry *dentry,
144*4882a593Smuzhiyun 					unsigned int flags, bool weak)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun 	struct ovl_entry *oe = dentry->d_fsdata;
147*4882a593Smuzhiyun 	struct dentry *upper;
148*4882a593Smuzhiyun 	unsigned int i;
149*4882a593Smuzhiyun 	int ret = 1;
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	upper = ovl_dentry_upper(dentry);
152*4882a593Smuzhiyun 	if (upper)
153*4882a593Smuzhiyun 		ret = ovl_revalidate_real(upper, flags, weak);
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	for (i = 0; ret > 0 && i < oe->numlower; i++) {
156*4882a593Smuzhiyun 		ret = ovl_revalidate_real(oe->lowerstack[i].dentry, flags,
157*4882a593Smuzhiyun 					  weak);
158*4882a593Smuzhiyun 	}
159*4882a593Smuzhiyun 	return ret;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun 
ovl_dentry_revalidate(struct dentry * dentry,unsigned int flags)162*4882a593Smuzhiyun static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun 	return ovl_dentry_revalidate_common(dentry, flags, false);
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun 
ovl_dentry_weak_revalidate(struct dentry * dentry,unsigned int flags)167*4882a593Smuzhiyun static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun 	return ovl_dentry_revalidate_common(dentry, flags, true);
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun static const struct dentry_operations ovl_dentry_operations = {
173*4882a593Smuzhiyun 	.d_release = ovl_dentry_release,
174*4882a593Smuzhiyun 	.d_real = ovl_d_real,
175*4882a593Smuzhiyun 	.d_revalidate = ovl_dentry_revalidate,
176*4882a593Smuzhiyun 	.d_weak_revalidate = ovl_dentry_weak_revalidate,
177*4882a593Smuzhiyun };
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun static struct kmem_cache *ovl_inode_cachep;
180*4882a593Smuzhiyun 
ovl_alloc_inode(struct super_block * sb)181*4882a593Smuzhiyun static struct inode *ovl_alloc_inode(struct super_block *sb)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun 	struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	if (!oi)
186*4882a593Smuzhiyun 		return NULL;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	oi->cache = NULL;
189*4882a593Smuzhiyun 	oi->redirect = NULL;
190*4882a593Smuzhiyun 	oi->version = 0;
191*4882a593Smuzhiyun 	oi->flags = 0;
192*4882a593Smuzhiyun 	oi->__upperdentry = NULL;
193*4882a593Smuzhiyun 	oi->lower = NULL;
194*4882a593Smuzhiyun 	oi->lowerdata = NULL;
195*4882a593Smuzhiyun 	mutex_init(&oi->lock);
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	return &oi->vfs_inode;
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun 
ovl_free_inode(struct inode * inode)200*4882a593Smuzhiyun static void ovl_free_inode(struct inode *inode)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun 	struct ovl_inode *oi = OVL_I(inode);
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	kfree(oi->redirect);
205*4882a593Smuzhiyun 	mutex_destroy(&oi->lock);
206*4882a593Smuzhiyun 	kmem_cache_free(ovl_inode_cachep, oi);
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun 
ovl_destroy_inode(struct inode * inode)209*4882a593Smuzhiyun static void ovl_destroy_inode(struct inode *inode)
210*4882a593Smuzhiyun {
211*4882a593Smuzhiyun 	struct ovl_inode *oi = OVL_I(inode);
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	dput(oi->__upperdentry);
214*4882a593Smuzhiyun 	iput(oi->lower);
215*4882a593Smuzhiyun 	if (S_ISDIR(inode->i_mode))
216*4882a593Smuzhiyun 		ovl_dir_cache_free(inode);
217*4882a593Smuzhiyun 	else
218*4882a593Smuzhiyun 		iput(oi->lowerdata);
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun 
ovl_free_fs(struct ovl_fs * ofs)221*4882a593Smuzhiyun static void ovl_free_fs(struct ovl_fs *ofs)
222*4882a593Smuzhiyun {
223*4882a593Smuzhiyun 	struct vfsmount **mounts;
224*4882a593Smuzhiyun 	unsigned i;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	iput(ofs->workbasedir_trap);
227*4882a593Smuzhiyun 	iput(ofs->indexdir_trap);
228*4882a593Smuzhiyun 	iput(ofs->workdir_trap);
229*4882a593Smuzhiyun 	dput(ofs->whiteout);
230*4882a593Smuzhiyun 	dput(ofs->indexdir);
231*4882a593Smuzhiyun 	dput(ofs->workdir);
232*4882a593Smuzhiyun 	if (ofs->workdir_locked)
233*4882a593Smuzhiyun 		ovl_inuse_unlock(ofs->workbasedir);
234*4882a593Smuzhiyun 	dput(ofs->workbasedir);
235*4882a593Smuzhiyun 	if (ofs->upperdir_locked)
236*4882a593Smuzhiyun 		ovl_inuse_unlock(ovl_upper_mnt(ofs)->mnt_root);
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	/* Hack!  Reuse ofs->layers as a vfsmount array before freeing it */
239*4882a593Smuzhiyun 	mounts = (struct vfsmount **) ofs->layers;
240*4882a593Smuzhiyun 	for (i = 0; i < ofs->numlayer; i++) {
241*4882a593Smuzhiyun 		iput(ofs->layers[i].trap);
242*4882a593Smuzhiyun 		mounts[i] = ofs->layers[i].mnt;
243*4882a593Smuzhiyun 	}
244*4882a593Smuzhiyun 	kern_unmount_array(mounts, ofs->numlayer);
245*4882a593Smuzhiyun 	kfree(ofs->layers);
246*4882a593Smuzhiyun 	for (i = 0; i < ofs->numfs; i++)
247*4882a593Smuzhiyun 		free_anon_bdev(ofs->fs[i].pseudo_dev);
248*4882a593Smuzhiyun 	kfree(ofs->fs);
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	kfree(ofs->config.lowerdir);
251*4882a593Smuzhiyun 	kfree(ofs->config.upperdir);
252*4882a593Smuzhiyun 	kfree(ofs->config.workdir);
253*4882a593Smuzhiyun 	kfree(ofs->config.redirect_mode);
254*4882a593Smuzhiyun 	if (ofs->creator_cred)
255*4882a593Smuzhiyun 		put_cred(ofs->creator_cred);
256*4882a593Smuzhiyun 	kfree(ofs);
257*4882a593Smuzhiyun }
258*4882a593Smuzhiyun 
ovl_put_super(struct super_block * sb)259*4882a593Smuzhiyun static void ovl_put_super(struct super_block *sb)
260*4882a593Smuzhiyun {
261*4882a593Smuzhiyun 	struct ovl_fs *ofs = sb->s_fs_info;
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	ovl_free_fs(ofs);
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun /* Sync real dirty inodes in upper filesystem (if it exists) */
ovl_sync_fs(struct super_block * sb,int wait)267*4882a593Smuzhiyun static int ovl_sync_fs(struct super_block *sb, int wait)
268*4882a593Smuzhiyun {
269*4882a593Smuzhiyun 	struct ovl_fs *ofs = sb->s_fs_info;
270*4882a593Smuzhiyun 	struct super_block *upper_sb;
271*4882a593Smuzhiyun 	int ret;
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	ret = ovl_sync_status(ofs);
274*4882a593Smuzhiyun 	/*
275*4882a593Smuzhiyun 	 * We have to always set the err, because the return value isn't
276*4882a593Smuzhiyun 	 * checked in syncfs, and instead indirectly return an error via
277*4882a593Smuzhiyun 	 * the sb's writeback errseq, which VFS inspects after this call.
278*4882a593Smuzhiyun 	 */
279*4882a593Smuzhiyun 	if (ret < 0) {
280*4882a593Smuzhiyun 		errseq_set(&sb->s_wb_err, -EIO);
281*4882a593Smuzhiyun 		return -EIO;
282*4882a593Smuzhiyun 	}
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	if (!ret)
285*4882a593Smuzhiyun 		return ret;
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	/*
288*4882a593Smuzhiyun 	 * Not called for sync(2) call or an emergency sync (SB_I_SKIP_SYNC).
289*4882a593Smuzhiyun 	 * All the super blocks will be iterated, including upper_sb.
290*4882a593Smuzhiyun 	 *
291*4882a593Smuzhiyun 	 * If this is a syncfs(2) call, then we do need to call
292*4882a593Smuzhiyun 	 * sync_filesystem() on upper_sb, but enough if we do it when being
293*4882a593Smuzhiyun 	 * called with wait == 1.
294*4882a593Smuzhiyun 	 */
295*4882a593Smuzhiyun 	if (!wait)
296*4882a593Smuzhiyun 		return 0;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	down_read(&upper_sb->s_umount);
301*4882a593Smuzhiyun 	ret = sync_filesystem(upper_sb);
302*4882a593Smuzhiyun 	up_read(&upper_sb->s_umount);
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 	return ret;
305*4882a593Smuzhiyun }
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun /**
308*4882a593Smuzhiyun  * ovl_statfs
309*4882a593Smuzhiyun  * @sb: The overlayfs super block
310*4882a593Smuzhiyun  * @buf: The struct kstatfs to fill in with stats
311*4882a593Smuzhiyun  *
312*4882a593Smuzhiyun  * Get the filesystem statistics.  As writes always target the upper layer
313*4882a593Smuzhiyun  * filesystem pass the statfs to the upper filesystem (if it exists)
314*4882a593Smuzhiyun  */
ovl_statfs(struct dentry * dentry,struct kstatfs * buf)315*4882a593Smuzhiyun static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
316*4882a593Smuzhiyun {
317*4882a593Smuzhiyun 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
318*4882a593Smuzhiyun 	struct dentry *root_dentry = dentry->d_sb->s_root;
319*4882a593Smuzhiyun 	struct path path;
320*4882a593Smuzhiyun 	int err;
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	ovl_path_real(root_dentry, &path);
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	err = vfs_statfs(&path, buf);
325*4882a593Smuzhiyun 	if (!err) {
326*4882a593Smuzhiyun 		buf->f_namelen = ofs->namelen;
327*4882a593Smuzhiyun 		buf->f_type = OVERLAYFS_SUPER_MAGIC;
328*4882a593Smuzhiyun 	}
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	return err;
331*4882a593Smuzhiyun }
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun /* Will this overlay be forced to mount/remount ro? */
ovl_force_readonly(struct ovl_fs * ofs)334*4882a593Smuzhiyun static bool ovl_force_readonly(struct ovl_fs *ofs)
335*4882a593Smuzhiyun {
336*4882a593Smuzhiyun 	return (!ovl_upper_mnt(ofs) || !ofs->workdir);
337*4882a593Smuzhiyun }
338*4882a593Smuzhiyun 
ovl_redirect_mode_def(void)339*4882a593Smuzhiyun static const char *ovl_redirect_mode_def(void)
340*4882a593Smuzhiyun {
341*4882a593Smuzhiyun 	return ovl_redirect_dir_def ? "on" : "off";
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun static const char * const ovl_xino_str[] = {
345*4882a593Smuzhiyun 	"off",
346*4882a593Smuzhiyun 	"auto",
347*4882a593Smuzhiyun 	"on",
348*4882a593Smuzhiyun };
349*4882a593Smuzhiyun 
ovl_xino_def(void)350*4882a593Smuzhiyun static inline int ovl_xino_def(void)
351*4882a593Smuzhiyun {
352*4882a593Smuzhiyun 	return ovl_xino_auto_def ? OVL_XINO_AUTO : OVL_XINO_OFF;
353*4882a593Smuzhiyun }
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun /**
356*4882a593Smuzhiyun  * ovl_show_options
357*4882a593Smuzhiyun  *
358*4882a593Smuzhiyun  * Prints the mount options for a given superblock.
359*4882a593Smuzhiyun  * Returns zero; does not fail.
360*4882a593Smuzhiyun  */
ovl_show_options(struct seq_file * m,struct dentry * dentry)361*4882a593Smuzhiyun static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
362*4882a593Smuzhiyun {
363*4882a593Smuzhiyun 	struct super_block *sb = dentry->d_sb;
364*4882a593Smuzhiyun 	struct ovl_fs *ofs = sb->s_fs_info;
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	seq_show_option(m, "lowerdir", ofs->config.lowerdir);
367*4882a593Smuzhiyun 	if (ofs->config.upperdir) {
368*4882a593Smuzhiyun 		seq_show_option(m, "upperdir", ofs->config.upperdir);
369*4882a593Smuzhiyun 		seq_show_option(m, "workdir", ofs->config.workdir);
370*4882a593Smuzhiyun 	}
371*4882a593Smuzhiyun 	if (ofs->config.default_permissions)
372*4882a593Smuzhiyun 		seq_puts(m, ",default_permissions");
373*4882a593Smuzhiyun 	if (strcmp(ofs->config.redirect_mode, ovl_redirect_mode_def()) != 0)
374*4882a593Smuzhiyun 		seq_printf(m, ",redirect_dir=%s", ofs->config.redirect_mode);
375*4882a593Smuzhiyun 	if (ofs->config.index != ovl_index_def)
376*4882a593Smuzhiyun 		seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off");
377*4882a593Smuzhiyun 	if (ofs->config.nfs_export != ovl_nfs_export_def)
378*4882a593Smuzhiyun 		seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ?
379*4882a593Smuzhiyun 						"on" : "off");
380*4882a593Smuzhiyun 	if (ofs->config.xino != ovl_xino_def() && !ovl_same_fs(sb))
381*4882a593Smuzhiyun 		seq_printf(m, ",xino=%s", ovl_xino_str[ofs->config.xino]);
382*4882a593Smuzhiyun 	if (ofs->config.metacopy != ovl_metacopy_def)
383*4882a593Smuzhiyun 		seq_printf(m, ",metacopy=%s",
384*4882a593Smuzhiyun 			   ofs->config.metacopy ? "on" : "off");
385*4882a593Smuzhiyun 	if (ofs->config.ovl_volatile)
386*4882a593Smuzhiyun 		seq_puts(m, ",volatile");
387*4882a593Smuzhiyun 	if (ofs->config.override_creds != ovl_override_creds_def)
388*4882a593Smuzhiyun 		seq_show_option(m, "override_creds",
389*4882a593Smuzhiyun 				ofs->config.override_creds ? "on" : "off");
390*4882a593Smuzhiyun 	return 0;
391*4882a593Smuzhiyun }
392*4882a593Smuzhiyun 
ovl_remount(struct super_block * sb,int * flags,char * data)393*4882a593Smuzhiyun static int ovl_remount(struct super_block *sb, int *flags, char *data)
394*4882a593Smuzhiyun {
395*4882a593Smuzhiyun 	struct ovl_fs *ofs = sb->s_fs_info;
396*4882a593Smuzhiyun 	struct super_block *upper_sb;
397*4882a593Smuzhiyun 	int ret = 0;
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun 	if (!(*flags & SB_RDONLY) && ovl_force_readonly(ofs))
400*4882a593Smuzhiyun 		return -EROFS;
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 	if (*flags & SB_RDONLY && !sb_rdonly(sb)) {
403*4882a593Smuzhiyun 		upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
404*4882a593Smuzhiyun 		if (ovl_should_sync(ofs)) {
405*4882a593Smuzhiyun 			down_read(&upper_sb->s_umount);
406*4882a593Smuzhiyun 			ret = sync_filesystem(upper_sb);
407*4882a593Smuzhiyun 			up_read(&upper_sb->s_umount);
408*4882a593Smuzhiyun 		}
409*4882a593Smuzhiyun 	}
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun 	return ret;
412*4882a593Smuzhiyun }
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun static const struct super_operations ovl_super_operations = {
415*4882a593Smuzhiyun 	.alloc_inode	= ovl_alloc_inode,
416*4882a593Smuzhiyun 	.free_inode	= ovl_free_inode,
417*4882a593Smuzhiyun 	.destroy_inode	= ovl_destroy_inode,
418*4882a593Smuzhiyun 	.drop_inode	= generic_delete_inode,
419*4882a593Smuzhiyun 	.put_super	= ovl_put_super,
420*4882a593Smuzhiyun 	.sync_fs	= ovl_sync_fs,
421*4882a593Smuzhiyun 	.statfs		= ovl_statfs,
422*4882a593Smuzhiyun 	.show_options	= ovl_show_options,
423*4882a593Smuzhiyun 	.remount_fs	= ovl_remount,
424*4882a593Smuzhiyun };
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun enum {
427*4882a593Smuzhiyun 	OPT_LOWERDIR,
428*4882a593Smuzhiyun 	OPT_UPPERDIR,
429*4882a593Smuzhiyun 	OPT_WORKDIR,
430*4882a593Smuzhiyun 	OPT_DEFAULT_PERMISSIONS,
431*4882a593Smuzhiyun 	OPT_REDIRECT_DIR,
432*4882a593Smuzhiyun 	OPT_INDEX_ON,
433*4882a593Smuzhiyun 	OPT_INDEX_OFF,
434*4882a593Smuzhiyun 	OPT_NFS_EXPORT_ON,
435*4882a593Smuzhiyun 	OPT_NFS_EXPORT_OFF,
436*4882a593Smuzhiyun 	OPT_XINO_ON,
437*4882a593Smuzhiyun 	OPT_XINO_OFF,
438*4882a593Smuzhiyun 	OPT_XINO_AUTO,
439*4882a593Smuzhiyun 	OPT_METACOPY_ON,
440*4882a593Smuzhiyun 	OPT_METACOPY_OFF,
441*4882a593Smuzhiyun 	OPT_VOLATILE,
442*4882a593Smuzhiyun 	OPT_OVERRIDE_CREDS_ON,
443*4882a593Smuzhiyun 	OPT_OVERRIDE_CREDS_OFF,
444*4882a593Smuzhiyun 	OPT_ERR,
445*4882a593Smuzhiyun };
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun static const match_table_t ovl_tokens = {
448*4882a593Smuzhiyun 	{OPT_LOWERDIR,			"lowerdir=%s"},
449*4882a593Smuzhiyun 	{OPT_UPPERDIR,			"upperdir=%s"},
450*4882a593Smuzhiyun 	{OPT_WORKDIR,			"workdir=%s"},
451*4882a593Smuzhiyun 	{OPT_DEFAULT_PERMISSIONS,	"default_permissions"},
452*4882a593Smuzhiyun 	{OPT_REDIRECT_DIR,		"redirect_dir=%s"},
453*4882a593Smuzhiyun 	{OPT_INDEX_ON,			"index=on"},
454*4882a593Smuzhiyun 	{OPT_INDEX_OFF,			"index=off"},
455*4882a593Smuzhiyun 	{OPT_NFS_EXPORT_ON,		"nfs_export=on"},
456*4882a593Smuzhiyun 	{OPT_NFS_EXPORT_OFF,		"nfs_export=off"},
457*4882a593Smuzhiyun 	{OPT_XINO_ON,			"xino=on"},
458*4882a593Smuzhiyun 	{OPT_XINO_OFF,			"xino=off"},
459*4882a593Smuzhiyun 	{OPT_XINO_AUTO,			"xino=auto"},
460*4882a593Smuzhiyun 	{OPT_METACOPY_ON,		"metacopy=on"},
461*4882a593Smuzhiyun 	{OPT_METACOPY_OFF,		"metacopy=off"},
462*4882a593Smuzhiyun 	{OPT_VOLATILE,			"volatile"},
463*4882a593Smuzhiyun 	{OPT_OVERRIDE_CREDS_ON,		"override_creds=on"},
464*4882a593Smuzhiyun 	{OPT_OVERRIDE_CREDS_OFF,	"override_creds=off"},
465*4882a593Smuzhiyun 	{OPT_ERR,			NULL}
466*4882a593Smuzhiyun };
467*4882a593Smuzhiyun 
ovl_next_opt(char ** s)468*4882a593Smuzhiyun static char *ovl_next_opt(char **s)
469*4882a593Smuzhiyun {
470*4882a593Smuzhiyun 	char *sbegin = *s;
471*4882a593Smuzhiyun 	char *p;
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	if (sbegin == NULL)
474*4882a593Smuzhiyun 		return NULL;
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 	for (p = sbegin; *p; p++) {
477*4882a593Smuzhiyun 		if (*p == '\\') {
478*4882a593Smuzhiyun 			p++;
479*4882a593Smuzhiyun 			if (!*p)
480*4882a593Smuzhiyun 				break;
481*4882a593Smuzhiyun 		} else if (*p == ',') {
482*4882a593Smuzhiyun 			*p = '\0';
483*4882a593Smuzhiyun 			*s = p + 1;
484*4882a593Smuzhiyun 			return sbegin;
485*4882a593Smuzhiyun 		}
486*4882a593Smuzhiyun 	}
487*4882a593Smuzhiyun 	*s = NULL;
488*4882a593Smuzhiyun 	return sbegin;
489*4882a593Smuzhiyun }
490*4882a593Smuzhiyun 
ovl_parse_redirect_mode(struct ovl_config * config,const char * mode)491*4882a593Smuzhiyun static int ovl_parse_redirect_mode(struct ovl_config *config, const char *mode)
492*4882a593Smuzhiyun {
493*4882a593Smuzhiyun 	if (strcmp(mode, "on") == 0) {
494*4882a593Smuzhiyun 		config->redirect_dir = true;
495*4882a593Smuzhiyun 		/*
496*4882a593Smuzhiyun 		 * Does not make sense to have redirect creation without
497*4882a593Smuzhiyun 		 * redirect following.
498*4882a593Smuzhiyun 		 */
499*4882a593Smuzhiyun 		config->redirect_follow = true;
500*4882a593Smuzhiyun 	} else if (strcmp(mode, "follow") == 0) {
501*4882a593Smuzhiyun 		config->redirect_follow = true;
502*4882a593Smuzhiyun 	} else if (strcmp(mode, "off") == 0) {
503*4882a593Smuzhiyun 		if (ovl_redirect_always_follow)
504*4882a593Smuzhiyun 			config->redirect_follow = true;
505*4882a593Smuzhiyun 	} else if (strcmp(mode, "nofollow") != 0) {
506*4882a593Smuzhiyun 		pr_err("bad mount option \"redirect_dir=%s\"\n",
507*4882a593Smuzhiyun 		       mode);
508*4882a593Smuzhiyun 		return -EINVAL;
509*4882a593Smuzhiyun 	}
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun 	return 0;
512*4882a593Smuzhiyun }
513*4882a593Smuzhiyun 
ovl_parse_opt(char * opt,struct ovl_config * config)514*4882a593Smuzhiyun static int ovl_parse_opt(char *opt, struct ovl_config *config)
515*4882a593Smuzhiyun {
516*4882a593Smuzhiyun 	char *p;
517*4882a593Smuzhiyun 	int err;
518*4882a593Smuzhiyun 	bool metacopy_opt = false, redirect_opt = false;
519*4882a593Smuzhiyun 	bool nfs_export_opt = false, index_opt = false;
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 	config->redirect_mode = kstrdup(ovl_redirect_mode_def(), GFP_KERNEL);
522*4882a593Smuzhiyun 	if (!config->redirect_mode)
523*4882a593Smuzhiyun 		return -ENOMEM;
524*4882a593Smuzhiyun 	config->override_creds = ovl_override_creds_def;
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun 	while ((p = ovl_next_opt(&opt)) != NULL) {
527*4882a593Smuzhiyun 		int token;
528*4882a593Smuzhiyun 		substring_t args[MAX_OPT_ARGS];
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 		if (!*p)
531*4882a593Smuzhiyun 			continue;
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun 		token = match_token(p, ovl_tokens, args);
534*4882a593Smuzhiyun 		switch (token) {
535*4882a593Smuzhiyun 		case OPT_UPPERDIR:
536*4882a593Smuzhiyun 			kfree(config->upperdir);
537*4882a593Smuzhiyun 			config->upperdir = match_strdup(&args[0]);
538*4882a593Smuzhiyun 			if (!config->upperdir)
539*4882a593Smuzhiyun 				return -ENOMEM;
540*4882a593Smuzhiyun 			break;
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun 		case OPT_LOWERDIR:
543*4882a593Smuzhiyun 			kfree(config->lowerdir);
544*4882a593Smuzhiyun 			config->lowerdir = match_strdup(&args[0]);
545*4882a593Smuzhiyun 			if (!config->lowerdir)
546*4882a593Smuzhiyun 				return -ENOMEM;
547*4882a593Smuzhiyun 			break;
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun 		case OPT_WORKDIR:
550*4882a593Smuzhiyun 			kfree(config->workdir);
551*4882a593Smuzhiyun 			config->workdir = match_strdup(&args[0]);
552*4882a593Smuzhiyun 			if (!config->workdir)
553*4882a593Smuzhiyun 				return -ENOMEM;
554*4882a593Smuzhiyun 			break;
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun 		case OPT_DEFAULT_PERMISSIONS:
557*4882a593Smuzhiyun 			config->default_permissions = true;
558*4882a593Smuzhiyun 			break;
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun 		case OPT_REDIRECT_DIR:
561*4882a593Smuzhiyun 			kfree(config->redirect_mode);
562*4882a593Smuzhiyun 			config->redirect_mode = match_strdup(&args[0]);
563*4882a593Smuzhiyun 			if (!config->redirect_mode)
564*4882a593Smuzhiyun 				return -ENOMEM;
565*4882a593Smuzhiyun 			redirect_opt = true;
566*4882a593Smuzhiyun 			break;
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun 		case OPT_INDEX_ON:
569*4882a593Smuzhiyun 			config->index = true;
570*4882a593Smuzhiyun 			index_opt = true;
571*4882a593Smuzhiyun 			break;
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 		case OPT_INDEX_OFF:
574*4882a593Smuzhiyun 			config->index = false;
575*4882a593Smuzhiyun 			index_opt = true;
576*4882a593Smuzhiyun 			break;
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun 		case OPT_NFS_EXPORT_ON:
579*4882a593Smuzhiyun 			config->nfs_export = true;
580*4882a593Smuzhiyun 			nfs_export_opt = true;
581*4882a593Smuzhiyun 			break;
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun 		case OPT_NFS_EXPORT_OFF:
584*4882a593Smuzhiyun 			config->nfs_export = false;
585*4882a593Smuzhiyun 			nfs_export_opt = true;
586*4882a593Smuzhiyun 			break;
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 		case OPT_XINO_ON:
589*4882a593Smuzhiyun 			config->xino = OVL_XINO_ON;
590*4882a593Smuzhiyun 			break;
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun 		case OPT_XINO_OFF:
593*4882a593Smuzhiyun 			config->xino = OVL_XINO_OFF;
594*4882a593Smuzhiyun 			break;
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun 		case OPT_XINO_AUTO:
597*4882a593Smuzhiyun 			config->xino = OVL_XINO_AUTO;
598*4882a593Smuzhiyun 			break;
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 		case OPT_METACOPY_ON:
601*4882a593Smuzhiyun 			config->metacopy = true;
602*4882a593Smuzhiyun 			metacopy_opt = true;
603*4882a593Smuzhiyun 			break;
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun 		case OPT_METACOPY_OFF:
606*4882a593Smuzhiyun 			config->metacopy = false;
607*4882a593Smuzhiyun 			metacopy_opt = true;
608*4882a593Smuzhiyun 			break;
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun 		case OPT_VOLATILE:
611*4882a593Smuzhiyun 			config->ovl_volatile = true;
612*4882a593Smuzhiyun 			break;
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun 		case OPT_OVERRIDE_CREDS_ON:
615*4882a593Smuzhiyun 			config->override_creds = true;
616*4882a593Smuzhiyun 			break;
617*4882a593Smuzhiyun 
618*4882a593Smuzhiyun 		case OPT_OVERRIDE_CREDS_OFF:
619*4882a593Smuzhiyun 			config->override_creds = false;
620*4882a593Smuzhiyun 			break;
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 		default:
623*4882a593Smuzhiyun 			pr_err("unrecognized mount option \"%s\" or missing value\n",
624*4882a593Smuzhiyun 					p);
625*4882a593Smuzhiyun 			return -EINVAL;
626*4882a593Smuzhiyun 		}
627*4882a593Smuzhiyun 	}
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 	/* Workdir/index are useless in non-upper mount */
630*4882a593Smuzhiyun 	if (!config->upperdir) {
631*4882a593Smuzhiyun 		if (config->workdir) {
632*4882a593Smuzhiyun 			pr_info("option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
633*4882a593Smuzhiyun 				config->workdir);
634*4882a593Smuzhiyun 			kfree(config->workdir);
635*4882a593Smuzhiyun 			config->workdir = NULL;
636*4882a593Smuzhiyun 		}
637*4882a593Smuzhiyun 		if (config->index && index_opt) {
638*4882a593Smuzhiyun 			pr_info("option \"index=on\" is useless in a non-upper mount, ignore\n");
639*4882a593Smuzhiyun 			index_opt = false;
640*4882a593Smuzhiyun 		}
641*4882a593Smuzhiyun 		config->index = false;
642*4882a593Smuzhiyun 	}
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 	if (!config->upperdir && config->ovl_volatile) {
645*4882a593Smuzhiyun 		pr_info("option \"volatile\" is meaningless in a non-upper mount, ignoring it.\n");
646*4882a593Smuzhiyun 		config->ovl_volatile = false;
647*4882a593Smuzhiyun 	}
648*4882a593Smuzhiyun 
649*4882a593Smuzhiyun 	err = ovl_parse_redirect_mode(config, config->redirect_mode);
650*4882a593Smuzhiyun 	if (err)
651*4882a593Smuzhiyun 		return err;
652*4882a593Smuzhiyun 
653*4882a593Smuzhiyun 	/*
654*4882a593Smuzhiyun 	 * This is to make the logic below simpler.  It doesn't make any other
655*4882a593Smuzhiyun 	 * difference, since config->redirect_dir is only used for upper.
656*4882a593Smuzhiyun 	 */
657*4882a593Smuzhiyun 	if (!config->upperdir && config->redirect_follow)
658*4882a593Smuzhiyun 		config->redirect_dir = true;
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun 	/* Resolve metacopy -> redirect_dir dependency */
661*4882a593Smuzhiyun 	if (config->metacopy && !config->redirect_dir) {
662*4882a593Smuzhiyun 		if (metacopy_opt && redirect_opt) {
663*4882a593Smuzhiyun 			pr_err("conflicting options: metacopy=on,redirect_dir=%s\n",
664*4882a593Smuzhiyun 			       config->redirect_mode);
665*4882a593Smuzhiyun 			return -EINVAL;
666*4882a593Smuzhiyun 		}
667*4882a593Smuzhiyun 		if (redirect_opt) {
668*4882a593Smuzhiyun 			/*
669*4882a593Smuzhiyun 			 * There was an explicit redirect_dir=... that resulted
670*4882a593Smuzhiyun 			 * in this conflict.
671*4882a593Smuzhiyun 			 */
672*4882a593Smuzhiyun 			pr_info("disabling metacopy due to redirect_dir=%s\n",
673*4882a593Smuzhiyun 				config->redirect_mode);
674*4882a593Smuzhiyun 			config->metacopy = false;
675*4882a593Smuzhiyun 		} else {
676*4882a593Smuzhiyun 			/* Automatically enable redirect otherwise. */
677*4882a593Smuzhiyun 			config->redirect_follow = config->redirect_dir = true;
678*4882a593Smuzhiyun 		}
679*4882a593Smuzhiyun 	}
680*4882a593Smuzhiyun 
681*4882a593Smuzhiyun 	/* Resolve nfs_export -> index dependency */
682*4882a593Smuzhiyun 	if (config->nfs_export && !config->index) {
683*4882a593Smuzhiyun 		if (!config->upperdir && config->redirect_follow) {
684*4882a593Smuzhiyun 			pr_info("NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n");
685*4882a593Smuzhiyun 			config->nfs_export = false;
686*4882a593Smuzhiyun 		} else if (nfs_export_opt && index_opt) {
687*4882a593Smuzhiyun 			pr_err("conflicting options: nfs_export=on,index=off\n");
688*4882a593Smuzhiyun 			return -EINVAL;
689*4882a593Smuzhiyun 		} else if (index_opt) {
690*4882a593Smuzhiyun 			/*
691*4882a593Smuzhiyun 			 * There was an explicit index=off that resulted
692*4882a593Smuzhiyun 			 * in this conflict.
693*4882a593Smuzhiyun 			 */
694*4882a593Smuzhiyun 			pr_info("disabling nfs_export due to index=off\n");
695*4882a593Smuzhiyun 			config->nfs_export = false;
696*4882a593Smuzhiyun 		} else {
697*4882a593Smuzhiyun 			/* Automatically enable index otherwise. */
698*4882a593Smuzhiyun 			config->index = true;
699*4882a593Smuzhiyun 		}
700*4882a593Smuzhiyun 	}
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun 	/* Resolve nfs_export -> !metacopy dependency */
703*4882a593Smuzhiyun 	if (config->nfs_export && config->metacopy) {
704*4882a593Smuzhiyun 		if (nfs_export_opt && metacopy_opt) {
705*4882a593Smuzhiyun 			pr_err("conflicting options: nfs_export=on,metacopy=on\n");
706*4882a593Smuzhiyun 			return -EINVAL;
707*4882a593Smuzhiyun 		}
708*4882a593Smuzhiyun 		if (metacopy_opt) {
709*4882a593Smuzhiyun 			/*
710*4882a593Smuzhiyun 			 * There was an explicit metacopy=on that resulted
711*4882a593Smuzhiyun 			 * in this conflict.
712*4882a593Smuzhiyun 			 */
713*4882a593Smuzhiyun 			pr_info("disabling nfs_export due to metacopy=on\n");
714*4882a593Smuzhiyun 			config->nfs_export = false;
715*4882a593Smuzhiyun 		} else {
716*4882a593Smuzhiyun 			/*
717*4882a593Smuzhiyun 			 * There was an explicit nfs_export=on that resulted
718*4882a593Smuzhiyun 			 * in this conflict.
719*4882a593Smuzhiyun 			 */
720*4882a593Smuzhiyun 			pr_info("disabling metacopy due to nfs_export=on\n");
721*4882a593Smuzhiyun 			config->metacopy = false;
722*4882a593Smuzhiyun 		}
723*4882a593Smuzhiyun 	}
724*4882a593Smuzhiyun 
725*4882a593Smuzhiyun 	return 0;
726*4882a593Smuzhiyun }
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun #define OVL_WORKDIR_NAME "work"
729*4882a593Smuzhiyun #define OVL_INDEXDIR_NAME "index"
730*4882a593Smuzhiyun 
ovl_workdir_create(struct ovl_fs * ofs,const char * name,bool persist)731*4882a593Smuzhiyun static struct dentry *ovl_workdir_create(struct ovl_fs *ofs,
732*4882a593Smuzhiyun 					 const char *name, bool persist)
733*4882a593Smuzhiyun {
734*4882a593Smuzhiyun 	struct inode *dir =  ofs->workbasedir->d_inode;
735*4882a593Smuzhiyun 	struct vfsmount *mnt = ovl_upper_mnt(ofs);
736*4882a593Smuzhiyun 	struct dentry *work;
737*4882a593Smuzhiyun 	int err;
738*4882a593Smuzhiyun 	bool retried = false;
739*4882a593Smuzhiyun 
740*4882a593Smuzhiyun 	inode_lock_nested(dir, I_MUTEX_PARENT);
741*4882a593Smuzhiyun retry:
742*4882a593Smuzhiyun 	work = lookup_one_len(name, ofs->workbasedir, strlen(name));
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun 	if (!IS_ERR(work)) {
745*4882a593Smuzhiyun 		struct iattr attr = {
746*4882a593Smuzhiyun 			.ia_valid = ATTR_MODE,
747*4882a593Smuzhiyun 			.ia_mode = S_IFDIR | 0,
748*4882a593Smuzhiyun 		};
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 		if (work->d_inode) {
751*4882a593Smuzhiyun 			err = -EEXIST;
752*4882a593Smuzhiyun 			if (retried)
753*4882a593Smuzhiyun 				goto out_dput;
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun 			if (persist)
756*4882a593Smuzhiyun 				goto out_unlock;
757*4882a593Smuzhiyun 
758*4882a593Smuzhiyun 			retried = true;
759*4882a593Smuzhiyun 			err = ovl_workdir_cleanup(dir, mnt, work, 0);
760*4882a593Smuzhiyun 			dput(work);
761*4882a593Smuzhiyun 			if (err == -EINVAL) {
762*4882a593Smuzhiyun 				work = ERR_PTR(err);
763*4882a593Smuzhiyun 				goto out_unlock;
764*4882a593Smuzhiyun 			}
765*4882a593Smuzhiyun 			goto retry;
766*4882a593Smuzhiyun 		}
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 		err = ovl_mkdir_real(dir, &work, attr.ia_mode);
769*4882a593Smuzhiyun 		if (err)
770*4882a593Smuzhiyun 			goto out_dput;
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun 		/* Weird filesystem returning with hashed negative (kernfs)? */
773*4882a593Smuzhiyun 		err = -EINVAL;
774*4882a593Smuzhiyun 		if (d_really_is_negative(work))
775*4882a593Smuzhiyun 			goto out_dput;
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 		/*
778*4882a593Smuzhiyun 		 * Try to remove POSIX ACL xattrs from workdir.  We are good if:
779*4882a593Smuzhiyun 		 *
780*4882a593Smuzhiyun 		 * a) success (there was a POSIX ACL xattr and was removed)
781*4882a593Smuzhiyun 		 * b) -ENODATA (there was no POSIX ACL xattr)
782*4882a593Smuzhiyun 		 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
783*4882a593Smuzhiyun 		 *
784*4882a593Smuzhiyun 		 * There are various other error values that could effectively
785*4882a593Smuzhiyun 		 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
786*4882a593Smuzhiyun 		 * if the xattr name is too long), but the set of filesystems
787*4882a593Smuzhiyun 		 * allowed as upper are limited to "normal" ones, where checking
788*4882a593Smuzhiyun 		 * for the above two errors is sufficient.
789*4882a593Smuzhiyun 		 */
790*4882a593Smuzhiyun 		err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
791*4882a593Smuzhiyun 		if (err && err != -ENODATA && err != -EOPNOTSUPP)
792*4882a593Smuzhiyun 			goto out_dput;
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun 		err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
795*4882a593Smuzhiyun 		if (err && err != -ENODATA && err != -EOPNOTSUPP)
796*4882a593Smuzhiyun 			goto out_dput;
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun 		/* Clear any inherited mode bits */
799*4882a593Smuzhiyun 		inode_lock(work->d_inode);
800*4882a593Smuzhiyun 		err = notify_change(work, &attr, NULL);
801*4882a593Smuzhiyun 		inode_unlock(work->d_inode);
802*4882a593Smuzhiyun 		if (err)
803*4882a593Smuzhiyun 			goto out_dput;
804*4882a593Smuzhiyun 	} else {
805*4882a593Smuzhiyun 		err = PTR_ERR(work);
806*4882a593Smuzhiyun 		goto out_err;
807*4882a593Smuzhiyun 	}
808*4882a593Smuzhiyun out_unlock:
809*4882a593Smuzhiyun 	inode_unlock(dir);
810*4882a593Smuzhiyun 	return work;
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun out_dput:
813*4882a593Smuzhiyun 	dput(work);
814*4882a593Smuzhiyun out_err:
815*4882a593Smuzhiyun 	pr_warn("failed to create directory %s/%s (errno: %i); mounting read-only\n",
816*4882a593Smuzhiyun 		ofs->config.workdir, name, -err);
817*4882a593Smuzhiyun 	work = NULL;
818*4882a593Smuzhiyun 	goto out_unlock;
819*4882a593Smuzhiyun }
820*4882a593Smuzhiyun 
ovl_unescape(char * s)821*4882a593Smuzhiyun static void ovl_unescape(char *s)
822*4882a593Smuzhiyun {
823*4882a593Smuzhiyun 	char *d = s;
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun 	for (;; s++, d++) {
826*4882a593Smuzhiyun 		if (*s == '\\')
827*4882a593Smuzhiyun 			s++;
828*4882a593Smuzhiyun 		*d = *s;
829*4882a593Smuzhiyun 		if (!*s)
830*4882a593Smuzhiyun 			break;
831*4882a593Smuzhiyun 	}
832*4882a593Smuzhiyun }
833*4882a593Smuzhiyun 
ovl_mount_dir_noesc(const char * name,struct path * path)834*4882a593Smuzhiyun static int ovl_mount_dir_noesc(const char *name, struct path *path)
835*4882a593Smuzhiyun {
836*4882a593Smuzhiyun 	int err = -EINVAL;
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun 	if (!*name) {
839*4882a593Smuzhiyun 		pr_err("empty lowerdir\n");
840*4882a593Smuzhiyun 		goto out;
841*4882a593Smuzhiyun 	}
842*4882a593Smuzhiyun 	err = kern_path(name, LOOKUP_FOLLOW, path);
843*4882a593Smuzhiyun 	if (err) {
844*4882a593Smuzhiyun 		pr_err("failed to resolve '%s': %i\n", name, err);
845*4882a593Smuzhiyun 		goto out;
846*4882a593Smuzhiyun 	}
847*4882a593Smuzhiyun 	err = -EINVAL;
848*4882a593Smuzhiyun 	if (ovl_dentry_weird(path->dentry)) {
849*4882a593Smuzhiyun 		pr_err("filesystem on '%s' not supported\n", name);
850*4882a593Smuzhiyun 		goto out_put;
851*4882a593Smuzhiyun 	}
852*4882a593Smuzhiyun 	if (!d_is_dir(path->dentry)) {
853*4882a593Smuzhiyun 		pr_err("'%s' not a directory\n", name);
854*4882a593Smuzhiyun 		goto out_put;
855*4882a593Smuzhiyun 	}
856*4882a593Smuzhiyun 	return 0;
857*4882a593Smuzhiyun 
858*4882a593Smuzhiyun out_put:
859*4882a593Smuzhiyun 	path_put_init(path);
860*4882a593Smuzhiyun out:
861*4882a593Smuzhiyun 	return err;
862*4882a593Smuzhiyun }
863*4882a593Smuzhiyun 
ovl_mount_dir(const char * name,struct path * path)864*4882a593Smuzhiyun static int ovl_mount_dir(const char *name, struct path *path)
865*4882a593Smuzhiyun {
866*4882a593Smuzhiyun 	int err = -ENOMEM;
867*4882a593Smuzhiyun 	char *tmp = kstrdup(name, GFP_KERNEL);
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun 	if (tmp) {
870*4882a593Smuzhiyun 		ovl_unescape(tmp);
871*4882a593Smuzhiyun 		err = ovl_mount_dir_noesc(tmp, path);
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun 		if (!err && path->dentry->d_flags & DCACHE_OP_REAL) {
874*4882a593Smuzhiyun 			pr_err("filesystem on '%s' not supported as upperdir\n",
875*4882a593Smuzhiyun 			       tmp);
876*4882a593Smuzhiyun 			path_put_init(path);
877*4882a593Smuzhiyun 			err = -EINVAL;
878*4882a593Smuzhiyun 		}
879*4882a593Smuzhiyun 		kfree(tmp);
880*4882a593Smuzhiyun 	}
881*4882a593Smuzhiyun 	return err;
882*4882a593Smuzhiyun }
883*4882a593Smuzhiyun 
ovl_check_namelen(struct path * path,struct ovl_fs * ofs,const char * name)884*4882a593Smuzhiyun static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
885*4882a593Smuzhiyun 			     const char *name)
886*4882a593Smuzhiyun {
887*4882a593Smuzhiyun 	struct kstatfs statfs;
888*4882a593Smuzhiyun 	int err = vfs_statfs(path, &statfs);
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun 	if (err)
891*4882a593Smuzhiyun 		pr_err("statfs failed on '%s'\n", name);
892*4882a593Smuzhiyun 	else
893*4882a593Smuzhiyun 		ofs->namelen = max(ofs->namelen, statfs.f_namelen);
894*4882a593Smuzhiyun 
895*4882a593Smuzhiyun 	return err;
896*4882a593Smuzhiyun }
897*4882a593Smuzhiyun 
ovl_lower_dir(const char * name,struct path * path,struct ovl_fs * ofs,int * stack_depth)898*4882a593Smuzhiyun static int ovl_lower_dir(const char *name, struct path *path,
899*4882a593Smuzhiyun 			 struct ovl_fs *ofs, int *stack_depth)
900*4882a593Smuzhiyun {
901*4882a593Smuzhiyun 	int fh_type;
902*4882a593Smuzhiyun 	int err;
903*4882a593Smuzhiyun 
904*4882a593Smuzhiyun 	err = ovl_mount_dir_noesc(name, path);
905*4882a593Smuzhiyun 	if (err)
906*4882a593Smuzhiyun 		return err;
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun 	err = ovl_check_namelen(path, ofs, name);
909*4882a593Smuzhiyun 	if (err)
910*4882a593Smuzhiyun 		return err;
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun 	*stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun 	/*
915*4882a593Smuzhiyun 	 * The inodes index feature and NFS export need to encode and decode
916*4882a593Smuzhiyun 	 * file handles, so they require that all layers support them.
917*4882a593Smuzhiyun 	 */
918*4882a593Smuzhiyun 	fh_type = ovl_can_decode_fh(path->dentry->d_sb);
919*4882a593Smuzhiyun 	if ((ofs->config.nfs_export ||
920*4882a593Smuzhiyun 	     (ofs->config.index && ofs->config.upperdir)) && !fh_type) {
921*4882a593Smuzhiyun 		ofs->config.index = false;
922*4882a593Smuzhiyun 		ofs->config.nfs_export = false;
923*4882a593Smuzhiyun 		pr_warn("fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n",
924*4882a593Smuzhiyun 			name);
925*4882a593Smuzhiyun 	}
926*4882a593Smuzhiyun 
927*4882a593Smuzhiyun 	/* Check if lower fs has 32bit inode numbers */
928*4882a593Smuzhiyun 	if (fh_type != FILEID_INO32_GEN)
929*4882a593Smuzhiyun 		ofs->xino_mode = -1;
930*4882a593Smuzhiyun 
931*4882a593Smuzhiyun 	return 0;
932*4882a593Smuzhiyun }
933*4882a593Smuzhiyun 
934*4882a593Smuzhiyun /* Workdir should not be subdir of upperdir and vice versa */
ovl_workdir_ok(struct dentry * workdir,struct dentry * upperdir)935*4882a593Smuzhiyun static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
936*4882a593Smuzhiyun {
937*4882a593Smuzhiyun 	bool ok = false;
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun 	if (workdir != upperdir) {
940*4882a593Smuzhiyun 		ok = (lock_rename(workdir, upperdir) == NULL);
941*4882a593Smuzhiyun 		unlock_rename(workdir, upperdir);
942*4882a593Smuzhiyun 	}
943*4882a593Smuzhiyun 	return ok;
944*4882a593Smuzhiyun }
945*4882a593Smuzhiyun 
ovl_split_lowerdirs(char * str)946*4882a593Smuzhiyun static unsigned int ovl_split_lowerdirs(char *str)
947*4882a593Smuzhiyun {
948*4882a593Smuzhiyun 	unsigned int ctr = 1;
949*4882a593Smuzhiyun 	char *s, *d;
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun 	for (s = d = str;; s++, d++) {
952*4882a593Smuzhiyun 		if (*s == '\\') {
953*4882a593Smuzhiyun 			s++;
954*4882a593Smuzhiyun 		} else if (*s == ':') {
955*4882a593Smuzhiyun 			*d = '\0';
956*4882a593Smuzhiyun 			ctr++;
957*4882a593Smuzhiyun 			continue;
958*4882a593Smuzhiyun 		}
959*4882a593Smuzhiyun 		*d = *s;
960*4882a593Smuzhiyun 		if (!*s)
961*4882a593Smuzhiyun 			break;
962*4882a593Smuzhiyun 	}
963*4882a593Smuzhiyun 	return ctr;
964*4882a593Smuzhiyun }
965*4882a593Smuzhiyun 
966*4882a593Smuzhiyun static int __maybe_unused
ovl_posix_acl_xattr_get(const struct xattr_handler * handler,struct dentry * dentry,struct inode * inode,const char * name,void * buffer,size_t size,int flags)967*4882a593Smuzhiyun ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
968*4882a593Smuzhiyun 			struct dentry *dentry, struct inode *inode,
969*4882a593Smuzhiyun 			const char *name, void *buffer, size_t size, int flags)
970*4882a593Smuzhiyun {
971*4882a593Smuzhiyun 	return ovl_xattr_get(dentry, inode, handler->name, buffer, size, flags);
972*4882a593Smuzhiyun }
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun static int __maybe_unused
ovl_posix_acl_xattr_set(const struct xattr_handler * handler,struct dentry * dentry,struct inode * inode,const char * name,const void * value,size_t size,int flags)975*4882a593Smuzhiyun ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
976*4882a593Smuzhiyun 			struct dentry *dentry, struct inode *inode,
977*4882a593Smuzhiyun 			const char *name, const void *value,
978*4882a593Smuzhiyun 			size_t size, int flags)
979*4882a593Smuzhiyun {
980*4882a593Smuzhiyun 	struct dentry *workdir = ovl_workdir(dentry);
981*4882a593Smuzhiyun 	struct inode *realinode = ovl_inode_real(inode);
982*4882a593Smuzhiyun 	struct posix_acl *acl = NULL;
983*4882a593Smuzhiyun 	int err;
984*4882a593Smuzhiyun 
985*4882a593Smuzhiyun 	/* Check that everything is OK before copy-up */
986*4882a593Smuzhiyun 	if (value) {
987*4882a593Smuzhiyun 		acl = posix_acl_from_xattr(&init_user_ns, value, size);
988*4882a593Smuzhiyun 		if (IS_ERR(acl))
989*4882a593Smuzhiyun 			return PTR_ERR(acl);
990*4882a593Smuzhiyun 	}
991*4882a593Smuzhiyun 	err = -EOPNOTSUPP;
992*4882a593Smuzhiyun 	if (!IS_POSIXACL(d_inode(workdir)))
993*4882a593Smuzhiyun 		goto out_acl_release;
994*4882a593Smuzhiyun 	if (!realinode->i_op->set_acl)
995*4882a593Smuzhiyun 		goto out_acl_release;
996*4882a593Smuzhiyun 	if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
997*4882a593Smuzhiyun 		err = acl ? -EACCES : 0;
998*4882a593Smuzhiyun 		goto out_acl_release;
999*4882a593Smuzhiyun 	}
1000*4882a593Smuzhiyun 	err = -EPERM;
1001*4882a593Smuzhiyun 	if (!inode_owner_or_capable(inode))
1002*4882a593Smuzhiyun 		goto out_acl_release;
1003*4882a593Smuzhiyun 
1004*4882a593Smuzhiyun 	posix_acl_release(acl);
1005*4882a593Smuzhiyun 
1006*4882a593Smuzhiyun 	/*
1007*4882a593Smuzhiyun 	 * Check if sgid bit needs to be cleared (actual setacl operation will
1008*4882a593Smuzhiyun 	 * be done with mounter's capabilities and so that won't do it for us).
1009*4882a593Smuzhiyun 	 */
1010*4882a593Smuzhiyun 	if (unlikely(inode->i_mode & S_ISGID) &&
1011*4882a593Smuzhiyun 	    handler->flags == ACL_TYPE_ACCESS &&
1012*4882a593Smuzhiyun 	    !in_group_p(inode->i_gid) &&
1013*4882a593Smuzhiyun 	    !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
1014*4882a593Smuzhiyun 		struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
1015*4882a593Smuzhiyun 
1016*4882a593Smuzhiyun 		err = ovl_setattr(dentry, &iattr);
1017*4882a593Smuzhiyun 		if (err)
1018*4882a593Smuzhiyun 			return err;
1019*4882a593Smuzhiyun 	}
1020*4882a593Smuzhiyun 
1021*4882a593Smuzhiyun 	err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
1022*4882a593Smuzhiyun 	if (!err)
1023*4882a593Smuzhiyun 		ovl_copyattr(ovl_inode_real(inode), inode);
1024*4882a593Smuzhiyun 
1025*4882a593Smuzhiyun 	return err;
1026*4882a593Smuzhiyun 
1027*4882a593Smuzhiyun out_acl_release:
1028*4882a593Smuzhiyun 	posix_acl_release(acl);
1029*4882a593Smuzhiyun 	return err;
1030*4882a593Smuzhiyun }
1031*4882a593Smuzhiyun 
ovl_own_xattr_get(const struct xattr_handler * handler,struct dentry * dentry,struct inode * inode,const char * name,void * buffer,size_t size,int flags)1032*4882a593Smuzhiyun static int ovl_own_xattr_get(const struct xattr_handler *handler,
1033*4882a593Smuzhiyun 			     struct dentry *dentry, struct inode *inode,
1034*4882a593Smuzhiyun 			     const char *name, void *buffer, size_t size,
1035*4882a593Smuzhiyun 			     int flags)
1036*4882a593Smuzhiyun {
1037*4882a593Smuzhiyun 	return -EOPNOTSUPP;
1038*4882a593Smuzhiyun }
1039*4882a593Smuzhiyun 
ovl_own_xattr_set(const struct xattr_handler * handler,struct dentry * dentry,struct inode * inode,const char * name,const void * value,size_t size,int flags)1040*4882a593Smuzhiyun static int ovl_own_xattr_set(const struct xattr_handler *handler,
1041*4882a593Smuzhiyun 			     struct dentry *dentry, struct inode *inode,
1042*4882a593Smuzhiyun 			     const char *name, const void *value,
1043*4882a593Smuzhiyun 			     size_t size, int flags)
1044*4882a593Smuzhiyun {
1045*4882a593Smuzhiyun 	return -EOPNOTSUPP;
1046*4882a593Smuzhiyun }
1047*4882a593Smuzhiyun 
ovl_other_xattr_get(const struct xattr_handler * handler,struct dentry * dentry,struct inode * inode,const char * name,void * buffer,size_t size,int flags)1048*4882a593Smuzhiyun static int ovl_other_xattr_get(const struct xattr_handler *handler,
1049*4882a593Smuzhiyun 			       struct dentry *dentry, struct inode *inode,
1050*4882a593Smuzhiyun 			       const char *name, void *buffer, size_t size,
1051*4882a593Smuzhiyun 			       int flags)
1052*4882a593Smuzhiyun {
1053*4882a593Smuzhiyun 	return ovl_xattr_get(dentry, inode, name, buffer, size, flags);
1054*4882a593Smuzhiyun }
1055*4882a593Smuzhiyun 
ovl_other_xattr_set(const struct xattr_handler * handler,struct dentry * dentry,struct inode * inode,const char * name,const void * value,size_t size,int flags)1056*4882a593Smuzhiyun static int ovl_other_xattr_set(const struct xattr_handler *handler,
1057*4882a593Smuzhiyun 			       struct dentry *dentry, struct inode *inode,
1058*4882a593Smuzhiyun 			       const char *name, const void *value,
1059*4882a593Smuzhiyun 			       size_t size, int flags)
1060*4882a593Smuzhiyun {
1061*4882a593Smuzhiyun 	return ovl_xattr_set(dentry, inode, name, value, size, flags);
1062*4882a593Smuzhiyun }
1063*4882a593Smuzhiyun 
1064*4882a593Smuzhiyun static const struct xattr_handler __maybe_unused
1065*4882a593Smuzhiyun ovl_posix_acl_access_xattr_handler = {
1066*4882a593Smuzhiyun 	.name = XATTR_NAME_POSIX_ACL_ACCESS,
1067*4882a593Smuzhiyun 	.flags = ACL_TYPE_ACCESS,
1068*4882a593Smuzhiyun 	.get = ovl_posix_acl_xattr_get,
1069*4882a593Smuzhiyun 	.set = ovl_posix_acl_xattr_set,
1070*4882a593Smuzhiyun };
1071*4882a593Smuzhiyun 
1072*4882a593Smuzhiyun static const struct xattr_handler __maybe_unused
1073*4882a593Smuzhiyun ovl_posix_acl_default_xattr_handler = {
1074*4882a593Smuzhiyun 	.name = XATTR_NAME_POSIX_ACL_DEFAULT,
1075*4882a593Smuzhiyun 	.flags = ACL_TYPE_DEFAULT,
1076*4882a593Smuzhiyun 	.get = ovl_posix_acl_xattr_get,
1077*4882a593Smuzhiyun 	.set = ovl_posix_acl_xattr_set,
1078*4882a593Smuzhiyun };
1079*4882a593Smuzhiyun 
1080*4882a593Smuzhiyun static const struct xattr_handler ovl_own_xattr_handler = {
1081*4882a593Smuzhiyun 	.prefix	= OVL_XATTR_PREFIX,
1082*4882a593Smuzhiyun 	.get = ovl_own_xattr_get,
1083*4882a593Smuzhiyun 	.set = ovl_own_xattr_set,
1084*4882a593Smuzhiyun };
1085*4882a593Smuzhiyun 
1086*4882a593Smuzhiyun static const struct xattr_handler ovl_other_xattr_handler = {
1087*4882a593Smuzhiyun 	.prefix	= "", /* catch all */
1088*4882a593Smuzhiyun 	.get = ovl_other_xattr_get,
1089*4882a593Smuzhiyun 	.set = ovl_other_xattr_set,
1090*4882a593Smuzhiyun };
1091*4882a593Smuzhiyun 
1092*4882a593Smuzhiyun static const struct xattr_handler *ovl_xattr_handlers[] = {
1093*4882a593Smuzhiyun #ifdef CONFIG_FS_POSIX_ACL
1094*4882a593Smuzhiyun 	&ovl_posix_acl_access_xattr_handler,
1095*4882a593Smuzhiyun 	&ovl_posix_acl_default_xattr_handler,
1096*4882a593Smuzhiyun #endif
1097*4882a593Smuzhiyun 	&ovl_own_xattr_handler,
1098*4882a593Smuzhiyun 	&ovl_other_xattr_handler,
1099*4882a593Smuzhiyun 	NULL
1100*4882a593Smuzhiyun };
1101*4882a593Smuzhiyun 
ovl_setup_trap(struct super_block * sb,struct dentry * dir,struct inode ** ptrap,const char * name)1102*4882a593Smuzhiyun static int ovl_setup_trap(struct super_block *sb, struct dentry *dir,
1103*4882a593Smuzhiyun 			  struct inode **ptrap, const char *name)
1104*4882a593Smuzhiyun {
1105*4882a593Smuzhiyun 	struct inode *trap;
1106*4882a593Smuzhiyun 	int err;
1107*4882a593Smuzhiyun 
1108*4882a593Smuzhiyun 	trap = ovl_get_trap_inode(sb, dir);
1109*4882a593Smuzhiyun 	err = PTR_ERR_OR_ZERO(trap);
1110*4882a593Smuzhiyun 	if (err) {
1111*4882a593Smuzhiyun 		if (err == -ELOOP)
1112*4882a593Smuzhiyun 			pr_err("conflicting %s path\n", name);
1113*4882a593Smuzhiyun 		return err;
1114*4882a593Smuzhiyun 	}
1115*4882a593Smuzhiyun 
1116*4882a593Smuzhiyun 	*ptrap = trap;
1117*4882a593Smuzhiyun 	return 0;
1118*4882a593Smuzhiyun }
1119*4882a593Smuzhiyun 
1120*4882a593Smuzhiyun /*
1121*4882a593Smuzhiyun  * Determine how we treat concurrent use of upperdir/workdir based on the
1122*4882a593Smuzhiyun  * index feature. This is papering over mount leaks of container runtimes,
1123*4882a593Smuzhiyun  * for example, an old overlay mount is leaked and now its upperdir is
1124*4882a593Smuzhiyun  * attempted to be used as a lower layer in a new overlay mount.
1125*4882a593Smuzhiyun  */
ovl_report_in_use(struct ovl_fs * ofs,const char * name)1126*4882a593Smuzhiyun static int ovl_report_in_use(struct ovl_fs *ofs, const char *name)
1127*4882a593Smuzhiyun {
1128*4882a593Smuzhiyun 	if (ofs->config.index) {
1129*4882a593Smuzhiyun 		pr_err("%s is in-use as upperdir/workdir of another mount, mount with '-o index=off' to override exclusive upperdir protection.\n",
1130*4882a593Smuzhiyun 		       name);
1131*4882a593Smuzhiyun 		return -EBUSY;
1132*4882a593Smuzhiyun 	} else {
1133*4882a593Smuzhiyun 		pr_warn("%s is in-use as upperdir/workdir of another mount, accessing files from both mounts will result in undefined behavior.\n",
1134*4882a593Smuzhiyun 			name);
1135*4882a593Smuzhiyun 		return 0;
1136*4882a593Smuzhiyun 	}
1137*4882a593Smuzhiyun }
1138*4882a593Smuzhiyun 
ovl_get_upper(struct super_block * sb,struct ovl_fs * ofs,struct ovl_layer * upper_layer,struct path * upperpath)1139*4882a593Smuzhiyun static int ovl_get_upper(struct super_block *sb, struct ovl_fs *ofs,
1140*4882a593Smuzhiyun 			 struct ovl_layer *upper_layer, struct path *upperpath)
1141*4882a593Smuzhiyun {
1142*4882a593Smuzhiyun 	struct vfsmount *upper_mnt;
1143*4882a593Smuzhiyun 	int err;
1144*4882a593Smuzhiyun 
1145*4882a593Smuzhiyun 	err = ovl_mount_dir(ofs->config.upperdir, upperpath);
1146*4882a593Smuzhiyun 	if (err)
1147*4882a593Smuzhiyun 		goto out;
1148*4882a593Smuzhiyun 
1149*4882a593Smuzhiyun 	/* Upper fs should not be r/o */
1150*4882a593Smuzhiyun 	if (sb_rdonly(upperpath->mnt->mnt_sb)) {
1151*4882a593Smuzhiyun 		pr_err("upper fs is r/o, try multi-lower layers mount\n");
1152*4882a593Smuzhiyun 		err = -EINVAL;
1153*4882a593Smuzhiyun 		goto out;
1154*4882a593Smuzhiyun 	}
1155*4882a593Smuzhiyun 
1156*4882a593Smuzhiyun 	err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir);
1157*4882a593Smuzhiyun 	if (err)
1158*4882a593Smuzhiyun 		goto out;
1159*4882a593Smuzhiyun 
1160*4882a593Smuzhiyun 	err = ovl_setup_trap(sb, upperpath->dentry, &upper_layer->trap,
1161*4882a593Smuzhiyun 			     "upperdir");
1162*4882a593Smuzhiyun 	if (err)
1163*4882a593Smuzhiyun 		goto out;
1164*4882a593Smuzhiyun 
1165*4882a593Smuzhiyun 	upper_mnt = clone_private_mount(upperpath);
1166*4882a593Smuzhiyun 	err = PTR_ERR(upper_mnt);
1167*4882a593Smuzhiyun 	if (IS_ERR(upper_mnt)) {
1168*4882a593Smuzhiyun 		pr_err("failed to clone upperpath\n");
1169*4882a593Smuzhiyun 		goto out;
1170*4882a593Smuzhiyun 	}
1171*4882a593Smuzhiyun 
1172*4882a593Smuzhiyun 	/* Don't inherit atime flags */
1173*4882a593Smuzhiyun 	upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
1174*4882a593Smuzhiyun 	upper_layer->mnt = upper_mnt;
1175*4882a593Smuzhiyun 	upper_layer->idx = 0;
1176*4882a593Smuzhiyun 	upper_layer->fsid = 0;
1177*4882a593Smuzhiyun 
1178*4882a593Smuzhiyun 	/*
1179*4882a593Smuzhiyun 	 * Inherit SB_NOSEC flag from upperdir.
1180*4882a593Smuzhiyun 	 *
1181*4882a593Smuzhiyun 	 * This optimization changes behavior when a security related attribute
1182*4882a593Smuzhiyun 	 * (suid/sgid/security.*) is changed on an underlying layer.  This is
1183*4882a593Smuzhiyun 	 * okay because we don't yet have guarantees in that case, but it will
1184*4882a593Smuzhiyun 	 * need careful treatment once we want to honour changes to underlying
1185*4882a593Smuzhiyun 	 * filesystems.
1186*4882a593Smuzhiyun 	 */
1187*4882a593Smuzhiyun 	if (upper_mnt->mnt_sb->s_flags & SB_NOSEC)
1188*4882a593Smuzhiyun 		sb->s_flags |= SB_NOSEC;
1189*4882a593Smuzhiyun 
1190*4882a593Smuzhiyun 	if (ovl_inuse_trylock(ovl_upper_mnt(ofs)->mnt_root)) {
1191*4882a593Smuzhiyun 		ofs->upperdir_locked = true;
1192*4882a593Smuzhiyun 	} else {
1193*4882a593Smuzhiyun 		err = ovl_report_in_use(ofs, "upperdir");
1194*4882a593Smuzhiyun 		if (err)
1195*4882a593Smuzhiyun 			goto out;
1196*4882a593Smuzhiyun 	}
1197*4882a593Smuzhiyun 
1198*4882a593Smuzhiyun 	err = 0;
1199*4882a593Smuzhiyun out:
1200*4882a593Smuzhiyun 	return err;
1201*4882a593Smuzhiyun }
1202*4882a593Smuzhiyun 
1203*4882a593Smuzhiyun /*
1204*4882a593Smuzhiyun  * Returns 1 if RENAME_WHITEOUT is supported, 0 if not supported and
1205*4882a593Smuzhiyun  * negative values if error is encountered.
1206*4882a593Smuzhiyun  */
ovl_check_rename_whiteout(struct dentry * workdir)1207*4882a593Smuzhiyun static int ovl_check_rename_whiteout(struct dentry *workdir)
1208*4882a593Smuzhiyun {
1209*4882a593Smuzhiyun 	struct inode *dir = d_inode(workdir);
1210*4882a593Smuzhiyun 	struct dentry *temp;
1211*4882a593Smuzhiyun 	struct dentry *dest;
1212*4882a593Smuzhiyun 	struct dentry *whiteout;
1213*4882a593Smuzhiyun 	struct name_snapshot name;
1214*4882a593Smuzhiyun 	int err;
1215*4882a593Smuzhiyun 
1216*4882a593Smuzhiyun 	inode_lock_nested(dir, I_MUTEX_PARENT);
1217*4882a593Smuzhiyun 
1218*4882a593Smuzhiyun 	temp = ovl_create_temp(workdir, OVL_CATTR(S_IFREG | 0));
1219*4882a593Smuzhiyun 	err = PTR_ERR(temp);
1220*4882a593Smuzhiyun 	if (IS_ERR(temp))
1221*4882a593Smuzhiyun 		goto out_unlock;
1222*4882a593Smuzhiyun 
1223*4882a593Smuzhiyun 	dest = ovl_lookup_temp(workdir);
1224*4882a593Smuzhiyun 	err = PTR_ERR(dest);
1225*4882a593Smuzhiyun 	if (IS_ERR(dest)) {
1226*4882a593Smuzhiyun 		dput(temp);
1227*4882a593Smuzhiyun 		goto out_unlock;
1228*4882a593Smuzhiyun 	}
1229*4882a593Smuzhiyun 
1230*4882a593Smuzhiyun 	/* Name is inline and stable - using snapshot as a copy helper */
1231*4882a593Smuzhiyun 	take_dentry_name_snapshot(&name, temp);
1232*4882a593Smuzhiyun 	err = ovl_do_rename(dir, temp, dir, dest, RENAME_WHITEOUT);
1233*4882a593Smuzhiyun 	if (err) {
1234*4882a593Smuzhiyun 		if (err == -EINVAL)
1235*4882a593Smuzhiyun 			err = 0;
1236*4882a593Smuzhiyun 		goto cleanup_temp;
1237*4882a593Smuzhiyun 	}
1238*4882a593Smuzhiyun 
1239*4882a593Smuzhiyun 	whiteout = lookup_one_len(name.name.name, workdir, name.name.len);
1240*4882a593Smuzhiyun 	err = PTR_ERR(whiteout);
1241*4882a593Smuzhiyun 	if (IS_ERR(whiteout))
1242*4882a593Smuzhiyun 		goto cleanup_temp;
1243*4882a593Smuzhiyun 
1244*4882a593Smuzhiyun 	err = ovl_is_whiteout(whiteout);
1245*4882a593Smuzhiyun 
1246*4882a593Smuzhiyun 	/* Best effort cleanup of whiteout and temp file */
1247*4882a593Smuzhiyun 	if (err)
1248*4882a593Smuzhiyun 		ovl_cleanup(dir, whiteout);
1249*4882a593Smuzhiyun 	dput(whiteout);
1250*4882a593Smuzhiyun 
1251*4882a593Smuzhiyun cleanup_temp:
1252*4882a593Smuzhiyun 	ovl_cleanup(dir, temp);
1253*4882a593Smuzhiyun 	release_dentry_name_snapshot(&name);
1254*4882a593Smuzhiyun 	dput(temp);
1255*4882a593Smuzhiyun 	dput(dest);
1256*4882a593Smuzhiyun 
1257*4882a593Smuzhiyun out_unlock:
1258*4882a593Smuzhiyun 	inode_unlock(dir);
1259*4882a593Smuzhiyun 
1260*4882a593Smuzhiyun 	return err;
1261*4882a593Smuzhiyun }
1262*4882a593Smuzhiyun 
ovl_lookup_or_create(struct dentry * parent,const char * name,umode_t mode)1263*4882a593Smuzhiyun static struct dentry *ovl_lookup_or_create(struct dentry *parent,
1264*4882a593Smuzhiyun 					   const char *name, umode_t mode)
1265*4882a593Smuzhiyun {
1266*4882a593Smuzhiyun 	size_t len = strlen(name);
1267*4882a593Smuzhiyun 	struct dentry *child;
1268*4882a593Smuzhiyun 
1269*4882a593Smuzhiyun 	inode_lock_nested(parent->d_inode, I_MUTEX_PARENT);
1270*4882a593Smuzhiyun 	child = lookup_one_len(name, parent, len);
1271*4882a593Smuzhiyun 	if (!IS_ERR(child) && !child->d_inode)
1272*4882a593Smuzhiyun 		child = ovl_create_real(parent->d_inode, child,
1273*4882a593Smuzhiyun 					OVL_CATTR(mode));
1274*4882a593Smuzhiyun 	inode_unlock(parent->d_inode);
1275*4882a593Smuzhiyun 	dput(parent);
1276*4882a593Smuzhiyun 
1277*4882a593Smuzhiyun 	return child;
1278*4882a593Smuzhiyun }
1279*4882a593Smuzhiyun 
1280*4882a593Smuzhiyun /*
1281*4882a593Smuzhiyun  * Creates $workdir/work/incompat/volatile/dirty file if it is not already
1282*4882a593Smuzhiyun  * present.
1283*4882a593Smuzhiyun  */
ovl_create_volatile_dirty(struct ovl_fs * ofs)1284*4882a593Smuzhiyun static int ovl_create_volatile_dirty(struct ovl_fs *ofs)
1285*4882a593Smuzhiyun {
1286*4882a593Smuzhiyun 	unsigned int ctr;
1287*4882a593Smuzhiyun 	struct dentry *d = dget(ofs->workbasedir);
1288*4882a593Smuzhiyun 	static const char *const volatile_path[] = {
1289*4882a593Smuzhiyun 		OVL_WORKDIR_NAME, "incompat", "volatile", "dirty"
1290*4882a593Smuzhiyun 	};
1291*4882a593Smuzhiyun 	const char *const *name = volatile_path;
1292*4882a593Smuzhiyun 
1293*4882a593Smuzhiyun 	for (ctr = ARRAY_SIZE(volatile_path); ctr; ctr--, name++) {
1294*4882a593Smuzhiyun 		d = ovl_lookup_or_create(d, *name, ctr > 1 ? S_IFDIR : S_IFREG);
1295*4882a593Smuzhiyun 		if (IS_ERR(d))
1296*4882a593Smuzhiyun 			return PTR_ERR(d);
1297*4882a593Smuzhiyun 	}
1298*4882a593Smuzhiyun 	dput(d);
1299*4882a593Smuzhiyun 	return 0;
1300*4882a593Smuzhiyun }
1301*4882a593Smuzhiyun 
ovl_make_workdir(struct super_block * sb,struct ovl_fs * ofs,struct path * workpath)1302*4882a593Smuzhiyun static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs,
1303*4882a593Smuzhiyun 			    struct path *workpath)
1304*4882a593Smuzhiyun {
1305*4882a593Smuzhiyun 	struct vfsmount *mnt = ovl_upper_mnt(ofs);
1306*4882a593Smuzhiyun 	struct dentry *temp, *workdir;
1307*4882a593Smuzhiyun 	bool rename_whiteout;
1308*4882a593Smuzhiyun 	bool d_type;
1309*4882a593Smuzhiyun 	int fh_type;
1310*4882a593Smuzhiyun 	int err;
1311*4882a593Smuzhiyun 
1312*4882a593Smuzhiyun 	err = mnt_want_write(mnt);
1313*4882a593Smuzhiyun 	if (err)
1314*4882a593Smuzhiyun 		return err;
1315*4882a593Smuzhiyun 
1316*4882a593Smuzhiyun 	workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false);
1317*4882a593Smuzhiyun 	err = PTR_ERR(workdir);
1318*4882a593Smuzhiyun 	if (IS_ERR_OR_NULL(workdir))
1319*4882a593Smuzhiyun 		goto out;
1320*4882a593Smuzhiyun 
1321*4882a593Smuzhiyun 	ofs->workdir = workdir;
1322*4882a593Smuzhiyun 
1323*4882a593Smuzhiyun 	err = ovl_setup_trap(sb, ofs->workdir, &ofs->workdir_trap, "workdir");
1324*4882a593Smuzhiyun 	if (err)
1325*4882a593Smuzhiyun 		goto out;
1326*4882a593Smuzhiyun 
1327*4882a593Smuzhiyun 	/*
1328*4882a593Smuzhiyun 	 * Upper should support d_type, else whiteouts are visible.  Given
1329*4882a593Smuzhiyun 	 * workdir and upper are on same fs, we can do iterate_dir() on
1330*4882a593Smuzhiyun 	 * workdir. This check requires successful creation of workdir in
1331*4882a593Smuzhiyun 	 * previous step.
1332*4882a593Smuzhiyun 	 */
1333*4882a593Smuzhiyun 	err = ovl_check_d_type_supported(workpath);
1334*4882a593Smuzhiyun 	if (err < 0)
1335*4882a593Smuzhiyun 		goto out;
1336*4882a593Smuzhiyun 
1337*4882a593Smuzhiyun 	d_type = err;
1338*4882a593Smuzhiyun 	if (!d_type)
1339*4882a593Smuzhiyun 		pr_warn("upper fs needs to support d_type.\n");
1340*4882a593Smuzhiyun 
1341*4882a593Smuzhiyun 	/* Check if upper/work fs supports O_TMPFILE */
1342*4882a593Smuzhiyun 	temp = ovl_do_tmpfile(ofs->workdir, S_IFREG | 0);
1343*4882a593Smuzhiyun 	ofs->tmpfile = !IS_ERR(temp);
1344*4882a593Smuzhiyun 	if (ofs->tmpfile)
1345*4882a593Smuzhiyun 		dput(temp);
1346*4882a593Smuzhiyun 	else
1347*4882a593Smuzhiyun 		pr_warn("upper fs does not support tmpfile.\n");
1348*4882a593Smuzhiyun 
1349*4882a593Smuzhiyun 
1350*4882a593Smuzhiyun 	/* Check if upper/work fs supports RENAME_WHITEOUT */
1351*4882a593Smuzhiyun 	err = ovl_check_rename_whiteout(ofs->workdir);
1352*4882a593Smuzhiyun 	if (err < 0)
1353*4882a593Smuzhiyun 		goto out;
1354*4882a593Smuzhiyun 
1355*4882a593Smuzhiyun 	rename_whiteout = err;
1356*4882a593Smuzhiyun 	if (!rename_whiteout)
1357*4882a593Smuzhiyun 		pr_warn("upper fs does not support RENAME_WHITEOUT.\n");
1358*4882a593Smuzhiyun 
1359*4882a593Smuzhiyun 	/*
1360*4882a593Smuzhiyun 	 * Check if upper/work fs supports trusted.overlay.* xattr
1361*4882a593Smuzhiyun 	 */
1362*4882a593Smuzhiyun 	err = ovl_do_setxattr(ofs, ofs->workdir, OVL_XATTR_OPAQUE, "0", 1);
1363*4882a593Smuzhiyun 	if (err) {
1364*4882a593Smuzhiyun 		ofs->noxattr = true;
1365*4882a593Smuzhiyun 		ofs->config.index = false;
1366*4882a593Smuzhiyun 		ofs->config.metacopy = false;
1367*4882a593Smuzhiyun 		pr_warn("upper fs does not support xattr, falling back to index=off and metacopy=off.\n");
1368*4882a593Smuzhiyun 		err = 0;
1369*4882a593Smuzhiyun 	} else {
1370*4882a593Smuzhiyun 		ovl_do_removexattr(ofs, ofs->workdir, OVL_XATTR_OPAQUE);
1371*4882a593Smuzhiyun 	}
1372*4882a593Smuzhiyun 
1373*4882a593Smuzhiyun 	/*
1374*4882a593Smuzhiyun 	 * We allowed sub-optimal upper fs configuration and don't want to break
1375*4882a593Smuzhiyun 	 * users over kernel upgrade, but we never allowed remote upper fs, so
1376*4882a593Smuzhiyun 	 * we can enforce strict requirements for remote upper fs.
1377*4882a593Smuzhiyun 	 */
1378*4882a593Smuzhiyun 	if (ovl_dentry_remote(ofs->workdir) &&
1379*4882a593Smuzhiyun 	    (!d_type || !rename_whiteout || ofs->noxattr)) {
1380*4882a593Smuzhiyun 		pr_err("upper fs missing required features.\n");
1381*4882a593Smuzhiyun 		err = -EINVAL;
1382*4882a593Smuzhiyun 		goto out;
1383*4882a593Smuzhiyun 	}
1384*4882a593Smuzhiyun 
1385*4882a593Smuzhiyun 	/*
1386*4882a593Smuzhiyun 	 * For volatile mount, create a incompat/volatile/dirty file to keep
1387*4882a593Smuzhiyun 	 * track of it.
1388*4882a593Smuzhiyun 	 */
1389*4882a593Smuzhiyun 	if (ofs->config.ovl_volatile) {
1390*4882a593Smuzhiyun 		err = ovl_create_volatile_dirty(ofs);
1391*4882a593Smuzhiyun 		if (err < 0) {
1392*4882a593Smuzhiyun 			pr_err("Failed to create volatile/dirty file.\n");
1393*4882a593Smuzhiyun 			goto out;
1394*4882a593Smuzhiyun 		}
1395*4882a593Smuzhiyun 	}
1396*4882a593Smuzhiyun 
1397*4882a593Smuzhiyun 	/* Check if upper/work fs supports file handles */
1398*4882a593Smuzhiyun 	fh_type = ovl_can_decode_fh(ofs->workdir->d_sb);
1399*4882a593Smuzhiyun 	if (ofs->config.index && !fh_type) {
1400*4882a593Smuzhiyun 		ofs->config.index = false;
1401*4882a593Smuzhiyun 		pr_warn("upper fs does not support file handles, falling back to index=off.\n");
1402*4882a593Smuzhiyun 	}
1403*4882a593Smuzhiyun 
1404*4882a593Smuzhiyun 	/* Check if upper fs has 32bit inode numbers */
1405*4882a593Smuzhiyun 	if (fh_type != FILEID_INO32_GEN)
1406*4882a593Smuzhiyun 		ofs->xino_mode = -1;
1407*4882a593Smuzhiyun 
1408*4882a593Smuzhiyun 	/* NFS export of r/w mount depends on index */
1409*4882a593Smuzhiyun 	if (ofs->config.nfs_export && !ofs->config.index) {
1410*4882a593Smuzhiyun 		pr_warn("NFS export requires \"index=on\", falling back to nfs_export=off.\n");
1411*4882a593Smuzhiyun 		ofs->config.nfs_export = false;
1412*4882a593Smuzhiyun 	}
1413*4882a593Smuzhiyun out:
1414*4882a593Smuzhiyun 	mnt_drop_write(mnt);
1415*4882a593Smuzhiyun 	return err;
1416*4882a593Smuzhiyun }
1417*4882a593Smuzhiyun 
ovl_get_workdir(struct super_block * sb,struct ovl_fs * ofs,struct path * upperpath)1418*4882a593Smuzhiyun static int ovl_get_workdir(struct super_block *sb, struct ovl_fs *ofs,
1419*4882a593Smuzhiyun 			   struct path *upperpath)
1420*4882a593Smuzhiyun {
1421*4882a593Smuzhiyun 	int err;
1422*4882a593Smuzhiyun 	struct path workpath = { };
1423*4882a593Smuzhiyun 
1424*4882a593Smuzhiyun 	err = ovl_mount_dir(ofs->config.workdir, &workpath);
1425*4882a593Smuzhiyun 	if (err)
1426*4882a593Smuzhiyun 		goto out;
1427*4882a593Smuzhiyun 
1428*4882a593Smuzhiyun 	err = -EINVAL;
1429*4882a593Smuzhiyun 	if (upperpath->mnt != workpath.mnt) {
1430*4882a593Smuzhiyun 		pr_err("workdir and upperdir must reside under the same mount\n");
1431*4882a593Smuzhiyun 		goto out;
1432*4882a593Smuzhiyun 	}
1433*4882a593Smuzhiyun 	if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) {
1434*4882a593Smuzhiyun 		pr_err("workdir and upperdir must be separate subtrees\n");
1435*4882a593Smuzhiyun 		goto out;
1436*4882a593Smuzhiyun 	}
1437*4882a593Smuzhiyun 
1438*4882a593Smuzhiyun 	ofs->workbasedir = dget(workpath.dentry);
1439*4882a593Smuzhiyun 
1440*4882a593Smuzhiyun 	if (ovl_inuse_trylock(ofs->workbasedir)) {
1441*4882a593Smuzhiyun 		ofs->workdir_locked = true;
1442*4882a593Smuzhiyun 	} else {
1443*4882a593Smuzhiyun 		err = ovl_report_in_use(ofs, "workdir");
1444*4882a593Smuzhiyun 		if (err)
1445*4882a593Smuzhiyun 			goto out;
1446*4882a593Smuzhiyun 	}
1447*4882a593Smuzhiyun 
1448*4882a593Smuzhiyun 	err = ovl_setup_trap(sb, ofs->workbasedir, &ofs->workbasedir_trap,
1449*4882a593Smuzhiyun 			     "workdir");
1450*4882a593Smuzhiyun 	if (err)
1451*4882a593Smuzhiyun 		goto out;
1452*4882a593Smuzhiyun 
1453*4882a593Smuzhiyun 	err = ovl_make_workdir(sb, ofs, &workpath);
1454*4882a593Smuzhiyun 
1455*4882a593Smuzhiyun out:
1456*4882a593Smuzhiyun 	path_put(&workpath);
1457*4882a593Smuzhiyun 
1458*4882a593Smuzhiyun 	return err;
1459*4882a593Smuzhiyun }
1460*4882a593Smuzhiyun 
ovl_get_indexdir(struct super_block * sb,struct ovl_fs * ofs,struct ovl_entry * oe,struct path * upperpath)1461*4882a593Smuzhiyun static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs,
1462*4882a593Smuzhiyun 			    struct ovl_entry *oe, struct path *upperpath)
1463*4882a593Smuzhiyun {
1464*4882a593Smuzhiyun 	struct vfsmount *mnt = ovl_upper_mnt(ofs);
1465*4882a593Smuzhiyun 	struct dentry *indexdir;
1466*4882a593Smuzhiyun 	int err;
1467*4882a593Smuzhiyun 
1468*4882a593Smuzhiyun 	err = mnt_want_write(mnt);
1469*4882a593Smuzhiyun 	if (err)
1470*4882a593Smuzhiyun 		return err;
1471*4882a593Smuzhiyun 
1472*4882a593Smuzhiyun 	/* Verify lower root is upper root origin */
1473*4882a593Smuzhiyun 	err = ovl_verify_origin(ofs, upperpath->dentry,
1474*4882a593Smuzhiyun 				oe->lowerstack[0].dentry, true);
1475*4882a593Smuzhiyun 	if (err) {
1476*4882a593Smuzhiyun 		pr_err("failed to verify upper root origin\n");
1477*4882a593Smuzhiyun 		goto out;
1478*4882a593Smuzhiyun 	}
1479*4882a593Smuzhiyun 
1480*4882a593Smuzhiyun 	/* index dir will act also as workdir */
1481*4882a593Smuzhiyun 	iput(ofs->workdir_trap);
1482*4882a593Smuzhiyun 	ofs->workdir_trap = NULL;
1483*4882a593Smuzhiyun 	dput(ofs->workdir);
1484*4882a593Smuzhiyun 	ofs->workdir = NULL;
1485*4882a593Smuzhiyun 	indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true);
1486*4882a593Smuzhiyun 	if (IS_ERR(indexdir)) {
1487*4882a593Smuzhiyun 		err = PTR_ERR(indexdir);
1488*4882a593Smuzhiyun 	} else if (indexdir) {
1489*4882a593Smuzhiyun 		ofs->indexdir = indexdir;
1490*4882a593Smuzhiyun 		ofs->workdir = dget(indexdir);
1491*4882a593Smuzhiyun 
1492*4882a593Smuzhiyun 		err = ovl_setup_trap(sb, ofs->indexdir, &ofs->indexdir_trap,
1493*4882a593Smuzhiyun 				     "indexdir");
1494*4882a593Smuzhiyun 		if (err)
1495*4882a593Smuzhiyun 			goto out;
1496*4882a593Smuzhiyun 
1497*4882a593Smuzhiyun 		/*
1498*4882a593Smuzhiyun 		 * Verify upper root is exclusively associated with index dir.
1499*4882a593Smuzhiyun 		 * Older kernels stored upper fh in "trusted.overlay.origin"
1500*4882a593Smuzhiyun 		 * xattr. If that xattr exists, verify that it is a match to
1501*4882a593Smuzhiyun 		 * upper dir file handle. In any case, verify or set xattr
1502*4882a593Smuzhiyun 		 * "trusted.overlay.upper" to indicate that index may have
1503*4882a593Smuzhiyun 		 * directory entries.
1504*4882a593Smuzhiyun 		 */
1505*4882a593Smuzhiyun 		if (ovl_check_origin_xattr(ofs, ofs->indexdir)) {
1506*4882a593Smuzhiyun 			err = ovl_verify_set_fh(ofs, ofs->indexdir,
1507*4882a593Smuzhiyun 						OVL_XATTR_ORIGIN,
1508*4882a593Smuzhiyun 						upperpath->dentry, true, false);
1509*4882a593Smuzhiyun 			if (err)
1510*4882a593Smuzhiyun 				pr_err("failed to verify index dir 'origin' xattr\n");
1511*4882a593Smuzhiyun 		}
1512*4882a593Smuzhiyun 		err = ovl_verify_upper(ofs, ofs->indexdir, upperpath->dentry,
1513*4882a593Smuzhiyun 				       true);
1514*4882a593Smuzhiyun 		if (err)
1515*4882a593Smuzhiyun 			pr_err("failed to verify index dir 'upper' xattr\n");
1516*4882a593Smuzhiyun 
1517*4882a593Smuzhiyun 		/* Cleanup bad/stale/orphan index entries */
1518*4882a593Smuzhiyun 		if (!err)
1519*4882a593Smuzhiyun 			err = ovl_indexdir_cleanup(ofs);
1520*4882a593Smuzhiyun 	}
1521*4882a593Smuzhiyun 	if (err || !ofs->indexdir)
1522*4882a593Smuzhiyun 		pr_warn("try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
1523*4882a593Smuzhiyun 
1524*4882a593Smuzhiyun out:
1525*4882a593Smuzhiyun 	mnt_drop_write(mnt);
1526*4882a593Smuzhiyun 	return err;
1527*4882a593Smuzhiyun }
1528*4882a593Smuzhiyun 
ovl_lower_uuid_ok(struct ovl_fs * ofs,const uuid_t * uuid)1529*4882a593Smuzhiyun static bool ovl_lower_uuid_ok(struct ovl_fs *ofs, const uuid_t *uuid)
1530*4882a593Smuzhiyun {
1531*4882a593Smuzhiyun 	unsigned int i;
1532*4882a593Smuzhiyun 
1533*4882a593Smuzhiyun 	if (!ofs->config.nfs_export && !ovl_upper_mnt(ofs))
1534*4882a593Smuzhiyun 		return true;
1535*4882a593Smuzhiyun 
1536*4882a593Smuzhiyun 	/*
1537*4882a593Smuzhiyun 	 * We allow using single lower with null uuid for index and nfs_export
1538*4882a593Smuzhiyun 	 * for example to support those features with single lower squashfs.
1539*4882a593Smuzhiyun 	 * To avoid regressions in setups of overlay with re-formatted lower
1540*4882a593Smuzhiyun 	 * squashfs, do not allow decoding origin with lower null uuid unless
1541*4882a593Smuzhiyun 	 * user opted-in to one of the new features that require following the
1542*4882a593Smuzhiyun 	 * lower inode of non-dir upper.
1543*4882a593Smuzhiyun 	 */
1544*4882a593Smuzhiyun 	if (!ofs->config.index && !ofs->config.metacopy && !ofs->config.xino &&
1545*4882a593Smuzhiyun 	    uuid_is_null(uuid))
1546*4882a593Smuzhiyun 		return false;
1547*4882a593Smuzhiyun 
1548*4882a593Smuzhiyun 	for (i = 0; i < ofs->numfs; i++) {
1549*4882a593Smuzhiyun 		/*
1550*4882a593Smuzhiyun 		 * We use uuid to associate an overlay lower file handle with a
1551*4882a593Smuzhiyun 		 * lower layer, so we can accept lower fs with null uuid as long
1552*4882a593Smuzhiyun 		 * as all lower layers with null uuid are on the same fs.
1553*4882a593Smuzhiyun 		 * if we detect multiple lower fs with the same uuid, we
1554*4882a593Smuzhiyun 		 * disable lower file handle decoding on all of them.
1555*4882a593Smuzhiyun 		 */
1556*4882a593Smuzhiyun 		if (ofs->fs[i].is_lower &&
1557*4882a593Smuzhiyun 		    uuid_equal(&ofs->fs[i].sb->s_uuid, uuid)) {
1558*4882a593Smuzhiyun 			ofs->fs[i].bad_uuid = true;
1559*4882a593Smuzhiyun 			return false;
1560*4882a593Smuzhiyun 		}
1561*4882a593Smuzhiyun 	}
1562*4882a593Smuzhiyun 	return true;
1563*4882a593Smuzhiyun }
1564*4882a593Smuzhiyun 
1565*4882a593Smuzhiyun /* Get a unique fsid for the layer */
ovl_get_fsid(struct ovl_fs * ofs,const struct path * path)1566*4882a593Smuzhiyun static int ovl_get_fsid(struct ovl_fs *ofs, const struct path *path)
1567*4882a593Smuzhiyun {
1568*4882a593Smuzhiyun 	struct super_block *sb = path->mnt->mnt_sb;
1569*4882a593Smuzhiyun 	unsigned int i;
1570*4882a593Smuzhiyun 	dev_t dev;
1571*4882a593Smuzhiyun 	int err;
1572*4882a593Smuzhiyun 	bool bad_uuid = false;
1573*4882a593Smuzhiyun 
1574*4882a593Smuzhiyun 	for (i = 0; i < ofs->numfs; i++) {
1575*4882a593Smuzhiyun 		if (ofs->fs[i].sb == sb)
1576*4882a593Smuzhiyun 			return i;
1577*4882a593Smuzhiyun 	}
1578*4882a593Smuzhiyun 
1579*4882a593Smuzhiyun 	if (!ovl_lower_uuid_ok(ofs, &sb->s_uuid)) {
1580*4882a593Smuzhiyun 		bad_uuid = true;
1581*4882a593Smuzhiyun 		if (ofs->config.index || ofs->config.nfs_export) {
1582*4882a593Smuzhiyun 			ofs->config.index = false;
1583*4882a593Smuzhiyun 			ofs->config.nfs_export = false;
1584*4882a593Smuzhiyun 			pr_warn("%s uuid detected in lower fs '%pd2', falling back to index=off,nfs_export=off.\n",
1585*4882a593Smuzhiyun 				uuid_is_null(&sb->s_uuid) ? "null" :
1586*4882a593Smuzhiyun 							    "conflicting",
1587*4882a593Smuzhiyun 				path->dentry);
1588*4882a593Smuzhiyun 		}
1589*4882a593Smuzhiyun 	}
1590*4882a593Smuzhiyun 
1591*4882a593Smuzhiyun 	err = get_anon_bdev(&dev);
1592*4882a593Smuzhiyun 	if (err) {
1593*4882a593Smuzhiyun 		pr_err("failed to get anonymous bdev for lowerpath\n");
1594*4882a593Smuzhiyun 		return err;
1595*4882a593Smuzhiyun 	}
1596*4882a593Smuzhiyun 
1597*4882a593Smuzhiyun 	ofs->fs[ofs->numfs].sb = sb;
1598*4882a593Smuzhiyun 	ofs->fs[ofs->numfs].pseudo_dev = dev;
1599*4882a593Smuzhiyun 	ofs->fs[ofs->numfs].bad_uuid = bad_uuid;
1600*4882a593Smuzhiyun 
1601*4882a593Smuzhiyun 	return ofs->numfs++;
1602*4882a593Smuzhiyun }
1603*4882a593Smuzhiyun 
ovl_get_layers(struct super_block * sb,struct ovl_fs * ofs,struct path * stack,unsigned int numlower,struct ovl_layer * layers)1604*4882a593Smuzhiyun static int ovl_get_layers(struct super_block *sb, struct ovl_fs *ofs,
1605*4882a593Smuzhiyun 			  struct path *stack, unsigned int numlower,
1606*4882a593Smuzhiyun 			  struct ovl_layer *layers)
1607*4882a593Smuzhiyun {
1608*4882a593Smuzhiyun 	int err;
1609*4882a593Smuzhiyun 	unsigned int i;
1610*4882a593Smuzhiyun 
1611*4882a593Smuzhiyun 	err = -ENOMEM;
1612*4882a593Smuzhiyun 	ofs->fs = kcalloc(numlower + 1, sizeof(struct ovl_sb), GFP_KERNEL);
1613*4882a593Smuzhiyun 	if (ofs->fs == NULL)
1614*4882a593Smuzhiyun 		goto out;
1615*4882a593Smuzhiyun 
1616*4882a593Smuzhiyun 	/* idx/fsid 0 are reserved for upper fs even with lower only overlay */
1617*4882a593Smuzhiyun 	ofs->numfs++;
1618*4882a593Smuzhiyun 
1619*4882a593Smuzhiyun 	/*
1620*4882a593Smuzhiyun 	 * All lower layers that share the same fs as upper layer, use the same
1621*4882a593Smuzhiyun 	 * pseudo_dev as upper layer.  Allocate fs[0].pseudo_dev even for lower
1622*4882a593Smuzhiyun 	 * only overlay to simplify ovl_fs_free().
1623*4882a593Smuzhiyun 	 * is_lower will be set if upper fs is shared with a lower layer.
1624*4882a593Smuzhiyun 	 */
1625*4882a593Smuzhiyun 	err = get_anon_bdev(&ofs->fs[0].pseudo_dev);
1626*4882a593Smuzhiyun 	if (err) {
1627*4882a593Smuzhiyun 		pr_err("failed to get anonymous bdev for upper fs\n");
1628*4882a593Smuzhiyun 		goto out;
1629*4882a593Smuzhiyun 	}
1630*4882a593Smuzhiyun 
1631*4882a593Smuzhiyun 	if (ovl_upper_mnt(ofs)) {
1632*4882a593Smuzhiyun 		ofs->fs[0].sb = ovl_upper_mnt(ofs)->mnt_sb;
1633*4882a593Smuzhiyun 		ofs->fs[0].is_lower = false;
1634*4882a593Smuzhiyun 	}
1635*4882a593Smuzhiyun 
1636*4882a593Smuzhiyun 	for (i = 0; i < numlower; i++) {
1637*4882a593Smuzhiyun 		struct vfsmount *mnt;
1638*4882a593Smuzhiyun 		struct inode *trap;
1639*4882a593Smuzhiyun 		int fsid;
1640*4882a593Smuzhiyun 
1641*4882a593Smuzhiyun 		err = fsid = ovl_get_fsid(ofs, &stack[i]);
1642*4882a593Smuzhiyun 		if (err < 0)
1643*4882a593Smuzhiyun 			goto out;
1644*4882a593Smuzhiyun 
1645*4882a593Smuzhiyun 		/*
1646*4882a593Smuzhiyun 		 * Check if lower root conflicts with this overlay layers before
1647*4882a593Smuzhiyun 		 * checking if it is in-use as upperdir/workdir of "another"
1648*4882a593Smuzhiyun 		 * mount, because we do not bother to check in ovl_is_inuse() if
1649*4882a593Smuzhiyun 		 * the upperdir/workdir is in fact in-use by our
1650*4882a593Smuzhiyun 		 * upperdir/workdir.
1651*4882a593Smuzhiyun 		 */
1652*4882a593Smuzhiyun 		err = ovl_setup_trap(sb, stack[i].dentry, &trap, "lowerdir");
1653*4882a593Smuzhiyun 		if (err)
1654*4882a593Smuzhiyun 			goto out;
1655*4882a593Smuzhiyun 
1656*4882a593Smuzhiyun 		if (ovl_is_inuse(stack[i].dentry)) {
1657*4882a593Smuzhiyun 			err = ovl_report_in_use(ofs, "lowerdir");
1658*4882a593Smuzhiyun 			if (err) {
1659*4882a593Smuzhiyun 				iput(trap);
1660*4882a593Smuzhiyun 				goto out;
1661*4882a593Smuzhiyun 			}
1662*4882a593Smuzhiyun 		}
1663*4882a593Smuzhiyun 
1664*4882a593Smuzhiyun 		mnt = clone_private_mount(&stack[i]);
1665*4882a593Smuzhiyun 		err = PTR_ERR(mnt);
1666*4882a593Smuzhiyun 		if (IS_ERR(mnt)) {
1667*4882a593Smuzhiyun 			pr_err("failed to clone lowerpath\n");
1668*4882a593Smuzhiyun 			iput(trap);
1669*4882a593Smuzhiyun 			goto out;
1670*4882a593Smuzhiyun 		}
1671*4882a593Smuzhiyun 
1672*4882a593Smuzhiyun 		/*
1673*4882a593Smuzhiyun 		 * Make lower layers R/O.  That way fchmod/fchown on lower file
1674*4882a593Smuzhiyun 		 * will fail instead of modifying lower fs.
1675*4882a593Smuzhiyun 		 */
1676*4882a593Smuzhiyun 		mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1677*4882a593Smuzhiyun 
1678*4882a593Smuzhiyun 		layers[ofs->numlayer].trap = trap;
1679*4882a593Smuzhiyun 		layers[ofs->numlayer].mnt = mnt;
1680*4882a593Smuzhiyun 		layers[ofs->numlayer].idx = ofs->numlayer;
1681*4882a593Smuzhiyun 		layers[ofs->numlayer].fsid = fsid;
1682*4882a593Smuzhiyun 		layers[ofs->numlayer].fs = &ofs->fs[fsid];
1683*4882a593Smuzhiyun 		ofs->numlayer++;
1684*4882a593Smuzhiyun 		ofs->fs[fsid].is_lower = true;
1685*4882a593Smuzhiyun 	}
1686*4882a593Smuzhiyun 
1687*4882a593Smuzhiyun 	/*
1688*4882a593Smuzhiyun 	 * When all layers on same fs, overlay can use real inode numbers.
1689*4882a593Smuzhiyun 	 * With mount option "xino=<on|auto>", mounter declares that there are
1690*4882a593Smuzhiyun 	 * enough free high bits in underlying fs to hold the unique fsid.
1691*4882a593Smuzhiyun 	 * If overlayfs does encounter underlying inodes using the high xino
1692*4882a593Smuzhiyun 	 * bits reserved for fsid, it emits a warning and uses the original
1693*4882a593Smuzhiyun 	 * inode number or a non persistent inode number allocated from a
1694*4882a593Smuzhiyun 	 * dedicated range.
1695*4882a593Smuzhiyun 	 */
1696*4882a593Smuzhiyun 	if (ofs->numfs - !ovl_upper_mnt(ofs) == 1) {
1697*4882a593Smuzhiyun 		if (ofs->config.xino == OVL_XINO_ON)
1698*4882a593Smuzhiyun 			pr_info("\"xino=on\" is useless with all layers on same fs, ignore.\n");
1699*4882a593Smuzhiyun 		ofs->xino_mode = 0;
1700*4882a593Smuzhiyun 	} else if (ofs->config.xino == OVL_XINO_OFF) {
1701*4882a593Smuzhiyun 		ofs->xino_mode = -1;
1702*4882a593Smuzhiyun 	} else if (ofs->xino_mode < 0) {
1703*4882a593Smuzhiyun 		/*
1704*4882a593Smuzhiyun 		 * This is a roundup of number of bits needed for encoding
1705*4882a593Smuzhiyun 		 * fsid, where fsid 0 is reserved for upper fs (even with
1706*4882a593Smuzhiyun 		 * lower only overlay) +1 extra bit is reserved for the non
1707*4882a593Smuzhiyun 		 * persistent inode number range that is used for resolving
1708*4882a593Smuzhiyun 		 * xino lower bits overflow.
1709*4882a593Smuzhiyun 		 */
1710*4882a593Smuzhiyun 		BUILD_BUG_ON(ilog2(OVL_MAX_STACK) > 30);
1711*4882a593Smuzhiyun 		ofs->xino_mode = ilog2(ofs->numfs - 1) + 2;
1712*4882a593Smuzhiyun 	}
1713*4882a593Smuzhiyun 
1714*4882a593Smuzhiyun 	if (ofs->xino_mode > 0) {
1715*4882a593Smuzhiyun 		pr_info("\"xino\" feature enabled using %d upper inode bits.\n",
1716*4882a593Smuzhiyun 			ofs->xino_mode);
1717*4882a593Smuzhiyun 	}
1718*4882a593Smuzhiyun 
1719*4882a593Smuzhiyun 	err = 0;
1720*4882a593Smuzhiyun out:
1721*4882a593Smuzhiyun 	return err;
1722*4882a593Smuzhiyun }
1723*4882a593Smuzhiyun 
ovl_get_lowerstack(struct super_block * sb,const char * lower,unsigned int numlower,struct ovl_fs * ofs,struct ovl_layer * layers)1724*4882a593Smuzhiyun static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
1725*4882a593Smuzhiyun 				const char *lower, unsigned int numlower,
1726*4882a593Smuzhiyun 				struct ovl_fs *ofs, struct ovl_layer *layers)
1727*4882a593Smuzhiyun {
1728*4882a593Smuzhiyun 	int err;
1729*4882a593Smuzhiyun 	struct path *stack = NULL;
1730*4882a593Smuzhiyun 	unsigned int i;
1731*4882a593Smuzhiyun 	struct ovl_entry *oe;
1732*4882a593Smuzhiyun 
1733*4882a593Smuzhiyun 	if (!ofs->config.upperdir && numlower == 1) {
1734*4882a593Smuzhiyun 		pr_err("at least 2 lowerdir are needed while upperdir nonexistent\n");
1735*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
1736*4882a593Smuzhiyun 	}
1737*4882a593Smuzhiyun 
1738*4882a593Smuzhiyun 	stack = kcalloc(numlower, sizeof(struct path), GFP_KERNEL);
1739*4882a593Smuzhiyun 	if (!stack)
1740*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
1741*4882a593Smuzhiyun 
1742*4882a593Smuzhiyun 	err = -EINVAL;
1743*4882a593Smuzhiyun 	for (i = 0; i < numlower; i++) {
1744*4882a593Smuzhiyun 		err = ovl_lower_dir(lower, &stack[i], ofs, &sb->s_stack_depth);
1745*4882a593Smuzhiyun 		if (err)
1746*4882a593Smuzhiyun 			goto out_err;
1747*4882a593Smuzhiyun 
1748*4882a593Smuzhiyun 		lower = strchr(lower, '\0') + 1;
1749*4882a593Smuzhiyun 	}
1750*4882a593Smuzhiyun 
1751*4882a593Smuzhiyun 	err = -EINVAL;
1752*4882a593Smuzhiyun 	sb->s_stack_depth++;
1753*4882a593Smuzhiyun 	if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1754*4882a593Smuzhiyun 		pr_err("maximum fs stacking depth exceeded\n");
1755*4882a593Smuzhiyun 		goto out_err;
1756*4882a593Smuzhiyun 	}
1757*4882a593Smuzhiyun 
1758*4882a593Smuzhiyun 	err = ovl_get_layers(sb, ofs, stack, numlower, layers);
1759*4882a593Smuzhiyun 	if (err)
1760*4882a593Smuzhiyun 		goto out_err;
1761*4882a593Smuzhiyun 
1762*4882a593Smuzhiyun 	err = -ENOMEM;
1763*4882a593Smuzhiyun 	oe = ovl_alloc_entry(numlower);
1764*4882a593Smuzhiyun 	if (!oe)
1765*4882a593Smuzhiyun 		goto out_err;
1766*4882a593Smuzhiyun 
1767*4882a593Smuzhiyun 	for (i = 0; i < numlower; i++) {
1768*4882a593Smuzhiyun 		oe->lowerstack[i].dentry = dget(stack[i].dentry);
1769*4882a593Smuzhiyun 		oe->lowerstack[i].layer = &ofs->layers[i+1];
1770*4882a593Smuzhiyun 	}
1771*4882a593Smuzhiyun 
1772*4882a593Smuzhiyun out:
1773*4882a593Smuzhiyun 	for (i = 0; i < numlower; i++)
1774*4882a593Smuzhiyun 		path_put(&stack[i]);
1775*4882a593Smuzhiyun 	kfree(stack);
1776*4882a593Smuzhiyun 
1777*4882a593Smuzhiyun 	return oe;
1778*4882a593Smuzhiyun 
1779*4882a593Smuzhiyun out_err:
1780*4882a593Smuzhiyun 	oe = ERR_PTR(err);
1781*4882a593Smuzhiyun 	goto out;
1782*4882a593Smuzhiyun }
1783*4882a593Smuzhiyun 
1784*4882a593Smuzhiyun /*
1785*4882a593Smuzhiyun  * Check if this layer root is a descendant of:
1786*4882a593Smuzhiyun  * - another layer of this overlayfs instance
1787*4882a593Smuzhiyun  * - upper/work dir of any overlayfs instance
1788*4882a593Smuzhiyun  */
ovl_check_layer(struct super_block * sb,struct ovl_fs * ofs,struct dentry * dentry,const char * name,bool is_lower)1789*4882a593Smuzhiyun static int ovl_check_layer(struct super_block *sb, struct ovl_fs *ofs,
1790*4882a593Smuzhiyun 			   struct dentry *dentry, const char *name,
1791*4882a593Smuzhiyun 			   bool is_lower)
1792*4882a593Smuzhiyun {
1793*4882a593Smuzhiyun 	struct dentry *next = dentry, *parent;
1794*4882a593Smuzhiyun 	int err = 0;
1795*4882a593Smuzhiyun 
1796*4882a593Smuzhiyun 	if (!dentry)
1797*4882a593Smuzhiyun 		return 0;
1798*4882a593Smuzhiyun 
1799*4882a593Smuzhiyun 	parent = dget_parent(next);
1800*4882a593Smuzhiyun 
1801*4882a593Smuzhiyun 	/* Walk back ancestors to root (inclusive) looking for traps */
1802*4882a593Smuzhiyun 	while (!err && parent != next) {
1803*4882a593Smuzhiyun 		if (is_lower && ovl_lookup_trap_inode(sb, parent)) {
1804*4882a593Smuzhiyun 			err = -ELOOP;
1805*4882a593Smuzhiyun 			pr_err("overlapping %s path\n", name);
1806*4882a593Smuzhiyun 		} else if (ovl_is_inuse(parent)) {
1807*4882a593Smuzhiyun 			err = ovl_report_in_use(ofs, name);
1808*4882a593Smuzhiyun 		}
1809*4882a593Smuzhiyun 		next = parent;
1810*4882a593Smuzhiyun 		parent = dget_parent(next);
1811*4882a593Smuzhiyun 		dput(next);
1812*4882a593Smuzhiyun 	}
1813*4882a593Smuzhiyun 
1814*4882a593Smuzhiyun 	dput(parent);
1815*4882a593Smuzhiyun 
1816*4882a593Smuzhiyun 	return err;
1817*4882a593Smuzhiyun }
1818*4882a593Smuzhiyun 
1819*4882a593Smuzhiyun /*
1820*4882a593Smuzhiyun  * Check if any of the layers or work dirs overlap.
1821*4882a593Smuzhiyun  */
ovl_check_overlapping_layers(struct super_block * sb,struct ovl_fs * ofs)1822*4882a593Smuzhiyun static int ovl_check_overlapping_layers(struct super_block *sb,
1823*4882a593Smuzhiyun 					struct ovl_fs *ofs)
1824*4882a593Smuzhiyun {
1825*4882a593Smuzhiyun 	int i, err;
1826*4882a593Smuzhiyun 
1827*4882a593Smuzhiyun 	if (ovl_upper_mnt(ofs)) {
1828*4882a593Smuzhiyun 		err = ovl_check_layer(sb, ofs, ovl_upper_mnt(ofs)->mnt_root,
1829*4882a593Smuzhiyun 				      "upperdir", false);
1830*4882a593Smuzhiyun 		if (err)
1831*4882a593Smuzhiyun 			return err;
1832*4882a593Smuzhiyun 
1833*4882a593Smuzhiyun 		/*
1834*4882a593Smuzhiyun 		 * Checking workbasedir avoids hitting ovl_is_inuse(parent) of
1835*4882a593Smuzhiyun 		 * this instance and covers overlapping work and index dirs,
1836*4882a593Smuzhiyun 		 * unless work or index dir have been moved since created inside
1837*4882a593Smuzhiyun 		 * workbasedir.  In that case, we already have their traps in
1838*4882a593Smuzhiyun 		 * inode cache and we will catch that case on lookup.
1839*4882a593Smuzhiyun 		 */
1840*4882a593Smuzhiyun 		err = ovl_check_layer(sb, ofs, ofs->workbasedir, "workdir",
1841*4882a593Smuzhiyun 				      false);
1842*4882a593Smuzhiyun 		if (err)
1843*4882a593Smuzhiyun 			return err;
1844*4882a593Smuzhiyun 	}
1845*4882a593Smuzhiyun 
1846*4882a593Smuzhiyun 	for (i = 1; i < ofs->numlayer; i++) {
1847*4882a593Smuzhiyun 		err = ovl_check_layer(sb, ofs,
1848*4882a593Smuzhiyun 				      ofs->layers[i].mnt->mnt_root,
1849*4882a593Smuzhiyun 				      "lowerdir", true);
1850*4882a593Smuzhiyun 		if (err)
1851*4882a593Smuzhiyun 			return err;
1852*4882a593Smuzhiyun 	}
1853*4882a593Smuzhiyun 
1854*4882a593Smuzhiyun 	return 0;
1855*4882a593Smuzhiyun }
1856*4882a593Smuzhiyun 
ovl_get_root(struct super_block * sb,struct dentry * upperdentry,struct ovl_entry * oe)1857*4882a593Smuzhiyun static struct dentry *ovl_get_root(struct super_block *sb,
1858*4882a593Smuzhiyun 				   struct dentry *upperdentry,
1859*4882a593Smuzhiyun 				   struct ovl_entry *oe)
1860*4882a593Smuzhiyun {
1861*4882a593Smuzhiyun 	struct dentry *root;
1862*4882a593Smuzhiyun 	struct ovl_path *lowerpath = &oe->lowerstack[0];
1863*4882a593Smuzhiyun 	unsigned long ino = d_inode(lowerpath->dentry)->i_ino;
1864*4882a593Smuzhiyun 	int fsid = lowerpath->layer->fsid;
1865*4882a593Smuzhiyun 	struct ovl_inode_params oip = {
1866*4882a593Smuzhiyun 		.upperdentry = upperdentry,
1867*4882a593Smuzhiyun 		.lowerpath = lowerpath,
1868*4882a593Smuzhiyun 	};
1869*4882a593Smuzhiyun 
1870*4882a593Smuzhiyun 	root = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
1871*4882a593Smuzhiyun 	if (!root)
1872*4882a593Smuzhiyun 		return NULL;
1873*4882a593Smuzhiyun 
1874*4882a593Smuzhiyun 	root->d_fsdata = oe;
1875*4882a593Smuzhiyun 
1876*4882a593Smuzhiyun 	if (upperdentry) {
1877*4882a593Smuzhiyun 		/* Root inode uses upper st_ino/i_ino */
1878*4882a593Smuzhiyun 		ino = d_inode(upperdentry)->i_ino;
1879*4882a593Smuzhiyun 		fsid = 0;
1880*4882a593Smuzhiyun 		ovl_dentry_set_upper_alias(root);
1881*4882a593Smuzhiyun 		if (ovl_is_impuredir(sb, upperdentry))
1882*4882a593Smuzhiyun 			ovl_set_flag(OVL_IMPURE, d_inode(root));
1883*4882a593Smuzhiyun 	}
1884*4882a593Smuzhiyun 
1885*4882a593Smuzhiyun 	/* Root is always merge -> can have whiteouts */
1886*4882a593Smuzhiyun 	ovl_set_flag(OVL_WHITEOUTS, d_inode(root));
1887*4882a593Smuzhiyun 	ovl_dentry_set_flag(OVL_E_CONNECTED, root);
1888*4882a593Smuzhiyun 	ovl_set_upperdata(d_inode(root));
1889*4882a593Smuzhiyun 	ovl_inode_init(d_inode(root), &oip, ino, fsid);
1890*4882a593Smuzhiyun 	ovl_dentry_update_reval(root, upperdentry, DCACHE_OP_WEAK_REVALIDATE);
1891*4882a593Smuzhiyun 
1892*4882a593Smuzhiyun 	return root;
1893*4882a593Smuzhiyun }
1894*4882a593Smuzhiyun 
ovl_fill_super(struct super_block * sb,void * data,int silent)1895*4882a593Smuzhiyun static int ovl_fill_super(struct super_block *sb, void *data, int silent)
1896*4882a593Smuzhiyun {
1897*4882a593Smuzhiyun 	struct path upperpath = { };
1898*4882a593Smuzhiyun 	struct dentry *root_dentry;
1899*4882a593Smuzhiyun 	struct ovl_entry *oe;
1900*4882a593Smuzhiyun 	struct ovl_fs *ofs;
1901*4882a593Smuzhiyun 	struct ovl_layer *layers;
1902*4882a593Smuzhiyun 	struct cred *cred;
1903*4882a593Smuzhiyun 	char *splitlower = NULL;
1904*4882a593Smuzhiyun 	unsigned int numlower;
1905*4882a593Smuzhiyun 	int err;
1906*4882a593Smuzhiyun 
1907*4882a593Smuzhiyun 	sb->s_d_op = &ovl_dentry_operations;
1908*4882a593Smuzhiyun 
1909*4882a593Smuzhiyun 	err = -ENOMEM;
1910*4882a593Smuzhiyun 	ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
1911*4882a593Smuzhiyun 	if (!ofs)
1912*4882a593Smuzhiyun 		goto out;
1913*4882a593Smuzhiyun 
1914*4882a593Smuzhiyun 	ofs->creator_cred = cred = prepare_creds();
1915*4882a593Smuzhiyun 	if (!cred)
1916*4882a593Smuzhiyun 		goto out_err;
1917*4882a593Smuzhiyun 
1918*4882a593Smuzhiyun 	/* Is there a reason anyone would want not to share whiteouts? */
1919*4882a593Smuzhiyun 	ofs->share_whiteout = true;
1920*4882a593Smuzhiyun 
1921*4882a593Smuzhiyun 	ofs->config.index = ovl_index_def;
1922*4882a593Smuzhiyun 	ofs->config.nfs_export = ovl_nfs_export_def;
1923*4882a593Smuzhiyun 	ofs->config.xino = ovl_xino_def();
1924*4882a593Smuzhiyun 	ofs->config.metacopy = ovl_metacopy_def;
1925*4882a593Smuzhiyun 	err = ovl_parse_opt((char *) data, &ofs->config);
1926*4882a593Smuzhiyun 	if (err)
1927*4882a593Smuzhiyun 		goto out_err;
1928*4882a593Smuzhiyun 
1929*4882a593Smuzhiyun 	err = -EINVAL;
1930*4882a593Smuzhiyun 	if (!ofs->config.lowerdir) {
1931*4882a593Smuzhiyun 		if (!silent)
1932*4882a593Smuzhiyun 			pr_err("missing 'lowerdir'\n");
1933*4882a593Smuzhiyun 		goto out_err;
1934*4882a593Smuzhiyun 	}
1935*4882a593Smuzhiyun 
1936*4882a593Smuzhiyun 	err = -ENOMEM;
1937*4882a593Smuzhiyun 	splitlower = kstrdup(ofs->config.lowerdir, GFP_KERNEL);
1938*4882a593Smuzhiyun 	if (!splitlower)
1939*4882a593Smuzhiyun 		goto out_err;
1940*4882a593Smuzhiyun 
1941*4882a593Smuzhiyun 	numlower = ovl_split_lowerdirs(splitlower);
1942*4882a593Smuzhiyun 	if (numlower > OVL_MAX_STACK) {
1943*4882a593Smuzhiyun 		pr_err("too many lower directories, limit is %d\n",
1944*4882a593Smuzhiyun 		       OVL_MAX_STACK);
1945*4882a593Smuzhiyun 		goto out_err;
1946*4882a593Smuzhiyun 	}
1947*4882a593Smuzhiyun 
1948*4882a593Smuzhiyun 	layers = kcalloc(numlower + 1, sizeof(struct ovl_layer), GFP_KERNEL);
1949*4882a593Smuzhiyun 	if (!layers)
1950*4882a593Smuzhiyun 		goto out_err;
1951*4882a593Smuzhiyun 
1952*4882a593Smuzhiyun 	ofs->layers = layers;
1953*4882a593Smuzhiyun 	/* Layer 0 is reserved for upper even if there's no upper */
1954*4882a593Smuzhiyun 	ofs->numlayer = 1;
1955*4882a593Smuzhiyun 
1956*4882a593Smuzhiyun 	sb->s_stack_depth = 0;
1957*4882a593Smuzhiyun 	sb->s_maxbytes = MAX_LFS_FILESIZE;
1958*4882a593Smuzhiyun 	atomic_long_set(&ofs->last_ino, 1);
1959*4882a593Smuzhiyun 	/* Assume underlaying fs uses 32bit inodes unless proven otherwise */
1960*4882a593Smuzhiyun 	if (ofs->config.xino != OVL_XINO_OFF) {
1961*4882a593Smuzhiyun 		ofs->xino_mode = BITS_PER_LONG - 32;
1962*4882a593Smuzhiyun 		if (!ofs->xino_mode) {
1963*4882a593Smuzhiyun 			pr_warn("xino not supported on 32bit kernel, falling back to xino=off.\n");
1964*4882a593Smuzhiyun 			ofs->config.xino = OVL_XINO_OFF;
1965*4882a593Smuzhiyun 		}
1966*4882a593Smuzhiyun 	}
1967*4882a593Smuzhiyun 
1968*4882a593Smuzhiyun 	/* alloc/destroy_inode needed for setting up traps in inode cache */
1969*4882a593Smuzhiyun 	sb->s_op = &ovl_super_operations;
1970*4882a593Smuzhiyun 
1971*4882a593Smuzhiyun 	if (ofs->config.upperdir) {
1972*4882a593Smuzhiyun 		struct super_block *upper_sb;
1973*4882a593Smuzhiyun 
1974*4882a593Smuzhiyun 		if (!ofs->config.workdir) {
1975*4882a593Smuzhiyun 			pr_err("missing 'workdir'\n");
1976*4882a593Smuzhiyun 			goto out_err;
1977*4882a593Smuzhiyun 		}
1978*4882a593Smuzhiyun 
1979*4882a593Smuzhiyun 		err = ovl_get_upper(sb, ofs, &layers[0], &upperpath);
1980*4882a593Smuzhiyun 		if (err)
1981*4882a593Smuzhiyun 			goto out_err;
1982*4882a593Smuzhiyun 
1983*4882a593Smuzhiyun 		upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
1984*4882a593Smuzhiyun 		if (!ovl_should_sync(ofs)) {
1985*4882a593Smuzhiyun 			ofs->errseq = errseq_sample(&upper_sb->s_wb_err);
1986*4882a593Smuzhiyun 			if (errseq_check(&upper_sb->s_wb_err, ofs->errseq)) {
1987*4882a593Smuzhiyun 				err = -EIO;
1988*4882a593Smuzhiyun 				pr_err("Cannot mount volatile when upperdir has an unseen error. Sync upperdir fs to clear state.\n");
1989*4882a593Smuzhiyun 				goto out_err;
1990*4882a593Smuzhiyun 			}
1991*4882a593Smuzhiyun 		}
1992*4882a593Smuzhiyun 
1993*4882a593Smuzhiyun 		err = ovl_get_workdir(sb, ofs, &upperpath);
1994*4882a593Smuzhiyun 		if (err)
1995*4882a593Smuzhiyun 			goto out_err;
1996*4882a593Smuzhiyun 
1997*4882a593Smuzhiyun 		if (!ofs->workdir)
1998*4882a593Smuzhiyun 			sb->s_flags |= SB_RDONLY;
1999*4882a593Smuzhiyun 
2000*4882a593Smuzhiyun 		sb->s_stack_depth = upper_sb->s_stack_depth;
2001*4882a593Smuzhiyun 		sb->s_time_gran = upper_sb->s_time_gran;
2002*4882a593Smuzhiyun 	}
2003*4882a593Smuzhiyun 	oe = ovl_get_lowerstack(sb, splitlower, numlower, ofs, layers);
2004*4882a593Smuzhiyun 	err = PTR_ERR(oe);
2005*4882a593Smuzhiyun 	if (IS_ERR(oe))
2006*4882a593Smuzhiyun 		goto out_err;
2007*4882a593Smuzhiyun 
2008*4882a593Smuzhiyun 	/* If the upper fs is nonexistent, we mark overlayfs r/o too */
2009*4882a593Smuzhiyun 	if (!ovl_upper_mnt(ofs))
2010*4882a593Smuzhiyun 		sb->s_flags |= SB_RDONLY;
2011*4882a593Smuzhiyun 
2012*4882a593Smuzhiyun 	if (!ovl_force_readonly(ofs) && ofs->config.index) {
2013*4882a593Smuzhiyun 		err = ovl_get_indexdir(sb, ofs, oe, &upperpath);
2014*4882a593Smuzhiyun 		if (err)
2015*4882a593Smuzhiyun 			goto out_free_oe;
2016*4882a593Smuzhiyun 
2017*4882a593Smuzhiyun 		/* Force r/o mount with no index dir */
2018*4882a593Smuzhiyun 		if (!ofs->indexdir)
2019*4882a593Smuzhiyun 			sb->s_flags |= SB_RDONLY;
2020*4882a593Smuzhiyun 	}
2021*4882a593Smuzhiyun 
2022*4882a593Smuzhiyun 	err = ovl_check_overlapping_layers(sb, ofs);
2023*4882a593Smuzhiyun 	if (err)
2024*4882a593Smuzhiyun 		goto out_free_oe;
2025*4882a593Smuzhiyun 
2026*4882a593Smuzhiyun 	/* Show index=off in /proc/mounts for forced r/o mount */
2027*4882a593Smuzhiyun 	if (!ofs->indexdir) {
2028*4882a593Smuzhiyun 		ofs->config.index = false;
2029*4882a593Smuzhiyun 		if (ovl_upper_mnt(ofs) && ofs->config.nfs_export) {
2030*4882a593Smuzhiyun 			pr_warn("NFS export requires an index dir, falling back to nfs_export=off.\n");
2031*4882a593Smuzhiyun 			ofs->config.nfs_export = false;
2032*4882a593Smuzhiyun 		}
2033*4882a593Smuzhiyun 	}
2034*4882a593Smuzhiyun 
2035*4882a593Smuzhiyun 	if (ofs->config.metacopy && ofs->config.nfs_export) {
2036*4882a593Smuzhiyun 		pr_warn("NFS export is not supported with metadata only copy up, falling back to nfs_export=off.\n");
2037*4882a593Smuzhiyun 		ofs->config.nfs_export = false;
2038*4882a593Smuzhiyun 	}
2039*4882a593Smuzhiyun 
2040*4882a593Smuzhiyun 	if (ofs->config.nfs_export)
2041*4882a593Smuzhiyun 		sb->s_export_op = &ovl_export_operations;
2042*4882a593Smuzhiyun 
2043*4882a593Smuzhiyun 	/* Never override disk quota limits or use reserved space */
2044*4882a593Smuzhiyun 	cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
2045*4882a593Smuzhiyun 
2046*4882a593Smuzhiyun 	sb->s_magic = OVERLAYFS_SUPER_MAGIC;
2047*4882a593Smuzhiyun 	sb->s_xattr = ovl_xattr_handlers;
2048*4882a593Smuzhiyun 	sb->s_fs_info = ofs;
2049*4882a593Smuzhiyun 	sb->s_flags |= SB_POSIXACL;
2050*4882a593Smuzhiyun 	sb->s_iflags |= SB_I_SKIP_SYNC;
2051*4882a593Smuzhiyun 
2052*4882a593Smuzhiyun 	err = -ENOMEM;
2053*4882a593Smuzhiyun 	root_dentry = ovl_get_root(sb, upperpath.dentry, oe);
2054*4882a593Smuzhiyun 	if (!root_dentry)
2055*4882a593Smuzhiyun 		goto out_free_oe;
2056*4882a593Smuzhiyun 
2057*4882a593Smuzhiyun 	mntput(upperpath.mnt);
2058*4882a593Smuzhiyun 	kfree(splitlower);
2059*4882a593Smuzhiyun 
2060*4882a593Smuzhiyun 	sb->s_root = root_dentry;
2061*4882a593Smuzhiyun 	return 0;
2062*4882a593Smuzhiyun 
2063*4882a593Smuzhiyun out_free_oe:
2064*4882a593Smuzhiyun 	ovl_entry_stack_free(oe);
2065*4882a593Smuzhiyun 	kfree(oe);
2066*4882a593Smuzhiyun out_err:
2067*4882a593Smuzhiyun 	kfree(splitlower);
2068*4882a593Smuzhiyun 	path_put(&upperpath);
2069*4882a593Smuzhiyun 	ovl_free_fs(ofs);
2070*4882a593Smuzhiyun out:
2071*4882a593Smuzhiyun 	return err;
2072*4882a593Smuzhiyun }
2073*4882a593Smuzhiyun 
ovl_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * raw_data)2074*4882a593Smuzhiyun static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
2075*4882a593Smuzhiyun 				const char *dev_name, void *raw_data)
2076*4882a593Smuzhiyun {
2077*4882a593Smuzhiyun 	return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
2078*4882a593Smuzhiyun }
2079*4882a593Smuzhiyun 
2080*4882a593Smuzhiyun static struct file_system_type ovl_fs_type = {
2081*4882a593Smuzhiyun 	.owner		= THIS_MODULE,
2082*4882a593Smuzhiyun 	.name		= "overlay",
2083*4882a593Smuzhiyun 	.mount		= ovl_mount,
2084*4882a593Smuzhiyun 	.kill_sb	= kill_anon_super,
2085*4882a593Smuzhiyun };
2086*4882a593Smuzhiyun MODULE_ALIAS_FS("overlay");
2087*4882a593Smuzhiyun 
ovl_inode_init_once(void * foo)2088*4882a593Smuzhiyun static void ovl_inode_init_once(void *foo)
2089*4882a593Smuzhiyun {
2090*4882a593Smuzhiyun 	struct ovl_inode *oi = foo;
2091*4882a593Smuzhiyun 
2092*4882a593Smuzhiyun 	inode_init_once(&oi->vfs_inode);
2093*4882a593Smuzhiyun }
2094*4882a593Smuzhiyun 
ovl_init(void)2095*4882a593Smuzhiyun static int __init ovl_init(void)
2096*4882a593Smuzhiyun {
2097*4882a593Smuzhiyun 	int err;
2098*4882a593Smuzhiyun 
2099*4882a593Smuzhiyun 	ovl_inode_cachep = kmem_cache_create("ovl_inode",
2100*4882a593Smuzhiyun 					     sizeof(struct ovl_inode), 0,
2101*4882a593Smuzhiyun 					     (SLAB_RECLAIM_ACCOUNT|
2102*4882a593Smuzhiyun 					      SLAB_MEM_SPREAD|SLAB_ACCOUNT),
2103*4882a593Smuzhiyun 					     ovl_inode_init_once);
2104*4882a593Smuzhiyun 	if (ovl_inode_cachep == NULL)
2105*4882a593Smuzhiyun 		return -ENOMEM;
2106*4882a593Smuzhiyun 
2107*4882a593Smuzhiyun 	err = ovl_aio_request_cache_init();
2108*4882a593Smuzhiyun 	if (!err) {
2109*4882a593Smuzhiyun 		err = register_filesystem(&ovl_fs_type);
2110*4882a593Smuzhiyun 		if (!err)
2111*4882a593Smuzhiyun 			return 0;
2112*4882a593Smuzhiyun 
2113*4882a593Smuzhiyun 		ovl_aio_request_cache_destroy();
2114*4882a593Smuzhiyun 	}
2115*4882a593Smuzhiyun 	kmem_cache_destroy(ovl_inode_cachep);
2116*4882a593Smuzhiyun 
2117*4882a593Smuzhiyun 	return err;
2118*4882a593Smuzhiyun }
2119*4882a593Smuzhiyun 
ovl_exit(void)2120*4882a593Smuzhiyun static void __exit ovl_exit(void)
2121*4882a593Smuzhiyun {
2122*4882a593Smuzhiyun 	unregister_filesystem(&ovl_fs_type);
2123*4882a593Smuzhiyun 
2124*4882a593Smuzhiyun 	/*
2125*4882a593Smuzhiyun 	 * Make sure all delayed rcu free inodes are flushed before we
2126*4882a593Smuzhiyun 	 * destroy cache.
2127*4882a593Smuzhiyun 	 */
2128*4882a593Smuzhiyun 	rcu_barrier();
2129*4882a593Smuzhiyun 	kmem_cache_destroy(ovl_inode_cachep);
2130*4882a593Smuzhiyun 	ovl_aio_request_cache_destroy();
2131*4882a593Smuzhiyun }
2132*4882a593Smuzhiyun 
2133*4882a593Smuzhiyun module_init(ovl_init);
2134*4882a593Smuzhiyun module_exit(ovl_exit);
2135