summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/fs/proc/prvfsops.c
blob: efc3c37ce743000f1e3c14c73fcb827b33c02e95 (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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * 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) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/


#include <sys/types.h>
#include <sys/param.h>
#include <sys/cmn_err.h>
#include <sys/cred.h>
#include <sys/debug.h>
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/procfs.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/zone.h>
#include <sys/var.h>
#include <sys/vfs.h>
#include <sys/vfs_opreg.h>
#include <sys/vnode.h>
#include <sys/mode.h>
#include <sys/signal.h>
#include <sys/user.h>
#include <sys/mount.h>
#include <sys/bitmap.h>
#include <sys/kmem.h>
#include <sys/policy.h>
#include <fs/fs_subr.h>
#include <fs/proc/prdata.h>

/*
 * This is the loadable module wrapper.
 */
#include <sys/modctl.h>

static int prinit();

static mntopts_t proc_mntopts = {
	NULL,
	0
};

static vfsdef_t vfw = {
	VFSDEF_VERSION,
	"proc",
	prinit,
	VSW_HASPROTO|VSW_STATS|VSW_XID|VSW_ZMOUNT,
	&proc_mntopts
};

/*
 * Module linkage information for the kernel.
 */
extern struct mod_ops mod_fsops;

static struct modlfs modlfs = {
	&mod_fsops, "filesystem for proc", &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));
}

/*
 * N.B.
 * No _fini routine. The module cannot be unloaded once loaded.
 * The NO_UNLOAD_STUB in modstubs.s must change if this module
 * is ever modified to become unloadable.
 */

int		nproc_highbit;		/* highbit(v.v_nproc) */

static int	procfstype;
static major_t	procfs_major;
static minor_t	procfs_minor;
static kmutex_t	procfs_minor_lock;

static kmutex_t	pr_mount_lock;

/*
 * /proc VFS operations vector.
 */
static int	prmount(), prunmount(), prroot(), prstatvfs();

static void
prinitrootnode(prnode_t *pnp, vfs_t *vfsp)
{
	struct vnode *vp;

	bzero((caddr_t)pnp, sizeof (*pnp));
	pnp->pr_vnode = vp = vn_alloc(KM_SLEEP);

	mutex_init(&pnp->pr_mutex, NULL, MUTEX_DEFAULT, NULL);
	vp->v_flag = VROOT|VNOCACHE|VNOMAP|VNOSWAP|VNOMOUNT;
	VN_SET_VFS_TYPE_DEV(vp, vfsp, VDIR, 0);
	vn_setops(vp, prvnodeops);
	vp->v_data = (caddr_t)pnp;
	pnp->pr_type = PR_PROCDIR;
	pnp->pr_mode = 0555;	/* read-search by everyone */
	vn_exists(vp);
}

static int
prinit(int fstype, char *name)
{
	static const fs_operation_def_t pr_vfsops_template[] = {
		VFSNAME_MOUNT,		{ .vfs_mount = prmount },
		VFSNAME_UNMOUNT,	{ .vfs_unmount = prunmount },
		VFSNAME_ROOT,		{ .vfs_root = prroot },
		VFSNAME_STATVFS,	{ .vfs_statvfs = prstatvfs },
		NULL,			NULL
	};
	extern const fs_operation_def_t pr_vnodeops_template[];
	int error;

	nproc_highbit = highbit(v.v_proc);
	procfstype = fstype;
	ASSERT(procfstype != 0);
	/*
	 * Associate VFS ops vector with this fstype.
	 */
	error = vfs_setfsops(fstype, pr_vfsops_template, NULL);
	if (error != 0) {
		cmn_err(CE_WARN, "prinit: bad vfs ops template");
		return (error);
	}

	/*
	 * Set up vnode ops vector too.
	 */

	error = vn_make_ops(name, pr_vnodeops_template, &prvnodeops);
	if (error != 0) {
		(void) vfs_freevfsops_by_type(fstype);
		cmn_err(CE_WARN, "prinit: bad vnode ops template");
		return (error);
	}

	/*
	 * Assign a unique "device" number (used by stat(2)).
	 */
	if ((procfs_major = getudev()) == (major_t)-1) {
		cmn_err(CE_WARN, "prinit: can't get unique device number");
		procfs_major = 0;
	}
	mutex_init(&pr_mount_lock, NULL, MUTEX_DEFAULT, NULL);
	mutex_init(&procfs_minor_lock, NULL, MUTEX_DEFAULT, NULL);

	return (0);
}

/* ARGSUSED */
static int
prmount(struct vfs *vfsp, struct vnode *mvp,
	struct mounta *uap, struct cred *cr)
{
	prnode_t *pnp;
	zone_t *zone = curproc->p_zone;

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

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

	if (zone == global_zone) {
		zone_t *mntzone;

		mntzone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
		zone_rele(mntzone);
		if (zone != mntzone)
			return (EBUSY);
	}
	/*
	 * Having the resource be anything but "proc" doesn't make sense
	 */
	vfs_setresource(vfsp, "proc", 0);

	pnp = kmem_alloc(sizeof (*pnp), KM_SLEEP);
	mutex_enter(&pr_mount_lock);

	mutex_enter(&mvp->v_lock);
	if ((uap->flags & MS_OVERLAY) == 0 &&
	    (mvp->v_count > 1 || (mvp->v_flag & VROOT))) {
		mutex_exit(&mvp->v_lock);
		mutex_exit(&pr_mount_lock);
		kmem_free(pnp, sizeof (*pnp));
		return (EBUSY);
	}
	mutex_exit(&mvp->v_lock);

	prinitrootnode(pnp, vfsp);
	vfsp->vfs_fstype = procfstype;
	vfsp->vfs_data = (caddr_t)pnp;
	vfsp->vfs_bsize = DEV_BSIZE;
	/*
	 * find an available minor device number for this mount
	 */
	mutex_enter(&procfs_minor_lock);
	do {
		vfsp->vfs_dev = makedevice(procfs_major, procfs_minor);
		procfs_minor = (procfs_minor + 1) & L_MAXMIN32;
	} while (vfs_devismounted(vfsp->vfs_dev));
	mutex_exit(&procfs_minor_lock);
	vfs_make_fsid(&vfsp->vfs_fsid, vfsp->vfs_dev, procfstype);

	mutex_exit(&pr_mount_lock);
	return (0);
}

/* ARGSUSED */
static int
prunmount(struct vfs *vfsp, int flag, struct cred *cr)
{
	prnode_t *pnp = (prnode_t *)vfsp->vfs_data;
	vnode_t *vp = PTOV(pnp);

	mutex_enter(&pr_mount_lock);
	if (secpolicy_fs_unmount(cr, vfsp) != 0) {
		mutex_exit(&pr_mount_lock);
		return (EPERM);
	}

	/*
	 * forced unmount is not supported by this file system
	 * and thus, ENOTSUP, is being returned.
	 */
	if (flag & MS_FORCE) {
		mutex_exit(&pr_mount_lock);
		return (ENOTSUP);
	}

	/*
	 * Ensure that no /proc vnodes are in use on this mount point.
	 */
	mutex_enter(&vp->v_lock);
	if (vp->v_count > 1) {
		mutex_exit(&vp->v_lock);
		mutex_exit(&pr_mount_lock);
		return (EBUSY);
	}

	mutex_exit(&vp->v_lock);
	mutex_exit(&pr_mount_lock);
	vn_invalid(vp);
	vn_free(vp);
	kmem_free(pnp, sizeof (*pnp));
	return (0);
}

/* ARGSUSED */
static int
prroot(struct vfs *vfsp, struct vnode **vpp)
{
	vnode_t *vp = PTOV((prnode_t *)vfsp->vfs_data);

	VN_HOLD(vp);
	*vpp = vp;
	return (0);
}

static int
prstatvfs(struct vfs *vfsp, struct statvfs64 *sp)
{
	int n;
	dev32_t d32;
	extern uint_t nproc;

	n = v.v_proc - nproc;

	bzero((caddr_t)sp, sizeof (*sp));
	sp->f_bsize	= DEV_BSIZE;
	sp->f_frsize	= DEV_BSIZE;
	sp->f_blocks	= (fsblkcnt64_t)0;
	sp->f_bfree	= (fsblkcnt64_t)0;
	sp->f_bavail	= (fsblkcnt64_t)0;
	sp->f_files	= (fsfilcnt64_t)v.v_proc + 2;
	sp->f_ffree	= (fsfilcnt64_t)n;
	sp->f_favail	= (fsfilcnt64_t)n;
	(void) cmpldev(&d32, vfsp->vfs_dev);
	sp->f_fsid	= d32;
	(void) strcpy(sp->f_basetype, vfssw[procfstype].vsw_name);
	sp->f_flag = vf_to_stf(vfsp->vfs_flag);
	sp->f_namemax = 64;		/* quite arbitrary */
	bzero(sp->f_fstr, sizeof (sp->f_fstr));
	(void) strcpy(sp->f_fstr, "/proc");
	(void) strcpy(&sp->f_fstr[6], "/proc");
	return (0);
}