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