1 /* $NetBSD: spec_vnops.c,v 1.180 2020/06/27 17:29:19 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /* 30 * Copyright (c) 1989, 1993 31 * The Regents of the University of California. All rights reserved. 32 * 33 * Redistribution and use in source and binary forms, with or without 34 * modification, are permitted provided that the following conditions 35 * are met: 36 * 1. Redistributions of source code must retain the above copyright 37 * notice, this list of conditions and the following disclaimer. 38 * 2. Redistributions in binary form must reproduce the above copyright 39 * notice, this list of conditions and the following disclaimer in the 40 * documentation and/or other materials provided with the distribution. 41 * 3. Neither the name of the University nor the names of its contributors 42 * may be used to endorse or promote products derived from this software 43 * without specific prior written permission. 44 * 45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 55 * SUCH DAMAGE. 56 * 57 * @(#)spec_vnops.c 8.15 (Berkeley) 7/14/95 58 */ 59 60 #include <sys/cdefs.h> 61 __KERNEL_RCSID(0, "$NetBSD: spec_vnops.c,v 1.180 2020/06/27 17:29:19 christos Exp $"); 62 63 #include <sys/param.h> 64 #include <sys/proc.h> 65 #include <sys/systm.h> 66 #include <sys/kernel.h> 67 #include <sys/conf.h> 68 #include <sys/buf.h> 69 #include <sys/mount.h> 70 #include <sys/namei.h> 71 #include <sys/vnode_impl.h> 72 #include <sys/stat.h> 73 #include <sys/errno.h> 74 #include <sys/ioctl.h> 75 #include <sys/poll.h> 76 #include <sys/file.h> 77 #include <sys/disklabel.h> 78 #include <sys/disk.h> 79 #include <sys/lockf.h> 80 #include <sys/tty.h> 81 #include <sys/kauth.h> 82 #include <sys/fstrans.h> 83 #include <sys/module.h> 84 85 #include <miscfs/genfs/genfs.h> 86 #include <miscfs/specfs/specdev.h> 87 88 /* symbolic sleep message strings for devices */ 89 const char devopn[] = "devopn"; 90 const char devio[] = "devio"; 91 const char devwait[] = "devwait"; 92 const char devin[] = "devin"; 93 const char devout[] = "devout"; 94 const char devioc[] = "devioc"; 95 const char devcls[] = "devcls"; 96 97 #define SPECHSZ 64 98 #if ((SPECHSZ&(SPECHSZ-1)) == 0) 99 #define SPECHASH(rdev) (((rdev>>5)+(rdev))&(SPECHSZ-1)) 100 #else 101 #define SPECHASH(rdev) (((unsigned)((rdev>>5)+(rdev)))%SPECHSZ) 102 #endif 103 104 static vnode_t *specfs_hash[SPECHSZ]; 105 extern struct mount *dead_rootmount; 106 107 /* 108 * This vnode operations vector is used for special device nodes 109 * created from whole cloth by the kernel. For the ops vector for 110 * vnodes built from special devices found in a filesystem, see (e.g) 111 * ffs_specop_entries[] in ffs_vnops.c or the equivalent for other 112 * filesystems. 113 */ 114 115 int (**spec_vnodeop_p)(void *); 116 const struct vnodeopv_entry_desc spec_vnodeop_entries[] = { 117 { &vop_default_desc, vn_default_error }, 118 { &vop_lookup_desc, spec_lookup }, /* lookup */ 119 { &vop_create_desc, spec_create }, /* create */ 120 { &vop_mknod_desc, spec_mknod }, /* mknod */ 121 { &vop_open_desc, spec_open }, /* open */ 122 { &vop_close_desc, spec_close }, /* close */ 123 { &vop_access_desc, spec_access }, /* access */ 124 { &vop_accessx_desc, genfs_accessx }, /* accessx */ 125 { &vop_getattr_desc, spec_getattr }, /* getattr */ 126 { &vop_setattr_desc, spec_setattr }, /* setattr */ 127 { &vop_read_desc, spec_read }, /* read */ 128 { &vop_write_desc, spec_write }, /* write */ 129 { &vop_fallocate_desc, spec_fallocate }, /* fallocate */ 130 { &vop_fdiscard_desc, spec_fdiscard }, /* fdiscard */ 131 { &vop_fcntl_desc, spec_fcntl }, /* fcntl */ 132 { &vop_ioctl_desc, spec_ioctl }, /* ioctl */ 133 { &vop_poll_desc, spec_poll }, /* poll */ 134 { &vop_kqfilter_desc, spec_kqfilter }, /* kqfilter */ 135 { &vop_revoke_desc, spec_revoke }, /* revoke */ 136 { &vop_mmap_desc, spec_mmap }, /* mmap */ 137 { &vop_fsync_desc, spec_fsync }, /* fsync */ 138 { &vop_seek_desc, spec_seek }, /* seek */ 139 { &vop_remove_desc, spec_remove }, /* remove */ 140 { &vop_link_desc, spec_link }, /* link */ 141 { &vop_rename_desc, spec_rename }, /* rename */ 142 { &vop_mkdir_desc, spec_mkdir }, /* mkdir */ 143 { &vop_rmdir_desc, spec_rmdir }, /* rmdir */ 144 { &vop_symlink_desc, spec_symlink }, /* symlink */ 145 { &vop_readdir_desc, spec_readdir }, /* readdir */ 146 { &vop_readlink_desc, spec_readlink }, /* readlink */ 147 { &vop_abortop_desc, spec_abortop }, /* abortop */ 148 { &vop_inactive_desc, spec_inactive }, /* inactive */ 149 { &vop_reclaim_desc, spec_reclaim }, /* reclaim */ 150 { &vop_lock_desc, spec_lock }, /* lock */ 151 { &vop_unlock_desc, spec_unlock }, /* unlock */ 152 { &vop_bmap_desc, spec_bmap }, /* bmap */ 153 { &vop_strategy_desc, spec_strategy }, /* strategy */ 154 { &vop_print_desc, spec_print }, /* print */ 155 { &vop_islocked_desc, spec_islocked }, /* islocked */ 156 { &vop_pathconf_desc, spec_pathconf }, /* pathconf */ 157 { &vop_advlock_desc, spec_advlock }, /* advlock */ 158 { &vop_bwrite_desc, spec_bwrite }, /* bwrite */ 159 { &vop_getpages_desc, spec_getpages }, /* getpages */ 160 { &vop_putpages_desc, spec_putpages }, /* putpages */ 161 { NULL, NULL } 162 }; 163 const struct vnodeopv_desc spec_vnodeop_opv_desc = 164 { &spec_vnodeop_p, spec_vnodeop_entries }; 165 166 static kauth_listener_t rawio_listener; 167 168 /* Returns true if vnode is /dev/mem or /dev/kmem. */ 169 bool 170 iskmemvp(struct vnode *vp) 171 { 172 return ((vp->v_type == VCHR) && iskmemdev(vp->v_rdev)); 173 } 174 175 /* 176 * Returns true if dev is /dev/mem or /dev/kmem. 177 */ 178 int 179 iskmemdev(dev_t dev) 180 { 181 /* mem_no is emitted by config(8) to generated devsw.c */ 182 extern const int mem_no; 183 184 /* minor 14 is /dev/io on i386 with COMPAT_10 */ 185 return (major(dev) == mem_no && (minor(dev) < 2 || minor(dev) == 14)); 186 } 187 188 static int 189 rawio_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie, 190 void *arg0, void *arg1, void *arg2, void *arg3) 191 { 192 int result; 193 194 result = KAUTH_RESULT_DEFER; 195 196 if ((action != KAUTH_DEVICE_RAWIO_SPEC) && 197 (action != KAUTH_DEVICE_RAWIO_PASSTHRU)) 198 return result; 199 200 /* Access is mandated by permissions. */ 201 result = KAUTH_RESULT_ALLOW; 202 203 return result; 204 } 205 206 void 207 spec_init(void) 208 { 209 210 rawio_listener = kauth_listen_scope(KAUTH_SCOPE_DEVICE, 211 rawio_listener_cb, NULL); 212 } 213 214 /* 215 * Initialize a vnode that represents a device. 216 */ 217 void 218 spec_node_init(vnode_t *vp, dev_t rdev) 219 { 220 specnode_t *sn; 221 specdev_t *sd; 222 vnode_t *vp2; 223 vnode_t **vpp; 224 225 KASSERT(vp->v_type == VBLK || vp->v_type == VCHR); 226 KASSERT(vp->v_specnode == NULL); 227 228 /* 229 * Search the hash table for this device. If known, add a 230 * reference to the device structure. If not known, create 231 * a new entry to represent the device. In all cases add 232 * the vnode to the hash table. 233 */ 234 sn = kmem_alloc(sizeof(*sn), KM_SLEEP); 235 sd = kmem_alloc(sizeof(*sd), KM_SLEEP); 236 mutex_enter(&device_lock); 237 vpp = &specfs_hash[SPECHASH(rdev)]; 238 for (vp2 = *vpp; vp2 != NULL; vp2 = vp2->v_specnext) { 239 KASSERT(vp2->v_specnode != NULL); 240 if (rdev == vp2->v_rdev && vp->v_type == vp2->v_type) { 241 break; 242 } 243 } 244 if (vp2 == NULL) { 245 /* No existing record, create a new one. */ 246 sd->sd_rdev = rdev; 247 sd->sd_mountpoint = NULL; 248 sd->sd_lockf = NULL; 249 sd->sd_refcnt = 1; 250 sd->sd_opencnt = 0; 251 sd->sd_bdevvp = NULL; 252 sn->sn_dev = sd; 253 sd = NULL; 254 } else { 255 /* Use the existing record. */ 256 sn->sn_dev = vp2->v_specnode->sn_dev; 257 sn->sn_dev->sd_refcnt++; 258 } 259 /* Insert vnode into the hash chain. */ 260 sn->sn_opencnt = 0; 261 sn->sn_rdev = rdev; 262 sn->sn_gone = false; 263 vp->v_specnode = sn; 264 vp->v_specnext = *vpp; 265 *vpp = vp; 266 mutex_exit(&device_lock); 267 268 /* Free the record we allocated if unused. */ 269 if (sd != NULL) { 270 kmem_free(sd, sizeof(*sd)); 271 } 272 } 273 274 /* 275 * Lookup a vnode by device number and return it referenced. 276 */ 277 int 278 spec_node_lookup_by_dev(enum vtype type, dev_t dev, vnode_t **vpp) 279 { 280 int error; 281 vnode_t *vp; 282 283 mutex_enter(&device_lock); 284 for (vp = specfs_hash[SPECHASH(dev)]; vp; vp = vp->v_specnext) { 285 if (type == vp->v_type && dev == vp->v_rdev) { 286 mutex_enter(vp->v_interlock); 287 /* If clean or being cleaned, then ignore it. */ 288 if (vdead_check(vp, VDEAD_NOWAIT) == 0) 289 break; 290 mutex_exit(vp->v_interlock); 291 } 292 } 293 KASSERT(vp == NULL || mutex_owned(vp->v_interlock)); 294 if (vp == NULL) { 295 mutex_exit(&device_lock); 296 return ENOENT; 297 } 298 /* 299 * If it is an opened block device return the opened vnode. 300 */ 301 if (type == VBLK && vp->v_specnode->sn_dev->sd_bdevvp != NULL) { 302 mutex_exit(vp->v_interlock); 303 vp = vp->v_specnode->sn_dev->sd_bdevvp; 304 mutex_enter(vp->v_interlock); 305 } 306 mutex_exit(&device_lock); 307 error = vcache_vget(vp); 308 if (error != 0) 309 return error; 310 *vpp = vp; 311 312 return 0; 313 } 314 315 /* 316 * Lookup a vnode by file system mounted on and return it referenced. 317 */ 318 int 319 spec_node_lookup_by_mount(struct mount *mp, vnode_t **vpp) 320 { 321 int i, error; 322 vnode_t *vp, *vq; 323 324 mutex_enter(&device_lock); 325 for (i = 0, vq = NULL; i < SPECHSZ && vq == NULL; i++) { 326 for (vp = specfs_hash[i]; vp; vp = vp->v_specnext) { 327 if (vp->v_type != VBLK) 328 continue; 329 vq = vp->v_specnode->sn_dev->sd_bdevvp; 330 if (vq != NULL && 331 vq->v_specnode->sn_dev->sd_mountpoint == mp) 332 break; 333 vq = NULL; 334 } 335 } 336 if (vq == NULL) { 337 mutex_exit(&device_lock); 338 return ENOENT; 339 } 340 mutex_enter(vq->v_interlock); 341 mutex_exit(&device_lock); 342 error = vcache_vget(vq); 343 if (error != 0) 344 return error; 345 *vpp = vq; 346 347 return 0; 348 349 } 350 351 /* 352 * Get the file system mounted on this block device. 353 */ 354 struct mount * 355 spec_node_getmountedfs(vnode_t *devvp) 356 { 357 struct mount *mp; 358 359 KASSERT(devvp->v_type == VBLK); 360 mp = devvp->v_specnode->sn_dev->sd_mountpoint; 361 362 return mp; 363 } 364 365 /* 366 * Set the file system mounted on this block device. 367 */ 368 void 369 spec_node_setmountedfs(vnode_t *devvp, struct mount *mp) 370 { 371 struct dkwedge_info dkw; 372 373 KASSERT(devvp->v_type == VBLK); 374 KASSERT(devvp->v_specnode->sn_dev->sd_mountpoint == NULL || mp == NULL); 375 devvp->v_specnode->sn_dev->sd_mountpoint = mp; 376 if (mp == NULL) 377 return; 378 379 if (bdev_ioctl(devvp->v_rdev, DIOCGWEDGEINFO, &dkw, FREAD, curlwp) != 0) 380 return; 381 382 strlcpy(mp->mnt_stat.f_mntfromlabel, dkw.dkw_wname, 383 sizeof(mp->mnt_stat.f_mntfromlabel)); 384 } 385 386 /* 387 * A vnode representing a special device is going away. Close 388 * the device if the vnode holds it open. 389 */ 390 void 391 spec_node_revoke(vnode_t *vp) 392 { 393 specnode_t *sn; 394 specdev_t *sd; 395 396 sn = vp->v_specnode; 397 sd = sn->sn_dev; 398 399 KASSERT(vp->v_type == VBLK || vp->v_type == VCHR); 400 KASSERT(vp->v_specnode != NULL); 401 KASSERT(sn->sn_gone == false); 402 403 mutex_enter(&device_lock); 404 KASSERT(sn->sn_opencnt <= sd->sd_opencnt); 405 if (sn->sn_opencnt != 0) { 406 sd->sd_opencnt -= (sn->sn_opencnt - 1); 407 sn->sn_opencnt = 1; 408 sn->sn_gone = true; 409 mutex_exit(&device_lock); 410 411 VOP_CLOSE(vp, FNONBLOCK, NOCRED); 412 413 mutex_enter(&device_lock); 414 KASSERT(sn->sn_opencnt == 0); 415 } 416 mutex_exit(&device_lock); 417 } 418 419 /* 420 * A vnode representing a special device is being recycled. 421 * Destroy the specfs component. 422 */ 423 void 424 spec_node_destroy(vnode_t *vp) 425 { 426 specnode_t *sn; 427 specdev_t *sd; 428 vnode_t **vpp, *vp2; 429 int refcnt; 430 431 sn = vp->v_specnode; 432 sd = sn->sn_dev; 433 434 KASSERT(vp->v_type == VBLK || vp->v_type == VCHR); 435 KASSERT(vp->v_specnode != NULL); 436 KASSERT(sn->sn_opencnt == 0); 437 438 mutex_enter(&device_lock); 439 /* Remove from the hash and destroy the node. */ 440 vpp = &specfs_hash[SPECHASH(vp->v_rdev)]; 441 for (vp2 = *vpp;; vp2 = vp2->v_specnext) { 442 if (vp2 == NULL) { 443 panic("spec_node_destroy: corrupt hash"); 444 } 445 if (vp2 == vp) { 446 KASSERT(vp == *vpp); 447 *vpp = vp->v_specnext; 448 break; 449 } 450 if (vp2->v_specnext == vp) { 451 vp2->v_specnext = vp->v_specnext; 452 break; 453 } 454 } 455 sn = vp->v_specnode; 456 vp->v_specnode = NULL; 457 refcnt = sd->sd_refcnt--; 458 KASSERT(refcnt > 0); 459 mutex_exit(&device_lock); 460 461 /* If the device is no longer in use, destroy our record. */ 462 if (refcnt == 1) { 463 KASSERT(sd->sd_opencnt == 0); 464 KASSERT(sd->sd_bdevvp == NULL); 465 kmem_free(sd, sizeof(*sd)); 466 } 467 kmem_free(sn, sizeof(*sn)); 468 } 469 470 /* 471 * Trivial lookup routine that always fails. 472 */ 473 int 474 spec_lookup(void *v) 475 { 476 struct vop_lookup_v2_args /* { 477 struct vnode *a_dvp; 478 struct vnode **a_vpp; 479 struct componentname *a_cnp; 480 } */ *ap = v; 481 482 *ap->a_vpp = NULL; 483 return (ENOTDIR); 484 } 485 486 typedef int (*spec_ioctl_t)(dev_t, u_long, void *, int, struct lwp *); 487 488 /* 489 * Open a special file. 490 */ 491 /* ARGSUSED */ 492 int 493 spec_open(void *v) 494 { 495 struct vop_open_args /* { 496 struct vnode *a_vp; 497 int a_mode; 498 kauth_cred_t a_cred; 499 } */ *ap = v; 500 struct lwp *l; 501 struct vnode *vp; 502 dev_t dev; 503 int error; 504 enum kauth_device_req req; 505 specnode_t *sn; 506 specdev_t *sd; 507 spec_ioctl_t ioctl; 508 u_int gen; 509 const char *name; 510 struct partinfo pi; 511 512 l = curlwp; 513 vp = ap->a_vp; 514 dev = vp->v_rdev; 515 sn = vp->v_specnode; 516 sd = sn->sn_dev; 517 name = NULL; 518 gen = 0; 519 520 /* 521 * Don't allow open if fs is mounted -nodev. 522 */ 523 if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV)) 524 return (ENXIO); 525 526 switch (ap->a_mode & (FREAD | FWRITE)) { 527 case FREAD | FWRITE: 528 req = KAUTH_REQ_DEVICE_RAWIO_SPEC_RW; 529 break; 530 case FWRITE: 531 req = KAUTH_REQ_DEVICE_RAWIO_SPEC_WRITE; 532 break; 533 default: 534 req = KAUTH_REQ_DEVICE_RAWIO_SPEC_READ; 535 break; 536 } 537 538 switch (vp->v_type) { 539 case VCHR: 540 error = kauth_authorize_device_spec(ap->a_cred, req, vp); 541 if (error != 0) 542 return (error); 543 544 /* 545 * Character devices can accept opens from multiple 546 * vnodes. 547 */ 548 mutex_enter(&device_lock); 549 if (sn->sn_gone) { 550 mutex_exit(&device_lock); 551 return (EBADF); 552 } 553 sd->sd_opencnt++; 554 sn->sn_opencnt++; 555 mutex_exit(&device_lock); 556 if (cdev_type(dev) == D_TTY) 557 vp->v_vflag |= VV_ISTTY; 558 VOP_UNLOCK(vp); 559 do { 560 const struct cdevsw *cdev; 561 562 gen = module_gen; 563 error = cdev_open(dev, ap->a_mode, S_IFCHR, l); 564 if (error != ENXIO) 565 break; 566 567 /* Check if we already have a valid driver */ 568 mutex_enter(&device_lock); 569 cdev = cdevsw_lookup(dev); 570 mutex_exit(&device_lock); 571 if (cdev != NULL) 572 break; 573 574 /* Get device name from devsw_conv array */ 575 if ((name = cdevsw_getname(major(dev))) == NULL) 576 break; 577 578 /* Try to autoload device module */ 579 (void) module_autoload(name, MODULE_CLASS_DRIVER); 580 } while (gen != module_gen); 581 582 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 583 break; 584 585 case VBLK: 586 error = kauth_authorize_device_spec(ap->a_cred, req, vp); 587 if (error != 0) 588 return (error); 589 590 /* 591 * For block devices, permit only one open. The buffer 592 * cache cannot remain self-consistent with multiple 593 * vnodes holding a block device open. 594 * 595 * Treat zero opencnt with non-NULL mountpoint as open. 596 * This may happen after forced detach of a mounted device. 597 */ 598 mutex_enter(&device_lock); 599 if (sn->sn_gone) { 600 mutex_exit(&device_lock); 601 return (EBADF); 602 } 603 if (sd->sd_opencnt != 0 || sd->sd_mountpoint != NULL) { 604 mutex_exit(&device_lock); 605 return EBUSY; 606 } 607 sn->sn_opencnt = 1; 608 sd->sd_opencnt = 1; 609 sd->sd_bdevvp = vp; 610 mutex_exit(&device_lock); 611 do { 612 const struct bdevsw *bdev; 613 614 gen = module_gen; 615 error = bdev_open(dev, ap->a_mode, S_IFBLK, l); 616 if (error != ENXIO) 617 break; 618 619 /* Check if we already have a valid driver */ 620 mutex_enter(&device_lock); 621 bdev = bdevsw_lookup(dev); 622 mutex_exit(&device_lock); 623 if (bdev != NULL) 624 break; 625 626 /* Get device name from devsw_conv array */ 627 if ((name = bdevsw_getname(major(dev))) == NULL) 628 break; 629 630 VOP_UNLOCK(vp); 631 632 /* Try to autoload device module */ 633 (void) module_autoload(name, MODULE_CLASS_DRIVER); 634 635 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 636 } while (gen != module_gen); 637 638 break; 639 640 case VNON: 641 case VLNK: 642 case VDIR: 643 case VREG: 644 case VBAD: 645 case VFIFO: 646 case VSOCK: 647 default: 648 return 0; 649 } 650 651 mutex_enter(&device_lock); 652 if (sn->sn_gone) { 653 if (error == 0) 654 error = EBADF; 655 } else if (error != 0) { 656 sd->sd_opencnt--; 657 sn->sn_opencnt--; 658 if (vp->v_type == VBLK) 659 sd->sd_bdevvp = NULL; 660 661 } 662 mutex_exit(&device_lock); 663 664 if (cdev_type(dev) != D_DISK || error != 0) 665 return error; 666 667 668 ioctl = vp->v_type == VCHR ? cdev_ioctl : bdev_ioctl; 669 error = (*ioctl)(vp->v_rdev, DIOCGPARTINFO, &pi, FREAD, curlwp); 670 if (error == 0) 671 uvm_vnp_setsize(vp, (voff_t)pi.pi_secsize * pi.pi_size); 672 673 return 0; 674 } 675 676 /* 677 * Vnode op for read 678 */ 679 /* ARGSUSED */ 680 int 681 spec_read(void *v) 682 { 683 struct vop_read_args /* { 684 struct vnode *a_vp; 685 struct uio *a_uio; 686 int a_ioflag; 687 kauth_cred_t a_cred; 688 } */ *ap = v; 689 struct vnode *vp = ap->a_vp; 690 struct uio *uio = ap->a_uio; 691 struct lwp *l = curlwp; 692 struct buf *bp; 693 daddr_t bn; 694 int bsize, bscale; 695 struct partinfo pi; 696 int n, on; 697 int error = 0; 698 699 KASSERT(uio->uio_rw == UIO_READ); 700 KASSERTMSG(VMSPACE_IS_KERNEL_P(uio->uio_vmspace) || 701 uio->uio_vmspace == curproc->p_vmspace, 702 "vmspace belongs to neither kernel nor curproc"); 703 704 if (uio->uio_resid == 0) 705 return (0); 706 707 switch (vp->v_type) { 708 709 case VCHR: 710 VOP_UNLOCK(vp); 711 error = cdev_read(vp->v_rdev, uio, ap->a_ioflag); 712 vn_lock(vp, LK_SHARED | LK_RETRY); 713 return (error); 714 715 case VBLK: 716 KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp); 717 if (uio->uio_offset < 0) 718 return (EINVAL); 719 720 if (bdev_ioctl(vp->v_rdev, DIOCGPARTINFO, &pi, FREAD, l) == 0) 721 bsize = imin(imax(pi.pi_bsize, DEV_BSIZE), MAXBSIZE); 722 else 723 bsize = BLKDEV_IOSIZE; 724 725 bscale = bsize >> DEV_BSHIFT; 726 do { 727 bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1); 728 on = uio->uio_offset % bsize; 729 n = uimin((unsigned)(bsize - on), uio->uio_resid); 730 error = bread(vp, bn, bsize, 0, &bp); 731 if (error) { 732 return (error); 733 } 734 n = uimin(n, bsize - bp->b_resid); 735 error = uiomove((char *)bp->b_data + on, n, uio); 736 brelse(bp, 0); 737 } while (error == 0 && uio->uio_resid > 0 && n != 0); 738 return (error); 739 740 default: 741 panic("spec_read type"); 742 } 743 /* NOTREACHED */ 744 } 745 746 /* 747 * Vnode op for write 748 */ 749 /* ARGSUSED */ 750 int 751 spec_write(void *v) 752 { 753 struct vop_write_args /* { 754 struct vnode *a_vp; 755 struct uio *a_uio; 756 int a_ioflag; 757 kauth_cred_t a_cred; 758 } */ *ap = v; 759 struct vnode *vp = ap->a_vp; 760 struct uio *uio = ap->a_uio; 761 struct lwp *l = curlwp; 762 struct buf *bp; 763 daddr_t bn; 764 int bsize, bscale; 765 struct partinfo pi; 766 int n, on; 767 int error = 0; 768 769 KASSERT(uio->uio_rw == UIO_WRITE); 770 KASSERTMSG(VMSPACE_IS_KERNEL_P(uio->uio_vmspace) || 771 uio->uio_vmspace == curproc->p_vmspace, 772 "vmspace belongs to neither kernel nor curproc"); 773 774 switch (vp->v_type) { 775 776 case VCHR: 777 VOP_UNLOCK(vp); 778 error = cdev_write(vp->v_rdev, uio, ap->a_ioflag); 779 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 780 return (error); 781 782 case VBLK: 783 KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp); 784 if (uio->uio_resid == 0) 785 return (0); 786 if (uio->uio_offset < 0) 787 return (EINVAL); 788 789 if (bdev_ioctl(vp->v_rdev, DIOCGPARTINFO, &pi, FREAD, l) == 0) 790 bsize = imin(imax(pi.pi_bsize, DEV_BSIZE), MAXBSIZE); 791 else 792 bsize = BLKDEV_IOSIZE; 793 794 bscale = bsize >> DEV_BSHIFT; 795 do { 796 bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1); 797 on = uio->uio_offset % bsize; 798 n = uimin((unsigned)(bsize - on), uio->uio_resid); 799 if (n == bsize) 800 bp = getblk(vp, bn, bsize, 0, 0); 801 else 802 error = bread(vp, bn, bsize, B_MODIFY, &bp); 803 if (error) { 804 return (error); 805 } 806 n = uimin(n, bsize - bp->b_resid); 807 error = uiomove((char *)bp->b_data + on, n, uio); 808 if (error) 809 brelse(bp, 0); 810 else { 811 if (n + on == bsize) 812 bawrite(bp); 813 else 814 bdwrite(bp); 815 error = bp->b_error; 816 } 817 } while (error == 0 && uio->uio_resid > 0 && n != 0); 818 return (error); 819 820 default: 821 panic("spec_write type"); 822 } 823 /* NOTREACHED */ 824 } 825 826 /* 827 * fdiscard, which on disk devices becomes TRIM. 828 */ 829 int 830 spec_fdiscard(void *v) 831 { 832 struct vop_fdiscard_args /* { 833 struct vnode *a_vp; 834 off_t a_pos; 835 off_t a_len; 836 } */ *ap = v; 837 struct vnode *vp; 838 dev_t dev; 839 840 vp = ap->a_vp; 841 dev = NODEV; 842 843 mutex_enter(vp->v_interlock); 844 if (vdead_check(vp, VDEAD_NOWAIT) == 0 && vp->v_specnode != NULL) { 845 dev = vp->v_rdev; 846 } 847 mutex_exit(vp->v_interlock); 848 849 if (dev == NODEV) { 850 return ENXIO; 851 } 852 853 switch (vp->v_type) { 854 case VCHR: 855 // this is not stored for character devices 856 //KASSERT(vp == vp->v_specnode->sn_dev->sd_cdevvp); 857 return cdev_discard(dev, ap->a_pos, ap->a_len); 858 case VBLK: 859 KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp); 860 return bdev_discard(dev, ap->a_pos, ap->a_len); 861 default: 862 panic("spec_fdiscard: not a device\n"); 863 } 864 } 865 866 /* 867 * Device ioctl operation. 868 */ 869 /* ARGSUSED */ 870 int 871 spec_ioctl(void *v) 872 { 873 struct vop_ioctl_args /* { 874 struct vnode *a_vp; 875 u_long a_command; 876 void *a_data; 877 int a_fflag; 878 kauth_cred_t a_cred; 879 } */ *ap = v; 880 struct vnode *vp; 881 dev_t dev; 882 883 /* 884 * Extract all the info we need from the vnode, taking care to 885 * avoid a race with VOP_REVOKE(). 886 */ 887 888 vp = ap->a_vp; 889 dev = NODEV; 890 mutex_enter(vp->v_interlock); 891 if (vdead_check(vp, VDEAD_NOWAIT) == 0 && vp->v_specnode) { 892 dev = vp->v_rdev; 893 } 894 mutex_exit(vp->v_interlock); 895 if (dev == NODEV) { 896 return ENXIO; 897 } 898 899 switch (vp->v_type) { 900 901 case VCHR: 902 return cdev_ioctl(dev, ap->a_command, ap->a_data, 903 ap->a_fflag, curlwp); 904 905 case VBLK: 906 KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp); 907 return bdev_ioctl(dev, ap->a_command, ap->a_data, 908 ap->a_fflag, curlwp); 909 910 default: 911 panic("spec_ioctl"); 912 /* NOTREACHED */ 913 } 914 } 915 916 /* ARGSUSED */ 917 int 918 spec_poll(void *v) 919 { 920 struct vop_poll_args /* { 921 struct vnode *a_vp; 922 int a_events; 923 } */ *ap = v; 924 struct vnode *vp; 925 dev_t dev; 926 927 /* 928 * Extract all the info we need from the vnode, taking care to 929 * avoid a race with VOP_REVOKE(). 930 */ 931 932 vp = ap->a_vp; 933 dev = NODEV; 934 mutex_enter(vp->v_interlock); 935 if (vdead_check(vp, VDEAD_NOWAIT) == 0 && vp->v_specnode) { 936 dev = vp->v_rdev; 937 } 938 mutex_exit(vp->v_interlock); 939 if (dev == NODEV) { 940 return POLLERR; 941 } 942 943 switch (vp->v_type) { 944 945 case VCHR: 946 return cdev_poll(dev, ap->a_events, curlwp); 947 948 default: 949 return (genfs_poll(v)); 950 } 951 } 952 953 /* ARGSUSED */ 954 int 955 spec_kqfilter(void *v) 956 { 957 struct vop_kqfilter_args /* { 958 struct vnode *a_vp; 959 struct proc *a_kn; 960 } */ *ap = v; 961 dev_t dev; 962 963 switch (ap->a_vp->v_type) { 964 965 case VCHR: 966 dev = ap->a_vp->v_rdev; 967 return cdev_kqfilter(dev, ap->a_kn); 968 default: 969 /* 970 * Block devices don't support kqfilter, and refuse it 971 * for any other files (like those vflush()ed) too. 972 */ 973 return (EOPNOTSUPP); 974 } 975 } 976 977 /* 978 * Allow mapping of only D_DISK. This is called only for VBLK. 979 */ 980 int 981 spec_mmap(void *v) 982 { 983 struct vop_mmap_args /* { 984 struct vnode *a_vp; 985 vm_prot_t a_prot; 986 kauth_cred_t a_cred; 987 } */ *ap = v; 988 struct vnode *vp = ap->a_vp; 989 990 KASSERT(vp->v_type == VBLK); 991 if (bdev_type(vp->v_rdev) != D_DISK) 992 return EINVAL; 993 994 return 0; 995 } 996 997 /* 998 * Synch buffers associated with a block device 999 */ 1000 /* ARGSUSED */ 1001 int 1002 spec_fsync(void *v) 1003 { 1004 struct vop_fsync_args /* { 1005 struct vnode *a_vp; 1006 kauth_cred_t a_cred; 1007 int a_flags; 1008 off_t offlo; 1009 off_t offhi; 1010 } */ *ap = v; 1011 struct vnode *vp = ap->a_vp; 1012 struct mount *mp; 1013 int error; 1014 1015 if (vp->v_type == VBLK) { 1016 if ((mp = spec_node_getmountedfs(vp)) != NULL) { 1017 error = VFS_FSYNC(mp, vp, ap->a_flags); 1018 if (error != EOPNOTSUPP) 1019 return error; 1020 } 1021 return vflushbuf(vp, ap->a_flags); 1022 } 1023 return (0); 1024 } 1025 1026 /* 1027 * Just call the device strategy routine 1028 */ 1029 int 1030 spec_strategy(void *v) 1031 { 1032 struct vop_strategy_args /* { 1033 struct vnode *a_vp; 1034 struct buf *a_bp; 1035 } */ *ap = v; 1036 struct vnode *vp = ap->a_vp; 1037 struct buf *bp = ap->a_bp; 1038 dev_t dev; 1039 int error; 1040 1041 dev = NODEV; 1042 1043 /* 1044 * Extract all the info we need from the vnode, taking care to 1045 * avoid a race with VOP_REVOKE(). 1046 */ 1047 1048 mutex_enter(vp->v_interlock); 1049 if (vdead_check(vp, VDEAD_NOWAIT) == 0 && vp->v_specnode != NULL) { 1050 KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp); 1051 dev = vp->v_rdev; 1052 } 1053 mutex_exit(vp->v_interlock); 1054 1055 if (dev == NODEV) { 1056 error = ENXIO; 1057 goto out; 1058 } 1059 bp->b_dev = dev; 1060 1061 if (!(bp->b_flags & B_READ)) { 1062 #ifdef DIAGNOSTIC 1063 if (bp->b_vp && bp->b_vp->v_type == VBLK) { 1064 struct mount *mp = spec_node_getmountedfs(bp->b_vp); 1065 1066 if (mp && (mp->mnt_flag & MNT_RDONLY)) { 1067 printf("%s blk %"PRId64" written while ro!\n", 1068 mp->mnt_stat.f_mntonname, bp->b_blkno); 1069 } 1070 } 1071 #endif /* DIAGNOSTIC */ 1072 error = fscow_run(bp, false); 1073 if (error) 1074 goto out; 1075 } 1076 bdev_strategy(bp); 1077 1078 return 0; 1079 1080 out: 1081 bp->b_error = error; 1082 bp->b_resid = bp->b_bcount; 1083 biodone(bp); 1084 1085 return error; 1086 } 1087 1088 int 1089 spec_inactive(void *v) 1090 { 1091 struct vop_inactive_v2_args /* { 1092 struct vnode *a_vp; 1093 struct bool *a_recycle; 1094 } */ *ap = v; 1095 1096 KASSERT(ap->a_vp->v_mount == dead_rootmount); 1097 *ap->a_recycle = true; 1098 1099 return 0; 1100 } 1101 1102 int 1103 spec_reclaim(void *v) 1104 { 1105 struct vop_reclaim_v2_args /* { 1106 struct vnode *a_vp; 1107 } */ *ap = v; 1108 struct vnode *vp = ap->a_vp; 1109 1110 VOP_UNLOCK(vp); 1111 1112 KASSERT(vp->v_mount == dead_rootmount); 1113 return 0; 1114 } 1115 1116 /* 1117 * This is a noop, simply returning what one has been given. 1118 */ 1119 int 1120 spec_bmap(void *v) 1121 { 1122 struct vop_bmap_args /* { 1123 struct vnode *a_vp; 1124 daddr_t a_bn; 1125 struct vnode **a_vpp; 1126 daddr_t *a_bnp; 1127 int *a_runp; 1128 } */ *ap = v; 1129 1130 if (ap->a_vpp != NULL) 1131 *ap->a_vpp = ap->a_vp; 1132 if (ap->a_bnp != NULL) 1133 *ap->a_bnp = ap->a_bn; 1134 if (ap->a_runp != NULL) 1135 *ap->a_runp = (MAXBSIZE >> DEV_BSHIFT) - 1; 1136 return (0); 1137 } 1138 1139 /* 1140 * Device close routine 1141 */ 1142 /* ARGSUSED */ 1143 int 1144 spec_close(void *v) 1145 { 1146 struct vop_close_args /* { 1147 struct vnode *a_vp; 1148 int a_fflag; 1149 kauth_cred_t a_cred; 1150 } */ *ap = v; 1151 struct vnode *vp = ap->a_vp; 1152 struct session *sess; 1153 dev_t dev = vp->v_rdev; 1154 int flags = ap->a_fflag; 1155 int mode, error, count; 1156 specnode_t *sn; 1157 specdev_t *sd; 1158 1159 mutex_enter(vp->v_interlock); 1160 sn = vp->v_specnode; 1161 sd = sn->sn_dev; 1162 /* 1163 * If we're going away soon, make this non-blocking. 1164 * Also ensures that we won't wedge in vn_lock below. 1165 */ 1166 if (vdead_check(vp, VDEAD_NOWAIT) != 0) 1167 flags |= FNONBLOCK; 1168 mutex_exit(vp->v_interlock); 1169 1170 switch (vp->v_type) { 1171 1172 case VCHR: 1173 /* 1174 * Hack: a tty device that is a controlling terminal 1175 * has a reference from the session structure. We 1176 * cannot easily tell that a character device is a 1177 * controlling terminal, unless it is the closing 1178 * process' controlling terminal. In that case, if the 1179 * open count is 1 release the reference from the 1180 * session. Also, remove the link from the tty back to 1181 * the session and pgrp. 1182 * 1183 * XXX V. fishy. 1184 */ 1185 mutex_enter(&proc_lock); 1186 sess = curlwp->l_proc->p_session; 1187 if (sn->sn_opencnt == 1 && vp == sess->s_ttyvp) { 1188 mutex_spin_enter(&tty_lock); 1189 sess->s_ttyvp = NULL; 1190 if (sess->s_ttyp->t_session != NULL) { 1191 sess->s_ttyp->t_pgrp = NULL; 1192 sess->s_ttyp->t_session = NULL; 1193 mutex_spin_exit(&tty_lock); 1194 /* Releases proc_lock. */ 1195 proc_sessrele(sess); 1196 } else { 1197 mutex_spin_exit(&tty_lock); 1198 if (sess->s_ttyp->t_pgrp != NULL) 1199 panic("spec_close: spurious pgrp ref"); 1200 mutex_exit(&proc_lock); 1201 } 1202 vrele(vp); 1203 } else 1204 mutex_exit(&proc_lock); 1205 1206 /* 1207 * If the vnode is locked, then we are in the midst 1208 * of forcably closing the device, otherwise we only 1209 * close on last reference. 1210 */ 1211 mode = S_IFCHR; 1212 break; 1213 1214 case VBLK: 1215 KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp); 1216 /* 1217 * On last close of a block device (that isn't mounted) 1218 * we must invalidate any in core blocks, so that 1219 * we can, for instance, change floppy disks. 1220 */ 1221 error = vinvalbuf(vp, V_SAVE, ap->a_cred, curlwp, 0, 0); 1222 if (error) 1223 return (error); 1224 /* 1225 * We do not want to really close the device if it 1226 * is still in use unless we are trying to close it 1227 * forcibly. Since every use (buffer, vnode, swap, cmap) 1228 * holds a reference to the vnode, and because we mark 1229 * any other vnodes that alias this device, when the 1230 * sum of the reference counts on all the aliased 1231 * vnodes descends to one, we are on last close. 1232 */ 1233 mode = S_IFBLK; 1234 break; 1235 1236 default: 1237 panic("spec_close: not special"); 1238 } 1239 1240 mutex_enter(&device_lock); 1241 sn->sn_opencnt--; 1242 count = --sd->sd_opencnt; 1243 if (vp->v_type == VBLK) 1244 sd->sd_bdevvp = NULL; 1245 mutex_exit(&device_lock); 1246 1247 if (count != 0 && (vp->v_type != VCHR || !(cdev_flags(dev) & D_MCLOSE))) 1248 return 0; 1249 1250 /* 1251 * If we're able to block, release the vnode lock & reacquire. We 1252 * might end up sleeping for someone else who wants our queues. They 1253 * won't get them if we hold the vnode locked. 1254 */ 1255 if (!(flags & FNONBLOCK)) 1256 VOP_UNLOCK(vp); 1257 1258 if (vp->v_type == VBLK) 1259 error = bdev_close(dev, flags, mode, curlwp); 1260 else 1261 error = cdev_close(dev, flags, mode, curlwp); 1262 1263 if (!(flags & FNONBLOCK)) 1264 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1265 1266 return (error); 1267 } 1268 1269 /* 1270 * Print out the contents of a special device vnode. 1271 */ 1272 int 1273 spec_print(void *v) 1274 { 1275 struct vop_print_args /* { 1276 struct vnode *a_vp; 1277 } */ *ap = v; 1278 1279 printf("dev %llu, %llu\n", (unsigned long long)major(ap->a_vp->v_rdev), 1280 (unsigned long long)minor(ap->a_vp->v_rdev)); 1281 return 0; 1282 } 1283 1284 /* 1285 * Return POSIX pathconf information applicable to special devices. 1286 */ 1287 int 1288 spec_pathconf(void *v) 1289 { 1290 struct vop_pathconf_args /* { 1291 struct vnode *a_vp; 1292 int a_name; 1293 register_t *a_retval; 1294 } */ *ap = v; 1295 1296 switch (ap->a_name) { 1297 case _PC_LINK_MAX: 1298 *ap->a_retval = LINK_MAX; 1299 return (0); 1300 case _PC_MAX_CANON: 1301 *ap->a_retval = MAX_CANON; 1302 return (0); 1303 case _PC_MAX_INPUT: 1304 *ap->a_retval = MAX_INPUT; 1305 return (0); 1306 case _PC_PIPE_BUF: 1307 *ap->a_retval = PIPE_BUF; 1308 return (0); 1309 case _PC_CHOWN_RESTRICTED: 1310 *ap->a_retval = 1; 1311 return (0); 1312 case _PC_VDISABLE: 1313 *ap->a_retval = _POSIX_VDISABLE; 1314 return (0); 1315 case _PC_SYNC_IO: 1316 *ap->a_retval = 1; 1317 return (0); 1318 default: 1319 return genfs_pathconf(ap); 1320 } 1321 /* NOTREACHED */ 1322 } 1323 1324 /* 1325 * Advisory record locking support. 1326 */ 1327 int 1328 spec_advlock(void *v) 1329 { 1330 struct vop_advlock_args /* { 1331 struct vnode *a_vp; 1332 void *a_id; 1333 int a_op; 1334 struct flock *a_fl; 1335 int a_flags; 1336 } */ *ap = v; 1337 struct vnode *vp = ap->a_vp; 1338 1339 return lf_advlock(ap, &vp->v_speclockf, (off_t)0); 1340 } 1341