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