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