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