Home | History | Annotate | Line # | Download | only in ptyfs
ptyfs_subr.c revision 1.22.6.1
      1  1.22.6.1    cherry /*	$NetBSD: ptyfs_subr.c,v 1.22.6.1 2011/06/23 14:20:15 cherry Exp $	*/
      2       1.1  jdolecek 
      3       1.1  jdolecek /*
      4       1.1  jdolecek  * Copyright (c) 1993
      5       1.1  jdolecek  *	The Regents of the University of California.  All rights reserved.
      6       1.1  jdolecek  *
      7       1.1  jdolecek  * This code is derived from software contributed to Berkeley by
      8       1.1  jdolecek  * Jan-Simon Pendry.
      9       1.1  jdolecek  *
     10       1.1  jdolecek  * Redistribution and use in source and binary forms, with or without
     11       1.1  jdolecek  * modification, are permitted provided that the following conditions
     12       1.1  jdolecek  * are met:
     13       1.1  jdolecek  * 1. Redistributions of source code must retain the above copyright
     14       1.1  jdolecek  *    notice, this list of conditions and the following disclaimer.
     15       1.1  jdolecek  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  jdolecek  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  jdolecek  *    documentation and/or other materials provided with the distribution.
     18       1.1  jdolecek  * 3. Neither the name of the University nor the names of its contributors
     19       1.1  jdolecek  *    may be used to endorse or promote products derived from this software
     20       1.1  jdolecek  *    without specific prior written permission.
     21       1.1  jdolecek  *
     22       1.1  jdolecek  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23       1.1  jdolecek  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24       1.1  jdolecek  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25       1.1  jdolecek  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26       1.1  jdolecek  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27       1.1  jdolecek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28       1.1  jdolecek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29       1.1  jdolecek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30       1.1  jdolecek  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31       1.1  jdolecek  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32       1.1  jdolecek  * SUCH DAMAGE.
     33       1.1  jdolecek  *
     34       1.1  jdolecek  *	@(#)ptyfs_subr.c	8.6 (Berkeley) 5/14/95
     35       1.1  jdolecek  */
     36       1.1  jdolecek 
     37       1.1  jdolecek /*
     38       1.1  jdolecek  * Copyright (c) 1994 Christopher G. Demetriou.  All rights reserved.
     39       1.1  jdolecek  * Copyright (c) 1993 Jan-Simon Pendry
     40       1.1  jdolecek  *
     41       1.1  jdolecek  * This code is derived from software contributed to Berkeley by
     42       1.1  jdolecek  * Jan-Simon Pendry.
     43       1.1  jdolecek  *
     44       1.1  jdolecek  * Redistribution and use in source and binary forms, with or without
     45       1.1  jdolecek  * modification, are permitted provided that the following conditions
     46       1.1  jdolecek  * are met:
     47       1.1  jdolecek  * 1. Redistributions of source code must retain the above copyright
     48       1.1  jdolecek  *    notice, this list of conditions and the following disclaimer.
     49       1.1  jdolecek  * 2. Redistributions in binary form must reproduce the above copyright
     50       1.1  jdolecek  *    notice, this list of conditions and the following disclaimer in the
     51       1.1  jdolecek  *    documentation and/or other materials provided with the distribution.
     52       1.1  jdolecek  * 3. All advertising materials mentioning features or use of this software
     53       1.1  jdolecek  *    must display the following acknowledgement:
     54       1.1  jdolecek  *	This product includes software developed by the University of
     55       1.1  jdolecek  *	California, Berkeley and its contributors.
     56       1.1  jdolecek  * 4. Neither the name of the University nor the names of its contributors
     57       1.1  jdolecek  *    may be used to endorse or promote products derived from this software
     58       1.1  jdolecek  *    without specific prior written permission.
     59       1.1  jdolecek  *
     60       1.1  jdolecek  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61       1.1  jdolecek  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62       1.1  jdolecek  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63       1.1  jdolecek  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64       1.1  jdolecek  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65       1.1  jdolecek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66       1.1  jdolecek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67       1.1  jdolecek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68       1.1  jdolecek  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69       1.1  jdolecek  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70       1.1  jdolecek  * SUCH DAMAGE.
     71       1.1  jdolecek  *
     72       1.1  jdolecek  *	@(#)procfs_subr.c	8.6 (Berkeley) 5/14/95
     73       1.1  jdolecek  */
     74       1.1  jdolecek 
     75       1.1  jdolecek #include <sys/cdefs.h>
     76  1.22.6.1    cherry __KERNEL_RCSID(0, "$NetBSD: ptyfs_subr.c,v 1.22.6.1 2011/06/23 14:20:15 cherry Exp $");
     77       1.1  jdolecek 
     78       1.1  jdolecek #include <sys/param.h>
     79       1.1  jdolecek #include <sys/systm.h>
     80       1.1  jdolecek #include <sys/time.h>
     81       1.1  jdolecek #include <sys/kernel.h>
     82       1.1  jdolecek #include <sys/vnode.h>
     83      1.16        ad #include <sys/stat.h>
     84       1.1  jdolecek #include <sys/malloc.h>
     85       1.1  jdolecek #include <sys/file.h>
     86       1.1  jdolecek #include <sys/namei.h>
     87       1.1  jdolecek #include <sys/filedesc.h>
     88       1.1  jdolecek #include <sys/select.h>
     89       1.1  jdolecek #include <sys/tty.h>
     90       1.1  jdolecek #include <sys/pty.h>
     91       1.7      elad #include <sys/kauth.h>
     92       1.8        ad #include <sys/lwp.h>
     93       1.1  jdolecek 
     94       1.2  jdolecek #include <fs/ptyfs/ptyfs.h>
     95       1.1  jdolecek #include <miscfs/specfs/specdev.h>
     96       1.1  jdolecek 
     97      1.14        ad static kmutex_t ptyfs_hashlock;
     98       1.1  jdolecek 
     99       1.1  jdolecek static LIST_HEAD(ptyfs_hashhead, ptyfsnode) *ptyfs_used_tbl, *ptyfs_free_tbl;
    100       1.1  jdolecek static u_long ptyfs_used_mask, ptyfs_free_mask; /* size of hash table - 1 */
    101       1.9        ad static kmutex_t ptyfs_used_slock, ptyfs_free_slock;
    102       1.1  jdolecek 
    103       1.5  christos static void ptyfs_getinfo(struct ptyfsnode *, struct lwp *);
    104       1.1  jdolecek 
    105       1.1  jdolecek static void ptyfs_hashins(struct ptyfsnode *);
    106       1.1  jdolecek static void ptyfs_hashrem(struct ptyfsnode *);
    107       1.1  jdolecek 
    108      1.14        ad static struct vnode *ptyfs_used_get(ptyfstype, int, struct mount *, int);
    109       1.5  christos static struct ptyfsnode *ptyfs_free_get(ptyfstype, int, struct lwp *);
    110       1.1  jdolecek 
    111       1.9        ad static void ptyfs_rehash(kmutex_t *, struct ptyfs_hashhead **,
    112       1.1  jdolecek     u_long *);
    113       1.1  jdolecek 
    114       1.1  jdolecek #define PTYHASH(type, pty, mask) (PTYFS_FILENO(type, pty) % (mask + 1))
    115       1.1  jdolecek 
    116       1.1  jdolecek 
    117       1.1  jdolecek static void
    118       1.5  christos ptyfs_getinfo(struct ptyfsnode *ptyfs, struct lwp *l)
    119       1.1  jdolecek {
    120       1.1  jdolecek 	extern struct ptm_pty *ptyfs_save_ptm, ptm_ptyfspty;
    121       1.1  jdolecek 
    122       1.1  jdolecek 	if (ptyfs->ptyfs_type == PTYFSroot) {
    123       1.1  jdolecek 		ptyfs->ptyfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|
    124       1.1  jdolecek 		    S_IROTH|S_IXOTH;
    125       1.1  jdolecek 		goto out;
    126       1.1  jdolecek 	} else
    127       1.1  jdolecek 		ptyfs->ptyfs_mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|
    128       1.1  jdolecek 		    S_IROTH|S_IWOTH;
    129       1.1  jdolecek 
    130       1.1  jdolecek 	if (ptyfs_save_ptm != NULL && ptyfs_save_ptm != &ptm_ptyfspty) {
    131       1.1  jdolecek 		int error;
    132      1.22  dholland 		struct pathbuf *pb;
    133       1.1  jdolecek 		struct nameidata nd;
    134       1.1  jdolecek 		char ttyname[64];
    135       1.7      elad 		kauth_cred_t cred;
    136       1.1  jdolecek 		struct vattr va;
    137      1.22  dholland 
    138       1.1  jdolecek 		/*
    139       1.1  jdolecek 		 * We support traditional ptys, so we copy the info
    140       1.1  jdolecek 		 * from the inode
    141       1.1  jdolecek 		 */
    142       1.1  jdolecek 		if ((error = (*ptyfs_save_ptm->makename)(
    143       1.6  christos 			ptyfs_save_ptm, l, ttyname, sizeof(ttyname),
    144       1.1  jdolecek 			ptyfs->ptyfs_pty, ptyfs->ptyfs_type == PTYFSpts ? 't'
    145       1.1  jdolecek 			: 'p')) != 0)
    146       1.1  jdolecek 				goto out;
    147      1.22  dholland 		pb = pathbuf_create(ttyname);
    148      1.22  dholland 		if (pb == NULL) {
    149      1.22  dholland 			error = ENOMEM;
    150      1.22  dholland  			goto out;
    151      1.22  dholland 		}
    152      1.22  dholland 		NDINIT(&nd, LOOKUP, NOFOLLOW|LOCKLEAF, pb);
    153      1.22  dholland 		if ((error = namei(&nd)) != 0) {
    154      1.22  dholland 			pathbuf_destroy(pb);
    155       1.1  jdolecek 			goto out;
    156      1.22  dholland 		}
    157       1.7      elad 		cred = kauth_cred_alloc();
    158      1.10     pooka 		error = VOP_GETATTR(nd.ni_vp, &va, cred);
    159       1.7      elad 		kauth_cred_free(cred);
    160      1.19   hannken 		VOP_UNLOCK(nd.ni_vp);
    161       1.1  jdolecek 		vrele(nd.ni_vp);
    162      1.22  dholland 		pathbuf_destroy(pb);
    163       1.1  jdolecek 		if (error)
    164       1.1  jdolecek 			goto out;
    165       1.1  jdolecek 		ptyfs->ptyfs_uid = va.va_uid;
    166       1.1  jdolecek 		ptyfs->ptyfs_gid = va.va_gid;
    167       1.1  jdolecek 		ptyfs->ptyfs_mode = va.va_mode;
    168       1.1  jdolecek 		ptyfs->ptyfs_flags = va.va_flags;
    169       1.1  jdolecek 		ptyfs->ptyfs_birthtime = va.va_birthtime;
    170       1.1  jdolecek 		ptyfs->ptyfs_ctime = va.va_ctime;
    171       1.1  jdolecek 		ptyfs->ptyfs_mtime = va.va_mtime;
    172       1.1  jdolecek 		ptyfs->ptyfs_atime = va.va_atime;
    173       1.1  jdolecek 		return;
    174       1.1  jdolecek 	}
    175       1.1  jdolecek out:
    176       1.1  jdolecek 	ptyfs->ptyfs_uid = ptyfs->ptyfs_gid = 0;
    177       1.3  christos 	ptyfs->ptyfs_flags |= PTYFS_CHANGE;
    178       1.3  christos 	PTYFS_ITIMES(ptyfs, NULL, NULL, NULL);
    179       1.1  jdolecek 	ptyfs->ptyfs_birthtime = ptyfs->ptyfs_mtime =
    180       1.1  jdolecek 	    ptyfs->ptyfs_atime = ptyfs->ptyfs_ctime;
    181       1.1  jdolecek 	ptyfs->ptyfs_flags = 0;
    182       1.1  jdolecek }
    183       1.1  jdolecek 
    184       1.1  jdolecek 
    185       1.1  jdolecek /*
    186       1.1  jdolecek  * allocate a ptyfsnode/vnode pair.  the vnode is
    187       1.1  jdolecek  * referenced, and locked.
    188       1.1  jdolecek  *
    189       1.1  jdolecek  * the pid, ptyfs_type, and mount point uniquely
    190       1.1  jdolecek  * identify a ptyfsnode.  the mount point is needed
    191       1.1  jdolecek  * because someone might mount this filesystem
    192       1.1  jdolecek  * twice.
    193       1.1  jdolecek  *
    194       1.1  jdolecek  * all ptyfsnodes are maintained on a singly-linked
    195       1.1  jdolecek  * list.  new nodes are only allocated when they cannot
    196       1.1  jdolecek  * be found on this list.  entries on the list are
    197       1.1  jdolecek  * removed when the vfs reclaim entry is called.
    198       1.1  jdolecek  *
    199       1.1  jdolecek  * a single lock is kept for the entire list.  this is
    200       1.1  jdolecek  * needed because the getnewvnode() function can block
    201       1.1  jdolecek  * waiting for a vnode to become free, in which case there
    202       1.1  jdolecek  * may be more than one ptyess trying to get the same
    203       1.1  jdolecek  * vnode.  this lock is only taken if we are going to
    204       1.1  jdolecek  * call getnewvnode, since the kernel itself is single-threaded.
    205       1.1  jdolecek  *
    206       1.1  jdolecek  * if an entry is found on the list, then call vget() to
    207       1.1  jdolecek  * take a reference.  this is done because there may be
    208       1.1  jdolecek  * zero references to it and so it needs to removed from
    209       1.1  jdolecek  * the vnode free list.
    210       1.1  jdolecek  */
    211       1.1  jdolecek int
    212       1.1  jdolecek ptyfs_allocvp(struct mount *mp, struct vnode **vpp, ptyfstype type, int pty,
    213       1.5  christos     struct lwp *l)
    214       1.1  jdolecek {
    215       1.1  jdolecek 	struct ptyfsnode *ptyfs;
    216      1.13        ad 	struct vnode *vp;
    217       1.1  jdolecek 	int error;
    218       1.1  jdolecek 
    219      1.14        ad  retry:
    220      1.14        ad 	if ((*vpp = ptyfs_used_get(type, pty, mp, LK_EXCLUSIVE)) != NULL)
    221      1.14        ad 		return 0;
    222       1.1  jdolecek 
    223  1.22.6.1    cherry 	error = getnewvnode(VT_PTYFS, mp, ptyfs_vnodeop_p, NULL, &vp);
    224  1.22.6.1    cherry 	if (error) {
    225       1.1  jdolecek 		*vpp = NULL;
    226       1.1  jdolecek 		return error;
    227       1.1  jdolecek 	}
    228       1.1  jdolecek 
    229      1.14        ad 	mutex_enter(&ptyfs_hashlock);
    230      1.14        ad 	if (ptyfs_used_get(type, pty, mp, 0) != NULL) {
    231      1.14        ad 		mutex_exit(&ptyfs_hashlock);
    232      1.14        ad 		ungetnewvnode(vp);
    233      1.14        ad 		goto retry;
    234      1.14        ad 	}
    235      1.14        ad 
    236       1.5  christos 	vp->v_data = ptyfs = ptyfs_free_get(type, pty, l);
    237       1.1  jdolecek 	ptyfs->ptyfs_vnode = vp;
    238       1.1  jdolecek 
    239       1.1  jdolecek 	switch (type) {
    240       1.1  jdolecek 	case PTYFSroot:	/* /pts = dr-xr-xr-x */
    241       1.1  jdolecek 		vp->v_type = VDIR;
    242       1.9        ad 		vp->v_vflag = VV_ROOT;
    243       1.1  jdolecek 		break;
    244       1.1  jdolecek 
    245       1.1  jdolecek 	case PTYFSpts:	/* /pts/N = cxxxxxxxxx */
    246       1.1  jdolecek 	case PTYFSptc:	/* controlling side = cxxxxxxxxx */
    247       1.1  jdolecek 		vp->v_type = VCHR;
    248      1.13        ad 		spec_node_init(vp, PTYFS_MAKEDEV(ptyfs));
    249       1.1  jdolecek 		break;
    250       1.1  jdolecek 	default:
    251       1.1  jdolecek 		panic("ptyfs_allocvp");
    252       1.1  jdolecek 	}
    253       1.1  jdolecek 
    254       1.1  jdolecek 	ptyfs_hashins(ptyfs);
    255       1.1  jdolecek 	uvm_vnp_setsize(vp, 0);
    256      1.14        ad 	mutex_exit(&ptyfs_hashlock);
    257       1.1  jdolecek 
    258       1.1  jdolecek 	*vpp = vp;
    259       1.1  jdolecek 	return 0;
    260       1.1  jdolecek }
    261       1.1  jdolecek 
    262       1.1  jdolecek int
    263       1.1  jdolecek ptyfs_freevp(struct vnode *vp)
    264       1.1  jdolecek {
    265       1.1  jdolecek 	struct ptyfsnode *ptyfs = VTOPTYFS(vp);
    266       1.1  jdolecek 
    267       1.1  jdolecek 	ptyfs_hashrem(ptyfs);
    268       1.1  jdolecek 	vp->v_data = NULL;
    269       1.1  jdolecek 	return 0;
    270       1.1  jdolecek }
    271       1.1  jdolecek 
    272       1.1  jdolecek /*
    273       1.1  jdolecek  * Initialize ptyfsnode hash table.
    274       1.1  jdolecek  */
    275       1.1  jdolecek void
    276       1.1  jdolecek ptyfs_hashinit(void)
    277       1.1  jdolecek {
    278      1.16        ad 	ptyfs_used_tbl = hashinit(desiredvnodes / 4, HASH_LIST, true,
    279      1.16        ad 	    &ptyfs_used_mask);
    280      1.16        ad 	ptyfs_free_tbl = hashinit(desiredvnodes / 4, HASH_LIST, true,
    281      1.16        ad 	    &ptyfs_free_mask);
    282      1.14        ad 	mutex_init(&ptyfs_hashlock, MUTEX_DEFAULT, IPL_NONE);
    283       1.9        ad 	mutex_init(&ptyfs_used_slock, MUTEX_DEFAULT, IPL_NONE);
    284       1.9        ad 	mutex_init(&ptyfs_free_slock, MUTEX_DEFAULT, IPL_NONE);
    285       1.1  jdolecek }
    286       1.1  jdolecek 
    287       1.1  jdolecek void
    288       1.1  jdolecek ptyfs_hashreinit(void)
    289       1.1  jdolecek {
    290      1.14        ad 	ptyfs_rehash(&ptyfs_used_slock, &ptyfs_used_tbl, &ptyfs_used_mask);
    291      1.14        ad 	ptyfs_rehash(&ptyfs_free_slock, &ptyfs_free_tbl, &ptyfs_free_mask);
    292       1.1  jdolecek }
    293       1.1  jdolecek 
    294       1.1  jdolecek static void
    295       1.9        ad ptyfs_rehash(kmutex_t *hlock, struct ptyfs_hashhead **hhead,
    296       1.1  jdolecek     u_long *hmask)
    297       1.1  jdolecek {
    298       1.1  jdolecek 	struct ptyfsnode *pp;
    299       1.1  jdolecek 	struct ptyfs_hashhead *oldhash, *hash;
    300       1.1  jdolecek 	u_long i, oldmask, mask, val;
    301       1.1  jdolecek 
    302      1.16        ad 	hash = hashinit(desiredvnodes / 4, HASH_LIST, true, &mask);
    303       1.1  jdolecek 
    304       1.9        ad 	mutex_enter(hlock);
    305       1.1  jdolecek 	oldhash = *hhead;
    306       1.1  jdolecek 	oldmask = *hmask;
    307       1.1  jdolecek 	*hhead = hash;
    308       1.1  jdolecek 	*hmask = mask;
    309       1.1  jdolecek 	for (i = 0; i <= oldmask; i++) {
    310       1.1  jdolecek 		while ((pp = LIST_FIRST(&oldhash[i])) != NULL) {
    311       1.1  jdolecek 			LIST_REMOVE(pp, ptyfs_hash);
    312       1.1  jdolecek 			val = PTYHASH(pp->ptyfs_type, pp->ptyfs_pty,
    313       1.1  jdolecek 			    ptyfs_used_mask);
    314       1.1  jdolecek 			LIST_INSERT_HEAD(&hash[val], pp, ptyfs_hash);
    315       1.1  jdolecek 		}
    316       1.1  jdolecek 	}
    317       1.9        ad 	mutex_exit(hlock);
    318      1.16        ad 	hashdone(oldhash, HASH_LIST, oldmask);
    319       1.1  jdolecek }
    320       1.1  jdolecek 
    321       1.1  jdolecek /*
    322       1.1  jdolecek  * Free ptyfsnode hash table.
    323       1.1  jdolecek  */
    324       1.1  jdolecek void
    325       1.1  jdolecek ptyfs_hashdone(void)
    326       1.1  jdolecek {
    327      1.18      haad 
    328      1.18      haad 	mutex_destroy(&ptyfs_hashlock);
    329      1.18      haad 	mutex_destroy(&ptyfs_used_slock);
    330      1.18      haad 	mutex_destroy(&ptyfs_free_slock);
    331      1.16        ad 	hashdone(ptyfs_used_tbl, HASH_LIST, ptyfs_used_mask);
    332      1.16        ad 	hashdone(ptyfs_free_tbl, HASH_LIST, ptyfs_free_mask);
    333       1.1  jdolecek }
    334       1.1  jdolecek 
    335       1.1  jdolecek /*
    336       1.1  jdolecek  * Get a ptyfsnode from the free table, or allocate one.
    337       1.1  jdolecek  * Removes the node from the free table.
    338       1.1  jdolecek  */
    339       1.1  jdolecek struct ptyfsnode *
    340       1.5  christos ptyfs_free_get(ptyfstype type, int pty, struct lwp *l)
    341       1.1  jdolecek {
    342       1.1  jdolecek 	struct ptyfs_hashhead *ppp;
    343       1.1  jdolecek 	struct ptyfsnode *pp;
    344       1.1  jdolecek 
    345       1.9        ad 	mutex_enter(&ptyfs_free_slock);
    346       1.1  jdolecek 	ppp = &ptyfs_free_tbl[PTYHASH(type, pty, ptyfs_free_mask)];
    347       1.1  jdolecek 	LIST_FOREACH(pp, ppp, ptyfs_hash) {
    348       1.1  jdolecek 		if (pty == pp->ptyfs_pty && pp->ptyfs_type == type) {
    349       1.1  jdolecek 			LIST_REMOVE(pp, ptyfs_hash);
    350       1.9        ad 			mutex_exit(&ptyfs_free_slock);
    351       1.1  jdolecek 			return pp;
    352       1.1  jdolecek 		}
    353       1.1  jdolecek 	}
    354       1.9        ad 	mutex_exit(&ptyfs_free_slock);
    355       1.1  jdolecek 
    356      1.17    cegger 	pp = malloc(sizeof(struct ptyfsnode), M_TEMP, M_WAITOK);
    357       1.1  jdolecek 	pp->ptyfs_pty = pty;
    358       1.1  jdolecek 	pp->ptyfs_type = type;
    359       1.1  jdolecek 	pp->ptyfs_fileno = PTYFS_FILENO(pty, type);
    360       1.5  christos 	ptyfs_getinfo(pp, l);
    361       1.1  jdolecek 	return pp;
    362       1.1  jdolecek }
    363       1.1  jdolecek 
    364       1.1  jdolecek struct vnode *
    365      1.14        ad ptyfs_used_get(ptyfstype type, int pty, struct mount *mp, int flags)
    366       1.1  jdolecek {
    367       1.1  jdolecek 	struct ptyfs_hashhead *ppp;
    368       1.1  jdolecek 	struct ptyfsnode *pp;
    369       1.1  jdolecek 	struct vnode *vp;
    370       1.1  jdolecek 
    371       1.1  jdolecek loop:
    372       1.9        ad 	mutex_enter(&ptyfs_used_slock);
    373       1.1  jdolecek 	ppp = &ptyfs_used_tbl[PTYHASH(type, pty, ptyfs_used_mask)];
    374       1.1  jdolecek 	LIST_FOREACH(pp, ppp, ptyfs_hash) {
    375       1.1  jdolecek 		vp = PTYFSTOV(pp);
    376       1.1  jdolecek 		if (pty == pp->ptyfs_pty && pp->ptyfs_type == type &&
    377       1.1  jdolecek 		    vp->v_mount == mp) {
    378      1.14        ad 		    	if (flags == 0) {
    379      1.14        ad 				mutex_exit(&ptyfs_used_slock);
    380      1.14        ad 			} else {
    381  1.22.6.1    cherry 				mutex_enter(vp->v_interlock);
    382      1.14        ad 				mutex_exit(&ptyfs_used_slock);
    383      1.21   hannken 				if (vget(vp, flags))
    384      1.14        ad 					goto loop;
    385      1.14        ad 			}
    386       1.1  jdolecek 			return vp;
    387       1.1  jdolecek 		}
    388       1.1  jdolecek 	}
    389       1.9        ad 	mutex_exit(&ptyfs_used_slock);
    390       1.1  jdolecek 	return NULL;
    391       1.1  jdolecek }
    392       1.1  jdolecek 
    393       1.1  jdolecek /*
    394       1.1  jdolecek  * Insert the ptyfsnode into the used table and lock it.
    395       1.1  jdolecek  */
    396       1.1  jdolecek static void
    397       1.1  jdolecek ptyfs_hashins(struct ptyfsnode *pp)
    398       1.1  jdolecek {
    399       1.1  jdolecek 	struct ptyfs_hashhead *ppp;
    400       1.1  jdolecek 
    401       1.1  jdolecek 	/* lock the ptyfsnode, then put it on the appropriate hash list */
    402      1.20   hannken 	VOP_LOCK(PTYFSTOV(pp), LK_EXCLUSIVE);
    403       1.1  jdolecek 
    404       1.9        ad 	mutex_enter(&ptyfs_used_slock);
    405       1.1  jdolecek 	ppp = &ptyfs_used_tbl[PTYHASH(pp->ptyfs_type, pp->ptyfs_pty,
    406       1.1  jdolecek 	    ptyfs_used_mask)];
    407       1.1  jdolecek 	LIST_INSERT_HEAD(ppp, pp, ptyfs_hash);
    408       1.9        ad 	mutex_exit(&ptyfs_used_slock);
    409       1.1  jdolecek }
    410       1.1  jdolecek 
    411       1.1  jdolecek /*
    412       1.1  jdolecek  * Remove the ptyfsnode from the used table, and add it to the free table
    413       1.1  jdolecek  */
    414       1.1  jdolecek static void
    415       1.1  jdolecek ptyfs_hashrem(struct ptyfsnode *pp)
    416       1.1  jdolecek {
    417       1.1  jdolecek 	struct ptyfs_hashhead *ppp;
    418       1.1  jdolecek 
    419       1.9        ad 	mutex_enter(&ptyfs_used_slock);
    420       1.1  jdolecek 	LIST_REMOVE(pp, ptyfs_hash);
    421       1.9        ad 	mutex_exit(&ptyfs_used_slock);
    422       1.1  jdolecek 
    423       1.9        ad 	mutex_enter(&ptyfs_free_slock);
    424       1.1  jdolecek 	ppp = &ptyfs_free_tbl[PTYHASH(pp->ptyfs_type, pp->ptyfs_pty,
    425       1.1  jdolecek 	    ptyfs_free_mask)];
    426       1.1  jdolecek 	LIST_INSERT_HEAD(ppp, pp, ptyfs_hash);
    427       1.9        ad 	mutex_exit(&ptyfs_free_slock);
    428       1.1  jdolecek }
    429