17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5aa59c4cbSrsb  * Common Development and Distribution License (the "License").
6aa59c4cbSrsb  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
220fbb751dSJohn Levon  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
267c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
277c478bd9Sstevel@tonic-gate #include <sys/errno.h>
287c478bd9Sstevel@tonic-gate #include <sys/mount.h>
297c478bd9Sstevel@tonic-gate #include <sys/objfs.h>
307c478bd9Sstevel@tonic-gate #include <sys/objfs_impl.h>
31aa59c4cbSrsb #include <sys/vfs_opreg.h>
327c478bd9Sstevel@tonic-gate #include <sys/policy.h>
337c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
347c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
357c478bd9Sstevel@tonic-gate #include <sys/systm.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate  * Kernel object filesystem.
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * This is a pseudo filesystem which exports information about currently loaded
417c478bd9Sstevel@tonic-gate  * kernel objects.  The root directory contains one directory for each loaded
427c478bd9Sstevel@tonic-gate  * object, indexed by module name.  Within each object directory is an ELF file,
437c478bd9Sstevel@tonic-gate  * 'object', that contains information about the currently loaded module.
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  * This file contains functions that interact with the VFS layer.  Each
467c478bd9Sstevel@tonic-gate  * filesystem element is represented by a a different node.
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * 	/		objfs_rootnode_t	objfs_root.c
497c478bd9Sstevel@tonic-gate  *	/<obj>		objfs_odirnode_t	objfs_odir.c
507c478bd9Sstevel@tonic-gate  *	/<obj>/object	objfs_datanode_t	objfs_data.c
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  * In addition, some common routines are found in the 'objfs_common.c' file.
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate vnodeops_t *objfs_ops_root;
567c478bd9Sstevel@tonic-gate vnodeops_t *objfs_ops_odir;
577c478bd9Sstevel@tonic-gate vnodeops_t *objfs_ops_data;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static const fs_operation_def_t objfs_vfstops[];
607c478bd9Sstevel@tonic-gate static gfs_opsvec_t objfs_opsvec[];
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate static int objfs_init(int, char *);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate  * Module linkage
667c478bd9Sstevel@tonic-gate  */
677c478bd9Sstevel@tonic-gate static mntopts_t objfs_mntopts = {
687c478bd9Sstevel@tonic-gate 	0,
697c478bd9Sstevel@tonic-gate 	NULL
707c478bd9Sstevel@tonic-gate };
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate static vfsdef_t vfw = {
737c478bd9Sstevel@tonic-gate 	VFSDEF_VERSION,
747c478bd9Sstevel@tonic-gate 	"objfs",
757c478bd9Sstevel@tonic-gate 	objfs_init,
760fbb751dSJohn Levon 	VSW_HASPROTO | VSW_ZMOUNT,
777c478bd9Sstevel@tonic-gate 	&objfs_mntopts,
787c478bd9Sstevel@tonic-gate };
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate extern struct mod_ops mod_fsops;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate static struct modlfs modlfs = {
837c478bd9Sstevel@tonic-gate 	&mod_fsops, "kernel object filesystem", &vfw
847c478bd9Sstevel@tonic-gate };
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
877c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modlfs, NULL
887c478bd9Sstevel@tonic-gate };
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate int
_init(void)917c478bd9Sstevel@tonic-gate _init(void)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)977c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate int
_fini(void)1037c478bd9Sstevel@tonic-gate _fini(void)
1047c478bd9Sstevel@tonic-gate {
1057c478bd9Sstevel@tonic-gate 	/*
1067c478bd9Sstevel@tonic-gate 	 * The object filesystem cannot be unloaded.
1077c478bd9Sstevel@tonic-gate 	 */
1087c478bd9Sstevel@tonic-gate 	return (EBUSY);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * Filesystem initialization.
1137c478bd9Sstevel@tonic-gate  */
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate static int objfs_fstype;
1167c478bd9Sstevel@tonic-gate static major_t objfs_major;
1177c478bd9Sstevel@tonic-gate static minor_t objfs_minor;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate static gfs_opsvec_t objfs_opsvec[] = {
1207c478bd9Sstevel@tonic-gate 	{ "objfs root directory", objfs_tops_root, &objfs_ops_root },
1217c478bd9Sstevel@tonic-gate 	{ "objfs object directory", objfs_tops_odir, &objfs_ops_odir },
1227c478bd9Sstevel@tonic-gate 	{ "objfs data file", objfs_tops_data, &objfs_ops_data },
1237c478bd9Sstevel@tonic-gate 	{ NULL }
1247c478bd9Sstevel@tonic-gate };
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /* ARGSUSED */
1277c478bd9Sstevel@tonic-gate static int
objfs_init(int fstype,char * name)1287c478bd9Sstevel@tonic-gate objfs_init(int fstype, char *name)
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate 	vfsops_t *vfsops;
1317c478bd9Sstevel@tonic-gate 	int error;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	objfs_fstype = fstype;
1347c478bd9Sstevel@tonic-gate 	if (error = vfs_setfsops(fstype, objfs_vfstops, &vfsops)) {
1357c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "objfs_init: bad vfs ops template");
1367c478bd9Sstevel@tonic-gate 		return (error);
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	if (error = gfs_make_opsvec(objfs_opsvec)) {
1407c478bd9Sstevel@tonic-gate 		(void) vfs_freevfsops(vfsops);
1417c478bd9Sstevel@tonic-gate 		return (error);
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	if ((objfs_major = getudev()) == (major_t)-1) {
1457c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "objfs_init: can't get unique device number");
1467c478bd9Sstevel@tonic-gate 		objfs_major = 0;
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	objfs_data_init();
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	return (0);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate /*
1557c478bd9Sstevel@tonic-gate  * VFS entry points
1567c478bd9Sstevel@tonic-gate  */
1577c478bd9Sstevel@tonic-gate static int
objfs_mount(vfs_t * vfsp,vnode_t * mvp,struct mounta * uap,cred_t * cr)1587c478bd9Sstevel@tonic-gate objfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate 	objfs_vfs_t *data;
1617c478bd9Sstevel@tonic-gate 	dev_t dev;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	if (secpolicy_fs_mount(cr, mvp, vfsp) != 0)
1647c478bd9Sstevel@tonic-gate 		return (EPERM);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	if (mvp->v_type != VDIR)
1677c478bd9Sstevel@tonic-gate 		return (ENOTDIR);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	if ((uap->flags & MS_OVERLAY) == 0 &&
1707c478bd9Sstevel@tonic-gate 	    (mvp->v_count > 1 || (mvp->v_flag & VROOT)))
1717c478bd9Sstevel@tonic-gate 		return (EBUSY);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	data = kmem_alloc(sizeof (objfs_vfs_t), KM_SLEEP);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	/*
1767c478bd9Sstevel@tonic-gate 	 * Initialize vfs fields
1777c478bd9Sstevel@tonic-gate 	 */
1787c478bd9Sstevel@tonic-gate 	vfsp->vfs_bsize = DEV_BSIZE;
1797c478bd9Sstevel@tonic-gate 	vfsp->vfs_fstype = objfs_fstype;
1807c478bd9Sstevel@tonic-gate 	do {
1817c478bd9Sstevel@tonic-gate 		dev = makedevice(objfs_major,
182*1a5e258fSJosef 'Jeff' Sipek 		    atomic_inc_32_nv(&objfs_minor) & L_MAXMIN32);
1837c478bd9Sstevel@tonic-gate 	} while (vfs_devismounted(dev));
1847c478bd9Sstevel@tonic-gate 	vfs_make_fsid(&vfsp->vfs_fsid, dev, objfs_fstype);
1857c478bd9Sstevel@tonic-gate 	vfsp->vfs_data = data;
1867c478bd9Sstevel@tonic-gate 	vfsp->vfs_dev = dev;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	/*
1897c478bd9Sstevel@tonic-gate 	 * Create root
1907c478bd9Sstevel@tonic-gate 	 */
1917c478bd9Sstevel@tonic-gate 	data->objfs_vfs_root = objfs_create_root(vfsp);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	return (0);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate static int
objfs_unmount(vfs_t * vfsp,int flag,struct cred * cr)1977c478bd9Sstevel@tonic-gate objfs_unmount(vfs_t *vfsp, int flag, struct cred *cr)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	objfs_vfs_t *data;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (secpolicy_fs_unmount(cr, vfsp) != 0)
2027c478bd9Sstevel@tonic-gate 		return (EPERM);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	/*
2057c478bd9Sstevel@tonic-gate 	 * We do not currently support forced unmounts
2067c478bd9Sstevel@tonic-gate 	 */
2077c478bd9Sstevel@tonic-gate 	if (flag & MS_FORCE)
2087c478bd9Sstevel@tonic-gate 		return (ENOTSUP);
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	/*
2117c478bd9Sstevel@tonic-gate 	 * We should never have a reference count of less than 2: one for the
2127c478bd9Sstevel@tonic-gate 	 * caller, one for the root vnode.
2137c478bd9Sstevel@tonic-gate 	 */
2147c478bd9Sstevel@tonic-gate 	ASSERT(vfsp->vfs_count >= 2);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	/*
2177c478bd9Sstevel@tonic-gate 	 * Any active vnodes will result in a hold on the root vnode
2187c478bd9Sstevel@tonic-gate 	 */
2197c478bd9Sstevel@tonic-gate 	data = vfsp->vfs_data;
2207c478bd9Sstevel@tonic-gate 	if (data->objfs_vfs_root->v_count > 1)
2217c478bd9Sstevel@tonic-gate 		return (EBUSY);
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	/*
2247c478bd9Sstevel@tonic-gate 	 * Release the last hold on the root vnode
2257c478bd9Sstevel@tonic-gate 	 */
2267c478bd9Sstevel@tonic-gate 	VN_RELE(data->objfs_vfs_root);
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	kmem_free(data, sizeof (objfs_vfs_t));
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	return (0);
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate static int
objfs_root(vfs_t * vfsp,vnode_t ** vpp)2347c478bd9Sstevel@tonic-gate objfs_root(vfs_t *vfsp, vnode_t **vpp)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	objfs_vfs_t *data = vfsp->vfs_data;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	*vpp = data->objfs_vfs_root;
2397c478bd9Sstevel@tonic-gate 	VN_HOLD(*vpp);
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	return (0);
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate static int
objfs_statvfs(vfs_t * vfsp,statvfs64_t * sp)2457c478bd9Sstevel@tonic-gate objfs_statvfs(vfs_t *vfsp, statvfs64_t *sp)
2467c478bd9Sstevel@tonic-gate {
2477c478bd9Sstevel@tonic-gate 	dev32_t d32;
2487c478bd9Sstevel@tonic-gate 	int total = objfs_nobjs();
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	bzero(sp, sizeof (*sp));
2517c478bd9Sstevel@tonic-gate 	sp->f_bsize = DEV_BSIZE;
2527c478bd9Sstevel@tonic-gate 	sp->f_frsize = DEV_BSIZE;
2537c478bd9Sstevel@tonic-gate 	sp->f_files = total;
2547c478bd9Sstevel@tonic-gate 	sp->f_ffree = sp->f_favail = INT_MAX - total;
2557c478bd9Sstevel@tonic-gate 	(void) cmpldev(&d32, vfsp->vfs_dev);
2567c478bd9Sstevel@tonic-gate 	sp->f_fsid = d32;
2577c478bd9Sstevel@tonic-gate 	(void) strlcpy(sp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name,
2587c478bd9Sstevel@tonic-gate 	    sizeof (sp->f_basetype));
2597c478bd9Sstevel@tonic-gate 	sp->f_flag = vf_to_stf(vfsp->vfs_flag);
2607c478bd9Sstevel@tonic-gate 	sp->f_namemax = OBJFS_NAME_MAX;
2617c478bd9Sstevel@tonic-gate 	(void) strlcpy(sp->f_fstr, "object", sizeof (sp->f_fstr));
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	return (0);
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate static const fs_operation_def_t objfs_vfstops[] = {
267aa59c4cbSrsb 	{ VFSNAME_MOUNT,	{ .vfs_mount = objfs_mount } },
268aa59c4cbSrsb 	{ VFSNAME_UNMOUNT,	{ .vfs_unmount = objfs_unmount } },
269aa59c4cbSrsb 	{ VFSNAME_ROOT,		{ .vfs_root = objfs_root } },
270aa59c4cbSrsb 	{ VFSNAME_STATVFS,	{ .vfs_statvfs = objfs_statvfs } },
2717c478bd9Sstevel@tonic-gate 	{ NULL }
2727c478bd9Sstevel@tonic-gate };
273