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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <fs/fs_subr.h>
27 
28 #include <sys/errno.h>
29 #include <sys/kmem.h>
30 #include <sys/modctl.h>
31 #include <sys/objfs.h>
32 #include <sys/objfs_impl.h>
33 #include <sys/vfs_opreg.h>
34 #include <sys/systm.h>
35 
36 extern int last_module_id;
37 
38 static int objfs_root_do_lookup(vnode_t *, const char *, vnode_t **, ino64_t *,
39     cred_t *, int, int *, pathname_t *);
40 static int objfs_root_do_readdir(vnode_t *, void *, int *,
41     offset_t *, offset_t *, void *, int);
42 
43 vnode_t *
objfs_create_root(vfs_t * vfsp)44 objfs_create_root(vfs_t *vfsp)
45 {
46 	vnode_t *vp = gfs_root_create(sizeof (objfs_rootnode_t), vfsp,
47 	    objfs_ops_root, OBJFS_INO_ROOT, NULL, NULL, OBJFS_NAME_MAX,
48 	    objfs_root_do_readdir, objfs_root_do_lookup);
49 
50 	return (vp);
51 }
52 
53 /* ARGSUSED */
54 static int
objfs_root_getattr(vnode_t * vp,vattr_t * vap,int flags,cred_t * cr,caller_context_t * ct)55 objfs_root_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
56 	caller_context_t *ct)
57 {
58 	vap->va_type = VDIR;
59 	vap->va_mode = 0555;
60 	vap->va_nodeid = gfs_file_inode(vp);
61 	vap->va_nlink = objfs_nobjs() + 2;
62 	vap->va_size = vap->va_nlink;
63 	vap->va_atime.tv_sec = vp->v_vfsp->vfs_mtime;
64 	vap->va_atime.tv_nsec = 0;
65 	vap->va_mtime = vap->va_ctime = vap->va_atime;
66 	return (objfs_common_getattr(vp, vap));
67 }
68 
69 /* ARGSUSED */
70 static int
objfs_root_do_lookup(vnode_t * vp,const char * nm,vnode_t ** vpp,ino64_t * inop,cred_t * cr,int flags,int * deflags,pathname_t * rpnp)71 objfs_root_do_lookup(vnode_t *vp, const char *nm, vnode_t **vpp, ino64_t *inop,
72     cred_t *cr, int flags, int *deflags, pathname_t *rpnp)
73 {
74 	int result = ENOENT;
75 	struct modctl *mp;
76 
77 	/*
78 	 * Run through all the loaded modules on the system looking for
79 	 * a matching module name.
80 	 */
81 	mutex_enter(&mod_lock);
82 	mp = &modules;
83 	do {
84 		if (mp->mod_loaded &&
85 		    strcmp(nm, mp->mod_modname) == 0) {
86 			/*
87 			 * We drop mod_lock in order to do allocations,
88 			 * as modctls are persistent.
89 			 */
90 			mutex_exit(&mod_lock);
91 			*vpp = objfs_create_odirnode(vp, mp);
92 			*inop = OBJFS_INO_ODIR(mp->mod_id);
93 			mutex_enter(&mod_lock);
94 			result = 0;
95 			break;
96 		}
97 	} while ((mp = mp->mod_next) != &modules);
98 	mutex_exit(&mod_lock);
99 
100 	return (result);
101 }
102 
103 /* ARGSUSED */
104 int
objfs_root_do_readdir(vnode_t * vp,void * dp,int * eofp,offset_t * offp,offset_t * nextp,void * data,int flags)105 objfs_root_do_readdir(vnode_t *vp, void *dp, int *eofp,
106     offset_t *offp, offset_t *nextp, void *data, int flags)
107 {
108 	struct modctl **mpp = data;
109 	struct modctl *mp = *mpp;
110 	struct dirent64 *odp = dp;
111 
112 	ASSERT(!(flags & V_RDDIR_ENTFLAGS));
113 
114 	mutex_enter(&mod_lock);
115 
116 	/* Check for EOF */
117 	if (*offp >= last_module_id) {
118 		*eofp = 1;
119 		mutex_exit(&mod_lock);
120 		return (0);
121 	}
122 
123 	/*
124 	 * Find the appropriate modctl
125 	 */
126 	while (mp->mod_id < *offp) {
127 		mp = mp->mod_next;
128 		ASSERT(mp != &modules);
129 	}
130 
131 	while (!mp->mod_loaded && mp != &modules)
132 		mp = mp->mod_next;
133 
134 	if (mp == &modules && *offp != 0) {
135 		*eofp = 1;
136 		mutex_exit(&mod_lock);
137 		return (0);
138 	}
139 
140 	/*
141 	 * The modctl will not change, as they are persistent.
142 	 */
143 	mutex_exit(&mod_lock);
144 
145 	(void) strncpy(odp->d_name, mp->mod_modname, OBJFS_NAME_MAX);
146 	odp->d_ino = OBJFS_INO_ODIR(mp->mod_id);
147 	*offp = mp->mod_id;
148 	*nextp = mp->mod_id + 1;
149 
150 	return (0);
151 }
152 
153 /* ARGSUSED */
154 static int
objfs_root_readdir(vnode_t * vp,uio_t * uiop,cred_t * cr,int * eofp,caller_context_t * ct,int flags)155 objfs_root_readdir(vnode_t *vp, uio_t *uiop, cred_t *cr, int *eofp,
156 	caller_context_t *ct, int flags)
157 {
158 	struct modctl *mp = &modules;
159 
160 	return (gfs_dir_readdir(vp, uiop, eofp, &mp, cr, ct, flags));
161 }
162 
163 const fs_operation_def_t objfs_tops_root[] = {
164 	{ VOPNAME_OPEN,		{ .vop_open = objfs_dir_open } },
165 	{ VOPNAME_CLOSE,	{ .vop_close = objfs_common_close } },
166 	{ VOPNAME_IOCTL,	{ .error = fs_inval } },
167 	{ VOPNAME_GETATTR,	{ .vop_getattr = objfs_root_getattr } },
168 	{ VOPNAME_ACCESS,	{ .vop_access = objfs_dir_access } },
169 	{ VOPNAME_READDIR,	{ .vop_readdir = objfs_root_readdir } },
170 	{ VOPNAME_LOOKUP,	{ .vop_lookup = gfs_vop_lookup } },
171 	{ VOPNAME_SEEK,		{ .vop_seek = fs_seek } },
172 	{ VOPNAME_INACTIVE,	{ .vop_inactive = gfs_vop_inactive } },
173 	{ NULL }
174 };
175