summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/fs/objfs/objfs_vfs.c
blob: 00dafeb625b09e6c2d575eb7e880600ea5431880 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
 */

#include <sys/atomic.h>
#include <sys/cmn_err.h>
#include <sys/errno.h>
#include <sys/mount.h>
#include <sys/objfs.h>
#include <sys/objfs_impl.h>
#include <sys/vfs_opreg.h>
#include <sys/policy.h>
#include <sys/sunddi.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>

/*
 * Kernel object filesystem.
 *
 * This is a pseudo filesystem which exports information about currently loaded
 * kernel objects.  The root directory contains one directory for each loaded
 * object, indexed by module name.  Within each object directory is an ELF file,
 * 'object', that contains information about the currently loaded module.
 *
 * This file contains functions that interact with the VFS layer.  Each
 * filesystem element is represented by a a different node.
 *
 * 	/		objfs_rootnode_t	objfs_root.c
 *	/<obj>		objfs_odirnode_t	objfs_odir.c
 *	/<obj>/object	objfs_datanode_t	objfs_data.c
 *
 * In addition, some common routines are found in the 'objfs_common.c' file.
 */

vnodeops_t *objfs_ops_root;
vnodeops_t *objfs_ops_odir;
vnodeops_t *objfs_ops_data;

static const fs_operation_def_t objfs_vfstops[];
static gfs_opsvec_t objfs_opsvec[];

static int objfs_init(int, char *);

/*
 * Module linkage
 */
static mntopts_t objfs_mntopts = {
	0,
	NULL
};

static vfsdef_t vfw = {
	VFSDEF_VERSION,
	"objfs",
	objfs_init,
	VSW_HASPROTO | VSW_ZMOUNT,
	&objfs_mntopts,
};

extern struct mod_ops mod_fsops;

static struct modlfs modlfs = {
	&mod_fsops, "kernel object filesystem", &vfw
};

static struct modlinkage modlinkage = {
	MODREV_1, (void *)&modlfs, NULL
};

int
_init(void)
{
	return (mod_install(&modlinkage));
}

int
_info(struct modinfo *modinfop)
{
	return (mod_info(&modlinkage, modinfop));
}

int
_fini(void)
{
	/*
	 * The object filesystem cannot be unloaded.
	 */
	return (EBUSY);
}

/*
 * Filesystem initialization.
 */

static int objfs_fstype;
static major_t objfs_major;
static minor_t objfs_minor;

static gfs_opsvec_t objfs_opsvec[] = {
	{ "objfs root directory", objfs_tops_root, &objfs_ops_root },
	{ "objfs object directory", objfs_tops_odir, &objfs_ops_odir },
	{ "objfs data file", objfs_tops_data, &objfs_ops_data },
	{ NULL }
};

/* ARGSUSED */
static int
objfs_init(int fstype, char *name)
{
	vfsops_t *vfsops;
	int error;

	objfs_fstype = fstype;
	if (error = vfs_setfsops(fstype, objfs_vfstops, &vfsops)) {
		cmn_err(CE_WARN, "objfs_init: bad vfs ops template");
		return (error);
	}

	if (error = gfs_make_opsvec(objfs_opsvec)) {
		(void) vfs_freevfsops(vfsops);
		return (error);
	}

	if ((objfs_major = getudev()) == (major_t)-1) {
		cmn_err(CE_WARN, "objfs_init: can't get unique device number");
		objfs_major = 0;
	}

	objfs_data_init();

	return (0);
}

/*
 * VFS entry points
 */
static int
objfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
{
	objfs_vfs_t *data;
	dev_t dev;

	if (secpolicy_fs_mount(cr, mvp, vfsp) != 0)
		return (EPERM);

	if (mvp->v_type != VDIR)
		return (ENOTDIR);

	if ((uap->flags & MS_OVERLAY) == 0 &&
	    (mvp->v_count > 1 || (mvp->v_flag & VROOT)))
		return (EBUSY);

	data = kmem_alloc(sizeof (objfs_vfs_t), KM_SLEEP);

	/*
	 * Initialize vfs fields
	 */
	vfsp->vfs_bsize = DEV_BSIZE;
	vfsp->vfs_fstype = objfs_fstype;
	do {
		dev = makedevice(objfs_major,
		    atomic_add_32_nv(&objfs_minor, 1) & L_MAXMIN32);
	} while (vfs_devismounted(dev));
	vfs_make_fsid(&vfsp->vfs_fsid, dev, objfs_fstype);
	vfsp->vfs_data = data;
	vfsp->vfs_dev = dev;

	/*
	 * Create root
	 */
	data->objfs_vfs_root = objfs_create_root(vfsp);

	return (0);
}

static int
objfs_unmount(vfs_t *vfsp, int flag, struct cred *cr)
{
	objfs_vfs_t *data;

	if (secpolicy_fs_unmount(cr, vfsp) != 0)
		return (EPERM);

	/*
	 * We do not currently support forced unmounts
	 */
	if (flag & MS_FORCE)
		return (ENOTSUP);

	/*
	 * We should never have a reference count of less than 2: one for the
	 * caller, one for the root vnode.
	 */
	ASSERT(vfsp->vfs_count >= 2);

	/*
	 * Any active vnodes will result in a hold on the root vnode
	 */
	data = vfsp->vfs_data;
	if (data->objfs_vfs_root->v_count > 1)
		return (EBUSY);

	/*
	 * Release the last hold on the root vnode
	 */
	VN_RELE(data->objfs_vfs_root);

	kmem_free(data, sizeof (objfs_vfs_t));

	return (0);
}

static int
objfs_root(vfs_t *vfsp, vnode_t **vpp)
{
	objfs_vfs_t *data = vfsp->vfs_data;

	*vpp = data->objfs_vfs_root;
	VN_HOLD(*vpp);

	return (0);
}

static int
objfs_statvfs(vfs_t *vfsp, statvfs64_t *sp)
{
	dev32_t d32;
	int total = objfs_nobjs();

	bzero(sp, sizeof (*sp));
	sp->f_bsize = DEV_BSIZE;
	sp->f_frsize = DEV_BSIZE;
	sp->f_files = total;
	sp->f_ffree = sp->f_favail = INT_MAX - total;
	(void) cmpldev(&d32, vfsp->vfs_dev);
	sp->f_fsid = d32;
	(void) strlcpy(sp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name,
	    sizeof (sp->f_basetype));
	sp->f_flag = vf_to_stf(vfsp->vfs_flag);
	sp->f_namemax = OBJFS_NAME_MAX;
	(void) strlcpy(sp->f_fstr, "object", sizeof (sp->f_fstr));

	return (0);
}

static const fs_operation_def_t objfs_vfstops[] = {
	{ VFSNAME_MOUNT,	{ .vfs_mount = objfs_mount } },
	{ VFSNAME_UNMOUNT,	{ .vfs_unmount = objfs_unmount } },
	{ VFSNAME_ROOT,		{ .vfs_root = objfs_root } },
	{ VFSNAME_STATVFS,	{ .vfs_statvfs = objfs_statvfs } },
	{ NULL }
};