Home | History | Annotate | Line # | Download | only in procfs
procfs_subr.c revision 1.68.14.4
      1 /*	$NetBSD: procfs_subr.c,v 1.68.14.4 2006/11/18 21:39:29 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1993
     41  *	The Regents of the University of California.  All rights reserved.
     42  *
     43  * This code is derived from software contributed to Berkeley by
     44  * Jan-Simon Pendry.
     45  *
     46  * Redistribution and use in source and binary forms, with or without
     47  * modification, are permitted provided that the following conditions
     48  * are met:
     49  * 1. Redistributions of source code must retain the above copyright
     50  *    notice, this list of conditions and the following disclaimer.
     51  * 2. Redistributions in binary form must reproduce the above copyright
     52  *    notice, this list of conditions and the following disclaimer in the
     53  *    documentation and/or other materials provided with the distribution.
     54  * 3. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  *
     70  *	@(#)procfs_subr.c	8.6 (Berkeley) 5/14/95
     71  */
     72 
     73 /*
     74  * Copyright (c) 1994 Christopher G. Demetriou.  All rights reserved.
     75  * Copyright (c) 1993 Jan-Simon Pendry
     76  *
     77  * This code is derived from software contributed to Berkeley by
     78  * Jan-Simon Pendry.
     79  *
     80  * Redistribution and use in source and binary forms, with or without
     81  * modification, are permitted provided that the following conditions
     82  * are met:
     83  * 1. Redistributions of source code must retain the above copyright
     84  *    notice, this list of conditions and the following disclaimer.
     85  * 2. Redistributions in binary form must reproduce the above copyright
     86  *    notice, this list of conditions and the following disclaimer in the
     87  *    documentation and/or other materials provided with the distribution.
     88  * 3. All advertising materials mentioning features or use of this software
     89  *    must display the following acknowledgement:
     90  *	This product includes software developed by the University of
     91  *	California, Berkeley and its contributors.
     92  * 4. Neither the name of the University nor the names of its contributors
     93  *    may be used to endorse or promote products derived from this software
     94  *    without specific prior written permission.
     95  *
     96  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     97  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     98  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     99  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    100  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    101  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    102  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    103  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    104  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    105  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    106  * SUCH DAMAGE.
    107  *
    108  *	@(#)procfs_subr.c	8.6 (Berkeley) 5/14/95
    109  */
    110 
    111 #include <sys/cdefs.h>
    112 __KERNEL_RCSID(0, "$NetBSD: procfs_subr.c,v 1.68.14.4 2006/11/18 21:39:29 ad Exp $");
    113 
    114 #include <sys/param.h>
    115 #include <sys/systm.h>
    116 #include <sys/time.h>
    117 #include <sys/kernel.h>
    118 #include <sys/proc.h>
    119 #include <sys/vnode.h>
    120 #include <sys/malloc.h>
    121 #include <sys/stat.h>
    122 #include <sys/file.h>
    123 #include <sys/filedesc.h>
    124 
    125 #include <miscfs/procfs/procfs.h>
    126 
    127 void procfs_hashins(struct pfsnode *);
    128 void procfs_hashrem(struct pfsnode *);
    129 struct vnode *procfs_hashget(pid_t, pfstype, int, struct mount *);
    130 
    131 LIST_HEAD(pfs_hashhead, pfsnode) *pfs_hashtbl;
    132 u_long	pfs_ihash;	/* size of hash table - 1 */
    133 #define PFSPIDHASH(pid)	((pid) & pfs_ihash)
    134 
    135 kmutex_t pfs_hashmutex;
    136 struct simplelock pfs_hash_slock;
    137 
    138 #define	ISSET(t, f)	((t) & (f))
    139 
    140 /*
    141  * allocate a pfsnode/vnode pair.  the vnode is
    142  * referenced, and locked.
    143  *
    144  * the pid, pfs_type, and mount point uniquely
    145  * identify a pfsnode.  the mount point is needed
    146  * because someone might mount this filesystem
    147  * twice.
    148  *
    149  * all pfsnodes are maintained on a singly-linked
    150  * list.  new nodes are only allocated when they cannot
    151  * be found on this list.  entries on the list are
    152  * removed when the vfs reclaim entry is called.
    153  *
    154  * a single lock is kept for the entire list.  this is
    155  * needed because the getnewvnode() function can block
    156  * waiting for a vnode to become free, in which case there
    157  * may be more than one process trying to get the same
    158  * vnode.  this lock is only taken if we are going to
    159  * call getnewvnode, since the kernel itself is single-threaded.
    160  *
    161  * if an entry is found on the list, then call vget() to
    162  * take a reference.  this is done because there may be
    163  * zero references to it and so it needs to removed from
    164  * the vnode free list.
    165  */
    166 int
    167 procfs_allocvp(mp, vpp, pid, pfs_type, fd, p)
    168 	struct mount *mp;
    169 	struct vnode **vpp;
    170 	pid_t pid;
    171 	pfstype pfs_type;
    172 	int fd;
    173 	struct proc *p;
    174 {
    175 	struct pfsnode *pfs;
    176 	struct vnode *vp;
    177 	int error;
    178 
    179 	do {
    180 		if ((*vpp = procfs_hashget(pid, pfs_type, fd, mp)) != NULL)
    181 			return (0);
    182 	} while (!mutex_tryenter(&pfs_hashmutex));
    183 
    184 	if ((error = getnewvnode(VT_PROCFS, mp, procfs_vnodeop_p, &vp)) != 0) {
    185 		*vpp = NULL;
    186 		mutex_exit(&pfs_hashmutex);
    187 		return (error);
    188 	}
    189 
    190 	MALLOC(pfs, void *, sizeof(struct pfsnode), M_TEMP, M_WAITOK);
    191 	vp->v_data = pfs;
    192 
    193 	pfs->pfs_pid = pid;
    194 	pfs->pfs_type = pfs_type;
    195 	pfs->pfs_vnode = vp;
    196 	pfs->pfs_flags = 0;
    197 	pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type, fd);
    198 	pfs->pfs_fd = fd;
    199 
    200 	switch (pfs_type) {
    201 	case PFSroot:	/* /proc = dr-xr-xr-x */
    202 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    203 		vp->v_type = VDIR;
    204 		vp->v_flag = VROOT;
    205 		break;
    206 
    207 	case PFScurproc:	/* /proc/curproc = lr-xr-xr-x */
    208 	case PFSself:	/* /proc/self    = lr-xr-xr-x */
    209 	case PFScwd:	/* /proc/N/cwd = lr-xr-xr-x */
    210 	case PFSchroot:	/* /proc/N/chroot = lr-xr-xr-x */
    211 	case PFSexe:	/* /proc/N/exe = lr-xr-xr-x */
    212 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    213 		vp->v_type = VLNK;
    214 		break;
    215 
    216 	case PFSproc:	/* /proc/N = dr-xr-xr-x */
    217 	case PFSfd:
    218 		if (fd == -1) {	/* /proc/N/fd = dr-xr-xr-x */
    219 			pfs->pfs_mode = S_IRUSR|S_IXUSR;
    220 			vp->v_type = VDIR;
    221 		} else {	/* /proc/N/fd/M = [ps-]rw------- */
    222 			struct file *fp;
    223 			struct vnode *vxp;
    224 
    225 			mutex_enter(&p->p_mutex);
    226 			fp = fd_getfile(p->p_fd, pfs->pfs_fd);
    227 			mutex_exit(&p->p_mutex);
    228 			if (fp == NULL) {
    229 				error = EBADF;
    230 				goto bad;
    231 			}
    232 			FILE_USE(fp);
    233 
    234 			pfs->pfs_mode = S_IRUSR|S_IWUSR;
    235 			switch (fp->f_type) {
    236 			case DTYPE_VNODE:
    237 				vxp = (struct vnode *)fp->f_data;
    238 
    239 				/*
    240 				 * We make symlinks for directories
    241 				 * to avoid cycles.
    242 				 */
    243 				if (vxp->v_type == VDIR)
    244 					goto symlink;
    245 				vp->v_type = vxp->v_type;
    246 				break;
    247 			case DTYPE_PIPE:
    248 				vp->v_type = VFIFO;
    249 				break;
    250 			case DTYPE_SOCKET:
    251 				vp->v_type = VSOCK;
    252 				break;
    253 			case DTYPE_KQUEUE:
    254 			case DTYPE_MISC:
    255 			symlink:
    256 				pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|
    257 				    S_IXGRP|S_IROTH|S_IXOTH;
    258 				vp->v_type = VLNK;
    259 				break;
    260 			default:
    261 				error = EOPNOTSUPP;
    262 				FILE_UNUSE(fp, curlwp);
    263 				goto bad;
    264 			}
    265 			FILE_UNUSE(fp, curlwp);
    266 		}
    267 		break;
    268 
    269 	case PFSfile:	/* /proc/N/file = -rw------- */
    270 	case PFSmem:	/* /proc/N/mem = -rw------- */
    271 	case PFSregs:	/* /proc/N/regs = -rw------- */
    272 	case PFSfpregs:	/* /proc/N/fpregs = -rw------- */
    273 		pfs->pfs_mode = S_IRUSR|S_IWUSR;
    274 		vp->v_type = VREG;
    275 		break;
    276 
    277 	case PFSctl:	/* /proc/N/ctl = --w------ */
    278 	case PFSnote:	/* /proc/N/note = --w------ */
    279 	case PFSnotepg:	/* /proc/N/notepg = --w------ */
    280 		pfs->pfs_mode = S_IWUSR;
    281 		vp->v_type = VREG;
    282 		break;
    283 
    284 	case PFSmap:	/* /proc/N/map = -r--r--r-- */
    285 	case PFSmaps:	/* /proc/N/maps = -r--r--r-- */
    286 	case PFSstatus:	/* /proc/N/status = -r--r--r-- */
    287 	case PFSstat:	/* /proc/N/stat = -r--r--r-- */
    288 	case PFScmdline:	/* /proc/N/cmdline = -r--r--r-- */
    289 	case PFSemul:	/* /proc/N/emul = -r--r--r-- */
    290 	case PFSmeminfo:	/* /proc/meminfo = -r--r--r-- */
    291 	case PFSdevices:	/* /proc/devices = -r--r--r-- */
    292 	case PFScpuinfo:	/* /proc/cpuinfo = -r--r--r-- */
    293 	case PFSuptime:	/* /proc/uptime = -r--r--r-- */
    294 	case PFSmounts:	/* /proc/mounts = -r--r--r-- */
    295 		pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH;
    296 		vp->v_type = VREG;
    297 		break;
    298 
    299 #ifdef __HAVE_PROCFS_MACHDEP
    300 	PROCFS_MACHDEP_NODETYPE_CASES
    301 		procfs_machdep_allocvp(vp);
    302 		break;
    303 #endif
    304 
    305 	default:
    306 		panic("procfs_allocvp");
    307 	}
    308 
    309 	procfs_hashins(pfs);
    310 	uvm_vnp_setsize(vp, 0);
    311 	mutex_exit(&pfs_hashmutex);
    312 
    313 	*vpp = vp;
    314 	return (0);
    315 
    316  bad:
    317 	mutex_exit(&pfs_hashmutex);
    318 	FREE(pfs, M_TEMP);
    319 	ungetnewvnode(vp);
    320 	return (error);
    321 }
    322 
    323 int
    324 procfs_freevp(vp)
    325 	struct vnode *vp;
    326 {
    327 	struct pfsnode *pfs = VTOPFS(vp);
    328 
    329 	procfs_hashrem(pfs);
    330 
    331 	FREE(vp->v_data, M_TEMP);
    332 	vp->v_data = 0;
    333 	return (0);
    334 }
    335 
    336 int
    337 procfs_rw(v)
    338 	void *v;
    339 {
    340 	struct vop_read_args *ap = v;
    341 	struct vnode *vp = ap->a_vp;
    342 	struct uio *uio = ap->a_uio;
    343 	struct lwp *curl;
    344 	struct lwp *l;
    345 	struct pfsnode *pfs = VTOPFS(vp);
    346 	struct proc *p;
    347 	int error;
    348 
    349 	if (uio->uio_offset < 0)
    350 		return EINVAL;
    351 
    352 	if ((error = procfs_proc_lock(pfs->pfs_pid, &p, ESRCH)) != 0)
    353 		return error;
    354 
    355 	/*
    356 	 * Do not allow init to be modified while in secure mode; it
    357 	 * could be duped into changing the security level.
    358 	 */
    359 	if (uio->uio_rw == UIO_WRITE && p == initproc && securelevel > -1)
    360 		return EPERM;
    361 
    362 	/*
    363 	 * Pick a suitable LWP to represent the process, and acquire a
    364 	 * reference on it so that it does not exit.
    365 	 *
    366 	 * XXX The entire procfs interface needs work to be useful to a
    367 	 * process with multiple LWPs.  For the moment, we'll just kluge
    368 	 * this and fail on others.
    369 	 */
    370 	mutex_enter(&p->p_smutex);
    371 	l = proc_representative_lwp(p, NULL, 1);
    372 	lwp_addref(l);
    373 	lwp_unlock(l);
    374 	mutex_exit(&p->p_smutex);
    375 
    376 	curl = curlwp;
    377 
    378 	switch (pfs->pfs_type) {
    379 	case PFSnote:
    380 	case PFSnotepg:
    381 		error = procfs_donote(curl, p, pfs, uio);
    382 		break;
    383 
    384 	case PFSregs:
    385 		error = procfs_doregs(curl, l, pfs, uio);
    386 		break;
    387 
    388 	case PFSfpregs:
    389 		error = procfs_dofpregs(curl, l, pfs, uio);
    390 		break;
    391 
    392 	case PFSctl:
    393 		error = procfs_doctl(curl, l, pfs, uio);
    394 		break;
    395 
    396 	case PFSstatus:
    397 		error = procfs_dostatus(curl, l, pfs, uio);
    398 		break;
    399 
    400 	case PFSstat:
    401 		error = procfs_do_pid_stat(curl, l, pfs, uio);
    402 		break;
    403 
    404 	case PFSmap:
    405 		error = procfs_domap(curl, p, pfs, uio, 0);
    406 		break;
    407 
    408 	case PFSmaps:
    409 		error = procfs_domap(curl, p, pfs, uio, 1);
    410 		break;
    411 
    412 	case PFSmem:
    413 		error = procfs_domem(curl, l, pfs, uio);
    414 		break;
    415 
    416 	case PFScmdline:
    417 		error = procfs_docmdline(curl, p, pfs, uio);
    418 		break;
    419 
    420 	case PFSmeminfo:
    421 		error = procfs_domeminfo(curl, p, pfs, uio);
    422 		break;
    423 
    424 	case PFSdevices:
    425 		return (procfs_dodevices(curl, p, pfs, uio));
    426 
    427 	case PFScpuinfo:
    428 		error = procfs_docpuinfo(curl, p, pfs, uio);
    429 		break;
    430 
    431 	case PFSfd:
    432 		error = procfs_dofd(curl, p, pfs, uio);
    433 		break;
    434 
    435 	case PFSuptime:
    436 		error = procfs_douptime(curl, p, pfs, uio);
    437 		break;
    438 
    439 	case PFSmounts:
    440 		error = procfs_domounts(curl, p, pfs, uio);
    441 		break;
    442 
    443 	case PFSemul:
    444 		return procfs_doemul(curl, p, pfs, uio);
    445 
    446 #ifdef __HAVE_PROCFS_MACHDEP
    447 	PROCFS_MACHDEP_NODETYPE_CASES
    448 		error = procfs_machdep_rw(curl, l, pfs, uio);
    449 		break;
    450 #endif
    451 
    452 	default:
    453 		error = EOPNOTSUPP;
    454 		break;
    455 	}
    456 
    457 	/*
    458 	 * Release the references that we acquired earlier.
    459 	 */
    460 	lwp_delref(l);
    461 	procfs_proc_unlock(p);
    462 
    463 	return (error);
    464 }
    465 
    466 /*
    467  * Get a string from userland into (bf).  Strip a trailing
    468  * nl character (to allow easy access from the shell).
    469  * The buffer should be *buflenp + 1 chars long.  vfs_getuserstr
    470  * will automatically add a nul char at the end.
    471  *
    472  * Returns 0 on success or the following errors
    473  *
    474  * EINVAL:    file offset is non-zero.
    475  * EMSGSIZE:  message is longer than kernel buffer
    476  * EFAULT:    user i/o buffer is not addressable
    477  */
    478 int
    479 vfs_getuserstr(uio, bf, buflenp)
    480 	struct uio *uio;
    481 	char *bf;
    482 	int *buflenp;
    483 {
    484 	int xlen;
    485 	int error;
    486 
    487 	if (uio->uio_offset != 0)
    488 		return (EINVAL);
    489 
    490 	xlen = *buflenp;
    491 
    492 	/* must be able to read the whole string in one go */
    493 	if (xlen < uio->uio_resid)
    494 		return (EMSGSIZE);
    495 	xlen = uio->uio_resid;
    496 
    497 	if ((error = uiomove(bf, xlen, uio)) != 0)
    498 		return (error);
    499 
    500 	/* allow multiple writes without seeks */
    501 	uio->uio_offset = 0;
    502 
    503 	/* cleanup string and remove trailing newline */
    504 	bf[xlen] = '\0';
    505 	xlen = strlen(bf);
    506 	if (xlen > 0 && bf[xlen-1] == '\n')
    507 		bf[--xlen] = '\0';
    508 	*buflenp = xlen;
    509 
    510 	return (0);
    511 }
    512 
    513 const vfs_namemap_t *
    514 vfs_findname(nm, bf, buflen)
    515 	const vfs_namemap_t *nm;
    516 	const char *bf;
    517 	int buflen;
    518 {
    519 
    520 	for (; nm->nm_name; nm++)
    521 		if (memcmp(bf, nm->nm_name, buflen+1) == 0)
    522 			return (nm);
    523 
    524 	return (0);
    525 }
    526 
    527 /*
    528  * Initialize pfsnode hash table.
    529  */
    530 void
    531 procfs_hashinit()
    532 {
    533 	mutex_init(&pfs_hashmutex, MUTEX_DEFAULT, IPL_NONE);
    534 	pfs_hashtbl = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT,
    535 	    M_WAITOK, &pfs_ihash);
    536 	simple_lock_init(&pfs_hash_slock);
    537 }
    538 
    539 void
    540 procfs_hashreinit()
    541 {
    542 	struct pfsnode *pp;
    543 	struct pfs_hashhead *oldhash, *hash;
    544 	u_long i, oldmask, mask, val;
    545 
    546 	hash = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT, M_WAITOK,
    547 	    &mask);
    548 
    549 	simple_lock(&pfs_hash_slock);
    550 	oldhash = pfs_hashtbl;
    551 	oldmask = pfs_ihash;
    552 	pfs_hashtbl = hash;
    553 	pfs_ihash = mask;
    554 	for (i = 0; i <= oldmask; i++) {
    555 		while ((pp = LIST_FIRST(&oldhash[i])) != NULL) {
    556 			LIST_REMOVE(pp, pfs_hash);
    557 			val = PFSPIDHASH(pp->pfs_pid);
    558 			LIST_INSERT_HEAD(&hash[val], pp, pfs_hash);
    559 		}
    560 	}
    561 	simple_unlock(&pfs_hash_slock);
    562 	hashdone(oldhash, M_UFSMNT);
    563 }
    564 
    565 /*
    566  * Free pfsnode hash table.
    567  */
    568 void
    569 procfs_hashdone()
    570 {
    571 	hashdone(pfs_hashtbl, M_UFSMNT);
    572 }
    573 
    574 struct vnode *
    575 procfs_hashget(pid, type, fd, mp)
    576 	pid_t pid;
    577 	pfstype type;
    578 	int fd;
    579 	struct mount *mp;
    580 {
    581 	struct pfs_hashhead *ppp;
    582 	struct pfsnode *pp;
    583 	struct vnode *vp;
    584 
    585 loop:
    586 	simple_lock(&pfs_hash_slock);
    587 	ppp = &pfs_hashtbl[PFSPIDHASH(pid)];
    588 	LIST_FOREACH(pp, ppp, pfs_hash) {
    589 		vp = PFSTOV(pp);
    590 		if (pid == pp->pfs_pid && pp->pfs_type == type &&
    591 		    pp->pfs_fd == fd && vp->v_mount == mp) {
    592 			simple_lock(&vp->v_interlock);
    593 			simple_unlock(&pfs_hash_slock);
    594 			if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
    595 				goto loop;
    596 			return (vp);
    597 		}
    598 	}
    599 	simple_unlock(&pfs_hash_slock);
    600 	return (NULL);
    601 }
    602 
    603 /*
    604  * Insert the pfsnode into the hash table and lock it.
    605  */
    606 void
    607 procfs_hashins(pp)
    608 	struct pfsnode *pp;
    609 {
    610 	struct pfs_hashhead *ppp;
    611 
    612 	/* lock the pfsnode, then put it on the appropriate hash list */
    613 	lockmgr(&pp->pfs_vnode->v_lock, LK_EXCLUSIVE, (struct simplelock *)0);
    614 
    615 	simple_lock(&pfs_hash_slock);
    616 	ppp = &pfs_hashtbl[PFSPIDHASH(pp->pfs_pid)];
    617 	LIST_INSERT_HEAD(ppp, pp, pfs_hash);
    618 	simple_unlock(&pfs_hash_slock);
    619 }
    620 
    621 /*
    622  * Remove the pfsnode from the hash table.
    623  */
    624 void
    625 procfs_hashrem(pp)
    626 	struct pfsnode *pp;
    627 {
    628 	simple_lock(&pfs_hash_slock);
    629 	LIST_REMOVE(pp, pfs_hash);
    630 	simple_unlock(&pfs_hash_slock);
    631 }
    632 
    633 void
    634 procfs_revoke_vnodes(p, arg)
    635 	struct proc *p;
    636 	void *arg;
    637 {
    638 	struct pfsnode *pfs, *pnext;
    639 	struct vnode *vp;
    640 	struct mount *mp = (struct mount *)arg;
    641 	struct pfs_hashhead *ppp;
    642 
    643 	if (!(p->p_flag & P_SUGID))
    644 		return;
    645 
    646 	ppp = &pfs_hashtbl[PFSPIDHASH(p->p_pid)];
    647 	for (pfs = LIST_FIRST(ppp); pfs; pfs = pnext) {
    648 		vp = PFSTOV(pfs);
    649 		pnext = LIST_NEXT(pfs, pfs_hash);
    650 		if (vp->v_usecount > 0 && pfs->pfs_pid == p->p_pid &&
    651 		    vp->v_mount == mp)
    652 			VOP_REVOKE(vp, REVOKEALL);
    653 	}
    654 }
    655 
    656 int
    657 procfs_proc_lock(int pid, struct proc **bunghole, int notfound)
    658 {
    659 	struct proc *tp;
    660 	int error = 0;
    661 
    662 	rw_enter(&proclist_lock, RW_READER);
    663 
    664 	if (pid == 0)
    665 		tp = &proc0;
    666 	else if ((tp = p_find(pid, PFIND_LOCKED)) == NULL)
    667 		error = notfound;
    668 
    669 	if (tp != NULL) {
    670 		mutex_enter(&tp->p_mutex);
    671 		error = proc_addref(tp);
    672 		mutex_exit(&tp->p_mutex);
    673 	}
    674 
    675 	rw_exit(&proclist_lock);
    676 
    677 	*bunghole = tp;
    678 	return error;
    679 }
    680 
    681 void
    682 procfs_proc_unlock(struct proc *p)
    683 {
    684 	mutex_enter(&p->p_mutex);
    685 	proc_delref(p);
    686 	mutex_exit(&p->p_mutex);
    687 }
    688 
    689 int
    690 procfs_doemul(struct lwp *curl, struct proc *p,
    691     struct pfsnode *pfs, struct uio *uio)
    692 {
    693 	const char *ename = p->p_emul->e_name;
    694 	return uiomove_frombuf(__UNCONST(ename), strlen(ename), uio);
    695 }
    696