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