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