1 /*- 2 * Copyright (c) 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software donated to Berkeley by 6 * Jan-Simon Pendry. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)fdesc_vnops.c 8.9 (Berkeley) 1/21/94 33 * 34 * $FreeBSD$ 35 */ 36 37 /* 38 * /dev/fd Filesystem 39 */ 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/conf.h> 44 #include <sys/dirent.h> 45 #include <sys/filedesc.h> 46 #include <sys/kernel.h> /* boottime */ 47 #include <sys/lock.h> 48 #include <sys/mutex.h> 49 #include <sys/malloc.h> 50 #include <sys/file.h> /* Must come after sys/malloc.h */ 51 #include <sys/mount.h> 52 #include <sys/namei.h> 53 #include <sys/proc.h> 54 #include <sys/stat.h> 55 #include <sys/vnode.h> 56 57 #include <fs/fdescfs/fdesc.h> 58 59 #define NFDCACHE 4 60 #define FD_NHASH(ix) \ 61 (&fdhashtbl[(ix) & fdhash]) 62 static LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl; 63 static u_long fdhash; 64 65 struct mtx fdesc_hashmtx; 66 67 static vop_getattr_t fdesc_getattr; 68 static vop_lookup_t fdesc_lookup; 69 static vop_open_t fdesc_open; 70 static vop_readdir_t fdesc_readdir; 71 static vop_reclaim_t fdesc_reclaim; 72 static vop_setattr_t fdesc_setattr; 73 74 static struct vop_vector fdesc_vnodeops = { 75 .vop_default = &default_vnodeops, 76 77 .vop_access = VOP_NULL, 78 .vop_getattr = fdesc_getattr, 79 .vop_lookup = fdesc_lookup, 80 .vop_open = fdesc_open, 81 .vop_pathconf = vop_stdpathconf, 82 .vop_readdir = fdesc_readdir, 83 .vop_reclaim = fdesc_reclaim, 84 .vop_setattr = fdesc_setattr, 85 }; 86 87 static void fdesc_insmntque_dtr(struct vnode *, void *); 88 static void fdesc_remove_entry(struct fdescnode *); 89 90 /* 91 * Initialise cache headers 92 */ 93 int 94 fdesc_init(vfsp) 95 struct vfsconf *vfsp; 96 { 97 98 mtx_init(&fdesc_hashmtx, "fdescfs_hash", NULL, MTX_DEF); 99 fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash); 100 return (0); 101 } 102 103 /* 104 * Uninit ready for unload. 105 */ 106 int 107 fdesc_uninit(vfsp) 108 struct vfsconf *vfsp; 109 { 110 111 hashdestroy(fdhashtbl, M_CACHE, fdhash); 112 mtx_destroy(&fdesc_hashmtx); 113 return (0); 114 } 115 116 /* 117 * If allocating vnode fails, call this. 118 */ 119 static void 120 fdesc_insmntque_dtr(struct vnode *vp, void *arg) 121 { 122 123 vgone(vp); 124 vput(vp); 125 } 126 127 /* 128 * Remove an entry from the hash if it exists. 129 */ 130 static void 131 fdesc_remove_entry(struct fdescnode *fd) 132 { 133 struct fdhashhead *fc; 134 struct fdescnode *fd2; 135 136 fc = FD_NHASH(fd->fd_ix); 137 mtx_lock(&fdesc_hashmtx); 138 LIST_FOREACH(fd2, fc, fd_hash) { 139 if (fd == fd2) { 140 LIST_REMOVE(fd, fd_hash); 141 break; 142 } 143 } 144 mtx_unlock(&fdesc_hashmtx); 145 } 146 147 int 148 fdesc_allocvp(ftype, fd_fd, ix, mp, vpp) 149 fdntype ftype; 150 unsigned fd_fd; 151 int ix; 152 struct mount *mp; 153 struct vnode **vpp; 154 { 155 struct fdescmount *fmp; 156 struct fdhashhead *fc; 157 struct fdescnode *fd, *fd2; 158 struct vnode *vp, *vp2; 159 struct thread *td; 160 int error = 0; 161 162 td = curthread; 163 fc = FD_NHASH(ix); 164 loop: 165 mtx_lock(&fdesc_hashmtx); 166 /* 167 * If a forced unmount is progressing, we need to drop it. The flags are 168 * protected by the hashmtx. 169 */ 170 fmp = (struct fdescmount *)mp->mnt_data; 171 if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) { 172 mtx_unlock(&fdesc_hashmtx); 173 return (-1); 174 } 175 176 LIST_FOREACH(fd, fc, fd_hash) { 177 if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) { 178 /* Get reference to vnode in case it's being free'd */ 179 vp = fd->fd_vnode; 180 VI_LOCK(vp); 181 mtx_unlock(&fdesc_hashmtx); 182 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) 183 goto loop; 184 *vpp = vp; 185 return (0); 186 } 187 } 188 mtx_unlock(&fdesc_hashmtx); 189 190 fd = malloc(sizeof(struct fdescnode), M_TEMP, M_WAITOK); 191 192 error = getnewvnode("fdescfs", mp, &fdesc_vnodeops, &vp); 193 if (error) { 194 free(fd, M_TEMP); 195 return (error); 196 } 197 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 198 vp->v_data = fd; 199 fd->fd_vnode = vp; 200 fd->fd_type = ftype; 201 fd->fd_fd = fd_fd; 202 fd->fd_ix = ix; 203 error = insmntque1(vp, mp, fdesc_insmntque_dtr, NULL); 204 if (error != 0) { 205 *vpp = NULLVP; 206 return (error); 207 } 208 209 /* Make sure that someone didn't beat us when inserting the vnode. */ 210 mtx_lock(&fdesc_hashmtx); 211 /* 212 * If a forced unmount is progressing, we need to drop it. The flags are 213 * protected by the hashmtx. 214 */ 215 fmp = (struct fdescmount *)mp->mnt_data; 216 if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) { 217 mtx_unlock(&fdesc_hashmtx); 218 vgone(vp); 219 vput(vp); 220 *vpp = NULLVP; 221 return (-1); 222 } 223 224 LIST_FOREACH(fd2, fc, fd_hash) { 225 if (fd2->fd_ix == ix && fd2->fd_vnode->v_mount == mp) { 226 /* Get reference to vnode in case it's being free'd */ 227 vp2 = fd2->fd_vnode; 228 VI_LOCK(vp2); 229 mtx_unlock(&fdesc_hashmtx); 230 error = vget(vp2, LK_EXCLUSIVE | LK_INTERLOCK, td); 231 /* Someone beat us, dec use count and wait for reclaim */ 232 vgone(vp); 233 vput(vp); 234 /* If we didn't get it, return no vnode. */ 235 if (error) 236 vp2 = NULLVP; 237 *vpp = vp2; 238 return (error); 239 } 240 } 241 242 /* If we came here, we can insert it safely. */ 243 LIST_INSERT_HEAD(fc, fd, fd_hash); 244 mtx_unlock(&fdesc_hashmtx); 245 *vpp = vp; 246 return (0); 247 } 248 249 /* 250 * vp is the current namei directory 251 * ndp is the name to locate in that directory... 252 */ 253 static int 254 fdesc_lookup(ap) 255 struct vop_lookup_args /* { 256 struct vnode * a_dvp; 257 struct vnode ** a_vpp; 258 struct componentname * a_cnp; 259 } */ *ap; 260 { 261 struct vnode **vpp = ap->a_vpp; 262 struct vnode *dvp = ap->a_dvp; 263 struct componentname *cnp = ap->a_cnp; 264 char *pname = cnp->cn_nameptr; 265 struct thread *td = cnp->cn_thread; 266 struct file *fp; 267 int nlen = cnp->cn_namelen; 268 u_int fd, fd1; 269 int error; 270 struct vnode *fvp; 271 272 if ((cnp->cn_flags & ISLASTCN) && 273 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 274 error = EROFS; 275 goto bad; 276 } 277 278 if (cnp->cn_namelen == 1 && *pname == '.') { 279 *vpp = dvp; 280 VREF(dvp); 281 return (0); 282 } 283 284 if (VTOFDESC(dvp)->fd_type != Froot) { 285 error = ENOTDIR; 286 goto bad; 287 } 288 289 fd = 0; 290 /* the only time a leading 0 is acceptable is if it's "0" */ 291 if (*pname == '0' && nlen != 1) { 292 error = ENOENT; 293 goto bad; 294 } 295 while (nlen--) { 296 if (*pname < '0' || *pname > '9') { 297 error = ENOENT; 298 goto bad; 299 } 300 fd1 = 10 * fd + *pname++ - '0'; 301 if (fd1 < fd) { 302 error = ENOENT; 303 goto bad; 304 } 305 fd = fd1; 306 } 307 308 if ((error = fget(td, fd, &fp)) != 0) 309 goto bad; 310 311 /* Check if we're looking up ourselves. */ 312 if (VTOFDESC(dvp)->fd_ix == FD_DESC + fd) { 313 /* 314 * In case we're holding the last reference to the file, the dvp 315 * will be re-acquired. 316 */ 317 vhold(dvp); 318 VOP_UNLOCK(dvp, 0); 319 fdrop(fp, td); 320 321 /* Re-aquire the lock afterwards. */ 322 vn_lock(dvp, LK_RETRY | LK_EXCLUSIVE); 323 vdrop(dvp); 324 fvp = dvp; 325 } else { 326 /* 327 * Unlock our root node (dvp) when doing this, since we might 328 * deadlock since the vnode might be locked by another thread 329 * and the root vnode lock will be obtained afterwards (in case 330 * we're looking up the fd of the root vnode), which will be the 331 * opposite lock order. Vhold the root vnode first so we don't 332 * loose it. 333 */ 334 vhold(dvp); 335 VOP_UNLOCK(dvp, 0); 336 error = fdesc_allocvp(Fdesc, fd, FD_DESC + fd, dvp->v_mount, 337 &fvp); 338 fdrop(fp, td); 339 /* 340 * The root vnode must be locked last to prevent deadlock condition. 341 */ 342 vn_lock(dvp, LK_RETRY | LK_EXCLUSIVE); 343 vdrop(dvp); 344 } 345 346 if (error) 347 goto bad; 348 *vpp = fvp; 349 return (0); 350 351 bad: 352 *vpp = NULL; 353 return (error); 354 } 355 356 static int 357 fdesc_open(ap) 358 struct vop_open_args /* { 359 struct vnode *a_vp; 360 int a_mode; 361 struct ucred *a_cred; 362 struct thread *a_td; 363 } */ *ap; 364 { 365 struct vnode *vp = ap->a_vp; 366 367 if (VTOFDESC(vp)->fd_type == Froot) 368 return (0); 369 370 /* 371 * XXX Kludge: set td->td_proc->p_dupfd to contain the value of the file 372 * descriptor being sought for duplication. The error return ensures 373 * that the vnode for this device will be released by vn_open. Open 374 * will detect this special error and take the actions in dupfdopen. 375 * Other callers of vn_open or VOP_OPEN will simply report the 376 * error. 377 */ 378 ap->a_td->td_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */ 379 return (ENODEV); 380 } 381 382 static int 383 fdesc_getattr(ap) 384 struct vop_getattr_args /* { 385 struct vnode *a_vp; 386 struct vattr *a_vap; 387 struct ucred *a_cred; 388 } */ *ap; 389 { 390 struct vnode *vp = ap->a_vp; 391 struct vattr *vap = ap->a_vap; 392 393 vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; 394 vap->va_fileid = VTOFDESC(vp)->fd_ix; 395 vap->va_uid = 0; 396 vap->va_gid = 0; 397 vap->va_blocksize = DEV_BSIZE; 398 vap->va_atime.tv_sec = boottime.tv_sec; 399 vap->va_atime.tv_nsec = 0; 400 vap->va_mtime = vap->va_atime; 401 vap->va_ctime = vap->va_mtime; 402 vap->va_gen = 0; 403 vap->va_flags = 0; 404 vap->va_bytes = 0; 405 vap->va_filerev = 0; 406 407 switch (VTOFDESC(vp)->fd_type) { 408 case Froot: 409 vap->va_type = VDIR; 410 vap->va_nlink = 2; 411 vap->va_size = DEV_BSIZE; 412 vap->va_rdev = NODEV; 413 break; 414 415 case Fdesc: 416 vap->va_type = VCHR; 417 vap->va_nlink = 1; 418 vap->va_size = 0; 419 vap->va_rdev = makedev(0, vap->va_fileid); 420 break; 421 422 default: 423 panic("fdesc_getattr"); 424 break; 425 } 426 427 vp->v_type = vap->va_type; 428 return (0); 429 } 430 431 static int 432 fdesc_setattr(ap) 433 struct vop_setattr_args /* { 434 struct vnode *a_vp; 435 struct vattr *a_vap; 436 struct ucred *a_cred; 437 } */ *ap; 438 { 439 struct vattr *vap = ap->a_vap; 440 struct vnode *vp; 441 struct mount *mp; 442 struct file *fp; 443 struct thread *td = curthread; 444 unsigned fd; 445 int error; 446 447 /* 448 * Can't mess with the root vnode 449 */ 450 if (VTOFDESC(ap->a_vp)->fd_type == Froot) 451 return (EACCES); 452 453 fd = VTOFDESC(ap->a_vp)->fd_fd; 454 455 /* 456 * Allow setattr where there is an underlying vnode. 457 */ 458 error = getvnode(td->td_proc->p_fd, fd, &fp); 459 if (error) { 460 /* 461 * getvnode() returns EINVAL if the file descriptor is not 462 * backed by a vnode. Silently drop all changes except 463 * chflags(2) in this case. 464 */ 465 if (error == EINVAL) { 466 if (vap->va_flags != VNOVAL) 467 error = EOPNOTSUPP; 468 else 469 error = 0; 470 } 471 return (error); 472 } 473 vp = fp->f_vnode; 474 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) == 0) { 475 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 476 error = VOP_SETATTR(vp, ap->a_vap, ap->a_cred); 477 VOP_UNLOCK(vp, 0); 478 vn_finished_write(mp); 479 } 480 fdrop(fp, td); 481 return (error); 482 } 483 484 #define UIO_MX 16 485 486 static int 487 fdesc_readdir(ap) 488 struct vop_readdir_args /* { 489 struct vnode *a_vp; 490 struct uio *a_uio; 491 struct ucred *a_cred; 492 int *a_eofflag; 493 u_long *a_cookies; 494 int a_ncookies; 495 } */ *ap; 496 { 497 struct uio *uio = ap->a_uio; 498 struct filedesc *fdp; 499 struct dirent d; 500 struct dirent *dp = &d; 501 int error, i, off, fcnt; 502 503 if (VTOFDESC(ap->a_vp)->fd_type != Froot) 504 panic("fdesc_readdir: not dir"); 505 506 if (ap->a_ncookies != NULL) 507 *ap->a_ncookies = 0; 508 509 off = (int)uio->uio_offset; 510 if (off != uio->uio_offset || off < 0 || (u_int)off % UIO_MX != 0 || 511 uio->uio_resid < UIO_MX) 512 return (EINVAL); 513 i = (u_int)off / UIO_MX; 514 fdp = uio->uio_td->td_proc->p_fd; 515 error = 0; 516 517 fcnt = i - 2; /* The first two nodes are `.' and `..' */ 518 519 FILEDESC_SLOCK(fdp); 520 while (i < fdp->fd_nfiles + 2 && uio->uio_resid >= UIO_MX) { 521 bzero((caddr_t)dp, UIO_MX); 522 switch (i) { 523 case 0: /* `.' */ 524 case 1: /* `..' */ 525 dp->d_fileno = i + FD_ROOT; 526 dp->d_namlen = i + 1; 527 dp->d_reclen = UIO_MX; 528 bcopy("..", dp->d_name, dp->d_namlen); 529 dp->d_name[i + 1] = '\0'; 530 dp->d_type = DT_DIR; 531 break; 532 default: 533 if (fdp->fd_ofiles[fcnt] == NULL) 534 break; 535 dp->d_namlen = sprintf(dp->d_name, "%d", fcnt); 536 dp->d_reclen = UIO_MX; 537 dp->d_type = DT_UNKNOWN; 538 dp->d_fileno = i + FD_DESC; 539 break; 540 } 541 if (dp->d_namlen != 0) { 542 /* 543 * And ship to userland 544 */ 545 FILEDESC_SUNLOCK(fdp); 546 error = uiomove(dp, UIO_MX, uio); 547 if (error) 548 goto done; 549 FILEDESC_SLOCK(fdp); 550 } 551 i++; 552 fcnt++; 553 } 554 FILEDESC_SUNLOCK(fdp); 555 556 done: 557 uio->uio_offset = i * UIO_MX; 558 return (error); 559 } 560 561 static int 562 fdesc_reclaim(ap) 563 struct vop_reclaim_args /* { 564 struct vnode *a_vp; 565 } */ *ap; 566 { 567 struct vnode *vp; 568 struct fdescnode *fd; 569 570 vp = ap->a_vp; 571 fd = VTOFDESC(vp); 572 fdesc_remove_entry(fd); 573 free(vp->v_data, M_TEMP); 574 vp->v_data = NULL; 575 return (0); 576 } 577