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