Home | History | Annotate | Line # | Download | only in procfs
procfs_subr.c revision 1.49
      1  1.49  jdolecek /*	$NetBSD: procfs_subr.c,v 1.49 2003/04/17 19:04:25 jdolecek 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.39     lukem 
     43  1.39     lukem #include <sys/cdefs.h>
     44  1.49  jdolecek __KERNEL_RCSID(0, "$NetBSD: procfs_subr.c,v 1.49 2003/04/17 19:04:25 jdolecek 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.42  christos #include <sys/file.h>
     55  1.42  christos #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.42  christos 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.38       chs #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.42  christos procfs_allocvp(mp, vpp, pid, pfs_type, fd)
    100   1.5       cgd 	struct mount *mp;
    101   1.5       cgd 	struct vnode **vpp;
    102  1.42  christos 	pid_t pid;
    103   1.5       cgd 	pfstype pfs_type;
    104  1.42  christos 	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.42  christos 		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.44  jdolecek 	if ((error = getnewvnode(VT_PROCFS, mp, procfs_vnodeop_p, &vp)) != 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.5       cgd 
    121  1.11   mycroft 	MALLOC(pfs, void *, sizeof(struct pfsnode), M_TEMP, M_WAITOK);
    122  1.11   mycroft 	vp->v_data = pfs;
    123   1.5       cgd 
    124  1.42  christos 	pfs->pfs_pid = pid;
    125   1.5       cgd 	pfs->pfs_type = pfs_type;
    126  1.11   mycroft 	pfs->pfs_vnode = vp;
    127   1.5       cgd 	pfs->pfs_flags = 0;
    128  1.42  christos 	pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type, fd);
    129  1.42  christos 	pfs->pfs_fd = fd;
    130   1.5       cgd 
    131   1.5       cgd 	switch (pfs_type) {
    132  1.11   mycroft 	case Proot:	/* /proc = dr-xr-xr-x */
    133  1.17   mycroft 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    134  1.11   mycroft 		vp->v_type = VDIR;
    135  1.11   mycroft 		vp->v_flag = VROOT;
    136  1.11   mycroft 		break;
    137  1.11   mycroft 
    138  1.22   mycroft 	case Pcurproc:	/* /proc/curproc = lr-xr-xr-x */
    139  1.28   thorpej 	case Pself:	/* /proc/self    = lr-xr-xr-x */
    140  1.22   mycroft 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    141  1.11   mycroft 		vp->v_type = VLNK;
    142   1.5       cgd 		break;
    143   1.5       cgd 
    144  1.17   mycroft 	case Pproc:	/* /proc/N = dr-xr-xr-x */
    145  1.42  christos 	case Pfd:
    146  1.42  christos 		if (fd == -1) {	/* /proc/N/fd = dr-xr-xr-x */
    147  1.42  christos 			pfs->pfs_mode =
    148  1.42  christos 			    S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    149  1.42  christos 			vp->v_type = VDIR;
    150  1.42  christos 		} else {	/* /proc/N/fd/M = [ps-]rw------- */
    151  1.42  christos 			struct file *fp;
    152  1.42  christos 			struct vnode *vxp;
    153  1.49  jdolecek 			struct proc *pown;
    154  1.44  jdolecek 
    155  1.44  jdolecek 			/* XXX can procfs_getfp() ever fail here? */
    156  1.49  jdolecek 			if ((error = procfs_getfp(pfs, &pown, &fp)) != 0)
    157  1.44  jdolecek 				goto bad;
    158  1.49  jdolecek 			FILE_USE(fp);
    159  1.44  jdolecek 
    160  1.42  christos 			pfs->pfs_mode = S_IRUSR|S_IWUSR;
    161  1.42  christos 			switch (fp->f_type) {
    162  1.42  christos 			case DTYPE_VNODE:
    163  1.42  christos 				vxp = (struct vnode *)fp->f_data;
    164  1.42  christos 				vp->v_type = vxp->v_type;
    165  1.42  christos 				break;
    166  1.42  christos 			case DTYPE_PIPE:
    167  1.42  christos 				vp->v_type = VFIFO;
    168  1.42  christos 				break;
    169  1.42  christos 			case DTYPE_SOCKET:
    170  1.42  christos 				vp->v_type = VSOCK;
    171  1.42  christos 				break;
    172  1.45  jdolecek 			default:
    173  1.44  jdolecek 				error = EOPNOTSUPP;
    174  1.49  jdolecek 				FILE_UNUSE(fp, pown);
    175  1.44  jdolecek 				goto bad;
    176  1.42  christos 			}
    177  1.49  jdolecek 			FILE_UNUSE(fp, pown);
    178  1.42  christos 		}
    179   1.5       cgd 		break;
    180   1.5       cgd 
    181  1.17   mycroft 	case Pfile:	/* /proc/N/file = -rw------- */
    182  1.17   mycroft 	case Pmem:	/* /proc/N/mem = -rw------- */
    183  1.17   mycroft 	case Pregs:	/* /proc/N/regs = -rw------- */
    184  1.17   mycroft 	case Pfpregs:	/* /proc/N/fpregs = -rw------- */
    185  1.17   mycroft 		pfs->pfs_mode = S_IRUSR|S_IWUSR;
    186   1.9       cgd 		vp->v_type = VREG;
    187   1.9       cgd 		break;
    188   1.9       cgd 
    189  1.17   mycroft 	case Pctl:	/* /proc/N/ctl = --w------ */
    190  1.17   mycroft 	case Pnote:	/* /proc/N/note = --w------ */
    191  1.17   mycroft 	case Pnotepg:	/* /proc/N/notepg = --w------ */
    192  1.17   mycroft 		pfs->pfs_mode = S_IWUSR;
    193   1.6        ws 		vp->v_type = VREG;
    194   1.5       cgd 		break;
    195   1.5       cgd 
    196  1.25   msaitoh 	case Pmap:	/* /proc/N/map = -r--r--r-- */
    197  1.37      fvdl 	case Pmaps:	/* /proc/N/maps = -r--r--r-- */
    198  1.17   mycroft 	case Pstatus:	/* /proc/N/status = -r--r--r-- */
    199  1.26  christos 	case Pcmdline:	/* /proc/N/cmdline = -r--r--r-- */
    200  1.35      fvdl 	case Pmeminfo:	/* /proc/meminfo = -r--r--r-- */
    201  1.35      fvdl 	case Pcpuinfo:	/* /proc/cpuinfo = -r--r--r-- */
    202  1.47      tron 	case Puptime:	/* /proc/uptime = -r--r--r-- */
    203  1.17   mycroft 		pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH;
    204   1.6        ws 		vp->v_type = VREG;
    205   1.5       cgd 		break;
    206   1.5       cgd 
    207  1.40   thorpej #ifdef __HAVE_PROCFS_MACHDEP
    208  1.40   thorpej 	PROCFS_MACHDEP_NODETYPE_CASES
    209  1.40   thorpej 		procfs_machdep_allocvp(vp);
    210  1.40   thorpej 		break;
    211  1.40   thorpej #endif
    212  1.40   thorpej 
    213   1.5       cgd 	default:
    214  1.11   mycroft 		panic("procfs_allocvp");
    215   1.5       cgd 	}
    216  1.27  wrstuden 
    217  1.29      fvdl 	procfs_hashins(pfs);
    218  1.34       chs 	uvm_vnp_setsize(vp, 0);
    219  1.33       chs 	lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
    220   1.1        pk 
    221  1.44  jdolecek 	*vpp = vp;
    222  1.44  jdolecek 	return (0);
    223  1.44  jdolecek 
    224  1.44  jdolecek  bad:
    225  1.48     enami 	lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
    226  1.44  jdolecek 	FREE(pfs, M_TEMP);
    227  1.44  jdolecek 	ungetnewvnode(vp);
    228   1.5       cgd 	return (error);
    229   1.1        pk }
    230   1.1        pk 
    231  1.11   mycroft int
    232   1.5       cgd procfs_freevp(vp)
    233   1.5       cgd 	struct vnode *vp;
    234   1.1        pk {
    235   1.5       cgd 	struct pfsnode *pfs = VTOPFS(vp);
    236   1.5       cgd 
    237  1.29      fvdl 	procfs_hashrem(pfs);
    238   1.1        pk 
    239  1.11   mycroft 	FREE(vp->v_data, M_TEMP);
    240  1.11   mycroft 	vp->v_data = 0;
    241   1.5       cgd 	return (0);
    242   1.1        pk }
    243   1.1        pk 
    244  1.11   mycroft int
    245  1.15  christos procfs_rw(v)
    246  1.15  christos 	void *v;
    247   1.1        pk {
    248  1.15  christos 	struct vop_read_args *ap = v;
    249  1.11   mycroft 	struct vnode *vp = ap->a_vp;
    250  1.11   mycroft 	struct uio *uio = ap->a_uio;
    251   1.5       cgd 	struct proc *curp = uio->uio_procp;
    252   1.5       cgd 	struct pfsnode *pfs = VTOPFS(vp);
    253  1.43   thorpej 	struct lwp *l;
    254   1.5       cgd 	struct proc *p;
    255   1.5       cgd 
    256   1.5       cgd 	p = PFIND(pfs->pfs_pid);
    257   1.5       cgd 	if (p == 0)
    258   1.1        pk 		return (EINVAL);
    259  1.19   mycroft 
    260  1.43   thorpej 	/* XXX NJWLWP
    261  1.43   thorpej 	 * The entire procfs interface needs work to be useful to
    262  1.43   thorpej 	 * a process with multiple LWPs. For the moment, we'll
    263  1.43   thorpej 	 * just kluge this and fail on others.
    264  1.43   thorpej 	 */
    265  1.43   thorpej 	l = proc_representative_lwp(p);
    266  1.43   thorpej 
    267  1.19   mycroft 	switch (pfs->pfs_type) {
    268  1.19   mycroft 	case Pregs:
    269  1.19   mycroft 	case Pfpregs:
    270  1.19   mycroft 	case Pmem:
    271  1.40   thorpej #if defined(__HAVE_PROCFS_MACHDEP) && defined(PROCFS_MACHDEP_PROTECT_CASES)
    272  1.40   thorpej 	PROCFS_MACHDEP_PROTECT_CASES
    273  1.40   thorpej #endif
    274  1.19   mycroft 		/*
    275  1.19   mycroft 		 * Do not allow init to be modified while in secure mode; it
    276  1.19   mycroft 		 * could be duped into changing the security level.
    277  1.19   mycroft 		 */
    278  1.19   mycroft 		if (uio->uio_rw == UIO_WRITE &&
    279  1.19   mycroft 		    p == initproc && securelevel > -1)
    280  1.19   mycroft 			return (EPERM);
    281  1.19   mycroft 		break;
    282  1.19   mycroft 
    283  1.19   mycroft 	default:
    284  1.19   mycroft 		break;
    285  1.19   mycroft 	}
    286   1.1        pk 
    287   1.5       cgd 	switch (pfs->pfs_type) {
    288   1.5       cgd 	case Pnote:
    289   1.5       cgd 	case Pnotepg:
    290  1.11   mycroft 		return (procfs_donote(curp, p, pfs, uio));
    291   1.5       cgd 
    292   1.5       cgd 	case Pregs:
    293  1.43   thorpej 		return (procfs_doregs(curp, l, pfs, uio));
    294   1.9       cgd 
    295   1.9       cgd 	case Pfpregs:
    296  1.43   thorpej 		return (procfs_dofpregs(curp, l, pfs, uio));
    297   1.5       cgd 
    298   1.5       cgd 	case Pctl:
    299  1.43   thorpej 		return (procfs_doctl(curp, l, pfs, uio));
    300   1.5       cgd 
    301   1.5       cgd 	case Pstatus:
    302  1.43   thorpej 		return (procfs_dostatus(curp, l, pfs, uio));
    303  1.25   msaitoh 
    304  1.25   msaitoh 	case Pmap:
    305  1.37      fvdl 		return (procfs_domap(curp, p, pfs, uio, 0));
    306  1.37      fvdl 
    307  1.37      fvdl 	case Pmaps:
    308  1.37      fvdl 		return (procfs_domap(curp, p, pfs, uio, 1));
    309   1.1        pk 
    310   1.5       cgd 	case Pmem:
    311  1.11   mycroft 		return (procfs_domem(curp, p, pfs, uio));
    312  1.26  christos 
    313  1.26  christos 	case Pcmdline:
    314  1.26  christos 		return (procfs_docmdline(curp, p, pfs, uio));
    315  1.35      fvdl 
    316  1.35      fvdl 	case Pmeminfo:
    317  1.35      fvdl 		return (procfs_domeminfo(curp, p, pfs, uio));
    318  1.40   thorpej 
    319  1.35      fvdl 	case Pcpuinfo:
    320  1.35      fvdl 		return (procfs_docpuinfo(curp, p, pfs, uio));
    321  1.40   thorpej 
    322  1.42  christos 	case Pfd:
    323  1.42  christos 		return (procfs_dofd(curp, p, pfs, uio));
    324  1.46       jrf 
    325  1.46       jrf 	case Puptime:
    326  1.46       jrf 		return (procfs_douptime(curp, p, pfs, uio));
    327  1.42  christos 
    328  1.40   thorpej #ifdef __HAVE_PROCFS_MACHDEP
    329  1.40   thorpej 	PROCFS_MACHDEP_NODETYPE_CASES
    330  1.43   thorpej 		return (procfs_machdep_rw(curp, l, pfs, uio));
    331  1.40   thorpej #endif
    332   1.1        pk 
    333   1.5       cgd 	default:
    334   1.5       cgd 		return (EOPNOTSUPP);
    335   1.5       cgd 	}
    336   1.1        pk }
    337   1.1        pk 
    338   1.5       cgd /*
    339   1.5       cgd  * Get a string from userland into (buf).  Strip a trailing
    340   1.5       cgd  * nl character (to allow easy access from the shell).
    341  1.11   mycroft  * The buffer should be *buflenp + 1 chars long.  vfs_getuserstr
    342   1.5       cgd  * will automatically add a nul char at the end.
    343   1.5       cgd  *
    344   1.5       cgd  * Returns 0 on success or the following errors
    345   1.5       cgd  *
    346   1.5       cgd  * EINVAL:    file offset is non-zero.
    347   1.5       cgd  * EMSGSIZE:  message is longer than kernel buffer
    348   1.5       cgd  * EFAULT:    user i/o buffer is not addressable
    349   1.5       cgd  */
    350  1.11   mycroft int
    351  1.11   mycroft vfs_getuserstr(uio, buf, buflenp)
    352   1.5       cgd 	struct uio *uio;
    353   1.5       cgd 	char *buf;
    354   1.5       cgd 	int *buflenp;
    355   1.1        pk {
    356   1.5       cgd 	int xlen;
    357   1.5       cgd 	int error;
    358   1.5       cgd 
    359  1.11   mycroft 	if (uio->uio_offset != 0)
    360  1.11   mycroft 		return (EINVAL);
    361  1.11   mycroft 
    362   1.5       cgd 	xlen = *buflenp;
    363   1.1        pk 
    364   1.5       cgd 	/* must be able to read the whole string in one go */
    365   1.5       cgd 	if (xlen < uio->uio_resid)
    366   1.5       cgd 		return (EMSGSIZE);
    367   1.5       cgd 	xlen = uio->uio_resid;
    368   1.5       cgd 
    369  1.14  christos 	if ((error = uiomove(buf, xlen, uio)) != 0)
    370   1.5       cgd 		return (error);
    371   1.5       cgd 
    372  1.11   mycroft 	/* allow multiple writes without seeks */
    373  1.11   mycroft 	uio->uio_offset = 0;
    374  1.11   mycroft 
    375   1.5       cgd 	/* cleanup string and remove trailing newline */
    376   1.5       cgd 	buf[xlen] = '\0';
    377   1.5       cgd 	xlen = strlen(buf);
    378   1.5       cgd 	if (xlen > 0 && buf[xlen-1] == '\n')
    379   1.5       cgd 		buf[--xlen] = '\0';
    380   1.5       cgd 	*buflenp = xlen;
    381   1.1        pk 
    382   1.5       cgd 	return (0);
    383   1.1        pk }
    384   1.1        pk 
    385  1.36  jdolecek const vfs_namemap_t *
    386  1.11   mycroft vfs_findname(nm, buf, buflen)
    387  1.36  jdolecek 	const vfs_namemap_t *nm;
    388  1.36  jdolecek 	const char *buf;
    389   1.5       cgd 	int buflen;
    390   1.1        pk {
    391  1.11   mycroft 
    392   1.5       cgd 	for (; nm->nm_name; nm++)
    393  1.24     perry 		if (memcmp(buf, nm->nm_name, buflen+1) == 0)
    394   1.5       cgd 			return (nm);
    395   1.5       cgd 
    396   1.5       cgd 	return (0);
    397  1.29      fvdl }
    398  1.29      fvdl 
    399  1.29      fvdl /*
    400  1.29      fvdl  * Initialize pfsnode hash table.
    401  1.29      fvdl  */
    402  1.29      fvdl void
    403  1.29      fvdl procfs_hashinit()
    404  1.29      fvdl {
    405  1.29      fvdl 	lockinit(&pfs_hashlock, PINOD, "pfs_hashlock", 0, 0);
    406  1.32        ad 	pfs_hashtbl = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT,
    407  1.32        ad 	    M_WAITOK, &pfs_ihash);
    408  1.29      fvdl 	simple_lock_init(&pfs_hash_slock);
    409  1.31  jdolecek }
    410  1.31  jdolecek 
    411  1.38       chs void
    412  1.38       chs procfs_hashreinit()
    413  1.38       chs {
    414  1.38       chs 	struct pfsnode *pp;
    415  1.38       chs 	struct pfs_hashhead *oldhash, *hash;
    416  1.41   thorpej 	u_long i, oldmask, mask, val;
    417  1.38       chs 
    418  1.38       chs 	hash = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT, M_WAITOK,
    419  1.38       chs 	    &mask);
    420  1.38       chs 
    421  1.38       chs 	simple_lock(&pfs_hash_slock);
    422  1.38       chs 	oldhash = pfs_hashtbl;
    423  1.38       chs 	oldmask = pfs_ihash;
    424  1.38       chs 	pfs_hashtbl = hash;
    425  1.38       chs 	pfs_ihash = mask;
    426  1.38       chs 	for (i = 0; i <= oldmask; i++) {
    427  1.38       chs 		while ((pp = LIST_FIRST(&oldhash[i])) != NULL) {
    428  1.38       chs 			LIST_REMOVE(pp, pfs_hash);
    429  1.38       chs 			val = PFSPIDHASH(pp->pfs_pid);
    430  1.38       chs 			LIST_INSERT_HEAD(&hash[val], pp, pfs_hash);
    431  1.38       chs 		}
    432  1.38       chs 	}
    433  1.38       chs 	simple_unlock(&pfs_hash_slock);
    434  1.38       chs 	hashdone(oldhash, M_UFSMNT);
    435  1.38       chs }
    436  1.38       chs 
    437  1.31  jdolecek /*
    438  1.31  jdolecek  * Free pfsnode hash table.
    439  1.31  jdolecek  */
    440  1.31  jdolecek void
    441  1.31  jdolecek procfs_hashdone()
    442  1.31  jdolecek {
    443  1.31  jdolecek 	hashdone(pfs_hashtbl, M_UFSMNT);
    444  1.29      fvdl }
    445  1.29      fvdl 
    446  1.29      fvdl struct vnode *
    447  1.42  christos procfs_hashget(pid, type, fd, mp)
    448  1.29      fvdl 	pid_t pid;
    449  1.29      fvdl 	pfstype type;
    450  1.42  christos 	int fd;
    451  1.29      fvdl 	struct mount *mp;
    452  1.29      fvdl {
    453  1.38       chs 	struct pfs_hashhead *ppp;
    454  1.29      fvdl 	struct pfsnode *pp;
    455  1.29      fvdl 	struct vnode *vp;
    456  1.29      fvdl 
    457  1.29      fvdl loop:
    458  1.29      fvdl 	simple_lock(&pfs_hash_slock);
    459  1.38       chs 	ppp = &pfs_hashtbl[PFSPIDHASH(pid)];
    460  1.38       chs 	LIST_FOREACH(pp, ppp, pfs_hash) {
    461  1.29      fvdl 		vp = PFSTOV(pp);
    462  1.29      fvdl 		if (pid == pp->pfs_pid && pp->pfs_type == type &&
    463  1.42  christos 		    pp->pfs_fd == fd && vp->v_mount == mp) {
    464  1.29      fvdl 			simple_lock(&vp->v_interlock);
    465  1.29      fvdl 			simple_unlock(&pfs_hash_slock);
    466  1.29      fvdl 			if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
    467  1.29      fvdl 				goto loop;
    468  1.29      fvdl 			return (vp);
    469  1.29      fvdl 		}
    470  1.29      fvdl 	}
    471  1.29      fvdl 	simple_unlock(&pfs_hash_slock);
    472  1.29      fvdl 	return (NULL);
    473  1.29      fvdl }
    474  1.29      fvdl 
    475  1.29      fvdl /*
    476  1.29      fvdl  * Insert the pfsnode into the hash table and lock it.
    477  1.29      fvdl  */
    478  1.29      fvdl void
    479  1.29      fvdl procfs_hashins(pp)
    480  1.29      fvdl 	struct pfsnode *pp;
    481  1.29      fvdl {
    482  1.29      fvdl 	struct pfs_hashhead *ppp;
    483  1.29      fvdl 
    484  1.29      fvdl 	/* lock the pfsnode, then put it on the appropriate hash list */
    485  1.29      fvdl 	lockmgr(&pp->pfs_vnode->v_lock, LK_EXCLUSIVE, (struct simplelock *)0);
    486  1.29      fvdl 
    487  1.29      fvdl 	simple_lock(&pfs_hash_slock);
    488  1.38       chs 	ppp = &pfs_hashtbl[PFSPIDHASH(pp->pfs_pid)];
    489  1.29      fvdl 	LIST_INSERT_HEAD(ppp, pp, pfs_hash);
    490  1.29      fvdl 	simple_unlock(&pfs_hash_slock);
    491  1.29      fvdl }
    492  1.29      fvdl 
    493  1.29      fvdl /*
    494  1.29      fvdl  * Remove the pfsnode from the hash table.
    495  1.29      fvdl  */
    496  1.29      fvdl void
    497  1.29      fvdl procfs_hashrem(pp)
    498  1.29      fvdl 	struct pfsnode *pp;
    499  1.29      fvdl {
    500  1.29      fvdl 	simple_lock(&pfs_hash_slock);
    501  1.29      fvdl 	LIST_REMOVE(pp, pfs_hash);
    502  1.29      fvdl 	simple_unlock(&pfs_hash_slock);
    503  1.29      fvdl }
    504  1.29      fvdl 
    505  1.29      fvdl void
    506  1.29      fvdl procfs_revoke_vnodes(p, arg)
    507  1.29      fvdl 	struct proc *p;
    508  1.29      fvdl 	void *arg;
    509  1.29      fvdl {
    510  1.29      fvdl 	struct pfsnode *pfs, *pnext;
    511  1.29      fvdl 	struct vnode *vp;
    512  1.29      fvdl 	struct mount *mp = (struct mount *)arg;
    513  1.38       chs 	struct pfs_hashhead *ppp;
    514  1.29      fvdl 
    515  1.29      fvdl 	if (!(p->p_flag & P_SUGID))
    516  1.29      fvdl 		return;
    517  1.29      fvdl 
    518  1.38       chs 	ppp = &pfs_hashtbl[PFSPIDHASH(p->p_pid)];
    519  1.38       chs 	for (pfs = LIST_FIRST(ppp); pfs; pfs = pnext) {
    520  1.29      fvdl 		vp = PFSTOV(pfs);
    521  1.38       chs 		pnext = LIST_NEXT(pfs, pfs_hash);
    522  1.29      fvdl 		if (vp->v_usecount > 0 && pfs->pfs_pid == p->p_pid &&
    523  1.29      fvdl 		    vp->v_mount == mp)
    524  1.29      fvdl 			VOP_REVOKE(vp, REVOKEALL);
    525  1.29      fvdl 	}
    526  1.42  christos }
    527  1.42  christos 
    528  1.42  christos int
    529  1.49  jdolecek procfs_getfp(pfs, pown, fp)
    530  1.42  christos 	struct pfsnode *pfs;
    531  1.49  jdolecek 	struct proc **pown;
    532  1.42  christos 	struct file **fp;
    533  1.42  christos {
    534  1.42  christos 	struct proc *p = PFIND(pfs->pfs_pid);
    535  1.42  christos 
    536  1.42  christos 	if (p == NULL)
    537  1.42  christos 		return ESRCH;
    538  1.42  christos 
    539  1.42  christos 	if (pfs->pfs_fd == -1)
    540  1.42  christos 		return EINVAL;
    541  1.42  christos 
    542  1.49  jdolecek 	if ((*fp = fd_getfile(p->p_fd, pfs->pfs_fd)) == NULL)
    543  1.42  christos 		return EBADF;
    544  1.49  jdolecek 
    545  1.49  jdolecek 	*pown = p;
    546  1.42  christos 	return 0;
    547   1.1        pk }
    548