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 * 3. 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/capsicum.h> 44 #include <sys/conf.h> 45 #include <sys/dirent.h> 46 #include <sys/filedesc.h> 47 #include <sys/kernel.h> /* boottime */ 48 #include <sys/lock.h> 49 #include <sys/mutex.h> 50 #include <sys/malloc.h> 51 #include <sys/file.h> /* Must come after sys/malloc.h */ 52 #include <sys/mount.h> 53 #include <sys/namei.h> 54 #include <sys/proc.h> 55 #include <sys/stat.h> 56 #include <sys/vnode.h> 57 58 #include <fs/fdescfs/fdesc.h> 59 60 #define NFDCACHE 4 61 #define FD_NHASH(ix) \ 62 (&fdhashtbl[(ix) & fdhash]) 63 static LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl; 64 static u_long fdhash; 65 66 struct mtx fdesc_hashmtx; 67 68 static vop_getattr_t fdesc_getattr; 69 static vop_lookup_t fdesc_lookup; 70 static vop_open_t fdesc_open; 71 static vop_readdir_t fdesc_readdir; 72 static vop_readlink_t fdesc_readlink; 73 static vop_reclaim_t fdesc_reclaim; 74 static vop_setattr_t fdesc_setattr; 75 76 static struct vop_vector fdesc_vnodeops = { 77 .vop_default = &default_vnodeops, 78 79 .vop_access = VOP_NULL, 80 .vop_getattr = fdesc_getattr, 81 .vop_lookup = fdesc_lookup, 82 .vop_open = fdesc_open, 83 .vop_pathconf = vop_stdpathconf, 84 .vop_readdir = fdesc_readdir, 85 .vop_readlink = fdesc_readlink, 86 .vop_reclaim = fdesc_reclaim, 87 .vop_setattr = fdesc_setattr, 88 }; 89 90 static void fdesc_insmntque_dtr(struct vnode *, void *); 91 static void fdesc_remove_entry(struct fdescnode *); 92 93 /* 94 * Initialise cache headers 95 */ 96 int 97 fdesc_init(struct vfsconf *vfsp) 98 { 99 100 mtx_init(&fdesc_hashmtx, "fdescfs_hash", NULL, MTX_DEF); 101 fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash); 102 return (0); 103 } 104 105 /* 106 * Uninit ready for unload. 107 */ 108 int 109 fdesc_uninit(struct vfsconf *vfsp) 110 { 111 112 hashdestroy(fdhashtbl, M_CACHE, fdhash); 113 mtx_destroy(&fdesc_hashmtx); 114 return (0); 115 } 116 117 /* 118 * If allocating vnode fails, call this. 119 */ 120 static void 121 fdesc_insmntque_dtr(struct vnode *vp, void *arg) 122 { 123 124 vgone(vp); 125 vput(vp); 126 } 127 128 /* 129 * Remove an entry from the hash if it exists. 130 */ 131 static void 132 fdesc_remove_entry(struct fdescnode *fd) 133 { 134 struct fdhashhead *fc; 135 struct fdescnode *fd2; 136 137 fc = FD_NHASH(fd->fd_ix); 138 mtx_lock(&fdesc_hashmtx); 139 LIST_FOREACH(fd2, fc, fd_hash) { 140 if (fd == fd2) { 141 LIST_REMOVE(fd, fd_hash); 142 break; 143 } 144 } 145 mtx_unlock(&fdesc_hashmtx); 146 } 147 148 int 149 fdesc_allocvp(fdntype ftype, unsigned fd_fd, int ix, struct mount *mp, 150 struct vnode **vpp) 151 { 152 struct fdescmount *fmp; 153 struct fdhashhead *fc; 154 struct fdescnode *fd, *fd2; 155 struct vnode *vp, *vp2; 156 struct thread *td; 157 int error; 158 159 td = curthread; 160 fc = FD_NHASH(ix); 161 loop: 162 mtx_lock(&fdesc_hashmtx); 163 /* 164 * If a forced unmount is progressing, we need to drop it. The flags are 165 * protected by the hashmtx. 166 */ 167 fmp = mp->mnt_data; 168 if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) { 169 mtx_unlock(&fdesc_hashmtx); 170 return (-1); 171 } 172 173 LIST_FOREACH(fd, fc, fd_hash) { 174 if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) { 175 /* Get reference to vnode in case it's being free'd */ 176 vp = fd->fd_vnode; 177 VI_LOCK(vp); 178 mtx_unlock(&fdesc_hashmtx); 179 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) 180 goto loop; 181 *vpp = vp; 182 return (0); 183 } 184 } 185 mtx_unlock(&fdesc_hashmtx); 186 187 fd = malloc(sizeof(struct fdescnode), M_TEMP, M_WAITOK); 188 189 error = getnewvnode("fdescfs", mp, &fdesc_vnodeops, &vp); 190 if (error) { 191 free(fd, M_TEMP); 192 return (error); 193 } 194 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 195 vp->v_data = fd; 196 fd->fd_vnode = vp; 197 fd->fd_type = ftype; 198 fd->fd_fd = fd_fd; 199 fd->fd_ix = ix; 200 if (ftype == Fdesc && fmp->flags & FMNT_LINRDLNKF) 201 vp->v_vflag |= VV_READLINK; 202 error = insmntque1(vp, mp, fdesc_insmntque_dtr, NULL); 203 if (error != 0) { 204 *vpp = NULLVP; 205 return (error); 206 } 207 208 /* Make sure that someone didn't beat us when inserting the vnode. */ 209 mtx_lock(&fdesc_hashmtx); 210 /* 211 * If a forced unmount is progressing, we need to drop it. The flags are 212 * protected by the hashmtx. 213 */ 214 fmp = mp->mnt_data; 215 if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) { 216 mtx_unlock(&fdesc_hashmtx); 217 vgone(vp); 218 vput(vp); 219 *vpp = NULLVP; 220 return (-1); 221 } 222 223 LIST_FOREACH(fd2, fc, fd_hash) { 224 if (fd2->fd_ix == ix && fd2->fd_vnode->v_mount == mp) { 225 /* Get reference to vnode in case it's being free'd */ 226 vp2 = fd2->fd_vnode; 227 VI_LOCK(vp2); 228 mtx_unlock(&fdesc_hashmtx); 229 error = vget(vp2, LK_EXCLUSIVE | LK_INTERLOCK, td); 230 /* Someone beat us, dec use count and wait for reclaim */ 231 vgone(vp); 232 vput(vp); 233 /* If we didn't get it, return no vnode. */ 234 if (error) 235 vp2 = NULLVP; 236 *vpp = vp2; 237 return (error); 238 } 239 } 240 241 /* If we came here, we can insert it safely. */ 242 LIST_INSERT_HEAD(fc, fd, fd_hash); 243 mtx_unlock(&fdesc_hashmtx); 244 *vpp = vp; 245 return (0); 246 } 247 248 struct fdesc_get_ino_args { 249 fdntype ftype; 250 unsigned fd_fd; 251 int ix; 252 struct file *fp; 253 struct thread *td; 254 }; 255 256 static int 257 fdesc_get_ino_alloc(struct mount *mp, void *arg, int lkflags, 258 struct vnode **rvp) 259 { 260 struct fdesc_get_ino_args *a; 261 int error; 262 263 a = arg; 264 error = fdesc_allocvp(a->ftype, a->fd_fd, a->ix, mp, rvp); 265 fdrop(a->fp, a->td); 266 return (error); 267 } 268 269 270 /* 271 * vp is the current namei directory 272 * ndp is the name to locate in that directory... 273 */ 274 static int 275 fdesc_lookup(struct vop_lookup_args *ap) 276 { 277 struct vnode **vpp = ap->a_vpp; 278 struct vnode *dvp = ap->a_dvp; 279 struct componentname *cnp = ap->a_cnp; 280 char *pname = cnp->cn_nameptr; 281 struct thread *td = cnp->cn_thread; 282 struct file *fp; 283 struct fdesc_get_ino_args arg; 284 cap_rights_t rights; 285 int nlen = cnp->cn_namelen; 286 u_int fd, fd1; 287 int error; 288 struct vnode *fvp; 289 290 if ((cnp->cn_flags & ISLASTCN) && 291 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 292 error = EROFS; 293 goto bad; 294 } 295 296 if (cnp->cn_namelen == 1 && *pname == '.') { 297 *vpp = dvp; 298 VREF(dvp); 299 return (0); 300 } 301 302 if (VTOFDESC(dvp)->fd_type != Froot) { 303 error = ENOTDIR; 304 goto bad; 305 } 306 307 fd = 0; 308 /* the only time a leading 0 is acceptable is if it's "0" */ 309 if (*pname == '0' && nlen != 1) { 310 error = ENOENT; 311 goto bad; 312 } 313 while (nlen--) { 314 if (*pname < '0' || *pname > '9') { 315 error = ENOENT; 316 goto bad; 317 } 318 fd1 = 10 * fd + *pname++ - '0'; 319 if (fd1 < fd) { 320 error = ENOENT; 321 goto bad; 322 } 323 fd = fd1; 324 } 325 326 /* 327 * No rights to check since 'fp' isn't actually used. 328 */ 329 if ((error = fget(td, fd, cap_rights_init(&rights), &fp)) != 0) 330 goto bad; 331 332 /* Check if we're looking up ourselves. */ 333 if (VTOFDESC(dvp)->fd_ix == FD_DESC + fd) { 334 /* 335 * In case we're holding the last reference to the file, the dvp 336 * will be re-acquired. 337 */ 338 vhold(dvp); 339 VOP_UNLOCK(dvp, 0); 340 fdrop(fp, td); 341 342 /* Re-aquire the lock afterwards. */ 343 vn_lock(dvp, LK_RETRY | LK_EXCLUSIVE); 344 vdrop(dvp); 345 fvp = dvp; 346 if ((dvp->v_iflag & VI_DOOMED) != 0) 347 error = ENOENT; 348 } else { 349 /* 350 * Unlock our root node (dvp) when doing this, since we might 351 * deadlock since the vnode might be locked by another thread 352 * and the root vnode lock will be obtained afterwards (in case 353 * we're looking up the fd of the root vnode), which will be the 354 * opposite lock order. Vhold the root vnode first so we don't 355 * lose it. 356 */ 357 arg.ftype = Fdesc; 358 arg.fd_fd = fd; 359 arg.ix = FD_DESC + fd; 360 arg.fp = fp; 361 arg.td = td; 362 error = vn_vget_ino_gen(dvp, fdesc_get_ino_alloc, &arg, 363 LK_EXCLUSIVE, &fvp); 364 } 365 366 if (error) 367 goto bad; 368 *vpp = fvp; 369 return (0); 370 371 bad: 372 *vpp = NULL; 373 return (error); 374 } 375 376 static int 377 fdesc_open(struct vop_open_args *ap) 378 { 379 struct vnode *vp = ap->a_vp; 380 381 if (VTOFDESC(vp)->fd_type == Froot) 382 return (0); 383 384 /* 385 * XXX Kludge: set td->td_proc->p_dupfd to contain the value of the file 386 * descriptor being sought for duplication. The error return ensures 387 * that the vnode for this device will be released by vn_open. Open 388 * will detect this special error and take the actions in dupfdopen. 389 * Other callers of vn_open or VOP_OPEN will simply report the 390 * error. 391 */ 392 ap->a_td->td_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */ 393 return (ENODEV); 394 } 395 396 static int 397 fdesc_getattr(struct vop_getattr_args *ap) 398 { 399 struct vnode *vp = ap->a_vp; 400 struct vattr *vap = ap->a_vap; 401 struct timeval boottime; 402 403 getboottime(&boottime); 404 vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; 405 vap->va_fileid = VTOFDESC(vp)->fd_ix; 406 vap->va_uid = 0; 407 vap->va_gid = 0; 408 vap->va_blocksize = DEV_BSIZE; 409 vap->va_atime.tv_sec = boottime.tv_sec; 410 vap->va_atime.tv_nsec = 0; 411 vap->va_mtime = vap->va_atime; 412 vap->va_ctime = vap->va_mtime; 413 vap->va_gen = 0; 414 vap->va_flags = 0; 415 vap->va_bytes = 0; 416 vap->va_filerev = 0; 417 418 switch (VTOFDESC(vp)->fd_type) { 419 case Froot: 420 vap->va_type = VDIR; 421 vap->va_nlink = 2; 422 vap->va_size = DEV_BSIZE; 423 vap->va_rdev = NODEV; 424 break; 425 426 case Fdesc: 427 vap->va_type = (vp->v_vflag & VV_READLINK) == 0 ? VCHR : VLNK; 428 vap->va_nlink = 1; 429 vap->va_size = 0; 430 vap->va_rdev = makedev(0, vap->va_fileid); 431 break; 432 433 default: 434 panic("fdesc_getattr"); 435 break; 436 } 437 438 vp->v_type = vap->va_type; 439 return (0); 440 } 441 442 static int 443 fdesc_setattr(struct vop_setattr_args *ap) 444 { 445 struct vattr *vap = ap->a_vap; 446 struct vnode *vp; 447 struct mount *mp; 448 struct file *fp; 449 struct thread *td = curthread; 450 cap_rights_t rights; 451 unsigned fd; 452 int error; 453 454 /* 455 * Can't mess with the root vnode 456 */ 457 if (VTOFDESC(ap->a_vp)->fd_type == Froot) 458 return (EACCES); 459 460 fd = VTOFDESC(ap->a_vp)->fd_fd; 461 462 /* 463 * Allow setattr where there is an underlying vnode. 464 */ 465 error = getvnode(td, fd, 466 cap_rights_init(&rights, CAP_EXTATTR_SET), &fp); 467 if (error) { 468 /* 469 * getvnode() returns EINVAL if the file descriptor is not 470 * backed by a vnode. Silently drop all changes except 471 * chflags(2) in this case. 472 */ 473 if (error == EINVAL) { 474 if (vap->va_flags != VNOVAL) 475 error = EOPNOTSUPP; 476 else 477 error = 0; 478 } 479 return (error); 480 } 481 vp = fp->f_vnode; 482 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) == 0) { 483 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 484 error = VOP_SETATTR(vp, ap->a_vap, ap->a_cred); 485 VOP_UNLOCK(vp, 0); 486 vn_finished_write(mp); 487 } 488 fdrop(fp, td); 489 return (error); 490 } 491 492 #define UIO_MX _GENERIC_DIRLEN(10) /* number of symbols in INT_MAX printout */ 493 494 static int 495 fdesc_readdir(struct vop_readdir_args *ap) 496 { 497 struct fdescmount *fmp; 498 struct uio *uio = ap->a_uio; 499 struct filedesc *fdp; 500 struct dirent d; 501 struct dirent *dp = &d; 502 int error, i, off, fcnt; 503 504 if (VTOFDESC(ap->a_vp)->fd_type != Froot) 505 panic("fdesc_readdir: not dir"); 506 507 fmp = VFSTOFDESC(ap->a_vp->v_mount); 508 if (ap->a_ncookies != NULL) 509 *ap->a_ncookies = 0; 510 511 off = (int)uio->uio_offset; 512 if (off != uio->uio_offset || off < 0 || (u_int)off % UIO_MX != 0 || 513 uio->uio_resid < UIO_MX) 514 return (EINVAL); 515 i = (u_int)off / UIO_MX; 516 fdp = uio->uio_td->td_proc->p_fd; 517 error = 0; 518 519 fcnt = i - 2; /* The first two nodes are `.' and `..' */ 520 521 FILEDESC_SLOCK(fdp); 522 while (i < fdp->fd_nfiles + 2 && uio->uio_resid >= UIO_MX) { 523 bzero((caddr_t)dp, UIO_MX); 524 switch (i) { 525 case 0: /* `.' */ 526 case 1: /* `..' */ 527 dp->d_fileno = i + FD_ROOT; 528 dp->d_namlen = i + 1; 529 dp->d_reclen = UIO_MX; 530 bcopy("..", dp->d_name, dp->d_namlen); 531 dp->d_name[i + 1] = '\0'; 532 dp->d_type = DT_DIR; 533 break; 534 default: 535 if (fdp->fd_ofiles[fcnt].fde_file == NULL) 536 break; 537 dp->d_namlen = sprintf(dp->d_name, "%d", fcnt); 538 dp->d_reclen = UIO_MX; 539 dp->d_type = (fmp->flags & FMNT_LINRDLNKF) == 0 ? 540 DT_CHR : DT_LNK; 541 dp->d_fileno = i + FD_DESC; 542 break; 543 } 544 if (dp->d_namlen != 0) { 545 /* 546 * And ship to userland 547 */ 548 FILEDESC_SUNLOCK(fdp); 549 error = uiomove(dp, UIO_MX, uio); 550 if (error) 551 goto done; 552 FILEDESC_SLOCK(fdp); 553 } 554 i++; 555 fcnt++; 556 } 557 FILEDESC_SUNLOCK(fdp); 558 559 done: 560 uio->uio_offset = i * UIO_MX; 561 return (error); 562 } 563 564 static int 565 fdesc_reclaim(struct vop_reclaim_args *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 578 static int 579 fdesc_readlink(struct vop_readlink_args *va) 580 { 581 struct vnode *vp, *vn; 582 cap_rights_t rights; 583 struct thread *td; 584 struct uio *uio; 585 struct file *fp; 586 char *freepath, *fullpath; 587 size_t pathlen; 588 int lockflags, fd_fd; 589 int error; 590 591 freepath = NULL; 592 vn = va->a_vp; 593 if (VTOFDESC(vn)->fd_type != Fdesc) 594 panic("fdesc_readlink: not fdescfs link"); 595 fd_fd = ((struct fdescnode *)vn->v_data)->fd_fd; 596 lockflags = VOP_ISLOCKED(vn); 597 VOP_UNLOCK(vn, 0); 598 599 td = curthread; 600 error = fget_cap(td, fd_fd, cap_rights_init(&rights), &fp, NULL); 601 if (error != 0) 602 goto out; 603 604 switch (fp->f_type) { 605 case DTYPE_VNODE: 606 vp = fp->f_vnode; 607 error = vn_fullpath(td, vp, &fullpath, &freepath); 608 break; 609 default: 610 fullpath = "anon_inode:[unknown]"; 611 break; 612 } 613 if (error == 0) { 614 uio = va->a_uio; 615 pathlen = strlen(fullpath); 616 error = uiomove(fullpath, pathlen, uio); 617 } 618 if (freepath != NULL) 619 free(freepath, M_TEMP); 620 fdrop(fp, td); 621 622 out: 623 vn_lock(vn, lockflags | LK_RETRY); 624 return (error); 625 } 626