1 /*- 2 * Copyright (c) 2001 Dag-Erling Co�dan Sm�rgrav 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer 10 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include "opt_pseudofs.h" 33 34 #include <sys/param.h> 35 #include <sys/kernel.h> 36 #include <sys/systm.h> 37 #include <sys/ctype.h> 38 #include <sys/dirent.h> 39 #include <sys/fcntl.h> 40 #include <sys/limits.h> 41 #include <sys/lock.h> 42 #include <sys/malloc.h> 43 #include <sys/mount.h> 44 #include <sys/mutex.h> 45 #include <sys/namei.h> 46 #include <sys/proc.h> 47 #include <sys/sbuf.h> 48 #include <sys/sx.h> 49 #include <sys/sysctl.h> 50 #include <sys/vnode.h> 51 52 #include <fs/pseudofs/pseudofs.h> 53 #include <fs/pseudofs/pseudofs_internal.h> 54 55 #define KASSERT_PN_IS_DIR(pn) \ 56 KASSERT((pn)->pn_type == pfstype_root || \ 57 (pn)->pn_type == pfstype_dir || \ 58 (pn)->pn_type == pfstype_procdir, \ 59 ("%s(): VDIR vnode refers to non-directory pfs_node", __func__)) 60 61 #define KASSERT_PN_IS_FILE(pn) \ 62 KASSERT((pn)->pn_type == pfstype_file, \ 63 ("%s(): VREG vnode refers to non-file pfs_node", __func__)) 64 65 #define KASSERT_PN_IS_LINK(pn) \ 66 KASSERT((pn)->pn_type == pfstype_symlink, \ 67 ("%s(): VLNK vnode refers to non-link pfs_node", __func__)) 68 69 /* 70 * Returns the fileno, adjusted for target pid 71 */ 72 static uint32_t 73 pn_fileno(struct pfs_node *pn, pid_t pid) 74 { 75 76 KASSERT(pn->pn_fileno > 0, 77 ("%s(): no fileno allocated", __func__)); 78 if (pid != NO_PID) 79 return (pn->pn_fileno * NO_PID + pid); 80 return (pn->pn_fileno); 81 } 82 83 /* 84 * Returns non-zero if given file is visible to given thread. 85 */ 86 static int 87 pfs_visible_proc(struct thread *td, struct pfs_node *pn, struct proc *proc) 88 { 89 int visible; 90 91 if (proc == NULL) 92 return (0); 93 94 PROC_LOCK_ASSERT(proc, MA_OWNED); 95 96 visible = ((proc->p_flag & P_WEXIT) == 0); 97 if (visible) 98 visible = (p_cansee(td, proc) == 0); 99 if (visible && pn->pn_vis != NULL) 100 visible = pn_vis(td, proc, pn); 101 if (!visible) 102 return (0); 103 return (1); 104 } 105 106 static int 107 pfs_visible(struct thread *td, struct pfs_node *pn, pid_t pid, struct proc **p) 108 { 109 struct proc *proc; 110 111 PFS_TRACE(("%s (pid: %d, req: %d)", 112 pn->pn_name, pid, td->td_proc->p_pid)); 113 114 if (p) 115 *p = NULL; 116 if (pid == NO_PID) 117 PFS_RETURN (1); 118 if ((proc = pfind(pid)) == NULL) 119 PFS_RETURN (0); 120 if (pfs_visible_proc(td, pn, proc)) { 121 if (p) 122 *p = proc; 123 else 124 PROC_UNLOCK(proc); 125 PFS_RETURN (1); 126 } 127 PROC_UNLOCK(proc); 128 PFS_RETURN (0); 129 } 130 131 /* 132 * Verify permissions 133 */ 134 static int 135 pfs_access(struct vop_access_args *va) 136 { 137 struct vnode *vn = va->a_vp; 138 struct pfs_vdata *pvd = vn->v_data; 139 struct vattr vattr; 140 int error; 141 142 PFS_TRACE(("%s", pvd->pvd_pn->pn_name)); 143 (void)pvd; 144 145 error = VOP_GETATTR(vn, &vattr, va->a_cred); 146 if (error) 147 PFS_RETURN (error); 148 error = vaccess(vn->v_type, vattr.va_mode, vattr.va_uid, 149 vattr.va_gid, va->a_accmode, va->a_cred, NULL); 150 PFS_RETURN (error); 151 } 152 153 /* 154 * Close a file or directory 155 */ 156 static int 157 pfs_close(struct vop_close_args *va) 158 { 159 struct vnode *vn = va->a_vp; 160 struct pfs_vdata *pvd = vn->v_data; 161 struct pfs_node *pn = pvd->pvd_pn; 162 struct proc *proc; 163 int error; 164 165 PFS_TRACE(("%s", pn->pn_name)); 166 pfs_assert_not_owned(pn); 167 168 /* 169 * Do nothing unless this is the last close and the node has a 170 * last-close handler. 171 */ 172 if (vrefcnt(vn) > 1 || pn->pn_close == NULL) 173 PFS_RETURN (0); 174 175 if (pvd->pvd_pid != NO_PID) { 176 proc = pfind(pvd->pvd_pid); 177 } else { 178 proc = NULL; 179 } 180 181 error = pn_close(va->a_td, proc, pn); 182 183 if (proc != NULL) 184 PROC_UNLOCK(proc); 185 186 PFS_RETURN (error); 187 } 188 189 /* 190 * Get file attributes 191 */ 192 static int 193 pfs_getattr(struct vop_getattr_args *va) 194 { 195 struct vnode *vn = va->a_vp; 196 struct pfs_vdata *pvd = vn->v_data; 197 struct pfs_node *pn = pvd->pvd_pn; 198 struct vattr *vap = va->a_vap; 199 struct proc *proc; 200 int error = 0; 201 202 PFS_TRACE(("%s", pn->pn_name)); 203 pfs_assert_not_owned(pn); 204 205 if (!pfs_visible(curthread, pn, pvd->pvd_pid, &proc)) 206 PFS_RETURN (ENOENT); 207 208 vap->va_type = vn->v_type; 209 vap->va_fileid = pn_fileno(pn, pvd->pvd_pid); 210 vap->va_flags = 0; 211 vap->va_blocksize = PAGE_SIZE; 212 vap->va_bytes = vap->va_size = 0; 213 vap->va_filerev = 0; 214 vap->va_fsid = vn->v_mount->mnt_stat.f_fsid.val[0]; 215 vap->va_nlink = 1; 216 nanotime(&vap->va_ctime); 217 vap->va_atime = vap->va_mtime = vap->va_ctime; 218 219 switch (pn->pn_type) { 220 case pfstype_procdir: 221 case pfstype_root: 222 case pfstype_dir: 223 #if 0 224 pfs_lock(pn); 225 /* compute link count */ 226 pfs_unlock(pn); 227 #endif 228 vap->va_mode = 0555; 229 break; 230 case pfstype_file: 231 case pfstype_symlink: 232 vap->va_mode = 0444; 233 break; 234 default: 235 printf("shouldn't be here!\n"); 236 vap->va_mode = 0; 237 break; 238 } 239 240 if (proc != NULL) { 241 vap->va_uid = proc->p_ucred->cr_ruid; 242 vap->va_gid = proc->p_ucred->cr_rgid; 243 } else { 244 vap->va_uid = 0; 245 vap->va_gid = 0; 246 } 247 248 if (pn->pn_attr != NULL) 249 error = pn_attr(curthread, proc, pn, vap); 250 251 if(proc != NULL) 252 PROC_UNLOCK(proc); 253 254 PFS_RETURN (error); 255 } 256 257 /* 258 * Perform an ioctl 259 */ 260 static int 261 pfs_ioctl(struct vop_ioctl_args *va) 262 { 263 struct vnode *vn; 264 struct pfs_vdata *pvd; 265 struct pfs_node *pn; 266 struct proc *proc; 267 int error; 268 269 vn = va->a_vp; 270 vn_lock(vn, LK_SHARED | LK_RETRY); 271 if (vn->v_iflag & VI_DOOMED) { 272 VOP_UNLOCK(vn, 0); 273 return (EBADF); 274 } 275 pvd = vn->v_data; 276 pn = pvd->pvd_pn; 277 278 PFS_TRACE(("%s: %lx", pn->pn_name, va->a_command)); 279 pfs_assert_not_owned(pn); 280 281 if (vn->v_type != VREG) { 282 VOP_UNLOCK(vn, 0); 283 PFS_RETURN (EINVAL); 284 } 285 KASSERT_PN_IS_FILE(pn); 286 287 if (pn->pn_ioctl == NULL) { 288 VOP_UNLOCK(vn, 0); 289 PFS_RETURN (ENOTTY); 290 } 291 292 /* 293 * This is necessary because process' privileges may 294 * have changed since the open() call. 295 */ 296 if (!pfs_visible(curthread, pn, pvd->pvd_pid, &proc)) { 297 VOP_UNLOCK(vn, 0); 298 PFS_RETURN (EIO); 299 } 300 301 error = pn_ioctl(curthread, proc, pn, va->a_command, va->a_data); 302 303 if (proc != NULL) 304 PROC_UNLOCK(proc); 305 306 VOP_UNLOCK(vn, 0); 307 PFS_RETURN (error); 308 } 309 310 /* 311 * Perform getextattr 312 */ 313 static int 314 pfs_getextattr(struct vop_getextattr_args *va) 315 { 316 struct vnode *vn = va->a_vp; 317 struct pfs_vdata *pvd = vn->v_data; 318 struct pfs_node *pn = pvd->pvd_pn; 319 struct proc *proc; 320 int error; 321 322 PFS_TRACE(("%s", pn->pn_name)); 323 pfs_assert_not_owned(pn); 324 325 /* 326 * This is necessary because either process' privileges may 327 * have changed since the open() call. 328 */ 329 if (!pfs_visible(curthread, pn, pvd->pvd_pid, &proc)) 330 PFS_RETURN (EIO); 331 332 if (pn->pn_getextattr == NULL) 333 error = EOPNOTSUPP; 334 else 335 error = pn_getextattr(curthread, proc, pn, 336 va->a_attrnamespace, va->a_name, va->a_uio, 337 va->a_size, va->a_cred); 338 339 if (proc != NULL) 340 PROC_UNLOCK(proc); 341 342 PFS_RETURN (error); 343 } 344 345 /* 346 * Convert a vnode to its component name 347 */ 348 static int 349 pfs_vptocnp(struct vop_vptocnp_args *ap) 350 { 351 struct vnode *vp = ap->a_vp; 352 struct vnode **dvp = ap->a_vpp; 353 struct pfs_vdata *pvd = vp->v_data; 354 struct pfs_node *pd = pvd->pvd_pn; 355 struct pfs_node *pn; 356 struct mount *mp; 357 char *buf = ap->a_buf; 358 int *buflen = ap->a_buflen; 359 char pidbuf[PFS_NAMELEN]; 360 pid_t pid = pvd->pvd_pid; 361 int len, i, error, locked; 362 363 i = *buflen; 364 error = 0; 365 366 pfs_lock(pd); 367 368 if (vp->v_type == VDIR && pd->pn_type == pfstype_root) { 369 *dvp = vp; 370 vhold(*dvp); 371 pfs_unlock(pd); 372 PFS_RETURN (0); 373 } else if (vp->v_type == VDIR && pd->pn_type == pfstype_procdir) { 374 len = snprintf(pidbuf, sizeof(pidbuf), "%d", pid); 375 i -= len; 376 if (i < 0) { 377 error = ENOMEM; 378 goto failed; 379 } 380 bcopy(pidbuf, buf + i, len); 381 } else { 382 len = strlen(pd->pn_name); 383 i -= len; 384 if (i < 0) { 385 error = ENOMEM; 386 goto failed; 387 } 388 bcopy(pd->pn_name, buf + i, len); 389 } 390 391 pn = pd->pn_parent; 392 pfs_unlock(pd); 393 394 mp = vp->v_mount; 395 error = vfs_busy(mp, 0); 396 if (error) 397 return (error); 398 399 /* 400 * vp is held by caller. 401 */ 402 locked = VOP_ISLOCKED(vp); 403 VOP_UNLOCK(vp, 0); 404 405 error = pfs_vncache_alloc(mp, dvp, pn, pid); 406 if (error) { 407 vn_lock(vp, locked | LK_RETRY); 408 vfs_unbusy(mp); 409 PFS_RETURN(error); 410 } 411 412 *buflen = i; 413 vhold(*dvp); 414 vput(*dvp); 415 vn_lock(vp, locked | LK_RETRY); 416 vfs_unbusy(mp); 417 418 PFS_RETURN (0); 419 failed: 420 pfs_unlock(pd); 421 PFS_RETURN(error); 422 } 423 424 /* 425 * Look up a file or directory 426 */ 427 static int 428 pfs_lookup(struct vop_cachedlookup_args *va) 429 { 430 struct vnode *vn = va->a_dvp; 431 struct vnode **vpp = va->a_vpp; 432 struct componentname *cnp = va->a_cnp; 433 struct pfs_vdata *pvd = vn->v_data; 434 struct pfs_node *pd = pvd->pvd_pn; 435 struct pfs_node *pn, *pdn = NULL; 436 pid_t pid = pvd->pvd_pid; 437 char *pname; 438 int error, i, namelen, visible; 439 440 PFS_TRACE(("%.*s", (int)cnp->cn_namelen, cnp->cn_nameptr)); 441 pfs_assert_not_owned(pd); 442 443 if (vn->v_type != VDIR) 444 PFS_RETURN (ENOTDIR); 445 KASSERT_PN_IS_DIR(pd); 446 447 error = VOP_ACCESS(vn, VEXEC, cnp->cn_cred, cnp->cn_thread); 448 if (error) 449 PFS_RETURN (error); 450 451 /* 452 * Don't support DELETE or RENAME. CREATE is supported so 453 * that O_CREAT will work, but the lookup will still fail if 454 * the file does not exist. 455 */ 456 if ((cnp->cn_flags & ISLASTCN) && 457 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) 458 PFS_RETURN (EOPNOTSUPP); 459 460 /* shortcut: check if the name is too long */ 461 if (cnp->cn_namelen >= PFS_NAMELEN) 462 PFS_RETURN (ENOENT); 463 464 /* check that parent directory is visible... */ 465 if (!pfs_visible(curthread, pd, pvd->pvd_pid, NULL)) 466 PFS_RETURN (ENOENT); 467 468 /* self */ 469 namelen = cnp->cn_namelen; 470 pname = cnp->cn_nameptr; 471 if (namelen == 1 && pname[0] == '.') { 472 pn = pd; 473 *vpp = vn; 474 VREF(vn); 475 PFS_RETURN (0); 476 } 477 478 /* parent */ 479 if (cnp->cn_flags & ISDOTDOT) { 480 if (pd->pn_type == pfstype_root) 481 PFS_RETURN (EIO); 482 VOP_UNLOCK(vn, 0); 483 KASSERT(pd->pn_parent != NULL, 484 ("%s(): non-root directory has no parent", __func__)); 485 /* 486 * This one is tricky. Descendents of procdir nodes 487 * inherit their parent's process affinity, but 488 * there's no easy reverse mapping. For simplicity, 489 * we assume that if this node is a procdir, its 490 * parent isn't (which is correct as long as 491 * descendents of procdir nodes are never procdir 492 * nodes themselves) 493 */ 494 if (pd->pn_type == pfstype_procdir) 495 pid = NO_PID; 496 pfs_lock(pd); 497 pn = pd->pn_parent; 498 pfs_unlock(pd); 499 goto got_pnode; 500 } 501 502 pfs_lock(pd); 503 504 /* named node */ 505 for (pn = pd->pn_nodes; pn != NULL; pn = pn->pn_next) 506 if (pn->pn_type == pfstype_procdir) 507 pdn = pn; 508 else if (pn->pn_name[namelen] == '\0' && 509 bcmp(pname, pn->pn_name, namelen) == 0) { 510 pfs_unlock(pd); 511 goto got_pnode; 512 } 513 514 /* process dependent node */ 515 if ((pn = pdn) != NULL) { 516 pid = 0; 517 for (pid = 0, i = 0; i < namelen && isdigit(pname[i]); ++i) 518 if ((pid = pid * 10 + pname[i] - '0') > PID_MAX) 519 break; 520 if (i == cnp->cn_namelen) { 521 pfs_unlock(pd); 522 goto got_pnode; 523 } 524 } 525 526 pfs_unlock(pd); 527 528 PFS_RETURN (ENOENT); 529 530 got_pnode: 531 pfs_assert_not_owned(pd); 532 pfs_assert_not_owned(pn); 533 visible = pfs_visible(curthread, pn, pid, NULL); 534 if (!visible) { 535 error = ENOENT; 536 goto failed; 537 } 538 539 error = pfs_vncache_alloc(vn->v_mount, vpp, pn, pid); 540 if (error) 541 goto failed; 542 543 if (cnp->cn_flags & ISDOTDOT) 544 vn_lock(vn, LK_EXCLUSIVE|LK_RETRY); 545 if (cnp->cn_flags & MAKEENTRY) 546 cache_enter(vn, *vpp, cnp); 547 PFS_RETURN (0); 548 failed: 549 if (cnp->cn_flags & ISDOTDOT) 550 vn_lock(vn, LK_EXCLUSIVE|LK_RETRY); 551 PFS_RETURN(error); 552 } 553 554 /* 555 * Open a file or directory. 556 */ 557 static int 558 pfs_open(struct vop_open_args *va) 559 { 560 struct vnode *vn = va->a_vp; 561 struct pfs_vdata *pvd = vn->v_data; 562 struct pfs_node *pn = pvd->pvd_pn; 563 int mode = va->a_mode; 564 565 PFS_TRACE(("%s (mode 0x%x)", pn->pn_name, mode)); 566 pfs_assert_not_owned(pn); 567 568 /* check if the requested mode is permitted */ 569 if (((mode & FREAD) && !(mode & PFS_RD)) || 570 ((mode & FWRITE) && !(mode & PFS_WR))) 571 PFS_RETURN (EPERM); 572 573 /* we don't support locking */ 574 if ((mode & O_SHLOCK) || (mode & O_EXLOCK)) 575 PFS_RETURN (EOPNOTSUPP); 576 577 PFS_RETURN (0); 578 } 579 580 /* 581 * Read from a file 582 */ 583 static int 584 pfs_read(struct vop_read_args *va) 585 { 586 struct vnode *vn = va->a_vp; 587 struct pfs_vdata *pvd = vn->v_data; 588 struct pfs_node *pn = pvd->pvd_pn; 589 struct uio *uio = va->a_uio; 590 struct proc *proc; 591 struct sbuf *sb = NULL; 592 int error, locked; 593 unsigned int buflen, offset, resid; 594 595 PFS_TRACE(("%s", pn->pn_name)); 596 pfs_assert_not_owned(pn); 597 598 if (vn->v_type != VREG) 599 PFS_RETURN (EINVAL); 600 KASSERT_PN_IS_FILE(pn); 601 602 if (!(pn->pn_flags & PFS_RD)) 603 PFS_RETURN (EBADF); 604 605 if (pn->pn_fill == NULL) 606 PFS_RETURN (EIO); 607 608 /* 609 * This is necessary because either process' privileges may 610 * have changed since the open() call. 611 */ 612 if (!pfs_visible(curthread, pn, pvd->pvd_pid, &proc)) 613 PFS_RETURN (EIO); 614 if (proc != NULL) { 615 _PHOLD(proc); 616 PROC_UNLOCK(proc); 617 } 618 619 vhold(vn); 620 locked = VOP_ISLOCKED(vn); 621 VOP_UNLOCK(vn, 0); 622 623 if (pn->pn_flags & PFS_RAWRD) { 624 PFS_TRACE(("%zd resid", uio->uio_resid)); 625 error = pn_fill(curthread, proc, pn, NULL, uio); 626 PFS_TRACE(("%zd resid", uio->uio_resid)); 627 goto ret; 628 } 629 630 /* beaucoup sanity checks so we don't ask for bogus allocation */ 631 if (uio->uio_offset < 0 || uio->uio_resid < 0 || 632 (offset = uio->uio_offset) != uio->uio_offset || 633 (resid = uio->uio_resid) != uio->uio_resid || 634 (buflen = offset + resid + 1) < offset || buflen > INT_MAX) { 635 if (proc != NULL) 636 PRELE(proc); 637 error = EINVAL; 638 goto ret; 639 } 640 if (buflen > MAXPHYS + 1) 641 buflen = MAXPHYS + 1; 642 643 sb = sbuf_new(sb, NULL, buflen, 0); 644 if (sb == NULL) { 645 error = EIO; 646 goto ret; 647 } 648 649 error = pn_fill(curthread, proc, pn, sb, uio); 650 651 if (error) { 652 sbuf_delete(sb); 653 goto ret; 654 } 655 656 sbuf_finish(sb); 657 error = uiomove_frombuf(sbuf_data(sb), sbuf_len(sb), uio); 658 sbuf_delete(sb); 659 ret: 660 vn_lock(vn, locked | LK_RETRY); 661 vdrop(vn); 662 if (proc != NULL) 663 PRELE(proc); 664 PFS_RETURN (error); 665 } 666 667 /* 668 * Iterate through directory entries 669 */ 670 static int 671 pfs_iterate(struct thread *td, struct proc *proc, struct pfs_node *pd, 672 struct pfs_node **pn, struct proc **p) 673 { 674 int visible; 675 676 sx_assert(&allproc_lock, SX_SLOCKED); 677 pfs_assert_owned(pd); 678 again: 679 if (*pn == NULL) { 680 /* first node */ 681 *pn = pd->pn_nodes; 682 } else if ((*pn)->pn_type != pfstype_procdir) { 683 /* next node */ 684 *pn = (*pn)->pn_next; 685 } 686 if (*pn != NULL && (*pn)->pn_type == pfstype_procdir) { 687 /* next process */ 688 if (*p == NULL) 689 *p = LIST_FIRST(&allproc); 690 else 691 *p = LIST_NEXT(*p, p_list); 692 /* out of processes: next node */ 693 if (*p == NULL) 694 *pn = (*pn)->pn_next; 695 else 696 PROC_LOCK(*p); 697 } 698 699 if ((*pn) == NULL) 700 return (-1); 701 702 if (*p != NULL) { 703 visible = pfs_visible_proc(td, *pn, *p); 704 PROC_UNLOCK(*p); 705 } else if (proc != NULL) { 706 visible = pfs_visible_proc(td, *pn, proc); 707 } else { 708 visible = 1; 709 } 710 if (!visible) 711 goto again; 712 713 return (0); 714 } 715 716 /* 717 * Return directory entries. 718 */ 719 static int 720 pfs_readdir(struct vop_readdir_args *va) 721 { 722 struct vnode *vn = va->a_vp; 723 struct pfs_vdata *pvd = vn->v_data; 724 struct pfs_node *pd = pvd->pvd_pn; 725 pid_t pid = pvd->pvd_pid; 726 struct proc *p, *proc; 727 struct pfs_node *pn; 728 struct dirent *entry; 729 struct uio *uio; 730 off_t offset; 731 int error, i, resid; 732 char *buf, *ent; 733 734 KASSERT(pd->pn_info == vn->v_mount->mnt_data, 735 ("%s(): pn_info does not match mountpoint", __func__)); 736 PFS_TRACE(("%s pid %lu", pd->pn_name, (unsigned long)pid)); 737 pfs_assert_not_owned(pd); 738 739 if (vn->v_type != VDIR) 740 PFS_RETURN (ENOTDIR); 741 KASSERT_PN_IS_DIR(pd); 742 uio = va->a_uio; 743 744 /* only allow reading entire entries */ 745 offset = uio->uio_offset; 746 resid = uio->uio_resid; 747 if (offset < 0 || offset % PFS_DELEN != 0 || 748 (resid && resid < PFS_DELEN)) 749 PFS_RETURN (EINVAL); 750 if (resid == 0) 751 PFS_RETURN (0); 752 753 /* can't do this while holding the proc lock... */ 754 buf = malloc(resid, M_IOV, M_WAITOK | M_ZERO); 755 sx_slock(&allproc_lock); 756 pfs_lock(pd); 757 758 /* check if the directory is visible to the caller */ 759 if (!pfs_visible(curthread, pd, pid, &proc)) { 760 sx_sunlock(&allproc_lock); 761 pfs_unlock(pd); 762 free(buf, M_IOV); 763 PFS_RETURN (ENOENT); 764 } 765 KASSERT(pid == NO_PID || proc != NULL, 766 ("%s(): no process for pid %lu", __func__, (unsigned long)pid)); 767 768 /* skip unwanted entries */ 769 for (pn = NULL, p = NULL; offset > 0; offset -= PFS_DELEN) { 770 if (pfs_iterate(curthread, proc, pd, &pn, &p) == -1) { 771 /* nothing left... */ 772 if (proc != NULL) 773 PROC_UNLOCK(proc); 774 pfs_unlock(pd); 775 sx_sunlock(&allproc_lock); 776 free(buf, M_IOV); 777 PFS_RETURN (0); 778 } 779 } 780 781 /* fill in entries */ 782 ent = buf; 783 while (pfs_iterate(curthread, proc, pd, &pn, &p) != -1 && 784 resid >= PFS_DELEN) { 785 entry = (struct dirent *)ent; 786 entry->d_reclen = PFS_DELEN; 787 entry->d_fileno = pn_fileno(pn, pid); 788 /* PFS_DELEN was picked to fit PFS_NAMLEN */ 789 for (i = 0; i < PFS_NAMELEN - 1 && pn->pn_name[i] != '\0'; ++i) 790 entry->d_name[i] = pn->pn_name[i]; 791 entry->d_name[i] = 0; 792 entry->d_namlen = i; 793 switch (pn->pn_type) { 794 case pfstype_procdir: 795 KASSERT(p != NULL, 796 ("reached procdir node with p == NULL")); 797 entry->d_namlen = snprintf(entry->d_name, 798 PFS_NAMELEN, "%d", p->p_pid); 799 /* fall through */ 800 case pfstype_root: 801 case pfstype_dir: 802 case pfstype_this: 803 case pfstype_parent: 804 entry->d_type = DT_DIR; 805 break; 806 case pfstype_file: 807 entry->d_type = DT_REG; 808 break; 809 case pfstype_symlink: 810 entry->d_type = DT_LNK; 811 break; 812 default: 813 panic("%s has unexpected node type: %d", pn->pn_name, pn->pn_type); 814 } 815 PFS_TRACE(("%s", entry->d_name)); 816 offset += PFS_DELEN; 817 resid -= PFS_DELEN; 818 ent += PFS_DELEN; 819 } 820 if (proc != NULL) 821 PROC_UNLOCK(proc); 822 pfs_unlock(pd); 823 sx_sunlock(&allproc_lock); 824 PFS_TRACE(("%zd bytes", ent - buf)); 825 error = uiomove(buf, ent - buf, uio); 826 free(buf, M_IOV); 827 PFS_RETURN (error); 828 } 829 830 /* 831 * Read a symbolic link 832 */ 833 static int 834 pfs_readlink(struct vop_readlink_args *va) 835 { 836 struct vnode *vn = va->a_vp; 837 struct pfs_vdata *pvd = vn->v_data; 838 struct pfs_node *pn = pvd->pvd_pn; 839 struct uio *uio = va->a_uio; 840 struct proc *proc = NULL; 841 char buf[PATH_MAX]; 842 struct sbuf sb; 843 int error, locked; 844 845 PFS_TRACE(("%s", pn->pn_name)); 846 pfs_assert_not_owned(pn); 847 848 if (vn->v_type != VLNK) 849 PFS_RETURN (EINVAL); 850 KASSERT_PN_IS_LINK(pn); 851 852 if (pn->pn_fill == NULL) 853 PFS_RETURN (EIO); 854 855 if (pvd->pvd_pid != NO_PID) { 856 if ((proc = pfind(pvd->pvd_pid)) == NULL) 857 PFS_RETURN (EIO); 858 if (proc->p_flag & P_WEXIT) { 859 PROC_UNLOCK(proc); 860 PFS_RETURN (EIO); 861 } 862 _PHOLD(proc); 863 PROC_UNLOCK(proc); 864 } 865 vhold(vn); 866 locked = VOP_ISLOCKED(vn); 867 VOP_UNLOCK(vn, 0); 868 869 /* sbuf_new() can't fail with a static buffer */ 870 sbuf_new(&sb, buf, sizeof buf, 0); 871 872 error = pn_fill(curthread, proc, pn, &sb, NULL); 873 874 if (proc != NULL) 875 PRELE(proc); 876 vn_lock(vn, locked | LK_RETRY); 877 vdrop(vn); 878 879 if (error) { 880 sbuf_delete(&sb); 881 PFS_RETURN (error); 882 } 883 884 sbuf_finish(&sb); 885 error = uiomove_frombuf(sbuf_data(&sb), sbuf_len(&sb), uio); 886 sbuf_delete(&sb); 887 PFS_RETURN (error); 888 } 889 890 /* 891 * Reclaim a vnode 892 */ 893 static int 894 pfs_reclaim(struct vop_reclaim_args *va) 895 { 896 struct vnode *vn = va->a_vp; 897 struct pfs_vdata *pvd = vn->v_data; 898 struct pfs_node *pn = pvd->pvd_pn; 899 900 PFS_TRACE(("%s", pn->pn_name)); 901 pfs_assert_not_owned(pn); 902 903 return (pfs_vncache_free(va->a_vp)); 904 } 905 906 /* 907 * Set attributes 908 */ 909 static int 910 pfs_setattr(struct vop_setattr_args *va) 911 { 912 struct vnode *vn = va->a_vp; 913 struct pfs_vdata *pvd = vn->v_data; 914 struct pfs_node *pn = pvd->pvd_pn; 915 916 PFS_TRACE(("%s", pn->pn_name)); 917 pfs_assert_not_owned(pn); 918 919 PFS_RETURN (EOPNOTSUPP); 920 } 921 922 /* 923 * Write to a file 924 */ 925 static int 926 pfs_write(struct vop_write_args *va) 927 { 928 struct vnode *vn = va->a_vp; 929 struct pfs_vdata *pvd = vn->v_data; 930 struct pfs_node *pn = pvd->pvd_pn; 931 struct uio *uio = va->a_uio; 932 struct proc *proc; 933 struct sbuf sb; 934 int error; 935 936 PFS_TRACE(("%s", pn->pn_name)); 937 pfs_assert_not_owned(pn); 938 939 if (vn->v_type != VREG) 940 PFS_RETURN (EINVAL); 941 KASSERT_PN_IS_FILE(pn); 942 943 if (!(pn->pn_flags & PFS_WR)) 944 PFS_RETURN (EBADF); 945 946 if (pn->pn_fill == NULL) 947 PFS_RETURN (EIO); 948 949 /* 950 * This is necessary because either process' privileges may 951 * have changed since the open() call. 952 */ 953 if (!pfs_visible(curthread, pn, pvd->pvd_pid, &proc)) 954 PFS_RETURN (EIO); 955 if (proc != NULL) { 956 _PHOLD(proc); 957 PROC_UNLOCK(proc); 958 } 959 960 if (pn->pn_flags & PFS_RAWWR) { 961 error = pn_fill(curthread, proc, pn, NULL, uio); 962 if (proc != NULL) 963 PRELE(proc); 964 PFS_RETURN (error); 965 } 966 967 sbuf_uionew(&sb, uio, &error); 968 if (error) { 969 if (proc != NULL) 970 PRELE(proc); 971 PFS_RETURN (error); 972 } 973 974 error = pn_fill(curthread, proc, pn, &sb, uio); 975 976 sbuf_delete(&sb); 977 if (proc != NULL) 978 PRELE(proc); 979 PFS_RETURN (error); 980 } 981 982 /* 983 * Vnode operations 984 */ 985 struct vop_vector pfs_vnodeops = { 986 .vop_default = &default_vnodeops, 987 988 .vop_access = pfs_access, 989 .vop_cachedlookup = pfs_lookup, 990 .vop_close = pfs_close, 991 .vop_create = VOP_EOPNOTSUPP, 992 .vop_getattr = pfs_getattr, 993 .vop_getextattr = pfs_getextattr, 994 .vop_ioctl = pfs_ioctl, 995 .vop_link = VOP_EOPNOTSUPP, 996 .vop_lookup = vfs_cache_lookup, 997 .vop_mkdir = VOP_EOPNOTSUPP, 998 .vop_mknod = VOP_EOPNOTSUPP, 999 .vop_open = pfs_open, 1000 .vop_read = pfs_read, 1001 .vop_readdir = pfs_readdir, 1002 .vop_readlink = pfs_readlink, 1003 .vop_reclaim = pfs_reclaim, 1004 .vop_remove = VOP_EOPNOTSUPP, 1005 .vop_rename = VOP_EOPNOTSUPP, 1006 .vop_rmdir = VOP_EOPNOTSUPP, 1007 .vop_setattr = pfs_setattr, 1008 .vop_symlink = VOP_EOPNOTSUPP, 1009 .vop_vptocnp = pfs_vptocnp, 1010 .vop_write = pfs_write, 1011 /* XXX I've probably forgotten a few that need VOP_EOPNOTSUPP */ 1012 }; 1013