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