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 VOP_UNLOCK(*dvp, 0); 414 vn_lock(vp, locked | LK_RETRY); 415 vfs_unbusy(mp); 416 417 PFS_RETURN (0); 418 failed: 419 pfs_unlock(pd); 420 PFS_RETURN(error); 421 } 422 423 /* 424 * Look up a file or directory 425 */ 426 static int 427 pfs_lookup(struct vop_cachedlookup_args *va) 428 { 429 struct vnode *vn = va->a_dvp; 430 struct vnode **vpp = va->a_vpp; 431 struct componentname *cnp = va->a_cnp; 432 struct pfs_vdata *pvd = vn->v_data; 433 struct pfs_node *pd = pvd->pvd_pn; 434 struct pfs_node *pn, *pdn = NULL; 435 struct mount *mp; 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 mp = vn->v_mount; 479 480 /* parent */ 481 if (cnp->cn_flags & ISDOTDOT) { 482 if (pd->pn_type == pfstype_root) 483 PFS_RETURN (EIO); 484 error = vfs_busy(mp, MBF_NOWAIT); 485 if (error != 0) { 486 vfs_ref(mp); 487 VOP_UNLOCK(vn, 0); 488 error = vfs_busy(mp, 0); 489 vn_lock(vn, LK_EXCLUSIVE | LK_RETRY); 490 vfs_rel(mp); 491 if (error != 0) 492 PFS_RETURN(ENOENT); 493 if (vn->v_iflag & VI_DOOMED) { 494 vfs_unbusy(mp); 495 PFS_RETURN(ENOENT); 496 } 497 } 498 VOP_UNLOCK(vn, 0); 499 KASSERT(pd->pn_parent != NULL, 500 ("%s(): non-root directory has no parent", __func__)); 501 /* 502 * This one is tricky. Descendents of procdir nodes 503 * inherit their parent's process affinity, but 504 * there's no easy reverse mapping. For simplicity, 505 * we assume that if this node is a procdir, its 506 * parent isn't (which is correct as long as 507 * descendents of procdir nodes are never procdir 508 * nodes themselves) 509 */ 510 if (pd->pn_type == pfstype_procdir) 511 pid = NO_PID; 512 pfs_lock(pd); 513 pn = pd->pn_parent; 514 pfs_unlock(pd); 515 goto got_pnode; 516 } 517 518 pfs_lock(pd); 519 520 /* named node */ 521 for (pn = pd->pn_nodes; pn != NULL; pn = pn->pn_next) 522 if (pn->pn_type == pfstype_procdir) 523 pdn = pn; 524 else if (pn->pn_name[namelen] == '\0' && 525 bcmp(pname, pn->pn_name, namelen) == 0) { 526 pfs_unlock(pd); 527 goto got_pnode; 528 } 529 530 /* process dependent node */ 531 if ((pn = pdn) != NULL) { 532 pid = 0; 533 for (pid = 0, i = 0; i < namelen && isdigit(pname[i]); ++i) 534 if ((pid = pid * 10 + pname[i] - '0') > PID_MAX) 535 break; 536 if (i == cnp->cn_namelen) { 537 pfs_unlock(pd); 538 goto got_pnode; 539 } 540 } 541 542 pfs_unlock(pd); 543 544 PFS_RETURN (ENOENT); 545 546 got_pnode: 547 pfs_assert_not_owned(pd); 548 pfs_assert_not_owned(pn); 549 visible = pfs_visible(curthread, pn, pid, NULL); 550 if (!visible) { 551 error = ENOENT; 552 goto failed; 553 } 554 555 error = pfs_vncache_alloc(mp, vpp, pn, pid); 556 if (error) 557 goto failed; 558 559 if (cnp->cn_flags & ISDOTDOT) { 560 vfs_unbusy(mp); 561 vn_lock(vn, LK_EXCLUSIVE | LK_RETRY); 562 if (vn->v_iflag & VI_DOOMED) { 563 vput(*vpp); 564 *vpp = NULL; 565 PFS_RETURN(ENOENT); 566 } 567 } 568 if (cnp->cn_flags & MAKEENTRY && !(vn->v_iflag & VI_DOOMED)) 569 cache_enter(vn, *vpp, cnp); 570 PFS_RETURN (0); 571 failed: 572 if (cnp->cn_flags & ISDOTDOT) { 573 vfs_unbusy(mp); 574 vn_lock(vn, LK_EXCLUSIVE | LK_RETRY); 575 *vpp = NULL; 576 } 577 PFS_RETURN(error); 578 } 579 580 /* 581 * Open a file or directory. 582 */ 583 static int 584 pfs_open(struct vop_open_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 int mode = va->a_mode; 590 591 PFS_TRACE(("%s (mode 0x%x)", pn->pn_name, mode)); 592 pfs_assert_not_owned(pn); 593 594 /* check if the requested mode is permitted */ 595 if (((mode & FREAD) && !(mode & PFS_RD)) || 596 ((mode & FWRITE) && !(mode & PFS_WR))) 597 PFS_RETURN (EPERM); 598 599 /* we don't support locking */ 600 if ((mode & O_SHLOCK) || (mode & O_EXLOCK)) 601 PFS_RETURN (EOPNOTSUPP); 602 603 PFS_RETURN (0); 604 } 605 606 /* 607 * Read from a file 608 */ 609 static int 610 pfs_read(struct vop_read_args *va) 611 { 612 struct vnode *vn = va->a_vp; 613 struct pfs_vdata *pvd = vn->v_data; 614 struct pfs_node *pn = pvd->pvd_pn; 615 struct uio *uio = va->a_uio; 616 struct proc *proc; 617 struct sbuf *sb = NULL; 618 int error, locked; 619 off_t offset; 620 ssize_t buflen, resid; 621 622 PFS_TRACE(("%s", pn->pn_name)); 623 pfs_assert_not_owned(pn); 624 625 if (vn->v_type != VREG) 626 PFS_RETURN (EINVAL); 627 KASSERT_PN_IS_FILE(pn); 628 629 if (!(pn->pn_flags & PFS_RD)) 630 PFS_RETURN (EBADF); 631 632 if (pn->pn_fill == NULL) 633 PFS_RETURN (EIO); 634 635 /* 636 * This is necessary because either process' privileges may 637 * have changed since the open() call. 638 */ 639 if (!pfs_visible(curthread, pn, pvd->pvd_pid, &proc)) 640 PFS_RETURN (EIO); 641 if (proc != NULL) { 642 _PHOLD(proc); 643 PROC_UNLOCK(proc); 644 } 645 646 vhold(vn); 647 locked = VOP_ISLOCKED(vn); 648 VOP_UNLOCK(vn, 0); 649 650 if (pn->pn_flags & PFS_RAWRD) { 651 PFS_TRACE(("%zd resid", uio->uio_resid)); 652 error = pn_fill(curthread, proc, pn, NULL, uio); 653 PFS_TRACE(("%zd resid", uio->uio_resid)); 654 goto ret; 655 } 656 657 /* beaucoup sanity checks so we don't ask for bogus allocation */ 658 if (uio->uio_offset < 0 || uio->uio_resid < 0 || 659 (offset = uio->uio_offset) != uio->uio_offset || 660 (resid = uio->uio_resid) != uio->uio_resid || 661 (buflen = offset + resid) < offset || buflen >= INT_MAX) { 662 error = EINVAL; 663 goto ret; 664 } 665 if (buflen > MAXPHYS) 666 buflen = MAXPHYS; 667 668 sb = sbuf_new(sb, NULL, buflen + 1, 0); 669 if (sb == NULL) { 670 error = EIO; 671 goto ret; 672 } 673 674 error = pn_fill(curthread, proc, pn, sb, uio); 675 676 if (error) { 677 sbuf_delete(sb); 678 goto ret; 679 } 680 681 /* 682 * XXX: If the buffer overflowed, sbuf_len() will not return 683 * the data length. Then just use the full length because an 684 * overflowed sbuf must be full. 685 */ 686 if (sbuf_finish(sb) == 0) 687 buflen = sbuf_len(sb); 688 error = uiomove_frombuf(sbuf_data(sb), buflen, uio); 689 sbuf_delete(sb); 690 ret: 691 vn_lock(vn, locked | LK_RETRY); 692 vdrop(vn); 693 if (proc != NULL) 694 PRELE(proc); 695 PFS_RETURN (error); 696 } 697 698 /* 699 * Iterate through directory entries 700 */ 701 static int 702 pfs_iterate(struct thread *td, struct proc *proc, struct pfs_node *pd, 703 struct pfs_node **pn, struct proc **p) 704 { 705 int visible; 706 707 sx_assert(&allproc_lock, SX_SLOCKED); 708 pfs_assert_owned(pd); 709 again: 710 if (*pn == NULL) { 711 /* first node */ 712 *pn = pd->pn_nodes; 713 } else if ((*pn)->pn_type != pfstype_procdir) { 714 /* next node */ 715 *pn = (*pn)->pn_next; 716 } 717 if (*pn != NULL && (*pn)->pn_type == pfstype_procdir) { 718 /* next process */ 719 if (*p == NULL) 720 *p = LIST_FIRST(&allproc); 721 else 722 *p = LIST_NEXT(*p, p_list); 723 /* out of processes: next node */ 724 if (*p == NULL) 725 *pn = (*pn)->pn_next; 726 else 727 PROC_LOCK(*p); 728 } 729 730 if ((*pn) == NULL) 731 return (-1); 732 733 if (*p != NULL) { 734 visible = pfs_visible_proc(td, *pn, *p); 735 PROC_UNLOCK(*p); 736 } else if (proc != NULL) { 737 visible = pfs_visible_proc(td, *pn, proc); 738 } else { 739 visible = 1; 740 } 741 if (!visible) 742 goto again; 743 744 return (0); 745 } 746 747 /* Directory entry list */ 748 struct pfsentry { 749 STAILQ_ENTRY(pfsentry) link; 750 struct dirent entry; 751 }; 752 STAILQ_HEAD(pfsdirentlist, pfsentry); 753 754 /* 755 * Return directory entries. 756 */ 757 static int 758 pfs_readdir(struct vop_readdir_args *va) 759 { 760 struct vnode *vn = va->a_vp; 761 struct pfs_vdata *pvd = vn->v_data; 762 struct pfs_node *pd = pvd->pvd_pn; 763 pid_t pid = pvd->pvd_pid; 764 struct proc *p, *proc; 765 struct pfs_node *pn; 766 struct uio *uio; 767 struct pfsentry *pfsent, *pfsent2; 768 struct pfsdirentlist lst; 769 off_t offset; 770 int error, i, resid; 771 772 STAILQ_INIT(&lst); 773 error = 0; 774 KASSERT(pd->pn_info == vn->v_mount->mnt_data, 775 ("%s(): pn_info does not match mountpoint", __func__)); 776 PFS_TRACE(("%s pid %lu", pd->pn_name, (unsigned long)pid)); 777 pfs_assert_not_owned(pd); 778 779 if (vn->v_type != VDIR) 780 PFS_RETURN (ENOTDIR); 781 KASSERT_PN_IS_DIR(pd); 782 uio = va->a_uio; 783 784 /* only allow reading entire entries */ 785 offset = uio->uio_offset; 786 resid = uio->uio_resid; 787 if (offset < 0 || offset % PFS_DELEN != 0 || 788 (resid && resid < PFS_DELEN)) 789 PFS_RETURN (EINVAL); 790 if (resid == 0) 791 PFS_RETURN (0); 792 793 sx_slock(&allproc_lock); 794 pfs_lock(pd); 795 796 /* check if the directory is visible to the caller */ 797 if (!pfs_visible(curthread, pd, pid, &proc)) { 798 sx_sunlock(&allproc_lock); 799 pfs_unlock(pd); 800 PFS_RETURN (ENOENT); 801 } 802 KASSERT(pid == NO_PID || proc != NULL, 803 ("%s(): no process for pid %lu", __func__, (unsigned long)pid)); 804 805 /* skip unwanted entries */ 806 for (pn = NULL, p = NULL; offset > 0; offset -= PFS_DELEN) { 807 if (pfs_iterate(curthread, proc, pd, &pn, &p) == -1) { 808 /* nothing left... */ 809 if (proc != NULL) 810 PROC_UNLOCK(proc); 811 pfs_unlock(pd); 812 sx_sunlock(&allproc_lock); 813 PFS_RETURN (0); 814 } 815 } 816 817 /* fill in entries */ 818 while (pfs_iterate(curthread, proc, pd, &pn, &p) != -1 && 819 resid >= PFS_DELEN) { 820 if ((pfsent = malloc(sizeof(struct pfsentry), M_IOV, 821 M_NOWAIT | M_ZERO)) == NULL) { 822 error = ENOMEM; 823 break; 824 } 825 pfsent->entry.d_reclen = PFS_DELEN; 826 pfsent->entry.d_fileno = pn_fileno(pn, pid); 827 /* PFS_DELEN was picked to fit PFS_NAMLEN */ 828 for (i = 0; i < PFS_NAMELEN - 1 && pn->pn_name[i] != '\0'; ++i) 829 pfsent->entry.d_name[i] = pn->pn_name[i]; 830 pfsent->entry.d_name[i] = 0; 831 pfsent->entry.d_namlen = i; 832 switch (pn->pn_type) { 833 case pfstype_procdir: 834 KASSERT(p != NULL, 835 ("reached procdir node with p == NULL")); 836 pfsent->entry.d_namlen = snprintf(pfsent->entry.d_name, 837 PFS_NAMELEN, "%d", p->p_pid); 838 /* fall through */ 839 case pfstype_root: 840 case pfstype_dir: 841 case pfstype_this: 842 case pfstype_parent: 843 pfsent->entry.d_type = DT_DIR; 844 break; 845 case pfstype_file: 846 pfsent->entry.d_type = DT_REG; 847 break; 848 case pfstype_symlink: 849 pfsent->entry.d_type = DT_LNK; 850 break; 851 default: 852 panic("%s has unexpected node type: %d", pn->pn_name, pn->pn_type); 853 } 854 PFS_TRACE(("%s", pfsent->entry.d_name)); 855 STAILQ_INSERT_TAIL(&lst, pfsent, link); 856 offset += PFS_DELEN; 857 resid -= PFS_DELEN; 858 } 859 if (proc != NULL) 860 PROC_UNLOCK(proc); 861 pfs_unlock(pd); 862 sx_sunlock(&allproc_lock); 863 i = 0; 864 STAILQ_FOREACH_SAFE(pfsent, &lst, link, pfsent2) { 865 if (error == 0) 866 error = uiomove(&pfsent->entry, PFS_DELEN, uio); 867 free(pfsent, M_IOV); 868 i++; 869 } 870 PFS_TRACE(("%d bytes", i * PFS_DELEN)); 871 PFS_RETURN (error); 872 } 873 874 /* 875 * Read a symbolic link 876 */ 877 static int 878 pfs_readlink(struct vop_readlink_args *va) 879 { 880 struct vnode *vn = va->a_vp; 881 struct pfs_vdata *pvd = vn->v_data; 882 struct pfs_node *pn = pvd->pvd_pn; 883 struct uio *uio = va->a_uio; 884 struct proc *proc = NULL; 885 char buf[PATH_MAX]; 886 struct sbuf sb; 887 int error, locked; 888 889 PFS_TRACE(("%s", pn->pn_name)); 890 pfs_assert_not_owned(pn); 891 892 if (vn->v_type != VLNK) 893 PFS_RETURN (EINVAL); 894 KASSERT_PN_IS_LINK(pn); 895 896 if (pn->pn_fill == NULL) 897 PFS_RETURN (EIO); 898 899 if (pvd->pvd_pid != NO_PID) { 900 if ((proc = pfind(pvd->pvd_pid)) == NULL) 901 PFS_RETURN (EIO); 902 if (proc->p_flag & P_WEXIT) { 903 PROC_UNLOCK(proc); 904 PFS_RETURN (EIO); 905 } 906 _PHOLD(proc); 907 PROC_UNLOCK(proc); 908 } 909 vhold(vn); 910 locked = VOP_ISLOCKED(vn); 911 VOP_UNLOCK(vn, 0); 912 913 /* sbuf_new() can't fail with a static buffer */ 914 sbuf_new(&sb, buf, sizeof buf, 0); 915 916 error = pn_fill(curthread, proc, pn, &sb, NULL); 917 918 if (proc != NULL) 919 PRELE(proc); 920 vn_lock(vn, locked | LK_RETRY); 921 vdrop(vn); 922 923 if (error) { 924 sbuf_delete(&sb); 925 PFS_RETURN (error); 926 } 927 928 if (sbuf_finish(&sb) != 0) { 929 sbuf_delete(&sb); 930 PFS_RETURN (ENAMETOOLONG); 931 } 932 933 error = uiomove_frombuf(sbuf_data(&sb), sbuf_len(&sb), uio); 934 sbuf_delete(&sb); 935 PFS_RETURN (error); 936 } 937 938 /* 939 * Reclaim a vnode 940 */ 941 static int 942 pfs_reclaim(struct vop_reclaim_args *va) 943 { 944 struct vnode *vn = va->a_vp; 945 struct pfs_vdata *pvd = vn->v_data; 946 struct pfs_node *pn = pvd->pvd_pn; 947 948 PFS_TRACE(("%s", pn->pn_name)); 949 pfs_assert_not_owned(pn); 950 951 return (pfs_vncache_free(va->a_vp)); 952 } 953 954 /* 955 * Set attributes 956 */ 957 static int 958 pfs_setattr(struct vop_setattr_args *va) 959 { 960 struct vnode *vn = va->a_vp; 961 struct pfs_vdata *pvd = vn->v_data; 962 struct pfs_node *pn = pvd->pvd_pn; 963 964 PFS_TRACE(("%s", pn->pn_name)); 965 pfs_assert_not_owned(pn); 966 967 PFS_RETURN (EOPNOTSUPP); 968 } 969 970 /* 971 * Write to a file 972 */ 973 static int 974 pfs_write(struct vop_write_args *va) 975 { 976 struct vnode *vn = va->a_vp; 977 struct pfs_vdata *pvd = vn->v_data; 978 struct pfs_node *pn = pvd->pvd_pn; 979 struct uio *uio = va->a_uio; 980 struct proc *proc; 981 struct sbuf sb; 982 int error; 983 984 PFS_TRACE(("%s", pn->pn_name)); 985 pfs_assert_not_owned(pn); 986 987 if (vn->v_type != VREG) 988 PFS_RETURN (EINVAL); 989 KASSERT_PN_IS_FILE(pn); 990 991 if (!(pn->pn_flags & PFS_WR)) 992 PFS_RETURN (EBADF); 993 994 if (pn->pn_fill == NULL) 995 PFS_RETURN (EIO); 996 997 /* 998 * This is necessary because either process' privileges may 999 * have changed since the open() call. 1000 */ 1001 if (!pfs_visible(curthread, pn, pvd->pvd_pid, &proc)) 1002 PFS_RETURN (EIO); 1003 if (proc != NULL) { 1004 _PHOLD(proc); 1005 PROC_UNLOCK(proc); 1006 } 1007 1008 if (pn->pn_flags & PFS_RAWWR) { 1009 error = pn_fill(curthread, proc, pn, NULL, uio); 1010 if (proc != NULL) 1011 PRELE(proc); 1012 PFS_RETURN (error); 1013 } 1014 1015 sbuf_uionew(&sb, uio, &error); 1016 if (error) { 1017 if (proc != NULL) 1018 PRELE(proc); 1019 PFS_RETURN (error); 1020 } 1021 1022 error = pn_fill(curthread, proc, pn, &sb, uio); 1023 1024 sbuf_delete(&sb); 1025 if (proc != NULL) 1026 PRELE(proc); 1027 PFS_RETURN (error); 1028 } 1029 1030 /* 1031 * Vnode operations 1032 */ 1033 struct vop_vector pfs_vnodeops = { 1034 .vop_default = &default_vnodeops, 1035 1036 .vop_access = pfs_access, 1037 .vop_cachedlookup = pfs_lookup, 1038 .vop_close = pfs_close, 1039 .vop_create = VOP_EOPNOTSUPP, 1040 .vop_getattr = pfs_getattr, 1041 .vop_getextattr = pfs_getextattr, 1042 .vop_ioctl = pfs_ioctl, 1043 .vop_link = VOP_EOPNOTSUPP, 1044 .vop_lookup = vfs_cache_lookup, 1045 .vop_mkdir = VOP_EOPNOTSUPP, 1046 .vop_mknod = VOP_EOPNOTSUPP, 1047 .vop_open = pfs_open, 1048 .vop_read = pfs_read, 1049 .vop_readdir = pfs_readdir, 1050 .vop_readlink = pfs_readlink, 1051 .vop_reclaim = pfs_reclaim, 1052 .vop_remove = VOP_EOPNOTSUPP, 1053 .vop_rename = VOP_EOPNOTSUPP, 1054 .vop_rmdir = VOP_EOPNOTSUPP, 1055 .vop_setattr = pfs_setattr, 1056 .vop_symlink = VOP_EOPNOTSUPP, 1057 .vop_vptocnp = pfs_vptocnp, 1058 .vop_write = pfs_write, 1059 /* XXX I've probably forgotten a few that need VOP_EOPNOTSUPP */ 1060 }; 1061