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