Home | History | Annotate | Line # | Download | only in procfs
procfs_subr.c revision 1.36.2.9
      1  1.36.2.9   thorpej /*	$NetBSD: procfs_subr.c,v 1.36.2.9 2003/01/07 21:41:14 thorpej Exp $	*/
      2      1.13       cgd 
      3       1.1        pk /*
      4      1.20   thorpej  * Copyright (c) 1994 Christopher G. Demetriou.  All rights reserved.
      5       1.5       cgd  * Copyright (c) 1993 Jan-Simon Pendry
      6      1.11   mycroft  * Copyright (c) 1993
      7      1.11   mycroft  *	The Regents of the University of California.  All rights reserved.
      8       1.2        pk  *
      9       1.5       cgd  * This code is derived from software contributed to Berkeley by
     10       1.5       cgd  * Jan-Simon Pendry.
     11       1.5       cgd  *
     12       1.2        pk  * Redistribution and use in source and binary forms, with or without
     13       1.2        pk  * modification, are permitted provided that the following conditions
     14       1.2        pk  * are met:
     15       1.2        pk  * 1. Redistributions of source code must retain the above copyright
     16       1.2        pk  *    notice, this list of conditions and the following disclaimer.
     17       1.2        pk  * 2. Redistributions in binary form must reproduce the above copyright
     18       1.2        pk  *    notice, this list of conditions and the following disclaimer in the
     19       1.2        pk  *    documentation and/or other materials provided with the distribution.
     20       1.2        pk  * 3. All advertising materials mentioning features or use of this software
     21       1.2        pk  *    must display the following acknowledgement:
     22       1.5       cgd  *	This product includes software developed by the University of
     23       1.5       cgd  *	California, Berkeley and its contributors.
     24       1.5       cgd  * 4. Neither the name of the University nor the names of its contributors
     25       1.5       cgd  *    may be used to endorse or promote products derived from this software
     26       1.5       cgd  *    without specific prior written permission.
     27       1.5       cgd  *
     28       1.5       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29       1.5       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30       1.5       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31       1.5       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32       1.5       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33       1.5       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34       1.5       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35       1.5       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36       1.5       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37       1.5       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38       1.5       cgd  * SUCH DAMAGE.
     39       1.2        pk  *
     40      1.23      fvdl  *	@(#)procfs_subr.c	8.6 (Berkeley) 5/14/95
     41       1.1        pk  */
     42  1.36.2.4   nathanw 
     43  1.36.2.4   nathanw #include <sys/cdefs.h>
     44  1.36.2.9   thorpej __KERNEL_RCSID(0, "$NetBSD: procfs_subr.c,v 1.36.2.9 2003/01/07 21:41:14 thorpej Exp $");
     45       1.5       cgd 
     46       1.4   mycroft #include <sys/param.h>
     47       1.4   mycroft #include <sys/systm.h>
     48       1.4   mycroft #include <sys/time.h>
     49       1.4   mycroft #include <sys/kernel.h>
     50       1.4   mycroft #include <sys/proc.h>
     51       1.4   mycroft #include <sys/vnode.h>
     52      1.11   mycroft #include <sys/malloc.h>
     53      1.18   mycroft #include <sys/stat.h>
     54  1.36.2.9   thorpej #include <sys/file.h>
     55  1.36.2.9   thorpej #include <sys/filedesc.h>
     56      1.18   mycroft 
     57       1.5       cgd #include <miscfs/procfs/procfs.h>
     58       1.1        pk 
     59      1.29      fvdl void procfs_hashins __P((struct pfsnode *));
     60      1.29      fvdl void procfs_hashrem __P((struct pfsnode *));
     61  1.36.2.9   thorpej struct vnode *procfs_hashget __P((pid_t, pfstype, int, struct mount *));
     62      1.29      fvdl 
     63      1.29      fvdl LIST_HEAD(pfs_hashhead, pfsnode) *pfs_hashtbl;
     64      1.30      fvdl u_long	pfs_ihash;	/* size of hash table - 1 */
     65  1.36.2.3   nathanw #define PFSPIDHASH(pid)	((pid) & pfs_ihash)
     66      1.29      fvdl 
     67      1.29      fvdl struct lock pfs_hashlock;
     68      1.29      fvdl struct simplelock pfs_hash_slock;
     69       1.1        pk 
     70      1.20   thorpej #define	ISSET(t, f)	((t) & (f))
     71      1.20   thorpej 
     72       1.1        pk /*
     73       1.5       cgd  * allocate a pfsnode/vnode pair.  the vnode is
     74      1.27  wrstuden  * referenced, and locked.
     75       1.5       cgd  *
     76       1.5       cgd  * the pid, pfs_type, and mount point uniquely
     77       1.5       cgd  * identify a pfsnode.  the mount point is needed
     78       1.5       cgd  * because someone might mount this filesystem
     79       1.5       cgd  * twice.
     80       1.5       cgd  *
     81       1.5       cgd  * all pfsnodes are maintained on a singly-linked
     82       1.5       cgd  * list.  new nodes are only allocated when they cannot
     83       1.5       cgd  * be found on this list.  entries on the list are
     84       1.5       cgd  * removed when the vfs reclaim entry is called.
     85       1.5       cgd  *
     86       1.5       cgd  * a single lock is kept for the entire list.  this is
     87       1.5       cgd  * needed because the getnewvnode() function can block
     88       1.5       cgd  * waiting for a vnode to become free, in which case there
     89       1.5       cgd  * may be more than one process trying to get the same
     90       1.5       cgd  * vnode.  this lock is only taken if we are going to
     91       1.5       cgd  * call getnewvnode, since the kernel itself is single-threaded.
     92       1.5       cgd  *
     93       1.5       cgd  * if an entry is found on the list, then call vget() to
     94       1.5       cgd  * take a reference.  this is done because there may be
     95       1.5       cgd  * zero references to it and so it needs to removed from
     96       1.5       cgd  * the vnode free list.
     97       1.1        pk  */
     98      1.11   mycroft int
     99  1.36.2.9   thorpej procfs_allocvp(mp, vpp, pid, pfs_type, fd)
    100       1.5       cgd 	struct mount *mp;
    101       1.5       cgd 	struct vnode **vpp;
    102  1.36.2.9   thorpej 	pid_t pid;
    103       1.5       cgd 	pfstype pfs_type;
    104  1.36.2.9   thorpej 	int fd;
    105       1.1        pk {
    106      1.12   mycroft 	struct pfsnode *pfs;
    107      1.12   mycroft 	struct vnode *vp;
    108       1.5       cgd 	int error;
    109       1.5       cgd 
    110      1.29      fvdl 	do {
    111  1.36.2.9   thorpej 		if ((*vpp = procfs_hashget(pid, pfs_type, fd, mp)) != NULL)
    112       1.5       cgd 			return (0);
    113      1.29      fvdl 	} while (lockmgr(&pfs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
    114       1.1        pk 
    115      1.29      fvdl 	if ((error = getnewvnode(VT_PROCFS, mp, procfs_vnodeop_p, vpp)) != 0) {
    116      1.29      fvdl 		*vpp = NULL;
    117      1.33       chs 		lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
    118      1.29      fvdl 		return (error);
    119       1.5       cgd 	}
    120      1.11   mycroft 	vp = *vpp;
    121       1.5       cgd 
    122      1.11   mycroft 	MALLOC(pfs, void *, sizeof(struct pfsnode), M_TEMP, M_WAITOK);
    123      1.11   mycroft 	vp->v_data = pfs;
    124       1.5       cgd 
    125  1.36.2.9   thorpej 	pfs->pfs_pid = pid;
    126       1.5       cgd 	pfs->pfs_type = pfs_type;
    127      1.11   mycroft 	pfs->pfs_vnode = vp;
    128       1.5       cgd 	pfs->pfs_flags = 0;
    129  1.36.2.9   thorpej 	pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type, fd);
    130  1.36.2.9   thorpej 	pfs->pfs_fd = fd;
    131       1.5       cgd 
    132       1.5       cgd 	switch (pfs_type) {
    133      1.11   mycroft 	case Proot:	/* /proc = dr-xr-xr-x */
    134      1.17   mycroft 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    135      1.11   mycroft 		vp->v_type = VDIR;
    136      1.11   mycroft 		vp->v_flag = VROOT;
    137      1.11   mycroft 		break;
    138      1.11   mycroft 
    139      1.22   mycroft 	case Pcurproc:	/* /proc/curproc = lr-xr-xr-x */
    140      1.28   thorpej 	case Pself:	/* /proc/self    = lr-xr-xr-x */
    141      1.22   mycroft 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    142      1.11   mycroft 		vp->v_type = VLNK;
    143       1.5       cgd 		break;
    144       1.5       cgd 
    145      1.17   mycroft 	case Pproc:	/* /proc/N = dr-xr-xr-x */
    146  1.36.2.9   thorpej 	case Pfd:
    147  1.36.2.9   thorpej 		if (fd == -1) {	/* /proc/N/fd = dr-xr-xr-x */
    148  1.36.2.9   thorpej 			pfs->pfs_mode =
    149  1.36.2.9   thorpej 			    S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    150  1.36.2.9   thorpej 			vp->v_type = VDIR;
    151  1.36.2.9   thorpej 		} else {	/* /proc/N/fd/M = [ps-]rw------- */
    152  1.36.2.9   thorpej 			struct file *fp;
    153  1.36.2.9   thorpej 			struct vnode *vxp;
    154  1.36.2.9   thorpej 			if ((error = procfs_getfp(pfs, &fp)) != 0)
    155  1.36.2.9   thorpej 				return error;
    156  1.36.2.9   thorpej 			pfs->pfs_mode = S_IRUSR|S_IWUSR;
    157  1.36.2.9   thorpej 			switch (fp->f_type) {
    158  1.36.2.9   thorpej 			case DTYPE_VNODE:
    159  1.36.2.9   thorpej 				vxp = (struct vnode *)fp->f_data;
    160  1.36.2.9   thorpej 				vp->v_type = vxp->v_type;
    161  1.36.2.9   thorpej 				break;
    162  1.36.2.9   thorpej 			case DTYPE_PIPE:
    163  1.36.2.9   thorpej 				vp->v_type = VFIFO;
    164  1.36.2.9   thorpej 				break;
    165  1.36.2.9   thorpej 			case DTYPE_SOCKET:
    166  1.36.2.9   thorpej 				vp->v_type = VSOCK;
    167  1.36.2.9   thorpej 				break;
    168  1.36.2.9   thorpej 			case DTYPE_KQUEUE:
    169  1.36.2.9   thorpej 			case DTYPE_MISC:
    170  1.36.2.9   thorpej 				return EOPNOTSUPP;
    171  1.36.2.9   thorpej 			default:
    172  1.36.2.9   thorpej 				panic("unknown file type %d\n", fp->f_type);
    173  1.36.2.9   thorpej 			}
    174  1.36.2.9   thorpej 		}
    175       1.5       cgd 		break;
    176       1.5       cgd 
    177      1.17   mycroft 	case Pfile:	/* /proc/N/file = -rw------- */
    178      1.17   mycroft 	case Pmem:	/* /proc/N/mem = -rw------- */
    179      1.17   mycroft 	case Pregs:	/* /proc/N/regs = -rw------- */
    180      1.17   mycroft 	case Pfpregs:	/* /proc/N/fpregs = -rw------- */
    181      1.17   mycroft 		pfs->pfs_mode = S_IRUSR|S_IWUSR;
    182       1.9       cgd 		vp->v_type = VREG;
    183       1.9       cgd 		break;
    184       1.9       cgd 
    185      1.17   mycroft 	case Pctl:	/* /proc/N/ctl = --w------ */
    186      1.17   mycroft 	case Pnote:	/* /proc/N/note = --w------ */
    187      1.17   mycroft 	case Pnotepg:	/* /proc/N/notepg = --w------ */
    188      1.17   mycroft 		pfs->pfs_mode = S_IWUSR;
    189       1.6        ws 		vp->v_type = VREG;
    190       1.5       cgd 		break;
    191       1.5       cgd 
    192      1.25   msaitoh 	case Pmap:	/* /proc/N/map = -r--r--r-- */
    193  1.36.2.2   nathanw 	case Pmaps:	/* /proc/N/maps = -r--r--r-- */
    194      1.17   mycroft 	case Pstatus:	/* /proc/N/status = -r--r--r-- */
    195      1.26  christos 	case Pcmdline:	/* /proc/N/cmdline = -r--r--r-- */
    196      1.35      fvdl 	case Pmeminfo:	/* /proc/meminfo = -r--r--r-- */
    197      1.35      fvdl 	case Pcpuinfo:	/* /proc/cpuinfo = -r--r--r-- */
    198      1.17   mycroft 		pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH;
    199       1.6        ws 		vp->v_type = VREG;
    200       1.5       cgd 		break;
    201       1.5       cgd 
    202  1.36.2.5   nathanw #ifdef __HAVE_PROCFS_MACHDEP
    203  1.36.2.5   nathanw 	PROCFS_MACHDEP_NODETYPE_CASES
    204  1.36.2.5   nathanw 		procfs_machdep_allocvp(vp);
    205  1.36.2.5   nathanw 		break;
    206  1.36.2.5   nathanw #endif
    207  1.36.2.5   nathanw 
    208       1.5       cgd 	default:
    209      1.11   mycroft 		panic("procfs_allocvp");
    210       1.5       cgd 	}
    211      1.27  wrstuden 
    212      1.29      fvdl 	procfs_hashins(pfs);
    213      1.34       chs 	uvm_vnp_setsize(vp, 0);
    214      1.33       chs 	lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
    215       1.1        pk 
    216       1.5       cgd 	return (error);
    217       1.1        pk }
    218       1.1        pk 
    219      1.11   mycroft int
    220       1.5       cgd procfs_freevp(vp)
    221       1.5       cgd 	struct vnode *vp;
    222       1.1        pk {
    223       1.5       cgd 	struct pfsnode *pfs = VTOPFS(vp);
    224       1.5       cgd 
    225      1.29      fvdl 	procfs_hashrem(pfs);
    226       1.1        pk 
    227      1.11   mycroft 	FREE(vp->v_data, M_TEMP);
    228      1.11   mycroft 	vp->v_data = 0;
    229       1.5       cgd 	return (0);
    230       1.1        pk }
    231       1.1        pk 
    232      1.11   mycroft int
    233      1.15  christos procfs_rw(v)
    234      1.15  christos 	void *v;
    235       1.1        pk {
    236      1.15  christos 	struct vop_read_args *ap = v;
    237      1.11   mycroft 	struct vnode *vp = ap->a_vp;
    238      1.11   mycroft 	struct uio *uio = ap->a_uio;
    239       1.5       cgd 	struct proc *curp = uio->uio_procp;
    240       1.5       cgd 	struct pfsnode *pfs = VTOPFS(vp);
    241  1.36.2.1   nathanw 	struct lwp *l;
    242       1.5       cgd 	struct proc *p;
    243       1.5       cgd 
    244       1.5       cgd 	p = PFIND(pfs->pfs_pid);
    245       1.5       cgd 	if (p == 0)
    246       1.1        pk 		return (EINVAL);
    247      1.19   mycroft 
    248  1.36.2.1   nathanw 	/* XXX NJWLWP
    249  1.36.2.1   nathanw 	 * The entire procfs interface needs work to be useful to
    250  1.36.2.1   nathanw 	 * a process with multiple LWPs. For the moment, we'll
    251  1.36.2.1   nathanw 	 * just kluge this and fail on others.
    252  1.36.2.1   nathanw 	 */
    253  1.36.2.6   nathanw 	l = proc_representative_lwp(p);
    254  1.36.2.1   nathanw 
    255      1.19   mycroft 	switch (pfs->pfs_type) {
    256      1.19   mycroft 	case Pregs:
    257      1.19   mycroft 	case Pfpregs:
    258      1.19   mycroft 	case Pmem:
    259  1.36.2.5   nathanw #if defined(__HAVE_PROCFS_MACHDEP) && defined(PROCFS_MACHDEP_PROTECT_CASES)
    260  1.36.2.5   nathanw 	PROCFS_MACHDEP_PROTECT_CASES
    261  1.36.2.5   nathanw #endif
    262      1.19   mycroft 		/*
    263      1.19   mycroft 		 * Do not allow init to be modified while in secure mode; it
    264      1.19   mycroft 		 * could be duped into changing the security level.
    265      1.19   mycroft 		 */
    266      1.19   mycroft 		if (uio->uio_rw == UIO_WRITE &&
    267      1.19   mycroft 		    p == initproc && securelevel > -1)
    268      1.19   mycroft 			return (EPERM);
    269      1.19   mycroft 		break;
    270      1.19   mycroft 
    271      1.19   mycroft 	default:
    272      1.19   mycroft 		break;
    273      1.19   mycroft 	}
    274       1.1        pk 
    275       1.5       cgd 	switch (pfs->pfs_type) {
    276       1.5       cgd 	case Pnote:
    277       1.5       cgd 	case Pnotepg:
    278      1.11   mycroft 		return (procfs_donote(curp, p, pfs, uio));
    279       1.5       cgd 
    280       1.5       cgd 	case Pregs:
    281  1.36.2.1   nathanw 		return (procfs_doregs(curp, l, pfs, uio));
    282       1.9       cgd 
    283       1.9       cgd 	case Pfpregs:
    284  1.36.2.1   nathanw 		return (procfs_dofpregs(curp, l, pfs, uio));
    285       1.5       cgd 
    286       1.5       cgd 	case Pctl:
    287  1.36.2.1   nathanw 		return (procfs_doctl(curp, l, pfs, uio));
    288       1.5       cgd 
    289       1.5       cgd 	case Pstatus:
    290  1.36.2.1   nathanw 		return (procfs_dostatus(curp, l, pfs, uio));
    291      1.25   msaitoh 
    292      1.25   msaitoh 	case Pmap:
    293  1.36.2.2   nathanw 		return (procfs_domap(curp, p, pfs, uio, 0));
    294  1.36.2.2   nathanw 
    295  1.36.2.2   nathanw 	case Pmaps:
    296  1.36.2.2   nathanw 		return (procfs_domap(curp, p, pfs, uio, 1));
    297       1.1        pk 
    298       1.5       cgd 	case Pmem:
    299  1.36.2.7   nathanw 		return (procfs_domem(curp, p, pfs, uio));
    300      1.26  christos 
    301      1.26  christos 	case Pcmdline:
    302      1.26  christos 		return (procfs_docmdline(curp, p, pfs, uio));
    303      1.35      fvdl 
    304      1.35      fvdl 	case Pmeminfo:
    305      1.35      fvdl 		return (procfs_domeminfo(curp, p, pfs, uio));
    306  1.36.2.5   nathanw 
    307      1.35      fvdl 	case Pcpuinfo:
    308      1.35      fvdl 		return (procfs_docpuinfo(curp, p, pfs, uio));
    309  1.36.2.5   nathanw 
    310  1.36.2.9   thorpej 	case Pfd:
    311  1.36.2.9   thorpej 		return (procfs_dofd(curp, p, pfs, uio));
    312  1.36.2.9   thorpej 
    313  1.36.2.5   nathanw #ifdef __HAVE_PROCFS_MACHDEP
    314  1.36.2.5   nathanw 	PROCFS_MACHDEP_NODETYPE_CASES
    315  1.36.2.6   nathanw 		return (procfs_machdep_rw(curp, l, pfs, uio));
    316  1.36.2.5   nathanw #endif
    317       1.1        pk 
    318       1.5       cgd 	default:
    319       1.5       cgd 		return (EOPNOTSUPP);
    320       1.5       cgd 	}
    321       1.1        pk }
    322       1.1        pk 
    323       1.5       cgd /*
    324       1.5       cgd  * Get a string from userland into (buf).  Strip a trailing
    325       1.5       cgd  * nl character (to allow easy access from the shell).
    326      1.11   mycroft  * The buffer should be *buflenp + 1 chars long.  vfs_getuserstr
    327       1.5       cgd  * will automatically add a nul char at the end.
    328       1.5       cgd  *
    329       1.5       cgd  * Returns 0 on success or the following errors
    330       1.5       cgd  *
    331       1.5       cgd  * EINVAL:    file offset is non-zero.
    332       1.5       cgd  * EMSGSIZE:  message is longer than kernel buffer
    333       1.5       cgd  * EFAULT:    user i/o buffer is not addressable
    334       1.5       cgd  */
    335      1.11   mycroft int
    336      1.11   mycroft vfs_getuserstr(uio, buf, buflenp)
    337       1.5       cgd 	struct uio *uio;
    338       1.5       cgd 	char *buf;
    339       1.5       cgd 	int *buflenp;
    340       1.1        pk {
    341       1.5       cgd 	int xlen;
    342       1.5       cgd 	int error;
    343       1.5       cgd 
    344      1.11   mycroft 	if (uio->uio_offset != 0)
    345      1.11   mycroft 		return (EINVAL);
    346      1.11   mycroft 
    347       1.5       cgd 	xlen = *buflenp;
    348       1.1        pk 
    349       1.5       cgd 	/* must be able to read the whole string in one go */
    350       1.5       cgd 	if (xlen < uio->uio_resid)
    351       1.5       cgd 		return (EMSGSIZE);
    352       1.5       cgd 	xlen = uio->uio_resid;
    353       1.5       cgd 
    354      1.14  christos 	if ((error = uiomove(buf, xlen, uio)) != 0)
    355       1.5       cgd 		return (error);
    356       1.5       cgd 
    357      1.11   mycroft 	/* allow multiple writes without seeks */
    358      1.11   mycroft 	uio->uio_offset = 0;
    359      1.11   mycroft 
    360       1.5       cgd 	/* cleanup string and remove trailing newline */
    361       1.5       cgd 	buf[xlen] = '\0';
    362       1.5       cgd 	xlen = strlen(buf);
    363       1.5       cgd 	if (xlen > 0 && buf[xlen-1] == '\n')
    364       1.5       cgd 		buf[--xlen] = '\0';
    365       1.5       cgd 	*buflenp = xlen;
    366       1.1        pk 
    367       1.5       cgd 	return (0);
    368       1.1        pk }
    369       1.1        pk 
    370      1.36  jdolecek const vfs_namemap_t *
    371      1.11   mycroft vfs_findname(nm, buf, buflen)
    372      1.36  jdolecek 	const vfs_namemap_t *nm;
    373      1.36  jdolecek 	const char *buf;
    374       1.5       cgd 	int buflen;
    375       1.1        pk {
    376      1.11   mycroft 
    377       1.5       cgd 	for (; nm->nm_name; nm++)
    378      1.24     perry 		if (memcmp(buf, nm->nm_name, buflen+1) == 0)
    379       1.5       cgd 			return (nm);
    380       1.5       cgd 
    381       1.5       cgd 	return (0);
    382      1.29      fvdl }
    383      1.29      fvdl 
    384      1.29      fvdl /*
    385      1.29      fvdl  * Initialize pfsnode hash table.
    386      1.29      fvdl  */
    387      1.29      fvdl void
    388      1.29      fvdl procfs_hashinit()
    389      1.29      fvdl {
    390      1.29      fvdl 	lockinit(&pfs_hashlock, PINOD, "pfs_hashlock", 0, 0);
    391      1.32        ad 	pfs_hashtbl = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT,
    392      1.32        ad 	    M_WAITOK, &pfs_ihash);
    393      1.29      fvdl 	simple_lock_init(&pfs_hash_slock);
    394      1.31  jdolecek }
    395      1.31  jdolecek 
    396  1.36.2.3   nathanw void
    397  1.36.2.3   nathanw procfs_hashreinit()
    398  1.36.2.3   nathanw {
    399  1.36.2.3   nathanw 	struct pfsnode *pp;
    400  1.36.2.3   nathanw 	struct pfs_hashhead *oldhash, *hash;
    401  1.36.2.8   nathanw 	u_long i, oldmask, mask, val;
    402  1.36.2.3   nathanw 
    403  1.36.2.3   nathanw 	hash = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT, M_WAITOK,
    404  1.36.2.3   nathanw 	    &mask);
    405  1.36.2.3   nathanw 
    406  1.36.2.3   nathanw 	simple_lock(&pfs_hash_slock);
    407  1.36.2.3   nathanw 	oldhash = pfs_hashtbl;
    408  1.36.2.3   nathanw 	oldmask = pfs_ihash;
    409  1.36.2.3   nathanw 	pfs_hashtbl = hash;
    410  1.36.2.3   nathanw 	pfs_ihash = mask;
    411  1.36.2.3   nathanw 	for (i = 0; i <= oldmask; i++) {
    412  1.36.2.3   nathanw 		while ((pp = LIST_FIRST(&oldhash[i])) != NULL) {
    413  1.36.2.3   nathanw 			LIST_REMOVE(pp, pfs_hash);
    414  1.36.2.3   nathanw 			val = PFSPIDHASH(pp->pfs_pid);
    415  1.36.2.3   nathanw 			LIST_INSERT_HEAD(&hash[val], pp, pfs_hash);
    416  1.36.2.3   nathanw 		}
    417  1.36.2.3   nathanw 	}
    418  1.36.2.3   nathanw 	simple_unlock(&pfs_hash_slock);
    419  1.36.2.3   nathanw 	hashdone(oldhash, M_UFSMNT);
    420  1.36.2.3   nathanw }
    421  1.36.2.3   nathanw 
    422      1.31  jdolecek /*
    423      1.31  jdolecek  * Free pfsnode hash table.
    424      1.31  jdolecek  */
    425      1.31  jdolecek void
    426      1.31  jdolecek procfs_hashdone()
    427      1.31  jdolecek {
    428      1.31  jdolecek 	hashdone(pfs_hashtbl, M_UFSMNT);
    429      1.29      fvdl }
    430      1.29      fvdl 
    431      1.29      fvdl struct vnode *
    432  1.36.2.9   thorpej procfs_hashget(pid, type, fd, mp)
    433      1.29      fvdl 	pid_t pid;
    434      1.29      fvdl 	pfstype type;
    435  1.36.2.9   thorpej 	int fd;
    436      1.29      fvdl 	struct mount *mp;
    437      1.29      fvdl {
    438  1.36.2.3   nathanw 	struct pfs_hashhead *ppp;
    439      1.29      fvdl 	struct pfsnode *pp;
    440      1.29      fvdl 	struct vnode *vp;
    441      1.29      fvdl 
    442      1.29      fvdl loop:
    443      1.29      fvdl 	simple_lock(&pfs_hash_slock);
    444  1.36.2.3   nathanw 	ppp = &pfs_hashtbl[PFSPIDHASH(pid)];
    445  1.36.2.3   nathanw 	LIST_FOREACH(pp, ppp, pfs_hash) {
    446      1.29      fvdl 		vp = PFSTOV(pp);
    447      1.29      fvdl 		if (pid == pp->pfs_pid && pp->pfs_type == type &&
    448  1.36.2.9   thorpej 		    pp->pfs_fd == fd && vp->v_mount == mp) {
    449      1.29      fvdl 			simple_lock(&vp->v_interlock);
    450      1.29      fvdl 			simple_unlock(&pfs_hash_slock);
    451      1.29      fvdl 			if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
    452      1.29      fvdl 				goto loop;
    453      1.29      fvdl 			return (vp);
    454      1.29      fvdl 		}
    455      1.29      fvdl 	}
    456      1.29      fvdl 	simple_unlock(&pfs_hash_slock);
    457      1.29      fvdl 	return (NULL);
    458      1.29      fvdl }
    459      1.29      fvdl 
    460      1.29      fvdl /*
    461      1.29      fvdl  * Insert the pfsnode into the hash table and lock it.
    462      1.29      fvdl  */
    463      1.29      fvdl void
    464      1.29      fvdl procfs_hashins(pp)
    465      1.29      fvdl 	struct pfsnode *pp;
    466      1.29      fvdl {
    467      1.29      fvdl 	struct pfs_hashhead *ppp;
    468      1.29      fvdl 
    469      1.29      fvdl 	/* lock the pfsnode, then put it on the appropriate hash list */
    470      1.29      fvdl 	lockmgr(&pp->pfs_vnode->v_lock, LK_EXCLUSIVE, (struct simplelock *)0);
    471      1.29      fvdl 
    472      1.29      fvdl 	simple_lock(&pfs_hash_slock);
    473  1.36.2.3   nathanw 	ppp = &pfs_hashtbl[PFSPIDHASH(pp->pfs_pid)];
    474      1.29      fvdl 	LIST_INSERT_HEAD(ppp, pp, pfs_hash);
    475      1.29      fvdl 	simple_unlock(&pfs_hash_slock);
    476      1.29      fvdl }
    477      1.29      fvdl 
    478      1.29      fvdl /*
    479      1.29      fvdl  * Remove the pfsnode from the hash table.
    480      1.29      fvdl  */
    481      1.29      fvdl void
    482      1.29      fvdl procfs_hashrem(pp)
    483      1.29      fvdl 	struct pfsnode *pp;
    484      1.29      fvdl {
    485      1.29      fvdl 	simple_lock(&pfs_hash_slock);
    486      1.29      fvdl 	LIST_REMOVE(pp, pfs_hash);
    487      1.29      fvdl 	simple_unlock(&pfs_hash_slock);
    488      1.29      fvdl }
    489      1.29      fvdl 
    490      1.29      fvdl void
    491      1.29      fvdl procfs_revoke_vnodes(p, arg)
    492      1.29      fvdl 	struct proc *p;
    493      1.29      fvdl 	void *arg;
    494      1.29      fvdl {
    495      1.29      fvdl 	struct pfsnode *pfs, *pnext;
    496      1.29      fvdl 	struct vnode *vp;
    497      1.29      fvdl 	struct mount *mp = (struct mount *)arg;
    498  1.36.2.3   nathanw 	struct pfs_hashhead *ppp;
    499      1.29      fvdl 
    500      1.29      fvdl 	if (!(p->p_flag & P_SUGID))
    501      1.29      fvdl 		return;
    502      1.29      fvdl 
    503  1.36.2.3   nathanw 	ppp = &pfs_hashtbl[PFSPIDHASH(p->p_pid)];
    504  1.36.2.3   nathanw 	for (pfs = LIST_FIRST(ppp); pfs; pfs = pnext) {
    505      1.29      fvdl 		vp = PFSTOV(pfs);
    506  1.36.2.3   nathanw 		pnext = LIST_NEXT(pfs, pfs_hash);
    507      1.29      fvdl 		if (vp->v_usecount > 0 && pfs->pfs_pid == p->p_pid &&
    508      1.29      fvdl 		    vp->v_mount == mp)
    509      1.29      fvdl 			VOP_REVOKE(vp, REVOKEALL);
    510      1.29      fvdl 	}
    511  1.36.2.9   thorpej }
    512  1.36.2.9   thorpej 
    513  1.36.2.9   thorpej int
    514  1.36.2.9   thorpej procfs_getfp(pfs, fp)
    515  1.36.2.9   thorpej 	struct pfsnode *pfs;
    516  1.36.2.9   thorpej 	struct file **fp;
    517  1.36.2.9   thorpej {
    518  1.36.2.9   thorpej 	struct proc *p = PFIND(pfs->pfs_pid);
    519  1.36.2.9   thorpej 
    520  1.36.2.9   thorpej 	if (p == NULL)
    521  1.36.2.9   thorpej 		return ESRCH;
    522  1.36.2.9   thorpej 
    523  1.36.2.9   thorpej 	if (pfs->pfs_fd == -1)
    524  1.36.2.9   thorpej 		return EINVAL;
    525  1.36.2.9   thorpej 
    526  1.36.2.9   thorpej 	if ((*fp = p->p_fd->fd_ofiles[pfs->pfs_fd]) == NULL)
    527  1.36.2.9   thorpej 		return EBADF;
    528  1.36.2.9   thorpej 	return 0;
    529       1.1        pk }
    530