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