1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2000-2004 5 * Poul-Henning Kamp. All rights reserved. 6 * Copyright (c) 1989, 1992-1993, 1995 7 * The Regents of the University of California. All rights reserved. 8 * 9 * This code is derived from software donated to Berkeley by 10 * Jan-Simon Pendry. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)kernfs_vnops.c 8.15 (Berkeley) 5/21/95 34 * From: FreeBSD: src/sys/miscfs/kernfs/kernfs_vnops.c 1.43 35 */ 36 37 /* 38 * TODO: 39 * mkdir: want it ? 40 */ 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/conf.h> 45 #include <sys/dirent.h> 46 #include <sys/eventhandler.h> 47 #include <sys/fcntl.h> 48 #include <sys/file.h> 49 #include <sys/filedesc.h> 50 #include <sys/filio.h> 51 #include <sys/jail.h> 52 #include <sys/kernel.h> 53 #include <sys/limits.h> 54 #include <sys/lock.h> 55 #include <sys/malloc.h> 56 #include <sys/mman.h> 57 #include <sys/mount.h> 58 #include <sys/namei.h> 59 #include <sys/priv.h> 60 #include <sys/proc.h> 61 #include <sys/stat.h> 62 #include <sys/sx.h> 63 #include <sys/sysctl.h> 64 #include <sys/time.h> 65 #include <sys/ttycom.h> 66 #include <sys/unistd.h> 67 #include <sys/vnode.h> 68 69 static struct vop_vector devfs_vnodeops; 70 static struct vop_vector devfs_specops; 71 static struct fileops devfs_ops_f; 72 73 #include <fs/devfs/devfs.h> 74 #include <fs/devfs/devfs_int.h> 75 76 #include <security/mac/mac_framework.h> 77 78 #include <vm/vm.h> 79 #include <vm/vm_extern.h> 80 #include <vm/vm_object.h> 81 82 static MALLOC_DEFINE(M_CDEVPDATA, "DEVFSP", "Metainfo for cdev-fp data"); 83 84 struct mtx devfs_de_interlock; 85 MTX_SYSINIT(devfs_de_interlock, &devfs_de_interlock, "devfs interlock", MTX_DEF); 86 struct mtx cdevpriv_mtx; 87 MTX_SYSINIT(cdevpriv_mtx, &cdevpriv_mtx, "cdevpriv lock", MTX_DEF); 88 89 SYSCTL_DECL(_vfs_devfs); 90 91 static int devfs_dotimes; 92 SYSCTL_INT(_vfs_devfs, OID_AUTO, dotimes, CTLFLAG_RW, 93 &devfs_dotimes, 0, "Update timestamps on DEVFS with default precision"); 94 95 /* 96 * Update devfs node timestamp. Note that updates are unlocked and 97 * stat(2) could see partially updated times. 98 */ 99 static void 100 devfs_timestamp(struct timespec *tsp) 101 { 102 time_t ts; 103 104 if (devfs_dotimes) { 105 vfs_timestamp(tsp); 106 } else { 107 ts = time_second; 108 if (tsp->tv_sec != ts) { 109 tsp->tv_sec = ts; 110 tsp->tv_nsec = 0; 111 } 112 } 113 } 114 115 static int 116 devfs_fp_check(struct file *fp, struct cdev **devp, struct cdevsw **dswp, 117 int *ref) 118 { 119 *dswp = devvn_refthread(fp->f_vnode, devp, ref); 120 if (*dswp == NULL || *devp != fp->f_data) { 121 if (*dswp != NULL) 122 dev_relthread(*devp, *ref); 123 return (ENXIO); 124 } 125 KASSERT((*devp)->si_refcount > 0, 126 ("devfs: un-referenced struct cdev *(%s)", devtoname(*devp))); 127 if (*dswp == NULL) 128 return (ENXIO); 129 curthread->td_fpop = fp; 130 return (0); 131 } 132 133 int 134 devfs_get_cdevpriv(void **datap) 135 { 136 struct file *fp; 137 struct cdev_privdata *p; 138 int error; 139 140 fp = curthread->td_fpop; 141 if (fp == NULL) 142 return (EBADF); 143 p = fp->f_cdevpriv; 144 if (p != NULL) { 145 error = 0; 146 *datap = p->cdpd_data; 147 } else 148 error = ENOENT; 149 return (error); 150 } 151 152 int 153 devfs_set_cdevpriv(void *priv, d_priv_dtor_t *priv_dtr) 154 { 155 struct file *fp; 156 struct cdev_priv *cdp; 157 struct cdev_privdata *p; 158 int error; 159 160 fp = curthread->td_fpop; 161 if (fp == NULL) 162 return (ENOENT); 163 cdp = cdev2priv((struct cdev *)fp->f_data); 164 p = malloc(sizeof(struct cdev_privdata), M_CDEVPDATA, M_WAITOK); 165 p->cdpd_data = priv; 166 p->cdpd_dtr = priv_dtr; 167 p->cdpd_fp = fp; 168 mtx_lock(&cdevpriv_mtx); 169 if (fp->f_cdevpriv == NULL) { 170 LIST_INSERT_HEAD(&cdp->cdp_fdpriv, p, cdpd_list); 171 fp->f_cdevpriv = p; 172 mtx_unlock(&cdevpriv_mtx); 173 error = 0; 174 } else { 175 mtx_unlock(&cdevpriv_mtx); 176 free(p, M_CDEVPDATA); 177 error = EBUSY; 178 } 179 return (error); 180 } 181 182 int 183 devfs_foreach_cdevpriv(struct cdev *dev, int (*cb)(void *data, void *arg), 184 void *arg) 185 { 186 struct cdev_priv *cdp; 187 struct cdev_privdata *p; 188 int error; 189 190 cdp = cdev2priv(dev); 191 error = 0; 192 mtx_lock(&cdevpriv_mtx); 193 LIST_FOREACH(p, &cdp->cdp_fdpriv, cdpd_list) { 194 error = cb(p->cdpd_data, arg); 195 if (error != 0) 196 break; 197 } 198 mtx_unlock(&cdevpriv_mtx); 199 return (error); 200 } 201 202 void 203 devfs_destroy_cdevpriv(struct cdev_privdata *p) 204 { 205 206 mtx_assert(&cdevpriv_mtx, MA_OWNED); 207 KASSERT(p->cdpd_fp->f_cdevpriv == p, 208 ("devfs_destoy_cdevpriv %p != %p", p->cdpd_fp->f_cdevpriv, p)); 209 p->cdpd_fp->f_cdevpriv = NULL; 210 LIST_REMOVE(p, cdpd_list); 211 mtx_unlock(&cdevpriv_mtx); 212 (p->cdpd_dtr)(p->cdpd_data); 213 free(p, M_CDEVPDATA); 214 } 215 216 static void 217 devfs_fpdrop(struct file *fp) 218 { 219 struct cdev_privdata *p; 220 221 mtx_lock(&cdevpriv_mtx); 222 if ((p = fp->f_cdevpriv) == NULL) { 223 mtx_unlock(&cdevpriv_mtx); 224 return; 225 } 226 devfs_destroy_cdevpriv(p); 227 } 228 229 void 230 devfs_clear_cdevpriv(void) 231 { 232 struct file *fp; 233 234 fp = curthread->td_fpop; 235 if (fp == NULL) 236 return; 237 devfs_fpdrop(fp); 238 } 239 240 static void 241 devfs_usecount_add(struct vnode *vp) 242 { 243 struct devfs_dirent *de; 244 struct cdev *dev; 245 246 mtx_lock(&devfs_de_interlock); 247 VI_LOCK(vp); 248 VNPASS(vp->v_type == VCHR || vp->v_type == VBAD, vp); 249 if (VN_IS_DOOMED(vp)) { 250 goto out_unlock; 251 } 252 253 de = vp->v_data; 254 dev = vp->v_rdev; 255 MPASS(de != NULL); 256 MPASS(dev != NULL); 257 dev->si_usecount++; 258 de->de_usecount++; 259 out_unlock: 260 VI_UNLOCK(vp); 261 mtx_unlock(&devfs_de_interlock); 262 } 263 264 static void 265 devfs_usecount_subl(struct vnode *vp) 266 { 267 struct devfs_dirent *de; 268 struct cdev *dev; 269 270 mtx_assert(&devfs_de_interlock, MA_OWNED); 271 ASSERT_VI_LOCKED(vp, __func__); 272 VNPASS(vp->v_type == VCHR || vp->v_type == VBAD, vp); 273 274 de = vp->v_data; 275 dev = vp->v_rdev; 276 if (de == NULL) 277 return; 278 if (dev == NULL) { 279 MPASS(de->de_usecount == 0); 280 return; 281 } 282 if (dev->si_usecount < de->de_usecount) 283 panic("%s: si_usecount underflow for dev %p " 284 "(has %ld, dirent has %d)\n", 285 __func__, dev, dev->si_usecount, de->de_usecount); 286 if (VN_IS_DOOMED(vp)) { 287 dev->si_usecount -= de->de_usecount; 288 de->de_usecount = 0; 289 } else { 290 if (de->de_usecount == 0) 291 panic("%s: de_usecount underflow for dev %p\n", 292 __func__, dev); 293 dev->si_usecount--; 294 de->de_usecount--; 295 } 296 } 297 298 static void 299 devfs_usecount_sub(struct vnode *vp) 300 { 301 302 mtx_lock(&devfs_de_interlock); 303 VI_LOCK(vp); 304 devfs_usecount_subl(vp); 305 VI_UNLOCK(vp); 306 mtx_unlock(&devfs_de_interlock); 307 } 308 309 static int 310 devfs_usecountl(struct vnode *vp) 311 { 312 313 VNPASS(vp->v_type == VCHR, vp); 314 mtx_assert(&devfs_de_interlock, MA_OWNED); 315 ASSERT_VI_LOCKED(vp, __func__); 316 return (vp->v_rdev->si_usecount); 317 } 318 319 int 320 devfs_usecount(struct vnode *vp) 321 { 322 int count; 323 324 VNPASS(vp->v_type == VCHR, vp); 325 mtx_lock(&devfs_de_interlock); 326 VI_LOCK(vp); 327 count = devfs_usecountl(vp); 328 VI_UNLOCK(vp); 329 mtx_unlock(&devfs_de_interlock); 330 return (count); 331 } 332 333 void 334 devfs_ctty_ref(struct vnode *vp) 335 { 336 337 vrefact(vp); 338 devfs_usecount_add(vp); 339 } 340 341 void 342 devfs_ctty_unref(struct vnode *vp) 343 { 344 345 devfs_usecount_sub(vp); 346 vrele(vp); 347 } 348 349 /* 350 * On success devfs_populate_vp() returns with dmp->dm_lock held. 351 */ 352 static int 353 devfs_populate_vp(struct vnode *vp) 354 { 355 struct devfs_dirent *de; 356 struct devfs_mount *dmp; 357 int locked; 358 359 ASSERT_VOP_LOCKED(vp, "devfs_populate_vp"); 360 361 dmp = VFSTODEVFS(vp->v_mount); 362 if (!devfs_populate_needed(dmp)) { 363 sx_xlock(&dmp->dm_lock); 364 goto out_nopopulate; 365 } 366 367 locked = VOP_ISLOCKED(vp); 368 369 sx_xlock(&dmp->dm_lock); 370 DEVFS_DMP_HOLD(dmp); 371 372 /* Can't call devfs_populate() with the vnode lock held. */ 373 VOP_UNLOCK(vp); 374 devfs_populate(dmp); 375 376 sx_xunlock(&dmp->dm_lock); 377 vn_lock(vp, locked | LK_RETRY); 378 sx_xlock(&dmp->dm_lock); 379 if (DEVFS_DMP_DROP(dmp)) { 380 sx_xunlock(&dmp->dm_lock); 381 devfs_unmount_final(dmp); 382 return (ERESTART); 383 } 384 out_nopopulate: 385 if (VN_IS_DOOMED(vp)) { 386 sx_xunlock(&dmp->dm_lock); 387 return (ERESTART); 388 } 389 de = vp->v_data; 390 KASSERT(de != NULL, 391 ("devfs_populate_vp: vp->v_data == NULL but vnode not doomed")); 392 if ((de->de_flags & DE_DOOMED) != 0) { 393 sx_xunlock(&dmp->dm_lock); 394 return (ERESTART); 395 } 396 397 return (0); 398 } 399 400 static int 401 devfs_vptocnp(struct vop_vptocnp_args *ap) 402 { 403 struct vnode *vp = ap->a_vp; 404 struct vnode **dvp = ap->a_vpp; 405 struct devfs_mount *dmp; 406 char *buf = ap->a_buf; 407 size_t *buflen = ap->a_buflen; 408 struct devfs_dirent *dd, *de; 409 int i, error; 410 411 dmp = VFSTODEVFS(vp->v_mount); 412 413 error = devfs_populate_vp(vp); 414 if (error != 0) 415 return (error); 416 417 if (vp->v_type != VCHR && vp->v_type != VDIR) { 418 error = ENOENT; 419 goto finished; 420 } 421 422 dd = vp->v_data; 423 if (vp->v_type == VDIR && dd == dmp->dm_rootdir) { 424 *dvp = vp; 425 vref(*dvp); 426 goto finished; 427 } 428 429 i = *buflen; 430 i -= dd->de_dirent->d_namlen; 431 if (i < 0) { 432 error = ENOMEM; 433 goto finished; 434 } 435 bcopy(dd->de_dirent->d_name, buf + i, dd->de_dirent->d_namlen); 436 *buflen = i; 437 de = devfs_parent_dirent(dd); 438 if (de == NULL) { 439 error = ENOENT; 440 goto finished; 441 } 442 mtx_lock(&devfs_de_interlock); 443 *dvp = de->de_vnode; 444 if (*dvp != NULL) { 445 VI_LOCK(*dvp); 446 mtx_unlock(&devfs_de_interlock); 447 vholdl(*dvp); 448 VI_UNLOCK(*dvp); 449 vref(*dvp); 450 vdrop(*dvp); 451 } else { 452 mtx_unlock(&devfs_de_interlock); 453 error = ENOENT; 454 } 455 finished: 456 sx_xunlock(&dmp->dm_lock); 457 return (error); 458 } 459 460 /* 461 * Construct the fully qualified path name relative to the mountpoint. 462 * If a NULL cnp is provided, no '/' is appended to the resulting path. 463 */ 464 char * 465 devfs_fqpn(char *buf, struct devfs_mount *dmp, struct devfs_dirent *dd, 466 struct componentname *cnp) 467 { 468 int i; 469 struct devfs_dirent *de; 470 471 sx_assert(&dmp->dm_lock, SA_LOCKED); 472 473 i = SPECNAMELEN; 474 buf[i] = '\0'; 475 if (cnp != NULL) 476 i -= cnp->cn_namelen; 477 if (i < 0) 478 return (NULL); 479 if (cnp != NULL) 480 bcopy(cnp->cn_nameptr, buf + i, cnp->cn_namelen); 481 de = dd; 482 while (de != dmp->dm_rootdir) { 483 if (cnp != NULL || i < SPECNAMELEN) { 484 i--; 485 if (i < 0) 486 return (NULL); 487 buf[i] = '/'; 488 } 489 i -= de->de_dirent->d_namlen; 490 if (i < 0) 491 return (NULL); 492 bcopy(de->de_dirent->d_name, buf + i, 493 de->de_dirent->d_namlen); 494 de = devfs_parent_dirent(de); 495 if (de == NULL) 496 return (NULL); 497 } 498 return (buf + i); 499 } 500 501 static int 502 devfs_allocv_drop_refs(int drop_dm_lock, struct devfs_mount *dmp, 503 struct devfs_dirent *de) 504 { 505 int not_found; 506 507 not_found = 0; 508 if (de->de_flags & DE_DOOMED) 509 not_found = 1; 510 if (DEVFS_DE_DROP(de)) { 511 KASSERT(not_found == 1, ("DEVFS de dropped but not doomed")); 512 devfs_dirent_free(de); 513 } 514 if (DEVFS_DMP_DROP(dmp)) { 515 KASSERT(not_found == 1, 516 ("DEVFS mount struct freed before dirent")); 517 not_found = 2; 518 sx_xunlock(&dmp->dm_lock); 519 devfs_unmount_final(dmp); 520 } 521 if (not_found == 1 || (drop_dm_lock && not_found != 2)) 522 sx_unlock(&dmp->dm_lock); 523 return (not_found); 524 } 525 526 /* 527 * devfs_allocv shall be entered with dmp->dm_lock held, and it drops 528 * it on return. 529 */ 530 int 531 devfs_allocv(struct devfs_dirent *de, struct mount *mp, int lockmode, 532 struct vnode **vpp) 533 { 534 int error; 535 struct vnode *vp; 536 struct cdev *dev; 537 struct devfs_mount *dmp; 538 struct cdevsw *dsw; 539 enum vgetstate vs; 540 541 dmp = VFSTODEVFS(mp); 542 if (de->de_flags & DE_DOOMED) { 543 sx_xunlock(&dmp->dm_lock); 544 return (ENOENT); 545 } 546 loop: 547 DEVFS_DE_HOLD(de); 548 DEVFS_DMP_HOLD(dmp); 549 mtx_lock(&devfs_de_interlock); 550 vp = de->de_vnode; 551 if (vp != NULL) { 552 vs = vget_prep(vp); 553 mtx_unlock(&devfs_de_interlock); 554 sx_xunlock(&dmp->dm_lock); 555 vget_finish(vp, lockmode | LK_RETRY, vs); 556 sx_xlock(&dmp->dm_lock); 557 if (devfs_allocv_drop_refs(0, dmp, de)) { 558 vput(vp); 559 return (ENOENT); 560 } 561 else if (VN_IS_DOOMED(vp)) { 562 mtx_lock(&devfs_de_interlock); 563 if (de->de_vnode == vp) { 564 de->de_vnode = NULL; 565 vp->v_data = NULL; 566 } 567 mtx_unlock(&devfs_de_interlock); 568 vput(vp); 569 goto loop; 570 } 571 sx_xunlock(&dmp->dm_lock); 572 *vpp = vp; 573 return (0); 574 } 575 mtx_unlock(&devfs_de_interlock); 576 if (de->de_dirent->d_type == DT_CHR) { 577 if (!(de->de_cdp->cdp_flags & CDP_ACTIVE)) { 578 devfs_allocv_drop_refs(1, dmp, de); 579 return (ENOENT); 580 } 581 dev = &de->de_cdp->cdp_c; 582 } else { 583 dev = NULL; 584 } 585 error = getnewvnode("devfs", mp, &devfs_vnodeops, &vp); 586 if (error != 0) { 587 devfs_allocv_drop_refs(1, dmp, de); 588 printf("devfs_allocv: failed to allocate new vnode\n"); 589 return (error); 590 } 591 592 if (de->de_dirent->d_type == DT_CHR) { 593 vp->v_type = VCHR; 594 VI_LOCK(vp); 595 dev_lock(); 596 dev_refl(dev); 597 /* XXX: v_rdev should be protect by vnode lock */ 598 vp->v_rdev = dev; 599 VNPASS(vp->v_usecount == 1, vp); 600 /* Special casing of ttys for deadfs. Probably redundant. */ 601 dsw = dev->si_devsw; 602 if (dsw != NULL && (dsw->d_flags & D_TTY) != 0) 603 vp->v_vflag |= VV_ISTTY; 604 dev_unlock(); 605 VI_UNLOCK(vp); 606 if ((dev->si_flags & SI_ETERNAL) != 0) 607 vp->v_vflag |= VV_ETERNALDEV; 608 vp->v_op = &devfs_specops; 609 } else if (de->de_dirent->d_type == DT_DIR) { 610 vp->v_type = VDIR; 611 } else if (de->de_dirent->d_type == DT_LNK) { 612 vp->v_type = VLNK; 613 } else { 614 vp->v_type = VBAD; 615 } 616 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_NOWITNESS); 617 VN_LOCK_ASHARE(vp); 618 mtx_lock(&devfs_de_interlock); 619 vp->v_data = de; 620 de->de_vnode = vp; 621 mtx_unlock(&devfs_de_interlock); 622 error = insmntque1(vp, mp); 623 if (error != 0) { 624 mtx_lock(&devfs_de_interlock); 625 vp->v_data = NULL; 626 de->de_vnode = NULL; 627 mtx_unlock(&devfs_de_interlock); 628 vgone(vp); 629 vput(vp); 630 (void) devfs_allocv_drop_refs(1, dmp, de); 631 return (error); 632 } 633 if (devfs_allocv_drop_refs(0, dmp, de)) { 634 vgone(vp); 635 vput(vp); 636 return (ENOENT); 637 } 638 #ifdef MAC 639 mac_devfs_vnode_associate(mp, de, vp); 640 #endif 641 sx_xunlock(&dmp->dm_lock); 642 vn_set_state(vp, VSTATE_CONSTRUCTED); 643 *vpp = vp; 644 return (0); 645 } 646 647 static int 648 devfs_access(struct vop_access_args *ap) 649 { 650 struct vnode *vp = ap->a_vp; 651 struct devfs_dirent *de; 652 struct proc *p; 653 int error; 654 655 de = vp->v_data; 656 if (vp->v_type == VDIR) 657 de = de->de_dir; 658 659 error = vaccess(vp->v_type, de->de_mode, de->de_uid, de->de_gid, 660 ap->a_accmode, ap->a_cred); 661 if (error == 0) 662 return (0); 663 if (error != EACCES) 664 return (error); 665 p = ap->a_td->td_proc; 666 /* We do, however, allow access to the controlling terminal */ 667 PROC_LOCK(p); 668 if (!(p->p_flag & P_CONTROLT)) { 669 PROC_UNLOCK(p); 670 return (error); 671 } 672 if (p->p_session->s_ttydp == de->de_cdp) 673 error = 0; 674 PROC_UNLOCK(p); 675 return (error); 676 } 677 678 _Static_assert(((FMASK | FCNTLFLAGS) & (FLASTCLOSE | FREVOKE)) == 0, 679 "devfs-only flag reuse failed"); 680 681 static int 682 devfs_close(struct vop_close_args *ap) 683 { 684 struct vnode *vp = ap->a_vp, *oldvp; 685 struct thread *td = ap->a_td; 686 struct proc *p; 687 struct cdev *dev = vp->v_rdev; 688 struct cdevsw *dsw; 689 struct devfs_dirent *de = vp->v_data; 690 int dflags, error, ref, vp_locked; 691 692 /* 693 * XXX: Don't call d_close() if we were called because of 694 * XXX: insmntque() failure. 695 */ 696 if (vp->v_data == NULL) 697 return (0); 698 699 /* 700 * Hack: a tty device that is a controlling terminal 701 * has a reference from the session structure. 702 * We cannot easily tell that a character device is 703 * a controlling terminal, unless it is the closing 704 * process' controlling terminal. In that case, 705 * if the reference count is 2 (this last descriptor 706 * plus the session), release the reference from the session. 707 */ 708 if (de->de_usecount == 2 && td != NULL) { 709 p = td->td_proc; 710 PROC_LOCK(p); 711 if (vp == p->p_session->s_ttyvp) { 712 PROC_UNLOCK(p); 713 oldvp = NULL; 714 sx_xlock(&proctree_lock); 715 if (vp == p->p_session->s_ttyvp) { 716 SESS_LOCK(p->p_session); 717 mtx_lock(&devfs_de_interlock); 718 VI_LOCK(vp); 719 if (devfs_usecountl(vp) == 2 && !VN_IS_DOOMED(vp)) { 720 p->p_session->s_ttyvp = NULL; 721 p->p_session->s_ttydp = NULL; 722 oldvp = vp; 723 } 724 VI_UNLOCK(vp); 725 mtx_unlock(&devfs_de_interlock); 726 SESS_UNLOCK(p->p_session); 727 } 728 sx_xunlock(&proctree_lock); 729 if (oldvp != NULL) 730 devfs_ctty_unref(oldvp); 731 } else 732 PROC_UNLOCK(p); 733 } 734 /* 735 * We do not want to really close the device if it 736 * is still in use unless we are trying to close it 737 * forcibly. Since every use (buffer, vnode, swap, cmap) 738 * holds a reference to the vnode, and because we mark 739 * any other vnodes that alias this device, when the 740 * sum of the reference counts on all the aliased 741 * vnodes descends to one, we are on last close. 742 */ 743 dsw = dev_refthread(dev, &ref); 744 if (dsw == NULL) 745 return (ENXIO); 746 dflags = 0; 747 mtx_lock(&devfs_de_interlock); 748 VI_LOCK(vp); 749 if (devfs_usecountl(vp) == 1) 750 dflags |= FLASTCLOSE; 751 devfs_usecount_subl(vp); 752 mtx_unlock(&devfs_de_interlock); 753 if (VN_IS_DOOMED(vp)) { 754 /* Forced close. */ 755 dflags |= FREVOKE | FNONBLOCK; 756 } else if (dsw->d_flags & D_TRACKCLOSE) { 757 /* Keep device updated on status. */ 758 } else if ((dflags & FLASTCLOSE) == 0) { 759 VI_UNLOCK(vp); 760 dev_relthread(dev, ref); 761 return (0); 762 } 763 vholdnz(vp); 764 VI_UNLOCK(vp); 765 vp_locked = VOP_ISLOCKED(vp); 766 VOP_UNLOCK(vp); 767 KASSERT(dev->si_refcount > 0, 768 ("devfs_close() on un-referenced struct cdev *(%s)", devtoname(dev))); 769 error = dsw->d_close(dev, ap->a_fflag | dflags, S_IFCHR, td); 770 dev_relthread(dev, ref); 771 vn_lock(vp, vp_locked | LK_RETRY); 772 vdrop(vp); 773 return (error); 774 } 775 776 static int 777 devfs_close_f(struct file *fp, struct thread *td) 778 { 779 int error; 780 struct file *fpop; 781 782 /* 783 * NB: td may be NULL if this descriptor is closed due to 784 * garbage collection from a closed UNIX domain socket. 785 */ 786 fpop = curthread->td_fpop; 787 curthread->td_fpop = fp; 788 error = vnops.fo_close(fp, td); 789 curthread->td_fpop = fpop; 790 791 /* 792 * The f_cdevpriv cannot be assigned non-NULL value while we 793 * are destroying the file. 794 */ 795 if (fp->f_cdevpriv != NULL) 796 devfs_fpdrop(fp); 797 return (error); 798 } 799 800 static int 801 devfs_getattr(struct vop_getattr_args *ap) 802 { 803 struct vnode *vp = ap->a_vp; 804 struct vattr *vap = ap->a_vap; 805 struct devfs_dirent *de; 806 struct devfs_mount *dmp; 807 struct cdev *dev; 808 struct timeval boottime; 809 int error; 810 811 error = devfs_populate_vp(vp); 812 if (error != 0) 813 return (error); 814 815 dmp = VFSTODEVFS(vp->v_mount); 816 sx_xunlock(&dmp->dm_lock); 817 818 de = vp->v_data; 819 KASSERT(de != NULL, ("Null dirent in devfs_getattr vp=%p", vp)); 820 if (vp->v_type == VDIR) { 821 de = de->de_dir; 822 KASSERT(de != NULL, 823 ("Null dir dirent in devfs_getattr vp=%p", vp)); 824 } 825 vap->va_uid = de->de_uid; 826 vap->va_gid = de->de_gid; 827 vap->va_mode = de->de_mode; 828 if (vp->v_type == VLNK) 829 vap->va_size = strlen(de->de_symlink); 830 else if (vp->v_type == VDIR) 831 vap->va_size = vap->va_bytes = DEV_BSIZE; 832 else 833 vap->va_size = 0; 834 if (vp->v_type != VDIR) 835 vap->va_bytes = 0; 836 vap->va_blocksize = DEV_BSIZE; 837 vap->va_type = vp->v_type; 838 839 getboottime(&boottime); 840 #define fix(aa) \ 841 do { \ 842 if ((aa).tv_sec <= 3600) { \ 843 (aa).tv_sec = boottime.tv_sec; \ 844 (aa).tv_nsec = boottime.tv_usec * 1000; \ 845 } \ 846 } while (0) 847 848 if (vp->v_type != VCHR) { 849 fix(de->de_atime); 850 vap->va_atime = de->de_atime; 851 fix(de->de_mtime); 852 vap->va_mtime = de->de_mtime; 853 fix(de->de_ctime); 854 vap->va_ctime = de->de_ctime; 855 } else { 856 dev = vp->v_rdev; 857 fix(dev->si_atime); 858 vap->va_atime = dev->si_atime; 859 fix(dev->si_mtime); 860 vap->va_mtime = dev->si_mtime; 861 fix(dev->si_ctime); 862 vap->va_ctime = dev->si_ctime; 863 864 vap->va_rdev = cdev2priv(dev)->cdp_inode; 865 } 866 vap->va_gen = 0; 867 vap->va_flags = 0; 868 vap->va_filerev = 0; 869 vap->va_nlink = de->de_links; 870 vap->va_fileid = de->de_inode; 871 872 return (error); 873 } 874 875 /* ARGSUSED */ 876 static int 877 devfs_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, struct thread *td) 878 { 879 struct file *fpop; 880 int error; 881 882 fpop = td->td_fpop; 883 td->td_fpop = fp; 884 error = vnops.fo_ioctl(fp, com, data, cred, td); 885 td->td_fpop = fpop; 886 return (error); 887 } 888 889 void * 890 fiodgname_buf_get_ptr(void *fgnp, u_long com) 891 { 892 union { 893 struct fiodgname_arg fgn; 894 #ifdef COMPAT_FREEBSD32 895 struct fiodgname_arg32 fgn32; 896 #endif 897 } *fgnup; 898 899 fgnup = fgnp; 900 switch (com) { 901 case FIODGNAME: 902 return (fgnup->fgn.buf); 903 #ifdef COMPAT_FREEBSD32 904 case FIODGNAME_32: 905 return ((void *)(uintptr_t)fgnup->fgn32.buf); 906 #endif 907 default: 908 panic("Unhandled ioctl command %ld", com); 909 } 910 } 911 912 static int 913 devfs_ioctl(struct vop_ioctl_args *ap) 914 { 915 struct fiodgname_arg *fgn; 916 struct vnode *vpold, *vp; 917 struct cdevsw *dsw; 918 struct thread *td; 919 struct session *sess; 920 struct cdev *dev; 921 int error, ref, i; 922 const char *p; 923 u_long com; 924 925 vp = ap->a_vp; 926 com = ap->a_command; 927 td = ap->a_td; 928 929 dsw = devvn_refthread(vp, &dev, &ref); 930 if (dsw == NULL) 931 return (ENXIO); 932 KASSERT(dev->si_refcount > 0, 933 ("devfs: un-referenced struct cdev *(%s)", devtoname(dev))); 934 935 switch (com) { 936 case FIODTYPE: 937 *(int *)ap->a_data = dsw->d_flags & D_TYPEMASK; 938 error = 0; 939 break; 940 case FIODGNAME: 941 #ifdef COMPAT_FREEBSD32 942 case FIODGNAME_32: 943 #endif 944 fgn = ap->a_data; 945 p = devtoname(dev); 946 i = strlen(p) + 1; 947 if (i > fgn->len) 948 error = EINVAL; 949 else 950 error = copyout(p, fiodgname_buf_get_ptr(fgn, com), i); 951 break; 952 default: 953 error = dsw->d_ioctl(dev, com, ap->a_data, ap->a_fflag, td); 954 } 955 956 dev_relthread(dev, ref); 957 if (error == ENOIOCTL) 958 error = ENOTTY; 959 960 if (error == 0 && com == TIOCSCTTY) { 961 /* 962 * Do nothing if reassigning same control tty, or if the 963 * control tty has already disappeared. If it disappeared, 964 * it's because we were racing with TIOCNOTTY. TIOCNOTTY 965 * already took care of releasing the old vnode and we have 966 * nothing left to do. 967 */ 968 sx_slock(&proctree_lock); 969 sess = td->td_proc->p_session; 970 if (sess->s_ttyvp == vp || sess->s_ttyp == NULL) { 971 sx_sunlock(&proctree_lock); 972 return (0); 973 } 974 975 devfs_ctty_ref(vp); 976 SESS_LOCK(sess); 977 vpold = sess->s_ttyvp; 978 sess->s_ttyvp = vp; 979 sess->s_ttydp = cdev2priv(dev); 980 SESS_UNLOCK(sess); 981 982 sx_sunlock(&proctree_lock); 983 984 /* Get rid of reference to old control tty */ 985 if (vpold) 986 devfs_ctty_unref(vpold); 987 } 988 return (error); 989 } 990 991 /* ARGSUSED */ 992 static int 993 devfs_kqfilter_f(struct file *fp, struct knote *kn) 994 { 995 struct cdev *dev; 996 struct cdevsw *dsw; 997 int error, ref; 998 struct file *fpop; 999 struct thread *td; 1000 1001 td = curthread; 1002 fpop = td->td_fpop; 1003 error = devfs_fp_check(fp, &dev, &dsw, &ref); 1004 if (error) 1005 return (error); 1006 error = dsw->d_kqfilter(dev, kn); 1007 td->td_fpop = fpop; 1008 dev_relthread(dev, ref); 1009 return (error); 1010 } 1011 1012 static inline int 1013 devfs_prison_check(struct devfs_dirent *de, struct thread *td) 1014 { 1015 struct cdev_priv *cdp; 1016 struct ucred *dcr; 1017 struct proc *p; 1018 int error; 1019 1020 cdp = de->de_cdp; 1021 if (cdp == NULL) 1022 return (0); 1023 dcr = cdp->cdp_c.si_cred; 1024 if (dcr == NULL) 1025 return (0); 1026 1027 error = prison_check(td->td_ucred, dcr); 1028 if (error == 0) 1029 return (0); 1030 /* We do, however, allow access to the controlling terminal */ 1031 p = td->td_proc; 1032 PROC_LOCK(p); 1033 if (!(p->p_flag & P_CONTROLT)) { 1034 PROC_UNLOCK(p); 1035 return (error); 1036 } 1037 if (p->p_session->s_ttydp == cdp) 1038 error = 0; 1039 PROC_UNLOCK(p); 1040 return (error); 1041 } 1042 1043 static int 1044 devfs_lookupx(struct vop_lookup_args *ap, int *dm_unlock) 1045 { 1046 struct componentname *cnp; 1047 struct vnode *dvp, **vpp; 1048 struct thread *td; 1049 struct devfs_dirent *de, *dd; 1050 struct devfs_dirent **dde; 1051 struct devfs_mount *dmp; 1052 struct mount *mp; 1053 struct cdev *cdev; 1054 int error, flags, nameiop, dvplocked; 1055 char specname[SPECNAMELEN + 1], *pname; 1056 1057 td = curthread; 1058 cnp = ap->a_cnp; 1059 vpp = ap->a_vpp; 1060 dvp = ap->a_dvp; 1061 pname = cnp->cn_nameptr; 1062 flags = cnp->cn_flags; 1063 nameiop = cnp->cn_nameiop; 1064 mp = dvp->v_mount; 1065 dmp = VFSTODEVFS(mp); 1066 dd = dvp->v_data; 1067 *vpp = NULLVP; 1068 1069 if ((flags & ISLASTCN) && nameiop == RENAME) 1070 return (EOPNOTSUPP); 1071 1072 if (dvp->v_type != VDIR) 1073 return (ENOTDIR); 1074 1075 if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT)) 1076 return (EIO); 1077 1078 error = vn_dir_check_exec(dvp, cnp); 1079 if (error != 0) 1080 return (error); 1081 1082 if (cnp->cn_namelen == 1 && *pname == '.') { 1083 if ((flags & ISLASTCN) && nameiop != LOOKUP) 1084 return (EINVAL); 1085 *vpp = dvp; 1086 VREF(dvp); 1087 return (0); 1088 } 1089 1090 if (flags & ISDOTDOT) { 1091 if ((flags & ISLASTCN) && nameiop != LOOKUP) 1092 return (EINVAL); 1093 de = devfs_parent_dirent(dd); 1094 if (de == NULL) 1095 return (ENOENT); 1096 dvplocked = VOP_ISLOCKED(dvp); 1097 VOP_UNLOCK(dvp); 1098 error = devfs_allocv(de, mp, cnp->cn_lkflags & LK_TYPE_MASK, 1099 vpp); 1100 *dm_unlock = 0; 1101 vn_lock(dvp, dvplocked | LK_RETRY); 1102 return (error); 1103 } 1104 1105 dd = dvp->v_data; 1106 de = devfs_find(dd, cnp->cn_nameptr, cnp->cn_namelen, 0); 1107 while (de == NULL) { /* While(...) so we can use break */ 1108 1109 if (nameiop == DELETE) 1110 return (ENOENT); 1111 1112 /* 1113 * OK, we didn't have an entry for the name we were asked for 1114 * so we try to see if anybody can create it on demand. 1115 */ 1116 pname = devfs_fqpn(specname, dmp, dd, cnp); 1117 if (pname == NULL) 1118 break; 1119 1120 cdev = NULL; 1121 DEVFS_DMP_HOLD(dmp); 1122 sx_xunlock(&dmp->dm_lock); 1123 EVENTHANDLER_INVOKE(dev_clone, 1124 td->td_ucred, pname, strlen(pname), &cdev); 1125 1126 if (cdev == NULL) 1127 sx_xlock(&dmp->dm_lock); 1128 else if (devfs_populate_vp(dvp) != 0) { 1129 *dm_unlock = 0; 1130 sx_xlock(&dmp->dm_lock); 1131 if (DEVFS_DMP_DROP(dmp)) { 1132 sx_xunlock(&dmp->dm_lock); 1133 devfs_unmount_final(dmp); 1134 } else 1135 sx_xunlock(&dmp->dm_lock); 1136 dev_rel(cdev); 1137 return (ENOENT); 1138 } 1139 if (DEVFS_DMP_DROP(dmp)) { 1140 *dm_unlock = 0; 1141 sx_xunlock(&dmp->dm_lock); 1142 devfs_unmount_final(dmp); 1143 if (cdev != NULL) 1144 dev_rel(cdev); 1145 return (ENOENT); 1146 } 1147 1148 if (cdev == NULL) 1149 break; 1150 1151 dev_lock(); 1152 dde = &cdev2priv(cdev)->cdp_dirents[dmp->dm_idx]; 1153 if (dde != NULL && *dde != NULL) 1154 de = *dde; 1155 dev_unlock(); 1156 dev_rel(cdev); 1157 break; 1158 } 1159 1160 if (de == NULL || de->de_flags & DE_WHITEOUT) { 1161 if ((nameiop == CREATE || nameiop == RENAME) && 1162 (flags & (LOCKPARENT | WANTPARENT)) && (flags & ISLASTCN)) { 1163 return (EJUSTRETURN); 1164 } 1165 return (ENOENT); 1166 } 1167 1168 if (devfs_prison_check(de, td)) 1169 return (ENOENT); 1170 1171 if ((cnp->cn_nameiop == DELETE) && (flags & ISLASTCN)) { 1172 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td); 1173 if (error) 1174 return (error); 1175 if (*vpp == dvp) { 1176 VREF(dvp); 1177 *vpp = dvp; 1178 return (0); 1179 } 1180 } 1181 error = devfs_allocv(de, mp, cnp->cn_lkflags & LK_TYPE_MASK, vpp); 1182 *dm_unlock = 0; 1183 return (error); 1184 } 1185 1186 static int 1187 devfs_lookup(struct vop_lookup_args *ap) 1188 { 1189 int j; 1190 struct devfs_mount *dmp; 1191 int dm_unlock; 1192 1193 if (devfs_populate_vp(ap->a_dvp) != 0) 1194 return (ENOTDIR); 1195 1196 dmp = VFSTODEVFS(ap->a_dvp->v_mount); 1197 dm_unlock = 1; 1198 j = devfs_lookupx(ap, &dm_unlock); 1199 if (dm_unlock == 1) 1200 sx_xunlock(&dmp->dm_lock); 1201 return (j); 1202 } 1203 1204 static int 1205 devfs_mknod(struct vop_mknod_args *ap) 1206 { 1207 struct componentname *cnp; 1208 struct vnode *dvp, **vpp; 1209 struct devfs_dirent *dd, *de; 1210 struct devfs_mount *dmp; 1211 int error; 1212 1213 /* 1214 * The only type of node we should be creating here is a 1215 * character device, for anything else return EOPNOTSUPP. 1216 */ 1217 if (ap->a_vap->va_type != VCHR) 1218 return (EOPNOTSUPP); 1219 dvp = ap->a_dvp; 1220 dmp = VFSTODEVFS(dvp->v_mount); 1221 1222 cnp = ap->a_cnp; 1223 vpp = ap->a_vpp; 1224 dd = dvp->v_data; 1225 1226 error = ENOENT; 1227 sx_xlock(&dmp->dm_lock); 1228 TAILQ_FOREACH(de, &dd->de_dlist, de_list) { 1229 if (cnp->cn_namelen != de->de_dirent->d_namlen) 1230 continue; 1231 if (de->de_dirent->d_type == DT_CHR && 1232 (de->de_cdp->cdp_flags & CDP_ACTIVE) == 0) 1233 continue; 1234 if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name, 1235 de->de_dirent->d_namlen) != 0) 1236 continue; 1237 if (de->de_flags & DE_WHITEOUT) 1238 break; 1239 goto notfound; 1240 } 1241 if (de == NULL) 1242 goto notfound; 1243 de->de_flags &= ~DE_WHITEOUT; 1244 error = devfs_allocv(de, dvp->v_mount, LK_EXCLUSIVE, vpp); 1245 return (error); 1246 notfound: 1247 sx_xunlock(&dmp->dm_lock); 1248 return (error); 1249 } 1250 1251 /* ARGSUSED */ 1252 static int 1253 devfs_open(struct vop_open_args *ap) 1254 { 1255 struct thread *td = ap->a_td; 1256 struct vnode *vp = ap->a_vp; 1257 struct cdev *dev = vp->v_rdev; 1258 struct file *fp = ap->a_fp; 1259 int error, ref, vlocked; 1260 struct cdevsw *dsw; 1261 struct file *fpop; 1262 1263 if (vp->v_type == VBLK) 1264 return (ENXIO); 1265 1266 if (dev == NULL) 1267 return (ENXIO); 1268 1269 /* Make this field valid before any I/O in d_open. */ 1270 if (dev->si_iosize_max == 0) 1271 dev->si_iosize_max = DFLTPHYS; 1272 1273 dsw = dev_refthread(dev, &ref); 1274 if (dsw == NULL) 1275 return (ENXIO); 1276 if (fp == NULL && dsw->d_fdopen != NULL) { 1277 dev_relthread(dev, ref); 1278 return (ENXIO); 1279 } 1280 1281 if (vp->v_type == VCHR) 1282 devfs_usecount_add(vp); 1283 1284 vlocked = VOP_ISLOCKED(vp); 1285 VOP_UNLOCK(vp); 1286 1287 fpop = td->td_fpop; 1288 td->td_fpop = fp; 1289 if (fp != NULL) { 1290 fp->f_data = dev; 1291 fp->f_vnode = vp; 1292 } 1293 if (dsw->d_fdopen != NULL) 1294 error = dsw->d_fdopen(dev, ap->a_mode, td, fp); 1295 else 1296 error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td); 1297 /* Clean up any cdevpriv upon error. */ 1298 if (error != 0) 1299 devfs_clear_cdevpriv(); 1300 td->td_fpop = fpop; 1301 1302 vn_lock(vp, vlocked | LK_RETRY); 1303 if (error != 0 && vp->v_type == VCHR) 1304 devfs_usecount_sub(vp); 1305 1306 dev_relthread(dev, ref); 1307 if (error != 0) { 1308 if (error == ERESTART) 1309 error = EINTR; 1310 return (error); 1311 } 1312 1313 #if 0 /* /dev/console */ 1314 KASSERT(fp != NULL, ("Could not vnode bypass device on NULL fp")); 1315 #else 1316 if (fp == NULL) 1317 return (error); 1318 #endif 1319 if (fp->f_ops == &badfileops) 1320 finit(fp, fp->f_flag, DTYPE_VNODE, dev, &devfs_ops_f); 1321 return (error); 1322 } 1323 1324 static int 1325 devfs_pathconf(struct vop_pathconf_args *ap) 1326 { 1327 1328 switch (ap->a_name) { 1329 case _PC_FILESIZEBITS: 1330 *ap->a_retval = 64; 1331 return (0); 1332 case _PC_NAME_MAX: 1333 *ap->a_retval = NAME_MAX; 1334 return (0); 1335 case _PC_LINK_MAX: 1336 *ap->a_retval = INT_MAX; 1337 return (0); 1338 case _PC_SYMLINK_MAX: 1339 *ap->a_retval = MAXPATHLEN; 1340 return (0); 1341 case _PC_MAX_CANON: 1342 if (ap->a_vp->v_vflag & VV_ISTTY) { 1343 *ap->a_retval = MAX_CANON; 1344 return (0); 1345 } 1346 return (EINVAL); 1347 case _PC_MAX_INPUT: 1348 if (ap->a_vp->v_vflag & VV_ISTTY) { 1349 *ap->a_retval = MAX_INPUT; 1350 return (0); 1351 } 1352 return (EINVAL); 1353 case _PC_VDISABLE: 1354 if (ap->a_vp->v_vflag & VV_ISTTY) { 1355 *ap->a_retval = _POSIX_VDISABLE; 1356 return (0); 1357 } 1358 return (EINVAL); 1359 case _PC_MAC_PRESENT: 1360 #ifdef MAC 1361 /* 1362 * If MAC is enabled, devfs automatically supports 1363 * trivial non-persistent label storage. 1364 */ 1365 *ap->a_retval = 1; 1366 #else 1367 *ap->a_retval = 0; 1368 #endif 1369 return (0); 1370 case _PC_CHOWN_RESTRICTED: 1371 *ap->a_retval = 1; 1372 return (0); 1373 default: 1374 return (vop_stdpathconf(ap)); 1375 } 1376 /* NOTREACHED */ 1377 } 1378 1379 /* ARGSUSED */ 1380 static int 1381 devfs_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td) 1382 { 1383 struct cdev *dev; 1384 struct cdevsw *dsw; 1385 int error, ref; 1386 struct file *fpop; 1387 1388 fpop = td->td_fpop; 1389 error = devfs_fp_check(fp, &dev, &dsw, &ref); 1390 if (error != 0) { 1391 error = vnops.fo_poll(fp, events, cred, td); 1392 return (error); 1393 } 1394 error = dsw->d_poll(dev, events, td); 1395 td->td_fpop = fpop; 1396 dev_relthread(dev, ref); 1397 return(error); 1398 } 1399 1400 /* 1401 * Print out the contents of a special device vnode. 1402 */ 1403 static int 1404 devfs_print(struct vop_print_args *ap) 1405 { 1406 1407 printf("\tdev %s\n", devtoname(ap->a_vp->v_rdev)); 1408 return (0); 1409 } 1410 1411 static int 1412 devfs_read_f(struct file *fp, struct uio *uio, struct ucred *cred, 1413 int flags, struct thread *td) 1414 { 1415 struct cdev *dev; 1416 int ioflag, error, ref; 1417 ssize_t resid; 1418 struct cdevsw *dsw; 1419 struct file *fpop; 1420 1421 if (uio->uio_resid > DEVFS_IOSIZE_MAX) 1422 return (EINVAL); 1423 fpop = td->td_fpop; 1424 error = devfs_fp_check(fp, &dev, &dsw, &ref); 1425 if (error != 0) { 1426 error = vnops.fo_read(fp, uio, cred, flags, td); 1427 return (error); 1428 } 1429 resid = uio->uio_resid; 1430 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT); 1431 if (ioflag & O_DIRECT) 1432 ioflag |= IO_DIRECT; 1433 1434 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK); 1435 error = dsw->d_read(dev, uio, ioflag); 1436 if (uio->uio_resid != resid || (error == 0 && resid != 0)) 1437 devfs_timestamp(&dev->si_atime); 1438 td->td_fpop = fpop; 1439 dev_relthread(dev, ref); 1440 1441 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF_R); 1442 return (error); 1443 } 1444 1445 static int 1446 devfs_readdir(struct vop_readdir_args *ap) 1447 { 1448 int error; 1449 struct uio *uio; 1450 struct dirent *dp; 1451 struct devfs_dirent *dd; 1452 struct devfs_dirent *de; 1453 struct devfs_mount *dmp; 1454 off_t off; 1455 int *tmp_ncookies = NULL; 1456 1457 if (ap->a_vp->v_type != VDIR) 1458 return (ENOTDIR); 1459 1460 uio = ap->a_uio; 1461 if (uio->uio_offset < 0) 1462 return (EINVAL); 1463 1464 /* 1465 * XXX: This is a temporary hack to get around this filesystem not 1466 * supporting cookies. We store the location of the ncookies pointer 1467 * in a temporary variable before calling vfs_subr.c:vfs_read_dirent() 1468 * and set the number of cookies to 0. We then set the pointer to 1469 * NULL so that vfs_read_dirent doesn't try to call realloc() on 1470 * ap->a_cookies. Later in this function, we restore the ap->a_ncookies 1471 * pointer to its original location before returning to the caller. 1472 */ 1473 if (ap->a_ncookies != NULL) { 1474 tmp_ncookies = ap->a_ncookies; 1475 *ap->a_ncookies = 0; 1476 ap->a_ncookies = NULL; 1477 } 1478 1479 dmp = VFSTODEVFS(ap->a_vp->v_mount); 1480 if (devfs_populate_vp(ap->a_vp) != 0) { 1481 if (tmp_ncookies != NULL) 1482 ap->a_ncookies = tmp_ncookies; 1483 return (EIO); 1484 } 1485 error = 0; 1486 de = ap->a_vp->v_data; 1487 off = 0; 1488 TAILQ_FOREACH(dd, &de->de_dlist, de_list) { 1489 KASSERT(dd->de_cdp != (void *)0xdeadc0de, ("%s %d\n", __func__, __LINE__)); 1490 if (dd->de_flags & (DE_COVERED | DE_WHITEOUT)) 1491 continue; 1492 if (devfs_prison_check(dd, uio->uio_td)) 1493 continue; 1494 if (dd->de_dirent->d_type == DT_DIR) 1495 de = dd->de_dir; 1496 else 1497 de = dd; 1498 dp = dd->de_dirent; 1499 MPASS(dp->d_reclen == GENERIC_DIRSIZ(dp)); 1500 if (dp->d_reclen > uio->uio_resid) 1501 break; 1502 dp->d_fileno = de->de_inode; 1503 /* NOTE: d_off is the offset for the *next* entry. */ 1504 dp->d_off = off + dp->d_reclen; 1505 if (off >= uio->uio_offset) { 1506 error = vfs_read_dirent(ap, dp, off); 1507 if (error) 1508 break; 1509 } 1510 off += dp->d_reclen; 1511 } 1512 sx_xunlock(&dmp->dm_lock); 1513 uio->uio_offset = off; 1514 1515 /* 1516 * Restore ap->a_ncookies if it wasn't originally NULL in the first 1517 * place. 1518 */ 1519 if (tmp_ncookies != NULL) 1520 ap->a_ncookies = tmp_ncookies; 1521 1522 return (error); 1523 } 1524 1525 static int 1526 devfs_readlink(struct vop_readlink_args *ap) 1527 { 1528 struct devfs_dirent *de; 1529 1530 de = ap->a_vp->v_data; 1531 return (uiomove(de->de_symlink, strlen(de->de_symlink), ap->a_uio)); 1532 } 1533 1534 static void 1535 devfs_reclaiml(struct vnode *vp) 1536 { 1537 struct devfs_dirent *de; 1538 1539 mtx_assert(&devfs_de_interlock, MA_OWNED); 1540 de = vp->v_data; 1541 if (de != NULL) { 1542 MPASS(de->de_usecount == 0); 1543 de->de_vnode = NULL; 1544 vp->v_data = NULL; 1545 } 1546 } 1547 1548 static int 1549 devfs_reclaim(struct vop_reclaim_args *ap) 1550 { 1551 struct vnode *vp; 1552 1553 vp = ap->a_vp; 1554 mtx_lock(&devfs_de_interlock); 1555 devfs_reclaiml(vp); 1556 mtx_unlock(&devfs_de_interlock); 1557 return (0); 1558 } 1559 1560 static int 1561 devfs_reclaim_vchr(struct vop_reclaim_args *ap) 1562 { 1563 struct vnode *vp; 1564 struct cdev *dev; 1565 1566 vp = ap->a_vp; 1567 MPASS(vp->v_type == VCHR); 1568 1569 mtx_lock(&devfs_de_interlock); 1570 VI_LOCK(vp); 1571 devfs_usecount_subl(vp); 1572 devfs_reclaiml(vp); 1573 mtx_unlock(&devfs_de_interlock); 1574 dev_lock(); 1575 dev = vp->v_rdev; 1576 vp->v_rdev = NULL; 1577 dev_unlock(); 1578 VI_UNLOCK(vp); 1579 if (dev != NULL) 1580 dev_rel(dev); 1581 return (0); 1582 } 1583 1584 static int 1585 devfs_remove(struct vop_remove_args *ap) 1586 { 1587 struct vnode *dvp = ap->a_dvp; 1588 struct vnode *vp = ap->a_vp; 1589 struct devfs_dirent *dd; 1590 struct devfs_dirent *de, *de_covered; 1591 struct devfs_mount *dmp = VFSTODEVFS(vp->v_mount); 1592 1593 ASSERT_VOP_ELOCKED(dvp, "devfs_remove"); 1594 ASSERT_VOP_ELOCKED(vp, "devfs_remove"); 1595 1596 sx_xlock(&dmp->dm_lock); 1597 dd = ap->a_dvp->v_data; 1598 de = vp->v_data; 1599 if (de->de_cdp == NULL) { 1600 TAILQ_REMOVE(&dd->de_dlist, de, de_list); 1601 if (de->de_dirent->d_type == DT_LNK) { 1602 de_covered = devfs_find(dd, de->de_dirent->d_name, 1603 de->de_dirent->d_namlen, 0); 1604 if (de_covered != NULL) 1605 de_covered->de_flags &= ~DE_COVERED; 1606 } 1607 /* We need to unlock dvp because devfs_delete() may lock it. */ 1608 VOP_UNLOCK(vp); 1609 if (dvp != vp) 1610 VOP_UNLOCK(dvp); 1611 devfs_delete(dmp, de, 0); 1612 sx_xunlock(&dmp->dm_lock); 1613 if (dvp != vp) 1614 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 1615 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1616 } else { 1617 de->de_flags |= DE_WHITEOUT; 1618 sx_xunlock(&dmp->dm_lock); 1619 } 1620 return (0); 1621 } 1622 1623 /* 1624 * Revoke is called on a tty when a terminal session ends. The vnode 1625 * is orphaned by setting v_op to deadfs so we need to let go of it 1626 * as well so that we create a new one next time around. 1627 * 1628 */ 1629 static int 1630 devfs_revoke(struct vop_revoke_args *ap) 1631 { 1632 struct vnode *vp = ap->a_vp, *vp2; 1633 struct cdev *dev; 1634 struct cdev_priv *cdp; 1635 struct devfs_dirent *de; 1636 enum vgetstate vs; 1637 u_int i; 1638 1639 KASSERT((ap->a_flags & REVOKEALL) != 0, ("devfs_revoke !REVOKEALL")); 1640 1641 dev = vp->v_rdev; 1642 cdp = cdev2priv(dev); 1643 1644 dev_lock(); 1645 cdp->cdp_inuse++; 1646 dev_unlock(); 1647 1648 vhold(vp); 1649 vgone(vp); 1650 vdrop(vp); 1651 1652 VOP_UNLOCK(vp); 1653 loop: 1654 for (;;) { 1655 mtx_lock(&devfs_de_interlock); 1656 dev_lock(); 1657 vp2 = NULL; 1658 for (i = 0; i <= cdp->cdp_maxdirent; i++) { 1659 de = cdp->cdp_dirents[i]; 1660 if (de == NULL) 1661 continue; 1662 1663 vp2 = de->de_vnode; 1664 if (vp2 != NULL) { 1665 dev_unlock(); 1666 vs = vget_prep(vp2); 1667 mtx_unlock(&devfs_de_interlock); 1668 if (vget_finish(vp2, LK_EXCLUSIVE, vs) != 0) 1669 goto loop; 1670 vhold(vp2); 1671 vgone(vp2); 1672 vdrop(vp2); 1673 vput(vp2); 1674 break; 1675 } 1676 } 1677 if (vp2 != NULL) { 1678 continue; 1679 } 1680 dev_unlock(); 1681 mtx_unlock(&devfs_de_interlock); 1682 break; 1683 } 1684 dev_lock(); 1685 cdp->cdp_inuse--; 1686 if (!(cdp->cdp_flags & CDP_ACTIVE) && cdp->cdp_inuse == 0) { 1687 KASSERT((cdp->cdp_flags & CDP_ON_ACTIVE_LIST) != 0, 1688 ("%s: cdp %p (%s) not on active list", 1689 __func__, cdp, dev->si_name)); 1690 cdp->cdp_flags &= ~CDP_ON_ACTIVE_LIST; 1691 TAILQ_REMOVE(&cdevp_list, cdp, cdp_list); 1692 dev_unlock(); 1693 dev_rel(&cdp->cdp_c); 1694 } else 1695 dev_unlock(); 1696 1697 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1698 return (0); 1699 } 1700 1701 static int 1702 devfs_rioctl(struct vop_ioctl_args *ap) 1703 { 1704 struct vnode *vp; 1705 struct devfs_mount *dmp; 1706 int error; 1707 1708 vp = ap->a_vp; 1709 vn_lock(vp, LK_SHARED | LK_RETRY); 1710 if (VN_IS_DOOMED(vp)) { 1711 VOP_UNLOCK(vp); 1712 return (EBADF); 1713 } 1714 dmp = VFSTODEVFS(vp->v_mount); 1715 sx_xlock(&dmp->dm_lock); 1716 VOP_UNLOCK(vp); 1717 DEVFS_DMP_HOLD(dmp); 1718 devfs_populate(dmp); 1719 if (DEVFS_DMP_DROP(dmp)) { 1720 sx_xunlock(&dmp->dm_lock); 1721 devfs_unmount_final(dmp); 1722 return (ENOENT); 1723 } 1724 error = devfs_rules_ioctl(dmp, ap->a_command, ap->a_data, ap->a_td); 1725 sx_xunlock(&dmp->dm_lock); 1726 return (error); 1727 } 1728 1729 static int 1730 devfs_rread(struct vop_read_args *ap) 1731 { 1732 1733 if (ap->a_vp->v_type != VDIR) 1734 return (EINVAL); 1735 return (VOP_READDIR(ap->a_vp, ap->a_uio, ap->a_cred, NULL, NULL, NULL)); 1736 } 1737 1738 static int 1739 devfs_setattr(struct vop_setattr_args *ap) 1740 { 1741 struct devfs_dirent *de; 1742 struct vattr *vap; 1743 struct vnode *vp; 1744 struct thread *td; 1745 int c, error; 1746 uid_t uid; 1747 gid_t gid; 1748 1749 vap = ap->a_vap; 1750 vp = ap->a_vp; 1751 td = curthread; 1752 if ((vap->va_type != VNON) || 1753 (vap->va_nlink != VNOVAL) || 1754 (vap->va_fsid != VNOVAL) || 1755 (vap->va_fileid != VNOVAL) || 1756 (vap->va_blocksize != VNOVAL) || 1757 (vap->va_flags != VNOVAL && vap->va_flags != 0) || 1758 (vap->va_rdev != VNOVAL) || 1759 ((int)vap->va_bytes != VNOVAL) || 1760 (vap->va_gen != VNOVAL)) { 1761 return (EINVAL); 1762 } 1763 1764 error = devfs_populate_vp(vp); 1765 if (error != 0) 1766 return (error); 1767 1768 de = vp->v_data; 1769 if (vp->v_type == VDIR) 1770 de = de->de_dir; 1771 1772 c = 0; 1773 if (vap->va_uid == (uid_t)VNOVAL) 1774 uid = de->de_uid; 1775 else 1776 uid = vap->va_uid; 1777 if (vap->va_gid == (gid_t)VNOVAL) 1778 gid = de->de_gid; 1779 else 1780 gid = vap->va_gid; 1781 if (uid != de->de_uid || gid != de->de_gid) { 1782 if ((ap->a_cred->cr_uid != de->de_uid) || uid != de->de_uid || 1783 (gid != de->de_gid && !groupmember(gid, ap->a_cred))) { 1784 error = priv_check(td, PRIV_VFS_CHOWN); 1785 if (error != 0) 1786 goto ret; 1787 } 1788 de->de_uid = uid; 1789 de->de_gid = gid; 1790 c = 1; 1791 } 1792 1793 if (vap->va_mode != (mode_t)VNOVAL) { 1794 if (ap->a_cred->cr_uid != de->de_uid) { 1795 error = priv_check(td, PRIV_VFS_ADMIN); 1796 if (error != 0) 1797 goto ret; 1798 } 1799 de->de_mode = vap->va_mode; 1800 c = 1; 1801 } 1802 1803 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) { 1804 error = vn_utimes_perm(vp, vap, ap->a_cred, td); 1805 if (error != 0) 1806 goto ret; 1807 if (vap->va_atime.tv_sec != VNOVAL) { 1808 if (vp->v_type == VCHR) 1809 vp->v_rdev->si_atime = vap->va_atime; 1810 else 1811 de->de_atime = vap->va_atime; 1812 } 1813 if (vap->va_mtime.tv_sec != VNOVAL) { 1814 if (vp->v_type == VCHR) 1815 vp->v_rdev->si_mtime = vap->va_mtime; 1816 else 1817 de->de_mtime = vap->va_mtime; 1818 } 1819 c = 1; 1820 } 1821 1822 if (c) { 1823 if (vp->v_type == VCHR) 1824 vfs_timestamp(&vp->v_rdev->si_ctime); 1825 else 1826 vfs_timestamp(&de->de_mtime); 1827 } 1828 1829 ret: 1830 sx_xunlock(&VFSTODEVFS(vp->v_mount)->dm_lock); 1831 return (error); 1832 } 1833 1834 #ifdef MAC 1835 static int 1836 devfs_setlabel(struct vop_setlabel_args *ap) 1837 { 1838 struct vnode *vp; 1839 struct devfs_dirent *de; 1840 1841 vp = ap->a_vp; 1842 de = vp->v_data; 1843 1844 mac_vnode_relabel(ap->a_cred, vp, ap->a_label); 1845 mac_devfs_update(vp->v_mount, de, vp); 1846 1847 return (0); 1848 } 1849 #endif 1850 1851 static int 1852 devfs_stat_f(struct file *fp, struct stat *sb, struct ucred *cred) 1853 { 1854 1855 return (vnops.fo_stat(fp, sb, cred)); 1856 } 1857 1858 static int 1859 devfs_symlink(struct vop_symlink_args *ap) 1860 { 1861 int i, error; 1862 struct devfs_dirent *dd; 1863 struct devfs_dirent *de, *de_covered, *de_dotdot; 1864 struct devfs_mount *dmp; 1865 1866 error = priv_check(curthread, PRIV_DEVFS_SYMLINK); 1867 if (error) 1868 return(error); 1869 dmp = VFSTODEVFS(ap->a_dvp->v_mount); 1870 if (devfs_populate_vp(ap->a_dvp) != 0) 1871 return (ENOENT); 1872 1873 dd = ap->a_dvp->v_data; 1874 de = devfs_newdirent(ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen); 1875 de->de_flags = DE_USER; 1876 de->de_uid = 0; 1877 de->de_gid = 0; 1878 de->de_mode = 0755; 1879 de->de_inode = alloc_unr(devfs_inos); 1880 de->de_dir = dd; 1881 de->de_dirent->d_type = DT_LNK; 1882 i = strlen(ap->a_target) + 1; 1883 de->de_symlink = malloc(i, M_DEVFS, M_WAITOK); 1884 bcopy(ap->a_target, de->de_symlink, i); 1885 #ifdef MAC 1886 mac_devfs_create_symlink(ap->a_cnp->cn_cred, dmp->dm_mount, dd, de); 1887 #endif 1888 de_covered = devfs_find(dd, de->de_dirent->d_name, 1889 de->de_dirent->d_namlen, 0); 1890 if (de_covered != NULL) { 1891 if ((de_covered->de_flags & DE_USER) != 0) { 1892 devfs_delete(dmp, de, DEVFS_DEL_NORECURSE); 1893 sx_xunlock(&dmp->dm_lock); 1894 return (EEXIST); 1895 } 1896 KASSERT((de_covered->de_flags & DE_COVERED) == 0, 1897 ("devfs_symlink: entry %p already covered", de_covered)); 1898 de_covered->de_flags |= DE_COVERED; 1899 } 1900 1901 de_dotdot = TAILQ_FIRST(&dd->de_dlist); /* "." */ 1902 de_dotdot = TAILQ_NEXT(de_dotdot, de_list); /* ".." */ 1903 TAILQ_INSERT_AFTER(&dd->de_dlist, de_dotdot, de, de_list); 1904 devfs_dir_ref_de(dmp, dd); 1905 devfs_rules_apply(dmp, de); 1906 1907 return (devfs_allocv(de, ap->a_dvp->v_mount, LK_EXCLUSIVE, ap->a_vpp)); 1908 } 1909 1910 static int 1911 devfs_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td) 1912 { 1913 1914 return (vnops.fo_truncate(fp, length, cred, td)); 1915 } 1916 1917 static int 1918 devfs_write_f(struct file *fp, struct uio *uio, struct ucred *cred, 1919 int flags, struct thread *td) 1920 { 1921 struct cdev *dev; 1922 int error, ioflag, ref; 1923 ssize_t resid; 1924 struct cdevsw *dsw; 1925 struct file *fpop; 1926 1927 if (uio->uio_resid > DEVFS_IOSIZE_MAX) 1928 return (EINVAL); 1929 fpop = td->td_fpop; 1930 error = devfs_fp_check(fp, &dev, &dsw, &ref); 1931 if (error != 0) { 1932 error = vnops.fo_write(fp, uio, cred, flags, td); 1933 return (error); 1934 } 1935 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td)); 1936 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT | O_FSYNC); 1937 if (ioflag & O_DIRECT) 1938 ioflag |= IO_DIRECT; 1939 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK); 1940 1941 resid = uio->uio_resid; 1942 1943 error = dsw->d_write(dev, uio, ioflag); 1944 if (uio->uio_resid != resid || (error == 0 && resid != 0)) { 1945 devfs_timestamp(&dev->si_ctime); 1946 dev->si_mtime = dev->si_ctime; 1947 } 1948 td->td_fpop = fpop; 1949 dev_relthread(dev, ref); 1950 1951 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF_W); 1952 return (error); 1953 } 1954 1955 static int 1956 devfs_mmap_f(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t size, 1957 vm_prot_t prot, vm_prot_t cap_maxprot, int flags, vm_ooffset_t foff, 1958 struct thread *td) 1959 { 1960 struct cdev *dev; 1961 struct cdevsw *dsw; 1962 struct mount *mp; 1963 struct vnode *vp; 1964 struct file *fpop; 1965 vm_object_t object; 1966 vm_prot_t maxprot; 1967 int error, ref; 1968 1969 vp = fp->f_vnode; 1970 1971 /* 1972 * Ensure that file and memory protections are 1973 * compatible. 1974 */ 1975 mp = vp->v_mount; 1976 if (mp != NULL && (mp->mnt_flag & MNT_NOEXEC) != 0) { 1977 maxprot = VM_PROT_NONE; 1978 if ((prot & VM_PROT_EXECUTE) != 0) 1979 return (EACCES); 1980 } else 1981 maxprot = VM_PROT_EXECUTE; 1982 if ((fp->f_flag & FREAD) != 0) 1983 maxprot |= VM_PROT_READ; 1984 else if ((prot & VM_PROT_READ) != 0) 1985 return (EACCES); 1986 1987 /* 1988 * If we are sharing potential changes via MAP_SHARED and we 1989 * are trying to get write permission although we opened it 1990 * without asking for it, bail out. 1991 * 1992 * Note that most character devices always share mappings. 1993 * The one exception is that D_MMAP_ANON devices 1994 * (i.e. /dev/zero) permit private writable mappings. 1995 * 1996 * Rely on vm_mmap_cdev() to fail invalid MAP_PRIVATE requests 1997 * as well as updating maxprot to permit writing for 1998 * D_MMAP_ANON devices rather than doing that here. 1999 */ 2000 if ((flags & MAP_SHARED) != 0) { 2001 if ((fp->f_flag & FWRITE) != 0) 2002 maxprot |= VM_PROT_WRITE; 2003 else if ((prot & VM_PROT_WRITE) != 0) 2004 return (EACCES); 2005 } 2006 maxprot &= cap_maxprot; 2007 2008 fpop = td->td_fpop; 2009 error = devfs_fp_check(fp, &dev, &dsw, &ref); 2010 if (error != 0) 2011 return (error); 2012 2013 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags, dev, dsw, &foff, 2014 &object); 2015 td->td_fpop = fpop; 2016 dev_relthread(dev, ref); 2017 if (error != 0) 2018 return (error); 2019 2020 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object, 2021 foff, FALSE, td); 2022 if (error != 0) 2023 vm_object_deallocate(object); 2024 return (error); 2025 } 2026 2027 dev_t 2028 dev2udev(struct cdev *x) 2029 { 2030 if (x == NULL) 2031 return (NODEV); 2032 return (cdev2priv(x)->cdp_inode); 2033 } 2034 2035 static int 2036 devfs_cmp_f(struct file *fp1, struct file *fp2, struct thread *td) 2037 { 2038 if (fp2->f_type != DTYPE_VNODE || fp2->f_ops != &devfs_ops_f) 2039 return (3); 2040 return (kcmp_cmp((uintptr_t)fp1->f_data, (uintptr_t)fp2->f_data)); 2041 } 2042 2043 static struct fileops devfs_ops_f = { 2044 .fo_read = devfs_read_f, 2045 .fo_write = devfs_write_f, 2046 .fo_truncate = devfs_truncate_f, 2047 .fo_ioctl = devfs_ioctl_f, 2048 .fo_poll = devfs_poll_f, 2049 .fo_kqfilter = devfs_kqfilter_f, 2050 .fo_stat = devfs_stat_f, 2051 .fo_close = devfs_close_f, 2052 .fo_chmod = vn_chmod, 2053 .fo_chown = vn_chown, 2054 .fo_sendfile = vn_sendfile, 2055 .fo_seek = vn_seek, 2056 .fo_fill_kinfo = vn_fill_kinfo, 2057 .fo_mmap = devfs_mmap_f, 2058 .fo_cmp = devfs_cmp_f, 2059 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE 2060 }; 2061 2062 /* Vops for non-CHR vnodes in /dev. */ 2063 static struct vop_vector devfs_vnodeops = { 2064 .vop_default = &default_vnodeops, 2065 2066 .vop_access = devfs_access, 2067 .vop_getattr = devfs_getattr, 2068 .vop_ioctl = devfs_rioctl, 2069 .vop_lookup = devfs_lookup, 2070 .vop_mknod = devfs_mknod, 2071 .vop_pathconf = devfs_pathconf, 2072 .vop_read = devfs_rread, 2073 .vop_readdir = devfs_readdir, 2074 .vop_readlink = devfs_readlink, 2075 .vop_reclaim = devfs_reclaim, 2076 .vop_remove = devfs_remove, 2077 .vop_revoke = devfs_revoke, 2078 .vop_setattr = devfs_setattr, 2079 #ifdef MAC 2080 .vop_setlabel = devfs_setlabel, 2081 #endif 2082 .vop_symlink = devfs_symlink, 2083 .vop_vptocnp = devfs_vptocnp, 2084 .vop_lock1 = vop_lock, 2085 .vop_unlock = vop_unlock, 2086 .vop_islocked = vop_islocked, 2087 .vop_add_writecount = vop_stdadd_writecount_nomsync, 2088 }; 2089 VFS_VOP_VECTOR_REGISTER(devfs_vnodeops); 2090 2091 /* Vops for VCHR vnodes in /dev. */ 2092 static struct vop_vector devfs_specops = { 2093 .vop_default = &default_vnodeops, 2094 2095 .vop_access = devfs_access, 2096 .vop_bmap = VOP_PANIC, 2097 .vop_close = devfs_close, 2098 .vop_create = VOP_PANIC, 2099 .vop_fsync = vop_stdfsync, 2100 .vop_getattr = devfs_getattr, 2101 .vop_ioctl = devfs_ioctl, 2102 .vop_link = VOP_PANIC, 2103 .vop_mkdir = VOP_PANIC, 2104 .vop_mknod = VOP_PANIC, 2105 .vop_open = devfs_open, 2106 .vop_pathconf = devfs_pathconf, 2107 .vop_poll = dead_poll, 2108 .vop_print = devfs_print, 2109 .vop_read = dead_read, 2110 .vop_readdir = VOP_PANIC, 2111 .vop_readlink = VOP_PANIC, 2112 .vop_reallocblks = VOP_PANIC, 2113 .vop_reclaim = devfs_reclaim_vchr, 2114 .vop_remove = devfs_remove, 2115 .vop_rename = VOP_PANIC, 2116 .vop_revoke = devfs_revoke, 2117 .vop_rmdir = VOP_PANIC, 2118 .vop_setattr = devfs_setattr, 2119 #ifdef MAC 2120 .vop_setlabel = devfs_setlabel, 2121 #endif 2122 .vop_strategy = VOP_PANIC, 2123 .vop_symlink = VOP_PANIC, 2124 .vop_vptocnp = devfs_vptocnp, 2125 .vop_write = dead_write, 2126 .vop_lock1 = vop_lock, 2127 .vop_unlock = vop_unlock, 2128 .vop_islocked = vop_islocked, 2129 .vop_add_writecount = vop_stdadd_writecount_nomsync, 2130 }; 2131 VFS_VOP_VECTOR_REGISTER(devfs_specops); 2132 2133 /* 2134 * Our calling convention to the device drivers used to be that we passed 2135 * vnode.h IO_* flags to read()/write(), but we're moving to fcntl.h O_ 2136 * flags instead since that's what open(), close() and ioctl() takes and 2137 * we don't really want vnode.h in device drivers. 2138 * We solved the source compatibility by redefining some vnode flags to 2139 * be the same as the fcntl ones and by sending down the bitwise OR of 2140 * the respective fcntl/vnode flags. These CTASSERTS make sure nobody 2141 * pulls the rug out under this. 2142 */ 2143 CTASSERT(O_NONBLOCK == IO_NDELAY); 2144 CTASSERT(O_FSYNC == IO_SYNC); 2145