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