Home | History | Annotate | Line # | Download | only in ptyfs
ptyfs_subr.c revision 1.30
      1  1.30   hannken /*	$NetBSD: ptyfs_subr.c,v 1.30 2014/08/13 14:10:00 hannken 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.30   hannken __KERNEL_RCSID(0, "$NetBSD: ptyfs_subr.c,v 1.30 2014/08/13 14:10:00 hannken 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.1  jdolecek static void ptyfs_hashins(struct ptyfsnode *);
    104   1.1  jdolecek static void ptyfs_hashrem(struct ptyfsnode *);
    105   1.1  jdolecek 
    106  1.30   hannken static struct vnode *ptyfs_used_get(ptyfstype, int, struct mount *, int);
    107  1.30   hannken static struct ptyfsnode *ptyfs_free_get(ptyfstype, int);
    108   1.1  jdolecek 
    109   1.9        ad static void ptyfs_rehash(kmutex_t *, struct ptyfs_hashhead **,
    110   1.1  jdolecek     u_long *);
    111   1.1  jdolecek 
    112   1.1  jdolecek #define PTYHASH(type, pty, mask) (PTYFS_FILENO(type, pty) % (mask + 1))
    113   1.1  jdolecek 
    114   1.1  jdolecek 
    115   1.1  jdolecek /*
    116   1.1  jdolecek  * allocate a ptyfsnode/vnode pair.  the vnode is
    117   1.1  jdolecek  * referenced, and locked.
    118   1.1  jdolecek  *
    119  1.27  christos  * the pty, ptyfs_type, and mount point uniquely
    120   1.1  jdolecek  * identify a ptyfsnode.  the mount point is needed
    121   1.1  jdolecek  * because someone might mount this filesystem
    122   1.1  jdolecek  * twice.
    123   1.1  jdolecek  *
    124   1.1  jdolecek  * all ptyfsnodes are maintained on a singly-linked
    125   1.1  jdolecek  * list.  new nodes are only allocated when they cannot
    126   1.1  jdolecek  * be found on this list.  entries on the list are
    127   1.1  jdolecek  * removed when the vfs reclaim entry is called.
    128   1.1  jdolecek  *
    129   1.1  jdolecek  * a single lock is kept for the entire list.  this is
    130   1.1  jdolecek  * needed because the getnewvnode() function can block
    131   1.1  jdolecek  * waiting for a vnode to become free, in which case there
    132   1.1  jdolecek  * may be more than one ptyess trying to get the same
    133   1.1  jdolecek  * vnode.  this lock is only taken if we are going to
    134   1.1  jdolecek  * call getnewvnode, since the kernel itself is single-threaded.
    135   1.1  jdolecek  *
    136   1.1  jdolecek  * if an entry is found on the list, then call vget() to
    137   1.1  jdolecek  * take a reference.  this is done because there may be
    138   1.1  jdolecek  * zero references to it and so it needs to removed from
    139   1.1  jdolecek  * the vnode free list.
    140   1.1  jdolecek  */
    141   1.1  jdolecek int
    142  1.30   hannken ptyfs_allocvp(struct mount *mp, struct vnode **vpp, ptyfstype type, int pty)
    143   1.1  jdolecek {
    144   1.1  jdolecek 	struct ptyfsnode *ptyfs;
    145  1.13        ad 	struct vnode *vp;
    146   1.1  jdolecek 	int error;
    147   1.1  jdolecek 
    148  1.14        ad  retry:
    149  1.14        ad 	if ((*vpp = ptyfs_used_get(type, pty, mp, LK_EXCLUSIVE)) != NULL)
    150  1.14        ad 		return 0;
    151   1.1  jdolecek 
    152  1.23     rmind 	error = getnewvnode(VT_PTYFS, mp, ptyfs_vnodeop_p, NULL, &vp);
    153  1.23     rmind 	if (error) {
    154   1.1  jdolecek 		*vpp = NULL;
    155   1.1  jdolecek 		return error;
    156   1.1  jdolecek 	}
    157   1.1  jdolecek 
    158  1.14        ad 	mutex_enter(&ptyfs_hashlock);
    159  1.14        ad 	if (ptyfs_used_get(type, pty, mp, 0) != NULL) {
    160  1.14        ad 		mutex_exit(&ptyfs_hashlock);
    161  1.14        ad 		ungetnewvnode(vp);
    162  1.14        ad 		goto retry;
    163  1.14        ad 	}
    164  1.14        ad 
    165  1.30   hannken 	vp->v_data = ptyfs = ptyfs_free_get(type, pty);
    166   1.1  jdolecek 	ptyfs->ptyfs_vnode = vp;
    167   1.1  jdolecek 
    168   1.1  jdolecek 	switch (type) {
    169   1.1  jdolecek 	case PTYFSroot:	/* /pts = dr-xr-xr-x */
    170   1.1  jdolecek 		vp->v_type = VDIR;
    171   1.9        ad 		vp->v_vflag = VV_ROOT;
    172   1.1  jdolecek 		break;
    173   1.1  jdolecek 
    174   1.1  jdolecek 	case PTYFSpts:	/* /pts/N = cxxxxxxxxx */
    175   1.1  jdolecek 	case PTYFSptc:	/* controlling side = cxxxxxxxxx */
    176   1.1  jdolecek 		vp->v_type = VCHR;
    177  1.13        ad 		spec_node_init(vp, PTYFS_MAKEDEV(ptyfs));
    178   1.1  jdolecek 		break;
    179   1.1  jdolecek 	default:
    180   1.1  jdolecek 		panic("ptyfs_allocvp");
    181   1.1  jdolecek 	}
    182   1.1  jdolecek 
    183   1.1  jdolecek 	ptyfs_hashins(ptyfs);
    184   1.1  jdolecek 	uvm_vnp_setsize(vp, 0);
    185  1.14        ad 	mutex_exit(&ptyfs_hashlock);
    186   1.1  jdolecek 
    187   1.1  jdolecek 	*vpp = vp;
    188   1.1  jdolecek 	return 0;
    189   1.1  jdolecek }
    190   1.1  jdolecek 
    191   1.1  jdolecek int
    192   1.1  jdolecek ptyfs_freevp(struct vnode *vp)
    193   1.1  jdolecek {
    194   1.1  jdolecek 	struct ptyfsnode *ptyfs = VTOPTYFS(vp);
    195   1.1  jdolecek 
    196   1.1  jdolecek 	ptyfs_hashrem(ptyfs);
    197   1.1  jdolecek 	vp->v_data = NULL;
    198   1.1  jdolecek 	return 0;
    199   1.1  jdolecek }
    200   1.1  jdolecek 
    201   1.1  jdolecek /*
    202   1.1  jdolecek  * Initialize ptyfsnode hash table.
    203   1.1  jdolecek  */
    204   1.1  jdolecek void
    205   1.1  jdolecek ptyfs_hashinit(void)
    206   1.1  jdolecek {
    207  1.16        ad 	ptyfs_used_tbl = hashinit(desiredvnodes / 4, HASH_LIST, true,
    208  1.16        ad 	    &ptyfs_used_mask);
    209  1.16        ad 	ptyfs_free_tbl = hashinit(desiredvnodes / 4, HASH_LIST, true,
    210  1.16        ad 	    &ptyfs_free_mask);
    211  1.14        ad 	mutex_init(&ptyfs_hashlock, MUTEX_DEFAULT, IPL_NONE);
    212   1.9        ad 	mutex_init(&ptyfs_used_slock, MUTEX_DEFAULT, IPL_NONE);
    213   1.9        ad 	mutex_init(&ptyfs_free_slock, MUTEX_DEFAULT, IPL_NONE);
    214   1.1  jdolecek }
    215   1.1  jdolecek 
    216   1.1  jdolecek void
    217   1.1  jdolecek ptyfs_hashreinit(void)
    218   1.1  jdolecek {
    219  1.14        ad 	ptyfs_rehash(&ptyfs_used_slock, &ptyfs_used_tbl, &ptyfs_used_mask);
    220  1.14        ad 	ptyfs_rehash(&ptyfs_free_slock, &ptyfs_free_tbl, &ptyfs_free_mask);
    221   1.1  jdolecek }
    222   1.1  jdolecek 
    223   1.1  jdolecek static void
    224   1.9        ad ptyfs_rehash(kmutex_t *hlock, struct ptyfs_hashhead **hhead,
    225   1.1  jdolecek     u_long *hmask)
    226   1.1  jdolecek {
    227   1.1  jdolecek 	struct ptyfsnode *pp;
    228   1.1  jdolecek 	struct ptyfs_hashhead *oldhash, *hash;
    229   1.1  jdolecek 	u_long i, oldmask, mask, val;
    230   1.1  jdolecek 
    231  1.16        ad 	hash = hashinit(desiredvnodes / 4, HASH_LIST, true, &mask);
    232   1.1  jdolecek 
    233   1.9        ad 	mutex_enter(hlock);
    234   1.1  jdolecek 	oldhash = *hhead;
    235   1.1  jdolecek 	oldmask = *hmask;
    236   1.1  jdolecek 	*hhead = hash;
    237   1.1  jdolecek 	*hmask = mask;
    238   1.1  jdolecek 	for (i = 0; i <= oldmask; i++) {
    239   1.1  jdolecek 		while ((pp = LIST_FIRST(&oldhash[i])) != NULL) {
    240   1.1  jdolecek 			LIST_REMOVE(pp, ptyfs_hash);
    241   1.1  jdolecek 			val = PTYHASH(pp->ptyfs_type, pp->ptyfs_pty,
    242   1.1  jdolecek 			    ptyfs_used_mask);
    243   1.1  jdolecek 			LIST_INSERT_HEAD(&hash[val], pp, ptyfs_hash);
    244   1.1  jdolecek 		}
    245   1.1  jdolecek 	}
    246   1.9        ad 	mutex_exit(hlock);
    247  1.16        ad 	hashdone(oldhash, HASH_LIST, oldmask);
    248   1.1  jdolecek }
    249   1.1  jdolecek 
    250   1.1  jdolecek /*
    251   1.1  jdolecek  * Free ptyfsnode hash table.
    252   1.1  jdolecek  */
    253   1.1  jdolecek void
    254   1.1  jdolecek ptyfs_hashdone(void)
    255   1.1  jdolecek {
    256  1.18      haad 
    257  1.18      haad 	mutex_destroy(&ptyfs_hashlock);
    258  1.18      haad 	mutex_destroy(&ptyfs_used_slock);
    259  1.18      haad 	mutex_destroy(&ptyfs_free_slock);
    260  1.16        ad 	hashdone(ptyfs_used_tbl, HASH_LIST, ptyfs_used_mask);
    261  1.16        ad 	hashdone(ptyfs_free_tbl, HASH_LIST, ptyfs_free_mask);
    262   1.1  jdolecek }
    263   1.1  jdolecek 
    264   1.1  jdolecek /*
    265   1.1  jdolecek  * Get a ptyfsnode from the free table, or allocate one.
    266   1.1  jdolecek  * Removes the node from the free table.
    267   1.1  jdolecek  */
    268   1.1  jdolecek struct ptyfsnode *
    269  1.30   hannken ptyfs_free_get(ptyfstype type, int pty)
    270   1.1  jdolecek {
    271   1.1  jdolecek 	struct ptyfs_hashhead *ppp;
    272   1.1  jdolecek 	struct ptyfsnode *pp;
    273   1.1  jdolecek 
    274   1.9        ad 	mutex_enter(&ptyfs_free_slock);
    275   1.1  jdolecek 	ppp = &ptyfs_free_tbl[PTYHASH(type, pty, ptyfs_free_mask)];
    276   1.1  jdolecek 	LIST_FOREACH(pp, ppp, ptyfs_hash) {
    277   1.1  jdolecek 		if (pty == pp->ptyfs_pty && pp->ptyfs_type == type) {
    278   1.1  jdolecek 			LIST_REMOVE(pp, ptyfs_hash);
    279   1.9        ad 			mutex_exit(&ptyfs_free_slock);
    280   1.1  jdolecek 			return pp;
    281   1.1  jdolecek 		}
    282   1.1  jdolecek 	}
    283   1.9        ad 	mutex_exit(&ptyfs_free_slock);
    284   1.1  jdolecek 
    285  1.17    cegger 	pp = malloc(sizeof(struct ptyfsnode), M_TEMP, M_WAITOK);
    286   1.1  jdolecek 	pp->ptyfs_pty = pty;
    287   1.1  jdolecek 	pp->ptyfs_type = type;
    288   1.1  jdolecek 	pp->ptyfs_fileno = PTYFS_FILENO(pty, type);
    289  1.30   hannken 	if (pp->ptyfs_type == PTYFSroot)
    290  1.30   hannken 		pp->ptyfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|
    291  1.30   hannken 		    S_IROTH|S_IXOTH;
    292  1.30   hannken 	else
    293  1.30   hannken 		pp->ptyfs_mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|
    294  1.30   hannken 		    S_IROTH|S_IWOTH;
    295  1.30   hannken 
    296  1.30   hannken 	pp->ptyfs_uid = pp->ptyfs_gid = 0;
    297  1.30   hannken 	pp->ptyfs_status = PTYFS_CHANGE;
    298  1.30   hannken 	PTYFS_ITIMES(pp, NULL, NULL, NULL);
    299  1.30   hannken 	pp->ptyfs_birthtime = pp->ptyfs_mtime =
    300  1.30   hannken 	    pp->ptyfs_atime = pp->ptyfs_ctime;
    301  1.30   hannken 	pp->ptyfs_flags = 0;
    302   1.1  jdolecek 	return pp;
    303   1.1  jdolecek }
    304   1.1  jdolecek 
    305   1.1  jdolecek struct vnode *
    306  1.14        ad ptyfs_used_get(ptyfstype type, int pty, struct mount *mp, int flags)
    307   1.1  jdolecek {
    308   1.1  jdolecek 	struct ptyfs_hashhead *ppp;
    309   1.1  jdolecek 	struct ptyfsnode *pp;
    310   1.1  jdolecek 	struct vnode *vp;
    311   1.1  jdolecek 
    312   1.1  jdolecek loop:
    313   1.9        ad 	mutex_enter(&ptyfs_used_slock);
    314   1.1  jdolecek 	ppp = &ptyfs_used_tbl[PTYHASH(type, pty, ptyfs_used_mask)];
    315   1.1  jdolecek 	LIST_FOREACH(pp, ppp, ptyfs_hash) {
    316   1.1  jdolecek 		vp = PTYFSTOV(pp);
    317   1.1  jdolecek 		if (pty == pp->ptyfs_pty && pp->ptyfs_type == type &&
    318   1.1  jdolecek 		    vp->v_mount == mp) {
    319  1.14        ad 		    	if (flags == 0) {
    320  1.14        ad 				mutex_exit(&ptyfs_used_slock);
    321  1.14        ad 			} else {
    322  1.23     rmind 				mutex_enter(vp->v_interlock);
    323  1.14        ad 				mutex_exit(&ptyfs_used_slock);
    324  1.21   hannken 				if (vget(vp, flags))
    325  1.14        ad 					goto loop;
    326  1.14        ad 			}
    327   1.1  jdolecek 			return vp;
    328   1.1  jdolecek 		}
    329   1.1  jdolecek 	}
    330   1.9        ad 	mutex_exit(&ptyfs_used_slock);
    331   1.1  jdolecek 	return NULL;
    332   1.1  jdolecek }
    333   1.1  jdolecek 
    334   1.1  jdolecek /*
    335   1.1  jdolecek  * Insert the ptyfsnode into the used table and lock it.
    336   1.1  jdolecek  */
    337   1.1  jdolecek static void
    338   1.1  jdolecek ptyfs_hashins(struct ptyfsnode *pp)
    339   1.1  jdolecek {
    340   1.1  jdolecek 	struct ptyfs_hashhead *ppp;
    341  1.26   hannken 	int error __diagused;
    342   1.1  jdolecek 
    343   1.1  jdolecek 	/* lock the ptyfsnode, then put it on the appropriate hash list */
    344  1.26   hannken 	error = VOP_LOCK(PTYFSTOV(pp), LK_EXCLUSIVE);
    345  1.26   hannken 	KASSERT(error == 0);
    346   1.1  jdolecek 
    347   1.9        ad 	mutex_enter(&ptyfs_used_slock);
    348   1.1  jdolecek 	ppp = &ptyfs_used_tbl[PTYHASH(pp->ptyfs_type, pp->ptyfs_pty,
    349   1.1  jdolecek 	    ptyfs_used_mask)];
    350   1.1  jdolecek 	LIST_INSERT_HEAD(ppp, pp, ptyfs_hash);
    351   1.9        ad 	mutex_exit(&ptyfs_used_slock);
    352   1.1  jdolecek }
    353   1.1  jdolecek 
    354   1.1  jdolecek /*
    355   1.1  jdolecek  * Remove the ptyfsnode from the used table, and add it to the free table
    356   1.1  jdolecek  */
    357   1.1  jdolecek static void
    358   1.1  jdolecek ptyfs_hashrem(struct ptyfsnode *pp)
    359   1.1  jdolecek {
    360   1.1  jdolecek 	struct ptyfs_hashhead *ppp;
    361   1.1  jdolecek 
    362   1.9        ad 	mutex_enter(&ptyfs_used_slock);
    363   1.1  jdolecek 	LIST_REMOVE(pp, ptyfs_hash);
    364   1.9        ad 	mutex_exit(&ptyfs_used_slock);
    365   1.1  jdolecek 
    366   1.9        ad 	mutex_enter(&ptyfs_free_slock);
    367   1.1  jdolecek 	ppp = &ptyfs_free_tbl[PTYHASH(pp->ptyfs_type, pp->ptyfs_pty,
    368   1.1  jdolecek 	    ptyfs_free_mask)];
    369   1.1  jdolecek 	LIST_INSERT_HEAD(ppp, pp, ptyfs_hash);
    370   1.9        ad 	mutex_exit(&ptyfs_free_slock);
    371   1.1  jdolecek }
    372  1.30   hannken 
    373  1.30   hannken /*
    374  1.30   hannken  * Mark this controlling pty as active.
    375  1.30   hannken  */
    376  1.30   hannken void
    377  1.30   hannken ptyfs_set_active(struct mount *mp, int pty)
    378  1.30   hannken {
    379  1.30   hannken 	struct ptyfsmount *pmnt = VFSTOPTY(mp);
    380  1.30   hannken 
    381  1.30   hannken 	KASSERT(pty >= 0);
    382  1.30   hannken 	/* Reallocate map if needed. */
    383  1.30   hannken 	if (pty >= pmnt->pmnt_bitmap_size * NBBY) {
    384  1.30   hannken 		int osize, nsize;
    385  1.30   hannken 		uint8_t *obitmap, *nbitmap;
    386  1.30   hannken 
    387  1.30   hannken 		nsize = roundup(howmany(pty + 1, NBBY), 64);
    388  1.30   hannken 		nbitmap = kmem_alloc(nsize, KM_SLEEP);
    389  1.30   hannken 		mutex_enter(&pmnt->pmnt_lock);
    390  1.30   hannken 		if (pty < pmnt->pmnt_bitmap_size * NBBY) {
    391  1.30   hannken 			mutex_exit(&pmnt->pmnt_lock);
    392  1.30   hannken 			kmem_free(nbitmap, nsize);
    393  1.30   hannken 		} else {
    394  1.30   hannken 			osize = pmnt->pmnt_bitmap_size;
    395  1.30   hannken 			obitmap = pmnt->pmnt_bitmap;
    396  1.30   hannken 			pmnt->pmnt_bitmap_size = nsize;
    397  1.30   hannken 			pmnt->pmnt_bitmap = nbitmap;
    398  1.30   hannken 			if (osize > 0)
    399  1.30   hannken 				memcpy(pmnt->pmnt_bitmap, obitmap, osize);
    400  1.30   hannken 			memset(pmnt->pmnt_bitmap + osize, 0, nsize - osize);
    401  1.30   hannken 			mutex_exit(&pmnt->pmnt_lock);
    402  1.30   hannken 			if (osize > 0)
    403  1.30   hannken 				kmem_free(obitmap, osize);
    404  1.30   hannken 		}
    405  1.30   hannken 	}
    406  1.30   hannken 
    407  1.30   hannken 	mutex_enter(&pmnt->pmnt_lock);
    408  1.30   hannken 	setbit(pmnt->pmnt_bitmap, pty);
    409  1.30   hannken 	mutex_exit(&pmnt->pmnt_lock);
    410  1.30   hannken }
    411  1.30   hannken 
    412  1.30   hannken /*
    413  1.30   hannken  * Mark this controlling pty as inactive.
    414  1.30   hannken  */
    415  1.30   hannken void
    416  1.30   hannken ptyfs_clr_active(struct mount *mp, int pty)
    417  1.30   hannken {
    418  1.30   hannken 	struct ptyfsmount *pmnt = VFSTOPTY(mp);
    419  1.30   hannken 
    420  1.30   hannken 	KASSERT(pty >= 0);
    421  1.30   hannken 	mutex_enter(&pmnt->pmnt_lock);
    422  1.30   hannken 	if (pty >= 0 && pty < pmnt->pmnt_bitmap_size * NBBY)
    423  1.30   hannken 		clrbit(pmnt->pmnt_bitmap, pty);
    424  1.30   hannken 	mutex_exit(&pmnt->pmnt_lock);
    425  1.30   hannken }
    426  1.30   hannken 
    427  1.30   hannken /*
    428  1.30   hannken  * Lookup the next active controlling pty greater or equal "pty".
    429  1.30   hannken  * Return -1 if not found.
    430  1.30   hannken  */
    431  1.30   hannken int
    432  1.30   hannken ptyfs_next_active(struct mount *mp, int pty)
    433  1.30   hannken {
    434  1.30   hannken 	struct ptyfsmount *pmnt = VFSTOPTY(mp);
    435  1.30   hannken 
    436  1.30   hannken 	KASSERT(pty >= 0);
    437  1.30   hannken 	mutex_enter(&pmnt->pmnt_lock);
    438  1.30   hannken 	while (pty < pmnt->pmnt_bitmap_size * NBBY) {
    439  1.30   hannken 		if (isset(pmnt->pmnt_bitmap, pty)) {
    440  1.30   hannken 			mutex_exit(&pmnt->pmnt_lock);
    441  1.30   hannken 			return pty;
    442  1.30   hannken 		}
    443  1.30   hannken 		pty++;
    444  1.30   hannken 	}
    445  1.30   hannken 	mutex_exit(&pmnt->pmnt_lock);
    446  1.30   hannken 	return -1;
    447  1.30   hannken }
    448