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