xref: /illumos-gate/usr/src/uts/common/fs/mntfs/mntvfsops.c (revision b1d7ec75953cd517f5b7c3d9cb427ff8ec5d7d07)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/param.h>
28 #include <sys/cmn_err.h>
29 #include <sys/cred.h>
30 #include <sys/debug.h>
31 #include <sys/errno.h>
32 #include <sys/proc.h>
33 #include <sys/procfs.h>
34 #include <sys/stat.h>
35 #include <sys/statvfs.h>
36 #include <sys/sysmacros.h>
37 #include <sys/systm.h>
38 #include <sys/var.h>
39 #include <sys/vfs.h>
40 #include <sys/vfs_opreg.h>
41 #include <sys/vnode.h>
42 #include <sys/mode.h>
43 #include <sys/signal.h>
44 #include <sys/user.h>
45 #include <sys/mount.h>
46 #include <sys/bitmap.h>
47 #include <sys/kmem.h>
48 #include <sys/policy.h>
49 #include <fs/fs_subr.h>
50 #include <sys/fs/mntdata.h>
51 #include <sys/zone.h>
52 
53 /*
54  * This is the loadable module wrapper.
55  */
56 #include <sys/modctl.h>
57 
58 static int mntinit(int, char *);
59 
60 static mntopts_t mnt_mntopts = {
61 	0,
62 	NULL
63 };
64 
65 static vfsdef_t vfw = {
66 	VFSDEF_VERSION,
67 	"mntfs",
68 	mntinit,
69 	VSW_HASPROTO|VSW_STATS,
70 	&mnt_mntopts
71 };
72 
73 /*
74  * Module linkage information for the kernel.
75  */
76 extern struct mod_ops mod_fsops;
77 
78 static struct modlfs modlfs = {
79 	&mod_fsops, "mount information file system", &vfw
80 };
81 
82 static struct modlinkage modlinkage = {
83 	MODREV_1, (void *)&modlfs, NULL
84 };
85 
86 int
87 _init(void)
88 {
89 	return (mod_install(&modlinkage));
90 }
91 
92 int
93 _info(struct modinfo *modinfop)
94 {
95 	return (mod_info(&modlinkage, modinfop));
96 }
97 
98 /*
99  * N.B.
100  * No _fini routine. The module cannot be unloaded once loaded.
101  * The NO_UNLOAD_STUB in modstubs.s must change if this module
102  * is ever modified to become unloadable.
103  */
104 
105 extern int	mntfstype;
106 static major_t	mnt_major;
107 static minor_t	mnt_minor;
108 static kmutex_t	mnt_minor_lock;
109 
110 /*
111  * /mnttab VFS operations vector.
112  */
113 static int	mntmount(), mntunmount(), mntroot(), mntstatvfs();
114 
115 static void
116 mntinitrootnode(mntnode_t *mnp)
117 {
118 	struct vnode *vp;
119 
120 	bzero((caddr_t)mnp, sizeof (*mnp));
121 
122 	mnp->mnt_vnode = vn_alloc(KM_SLEEP);
123 
124 	vp = MTOV(mnp);
125 
126 	vp->v_flag = VROOT|VNOCACHE|VNOMAP|VNOSWAP|VNOMOUNT;
127 	vn_setops(vp, mntvnodeops);
128 	vp->v_type = VREG;
129 	vp->v_data = (caddr_t)mnp;
130 }
131 
132 static int
133 mntinit(int fstype, char *name)
134 {
135 	static const fs_operation_def_t mnt_vfsops_template[] = {
136 		VFSNAME_MOUNT,		{ .vfs_mount = mntmount },
137 		VFSNAME_UNMOUNT,	{ .vfs_unmount = mntunmount },
138 		VFSNAME_ROOT,		{ .vfs_root = mntroot },
139 		VFSNAME_STATVFS,	{ .vfs_statvfs = mntstatvfs },
140 		NULL,			NULL
141 	};
142 	extern const fs_operation_def_t mnt_vnodeops_template[];
143 	int error;
144 
145 	mntfstype = fstype;
146 	ASSERT(mntfstype != 0);
147 	/*
148 	 * Associate VFS ops vector with this fstype.
149 	 */
150 	error = vfs_setfsops(fstype, mnt_vfsops_template, NULL);
151 	if (error != 0) {
152 		cmn_err(CE_WARN, "mntinit: bad vfs ops template");
153 		return (error);
154 	}
155 
156 	/* Vnode ops too. */
157 
158 	error = vn_make_ops(name, mnt_vnodeops_template, &mntvnodeops);
159 	if (error != 0) {
160 		(void) vfs_freevfsops_by_type(fstype);
161 		cmn_err(CE_WARN, "mntinit: bad vnode ops template");
162 		return (error);
163 	}
164 
165 	/*
166 	 * Assign a unique "device" number (used by stat(2)).
167 	 */
168 	if ((mnt_major = getudev()) == (major_t)-1) {
169 		cmn_err(CE_WARN, "mntinit: can't get unique device number");
170 		mnt_major = 0;
171 	}
172 	mutex_init(&mnt_minor_lock, NULL, MUTEX_DEFAULT, NULL);
173 
174 	return (0);
175 }
176 
177 /* ARGSUSED */
178 static int
179 mntmount(struct vfs *vfsp, struct vnode *mvp,
180 	struct mounta *uap, struct cred *cr)
181 {
182 	mntdata_t *mnt;
183 	mntnode_t *mnp;
184 	zone_t *zone = curproc->p_zone;
185 
186 	if (secpolicy_fs_mount(cr, mvp, vfsp) != 0)
187 		return (EPERM);
188 
189 	/*
190 	 * You can only mount mnttab in your current zone.
191 	 */
192 	if (zone == global_zone) {
193 		zone_t *mntzone;
194 
195 		mntzone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
196 		ASSERT(mntzone != NULL);
197 		zone_rele(mntzone);
198 		if (mntzone != zone)
199 			return (EBUSY);
200 	}
201 
202 	/*
203 	 * Having the resource be anything but "mnttab" doesn't make sense
204 	 */
205 	vfs_setresource(vfsp, "mnttab");
206 
207 	mnt = kmem_zalloc(sizeof (*mnt), KM_SLEEP);
208 	mutex_enter(&mvp->v_lock);
209 	if ((uap->flags & MS_OVERLAY) == 0 &&
210 	    (mvp->v_count > 1 || (mvp->v_flag & VROOT))) {
211 		mutex_exit(&mvp->v_lock);
212 		kmem_free(mnt, sizeof (*mnt));
213 		return (EBUSY);
214 	}
215 	mutex_exit(&mvp->v_lock);
216 
217 	zone_hold(mnt->mnt_zone = zone);
218 	mnp = &mnt->mnt_node;
219 
220 	vfsp->vfs_fstype = mntfstype;
221 	vfsp->vfs_data = (caddr_t)mnt;
222 	/*
223 	 * find an available minor device number for this mount.
224 	 */
225 	mutex_enter(&mnt_minor_lock);
226 	do {
227 		mnt_minor = (mnt_minor + 1) & L_MAXMIN32;
228 		vfsp->vfs_dev = makedevice(mnt_major, mnt_minor);
229 	} while (vfs_devismounted(vfsp->vfs_dev));
230 	mutex_exit(&mnt_minor_lock);
231 	vfs_make_fsid(&vfsp->vfs_fsid, vfsp->vfs_dev, mntfstype);
232 	vfsp->vfs_bsize = DEV_BSIZE;
233 	mntinitrootnode(mnp);
234 	MTOV(mnp)->v_vfsp = vfsp;
235 	mnp->mnt_mountvp = mvp;
236 	vn_exists(MTOV(mnp));
237 	return (0);
238 }
239 
240 /* ARGSUSED */
241 static int
242 mntunmount(struct vfs *vfsp, int flag, struct cred *cr)
243 {
244 	mntdata_t *mnt = (mntdata_t *)vfsp->vfs_data;
245 	vnode_t *vp = MTOV(&mnt->mnt_node);
246 
247 	if (secpolicy_fs_unmount(cr, vfsp) != 0)
248 		return (EPERM);
249 
250 	/*
251 	 * Ensure that no /mnttab vnodes are in use on this mount point.
252 	 */
253 	mutex_enter(&vp->v_lock);
254 	if (vp->v_count > 1 || mnt->mnt_nopen > 0) {
255 		mutex_exit(&vp->v_lock);
256 		return (EBUSY);
257 	}
258 
259 	mutex_exit(&vp->v_lock);
260 	zone_rele(mnt->mnt_zone);
261 	vn_invalid(vp);
262 	vn_free(vp);
263 	kmem_free(mnt, sizeof (*mnt));
264 	return (0);
265 }
266 
267 /* ARGSUSED */
268 static int
269 mntroot(struct vfs *vfsp, struct vnode **vpp)
270 {
271 	mntnode_t *mnp = &((mntdata_t *)vfsp->vfs_data)->mnt_node;
272 	struct vnode *vp = MTOV(mnp);
273 
274 	VN_HOLD(vp);
275 	*vpp = vp;
276 	return (0);
277 }
278 
279 static int
280 mntstatvfs(struct vfs *vfsp, struct statvfs64 *sp)
281 {
282 	dev32_t d32;
283 
284 	bzero((caddr_t)sp, sizeof (*sp));
285 	sp->f_bsize	= DEV_BSIZE;
286 	sp->f_frsize	= DEV_BSIZE;
287 	sp->f_blocks	= (fsblkcnt64_t)0;
288 	sp->f_bfree	= (fsblkcnt64_t)0;
289 	sp->f_bavail	= (fsblkcnt64_t)0;
290 	sp->f_files	= (fsfilcnt64_t)1;
291 	sp->f_ffree	= (fsfilcnt64_t)0;
292 	sp->f_favail	= (fsfilcnt64_t)0;
293 	(void) cmpldev(&d32, vfsp->vfs_dev);
294 	sp->f_fsid	= d32;
295 	(void) strcpy(sp->f_basetype, vfssw[mntfstype].vsw_name);
296 	sp->f_flag = vf_to_stf(vfsp->vfs_flag);
297 	sp->f_namemax = 64;		/* quite arbitrary */
298 	bzero(sp->f_fstr, sizeof (sp->f_fstr));
299 	(void) strcpy(sp->f_fstr, "/mnttab");
300 	(void) strcpy(&sp->f_fstr[8], "/mnttab");
301 	return (0);
302 }
303