xref: /titanic_52/usr/src/uts/common/fs/objfs/objfs_root.c (revision ab04eb8ef60d9dc9614d6cccffc474f24ca1d162)
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 /*
22*ab04eb8eStimh  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/errno.h>
327c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
337c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
347c478bd9Sstevel@tonic-gate #include <sys/objfs.h>
357c478bd9Sstevel@tonic-gate #include <sys/objfs_impl.h>
36aa59c4cbSrsb #include <sys/vfs_opreg.h>
377c478bd9Sstevel@tonic-gate #include <sys/systm.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate extern int last_module_id;
407c478bd9Sstevel@tonic-gate 
41da6c28aaSamw static int objfs_root_do_lookup(vnode_t *, const char *, vnode_t **, ino64_t *,
42*ab04eb8eStimh     cred_t *, int, int *, pathname_t *);
43b38f0970Sck153898 static int objfs_root_do_readdir(vnode_t *, void *, int *,
44b38f0970Sck153898     offset_t *, offset_t *, void *, int);
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate vnode_t *
477c478bd9Sstevel@tonic-gate objfs_create_root(vfs_t *vfsp)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate 	vnode_t *vp = gfs_root_create(sizeof (objfs_rootnode_t), vfsp,
507c478bd9Sstevel@tonic-gate 	    objfs_ops_root, OBJFS_INO_ROOT, NULL, NULL, OBJFS_NAME_MAX,
517c478bd9Sstevel@tonic-gate 	    objfs_root_do_readdir, objfs_root_do_lookup);
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	return (vp);
547c478bd9Sstevel@tonic-gate }
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /* ARGSUSED */
577c478bd9Sstevel@tonic-gate static int
58da6c28aaSamw objfs_root_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
59da6c28aaSamw 	caller_context_t *ct)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	vap->va_type = VDIR;
627c478bd9Sstevel@tonic-gate 	vap->va_mode = 0555;
637c478bd9Sstevel@tonic-gate 	vap->va_nodeid = gfs_file_inode(vp);
647c478bd9Sstevel@tonic-gate 	vap->va_nlink = objfs_nobjs() + 2;
657c478bd9Sstevel@tonic-gate 	vap->va_size = vap->va_nlink;
667c478bd9Sstevel@tonic-gate 	vap->va_atime.tv_sec = vp->v_vfsp->vfs_mtime;
677c478bd9Sstevel@tonic-gate 	vap->va_atime.tv_nsec = 0;
687c478bd9Sstevel@tonic-gate 	vap->va_mtime = vap->va_ctime = vap->va_atime;
697c478bd9Sstevel@tonic-gate 	return (objfs_common_getattr(vp, vap));
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate 
72da6c28aaSamw /* ARGSUSED */
737c478bd9Sstevel@tonic-gate static int
74da6c28aaSamw objfs_root_do_lookup(vnode_t *vp, const char *nm, vnode_t **vpp, ino64_t *inop,
75*ab04eb8eStimh     cred_t *cr, int flags, int *deflags, pathname_t *rpnp)
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate 	int result = ENOENT;
787c478bd9Sstevel@tonic-gate 	struct modctl *mp;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	/*
817c478bd9Sstevel@tonic-gate 	 * Run through all the loaded modules on the system looking for
827c478bd9Sstevel@tonic-gate 	 * a matching module name.
837c478bd9Sstevel@tonic-gate 	 */
847c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
857c478bd9Sstevel@tonic-gate 	mp = &modules;
867c478bd9Sstevel@tonic-gate 	do {
877c478bd9Sstevel@tonic-gate 		if (mp->mod_loaded &&
887c478bd9Sstevel@tonic-gate 		    strcmp(nm, mp->mod_modname) == 0) {
897c478bd9Sstevel@tonic-gate 			/*
907c478bd9Sstevel@tonic-gate 			 * We drop mod_lock in order to do allocations,
917c478bd9Sstevel@tonic-gate 			 * as modctls are persistent.
927c478bd9Sstevel@tonic-gate 			 */
937c478bd9Sstevel@tonic-gate 			mutex_exit(&mod_lock);
947c478bd9Sstevel@tonic-gate 			*vpp = objfs_create_odirnode(vp, mp);
957c478bd9Sstevel@tonic-gate 			*inop = OBJFS_INO_ODIR(mp->mod_id);
967c478bd9Sstevel@tonic-gate 			mutex_enter(&mod_lock);
977c478bd9Sstevel@tonic-gate 			result = 0;
987c478bd9Sstevel@tonic-gate 			break;
997c478bd9Sstevel@tonic-gate 		}
1007c478bd9Sstevel@tonic-gate 	} while ((mp = mp->mod_next) != &modules);
1017c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	return (result);
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /* ARGSUSED */
1077c478bd9Sstevel@tonic-gate int
108b38f0970Sck153898 objfs_root_do_readdir(vnode_t *vp, void *dp, int *eofp,
109b38f0970Sck153898     offset_t *offp, offset_t *nextp, void *data, int flags)
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate 	struct modctl **mpp = data;
1127c478bd9Sstevel@tonic-gate 	struct modctl *mp = *mpp;
113b38f0970Sck153898 	struct dirent64 *odp = dp;
114b38f0970Sck153898 
115*ab04eb8eStimh 	ASSERT(!(flags & V_RDDIR_ENTFLAGS));
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	/* Check for EOF */
1207c478bd9Sstevel@tonic-gate 	if (*offp >= last_module_id) {
1217c478bd9Sstevel@tonic-gate 		*eofp = 1;
1227c478bd9Sstevel@tonic-gate 		mutex_exit(&mod_lock);
1237c478bd9Sstevel@tonic-gate 		return (0);
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	/*
1277c478bd9Sstevel@tonic-gate 	 * Find the appropriate modctl
1287c478bd9Sstevel@tonic-gate 	 */
1297c478bd9Sstevel@tonic-gate 	while (mp->mod_id < *offp) {
1307c478bd9Sstevel@tonic-gate 		mp = mp->mod_next;
1317c478bd9Sstevel@tonic-gate 		ASSERT(mp != &modules);
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	while (!mp->mod_loaded && mp != &modules)
1357c478bd9Sstevel@tonic-gate 		mp = mp->mod_next;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	if (mp == &modules && *offp != 0) {
1387c478bd9Sstevel@tonic-gate 		*eofp = 1;
1397c478bd9Sstevel@tonic-gate 		mutex_exit(&mod_lock);
1407c478bd9Sstevel@tonic-gate 		return (0);
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	/*
1447c478bd9Sstevel@tonic-gate 	 * The modctl will not change, as they are persistent.
1457c478bd9Sstevel@tonic-gate 	 */
1467c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
1477c478bd9Sstevel@tonic-gate 
148b38f0970Sck153898 	(void) strncpy(odp->d_name, mp->mod_modname, OBJFS_NAME_MAX);
149b38f0970Sck153898 	odp->d_ino = OBJFS_INO_ODIR(mp->mod_id);
1507c478bd9Sstevel@tonic-gate 	*offp = mp->mod_id;
1517c478bd9Sstevel@tonic-gate 	*nextp = mp->mod_id + 1;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	return (0);
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate /* ARGSUSED */
1577c478bd9Sstevel@tonic-gate static int
158da6c28aaSamw objfs_root_readdir(vnode_t *vp, uio_t *uiop, cred_t *cr, int *eofp,
159da6c28aaSamw 	caller_context_t *ct, int flags)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	struct modctl *mp = &modules;
1627c478bd9Sstevel@tonic-gate 
163b38f0970Sck153898 	return (gfs_dir_readdir(vp, uiop, eofp, &mp, cr, ct, flags));
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate const fs_operation_def_t objfs_tops_root[] = {
167aa59c4cbSrsb 	{ VOPNAME_OPEN,		{ .vop_open = objfs_dir_open } },
168aa59c4cbSrsb 	{ VOPNAME_CLOSE,	{ .vop_close = objfs_common_close } },
169aa59c4cbSrsb 	{ VOPNAME_IOCTL,	{ .error = fs_inval } },
170aa59c4cbSrsb 	{ VOPNAME_GETATTR,	{ .vop_getattr = objfs_root_getattr } },
171aa59c4cbSrsb 	{ VOPNAME_ACCESS,	{ .vop_access = objfs_dir_access } },
172aa59c4cbSrsb 	{ VOPNAME_READDIR,	{ .vop_readdir = objfs_root_readdir } },
173aa59c4cbSrsb 	{ VOPNAME_LOOKUP,	{ .vop_lookup = gfs_vop_lookup } },
174aa59c4cbSrsb 	{ VOPNAME_SEEK,		{ .vop_seek = fs_seek } },
175aa59c4cbSrsb 	{ VOPNAME_INACTIVE,	{ .vop_inactive = gfs_vop_inactive } },
1767c478bd9Sstevel@tonic-gate 	{ NULL }
1777c478bd9Sstevel@tonic-gate };
178