Home | History | Annotate | Line # | Download | only in ptyfs
ptyfs_subr.c revision 1.14
      1  1.14        ad /*	$NetBSD: ptyfs_subr.c,v 1.14 2008/01/25 20:29:28 ad 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.14        ad __KERNEL_RCSID(0, "$NetBSD: ptyfs_subr.c,v 1.14 2008/01/25 20:29:28 ad 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.1  jdolecek #include <sys/malloc.h>
     84   1.1  jdolecek #include <sys/stat.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.1  jdolecek 		struct nameidata nd;
    133   1.1  jdolecek 		char ttyname[64];
    134   1.7      elad 		kauth_cred_t cred;
    135   1.1  jdolecek 		struct vattr va;
    136   1.1  jdolecek 		/*
    137   1.1  jdolecek 		 * We support traditional ptys, so we copy the info
    138   1.1  jdolecek 		 * from the inode
    139   1.1  jdolecek 		 */
    140   1.1  jdolecek 		if ((error = (*ptyfs_save_ptm->makename)(
    141   1.6  christos 			ptyfs_save_ptm, l, ttyname, sizeof(ttyname),
    142   1.1  jdolecek 			ptyfs->ptyfs_pty, ptyfs->ptyfs_type == PTYFSpts ? 't'
    143   1.1  jdolecek 			: 'p')) != 0)
    144   1.1  jdolecek 				goto out;
    145  1.11     pooka 		NDINIT(&nd, LOOKUP, NOFOLLOW|LOCKLEAF, UIO_SYSSPACE, ttyname);
    146   1.1  jdolecek 		if ((error = namei(&nd)) != 0)
    147   1.1  jdolecek 			goto out;
    148   1.7      elad 		cred = kauth_cred_alloc();
    149  1.10     pooka 		error = VOP_GETATTR(nd.ni_vp, &va, cred);
    150   1.7      elad 		kauth_cred_free(cred);
    151   1.1  jdolecek 		VOP_UNLOCK(nd.ni_vp, 0);
    152   1.1  jdolecek 		vrele(nd.ni_vp);
    153   1.1  jdolecek 		if (error)
    154   1.1  jdolecek 			goto out;
    155   1.1  jdolecek 		ptyfs->ptyfs_uid = va.va_uid;
    156   1.1  jdolecek 		ptyfs->ptyfs_gid = va.va_gid;
    157   1.1  jdolecek 		ptyfs->ptyfs_mode = va.va_mode;
    158   1.1  jdolecek 		ptyfs->ptyfs_flags = va.va_flags;
    159   1.1  jdolecek 		ptyfs->ptyfs_birthtime = va.va_birthtime;
    160   1.1  jdolecek 		ptyfs->ptyfs_ctime = va.va_ctime;
    161   1.1  jdolecek 		ptyfs->ptyfs_mtime = va.va_mtime;
    162   1.1  jdolecek 		ptyfs->ptyfs_atime = va.va_atime;
    163   1.1  jdolecek 		return;
    164   1.1  jdolecek 	}
    165   1.1  jdolecek out:
    166   1.1  jdolecek 	ptyfs->ptyfs_uid = ptyfs->ptyfs_gid = 0;
    167   1.3  christos 	ptyfs->ptyfs_flags |= PTYFS_CHANGE;
    168   1.3  christos 	PTYFS_ITIMES(ptyfs, NULL, NULL, NULL);
    169   1.1  jdolecek 	ptyfs->ptyfs_birthtime = ptyfs->ptyfs_mtime =
    170   1.1  jdolecek 	    ptyfs->ptyfs_atime = ptyfs->ptyfs_ctime;
    171   1.1  jdolecek 	ptyfs->ptyfs_flags = 0;
    172   1.1  jdolecek }
    173   1.1  jdolecek 
    174   1.1  jdolecek 
    175   1.1  jdolecek /*
    176   1.1  jdolecek  * allocate a ptyfsnode/vnode pair.  the vnode is
    177   1.1  jdolecek  * referenced, and locked.
    178   1.1  jdolecek  *
    179   1.1  jdolecek  * the pid, ptyfs_type, and mount point uniquely
    180   1.1  jdolecek  * identify a ptyfsnode.  the mount point is needed
    181   1.1  jdolecek  * because someone might mount this filesystem
    182   1.1  jdolecek  * twice.
    183   1.1  jdolecek  *
    184   1.1  jdolecek  * all ptyfsnodes are maintained on a singly-linked
    185   1.1  jdolecek  * list.  new nodes are only allocated when they cannot
    186   1.1  jdolecek  * be found on this list.  entries on the list are
    187   1.1  jdolecek  * removed when the vfs reclaim entry is called.
    188   1.1  jdolecek  *
    189   1.1  jdolecek  * a single lock is kept for the entire list.  this is
    190   1.1  jdolecek  * needed because the getnewvnode() function can block
    191   1.1  jdolecek  * waiting for a vnode to become free, in which case there
    192   1.1  jdolecek  * may be more than one ptyess trying to get the same
    193   1.1  jdolecek  * vnode.  this lock is only taken if we are going to
    194   1.1  jdolecek  * call getnewvnode, since the kernel itself is single-threaded.
    195   1.1  jdolecek  *
    196   1.1  jdolecek  * if an entry is found on the list, then call vget() to
    197   1.1  jdolecek  * take a reference.  this is done because there may be
    198   1.1  jdolecek  * zero references to it and so it needs to removed from
    199   1.1  jdolecek  * the vnode free list.
    200   1.1  jdolecek  */
    201   1.1  jdolecek int
    202   1.1  jdolecek ptyfs_allocvp(struct mount *mp, struct vnode **vpp, ptyfstype type, int pty,
    203   1.5  christos     struct lwp *l)
    204   1.1  jdolecek {
    205   1.1  jdolecek 	struct ptyfsnode *ptyfs;
    206  1.13        ad 	struct vnode *vp;
    207   1.1  jdolecek 	int error;
    208   1.1  jdolecek 
    209  1.14        ad  retry:
    210  1.14        ad 	if ((*vpp = ptyfs_used_get(type, pty, mp, LK_EXCLUSIVE)) != NULL)
    211  1.14        ad 		return 0;
    212   1.1  jdolecek 
    213   1.1  jdolecek 	if ((error = getnewvnode(VT_PTYFS, mp, ptyfs_vnodeop_p, &vp)) != 0) {
    214   1.1  jdolecek 		*vpp = NULL;
    215   1.1  jdolecek 		return error;
    216   1.1  jdolecek 	}
    217   1.1  jdolecek 
    218  1.14        ad 	mutex_enter(&ptyfs_hashlock);
    219  1.14        ad 	if (ptyfs_used_get(type, pty, mp, 0) != NULL) {
    220  1.14        ad 		mutex_exit(&ptyfs_hashlock);
    221  1.14        ad 		ungetnewvnode(vp);
    222  1.14        ad 		goto retry;
    223  1.14        ad 	}
    224  1.14        ad 
    225   1.5  christos 	vp->v_data = ptyfs = ptyfs_free_get(type, pty, l);
    226   1.1  jdolecek 	ptyfs->ptyfs_vnode = vp;
    227   1.1  jdolecek 
    228   1.1  jdolecek 	switch (type) {
    229   1.1  jdolecek 	case PTYFSroot:	/* /pts = dr-xr-xr-x */
    230   1.1  jdolecek 		vp->v_type = VDIR;
    231   1.9        ad 		vp->v_vflag = VV_ROOT;
    232   1.1  jdolecek 		break;
    233   1.1  jdolecek 
    234   1.1  jdolecek 	case PTYFSpts:	/* /pts/N = cxxxxxxxxx */
    235   1.1  jdolecek 	case PTYFSptc:	/* controlling side = cxxxxxxxxx */
    236   1.1  jdolecek 		vp->v_type = VCHR;
    237  1.13        ad 		spec_node_init(vp, PTYFS_MAKEDEV(ptyfs));
    238   1.1  jdolecek 		break;
    239   1.1  jdolecek 	default:
    240   1.1  jdolecek 		panic("ptyfs_allocvp");
    241   1.1  jdolecek 	}
    242   1.1  jdolecek 
    243   1.1  jdolecek 	ptyfs_hashins(ptyfs);
    244   1.1  jdolecek 	uvm_vnp_setsize(vp, 0);
    245  1.14        ad 	mutex_exit(&ptyfs_hashlock);
    246   1.1  jdolecek 
    247   1.1  jdolecek 	*vpp = vp;
    248   1.1  jdolecek 	return 0;
    249   1.1  jdolecek }
    250   1.1  jdolecek 
    251   1.1  jdolecek int
    252   1.1  jdolecek ptyfs_freevp(struct vnode *vp)
    253   1.1  jdolecek {
    254   1.1  jdolecek 	struct ptyfsnode *ptyfs = VTOPTYFS(vp);
    255   1.1  jdolecek 
    256   1.1  jdolecek 	ptyfs_hashrem(ptyfs);
    257   1.1  jdolecek 	vp->v_data = NULL;
    258   1.1  jdolecek 	return 0;
    259   1.1  jdolecek }
    260   1.1  jdolecek 
    261   1.1  jdolecek /*
    262   1.1  jdolecek  * Initialize ptyfsnode hash table.
    263   1.1  jdolecek  */
    264   1.1  jdolecek void
    265   1.1  jdolecek ptyfs_hashinit(void)
    266   1.1  jdolecek {
    267   1.1  jdolecek 	ptyfs_used_tbl = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT,
    268   1.1  jdolecek 	    M_WAITOK, &ptyfs_used_mask);
    269   1.1  jdolecek 	ptyfs_free_tbl = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT,
    270   1.1  jdolecek 	    M_WAITOK, &ptyfs_free_mask);
    271  1.14        ad 	mutex_init(&ptyfs_hashlock, MUTEX_DEFAULT, IPL_NONE);
    272   1.9        ad 	mutex_init(&ptyfs_used_slock, MUTEX_DEFAULT, IPL_NONE);
    273   1.9        ad 	mutex_init(&ptyfs_free_slock, MUTEX_DEFAULT, IPL_NONE);
    274   1.1  jdolecek }
    275   1.1  jdolecek 
    276   1.1  jdolecek void
    277   1.1  jdolecek ptyfs_hashreinit(void)
    278   1.1  jdolecek {
    279  1.14        ad 	ptyfs_rehash(&ptyfs_used_slock, &ptyfs_used_tbl, &ptyfs_used_mask);
    280  1.14        ad 	ptyfs_rehash(&ptyfs_free_slock, &ptyfs_free_tbl, &ptyfs_free_mask);
    281   1.1  jdolecek }
    282   1.1  jdolecek 
    283   1.1  jdolecek static void
    284   1.9        ad ptyfs_rehash(kmutex_t *hlock, struct ptyfs_hashhead **hhead,
    285   1.1  jdolecek     u_long *hmask)
    286   1.1  jdolecek {
    287   1.1  jdolecek 	struct ptyfsnode *pp;
    288   1.1  jdolecek 	struct ptyfs_hashhead *oldhash, *hash;
    289   1.1  jdolecek 	u_long i, oldmask, mask, val;
    290   1.1  jdolecek 
    291   1.1  jdolecek 	hash = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT, M_WAITOK,
    292   1.1  jdolecek 	    &mask);
    293   1.1  jdolecek 
    294   1.9        ad 	mutex_enter(hlock);
    295   1.1  jdolecek 	oldhash = *hhead;
    296   1.1  jdolecek 	oldmask = *hmask;
    297   1.1  jdolecek 	*hhead = hash;
    298   1.1  jdolecek 	*hmask = mask;
    299   1.1  jdolecek 	for (i = 0; i <= oldmask; i++) {
    300   1.1  jdolecek 		while ((pp = LIST_FIRST(&oldhash[i])) != NULL) {
    301   1.1  jdolecek 			LIST_REMOVE(pp, ptyfs_hash);
    302   1.1  jdolecek 			val = PTYHASH(pp->ptyfs_type, pp->ptyfs_pty,
    303   1.1  jdolecek 			    ptyfs_used_mask);
    304   1.1  jdolecek 			LIST_INSERT_HEAD(&hash[val], pp, ptyfs_hash);
    305   1.1  jdolecek 		}
    306   1.1  jdolecek 	}
    307   1.9        ad 	mutex_exit(hlock);
    308   1.1  jdolecek 	hashdone(oldhash, M_UFSMNT);
    309   1.1  jdolecek }
    310   1.1  jdolecek 
    311   1.1  jdolecek /*
    312   1.1  jdolecek  * Free ptyfsnode hash table.
    313   1.1  jdolecek  */
    314   1.1  jdolecek void
    315   1.1  jdolecek ptyfs_hashdone(void)
    316   1.1  jdolecek {
    317   1.1  jdolecek 	hashdone(ptyfs_used_tbl, M_UFSMNT);
    318   1.1  jdolecek 	hashdone(ptyfs_free_tbl, M_UFSMNT);
    319   1.1  jdolecek }
    320   1.1  jdolecek 
    321   1.1  jdolecek /*
    322   1.1  jdolecek  * Get a ptyfsnode from the free table, or allocate one.
    323   1.1  jdolecek  * Removes the node from the free table.
    324   1.1  jdolecek  */
    325   1.1  jdolecek struct ptyfsnode *
    326   1.5  christos ptyfs_free_get(ptyfstype type, int pty, struct lwp *l)
    327   1.1  jdolecek {
    328   1.1  jdolecek 	struct ptyfs_hashhead *ppp;
    329   1.1  jdolecek 	struct ptyfsnode *pp;
    330   1.1  jdolecek 
    331   1.9        ad 	mutex_enter(&ptyfs_free_slock);
    332   1.1  jdolecek 	ppp = &ptyfs_free_tbl[PTYHASH(type, pty, ptyfs_free_mask)];
    333   1.1  jdolecek 	LIST_FOREACH(pp, ppp, ptyfs_hash) {
    334   1.1  jdolecek 		if (pty == pp->ptyfs_pty && pp->ptyfs_type == type) {
    335   1.1  jdolecek 			LIST_REMOVE(pp, ptyfs_hash);
    336   1.9        ad 			mutex_exit(&ptyfs_free_slock);
    337   1.1  jdolecek 			return pp;
    338   1.1  jdolecek 		}
    339   1.1  jdolecek 	}
    340   1.9        ad 	mutex_exit(&ptyfs_free_slock);
    341   1.1  jdolecek 
    342   1.1  jdolecek 	MALLOC(pp, void *, sizeof(struct ptyfsnode), M_TEMP, M_WAITOK);
    343   1.1  jdolecek 	pp->ptyfs_pty = pty;
    344   1.1  jdolecek 	pp->ptyfs_type = type;
    345   1.1  jdolecek 	pp->ptyfs_fileno = PTYFS_FILENO(pty, type);
    346   1.5  christos 	ptyfs_getinfo(pp, l);
    347   1.1  jdolecek 	return pp;
    348   1.1  jdolecek }
    349   1.1  jdolecek 
    350   1.1  jdolecek struct vnode *
    351  1.14        ad ptyfs_used_get(ptyfstype type, int pty, struct mount *mp, int flags)
    352   1.1  jdolecek {
    353   1.1  jdolecek 	struct ptyfs_hashhead *ppp;
    354   1.1  jdolecek 	struct ptyfsnode *pp;
    355   1.1  jdolecek 	struct vnode *vp;
    356   1.1  jdolecek 
    357   1.1  jdolecek loop:
    358   1.9        ad 	mutex_enter(&ptyfs_used_slock);
    359   1.1  jdolecek 	ppp = &ptyfs_used_tbl[PTYHASH(type, pty, ptyfs_used_mask)];
    360   1.1  jdolecek 	LIST_FOREACH(pp, ppp, ptyfs_hash) {
    361   1.1  jdolecek 		vp = PTYFSTOV(pp);
    362   1.1  jdolecek 		if (pty == pp->ptyfs_pty && pp->ptyfs_type == type &&
    363   1.1  jdolecek 		    vp->v_mount == mp) {
    364  1.14        ad 		    	if (flags == 0) {
    365  1.14        ad 				mutex_exit(&ptyfs_used_slock);
    366  1.14        ad 			} else {
    367  1.14        ad 				mutex_enter(&vp->v_interlock);
    368  1.14        ad 				mutex_exit(&ptyfs_used_slock);
    369  1.14        ad 				if (vget(vp, flags | LK_INTERLOCK))
    370  1.14        ad 					goto loop;
    371  1.14        ad 			}
    372   1.1  jdolecek 			return vp;
    373   1.1  jdolecek 		}
    374   1.1  jdolecek 	}
    375   1.9        ad 	mutex_exit(&ptyfs_used_slock);
    376   1.1  jdolecek 	return NULL;
    377   1.1  jdolecek }
    378   1.1  jdolecek 
    379   1.1  jdolecek /*
    380   1.1  jdolecek  * Insert the ptyfsnode into the used table and lock it.
    381   1.1  jdolecek  */
    382   1.1  jdolecek static void
    383   1.1  jdolecek ptyfs_hashins(struct ptyfsnode *pp)
    384   1.1  jdolecek {
    385   1.1  jdolecek 	struct ptyfs_hashhead *ppp;
    386   1.1  jdolecek 
    387   1.1  jdolecek 	/* lock the ptyfsnode, then put it on the appropriate hash list */
    388   1.1  jdolecek 	lockmgr(&pp->ptyfs_vnode->v_lock, LK_EXCLUSIVE, NULL);
    389   1.1  jdolecek 
    390   1.9        ad 	mutex_enter(&ptyfs_used_slock);
    391   1.1  jdolecek 	ppp = &ptyfs_used_tbl[PTYHASH(pp->ptyfs_type, pp->ptyfs_pty,
    392   1.1  jdolecek 	    ptyfs_used_mask)];
    393   1.1  jdolecek 	LIST_INSERT_HEAD(ppp, pp, ptyfs_hash);
    394   1.9        ad 	mutex_exit(&ptyfs_used_slock);
    395   1.1  jdolecek }
    396   1.1  jdolecek 
    397   1.1  jdolecek /*
    398   1.1  jdolecek  * Remove the ptyfsnode from the used table, and add it to the free table
    399   1.1  jdolecek  */
    400   1.1  jdolecek static void
    401   1.1  jdolecek ptyfs_hashrem(struct ptyfsnode *pp)
    402   1.1  jdolecek {
    403   1.1  jdolecek 	struct ptyfs_hashhead *ppp;
    404   1.1  jdolecek 
    405   1.9        ad 	mutex_enter(&ptyfs_used_slock);
    406   1.1  jdolecek 	LIST_REMOVE(pp, ptyfs_hash);
    407   1.9        ad 	mutex_exit(&ptyfs_used_slock);
    408   1.1  jdolecek 
    409   1.9        ad 	mutex_enter(&ptyfs_free_slock);
    410   1.1  jdolecek 	ppp = &ptyfs_free_tbl[PTYHASH(pp->ptyfs_type, pp->ptyfs_pty,
    411   1.1  jdolecek 	    ptyfs_free_mask)];
    412   1.1  jdolecek 	LIST_INSERT_HEAD(ppp, pp, ptyfs_hash);
    413   1.9        ad 	mutex_exit(&ptyfs_free_slock);
    414   1.1  jdolecek }
    415