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